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 383deb3ec6SMatthias Ringwald 393deb3ec6SMatthias Ringwald // 403deb3ec6SMatthias Ringwald // ATT Server Globals 413deb3ec6SMatthias Ringwald // 423deb3ec6SMatthias Ringwald 433deb3ec6SMatthias Ringwald #include <stdint.h> 443deb3ec6SMatthias Ringwald #include <stdio.h> 453deb3ec6SMatthias Ringwald #include <stdlib.h> 463deb3ec6SMatthias Ringwald #include <string.h> 473deb3ec6SMatthias Ringwald #include <inttypes.h> 483deb3ec6SMatthias Ringwald 497907f069SMatthias Ringwald #include "btstack_config.h" 503deb3ec6SMatthias Ringwald 5159c6af15SMatthias Ringwald #include "att_dispatch.h" 5259c6af15SMatthias Ringwald #include "ble/att_db.h" 5359c6af15SMatthias Ringwald #include "ble/att_server.h" 5459c6af15SMatthias Ringwald #include "ble/core.h" 5559c6af15SMatthias Ringwald #include "ble/le_device_db.h" 5659c6af15SMatthias Ringwald #include "ble/sm.h" 5716ece135SMatthias Ringwald #include "btstack_debug.h" 580e2df43fSMatthias Ringwald #include "btstack_event.h" 593deb3ec6SMatthias Ringwald #include "btstack_memory.h" 600e2df43fSMatthias Ringwald #include "btstack_run_loop.h" 6159c6af15SMatthias Ringwald #include "gap.h" 623deb3ec6SMatthias Ringwald #include "hci.h" 633deb3ec6SMatthias Ringwald #include "hci_dump.h" 643deb3ec6SMatthias Ringwald #include "l2cap.h" 653deb3ec6SMatthias Ringwald 663deb3ec6SMatthias Ringwald static void att_run(void); 673deb3ec6SMatthias Ringwald 6899c58ad0SMatthias Ringwald // max ATT request matches L2CAP PDU -- allow to use smaller buffer 6999c58ad0SMatthias Ringwald #ifndef ATT_REQUEST_BUFFER_SIZE 7099c58ad0SMatthias Ringwald #define ATT_REQUEST_BUFFER_SIZE HCI_ACL_PAYLOAD_SIZE 7199c58ad0SMatthias Ringwald #endif 7299c58ad0SMatthias Ringwald 733deb3ec6SMatthias Ringwald typedef enum { 743deb3ec6SMatthias Ringwald ATT_SERVER_IDLE, 753deb3ec6SMatthias Ringwald ATT_SERVER_REQUEST_RECEIVED, 763deb3ec6SMatthias Ringwald ATT_SERVER_W4_SIGNED_WRITE_VALIDATION, 773deb3ec6SMatthias Ringwald ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED, 783deb3ec6SMatthias Ringwald } att_server_state_t; 793deb3ec6SMatthias Ringwald 803deb3ec6SMatthias Ringwald static att_connection_t att_connection; 813deb3ec6SMatthias Ringwald static att_server_state_t att_server_state; 823deb3ec6SMatthias Ringwald 833deb3ec6SMatthias Ringwald static uint8_t att_client_addr_type; 843deb3ec6SMatthias Ringwald static bd_addr_t att_client_address; 851b96b007SMatthias Ringwald static uint8_t att_client_waiting_for_can_send; 863deb3ec6SMatthias Ringwald static uint16_t att_request_size = 0; 8799c58ad0SMatthias Ringwald static uint8_t att_request_buffer[ATT_REQUEST_BUFFER_SIZE]; 883deb3ec6SMatthias Ringwald 893deb3ec6SMatthias Ringwald static int att_ir_le_device_db_index = -1; 903deb3ec6SMatthias Ringwald static int att_ir_lookup_active = 0; 913deb3ec6SMatthias Ringwald 923deb3ec6SMatthias Ringwald static int att_handle_value_indication_handle = 0; 93ec820d77SMatthias Ringwald static btstack_timer_source_t att_handle_value_indication_timer; 941a389cdcSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration; 95406722e4SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration; 963deb3ec6SMatthias Ringwald static btstack_packet_handler_t att_client_packet_handler = NULL; 973deb3ec6SMatthias Ringwald 983deb3ec6SMatthias Ringwald static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){ 993deb3ec6SMatthias Ringwald if (!att_client_packet_handler) return; 1003deb3ec6SMatthias Ringwald 1013deb3ec6SMatthias Ringwald uint8_t event[7]; 1023deb3ec6SMatthias Ringwald int pos = 0; 1035611a760SMatthias Ringwald event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE; 1043deb3ec6SMatthias Ringwald event[pos++] = sizeof(event) - 2; 1053deb3ec6SMatthias Ringwald event[pos++] = status; 106f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, client_handle); 1073deb3ec6SMatthias Ringwald pos += 2; 108f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, attribute_handle); 1093deb3ec6SMatthias Ringwald pos += 2; 1103deb3ec6SMatthias Ringwald (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 1113deb3ec6SMatthias Ringwald } 1123deb3ec6SMatthias Ringwald 113fc64f94aSMatthias Ringwald static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){ 1143deb3ec6SMatthias Ringwald if (!att_client_packet_handler) return; 1153deb3ec6SMatthias Ringwald 1163deb3ec6SMatthias Ringwald uint8_t event[6]; 1173deb3ec6SMatthias Ringwald int pos = 0; 1185611a760SMatthias Ringwald event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE; 1193deb3ec6SMatthias Ringwald event[pos++] = sizeof(event) - 2; 120fc64f94aSMatthias Ringwald little_endian_store_16(event, pos, con_handle); 1213deb3ec6SMatthias Ringwald pos += 2; 122f8fbdce0SMatthias Ringwald little_endian_store_16(event, pos, mtu); 1233deb3ec6SMatthias Ringwald pos += 2; 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 1341b96b007SMatthias Ringwald static void att_server_notify_can_send(void){ 1351b96b007SMatthias Ringwald if (att_connection.con_handle == 0) return; 1361b96b007SMatthias Ringwald if (!att_client_waiting_for_can_send) return; 1371b96b007SMatthias Ringwald if (!att_dispatch_server_can_send_now(att_connection.con_handle)) return; 1381b96b007SMatthias Ringwald 1391b96b007SMatthias Ringwald att_client_waiting_for_can_send = 0; 1401b96b007SMatthias Ringwald att_emit_can_send_now_event(); 1411b96b007SMatthias Ringwald } 1421b96b007SMatthias Ringwald 143ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){ 1443deb3ec6SMatthias Ringwald uint16_t att_handle = att_handle_value_indication_handle; 1453deb3ec6SMatthias Ringwald att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_connection.con_handle, att_handle); 1463deb3ec6SMatthias Ringwald } 1473deb3ec6SMatthias Ringwald 1483deb3ec6SMatthias Ringwald static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1493deb3ec6SMatthias Ringwald 1503deb3ec6SMatthias Ringwald bd_addr_t event_address; 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: 1593deb3ec6SMatthias Ringwald // store connection info 1603deb3ec6SMatthias Ringwald att_client_addr_type = packet[7]; 161724d70a2SMatthias Ringwald reverse_bd_addr(&packet[8], att_client_address); 1623deb3ec6SMatthias Ringwald // reset connection properties 163f8fbdce0SMatthias Ringwald att_connection.con_handle = little_endian_read_16(packet, 4); 1643deb3ec6SMatthias Ringwald att_connection.mtu = ATT_DEFAULT_MTU; 1653deb3ec6SMatthias Ringwald att_connection.max_mtu = l2cap_max_le_mtu(); 16699c58ad0SMatthias Ringwald if (att_connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){ 16799c58ad0SMatthias Ringwald att_connection.max_mtu = ATT_REQUEST_BUFFER_SIZE; 16899c58ad0SMatthias Ringwald } 1693deb3ec6SMatthias Ringwald att_connection.encryption_key_size = 0; 1703deb3ec6SMatthias Ringwald att_connection.authenticated = 0; 1713deb3ec6SMatthias Ringwald att_connection.authorized = 0; 1723deb3ec6SMatthias Ringwald break; 1733deb3ec6SMatthias Ringwald 1743deb3ec6SMatthias Ringwald default: 1753deb3ec6SMatthias Ringwald break; 1763deb3ec6SMatthias Ringwald } 1773deb3ec6SMatthias Ringwald break; 1783deb3ec6SMatthias Ringwald 1793deb3ec6SMatthias Ringwald case HCI_EVENT_ENCRYPTION_CHANGE: 1803deb3ec6SMatthias Ringwald case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE: 1813deb3ec6SMatthias Ringwald // check handle 182f8fbdce0SMatthias Ringwald if (att_connection.con_handle != little_endian_read_16(packet, 3)) break; 1833deb3ec6SMatthias Ringwald att_connection.encryption_key_size = sm_encryption_key_size(att_connection.con_handle); 1843deb3ec6SMatthias Ringwald att_connection.authenticated = sm_authenticated(att_connection.con_handle); 1853deb3ec6SMatthias Ringwald break; 1863deb3ec6SMatthias Ringwald 1873deb3ec6SMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 1883deb3ec6SMatthias Ringwald att_clear_transaction_queue(&att_connection); 1893deb3ec6SMatthias Ringwald att_connection.con_handle = 0; 1903deb3ec6SMatthias Ringwald att_handle_value_indication_handle = 0; // reset error state 1913deb3ec6SMatthias Ringwald // restart advertising if we have been connected before 1923deb3ec6SMatthias Ringwald // -> avoid sending advertise enable a second time before command complete was received 1933deb3ec6SMatthias Ringwald att_server_state = ATT_SERVER_IDLE; 1943deb3ec6SMatthias Ringwald break; 1953deb3ec6SMatthias Ringwald 1965611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_STARTED: 1975611a760SMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED"); 1983deb3ec6SMatthias Ringwald att_ir_lookup_active = 1; 1993deb3ec6SMatthias Ringwald break; 2005611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED: 2013deb3ec6SMatthias Ringwald att_ir_lookup_active = 0; 202f8fbdce0SMatthias Ringwald att_ir_le_device_db_index = little_endian_read_16(packet, 11); 2035611a760SMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED id %u", att_ir_le_device_db_index); 2043deb3ec6SMatthias Ringwald att_run(); 2053deb3ec6SMatthias Ringwald break; 2065611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_FAILED: 2075611a760SMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED"); 2083deb3ec6SMatthias Ringwald att_ir_lookup_active = 0; 2093deb3ec6SMatthias Ringwald att_ir_le_device_db_index = -1; 2103deb3ec6SMatthias Ringwald att_run(); 2113deb3ec6SMatthias Ringwald break; 2125611a760SMatthias Ringwald case SM_EVENT_AUTHORIZATION_RESULT: { 2133deb3ec6SMatthias Ringwald if (packet[4] != att_client_addr_type) break; 214724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], event_address); 2153deb3ec6SMatthias Ringwald if (memcmp(event_address, att_client_address, 6) != 0) break; 2163deb3ec6SMatthias Ringwald att_connection.authorized = packet[11]; 2173deb3ec6SMatthias Ringwald att_run(); 2183deb3ec6SMatthias Ringwald break; 2193deb3ec6SMatthias Ringwald } 2203deb3ec6SMatthias Ringwald default: 2213deb3ec6SMatthias Ringwald break; 2223deb3ec6SMatthias Ringwald } 22365b44ffdSMatthias Ringwald break; 22465b44ffdSMatthias Ringwald default: 22565b44ffdSMatthias Ringwald break; 2263deb3ec6SMatthias Ringwald } 2273deb3ec6SMatthias Ringwald } 2283deb3ec6SMatthias Ringwald 2293deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){ 2303deb3ec6SMatthias Ringwald 2313deb3ec6SMatthias Ringwald if (att_server_state != ATT_SERVER_W4_SIGNED_WRITE_VALIDATION) return; 2323deb3ec6SMatthias Ringwald 2333deb3ec6SMatthias Ringwald uint8_t hash_flipped[8]; 2349c80e4ccSMatthias Ringwald reverse_64(hash, hash_flipped); 2353deb3ec6SMatthias Ringwald if (memcmp(hash_flipped, &att_request_buffer[att_request_size-8], 8)){ 2363deb3ec6SMatthias Ringwald log_info("ATT Signed Write, invalid signature"); 2373deb3ec6SMatthias Ringwald att_server_state = ATT_SERVER_IDLE; 2383deb3ec6SMatthias Ringwald return; 2393deb3ec6SMatthias Ringwald } 2403deb3ec6SMatthias Ringwald log_info("ATT Signed Write, valid signature"); 2413deb3ec6SMatthias Ringwald 2423deb3ec6SMatthias Ringwald // update sequence number 243f8fbdce0SMatthias Ringwald uint32_t counter_packet = little_endian_read_32(att_request_buffer, att_request_size-12); 2443deb3ec6SMatthias Ringwald le_device_db_remote_counter_set(att_ir_le_device_db_index, counter_packet+1); 2453deb3ec6SMatthias Ringwald att_server_state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 2463deb3ec6SMatthias Ringwald att_run(); 2473deb3ec6SMatthias Ringwald } 2483deb3ec6SMatthias Ringwald 249*88a48dd8SMatthias Ringwald #if 0 250*88a48dd8SMatthias Ringwald static int att_server_ready_to_send(att_connection_t * connection){ 251*88a48dd8SMatthias Ringwald } 252*88a48dd8SMatthias Ringwald #endif 253*88a48dd8SMatthias Ringwald 2543deb3ec6SMatthias Ringwald static void att_run(void){ 2553deb3ec6SMatthias Ringwald switch (att_server_state){ 2563deb3ec6SMatthias Ringwald case ATT_SERVER_IDLE: 2573deb3ec6SMatthias Ringwald case ATT_SERVER_W4_SIGNED_WRITE_VALIDATION: 2583deb3ec6SMatthias Ringwald return; 2593deb3ec6SMatthias Ringwald case ATT_SERVER_REQUEST_RECEIVED: 2603deb3ec6SMatthias Ringwald if (att_request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){ 2613deb3ec6SMatthias Ringwald log_info("ATT Signed Write!"); 2623deb3ec6SMatthias Ringwald if (!sm_cmac_ready()) { 2633deb3ec6SMatthias Ringwald log_info("ATT Signed Write, sm_cmac engine not ready. Abort"); 2643deb3ec6SMatthias Ringwald att_server_state = ATT_SERVER_IDLE; 2653deb3ec6SMatthias Ringwald return; 2663deb3ec6SMatthias Ringwald } 2673deb3ec6SMatthias Ringwald if (att_request_size < (3 + 12)) { 2683deb3ec6SMatthias Ringwald log_info("ATT Signed Write, request to short. Abort."); 2693deb3ec6SMatthias Ringwald att_server_state = ATT_SERVER_IDLE; 2703deb3ec6SMatthias Ringwald return; 2713deb3ec6SMatthias Ringwald } 2723deb3ec6SMatthias Ringwald if (att_ir_lookup_active){ 2733deb3ec6SMatthias Ringwald return; 2743deb3ec6SMatthias Ringwald } 2753deb3ec6SMatthias Ringwald if (att_ir_le_device_db_index < 0){ 2763deb3ec6SMatthias Ringwald log_info("ATT Signed Write, CSRK not available"); 2773deb3ec6SMatthias Ringwald att_server_state = ATT_SERVER_IDLE; 2783deb3ec6SMatthias Ringwald return; 2793deb3ec6SMatthias Ringwald } 2803deb3ec6SMatthias Ringwald 2813deb3ec6SMatthias Ringwald // check counter 282f8fbdce0SMatthias Ringwald uint32_t counter_packet = little_endian_read_32(att_request_buffer, att_request_size-12); 2833deb3ec6SMatthias Ringwald uint32_t counter_db = le_device_db_remote_counter_get(att_ir_le_device_db_index); 2843deb3ec6SMatthias Ringwald log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet); 2853deb3ec6SMatthias Ringwald if (counter_packet < counter_db){ 2863deb3ec6SMatthias Ringwald log_info("ATT Signed Write, db reports higher counter, abort"); 2873deb3ec6SMatthias Ringwald att_server_state = ATT_SERVER_IDLE; 2883deb3ec6SMatthias Ringwald return; 2893deb3ec6SMatthias Ringwald } 2903deb3ec6SMatthias Ringwald 2913deb3ec6SMatthias Ringwald // signature is { sequence counter, secure hash } 2923deb3ec6SMatthias Ringwald sm_key_t csrk; 2933deb3ec6SMatthias Ringwald le_device_db_remote_csrk_get(att_ir_le_device_db_index, csrk); 2943deb3ec6SMatthias Ringwald att_server_state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION; 2953deb3ec6SMatthias Ringwald log_info("Orig Signature: "); 2968314c363SMatthias Ringwald log_info_hexdump( &att_request_buffer[att_request_size-8], 8); 297f8fbdce0SMatthias Ringwald uint16_t attribute_handle = little_endian_read_16(att_request_buffer, 1); 2983deb3ec6SMatthias Ringwald sm_cmac_start(csrk, att_request_buffer[0], attribute_handle, att_request_size - 15, &att_request_buffer[3], counter_packet, att_signed_write_handle_cmac_result); 2993deb3ec6SMatthias Ringwald return; 3003deb3ec6SMatthias Ringwald } 3013deb3ec6SMatthias Ringwald // NOTE: fall through for regular commands 3023deb3ec6SMatthias Ringwald 3033deb3ec6SMatthias Ringwald case ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED: 304*88a48dd8SMatthias Ringwald 30519448c0bSMatthias Ringwald if (!att_dispatch_server_can_send_now(att_connection.con_handle)) return; 3063deb3ec6SMatthias Ringwald 3073deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 3083deb3ec6SMatthias Ringwald uint8_t * att_response_buffer = l2cap_get_outgoing_buffer(); 3093deb3ec6SMatthias Ringwald uint16_t att_response_size = att_handle_request(&att_connection, att_request_buffer, att_request_size, att_response_buffer); 3103deb3ec6SMatthias Ringwald 3113deb3ec6SMatthias Ringwald // intercept "insufficient authorization" for authenticated connections to allow for user authorization 3123deb3ec6SMatthias Ringwald if ((att_response_size >= 4) 3133deb3ec6SMatthias Ringwald && (att_response_buffer[0] == ATT_ERROR_RESPONSE) 3143deb3ec6SMatthias Ringwald && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION) 3153deb3ec6SMatthias Ringwald && (att_connection.authenticated)){ 3163deb3ec6SMatthias Ringwald 3173deb3ec6SMatthias Ringwald switch (sm_authorization_state(att_connection.con_handle)){ 3183deb3ec6SMatthias Ringwald case AUTHORIZATION_UNKNOWN: 3193deb3ec6SMatthias Ringwald l2cap_release_packet_buffer(); 3203deb3ec6SMatthias Ringwald sm_request_pairing(att_connection.con_handle); 3213deb3ec6SMatthias Ringwald return; 3223deb3ec6SMatthias Ringwald case AUTHORIZATION_PENDING: 3233deb3ec6SMatthias Ringwald l2cap_release_packet_buffer(); 3243deb3ec6SMatthias Ringwald return; 3253deb3ec6SMatthias Ringwald default: 3263deb3ec6SMatthias Ringwald break; 3273deb3ec6SMatthias Ringwald } 3283deb3ec6SMatthias Ringwald } 3293deb3ec6SMatthias Ringwald 3303deb3ec6SMatthias Ringwald att_server_state = ATT_SERVER_IDLE; 3313deb3ec6SMatthias Ringwald if (att_response_size == 0) { 3323deb3ec6SMatthias Ringwald l2cap_release_packet_buffer(); 3333deb3ec6SMatthias Ringwald return; 3343deb3ec6SMatthias Ringwald } 3353deb3ec6SMatthias Ringwald 3363deb3ec6SMatthias Ringwald l2cap_send_prepared_connectionless(att_connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size); 3373deb3ec6SMatthias Ringwald 3383deb3ec6SMatthias Ringwald // notify client about MTU exchange result 3393deb3ec6SMatthias Ringwald if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){ 3403deb3ec6SMatthias Ringwald att_emit_mtu_event(att_connection.con_handle, att_connection.mtu); 3413deb3ec6SMatthias Ringwald } 3423deb3ec6SMatthias Ringwald 3433deb3ec6SMatthias Ringwald break; 3443deb3ec6SMatthias Ringwald } 3453deb3ec6SMatthias Ringwald } 3463deb3ec6SMatthias Ringwald 347*88a48dd8SMatthias Ringwald static void att_server_handle_can_send_now(hci_con_handle_t con_handle){ 348372d62c4SMatthias Ringwald att_run(); 3493c97b242SMatthias Ringwald // if we cannot send now, we'll get another l2cap can send now soon 3503c97b242SMatthias Ringwald att_server_notify_can_send(); 351*88a48dd8SMatthias Ringwald } 352*88a48dd8SMatthias Ringwald 353*88a48dd8SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){ 354*88a48dd8SMatthias Ringwald switch (packet_type){ 355*88a48dd8SMatthias Ringwald 356*88a48dd8SMatthias Ringwald case HCI_EVENT_PACKET: 357*88a48dd8SMatthias Ringwald if (packet[0] != L2CAP_EVENT_CAN_SEND_NOW) break; 358*88a48dd8SMatthias Ringwald att_server_handle_can_send_now(l2cap_event_can_send_now_get_local_cid(packet)); 359372d62c4SMatthias Ringwald break; 3603deb3ec6SMatthias Ringwald 361372d62c4SMatthias Ringwald case ATT_DATA_PACKET: 3623deb3ec6SMatthias Ringwald // handle value indication confirms 3633deb3ec6SMatthias Ringwald if (packet[0] == ATT_HANDLE_VALUE_CONFIRMATION && att_handle_value_indication_handle){ 364528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&att_handle_value_indication_timer); 3653deb3ec6SMatthias Ringwald uint16_t att_handle = att_handle_value_indication_handle; 3663deb3ec6SMatthias Ringwald att_handle_value_indication_handle = 0; 3673deb3ec6SMatthias Ringwald att_handle_value_indication_notify_client(0, att_connection.con_handle, att_handle); 3683deb3ec6SMatthias Ringwald return; 3693deb3ec6SMatthias Ringwald } 3703deb3ec6SMatthias Ringwald 3716428eb5aSMatthias Ringwald // directly process command 3726428eb5aSMatthias Ringwald // note: signed write cannot be handled directly as authentication needs to be verified 3736428eb5aSMatthias Ringwald if (packet[0] == ATT_WRITE_COMMAND){ 3746428eb5aSMatthias Ringwald att_handle_request(&att_connection, packet, size, 0); 3756428eb5aSMatthias Ringwald return; 3766428eb5aSMatthias Ringwald } 3776428eb5aSMatthias Ringwald 3783deb3ec6SMatthias Ringwald // check size 3796428eb5aSMatthias Ringwald if (size > sizeof(att_request_buffer)) { 3806428eb5aSMatthias Ringwald log_info("att_packet_handler: dropping att pdu 0x%02x as size %u > att_request_buffer %u", packet[0], size, (int) sizeof(att_request_buffer)); 3816428eb5aSMatthias Ringwald return; 3826428eb5aSMatthias Ringwald } 3833deb3ec6SMatthias Ringwald 3843deb3ec6SMatthias Ringwald // last request still in processing? 3856428eb5aSMatthias Ringwald if (att_server_state != ATT_SERVER_IDLE){ 3866428eb5aSMatthias Ringwald log_info("att_packet_handler: skipping att pdu 0x%02x as server not idle (state %u)", packet[0], att_server_state); 3876428eb5aSMatthias Ringwald return; 3886428eb5aSMatthias Ringwald } 3893deb3ec6SMatthias Ringwald 3903deb3ec6SMatthias Ringwald // store request 3913deb3ec6SMatthias Ringwald att_server_state = ATT_SERVER_REQUEST_RECEIVED; 3923deb3ec6SMatthias Ringwald att_request_size = size; 3933deb3ec6SMatthias Ringwald memcpy(att_request_buffer, packet, size); 3943deb3ec6SMatthias Ringwald 3953deb3ec6SMatthias Ringwald att_run(); 396372d62c4SMatthias Ringwald break; 397372d62c4SMatthias Ringwald } 3983deb3ec6SMatthias Ringwald } 3993deb3ec6SMatthias Ringwald 4003deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){ 4013deb3ec6SMatthias Ringwald 4021a389cdcSMatthias Ringwald // register for HCI Events 403d9a7306aSMatthias Ringwald hci_event_callback_registration.callback = &att_event_packet_handler; 4041a389cdcSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 4051a389cdcSMatthias Ringwald 406406722e4SMatthias Ringwald // register for SM events 407d9a7306aSMatthias Ringwald sm_event_callback_registration.callback = &att_event_packet_handler; 408406722e4SMatthias Ringwald sm_add_event_handler(&sm_event_callback_registration); 4093deb3ec6SMatthias Ringwald 4101a389cdcSMatthias Ringwald // and L2CAP ATT Server PDUs 4113deb3ec6SMatthias Ringwald att_dispatch_register_server(att_packet_handler); 4123deb3ec6SMatthias Ringwald 4133deb3ec6SMatthias Ringwald att_server_state = ATT_SERVER_IDLE; 4143deb3ec6SMatthias Ringwald att_set_db(db); 4153deb3ec6SMatthias Ringwald att_set_read_callback(read_callback); 4163deb3ec6SMatthias Ringwald att_set_write_callback(write_callback); 4173deb3ec6SMatthias Ringwald 4183deb3ec6SMatthias Ringwald } 4193deb3ec6SMatthias Ringwald 4203deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){ 4213deb3ec6SMatthias Ringwald att_client_packet_handler = handler; 4223deb3ec6SMatthias Ringwald } 4233deb3ec6SMatthias Ringwald 424c141988cSMatthias Ringwald // NOTE: con_handle is ignored for now as only a single connection is supported 425c141988cSMatthias Ringwald // TODO: support multiple connections 426c141988cSMatthias Ringwald 427c141988cSMatthias Ringwald int att_server_can_send_packet_now(hci_con_handle_t con_handle){ 4283deb3ec6SMatthias Ringwald if (att_connection.con_handle == 0) return 0; 4290b9d7e78SMatthias Ringwald return att_dispatch_server_can_send_now(att_connection.con_handle); 4301b96b007SMatthias Ringwald } 4311b96b007SMatthias Ringwald 432c141988cSMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){ 4331b96b007SMatthias Ringwald att_client_waiting_for_can_send = 1; 4340b9d7e78SMatthias Ringwald att_dispatch_server_request_can_send_now_event(att_connection.con_handle); 4353deb3ec6SMatthias Ringwald } 4363deb3ec6SMatthias Ringwald 437c141988cSMatthias Ringwald int att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){ 43819448c0bSMatthias Ringwald if (!att_dispatch_server_can_send_now(att_connection.con_handle)) return BTSTACK_ACL_BUFFERS_FULL; 4393deb3ec6SMatthias Ringwald 4403deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 4413deb3ec6SMatthias Ringwald uint8_t * packet_buffer = l2cap_get_outgoing_buffer(); 442fc64f94aSMatthias Ringwald uint16_t size = att_prepare_handle_value_notification(&att_connection, attribute_handle, value, value_len, packet_buffer); 4433deb3ec6SMatthias Ringwald return l2cap_send_prepared_connectionless(att_connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); 4443deb3ec6SMatthias Ringwald } 4453deb3ec6SMatthias Ringwald 446c141988cSMatthias Ringwald int att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){ 4473deb3ec6SMatthias Ringwald if (att_handle_value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PORGRESS; 44819448c0bSMatthias Ringwald if (!att_dispatch_server_can_send_now(att_connection.con_handle)) return BTSTACK_ACL_BUFFERS_FULL; 4493deb3ec6SMatthias Ringwald 4503deb3ec6SMatthias Ringwald // track indication 451fc64f94aSMatthias Ringwald att_handle_value_indication_handle = attribute_handle; 452528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&att_handle_value_indication_timer, att_handle_value_indication_timeout); 453528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&att_handle_value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS); 454528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&att_handle_value_indication_timer); 4553deb3ec6SMatthias Ringwald 4563deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 4573deb3ec6SMatthias Ringwald uint8_t * packet_buffer = l2cap_get_outgoing_buffer(); 458fc64f94aSMatthias Ringwald uint16_t size = att_prepare_handle_value_indication(&att_connection, attribute_handle, value, value_len, packet_buffer); 4593deb3ec6SMatthias Ringwald l2cap_send_prepared_connectionless(att_connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); 4603deb3ec6SMatthias Ringwald return 0; 4613deb3ec6SMatthias Ringwald } 462