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 38ab2c6ae4SMatthias 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 <stdio.h> 473deb3ec6SMatthias Ringwald #include <stdlib.h> 483deb3ec6SMatthias Ringwald #include <string.h> 493deb3ec6SMatthias Ringwald #include <inttypes.h> 503deb3ec6SMatthias Ringwald 517907f069SMatthias Ringwald #include "btstack_config.h" 523deb3ec6SMatthias Ringwald 5359c6af15SMatthias Ringwald #include "att_dispatch.h" 5459c6af15SMatthias Ringwald #include "ble/att_db.h" 5559c6af15SMatthias Ringwald #include "ble/att_server.h" 5659c6af15SMatthias Ringwald #include "ble/core.h" 5759c6af15SMatthias Ringwald #include "ble/le_device_db.h" 5859c6af15SMatthias Ringwald #include "ble/sm.h" 5916ece135SMatthias Ringwald #include "btstack_debug.h" 600e2df43fSMatthias Ringwald #include "btstack_event.h" 613deb3ec6SMatthias Ringwald #include "btstack_memory.h" 620e2df43fSMatthias Ringwald #include "btstack_run_loop.h" 6359c6af15SMatthias Ringwald #include "gap.h" 643deb3ec6SMatthias Ringwald #include "hci.h" 653deb3ec6SMatthias Ringwald #include "hci_dump.h" 663deb3ec6SMatthias Ringwald #include "l2cap.h" 673deb3ec6SMatthias Ringwald 6818707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server); 693deb3ec6SMatthias Ringwald 7018707219SMatthias Ringwald // global 711a389cdcSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 72406722e4SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration; 733deb3ec6SMatthias Ringwald static btstack_packet_handler_t att_client_packet_handler = NULL; 74bb38f057SMatthias Ringwald static btstack_linked_list_t can_send_now_clients; 75*3d71c7a4SMatthias Ringwald static btstack_linked_list_t service_handlers; 7618707219SMatthias Ringwald static uint8_t att_client_waiting_for_can_send; 7718707219SMatthias Ringwald 78*3d71c7a4SMatthias Ringwald static att_read_callback_t att_server_client_read_callback; 79*3d71c7a4SMatthias Ringwald static att_write_callback_t att_server_client_write_callback; 80*3d71c7a4SMatthias Ringwald 8118707219SMatthias Ringwald static att_server_t * att_server_for_handle(hci_con_handle_t con_handle){ 8218707219SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 8318707219SMatthias Ringwald if (!hci_connection) return NULL; 8418707219SMatthias Ringwald return &hci_connection->att_server; 8518707219SMatthias Ringwald } 8618707219SMatthias Ringwald 8702b78fc8SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 8818707219SMatthias Ringwald static att_server_t * att_server_for_state(att_server_state_t state){ 8918707219SMatthias Ringwald btstack_linked_list_iterator_t it; 9018707219SMatthias Ringwald hci_connections_get_iterator(&it); 9118707219SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 9218707219SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 9318707219SMatthias Ringwald att_server_t * att_server = &connection->att_server; 9418707219SMatthias Ringwald if (att_server->state == state) return att_server; 9518707219SMatthias Ringwald } 9618707219SMatthias Ringwald return NULL; 9718707219SMatthias Ringwald } 9802b78fc8SMatthias Ringwald #endif 99bb38f057SMatthias Ringwald 1003deb3ec6SMatthias Ringwald static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){ 1013deb3ec6SMatthias Ringwald if (!att_client_packet_handler) return; 1023deb3ec6SMatthias Ringwald 1033deb3ec6SMatthias Ringwald uint8_t event[7]; 1043deb3ec6SMatthias Ringwald int pos = 0; 1055611a760SMatthias Ringwald event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE; 1063deb3ec6SMatthias Ringwald event[pos++] = sizeof(event) - 2; 1073deb3ec6SMatthias Ringwald event[pos++] = status; 108f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, client_handle); 1093deb3ec6SMatthias Ringwald pos += 2; 110f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, attribute_handle); 1113deb3ec6SMatthias Ringwald (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 1123deb3ec6SMatthias Ringwald } 1133deb3ec6SMatthias Ringwald 114fc64f94aSMatthias Ringwald static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){ 1153deb3ec6SMatthias Ringwald if (!att_client_packet_handler) return; 1163deb3ec6SMatthias Ringwald 1173deb3ec6SMatthias Ringwald uint8_t event[6]; 1183deb3ec6SMatthias Ringwald int pos = 0; 1195611a760SMatthias Ringwald event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE; 1203deb3ec6SMatthias Ringwald event[pos++] = sizeof(event) - 2; 121fc64f94aSMatthias Ringwald little_endian_store_16(event, pos, con_handle); 1223deb3ec6SMatthias Ringwald pos += 2; 123f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, mtu); 1243deb3ec6SMatthias Ringwald (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 1253deb3ec6SMatthias Ringwald } 1263deb3ec6SMatthias Ringwald 1271b96b007SMatthias Ringwald static void att_emit_can_send_now_event(void){ 1283c97b242SMatthias Ringwald if (!att_client_packet_handler) return; 1293c97b242SMatthias Ringwald 1301b96b007SMatthias Ringwald uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0}; 1311b96b007SMatthias Ringwald (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 1321b96b007SMatthias Ringwald } 1331b96b007SMatthias Ringwald 134ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){ 13554178543SMatthias Ringwald void * context = btstack_run_loop_get_timer_context(ts); 13654178543SMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context; 13718707219SMatthias Ringwald att_server_t * att_server = att_server_for_handle(con_handle); 13818707219SMatthias Ringwald if (!att_server) return; 13918707219SMatthias Ringwald uint16_t att_handle = att_server->value_indication_handle; 14018707219SMatthias Ringwald att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_server->connection.con_handle, att_handle); 1413deb3ec6SMatthias Ringwald } 1423deb3ec6SMatthias Ringwald 1433deb3ec6SMatthias Ringwald static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1449ec2630cSMatthias Ringwald 1457dad9ff8SMatthias Ringwald UNUSED(channel); // ok: there is no channel 1467dad9ff8SMatthias Ringwald UNUSED(size); // ok: handling own l2cap events 1473deb3ec6SMatthias Ringwald 14818707219SMatthias Ringwald att_server_t * att_server; 14918707219SMatthias Ringwald hci_con_handle_t con_handle; 15018707219SMatthias Ringwald 1513deb3ec6SMatthias Ringwald switch (packet_type) { 1523deb3ec6SMatthias Ringwald 1533deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 1540e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 1553deb3ec6SMatthias Ringwald 1563deb3ec6SMatthias Ringwald case HCI_EVENT_LE_META: 1573deb3ec6SMatthias Ringwald switch (packet[2]) { 1583deb3ec6SMatthias Ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 15918707219SMatthias Ringwald con_handle = little_endian_read_16(packet, 4); 16018707219SMatthias Ringwald att_server = att_server_for_handle(con_handle); 16118707219SMatthias Ringwald if (!att_server) break; 1623deb3ec6SMatthias Ringwald // store connection info 16318707219SMatthias Ringwald att_server->peer_addr_type = packet[7]; 16418707219SMatthias Ringwald reverse_bd_addr(&packet[8], att_server->peer_address); 16518707219SMatthias Ringwald att_server->connection.con_handle = con_handle; 1663deb3ec6SMatthias Ringwald // reset connection properties 16718707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 16818707219SMatthias Ringwald att_server->connection.mtu = ATT_DEFAULT_MTU; 16918707219SMatthias Ringwald att_server->connection.max_mtu = l2cap_max_le_mtu(); 17018707219SMatthias Ringwald if (att_server->connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){ 17118707219SMatthias Ringwald att_server->connection.max_mtu = ATT_REQUEST_BUFFER_SIZE; 17299c58ad0SMatthias Ringwald } 17318707219SMatthias Ringwald att_server->connection.encryption_key_size = 0; 17418707219SMatthias Ringwald att_server->connection.authenticated = 0; 17518707219SMatthias Ringwald att_server->connection.authorized = 0; 17618707219SMatthias Ringwald att_server->ir_le_device_db_index = -1; 1773deb3ec6SMatthias Ringwald break; 1783deb3ec6SMatthias Ringwald 1793deb3ec6SMatthias Ringwald default: 1803deb3ec6SMatthias Ringwald break; 1813deb3ec6SMatthias Ringwald } 1823deb3ec6SMatthias Ringwald break; 1833deb3ec6SMatthias Ringwald 1843deb3ec6SMatthias Ringwald case HCI_EVENT_ENCRYPTION_CHANGE: 1853deb3ec6SMatthias Ringwald case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE: 1863deb3ec6SMatthias Ringwald // check handle 18718707219SMatthias Ringwald con_handle = little_endian_read_16(packet, 3); 18818707219SMatthias Ringwald att_server = att_server_for_handle(con_handle); 18918707219SMatthias Ringwald if (!att_server) break; 19018707219SMatthias Ringwald att_server->connection.encryption_key_size = sm_encryption_key_size(con_handle); 19118707219SMatthias Ringwald att_server->connection.authenticated = sm_authenticated(con_handle); 1923deb3ec6SMatthias Ringwald break; 1933deb3ec6SMatthias Ringwald 1943deb3ec6SMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 19570a390c7SMatthias Ringwald // check handle 19618707219SMatthias Ringwald con_handle = hci_event_disconnection_complete_get_connection_handle(packet); 19718707219SMatthias Ringwald att_server = att_server_for_handle(con_handle); 19818707219SMatthias Ringwald if (!att_server) break; 19918707219SMatthias Ringwald att_clear_transaction_queue(&att_server->connection); 20018707219SMatthias Ringwald att_server->connection.con_handle = 0; 20118707219SMatthias Ringwald att_server->value_indication_handle = 0; // reset error state 20218707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 2033deb3ec6SMatthias Ringwald break; 2043deb3ec6SMatthias Ringwald 2055611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_STARTED: 20618707219SMatthias Ringwald con_handle = sm_event_identity_resolving_started_get_handle(packet); 20718707219SMatthias Ringwald att_server = att_server_for_handle(con_handle); 20818707219SMatthias Ringwald if (!att_server) break; 2095611a760SMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED"); 21018707219SMatthias Ringwald att_server->ir_lookup_active = 1; 2113deb3ec6SMatthias Ringwald break; 2125611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED: 21318707219SMatthias Ringwald con_handle = sm_event_identity_resolving_succeeded_get_handle(packet); 21418707219SMatthias Ringwald att_server = att_server_for_handle(con_handle); 21518707219SMatthias Ringwald if (!att_server) break; 21618707219SMatthias Ringwald att_server->ir_lookup_active = 0; 2176cf9be56SMatthias Ringwald att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index(packet); 21818707219SMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED id %u", att_server->ir_le_device_db_index); 21918707219SMatthias Ringwald att_run_for_context(att_server); 2203deb3ec6SMatthias Ringwald break; 2215611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_FAILED: 22218707219SMatthias Ringwald con_handle = sm_event_identity_resolving_failed_get_handle(packet); 22318707219SMatthias Ringwald att_server = att_server_for_handle(con_handle); 22418707219SMatthias Ringwald if (!att_server) break; 2255611a760SMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED"); 22618707219SMatthias Ringwald att_server->ir_lookup_active = 0; 22718707219SMatthias Ringwald att_server->ir_le_device_db_index = -1; 22818707219SMatthias Ringwald att_run_for_context(att_server); 2293deb3ec6SMatthias Ringwald break; 2305611a760SMatthias Ringwald case SM_EVENT_AUTHORIZATION_RESULT: { 23118707219SMatthias Ringwald con_handle = sm_event_authorization_result_get_handle(packet); 23218707219SMatthias Ringwald att_server = att_server_for_handle(con_handle); 23318707219SMatthias Ringwald if (!att_server) break; 23418707219SMatthias Ringwald att_server->connection.authorized = sm_event_authorization_result_get_authorization_result(packet); 23518707219SMatthias Ringwald att_dispatch_server_request_can_send_now_event(con_handle); 2363deb3ec6SMatthias Ringwald break; 2373deb3ec6SMatthias Ringwald } 2383deb3ec6SMatthias Ringwald default: 2393deb3ec6SMatthias Ringwald break; 2403deb3ec6SMatthias Ringwald } 24165b44ffdSMatthias Ringwald break; 24265b44ffdSMatthias Ringwald default: 24365b44ffdSMatthias Ringwald break; 2443deb3ec6SMatthias Ringwald } 2453deb3ec6SMatthias Ringwald } 2463deb3ec6SMatthias Ringwald 2477a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 2483deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){ 2493deb3ec6SMatthias Ringwald 25018707219SMatthias Ringwald att_server_t * att_server = att_server_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION); 25118707219SMatthias Ringwald if (!att_server) return; 2523deb3ec6SMatthias Ringwald 2533deb3ec6SMatthias Ringwald uint8_t hash_flipped[8]; 2549c80e4ccSMatthias Ringwald reverse_64(hash, hash_flipped); 25518707219SMatthias Ringwald if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8)){ 2563deb3ec6SMatthias Ringwald log_info("ATT Signed Write, invalid signature"); 25718707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 2583deb3ec6SMatthias Ringwald return; 2593deb3ec6SMatthias Ringwald } 2603deb3ec6SMatthias Ringwald log_info("ATT Signed Write, valid signature"); 2613deb3ec6SMatthias Ringwald 2623deb3ec6SMatthias Ringwald // update sequence number 26318707219SMatthias Ringwald uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12); 26418707219SMatthias Ringwald le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1); 26518707219SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 26618707219SMatthias Ringwald att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle); 2673deb3ec6SMatthias Ringwald } 2687a766ebfSMatthias Ringwald #endif 26988a48dd8SMatthias Ringwald 27018707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED 271b06f2579SMatthias Ringwald // pre: can send now 272b06f2579SMatthias Ringwald // returns: 1 if packet was sent 27318707219SMatthias Ringwald static int att_server_process_validated_request(att_server_t * att_server){ 274b06f2579SMatthias Ringwald 275b06f2579SMatthias Ringwald l2cap_reserve_packet_buffer(); 276b06f2579SMatthias Ringwald uint8_t * att_response_buffer = l2cap_get_outgoing_buffer(); 27718707219SMatthias Ringwald uint16_t att_response_size = att_handle_request(&att_server->connection, att_server->request_buffer, att_server->request_size, att_response_buffer); 278b06f2579SMatthias Ringwald 279b06f2579SMatthias Ringwald // intercept "insufficient authorization" for authenticated connections to allow for user authorization 280b06f2579SMatthias Ringwald if ((att_response_size >= 4) 281b06f2579SMatthias Ringwald && (att_response_buffer[0] == ATT_ERROR_RESPONSE) 282b06f2579SMatthias Ringwald && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION) 28318707219SMatthias Ringwald && (att_server->connection.authenticated)){ 284b06f2579SMatthias Ringwald 28518707219SMatthias Ringwald switch (sm_authorization_state(att_server->connection.con_handle)){ 286b06f2579SMatthias Ringwald case AUTHORIZATION_UNKNOWN: 287b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 28818707219SMatthias Ringwald sm_request_pairing(att_server->connection.con_handle); 289b06f2579SMatthias Ringwald return 0; 290b06f2579SMatthias Ringwald case AUTHORIZATION_PENDING: 291b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 292b06f2579SMatthias Ringwald return 0; 293b06f2579SMatthias Ringwald default: 294b06f2579SMatthias Ringwald break; 295b06f2579SMatthias Ringwald } 296b06f2579SMatthias Ringwald } 297b06f2579SMatthias Ringwald 29818707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 299b06f2579SMatthias Ringwald if (att_response_size == 0) { 300b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 301b06f2579SMatthias Ringwald return 0; 302b06f2579SMatthias Ringwald } 303b06f2579SMatthias Ringwald 30418707219SMatthias Ringwald l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size); 305b06f2579SMatthias Ringwald 306b06f2579SMatthias Ringwald // notify client about MTU exchange result 307b06f2579SMatthias Ringwald if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){ 30818707219SMatthias Ringwald att_emit_mtu_event(att_server->connection.con_handle, att_server->connection.mtu); 309b06f2579SMatthias Ringwald } 310b06f2579SMatthias Ringwald return 1; 311b06f2579SMatthias Ringwald } 312b06f2579SMatthias Ringwald 31318707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server){ 31418707219SMatthias Ringwald switch (att_server->state){ 3153deb3ec6SMatthias Ringwald case ATT_SERVER_REQUEST_RECEIVED: 3167a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 31718707219SMatthias Ringwald if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){ 3183deb3ec6SMatthias Ringwald log_info("ATT Signed Write!"); 3193deb3ec6SMatthias Ringwald if (!sm_cmac_ready()) { 3203deb3ec6SMatthias Ringwald log_info("ATT Signed Write, sm_cmac engine not ready. Abort"); 32118707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 3223deb3ec6SMatthias Ringwald return; 3233deb3ec6SMatthias Ringwald } 32418707219SMatthias Ringwald if (att_server->request_size < (3 + 12)) { 3253deb3ec6SMatthias Ringwald log_info("ATT Signed Write, request to short. Abort."); 32618707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 3273deb3ec6SMatthias Ringwald return; 3283deb3ec6SMatthias Ringwald } 32918707219SMatthias Ringwald if (att_server->ir_lookup_active){ 3303deb3ec6SMatthias Ringwald return; 3313deb3ec6SMatthias Ringwald } 33218707219SMatthias Ringwald if (att_server->ir_le_device_db_index < 0){ 3333deb3ec6SMatthias Ringwald log_info("ATT Signed Write, CSRK not available"); 33418707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 3353deb3ec6SMatthias Ringwald return; 3363deb3ec6SMatthias Ringwald } 3373deb3ec6SMatthias Ringwald 3383deb3ec6SMatthias Ringwald // check counter 33918707219SMatthias Ringwald uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12); 34018707219SMatthias Ringwald uint32_t counter_db = le_device_db_remote_counter_get(att_server->ir_le_device_db_index); 3413deb3ec6SMatthias Ringwald log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet); 3423deb3ec6SMatthias Ringwald if (counter_packet < counter_db){ 3433deb3ec6SMatthias Ringwald log_info("ATT Signed Write, db reports higher counter, abort"); 34418707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 3453deb3ec6SMatthias Ringwald return; 3463deb3ec6SMatthias Ringwald } 3473deb3ec6SMatthias Ringwald 3483deb3ec6SMatthias Ringwald // signature is { sequence counter, secure hash } 3493deb3ec6SMatthias Ringwald sm_key_t csrk; 35018707219SMatthias Ringwald le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk); 35118707219SMatthias Ringwald att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION; 3523deb3ec6SMatthias Ringwald log_info("Orig Signature: "); 35318707219SMatthias Ringwald log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8); 35418707219SMatthias Ringwald uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1); 35518707219SMatthias 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); 3563deb3ec6SMatthias Ringwald return; 3573deb3ec6SMatthias Ringwald } 3587a766ebfSMatthias Ringwald #endif 359b06f2579SMatthias Ringwald // move on 36018707219SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 36118707219SMatthias Ringwald att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle); 362b06f2579SMatthias Ringwald break; 36388a48dd8SMatthias Ringwald 3643deb3ec6SMatthias Ringwald default: 3653deb3ec6SMatthias Ringwald break; 3663deb3ec6SMatthias Ringwald } 3673deb3ec6SMatthias Ringwald } 3683deb3ec6SMatthias Ringwald 3697eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){ 370b06f2579SMatthias Ringwald 371b06f2579SMatthias Ringwald // NOTE: we get l2cap fixed channel instead of con_handle 372b06f2579SMatthias Ringwald 37318707219SMatthias Ringwald btstack_linked_list_iterator_t it; 37418707219SMatthias Ringwald hci_connections_get_iterator(&it); 37518707219SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 37618707219SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 37718707219SMatthias Ringwald att_server_t * att_server = &connection->att_server; 37818707219SMatthias Ringwald if (att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED){ 37918707219SMatthias Ringwald int sent = att_server_process_validated_request(att_server); 380bb38f057SMatthias Ringwald if (sent && (att_client_waiting_for_can_send || !btstack_linked_list_empty(&can_send_now_clients))){ 38118707219SMatthias Ringwald att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle); 3823deb3ec6SMatthias Ringwald return; 3833deb3ec6SMatthias Ringwald } 3843deb3ec6SMatthias Ringwald } 38518707219SMatthias Ringwald } 3863deb3ec6SMatthias Ringwald 387bb38f057SMatthias Ringwald while (!btstack_linked_list_empty(&can_send_now_clients)){ 388bb38f057SMatthias Ringwald // handle first client 389bb38f057SMatthias Ringwald btstack_context_callback_registration_t * client = (btstack_context_callback_registration_t*) can_send_now_clients; 39018707219SMatthias Ringwald hci_con_handle_t con_handle = (uintptr_t) client->context; 391bb38f057SMatthias Ringwald btstack_linked_list_remove(&can_send_now_clients, (btstack_linked_item_t *) client); 392bb38f057SMatthias Ringwald client->callback(client->context); 393bb38f057SMatthias Ringwald 394bb38f057SMatthias Ringwald // request again if needed 39518707219SMatthias Ringwald if (!att_dispatch_server_can_send_now(con_handle)){ 396bb38f057SMatthias Ringwald if (!btstack_linked_list_empty(&can_send_now_clients) || att_client_waiting_for_can_send){ 39718707219SMatthias Ringwald att_dispatch_server_request_can_send_now_event(con_handle); 398bb38f057SMatthias Ringwald } 399bb38f057SMatthias Ringwald return; 400bb38f057SMatthias Ringwald } 401bb38f057SMatthias Ringwald } 402bb38f057SMatthias Ringwald 403b06f2579SMatthias Ringwald if (att_client_waiting_for_can_send){ 404b06f2579SMatthias Ringwald att_client_waiting_for_can_send = 0; 405b06f2579SMatthias Ringwald att_emit_can_send_now_event(); 4063deb3ec6SMatthias Ringwald } 4073deb3ec6SMatthias Ringwald } 4083deb3ec6SMatthias Ringwald 40988a48dd8SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){ 41018707219SMatthias Ringwald 41118707219SMatthias Ringwald att_server_t * att_server; 41218707219SMatthias Ringwald 41388a48dd8SMatthias Ringwald switch (packet_type){ 41488a48dd8SMatthias Ringwald 41588a48dd8SMatthias Ringwald case HCI_EVENT_PACKET: 41688a48dd8SMatthias Ringwald if (packet[0] != L2CAP_EVENT_CAN_SEND_NOW) break; 4174d2be802SMatthias Ringwald att_server_handle_can_send_now(); 418372d62c4SMatthias Ringwald break; 4193deb3ec6SMatthias Ringwald 420372d62c4SMatthias Ringwald case ATT_DATA_PACKET: 42118707219SMatthias Ringwald log_info("ATT Packet, handle 0x%04x", handle); 42218707219SMatthias Ringwald att_server = att_server_for_handle(handle); 42318707219SMatthias Ringwald if (!att_server) break; 42418707219SMatthias Ringwald 4253deb3ec6SMatthias Ringwald // handle value indication confirms 42618707219SMatthias Ringwald if (packet[0] == ATT_HANDLE_VALUE_CONFIRMATION && att_server->value_indication_handle){ 42718707219SMatthias Ringwald btstack_run_loop_remove_timer(&att_server->value_indication_timer); 42818707219SMatthias Ringwald uint16_t att_handle = att_server->value_indication_handle; 42918707219SMatthias Ringwald att_server->value_indication_handle = 0; 43018707219SMatthias Ringwald att_handle_value_indication_notify_client(0, att_server->connection.con_handle, att_handle); 4313deb3ec6SMatthias Ringwald return; 4323deb3ec6SMatthias Ringwald } 4333deb3ec6SMatthias Ringwald 4346428eb5aSMatthias Ringwald // directly process command 4356428eb5aSMatthias Ringwald // note: signed write cannot be handled directly as authentication needs to be verified 4366428eb5aSMatthias Ringwald if (packet[0] == ATT_WRITE_COMMAND){ 43718707219SMatthias Ringwald att_handle_request(&att_server->connection, packet, size, 0); 4386428eb5aSMatthias Ringwald return; 4396428eb5aSMatthias Ringwald } 4406428eb5aSMatthias Ringwald 4413deb3ec6SMatthias Ringwald // check size 44218707219SMatthias Ringwald if (size > sizeof(att_server->request_buffer)) { 44318707219SMatthias Ringwald log_info("att_packet_handler: dropping att pdu 0x%02x as size %u > att_server->request_buffer %u", packet[0], size, (int) sizeof(att_server->request_buffer)); 4446428eb5aSMatthias Ringwald return; 4456428eb5aSMatthias Ringwald } 4463deb3ec6SMatthias Ringwald 4473deb3ec6SMatthias Ringwald // last request still in processing? 44818707219SMatthias Ringwald if (att_server->state != ATT_SERVER_IDLE){ 44918707219SMatthias Ringwald log_info("att_packet_handler: skipping att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state); 4506428eb5aSMatthias Ringwald return; 4516428eb5aSMatthias Ringwald } 4523deb3ec6SMatthias Ringwald 4533deb3ec6SMatthias Ringwald // store request 45418707219SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED; 45518707219SMatthias Ringwald att_server->request_size = size; 45618707219SMatthias Ringwald memcpy(att_server->request_buffer, packet, size); 4573deb3ec6SMatthias Ringwald 45818707219SMatthias Ringwald att_run_for_context(att_server); 459372d62c4SMatthias Ringwald break; 460372d62c4SMatthias Ringwald } 4613deb3ec6SMatthias Ringwald } 4623deb3ec6SMatthias Ringwald 463*3d71c7a4SMatthias Ringwald 464*3d71c7a4SMatthias Ringwald // gatt service management 465*3d71c7a4SMatthias Ringwald static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){ 466*3d71c7a4SMatthias Ringwald btstack_linked_list_iterator_t it; 467*3d71c7a4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 468*3d71c7a4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 469*3d71c7a4SMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 470*3d71c7a4SMatthias Ringwald if (handler->start_handle > handle) continue; 471*3d71c7a4SMatthias Ringwald if (handler->end_handle < handle) continue; 472*3d71c7a4SMatthias Ringwald return handler; 473*3d71c7a4SMatthias Ringwald } 474*3d71c7a4SMatthias Ringwald return NULL; 475*3d71c7a4SMatthias Ringwald } 476*3d71c7a4SMatthias Ringwald static att_read_callback_t att_server_read_callback_for_handle(uint16_t handle){ 477*3d71c7a4SMatthias Ringwald att_service_handler_t * handler = att_service_handler_for_handle(handle); 478*3d71c7a4SMatthias Ringwald if (handler) return handler->read_callback; 479*3d71c7a4SMatthias Ringwald return att_server_client_read_callback; 480*3d71c7a4SMatthias Ringwald } 481*3d71c7a4SMatthias Ringwald 482*3d71c7a4SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){ 483*3d71c7a4SMatthias Ringwald att_service_handler_t * handler = att_service_handler_for_handle(handle); 484*3d71c7a4SMatthias Ringwald if (handler) return handler->write_callback; 485*3d71c7a4SMatthias Ringwald return att_server_client_write_callback; 486*3d71c7a4SMatthias Ringwald } 487*3d71c7a4SMatthias Ringwald 488*3d71c7a4SMatthias Ringwald static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){ 489*3d71c7a4SMatthias Ringwald // notify all callbacks 490*3d71c7a4SMatthias Ringwald btstack_linked_list_iterator_t it; 491*3d71c7a4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 492*3d71c7a4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 493*3d71c7a4SMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 494*3d71c7a4SMatthias Ringwald if (!handler->write_callback) continue; 495*3d71c7a4SMatthias Ringwald (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0); 496*3d71c7a4SMatthias Ringwald } 497*3d71c7a4SMatthias Ringwald if (!att_server_client_write_callback) return; 498*3d71c7a4SMatthias Ringwald (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0); 499*3d71c7a4SMatthias Ringwald } 500*3d71c7a4SMatthias Ringwald 501*3d71c7a4SMatthias Ringwald // returns first reported error or 0 502*3d71c7a4SMatthias Ringwald static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){ 503*3d71c7a4SMatthias Ringwald btstack_linked_list_iterator_t it; 504*3d71c7a4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 505*3d71c7a4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 506*3d71c7a4SMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 507*3d71c7a4SMatthias Ringwald if (!handler->write_callback) continue; 508*3d71c7a4SMatthias Ringwald uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0); 509*3d71c7a4SMatthias Ringwald if (error_code) return error_code; 510*3d71c7a4SMatthias Ringwald } 511*3d71c7a4SMatthias Ringwald if (!att_server_client_write_callback) return 0; 512*3d71c7a4SMatthias Ringwald return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0); 513*3d71c7a4SMatthias Ringwald } 514*3d71c7a4SMatthias Ringwald 515*3d71c7a4SMatthias 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){ 516*3d71c7a4SMatthias Ringwald att_read_callback_t callback = att_server_read_callback_for_handle(attribute_handle); 517*3d71c7a4SMatthias Ringwald return (*callback)(con_handle, attribute_handle, offset, buffer, buffer_size); 518*3d71c7a4SMatthias Ringwald } 519*3d71c7a4SMatthias Ringwald 520*3d71c7a4SMatthias 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){ 521*3d71c7a4SMatthias Ringwald switch (transaction_mode){ 522*3d71c7a4SMatthias Ringwald case ATT_TRANSACTION_MODE_VALIDATE: 523*3d71c7a4SMatthias Ringwald return att_validate_prepared_write(con_handle); 524*3d71c7a4SMatthias Ringwald case ATT_TRANSACTION_MODE_EXECUTE: 525*3d71c7a4SMatthias Ringwald case ATT_TRANSACTION_MODE_CANCEL: 526*3d71c7a4SMatthias Ringwald att_notify_write_callbacks(con_handle, transaction_mode); 527*3d71c7a4SMatthias Ringwald return 0; 528*3d71c7a4SMatthias Ringwald default: 529*3d71c7a4SMatthias Ringwald break; 530*3d71c7a4SMatthias Ringwald } 531*3d71c7a4SMatthias Ringwald att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle); 532*3d71c7a4SMatthias Ringwald 533*3d71c7a4SMatthias Ringwald // TODO: check if write for ccc, if yes store in tlv 534*3d71c7a4SMatthias Ringwald return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size); 535*3d71c7a4SMatthias Ringwald } 536*3d71c7a4SMatthias Ringwald 537*3d71c7a4SMatthias Ringwald /** 538*3d71c7a4SMatthias Ringwald * @brief register read/write callbacks for specific handle range 539*3d71c7a4SMatthias Ringwald * @param att_service_handler_t 540*3d71c7a4SMatthias Ringwald */ 541*3d71c7a4SMatthias Ringwald void att_server_register_service_handler(att_service_handler_t * handler){ 542*3d71c7a4SMatthias Ringwald if (att_service_handler_for_handle(handler->start_handle) || 543*3d71c7a4SMatthias Ringwald att_service_handler_for_handle(handler->end_handle)){ 544*3d71c7a4SMatthias Ringwald log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle); 545*3d71c7a4SMatthias Ringwald return; 546*3d71c7a4SMatthias Ringwald } 547*3d71c7a4SMatthias Ringwald btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler); 548*3d71c7a4SMatthias Ringwald } 549*3d71c7a4SMatthias Ringwald 5503deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){ 5513deb3ec6SMatthias Ringwald 552*3d71c7a4SMatthias Ringwald // store callbacks 553*3d71c7a4SMatthias Ringwald att_server_client_read_callback = read_callback; 554*3d71c7a4SMatthias Ringwald att_server_client_write_callback = write_callback; 555*3d71c7a4SMatthias Ringwald 5561a389cdcSMatthias Ringwald // register for HCI Events 557d9a7306aSMatthias Ringwald hci_event_callback_registration.callback = &att_event_packet_handler; 5581a389cdcSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 5591a389cdcSMatthias Ringwald 560406722e4SMatthias Ringwald // register for SM events 561d9a7306aSMatthias Ringwald sm_event_callback_registration.callback = &att_event_packet_handler; 562406722e4SMatthias Ringwald sm_add_event_handler(&sm_event_callback_registration); 5633deb3ec6SMatthias Ringwald 5641a389cdcSMatthias Ringwald // and L2CAP ATT Server PDUs 5653deb3ec6SMatthias Ringwald att_dispatch_register_server(att_packet_handler); 5663deb3ec6SMatthias Ringwald 5673deb3ec6SMatthias Ringwald att_set_db(db); 568*3d71c7a4SMatthias Ringwald att_set_read_callback(att_server_read_callback); 569*3d71c7a4SMatthias Ringwald att_set_write_callback(att_server_write_callback); 5703deb3ec6SMatthias Ringwald 5713deb3ec6SMatthias Ringwald } 5723deb3ec6SMatthias Ringwald 5733deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){ 5743deb3ec6SMatthias Ringwald att_client_packet_handler = handler; 5753deb3ec6SMatthias Ringwald } 5763deb3ec6SMatthias Ringwald 577c141988cSMatthias Ringwald int att_server_can_send_packet_now(hci_con_handle_t con_handle){ 57818707219SMatthias Ringwald return att_dispatch_server_can_send_now(con_handle); 5791b96b007SMatthias Ringwald } 5801b96b007SMatthias Ringwald 581c141988cSMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){ 582d9d23054SMatthias Ringwald log_debug("att_server_request_can_send_now_event 0x%04x", con_handle); 5831b96b007SMatthias Ringwald att_client_waiting_for_can_send = 1; 58418707219SMatthias Ringwald att_dispatch_server_request_can_send_now_event(con_handle); 5853deb3ec6SMatthias Ringwald } 5863deb3ec6SMatthias Ringwald 587bb38f057SMatthias Ringwald void att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 588bb38f057SMatthias Ringwald // check if can send already 589bb38f057SMatthias Ringwald if (att_dispatch_server_can_send_now(con_handle)){ 590bb38f057SMatthias Ringwald callback_registration->callback(callback_registration->context); 591bb38f057SMatthias Ringwald return; 592bb38f057SMatthias Ringwald } 59318707219SMatthias Ringwald callback_registration->context = (void*)(uintptr_t) con_handle; 594bb38f057SMatthias Ringwald btstack_linked_list_add_tail(&can_send_now_clients, (btstack_linked_item_t*) callback_registration); 595bb38f057SMatthias Ringwald att_dispatch_server_request_can_send_now_event(con_handle); 596bb38f057SMatthias Ringwald } 597bb38f057SMatthias Ringwald 59818707219SMatthias Ringwald 599c141988cSMatthias Ringwald int att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){ 60018707219SMatthias Ringwald att_server_t * att_server = att_server_for_handle(con_handle); 60118707219SMatthias Ringwald if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 60218707219SMatthias Ringwald 60318707219SMatthias Ringwald if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL; 6043deb3ec6SMatthias Ringwald 6053deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 6063deb3ec6SMatthias Ringwald uint8_t * packet_buffer = l2cap_get_outgoing_buffer(); 60718707219SMatthias Ringwald uint16_t size = att_prepare_handle_value_notification(&att_server->connection, attribute_handle, value, value_len, packet_buffer); 60818707219SMatthias Ringwald return l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); 6093deb3ec6SMatthias Ringwald } 6103deb3ec6SMatthias Ringwald 611c141988cSMatthias Ringwald int att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){ 61218707219SMatthias Ringwald att_server_t * att_server = att_server_for_handle(con_handle); 61318707219SMatthias Ringwald if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 61418707219SMatthias Ringwald 615eaac31e8SMatthias Ringwald if (att_server->value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS; 61618707219SMatthias Ringwald if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL; 6173deb3ec6SMatthias Ringwald 6183deb3ec6SMatthias Ringwald // track indication 61918707219SMatthias Ringwald att_server->value_indication_handle = attribute_handle; 62018707219SMatthias Ringwald btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout); 62118707219SMatthias Ringwald btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS); 62218707219SMatthias Ringwald btstack_run_loop_add_timer(&att_server->value_indication_timer); 6233deb3ec6SMatthias Ringwald 6243deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 6253deb3ec6SMatthias Ringwald uint8_t * packet_buffer = l2cap_get_outgoing_buffer(); 62618707219SMatthias Ringwald uint16_t size = att_prepare_handle_value_indication(&att_server->connection, attribute_handle, value, value_len, packet_buffer); 62718707219SMatthias Ringwald l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); 6283deb3ec6SMatthias Ringwald return 0; 6293deb3ec6SMatthias Ringwald } 630