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 115*c2cbe117SMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT 116*c2cbe117SMatthias Ringwald typedef struct { 117*c2cbe117SMatthias Ringwald btstack_linked_item_t item; 118*c2cbe117SMatthias Ringwald att_server_t att_server; 119*c2cbe117SMatthias Ringwald att_connection_t att_connection; 120*c2cbe117SMatthias Ringwald uint8_t * receive_buffer; 121*c2cbe117SMatthias Ringwald uint8_t * send_buffer; 122*c2cbe117SMatthias Ringwald } att_server_eatt_bearer_t; 123*c2cbe117SMatthias Ringwald static btstack_linked_list_t att_server_eatt_bearer_pool; 124*c2cbe117SMatthias Ringwald static btstack_linked_list_t att_server_eatt_bearer_active; 125*c2cbe117SMatthias Ringwald #endif 126*c2cbe117SMatthias Ringwald 12702b78fc8SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 12826567ee7SMatthias 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){ 12918707219SMatthias Ringwald btstack_linked_list_iterator_t it; 13018707219SMatthias Ringwald hci_connections_get_iterator(&it); 13118707219SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 13226567ee7SMatthias Ringwald hci_connection_t * hci_connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 13326567ee7SMatthias Ringwald if (hci_connection->att_server.state == state) { 13426567ee7SMatthias Ringwald *att_server_out = &hci_connection->att_server; 13526567ee7SMatthias Ringwald *att_connection_out = &hci_connection->att_connection; 13626567ee7SMatthias Ringwald return true; 13718707219SMatthias Ringwald } 13826567ee7SMatthias Ringwald } 139*c2cbe117SMatthias Ringwald 140*c2cbe117SMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT 141*c2cbe117SMatthias Ringwald btstack_linked_list_iterator_init(&it, &att_server_eatt_bearer_active); 142*c2cbe117SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 143*c2cbe117SMatthias Ringwald att_server_eatt_bearer_t * eatt_bearer = (att_server_eatt_bearer_t *) btstack_linked_list_iterator_next(&it); 144*c2cbe117SMatthias Ringwald if (eatt_bearer->att_server.state == state) { 145*c2cbe117SMatthias Ringwald *att_server_out = &eatt_bearer->att_server; 146*c2cbe117SMatthias Ringwald *att_connection_out = &eatt_bearer->att_connection; 147*c2cbe117SMatthias Ringwald return true; 148*c2cbe117SMatthias Ringwald } 149*c2cbe117SMatthias Ringwald } 150*c2cbe117SMatthias Ringwald #endif 151*c2cbe117SMatthias Ringwald 15226567ee7SMatthias Ringwald return false; 15318707219SMatthias Ringwald } 15402b78fc8SMatthias Ringwald #endif 155bb38f057SMatthias Ringwald 1563f9b77dbSMatthias Ringwald static void att_server_request_can_send_now(att_server_t * att_server, att_connection_t * att_connection ){ 15723079b39SMatthias Ringwald switch (att_server->bearer_type){ 15823079b39SMatthias Ringwald case ATT_BEARER_UNENHANCED_LE: 159b6df12b6SMatthias Ringwald att_dispatch_server_request_can_send_now_event(att_connection->con_handle); 16023079b39SMatthias Ringwald break; 1612954c397SMatthias Ringwald #if defined (ENABLE_GATT_OVER_CLASSIC) || defined (ENABLE_GATT_OVER_EATT) 16223079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 16323079b39SMatthias Ringwald case ATT_BEARER_UNENHANCED_CLASSIC: 1642954c397SMatthias Ringwald /* fall through */ 1652954c397SMatthias Ringwald #endif 1662954c397SMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT 1672954c397SMatthias Ringwald case ATT_BEARER_ENHANCED_LE: 1682954c397SMatthias Ringwald /* fall through */ 1692954c397SMatthias Ringwald #endif 17023079b39SMatthias Ringwald l2cap_request_can_send_now_event(att_server->l2cap_cid); 17123079b39SMatthias Ringwald break; 17223079b39SMatthias Ringwald #endif 17323079b39SMatthias Ringwald default: 17423079b39SMatthias Ringwald btstack_unreachable(); 17523079b39SMatthias Ringwald break; 17623079b39SMatthias Ringwald } 177c8c6e9b4SMatthias Ringwald } 178c8c6e9b4SMatthias Ringwald 17937a8059dSMatthias Ringwald static bool att_server_can_send_packet(att_server_t * att_server, att_connection_t * att_connection){ 18023079b39SMatthias Ringwald switch (att_server->bearer_type) { 18123079b39SMatthias Ringwald case ATT_BEARER_UNENHANCED_LE: 1821979f09cSMatthias Ringwald return att_dispatch_server_can_send_now(att_connection->con_handle) != 0; 1832954c397SMatthias Ringwald #if defined (ENABLE_GATT_OVER_CLASSIC) || defined (ENABLE_GATT_OVER_EATT) 18423079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 18523079b39SMatthias Ringwald case ATT_BEARER_UNENHANCED_CLASSIC: 1862954c397SMatthias Ringwald /* fall through */ 1872954c397SMatthias Ringwald #endif 1882954c397SMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT 1892954c397SMatthias Ringwald case ATT_BEARER_ENHANCED_LE: 1902954c397SMatthias Ringwald #endif 1912954c397SMatthias Ringwald return l2cap_can_send_packet_now(att_server->l2cap_cid); 19223079b39SMatthias Ringwald #endif 19323079b39SMatthias Ringwald default: 19423079b39SMatthias Ringwald btstack_unreachable(); 19523079b39SMatthias Ringwald break; 19623079b39SMatthias Ringwald } 19723079b39SMatthias Ringwald return false; 198c0f26e1cSMatthias Ringwald } 199c0f26e1cSMatthias Ringwald 2003deb3ec6SMatthias Ringwald static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){ 2015d07396bSMatthias Ringwald btstack_packet_handler_t packet_handler = att_server_packet_handler_for_handle(attribute_handle); 2025d07396bSMatthias Ringwald if (!packet_handler) return; 2033deb3ec6SMatthias Ringwald 2043deb3ec6SMatthias Ringwald uint8_t event[7]; 2053deb3ec6SMatthias Ringwald int pos = 0; 2065611a760SMatthias Ringwald event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE; 2074ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 2083deb3ec6SMatthias Ringwald event[pos++] = status; 209f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, client_handle); 2103deb3ec6SMatthias Ringwald pos += 2; 211f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, attribute_handle); 212a444112bSMatthias Ringwald (*packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 2133deb3ec6SMatthias Ringwald } 2143deb3ec6SMatthias Ringwald 2155d07396bSMatthias Ringwald static void att_emit_event_to_all(const uint8_t * event, uint16_t size){ 2165d07396bSMatthias Ringwald // dispatch to app level handler 2179305033eSMatthias Ringwald if (att_client_packet_handler != NULL){ 2185d07396bSMatthias Ringwald (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size); 2195d07396bSMatthias Ringwald } 2203deb3ec6SMatthias Ringwald 2215d07396bSMatthias Ringwald // dispatch to service handlers 2225d07396bSMatthias Ringwald btstack_linked_list_iterator_t it; 2235d07396bSMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 2245d07396bSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2255d07396bSMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 2265d07396bSMatthias Ringwald if (!handler->packet_handler) continue; 2275d07396bSMatthias Ringwald (*handler->packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size); 2285d07396bSMatthias Ringwald } 2295d07396bSMatthias Ringwald } 2305d07396bSMatthias Ringwald 2315d07396bSMatthias Ringwald static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){ 2323deb3ec6SMatthias Ringwald uint8_t event[6]; 2333deb3ec6SMatthias Ringwald int pos = 0; 2345611a760SMatthias Ringwald event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE; 2354ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 236fc64f94aSMatthias Ringwald little_endian_store_16(event, pos, con_handle); 2373deb3ec6SMatthias Ringwald pos += 2; 238f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, mtu); 2395d07396bSMatthias Ringwald 2405d07396bSMatthias Ringwald // also dispatch to GATT Clients 241b12646c5SJakob Krantz att_dispatch_server_mtu_exchanged(con_handle, mtu); 2425d07396bSMatthias Ringwald 2435d07396bSMatthias Ringwald // dispatch to app level handler and service handlers 2445d07396bSMatthias Ringwald att_emit_event_to_all(&event[0], sizeof(event)); 2453deb3ec6SMatthias Ringwald } 2463deb3ec6SMatthias Ringwald 2475f141511SMatthias Ringwald static void att_emit_can_send_now_event(void * context){ 2485f141511SMatthias Ringwald UNUSED(context); 2493c97b242SMatthias Ringwald if (!att_client_packet_handler) return; 2503c97b242SMatthias Ringwald 2511b96b007SMatthias Ringwald uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0}; 2521b96b007SMatthias Ringwald (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 2531b96b007SMatthias Ringwald } 2541b96b007SMatthias Ringwald 2556d2214cbSMatthias Ringwald static void att_emit_connected_event(att_server_t * att_server, att_connection_t * att_connection){ 256ff1bec95SMatthias Ringwald uint8_t event[11]; 257ff1bec95SMatthias Ringwald int pos = 0; 258ff1bec95SMatthias Ringwald event[pos++] = ATT_EVENT_CONNECTED; 2594ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 260ff1bec95SMatthias Ringwald event[pos++] = att_server->peer_addr_type; 261ff1bec95SMatthias Ringwald reverse_bd_addr(att_server->peer_address, &event[pos]); 262ff1bec95SMatthias Ringwald pos += 6; 263b6df12b6SMatthias Ringwald little_endian_store_16(event, pos, att_connection->con_handle); 264ff1bec95SMatthias Ringwald pos += 2; 265ff1bec95SMatthias Ringwald 266ff1bec95SMatthias Ringwald // dispatch to app level handler and service handlers 267ff1bec95SMatthias Ringwald att_emit_event_to_all(&event[0], sizeof(event)); 268ff1bec95SMatthias Ringwald } 269ff1bec95SMatthias Ringwald 270ff1bec95SMatthias Ringwald 2716187ad4cSMatthias Ringwald static void att_emit_disconnected_event(uint16_t con_handle){ 272ff1bec95SMatthias Ringwald uint8_t event[4]; 273ff1bec95SMatthias Ringwald int pos = 0; 274ff1bec95SMatthias Ringwald event[pos++] = ATT_EVENT_DISCONNECTED; 2754ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 2766187ad4cSMatthias Ringwald little_endian_store_16(event, pos, con_handle); 277ff1bec95SMatthias Ringwald pos += 2; 278ff1bec95SMatthias Ringwald 279ff1bec95SMatthias Ringwald // dispatch to app level handler and service handlers 280ff1bec95SMatthias Ringwald att_emit_event_to_all(&event[0], sizeof(event)); 281ff1bec95SMatthias Ringwald } 282ff1bec95SMatthias Ringwald 283ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){ 28454178543SMatthias Ringwald void * context = btstack_run_loop_get_timer_context(ts); 28554178543SMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context; 28668591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 28768591e36SMatthias Ringwald if (!hci_connection) return; 28868591e36SMatthias Ringwald // @note: after a transaction timeout, no more requests shall be sent over this ATT Bearer 289bce48a26SMatthias Ringwald // (that's why we don't reset the value_indication_handle) 290b6df12b6SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 29118707219SMatthias Ringwald uint16_t att_handle = att_server->value_indication_handle; 292388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 29338b893aaSMilanka Ringwald att_handle_value_indication_notify_client((uint8_t)ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_connection->con_handle, att_handle); 2943deb3ec6SMatthias Ringwald } 2953deb3ec6SMatthias Ringwald 2963deb3ec6SMatthias Ringwald static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2979ec2630cSMatthias Ringwald 2987dad9ff8SMatthias Ringwald UNUSED(channel); // ok: there is no channel 2997dad9ff8SMatthias Ringwald UNUSED(size); // ok: handling own l2cap events 3003deb3ec6SMatthias Ringwald 30118707219SMatthias Ringwald att_server_t * att_server; 302b6df12b6SMatthias Ringwald att_connection_t * att_connection; 30318707219SMatthias Ringwald hci_con_handle_t con_handle; 30468591e36SMatthias Ringwald hci_connection_t * hci_connection; 30570dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 30670dca4d4SMatthias Ringwald bd_addr_t address; 30768591e36SMatthias Ringwald btstack_linked_list_iterator_t it; 30870dca4d4SMatthias Ringwald #endif 30918707219SMatthias Ringwald 3103deb3ec6SMatthias Ringwald switch (packet_type) { 3113deb3ec6SMatthias Ringwald 3123deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 3130e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 3143deb3ec6SMatthias Ringwald 31570dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 31670dca4d4SMatthias Ringwald case L2CAP_EVENT_INCOMING_CONNECTION: 31770dca4d4SMatthias Ringwald l2cap_event_incoming_connection_get_address(packet, address); 31870dca4d4SMatthias Ringwald l2cap_accept_connection(channel); 31936de8547SMatthias Ringwald log_info("Accept incoming connection from %s", bd_addr_to_str(address)); 32070dca4d4SMatthias Ringwald break; 32170dca4d4SMatthias Ringwald case L2CAP_EVENT_CHANNEL_OPENED: 322122b723eSMatthias Ringwald con_handle = l2cap_event_channel_opened_get_handle(packet); 32368591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 32468591e36SMatthias Ringwald if (!hci_connection) break; 32568591e36SMatthias Ringwald att_server = &hci_connection->att_server; 326122b723eSMatthias Ringwald // store connection info 32723079b39SMatthias Ringwald att_server->bearer_type = ATT_BEARER_UNENHANCED_CLASSIC; 328f16129ceSMatthias Ringwald att_server->peer_addr_type = BD_ADDR_TYPE_ACL; 329122b723eSMatthias Ringwald l2cap_event_channel_opened_get_address(packet, att_server->peer_address); 330388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 331b6df12b6SMatthias Ringwald att_connection->con_handle = con_handle; 33270dca4d4SMatthias Ringwald att_server->l2cap_cid = l2cap_event_channel_opened_get_local_cid(packet); 333122b723eSMatthias Ringwald // reset connection properties 334122b723eSMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 335b6df12b6SMatthias Ringwald att_connection->mtu = l2cap_event_channel_opened_get_remote_mtu(packet); 336b6df12b6SMatthias Ringwald att_connection->max_mtu = l2cap_max_mtu(); 337b6df12b6SMatthias Ringwald if (att_connection->max_mtu > ATT_REQUEST_BUFFER_SIZE){ 338b6df12b6SMatthias Ringwald att_connection->max_mtu = ATT_REQUEST_BUFFER_SIZE; 339122b723eSMatthias Ringwald } 340df33ee14SMatthias Ringwald 341b6df12b6SMatthias Ringwald log_info("Connection opened %s, l2cap cid %04x, mtu %u", bd_addr_to_str(address), att_server->l2cap_cid, att_connection->mtu); 342df33ee14SMatthias Ringwald 343914988a1SMatthias Ringwald // update security params 344b6df12b6SMatthias Ringwald att_connection->encryption_key_size = gap_encryption_key_size(con_handle); 345b6df12b6SMatthias Ringwald att_connection->authenticated = gap_authenticated(con_handle); 346b6df12b6SMatthias Ringwald att_connection->secure_connection = gap_secure_connection(con_handle); 347914988a1SMatthias Ringwald log_info("encrypted key size %u, authenticated %u, secure connection %u", 348b6df12b6SMatthias Ringwald att_connection->encryption_key_size, att_connection->authenticated, att_connection->secure_connection); 349df33ee14SMatthias Ringwald 350df33ee14SMatthias Ringwald // notify connection opened 3516d2214cbSMatthias Ringwald att_emit_connected_event(att_server, att_connection); 352df33ee14SMatthias Ringwald 353914988a1SMatthias Ringwald // restore persisten ccc if encrypted 354914988a1SMatthias Ringwald if ( gap_security_level(con_handle) >= LEVEL_2){ 3559a1f55eaSMatthias Ringwald att_server_persistent_ccc_restore(att_server, att_connection); 356914988a1SMatthias Ringwald } 357122b723eSMatthias Ringwald // TODO: what to do about le device db? 358122b723eSMatthias Ringwald att_server->pairing_active = 0; 35970dca4d4SMatthias Ringwald break; 36048364364SMatthias Ringwald case L2CAP_EVENT_CAN_SEND_NOW: 36148364364SMatthias Ringwald att_server_handle_can_send_now(); 36248364364SMatthias Ringwald break; 36348364364SMatthias Ringwald 36470dca4d4SMatthias Ringwald #endif 3653deb3ec6SMatthias Ringwald case HCI_EVENT_LE_META: 3663deb3ec6SMatthias Ringwald switch (packet[2]) { 3673deb3ec6SMatthias Ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 36818707219SMatthias Ringwald con_handle = little_endian_read_16(packet, 4); 36968591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 37068591e36SMatthias Ringwald if (!hci_connection) break; 37168591e36SMatthias Ringwald att_server = &hci_connection->att_server; 3723deb3ec6SMatthias Ringwald // store connection info 37318707219SMatthias Ringwald att_server->peer_addr_type = packet[7]; 37418707219SMatthias Ringwald reverse_bd_addr(&packet[8], att_server->peer_address); 375388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 376b6df12b6SMatthias Ringwald att_connection->con_handle = con_handle; 3773deb3ec6SMatthias Ringwald // reset connection properties 37818707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 37923079b39SMatthias Ringwald att_server->bearer_type = ATT_BEARER_UNENHANCED_LE; 380b6df12b6SMatthias Ringwald att_connection->mtu = ATT_DEFAULT_MTU; 381b6df12b6SMatthias Ringwald att_connection->max_mtu = l2cap_max_le_mtu(); 382b6df12b6SMatthias Ringwald if (att_connection->max_mtu > ATT_REQUEST_BUFFER_SIZE){ 383b6df12b6SMatthias Ringwald att_connection->max_mtu = ATT_REQUEST_BUFFER_SIZE; 38499c58ad0SMatthias Ringwald } 38538b893aaSMilanka Ringwald att_connection->encryption_key_size = 0u; 38638b893aaSMilanka Ringwald att_connection->authenticated = 0u; 38738b893aaSMilanka Ringwald att_connection->authorized = 0u; 388b2c1e52cSMatthias Ringwald // workaround: identity resolving can already be complete, at least store result 389b2c1e52cSMatthias Ringwald att_server->ir_le_device_db_index = sm_le_device_index(con_handle); 39038b893aaSMilanka Ringwald att_server->ir_lookup_active = 0u; 39138b893aaSMilanka Ringwald att_server->pairing_active = 0u; 392ff1bec95SMatthias Ringwald // notify all - old 3935d07396bSMatthias Ringwald att_emit_event_to_all(packet, size); 394ff1bec95SMatthias Ringwald // notify all - new 3956d2214cbSMatthias Ringwald att_emit_connected_event(att_server, att_connection); 3963deb3ec6SMatthias Ringwald break; 3973deb3ec6SMatthias Ringwald 3983deb3ec6SMatthias Ringwald default: 3993deb3ec6SMatthias Ringwald break; 4003deb3ec6SMatthias Ringwald } 4013deb3ec6SMatthias Ringwald break; 4023deb3ec6SMatthias Ringwald 4033deb3ec6SMatthias Ringwald case HCI_EVENT_ENCRYPTION_CHANGE: 4048601e696SMatthias Ringwald case HCI_EVENT_ENCRYPTION_CHANGE_V2: 4053deb3ec6SMatthias Ringwald case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE: 4063deb3ec6SMatthias Ringwald // check handle 40718707219SMatthias Ringwald con_handle = little_endian_read_16(packet, 3); 40868591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 40968591e36SMatthias Ringwald if (!hci_connection) break; 410914988a1SMatthias Ringwald if (gap_get_connection_type(con_handle) != GAP_CONNECTION_LE) break; 411914988a1SMatthias Ringwald // update security params 41268591e36SMatthias Ringwald att_server = &hci_connection->att_server; 413388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 414b6df12b6SMatthias Ringwald att_connection->encryption_key_size = gap_encryption_key_size(con_handle); 415ff3f1026SMatthias Ringwald att_connection->authenticated = gap_authenticated(con_handle) ? 1 : 0; 416f461b3dfSMatthias Ringwald att_connection->secure_connection = gap_secure_connection(con_handle) ? 1 : 0; 417914988a1SMatthias Ringwald log_info("encrypted key size %u, authenticated %u, secure connection %u", 418b6df12b6SMatthias Ringwald att_connection->encryption_key_size, att_connection->authenticated, att_connection->secure_connection); 419914988a1SMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){ 4204097998aSMatthias Ringwald // restore CCC values when encrypted for LE Connections 42122bfc225SMilanka Ringwald if (hci_event_encryption_change_get_encryption_enabled(packet) != 0){ 4229a1f55eaSMatthias Ringwald att_server_persistent_ccc_restore(att_server, att_connection); 42301ac2008SMatthias Ringwald } 42401ac2008SMatthias Ringwald } 425cd45e970SMatthias Ringwald att_run_for_context(att_server, att_connection); 4263deb3ec6SMatthias Ringwald break; 4273deb3ec6SMatthias Ringwald 4283deb3ec6SMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 42970a390c7SMatthias Ringwald // check handle 43018707219SMatthias Ringwald con_handle = hci_event_disconnection_complete_get_connection_handle(packet); 43168591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 43268591e36SMatthias Ringwald if (!hci_connection) break; 43368591e36SMatthias Ringwald att_server = &hci_connection->att_server; 434388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 435b6df12b6SMatthias Ringwald att_clear_transaction_queue(att_connection); 436b6df12b6SMatthias Ringwald att_connection->con_handle = 0; 437760ad805SMatthias Ringwald att_server->pairing_active = 0; 43818707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 43938b893aaSMilanka Ringwald if (att_server->value_indication_handle != 0u){ 44027328ecbSMatthias Ringwald btstack_run_loop_remove_timer(&att_server->value_indication_timer); 441bce48a26SMatthias Ringwald uint16_t att_handle = att_server->value_indication_handle; 44238b893aaSMilanka Ringwald att_server->value_indication_handle = 0u; // reset error state 44338b893aaSMilanka Ringwald att_handle_value_indication_notify_client((uint8_t)ATT_HANDLE_VALUE_INDICATION_DISCONNECT, att_connection->con_handle, att_handle); 444bce48a26SMatthias Ringwald } 445ff1bec95SMatthias Ringwald // notify all - new 4466187ad4cSMatthias Ringwald att_emit_disconnected_event(con_handle); 447ff1bec95SMatthias Ringwald // notify all - old 4485d07396bSMatthias Ringwald att_emit_event_to_all(packet, size); 4493deb3ec6SMatthias Ringwald break; 4503deb3ec6SMatthias Ringwald 451760ad805SMatthias Ringwald // Identity Resolving 4525611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_STARTED: 45318707219SMatthias Ringwald con_handle = sm_event_identity_resolving_started_get_handle(packet); 45468591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 45568591e36SMatthias Ringwald if (!hci_connection) break; 45668591e36SMatthias Ringwald att_server = &hci_connection->att_server; 4575611a760SMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED"); 45830c8e114SMatthias Ringwald att_server->ir_lookup_active = 1; 4593deb3ec6SMatthias Ringwald break; 4605611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED: 461760ad805SMatthias Ringwald con_handle = sm_event_identity_created_get_handle(packet); 46268591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 46368591e36SMatthias Ringwald if (!hci_connection) return; 46468591e36SMatthias Ringwald att_server = &hci_connection->att_server; 46530c8e114SMatthias Ringwald att_server->ir_lookup_active = 0; 4661b7acd0dSMatthias Ringwald att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index(packet); 4671b7acd0dSMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED"); 468cd45e970SMatthias Ringwald att_run_for_context(att_server, att_connection); 4693deb3ec6SMatthias Ringwald break; 4705611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_FAILED: 47118707219SMatthias Ringwald con_handle = sm_event_identity_resolving_failed_get_handle(packet); 47268591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 47368591e36SMatthias Ringwald if (!hci_connection) break; 47468591e36SMatthias Ringwald att_server = &hci_connection->att_server; 4755611a760SMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED"); 47630c8e114SMatthias Ringwald att_server->ir_lookup_active = 0; 47718707219SMatthias Ringwald att_server->ir_le_device_db_index = -1; 478cd45e970SMatthias Ringwald att_run_for_context(att_server, att_connection); 4793deb3ec6SMatthias Ringwald break; 480760ad805SMatthias Ringwald 481760ad805SMatthias Ringwald // Pairing started - delete stored CCC values 482760ad805SMatthias Ringwald // - assumes pairing indicates either new device or re-pairing, in both cases there should be no stored CCC values 483760ad805SMatthias Ringwald // - assumes that all events have the con handle as the first field 484760ad805SMatthias Ringwald case SM_EVENT_JUST_WORKS_REQUEST: 485760ad805SMatthias Ringwald case SM_EVENT_PASSKEY_DISPLAY_NUMBER: 486760ad805SMatthias Ringwald case SM_EVENT_PASSKEY_INPUT_NUMBER: 487760ad805SMatthias Ringwald case SM_EVENT_NUMERIC_COMPARISON_REQUEST: 488760ad805SMatthias Ringwald con_handle = sm_event_just_works_request_get_handle(packet); 48968591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 49068591e36SMatthias Ringwald if (!hci_connection) break; 49168591e36SMatthias Ringwald att_server = &hci_connection->att_server; 492760ad805SMatthias Ringwald att_server->pairing_active = 1; 493760ad805SMatthias Ringwald log_info("SM Pairing started"); 494760ad805SMatthias Ringwald if (att_server->ir_le_device_db_index < 0) break; 495cd45e970SMatthias Ringwald att_run_for_context(att_server, att_connection); 496760ad805SMatthias Ringwald // index not valid anymore 497760ad805SMatthias Ringwald att_server->ir_le_device_db_index = -1; 498760ad805SMatthias Ringwald break; 499760ad805SMatthias Ringwald 5001b7acd0dSMatthias Ringwald // Bonding completed 501760ad805SMatthias Ringwald case SM_EVENT_IDENTITY_CREATED: 502760ad805SMatthias Ringwald con_handle = sm_event_identity_created_get_handle(packet); 50368591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 50468591e36SMatthias Ringwald if (!hci_connection) return; 50568591e36SMatthias Ringwald att_server = &hci_connection->att_server; 506760ad805SMatthias Ringwald att_server->pairing_active = 0; 5071b7acd0dSMatthias Ringwald att_server->ir_le_device_db_index = sm_event_identity_created_get_index(packet); 508cd45e970SMatthias Ringwald att_run_for_context(att_server, att_connection); 5091b7acd0dSMatthias Ringwald break; 5101b7acd0dSMatthias Ringwald 5111b7acd0dSMatthias Ringwald // Pairing complete (with/without bonding=storing of pairing information) 5121b7acd0dSMatthias Ringwald case SM_EVENT_PAIRING_COMPLETE: 5131b7acd0dSMatthias Ringwald con_handle = sm_event_pairing_complete_get_handle(packet); 51468591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 51568591e36SMatthias Ringwald if (!hci_connection) return; 51668591e36SMatthias Ringwald att_server = &hci_connection->att_server; 5171b7acd0dSMatthias Ringwald att_server->pairing_active = 0; 518cd45e970SMatthias Ringwald att_run_for_context(att_server, att_connection); 519760ad805SMatthias Ringwald break; 520760ad805SMatthias Ringwald 521760ad805SMatthias Ringwald // Authorization 5225611a760SMatthias Ringwald case SM_EVENT_AUTHORIZATION_RESULT: { 52318707219SMatthias Ringwald con_handle = sm_event_authorization_result_get_handle(packet); 52468591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 52568591e36SMatthias Ringwald if (!hci_connection) break; 52668591e36SMatthias Ringwald att_server = &hci_connection->att_server; 527388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 528b6df12b6SMatthias Ringwald att_connection->authorized = sm_event_authorization_result_get_authorization_result(packet); 5293f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 5303deb3ec6SMatthias Ringwald break; 5313deb3ec6SMatthias Ringwald } 5323deb3ec6SMatthias Ringwald default: 5333deb3ec6SMatthias Ringwald break; 5343deb3ec6SMatthias Ringwald } 53565b44ffdSMatthias Ringwald break; 53670dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 53770dca4d4SMatthias Ringwald case L2CAP_DATA_PACKET: 53868591e36SMatthias Ringwald hci_connections_get_iterator(&it); 53968591e36SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 540388fa7c4SMatthias Ringwald hci_connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 541388fa7c4SMatthias Ringwald att_server = &hci_connection->att_server; 54226b42dcfSMatthias Ringwald att_connection = &hci_connection->att_connection; 54368591e36SMatthias Ringwald if (att_server->l2cap_cid == channel) { 54426b42dcfSMatthias Ringwald att_server_handle_att_pdu(att_server, att_connection, packet, size); 54568591e36SMatthias Ringwald break; 54668591e36SMatthias Ringwald } 54768591e36SMatthias Ringwald } 54870dca4d4SMatthias Ringwald break; 54970dca4d4SMatthias Ringwald #endif 55041772f37SMatthias Ringwald 55165b44ffdSMatthias Ringwald default: 55265b44ffdSMatthias Ringwald break; 5533deb3ec6SMatthias Ringwald } 5543deb3ec6SMatthias Ringwald } 5553deb3ec6SMatthias Ringwald 5560f70c52eSMatthias Ringwald static uint8_t 5570f70c52eSMatthias Ringwald att_server_send_prepared(const att_server_t *att_server, const att_connection_t *att_connection, uint8_t *buffer, 5580f70c52eSMatthias Ringwald uint16_t size) { 559d415948cSMatthias Ringwald uint8_t status = ERROR_CODE_SUCCESS; 560d415948cSMatthias Ringwald switch (att_server->bearer_type) { 561d415948cSMatthias Ringwald case ATT_BEARER_UNENHANCED_LE: 562d415948cSMatthias Ringwald status = l2cap_send_prepared_connectionless(att_connection->con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); 563d415948cSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 564d415948cSMatthias Ringwald case ATT_BEARER_UNENHANCED_CLASSIC: 565d415948cSMatthias Ringwald status = l2cap_send_prepared(att_server->l2cap_cid, size); 566d415948cSMatthias Ringwald break; 567d415948cSMatthias Ringwald #endif 5680f70c52eSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT 5690f70c52eSMatthias Ringwald case ATT_BEARER_ENHANCED_LE: 5700f70c52eSMatthias Ringwald btstack_assert(buffer != NULL); 5710f70c52eSMatthias Ringwald status = l2cap_send(att_server->l2cap_cid, buffer, size); 5720f70c52eSMatthias Ringwald break; 5730f70c52eSMatthias Ringwald #endif 574d415948cSMatthias Ringwald default: 575d415948cSMatthias Ringwald btstack_unreachable(); 576d415948cSMatthias Ringwald break; 577d415948cSMatthias Ringwald } 578d415948cSMatthias Ringwald return status; 579d415948cSMatthias Ringwald } 580d415948cSMatthias Ringwald 5817a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 5823deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){ 58326567ee7SMatthias Ringwald att_server_t * att_server = NULL; 58426567ee7SMatthias Ringwald att_connection_t * att_connection = NULL; 58526567ee7SMatthias Ringwald bool found = att_server_connections_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION, &att_server, &att_connection); 5863deb3ec6SMatthias Ringwald 587*c2cbe117SMatthias Ringwald if (found == false){ 588*c2cbe117SMatthias Ringwald return; 589*c2cbe117SMatthias Ringwald } 590*c2cbe117SMatthias Ringwald 5913deb3ec6SMatthias Ringwald uint8_t hash_flipped[8]; 5929c80e4ccSMatthias Ringwald reverse_64(hash, hash_flipped); 59326567ee7SMatthias Ringwald if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8) != 0){ 5943deb3ec6SMatthias Ringwald log_info("ATT Signed Write, invalid signature"); 59503f53d81SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT 59603f53d81SMatthias Ringwald printf("ATT Signed Write, invalid signature\n"); 59703f53d81SMatthias Ringwald #endif 59818707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 5993deb3ec6SMatthias Ringwald return; 6003deb3ec6SMatthias Ringwald } 6013deb3ec6SMatthias Ringwald log_info("ATT Signed Write, valid signature"); 60203f53d81SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT 60303f53d81SMatthias Ringwald printf("ATT Signed Write, valid signature\n"); 60403f53d81SMatthias Ringwald #endif 6053deb3ec6SMatthias Ringwald 6063deb3ec6SMatthias Ringwald // update sequence number 60718707219SMatthias Ringwald uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12); 60818707219SMatthias Ringwald le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1); 60918707219SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 6103f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 6113deb3ec6SMatthias Ringwald } 6127a766ebfSMatthias Ringwald #endif 61388a48dd8SMatthias Ringwald 61418707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED 615b06f2579SMatthias Ringwald // pre: can send now 6169988bc8aSMatthias Ringwald // uses l2cap outgoing buffer if no eatt_buffer provided 617b06f2579SMatthias Ringwald // returns: 1 if packet was sent 6189988bc8aSMatthias Ringwald static int 6199988bc8aSMatthias Ringwald att_server_process_validated_request(att_server_t *att_server, att_connection_t *att_connection, uint8_t *eatt_buffer) { 620b06f2579SMatthias Ringwald 6219988bc8aSMatthias Ringwald uint8_t * att_response_buffer; 6229988bc8aSMatthias Ringwald if (eatt_buffer != NULL){ 6239988bc8aSMatthias Ringwald att_response_buffer = eatt_buffer; 6249988bc8aSMatthias Ringwald } else { 625b06f2579SMatthias Ringwald l2cap_reserve_packet_buffer(); 6269988bc8aSMatthias Ringwald att_response_buffer = l2cap_get_outgoing_buffer(); 6279988bc8aSMatthias Ringwald } 6289988bc8aSMatthias Ringwald 6297a293e22SMatthias Ringwald uint16_t att_response_size = 0; 6307a293e22SMatthias Ringwald // Send Error Response for MTU Request over connection-oriented channel 6317a293e22SMatthias Ringwald if ((att_server->bearer_type != ATT_BEARER_UNENHANCED_LE) && (att_server->request_buffer[0] == ATT_EXCHANGE_MTU_REQUEST)){ 6327a293e22SMatthias Ringwald att_response_size = 5; 6337a293e22SMatthias Ringwald att_response_buffer[0] = ATT_ERROR_RESPONSE; 6347a293e22SMatthias Ringwald att_response_buffer[1] = ATT_EXCHANGE_MTU_REQUEST; 6357a293e22SMatthias Ringwald att_response_buffer[2] = 0; 6367a293e22SMatthias Ringwald att_response_buffer[3] = 0; 6377a293e22SMatthias Ringwald att_response_buffer[4] = ATT_ERROR_REQUEST_NOT_SUPPORTED; 6387a293e22SMatthias Ringwald } else { 6397a293e22SMatthias Ringwald att_response_size = att_handle_request(att_connection, att_server->request_buffer, att_server->request_size, att_response_buffer); 6407a293e22SMatthias Ringwald } 641b06f2579SMatthias Ringwald 642beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE 643c1ab6cc1SMatthias Ringwald if ((att_response_size == ATT_READ_RESPONSE_PENDING) || (att_response_size == ATT_INTERNAL_WRITE_RESPONSE_PENDING)){ 644e404a688SMatthias Ringwald // update state 645beb20288SMatthias Ringwald att_server->state = ATT_SERVER_RESPONSE_PENDING; 646e404a688SMatthias Ringwald 64756c3a347SMatthias Ringwald // callback with handle ATT_READ_RESPONSE_PENDING for reads 64856c3a347SMatthias Ringwald if (att_response_size == ATT_READ_RESPONSE_PENDING){ 649b6df12b6SMatthias Ringwald att_server_client_read_callback(att_connection->con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0); 65056c3a347SMatthias Ringwald } 651e8e7403eSMatthias Ringwald 652e8e7403eSMatthias Ringwald // free reserved buffer 6539988bc8aSMatthias Ringwald if (eatt_buffer == NULL){ 654e8e7403eSMatthias Ringwald l2cap_release_packet_buffer(); 6559988bc8aSMatthias Ringwald } 656e8e7403eSMatthias Ringwald return 0; 657e404a688SMatthias Ringwald } 658e404a688SMatthias Ringwald #endif 659e404a688SMatthias Ringwald 660b06f2579SMatthias Ringwald // intercept "insufficient authorization" for authenticated connections to allow for user authorization 6614ea43905SMatthias Ringwald if ((att_response_size >= 4u) 662b06f2579SMatthias Ringwald && (att_response_buffer[0] == ATT_ERROR_RESPONSE) 663b06f2579SMatthias Ringwald && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION) 66438b893aaSMilanka Ringwald && (att_connection->authenticated != 0u)){ 665b06f2579SMatthias Ringwald 666b6df12b6SMatthias Ringwald switch (gap_authorization_state(att_connection->con_handle)){ 667b06f2579SMatthias Ringwald case AUTHORIZATION_UNKNOWN: 6689988bc8aSMatthias Ringwald if (eatt_buffer == NULL){ 669b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 6709988bc8aSMatthias Ringwald } 671b6df12b6SMatthias Ringwald sm_request_pairing(att_connection->con_handle); 672b06f2579SMatthias Ringwald return 0; 673b06f2579SMatthias Ringwald case AUTHORIZATION_PENDING: 6749988bc8aSMatthias Ringwald if (eatt_buffer == NULL){ 675b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 6769988bc8aSMatthias Ringwald } 677b06f2579SMatthias Ringwald return 0; 678b06f2579SMatthias Ringwald default: 679b06f2579SMatthias Ringwald break; 680b06f2579SMatthias Ringwald } 681b06f2579SMatthias Ringwald } 682b06f2579SMatthias Ringwald 68318707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 6844ea43905SMatthias Ringwald if (att_response_size == 0u) { 6859988bc8aSMatthias Ringwald if (eatt_buffer == NULL){ 686b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 6879988bc8aSMatthias Ringwald } 688b06f2579SMatthias Ringwald return 0; 689b06f2579SMatthias Ringwald } 690b06f2579SMatthias Ringwald 6919988bc8aSMatthias Ringwald (void) att_server_send_prepared(att_server, att_connection, eatt_buffer, att_response_size); 692b06f2579SMatthias Ringwald 693b06f2579SMatthias Ringwald // notify client about MTU exchange result 694b06f2579SMatthias Ringwald if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){ 695b6df12b6SMatthias Ringwald att_emit_mtu_event(att_connection->con_handle, att_connection->mtu); 696b06f2579SMatthias Ringwald } 697b06f2579SMatthias Ringwald return 1; 698b06f2579SMatthias Ringwald } 699b06f2579SMatthias Ringwald 700beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE 7019816429dSMatthias Ringwald uint8_t att_server_response_ready(hci_con_handle_t con_handle){ 70268591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 70368591e36SMatthias Ringwald if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 70468591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 7053f9b77dbSMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 706beb20288SMatthias Ringwald if (att_server->state != ATT_SERVER_RESPONSE_PENDING) return ERROR_CODE_COMMAND_DISALLOWED; 707e404a688SMatthias Ringwald 708e404a688SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 7093f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 710e404a688SMatthias Ringwald return ERROR_CODE_SUCCESS; 711e404a688SMatthias Ringwald } 712e404a688SMatthias Ringwald #endif 713e404a688SMatthias Ringwald 714cd45e970SMatthias Ringwald static void att_run_for_context(att_server_t * att_server, att_connection_t * att_connection){ 71518707219SMatthias Ringwald switch (att_server->state){ 7163deb3ec6SMatthias Ringwald case ATT_SERVER_REQUEST_RECEIVED: 71723079b39SMatthias Ringwald switch (att_server->bearer_type){ 71823079b39SMatthias Ringwald case ATT_BEARER_UNENHANCED_LE: 7190234fbbcSMatthias Ringwald // wait until re-encryption as central is complete 72023079b39SMatthias Ringwald if (gap_reconnect_security_setup_active(att_connection->con_handle)) { 72123079b39SMatthias Ringwald return; 72223079b39SMatthias Ringwald }; 72323079b39SMatthias Ringwald break; 7242954c397SMatthias Ringwald #if defined(ENABLE_GATT_OVER_CLASSIC) || defined (ENABLE_GATT_OVER_EATT) 72523079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 72623079b39SMatthias Ringwald case ATT_BEARER_UNENHANCED_CLASSIC: 7272954c397SMatthias Ringwald /* fall through */ 7282954c397SMatthias Ringwald #endif 7292954c397SMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT 7302954c397SMatthias Ringwald case ATT_BEARER_ENHANCED_LE: 7312954c397SMatthias Ringwald #endif 73223079b39SMatthias Ringwald // ok 73323079b39SMatthias Ringwald break; 73423079b39SMatthias Ringwald #endif 73523079b39SMatthias Ringwald default: 73623079b39SMatthias Ringwald btstack_unreachable(); 73723079b39SMatthias Ringwald break; 73848364364SMatthias Ringwald } 7390234fbbcSMatthias Ringwald 740760ad805SMatthias Ringwald // wait until pairing is complete 741760ad805SMatthias Ringwald if (att_server->pairing_active) break; 742760ad805SMatthias Ringwald 7437a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 74418707219SMatthias Ringwald if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){ 7453deb3ec6SMatthias Ringwald log_info("ATT Signed Write!"); 7463deb3ec6SMatthias Ringwald if (!sm_cmac_ready()) { 7473deb3ec6SMatthias Ringwald log_info("ATT Signed Write, sm_cmac engine not ready. Abort"); 74818707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 7493deb3ec6SMatthias Ringwald return; 7503deb3ec6SMatthias Ringwald } 75118707219SMatthias Ringwald if (att_server->request_size < (3 + 12)) { 7523deb3ec6SMatthias Ringwald log_info("ATT Signed Write, request to short. Abort."); 75318707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 7543deb3ec6SMatthias Ringwald return; 7553deb3ec6SMatthias Ringwald } 75618707219SMatthias Ringwald if (att_server->ir_lookup_active){ 7573deb3ec6SMatthias Ringwald return; 7583deb3ec6SMatthias Ringwald } 75918707219SMatthias Ringwald if (att_server->ir_le_device_db_index < 0){ 7603deb3ec6SMatthias Ringwald log_info("ATT Signed Write, CSRK not available"); 76118707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 7623deb3ec6SMatthias Ringwald return; 7633deb3ec6SMatthias Ringwald } 7643deb3ec6SMatthias Ringwald 7653deb3ec6SMatthias Ringwald // check counter 76618707219SMatthias Ringwald uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12); 76718707219SMatthias Ringwald uint32_t counter_db = le_device_db_remote_counter_get(att_server->ir_le_device_db_index); 7683deb3ec6SMatthias Ringwald log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet); 7693deb3ec6SMatthias Ringwald if (counter_packet < counter_db){ 7703deb3ec6SMatthias Ringwald log_info("ATT Signed Write, db reports higher counter, abort"); 77118707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 7723deb3ec6SMatthias Ringwald return; 7733deb3ec6SMatthias Ringwald } 7743deb3ec6SMatthias Ringwald 7753deb3ec6SMatthias Ringwald // signature is { sequence counter, secure hash } 7763deb3ec6SMatthias Ringwald sm_key_t csrk; 77718707219SMatthias Ringwald le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk); 77818707219SMatthias Ringwald att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION; 7793deb3ec6SMatthias Ringwald log_info("Orig Signature: "); 78018707219SMatthias Ringwald log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8); 78118707219SMatthias Ringwald uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1); 78218707219SMatthias 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); 7833deb3ec6SMatthias Ringwald return; 7843deb3ec6SMatthias Ringwald } 7857a766ebfSMatthias Ringwald #endif 786b06f2579SMatthias Ringwald // move on 78718707219SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 7883f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 789b06f2579SMatthias Ringwald break; 79088a48dd8SMatthias Ringwald 7913deb3ec6SMatthias Ringwald default: 7923deb3ec6SMatthias Ringwald break; 7933deb3ec6SMatthias Ringwald } 7943deb3ec6SMatthias Ringwald } 7953deb3ec6SMatthias Ringwald 7961979f09cSMatthias Ringwald static bool att_server_data_ready_for_phase(att_server_t * att_server, att_server_run_phase_t phase){ 79790f03c80SMatthias Ringwald switch (phase){ 79890f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_1_REQUESTS: 79990f03c80SMatthias Ringwald return att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 80090f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_2_INDICATIONS: 80138b893aaSMilanka Ringwald return (!btstack_linked_list_empty(&att_server->indication_requests) && (att_server->value_indication_handle == 0u)); 80290f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS: 80390f03c80SMatthias Ringwald return (!btstack_linked_list_empty(&att_server->notification_requests)); 8047bbeb3adSMilanka Ringwald default: 8057bbeb3adSMilanka Ringwald btstack_assert(false); 8061979f09cSMatthias Ringwald return false; 80790f03c80SMatthias Ringwald } 8087bbeb3adSMilanka Ringwald } 80990f03c80SMatthias Ringwald 8109d6c0193SMatthias 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){ 81190f03c80SMatthias Ringwald btstack_context_callback_registration_t * client; 81290f03c80SMatthias Ringwald switch (phase){ 81390f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_1_REQUESTS: 8140f70c52eSMatthias Ringwald att_server_process_validated_request(att_server, att_connection, NULL); 81590f03c80SMatthias Ringwald break; 81690f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_2_INDICATIONS: 81790f03c80SMatthias Ringwald client = (btstack_context_callback_registration_t*) att_server->indication_requests; 81890f03c80SMatthias Ringwald btstack_linked_list_remove(&att_server->indication_requests, (btstack_linked_item_t *) client); 81990f03c80SMatthias Ringwald client->callback(client->context); 82090f03c80SMatthias Ringwald break; 82190f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS: 82290f03c80SMatthias Ringwald client = (btstack_context_callback_registration_t*) att_server->notification_requests; 82390f03c80SMatthias Ringwald btstack_linked_list_remove(&att_server->notification_requests, (btstack_linked_item_t *) client); 82490f03c80SMatthias Ringwald client->callback(client->context); 82590f03c80SMatthias Ringwald break; 8267bbeb3adSMilanka Ringwald default: 8277bbeb3adSMilanka Ringwald btstack_assert(false); 8287bbeb3adSMilanka Ringwald break; 82990f03c80SMatthias Ringwald } 83090f03c80SMatthias Ringwald } 83190f03c80SMatthias Ringwald 8327eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){ 833b06f2579SMatthias Ringwald 8346438547aSMatthias Ringwald hci_con_handle_t last_send_con_handle = HCI_CON_HANDLE_INVALID; 83568591e36SMatthias Ringwald hci_connection_t * request_hci_connection = NULL; 8361979f09cSMatthias Ringwald bool can_send_now = true; 83738b893aaSMilanka Ringwald uint8_t phase_index; 8386438547aSMatthias Ringwald 8394395a000SMatthias Ringwald for (phase_index = ATT_SERVER_RUN_PHASE_1_REQUESTS; phase_index <= ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS; phase_index++){ 8404395a000SMatthias Ringwald att_server_run_phase_t phase = (att_server_run_phase_t) phase_index; 8416438547aSMatthias Ringwald hci_con_handle_t skip_connections_until = att_server_last_can_send_now; 842ff3cc4a5SMatthias Ringwald while (true){ 8436438547aSMatthias Ringwald btstack_linked_list_iterator_t it; 84418707219SMatthias Ringwald hci_connections_get_iterator(&it); 84518707219SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 84618707219SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 84718707219SMatthias Ringwald att_server_t * att_server = &connection->att_server; 848388fa7c4SMatthias Ringwald att_connection_t * att_connection = &connection->att_connection; 8496438547aSMatthias Ringwald 8501979f09cSMatthias Ringwald bool data_ready = att_server_data_ready_for_phase(att_server, phase); 8516438547aSMatthias Ringwald 852b6df12b6SMatthias 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); 8536438547aSMatthias Ringwald 8546438547aSMatthias Ringwald // skip until last sender found (which is also skipped) 8556438547aSMatthias Ringwald if (skip_connections_until != HCI_CON_HANDLE_INVALID){ 85668591e36SMatthias Ringwald if (data_ready && (request_hci_connection == NULL)){ 85768591e36SMatthias Ringwald request_hci_connection = connection; 8586438547aSMatthias Ringwald } 859b6df12b6SMatthias Ringwald if (skip_connections_until == att_connection->con_handle){ 8606438547aSMatthias Ringwald skip_connections_until = HCI_CON_HANDLE_INVALID; 8616438547aSMatthias Ringwald } 8626438547aSMatthias Ringwald continue; 8636438547aSMatthias Ringwald }; 8646438547aSMatthias Ringwald 86590f03c80SMatthias Ringwald if (data_ready){ 866969a5bbaSMatthias Ringwald if (can_send_now){ 8679d6c0193SMatthias Ringwald att_server_trigger_send_for_phase(att_server, att_connection, phase); 868b6df12b6SMatthias Ringwald last_send_con_handle = att_connection->con_handle; 86937a8059dSMatthias Ringwald can_send_now = att_server_can_send_packet(att_server, att_connection); 87090f03c80SMatthias Ringwald data_ready = att_server_data_ready_for_phase(att_server, phase); 87168591e36SMatthias Ringwald if (data_ready && (request_hci_connection == NULL)){ 87268591e36SMatthias Ringwald request_hci_connection = connection; 873cbbb12d9SMatthias Ringwald } 874cbbb12d9SMatthias Ringwald } else { 87568591e36SMatthias Ringwald request_hci_connection = connection; 876f29a88d8SMatthias Ringwald break; 877cbbb12d9SMatthias Ringwald } 878cbbb12d9SMatthias Ringwald } 8793deb3ec6SMatthias Ringwald } 8803deb3ec6SMatthias Ringwald 8816438547aSMatthias Ringwald // stop skipping (handles disconnect by last send connection) 8826438547aSMatthias Ringwald skip_connections_until = HCI_CON_HANDLE_INVALID; 8836438547aSMatthias Ringwald 8843551936eSMatthias Ringwald // Exit loop, if we cannot send 885969a5bbaSMatthias Ringwald if (!can_send_now) break; 886969a5bbaSMatthias Ringwald 887969a5bbaSMatthias Ringwald // Exit loop, if we can send but there are also no further request 88868591e36SMatthias Ringwald if (request_hci_connection == NULL) break; 889969a5bbaSMatthias Ringwald 890969a5bbaSMatthias Ringwald // Finally, if we still can send and there are requests, just try again 89168591e36SMatthias Ringwald request_hci_connection = NULL; 892969a5bbaSMatthias Ringwald } 8936438547aSMatthias Ringwald // update last send con handle for round robin 8946438547aSMatthias Ringwald if (last_send_con_handle != HCI_CON_HANDLE_INVALID){ 8956438547aSMatthias Ringwald att_server_last_can_send_now = last_send_con_handle; 8966438547aSMatthias Ringwald } 8973551936eSMatthias Ringwald } 898969a5bbaSMatthias Ringwald 89968591e36SMatthias Ringwald if (request_hci_connection == NULL) return; 9003f9b77dbSMatthias Ringwald 9013f9b77dbSMatthias Ringwald att_server_t * att_server = &request_hci_connection->att_server; 9023f9b77dbSMatthias Ringwald att_connection_t * att_connection = &request_hci_connection->att_connection; 9033f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 904969a5bbaSMatthias Ringwald } 905969a5bbaSMatthias Ringwald 90626b42dcfSMatthias Ringwald static void att_server_handle_att_pdu(att_server_t * att_server, att_connection_t * att_connection, uint8_t * packet, uint16_t size){ 90718707219SMatthias Ringwald 908838bd521SMatthias Ringwald uint8_t opcode = packet[0u]; 90938b893aaSMilanka Ringwald uint8_t method = opcode & 0x03fu; 910ed68ae9dSMilanka Ringwald bool invalid = method > ATT_MULTIPLE_HANDLE_VALUE_NTF; 91138b893aaSMilanka Ringwald bool command = (opcode & 0x40u) != 0u; 912838bd521SMatthias Ringwald 913838bd521SMatthias Ringwald // ignore invalid commands 914838bd521SMatthias Ringwald if (invalid && command){ 915838bd521SMatthias Ringwald return; 916838bd521SMatthias Ringwald } 917838bd521SMatthias Ringwald 9183deb3ec6SMatthias Ringwald // handle value indication confirms 91938b893aaSMilanka Ringwald if ((opcode == ATT_HANDLE_VALUE_CONFIRMATION) && (att_server->value_indication_handle != 0u)){ 92018707219SMatthias Ringwald btstack_run_loop_remove_timer(&att_server->value_indication_timer); 92118707219SMatthias Ringwald uint16_t att_handle = att_server->value_indication_handle; 92238b893aaSMilanka Ringwald att_server->value_indication_handle = 0u; 92338b893aaSMilanka Ringwald att_handle_value_indication_notify_client(0u, att_connection->con_handle, att_handle); 9243f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 9253deb3ec6SMatthias Ringwald return; 9263deb3ec6SMatthias Ringwald } 9273deb3ec6SMatthias Ringwald 9286428eb5aSMatthias Ringwald // directly process command 9296428eb5aSMatthias Ringwald // note: signed write cannot be handled directly as authentication needs to be verified 930838bd521SMatthias Ringwald if (opcode == ATT_WRITE_COMMAND){ 931b6df12b6SMatthias Ringwald att_handle_request(att_connection, packet, size, NULL); 9326428eb5aSMatthias Ringwald return; 9336428eb5aSMatthias Ringwald } 9346428eb5aSMatthias Ringwald 9353deb3ec6SMatthias Ringwald // check size 93618707219SMatthias Ringwald if (size > sizeof(att_server->request_buffer)) { 937e0463bdcSMatthias 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)); 9386428eb5aSMatthias Ringwald return; 9396428eb5aSMatthias Ringwald } 9403deb3ec6SMatthias Ringwald 941e0463bdcSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 942e0463bdcSMatthias Ringwald // abort signed write validation if a new request comes in (but finish previous signed write if possible) 943e0463bdcSMatthias Ringwald if (att_server->state == ATT_SERVER_W4_SIGNED_WRITE_VALIDATION){ 944e0463bdcSMatthias Ringwald if (packet[0] == ATT_SIGNED_WRITE_COMMAND){ 945e0463bdcSMatthias Ringwald log_info("skip new signed write request as previous is in validation"); 946e0463bdcSMatthias Ringwald return; 947e0463bdcSMatthias Ringwald } else { 948e0463bdcSMatthias Ringwald log_info("abort signed write validation to process new request"); 949e0463bdcSMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 950e0463bdcSMatthias Ringwald } 951e0463bdcSMatthias Ringwald } 952e0463bdcSMatthias Ringwald #endif 9533deb3ec6SMatthias Ringwald // last request still in processing? 95418707219SMatthias Ringwald if (att_server->state != ATT_SERVER_IDLE){ 955e0463bdcSMatthias Ringwald log_info("skip att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state); 9566428eb5aSMatthias Ringwald return; 9576428eb5aSMatthias Ringwald } 9583deb3ec6SMatthias Ringwald 9593deb3ec6SMatthias Ringwald // store request 96018707219SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED; 96118707219SMatthias Ringwald att_server->request_size = size; 9626535961aSMatthias Ringwald (void)memcpy(att_server->request_buffer, packet, size); 9633deb3ec6SMatthias Ringwald 964cd45e970SMatthias Ringwald att_run_for_context(att_server, att_connection); 96541772f37SMatthias Ringwald } 96641772f37SMatthias Ringwald 96741772f37SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){ 96868591e36SMatthias Ringwald hci_connection_t * hci_connection; 969b6df12b6SMatthias Ringwald att_connection_t * att_connection; 97026b42dcfSMatthias Ringwald att_server_t * att_server; 97141772f37SMatthias Ringwald 97241772f37SMatthias Ringwald switch (packet_type){ 97341772f37SMatthias Ringwald case HCI_EVENT_PACKET: 97441772f37SMatthias Ringwald switch (packet[0]){ 97541772f37SMatthias Ringwald case L2CAP_EVENT_CAN_SEND_NOW: 97641772f37SMatthias Ringwald att_server_handle_can_send_now(); 97741772f37SMatthias Ringwald break; 97841772f37SMatthias Ringwald case ATT_EVENT_MTU_EXCHANGE_COMPLETE: 97941772f37SMatthias Ringwald // GATT client has negotiated the mtu for this connection 98068591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(handle); 98168591e36SMatthias Ringwald if (!hci_connection) break; 982388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 983b6df12b6SMatthias Ringwald att_connection->mtu = little_endian_read_16(packet, 4); 98441772f37SMatthias Ringwald break; 98541772f37SMatthias Ringwald default: 98641772f37SMatthias Ringwald break; 98741772f37SMatthias Ringwald } 98841772f37SMatthias Ringwald break; 98941772f37SMatthias Ringwald 99041772f37SMatthias Ringwald case ATT_DATA_PACKET: 99141772f37SMatthias Ringwald log_debug("ATT Packet, handle 0x%04x", handle); 99268591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(handle); 99368591e36SMatthias Ringwald if (!hci_connection) break; 99441772f37SMatthias Ringwald 99526b42dcfSMatthias Ringwald att_server = &hci_connection->att_server; 99626b42dcfSMatthias Ringwald att_connection = &hci_connection->att_connection; 99726b42dcfSMatthias Ringwald att_server_handle_att_pdu(att_server, att_connection, packet, size); 998372d62c4SMatthias Ringwald break; 9997bbeb3adSMilanka Ringwald 10007bbeb3adSMilanka Ringwald default: 10017bbeb3adSMilanka Ringwald break; 1002372d62c4SMatthias Ringwald } 10033deb3ec6SMatthias Ringwald } 10043deb3ec6SMatthias Ringwald 1005bf78aac6SMatthias Ringwald // --------------------- 1006bf78aac6SMatthias Ringwald // persistent CCC writes 1007bf78aac6SMatthias Ringwald static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){ 10084ea43905SMatthias Ringwald return ('B' << 24u) | ('T' << 16u) | ('C' << 8u) | index; 1009bf78aac6SMatthias Ringwald } 1010bf78aac6SMatthias Ringwald 1011bf78aac6SMatthias Ringwald static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){ 1012bf78aac6SMatthias Ringwald // lookup att_server instance 101368591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 101468591e36SMatthias Ringwald if (!hci_connection) return; 101568591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 1016bf78aac6SMatthias Ringwald int le_device_index = att_server->ir_le_device_db_index; 1017bf78aac6SMatthias 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); 10186a540790SMatthias Ringwald 1019bf78aac6SMatthias Ringwald // check if bonded 1020bf78aac6SMatthias Ringwald if (le_device_index < 0) return; 10216a540790SMatthias Ringwald 1022bf78aac6SMatthias Ringwald // get btstack_tlv 1023bf78aac6SMatthias Ringwald const btstack_tlv_t * tlv_impl = NULL; 1024bf78aac6SMatthias Ringwald void * tlv_context; 1025bf78aac6SMatthias Ringwald btstack_tlv_get_instance(&tlv_impl, &tlv_context); 1026bf78aac6SMatthias Ringwald if (!tlv_impl) return; 10276a540790SMatthias Ringwald 1028bf78aac6SMatthias Ringwald // update ccc tag 1029bf78aac6SMatthias Ringwald int index; 10306a540790SMatthias Ringwald uint32_t highest_seq_nr = 0; 10316a540790SMatthias Ringwald uint32_t lowest_seq_nr = 0; 10326a540790SMatthias Ringwald uint32_t tag_for_lowest_seq_nr = 0; 10336a540790SMatthias Ringwald uint32_t tag_for_empty = 0; 10346a540790SMatthias Ringwald persistent_ccc_entry_t entry; 10356afb9acaSMatthias Ringwald for (index=0; index<NVN_NUM_GATT_SERVER_CCC; index++){ 1036bf78aac6SMatthias Ringwald uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 10376a540790SMatthias Ringwald int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 10386a540790SMatthias Ringwald 10396a540790SMatthias Ringwald // empty/invalid tag 10406a540790SMatthias Ringwald if (len != sizeof(persistent_ccc_entry_t)){ 10416a540790SMatthias Ringwald tag_for_empty = tag; 1042bf78aac6SMatthias Ringwald continue; 1043bf78aac6SMatthias Ringwald } 10446a540790SMatthias Ringwald // update highest seq nr 10456a540790SMatthias Ringwald if (entry.seq_nr > highest_seq_nr){ 10466a540790SMatthias Ringwald highest_seq_nr = entry.seq_nr; 10476a540790SMatthias Ringwald } 10486a540790SMatthias Ringwald // find entry with lowest seq nr 10494ea43905SMatthias Ringwald if ((tag_for_lowest_seq_nr == 0u) || (entry.seq_nr < lowest_seq_nr)){ 10506a540790SMatthias Ringwald tag_for_lowest_seq_nr = tag; 10516a540790SMatthias Ringwald lowest_seq_nr = entry.seq_nr; 10526a540790SMatthias Ringwald } 10536a540790SMatthias Ringwald 10546a540790SMatthias Ringwald if (entry.device_index != le_device_index) continue; 10556a540790SMatthias Ringwald if (entry.att_handle != att_handle) continue; 10566a540790SMatthias Ringwald 10576a540790SMatthias Ringwald // found matching entry 10589305033eSMatthias Ringwald if (value != 0){ 1059bf78aac6SMatthias Ringwald // update 10606a540790SMatthias Ringwald if (entry.value == value) { 1061760ad805SMatthias Ringwald log_info("CCC Index %u: Up-to-date", index); 1062760ad805SMatthias Ringwald return; 1063760ad805SMatthias Ringwald } 1064348fc806SMatthias Ringwald entry.value = (uint8_t) value; 10654ea43905SMatthias Ringwald entry.seq_nr = highest_seq_nr + 1u; 1066760ad805SMatthias Ringwald log_info("CCC Index %u: Store", index); 10670fe46bc1SMatthias Ringwald int result = tlv_impl->store_tag(tlv_context, tag, (const uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 10680fe46bc1SMatthias Ringwald if (result != 0){ 10690fe46bc1SMatthias Ringwald log_error("Store tag index %u failed", index); 10700fe46bc1SMatthias Ringwald } 107101ac2008SMatthias Ringwald } else { 107201ac2008SMatthias Ringwald // delete 1073760ad805SMatthias Ringwald log_info("CCC Index %u: Delete", index); 107401ac2008SMatthias Ringwald tlv_impl->delete_tag(tlv_context, tag); 107501ac2008SMatthias Ringwald } 1076bf78aac6SMatthias Ringwald return; 1077bf78aac6SMatthias Ringwald } 10786a540790SMatthias Ringwald 10790fe46bc1SMatthias Ringwald log_info("tag_for_empty %"PRIx32", tag_for_lowest_seq_nr %"PRIx32, tag_for_empty, tag_for_lowest_seq_nr); 10806a540790SMatthias Ringwald 10814ea43905SMatthias Ringwald if (value == 0u){ 10826a540790SMatthias Ringwald // done 10836a540790SMatthias Ringwald return; 10846a540790SMatthias Ringwald } 10856a540790SMatthias Ringwald 108638b893aaSMilanka Ringwald uint32_t tag_to_use = 0u; 108738b893aaSMilanka Ringwald if (tag_for_empty != 0u){ 10886a540790SMatthias Ringwald tag_to_use = tag_for_empty; 10896a540790SMatthias Ringwald } else if (tag_for_lowest_seq_nr){ 10906a540790SMatthias Ringwald tag_to_use = tag_for_lowest_seq_nr; 10916a540790SMatthias Ringwald } else { 10926a540790SMatthias Ringwald // should not happen 10936a540790SMatthias Ringwald return; 10946a540790SMatthias Ringwald } 1095bf78aac6SMatthias Ringwald // store ccc tag 10964ea43905SMatthias Ringwald entry.seq_nr = highest_seq_nr + 1u; 10976a540790SMatthias Ringwald entry.device_index = le_device_index; 10986a540790SMatthias Ringwald entry.att_handle = att_handle; 1099348fc806SMatthias Ringwald entry.value = (uint8_t) value; 11000fe46bc1SMatthias Ringwald int result = tlv_impl->store_tag(tlv_context, tag_to_use, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 11010fe46bc1SMatthias Ringwald if (result != 0){ 11020fe46bc1SMatthias Ringwald log_error("Store tag index %u failed", index); 11030fe46bc1SMatthias Ringwald } 1104bf78aac6SMatthias Ringwald } 1105bf78aac6SMatthias Ringwald 1106fe3d0cbaSMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server){ 1107760ad805SMatthias Ringwald int le_device_index = att_server->ir_le_device_db_index; 1108760ad805SMatthias Ringwald log_info("Clear CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index); 1109760ad805SMatthias Ringwald // check if bonded 1110760ad805SMatthias Ringwald if (le_device_index < 0) return; 1111760ad805SMatthias Ringwald // get btstack_tlv 1112760ad805SMatthias Ringwald const btstack_tlv_t * tlv_impl = NULL; 1113760ad805SMatthias Ringwald void * tlv_context; 1114760ad805SMatthias Ringwald btstack_tlv_get_instance(&tlv_impl, &tlv_context); 1115760ad805SMatthias Ringwald if (!tlv_impl) return; 1116760ad805SMatthias Ringwald // get all ccc tag 1117760ad805SMatthias Ringwald int index; 11186a540790SMatthias Ringwald persistent_ccc_entry_t entry; 11196afb9acaSMatthias Ringwald for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){ 1120760ad805SMatthias Ringwald uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 11216a540790SMatthias Ringwald int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 11226a540790SMatthias Ringwald if (len != sizeof(persistent_ccc_entry_t)) continue; 11236a540790SMatthias Ringwald if (entry.device_index != le_device_index) continue; 1124760ad805SMatthias Ringwald // delete entry 1125760ad805SMatthias Ringwald log_info("CCC Index %u: Delete", index); 1126760ad805SMatthias Ringwald tlv_impl->delete_tag(tlv_context, tag); 1127760ad805SMatthias Ringwald } 1128760ad805SMatthias Ringwald } 1129760ad805SMatthias Ringwald 11309a1f55eaSMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server, att_connection_t * att_connection){ 113101ac2008SMatthias Ringwald int le_device_index = att_server->ir_le_device_db_index; 113201ac2008SMatthias Ringwald log_info("Restore CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index); 11331b7acd0dSMatthias Ringwald // check if bonded 11341b7acd0dSMatthias Ringwald if (le_device_index < 0) return; 113501ac2008SMatthias Ringwald // get btstack_tlv 113601ac2008SMatthias Ringwald const btstack_tlv_t * tlv_impl = NULL; 113701ac2008SMatthias Ringwald void * tlv_context; 113801ac2008SMatthias Ringwald btstack_tlv_get_instance(&tlv_impl, &tlv_context); 113901ac2008SMatthias Ringwald if (!tlv_impl) return; 114001ac2008SMatthias Ringwald // get all ccc tag 114101ac2008SMatthias Ringwald int index; 11426a540790SMatthias Ringwald persistent_ccc_entry_t entry; 11436afb9acaSMatthias Ringwald for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){ 114401ac2008SMatthias Ringwald uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 11456a540790SMatthias Ringwald int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 11466a540790SMatthias Ringwald if (len != sizeof(persistent_ccc_entry_t)) continue; 11476a540790SMatthias Ringwald if (entry.device_index != le_device_index) continue; 114801ac2008SMatthias Ringwald // simulate write callback 11496a540790SMatthias Ringwald uint16_t attribute_handle = entry.att_handle; 115001ac2008SMatthias Ringwald uint8_t value[2]; 11516a540790SMatthias Ringwald little_endian_store_16(value, 0, entry.value); 115201ac2008SMatthias Ringwald att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle); 115301ac2008SMatthias Ringwald if (!callback) continue; 11546a540790SMatthias Ringwald log_info("CCC Index %u: Set Attribute handle 0x%04x to value 0x%04x", index, attribute_handle, entry.value ); 1155b6df12b6SMatthias Ringwald (*callback)(att_connection->con_handle, attribute_handle, ATT_TRANSACTION_MODE_NONE, 0, value, sizeof(value)); 115601ac2008SMatthias Ringwald } 115701ac2008SMatthias Ringwald } 115801ac2008SMatthias Ringwald 1159bf78aac6SMatthias Ringwald // persistent CCC writes 1160bf78aac6SMatthias Ringwald // --------------------- 11613d71c7a4SMatthias Ringwald 11623d71c7a4SMatthias Ringwald // gatt service management 11633d71c7a4SMatthias Ringwald static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){ 11643d71c7a4SMatthias Ringwald btstack_linked_list_iterator_t it; 11653d71c7a4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 11663d71c7a4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 11673d71c7a4SMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 11683d71c7a4SMatthias Ringwald if (handler->start_handle > handle) continue; 11693d71c7a4SMatthias Ringwald if (handler->end_handle < handle) continue; 11703d71c7a4SMatthias Ringwald return handler; 11713d71c7a4SMatthias Ringwald } 11723d71c7a4SMatthias Ringwald return NULL; 11733d71c7a4SMatthias Ringwald } 11743d71c7a4SMatthias Ringwald static att_read_callback_t att_server_read_callback_for_handle(uint16_t handle){ 11753d71c7a4SMatthias Ringwald att_service_handler_t * handler = att_service_handler_for_handle(handle); 11769305033eSMatthias Ringwald if (handler != NULL) return handler->read_callback; 11773d71c7a4SMatthias Ringwald return att_server_client_read_callback; 11783d71c7a4SMatthias Ringwald } 11793d71c7a4SMatthias Ringwald 11803d71c7a4SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){ 11813d71c7a4SMatthias Ringwald att_service_handler_t * handler = att_service_handler_for_handle(handle); 11829305033eSMatthias Ringwald if (handler != NULL) return handler->write_callback; 11833d71c7a4SMatthias Ringwald return att_server_client_write_callback; 11843d71c7a4SMatthias Ringwald } 11853d71c7a4SMatthias Ringwald 11865d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle){ 11875d07396bSMatthias Ringwald att_service_handler_t * handler = att_service_handler_for_handle(handle); 11889305033eSMatthias Ringwald if (handler != NULL) return handler->packet_handler; 11895d07396bSMatthias Ringwald return att_client_packet_handler; 11905d07396bSMatthias Ringwald } 11915d07396bSMatthias Ringwald 11923d71c7a4SMatthias Ringwald static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){ 11933d71c7a4SMatthias Ringwald // notify all callbacks 11943d71c7a4SMatthias Ringwald btstack_linked_list_iterator_t it; 11953d71c7a4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 11963d71c7a4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 11973d71c7a4SMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 11983d71c7a4SMatthias Ringwald if (!handler->write_callback) continue; 11993d71c7a4SMatthias Ringwald (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0); 12003d71c7a4SMatthias Ringwald } 12013d71c7a4SMatthias Ringwald if (!att_server_client_write_callback) return; 12023d71c7a4SMatthias Ringwald (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0); 12033d71c7a4SMatthias Ringwald } 12043d71c7a4SMatthias Ringwald 12053d71c7a4SMatthias Ringwald // returns first reported error or 0 12063d71c7a4SMatthias Ringwald static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){ 12073d71c7a4SMatthias Ringwald btstack_linked_list_iterator_t it; 12083d71c7a4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 12093d71c7a4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 12103d71c7a4SMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 12113d71c7a4SMatthias Ringwald if (!handler->write_callback) continue; 12123d71c7a4SMatthias Ringwald uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0); 121338b893aaSMilanka Ringwald if (error_code != 0u) return error_code; 12143d71c7a4SMatthias Ringwald } 12153d71c7a4SMatthias Ringwald if (!att_server_client_write_callback) return 0; 12163d71c7a4SMatthias Ringwald return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0); 12173d71c7a4SMatthias Ringwald } 12183d71c7a4SMatthias Ringwald 12193d71c7a4SMatthias 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){ 12203d71c7a4SMatthias Ringwald att_read_callback_t callback = att_server_read_callback_for_handle(attribute_handle); 122101ac2008SMatthias Ringwald if (!callback) return 0; 12223d71c7a4SMatthias Ringwald return (*callback)(con_handle, attribute_handle, offset, buffer, buffer_size); 12233d71c7a4SMatthias Ringwald } 12243d71c7a4SMatthias Ringwald 12253d71c7a4SMatthias 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){ 12263d71c7a4SMatthias Ringwald switch (transaction_mode){ 12273d71c7a4SMatthias Ringwald case ATT_TRANSACTION_MODE_VALIDATE: 12283d71c7a4SMatthias Ringwald return att_validate_prepared_write(con_handle); 12293d71c7a4SMatthias Ringwald case ATT_TRANSACTION_MODE_EXECUTE: 12303d71c7a4SMatthias Ringwald case ATT_TRANSACTION_MODE_CANCEL: 12313d71c7a4SMatthias Ringwald att_notify_write_callbacks(con_handle, transaction_mode); 12323d71c7a4SMatthias Ringwald return 0; 12333d71c7a4SMatthias Ringwald default: 12343d71c7a4SMatthias Ringwald break; 12353d71c7a4SMatthias Ringwald } 12363d71c7a4SMatthias Ringwald 1237bf78aac6SMatthias Ringwald // track CCC writes 12384ea43905SMatthias Ringwald if (att_is_persistent_ccc(attribute_handle) && (offset == 0u) && (buffer_size == 2u)){ 1239bf78aac6SMatthias Ringwald att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0)); 1240bf78aac6SMatthias Ringwald } 1241bf78aac6SMatthias Ringwald 124201ac2008SMatthias Ringwald att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle); 124301ac2008SMatthias Ringwald if (!callback) return 0; 12443d71c7a4SMatthias Ringwald return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size); 12453d71c7a4SMatthias Ringwald } 12463d71c7a4SMatthias Ringwald 12473d71c7a4SMatthias Ringwald /** 12483d71c7a4SMatthias Ringwald * @brief register read/write callbacks for specific handle range 12493d71c7a4SMatthias Ringwald * @param att_service_handler_t 12503d71c7a4SMatthias Ringwald */ 12513d71c7a4SMatthias Ringwald void att_server_register_service_handler(att_service_handler_t * handler){ 1252ec8f5414SMilanka Ringwald bool att_server_registered = false; 1253ec8f5414SMilanka Ringwald if (att_service_handler_for_handle(handler->start_handle)){ 1254ec8f5414SMilanka Ringwald att_server_registered = true; 1255ec8f5414SMilanka Ringwald } 1256ec8f5414SMilanka Ringwald 1257ec8f5414SMilanka Ringwald if (att_service_handler_for_handle(handler->end_handle)){ 1258ec8f5414SMilanka Ringwald att_server_registered = true; 1259ec8f5414SMilanka Ringwald } 1260ec8f5414SMilanka Ringwald 1261ec8f5414SMilanka Ringwald if (att_server_registered){ 12623d71c7a4SMatthias Ringwald log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle); 12633d71c7a4SMatthias Ringwald return; 12643d71c7a4SMatthias Ringwald } 12653d71c7a4SMatthias Ringwald btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler); 12663d71c7a4SMatthias Ringwald } 12673d71c7a4SMatthias Ringwald 12683deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){ 12693deb3ec6SMatthias Ringwald 12703d71c7a4SMatthias Ringwald // store callbacks 12713d71c7a4SMatthias Ringwald att_server_client_read_callback = read_callback; 12723d71c7a4SMatthias Ringwald att_server_client_write_callback = write_callback; 12733d71c7a4SMatthias Ringwald 12741a389cdcSMatthias Ringwald // register for HCI Events 1275d9a7306aSMatthias Ringwald hci_event_callback_registration.callback = &att_event_packet_handler; 12761a389cdcSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 12771a389cdcSMatthias Ringwald 1278406722e4SMatthias Ringwald // register for SM events 1279d9a7306aSMatthias Ringwald sm_event_callback_registration.callback = &att_event_packet_handler; 1280406722e4SMatthias Ringwald sm_add_event_handler(&sm_event_callback_registration); 12813deb3ec6SMatthias Ringwald 12821a389cdcSMatthias Ringwald // and L2CAP ATT Server PDUs 12833deb3ec6SMatthias Ringwald att_dispatch_register_server(att_packet_handler); 12843deb3ec6SMatthias Ringwald 128570dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 128670dca4d4SMatthias Ringwald // setup l2cap service 1287f2372b44SMatthias Ringwald l2cap_register_service(&att_event_packet_handler, PSM_ATT, 0xffff, gap_get_security_level()); 128870dca4d4SMatthias Ringwald #endif 128970dca4d4SMatthias Ringwald 12903deb3ec6SMatthias Ringwald att_set_db(db); 12913d71c7a4SMatthias Ringwald att_set_read_callback(att_server_read_callback); 12923d71c7a4SMatthias Ringwald att_set_write_callback(att_server_write_callback); 12933deb3ec6SMatthias Ringwald } 12943deb3ec6SMatthias Ringwald 12953deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){ 12963deb3ec6SMatthias Ringwald att_client_packet_handler = handler; 12973deb3ec6SMatthias Ringwald } 12983deb3ec6SMatthias Ringwald 1299cbbb12d9SMatthias Ringwald 1300cbbb12d9SMatthias Ringwald // to be deprecated 1301c141988cSMatthias Ringwald int att_server_can_send_packet_now(hci_con_handle_t con_handle){ 130268591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 130368591e36SMatthias Ringwald if (!hci_connection) return 0; 130437a8059dSMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 130537a8059dSMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 130637a8059dSMatthias Ringwald return att_server_can_send_packet(att_server, att_connection); 13071b96b007SMatthias Ringwald } 13081b96b007SMatthias Ringwald 13099816429dSMatthias Ringwald uint8_t att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 1310cbbb12d9SMatthias Ringwald return att_server_request_to_send_notification(callback_registration, con_handle); 13113deb3ec6SMatthias Ringwald } 13123deb3ec6SMatthias Ringwald 1313cbbb12d9SMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){ 1314cbbb12d9SMatthias Ringwald att_client_waiting_for_can_send_registration.callback = &att_emit_can_send_now_event; 1315cbbb12d9SMatthias Ringwald att_server_request_to_send_notification(&att_client_waiting_for_can_send_registration, con_handle); 1316cbbb12d9SMatthias Ringwald } 1317cbbb12d9SMatthias Ringwald // end of deprecated 1318cbbb12d9SMatthias Ringwald 13199816429dSMatthias Ringwald uint8_t att_server_request_to_send_notification(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 132068591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 132168591e36SMatthias Ringwald if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 132268591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 13233f9b77dbSMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 1324c37df6f6SMatthias Ringwald bool added = btstack_linked_list_add_tail(&att_server->notification_requests, (btstack_linked_item_t*) callback_registration); 13253f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 1326c37df6f6SMatthias Ringwald if (added){ 1327cbbb12d9SMatthias Ringwald return ERROR_CODE_SUCCESS; 1328c37df6f6SMatthias Ringwald } else { 1329c37df6f6SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1330c37df6f6SMatthias Ringwald } 1331cbbb12d9SMatthias Ringwald } 1332cbbb12d9SMatthias Ringwald 13339816429dSMatthias Ringwald uint8_t att_server_request_to_send_indication(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 133468591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 133568591e36SMatthias Ringwald if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 133668591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 13373f9b77dbSMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 1338c37df6f6SMatthias Ringwald bool added = btstack_linked_list_add_tail(&att_server->indication_requests, (btstack_linked_item_t*) callback_registration); 13393f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 1340c37df6f6SMatthias Ringwald if (added){ 1341969a5bbaSMatthias Ringwald return ERROR_CODE_SUCCESS; 1342c37df6f6SMatthias Ringwald } else { 1343c37df6f6SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1344c37df6f6SMatthias Ringwald } 1345bb38f057SMatthias Ringwald } 1346bb38f057SMatthias Ringwald 13479816429dSMatthias Ringwald uint8_t att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){ 134868591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 134968591e36SMatthias Ringwald if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 135037a8059dSMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 1351388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 135268591e36SMatthias Ringwald 135337a8059dSMatthias Ringwald if (!att_server_can_send_packet(att_server, att_connection)) return BTSTACK_ACL_BUFFERS_FULL; 13543deb3ec6SMatthias Ringwald 13553deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 13563deb3ec6SMatthias Ringwald uint8_t * packet_buffer = l2cap_get_outgoing_buffer(); 1357b6df12b6SMatthias Ringwald uint16_t size = att_prepare_handle_value_notification(att_connection, attribute_handle, value, value_len, packet_buffer); 1358d415948cSMatthias Ringwald 13590f70c52eSMatthias Ringwald return att_server_send_prepared(att_server, att_connection, NULL, size); 13603deb3ec6SMatthias Ringwald } 13613deb3ec6SMatthias Ringwald 13629816429dSMatthias Ringwald uint8_t att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){ 136368591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 136468591e36SMatthias Ringwald if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 136568591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 1366388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 136718707219SMatthias Ringwald 136838b893aaSMilanka Ringwald if (att_server->value_indication_handle != 0u) return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS; 136937a8059dSMatthias Ringwald if (!att_server_can_send_packet(att_server, att_connection)) return BTSTACK_ACL_BUFFERS_FULL; 13703deb3ec6SMatthias Ringwald 13713deb3ec6SMatthias Ringwald // track indication 137218707219SMatthias Ringwald att_server->value_indication_handle = attribute_handle; 137318707219SMatthias Ringwald btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout); 137418707219SMatthias Ringwald btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS); 137518707219SMatthias Ringwald btstack_run_loop_add_timer(&att_server->value_indication_timer); 13763deb3ec6SMatthias Ringwald 13773deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 13783deb3ec6SMatthias Ringwald uint8_t * packet_buffer = l2cap_get_outgoing_buffer(); 1379b6df12b6SMatthias Ringwald uint16_t size = att_prepare_handle_value_indication(att_connection, attribute_handle, value, value_len, packet_buffer); 138023079b39SMatthias Ringwald 13810f70c52eSMatthias Ringwald return att_server_send_prepared(att_server, att_connection, NULL, size); 13823deb3ec6SMatthias Ringwald } 13838f9132b5SMilanka Ringwald 13848f9132b5SMilanka Ringwald uint16_t att_server_get_mtu(hci_con_handle_t con_handle){ 138568591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 138668591e36SMatthias Ringwald if (!hci_connection) return 0; 1387388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 1388b6df12b6SMatthias Ringwald return att_connection->mtu; 13898f9132b5SMilanka Ringwald } 139035d7f2dfSMilanka Ringwald 139135d7f2dfSMilanka Ringwald void att_server_deinit(void){ 139235d7f2dfSMilanka Ringwald att_server_client_read_callback = NULL; 139335d7f2dfSMilanka Ringwald att_server_client_write_callback = NULL; 139435d7f2dfSMilanka Ringwald att_client_packet_handler = NULL; 139535d7f2dfSMilanka Ringwald service_handlers = NULL; 139635d7f2dfSMilanka Ringwald } 139720d4358dSMatthias Ringwald 139820d4358dSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT 13998d3bb68fSMatthias Ringwald 140020d4358dSMatthias Ringwald static uint16_t att_server_eatt_receive_buffer_size; 14018d3bb68fSMatthias Ringwald static uint16_t att_server_eatt_send_buffer_size; 140220d4358dSMatthias Ringwald 1403523bb989SMatthias Ringwald static att_server_eatt_bearer_t * att_server_eatt_bearer_for_cid(uint16_t cid){ 1404523bb989SMatthias Ringwald btstack_linked_list_iterator_t it; 1405523bb989SMatthias Ringwald btstack_linked_list_iterator_init(&it, &att_server_eatt_bearer_active); 1406523bb989SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 1407523bb989SMatthias Ringwald att_server_eatt_bearer_t * eatt_bearer = (att_server_eatt_bearer_t *) btstack_linked_list_iterator_next(&it); 1408f786dd08SMatthias Ringwald if (eatt_bearer->att_server.l2cap_cid == cid) { 1409523bb989SMatthias Ringwald return eatt_bearer; 1410523bb989SMatthias Ringwald } 1411523bb989SMatthias Ringwald } 1412523bb989SMatthias Ringwald return NULL; 1413523bb989SMatthias Ringwald } 1414523bb989SMatthias Ringwald 141520d4358dSMatthias Ringwald static void att_server_eatt_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 141620d4358dSMatthias Ringwald uint16_t cid; 141720d4358dSMatthias Ringwald uint8_t status; 141820d4358dSMatthias Ringwald uint16_t remote_mtu; 141920d4358dSMatthias Ringwald 142020d4358dSMatthias Ringwald uint8_t i; 142120d4358dSMatthias Ringwald uint8_t num_requested_bearers; 142220d4358dSMatthias Ringwald uint8_t num_accepted_bearers; 142320d4358dSMatthias Ringwald uint8_t initial_credits = 5; 142420d4358dSMatthias Ringwald uint8_t * receive_buffers[5]; 142520d4358dSMatthias Ringwald uint16_t cids[5]; 142620d4358dSMatthias Ringwald att_server_eatt_bearer_t * eatt_bearers[5]; 1427523bb989SMatthias Ringwald att_server_eatt_bearer_t * eatt_bearer; 14282954c397SMatthias Ringwald att_server_t * att_server; 14292954c397SMatthias Ringwald att_connection_t * att_connection; 143020d4358dSMatthias Ringwald 143120d4358dSMatthias Ringwald switch (packet_type) { 143220d4358dSMatthias Ringwald 143320d4358dSMatthias Ringwald case L2CAP_DATA_PACKET: 1434f786dd08SMatthias Ringwald eatt_bearer = att_server_eatt_bearer_for_cid(channel); 1435f786dd08SMatthias Ringwald btstack_assert(eatt_bearer != NULL); 1436f786dd08SMatthias Ringwald att_server = &eatt_bearer->att_server; 1437f786dd08SMatthias Ringwald att_connection = &eatt_bearer->att_connection; 1438f786dd08SMatthias Ringwald att_server_handle_att_pdu(att_server, att_connection, packet, size); 143920d4358dSMatthias Ringwald break; 144020d4358dSMatthias Ringwald 144120d4358dSMatthias Ringwald case HCI_EVENT_PACKET: 144220d4358dSMatthias Ringwald switch (packet[0]) { 144320d4358dSMatthias Ringwald 144420d4358dSMatthias Ringwald case L2CAP_EVENT_CAN_SEND_NOW: 1445f786dd08SMatthias Ringwald cid = l2cap_event_packet_sent_get_local_cid(packet); 1446f786dd08SMatthias Ringwald eatt_bearer = att_server_eatt_bearer_for_cid(cid); 1447f786dd08SMatthias Ringwald btstack_assert(eatt_bearer != NULL); 1448f786dd08SMatthias Ringwald att_server = &eatt_bearer->att_server; 1449f786dd08SMatthias Ringwald att_connection = &eatt_bearer->att_connection; 1450f786dd08SMatthias Ringwald // only used for EATT request responses 1451f786dd08SMatthias Ringwald btstack_assert(att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED); 1452f786dd08SMatthias Ringwald att_server_process_validated_request(att_server, att_connection, eatt_bearer->send_buffer); 145320d4358dSMatthias Ringwald break; 145420d4358dSMatthias Ringwald 145520d4358dSMatthias Ringwald case L2CAP_EVENT_PACKET_SENT: 145620d4358dSMatthias Ringwald cid = l2cap_event_packet_sent_get_local_cid(packet); 1457be4ca859SMatthias Ringwald eatt_bearer = att_server_eatt_bearer_for_cid(cid); 1458f786dd08SMatthias Ringwald btstack_assert(eatt_bearer != NULL); 14592954c397SMatthias Ringwald att_server = &eatt_bearer->att_server; 14602954c397SMatthias Ringwald att_connection = &eatt_bearer->att_connection; 14612954c397SMatthias Ringwald att_server_handle_att_pdu(att_server, att_connection, packet, size); 146220d4358dSMatthias Ringwald break; 146320d4358dSMatthias Ringwald 146420d4358dSMatthias Ringwald case L2CAP_EVENT_ECBM_INCOMING_CONNECTION: 146520d4358dSMatthias Ringwald cid = l2cap_event_ecbm_incoming_connection_get_local_cid(packet); 146620d4358dSMatthias Ringwald num_requested_bearers = l2cap_event_ecbm_incoming_connection_get_num_channels(packet); 146720d4358dSMatthias Ringwald for (i = 0; i < num_requested_bearers; i++){ 1468*c2cbe117SMatthias Ringwald eatt_bearers[i] = (att_server_eatt_bearer_t *) btstack_linked_list_pop(&att_server_eatt_bearer_pool); 146920d4358dSMatthias Ringwald if (eatt_bearers[i] == NULL) { 147020d4358dSMatthias Ringwald break; 147120d4358dSMatthias Ringwald } 1472be4ca859SMatthias Ringwald eatt_bearers[i]->att_connection.con_handle = l2cap_event_ecbm_incoming_connection_get_handle(packet); 1473f786dd08SMatthias Ringwald eatt_bearers[i]->att_server.bearer_type = ATT_BEARER_ENHANCED_LE; 1474735b0222SMatthias Ringwald receive_buffers[i] = eatt_bearers[i]->receive_buffer; 14756b236cb0SMatthias Ringwald btstack_linked_list_add(&att_server_eatt_bearer_active, (btstack_linked_item_t *) eatt_bearers[i]); 147620d4358dSMatthias Ringwald } 147720d4358dSMatthias Ringwald num_accepted_bearers = i; 147820d4358dSMatthias Ringwald status = l2cap_ecbm_accept_channels(cid, num_accepted_bearers, initial_credits, att_server_eatt_receive_buffer_size, receive_buffers, cids); 147920d4358dSMatthias Ringwald btstack_assert(status == ERROR_CODE_SUCCESS); 1480*c2cbe117SMatthias Ringwald log_info("requested %u, accepted %u", num_requested_bearers, num_accepted_bearers); 14816b236cb0SMatthias Ringwald for (i=0;i<num_accepted_bearers;i++){ 1482*c2cbe117SMatthias Ringwald log_info("eatt l2cap cid: 0x%04x", cids[i]); 1483f786dd08SMatthias Ringwald eatt_bearers[i]->att_server.l2cap_cid = cids[i]; 14846b236cb0SMatthias Ringwald } 148520d4358dSMatthias Ringwald break; 148620d4358dSMatthias Ringwald 148720d4358dSMatthias Ringwald case L2CAP_EVENT_ECBM_CHANNEL_OPENED: 148820d4358dSMatthias Ringwald cid = l2cap_event_ecbm_channel_opened_get_local_cid(packet); 1489*c2cbe117SMatthias Ringwald status = l2cap_event_ecbm_channel_opened_get_status(packet); 1490*c2cbe117SMatthias Ringwald remote_mtu = l2cap_event_ecbm_channel_opened_get_remote_mtu(packet); 1491be4ca859SMatthias Ringwald eatt_bearer = att_server_eatt_bearer_for_cid(cid); 1492be4ca859SMatthias Ringwald btstack_assert(eatt_bearer != NULL); 1493*c2cbe117SMatthias Ringwald eatt_bearer->att_connection.mtu_exchanged = true; 1494*c2cbe117SMatthias Ringwald eatt_bearer->att_connection.mtu = remote_mtu; 1495be4ca859SMatthias Ringwald eatt_bearer->att_connection.max_mtu = remote_mtu; 149620d4358dSMatthias Ringwald log_info("L2CAP_EVENT_ECBM_CHANNEL_OPENED - cid 0x%04x mtu %u, status 0x%02x", cid, remote_mtu, status); 149720d4358dSMatthias Ringwald break; 149820d4358dSMatthias Ringwald 149920d4358dSMatthias Ringwald case L2CAP_EVENT_ECBM_RECONFIGURED: 150020d4358dSMatthias Ringwald break; 150120d4358dSMatthias Ringwald 1502523bb989SMatthias Ringwald case L2CAP_EVENT_CHANNEL_CLOSED: 1503523bb989SMatthias Ringwald eatt_bearer = att_server_eatt_bearer_for_cid(l2cap_event_channel_closed_get_local_cid(packet)); 1504523bb989SMatthias Ringwald btstack_assert(eatt_bearers != NULL); 1505523bb989SMatthias Ringwald 1506523bb989SMatthias Ringwald // TODO: finalize - abort queued writes 1507523bb989SMatthias Ringwald 1508523bb989SMatthias Ringwald btstack_linked_list_remove(&att_server_eatt_bearer_active, (btstack_linked_item_t *) eatt_bearers); 1509523bb989SMatthias Ringwald btstack_linked_list_add(&att_server_eatt_bearer_pool, (btstack_linked_item_t *) eatt_bearer); 1510523bb989SMatthias Ringwald break; 151120d4358dSMatthias Ringwald default: 151220d4358dSMatthias Ringwald break; 151320d4358dSMatthias Ringwald } 151420d4358dSMatthias Ringwald } 151520d4358dSMatthias Ringwald } 151620d4358dSMatthias Ringwald 151720d4358dSMatthias Ringwald // create eatt bearers 151820d4358dSMatthias Ringwald uint8_t att_server_eatt_init(uint8_t num_eatt_bearers, uint8_t * storage_buffer, uint16_t storage_size){ 151920d4358dSMatthias Ringwald uint16_t size_for_structs = num_eatt_bearers * sizeof(att_server_eatt_bearer_t); 152020d4358dSMatthias Ringwald if (storage_size < size_for_structs) { 152120d4358dSMatthias Ringwald return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED; 152220d4358dSMatthias Ringwald } 152320d4358dSMatthias Ringwald 152420d4358dSMatthias Ringwald // TODO: The minimum ATT_MTU for an Enhanced ATT bearer is 64 octets. 152520d4358dSMatthias Ringwald 152620d4358dSMatthias Ringwald memset(storage_buffer, 0, storage_size); 15278d3bb68fSMatthias Ringwald uint16_t buffer_size_per_bearer = ((storage_size - size_for_structs) / num_eatt_bearers); 15288d3bb68fSMatthias Ringwald att_server_eatt_receive_buffer_size = buffer_size_per_bearer / 2; 15298d3bb68fSMatthias Ringwald att_server_eatt_send_buffer_size = buffer_size_per_bearer / 2; 1530735b0222SMatthias Ringwald uint8_t * bearer_buffer = &storage_buffer[size_for_structs]; 153120d4358dSMatthias Ringwald uint8_t i; 153220d4358dSMatthias Ringwald att_server_eatt_bearer_t * eatt_bearer = (att_server_eatt_bearer_t *) storage_buffer; 1533be4ca859SMatthias Ringwald log_info("%u EATT bearers with receive buffer size %u", 1534be4ca859SMatthias Ringwald num_eatt_bearers, att_server_eatt_receive_buffer_size); 153520d4358dSMatthias Ringwald for (i=0;i<num_eatt_bearers;i++){ 1536be4ca859SMatthias Ringwald eatt_bearer->att_connection.con_handle = HCI_CON_HANDLE_INVALID; 1537735b0222SMatthias Ringwald eatt_bearer->receive_buffer = bearer_buffer; 1538735b0222SMatthias Ringwald bearer_buffer += att_server_eatt_receive_buffer_size; 15398d3bb68fSMatthias Ringwald eatt_bearer->send_buffer = bearer_buffer; 15408d3bb68fSMatthias Ringwald bearer_buffer += att_server_eatt_send_buffer_size; 15416b236cb0SMatthias Ringwald btstack_linked_list_add(&att_server_eatt_bearer_pool, (btstack_linked_item_t *) eatt_bearer); 154220d4358dSMatthias Ringwald eatt_bearer++; 154320d4358dSMatthias Ringwald } 154420d4358dSMatthias Ringwald // TODO: define minimum EATT MTU 154520d4358dSMatthias Ringwald l2cap_ecbm_register_service(att_server_eatt_handler, BLUETOOTH_PSM_EATT, 64, 0); 154620d4358dSMatthias Ringwald 154720d4358dSMatthias Ringwald return 0; 154820d4358dSMatthias Ringwald } 154920d4358dSMatthias Ringwald #endif