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 232fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 242fca4dadSMilanka Ringwald * GMBH 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 38e501bae0SMatthias 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 <string.h> 473deb3ec6SMatthias Ringwald #include <inttypes.h> 483deb3ec6SMatthias Ringwald 497907f069SMatthias Ringwald #include "btstack_config.h" 503deb3ec6SMatthias Ringwald 51296289e3SMatthias Ringwald #include "ble/att_dispatch.h" 5259c6af15SMatthias Ringwald #include "ble/att_db.h" 5359c6af15SMatthias Ringwald #include "ble/att_server.h" 5459c6af15SMatthias Ringwald #include "ble/core.h" 5559c6af15SMatthias Ringwald #include "ble/le_device_db.h" 5659c6af15SMatthias Ringwald #include "ble/sm.h" 5716ece135SMatthias Ringwald #include "btstack_debug.h" 580e2df43fSMatthias Ringwald #include "btstack_event.h" 593deb3ec6SMatthias Ringwald #include "btstack_memory.h" 600e2df43fSMatthias Ringwald #include "btstack_run_loop.h" 6159c6af15SMatthias Ringwald #include "gap.h" 623deb3ec6SMatthias Ringwald #include "hci.h" 633deb3ec6SMatthias Ringwald #include "hci_dump.h" 643deb3ec6SMatthias Ringwald #include "l2cap.h" 65bf78aac6SMatthias Ringwald #include "btstack_tlv.h" 66d1a1f6a4SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 67d1a1f6a4SMatthias Ringwald #include "ble/sm.h" 6820d4358dSMatthias Ringwald #include "bluetooth_psm.h" 6920d4358dSMatthias Ringwald 70d1a1f6a4SMatthias Ringwald #endif 71bf78aac6SMatthias Ringwald 7203f53d81SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT 7303f53d81SMatthias Ringwald #include <stdio.h> 7403f53d81SMatthias Ringwald #endif 7503f53d81SMatthias Ringwald 766afb9acaSMatthias Ringwald #ifndef NVN_NUM_GATT_SERVER_CCC 776afb9acaSMatthias Ringwald #define NVN_NUM_GATT_SERVER_CCC 20 78bf78aac6SMatthias Ringwald #endif 793deb3ec6SMatthias Ringwald 80cd45e970SMatthias Ringwald static void att_run_for_context(att_server_t * att_server, att_connection_t * att_connection); 8101ac2008SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle); 825d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle); 8348364364SMatthias Ringwald static void att_server_handle_can_send_now(void); 849a1f55eaSMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server, att_connection_t * att_connection); 85fe3d0cbaSMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server); 8626b42dcfSMatthias Ringwald static void att_server_handle_att_pdu(att_server_t * att_server, att_connection_t * att_connection, uint8_t * packet, uint16_t size); 873deb3ec6SMatthias Ringwald 883551936eSMatthias Ringwald typedef enum { 8938b893aaSMilanka Ringwald ATT_SERVER_RUN_PHASE_1_REQUESTS = 0, 903551936eSMatthias Ringwald ATT_SERVER_RUN_PHASE_2_INDICATIONS, 913551936eSMatthias Ringwald ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS, 923551936eSMatthias Ringwald } att_server_run_phase_t; 933551936eSMatthias Ringwald 946a540790SMatthias Ringwald // 956a540790SMatthias Ringwald typedef struct { 966a540790SMatthias Ringwald uint32_t seq_nr; 976a540790SMatthias Ringwald uint16_t att_handle; 986a540790SMatthias Ringwald uint8_t value; 996a540790SMatthias Ringwald uint8_t device_index; 1006a540790SMatthias Ringwald } persistent_ccc_entry_t; 1016a540790SMatthias Ringwald 10218707219SMatthias Ringwald // global 1031a389cdcSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 104406722e4SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration; 10535d7f2dfSMilanka Ringwald static btstack_packet_handler_t att_client_packet_handler; 1063d71c7a4SMatthias Ringwald static btstack_linked_list_t service_handlers; 1075f141511SMatthias Ringwald static btstack_context_callback_registration_t att_client_waiting_for_can_send_registration; 10818707219SMatthias Ringwald 1093d71c7a4SMatthias Ringwald static att_read_callback_t att_server_client_read_callback; 1103d71c7a4SMatthias Ringwald static att_write_callback_t att_server_client_write_callback; 1113d71c7a4SMatthias Ringwald 1126438547aSMatthias Ringwald // round robin 1136438547aSMatthias Ringwald static hci_con_handle_t att_server_last_can_send_now = HCI_CON_HANDLE_INVALID; 114bf78aac6SMatthias Ringwald 11502b78fc8SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 11626567ee7SMatthias Ringwald static bool att_server_connections_for_state(att_server_state_t state, att_server_t ** att_server_out, att_connection_t ** att_connection_out){ 11718707219SMatthias Ringwald btstack_linked_list_iterator_t it; 11818707219SMatthias Ringwald hci_connections_get_iterator(&it); 11918707219SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 12026567ee7SMatthias Ringwald hci_connection_t * hci_connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 12126567ee7SMatthias Ringwald if (hci_connection->att_server.state == state) { 12226567ee7SMatthias Ringwald *att_server_out = &hci_connection->att_server; 12326567ee7SMatthias Ringwald *att_connection_out = &hci_connection->att_connection; 12426567ee7SMatthias Ringwald return true; 12518707219SMatthias Ringwald } 12626567ee7SMatthias Ringwald } 12726567ee7SMatthias Ringwald return false; 12818707219SMatthias Ringwald } 12902b78fc8SMatthias Ringwald #endif 130bb38f057SMatthias Ringwald 1313f9b77dbSMatthias Ringwald static void att_server_request_can_send_now(att_server_t * att_server, att_connection_t * att_connection ){ 13223079b39SMatthias Ringwald switch (att_server->bearer_type){ 13323079b39SMatthias Ringwald case ATT_BEARER_UNENHANCED_LE: 134b6df12b6SMatthias Ringwald att_dispatch_server_request_can_send_now_event(att_connection->con_handle); 13523079b39SMatthias Ringwald break; 1362954c397SMatthias Ringwald #if defined (ENABLE_GATT_OVER_CLASSIC) || defined (ENABLE_GATT_OVER_EATT) 13723079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 13823079b39SMatthias Ringwald case ATT_BEARER_UNENHANCED_CLASSIC: 1392954c397SMatthias Ringwald /* fall through */ 1402954c397SMatthias Ringwald #endif 1412954c397SMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT 1422954c397SMatthias Ringwald case ATT_BEARER_ENHANCED_LE: 1432954c397SMatthias Ringwald /* fall through */ 1442954c397SMatthias Ringwald #endif 14523079b39SMatthias Ringwald l2cap_request_can_send_now_event(att_server->l2cap_cid); 14623079b39SMatthias Ringwald break; 14723079b39SMatthias Ringwald #endif 14823079b39SMatthias Ringwald default: 14923079b39SMatthias Ringwald btstack_unreachable(); 15023079b39SMatthias Ringwald break; 15123079b39SMatthias Ringwald } 152c8c6e9b4SMatthias Ringwald } 153c8c6e9b4SMatthias Ringwald 15437a8059dSMatthias Ringwald static bool att_server_can_send_packet(att_server_t * att_server, att_connection_t * att_connection){ 15523079b39SMatthias Ringwald switch (att_server->bearer_type) { 15623079b39SMatthias Ringwald case ATT_BEARER_UNENHANCED_LE: 1571979f09cSMatthias Ringwald return att_dispatch_server_can_send_now(att_connection->con_handle) != 0; 1582954c397SMatthias Ringwald #if defined (ENABLE_GATT_OVER_CLASSIC) || defined (ENABLE_GATT_OVER_EATT) 15923079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 16023079b39SMatthias Ringwald case ATT_BEARER_UNENHANCED_CLASSIC: 1612954c397SMatthias Ringwald /* fall through */ 1622954c397SMatthias Ringwald #endif 1632954c397SMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT 1642954c397SMatthias Ringwald case ATT_BEARER_ENHANCED_LE: 1652954c397SMatthias Ringwald #endif 1662954c397SMatthias Ringwald return l2cap_can_send_packet_now(att_server->l2cap_cid); 16723079b39SMatthias Ringwald #endif 16823079b39SMatthias Ringwald default: 16923079b39SMatthias Ringwald btstack_unreachable(); 17023079b39SMatthias Ringwald break; 17123079b39SMatthias Ringwald } 17223079b39SMatthias Ringwald return false; 173c0f26e1cSMatthias Ringwald } 174c0f26e1cSMatthias Ringwald 1753deb3ec6SMatthias Ringwald static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){ 1765d07396bSMatthias Ringwald btstack_packet_handler_t packet_handler = att_server_packet_handler_for_handle(attribute_handle); 1775d07396bSMatthias Ringwald if (!packet_handler) return; 1783deb3ec6SMatthias Ringwald 1793deb3ec6SMatthias Ringwald uint8_t event[7]; 1803deb3ec6SMatthias Ringwald int pos = 0; 1815611a760SMatthias Ringwald event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE; 1824ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 1833deb3ec6SMatthias Ringwald event[pos++] = status; 184f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, client_handle); 1853deb3ec6SMatthias Ringwald pos += 2; 186f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, attribute_handle); 187a444112bSMatthias Ringwald (*packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 1883deb3ec6SMatthias Ringwald } 1893deb3ec6SMatthias Ringwald 1905d07396bSMatthias Ringwald static void att_emit_event_to_all(const uint8_t * event, uint16_t size){ 1915d07396bSMatthias Ringwald // dispatch to app level handler 1929305033eSMatthias Ringwald if (att_client_packet_handler != NULL){ 1935d07396bSMatthias Ringwald (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size); 1945d07396bSMatthias Ringwald } 1953deb3ec6SMatthias Ringwald 1965d07396bSMatthias Ringwald // dispatch to service handlers 1975d07396bSMatthias Ringwald btstack_linked_list_iterator_t it; 1985d07396bSMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 1995d07396bSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2005d07396bSMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 2015d07396bSMatthias Ringwald if (!handler->packet_handler) continue; 2025d07396bSMatthias Ringwald (*handler->packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size); 2035d07396bSMatthias Ringwald } 2045d07396bSMatthias Ringwald } 2055d07396bSMatthias Ringwald 2065d07396bSMatthias Ringwald static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){ 2073deb3ec6SMatthias Ringwald uint8_t event[6]; 2083deb3ec6SMatthias Ringwald int pos = 0; 2095611a760SMatthias Ringwald event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE; 2104ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 211fc64f94aSMatthias Ringwald little_endian_store_16(event, pos, con_handle); 2123deb3ec6SMatthias Ringwald pos += 2; 213f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, mtu); 2145d07396bSMatthias Ringwald 2155d07396bSMatthias Ringwald // also dispatch to GATT Clients 216b12646c5SJakob Krantz att_dispatch_server_mtu_exchanged(con_handle, mtu); 2175d07396bSMatthias Ringwald 2185d07396bSMatthias Ringwald // dispatch to app level handler and service handlers 2195d07396bSMatthias Ringwald att_emit_event_to_all(&event[0], sizeof(event)); 2203deb3ec6SMatthias Ringwald } 2213deb3ec6SMatthias Ringwald 2225f141511SMatthias Ringwald static void att_emit_can_send_now_event(void * context){ 2235f141511SMatthias Ringwald UNUSED(context); 2243c97b242SMatthias Ringwald if (!att_client_packet_handler) return; 2253c97b242SMatthias Ringwald 2261b96b007SMatthias Ringwald uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0}; 2271b96b007SMatthias Ringwald (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 2281b96b007SMatthias Ringwald } 2291b96b007SMatthias Ringwald 2306d2214cbSMatthias Ringwald static void att_emit_connected_event(att_server_t * att_server, att_connection_t * att_connection){ 231ff1bec95SMatthias Ringwald uint8_t event[11]; 232ff1bec95SMatthias Ringwald int pos = 0; 233ff1bec95SMatthias Ringwald event[pos++] = ATT_EVENT_CONNECTED; 2344ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 235ff1bec95SMatthias Ringwald event[pos++] = att_server->peer_addr_type; 236ff1bec95SMatthias Ringwald reverse_bd_addr(att_server->peer_address, &event[pos]); 237ff1bec95SMatthias Ringwald pos += 6; 238b6df12b6SMatthias Ringwald little_endian_store_16(event, pos, att_connection->con_handle); 239ff1bec95SMatthias Ringwald pos += 2; 240ff1bec95SMatthias Ringwald 241ff1bec95SMatthias Ringwald // dispatch to app level handler and service handlers 242ff1bec95SMatthias Ringwald att_emit_event_to_all(&event[0], sizeof(event)); 243ff1bec95SMatthias Ringwald } 244ff1bec95SMatthias Ringwald 245ff1bec95SMatthias Ringwald 2466187ad4cSMatthias Ringwald static void att_emit_disconnected_event(uint16_t con_handle){ 247ff1bec95SMatthias Ringwald uint8_t event[4]; 248ff1bec95SMatthias Ringwald int pos = 0; 249ff1bec95SMatthias Ringwald event[pos++] = ATT_EVENT_DISCONNECTED; 2504ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 2516187ad4cSMatthias Ringwald little_endian_store_16(event, pos, con_handle); 252ff1bec95SMatthias Ringwald pos += 2; 253ff1bec95SMatthias Ringwald 254ff1bec95SMatthias Ringwald // dispatch to app level handler and service handlers 255ff1bec95SMatthias Ringwald att_emit_event_to_all(&event[0], sizeof(event)); 256ff1bec95SMatthias Ringwald } 257ff1bec95SMatthias Ringwald 258ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){ 25954178543SMatthias Ringwald void * context = btstack_run_loop_get_timer_context(ts); 26054178543SMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context; 26168591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 26268591e36SMatthias Ringwald if (!hci_connection) return; 26368591e36SMatthias Ringwald // @note: after a transaction timeout, no more requests shall be sent over this ATT Bearer 264bce48a26SMatthias Ringwald // (that's why we don't reset the value_indication_handle) 265b6df12b6SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 26618707219SMatthias Ringwald uint16_t att_handle = att_server->value_indication_handle; 267388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 26838b893aaSMilanka Ringwald att_handle_value_indication_notify_client((uint8_t)ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_connection->con_handle, att_handle); 2693deb3ec6SMatthias Ringwald } 2703deb3ec6SMatthias Ringwald 2713deb3ec6SMatthias Ringwald static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2729ec2630cSMatthias Ringwald 2737dad9ff8SMatthias Ringwald UNUSED(channel); // ok: there is no channel 2747dad9ff8SMatthias Ringwald UNUSED(size); // ok: handling own l2cap events 2753deb3ec6SMatthias Ringwald 27618707219SMatthias Ringwald att_server_t * att_server; 277b6df12b6SMatthias Ringwald att_connection_t * att_connection; 27818707219SMatthias Ringwald hci_con_handle_t con_handle; 27968591e36SMatthias Ringwald hci_connection_t * hci_connection; 28070dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 28170dca4d4SMatthias Ringwald bd_addr_t address; 28268591e36SMatthias Ringwald btstack_linked_list_iterator_t it; 28370dca4d4SMatthias Ringwald #endif 28418707219SMatthias Ringwald 2853deb3ec6SMatthias Ringwald switch (packet_type) { 2863deb3ec6SMatthias Ringwald 2873deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 2880e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 2893deb3ec6SMatthias Ringwald 29070dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 29170dca4d4SMatthias Ringwald case L2CAP_EVENT_INCOMING_CONNECTION: 29270dca4d4SMatthias Ringwald l2cap_event_incoming_connection_get_address(packet, address); 29370dca4d4SMatthias Ringwald l2cap_accept_connection(channel); 29436de8547SMatthias Ringwald log_info("Accept incoming connection from %s", bd_addr_to_str(address)); 29570dca4d4SMatthias Ringwald break; 29670dca4d4SMatthias Ringwald case L2CAP_EVENT_CHANNEL_OPENED: 297122b723eSMatthias Ringwald con_handle = l2cap_event_channel_opened_get_handle(packet); 29868591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 29968591e36SMatthias Ringwald if (!hci_connection) break; 30068591e36SMatthias Ringwald att_server = &hci_connection->att_server; 301122b723eSMatthias Ringwald // store connection info 30223079b39SMatthias Ringwald att_server->bearer_type = ATT_BEARER_UNENHANCED_CLASSIC; 303f16129ceSMatthias Ringwald att_server->peer_addr_type = BD_ADDR_TYPE_ACL; 304122b723eSMatthias Ringwald l2cap_event_channel_opened_get_address(packet, att_server->peer_address); 305388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 306b6df12b6SMatthias Ringwald att_connection->con_handle = con_handle; 30770dca4d4SMatthias Ringwald att_server->l2cap_cid = l2cap_event_channel_opened_get_local_cid(packet); 308122b723eSMatthias Ringwald // reset connection properties 309122b723eSMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 310b6df12b6SMatthias Ringwald att_connection->mtu = l2cap_event_channel_opened_get_remote_mtu(packet); 311b6df12b6SMatthias Ringwald att_connection->max_mtu = l2cap_max_mtu(); 312b6df12b6SMatthias Ringwald if (att_connection->max_mtu > ATT_REQUEST_BUFFER_SIZE){ 313b6df12b6SMatthias Ringwald att_connection->max_mtu = ATT_REQUEST_BUFFER_SIZE; 314122b723eSMatthias Ringwald } 315df33ee14SMatthias Ringwald 316b6df12b6SMatthias Ringwald log_info("Connection opened %s, l2cap cid %04x, mtu %u", bd_addr_to_str(address), att_server->l2cap_cid, att_connection->mtu); 317df33ee14SMatthias Ringwald 318914988a1SMatthias Ringwald // update security params 319b6df12b6SMatthias Ringwald att_connection->encryption_key_size = gap_encryption_key_size(con_handle); 320b6df12b6SMatthias Ringwald att_connection->authenticated = gap_authenticated(con_handle); 321b6df12b6SMatthias Ringwald att_connection->secure_connection = gap_secure_connection(con_handle); 322914988a1SMatthias Ringwald log_info("encrypted key size %u, authenticated %u, secure connection %u", 323b6df12b6SMatthias Ringwald att_connection->encryption_key_size, att_connection->authenticated, att_connection->secure_connection); 324df33ee14SMatthias Ringwald 325df33ee14SMatthias Ringwald // notify connection opened 3266d2214cbSMatthias Ringwald att_emit_connected_event(att_server, att_connection); 327df33ee14SMatthias Ringwald 328914988a1SMatthias Ringwald // restore persisten ccc if encrypted 329914988a1SMatthias Ringwald if ( gap_security_level(con_handle) >= LEVEL_2){ 3309a1f55eaSMatthias Ringwald att_server_persistent_ccc_restore(att_server, att_connection); 331914988a1SMatthias Ringwald } 332122b723eSMatthias Ringwald // TODO: what to do about le device db? 333122b723eSMatthias Ringwald att_server->pairing_active = 0; 33470dca4d4SMatthias Ringwald break; 33548364364SMatthias Ringwald case L2CAP_EVENT_CAN_SEND_NOW: 33648364364SMatthias Ringwald att_server_handle_can_send_now(); 33748364364SMatthias Ringwald break; 33848364364SMatthias Ringwald 33970dca4d4SMatthias Ringwald #endif 3403deb3ec6SMatthias Ringwald case HCI_EVENT_LE_META: 3413deb3ec6SMatthias Ringwald switch (packet[2]) { 3423deb3ec6SMatthias Ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 34318707219SMatthias Ringwald con_handle = little_endian_read_16(packet, 4); 34468591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 34568591e36SMatthias Ringwald if (!hci_connection) break; 34668591e36SMatthias Ringwald att_server = &hci_connection->att_server; 3473deb3ec6SMatthias Ringwald // store connection info 34818707219SMatthias Ringwald att_server->peer_addr_type = packet[7]; 34918707219SMatthias Ringwald reverse_bd_addr(&packet[8], att_server->peer_address); 350388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 351b6df12b6SMatthias Ringwald att_connection->con_handle = con_handle; 3523deb3ec6SMatthias Ringwald // reset connection properties 35318707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 35423079b39SMatthias Ringwald att_server->bearer_type = ATT_BEARER_UNENHANCED_LE; 355b6df12b6SMatthias Ringwald att_connection->mtu = ATT_DEFAULT_MTU; 356b6df12b6SMatthias Ringwald att_connection->max_mtu = l2cap_max_le_mtu(); 357b6df12b6SMatthias Ringwald if (att_connection->max_mtu > ATT_REQUEST_BUFFER_SIZE){ 358b6df12b6SMatthias Ringwald att_connection->max_mtu = ATT_REQUEST_BUFFER_SIZE; 35999c58ad0SMatthias Ringwald } 36038b893aaSMilanka Ringwald att_connection->encryption_key_size = 0u; 36138b893aaSMilanka Ringwald att_connection->authenticated = 0u; 36238b893aaSMilanka Ringwald att_connection->authorized = 0u; 363b2c1e52cSMatthias Ringwald // workaround: identity resolving can already be complete, at least store result 364b2c1e52cSMatthias Ringwald att_server->ir_le_device_db_index = sm_le_device_index(con_handle); 36538b893aaSMilanka Ringwald att_server->ir_lookup_active = 0u; 36638b893aaSMilanka Ringwald att_server->pairing_active = 0u; 367ff1bec95SMatthias Ringwald // notify all - old 3685d07396bSMatthias Ringwald att_emit_event_to_all(packet, size); 369ff1bec95SMatthias Ringwald // notify all - new 3706d2214cbSMatthias Ringwald att_emit_connected_event(att_server, att_connection); 3713deb3ec6SMatthias Ringwald break; 3723deb3ec6SMatthias Ringwald 3733deb3ec6SMatthias Ringwald default: 3743deb3ec6SMatthias Ringwald break; 3753deb3ec6SMatthias Ringwald } 3763deb3ec6SMatthias Ringwald break; 3773deb3ec6SMatthias Ringwald 3783deb3ec6SMatthias Ringwald case HCI_EVENT_ENCRYPTION_CHANGE: 3798601e696SMatthias Ringwald case HCI_EVENT_ENCRYPTION_CHANGE_V2: 3803deb3ec6SMatthias Ringwald case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE: 3813deb3ec6SMatthias Ringwald // check handle 38218707219SMatthias Ringwald con_handle = little_endian_read_16(packet, 3); 38368591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 38468591e36SMatthias Ringwald if (!hci_connection) break; 385914988a1SMatthias Ringwald if (gap_get_connection_type(con_handle) != GAP_CONNECTION_LE) break; 386914988a1SMatthias Ringwald // update security params 38768591e36SMatthias Ringwald att_server = &hci_connection->att_server; 388388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 389b6df12b6SMatthias Ringwald att_connection->encryption_key_size = gap_encryption_key_size(con_handle); 390ff3f1026SMatthias Ringwald att_connection->authenticated = gap_authenticated(con_handle) ? 1 : 0; 391f461b3dfSMatthias Ringwald att_connection->secure_connection = gap_secure_connection(con_handle) ? 1 : 0; 392914988a1SMatthias Ringwald log_info("encrypted key size %u, authenticated %u, secure connection %u", 393b6df12b6SMatthias Ringwald att_connection->encryption_key_size, att_connection->authenticated, att_connection->secure_connection); 394914988a1SMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){ 3954097998aSMatthias Ringwald // restore CCC values when encrypted for LE Connections 39622bfc225SMilanka Ringwald if (hci_event_encryption_change_get_encryption_enabled(packet) != 0){ 3979a1f55eaSMatthias Ringwald att_server_persistent_ccc_restore(att_server, att_connection); 39801ac2008SMatthias Ringwald } 39901ac2008SMatthias Ringwald } 400cd45e970SMatthias Ringwald att_run_for_context(att_server, att_connection); 4013deb3ec6SMatthias Ringwald break; 4023deb3ec6SMatthias Ringwald 4033deb3ec6SMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 40470a390c7SMatthias Ringwald // check handle 40518707219SMatthias Ringwald con_handle = hci_event_disconnection_complete_get_connection_handle(packet); 40668591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 40768591e36SMatthias Ringwald if (!hci_connection) break; 40868591e36SMatthias Ringwald att_server = &hci_connection->att_server; 409388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 410b6df12b6SMatthias Ringwald att_clear_transaction_queue(att_connection); 411b6df12b6SMatthias Ringwald att_connection->con_handle = 0; 412760ad805SMatthias Ringwald att_server->pairing_active = 0; 41318707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 41438b893aaSMilanka Ringwald if (att_server->value_indication_handle != 0u){ 41527328ecbSMatthias Ringwald btstack_run_loop_remove_timer(&att_server->value_indication_timer); 416bce48a26SMatthias Ringwald uint16_t att_handle = att_server->value_indication_handle; 41738b893aaSMilanka Ringwald att_server->value_indication_handle = 0u; // reset error state 41838b893aaSMilanka Ringwald att_handle_value_indication_notify_client((uint8_t)ATT_HANDLE_VALUE_INDICATION_DISCONNECT, att_connection->con_handle, att_handle); 419bce48a26SMatthias Ringwald } 420ff1bec95SMatthias Ringwald // notify all - new 4216187ad4cSMatthias Ringwald att_emit_disconnected_event(con_handle); 422ff1bec95SMatthias Ringwald // notify all - old 4235d07396bSMatthias Ringwald att_emit_event_to_all(packet, size); 4243deb3ec6SMatthias Ringwald break; 4253deb3ec6SMatthias Ringwald 426760ad805SMatthias Ringwald // Identity Resolving 4275611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_STARTED: 42818707219SMatthias Ringwald con_handle = sm_event_identity_resolving_started_get_handle(packet); 42968591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 43068591e36SMatthias Ringwald if (!hci_connection) break; 43168591e36SMatthias Ringwald att_server = &hci_connection->att_server; 4325611a760SMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED"); 43330c8e114SMatthias Ringwald att_server->ir_lookup_active = 1; 4343deb3ec6SMatthias Ringwald break; 4355611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED: 436760ad805SMatthias Ringwald con_handle = sm_event_identity_created_get_handle(packet); 43768591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 43868591e36SMatthias Ringwald if (!hci_connection) return; 43968591e36SMatthias Ringwald att_server = &hci_connection->att_server; 44030c8e114SMatthias Ringwald att_server->ir_lookup_active = 0; 4411b7acd0dSMatthias Ringwald att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index(packet); 4421b7acd0dSMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED"); 443cd45e970SMatthias Ringwald att_run_for_context(att_server, att_connection); 4443deb3ec6SMatthias Ringwald break; 4455611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_FAILED: 44618707219SMatthias Ringwald con_handle = sm_event_identity_resolving_failed_get_handle(packet); 44768591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 44868591e36SMatthias Ringwald if (!hci_connection) break; 44968591e36SMatthias Ringwald att_server = &hci_connection->att_server; 4505611a760SMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED"); 45130c8e114SMatthias Ringwald att_server->ir_lookup_active = 0; 45218707219SMatthias Ringwald att_server->ir_le_device_db_index = -1; 453cd45e970SMatthias Ringwald att_run_for_context(att_server, att_connection); 4543deb3ec6SMatthias Ringwald break; 455760ad805SMatthias Ringwald 456760ad805SMatthias Ringwald // Pairing started - delete stored CCC values 457760ad805SMatthias Ringwald // - assumes pairing indicates either new device or re-pairing, in both cases there should be no stored CCC values 458760ad805SMatthias Ringwald // - assumes that all events have the con handle as the first field 459760ad805SMatthias Ringwald case SM_EVENT_JUST_WORKS_REQUEST: 460760ad805SMatthias Ringwald case SM_EVENT_PASSKEY_DISPLAY_NUMBER: 461760ad805SMatthias Ringwald case SM_EVENT_PASSKEY_INPUT_NUMBER: 462760ad805SMatthias Ringwald case SM_EVENT_NUMERIC_COMPARISON_REQUEST: 463760ad805SMatthias Ringwald con_handle = sm_event_just_works_request_get_handle(packet); 46468591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 46568591e36SMatthias Ringwald if (!hci_connection) break; 46668591e36SMatthias Ringwald att_server = &hci_connection->att_server; 467760ad805SMatthias Ringwald att_server->pairing_active = 1; 468760ad805SMatthias Ringwald log_info("SM Pairing started"); 469760ad805SMatthias Ringwald if (att_server->ir_le_device_db_index < 0) break; 470cd45e970SMatthias Ringwald att_run_for_context(att_server, att_connection); 471760ad805SMatthias Ringwald // index not valid anymore 472760ad805SMatthias Ringwald att_server->ir_le_device_db_index = -1; 473760ad805SMatthias Ringwald break; 474760ad805SMatthias Ringwald 4751b7acd0dSMatthias Ringwald // Bonding completed 476760ad805SMatthias Ringwald case SM_EVENT_IDENTITY_CREATED: 477760ad805SMatthias Ringwald con_handle = sm_event_identity_created_get_handle(packet); 47868591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 47968591e36SMatthias Ringwald if (!hci_connection) return; 48068591e36SMatthias Ringwald att_server = &hci_connection->att_server; 481760ad805SMatthias Ringwald att_server->pairing_active = 0; 4821b7acd0dSMatthias Ringwald att_server->ir_le_device_db_index = sm_event_identity_created_get_index(packet); 483cd45e970SMatthias Ringwald att_run_for_context(att_server, att_connection); 4841b7acd0dSMatthias Ringwald break; 4851b7acd0dSMatthias Ringwald 4861b7acd0dSMatthias Ringwald // Pairing complete (with/without bonding=storing of pairing information) 4871b7acd0dSMatthias Ringwald case SM_EVENT_PAIRING_COMPLETE: 4881b7acd0dSMatthias Ringwald con_handle = sm_event_pairing_complete_get_handle(packet); 48968591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 49068591e36SMatthias Ringwald if (!hci_connection) return; 49168591e36SMatthias Ringwald att_server = &hci_connection->att_server; 4921b7acd0dSMatthias Ringwald att_server->pairing_active = 0; 493cd45e970SMatthias Ringwald att_run_for_context(att_server, att_connection); 494760ad805SMatthias Ringwald break; 495760ad805SMatthias Ringwald 496760ad805SMatthias Ringwald // Authorization 4975611a760SMatthias Ringwald case SM_EVENT_AUTHORIZATION_RESULT: { 49818707219SMatthias Ringwald con_handle = sm_event_authorization_result_get_handle(packet); 49968591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 50068591e36SMatthias Ringwald if (!hci_connection) break; 50168591e36SMatthias Ringwald att_server = &hci_connection->att_server; 502388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 503b6df12b6SMatthias Ringwald att_connection->authorized = sm_event_authorization_result_get_authorization_result(packet); 5043f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 5053deb3ec6SMatthias Ringwald break; 5063deb3ec6SMatthias Ringwald } 5073deb3ec6SMatthias Ringwald default: 5083deb3ec6SMatthias Ringwald break; 5093deb3ec6SMatthias Ringwald } 51065b44ffdSMatthias Ringwald break; 51170dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 51270dca4d4SMatthias Ringwald case L2CAP_DATA_PACKET: 51368591e36SMatthias Ringwald hci_connections_get_iterator(&it); 51468591e36SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 515388fa7c4SMatthias Ringwald hci_connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 516388fa7c4SMatthias Ringwald att_server = &hci_connection->att_server; 51726b42dcfSMatthias Ringwald att_connection = &hci_connection->att_connection; 51868591e36SMatthias Ringwald if (att_server->l2cap_cid == channel) { 51926b42dcfSMatthias Ringwald att_server_handle_att_pdu(att_server, att_connection, packet, size); 52068591e36SMatthias Ringwald break; 52168591e36SMatthias Ringwald } 52268591e36SMatthias Ringwald } 52370dca4d4SMatthias Ringwald break; 52470dca4d4SMatthias Ringwald #endif 52541772f37SMatthias Ringwald 52665b44ffdSMatthias Ringwald default: 52765b44ffdSMatthias Ringwald break; 5283deb3ec6SMatthias Ringwald } 5293deb3ec6SMatthias Ringwald } 5303deb3ec6SMatthias Ringwald 5310f70c52eSMatthias Ringwald static uint8_t 5320f70c52eSMatthias Ringwald att_server_send_prepared(const att_server_t *att_server, const att_connection_t *att_connection, uint8_t *buffer, 5330f70c52eSMatthias Ringwald uint16_t size) { 534d415948cSMatthias Ringwald uint8_t status = ERROR_CODE_SUCCESS; 535d415948cSMatthias Ringwald switch (att_server->bearer_type) { 536d415948cSMatthias Ringwald case ATT_BEARER_UNENHANCED_LE: 537d415948cSMatthias Ringwald status = l2cap_send_prepared_connectionless(att_connection->con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); 538d415948cSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 539d415948cSMatthias Ringwald case ATT_BEARER_UNENHANCED_CLASSIC: 540d415948cSMatthias Ringwald status = l2cap_send_prepared(att_server->l2cap_cid, size); 541d415948cSMatthias Ringwald break; 542d415948cSMatthias Ringwald #endif 5430f70c52eSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT 5440f70c52eSMatthias Ringwald case ATT_BEARER_ENHANCED_LE: 5450f70c52eSMatthias Ringwald btstack_assert(buffer != NULL); 5460f70c52eSMatthias Ringwald status = l2cap_send(att_server->l2cap_cid, buffer, size); 5470f70c52eSMatthias Ringwald break; 5480f70c52eSMatthias Ringwald #endif 549d415948cSMatthias Ringwald default: 550d415948cSMatthias Ringwald btstack_unreachable(); 551d415948cSMatthias Ringwald break; 552d415948cSMatthias Ringwald } 553d415948cSMatthias Ringwald return status; 554d415948cSMatthias Ringwald } 555d415948cSMatthias Ringwald 5567a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 5573deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){ 55826567ee7SMatthias Ringwald att_server_t * att_server = NULL; 55926567ee7SMatthias Ringwald att_connection_t * att_connection = NULL; 56026567ee7SMatthias Ringwald bool found = att_server_connections_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION, &att_server, &att_connection); 5613deb3ec6SMatthias Ringwald 5623deb3ec6SMatthias Ringwald uint8_t hash_flipped[8]; 5639c80e4ccSMatthias Ringwald reverse_64(hash, hash_flipped); 56426567ee7SMatthias Ringwald if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8) != 0){ 5653deb3ec6SMatthias Ringwald log_info("ATT Signed Write, invalid signature"); 56603f53d81SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT 56703f53d81SMatthias Ringwald printf("ATT Signed Write, invalid signature\n"); 56803f53d81SMatthias Ringwald #endif 56918707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 5703deb3ec6SMatthias Ringwald return; 5713deb3ec6SMatthias Ringwald } 5723deb3ec6SMatthias Ringwald log_info("ATT Signed Write, valid signature"); 57303f53d81SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT 57403f53d81SMatthias Ringwald printf("ATT Signed Write, valid signature\n"); 57503f53d81SMatthias Ringwald #endif 5763deb3ec6SMatthias Ringwald 5773deb3ec6SMatthias Ringwald // update sequence number 57818707219SMatthias Ringwald uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12); 57918707219SMatthias Ringwald le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1); 58018707219SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 5813f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 5823deb3ec6SMatthias Ringwald } 5837a766ebfSMatthias Ringwald #endif 58488a48dd8SMatthias Ringwald 58518707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED 586b06f2579SMatthias Ringwald // pre: can send now 5879988bc8aSMatthias Ringwald // uses l2cap outgoing buffer if no eatt_buffer provided 588b06f2579SMatthias Ringwald // returns: 1 if packet was sent 5899988bc8aSMatthias Ringwald static int 5909988bc8aSMatthias Ringwald att_server_process_validated_request(att_server_t *att_server, att_connection_t *att_connection, uint8_t *eatt_buffer) { 591b06f2579SMatthias Ringwald 5929988bc8aSMatthias Ringwald uint8_t * att_response_buffer; 5939988bc8aSMatthias Ringwald if (eatt_buffer != NULL){ 5949988bc8aSMatthias Ringwald att_response_buffer = eatt_buffer; 5959988bc8aSMatthias Ringwald } else { 596b06f2579SMatthias Ringwald l2cap_reserve_packet_buffer(); 5979988bc8aSMatthias Ringwald att_response_buffer = l2cap_get_outgoing_buffer(); 5989988bc8aSMatthias Ringwald } 5999988bc8aSMatthias Ringwald 600*7a293e22SMatthias Ringwald uint16_t att_response_size = 0; 601*7a293e22SMatthias Ringwald // Send Error Response for MTU Request over connection-oriented channel 602*7a293e22SMatthias Ringwald if ((att_server->bearer_type != ATT_BEARER_UNENHANCED_LE) && (att_server->request_buffer[0] == ATT_EXCHANGE_MTU_REQUEST)){ 603*7a293e22SMatthias Ringwald att_response_size = 5; 604*7a293e22SMatthias Ringwald att_response_buffer[0] = ATT_ERROR_RESPONSE; 605*7a293e22SMatthias Ringwald att_response_buffer[1] = ATT_EXCHANGE_MTU_REQUEST; 606*7a293e22SMatthias Ringwald att_response_buffer[2] = 0; 607*7a293e22SMatthias Ringwald att_response_buffer[3] = 0; 608*7a293e22SMatthias Ringwald att_response_buffer[4] = ATT_ERROR_REQUEST_NOT_SUPPORTED; 609*7a293e22SMatthias Ringwald } else { 610*7a293e22SMatthias Ringwald att_response_size = att_handle_request(att_connection, att_server->request_buffer, att_server->request_size, att_response_buffer); 611*7a293e22SMatthias Ringwald } 612b06f2579SMatthias Ringwald 613beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE 614c1ab6cc1SMatthias Ringwald if ((att_response_size == ATT_READ_RESPONSE_PENDING) || (att_response_size == ATT_INTERNAL_WRITE_RESPONSE_PENDING)){ 615e404a688SMatthias Ringwald // update state 616beb20288SMatthias Ringwald att_server->state = ATT_SERVER_RESPONSE_PENDING; 617e404a688SMatthias Ringwald 61856c3a347SMatthias Ringwald // callback with handle ATT_READ_RESPONSE_PENDING for reads 61956c3a347SMatthias Ringwald if (att_response_size == ATT_READ_RESPONSE_PENDING){ 620b6df12b6SMatthias Ringwald att_server_client_read_callback(att_connection->con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0); 62156c3a347SMatthias Ringwald } 622e8e7403eSMatthias Ringwald 623e8e7403eSMatthias Ringwald // free reserved buffer 6249988bc8aSMatthias Ringwald if (eatt_buffer == NULL){ 625e8e7403eSMatthias Ringwald l2cap_release_packet_buffer(); 6269988bc8aSMatthias Ringwald } 627e8e7403eSMatthias Ringwald return 0; 628e404a688SMatthias Ringwald } 629e404a688SMatthias Ringwald #endif 630e404a688SMatthias Ringwald 631b06f2579SMatthias Ringwald // intercept "insufficient authorization" for authenticated connections to allow for user authorization 6324ea43905SMatthias Ringwald if ((att_response_size >= 4u) 633b06f2579SMatthias Ringwald && (att_response_buffer[0] == ATT_ERROR_RESPONSE) 634b06f2579SMatthias Ringwald && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION) 63538b893aaSMilanka Ringwald && (att_connection->authenticated != 0u)){ 636b06f2579SMatthias Ringwald 637b6df12b6SMatthias Ringwald switch (gap_authorization_state(att_connection->con_handle)){ 638b06f2579SMatthias Ringwald case AUTHORIZATION_UNKNOWN: 6399988bc8aSMatthias Ringwald if (eatt_buffer == NULL){ 640b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 6419988bc8aSMatthias Ringwald } 642b6df12b6SMatthias Ringwald sm_request_pairing(att_connection->con_handle); 643b06f2579SMatthias Ringwald return 0; 644b06f2579SMatthias Ringwald case AUTHORIZATION_PENDING: 6459988bc8aSMatthias Ringwald if (eatt_buffer == NULL){ 646b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 6479988bc8aSMatthias Ringwald } 648b06f2579SMatthias Ringwald return 0; 649b06f2579SMatthias Ringwald default: 650b06f2579SMatthias Ringwald break; 651b06f2579SMatthias Ringwald } 652b06f2579SMatthias Ringwald } 653b06f2579SMatthias Ringwald 65418707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 6554ea43905SMatthias Ringwald if (att_response_size == 0u) { 6569988bc8aSMatthias Ringwald if (eatt_buffer == NULL){ 657b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 6589988bc8aSMatthias Ringwald } 659b06f2579SMatthias Ringwald return 0; 660b06f2579SMatthias Ringwald } 661b06f2579SMatthias Ringwald 6629988bc8aSMatthias Ringwald (void) att_server_send_prepared(att_server, att_connection, eatt_buffer, att_response_size); 663b06f2579SMatthias Ringwald 664b06f2579SMatthias Ringwald // notify client about MTU exchange result 665b06f2579SMatthias Ringwald if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){ 666b6df12b6SMatthias Ringwald att_emit_mtu_event(att_connection->con_handle, att_connection->mtu); 667b06f2579SMatthias Ringwald } 668b06f2579SMatthias Ringwald return 1; 669b06f2579SMatthias Ringwald } 670b06f2579SMatthias Ringwald 671beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE 6729816429dSMatthias Ringwald uint8_t att_server_response_ready(hci_con_handle_t con_handle){ 67368591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 67468591e36SMatthias Ringwald if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 67568591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 6763f9b77dbSMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 677beb20288SMatthias Ringwald if (att_server->state != ATT_SERVER_RESPONSE_PENDING) return ERROR_CODE_COMMAND_DISALLOWED; 678e404a688SMatthias Ringwald 679e404a688SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 6803f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 681e404a688SMatthias Ringwald return ERROR_CODE_SUCCESS; 682e404a688SMatthias Ringwald } 683e404a688SMatthias Ringwald #endif 684e404a688SMatthias Ringwald 685cd45e970SMatthias Ringwald static void att_run_for_context(att_server_t * att_server, att_connection_t * att_connection){ 68618707219SMatthias Ringwald switch (att_server->state){ 6873deb3ec6SMatthias Ringwald case ATT_SERVER_REQUEST_RECEIVED: 68823079b39SMatthias Ringwald switch (att_server->bearer_type){ 68923079b39SMatthias Ringwald case ATT_BEARER_UNENHANCED_LE: 6900234fbbcSMatthias Ringwald // wait until re-encryption as central is complete 69123079b39SMatthias Ringwald if (gap_reconnect_security_setup_active(att_connection->con_handle)) { 69223079b39SMatthias Ringwald return; 69323079b39SMatthias Ringwald }; 69423079b39SMatthias Ringwald break; 6952954c397SMatthias Ringwald #if defined(ENABLE_GATT_OVER_CLASSIC) || defined (ENABLE_GATT_OVER_EATT) 69623079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 69723079b39SMatthias Ringwald case ATT_BEARER_UNENHANCED_CLASSIC: 6982954c397SMatthias Ringwald /* fall through */ 6992954c397SMatthias Ringwald #endif 7002954c397SMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT 7012954c397SMatthias Ringwald case ATT_BEARER_ENHANCED_LE: 7022954c397SMatthias Ringwald #endif 70323079b39SMatthias Ringwald // ok 70423079b39SMatthias Ringwald break; 70523079b39SMatthias Ringwald #endif 70623079b39SMatthias Ringwald default: 70723079b39SMatthias Ringwald btstack_unreachable(); 70823079b39SMatthias Ringwald break; 70948364364SMatthias Ringwald } 7100234fbbcSMatthias Ringwald 711760ad805SMatthias Ringwald // wait until pairing is complete 712760ad805SMatthias Ringwald if (att_server->pairing_active) break; 713760ad805SMatthias Ringwald 7147a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 71518707219SMatthias Ringwald if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){ 7163deb3ec6SMatthias Ringwald log_info("ATT Signed Write!"); 7173deb3ec6SMatthias Ringwald if (!sm_cmac_ready()) { 7183deb3ec6SMatthias Ringwald log_info("ATT Signed Write, sm_cmac engine not ready. Abort"); 71918707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 7203deb3ec6SMatthias Ringwald return; 7213deb3ec6SMatthias Ringwald } 72218707219SMatthias Ringwald if (att_server->request_size < (3 + 12)) { 7233deb3ec6SMatthias Ringwald log_info("ATT Signed Write, request to short. Abort."); 72418707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 7253deb3ec6SMatthias Ringwald return; 7263deb3ec6SMatthias Ringwald } 72718707219SMatthias Ringwald if (att_server->ir_lookup_active){ 7283deb3ec6SMatthias Ringwald return; 7293deb3ec6SMatthias Ringwald } 73018707219SMatthias Ringwald if (att_server->ir_le_device_db_index < 0){ 7313deb3ec6SMatthias Ringwald log_info("ATT Signed Write, CSRK not available"); 73218707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 7333deb3ec6SMatthias Ringwald return; 7343deb3ec6SMatthias Ringwald } 7353deb3ec6SMatthias Ringwald 7363deb3ec6SMatthias Ringwald // check counter 73718707219SMatthias Ringwald uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12); 73818707219SMatthias Ringwald uint32_t counter_db = le_device_db_remote_counter_get(att_server->ir_le_device_db_index); 7393deb3ec6SMatthias Ringwald log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet); 7403deb3ec6SMatthias Ringwald if (counter_packet < counter_db){ 7413deb3ec6SMatthias Ringwald log_info("ATT Signed Write, db reports higher counter, abort"); 74218707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 7433deb3ec6SMatthias Ringwald return; 7443deb3ec6SMatthias Ringwald } 7453deb3ec6SMatthias Ringwald 7463deb3ec6SMatthias Ringwald // signature is { sequence counter, secure hash } 7473deb3ec6SMatthias Ringwald sm_key_t csrk; 74818707219SMatthias Ringwald le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk); 74918707219SMatthias Ringwald att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION; 7503deb3ec6SMatthias Ringwald log_info("Orig Signature: "); 75118707219SMatthias Ringwald log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8); 75218707219SMatthias Ringwald uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1); 75318707219SMatthias 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); 7543deb3ec6SMatthias Ringwald return; 7553deb3ec6SMatthias Ringwald } 7567a766ebfSMatthias Ringwald #endif 757b06f2579SMatthias Ringwald // move on 75818707219SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 7593f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 760b06f2579SMatthias Ringwald break; 76188a48dd8SMatthias Ringwald 7623deb3ec6SMatthias Ringwald default: 7633deb3ec6SMatthias Ringwald break; 7643deb3ec6SMatthias Ringwald } 7653deb3ec6SMatthias Ringwald } 7663deb3ec6SMatthias Ringwald 7671979f09cSMatthias Ringwald static bool att_server_data_ready_for_phase(att_server_t * att_server, att_server_run_phase_t phase){ 76890f03c80SMatthias Ringwald switch (phase){ 76990f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_1_REQUESTS: 77090f03c80SMatthias Ringwald return att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 77190f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_2_INDICATIONS: 77238b893aaSMilanka Ringwald return (!btstack_linked_list_empty(&att_server->indication_requests) && (att_server->value_indication_handle == 0u)); 77390f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS: 77490f03c80SMatthias Ringwald return (!btstack_linked_list_empty(&att_server->notification_requests)); 7757bbeb3adSMilanka Ringwald default: 7767bbeb3adSMilanka Ringwald btstack_assert(false); 7771979f09cSMatthias Ringwald return false; 77890f03c80SMatthias Ringwald } 7797bbeb3adSMilanka Ringwald } 78090f03c80SMatthias Ringwald 7819d6c0193SMatthias Ringwald static void att_server_trigger_send_for_phase(att_server_t * att_server, att_connection_t * att_connection, att_server_run_phase_t phase){ 78290f03c80SMatthias Ringwald btstack_context_callback_registration_t * client; 78390f03c80SMatthias Ringwald switch (phase){ 78490f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_1_REQUESTS: 7850f70c52eSMatthias Ringwald att_server_process_validated_request(att_server, att_connection, NULL); 78690f03c80SMatthias Ringwald break; 78790f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_2_INDICATIONS: 78890f03c80SMatthias Ringwald client = (btstack_context_callback_registration_t*) att_server->indication_requests; 78990f03c80SMatthias Ringwald btstack_linked_list_remove(&att_server->indication_requests, (btstack_linked_item_t *) client); 79090f03c80SMatthias Ringwald client->callback(client->context); 79190f03c80SMatthias Ringwald break; 79290f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS: 79390f03c80SMatthias Ringwald client = (btstack_context_callback_registration_t*) att_server->notification_requests; 79490f03c80SMatthias Ringwald btstack_linked_list_remove(&att_server->notification_requests, (btstack_linked_item_t *) client); 79590f03c80SMatthias Ringwald client->callback(client->context); 79690f03c80SMatthias Ringwald break; 7977bbeb3adSMilanka Ringwald default: 7987bbeb3adSMilanka Ringwald btstack_assert(false); 7997bbeb3adSMilanka Ringwald break; 80090f03c80SMatthias Ringwald } 80190f03c80SMatthias Ringwald } 80290f03c80SMatthias Ringwald 8037eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){ 804b06f2579SMatthias Ringwald 8056438547aSMatthias Ringwald hci_con_handle_t last_send_con_handle = HCI_CON_HANDLE_INVALID; 80668591e36SMatthias Ringwald hci_connection_t * request_hci_connection = NULL; 8071979f09cSMatthias Ringwald bool can_send_now = true; 80838b893aaSMilanka Ringwald uint8_t phase_index; 8096438547aSMatthias Ringwald 8104395a000SMatthias Ringwald for (phase_index = ATT_SERVER_RUN_PHASE_1_REQUESTS; phase_index <= ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS; phase_index++){ 8114395a000SMatthias Ringwald att_server_run_phase_t phase = (att_server_run_phase_t) phase_index; 8126438547aSMatthias Ringwald hci_con_handle_t skip_connections_until = att_server_last_can_send_now; 813ff3cc4a5SMatthias Ringwald while (true){ 8146438547aSMatthias Ringwald btstack_linked_list_iterator_t it; 81518707219SMatthias Ringwald hci_connections_get_iterator(&it); 81618707219SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 81718707219SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 81818707219SMatthias Ringwald att_server_t * att_server = &connection->att_server; 819388fa7c4SMatthias Ringwald att_connection_t * att_connection = &connection->att_connection; 8206438547aSMatthias Ringwald 8211979f09cSMatthias Ringwald bool data_ready = att_server_data_ready_for_phase(att_server, phase); 8226438547aSMatthias Ringwald 823b6df12b6SMatthias Ringwald // log_debug("phase %u, handle 0x%04x, skip until 0x%04x, data ready %u", phase, att_connection->con_handle, skip_connections_until, data_ready); 8246438547aSMatthias Ringwald 8256438547aSMatthias Ringwald // skip until last sender found (which is also skipped) 8266438547aSMatthias Ringwald if (skip_connections_until != HCI_CON_HANDLE_INVALID){ 82768591e36SMatthias Ringwald if (data_ready && (request_hci_connection == NULL)){ 82868591e36SMatthias Ringwald request_hci_connection = connection; 8296438547aSMatthias Ringwald } 830b6df12b6SMatthias Ringwald if (skip_connections_until == att_connection->con_handle){ 8316438547aSMatthias Ringwald skip_connections_until = HCI_CON_HANDLE_INVALID; 8326438547aSMatthias Ringwald } 8336438547aSMatthias Ringwald continue; 8346438547aSMatthias Ringwald }; 8356438547aSMatthias Ringwald 83690f03c80SMatthias Ringwald if (data_ready){ 837969a5bbaSMatthias Ringwald if (can_send_now){ 8389d6c0193SMatthias Ringwald att_server_trigger_send_for_phase(att_server, att_connection, phase); 839b6df12b6SMatthias Ringwald last_send_con_handle = att_connection->con_handle; 84037a8059dSMatthias Ringwald can_send_now = att_server_can_send_packet(att_server, att_connection); 84190f03c80SMatthias Ringwald data_ready = att_server_data_ready_for_phase(att_server, phase); 84268591e36SMatthias Ringwald if (data_ready && (request_hci_connection == NULL)){ 84368591e36SMatthias Ringwald request_hci_connection = connection; 844cbbb12d9SMatthias Ringwald } 845cbbb12d9SMatthias Ringwald } else { 84668591e36SMatthias Ringwald request_hci_connection = connection; 847f29a88d8SMatthias Ringwald break; 848cbbb12d9SMatthias Ringwald } 849cbbb12d9SMatthias Ringwald } 8503deb3ec6SMatthias Ringwald } 8513deb3ec6SMatthias Ringwald 8526438547aSMatthias Ringwald // stop skipping (handles disconnect by last send connection) 8536438547aSMatthias Ringwald skip_connections_until = HCI_CON_HANDLE_INVALID; 8546438547aSMatthias Ringwald 8553551936eSMatthias Ringwald // Exit loop, if we cannot send 856969a5bbaSMatthias Ringwald if (!can_send_now) break; 857969a5bbaSMatthias Ringwald 858969a5bbaSMatthias Ringwald // Exit loop, if we can send but there are also no further request 85968591e36SMatthias Ringwald if (request_hci_connection == NULL) break; 860969a5bbaSMatthias Ringwald 861969a5bbaSMatthias Ringwald // Finally, if we still can send and there are requests, just try again 86268591e36SMatthias Ringwald request_hci_connection = NULL; 863969a5bbaSMatthias Ringwald } 8646438547aSMatthias Ringwald // update last send con handle for round robin 8656438547aSMatthias Ringwald if (last_send_con_handle != HCI_CON_HANDLE_INVALID){ 8666438547aSMatthias Ringwald att_server_last_can_send_now = last_send_con_handle; 8676438547aSMatthias Ringwald } 8683551936eSMatthias Ringwald } 869969a5bbaSMatthias Ringwald 87068591e36SMatthias Ringwald if (request_hci_connection == NULL) return; 8713f9b77dbSMatthias Ringwald 8723f9b77dbSMatthias Ringwald att_server_t * att_server = &request_hci_connection->att_server; 8733f9b77dbSMatthias Ringwald att_connection_t * att_connection = &request_hci_connection->att_connection; 8743f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 875969a5bbaSMatthias Ringwald } 876969a5bbaSMatthias Ringwald 87726b42dcfSMatthias Ringwald static void att_server_handle_att_pdu(att_server_t * att_server, att_connection_t * att_connection, uint8_t * packet, uint16_t size){ 87818707219SMatthias Ringwald 879838bd521SMatthias Ringwald uint8_t opcode = packet[0u]; 88038b893aaSMilanka Ringwald uint8_t method = opcode & 0x03fu; 881ed68ae9dSMilanka Ringwald bool invalid = method > ATT_MULTIPLE_HANDLE_VALUE_NTF; 88238b893aaSMilanka Ringwald bool command = (opcode & 0x40u) != 0u; 883838bd521SMatthias Ringwald 884838bd521SMatthias Ringwald // ignore invalid commands 885838bd521SMatthias Ringwald if (invalid && command){ 886838bd521SMatthias Ringwald return; 887838bd521SMatthias Ringwald } 888838bd521SMatthias Ringwald 8893deb3ec6SMatthias Ringwald // handle value indication confirms 89038b893aaSMilanka Ringwald if ((opcode == ATT_HANDLE_VALUE_CONFIRMATION) && (att_server->value_indication_handle != 0u)){ 89118707219SMatthias Ringwald btstack_run_loop_remove_timer(&att_server->value_indication_timer); 89218707219SMatthias Ringwald uint16_t att_handle = att_server->value_indication_handle; 89338b893aaSMilanka Ringwald att_server->value_indication_handle = 0u; 89438b893aaSMilanka Ringwald att_handle_value_indication_notify_client(0u, att_connection->con_handle, att_handle); 8953f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 8963deb3ec6SMatthias Ringwald return; 8973deb3ec6SMatthias Ringwald } 8983deb3ec6SMatthias Ringwald 8996428eb5aSMatthias Ringwald // directly process command 9006428eb5aSMatthias Ringwald // note: signed write cannot be handled directly as authentication needs to be verified 901838bd521SMatthias Ringwald if (opcode == ATT_WRITE_COMMAND){ 902b6df12b6SMatthias Ringwald att_handle_request(att_connection, packet, size, NULL); 9036428eb5aSMatthias Ringwald return; 9046428eb5aSMatthias Ringwald } 9056428eb5aSMatthias Ringwald 9063deb3ec6SMatthias Ringwald // check size 90718707219SMatthias Ringwald if (size > sizeof(att_server->request_buffer)) { 908e0463bdcSMatthias Ringwald log_info("drop att pdu 0x%02x as size %u > att_server->request_buffer %u", packet[0], size, (int) sizeof(att_server->request_buffer)); 9096428eb5aSMatthias Ringwald return; 9106428eb5aSMatthias Ringwald } 9113deb3ec6SMatthias Ringwald 912e0463bdcSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 913e0463bdcSMatthias Ringwald // abort signed write validation if a new request comes in (but finish previous signed write if possible) 914e0463bdcSMatthias Ringwald if (att_server->state == ATT_SERVER_W4_SIGNED_WRITE_VALIDATION){ 915e0463bdcSMatthias Ringwald if (packet[0] == ATT_SIGNED_WRITE_COMMAND){ 916e0463bdcSMatthias Ringwald log_info("skip new signed write request as previous is in validation"); 917e0463bdcSMatthias Ringwald return; 918e0463bdcSMatthias Ringwald } else { 919e0463bdcSMatthias Ringwald log_info("abort signed write validation to process new request"); 920e0463bdcSMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 921e0463bdcSMatthias Ringwald } 922e0463bdcSMatthias Ringwald } 923e0463bdcSMatthias Ringwald #endif 9243deb3ec6SMatthias Ringwald // last request still in processing? 92518707219SMatthias Ringwald if (att_server->state != ATT_SERVER_IDLE){ 926e0463bdcSMatthias Ringwald log_info("skip att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state); 9276428eb5aSMatthias Ringwald return; 9286428eb5aSMatthias Ringwald } 9293deb3ec6SMatthias Ringwald 9303deb3ec6SMatthias Ringwald // store request 93118707219SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED; 93218707219SMatthias Ringwald att_server->request_size = size; 9336535961aSMatthias Ringwald (void)memcpy(att_server->request_buffer, packet, size); 9343deb3ec6SMatthias Ringwald 935cd45e970SMatthias Ringwald att_run_for_context(att_server, att_connection); 93641772f37SMatthias Ringwald } 93741772f37SMatthias Ringwald 93841772f37SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){ 93968591e36SMatthias Ringwald hci_connection_t * hci_connection; 940b6df12b6SMatthias Ringwald att_connection_t * att_connection; 94126b42dcfSMatthias Ringwald att_server_t * att_server; 94241772f37SMatthias Ringwald 94341772f37SMatthias Ringwald switch (packet_type){ 94441772f37SMatthias Ringwald case HCI_EVENT_PACKET: 94541772f37SMatthias Ringwald switch (packet[0]){ 94641772f37SMatthias Ringwald case L2CAP_EVENT_CAN_SEND_NOW: 94741772f37SMatthias Ringwald att_server_handle_can_send_now(); 94841772f37SMatthias Ringwald break; 94941772f37SMatthias Ringwald case ATT_EVENT_MTU_EXCHANGE_COMPLETE: 95041772f37SMatthias Ringwald // GATT client has negotiated the mtu for this connection 95168591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(handle); 95268591e36SMatthias Ringwald if (!hci_connection) break; 953388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 954b6df12b6SMatthias Ringwald att_connection->mtu = little_endian_read_16(packet, 4); 95541772f37SMatthias Ringwald break; 95641772f37SMatthias Ringwald default: 95741772f37SMatthias Ringwald break; 95841772f37SMatthias Ringwald } 95941772f37SMatthias Ringwald break; 96041772f37SMatthias Ringwald 96141772f37SMatthias Ringwald case ATT_DATA_PACKET: 96241772f37SMatthias Ringwald log_debug("ATT Packet, handle 0x%04x", handle); 96368591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(handle); 96468591e36SMatthias Ringwald if (!hci_connection) break; 96541772f37SMatthias Ringwald 96626b42dcfSMatthias Ringwald att_server = &hci_connection->att_server; 96726b42dcfSMatthias Ringwald att_connection = &hci_connection->att_connection; 96826b42dcfSMatthias Ringwald att_server_handle_att_pdu(att_server, att_connection, packet, size); 969372d62c4SMatthias Ringwald break; 9707bbeb3adSMilanka Ringwald 9717bbeb3adSMilanka Ringwald default: 9727bbeb3adSMilanka Ringwald break; 973372d62c4SMatthias Ringwald } 9743deb3ec6SMatthias Ringwald } 9753deb3ec6SMatthias Ringwald 976bf78aac6SMatthias Ringwald // --------------------- 977bf78aac6SMatthias Ringwald // persistent CCC writes 978bf78aac6SMatthias Ringwald static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){ 9794ea43905SMatthias Ringwald return ('B' << 24u) | ('T' << 16u) | ('C' << 8u) | index; 980bf78aac6SMatthias Ringwald } 981bf78aac6SMatthias Ringwald 982bf78aac6SMatthias Ringwald static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){ 983bf78aac6SMatthias Ringwald // lookup att_server instance 98468591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 98568591e36SMatthias Ringwald if (!hci_connection) return; 98668591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 987bf78aac6SMatthias Ringwald int le_device_index = att_server->ir_le_device_db_index; 988bf78aac6SMatthias 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); 9896a540790SMatthias Ringwald 990bf78aac6SMatthias Ringwald // check if bonded 991bf78aac6SMatthias Ringwald if (le_device_index < 0) return; 9926a540790SMatthias Ringwald 993bf78aac6SMatthias Ringwald // get btstack_tlv 994bf78aac6SMatthias Ringwald const btstack_tlv_t * tlv_impl = NULL; 995bf78aac6SMatthias Ringwald void * tlv_context; 996bf78aac6SMatthias Ringwald btstack_tlv_get_instance(&tlv_impl, &tlv_context); 997bf78aac6SMatthias Ringwald if (!tlv_impl) return; 9986a540790SMatthias Ringwald 999bf78aac6SMatthias Ringwald // update ccc tag 1000bf78aac6SMatthias Ringwald int index; 10016a540790SMatthias Ringwald uint32_t highest_seq_nr = 0; 10026a540790SMatthias Ringwald uint32_t lowest_seq_nr = 0; 10036a540790SMatthias Ringwald uint32_t tag_for_lowest_seq_nr = 0; 10046a540790SMatthias Ringwald uint32_t tag_for_empty = 0; 10056a540790SMatthias Ringwald persistent_ccc_entry_t entry; 10066afb9acaSMatthias Ringwald for (index=0; index<NVN_NUM_GATT_SERVER_CCC; index++){ 1007bf78aac6SMatthias Ringwald uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 10086a540790SMatthias Ringwald int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 10096a540790SMatthias Ringwald 10106a540790SMatthias Ringwald // empty/invalid tag 10116a540790SMatthias Ringwald if (len != sizeof(persistent_ccc_entry_t)){ 10126a540790SMatthias Ringwald tag_for_empty = tag; 1013bf78aac6SMatthias Ringwald continue; 1014bf78aac6SMatthias Ringwald } 10156a540790SMatthias Ringwald // update highest seq nr 10166a540790SMatthias Ringwald if (entry.seq_nr > highest_seq_nr){ 10176a540790SMatthias Ringwald highest_seq_nr = entry.seq_nr; 10186a540790SMatthias Ringwald } 10196a540790SMatthias Ringwald // find entry with lowest seq nr 10204ea43905SMatthias Ringwald if ((tag_for_lowest_seq_nr == 0u) || (entry.seq_nr < lowest_seq_nr)){ 10216a540790SMatthias Ringwald tag_for_lowest_seq_nr = tag; 10226a540790SMatthias Ringwald lowest_seq_nr = entry.seq_nr; 10236a540790SMatthias Ringwald } 10246a540790SMatthias Ringwald 10256a540790SMatthias Ringwald if (entry.device_index != le_device_index) continue; 10266a540790SMatthias Ringwald if (entry.att_handle != att_handle) continue; 10276a540790SMatthias Ringwald 10286a540790SMatthias Ringwald // found matching entry 10299305033eSMatthias Ringwald if (value != 0){ 1030bf78aac6SMatthias Ringwald // update 10316a540790SMatthias Ringwald if (entry.value == value) { 1032760ad805SMatthias Ringwald log_info("CCC Index %u: Up-to-date", index); 1033760ad805SMatthias Ringwald return; 1034760ad805SMatthias Ringwald } 1035348fc806SMatthias Ringwald entry.value = (uint8_t) value; 10364ea43905SMatthias Ringwald entry.seq_nr = highest_seq_nr + 1u; 1037760ad805SMatthias Ringwald log_info("CCC Index %u: Store", index); 10380fe46bc1SMatthias Ringwald int result = tlv_impl->store_tag(tlv_context, tag, (const uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 10390fe46bc1SMatthias Ringwald if (result != 0){ 10400fe46bc1SMatthias Ringwald log_error("Store tag index %u failed", index); 10410fe46bc1SMatthias Ringwald } 104201ac2008SMatthias Ringwald } else { 104301ac2008SMatthias Ringwald // delete 1044760ad805SMatthias Ringwald log_info("CCC Index %u: Delete", index); 104501ac2008SMatthias Ringwald tlv_impl->delete_tag(tlv_context, tag); 104601ac2008SMatthias Ringwald } 1047bf78aac6SMatthias Ringwald return; 1048bf78aac6SMatthias Ringwald } 10496a540790SMatthias Ringwald 10500fe46bc1SMatthias Ringwald log_info("tag_for_empty %"PRIx32", tag_for_lowest_seq_nr %"PRIx32, tag_for_empty, tag_for_lowest_seq_nr); 10516a540790SMatthias Ringwald 10524ea43905SMatthias Ringwald if (value == 0u){ 10536a540790SMatthias Ringwald // done 10546a540790SMatthias Ringwald return; 10556a540790SMatthias Ringwald } 10566a540790SMatthias Ringwald 105738b893aaSMilanka Ringwald uint32_t tag_to_use = 0u; 105838b893aaSMilanka Ringwald if (tag_for_empty != 0u){ 10596a540790SMatthias Ringwald tag_to_use = tag_for_empty; 10606a540790SMatthias Ringwald } else if (tag_for_lowest_seq_nr){ 10616a540790SMatthias Ringwald tag_to_use = tag_for_lowest_seq_nr; 10626a540790SMatthias Ringwald } else { 10636a540790SMatthias Ringwald // should not happen 10646a540790SMatthias Ringwald return; 10656a540790SMatthias Ringwald } 1066bf78aac6SMatthias Ringwald // store ccc tag 10674ea43905SMatthias Ringwald entry.seq_nr = highest_seq_nr + 1u; 10686a540790SMatthias Ringwald entry.device_index = le_device_index; 10696a540790SMatthias Ringwald entry.att_handle = att_handle; 1070348fc806SMatthias Ringwald entry.value = (uint8_t) value; 10710fe46bc1SMatthias Ringwald int result = tlv_impl->store_tag(tlv_context, tag_to_use, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 10720fe46bc1SMatthias Ringwald if (result != 0){ 10730fe46bc1SMatthias Ringwald log_error("Store tag index %u failed", index); 10740fe46bc1SMatthias Ringwald } 1075bf78aac6SMatthias Ringwald } 1076bf78aac6SMatthias Ringwald 1077fe3d0cbaSMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server){ 1078760ad805SMatthias Ringwald int le_device_index = att_server->ir_le_device_db_index; 1079760ad805SMatthias Ringwald log_info("Clear CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index); 1080760ad805SMatthias Ringwald // check if bonded 1081760ad805SMatthias Ringwald if (le_device_index < 0) return; 1082760ad805SMatthias Ringwald // get btstack_tlv 1083760ad805SMatthias Ringwald const btstack_tlv_t * tlv_impl = NULL; 1084760ad805SMatthias Ringwald void * tlv_context; 1085760ad805SMatthias Ringwald btstack_tlv_get_instance(&tlv_impl, &tlv_context); 1086760ad805SMatthias Ringwald if (!tlv_impl) return; 1087760ad805SMatthias Ringwald // get all ccc tag 1088760ad805SMatthias Ringwald int index; 10896a540790SMatthias Ringwald persistent_ccc_entry_t entry; 10906afb9acaSMatthias Ringwald for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){ 1091760ad805SMatthias Ringwald uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 10926a540790SMatthias Ringwald int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 10936a540790SMatthias Ringwald if (len != sizeof(persistent_ccc_entry_t)) continue; 10946a540790SMatthias Ringwald if (entry.device_index != le_device_index) continue; 1095760ad805SMatthias Ringwald // delete entry 1096760ad805SMatthias Ringwald log_info("CCC Index %u: Delete", index); 1097760ad805SMatthias Ringwald tlv_impl->delete_tag(tlv_context, tag); 1098760ad805SMatthias Ringwald } 1099760ad805SMatthias Ringwald } 1100760ad805SMatthias Ringwald 11019a1f55eaSMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server, att_connection_t * att_connection){ 110201ac2008SMatthias Ringwald int le_device_index = att_server->ir_le_device_db_index; 110301ac2008SMatthias Ringwald log_info("Restore CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index); 11041b7acd0dSMatthias Ringwald // check if bonded 11051b7acd0dSMatthias Ringwald if (le_device_index < 0) return; 110601ac2008SMatthias Ringwald // get btstack_tlv 110701ac2008SMatthias Ringwald const btstack_tlv_t * tlv_impl = NULL; 110801ac2008SMatthias Ringwald void * tlv_context; 110901ac2008SMatthias Ringwald btstack_tlv_get_instance(&tlv_impl, &tlv_context); 111001ac2008SMatthias Ringwald if (!tlv_impl) return; 111101ac2008SMatthias Ringwald // get all ccc tag 111201ac2008SMatthias Ringwald int index; 11136a540790SMatthias Ringwald persistent_ccc_entry_t entry; 11146afb9acaSMatthias Ringwald for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){ 111501ac2008SMatthias Ringwald uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 11166a540790SMatthias Ringwald int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 11176a540790SMatthias Ringwald if (len != sizeof(persistent_ccc_entry_t)) continue; 11186a540790SMatthias Ringwald if (entry.device_index != le_device_index) continue; 111901ac2008SMatthias Ringwald // simulate write callback 11206a540790SMatthias Ringwald uint16_t attribute_handle = entry.att_handle; 112101ac2008SMatthias Ringwald uint8_t value[2]; 11226a540790SMatthias Ringwald little_endian_store_16(value, 0, entry.value); 112301ac2008SMatthias Ringwald att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle); 112401ac2008SMatthias Ringwald if (!callback) continue; 11256a540790SMatthias Ringwald log_info("CCC Index %u: Set Attribute handle 0x%04x to value 0x%04x", index, attribute_handle, entry.value ); 1126b6df12b6SMatthias Ringwald (*callback)(att_connection->con_handle, attribute_handle, ATT_TRANSACTION_MODE_NONE, 0, value, sizeof(value)); 112701ac2008SMatthias Ringwald } 112801ac2008SMatthias Ringwald } 112901ac2008SMatthias Ringwald 1130bf78aac6SMatthias Ringwald // persistent CCC writes 1131bf78aac6SMatthias Ringwald // --------------------- 11323d71c7a4SMatthias Ringwald 11333d71c7a4SMatthias Ringwald // gatt service management 11343d71c7a4SMatthias Ringwald static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){ 11353d71c7a4SMatthias Ringwald btstack_linked_list_iterator_t it; 11363d71c7a4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 11373d71c7a4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 11383d71c7a4SMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 11393d71c7a4SMatthias Ringwald if (handler->start_handle > handle) continue; 11403d71c7a4SMatthias Ringwald if (handler->end_handle < handle) continue; 11413d71c7a4SMatthias Ringwald return handler; 11423d71c7a4SMatthias Ringwald } 11433d71c7a4SMatthias Ringwald return NULL; 11443d71c7a4SMatthias Ringwald } 11453d71c7a4SMatthias Ringwald static att_read_callback_t att_server_read_callback_for_handle(uint16_t handle){ 11463d71c7a4SMatthias Ringwald att_service_handler_t * handler = att_service_handler_for_handle(handle); 11479305033eSMatthias Ringwald if (handler != NULL) return handler->read_callback; 11483d71c7a4SMatthias Ringwald return att_server_client_read_callback; 11493d71c7a4SMatthias Ringwald } 11503d71c7a4SMatthias Ringwald 11513d71c7a4SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){ 11523d71c7a4SMatthias Ringwald att_service_handler_t * handler = att_service_handler_for_handle(handle); 11539305033eSMatthias Ringwald if (handler != NULL) return handler->write_callback; 11543d71c7a4SMatthias Ringwald return att_server_client_write_callback; 11553d71c7a4SMatthias Ringwald } 11563d71c7a4SMatthias Ringwald 11575d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle){ 11585d07396bSMatthias Ringwald att_service_handler_t * handler = att_service_handler_for_handle(handle); 11599305033eSMatthias Ringwald if (handler != NULL) return handler->packet_handler; 11605d07396bSMatthias Ringwald return att_client_packet_handler; 11615d07396bSMatthias Ringwald } 11625d07396bSMatthias Ringwald 11633d71c7a4SMatthias Ringwald static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){ 11643d71c7a4SMatthias Ringwald // notify all callbacks 11653d71c7a4SMatthias Ringwald btstack_linked_list_iterator_t it; 11663d71c7a4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 11673d71c7a4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 11683d71c7a4SMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 11693d71c7a4SMatthias Ringwald if (!handler->write_callback) continue; 11703d71c7a4SMatthias Ringwald (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0); 11713d71c7a4SMatthias Ringwald } 11723d71c7a4SMatthias Ringwald if (!att_server_client_write_callback) return; 11733d71c7a4SMatthias Ringwald (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0); 11743d71c7a4SMatthias Ringwald } 11753d71c7a4SMatthias Ringwald 11763d71c7a4SMatthias Ringwald // returns first reported error or 0 11773d71c7a4SMatthias Ringwald static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){ 11783d71c7a4SMatthias Ringwald btstack_linked_list_iterator_t it; 11793d71c7a4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 11803d71c7a4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 11813d71c7a4SMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 11823d71c7a4SMatthias Ringwald if (!handler->write_callback) continue; 11833d71c7a4SMatthias Ringwald uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0); 118438b893aaSMilanka Ringwald if (error_code != 0u) return error_code; 11853d71c7a4SMatthias Ringwald } 11863d71c7a4SMatthias Ringwald if (!att_server_client_write_callback) return 0; 11873d71c7a4SMatthias Ringwald return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0); 11883d71c7a4SMatthias Ringwald } 11893d71c7a4SMatthias Ringwald 11903d71c7a4SMatthias 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){ 11913d71c7a4SMatthias Ringwald att_read_callback_t callback = att_server_read_callback_for_handle(attribute_handle); 119201ac2008SMatthias Ringwald if (!callback) return 0; 11933d71c7a4SMatthias Ringwald return (*callback)(con_handle, attribute_handle, offset, buffer, buffer_size); 11943d71c7a4SMatthias Ringwald } 11953d71c7a4SMatthias Ringwald 11963d71c7a4SMatthias 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){ 11973d71c7a4SMatthias Ringwald switch (transaction_mode){ 11983d71c7a4SMatthias Ringwald case ATT_TRANSACTION_MODE_VALIDATE: 11993d71c7a4SMatthias Ringwald return att_validate_prepared_write(con_handle); 12003d71c7a4SMatthias Ringwald case ATT_TRANSACTION_MODE_EXECUTE: 12013d71c7a4SMatthias Ringwald case ATT_TRANSACTION_MODE_CANCEL: 12023d71c7a4SMatthias Ringwald att_notify_write_callbacks(con_handle, transaction_mode); 12033d71c7a4SMatthias Ringwald return 0; 12043d71c7a4SMatthias Ringwald default: 12053d71c7a4SMatthias Ringwald break; 12063d71c7a4SMatthias Ringwald } 12073d71c7a4SMatthias Ringwald 1208bf78aac6SMatthias Ringwald // track CCC writes 12094ea43905SMatthias Ringwald if (att_is_persistent_ccc(attribute_handle) && (offset == 0u) && (buffer_size == 2u)){ 1210bf78aac6SMatthias Ringwald att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0)); 1211bf78aac6SMatthias Ringwald } 1212bf78aac6SMatthias Ringwald 121301ac2008SMatthias Ringwald att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle); 121401ac2008SMatthias Ringwald if (!callback) return 0; 12153d71c7a4SMatthias Ringwald return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size); 12163d71c7a4SMatthias Ringwald } 12173d71c7a4SMatthias Ringwald 12183d71c7a4SMatthias Ringwald /** 12193d71c7a4SMatthias Ringwald * @brief register read/write callbacks for specific handle range 12203d71c7a4SMatthias Ringwald * @param att_service_handler_t 12213d71c7a4SMatthias Ringwald */ 12223d71c7a4SMatthias Ringwald void att_server_register_service_handler(att_service_handler_t * handler){ 1223ec8f5414SMilanka Ringwald bool att_server_registered = false; 1224ec8f5414SMilanka Ringwald if (att_service_handler_for_handle(handler->start_handle)){ 1225ec8f5414SMilanka Ringwald att_server_registered = true; 1226ec8f5414SMilanka Ringwald } 1227ec8f5414SMilanka Ringwald 1228ec8f5414SMilanka Ringwald if (att_service_handler_for_handle(handler->end_handle)){ 1229ec8f5414SMilanka Ringwald att_server_registered = true; 1230ec8f5414SMilanka Ringwald } 1231ec8f5414SMilanka Ringwald 1232ec8f5414SMilanka Ringwald if (att_server_registered){ 12333d71c7a4SMatthias Ringwald log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle); 12343d71c7a4SMatthias Ringwald return; 12353d71c7a4SMatthias Ringwald } 12363d71c7a4SMatthias Ringwald btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler); 12373d71c7a4SMatthias Ringwald } 12383d71c7a4SMatthias Ringwald 12393deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){ 12403deb3ec6SMatthias Ringwald 12413d71c7a4SMatthias Ringwald // store callbacks 12423d71c7a4SMatthias Ringwald att_server_client_read_callback = read_callback; 12433d71c7a4SMatthias Ringwald att_server_client_write_callback = write_callback; 12443d71c7a4SMatthias Ringwald 12451a389cdcSMatthias Ringwald // register for HCI Events 1246d9a7306aSMatthias Ringwald hci_event_callback_registration.callback = &att_event_packet_handler; 12471a389cdcSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 12481a389cdcSMatthias Ringwald 1249406722e4SMatthias Ringwald // register for SM events 1250d9a7306aSMatthias Ringwald sm_event_callback_registration.callback = &att_event_packet_handler; 1251406722e4SMatthias Ringwald sm_add_event_handler(&sm_event_callback_registration); 12523deb3ec6SMatthias Ringwald 12531a389cdcSMatthias Ringwald // and L2CAP ATT Server PDUs 12543deb3ec6SMatthias Ringwald att_dispatch_register_server(att_packet_handler); 12553deb3ec6SMatthias Ringwald 125670dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 125770dca4d4SMatthias Ringwald // setup l2cap service 1258f2372b44SMatthias Ringwald l2cap_register_service(&att_event_packet_handler, PSM_ATT, 0xffff, gap_get_security_level()); 125970dca4d4SMatthias Ringwald #endif 126070dca4d4SMatthias Ringwald 12613deb3ec6SMatthias Ringwald att_set_db(db); 12623d71c7a4SMatthias Ringwald att_set_read_callback(att_server_read_callback); 12633d71c7a4SMatthias Ringwald att_set_write_callback(att_server_write_callback); 12643deb3ec6SMatthias Ringwald } 12653deb3ec6SMatthias Ringwald 12663deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){ 12673deb3ec6SMatthias Ringwald att_client_packet_handler = handler; 12683deb3ec6SMatthias Ringwald } 12693deb3ec6SMatthias Ringwald 1270cbbb12d9SMatthias Ringwald 1271cbbb12d9SMatthias Ringwald // to be deprecated 1272c141988cSMatthias Ringwald int att_server_can_send_packet_now(hci_con_handle_t con_handle){ 127368591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 127468591e36SMatthias Ringwald if (!hci_connection) return 0; 127537a8059dSMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 127637a8059dSMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 127737a8059dSMatthias Ringwald return att_server_can_send_packet(att_server, att_connection); 12781b96b007SMatthias Ringwald } 12791b96b007SMatthias Ringwald 12809816429dSMatthias Ringwald uint8_t att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 1281cbbb12d9SMatthias Ringwald return att_server_request_to_send_notification(callback_registration, con_handle); 12823deb3ec6SMatthias Ringwald } 12833deb3ec6SMatthias Ringwald 1284cbbb12d9SMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){ 1285cbbb12d9SMatthias Ringwald att_client_waiting_for_can_send_registration.callback = &att_emit_can_send_now_event; 1286cbbb12d9SMatthias Ringwald att_server_request_to_send_notification(&att_client_waiting_for_can_send_registration, con_handle); 1287cbbb12d9SMatthias Ringwald } 1288cbbb12d9SMatthias Ringwald // end of deprecated 1289cbbb12d9SMatthias Ringwald 12909816429dSMatthias Ringwald uint8_t att_server_request_to_send_notification(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 129168591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 129268591e36SMatthias Ringwald if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 129368591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 12943f9b77dbSMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 1295c37df6f6SMatthias Ringwald bool added = btstack_linked_list_add_tail(&att_server->notification_requests, (btstack_linked_item_t*) callback_registration); 12963f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 1297c37df6f6SMatthias Ringwald if (added){ 1298cbbb12d9SMatthias Ringwald return ERROR_CODE_SUCCESS; 1299c37df6f6SMatthias Ringwald } else { 1300c37df6f6SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1301c37df6f6SMatthias Ringwald } 1302cbbb12d9SMatthias Ringwald } 1303cbbb12d9SMatthias Ringwald 13049816429dSMatthias Ringwald uint8_t att_server_request_to_send_indication(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 130568591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 130668591e36SMatthias Ringwald if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 130768591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 13083f9b77dbSMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 1309c37df6f6SMatthias Ringwald bool added = btstack_linked_list_add_tail(&att_server->indication_requests, (btstack_linked_item_t*) callback_registration); 13103f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 1311c37df6f6SMatthias Ringwald if (added){ 1312969a5bbaSMatthias Ringwald return ERROR_CODE_SUCCESS; 1313c37df6f6SMatthias Ringwald } else { 1314c37df6f6SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1315c37df6f6SMatthias Ringwald } 1316bb38f057SMatthias Ringwald } 1317bb38f057SMatthias Ringwald 13189816429dSMatthias Ringwald uint8_t att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){ 131968591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 132068591e36SMatthias Ringwald if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 132137a8059dSMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 1322388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 132368591e36SMatthias Ringwald 132437a8059dSMatthias Ringwald if (!att_server_can_send_packet(att_server, att_connection)) return BTSTACK_ACL_BUFFERS_FULL; 13253deb3ec6SMatthias Ringwald 13263deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 13273deb3ec6SMatthias Ringwald uint8_t * packet_buffer = l2cap_get_outgoing_buffer(); 1328b6df12b6SMatthias Ringwald uint16_t size = att_prepare_handle_value_notification(att_connection, attribute_handle, value, value_len, packet_buffer); 1329d415948cSMatthias Ringwald 13300f70c52eSMatthias Ringwald return att_server_send_prepared(att_server, att_connection, NULL, size); 13313deb3ec6SMatthias Ringwald } 13323deb3ec6SMatthias Ringwald 13339816429dSMatthias Ringwald uint8_t att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){ 133468591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 133568591e36SMatthias Ringwald if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 133668591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 1337388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 133818707219SMatthias Ringwald 133938b893aaSMilanka Ringwald if (att_server->value_indication_handle != 0u) return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS; 134037a8059dSMatthias Ringwald if (!att_server_can_send_packet(att_server, att_connection)) return BTSTACK_ACL_BUFFERS_FULL; 13413deb3ec6SMatthias Ringwald 13423deb3ec6SMatthias Ringwald // track indication 134318707219SMatthias Ringwald att_server->value_indication_handle = attribute_handle; 134418707219SMatthias Ringwald btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout); 134518707219SMatthias Ringwald btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS); 134618707219SMatthias Ringwald btstack_run_loop_add_timer(&att_server->value_indication_timer); 13473deb3ec6SMatthias Ringwald 13483deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 13493deb3ec6SMatthias Ringwald uint8_t * packet_buffer = l2cap_get_outgoing_buffer(); 1350b6df12b6SMatthias Ringwald uint16_t size = att_prepare_handle_value_indication(att_connection, attribute_handle, value, value_len, packet_buffer); 135123079b39SMatthias Ringwald 13520f70c52eSMatthias Ringwald return att_server_send_prepared(att_server, att_connection, NULL, size); 13533deb3ec6SMatthias Ringwald } 13548f9132b5SMilanka Ringwald 13558f9132b5SMilanka Ringwald uint16_t att_server_get_mtu(hci_con_handle_t con_handle){ 135668591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 135768591e36SMatthias Ringwald if (!hci_connection) return 0; 1358388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 1359b6df12b6SMatthias Ringwald return att_connection->mtu; 13608f9132b5SMilanka Ringwald } 136135d7f2dfSMilanka Ringwald 136235d7f2dfSMilanka Ringwald void att_server_deinit(void){ 136335d7f2dfSMilanka Ringwald att_server_client_read_callback = NULL; 136435d7f2dfSMilanka Ringwald att_server_client_write_callback = NULL; 136535d7f2dfSMilanka Ringwald att_client_packet_handler = NULL; 136635d7f2dfSMilanka Ringwald service_handlers = NULL; 136735d7f2dfSMilanka Ringwald } 136820d4358dSMatthias Ringwald 136920d4358dSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT 13708d3bb68fSMatthias Ringwald 137120d4358dSMatthias Ringwald static uint16_t att_server_eatt_receive_buffer_size; 13728d3bb68fSMatthias Ringwald static uint16_t att_server_eatt_send_buffer_size; 137320d4358dSMatthias Ringwald typedef struct { 137420d4358dSMatthias Ringwald btstack_linked_item_t item; 1375be4ca859SMatthias Ringwald att_server_t att_server; 1376be4ca859SMatthias Ringwald att_connection_t att_connection; 1377735b0222SMatthias Ringwald uint8_t * receive_buffer; 13788d3bb68fSMatthias Ringwald uint8_t * send_buffer; 137920d4358dSMatthias Ringwald } att_server_eatt_bearer_t; 1380be4ca859SMatthias Ringwald 13816b236cb0SMatthias Ringwald static btstack_linked_list_t att_server_eatt_bearer_pool; 13826b236cb0SMatthias Ringwald static btstack_linked_list_t att_server_eatt_bearer_active; 138320d4358dSMatthias Ringwald 1384523bb989SMatthias Ringwald static att_server_eatt_bearer_t * att_server_eatt_bearer_for_cid(uint16_t cid){ 1385523bb989SMatthias Ringwald btstack_linked_list_iterator_t it; 1386523bb989SMatthias Ringwald btstack_linked_list_iterator_init(&it, &att_server_eatt_bearer_active); 1387523bb989SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 1388523bb989SMatthias Ringwald att_server_eatt_bearer_t * eatt_bearer = (att_server_eatt_bearer_t *) btstack_linked_list_iterator_next(&it); 1389f786dd08SMatthias Ringwald if (eatt_bearer->att_server.l2cap_cid == cid) { 1390523bb989SMatthias Ringwald return eatt_bearer; 1391523bb989SMatthias Ringwald } 1392523bb989SMatthias Ringwald } 1393523bb989SMatthias Ringwald return NULL; 1394523bb989SMatthias Ringwald } 1395523bb989SMatthias Ringwald 139620d4358dSMatthias Ringwald static void att_server_eatt_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 139720d4358dSMatthias Ringwald uint16_t cid; 139820d4358dSMatthias Ringwald uint8_t status; 139920d4358dSMatthias Ringwald uint16_t remote_mtu; 140020d4358dSMatthias Ringwald 140120d4358dSMatthias Ringwald uint8_t i; 140220d4358dSMatthias Ringwald uint8_t num_requested_bearers; 140320d4358dSMatthias Ringwald uint8_t num_accepted_bearers; 140420d4358dSMatthias Ringwald uint8_t initial_credits = 5; 140520d4358dSMatthias Ringwald uint8_t * receive_buffers[5]; 140620d4358dSMatthias Ringwald uint16_t cids[5]; 140720d4358dSMatthias Ringwald att_server_eatt_bearer_t * eatt_bearers[5]; 1408523bb989SMatthias Ringwald att_server_eatt_bearer_t * eatt_bearer; 14092954c397SMatthias Ringwald att_server_t * att_server; 14102954c397SMatthias Ringwald att_connection_t * att_connection; 141120d4358dSMatthias Ringwald 141220d4358dSMatthias Ringwald switch (packet_type) { 141320d4358dSMatthias Ringwald 141420d4358dSMatthias Ringwald case L2CAP_DATA_PACKET: 1415f786dd08SMatthias Ringwald eatt_bearer = att_server_eatt_bearer_for_cid(channel); 1416f786dd08SMatthias Ringwald btstack_assert(eatt_bearer != NULL); 1417f786dd08SMatthias Ringwald att_server = &eatt_bearer->att_server; 1418f786dd08SMatthias Ringwald att_connection = &eatt_bearer->att_connection; 1419f786dd08SMatthias Ringwald att_server_handle_att_pdu(att_server, att_connection, packet, size); 142020d4358dSMatthias Ringwald break; 142120d4358dSMatthias Ringwald 142220d4358dSMatthias Ringwald case HCI_EVENT_PACKET: 142320d4358dSMatthias Ringwald switch (packet[0]) { 142420d4358dSMatthias Ringwald 142520d4358dSMatthias Ringwald case L2CAP_EVENT_CAN_SEND_NOW: 1426f786dd08SMatthias Ringwald cid = l2cap_event_packet_sent_get_local_cid(packet); 1427f786dd08SMatthias Ringwald eatt_bearer = att_server_eatt_bearer_for_cid(cid); 1428f786dd08SMatthias Ringwald btstack_assert(eatt_bearer != NULL); 1429f786dd08SMatthias Ringwald att_server = &eatt_bearer->att_server; 1430f786dd08SMatthias Ringwald att_connection = &eatt_bearer->att_connection; 1431f786dd08SMatthias Ringwald // only used for EATT request responses 1432f786dd08SMatthias Ringwald btstack_assert(att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED); 1433f786dd08SMatthias Ringwald att_server_process_validated_request(att_server, att_connection, eatt_bearer->send_buffer); 143420d4358dSMatthias Ringwald break; 143520d4358dSMatthias Ringwald 143620d4358dSMatthias Ringwald case L2CAP_EVENT_PACKET_SENT: 143720d4358dSMatthias Ringwald cid = l2cap_event_packet_sent_get_local_cid(packet); 1438be4ca859SMatthias Ringwald eatt_bearer = att_server_eatt_bearer_for_cid(cid); 1439f786dd08SMatthias Ringwald btstack_assert(eatt_bearer != NULL); 14402954c397SMatthias Ringwald att_server = &eatt_bearer->att_server; 14412954c397SMatthias Ringwald att_connection = &eatt_bearer->att_connection; 14422954c397SMatthias Ringwald att_server_handle_att_pdu(att_server, att_connection, packet, size); 144320d4358dSMatthias Ringwald break; 144420d4358dSMatthias Ringwald 144520d4358dSMatthias Ringwald case L2CAP_EVENT_ECBM_INCOMING_CONNECTION: 144620d4358dSMatthias Ringwald cid = l2cap_event_ecbm_incoming_connection_get_local_cid(packet); 144720d4358dSMatthias Ringwald num_requested_bearers = l2cap_event_ecbm_incoming_connection_get_num_channels(packet); 144820d4358dSMatthias Ringwald for (i = 0; i < num_requested_bearers; i++){ 14496b236cb0SMatthias Ringwald eatt_bearers[i] = (att_server_eatt_bearer_t *) btstack_linked_list_get_first_item(&att_server_eatt_bearer_pool); 145020d4358dSMatthias Ringwald if (eatt_bearers[i] == NULL) { 145120d4358dSMatthias Ringwald break; 145220d4358dSMatthias Ringwald } 1453be4ca859SMatthias Ringwald eatt_bearers[i]->att_connection.con_handle = l2cap_event_ecbm_incoming_connection_get_handle(packet); 1454f786dd08SMatthias Ringwald eatt_bearers[i]->att_server.bearer_type = ATT_BEARER_ENHANCED_LE; 1455735b0222SMatthias Ringwald receive_buffers[i] = eatt_bearers[i]->receive_buffer; 14566b236cb0SMatthias Ringwald btstack_linked_list_add(&att_server_eatt_bearer_active, (btstack_linked_item_t *) eatt_bearers[i]); 145720d4358dSMatthias Ringwald } 145820d4358dSMatthias Ringwald num_accepted_bearers = i; 145920d4358dSMatthias Ringwald status = l2cap_ecbm_accept_channels(cid, num_accepted_bearers, initial_credits, att_server_eatt_receive_buffer_size, receive_buffers, cids); 146020d4358dSMatthias Ringwald btstack_assert(status == ERROR_CODE_SUCCESS); 14616b236cb0SMatthias Ringwald for (i=0;i<num_accepted_bearers;i++){ 1462f786dd08SMatthias Ringwald eatt_bearers[i]->att_server.l2cap_cid = cids[i]; 14636b236cb0SMatthias Ringwald } 146420d4358dSMatthias Ringwald log_info("requested %u, accepted %u", num_requested_bearers, num_accepted_bearers); 146520d4358dSMatthias Ringwald break; 146620d4358dSMatthias Ringwald 146720d4358dSMatthias Ringwald case L2CAP_EVENT_ECBM_CHANNEL_OPENED: 146820d4358dSMatthias Ringwald cid = l2cap_event_ecbm_channel_opened_get_local_cid(packet); 146920d4358dSMatthias Ringwald status = l2cap_event_channel_opened_get_status(packet); 147020d4358dSMatthias Ringwald remote_mtu = l2cap_event_channel_opened_get_remote_mtu(packet); 1471be4ca859SMatthias Ringwald eatt_bearer = att_server_eatt_bearer_for_cid(cid); 1472be4ca859SMatthias Ringwald btstack_assert(eatt_bearer != NULL); 1473be4ca859SMatthias Ringwald eatt_bearer->att_connection.max_mtu = remote_mtu; 147420d4358dSMatthias Ringwald log_info("L2CAP_EVENT_ECBM_CHANNEL_OPENED - cid 0x%04x mtu %u, status 0x%02x", cid, remote_mtu, status); 147520d4358dSMatthias Ringwald break; 147620d4358dSMatthias Ringwald 147720d4358dSMatthias Ringwald case L2CAP_EVENT_ECBM_RECONFIGURED: 147820d4358dSMatthias Ringwald break; 147920d4358dSMatthias Ringwald 1480523bb989SMatthias Ringwald case L2CAP_EVENT_CHANNEL_CLOSED: 1481523bb989SMatthias Ringwald eatt_bearer = att_server_eatt_bearer_for_cid(l2cap_event_channel_closed_get_local_cid(packet)); 1482523bb989SMatthias Ringwald btstack_assert(eatt_bearers != NULL); 1483523bb989SMatthias Ringwald 1484523bb989SMatthias Ringwald // TODO: finalize - abort queued writes 1485523bb989SMatthias Ringwald 1486523bb989SMatthias Ringwald btstack_linked_list_remove(&att_server_eatt_bearer_active, (btstack_linked_item_t *) eatt_bearers); 1487523bb989SMatthias Ringwald btstack_linked_list_add(&att_server_eatt_bearer_pool, (btstack_linked_item_t *) eatt_bearer); 1488523bb989SMatthias Ringwald break; 148920d4358dSMatthias Ringwald default: 149020d4358dSMatthias Ringwald break; 149120d4358dSMatthias Ringwald } 149220d4358dSMatthias Ringwald } 149320d4358dSMatthias Ringwald } 149420d4358dSMatthias Ringwald 149520d4358dSMatthias Ringwald // create eatt bearers 149620d4358dSMatthias Ringwald uint8_t att_server_eatt_init(uint8_t num_eatt_bearers, uint8_t * storage_buffer, uint16_t storage_size){ 149720d4358dSMatthias Ringwald uint16_t size_for_structs = num_eatt_bearers * sizeof(att_server_eatt_bearer_t); 149820d4358dSMatthias Ringwald if (storage_size < size_for_structs) { 149920d4358dSMatthias Ringwald return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED; 150020d4358dSMatthias Ringwald } 150120d4358dSMatthias Ringwald 150220d4358dSMatthias Ringwald // TODO: The minimum ATT_MTU for an Enhanced ATT bearer is 64 octets. 150320d4358dSMatthias Ringwald 150420d4358dSMatthias Ringwald memset(storage_buffer, 0, storage_size); 15058d3bb68fSMatthias Ringwald uint16_t buffer_size_per_bearer = ((storage_size - size_for_structs) / num_eatt_bearers); 15068d3bb68fSMatthias Ringwald att_server_eatt_receive_buffer_size = buffer_size_per_bearer / 2; 15078d3bb68fSMatthias Ringwald att_server_eatt_send_buffer_size = buffer_size_per_bearer / 2; 1508735b0222SMatthias Ringwald uint8_t * bearer_buffer = &storage_buffer[size_for_structs]; 150920d4358dSMatthias Ringwald uint8_t i; 151020d4358dSMatthias Ringwald att_server_eatt_bearer_t * eatt_bearer = (att_server_eatt_bearer_t *) storage_buffer; 1511be4ca859SMatthias Ringwald log_info("%u EATT bearers with receive buffer size %u", 1512be4ca859SMatthias Ringwald num_eatt_bearers, att_server_eatt_receive_buffer_size); 151320d4358dSMatthias Ringwald for (i=0;i<num_eatt_bearers;i++){ 1514be4ca859SMatthias Ringwald eatt_bearer->att_connection.con_handle = HCI_CON_HANDLE_INVALID; 1515735b0222SMatthias Ringwald eatt_bearer->receive_buffer = bearer_buffer; 1516735b0222SMatthias Ringwald bearer_buffer += att_server_eatt_receive_buffer_size; 15178d3bb68fSMatthias Ringwald eatt_bearer->send_buffer = bearer_buffer; 15188d3bb68fSMatthias Ringwald bearer_buffer += att_server_eatt_send_buffer_size; 15196b236cb0SMatthias Ringwald btstack_linked_list_add(&att_server_eatt_bearer_pool, (btstack_linked_item_t *) eatt_bearer); 152020d4358dSMatthias Ringwald eatt_bearer++; 152120d4358dSMatthias Ringwald } 152220d4358dSMatthias Ringwald // TODO: define minimum EATT MTU 152320d4358dSMatthias Ringwald l2cap_ecbm_register_service(att_server_eatt_handler, BLUETOOTH_PSM_EATT, 64, 0); 152420d4358dSMatthias Ringwald 152520d4358dSMatthias Ringwald return 0; 152620d4358dSMatthias Ringwald } 152720d4358dSMatthias Ringwald #endif