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 134ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){ 1353deb3ec6SMatthias Ringwald uint16_t att_handle = att_handle_value_indication_handle; 1363deb3ec6SMatthias Ringwald att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_connection.con_handle, att_handle); 1373deb3ec6SMatthias Ringwald } 1383deb3ec6SMatthias Ringwald 1393deb3ec6SMatthias Ringwald static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1403deb3ec6SMatthias Ringwald 1413deb3ec6SMatthias Ringwald bd_addr_t event_address; 1423deb3ec6SMatthias Ringwald switch (packet_type) { 1433deb3ec6SMatthias Ringwald 1443deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 1450e2df43fSMatthias Ringwald switch (hci_event_packet_get_type(packet)) { 1463deb3ec6SMatthias Ringwald 1473deb3ec6SMatthias Ringwald case HCI_EVENT_LE_META: 1483deb3ec6SMatthias Ringwald switch (packet[2]) { 1493deb3ec6SMatthias Ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 1503deb3ec6SMatthias Ringwald // store connection info 1513deb3ec6SMatthias Ringwald att_client_addr_type = packet[7]; 152724d70a2SMatthias Ringwald reverse_bd_addr(&packet[8], att_client_address); 1533deb3ec6SMatthias Ringwald // reset connection properties 154f8fbdce0SMatthias Ringwald att_connection.con_handle = little_endian_read_16(packet, 4); 1553deb3ec6SMatthias Ringwald att_connection.mtu = ATT_DEFAULT_MTU; 1563deb3ec6SMatthias Ringwald att_connection.max_mtu = l2cap_max_le_mtu(); 15799c58ad0SMatthias Ringwald if (att_connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){ 15899c58ad0SMatthias Ringwald att_connection.max_mtu = ATT_REQUEST_BUFFER_SIZE; 15999c58ad0SMatthias Ringwald } 1603deb3ec6SMatthias Ringwald att_connection.encryption_key_size = 0; 1613deb3ec6SMatthias Ringwald att_connection.authenticated = 0; 1623deb3ec6SMatthias Ringwald att_connection.authorized = 0; 1633deb3ec6SMatthias Ringwald break; 1643deb3ec6SMatthias Ringwald 1653deb3ec6SMatthias Ringwald default: 1663deb3ec6SMatthias Ringwald break; 1673deb3ec6SMatthias Ringwald } 1683deb3ec6SMatthias Ringwald break; 1693deb3ec6SMatthias Ringwald 1703deb3ec6SMatthias Ringwald case HCI_EVENT_ENCRYPTION_CHANGE: 1713deb3ec6SMatthias Ringwald case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE: 1723deb3ec6SMatthias Ringwald // check handle 173f8fbdce0SMatthias Ringwald if (att_connection.con_handle != little_endian_read_16(packet, 3)) break; 1743deb3ec6SMatthias Ringwald att_connection.encryption_key_size = sm_encryption_key_size(att_connection.con_handle); 1753deb3ec6SMatthias Ringwald att_connection.authenticated = sm_authenticated(att_connection.con_handle); 1763deb3ec6SMatthias Ringwald break; 1773deb3ec6SMatthias Ringwald 1783deb3ec6SMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 1793deb3ec6SMatthias Ringwald att_clear_transaction_queue(&att_connection); 1803deb3ec6SMatthias Ringwald att_connection.con_handle = 0; 1813deb3ec6SMatthias Ringwald att_handle_value_indication_handle = 0; // reset error state 1823deb3ec6SMatthias Ringwald // restart advertising if we have been connected before 1833deb3ec6SMatthias Ringwald // -> avoid sending advertise enable a second time before command complete was received 1843deb3ec6SMatthias Ringwald att_server_state = ATT_SERVER_IDLE; 1853deb3ec6SMatthias Ringwald break; 1863deb3ec6SMatthias Ringwald 1875611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_STARTED: 1885611a760SMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED"); 1893deb3ec6SMatthias Ringwald att_ir_lookup_active = 1; 1903deb3ec6SMatthias Ringwald break; 1915611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED: 1923deb3ec6SMatthias Ringwald att_ir_lookup_active = 0; 193f8fbdce0SMatthias Ringwald att_ir_le_device_db_index = little_endian_read_16(packet, 11); 1945611a760SMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED id %u", att_ir_le_device_db_index); 1953deb3ec6SMatthias Ringwald att_run(); 1963deb3ec6SMatthias Ringwald break; 1975611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_FAILED: 1985611a760SMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED"); 1993deb3ec6SMatthias Ringwald att_ir_lookup_active = 0; 2003deb3ec6SMatthias Ringwald att_ir_le_device_db_index = -1; 2013deb3ec6SMatthias Ringwald att_run(); 2023deb3ec6SMatthias Ringwald break; 2035611a760SMatthias Ringwald case SM_EVENT_AUTHORIZATION_RESULT: { 2043deb3ec6SMatthias Ringwald if (packet[4] != att_client_addr_type) break; 205724d70a2SMatthias Ringwald reverse_bd_addr(&packet[5], event_address); 2063deb3ec6SMatthias Ringwald if (memcmp(event_address, att_client_address, 6) != 0) break; 2073deb3ec6SMatthias Ringwald att_connection.authorized = packet[11]; 208b06f2579SMatthias Ringwald att_dispatch_server_request_can_send_now_event(att_connection.con_handle); 2093deb3ec6SMatthias Ringwald break; 2103deb3ec6SMatthias Ringwald } 2113deb3ec6SMatthias Ringwald default: 2123deb3ec6SMatthias Ringwald break; 2133deb3ec6SMatthias Ringwald } 21465b44ffdSMatthias Ringwald break; 21565b44ffdSMatthias Ringwald default: 21665b44ffdSMatthias Ringwald break; 2173deb3ec6SMatthias Ringwald } 2183deb3ec6SMatthias Ringwald } 2193deb3ec6SMatthias Ringwald 2203deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){ 2213deb3ec6SMatthias Ringwald 2223deb3ec6SMatthias Ringwald if (att_server_state != ATT_SERVER_W4_SIGNED_WRITE_VALIDATION) return; 2233deb3ec6SMatthias Ringwald 2243deb3ec6SMatthias Ringwald uint8_t hash_flipped[8]; 2259c80e4ccSMatthias Ringwald reverse_64(hash, hash_flipped); 2263deb3ec6SMatthias Ringwald if (memcmp(hash_flipped, &att_request_buffer[att_request_size-8], 8)){ 2273deb3ec6SMatthias Ringwald log_info("ATT Signed Write, invalid signature"); 2283deb3ec6SMatthias Ringwald att_server_state = ATT_SERVER_IDLE; 2293deb3ec6SMatthias Ringwald return; 2303deb3ec6SMatthias Ringwald } 2313deb3ec6SMatthias Ringwald log_info("ATT Signed Write, valid signature"); 2323deb3ec6SMatthias Ringwald 2333deb3ec6SMatthias Ringwald // update sequence number 234f8fbdce0SMatthias Ringwald uint32_t counter_packet = little_endian_read_32(att_request_buffer, att_request_size-12); 2353deb3ec6SMatthias Ringwald le_device_db_remote_counter_set(att_ir_le_device_db_index, counter_packet+1); 2363deb3ec6SMatthias Ringwald att_server_state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 237b06f2579SMatthias Ringwald att_dispatch_server_request_can_send_now_event(att_connection.con_handle); 2383deb3ec6SMatthias Ringwald } 2393deb3ec6SMatthias Ringwald 24088a48dd8SMatthias Ringwald #if 0 24188a48dd8SMatthias Ringwald static int att_server_ready_to_send(att_connection_t * connection){ 24288a48dd8SMatthias Ringwald } 24388a48dd8SMatthias Ringwald #endif 24488a48dd8SMatthias Ringwald 245b06f2579SMatthias Ringwald // pre: att_server_state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED 246b06f2579SMatthias Ringwald // pre: can send now 247b06f2579SMatthias Ringwald // returns: 1 if packet was sent 248b06f2579SMatthias Ringwald static int att_server_process_validated_request(hci_con_handle_t con_handle){ 249b06f2579SMatthias Ringwald 250b06f2579SMatthias Ringwald l2cap_reserve_packet_buffer(); 251b06f2579SMatthias Ringwald uint8_t * att_response_buffer = l2cap_get_outgoing_buffer(); 252b06f2579SMatthias Ringwald uint16_t att_response_size = att_handle_request(&att_connection, att_request_buffer, att_request_size, att_response_buffer); 253b06f2579SMatthias Ringwald 254b06f2579SMatthias Ringwald // intercept "insufficient authorization" for authenticated connections to allow for user authorization 255b06f2579SMatthias Ringwald if ((att_response_size >= 4) 256b06f2579SMatthias Ringwald && (att_response_buffer[0] == ATT_ERROR_RESPONSE) 257b06f2579SMatthias Ringwald && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION) 258b06f2579SMatthias Ringwald && (att_connection.authenticated)){ 259b06f2579SMatthias Ringwald 260b06f2579SMatthias Ringwald switch (sm_authorization_state(att_connection.con_handle)){ 261b06f2579SMatthias Ringwald case AUTHORIZATION_UNKNOWN: 262b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 263b06f2579SMatthias Ringwald sm_request_pairing(att_connection.con_handle); 264b06f2579SMatthias Ringwald return 0; 265b06f2579SMatthias Ringwald case AUTHORIZATION_PENDING: 266b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 267b06f2579SMatthias Ringwald return 0; 268b06f2579SMatthias Ringwald default: 269b06f2579SMatthias Ringwald break; 270b06f2579SMatthias Ringwald } 271b06f2579SMatthias Ringwald } 272b06f2579SMatthias Ringwald 273b06f2579SMatthias Ringwald att_server_state = ATT_SERVER_IDLE; 274b06f2579SMatthias Ringwald if (att_response_size == 0) { 275b06f2579SMatthias Ringwald l2cap_release_packet_buffer(); 276b06f2579SMatthias Ringwald return 0; 277b06f2579SMatthias Ringwald } 278b06f2579SMatthias Ringwald 279b06f2579SMatthias Ringwald l2cap_send_prepared_connectionless(att_connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size); 280b06f2579SMatthias Ringwald 281b06f2579SMatthias Ringwald // notify client about MTU exchange result 282b06f2579SMatthias Ringwald if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){ 283b06f2579SMatthias Ringwald att_emit_mtu_event(att_connection.con_handle, att_connection.mtu); 284b06f2579SMatthias Ringwald } 285b06f2579SMatthias Ringwald return 1; 286b06f2579SMatthias Ringwald } 287b06f2579SMatthias Ringwald 2883deb3ec6SMatthias Ringwald static void att_run(void){ 2893deb3ec6SMatthias Ringwald switch (att_server_state){ 2903deb3ec6SMatthias Ringwald case ATT_SERVER_REQUEST_RECEIVED: 2913deb3ec6SMatthias Ringwald if (att_request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){ 2923deb3ec6SMatthias Ringwald log_info("ATT Signed Write!"); 2933deb3ec6SMatthias Ringwald if (!sm_cmac_ready()) { 2943deb3ec6SMatthias Ringwald log_info("ATT Signed Write, sm_cmac engine not ready. Abort"); 2953deb3ec6SMatthias Ringwald att_server_state = ATT_SERVER_IDLE; 2963deb3ec6SMatthias Ringwald return; 2973deb3ec6SMatthias Ringwald } 2983deb3ec6SMatthias Ringwald if (att_request_size < (3 + 12)) { 2993deb3ec6SMatthias Ringwald log_info("ATT Signed Write, request to short. Abort."); 3003deb3ec6SMatthias Ringwald att_server_state = ATT_SERVER_IDLE; 3013deb3ec6SMatthias Ringwald return; 3023deb3ec6SMatthias Ringwald } 3033deb3ec6SMatthias Ringwald if (att_ir_lookup_active){ 3043deb3ec6SMatthias Ringwald return; 3053deb3ec6SMatthias Ringwald } 3063deb3ec6SMatthias Ringwald if (att_ir_le_device_db_index < 0){ 3073deb3ec6SMatthias Ringwald log_info("ATT Signed Write, CSRK not available"); 3083deb3ec6SMatthias Ringwald att_server_state = ATT_SERVER_IDLE; 3093deb3ec6SMatthias Ringwald return; 3103deb3ec6SMatthias Ringwald } 3113deb3ec6SMatthias Ringwald 3123deb3ec6SMatthias Ringwald // check counter 313f8fbdce0SMatthias Ringwald uint32_t counter_packet = little_endian_read_32(att_request_buffer, att_request_size-12); 3143deb3ec6SMatthias Ringwald uint32_t counter_db = le_device_db_remote_counter_get(att_ir_le_device_db_index); 3153deb3ec6SMatthias Ringwald log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet); 3163deb3ec6SMatthias Ringwald if (counter_packet < counter_db){ 3173deb3ec6SMatthias Ringwald log_info("ATT Signed Write, db reports higher counter, abort"); 3183deb3ec6SMatthias Ringwald att_server_state = ATT_SERVER_IDLE; 3193deb3ec6SMatthias Ringwald return; 3203deb3ec6SMatthias Ringwald } 3213deb3ec6SMatthias Ringwald 3223deb3ec6SMatthias Ringwald // signature is { sequence counter, secure hash } 3233deb3ec6SMatthias Ringwald sm_key_t csrk; 3243deb3ec6SMatthias Ringwald le_device_db_remote_csrk_get(att_ir_le_device_db_index, csrk); 3253deb3ec6SMatthias Ringwald att_server_state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION; 3263deb3ec6SMatthias Ringwald log_info("Orig Signature: "); 3278314c363SMatthias Ringwald log_info_hexdump( &att_request_buffer[att_request_size-8], 8); 328f8fbdce0SMatthias Ringwald uint16_t attribute_handle = little_endian_read_16(att_request_buffer, 1); 3293deb3ec6SMatthias 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); 3303deb3ec6SMatthias Ringwald return; 3313deb3ec6SMatthias Ringwald } 3323deb3ec6SMatthias Ringwald 333b06f2579SMatthias Ringwald // move on 334b06f2579SMatthias Ringwald att_server_state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED; 335b06f2579SMatthias Ringwald att_dispatch_server_request_can_send_now_event(att_connection.con_handle); 336b06f2579SMatthias Ringwald break; 33788a48dd8SMatthias Ringwald 3383deb3ec6SMatthias Ringwald default: 3393deb3ec6SMatthias Ringwald break; 3403deb3ec6SMatthias Ringwald } 3413deb3ec6SMatthias Ringwald } 3423deb3ec6SMatthias Ringwald 343*7eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){ 344b06f2579SMatthias Ringwald 345b06f2579SMatthias Ringwald // NOTE: we get l2cap fixed channel instead of con_handle 346b06f2579SMatthias Ringwald // TODO: get con_handle 347b06f2579SMatthias Ringwald 348b06f2579SMatthias Ringwald if (att_server_state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED){ 3494d2be802SMatthias Ringwald int sent = att_server_process_validated_request(att_connection.con_handle); 350b06f2579SMatthias Ringwald if (sent && att_client_waiting_for_can_send){ 3514d2be802SMatthias Ringwald att_dispatch_server_request_can_send_now_event(att_connection.con_handle); 3523deb3ec6SMatthias Ringwald return; 3533deb3ec6SMatthias Ringwald } 3543deb3ec6SMatthias Ringwald } 3553deb3ec6SMatthias Ringwald 356b06f2579SMatthias Ringwald if (att_client_waiting_for_can_send){ 357b06f2579SMatthias Ringwald att_client_waiting_for_can_send = 0; 358b06f2579SMatthias Ringwald att_emit_can_send_now_event(); 3593deb3ec6SMatthias Ringwald } 3603deb3ec6SMatthias Ringwald } 3613deb3ec6SMatthias Ringwald 36288a48dd8SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){ 36388a48dd8SMatthias Ringwald switch (packet_type){ 36488a48dd8SMatthias Ringwald 36588a48dd8SMatthias Ringwald case HCI_EVENT_PACKET: 36688a48dd8SMatthias Ringwald if (packet[0] != L2CAP_EVENT_CAN_SEND_NOW) break; 3674d2be802SMatthias Ringwald att_server_handle_can_send_now(); 368372d62c4SMatthias Ringwald break; 3693deb3ec6SMatthias Ringwald 370372d62c4SMatthias Ringwald case ATT_DATA_PACKET: 3713deb3ec6SMatthias Ringwald // handle value indication confirms 3723deb3ec6SMatthias Ringwald if (packet[0] == ATT_HANDLE_VALUE_CONFIRMATION && att_handle_value_indication_handle){ 373528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&att_handle_value_indication_timer); 3743deb3ec6SMatthias Ringwald uint16_t att_handle = att_handle_value_indication_handle; 3753deb3ec6SMatthias Ringwald att_handle_value_indication_handle = 0; 3763deb3ec6SMatthias Ringwald att_handle_value_indication_notify_client(0, att_connection.con_handle, att_handle); 3773deb3ec6SMatthias Ringwald return; 3783deb3ec6SMatthias Ringwald } 3793deb3ec6SMatthias Ringwald 3806428eb5aSMatthias Ringwald // directly process command 3816428eb5aSMatthias Ringwald // note: signed write cannot be handled directly as authentication needs to be verified 3826428eb5aSMatthias Ringwald if (packet[0] == ATT_WRITE_COMMAND){ 3836428eb5aSMatthias Ringwald att_handle_request(&att_connection, packet, size, 0); 3846428eb5aSMatthias Ringwald return; 3856428eb5aSMatthias Ringwald } 3866428eb5aSMatthias Ringwald 3873deb3ec6SMatthias Ringwald // check size 3886428eb5aSMatthias Ringwald if (size > sizeof(att_request_buffer)) { 3896428eb5aSMatthias 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)); 3906428eb5aSMatthias Ringwald return; 3916428eb5aSMatthias Ringwald } 3923deb3ec6SMatthias Ringwald 3933deb3ec6SMatthias Ringwald // last request still in processing? 3946428eb5aSMatthias Ringwald if (att_server_state != ATT_SERVER_IDLE){ 3956428eb5aSMatthias Ringwald log_info("att_packet_handler: skipping att pdu 0x%02x as server not idle (state %u)", packet[0], att_server_state); 3966428eb5aSMatthias Ringwald return; 3976428eb5aSMatthias Ringwald } 3983deb3ec6SMatthias Ringwald 3993deb3ec6SMatthias Ringwald // store request 4003deb3ec6SMatthias Ringwald att_server_state = ATT_SERVER_REQUEST_RECEIVED; 4013deb3ec6SMatthias Ringwald att_request_size = size; 4023deb3ec6SMatthias Ringwald memcpy(att_request_buffer, packet, size); 4033deb3ec6SMatthias Ringwald 4043deb3ec6SMatthias Ringwald att_run(); 405372d62c4SMatthias Ringwald break; 406372d62c4SMatthias Ringwald } 4073deb3ec6SMatthias Ringwald } 4083deb3ec6SMatthias Ringwald 4093deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){ 4103deb3ec6SMatthias Ringwald 4111a389cdcSMatthias Ringwald // register for HCI Events 412d9a7306aSMatthias Ringwald hci_event_callback_registration.callback = &att_event_packet_handler; 4131a389cdcSMatthias Ringwald hci_add_event_handler(&hci_event_callback_registration); 4141a389cdcSMatthias Ringwald 415406722e4SMatthias Ringwald // register for SM events 416d9a7306aSMatthias Ringwald sm_event_callback_registration.callback = &att_event_packet_handler; 417406722e4SMatthias Ringwald sm_add_event_handler(&sm_event_callback_registration); 4183deb3ec6SMatthias Ringwald 4191a389cdcSMatthias Ringwald // and L2CAP ATT Server PDUs 4203deb3ec6SMatthias Ringwald att_dispatch_register_server(att_packet_handler); 4213deb3ec6SMatthias Ringwald 4223deb3ec6SMatthias Ringwald att_server_state = ATT_SERVER_IDLE; 4233deb3ec6SMatthias Ringwald att_set_db(db); 4243deb3ec6SMatthias Ringwald att_set_read_callback(read_callback); 4253deb3ec6SMatthias Ringwald att_set_write_callback(write_callback); 4263deb3ec6SMatthias Ringwald 4273deb3ec6SMatthias Ringwald } 4283deb3ec6SMatthias Ringwald 4293deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){ 4303deb3ec6SMatthias Ringwald att_client_packet_handler = handler; 4313deb3ec6SMatthias Ringwald } 4323deb3ec6SMatthias Ringwald 433c141988cSMatthias Ringwald // NOTE: con_handle is ignored for now as only a single connection is supported 434c141988cSMatthias Ringwald // TODO: support multiple connections 435c141988cSMatthias Ringwald 436c141988cSMatthias Ringwald int att_server_can_send_packet_now(hci_con_handle_t con_handle){ 4373deb3ec6SMatthias Ringwald if (att_connection.con_handle == 0) return 0; 4380b9d7e78SMatthias Ringwald return att_dispatch_server_can_send_now(att_connection.con_handle); 4391b96b007SMatthias Ringwald } 4401b96b007SMatthias Ringwald 441c141988cSMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){ 4421b96b007SMatthias Ringwald att_client_waiting_for_can_send = 1; 4430b9d7e78SMatthias Ringwald att_dispatch_server_request_can_send_now_event(att_connection.con_handle); 4443deb3ec6SMatthias Ringwald } 4453deb3ec6SMatthias Ringwald 446c141988cSMatthias Ringwald int att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){ 44719448c0bSMatthias Ringwald if (!att_dispatch_server_can_send_now(att_connection.con_handle)) return BTSTACK_ACL_BUFFERS_FULL; 4483deb3ec6SMatthias Ringwald 4493deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 4503deb3ec6SMatthias Ringwald uint8_t * packet_buffer = l2cap_get_outgoing_buffer(); 451fc64f94aSMatthias Ringwald uint16_t size = att_prepare_handle_value_notification(&att_connection, attribute_handle, value, value_len, packet_buffer); 4523deb3ec6SMatthias Ringwald return l2cap_send_prepared_connectionless(att_connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); 4533deb3ec6SMatthias Ringwald } 4543deb3ec6SMatthias Ringwald 455c141988cSMatthias Ringwald int att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){ 4563deb3ec6SMatthias Ringwald if (att_handle_value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PORGRESS; 45719448c0bSMatthias Ringwald if (!att_dispatch_server_can_send_now(att_connection.con_handle)) return BTSTACK_ACL_BUFFERS_FULL; 4583deb3ec6SMatthias Ringwald 4593deb3ec6SMatthias Ringwald // track indication 460fc64f94aSMatthias Ringwald att_handle_value_indication_handle = attribute_handle; 461528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&att_handle_value_indication_timer, att_handle_value_indication_timeout); 462528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&att_handle_value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS); 463528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&att_handle_value_indication_timer); 4643deb3ec6SMatthias Ringwald 4653deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 4663deb3ec6SMatthias Ringwald uint8_t * packet_buffer = l2cap_get_outgoing_buffer(); 467fc64f94aSMatthias Ringwald uint16_t size = att_prepare_handle_value_indication(&att_connection, attribute_handle, value, value_len, packet_buffer); 4683deb3ec6SMatthias Ringwald l2cap_send_prepared_connectionless(att_connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); 4693deb3ec6SMatthias Ringwald return 0; 4703deb3ec6SMatthias Ringwald } 471