xref: /btstack/src/ble/att_server.c (revision 18707219a51938aac02df55689181d26a61334f9)
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 
66*18707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server);
673deb3ec6SMatthias Ringwald 
68*18707219SMatthias 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;
73*18707219SMatthias Ringwald static uint8_t                                att_client_waiting_for_can_send;
74*18707219SMatthias Ringwald 
75*18707219SMatthias Ringwald static att_server_t * att_server_for_handle(hci_con_handle_t con_handle){
76*18707219SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
77*18707219SMatthias Ringwald     if (!hci_connection) return NULL;
78*18707219SMatthias Ringwald     return &hci_connection->att_server;
79*18707219SMatthias Ringwald }
80*18707219SMatthias Ringwald 
81*18707219SMatthias Ringwald static att_server_t * att_server_for_state(att_server_state_t state){
82*18707219SMatthias Ringwald     btstack_linked_list_iterator_t it;
83*18707219SMatthias Ringwald     hci_connections_get_iterator(&it);
84*18707219SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
85*18707219SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
86*18707219SMatthias Ringwald         att_server_t * att_server = &connection->att_server;
87*18707219SMatthias Ringwald         if (att_server->state == state) return att_server;
88*18707219SMatthias Ringwald     }
89*18707219SMatthias Ringwald     return NULL;
90*18707219SMatthias 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){
127*18707219SMatthias Ringwald     hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) btstack_run_loop_get_timer_context(ts);
128*18707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
129*18707219SMatthias Ringwald     if (!att_server) return;
130*18707219SMatthias Ringwald     uint16_t att_handle = att_server->value_indication_handle;
131*18707219SMatthias 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){
1353deb3ec6SMatthias Ringwald 
136*18707219SMatthias Ringwald     att_server_t * att_server;
137*18707219SMatthias Ringwald     hci_con_handle_t con_handle;
138*18707219SMatthias Ringwald 
1393deb3ec6SMatthias Ringwald     switch (packet_type) {
1403deb3ec6SMatthias Ringwald 
1413deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1420e2df43fSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
1433deb3ec6SMatthias Ringwald 
1443deb3ec6SMatthias Ringwald                 case HCI_EVENT_LE_META:
1453deb3ec6SMatthias Ringwald                     switch (packet[2]) {
1463deb3ec6SMatthias Ringwald                         case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
147*18707219SMatthias Ringwald                             con_handle = little_endian_read_16(packet, 4);
148*18707219SMatthias Ringwald                             att_server = att_server_for_handle(con_handle);
149*18707219SMatthias Ringwald                             if (!att_server) break;
1503deb3ec6SMatthias Ringwald                         	// store connection info
151*18707219SMatthias Ringwald                         	att_server->peer_addr_type = packet[7];
152*18707219SMatthias Ringwald                             reverse_bd_addr(&packet[8], att_server->peer_address);
153*18707219SMatthias Ringwald                             att_server->connection.con_handle = con_handle;
1543deb3ec6SMatthias Ringwald                             // reset connection properties
155*18707219SMatthias Ringwald                             att_server->state = ATT_SERVER_IDLE;
156*18707219SMatthias Ringwald                             att_server->connection.mtu = ATT_DEFAULT_MTU;
157*18707219SMatthias Ringwald                             att_server->connection.max_mtu = l2cap_max_le_mtu();
158*18707219SMatthias Ringwald                             if (att_server->connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){
159*18707219SMatthias Ringwald                                 att_server->connection.max_mtu = ATT_REQUEST_BUFFER_SIZE;
16099c58ad0SMatthias Ringwald                             }
161*18707219SMatthias Ringwald                             att_server->connection.encryption_key_size = 0;
162*18707219SMatthias Ringwald                             att_server->connection.authenticated = 0;
163*18707219SMatthias Ringwald 		                	att_server->connection.authorized = 0;
164*18707219SMatthias Ringwald                             att_server->ir_le_device_db_index = -1;
1653deb3ec6SMatthias Ringwald                             break;
1663deb3ec6SMatthias Ringwald 
1673deb3ec6SMatthias Ringwald                         default:
1683deb3ec6SMatthias Ringwald                             break;
1693deb3ec6SMatthias Ringwald                     }
1703deb3ec6SMatthias Ringwald                     break;
1713deb3ec6SMatthias Ringwald 
1723deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_CHANGE:
1733deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE:
1743deb3ec6SMatthias Ringwald                 	// check handle
175*18707219SMatthias Ringwald                     con_handle = little_endian_read_16(packet, 3);
176*18707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
177*18707219SMatthias Ringwald                     if (!att_server) break;
178*18707219SMatthias Ringwald                 	att_server->connection.encryption_key_size = sm_encryption_key_size(con_handle);
179*18707219SMatthias Ringwald                 	att_server->connection.authenticated = sm_authenticated(con_handle);
1803deb3ec6SMatthias Ringwald                 	break;
1813deb3ec6SMatthias Ringwald 
1823deb3ec6SMatthias Ringwald                 case HCI_EVENT_DISCONNECTION_COMPLETE:
18370a390c7SMatthias Ringwald                     // check handle
184*18707219SMatthias Ringwald                     con_handle = hci_event_disconnection_complete_get_connection_handle(packet);
185*18707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
186*18707219SMatthias Ringwald                     if (!att_server) break;
187*18707219SMatthias Ringwald                     att_clear_transaction_queue(&att_server->connection);
188*18707219SMatthias Ringwald                     att_server->connection.con_handle = 0;
189*18707219SMatthias Ringwald                     att_server->value_indication_handle = 0; // reset error state
190*18707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
1913deb3ec6SMatthias Ringwald                     break;
1923deb3ec6SMatthias Ringwald 
1935611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_STARTED:
194*18707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_started_get_handle(packet);
195*18707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
196*18707219SMatthias Ringwald                     if (!att_server) break;
1975611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED");
198*18707219SMatthias Ringwald                     att_server->ir_lookup_active = 1;
1993deb3ec6SMatthias Ringwald                     break;
2005611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
201*18707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_succeeded_get_handle(packet);
202*18707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
203*18707219SMatthias Ringwald                     if (!att_server) break;
204*18707219SMatthias Ringwald                     att_server->ir_lookup_active = 0;
205*18707219SMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index_internal(packet);
206*18707219SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED id %u", att_server->ir_le_device_db_index);
207*18707219SMatthias Ringwald                     att_run_for_context(att_server);
2083deb3ec6SMatthias Ringwald                     break;
2095611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_FAILED:
210*18707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_failed_get_handle(packet);
211*18707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
212*18707219SMatthias Ringwald                     if (!att_server) break;
2135611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED");
214*18707219SMatthias Ringwald                     att_server->ir_lookup_active = 0;
215*18707219SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
216*18707219SMatthias Ringwald                     att_run_for_context(att_server);
2173deb3ec6SMatthias Ringwald                     break;
2185611a760SMatthias Ringwald                 case SM_EVENT_AUTHORIZATION_RESULT: {
219*18707219SMatthias Ringwald                     con_handle = sm_event_authorization_result_get_handle(packet);
220*18707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
221*18707219SMatthias Ringwald                     if (!att_server) break;
222*18707219SMatthias Ringwald                     att_server->connection.authorized = sm_event_authorization_result_get_authorization_result(packet);
223*18707219SMatthias Ringwald                     att_dispatch_server_request_can_send_now_event(con_handle);
2243deb3ec6SMatthias Ringwald                 	break;
2253deb3ec6SMatthias Ringwald                 }
2263deb3ec6SMatthias Ringwald                 default:
2273deb3ec6SMatthias Ringwald                     break;
2283deb3ec6SMatthias Ringwald             }
22965b44ffdSMatthias Ringwald             break;
23065b44ffdSMatthias Ringwald         default:
23165b44ffdSMatthias Ringwald             break;
2323deb3ec6SMatthias Ringwald     }
2333deb3ec6SMatthias Ringwald }
2343deb3ec6SMatthias Ringwald 
2353deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
2363deb3ec6SMatthias Ringwald 
237*18707219SMatthias Ringwald     att_server_t * att_server = att_server_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION);
238*18707219SMatthias Ringwald     if (!att_server) return;
2393deb3ec6SMatthias Ringwald 
2403deb3ec6SMatthias Ringwald     uint8_t hash_flipped[8];
2419c80e4ccSMatthias Ringwald     reverse_64(hash, hash_flipped);
242*18707219SMatthias Ringwald     if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8)){
2433deb3ec6SMatthias Ringwald         log_info("ATT Signed Write, invalid signature");
244*18707219SMatthias Ringwald         att_server->state = ATT_SERVER_IDLE;
2453deb3ec6SMatthias Ringwald         return;
2463deb3ec6SMatthias Ringwald     }
2473deb3ec6SMatthias Ringwald     log_info("ATT Signed Write, valid signature");
2483deb3ec6SMatthias Ringwald 
2493deb3ec6SMatthias Ringwald     // update sequence number
250*18707219SMatthias Ringwald     uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
251*18707219SMatthias Ringwald     le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1);
252*18707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
253*18707219SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
2543deb3ec6SMatthias Ringwald }
2553deb3ec6SMatthias Ringwald 
25688a48dd8SMatthias Ringwald 
257*18707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED
258b06f2579SMatthias Ringwald // pre: can send now
259b06f2579SMatthias Ringwald // returns: 1 if packet was sent
260*18707219SMatthias Ringwald static int att_server_process_validated_request(att_server_t * att_server){
261b06f2579SMatthias Ringwald 
262b06f2579SMatthias Ringwald     l2cap_reserve_packet_buffer();
263b06f2579SMatthias Ringwald     uint8_t * att_response_buffer = l2cap_get_outgoing_buffer();
264*18707219SMatthias Ringwald     uint16_t  att_response_size   = att_handle_request(&att_server->connection, att_server->request_buffer, att_server->request_size, att_response_buffer);
265b06f2579SMatthias Ringwald 
266b06f2579SMatthias Ringwald     // intercept "insufficient authorization" for authenticated connections to allow for user authorization
267b06f2579SMatthias Ringwald     if ((att_response_size     >= 4)
268b06f2579SMatthias Ringwald     && (att_response_buffer[0] == ATT_ERROR_RESPONSE)
269b06f2579SMatthias Ringwald     && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION)
270*18707219SMatthias Ringwald     && (att_server->connection.authenticated)){
271b06f2579SMatthias Ringwald 
272*18707219SMatthias Ringwald         switch (sm_authorization_state(att_server->connection.con_handle)){
273b06f2579SMatthias Ringwald             case AUTHORIZATION_UNKNOWN:
274b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
275*18707219SMatthias Ringwald                 sm_request_pairing(att_server->connection.con_handle);
276b06f2579SMatthias Ringwald                 return 0;
277b06f2579SMatthias Ringwald             case AUTHORIZATION_PENDING:
278b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
279b06f2579SMatthias Ringwald                 return 0;
280b06f2579SMatthias Ringwald             default:
281b06f2579SMatthias Ringwald                 break;
282b06f2579SMatthias Ringwald         }
283b06f2579SMatthias Ringwald     }
284b06f2579SMatthias Ringwald 
285*18707219SMatthias Ringwald     att_server->state = ATT_SERVER_IDLE;
286b06f2579SMatthias Ringwald     if (att_response_size == 0) {
287b06f2579SMatthias Ringwald         l2cap_release_packet_buffer();
288b06f2579SMatthias Ringwald         return 0;
289b06f2579SMatthias Ringwald     }
290b06f2579SMatthias Ringwald 
291*18707219SMatthias Ringwald     l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size);
292b06f2579SMatthias Ringwald 
293b06f2579SMatthias Ringwald     // notify client about MTU exchange result
294b06f2579SMatthias Ringwald     if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){
295*18707219SMatthias Ringwald         att_emit_mtu_event(att_server->connection.con_handle, att_server->connection.mtu);
296b06f2579SMatthias Ringwald     }
297b06f2579SMatthias Ringwald     return 1;
298b06f2579SMatthias Ringwald }
299b06f2579SMatthias Ringwald 
300*18707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server){
301*18707219SMatthias Ringwald     switch (att_server->state){
3023deb3ec6SMatthias Ringwald         case ATT_SERVER_REQUEST_RECEIVED:
303*18707219SMatthias Ringwald             if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){
3043deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write!");
3053deb3ec6SMatthias Ringwald                 if (!sm_cmac_ready()) {
3063deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, sm_cmac engine not ready. Abort");
307*18707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
3083deb3ec6SMatthias Ringwald                     return;
3093deb3ec6SMatthias Ringwald                 }
310*18707219SMatthias Ringwald                 if (att_server->request_size < (3 + 12)) {
3113deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, request to short. Abort.");
312*18707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
3133deb3ec6SMatthias Ringwald                     return;
3143deb3ec6SMatthias Ringwald                 }
315*18707219SMatthias Ringwald                 if (att_server->ir_lookup_active){
3163deb3ec6SMatthias Ringwald                     return;
3173deb3ec6SMatthias Ringwald                 }
318*18707219SMatthias Ringwald                 if (att_server->ir_le_device_db_index < 0){
3193deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, CSRK not available");
320*18707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
3213deb3ec6SMatthias Ringwald                     return;
3223deb3ec6SMatthias Ringwald                 }
3233deb3ec6SMatthias Ringwald 
3243deb3ec6SMatthias Ringwald                 // check counter
325*18707219SMatthias Ringwald                 uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
326*18707219SMatthias Ringwald                 uint32_t counter_db     = le_device_db_remote_counter_get(att_server->ir_le_device_db_index);
3273deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet);
3283deb3ec6SMatthias Ringwald                 if (counter_packet < counter_db){
3293deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, db reports higher counter, abort");
330*18707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
3313deb3ec6SMatthias Ringwald                     return;
3323deb3ec6SMatthias Ringwald                 }
3333deb3ec6SMatthias Ringwald 
3343deb3ec6SMatthias Ringwald                 // signature is { sequence counter, secure hash }
3353deb3ec6SMatthias Ringwald                 sm_key_t csrk;
336*18707219SMatthias Ringwald                 le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk);
337*18707219SMatthias Ringwald                 att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION;
3383deb3ec6SMatthias Ringwald                 log_info("Orig Signature: ");
339*18707219SMatthias Ringwald                 log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8);
340*18707219SMatthias Ringwald                 uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1);
341*18707219SMatthias 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);
3423deb3ec6SMatthias Ringwald                 return;
3433deb3ec6SMatthias Ringwald             }
3443deb3ec6SMatthias Ringwald 
345b06f2579SMatthias Ringwald             // move on
346*18707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
347*18707219SMatthias Ringwald             att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
348b06f2579SMatthias Ringwald             break;
34988a48dd8SMatthias Ringwald 
3503deb3ec6SMatthias Ringwald         default:
3513deb3ec6SMatthias Ringwald             break;
3523deb3ec6SMatthias Ringwald     }
3533deb3ec6SMatthias Ringwald }
3543deb3ec6SMatthias Ringwald 
3557eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){
356b06f2579SMatthias Ringwald 
357b06f2579SMatthias Ringwald     // NOTE: we get l2cap fixed channel instead of con_handle
358b06f2579SMatthias Ringwald 
359*18707219SMatthias Ringwald     btstack_linked_list_iterator_t it;
360*18707219SMatthias Ringwald     hci_connections_get_iterator(&it);
361*18707219SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
362*18707219SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
363*18707219SMatthias Ringwald         att_server_t * att_server = &connection->att_server;
364*18707219SMatthias Ringwald         if (att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED){
365*18707219SMatthias Ringwald             int sent = att_server_process_validated_request(att_server);
366bb38f057SMatthias Ringwald             if (sent && (att_client_waiting_for_can_send || !btstack_linked_list_empty(&can_send_now_clients))){
367*18707219SMatthias Ringwald                 att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
3683deb3ec6SMatthias Ringwald                 return;
3693deb3ec6SMatthias Ringwald             }
3703deb3ec6SMatthias Ringwald         }
371*18707219SMatthias Ringwald     }
3723deb3ec6SMatthias Ringwald 
373bb38f057SMatthias Ringwald     while (!btstack_linked_list_empty(&can_send_now_clients)){
374bb38f057SMatthias Ringwald         // handle first client
375bb38f057SMatthias Ringwald         btstack_context_callback_registration_t * client = (btstack_context_callback_registration_t*) can_send_now_clients;
376*18707219SMatthias Ringwald         hci_con_handle_t con_handle = (uintptr_t) client->context;
377bb38f057SMatthias Ringwald         btstack_linked_list_remove(&can_send_now_clients, (btstack_linked_item_t *) client);
378bb38f057SMatthias Ringwald         client->callback(client->context);
379bb38f057SMatthias Ringwald 
380bb38f057SMatthias Ringwald         // request again if needed
381*18707219SMatthias Ringwald         if (!att_dispatch_server_can_send_now(con_handle)){
382bb38f057SMatthias Ringwald             if (!btstack_linked_list_empty(&can_send_now_clients) || att_client_waiting_for_can_send){
383*18707219SMatthias Ringwald                 att_dispatch_server_request_can_send_now_event(con_handle);
384bb38f057SMatthias Ringwald             }
385bb38f057SMatthias Ringwald             return;
386bb38f057SMatthias Ringwald         }
387bb38f057SMatthias Ringwald     }
388bb38f057SMatthias Ringwald 
389b06f2579SMatthias Ringwald     if (att_client_waiting_for_can_send){
390b06f2579SMatthias Ringwald         att_client_waiting_for_can_send = 0;
391b06f2579SMatthias Ringwald         att_emit_can_send_now_event();
3923deb3ec6SMatthias Ringwald     }
3933deb3ec6SMatthias Ringwald }
3943deb3ec6SMatthias Ringwald 
39588a48dd8SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
396*18707219SMatthias Ringwald 
397*18707219SMatthias Ringwald     att_server_t * att_server;
398*18707219SMatthias Ringwald 
39988a48dd8SMatthias Ringwald     switch (packet_type){
40088a48dd8SMatthias Ringwald 
40188a48dd8SMatthias Ringwald         case HCI_EVENT_PACKET:
40288a48dd8SMatthias Ringwald             if (packet[0] != L2CAP_EVENT_CAN_SEND_NOW) break;
4034d2be802SMatthias Ringwald             att_server_handle_can_send_now();
404372d62c4SMatthias Ringwald             break;
4053deb3ec6SMatthias Ringwald 
406372d62c4SMatthias Ringwald         case ATT_DATA_PACKET:
407*18707219SMatthias Ringwald             log_info("ATT Packet, handle 0x%04x", handle);
408*18707219SMatthias Ringwald             att_server = att_server_for_handle(handle);
409*18707219SMatthias Ringwald             if (!att_server) break;
410*18707219SMatthias Ringwald 
4113deb3ec6SMatthias Ringwald             // handle value indication confirms
412*18707219SMatthias Ringwald             if (packet[0] == ATT_HANDLE_VALUE_CONFIRMATION && att_server->value_indication_handle){
413*18707219SMatthias Ringwald                 btstack_run_loop_remove_timer(&att_server->value_indication_timer);
414*18707219SMatthias Ringwald                 uint16_t att_handle = att_server->value_indication_handle;
415*18707219SMatthias Ringwald                 att_server->value_indication_handle = 0;
416*18707219SMatthias Ringwald                 att_handle_value_indication_notify_client(0, att_server->connection.con_handle, att_handle);
4173deb3ec6SMatthias Ringwald                 return;
4183deb3ec6SMatthias Ringwald             }
4193deb3ec6SMatthias Ringwald 
4206428eb5aSMatthias Ringwald             // directly process command
4216428eb5aSMatthias Ringwald             // note: signed write cannot be handled directly as authentication needs to be verified
4226428eb5aSMatthias Ringwald             if (packet[0] == ATT_WRITE_COMMAND){
423*18707219SMatthias Ringwald                 att_handle_request(&att_server->connection, packet, size, 0);
4246428eb5aSMatthias Ringwald                 return;
4256428eb5aSMatthias Ringwald             }
4266428eb5aSMatthias Ringwald 
4273deb3ec6SMatthias Ringwald             // check size
428*18707219SMatthias Ringwald             if (size > sizeof(att_server->request_buffer)) {
429*18707219SMatthias 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));
4306428eb5aSMatthias Ringwald                 return;
4316428eb5aSMatthias Ringwald             }
4323deb3ec6SMatthias Ringwald 
4333deb3ec6SMatthias Ringwald             // last request still in processing?
434*18707219SMatthias Ringwald             if (att_server->state != ATT_SERVER_IDLE){
435*18707219SMatthias Ringwald                 log_info("att_packet_handler: skipping att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state);
4366428eb5aSMatthias Ringwald                 return;
4376428eb5aSMatthias Ringwald             }
4383deb3ec6SMatthias Ringwald 
4393deb3ec6SMatthias Ringwald             // store request
440*18707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED;
441*18707219SMatthias Ringwald             att_server->request_size = size;
442*18707219SMatthias Ringwald             memcpy(att_server->request_buffer, packet, size);
4433deb3ec6SMatthias Ringwald 
444*18707219SMatthias Ringwald             att_run_for_context(att_server);
445372d62c4SMatthias Ringwald             break;
446372d62c4SMatthias Ringwald     }
4473deb3ec6SMatthias Ringwald }
4483deb3ec6SMatthias Ringwald 
4493deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){
4503deb3ec6SMatthias Ringwald 
4511a389cdcSMatthias Ringwald     // register for HCI Events
452d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &att_event_packet_handler;
4531a389cdcSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
4541a389cdcSMatthias Ringwald 
455406722e4SMatthias Ringwald     // register for SM events
456d9a7306aSMatthias Ringwald     sm_event_callback_registration.callback = &att_event_packet_handler;
457406722e4SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
4583deb3ec6SMatthias Ringwald 
4591a389cdcSMatthias Ringwald     // and L2CAP ATT Server PDUs
4603deb3ec6SMatthias Ringwald     att_dispatch_register_server(att_packet_handler);
4613deb3ec6SMatthias Ringwald 
4623deb3ec6SMatthias Ringwald     att_set_db(db);
4633deb3ec6SMatthias Ringwald     att_set_read_callback(read_callback);
4643deb3ec6SMatthias Ringwald     att_set_write_callback(write_callback);
4653deb3ec6SMatthias Ringwald 
4663deb3ec6SMatthias Ringwald }
4673deb3ec6SMatthias Ringwald 
4683deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){
4693deb3ec6SMatthias Ringwald     att_client_packet_handler = handler;
4703deb3ec6SMatthias Ringwald }
4713deb3ec6SMatthias Ringwald 
472c141988cSMatthias Ringwald int  att_server_can_send_packet_now(hci_con_handle_t con_handle){
473*18707219SMatthias Ringwald 	return att_dispatch_server_can_send_now(con_handle);
4741b96b007SMatthias Ringwald }
4751b96b007SMatthias Ringwald 
476c141988cSMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){
477*18707219SMatthias Ringwald     log_info("att_server_request_can_send_now_event 0x%04x", con_handle);
4781b96b007SMatthias Ringwald     att_client_waiting_for_can_send = 1;
479*18707219SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
4803deb3ec6SMatthias Ringwald }
4813deb3ec6SMatthias Ringwald 
482bb38f057SMatthias Ringwald void att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
483bb38f057SMatthias Ringwald     // check if can send already
484bb38f057SMatthias Ringwald     if (att_dispatch_server_can_send_now(con_handle)){
485bb38f057SMatthias Ringwald         callback_registration->callback(callback_registration->context);
486bb38f057SMatthias Ringwald         return;
487bb38f057SMatthias Ringwald     }
488*18707219SMatthias Ringwald     callback_registration->context = (void*)(uintptr_t) con_handle;
489bb38f057SMatthias Ringwald     btstack_linked_list_add_tail(&can_send_now_clients, (btstack_linked_item_t*) callback_registration);
490bb38f057SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
491bb38f057SMatthias Ringwald }
492bb38f057SMatthias Ringwald 
493*18707219SMatthias Ringwald 
494c141988cSMatthias Ringwald int att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){
495*18707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
496*18707219SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
497*18707219SMatthias Ringwald 
498*18707219SMatthias Ringwald     if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
4993deb3ec6SMatthias Ringwald 
5003deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
5013deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
502*18707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_notification(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
503*18707219SMatthias Ringwald 	return l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
5043deb3ec6SMatthias Ringwald }
5053deb3ec6SMatthias Ringwald 
506c141988cSMatthias Ringwald int att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){
507*18707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
508*18707219SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
509*18707219SMatthias Ringwald 
510*18707219SMatthias Ringwald     if (att_server->value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PORGRESS;
511*18707219SMatthias Ringwald     if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
5123deb3ec6SMatthias Ringwald 
5133deb3ec6SMatthias Ringwald     // track indication
514*18707219SMatthias Ringwald     att_server->value_indication_handle = attribute_handle;
515*18707219SMatthias Ringwald     btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout);
516*18707219SMatthias Ringwald     btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS);
517*18707219SMatthias Ringwald     btstack_run_loop_add_timer(&att_server->value_indication_timer);
5183deb3ec6SMatthias Ringwald 
5193deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
5203deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
521*18707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_indication(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
522*18707219SMatthias Ringwald 	l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
5233deb3ec6SMatthias Ringwald     return 0;
5243deb3ec6SMatthias Ringwald }
525