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" 67*bf78aac6SMatthias Ringwald #include "btstack_tlv.h" 68*bf78aac6SMatthias Ringwald 69*bf78aac6SMatthias Ringwald #ifndef GATT_SERVER_NUM_PERSISTENT_CCC 70*bf78aac6SMatthias Ringwald #define GATT_SERVER_NUM_PERSISTENT_CCC 20 71*bf78aac6SMatthias Ringwald #endif 723deb3ec6SMatthias Ringwald 7318707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server); 743deb3ec6SMatthias Ringwald 7518707219SMatthias Ringwald // global 761a389cdcSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 77406722e4SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration; 783deb3ec6SMatthias Ringwald static btstack_packet_handler_t att_client_packet_handler = NULL; 79bb38f057SMatthias Ringwald static btstack_linked_list_t can_send_now_clients; 803d71c7a4SMatthias Ringwald static btstack_linked_list_t service_handlers; 8118707219SMatthias Ringwald static uint8_t att_client_waiting_for_can_send; 8218707219SMatthias Ringwald 833d71c7a4SMatthias Ringwald static att_read_callback_t att_server_client_read_callback; 843d71c7a4SMatthias Ringwald static att_write_callback_t att_server_client_write_callback; 853d71c7a4SMatthias Ringwald 86*bf78aac6SMatthias Ringwald // track CCC 1-entry cache 87*bf78aac6SMatthias Ringwald // static att_server_t * att_persistent_ccc_server; 88*bf78aac6SMatthias Ringwald // static hci_con_handle_t att_persistent_ccc_con_handle; 89*bf78aac6SMatthias Ringwald 9018707219SMatthias Ringwald static att_server_t * att_server_for_handle(hci_con_handle_t con_handle){ 9118707219SMatthias Ringwald hci_connection_t * hci_connection = hci_connection_for_handle(con_handle); 9218707219SMatthias Ringwald if (!hci_connection) return NULL; 9318707219SMatthias Ringwald return &hci_connection->att_server; 9418707219SMatthias Ringwald } 9518707219SMatthias Ringwald 9602b78fc8SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 9718707219SMatthias Ringwald static att_server_t * att_server_for_state(att_server_state_t state){ 9818707219SMatthias Ringwald btstack_linked_list_iterator_t it; 9918707219SMatthias Ringwald hci_connections_get_iterator(&it); 10018707219SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 10118707219SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 10218707219SMatthias Ringwald att_server_t * att_server = &connection->att_server; 10318707219SMatthias Ringwald if (att_server->state == state) return att_server; 10418707219SMatthias Ringwald } 10518707219SMatthias Ringwald return NULL; 10618707219SMatthias Ringwald } 10702b78fc8SMatthias Ringwald #endif 108bb38f057SMatthias Ringwald 1093deb3ec6SMatthias Ringwald static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){ 1103deb3ec6SMatthias Ringwald if (!att_client_packet_handler) return; 1113deb3ec6SMatthias Ringwald 1123deb3ec6SMatthias Ringwald uint8_t event[7]; 1133deb3ec6SMatthias Ringwald int pos = 0; 1145611a760SMatthias Ringwald event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE; 1153deb3ec6SMatthias Ringwald event[pos++] = sizeof(event) - 2; 1163deb3ec6SMatthias Ringwald event[pos++] = status; 117f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, client_handle); 1183deb3ec6SMatthias Ringwald pos += 2; 119f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, attribute_handle); 1203deb3ec6SMatthias Ringwald (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 1213deb3ec6SMatthias Ringwald } 1223deb3ec6SMatthias Ringwald 123fc64f94aSMatthias Ringwald static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){ 1243deb3ec6SMatthias Ringwald if (!att_client_packet_handler) return; 1253deb3ec6SMatthias Ringwald 1263deb3ec6SMatthias Ringwald uint8_t event[6]; 1273deb3ec6SMatthias Ringwald int pos = 0; 1285611a760SMatthias Ringwald event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE; 1293deb3ec6SMatthias Ringwald event[pos++] = sizeof(event) - 2; 130fc64f94aSMatthias Ringwald little_endian_store_16(event, pos, con_handle); 1313deb3ec6SMatthias Ringwald pos += 2; 132f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, mtu); 1333deb3ec6SMatthias Ringwald (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 1343deb3ec6SMatthias Ringwald } 1353deb3ec6SMatthias Ringwald 1361b96b007SMatthias Ringwald static void att_emit_can_send_now_event(void){ 1373c97b242SMatthias Ringwald if (!att_client_packet_handler) return; 1383c97b242SMatthias Ringwald 1391b96b007SMatthias Ringwald uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0}; 1401b96b007SMatthias Ringwald (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 1411b96b007SMatthias Ringwald } 1421b96b007SMatthias Ringwald 143ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){ 14454178543SMatthias Ringwald void * context = btstack_run_loop_get_timer_context(ts); 14554178543SMatthias Ringwald hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context; 14618707219SMatthias Ringwald att_server_t * att_server = att_server_for_handle(con_handle); 14718707219SMatthias Ringwald if (!att_server) return; 14818707219SMatthias Ringwald uint16_t att_handle = att_server->value_indication_handle; 14918707219SMatthias Ringwald att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_server->connection.con_handle, att_handle); 1503deb3ec6SMatthias Ringwald } 1513deb3ec6SMatthias Ringwald 1523deb3ec6SMatthias Ringwald static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1539ec2630cSMatthias Ringwald 1547dad9ff8SMatthias Ringwald UNUSED(channel); // ok: there is no channel 1557dad9ff8SMatthias Ringwald UNUSED(size); // ok: handling own l2cap events 1563deb3ec6SMatthias Ringwald 15718707219SMatthias Ringwald att_server_t * att_server; 15818707219SMatthias Ringwald hci_con_handle_t con_handle; 15918707219SMatthias Ringwald 1603deb3ec6SMatthias Ringwald switch (packet_type) { 1613deb3ec6SMatthias Ringwald 1623deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 1630e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 1643deb3ec6SMatthias Ringwald 1653deb3ec6SMatthias Ringwald case HCI_EVENT_LE_META: 1663deb3ec6SMatthias Ringwald switch (packet[2]) { 1673deb3ec6SMatthias Ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 16818707219SMatthias Ringwald con_handle = little_endian_read_16(packet, 4); 16918707219SMatthias Ringwald att_server = att_server_for_handle(con_handle); 17018707219SMatthias Ringwald if (!att_server) break; 1713deb3ec6SMatthias Ringwald // store connection info 17218707219SMatthias Ringwald att_server->peer_addr_type = packet[7]; 17318707219SMatthias Ringwald reverse_bd_addr(&packet[8], att_server->peer_address); 17418707219SMatthias Ringwald att_server->connection.con_handle = con_handle; 1753deb3ec6SMatthias Ringwald // reset connection properties 17618707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 17718707219SMatthias Ringwald att_server->connection.mtu = ATT_DEFAULT_MTU; 17818707219SMatthias Ringwald att_server->connection.max_mtu = l2cap_max_le_mtu(); 17918707219SMatthias Ringwald if (att_server->connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){ 18018707219SMatthias Ringwald att_server->connection.max_mtu = ATT_REQUEST_BUFFER_SIZE; 18199c58ad0SMatthias Ringwald } 18218707219SMatthias Ringwald att_server->connection.encryption_key_size = 0; 18318707219SMatthias Ringwald att_server->connection.authenticated = 0; 18418707219SMatthias Ringwald att_server->connection.authorized = 0; 18518707219SMatthias Ringwald att_server->ir_le_device_db_index = -1; 1863deb3ec6SMatthias Ringwald break; 1873deb3ec6SMatthias Ringwald 1883deb3ec6SMatthias Ringwald default: 1893deb3ec6SMatthias Ringwald break; 1903deb3ec6SMatthias Ringwald } 1913deb3ec6SMatthias Ringwald break; 1923deb3ec6SMatthias Ringwald 1933deb3ec6SMatthias Ringwald case HCI_EVENT_ENCRYPTION_CHANGE: 1943deb3ec6SMatthias Ringwald case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE: 1953deb3ec6SMatthias Ringwald // check handle 19618707219SMatthias Ringwald con_handle = little_endian_read_16(packet, 3); 19718707219SMatthias Ringwald att_server = att_server_for_handle(con_handle); 19818707219SMatthias Ringwald if (!att_server) break; 19918707219SMatthias Ringwald att_server->connection.encryption_key_size = sm_encryption_key_size(con_handle); 20018707219SMatthias Ringwald att_server->connection.authenticated = sm_authenticated(con_handle); 2013deb3ec6SMatthias Ringwald break; 2023deb3ec6SMatthias Ringwald 2033deb3ec6SMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 20470a390c7SMatthias Ringwald // check handle 20518707219SMatthias Ringwald con_handle = hci_event_disconnection_complete_get_connection_handle(packet); 20618707219SMatthias Ringwald att_server = att_server_for_handle(con_handle); 20718707219SMatthias Ringwald if (!att_server) break; 20818707219SMatthias Ringwald att_clear_transaction_queue(&att_server->connection); 20918707219SMatthias Ringwald att_server->connection.con_handle = 0; 21018707219SMatthias Ringwald att_server->value_indication_handle = 0; // reset error state 21118707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 2123deb3ec6SMatthias Ringwald break; 2133deb3ec6SMatthias Ringwald 2145611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_STARTED: 21518707219SMatthias Ringwald con_handle = sm_event_identity_resolving_started_get_handle(packet); 21618707219SMatthias Ringwald att_server = att_server_for_handle(con_handle); 21718707219SMatthias Ringwald if (!att_server) break; 2185611a760SMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED"); 21918707219SMatthias Ringwald att_server->ir_lookup_active = 1; 2203deb3ec6SMatthias Ringwald break; 2215611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED: 22218707219SMatthias Ringwald con_handle = sm_event_identity_resolving_succeeded_get_handle(packet); 22318707219SMatthias Ringwald att_server = att_server_for_handle(con_handle); 22418707219SMatthias Ringwald if (!att_server) break; 22518707219SMatthias Ringwald att_server->ir_lookup_active = 0; 2266cf9be56SMatthias Ringwald att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index(packet); 22718707219SMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED id %u", att_server->ir_le_device_db_index); 22818707219SMatthias Ringwald att_run_for_context(att_server); 2293deb3ec6SMatthias Ringwald break; 2305611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_FAILED: 23118707219SMatthias Ringwald con_handle = sm_event_identity_resolving_failed_get_handle(packet); 23218707219SMatthias Ringwald att_server = att_server_for_handle(con_handle); 23318707219SMatthias Ringwald if (!att_server) break; 2345611a760SMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED"); 23518707219SMatthias Ringwald att_server->ir_lookup_active = 0; 23618707219SMatthias Ringwald att_server->ir_le_device_db_index = -1; 23718707219SMatthias Ringwald att_run_for_context(att_server); 2383deb3ec6SMatthias Ringwald break; 2395611a760SMatthias Ringwald case SM_EVENT_AUTHORIZATION_RESULT: { 24018707219SMatthias Ringwald con_handle = sm_event_authorization_result_get_handle(packet); 24118707219SMatthias Ringwald att_server = att_server_for_handle(con_handle); 24218707219SMatthias Ringwald if (!att_server) break; 24318707219SMatthias Ringwald att_server->connection.authorized = sm_event_authorization_result_get_authorization_result(packet); 24418707219SMatthias Ringwald att_dispatch_server_request_can_send_now_event(con_handle); 2453deb3ec6SMatthias Ringwald break; 2463deb3ec6SMatthias Ringwald } 2473deb3ec6SMatthias Ringwald default: 2483deb3ec6SMatthias Ringwald break; 2493deb3ec6SMatthias Ringwald } 25065b44ffdSMatthias Ringwald break; 25165b44ffdSMatthias Ringwald default: 25265b44ffdSMatthias Ringwald break; 2533deb3ec6SMatthias Ringwald } 2543deb3ec6SMatthias Ringwald } 2553deb3ec6SMatthias Ringwald 2567a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 2573deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){ 2583deb3ec6SMatthias Ringwald 25918707219SMatthias Ringwald att_server_t * att_server = att_server_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION); 26018707219SMatthias Ringwald if (!att_server) return; 2613deb3ec6SMatthias Ringwald 2623deb3ec6SMatthias Ringwald uint8_t hash_flipped[8]; 2639c80e4ccSMatthias Ringwald reverse_64(hash, hash_flipped); 26418707219SMatthias Ringwald if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8)){ 2653deb3ec6SMatthias Ringwald log_info("ATT Signed Write, invalid signature"); 26618707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 2673deb3ec6SMatthias Ringwald return; 2683deb3ec6SMatthias Ringwald } 2693deb3ec6SMatthias Ringwald log_info("ATT Signed Write, valid signature"); 2703deb3ec6SMatthias Ringwald 2713deb3ec6SMatthias Ringwald // update sequence number 27218707219SMatthias Ringwald uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12); 27318707219SMatthias Ringwald le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1); 27418707219SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 27518707219SMatthias Ringwald att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle); 2763deb3ec6SMatthias Ringwald } 2777a766ebfSMatthias Ringwald #endif 27888a48dd8SMatthias Ringwald 27918707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED 280b06f2579SMatthias Ringwald // pre: can send now 281b06f2579SMatthias Ringwald // returns: 1 if packet was sent 28218707219SMatthias Ringwald static int att_server_process_validated_request(att_server_t * att_server){ 283b06f2579SMatthias Ringwald 284b06f2579SMatthias Ringwald l2cap_reserve_packet_buffer(); 285b06f2579SMatthias Ringwald uint8_t * att_response_buffer = l2cap_get_outgoing_buffer(); 28618707219SMatthias Ringwald uint16_t att_response_size = att_handle_request(&att_server->connection, att_server->request_buffer, att_server->request_size, att_response_buffer); 287b06f2579SMatthias Ringwald 288b06f2579SMatthias Ringwald // intercept "insufficient authorization" for authenticated connections to allow for user authorization 289b06f2579SMatthias Ringwald if ((att_response_size >= 4) 290b06f2579SMatthias Ringwald && (att_response_buffer[0] == ATT_ERROR_RESPONSE) 291b06f2579SMatthias Ringwald && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION) 29218707219SMatthias Ringwald && (att_server->connection.authenticated)){ 293b06f2579SMatthias Ringwald 29418707219SMatthias Ringwald switch (sm_authorization_state(att_server->connection.con_handle)){ 295b06f2579SMatthias Ringwald case AUTHORIZATION_UNKNOWN: 296b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 29718707219SMatthias Ringwald sm_request_pairing(att_server->connection.con_handle); 298b06f2579SMatthias Ringwald return 0; 299b06f2579SMatthias Ringwald case AUTHORIZATION_PENDING: 300b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 301b06f2579SMatthias Ringwald return 0; 302b06f2579SMatthias Ringwald default: 303b06f2579SMatthias Ringwald break; 304b06f2579SMatthias Ringwald } 305b06f2579SMatthias Ringwald } 306b06f2579SMatthias Ringwald 30718707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 308b06f2579SMatthias Ringwald if (att_response_size == 0) { 309b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 310b06f2579SMatthias Ringwald return 0; 311b06f2579SMatthias Ringwald } 312b06f2579SMatthias Ringwald 31318707219SMatthias Ringwald l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size); 314b06f2579SMatthias Ringwald 315b06f2579SMatthias Ringwald // notify client about MTU exchange result 316b06f2579SMatthias Ringwald if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){ 31718707219SMatthias Ringwald att_emit_mtu_event(att_server->connection.con_handle, att_server->connection.mtu); 318b06f2579SMatthias Ringwald } 319b06f2579SMatthias Ringwald return 1; 320b06f2579SMatthias Ringwald } 321b06f2579SMatthias Ringwald 32218707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server){ 32318707219SMatthias Ringwald switch (att_server->state){ 3243deb3ec6SMatthias Ringwald case ATT_SERVER_REQUEST_RECEIVED: 3257a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE 32618707219SMatthias Ringwald if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){ 3273deb3ec6SMatthias Ringwald log_info("ATT Signed Write!"); 3283deb3ec6SMatthias Ringwald if (!sm_cmac_ready()) { 3293deb3ec6SMatthias Ringwald log_info("ATT Signed Write, sm_cmac engine not ready. Abort"); 33018707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 3313deb3ec6SMatthias Ringwald return; 3323deb3ec6SMatthias Ringwald } 33318707219SMatthias Ringwald if (att_server->request_size < (3 + 12)) { 3343deb3ec6SMatthias Ringwald log_info("ATT Signed Write, request to short. Abort."); 33518707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 3363deb3ec6SMatthias Ringwald return; 3373deb3ec6SMatthias Ringwald } 33818707219SMatthias Ringwald if (att_server->ir_lookup_active){ 3393deb3ec6SMatthias Ringwald return; 3403deb3ec6SMatthias Ringwald } 34118707219SMatthias Ringwald if (att_server->ir_le_device_db_index < 0){ 3423deb3ec6SMatthias Ringwald log_info("ATT Signed Write, CSRK not available"); 34318707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 3443deb3ec6SMatthias Ringwald return; 3453deb3ec6SMatthias Ringwald } 3463deb3ec6SMatthias Ringwald 3473deb3ec6SMatthias Ringwald // check counter 34818707219SMatthias Ringwald uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12); 34918707219SMatthias Ringwald uint32_t counter_db = le_device_db_remote_counter_get(att_server->ir_le_device_db_index); 3503deb3ec6SMatthias Ringwald log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet); 3513deb3ec6SMatthias Ringwald if (counter_packet < counter_db){ 3523deb3ec6SMatthias Ringwald log_info("ATT Signed Write, db reports higher counter, abort"); 35318707219SMatthias Ringwald att_server->state = ATT_SERVER_IDLE; 3543deb3ec6SMatthias Ringwald return; 3553deb3ec6SMatthias Ringwald } 3563deb3ec6SMatthias Ringwald 3573deb3ec6SMatthias Ringwald // signature is { sequence counter, secure hash } 3583deb3ec6SMatthias Ringwald sm_key_t csrk; 35918707219SMatthias Ringwald le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk); 36018707219SMatthias Ringwald att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION; 3613deb3ec6SMatthias Ringwald log_info("Orig Signature: "); 36218707219SMatthias Ringwald log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8); 36318707219SMatthias Ringwald uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1); 36418707219SMatthias 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); 3653deb3ec6SMatthias Ringwald return; 3663deb3ec6SMatthias Ringwald } 3677a766ebfSMatthias Ringwald #endif 368b06f2579SMatthias Ringwald // move on 36918707219SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 37018707219SMatthias Ringwald att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle); 371b06f2579SMatthias Ringwald break; 37288a48dd8SMatthias Ringwald 3733deb3ec6SMatthias Ringwald default: 3743deb3ec6SMatthias Ringwald break; 3753deb3ec6SMatthias Ringwald } 3763deb3ec6SMatthias Ringwald } 3773deb3ec6SMatthias Ringwald 3787eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){ 379b06f2579SMatthias Ringwald 380b06f2579SMatthias Ringwald // NOTE: we get l2cap fixed channel instead of con_handle 381b06f2579SMatthias Ringwald 38218707219SMatthias Ringwald btstack_linked_list_iterator_t it; 38318707219SMatthias Ringwald hci_connections_get_iterator(&it); 38418707219SMatthias Ringwald while(btstack_linked_list_iterator_has_next(&it)){ 38518707219SMatthias Ringwald hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it); 38618707219SMatthias Ringwald att_server_t * att_server = &connection->att_server; 38718707219SMatthias Ringwald if (att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED){ 38818707219SMatthias Ringwald int sent = att_server_process_validated_request(att_server); 389bb38f057SMatthias Ringwald if (sent && (att_client_waiting_for_can_send || !btstack_linked_list_empty(&can_send_now_clients))){ 39018707219SMatthias Ringwald att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle); 3913deb3ec6SMatthias Ringwald return; 3923deb3ec6SMatthias Ringwald } 3933deb3ec6SMatthias Ringwald } 39418707219SMatthias Ringwald } 3953deb3ec6SMatthias Ringwald 396bb38f057SMatthias Ringwald while (!btstack_linked_list_empty(&can_send_now_clients)){ 397bb38f057SMatthias Ringwald // handle first client 398bb38f057SMatthias Ringwald btstack_context_callback_registration_t * client = (btstack_context_callback_registration_t*) can_send_now_clients; 39918707219SMatthias Ringwald hci_con_handle_t con_handle = (uintptr_t) client->context; 400bb38f057SMatthias Ringwald btstack_linked_list_remove(&can_send_now_clients, (btstack_linked_item_t *) client); 401bb38f057SMatthias Ringwald client->callback(client->context); 402bb38f057SMatthias Ringwald 403bb38f057SMatthias Ringwald // request again if needed 40418707219SMatthias Ringwald if (!att_dispatch_server_can_send_now(con_handle)){ 405bb38f057SMatthias Ringwald if (!btstack_linked_list_empty(&can_send_now_clients) || att_client_waiting_for_can_send){ 40618707219SMatthias Ringwald att_dispatch_server_request_can_send_now_event(con_handle); 407bb38f057SMatthias Ringwald } 408bb38f057SMatthias Ringwald return; 409bb38f057SMatthias Ringwald } 410bb38f057SMatthias Ringwald } 411bb38f057SMatthias Ringwald 412b06f2579SMatthias Ringwald if (att_client_waiting_for_can_send){ 413b06f2579SMatthias Ringwald att_client_waiting_for_can_send = 0; 414b06f2579SMatthias Ringwald att_emit_can_send_now_event(); 4153deb3ec6SMatthias Ringwald } 4163deb3ec6SMatthias Ringwald } 4173deb3ec6SMatthias Ringwald 41888a48dd8SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){ 41918707219SMatthias Ringwald 42018707219SMatthias Ringwald att_server_t * att_server; 42118707219SMatthias Ringwald 42288a48dd8SMatthias Ringwald switch (packet_type){ 42388a48dd8SMatthias Ringwald 42488a48dd8SMatthias Ringwald case HCI_EVENT_PACKET: 42588a48dd8SMatthias Ringwald if (packet[0] != L2CAP_EVENT_CAN_SEND_NOW) break; 4264d2be802SMatthias Ringwald att_server_handle_can_send_now(); 427372d62c4SMatthias Ringwald break; 4283deb3ec6SMatthias Ringwald 429372d62c4SMatthias Ringwald case ATT_DATA_PACKET: 43018707219SMatthias Ringwald log_info("ATT Packet, handle 0x%04x", handle); 43118707219SMatthias Ringwald att_server = att_server_for_handle(handle); 43218707219SMatthias Ringwald if (!att_server) break; 43318707219SMatthias Ringwald 4343deb3ec6SMatthias Ringwald // handle value indication confirms 43518707219SMatthias Ringwald if (packet[0] == ATT_HANDLE_VALUE_CONFIRMATION && att_server->value_indication_handle){ 43618707219SMatthias Ringwald btstack_run_loop_remove_timer(&att_server->value_indication_timer); 43718707219SMatthias Ringwald uint16_t att_handle = att_server->value_indication_handle; 43818707219SMatthias Ringwald att_server->value_indication_handle = 0; 43918707219SMatthias Ringwald att_handle_value_indication_notify_client(0, att_server->connection.con_handle, att_handle); 4403deb3ec6SMatthias Ringwald return; 4413deb3ec6SMatthias Ringwald } 4423deb3ec6SMatthias Ringwald 4436428eb5aSMatthias Ringwald // directly process command 4446428eb5aSMatthias Ringwald // note: signed write cannot be handled directly as authentication needs to be verified 4456428eb5aSMatthias Ringwald if (packet[0] == ATT_WRITE_COMMAND){ 44618707219SMatthias Ringwald att_handle_request(&att_server->connection, packet, size, 0); 4476428eb5aSMatthias Ringwald return; 4486428eb5aSMatthias Ringwald } 4496428eb5aSMatthias Ringwald 4503deb3ec6SMatthias Ringwald // check size 45118707219SMatthias Ringwald if (size > sizeof(att_server->request_buffer)) { 45218707219SMatthias 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)); 4536428eb5aSMatthias Ringwald return; 4546428eb5aSMatthias Ringwald } 4553deb3ec6SMatthias Ringwald 4563deb3ec6SMatthias Ringwald // last request still in processing? 45718707219SMatthias Ringwald if (att_server->state != ATT_SERVER_IDLE){ 45818707219SMatthias Ringwald log_info("att_packet_handler: skipping att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state); 4596428eb5aSMatthias Ringwald return; 4606428eb5aSMatthias Ringwald } 4613deb3ec6SMatthias Ringwald 4623deb3ec6SMatthias Ringwald // store request 46318707219SMatthias Ringwald att_server->state = ATT_SERVER_REQUEST_RECEIVED; 46418707219SMatthias Ringwald att_server->request_size = size; 46518707219SMatthias Ringwald memcpy(att_server->request_buffer, packet, size); 4663deb3ec6SMatthias Ringwald 46718707219SMatthias Ringwald att_run_for_context(att_server); 468372d62c4SMatthias Ringwald break; 469372d62c4SMatthias Ringwald } 4703deb3ec6SMatthias Ringwald } 4713deb3ec6SMatthias Ringwald 472*bf78aac6SMatthias Ringwald // --------------------- 473*bf78aac6SMatthias Ringwald // persistent CCC writes 474*bf78aac6SMatthias Ringwald // TLV value format: le_device_index(8), attribute_handle(16), value (8) 475*bf78aac6SMatthias Ringwald static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){ 476*bf78aac6SMatthias Ringwald return 'B' << 24 | 'T' << 16 | 'C' << 8 | index; 477*bf78aac6SMatthias Ringwald } 478*bf78aac6SMatthias Ringwald 479*bf78aac6SMatthias Ringwald static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){ 480*bf78aac6SMatthias Ringwald // lookup att_server instance 481*bf78aac6SMatthias Ringwald att_server_t * att_server = att_server_for_handle(con_handle); 482*bf78aac6SMatthias Ringwald if (!att_server) return; 483*bf78aac6SMatthias Ringwald int le_device_index = att_server->ir_le_device_db_index; 484*bf78aac6SMatthias 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); 485*bf78aac6SMatthias Ringwald // check if bonded 486*bf78aac6SMatthias Ringwald if (le_device_index < 0) return; 487*bf78aac6SMatthias Ringwald // get btstack_tlv 488*bf78aac6SMatthias Ringwald const btstack_tlv_t * tlv_impl = NULL; 489*bf78aac6SMatthias Ringwald void * tlv_context; 490*bf78aac6SMatthias Ringwald btstack_tlv_get_instance(&tlv_impl, &tlv_context); 491*bf78aac6SMatthias Ringwald if (!tlv_impl) return; 492*bf78aac6SMatthias Ringwald // update ccc tag 493*bf78aac6SMatthias Ringwald int index; 494*bf78aac6SMatthias Ringwald uint8_t buffer[4]; 495*bf78aac6SMatthias Ringwald int free_index = -1; 496*bf78aac6SMatthias Ringwald for (index=0;index<GATT_SERVER_NUM_PERSISTENT_CCC;index++){ 497*bf78aac6SMatthias Ringwald uint32_t tag = att_server_persistent_ccc_tag_for_index(index); 498*bf78aac6SMatthias Ringwald int len = tlv_impl->get_tag(tlv_context, tag, buffer, sizeof(buffer)); 499*bf78aac6SMatthias Ringwald if (len == 0){ 500*bf78aac6SMatthias Ringwald free_index = index; 501*bf78aac6SMatthias Ringwald continue; 502*bf78aac6SMatthias Ringwald } 503*bf78aac6SMatthias Ringwald if (len != sizeof(buffer)) continue; 504*bf78aac6SMatthias Ringwald if (buffer[0] != le_device_index) continue; 505*bf78aac6SMatthias Ringwald if (little_endian_read_16(buffer, 1) != att_handle) continue; 506*bf78aac6SMatthias Ringwald // update 507*bf78aac6SMatthias Ringwald buffer[3] = value; 508*bf78aac6SMatthias Ringwald tlv_impl->store_tag(tlv_context, tag, buffer, sizeof(buffer)); 509*bf78aac6SMatthias Ringwald return; 510*bf78aac6SMatthias Ringwald } 511*bf78aac6SMatthias Ringwald if (free_index < 0) return; 512*bf78aac6SMatthias Ringwald // store ccc tag 513*bf78aac6SMatthias Ringwald buffer[0] = le_device_index; 514*bf78aac6SMatthias Ringwald little_endian_store_16(buffer, 1, att_handle); 515*bf78aac6SMatthias Ringwald buffer[3] = value; 516*bf78aac6SMatthias Ringwald uint32_t tag = att_server_persistent_ccc_tag_for_index(free_index); 517*bf78aac6SMatthias Ringwald tlv_impl->store_tag(tlv_context, tag, buffer, sizeof(buffer)); 518*bf78aac6SMatthias Ringwald } 519*bf78aac6SMatthias Ringwald 520*bf78aac6SMatthias Ringwald // persistent CCC writes 521*bf78aac6SMatthias Ringwald // --------------------- 5223d71c7a4SMatthias Ringwald 5233d71c7a4SMatthias Ringwald // gatt service management 5243d71c7a4SMatthias Ringwald static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){ 5253d71c7a4SMatthias Ringwald btstack_linked_list_iterator_t it; 5263d71c7a4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 5273d71c7a4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 5283d71c7a4SMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 5293d71c7a4SMatthias Ringwald if (handler->start_handle > handle) continue; 5303d71c7a4SMatthias Ringwald if (handler->end_handle < handle) continue; 5313d71c7a4SMatthias Ringwald return handler; 5323d71c7a4SMatthias Ringwald } 5333d71c7a4SMatthias Ringwald return NULL; 5343d71c7a4SMatthias Ringwald } 5353d71c7a4SMatthias Ringwald static att_read_callback_t att_server_read_callback_for_handle(uint16_t handle){ 5363d71c7a4SMatthias Ringwald att_service_handler_t * handler = att_service_handler_for_handle(handle); 5373d71c7a4SMatthias Ringwald if (handler) return handler->read_callback; 5383d71c7a4SMatthias Ringwald return att_server_client_read_callback; 5393d71c7a4SMatthias Ringwald } 5403d71c7a4SMatthias Ringwald 5413d71c7a4SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){ 5423d71c7a4SMatthias Ringwald att_service_handler_t * handler = att_service_handler_for_handle(handle); 5433d71c7a4SMatthias Ringwald if (handler) return handler->write_callback; 5443d71c7a4SMatthias Ringwald return att_server_client_write_callback; 5453d71c7a4SMatthias Ringwald } 5463d71c7a4SMatthias Ringwald 5473d71c7a4SMatthias Ringwald static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){ 5483d71c7a4SMatthias Ringwald // notify all callbacks 5493d71c7a4SMatthias Ringwald btstack_linked_list_iterator_t it; 5503d71c7a4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 5513d71c7a4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 5523d71c7a4SMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 5533d71c7a4SMatthias Ringwald if (!handler->write_callback) continue; 5543d71c7a4SMatthias Ringwald (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0); 5553d71c7a4SMatthias Ringwald } 5563d71c7a4SMatthias Ringwald if (!att_server_client_write_callback) return; 5573d71c7a4SMatthias Ringwald (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0); 5583d71c7a4SMatthias Ringwald } 5593d71c7a4SMatthias Ringwald 5603d71c7a4SMatthias Ringwald // returns first reported error or 0 5613d71c7a4SMatthias Ringwald static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){ 5623d71c7a4SMatthias Ringwald btstack_linked_list_iterator_t it; 5633d71c7a4SMatthias Ringwald btstack_linked_list_iterator_init(&it, &service_handlers); 5643d71c7a4SMatthias Ringwald while (btstack_linked_list_iterator_has_next(&it)){ 5653d71c7a4SMatthias Ringwald att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it); 5663d71c7a4SMatthias Ringwald if (!handler->write_callback) continue; 5673d71c7a4SMatthias Ringwald uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0); 5683d71c7a4SMatthias Ringwald if (error_code) return error_code; 5693d71c7a4SMatthias Ringwald } 5703d71c7a4SMatthias Ringwald if (!att_server_client_write_callback) return 0; 5713d71c7a4SMatthias Ringwald return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0); 5723d71c7a4SMatthias Ringwald } 5733d71c7a4SMatthias Ringwald 5743d71c7a4SMatthias 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){ 5753d71c7a4SMatthias Ringwald att_read_callback_t callback = att_server_read_callback_for_handle(attribute_handle); 5763d71c7a4SMatthias Ringwald return (*callback)(con_handle, attribute_handle, offset, buffer, buffer_size); 5773d71c7a4SMatthias Ringwald } 5783d71c7a4SMatthias Ringwald 5793d71c7a4SMatthias 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){ 5803d71c7a4SMatthias Ringwald switch (transaction_mode){ 5813d71c7a4SMatthias Ringwald case ATT_TRANSACTION_MODE_VALIDATE: 5823d71c7a4SMatthias Ringwald return att_validate_prepared_write(con_handle); 5833d71c7a4SMatthias Ringwald case ATT_TRANSACTION_MODE_EXECUTE: 5843d71c7a4SMatthias Ringwald case ATT_TRANSACTION_MODE_CANCEL: 5853d71c7a4SMatthias Ringwald att_notify_write_callbacks(con_handle, transaction_mode); 5863d71c7a4SMatthias Ringwald return 0; 5873d71c7a4SMatthias Ringwald default: 5883d71c7a4SMatthias Ringwald break; 5893d71c7a4SMatthias Ringwald } 5903d71c7a4SMatthias Ringwald att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle); 5913d71c7a4SMatthias Ringwald 592*bf78aac6SMatthias Ringwald // track CCC writes 593*bf78aac6SMatthias Ringwald if (att_is_persistent_ccc(attribute_handle) && offset == 0 && buffer_size == 2){ 594*bf78aac6SMatthias Ringwald att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0)); 595*bf78aac6SMatthias Ringwald } 596*bf78aac6SMatthias Ringwald 5973d71c7a4SMatthias Ringwald return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size); 5983d71c7a4SMatthias Ringwald } 5993d71c7a4SMatthias Ringwald 6003d71c7a4SMatthias Ringwald /** 6013d71c7a4SMatthias Ringwald * @brief register read/write callbacks for specific handle range 6023d71c7a4SMatthias Ringwald * @param att_service_handler_t 6033d71c7a4SMatthias Ringwald */ 6043d71c7a4SMatthias Ringwald void att_server_register_service_handler(att_service_handler_t * handler){ 6053d71c7a4SMatthias Ringwald if (att_service_handler_for_handle(handler->start_handle) || 6063d71c7a4SMatthias Ringwald att_service_handler_for_handle(handler->end_handle)){ 6073d71c7a4SMatthias Ringwald log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle); 6083d71c7a4SMatthias Ringwald return; 6093d71c7a4SMatthias Ringwald } 6103d71c7a4SMatthias Ringwald btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler); 6113d71c7a4SMatthias Ringwald } 6123d71c7a4SMatthias Ringwald 6133deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){ 6143deb3ec6SMatthias Ringwald 6153d71c7a4SMatthias Ringwald // store callbacks 6163d71c7a4SMatthias Ringwald att_server_client_read_callback = read_callback; 6173d71c7a4SMatthias Ringwald att_server_client_write_callback = write_callback; 6183d71c7a4SMatthias Ringwald 6191a389cdcSMatthias Ringwald // register for HCI Events 620d9a7306aSMatthias Ringwald hci_event_callback_registration.callback = &att_event_packet_handler; 6211a389cdcSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 6221a389cdcSMatthias Ringwald 623406722e4SMatthias Ringwald // register for SM events 624d9a7306aSMatthias Ringwald sm_event_callback_registration.callback = &att_event_packet_handler; 625406722e4SMatthias Ringwald sm_add_event_handler(&sm_event_callback_registration); 6263deb3ec6SMatthias Ringwald 6271a389cdcSMatthias Ringwald // and L2CAP ATT Server PDUs 6283deb3ec6SMatthias Ringwald att_dispatch_register_server(att_packet_handler); 6293deb3ec6SMatthias Ringwald 6303deb3ec6SMatthias Ringwald att_set_db(db); 6313d71c7a4SMatthias Ringwald att_set_read_callback(att_server_read_callback); 6323d71c7a4SMatthias Ringwald att_set_write_callback(att_server_write_callback); 6333deb3ec6SMatthias Ringwald 6343deb3ec6SMatthias Ringwald } 6353deb3ec6SMatthias Ringwald 6363deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){ 6373deb3ec6SMatthias Ringwald att_client_packet_handler = handler; 6383deb3ec6SMatthias Ringwald } 6393deb3ec6SMatthias Ringwald 640c141988cSMatthias Ringwald int att_server_can_send_packet_now(hci_con_handle_t con_handle){ 64118707219SMatthias Ringwald return att_dispatch_server_can_send_now(con_handle); 6421b96b007SMatthias Ringwald } 6431b96b007SMatthias Ringwald 644c141988cSMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){ 645d9d23054SMatthias Ringwald log_debug("att_server_request_can_send_now_event 0x%04x", con_handle); 6461b96b007SMatthias Ringwald att_client_waiting_for_can_send = 1; 64718707219SMatthias Ringwald att_dispatch_server_request_can_send_now_event(con_handle); 6483deb3ec6SMatthias Ringwald } 6493deb3ec6SMatthias Ringwald 650bb38f057SMatthias Ringwald void att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){ 651bb38f057SMatthias Ringwald // check if can send already 652bb38f057SMatthias Ringwald if (att_dispatch_server_can_send_now(con_handle)){ 653bb38f057SMatthias Ringwald callback_registration->callback(callback_registration->context); 654bb38f057SMatthias Ringwald return; 655bb38f057SMatthias Ringwald } 65618707219SMatthias Ringwald callback_registration->context = (void*)(uintptr_t) con_handle; 657bb38f057SMatthias Ringwald btstack_linked_list_add_tail(&can_send_now_clients, (btstack_linked_item_t*) callback_registration); 658bb38f057SMatthias Ringwald att_dispatch_server_request_can_send_now_event(con_handle); 659bb38f057SMatthias Ringwald } 660bb38f057SMatthias Ringwald 66118707219SMatthias Ringwald 662c141988cSMatthias Ringwald int att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){ 66318707219SMatthias Ringwald att_server_t * att_server = att_server_for_handle(con_handle); 66418707219SMatthias Ringwald if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 66518707219SMatthias Ringwald 66618707219SMatthias Ringwald if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL; 6673deb3ec6SMatthias Ringwald 6683deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 6693deb3ec6SMatthias Ringwald uint8_t * packet_buffer = l2cap_get_outgoing_buffer(); 67018707219SMatthias Ringwald uint16_t size = att_prepare_handle_value_notification(&att_server->connection, attribute_handle, value, value_len, packet_buffer); 67118707219SMatthias Ringwald return l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); 6723deb3ec6SMatthias Ringwald } 6733deb3ec6SMatthias Ringwald 674c141988cSMatthias Ringwald int att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){ 67518707219SMatthias Ringwald att_server_t * att_server = att_server_for_handle(con_handle); 67618707219SMatthias Ringwald if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER; 67718707219SMatthias Ringwald 678eaac31e8SMatthias Ringwald if (att_server->value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS; 67918707219SMatthias Ringwald if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL; 6803deb3ec6SMatthias Ringwald 6813deb3ec6SMatthias Ringwald // track indication 68218707219SMatthias Ringwald att_server->value_indication_handle = attribute_handle; 68318707219SMatthias Ringwald btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout); 68418707219SMatthias Ringwald btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS); 68518707219SMatthias Ringwald btstack_run_loop_add_timer(&att_server->value_indication_timer); 6863deb3ec6SMatthias Ringwald 6873deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 6883deb3ec6SMatthias Ringwald uint8_t * packet_buffer = l2cap_get_outgoing_buffer(); 68918707219SMatthias Ringwald uint16_t size = att_prepare_handle_value_indication(&att_server->connection, attribute_handle, value, value_len, packet_buffer); 69018707219SMatthias Ringwald l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); 6913deb3ec6SMatthias Ringwald return 0; 6923deb3ec6SMatthias Ringwald } 693