13deb3ec6SMatthias Ringwald /* 23deb3ec6SMatthias Ringwald * Copyright (C) 2014 BlueKitchen GmbH 33deb3ec6SMatthias Ringwald * 43deb3ec6SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 53deb3ec6SMatthias Ringwald * modification, are permitted provided that the following conditions 63deb3ec6SMatthias Ringwald * are met: 73deb3ec6SMatthias Ringwald * 83deb3ec6SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 93deb3ec6SMatthias Ringwald * notice, this list of conditions and the following disclaimer. 103deb3ec6SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 113deb3ec6SMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 123deb3ec6SMatthias Ringwald * documentation and/or other materials provided with the distribution. 133deb3ec6SMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 143deb3ec6SMatthias Ringwald * contributors may be used to endorse or promote products derived 153deb3ec6SMatthias Ringwald * from this software without specific prior written permission. 163deb3ec6SMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 173deb3ec6SMatthias Ringwald * personal benefit and not for any commercial purpose or for 183deb3ec6SMatthias Ringwald * monetary gain. 193deb3ec6SMatthias Ringwald * 203deb3ec6SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 213deb3ec6SMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 223deb3ec6SMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 232fca4dadSMilanka Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN 242fca4dadSMilanka Ringwald * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 253deb3ec6SMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 263deb3ec6SMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 273deb3ec6SMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 283deb3ec6SMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 293deb3ec6SMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 303deb3ec6SMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 313deb3ec6SMatthias Ringwald * SUCH DAMAGE. 323deb3ec6SMatthias Ringwald * 333deb3ec6SMatthias Ringwald * Please inquire about commercial licensing options at 343deb3ec6SMatthias Ringwald * [email protected] 353deb3ec6SMatthias Ringwald * 363deb3ec6SMatthias Ringwald */ 373deb3ec6SMatthias Ringwald 38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "att_server.c" 39ab2c6ae4SMatthias Ringwald 403deb3ec6SMatthias Ringwald 413deb3ec6SMatthias Ringwald // 423deb3ec6SMatthias Ringwald // ATT Server Globals 433deb3ec6SMatthias Ringwald // 443deb3ec6SMatthias Ringwald 453deb3ec6SMatthias Ringwald #include <stdint.h> 463deb3ec6SMatthias Ringwald #include <string.h> 473deb3ec6SMatthias Ringwald #include <inttypes.h> 483deb3ec6SMatthias Ringwald 497907f069SMatthias Ringwald #include "btstack_config.h" 503deb3ec6SMatthias Ringwald 51296289e3SMatthias Ringwald #include "ble/att_dispatch.h" 5259c6af15SMatthias Ringwald #include "ble/att_db.h" 5359c6af15SMatthias Ringwald #include "ble/att_server.h" 5459c6af15SMatthias Ringwald #include "ble/core.h" 5559c6af15SMatthias Ringwald #include "ble/le_device_db.h" 5659c6af15SMatthias Ringwald #include "ble/sm.h" 5716ece135SMatthias Ringwald #include "btstack_debug.h" 580e2df43fSMatthias Ringwald #include "btstack_event.h" 593deb3ec6SMatthias Ringwald #include "btstack_memory.h" 600e2df43fSMatthias Ringwald #include "btstack_run_loop.h" 6159c6af15SMatthias Ringwald #include "gap.h" 623deb3ec6SMatthias Ringwald #include "hci.h" 633deb3ec6SMatthias Ringwald #include "hci_dump.h" 643deb3ec6SMatthias Ringwald #include "l2cap.h" 65bf78aac6SMatthias Ringwald #include "btstack_tlv.h" 66d1a1f6a4SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 67d1a1f6a4SMatthias Ringwald #include "ble/sm.h" 68d1a1f6a4SMatthias Ringwald #endif 69bf78aac6SMatthias Ringwald 7003f53d81SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT 7103f53d81SMatthias Ringwald #include <stdio.h> 7203f53d81SMatthias Ringwald #endif 7303f53d81SMatthias Ringwald 746afb9acaSMatthias Ringwald #ifndef NVN_NUM_GATT_SERVER_CCC 756afb9acaSMatthias Ringwald #define NVN_NUM_GATT_SERVER_CCC 20 76bf78aac6SMatthias Ringwald #endif 773deb3ec6SMatthias Ringwald 7868591e36SMatthias Ringwald static void att_run_for_context(hci_connection_t * hci_connection); 7901ac2008SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle); 805d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle); 8148364364SMatthias Ringwald static void att_server_handle_can_send_now(void); 8268591e36SMatthias Ringwald static void att_server_persistent_ccc_restore(hci_connection_t * hci_connection); 8368591e36SMatthias Ringwald static void att_server_persistent_ccc_clear(hci_connection_t * hci_connection); 8468591e36SMatthias Ringwald static void att_server_handle_att_pdu(hci_connection_t * hci_connection, uint8_t * packet, uint16_t size); 853deb3ec6SMatthias Ringwald 863551936eSMatthias Ringwald typedef enum { 8738b893aaSMilanka Ringwald ATT_SERVER_RUN_PHASE_1_REQUESTS = 0, 883551936eSMatthias Ringwald ATT_SERVER_RUN_PHASE_2_INDICATIONS, 893551936eSMatthias Ringwald ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS, 903551936eSMatthias Ringwald } att_server_run_phase_t; 913551936eSMatthias Ringwald 926a540790SMatthias Ringwald // 936a540790SMatthias Ringwald typedef struct { 946a540790SMatthias Ringwald uint32_t seq_nr; 956a540790SMatthias Ringwald uint16_t att_handle; 966a540790SMatthias Ringwald uint8_t value; 976a540790SMatthias Ringwald uint8_t device_index; 986a540790SMatthias Ringwald } persistent_ccc_entry_t; 996a540790SMatthias Ringwald 10018707219SMatthias Ringwald // global 1011a389cdcSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 102406722e4SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration; 10335d7f2dfSMilanka Ringwald static btstack_packet_handler_t att_client_packet_handler; 1043d71c7a4SMatthias Ringwald static btstack_linked_list_t service_handlers; 1055f141511SMatthias Ringwald static btstack_context_callback_registration_t att_client_waiting_for_can_send_registration; 10618707219SMatthias Ringwald 1073d71c7a4SMatthias Ringwald static att_read_callback_t att_server_client_read_callback; 1083d71c7a4SMatthias Ringwald static att_write_callback_t att_server_client_write_callback; 1093d71c7a4SMatthias Ringwald 1106438547aSMatthias Ringwald // round robin 1116438547aSMatthias Ringwald static hci_con_handle_t att_server_last_can_send_now = HCI_CON_HANDLE_INVALID; 112bf78aac6SMatthias Ringwald 11302b78fc8SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 11468591e36SMatthias Ringwald static hci_connection_t * hci_connection_for_state(att_server_state_t state){ 11518707219SMatthias Ringwald btstack_linked_list_iterator_t it; 11618707219SMatthias Ringwald hci_connections_get_iterator(&it); 11718707219SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 11818707219SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 11918707219SMatthias Ringwald att_server_t * att_server = &connection->att_server; 12068591e36SMatthias Ringwald if (att_server->state == state) return connection; 12118707219SMatthias Ringwald } 12218707219SMatthias Ringwald return NULL; 12318707219SMatthias Ringwald } 12402b78fc8SMatthias Ringwald #endif 125bb38f057SMatthias Ringwald 126*3f9b77dbSMatthias Ringwald static void att_server_request_can_send_now(att_server_t * att_server, att_connection_t * att_connection ){ 12723079b39SMatthias Ringwald switch (att_server->bearer_type){ 12823079b39SMatthias Ringwald case ATT_BEARER_UNENHANCED_LE: 129b6df12b6SMatthias Ringwald att_dispatch_server_request_can_send_now_event(att_connection->con_handle); 13023079b39SMatthias Ringwald break; 13123079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 13223079b39SMatthias Ringwald case ATT_BEARER_UNENHANCED_CLASSIC: 13323079b39SMatthias Ringwald l2cap_request_can_send_now_event(att_server->l2cap_cid); 13423079b39SMatthias Ringwald break; 13523079b39SMatthias Ringwald #endif 13623079b39SMatthias Ringwald default: 13723079b39SMatthias Ringwald btstack_unreachable(); 13823079b39SMatthias Ringwald break; 13923079b39SMatthias Ringwald } 140c8c6e9b4SMatthias Ringwald } 141c8c6e9b4SMatthias Ringwald 1421979f09cSMatthias Ringwald static bool att_server_can_send_packet(hci_connection_t * hci_connection){ 143388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 14423079b39SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 14523079b39SMatthias Ringwald switch (att_server->bearer_type) { 14623079b39SMatthias Ringwald case ATT_BEARER_UNENHANCED_LE: 1471979f09cSMatthias Ringwald return att_dispatch_server_can_send_now(att_connection->con_handle) != 0; 14823079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 14923079b39SMatthias Ringwald case ATT_BEARER_UNENHANCED_CLASSIC: 15023079b39SMatthias Ringwald return l2cap_can_send_packet_now(att_server->l2cap_cid) != 0; 15123079b39SMatthias Ringwald #endif 15223079b39SMatthias Ringwald default: 15323079b39SMatthias Ringwald btstack_unreachable(); 15423079b39SMatthias Ringwald break; 15523079b39SMatthias Ringwald } 15623079b39SMatthias Ringwald return false; 157c0f26e1cSMatthias Ringwald } 158c0f26e1cSMatthias Ringwald 1593deb3ec6SMatthias Ringwald static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){ 1605d07396bSMatthias Ringwald btstack_packet_handler_t packet_handler = att_server_packet_handler_for_handle(attribute_handle); 1615d07396bSMatthias Ringwald if (!packet_handler) return; 1623deb3ec6SMatthias Ringwald 1633deb3ec6SMatthias Ringwald uint8_t event[7]; 1643deb3ec6SMatthias Ringwald int pos = 0; 1655611a760SMatthias Ringwald event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE; 1664ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 1673deb3ec6SMatthias Ringwald event[pos++] = status; 168f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, client_handle); 1693deb3ec6SMatthias Ringwald pos += 2; 170f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, attribute_handle); 171a444112bSMatthias Ringwald (*packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 1723deb3ec6SMatthias Ringwald } 1733deb3ec6SMatthias Ringwald 1745d07396bSMatthias Ringwald static void att_emit_event_to_all(const uint8_t * event, uint16_t size){ 1755d07396bSMatthias Ringwald // dispatch to app level handler 1769305033eSMatthias Ringwald if (att_client_packet_handler != NULL){ 1775d07396bSMatthias Ringwald (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size); 1785d07396bSMatthias Ringwald } 1793deb3ec6SMatthias Ringwald 1805d07396bSMatthias Ringwald // dispatch to service handlers 1815d07396bSMatthias Ringwald btstack_linked_list_iterator_t it; 1825d07396bSMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 1835d07396bSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1845d07396bSMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 1855d07396bSMatthias Ringwald if (!handler->packet_handler) continue; 1865d07396bSMatthias Ringwald (*handler->packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size); 1875d07396bSMatthias Ringwald } 1885d07396bSMatthias Ringwald } 1895d07396bSMatthias Ringwald 1905d07396bSMatthias Ringwald static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){ 1913deb3ec6SMatthias Ringwald uint8_t event[6]; 1923deb3ec6SMatthias Ringwald int pos = 0; 1935611a760SMatthias Ringwald event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE; 1944ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 195fc64f94aSMatthias Ringwald little_endian_store_16(event, pos, con_handle); 1963deb3ec6SMatthias Ringwald pos += 2; 197f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, mtu); 1985d07396bSMatthias Ringwald 1995d07396bSMatthias Ringwald // also dispatch to GATT Clients 200b12646c5SJakob Krantz att_dispatch_server_mtu_exchanged(con_handle, mtu); 2015d07396bSMatthias Ringwald 2025d07396bSMatthias Ringwald // dispatch to app level handler and service handlers 2035d07396bSMatthias Ringwald att_emit_event_to_all(&event[0], sizeof(event)); 2043deb3ec6SMatthias Ringwald } 2053deb3ec6SMatthias Ringwald 2065f141511SMatthias Ringwald static void att_emit_can_send_now_event(void * context){ 2075f141511SMatthias Ringwald UNUSED(context); 2083c97b242SMatthias Ringwald if (!att_client_packet_handler) return; 2093c97b242SMatthias Ringwald 2101b96b007SMatthias Ringwald uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0}; 2111b96b007SMatthias Ringwald (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 2121b96b007SMatthias Ringwald } 2131b96b007SMatthias Ringwald 21468591e36SMatthias Ringwald static void att_emit_connected_event(hci_connection_t * hci_connection){ 21568591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 216388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 217ff1bec95SMatthias Ringwald uint8_t event[11]; 218ff1bec95SMatthias Ringwald int pos = 0; 219ff1bec95SMatthias Ringwald event[pos++] = ATT_EVENT_CONNECTED; 2204ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 221ff1bec95SMatthias Ringwald event[pos++] = att_server->peer_addr_type; 222ff1bec95SMatthias Ringwald reverse_bd_addr(att_server->peer_address, &event[pos]); 223ff1bec95SMatthias Ringwald pos += 6; 224b6df12b6SMatthias Ringwald little_endian_store_16(event, pos, att_connection->con_handle); 225ff1bec95SMatthias Ringwald pos += 2; 226ff1bec95SMatthias Ringwald 227ff1bec95SMatthias Ringwald // dispatch to app level handler and service handlers 228ff1bec95SMatthias Ringwald att_emit_event_to_all(&event[0], sizeof(event)); 229ff1bec95SMatthias Ringwald } 230ff1bec95SMatthias Ringwald 231ff1bec95SMatthias Ringwald 2326187ad4cSMatthias Ringwald static void att_emit_disconnected_event(uint16_t con_handle){ 233ff1bec95SMatthias Ringwald uint8_t event[4]; 234ff1bec95SMatthias Ringwald int pos = 0; 235ff1bec95SMatthias Ringwald event[pos++] = ATT_EVENT_DISCONNECTED; 2364ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 2376187ad4cSMatthias Ringwald little_endian_store_16(event, pos, con_handle); 238ff1bec95SMatthias Ringwald pos += 2; 239ff1bec95SMatthias Ringwald 240ff1bec95SMatthias Ringwald // dispatch to app level handler and service handlers 241ff1bec95SMatthias Ringwald att_emit_event_to_all(&event[0], sizeof(event)); 242ff1bec95SMatthias Ringwald } 243ff1bec95SMatthias Ringwald 244ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){ 24554178543SMatthias Ringwald void * context = btstack_run_loop_get_timer_context(ts); 24654178543SMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context; 24768591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 24868591e36SMatthias Ringwald if (!hci_connection) return; 24968591e36SMatthias Ringwald // @note: after a transaction timeout, no more requests shall be sent over this ATT Bearer 250bce48a26SMatthias Ringwald // (that's why we don't reset the value_indication_handle) 251b6df12b6SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 25218707219SMatthias Ringwald uint16_t att_handle = att_server->value_indication_handle; 253388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 25438b893aaSMilanka Ringwald att_handle_value_indication_notify_client((uint8_t)ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_connection->con_handle, att_handle); 2553deb3ec6SMatthias Ringwald } 2563deb3ec6SMatthias Ringwald 2573deb3ec6SMatthias Ringwald static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2589ec2630cSMatthias Ringwald 2597dad9ff8SMatthias Ringwald UNUSED(channel); // ok: there is no channel 2607dad9ff8SMatthias Ringwald UNUSED(size); // ok: handling own l2cap events 2613deb3ec6SMatthias Ringwald 26218707219SMatthias Ringwald att_server_t * att_server; 263b6df12b6SMatthias Ringwald att_connection_t * att_connection; 26418707219SMatthias Ringwald hci_con_handle_t con_handle; 26568591e36SMatthias Ringwald hci_connection_t * hci_connection; 26670dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 26770dca4d4SMatthias Ringwald bd_addr_t address; 26868591e36SMatthias Ringwald btstack_linked_list_iterator_t it; 26970dca4d4SMatthias Ringwald #endif 27018707219SMatthias Ringwald 2713deb3ec6SMatthias Ringwald switch (packet_type) { 2723deb3ec6SMatthias Ringwald 2733deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 2740e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 2753deb3ec6SMatthias Ringwald 27670dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 27770dca4d4SMatthias Ringwald case L2CAP_EVENT_INCOMING_CONNECTION: 27870dca4d4SMatthias Ringwald l2cap_event_incoming_connection_get_address(packet, address); 27970dca4d4SMatthias Ringwald l2cap_accept_connection(channel); 28036de8547SMatthias Ringwald log_info("Accept incoming connection from %s", bd_addr_to_str(address)); 28170dca4d4SMatthias Ringwald break; 28270dca4d4SMatthias Ringwald case L2CAP_EVENT_CHANNEL_OPENED: 283122b723eSMatthias Ringwald con_handle = l2cap_event_channel_opened_get_handle(packet); 28468591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 28568591e36SMatthias Ringwald if (!hci_connection) break; 28668591e36SMatthias Ringwald att_server = &hci_connection->att_server; 287122b723eSMatthias Ringwald // store connection info 28823079b39SMatthias Ringwald att_server->bearer_type = ATT_BEARER_UNENHANCED_CLASSIC; 289f16129ceSMatthias Ringwald att_server->peer_addr_type = BD_ADDR_TYPE_ACL; 290122b723eSMatthias Ringwald l2cap_event_channel_opened_get_address(packet, att_server->peer_address); 291388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 292b6df12b6SMatthias Ringwald att_connection->con_handle = con_handle; 29370dca4d4SMatthias Ringwald att_server->l2cap_cid = l2cap_event_channel_opened_get_local_cid(packet); 294122b723eSMatthias Ringwald // reset connection properties 295122b723eSMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 296b6df12b6SMatthias Ringwald att_connection->mtu = l2cap_event_channel_opened_get_remote_mtu(packet); 297b6df12b6SMatthias Ringwald att_connection->max_mtu = l2cap_max_mtu(); 298b6df12b6SMatthias Ringwald if (att_connection->max_mtu > ATT_REQUEST_BUFFER_SIZE){ 299b6df12b6SMatthias Ringwald att_connection->max_mtu = ATT_REQUEST_BUFFER_SIZE; 300122b723eSMatthias Ringwald } 301df33ee14SMatthias Ringwald 302b6df12b6SMatthias Ringwald log_info("Connection opened %s, l2cap cid %04x, mtu %u", bd_addr_to_str(address), att_server->l2cap_cid, att_connection->mtu); 303df33ee14SMatthias Ringwald 304914988a1SMatthias Ringwald // update security params 305b6df12b6SMatthias Ringwald att_connection->encryption_key_size = gap_encryption_key_size(con_handle); 306b6df12b6SMatthias Ringwald att_connection->authenticated = gap_authenticated(con_handle); 307b6df12b6SMatthias Ringwald att_connection->secure_connection = gap_secure_connection(con_handle); 308914988a1SMatthias Ringwald log_info("encrypted key size %u, authenticated %u, secure connection %u", 309b6df12b6SMatthias Ringwald att_connection->encryption_key_size, att_connection->authenticated, att_connection->secure_connection); 310df33ee14SMatthias Ringwald 311df33ee14SMatthias Ringwald // notify connection opened 31268591e36SMatthias Ringwald att_emit_connected_event(hci_connection); 313df33ee14SMatthias Ringwald 314914988a1SMatthias Ringwald // restore persisten ccc if encrypted 315914988a1SMatthias Ringwald if ( gap_security_level(con_handle) >= LEVEL_2){ 31668591e36SMatthias Ringwald att_server_persistent_ccc_restore(hci_connection); 317914988a1SMatthias Ringwald } 318122b723eSMatthias Ringwald // TODO: what to do about le device db? 319122b723eSMatthias Ringwald att_server->pairing_active = 0; 32070dca4d4SMatthias Ringwald break; 32148364364SMatthias Ringwald case L2CAP_EVENT_CAN_SEND_NOW: 32248364364SMatthias Ringwald att_server_handle_can_send_now(); 32348364364SMatthias Ringwald break; 32448364364SMatthias Ringwald 32570dca4d4SMatthias Ringwald #endif 3263deb3ec6SMatthias Ringwald case HCI_EVENT_LE_META: 3273deb3ec6SMatthias Ringwald switch (packet[2]) { 3283deb3ec6SMatthias Ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 32918707219SMatthias Ringwald con_handle = little_endian_read_16(packet, 4); 33068591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 33168591e36SMatthias Ringwald if (!hci_connection) break; 33268591e36SMatthias Ringwald att_server = &hci_connection->att_server; 3333deb3ec6SMatthias Ringwald // store connection info 33418707219SMatthias Ringwald att_server->peer_addr_type = packet[7]; 33518707219SMatthias Ringwald reverse_bd_addr(&packet[8], att_server->peer_address); 336388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 337b6df12b6SMatthias Ringwald att_connection->con_handle = con_handle; 3383deb3ec6SMatthias Ringwald // reset connection properties 33918707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 34023079b39SMatthias Ringwald att_server->bearer_type = ATT_BEARER_UNENHANCED_LE; 341b6df12b6SMatthias Ringwald att_connection->mtu = ATT_DEFAULT_MTU; 342b6df12b6SMatthias Ringwald att_connection->max_mtu = l2cap_max_le_mtu(); 343b6df12b6SMatthias Ringwald if (att_connection->max_mtu > ATT_REQUEST_BUFFER_SIZE){ 344b6df12b6SMatthias Ringwald att_connection->max_mtu = ATT_REQUEST_BUFFER_SIZE; 34599c58ad0SMatthias Ringwald } 34638b893aaSMilanka Ringwald att_connection->encryption_key_size = 0u; 34738b893aaSMilanka Ringwald att_connection->authenticated = 0u; 34838b893aaSMilanka Ringwald att_connection->authorized = 0u; 349b2c1e52cSMatthias Ringwald // workaround: identity resolving can already be complete, at least store result 350b2c1e52cSMatthias Ringwald att_server->ir_le_device_db_index = sm_le_device_index(con_handle); 35138b893aaSMilanka Ringwald att_server->ir_lookup_active = 0u; 35238b893aaSMilanka Ringwald att_server->pairing_active = 0u; 353ff1bec95SMatthias Ringwald // notify all - old 3545d07396bSMatthias Ringwald att_emit_event_to_all(packet, size); 355ff1bec95SMatthias Ringwald // notify all - new 35668591e36SMatthias Ringwald att_emit_connected_event(hci_connection); 3573deb3ec6SMatthias Ringwald break; 3583deb3ec6SMatthias Ringwald 3593deb3ec6SMatthias Ringwald default: 3603deb3ec6SMatthias Ringwald break; 3613deb3ec6SMatthias Ringwald } 3623deb3ec6SMatthias Ringwald break; 3633deb3ec6SMatthias Ringwald 3643deb3ec6SMatthias Ringwald case HCI_EVENT_ENCRYPTION_CHANGE: 3658601e696SMatthias Ringwald case HCI_EVENT_ENCRYPTION_CHANGE_V2: 3663deb3ec6SMatthias Ringwald case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE: 3673deb3ec6SMatthias Ringwald // check handle 36818707219SMatthias Ringwald con_handle = little_endian_read_16(packet, 3); 36968591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 37068591e36SMatthias Ringwald if (!hci_connection) break; 371914988a1SMatthias Ringwald if (gap_get_connection_type(con_handle) != GAP_CONNECTION_LE) break; 372914988a1SMatthias Ringwald // update security params 37368591e36SMatthias Ringwald att_server = &hci_connection->att_server; 374388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 375b6df12b6SMatthias Ringwald att_connection->encryption_key_size = gap_encryption_key_size(con_handle); 376ff3f1026SMatthias Ringwald att_connection->authenticated = gap_authenticated(con_handle) ? 1 : 0; 377f461b3dfSMatthias Ringwald att_connection->secure_connection = gap_secure_connection(con_handle) ? 1 : 0; 378914988a1SMatthias Ringwald log_info("encrypted key size %u, authenticated %u, secure connection %u", 379b6df12b6SMatthias Ringwald att_connection->encryption_key_size, att_connection->authenticated, att_connection->secure_connection); 380914988a1SMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){ 3814097998aSMatthias Ringwald // restore CCC values when encrypted for LE Connections 38222bfc225SMilanka Ringwald if (hci_event_encryption_change_get_encryption_enabled(packet) != 0){ 38368591e36SMatthias Ringwald att_server_persistent_ccc_restore(hci_connection); 38401ac2008SMatthias Ringwald } 38501ac2008SMatthias Ringwald } 38668591e36SMatthias Ringwald att_run_for_context(hci_connection); 3873deb3ec6SMatthias Ringwald break; 3883deb3ec6SMatthias Ringwald 3893deb3ec6SMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 39070a390c7SMatthias Ringwald // check handle 39118707219SMatthias Ringwald con_handle = hci_event_disconnection_complete_get_connection_handle(packet); 39268591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 39368591e36SMatthias Ringwald if (!hci_connection) break; 39468591e36SMatthias Ringwald att_server = &hci_connection->att_server; 395388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 396b6df12b6SMatthias Ringwald att_clear_transaction_queue(att_connection); 397b6df12b6SMatthias Ringwald att_connection->con_handle = 0; 398760ad805SMatthias Ringwald att_server->pairing_active = 0; 39918707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 40038b893aaSMilanka Ringwald if (att_server->value_indication_handle != 0u){ 40127328ecbSMatthias Ringwald btstack_run_loop_remove_timer(&att_server->value_indication_timer); 402bce48a26SMatthias Ringwald uint16_t att_handle = att_server->value_indication_handle; 40338b893aaSMilanka Ringwald att_server->value_indication_handle = 0u; // reset error state 40438b893aaSMilanka Ringwald att_handle_value_indication_notify_client((uint8_t)ATT_HANDLE_VALUE_INDICATION_DISCONNECT, att_connection->con_handle, att_handle); 405bce48a26SMatthias Ringwald } 406ff1bec95SMatthias Ringwald // notify all - new 4076187ad4cSMatthias Ringwald att_emit_disconnected_event(con_handle); 408ff1bec95SMatthias Ringwald // notify all - old 4095d07396bSMatthias Ringwald att_emit_event_to_all(packet, size); 4103deb3ec6SMatthias Ringwald break; 4113deb3ec6SMatthias Ringwald 412760ad805SMatthias Ringwald // Identity Resolving 4135611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_STARTED: 41418707219SMatthias Ringwald con_handle = sm_event_identity_resolving_started_get_handle(packet); 41568591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 41668591e36SMatthias Ringwald if (!hci_connection) break; 41768591e36SMatthias Ringwald att_server = &hci_connection->att_server; 4185611a760SMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED"); 41930c8e114SMatthias Ringwald att_server->ir_lookup_active = 1; 4203deb3ec6SMatthias Ringwald break; 4215611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED: 422760ad805SMatthias Ringwald con_handle = sm_event_identity_created_get_handle(packet); 42368591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 42468591e36SMatthias Ringwald if (!hci_connection) return; 42568591e36SMatthias Ringwald att_server = &hci_connection->att_server; 42630c8e114SMatthias Ringwald att_server->ir_lookup_active = 0; 4271b7acd0dSMatthias Ringwald att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index(packet); 4281b7acd0dSMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED"); 42968591e36SMatthias Ringwald att_run_for_context(hci_connection); 4303deb3ec6SMatthias Ringwald break; 4315611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_FAILED: 43218707219SMatthias Ringwald con_handle = sm_event_identity_resolving_failed_get_handle(packet); 43368591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 43468591e36SMatthias Ringwald if (!hci_connection) break; 43568591e36SMatthias Ringwald att_server = &hci_connection->att_server; 4365611a760SMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED"); 43730c8e114SMatthias Ringwald att_server->ir_lookup_active = 0; 43818707219SMatthias Ringwald att_server->ir_le_device_db_index = -1; 43968591e36SMatthias Ringwald att_run_for_context(hci_connection); 4403deb3ec6SMatthias Ringwald break; 441760ad805SMatthias Ringwald 442760ad805SMatthias Ringwald // Pairing started - delete stored CCC values 443760ad805SMatthias Ringwald // - assumes pairing indicates either new device or re-pairing, in both cases there should be no stored CCC values 444760ad805SMatthias Ringwald // - assumes that all events have the con handle as the first field 445760ad805SMatthias Ringwald case SM_EVENT_JUST_WORKS_REQUEST: 446760ad805SMatthias Ringwald case SM_EVENT_PASSKEY_DISPLAY_NUMBER: 447760ad805SMatthias Ringwald case SM_EVENT_PASSKEY_INPUT_NUMBER: 448760ad805SMatthias Ringwald case SM_EVENT_NUMERIC_COMPARISON_REQUEST: 449760ad805SMatthias Ringwald con_handle = sm_event_just_works_request_get_handle(packet); 45068591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 45168591e36SMatthias Ringwald if (!hci_connection) break; 45268591e36SMatthias Ringwald att_server = &hci_connection->att_server; 453760ad805SMatthias Ringwald att_server->pairing_active = 1; 454760ad805SMatthias Ringwald log_info("SM Pairing started"); 455760ad805SMatthias Ringwald if (att_server->ir_le_device_db_index < 0) break; 45668591e36SMatthias Ringwald att_server_persistent_ccc_clear(hci_connection); 457760ad805SMatthias Ringwald // index not valid anymore 458760ad805SMatthias Ringwald att_server->ir_le_device_db_index = -1; 459760ad805SMatthias Ringwald break; 460760ad805SMatthias Ringwald 4611b7acd0dSMatthias Ringwald // Bonding completed 462760ad805SMatthias Ringwald case SM_EVENT_IDENTITY_CREATED: 463760ad805SMatthias Ringwald con_handle = sm_event_identity_created_get_handle(packet); 46468591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 46568591e36SMatthias Ringwald if (!hci_connection) return; 46668591e36SMatthias Ringwald att_server = &hci_connection->att_server; 467760ad805SMatthias Ringwald att_server->pairing_active = 0; 4681b7acd0dSMatthias Ringwald att_server->ir_le_device_db_index = sm_event_identity_created_get_index(packet); 46968591e36SMatthias Ringwald att_run_for_context(hci_connection); 4701b7acd0dSMatthias Ringwald break; 4711b7acd0dSMatthias Ringwald 4721b7acd0dSMatthias Ringwald // Pairing complete (with/without bonding=storing of pairing information) 4731b7acd0dSMatthias Ringwald case SM_EVENT_PAIRING_COMPLETE: 4741b7acd0dSMatthias Ringwald con_handle = sm_event_pairing_complete_get_handle(packet); 47568591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 47668591e36SMatthias Ringwald if (!hci_connection) return; 47768591e36SMatthias Ringwald att_server = &hci_connection->att_server; 4781b7acd0dSMatthias Ringwald att_server->pairing_active = 0; 47968591e36SMatthias Ringwald att_run_for_context(hci_connection); 480760ad805SMatthias Ringwald break; 481760ad805SMatthias Ringwald 482760ad805SMatthias Ringwald // Authorization 4835611a760SMatthias Ringwald case SM_EVENT_AUTHORIZATION_RESULT: { 48418707219SMatthias Ringwald con_handle = sm_event_authorization_result_get_handle(packet); 48568591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 48668591e36SMatthias Ringwald if (!hci_connection) break; 48768591e36SMatthias Ringwald att_server = &hci_connection->att_server; 488388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 489b6df12b6SMatthias Ringwald att_connection->authorized = sm_event_authorization_result_get_authorization_result(packet); 490*3f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 4913deb3ec6SMatthias Ringwald break; 4923deb3ec6SMatthias Ringwald } 4933deb3ec6SMatthias Ringwald default: 4943deb3ec6SMatthias Ringwald break; 4953deb3ec6SMatthias Ringwald } 49665b44ffdSMatthias Ringwald break; 49770dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 49870dca4d4SMatthias Ringwald case L2CAP_DATA_PACKET: 49968591e36SMatthias Ringwald hci_connections_get_iterator(&it); 50068591e36SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 501388fa7c4SMatthias Ringwald hci_connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 502388fa7c4SMatthias Ringwald att_server = &hci_connection->att_server; 50368591e36SMatthias Ringwald if (att_server->l2cap_cid == channel) { 504388fa7c4SMatthias Ringwald att_server_handle_att_pdu(hci_connection, packet, size); 50568591e36SMatthias Ringwald break; 50668591e36SMatthias Ringwald } 50768591e36SMatthias Ringwald } 50870dca4d4SMatthias Ringwald break; 50970dca4d4SMatthias Ringwald #endif 51041772f37SMatthias Ringwald 51165b44ffdSMatthias Ringwald default: 51265b44ffdSMatthias Ringwald break; 5133deb3ec6SMatthias Ringwald } 5143deb3ec6SMatthias Ringwald } 5153deb3ec6SMatthias Ringwald 516d415948cSMatthias Ringwald static uint8_t att_server_send_prepared(const att_server_t *att_server, const att_connection_t *att_connection, uint16_t size) { 517d415948cSMatthias Ringwald uint8_t status = ERROR_CODE_SUCCESS; 518d415948cSMatthias Ringwald switch (att_server->bearer_type) { 519d415948cSMatthias Ringwald case ATT_BEARER_UNENHANCED_LE: 520d415948cSMatthias Ringwald status = l2cap_send_prepared_connectionless(att_connection->con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); 521d415948cSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 522d415948cSMatthias Ringwald case ATT_BEARER_UNENHANCED_CLASSIC: 523d415948cSMatthias Ringwald status = l2cap_send_prepared(att_server->l2cap_cid, size); 524d415948cSMatthias Ringwald break; 525d415948cSMatthias Ringwald #endif 526d415948cSMatthias Ringwald default: 527d415948cSMatthias Ringwald btstack_unreachable(); 528d415948cSMatthias Ringwald break; 529d415948cSMatthias Ringwald } 530d415948cSMatthias Ringwald return status; 531d415948cSMatthias Ringwald } 532d415948cSMatthias Ringwald 5337a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 5343deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){ 5353deb3ec6SMatthias Ringwald 53668591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION); 53768591e36SMatthias Ringwald if (!hci_connection) return; 53868591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 539*3f9b77dbSMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 5403deb3ec6SMatthias Ringwald 5413deb3ec6SMatthias Ringwald uint8_t hash_flipped[8]; 5429c80e4ccSMatthias Ringwald reverse_64(hash, hash_flipped); 54318707219SMatthias Ringwald if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8)){ 5443deb3ec6SMatthias Ringwald log_info("ATT Signed Write, invalid signature"); 54503f53d81SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT 54603f53d81SMatthias Ringwald printf("ATT Signed Write, invalid signature\n"); 54703f53d81SMatthias Ringwald #endif 54818707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 5493deb3ec6SMatthias Ringwald return; 5503deb3ec6SMatthias Ringwald } 5513deb3ec6SMatthias Ringwald log_info("ATT Signed Write, valid signature"); 55203f53d81SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT 55303f53d81SMatthias Ringwald printf("ATT Signed Write, valid signature\n"); 55403f53d81SMatthias Ringwald #endif 5553deb3ec6SMatthias Ringwald 5563deb3ec6SMatthias Ringwald // update sequence number 55718707219SMatthias Ringwald uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12); 55818707219SMatthias Ringwald le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1); 55918707219SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 560*3f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 5613deb3ec6SMatthias Ringwald } 5627a766ebfSMatthias Ringwald #endif 56388a48dd8SMatthias Ringwald 56418707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED 565b06f2579SMatthias Ringwald // pre: can send now 566b06f2579SMatthias Ringwald // returns: 1 if packet was sent 56768591e36SMatthias Ringwald static int att_server_process_validated_request(hci_connection_t * hci_connection){ 56868591e36SMatthias Ringwald att_server_t * att_server = & hci_connection->att_server; 569388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 570b06f2579SMatthias Ringwald 571b06f2579SMatthias Ringwald l2cap_reserve_packet_buffer(); 572b06f2579SMatthias Ringwald uint8_t * att_response_buffer = l2cap_get_outgoing_buffer(); 573b6df12b6SMatthias Ringwald uint16_t att_response_size = att_handle_request(att_connection, att_server->request_buffer, att_server->request_size, att_response_buffer); 574b06f2579SMatthias Ringwald 575beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE 576c1ab6cc1SMatthias Ringwald if ((att_response_size == ATT_READ_RESPONSE_PENDING) || (att_response_size == ATT_INTERNAL_WRITE_RESPONSE_PENDING)){ 577e404a688SMatthias Ringwald // update state 578beb20288SMatthias Ringwald att_server->state = ATT_SERVER_RESPONSE_PENDING; 579e404a688SMatthias Ringwald 58056c3a347SMatthias Ringwald // callback with handle ATT_READ_RESPONSE_PENDING for reads 58156c3a347SMatthias Ringwald if (att_response_size == ATT_READ_RESPONSE_PENDING){ 582b6df12b6SMatthias Ringwald att_server_client_read_callback(att_connection->con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0); 58356c3a347SMatthias Ringwald } 584e8e7403eSMatthias Ringwald 585e8e7403eSMatthias Ringwald // free reserved buffer 586e8e7403eSMatthias Ringwald l2cap_release_packet_buffer(); 587e8e7403eSMatthias Ringwald return 0; 588e404a688SMatthias Ringwald } 589e404a688SMatthias Ringwald #endif 590e404a688SMatthias Ringwald 591b06f2579SMatthias Ringwald // intercept "insufficient authorization" for authenticated connections to allow for user authorization 5924ea43905SMatthias Ringwald if ((att_response_size >= 4u) 593b06f2579SMatthias Ringwald && (att_response_buffer[0] == ATT_ERROR_RESPONSE) 594b06f2579SMatthias Ringwald && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION) 59538b893aaSMilanka Ringwald && (att_connection->authenticated != 0u)){ 596b06f2579SMatthias Ringwald 597b6df12b6SMatthias Ringwald switch (gap_authorization_state(att_connection->con_handle)){ 598b06f2579SMatthias Ringwald case AUTHORIZATION_UNKNOWN: 599b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 600b6df12b6SMatthias Ringwald sm_request_pairing(att_connection->con_handle); 601b06f2579SMatthias Ringwald return 0; 602b06f2579SMatthias Ringwald case AUTHORIZATION_PENDING: 603b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 604b06f2579SMatthias Ringwald return 0; 605b06f2579SMatthias Ringwald default: 606b06f2579SMatthias Ringwald break; 607b06f2579SMatthias Ringwald } 608b06f2579SMatthias Ringwald } 609b06f2579SMatthias Ringwald 61018707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 6114ea43905SMatthias Ringwald if (att_response_size == 0u) { 612b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 613b06f2579SMatthias Ringwald return 0; 614b06f2579SMatthias Ringwald } 615b06f2579SMatthias Ringwald 616d415948cSMatthias Ringwald (void) att_server_send_prepared(att_server, att_connection, att_response_size); 617b06f2579SMatthias Ringwald 618b06f2579SMatthias Ringwald // notify client about MTU exchange result 619b06f2579SMatthias Ringwald if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){ 620b6df12b6SMatthias Ringwald att_emit_mtu_event(att_connection->con_handle, att_connection->mtu); 621b06f2579SMatthias Ringwald } 622b06f2579SMatthias Ringwald return 1; 623b06f2579SMatthias Ringwald } 624b06f2579SMatthias Ringwald 625beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE 6269816429dSMatthias Ringwald uint8_t att_server_response_ready(hci_con_handle_t con_handle){ 62768591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 62868591e36SMatthias Ringwald if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 62968591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 630*3f9b77dbSMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 631beb20288SMatthias Ringwald if (att_server->state != ATT_SERVER_RESPONSE_PENDING) return ERROR_CODE_COMMAND_DISALLOWED; 632e404a688SMatthias Ringwald 633e404a688SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 634*3f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 635e404a688SMatthias Ringwald return ERROR_CODE_SUCCESS; 636e404a688SMatthias Ringwald } 637e404a688SMatthias Ringwald #endif 638e404a688SMatthias Ringwald 63968591e36SMatthias Ringwald static void att_run_for_context(hci_connection_t * hci_connection){ 64068591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 641388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 64218707219SMatthias Ringwald switch (att_server->state){ 6433deb3ec6SMatthias Ringwald case ATT_SERVER_REQUEST_RECEIVED: 64423079b39SMatthias Ringwald switch (att_server->bearer_type){ 64523079b39SMatthias Ringwald case ATT_BEARER_UNENHANCED_LE: 6460234fbbcSMatthias Ringwald // wait until re-encryption as central is complete 64723079b39SMatthias Ringwald if (gap_reconnect_security_setup_active(att_connection->con_handle)) { 64823079b39SMatthias Ringwald return; 64923079b39SMatthias Ringwald }; 65023079b39SMatthias Ringwald break; 65123079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 65223079b39SMatthias Ringwald case ATT_BEARER_UNENHANCED_CLASSIC: 65323079b39SMatthias Ringwald // ok 65423079b39SMatthias Ringwald break; 65523079b39SMatthias Ringwald #endif 65623079b39SMatthias Ringwald default: 65723079b39SMatthias Ringwald btstack_unreachable(); 65823079b39SMatthias Ringwald break; 65948364364SMatthias Ringwald } 6600234fbbcSMatthias Ringwald 661760ad805SMatthias Ringwald // wait until pairing is complete 662760ad805SMatthias Ringwald if (att_server->pairing_active) break; 663760ad805SMatthias Ringwald 6647a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 66518707219SMatthias Ringwald if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){ 6663deb3ec6SMatthias Ringwald log_info("ATT Signed Write!"); 6673deb3ec6SMatthias Ringwald if (!sm_cmac_ready()) { 6683deb3ec6SMatthias Ringwald log_info("ATT Signed Write, sm_cmac engine not ready. Abort"); 66918707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 6703deb3ec6SMatthias Ringwald return; 6713deb3ec6SMatthias Ringwald } 67218707219SMatthias Ringwald if (att_server->request_size < (3 + 12)) { 6733deb3ec6SMatthias Ringwald log_info("ATT Signed Write, request to short. Abort."); 67418707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 6753deb3ec6SMatthias Ringwald return; 6763deb3ec6SMatthias Ringwald } 67718707219SMatthias Ringwald if (att_server->ir_lookup_active){ 6783deb3ec6SMatthias Ringwald return; 6793deb3ec6SMatthias Ringwald } 68018707219SMatthias Ringwald if (att_server->ir_le_device_db_index < 0){ 6813deb3ec6SMatthias Ringwald log_info("ATT Signed Write, CSRK not available"); 68218707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 6833deb3ec6SMatthias Ringwald return; 6843deb3ec6SMatthias Ringwald } 6853deb3ec6SMatthias Ringwald 6863deb3ec6SMatthias Ringwald // check counter 68718707219SMatthias Ringwald uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12); 68818707219SMatthias Ringwald uint32_t counter_db = le_device_db_remote_counter_get(att_server->ir_le_device_db_index); 6893deb3ec6SMatthias Ringwald log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet); 6903deb3ec6SMatthias Ringwald if (counter_packet < counter_db){ 6913deb3ec6SMatthias Ringwald log_info("ATT Signed Write, db reports higher counter, abort"); 69218707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 6933deb3ec6SMatthias Ringwald return; 6943deb3ec6SMatthias Ringwald } 6953deb3ec6SMatthias Ringwald 6963deb3ec6SMatthias Ringwald // signature is { sequence counter, secure hash } 6973deb3ec6SMatthias Ringwald sm_key_t csrk; 69818707219SMatthias Ringwald le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk); 69918707219SMatthias Ringwald att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION; 7003deb3ec6SMatthias Ringwald log_info("Orig Signature: "); 70118707219SMatthias Ringwald log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8); 70218707219SMatthias Ringwald uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1); 70318707219SMatthias 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); 7043deb3ec6SMatthias Ringwald return; 7053deb3ec6SMatthias Ringwald } 7067a766ebfSMatthias Ringwald #endif 707b06f2579SMatthias Ringwald // move on 70818707219SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 709*3f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 710b06f2579SMatthias Ringwald break; 71188a48dd8SMatthias Ringwald 7123deb3ec6SMatthias Ringwald default: 7133deb3ec6SMatthias Ringwald break; 7143deb3ec6SMatthias Ringwald } 7153deb3ec6SMatthias Ringwald } 7163deb3ec6SMatthias Ringwald 7171979f09cSMatthias Ringwald static bool att_server_data_ready_for_phase(att_server_t * att_server, att_server_run_phase_t phase){ 71890f03c80SMatthias Ringwald switch (phase){ 71990f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_1_REQUESTS: 72090f03c80SMatthias Ringwald return att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 72190f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_2_INDICATIONS: 72238b893aaSMilanka Ringwald return (!btstack_linked_list_empty(&att_server->indication_requests) && (att_server->value_indication_handle == 0u)); 72390f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS: 72490f03c80SMatthias Ringwald return (!btstack_linked_list_empty(&att_server->notification_requests)); 7257bbeb3adSMilanka Ringwald default: 7267bbeb3adSMilanka Ringwald btstack_assert(false); 7271979f09cSMatthias Ringwald return false; 72890f03c80SMatthias Ringwald } 7297bbeb3adSMilanka Ringwald } 73090f03c80SMatthias Ringwald 73168591e36SMatthias Ringwald static void att_server_trigger_send_for_phase(hci_connection_t * hci_connection, att_server_run_phase_t phase){ 73290f03c80SMatthias Ringwald btstack_context_callback_registration_t * client; 73368591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 73490f03c80SMatthias Ringwald switch (phase){ 73590f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_1_REQUESTS: 73668591e36SMatthias Ringwald att_server_process_validated_request(hci_connection); 73790f03c80SMatthias Ringwald break; 73890f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_2_INDICATIONS: 73990f03c80SMatthias Ringwald client = (btstack_context_callback_registration_t*) att_server->indication_requests; 74090f03c80SMatthias Ringwald btstack_linked_list_remove(&att_server->indication_requests, (btstack_linked_item_t *) client); 74190f03c80SMatthias Ringwald client->callback(client->context); 74290f03c80SMatthias Ringwald break; 74390f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS: 74490f03c80SMatthias Ringwald client = (btstack_context_callback_registration_t*) att_server->notification_requests; 74590f03c80SMatthias Ringwald btstack_linked_list_remove(&att_server->notification_requests, (btstack_linked_item_t *) client); 74690f03c80SMatthias Ringwald client->callback(client->context); 74790f03c80SMatthias Ringwald break; 7487bbeb3adSMilanka Ringwald default: 7497bbeb3adSMilanka Ringwald btstack_assert(false); 7507bbeb3adSMilanka Ringwald break; 75190f03c80SMatthias Ringwald } 75290f03c80SMatthias Ringwald } 75390f03c80SMatthias Ringwald 7547eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){ 755b06f2579SMatthias Ringwald 7566438547aSMatthias Ringwald hci_con_handle_t last_send_con_handle = HCI_CON_HANDLE_INVALID; 75768591e36SMatthias Ringwald hci_connection_t * request_hci_connection = NULL; 7581979f09cSMatthias Ringwald bool can_send_now = true; 75938b893aaSMilanka Ringwald uint8_t phase_index; 7606438547aSMatthias Ringwald 7614395a000SMatthias Ringwald for (phase_index = ATT_SERVER_RUN_PHASE_1_REQUESTS; phase_index <= ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS; phase_index++){ 7624395a000SMatthias Ringwald att_server_run_phase_t phase = (att_server_run_phase_t) phase_index; 7636438547aSMatthias Ringwald hci_con_handle_t skip_connections_until = att_server_last_can_send_now; 764ff3cc4a5SMatthias Ringwald while (true){ 7656438547aSMatthias Ringwald btstack_linked_list_iterator_t it; 76618707219SMatthias Ringwald hci_connections_get_iterator(&it); 76718707219SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 76818707219SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 76918707219SMatthias Ringwald att_server_t * att_server = &connection->att_server; 770388fa7c4SMatthias Ringwald att_connection_t * att_connection = &connection->att_connection; 7716438547aSMatthias Ringwald 7721979f09cSMatthias Ringwald bool data_ready = att_server_data_ready_for_phase(att_server, phase); 7736438547aSMatthias Ringwald 774b6df12b6SMatthias 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); 7756438547aSMatthias Ringwald 7766438547aSMatthias Ringwald // skip until last sender found (which is also skipped) 7776438547aSMatthias Ringwald if (skip_connections_until != HCI_CON_HANDLE_INVALID){ 77868591e36SMatthias Ringwald if (data_ready && (request_hci_connection == NULL)){ 77968591e36SMatthias Ringwald request_hci_connection = connection; 7806438547aSMatthias Ringwald } 781b6df12b6SMatthias Ringwald if (skip_connections_until == att_connection->con_handle){ 7826438547aSMatthias Ringwald skip_connections_until = HCI_CON_HANDLE_INVALID; 7836438547aSMatthias Ringwald } 7846438547aSMatthias Ringwald continue; 7856438547aSMatthias Ringwald }; 7866438547aSMatthias Ringwald 78790f03c80SMatthias Ringwald if (data_ready){ 788969a5bbaSMatthias Ringwald if (can_send_now){ 78968591e36SMatthias Ringwald att_server_trigger_send_for_phase(connection, phase); 790b6df12b6SMatthias Ringwald last_send_con_handle = att_connection->con_handle; 79168591e36SMatthias Ringwald can_send_now = att_server_can_send_packet(connection); 79290f03c80SMatthias Ringwald data_ready = att_server_data_ready_for_phase(att_server, phase); 79368591e36SMatthias Ringwald if (data_ready && (request_hci_connection == NULL)){ 79468591e36SMatthias Ringwald request_hci_connection = connection; 795cbbb12d9SMatthias Ringwald } 796cbbb12d9SMatthias Ringwald } else { 79768591e36SMatthias Ringwald request_hci_connection = connection; 798f29a88d8SMatthias Ringwald break; 799cbbb12d9SMatthias Ringwald } 800cbbb12d9SMatthias Ringwald } 8013deb3ec6SMatthias Ringwald } 8023deb3ec6SMatthias Ringwald 8036438547aSMatthias Ringwald // stop skipping (handles disconnect by last send connection) 8046438547aSMatthias Ringwald skip_connections_until = HCI_CON_HANDLE_INVALID; 8056438547aSMatthias Ringwald 8063551936eSMatthias Ringwald // Exit loop, if we cannot send 807969a5bbaSMatthias Ringwald if (!can_send_now) break; 808969a5bbaSMatthias Ringwald 809969a5bbaSMatthias Ringwald // Exit loop, if we can send but there are also no further request 81068591e36SMatthias Ringwald if (request_hci_connection == NULL) break; 811969a5bbaSMatthias Ringwald 812969a5bbaSMatthias Ringwald // Finally, if we still can send and there are requests, just try again 81368591e36SMatthias Ringwald request_hci_connection = NULL; 814969a5bbaSMatthias Ringwald } 8156438547aSMatthias Ringwald // update last send con handle for round robin 8166438547aSMatthias Ringwald if (last_send_con_handle != HCI_CON_HANDLE_INVALID){ 8176438547aSMatthias Ringwald att_server_last_can_send_now = last_send_con_handle; 8186438547aSMatthias Ringwald } 8193551936eSMatthias Ringwald } 820969a5bbaSMatthias Ringwald 82168591e36SMatthias Ringwald if (request_hci_connection == NULL) return; 822*3f9b77dbSMatthias Ringwald 823*3f9b77dbSMatthias Ringwald att_server_t * att_server = &request_hci_connection->att_server; 824*3f9b77dbSMatthias Ringwald att_connection_t * att_connection = &request_hci_connection->att_connection; 825*3f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 826969a5bbaSMatthias Ringwald } 827969a5bbaSMatthias Ringwald 82868591e36SMatthias Ringwald static void att_server_handle_att_pdu(hci_connection_t * hci_connection, uint8_t * packet, uint16_t size){ 82968591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 830388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 83118707219SMatthias Ringwald 832838bd521SMatthias Ringwald uint8_t opcode = packet[0u]; 83338b893aaSMilanka Ringwald uint8_t method = opcode & 0x03fu; 834ed68ae9dSMilanka Ringwald bool invalid = method > ATT_MULTIPLE_HANDLE_VALUE_NTF; 83538b893aaSMilanka Ringwald bool command = (opcode & 0x40u) != 0u; 836838bd521SMatthias Ringwald 837838bd521SMatthias Ringwald // ignore invalid commands 838838bd521SMatthias Ringwald if (invalid && command){ 839838bd521SMatthias Ringwald return; 840838bd521SMatthias Ringwald } 841838bd521SMatthias Ringwald 8423deb3ec6SMatthias Ringwald // handle value indication confirms 84338b893aaSMilanka Ringwald if ((opcode == ATT_HANDLE_VALUE_CONFIRMATION) && (att_server->value_indication_handle != 0u)){ 84418707219SMatthias Ringwald btstack_run_loop_remove_timer(&att_server->value_indication_timer); 84518707219SMatthias Ringwald uint16_t att_handle = att_server->value_indication_handle; 84638b893aaSMilanka Ringwald att_server->value_indication_handle = 0u; 84738b893aaSMilanka Ringwald att_handle_value_indication_notify_client(0u, att_connection->con_handle, att_handle); 848*3f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 8493deb3ec6SMatthias Ringwald return; 8503deb3ec6SMatthias Ringwald } 8513deb3ec6SMatthias Ringwald 8526428eb5aSMatthias Ringwald // directly process command 8536428eb5aSMatthias Ringwald // note: signed write cannot be handled directly as authentication needs to be verified 854838bd521SMatthias Ringwald if (opcode == ATT_WRITE_COMMAND){ 855b6df12b6SMatthias Ringwald att_handle_request(att_connection, packet, size, NULL); 8566428eb5aSMatthias Ringwald return; 8576428eb5aSMatthias Ringwald } 8586428eb5aSMatthias Ringwald 8593deb3ec6SMatthias Ringwald // check size 86018707219SMatthias Ringwald if (size > sizeof(att_server->request_buffer)) { 861e0463bdcSMatthias 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)); 8626428eb5aSMatthias Ringwald return; 8636428eb5aSMatthias Ringwald } 8643deb3ec6SMatthias Ringwald 865e0463bdcSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 866e0463bdcSMatthias Ringwald // abort signed write validation if a new request comes in (but finish previous signed write if possible) 867e0463bdcSMatthias Ringwald if (att_server->state == ATT_SERVER_W4_SIGNED_WRITE_VALIDATION){ 868e0463bdcSMatthias Ringwald if (packet[0] == ATT_SIGNED_WRITE_COMMAND){ 869e0463bdcSMatthias Ringwald log_info("skip new signed write request as previous is in validation"); 870e0463bdcSMatthias Ringwald return; 871e0463bdcSMatthias Ringwald } else { 872e0463bdcSMatthias Ringwald log_info("abort signed write validation to process new request"); 873e0463bdcSMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 874e0463bdcSMatthias Ringwald } 875e0463bdcSMatthias Ringwald } 876e0463bdcSMatthias Ringwald #endif 8773deb3ec6SMatthias Ringwald // last request still in processing? 87818707219SMatthias Ringwald if (att_server->state != ATT_SERVER_IDLE){ 879e0463bdcSMatthias Ringwald log_info("skip att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state); 8806428eb5aSMatthias Ringwald return; 8816428eb5aSMatthias Ringwald } 8823deb3ec6SMatthias Ringwald 8833deb3ec6SMatthias Ringwald // store request 88418707219SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED; 88518707219SMatthias Ringwald att_server->request_size = size; 8866535961aSMatthias Ringwald (void)memcpy(att_server->request_buffer, packet, size); 8873deb3ec6SMatthias Ringwald 88868591e36SMatthias Ringwald att_run_for_context(hci_connection); 88941772f37SMatthias Ringwald } 89041772f37SMatthias Ringwald 89141772f37SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){ 89268591e36SMatthias Ringwald hci_connection_t * hci_connection; 893b6df12b6SMatthias Ringwald att_connection_t * att_connection; 89441772f37SMatthias Ringwald 89541772f37SMatthias Ringwald switch (packet_type){ 89641772f37SMatthias Ringwald case HCI_EVENT_PACKET: 89741772f37SMatthias Ringwald switch (packet[0]){ 89841772f37SMatthias Ringwald case L2CAP_EVENT_CAN_SEND_NOW: 89941772f37SMatthias Ringwald att_server_handle_can_send_now(); 90041772f37SMatthias Ringwald break; 90141772f37SMatthias Ringwald case ATT_EVENT_MTU_EXCHANGE_COMPLETE: 90241772f37SMatthias Ringwald // GATT client has negotiated the mtu for this connection 90368591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(handle); 90468591e36SMatthias Ringwald if (!hci_connection) break; 905388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 906b6df12b6SMatthias Ringwald att_connection->mtu = little_endian_read_16(packet, 4); 90741772f37SMatthias Ringwald break; 90841772f37SMatthias Ringwald default: 90941772f37SMatthias Ringwald break; 91041772f37SMatthias Ringwald } 91141772f37SMatthias Ringwald break; 91241772f37SMatthias Ringwald 91341772f37SMatthias Ringwald case ATT_DATA_PACKET: 91441772f37SMatthias Ringwald log_debug("ATT Packet, handle 0x%04x", handle); 91568591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(handle); 91668591e36SMatthias Ringwald if (!hci_connection) break; 91741772f37SMatthias Ringwald 91868591e36SMatthias Ringwald att_server_handle_att_pdu(hci_connection, packet, size); 919372d62c4SMatthias Ringwald break; 9207bbeb3adSMilanka Ringwald 9217bbeb3adSMilanka Ringwald default: 9227bbeb3adSMilanka Ringwald break; 923372d62c4SMatthias Ringwald } 9243deb3ec6SMatthias Ringwald } 9253deb3ec6SMatthias Ringwald 926bf78aac6SMatthias Ringwald // --------------------- 927bf78aac6SMatthias Ringwald // persistent CCC writes 928bf78aac6SMatthias Ringwald static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){ 9294ea43905SMatthias Ringwald return ('B' << 24u) | ('T' << 16u) | ('C' << 8u) | index; 930bf78aac6SMatthias Ringwald } 931bf78aac6SMatthias Ringwald 932bf78aac6SMatthias Ringwald static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){ 933bf78aac6SMatthias Ringwald // lookup att_server instance 93468591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 93568591e36SMatthias Ringwald if (!hci_connection) return; 93668591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 937bf78aac6SMatthias Ringwald int le_device_index = att_server->ir_le_device_db_index; 938bf78aac6SMatthias 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); 9396a540790SMatthias Ringwald 940bf78aac6SMatthias Ringwald // check if bonded 941bf78aac6SMatthias Ringwald if (le_device_index < 0) return; 9426a540790SMatthias Ringwald 943bf78aac6SMatthias Ringwald // get btstack_tlv 944bf78aac6SMatthias Ringwald const btstack_tlv_t * tlv_impl = NULL; 945bf78aac6SMatthias Ringwald void * tlv_context; 946bf78aac6SMatthias Ringwald btstack_tlv_get_instance(&tlv_impl, &tlv_context); 947bf78aac6SMatthias Ringwald if (!tlv_impl) return; 9486a540790SMatthias Ringwald 949bf78aac6SMatthias Ringwald // update ccc tag 950bf78aac6SMatthias Ringwald int index; 9516a540790SMatthias Ringwald uint32_t highest_seq_nr = 0; 9526a540790SMatthias Ringwald uint32_t lowest_seq_nr = 0; 9536a540790SMatthias Ringwald uint32_t tag_for_lowest_seq_nr = 0; 9546a540790SMatthias Ringwald uint32_t tag_for_empty = 0; 9556a540790SMatthias Ringwald persistent_ccc_entry_t entry; 9566afb9acaSMatthias Ringwald for (index=0; index<NVN_NUM_GATT_SERVER_CCC; index++){ 957bf78aac6SMatthias Ringwald uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 9586a540790SMatthias Ringwald int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 9596a540790SMatthias Ringwald 9606a540790SMatthias Ringwald // empty/invalid tag 9616a540790SMatthias Ringwald if (len != sizeof(persistent_ccc_entry_t)){ 9626a540790SMatthias Ringwald tag_for_empty = tag; 963bf78aac6SMatthias Ringwald continue; 964bf78aac6SMatthias Ringwald } 9656a540790SMatthias Ringwald // update highest seq nr 9666a540790SMatthias Ringwald if (entry.seq_nr > highest_seq_nr){ 9676a540790SMatthias Ringwald highest_seq_nr = entry.seq_nr; 9686a540790SMatthias Ringwald } 9696a540790SMatthias Ringwald // find entry with lowest seq nr 9704ea43905SMatthias Ringwald if ((tag_for_lowest_seq_nr == 0u) || (entry.seq_nr < lowest_seq_nr)){ 9716a540790SMatthias Ringwald tag_for_lowest_seq_nr = tag; 9726a540790SMatthias Ringwald lowest_seq_nr = entry.seq_nr; 9736a540790SMatthias Ringwald } 9746a540790SMatthias Ringwald 9756a540790SMatthias Ringwald if (entry.device_index != le_device_index) continue; 9766a540790SMatthias Ringwald if (entry.att_handle != att_handle) continue; 9776a540790SMatthias Ringwald 9786a540790SMatthias Ringwald // found matching entry 9799305033eSMatthias Ringwald if (value != 0){ 980bf78aac6SMatthias Ringwald // update 9816a540790SMatthias Ringwald if (entry.value == value) { 982760ad805SMatthias Ringwald log_info("CCC Index %u: Up-to-date", index); 983760ad805SMatthias Ringwald return; 984760ad805SMatthias Ringwald } 985348fc806SMatthias Ringwald entry.value = (uint8_t) value; 9864ea43905SMatthias Ringwald entry.seq_nr = highest_seq_nr + 1u; 987760ad805SMatthias Ringwald log_info("CCC Index %u: Store", index); 9880fe46bc1SMatthias Ringwald int result = tlv_impl->store_tag(tlv_context, tag, (const uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 9890fe46bc1SMatthias Ringwald if (result != 0){ 9900fe46bc1SMatthias Ringwald log_error("Store tag index %u failed", index); 9910fe46bc1SMatthias Ringwald } 99201ac2008SMatthias Ringwald } else { 99301ac2008SMatthias Ringwald // delete 994760ad805SMatthias Ringwald log_info("CCC Index %u: Delete", index); 99501ac2008SMatthias Ringwald tlv_impl->delete_tag(tlv_context, tag); 99601ac2008SMatthias Ringwald } 997bf78aac6SMatthias Ringwald return; 998bf78aac6SMatthias Ringwald } 9996a540790SMatthias Ringwald 10000fe46bc1SMatthias Ringwald log_info("tag_for_empty %"PRIx32", tag_for_lowest_seq_nr %"PRIx32, tag_for_empty, tag_for_lowest_seq_nr); 10016a540790SMatthias Ringwald 10024ea43905SMatthias Ringwald if (value == 0u){ 10036a540790SMatthias Ringwald // done 10046a540790SMatthias Ringwald return; 10056a540790SMatthias Ringwald } 10066a540790SMatthias Ringwald 100738b893aaSMilanka Ringwald uint32_t tag_to_use = 0u; 100838b893aaSMilanka Ringwald if (tag_for_empty != 0u){ 10096a540790SMatthias Ringwald tag_to_use = tag_for_empty; 10106a540790SMatthias Ringwald } else if (tag_for_lowest_seq_nr){ 10116a540790SMatthias Ringwald tag_to_use = tag_for_lowest_seq_nr; 10126a540790SMatthias Ringwald } else { 10136a540790SMatthias Ringwald // should not happen 10146a540790SMatthias Ringwald return; 10156a540790SMatthias Ringwald } 1016bf78aac6SMatthias Ringwald // store ccc tag 10174ea43905SMatthias Ringwald entry.seq_nr = highest_seq_nr + 1u; 10186a540790SMatthias Ringwald entry.device_index = le_device_index; 10196a540790SMatthias Ringwald entry.att_handle = att_handle; 1020348fc806SMatthias Ringwald entry.value = (uint8_t) value; 10210fe46bc1SMatthias Ringwald int result = tlv_impl->store_tag(tlv_context, tag_to_use, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 10220fe46bc1SMatthias Ringwald if (result != 0){ 10230fe46bc1SMatthias Ringwald log_error("Store tag index %u failed", index); 10240fe46bc1SMatthias Ringwald } 1025bf78aac6SMatthias Ringwald } 1026bf78aac6SMatthias Ringwald 102768591e36SMatthias Ringwald static void att_server_persistent_ccc_clear(hci_connection_t * hci_connection){ 102868591e36SMatthias Ringwald if (!hci_connection) return; 102968591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 103068591e36SMatthias Ringwald 1031760ad805SMatthias Ringwald int le_device_index = att_server->ir_le_device_db_index; 1032760ad805SMatthias Ringwald log_info("Clear CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index); 1033760ad805SMatthias Ringwald // check if bonded 1034760ad805SMatthias Ringwald if (le_device_index < 0) return; 1035760ad805SMatthias Ringwald // get btstack_tlv 1036760ad805SMatthias Ringwald const btstack_tlv_t * tlv_impl = NULL; 1037760ad805SMatthias Ringwald void * tlv_context; 1038760ad805SMatthias Ringwald btstack_tlv_get_instance(&tlv_impl, &tlv_context); 1039760ad805SMatthias Ringwald if (!tlv_impl) return; 1040760ad805SMatthias Ringwald // get all ccc tag 1041760ad805SMatthias Ringwald int index; 10426a540790SMatthias Ringwald persistent_ccc_entry_t entry; 10436afb9acaSMatthias Ringwald for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){ 1044760ad805SMatthias Ringwald uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 10456a540790SMatthias Ringwald int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 10466a540790SMatthias Ringwald if (len != sizeof(persistent_ccc_entry_t)) continue; 10476a540790SMatthias Ringwald if (entry.device_index != le_device_index) continue; 1048760ad805SMatthias Ringwald // delete entry 1049760ad805SMatthias Ringwald log_info("CCC Index %u: Delete", index); 1050760ad805SMatthias Ringwald tlv_impl->delete_tag(tlv_context, tag); 1051760ad805SMatthias Ringwald } 1052760ad805SMatthias Ringwald } 1053760ad805SMatthias Ringwald 105468591e36SMatthias Ringwald static void att_server_persistent_ccc_restore(hci_connection_t * hci_connection){ 105568591e36SMatthias Ringwald if (!hci_connection) return; 105668591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 1057388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 105868591e36SMatthias Ringwald 105901ac2008SMatthias Ringwald int le_device_index = att_server->ir_le_device_db_index; 106001ac2008SMatthias Ringwald log_info("Restore CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index); 10611b7acd0dSMatthias Ringwald // check if bonded 10621b7acd0dSMatthias Ringwald if (le_device_index < 0) return; 106301ac2008SMatthias Ringwald // get btstack_tlv 106401ac2008SMatthias Ringwald const btstack_tlv_t * tlv_impl = NULL; 106501ac2008SMatthias Ringwald void * tlv_context; 106601ac2008SMatthias Ringwald btstack_tlv_get_instance(&tlv_impl, &tlv_context); 106701ac2008SMatthias Ringwald if (!tlv_impl) return; 106801ac2008SMatthias Ringwald // get all ccc tag 106901ac2008SMatthias Ringwald int index; 10706a540790SMatthias Ringwald persistent_ccc_entry_t entry; 10716afb9acaSMatthias Ringwald for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){ 107201ac2008SMatthias Ringwald uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 10736a540790SMatthias Ringwald int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 10746a540790SMatthias Ringwald if (len != sizeof(persistent_ccc_entry_t)) continue; 10756a540790SMatthias Ringwald if (entry.device_index != le_device_index) continue; 107601ac2008SMatthias Ringwald // simulate write callback 10776a540790SMatthias Ringwald uint16_t attribute_handle = entry.att_handle; 107801ac2008SMatthias Ringwald uint8_t value[2]; 10796a540790SMatthias Ringwald little_endian_store_16(value, 0, entry.value); 108001ac2008SMatthias Ringwald att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle); 108101ac2008SMatthias Ringwald if (!callback) continue; 10826a540790SMatthias Ringwald log_info("CCC Index %u: Set Attribute handle 0x%04x to value 0x%04x", index, attribute_handle, entry.value ); 1083b6df12b6SMatthias Ringwald (*callback)(att_connection->con_handle, attribute_handle, ATT_TRANSACTION_MODE_NONE, 0, value, sizeof(value)); 108401ac2008SMatthias Ringwald } 108501ac2008SMatthias Ringwald } 108601ac2008SMatthias Ringwald 1087bf78aac6SMatthias Ringwald // persistent CCC writes 1088bf78aac6SMatthias Ringwald // --------------------- 10893d71c7a4SMatthias Ringwald 10903d71c7a4SMatthias Ringwald // gatt service management 10913d71c7a4SMatthias Ringwald static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){ 10923d71c7a4SMatthias Ringwald btstack_linked_list_iterator_t it; 10933d71c7a4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 10943d71c7a4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 10953d71c7a4SMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 10963d71c7a4SMatthias Ringwald if (handler->start_handle > handle) continue; 10973d71c7a4SMatthias Ringwald if (handler->end_handle < handle) continue; 10983d71c7a4SMatthias Ringwald return handler; 10993d71c7a4SMatthias Ringwald } 11003d71c7a4SMatthias Ringwald return NULL; 11013d71c7a4SMatthias Ringwald } 11023d71c7a4SMatthias Ringwald static att_read_callback_t att_server_read_callback_for_handle(uint16_t handle){ 11033d71c7a4SMatthias Ringwald att_service_handler_t * handler = att_service_handler_for_handle(handle); 11049305033eSMatthias Ringwald if (handler != NULL) return handler->read_callback; 11053d71c7a4SMatthias Ringwald return att_server_client_read_callback; 11063d71c7a4SMatthias Ringwald } 11073d71c7a4SMatthias Ringwald 11083d71c7a4SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){ 11093d71c7a4SMatthias Ringwald att_service_handler_t * handler = att_service_handler_for_handle(handle); 11109305033eSMatthias Ringwald if (handler != NULL) return handler->write_callback; 11113d71c7a4SMatthias Ringwald return att_server_client_write_callback; 11123d71c7a4SMatthias Ringwald } 11133d71c7a4SMatthias Ringwald 11145d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle){ 11155d07396bSMatthias Ringwald att_service_handler_t * handler = att_service_handler_for_handle(handle); 11169305033eSMatthias Ringwald if (handler != NULL) return handler->packet_handler; 11175d07396bSMatthias Ringwald return att_client_packet_handler; 11185d07396bSMatthias Ringwald } 11195d07396bSMatthias Ringwald 11203d71c7a4SMatthias Ringwald static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){ 11213d71c7a4SMatthias Ringwald // notify all callbacks 11223d71c7a4SMatthias Ringwald btstack_linked_list_iterator_t it; 11233d71c7a4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 11243d71c7a4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 11253d71c7a4SMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 11263d71c7a4SMatthias Ringwald if (!handler->write_callback) continue; 11273d71c7a4SMatthias Ringwald (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0); 11283d71c7a4SMatthias Ringwald } 11293d71c7a4SMatthias Ringwald if (!att_server_client_write_callback) return; 11303d71c7a4SMatthias Ringwald (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0); 11313d71c7a4SMatthias Ringwald } 11323d71c7a4SMatthias Ringwald 11333d71c7a4SMatthias Ringwald // returns first reported error or 0 11343d71c7a4SMatthias Ringwald static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){ 11353d71c7a4SMatthias Ringwald btstack_linked_list_iterator_t it; 11363d71c7a4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 11373d71c7a4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 11383d71c7a4SMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 11393d71c7a4SMatthias Ringwald if (!handler->write_callback) continue; 11403d71c7a4SMatthias Ringwald uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0); 114138b893aaSMilanka Ringwald if (error_code != 0u) return error_code; 11423d71c7a4SMatthias Ringwald } 11433d71c7a4SMatthias Ringwald if (!att_server_client_write_callback) return 0; 11443d71c7a4SMatthias Ringwald return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0); 11453d71c7a4SMatthias Ringwald } 11463d71c7a4SMatthias Ringwald 11473d71c7a4SMatthias 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){ 11483d71c7a4SMatthias Ringwald att_read_callback_t callback = att_server_read_callback_for_handle(attribute_handle); 114901ac2008SMatthias Ringwald if (!callback) return 0; 11503d71c7a4SMatthias Ringwald return (*callback)(con_handle, attribute_handle, offset, buffer, buffer_size); 11513d71c7a4SMatthias Ringwald } 11523d71c7a4SMatthias Ringwald 11533d71c7a4SMatthias 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){ 11543d71c7a4SMatthias Ringwald switch (transaction_mode){ 11553d71c7a4SMatthias Ringwald case ATT_TRANSACTION_MODE_VALIDATE: 11563d71c7a4SMatthias Ringwald return att_validate_prepared_write(con_handle); 11573d71c7a4SMatthias Ringwald case ATT_TRANSACTION_MODE_EXECUTE: 11583d71c7a4SMatthias Ringwald case ATT_TRANSACTION_MODE_CANCEL: 11593d71c7a4SMatthias Ringwald att_notify_write_callbacks(con_handle, transaction_mode); 11603d71c7a4SMatthias Ringwald return 0; 11613d71c7a4SMatthias Ringwald default: 11623d71c7a4SMatthias Ringwald break; 11633d71c7a4SMatthias Ringwald } 11643d71c7a4SMatthias Ringwald 1165bf78aac6SMatthias Ringwald // track CCC writes 11664ea43905SMatthias Ringwald if (att_is_persistent_ccc(attribute_handle) && (offset == 0u) && (buffer_size == 2u)){ 1167bf78aac6SMatthias Ringwald att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0)); 1168bf78aac6SMatthias Ringwald } 1169bf78aac6SMatthias Ringwald 117001ac2008SMatthias Ringwald att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle); 117101ac2008SMatthias Ringwald if (!callback) return 0; 11723d71c7a4SMatthias Ringwald return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size); 11733d71c7a4SMatthias Ringwald } 11743d71c7a4SMatthias Ringwald 11753d71c7a4SMatthias Ringwald /** 11763d71c7a4SMatthias Ringwald * @brief register read/write callbacks for specific handle range 11773d71c7a4SMatthias Ringwald * @param att_service_handler_t 11783d71c7a4SMatthias Ringwald */ 11793d71c7a4SMatthias Ringwald void att_server_register_service_handler(att_service_handler_t * handler){ 1180ec8f5414SMilanka Ringwald bool att_server_registered = false; 1181ec8f5414SMilanka Ringwald if (att_service_handler_for_handle(handler->start_handle)){ 1182ec8f5414SMilanka Ringwald att_server_registered = true; 1183ec8f5414SMilanka Ringwald } 1184ec8f5414SMilanka Ringwald 1185ec8f5414SMilanka Ringwald if (att_service_handler_for_handle(handler->end_handle)){ 1186ec8f5414SMilanka Ringwald att_server_registered = true; 1187ec8f5414SMilanka Ringwald } 1188ec8f5414SMilanka Ringwald 1189ec8f5414SMilanka Ringwald if (att_server_registered){ 11903d71c7a4SMatthias Ringwald log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle); 11913d71c7a4SMatthias Ringwald return; 11923d71c7a4SMatthias Ringwald } 11933d71c7a4SMatthias Ringwald btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler); 11943d71c7a4SMatthias Ringwald } 11953d71c7a4SMatthias Ringwald 11963deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){ 11973deb3ec6SMatthias Ringwald 11983d71c7a4SMatthias Ringwald // store callbacks 11993d71c7a4SMatthias Ringwald att_server_client_read_callback = read_callback; 12003d71c7a4SMatthias Ringwald att_server_client_write_callback = write_callback; 12013d71c7a4SMatthias Ringwald 12021a389cdcSMatthias Ringwald // register for HCI Events 1203d9a7306aSMatthias Ringwald hci_event_callback_registration.callback = &att_event_packet_handler; 12041a389cdcSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 12051a389cdcSMatthias Ringwald 1206406722e4SMatthias Ringwald // register for SM events 1207d9a7306aSMatthias Ringwald sm_event_callback_registration.callback = &att_event_packet_handler; 1208406722e4SMatthias Ringwald sm_add_event_handler(&sm_event_callback_registration); 12093deb3ec6SMatthias Ringwald 12101a389cdcSMatthias Ringwald // and L2CAP ATT Server PDUs 12113deb3ec6SMatthias Ringwald att_dispatch_register_server(att_packet_handler); 12123deb3ec6SMatthias Ringwald 121370dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 121470dca4d4SMatthias Ringwald // setup l2cap service 1215f2372b44SMatthias Ringwald l2cap_register_service(&att_event_packet_handler, PSM_ATT, 0xffff, gap_get_security_level()); 121670dca4d4SMatthias Ringwald #endif 121770dca4d4SMatthias Ringwald 12183deb3ec6SMatthias Ringwald att_set_db(db); 12193d71c7a4SMatthias Ringwald att_set_read_callback(att_server_read_callback); 12203d71c7a4SMatthias Ringwald att_set_write_callback(att_server_write_callback); 12213deb3ec6SMatthias Ringwald } 12223deb3ec6SMatthias Ringwald 12233deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){ 12243deb3ec6SMatthias Ringwald att_client_packet_handler = handler; 12253deb3ec6SMatthias Ringwald } 12263deb3ec6SMatthias Ringwald 1227cbbb12d9SMatthias Ringwald 1228cbbb12d9SMatthias Ringwald // to be deprecated 1229c141988cSMatthias Ringwald int att_server_can_send_packet_now(hci_con_handle_t con_handle){ 123068591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 123168591e36SMatthias Ringwald if (!hci_connection) return 0; 123268591e36SMatthias Ringwald return att_server_can_send_packet(hci_connection); 12331b96b007SMatthias Ringwald } 12341b96b007SMatthias Ringwald 12359816429dSMatthias Ringwald uint8_t att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 1236cbbb12d9SMatthias Ringwald return att_server_request_to_send_notification(callback_registration, con_handle); 12373deb3ec6SMatthias Ringwald } 12383deb3ec6SMatthias Ringwald 1239cbbb12d9SMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){ 1240cbbb12d9SMatthias Ringwald att_client_waiting_for_can_send_registration.callback = &att_emit_can_send_now_event; 1241cbbb12d9SMatthias Ringwald att_server_request_to_send_notification(&att_client_waiting_for_can_send_registration, con_handle); 1242cbbb12d9SMatthias Ringwald } 1243cbbb12d9SMatthias Ringwald // end of deprecated 1244cbbb12d9SMatthias Ringwald 12459816429dSMatthias Ringwald uint8_t att_server_request_to_send_notification(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 124668591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 124768591e36SMatthias Ringwald if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 124868591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 1249*3f9b77dbSMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 1250c37df6f6SMatthias Ringwald bool added = btstack_linked_list_add_tail(&att_server->notification_requests, (btstack_linked_item_t*) callback_registration); 1251*3f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 1252c37df6f6SMatthias Ringwald if (added){ 1253cbbb12d9SMatthias Ringwald return ERROR_CODE_SUCCESS; 1254c37df6f6SMatthias Ringwald } else { 1255c37df6f6SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1256c37df6f6SMatthias Ringwald } 1257cbbb12d9SMatthias Ringwald } 1258cbbb12d9SMatthias Ringwald 12599816429dSMatthias Ringwald uint8_t att_server_request_to_send_indication(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 126068591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 126168591e36SMatthias Ringwald if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 126268591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 1263*3f9b77dbSMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 1264c37df6f6SMatthias Ringwald bool added = btstack_linked_list_add_tail(&att_server->indication_requests, (btstack_linked_item_t*) callback_registration); 1265*3f9b77dbSMatthias Ringwald att_server_request_can_send_now(att_server, att_connection); 1266c37df6f6SMatthias Ringwald if (added){ 1267969a5bbaSMatthias Ringwald return ERROR_CODE_SUCCESS; 1268c37df6f6SMatthias Ringwald } else { 1269c37df6f6SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1270c37df6f6SMatthias Ringwald } 1271bb38f057SMatthias Ringwald } 1272bb38f057SMatthias Ringwald 12739816429dSMatthias Ringwald uint8_t att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){ 127468591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 127568591e36SMatthias Ringwald if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1276388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 127768591e36SMatthias Ringwald 127868591e36SMatthias Ringwald if (!att_server_can_send_packet(hci_connection)) return BTSTACK_ACL_BUFFERS_FULL; 12793deb3ec6SMatthias Ringwald 12803deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 12813deb3ec6SMatthias Ringwald uint8_t * packet_buffer = l2cap_get_outgoing_buffer(); 1282b6df12b6SMatthias Ringwald uint16_t size = att_prepare_handle_value_notification(att_connection, attribute_handle, value, value_len, packet_buffer); 12831676a2b8SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 1284d415948cSMatthias Ringwald 1285d415948cSMatthias Ringwald return att_server_send_prepared(att_server, att_connection, size); 12863deb3ec6SMatthias Ringwald } 12873deb3ec6SMatthias Ringwald 12889816429dSMatthias Ringwald uint8_t att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){ 128968591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 129068591e36SMatthias Ringwald if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 129168591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 1292388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 129318707219SMatthias Ringwald 129438b893aaSMilanka Ringwald if (att_server->value_indication_handle != 0u) return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS; 129568591e36SMatthias Ringwald if (!att_server_can_send_packet(hci_connection)) return BTSTACK_ACL_BUFFERS_FULL; 12963deb3ec6SMatthias Ringwald 12973deb3ec6SMatthias Ringwald // track indication 129818707219SMatthias Ringwald att_server->value_indication_handle = attribute_handle; 129918707219SMatthias Ringwald btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout); 130018707219SMatthias Ringwald btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS); 130118707219SMatthias Ringwald btstack_run_loop_add_timer(&att_server->value_indication_timer); 13023deb3ec6SMatthias Ringwald 13033deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 13043deb3ec6SMatthias Ringwald uint8_t * packet_buffer = l2cap_get_outgoing_buffer(); 1305b6df12b6SMatthias Ringwald uint16_t size = att_prepare_handle_value_indication(att_connection, attribute_handle, value, value_len, packet_buffer); 130623079b39SMatthias Ringwald 1307d415948cSMatthias Ringwald return att_server_send_prepared(att_server, att_connection, size); 13083deb3ec6SMatthias Ringwald } 13098f9132b5SMilanka Ringwald 13108f9132b5SMilanka Ringwald uint16_t att_server_get_mtu(hci_con_handle_t con_handle){ 131168591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 131268591e36SMatthias Ringwald if (!hci_connection) return 0; 1313388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 1314b6df12b6SMatthias Ringwald return att_connection->mtu; 13158f9132b5SMilanka Ringwald } 131635d7f2dfSMilanka Ringwald 131735d7f2dfSMilanka Ringwald void att_server_deinit(void){ 131835d7f2dfSMilanka Ringwald att_server_client_read_callback = NULL; 131935d7f2dfSMilanka Ringwald att_server_client_write_callback = NULL; 132035d7f2dfSMilanka Ringwald att_client_packet_handler = NULL; 132135d7f2dfSMilanka Ringwald service_handlers = NULL; 132235d7f2dfSMilanka Ringwald } 1323