xref: /btstack/src/ble/att_server.c (revision 9ec2630ce4bfd36adcb438d2e8ba6fc26890f857)
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 
6618707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server);
673deb3ec6SMatthias Ringwald 
6818707219SMatthias Ringwald // global
691a389cdcSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
70406722e4SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration;
713deb3ec6SMatthias Ringwald static btstack_packet_handler_t               att_client_packet_handler = NULL;
72bb38f057SMatthias Ringwald static btstack_linked_list_t                  can_send_now_clients;
7318707219SMatthias Ringwald static uint8_t                                att_client_waiting_for_can_send;
7418707219SMatthias Ringwald 
7518707219SMatthias Ringwald static att_server_t * att_server_for_handle(hci_con_handle_t con_handle){
7618707219SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
7718707219SMatthias Ringwald     if (!hci_connection) return NULL;
7818707219SMatthias Ringwald     return &hci_connection->att_server;
7918707219SMatthias Ringwald }
8018707219SMatthias Ringwald 
8118707219SMatthias Ringwald static att_server_t * att_server_for_state(att_server_state_t state){
8218707219SMatthias Ringwald     btstack_linked_list_iterator_t it;
8318707219SMatthias Ringwald     hci_connections_get_iterator(&it);
8418707219SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
8518707219SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
8618707219SMatthias Ringwald         att_server_t * att_server = &connection->att_server;
8718707219SMatthias Ringwald         if (att_server->state == state) return att_server;
8818707219SMatthias Ringwald     }
8918707219SMatthias Ringwald     return NULL;
9018707219SMatthias Ringwald }
91bb38f057SMatthias Ringwald 
923deb3ec6SMatthias Ringwald static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){
933deb3ec6SMatthias Ringwald     if (!att_client_packet_handler) return;
943deb3ec6SMatthias Ringwald 
953deb3ec6SMatthias Ringwald     uint8_t event[7];
963deb3ec6SMatthias Ringwald     int pos = 0;
975611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE;
983deb3ec6SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
993deb3ec6SMatthias Ringwald     event[pos++] = status;
100f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, client_handle);
1013deb3ec6SMatthias Ringwald     pos += 2;
102f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, attribute_handle);
1033deb3ec6SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1043deb3ec6SMatthias Ringwald }
1053deb3ec6SMatthias Ringwald 
106fc64f94aSMatthias Ringwald static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){
1073deb3ec6SMatthias Ringwald     if (!att_client_packet_handler) return;
1083deb3ec6SMatthias Ringwald 
1093deb3ec6SMatthias Ringwald     uint8_t event[6];
1103deb3ec6SMatthias Ringwald     int pos = 0;
1115611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE;
1123deb3ec6SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
113fc64f94aSMatthias Ringwald     little_endian_store_16(event, pos, con_handle);
1143deb3ec6SMatthias Ringwald     pos += 2;
115f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, mtu);
1163deb3ec6SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1173deb3ec6SMatthias Ringwald }
1183deb3ec6SMatthias Ringwald 
1191b96b007SMatthias Ringwald static void att_emit_can_send_now_event(void){
1203c97b242SMatthias Ringwald     if (!att_client_packet_handler) return;
1213c97b242SMatthias Ringwald 
1221b96b007SMatthias Ringwald     uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0};
1231b96b007SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1241b96b007SMatthias Ringwald }
1251b96b007SMatthias Ringwald 
126ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){
12718707219SMatthias Ringwald     hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) btstack_run_loop_get_timer_context(ts);
12818707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
12918707219SMatthias Ringwald     if (!att_server) return;
13018707219SMatthias Ringwald     uint16_t att_handle = att_server->value_indication_handle;
13118707219SMatthias Ringwald     att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_server->connection.con_handle, att_handle);
1323deb3ec6SMatthias Ringwald }
1333deb3ec6SMatthias Ringwald 
1343deb3ec6SMatthias Ringwald static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
135*9ec2630cSMatthias Ringwald 
136*9ec2630cSMatthias Ringwald     UNUSED(channel);
137*9ec2630cSMatthias Ringwald     UNUSED(size);
1383deb3ec6SMatthias Ringwald 
13918707219SMatthias Ringwald     att_server_t * att_server;
14018707219SMatthias Ringwald     hci_con_handle_t con_handle;
14118707219SMatthias Ringwald 
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:
15018707219SMatthias Ringwald                             con_handle = little_endian_read_16(packet, 4);
15118707219SMatthias Ringwald                             att_server = att_server_for_handle(con_handle);
15218707219SMatthias Ringwald                             if (!att_server) break;
1533deb3ec6SMatthias Ringwald                         	// store connection info
15418707219SMatthias Ringwald                         	att_server->peer_addr_type = packet[7];
15518707219SMatthias Ringwald                             reverse_bd_addr(&packet[8], att_server->peer_address);
15618707219SMatthias Ringwald                             att_server->connection.con_handle = con_handle;
1573deb3ec6SMatthias Ringwald                             // reset connection properties
15818707219SMatthias Ringwald                             att_server->state = ATT_SERVER_IDLE;
15918707219SMatthias Ringwald                             att_server->connection.mtu = ATT_DEFAULT_MTU;
16018707219SMatthias Ringwald                             att_server->connection.max_mtu = l2cap_max_le_mtu();
16118707219SMatthias Ringwald                             if (att_server->connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){
16218707219SMatthias Ringwald                                 att_server->connection.max_mtu = ATT_REQUEST_BUFFER_SIZE;
16399c58ad0SMatthias Ringwald                             }
16418707219SMatthias Ringwald                             att_server->connection.encryption_key_size = 0;
16518707219SMatthias Ringwald                             att_server->connection.authenticated = 0;
16618707219SMatthias Ringwald 		                	att_server->connection.authorized = 0;
16718707219SMatthias Ringwald                             att_server->ir_le_device_db_index = -1;
1683deb3ec6SMatthias Ringwald                             break;
1693deb3ec6SMatthias Ringwald 
1703deb3ec6SMatthias Ringwald                         default:
1713deb3ec6SMatthias Ringwald                             break;
1723deb3ec6SMatthias Ringwald                     }
1733deb3ec6SMatthias Ringwald                     break;
1743deb3ec6SMatthias Ringwald 
1753deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_CHANGE:
1763deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE:
1773deb3ec6SMatthias Ringwald                 	// check handle
17818707219SMatthias Ringwald                     con_handle = little_endian_read_16(packet, 3);
17918707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
18018707219SMatthias Ringwald                     if (!att_server) break;
18118707219SMatthias Ringwald                 	att_server->connection.encryption_key_size = sm_encryption_key_size(con_handle);
18218707219SMatthias Ringwald                 	att_server->connection.authenticated = sm_authenticated(con_handle);
1833deb3ec6SMatthias Ringwald                 	break;
1843deb3ec6SMatthias Ringwald 
1853deb3ec6SMatthias Ringwald                 case HCI_EVENT_DISCONNECTION_COMPLETE:
18670a390c7SMatthias Ringwald                     // check handle
18718707219SMatthias Ringwald                     con_handle = hci_event_disconnection_complete_get_connection_handle(packet);
18818707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
18918707219SMatthias Ringwald                     if (!att_server) break;
19018707219SMatthias Ringwald                     att_clear_transaction_queue(&att_server->connection);
19118707219SMatthias Ringwald                     att_server->connection.con_handle = 0;
19218707219SMatthias Ringwald                     att_server->value_indication_handle = 0; // reset error state
19318707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
1943deb3ec6SMatthias Ringwald                     break;
1953deb3ec6SMatthias Ringwald 
1965611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_STARTED:
19718707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_started_get_handle(packet);
19818707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
19918707219SMatthias Ringwald                     if (!att_server) break;
2005611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED");
20118707219SMatthias Ringwald                     att_server->ir_lookup_active = 1;
2023deb3ec6SMatthias Ringwald                     break;
2035611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
20418707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_succeeded_get_handle(packet);
20518707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
20618707219SMatthias Ringwald                     if (!att_server) break;
20718707219SMatthias Ringwald                     att_server->ir_lookup_active = 0;
20818707219SMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index_internal(packet);
20918707219SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED id %u", att_server->ir_le_device_db_index);
21018707219SMatthias Ringwald                     att_run_for_context(att_server);
2113deb3ec6SMatthias Ringwald                     break;
2125611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_FAILED:
21318707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_failed_get_handle(packet);
21418707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
21518707219SMatthias Ringwald                     if (!att_server) break;
2165611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED");
21718707219SMatthias Ringwald                     att_server->ir_lookup_active = 0;
21818707219SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
21918707219SMatthias Ringwald                     att_run_for_context(att_server);
2203deb3ec6SMatthias Ringwald                     break;
2215611a760SMatthias Ringwald                 case SM_EVENT_AUTHORIZATION_RESULT: {
22218707219SMatthias Ringwald                     con_handle = sm_event_authorization_result_get_handle(packet);
22318707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
22418707219SMatthias Ringwald                     if (!att_server) break;
22518707219SMatthias Ringwald                     att_server->connection.authorized = sm_event_authorization_result_get_authorization_result(packet);
22618707219SMatthias Ringwald                     att_dispatch_server_request_can_send_now_event(con_handle);
2273deb3ec6SMatthias Ringwald                 	break;
2283deb3ec6SMatthias Ringwald                 }
2293deb3ec6SMatthias Ringwald                 default:
2303deb3ec6SMatthias Ringwald                     break;
2313deb3ec6SMatthias Ringwald             }
23265b44ffdSMatthias Ringwald             break;
23365b44ffdSMatthias Ringwald         default:
23465b44ffdSMatthias Ringwald             break;
2353deb3ec6SMatthias Ringwald     }
2363deb3ec6SMatthias Ringwald }
2373deb3ec6SMatthias Ringwald 
2383deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
2393deb3ec6SMatthias Ringwald 
24018707219SMatthias Ringwald     att_server_t * att_server = att_server_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION);
24118707219SMatthias Ringwald     if (!att_server) return;
2423deb3ec6SMatthias Ringwald 
2433deb3ec6SMatthias Ringwald     uint8_t hash_flipped[8];
2449c80e4ccSMatthias Ringwald     reverse_64(hash, hash_flipped);
24518707219SMatthias Ringwald     if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8)){
2463deb3ec6SMatthias Ringwald         log_info("ATT Signed Write, invalid signature");
24718707219SMatthias Ringwald         att_server->state = ATT_SERVER_IDLE;
2483deb3ec6SMatthias Ringwald         return;
2493deb3ec6SMatthias Ringwald     }
2503deb3ec6SMatthias Ringwald     log_info("ATT Signed Write, valid signature");
2513deb3ec6SMatthias Ringwald 
2523deb3ec6SMatthias Ringwald     // update sequence number
25318707219SMatthias Ringwald     uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
25418707219SMatthias Ringwald     le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1);
25518707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
25618707219SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
2573deb3ec6SMatthias Ringwald }
2583deb3ec6SMatthias Ringwald 
25988a48dd8SMatthias Ringwald 
26018707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED
261b06f2579SMatthias Ringwald // pre: can send now
262b06f2579SMatthias Ringwald // returns: 1 if packet was sent
26318707219SMatthias Ringwald static int att_server_process_validated_request(att_server_t * att_server){
264b06f2579SMatthias Ringwald 
265b06f2579SMatthias Ringwald     l2cap_reserve_packet_buffer();
266b06f2579SMatthias Ringwald     uint8_t * att_response_buffer = l2cap_get_outgoing_buffer();
26718707219SMatthias Ringwald     uint16_t  att_response_size   = att_handle_request(&att_server->connection, att_server->request_buffer, att_server->request_size, att_response_buffer);
268b06f2579SMatthias Ringwald 
269b06f2579SMatthias Ringwald     // intercept "insufficient authorization" for authenticated connections to allow for user authorization
270b06f2579SMatthias Ringwald     if ((att_response_size     >= 4)
271b06f2579SMatthias Ringwald     && (att_response_buffer[0] == ATT_ERROR_RESPONSE)
272b06f2579SMatthias Ringwald     && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION)
27318707219SMatthias Ringwald     && (att_server->connection.authenticated)){
274b06f2579SMatthias Ringwald 
27518707219SMatthias Ringwald         switch (sm_authorization_state(att_server->connection.con_handle)){
276b06f2579SMatthias Ringwald             case AUTHORIZATION_UNKNOWN:
277b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
27818707219SMatthias Ringwald                 sm_request_pairing(att_server->connection.con_handle);
279b06f2579SMatthias Ringwald                 return 0;
280b06f2579SMatthias Ringwald             case AUTHORIZATION_PENDING:
281b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
282b06f2579SMatthias Ringwald                 return 0;
283b06f2579SMatthias Ringwald             default:
284b06f2579SMatthias Ringwald                 break;
285b06f2579SMatthias Ringwald         }
286b06f2579SMatthias Ringwald     }
287b06f2579SMatthias Ringwald 
28818707219SMatthias Ringwald     att_server->state = ATT_SERVER_IDLE;
289b06f2579SMatthias Ringwald     if (att_response_size == 0) {
290b06f2579SMatthias Ringwald         l2cap_release_packet_buffer();
291b06f2579SMatthias Ringwald         return 0;
292b06f2579SMatthias Ringwald     }
293b06f2579SMatthias Ringwald 
29418707219SMatthias Ringwald     l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size);
295b06f2579SMatthias Ringwald 
296b06f2579SMatthias Ringwald     // notify client about MTU exchange result
297b06f2579SMatthias Ringwald     if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){
29818707219SMatthias Ringwald         att_emit_mtu_event(att_server->connection.con_handle, att_server->connection.mtu);
299b06f2579SMatthias Ringwald     }
300b06f2579SMatthias Ringwald     return 1;
301b06f2579SMatthias Ringwald }
302b06f2579SMatthias Ringwald 
30318707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server){
30418707219SMatthias Ringwald     switch (att_server->state){
3053deb3ec6SMatthias Ringwald         case ATT_SERVER_REQUEST_RECEIVED:
30618707219SMatthias Ringwald             if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){
3073deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write!");
3083deb3ec6SMatthias Ringwald                 if (!sm_cmac_ready()) {
3093deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, sm_cmac engine not ready. Abort");
31018707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
3113deb3ec6SMatthias Ringwald                     return;
3123deb3ec6SMatthias Ringwald                 }
31318707219SMatthias Ringwald                 if (att_server->request_size < (3 + 12)) {
3143deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, request to short. Abort.");
31518707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
3163deb3ec6SMatthias Ringwald                     return;
3173deb3ec6SMatthias Ringwald                 }
31818707219SMatthias Ringwald                 if (att_server->ir_lookup_active){
3193deb3ec6SMatthias Ringwald                     return;
3203deb3ec6SMatthias Ringwald                 }
32118707219SMatthias Ringwald                 if (att_server->ir_le_device_db_index < 0){
3223deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, CSRK not available");
32318707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
3243deb3ec6SMatthias Ringwald                     return;
3253deb3ec6SMatthias Ringwald                 }
3263deb3ec6SMatthias Ringwald 
3273deb3ec6SMatthias Ringwald                 // check counter
32818707219SMatthias Ringwald                 uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
32918707219SMatthias Ringwald                 uint32_t counter_db     = le_device_db_remote_counter_get(att_server->ir_le_device_db_index);
3303deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet);
3313deb3ec6SMatthias Ringwald                 if (counter_packet < counter_db){
3323deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, db reports higher counter, abort");
33318707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
3343deb3ec6SMatthias Ringwald                     return;
3353deb3ec6SMatthias Ringwald                 }
3363deb3ec6SMatthias Ringwald 
3373deb3ec6SMatthias Ringwald                 // signature is { sequence counter, secure hash }
3383deb3ec6SMatthias Ringwald                 sm_key_t csrk;
33918707219SMatthias Ringwald                 le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk);
34018707219SMatthias Ringwald                 att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION;
3413deb3ec6SMatthias Ringwald                 log_info("Orig Signature: ");
34218707219SMatthias Ringwald                 log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8);
34318707219SMatthias Ringwald                 uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1);
34418707219SMatthias Ringwald                 sm_cmac_signed_write_start(csrk, att_server->request_buffer[0], attribute_handle, att_server->request_size - 15, &att_server->request_buffer[3], counter_packet, att_signed_write_handle_cmac_result);
3453deb3ec6SMatthias Ringwald                 return;
3463deb3ec6SMatthias Ringwald             }
3473deb3ec6SMatthias Ringwald 
348b06f2579SMatthias Ringwald             // move on
34918707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
35018707219SMatthias Ringwald             att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
351b06f2579SMatthias Ringwald             break;
35288a48dd8SMatthias Ringwald 
3533deb3ec6SMatthias Ringwald         default:
3543deb3ec6SMatthias Ringwald             break;
3553deb3ec6SMatthias Ringwald     }
3563deb3ec6SMatthias Ringwald }
3573deb3ec6SMatthias Ringwald 
3587eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){
359b06f2579SMatthias Ringwald 
360b06f2579SMatthias Ringwald     // NOTE: we get l2cap fixed channel instead of con_handle
361b06f2579SMatthias Ringwald 
36218707219SMatthias Ringwald     btstack_linked_list_iterator_t it;
36318707219SMatthias Ringwald     hci_connections_get_iterator(&it);
36418707219SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
36518707219SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
36618707219SMatthias Ringwald         att_server_t * att_server = &connection->att_server;
36718707219SMatthias Ringwald         if (att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED){
36818707219SMatthias Ringwald             int sent = att_server_process_validated_request(att_server);
369bb38f057SMatthias Ringwald             if (sent && (att_client_waiting_for_can_send || !btstack_linked_list_empty(&can_send_now_clients))){
37018707219SMatthias Ringwald                 att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
3713deb3ec6SMatthias Ringwald                 return;
3723deb3ec6SMatthias Ringwald             }
3733deb3ec6SMatthias Ringwald         }
37418707219SMatthias Ringwald     }
3753deb3ec6SMatthias Ringwald 
376bb38f057SMatthias Ringwald     while (!btstack_linked_list_empty(&can_send_now_clients)){
377bb38f057SMatthias Ringwald         // handle first client
378bb38f057SMatthias Ringwald         btstack_context_callback_registration_t * client = (btstack_context_callback_registration_t*) can_send_now_clients;
37918707219SMatthias Ringwald         hci_con_handle_t con_handle = (uintptr_t) client->context;
380bb38f057SMatthias Ringwald         btstack_linked_list_remove(&can_send_now_clients, (btstack_linked_item_t *) client);
381bb38f057SMatthias Ringwald         client->callback(client->context);
382bb38f057SMatthias Ringwald 
383bb38f057SMatthias Ringwald         // request again if needed
38418707219SMatthias Ringwald         if (!att_dispatch_server_can_send_now(con_handle)){
385bb38f057SMatthias Ringwald             if (!btstack_linked_list_empty(&can_send_now_clients) || att_client_waiting_for_can_send){
38618707219SMatthias Ringwald                 att_dispatch_server_request_can_send_now_event(con_handle);
387bb38f057SMatthias Ringwald             }
388bb38f057SMatthias Ringwald             return;
389bb38f057SMatthias Ringwald         }
390bb38f057SMatthias Ringwald     }
391bb38f057SMatthias Ringwald 
392b06f2579SMatthias Ringwald     if (att_client_waiting_for_can_send){
393b06f2579SMatthias Ringwald         att_client_waiting_for_can_send = 0;
394b06f2579SMatthias Ringwald         att_emit_can_send_now_event();
3953deb3ec6SMatthias Ringwald     }
3963deb3ec6SMatthias Ringwald }
3973deb3ec6SMatthias Ringwald 
39888a48dd8SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
39918707219SMatthias Ringwald 
40018707219SMatthias Ringwald     att_server_t * att_server;
40118707219SMatthias Ringwald 
40288a48dd8SMatthias Ringwald     switch (packet_type){
40388a48dd8SMatthias Ringwald 
40488a48dd8SMatthias Ringwald         case HCI_EVENT_PACKET:
40588a48dd8SMatthias Ringwald             if (packet[0] != L2CAP_EVENT_CAN_SEND_NOW) break;
4064d2be802SMatthias Ringwald             att_server_handle_can_send_now();
407372d62c4SMatthias Ringwald             break;
4083deb3ec6SMatthias Ringwald 
409372d62c4SMatthias Ringwald         case ATT_DATA_PACKET:
41018707219SMatthias Ringwald             log_info("ATT Packet, handle 0x%04x", handle);
41118707219SMatthias Ringwald             att_server = att_server_for_handle(handle);
41218707219SMatthias Ringwald             if (!att_server) break;
41318707219SMatthias Ringwald 
4143deb3ec6SMatthias Ringwald             // handle value indication confirms
41518707219SMatthias Ringwald             if (packet[0] == ATT_HANDLE_VALUE_CONFIRMATION && att_server->value_indication_handle){
41618707219SMatthias Ringwald                 btstack_run_loop_remove_timer(&att_server->value_indication_timer);
41718707219SMatthias Ringwald                 uint16_t att_handle = att_server->value_indication_handle;
41818707219SMatthias Ringwald                 att_server->value_indication_handle = 0;
41918707219SMatthias Ringwald                 att_handle_value_indication_notify_client(0, att_server->connection.con_handle, att_handle);
4203deb3ec6SMatthias Ringwald                 return;
4213deb3ec6SMatthias Ringwald             }
4223deb3ec6SMatthias Ringwald 
4236428eb5aSMatthias Ringwald             // directly process command
4246428eb5aSMatthias Ringwald             // note: signed write cannot be handled directly as authentication needs to be verified
4256428eb5aSMatthias Ringwald             if (packet[0] == ATT_WRITE_COMMAND){
42618707219SMatthias Ringwald                 att_handle_request(&att_server->connection, packet, size, 0);
4276428eb5aSMatthias Ringwald                 return;
4286428eb5aSMatthias Ringwald             }
4296428eb5aSMatthias Ringwald 
4303deb3ec6SMatthias Ringwald             // check size
43118707219SMatthias Ringwald             if (size > sizeof(att_server->request_buffer)) {
43218707219SMatthias Ringwald                 log_info("att_packet_handler: dropping att pdu 0x%02x as size %u > att_server->request_buffer %u", packet[0], size, (int) sizeof(att_server->request_buffer));
4336428eb5aSMatthias Ringwald                 return;
4346428eb5aSMatthias Ringwald             }
4353deb3ec6SMatthias Ringwald 
4363deb3ec6SMatthias Ringwald             // last request still in processing?
43718707219SMatthias Ringwald             if (att_server->state != ATT_SERVER_IDLE){
43818707219SMatthias Ringwald                 log_info("att_packet_handler: skipping att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state);
4396428eb5aSMatthias Ringwald                 return;
4406428eb5aSMatthias Ringwald             }
4413deb3ec6SMatthias Ringwald 
4423deb3ec6SMatthias Ringwald             // store request
44318707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED;
44418707219SMatthias Ringwald             att_server->request_size = size;
44518707219SMatthias Ringwald             memcpy(att_server->request_buffer, packet, size);
4463deb3ec6SMatthias Ringwald 
44718707219SMatthias Ringwald             att_run_for_context(att_server);
448372d62c4SMatthias Ringwald             break;
449372d62c4SMatthias Ringwald     }
4503deb3ec6SMatthias Ringwald }
4513deb3ec6SMatthias Ringwald 
4523deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){
4533deb3ec6SMatthias Ringwald 
4541a389cdcSMatthias Ringwald     // register for HCI Events
455d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &att_event_packet_handler;
4561a389cdcSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
4571a389cdcSMatthias Ringwald 
458406722e4SMatthias Ringwald     // register for SM events
459d9a7306aSMatthias Ringwald     sm_event_callback_registration.callback = &att_event_packet_handler;
460406722e4SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
4613deb3ec6SMatthias Ringwald 
4621a389cdcSMatthias Ringwald     // and L2CAP ATT Server PDUs
4633deb3ec6SMatthias Ringwald     att_dispatch_register_server(att_packet_handler);
4643deb3ec6SMatthias Ringwald 
4653deb3ec6SMatthias Ringwald     att_set_db(db);
4663deb3ec6SMatthias Ringwald     att_set_read_callback(read_callback);
4673deb3ec6SMatthias Ringwald     att_set_write_callback(write_callback);
4683deb3ec6SMatthias Ringwald 
4693deb3ec6SMatthias Ringwald }
4703deb3ec6SMatthias Ringwald 
4713deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){
4723deb3ec6SMatthias Ringwald     att_client_packet_handler = handler;
4733deb3ec6SMatthias Ringwald }
4743deb3ec6SMatthias Ringwald 
475c141988cSMatthias Ringwald int  att_server_can_send_packet_now(hci_con_handle_t con_handle){
47618707219SMatthias Ringwald 	return att_dispatch_server_can_send_now(con_handle);
4771b96b007SMatthias Ringwald }
4781b96b007SMatthias Ringwald 
479c141988cSMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){
48018707219SMatthias Ringwald     log_info("att_server_request_can_send_now_event 0x%04x", con_handle);
4811b96b007SMatthias Ringwald     att_client_waiting_for_can_send = 1;
48218707219SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
4833deb3ec6SMatthias Ringwald }
4843deb3ec6SMatthias Ringwald 
485bb38f057SMatthias Ringwald void att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
486bb38f057SMatthias Ringwald     // check if can send already
487bb38f057SMatthias Ringwald     if (att_dispatch_server_can_send_now(con_handle)){
488bb38f057SMatthias Ringwald         callback_registration->callback(callback_registration->context);
489bb38f057SMatthias Ringwald         return;
490bb38f057SMatthias Ringwald     }
49118707219SMatthias Ringwald     callback_registration->context = (void*)(uintptr_t) con_handle;
492bb38f057SMatthias Ringwald     btstack_linked_list_add_tail(&can_send_now_clients, (btstack_linked_item_t*) callback_registration);
493bb38f057SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
494bb38f057SMatthias Ringwald }
495bb38f057SMatthias Ringwald 
49618707219SMatthias Ringwald 
497c141988cSMatthias Ringwald int att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){
49818707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
49918707219SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
50018707219SMatthias Ringwald 
50118707219SMatthias Ringwald     if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
5023deb3ec6SMatthias Ringwald 
5033deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
5043deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
50518707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_notification(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
50618707219SMatthias Ringwald 	return l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
5073deb3ec6SMatthias Ringwald }
5083deb3ec6SMatthias Ringwald 
509c141988cSMatthias Ringwald int att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){
51018707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
51118707219SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
51218707219SMatthias Ringwald 
51318707219SMatthias Ringwald     if (att_server->value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PORGRESS;
51418707219SMatthias Ringwald     if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
5153deb3ec6SMatthias Ringwald 
5163deb3ec6SMatthias Ringwald     // track indication
51718707219SMatthias Ringwald     att_server->value_indication_handle = attribute_handle;
51818707219SMatthias Ringwald     btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout);
51918707219SMatthias Ringwald     btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS);
52018707219SMatthias Ringwald     btstack_run_loop_add_timer(&att_server->value_indication_timer);
5213deb3ec6SMatthias Ringwald 
5223deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
5233deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
52418707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_indication(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
52518707219SMatthias Ringwald 	l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
5263deb3ec6SMatthias Ringwald     return 0;
5273deb3ec6SMatthias Ringwald }
528