13deb3ec6SMatthias Ringwald /* 23deb3ec6SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 33deb3ec6SMatthias Ringwald * 43deb3ec6SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 53deb3ec6SMatthias Ringwald * modification, are permitted provided that the following conditions 63deb3ec6SMatthias Ringwald * are met: 73deb3ec6SMatthias Ringwald * 83deb3ec6SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 93deb3ec6SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 103deb3ec6SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 113deb3ec6SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 123deb3ec6SMatthias Ringwald * documentation and/or other materials provided with the distribution. 133deb3ec6SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 143deb3ec6SMatthias Ringwald * contributors may be used to endorse or promote products derived 153deb3ec6SMatthias Ringwald * from this software without specific prior written permission. 163deb3ec6SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 173deb3ec6SMatthias Ringwald * personal benefit and not for any commercial purpose or for 183deb3ec6SMatthias Ringwald * monetary gain. 193deb3ec6SMatthias Ringwald * 203deb3ec6SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 213deb3ec6SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 223deb3ec6SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 233deb3ec6SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 243deb3ec6SMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 253deb3ec6SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 263deb3ec6SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 273deb3ec6SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 283deb3ec6SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 293deb3ec6SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 303deb3ec6SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 313deb3ec6SMatthias Ringwald * SUCH DAMAGE. 323deb3ec6SMatthias Ringwald * 333deb3ec6SMatthias Ringwald * Please inquire about commercial licensing options at 343deb3ec6SMatthias Ringwald * [email protected] 353deb3ec6SMatthias Ringwald * 363deb3ec6SMatthias Ringwald */ 373deb3ec6SMatthias Ringwald 38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "att_server.c" 39ab2c6ae4SMatthias Ringwald 403deb3ec6SMatthias Ringwald 413deb3ec6SMatthias Ringwald // 423deb3ec6SMatthias Ringwald // ATT Server Globals 433deb3ec6SMatthias Ringwald // 443deb3ec6SMatthias Ringwald 453deb3ec6SMatthias Ringwald #include <stdint.h> 463deb3ec6SMatthias Ringwald #include <stdio.h> 473deb3ec6SMatthias Ringwald #include <stdlib.h> 483deb3ec6SMatthias Ringwald #include <string.h> 493deb3ec6SMatthias Ringwald #include <inttypes.h> 503deb3ec6SMatthias Ringwald 517907f069SMatthias Ringwald #include "btstack_config.h" 523deb3ec6SMatthias Ringwald 5359c6af15SMatthias Ringwald #include "att_dispatch.h" 5459c6af15SMatthias Ringwald #include "ble/att_db.h" 5559c6af15SMatthias Ringwald #include "ble/att_server.h" 5659c6af15SMatthias Ringwald #include "ble/core.h" 5759c6af15SMatthias Ringwald #include "ble/le_device_db.h" 5859c6af15SMatthias Ringwald #include "ble/sm.h" 5916ece135SMatthias Ringwald #include "btstack_debug.h" 600e2df43fSMatthias Ringwald #include "btstack_event.h" 613deb3ec6SMatthias Ringwald #include "btstack_memory.h" 620e2df43fSMatthias Ringwald #include "btstack_run_loop.h" 6359c6af15SMatthias Ringwald #include "gap.h" 643deb3ec6SMatthias Ringwald #include "hci.h" 653deb3ec6SMatthias Ringwald #include "hci_dump.h" 663deb3ec6SMatthias Ringwald #include "l2cap.h" 67bf78aac6SMatthias Ringwald #include "btstack_tlv.h" 68d1a1f6a4SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 69d1a1f6a4SMatthias Ringwald #include "ble/sm.h" 70d1a1f6a4SMatthias Ringwald #endif 71bf78aac6SMatthias Ringwald 726afb9acaSMatthias Ringwald #ifndef NVN_NUM_GATT_SERVER_CCC 736afb9acaSMatthias Ringwald #define NVN_NUM_GATT_SERVER_CCC 20 74bf78aac6SMatthias Ringwald #endif 753deb3ec6SMatthias Ringwald 7618707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server); 7701ac2008SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle); 7801ac2008SMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server); 79760ad805SMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server); 803deb3ec6SMatthias Ringwald 816a540790SMatthias Ringwald // 826a540790SMatthias Ringwald typedef struct { 836a540790SMatthias Ringwald uint32_t seq_nr; 846a540790SMatthias Ringwald uint16_t att_handle; 856a540790SMatthias Ringwald uint8_t value; 866a540790SMatthias Ringwald uint8_t device_index; 876a540790SMatthias Ringwald } persistent_ccc_entry_t; 886a540790SMatthias Ringwald 8918707219SMatthias Ringwald // global 901a389cdcSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 91406722e4SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration; 923deb3ec6SMatthias Ringwald static btstack_packet_handler_t att_client_packet_handler = NULL; 93bb38f057SMatthias Ringwald static btstack_linked_list_t can_send_now_clients; 943d71c7a4SMatthias Ringwald static btstack_linked_list_t service_handlers; 9518707219SMatthias Ringwald static uint8_t att_client_waiting_for_can_send; 9618707219SMatthias Ringwald 973d71c7a4SMatthias Ringwald static att_read_callback_t att_server_client_read_callback; 983d71c7a4SMatthias Ringwald static att_write_callback_t att_server_client_write_callback; 993d71c7a4SMatthias Ringwald 100bf78aac6SMatthias Ringwald // track CCC 1-entry cache 101bf78aac6SMatthias Ringwald // static att_server_t * att_persistent_ccc_server; 102bf78aac6SMatthias Ringwald // static hci_con_handle_t att_persistent_ccc_con_handle; 103bf78aac6SMatthias Ringwald 10418707219SMatthias Ringwald static att_server_t * att_server_for_handle(hci_con_handle_t con_handle){ 10518707219SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 10618707219SMatthias Ringwald if (!hci_connection) return NULL; 10718707219SMatthias Ringwald return &hci_connection->att_server; 10818707219SMatthias Ringwald } 10918707219SMatthias Ringwald 11002b78fc8SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 11118707219SMatthias Ringwald static att_server_t * att_server_for_state(att_server_state_t state){ 11218707219SMatthias Ringwald btstack_linked_list_iterator_t it; 11318707219SMatthias Ringwald hci_connections_get_iterator(&it); 11418707219SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 11518707219SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 11618707219SMatthias Ringwald att_server_t * att_server = &connection->att_server; 11718707219SMatthias Ringwald if (att_server->state == state) return att_server; 11818707219SMatthias Ringwald } 11918707219SMatthias Ringwald return NULL; 12018707219SMatthias Ringwald } 12102b78fc8SMatthias Ringwald #endif 122bb38f057SMatthias Ringwald 1233deb3ec6SMatthias Ringwald static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){ 1243deb3ec6SMatthias Ringwald if (!att_client_packet_handler) return; 1253deb3ec6SMatthias Ringwald 1263deb3ec6SMatthias Ringwald uint8_t event[7]; 1273deb3ec6SMatthias Ringwald int pos = 0; 1285611a760SMatthias Ringwald event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE; 1293deb3ec6SMatthias Ringwald event[pos++] = sizeof(event) - 2; 1303deb3ec6SMatthias Ringwald event[pos++] = status; 131f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, client_handle); 1323deb3ec6SMatthias Ringwald pos += 2; 133f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, attribute_handle); 1343deb3ec6SMatthias Ringwald (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 1353deb3ec6SMatthias Ringwald } 1363deb3ec6SMatthias Ringwald 137fc64f94aSMatthias Ringwald static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){ 1383deb3ec6SMatthias Ringwald if (!att_client_packet_handler) return; 1393deb3ec6SMatthias Ringwald 1403deb3ec6SMatthias Ringwald uint8_t event[6]; 1413deb3ec6SMatthias Ringwald int pos = 0; 1425611a760SMatthias Ringwald event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE; 1433deb3ec6SMatthias Ringwald event[pos++] = sizeof(event) - 2; 144fc64f94aSMatthias Ringwald little_endian_store_16(event, pos, con_handle); 1453deb3ec6SMatthias Ringwald pos += 2; 146f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, mtu); 147b12646c5SJakob Krantz att_dispatch_server_mtu_exchanged(con_handle, mtu); 1483deb3ec6SMatthias Ringwald (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 1493deb3ec6SMatthias Ringwald } 1503deb3ec6SMatthias Ringwald 1511b96b007SMatthias Ringwald static void att_emit_can_send_now_event(void){ 1523c97b242SMatthias Ringwald if (!att_client_packet_handler) return; 1533c97b242SMatthias Ringwald 1541b96b007SMatthias Ringwald uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0}; 1551b96b007SMatthias Ringwald (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 1561b96b007SMatthias Ringwald } 1571b96b007SMatthias Ringwald 158ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){ 15954178543SMatthias Ringwald void * context = btstack_run_loop_get_timer_context(ts); 16054178543SMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context; 16118707219SMatthias Ringwald att_server_t * att_server = att_server_for_handle(con_handle); 16218707219SMatthias Ringwald if (!att_server) return; 16318707219SMatthias Ringwald uint16_t att_handle = att_server->value_indication_handle; 16418707219SMatthias Ringwald att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_server->connection.con_handle, att_handle); 1653deb3ec6SMatthias Ringwald } 1663deb3ec6SMatthias Ringwald 1673deb3ec6SMatthias Ringwald static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1689ec2630cSMatthias Ringwald 1697dad9ff8SMatthias Ringwald UNUSED(channel); // ok: there is no channel 1707dad9ff8SMatthias Ringwald UNUSED(size); // ok: handling own l2cap events 1713deb3ec6SMatthias Ringwald 17218707219SMatthias Ringwald att_server_t * att_server; 17318707219SMatthias Ringwald hci_con_handle_t con_handle; 17418707219SMatthias Ringwald 1753deb3ec6SMatthias Ringwald switch (packet_type) { 1763deb3ec6SMatthias Ringwald 1773deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 1780e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 1793deb3ec6SMatthias Ringwald 1803deb3ec6SMatthias Ringwald case HCI_EVENT_LE_META: 1813deb3ec6SMatthias Ringwald switch (packet[2]) { 1823deb3ec6SMatthias Ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 18318707219SMatthias Ringwald con_handle = little_endian_read_16(packet, 4); 18418707219SMatthias Ringwald att_server = att_server_for_handle(con_handle); 18518707219SMatthias Ringwald if (!att_server) break; 1863deb3ec6SMatthias Ringwald // store connection info 18718707219SMatthias Ringwald att_server->peer_addr_type = packet[7]; 18818707219SMatthias Ringwald reverse_bd_addr(&packet[8], att_server->peer_address); 18918707219SMatthias Ringwald att_server->connection.con_handle = con_handle; 1903deb3ec6SMatthias Ringwald // reset connection properties 19118707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 19218707219SMatthias Ringwald att_server->connection.mtu = ATT_DEFAULT_MTU; 19318707219SMatthias Ringwald att_server->connection.max_mtu = l2cap_max_le_mtu(); 19418707219SMatthias Ringwald if (att_server->connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){ 19518707219SMatthias Ringwald att_server->connection.max_mtu = ATT_REQUEST_BUFFER_SIZE; 19699c58ad0SMatthias Ringwald } 19718707219SMatthias Ringwald att_server->connection.encryption_key_size = 0; 19818707219SMatthias Ringwald att_server->connection.authenticated = 0; 19918707219SMatthias Ringwald att_server->connection.authorized = 0; 200b2c1e52cSMatthias Ringwald // workaround: identity resolving can already be complete, at least store result 201b2c1e52cSMatthias Ringwald att_server->ir_le_device_db_index = sm_le_device_index(con_handle); 202760ad805SMatthias Ringwald att_server->pairing_active = 0; 2033deb3ec6SMatthias Ringwald break; 2043deb3ec6SMatthias Ringwald 2053deb3ec6SMatthias Ringwald default: 2063deb3ec6SMatthias Ringwald break; 2073deb3ec6SMatthias Ringwald } 2083deb3ec6SMatthias Ringwald break; 2093deb3ec6SMatthias Ringwald 2103deb3ec6SMatthias Ringwald case HCI_EVENT_ENCRYPTION_CHANGE: 2113deb3ec6SMatthias Ringwald case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE: 2123deb3ec6SMatthias Ringwald // check handle 21318707219SMatthias Ringwald con_handle = little_endian_read_16(packet, 3); 21418707219SMatthias Ringwald att_server = att_server_for_handle(con_handle); 21518707219SMatthias Ringwald if (!att_server) break; 2169c6e867eSMatthias Ringwald att_server->connection.encryption_key_size = gap_encryption_key_size(con_handle); 2179c6e867eSMatthias Ringwald att_server->connection.authenticated = gap_authenticated(con_handle); 21801ac2008SMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){ 21901ac2008SMatthias Ringwald // restore CCC values when encrypted 22001ac2008SMatthias Ringwald if (hci_event_encryption_change_get_encryption_enabled(packet)){ 22101ac2008SMatthias Ringwald att_server_persistent_ccc_restore(att_server); 22201ac2008SMatthias Ringwald } 22301ac2008SMatthias Ringwald } 2243deb3ec6SMatthias Ringwald break; 2253deb3ec6SMatthias Ringwald 2263deb3ec6SMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 22770a390c7SMatthias Ringwald // check handle 22818707219SMatthias Ringwald con_handle = hci_event_disconnection_complete_get_connection_handle(packet); 22918707219SMatthias Ringwald att_server = att_server_for_handle(con_handle); 23018707219SMatthias Ringwald if (!att_server) break; 23118707219SMatthias Ringwald att_clear_transaction_queue(&att_server->connection); 23218707219SMatthias Ringwald att_server->connection.con_handle = 0; 23318707219SMatthias Ringwald att_server->value_indication_handle = 0; // reset error state 234760ad805SMatthias Ringwald att_server->pairing_active = 0; 23518707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 2363deb3ec6SMatthias Ringwald break; 2373deb3ec6SMatthias Ringwald 238760ad805SMatthias Ringwald // Identity Resolving 2395611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_STARTED: 24018707219SMatthias Ringwald con_handle = sm_event_identity_resolving_started_get_handle(packet); 24118707219SMatthias Ringwald att_server = att_server_for_handle(con_handle); 24218707219SMatthias Ringwald if (!att_server) break; 2435611a760SMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED"); 24418707219SMatthias Ringwald att_server->ir_lookup_active = 1; 2453deb3ec6SMatthias Ringwald break; 2465611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED: 247760ad805SMatthias Ringwald con_handle = sm_event_identity_created_get_handle(packet); 248760ad805SMatthias Ringwald att_server = att_server_for_handle(con_handle); 249760ad805SMatthias Ringwald if (!att_server) return; 2501b7acd0dSMatthias Ringwald att_server->ir_lookup_active = 0; 2511b7acd0dSMatthias Ringwald att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index(packet); 2521b7acd0dSMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED"); 2531b7acd0dSMatthias Ringwald att_run_for_context(att_server); 2543deb3ec6SMatthias Ringwald break; 2555611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_FAILED: 25618707219SMatthias Ringwald con_handle = sm_event_identity_resolving_failed_get_handle(packet); 25718707219SMatthias Ringwald att_server = att_server_for_handle(con_handle); 25818707219SMatthias Ringwald if (!att_server) break; 2595611a760SMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED"); 26018707219SMatthias Ringwald att_server->ir_lookup_active = 0; 26118707219SMatthias Ringwald att_server->ir_le_device_db_index = -1; 26218707219SMatthias Ringwald att_run_for_context(att_server); 2633deb3ec6SMatthias Ringwald break; 264760ad805SMatthias Ringwald 265760ad805SMatthias Ringwald // Pairing started - delete stored CCC values 266760ad805SMatthias Ringwald // - assumes pairing indicates either new device or re-pairing, in both cases there should be no stored CCC values 267760ad805SMatthias Ringwald // - assumes that all events have the con handle as the first field 268760ad805SMatthias Ringwald case SM_EVENT_JUST_WORKS_REQUEST: 269760ad805SMatthias Ringwald case SM_EVENT_PASSKEY_DISPLAY_NUMBER: 270760ad805SMatthias Ringwald case SM_EVENT_PASSKEY_INPUT_NUMBER: 271760ad805SMatthias Ringwald case SM_EVENT_NUMERIC_COMPARISON_REQUEST: 272760ad805SMatthias Ringwald con_handle = sm_event_just_works_request_get_handle(packet); 273760ad805SMatthias Ringwald att_server = att_server_for_handle(con_handle); 274760ad805SMatthias Ringwald if (!att_server) break; 275760ad805SMatthias Ringwald att_server->pairing_active = 1; 276760ad805SMatthias Ringwald log_info("SM Pairing started"); 277760ad805SMatthias Ringwald if (att_server->ir_le_device_db_index < 0) break; 278760ad805SMatthias Ringwald att_server_persistent_ccc_clear(att_server); 279760ad805SMatthias Ringwald // index not valid anymore 280760ad805SMatthias Ringwald att_server->ir_le_device_db_index = -1; 281760ad805SMatthias Ringwald break; 282760ad805SMatthias Ringwald 2831b7acd0dSMatthias Ringwald // Bonding completed 284760ad805SMatthias Ringwald case SM_EVENT_IDENTITY_CREATED: 285760ad805SMatthias Ringwald con_handle = sm_event_identity_created_get_handle(packet); 286760ad805SMatthias Ringwald att_server = att_server_for_handle(con_handle); 287760ad805SMatthias Ringwald if (!att_server) return; 288760ad805SMatthias Ringwald att_server->pairing_active = 0; 2891b7acd0dSMatthias Ringwald att_server->ir_le_device_db_index = sm_event_identity_created_get_index(packet); 2901b7acd0dSMatthias Ringwald att_run_for_context(att_server); 2911b7acd0dSMatthias Ringwald break; 2921b7acd0dSMatthias Ringwald 2931b7acd0dSMatthias Ringwald // Pairing complete (with/without bonding=storing of pairing information) 2941b7acd0dSMatthias Ringwald case SM_EVENT_PAIRING_COMPLETE: 2951b7acd0dSMatthias Ringwald con_handle = sm_event_pairing_complete_get_handle(packet); 2961b7acd0dSMatthias Ringwald att_server = att_server_for_handle(con_handle); 2971b7acd0dSMatthias Ringwald if (!att_server) return; 2981b7acd0dSMatthias Ringwald att_server->pairing_active = 0; 2991b7acd0dSMatthias Ringwald att_run_for_context(att_server); 300760ad805SMatthias Ringwald break; 301760ad805SMatthias Ringwald 302760ad805SMatthias Ringwald // Authorization 3035611a760SMatthias Ringwald case SM_EVENT_AUTHORIZATION_RESULT: { 30418707219SMatthias Ringwald con_handle = sm_event_authorization_result_get_handle(packet); 30518707219SMatthias Ringwald att_server = att_server_for_handle(con_handle); 30618707219SMatthias Ringwald if (!att_server) break; 30718707219SMatthias Ringwald att_server->connection.authorized = sm_event_authorization_result_get_authorization_result(packet); 30818707219SMatthias Ringwald att_dispatch_server_request_can_send_now_event(con_handle); 3093deb3ec6SMatthias Ringwald break; 3103deb3ec6SMatthias Ringwald } 3113deb3ec6SMatthias Ringwald default: 3123deb3ec6SMatthias Ringwald break; 3133deb3ec6SMatthias Ringwald } 31465b44ffdSMatthias Ringwald break; 31565b44ffdSMatthias Ringwald default: 31665b44ffdSMatthias Ringwald break; 3173deb3ec6SMatthias Ringwald } 3183deb3ec6SMatthias Ringwald } 3193deb3ec6SMatthias Ringwald 3207a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 3213deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){ 3223deb3ec6SMatthias Ringwald 32318707219SMatthias Ringwald att_server_t * att_server = att_server_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION); 32418707219SMatthias Ringwald if (!att_server) return; 3253deb3ec6SMatthias Ringwald 3263deb3ec6SMatthias Ringwald uint8_t hash_flipped[8]; 3279c80e4ccSMatthias Ringwald reverse_64(hash, hash_flipped); 32818707219SMatthias Ringwald if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8)){ 3293deb3ec6SMatthias Ringwald log_info("ATT Signed Write, invalid signature"); 33018707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 3313deb3ec6SMatthias Ringwald return; 3323deb3ec6SMatthias Ringwald } 3333deb3ec6SMatthias Ringwald log_info("ATT Signed Write, valid signature"); 3343deb3ec6SMatthias Ringwald 3353deb3ec6SMatthias Ringwald // update sequence number 33618707219SMatthias Ringwald uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12); 33718707219SMatthias Ringwald le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1); 33818707219SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 33918707219SMatthias Ringwald att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle); 3403deb3ec6SMatthias Ringwald } 3417a766ebfSMatthias Ringwald #endif 34288a48dd8SMatthias Ringwald 34318707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED 344b06f2579SMatthias Ringwald // pre: can send now 345b06f2579SMatthias Ringwald // returns: 1 if packet was sent 34618707219SMatthias Ringwald static int att_server_process_validated_request(att_server_t * att_server){ 347b06f2579SMatthias Ringwald 348b06f2579SMatthias Ringwald l2cap_reserve_packet_buffer(); 349b06f2579SMatthias Ringwald uint8_t * att_response_buffer = l2cap_get_outgoing_buffer(); 35018707219SMatthias Ringwald uint16_t att_response_size = att_handle_request(&att_server->connection, att_server->request_buffer, att_server->request_size, att_response_buffer); 351b06f2579SMatthias Ringwald 352e404a688SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_READ_RESPONSE 353e404a688SMatthias Ringwald if (att_response_size == ATT_READ_RESPONSE_PENDING){ 354e404a688SMatthias Ringwald // update state 355e404a688SMatthias Ringwald att_server->state = ATT_SERVER_READ_RESPONSE_PENDING; 356e404a688SMatthias Ringwald 357e404a688SMatthias Ringwald // callback with handle ATT_READ_RESPONSE_PENDING 358e404a688SMatthias Ringwald att_server_client_read_callback(att_server->connection.con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0); 359e8e7403eSMatthias Ringwald 360e8e7403eSMatthias Ringwald // free reserved buffer 361e8e7403eSMatthias Ringwald l2cap_release_packet_buffer(); 362e8e7403eSMatthias Ringwald return 0; 363e404a688SMatthias Ringwald } 364e404a688SMatthias Ringwald #endif 365e404a688SMatthias Ringwald 366b06f2579SMatthias Ringwald // intercept "insufficient authorization" for authenticated connections to allow for user authorization 367b06f2579SMatthias Ringwald if ((att_response_size >= 4) 368b06f2579SMatthias Ringwald && (att_response_buffer[0] == ATT_ERROR_RESPONSE) 369b06f2579SMatthias Ringwald && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION) 37018707219SMatthias Ringwald && (att_server->connection.authenticated)){ 371b06f2579SMatthias Ringwald 3729c6e867eSMatthias Ringwald switch (gap_authorization_state(att_server->connection.con_handle)){ 373b06f2579SMatthias Ringwald case AUTHORIZATION_UNKNOWN: 374b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 37518707219SMatthias Ringwald sm_request_pairing(att_server->connection.con_handle); 376b06f2579SMatthias Ringwald return 0; 377b06f2579SMatthias Ringwald case AUTHORIZATION_PENDING: 378b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 379b06f2579SMatthias Ringwald return 0; 380b06f2579SMatthias Ringwald default: 381b06f2579SMatthias Ringwald break; 382b06f2579SMatthias Ringwald } 383b06f2579SMatthias Ringwald } 384b06f2579SMatthias Ringwald 38518707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 386b06f2579SMatthias Ringwald if (att_response_size == 0) { 387b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 388b06f2579SMatthias Ringwald return 0; 389b06f2579SMatthias Ringwald } 390b06f2579SMatthias Ringwald 39118707219SMatthias Ringwald l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size); 392b06f2579SMatthias Ringwald 393b06f2579SMatthias Ringwald // notify client about MTU exchange result 394b06f2579SMatthias Ringwald if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){ 39518707219SMatthias Ringwald att_emit_mtu_event(att_server->connection.con_handle, att_server->connection.mtu); 396b06f2579SMatthias Ringwald } 397b06f2579SMatthias Ringwald return 1; 398b06f2579SMatthias Ringwald } 399b06f2579SMatthias Ringwald 400e404a688SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_READ_RESPONSE 401e404a688SMatthias Ringwald int att_server_read_response_ready(hci_con_handle_t con_handle){ 402e404a688SMatthias Ringwald att_server_t * att_server = att_server_for_handle(con_handle); 403e404a688SMatthias Ringwald if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 404e404a688SMatthias Ringwald if (att_server->state != ATT_SERVER_READ_RESPONSE_PENDING) return ERROR_CODE_COMMAND_DISALLOWED; 405e404a688SMatthias Ringwald 406e404a688SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 407e404a688SMatthias Ringwald att_dispatch_server_request_can_send_now_event(con_handle); 408e404a688SMatthias Ringwald return ERROR_CODE_SUCCESS; 409e404a688SMatthias Ringwald } 410e404a688SMatthias Ringwald #endif 411e404a688SMatthias Ringwald 41218707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server){ 41318707219SMatthias Ringwald switch (att_server->state){ 4143deb3ec6SMatthias Ringwald case ATT_SERVER_REQUEST_RECEIVED: 415760ad805SMatthias Ringwald 416760ad805SMatthias Ringwald // wait until pairing is complete 417760ad805SMatthias Ringwald if (att_server->pairing_active) break; 418760ad805SMatthias Ringwald 4197a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 42018707219SMatthias Ringwald if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){ 4213deb3ec6SMatthias Ringwald log_info("ATT Signed Write!"); 4223deb3ec6SMatthias Ringwald if (!sm_cmac_ready()) { 4233deb3ec6SMatthias Ringwald log_info("ATT Signed Write, sm_cmac engine not ready. Abort"); 42418707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 4253deb3ec6SMatthias Ringwald return; 4263deb3ec6SMatthias Ringwald } 42718707219SMatthias Ringwald if (att_server->request_size < (3 + 12)) { 4283deb3ec6SMatthias Ringwald log_info("ATT Signed Write, request to short. Abort."); 42918707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 4303deb3ec6SMatthias Ringwald return; 4313deb3ec6SMatthias Ringwald } 43218707219SMatthias Ringwald if (att_server->ir_lookup_active){ 4333deb3ec6SMatthias Ringwald return; 4343deb3ec6SMatthias Ringwald } 43518707219SMatthias Ringwald if (att_server->ir_le_device_db_index < 0){ 4363deb3ec6SMatthias Ringwald log_info("ATT Signed Write, CSRK not available"); 43718707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 4383deb3ec6SMatthias Ringwald return; 4393deb3ec6SMatthias Ringwald } 4403deb3ec6SMatthias Ringwald 4413deb3ec6SMatthias Ringwald // check counter 44218707219SMatthias Ringwald uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12); 44318707219SMatthias Ringwald uint32_t counter_db = le_device_db_remote_counter_get(att_server->ir_le_device_db_index); 4443deb3ec6SMatthias Ringwald log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet); 4453deb3ec6SMatthias Ringwald if (counter_packet < counter_db){ 4463deb3ec6SMatthias Ringwald log_info("ATT Signed Write, db reports higher counter, abort"); 44718707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 4483deb3ec6SMatthias Ringwald return; 4493deb3ec6SMatthias Ringwald } 4503deb3ec6SMatthias Ringwald 4513deb3ec6SMatthias Ringwald // signature is { sequence counter, secure hash } 4523deb3ec6SMatthias Ringwald sm_key_t csrk; 45318707219SMatthias Ringwald le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk); 45418707219SMatthias Ringwald att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION; 4553deb3ec6SMatthias Ringwald log_info("Orig Signature: "); 45618707219SMatthias Ringwald log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8); 45718707219SMatthias Ringwald uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1); 45818707219SMatthias Ringwald sm_cmac_signed_write_start(csrk, att_server->request_buffer[0], attribute_handle, att_server->request_size - 15, &att_server->request_buffer[3], counter_packet, att_signed_write_handle_cmac_result); 4593deb3ec6SMatthias Ringwald return; 4603deb3ec6SMatthias Ringwald } 4617a766ebfSMatthias Ringwald #endif 462b06f2579SMatthias Ringwald // move on 46318707219SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 46418707219SMatthias Ringwald att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle); 465b06f2579SMatthias Ringwald break; 46688a48dd8SMatthias Ringwald 4673deb3ec6SMatthias Ringwald default: 4683deb3ec6SMatthias Ringwald break; 4693deb3ec6SMatthias Ringwald } 4703deb3ec6SMatthias Ringwald } 4713deb3ec6SMatthias Ringwald 4727eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){ 473b06f2579SMatthias Ringwald 474b06f2579SMatthias Ringwald // NOTE: we get l2cap fixed channel instead of con_handle 475b06f2579SMatthias Ringwald 47618707219SMatthias Ringwald btstack_linked_list_iterator_t it; 47718707219SMatthias Ringwald hci_connections_get_iterator(&it); 47818707219SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 47918707219SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 48018707219SMatthias Ringwald att_server_t * att_server = &connection->att_server; 48118707219SMatthias Ringwald if (att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED){ 48218707219SMatthias Ringwald int sent = att_server_process_validated_request(att_server); 483bb38f057SMatthias Ringwald if (sent && (att_client_waiting_for_can_send || !btstack_linked_list_empty(&can_send_now_clients))){ 48418707219SMatthias Ringwald att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle); 4853deb3ec6SMatthias Ringwald return; 4863deb3ec6SMatthias Ringwald } 4873deb3ec6SMatthias Ringwald } 48818707219SMatthias Ringwald } 4893deb3ec6SMatthias Ringwald 490bb38f057SMatthias Ringwald while (!btstack_linked_list_empty(&can_send_now_clients)){ 491bb38f057SMatthias Ringwald // handle first client 492bb38f057SMatthias Ringwald btstack_context_callback_registration_t * client = (btstack_context_callback_registration_t*) can_send_now_clients; 49318707219SMatthias Ringwald hci_con_handle_t con_handle = (uintptr_t) client->context; 494bb38f057SMatthias Ringwald btstack_linked_list_remove(&can_send_now_clients, (btstack_linked_item_t *) client); 495bb38f057SMatthias Ringwald client->callback(client->context); 496bb38f057SMatthias Ringwald 497bb38f057SMatthias Ringwald // request again if needed 49818707219SMatthias Ringwald if (!att_dispatch_server_can_send_now(con_handle)){ 499bb38f057SMatthias Ringwald if (!btstack_linked_list_empty(&can_send_now_clients) || att_client_waiting_for_can_send){ 50018707219SMatthias Ringwald att_dispatch_server_request_can_send_now_event(con_handle); 501bb38f057SMatthias Ringwald } 502bb38f057SMatthias Ringwald return; 503bb38f057SMatthias Ringwald } 504bb38f057SMatthias Ringwald } 505bb38f057SMatthias Ringwald 506b06f2579SMatthias Ringwald if (att_client_waiting_for_can_send){ 507b06f2579SMatthias Ringwald att_client_waiting_for_can_send = 0; 508b06f2579SMatthias Ringwald att_emit_can_send_now_event(); 5093deb3ec6SMatthias Ringwald } 5103deb3ec6SMatthias Ringwald } 5113deb3ec6SMatthias Ringwald 51288a48dd8SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){ 51318707219SMatthias Ringwald 51418707219SMatthias Ringwald att_server_t * att_server; 51518707219SMatthias Ringwald 51688a48dd8SMatthias Ringwald switch (packet_type){ 51788a48dd8SMatthias Ringwald 51888a48dd8SMatthias Ringwald case HCI_EVENT_PACKET: 519bd87a16eSMatthias Ringwald switch (packet[0]){ 520bd87a16eSMatthias Ringwald case L2CAP_EVENT_CAN_SEND_NOW: 5214d2be802SMatthias Ringwald att_server_handle_can_send_now(); 522372d62c4SMatthias Ringwald break; 523bd87a16eSMatthias Ringwald case ATT_EVENT_MTU_EXCHANGE_COMPLETE: 524bd87a16eSMatthias Ringwald // GATT client has negotiated the mtu for this connection 5256155bae0SMatthias Ringwald att_server = att_server_for_handle(handle); 5266155bae0SMatthias Ringwald if (!att_server) break; 527bd87a16eSMatthias Ringwald att_server->connection.mtu = little_endian_read_16(packet, 4); 528bd87a16eSMatthias Ringwald break; 529bd87a16eSMatthias Ringwald default: 530bd87a16eSMatthias Ringwald break; 531bd87a16eSMatthias Ringwald } 532bd87a16eSMatthias Ringwald break; 5333deb3ec6SMatthias Ringwald 534372d62c4SMatthias Ringwald case ATT_DATA_PACKET: 535f50ef7ddSMatthias Ringwald log_debug("ATT Packet, handle 0x%04x", handle); 53618707219SMatthias Ringwald att_server = att_server_for_handle(handle); 53718707219SMatthias Ringwald if (!att_server) break; 53818707219SMatthias Ringwald 5393deb3ec6SMatthias Ringwald // handle value indication confirms 54018707219SMatthias Ringwald if (packet[0] == ATT_HANDLE_VALUE_CONFIRMATION && att_server->value_indication_handle){ 54118707219SMatthias Ringwald btstack_run_loop_remove_timer(&att_server->value_indication_timer); 54218707219SMatthias Ringwald uint16_t att_handle = att_server->value_indication_handle; 54318707219SMatthias Ringwald att_server->value_indication_handle = 0; 54418707219SMatthias Ringwald att_handle_value_indication_notify_client(0, att_server->connection.con_handle, att_handle); 5453deb3ec6SMatthias Ringwald return; 5463deb3ec6SMatthias Ringwald } 5473deb3ec6SMatthias Ringwald 5486428eb5aSMatthias Ringwald // directly process command 5496428eb5aSMatthias Ringwald // note: signed write cannot be handled directly as authentication needs to be verified 5506428eb5aSMatthias Ringwald if (packet[0] == ATT_WRITE_COMMAND){ 55118707219SMatthias Ringwald att_handle_request(&att_server->connection, packet, size, 0); 5526428eb5aSMatthias Ringwald return; 5536428eb5aSMatthias Ringwald } 5546428eb5aSMatthias Ringwald 5553deb3ec6SMatthias Ringwald // check size 55618707219SMatthias Ringwald if (size > sizeof(att_server->request_buffer)) { 55718707219SMatthias Ringwald log_info("att_packet_handler: dropping att pdu 0x%02x as size %u > att_server->request_buffer %u", packet[0], size, (int) sizeof(att_server->request_buffer)); 5586428eb5aSMatthias Ringwald return; 5596428eb5aSMatthias Ringwald } 5603deb3ec6SMatthias Ringwald 5613deb3ec6SMatthias Ringwald // last request still in processing? 56218707219SMatthias Ringwald if (att_server->state != ATT_SERVER_IDLE){ 56318707219SMatthias Ringwald log_info("att_packet_handler: skipping att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state); 5646428eb5aSMatthias Ringwald return; 5656428eb5aSMatthias Ringwald } 5663deb3ec6SMatthias Ringwald 5673deb3ec6SMatthias Ringwald // store request 56818707219SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED; 56918707219SMatthias Ringwald att_server->request_size = size; 57018707219SMatthias Ringwald memcpy(att_server->request_buffer, packet, size); 5713deb3ec6SMatthias Ringwald 57218707219SMatthias Ringwald att_run_for_context(att_server); 573372d62c4SMatthias Ringwald break; 574372d62c4SMatthias Ringwald } 5753deb3ec6SMatthias Ringwald } 5763deb3ec6SMatthias Ringwald 577bf78aac6SMatthias Ringwald // --------------------- 578bf78aac6SMatthias Ringwald // persistent CCC writes 579bf78aac6SMatthias Ringwald static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){ 580bf78aac6SMatthias Ringwald return 'B' << 24 | 'T' << 16 | 'C' << 8 | index; 581bf78aac6SMatthias Ringwald } 582bf78aac6SMatthias Ringwald 583bf78aac6SMatthias Ringwald static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){ 584bf78aac6SMatthias Ringwald // lookup att_server instance 585bf78aac6SMatthias Ringwald att_server_t * att_server = att_server_for_handle(con_handle); 586bf78aac6SMatthias Ringwald if (!att_server) return; 587bf78aac6SMatthias Ringwald int le_device_index = att_server->ir_le_device_db_index; 588bf78aac6SMatthias Ringwald log_info("Store CCC value 0x%04x for handle 0x%04x of remote %s, le device id %d", value, att_handle, bd_addr_to_str(att_server->peer_address), le_device_index); 5896a540790SMatthias Ringwald 590bf78aac6SMatthias Ringwald // check if bonded 591bf78aac6SMatthias Ringwald if (le_device_index < 0) return; 5926a540790SMatthias Ringwald 593bf78aac6SMatthias Ringwald // get btstack_tlv 594bf78aac6SMatthias Ringwald const btstack_tlv_t * tlv_impl = NULL; 595bf78aac6SMatthias Ringwald void * tlv_context; 596bf78aac6SMatthias Ringwald btstack_tlv_get_instance(&tlv_impl, &tlv_context); 597bf78aac6SMatthias Ringwald if (!tlv_impl) return; 5986a540790SMatthias Ringwald 599bf78aac6SMatthias Ringwald // update ccc tag 600bf78aac6SMatthias Ringwald int index; 6016a540790SMatthias Ringwald uint32_t highest_seq_nr = 0; 6026a540790SMatthias Ringwald uint32_t lowest_seq_nr = 0; 6036a540790SMatthias Ringwald uint32_t tag_for_lowest_seq_nr = 0; 6046a540790SMatthias Ringwald uint32_t tag_for_empty = 0; 6056a540790SMatthias Ringwald persistent_ccc_entry_t entry; 6066afb9acaSMatthias Ringwald for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){ 607bf78aac6SMatthias Ringwald uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 6086a540790SMatthias Ringwald int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 6096a540790SMatthias Ringwald 6106a540790SMatthias Ringwald // empty/invalid tag 6116a540790SMatthias Ringwald if (len != sizeof(persistent_ccc_entry_t)){ 6126a540790SMatthias Ringwald tag_for_empty = tag; 613bf78aac6SMatthias Ringwald continue; 614bf78aac6SMatthias Ringwald } 6156a540790SMatthias Ringwald // update highest seq nr 6166a540790SMatthias Ringwald if (entry.seq_nr > highest_seq_nr){ 6176a540790SMatthias Ringwald highest_seq_nr = entry.seq_nr; 6186a540790SMatthias Ringwald } 6196a540790SMatthias Ringwald // find entry with lowest seq nr 6206a540790SMatthias Ringwald if ((tag_for_lowest_seq_nr == 0) || (entry.seq_nr < lowest_seq_nr)){ 6216a540790SMatthias Ringwald tag_for_lowest_seq_nr = tag; 6226a540790SMatthias Ringwald lowest_seq_nr = entry.seq_nr; 6236a540790SMatthias Ringwald } 6246a540790SMatthias Ringwald 6256a540790SMatthias Ringwald if (entry.device_index != le_device_index) continue; 6266a540790SMatthias Ringwald if (entry.att_handle != att_handle) continue; 6276a540790SMatthias Ringwald 6286a540790SMatthias Ringwald // found matching entry 62901ac2008SMatthias Ringwald if (value){ 630bf78aac6SMatthias Ringwald // update 6316a540790SMatthias Ringwald if (entry.value == value) { 632760ad805SMatthias Ringwald log_info("CCC Index %u: Up-to-date", index); 633760ad805SMatthias Ringwald return; 634760ad805SMatthias Ringwald } 6356a540790SMatthias Ringwald entry.value = value; 6366a540790SMatthias Ringwald entry.seq_nr = highest_seq_nr + 1; 637760ad805SMatthias Ringwald log_info("CCC Index %u: Store", index); 6386a540790SMatthias Ringwald tlv_impl->store_tag(tlv_context, tag, (const uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 63901ac2008SMatthias Ringwald } else { 64001ac2008SMatthias Ringwald // delete 641760ad805SMatthias Ringwald log_info("CCC Index %u: Delete", index); 64201ac2008SMatthias Ringwald tlv_impl->delete_tag(tlv_context, tag); 64301ac2008SMatthias Ringwald } 644bf78aac6SMatthias Ringwald return; 645bf78aac6SMatthias Ringwald } 6466a540790SMatthias Ringwald 647faf2b6c3SMatthias Ringwald log_info("tag_for_empy %"PRIx32", tag_for_lowest_seq_nr %"PRIx32, tag_for_empty, tag_for_lowest_seq_nr); 6486a540790SMatthias Ringwald 6496a540790SMatthias Ringwald if (value == 0){ 6506a540790SMatthias Ringwald // done 6516a540790SMatthias Ringwald return; 6526a540790SMatthias Ringwald } 6536a540790SMatthias Ringwald 6546a540790SMatthias Ringwald uint32_t tag_to_use = 0; 6556a540790SMatthias Ringwald if (tag_for_empty){ 6566a540790SMatthias Ringwald tag_to_use = tag_for_empty; 6576a540790SMatthias Ringwald } else if (tag_for_lowest_seq_nr){ 6586a540790SMatthias Ringwald tag_to_use = tag_for_lowest_seq_nr; 6596a540790SMatthias Ringwald } else { 6606a540790SMatthias Ringwald // should not happen 6616a540790SMatthias Ringwald return; 6626a540790SMatthias Ringwald } 663bf78aac6SMatthias Ringwald // store ccc tag 6646a540790SMatthias Ringwald entry.seq_nr = highest_seq_nr + 1; 6656a540790SMatthias Ringwald entry.device_index = le_device_index; 6666a540790SMatthias Ringwald entry.att_handle = att_handle; 6676a540790SMatthias Ringwald entry.value = value; 6686a540790SMatthias Ringwald tlv_impl->store_tag(tlv_context, tag_to_use, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 669bf78aac6SMatthias Ringwald } 670bf78aac6SMatthias Ringwald 671760ad805SMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server){ 672760ad805SMatthias Ringwald if (!att_server) return; 673760ad805SMatthias Ringwald int le_device_index = att_server->ir_le_device_db_index; 674760ad805SMatthias Ringwald log_info("Clear CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index); 675760ad805SMatthias Ringwald // check if bonded 676760ad805SMatthias Ringwald if (le_device_index < 0) return; 677760ad805SMatthias Ringwald // get btstack_tlv 678760ad805SMatthias Ringwald const btstack_tlv_t * tlv_impl = NULL; 679760ad805SMatthias Ringwald void * tlv_context; 680760ad805SMatthias Ringwald btstack_tlv_get_instance(&tlv_impl, &tlv_context); 681760ad805SMatthias Ringwald if (!tlv_impl) return; 682760ad805SMatthias Ringwald // get all ccc tag 683760ad805SMatthias Ringwald int index; 6846a540790SMatthias Ringwald persistent_ccc_entry_t entry; 6856afb9acaSMatthias Ringwald for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){ 686760ad805SMatthias Ringwald uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 6876a540790SMatthias Ringwald int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 6886a540790SMatthias Ringwald if (len != sizeof(persistent_ccc_entry_t)) continue; 6896a540790SMatthias Ringwald if (entry.device_index != le_device_index) continue; 690760ad805SMatthias Ringwald // delete entry 691760ad805SMatthias Ringwald log_info("CCC Index %u: Delete", index); 692760ad805SMatthias Ringwald tlv_impl->delete_tag(tlv_context, tag); 693760ad805SMatthias Ringwald } 694760ad805SMatthias Ringwald } 695760ad805SMatthias Ringwald 69601ac2008SMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server){ 69701ac2008SMatthias Ringwald if (!att_server) return; 69801ac2008SMatthias Ringwald int le_device_index = att_server->ir_le_device_db_index; 69901ac2008SMatthias Ringwald log_info("Restore CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index); 7001b7acd0dSMatthias Ringwald // check if bonded 7011b7acd0dSMatthias Ringwald if (le_device_index < 0) return; 70201ac2008SMatthias Ringwald // get btstack_tlv 70301ac2008SMatthias Ringwald const btstack_tlv_t * tlv_impl = NULL; 70401ac2008SMatthias Ringwald void * tlv_context; 70501ac2008SMatthias Ringwald btstack_tlv_get_instance(&tlv_impl, &tlv_context); 70601ac2008SMatthias Ringwald if (!tlv_impl) return; 70701ac2008SMatthias Ringwald // get all ccc tag 70801ac2008SMatthias Ringwald int index; 7096a540790SMatthias Ringwald persistent_ccc_entry_t entry; 7106afb9acaSMatthias Ringwald for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){ 71101ac2008SMatthias Ringwald uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 7126a540790SMatthias Ringwald int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 7136a540790SMatthias Ringwald if (len != sizeof(persistent_ccc_entry_t)) continue; 7146a540790SMatthias Ringwald if (entry.device_index != le_device_index) continue; 71501ac2008SMatthias Ringwald // simulate write callback 7166a540790SMatthias Ringwald uint16_t attribute_handle = entry.att_handle; 71701ac2008SMatthias Ringwald uint8_t value[2]; 7186a540790SMatthias Ringwald little_endian_store_16(value, 0, entry.value); 71901ac2008SMatthias Ringwald att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle); 72001ac2008SMatthias Ringwald if (!callback) continue; 7216a540790SMatthias Ringwald log_info("CCC Index %u: Set Attribute handle 0x%04x to value 0x%04x", index, attribute_handle, entry.value ); 72201ac2008SMatthias Ringwald (*callback)(att_server->connection.con_handle, attribute_handle, ATT_TRANSACTION_MODE_NONE, 0, value, sizeof(value)); 72301ac2008SMatthias Ringwald } 72401ac2008SMatthias Ringwald } 72501ac2008SMatthias Ringwald 726bf78aac6SMatthias Ringwald // persistent CCC writes 727bf78aac6SMatthias Ringwald // --------------------- 7283d71c7a4SMatthias Ringwald 7293d71c7a4SMatthias Ringwald // gatt service management 7303d71c7a4SMatthias Ringwald static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){ 7313d71c7a4SMatthias Ringwald btstack_linked_list_iterator_t it; 7323d71c7a4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 7333d71c7a4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 7343d71c7a4SMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 7353d71c7a4SMatthias Ringwald if (handler->start_handle > handle) continue; 7363d71c7a4SMatthias Ringwald if (handler->end_handle < handle) continue; 7373d71c7a4SMatthias Ringwald return handler; 7383d71c7a4SMatthias Ringwald } 7393d71c7a4SMatthias Ringwald return NULL; 7403d71c7a4SMatthias Ringwald } 7413d71c7a4SMatthias Ringwald static att_read_callback_t att_server_read_callback_for_handle(uint16_t handle){ 7423d71c7a4SMatthias Ringwald att_service_handler_t * handler = att_service_handler_for_handle(handle); 7433d71c7a4SMatthias Ringwald if (handler) return handler->read_callback; 7443d71c7a4SMatthias Ringwald return att_server_client_read_callback; 7453d71c7a4SMatthias Ringwald } 7463d71c7a4SMatthias Ringwald 7473d71c7a4SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){ 7483d71c7a4SMatthias Ringwald att_service_handler_t * handler = att_service_handler_for_handle(handle); 7493d71c7a4SMatthias Ringwald if (handler) return handler->write_callback; 7503d71c7a4SMatthias Ringwald return att_server_client_write_callback; 7513d71c7a4SMatthias Ringwald } 7523d71c7a4SMatthias Ringwald 7533d71c7a4SMatthias Ringwald static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){ 7543d71c7a4SMatthias Ringwald // notify all callbacks 7553d71c7a4SMatthias Ringwald btstack_linked_list_iterator_t it; 7563d71c7a4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 7573d71c7a4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 7583d71c7a4SMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 7593d71c7a4SMatthias Ringwald if (!handler->write_callback) continue; 7603d71c7a4SMatthias Ringwald (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0); 7613d71c7a4SMatthias Ringwald } 7623d71c7a4SMatthias Ringwald if (!att_server_client_write_callback) return; 7633d71c7a4SMatthias Ringwald (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0); 7643d71c7a4SMatthias Ringwald } 7653d71c7a4SMatthias Ringwald 7663d71c7a4SMatthias Ringwald // returns first reported error or 0 7673d71c7a4SMatthias Ringwald static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){ 7683d71c7a4SMatthias Ringwald btstack_linked_list_iterator_t it; 7693d71c7a4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 7703d71c7a4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 7713d71c7a4SMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 7723d71c7a4SMatthias Ringwald if (!handler->write_callback) continue; 7733d71c7a4SMatthias Ringwald uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0); 7743d71c7a4SMatthias Ringwald if (error_code) return error_code; 7753d71c7a4SMatthias Ringwald } 7763d71c7a4SMatthias Ringwald if (!att_server_client_write_callback) return 0; 7773d71c7a4SMatthias Ringwald return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0); 7783d71c7a4SMatthias Ringwald } 7793d71c7a4SMatthias Ringwald 7803d71c7a4SMatthias Ringwald static uint16_t att_server_read_callback(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){ 7813d71c7a4SMatthias Ringwald att_read_callback_t callback = att_server_read_callback_for_handle(attribute_handle); 78201ac2008SMatthias Ringwald if (!callback) return 0; 7833d71c7a4SMatthias Ringwald return (*callback)(con_handle, attribute_handle, offset, buffer, buffer_size); 7843d71c7a4SMatthias Ringwald } 7853d71c7a4SMatthias Ringwald 7863d71c7a4SMatthias Ringwald static int att_server_write_callback(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size){ 7873d71c7a4SMatthias Ringwald switch (transaction_mode){ 7883d71c7a4SMatthias Ringwald case ATT_TRANSACTION_MODE_VALIDATE: 7893d71c7a4SMatthias Ringwald return att_validate_prepared_write(con_handle); 7903d71c7a4SMatthias Ringwald case ATT_TRANSACTION_MODE_EXECUTE: 7913d71c7a4SMatthias Ringwald case ATT_TRANSACTION_MODE_CANCEL: 7923d71c7a4SMatthias Ringwald att_notify_write_callbacks(con_handle, transaction_mode); 7933d71c7a4SMatthias Ringwald return 0; 7943d71c7a4SMatthias Ringwald default: 7953d71c7a4SMatthias Ringwald break; 7963d71c7a4SMatthias Ringwald } 7973d71c7a4SMatthias Ringwald 798bf78aac6SMatthias Ringwald // track CCC writes 799bf78aac6SMatthias Ringwald if (att_is_persistent_ccc(attribute_handle) && offset == 0 && buffer_size == 2){ 800bf78aac6SMatthias Ringwald att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0)); 801bf78aac6SMatthias Ringwald } 802bf78aac6SMatthias Ringwald 80301ac2008SMatthias Ringwald att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle); 80401ac2008SMatthias Ringwald if (!callback) return 0; 8053d71c7a4SMatthias Ringwald return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size); 8063d71c7a4SMatthias Ringwald } 8073d71c7a4SMatthias Ringwald 8083d71c7a4SMatthias Ringwald /** 8093d71c7a4SMatthias Ringwald * @brief register read/write callbacks for specific handle range 8103d71c7a4SMatthias Ringwald * @param att_service_handler_t 8113d71c7a4SMatthias Ringwald */ 8123d71c7a4SMatthias Ringwald void att_server_register_service_handler(att_service_handler_t * handler){ 8133d71c7a4SMatthias Ringwald if (att_service_handler_for_handle(handler->start_handle) || 8143d71c7a4SMatthias Ringwald att_service_handler_for_handle(handler->end_handle)){ 8153d71c7a4SMatthias Ringwald log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle); 8163d71c7a4SMatthias Ringwald return; 8173d71c7a4SMatthias Ringwald } 8183d71c7a4SMatthias Ringwald btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler); 8193d71c7a4SMatthias Ringwald } 8203d71c7a4SMatthias Ringwald 8213deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){ 8223deb3ec6SMatthias Ringwald 8233d71c7a4SMatthias Ringwald // store callbacks 8243d71c7a4SMatthias Ringwald att_server_client_read_callback = read_callback; 8253d71c7a4SMatthias Ringwald att_server_client_write_callback = write_callback; 8263d71c7a4SMatthias Ringwald 8271a389cdcSMatthias Ringwald // register for HCI Events 828d9a7306aSMatthias Ringwald hci_event_callback_registration.callback = &att_event_packet_handler; 8291a389cdcSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 8301a389cdcSMatthias Ringwald 831406722e4SMatthias Ringwald // register for SM events 832d9a7306aSMatthias Ringwald sm_event_callback_registration.callback = &att_event_packet_handler; 833406722e4SMatthias Ringwald sm_add_event_handler(&sm_event_callback_registration); 8343deb3ec6SMatthias Ringwald 8351a389cdcSMatthias Ringwald // and L2CAP ATT Server PDUs 8363deb3ec6SMatthias Ringwald att_dispatch_register_server(att_packet_handler); 8373deb3ec6SMatthias Ringwald 8383deb3ec6SMatthias Ringwald att_set_db(db); 8393d71c7a4SMatthias Ringwald att_set_read_callback(att_server_read_callback); 8403d71c7a4SMatthias Ringwald att_set_write_callback(att_server_write_callback); 8413deb3ec6SMatthias Ringwald 8423deb3ec6SMatthias Ringwald } 8433deb3ec6SMatthias Ringwald 8443deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){ 8453deb3ec6SMatthias Ringwald att_client_packet_handler = handler; 8463deb3ec6SMatthias Ringwald } 8473deb3ec6SMatthias Ringwald 848c141988cSMatthias Ringwald int att_server_can_send_packet_now(hci_con_handle_t con_handle){ 84918707219SMatthias Ringwald return att_dispatch_server_can_send_now(con_handle); 8501b96b007SMatthias Ringwald } 8511b96b007SMatthias Ringwald 852c141988cSMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){ 853d9d23054SMatthias Ringwald log_debug("att_server_request_can_send_now_event 0x%04x", con_handle); 8541b96b007SMatthias Ringwald att_client_waiting_for_can_send = 1; 85518707219SMatthias Ringwald att_dispatch_server_request_can_send_now_event(con_handle); 8563deb3ec6SMatthias Ringwald } 8573deb3ec6SMatthias Ringwald 858bb38f057SMatthias Ringwald void att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 859ae89fd3aSMatthias Ringwald // check if valid con handle 860*49aa330bSMatthias Ringwald if (gap_get_connection_type(con_handle) != GAP_CONNECTION_LE) return; 86118707219SMatthias Ringwald callback_registration->context = (void*)(uintptr_t) con_handle; 862bb38f057SMatthias Ringwald btstack_linked_list_add_tail(&can_send_now_clients, (btstack_linked_item_t*) callback_registration); 863bb38f057SMatthias Ringwald att_dispatch_server_request_can_send_now_event(con_handle); 864bb38f057SMatthias Ringwald } 865bb38f057SMatthias Ringwald 866c141988cSMatthias Ringwald int att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){ 86718707219SMatthias Ringwald att_server_t * att_server = att_server_for_handle(con_handle); 86818707219SMatthias Ringwald if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 86918707219SMatthias Ringwald 87018707219SMatthias Ringwald if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL; 8713deb3ec6SMatthias Ringwald 8723deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 8733deb3ec6SMatthias Ringwald uint8_t * packet_buffer = l2cap_get_outgoing_buffer(); 87418707219SMatthias Ringwald uint16_t size = att_prepare_handle_value_notification(&att_server->connection, attribute_handle, value, value_len, packet_buffer); 87518707219SMatthias Ringwald return l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); 8763deb3ec6SMatthias Ringwald } 8773deb3ec6SMatthias Ringwald 878c141988cSMatthias Ringwald int att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){ 87918707219SMatthias Ringwald att_server_t * att_server = att_server_for_handle(con_handle); 88018707219SMatthias Ringwald if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 88118707219SMatthias Ringwald 882eaac31e8SMatthias Ringwald if (att_server->value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS; 88318707219SMatthias Ringwald if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL; 8843deb3ec6SMatthias Ringwald 8853deb3ec6SMatthias Ringwald // track indication 88618707219SMatthias Ringwald att_server->value_indication_handle = attribute_handle; 88718707219SMatthias Ringwald btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout); 88818707219SMatthias Ringwald btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS); 88918707219SMatthias Ringwald btstack_run_loop_add_timer(&att_server->value_indication_timer); 8903deb3ec6SMatthias Ringwald 8913deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 8923deb3ec6SMatthias Ringwald uint8_t * packet_buffer = l2cap_get_outgoing_buffer(); 89318707219SMatthias Ringwald uint16_t size = att_prepare_handle_value_indication(&att_server->connection, attribute_handle, value, value_len, packet_buffer); 89418707219SMatthias Ringwald l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); 8953deb3ec6SMatthias Ringwald return 0; 8963deb3ec6SMatthias Ringwald } 897