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 233deb3ec6SMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 243deb3ec6SMatthias Ringwald * RINGWALD 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 5159c6af15SMatthias Ringwald #include "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 706afb9acaSMatthias Ringwald #ifndef NVN_NUM_GATT_SERVER_CCC 716afb9acaSMatthias Ringwald #define NVN_NUM_GATT_SERVER_CCC 20 72bf78aac6SMatthias Ringwald #endif 733deb3ec6SMatthias Ringwald 7418707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server); 7501ac2008SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle); 765d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle); 7701ac2008SMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server); 78760ad805SMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server); 7941772f37SMatthias Ringwald static void att_server_handle_att_pdu(att_server_t * att_server, uint8_t * packet, uint16_t size); 803deb3ec6SMatthias Ringwald 813551936eSMatthias Ringwald typedef enum { 823551936eSMatthias Ringwald ATT_SERVER_RUN_PHASE_1_REQUESTS, 833551936eSMatthias Ringwald ATT_SERVER_RUN_PHASE_2_INDICATIONS, 843551936eSMatthias Ringwald ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS, 853551936eSMatthias Ringwald } att_server_run_phase_t; 863551936eSMatthias Ringwald 876a540790SMatthias Ringwald // 886a540790SMatthias Ringwald typedef struct { 896a540790SMatthias Ringwald uint32_t seq_nr; 906a540790SMatthias Ringwald uint16_t att_handle; 916a540790SMatthias Ringwald uint8_t value; 926a540790SMatthias Ringwald uint8_t device_index; 936a540790SMatthias Ringwald } persistent_ccc_entry_t; 946a540790SMatthias Ringwald 9518707219SMatthias Ringwald // global 961a389cdcSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 97406722e4SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration; 983deb3ec6SMatthias Ringwald static btstack_packet_handler_t att_client_packet_handler = NULL; 993d71c7a4SMatthias Ringwald static btstack_linked_list_t service_handlers; 1005f141511SMatthias Ringwald static btstack_context_callback_registration_t att_client_waiting_for_can_send_registration; 10118707219SMatthias Ringwald 1023d71c7a4SMatthias Ringwald static att_read_callback_t att_server_client_read_callback; 1033d71c7a4SMatthias Ringwald static att_write_callback_t att_server_client_write_callback; 1043d71c7a4SMatthias Ringwald 1056438547aSMatthias Ringwald // round robin 1066438547aSMatthias Ringwald static hci_con_handle_t att_server_last_can_send_now = HCI_CON_HANDLE_INVALID; 107bf78aac6SMatthias Ringwald 10818707219SMatthias Ringwald static att_server_t * att_server_for_handle(hci_con_handle_t con_handle){ 10918707219SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 11018707219SMatthias Ringwald if (!hci_connection) return NULL; 11118707219SMatthias Ringwald return &hci_connection->att_server; 11218707219SMatthias Ringwald } 11318707219SMatthias Ringwald 11470dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 11570dca4d4SMatthias Ringwald static att_server_t * att_server_for_l2cap_cid(uint16_t l2cap_cid){ 11670dca4d4SMatthias Ringwald btstack_linked_list_iterator_t it; 11770dca4d4SMatthias Ringwald hci_connections_get_iterator(&it); 11870dca4d4SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 11970dca4d4SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 12070dca4d4SMatthias Ringwald att_server_t * att_server = &connection->att_server; 12170dca4d4SMatthias Ringwald if (att_server->l2cap_cid == l2cap_cid) return att_server; 12270dca4d4SMatthias Ringwald } 12370dca4d4SMatthias Ringwald return NULL; 12470dca4d4SMatthias Ringwald } 12570dca4d4SMatthias Ringwald #endif 12670dca4d4SMatthias Ringwald 12702b78fc8SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 12818707219SMatthias Ringwald static att_server_t * att_server_for_state(att_server_state_t state){ 12918707219SMatthias Ringwald btstack_linked_list_iterator_t it; 13018707219SMatthias Ringwald hci_connections_get_iterator(&it); 13118707219SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 13218707219SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 13318707219SMatthias Ringwald att_server_t * att_server = &connection->att_server; 13418707219SMatthias Ringwald if (att_server->state == state) return att_server; 13518707219SMatthias Ringwald } 13618707219SMatthias Ringwald return NULL; 13718707219SMatthias Ringwald } 13802b78fc8SMatthias Ringwald #endif 139bb38f057SMatthias Ringwald 1403deb3ec6SMatthias Ringwald static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){ 1415d07396bSMatthias Ringwald btstack_packet_handler_t packet_handler = att_server_packet_handler_for_handle(attribute_handle); 1425d07396bSMatthias Ringwald if (!packet_handler) return; 1433deb3ec6SMatthias Ringwald 1443deb3ec6SMatthias Ringwald uint8_t event[7]; 1453deb3ec6SMatthias Ringwald int pos = 0; 1465611a760SMatthias Ringwald event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE; 1473deb3ec6SMatthias Ringwald event[pos++] = sizeof(event) - 2; 1483deb3ec6SMatthias Ringwald event[pos++] = status; 149f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, client_handle); 1503deb3ec6SMatthias Ringwald pos += 2; 151f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, attribute_handle); 152a444112bSMatthias Ringwald (*packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 1533deb3ec6SMatthias Ringwald } 1543deb3ec6SMatthias Ringwald 1555d07396bSMatthias Ringwald static void att_emit_event_to_all(const uint8_t * event, uint16_t size){ 1565d07396bSMatthias Ringwald // dispatch to app level handler 1575d07396bSMatthias Ringwald if (att_client_packet_handler){ 1585d07396bSMatthias Ringwald (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size); 1595d07396bSMatthias Ringwald } 1603deb3ec6SMatthias Ringwald 1615d07396bSMatthias Ringwald // dispatch to service handlers 1625d07396bSMatthias Ringwald btstack_linked_list_iterator_t it; 1635d07396bSMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 1645d07396bSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1655d07396bSMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 1665d07396bSMatthias Ringwald if (!handler->packet_handler) continue; 1675d07396bSMatthias Ringwald (*handler->packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size); 1685d07396bSMatthias Ringwald } 1695d07396bSMatthias Ringwald } 1705d07396bSMatthias Ringwald 1715d07396bSMatthias Ringwald static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){ 1723deb3ec6SMatthias Ringwald uint8_t event[6]; 1733deb3ec6SMatthias Ringwald int pos = 0; 1745611a760SMatthias Ringwald event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE; 1753deb3ec6SMatthias Ringwald event[pos++] = sizeof(event) - 2; 176fc64f94aSMatthias Ringwald little_endian_store_16(event, pos, con_handle); 1773deb3ec6SMatthias Ringwald pos += 2; 178f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, mtu); 1795d07396bSMatthias Ringwald 1805d07396bSMatthias Ringwald // also dispatch to GATT Clients 181b12646c5SJakob Krantz att_dispatch_server_mtu_exchanged(con_handle, mtu); 1825d07396bSMatthias Ringwald 1835d07396bSMatthias Ringwald // dispatch to app level handler and service handlers 1845d07396bSMatthias Ringwald att_emit_event_to_all(&event[0], sizeof(event)); 1853deb3ec6SMatthias Ringwald } 1863deb3ec6SMatthias Ringwald 1875f141511SMatthias Ringwald static void att_emit_can_send_now_event(void * context){ 1885f141511SMatthias Ringwald UNUSED(context); 1893c97b242SMatthias Ringwald if (!att_client_packet_handler) return; 1903c97b242SMatthias Ringwald 1911b96b007SMatthias Ringwald uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0}; 1921b96b007SMatthias Ringwald (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 1931b96b007SMatthias Ringwald } 1941b96b007SMatthias Ringwald 195ff1bec95SMatthias Ringwald static void att_emit_connected_event(att_server_t * att_server){ 196ff1bec95SMatthias Ringwald uint8_t event[11]; 197ff1bec95SMatthias Ringwald int pos = 0; 198ff1bec95SMatthias Ringwald event[pos++] = ATT_EVENT_CONNECTED; 199ff1bec95SMatthias Ringwald event[pos++] = sizeof(event) - 2; 200ff1bec95SMatthias Ringwald event[pos++] = att_server->peer_addr_type; 201ff1bec95SMatthias Ringwald reverse_bd_addr(att_server->peer_address, &event[pos]); 202ff1bec95SMatthias Ringwald pos += 6; 203ff1bec95SMatthias Ringwald little_endian_store_16(event, pos, att_server->connection.con_handle); 204ff1bec95SMatthias Ringwald pos += 2; 205ff1bec95SMatthias Ringwald 206ff1bec95SMatthias Ringwald // dispatch to app level handler and service handlers 207ff1bec95SMatthias Ringwald att_emit_event_to_all(&event[0], sizeof(event)); 208ff1bec95SMatthias Ringwald } 209ff1bec95SMatthias Ringwald 210ff1bec95SMatthias Ringwald 2116187ad4cSMatthias Ringwald static void att_emit_disconnected_event(uint16_t con_handle){ 212ff1bec95SMatthias Ringwald uint8_t event[4]; 213ff1bec95SMatthias Ringwald int pos = 0; 214ff1bec95SMatthias Ringwald event[pos++] = ATT_EVENT_DISCONNECTED; 215ff1bec95SMatthias Ringwald event[pos++] = sizeof(event) - 2; 2166187ad4cSMatthias Ringwald little_endian_store_16(event, pos, con_handle); 217ff1bec95SMatthias Ringwald pos += 2; 218ff1bec95SMatthias Ringwald 219ff1bec95SMatthias Ringwald // dispatch to app level handler and service handlers 220ff1bec95SMatthias Ringwald att_emit_event_to_all(&event[0], sizeof(event)); 221ff1bec95SMatthias Ringwald } 222ff1bec95SMatthias Ringwald 223ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){ 22454178543SMatthias Ringwald void * context = btstack_run_loop_get_timer_context(ts); 22554178543SMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context; 22618707219SMatthias Ringwald att_server_t * att_server = att_server_for_handle(con_handle); 22718707219SMatthias Ringwald if (!att_server) return; 228bce48a26SMatthias Ringwald // @note: after a transcation timeout, no more requests shall be sent over this ATT Bearer 229bce48a26SMatthias Ringwald // (that's why we don't reset the value_indication_handle) 23018707219SMatthias Ringwald uint16_t att_handle = att_server->value_indication_handle; 23118707219SMatthias Ringwald att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_server->connection.con_handle, att_handle); 2323deb3ec6SMatthias Ringwald } 2333deb3ec6SMatthias Ringwald 2343deb3ec6SMatthias Ringwald static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2359ec2630cSMatthias Ringwald 2367dad9ff8SMatthias Ringwald UNUSED(channel); // ok: there is no channel 2377dad9ff8SMatthias Ringwald UNUSED(size); // ok: handling own l2cap events 2383deb3ec6SMatthias Ringwald 23918707219SMatthias Ringwald att_server_t * att_server; 24018707219SMatthias Ringwald hci_con_handle_t con_handle; 24170dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 24270dca4d4SMatthias Ringwald bd_addr_t address; 24370dca4d4SMatthias Ringwald #endif 24418707219SMatthias Ringwald 2453deb3ec6SMatthias Ringwald switch (packet_type) { 2463deb3ec6SMatthias Ringwald 2473deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 2480e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 2493deb3ec6SMatthias Ringwald 25070dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 25170dca4d4SMatthias Ringwald case L2CAP_EVENT_INCOMING_CONNECTION: 25270dca4d4SMatthias Ringwald l2cap_event_incoming_connection_get_address(packet, address); 25370dca4d4SMatthias Ringwald l2cap_accept_connection(channel); 25470dca4d4SMatthias Ringwald printf("Accept incoming connection from %s\n", bd_addr_to_str(address)); 25570dca4d4SMatthias Ringwald break; 25670dca4d4SMatthias Ringwald case L2CAP_EVENT_CHANNEL_OPENED: 257122b723eSMatthias Ringwald con_handle = l2cap_event_channel_opened_get_handle(packet); 258122b723eSMatthias Ringwald att_server = att_server_for_handle(con_handle); 25970dca4d4SMatthias Ringwald if (!att_server) break; 260122b723eSMatthias Ringwald // store connection info 261122b723eSMatthias Ringwald att_server->peer_addr_type = BD_ADDR_TYPE_CLASSIC; 262122b723eSMatthias Ringwald l2cap_event_channel_opened_get_address(packet, att_server->peer_address); 263122b723eSMatthias Ringwald att_server->connection.con_handle = con_handle; 26470dca4d4SMatthias Ringwald att_server->l2cap_cid = l2cap_event_channel_opened_get_local_cid(packet); 265122b723eSMatthias Ringwald // reset connection properties 266122b723eSMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 267122b723eSMatthias Ringwald att_server->connection.mtu = l2cap_event_channel_opened_get_remote_mtu(packet); 268122b723eSMatthias Ringwald att_server->connection.max_mtu = l2cap_max_mtu(); 269122b723eSMatthias Ringwald if (att_server->connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){ 270122b723eSMatthias Ringwald att_server->connection.max_mtu = ATT_REQUEST_BUFFER_SIZE; 271122b723eSMatthias Ringwald } 272122b723eSMatthias Ringwald // TODO: use security level from underlying HCI connection 273122b723eSMatthias Ringwald att_server->connection.encryption_key_size = 0; 274122b723eSMatthias Ringwald att_server->connection.authenticated = 0; 275122b723eSMatthias Ringwald att_server->connection.authorized = 0; 276122b723eSMatthias Ringwald // TODO: what to do about le device db? 277122b723eSMatthias Ringwald att_server->pairing_active = 0; 278122b723eSMatthias Ringwald printf("Connection opened %s, l2cap cid %04x, \n", bd_addr_to_str(address), att_server->l2cap_cid); 27970dca4d4SMatthias Ringwald break; 28070dca4d4SMatthias Ringwald #endif 2813deb3ec6SMatthias Ringwald case HCI_EVENT_LE_META: 2823deb3ec6SMatthias Ringwald switch (packet[2]) { 2833deb3ec6SMatthias Ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 28418707219SMatthias Ringwald con_handle = little_endian_read_16(packet, 4); 28518707219SMatthias Ringwald att_server = att_server_for_handle(con_handle); 28618707219SMatthias Ringwald if (!att_server) break; 2873deb3ec6SMatthias Ringwald // store connection info 28818707219SMatthias Ringwald att_server->peer_addr_type = packet[7]; 28918707219SMatthias Ringwald reverse_bd_addr(&packet[8], att_server->peer_address); 29018707219SMatthias Ringwald att_server->connection.con_handle = con_handle; 2913deb3ec6SMatthias Ringwald // reset connection properties 29218707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 29318707219SMatthias Ringwald att_server->connection.mtu = ATT_DEFAULT_MTU; 29418707219SMatthias Ringwald att_server->connection.max_mtu = l2cap_max_le_mtu(); 29518707219SMatthias Ringwald if (att_server->connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){ 29618707219SMatthias Ringwald att_server->connection.max_mtu = ATT_REQUEST_BUFFER_SIZE; 29799c58ad0SMatthias Ringwald } 29818707219SMatthias Ringwald att_server->connection.encryption_key_size = 0; 29918707219SMatthias Ringwald att_server->connection.authenticated = 0; 30018707219SMatthias Ringwald att_server->connection.authorized = 0; 301b2c1e52cSMatthias Ringwald // workaround: identity resolving can already be complete, at least store result 302b2c1e52cSMatthias Ringwald att_server->ir_le_device_db_index = sm_le_device_index(con_handle); 303*ed053a35SMatthias Ringwald att_server->ir_lookup_active = 0; 304760ad805SMatthias Ringwald att_server->pairing_active = 0; 305ff1bec95SMatthias Ringwald // notify all - old 3065d07396bSMatthias Ringwald att_emit_event_to_all(packet, size); 307ff1bec95SMatthias Ringwald // notify all - new 308ff1bec95SMatthias Ringwald att_emit_connected_event(att_server); 3093deb3ec6SMatthias Ringwald break; 3103deb3ec6SMatthias Ringwald 3113deb3ec6SMatthias Ringwald default: 3123deb3ec6SMatthias Ringwald break; 3133deb3ec6SMatthias Ringwald } 3143deb3ec6SMatthias Ringwald break; 3153deb3ec6SMatthias Ringwald 3163deb3ec6SMatthias Ringwald case HCI_EVENT_ENCRYPTION_CHANGE: 3173deb3ec6SMatthias Ringwald case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE: 3183deb3ec6SMatthias Ringwald // check handle 31918707219SMatthias Ringwald con_handle = little_endian_read_16(packet, 3); 32018707219SMatthias Ringwald att_server = att_server_for_handle(con_handle); 32118707219SMatthias Ringwald if (!att_server) break; 3229c6e867eSMatthias Ringwald att_server->connection.encryption_key_size = gap_encryption_key_size(con_handle); 3239c6e867eSMatthias Ringwald att_server->connection.authenticated = gap_authenticated(con_handle); 32413eb7322SMatthias Ringwald att_server->connection.secure_connection = gap_secure_connection(con_handle); 32513eb7322SMatthias Ringwald log_info("encrypted key size %u, authenticated %u, secure connectipon %u", 32613eb7322SMatthias Ringwald att_server->connection.encryption_key_size, att_server->connection.authenticated, att_server->connection.secure_connection); 32701ac2008SMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){ 32801ac2008SMatthias Ringwald // restore CCC values when encrypted 32901ac2008SMatthias Ringwald if (hci_event_encryption_change_get_encryption_enabled(packet)){ 33001ac2008SMatthias Ringwald att_server_persistent_ccc_restore(att_server); 33101ac2008SMatthias Ringwald } 33201ac2008SMatthias Ringwald } 3330234fbbcSMatthias Ringwald att_run_for_context(att_server); 3343deb3ec6SMatthias Ringwald break; 3353deb3ec6SMatthias Ringwald 3363deb3ec6SMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 33770a390c7SMatthias Ringwald // check handle 33818707219SMatthias Ringwald con_handle = hci_event_disconnection_complete_get_connection_handle(packet); 33918707219SMatthias Ringwald att_server = att_server_for_handle(con_handle); 34018707219SMatthias Ringwald if (!att_server) break; 34118707219SMatthias Ringwald att_clear_transaction_queue(&att_server->connection); 34218707219SMatthias Ringwald att_server->connection.con_handle = 0; 343760ad805SMatthias Ringwald att_server->pairing_active = 0; 34418707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 345bce48a26SMatthias Ringwald if (att_server->value_indication_handle){ 34627328ecbSMatthias Ringwald btstack_run_loop_remove_timer(&att_server->value_indication_timer); 347bce48a26SMatthias Ringwald uint16_t att_handle = att_server->value_indication_handle; 348bce48a26SMatthias Ringwald att_server->value_indication_handle = 0; // reset error state 349bce48a26SMatthias Ringwald att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_DISCONNECT, att_server->connection.con_handle, att_handle); 350bce48a26SMatthias Ringwald } 351ff1bec95SMatthias Ringwald // notify all - new 3526187ad4cSMatthias Ringwald att_emit_disconnected_event(con_handle); 353ff1bec95SMatthias Ringwald // notify all - old 3545d07396bSMatthias Ringwald att_emit_event_to_all(packet, size); 3553deb3ec6SMatthias Ringwald break; 3563deb3ec6SMatthias Ringwald 357760ad805SMatthias Ringwald // Identity Resolving 3585611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_STARTED: 35918707219SMatthias Ringwald con_handle = sm_event_identity_resolving_started_get_handle(packet); 36018707219SMatthias Ringwald att_server = att_server_for_handle(con_handle); 36118707219SMatthias Ringwald if (!att_server) break; 3625611a760SMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED"); 36318707219SMatthias Ringwald att_server->ir_lookup_active = 1; 3643deb3ec6SMatthias Ringwald break; 3655611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED: 366760ad805SMatthias Ringwald con_handle = sm_event_identity_created_get_handle(packet); 367760ad805SMatthias Ringwald att_server = att_server_for_handle(con_handle); 368760ad805SMatthias Ringwald if (!att_server) return; 3691b7acd0dSMatthias Ringwald att_server->ir_lookup_active = 0; 3701b7acd0dSMatthias Ringwald att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index(packet); 3711b7acd0dSMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED"); 3721b7acd0dSMatthias Ringwald att_run_for_context(att_server); 3733deb3ec6SMatthias Ringwald break; 3745611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_FAILED: 37518707219SMatthias Ringwald con_handle = sm_event_identity_resolving_failed_get_handle(packet); 37618707219SMatthias Ringwald att_server = att_server_for_handle(con_handle); 37718707219SMatthias Ringwald if (!att_server) break; 3785611a760SMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED"); 37918707219SMatthias Ringwald att_server->ir_lookup_active = 0; 38018707219SMatthias Ringwald att_server->ir_le_device_db_index = -1; 38118707219SMatthias Ringwald att_run_for_context(att_server); 3823deb3ec6SMatthias Ringwald break; 383760ad805SMatthias Ringwald 384760ad805SMatthias Ringwald // Pairing started - delete stored CCC values 385760ad805SMatthias Ringwald // - assumes pairing indicates either new device or re-pairing, in both cases there should be no stored CCC values 386760ad805SMatthias Ringwald // - assumes that all events have the con handle as the first field 387760ad805SMatthias Ringwald case SM_EVENT_JUST_WORKS_REQUEST: 388760ad805SMatthias Ringwald case SM_EVENT_PASSKEY_DISPLAY_NUMBER: 389760ad805SMatthias Ringwald case SM_EVENT_PASSKEY_INPUT_NUMBER: 390760ad805SMatthias Ringwald case SM_EVENT_NUMERIC_COMPARISON_REQUEST: 391760ad805SMatthias Ringwald con_handle = sm_event_just_works_request_get_handle(packet); 392760ad805SMatthias Ringwald att_server = att_server_for_handle(con_handle); 393760ad805SMatthias Ringwald if (!att_server) break; 394760ad805SMatthias Ringwald att_server->pairing_active = 1; 395760ad805SMatthias Ringwald log_info("SM Pairing started"); 396760ad805SMatthias Ringwald if (att_server->ir_le_device_db_index < 0) break; 397760ad805SMatthias Ringwald att_server_persistent_ccc_clear(att_server); 398760ad805SMatthias Ringwald // index not valid anymore 399760ad805SMatthias Ringwald att_server->ir_le_device_db_index = -1; 400760ad805SMatthias Ringwald break; 401760ad805SMatthias Ringwald 4021b7acd0dSMatthias Ringwald // Bonding completed 403760ad805SMatthias Ringwald case SM_EVENT_IDENTITY_CREATED: 404760ad805SMatthias Ringwald con_handle = sm_event_identity_created_get_handle(packet); 405760ad805SMatthias Ringwald att_server = att_server_for_handle(con_handle); 406760ad805SMatthias Ringwald if (!att_server) return; 407760ad805SMatthias Ringwald att_server->pairing_active = 0; 4081b7acd0dSMatthias Ringwald att_server->ir_le_device_db_index = sm_event_identity_created_get_index(packet); 4091b7acd0dSMatthias Ringwald att_run_for_context(att_server); 4101b7acd0dSMatthias Ringwald break; 4111b7acd0dSMatthias Ringwald 4121b7acd0dSMatthias Ringwald // Pairing complete (with/without bonding=storing of pairing information) 4131b7acd0dSMatthias Ringwald case SM_EVENT_PAIRING_COMPLETE: 4141b7acd0dSMatthias Ringwald con_handle = sm_event_pairing_complete_get_handle(packet); 4151b7acd0dSMatthias Ringwald att_server = att_server_for_handle(con_handle); 4161b7acd0dSMatthias Ringwald if (!att_server) return; 4171b7acd0dSMatthias Ringwald att_server->pairing_active = 0; 4181b7acd0dSMatthias Ringwald att_run_for_context(att_server); 419760ad805SMatthias Ringwald break; 420760ad805SMatthias Ringwald 421760ad805SMatthias Ringwald // Authorization 4225611a760SMatthias Ringwald case SM_EVENT_AUTHORIZATION_RESULT: { 42318707219SMatthias Ringwald con_handle = sm_event_authorization_result_get_handle(packet); 42418707219SMatthias Ringwald att_server = att_server_for_handle(con_handle); 42518707219SMatthias Ringwald if (!att_server) break; 42618707219SMatthias Ringwald att_server->connection.authorized = sm_event_authorization_result_get_authorization_result(packet); 42718707219SMatthias Ringwald att_dispatch_server_request_can_send_now_event(con_handle); 4283deb3ec6SMatthias Ringwald break; 4293deb3ec6SMatthias Ringwald } 4303deb3ec6SMatthias Ringwald default: 4313deb3ec6SMatthias Ringwald break; 4323deb3ec6SMatthias Ringwald } 43365b44ffdSMatthias Ringwald break; 43470dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 43570dca4d4SMatthias Ringwald case L2CAP_DATA_PACKET: 43670dca4d4SMatthias Ringwald att_server = att_server_for_l2cap_cid(channel); 43770dca4d4SMatthias Ringwald if (!att_server) break; 43841772f37SMatthias Ringwald 43941772f37SMatthias Ringwald att_server_handle_att_pdu(att_server, packet, size); 44070dca4d4SMatthias Ringwald break; 44170dca4d4SMatthias Ringwald #endif 44241772f37SMatthias Ringwald 44365b44ffdSMatthias Ringwald default: 44465b44ffdSMatthias Ringwald break; 4453deb3ec6SMatthias Ringwald } 4463deb3ec6SMatthias Ringwald } 4473deb3ec6SMatthias Ringwald 4487a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 4493deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){ 4503deb3ec6SMatthias Ringwald 45118707219SMatthias Ringwald att_server_t * att_server = att_server_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION); 45218707219SMatthias Ringwald if (!att_server) return; 4533deb3ec6SMatthias Ringwald 4543deb3ec6SMatthias Ringwald uint8_t hash_flipped[8]; 4559c80e4ccSMatthias Ringwald reverse_64(hash, hash_flipped); 45618707219SMatthias Ringwald if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8)){ 4573deb3ec6SMatthias Ringwald log_info("ATT Signed Write, invalid signature"); 45818707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 4593deb3ec6SMatthias Ringwald return; 4603deb3ec6SMatthias Ringwald } 4613deb3ec6SMatthias Ringwald log_info("ATT Signed Write, valid signature"); 4623deb3ec6SMatthias Ringwald 4633deb3ec6SMatthias Ringwald // update sequence number 46418707219SMatthias Ringwald uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12); 46518707219SMatthias Ringwald le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1); 46618707219SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 46718707219SMatthias Ringwald att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle); 4683deb3ec6SMatthias Ringwald } 4697a766ebfSMatthias Ringwald #endif 47088a48dd8SMatthias Ringwald 47118707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED 472b06f2579SMatthias Ringwald // pre: can send now 473b06f2579SMatthias Ringwald // returns: 1 if packet was sent 47418707219SMatthias Ringwald static int att_server_process_validated_request(att_server_t * att_server){ 475b06f2579SMatthias Ringwald 476b06f2579SMatthias Ringwald l2cap_reserve_packet_buffer(); 477b06f2579SMatthias Ringwald uint8_t * att_response_buffer = l2cap_get_outgoing_buffer(); 47818707219SMatthias Ringwald uint16_t att_response_size = att_handle_request(&att_server->connection, att_server->request_buffer, att_server->request_size, att_response_buffer); 479b06f2579SMatthias Ringwald 480beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE 48156c3a347SMatthias Ringwald if (att_response_size == ATT_READ_RESPONSE_PENDING || att_response_size == ATT_INTERNAL_WRITE_RESPONSE_PENDING){ 482e404a688SMatthias Ringwald // update state 483beb20288SMatthias Ringwald att_server->state = ATT_SERVER_RESPONSE_PENDING; 484e404a688SMatthias Ringwald 48556c3a347SMatthias Ringwald // callback with handle ATT_READ_RESPONSE_PENDING for reads 48656c3a347SMatthias Ringwald if (att_response_size == ATT_READ_RESPONSE_PENDING){ 487e404a688SMatthias Ringwald att_server_client_read_callback(att_server->connection.con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0); 48856c3a347SMatthias Ringwald } 489e8e7403eSMatthias Ringwald 490e8e7403eSMatthias Ringwald // free reserved buffer 491e8e7403eSMatthias Ringwald l2cap_release_packet_buffer(); 492e8e7403eSMatthias Ringwald return 0; 493e404a688SMatthias Ringwald } 494e404a688SMatthias Ringwald #endif 495e404a688SMatthias Ringwald 496b06f2579SMatthias Ringwald // intercept "insufficient authorization" for authenticated connections to allow for user authorization 497b06f2579SMatthias Ringwald if ((att_response_size >= 4) 498b06f2579SMatthias Ringwald && (att_response_buffer[0] == ATT_ERROR_RESPONSE) 499b06f2579SMatthias Ringwald && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION) 50018707219SMatthias Ringwald && (att_server->connection.authenticated)){ 501b06f2579SMatthias Ringwald 5029c6e867eSMatthias Ringwald switch (gap_authorization_state(att_server->connection.con_handle)){ 503b06f2579SMatthias Ringwald case AUTHORIZATION_UNKNOWN: 504b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 50518707219SMatthias Ringwald sm_request_pairing(att_server->connection.con_handle); 506b06f2579SMatthias Ringwald return 0; 507b06f2579SMatthias Ringwald case AUTHORIZATION_PENDING: 508b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 509b06f2579SMatthias Ringwald return 0; 510b06f2579SMatthias Ringwald default: 511b06f2579SMatthias Ringwald break; 512b06f2579SMatthias Ringwald } 513b06f2579SMatthias Ringwald } 514b06f2579SMatthias Ringwald 51518707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 516b06f2579SMatthias Ringwald if (att_response_size == 0) { 517b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 518b06f2579SMatthias Ringwald return 0; 519b06f2579SMatthias Ringwald } 520b06f2579SMatthias Ringwald 52118707219SMatthias Ringwald l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size); 522b06f2579SMatthias Ringwald 523b06f2579SMatthias Ringwald // notify client about MTU exchange result 524b06f2579SMatthias Ringwald if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){ 52518707219SMatthias Ringwald att_emit_mtu_event(att_server->connection.con_handle, att_server->connection.mtu); 526b06f2579SMatthias Ringwald } 527b06f2579SMatthias Ringwald return 1; 528b06f2579SMatthias Ringwald } 529b06f2579SMatthias Ringwald 530beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE 531beb20288SMatthias Ringwald int att_server_response_ready(hci_con_handle_t con_handle){ 532e404a688SMatthias Ringwald att_server_t * att_server = att_server_for_handle(con_handle); 533e404a688SMatthias Ringwald if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 534beb20288SMatthias Ringwald if (att_server->state != ATT_SERVER_RESPONSE_PENDING) return ERROR_CODE_COMMAND_DISALLOWED; 535e404a688SMatthias Ringwald 536e404a688SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 537e404a688SMatthias Ringwald att_dispatch_server_request_can_send_now_event(con_handle); 538e404a688SMatthias Ringwald return ERROR_CODE_SUCCESS; 539e404a688SMatthias Ringwald } 540e404a688SMatthias Ringwald #endif 541e404a688SMatthias Ringwald 542*ed053a35SMatthias Ringwald static void att_server_request_can_send_now(att_server_t * att_server){ 543*ed053a35SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 544*ed053a35SMatthias Ringwald if (att_server->l2cap_cid != 0){ 545*ed053a35SMatthias Ringwald l2cap_request_can_send_now_event(att_server->l2cap_cid); 546*ed053a35SMatthias Ringwald return; 547*ed053a35SMatthias Ringwald } 548*ed053a35SMatthias Ringwald #endif 549*ed053a35SMatthias Ringwald att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle); 550*ed053a35SMatthias Ringwald } 551*ed053a35SMatthias Ringwald 55218707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server){ 55318707219SMatthias Ringwald switch (att_server->state){ 5543deb3ec6SMatthias Ringwald case ATT_SERVER_REQUEST_RECEIVED: 555760ad805SMatthias Ringwald 5560234fbbcSMatthias Ringwald // wait until re-encryption as central is complete 5570234fbbcSMatthias Ringwald if (gap_reconnect_security_setup_active(att_server->connection.con_handle)) break; 5580234fbbcSMatthias Ringwald 559760ad805SMatthias Ringwald // wait until pairing is complete 560760ad805SMatthias Ringwald if (att_server->pairing_active) break; 561760ad805SMatthias Ringwald 5627a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 56318707219SMatthias Ringwald if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){ 5643deb3ec6SMatthias Ringwald log_info("ATT Signed Write!"); 5653deb3ec6SMatthias Ringwald if (!sm_cmac_ready()) { 5663deb3ec6SMatthias Ringwald log_info("ATT Signed Write, sm_cmac engine not ready. Abort"); 56718707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 5683deb3ec6SMatthias Ringwald return; 5693deb3ec6SMatthias Ringwald } 57018707219SMatthias Ringwald if (att_server->request_size < (3 + 12)) { 5713deb3ec6SMatthias Ringwald log_info("ATT Signed Write, request to short. Abort."); 57218707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 5733deb3ec6SMatthias Ringwald return; 5743deb3ec6SMatthias Ringwald } 57518707219SMatthias Ringwald if (att_server->ir_lookup_active){ 5763deb3ec6SMatthias Ringwald return; 5773deb3ec6SMatthias Ringwald } 57818707219SMatthias Ringwald if (att_server->ir_le_device_db_index < 0){ 5793deb3ec6SMatthias Ringwald log_info("ATT Signed Write, CSRK not available"); 58018707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 5813deb3ec6SMatthias Ringwald return; 5823deb3ec6SMatthias Ringwald } 5833deb3ec6SMatthias Ringwald 5843deb3ec6SMatthias Ringwald // check counter 58518707219SMatthias Ringwald uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12); 58618707219SMatthias Ringwald uint32_t counter_db = le_device_db_remote_counter_get(att_server->ir_le_device_db_index); 5873deb3ec6SMatthias Ringwald log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet); 5883deb3ec6SMatthias Ringwald if (counter_packet < counter_db){ 5893deb3ec6SMatthias Ringwald log_info("ATT Signed Write, db reports higher counter, abort"); 59018707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 5913deb3ec6SMatthias Ringwald return; 5923deb3ec6SMatthias Ringwald } 5933deb3ec6SMatthias Ringwald 5943deb3ec6SMatthias Ringwald // signature is { sequence counter, secure hash } 5953deb3ec6SMatthias Ringwald sm_key_t csrk; 59618707219SMatthias Ringwald le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk); 59718707219SMatthias Ringwald att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION; 5983deb3ec6SMatthias Ringwald log_info("Orig Signature: "); 59918707219SMatthias Ringwald log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8); 60018707219SMatthias Ringwald uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1); 60118707219SMatthias 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); 6023deb3ec6SMatthias Ringwald return; 6033deb3ec6SMatthias Ringwald } 6047a766ebfSMatthias Ringwald #endif 605b06f2579SMatthias Ringwald // move on 60618707219SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 607*ed053a35SMatthias Ringwald att_server_request_can_send_now(att_server); 608b06f2579SMatthias Ringwald break; 60988a48dd8SMatthias Ringwald 6103deb3ec6SMatthias Ringwald default: 6113deb3ec6SMatthias Ringwald break; 6123deb3ec6SMatthias Ringwald } 6133deb3ec6SMatthias Ringwald } 6143deb3ec6SMatthias Ringwald 61590f03c80SMatthias Ringwald static int att_server_data_ready_for_phase(att_server_t * att_server, att_server_run_phase_t phase){ 61690f03c80SMatthias Ringwald switch (phase){ 61790f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_1_REQUESTS: 61890f03c80SMatthias Ringwald return att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 61990f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_2_INDICATIONS: 62090f03c80SMatthias Ringwald return (!btstack_linked_list_empty(&att_server->indication_requests) && att_server->value_indication_handle == 0); 62190f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS: 62290f03c80SMatthias Ringwald return (!btstack_linked_list_empty(&att_server->notification_requests)); 62390f03c80SMatthias Ringwald } 624fa5e3464SMatthias Ringwald // avoid warning 625fa5e3464SMatthias Ringwald return 0; 62690f03c80SMatthias Ringwald } 62790f03c80SMatthias Ringwald 62890f03c80SMatthias Ringwald static void att_server_trigger_send_for_phase(att_server_t * att_server, att_server_run_phase_t phase){ 62990f03c80SMatthias Ringwald btstack_context_callback_registration_t * client; 63090f03c80SMatthias Ringwald switch (phase){ 63190f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_1_REQUESTS: 63290f03c80SMatthias Ringwald att_server_process_validated_request(att_server); 63390f03c80SMatthias Ringwald break; 63490f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_2_INDICATIONS: 63590f03c80SMatthias Ringwald client = (btstack_context_callback_registration_t*) att_server->indication_requests; 63690f03c80SMatthias Ringwald btstack_linked_list_remove(&att_server->indication_requests, (btstack_linked_item_t *) client); 63790f03c80SMatthias Ringwald client->callback(client->context); 63890f03c80SMatthias Ringwald break; 63990f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS: 64090f03c80SMatthias Ringwald client = (btstack_context_callback_registration_t*) att_server->notification_requests; 64190f03c80SMatthias Ringwald btstack_linked_list_remove(&att_server->notification_requests, (btstack_linked_item_t *) client); 64290f03c80SMatthias Ringwald client->callback(client->context); 64390f03c80SMatthias Ringwald break; 64490f03c80SMatthias Ringwald } 64590f03c80SMatthias Ringwald } 64690f03c80SMatthias Ringwald 6477eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){ 648b06f2579SMatthias Ringwald 649374a288aSMatthias Ringwald hci_con_handle_t request_con_handle = HCI_CON_HANDLE_INVALID; 6506438547aSMatthias Ringwald hci_con_handle_t last_send_con_handle = HCI_CON_HANDLE_INVALID; 6516438547aSMatthias Ringwald int can_send_now = 1; 6524395a000SMatthias Ringwald int phase_index; 6536438547aSMatthias Ringwald 6544395a000SMatthias Ringwald for (phase_index = ATT_SERVER_RUN_PHASE_1_REQUESTS; phase_index <= ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS; phase_index++){ 6554395a000SMatthias Ringwald att_server_run_phase_t phase = (att_server_run_phase_t) phase_index; 6566438547aSMatthias Ringwald hci_con_handle_t skip_connections_until = att_server_last_can_send_now; 657374a288aSMatthias Ringwald while (1){ 6586438547aSMatthias Ringwald btstack_linked_list_iterator_t it; 65918707219SMatthias Ringwald hci_connections_get_iterator(&it); 66018707219SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 66118707219SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 66218707219SMatthias Ringwald att_server_t * att_server = &connection->att_server; 6636438547aSMatthias Ringwald 66490f03c80SMatthias Ringwald int data_ready = att_server_data_ready_for_phase(att_server, phase); 6656438547aSMatthias Ringwald 6666438547aSMatthias Ringwald // log_debug("phase %u, handle 0x%04x, skip until 0x%04x, data ready %u", phase, att_server->connection.con_handle, skip_connections_until, data_ready); 6676438547aSMatthias Ringwald 6686438547aSMatthias Ringwald // skip until last sender found (which is also skipped) 6696438547aSMatthias Ringwald if (skip_connections_until != HCI_CON_HANDLE_INVALID){ 6706438547aSMatthias Ringwald if (data_ready && request_con_handle == HCI_CON_HANDLE_INVALID){ 6716438547aSMatthias Ringwald request_con_handle = att_server->connection.con_handle; 6726438547aSMatthias Ringwald } 6736438547aSMatthias Ringwald if (skip_connections_until == att_server->connection.con_handle){ 6746438547aSMatthias Ringwald skip_connections_until = HCI_CON_HANDLE_INVALID; 6756438547aSMatthias Ringwald } 6766438547aSMatthias Ringwald continue; 6776438547aSMatthias Ringwald }; 6786438547aSMatthias Ringwald 67990f03c80SMatthias Ringwald if (data_ready){ 680969a5bbaSMatthias Ringwald if (can_send_now){ 68190f03c80SMatthias Ringwald att_server_trigger_send_for_phase(att_server, phase); 6826438547aSMatthias Ringwald last_send_con_handle = att_server->connection.con_handle; 683969a5bbaSMatthias Ringwald can_send_now = att_dispatch_server_can_send_now(att_server->connection.con_handle); 68490f03c80SMatthias Ringwald data_ready = att_server_data_ready_for_phase(att_server, phase); 6856438547aSMatthias Ringwald if (data_ready && request_con_handle == HCI_CON_HANDLE_INVALID){ 686cbbb12d9SMatthias Ringwald request_con_handle = att_server->connection.con_handle; 687cbbb12d9SMatthias Ringwald } 688cbbb12d9SMatthias Ringwald } else { 689f29a88d8SMatthias Ringwald request_con_handle = att_server->connection.con_handle; 690f29a88d8SMatthias Ringwald break; 691cbbb12d9SMatthias Ringwald } 692cbbb12d9SMatthias Ringwald } 6933deb3ec6SMatthias Ringwald } 6943deb3ec6SMatthias Ringwald 6956438547aSMatthias Ringwald // stop skipping (handles disconnect by last send connection) 6966438547aSMatthias Ringwald skip_connections_until = HCI_CON_HANDLE_INVALID; 6976438547aSMatthias Ringwald 6983551936eSMatthias Ringwald // Exit loop, if we cannot send 699969a5bbaSMatthias Ringwald if (!can_send_now) break; 700969a5bbaSMatthias Ringwald 701969a5bbaSMatthias Ringwald // Exit loop, if we can send but there are also no further request 702969a5bbaSMatthias Ringwald if (request_con_handle == HCI_CON_HANDLE_INVALID) break; 703969a5bbaSMatthias Ringwald 704969a5bbaSMatthias Ringwald // Finally, if we still can send and there are requests, just try again 705969a5bbaSMatthias Ringwald request_con_handle = HCI_CON_HANDLE_INVALID; 706969a5bbaSMatthias Ringwald } 7076438547aSMatthias Ringwald // update last send con handle for round robin 7086438547aSMatthias Ringwald if (last_send_con_handle != HCI_CON_HANDLE_INVALID){ 7096438547aSMatthias Ringwald att_server_last_can_send_now = last_send_con_handle; 7106438547aSMatthias Ringwald } 7113551936eSMatthias Ringwald } 712969a5bbaSMatthias Ringwald 713969a5bbaSMatthias Ringwald if (request_con_handle == HCI_CON_HANDLE_INVALID) return; 714969a5bbaSMatthias Ringwald att_dispatch_server_request_can_send_now_event(request_con_handle); 715969a5bbaSMatthias Ringwald } 716969a5bbaSMatthias Ringwald 71741772f37SMatthias Ringwald static void att_server_handle_att_pdu(att_server_t * att_server, uint8_t * packet, uint16_t size){ 71818707219SMatthias Ringwald 7193deb3ec6SMatthias Ringwald // handle value indication confirms 72018707219SMatthias Ringwald if (packet[0] == ATT_HANDLE_VALUE_CONFIRMATION && att_server->value_indication_handle){ 72118707219SMatthias Ringwald btstack_run_loop_remove_timer(&att_server->value_indication_timer); 72218707219SMatthias Ringwald uint16_t att_handle = att_server->value_indication_handle; 72318707219SMatthias Ringwald att_server->value_indication_handle = 0; 72418707219SMatthias Ringwald att_handle_value_indication_notify_client(0, att_server->connection.con_handle, att_handle); 72541772f37SMatthias Ringwald att_server_request_can_send_now(att_server); 7263deb3ec6SMatthias Ringwald return; 7273deb3ec6SMatthias Ringwald } 7283deb3ec6SMatthias Ringwald 7296428eb5aSMatthias Ringwald // directly process command 7306428eb5aSMatthias Ringwald // note: signed write cannot be handled directly as authentication needs to be verified 7316428eb5aSMatthias Ringwald if (packet[0] == ATT_WRITE_COMMAND){ 73218707219SMatthias Ringwald att_handle_request(&att_server->connection, packet, size, 0); 7336428eb5aSMatthias Ringwald return; 7346428eb5aSMatthias Ringwald } 7356428eb5aSMatthias Ringwald 7363deb3ec6SMatthias Ringwald // check size 73718707219SMatthias Ringwald if (size > sizeof(att_server->request_buffer)) { 738e0463bdcSMatthias 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)); 7396428eb5aSMatthias Ringwald return; 7406428eb5aSMatthias Ringwald } 7413deb3ec6SMatthias Ringwald 742e0463bdcSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 743e0463bdcSMatthias Ringwald // abort signed write validation if a new request comes in (but finish previous signed write if possible) 744e0463bdcSMatthias Ringwald if (att_server->state == ATT_SERVER_W4_SIGNED_WRITE_VALIDATION){ 745e0463bdcSMatthias Ringwald if (packet[0] == ATT_SIGNED_WRITE_COMMAND){ 746e0463bdcSMatthias Ringwald log_info("skip new signed write request as previous is in validation"); 747e0463bdcSMatthias Ringwald return; 748e0463bdcSMatthias Ringwald } else { 749e0463bdcSMatthias Ringwald log_info("abort signed write validation to process new request"); 750e0463bdcSMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 751e0463bdcSMatthias Ringwald } 752e0463bdcSMatthias Ringwald } 753e0463bdcSMatthias Ringwald #endif 7543deb3ec6SMatthias Ringwald // last request still in processing? 75518707219SMatthias Ringwald if (att_server->state != ATT_SERVER_IDLE){ 756e0463bdcSMatthias Ringwald log_info("skip att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state); 7576428eb5aSMatthias Ringwald return; 7586428eb5aSMatthias Ringwald } 7593deb3ec6SMatthias Ringwald 7603deb3ec6SMatthias Ringwald // store request 76118707219SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED; 76218707219SMatthias Ringwald att_server->request_size = size; 76318707219SMatthias Ringwald memcpy(att_server->request_buffer, packet, size); 7643deb3ec6SMatthias Ringwald 76518707219SMatthias Ringwald att_run_for_context(att_server); 76641772f37SMatthias Ringwald } 76741772f37SMatthias Ringwald 76841772f37SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){ 76941772f37SMatthias Ringwald att_server_t * att_server; 77041772f37SMatthias Ringwald 77141772f37SMatthias Ringwald switch (packet_type){ 77241772f37SMatthias Ringwald case HCI_EVENT_PACKET: 77341772f37SMatthias Ringwald switch (packet[0]){ 77441772f37SMatthias Ringwald case L2CAP_EVENT_CAN_SEND_NOW: 77541772f37SMatthias Ringwald att_server_handle_can_send_now(); 77641772f37SMatthias Ringwald break; 77741772f37SMatthias Ringwald case ATT_EVENT_MTU_EXCHANGE_COMPLETE: 77841772f37SMatthias Ringwald // GATT client has negotiated the mtu for this connection 77941772f37SMatthias Ringwald att_server = att_server_for_handle(handle); 78041772f37SMatthias Ringwald if (!att_server) break; 78141772f37SMatthias Ringwald att_server->connection.mtu = little_endian_read_16(packet, 4); 78241772f37SMatthias Ringwald break; 78341772f37SMatthias Ringwald default: 78441772f37SMatthias Ringwald break; 78541772f37SMatthias Ringwald } 78641772f37SMatthias Ringwald break; 78741772f37SMatthias Ringwald 78841772f37SMatthias Ringwald case ATT_DATA_PACKET: 78941772f37SMatthias Ringwald log_debug("ATT Packet, handle 0x%04x", handle); 79041772f37SMatthias Ringwald att_server = att_server_for_handle(handle); 79141772f37SMatthias Ringwald if (!att_server) break; 79241772f37SMatthias Ringwald 79341772f37SMatthias Ringwald att_server_handle_att_pdu(att_server, packet, size); 794372d62c4SMatthias Ringwald break; 795372d62c4SMatthias Ringwald } 7963deb3ec6SMatthias Ringwald } 7973deb3ec6SMatthias Ringwald 798bf78aac6SMatthias Ringwald // --------------------- 799bf78aac6SMatthias Ringwald // persistent CCC writes 800bf78aac6SMatthias Ringwald static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){ 801bf78aac6SMatthias Ringwald return 'B' << 24 | 'T' << 16 | 'C' << 8 | index; 802bf78aac6SMatthias Ringwald } 803bf78aac6SMatthias Ringwald 804bf78aac6SMatthias Ringwald static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){ 805bf78aac6SMatthias Ringwald // lookup att_server instance 806bf78aac6SMatthias Ringwald att_server_t * att_server = att_server_for_handle(con_handle); 807bf78aac6SMatthias Ringwald if (!att_server) return; 808bf78aac6SMatthias Ringwald int le_device_index = att_server->ir_le_device_db_index; 809bf78aac6SMatthias 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); 8106a540790SMatthias Ringwald 811bf78aac6SMatthias Ringwald // check if bonded 812bf78aac6SMatthias Ringwald if (le_device_index < 0) return; 8136a540790SMatthias Ringwald 814bf78aac6SMatthias Ringwald // get btstack_tlv 815bf78aac6SMatthias Ringwald const btstack_tlv_t * tlv_impl = NULL; 816bf78aac6SMatthias Ringwald void * tlv_context; 817bf78aac6SMatthias Ringwald btstack_tlv_get_instance(&tlv_impl, &tlv_context); 818bf78aac6SMatthias Ringwald if (!tlv_impl) return; 8196a540790SMatthias Ringwald 820bf78aac6SMatthias Ringwald // update ccc tag 821bf78aac6SMatthias Ringwald int index; 8226a540790SMatthias Ringwald uint32_t highest_seq_nr = 0; 8236a540790SMatthias Ringwald uint32_t lowest_seq_nr = 0; 8246a540790SMatthias Ringwald uint32_t tag_for_lowest_seq_nr = 0; 8256a540790SMatthias Ringwald uint32_t tag_for_empty = 0; 8266a540790SMatthias Ringwald persistent_ccc_entry_t entry; 8276afb9acaSMatthias Ringwald for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){ 828bf78aac6SMatthias Ringwald uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 8296a540790SMatthias Ringwald int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 8306a540790SMatthias Ringwald 8316a540790SMatthias Ringwald // empty/invalid tag 8326a540790SMatthias Ringwald if (len != sizeof(persistent_ccc_entry_t)){ 8336a540790SMatthias Ringwald tag_for_empty = tag; 834bf78aac6SMatthias Ringwald continue; 835bf78aac6SMatthias Ringwald } 8366a540790SMatthias Ringwald // update highest seq nr 8376a540790SMatthias Ringwald if (entry.seq_nr > highest_seq_nr){ 8386a540790SMatthias Ringwald highest_seq_nr = entry.seq_nr; 8396a540790SMatthias Ringwald } 8406a540790SMatthias Ringwald // find entry with lowest seq nr 8416a540790SMatthias Ringwald if ((tag_for_lowest_seq_nr == 0) || (entry.seq_nr < lowest_seq_nr)){ 8426a540790SMatthias Ringwald tag_for_lowest_seq_nr = tag; 8436a540790SMatthias Ringwald lowest_seq_nr = entry.seq_nr; 8446a540790SMatthias Ringwald } 8456a540790SMatthias Ringwald 8466a540790SMatthias Ringwald if (entry.device_index != le_device_index) continue; 8476a540790SMatthias Ringwald if (entry.att_handle != att_handle) continue; 8486a540790SMatthias Ringwald 8496a540790SMatthias Ringwald // found matching entry 85001ac2008SMatthias Ringwald if (value){ 851bf78aac6SMatthias Ringwald // update 8526a540790SMatthias Ringwald if (entry.value == value) { 853760ad805SMatthias Ringwald log_info("CCC Index %u: Up-to-date", index); 854760ad805SMatthias Ringwald return; 855760ad805SMatthias Ringwald } 8566a540790SMatthias Ringwald entry.value = value; 8576a540790SMatthias Ringwald entry.seq_nr = highest_seq_nr + 1; 858760ad805SMatthias Ringwald log_info("CCC Index %u: Store", index); 8596a540790SMatthias Ringwald tlv_impl->store_tag(tlv_context, tag, (const uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 86001ac2008SMatthias Ringwald } else { 86101ac2008SMatthias Ringwald // delete 862760ad805SMatthias Ringwald log_info("CCC Index %u: Delete", index); 86301ac2008SMatthias Ringwald tlv_impl->delete_tag(tlv_context, tag); 86401ac2008SMatthias Ringwald } 865bf78aac6SMatthias Ringwald return; 866bf78aac6SMatthias Ringwald } 8676a540790SMatthias Ringwald 868faf2b6c3SMatthias Ringwald log_info("tag_for_empy %"PRIx32", tag_for_lowest_seq_nr %"PRIx32, tag_for_empty, tag_for_lowest_seq_nr); 8696a540790SMatthias Ringwald 8706a540790SMatthias Ringwald if (value == 0){ 8716a540790SMatthias Ringwald // done 8726a540790SMatthias Ringwald return; 8736a540790SMatthias Ringwald } 8746a540790SMatthias Ringwald 8756a540790SMatthias Ringwald uint32_t tag_to_use = 0; 8766a540790SMatthias Ringwald if (tag_for_empty){ 8776a540790SMatthias Ringwald tag_to_use = tag_for_empty; 8786a540790SMatthias Ringwald } else if (tag_for_lowest_seq_nr){ 8796a540790SMatthias Ringwald tag_to_use = tag_for_lowest_seq_nr; 8806a540790SMatthias Ringwald } else { 8816a540790SMatthias Ringwald // should not happen 8826a540790SMatthias Ringwald return; 8836a540790SMatthias Ringwald } 884bf78aac6SMatthias Ringwald // store ccc tag 8856a540790SMatthias Ringwald entry.seq_nr = highest_seq_nr + 1; 8866a540790SMatthias Ringwald entry.device_index = le_device_index; 8876a540790SMatthias Ringwald entry.att_handle = att_handle; 8886a540790SMatthias Ringwald entry.value = value; 8896a540790SMatthias Ringwald tlv_impl->store_tag(tlv_context, tag_to_use, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 890bf78aac6SMatthias Ringwald } 891bf78aac6SMatthias Ringwald 892760ad805SMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server){ 893760ad805SMatthias Ringwald if (!att_server) return; 894760ad805SMatthias Ringwald int le_device_index = att_server->ir_le_device_db_index; 895760ad805SMatthias Ringwald log_info("Clear CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index); 896760ad805SMatthias Ringwald // check if bonded 897760ad805SMatthias Ringwald if (le_device_index < 0) return; 898760ad805SMatthias Ringwald // get btstack_tlv 899760ad805SMatthias Ringwald const btstack_tlv_t * tlv_impl = NULL; 900760ad805SMatthias Ringwald void * tlv_context; 901760ad805SMatthias Ringwald btstack_tlv_get_instance(&tlv_impl, &tlv_context); 902760ad805SMatthias Ringwald if (!tlv_impl) return; 903760ad805SMatthias Ringwald // get all ccc tag 904760ad805SMatthias Ringwald int index; 9056a540790SMatthias Ringwald persistent_ccc_entry_t entry; 9066afb9acaSMatthias Ringwald for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){ 907760ad805SMatthias Ringwald uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 9086a540790SMatthias Ringwald int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 9096a540790SMatthias Ringwald if (len != sizeof(persistent_ccc_entry_t)) continue; 9106a540790SMatthias Ringwald if (entry.device_index != le_device_index) continue; 911760ad805SMatthias Ringwald // delete entry 912760ad805SMatthias Ringwald log_info("CCC Index %u: Delete", index); 913760ad805SMatthias Ringwald tlv_impl->delete_tag(tlv_context, tag); 914760ad805SMatthias Ringwald } 915760ad805SMatthias Ringwald } 916760ad805SMatthias Ringwald 91701ac2008SMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server){ 91801ac2008SMatthias Ringwald if (!att_server) return; 91901ac2008SMatthias Ringwald int le_device_index = att_server->ir_le_device_db_index; 92001ac2008SMatthias Ringwald log_info("Restore CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index); 9211b7acd0dSMatthias Ringwald // check if bonded 9221b7acd0dSMatthias Ringwald if (le_device_index < 0) return; 92301ac2008SMatthias Ringwald // get btstack_tlv 92401ac2008SMatthias Ringwald const btstack_tlv_t * tlv_impl = NULL; 92501ac2008SMatthias Ringwald void * tlv_context; 92601ac2008SMatthias Ringwald btstack_tlv_get_instance(&tlv_impl, &tlv_context); 92701ac2008SMatthias Ringwald if (!tlv_impl) return; 92801ac2008SMatthias Ringwald // get all ccc tag 92901ac2008SMatthias Ringwald int index; 9306a540790SMatthias Ringwald persistent_ccc_entry_t entry; 9316afb9acaSMatthias Ringwald for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){ 93201ac2008SMatthias Ringwald uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 9336a540790SMatthias Ringwald int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 9346a540790SMatthias Ringwald if (len != sizeof(persistent_ccc_entry_t)) continue; 9356a540790SMatthias Ringwald if (entry.device_index != le_device_index) continue; 93601ac2008SMatthias Ringwald // simulate write callback 9376a540790SMatthias Ringwald uint16_t attribute_handle = entry.att_handle; 93801ac2008SMatthias Ringwald uint8_t value[2]; 9396a540790SMatthias Ringwald little_endian_store_16(value, 0, entry.value); 94001ac2008SMatthias Ringwald att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle); 94101ac2008SMatthias Ringwald if (!callback) continue; 9426a540790SMatthias Ringwald log_info("CCC Index %u: Set Attribute handle 0x%04x to value 0x%04x", index, attribute_handle, entry.value ); 94301ac2008SMatthias Ringwald (*callback)(att_server->connection.con_handle, attribute_handle, ATT_TRANSACTION_MODE_NONE, 0, value, sizeof(value)); 94401ac2008SMatthias Ringwald } 94501ac2008SMatthias Ringwald } 94601ac2008SMatthias Ringwald 947bf78aac6SMatthias Ringwald // persistent CCC writes 948bf78aac6SMatthias Ringwald // --------------------- 9493d71c7a4SMatthias Ringwald 9503d71c7a4SMatthias Ringwald // gatt service management 9513d71c7a4SMatthias Ringwald static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){ 9523d71c7a4SMatthias Ringwald btstack_linked_list_iterator_t it; 9533d71c7a4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 9543d71c7a4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 9553d71c7a4SMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 9563d71c7a4SMatthias Ringwald if (handler->start_handle > handle) continue; 9573d71c7a4SMatthias Ringwald if (handler->end_handle < handle) continue; 9583d71c7a4SMatthias Ringwald return handler; 9593d71c7a4SMatthias Ringwald } 9603d71c7a4SMatthias Ringwald return NULL; 9613d71c7a4SMatthias Ringwald } 9623d71c7a4SMatthias Ringwald static att_read_callback_t att_server_read_callback_for_handle(uint16_t handle){ 9633d71c7a4SMatthias Ringwald att_service_handler_t * handler = att_service_handler_for_handle(handle); 9643d71c7a4SMatthias Ringwald if (handler) return handler->read_callback; 9653d71c7a4SMatthias Ringwald return att_server_client_read_callback; 9663d71c7a4SMatthias Ringwald } 9673d71c7a4SMatthias Ringwald 9683d71c7a4SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){ 9693d71c7a4SMatthias Ringwald att_service_handler_t * handler = att_service_handler_for_handle(handle); 9703d71c7a4SMatthias Ringwald if (handler) return handler->write_callback; 9713d71c7a4SMatthias Ringwald return att_server_client_write_callback; 9723d71c7a4SMatthias Ringwald } 9733d71c7a4SMatthias Ringwald 9745d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle){ 9755d07396bSMatthias Ringwald att_service_handler_t * handler = att_service_handler_for_handle(handle); 9765d07396bSMatthias Ringwald if (handler) return handler->packet_handler; 9775d07396bSMatthias Ringwald return att_client_packet_handler; 9785d07396bSMatthias Ringwald } 9795d07396bSMatthias Ringwald 9803d71c7a4SMatthias Ringwald static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){ 9813d71c7a4SMatthias Ringwald // notify all callbacks 9823d71c7a4SMatthias Ringwald btstack_linked_list_iterator_t it; 9833d71c7a4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 9843d71c7a4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 9853d71c7a4SMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 9863d71c7a4SMatthias Ringwald if (!handler->write_callback) continue; 9873d71c7a4SMatthias Ringwald (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0); 9883d71c7a4SMatthias Ringwald } 9893d71c7a4SMatthias Ringwald if (!att_server_client_write_callback) return; 9903d71c7a4SMatthias Ringwald (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0); 9913d71c7a4SMatthias Ringwald } 9923d71c7a4SMatthias Ringwald 9933d71c7a4SMatthias Ringwald // returns first reported error or 0 9943d71c7a4SMatthias Ringwald static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){ 9953d71c7a4SMatthias Ringwald btstack_linked_list_iterator_t it; 9963d71c7a4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 9973d71c7a4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 9983d71c7a4SMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 9993d71c7a4SMatthias Ringwald if (!handler->write_callback) continue; 10003d71c7a4SMatthias Ringwald uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0); 10013d71c7a4SMatthias Ringwald if (error_code) return error_code; 10023d71c7a4SMatthias Ringwald } 10033d71c7a4SMatthias Ringwald if (!att_server_client_write_callback) return 0; 10043d71c7a4SMatthias Ringwald return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0); 10053d71c7a4SMatthias Ringwald } 10063d71c7a4SMatthias Ringwald 10073d71c7a4SMatthias 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){ 10083d71c7a4SMatthias Ringwald att_read_callback_t callback = att_server_read_callback_for_handle(attribute_handle); 100901ac2008SMatthias Ringwald if (!callback) return 0; 10103d71c7a4SMatthias Ringwald return (*callback)(con_handle, attribute_handle, offset, buffer, buffer_size); 10113d71c7a4SMatthias Ringwald } 10123d71c7a4SMatthias Ringwald 10133d71c7a4SMatthias 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){ 10143d71c7a4SMatthias Ringwald switch (transaction_mode){ 10153d71c7a4SMatthias Ringwald case ATT_TRANSACTION_MODE_VALIDATE: 10163d71c7a4SMatthias Ringwald return att_validate_prepared_write(con_handle); 10173d71c7a4SMatthias Ringwald case ATT_TRANSACTION_MODE_EXECUTE: 10183d71c7a4SMatthias Ringwald case ATT_TRANSACTION_MODE_CANCEL: 10193d71c7a4SMatthias Ringwald att_notify_write_callbacks(con_handle, transaction_mode); 10203d71c7a4SMatthias Ringwald return 0; 10213d71c7a4SMatthias Ringwald default: 10223d71c7a4SMatthias Ringwald break; 10233d71c7a4SMatthias Ringwald } 10243d71c7a4SMatthias Ringwald 1025bf78aac6SMatthias Ringwald // track CCC writes 1026bf78aac6SMatthias Ringwald if (att_is_persistent_ccc(attribute_handle) && offset == 0 && buffer_size == 2){ 1027bf78aac6SMatthias Ringwald att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0)); 1028bf78aac6SMatthias Ringwald } 1029bf78aac6SMatthias Ringwald 103001ac2008SMatthias Ringwald att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle); 103101ac2008SMatthias Ringwald if (!callback) return 0; 10323d71c7a4SMatthias Ringwald return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size); 10333d71c7a4SMatthias Ringwald } 10343d71c7a4SMatthias Ringwald 10353d71c7a4SMatthias Ringwald /** 10363d71c7a4SMatthias Ringwald * @brief register read/write callbacks for specific handle range 10373d71c7a4SMatthias Ringwald * @param att_service_handler_t 10383d71c7a4SMatthias Ringwald */ 10393d71c7a4SMatthias Ringwald void att_server_register_service_handler(att_service_handler_t * handler){ 10403d71c7a4SMatthias Ringwald if (att_service_handler_for_handle(handler->start_handle) || 10413d71c7a4SMatthias Ringwald att_service_handler_for_handle(handler->end_handle)){ 10423d71c7a4SMatthias Ringwald log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle); 10433d71c7a4SMatthias Ringwald return; 10443d71c7a4SMatthias Ringwald } 10453d71c7a4SMatthias Ringwald btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler); 10463d71c7a4SMatthias Ringwald } 10473d71c7a4SMatthias Ringwald 10483deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){ 10493deb3ec6SMatthias Ringwald 10503d71c7a4SMatthias Ringwald // store callbacks 10513d71c7a4SMatthias Ringwald att_server_client_read_callback = read_callback; 10523d71c7a4SMatthias Ringwald att_server_client_write_callback = write_callback; 10533d71c7a4SMatthias Ringwald 10541a389cdcSMatthias Ringwald // register for HCI Events 1055d9a7306aSMatthias Ringwald hci_event_callback_registration.callback = &att_event_packet_handler; 10561a389cdcSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 10571a389cdcSMatthias Ringwald 1058406722e4SMatthias Ringwald // register for SM events 1059d9a7306aSMatthias Ringwald sm_event_callback_registration.callback = &att_event_packet_handler; 1060406722e4SMatthias Ringwald sm_add_event_handler(&sm_event_callback_registration); 10613deb3ec6SMatthias Ringwald 10621a389cdcSMatthias Ringwald // and L2CAP ATT Server PDUs 10633deb3ec6SMatthias Ringwald att_dispatch_register_server(att_packet_handler); 10643deb3ec6SMatthias Ringwald 106570dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 106670dca4d4SMatthias Ringwald // setup l2cap service 106770dca4d4SMatthias Ringwald l2cap_register_service(&att_event_packet_handler, PSM_ATT, 0xffff, LEVEL_2); 106870dca4d4SMatthias Ringwald #endif 106970dca4d4SMatthias Ringwald 10703deb3ec6SMatthias Ringwald att_set_db(db); 10713d71c7a4SMatthias Ringwald att_set_read_callback(att_server_read_callback); 10723d71c7a4SMatthias Ringwald att_set_write_callback(att_server_write_callback); 10733deb3ec6SMatthias Ringwald } 10743deb3ec6SMatthias Ringwald 10753deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){ 10763deb3ec6SMatthias Ringwald att_client_packet_handler = handler; 10773deb3ec6SMatthias Ringwald } 10783deb3ec6SMatthias Ringwald 1079cbbb12d9SMatthias Ringwald 1080cbbb12d9SMatthias Ringwald // to be deprecated 1081c141988cSMatthias Ringwald int att_server_can_send_packet_now(hci_con_handle_t con_handle){ 108218707219SMatthias Ringwald return att_dispatch_server_can_send_now(con_handle); 10831b96b007SMatthias Ringwald } 10841b96b007SMatthias Ringwald 1085cbbb12d9SMatthias Ringwald int att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 1086cbbb12d9SMatthias Ringwald return att_server_request_to_send_notification(callback_registration, con_handle); 10873deb3ec6SMatthias Ringwald } 10883deb3ec6SMatthias Ringwald 1089cbbb12d9SMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){ 1090cbbb12d9SMatthias Ringwald att_client_waiting_for_can_send_registration.callback = &att_emit_can_send_now_event; 1091cbbb12d9SMatthias Ringwald att_server_request_to_send_notification(&att_client_waiting_for_can_send_registration, con_handle); 1092cbbb12d9SMatthias Ringwald } 1093cbbb12d9SMatthias Ringwald // end of deprecated 1094cbbb12d9SMatthias Ringwald 1095cbbb12d9SMatthias Ringwald int att_server_request_to_send_notification(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 1096969a5bbaSMatthias Ringwald att_server_t * att_server = att_server_for_handle(con_handle); 1097969a5bbaSMatthias Ringwald if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1098cbbb12d9SMatthias Ringwald btstack_linked_list_add_tail(&att_server->notification_requests, (btstack_linked_item_t*) callback_registration); 1099cbbb12d9SMatthias Ringwald att_dispatch_server_request_can_send_now_event(con_handle); 1100cbbb12d9SMatthias Ringwald return ERROR_CODE_SUCCESS; 1101cbbb12d9SMatthias Ringwald } 1102cbbb12d9SMatthias Ringwald 1103cbbb12d9SMatthias Ringwald int att_server_request_to_send_indication(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 1104cbbb12d9SMatthias Ringwald att_server_t * att_server = att_server_for_handle(con_handle); 1105cbbb12d9SMatthias Ringwald if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1106cbbb12d9SMatthias Ringwald btstack_linked_list_add_tail(&att_server->indication_requests, (btstack_linked_item_t*) callback_registration); 1107bb38f057SMatthias Ringwald att_dispatch_server_request_can_send_now_event(con_handle); 1108969a5bbaSMatthias Ringwald return ERROR_CODE_SUCCESS; 1109bb38f057SMatthias Ringwald } 1110bb38f057SMatthias Ringwald 11113bb3035fSMilanka Ringwald int att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){ 111218707219SMatthias Ringwald att_server_t * att_server = att_server_for_handle(con_handle); 111318707219SMatthias Ringwald if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 111418707219SMatthias Ringwald 111518707219SMatthias Ringwald if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL; 11163deb3ec6SMatthias Ringwald 11173deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 11183deb3ec6SMatthias Ringwald uint8_t * packet_buffer = l2cap_get_outgoing_buffer(); 111918707219SMatthias Ringwald uint16_t size = att_prepare_handle_value_notification(&att_server->connection, attribute_handle, value, value_len, packet_buffer); 112018707219SMatthias Ringwald return l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); 11213deb3ec6SMatthias Ringwald } 11223deb3ec6SMatthias Ringwald 11233bb3035fSMilanka Ringwald int att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){ 112418707219SMatthias Ringwald att_server_t * att_server = att_server_for_handle(con_handle); 112518707219SMatthias Ringwald if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 112618707219SMatthias Ringwald 1127eaac31e8SMatthias Ringwald if (att_server->value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS; 112818707219SMatthias Ringwald if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL; 11293deb3ec6SMatthias Ringwald 11303deb3ec6SMatthias Ringwald // track indication 113118707219SMatthias Ringwald att_server->value_indication_handle = attribute_handle; 113218707219SMatthias Ringwald btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout); 113318707219SMatthias Ringwald btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS); 113418707219SMatthias Ringwald btstack_run_loop_add_timer(&att_server->value_indication_timer); 11353deb3ec6SMatthias Ringwald 11363deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 11373deb3ec6SMatthias Ringwald uint8_t * packet_buffer = l2cap_get_outgoing_buffer(); 113818707219SMatthias Ringwald uint16_t size = att_prepare_handle_value_indication(&att_server->connection, attribute_handle, value, value_len, packet_buffer); 113918707219SMatthias Ringwald l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); 11403deb3ec6SMatthias Ringwald return 0; 11413deb3ec6SMatthias Ringwald } 11428f9132b5SMilanka Ringwald 11438f9132b5SMilanka Ringwald uint16_t att_server_get_mtu(hci_con_handle_t con_handle){ 11448f9132b5SMilanka Ringwald att_server_t * att_server = att_server_for_handle(con_handle); 11458f9132b5SMilanka Ringwald if (!att_server) return 0; 11468f9132b5SMilanka Ringwald return att_server->connection.mtu; 11478f9132b5SMilanka Ringwald } 1148