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; 1033deb3ec6SMatthias Ringwald static btstack_packet_handler_t att_client_packet_handler = NULL; 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 12668591e36SMatthias Ringwald static void att_server_request_can_send_now(hci_connection_t * hci_connection){ 127c8c6e9b4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 128b6df12b6SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 129c8c6e9b4SMatthias Ringwald if (att_server->l2cap_cid != 0){ 130c8c6e9b4SMatthias Ringwald l2cap_request_can_send_now_event(att_server->l2cap_cid); 131c8c6e9b4SMatthias Ringwald return; 132c8c6e9b4SMatthias Ringwald } 133c8c6e9b4SMatthias Ringwald #endif 134388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 135b6df12b6SMatthias Ringwald att_dispatch_server_request_can_send_now_event(att_connection->con_handle); 136c8c6e9b4SMatthias Ringwald } 137c8c6e9b4SMatthias Ringwald 1381979f09cSMatthias Ringwald static bool att_server_can_send_packet(hci_connection_t * hci_connection){ 139c0f26e1cSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 140b6df12b6SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 141c0f26e1cSMatthias Ringwald if (att_server->l2cap_cid != 0){ 1421979f09cSMatthias Ringwald return l2cap_can_send_packet_now(att_server->l2cap_cid) != 0; 143c0f26e1cSMatthias Ringwald } 144c0f26e1cSMatthias Ringwald #endif 145388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 1461979f09cSMatthias Ringwald return att_dispatch_server_can_send_now(att_connection->con_handle) != 0; 147c0f26e1cSMatthias Ringwald } 148c0f26e1cSMatthias Ringwald 1493deb3ec6SMatthias Ringwald static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){ 1505d07396bSMatthias Ringwald btstack_packet_handler_t packet_handler = att_server_packet_handler_for_handle(attribute_handle); 1515d07396bSMatthias Ringwald if (!packet_handler) return; 1523deb3ec6SMatthias Ringwald 1533deb3ec6SMatthias Ringwald uint8_t event[7]; 1543deb3ec6SMatthias Ringwald int pos = 0; 1555611a760SMatthias Ringwald event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE; 1564ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 1573deb3ec6SMatthias Ringwald event[pos++] = status; 158f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, client_handle); 1593deb3ec6SMatthias Ringwald pos += 2; 160f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, attribute_handle); 161a444112bSMatthias Ringwald (*packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 1623deb3ec6SMatthias Ringwald } 1633deb3ec6SMatthias Ringwald 1645d07396bSMatthias Ringwald static void att_emit_event_to_all(const uint8_t * event, uint16_t size){ 1655d07396bSMatthias Ringwald // dispatch to app level handler 1669305033eSMatthias Ringwald if (att_client_packet_handler != NULL){ 1675d07396bSMatthias Ringwald (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size); 1685d07396bSMatthias Ringwald } 1693deb3ec6SMatthias Ringwald 1705d07396bSMatthias Ringwald // dispatch to service handlers 1715d07396bSMatthias Ringwald btstack_linked_list_iterator_t it; 1725d07396bSMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 1735d07396bSMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 1745d07396bSMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 1755d07396bSMatthias Ringwald if (!handler->packet_handler) continue; 1765d07396bSMatthias Ringwald (*handler->packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size); 1775d07396bSMatthias Ringwald } 1785d07396bSMatthias Ringwald } 1795d07396bSMatthias Ringwald 1805d07396bSMatthias Ringwald static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){ 1813deb3ec6SMatthias Ringwald uint8_t event[6]; 1823deb3ec6SMatthias Ringwald int pos = 0; 1835611a760SMatthias Ringwald event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE; 1844ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 185fc64f94aSMatthias Ringwald little_endian_store_16(event, pos, con_handle); 1863deb3ec6SMatthias Ringwald pos += 2; 187f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, mtu); 1885d07396bSMatthias Ringwald 1895d07396bSMatthias Ringwald // also dispatch to GATT Clients 190b12646c5SJakob Krantz att_dispatch_server_mtu_exchanged(con_handle, mtu); 1915d07396bSMatthias Ringwald 1925d07396bSMatthias Ringwald // dispatch to app level handler and service handlers 1935d07396bSMatthias Ringwald att_emit_event_to_all(&event[0], sizeof(event)); 1943deb3ec6SMatthias Ringwald } 1953deb3ec6SMatthias Ringwald 1965f141511SMatthias Ringwald static void att_emit_can_send_now_event(void * context){ 1975f141511SMatthias Ringwald UNUSED(context); 1983c97b242SMatthias Ringwald if (!att_client_packet_handler) return; 1993c97b242SMatthias Ringwald 2001b96b007SMatthias Ringwald uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0}; 2011b96b007SMatthias Ringwald (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 2021b96b007SMatthias Ringwald } 2031b96b007SMatthias Ringwald 20468591e36SMatthias Ringwald static void att_emit_connected_event(hci_connection_t * hci_connection){ 20568591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 206388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 207ff1bec95SMatthias Ringwald uint8_t event[11]; 208ff1bec95SMatthias Ringwald int pos = 0; 209ff1bec95SMatthias Ringwald event[pos++] = ATT_EVENT_CONNECTED; 2104ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 211ff1bec95SMatthias Ringwald event[pos++] = att_server->peer_addr_type; 212ff1bec95SMatthias Ringwald reverse_bd_addr(att_server->peer_address, &event[pos]); 213ff1bec95SMatthias Ringwald pos += 6; 214b6df12b6SMatthias Ringwald little_endian_store_16(event, pos, att_connection->con_handle); 215ff1bec95SMatthias Ringwald pos += 2; 216ff1bec95SMatthias Ringwald 217ff1bec95SMatthias Ringwald // dispatch to app level handler and service handlers 218ff1bec95SMatthias Ringwald att_emit_event_to_all(&event[0], sizeof(event)); 219ff1bec95SMatthias Ringwald } 220ff1bec95SMatthias Ringwald 221ff1bec95SMatthias Ringwald 2226187ad4cSMatthias Ringwald static void att_emit_disconnected_event(uint16_t con_handle){ 223ff1bec95SMatthias Ringwald uint8_t event[4]; 224ff1bec95SMatthias Ringwald int pos = 0; 225ff1bec95SMatthias Ringwald event[pos++] = ATT_EVENT_DISCONNECTED; 2264ea43905SMatthias Ringwald event[pos++] = sizeof(event) - 2u; 2276187ad4cSMatthias Ringwald little_endian_store_16(event, pos, con_handle); 228ff1bec95SMatthias Ringwald pos += 2; 229ff1bec95SMatthias Ringwald 230ff1bec95SMatthias Ringwald // dispatch to app level handler and service handlers 231ff1bec95SMatthias Ringwald att_emit_event_to_all(&event[0], sizeof(event)); 232ff1bec95SMatthias Ringwald } 233ff1bec95SMatthias Ringwald 234ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){ 23554178543SMatthias Ringwald void * context = btstack_run_loop_get_timer_context(ts); 23654178543SMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context; 23768591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 23868591e36SMatthias Ringwald if (!hci_connection) return; 23968591e36SMatthias Ringwald // @note: after a transaction timeout, no more requests shall be sent over this ATT Bearer 240bce48a26SMatthias Ringwald // (that's why we don't reset the value_indication_handle) 241b6df12b6SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 24218707219SMatthias Ringwald uint16_t att_handle = att_server->value_indication_handle; 243388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 24438b893aaSMilanka Ringwald att_handle_value_indication_notify_client((uint8_t)ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_connection->con_handle, att_handle); 2453deb3ec6SMatthias Ringwald } 2463deb3ec6SMatthias Ringwald 2473deb3ec6SMatthias Ringwald static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 2489ec2630cSMatthias Ringwald 2497dad9ff8SMatthias Ringwald UNUSED(channel); // ok: there is no channel 2507dad9ff8SMatthias Ringwald UNUSED(size); // ok: handling own l2cap events 2513deb3ec6SMatthias Ringwald 25218707219SMatthias Ringwald att_server_t * att_server; 253b6df12b6SMatthias Ringwald att_connection_t * att_connection; 25418707219SMatthias Ringwald hci_con_handle_t con_handle; 25568591e36SMatthias Ringwald hci_connection_t * hci_connection; 25670dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 25770dca4d4SMatthias Ringwald bd_addr_t address; 25868591e36SMatthias Ringwald btstack_linked_list_iterator_t it; 25970dca4d4SMatthias Ringwald #endif 26018707219SMatthias Ringwald 2613deb3ec6SMatthias Ringwald switch (packet_type) { 2623deb3ec6SMatthias Ringwald 2633deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 2640e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 2653deb3ec6SMatthias Ringwald 26670dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 26770dca4d4SMatthias Ringwald case L2CAP_EVENT_INCOMING_CONNECTION: 26870dca4d4SMatthias Ringwald l2cap_event_incoming_connection_get_address(packet, address); 26970dca4d4SMatthias Ringwald l2cap_accept_connection(channel); 27036de8547SMatthias Ringwald log_info("Accept incoming connection from %s", bd_addr_to_str(address)); 27170dca4d4SMatthias Ringwald break; 27270dca4d4SMatthias Ringwald case L2CAP_EVENT_CHANNEL_OPENED: 273122b723eSMatthias Ringwald con_handle = l2cap_event_channel_opened_get_handle(packet); 27468591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 27568591e36SMatthias Ringwald if (!hci_connection) break; 27668591e36SMatthias Ringwald att_server = &hci_connection->att_server; 277122b723eSMatthias Ringwald // store connection info 278f16129ceSMatthias Ringwald att_server->peer_addr_type = BD_ADDR_TYPE_ACL; 279122b723eSMatthias Ringwald l2cap_event_channel_opened_get_address(packet, att_server->peer_address); 280388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 281b6df12b6SMatthias Ringwald att_connection->con_handle = con_handle; 28270dca4d4SMatthias Ringwald att_server->l2cap_cid = l2cap_event_channel_opened_get_local_cid(packet); 283122b723eSMatthias Ringwald // reset connection properties 284122b723eSMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 285b6df12b6SMatthias Ringwald att_connection->mtu = l2cap_event_channel_opened_get_remote_mtu(packet); 286b6df12b6SMatthias Ringwald att_connection->max_mtu = l2cap_max_mtu(); 287b6df12b6SMatthias Ringwald if (att_connection->max_mtu > ATT_REQUEST_BUFFER_SIZE){ 288b6df12b6SMatthias Ringwald att_connection->max_mtu = ATT_REQUEST_BUFFER_SIZE; 289122b723eSMatthias Ringwald } 290df33ee14SMatthias Ringwald 291b6df12b6SMatthias Ringwald log_info("Connection opened %s, l2cap cid %04x, mtu %u", bd_addr_to_str(address), att_server->l2cap_cid, att_connection->mtu); 292df33ee14SMatthias Ringwald 293914988a1SMatthias Ringwald // update security params 294b6df12b6SMatthias Ringwald att_connection->encryption_key_size = gap_encryption_key_size(con_handle); 295b6df12b6SMatthias Ringwald att_connection->authenticated = gap_authenticated(con_handle); 296b6df12b6SMatthias Ringwald att_connection->secure_connection = gap_secure_connection(con_handle); 297914988a1SMatthias Ringwald log_info("encrypted key size %u, authenticated %u, secure connection %u", 298b6df12b6SMatthias Ringwald att_connection->encryption_key_size, att_connection->authenticated, att_connection->secure_connection); 299df33ee14SMatthias Ringwald 300df33ee14SMatthias Ringwald // notify connection opened 30168591e36SMatthias Ringwald att_emit_connected_event(hci_connection); 302df33ee14SMatthias Ringwald 303914988a1SMatthias Ringwald // restore persisten ccc if encrypted 304914988a1SMatthias Ringwald if ( gap_security_level(con_handle) >= LEVEL_2){ 30568591e36SMatthias Ringwald att_server_persistent_ccc_restore(hci_connection); 306914988a1SMatthias Ringwald } 307122b723eSMatthias Ringwald // TODO: what to do about le device db? 308122b723eSMatthias Ringwald att_server->pairing_active = 0; 30970dca4d4SMatthias Ringwald break; 31048364364SMatthias Ringwald case L2CAP_EVENT_CAN_SEND_NOW: 31148364364SMatthias Ringwald att_server_handle_can_send_now(); 31248364364SMatthias Ringwald break; 31348364364SMatthias Ringwald 31470dca4d4SMatthias Ringwald #endif 3153deb3ec6SMatthias Ringwald case HCI_EVENT_LE_META: 3163deb3ec6SMatthias Ringwald switch (packet[2]) { 3173deb3ec6SMatthias Ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 31818707219SMatthias Ringwald con_handle = little_endian_read_16(packet, 4); 31968591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 32068591e36SMatthias Ringwald if (!hci_connection) break; 32168591e36SMatthias Ringwald att_server = &hci_connection->att_server; 3223deb3ec6SMatthias Ringwald // store connection info 32318707219SMatthias Ringwald att_server->peer_addr_type = packet[7]; 32418707219SMatthias Ringwald reverse_bd_addr(&packet[8], att_server->peer_address); 325388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 326b6df12b6SMatthias Ringwald att_connection->con_handle = con_handle; 3273deb3ec6SMatthias Ringwald // reset connection properties 32818707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 329b6df12b6SMatthias Ringwald att_connection->mtu = ATT_DEFAULT_MTU; 330b6df12b6SMatthias Ringwald att_connection->max_mtu = l2cap_max_le_mtu(); 331b6df12b6SMatthias Ringwald if (att_connection->max_mtu > ATT_REQUEST_BUFFER_SIZE){ 332b6df12b6SMatthias Ringwald att_connection->max_mtu = ATT_REQUEST_BUFFER_SIZE; 33399c58ad0SMatthias Ringwald } 33438b893aaSMilanka Ringwald att_connection->encryption_key_size = 0u; 33538b893aaSMilanka Ringwald att_connection->authenticated = 0u; 33638b893aaSMilanka Ringwald att_connection->authorized = 0u; 337b2c1e52cSMatthias Ringwald // workaround: identity resolving can already be complete, at least store result 338b2c1e52cSMatthias Ringwald att_server->ir_le_device_db_index = sm_le_device_index(con_handle); 33938b893aaSMilanka Ringwald att_server->ir_lookup_active = 0u; 34038b893aaSMilanka Ringwald att_server->pairing_active = 0u; 341ff1bec95SMatthias Ringwald // notify all - old 3425d07396bSMatthias Ringwald att_emit_event_to_all(packet, size); 343ff1bec95SMatthias Ringwald // notify all - new 34468591e36SMatthias Ringwald att_emit_connected_event(hci_connection); 3453deb3ec6SMatthias Ringwald break; 3463deb3ec6SMatthias Ringwald 3473deb3ec6SMatthias Ringwald default: 3483deb3ec6SMatthias Ringwald break; 3493deb3ec6SMatthias Ringwald } 3503deb3ec6SMatthias Ringwald break; 3513deb3ec6SMatthias Ringwald 3523deb3ec6SMatthias Ringwald case HCI_EVENT_ENCRYPTION_CHANGE: 3533deb3ec6SMatthias Ringwald case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE: 3543deb3ec6SMatthias Ringwald // check handle 35518707219SMatthias Ringwald con_handle = little_endian_read_16(packet, 3); 35668591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 35768591e36SMatthias Ringwald if (!hci_connection) break; 358914988a1SMatthias Ringwald if (gap_get_connection_type(con_handle) != GAP_CONNECTION_LE) break; 359914988a1SMatthias Ringwald // update security params 36068591e36SMatthias Ringwald att_server = &hci_connection->att_server; 361388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 362b6df12b6SMatthias Ringwald att_connection->encryption_key_size = gap_encryption_key_size(con_handle); 363*ff3f1026SMatthias Ringwald att_connection->authenticated = gap_authenticated(con_handle) ? 1 : 0; 364b6df12b6SMatthias Ringwald att_connection->secure_connection = gap_secure_connection(con_handle); 365914988a1SMatthias Ringwald log_info("encrypted key size %u, authenticated %u, secure connection %u", 366b6df12b6SMatthias Ringwald att_connection->encryption_key_size, att_connection->authenticated, att_connection->secure_connection); 367914988a1SMatthias Ringwald if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){ 3684097998aSMatthias Ringwald // restore CCC values when encrypted for LE Connections 36901ac2008SMatthias Ringwald if (hci_event_encryption_change_get_encryption_enabled(packet)){ 37068591e36SMatthias Ringwald att_server_persistent_ccc_restore(hci_connection); 37101ac2008SMatthias Ringwald } 37201ac2008SMatthias Ringwald } 37368591e36SMatthias Ringwald att_run_for_context(hci_connection); 3743deb3ec6SMatthias Ringwald break; 3753deb3ec6SMatthias Ringwald 3763deb3ec6SMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 37770a390c7SMatthias Ringwald // check handle 37818707219SMatthias Ringwald con_handle = hci_event_disconnection_complete_get_connection_handle(packet); 37968591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 38068591e36SMatthias Ringwald if (!hci_connection) break; 38168591e36SMatthias Ringwald att_server = &hci_connection->att_server; 382388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 383b6df12b6SMatthias Ringwald att_clear_transaction_queue(att_connection); 384b6df12b6SMatthias Ringwald att_connection->con_handle = 0; 385760ad805SMatthias Ringwald att_server->pairing_active = 0; 38618707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 38738b893aaSMilanka Ringwald if (att_server->value_indication_handle != 0u){ 38827328ecbSMatthias Ringwald btstack_run_loop_remove_timer(&att_server->value_indication_timer); 389bce48a26SMatthias Ringwald uint16_t att_handle = att_server->value_indication_handle; 39038b893aaSMilanka Ringwald att_server->value_indication_handle = 0u; // reset error state 39138b893aaSMilanka Ringwald att_handle_value_indication_notify_client((uint8_t)ATT_HANDLE_VALUE_INDICATION_DISCONNECT, att_connection->con_handle, att_handle); 392bce48a26SMatthias Ringwald } 393ff1bec95SMatthias Ringwald // notify all - new 3946187ad4cSMatthias Ringwald att_emit_disconnected_event(con_handle); 395ff1bec95SMatthias Ringwald // notify all - old 3965d07396bSMatthias Ringwald att_emit_event_to_all(packet, size); 3973deb3ec6SMatthias Ringwald break; 3983deb3ec6SMatthias Ringwald 399760ad805SMatthias Ringwald // Identity Resolving 4005611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_STARTED: 40118707219SMatthias Ringwald con_handle = sm_event_identity_resolving_started_get_handle(packet); 40268591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 40368591e36SMatthias Ringwald if (!hci_connection) break; 40468591e36SMatthias Ringwald att_server = &hci_connection->att_server; 4055611a760SMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED"); 40630c8e114SMatthias Ringwald att_server->ir_lookup_active = 1; 4073deb3ec6SMatthias Ringwald break; 4085611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED: 409760ad805SMatthias Ringwald con_handle = sm_event_identity_created_get_handle(packet); 41068591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 41168591e36SMatthias Ringwald if (!hci_connection) return; 41268591e36SMatthias Ringwald att_server = &hci_connection->att_server; 41330c8e114SMatthias Ringwald att_server->ir_lookup_active = 0; 4141b7acd0dSMatthias Ringwald att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index(packet); 4151b7acd0dSMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED"); 41668591e36SMatthias Ringwald att_run_for_context(hci_connection); 4173deb3ec6SMatthias Ringwald break; 4185611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_FAILED: 41918707219SMatthias Ringwald con_handle = sm_event_identity_resolving_failed_get_handle(packet); 42068591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 42168591e36SMatthias Ringwald if (!hci_connection) break; 42268591e36SMatthias Ringwald att_server = &hci_connection->att_server; 4235611a760SMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED"); 42430c8e114SMatthias Ringwald att_server->ir_lookup_active = 0; 42518707219SMatthias Ringwald att_server->ir_le_device_db_index = -1; 42668591e36SMatthias Ringwald att_run_for_context(hci_connection); 4273deb3ec6SMatthias Ringwald break; 428760ad805SMatthias Ringwald 429760ad805SMatthias Ringwald // Pairing started - delete stored CCC values 430760ad805SMatthias Ringwald // - assumes pairing indicates either new device or re-pairing, in both cases there should be no stored CCC values 431760ad805SMatthias Ringwald // - assumes that all events have the con handle as the first field 432760ad805SMatthias Ringwald case SM_EVENT_JUST_WORKS_REQUEST: 433760ad805SMatthias Ringwald case SM_EVENT_PASSKEY_DISPLAY_NUMBER: 434760ad805SMatthias Ringwald case SM_EVENT_PASSKEY_INPUT_NUMBER: 435760ad805SMatthias Ringwald case SM_EVENT_NUMERIC_COMPARISON_REQUEST: 436760ad805SMatthias Ringwald con_handle = sm_event_just_works_request_get_handle(packet); 43768591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 43868591e36SMatthias Ringwald if (!hci_connection) break; 43968591e36SMatthias Ringwald att_server = &hci_connection->att_server; 440760ad805SMatthias Ringwald att_server->pairing_active = 1; 441760ad805SMatthias Ringwald log_info("SM Pairing started"); 442760ad805SMatthias Ringwald if (att_server->ir_le_device_db_index < 0) break; 44368591e36SMatthias Ringwald att_server_persistent_ccc_clear(hci_connection); 444760ad805SMatthias Ringwald // index not valid anymore 445760ad805SMatthias Ringwald att_server->ir_le_device_db_index = -1; 446760ad805SMatthias Ringwald break; 447760ad805SMatthias Ringwald 4481b7acd0dSMatthias Ringwald // Bonding completed 449760ad805SMatthias Ringwald case SM_EVENT_IDENTITY_CREATED: 450760ad805SMatthias Ringwald con_handle = sm_event_identity_created_get_handle(packet); 45168591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 45268591e36SMatthias Ringwald if (!hci_connection) return; 45368591e36SMatthias Ringwald att_server = &hci_connection->att_server; 454760ad805SMatthias Ringwald att_server->pairing_active = 0; 4551b7acd0dSMatthias Ringwald att_server->ir_le_device_db_index = sm_event_identity_created_get_index(packet); 45668591e36SMatthias Ringwald att_run_for_context(hci_connection); 4571b7acd0dSMatthias Ringwald break; 4581b7acd0dSMatthias Ringwald 4591b7acd0dSMatthias Ringwald // Pairing complete (with/without bonding=storing of pairing information) 4601b7acd0dSMatthias Ringwald case SM_EVENT_PAIRING_COMPLETE: 4611b7acd0dSMatthias Ringwald con_handle = sm_event_pairing_complete_get_handle(packet); 46268591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 46368591e36SMatthias Ringwald if (!hci_connection) return; 46468591e36SMatthias Ringwald att_server = &hci_connection->att_server; 4651b7acd0dSMatthias Ringwald att_server->pairing_active = 0; 46668591e36SMatthias Ringwald att_run_for_context(hci_connection); 467760ad805SMatthias Ringwald break; 468760ad805SMatthias Ringwald 469760ad805SMatthias Ringwald // Authorization 4705611a760SMatthias Ringwald case SM_EVENT_AUTHORIZATION_RESULT: { 47118707219SMatthias Ringwald con_handle = sm_event_authorization_result_get_handle(packet); 47268591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(con_handle); 47368591e36SMatthias Ringwald if (!hci_connection) break; 47468591e36SMatthias Ringwald att_server = &hci_connection->att_server; 475388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 476b6df12b6SMatthias Ringwald att_connection->authorized = sm_event_authorization_result_get_authorization_result(packet); 47768591e36SMatthias Ringwald att_server_request_can_send_now(hci_connection); 4783deb3ec6SMatthias Ringwald break; 4793deb3ec6SMatthias Ringwald } 4803deb3ec6SMatthias Ringwald default: 4813deb3ec6SMatthias Ringwald break; 4823deb3ec6SMatthias Ringwald } 48365b44ffdSMatthias Ringwald break; 48470dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 48570dca4d4SMatthias Ringwald case L2CAP_DATA_PACKET: 48668591e36SMatthias Ringwald hci_connections_get_iterator(&it); 48768591e36SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 488388fa7c4SMatthias Ringwald hci_connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 489388fa7c4SMatthias Ringwald att_server = &hci_connection->att_server; 49068591e36SMatthias Ringwald if (att_server->l2cap_cid == channel) { 491388fa7c4SMatthias Ringwald att_server_handle_att_pdu(hci_connection, packet, size); 49268591e36SMatthias Ringwald break; 49368591e36SMatthias Ringwald } 49468591e36SMatthias Ringwald } 49570dca4d4SMatthias Ringwald break; 49670dca4d4SMatthias Ringwald #endif 49741772f37SMatthias Ringwald 49865b44ffdSMatthias Ringwald default: 49965b44ffdSMatthias Ringwald break; 5003deb3ec6SMatthias Ringwald } 5013deb3ec6SMatthias Ringwald } 5023deb3ec6SMatthias Ringwald 5037a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 5043deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){ 5053deb3ec6SMatthias Ringwald 50668591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION); 50768591e36SMatthias Ringwald if (!hci_connection) return; 50868591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 5093deb3ec6SMatthias Ringwald 5103deb3ec6SMatthias Ringwald uint8_t hash_flipped[8]; 5119c80e4ccSMatthias Ringwald reverse_64(hash, hash_flipped); 51218707219SMatthias Ringwald if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8)){ 5133deb3ec6SMatthias Ringwald log_info("ATT Signed Write, invalid signature"); 51403f53d81SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT 51503f53d81SMatthias Ringwald printf("ATT Signed Write, invalid signature\n"); 51603f53d81SMatthias Ringwald #endif 51718707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 5183deb3ec6SMatthias Ringwald return; 5193deb3ec6SMatthias Ringwald } 5203deb3ec6SMatthias Ringwald log_info("ATT Signed Write, valid signature"); 52103f53d81SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT 52203f53d81SMatthias Ringwald printf("ATT Signed Write, valid signature\n"); 52303f53d81SMatthias Ringwald #endif 5243deb3ec6SMatthias Ringwald 5253deb3ec6SMatthias Ringwald // update sequence number 52618707219SMatthias Ringwald uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12); 52718707219SMatthias Ringwald le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1); 52818707219SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 52968591e36SMatthias Ringwald att_server_request_can_send_now(hci_connection); 5303deb3ec6SMatthias Ringwald } 5317a766ebfSMatthias Ringwald #endif 53288a48dd8SMatthias Ringwald 53318707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED 534b06f2579SMatthias Ringwald // pre: can send now 535b06f2579SMatthias Ringwald // returns: 1 if packet was sent 53668591e36SMatthias Ringwald static int att_server_process_validated_request(hci_connection_t * hci_connection){ 53768591e36SMatthias Ringwald att_server_t * att_server = & hci_connection->att_server; 538388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 539b06f2579SMatthias Ringwald 540b06f2579SMatthias Ringwald l2cap_reserve_packet_buffer(); 541b06f2579SMatthias Ringwald uint8_t * att_response_buffer = l2cap_get_outgoing_buffer(); 542b6df12b6SMatthias Ringwald uint16_t att_response_size = att_handle_request(att_connection, att_server->request_buffer, att_server->request_size, att_response_buffer); 543b06f2579SMatthias Ringwald 544beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE 545c1ab6cc1SMatthias Ringwald if ((att_response_size == ATT_READ_RESPONSE_PENDING) || (att_response_size == ATT_INTERNAL_WRITE_RESPONSE_PENDING)){ 546e404a688SMatthias Ringwald // update state 547beb20288SMatthias Ringwald att_server->state = ATT_SERVER_RESPONSE_PENDING; 548e404a688SMatthias Ringwald 54956c3a347SMatthias Ringwald // callback with handle ATT_READ_RESPONSE_PENDING for reads 55056c3a347SMatthias Ringwald if (att_response_size == ATT_READ_RESPONSE_PENDING){ 551b6df12b6SMatthias Ringwald att_server_client_read_callback(att_connection->con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0); 55256c3a347SMatthias Ringwald } 553e8e7403eSMatthias Ringwald 554e8e7403eSMatthias Ringwald // free reserved buffer 555e8e7403eSMatthias Ringwald l2cap_release_packet_buffer(); 556e8e7403eSMatthias Ringwald return 0; 557e404a688SMatthias Ringwald } 558e404a688SMatthias Ringwald #endif 559e404a688SMatthias Ringwald 560b06f2579SMatthias Ringwald // intercept "insufficient authorization" for authenticated connections to allow for user authorization 5614ea43905SMatthias Ringwald if ((att_response_size >= 4u) 562b06f2579SMatthias Ringwald && (att_response_buffer[0] == ATT_ERROR_RESPONSE) 563b06f2579SMatthias Ringwald && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION) 56438b893aaSMilanka Ringwald && (att_connection->authenticated != 0u)){ 565b06f2579SMatthias Ringwald 566b6df12b6SMatthias Ringwald switch (gap_authorization_state(att_connection->con_handle)){ 567b06f2579SMatthias Ringwald case AUTHORIZATION_UNKNOWN: 568b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 569b6df12b6SMatthias Ringwald sm_request_pairing(att_connection->con_handle); 570b06f2579SMatthias Ringwald return 0; 571b06f2579SMatthias Ringwald case AUTHORIZATION_PENDING: 572b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 573b06f2579SMatthias Ringwald return 0; 574b06f2579SMatthias Ringwald default: 575b06f2579SMatthias Ringwald break; 576b06f2579SMatthias Ringwald } 577b06f2579SMatthias Ringwald } 578b06f2579SMatthias Ringwald 57918707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 5804ea43905SMatthias Ringwald if (att_response_size == 0u) { 581b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 582b06f2579SMatthias Ringwald return 0; 583b06f2579SMatthias Ringwald } 584b06f2579SMatthias Ringwald 58548364364SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 58638b893aaSMilanka Ringwald if (att_server->l2cap_cid != 0u){ 58748364364SMatthias Ringwald l2cap_send_prepared(att_server->l2cap_cid, att_response_size); 58848364364SMatthias Ringwald } else 58948364364SMatthias Ringwald #endif 59048364364SMatthias Ringwald { 591b6df12b6SMatthias Ringwald l2cap_send_prepared_connectionless(att_connection->con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size); 59248364364SMatthias Ringwald } 593b06f2579SMatthias Ringwald 594b06f2579SMatthias Ringwald // notify client about MTU exchange result 595b06f2579SMatthias Ringwald if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){ 596b6df12b6SMatthias Ringwald att_emit_mtu_event(att_connection->con_handle, att_connection->mtu); 597b06f2579SMatthias Ringwald } 598b06f2579SMatthias Ringwald return 1; 599b06f2579SMatthias Ringwald } 600b06f2579SMatthias Ringwald 601beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE 602beb20288SMatthias Ringwald int att_server_response_ready(hci_con_handle_t con_handle){ 60368591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 60468591e36SMatthias Ringwald if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 60568591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 60668591e36SMatthias Ringwald 607beb20288SMatthias Ringwald if (att_server->state != ATT_SERVER_RESPONSE_PENDING) return ERROR_CODE_COMMAND_DISALLOWED; 608e404a688SMatthias Ringwald 609e404a688SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 61068591e36SMatthias Ringwald att_server_request_can_send_now(hci_connection); 611e404a688SMatthias Ringwald return ERROR_CODE_SUCCESS; 612e404a688SMatthias Ringwald } 613e404a688SMatthias Ringwald #endif 614e404a688SMatthias Ringwald 61568591e36SMatthias Ringwald static void att_run_for_context(hci_connection_t * hci_connection){ 61668591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 617388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 61818707219SMatthias Ringwald switch (att_server->state){ 6193deb3ec6SMatthias Ringwald case ATT_SERVER_REQUEST_RECEIVED: 620760ad805SMatthias Ringwald 62148364364SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 62248364364SMatthias Ringwald if (att_server->l2cap_cid != 0){ 62348364364SMatthias Ringwald // ok 62448364364SMatthias Ringwald } else 62548364364SMatthias Ringwald #endif 62648364364SMatthias Ringwald { 6270234fbbcSMatthias Ringwald // wait until re-encryption as central is complete 628b6df12b6SMatthias Ringwald if (gap_reconnect_security_setup_active(att_connection->con_handle)) break; 62948364364SMatthias Ringwald } 6300234fbbcSMatthias Ringwald 631760ad805SMatthias Ringwald // wait until pairing is complete 632760ad805SMatthias Ringwald if (att_server->pairing_active) break; 633760ad805SMatthias Ringwald 6347a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 63518707219SMatthias Ringwald if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){ 6363deb3ec6SMatthias Ringwald log_info("ATT Signed Write!"); 6373deb3ec6SMatthias Ringwald if (!sm_cmac_ready()) { 6383deb3ec6SMatthias Ringwald log_info("ATT Signed Write, sm_cmac engine not ready. Abort"); 63918707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 6403deb3ec6SMatthias Ringwald return; 6413deb3ec6SMatthias Ringwald } 64218707219SMatthias Ringwald if (att_server->request_size < (3 + 12)) { 6433deb3ec6SMatthias Ringwald log_info("ATT Signed Write, request to short. Abort."); 64418707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 6453deb3ec6SMatthias Ringwald return; 6463deb3ec6SMatthias Ringwald } 64718707219SMatthias Ringwald if (att_server->ir_lookup_active){ 6483deb3ec6SMatthias Ringwald return; 6493deb3ec6SMatthias Ringwald } 65018707219SMatthias Ringwald if (att_server->ir_le_device_db_index < 0){ 6513deb3ec6SMatthias Ringwald log_info("ATT Signed Write, CSRK not available"); 65218707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 6533deb3ec6SMatthias Ringwald return; 6543deb3ec6SMatthias Ringwald } 6553deb3ec6SMatthias Ringwald 6563deb3ec6SMatthias Ringwald // check counter 65718707219SMatthias Ringwald uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12); 65818707219SMatthias Ringwald uint32_t counter_db = le_device_db_remote_counter_get(att_server->ir_le_device_db_index); 6593deb3ec6SMatthias Ringwald log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet); 6603deb3ec6SMatthias Ringwald if (counter_packet < counter_db){ 6613deb3ec6SMatthias Ringwald log_info("ATT Signed Write, db reports higher counter, abort"); 66218707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 6633deb3ec6SMatthias Ringwald return; 6643deb3ec6SMatthias Ringwald } 6653deb3ec6SMatthias Ringwald 6663deb3ec6SMatthias Ringwald // signature is { sequence counter, secure hash } 6673deb3ec6SMatthias Ringwald sm_key_t csrk; 66818707219SMatthias Ringwald le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk); 66918707219SMatthias Ringwald att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION; 6703deb3ec6SMatthias Ringwald log_info("Orig Signature: "); 67118707219SMatthias Ringwald log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8); 67218707219SMatthias Ringwald uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1); 67318707219SMatthias 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); 6743deb3ec6SMatthias Ringwald return; 6753deb3ec6SMatthias Ringwald } 6767a766ebfSMatthias Ringwald #endif 677b06f2579SMatthias Ringwald // move on 67818707219SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 67968591e36SMatthias Ringwald att_server_request_can_send_now(hci_connection); 680b06f2579SMatthias Ringwald break; 68188a48dd8SMatthias Ringwald 6823deb3ec6SMatthias Ringwald default: 6833deb3ec6SMatthias Ringwald break; 6843deb3ec6SMatthias Ringwald } 6853deb3ec6SMatthias Ringwald } 6863deb3ec6SMatthias Ringwald 6871979f09cSMatthias Ringwald static bool att_server_data_ready_for_phase(att_server_t * att_server, att_server_run_phase_t phase){ 68890f03c80SMatthias Ringwald switch (phase){ 68990f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_1_REQUESTS: 69090f03c80SMatthias Ringwald return att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 69190f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_2_INDICATIONS: 69238b893aaSMilanka Ringwald return (!btstack_linked_list_empty(&att_server->indication_requests) && (att_server->value_indication_handle == 0u)); 69390f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS: 69490f03c80SMatthias Ringwald return (!btstack_linked_list_empty(&att_server->notification_requests)); 6957bbeb3adSMilanka Ringwald default: 6967bbeb3adSMilanka Ringwald btstack_assert(false); 6971979f09cSMatthias Ringwald return false; 69890f03c80SMatthias Ringwald } 6997bbeb3adSMilanka Ringwald } 70090f03c80SMatthias Ringwald 70168591e36SMatthias Ringwald static void att_server_trigger_send_for_phase(hci_connection_t * hci_connection, att_server_run_phase_t phase){ 70290f03c80SMatthias Ringwald btstack_context_callback_registration_t * client; 70368591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 70490f03c80SMatthias Ringwald switch (phase){ 70590f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_1_REQUESTS: 70668591e36SMatthias Ringwald att_server_process_validated_request(hci_connection); 70790f03c80SMatthias Ringwald break; 70890f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_2_INDICATIONS: 70990f03c80SMatthias Ringwald client = (btstack_context_callback_registration_t*) att_server->indication_requests; 71090f03c80SMatthias Ringwald btstack_linked_list_remove(&att_server->indication_requests, (btstack_linked_item_t *) client); 71190f03c80SMatthias Ringwald client->callback(client->context); 71290f03c80SMatthias Ringwald break; 71390f03c80SMatthias Ringwald case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS: 71490f03c80SMatthias Ringwald client = (btstack_context_callback_registration_t*) att_server->notification_requests; 71590f03c80SMatthias Ringwald btstack_linked_list_remove(&att_server->notification_requests, (btstack_linked_item_t *) client); 71690f03c80SMatthias Ringwald client->callback(client->context); 71790f03c80SMatthias Ringwald break; 7187bbeb3adSMilanka Ringwald default: 7197bbeb3adSMilanka Ringwald btstack_assert(false); 7207bbeb3adSMilanka Ringwald break; 72190f03c80SMatthias Ringwald } 72290f03c80SMatthias Ringwald } 72390f03c80SMatthias Ringwald 7247eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){ 725b06f2579SMatthias Ringwald 7266438547aSMatthias Ringwald hci_con_handle_t last_send_con_handle = HCI_CON_HANDLE_INVALID; 72768591e36SMatthias Ringwald hci_connection_t * request_hci_connection = NULL; 7281979f09cSMatthias Ringwald bool can_send_now = true; 72938b893aaSMilanka Ringwald uint8_t phase_index; 7306438547aSMatthias Ringwald 7314395a000SMatthias Ringwald for (phase_index = ATT_SERVER_RUN_PHASE_1_REQUESTS; phase_index <= ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS; phase_index++){ 7324395a000SMatthias Ringwald att_server_run_phase_t phase = (att_server_run_phase_t) phase_index; 7336438547aSMatthias Ringwald hci_con_handle_t skip_connections_until = att_server_last_can_send_now; 734ff3cc4a5SMatthias Ringwald while (true){ 7356438547aSMatthias Ringwald btstack_linked_list_iterator_t it; 73618707219SMatthias Ringwald hci_connections_get_iterator(&it); 73718707219SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 73818707219SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 73918707219SMatthias Ringwald att_server_t * att_server = &connection->att_server; 740388fa7c4SMatthias Ringwald att_connection_t * att_connection = &connection->att_connection; 7416438547aSMatthias Ringwald 7421979f09cSMatthias Ringwald bool data_ready = att_server_data_ready_for_phase(att_server, phase); 7436438547aSMatthias Ringwald 744b6df12b6SMatthias 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); 7456438547aSMatthias Ringwald 7466438547aSMatthias Ringwald // skip until last sender found (which is also skipped) 7476438547aSMatthias Ringwald if (skip_connections_until != HCI_CON_HANDLE_INVALID){ 74868591e36SMatthias Ringwald if (data_ready && (request_hci_connection == NULL)){ 74968591e36SMatthias Ringwald request_hci_connection = connection; 7506438547aSMatthias Ringwald } 751b6df12b6SMatthias Ringwald if (skip_connections_until == att_connection->con_handle){ 7526438547aSMatthias Ringwald skip_connections_until = HCI_CON_HANDLE_INVALID; 7536438547aSMatthias Ringwald } 7546438547aSMatthias Ringwald continue; 7556438547aSMatthias Ringwald }; 7566438547aSMatthias Ringwald 75790f03c80SMatthias Ringwald if (data_ready){ 758969a5bbaSMatthias Ringwald if (can_send_now){ 75968591e36SMatthias Ringwald att_server_trigger_send_for_phase(connection, phase); 760b6df12b6SMatthias Ringwald last_send_con_handle = att_connection->con_handle; 76168591e36SMatthias Ringwald can_send_now = att_server_can_send_packet(connection); 76290f03c80SMatthias Ringwald data_ready = att_server_data_ready_for_phase(att_server, phase); 76368591e36SMatthias Ringwald if (data_ready && (request_hci_connection == NULL)){ 76468591e36SMatthias Ringwald request_hci_connection = connection; 765cbbb12d9SMatthias Ringwald } 766cbbb12d9SMatthias Ringwald } else { 76768591e36SMatthias Ringwald request_hci_connection = connection; 768f29a88d8SMatthias Ringwald break; 769cbbb12d9SMatthias Ringwald } 770cbbb12d9SMatthias Ringwald } 7713deb3ec6SMatthias Ringwald } 7723deb3ec6SMatthias Ringwald 7736438547aSMatthias Ringwald // stop skipping (handles disconnect by last send connection) 7746438547aSMatthias Ringwald skip_connections_until = HCI_CON_HANDLE_INVALID; 7756438547aSMatthias Ringwald 7763551936eSMatthias Ringwald // Exit loop, if we cannot send 777969a5bbaSMatthias Ringwald if (!can_send_now) break; 778969a5bbaSMatthias Ringwald 779969a5bbaSMatthias Ringwald // Exit loop, if we can send but there are also no further request 78068591e36SMatthias Ringwald if (request_hci_connection == NULL) break; 781969a5bbaSMatthias Ringwald 782969a5bbaSMatthias Ringwald // Finally, if we still can send and there are requests, just try again 78368591e36SMatthias Ringwald request_hci_connection = NULL; 784969a5bbaSMatthias Ringwald } 7856438547aSMatthias Ringwald // update last send con handle for round robin 7866438547aSMatthias Ringwald if (last_send_con_handle != HCI_CON_HANDLE_INVALID){ 7876438547aSMatthias Ringwald att_server_last_can_send_now = last_send_con_handle; 7886438547aSMatthias Ringwald } 7893551936eSMatthias Ringwald } 790969a5bbaSMatthias Ringwald 79168591e36SMatthias Ringwald if (request_hci_connection == NULL) return; 79268591e36SMatthias Ringwald att_server_request_can_send_now(request_hci_connection); 793969a5bbaSMatthias Ringwald } 794969a5bbaSMatthias Ringwald 79568591e36SMatthias Ringwald static void att_server_handle_att_pdu(hci_connection_t * hci_connection, uint8_t * packet, uint16_t size){ 79668591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 797388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 79818707219SMatthias Ringwald 799838bd521SMatthias Ringwald uint8_t opcode = packet[0u]; 80038b893aaSMilanka Ringwald uint8_t method = opcode & 0x03fu; 801ed68ae9dSMilanka Ringwald bool invalid = method > ATT_MULTIPLE_HANDLE_VALUE_NTF; 80238b893aaSMilanka Ringwald bool command = (opcode & 0x40u) != 0u; 803838bd521SMatthias Ringwald 804838bd521SMatthias Ringwald // ignore invalid commands 805838bd521SMatthias Ringwald if (invalid && command){ 806838bd521SMatthias Ringwald return; 807838bd521SMatthias Ringwald } 808838bd521SMatthias Ringwald 8093deb3ec6SMatthias Ringwald // handle value indication confirms 81038b893aaSMilanka Ringwald if ((opcode == ATT_HANDLE_VALUE_CONFIRMATION) && (att_server->value_indication_handle != 0u)){ 81118707219SMatthias Ringwald btstack_run_loop_remove_timer(&att_server->value_indication_timer); 81218707219SMatthias Ringwald uint16_t att_handle = att_server->value_indication_handle; 81338b893aaSMilanka Ringwald att_server->value_indication_handle = 0u; 81438b893aaSMilanka Ringwald att_handle_value_indication_notify_client(0u, att_connection->con_handle, att_handle); 81568591e36SMatthias Ringwald att_server_request_can_send_now(hci_connection); 8163deb3ec6SMatthias Ringwald return; 8173deb3ec6SMatthias Ringwald } 8183deb3ec6SMatthias Ringwald 8196428eb5aSMatthias Ringwald // directly process command 8206428eb5aSMatthias Ringwald // note: signed write cannot be handled directly as authentication needs to be verified 821838bd521SMatthias Ringwald if (opcode == ATT_WRITE_COMMAND){ 822b6df12b6SMatthias Ringwald att_handle_request(att_connection, packet, size, NULL); 8236428eb5aSMatthias Ringwald return; 8246428eb5aSMatthias Ringwald } 8256428eb5aSMatthias Ringwald 8263deb3ec6SMatthias Ringwald // check size 82718707219SMatthias Ringwald if (size > sizeof(att_server->request_buffer)) { 828e0463bdcSMatthias 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)); 8296428eb5aSMatthias Ringwald return; 8306428eb5aSMatthias Ringwald } 8313deb3ec6SMatthias Ringwald 832e0463bdcSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 833e0463bdcSMatthias Ringwald // abort signed write validation if a new request comes in (but finish previous signed write if possible) 834e0463bdcSMatthias Ringwald if (att_server->state == ATT_SERVER_W4_SIGNED_WRITE_VALIDATION){ 835e0463bdcSMatthias Ringwald if (packet[0] == ATT_SIGNED_WRITE_COMMAND){ 836e0463bdcSMatthias Ringwald log_info("skip new signed write request as previous is in validation"); 837e0463bdcSMatthias Ringwald return; 838e0463bdcSMatthias Ringwald } else { 839e0463bdcSMatthias Ringwald log_info("abort signed write validation to process new request"); 840e0463bdcSMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 841e0463bdcSMatthias Ringwald } 842e0463bdcSMatthias Ringwald } 843e0463bdcSMatthias Ringwald #endif 8443deb3ec6SMatthias Ringwald // last request still in processing? 84518707219SMatthias Ringwald if (att_server->state != ATT_SERVER_IDLE){ 846e0463bdcSMatthias Ringwald log_info("skip att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state); 8476428eb5aSMatthias Ringwald return; 8486428eb5aSMatthias Ringwald } 8493deb3ec6SMatthias Ringwald 8503deb3ec6SMatthias Ringwald // store request 85118707219SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED; 85218707219SMatthias Ringwald att_server->request_size = size; 8536535961aSMatthias Ringwald (void)memcpy(att_server->request_buffer, packet, size); 8543deb3ec6SMatthias Ringwald 85568591e36SMatthias Ringwald att_run_for_context(hci_connection); 85641772f37SMatthias Ringwald } 85741772f37SMatthias Ringwald 85841772f37SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){ 85968591e36SMatthias Ringwald hci_connection_t * hci_connection; 860b6df12b6SMatthias Ringwald att_connection_t * att_connection; 86141772f37SMatthias Ringwald 86241772f37SMatthias Ringwald switch (packet_type){ 86341772f37SMatthias Ringwald case HCI_EVENT_PACKET: 86441772f37SMatthias Ringwald switch (packet[0]){ 86541772f37SMatthias Ringwald case L2CAP_EVENT_CAN_SEND_NOW: 86641772f37SMatthias Ringwald att_server_handle_can_send_now(); 86741772f37SMatthias Ringwald break; 86841772f37SMatthias Ringwald case ATT_EVENT_MTU_EXCHANGE_COMPLETE: 86941772f37SMatthias Ringwald // GATT client has negotiated the mtu for this connection 87068591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(handle); 87168591e36SMatthias Ringwald if (!hci_connection) break; 872388fa7c4SMatthias Ringwald att_connection = &hci_connection->att_connection; 873b6df12b6SMatthias Ringwald att_connection->mtu = little_endian_read_16(packet, 4); 87441772f37SMatthias Ringwald break; 87541772f37SMatthias Ringwald default: 87641772f37SMatthias Ringwald break; 87741772f37SMatthias Ringwald } 87841772f37SMatthias Ringwald break; 87941772f37SMatthias Ringwald 88041772f37SMatthias Ringwald case ATT_DATA_PACKET: 88141772f37SMatthias Ringwald log_debug("ATT Packet, handle 0x%04x", handle); 88268591e36SMatthias Ringwald hci_connection = hci_connection_for_handle(handle); 88368591e36SMatthias Ringwald if (!hci_connection) break; 88441772f37SMatthias Ringwald 88568591e36SMatthias Ringwald att_server_handle_att_pdu(hci_connection, packet, size); 886372d62c4SMatthias Ringwald break; 8877bbeb3adSMilanka Ringwald 8887bbeb3adSMilanka Ringwald default: 8897bbeb3adSMilanka Ringwald break; 890372d62c4SMatthias Ringwald } 8913deb3ec6SMatthias Ringwald } 8923deb3ec6SMatthias Ringwald 893bf78aac6SMatthias Ringwald // --------------------- 894bf78aac6SMatthias Ringwald // persistent CCC writes 895bf78aac6SMatthias Ringwald static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){ 8964ea43905SMatthias Ringwald return ('B' << 24u) | ('T' << 16u) | ('C' << 8u) | index; 897bf78aac6SMatthias Ringwald } 898bf78aac6SMatthias Ringwald 899bf78aac6SMatthias Ringwald static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){ 900bf78aac6SMatthias Ringwald // lookup att_server instance 90168591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 90268591e36SMatthias Ringwald if (!hci_connection) return; 90368591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 904bf78aac6SMatthias Ringwald int le_device_index = att_server->ir_le_device_db_index; 905bf78aac6SMatthias 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); 9066a540790SMatthias Ringwald 907bf78aac6SMatthias Ringwald // check if bonded 908bf78aac6SMatthias Ringwald if (le_device_index < 0) return; 9096a540790SMatthias Ringwald 910bf78aac6SMatthias Ringwald // get btstack_tlv 911bf78aac6SMatthias Ringwald const btstack_tlv_t * tlv_impl = NULL; 912bf78aac6SMatthias Ringwald void * tlv_context; 913bf78aac6SMatthias Ringwald btstack_tlv_get_instance(&tlv_impl, &tlv_context); 914bf78aac6SMatthias Ringwald if (!tlv_impl) return; 9156a540790SMatthias Ringwald 916bf78aac6SMatthias Ringwald // update ccc tag 917bf78aac6SMatthias Ringwald int index; 9186a540790SMatthias Ringwald uint32_t highest_seq_nr = 0; 9196a540790SMatthias Ringwald uint32_t lowest_seq_nr = 0; 9206a540790SMatthias Ringwald uint32_t tag_for_lowest_seq_nr = 0; 9216a540790SMatthias Ringwald uint32_t tag_for_empty = 0; 9226a540790SMatthias Ringwald persistent_ccc_entry_t entry; 9236afb9acaSMatthias Ringwald for (index=0; index<NVN_NUM_GATT_SERVER_CCC; index++){ 924bf78aac6SMatthias Ringwald uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 9256a540790SMatthias Ringwald int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 9266a540790SMatthias Ringwald 9276a540790SMatthias Ringwald // empty/invalid tag 9286a540790SMatthias Ringwald if (len != sizeof(persistent_ccc_entry_t)){ 9296a540790SMatthias Ringwald tag_for_empty = tag; 930bf78aac6SMatthias Ringwald continue; 931bf78aac6SMatthias Ringwald } 9326a540790SMatthias Ringwald // update highest seq nr 9336a540790SMatthias Ringwald if (entry.seq_nr > highest_seq_nr){ 9346a540790SMatthias Ringwald highest_seq_nr = entry.seq_nr; 9356a540790SMatthias Ringwald } 9366a540790SMatthias Ringwald // find entry with lowest seq nr 9374ea43905SMatthias Ringwald if ((tag_for_lowest_seq_nr == 0u) || (entry.seq_nr < lowest_seq_nr)){ 9386a540790SMatthias Ringwald tag_for_lowest_seq_nr = tag; 9396a540790SMatthias Ringwald lowest_seq_nr = entry.seq_nr; 9406a540790SMatthias Ringwald } 9416a540790SMatthias Ringwald 9426a540790SMatthias Ringwald if (entry.device_index != le_device_index) continue; 9436a540790SMatthias Ringwald if (entry.att_handle != att_handle) continue; 9446a540790SMatthias Ringwald 9456a540790SMatthias Ringwald // found matching entry 9469305033eSMatthias Ringwald if (value != 0){ 947bf78aac6SMatthias Ringwald // update 9486a540790SMatthias Ringwald if (entry.value == value) { 949760ad805SMatthias Ringwald log_info("CCC Index %u: Up-to-date", index); 950760ad805SMatthias Ringwald return; 951760ad805SMatthias Ringwald } 9526a540790SMatthias Ringwald entry.value = value; 9534ea43905SMatthias Ringwald entry.seq_nr = highest_seq_nr + 1u; 954760ad805SMatthias Ringwald log_info("CCC Index %u: Store", index); 9550fe46bc1SMatthias Ringwald int result = tlv_impl->store_tag(tlv_context, tag, (const uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 9560fe46bc1SMatthias Ringwald if (result != 0){ 9570fe46bc1SMatthias Ringwald log_error("Store tag index %u failed", index); 9580fe46bc1SMatthias Ringwald } 95901ac2008SMatthias Ringwald } else { 96001ac2008SMatthias Ringwald // delete 961760ad805SMatthias Ringwald log_info("CCC Index %u: Delete", index); 96201ac2008SMatthias Ringwald tlv_impl->delete_tag(tlv_context, tag); 96301ac2008SMatthias Ringwald } 964bf78aac6SMatthias Ringwald return; 965bf78aac6SMatthias Ringwald } 9666a540790SMatthias Ringwald 9670fe46bc1SMatthias Ringwald log_info("tag_for_empty %"PRIx32", tag_for_lowest_seq_nr %"PRIx32, tag_for_empty, tag_for_lowest_seq_nr); 9686a540790SMatthias Ringwald 9694ea43905SMatthias Ringwald if (value == 0u){ 9706a540790SMatthias Ringwald // done 9716a540790SMatthias Ringwald return; 9726a540790SMatthias Ringwald } 9736a540790SMatthias Ringwald 97438b893aaSMilanka Ringwald uint32_t tag_to_use = 0u; 97538b893aaSMilanka Ringwald if (tag_for_empty != 0u){ 9766a540790SMatthias Ringwald tag_to_use = tag_for_empty; 9776a540790SMatthias Ringwald } else if (tag_for_lowest_seq_nr){ 9786a540790SMatthias Ringwald tag_to_use = tag_for_lowest_seq_nr; 9796a540790SMatthias Ringwald } else { 9806a540790SMatthias Ringwald // should not happen 9816a540790SMatthias Ringwald return; 9826a540790SMatthias Ringwald } 983bf78aac6SMatthias Ringwald // store ccc tag 9844ea43905SMatthias Ringwald entry.seq_nr = highest_seq_nr + 1u; 9856a540790SMatthias Ringwald entry.device_index = le_device_index; 9866a540790SMatthias Ringwald entry.att_handle = att_handle; 9876a540790SMatthias Ringwald entry.value = value; 9880fe46bc1SMatthias Ringwald int result = tlv_impl->store_tag(tlv_context, tag_to_use, (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 } 992bf78aac6SMatthias Ringwald } 993bf78aac6SMatthias Ringwald 99468591e36SMatthias Ringwald static void att_server_persistent_ccc_clear(hci_connection_t * hci_connection){ 99568591e36SMatthias Ringwald if (!hci_connection) return; 99668591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 99768591e36SMatthias Ringwald 998760ad805SMatthias Ringwald int le_device_index = att_server->ir_le_device_db_index; 999760ad805SMatthias Ringwald log_info("Clear CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index); 1000760ad805SMatthias Ringwald // check if bonded 1001760ad805SMatthias Ringwald if (le_device_index < 0) return; 1002760ad805SMatthias Ringwald // get btstack_tlv 1003760ad805SMatthias Ringwald const btstack_tlv_t * tlv_impl = NULL; 1004760ad805SMatthias Ringwald void * tlv_context; 1005760ad805SMatthias Ringwald btstack_tlv_get_instance(&tlv_impl, &tlv_context); 1006760ad805SMatthias Ringwald if (!tlv_impl) return; 1007760ad805SMatthias Ringwald // get all ccc tag 1008760ad805SMatthias Ringwald int index; 10096a540790SMatthias Ringwald persistent_ccc_entry_t entry; 10106afb9acaSMatthias Ringwald for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){ 1011760ad805SMatthias Ringwald uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 10126a540790SMatthias Ringwald int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 10136a540790SMatthias Ringwald if (len != sizeof(persistent_ccc_entry_t)) continue; 10146a540790SMatthias Ringwald if (entry.device_index != le_device_index) continue; 1015760ad805SMatthias Ringwald // delete entry 1016760ad805SMatthias Ringwald log_info("CCC Index %u: Delete", index); 1017760ad805SMatthias Ringwald tlv_impl->delete_tag(tlv_context, tag); 1018760ad805SMatthias Ringwald } 1019760ad805SMatthias Ringwald } 1020760ad805SMatthias Ringwald 102168591e36SMatthias Ringwald static void att_server_persistent_ccc_restore(hci_connection_t * hci_connection){ 102268591e36SMatthias Ringwald if (!hci_connection) return; 102368591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 1024388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 102568591e36SMatthias Ringwald 102601ac2008SMatthias Ringwald int le_device_index = att_server->ir_le_device_db_index; 102701ac2008SMatthias Ringwald log_info("Restore CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index); 10281b7acd0dSMatthias Ringwald // check if bonded 10291b7acd0dSMatthias Ringwald if (le_device_index < 0) return; 103001ac2008SMatthias Ringwald // get btstack_tlv 103101ac2008SMatthias Ringwald const btstack_tlv_t * tlv_impl = NULL; 103201ac2008SMatthias Ringwald void * tlv_context; 103301ac2008SMatthias Ringwald btstack_tlv_get_instance(&tlv_impl, &tlv_context); 103401ac2008SMatthias Ringwald if (!tlv_impl) return; 103501ac2008SMatthias Ringwald // get all ccc tag 103601ac2008SMatthias Ringwald int index; 10376a540790SMatthias Ringwald persistent_ccc_entry_t entry; 10386afb9acaSMatthias Ringwald for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){ 103901ac2008SMatthias Ringwald uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 10406a540790SMatthias Ringwald int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t)); 10416a540790SMatthias Ringwald if (len != sizeof(persistent_ccc_entry_t)) continue; 10426a540790SMatthias Ringwald if (entry.device_index != le_device_index) continue; 104301ac2008SMatthias Ringwald // simulate write callback 10446a540790SMatthias Ringwald uint16_t attribute_handle = entry.att_handle; 104501ac2008SMatthias Ringwald uint8_t value[2]; 10466a540790SMatthias Ringwald little_endian_store_16(value, 0, entry.value); 104701ac2008SMatthias Ringwald att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle); 104801ac2008SMatthias Ringwald if (!callback) continue; 10496a540790SMatthias Ringwald log_info("CCC Index %u: Set Attribute handle 0x%04x to value 0x%04x", index, attribute_handle, entry.value ); 1050b6df12b6SMatthias Ringwald (*callback)(att_connection->con_handle, attribute_handle, ATT_TRANSACTION_MODE_NONE, 0, value, sizeof(value)); 105101ac2008SMatthias Ringwald } 105201ac2008SMatthias Ringwald } 105301ac2008SMatthias Ringwald 1054bf78aac6SMatthias Ringwald // persistent CCC writes 1055bf78aac6SMatthias Ringwald // --------------------- 10563d71c7a4SMatthias Ringwald 10573d71c7a4SMatthias Ringwald // gatt service management 10583d71c7a4SMatthias Ringwald static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){ 10593d71c7a4SMatthias Ringwald btstack_linked_list_iterator_t it; 10603d71c7a4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 10613d71c7a4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 10623d71c7a4SMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 10633d71c7a4SMatthias Ringwald if (handler->start_handle > handle) continue; 10643d71c7a4SMatthias Ringwald if (handler->end_handle < handle) continue; 10653d71c7a4SMatthias Ringwald return handler; 10663d71c7a4SMatthias Ringwald } 10673d71c7a4SMatthias Ringwald return NULL; 10683d71c7a4SMatthias Ringwald } 10693d71c7a4SMatthias Ringwald static att_read_callback_t att_server_read_callback_for_handle(uint16_t handle){ 10703d71c7a4SMatthias Ringwald att_service_handler_t * handler = att_service_handler_for_handle(handle); 10719305033eSMatthias Ringwald if (handler != NULL) return handler->read_callback; 10723d71c7a4SMatthias Ringwald return att_server_client_read_callback; 10733d71c7a4SMatthias Ringwald } 10743d71c7a4SMatthias Ringwald 10753d71c7a4SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){ 10763d71c7a4SMatthias Ringwald att_service_handler_t * handler = att_service_handler_for_handle(handle); 10779305033eSMatthias Ringwald if (handler != NULL) return handler->write_callback; 10783d71c7a4SMatthias Ringwald return att_server_client_write_callback; 10793d71c7a4SMatthias Ringwald } 10803d71c7a4SMatthias Ringwald 10815d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle){ 10825d07396bSMatthias Ringwald att_service_handler_t * handler = att_service_handler_for_handle(handle); 10839305033eSMatthias Ringwald if (handler != NULL) return handler->packet_handler; 10845d07396bSMatthias Ringwald return att_client_packet_handler; 10855d07396bSMatthias Ringwald } 10865d07396bSMatthias Ringwald 10873d71c7a4SMatthias Ringwald static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){ 10883d71c7a4SMatthias Ringwald // notify all callbacks 10893d71c7a4SMatthias Ringwald btstack_linked_list_iterator_t it; 10903d71c7a4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 10913d71c7a4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 10923d71c7a4SMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 10933d71c7a4SMatthias Ringwald if (!handler->write_callback) continue; 10943d71c7a4SMatthias Ringwald (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0); 10953d71c7a4SMatthias Ringwald } 10963d71c7a4SMatthias Ringwald if (!att_server_client_write_callback) return; 10973d71c7a4SMatthias Ringwald (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0); 10983d71c7a4SMatthias Ringwald } 10993d71c7a4SMatthias Ringwald 11003d71c7a4SMatthias Ringwald // returns first reported error or 0 11013d71c7a4SMatthias Ringwald static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){ 11023d71c7a4SMatthias Ringwald btstack_linked_list_iterator_t it; 11033d71c7a4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 11043d71c7a4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 11053d71c7a4SMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 11063d71c7a4SMatthias Ringwald if (!handler->write_callback) continue; 11073d71c7a4SMatthias Ringwald uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0); 110838b893aaSMilanka Ringwald if (error_code != 0u) return error_code; 11093d71c7a4SMatthias Ringwald } 11103d71c7a4SMatthias Ringwald if (!att_server_client_write_callback) return 0; 11113d71c7a4SMatthias Ringwald return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0); 11123d71c7a4SMatthias Ringwald } 11133d71c7a4SMatthias Ringwald 11143d71c7a4SMatthias 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){ 11153d71c7a4SMatthias Ringwald att_read_callback_t callback = att_server_read_callback_for_handle(attribute_handle); 111601ac2008SMatthias Ringwald if (!callback) return 0; 11173d71c7a4SMatthias Ringwald return (*callback)(con_handle, attribute_handle, offset, buffer, buffer_size); 11183d71c7a4SMatthias Ringwald } 11193d71c7a4SMatthias Ringwald 11203d71c7a4SMatthias 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){ 11213d71c7a4SMatthias Ringwald switch (transaction_mode){ 11223d71c7a4SMatthias Ringwald case ATT_TRANSACTION_MODE_VALIDATE: 11233d71c7a4SMatthias Ringwald return att_validate_prepared_write(con_handle); 11243d71c7a4SMatthias Ringwald case ATT_TRANSACTION_MODE_EXECUTE: 11253d71c7a4SMatthias Ringwald case ATT_TRANSACTION_MODE_CANCEL: 11263d71c7a4SMatthias Ringwald att_notify_write_callbacks(con_handle, transaction_mode); 11273d71c7a4SMatthias Ringwald return 0; 11283d71c7a4SMatthias Ringwald default: 11293d71c7a4SMatthias Ringwald break; 11303d71c7a4SMatthias Ringwald } 11313d71c7a4SMatthias Ringwald 1132bf78aac6SMatthias Ringwald // track CCC writes 11334ea43905SMatthias Ringwald if (att_is_persistent_ccc(attribute_handle) && (offset == 0u) && (buffer_size == 2u)){ 1134bf78aac6SMatthias Ringwald att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0)); 1135bf78aac6SMatthias Ringwald } 1136bf78aac6SMatthias Ringwald 113701ac2008SMatthias Ringwald att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle); 113801ac2008SMatthias Ringwald if (!callback) return 0; 11393d71c7a4SMatthias Ringwald return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size); 11403d71c7a4SMatthias Ringwald } 11413d71c7a4SMatthias Ringwald 11423d71c7a4SMatthias Ringwald /** 11433d71c7a4SMatthias Ringwald * @brief register read/write callbacks for specific handle range 11443d71c7a4SMatthias Ringwald * @param att_service_handler_t 11453d71c7a4SMatthias Ringwald */ 11463d71c7a4SMatthias Ringwald void att_server_register_service_handler(att_service_handler_t * handler){ 11473d71c7a4SMatthias Ringwald if (att_service_handler_for_handle(handler->start_handle) || 11483d71c7a4SMatthias Ringwald att_service_handler_for_handle(handler->end_handle)){ 11493d71c7a4SMatthias Ringwald log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle); 11503d71c7a4SMatthias Ringwald return; 11513d71c7a4SMatthias Ringwald } 11523d71c7a4SMatthias Ringwald btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler); 11533d71c7a4SMatthias Ringwald } 11543d71c7a4SMatthias Ringwald 11553deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){ 11563deb3ec6SMatthias Ringwald 11573d71c7a4SMatthias Ringwald // store callbacks 11583d71c7a4SMatthias Ringwald att_server_client_read_callback = read_callback; 11593d71c7a4SMatthias Ringwald att_server_client_write_callback = write_callback; 11603d71c7a4SMatthias Ringwald 11611a389cdcSMatthias Ringwald // register for HCI Events 1162d9a7306aSMatthias Ringwald hci_event_callback_registration.callback = &att_event_packet_handler; 11631a389cdcSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 11641a389cdcSMatthias Ringwald 1165406722e4SMatthias Ringwald // register for SM events 1166d9a7306aSMatthias Ringwald sm_event_callback_registration.callback = &att_event_packet_handler; 1167406722e4SMatthias Ringwald sm_add_event_handler(&sm_event_callback_registration); 11683deb3ec6SMatthias Ringwald 11691a389cdcSMatthias Ringwald // and L2CAP ATT Server PDUs 11703deb3ec6SMatthias Ringwald att_dispatch_register_server(att_packet_handler); 11713deb3ec6SMatthias Ringwald 117270dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 117370dca4d4SMatthias Ringwald // setup l2cap service 1174f2372b44SMatthias Ringwald l2cap_register_service(&att_event_packet_handler, PSM_ATT, 0xffff, gap_get_security_level()); 117570dca4d4SMatthias Ringwald #endif 117670dca4d4SMatthias Ringwald 11773deb3ec6SMatthias Ringwald att_set_db(db); 11783d71c7a4SMatthias Ringwald att_set_read_callback(att_server_read_callback); 11793d71c7a4SMatthias Ringwald att_set_write_callback(att_server_write_callback); 11803deb3ec6SMatthias Ringwald } 11813deb3ec6SMatthias Ringwald 11823deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){ 11833deb3ec6SMatthias Ringwald att_client_packet_handler = handler; 11843deb3ec6SMatthias Ringwald } 11853deb3ec6SMatthias Ringwald 1186cbbb12d9SMatthias Ringwald 1187cbbb12d9SMatthias Ringwald // to be deprecated 1188c141988cSMatthias Ringwald int att_server_can_send_packet_now(hci_con_handle_t con_handle){ 118968591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 119068591e36SMatthias Ringwald if (!hci_connection) return 0; 119168591e36SMatthias Ringwald return att_server_can_send_packet(hci_connection); 11921b96b007SMatthias Ringwald } 11931b96b007SMatthias Ringwald 1194cbbb12d9SMatthias Ringwald int att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 1195cbbb12d9SMatthias Ringwald return att_server_request_to_send_notification(callback_registration, con_handle); 11963deb3ec6SMatthias Ringwald } 11973deb3ec6SMatthias Ringwald 1198cbbb12d9SMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){ 1199cbbb12d9SMatthias Ringwald att_client_waiting_for_can_send_registration.callback = &att_emit_can_send_now_event; 1200cbbb12d9SMatthias Ringwald att_server_request_to_send_notification(&att_client_waiting_for_can_send_registration, con_handle); 1201cbbb12d9SMatthias Ringwald } 1202cbbb12d9SMatthias Ringwald // end of deprecated 1203cbbb12d9SMatthias Ringwald 1204cbbb12d9SMatthias Ringwald int att_server_request_to_send_notification(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 120568591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 120668591e36SMatthias Ringwald if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 120768591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 1208c37df6f6SMatthias Ringwald bool added = btstack_linked_list_add_tail(&att_server->notification_requests, (btstack_linked_item_t*) callback_registration); 120968591e36SMatthias Ringwald att_server_request_can_send_now(hci_connection); 1210c37df6f6SMatthias Ringwald if (added){ 1211cbbb12d9SMatthias Ringwald return ERROR_CODE_SUCCESS; 1212c37df6f6SMatthias Ringwald } else { 1213c37df6f6SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1214c37df6f6SMatthias Ringwald } 1215cbbb12d9SMatthias Ringwald } 1216cbbb12d9SMatthias Ringwald 1217cbbb12d9SMatthias Ringwald int att_server_request_to_send_indication(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 121868591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 121968591e36SMatthias Ringwald if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 122068591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 1221c37df6f6SMatthias Ringwald bool added = btstack_linked_list_add_tail(&att_server->indication_requests, (btstack_linked_item_t*) callback_registration); 122268591e36SMatthias Ringwald att_server_request_can_send_now(hci_connection); 1223c37df6f6SMatthias Ringwald if (added){ 1224969a5bbaSMatthias Ringwald return ERROR_CODE_SUCCESS; 1225c37df6f6SMatthias Ringwald } else { 1226c37df6f6SMatthias Ringwald return ERROR_CODE_COMMAND_DISALLOWED; 1227c37df6f6SMatthias Ringwald } 1228bb38f057SMatthias Ringwald } 1229bb38f057SMatthias Ringwald 12303bb3035fSMilanka Ringwald int att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){ 123168591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 123268591e36SMatthias Ringwald if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 1233388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 123468591e36SMatthias Ringwald 123568591e36SMatthias Ringwald if (!att_server_can_send_packet(hci_connection)) return BTSTACK_ACL_BUFFERS_FULL; 12363deb3ec6SMatthias Ringwald 12373deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 12383deb3ec6SMatthias Ringwald uint8_t * packet_buffer = l2cap_get_outgoing_buffer(); 1239b6df12b6SMatthias Ringwald uint16_t size = att_prepare_handle_value_notification(att_connection, attribute_handle, value, value_len, packet_buffer); 12401676a2b8SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 12411676a2b8SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 12421676a2b8SMatthias Ringwald if (att_server->l2cap_cid != 0){ 12431676a2b8SMatthias Ringwald return l2cap_send_prepared(att_server->l2cap_cid, size);; 12441676a2b8SMatthias Ringwald } 12451676a2b8SMatthias Ringwald #endif 1246b6df12b6SMatthias Ringwald return l2cap_send_prepared_connectionless(att_connection->con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); 12473deb3ec6SMatthias Ringwald } 12483deb3ec6SMatthias Ringwald 12493bb3035fSMilanka Ringwald int att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){ 125068591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 125168591e36SMatthias Ringwald if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 125268591e36SMatthias Ringwald att_server_t * att_server = &hci_connection->att_server; 1253388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 125418707219SMatthias Ringwald 125538b893aaSMilanka Ringwald if (att_server->value_indication_handle != 0u) return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS; 125668591e36SMatthias Ringwald if (!att_server_can_send_packet(hci_connection)) return BTSTACK_ACL_BUFFERS_FULL; 12573deb3ec6SMatthias Ringwald 12583deb3ec6SMatthias Ringwald // track indication 125918707219SMatthias Ringwald att_server->value_indication_handle = attribute_handle; 126018707219SMatthias Ringwald btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout); 126118707219SMatthias Ringwald btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS); 126218707219SMatthias Ringwald btstack_run_loop_add_timer(&att_server->value_indication_timer); 12633deb3ec6SMatthias Ringwald 12643deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 12653deb3ec6SMatthias Ringwald uint8_t * packet_buffer = l2cap_get_outgoing_buffer(); 1266b6df12b6SMatthias Ringwald uint16_t size = att_prepare_handle_value_indication(att_connection, attribute_handle, value, value_len, packet_buffer); 12671676a2b8SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC 12681676a2b8SMatthias Ringwald if (att_server->l2cap_cid != 0){ 12691676a2b8SMatthias Ringwald return l2cap_send_prepared(att_server->l2cap_cid, size);; 12701676a2b8SMatthias Ringwald } 12711676a2b8SMatthias Ringwald #endif 1272b6df12b6SMatthias Ringwald l2cap_send_prepared_connectionless(att_connection->con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); 12733deb3ec6SMatthias Ringwald return 0; 12743deb3ec6SMatthias Ringwald } 12758f9132b5SMilanka Ringwald 12768f9132b5SMilanka Ringwald uint16_t att_server_get_mtu(hci_con_handle_t con_handle){ 127768591e36SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 127868591e36SMatthias Ringwald if (!hci_connection) return 0; 1279388fa7c4SMatthias Ringwald att_connection_t * att_connection = &hci_connection->att_connection; 1280b6df12b6SMatthias Ringwald return att_connection->mtu; 12818f9132b5SMilanka Ringwald } 1282