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 5182636622SMatthias Ringwald #include "btstack_run_loop.h" 5216ece135SMatthias Ringwald #include "btstack_debug.h" 533deb3ec6SMatthias Ringwald #include "btstack_memory.h" 543deb3ec6SMatthias Ringwald #include "hci.h" 553deb3ec6SMatthias Ringwald #include "hci_dump.h" 563deb3ec6SMatthias Ringwald 573deb3ec6SMatthias Ringwald #include "l2cap.h" 583deb3ec6SMatthias Ringwald 593edc84c5SMatthias Ringwald #include "ble/sm.h" 603edc84c5SMatthias Ringwald #include "ble/att.h" 613deb3ec6SMatthias Ringwald #include "att_dispatch.h" 62f7a05cdaSMatthias Ringwald #include "gap.h" 633edc84c5SMatthias Ringwald #include "ble/le_device_db.h" 643deb3ec6SMatthias Ringwald 653edc84c5SMatthias Ringwald #include "ble/att_server.h" 663deb3ec6SMatthias Ringwald 673deb3ec6SMatthias Ringwald static void att_run(void); 683deb3ec6SMatthias Ringwald 6999c58ad0SMatthias Ringwald // max ATT request matches L2CAP PDU -- allow to use smaller buffer 7099c58ad0SMatthias Ringwald #ifndef ATT_REQUEST_BUFFER_SIZE 7199c58ad0SMatthias Ringwald #define ATT_REQUEST_BUFFER_SIZE HCI_ACL_PAYLOAD_SIZE 7299c58ad0SMatthias Ringwald #endif 7399c58ad0SMatthias Ringwald 743deb3ec6SMatthias Ringwald typedef enum { 753deb3ec6SMatthias Ringwald ATT_SERVER_IDLE, 763deb3ec6SMatthias Ringwald ATT_SERVER_REQUEST_RECEIVED, 773deb3ec6SMatthias Ringwald ATT_SERVER_W4_SIGNED_WRITE_VALIDATION, 783deb3ec6SMatthias Ringwald ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED, 793deb3ec6SMatthias Ringwald } att_server_state_t; 803deb3ec6SMatthias Ringwald 813deb3ec6SMatthias Ringwald static att_connection_t att_connection; 823deb3ec6SMatthias Ringwald static att_server_state_t att_server_state; 833deb3ec6SMatthias Ringwald 843deb3ec6SMatthias Ringwald static uint8_t att_client_addr_type; 853deb3ec6SMatthias Ringwald static bd_addr_t att_client_address; 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; 943deb3ec6SMatthias Ringwald 953deb3ec6SMatthias Ringwald static btstack_packet_handler_t att_client_packet_handler = NULL; 963deb3ec6SMatthias Ringwald 973deb3ec6SMatthias Ringwald static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){ 983deb3ec6SMatthias Ringwald 993deb3ec6SMatthias Ringwald if (!att_client_packet_handler) return; 1003deb3ec6SMatthias Ringwald 1013deb3ec6SMatthias Ringwald uint8_t event[7]; 1023deb3ec6SMatthias Ringwald int pos = 0; 103*5611a760SMatthias Ringwald event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE; 1043deb3ec6SMatthias Ringwald event[pos++] = sizeof(event) - 2; 1053deb3ec6SMatthias Ringwald event[pos++] = status; 1063deb3ec6SMatthias Ringwald bt_store_16(event, pos, client_handle); 1073deb3ec6SMatthias Ringwald pos += 2; 1083deb3ec6SMatthias Ringwald bt_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 1133deb3ec6SMatthias Ringwald static void att_emit_mtu_event(uint16_t handle, uint16_t mtu){ 1143deb3ec6SMatthias Ringwald 1153deb3ec6SMatthias Ringwald if (!att_client_packet_handler) return; 1163deb3ec6SMatthias Ringwald 1173deb3ec6SMatthias Ringwald uint8_t event[6]; 1183deb3ec6SMatthias Ringwald int pos = 0; 119*5611a760SMatthias Ringwald event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE; 1203deb3ec6SMatthias Ringwald event[pos++] = sizeof(event) - 2; 1213deb3ec6SMatthias Ringwald bt_store_16(event, pos, handle); 1223deb3ec6SMatthias Ringwald pos += 2; 1233deb3ec6SMatthias Ringwald bt_store_16(event, pos, mtu); 1243deb3ec6SMatthias Ringwald pos += 2; 1253deb3ec6SMatthias Ringwald (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event)); 1263deb3ec6SMatthias Ringwald } 1273deb3ec6SMatthias Ringwald 128ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){ 1293deb3ec6SMatthias Ringwald uint16_t att_handle = att_handle_value_indication_handle; 1303deb3ec6SMatthias Ringwald att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_connection.con_handle, att_handle); 1313deb3ec6SMatthias Ringwald } 1323deb3ec6SMatthias Ringwald 1333deb3ec6SMatthias Ringwald static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){ 1343deb3ec6SMatthias Ringwald 1353deb3ec6SMatthias Ringwald bd_addr_t event_address; 1363deb3ec6SMatthias Ringwald switch (packet_type) { 1373deb3ec6SMatthias Ringwald 1383deb3ec6SMatthias Ringwald case HCI_EVENT_PACKET: 1393deb3ec6SMatthias Ringwald switch (packet[0]) { 1403deb3ec6SMatthias Ringwald 1413deb3ec6SMatthias Ringwald case DAEMON_EVENT_HCI_PACKET_SENT: 1423deb3ec6SMatthias Ringwald att_run(); 1433deb3ec6SMatthias Ringwald break; 1443deb3ec6SMatthias Ringwald 1453deb3ec6SMatthias Ringwald case HCI_EVENT_LE_META: 1463deb3ec6SMatthias Ringwald switch (packet[2]) { 1473deb3ec6SMatthias Ringwald case HCI_SUBEVENT_LE_CONNECTION_COMPLETE: 1483deb3ec6SMatthias Ringwald // store connection info 1493deb3ec6SMatthias Ringwald att_client_addr_type = packet[7]; 1503deb3ec6SMatthias Ringwald bt_flip_addr(att_client_address, &packet[8]); 1513deb3ec6SMatthias Ringwald // reset connection properties 1523deb3ec6SMatthias Ringwald att_connection.con_handle = READ_BT_16(packet, 4); 1533deb3ec6SMatthias Ringwald att_connection.mtu = ATT_DEFAULT_MTU; 1543deb3ec6SMatthias Ringwald att_connection.max_mtu = l2cap_max_le_mtu(); 15599c58ad0SMatthias Ringwald if (att_connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){ 15699c58ad0SMatthias Ringwald att_connection.max_mtu = ATT_REQUEST_BUFFER_SIZE; 15799c58ad0SMatthias Ringwald } 1583deb3ec6SMatthias Ringwald att_connection.encryption_key_size = 0; 1593deb3ec6SMatthias Ringwald att_connection.authenticated = 0; 1603deb3ec6SMatthias Ringwald att_connection.authorized = 0; 1613deb3ec6SMatthias Ringwald break; 1623deb3ec6SMatthias Ringwald 1633deb3ec6SMatthias Ringwald default: 1643deb3ec6SMatthias Ringwald break; 1653deb3ec6SMatthias Ringwald } 1663deb3ec6SMatthias Ringwald break; 1673deb3ec6SMatthias Ringwald 1683deb3ec6SMatthias Ringwald case HCI_EVENT_ENCRYPTION_CHANGE: 1693deb3ec6SMatthias Ringwald case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE: 1703deb3ec6SMatthias Ringwald // check handle 1713deb3ec6SMatthias Ringwald if (att_connection.con_handle != READ_BT_16(packet, 3)) break; 1723deb3ec6SMatthias Ringwald att_connection.encryption_key_size = sm_encryption_key_size(att_connection.con_handle); 1733deb3ec6SMatthias Ringwald att_connection.authenticated = sm_authenticated(att_connection.con_handle); 1743deb3ec6SMatthias Ringwald break; 1753deb3ec6SMatthias Ringwald 1763deb3ec6SMatthias Ringwald case HCI_EVENT_DISCONNECTION_COMPLETE: 1773deb3ec6SMatthias Ringwald att_clear_transaction_queue(&att_connection); 1783deb3ec6SMatthias Ringwald att_connection.con_handle = 0; 1793deb3ec6SMatthias Ringwald att_handle_value_indication_handle = 0; // reset error state 1803deb3ec6SMatthias Ringwald // restart advertising if we have been connected before 1813deb3ec6SMatthias Ringwald // -> avoid sending advertise enable a second time before command complete was received 1823deb3ec6SMatthias Ringwald att_server_state = ATT_SERVER_IDLE; 1833deb3ec6SMatthias Ringwald break; 1843deb3ec6SMatthias Ringwald 185*5611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_STARTED: 186*5611a760SMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED"); 1873deb3ec6SMatthias Ringwald att_ir_lookup_active = 1; 1883deb3ec6SMatthias Ringwald break; 189*5611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED: 1903deb3ec6SMatthias Ringwald att_ir_lookup_active = 0; 1913deb3ec6SMatthias Ringwald att_ir_le_device_db_index = READ_BT_16(packet, 11); 192*5611a760SMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED id %u", att_ir_le_device_db_index); 1933deb3ec6SMatthias Ringwald att_run(); 1943deb3ec6SMatthias Ringwald break; 195*5611a760SMatthias Ringwald case SM_EVENT_IDENTITY_RESOLVING_FAILED: 196*5611a760SMatthias Ringwald log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED"); 1973deb3ec6SMatthias Ringwald att_ir_lookup_active = 0; 1983deb3ec6SMatthias Ringwald att_ir_le_device_db_index = -1; 1993deb3ec6SMatthias Ringwald att_run(); 2003deb3ec6SMatthias Ringwald break; 2013deb3ec6SMatthias Ringwald 202*5611a760SMatthias Ringwald case SM_EVENT_AUTHORIZATION_RESULT: { 2033deb3ec6SMatthias Ringwald if (packet[4] != att_client_addr_type) break; 2043deb3ec6SMatthias Ringwald bt_flip_addr(event_address, &packet[5]); 2053deb3ec6SMatthias Ringwald if (memcmp(event_address, att_client_address, 6) != 0) break; 2063deb3ec6SMatthias Ringwald att_connection.authorized = packet[11]; 2073deb3ec6SMatthias Ringwald att_run(); 2083deb3ec6SMatthias Ringwald break; 2093deb3ec6SMatthias Ringwald } 2103deb3ec6SMatthias Ringwald 2113deb3ec6SMatthias Ringwald default: 2123deb3ec6SMatthias Ringwald break; 2133deb3ec6SMatthias Ringwald } 2143deb3ec6SMatthias Ringwald } 2153deb3ec6SMatthias Ringwald if (att_client_packet_handler){ 2163deb3ec6SMatthias Ringwald att_client_packet_handler(packet_type, channel, packet, size); 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]; 2253deb3ec6SMatthias Ringwald swap64(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 2343deb3ec6SMatthias Ringwald uint32_t counter_packet = READ_BT_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; 2373deb3ec6SMatthias Ringwald att_run(); 2383deb3ec6SMatthias Ringwald } 2393deb3ec6SMatthias Ringwald 2403deb3ec6SMatthias Ringwald static void att_run(void){ 2413deb3ec6SMatthias Ringwald switch (att_server_state){ 2423deb3ec6SMatthias Ringwald case ATT_SERVER_IDLE: 2433deb3ec6SMatthias Ringwald case ATT_SERVER_W4_SIGNED_WRITE_VALIDATION: 2443deb3ec6SMatthias Ringwald return; 2453deb3ec6SMatthias Ringwald case ATT_SERVER_REQUEST_RECEIVED: 2463deb3ec6SMatthias Ringwald if (att_request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){ 2473deb3ec6SMatthias Ringwald log_info("ATT Signed Write!"); 2483deb3ec6SMatthias Ringwald if (!sm_cmac_ready()) { 2493deb3ec6SMatthias Ringwald log_info("ATT Signed Write, sm_cmac engine not ready. Abort"); 2503deb3ec6SMatthias Ringwald att_server_state = ATT_SERVER_IDLE; 2513deb3ec6SMatthias Ringwald return; 2523deb3ec6SMatthias Ringwald } 2533deb3ec6SMatthias Ringwald if (att_request_size < (3 + 12)) { 2543deb3ec6SMatthias Ringwald log_info("ATT Signed Write, request to short. Abort."); 2553deb3ec6SMatthias Ringwald att_server_state = ATT_SERVER_IDLE; 2563deb3ec6SMatthias Ringwald return; 2573deb3ec6SMatthias Ringwald } 2583deb3ec6SMatthias Ringwald if (att_ir_lookup_active){ 2593deb3ec6SMatthias Ringwald return; 2603deb3ec6SMatthias Ringwald } 2613deb3ec6SMatthias Ringwald if (att_ir_le_device_db_index < 0){ 2623deb3ec6SMatthias Ringwald log_info("ATT Signed Write, CSRK not available"); 2633deb3ec6SMatthias Ringwald att_server_state = ATT_SERVER_IDLE; 2643deb3ec6SMatthias Ringwald return; 2653deb3ec6SMatthias Ringwald } 2663deb3ec6SMatthias Ringwald 2673deb3ec6SMatthias Ringwald // check counter 2683deb3ec6SMatthias Ringwald uint32_t counter_packet = READ_BT_32(att_request_buffer, att_request_size-12); 2693deb3ec6SMatthias Ringwald uint32_t counter_db = le_device_db_remote_counter_get(att_ir_le_device_db_index); 2703deb3ec6SMatthias Ringwald log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet); 2713deb3ec6SMatthias Ringwald if (counter_packet < counter_db){ 2723deb3ec6SMatthias Ringwald log_info("ATT Signed Write, db reports higher counter, abort"); 2733deb3ec6SMatthias Ringwald att_server_state = ATT_SERVER_IDLE; 2743deb3ec6SMatthias Ringwald return; 2753deb3ec6SMatthias Ringwald } 2763deb3ec6SMatthias Ringwald 2773deb3ec6SMatthias Ringwald // signature is { sequence counter, secure hash } 2783deb3ec6SMatthias Ringwald sm_key_t csrk; 2793deb3ec6SMatthias Ringwald le_device_db_remote_csrk_get(att_ir_le_device_db_index, csrk); 2803deb3ec6SMatthias Ringwald att_server_state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION; 2813deb3ec6SMatthias Ringwald log_info("Orig Signature: "); 2823deb3ec6SMatthias Ringwald hexdump( &att_request_buffer[att_request_size-8], 8); 2833deb3ec6SMatthias Ringwald uint16_t attribute_handle = READ_BT_16(att_request_buffer, 1); 2843deb3ec6SMatthias 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); 2853deb3ec6SMatthias Ringwald return; 2863deb3ec6SMatthias Ringwald } 2873deb3ec6SMatthias Ringwald // NOTE: fall through for regular commands 2883deb3ec6SMatthias Ringwald 2893deb3ec6SMatthias Ringwald case ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED: 2903deb3ec6SMatthias Ringwald if (!l2cap_can_send_fixed_channel_packet_now(att_connection.con_handle)) return; 2913deb3ec6SMatthias Ringwald 2923deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 2933deb3ec6SMatthias Ringwald uint8_t * att_response_buffer = l2cap_get_outgoing_buffer(); 2943deb3ec6SMatthias Ringwald uint16_t att_response_size = att_handle_request(&att_connection, att_request_buffer, att_request_size, att_response_buffer); 2953deb3ec6SMatthias Ringwald 2963deb3ec6SMatthias Ringwald // intercept "insufficient authorization" for authenticated connections to allow for user authorization 2973deb3ec6SMatthias Ringwald if ((att_response_size >= 4) 2983deb3ec6SMatthias Ringwald && (att_response_buffer[0] == ATT_ERROR_RESPONSE) 2993deb3ec6SMatthias Ringwald && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION) 3003deb3ec6SMatthias Ringwald && (att_connection.authenticated)){ 3013deb3ec6SMatthias Ringwald 3023deb3ec6SMatthias Ringwald switch (sm_authorization_state(att_connection.con_handle)){ 3033deb3ec6SMatthias Ringwald case AUTHORIZATION_UNKNOWN: 3043deb3ec6SMatthias Ringwald l2cap_release_packet_buffer(); 3053deb3ec6SMatthias Ringwald sm_request_pairing(att_connection.con_handle); 3063deb3ec6SMatthias Ringwald return; 3073deb3ec6SMatthias Ringwald case AUTHORIZATION_PENDING: 3083deb3ec6SMatthias Ringwald l2cap_release_packet_buffer(); 3093deb3ec6SMatthias Ringwald return; 3103deb3ec6SMatthias Ringwald default: 3113deb3ec6SMatthias Ringwald break; 3123deb3ec6SMatthias Ringwald } 3133deb3ec6SMatthias Ringwald } 3143deb3ec6SMatthias Ringwald 3153deb3ec6SMatthias Ringwald att_server_state = ATT_SERVER_IDLE; 3163deb3ec6SMatthias Ringwald if (att_response_size == 0) { 3173deb3ec6SMatthias Ringwald l2cap_release_packet_buffer(); 3183deb3ec6SMatthias Ringwald return; 3193deb3ec6SMatthias Ringwald } 3203deb3ec6SMatthias Ringwald 3213deb3ec6SMatthias Ringwald l2cap_send_prepared_connectionless(att_connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size); 3223deb3ec6SMatthias Ringwald 3233deb3ec6SMatthias Ringwald // notify client about MTU exchange result 3243deb3ec6SMatthias Ringwald if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){ 3253deb3ec6SMatthias Ringwald att_emit_mtu_event(att_connection.con_handle, att_connection.mtu); 3263deb3ec6SMatthias Ringwald } 3273deb3ec6SMatthias Ringwald 3283deb3ec6SMatthias Ringwald break; 3293deb3ec6SMatthias Ringwald } 3303deb3ec6SMatthias Ringwald } 3313deb3ec6SMatthias Ringwald 3323deb3ec6SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){ 3333deb3ec6SMatthias Ringwald if (packet_type != ATT_DATA_PACKET) return; 3343deb3ec6SMatthias Ringwald 3353deb3ec6SMatthias Ringwald // handle value indication confirms 3363deb3ec6SMatthias Ringwald if (packet[0] == ATT_HANDLE_VALUE_CONFIRMATION && att_handle_value_indication_handle){ 337528a4a3bSMatthias Ringwald btstack_run_loop_remove_timer(&att_handle_value_indication_timer); 3383deb3ec6SMatthias Ringwald uint16_t att_handle = att_handle_value_indication_handle; 3393deb3ec6SMatthias Ringwald att_handle_value_indication_handle = 0; 3403deb3ec6SMatthias Ringwald att_handle_value_indication_notify_client(0, att_connection.con_handle, att_handle); 3413deb3ec6SMatthias Ringwald return; 3423deb3ec6SMatthias Ringwald } 3433deb3ec6SMatthias Ringwald 3443deb3ec6SMatthias Ringwald // check size 3453deb3ec6SMatthias Ringwald if (size > sizeof(att_request_buffer)) return; 3463deb3ec6SMatthias Ringwald 3473deb3ec6SMatthias Ringwald // last request still in processing? 3483deb3ec6SMatthias Ringwald if (att_server_state != ATT_SERVER_IDLE) return; 3493deb3ec6SMatthias Ringwald 3503deb3ec6SMatthias Ringwald // store request 3513deb3ec6SMatthias Ringwald att_server_state = ATT_SERVER_REQUEST_RECEIVED; 3523deb3ec6SMatthias Ringwald att_request_size = size; 3533deb3ec6SMatthias Ringwald memcpy(att_request_buffer, packet, size); 3543deb3ec6SMatthias Ringwald 3553deb3ec6SMatthias Ringwald att_run(); 3563deb3ec6SMatthias Ringwald } 3573deb3ec6SMatthias Ringwald 3583deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){ 3593deb3ec6SMatthias Ringwald 3603deb3ec6SMatthias Ringwald sm_register_packet_handler(att_event_packet_handler); 3613deb3ec6SMatthias Ringwald 3623deb3ec6SMatthias Ringwald att_dispatch_register_server(att_packet_handler); 3633deb3ec6SMatthias Ringwald 3643deb3ec6SMatthias Ringwald att_server_state = ATT_SERVER_IDLE; 3653deb3ec6SMatthias Ringwald att_set_db(db); 3663deb3ec6SMatthias Ringwald att_set_read_callback(read_callback); 3673deb3ec6SMatthias Ringwald att_set_write_callback(write_callback); 3683deb3ec6SMatthias Ringwald 3693deb3ec6SMatthias Ringwald } 3703deb3ec6SMatthias Ringwald 3713deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){ 3723deb3ec6SMatthias Ringwald att_client_packet_handler = handler; 3733deb3ec6SMatthias Ringwald } 3743deb3ec6SMatthias Ringwald 375941a6c67SMatthias Ringwald int att_server_can_send_packet_now(void){ 3763deb3ec6SMatthias Ringwald if (att_connection.con_handle == 0) return 0; 3773deb3ec6SMatthias Ringwald return l2cap_can_send_fixed_channel_packet_now(att_connection.con_handle); 3783deb3ec6SMatthias Ringwald } 3793deb3ec6SMatthias Ringwald 3803deb3ec6SMatthias Ringwald int att_server_notify(uint16_t handle, uint8_t *value, uint16_t value_len){ 3813deb3ec6SMatthias Ringwald if (!l2cap_can_send_fixed_channel_packet_now(att_connection.con_handle)) return BTSTACK_ACL_BUFFERS_FULL; 3823deb3ec6SMatthias Ringwald 3833deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 3843deb3ec6SMatthias Ringwald uint8_t * packet_buffer = l2cap_get_outgoing_buffer(); 3853deb3ec6SMatthias Ringwald uint16_t size = att_prepare_handle_value_notification(&att_connection, handle, value, value_len, packet_buffer); 3863deb3ec6SMatthias Ringwald return l2cap_send_prepared_connectionless(att_connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); 3873deb3ec6SMatthias Ringwald } 3883deb3ec6SMatthias Ringwald 3893deb3ec6SMatthias Ringwald int att_server_indicate(uint16_t handle, uint8_t *value, uint16_t value_len){ 3903deb3ec6SMatthias Ringwald if (att_handle_value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PORGRESS; 3913deb3ec6SMatthias Ringwald if (!l2cap_can_send_fixed_channel_packet_now(att_connection.con_handle)) return BTSTACK_ACL_BUFFERS_FULL; 3923deb3ec6SMatthias Ringwald 3933deb3ec6SMatthias Ringwald // track indication 3943deb3ec6SMatthias Ringwald att_handle_value_indication_handle = handle; 395528a4a3bSMatthias Ringwald btstack_run_loop_set_timer_handler(&att_handle_value_indication_timer, att_handle_value_indication_timeout); 396528a4a3bSMatthias Ringwald btstack_run_loop_set_timer(&att_handle_value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS); 397528a4a3bSMatthias Ringwald btstack_run_loop_add_timer(&att_handle_value_indication_timer); 3983deb3ec6SMatthias Ringwald 3993deb3ec6SMatthias Ringwald l2cap_reserve_packet_buffer(); 4003deb3ec6SMatthias Ringwald uint8_t * packet_buffer = l2cap_get_outgoing_buffer(); 4013deb3ec6SMatthias Ringwald uint16_t size = att_prepare_handle_value_indication(&att_connection, handle, value, value_len, packet_buffer); 4023deb3ec6SMatthias Ringwald l2cap_send_prepared_connectionless(att_connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size); 4033deb3ec6SMatthias Ringwald return 0; 4043deb3ec6SMatthias Ringwald } 405