xref: /btstack/src/ble/att_server.c (revision b2c1e52c9809022f049c3515e932dcafd46c6f33)
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 
38ab2c6ae4SMatthias Ringwald #define __BTSTACK_FILE__ "att_server.c"
39ab2c6ae4SMatthias Ringwald 
403deb3ec6SMatthias Ringwald 
413deb3ec6SMatthias Ringwald //
423deb3ec6SMatthias Ringwald // ATT Server Globals
433deb3ec6SMatthias Ringwald //
443deb3ec6SMatthias Ringwald 
453deb3ec6SMatthias Ringwald #include <stdint.h>
463deb3ec6SMatthias Ringwald #include <stdio.h>
473deb3ec6SMatthias Ringwald #include <stdlib.h>
483deb3ec6SMatthias Ringwald #include <string.h>
493deb3ec6SMatthias Ringwald #include <inttypes.h>
503deb3ec6SMatthias Ringwald 
517907f069SMatthias Ringwald #include "btstack_config.h"
523deb3ec6SMatthias Ringwald 
5359c6af15SMatthias Ringwald #include "att_dispatch.h"
5459c6af15SMatthias Ringwald #include "ble/att_db.h"
5559c6af15SMatthias Ringwald #include "ble/att_server.h"
5659c6af15SMatthias Ringwald #include "ble/core.h"
5759c6af15SMatthias Ringwald #include "ble/le_device_db.h"
5859c6af15SMatthias Ringwald #include "ble/sm.h"
5916ece135SMatthias Ringwald #include "btstack_debug.h"
600e2df43fSMatthias Ringwald #include "btstack_event.h"
613deb3ec6SMatthias Ringwald #include "btstack_memory.h"
620e2df43fSMatthias Ringwald #include "btstack_run_loop.h"
6359c6af15SMatthias Ringwald #include "gap.h"
643deb3ec6SMatthias Ringwald #include "hci.h"
653deb3ec6SMatthias Ringwald #include "hci_dump.h"
663deb3ec6SMatthias Ringwald #include "l2cap.h"
67bf78aac6SMatthias Ringwald #include "btstack_tlv.h"
68bf78aac6SMatthias Ringwald 
69bf78aac6SMatthias Ringwald #ifndef GATT_SERVER_NUM_PERSISTENT_CCC
70bf78aac6SMatthias Ringwald #define GATT_SERVER_NUM_PERSISTENT_CCC 20
71bf78aac6SMatthias Ringwald #endif
723deb3ec6SMatthias Ringwald 
7318707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server);
7401ac2008SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle);
7501ac2008SMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server);
76760ad805SMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server);
773deb3ec6SMatthias Ringwald 
7818707219SMatthias Ringwald // global
791a389cdcSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
80406722e4SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration;
813deb3ec6SMatthias Ringwald static btstack_packet_handler_t               att_client_packet_handler = NULL;
82bb38f057SMatthias Ringwald static btstack_linked_list_t                  can_send_now_clients;
833d71c7a4SMatthias Ringwald static btstack_linked_list_t                  service_handlers;
8418707219SMatthias Ringwald static uint8_t                                att_client_waiting_for_can_send;
8518707219SMatthias Ringwald 
863d71c7a4SMatthias Ringwald static att_read_callback_t                    att_server_client_read_callback;
873d71c7a4SMatthias Ringwald static att_write_callback_t                   att_server_client_write_callback;
883d71c7a4SMatthias Ringwald 
89bf78aac6SMatthias Ringwald // track CCC 1-entry cache
90bf78aac6SMatthias Ringwald // static att_server_t *    att_persistent_ccc_server;
91bf78aac6SMatthias Ringwald // static hci_con_handle_t  att_persistent_ccc_con_handle;
92bf78aac6SMatthias Ringwald 
9318707219SMatthias Ringwald static att_server_t * att_server_for_handle(hci_con_handle_t con_handle){
9418707219SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
9518707219SMatthias Ringwald     if (!hci_connection) return NULL;
9618707219SMatthias Ringwald     return &hci_connection->att_server;
9718707219SMatthias Ringwald }
9818707219SMatthias Ringwald 
9902b78fc8SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
10018707219SMatthias Ringwald static att_server_t * att_server_for_state(att_server_state_t state){
10118707219SMatthias Ringwald     btstack_linked_list_iterator_t it;
10218707219SMatthias Ringwald     hci_connections_get_iterator(&it);
10318707219SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
10418707219SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
10518707219SMatthias Ringwald         att_server_t * att_server = &connection->att_server;
10618707219SMatthias Ringwald         if (att_server->state == state) return att_server;
10718707219SMatthias Ringwald     }
10818707219SMatthias Ringwald     return NULL;
10918707219SMatthias Ringwald }
11002b78fc8SMatthias Ringwald #endif
111bb38f057SMatthias Ringwald 
1123deb3ec6SMatthias Ringwald static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){
1133deb3ec6SMatthias Ringwald     if (!att_client_packet_handler) return;
1143deb3ec6SMatthias Ringwald 
1153deb3ec6SMatthias Ringwald     uint8_t event[7];
1163deb3ec6SMatthias Ringwald     int pos = 0;
1175611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE;
1183deb3ec6SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
1193deb3ec6SMatthias Ringwald     event[pos++] = status;
120f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, client_handle);
1213deb3ec6SMatthias Ringwald     pos += 2;
122f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, attribute_handle);
1233deb3ec6SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1243deb3ec6SMatthias Ringwald }
1253deb3ec6SMatthias Ringwald 
126fc64f94aSMatthias Ringwald static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){
1273deb3ec6SMatthias Ringwald     if (!att_client_packet_handler) return;
1283deb3ec6SMatthias Ringwald 
1293deb3ec6SMatthias Ringwald     uint8_t event[6];
1303deb3ec6SMatthias Ringwald     int pos = 0;
1315611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE;
1323deb3ec6SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
133fc64f94aSMatthias Ringwald     little_endian_store_16(event, pos, con_handle);
1343deb3ec6SMatthias Ringwald     pos += 2;
135f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, mtu);
1363deb3ec6SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1373deb3ec6SMatthias Ringwald }
1383deb3ec6SMatthias Ringwald 
1391b96b007SMatthias Ringwald static void att_emit_can_send_now_event(void){
1403c97b242SMatthias Ringwald     if (!att_client_packet_handler) return;
1413c97b242SMatthias Ringwald 
1421b96b007SMatthias Ringwald     uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0};
1431b96b007SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1441b96b007SMatthias Ringwald }
1451b96b007SMatthias Ringwald 
146ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){
14754178543SMatthias Ringwald     void * context = btstack_run_loop_get_timer_context(ts);
14854178543SMatthias Ringwald     hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
14918707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
15018707219SMatthias Ringwald     if (!att_server) return;
15118707219SMatthias Ringwald     uint16_t att_handle = att_server->value_indication_handle;
15218707219SMatthias Ringwald     att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_server->connection.con_handle, att_handle);
1533deb3ec6SMatthias Ringwald }
1543deb3ec6SMatthias Ringwald 
155760ad805SMatthias Ringwald static void att_device_identified(att_server_t * att_server, uint8_t index){
156e62c502cSMatthias Ringwald     att_server->ir_lookup_active = 0;
157e62c502cSMatthias Ringwald     att_server->ir_le_device_db_index = index;
158760ad805SMatthias Ringwald     log_info("Remote device with conn 0%04x registered with index id %u", (int) att_server->connection.con_handle, att_server->ir_le_device_db_index);
159e62c502cSMatthias Ringwald     att_run_for_context(att_server);
160e62c502cSMatthias Ringwald }
161e62c502cSMatthias Ringwald 
1623deb3ec6SMatthias Ringwald static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1639ec2630cSMatthias Ringwald 
1647dad9ff8SMatthias Ringwald     UNUSED(channel); // ok: there is no channel
1657dad9ff8SMatthias Ringwald     UNUSED(size);    // ok: handling own l2cap events
1663deb3ec6SMatthias Ringwald 
16718707219SMatthias Ringwald     att_server_t * att_server;
16818707219SMatthias Ringwald     hci_con_handle_t con_handle;
16918707219SMatthias Ringwald 
1703deb3ec6SMatthias Ringwald     switch (packet_type) {
1713deb3ec6SMatthias Ringwald 
1723deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1730e2df43fSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
1743deb3ec6SMatthias Ringwald 
1753deb3ec6SMatthias Ringwald                 case HCI_EVENT_LE_META:
1763deb3ec6SMatthias Ringwald                     switch (packet[2]) {
1773deb3ec6SMatthias Ringwald                         case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
17818707219SMatthias Ringwald                             con_handle = little_endian_read_16(packet, 4);
17918707219SMatthias Ringwald                             att_server = att_server_for_handle(con_handle);
18018707219SMatthias Ringwald                             if (!att_server) break;
1813deb3ec6SMatthias Ringwald                         	// store connection info
18218707219SMatthias Ringwald                         	att_server->peer_addr_type = packet[7];
18318707219SMatthias Ringwald                             reverse_bd_addr(&packet[8], att_server->peer_address);
18418707219SMatthias Ringwald                             att_server->connection.con_handle = con_handle;
1853deb3ec6SMatthias Ringwald                             // reset connection properties
18618707219SMatthias Ringwald                             att_server->state = ATT_SERVER_IDLE;
18718707219SMatthias Ringwald                             att_server->connection.mtu = ATT_DEFAULT_MTU;
18818707219SMatthias Ringwald                             att_server->connection.max_mtu = l2cap_max_le_mtu();
18918707219SMatthias Ringwald                             if (att_server->connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){
19018707219SMatthias Ringwald                                 att_server->connection.max_mtu = ATT_REQUEST_BUFFER_SIZE;
19199c58ad0SMatthias Ringwald                             }
19218707219SMatthias Ringwald                             att_server->connection.encryption_key_size = 0;
19318707219SMatthias Ringwald                             att_server->connection.authenticated = 0;
19418707219SMatthias Ringwald 		                	att_server->connection.authorized = 0;
195*b2c1e52cSMatthias Ringwald                             // workaround: identity resolving can already be complete, at least store result
196*b2c1e52cSMatthias Ringwald                             att_server->ir_le_device_db_index = sm_le_device_index(con_handle);
197760ad805SMatthias Ringwald                             att_server->pairing_active = 0;
1983deb3ec6SMatthias Ringwald                             break;
1993deb3ec6SMatthias Ringwald 
2003deb3ec6SMatthias Ringwald                         default:
2013deb3ec6SMatthias Ringwald                             break;
2023deb3ec6SMatthias Ringwald                     }
2033deb3ec6SMatthias Ringwald                     break;
2043deb3ec6SMatthias Ringwald 
2053deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_CHANGE:
2063deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE:
2073deb3ec6SMatthias Ringwald                 	// check handle
20818707219SMatthias Ringwald                     con_handle = little_endian_read_16(packet, 3);
20918707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
21018707219SMatthias Ringwald                     if (!att_server) break;
21118707219SMatthias Ringwald                 	att_server->connection.encryption_key_size = sm_encryption_key_size(con_handle);
21218707219SMatthias Ringwald                 	att_server->connection.authenticated = sm_authenticated(con_handle);
21301ac2008SMatthias Ringwald                     if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){
21401ac2008SMatthias Ringwald                         // restore CCC values when encrypted
21501ac2008SMatthias Ringwald                         if (hci_event_encryption_change_get_encryption_enabled(packet)){
21601ac2008SMatthias Ringwald                             att_server_persistent_ccc_restore(att_server);
21701ac2008SMatthias Ringwald                         }
21801ac2008SMatthias Ringwald                     }
2193deb3ec6SMatthias Ringwald                 	break;
2203deb3ec6SMatthias Ringwald 
2213deb3ec6SMatthias Ringwald                 case HCI_EVENT_DISCONNECTION_COMPLETE:
22270a390c7SMatthias Ringwald                     // check handle
22318707219SMatthias Ringwald                     con_handle = hci_event_disconnection_complete_get_connection_handle(packet);
22418707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
22518707219SMatthias Ringwald                     if (!att_server) break;
22618707219SMatthias Ringwald                     att_clear_transaction_queue(&att_server->connection);
22718707219SMatthias Ringwald                     att_server->connection.con_handle = 0;
22818707219SMatthias Ringwald                     att_server->value_indication_handle = 0; // reset error state
229760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
23018707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
2313deb3ec6SMatthias Ringwald                     break;
2323deb3ec6SMatthias Ringwald 
233760ad805SMatthias Ringwald                 // Identity Resolving
2345611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_STARTED:
23518707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_started_get_handle(packet);
23618707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
23718707219SMatthias Ringwald                     if (!att_server) break;
2385611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED");
23918707219SMatthias Ringwald                     att_server->ir_lookup_active = 1;
2403deb3ec6SMatthias Ringwald                     break;
2415611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
242760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
243760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
244760ad805SMatthias Ringwald                     if (!att_server) return;
245760ad805SMatthias Ringwald                     att_device_identified(att_server, sm_event_identity_resolving_succeeded_get_index(packet));
2463deb3ec6SMatthias Ringwald                     break;
2475611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_FAILED:
24818707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_failed_get_handle(packet);
24918707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
25018707219SMatthias Ringwald                     if (!att_server) break;
2515611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED");
25218707219SMatthias Ringwald                     att_server->ir_lookup_active = 0;
25318707219SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
25418707219SMatthias Ringwald                     att_run_for_context(att_server);
2553deb3ec6SMatthias Ringwald                     break;
256760ad805SMatthias Ringwald 
257760ad805SMatthias Ringwald                 // Pairing started - delete stored CCC values
258760ad805SMatthias Ringwald                 // - assumes pairing indicates either new device or re-pairing, in both cases there should be no stored CCC values
259760ad805SMatthias Ringwald                 // - assumes that all events have the con handle as the first field
260760ad805SMatthias Ringwald                 case SM_EVENT_JUST_WORKS_REQUEST:
261760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
262760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_INPUT_NUMBER:
263760ad805SMatthias Ringwald                 case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
264760ad805SMatthias Ringwald                     con_handle = sm_event_just_works_request_get_handle(packet);
265760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
266760ad805SMatthias Ringwald                     if (!att_server) break;
267760ad805SMatthias Ringwald                     att_server->pairing_active = 1;
268760ad805SMatthias Ringwald                     log_info("SM Pairing started");
269760ad805SMatthias Ringwald                     if (att_server->ir_le_device_db_index < 0) break;
270760ad805SMatthias Ringwald                     att_server_persistent_ccc_clear(att_server);
271760ad805SMatthias Ringwald                     // index not valid anymore
272760ad805SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
273760ad805SMatthias Ringwald                     break;
274760ad805SMatthias Ringwald 
275760ad805SMatthias Ringwald                 // Pairing completed
276760ad805SMatthias Ringwald                 case SM_EVENT_IDENTITY_CREATED:
277760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
278760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
279760ad805SMatthias Ringwald                     if (!att_server) return;
280760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
281760ad805SMatthias Ringwald                     att_device_identified(att_server, sm_event_identity_created_get_index(packet));
282760ad805SMatthias Ringwald                     break;
283760ad805SMatthias Ringwald 
284760ad805SMatthias Ringwald                 // Authorization
2855611a760SMatthias Ringwald                 case SM_EVENT_AUTHORIZATION_RESULT: {
28618707219SMatthias Ringwald                     con_handle = sm_event_authorization_result_get_handle(packet);
28718707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
28818707219SMatthias Ringwald                     if (!att_server) break;
28918707219SMatthias Ringwald                     att_server->connection.authorized = sm_event_authorization_result_get_authorization_result(packet);
29018707219SMatthias Ringwald                     att_dispatch_server_request_can_send_now_event(con_handle);
2913deb3ec6SMatthias Ringwald                 	break;
2923deb3ec6SMatthias Ringwald                 }
2933deb3ec6SMatthias Ringwald                 default:
2943deb3ec6SMatthias Ringwald                     break;
2953deb3ec6SMatthias Ringwald             }
29665b44ffdSMatthias Ringwald             break;
29765b44ffdSMatthias Ringwald         default:
29865b44ffdSMatthias Ringwald             break;
2993deb3ec6SMatthias Ringwald     }
3003deb3ec6SMatthias Ringwald }
3013deb3ec6SMatthias Ringwald 
3027a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
3033deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
3043deb3ec6SMatthias Ringwald 
30518707219SMatthias Ringwald     att_server_t * att_server = att_server_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION);
30618707219SMatthias Ringwald     if (!att_server) return;
3073deb3ec6SMatthias Ringwald 
3083deb3ec6SMatthias Ringwald     uint8_t hash_flipped[8];
3099c80e4ccSMatthias Ringwald     reverse_64(hash, hash_flipped);
31018707219SMatthias Ringwald     if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8)){
3113deb3ec6SMatthias Ringwald         log_info("ATT Signed Write, invalid signature");
31218707219SMatthias Ringwald         att_server->state = ATT_SERVER_IDLE;
3133deb3ec6SMatthias Ringwald         return;
3143deb3ec6SMatthias Ringwald     }
3153deb3ec6SMatthias Ringwald     log_info("ATT Signed Write, valid signature");
3163deb3ec6SMatthias Ringwald 
3173deb3ec6SMatthias Ringwald     // update sequence number
31818707219SMatthias Ringwald     uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
31918707219SMatthias Ringwald     le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1);
32018707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
32118707219SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
3223deb3ec6SMatthias Ringwald }
3237a766ebfSMatthias Ringwald #endif
32488a48dd8SMatthias Ringwald 
32518707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED
326b06f2579SMatthias Ringwald // pre: can send now
327b06f2579SMatthias Ringwald // returns: 1 if packet was sent
32818707219SMatthias Ringwald static int att_server_process_validated_request(att_server_t * att_server){
329b06f2579SMatthias Ringwald 
330b06f2579SMatthias Ringwald     l2cap_reserve_packet_buffer();
331b06f2579SMatthias Ringwald     uint8_t * att_response_buffer = l2cap_get_outgoing_buffer();
33218707219SMatthias Ringwald     uint16_t  att_response_size   = att_handle_request(&att_server->connection, att_server->request_buffer, att_server->request_size, att_response_buffer);
333b06f2579SMatthias Ringwald 
334b06f2579SMatthias Ringwald     // intercept "insufficient authorization" for authenticated connections to allow for user authorization
335b06f2579SMatthias Ringwald     if ((att_response_size     >= 4)
336b06f2579SMatthias Ringwald     && (att_response_buffer[0] == ATT_ERROR_RESPONSE)
337b06f2579SMatthias Ringwald     && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION)
33818707219SMatthias Ringwald     && (att_server->connection.authenticated)){
339b06f2579SMatthias Ringwald 
34018707219SMatthias Ringwald         switch (sm_authorization_state(att_server->connection.con_handle)){
341b06f2579SMatthias Ringwald             case AUTHORIZATION_UNKNOWN:
342b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
34318707219SMatthias Ringwald                 sm_request_pairing(att_server->connection.con_handle);
344b06f2579SMatthias Ringwald                 return 0;
345b06f2579SMatthias Ringwald             case AUTHORIZATION_PENDING:
346b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
347b06f2579SMatthias Ringwald                 return 0;
348b06f2579SMatthias Ringwald             default:
349b06f2579SMatthias Ringwald                 break;
350b06f2579SMatthias Ringwald         }
351b06f2579SMatthias Ringwald     }
352b06f2579SMatthias Ringwald 
35318707219SMatthias Ringwald     att_server->state = ATT_SERVER_IDLE;
354b06f2579SMatthias Ringwald     if (att_response_size == 0) {
355b06f2579SMatthias Ringwald         l2cap_release_packet_buffer();
356b06f2579SMatthias Ringwald         return 0;
357b06f2579SMatthias Ringwald     }
358b06f2579SMatthias Ringwald 
35918707219SMatthias Ringwald     l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size);
360b06f2579SMatthias Ringwald 
361b06f2579SMatthias Ringwald     // notify client about MTU exchange result
362b06f2579SMatthias Ringwald     if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){
36318707219SMatthias Ringwald         att_emit_mtu_event(att_server->connection.con_handle, att_server->connection.mtu);
364b06f2579SMatthias Ringwald     }
365b06f2579SMatthias Ringwald     return 1;
366b06f2579SMatthias Ringwald }
367b06f2579SMatthias Ringwald 
36818707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server){
36918707219SMatthias Ringwald     switch (att_server->state){
3703deb3ec6SMatthias Ringwald         case ATT_SERVER_REQUEST_RECEIVED:
371760ad805SMatthias Ringwald 
372760ad805SMatthias Ringwald             // wait until pairing is complete
373760ad805SMatthias Ringwald             if (att_server->pairing_active) break;
374760ad805SMatthias Ringwald 
3757a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
37618707219SMatthias Ringwald             if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){
3773deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write!");
3783deb3ec6SMatthias Ringwald                 if (!sm_cmac_ready()) {
3793deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, sm_cmac engine not ready. Abort");
38018707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
3813deb3ec6SMatthias Ringwald                     return;
3823deb3ec6SMatthias Ringwald                 }
38318707219SMatthias Ringwald                 if (att_server->request_size < (3 + 12)) {
3843deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, request to short. Abort.");
38518707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
3863deb3ec6SMatthias Ringwald                     return;
3873deb3ec6SMatthias Ringwald                 }
38818707219SMatthias Ringwald                 if (att_server->ir_lookup_active){
3893deb3ec6SMatthias Ringwald                     return;
3903deb3ec6SMatthias Ringwald                 }
39118707219SMatthias Ringwald                 if (att_server->ir_le_device_db_index < 0){
3923deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, CSRK not available");
39318707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
3943deb3ec6SMatthias Ringwald                     return;
3953deb3ec6SMatthias Ringwald                 }
3963deb3ec6SMatthias Ringwald 
3973deb3ec6SMatthias Ringwald                 // check counter
39818707219SMatthias Ringwald                 uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
39918707219SMatthias Ringwald                 uint32_t counter_db     = le_device_db_remote_counter_get(att_server->ir_le_device_db_index);
4003deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet);
4013deb3ec6SMatthias Ringwald                 if (counter_packet < counter_db){
4023deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, db reports higher counter, abort");
40318707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
4043deb3ec6SMatthias Ringwald                     return;
4053deb3ec6SMatthias Ringwald                 }
4063deb3ec6SMatthias Ringwald 
4073deb3ec6SMatthias Ringwald                 // signature is { sequence counter, secure hash }
4083deb3ec6SMatthias Ringwald                 sm_key_t csrk;
40918707219SMatthias Ringwald                 le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk);
41018707219SMatthias Ringwald                 att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION;
4113deb3ec6SMatthias Ringwald                 log_info("Orig Signature: ");
41218707219SMatthias Ringwald                 log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8);
41318707219SMatthias Ringwald                 uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1);
41418707219SMatthias 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);
4153deb3ec6SMatthias Ringwald                 return;
4163deb3ec6SMatthias Ringwald             }
4177a766ebfSMatthias Ringwald #endif
418b06f2579SMatthias Ringwald             // move on
41918707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
42018707219SMatthias Ringwald             att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
421b06f2579SMatthias Ringwald             break;
42288a48dd8SMatthias Ringwald 
4233deb3ec6SMatthias Ringwald         default:
4243deb3ec6SMatthias Ringwald             break;
4253deb3ec6SMatthias Ringwald     }
4263deb3ec6SMatthias Ringwald }
4273deb3ec6SMatthias Ringwald 
4287eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){
429b06f2579SMatthias Ringwald 
430b06f2579SMatthias Ringwald     // NOTE: we get l2cap fixed channel instead of con_handle
431b06f2579SMatthias Ringwald 
43218707219SMatthias Ringwald     btstack_linked_list_iterator_t it;
43318707219SMatthias Ringwald     hci_connections_get_iterator(&it);
43418707219SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
43518707219SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
43618707219SMatthias Ringwald         att_server_t * att_server = &connection->att_server;
43718707219SMatthias Ringwald         if (att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED){
43818707219SMatthias Ringwald             int sent = att_server_process_validated_request(att_server);
439bb38f057SMatthias Ringwald             if (sent && (att_client_waiting_for_can_send || !btstack_linked_list_empty(&can_send_now_clients))){
44018707219SMatthias Ringwald                 att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
4413deb3ec6SMatthias Ringwald                 return;
4423deb3ec6SMatthias Ringwald             }
4433deb3ec6SMatthias Ringwald         }
44418707219SMatthias Ringwald     }
4453deb3ec6SMatthias Ringwald 
446bb38f057SMatthias Ringwald     while (!btstack_linked_list_empty(&can_send_now_clients)){
447bb38f057SMatthias Ringwald         // handle first client
448bb38f057SMatthias Ringwald         btstack_context_callback_registration_t * client = (btstack_context_callback_registration_t*) can_send_now_clients;
44918707219SMatthias Ringwald         hci_con_handle_t con_handle = (uintptr_t) client->context;
450bb38f057SMatthias Ringwald         btstack_linked_list_remove(&can_send_now_clients, (btstack_linked_item_t *) client);
451bb38f057SMatthias Ringwald         client->callback(client->context);
452bb38f057SMatthias Ringwald 
453bb38f057SMatthias Ringwald         // request again if needed
45418707219SMatthias Ringwald         if (!att_dispatch_server_can_send_now(con_handle)){
455bb38f057SMatthias Ringwald             if (!btstack_linked_list_empty(&can_send_now_clients) || att_client_waiting_for_can_send){
45618707219SMatthias Ringwald                 att_dispatch_server_request_can_send_now_event(con_handle);
457bb38f057SMatthias Ringwald             }
458bb38f057SMatthias Ringwald             return;
459bb38f057SMatthias Ringwald         }
460bb38f057SMatthias Ringwald     }
461bb38f057SMatthias Ringwald 
462b06f2579SMatthias Ringwald     if (att_client_waiting_for_can_send){
463b06f2579SMatthias Ringwald         att_client_waiting_for_can_send = 0;
464b06f2579SMatthias Ringwald         att_emit_can_send_now_event();
4653deb3ec6SMatthias Ringwald     }
4663deb3ec6SMatthias Ringwald }
4673deb3ec6SMatthias Ringwald 
46888a48dd8SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
46918707219SMatthias Ringwald 
47018707219SMatthias Ringwald     att_server_t * att_server;
47118707219SMatthias Ringwald 
47288a48dd8SMatthias Ringwald     switch (packet_type){
47388a48dd8SMatthias Ringwald 
47488a48dd8SMatthias Ringwald         case HCI_EVENT_PACKET:
47588a48dd8SMatthias Ringwald             if (packet[0] != L2CAP_EVENT_CAN_SEND_NOW) break;
4764d2be802SMatthias Ringwald             att_server_handle_can_send_now();
477372d62c4SMatthias Ringwald             break;
4783deb3ec6SMatthias Ringwald 
479372d62c4SMatthias Ringwald         case ATT_DATA_PACKET:
48018707219SMatthias Ringwald             log_info("ATT Packet, handle 0x%04x", handle);
48118707219SMatthias Ringwald             att_server = att_server_for_handle(handle);
48218707219SMatthias Ringwald             if (!att_server) break;
48318707219SMatthias Ringwald 
4843deb3ec6SMatthias Ringwald             // handle value indication confirms
48518707219SMatthias Ringwald             if (packet[0] == ATT_HANDLE_VALUE_CONFIRMATION && att_server->value_indication_handle){
48618707219SMatthias Ringwald                 btstack_run_loop_remove_timer(&att_server->value_indication_timer);
48718707219SMatthias Ringwald                 uint16_t att_handle = att_server->value_indication_handle;
48818707219SMatthias Ringwald                 att_server->value_indication_handle = 0;
48918707219SMatthias Ringwald                 att_handle_value_indication_notify_client(0, att_server->connection.con_handle, att_handle);
4903deb3ec6SMatthias Ringwald                 return;
4913deb3ec6SMatthias Ringwald             }
4923deb3ec6SMatthias Ringwald 
4936428eb5aSMatthias Ringwald             // directly process command
4946428eb5aSMatthias Ringwald             // note: signed write cannot be handled directly as authentication needs to be verified
4956428eb5aSMatthias Ringwald             if (packet[0] == ATT_WRITE_COMMAND){
49618707219SMatthias Ringwald                 att_handle_request(&att_server->connection, packet, size, 0);
4976428eb5aSMatthias Ringwald                 return;
4986428eb5aSMatthias Ringwald             }
4996428eb5aSMatthias Ringwald 
5003deb3ec6SMatthias Ringwald             // check size
50118707219SMatthias Ringwald             if (size > sizeof(att_server->request_buffer)) {
50218707219SMatthias 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));
5036428eb5aSMatthias Ringwald                 return;
5046428eb5aSMatthias Ringwald             }
5053deb3ec6SMatthias Ringwald 
5063deb3ec6SMatthias Ringwald             // last request still in processing?
50718707219SMatthias Ringwald             if (att_server->state != ATT_SERVER_IDLE){
50818707219SMatthias Ringwald                 log_info("att_packet_handler: skipping att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state);
5096428eb5aSMatthias Ringwald                 return;
5106428eb5aSMatthias Ringwald             }
5113deb3ec6SMatthias Ringwald 
5123deb3ec6SMatthias Ringwald             // store request
51318707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED;
51418707219SMatthias Ringwald             att_server->request_size = size;
51518707219SMatthias Ringwald             memcpy(att_server->request_buffer, packet, size);
5163deb3ec6SMatthias Ringwald 
51718707219SMatthias Ringwald             att_run_for_context(att_server);
518372d62c4SMatthias Ringwald             break;
519372d62c4SMatthias Ringwald     }
5203deb3ec6SMatthias Ringwald }
5213deb3ec6SMatthias Ringwald 
522bf78aac6SMatthias Ringwald // ---------------------
523bf78aac6SMatthias Ringwald // persistent CCC writes
524bf78aac6SMatthias Ringwald // TLV value format: le_device_index(8), attribute_handle(16), value (8)
525bf78aac6SMatthias Ringwald static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){
526bf78aac6SMatthias Ringwald     return 'B' << 24 | 'T' << 16 | 'C' << 8 | index;
527bf78aac6SMatthias Ringwald }
528bf78aac6SMatthias Ringwald 
529bf78aac6SMatthias Ringwald static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){
530bf78aac6SMatthias Ringwald     // lookup att_server instance
531bf78aac6SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
532bf78aac6SMatthias Ringwald     if (!att_server) return;
533bf78aac6SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
534bf78aac6SMatthias Ringwald     log_info("Store CCC value 0x%04x for handle 0x%04x of remote %s, le device id %d", value, att_handle, bd_addr_to_str(att_server->peer_address), le_device_index);
535bf78aac6SMatthias Ringwald     // check if bonded
536bf78aac6SMatthias Ringwald     if (le_device_index < 0) return;
537bf78aac6SMatthias Ringwald     // get btstack_tlv
538bf78aac6SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
539bf78aac6SMatthias Ringwald     void * tlv_context;
540bf78aac6SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
541bf78aac6SMatthias Ringwald     if (!tlv_impl) return;
542bf78aac6SMatthias Ringwald     // update ccc tag
543bf78aac6SMatthias Ringwald     int index;
544bf78aac6SMatthias Ringwald     uint8_t buffer[4];
545bf78aac6SMatthias Ringwald     int free_index = -1;
546bf78aac6SMatthias Ringwald     for (index=0;index<GATT_SERVER_NUM_PERSISTENT_CCC;index++){
547bf78aac6SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
548bf78aac6SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, buffer, sizeof(buffer));
549bf78aac6SMatthias Ringwald         if (len == 0){
550bf78aac6SMatthias Ringwald             free_index = index;
551bf78aac6SMatthias Ringwald             continue;
552bf78aac6SMatthias Ringwald         }
553bf78aac6SMatthias Ringwald         if (len != sizeof(buffer)) continue;
554bf78aac6SMatthias Ringwald         if (buffer[0] != le_device_index) continue;
555bf78aac6SMatthias Ringwald         if (little_endian_read_16(buffer, 1) != att_handle) continue;
55601ac2008SMatthias Ringwald         if (value){
557bf78aac6SMatthias Ringwald             // update
558760ad805SMatthias Ringwald             if (buffer[3] == value) {
559760ad805SMatthias Ringwald                 log_info("CCC Index %u: Up-to-date", index);
560760ad805SMatthias Ringwald                 return;
561760ad805SMatthias Ringwald             }
562bf78aac6SMatthias Ringwald             buffer[3] = value;
563760ad805SMatthias Ringwald             log_info("CCC Index %u: Store", index);
564bf78aac6SMatthias Ringwald             tlv_impl->store_tag(tlv_context, tag, buffer, sizeof(buffer));
56501ac2008SMatthias Ringwald         } else {
56601ac2008SMatthias Ringwald             // delete
567760ad805SMatthias Ringwald             log_info("CCC Index %u: Delete", index);
56801ac2008SMatthias Ringwald             tlv_impl->delete_tag(tlv_context, tag);
56901ac2008SMatthias Ringwald         }
570bf78aac6SMatthias Ringwald         return;
571bf78aac6SMatthias Ringwald     }
572bf78aac6SMatthias Ringwald     if (free_index < 0) return;
573bf78aac6SMatthias Ringwald     // store ccc tag
574bf78aac6SMatthias Ringwald     buffer[0] = le_device_index;
575bf78aac6SMatthias Ringwald     little_endian_store_16(buffer, 1, att_handle);
576bf78aac6SMatthias Ringwald     buffer[3] = value;
577bf78aac6SMatthias Ringwald     uint32_t tag = att_server_persistent_ccc_tag_for_index(free_index);
578bf78aac6SMatthias Ringwald     tlv_impl->store_tag(tlv_context, tag, buffer, sizeof(buffer));
579bf78aac6SMatthias Ringwald }
580bf78aac6SMatthias Ringwald 
581760ad805SMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server){
582760ad805SMatthias Ringwald     if (!att_server) return;
583760ad805SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
584760ad805SMatthias Ringwald     log_info("Clear CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
585760ad805SMatthias Ringwald     // check if bonded
586760ad805SMatthias Ringwald     if (le_device_index < 0) return;
587760ad805SMatthias Ringwald     // get btstack_tlv
588760ad805SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
589760ad805SMatthias Ringwald     void * tlv_context;
590760ad805SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
591760ad805SMatthias Ringwald     if (!tlv_impl) return;
592760ad805SMatthias Ringwald     // get all ccc tag
593760ad805SMatthias Ringwald     int index;
594760ad805SMatthias Ringwald     uint8_t buffer[4];
595760ad805SMatthias Ringwald     for (index=0;index<GATT_SERVER_NUM_PERSISTENT_CCC;index++){
596760ad805SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
597760ad805SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, buffer, sizeof(buffer));
598760ad805SMatthias Ringwald         if (len != sizeof(buffer)) continue;
599760ad805SMatthias Ringwald         if (buffer[0] != le_device_index) continue;
600760ad805SMatthias Ringwald         // delete entry
601760ad805SMatthias Ringwald         log_info("CCC Index %u: Delete", index);
602760ad805SMatthias Ringwald         tlv_impl->delete_tag(tlv_context, tag);
603760ad805SMatthias Ringwald     }
604760ad805SMatthias Ringwald }
605760ad805SMatthias Ringwald 
60601ac2008SMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server){
60701ac2008SMatthias Ringwald     if (!att_server) return;
60801ac2008SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
60901ac2008SMatthias Ringwald     log_info("Restore CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
61001ac2008SMatthias Ringwald     // get btstack_tlv
61101ac2008SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
61201ac2008SMatthias Ringwald     void * tlv_context;
61301ac2008SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
61401ac2008SMatthias Ringwald     if (!tlv_impl) return;
61501ac2008SMatthias Ringwald     // get all ccc tag
61601ac2008SMatthias Ringwald     int index;
61701ac2008SMatthias Ringwald     uint8_t buffer[4];
61801ac2008SMatthias Ringwald     for (index=0;index<GATT_SERVER_NUM_PERSISTENT_CCC;index++){
61901ac2008SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
62001ac2008SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, buffer, sizeof(buffer));
62101ac2008SMatthias Ringwald         if (len != sizeof(buffer)) continue;
62201ac2008SMatthias Ringwald         if (buffer[0] != le_device_index) continue;
62301ac2008SMatthias Ringwald         // simulate write callback
62401ac2008SMatthias Ringwald         uint16_t attribute_handle = little_endian_read_16(buffer, 1);
62501ac2008SMatthias Ringwald         uint8_t  value[2];
62601ac2008SMatthias Ringwald         little_endian_store_16(value, 0, buffer[3]);
62701ac2008SMatthias Ringwald         att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
62801ac2008SMatthias Ringwald         if (!callback) continue;
629760ad805SMatthias Ringwald         log_info("CCC Index %u: Set Attribute handle 0x%04x to value 0x%04x", index, attribute_handle, buffer[3]);
63001ac2008SMatthias Ringwald         (*callback)(att_server->connection.con_handle, attribute_handle, ATT_TRANSACTION_MODE_NONE, 0, value, sizeof(value));
63101ac2008SMatthias Ringwald     }
63201ac2008SMatthias Ringwald }
63301ac2008SMatthias Ringwald 
634bf78aac6SMatthias Ringwald // persistent CCC writes
635bf78aac6SMatthias Ringwald // ---------------------
6363d71c7a4SMatthias Ringwald 
6373d71c7a4SMatthias Ringwald // gatt service management
6383d71c7a4SMatthias Ringwald static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){
6393d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
6403d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
6413d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
6423d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
6433d71c7a4SMatthias Ringwald         if (handler->start_handle > handle) continue;
6443d71c7a4SMatthias Ringwald         if (handler->end_handle   < handle) continue;
6453d71c7a4SMatthias Ringwald         return handler;
6463d71c7a4SMatthias Ringwald     }
6473d71c7a4SMatthias Ringwald     return NULL;
6483d71c7a4SMatthias Ringwald }
6493d71c7a4SMatthias Ringwald static att_read_callback_t att_server_read_callback_for_handle(uint16_t handle){
6503d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
6513d71c7a4SMatthias Ringwald     if (handler) return handler->read_callback;
6523d71c7a4SMatthias Ringwald     return att_server_client_read_callback;
6533d71c7a4SMatthias Ringwald }
6543d71c7a4SMatthias Ringwald 
6553d71c7a4SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){
6563d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
6573d71c7a4SMatthias Ringwald     if (handler) return handler->write_callback;
6583d71c7a4SMatthias Ringwald     return att_server_client_write_callback;
6593d71c7a4SMatthias Ringwald }
6603d71c7a4SMatthias Ringwald 
6613d71c7a4SMatthias Ringwald static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){
6623d71c7a4SMatthias Ringwald     // notify all callbacks
6633d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
6643d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
6653d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
6663d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
6673d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
6683d71c7a4SMatthias Ringwald         (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
6693d71c7a4SMatthias Ringwald     }
6703d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return;
6713d71c7a4SMatthias Ringwald     (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
6723d71c7a4SMatthias Ringwald }
6733d71c7a4SMatthias Ringwald 
6743d71c7a4SMatthias Ringwald // returns first reported error or 0
6753d71c7a4SMatthias Ringwald static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){
6763d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
6773d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
6783d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
6793d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
6803d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
6813d71c7a4SMatthias Ringwald         uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
6823d71c7a4SMatthias Ringwald         if (error_code) return error_code;
6833d71c7a4SMatthias Ringwald     }
6843d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return 0;
6853d71c7a4SMatthias Ringwald     return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
6863d71c7a4SMatthias Ringwald }
6873d71c7a4SMatthias Ringwald 
6883d71c7a4SMatthias Ringwald static uint16_t att_server_read_callback(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
6893d71c7a4SMatthias Ringwald     att_read_callback_t callback = att_server_read_callback_for_handle(attribute_handle);
69001ac2008SMatthias Ringwald     if (!callback) return 0;
6913d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, offset, buffer, buffer_size);
6923d71c7a4SMatthias Ringwald }
6933d71c7a4SMatthias Ringwald 
6943d71c7a4SMatthias Ringwald static int att_server_write_callback(hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size){
6953d71c7a4SMatthias Ringwald     switch (transaction_mode){
6963d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_VALIDATE:
6973d71c7a4SMatthias Ringwald             return att_validate_prepared_write(con_handle);
6983d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_EXECUTE:
6993d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_CANCEL:
7003d71c7a4SMatthias Ringwald             att_notify_write_callbacks(con_handle, transaction_mode);
7013d71c7a4SMatthias Ringwald             return 0;
7023d71c7a4SMatthias Ringwald         default:
7033d71c7a4SMatthias Ringwald             break;
7043d71c7a4SMatthias Ringwald     }
7053d71c7a4SMatthias Ringwald 
706bf78aac6SMatthias Ringwald     // track CCC writes
707bf78aac6SMatthias Ringwald     if (att_is_persistent_ccc(attribute_handle) && offset == 0 && buffer_size == 2){
708bf78aac6SMatthias Ringwald         att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0));
709bf78aac6SMatthias Ringwald     }
710bf78aac6SMatthias Ringwald 
71101ac2008SMatthias Ringwald     att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
71201ac2008SMatthias Ringwald     if (!callback) return 0;
7133d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size);
7143d71c7a4SMatthias Ringwald }
7153d71c7a4SMatthias Ringwald 
7163d71c7a4SMatthias Ringwald /**
7173d71c7a4SMatthias Ringwald  * @brief register read/write callbacks for specific handle range
7183d71c7a4SMatthias Ringwald  * @param att_service_handler_t
7193d71c7a4SMatthias Ringwald  */
7203d71c7a4SMatthias Ringwald void att_server_register_service_handler(att_service_handler_t * handler){
7213d71c7a4SMatthias Ringwald     if (att_service_handler_for_handle(handler->start_handle) ||
7223d71c7a4SMatthias Ringwald         att_service_handler_for_handle(handler->end_handle)){
7233d71c7a4SMatthias Ringwald         log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle);
7243d71c7a4SMatthias Ringwald         return;
7253d71c7a4SMatthias Ringwald     }
7263d71c7a4SMatthias Ringwald     btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler);
7273d71c7a4SMatthias Ringwald }
7283d71c7a4SMatthias Ringwald 
7293deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){
7303deb3ec6SMatthias Ringwald 
7313d71c7a4SMatthias Ringwald     // store callbacks
7323d71c7a4SMatthias Ringwald     att_server_client_read_callback  = read_callback;
7333d71c7a4SMatthias Ringwald     att_server_client_write_callback = write_callback;
7343d71c7a4SMatthias Ringwald 
7351a389cdcSMatthias Ringwald     // register for HCI Events
736d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &att_event_packet_handler;
7371a389cdcSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
7381a389cdcSMatthias Ringwald 
739406722e4SMatthias Ringwald     // register for SM events
740d9a7306aSMatthias Ringwald     sm_event_callback_registration.callback = &att_event_packet_handler;
741406722e4SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
7423deb3ec6SMatthias Ringwald 
7431a389cdcSMatthias Ringwald     // and L2CAP ATT Server PDUs
7443deb3ec6SMatthias Ringwald     att_dispatch_register_server(att_packet_handler);
7453deb3ec6SMatthias Ringwald 
7463deb3ec6SMatthias Ringwald     att_set_db(db);
7473d71c7a4SMatthias Ringwald     att_set_read_callback(att_server_read_callback);
7483d71c7a4SMatthias Ringwald     att_set_write_callback(att_server_write_callback);
7493deb3ec6SMatthias Ringwald 
7503deb3ec6SMatthias Ringwald }
7513deb3ec6SMatthias Ringwald 
7523deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){
7533deb3ec6SMatthias Ringwald     att_client_packet_handler = handler;
7543deb3ec6SMatthias Ringwald }
7553deb3ec6SMatthias Ringwald 
756c141988cSMatthias Ringwald int  att_server_can_send_packet_now(hci_con_handle_t con_handle){
75718707219SMatthias Ringwald 	return att_dispatch_server_can_send_now(con_handle);
7581b96b007SMatthias Ringwald }
7591b96b007SMatthias Ringwald 
760c141988cSMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){
761d9d23054SMatthias Ringwald     log_debug("att_server_request_can_send_now_event 0x%04x", con_handle);
7621b96b007SMatthias Ringwald     att_client_waiting_for_can_send = 1;
76318707219SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
7643deb3ec6SMatthias Ringwald }
7653deb3ec6SMatthias Ringwald 
766bb38f057SMatthias Ringwald void att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
767bb38f057SMatthias Ringwald     // check if can send already
768bb38f057SMatthias Ringwald     if (att_dispatch_server_can_send_now(con_handle)){
769bb38f057SMatthias Ringwald         callback_registration->callback(callback_registration->context);
770bb38f057SMatthias Ringwald         return;
771bb38f057SMatthias Ringwald     }
77218707219SMatthias Ringwald     callback_registration->context = (void*)(uintptr_t) con_handle;
773bb38f057SMatthias Ringwald     btstack_linked_list_add_tail(&can_send_now_clients, (btstack_linked_item_t*) callback_registration);
774bb38f057SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
775bb38f057SMatthias Ringwald }
776bb38f057SMatthias Ringwald 
77718707219SMatthias Ringwald 
778c141988cSMatthias Ringwald int att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){
77918707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
78018707219SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
78118707219SMatthias Ringwald 
78218707219SMatthias Ringwald     if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
7833deb3ec6SMatthias Ringwald 
7843deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
7853deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
78618707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_notification(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
78718707219SMatthias Ringwald 	return l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
7883deb3ec6SMatthias Ringwald }
7893deb3ec6SMatthias Ringwald 
790c141988cSMatthias Ringwald int att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){
79118707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
79218707219SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
79318707219SMatthias Ringwald 
794eaac31e8SMatthias Ringwald     if (att_server->value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS;
79518707219SMatthias Ringwald     if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
7963deb3ec6SMatthias Ringwald 
7973deb3ec6SMatthias Ringwald     // track indication
79818707219SMatthias Ringwald     att_server->value_indication_handle = attribute_handle;
79918707219SMatthias Ringwald     btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout);
80018707219SMatthias Ringwald     btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS);
80118707219SMatthias Ringwald     btstack_run_loop_add_timer(&att_server->value_indication_timer);
8023deb3ec6SMatthias Ringwald 
8033deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
8043deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
80518707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_indication(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
80618707219SMatthias Ringwald 	l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
8073deb3ec6SMatthias Ringwald     return 0;
8083deb3ec6SMatthias Ringwald }
809