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 115c2cbe117SMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT 116c2cbe117SMatthias Ringwald typedef struct { 117c2cbe117SMatthias Ringwald btstack_linked_item_t item; 118c2cbe117SMatthias Ringwald att_server_t att_server; 119c2cbe117SMatthias Ringwald att_connection_t att_connection; 120c2cbe117SMatthias Ringwald uint8_t * receive_buffer; 121c2cbe117SMatthias Ringwald uint8_t * send_buffer; 122c2cbe117SMatthias Ringwald } att_server_eatt_bearer_t; 123*40f8d8f2SMatthias Ringwald static att_server_eatt_bearer_t * att_server_eatt_bearer_for_con_handle(hci_con_handle_t con_handle); 124c2cbe117SMatthias Ringwald static btstack_linked_list_t att_server_eatt_bearer_pool; 125c2cbe117SMatthias Ringwald static btstack_linked_list_t att_server_eatt_bearer_active; 126c2cbe117SMatthias Ringwald #endif 127c2cbe117SMatthias Ringwald 12802b78fc8SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 12926567ee7SMatthias 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){ 13018707219SMatthias Ringwald btstack_linked_list_iterator_t it; 13118707219SMatthias Ringwald hci_connections_get_iterator(&it); 13218707219SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 13326567ee7SMatthias Ringwald hci_connection_t * hci_connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 13426567ee7SMatthias Ringwald if (hci_connection->att_server.state == state) { 13526567ee7SMatthias Ringwald *att_server_out = &hci_connection->att_server; 13626567ee7SMatthias Ringwald *att_connection_out = &hci_connection->att_connection; 13726567ee7SMatthias Ringwald return true; 13818707219SMatthias Ringwald } 13926567ee7SMatthias Ringwald } 140c2cbe117SMatthias Ringwald 141c2cbe117SMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT 142c2cbe117SMatthias Ringwald btstack_linked_list_iterator_init(&it, &att_server_eatt_bearer_active); 143c2cbe117SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 144c2cbe117SMatthias Ringwald att_server_eatt_bearer_t * eatt_bearer = (att_server_eatt_bearer_t *) btstack_linked_list_iterator_next(&it); 145c2cbe117SMatthias Ringwald if (eatt_bearer->att_server.state == state) { 146c2cbe117SMatthias Ringwald *att_server_out = &eatt_bearer->att_server; 147c2cbe117SMatthias Ringwald *att_connection_out = &eatt_bearer->att_connection; 148c2cbe117SMatthias Ringwald return true; 149c2cbe117SMatthias Ringwald } 150c2cbe117SMatthias Ringwald } 151c2cbe117SMatthias Ringwald #endif 152c2cbe117SMatthias Ringwald 15326567ee7SMatthias Ringwald return false; 15418707219SMatthias Ringwald } 15502b78fc8SMatthias Ringwald #endif 156bb38f057SMatthias Ringwald 1573f9b77dbSMatthias Ringwald static void att_server_request_can_send_now(att_server_t * att_server, att_connection_t * att_connection ){ 15823079b39SMatthias Ringwald switch (att_server->bearer_type){ 15923079b39SMatthias Ringwald case ATT_BEARER_UNENHANCED_LE: 160b6df12b6SMatthias Ringwald att_dispatch_server_request_can_send_now_event(att_connection->con_handle); 16123079b39SMatthias Ringwald break; 1622954c397SMatthias Ringwald #if defined (ENABLE_GATT_OVER_CLASSIC) || defined (ENABLE_GATT_OVER_EATT) 16323079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 16423079b39SMatthias Ringwald case ATT_BEARER_UNENHANCED_CLASSIC: 1652954c397SMatthias Ringwald /* fall through */ 1662954c397SMatthias Ringwald #endif 1672954c397SMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT 1682954c397SMatthias Ringwald case ATT_BEARER_ENHANCED_LE: 1692954c397SMatthias Ringwald /* fall through */ 1702954c397SMatthias Ringwald #endif 17123079b39SMatthias Ringwald l2cap_request_can_send_now_event(att_server->l2cap_cid); 17223079b39SMatthias Ringwald break; 17323079b39SMatthias Ringwald #endif 17423079b39SMatthias Ringwald default: 17523079b39SMatthias Ringwald btstack_unreachable(); 17623079b39SMatthias Ringwald break; 17723079b39SMatthias Ringwald } 178c8c6e9b4SMatthias Ringwald } 179c8c6e9b4SMatthias Ringwald 18037a8059dSMatthias Ringwald static bool att_server_can_send_packet(att_server_t * att_server, att_connection_t * att_connection){ 18123079b39SMatthias Ringwald switch (att_server->bearer_type) { 18223079b39SMatthias Ringwald case ATT_BEARER_UNENHANCED_LE: 1831979f09cSMatthias Ringwald return att_dispatch_server_can_send_now(att_connection->con_handle) != 0; 1842954c397SMatthias Ringwald #if defined (ENABLE_GATT_OVER_CLASSIC) || defined (ENABLE_GATT_OVER_EATT) 18523079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 18623079b39SMatthias Ringwald case ATT_BEARER_UNENHANCED_CLASSIC: 1872954c397SMatthias Ringwald /* fall through */ 1882954c397SMatthias Ringwald #endif 1892954c397SMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT 1902954c397SMatthias Ringwald case ATT_BEARER_ENHANCED_LE: 1912954c397SMatthias Ringwald #endif 1922954c397SMatthias Ringwald return l2cap_can_send_packet_now(att_server->l2cap_cid); 19323079b39SMatthias Ringwald #endif 19423079b39SMatthias Ringwald default: 19523079b39SMatthias Ringwald btstack_unreachable(); 19623079b39SMatthias Ringwald break; 19723079b39SMatthias Ringwald } 19823079b39SMatthias Ringwald return false; 199c0f26e1cSMatthias Ringwald } 200c0f26e1cSMatthias Ringwald 2013deb3ec6SMatthias Ringwald static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){ 2025d07396bSMatthias Ringwald btstack_packet_handler_t packet_handler = att_server_packet_handler_for_handle(attribute_handle); 2035d07396bSMatthias Ringwald if (!packet_handler) return; 2043deb3ec6SMatthias Ringwald 2053deb3ec6SMatthias Ringwald uint8_t event[7]; 2063deb3ec6SMatthias Ringwald int pos = 0; 2075611a760SMatthias Ringwald event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE; 2084ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 2093deb3ec6SMatthias Ringwald event[pos++] = status; 210f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, client_handle); 2113deb3ec6SMatthias Ringwald pos += 2; 212f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, attribute_handle); 213a444112bSMatthias Ringwald (*packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 2143deb3ec6SMatthias Ringwald } 2153deb3ec6SMatthias Ringwald 2165d07396bSMatthias Ringwald static void att_emit_event_to_all(const uint8_t * event, uint16_t size){ 2175d07396bSMatthias Ringwald // dispatch to app level handler 2189305033eSMatthias Ringwald if (att_client_packet_handler != NULL){ 2195d07396bSMatthias Ringwald (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size); 2205d07396bSMatthias Ringwald } 2213deb3ec6SMatthias Ringwald 2225d07396bSMatthias Ringwald // dispatch to service handlers 2235d07396bSMatthias Ringwald btstack_linked_list_iterator_t it; 2245d07396bSMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 2255d07396bSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2265d07396bSMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 2275d07396bSMatthias Ringwald if (!handler->packet_handler) continue; 2285d07396bSMatthias Ringwald (*handler->packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size); 2295d07396bSMatthias Ringwald } 2305d07396bSMatthias Ringwald } 2315d07396bSMatthias Ringwald 2325d07396bSMatthias Ringwald static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){ 2333deb3ec6SMatthias Ringwald uint8_t event[6]; 2343deb3ec6SMatthias Ringwald int pos = 0; 2355611a760SMatthias Ringwald event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE; 2364ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 237fc64f94aSMatthias Ringwald little_endian_store_16(event, pos, con_handle); 2383deb3ec6SMatthias Ringwald pos += 2; 239f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, mtu); 2405d07396bSMatthias Ringwald 2415d07396bSMatthias Ringwald // also dispatch to GATT Clients 242b12646c5SJakob Krantz att_dispatch_server_mtu_exchanged(con_handle, mtu); 2435d07396bSMatthias Ringwald 2445d07396bSMatthias Ringwald // dispatch to app level handler and service handlers 2455d07396bSMatthias Ringwald att_emit_event_to_all(&event[0], sizeof(event)); 2463deb3ec6SMatthias Ringwald } 2473deb3ec6SMatthias Ringwald 2485f141511SMatthias Ringwald static void att_emit_can_send_now_event(void * context){ 2495f141511SMatthias Ringwald UNUSED(context); 2503c97b242SMatthias Ringwald if (!att_client_packet_handler) return; 2513c97b242SMatthias Ringwald 2521b96b007SMatthias Ringwald uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0}; 2531b96b007SMatthias Ringwald (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 2541b96b007SMatthias Ringwald } 2551b96b007SMatthias Ringwald 2566d2214cbSMatthias Ringwald static void att_emit_connected_event(att_server_t * att_server, att_connection_t * att_connection){ 257ff1bec95SMatthias Ringwald uint8_t event[11]; 258ff1bec95SMatthias Ringwald int pos = 0; 259ff1bec95SMatthias Ringwald event[pos++] = ATT_EVENT_CONNECTED; 2604ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 261ff1bec95SMatthias Ringwald event[pos++] = att_server->peer_addr_type; 262ff1bec95SMatthias Ringwald reverse_bd_addr(att_server->peer_address, &event[pos]); 263ff1bec95SMatthias Ringwald pos += 6; 264b6df12b6SMatthias Ringwald little_endian_store_16(event, pos, att_connection->con_handle); 265ff1bec95SMatthias Ringwald pos += 2; 266ff1bec95SMatthias Ringwald 267ff1bec95SMatthias Ringwald // dispatch to app level handler and service handlers 268ff1bec95SMatthias Ringwald att_emit_event_to_all(&event[0], sizeof(event)); 269ff1bec95SMatthias Ringwald } 270ff1bec95SMatthias Ringwald 271ff1bec95SMatthias Ringwald 2726187ad4cSMatthias Ringwald static void att_emit_disconnected_event(uint16_t con_handle){ 273ff1bec95SMatthias Ringwald uint8_t event[4]; 274ff1bec95SMatthias Ringwald int pos = 0; 275ff1bec95SMatthias Ringwald event[pos++] = ATT_EVENT_DISCONNECTED; 2764ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 2776187ad4cSMatthias Ringwald little_endian_store_16(event, pos, con_handle); 278ff1bec95SMatthias Ringwald pos += 2; 279ff1bec95SMatthias Ringwald 280ff1bec95SMatthias Ringwald // dispatch to app level handler and service handlers 281ff1bec95SMatthias Ringwald att_emit_event_to_all(&event[0], sizeof(event)); 282ff1bec95SMatthias Ringwald } 283ff1bec95SMatthias Ringwald 284ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){ 28554178543SMatthias Ringwald void * context = btstack_run_loop_get_timer_context(ts); 28654178543SMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context; 28768591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 28868591e36SMatthias Ringwald if (!hci_connection) return; 28968591e36SMatthias Ringwald // @note: after a transaction timeout, no more requests shall be sent over this ATT Bearer 290bce48a26SMatthias Ringwald // (that's why we don't reset the value_indication_handle) 291b6df12b6SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 29218707219SMatthias Ringwald uint16_t att_handle = att_server->value_indication_handle; 293388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 29438b893aaSMilanka Ringwald att_handle_value_indication_notify_client((uint8_t)ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_connection->con_handle, att_handle); 2953deb3ec6SMatthias Ringwald } 2963deb3ec6SMatthias Ringwald 2973deb3ec6SMatthias Ringwald static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2989ec2630cSMatthias Ringwald 2997dad9ff8SMatthias Ringwald UNUSED(channel); // ok: there is no channel 3007dad9ff8SMatthias Ringwald UNUSED(size); // ok: handling own l2cap events 3013deb3ec6SMatthias Ringwald 30218707219SMatthias Ringwald att_server_t * att_server; 303b6df12b6SMatthias Ringwald att_connection_t * att_connection; 30418707219SMatthias Ringwald hci_con_handle_t con_handle; 30568591e36SMatthias Ringwald hci_connection_t * hci_connection; 30670dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 30770dca4d4SMatthias Ringwald bd_addr_t address; 30868591e36SMatthias Ringwald btstack_linked_list_iterator_t it; 30970dca4d4SMatthias Ringwald #endif 31018707219SMatthias Ringwald 3113deb3ec6SMatthias Ringwald switch (packet_type) { 3123deb3ec6SMatthias Ringwald 3133deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 3140e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 3153deb3ec6SMatthias Ringwald 31670dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 31770dca4d4SMatthias Ringwald case L2CAP_EVENT_INCOMING_CONNECTION: 31870dca4d4SMatthias Ringwald l2cap_event_incoming_connection_get_address(packet, address); 31970dca4d4SMatthias Ringwald l2cap_accept_connection(channel); 32036de8547SMatthias Ringwald log_info("Accept incoming connection from %s", bd_addr_to_str(address)); 32170dca4d4SMatthias Ringwald break; 32270dca4d4SMatthias Ringwald case L2CAP_EVENT_CHANNEL_OPENED: 323122b723eSMatthias Ringwald con_handle = l2cap_event_channel_opened_get_handle(packet); 32468591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 32568591e36SMatthias Ringwald if (!hci_connection) break; 32668591e36SMatthias Ringwald att_server = &hci_connection->att_server; 327122b723eSMatthias Ringwald // store connection info 32823079b39SMatthias Ringwald att_server->bearer_type = ATT_BEARER_UNENHANCED_CLASSIC; 329f16129ceSMatthias Ringwald att_server->peer_addr_type = BD_ADDR_TYPE_ACL; 330122b723eSMatthias Ringwald l2cap_event_channel_opened_get_address(packet, att_server->peer_address); 331388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 332b6df12b6SMatthias Ringwald att_connection->con_handle = con_handle; 33370dca4d4SMatthias Ringwald att_server->l2cap_cid = l2cap_event_channel_opened_get_local_cid(packet); 334122b723eSMatthias Ringwald // reset connection properties 335122b723eSMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 336b6df12b6SMatthias Ringwald att_connection->mtu = l2cap_event_channel_opened_get_remote_mtu(packet); 337b6df12b6SMatthias Ringwald att_connection->max_mtu = l2cap_max_mtu(); 338b6df12b6SMatthias Ringwald if (att_connection->max_mtu > ATT_REQUEST_BUFFER_SIZE){ 339b6df12b6SMatthias Ringwald att_connection->max_mtu = ATT_REQUEST_BUFFER_SIZE; 340122b723eSMatthias Ringwald } 341df33ee14SMatthias Ringwald 342b6df12b6SMatthias Ringwald log_info("Connection opened %s, l2cap cid %04x, mtu %u", bd_addr_to_str(address), att_server->l2cap_cid, att_connection->mtu); 343df33ee14SMatthias Ringwald 344914988a1SMatthias Ringwald // update security params 345b6df12b6SMatthias Ringwald att_connection->encryption_key_size = gap_encryption_key_size(con_handle); 346b6df12b6SMatthias Ringwald att_connection->authenticated = gap_authenticated(con_handle); 347b6df12b6SMatthias Ringwald att_connection->secure_connection = gap_secure_connection(con_handle); 348914988a1SMatthias Ringwald log_info("encrypted key size %u, authenticated %u, secure connection %u", 349b6df12b6SMatthias Ringwald att_connection->encryption_key_size, att_connection->authenticated, att_connection->secure_connection); 350df33ee14SMatthias Ringwald 351df33ee14SMatthias Ringwald // notify connection opened 3526d2214cbSMatthias Ringwald att_emit_connected_event(att_server, att_connection); 353df33ee14SMatthias Ringwald 354914988a1SMatthias Ringwald // restore persisten ccc if encrypted 355914988a1SMatthias Ringwald if ( gap_security_level(con_handle) >= LEVEL_2){ 3569a1f55eaSMatthias Ringwald att_server_persistent_ccc_restore(att_server, att_connection); 357914988a1SMatthias Ringwald } 358122b723eSMatthias Ringwald // TODO: what to do about le device db? 359122b723eSMatthias Ringwald att_server->pairing_active = 0; 36070dca4d4SMatthias Ringwald break; 36148364364SMatthias Ringwald case L2CAP_EVENT_CAN_SEND_NOW: 36248364364SMatthias Ringwald att_server_handle_can_send_now(); 36348364364SMatthias Ringwald break; 36448364364SMatthias Ringwald 36570dca4d4SMatthias Ringwald #endif 3663deb3ec6SMatthias Ringwald case HCI_EVENT_LE_META: 3673deb3ec6SMatthias Ringwald switch (packet[2]) { 3683deb3ec6SMatthias Ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 36918707219SMatthias Ringwald con_handle = little_endian_read_16(packet, 4); 37068591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 37168591e36SMatthias Ringwald if (!hci_connection) break; 37268591e36SMatthias Ringwald att_server = &hci_connection->att_server; 3733deb3ec6SMatthias Ringwald // store connection info 37418707219SMatthias Ringwald att_server->peer_addr_type = packet[7]; 37518707219SMatthias Ringwald reverse_bd_addr(&packet[8], att_server->peer_address); 376388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 377b6df12b6SMatthias Ringwald att_connection->con_handle = con_handle; 3783deb3ec6SMatthias Ringwald // reset connection properties 37918707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 38023079b39SMatthias Ringwald att_server->bearer_type = ATT_BEARER_UNENHANCED_LE; 381b6df12b6SMatthias Ringwald att_connection->mtu = ATT_DEFAULT_MTU; 382b6df12b6SMatthias Ringwald att_connection->max_mtu = l2cap_max_le_mtu(); 383b6df12b6SMatthias Ringwald if (att_connection->max_mtu > ATT_REQUEST_BUFFER_SIZE){ 384b6df12b6SMatthias Ringwald att_connection->max_mtu = ATT_REQUEST_BUFFER_SIZE; 38599c58ad0SMatthias Ringwald } 38638b893aaSMilanka Ringwald att_connection->encryption_key_size = 0u; 38738b893aaSMilanka Ringwald att_connection->authenticated = 0u; 38838b893aaSMilanka Ringwald att_connection->authorized = 0u; 389b2c1e52cSMatthias Ringwald // workaround: identity resolving can already be complete, at least store result 390b2c1e52cSMatthias Ringwald att_server->ir_le_device_db_index = sm_le_device_index(con_handle); 39138b893aaSMilanka Ringwald att_server->ir_lookup_active = 0u; 39238b893aaSMilanka Ringwald att_server->pairing_active = 0u; 393ff1bec95SMatthias Ringwald // notify all - old 3945d07396bSMatthias Ringwald att_emit_event_to_all(packet, size); 395ff1bec95SMatthias Ringwald // notify all - new 3966d2214cbSMatthias Ringwald att_emit_connected_event(att_server, att_connection); 3973deb3ec6SMatthias Ringwald break; 3983deb3ec6SMatthias Ringwald 3993deb3ec6SMatthias Ringwald default: 4003deb3ec6SMatthias Ringwald break; 4013deb3ec6SMatthias Ringwald } 4023deb3ec6SMatthias Ringwald break; 4033deb3ec6SMatthias Ringwald 4043deb3ec6SMatthias Ringwald case HCI_EVENT_ENCRYPTION_CHANGE: 4058601e696SMatthias Ringwald case HCI_EVENT_ENCRYPTION_CHANGE_V2: 4063deb3ec6SMatthias Ringwald case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE: 4073deb3ec6SMatthias Ringwald // check handle 40818707219SMatthias Ringwald con_handle = little_endian_read_16(packet, 3); 40968591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 41068591e36SMatthias Ringwald if (!hci_connection) break; 411914988a1SMatthias Ringwald if (gap_get_connection_type(con_handle) != GAP_CONNECTION_LE) break; 412914988a1SMatthias Ringwald // update security params 41368591e36SMatthias Ringwald att_server = &hci_connection->att_server; 414388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 415b6df12b6SMatthias Ringwald att_connection->encryption_key_size = gap_encryption_key_size(con_handle); 416ff3f1026SMatthias Ringwald att_connection->authenticated = gap_authenticated(con_handle) ? 1 : 0; 417f461b3dfSMatthias Ringwald att_connection->secure_connection = gap_secure_connection(con_handle) ? 1 : 0; 418914988a1SMatthias Ringwald log_info("encrypted key size %u, authenticated %u, secure connection %u", 419b6df12b6SMatthias Ringwald att_connection->encryption_key_size, att_connection->authenticated, att_connection->secure_connection); 420914988a1SMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){ 4214097998aSMatthias Ringwald // restore CCC values when encrypted for LE Connections 42222bfc225SMilanka Ringwald if (hci_event_encryption_change_get_encryption_enabled(packet) != 0){ 4239a1f55eaSMatthias Ringwald att_server_persistent_ccc_restore(att_server, att_connection); 42401ac2008SMatthias Ringwald } 42501ac2008SMatthias Ringwald } 426cd45e970SMatthias Ringwald att_run_for_context(att_server, att_connection); 4273deb3ec6SMatthias Ringwald break; 4283deb3ec6SMatthias Ringwald 4293deb3ec6SMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 43070a390c7SMatthias Ringwald // check handle 43118707219SMatthias Ringwald con_handle = hci_event_disconnection_complete_get_connection_handle(packet); 43268591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 43368591e36SMatthias Ringwald if (!hci_connection) break; 43468591e36SMatthias Ringwald att_server = &hci_connection->att_server; 435388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 436b6df12b6SMatthias Ringwald att_clear_transaction_queue(att_connection); 437b6df12b6SMatthias Ringwald att_connection->con_handle = 0; 438760ad805SMatthias Ringwald att_server->pairing_active = 0; 43918707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 44038b893aaSMilanka Ringwald if (att_server->value_indication_handle != 0u){ 44127328ecbSMatthias Ringwald btstack_run_loop_remove_timer(&att_server->value_indication_timer); 442bce48a26SMatthias Ringwald uint16_t att_handle = att_server->value_indication_handle; 44338b893aaSMilanka Ringwald att_server->value_indication_handle = 0u; // reset error state 44438b893aaSMilanka Ringwald att_handle_value_indication_notify_client((uint8_t)ATT_HANDLE_VALUE_INDICATION_DISCONNECT, att_connection->con_handle, att_handle); 445bce48a26SMatthias Ringwald } 446ff1bec95SMatthias Ringwald // notify all - new 4476187ad4cSMatthias Ringwald att_emit_disconnected_event(con_handle); 448ff1bec95SMatthias Ringwald // notify all - old 4495d07396bSMatthias Ringwald att_emit_event_to_all(packet, size); 4503deb3ec6SMatthias Ringwald break; 4513deb3ec6SMatthias Ringwald 452760ad805SMatthias Ringwald // Identity Resolving 4535611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_STARTED: 45418707219SMatthias Ringwald con_handle = sm_event_identity_resolving_started_get_handle(packet); 45568591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 45668591e36SMatthias Ringwald if (!hci_connection) break; 45768591e36SMatthias Ringwald att_server = &hci_connection->att_server; 4585611a760SMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED"); 45930c8e114SMatthias Ringwald att_server->ir_lookup_active = 1; 4603deb3ec6SMatthias Ringwald break; 4615611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED: 462760ad805SMatthias Ringwald con_handle = sm_event_identity_created_get_handle(packet); 46368591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 46468591e36SMatthias Ringwald if (!hci_connection) return; 46568591e36SMatthias Ringwald att_server = &hci_connection->att_server; 46630c8e114SMatthias Ringwald att_server->ir_lookup_active = 0; 4671b7acd0dSMatthias Ringwald att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index(packet); 4681b7acd0dSMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED"); 469cd45e970SMatthias Ringwald att_run_for_context(att_server, att_connection); 4703deb3ec6SMatthias Ringwald break; 4715611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_FAILED: 47218707219SMatthias Ringwald con_handle = sm_event_identity_resolving_failed_get_handle(packet); 47368591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 47468591e36SMatthias Ringwald if (!hci_connection) break; 47568591e36SMatthias Ringwald att_server = &hci_connection->att_server; 4765611a760SMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED"); 47730c8e114SMatthias Ringwald att_server->ir_lookup_active = 0; 47818707219SMatthias Ringwald att_server->ir_le_device_db_index = -1; 479cd45e970SMatthias Ringwald att_run_for_context(att_server, att_connection); 4803deb3ec6SMatthias Ringwald break; 481760ad805SMatthias Ringwald 482760ad805SMatthias Ringwald // Pairing started - delete stored CCC values 483760ad805SMatthias Ringwald // - assumes pairing indicates either new device or re-pairing, in both cases there should be no stored CCC values 484760ad805SMatthias Ringwald // - assumes that all events have the con handle as the first field 485760ad805SMatthias Ringwald case SM_EVENT_JUST_WORKS_REQUEST: 486760ad805SMatthias Ringwald case SM_EVENT_PASSKEY_DISPLAY_NUMBER: 487760ad805SMatthias Ringwald case SM_EVENT_PASSKEY_INPUT_NUMBER: 488760ad805SMatthias Ringwald case SM_EVENT_NUMERIC_COMPARISON_REQUEST: 489760ad805SMatthias Ringwald con_handle = sm_event_just_works_request_get_handle(packet); 49068591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 49168591e36SMatthias Ringwald if (!hci_connection) break; 49268591e36SMatthias Ringwald att_server = &hci_connection->att_server; 493760ad805SMatthias Ringwald att_server->pairing_active = 1; 494760ad805SMatthias Ringwald log_info("SM Pairing started"); 495760ad805SMatthias Ringwald if (att_server->ir_le_device_db_index < 0) break; 496cd45e970SMatthias Ringwald att_run_for_context(att_server, att_connection); 497760ad805SMatthias Ringwald // index not valid anymore 498760ad805SMatthias Ringwald att_server->ir_le_device_db_index = -1; 499760ad805SMatthias Ringwald break; 500760ad805SMatthias Ringwald 5011b7acd0dSMatthias Ringwald // Bonding completed 502760ad805SMatthias Ringwald case SM_EVENT_IDENTITY_CREATED: 503760ad805SMatthias Ringwald con_handle = sm_event_identity_created_get_handle(packet); 50468591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 50568591e36SMatthias Ringwald if (!hci_connection) return; 50668591e36SMatthias Ringwald att_server = &hci_connection->att_server; 507760ad805SMatthias Ringwald att_server->pairing_active = 0; 5081b7acd0dSMatthias Ringwald att_server->ir_le_device_db_index = sm_event_identity_created_get_index(packet); 509cd45e970SMatthias Ringwald att_run_for_context(att_server, att_connection); 5101b7acd0dSMatthias Ringwald break; 5111b7acd0dSMatthias Ringwald 5121b7acd0dSMatthias Ringwald // Pairing complete (with/without bonding=storing of pairing information) 5131b7acd0dSMatthias Ringwald case SM_EVENT_PAIRING_COMPLETE: 5141b7acd0dSMatthias Ringwald con_handle = sm_event_pairing_complete_get_handle(packet); 51568591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 51668591e36SMatthias Ringwald if (!hci_connection) return; 51768591e36SMatthias Ringwald att_server = &hci_connection->att_server; 5181b7acd0dSMatthias Ringwald att_server->pairing_active = 0; 519cd45e970SMatthias Ringwald att_run_for_context(att_server, att_connection); 520760ad805SMatthias Ringwald break; 521760ad805SMatthias Ringwald 522760ad805SMatthias Ringwald // Authorization 5235611a760SMatthias Ringwald case SM_EVENT_AUTHORIZATION_RESULT: { 52418707219SMatthias Ringwald con_handle = sm_event_authorization_result_get_handle(packet); 52568591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 52668591e36SMatthias Ringwald if (!hci_connection) break; 52768591e36SMatthias Ringwald att_server = &hci_connection->att_server; 528388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 529b6df12b6SMatthias Ringwald att_connection->authorized = sm_event_authorization_result_get_authorization_result(packet); 5303f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 5313deb3ec6SMatthias Ringwald break; 5323deb3ec6SMatthias Ringwald } 5333deb3ec6SMatthias Ringwald default: 5343deb3ec6SMatthias Ringwald break; 5353deb3ec6SMatthias Ringwald } 53665b44ffdSMatthias Ringwald break; 53770dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 53870dca4d4SMatthias Ringwald case L2CAP_DATA_PACKET: 53968591e36SMatthias Ringwald hci_connections_get_iterator(&it); 54068591e36SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 541388fa7c4SMatthias Ringwald hci_connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 542388fa7c4SMatthias Ringwald att_server = &hci_connection->att_server; 54326b42dcfSMatthias Ringwald att_connection = &hci_connection->att_connection; 54468591e36SMatthias Ringwald if (att_server->l2cap_cid == channel) { 54526b42dcfSMatthias Ringwald att_server_handle_att_pdu(att_server, att_connection, packet, size); 54668591e36SMatthias Ringwald break; 54768591e36SMatthias Ringwald } 54868591e36SMatthias Ringwald } 54970dca4d4SMatthias Ringwald break; 55070dca4d4SMatthias Ringwald #endif 55141772f37SMatthias Ringwald 55265b44ffdSMatthias Ringwald default: 55365b44ffdSMatthias Ringwald break; 5543deb3ec6SMatthias Ringwald } 5553deb3ec6SMatthias Ringwald } 5563deb3ec6SMatthias Ringwald 5570f70c52eSMatthias Ringwald static uint8_t 5580f70c52eSMatthias Ringwald att_server_send_prepared(const att_server_t *att_server, const att_connection_t *att_connection, uint8_t *buffer, 5590f70c52eSMatthias Ringwald uint16_t size) { 560d415948cSMatthias Ringwald uint8_t status = ERROR_CODE_SUCCESS; 561d415948cSMatthias Ringwald switch (att_server->bearer_type) { 562d415948cSMatthias Ringwald case ATT_BEARER_UNENHANCED_LE: 563d415948cSMatthias Ringwald status = l2cap_send_prepared_connectionless(att_connection->con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); 564*40f8d8f2SMatthias Ringwald break; 565d415948cSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 566d415948cSMatthias Ringwald case ATT_BEARER_UNENHANCED_CLASSIC: 567d415948cSMatthias Ringwald status = l2cap_send_prepared(att_server->l2cap_cid, size); 568d415948cSMatthias Ringwald break; 569d415948cSMatthias Ringwald #endif 5700f70c52eSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT 5710f70c52eSMatthias Ringwald case ATT_BEARER_ENHANCED_LE: 5720f70c52eSMatthias Ringwald btstack_assert(buffer != NULL); 5730f70c52eSMatthias Ringwald status = l2cap_send(att_server->l2cap_cid, buffer, size); 5740f70c52eSMatthias Ringwald break; 5750f70c52eSMatthias Ringwald #endif 576d415948cSMatthias Ringwald default: 577d415948cSMatthias Ringwald btstack_unreachable(); 578d415948cSMatthias Ringwald break; 579d415948cSMatthias Ringwald } 580d415948cSMatthias Ringwald return status; 581d415948cSMatthias Ringwald } 582d415948cSMatthias Ringwald 5837a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 5843deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){ 58526567ee7SMatthias Ringwald att_server_t * att_server = NULL; 58626567ee7SMatthias Ringwald att_connection_t * att_connection = NULL; 58726567ee7SMatthias Ringwald bool found = att_server_connections_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION, &att_server, &att_connection); 5883deb3ec6SMatthias Ringwald 589c2cbe117SMatthias Ringwald if (found == false){ 590c2cbe117SMatthias Ringwald return; 591c2cbe117SMatthias Ringwald } 592c2cbe117SMatthias Ringwald 5933deb3ec6SMatthias Ringwald uint8_t hash_flipped[8]; 5949c80e4ccSMatthias Ringwald reverse_64(hash, hash_flipped); 59526567ee7SMatthias Ringwald if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8) != 0){ 5963deb3ec6SMatthias Ringwald log_info("ATT Signed Write, invalid signature"); 59703f53d81SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT 59803f53d81SMatthias Ringwald printf("ATT Signed Write, invalid signature\n"); 59903f53d81SMatthias Ringwald #endif 60018707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 6013deb3ec6SMatthias Ringwald return; 6023deb3ec6SMatthias Ringwald } 6033deb3ec6SMatthias Ringwald log_info("ATT Signed Write, valid signature"); 60403f53d81SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT 60503f53d81SMatthias Ringwald printf("ATT Signed Write, valid signature\n"); 60603f53d81SMatthias Ringwald #endif 6073deb3ec6SMatthias Ringwald 6083deb3ec6SMatthias Ringwald // update sequence number 60918707219SMatthias Ringwald uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12); 61018707219SMatthias Ringwald le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1); 61118707219SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 6123f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 6133deb3ec6SMatthias Ringwald } 6147a766ebfSMatthias Ringwald #endif 61588a48dd8SMatthias Ringwald 61618707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED 617b06f2579SMatthias Ringwald // pre: can send now 6189988bc8aSMatthias Ringwald // uses l2cap outgoing buffer if no eatt_buffer provided 619b06f2579SMatthias Ringwald // returns: 1 if packet was sent 6209988bc8aSMatthias Ringwald static int 6219988bc8aSMatthias Ringwald att_server_process_validated_request(att_server_t *att_server, att_connection_t *att_connection, uint8_t *eatt_buffer) { 622b06f2579SMatthias Ringwald 6239988bc8aSMatthias Ringwald uint8_t * att_response_buffer; 6249988bc8aSMatthias Ringwald if (eatt_buffer != NULL){ 6259988bc8aSMatthias Ringwald att_response_buffer = eatt_buffer; 6269988bc8aSMatthias Ringwald } else { 627b06f2579SMatthias Ringwald l2cap_reserve_packet_buffer(); 6289988bc8aSMatthias Ringwald att_response_buffer = l2cap_get_outgoing_buffer(); 6299988bc8aSMatthias Ringwald } 6309988bc8aSMatthias Ringwald 6317a293e22SMatthias Ringwald uint16_t att_response_size = 0; 6327a293e22SMatthias Ringwald // Send Error Response for MTU Request over connection-oriented channel 6337a293e22SMatthias Ringwald if ((att_server->bearer_type != ATT_BEARER_UNENHANCED_LE) && (att_server->request_buffer[0] == ATT_EXCHANGE_MTU_REQUEST)){ 6347a293e22SMatthias Ringwald att_response_size = 5; 6357a293e22SMatthias Ringwald att_response_buffer[0] = ATT_ERROR_RESPONSE; 6367a293e22SMatthias Ringwald att_response_buffer[1] = ATT_EXCHANGE_MTU_REQUEST; 6377a293e22SMatthias Ringwald att_response_buffer[2] = 0; 6387a293e22SMatthias Ringwald att_response_buffer[3] = 0; 6397a293e22SMatthias Ringwald att_response_buffer[4] = ATT_ERROR_REQUEST_NOT_SUPPORTED; 6407a293e22SMatthias Ringwald } else { 6417a293e22SMatthias Ringwald att_response_size = att_handle_request(att_connection, att_server->request_buffer, att_server->request_size, att_response_buffer); 6427a293e22SMatthias Ringwald } 643b06f2579SMatthias Ringwald 644beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE 645c1ab6cc1SMatthias Ringwald if ((att_response_size == ATT_READ_RESPONSE_PENDING) || (att_response_size == ATT_INTERNAL_WRITE_RESPONSE_PENDING)){ 646e404a688SMatthias Ringwald // update state 647beb20288SMatthias Ringwald att_server->state = ATT_SERVER_RESPONSE_PENDING; 648e404a688SMatthias Ringwald 64956c3a347SMatthias Ringwald // callback with handle ATT_READ_RESPONSE_PENDING for reads 65056c3a347SMatthias Ringwald if (att_response_size == ATT_READ_RESPONSE_PENDING){ 651b6df12b6SMatthias Ringwald att_server_client_read_callback(att_connection->con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0); 65256c3a347SMatthias Ringwald } 653e8e7403eSMatthias Ringwald 654e8e7403eSMatthias Ringwald // free reserved buffer 6559988bc8aSMatthias Ringwald if (eatt_buffer == NULL){ 656e8e7403eSMatthias Ringwald l2cap_release_packet_buffer(); 6579988bc8aSMatthias Ringwald } 658e8e7403eSMatthias Ringwald return 0; 659e404a688SMatthias Ringwald } 660e404a688SMatthias Ringwald #endif 661e404a688SMatthias Ringwald 662b06f2579SMatthias Ringwald // intercept "insufficient authorization" for authenticated connections to allow for user authorization 6634ea43905SMatthias Ringwald if ((att_response_size >= 4u) 664b06f2579SMatthias Ringwald && (att_response_buffer[0] == ATT_ERROR_RESPONSE) 665b06f2579SMatthias Ringwald && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION) 66638b893aaSMilanka Ringwald && (att_connection->authenticated != 0u)){ 667b06f2579SMatthias Ringwald 668b6df12b6SMatthias Ringwald switch (gap_authorization_state(att_connection->con_handle)){ 669b06f2579SMatthias Ringwald case AUTHORIZATION_UNKNOWN: 6709988bc8aSMatthias Ringwald if (eatt_buffer == NULL){ 671b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 6729988bc8aSMatthias Ringwald } 673b6df12b6SMatthias Ringwald sm_request_pairing(att_connection->con_handle); 674b06f2579SMatthias Ringwald return 0; 675b06f2579SMatthias Ringwald case AUTHORIZATION_PENDING: 6769988bc8aSMatthias Ringwald if (eatt_buffer == NULL){ 677b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 6789988bc8aSMatthias Ringwald } 679b06f2579SMatthias Ringwald return 0; 680b06f2579SMatthias Ringwald default: 681b06f2579SMatthias Ringwald break; 682b06f2579SMatthias Ringwald } 683b06f2579SMatthias Ringwald } 684b06f2579SMatthias Ringwald 68518707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 6864ea43905SMatthias Ringwald if (att_response_size == 0u) { 6879988bc8aSMatthias Ringwald if (eatt_buffer == NULL){ 688b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 6899988bc8aSMatthias Ringwald } 690b06f2579SMatthias Ringwald return 0; 691b06f2579SMatthias Ringwald } 692b06f2579SMatthias Ringwald 6939988bc8aSMatthias Ringwald (void) att_server_send_prepared(att_server, att_connection, eatt_buffer, att_response_size); 694b06f2579SMatthias Ringwald 695b06f2579SMatthias Ringwald // notify client about MTU exchange result 696b06f2579SMatthias Ringwald if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){ 697b6df12b6SMatthias Ringwald att_emit_mtu_event(att_connection->con_handle, att_connection->mtu); 698b06f2579SMatthias Ringwald } 699b06f2579SMatthias Ringwald return 1; 700b06f2579SMatthias Ringwald } 701b06f2579SMatthias Ringwald 702beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE 7039816429dSMatthias Ringwald uint8_t att_server_response_ready(hci_con_handle_t con_handle){ 70468591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 70568591e36SMatthias Ringwald if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 70668591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 7073f9b77dbSMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 708beb20288SMatthias Ringwald if (att_server->state != ATT_SERVER_RESPONSE_PENDING) return ERROR_CODE_COMMAND_DISALLOWED; 709e404a688SMatthias Ringwald 710e404a688SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 7113f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 712e404a688SMatthias Ringwald return ERROR_CODE_SUCCESS; 713e404a688SMatthias Ringwald } 714e404a688SMatthias Ringwald #endif 715e404a688SMatthias Ringwald 716cd45e970SMatthias Ringwald static void att_run_for_context(att_server_t * att_server, att_connection_t * att_connection){ 71718707219SMatthias Ringwald switch (att_server->state){ 7183deb3ec6SMatthias Ringwald case ATT_SERVER_REQUEST_RECEIVED: 71923079b39SMatthias Ringwald switch (att_server->bearer_type){ 72023079b39SMatthias Ringwald case ATT_BEARER_UNENHANCED_LE: 7210234fbbcSMatthias Ringwald // wait until re-encryption as central is complete 72223079b39SMatthias Ringwald if (gap_reconnect_security_setup_active(att_connection->con_handle)) { 72323079b39SMatthias Ringwald return; 72423079b39SMatthias Ringwald }; 72523079b39SMatthias Ringwald break; 7262954c397SMatthias Ringwald #if defined(ENABLE_GATT_OVER_CLASSIC) || defined (ENABLE_GATT_OVER_EATT) 72723079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 72823079b39SMatthias Ringwald case ATT_BEARER_UNENHANCED_CLASSIC: 7292954c397SMatthias Ringwald /* fall through */ 7302954c397SMatthias Ringwald #endif 7312954c397SMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT 7322954c397SMatthias Ringwald case ATT_BEARER_ENHANCED_LE: 7332954c397SMatthias Ringwald #endif 73423079b39SMatthias Ringwald // ok 73523079b39SMatthias Ringwald break; 73623079b39SMatthias Ringwald #endif 73723079b39SMatthias Ringwald default: 73823079b39SMatthias Ringwald btstack_unreachable(); 73923079b39SMatthias Ringwald break; 74048364364SMatthias Ringwald } 7410234fbbcSMatthias Ringwald 742760ad805SMatthias Ringwald // wait until pairing is complete 743760ad805SMatthias Ringwald if (att_server->pairing_active) break; 744760ad805SMatthias Ringwald 7457a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 74618707219SMatthias Ringwald if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){ 7473deb3ec6SMatthias Ringwald log_info("ATT Signed Write!"); 7483deb3ec6SMatthias Ringwald if (!sm_cmac_ready()) { 7493deb3ec6SMatthias Ringwald log_info("ATT Signed Write, sm_cmac engine not ready. Abort"); 75018707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 7513deb3ec6SMatthias Ringwald return; 7523deb3ec6SMatthias Ringwald } 75318707219SMatthias Ringwald if (att_server->request_size < (3 + 12)) { 7543deb3ec6SMatthias Ringwald log_info("ATT Signed Write, request to short. Abort."); 75518707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 7563deb3ec6SMatthias Ringwald return; 7573deb3ec6SMatthias Ringwald } 75818707219SMatthias Ringwald if (att_server->ir_lookup_active){ 7593deb3ec6SMatthias Ringwald return; 7603deb3ec6SMatthias Ringwald } 76118707219SMatthias Ringwald if (att_server->ir_le_device_db_index < 0){ 7623deb3ec6SMatthias Ringwald log_info("ATT Signed Write, CSRK not available"); 76318707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 7643deb3ec6SMatthias Ringwald return; 7653deb3ec6SMatthias Ringwald } 7663deb3ec6SMatthias Ringwald 7673deb3ec6SMatthias Ringwald // check counter 76818707219SMatthias Ringwald uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12); 76918707219SMatthias Ringwald uint32_t counter_db = le_device_db_remote_counter_get(att_server->ir_le_device_db_index); 7703deb3ec6SMatthias Ringwald log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet); 7713deb3ec6SMatthias Ringwald if (counter_packet < counter_db){ 7723deb3ec6SMatthias Ringwald log_info("ATT Signed Write, db reports higher counter, abort"); 77318707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 7743deb3ec6SMatthias Ringwald return; 7753deb3ec6SMatthias Ringwald } 7763deb3ec6SMatthias Ringwald 7773deb3ec6SMatthias Ringwald // signature is { sequence counter, secure hash } 7783deb3ec6SMatthias Ringwald sm_key_t csrk; 77918707219SMatthias Ringwald le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk); 78018707219SMatthias Ringwald att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION; 7813deb3ec6SMatthias Ringwald log_info("Orig Signature: "); 78218707219SMatthias Ringwald log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8); 78318707219SMatthias Ringwald uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1); 78418707219SMatthias 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); 7853deb3ec6SMatthias Ringwald return; 7863deb3ec6SMatthias Ringwald } 7877a766ebfSMatthias Ringwald #endif 788b06f2579SMatthias Ringwald // move on 78918707219SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 7903f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 791b06f2579SMatthias Ringwald break; 79288a48dd8SMatthias Ringwald 7933deb3ec6SMatthias Ringwald default: 7943deb3ec6SMatthias Ringwald break; 7953deb3ec6SMatthias Ringwald } 7963deb3ec6SMatthias Ringwald } 7973deb3ec6SMatthias Ringwald 7981979f09cSMatthias Ringwald static bool att_server_data_ready_for_phase(att_server_t * att_server, att_server_run_phase_t phase){ 79990f03c80SMatthias Ringwald switch (phase){ 80090f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_1_REQUESTS: 80190f03c80SMatthias Ringwald return att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 80290f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_2_INDICATIONS: 80338b893aaSMilanka Ringwald return (!btstack_linked_list_empty(&att_server->indication_requests) && (att_server->value_indication_handle == 0u)); 80490f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS: 80590f03c80SMatthias Ringwald return (!btstack_linked_list_empty(&att_server->notification_requests)); 8067bbeb3adSMilanka Ringwald default: 8077bbeb3adSMilanka Ringwald btstack_assert(false); 8081979f09cSMatthias Ringwald return false; 80990f03c80SMatthias Ringwald } 8107bbeb3adSMilanka Ringwald } 81190f03c80SMatthias Ringwald 8129d6c0193SMatthias 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){ 81390f03c80SMatthias Ringwald btstack_context_callback_registration_t * client; 81490f03c80SMatthias Ringwald switch (phase){ 81590f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_1_REQUESTS: 8160f70c52eSMatthias Ringwald att_server_process_validated_request(att_server, att_connection, NULL); 81790f03c80SMatthias Ringwald break; 81890f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_2_INDICATIONS: 81990f03c80SMatthias Ringwald client = (btstack_context_callback_registration_t*) att_server->indication_requests; 82090f03c80SMatthias Ringwald btstack_linked_list_remove(&att_server->indication_requests, (btstack_linked_item_t *) client); 82190f03c80SMatthias Ringwald client->callback(client->context); 82290f03c80SMatthias Ringwald break; 82390f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS: 82490f03c80SMatthias Ringwald client = (btstack_context_callback_registration_t*) att_server->notification_requests; 82590f03c80SMatthias Ringwald btstack_linked_list_remove(&att_server->notification_requests, (btstack_linked_item_t *) client); 82690f03c80SMatthias Ringwald client->callback(client->context); 82790f03c80SMatthias Ringwald break; 8287bbeb3adSMilanka Ringwald default: 8297bbeb3adSMilanka Ringwald btstack_assert(false); 8307bbeb3adSMilanka Ringwald break; 83190f03c80SMatthias Ringwald } 83290f03c80SMatthias Ringwald } 83390f03c80SMatthias Ringwald 8347eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){ 835b06f2579SMatthias Ringwald 8366438547aSMatthias Ringwald hci_con_handle_t last_send_con_handle = HCI_CON_HANDLE_INVALID; 83768591e36SMatthias Ringwald hci_connection_t * request_hci_connection = NULL; 8381979f09cSMatthias Ringwald bool can_send_now = true; 83938b893aaSMilanka Ringwald uint8_t phase_index; 8406438547aSMatthias Ringwald 8414395a000SMatthias Ringwald for (phase_index = ATT_SERVER_RUN_PHASE_1_REQUESTS; phase_index <= ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS; phase_index++){ 8424395a000SMatthias Ringwald att_server_run_phase_t phase = (att_server_run_phase_t) phase_index; 8436438547aSMatthias Ringwald hci_con_handle_t skip_connections_until = att_server_last_can_send_now; 844ff3cc4a5SMatthias Ringwald while (true){ 8456438547aSMatthias Ringwald btstack_linked_list_iterator_t it; 84618707219SMatthias Ringwald hci_connections_get_iterator(&it); 84718707219SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 84818707219SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 84918707219SMatthias Ringwald att_server_t * att_server = &connection->att_server; 850388fa7c4SMatthias Ringwald att_connection_t * att_connection = &connection->att_connection; 8516438547aSMatthias Ringwald 8521979f09cSMatthias Ringwald bool data_ready = att_server_data_ready_for_phase(att_server, phase); 8536438547aSMatthias Ringwald 854b6df12b6SMatthias 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); 8556438547aSMatthias Ringwald 8566438547aSMatthias Ringwald // skip until last sender found (which is also skipped) 8576438547aSMatthias Ringwald if (skip_connections_until != HCI_CON_HANDLE_INVALID){ 85868591e36SMatthias Ringwald if (data_ready && (request_hci_connection == NULL)){ 85968591e36SMatthias Ringwald request_hci_connection = connection; 8606438547aSMatthias Ringwald } 861b6df12b6SMatthias Ringwald if (skip_connections_until == att_connection->con_handle){ 8626438547aSMatthias Ringwald skip_connections_until = HCI_CON_HANDLE_INVALID; 8636438547aSMatthias Ringwald } 8646438547aSMatthias Ringwald continue; 8656438547aSMatthias Ringwald }; 8666438547aSMatthias Ringwald 86790f03c80SMatthias Ringwald if (data_ready){ 868969a5bbaSMatthias Ringwald if (can_send_now){ 8699d6c0193SMatthias Ringwald att_server_trigger_send_for_phase(att_server, att_connection, phase); 870b6df12b6SMatthias Ringwald last_send_con_handle = att_connection->con_handle; 87137a8059dSMatthias Ringwald can_send_now = att_server_can_send_packet(att_server, att_connection); 87290f03c80SMatthias Ringwald data_ready = att_server_data_ready_for_phase(att_server, phase); 87368591e36SMatthias Ringwald if (data_ready && (request_hci_connection == NULL)){ 87468591e36SMatthias Ringwald request_hci_connection = connection; 875cbbb12d9SMatthias Ringwald } 876cbbb12d9SMatthias Ringwald } else { 87768591e36SMatthias Ringwald request_hci_connection = connection; 878f29a88d8SMatthias Ringwald break; 879cbbb12d9SMatthias Ringwald } 880cbbb12d9SMatthias Ringwald } 8813deb3ec6SMatthias Ringwald } 8823deb3ec6SMatthias Ringwald 8836438547aSMatthias Ringwald // stop skipping (handles disconnect by last send connection) 8846438547aSMatthias Ringwald skip_connections_until = HCI_CON_HANDLE_INVALID; 8856438547aSMatthias Ringwald 8863551936eSMatthias Ringwald // Exit loop, if we cannot send 887969a5bbaSMatthias Ringwald if (!can_send_now) break; 888969a5bbaSMatthias Ringwald 889969a5bbaSMatthias Ringwald // Exit loop, if we can send but there are also no further request 89068591e36SMatthias Ringwald if (request_hci_connection == NULL) break; 891969a5bbaSMatthias Ringwald 892969a5bbaSMatthias Ringwald // Finally, if we still can send and there are requests, just try again 89368591e36SMatthias Ringwald request_hci_connection = NULL; 894969a5bbaSMatthias Ringwald } 8956438547aSMatthias Ringwald // update last send con handle for round robin 8966438547aSMatthias Ringwald if (last_send_con_handle != HCI_CON_HANDLE_INVALID){ 8976438547aSMatthias Ringwald att_server_last_can_send_now = last_send_con_handle; 8986438547aSMatthias Ringwald } 8993551936eSMatthias Ringwald } 900969a5bbaSMatthias Ringwald 90168591e36SMatthias Ringwald if (request_hci_connection == NULL) return; 9023f9b77dbSMatthias Ringwald 9033f9b77dbSMatthias Ringwald att_server_t * att_server = &request_hci_connection->att_server; 9043f9b77dbSMatthias Ringwald att_connection_t * att_connection = &request_hci_connection->att_connection; 9053f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 906969a5bbaSMatthias Ringwald } 907969a5bbaSMatthias Ringwald 90826b42dcfSMatthias Ringwald static void att_server_handle_att_pdu(att_server_t * att_server, att_connection_t * att_connection, uint8_t * packet, uint16_t size){ 90918707219SMatthias Ringwald 910838bd521SMatthias Ringwald uint8_t opcode = packet[0u]; 91138b893aaSMilanka Ringwald uint8_t method = opcode & 0x03fu; 912ed68ae9dSMilanka Ringwald bool invalid = method > ATT_MULTIPLE_HANDLE_VALUE_NTF; 91338b893aaSMilanka Ringwald bool command = (opcode & 0x40u) != 0u; 914838bd521SMatthias Ringwald 915838bd521SMatthias Ringwald // ignore invalid commands 916838bd521SMatthias Ringwald if (invalid && command){ 917838bd521SMatthias Ringwald return; 918838bd521SMatthias Ringwald } 919838bd521SMatthias Ringwald 9203deb3ec6SMatthias Ringwald // handle value indication confirms 92138b893aaSMilanka Ringwald if ((opcode == ATT_HANDLE_VALUE_CONFIRMATION) && (att_server->value_indication_handle != 0u)){ 92218707219SMatthias Ringwald btstack_run_loop_remove_timer(&att_server->value_indication_timer); 92318707219SMatthias Ringwald uint16_t att_handle = att_server->value_indication_handle; 92438b893aaSMilanka Ringwald att_server->value_indication_handle = 0u; 92538b893aaSMilanka Ringwald att_handle_value_indication_notify_client(0u, att_connection->con_handle, att_handle); 9263f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 9273deb3ec6SMatthias Ringwald return; 9283deb3ec6SMatthias Ringwald } 9293deb3ec6SMatthias Ringwald 9306428eb5aSMatthias Ringwald // directly process command 9316428eb5aSMatthias Ringwald // note: signed write cannot be handled directly as authentication needs to be verified 932838bd521SMatthias Ringwald if (opcode == ATT_WRITE_COMMAND){ 933b6df12b6SMatthias Ringwald att_handle_request(att_connection, packet, size, NULL); 9346428eb5aSMatthias Ringwald return; 9356428eb5aSMatthias Ringwald } 9366428eb5aSMatthias Ringwald 9373deb3ec6SMatthias Ringwald // check size 93818707219SMatthias Ringwald if (size > sizeof(att_server->request_buffer)) { 939e0463bdcSMatthias 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)); 9406428eb5aSMatthias Ringwald return; 9416428eb5aSMatthias Ringwald } 9423deb3ec6SMatthias Ringwald 943e0463bdcSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 944e0463bdcSMatthias Ringwald // abort signed write validation if a new request comes in (but finish previous signed write if possible) 945e0463bdcSMatthias Ringwald if (att_server->state == ATT_SERVER_W4_SIGNED_WRITE_VALIDATION){ 946e0463bdcSMatthias Ringwald if (packet[0] == ATT_SIGNED_WRITE_COMMAND){ 947e0463bdcSMatthias Ringwald log_info("skip new signed write request as previous is in validation"); 948e0463bdcSMatthias Ringwald return; 949e0463bdcSMatthias Ringwald } else { 950e0463bdcSMatthias Ringwald log_info("abort signed write validation to process new request"); 951e0463bdcSMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 952e0463bdcSMatthias Ringwald } 953e0463bdcSMatthias Ringwald } 954e0463bdcSMatthias Ringwald #endif 9553deb3ec6SMatthias Ringwald // last request still in processing? 95618707219SMatthias Ringwald if (att_server->state != ATT_SERVER_IDLE){ 957e0463bdcSMatthias Ringwald log_info("skip att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state); 9586428eb5aSMatthias Ringwald return; 9596428eb5aSMatthias Ringwald } 9603deb3ec6SMatthias Ringwald 9613deb3ec6SMatthias Ringwald // store request 96218707219SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED; 96318707219SMatthias Ringwald att_server->request_size = size; 9646535961aSMatthias Ringwald (void)memcpy(att_server->request_buffer, packet, size); 9653deb3ec6SMatthias Ringwald 966cd45e970SMatthias Ringwald att_run_for_context(att_server, att_connection); 96741772f37SMatthias Ringwald } 96841772f37SMatthias Ringwald 96941772f37SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){ 97068591e36SMatthias Ringwald hci_connection_t * hci_connection; 971b6df12b6SMatthias Ringwald att_connection_t * att_connection; 97226b42dcfSMatthias Ringwald att_server_t * att_server; 97341772f37SMatthias Ringwald 97441772f37SMatthias Ringwald switch (packet_type){ 97541772f37SMatthias Ringwald case HCI_EVENT_PACKET: 97641772f37SMatthias Ringwald switch (packet[0]){ 97741772f37SMatthias Ringwald case L2CAP_EVENT_CAN_SEND_NOW: 97841772f37SMatthias Ringwald att_server_handle_can_send_now(); 97941772f37SMatthias Ringwald break; 98041772f37SMatthias Ringwald case ATT_EVENT_MTU_EXCHANGE_COMPLETE: 98141772f37SMatthias Ringwald // GATT client has negotiated the mtu for this connection 98268591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(handle); 98368591e36SMatthias Ringwald if (!hci_connection) break; 984388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 985b6df12b6SMatthias Ringwald att_connection->mtu = little_endian_read_16(packet, 4); 98641772f37SMatthias Ringwald break; 98741772f37SMatthias Ringwald default: 98841772f37SMatthias Ringwald break; 98941772f37SMatthias Ringwald } 99041772f37SMatthias Ringwald break; 99141772f37SMatthias Ringwald 99241772f37SMatthias Ringwald case ATT_DATA_PACKET: 99341772f37SMatthias Ringwald log_debug("ATT Packet, handle 0x%04x", handle); 99468591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(handle); 99568591e36SMatthias Ringwald if (!hci_connection) break; 99641772f37SMatthias Ringwald 99726b42dcfSMatthias Ringwald att_server = &hci_connection->att_server; 99826b42dcfSMatthias Ringwald att_connection = &hci_connection->att_connection; 99926b42dcfSMatthias Ringwald att_server_handle_att_pdu(att_server, att_connection, packet, size); 1000372d62c4SMatthias Ringwald break; 10017bbeb3adSMilanka Ringwald 10027bbeb3adSMilanka Ringwald default: 10037bbeb3adSMilanka Ringwald break; 1004372d62c4SMatthias Ringwald } 10053deb3ec6SMatthias Ringwald } 10063deb3ec6SMatthias Ringwald 1007bf78aac6SMatthias Ringwald // --------------------- 1008bf78aac6SMatthias Ringwald // persistent CCC writes 1009bf78aac6SMatthias Ringwald static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){ 10104ea43905SMatthias Ringwald return ('B' << 24u) | ('T' << 16u) | ('C' << 8u) | index; 1011bf78aac6SMatthias Ringwald } 1012bf78aac6SMatthias Ringwald 1013bf78aac6SMatthias Ringwald static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){ 1014bf78aac6SMatthias Ringwald // lookup att_server instance 101568591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 101668591e36SMatthias Ringwald if (!hci_connection) return; 101768591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 1018bf78aac6SMatthias Ringwald int le_device_index = att_server->ir_le_device_db_index; 1019bf78aac6SMatthias 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); 10206a540790SMatthias Ringwald 1021bf78aac6SMatthias Ringwald // check if bonded 1022bf78aac6SMatthias Ringwald if (le_device_index < 0) return; 10236a540790SMatthias Ringwald 1024bf78aac6SMatthias Ringwald // get btstack_tlv 1025bf78aac6SMatthias Ringwald const btstack_tlv_t * tlv_impl = NULL; 1026bf78aac6SMatthias Ringwald void * tlv_context; 1027bf78aac6SMatthias Ringwald btstack_tlv_get_instance(&tlv_impl, &tlv_context); 1028bf78aac6SMatthias Ringwald if (!tlv_impl) return; 10296a540790SMatthias Ringwald 1030bf78aac6SMatthias Ringwald // update ccc tag 1031bf78aac6SMatthias Ringwald int index; 10326a540790SMatthias Ringwald uint32_t highest_seq_nr = 0; 10336a540790SMatthias Ringwald uint32_t lowest_seq_nr = 0; 10346a540790SMatthias Ringwald uint32_t tag_for_lowest_seq_nr = 0; 10356a540790SMatthias Ringwald uint32_t tag_for_empty = 0; 10366a540790SMatthias Ringwald persistent_ccc_entry_t entry; 10376afb9acaSMatthias Ringwald for (index=0; index<NVN_NUM_GATT_SERVER_CCC; index++){ 1038bf78aac6SMatthias Ringwald uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 10396a540790SMatthias Ringwald int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 10406a540790SMatthias Ringwald 10416a540790SMatthias Ringwald // empty/invalid tag 10426a540790SMatthias Ringwald if (len != sizeof(persistent_ccc_entry_t)){ 10436a540790SMatthias Ringwald tag_for_empty = tag; 1044bf78aac6SMatthias Ringwald continue; 1045bf78aac6SMatthias Ringwald } 10466a540790SMatthias Ringwald // update highest seq nr 10476a540790SMatthias Ringwald if (entry.seq_nr > highest_seq_nr){ 10486a540790SMatthias Ringwald highest_seq_nr = entry.seq_nr; 10496a540790SMatthias Ringwald } 10506a540790SMatthias Ringwald // find entry with lowest seq nr 10514ea43905SMatthias Ringwald if ((tag_for_lowest_seq_nr == 0u) || (entry.seq_nr < lowest_seq_nr)){ 10526a540790SMatthias Ringwald tag_for_lowest_seq_nr = tag; 10536a540790SMatthias Ringwald lowest_seq_nr = entry.seq_nr; 10546a540790SMatthias Ringwald } 10556a540790SMatthias Ringwald 10566a540790SMatthias Ringwald if (entry.device_index != le_device_index) continue; 10576a540790SMatthias Ringwald if (entry.att_handle != att_handle) continue; 10586a540790SMatthias Ringwald 10596a540790SMatthias Ringwald // found matching entry 10609305033eSMatthias Ringwald if (value != 0){ 1061bf78aac6SMatthias Ringwald // update 10626a540790SMatthias Ringwald if (entry.value == value) { 1063760ad805SMatthias Ringwald log_info("CCC Index %u: Up-to-date", index); 1064760ad805SMatthias Ringwald return; 1065760ad805SMatthias Ringwald } 1066348fc806SMatthias Ringwald entry.value = (uint8_t) value; 10674ea43905SMatthias Ringwald entry.seq_nr = highest_seq_nr + 1u; 1068760ad805SMatthias Ringwald log_info("CCC Index %u: Store", index); 10690fe46bc1SMatthias Ringwald int result = tlv_impl->store_tag(tlv_context, tag, (const uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 10700fe46bc1SMatthias Ringwald if (result != 0){ 10710fe46bc1SMatthias Ringwald log_error("Store tag index %u failed", index); 10720fe46bc1SMatthias Ringwald } 107301ac2008SMatthias Ringwald } else { 107401ac2008SMatthias Ringwald // delete 1075760ad805SMatthias Ringwald log_info("CCC Index %u: Delete", index); 107601ac2008SMatthias Ringwald tlv_impl->delete_tag(tlv_context, tag); 107701ac2008SMatthias Ringwald } 1078bf78aac6SMatthias Ringwald return; 1079bf78aac6SMatthias Ringwald } 10806a540790SMatthias Ringwald 10810fe46bc1SMatthias Ringwald log_info("tag_for_empty %"PRIx32", tag_for_lowest_seq_nr %"PRIx32, tag_for_empty, tag_for_lowest_seq_nr); 10826a540790SMatthias Ringwald 10834ea43905SMatthias Ringwald if (value == 0u){ 10846a540790SMatthias Ringwald // done 10856a540790SMatthias Ringwald return; 10866a540790SMatthias Ringwald } 10876a540790SMatthias Ringwald 108838b893aaSMilanka Ringwald uint32_t tag_to_use = 0u; 108938b893aaSMilanka Ringwald if (tag_for_empty != 0u){ 10906a540790SMatthias Ringwald tag_to_use = tag_for_empty; 10916a540790SMatthias Ringwald } else if (tag_for_lowest_seq_nr){ 10926a540790SMatthias Ringwald tag_to_use = tag_for_lowest_seq_nr; 10936a540790SMatthias Ringwald } else { 10946a540790SMatthias Ringwald // should not happen 10956a540790SMatthias Ringwald return; 10966a540790SMatthias Ringwald } 1097bf78aac6SMatthias Ringwald // store ccc tag 10984ea43905SMatthias Ringwald entry.seq_nr = highest_seq_nr + 1u; 10996a540790SMatthias Ringwald entry.device_index = le_device_index; 11006a540790SMatthias Ringwald entry.att_handle = att_handle; 1101348fc806SMatthias Ringwald entry.value = (uint8_t) value; 11020fe46bc1SMatthias Ringwald int result = tlv_impl->store_tag(tlv_context, tag_to_use, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 11030fe46bc1SMatthias Ringwald if (result != 0){ 11040fe46bc1SMatthias Ringwald log_error("Store tag index %u failed", index); 11050fe46bc1SMatthias Ringwald } 1106bf78aac6SMatthias Ringwald } 1107bf78aac6SMatthias Ringwald 1108fe3d0cbaSMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server){ 1109760ad805SMatthias Ringwald int le_device_index = att_server->ir_le_device_db_index; 1110760ad805SMatthias Ringwald log_info("Clear CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index); 1111760ad805SMatthias Ringwald // check if bonded 1112760ad805SMatthias Ringwald if (le_device_index < 0) return; 1113760ad805SMatthias Ringwald // get btstack_tlv 1114760ad805SMatthias Ringwald const btstack_tlv_t * tlv_impl = NULL; 1115760ad805SMatthias Ringwald void * tlv_context; 1116760ad805SMatthias Ringwald btstack_tlv_get_instance(&tlv_impl, &tlv_context); 1117760ad805SMatthias Ringwald if (!tlv_impl) return; 1118760ad805SMatthias Ringwald // get all ccc tag 1119760ad805SMatthias Ringwald int index; 11206a540790SMatthias Ringwald persistent_ccc_entry_t entry; 11216afb9acaSMatthias Ringwald for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){ 1122760ad805SMatthias Ringwald uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 11236a540790SMatthias Ringwald int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 11246a540790SMatthias Ringwald if (len != sizeof(persistent_ccc_entry_t)) continue; 11256a540790SMatthias Ringwald if (entry.device_index != le_device_index) continue; 1126760ad805SMatthias Ringwald // delete entry 1127760ad805SMatthias Ringwald log_info("CCC Index %u: Delete", index); 1128760ad805SMatthias Ringwald tlv_impl->delete_tag(tlv_context, tag); 1129760ad805SMatthias Ringwald } 1130760ad805SMatthias Ringwald } 1131760ad805SMatthias Ringwald 11329a1f55eaSMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server, att_connection_t * att_connection){ 113301ac2008SMatthias Ringwald int le_device_index = att_server->ir_le_device_db_index; 113401ac2008SMatthias Ringwald log_info("Restore CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index); 11351b7acd0dSMatthias Ringwald // check if bonded 11361b7acd0dSMatthias Ringwald if (le_device_index < 0) return; 113701ac2008SMatthias Ringwald // get btstack_tlv 113801ac2008SMatthias Ringwald const btstack_tlv_t * tlv_impl = NULL; 113901ac2008SMatthias Ringwald void * tlv_context; 114001ac2008SMatthias Ringwald btstack_tlv_get_instance(&tlv_impl, &tlv_context); 114101ac2008SMatthias Ringwald if (!tlv_impl) return; 114201ac2008SMatthias Ringwald // get all ccc tag 114301ac2008SMatthias Ringwald int index; 11446a540790SMatthias Ringwald persistent_ccc_entry_t entry; 11456afb9acaSMatthias Ringwald for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){ 114601ac2008SMatthias Ringwald uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 11476a540790SMatthias Ringwald int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 11486a540790SMatthias Ringwald if (len != sizeof(persistent_ccc_entry_t)) continue; 11496a540790SMatthias Ringwald if (entry.device_index != le_device_index) continue; 115001ac2008SMatthias Ringwald // simulate write callback 11516a540790SMatthias Ringwald uint16_t attribute_handle = entry.att_handle; 115201ac2008SMatthias Ringwald uint8_t value[2]; 11536a540790SMatthias Ringwald little_endian_store_16(value, 0, entry.value); 115401ac2008SMatthias Ringwald att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle); 115501ac2008SMatthias Ringwald if (!callback) continue; 11566a540790SMatthias Ringwald log_info("CCC Index %u: Set Attribute handle 0x%04x to value 0x%04x", index, attribute_handle, entry.value ); 1157b6df12b6SMatthias Ringwald (*callback)(att_connection->con_handle, attribute_handle, ATT_TRANSACTION_MODE_NONE, 0, value, sizeof(value)); 115801ac2008SMatthias Ringwald } 115901ac2008SMatthias Ringwald } 116001ac2008SMatthias Ringwald 1161bf78aac6SMatthias Ringwald // persistent CCC writes 1162bf78aac6SMatthias Ringwald // --------------------- 11633d71c7a4SMatthias Ringwald 11643d71c7a4SMatthias Ringwald // gatt service management 11653d71c7a4SMatthias Ringwald static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){ 11663d71c7a4SMatthias Ringwald btstack_linked_list_iterator_t it; 11673d71c7a4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 11683d71c7a4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 11693d71c7a4SMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 11703d71c7a4SMatthias Ringwald if (handler->start_handle > handle) continue; 11713d71c7a4SMatthias Ringwald if (handler->end_handle < handle) continue; 11723d71c7a4SMatthias Ringwald return handler; 11733d71c7a4SMatthias Ringwald } 11743d71c7a4SMatthias Ringwald return NULL; 11753d71c7a4SMatthias Ringwald } 11763d71c7a4SMatthias Ringwald static att_read_callback_t att_server_read_callback_for_handle(uint16_t handle){ 11773d71c7a4SMatthias Ringwald att_service_handler_t * handler = att_service_handler_for_handle(handle); 11789305033eSMatthias Ringwald if (handler != NULL) return handler->read_callback; 11793d71c7a4SMatthias Ringwald return att_server_client_read_callback; 11803d71c7a4SMatthias Ringwald } 11813d71c7a4SMatthias Ringwald 11823d71c7a4SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){ 11833d71c7a4SMatthias Ringwald att_service_handler_t * handler = att_service_handler_for_handle(handle); 11849305033eSMatthias Ringwald if (handler != NULL) return handler->write_callback; 11853d71c7a4SMatthias Ringwald return att_server_client_write_callback; 11863d71c7a4SMatthias Ringwald } 11873d71c7a4SMatthias Ringwald 11885d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle){ 11895d07396bSMatthias Ringwald att_service_handler_t * handler = att_service_handler_for_handle(handle); 11909305033eSMatthias Ringwald if (handler != NULL) return handler->packet_handler; 11915d07396bSMatthias Ringwald return att_client_packet_handler; 11925d07396bSMatthias Ringwald } 11935d07396bSMatthias Ringwald 11943d71c7a4SMatthias Ringwald static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){ 11953d71c7a4SMatthias Ringwald // notify all callbacks 11963d71c7a4SMatthias Ringwald btstack_linked_list_iterator_t it; 11973d71c7a4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 11983d71c7a4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 11993d71c7a4SMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 12003d71c7a4SMatthias Ringwald if (!handler->write_callback) continue; 12013d71c7a4SMatthias Ringwald (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0); 12023d71c7a4SMatthias Ringwald } 12033d71c7a4SMatthias Ringwald if (!att_server_client_write_callback) return; 12043d71c7a4SMatthias Ringwald (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0); 12053d71c7a4SMatthias Ringwald } 12063d71c7a4SMatthias Ringwald 12073d71c7a4SMatthias Ringwald // returns first reported error or 0 12083d71c7a4SMatthias Ringwald static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){ 12093d71c7a4SMatthias Ringwald btstack_linked_list_iterator_t it; 12103d71c7a4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 12113d71c7a4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 12123d71c7a4SMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 12133d71c7a4SMatthias Ringwald if (!handler->write_callback) continue; 12143d71c7a4SMatthias Ringwald uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0); 121538b893aaSMilanka Ringwald if (error_code != 0u) return error_code; 12163d71c7a4SMatthias Ringwald } 12173d71c7a4SMatthias Ringwald if (!att_server_client_write_callback) return 0; 12183d71c7a4SMatthias Ringwald return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0); 12193d71c7a4SMatthias Ringwald } 12203d71c7a4SMatthias Ringwald 12213d71c7a4SMatthias 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){ 12223d71c7a4SMatthias Ringwald att_read_callback_t callback = att_server_read_callback_for_handle(attribute_handle); 122301ac2008SMatthias Ringwald if (!callback) return 0; 12243d71c7a4SMatthias Ringwald return (*callback)(con_handle, attribute_handle, offset, buffer, buffer_size); 12253d71c7a4SMatthias Ringwald } 12263d71c7a4SMatthias Ringwald 12273d71c7a4SMatthias 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){ 12283d71c7a4SMatthias Ringwald switch (transaction_mode){ 12293d71c7a4SMatthias Ringwald case ATT_TRANSACTION_MODE_VALIDATE: 12303d71c7a4SMatthias Ringwald return att_validate_prepared_write(con_handle); 12313d71c7a4SMatthias Ringwald case ATT_TRANSACTION_MODE_EXECUTE: 12323d71c7a4SMatthias Ringwald case ATT_TRANSACTION_MODE_CANCEL: 12333d71c7a4SMatthias Ringwald att_notify_write_callbacks(con_handle, transaction_mode); 12343d71c7a4SMatthias Ringwald return 0; 12353d71c7a4SMatthias Ringwald default: 12363d71c7a4SMatthias Ringwald break; 12373d71c7a4SMatthias Ringwald } 12383d71c7a4SMatthias Ringwald 1239bf78aac6SMatthias Ringwald // track CCC writes 12404ea43905SMatthias Ringwald if (att_is_persistent_ccc(attribute_handle) && (offset == 0u) && (buffer_size == 2u)){ 1241bf78aac6SMatthias Ringwald att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0)); 1242bf78aac6SMatthias Ringwald } 1243bf78aac6SMatthias Ringwald 124401ac2008SMatthias Ringwald att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle); 124501ac2008SMatthias Ringwald if (!callback) return 0; 12463d71c7a4SMatthias Ringwald return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size); 12473d71c7a4SMatthias Ringwald } 12483d71c7a4SMatthias Ringwald 12493d71c7a4SMatthias Ringwald /** 12503d71c7a4SMatthias Ringwald * @brief register read/write callbacks for specific handle range 12513d71c7a4SMatthias Ringwald * @param att_service_handler_t 12523d71c7a4SMatthias Ringwald */ 12533d71c7a4SMatthias Ringwald void att_server_register_service_handler(att_service_handler_t * handler){ 1254ec8f5414SMilanka Ringwald bool att_server_registered = false; 1255ec8f5414SMilanka Ringwald if (att_service_handler_for_handle(handler->start_handle)){ 1256ec8f5414SMilanka Ringwald att_server_registered = true; 1257ec8f5414SMilanka Ringwald } 1258ec8f5414SMilanka Ringwald 1259ec8f5414SMilanka Ringwald if (att_service_handler_for_handle(handler->end_handle)){ 1260ec8f5414SMilanka Ringwald att_server_registered = true; 1261ec8f5414SMilanka Ringwald } 1262ec8f5414SMilanka Ringwald 1263ec8f5414SMilanka Ringwald if (att_server_registered){ 12643d71c7a4SMatthias Ringwald log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle); 12653d71c7a4SMatthias Ringwald return; 12663d71c7a4SMatthias Ringwald } 12673d71c7a4SMatthias Ringwald btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler); 12683d71c7a4SMatthias Ringwald } 12693d71c7a4SMatthias Ringwald 12703deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){ 12713deb3ec6SMatthias Ringwald 12723d71c7a4SMatthias Ringwald // store callbacks 12733d71c7a4SMatthias Ringwald att_server_client_read_callback = read_callback; 12743d71c7a4SMatthias Ringwald att_server_client_write_callback = write_callback; 12753d71c7a4SMatthias Ringwald 12761a389cdcSMatthias Ringwald // register for HCI Events 1277d9a7306aSMatthias Ringwald hci_event_callback_registration.callback = &att_event_packet_handler; 12781a389cdcSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 12791a389cdcSMatthias Ringwald 1280406722e4SMatthias Ringwald // register for SM events 1281d9a7306aSMatthias Ringwald sm_event_callback_registration.callback = &att_event_packet_handler; 1282406722e4SMatthias Ringwald sm_add_event_handler(&sm_event_callback_registration); 12833deb3ec6SMatthias Ringwald 12841a389cdcSMatthias Ringwald // and L2CAP ATT Server PDUs 12853deb3ec6SMatthias Ringwald att_dispatch_register_server(att_packet_handler); 12863deb3ec6SMatthias Ringwald 128770dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 128870dca4d4SMatthias Ringwald // setup l2cap service 1289f2372b44SMatthias Ringwald l2cap_register_service(&att_event_packet_handler, PSM_ATT, 0xffff, gap_get_security_level()); 129070dca4d4SMatthias Ringwald #endif 129170dca4d4SMatthias Ringwald 12923deb3ec6SMatthias Ringwald att_set_db(db); 12933d71c7a4SMatthias Ringwald att_set_read_callback(att_server_read_callback); 12943d71c7a4SMatthias Ringwald att_set_write_callback(att_server_write_callback); 12953deb3ec6SMatthias Ringwald } 12963deb3ec6SMatthias Ringwald 12973deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){ 12983deb3ec6SMatthias Ringwald att_client_packet_handler = handler; 12993deb3ec6SMatthias Ringwald } 13003deb3ec6SMatthias Ringwald 1301cbbb12d9SMatthias Ringwald 1302cbbb12d9SMatthias Ringwald // to be deprecated 1303c141988cSMatthias Ringwald int att_server_can_send_packet_now(hci_con_handle_t con_handle){ 130468591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 130568591e36SMatthias Ringwald if (!hci_connection) return 0; 130637a8059dSMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 130737a8059dSMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 130837a8059dSMatthias Ringwald return att_server_can_send_packet(att_server, att_connection); 13091b96b007SMatthias Ringwald } 13101b96b007SMatthias Ringwald 13119816429dSMatthias Ringwald uint8_t att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 1312cbbb12d9SMatthias Ringwald return att_server_request_to_send_notification(callback_registration, con_handle); 13133deb3ec6SMatthias Ringwald } 13143deb3ec6SMatthias Ringwald 1315cbbb12d9SMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){ 1316cbbb12d9SMatthias Ringwald att_client_waiting_for_can_send_registration.callback = &att_emit_can_send_now_event; 1317cbbb12d9SMatthias Ringwald att_server_request_to_send_notification(&att_client_waiting_for_can_send_registration, con_handle); 1318cbbb12d9SMatthias Ringwald } 1319cbbb12d9SMatthias Ringwald // end of deprecated 1320cbbb12d9SMatthias Ringwald 13219816429dSMatthias Ringwald uint8_t att_server_request_to_send_notification(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 132268591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 132368591e36SMatthias Ringwald if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 132468591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 13253f9b77dbSMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 1326c37df6f6SMatthias Ringwald bool added = btstack_linked_list_add_tail(&att_server->notification_requests, (btstack_linked_item_t*) callback_registration); 13273f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 1328c37df6f6SMatthias Ringwald if (added){ 1329cbbb12d9SMatthias Ringwald return ERROR_CODE_SUCCESS; 1330c37df6f6SMatthias Ringwald } else { 1331c37df6f6SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1332c37df6f6SMatthias Ringwald } 1333cbbb12d9SMatthias Ringwald } 1334cbbb12d9SMatthias Ringwald 13359816429dSMatthias Ringwald uint8_t att_server_request_to_send_indication(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 133668591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 133768591e36SMatthias Ringwald if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 133868591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 13393f9b77dbSMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 1340c37df6f6SMatthias Ringwald bool added = btstack_linked_list_add_tail(&att_server->indication_requests, (btstack_linked_item_t*) callback_registration); 13413f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 1342c37df6f6SMatthias Ringwald if (added){ 1343969a5bbaSMatthias Ringwald return ERROR_CODE_SUCCESS; 1344c37df6f6SMatthias Ringwald } else { 1345c37df6f6SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1346c37df6f6SMatthias Ringwald } 1347bb38f057SMatthias Ringwald } 1348bb38f057SMatthias Ringwald 13499816429dSMatthias Ringwald uint8_t att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){ 135068591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 135168591e36SMatthias Ringwald if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 135237a8059dSMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 1353388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 135468591e36SMatthias Ringwald 135537a8059dSMatthias Ringwald if (!att_server_can_send_packet(att_server, att_connection)) return BTSTACK_ACL_BUFFERS_FULL; 13563deb3ec6SMatthias Ringwald 13573deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 13583deb3ec6SMatthias Ringwald uint8_t * packet_buffer = l2cap_get_outgoing_buffer(); 1359b6df12b6SMatthias Ringwald uint16_t size = att_prepare_handle_value_notification(att_connection, attribute_handle, value, value_len, packet_buffer); 1360d415948cSMatthias Ringwald 13610f70c52eSMatthias Ringwald return att_server_send_prepared(att_server, att_connection, NULL, size); 13623deb3ec6SMatthias Ringwald } 13633deb3ec6SMatthias Ringwald 1364*40f8d8f2SMatthias Ringwald /** 1365*40f8d8f2SMatthias Ringwald * @brief notify client about multiple attribute value changes 1366*40f8d8f2SMatthias Ringwald * @param con_handle 1367*40f8d8f2SMatthias Ringwald * @param num_attributes 1368*40f8d8f2SMatthias Ringwald * @param attribute_handles[] 1369*40f8d8f2SMatthias Ringwald * @param values_data[] 1370*40f8d8f2SMatthias Ringwald * @param values_len[] 1371*40f8d8f2SMatthias Ringwald * @return 0 if ok, error otherwise 1372*40f8d8f2SMatthias Ringwald */ 1373*40f8d8f2SMatthias Ringwald uint8_t att_server_multiple_notify(hci_con_handle_t con_handle, uint8_t num_attributes, 1374*40f8d8f2SMatthias Ringwald const uint16_t * attribute_handles, const uint8_t ** values_data, const uint16_t * values_len){ 1375*40f8d8f2SMatthias Ringwald 1376*40f8d8f2SMatthias Ringwald att_server_t * att_server = NULL; 1377*40f8d8f2SMatthias Ringwald att_connection_t * att_connection = NULL; 1378*40f8d8f2SMatthias Ringwald uint8_t * packet_buffer = NULL; 1379*40f8d8f2SMatthias Ringwald 1380*40f8d8f2SMatthias Ringwald // prfer enhanced bearer 1381*40f8d8f2SMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT 1382*40f8d8f2SMatthias Ringwald att_server_eatt_bearer_t * eatt_bearer = att_server_eatt_bearer_for_con_handle(con_handle); 1383*40f8d8f2SMatthias Ringwald if (eatt_bearer != NULL){ 1384*40f8d8f2SMatthias Ringwald att_server = &eatt_bearer->att_server; 1385*40f8d8f2SMatthias Ringwald att_connection = &eatt_bearer->att_connection; 1386*40f8d8f2SMatthias Ringwald packet_buffer = eatt_bearer->send_buffer; 1387*40f8d8f2SMatthias Ringwald } else 1388*40f8d8f2SMatthias Ringwald #endif 1389*40f8d8f2SMatthias Ringwald { 1390*40f8d8f2SMatthias Ringwald hci_connection_t *hci_connection = hci_connection_for_handle(con_handle); 1391*40f8d8f2SMatthias Ringwald if (hci_connection != NULL) { 1392*40f8d8f2SMatthias Ringwald att_server = &hci_connection->att_server; 1393*40f8d8f2SMatthias Ringwald att_connection = &hci_connection->att_connection; 1394*40f8d8f2SMatthias Ringwald } 1395*40f8d8f2SMatthias Ringwald } 1396*40f8d8f2SMatthias Ringwald 1397*40f8d8f2SMatthias Ringwald if (att_server == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1398*40f8d8f2SMatthias Ringwald if (!att_server_can_send_packet(att_server, att_connection)) return BTSTACK_ACL_BUFFERS_FULL; 1399*40f8d8f2SMatthias Ringwald 1400*40f8d8f2SMatthias Ringwald if (packet_buffer == NULL){ 1401*40f8d8f2SMatthias Ringwald l2cap_reserve_packet_buffer(); 1402*40f8d8f2SMatthias Ringwald packet_buffer = l2cap_get_outgoing_buffer(); 1403*40f8d8f2SMatthias Ringwald } 1404*40f8d8f2SMatthias Ringwald 1405*40f8d8f2SMatthias Ringwald uint16_t size = att_prepare_handle_value_multiple_notification(att_connection, num_attributes, attribute_handles, values_data, values_len, packet_buffer); 1406*40f8d8f2SMatthias Ringwald 1407*40f8d8f2SMatthias Ringwald return att_server_send_prepared(att_server, att_connection, packet_buffer, size); 1408*40f8d8f2SMatthias Ringwald } 1409*40f8d8f2SMatthias Ringwald 14109816429dSMatthias Ringwald uint8_t att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){ 141168591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 141268591e36SMatthias Ringwald if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 141368591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 1414388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 141518707219SMatthias Ringwald 141638b893aaSMilanka Ringwald if (att_server->value_indication_handle != 0u) return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS; 141737a8059dSMatthias Ringwald if (!att_server_can_send_packet(att_server, att_connection)) return BTSTACK_ACL_BUFFERS_FULL; 14183deb3ec6SMatthias Ringwald 14193deb3ec6SMatthias Ringwald // track indication 142018707219SMatthias Ringwald att_server->value_indication_handle = attribute_handle; 142118707219SMatthias Ringwald btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout); 142218707219SMatthias Ringwald btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS); 142318707219SMatthias Ringwald btstack_run_loop_add_timer(&att_server->value_indication_timer); 14243deb3ec6SMatthias Ringwald 14253deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 14263deb3ec6SMatthias Ringwald uint8_t * packet_buffer = l2cap_get_outgoing_buffer(); 1427b6df12b6SMatthias Ringwald uint16_t size = att_prepare_handle_value_indication(att_connection, attribute_handle, value, value_len, packet_buffer); 142823079b39SMatthias Ringwald 14290f70c52eSMatthias Ringwald return att_server_send_prepared(att_server, att_connection, NULL, size); 14303deb3ec6SMatthias Ringwald } 14318f9132b5SMilanka Ringwald 14328f9132b5SMilanka Ringwald uint16_t att_server_get_mtu(hci_con_handle_t con_handle){ 143368591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 143468591e36SMatthias Ringwald if (!hci_connection) return 0; 1435388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 1436b6df12b6SMatthias Ringwald return att_connection->mtu; 14378f9132b5SMilanka Ringwald } 143835d7f2dfSMilanka Ringwald 143935d7f2dfSMilanka Ringwald void att_server_deinit(void){ 144035d7f2dfSMilanka Ringwald att_server_client_read_callback = NULL; 144135d7f2dfSMilanka Ringwald att_server_client_write_callback = NULL; 144235d7f2dfSMilanka Ringwald att_client_packet_handler = NULL; 144335d7f2dfSMilanka Ringwald service_handlers = NULL; 144435d7f2dfSMilanka Ringwald } 144520d4358dSMatthias Ringwald 144620d4358dSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT 14478d3bb68fSMatthias Ringwald 144827afb094SMatthias Ringwald #define MAX_NR_EATT_CHANNELS 5 144927afb094SMatthias Ringwald 145020d4358dSMatthias Ringwald static uint16_t att_server_eatt_receive_buffer_size; 14518d3bb68fSMatthias Ringwald static uint16_t att_server_eatt_send_buffer_size; 145220d4358dSMatthias Ringwald 1453523bb989SMatthias Ringwald static att_server_eatt_bearer_t * att_server_eatt_bearer_for_cid(uint16_t cid){ 1454523bb989SMatthias Ringwald btstack_linked_list_iterator_t it; 1455523bb989SMatthias Ringwald btstack_linked_list_iterator_init(&it, &att_server_eatt_bearer_active); 1456523bb989SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 1457523bb989SMatthias Ringwald att_server_eatt_bearer_t * eatt_bearer = (att_server_eatt_bearer_t *) btstack_linked_list_iterator_next(&it); 1458f786dd08SMatthias Ringwald if (eatt_bearer->att_server.l2cap_cid == cid) { 1459523bb989SMatthias Ringwald return eatt_bearer; 1460523bb989SMatthias Ringwald } 1461523bb989SMatthias Ringwald } 1462523bb989SMatthias Ringwald return NULL; 1463523bb989SMatthias Ringwald } 1464523bb989SMatthias Ringwald 1465*40f8d8f2SMatthias Ringwald static att_server_eatt_bearer_t * att_server_eatt_bearer_for_con_handle(hci_con_handle_t con_handle){ 1466*40f8d8f2SMatthias Ringwald btstack_linked_list_iterator_t it; 1467*40f8d8f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &att_server_eatt_bearer_active); 1468*40f8d8f2SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 1469*40f8d8f2SMatthias Ringwald att_server_eatt_bearer_t * eatt_bearer = (att_server_eatt_bearer_t *) btstack_linked_list_iterator_next(&it); 1470*40f8d8f2SMatthias Ringwald if (eatt_bearer->att_connection.con_handle == con_handle) { 1471*40f8d8f2SMatthias Ringwald return eatt_bearer; 1472*40f8d8f2SMatthias Ringwald } 1473*40f8d8f2SMatthias Ringwald } 1474*40f8d8f2SMatthias Ringwald return NULL; 1475*40f8d8f2SMatthias Ringwald } 1476*40f8d8f2SMatthias Ringwald 147720d4358dSMatthias Ringwald static void att_server_eatt_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 147820d4358dSMatthias Ringwald uint16_t cid; 147920d4358dSMatthias Ringwald uint8_t status; 148020d4358dSMatthias Ringwald uint16_t remote_mtu; 148120d4358dSMatthias Ringwald 148220d4358dSMatthias Ringwald uint8_t i; 148320d4358dSMatthias Ringwald uint8_t num_requested_bearers; 148420d4358dSMatthias Ringwald uint8_t num_accepted_bearers; 148527afb094SMatthias Ringwald uint16_t initial_credits = L2CAP_LE_AUTOMATIC_CREDITS; 148627afb094SMatthias Ringwald uint8_t * receive_buffers[MAX_NR_EATT_CHANNELS]; 148727afb094SMatthias Ringwald uint16_t cids[MAX_NR_EATT_CHANNELS]; 148827afb094SMatthias Ringwald att_server_eatt_bearer_t * eatt_bearers[MAX_NR_EATT_CHANNELS]; 1489523bb989SMatthias Ringwald att_server_eatt_bearer_t * eatt_bearer; 14902954c397SMatthias Ringwald att_server_t * att_server; 14912954c397SMatthias Ringwald att_connection_t * att_connection; 149220d4358dSMatthias Ringwald 149320d4358dSMatthias Ringwald switch (packet_type) { 149420d4358dSMatthias Ringwald 149520d4358dSMatthias Ringwald case L2CAP_DATA_PACKET: 1496f786dd08SMatthias Ringwald eatt_bearer = att_server_eatt_bearer_for_cid(channel); 1497f786dd08SMatthias Ringwald btstack_assert(eatt_bearer != NULL); 1498f786dd08SMatthias Ringwald att_server = &eatt_bearer->att_server; 1499f786dd08SMatthias Ringwald att_connection = &eatt_bearer->att_connection; 1500f786dd08SMatthias Ringwald att_server_handle_att_pdu(att_server, att_connection, packet, size); 150120d4358dSMatthias Ringwald break; 150220d4358dSMatthias Ringwald 150320d4358dSMatthias Ringwald case HCI_EVENT_PACKET: 150420d4358dSMatthias Ringwald switch (packet[0]) { 150520d4358dSMatthias Ringwald 150620d4358dSMatthias Ringwald case L2CAP_EVENT_CAN_SEND_NOW: 1507f786dd08SMatthias Ringwald cid = l2cap_event_packet_sent_get_local_cid(packet); 1508f786dd08SMatthias Ringwald eatt_bearer = att_server_eatt_bearer_for_cid(cid); 1509f786dd08SMatthias Ringwald btstack_assert(eatt_bearer != NULL); 1510f786dd08SMatthias Ringwald att_server = &eatt_bearer->att_server; 1511f786dd08SMatthias Ringwald att_connection = &eatt_bearer->att_connection; 1512f786dd08SMatthias Ringwald // only used for EATT request responses 1513f786dd08SMatthias Ringwald btstack_assert(att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED); 1514f786dd08SMatthias Ringwald att_server_process_validated_request(att_server, att_connection, eatt_bearer->send_buffer); 151520d4358dSMatthias Ringwald break; 151620d4358dSMatthias Ringwald 151720d4358dSMatthias Ringwald case L2CAP_EVENT_PACKET_SENT: 151820d4358dSMatthias Ringwald cid = l2cap_event_packet_sent_get_local_cid(packet); 1519be4ca859SMatthias Ringwald eatt_bearer = att_server_eatt_bearer_for_cid(cid); 1520f786dd08SMatthias Ringwald btstack_assert(eatt_bearer != NULL); 15212954c397SMatthias Ringwald att_server = &eatt_bearer->att_server; 15222954c397SMatthias Ringwald att_connection = &eatt_bearer->att_connection; 15232954c397SMatthias Ringwald att_server_handle_att_pdu(att_server, att_connection, packet, size); 152420d4358dSMatthias Ringwald break; 152520d4358dSMatthias Ringwald 152620d4358dSMatthias Ringwald case L2CAP_EVENT_ECBM_INCOMING_CONNECTION: 152720d4358dSMatthias Ringwald cid = l2cap_event_ecbm_incoming_connection_get_local_cid(packet); 152820d4358dSMatthias Ringwald num_requested_bearers = l2cap_event_ecbm_incoming_connection_get_num_channels(packet); 152920d4358dSMatthias Ringwald for (i = 0; i < num_requested_bearers; i++){ 1530c2cbe117SMatthias Ringwald eatt_bearers[i] = (att_server_eatt_bearer_t *) btstack_linked_list_pop(&att_server_eatt_bearer_pool); 153120d4358dSMatthias Ringwald if (eatt_bearers[i] == NULL) { 153220d4358dSMatthias Ringwald break; 153320d4358dSMatthias Ringwald } 1534be4ca859SMatthias Ringwald eatt_bearers[i]->att_connection.con_handle = l2cap_event_ecbm_incoming_connection_get_handle(packet); 1535f786dd08SMatthias Ringwald eatt_bearers[i]->att_server.bearer_type = ATT_BEARER_ENHANCED_LE; 1536735b0222SMatthias Ringwald receive_buffers[i] = eatt_bearers[i]->receive_buffer; 15376b236cb0SMatthias Ringwald btstack_linked_list_add(&att_server_eatt_bearer_active, (btstack_linked_item_t *) eatt_bearers[i]); 153820d4358dSMatthias Ringwald } 153920d4358dSMatthias Ringwald num_accepted_bearers = i; 154020d4358dSMatthias Ringwald status = l2cap_ecbm_accept_channels(cid, num_accepted_bearers, initial_credits, att_server_eatt_receive_buffer_size, receive_buffers, cids); 154120d4358dSMatthias Ringwald btstack_assert(status == ERROR_CODE_SUCCESS); 1542c2cbe117SMatthias Ringwald log_info("requested %u, accepted %u", num_requested_bearers, num_accepted_bearers); 15436b236cb0SMatthias Ringwald for (i=0;i<num_accepted_bearers;i++){ 1544c2cbe117SMatthias Ringwald log_info("eatt l2cap cid: 0x%04x", cids[i]); 1545f786dd08SMatthias Ringwald eatt_bearers[i]->att_server.l2cap_cid = cids[i]; 15466b236cb0SMatthias Ringwald } 154720d4358dSMatthias Ringwald break; 154820d4358dSMatthias Ringwald 154920d4358dSMatthias Ringwald case L2CAP_EVENT_ECBM_CHANNEL_OPENED: 155020d4358dSMatthias Ringwald cid = l2cap_event_ecbm_channel_opened_get_local_cid(packet); 1551c2cbe117SMatthias Ringwald status = l2cap_event_ecbm_channel_opened_get_status(packet); 1552c2cbe117SMatthias Ringwald remote_mtu = l2cap_event_ecbm_channel_opened_get_remote_mtu(packet); 1553be4ca859SMatthias Ringwald eatt_bearer = att_server_eatt_bearer_for_cid(cid); 1554be4ca859SMatthias Ringwald btstack_assert(eatt_bearer != NULL); 1555c2cbe117SMatthias Ringwald eatt_bearer->att_connection.mtu_exchanged = true; 1556c2cbe117SMatthias Ringwald eatt_bearer->att_connection.mtu = remote_mtu; 1557be4ca859SMatthias Ringwald eatt_bearer->att_connection.max_mtu = remote_mtu; 155820d4358dSMatthias Ringwald log_info("L2CAP_EVENT_ECBM_CHANNEL_OPENED - cid 0x%04x mtu %u, status 0x%02x", cid, remote_mtu, status); 155920d4358dSMatthias Ringwald break; 156020d4358dSMatthias Ringwald 156120d4358dSMatthias Ringwald case L2CAP_EVENT_ECBM_RECONFIGURED: 156220d4358dSMatthias Ringwald break; 156320d4358dSMatthias Ringwald 1564523bb989SMatthias Ringwald case L2CAP_EVENT_CHANNEL_CLOSED: 1565523bb989SMatthias Ringwald eatt_bearer = att_server_eatt_bearer_for_cid(l2cap_event_channel_closed_get_local_cid(packet)); 1566523bb989SMatthias Ringwald btstack_assert(eatt_bearers != NULL); 1567523bb989SMatthias Ringwald 1568523bb989SMatthias Ringwald // TODO: finalize - abort queued writes 1569523bb989SMatthias Ringwald 157027afb094SMatthias Ringwald btstack_linked_list_remove(&att_server_eatt_bearer_active, (btstack_linked_item_t *) eatt_bearer); 1571523bb989SMatthias Ringwald btstack_linked_list_add(&att_server_eatt_bearer_pool, (btstack_linked_item_t *) eatt_bearer); 1572523bb989SMatthias Ringwald break; 157320d4358dSMatthias Ringwald default: 157420d4358dSMatthias Ringwald break; 157520d4358dSMatthias Ringwald } 157627afb094SMatthias Ringwald break; 157727afb094SMatthias Ringwald default: 157827afb094SMatthias Ringwald btstack_unreachable(); 157927afb094SMatthias Ringwald break; 158020d4358dSMatthias Ringwald } 158120d4358dSMatthias Ringwald } 158220d4358dSMatthias Ringwald 158320d4358dSMatthias Ringwald // create eatt bearers 158420d4358dSMatthias Ringwald uint8_t att_server_eatt_init(uint8_t num_eatt_bearers, uint8_t * storage_buffer, uint16_t storage_size){ 158520d4358dSMatthias Ringwald uint16_t size_for_structs = num_eatt_bearers * sizeof(att_server_eatt_bearer_t); 158620d4358dSMatthias Ringwald if (storage_size < size_for_structs) { 158720d4358dSMatthias Ringwald return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED; 158820d4358dSMatthias Ringwald } 158920d4358dSMatthias Ringwald 159020d4358dSMatthias Ringwald // TODO: The minimum ATT_MTU for an Enhanced ATT bearer is 64 octets. 159120d4358dSMatthias Ringwald 159220d4358dSMatthias Ringwald memset(storage_buffer, 0, storage_size); 15938d3bb68fSMatthias Ringwald uint16_t buffer_size_per_bearer = ((storage_size - size_for_structs) / num_eatt_bearers); 15948d3bb68fSMatthias Ringwald att_server_eatt_receive_buffer_size = buffer_size_per_bearer / 2; 15958d3bb68fSMatthias Ringwald att_server_eatt_send_buffer_size = buffer_size_per_bearer / 2; 1596735b0222SMatthias Ringwald uint8_t * bearer_buffer = &storage_buffer[size_for_structs]; 159720d4358dSMatthias Ringwald uint8_t i; 159820d4358dSMatthias Ringwald att_server_eatt_bearer_t * eatt_bearer = (att_server_eatt_bearer_t *) storage_buffer; 1599be4ca859SMatthias Ringwald log_info("%u EATT bearers with receive buffer size %u", 1600be4ca859SMatthias Ringwald num_eatt_bearers, att_server_eatt_receive_buffer_size); 160120d4358dSMatthias Ringwald for (i=0;i<num_eatt_bearers;i++){ 1602be4ca859SMatthias Ringwald eatt_bearer->att_connection.con_handle = HCI_CON_HANDLE_INVALID; 1603735b0222SMatthias Ringwald eatt_bearer->receive_buffer = bearer_buffer; 1604735b0222SMatthias Ringwald bearer_buffer += att_server_eatt_receive_buffer_size; 16058d3bb68fSMatthias Ringwald eatt_bearer->send_buffer = bearer_buffer; 16068d3bb68fSMatthias Ringwald bearer_buffer += att_server_eatt_send_buffer_size; 16076b236cb0SMatthias Ringwald btstack_linked_list_add(&att_server_eatt_bearer_pool, (btstack_linked_item_t *) eatt_bearer); 160820d4358dSMatthias Ringwald eatt_bearer++; 160920d4358dSMatthias Ringwald } 161020d4358dSMatthias Ringwald // TODO: define minimum EATT MTU 161120d4358dSMatthias Ringwald l2cap_ecbm_register_service(att_server_eatt_handler, BLUETOOTH_PSM_EATT, 64, 0); 161220d4358dSMatthias Ringwald 161320d4358dSMatthias Ringwald return 0; 161420d4358dSMatthias Ringwald } 161520d4358dSMatthias Ringwald #endif