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" 57be75fc08SMatthias Ringwald #include "bluetooth_psm.h" 5816ece135SMatthias Ringwald #include "btstack_debug.h" 590e2df43fSMatthias Ringwald #include "btstack_event.h" 603deb3ec6SMatthias Ringwald #include "btstack_memory.h" 610e2df43fSMatthias Ringwald #include "btstack_run_loop.h" 6259c6af15SMatthias Ringwald #include "gap.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 80c61037ccSMatthias Ringwald #define ATT_SERVICE_FLAGS_DELAYED_RESPONSE (1<<0u) 81c61037ccSMatthias Ringwald 82cd45e970SMatthias Ringwald static void att_run_for_context(att_server_t * att_server, att_connection_t * att_connection); 8301ac2008SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle); 845d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle); 8548364364SMatthias Ringwald static void att_server_handle_can_send_now(void); 869a1f55eaSMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server, att_connection_t * att_connection); 87fe3d0cbaSMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server); 8826b42dcfSMatthias Ringwald static void att_server_handle_att_pdu(att_server_t * att_server, att_connection_t * att_connection, uint8_t * packet, uint16_t size); 893deb3ec6SMatthias Ringwald 903551936eSMatthias Ringwald typedef enum { 9138b893aaSMilanka Ringwald ATT_SERVER_RUN_PHASE_1_REQUESTS = 0, 923551936eSMatthias Ringwald ATT_SERVER_RUN_PHASE_2_INDICATIONS, 933551936eSMatthias Ringwald ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS, 943551936eSMatthias Ringwald } att_server_run_phase_t; 953551936eSMatthias Ringwald 966a540790SMatthias Ringwald // 976a540790SMatthias Ringwald typedef struct { 986a540790SMatthias Ringwald uint32_t seq_nr; 996a540790SMatthias Ringwald uint16_t att_handle; 1006a540790SMatthias Ringwald uint8_t value; 1016a540790SMatthias Ringwald uint8_t device_index; 1026a540790SMatthias Ringwald } persistent_ccc_entry_t; 1036a540790SMatthias Ringwald 10418707219SMatthias Ringwald // global 1051a389cdcSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 106406722e4SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration; 10735d7f2dfSMilanka Ringwald static btstack_packet_handler_t att_client_packet_handler; 1083d71c7a4SMatthias Ringwald static btstack_linked_list_t service_handlers; 1095f141511SMatthias Ringwald static btstack_context_callback_registration_t att_client_waiting_for_can_send_registration; 11018707219SMatthias Ringwald 1113d71c7a4SMatthias Ringwald static att_read_callback_t att_server_client_read_callback; 1123d71c7a4SMatthias Ringwald static att_write_callback_t att_server_client_write_callback; 1133d71c7a4SMatthias Ringwald 1146438547aSMatthias Ringwald // round robin 1156438547aSMatthias Ringwald static hci_con_handle_t att_server_last_can_send_now = HCI_CON_HANDLE_INVALID; 116bf78aac6SMatthias Ringwald 117c61037ccSMatthias Ringwald static uint8_t att_server_flags; 118c61037ccSMatthias Ringwald 119c2cbe117SMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT 12040f8d8f2SMatthias Ringwald static att_server_eatt_bearer_t * att_server_eatt_bearer_for_con_handle(hci_con_handle_t con_handle); 121c2cbe117SMatthias Ringwald static btstack_linked_list_t att_server_eatt_bearer_pool; 122c2cbe117SMatthias Ringwald static btstack_linked_list_t att_server_eatt_bearer_active; 123c2cbe117SMatthias Ringwald #endif 124c2cbe117SMatthias Ringwald 12502b78fc8SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 12626567ee7SMatthias 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){ 12718707219SMatthias Ringwald btstack_linked_list_iterator_t it; 12818707219SMatthias Ringwald hci_connections_get_iterator(&it); 12918707219SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 13026567ee7SMatthias Ringwald hci_connection_t * hci_connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 13126567ee7SMatthias Ringwald if (hci_connection->att_server.state == state) { 13226567ee7SMatthias Ringwald *att_server_out = &hci_connection->att_server; 13326567ee7SMatthias Ringwald *att_connection_out = &hci_connection->att_connection; 13426567ee7SMatthias Ringwald return true; 13518707219SMatthias Ringwald } 13626567ee7SMatthias Ringwald } 137c2cbe117SMatthias Ringwald 138c2cbe117SMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT 139c2cbe117SMatthias Ringwald btstack_linked_list_iterator_init(&it, &att_server_eatt_bearer_active); 140c2cbe117SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 141c2cbe117SMatthias Ringwald att_server_eatt_bearer_t * eatt_bearer = (att_server_eatt_bearer_t *) btstack_linked_list_iterator_next(&it); 142c2cbe117SMatthias Ringwald if (eatt_bearer->att_server.state == state) { 143c2cbe117SMatthias Ringwald *att_server_out = &eatt_bearer->att_server; 144c2cbe117SMatthias Ringwald *att_connection_out = &eatt_bearer->att_connection; 145c2cbe117SMatthias Ringwald return true; 146c2cbe117SMatthias Ringwald } 147c2cbe117SMatthias Ringwald } 148c2cbe117SMatthias Ringwald #endif 149c2cbe117SMatthias Ringwald 15026567ee7SMatthias Ringwald return false; 15118707219SMatthias Ringwald } 15202b78fc8SMatthias Ringwald #endif 153bb38f057SMatthias Ringwald 1543f9b77dbSMatthias Ringwald static void att_server_request_can_send_now(att_server_t * att_server, att_connection_t * att_connection ){ 15523079b39SMatthias Ringwald switch (att_server->bearer_type){ 15623079b39SMatthias Ringwald case ATT_BEARER_UNENHANCED_LE: 15723079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 15823079b39SMatthias Ringwald case ATT_BEARER_UNENHANCED_CLASSIC: 1592954c397SMatthias Ringwald /* fall through */ 1602954c397SMatthias Ringwald #endif 161328bed13SMatthias Ringwald att_dispatch_server_request_can_send_now_event(att_connection->con_handle); 162328bed13SMatthias Ringwald break; 1632954c397SMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT 1642954c397SMatthias Ringwald case ATT_BEARER_ENHANCED_LE: 16523079b39SMatthias Ringwald l2cap_request_can_send_now_event(att_server->l2cap_cid); 16623079b39SMatthias Ringwald break; 16723079b39SMatthias Ringwald #endif 16823079b39SMatthias Ringwald default: 16923079b39SMatthias Ringwald btstack_unreachable(); 17023079b39SMatthias Ringwald break; 17123079b39SMatthias Ringwald } 172c8c6e9b4SMatthias Ringwald } 173c8c6e9b4SMatthias Ringwald 17437a8059dSMatthias Ringwald static bool att_server_can_send_packet(att_server_t * att_server, att_connection_t * att_connection){ 17523079b39SMatthias Ringwald switch (att_server->bearer_type) { 17623079b39SMatthias Ringwald case ATT_BEARER_UNENHANCED_LE: 17723079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 17823079b39SMatthias Ringwald case ATT_BEARER_UNENHANCED_CLASSIC: 1792954c397SMatthias Ringwald /* fall through */ 1802954c397SMatthias Ringwald #endif 181328bed13SMatthias Ringwald return att_dispatch_server_can_send_now(att_connection->con_handle); 1822954c397SMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT 1832954c397SMatthias Ringwald case ATT_BEARER_ENHANCED_LE: 1842954c397SMatthias Ringwald return l2cap_can_send_packet_now(att_server->l2cap_cid); 18523079b39SMatthias Ringwald #endif 18623079b39SMatthias Ringwald default: 18723079b39SMatthias Ringwald btstack_unreachable(); 18823079b39SMatthias Ringwald break; 18923079b39SMatthias Ringwald } 19023079b39SMatthias Ringwald return false; 191c0f26e1cSMatthias Ringwald } 192c0f26e1cSMatthias Ringwald 1933deb3ec6SMatthias Ringwald static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){ 1945d07396bSMatthias Ringwald btstack_packet_handler_t packet_handler = att_server_packet_handler_for_handle(attribute_handle); 1955d07396bSMatthias Ringwald if (!packet_handler) return; 1963deb3ec6SMatthias Ringwald 1973deb3ec6SMatthias Ringwald uint8_t event[7]; 1983deb3ec6SMatthias Ringwald int pos = 0; 1995611a760SMatthias Ringwald event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE; 2004ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 2013deb3ec6SMatthias Ringwald event[pos++] = status; 202f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, client_handle); 2033deb3ec6SMatthias Ringwald pos += 2; 204f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, attribute_handle); 205a444112bSMatthias Ringwald (*packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 2063deb3ec6SMatthias Ringwald } 2073deb3ec6SMatthias Ringwald 2085d07396bSMatthias Ringwald static void att_emit_event_to_all(const uint8_t * event, uint16_t size){ 2095d07396bSMatthias Ringwald // dispatch to app level handler 2109305033eSMatthias Ringwald if (att_client_packet_handler != NULL){ 2115d07396bSMatthias Ringwald (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size); 2125d07396bSMatthias Ringwald } 2133deb3ec6SMatthias Ringwald 2145d07396bSMatthias Ringwald // dispatch to service handlers 2155d07396bSMatthias Ringwald btstack_linked_list_iterator_t it; 2165d07396bSMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 2175d07396bSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 2185d07396bSMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 2195d07396bSMatthias Ringwald if (!handler->packet_handler) continue; 2205d07396bSMatthias Ringwald (*handler->packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size); 2215d07396bSMatthias Ringwald } 2225d07396bSMatthias Ringwald } 2235d07396bSMatthias Ringwald 2245d07396bSMatthias Ringwald static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){ 2253deb3ec6SMatthias Ringwald uint8_t event[6]; 2263deb3ec6SMatthias Ringwald int pos = 0; 2275611a760SMatthias Ringwald event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE; 2284ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 229fc64f94aSMatthias Ringwald little_endian_store_16(event, pos, con_handle); 2303deb3ec6SMatthias Ringwald pos += 2; 231f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, mtu); 2325d07396bSMatthias Ringwald 2335d07396bSMatthias Ringwald // also dispatch to GATT Clients 234b12646c5SJakob Krantz att_dispatch_server_mtu_exchanged(con_handle, mtu); 2355d07396bSMatthias Ringwald 2365d07396bSMatthias Ringwald // dispatch to app level handler and service handlers 2375d07396bSMatthias Ringwald att_emit_event_to_all(&event[0], sizeof(event)); 2383deb3ec6SMatthias Ringwald } 2393deb3ec6SMatthias Ringwald 2405f141511SMatthias Ringwald static void att_emit_can_send_now_event(void * context){ 2415f141511SMatthias Ringwald UNUSED(context); 2423c97b242SMatthias Ringwald if (!att_client_packet_handler) return; 2433c97b242SMatthias Ringwald 2441b96b007SMatthias Ringwald uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0}; 2451b96b007SMatthias Ringwald (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 2461b96b007SMatthias Ringwald } 2471b96b007SMatthias Ringwald 2486d2214cbSMatthias Ringwald static void att_emit_connected_event(att_server_t * att_server, att_connection_t * att_connection){ 249ff1bec95SMatthias Ringwald uint8_t event[11]; 250ff1bec95SMatthias Ringwald int pos = 0; 251ff1bec95SMatthias Ringwald event[pos++] = ATT_EVENT_CONNECTED; 2524ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 253ff1bec95SMatthias Ringwald event[pos++] = att_server->peer_addr_type; 254ff1bec95SMatthias Ringwald reverse_bd_addr(att_server->peer_address, &event[pos]); 255ff1bec95SMatthias Ringwald pos += 6; 256b6df12b6SMatthias Ringwald little_endian_store_16(event, pos, att_connection->con_handle); 257ff1bec95SMatthias Ringwald pos += 2; 258ff1bec95SMatthias Ringwald 259ff1bec95SMatthias Ringwald // dispatch to app level handler and service handlers 260ff1bec95SMatthias Ringwald att_emit_event_to_all(&event[0], sizeof(event)); 261ff1bec95SMatthias Ringwald } 262ff1bec95SMatthias Ringwald 263ff1bec95SMatthias Ringwald 2646187ad4cSMatthias Ringwald static void att_emit_disconnected_event(uint16_t con_handle){ 265ff1bec95SMatthias Ringwald uint8_t event[4]; 266ff1bec95SMatthias Ringwald int pos = 0; 267ff1bec95SMatthias Ringwald event[pos++] = ATT_EVENT_DISCONNECTED; 2684ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 2696187ad4cSMatthias Ringwald little_endian_store_16(event, pos, con_handle); 270ff1bec95SMatthias Ringwald pos += 2; 271ff1bec95SMatthias Ringwald 272ff1bec95SMatthias Ringwald // dispatch to app level handler and service handlers 273ff1bec95SMatthias Ringwald att_emit_event_to_all(&event[0], sizeof(event)); 274ff1bec95SMatthias Ringwald } 275ff1bec95SMatthias Ringwald 276ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){ 27754178543SMatthias Ringwald void * context = btstack_run_loop_get_timer_context(ts); 27854178543SMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context; 27968591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 28068591e36SMatthias Ringwald if (!hci_connection) return; 28168591e36SMatthias Ringwald // @note: after a transaction timeout, no more requests shall be sent over this ATT Bearer 282bce48a26SMatthias Ringwald // (that's why we don't reset the value_indication_handle) 283b6df12b6SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 28418707219SMatthias Ringwald uint16_t att_handle = att_server->value_indication_handle; 285388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 28638b893aaSMilanka Ringwald att_handle_value_indication_notify_client((uint8_t)ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_connection->con_handle, att_handle); 2873deb3ec6SMatthias Ringwald } 2883deb3ec6SMatthias Ringwald 28952e211deSMatthias Ringwald static void att_server_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2909ec2630cSMatthias Ringwald 2917dad9ff8SMatthias Ringwald UNUSED(channel); // ok: there is no channel 2927dad9ff8SMatthias Ringwald UNUSED(size); // ok: handling own l2cap events 2933deb3ec6SMatthias Ringwald 29418707219SMatthias Ringwald att_server_t * att_server; 295b6df12b6SMatthias Ringwald att_connection_t * att_connection; 29618707219SMatthias Ringwald hci_con_handle_t con_handle; 29768591e36SMatthias Ringwald hci_connection_t * hci_connection; 29818707219SMatthias Ringwald 2993deb3ec6SMatthias Ringwald switch (packet_type) { 3003deb3ec6SMatthias Ringwald 3013deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 3020e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 3039d1eff91SMatthias Ringwald case HCI_EVENT_META_GAP: 3049d1eff91SMatthias Ringwald switch (hci_event_gap_meta_get_subevent_code(packet)) { 3059d1eff91SMatthias Ringwald case GAP_SUBEVENT_LE_CONNECTION_COMPLETE: 3069d1eff91SMatthias Ringwald con_handle = gap_subevent_le_connection_complete_get_connection_handle(packet); 30768591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 30868591e36SMatthias Ringwald if (!hci_connection) break; 30968591e36SMatthias Ringwald att_server = &hci_connection->att_server; 3103deb3ec6SMatthias Ringwald // store connection info 3119d1eff91SMatthias Ringwald att_server->peer_addr_type = gap_subevent_le_connection_complete_get_peer_address_type(packet); 3129d1eff91SMatthias Ringwald gap_subevent_le_connection_complete_get_peer_address(packet, att_server->peer_address); 313388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 314b6df12b6SMatthias Ringwald att_connection->con_handle = con_handle; 3153deb3ec6SMatthias Ringwald // reset connection properties 31618707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 31723079b39SMatthias Ringwald att_server->bearer_type = ATT_BEARER_UNENHANCED_LE; 318b6df12b6SMatthias Ringwald att_connection->mtu = ATT_DEFAULT_MTU; 319b6df12b6SMatthias Ringwald att_connection->max_mtu = l2cap_max_le_mtu(); 320b6df12b6SMatthias Ringwald if (att_connection->max_mtu > ATT_REQUEST_BUFFER_SIZE){ 321b6df12b6SMatthias Ringwald att_connection->max_mtu = ATT_REQUEST_BUFFER_SIZE; 32299c58ad0SMatthias Ringwald } 32338b893aaSMilanka Ringwald att_connection->encryption_key_size = 0u; 32438b893aaSMilanka Ringwald att_connection->authenticated = 0u; 32538b893aaSMilanka Ringwald att_connection->authorized = 0u; 326b2c1e52cSMatthias Ringwald // workaround: identity resolving can already be complete, at least store result 327b2c1e52cSMatthias Ringwald att_server->ir_le_device_db_index = sm_le_device_index(con_handle); 3284ab47674SMatthias Ringwald att_server->ir_lookup_active = false; 3294ab47674SMatthias Ringwald att_server->pairing_active = false; 330ff1bec95SMatthias Ringwald // notify all - new 3316d2214cbSMatthias Ringwald att_emit_connected_event(att_server, att_connection); 3323deb3ec6SMatthias Ringwald break; 3333deb3ec6SMatthias Ringwald default: 3343deb3ec6SMatthias Ringwald break; 3353deb3ec6SMatthias Ringwald } 3363deb3ec6SMatthias Ringwald break; 3373deb3ec6SMatthias Ringwald 33894e69787SMatthias Ringwald case HCI_EVENT_LE_META: 33994e69787SMatthias Ringwald switch (hci_event_le_meta_get_subevent_code(packet)) { 34094e69787SMatthias Ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 34194e69787SMatthias Ringwald // forward LE Connection Complete event to keep backward compatibility 34294e69787SMatthias Ringwald // deprecated: please register hci handler with hci_add_event_handler or handle ATT_EVENT_CONNECTED 34394e69787SMatthias Ringwald att_emit_event_to_all(packet, size); 34494e69787SMatthias Ringwald break; 34594e69787SMatthias Ringwald default: 34694e69787SMatthias Ringwald break; 34794e69787SMatthias Ringwald } 34894e69787SMatthias Ringwald break; 34994e69787SMatthias Ringwald 3503deb3ec6SMatthias Ringwald case HCI_EVENT_ENCRYPTION_CHANGE: 3518601e696SMatthias Ringwald case HCI_EVENT_ENCRYPTION_CHANGE_V2: 3523deb3ec6SMatthias Ringwald case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE: 3533deb3ec6SMatthias Ringwald // check handle 35418707219SMatthias Ringwald con_handle = little_endian_read_16(packet, 3); 35568591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 35668591e36SMatthias Ringwald if (!hci_connection) break; 357914988a1SMatthias Ringwald if (gap_get_connection_type(con_handle) != GAP_CONNECTION_LE) break; 358914988a1SMatthias Ringwald // update security params 35968591e36SMatthias Ringwald att_server = &hci_connection->att_server; 360388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 361b6df12b6SMatthias Ringwald att_connection->encryption_key_size = gap_encryption_key_size(con_handle); 362ff3f1026SMatthias Ringwald att_connection->authenticated = gap_authenticated(con_handle) ? 1 : 0; 363f461b3dfSMatthias Ringwald att_connection->secure_connection = gap_secure_connection(con_handle) ? 1 : 0; 364914988a1SMatthias Ringwald log_info("encrypted key size %u, authenticated %u, secure connection %u", 365b6df12b6SMatthias Ringwald att_connection->encryption_key_size, att_connection->authenticated, att_connection->secure_connection); 366914988a1SMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){ 3674097998aSMatthias Ringwald // restore CCC values when encrypted for LE Connections 36822bfc225SMilanka Ringwald if (hci_event_encryption_change_get_encryption_enabled(packet) != 0){ 3699a1f55eaSMatthias Ringwald att_server_persistent_ccc_restore(att_server, att_connection); 37001ac2008SMatthias Ringwald } 37101ac2008SMatthias Ringwald } 372cd45e970SMatthias Ringwald att_run_for_context(att_server, att_connection); 3733deb3ec6SMatthias Ringwald break; 3743deb3ec6SMatthias Ringwald 3753deb3ec6SMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 37670a390c7SMatthias Ringwald // check handle 37718707219SMatthias Ringwald con_handle = hci_event_disconnection_complete_get_connection_handle(packet); 37868591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 37968591e36SMatthias Ringwald if (!hci_connection) break; 38068591e36SMatthias Ringwald att_server = &hci_connection->att_server; 381388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 382b6df12b6SMatthias Ringwald att_clear_transaction_queue(att_connection); 383b6df12b6SMatthias Ringwald att_connection->con_handle = 0; 3844ab47674SMatthias Ringwald att_server->pairing_active = false; 38518707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 38638b893aaSMilanka Ringwald if (att_server->value_indication_handle != 0u){ 38727328ecbSMatthias Ringwald btstack_run_loop_remove_timer(&att_server->value_indication_timer); 388bce48a26SMatthias Ringwald uint16_t att_handle = att_server->value_indication_handle; 38938b893aaSMilanka Ringwald att_server->value_indication_handle = 0u; // reset error state 39038b893aaSMilanka Ringwald att_handle_value_indication_notify_client((uint8_t)ATT_HANDLE_VALUE_INDICATION_DISCONNECT, att_connection->con_handle, att_handle); 391bce48a26SMatthias Ringwald } 392ff1bec95SMatthias Ringwald // notify all - new 3936187ad4cSMatthias Ringwald att_emit_disconnected_event(con_handle); 394ff1bec95SMatthias Ringwald // notify all - old 3955d07396bSMatthias Ringwald att_emit_event_to_all(packet, size); 3963deb3ec6SMatthias Ringwald break; 3973deb3ec6SMatthias Ringwald 398760ad805SMatthias Ringwald // Identity Resolving 3995611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_STARTED: 40018707219SMatthias Ringwald con_handle = sm_event_identity_resolving_started_get_handle(packet); 40168591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 40268591e36SMatthias Ringwald if (!hci_connection) break; 40368591e36SMatthias Ringwald att_server = &hci_connection->att_server; 4045611a760SMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED"); 4054ab47674SMatthias Ringwald att_server->ir_lookup_active = true; 4063deb3ec6SMatthias Ringwald break; 4075611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED: 408760ad805SMatthias Ringwald con_handle = sm_event_identity_created_get_handle(packet); 40968591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 41068591e36SMatthias Ringwald if (!hci_connection) return; 411acb2a577SMatthias Ringwald att_connection = &hci_connection->att_connection; 41268591e36SMatthias Ringwald att_server = &hci_connection->att_server; 4134ab47674SMatthias Ringwald att_server->ir_lookup_active = false; 4141b7acd0dSMatthias Ringwald att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index(packet); 4151b7acd0dSMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED"); 416cd45e970SMatthias Ringwald att_run_for_context(att_server, att_connection); 4173deb3ec6SMatthias Ringwald break; 4185611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_FAILED: 41918707219SMatthias Ringwald con_handle = sm_event_identity_resolving_failed_get_handle(packet); 42068591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 42168591e36SMatthias Ringwald if (!hci_connection) break; 422acb2a577SMatthias Ringwald att_connection = &hci_connection->att_connection; 42368591e36SMatthias Ringwald att_server = &hci_connection->att_server; 4245611a760SMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED"); 4254ab47674SMatthias Ringwald att_server->ir_lookup_active = false; 42618707219SMatthias Ringwald att_server->ir_le_device_db_index = -1; 427cd45e970SMatthias Ringwald att_run_for_context(att_server, att_connection); 4283deb3ec6SMatthias Ringwald break; 429760ad805SMatthias Ringwald 430760ad805SMatthias Ringwald // Pairing started - delete stored CCC values 431760ad805SMatthias Ringwald // - assumes pairing indicates either new device or re-pairing, in both cases there should be no stored CCC values 432760ad805SMatthias Ringwald // - assumes that all events have the con handle as the first field 433760ad805SMatthias Ringwald case SM_EVENT_JUST_WORKS_REQUEST: 434760ad805SMatthias Ringwald case SM_EVENT_PASSKEY_DISPLAY_NUMBER: 435760ad805SMatthias Ringwald case SM_EVENT_PASSKEY_INPUT_NUMBER: 436760ad805SMatthias Ringwald case SM_EVENT_NUMERIC_COMPARISON_REQUEST: 437760ad805SMatthias Ringwald con_handle = sm_event_just_works_request_get_handle(packet); 43868591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 43968591e36SMatthias Ringwald if (!hci_connection) break; 44068591e36SMatthias Ringwald att_server = &hci_connection->att_server; 441760ad805SMatthias Ringwald log_info("SM Pairing started"); 4424ab47674SMatthias Ringwald att_server->pairing_active = true; 443760ad805SMatthias Ringwald if (att_server->ir_le_device_db_index < 0) break; 44401c0691bSMatthias Ringwald att_server_persistent_ccc_clear(att_server); 445760ad805SMatthias Ringwald // index not valid anymore 446760ad805SMatthias Ringwald att_server->ir_le_device_db_index = -1; 447760ad805SMatthias Ringwald break; 448760ad805SMatthias Ringwald 4491b7acd0dSMatthias Ringwald // Bonding completed 450760ad805SMatthias Ringwald case SM_EVENT_IDENTITY_CREATED: 451760ad805SMatthias Ringwald con_handle = sm_event_identity_created_get_handle(packet); 45268591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 45368591e36SMatthias Ringwald if (!hci_connection) return; 454acb2a577SMatthias Ringwald att_connection = &hci_connection->att_connection; 45568591e36SMatthias Ringwald att_server = &hci_connection->att_server; 4564ab47674SMatthias Ringwald att_server->pairing_active = false; 4571b7acd0dSMatthias Ringwald att_server->ir_le_device_db_index = sm_event_identity_created_get_index(packet); 458cd45e970SMatthias Ringwald att_run_for_context(att_server, att_connection); 4591b7acd0dSMatthias Ringwald break; 4601b7acd0dSMatthias Ringwald 4611b7acd0dSMatthias Ringwald // Pairing complete (with/without bonding=storing of pairing information) 4621b7acd0dSMatthias Ringwald case SM_EVENT_PAIRING_COMPLETE: 4631b7acd0dSMatthias Ringwald con_handle = sm_event_pairing_complete_get_handle(packet); 46468591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 46568591e36SMatthias Ringwald if (!hci_connection) return; 466acb2a577SMatthias Ringwald att_connection = &hci_connection->att_connection; 46768591e36SMatthias Ringwald att_server = &hci_connection->att_server; 4684ab47674SMatthias Ringwald att_server->pairing_active = false; 469cd45e970SMatthias Ringwald att_run_for_context(att_server, att_connection); 470760ad805SMatthias Ringwald break; 471760ad805SMatthias Ringwald 472760ad805SMatthias Ringwald // Authorization 4735611a760SMatthias Ringwald case SM_EVENT_AUTHORIZATION_RESULT: { 47418707219SMatthias Ringwald con_handle = sm_event_authorization_result_get_handle(packet); 47568591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 47668591e36SMatthias Ringwald if (!hci_connection) break; 477388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 478acb2a577SMatthias Ringwald att_server = &hci_connection->att_server; 479b6df12b6SMatthias Ringwald att_connection->authorized = sm_event_authorization_result_get_authorization_result(packet); 4803f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 4813deb3ec6SMatthias Ringwald break; 4823deb3ec6SMatthias Ringwald } 4833deb3ec6SMatthias Ringwald default: 4843deb3ec6SMatthias Ringwald break; 4853deb3ec6SMatthias Ringwald } 48665b44ffdSMatthias Ringwald break; 48765b44ffdSMatthias Ringwald default: 48865b44ffdSMatthias Ringwald break; 4893deb3ec6SMatthias Ringwald } 4903deb3ec6SMatthias Ringwald } 4913deb3ec6SMatthias Ringwald 4920f70c52eSMatthias Ringwald static uint8_t 4930f70c52eSMatthias Ringwald att_server_send_prepared(const att_server_t *att_server, const att_connection_t *att_connection, uint8_t *buffer, 4940f70c52eSMatthias Ringwald uint16_t size) { 4953548b7cbSDirk Helbig UNUSED(buffer); 496d415948cSMatthias Ringwald uint8_t status = ERROR_CODE_SUCCESS; 497d415948cSMatthias Ringwald switch (att_server->bearer_type) { 498d415948cSMatthias Ringwald case ATT_BEARER_UNENHANCED_LE: 499d415948cSMatthias Ringwald status = l2cap_send_prepared_connectionless(att_connection->con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); 50040f8d8f2SMatthias Ringwald break; 501d415948cSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 502d415948cSMatthias Ringwald case ATT_BEARER_UNENHANCED_CLASSIC: 503d415948cSMatthias Ringwald status = l2cap_send_prepared(att_server->l2cap_cid, size); 504d415948cSMatthias Ringwald break; 505d415948cSMatthias Ringwald #endif 5060f70c52eSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT 5070f70c52eSMatthias Ringwald case ATT_BEARER_ENHANCED_LE: 5080f70c52eSMatthias Ringwald btstack_assert(buffer != NULL); 5090f70c52eSMatthias Ringwald status = l2cap_send(att_server->l2cap_cid, buffer, size); 5100f70c52eSMatthias Ringwald break; 5110f70c52eSMatthias Ringwald #endif 512d415948cSMatthias Ringwald default: 513d415948cSMatthias Ringwald btstack_unreachable(); 514d415948cSMatthias Ringwald break; 515d415948cSMatthias Ringwald } 516d415948cSMatthias Ringwald return status; 517d415948cSMatthias Ringwald } 518d415948cSMatthias Ringwald 5197a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 5203deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){ 52126567ee7SMatthias Ringwald att_server_t * att_server = NULL; 52226567ee7SMatthias Ringwald att_connection_t * att_connection = NULL; 52326567ee7SMatthias Ringwald bool found = att_server_connections_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION, &att_server, &att_connection); 5243deb3ec6SMatthias Ringwald 525c2cbe117SMatthias Ringwald if (found == false){ 526c2cbe117SMatthias Ringwald return; 527c2cbe117SMatthias Ringwald } 528c2cbe117SMatthias Ringwald 5293deb3ec6SMatthias Ringwald uint8_t hash_flipped[8]; 5309c80e4ccSMatthias Ringwald reverse_64(hash, hash_flipped); 53126567ee7SMatthias Ringwald if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8) != 0){ 5323deb3ec6SMatthias Ringwald log_info("ATT Signed Write, invalid signature"); 53303f53d81SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT 53403f53d81SMatthias Ringwald printf("ATT Signed Write, invalid signature\n"); 53503f53d81SMatthias Ringwald #endif 53618707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 5373deb3ec6SMatthias Ringwald return; 5383deb3ec6SMatthias Ringwald } 5393deb3ec6SMatthias Ringwald log_info("ATT Signed Write, valid signature"); 54003f53d81SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT 54103f53d81SMatthias Ringwald printf("ATT Signed Write, valid signature\n"); 54203f53d81SMatthias Ringwald #endif 5433deb3ec6SMatthias Ringwald 5443deb3ec6SMatthias Ringwald // update sequence number 54518707219SMatthias Ringwald uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12); 54618707219SMatthias Ringwald le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1); 54718707219SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 5483f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 5493deb3ec6SMatthias Ringwald } 5507a766ebfSMatthias Ringwald #endif 55188a48dd8SMatthias Ringwald 552c5867c46SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE 553c5867c46SMatthias Ringwald static void att_server_handle_response_pending(att_server_t *att_server, const att_connection_t *att_connection, 554c5867c46SMatthias Ringwald const uint8_t *eatt_buffer, 555c5867c46SMatthias Ringwald uint16_t att_response_size) { 556c5867c46SMatthias Ringwald // update state 557c5867c46SMatthias Ringwald att_server->state = ATT_SERVER_RESPONSE_PENDING; 558c5867c46SMatthias Ringwald 559c5867c46SMatthias Ringwald // callback with handle ATT_READ_RESPONSE_PENDING for reads 560c5867c46SMatthias Ringwald if (att_response_size == ATT_READ_RESPONSE_PENDING){ 561c5867c46SMatthias Ringwald // notify services that returned response pending 562c5867c46SMatthias Ringwald btstack_linked_list_iterator_t it; 563c5867c46SMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 564c5867c46SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)) { 565c5867c46SMatthias Ringwald att_service_handler_t *handler = (att_service_handler_t *) btstack_linked_list_iterator_next(&it); 566c5867c46SMatthias Ringwald if ((handler->flags & ATT_SERVICE_FLAGS_DELAYED_RESPONSE) != 0){ 567c5867c46SMatthias Ringwald handler->flags &= ~ATT_SERVICE_FLAGS_DELAYED_RESPONSE; 568c5867c46SMatthias Ringwald handler->read_callback(att_connection->con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0); 569c5867c46SMatthias Ringwald } 570c5867c46SMatthias Ringwald } 571c5867c46SMatthias Ringwald // notify main read callback if it returned response pending 572c5867c46SMatthias Ringwald if ((att_server_flags & ATT_SERVICE_FLAGS_DELAYED_RESPONSE) != 0){ 573c5867c46SMatthias Ringwald // flag was set by read callback 574c5867c46SMatthias Ringwald btstack_assert(att_server_client_read_callback != NULL); 575c5867c46SMatthias Ringwald (*att_server_client_read_callback)(att_connection->con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0); 576c5867c46SMatthias Ringwald } 577c5867c46SMatthias Ringwald } 57853a869daSMatthias Ringwald 57953a869daSMatthias Ringwald // free reserved buffer - might trigger next read/write 58053a869daSMatthias Ringwald if (eatt_buffer == NULL){ 58153a869daSMatthias Ringwald l2cap_release_packet_buffer(); 58253a869daSMatthias Ringwald } 583c5867c46SMatthias Ringwald } 584c5867c46SMatthias Ringwald #endif 585c5867c46SMatthias Ringwald 58618707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED 587b06f2579SMatthias Ringwald // pre: can send now 5889988bc8aSMatthias Ringwald // uses l2cap outgoing buffer if no eatt_buffer provided 589b06f2579SMatthias Ringwald // returns: 1 if packet was sent 5909988bc8aSMatthias Ringwald static int 5919988bc8aSMatthias Ringwald att_server_process_validated_request(att_server_t *att_server, att_connection_t *att_connection, uint8_t *eatt_buffer) { 592b06f2579SMatthias Ringwald 5939988bc8aSMatthias Ringwald uint8_t * att_response_buffer; 5949988bc8aSMatthias Ringwald if (eatt_buffer != NULL){ 5959988bc8aSMatthias Ringwald att_response_buffer = eatt_buffer; 5969988bc8aSMatthias Ringwald } else { 597b06f2579SMatthias Ringwald l2cap_reserve_packet_buffer(); 5989988bc8aSMatthias Ringwald att_response_buffer = l2cap_get_outgoing_buffer(); 5999988bc8aSMatthias Ringwald } 6009988bc8aSMatthias Ringwald 6017a293e22SMatthias Ringwald uint16_t att_response_size = 0; 6027a293e22SMatthias Ringwald // Send Error Response for MTU Request over connection-oriented channel 6037a293e22SMatthias Ringwald if ((att_server->bearer_type != ATT_BEARER_UNENHANCED_LE) && (att_server->request_buffer[0] == ATT_EXCHANGE_MTU_REQUEST)){ 6047a293e22SMatthias Ringwald att_response_size = 5; 6057a293e22SMatthias Ringwald att_response_buffer[0] = ATT_ERROR_RESPONSE; 6067a293e22SMatthias Ringwald att_response_buffer[1] = ATT_EXCHANGE_MTU_REQUEST; 6077a293e22SMatthias Ringwald att_response_buffer[2] = 0; 6087a293e22SMatthias Ringwald att_response_buffer[3] = 0; 6097a293e22SMatthias Ringwald att_response_buffer[4] = ATT_ERROR_REQUEST_NOT_SUPPORTED; 6107a293e22SMatthias Ringwald } else { 6117a293e22SMatthias Ringwald att_response_size = att_handle_request(att_connection, att_server->request_buffer, att_server->request_size, att_response_buffer); 6127a293e22SMatthias Ringwald } 613b06f2579SMatthias Ringwald 614beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE 615c1ab6cc1SMatthias Ringwald if ((att_response_size == ATT_READ_RESPONSE_PENDING) || (att_response_size == ATT_INTERNAL_WRITE_RESPONSE_PENDING)){ 616c5867c46SMatthias Ringwald att_server_handle_response_pending(att_server, att_connection, eatt_buffer, att_response_size); 617e8e7403eSMatthias Ringwald return 0; 618e404a688SMatthias Ringwald } 619e404a688SMatthias Ringwald #endif 620e404a688SMatthias Ringwald 621b06f2579SMatthias Ringwald // intercept "insufficient authorization" for authenticated connections to allow for user authorization 6224ea43905SMatthias Ringwald if ((att_response_size >= 4u) 623b06f2579SMatthias Ringwald && (att_response_buffer[0] == ATT_ERROR_RESPONSE) 624b06f2579SMatthias Ringwald && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION) 62538b893aaSMilanka Ringwald && (att_connection->authenticated != 0u)){ 626b06f2579SMatthias Ringwald 627b6df12b6SMatthias Ringwald switch (gap_authorization_state(att_connection->con_handle)){ 628b06f2579SMatthias Ringwald case AUTHORIZATION_UNKNOWN: 6299988bc8aSMatthias Ringwald if (eatt_buffer == NULL){ 630b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 6319988bc8aSMatthias Ringwald } 632b6df12b6SMatthias Ringwald sm_request_pairing(att_connection->con_handle); 633b06f2579SMatthias Ringwald return 0; 634b06f2579SMatthias Ringwald case AUTHORIZATION_PENDING: 6359988bc8aSMatthias Ringwald if (eatt_buffer == NULL){ 636b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 6379988bc8aSMatthias Ringwald } 638b06f2579SMatthias Ringwald return 0; 639b06f2579SMatthias Ringwald default: 640b06f2579SMatthias Ringwald break; 641b06f2579SMatthias Ringwald } 642b06f2579SMatthias Ringwald } 643b06f2579SMatthias Ringwald 64418707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 6454ea43905SMatthias Ringwald if (att_response_size == 0u) { 6469988bc8aSMatthias Ringwald if (eatt_buffer == NULL){ 647b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 6489988bc8aSMatthias Ringwald } 649b06f2579SMatthias Ringwald return 0; 650b06f2579SMatthias Ringwald } 651b06f2579SMatthias Ringwald 6529988bc8aSMatthias Ringwald (void) att_server_send_prepared(att_server, att_connection, eatt_buffer, att_response_size); 653b06f2579SMatthias Ringwald 654b06f2579SMatthias Ringwald // notify client about MTU exchange result 655b06f2579SMatthias Ringwald if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){ 656b6df12b6SMatthias Ringwald att_emit_mtu_event(att_connection->con_handle, att_connection->mtu); 657b06f2579SMatthias Ringwald } 658b06f2579SMatthias Ringwald return 1; 659b06f2579SMatthias Ringwald } 660b06f2579SMatthias Ringwald 661beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE 6629816429dSMatthias Ringwald uint8_t att_server_response_ready(hci_con_handle_t con_handle){ 66368591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 66468591e36SMatthias Ringwald if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 66568591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 6663f9b77dbSMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 667beb20288SMatthias Ringwald if (att_server->state != ATT_SERVER_RESPONSE_PENDING) return ERROR_CODE_COMMAND_DISALLOWED; 668e404a688SMatthias Ringwald 669e404a688SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 6703f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 671e404a688SMatthias Ringwald return ERROR_CODE_SUCCESS; 672e404a688SMatthias Ringwald } 673e404a688SMatthias Ringwald #endif 674e404a688SMatthias Ringwald 675cd45e970SMatthias Ringwald static void att_run_for_context(att_server_t * att_server, att_connection_t * att_connection){ 67618707219SMatthias Ringwald switch (att_server->state){ 6773deb3ec6SMatthias Ringwald case ATT_SERVER_REQUEST_RECEIVED: 67823079b39SMatthias Ringwald switch (att_server->bearer_type){ 67923079b39SMatthias Ringwald case ATT_BEARER_UNENHANCED_LE: 6800234fbbcSMatthias Ringwald // wait until re-encryption as central is complete 68123079b39SMatthias Ringwald if (gap_reconnect_security_setup_active(att_connection->con_handle)) { 68223079b39SMatthias Ringwald return; 68323079b39SMatthias Ringwald }; 68423079b39SMatthias Ringwald break; 6852954c397SMatthias Ringwald #if defined(ENABLE_GATT_OVER_CLASSIC) || defined (ENABLE_GATT_OVER_EATT) 68623079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 68723079b39SMatthias Ringwald case ATT_BEARER_UNENHANCED_CLASSIC: 6882954c397SMatthias Ringwald /* fall through */ 6892954c397SMatthias Ringwald #endif 6902954c397SMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT 6912954c397SMatthias Ringwald case ATT_BEARER_ENHANCED_LE: 6922954c397SMatthias Ringwald #endif 69323079b39SMatthias Ringwald // ok 69423079b39SMatthias Ringwald break; 69523079b39SMatthias Ringwald #endif 69623079b39SMatthias Ringwald default: 69723079b39SMatthias Ringwald btstack_unreachable(); 69823079b39SMatthias Ringwald break; 69948364364SMatthias Ringwald } 7000234fbbcSMatthias Ringwald 701760ad805SMatthias Ringwald // wait until pairing is complete 702760ad805SMatthias Ringwald if (att_server->pairing_active) break; 703760ad805SMatthias Ringwald 7047a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 70518707219SMatthias Ringwald if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){ 7063deb3ec6SMatthias Ringwald log_info("ATT Signed Write!"); 7073deb3ec6SMatthias Ringwald if (!sm_cmac_ready()) { 7083deb3ec6SMatthias Ringwald log_info("ATT Signed Write, sm_cmac engine not ready. Abort"); 70918707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 7103deb3ec6SMatthias Ringwald return; 7113deb3ec6SMatthias Ringwald } 71218707219SMatthias Ringwald if (att_server->request_size < (3 + 12)) { 7133deb3ec6SMatthias Ringwald log_info("ATT Signed Write, request to short. Abort."); 71418707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 7153deb3ec6SMatthias Ringwald return; 7163deb3ec6SMatthias Ringwald } 71718707219SMatthias Ringwald if (att_server->ir_lookup_active){ 7183deb3ec6SMatthias Ringwald return; 7193deb3ec6SMatthias Ringwald } 72018707219SMatthias Ringwald if (att_server->ir_le_device_db_index < 0){ 7213deb3ec6SMatthias Ringwald log_info("ATT Signed Write, CSRK not available"); 72218707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 7233deb3ec6SMatthias Ringwald return; 7243deb3ec6SMatthias Ringwald } 7253deb3ec6SMatthias Ringwald 7263deb3ec6SMatthias Ringwald // check counter 72718707219SMatthias Ringwald uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12); 72818707219SMatthias Ringwald uint32_t counter_db = le_device_db_remote_counter_get(att_server->ir_le_device_db_index); 7293deb3ec6SMatthias Ringwald log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet); 7303deb3ec6SMatthias Ringwald if (counter_packet < counter_db){ 7313deb3ec6SMatthias Ringwald log_info("ATT Signed Write, db reports higher counter, abort"); 73218707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 7333deb3ec6SMatthias Ringwald return; 7343deb3ec6SMatthias Ringwald } 7353deb3ec6SMatthias Ringwald 7363deb3ec6SMatthias Ringwald // signature is { sequence counter, secure hash } 7373deb3ec6SMatthias Ringwald sm_key_t csrk; 73818707219SMatthias Ringwald le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk); 73918707219SMatthias Ringwald att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION; 7403deb3ec6SMatthias Ringwald log_info("Orig Signature: "); 74118707219SMatthias Ringwald log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8); 74218707219SMatthias Ringwald uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1); 74318707219SMatthias 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); 7443deb3ec6SMatthias Ringwald return; 7453deb3ec6SMatthias Ringwald } 7467a766ebfSMatthias Ringwald #endif 747b06f2579SMatthias Ringwald // move on 74818707219SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 7493f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 750b06f2579SMatthias Ringwald break; 75188a48dd8SMatthias Ringwald 7523deb3ec6SMatthias Ringwald default: 7533deb3ec6SMatthias Ringwald break; 7543deb3ec6SMatthias Ringwald } 7553deb3ec6SMatthias Ringwald } 7563deb3ec6SMatthias Ringwald 7571979f09cSMatthias Ringwald static bool att_server_data_ready_for_phase(att_server_t * att_server, att_server_run_phase_t phase){ 75890f03c80SMatthias Ringwald switch (phase){ 75990f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_1_REQUESTS: 76090f03c80SMatthias Ringwald return att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 76190f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_2_INDICATIONS: 76238b893aaSMilanka Ringwald return (!btstack_linked_list_empty(&att_server->indication_requests) && (att_server->value_indication_handle == 0u)); 76390f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS: 76490f03c80SMatthias Ringwald return (!btstack_linked_list_empty(&att_server->notification_requests)); 7657bbeb3adSMilanka Ringwald default: 7667bbeb3adSMilanka Ringwald btstack_assert(false); 7671979f09cSMatthias Ringwald return false; 76890f03c80SMatthias Ringwald } 7697bbeb3adSMilanka Ringwald } 77090f03c80SMatthias Ringwald 7719d6c0193SMatthias 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){ 77290f03c80SMatthias Ringwald btstack_context_callback_registration_t * client; 77390f03c80SMatthias Ringwald switch (phase){ 77490f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_1_REQUESTS: 7750f70c52eSMatthias Ringwald att_server_process_validated_request(att_server, att_connection, NULL); 77690f03c80SMatthias Ringwald break; 77790f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_2_INDICATIONS: 77890f03c80SMatthias Ringwald client = (btstack_context_callback_registration_t*) att_server->indication_requests; 77990f03c80SMatthias Ringwald btstack_linked_list_remove(&att_server->indication_requests, (btstack_linked_item_t *) client); 78090f03c80SMatthias Ringwald client->callback(client->context); 78190f03c80SMatthias Ringwald break; 78290f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS: 78390f03c80SMatthias Ringwald client = (btstack_context_callback_registration_t*) att_server->notification_requests; 78490f03c80SMatthias Ringwald btstack_linked_list_remove(&att_server->notification_requests, (btstack_linked_item_t *) client); 78590f03c80SMatthias Ringwald client->callback(client->context); 78690f03c80SMatthias Ringwald break; 7877bbeb3adSMilanka Ringwald default: 7887bbeb3adSMilanka Ringwald btstack_assert(false); 7897bbeb3adSMilanka Ringwald break; 79090f03c80SMatthias Ringwald } 79190f03c80SMatthias Ringwald } 79290f03c80SMatthias Ringwald 7937eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){ 794b06f2579SMatthias Ringwald 7956438547aSMatthias Ringwald hci_con_handle_t last_send_con_handle = HCI_CON_HANDLE_INVALID; 79668591e36SMatthias Ringwald hci_connection_t * request_hci_connection = NULL; 7971979f09cSMatthias Ringwald bool can_send_now = true; 79838b893aaSMilanka Ringwald uint8_t phase_index; 7996438547aSMatthias Ringwald 8004395a000SMatthias Ringwald for (phase_index = ATT_SERVER_RUN_PHASE_1_REQUESTS; phase_index <= ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS; phase_index++){ 8014395a000SMatthias Ringwald att_server_run_phase_t phase = (att_server_run_phase_t) phase_index; 8026438547aSMatthias Ringwald hci_con_handle_t skip_connections_until = att_server_last_can_send_now; 803ff3cc4a5SMatthias Ringwald while (true){ 8046438547aSMatthias Ringwald btstack_linked_list_iterator_t it; 80518707219SMatthias Ringwald hci_connections_get_iterator(&it); 80618707219SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 80718707219SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 80818707219SMatthias Ringwald att_server_t * att_server = &connection->att_server; 809388fa7c4SMatthias Ringwald att_connection_t * att_connection = &connection->att_connection; 8106438547aSMatthias Ringwald 8111979f09cSMatthias Ringwald bool data_ready = att_server_data_ready_for_phase(att_server, phase); 8126438547aSMatthias Ringwald 813b6df12b6SMatthias 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); 8146438547aSMatthias Ringwald 8156438547aSMatthias Ringwald // skip until last sender found (which is also skipped) 8166438547aSMatthias Ringwald if (skip_connections_until != HCI_CON_HANDLE_INVALID){ 81768591e36SMatthias Ringwald if (data_ready && (request_hci_connection == NULL)){ 81868591e36SMatthias Ringwald request_hci_connection = connection; 8196438547aSMatthias Ringwald } 820b6df12b6SMatthias Ringwald if (skip_connections_until == att_connection->con_handle){ 8216438547aSMatthias Ringwald skip_connections_until = HCI_CON_HANDLE_INVALID; 8226438547aSMatthias Ringwald } 8236438547aSMatthias Ringwald continue; 8246438547aSMatthias Ringwald }; 8256438547aSMatthias Ringwald 82690f03c80SMatthias Ringwald if (data_ready){ 827969a5bbaSMatthias Ringwald if (can_send_now){ 8289d6c0193SMatthias Ringwald att_server_trigger_send_for_phase(att_server, att_connection, phase); 829b6df12b6SMatthias Ringwald last_send_con_handle = att_connection->con_handle; 83037a8059dSMatthias Ringwald can_send_now = att_server_can_send_packet(att_server, att_connection); 83190f03c80SMatthias Ringwald data_ready = att_server_data_ready_for_phase(att_server, phase); 83268591e36SMatthias Ringwald if (data_ready && (request_hci_connection == NULL)){ 83368591e36SMatthias Ringwald request_hci_connection = connection; 834cbbb12d9SMatthias Ringwald } 835cbbb12d9SMatthias Ringwald } else { 83668591e36SMatthias Ringwald request_hci_connection = connection; 837f29a88d8SMatthias Ringwald break; 838cbbb12d9SMatthias Ringwald } 839cbbb12d9SMatthias Ringwald } 8403deb3ec6SMatthias Ringwald } 8413deb3ec6SMatthias Ringwald 8426438547aSMatthias Ringwald // stop skipping (handles disconnect by last send connection) 8436438547aSMatthias Ringwald skip_connections_until = HCI_CON_HANDLE_INVALID; 8446438547aSMatthias Ringwald 8453551936eSMatthias Ringwald // Exit loop, if we cannot send 846969a5bbaSMatthias Ringwald if (!can_send_now) break; 847969a5bbaSMatthias Ringwald 848969a5bbaSMatthias Ringwald // Exit loop, if we can send but there are also no further request 84968591e36SMatthias Ringwald if (request_hci_connection == NULL) break; 850969a5bbaSMatthias Ringwald 851969a5bbaSMatthias Ringwald // Finally, if we still can send and there are requests, just try again 85268591e36SMatthias Ringwald request_hci_connection = NULL; 853969a5bbaSMatthias Ringwald } 8546438547aSMatthias Ringwald // update last send con handle for round robin 8556438547aSMatthias Ringwald if (last_send_con_handle != HCI_CON_HANDLE_INVALID){ 8566438547aSMatthias Ringwald att_server_last_can_send_now = last_send_con_handle; 8576438547aSMatthias Ringwald } 8583551936eSMatthias Ringwald } 859969a5bbaSMatthias Ringwald 86068591e36SMatthias Ringwald if (request_hci_connection == NULL) return; 8613f9b77dbSMatthias Ringwald 8623f9b77dbSMatthias Ringwald att_server_t * att_server = &request_hci_connection->att_server; 8633f9b77dbSMatthias Ringwald att_connection_t * att_connection = &request_hci_connection->att_connection; 8643f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 865969a5bbaSMatthias Ringwald } 866969a5bbaSMatthias Ringwald 86726b42dcfSMatthias Ringwald static void att_server_handle_att_pdu(att_server_t * att_server, att_connection_t * att_connection, uint8_t * packet, uint16_t size){ 86818707219SMatthias Ringwald 869838bd521SMatthias Ringwald uint8_t opcode = packet[0u]; 87038b893aaSMilanka Ringwald uint8_t method = opcode & 0x03fu; 871ed68ae9dSMilanka Ringwald bool invalid = method > ATT_MULTIPLE_HANDLE_VALUE_NTF; 87238b893aaSMilanka Ringwald bool command = (opcode & 0x40u) != 0u; 873838bd521SMatthias Ringwald 874838bd521SMatthias Ringwald // ignore invalid commands 875838bd521SMatthias Ringwald if (invalid && command){ 876838bd521SMatthias Ringwald return; 877838bd521SMatthias Ringwald } 878838bd521SMatthias Ringwald 8793deb3ec6SMatthias Ringwald // handle value indication confirms 88038b893aaSMilanka Ringwald if ((opcode == ATT_HANDLE_VALUE_CONFIRMATION) && (att_server->value_indication_handle != 0u)){ 88118707219SMatthias Ringwald btstack_run_loop_remove_timer(&att_server->value_indication_timer); 88218707219SMatthias Ringwald uint16_t att_handle = att_server->value_indication_handle; 88338b893aaSMilanka Ringwald att_server->value_indication_handle = 0u; 88438b893aaSMilanka Ringwald att_handle_value_indication_notify_client(0u, att_connection->con_handle, att_handle); 8853f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 8863deb3ec6SMatthias Ringwald return; 8873deb3ec6SMatthias Ringwald } 8883deb3ec6SMatthias Ringwald 8896428eb5aSMatthias Ringwald // directly process command 8906428eb5aSMatthias Ringwald // note: signed write cannot be handled directly as authentication needs to be verified 891838bd521SMatthias Ringwald if (opcode == ATT_WRITE_COMMAND){ 892b6df12b6SMatthias Ringwald att_handle_request(att_connection, packet, size, NULL); 8936428eb5aSMatthias Ringwald return; 8946428eb5aSMatthias Ringwald } 8956428eb5aSMatthias Ringwald 8963deb3ec6SMatthias Ringwald // check size 89718707219SMatthias Ringwald if (size > sizeof(att_server->request_buffer)) { 898e0463bdcSMatthias 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)); 8996428eb5aSMatthias Ringwald return; 9006428eb5aSMatthias Ringwald } 9013deb3ec6SMatthias Ringwald 902e0463bdcSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 903e0463bdcSMatthias Ringwald // abort signed write validation if a new request comes in (but finish previous signed write if possible) 904e0463bdcSMatthias Ringwald if (att_server->state == ATT_SERVER_W4_SIGNED_WRITE_VALIDATION){ 905e0463bdcSMatthias Ringwald if (packet[0] == ATT_SIGNED_WRITE_COMMAND){ 906e0463bdcSMatthias Ringwald log_info("skip new signed write request as previous is in validation"); 907e0463bdcSMatthias Ringwald return; 908e0463bdcSMatthias Ringwald } else { 909e0463bdcSMatthias Ringwald log_info("abort signed write validation to process new request"); 910e0463bdcSMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 911e0463bdcSMatthias Ringwald } 912e0463bdcSMatthias Ringwald } 913e0463bdcSMatthias Ringwald #endif 9143deb3ec6SMatthias Ringwald // last request still in processing? 91518707219SMatthias Ringwald if (att_server->state != ATT_SERVER_IDLE){ 916e0463bdcSMatthias Ringwald log_info("skip att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state); 9176428eb5aSMatthias Ringwald return; 9186428eb5aSMatthias Ringwald } 9193deb3ec6SMatthias Ringwald 9203deb3ec6SMatthias Ringwald // store request 92118707219SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED; 92218707219SMatthias Ringwald att_server->request_size = size; 9236535961aSMatthias Ringwald (void)memcpy(att_server->request_buffer, packet, size); 9243deb3ec6SMatthias Ringwald 925cd45e970SMatthias Ringwald att_run_for_context(att_server, att_connection); 92641772f37SMatthias Ringwald } 92741772f37SMatthias Ringwald 92852e211deSMatthias Ringwald static void att_server_dispatch_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 92968591e36SMatthias Ringwald hci_connection_t * hci_connection; 930b6df12b6SMatthias Ringwald att_connection_t * att_connection; 93126b42dcfSMatthias Ringwald att_server_t * att_server; 93252e211deSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 93352e211deSMatthias Ringwald hci_con_handle_t con_handle; 93452e211deSMatthias Ringwald bd_addr_t address; 93552e211deSMatthias Ringwald btstack_linked_list_iterator_t it; 93652e211deSMatthias Ringwald #endif 93741772f37SMatthias Ringwald 93841772f37SMatthias Ringwald switch (packet_type){ 93941772f37SMatthias Ringwald case HCI_EVENT_PACKET: 94041772f37SMatthias Ringwald switch (packet[0]){ 94152e211deSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 94252e211deSMatthias Ringwald case L2CAP_EVENT_CHANNEL_OPENED: 94352e211deSMatthias Ringwald con_handle = l2cap_event_channel_opened_get_handle(packet); 94452e211deSMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 94552e211deSMatthias Ringwald if (!hci_connection) break; 94652e211deSMatthias Ringwald att_server = &hci_connection->att_server; 94752e211deSMatthias Ringwald // store connection info 94852e211deSMatthias Ringwald att_server->bearer_type = ATT_BEARER_UNENHANCED_CLASSIC; 94952e211deSMatthias Ringwald att_server->peer_addr_type = BD_ADDR_TYPE_ACL; 95052e211deSMatthias Ringwald l2cap_event_channel_opened_get_address(packet, att_server->peer_address); 95152e211deSMatthias Ringwald att_connection = &hci_connection->att_connection; 95252e211deSMatthias Ringwald att_connection->con_handle = con_handle; 95352e211deSMatthias Ringwald // reset connection properties 95452e211deSMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 95552e211deSMatthias Ringwald att_connection->mtu = l2cap_event_channel_opened_get_remote_mtu(packet); 95652e211deSMatthias Ringwald att_connection->max_mtu = l2cap_max_mtu(); 95752e211deSMatthias Ringwald if (att_connection->max_mtu > ATT_REQUEST_BUFFER_SIZE){ 95852e211deSMatthias Ringwald att_connection->max_mtu = ATT_REQUEST_BUFFER_SIZE; 95952e211deSMatthias Ringwald } 96052e211deSMatthias Ringwald 96152e211deSMatthias Ringwald log_info("Connection opened %s, l2cap cid %04x, mtu %u", bd_addr_to_str(address), att_server->l2cap_cid, att_connection->mtu); 96252e211deSMatthias Ringwald 96352e211deSMatthias Ringwald // update security params 96452e211deSMatthias Ringwald att_connection->encryption_key_size = gap_encryption_key_size(con_handle); 96552e211deSMatthias Ringwald att_connection->authenticated = gap_authenticated(con_handle); 96652e211deSMatthias Ringwald att_connection->secure_connection = gap_secure_connection(con_handle); 96752e211deSMatthias Ringwald log_info("encrypted key size %u, authenticated %u, secure connection %u", 96852e211deSMatthias Ringwald att_connection->encryption_key_size, att_connection->authenticated, att_connection->secure_connection); 96952e211deSMatthias Ringwald 97052e211deSMatthias Ringwald // notify connection opened 97152e211deSMatthias Ringwald att_emit_connected_event(att_server, att_connection); 97252e211deSMatthias Ringwald 97352e211deSMatthias Ringwald // restore persisten ccc if encrypted 97452e211deSMatthias Ringwald if ( gap_security_level(con_handle) >= LEVEL_2){ 97552e211deSMatthias Ringwald att_server_persistent_ccc_restore(att_server, att_connection); 97652e211deSMatthias Ringwald } 97752e211deSMatthias Ringwald // TODO: what to do about le device db? 97852e211deSMatthias Ringwald att_server->pairing_active = 0; 97952e211deSMatthias Ringwald break; 98052e211deSMatthias Ringwald #endif 98141772f37SMatthias Ringwald case L2CAP_EVENT_CAN_SEND_NOW: 98241772f37SMatthias Ringwald att_server_handle_can_send_now(); 98341772f37SMatthias Ringwald break; 98441772f37SMatthias Ringwald case ATT_EVENT_MTU_EXCHANGE_COMPLETE: 98541772f37SMatthias Ringwald // GATT client has negotiated the mtu for this connection 98652e211deSMatthias Ringwald hci_connection = hci_connection_for_handle(channel); 98768591e36SMatthias Ringwald if (!hci_connection) break; 988388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 989b6df12b6SMatthias Ringwald att_connection->mtu = little_endian_read_16(packet, 4); 99041772f37SMatthias Ringwald break; 99141772f37SMatthias Ringwald default: 99241772f37SMatthias Ringwald break; 99341772f37SMatthias Ringwald } 99441772f37SMatthias Ringwald break; 99541772f37SMatthias Ringwald 99641772f37SMatthias Ringwald case ATT_DATA_PACKET: 99752e211deSMatthias Ringwald log_debug("ATT Packet, handle 0x%04x", channel); 99852e211deSMatthias Ringwald hci_connection = hci_connection_for_handle(channel); 99968591e36SMatthias Ringwald if (!hci_connection) break; 100041772f37SMatthias Ringwald 100126b42dcfSMatthias Ringwald att_server = &hci_connection->att_server; 100226b42dcfSMatthias Ringwald att_connection = &hci_connection->att_connection; 100326b42dcfSMatthias Ringwald att_server_handle_att_pdu(att_server, att_connection, packet, size); 1004372d62c4SMatthias Ringwald break; 10057bbeb3adSMilanka Ringwald 100652e211deSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 100752e211deSMatthias Ringwald case L2CAP_DATA_PACKET: 100852e211deSMatthias Ringwald hci_connections_get_iterator(&it); 100952e211deSMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 101052e211deSMatthias Ringwald hci_connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 101152e211deSMatthias Ringwald att_server = &hci_connection->att_server; 101252e211deSMatthias Ringwald att_connection = &hci_connection->att_connection; 101352e211deSMatthias Ringwald if (att_server->l2cap_cid == channel) { 101452e211deSMatthias Ringwald att_server_handle_att_pdu(att_server, att_connection, packet, size); 101552e211deSMatthias Ringwald break; 101652e211deSMatthias Ringwald } 101752e211deSMatthias Ringwald } 101852e211deSMatthias Ringwald break; 101952e211deSMatthias Ringwald #endif 102052e211deSMatthias Ringwald 10217bbeb3adSMilanka Ringwald default: 10227bbeb3adSMilanka Ringwald break; 1023372d62c4SMatthias Ringwald } 10243deb3ec6SMatthias Ringwald } 10253deb3ec6SMatthias Ringwald 1026bf78aac6SMatthias Ringwald // --------------------- 1027bf78aac6SMatthias Ringwald // persistent CCC writes 1028bf78aac6SMatthias Ringwald static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){ 10294ea43905SMatthias Ringwald return ('B' << 24u) | ('T' << 16u) | ('C' << 8u) | index; 1030bf78aac6SMatthias Ringwald } 1031bf78aac6SMatthias Ringwald 1032bf78aac6SMatthias Ringwald static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){ 1033bf78aac6SMatthias Ringwald // lookup att_server instance 103468591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 103568591e36SMatthias Ringwald if (!hci_connection) return; 103668591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 1037bf78aac6SMatthias Ringwald int le_device_index = att_server->ir_le_device_db_index; 1038bf78aac6SMatthias 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); 10396a540790SMatthias Ringwald 1040bf78aac6SMatthias Ringwald // check if bonded 1041bf78aac6SMatthias Ringwald if (le_device_index < 0) return; 10426a540790SMatthias Ringwald 1043bf78aac6SMatthias Ringwald // get btstack_tlv 1044bf78aac6SMatthias Ringwald const btstack_tlv_t * tlv_impl = NULL; 1045bf78aac6SMatthias Ringwald void * tlv_context; 1046bf78aac6SMatthias Ringwald btstack_tlv_get_instance(&tlv_impl, &tlv_context); 1047bf78aac6SMatthias Ringwald if (!tlv_impl) return; 10486a540790SMatthias Ringwald 1049bf78aac6SMatthias Ringwald // update ccc tag 1050bf78aac6SMatthias Ringwald int index; 10516a540790SMatthias Ringwald uint32_t highest_seq_nr = 0; 10526a540790SMatthias Ringwald uint32_t lowest_seq_nr = 0; 10536a540790SMatthias Ringwald uint32_t tag_for_lowest_seq_nr = 0; 10546a540790SMatthias Ringwald uint32_t tag_for_empty = 0; 10556a540790SMatthias Ringwald persistent_ccc_entry_t entry; 10566afb9acaSMatthias Ringwald for (index=0; index<NVN_NUM_GATT_SERVER_CCC; index++){ 1057bf78aac6SMatthias Ringwald uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 10586a540790SMatthias Ringwald int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 10596a540790SMatthias Ringwald 10606a540790SMatthias Ringwald // empty/invalid tag 10616a540790SMatthias Ringwald if (len != sizeof(persistent_ccc_entry_t)){ 10626a540790SMatthias Ringwald tag_for_empty = tag; 1063bf78aac6SMatthias Ringwald continue; 1064bf78aac6SMatthias Ringwald } 10656a540790SMatthias Ringwald // update highest seq nr 10666a540790SMatthias Ringwald if (entry.seq_nr > highest_seq_nr){ 10676a540790SMatthias Ringwald highest_seq_nr = entry.seq_nr; 10686a540790SMatthias Ringwald } 10696a540790SMatthias Ringwald // find entry with lowest seq nr 10704ea43905SMatthias Ringwald if ((tag_for_lowest_seq_nr == 0u) || (entry.seq_nr < lowest_seq_nr)){ 10716a540790SMatthias Ringwald tag_for_lowest_seq_nr = tag; 10726a540790SMatthias Ringwald lowest_seq_nr = entry.seq_nr; 10736a540790SMatthias Ringwald } 10746a540790SMatthias Ringwald 10756a540790SMatthias Ringwald if (entry.device_index != le_device_index) continue; 10766a540790SMatthias Ringwald if (entry.att_handle != att_handle) continue; 10776a540790SMatthias Ringwald 10786a540790SMatthias Ringwald // found matching entry 10799305033eSMatthias Ringwald if (value != 0){ 1080bf78aac6SMatthias Ringwald // update 10816a540790SMatthias Ringwald if (entry.value == value) { 1082760ad805SMatthias Ringwald log_info("CCC Index %u: Up-to-date", index); 1083760ad805SMatthias Ringwald return; 1084760ad805SMatthias Ringwald } 1085348fc806SMatthias Ringwald entry.value = (uint8_t) value; 10864ea43905SMatthias Ringwald entry.seq_nr = highest_seq_nr + 1u; 1087760ad805SMatthias Ringwald log_info("CCC Index %u: Store", index); 10880fe46bc1SMatthias Ringwald int result = tlv_impl->store_tag(tlv_context, tag, (const uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 10890fe46bc1SMatthias Ringwald if (result != 0){ 10900fe46bc1SMatthias Ringwald log_error("Store tag index %u failed", index); 10910fe46bc1SMatthias Ringwald } 109201ac2008SMatthias Ringwald } else { 109301ac2008SMatthias Ringwald // delete 1094760ad805SMatthias Ringwald log_info("CCC Index %u: Delete", index); 109501ac2008SMatthias Ringwald tlv_impl->delete_tag(tlv_context, tag); 109601ac2008SMatthias Ringwald } 1097bf78aac6SMatthias Ringwald return; 1098bf78aac6SMatthias Ringwald } 10996a540790SMatthias Ringwald 11000fe46bc1SMatthias Ringwald log_info("tag_for_empty %"PRIx32", tag_for_lowest_seq_nr %"PRIx32, tag_for_empty, tag_for_lowest_seq_nr); 11016a540790SMatthias Ringwald 11024ea43905SMatthias Ringwald if (value == 0u){ 11036a540790SMatthias Ringwald // done 11046a540790SMatthias Ringwald return; 11056a540790SMatthias Ringwald } 11066a540790SMatthias Ringwald 110738b893aaSMilanka Ringwald uint32_t tag_to_use = 0u; 110838b893aaSMilanka Ringwald if (tag_for_empty != 0u){ 11096a540790SMatthias Ringwald tag_to_use = tag_for_empty; 1110f688bdb8SMatthias Ringwald } else if (tag_for_lowest_seq_nr != 0){ 11116a540790SMatthias Ringwald tag_to_use = tag_for_lowest_seq_nr; 11126a540790SMatthias Ringwald } else { 11136a540790SMatthias Ringwald // should not happen 11146a540790SMatthias Ringwald return; 11156a540790SMatthias Ringwald } 1116bf78aac6SMatthias Ringwald // store ccc tag 11174ea43905SMatthias Ringwald entry.seq_nr = highest_seq_nr + 1u; 11186a540790SMatthias Ringwald entry.device_index = le_device_index; 11196a540790SMatthias Ringwald entry.att_handle = att_handle; 1120348fc806SMatthias Ringwald entry.value = (uint8_t) value; 11210fe46bc1SMatthias Ringwald int result = tlv_impl->store_tag(tlv_context, tag_to_use, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 11220fe46bc1SMatthias Ringwald if (result != 0){ 11230fe46bc1SMatthias Ringwald log_error("Store tag index %u failed", index); 11240fe46bc1SMatthias Ringwald } 1125bf78aac6SMatthias Ringwald } 1126bf78aac6SMatthias Ringwald 1127fe3d0cbaSMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server){ 1128760ad805SMatthias Ringwald int le_device_index = att_server->ir_le_device_db_index; 1129760ad805SMatthias Ringwald log_info("Clear CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index); 1130760ad805SMatthias Ringwald // check if bonded 1131760ad805SMatthias Ringwald if (le_device_index < 0) return; 1132760ad805SMatthias Ringwald // get btstack_tlv 1133760ad805SMatthias Ringwald const btstack_tlv_t * tlv_impl = NULL; 1134760ad805SMatthias Ringwald void * tlv_context; 1135760ad805SMatthias Ringwald btstack_tlv_get_instance(&tlv_impl, &tlv_context); 1136760ad805SMatthias Ringwald if (!tlv_impl) return; 1137760ad805SMatthias Ringwald // get all ccc tag 1138760ad805SMatthias Ringwald int index; 11396a540790SMatthias Ringwald persistent_ccc_entry_t entry; 11406afb9acaSMatthias Ringwald for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){ 1141760ad805SMatthias Ringwald uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 11426a540790SMatthias Ringwald int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 11436a540790SMatthias Ringwald if (len != sizeof(persistent_ccc_entry_t)) continue; 11446a540790SMatthias Ringwald if (entry.device_index != le_device_index) continue; 1145760ad805SMatthias Ringwald // delete entry 1146760ad805SMatthias Ringwald log_info("CCC Index %u: Delete", index); 1147760ad805SMatthias Ringwald tlv_impl->delete_tag(tlv_context, tag); 1148760ad805SMatthias Ringwald } 1149760ad805SMatthias Ringwald } 1150760ad805SMatthias Ringwald 11519a1f55eaSMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server, att_connection_t * att_connection){ 115201ac2008SMatthias Ringwald int le_device_index = att_server->ir_le_device_db_index; 115301ac2008SMatthias Ringwald log_info("Restore CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index); 11541b7acd0dSMatthias Ringwald // check if bonded 11551b7acd0dSMatthias Ringwald if (le_device_index < 0) return; 115601ac2008SMatthias Ringwald // get btstack_tlv 115701ac2008SMatthias Ringwald const btstack_tlv_t * tlv_impl = NULL; 115801ac2008SMatthias Ringwald void * tlv_context; 115901ac2008SMatthias Ringwald btstack_tlv_get_instance(&tlv_impl, &tlv_context); 116001ac2008SMatthias Ringwald if (!tlv_impl) return; 116101ac2008SMatthias Ringwald // get all ccc tag 116201ac2008SMatthias Ringwald int index; 11636a540790SMatthias Ringwald persistent_ccc_entry_t entry; 11646afb9acaSMatthias Ringwald for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){ 116501ac2008SMatthias Ringwald uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 11666a540790SMatthias Ringwald int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 11676a540790SMatthias Ringwald if (len != sizeof(persistent_ccc_entry_t)) continue; 11686a540790SMatthias Ringwald if (entry.device_index != le_device_index) continue; 116901ac2008SMatthias Ringwald // simulate write callback 11706a540790SMatthias Ringwald uint16_t attribute_handle = entry.att_handle; 117101ac2008SMatthias Ringwald uint8_t value[2]; 11726a540790SMatthias Ringwald little_endian_store_16(value, 0, entry.value); 117301ac2008SMatthias Ringwald att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle); 117401ac2008SMatthias Ringwald if (!callback) continue; 11756a540790SMatthias Ringwald log_info("CCC Index %u: Set Attribute handle 0x%04x to value 0x%04x", index, attribute_handle, entry.value ); 1176b6df12b6SMatthias Ringwald (*callback)(att_connection->con_handle, attribute_handle, ATT_TRANSACTION_MODE_NONE, 0, value, sizeof(value)); 117701ac2008SMatthias Ringwald } 117801ac2008SMatthias Ringwald } 117901ac2008SMatthias Ringwald 1180bf78aac6SMatthias Ringwald // persistent CCC writes 1181bf78aac6SMatthias Ringwald // --------------------- 11823d71c7a4SMatthias Ringwald 11833d71c7a4SMatthias Ringwald // gatt service management 11843d71c7a4SMatthias Ringwald static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){ 11853d71c7a4SMatthias Ringwald btstack_linked_list_iterator_t it; 11863d71c7a4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 11873d71c7a4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 11883d71c7a4SMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 11893d71c7a4SMatthias Ringwald if (handler->start_handle > handle) continue; 11903d71c7a4SMatthias Ringwald if (handler->end_handle < handle) continue; 11913d71c7a4SMatthias Ringwald return handler; 11923d71c7a4SMatthias Ringwald } 11933d71c7a4SMatthias Ringwald return NULL; 11943d71c7a4SMatthias Ringwald } 11953d71c7a4SMatthias Ringwald 11963d71c7a4SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){ 11973d71c7a4SMatthias Ringwald att_service_handler_t * handler = att_service_handler_for_handle(handle); 11989305033eSMatthias Ringwald if (handler != NULL) return handler->write_callback; 11993d71c7a4SMatthias Ringwald return att_server_client_write_callback; 12003d71c7a4SMatthias Ringwald } 12013d71c7a4SMatthias Ringwald 12025d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle){ 12035d07396bSMatthias Ringwald att_service_handler_t * handler = att_service_handler_for_handle(handle); 12049305033eSMatthias Ringwald if (handler != NULL) return handler->packet_handler; 12055d07396bSMatthias Ringwald return att_client_packet_handler; 12065d07396bSMatthias Ringwald } 12075d07396bSMatthias Ringwald 12083d71c7a4SMatthias Ringwald static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){ 12093d71c7a4SMatthias Ringwald // notify all callbacks 12103d71c7a4SMatthias Ringwald btstack_linked_list_iterator_t it; 12113d71c7a4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 12123d71c7a4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 12133d71c7a4SMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 12143d71c7a4SMatthias Ringwald if (!handler->write_callback) continue; 12153d71c7a4SMatthias Ringwald (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0); 12163d71c7a4SMatthias Ringwald } 12173d71c7a4SMatthias Ringwald if (!att_server_client_write_callback) return; 12183d71c7a4SMatthias Ringwald (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0); 12193d71c7a4SMatthias Ringwald } 12203d71c7a4SMatthias Ringwald 12213d71c7a4SMatthias Ringwald // returns first reported error or 0 12223d71c7a4SMatthias Ringwald static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){ 12233d71c7a4SMatthias Ringwald btstack_linked_list_iterator_t it; 12243d71c7a4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 12253d71c7a4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 12263d71c7a4SMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 12273d71c7a4SMatthias Ringwald if (!handler->write_callback) continue; 12283d71c7a4SMatthias Ringwald uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0); 122938b893aaSMilanka Ringwald if (error_code != 0u) return error_code; 12303d71c7a4SMatthias Ringwald } 12313d71c7a4SMatthias Ringwald if (!att_server_client_write_callback) return 0; 12323d71c7a4SMatthias Ringwald return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0); 12333d71c7a4SMatthias Ringwald } 12343d71c7a4SMatthias Ringwald 12353d71c7a4SMatthias 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){ 1236c61037ccSMatthias Ringwald att_service_handler_t * service = att_service_handler_for_handle(attribute_handle); 1237c61037ccSMatthias Ringwald att_read_callback_t read_callback = (service != NULL) ? service->read_callback : att_server_client_read_callback; 1238c61037ccSMatthias Ringwald uint16_t result = 0; 1239c61037ccSMatthias Ringwald if (read_callback != NULL){ 1240c61037ccSMatthias Ringwald result = (*read_callback)(con_handle, attribute_handle, offset, buffer, buffer_size); 1241c61037ccSMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE 1242c61037ccSMatthias Ringwald if (result == ATT_READ_RESPONSE_PENDING){ 1243c61037ccSMatthias Ringwald if (service == NULL){ 1244c61037ccSMatthias Ringwald att_server_flags |= ATT_SERVICE_FLAGS_DELAYED_RESPONSE; 1245c61037ccSMatthias Ringwald } else { 1246c61037ccSMatthias Ringwald service->flags |= ATT_SERVICE_FLAGS_DELAYED_RESPONSE; 1247c61037ccSMatthias Ringwald } 1248c61037ccSMatthias Ringwald } 1249c61037ccSMatthias Ringwald #endif 1250c61037ccSMatthias Ringwald } 1251c61037ccSMatthias Ringwald return result; 12523d71c7a4SMatthias Ringwald } 12533d71c7a4SMatthias Ringwald 12543d71c7a4SMatthias 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){ 12553d71c7a4SMatthias Ringwald switch (transaction_mode){ 12563d71c7a4SMatthias Ringwald case ATT_TRANSACTION_MODE_VALIDATE: 12573d71c7a4SMatthias Ringwald return att_validate_prepared_write(con_handle); 12583d71c7a4SMatthias Ringwald case ATT_TRANSACTION_MODE_EXECUTE: 12593d71c7a4SMatthias Ringwald case ATT_TRANSACTION_MODE_CANCEL: 12603d71c7a4SMatthias Ringwald att_notify_write_callbacks(con_handle, transaction_mode); 12613d71c7a4SMatthias Ringwald return 0; 12623d71c7a4SMatthias Ringwald default: 12633d71c7a4SMatthias Ringwald break; 12643d71c7a4SMatthias Ringwald } 12653d71c7a4SMatthias Ringwald 1266bf78aac6SMatthias Ringwald // track CCC writes 12674ea43905SMatthias Ringwald if (att_is_persistent_ccc(attribute_handle) && (offset == 0u) && (buffer_size == 2u)){ 1268bf78aac6SMatthias Ringwald att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0)); 1269bf78aac6SMatthias Ringwald } 1270bf78aac6SMatthias Ringwald 127101ac2008SMatthias Ringwald att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle); 127201ac2008SMatthias Ringwald if (!callback) return 0; 12733d71c7a4SMatthias Ringwald return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size); 12743d71c7a4SMatthias Ringwald } 12753d71c7a4SMatthias Ringwald 12763d71c7a4SMatthias Ringwald /** 12773d71c7a4SMatthias Ringwald * @brief register read/write callbacks for specific handle range 12783d71c7a4SMatthias Ringwald * @param att_service_handler_t 12793d71c7a4SMatthias Ringwald */ 12803d71c7a4SMatthias Ringwald void att_server_register_service_handler(att_service_handler_t * handler){ 1281ec8f5414SMilanka Ringwald bool att_server_registered = false; 1282f688bdb8SMatthias Ringwald if (att_service_handler_for_handle(handler->start_handle) != NULL){ 1283ec8f5414SMilanka Ringwald att_server_registered = true; 1284ec8f5414SMilanka Ringwald } 1285ec8f5414SMilanka Ringwald 1286f688bdb8SMatthias Ringwald if (att_service_handler_for_handle(handler->end_handle) != NULL){ 1287ec8f5414SMilanka Ringwald att_server_registered = true; 1288ec8f5414SMilanka Ringwald } 1289ec8f5414SMilanka Ringwald 1290ec8f5414SMilanka Ringwald if (att_server_registered){ 12913d71c7a4SMatthias Ringwald log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle); 12923d71c7a4SMatthias Ringwald return; 12933d71c7a4SMatthias Ringwald } 129485878f9dSMatthias Ringwald 129585878f9dSMatthias Ringwald handler->flags = 0; 12963d71c7a4SMatthias Ringwald btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler); 12973d71c7a4SMatthias Ringwald } 12983d71c7a4SMatthias Ringwald 12993deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){ 13003deb3ec6SMatthias Ringwald 13013d71c7a4SMatthias Ringwald // store callbacks 13023d71c7a4SMatthias Ringwald att_server_client_read_callback = read_callback; 13033d71c7a4SMatthias Ringwald att_server_client_write_callback = write_callback; 13043d71c7a4SMatthias Ringwald 13051a389cdcSMatthias Ringwald // register for HCI Events 130652e211deSMatthias Ringwald hci_event_callback_registration.callback = &att_server_event_packet_handler; 13071a389cdcSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 13081a389cdcSMatthias Ringwald 1309406722e4SMatthias Ringwald // register for SM events 131052e211deSMatthias Ringwald sm_event_callback_registration.callback = &att_server_event_packet_handler; 1311406722e4SMatthias Ringwald sm_add_event_handler(&sm_event_callback_registration); 13123deb3ec6SMatthias Ringwald 13131a389cdcSMatthias Ringwald // and L2CAP ATT Server PDUs 131452e211deSMatthias Ringwald att_dispatch_register_server(att_server_dispatch_packet_handler); 13153deb3ec6SMatthias Ringwald 131670dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 131770dca4d4SMatthias Ringwald // setup l2cap service 13186bf42d1fSMatthias Ringwald att_dispatch_classic_register_service(); 131970dca4d4SMatthias Ringwald #endif 132070dca4d4SMatthias Ringwald 13213deb3ec6SMatthias Ringwald att_set_db(db); 13223d71c7a4SMatthias Ringwald att_set_read_callback(att_server_read_callback); 13233d71c7a4SMatthias Ringwald att_set_write_callback(att_server_write_callback); 13243deb3ec6SMatthias Ringwald } 13253deb3ec6SMatthias Ringwald 13263deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){ 13273deb3ec6SMatthias Ringwald att_client_packet_handler = handler; 13283deb3ec6SMatthias Ringwald } 13293deb3ec6SMatthias Ringwald 1330cbbb12d9SMatthias Ringwald 1331cbbb12d9SMatthias Ringwald // to be deprecated 1332c141988cSMatthias Ringwald int att_server_can_send_packet_now(hci_con_handle_t con_handle){ 133368591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 133468591e36SMatthias Ringwald if (!hci_connection) return 0; 133537a8059dSMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 133637a8059dSMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 133737a8059dSMatthias Ringwald return att_server_can_send_packet(att_server, att_connection); 13381b96b007SMatthias Ringwald } 13391b96b007SMatthias Ringwald 13409816429dSMatthias Ringwald uint8_t att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 1341cbbb12d9SMatthias Ringwald return att_server_request_to_send_notification(callback_registration, con_handle); 13423deb3ec6SMatthias Ringwald } 13433deb3ec6SMatthias Ringwald 1344cbbb12d9SMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){ 1345cbbb12d9SMatthias Ringwald att_client_waiting_for_can_send_registration.callback = &att_emit_can_send_now_event; 1346cbbb12d9SMatthias Ringwald att_server_request_to_send_notification(&att_client_waiting_for_can_send_registration, con_handle); 1347cbbb12d9SMatthias Ringwald } 1348cbbb12d9SMatthias Ringwald // end of deprecated 1349cbbb12d9SMatthias Ringwald 13509816429dSMatthias Ringwald uint8_t att_server_request_to_send_notification(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 135168591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 135268591e36SMatthias Ringwald if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 135368591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 13543f9b77dbSMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 1355c37df6f6SMatthias Ringwald bool added = btstack_linked_list_add_tail(&att_server->notification_requests, (btstack_linked_item_t*) callback_registration); 13563f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 1357c37df6f6SMatthias Ringwald if (added){ 1358cbbb12d9SMatthias Ringwald return ERROR_CODE_SUCCESS; 1359c37df6f6SMatthias Ringwald } else { 1360c37df6f6SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1361c37df6f6SMatthias Ringwald } 1362cbbb12d9SMatthias Ringwald } 1363cbbb12d9SMatthias Ringwald 13649816429dSMatthias Ringwald uint8_t att_server_request_to_send_indication(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 136568591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 136668591e36SMatthias Ringwald if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 136768591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 13683f9b77dbSMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 1369c37df6f6SMatthias Ringwald bool added = btstack_linked_list_add_tail(&att_server->indication_requests, (btstack_linked_item_t*) callback_registration); 13703f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 1371c37df6f6SMatthias Ringwald if (added){ 1372969a5bbaSMatthias Ringwald return ERROR_CODE_SUCCESS; 1373c37df6f6SMatthias Ringwald } else { 1374c37df6f6SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1375c37df6f6SMatthias Ringwald } 1376bb38f057SMatthias Ringwald } 1377bb38f057SMatthias Ringwald 1378d74c7b2bSMatthias Ringwald static uint8_t att_server_prepare_server_message(hci_con_handle_t con_handle, att_server_t ** out_att_server, att_connection_t ** out_att_connection, uint8_t ** out_packet_buffer){ 1379d74c7b2bSMatthias Ringwald 1380d74c7b2bSMatthias Ringwald att_server_t * att_server = NULL; 1381d74c7b2bSMatthias Ringwald att_connection_t * att_connection = NULL; 1382d74c7b2bSMatthias Ringwald uint8_t * packet_buffer = NULL; 1383d74c7b2bSMatthias Ringwald 1384d74c7b2bSMatthias Ringwald // prefer enhanced bearer 1385d74c7b2bSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT 1386d74c7b2bSMatthias Ringwald att_server_eatt_bearer_t * eatt_bearer = att_server_eatt_bearer_for_con_handle(con_handle); 1387d74c7b2bSMatthias Ringwald if (eatt_bearer != NULL){ 1388d74c7b2bSMatthias Ringwald att_server = &eatt_bearer->att_server; 1389d74c7b2bSMatthias Ringwald att_connection = &eatt_bearer->att_connection; 1390d74c7b2bSMatthias Ringwald packet_buffer = eatt_bearer->send_buffer; 1391d74c7b2bSMatthias Ringwald } else 1392d74c7b2bSMatthias Ringwald #endif 1393d74c7b2bSMatthias Ringwald { 1394d74c7b2bSMatthias Ringwald hci_connection_t *hci_connection = hci_connection_for_handle(con_handle); 1395d74c7b2bSMatthias Ringwald if (hci_connection != NULL) { 1396d74c7b2bSMatthias Ringwald att_server = &hci_connection->att_server; 1397d74c7b2bSMatthias Ringwald att_connection = &hci_connection->att_connection; 1398d74c7b2bSMatthias Ringwald } 1399d74c7b2bSMatthias Ringwald } 1400d74c7b2bSMatthias Ringwald 1401d74c7b2bSMatthias Ringwald if (att_server == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1402d74c7b2bSMatthias Ringwald if (!att_server_can_send_packet(att_server, att_connection)) return BTSTACK_ACL_BUFFERS_FULL; 1403d74c7b2bSMatthias Ringwald 1404d74c7b2bSMatthias Ringwald if (packet_buffer == NULL){ 1405d74c7b2bSMatthias Ringwald l2cap_reserve_packet_buffer(); 1406d74c7b2bSMatthias Ringwald packet_buffer = l2cap_get_outgoing_buffer(); 1407d74c7b2bSMatthias Ringwald } 1408d74c7b2bSMatthias Ringwald 1409d74c7b2bSMatthias Ringwald *out_att_connection = att_connection; 1410d74c7b2bSMatthias Ringwald *out_att_server = att_server; 1411d74c7b2bSMatthias Ringwald *out_packet_buffer = packet_buffer; 1412d74c7b2bSMatthias Ringwald return ERROR_CODE_SUCCESS; 1413d74c7b2bSMatthias Ringwald } 1414d74c7b2bSMatthias Ringwald 14159816429dSMatthias Ringwald uint8_t att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){ 14165683a8e4SMatthias Ringwald att_server_t * att_server = NULL; 14175683a8e4SMatthias Ringwald att_connection_t * att_connection = NULL; 14185683a8e4SMatthias Ringwald uint8_t * packet_buffer = NULL; 141968591e36SMatthias Ringwald 14205683a8e4SMatthias Ringwald uint8_t status = att_server_prepare_server_message(con_handle, &att_server, &att_connection, &packet_buffer); 14215683a8e4SMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 14225683a8e4SMatthias Ringwald return status; 14235683a8e4SMatthias Ringwald } 14243deb3ec6SMatthias Ringwald 1425b6df12b6SMatthias Ringwald uint16_t size = att_prepare_handle_value_notification(att_connection, attribute_handle, value, value_len, packet_buffer); 1426d415948cSMatthias Ringwald 14270f70c52eSMatthias Ringwald return att_server_send_prepared(att_server, att_connection, NULL, size); 14283deb3ec6SMatthias Ringwald } 14293deb3ec6SMatthias Ringwald 143040f8d8f2SMatthias Ringwald /** 143140f8d8f2SMatthias Ringwald * @brief notify client about multiple attribute value changes 143240f8d8f2SMatthias Ringwald * @param con_handle 143340f8d8f2SMatthias Ringwald * @param num_attributes 143440f8d8f2SMatthias Ringwald * @param attribute_handles[] 143540f8d8f2SMatthias Ringwald * @param values_data[] 143640f8d8f2SMatthias Ringwald * @param values_len[] 143740f8d8f2SMatthias Ringwald * @return 0 if ok, error otherwise 143840f8d8f2SMatthias Ringwald */ 143940f8d8f2SMatthias Ringwald uint8_t att_server_multiple_notify(hci_con_handle_t con_handle, uint8_t num_attributes, 144040f8d8f2SMatthias Ringwald const uint16_t * attribute_handles, const uint8_t ** values_data, const uint16_t * values_len){ 144140f8d8f2SMatthias Ringwald 144240f8d8f2SMatthias Ringwald att_server_t * att_server = NULL; 144340f8d8f2SMatthias Ringwald att_connection_t * att_connection = NULL; 144440f8d8f2SMatthias Ringwald uint8_t * packet_buffer = NULL; 144540f8d8f2SMatthias Ringwald 1446d74c7b2bSMatthias Ringwald uint8_t status = att_server_prepare_server_message(con_handle, &att_server, &att_connection, &packet_buffer); 1447d74c7b2bSMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 1448d74c7b2bSMatthias Ringwald return status; 144940f8d8f2SMatthias Ringwald } 145040f8d8f2SMatthias Ringwald 145140f8d8f2SMatthias Ringwald uint16_t size = att_prepare_handle_value_multiple_notification(att_connection, num_attributes, attribute_handles, values_data, values_len, packet_buffer); 145240f8d8f2SMatthias Ringwald 145340f8d8f2SMatthias Ringwald return att_server_send_prepared(att_server, att_connection, packet_buffer, size); 145440f8d8f2SMatthias Ringwald } 145540f8d8f2SMatthias Ringwald 14569816429dSMatthias Ringwald uint8_t att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){ 145718707219SMatthias Ringwald 14585683a8e4SMatthias Ringwald att_server_t * att_server = NULL; 14595683a8e4SMatthias Ringwald att_connection_t * att_connection = NULL; 14605683a8e4SMatthias Ringwald uint8_t * packet_buffer = NULL; 14615683a8e4SMatthias Ringwald 14625683a8e4SMatthias Ringwald uint8_t status = att_server_prepare_server_message(con_handle, &att_server, &att_connection, &packet_buffer); 14635683a8e4SMatthias Ringwald if (status != ERROR_CODE_SUCCESS){ 14645683a8e4SMatthias Ringwald return status; 14655683a8e4SMatthias Ringwald } 14665683a8e4SMatthias Ringwald 14675683a8e4SMatthias Ringwald if (att_server->value_indication_handle != 0u) { 14685683a8e4SMatthias Ringwald // free reserved packet buffer 14695683a8e4SMatthias Ringwald if (att_server->bearer_type == ATT_BEARER_ENHANCED_LE){ 14705683a8e4SMatthias Ringwald l2cap_release_packet_buffer(); 14715683a8e4SMatthias Ringwald } 14725683a8e4SMatthias Ringwald return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS; 14735683a8e4SMatthias Ringwald } 14743deb3ec6SMatthias Ringwald 14753deb3ec6SMatthias Ringwald // track indication 147618707219SMatthias Ringwald att_server->value_indication_handle = attribute_handle; 147718707219SMatthias Ringwald btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout); 147818707219SMatthias Ringwald btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS); 147918707219SMatthias Ringwald btstack_run_loop_add_timer(&att_server->value_indication_timer); 14803deb3ec6SMatthias Ringwald 1481b6df12b6SMatthias Ringwald uint16_t size = att_prepare_handle_value_indication(att_connection, attribute_handle, value, value_len, packet_buffer); 148223079b39SMatthias Ringwald 14830f70c52eSMatthias Ringwald return att_server_send_prepared(att_server, att_connection, NULL, size); 14843deb3ec6SMatthias Ringwald } 14858f9132b5SMilanka Ringwald 14868f9132b5SMilanka Ringwald uint16_t att_server_get_mtu(hci_con_handle_t con_handle){ 148768591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 148868591e36SMatthias Ringwald if (!hci_connection) return 0; 1489388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 1490b6df12b6SMatthias Ringwald return att_connection->mtu; 14918f9132b5SMilanka Ringwald } 149235d7f2dfSMilanka Ringwald 149335d7f2dfSMilanka Ringwald void att_server_deinit(void){ 149435d7f2dfSMilanka Ringwald att_server_client_read_callback = NULL; 149535d7f2dfSMilanka Ringwald att_server_client_write_callback = NULL; 149635d7f2dfSMilanka Ringwald att_client_packet_handler = NULL; 149735d7f2dfSMilanka Ringwald service_handlers = NULL; 1498c61037ccSMatthias Ringwald att_server_flags = 0; 149935d7f2dfSMilanka Ringwald } 150020d4358dSMatthias Ringwald 150120d4358dSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT 15028d3bb68fSMatthias Ringwald 150327afb094SMatthias Ringwald #define MAX_NR_EATT_CHANNELS 5 150427afb094SMatthias Ringwald 150520d4358dSMatthias Ringwald static uint16_t att_server_eatt_receive_buffer_size; 15068d3bb68fSMatthias Ringwald static uint16_t att_server_eatt_send_buffer_size; 150720d4358dSMatthias Ringwald 1508523bb989SMatthias Ringwald static att_server_eatt_bearer_t * att_server_eatt_bearer_for_cid(uint16_t cid){ 1509523bb989SMatthias Ringwald btstack_linked_list_iterator_t it; 1510523bb989SMatthias Ringwald btstack_linked_list_iterator_init(&it, &att_server_eatt_bearer_active); 1511523bb989SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 1512523bb989SMatthias Ringwald att_server_eatt_bearer_t * eatt_bearer = (att_server_eatt_bearer_t *) btstack_linked_list_iterator_next(&it); 1513f786dd08SMatthias Ringwald if (eatt_bearer->att_server.l2cap_cid == cid) { 1514523bb989SMatthias Ringwald return eatt_bearer; 1515523bb989SMatthias Ringwald } 1516523bb989SMatthias Ringwald } 1517523bb989SMatthias Ringwald return NULL; 1518523bb989SMatthias Ringwald } 1519523bb989SMatthias Ringwald 152040f8d8f2SMatthias Ringwald static att_server_eatt_bearer_t * att_server_eatt_bearer_for_con_handle(hci_con_handle_t con_handle){ 152140f8d8f2SMatthias Ringwald btstack_linked_list_iterator_t it; 152240f8d8f2SMatthias Ringwald btstack_linked_list_iterator_init(&it, &att_server_eatt_bearer_active); 152340f8d8f2SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 152440f8d8f2SMatthias Ringwald att_server_eatt_bearer_t * eatt_bearer = (att_server_eatt_bearer_t *) btstack_linked_list_iterator_next(&it); 152540f8d8f2SMatthias Ringwald if (eatt_bearer->att_connection.con_handle == con_handle) { 152640f8d8f2SMatthias Ringwald return eatt_bearer; 152740f8d8f2SMatthias Ringwald } 152840f8d8f2SMatthias Ringwald } 152940f8d8f2SMatthias Ringwald return NULL; 153040f8d8f2SMatthias Ringwald } 153140f8d8f2SMatthias Ringwald 153220d4358dSMatthias Ringwald static void att_server_eatt_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 153320d4358dSMatthias Ringwald uint16_t cid; 153420d4358dSMatthias Ringwald uint8_t status; 153520d4358dSMatthias Ringwald uint16_t remote_mtu; 153620d4358dSMatthias Ringwald 153720d4358dSMatthias Ringwald uint8_t i; 153820d4358dSMatthias Ringwald uint8_t num_requested_bearers; 153920d4358dSMatthias Ringwald uint8_t num_accepted_bearers; 154027afb094SMatthias Ringwald uint16_t initial_credits = L2CAP_LE_AUTOMATIC_CREDITS; 154127afb094SMatthias Ringwald uint8_t * receive_buffers[MAX_NR_EATT_CHANNELS]; 154227afb094SMatthias Ringwald uint16_t cids[MAX_NR_EATT_CHANNELS]; 154327afb094SMatthias Ringwald att_server_eatt_bearer_t * eatt_bearers[MAX_NR_EATT_CHANNELS]; 1544523bb989SMatthias Ringwald att_server_eatt_bearer_t * eatt_bearer; 15452954c397SMatthias Ringwald att_server_t * att_server; 15462954c397SMatthias Ringwald att_connection_t * att_connection; 15479828b48aSMatthias Ringwald hci_con_handle_t con_handle; 15489828b48aSMatthias Ringwald hci_connection_t * hci_connection; 154920d4358dSMatthias Ringwald 155020d4358dSMatthias Ringwald switch (packet_type) { 155120d4358dSMatthias Ringwald 155220d4358dSMatthias Ringwald case L2CAP_DATA_PACKET: 1553f786dd08SMatthias Ringwald eatt_bearer = att_server_eatt_bearer_for_cid(channel); 1554f786dd08SMatthias Ringwald btstack_assert(eatt_bearer != NULL); 1555f786dd08SMatthias Ringwald att_server = &eatt_bearer->att_server; 1556f786dd08SMatthias Ringwald att_connection = &eatt_bearer->att_connection; 1557f786dd08SMatthias Ringwald att_server_handle_att_pdu(att_server, att_connection, packet, size); 155820d4358dSMatthias Ringwald break; 155920d4358dSMatthias Ringwald 156020d4358dSMatthias Ringwald case HCI_EVENT_PACKET: 156120d4358dSMatthias Ringwald switch (packet[0]) { 156220d4358dSMatthias Ringwald 156320d4358dSMatthias Ringwald case L2CAP_EVENT_CAN_SEND_NOW: 1564f786dd08SMatthias Ringwald cid = l2cap_event_packet_sent_get_local_cid(packet); 1565f786dd08SMatthias Ringwald eatt_bearer = att_server_eatt_bearer_for_cid(cid); 1566f786dd08SMatthias Ringwald btstack_assert(eatt_bearer != NULL); 1567f786dd08SMatthias Ringwald att_server = &eatt_bearer->att_server; 1568f786dd08SMatthias Ringwald att_connection = &eatt_bearer->att_connection; 1569f786dd08SMatthias Ringwald // only used for EATT request responses 1570f786dd08SMatthias Ringwald btstack_assert(att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED); 1571f786dd08SMatthias Ringwald att_server_process_validated_request(att_server, att_connection, eatt_bearer->send_buffer); 157220d4358dSMatthias Ringwald break; 157320d4358dSMatthias Ringwald 157420d4358dSMatthias Ringwald case L2CAP_EVENT_PACKET_SENT: 157520d4358dSMatthias Ringwald cid = l2cap_event_packet_sent_get_local_cid(packet); 1576be4ca859SMatthias Ringwald eatt_bearer = att_server_eatt_bearer_for_cid(cid); 1577f786dd08SMatthias Ringwald btstack_assert(eatt_bearer != NULL); 15782954c397SMatthias Ringwald att_server = &eatt_bearer->att_server; 15792954c397SMatthias Ringwald att_connection = &eatt_bearer->att_connection; 15802954c397SMatthias Ringwald att_server_handle_att_pdu(att_server, att_connection, packet, size); 158120d4358dSMatthias Ringwald break; 158220d4358dSMatthias Ringwald 158320d4358dSMatthias Ringwald case L2CAP_EVENT_ECBM_INCOMING_CONNECTION: 158420d4358dSMatthias Ringwald cid = l2cap_event_ecbm_incoming_connection_get_local_cid(packet); 15859828b48aSMatthias Ringwald 15869828b48aSMatthias Ringwald // reject if outgoing l2cap connection active, L2CAP/TIM/BV-01-C 15879828b48aSMatthias Ringwald con_handle = l2cap_event_ecbm_incoming_connection_get_handle(packet); 15889828b48aSMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 15899828b48aSMatthias Ringwald btstack_assert(hci_connection != NULL); 15909828b48aSMatthias Ringwald if (hci_connection->att_server.eatt_outgoing_active) { 15919828b48aSMatthias Ringwald hci_connection->att_server.incoming_connection_request = true; 15929828b48aSMatthias Ringwald l2cap_ecbm_decline_channels(cid, L2CAP_ECBM_CONNECTION_RESULT_SOME_REFUSED_INSUFFICIENT_RESOURCES_AVAILABLE ); 15939828b48aSMatthias Ringwald log_info("Decline incoming connection from %s", bd_addr_to_str(hci_connection->address)); 15949828b48aSMatthias Ringwald } else { 159520d4358dSMatthias Ringwald num_requested_bearers = l2cap_event_ecbm_incoming_connection_get_num_channels(packet); 159620d4358dSMatthias Ringwald for (i = 0; i < num_requested_bearers; i++){ 1597c2cbe117SMatthias Ringwald eatt_bearers[i] = (att_server_eatt_bearer_t *) btstack_linked_list_pop(&att_server_eatt_bearer_pool); 159820d4358dSMatthias Ringwald if (eatt_bearers[i] == NULL) { 159920d4358dSMatthias Ringwald break; 160020d4358dSMatthias Ringwald } 1601be4ca859SMatthias Ringwald eatt_bearers[i]->att_connection.con_handle = l2cap_event_ecbm_incoming_connection_get_handle(packet); 1602f786dd08SMatthias Ringwald eatt_bearers[i]->att_server.bearer_type = ATT_BEARER_ENHANCED_LE; 1603735b0222SMatthias Ringwald receive_buffers[i] = eatt_bearers[i]->receive_buffer; 16046b236cb0SMatthias Ringwald btstack_linked_list_add(&att_server_eatt_bearer_active, (btstack_linked_item_t *) eatt_bearers[i]); 160520d4358dSMatthias Ringwald } 160620d4358dSMatthias Ringwald num_accepted_bearers = i; 160720d4358dSMatthias Ringwald status = l2cap_ecbm_accept_channels(cid, num_accepted_bearers, initial_credits, att_server_eatt_receive_buffer_size, receive_buffers, cids); 160820d4358dSMatthias Ringwald btstack_assert(status == ERROR_CODE_SUCCESS); 1609c2cbe117SMatthias Ringwald log_info("requested %u, accepted %u", num_requested_bearers, num_accepted_bearers); 16106b236cb0SMatthias Ringwald for (i=0;i<num_accepted_bearers;i++){ 1611c2cbe117SMatthias Ringwald log_info("eatt l2cap cid: 0x%04x", cids[i]); 1612f786dd08SMatthias Ringwald eatt_bearers[i]->att_server.l2cap_cid = cids[i]; 16136b236cb0SMatthias Ringwald } 16149828b48aSMatthias Ringwald } 161520d4358dSMatthias Ringwald break; 161620d4358dSMatthias Ringwald 161720d4358dSMatthias Ringwald case L2CAP_EVENT_ECBM_CHANNEL_OPENED: 161820d4358dSMatthias Ringwald cid = l2cap_event_ecbm_channel_opened_get_local_cid(packet); 1619c2cbe117SMatthias Ringwald status = l2cap_event_ecbm_channel_opened_get_status(packet); 1620c2cbe117SMatthias Ringwald remote_mtu = l2cap_event_ecbm_channel_opened_get_remote_mtu(packet); 1621be4ca859SMatthias Ringwald eatt_bearer = att_server_eatt_bearer_for_cid(cid); 1622be4ca859SMatthias Ringwald btstack_assert(eatt_bearer != NULL); 1623c2cbe117SMatthias Ringwald eatt_bearer->att_connection.mtu_exchanged = true; 1624c2cbe117SMatthias Ringwald eatt_bearer->att_connection.mtu = remote_mtu; 1625be4ca859SMatthias Ringwald eatt_bearer->att_connection.max_mtu = remote_mtu; 162620d4358dSMatthias Ringwald log_info("L2CAP_EVENT_ECBM_CHANNEL_OPENED - cid 0x%04x mtu %u, status 0x%02x", cid, remote_mtu, status); 162720d4358dSMatthias Ringwald break; 162820d4358dSMatthias Ringwald 162920d4358dSMatthias Ringwald case L2CAP_EVENT_ECBM_RECONFIGURED: 163020d4358dSMatthias Ringwald break; 163120d4358dSMatthias Ringwald 1632523bb989SMatthias Ringwald case L2CAP_EVENT_CHANNEL_CLOSED: 1633523bb989SMatthias Ringwald eatt_bearer = att_server_eatt_bearer_for_cid(l2cap_event_channel_closed_get_local_cid(packet)); 1634370c8b58SMatthias Ringwald btstack_assert(eatt_bearer != NULL); 1635523bb989SMatthias Ringwald 1636523bb989SMatthias Ringwald // TODO: finalize - abort queued writes 1637523bb989SMatthias Ringwald 163827afb094SMatthias Ringwald btstack_linked_list_remove(&att_server_eatt_bearer_active, (btstack_linked_item_t *) eatt_bearer); 1639523bb989SMatthias Ringwald btstack_linked_list_add(&att_server_eatt_bearer_pool, (btstack_linked_item_t *) eatt_bearer); 1640523bb989SMatthias Ringwald break; 164120d4358dSMatthias Ringwald default: 164220d4358dSMatthias Ringwald break; 164320d4358dSMatthias Ringwald } 164427afb094SMatthias Ringwald break; 164527afb094SMatthias Ringwald default: 164627afb094SMatthias Ringwald btstack_unreachable(); 164727afb094SMatthias Ringwald break; 164820d4358dSMatthias Ringwald } 164920d4358dSMatthias Ringwald } 165020d4358dSMatthias Ringwald 165120d4358dSMatthias Ringwald // create eatt bearers 165220d4358dSMatthias Ringwald uint8_t att_server_eatt_init(uint8_t num_eatt_bearers, uint8_t * storage_buffer, uint16_t storage_size){ 165320d4358dSMatthias Ringwald uint16_t size_for_structs = num_eatt_bearers * sizeof(att_server_eatt_bearer_t); 165420d4358dSMatthias Ringwald if (storage_size < size_for_structs) { 165520d4358dSMatthias Ringwald return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED; 165620d4358dSMatthias Ringwald } 165720d4358dSMatthias Ringwald 165820d4358dSMatthias Ringwald // TODO: The minimum ATT_MTU for an Enhanced ATT bearer is 64 octets. 165920d4358dSMatthias Ringwald 166020d4358dSMatthias Ringwald memset(storage_buffer, 0, storage_size); 16618d3bb68fSMatthias Ringwald uint16_t buffer_size_per_bearer = ((storage_size - size_for_structs) / num_eatt_bearers); 16628d3bb68fSMatthias Ringwald att_server_eatt_receive_buffer_size = buffer_size_per_bearer / 2; 16638d3bb68fSMatthias Ringwald att_server_eatt_send_buffer_size = buffer_size_per_bearer / 2; 1664735b0222SMatthias Ringwald uint8_t * bearer_buffer = &storage_buffer[size_for_structs]; 166520d4358dSMatthias Ringwald uint8_t i; 166620d4358dSMatthias Ringwald att_server_eatt_bearer_t * eatt_bearer = (att_server_eatt_bearer_t *) storage_buffer; 1667be4ca859SMatthias Ringwald log_info("%u EATT bearers with receive buffer size %u", 1668be4ca859SMatthias Ringwald num_eatt_bearers, att_server_eatt_receive_buffer_size); 166920d4358dSMatthias Ringwald for (i=0;i<num_eatt_bearers;i++){ 1670be4ca859SMatthias Ringwald eatt_bearer->att_connection.con_handle = HCI_CON_HANDLE_INVALID; 1671735b0222SMatthias Ringwald eatt_bearer->receive_buffer = bearer_buffer; 1672735b0222SMatthias Ringwald bearer_buffer += att_server_eatt_receive_buffer_size; 16738d3bb68fSMatthias Ringwald eatt_bearer->send_buffer = bearer_buffer; 16748d3bb68fSMatthias Ringwald bearer_buffer += att_server_eatt_send_buffer_size; 16756b236cb0SMatthias Ringwald btstack_linked_list_add(&att_server_eatt_bearer_pool, (btstack_linked_item_t *) eatt_bearer); 167620d4358dSMatthias Ringwald eatt_bearer++; 167720d4358dSMatthias Ringwald } 167820d4358dSMatthias Ringwald // TODO: define minimum EATT MTU 1679*29f2ca21SMatthias Ringwald return l2cap_ecbm_register_service(att_server_eatt_handler, BLUETOOTH_PSM_EATT, 64, LEVEL_2, false); 168020d4358dSMatthias Ringwald } 168120d4358dSMatthias Ringwald #endif 1682