xref: /btstack/src/ble/att_server.c (revision c141988c583922eacaf7d6244205a587e58e373f)
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 
1561b89a84bSMatthias Ringwald                 case L2CAP_EVENT_CAN_SEND_NOW:
1573deb3ec6SMatthias Ringwald                     att_run();
1583deb3ec6SMatthias Ringwald                     break;
1593deb3ec6SMatthias Ringwald 
1603deb3ec6SMatthias Ringwald                 case HCI_EVENT_LE_META:
1613deb3ec6SMatthias Ringwald                     switch (packet[2]) {
1623deb3ec6SMatthias Ringwald                         case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
1633deb3ec6SMatthias Ringwald                         	// store connection info
1643deb3ec6SMatthias Ringwald                         	att_client_addr_type = packet[7];
165724d70a2SMatthias Ringwald                             reverse_bd_addr(&packet[8], att_client_address);
1663deb3ec6SMatthias Ringwald                             // reset connection properties
167f8fbdce0SMatthias Ringwald                             att_connection.con_handle = little_endian_read_16(packet, 4);
1683deb3ec6SMatthias Ringwald                             att_connection.mtu = ATT_DEFAULT_MTU;
1693deb3ec6SMatthias Ringwald                             att_connection.max_mtu = l2cap_max_le_mtu();
17099c58ad0SMatthias Ringwald                             if (att_connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){
17199c58ad0SMatthias Ringwald                                 att_connection.max_mtu = ATT_REQUEST_BUFFER_SIZE;
17299c58ad0SMatthias Ringwald                             }
1733deb3ec6SMatthias Ringwald                             att_connection.encryption_key_size = 0;
1743deb3ec6SMatthias Ringwald                             att_connection.authenticated = 0;
1753deb3ec6SMatthias Ringwald 		                	att_connection.authorized = 0;
1763deb3ec6SMatthias Ringwald                             break;
1773deb3ec6SMatthias Ringwald 
1783deb3ec6SMatthias Ringwald                         default:
1793deb3ec6SMatthias Ringwald                             break;
1803deb3ec6SMatthias Ringwald                     }
1813deb3ec6SMatthias Ringwald                     break;
1823deb3ec6SMatthias Ringwald 
1833deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_CHANGE:
1843deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE:
1853deb3ec6SMatthias Ringwald                 	// check handle
186f8fbdce0SMatthias Ringwald                 	if (att_connection.con_handle != little_endian_read_16(packet, 3)) break;
1873deb3ec6SMatthias Ringwald                 	att_connection.encryption_key_size = sm_encryption_key_size(att_connection.con_handle);
1883deb3ec6SMatthias Ringwald                 	att_connection.authenticated = sm_authenticated(att_connection.con_handle);
1893deb3ec6SMatthias Ringwald                 	break;
1903deb3ec6SMatthias Ringwald 
1913deb3ec6SMatthias Ringwald                 case HCI_EVENT_DISCONNECTION_COMPLETE:
1923deb3ec6SMatthias Ringwald                     att_clear_transaction_queue(&att_connection);
1933deb3ec6SMatthias Ringwald                     att_connection.con_handle = 0;
1943deb3ec6SMatthias Ringwald                     att_handle_value_indication_handle = 0; // reset error state
1953deb3ec6SMatthias Ringwald                     // restart advertising if we have been connected before
1963deb3ec6SMatthias Ringwald                     // -> avoid sending advertise enable a second time before command complete was received
1973deb3ec6SMatthias Ringwald                     att_server_state = ATT_SERVER_IDLE;
1983deb3ec6SMatthias Ringwald                     break;
1993deb3ec6SMatthias Ringwald 
2005611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_STARTED:
2015611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED");
2023deb3ec6SMatthias Ringwald                     att_ir_lookup_active = 1;
2033deb3ec6SMatthias Ringwald                     break;
2045611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
2053deb3ec6SMatthias Ringwald                     att_ir_lookup_active = 0;
206f8fbdce0SMatthias Ringwald                     att_ir_le_device_db_index = little_endian_read_16(packet, 11);
2075611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED id %u", att_ir_le_device_db_index);
2083deb3ec6SMatthias Ringwald                     att_run();
2093deb3ec6SMatthias Ringwald                     break;
2105611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_FAILED:
2115611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED");
2123deb3ec6SMatthias Ringwald                     att_ir_lookup_active = 0;
2133deb3ec6SMatthias Ringwald                     att_ir_le_device_db_index = -1;
2143deb3ec6SMatthias Ringwald                     att_run();
2153deb3ec6SMatthias Ringwald                     break;
2165611a760SMatthias Ringwald                 case SM_EVENT_AUTHORIZATION_RESULT: {
2173deb3ec6SMatthias Ringwald                     if (packet[4] != att_client_addr_type) break;
218724d70a2SMatthias Ringwald                     reverse_bd_addr(&packet[5], event_address);
2193deb3ec6SMatthias Ringwald                     if (memcmp(event_address, att_client_address, 6) != 0) break;
2203deb3ec6SMatthias Ringwald                     att_connection.authorized = packet[11];
2213deb3ec6SMatthias Ringwald                     att_run();
2223deb3ec6SMatthias Ringwald                 	break;
2233deb3ec6SMatthias Ringwald                 }
2243deb3ec6SMatthias Ringwald                 default:
2253deb3ec6SMatthias Ringwald                     break;
2263deb3ec6SMatthias Ringwald             }
22765b44ffdSMatthias Ringwald             break;
22865b44ffdSMatthias Ringwald         default:
22965b44ffdSMatthias Ringwald             break;
2303deb3ec6SMatthias Ringwald     }
2313deb3ec6SMatthias Ringwald }
2323deb3ec6SMatthias Ringwald 
2333deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
2343deb3ec6SMatthias Ringwald 
2353deb3ec6SMatthias Ringwald     if (att_server_state != ATT_SERVER_W4_SIGNED_WRITE_VALIDATION) return;
2363deb3ec6SMatthias Ringwald 
2373deb3ec6SMatthias Ringwald     uint8_t hash_flipped[8];
2389c80e4ccSMatthias Ringwald     reverse_64(hash, hash_flipped);
2393deb3ec6SMatthias Ringwald     if (memcmp(hash_flipped, &att_request_buffer[att_request_size-8], 8)){
2403deb3ec6SMatthias Ringwald         log_info("ATT Signed Write, invalid signature");
2413deb3ec6SMatthias Ringwald         att_server_state = ATT_SERVER_IDLE;
2423deb3ec6SMatthias Ringwald         return;
2433deb3ec6SMatthias Ringwald     }
2443deb3ec6SMatthias Ringwald     log_info("ATT Signed Write, valid signature");
2453deb3ec6SMatthias Ringwald 
2463deb3ec6SMatthias Ringwald     // update sequence number
247f8fbdce0SMatthias Ringwald     uint32_t counter_packet = little_endian_read_32(att_request_buffer, att_request_size-12);
2483deb3ec6SMatthias Ringwald     le_device_db_remote_counter_set(att_ir_le_device_db_index, counter_packet+1);
2493deb3ec6SMatthias Ringwald     att_server_state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
2503deb3ec6SMatthias Ringwald     att_run();
2513deb3ec6SMatthias Ringwald }
2523deb3ec6SMatthias Ringwald 
2533deb3ec6SMatthias Ringwald static void att_run(void){
2543deb3ec6SMatthias Ringwald     switch (att_server_state){
2553deb3ec6SMatthias Ringwald         case ATT_SERVER_IDLE:
2563deb3ec6SMatthias Ringwald         case ATT_SERVER_W4_SIGNED_WRITE_VALIDATION:
2573deb3ec6SMatthias Ringwald             return;
2583deb3ec6SMatthias Ringwald         case ATT_SERVER_REQUEST_RECEIVED:
2593deb3ec6SMatthias Ringwald             if (att_request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){
2603deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write!");
2613deb3ec6SMatthias Ringwald                 if (!sm_cmac_ready()) {
2623deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, sm_cmac engine not ready. Abort");
2633deb3ec6SMatthias Ringwald                     att_server_state = ATT_SERVER_IDLE;
2643deb3ec6SMatthias Ringwald                      return;
2653deb3ec6SMatthias Ringwald                 }
2663deb3ec6SMatthias Ringwald                 if (att_request_size < (3 + 12)) {
2673deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, request to short. Abort.");
2683deb3ec6SMatthias Ringwald                     att_server_state = ATT_SERVER_IDLE;
2693deb3ec6SMatthias Ringwald                     return;
2703deb3ec6SMatthias Ringwald                 }
2713deb3ec6SMatthias Ringwald                 if (att_ir_lookup_active){
2723deb3ec6SMatthias Ringwald                     return;
2733deb3ec6SMatthias Ringwald                 }
2743deb3ec6SMatthias Ringwald                 if (att_ir_le_device_db_index < 0){
2753deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, CSRK not available");
2763deb3ec6SMatthias Ringwald                     att_server_state = ATT_SERVER_IDLE;
2773deb3ec6SMatthias Ringwald                     return;
2783deb3ec6SMatthias Ringwald                 }
2793deb3ec6SMatthias Ringwald 
2803deb3ec6SMatthias Ringwald                 // check counter
281f8fbdce0SMatthias Ringwald                 uint32_t counter_packet = little_endian_read_32(att_request_buffer, att_request_size-12);
2823deb3ec6SMatthias Ringwald                 uint32_t counter_db     = le_device_db_remote_counter_get(att_ir_le_device_db_index);
2833deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet);
2843deb3ec6SMatthias Ringwald                 if (counter_packet < counter_db){
2853deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, db reports higher counter, abort");
2863deb3ec6SMatthias Ringwald                     att_server_state = ATT_SERVER_IDLE;
2873deb3ec6SMatthias Ringwald                     return;
2883deb3ec6SMatthias Ringwald                 }
2893deb3ec6SMatthias Ringwald 
2903deb3ec6SMatthias Ringwald                 // signature is { sequence counter, secure hash }
2913deb3ec6SMatthias Ringwald                 sm_key_t csrk;
2923deb3ec6SMatthias Ringwald                 le_device_db_remote_csrk_get(att_ir_le_device_db_index, csrk);
2933deb3ec6SMatthias Ringwald                 att_server_state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION;
2943deb3ec6SMatthias Ringwald                 log_info("Orig Signature: ");
2958314c363SMatthias Ringwald                 log_info_hexdump( &att_request_buffer[att_request_size-8], 8);
296f8fbdce0SMatthias Ringwald                 uint16_t attribute_handle = little_endian_read_16(att_request_buffer, 1);
2973deb3ec6SMatthias 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);
2983deb3ec6SMatthias Ringwald                 return;
2993deb3ec6SMatthias Ringwald             }
3003deb3ec6SMatthias Ringwald             // NOTE: fall through for regular commands
3013deb3ec6SMatthias Ringwald 
3023deb3ec6SMatthias Ringwald         case ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED:
30319448c0bSMatthias Ringwald             if (!att_dispatch_server_can_send_now(att_connection.con_handle)) return;
3043deb3ec6SMatthias Ringwald 
3053deb3ec6SMatthias Ringwald             l2cap_reserve_packet_buffer();
3063deb3ec6SMatthias Ringwald             uint8_t * att_response_buffer = l2cap_get_outgoing_buffer();
3073deb3ec6SMatthias Ringwald             uint16_t  att_response_size   = att_handle_request(&att_connection, att_request_buffer, att_request_size, att_response_buffer);
3083deb3ec6SMatthias Ringwald 
3093deb3ec6SMatthias Ringwald             // intercept "insufficient authorization" for authenticated connections to allow for user authorization
3103deb3ec6SMatthias Ringwald             if ((att_response_size     >= 4)
3113deb3ec6SMatthias Ringwald             && (att_response_buffer[0] == ATT_ERROR_RESPONSE)
3123deb3ec6SMatthias Ringwald             && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION)
3133deb3ec6SMatthias Ringwald             && (att_connection.authenticated)){
3143deb3ec6SMatthias Ringwald 
3153deb3ec6SMatthias Ringwald             	switch (sm_authorization_state(att_connection.con_handle)){
3163deb3ec6SMatthias Ringwald             		case AUTHORIZATION_UNKNOWN:
3173deb3ec6SMatthias Ringwald                         l2cap_release_packet_buffer();
3183deb3ec6SMatthias Ringwald 		             	sm_request_pairing(att_connection.con_handle);
3193deb3ec6SMatthias Ringwald 	    		        return;
3203deb3ec6SMatthias Ringwald 	    		    case AUTHORIZATION_PENDING:
3213deb3ec6SMatthias Ringwald                         l2cap_release_packet_buffer();
3223deb3ec6SMatthias Ringwald 	    		    	return;
3233deb3ec6SMatthias Ringwald 	    		    default:
3243deb3ec6SMatthias Ringwald 	    		    	break;
3253deb3ec6SMatthias Ringwald             	}
3263deb3ec6SMatthias Ringwald             }
3273deb3ec6SMatthias Ringwald 
3283deb3ec6SMatthias Ringwald             att_server_state = ATT_SERVER_IDLE;
3293deb3ec6SMatthias Ringwald             if (att_response_size == 0) {
3303deb3ec6SMatthias Ringwald                 l2cap_release_packet_buffer();
3313deb3ec6SMatthias Ringwald                 return;
3323deb3ec6SMatthias Ringwald             }
3333deb3ec6SMatthias Ringwald 
3343deb3ec6SMatthias Ringwald             l2cap_send_prepared_connectionless(att_connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size);
3353deb3ec6SMatthias Ringwald 
3363deb3ec6SMatthias Ringwald             // notify client about MTU exchange result
3373deb3ec6SMatthias Ringwald             if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){
3383deb3ec6SMatthias Ringwald                 att_emit_mtu_event(att_connection.con_handle, att_connection.mtu);
3393deb3ec6SMatthias Ringwald             }
3403deb3ec6SMatthias Ringwald 
3413deb3ec6SMatthias Ringwald             break;
3423deb3ec6SMatthias Ringwald     }
3433deb3ec6SMatthias Ringwald }
3443deb3ec6SMatthias Ringwald 
3453deb3ec6SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
346372d62c4SMatthias Ringwald     switch (packet_type){
347372d62c4SMatthias Ringwald         case HCI_EVENT_PACKET:
348372d62c4SMatthias Ringwald             if (packet[0] != L2CAP_EVENT_CAN_SEND_NOW) break;
349372d62c4SMatthias Ringwald             att_run();
3503c97b242SMatthias Ringwald             // if we cannot send now, we'll get another l2cap can send now soon
3513c97b242SMatthias Ringwald             att_server_notify_can_send();
352372d62c4SMatthias Ringwald             break;
3533deb3ec6SMatthias Ringwald 
354372d62c4SMatthias Ringwald         case ATT_DATA_PACKET:
3553deb3ec6SMatthias Ringwald             // handle value indication confirms
3563deb3ec6SMatthias Ringwald             if (packet[0] == ATT_HANDLE_VALUE_CONFIRMATION && att_handle_value_indication_handle){
357528a4a3bSMatthias Ringwald                 btstack_run_loop_remove_timer(&att_handle_value_indication_timer);
3583deb3ec6SMatthias Ringwald                 uint16_t att_handle = att_handle_value_indication_handle;
3593deb3ec6SMatthias Ringwald                 att_handle_value_indication_handle = 0;
3603deb3ec6SMatthias Ringwald                 att_handle_value_indication_notify_client(0, att_connection.con_handle, att_handle);
3613deb3ec6SMatthias Ringwald                 return;
3623deb3ec6SMatthias Ringwald             }
3633deb3ec6SMatthias Ringwald 
3646428eb5aSMatthias Ringwald             // directly process command
3656428eb5aSMatthias Ringwald             // note: signed write cannot be handled directly as authentication needs to be verified
3666428eb5aSMatthias Ringwald             if (packet[0] == ATT_WRITE_COMMAND){
3676428eb5aSMatthias Ringwald                 att_handle_request(&att_connection, packet, size, 0);
3686428eb5aSMatthias Ringwald                 return;
3696428eb5aSMatthias Ringwald             }
3706428eb5aSMatthias Ringwald 
3713deb3ec6SMatthias Ringwald             // check size
3726428eb5aSMatthias Ringwald             if (size > sizeof(att_request_buffer)) {
3736428eb5aSMatthias 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));
3746428eb5aSMatthias Ringwald                 return;
3756428eb5aSMatthias Ringwald             }
3763deb3ec6SMatthias Ringwald 
3773deb3ec6SMatthias Ringwald             // last request still in processing?
3786428eb5aSMatthias Ringwald             if (att_server_state != ATT_SERVER_IDLE){
3796428eb5aSMatthias Ringwald                 log_info("att_packet_handler: skipping att pdu 0x%02x as server not idle (state %u)", packet[0], att_server_state);
3806428eb5aSMatthias Ringwald                 return;
3816428eb5aSMatthias Ringwald             }
3823deb3ec6SMatthias Ringwald 
3833deb3ec6SMatthias Ringwald             // store request
3843deb3ec6SMatthias Ringwald             att_server_state = ATT_SERVER_REQUEST_RECEIVED;
3853deb3ec6SMatthias Ringwald             att_request_size = size;
3863deb3ec6SMatthias Ringwald             memcpy(att_request_buffer, packet, size);
3873deb3ec6SMatthias Ringwald 
3883deb3ec6SMatthias Ringwald             att_run();
389372d62c4SMatthias Ringwald             break;
390372d62c4SMatthias Ringwald     }
3913deb3ec6SMatthias Ringwald }
3923deb3ec6SMatthias Ringwald 
3933deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){
3943deb3ec6SMatthias Ringwald 
3951a389cdcSMatthias Ringwald     // register for HCI Events
396d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &att_event_packet_handler;
3971a389cdcSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
3981a389cdcSMatthias Ringwald 
399406722e4SMatthias Ringwald     // register for SM events
400d9a7306aSMatthias Ringwald     sm_event_callback_registration.callback = &att_event_packet_handler;
401406722e4SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
4023deb3ec6SMatthias Ringwald 
4031a389cdcSMatthias Ringwald     // and L2CAP ATT Server PDUs
4043deb3ec6SMatthias Ringwald     att_dispatch_register_server(att_packet_handler);
4053deb3ec6SMatthias Ringwald 
4063deb3ec6SMatthias Ringwald     att_server_state = ATT_SERVER_IDLE;
4073deb3ec6SMatthias Ringwald     att_set_db(db);
4083deb3ec6SMatthias Ringwald     att_set_read_callback(read_callback);
4093deb3ec6SMatthias Ringwald     att_set_write_callback(write_callback);
4103deb3ec6SMatthias Ringwald 
4113deb3ec6SMatthias Ringwald }
4123deb3ec6SMatthias Ringwald 
4133deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){
4143deb3ec6SMatthias Ringwald     att_client_packet_handler = handler;
4153deb3ec6SMatthias Ringwald }
4163deb3ec6SMatthias Ringwald 
417*c141988cSMatthias Ringwald // NOTE: con_handle is ignored for now as only a single connection is supported
418*c141988cSMatthias Ringwald // TODO: support multiple connections
419*c141988cSMatthias Ringwald 
420*c141988cSMatthias Ringwald int  att_server_can_send_packet_now(hci_con_handle_t con_handle){
4213deb3ec6SMatthias Ringwald 	if (att_connection.con_handle == 0) return 0;
4220b9d7e78SMatthias Ringwald 	return att_dispatch_server_can_send_now(att_connection.con_handle);
4231b96b007SMatthias Ringwald }
4241b96b007SMatthias Ringwald 
425*c141988cSMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){
4261b96b007SMatthias Ringwald     att_client_waiting_for_can_send = 1;
4270b9d7e78SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(att_connection.con_handle);
4283deb3ec6SMatthias Ringwald }
4293deb3ec6SMatthias Ringwald 
430*c141988cSMatthias Ringwald int att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){
43119448c0bSMatthias Ringwald     if (!att_dispatch_server_can_send_now(att_connection.con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
4323deb3ec6SMatthias Ringwald 
4333deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
4343deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
435fc64f94aSMatthias Ringwald     uint16_t size = att_prepare_handle_value_notification(&att_connection, attribute_handle, value, value_len, packet_buffer);
4363deb3ec6SMatthias Ringwald 	return l2cap_send_prepared_connectionless(att_connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
4373deb3ec6SMatthias Ringwald }
4383deb3ec6SMatthias Ringwald 
439*c141988cSMatthias Ringwald int att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){
4403deb3ec6SMatthias Ringwald     if (att_handle_value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PORGRESS;
44119448c0bSMatthias Ringwald     if (!att_dispatch_server_can_send_now(att_connection.con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
4423deb3ec6SMatthias Ringwald 
4433deb3ec6SMatthias Ringwald     // track indication
444fc64f94aSMatthias Ringwald     att_handle_value_indication_handle = attribute_handle;
445528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer_handler(&att_handle_value_indication_timer, att_handle_value_indication_timeout);
446528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer(&att_handle_value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS);
447528a4a3bSMatthias Ringwald     btstack_run_loop_add_timer(&att_handle_value_indication_timer);
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_indication(&att_connection, attribute_handle, value, value_len, packet_buffer);
4523deb3ec6SMatthias Ringwald 	l2cap_send_prepared_connectionless(att_connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
4533deb3ec6SMatthias Ringwald     return 0;
4543deb3ec6SMatthias Ringwald }
455