xref: /btstack/src/ble/att_server.c (revision 6155bae02d89a7f11db3282fecec234175fa5a47)
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 
696afb9acaSMatthias Ringwald #ifndef NVN_NUM_GATT_SERVER_CCC
706afb9acaSMatthias 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);
144b12646c5SJakob Krantz     att_dispatch_server_mtu_exchanged(con_handle, mtu);
1453deb3ec6SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1463deb3ec6SMatthias Ringwald }
1473deb3ec6SMatthias Ringwald 
1481b96b007SMatthias Ringwald static void att_emit_can_send_now_event(void){
1493c97b242SMatthias Ringwald     if (!att_client_packet_handler) return;
1503c97b242SMatthias Ringwald 
1511b96b007SMatthias Ringwald     uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0};
1521b96b007SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1531b96b007SMatthias Ringwald }
1541b96b007SMatthias Ringwald 
155ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){
15654178543SMatthias Ringwald     void * context = btstack_run_loop_get_timer_context(ts);
15754178543SMatthias Ringwald     hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
15818707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
15918707219SMatthias Ringwald     if (!att_server) return;
16018707219SMatthias Ringwald     uint16_t att_handle = att_server->value_indication_handle;
16118707219SMatthias Ringwald     att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_server->connection.con_handle, att_handle);
1623deb3ec6SMatthias Ringwald }
1633deb3ec6SMatthias Ringwald 
164760ad805SMatthias Ringwald static void att_device_identified(att_server_t * att_server, uint8_t index){
165e62c502cSMatthias Ringwald     att_server->ir_lookup_active = 0;
166e62c502cSMatthias Ringwald     att_server->ir_le_device_db_index = index;
167760ad805SMatthias 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);
168e62c502cSMatthias Ringwald     att_run_for_context(att_server);
169e62c502cSMatthias Ringwald }
170e62c502cSMatthias Ringwald 
1713deb3ec6SMatthias Ringwald static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1729ec2630cSMatthias Ringwald 
1737dad9ff8SMatthias Ringwald     UNUSED(channel); // ok: there is no channel
1747dad9ff8SMatthias Ringwald     UNUSED(size);    // ok: handling own l2cap events
1753deb3ec6SMatthias Ringwald 
17618707219SMatthias Ringwald     att_server_t * att_server;
17718707219SMatthias Ringwald     hci_con_handle_t con_handle;
17818707219SMatthias Ringwald 
1793deb3ec6SMatthias Ringwald     switch (packet_type) {
1803deb3ec6SMatthias Ringwald 
1813deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1820e2df43fSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
1833deb3ec6SMatthias Ringwald 
1843deb3ec6SMatthias Ringwald                 case HCI_EVENT_LE_META:
1853deb3ec6SMatthias Ringwald                     switch (packet[2]) {
1863deb3ec6SMatthias Ringwald                         case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
18718707219SMatthias Ringwald                             con_handle = little_endian_read_16(packet, 4);
18818707219SMatthias Ringwald                             att_server = att_server_for_handle(con_handle);
18918707219SMatthias Ringwald                             if (!att_server) break;
1903deb3ec6SMatthias Ringwald                         	// store connection info
19118707219SMatthias Ringwald                         	att_server->peer_addr_type = packet[7];
19218707219SMatthias Ringwald                             reverse_bd_addr(&packet[8], att_server->peer_address);
19318707219SMatthias Ringwald                             att_server->connection.con_handle = con_handle;
1943deb3ec6SMatthias Ringwald                             // reset connection properties
19518707219SMatthias Ringwald                             att_server->state = ATT_SERVER_IDLE;
19618707219SMatthias Ringwald                             att_server->connection.mtu = ATT_DEFAULT_MTU;
19718707219SMatthias Ringwald                             att_server->connection.max_mtu = l2cap_max_le_mtu();
19818707219SMatthias Ringwald                             if (att_server->connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){
19918707219SMatthias Ringwald                                 att_server->connection.max_mtu = ATT_REQUEST_BUFFER_SIZE;
20099c58ad0SMatthias Ringwald                             }
20118707219SMatthias Ringwald                             att_server->connection.encryption_key_size = 0;
20218707219SMatthias Ringwald                             att_server->connection.authenticated = 0;
20318707219SMatthias Ringwald 		                	att_server->connection.authorized = 0;
204b2c1e52cSMatthias Ringwald                             // workaround: identity resolving can already be complete, at least store result
205b2c1e52cSMatthias Ringwald                             att_server->ir_le_device_db_index = sm_le_device_index(con_handle);
206760ad805SMatthias Ringwald                             att_server->pairing_active = 0;
2073deb3ec6SMatthias Ringwald                             break;
2083deb3ec6SMatthias Ringwald 
2093deb3ec6SMatthias Ringwald                         default:
2103deb3ec6SMatthias Ringwald                             break;
2113deb3ec6SMatthias Ringwald                     }
2123deb3ec6SMatthias Ringwald                     break;
2133deb3ec6SMatthias Ringwald 
2143deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_CHANGE:
2153deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE:
2163deb3ec6SMatthias Ringwald                 	// check handle
21718707219SMatthias Ringwald                     con_handle = little_endian_read_16(packet, 3);
21818707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
21918707219SMatthias Ringwald                     if (!att_server) break;
2209c6e867eSMatthias Ringwald                     att_server->connection.encryption_key_size = gap_encryption_key_size(con_handle);
2219c6e867eSMatthias Ringwald                     att_server->connection.authenticated = gap_authenticated(con_handle);
22201ac2008SMatthias Ringwald                     if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){
22301ac2008SMatthias Ringwald                         // restore CCC values when encrypted
22401ac2008SMatthias Ringwald                         if (hci_event_encryption_change_get_encryption_enabled(packet)){
22501ac2008SMatthias Ringwald                             att_server_persistent_ccc_restore(att_server);
22601ac2008SMatthias Ringwald                         }
22701ac2008SMatthias Ringwald                     }
2283deb3ec6SMatthias Ringwald                 	break;
2293deb3ec6SMatthias Ringwald 
2303deb3ec6SMatthias Ringwald                 case HCI_EVENT_DISCONNECTION_COMPLETE:
23170a390c7SMatthias Ringwald                     // check handle
23218707219SMatthias Ringwald                     con_handle = hci_event_disconnection_complete_get_connection_handle(packet);
23318707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
23418707219SMatthias Ringwald                     if (!att_server) break;
23518707219SMatthias Ringwald                     att_clear_transaction_queue(&att_server->connection);
23618707219SMatthias Ringwald                     att_server->connection.con_handle = 0;
23718707219SMatthias Ringwald                     att_server->value_indication_handle = 0; // reset error state
238760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
23918707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
2403deb3ec6SMatthias Ringwald                     break;
2413deb3ec6SMatthias Ringwald 
242760ad805SMatthias Ringwald                 // Identity Resolving
2435611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_STARTED:
24418707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_started_get_handle(packet);
24518707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
24618707219SMatthias Ringwald                     if (!att_server) break;
2475611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED");
24818707219SMatthias Ringwald                     att_server->ir_lookup_active = 1;
2493deb3ec6SMatthias Ringwald                     break;
2505611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
251760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
252760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
253760ad805SMatthias Ringwald                     if (!att_server) return;
254760ad805SMatthias Ringwald                     att_device_identified(att_server, sm_event_identity_resolving_succeeded_get_index(packet));
2553deb3ec6SMatthias Ringwald                     break;
2565611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_FAILED:
25718707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_failed_get_handle(packet);
25818707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
25918707219SMatthias Ringwald                     if (!att_server) break;
2605611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED");
26118707219SMatthias Ringwald                     att_server->ir_lookup_active = 0;
26218707219SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
26318707219SMatthias Ringwald                     att_run_for_context(att_server);
2643deb3ec6SMatthias Ringwald                     break;
265760ad805SMatthias Ringwald 
266760ad805SMatthias Ringwald                 // Pairing started - delete stored CCC values
267760ad805SMatthias Ringwald                 // - assumes pairing indicates either new device or re-pairing, in both cases there should be no stored CCC values
268760ad805SMatthias Ringwald                 // - assumes that all events have the con handle as the first field
269760ad805SMatthias Ringwald                 case SM_EVENT_JUST_WORKS_REQUEST:
270760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
271760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_INPUT_NUMBER:
272760ad805SMatthias Ringwald                 case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
273760ad805SMatthias Ringwald                     con_handle = sm_event_just_works_request_get_handle(packet);
274760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
275760ad805SMatthias Ringwald                     if (!att_server) break;
276760ad805SMatthias Ringwald                     att_server->pairing_active = 1;
277760ad805SMatthias Ringwald                     log_info("SM Pairing started");
278760ad805SMatthias Ringwald                     if (att_server->ir_le_device_db_index < 0) break;
279760ad805SMatthias Ringwald                     att_server_persistent_ccc_clear(att_server);
280760ad805SMatthias Ringwald                     // index not valid anymore
281760ad805SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
282760ad805SMatthias Ringwald                     break;
283760ad805SMatthias Ringwald 
284760ad805SMatthias Ringwald                 // Pairing completed
285760ad805SMatthias Ringwald                 case SM_EVENT_IDENTITY_CREATED:
286760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
287760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
288760ad805SMatthias Ringwald                     if (!att_server) return;
289760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
290760ad805SMatthias Ringwald                     att_device_identified(att_server, sm_event_identity_created_get_index(packet));
291760ad805SMatthias Ringwald                     break;
292760ad805SMatthias Ringwald 
293760ad805SMatthias Ringwald                 // Authorization
2945611a760SMatthias Ringwald                 case SM_EVENT_AUTHORIZATION_RESULT: {
29518707219SMatthias Ringwald                     con_handle = sm_event_authorization_result_get_handle(packet);
29618707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
29718707219SMatthias Ringwald                     if (!att_server) break;
29818707219SMatthias Ringwald                     att_server->connection.authorized = sm_event_authorization_result_get_authorization_result(packet);
29918707219SMatthias Ringwald                     att_dispatch_server_request_can_send_now_event(con_handle);
3003deb3ec6SMatthias Ringwald                 	break;
3013deb3ec6SMatthias Ringwald                 }
3023deb3ec6SMatthias Ringwald                 default:
3033deb3ec6SMatthias Ringwald                     break;
3043deb3ec6SMatthias Ringwald             }
30565b44ffdSMatthias Ringwald             break;
30665b44ffdSMatthias Ringwald         default:
30765b44ffdSMatthias Ringwald             break;
3083deb3ec6SMatthias Ringwald     }
3093deb3ec6SMatthias Ringwald }
3103deb3ec6SMatthias Ringwald 
3117a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
3123deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
3133deb3ec6SMatthias Ringwald 
31418707219SMatthias Ringwald     att_server_t * att_server = att_server_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION);
31518707219SMatthias Ringwald     if (!att_server) return;
3163deb3ec6SMatthias Ringwald 
3173deb3ec6SMatthias Ringwald     uint8_t hash_flipped[8];
3189c80e4ccSMatthias Ringwald     reverse_64(hash, hash_flipped);
31918707219SMatthias Ringwald     if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8)){
3203deb3ec6SMatthias Ringwald         log_info("ATT Signed Write, invalid signature");
32118707219SMatthias Ringwald         att_server->state = ATT_SERVER_IDLE;
3223deb3ec6SMatthias Ringwald         return;
3233deb3ec6SMatthias Ringwald     }
3243deb3ec6SMatthias Ringwald     log_info("ATT Signed Write, valid signature");
3253deb3ec6SMatthias Ringwald 
3263deb3ec6SMatthias Ringwald     // update sequence number
32718707219SMatthias Ringwald     uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
32818707219SMatthias Ringwald     le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1);
32918707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
33018707219SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
3313deb3ec6SMatthias Ringwald }
3327a766ebfSMatthias Ringwald #endif
33388a48dd8SMatthias Ringwald 
33418707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED
335b06f2579SMatthias Ringwald // pre: can send now
336b06f2579SMatthias Ringwald // returns: 1 if packet was sent
33718707219SMatthias Ringwald static int att_server_process_validated_request(att_server_t * att_server){
338b06f2579SMatthias Ringwald 
339b06f2579SMatthias Ringwald     l2cap_reserve_packet_buffer();
340b06f2579SMatthias Ringwald     uint8_t * att_response_buffer = l2cap_get_outgoing_buffer();
34118707219SMatthias Ringwald     uint16_t  att_response_size   = att_handle_request(&att_server->connection, att_server->request_buffer, att_server->request_size, att_response_buffer);
342b06f2579SMatthias Ringwald 
343b06f2579SMatthias Ringwald     // intercept "insufficient authorization" for authenticated connections to allow for user authorization
344b06f2579SMatthias Ringwald     if ((att_response_size     >= 4)
345b06f2579SMatthias Ringwald     && (att_response_buffer[0] == ATT_ERROR_RESPONSE)
346b06f2579SMatthias Ringwald     && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION)
34718707219SMatthias Ringwald     && (att_server->connection.authenticated)){
348b06f2579SMatthias Ringwald 
3499c6e867eSMatthias Ringwald         switch (gap_authorization_state(att_server->connection.con_handle)){
350b06f2579SMatthias Ringwald             case AUTHORIZATION_UNKNOWN:
351b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
35218707219SMatthias Ringwald                 sm_request_pairing(att_server->connection.con_handle);
353b06f2579SMatthias Ringwald                 return 0;
354b06f2579SMatthias Ringwald             case AUTHORIZATION_PENDING:
355b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
356b06f2579SMatthias Ringwald                 return 0;
357b06f2579SMatthias Ringwald             default:
358b06f2579SMatthias Ringwald                 break;
359b06f2579SMatthias Ringwald         }
360b06f2579SMatthias Ringwald     }
361b06f2579SMatthias Ringwald 
36218707219SMatthias Ringwald     att_server->state = ATT_SERVER_IDLE;
363b06f2579SMatthias Ringwald     if (att_response_size == 0) {
364b06f2579SMatthias Ringwald         l2cap_release_packet_buffer();
365b06f2579SMatthias Ringwald         return 0;
366b06f2579SMatthias Ringwald     }
367b06f2579SMatthias Ringwald 
36818707219SMatthias Ringwald     l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size);
369b06f2579SMatthias Ringwald 
370b06f2579SMatthias Ringwald     // notify client about MTU exchange result
371b06f2579SMatthias Ringwald     if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){
37218707219SMatthias Ringwald         att_emit_mtu_event(att_server->connection.con_handle, att_server->connection.mtu);
373b06f2579SMatthias Ringwald     }
374b06f2579SMatthias Ringwald     return 1;
375b06f2579SMatthias Ringwald }
376b06f2579SMatthias Ringwald 
37718707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server){
37818707219SMatthias Ringwald     switch (att_server->state){
3793deb3ec6SMatthias Ringwald         case ATT_SERVER_REQUEST_RECEIVED:
380760ad805SMatthias Ringwald 
381760ad805SMatthias Ringwald             // wait until pairing is complete
382760ad805SMatthias Ringwald             if (att_server->pairing_active) break;
383760ad805SMatthias Ringwald 
3847a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
38518707219SMatthias Ringwald             if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){
3863deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write!");
3873deb3ec6SMatthias Ringwald                 if (!sm_cmac_ready()) {
3883deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, sm_cmac engine not ready. Abort");
38918707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
3903deb3ec6SMatthias Ringwald                     return;
3913deb3ec6SMatthias Ringwald                 }
39218707219SMatthias Ringwald                 if (att_server->request_size < (3 + 12)) {
3933deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, request to short. Abort.");
39418707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
3953deb3ec6SMatthias Ringwald                     return;
3963deb3ec6SMatthias Ringwald                 }
39718707219SMatthias Ringwald                 if (att_server->ir_lookup_active){
3983deb3ec6SMatthias Ringwald                     return;
3993deb3ec6SMatthias Ringwald                 }
40018707219SMatthias Ringwald                 if (att_server->ir_le_device_db_index < 0){
4013deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, CSRK not available");
40218707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
4033deb3ec6SMatthias Ringwald                     return;
4043deb3ec6SMatthias Ringwald                 }
4053deb3ec6SMatthias Ringwald 
4063deb3ec6SMatthias Ringwald                 // check counter
40718707219SMatthias Ringwald                 uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
40818707219SMatthias Ringwald                 uint32_t counter_db     = le_device_db_remote_counter_get(att_server->ir_le_device_db_index);
4093deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet);
4103deb3ec6SMatthias Ringwald                 if (counter_packet < counter_db){
4113deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, db reports higher counter, abort");
41218707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
4133deb3ec6SMatthias Ringwald                     return;
4143deb3ec6SMatthias Ringwald                 }
4153deb3ec6SMatthias Ringwald 
4163deb3ec6SMatthias Ringwald                 // signature is { sequence counter, secure hash }
4173deb3ec6SMatthias Ringwald                 sm_key_t csrk;
41818707219SMatthias Ringwald                 le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk);
41918707219SMatthias Ringwald                 att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION;
4203deb3ec6SMatthias Ringwald                 log_info("Orig Signature: ");
42118707219SMatthias Ringwald                 log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8);
42218707219SMatthias Ringwald                 uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1);
42318707219SMatthias 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);
4243deb3ec6SMatthias Ringwald                 return;
4253deb3ec6SMatthias Ringwald             }
4267a766ebfSMatthias Ringwald #endif
427b06f2579SMatthias Ringwald             // move on
42818707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
42918707219SMatthias Ringwald             att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
430b06f2579SMatthias Ringwald             break;
43188a48dd8SMatthias Ringwald 
4323deb3ec6SMatthias Ringwald         default:
4333deb3ec6SMatthias Ringwald             break;
4343deb3ec6SMatthias Ringwald     }
4353deb3ec6SMatthias Ringwald }
4363deb3ec6SMatthias Ringwald 
4377eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){
438b06f2579SMatthias Ringwald 
439b06f2579SMatthias Ringwald     // NOTE: we get l2cap fixed channel instead of con_handle
440b06f2579SMatthias Ringwald 
44118707219SMatthias Ringwald     btstack_linked_list_iterator_t it;
44218707219SMatthias Ringwald     hci_connections_get_iterator(&it);
44318707219SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
44418707219SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
44518707219SMatthias Ringwald         att_server_t * att_server = &connection->att_server;
44618707219SMatthias Ringwald         if (att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED){
44718707219SMatthias Ringwald             int sent = att_server_process_validated_request(att_server);
448bb38f057SMatthias Ringwald             if (sent && (att_client_waiting_for_can_send || !btstack_linked_list_empty(&can_send_now_clients))){
44918707219SMatthias Ringwald                 att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
4503deb3ec6SMatthias Ringwald                 return;
4513deb3ec6SMatthias Ringwald             }
4523deb3ec6SMatthias Ringwald         }
45318707219SMatthias Ringwald     }
4543deb3ec6SMatthias Ringwald 
455bb38f057SMatthias Ringwald     while (!btstack_linked_list_empty(&can_send_now_clients)){
456bb38f057SMatthias Ringwald         // handle first client
457bb38f057SMatthias Ringwald         btstack_context_callback_registration_t * client = (btstack_context_callback_registration_t*) can_send_now_clients;
45818707219SMatthias Ringwald         hci_con_handle_t con_handle = (uintptr_t) client->context;
459bb38f057SMatthias Ringwald         btstack_linked_list_remove(&can_send_now_clients, (btstack_linked_item_t *) client);
460bb38f057SMatthias Ringwald         client->callback(client->context);
461bb38f057SMatthias Ringwald 
462bb38f057SMatthias Ringwald         // request again if needed
46318707219SMatthias Ringwald         if (!att_dispatch_server_can_send_now(con_handle)){
464bb38f057SMatthias Ringwald             if (!btstack_linked_list_empty(&can_send_now_clients) || att_client_waiting_for_can_send){
46518707219SMatthias Ringwald                 att_dispatch_server_request_can_send_now_event(con_handle);
466bb38f057SMatthias Ringwald             }
467bb38f057SMatthias Ringwald             return;
468bb38f057SMatthias Ringwald         }
469bb38f057SMatthias Ringwald     }
470bb38f057SMatthias Ringwald 
471b06f2579SMatthias Ringwald     if (att_client_waiting_for_can_send){
472b06f2579SMatthias Ringwald         att_client_waiting_for_can_send = 0;
473b06f2579SMatthias Ringwald         att_emit_can_send_now_event();
4743deb3ec6SMatthias Ringwald     }
4753deb3ec6SMatthias Ringwald }
4763deb3ec6SMatthias Ringwald 
47788a48dd8SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
47818707219SMatthias Ringwald 
47918707219SMatthias Ringwald     att_server_t * att_server;
48018707219SMatthias Ringwald 
48188a48dd8SMatthias Ringwald     switch (packet_type){
48288a48dd8SMatthias Ringwald 
48388a48dd8SMatthias Ringwald         case HCI_EVENT_PACKET:
484bd87a16eSMatthias Ringwald             switch (packet[0]){
485bd87a16eSMatthias Ringwald                 case L2CAP_EVENT_CAN_SEND_NOW:
4864d2be802SMatthias Ringwald                     att_server_handle_can_send_now();
487372d62c4SMatthias Ringwald                     break;
488bd87a16eSMatthias Ringwald                 case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
489bd87a16eSMatthias Ringwald                     // GATT client has negotiated the mtu for this connection
490*6155bae0SMatthias Ringwald                     att_server = att_server_for_handle(handle);
491*6155bae0SMatthias Ringwald                     if (!att_server) break;
492bd87a16eSMatthias Ringwald                     att_server->connection.mtu = little_endian_read_16(packet, 4);
493bd87a16eSMatthias Ringwald                     break;
494bd87a16eSMatthias Ringwald                 default:
495bd87a16eSMatthias Ringwald                     break;
496bd87a16eSMatthias Ringwald             }
497bd87a16eSMatthias Ringwald             break;
4983deb3ec6SMatthias Ringwald 
499372d62c4SMatthias Ringwald         case ATT_DATA_PACKET:
500f50ef7ddSMatthias Ringwald             log_debug("ATT Packet, handle 0x%04x", handle);
50118707219SMatthias Ringwald             att_server = att_server_for_handle(handle);
50218707219SMatthias Ringwald             if (!att_server) break;
50318707219SMatthias Ringwald 
5043deb3ec6SMatthias Ringwald             // handle value indication confirms
50518707219SMatthias Ringwald             if (packet[0] == ATT_HANDLE_VALUE_CONFIRMATION && att_server->value_indication_handle){
50618707219SMatthias Ringwald                 btstack_run_loop_remove_timer(&att_server->value_indication_timer);
50718707219SMatthias Ringwald                 uint16_t att_handle = att_server->value_indication_handle;
50818707219SMatthias Ringwald                 att_server->value_indication_handle = 0;
50918707219SMatthias Ringwald                 att_handle_value_indication_notify_client(0, att_server->connection.con_handle, att_handle);
5103deb3ec6SMatthias Ringwald                 return;
5113deb3ec6SMatthias Ringwald             }
5123deb3ec6SMatthias Ringwald 
5136428eb5aSMatthias Ringwald             // directly process command
5146428eb5aSMatthias Ringwald             // note: signed write cannot be handled directly as authentication needs to be verified
5156428eb5aSMatthias Ringwald             if (packet[0] == ATT_WRITE_COMMAND){
51618707219SMatthias Ringwald                 att_handle_request(&att_server->connection, packet, size, 0);
5176428eb5aSMatthias Ringwald                 return;
5186428eb5aSMatthias Ringwald             }
5196428eb5aSMatthias Ringwald 
5203deb3ec6SMatthias Ringwald             // check size
52118707219SMatthias Ringwald             if (size > sizeof(att_server->request_buffer)) {
52218707219SMatthias 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));
5236428eb5aSMatthias Ringwald                 return;
5246428eb5aSMatthias Ringwald             }
5253deb3ec6SMatthias Ringwald 
5263deb3ec6SMatthias Ringwald             // last request still in processing?
52718707219SMatthias Ringwald             if (att_server->state != ATT_SERVER_IDLE){
52818707219SMatthias Ringwald                 log_info("att_packet_handler: skipping att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state);
5296428eb5aSMatthias Ringwald                 return;
5306428eb5aSMatthias Ringwald             }
5313deb3ec6SMatthias Ringwald 
5323deb3ec6SMatthias Ringwald             // store request
53318707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED;
53418707219SMatthias Ringwald             att_server->request_size = size;
53518707219SMatthias Ringwald             memcpy(att_server->request_buffer, packet, size);
5363deb3ec6SMatthias Ringwald 
53718707219SMatthias Ringwald             att_run_for_context(att_server);
538372d62c4SMatthias Ringwald             break;
539372d62c4SMatthias Ringwald     }
5403deb3ec6SMatthias Ringwald }
5413deb3ec6SMatthias Ringwald 
542bf78aac6SMatthias Ringwald // ---------------------
543bf78aac6SMatthias Ringwald // persistent CCC writes
544bf78aac6SMatthias Ringwald static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){
545bf78aac6SMatthias Ringwald     return 'B' << 24 | 'T' << 16 | 'C' << 8 | index;
546bf78aac6SMatthias Ringwald }
547bf78aac6SMatthias Ringwald 
548bf78aac6SMatthias Ringwald static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){
549bf78aac6SMatthias Ringwald     // lookup att_server instance
550bf78aac6SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
551bf78aac6SMatthias Ringwald     if (!att_server) return;
552bf78aac6SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
553bf78aac6SMatthias 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);
5546a540790SMatthias Ringwald 
555bf78aac6SMatthias Ringwald     // check if bonded
556bf78aac6SMatthias Ringwald     if (le_device_index < 0) return;
5576a540790SMatthias Ringwald 
558bf78aac6SMatthias Ringwald     // get btstack_tlv
559bf78aac6SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
560bf78aac6SMatthias Ringwald     void * tlv_context;
561bf78aac6SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
562bf78aac6SMatthias Ringwald     if (!tlv_impl) return;
5636a540790SMatthias Ringwald 
564bf78aac6SMatthias Ringwald     // update ccc tag
565bf78aac6SMatthias Ringwald     int index;
5666a540790SMatthias Ringwald     uint32_t highest_seq_nr = 0;
5676a540790SMatthias Ringwald     uint32_t lowest_seq_nr = 0;
5686a540790SMatthias Ringwald     uint32_t tag_for_lowest_seq_nr = 0;
5696a540790SMatthias Ringwald     uint32_t tag_for_empty = 0;
5706a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
5716afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
572bf78aac6SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
5736a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
5746a540790SMatthias Ringwald 
5756a540790SMatthias Ringwald         // empty/invalid tag
5766a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)){
5776a540790SMatthias Ringwald             tag_for_empty = tag;
578bf78aac6SMatthias Ringwald             continue;
579bf78aac6SMatthias Ringwald         }
5806a540790SMatthias Ringwald         // update highest seq nr
5816a540790SMatthias Ringwald         if (entry.seq_nr > highest_seq_nr){
5826a540790SMatthias Ringwald             highest_seq_nr = entry.seq_nr;
5836a540790SMatthias Ringwald         }
5846a540790SMatthias Ringwald         // find entry with lowest seq nr
5856a540790SMatthias Ringwald         if ((tag_for_lowest_seq_nr == 0) || (entry.seq_nr < lowest_seq_nr)){
5866a540790SMatthias Ringwald             tag_for_lowest_seq_nr = tag;
5876a540790SMatthias Ringwald             lowest_seq_nr = entry.seq_nr;
5886a540790SMatthias Ringwald         }
5896a540790SMatthias Ringwald 
5906a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
5916a540790SMatthias Ringwald         if (entry.att_handle   != att_handle)      continue;
5926a540790SMatthias Ringwald 
5936a540790SMatthias Ringwald         // found matching entry
59401ac2008SMatthias Ringwald         if (value){
595bf78aac6SMatthias Ringwald             // update
5966a540790SMatthias Ringwald             if (entry.value == value) {
597760ad805SMatthias Ringwald                 log_info("CCC Index %u: Up-to-date", index);
598760ad805SMatthias Ringwald                 return;
599760ad805SMatthias Ringwald             }
6006a540790SMatthias Ringwald             entry.value = value;
6016a540790SMatthias Ringwald             entry.seq_nr = highest_seq_nr + 1;
602760ad805SMatthias Ringwald             log_info("CCC Index %u: Store", index);
6036a540790SMatthias Ringwald             tlv_impl->store_tag(tlv_context, tag, (const uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
60401ac2008SMatthias Ringwald         } else {
60501ac2008SMatthias Ringwald             // delete
606760ad805SMatthias Ringwald             log_info("CCC Index %u: Delete", index);
60701ac2008SMatthias Ringwald             tlv_impl->delete_tag(tlv_context, tag);
60801ac2008SMatthias Ringwald         }
609bf78aac6SMatthias Ringwald         return;
610bf78aac6SMatthias Ringwald     }
6116a540790SMatthias Ringwald 
612faf2b6c3SMatthias Ringwald     log_info("tag_for_empy %"PRIx32", tag_for_lowest_seq_nr %"PRIx32, tag_for_empty, tag_for_lowest_seq_nr);
6136a540790SMatthias Ringwald 
6146a540790SMatthias Ringwald     if (value == 0){
6156a540790SMatthias Ringwald         // done
6166a540790SMatthias Ringwald         return;
6176a540790SMatthias Ringwald     }
6186a540790SMatthias Ringwald 
6196a540790SMatthias Ringwald     uint32_t tag_to_use = 0;
6206a540790SMatthias Ringwald     if (tag_for_empty){
6216a540790SMatthias Ringwald         tag_to_use = tag_for_empty;
6226a540790SMatthias Ringwald     } else if (tag_for_lowest_seq_nr){
6236a540790SMatthias Ringwald         tag_to_use = tag_for_lowest_seq_nr;
6246a540790SMatthias Ringwald     } else {
6256a540790SMatthias Ringwald         // should not happen
6266a540790SMatthias Ringwald         return;
6276a540790SMatthias Ringwald     }
628bf78aac6SMatthias Ringwald     // store ccc tag
6296a540790SMatthias Ringwald     entry.seq_nr       = highest_seq_nr + 1;
6306a540790SMatthias Ringwald     entry.device_index = le_device_index;
6316a540790SMatthias Ringwald     entry.att_handle   = att_handle;
6326a540790SMatthias Ringwald     entry.value        = value;
6336a540790SMatthias Ringwald     tlv_impl->store_tag(tlv_context, tag_to_use, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
634bf78aac6SMatthias Ringwald }
635bf78aac6SMatthias Ringwald 
636760ad805SMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server){
637760ad805SMatthias Ringwald     if (!att_server) return;
638760ad805SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
639760ad805SMatthias Ringwald     log_info("Clear CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
640760ad805SMatthias Ringwald     // check if bonded
641760ad805SMatthias Ringwald     if (le_device_index < 0) return;
642760ad805SMatthias Ringwald     // get btstack_tlv
643760ad805SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
644760ad805SMatthias Ringwald     void * tlv_context;
645760ad805SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
646760ad805SMatthias Ringwald     if (!tlv_impl) return;
647760ad805SMatthias Ringwald     // get all ccc tag
648760ad805SMatthias Ringwald     int index;
6496a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
6506afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
651760ad805SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
6526a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
6536a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
6546a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
655760ad805SMatthias Ringwald         // delete entry
656760ad805SMatthias Ringwald         log_info("CCC Index %u: Delete", index);
657760ad805SMatthias Ringwald         tlv_impl->delete_tag(tlv_context, tag);
658760ad805SMatthias Ringwald     }
659760ad805SMatthias Ringwald }
660760ad805SMatthias Ringwald 
66101ac2008SMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server){
66201ac2008SMatthias Ringwald     if (!att_server) return;
66301ac2008SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
66401ac2008SMatthias Ringwald     log_info("Restore CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
66501ac2008SMatthias Ringwald     // get btstack_tlv
66601ac2008SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
66701ac2008SMatthias Ringwald     void * tlv_context;
66801ac2008SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
66901ac2008SMatthias Ringwald     if (!tlv_impl) return;
67001ac2008SMatthias Ringwald     // get all ccc tag
67101ac2008SMatthias Ringwald     int index;
6726a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
6736afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
67401ac2008SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
6756a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
6766a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
6776a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
67801ac2008SMatthias Ringwald         // simulate write callback
6796a540790SMatthias Ringwald         uint16_t attribute_handle = entry.att_handle;
68001ac2008SMatthias Ringwald         uint8_t  value[2];
6816a540790SMatthias Ringwald         little_endian_store_16(value, 0, entry.value);
68201ac2008SMatthias Ringwald         att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
68301ac2008SMatthias Ringwald         if (!callback) continue;
6846a540790SMatthias Ringwald         log_info("CCC Index %u: Set Attribute handle 0x%04x to value 0x%04x", index, attribute_handle, entry.value );
68501ac2008SMatthias Ringwald         (*callback)(att_server->connection.con_handle, attribute_handle, ATT_TRANSACTION_MODE_NONE, 0, value, sizeof(value));
68601ac2008SMatthias Ringwald     }
68701ac2008SMatthias Ringwald }
68801ac2008SMatthias Ringwald 
689bf78aac6SMatthias Ringwald // persistent CCC writes
690bf78aac6SMatthias Ringwald // ---------------------
6913d71c7a4SMatthias Ringwald 
6923d71c7a4SMatthias Ringwald // gatt service management
6933d71c7a4SMatthias Ringwald static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){
6943d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
6953d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
6963d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
6973d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
6983d71c7a4SMatthias Ringwald         if (handler->start_handle > handle) continue;
6993d71c7a4SMatthias Ringwald         if (handler->end_handle   < handle) continue;
7003d71c7a4SMatthias Ringwald         return handler;
7013d71c7a4SMatthias Ringwald     }
7023d71c7a4SMatthias Ringwald     return NULL;
7033d71c7a4SMatthias Ringwald }
7043d71c7a4SMatthias Ringwald static att_read_callback_t att_server_read_callback_for_handle(uint16_t handle){
7053d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
7063d71c7a4SMatthias Ringwald     if (handler) return handler->read_callback;
7073d71c7a4SMatthias Ringwald     return att_server_client_read_callback;
7083d71c7a4SMatthias Ringwald }
7093d71c7a4SMatthias Ringwald 
7103d71c7a4SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){
7113d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
7123d71c7a4SMatthias Ringwald     if (handler) return handler->write_callback;
7133d71c7a4SMatthias Ringwald     return att_server_client_write_callback;
7143d71c7a4SMatthias Ringwald }
7153d71c7a4SMatthias Ringwald 
7163d71c7a4SMatthias Ringwald static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){
7173d71c7a4SMatthias Ringwald     // notify all callbacks
7183d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
7193d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
7203d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
7213d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
7223d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
7233d71c7a4SMatthias Ringwald         (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
7243d71c7a4SMatthias Ringwald     }
7253d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return;
7263d71c7a4SMatthias Ringwald     (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
7273d71c7a4SMatthias Ringwald }
7283d71c7a4SMatthias Ringwald 
7293d71c7a4SMatthias Ringwald // returns first reported error or 0
7303d71c7a4SMatthias Ringwald static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){
7313d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
7323d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
7333d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
7343d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
7353d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
7363d71c7a4SMatthias Ringwald         uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
7373d71c7a4SMatthias Ringwald         if (error_code) return error_code;
7383d71c7a4SMatthias Ringwald     }
7393d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return 0;
7403d71c7a4SMatthias Ringwald     return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
7413d71c7a4SMatthias Ringwald }
7423d71c7a4SMatthias Ringwald 
7433d71c7a4SMatthias 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){
7443d71c7a4SMatthias Ringwald     att_read_callback_t callback = att_server_read_callback_for_handle(attribute_handle);
74501ac2008SMatthias Ringwald     if (!callback) return 0;
7463d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, offset, buffer, buffer_size);
7473d71c7a4SMatthias Ringwald }
7483d71c7a4SMatthias Ringwald 
7493d71c7a4SMatthias 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){
7503d71c7a4SMatthias Ringwald     switch (transaction_mode){
7513d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_VALIDATE:
7523d71c7a4SMatthias Ringwald             return att_validate_prepared_write(con_handle);
7533d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_EXECUTE:
7543d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_CANCEL:
7553d71c7a4SMatthias Ringwald             att_notify_write_callbacks(con_handle, transaction_mode);
7563d71c7a4SMatthias Ringwald             return 0;
7573d71c7a4SMatthias Ringwald         default:
7583d71c7a4SMatthias Ringwald             break;
7593d71c7a4SMatthias Ringwald     }
7603d71c7a4SMatthias Ringwald 
761bf78aac6SMatthias Ringwald     // track CCC writes
762bf78aac6SMatthias Ringwald     if (att_is_persistent_ccc(attribute_handle) && offset == 0 && buffer_size == 2){
763bf78aac6SMatthias Ringwald         att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0));
764bf78aac6SMatthias Ringwald     }
765bf78aac6SMatthias Ringwald 
76601ac2008SMatthias Ringwald     att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
76701ac2008SMatthias Ringwald     if (!callback) return 0;
7683d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size);
7693d71c7a4SMatthias Ringwald }
7703d71c7a4SMatthias Ringwald 
7713d71c7a4SMatthias Ringwald /**
7723d71c7a4SMatthias Ringwald  * @brief register read/write callbacks for specific handle range
7733d71c7a4SMatthias Ringwald  * @param att_service_handler_t
7743d71c7a4SMatthias Ringwald  */
7753d71c7a4SMatthias Ringwald void att_server_register_service_handler(att_service_handler_t * handler){
7763d71c7a4SMatthias Ringwald     if (att_service_handler_for_handle(handler->start_handle) ||
7773d71c7a4SMatthias Ringwald         att_service_handler_for_handle(handler->end_handle)){
7783d71c7a4SMatthias Ringwald         log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle);
7793d71c7a4SMatthias Ringwald         return;
7803d71c7a4SMatthias Ringwald     }
7813d71c7a4SMatthias Ringwald     btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler);
7823d71c7a4SMatthias Ringwald }
7833d71c7a4SMatthias Ringwald 
7843deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){
7853deb3ec6SMatthias Ringwald 
7863d71c7a4SMatthias Ringwald     // store callbacks
7873d71c7a4SMatthias Ringwald     att_server_client_read_callback  = read_callback;
7883d71c7a4SMatthias Ringwald     att_server_client_write_callback = write_callback;
7893d71c7a4SMatthias Ringwald 
7901a389cdcSMatthias Ringwald     // register for HCI Events
791d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &att_event_packet_handler;
7921a389cdcSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
7931a389cdcSMatthias Ringwald 
794406722e4SMatthias Ringwald     // register for SM events
795d9a7306aSMatthias Ringwald     sm_event_callback_registration.callback = &att_event_packet_handler;
796406722e4SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
7973deb3ec6SMatthias Ringwald 
7981a389cdcSMatthias Ringwald     // and L2CAP ATT Server PDUs
7993deb3ec6SMatthias Ringwald     att_dispatch_register_server(att_packet_handler);
8003deb3ec6SMatthias Ringwald 
8013deb3ec6SMatthias Ringwald     att_set_db(db);
8023d71c7a4SMatthias Ringwald     att_set_read_callback(att_server_read_callback);
8033d71c7a4SMatthias Ringwald     att_set_write_callback(att_server_write_callback);
8043deb3ec6SMatthias Ringwald 
8053deb3ec6SMatthias Ringwald }
8063deb3ec6SMatthias Ringwald 
8073deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){
8083deb3ec6SMatthias Ringwald     att_client_packet_handler = handler;
8093deb3ec6SMatthias Ringwald }
8103deb3ec6SMatthias Ringwald 
811c141988cSMatthias Ringwald int  att_server_can_send_packet_now(hci_con_handle_t con_handle){
81218707219SMatthias Ringwald 	return att_dispatch_server_can_send_now(con_handle);
8131b96b007SMatthias Ringwald }
8141b96b007SMatthias Ringwald 
815c141988cSMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){
816d9d23054SMatthias Ringwald     log_debug("att_server_request_can_send_now_event 0x%04x", con_handle);
8171b96b007SMatthias Ringwald     att_client_waiting_for_can_send = 1;
81818707219SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
8193deb3ec6SMatthias Ringwald }
8203deb3ec6SMatthias Ringwald 
821bb38f057SMatthias Ringwald void att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
822bb38f057SMatthias Ringwald     // check if can send already
823bb38f057SMatthias Ringwald     if (att_dispatch_server_can_send_now(con_handle)){
824bb38f057SMatthias Ringwald         callback_registration->callback(callback_registration->context);
825bb38f057SMatthias Ringwald         return;
826bb38f057SMatthias Ringwald     }
82718707219SMatthias Ringwald     callback_registration->context = (void*)(uintptr_t) con_handle;
828bb38f057SMatthias Ringwald     btstack_linked_list_add_tail(&can_send_now_clients, (btstack_linked_item_t*) callback_registration);
829bb38f057SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
830bb38f057SMatthias Ringwald }
831bb38f057SMatthias Ringwald 
83218707219SMatthias Ringwald 
833c141988cSMatthias Ringwald int att_server_notify(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 
83718707219SMatthias Ringwald     if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
8383deb3ec6SMatthias Ringwald 
8393deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
8403deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
84118707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_notification(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
84218707219SMatthias Ringwald 	return l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
8433deb3ec6SMatthias Ringwald }
8443deb3ec6SMatthias Ringwald 
845c141988cSMatthias Ringwald int att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){
84618707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
84718707219SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
84818707219SMatthias Ringwald 
849eaac31e8SMatthias Ringwald     if (att_server->value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS;
85018707219SMatthias Ringwald     if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
8513deb3ec6SMatthias Ringwald 
8523deb3ec6SMatthias Ringwald     // track indication
85318707219SMatthias Ringwald     att_server->value_indication_handle = attribute_handle;
85418707219SMatthias Ringwald     btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout);
85518707219SMatthias Ringwald     btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS);
85618707219SMatthias Ringwald     btstack_run_loop_add_timer(&att_server->value_indication_timer);
8573deb3ec6SMatthias Ringwald 
8583deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
8593deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
86018707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_indication(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
86118707219SMatthias Ringwald 	l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
8623deb3ec6SMatthias Ringwald     return 0;
8633deb3ec6SMatthias Ringwald }
864