xref: /btstack/src/ble/att_server.c (revision 6afb9aca51008bcf5a8cca061ac5ecd3da81fcad)
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 
69*6afb9acaSMatthias Ringwald #ifndef NVN_NUM_GATT_SERVER_CCC
70*6afb9acaSMatthias Ringwald #define NVN_NUM_GATT_SERVER_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 
786a540790SMatthias Ringwald //
796a540790SMatthias Ringwald typedef struct {
806a540790SMatthias Ringwald     uint32_t seq_nr;
816a540790SMatthias Ringwald     uint16_t att_handle;
826a540790SMatthias Ringwald     uint8_t  value;
836a540790SMatthias Ringwald     uint8_t  device_index;
846a540790SMatthias Ringwald } persistent_ccc_entry_t;
856a540790SMatthias Ringwald 
8618707219SMatthias Ringwald // global
871a389cdcSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
88406722e4SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration;
893deb3ec6SMatthias Ringwald static btstack_packet_handler_t               att_client_packet_handler = NULL;
90bb38f057SMatthias Ringwald static btstack_linked_list_t                  can_send_now_clients;
913d71c7a4SMatthias Ringwald static btstack_linked_list_t                  service_handlers;
9218707219SMatthias Ringwald static uint8_t                                att_client_waiting_for_can_send;
9318707219SMatthias Ringwald 
943d71c7a4SMatthias Ringwald static att_read_callback_t                    att_server_client_read_callback;
953d71c7a4SMatthias Ringwald static att_write_callback_t                   att_server_client_write_callback;
963d71c7a4SMatthias Ringwald 
97bf78aac6SMatthias Ringwald // track CCC 1-entry cache
98bf78aac6SMatthias Ringwald // static att_server_t *    att_persistent_ccc_server;
99bf78aac6SMatthias Ringwald // static hci_con_handle_t  att_persistent_ccc_con_handle;
100bf78aac6SMatthias Ringwald 
10118707219SMatthias Ringwald static att_server_t * att_server_for_handle(hci_con_handle_t con_handle){
10218707219SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
10318707219SMatthias Ringwald     if (!hci_connection) return NULL;
10418707219SMatthias Ringwald     return &hci_connection->att_server;
10518707219SMatthias Ringwald }
10618707219SMatthias Ringwald 
10702b78fc8SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
10818707219SMatthias Ringwald static att_server_t * att_server_for_state(att_server_state_t state){
10918707219SMatthias Ringwald     btstack_linked_list_iterator_t it;
11018707219SMatthias Ringwald     hci_connections_get_iterator(&it);
11118707219SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
11218707219SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
11318707219SMatthias Ringwald         att_server_t * att_server = &connection->att_server;
11418707219SMatthias Ringwald         if (att_server->state == state) return att_server;
11518707219SMatthias Ringwald     }
11618707219SMatthias Ringwald     return NULL;
11718707219SMatthias Ringwald }
11802b78fc8SMatthias Ringwald #endif
119bb38f057SMatthias Ringwald 
1203deb3ec6SMatthias Ringwald static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){
1213deb3ec6SMatthias Ringwald     if (!att_client_packet_handler) return;
1223deb3ec6SMatthias Ringwald 
1233deb3ec6SMatthias Ringwald     uint8_t event[7];
1243deb3ec6SMatthias Ringwald     int pos = 0;
1255611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE;
1263deb3ec6SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
1273deb3ec6SMatthias Ringwald     event[pos++] = status;
128f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, client_handle);
1293deb3ec6SMatthias Ringwald     pos += 2;
130f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, attribute_handle);
1313deb3ec6SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1323deb3ec6SMatthias Ringwald }
1333deb3ec6SMatthias Ringwald 
134fc64f94aSMatthias Ringwald static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){
1353deb3ec6SMatthias Ringwald     if (!att_client_packet_handler) return;
1363deb3ec6SMatthias Ringwald 
1373deb3ec6SMatthias Ringwald     uint8_t event[6];
1383deb3ec6SMatthias Ringwald     int pos = 0;
1395611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE;
1403deb3ec6SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
141fc64f94aSMatthias Ringwald     little_endian_store_16(event, pos, con_handle);
1423deb3ec6SMatthias Ringwald     pos += 2;
143f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, mtu);
1443deb3ec6SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1453deb3ec6SMatthias Ringwald }
1463deb3ec6SMatthias Ringwald 
1471b96b007SMatthias Ringwald static void att_emit_can_send_now_event(void){
1483c97b242SMatthias Ringwald     if (!att_client_packet_handler) return;
1493c97b242SMatthias Ringwald 
1501b96b007SMatthias Ringwald     uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0};
1511b96b007SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1521b96b007SMatthias Ringwald }
1531b96b007SMatthias Ringwald 
154ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){
15554178543SMatthias Ringwald     void * context = btstack_run_loop_get_timer_context(ts);
15654178543SMatthias Ringwald     hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
15718707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
15818707219SMatthias Ringwald     if (!att_server) return;
15918707219SMatthias Ringwald     uint16_t att_handle = att_server->value_indication_handle;
16018707219SMatthias Ringwald     att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_server->connection.con_handle, att_handle);
1613deb3ec6SMatthias Ringwald }
1623deb3ec6SMatthias Ringwald 
163760ad805SMatthias Ringwald static void att_device_identified(att_server_t * att_server, uint8_t index){
164e62c502cSMatthias Ringwald     att_server->ir_lookup_active = 0;
165e62c502cSMatthias Ringwald     att_server->ir_le_device_db_index = index;
166760ad805SMatthias 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);
167e62c502cSMatthias Ringwald     att_run_for_context(att_server);
168e62c502cSMatthias Ringwald }
169e62c502cSMatthias Ringwald 
1703deb3ec6SMatthias Ringwald static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1719ec2630cSMatthias Ringwald 
1727dad9ff8SMatthias Ringwald     UNUSED(channel); // ok: there is no channel
1737dad9ff8SMatthias Ringwald     UNUSED(size);    // ok: handling own l2cap events
1743deb3ec6SMatthias Ringwald 
17518707219SMatthias Ringwald     att_server_t * att_server;
17618707219SMatthias Ringwald     hci_con_handle_t con_handle;
17718707219SMatthias Ringwald 
1783deb3ec6SMatthias Ringwald     switch (packet_type) {
1793deb3ec6SMatthias Ringwald 
1803deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1810e2df43fSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
1823deb3ec6SMatthias Ringwald 
1833deb3ec6SMatthias Ringwald                 case HCI_EVENT_LE_META:
1843deb3ec6SMatthias Ringwald                     switch (packet[2]) {
1853deb3ec6SMatthias Ringwald                         case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
18618707219SMatthias Ringwald                             con_handle = little_endian_read_16(packet, 4);
18718707219SMatthias Ringwald                             att_server = att_server_for_handle(con_handle);
18818707219SMatthias Ringwald                             if (!att_server) break;
1893deb3ec6SMatthias Ringwald                         	// store connection info
19018707219SMatthias Ringwald                         	att_server->peer_addr_type = packet[7];
19118707219SMatthias Ringwald                             reverse_bd_addr(&packet[8], att_server->peer_address);
19218707219SMatthias Ringwald                             att_server->connection.con_handle = con_handle;
1933deb3ec6SMatthias Ringwald                             // reset connection properties
19418707219SMatthias Ringwald                             att_server->state = ATT_SERVER_IDLE;
19518707219SMatthias Ringwald                             att_server->connection.mtu = ATT_DEFAULT_MTU;
19618707219SMatthias Ringwald                             att_server->connection.max_mtu = l2cap_max_le_mtu();
19718707219SMatthias Ringwald                             if (att_server->connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){
19818707219SMatthias Ringwald                                 att_server->connection.max_mtu = ATT_REQUEST_BUFFER_SIZE;
19999c58ad0SMatthias Ringwald                             }
20018707219SMatthias Ringwald                             att_server->connection.encryption_key_size = 0;
20118707219SMatthias Ringwald                             att_server->connection.authenticated = 0;
20218707219SMatthias Ringwald 		                	att_server->connection.authorized = 0;
203b2c1e52cSMatthias Ringwald                             // workaround: identity resolving can already be complete, at least store result
204b2c1e52cSMatthias Ringwald                             att_server->ir_le_device_db_index = sm_le_device_index(con_handle);
205760ad805SMatthias Ringwald                             att_server->pairing_active = 0;
2063deb3ec6SMatthias Ringwald                             break;
2073deb3ec6SMatthias Ringwald 
2083deb3ec6SMatthias Ringwald                         default:
2093deb3ec6SMatthias Ringwald                             break;
2103deb3ec6SMatthias Ringwald                     }
2113deb3ec6SMatthias Ringwald                     break;
2123deb3ec6SMatthias Ringwald 
2133deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_CHANGE:
2143deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE:
2153deb3ec6SMatthias Ringwald                 	// check handle
21618707219SMatthias Ringwald                     con_handle = little_endian_read_16(packet, 3);
21718707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
21818707219SMatthias Ringwald                     if (!att_server) break;
21918707219SMatthias Ringwald                 	att_server->connection.encryption_key_size = sm_encryption_key_size(con_handle);
22018707219SMatthias Ringwald                 	att_server->connection.authenticated = sm_authenticated(con_handle);
22101ac2008SMatthias Ringwald                     if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){
22201ac2008SMatthias Ringwald                         // restore CCC values when encrypted
22301ac2008SMatthias Ringwald                         if (hci_event_encryption_change_get_encryption_enabled(packet)){
22401ac2008SMatthias Ringwald                             att_server_persistent_ccc_restore(att_server);
22501ac2008SMatthias Ringwald                         }
22601ac2008SMatthias Ringwald                     }
2273deb3ec6SMatthias Ringwald                 	break;
2283deb3ec6SMatthias Ringwald 
2293deb3ec6SMatthias Ringwald                 case HCI_EVENT_DISCONNECTION_COMPLETE:
23070a390c7SMatthias Ringwald                     // check handle
23118707219SMatthias Ringwald                     con_handle = hci_event_disconnection_complete_get_connection_handle(packet);
23218707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
23318707219SMatthias Ringwald                     if (!att_server) break;
23418707219SMatthias Ringwald                     att_clear_transaction_queue(&att_server->connection);
23518707219SMatthias Ringwald                     att_server->connection.con_handle = 0;
23618707219SMatthias Ringwald                     att_server->value_indication_handle = 0; // reset error state
237760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
23818707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
2393deb3ec6SMatthias Ringwald                     break;
2403deb3ec6SMatthias Ringwald 
241760ad805SMatthias Ringwald                 // Identity Resolving
2425611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_STARTED:
24318707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_started_get_handle(packet);
24418707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
24518707219SMatthias Ringwald                     if (!att_server) break;
2465611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED");
24718707219SMatthias Ringwald                     att_server->ir_lookup_active = 1;
2483deb3ec6SMatthias Ringwald                     break;
2495611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
250760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
251760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
252760ad805SMatthias Ringwald                     if (!att_server) return;
253760ad805SMatthias Ringwald                     att_device_identified(att_server, sm_event_identity_resolving_succeeded_get_index(packet));
2543deb3ec6SMatthias Ringwald                     break;
2555611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_FAILED:
25618707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_failed_get_handle(packet);
25718707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
25818707219SMatthias Ringwald                     if (!att_server) break;
2595611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED");
26018707219SMatthias Ringwald                     att_server->ir_lookup_active = 0;
26118707219SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
26218707219SMatthias Ringwald                     att_run_for_context(att_server);
2633deb3ec6SMatthias Ringwald                     break;
264760ad805SMatthias Ringwald 
265760ad805SMatthias Ringwald                 // Pairing started - delete stored CCC values
266760ad805SMatthias Ringwald                 // - assumes pairing indicates either new device or re-pairing, in both cases there should be no stored CCC values
267760ad805SMatthias Ringwald                 // - assumes that all events have the con handle as the first field
268760ad805SMatthias Ringwald                 case SM_EVENT_JUST_WORKS_REQUEST:
269760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
270760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_INPUT_NUMBER:
271760ad805SMatthias Ringwald                 case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
272760ad805SMatthias Ringwald                     con_handle = sm_event_just_works_request_get_handle(packet);
273760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
274760ad805SMatthias Ringwald                     if (!att_server) break;
275760ad805SMatthias Ringwald                     att_server->pairing_active = 1;
276760ad805SMatthias Ringwald                     log_info("SM Pairing started");
277760ad805SMatthias Ringwald                     if (att_server->ir_le_device_db_index < 0) break;
278760ad805SMatthias Ringwald                     att_server_persistent_ccc_clear(att_server);
279760ad805SMatthias Ringwald                     // index not valid anymore
280760ad805SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
281760ad805SMatthias Ringwald                     break;
282760ad805SMatthias Ringwald 
283760ad805SMatthias Ringwald                 // Pairing completed
284760ad805SMatthias Ringwald                 case SM_EVENT_IDENTITY_CREATED:
285760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
286760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
287760ad805SMatthias Ringwald                     if (!att_server) return;
288760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
289760ad805SMatthias Ringwald                     att_device_identified(att_server, sm_event_identity_created_get_index(packet));
290760ad805SMatthias Ringwald                     break;
291760ad805SMatthias Ringwald 
292760ad805SMatthias Ringwald                 // Authorization
2935611a760SMatthias Ringwald                 case SM_EVENT_AUTHORIZATION_RESULT: {
29418707219SMatthias Ringwald                     con_handle = sm_event_authorization_result_get_handle(packet);
29518707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
29618707219SMatthias Ringwald                     if (!att_server) break;
29718707219SMatthias Ringwald                     att_server->connection.authorized = sm_event_authorization_result_get_authorization_result(packet);
29818707219SMatthias Ringwald                     att_dispatch_server_request_can_send_now_event(con_handle);
2993deb3ec6SMatthias Ringwald                 	break;
3003deb3ec6SMatthias Ringwald                 }
3013deb3ec6SMatthias Ringwald                 default:
3023deb3ec6SMatthias Ringwald                     break;
3033deb3ec6SMatthias Ringwald             }
30465b44ffdSMatthias Ringwald             break;
30565b44ffdSMatthias Ringwald         default:
30665b44ffdSMatthias Ringwald             break;
3073deb3ec6SMatthias Ringwald     }
3083deb3ec6SMatthias Ringwald }
3093deb3ec6SMatthias Ringwald 
3107a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
3113deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
3123deb3ec6SMatthias Ringwald 
31318707219SMatthias Ringwald     att_server_t * att_server = att_server_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION);
31418707219SMatthias Ringwald     if (!att_server) return;
3153deb3ec6SMatthias Ringwald 
3163deb3ec6SMatthias Ringwald     uint8_t hash_flipped[8];
3179c80e4ccSMatthias Ringwald     reverse_64(hash, hash_flipped);
31818707219SMatthias Ringwald     if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8)){
3193deb3ec6SMatthias Ringwald         log_info("ATT Signed Write, invalid signature");
32018707219SMatthias Ringwald         att_server->state = ATT_SERVER_IDLE;
3213deb3ec6SMatthias Ringwald         return;
3223deb3ec6SMatthias Ringwald     }
3233deb3ec6SMatthias Ringwald     log_info("ATT Signed Write, valid signature");
3243deb3ec6SMatthias Ringwald 
3253deb3ec6SMatthias Ringwald     // update sequence number
32618707219SMatthias Ringwald     uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
32718707219SMatthias Ringwald     le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1);
32818707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
32918707219SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
3303deb3ec6SMatthias Ringwald }
3317a766ebfSMatthias Ringwald #endif
33288a48dd8SMatthias Ringwald 
33318707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED
334b06f2579SMatthias Ringwald // pre: can send now
335b06f2579SMatthias Ringwald // returns: 1 if packet was sent
33618707219SMatthias Ringwald static int att_server_process_validated_request(att_server_t * att_server){
337b06f2579SMatthias Ringwald 
338b06f2579SMatthias Ringwald     l2cap_reserve_packet_buffer();
339b06f2579SMatthias Ringwald     uint8_t * att_response_buffer = l2cap_get_outgoing_buffer();
34018707219SMatthias Ringwald     uint16_t  att_response_size   = att_handle_request(&att_server->connection, att_server->request_buffer, att_server->request_size, att_response_buffer);
341b06f2579SMatthias Ringwald 
342b06f2579SMatthias Ringwald     // intercept "insufficient authorization" for authenticated connections to allow for user authorization
343b06f2579SMatthias Ringwald     if ((att_response_size     >= 4)
344b06f2579SMatthias Ringwald     && (att_response_buffer[0] == ATT_ERROR_RESPONSE)
345b06f2579SMatthias Ringwald     && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION)
34618707219SMatthias Ringwald     && (att_server->connection.authenticated)){
347b06f2579SMatthias Ringwald 
34818707219SMatthias Ringwald         switch (sm_authorization_state(att_server->connection.con_handle)){
349b06f2579SMatthias Ringwald             case AUTHORIZATION_UNKNOWN:
350b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
35118707219SMatthias Ringwald                 sm_request_pairing(att_server->connection.con_handle);
352b06f2579SMatthias Ringwald                 return 0;
353b06f2579SMatthias Ringwald             case AUTHORIZATION_PENDING:
354b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
355b06f2579SMatthias Ringwald                 return 0;
356b06f2579SMatthias Ringwald             default:
357b06f2579SMatthias Ringwald                 break;
358b06f2579SMatthias Ringwald         }
359b06f2579SMatthias Ringwald     }
360b06f2579SMatthias Ringwald 
36118707219SMatthias Ringwald     att_server->state = ATT_SERVER_IDLE;
362b06f2579SMatthias Ringwald     if (att_response_size == 0) {
363b06f2579SMatthias Ringwald         l2cap_release_packet_buffer();
364b06f2579SMatthias Ringwald         return 0;
365b06f2579SMatthias Ringwald     }
366b06f2579SMatthias Ringwald 
36718707219SMatthias Ringwald     l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size);
368b06f2579SMatthias Ringwald 
369b06f2579SMatthias Ringwald     // notify client about MTU exchange result
370b06f2579SMatthias Ringwald     if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){
37118707219SMatthias Ringwald         att_emit_mtu_event(att_server->connection.con_handle, att_server->connection.mtu);
372b06f2579SMatthias Ringwald     }
373b06f2579SMatthias Ringwald     return 1;
374b06f2579SMatthias Ringwald }
375b06f2579SMatthias Ringwald 
37618707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server){
37718707219SMatthias Ringwald     switch (att_server->state){
3783deb3ec6SMatthias Ringwald         case ATT_SERVER_REQUEST_RECEIVED:
379760ad805SMatthias Ringwald 
380760ad805SMatthias Ringwald             // wait until pairing is complete
381760ad805SMatthias Ringwald             if (att_server->pairing_active) break;
382760ad805SMatthias Ringwald 
3837a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
38418707219SMatthias Ringwald             if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){
3853deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write!");
3863deb3ec6SMatthias Ringwald                 if (!sm_cmac_ready()) {
3873deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, sm_cmac engine not ready. Abort");
38818707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
3893deb3ec6SMatthias Ringwald                     return;
3903deb3ec6SMatthias Ringwald                 }
39118707219SMatthias Ringwald                 if (att_server->request_size < (3 + 12)) {
3923deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, request to short. Abort.");
39318707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
3943deb3ec6SMatthias Ringwald                     return;
3953deb3ec6SMatthias Ringwald                 }
39618707219SMatthias Ringwald                 if (att_server->ir_lookup_active){
3973deb3ec6SMatthias Ringwald                     return;
3983deb3ec6SMatthias Ringwald                 }
39918707219SMatthias Ringwald                 if (att_server->ir_le_device_db_index < 0){
4003deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, CSRK not available");
40118707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
4023deb3ec6SMatthias Ringwald                     return;
4033deb3ec6SMatthias Ringwald                 }
4043deb3ec6SMatthias Ringwald 
4053deb3ec6SMatthias Ringwald                 // check counter
40618707219SMatthias Ringwald                 uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
40718707219SMatthias Ringwald                 uint32_t counter_db     = le_device_db_remote_counter_get(att_server->ir_le_device_db_index);
4083deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet);
4093deb3ec6SMatthias Ringwald                 if (counter_packet < counter_db){
4103deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, db reports higher counter, abort");
41118707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
4123deb3ec6SMatthias Ringwald                     return;
4133deb3ec6SMatthias Ringwald                 }
4143deb3ec6SMatthias Ringwald 
4153deb3ec6SMatthias Ringwald                 // signature is { sequence counter, secure hash }
4163deb3ec6SMatthias Ringwald                 sm_key_t csrk;
41718707219SMatthias Ringwald                 le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk);
41818707219SMatthias Ringwald                 att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION;
4193deb3ec6SMatthias Ringwald                 log_info("Orig Signature: ");
42018707219SMatthias Ringwald                 log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8);
42118707219SMatthias Ringwald                 uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1);
42218707219SMatthias 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);
4233deb3ec6SMatthias Ringwald                 return;
4243deb3ec6SMatthias Ringwald             }
4257a766ebfSMatthias Ringwald #endif
426b06f2579SMatthias Ringwald             // move on
42718707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
42818707219SMatthias Ringwald             att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
429b06f2579SMatthias Ringwald             break;
43088a48dd8SMatthias Ringwald 
4313deb3ec6SMatthias Ringwald         default:
4323deb3ec6SMatthias Ringwald             break;
4333deb3ec6SMatthias Ringwald     }
4343deb3ec6SMatthias Ringwald }
4353deb3ec6SMatthias Ringwald 
4367eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){
437b06f2579SMatthias Ringwald 
438b06f2579SMatthias Ringwald     // NOTE: we get l2cap fixed channel instead of con_handle
439b06f2579SMatthias Ringwald 
44018707219SMatthias Ringwald     btstack_linked_list_iterator_t it;
44118707219SMatthias Ringwald     hci_connections_get_iterator(&it);
44218707219SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
44318707219SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
44418707219SMatthias Ringwald         att_server_t * att_server = &connection->att_server;
44518707219SMatthias Ringwald         if (att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED){
44618707219SMatthias Ringwald             int sent = att_server_process_validated_request(att_server);
447bb38f057SMatthias Ringwald             if (sent && (att_client_waiting_for_can_send || !btstack_linked_list_empty(&can_send_now_clients))){
44818707219SMatthias Ringwald                 att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
4493deb3ec6SMatthias Ringwald                 return;
4503deb3ec6SMatthias Ringwald             }
4513deb3ec6SMatthias Ringwald         }
45218707219SMatthias Ringwald     }
4533deb3ec6SMatthias Ringwald 
454bb38f057SMatthias Ringwald     while (!btstack_linked_list_empty(&can_send_now_clients)){
455bb38f057SMatthias Ringwald         // handle first client
456bb38f057SMatthias Ringwald         btstack_context_callback_registration_t * client = (btstack_context_callback_registration_t*) can_send_now_clients;
45718707219SMatthias Ringwald         hci_con_handle_t con_handle = (uintptr_t) client->context;
458bb38f057SMatthias Ringwald         btstack_linked_list_remove(&can_send_now_clients, (btstack_linked_item_t *) client);
459bb38f057SMatthias Ringwald         client->callback(client->context);
460bb38f057SMatthias Ringwald 
461bb38f057SMatthias Ringwald         // request again if needed
46218707219SMatthias Ringwald         if (!att_dispatch_server_can_send_now(con_handle)){
463bb38f057SMatthias Ringwald             if (!btstack_linked_list_empty(&can_send_now_clients) || att_client_waiting_for_can_send){
46418707219SMatthias Ringwald                 att_dispatch_server_request_can_send_now_event(con_handle);
465bb38f057SMatthias Ringwald             }
466bb38f057SMatthias Ringwald             return;
467bb38f057SMatthias Ringwald         }
468bb38f057SMatthias Ringwald     }
469bb38f057SMatthias Ringwald 
470b06f2579SMatthias Ringwald     if (att_client_waiting_for_can_send){
471b06f2579SMatthias Ringwald         att_client_waiting_for_can_send = 0;
472b06f2579SMatthias Ringwald         att_emit_can_send_now_event();
4733deb3ec6SMatthias Ringwald     }
4743deb3ec6SMatthias Ringwald }
4753deb3ec6SMatthias Ringwald 
47688a48dd8SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
47718707219SMatthias Ringwald 
47818707219SMatthias Ringwald     att_server_t * att_server;
47918707219SMatthias Ringwald 
48088a48dd8SMatthias Ringwald     switch (packet_type){
48188a48dd8SMatthias Ringwald 
48288a48dd8SMatthias Ringwald         case HCI_EVENT_PACKET:
48388a48dd8SMatthias Ringwald             if (packet[0] != L2CAP_EVENT_CAN_SEND_NOW) break;
4844d2be802SMatthias Ringwald             att_server_handle_can_send_now();
485372d62c4SMatthias Ringwald             break;
4863deb3ec6SMatthias Ringwald 
487372d62c4SMatthias Ringwald         case ATT_DATA_PACKET:
48818707219SMatthias Ringwald             log_info("ATT Packet, handle 0x%04x", handle);
48918707219SMatthias Ringwald             att_server = att_server_for_handle(handle);
49018707219SMatthias Ringwald             if (!att_server) break;
49118707219SMatthias Ringwald 
4923deb3ec6SMatthias Ringwald             // handle value indication confirms
49318707219SMatthias Ringwald             if (packet[0] == ATT_HANDLE_VALUE_CONFIRMATION && att_server->value_indication_handle){
49418707219SMatthias Ringwald                 btstack_run_loop_remove_timer(&att_server->value_indication_timer);
49518707219SMatthias Ringwald                 uint16_t att_handle = att_server->value_indication_handle;
49618707219SMatthias Ringwald                 att_server->value_indication_handle = 0;
49718707219SMatthias Ringwald                 att_handle_value_indication_notify_client(0, att_server->connection.con_handle, att_handle);
4983deb3ec6SMatthias Ringwald                 return;
4993deb3ec6SMatthias Ringwald             }
5003deb3ec6SMatthias Ringwald 
5016428eb5aSMatthias Ringwald             // directly process command
5026428eb5aSMatthias Ringwald             // note: signed write cannot be handled directly as authentication needs to be verified
5036428eb5aSMatthias Ringwald             if (packet[0] == ATT_WRITE_COMMAND){
50418707219SMatthias Ringwald                 att_handle_request(&att_server->connection, packet, size, 0);
5056428eb5aSMatthias Ringwald                 return;
5066428eb5aSMatthias Ringwald             }
5076428eb5aSMatthias Ringwald 
5083deb3ec6SMatthias Ringwald             // check size
50918707219SMatthias Ringwald             if (size > sizeof(att_server->request_buffer)) {
51018707219SMatthias 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));
5116428eb5aSMatthias Ringwald                 return;
5126428eb5aSMatthias Ringwald             }
5133deb3ec6SMatthias Ringwald 
5143deb3ec6SMatthias Ringwald             // last request still in processing?
51518707219SMatthias Ringwald             if (att_server->state != ATT_SERVER_IDLE){
51618707219SMatthias Ringwald                 log_info("att_packet_handler: skipping att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state);
5176428eb5aSMatthias Ringwald                 return;
5186428eb5aSMatthias Ringwald             }
5193deb3ec6SMatthias Ringwald 
5203deb3ec6SMatthias Ringwald             // store request
52118707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED;
52218707219SMatthias Ringwald             att_server->request_size = size;
52318707219SMatthias Ringwald             memcpy(att_server->request_buffer, packet, size);
5243deb3ec6SMatthias Ringwald 
52518707219SMatthias Ringwald             att_run_for_context(att_server);
526372d62c4SMatthias Ringwald             break;
527372d62c4SMatthias Ringwald     }
5283deb3ec6SMatthias Ringwald }
5293deb3ec6SMatthias Ringwald 
530bf78aac6SMatthias Ringwald // ---------------------
531bf78aac6SMatthias Ringwald // persistent CCC writes
532bf78aac6SMatthias Ringwald static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){
533bf78aac6SMatthias Ringwald     return 'B' << 24 | 'T' << 16 | 'C' << 8 | index;
534bf78aac6SMatthias Ringwald }
535bf78aac6SMatthias Ringwald 
536bf78aac6SMatthias Ringwald static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){
537bf78aac6SMatthias Ringwald     // lookup att_server instance
538bf78aac6SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
539bf78aac6SMatthias Ringwald     if (!att_server) return;
540bf78aac6SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
541bf78aac6SMatthias 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);
5426a540790SMatthias Ringwald 
543bf78aac6SMatthias Ringwald     // check if bonded
544bf78aac6SMatthias Ringwald     if (le_device_index < 0) return;
5456a540790SMatthias Ringwald 
546bf78aac6SMatthias Ringwald     // get btstack_tlv
547bf78aac6SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
548bf78aac6SMatthias Ringwald     void * tlv_context;
549bf78aac6SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
550bf78aac6SMatthias Ringwald     if (!tlv_impl) return;
5516a540790SMatthias Ringwald 
552bf78aac6SMatthias Ringwald     // update ccc tag
553bf78aac6SMatthias Ringwald     int index;
5546a540790SMatthias Ringwald     uint32_t highest_seq_nr = 0;
5556a540790SMatthias Ringwald     uint32_t lowest_seq_nr = 0;
5566a540790SMatthias Ringwald     uint32_t tag_for_lowest_seq_nr = 0;
5576a540790SMatthias Ringwald     uint32_t tag_for_empty = 0;
5586a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
559*6afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
560bf78aac6SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
5616a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
5626a540790SMatthias Ringwald 
5636a540790SMatthias Ringwald         // empty/invalid tag
5646a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)){
5656a540790SMatthias Ringwald             tag_for_empty = tag;
566bf78aac6SMatthias Ringwald             continue;
567bf78aac6SMatthias Ringwald         }
5686a540790SMatthias Ringwald         // update highest seq nr
5696a540790SMatthias Ringwald         if (entry.seq_nr > highest_seq_nr){
5706a540790SMatthias Ringwald             highest_seq_nr = entry.seq_nr;
5716a540790SMatthias Ringwald         }
5726a540790SMatthias Ringwald         // find entry with lowest seq nr
5736a540790SMatthias Ringwald         if ((tag_for_lowest_seq_nr == 0) || (entry.seq_nr < lowest_seq_nr)){
5746a540790SMatthias Ringwald             tag_for_lowest_seq_nr = tag;
5756a540790SMatthias Ringwald             lowest_seq_nr = entry.seq_nr;
5766a540790SMatthias Ringwald         }
5776a540790SMatthias Ringwald 
5786a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
5796a540790SMatthias Ringwald         if (entry.att_handle   != att_handle)      continue;
5806a540790SMatthias Ringwald 
5816a540790SMatthias Ringwald         // found matching entry
58201ac2008SMatthias Ringwald         if (value){
583bf78aac6SMatthias Ringwald             // update
5846a540790SMatthias Ringwald             if (entry.value == value) {
585760ad805SMatthias Ringwald                 log_info("CCC Index %u: Up-to-date", index);
586760ad805SMatthias Ringwald                 return;
587760ad805SMatthias Ringwald             }
5886a540790SMatthias Ringwald             entry.value = value;
5896a540790SMatthias Ringwald             entry.seq_nr = highest_seq_nr + 1;
590760ad805SMatthias Ringwald             log_info("CCC Index %u: Store", index);
5916a540790SMatthias Ringwald             tlv_impl->store_tag(tlv_context, tag, (const uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
59201ac2008SMatthias Ringwald         } else {
59301ac2008SMatthias Ringwald             // delete
594760ad805SMatthias Ringwald             log_info("CCC Index %u: Delete", index);
59501ac2008SMatthias Ringwald             tlv_impl->delete_tag(tlv_context, tag);
59601ac2008SMatthias Ringwald         }
597bf78aac6SMatthias Ringwald         return;
598bf78aac6SMatthias Ringwald     }
5996a540790SMatthias Ringwald 
6006a540790SMatthias Ringwald     log_info("tag_for_empy %x, tag_for_lowest_seq_nr %x", tag_for_empty, tag_for_lowest_seq_nr);
6016a540790SMatthias Ringwald 
6026a540790SMatthias Ringwald     if (value == 0){
6036a540790SMatthias Ringwald         // done
6046a540790SMatthias Ringwald         return;
6056a540790SMatthias Ringwald     }
6066a540790SMatthias Ringwald 
6076a540790SMatthias Ringwald     uint32_t tag_to_use = 0;
6086a540790SMatthias Ringwald     if (tag_for_empty){
6096a540790SMatthias Ringwald         tag_to_use = tag_for_empty;
6106a540790SMatthias Ringwald     } else if (tag_for_lowest_seq_nr){
6116a540790SMatthias Ringwald         tag_to_use = tag_for_lowest_seq_nr;
6126a540790SMatthias Ringwald     } else {
6136a540790SMatthias Ringwald         // should not happen
6146a540790SMatthias Ringwald         return;
6156a540790SMatthias Ringwald     }
616bf78aac6SMatthias Ringwald     // store ccc tag
6176a540790SMatthias Ringwald     entry.seq_nr       = highest_seq_nr + 1;
6186a540790SMatthias Ringwald     entry.device_index = le_device_index;
6196a540790SMatthias Ringwald     entry.att_handle   = att_handle;
6206a540790SMatthias Ringwald     entry.value        = value;
6216a540790SMatthias Ringwald     tlv_impl->store_tag(tlv_context, tag_to_use, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
622bf78aac6SMatthias Ringwald }
623bf78aac6SMatthias Ringwald 
624760ad805SMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server){
625760ad805SMatthias Ringwald     if (!att_server) return;
626760ad805SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
627760ad805SMatthias Ringwald     log_info("Clear CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
628760ad805SMatthias Ringwald     // check if bonded
629760ad805SMatthias Ringwald     if (le_device_index < 0) return;
630760ad805SMatthias Ringwald     // get btstack_tlv
631760ad805SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
632760ad805SMatthias Ringwald     void * tlv_context;
633760ad805SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
634760ad805SMatthias Ringwald     if (!tlv_impl) return;
635760ad805SMatthias Ringwald     // get all ccc tag
636760ad805SMatthias Ringwald     int index;
6376a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
638*6afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
639760ad805SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
6406a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
6416a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
6426a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
643760ad805SMatthias Ringwald         // delete entry
644760ad805SMatthias Ringwald         log_info("CCC Index %u: Delete", index);
645760ad805SMatthias Ringwald         tlv_impl->delete_tag(tlv_context, tag);
646760ad805SMatthias Ringwald     }
647760ad805SMatthias Ringwald }
648760ad805SMatthias Ringwald 
64901ac2008SMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server){
65001ac2008SMatthias Ringwald     if (!att_server) return;
65101ac2008SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
65201ac2008SMatthias Ringwald     log_info("Restore CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
65301ac2008SMatthias Ringwald     // get btstack_tlv
65401ac2008SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
65501ac2008SMatthias Ringwald     void * tlv_context;
65601ac2008SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
65701ac2008SMatthias Ringwald     if (!tlv_impl) return;
65801ac2008SMatthias Ringwald     // get all ccc tag
65901ac2008SMatthias Ringwald     int index;
6606a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
661*6afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
66201ac2008SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
6636a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
6646a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
6656a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
66601ac2008SMatthias Ringwald         // simulate write callback
6676a540790SMatthias Ringwald         uint16_t attribute_handle = entry.att_handle;
66801ac2008SMatthias Ringwald         uint8_t  value[2];
6696a540790SMatthias Ringwald         little_endian_store_16(value, 0, entry.value);
67001ac2008SMatthias Ringwald         att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
67101ac2008SMatthias Ringwald         if (!callback) continue;
6726a540790SMatthias Ringwald         log_info("CCC Index %u: Set Attribute handle 0x%04x to value 0x%04x", index, attribute_handle, entry.value );
67301ac2008SMatthias Ringwald         (*callback)(att_server->connection.con_handle, attribute_handle, ATT_TRANSACTION_MODE_NONE, 0, value, sizeof(value));
67401ac2008SMatthias Ringwald     }
67501ac2008SMatthias Ringwald }
67601ac2008SMatthias Ringwald 
677bf78aac6SMatthias Ringwald // persistent CCC writes
678bf78aac6SMatthias Ringwald // ---------------------
6793d71c7a4SMatthias Ringwald 
6803d71c7a4SMatthias Ringwald // gatt service management
6813d71c7a4SMatthias Ringwald static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){
6823d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
6833d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
6843d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
6853d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
6863d71c7a4SMatthias Ringwald         if (handler->start_handle > handle) continue;
6873d71c7a4SMatthias Ringwald         if (handler->end_handle   < handle) continue;
6883d71c7a4SMatthias Ringwald         return handler;
6893d71c7a4SMatthias Ringwald     }
6903d71c7a4SMatthias Ringwald     return NULL;
6913d71c7a4SMatthias Ringwald }
6923d71c7a4SMatthias Ringwald static att_read_callback_t att_server_read_callback_for_handle(uint16_t handle){
6933d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
6943d71c7a4SMatthias Ringwald     if (handler) return handler->read_callback;
6953d71c7a4SMatthias Ringwald     return att_server_client_read_callback;
6963d71c7a4SMatthias Ringwald }
6973d71c7a4SMatthias Ringwald 
6983d71c7a4SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){
6993d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
7003d71c7a4SMatthias Ringwald     if (handler) return handler->write_callback;
7013d71c7a4SMatthias Ringwald     return att_server_client_write_callback;
7023d71c7a4SMatthias Ringwald }
7033d71c7a4SMatthias Ringwald 
7043d71c7a4SMatthias Ringwald static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){
7053d71c7a4SMatthias Ringwald     // notify all callbacks
7063d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
7073d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
7083d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
7093d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
7103d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
7113d71c7a4SMatthias Ringwald         (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
7123d71c7a4SMatthias Ringwald     }
7133d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return;
7143d71c7a4SMatthias Ringwald     (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
7153d71c7a4SMatthias Ringwald }
7163d71c7a4SMatthias Ringwald 
7173d71c7a4SMatthias Ringwald // returns first reported error or 0
7183d71c7a4SMatthias Ringwald static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){
7193d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
7203d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
7213d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
7223d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
7233d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
7243d71c7a4SMatthias Ringwald         uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
7253d71c7a4SMatthias Ringwald         if (error_code) return error_code;
7263d71c7a4SMatthias Ringwald     }
7273d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return 0;
7283d71c7a4SMatthias Ringwald     return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
7293d71c7a4SMatthias Ringwald }
7303d71c7a4SMatthias Ringwald 
7313d71c7a4SMatthias 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){
7323d71c7a4SMatthias Ringwald     att_read_callback_t callback = att_server_read_callback_for_handle(attribute_handle);
73301ac2008SMatthias Ringwald     if (!callback) return 0;
7343d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, offset, buffer, buffer_size);
7353d71c7a4SMatthias Ringwald }
7363d71c7a4SMatthias Ringwald 
7373d71c7a4SMatthias 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){
7383d71c7a4SMatthias Ringwald     switch (transaction_mode){
7393d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_VALIDATE:
7403d71c7a4SMatthias Ringwald             return att_validate_prepared_write(con_handle);
7413d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_EXECUTE:
7423d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_CANCEL:
7433d71c7a4SMatthias Ringwald             att_notify_write_callbacks(con_handle, transaction_mode);
7443d71c7a4SMatthias Ringwald             return 0;
7453d71c7a4SMatthias Ringwald         default:
7463d71c7a4SMatthias Ringwald             break;
7473d71c7a4SMatthias Ringwald     }
7483d71c7a4SMatthias Ringwald 
749bf78aac6SMatthias Ringwald     // track CCC writes
750bf78aac6SMatthias Ringwald     if (att_is_persistent_ccc(attribute_handle) && offset == 0 && buffer_size == 2){
751bf78aac6SMatthias Ringwald         att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0));
752bf78aac6SMatthias Ringwald     }
753bf78aac6SMatthias Ringwald 
75401ac2008SMatthias Ringwald     att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
75501ac2008SMatthias Ringwald     if (!callback) return 0;
7563d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size);
7573d71c7a4SMatthias Ringwald }
7583d71c7a4SMatthias Ringwald 
7593d71c7a4SMatthias Ringwald /**
7603d71c7a4SMatthias Ringwald  * @brief register read/write callbacks for specific handle range
7613d71c7a4SMatthias Ringwald  * @param att_service_handler_t
7623d71c7a4SMatthias Ringwald  */
7633d71c7a4SMatthias Ringwald void att_server_register_service_handler(att_service_handler_t * handler){
7643d71c7a4SMatthias Ringwald     if (att_service_handler_for_handle(handler->start_handle) ||
7653d71c7a4SMatthias Ringwald         att_service_handler_for_handle(handler->end_handle)){
7663d71c7a4SMatthias Ringwald         log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle);
7673d71c7a4SMatthias Ringwald         return;
7683d71c7a4SMatthias Ringwald     }
7693d71c7a4SMatthias Ringwald     btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler);
7703d71c7a4SMatthias Ringwald }
7713d71c7a4SMatthias Ringwald 
7723deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){
7733deb3ec6SMatthias Ringwald 
7743d71c7a4SMatthias Ringwald     // store callbacks
7753d71c7a4SMatthias Ringwald     att_server_client_read_callback  = read_callback;
7763d71c7a4SMatthias Ringwald     att_server_client_write_callback = write_callback;
7773d71c7a4SMatthias Ringwald 
7781a389cdcSMatthias Ringwald     // register for HCI Events
779d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &att_event_packet_handler;
7801a389cdcSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
7811a389cdcSMatthias Ringwald 
782406722e4SMatthias Ringwald     // register for SM events
783d9a7306aSMatthias Ringwald     sm_event_callback_registration.callback = &att_event_packet_handler;
784406722e4SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
7853deb3ec6SMatthias Ringwald 
7861a389cdcSMatthias Ringwald     // and L2CAP ATT Server PDUs
7873deb3ec6SMatthias Ringwald     att_dispatch_register_server(att_packet_handler);
7883deb3ec6SMatthias Ringwald 
7893deb3ec6SMatthias Ringwald     att_set_db(db);
7903d71c7a4SMatthias Ringwald     att_set_read_callback(att_server_read_callback);
7913d71c7a4SMatthias Ringwald     att_set_write_callback(att_server_write_callback);
7923deb3ec6SMatthias Ringwald 
7933deb3ec6SMatthias Ringwald }
7943deb3ec6SMatthias Ringwald 
7953deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){
7963deb3ec6SMatthias Ringwald     att_client_packet_handler = handler;
7973deb3ec6SMatthias Ringwald }
7983deb3ec6SMatthias Ringwald 
799c141988cSMatthias Ringwald int  att_server_can_send_packet_now(hci_con_handle_t con_handle){
80018707219SMatthias Ringwald 	return att_dispatch_server_can_send_now(con_handle);
8011b96b007SMatthias Ringwald }
8021b96b007SMatthias Ringwald 
803c141988cSMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){
804d9d23054SMatthias Ringwald     log_debug("att_server_request_can_send_now_event 0x%04x", con_handle);
8051b96b007SMatthias Ringwald     att_client_waiting_for_can_send = 1;
80618707219SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
8073deb3ec6SMatthias Ringwald }
8083deb3ec6SMatthias Ringwald 
809bb38f057SMatthias Ringwald void att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
810bb38f057SMatthias Ringwald     // check if can send already
811bb38f057SMatthias Ringwald     if (att_dispatch_server_can_send_now(con_handle)){
812bb38f057SMatthias Ringwald         callback_registration->callback(callback_registration->context);
813bb38f057SMatthias Ringwald         return;
814bb38f057SMatthias Ringwald     }
81518707219SMatthias Ringwald     callback_registration->context = (void*)(uintptr_t) con_handle;
816bb38f057SMatthias Ringwald     btstack_linked_list_add_tail(&can_send_now_clients, (btstack_linked_item_t*) callback_registration);
817bb38f057SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
818bb38f057SMatthias Ringwald }
819bb38f057SMatthias Ringwald 
82018707219SMatthias Ringwald 
821c141988cSMatthias Ringwald int att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){
82218707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
82318707219SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
82418707219SMatthias Ringwald 
82518707219SMatthias Ringwald     if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
8263deb3ec6SMatthias Ringwald 
8273deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
8283deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
82918707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_notification(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
83018707219SMatthias Ringwald 	return l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
8313deb3ec6SMatthias Ringwald }
8323deb3ec6SMatthias Ringwald 
833c141988cSMatthias Ringwald int att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){
83418707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
83518707219SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
83618707219SMatthias Ringwald 
837eaac31e8SMatthias Ringwald     if (att_server->value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS;
83818707219SMatthias Ringwald     if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
8393deb3ec6SMatthias Ringwald 
8403deb3ec6SMatthias Ringwald     // track indication
84118707219SMatthias Ringwald     att_server->value_indication_handle = attribute_handle;
84218707219SMatthias Ringwald     btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout);
84318707219SMatthias Ringwald     btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS);
84418707219SMatthias Ringwald     btstack_run_loop_add_timer(&att_server->value_indication_timer);
8453deb3ec6SMatthias Ringwald 
8463deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
8473deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
84818707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_indication(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
84918707219SMatthias Ringwald 	l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
8503deb3ec6SMatthias Ringwald     return 0;
8513deb3ec6SMatthias Ringwald }
852