xref: /btstack/src/ble/att_server.c (revision 70a390c7e09251d39a245e7c114302be0c95b94b)
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     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1103deb3ec6SMatthias Ringwald }
1113deb3ec6SMatthias Ringwald 
112fc64f94aSMatthias Ringwald static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){
1133deb3ec6SMatthias Ringwald     if (!att_client_packet_handler) return;
1143deb3ec6SMatthias Ringwald 
1153deb3ec6SMatthias Ringwald     uint8_t event[6];
1163deb3ec6SMatthias Ringwald     int pos = 0;
1175611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE;
1183deb3ec6SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
119fc64f94aSMatthias Ringwald     little_endian_store_16(event, pos, con_handle);
1203deb3ec6SMatthias Ringwald     pos += 2;
121f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, mtu);
1223deb3ec6SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1233deb3ec6SMatthias Ringwald }
1243deb3ec6SMatthias Ringwald 
1251b96b007SMatthias Ringwald static void att_emit_can_send_now_event(void){
1263c97b242SMatthias Ringwald     if (!att_client_packet_handler) return;
1273c97b242SMatthias Ringwald 
1281b96b007SMatthias Ringwald     uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0};
1291b96b007SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1301b96b007SMatthias Ringwald }
1311b96b007SMatthias Ringwald 
132ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){
1333deb3ec6SMatthias Ringwald     uint16_t att_handle = att_handle_value_indication_handle;
1343deb3ec6SMatthias Ringwald     att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_connection.con_handle, att_handle);
1353deb3ec6SMatthias Ringwald }
1363deb3ec6SMatthias Ringwald 
1373deb3ec6SMatthias Ringwald static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1383deb3ec6SMatthias Ringwald 
1393deb3ec6SMatthias Ringwald     bd_addr_t event_address;
1403deb3ec6SMatthias Ringwald     switch (packet_type) {
1413deb3ec6SMatthias Ringwald 
1423deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1430e2df43fSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
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];
150724d70a2SMatthias Ringwald                             reverse_bd_addr(&packet[8], att_client_address);
1513deb3ec6SMatthias Ringwald                             // reset connection properties
152f8fbdce0SMatthias Ringwald                             att_connection.con_handle = little_endian_read_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
171f8fbdce0SMatthias Ringwald                 	if (att_connection.con_handle != little_endian_read_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:
177*70a390c7SMatthias Ringwald                     // check handle
178*70a390c7SMatthias Ringwald                     if (att_connection.con_handle != hci_event_disconnection_complete_get_connection_handle(packet)) break;
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                     att_server_state = ATT_SERVER_IDLE;
1833deb3ec6SMatthias Ringwald                     break;
1843deb3ec6SMatthias Ringwald 
1855611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_STARTED:
1865611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED");
1873deb3ec6SMatthias Ringwald                     att_ir_lookup_active = 1;
1883deb3ec6SMatthias Ringwald                     break;
1895611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
1903deb3ec6SMatthias Ringwald                     att_ir_lookup_active = 0;
191f8fbdce0SMatthias Ringwald                     att_ir_le_device_db_index = little_endian_read_16(packet, 11);
1925611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED id %u", att_ir_le_device_db_index);
1933deb3ec6SMatthias Ringwald                     att_run();
1943deb3ec6SMatthias Ringwald                     break;
1955611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_FAILED:
1965611a760SMatthias 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;
2015611a760SMatthias Ringwald                 case SM_EVENT_AUTHORIZATION_RESULT: {
2023deb3ec6SMatthias Ringwald                     if (packet[4] != att_client_addr_type) break;
203724d70a2SMatthias Ringwald                     reverse_bd_addr(&packet[5], event_address);
2043deb3ec6SMatthias Ringwald                     if (memcmp(event_address, att_client_address, 6) != 0) break;
2053deb3ec6SMatthias Ringwald                     att_connection.authorized = packet[11];
206b06f2579SMatthias Ringwald                     att_dispatch_server_request_can_send_now_event(att_connection.con_handle);
2073deb3ec6SMatthias Ringwald                 	break;
2083deb3ec6SMatthias Ringwald                 }
2093deb3ec6SMatthias Ringwald                 default:
2103deb3ec6SMatthias Ringwald                     break;
2113deb3ec6SMatthias Ringwald             }
21265b44ffdSMatthias Ringwald             break;
21365b44ffdSMatthias Ringwald         default:
21465b44ffdSMatthias Ringwald             break;
2153deb3ec6SMatthias Ringwald     }
2163deb3ec6SMatthias Ringwald }
2173deb3ec6SMatthias Ringwald 
2183deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
2193deb3ec6SMatthias Ringwald 
2203deb3ec6SMatthias Ringwald     if (att_server_state != ATT_SERVER_W4_SIGNED_WRITE_VALIDATION) return;
2213deb3ec6SMatthias Ringwald 
2223deb3ec6SMatthias Ringwald     uint8_t hash_flipped[8];
2239c80e4ccSMatthias Ringwald     reverse_64(hash, hash_flipped);
2243deb3ec6SMatthias Ringwald     if (memcmp(hash_flipped, &att_request_buffer[att_request_size-8], 8)){
2253deb3ec6SMatthias Ringwald         log_info("ATT Signed Write, invalid signature");
2263deb3ec6SMatthias Ringwald         att_server_state = ATT_SERVER_IDLE;
2273deb3ec6SMatthias Ringwald         return;
2283deb3ec6SMatthias Ringwald     }
2293deb3ec6SMatthias Ringwald     log_info("ATT Signed Write, valid signature");
2303deb3ec6SMatthias Ringwald 
2313deb3ec6SMatthias Ringwald     // update sequence number
232f8fbdce0SMatthias Ringwald     uint32_t counter_packet = little_endian_read_32(att_request_buffer, att_request_size-12);
2333deb3ec6SMatthias Ringwald     le_device_db_remote_counter_set(att_ir_le_device_db_index, counter_packet+1);
2343deb3ec6SMatthias Ringwald     att_server_state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
235b06f2579SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(att_connection.con_handle);
2363deb3ec6SMatthias Ringwald }
2373deb3ec6SMatthias Ringwald 
23888a48dd8SMatthias Ringwald #if 0
23988a48dd8SMatthias Ringwald static int att_server_ready_to_send(att_connection_t * connection){
24088a48dd8SMatthias Ringwald }
24188a48dd8SMatthias Ringwald #endif
24288a48dd8SMatthias Ringwald 
243b06f2579SMatthias Ringwald // pre: att_server_state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED
244b06f2579SMatthias Ringwald // pre: can send now
245b06f2579SMatthias Ringwald // returns: 1 if packet was sent
246b06f2579SMatthias Ringwald static int att_server_process_validated_request(hci_con_handle_t con_handle){
247b06f2579SMatthias Ringwald 
248b06f2579SMatthias Ringwald     l2cap_reserve_packet_buffer();
249b06f2579SMatthias Ringwald     uint8_t * att_response_buffer = l2cap_get_outgoing_buffer();
250b06f2579SMatthias Ringwald     uint16_t  att_response_size   = att_handle_request(&att_connection, att_request_buffer, att_request_size, att_response_buffer);
251b06f2579SMatthias Ringwald 
252b06f2579SMatthias Ringwald     // intercept "insufficient authorization" for authenticated connections to allow for user authorization
253b06f2579SMatthias Ringwald     if ((att_response_size     >= 4)
254b06f2579SMatthias Ringwald     && (att_response_buffer[0] == ATT_ERROR_RESPONSE)
255b06f2579SMatthias Ringwald     && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION)
256b06f2579SMatthias Ringwald     && (att_connection.authenticated)){
257b06f2579SMatthias Ringwald 
258b06f2579SMatthias Ringwald         switch (sm_authorization_state(att_connection.con_handle)){
259b06f2579SMatthias Ringwald             case AUTHORIZATION_UNKNOWN:
260b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
261b06f2579SMatthias Ringwald                 sm_request_pairing(att_connection.con_handle);
262b06f2579SMatthias Ringwald                 return 0;
263b06f2579SMatthias Ringwald             case AUTHORIZATION_PENDING:
264b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
265b06f2579SMatthias Ringwald                 return 0;
266b06f2579SMatthias Ringwald             default:
267b06f2579SMatthias Ringwald                 break;
268b06f2579SMatthias Ringwald         }
269b06f2579SMatthias Ringwald     }
270b06f2579SMatthias Ringwald 
271b06f2579SMatthias Ringwald     att_server_state = ATT_SERVER_IDLE;
272b06f2579SMatthias Ringwald     if (att_response_size == 0) {
273b06f2579SMatthias Ringwald         l2cap_release_packet_buffer();
274b06f2579SMatthias Ringwald         return 0;
275b06f2579SMatthias Ringwald     }
276b06f2579SMatthias Ringwald 
277b06f2579SMatthias Ringwald     l2cap_send_prepared_connectionless(att_connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size);
278b06f2579SMatthias Ringwald 
279b06f2579SMatthias Ringwald     // notify client about MTU exchange result
280b06f2579SMatthias Ringwald     if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){
281b06f2579SMatthias Ringwald         att_emit_mtu_event(att_connection.con_handle, att_connection.mtu);
282b06f2579SMatthias Ringwald     }
283b06f2579SMatthias Ringwald     return 1;
284b06f2579SMatthias Ringwald }
285b06f2579SMatthias Ringwald 
2863deb3ec6SMatthias Ringwald static void att_run(void){
2873deb3ec6SMatthias Ringwald     switch (att_server_state){
2883deb3ec6SMatthias Ringwald         case ATT_SERVER_REQUEST_RECEIVED:
2893deb3ec6SMatthias Ringwald             if (att_request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){
2903deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write!");
2913deb3ec6SMatthias Ringwald                 if (!sm_cmac_ready()) {
2923deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, sm_cmac engine not ready. Abort");
2933deb3ec6SMatthias Ringwald                     att_server_state = ATT_SERVER_IDLE;
2943deb3ec6SMatthias Ringwald                     return;
2953deb3ec6SMatthias Ringwald                 }
2963deb3ec6SMatthias Ringwald                 if (att_request_size < (3 + 12)) {
2973deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, request to short. Abort.");
2983deb3ec6SMatthias Ringwald                     att_server_state = ATT_SERVER_IDLE;
2993deb3ec6SMatthias Ringwald                     return;
3003deb3ec6SMatthias Ringwald                 }
3013deb3ec6SMatthias Ringwald                 if (att_ir_lookup_active){
3023deb3ec6SMatthias Ringwald                     return;
3033deb3ec6SMatthias Ringwald                 }
3043deb3ec6SMatthias Ringwald                 if (att_ir_le_device_db_index < 0){
3053deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, CSRK not available");
3063deb3ec6SMatthias Ringwald                     att_server_state = ATT_SERVER_IDLE;
3073deb3ec6SMatthias Ringwald                     return;
3083deb3ec6SMatthias Ringwald                 }
3093deb3ec6SMatthias Ringwald 
3103deb3ec6SMatthias Ringwald                 // check counter
311f8fbdce0SMatthias Ringwald                 uint32_t counter_packet = little_endian_read_32(att_request_buffer, att_request_size-12);
3123deb3ec6SMatthias Ringwald                 uint32_t counter_db     = le_device_db_remote_counter_get(att_ir_le_device_db_index);
3133deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet);
3143deb3ec6SMatthias Ringwald                 if (counter_packet < counter_db){
3153deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, db reports higher counter, abort");
3163deb3ec6SMatthias Ringwald                     att_server_state = ATT_SERVER_IDLE;
3173deb3ec6SMatthias Ringwald                     return;
3183deb3ec6SMatthias Ringwald                 }
3193deb3ec6SMatthias Ringwald 
3203deb3ec6SMatthias Ringwald                 // signature is { sequence counter, secure hash }
3213deb3ec6SMatthias Ringwald                 sm_key_t csrk;
3223deb3ec6SMatthias Ringwald                 le_device_db_remote_csrk_get(att_ir_le_device_db_index, csrk);
3233deb3ec6SMatthias Ringwald                 att_server_state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION;
3243deb3ec6SMatthias Ringwald                 log_info("Orig Signature: ");
3258314c363SMatthias Ringwald                 log_info_hexdump( &att_request_buffer[att_request_size-8], 8);
326f8fbdce0SMatthias Ringwald                 uint16_t attribute_handle = little_endian_read_16(att_request_buffer, 1);
3274dfd504aSMatthias Ringwald                 sm_cmac_signed_write_start(csrk, att_request_buffer[0], attribute_handle, att_request_size - 15, &att_request_buffer[3], counter_packet, att_signed_write_handle_cmac_result);
3283deb3ec6SMatthias Ringwald                 return;
3293deb3ec6SMatthias Ringwald             }
3303deb3ec6SMatthias Ringwald 
331b06f2579SMatthias Ringwald             // move on
332b06f2579SMatthias Ringwald             att_server_state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
333b06f2579SMatthias Ringwald             att_dispatch_server_request_can_send_now_event(att_connection.con_handle);
334b06f2579SMatthias Ringwald             break;
33588a48dd8SMatthias Ringwald 
3363deb3ec6SMatthias Ringwald         default:
3373deb3ec6SMatthias Ringwald             break;
3383deb3ec6SMatthias Ringwald     }
3393deb3ec6SMatthias Ringwald }
3403deb3ec6SMatthias Ringwald 
3417eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){
342b06f2579SMatthias Ringwald 
343b06f2579SMatthias Ringwald     // NOTE: we get l2cap fixed channel instead of con_handle
344b06f2579SMatthias Ringwald     // TODO: get con_handle
345b06f2579SMatthias Ringwald 
346b06f2579SMatthias Ringwald     if (att_server_state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED){
3474d2be802SMatthias Ringwald         int sent = att_server_process_validated_request(att_connection.con_handle);
348b06f2579SMatthias Ringwald         if (sent && att_client_waiting_for_can_send){
3494d2be802SMatthias Ringwald             att_dispatch_server_request_can_send_now_event(att_connection.con_handle);
3503deb3ec6SMatthias Ringwald             return;
3513deb3ec6SMatthias Ringwald         }
3523deb3ec6SMatthias Ringwald     }
3533deb3ec6SMatthias Ringwald 
354b06f2579SMatthias Ringwald     if (att_client_waiting_for_can_send){
355b06f2579SMatthias Ringwald         att_client_waiting_for_can_send = 0;
356b06f2579SMatthias Ringwald         att_emit_can_send_now_event();
3573deb3ec6SMatthias Ringwald     }
3583deb3ec6SMatthias Ringwald }
3593deb3ec6SMatthias Ringwald 
36088a48dd8SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
36188a48dd8SMatthias Ringwald     switch (packet_type){
36288a48dd8SMatthias Ringwald 
36388a48dd8SMatthias Ringwald         case HCI_EVENT_PACKET:
36488a48dd8SMatthias Ringwald             if (packet[0] != L2CAP_EVENT_CAN_SEND_NOW) break;
3654d2be802SMatthias Ringwald             att_server_handle_can_send_now();
366372d62c4SMatthias Ringwald             break;
3673deb3ec6SMatthias Ringwald 
368372d62c4SMatthias Ringwald         case ATT_DATA_PACKET:
3693deb3ec6SMatthias Ringwald             // handle value indication confirms
3703deb3ec6SMatthias Ringwald             if (packet[0] == ATT_HANDLE_VALUE_CONFIRMATION && att_handle_value_indication_handle){
371528a4a3bSMatthias Ringwald                 btstack_run_loop_remove_timer(&att_handle_value_indication_timer);
3723deb3ec6SMatthias Ringwald                 uint16_t att_handle = att_handle_value_indication_handle;
3733deb3ec6SMatthias Ringwald                 att_handle_value_indication_handle = 0;
3743deb3ec6SMatthias Ringwald                 att_handle_value_indication_notify_client(0, att_connection.con_handle, att_handle);
3753deb3ec6SMatthias Ringwald                 return;
3763deb3ec6SMatthias Ringwald             }
3773deb3ec6SMatthias Ringwald 
3786428eb5aSMatthias Ringwald             // directly process command
3796428eb5aSMatthias Ringwald             // note: signed write cannot be handled directly as authentication needs to be verified
3806428eb5aSMatthias Ringwald             if (packet[0] == ATT_WRITE_COMMAND){
3816428eb5aSMatthias Ringwald                 att_handle_request(&att_connection, packet, size, 0);
3826428eb5aSMatthias Ringwald                 return;
3836428eb5aSMatthias Ringwald             }
3846428eb5aSMatthias Ringwald 
3853deb3ec6SMatthias Ringwald             // check size
3866428eb5aSMatthias Ringwald             if (size > sizeof(att_request_buffer)) {
3876428eb5aSMatthias 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));
3886428eb5aSMatthias Ringwald                 return;
3896428eb5aSMatthias Ringwald             }
3903deb3ec6SMatthias Ringwald 
3913deb3ec6SMatthias Ringwald             // last request still in processing?
3926428eb5aSMatthias Ringwald             if (att_server_state != ATT_SERVER_IDLE){
3936428eb5aSMatthias Ringwald                 log_info("att_packet_handler: skipping att pdu 0x%02x as server not idle (state %u)", packet[0], att_server_state);
3946428eb5aSMatthias Ringwald                 return;
3956428eb5aSMatthias Ringwald             }
3963deb3ec6SMatthias Ringwald 
3973deb3ec6SMatthias Ringwald             // store request
3983deb3ec6SMatthias Ringwald             att_server_state = ATT_SERVER_REQUEST_RECEIVED;
3993deb3ec6SMatthias Ringwald             att_request_size = size;
4003deb3ec6SMatthias Ringwald             memcpy(att_request_buffer, packet, size);
4013deb3ec6SMatthias Ringwald 
4023deb3ec6SMatthias Ringwald             att_run();
403372d62c4SMatthias Ringwald             break;
404372d62c4SMatthias Ringwald     }
4053deb3ec6SMatthias Ringwald }
4063deb3ec6SMatthias Ringwald 
4073deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){
4083deb3ec6SMatthias Ringwald 
4091a389cdcSMatthias Ringwald     // register for HCI Events
410d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &att_event_packet_handler;
4111a389cdcSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
4121a389cdcSMatthias Ringwald 
413406722e4SMatthias Ringwald     // register for SM events
414d9a7306aSMatthias Ringwald     sm_event_callback_registration.callback = &att_event_packet_handler;
415406722e4SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
4163deb3ec6SMatthias Ringwald 
4171a389cdcSMatthias Ringwald     // and L2CAP ATT Server PDUs
4183deb3ec6SMatthias Ringwald     att_dispatch_register_server(att_packet_handler);
4193deb3ec6SMatthias Ringwald 
4203deb3ec6SMatthias Ringwald     att_server_state = ATT_SERVER_IDLE;
4213deb3ec6SMatthias Ringwald     att_set_db(db);
4223deb3ec6SMatthias Ringwald     att_set_read_callback(read_callback);
4233deb3ec6SMatthias Ringwald     att_set_write_callback(write_callback);
4243deb3ec6SMatthias Ringwald 
4253deb3ec6SMatthias Ringwald }
4263deb3ec6SMatthias Ringwald 
4273deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){
4283deb3ec6SMatthias Ringwald     att_client_packet_handler = handler;
4293deb3ec6SMatthias Ringwald }
4303deb3ec6SMatthias Ringwald 
431c141988cSMatthias Ringwald // NOTE: con_handle is ignored for now as only a single connection is supported
432c141988cSMatthias Ringwald // TODO: support multiple connections
433c141988cSMatthias Ringwald 
434c141988cSMatthias Ringwald int  att_server_can_send_packet_now(hci_con_handle_t con_handle){
4353deb3ec6SMatthias Ringwald 	if (att_connection.con_handle == 0) return 0;
4360b9d7e78SMatthias Ringwald 	return att_dispatch_server_can_send_now(att_connection.con_handle);
4371b96b007SMatthias Ringwald }
4381b96b007SMatthias Ringwald 
439c141988cSMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){
4401b96b007SMatthias Ringwald     att_client_waiting_for_can_send = 1;
4410b9d7e78SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(att_connection.con_handle);
4423deb3ec6SMatthias Ringwald }
4433deb3ec6SMatthias Ringwald 
444c141988cSMatthias Ringwald int att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){
44519448c0bSMatthias Ringwald     if (!att_dispatch_server_can_send_now(att_connection.con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
4463deb3ec6SMatthias Ringwald 
4473deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
4483deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
449fc64f94aSMatthias Ringwald     uint16_t size = att_prepare_handle_value_notification(&att_connection, attribute_handle, value, value_len, packet_buffer);
4503deb3ec6SMatthias Ringwald 	return l2cap_send_prepared_connectionless(att_connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
4513deb3ec6SMatthias Ringwald }
4523deb3ec6SMatthias Ringwald 
453c141988cSMatthias Ringwald int att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){
4543deb3ec6SMatthias Ringwald     if (att_handle_value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PORGRESS;
45519448c0bSMatthias Ringwald     if (!att_dispatch_server_can_send_now(att_connection.con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
4563deb3ec6SMatthias Ringwald 
4573deb3ec6SMatthias Ringwald     // track indication
458fc64f94aSMatthias Ringwald     att_handle_value_indication_handle = attribute_handle;
459528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer_handler(&att_handle_value_indication_timer, att_handle_value_indication_timeout);
460528a4a3bSMatthias Ringwald     btstack_run_loop_set_timer(&att_handle_value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS);
461528a4a3bSMatthias Ringwald     btstack_run_loop_add_timer(&att_handle_value_indication_timer);
4623deb3ec6SMatthias Ringwald 
4633deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
4643deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
465fc64f94aSMatthias Ringwald     uint16_t size = att_prepare_handle_value_indication(&att_connection, attribute_handle, value, value_len, packet_buffer);
4663deb3ec6SMatthias Ringwald 	l2cap_send_prepared_connectionless(att_connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
4673deb3ec6SMatthias Ringwald     return 0;
4683deb3ec6SMatthias Ringwald }
469