xref: /btstack/src/ble/att_server.c (revision 5d07396b2432226952ea1e16305d745278a64416)
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"
68d1a1f6a4SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
69d1a1f6a4SMatthias Ringwald #include "ble/sm.h"
70d1a1f6a4SMatthias Ringwald #endif
71bf78aac6SMatthias Ringwald 
726afb9acaSMatthias Ringwald #ifndef NVN_NUM_GATT_SERVER_CCC
736afb9acaSMatthias Ringwald #define NVN_NUM_GATT_SERVER_CCC 20
74bf78aac6SMatthias Ringwald #endif
753deb3ec6SMatthias Ringwald 
7618707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server);
7701ac2008SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle);
78*5d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle);
7901ac2008SMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server);
80760ad805SMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server);
813deb3ec6SMatthias Ringwald 
823551936eSMatthias Ringwald typedef enum {
833551936eSMatthias Ringwald     ATT_SERVER_RUN_PHASE_1_REQUESTS,
843551936eSMatthias Ringwald     ATT_SERVER_RUN_PHASE_2_INDICATIONS,
853551936eSMatthias Ringwald     ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS,
863551936eSMatthias Ringwald } att_server_run_phase_t;
873551936eSMatthias Ringwald 
886a540790SMatthias Ringwald //
896a540790SMatthias Ringwald typedef struct {
906a540790SMatthias Ringwald     uint32_t seq_nr;
916a540790SMatthias Ringwald     uint16_t att_handle;
926a540790SMatthias Ringwald     uint8_t  value;
936a540790SMatthias Ringwald     uint8_t  device_index;
946a540790SMatthias Ringwald } persistent_ccc_entry_t;
956a540790SMatthias Ringwald 
9618707219SMatthias Ringwald // global
971a389cdcSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
98406722e4SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration;
993deb3ec6SMatthias Ringwald static btstack_packet_handler_t               att_client_packet_handler = NULL;
1003d71c7a4SMatthias Ringwald static btstack_linked_list_t                  service_handlers;
1015f141511SMatthias Ringwald static btstack_context_callback_registration_t att_client_waiting_for_can_send_registration;
10218707219SMatthias Ringwald 
1033d71c7a4SMatthias Ringwald static att_read_callback_t                    att_server_client_read_callback;
1043d71c7a4SMatthias Ringwald static att_write_callback_t                   att_server_client_write_callback;
1053d71c7a4SMatthias Ringwald 
1066438547aSMatthias Ringwald // round robin
1076438547aSMatthias Ringwald static hci_con_handle_t att_server_last_can_send_now = HCI_CON_HANDLE_INVALID;
108bf78aac6SMatthias Ringwald 
10918707219SMatthias Ringwald static att_server_t * att_server_for_handle(hci_con_handle_t con_handle){
11018707219SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
11118707219SMatthias Ringwald     if (!hci_connection) return NULL;
11218707219SMatthias Ringwald     return &hci_connection->att_server;
11318707219SMatthias Ringwald }
11418707219SMatthias Ringwald 
11502b78fc8SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
11618707219SMatthias Ringwald static att_server_t * att_server_for_state(att_server_state_t state){
11718707219SMatthias Ringwald     btstack_linked_list_iterator_t it;
11818707219SMatthias Ringwald     hci_connections_get_iterator(&it);
11918707219SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
12018707219SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
12118707219SMatthias Ringwald         att_server_t * att_server = &connection->att_server;
12218707219SMatthias Ringwald         if (att_server->state == state) return att_server;
12318707219SMatthias Ringwald     }
12418707219SMatthias Ringwald     return NULL;
12518707219SMatthias Ringwald }
12602b78fc8SMatthias Ringwald #endif
127bb38f057SMatthias Ringwald 
1283deb3ec6SMatthias Ringwald static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){
129*5d07396bSMatthias Ringwald     btstack_packet_handler_t packet_handler = att_server_packet_handler_for_handle(attribute_handle);
130*5d07396bSMatthias Ringwald     if (!packet_handler) return;
1313deb3ec6SMatthias Ringwald 
1323deb3ec6SMatthias Ringwald     uint8_t event[7];
1333deb3ec6SMatthias Ringwald     int pos = 0;
1345611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE;
1353deb3ec6SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
1363deb3ec6SMatthias Ringwald     event[pos++] = status;
137f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, client_handle);
1383deb3ec6SMatthias Ringwald     pos += 2;
139f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, attribute_handle);
1403deb3ec6SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1413deb3ec6SMatthias Ringwald }
1423deb3ec6SMatthias Ringwald 
143*5d07396bSMatthias Ringwald static void att_emit_event_to_all(const uint8_t * event, uint16_t size){
144*5d07396bSMatthias Ringwald     // dispatch to app level handler
145*5d07396bSMatthias Ringwald     if (att_client_packet_handler){
146*5d07396bSMatthias Ringwald         (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size);
147*5d07396bSMatthias Ringwald     }
1483deb3ec6SMatthias Ringwald 
149*5d07396bSMatthias Ringwald     // dispatch to service handlers
150*5d07396bSMatthias Ringwald     btstack_linked_list_iterator_t it;
151*5d07396bSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
152*5d07396bSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
153*5d07396bSMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
154*5d07396bSMatthias Ringwald         if (!handler->packet_handler) continue;
155*5d07396bSMatthias Ringwald         (*handler->packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size);
156*5d07396bSMatthias Ringwald     }
157*5d07396bSMatthias Ringwald }
158*5d07396bSMatthias Ringwald 
159*5d07396bSMatthias Ringwald static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){
1603deb3ec6SMatthias Ringwald     uint8_t event[6];
1613deb3ec6SMatthias Ringwald     int pos = 0;
1625611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE;
1633deb3ec6SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
164fc64f94aSMatthias Ringwald     little_endian_store_16(event, pos, con_handle);
1653deb3ec6SMatthias Ringwald     pos += 2;
166f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, mtu);
167*5d07396bSMatthias Ringwald 
168*5d07396bSMatthias Ringwald     // also dispatch to GATT Clients
169b12646c5SJakob Krantz     att_dispatch_server_mtu_exchanged(con_handle, mtu);
170*5d07396bSMatthias Ringwald 
171*5d07396bSMatthias Ringwald     // dispatch to app level handler and service handlers
172*5d07396bSMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
1733deb3ec6SMatthias Ringwald }
1743deb3ec6SMatthias Ringwald 
1755f141511SMatthias Ringwald static void att_emit_can_send_now_event(void * context){
1765f141511SMatthias Ringwald     UNUSED(context);
1773c97b242SMatthias Ringwald     if (!att_client_packet_handler) return;
1783c97b242SMatthias Ringwald 
1791b96b007SMatthias Ringwald     uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0};
1801b96b007SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1811b96b007SMatthias Ringwald }
1821b96b007SMatthias Ringwald 
183ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){
18454178543SMatthias Ringwald     void * context = btstack_run_loop_get_timer_context(ts);
18554178543SMatthias Ringwald     hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
18618707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
18718707219SMatthias Ringwald     if (!att_server) return;
188bce48a26SMatthias Ringwald     // @note: after a transcation timeout, no more requests shall be sent over this ATT Bearer
189bce48a26SMatthias Ringwald     // (that's why we don't reset the value_indication_handle)
19018707219SMatthias Ringwald     uint16_t att_handle = att_server->value_indication_handle;
19118707219SMatthias Ringwald     att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_server->connection.con_handle, att_handle);
1923deb3ec6SMatthias Ringwald }
1933deb3ec6SMatthias Ringwald 
1943deb3ec6SMatthias Ringwald static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1959ec2630cSMatthias Ringwald 
1967dad9ff8SMatthias Ringwald     UNUSED(channel); // ok: there is no channel
1977dad9ff8SMatthias Ringwald     UNUSED(size);    // ok: handling own l2cap events
1983deb3ec6SMatthias Ringwald 
19918707219SMatthias Ringwald     att_server_t * att_server;
20018707219SMatthias Ringwald     hci_con_handle_t con_handle;
20118707219SMatthias Ringwald 
2023deb3ec6SMatthias Ringwald     switch (packet_type) {
2033deb3ec6SMatthias Ringwald 
2043deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
2050e2df43fSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
2063deb3ec6SMatthias Ringwald 
2073deb3ec6SMatthias Ringwald                 case HCI_EVENT_LE_META:
2083deb3ec6SMatthias Ringwald                     switch (packet[2]) {
2093deb3ec6SMatthias Ringwald                         case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
21018707219SMatthias Ringwald                             con_handle = little_endian_read_16(packet, 4);
21118707219SMatthias Ringwald                             att_server = att_server_for_handle(con_handle);
21218707219SMatthias Ringwald                             if (!att_server) break;
2133deb3ec6SMatthias Ringwald                         	// store connection info
21418707219SMatthias Ringwald                         	att_server->peer_addr_type = packet[7];
21518707219SMatthias Ringwald                             reverse_bd_addr(&packet[8], att_server->peer_address);
21618707219SMatthias Ringwald                             att_server->connection.con_handle = con_handle;
2173deb3ec6SMatthias Ringwald                             // reset connection properties
21818707219SMatthias Ringwald                             att_server->state = ATT_SERVER_IDLE;
21918707219SMatthias Ringwald                             att_server->connection.mtu = ATT_DEFAULT_MTU;
22018707219SMatthias Ringwald                             att_server->connection.max_mtu = l2cap_max_le_mtu();
22118707219SMatthias Ringwald                             if (att_server->connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){
22218707219SMatthias Ringwald                                 att_server->connection.max_mtu = ATT_REQUEST_BUFFER_SIZE;
22399c58ad0SMatthias Ringwald                             }
22418707219SMatthias Ringwald                             att_server->connection.encryption_key_size = 0;
22518707219SMatthias Ringwald                             att_server->connection.authenticated = 0;
22618707219SMatthias Ringwald 		                	att_server->connection.authorized = 0;
227b2c1e52cSMatthias Ringwald                             // workaround: identity resolving can already be complete, at least store result
228b2c1e52cSMatthias Ringwald                             att_server->ir_le_device_db_index = sm_le_device_index(con_handle);
229760ad805SMatthias Ringwald                             att_server->pairing_active = 0;
230*5d07396bSMatthias Ringwald                             // notify all
231*5d07396bSMatthias Ringwald                             att_emit_event_to_all(packet, size);
2323deb3ec6SMatthias Ringwald                             break;
2333deb3ec6SMatthias Ringwald 
2343deb3ec6SMatthias Ringwald                         default:
2353deb3ec6SMatthias Ringwald                             break;
2363deb3ec6SMatthias Ringwald                     }
2373deb3ec6SMatthias Ringwald                     break;
2383deb3ec6SMatthias Ringwald 
2393deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_CHANGE:
2403deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE:
2413deb3ec6SMatthias Ringwald                 	// check handle
24218707219SMatthias Ringwald                     con_handle = little_endian_read_16(packet, 3);
24318707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
24418707219SMatthias Ringwald                     if (!att_server) break;
2459c6e867eSMatthias Ringwald                     att_server->connection.encryption_key_size = gap_encryption_key_size(con_handle);
2469c6e867eSMatthias Ringwald                     att_server->connection.authenticated = gap_authenticated(con_handle);
24701ac2008SMatthias Ringwald                     if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){
24801ac2008SMatthias Ringwald                         // restore CCC values when encrypted
24901ac2008SMatthias Ringwald                         if (hci_event_encryption_change_get_encryption_enabled(packet)){
25001ac2008SMatthias Ringwald                             att_server_persistent_ccc_restore(att_server);
25101ac2008SMatthias Ringwald                         }
25201ac2008SMatthias Ringwald                     }
2530234fbbcSMatthias Ringwald                     att_run_for_context(att_server);
2543deb3ec6SMatthias Ringwald                     break;
2553deb3ec6SMatthias Ringwald 
2563deb3ec6SMatthias Ringwald                 case HCI_EVENT_DISCONNECTION_COMPLETE:
25770a390c7SMatthias Ringwald                     // check handle
25818707219SMatthias Ringwald                     con_handle = hci_event_disconnection_complete_get_connection_handle(packet);
25918707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
26018707219SMatthias Ringwald                     if (!att_server) break;
26118707219SMatthias Ringwald                     att_clear_transaction_queue(&att_server->connection);
26218707219SMatthias Ringwald                     att_server->connection.con_handle = 0;
263760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
26418707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
265bce48a26SMatthias Ringwald                     if (att_server->value_indication_handle){
266bce48a26SMatthias Ringwald                         uint16_t att_handle = att_server->value_indication_handle;
267bce48a26SMatthias Ringwald                         att_server->value_indication_handle = 0; // reset error state
268bce48a26SMatthias Ringwald                         att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_DISCONNECT, att_server->connection.con_handle, att_handle);
269bce48a26SMatthias Ringwald                     }
270*5d07396bSMatthias Ringwald                     // notify all
271*5d07396bSMatthias Ringwald                     att_emit_event_to_all(packet, size);
2723deb3ec6SMatthias Ringwald                     break;
2733deb3ec6SMatthias Ringwald 
274760ad805SMatthias Ringwald                 // Identity Resolving
2755611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_STARTED:
27618707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_started_get_handle(packet);
27718707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
27818707219SMatthias Ringwald                     if (!att_server) break;
2795611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED");
28018707219SMatthias Ringwald                     att_server->ir_lookup_active = 1;
2813deb3ec6SMatthias Ringwald                     break;
2825611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
283760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
284760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
285760ad805SMatthias Ringwald                     if (!att_server) return;
2861b7acd0dSMatthias Ringwald                     att_server->ir_lookup_active = 0;
2871b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index(packet);
2881b7acd0dSMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED");
2891b7acd0dSMatthias Ringwald                     att_run_for_context(att_server);
2903deb3ec6SMatthias Ringwald                     break;
2915611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_FAILED:
29218707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_failed_get_handle(packet);
29318707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
29418707219SMatthias Ringwald                     if (!att_server) break;
2955611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED");
29618707219SMatthias Ringwald                     att_server->ir_lookup_active = 0;
29718707219SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
29818707219SMatthias Ringwald                     att_run_for_context(att_server);
2993deb3ec6SMatthias Ringwald                     break;
300760ad805SMatthias Ringwald 
301760ad805SMatthias Ringwald                 // Pairing started - delete stored CCC values
302760ad805SMatthias Ringwald                 // - assumes pairing indicates either new device or re-pairing, in both cases there should be no stored CCC values
303760ad805SMatthias Ringwald                 // - assumes that all events have the con handle as the first field
304760ad805SMatthias Ringwald                 case SM_EVENT_JUST_WORKS_REQUEST:
305760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
306760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_INPUT_NUMBER:
307760ad805SMatthias Ringwald                 case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
308760ad805SMatthias Ringwald                     con_handle = sm_event_just_works_request_get_handle(packet);
309760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
310760ad805SMatthias Ringwald                     if (!att_server) break;
311760ad805SMatthias Ringwald                     att_server->pairing_active = 1;
312760ad805SMatthias Ringwald                     log_info("SM Pairing started");
313760ad805SMatthias Ringwald                     if (att_server->ir_le_device_db_index < 0) break;
314760ad805SMatthias Ringwald                     att_server_persistent_ccc_clear(att_server);
315760ad805SMatthias Ringwald                     // index not valid anymore
316760ad805SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
317760ad805SMatthias Ringwald                     break;
318760ad805SMatthias Ringwald 
3191b7acd0dSMatthias Ringwald                 // Bonding completed
320760ad805SMatthias Ringwald                 case SM_EVENT_IDENTITY_CREATED:
321760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
322760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
323760ad805SMatthias Ringwald                     if (!att_server) return;
324760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
3251b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_created_get_index(packet);
3261b7acd0dSMatthias Ringwald                     att_run_for_context(att_server);
3271b7acd0dSMatthias Ringwald                     break;
3281b7acd0dSMatthias Ringwald 
3291b7acd0dSMatthias Ringwald                 // Pairing complete (with/without bonding=storing of pairing information)
3301b7acd0dSMatthias Ringwald                 case SM_EVENT_PAIRING_COMPLETE:
3311b7acd0dSMatthias Ringwald                     con_handle = sm_event_pairing_complete_get_handle(packet);
3321b7acd0dSMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
3331b7acd0dSMatthias Ringwald                     if (!att_server) return;
3341b7acd0dSMatthias Ringwald                     att_server->pairing_active = 0;
3351b7acd0dSMatthias Ringwald                     att_run_for_context(att_server);
336760ad805SMatthias Ringwald                     break;
337760ad805SMatthias Ringwald 
338760ad805SMatthias Ringwald                 // Authorization
3395611a760SMatthias Ringwald                 case SM_EVENT_AUTHORIZATION_RESULT: {
34018707219SMatthias Ringwald                     con_handle = sm_event_authorization_result_get_handle(packet);
34118707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
34218707219SMatthias Ringwald                     if (!att_server) break;
34318707219SMatthias Ringwald                     att_server->connection.authorized = sm_event_authorization_result_get_authorization_result(packet);
34418707219SMatthias Ringwald                     att_dispatch_server_request_can_send_now_event(con_handle);
3453deb3ec6SMatthias Ringwald                 	break;
3463deb3ec6SMatthias Ringwald                 }
3473deb3ec6SMatthias Ringwald                 default:
3483deb3ec6SMatthias Ringwald                     break;
3493deb3ec6SMatthias Ringwald             }
35065b44ffdSMatthias Ringwald             break;
35165b44ffdSMatthias Ringwald         default:
35265b44ffdSMatthias Ringwald             break;
3533deb3ec6SMatthias Ringwald     }
3543deb3ec6SMatthias Ringwald }
3553deb3ec6SMatthias Ringwald 
3567a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
3573deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
3583deb3ec6SMatthias Ringwald 
35918707219SMatthias Ringwald     att_server_t * att_server = att_server_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION);
36018707219SMatthias Ringwald     if (!att_server) return;
3613deb3ec6SMatthias Ringwald 
3623deb3ec6SMatthias Ringwald     uint8_t hash_flipped[8];
3639c80e4ccSMatthias Ringwald     reverse_64(hash, hash_flipped);
36418707219SMatthias Ringwald     if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8)){
3653deb3ec6SMatthias Ringwald         log_info("ATT Signed Write, invalid signature");
36618707219SMatthias Ringwald         att_server->state = ATT_SERVER_IDLE;
3673deb3ec6SMatthias Ringwald         return;
3683deb3ec6SMatthias Ringwald     }
3693deb3ec6SMatthias Ringwald     log_info("ATT Signed Write, valid signature");
3703deb3ec6SMatthias Ringwald 
3713deb3ec6SMatthias Ringwald     // update sequence number
37218707219SMatthias Ringwald     uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
37318707219SMatthias Ringwald     le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1);
37418707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
37518707219SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
3763deb3ec6SMatthias Ringwald }
3777a766ebfSMatthias Ringwald #endif
37888a48dd8SMatthias Ringwald 
37918707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED
380b06f2579SMatthias Ringwald // pre: can send now
381b06f2579SMatthias Ringwald // returns: 1 if packet was sent
38218707219SMatthias Ringwald static int att_server_process_validated_request(att_server_t * att_server){
383b06f2579SMatthias Ringwald 
384b06f2579SMatthias Ringwald     l2cap_reserve_packet_buffer();
385b06f2579SMatthias Ringwald     uint8_t * att_response_buffer = l2cap_get_outgoing_buffer();
38618707219SMatthias Ringwald     uint16_t  att_response_size   = att_handle_request(&att_server->connection, att_server->request_buffer, att_server->request_size, att_response_buffer);
387b06f2579SMatthias Ringwald 
388e404a688SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_READ_RESPONSE
389e404a688SMatthias Ringwald     if (att_response_size == ATT_READ_RESPONSE_PENDING){
390e404a688SMatthias Ringwald         // update state
391e404a688SMatthias Ringwald         att_server->state = ATT_SERVER_READ_RESPONSE_PENDING;
392e404a688SMatthias Ringwald 
393e404a688SMatthias Ringwald         // callback with handle ATT_READ_RESPONSE_PENDING
394e404a688SMatthias Ringwald         att_server_client_read_callback(att_server->connection.con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0);
395e8e7403eSMatthias Ringwald 
396e8e7403eSMatthias Ringwald         // free reserved buffer
397e8e7403eSMatthias Ringwald         l2cap_release_packet_buffer();
398e8e7403eSMatthias Ringwald         return 0;
399e404a688SMatthias Ringwald     }
400e404a688SMatthias Ringwald #endif
401e404a688SMatthias Ringwald 
402b06f2579SMatthias Ringwald     // intercept "insufficient authorization" for authenticated connections to allow for user authorization
403b06f2579SMatthias Ringwald     if ((att_response_size     >= 4)
404b06f2579SMatthias Ringwald     && (att_response_buffer[0] == ATT_ERROR_RESPONSE)
405b06f2579SMatthias Ringwald     && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION)
40618707219SMatthias Ringwald     && (att_server->connection.authenticated)){
407b06f2579SMatthias Ringwald 
4089c6e867eSMatthias Ringwald         switch (gap_authorization_state(att_server->connection.con_handle)){
409b06f2579SMatthias Ringwald             case AUTHORIZATION_UNKNOWN:
410b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
41118707219SMatthias Ringwald                 sm_request_pairing(att_server->connection.con_handle);
412b06f2579SMatthias Ringwald                 return 0;
413b06f2579SMatthias Ringwald             case AUTHORIZATION_PENDING:
414b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
415b06f2579SMatthias Ringwald                 return 0;
416b06f2579SMatthias Ringwald             default:
417b06f2579SMatthias Ringwald                 break;
418b06f2579SMatthias Ringwald         }
419b06f2579SMatthias Ringwald     }
420b06f2579SMatthias Ringwald 
42118707219SMatthias Ringwald     att_server->state = ATT_SERVER_IDLE;
422b06f2579SMatthias Ringwald     if (att_response_size == 0) {
423b06f2579SMatthias Ringwald         l2cap_release_packet_buffer();
424b06f2579SMatthias Ringwald         return 0;
425b06f2579SMatthias Ringwald     }
426b06f2579SMatthias Ringwald 
42718707219SMatthias Ringwald     l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size);
428b06f2579SMatthias Ringwald 
429b06f2579SMatthias Ringwald     // notify client about MTU exchange result
430b06f2579SMatthias Ringwald     if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){
43118707219SMatthias Ringwald         att_emit_mtu_event(att_server->connection.con_handle, att_server->connection.mtu);
432b06f2579SMatthias Ringwald     }
433b06f2579SMatthias Ringwald     return 1;
434b06f2579SMatthias Ringwald }
435b06f2579SMatthias Ringwald 
436e404a688SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_READ_RESPONSE
437e404a688SMatthias Ringwald int att_server_read_response_ready(hci_con_handle_t con_handle){
438e404a688SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
439e404a688SMatthias Ringwald     if (!att_server)                                             return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
440e404a688SMatthias Ringwald     if (att_server->state != ATT_SERVER_READ_RESPONSE_PENDING)   return ERROR_CODE_COMMAND_DISALLOWED;
441e404a688SMatthias Ringwald 
442e404a688SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
443e404a688SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
444e404a688SMatthias Ringwald     return ERROR_CODE_SUCCESS;
445e404a688SMatthias Ringwald }
446e404a688SMatthias Ringwald #endif
447e404a688SMatthias Ringwald 
44818707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server){
44918707219SMatthias Ringwald     switch (att_server->state){
4503deb3ec6SMatthias Ringwald         case ATT_SERVER_REQUEST_RECEIVED:
451760ad805SMatthias Ringwald 
4520234fbbcSMatthias Ringwald             // wait until re-encryption as central is complete
4530234fbbcSMatthias Ringwald             if (gap_reconnect_security_setup_active(att_server->connection.con_handle)) break;
4540234fbbcSMatthias Ringwald 
455760ad805SMatthias Ringwald             // wait until pairing is complete
456760ad805SMatthias Ringwald             if (att_server->pairing_active) break;
457760ad805SMatthias Ringwald 
4587a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
45918707219SMatthias Ringwald             if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){
4603deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write!");
4613deb3ec6SMatthias Ringwald                 if (!sm_cmac_ready()) {
4623deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, sm_cmac engine not ready. Abort");
46318707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
4643deb3ec6SMatthias Ringwald                     return;
4653deb3ec6SMatthias Ringwald                 }
46618707219SMatthias Ringwald                 if (att_server->request_size < (3 + 12)) {
4673deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, request to short. Abort.");
46818707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
4693deb3ec6SMatthias Ringwald                     return;
4703deb3ec6SMatthias Ringwald                 }
47118707219SMatthias Ringwald                 if (att_server->ir_lookup_active){
4723deb3ec6SMatthias Ringwald                     return;
4733deb3ec6SMatthias Ringwald                 }
47418707219SMatthias Ringwald                 if (att_server->ir_le_device_db_index < 0){
4753deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, CSRK not available");
47618707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
4773deb3ec6SMatthias Ringwald                     return;
4783deb3ec6SMatthias Ringwald                 }
4793deb3ec6SMatthias Ringwald 
4803deb3ec6SMatthias Ringwald                 // check counter
48118707219SMatthias Ringwald                 uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
48218707219SMatthias Ringwald                 uint32_t counter_db     = le_device_db_remote_counter_get(att_server->ir_le_device_db_index);
4833deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet);
4843deb3ec6SMatthias Ringwald                 if (counter_packet < counter_db){
4853deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, db reports higher counter, abort");
48618707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
4873deb3ec6SMatthias Ringwald                     return;
4883deb3ec6SMatthias Ringwald                 }
4893deb3ec6SMatthias Ringwald 
4903deb3ec6SMatthias Ringwald                 // signature is { sequence counter, secure hash }
4913deb3ec6SMatthias Ringwald                 sm_key_t csrk;
49218707219SMatthias Ringwald                 le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk);
49318707219SMatthias Ringwald                 att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION;
4943deb3ec6SMatthias Ringwald                 log_info("Orig Signature: ");
49518707219SMatthias Ringwald                 log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8);
49618707219SMatthias Ringwald                 uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1);
49718707219SMatthias 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);
4983deb3ec6SMatthias Ringwald                 return;
4993deb3ec6SMatthias Ringwald             }
5007a766ebfSMatthias Ringwald #endif
501b06f2579SMatthias Ringwald             // move on
50218707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
50318707219SMatthias Ringwald             att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
504b06f2579SMatthias Ringwald             break;
50588a48dd8SMatthias Ringwald 
5063deb3ec6SMatthias Ringwald         default:
5073deb3ec6SMatthias Ringwald             break;
5083deb3ec6SMatthias Ringwald     }
5093deb3ec6SMatthias Ringwald }
5103deb3ec6SMatthias Ringwald 
51190f03c80SMatthias Ringwald static int att_server_data_ready_for_phase(att_server_t * att_server,  att_server_run_phase_t phase){
51290f03c80SMatthias Ringwald     switch (phase){
51390f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
51490f03c80SMatthias Ringwald             return att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
51590f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
51690f03c80SMatthias Ringwald              return (!btstack_linked_list_empty(&att_server->indication_requests) && att_server->value_indication_handle == 0);
51790f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
51890f03c80SMatthias Ringwald             return (!btstack_linked_list_empty(&att_server->notification_requests));
51990f03c80SMatthias Ringwald     }
520fa5e3464SMatthias Ringwald     // avoid warning
521fa5e3464SMatthias Ringwald     return 0;
52290f03c80SMatthias Ringwald }
52390f03c80SMatthias Ringwald 
52490f03c80SMatthias Ringwald static void att_server_trigger_send_for_phase(att_server_t * att_server,  att_server_run_phase_t phase){
52590f03c80SMatthias Ringwald     btstack_context_callback_registration_t * client;
52690f03c80SMatthias Ringwald     switch (phase){
52790f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
52890f03c80SMatthias Ringwald             att_server_process_validated_request(att_server);
52990f03c80SMatthias Ringwald             break;
53090f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
53190f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->indication_requests;
53290f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->indication_requests, (btstack_linked_item_t *) client);
53390f03c80SMatthias Ringwald             client->callback(client->context);
53490f03c80SMatthias Ringwald             break;
53590f03c80SMatthias Ringwald        case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
53690f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->notification_requests;
53790f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->notification_requests, (btstack_linked_item_t *) client);
53890f03c80SMatthias Ringwald             client->callback(client->context);
53990f03c80SMatthias Ringwald             break;
54090f03c80SMatthias Ringwald     }
54190f03c80SMatthias Ringwald }
54290f03c80SMatthias Ringwald 
5437eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){
544b06f2579SMatthias Ringwald 
545374a288aSMatthias Ringwald     hci_con_handle_t request_con_handle   = HCI_CON_HANDLE_INVALID;
5466438547aSMatthias Ringwald     hci_con_handle_t last_send_con_handle = HCI_CON_HANDLE_INVALID;
5476438547aSMatthias Ringwald     int can_send_now = 1;
5483551936eSMatthias Ringwald     int phase;
5496438547aSMatthias Ringwald 
5503551936eSMatthias Ringwald     for (phase = ATT_SERVER_RUN_PHASE_1_REQUESTS; phase <= ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS; phase++){
5516438547aSMatthias Ringwald         hci_con_handle_t skip_connections_until = att_server_last_can_send_now;
552374a288aSMatthias Ringwald         while (1){
5536438547aSMatthias Ringwald             btstack_linked_list_iterator_t it;
55418707219SMatthias Ringwald             hci_connections_get_iterator(&it);
55518707219SMatthias Ringwald             while(btstack_linked_list_iterator_has_next(&it)){
55618707219SMatthias Ringwald                 hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
55718707219SMatthias Ringwald                 att_server_t * att_server = &connection->att_server;
5586438547aSMatthias Ringwald 
55990f03c80SMatthias Ringwald                 int data_ready = att_server_data_ready_for_phase(att_server, phase);
5606438547aSMatthias Ringwald 
5616438547aSMatthias Ringwald                 // log_debug("phase %u, handle 0x%04x, skip until 0x%04x, data ready %u", phase, att_server->connection.con_handle, skip_connections_until, data_ready);
5626438547aSMatthias Ringwald 
5636438547aSMatthias Ringwald                 // skip until last sender found (which is also skipped)
5646438547aSMatthias Ringwald                 if (skip_connections_until != HCI_CON_HANDLE_INVALID){
5656438547aSMatthias Ringwald                     if (data_ready && request_con_handle == HCI_CON_HANDLE_INVALID){
5666438547aSMatthias Ringwald                         request_con_handle = att_server->connection.con_handle;
5676438547aSMatthias Ringwald                     }
5686438547aSMatthias Ringwald                     if (skip_connections_until == att_server->connection.con_handle){
5696438547aSMatthias Ringwald                         skip_connections_until = HCI_CON_HANDLE_INVALID;
5706438547aSMatthias Ringwald                     }
5716438547aSMatthias Ringwald                     continue;
5726438547aSMatthias Ringwald                 };
5736438547aSMatthias Ringwald 
57490f03c80SMatthias Ringwald                 if (data_ready){
575969a5bbaSMatthias Ringwald                     if (can_send_now){
57690f03c80SMatthias Ringwald                         att_server_trigger_send_for_phase(att_server, phase);
5776438547aSMatthias Ringwald                         last_send_con_handle = att_server->connection.con_handle;
578969a5bbaSMatthias Ringwald                         can_send_now = att_dispatch_server_can_send_now(att_server->connection.con_handle);
57990f03c80SMatthias Ringwald                         data_ready = att_server_data_ready_for_phase(att_server, phase);
5806438547aSMatthias Ringwald                         if (data_ready && request_con_handle == HCI_CON_HANDLE_INVALID){
581cbbb12d9SMatthias Ringwald                             request_con_handle = att_server->connection.con_handle;
582cbbb12d9SMatthias Ringwald                         }
583cbbb12d9SMatthias Ringwald                     } else {
584f29a88d8SMatthias Ringwald                         request_con_handle = att_server->connection.con_handle;
585f29a88d8SMatthias Ringwald                         break;
586cbbb12d9SMatthias Ringwald                     }
587cbbb12d9SMatthias Ringwald                 }
5883deb3ec6SMatthias Ringwald             }
5893deb3ec6SMatthias Ringwald 
5906438547aSMatthias Ringwald             // stop skipping (handles disconnect by last send connection)
5916438547aSMatthias Ringwald             skip_connections_until = HCI_CON_HANDLE_INVALID;
5926438547aSMatthias Ringwald 
5933551936eSMatthias Ringwald             // Exit loop, if we cannot send
594969a5bbaSMatthias Ringwald             if (!can_send_now) break;
595969a5bbaSMatthias Ringwald 
596969a5bbaSMatthias Ringwald             // Exit loop, if we can send but there are also no further request
597969a5bbaSMatthias Ringwald             if (request_con_handle == HCI_CON_HANDLE_INVALID) break;
598969a5bbaSMatthias Ringwald 
599969a5bbaSMatthias Ringwald             // Finally, if we still can send and there are requests, just try again
600969a5bbaSMatthias Ringwald             request_con_handle = HCI_CON_HANDLE_INVALID;
601969a5bbaSMatthias Ringwald         }
6026438547aSMatthias Ringwald         // update last send con handle for round robin
6036438547aSMatthias Ringwald         if (last_send_con_handle != HCI_CON_HANDLE_INVALID){
6046438547aSMatthias Ringwald             att_server_last_can_send_now = last_send_con_handle;
6056438547aSMatthias Ringwald         }
6063551936eSMatthias Ringwald     }
607969a5bbaSMatthias Ringwald 
608969a5bbaSMatthias Ringwald     if (request_con_handle == HCI_CON_HANDLE_INVALID) return;
609969a5bbaSMatthias Ringwald     att_dispatch_server_request_can_send_now_event(request_con_handle);
610969a5bbaSMatthias Ringwald }
611969a5bbaSMatthias Ringwald 
61288a48dd8SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
61318707219SMatthias Ringwald 
61418707219SMatthias Ringwald     att_server_t * att_server;
61518707219SMatthias Ringwald 
61688a48dd8SMatthias Ringwald     switch (packet_type){
61788a48dd8SMatthias Ringwald 
61888a48dd8SMatthias Ringwald         case HCI_EVENT_PACKET:
619bd87a16eSMatthias Ringwald             switch (packet[0]){
620bd87a16eSMatthias Ringwald                 case L2CAP_EVENT_CAN_SEND_NOW:
6214d2be802SMatthias Ringwald                     att_server_handle_can_send_now();
622372d62c4SMatthias Ringwald                     break;
623bd87a16eSMatthias Ringwald                 case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
624bd87a16eSMatthias Ringwald                     // GATT client has negotiated the mtu for this connection
6256155bae0SMatthias Ringwald                     att_server = att_server_for_handle(handle);
6266155bae0SMatthias Ringwald                     if (!att_server) break;
627bd87a16eSMatthias Ringwald                     att_server->connection.mtu = little_endian_read_16(packet, 4);
628bd87a16eSMatthias Ringwald                     break;
629bd87a16eSMatthias Ringwald                 default:
630bd87a16eSMatthias Ringwald                     break;
631bd87a16eSMatthias Ringwald             }
632bd87a16eSMatthias Ringwald             break;
6333deb3ec6SMatthias Ringwald 
634372d62c4SMatthias Ringwald         case ATT_DATA_PACKET:
635f50ef7ddSMatthias Ringwald             log_debug("ATT Packet, handle 0x%04x", handle);
63618707219SMatthias Ringwald             att_server = att_server_for_handle(handle);
63718707219SMatthias Ringwald             if (!att_server) break;
63818707219SMatthias Ringwald 
6393deb3ec6SMatthias Ringwald             // handle value indication confirms
64018707219SMatthias Ringwald             if (packet[0] == ATT_HANDLE_VALUE_CONFIRMATION && att_server->value_indication_handle){
64118707219SMatthias Ringwald                 btstack_run_loop_remove_timer(&att_server->value_indication_timer);
64218707219SMatthias Ringwald                 uint16_t att_handle = att_server->value_indication_handle;
64318707219SMatthias Ringwald                 att_server->value_indication_handle = 0;
64418707219SMatthias Ringwald                 att_handle_value_indication_notify_client(0, att_server->connection.con_handle, att_handle);
645cbbb12d9SMatthias Ringwald                 att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
6463deb3ec6SMatthias Ringwald                 return;
6473deb3ec6SMatthias Ringwald             }
6483deb3ec6SMatthias Ringwald 
6496428eb5aSMatthias Ringwald             // directly process command
6506428eb5aSMatthias Ringwald             // note: signed write cannot be handled directly as authentication needs to be verified
6516428eb5aSMatthias Ringwald             if (packet[0] == ATT_WRITE_COMMAND){
65218707219SMatthias Ringwald                 att_handle_request(&att_server->connection, packet, size, 0);
6536428eb5aSMatthias Ringwald                 return;
6546428eb5aSMatthias Ringwald             }
6556428eb5aSMatthias Ringwald 
6563deb3ec6SMatthias Ringwald             // check size
65718707219SMatthias Ringwald             if (size > sizeof(att_server->request_buffer)) {
65818707219SMatthias 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));
6596428eb5aSMatthias Ringwald                 return;
6606428eb5aSMatthias Ringwald             }
6613deb3ec6SMatthias Ringwald 
6623deb3ec6SMatthias Ringwald             // last request still in processing?
66318707219SMatthias Ringwald             if (att_server->state != ATT_SERVER_IDLE){
66418707219SMatthias Ringwald                 log_info("att_packet_handler: skipping att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state);
6656428eb5aSMatthias Ringwald                 return;
6666428eb5aSMatthias Ringwald             }
6673deb3ec6SMatthias Ringwald 
6683deb3ec6SMatthias Ringwald             // store request
66918707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED;
67018707219SMatthias Ringwald             att_server->request_size = size;
67118707219SMatthias Ringwald             memcpy(att_server->request_buffer, packet, size);
6723deb3ec6SMatthias Ringwald 
67318707219SMatthias Ringwald             att_run_for_context(att_server);
674372d62c4SMatthias Ringwald             break;
675372d62c4SMatthias Ringwald     }
6763deb3ec6SMatthias Ringwald }
6773deb3ec6SMatthias Ringwald 
678bf78aac6SMatthias Ringwald // ---------------------
679bf78aac6SMatthias Ringwald // persistent CCC writes
680bf78aac6SMatthias Ringwald static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){
681bf78aac6SMatthias Ringwald     return 'B' << 24 | 'T' << 16 | 'C' << 8 | index;
682bf78aac6SMatthias Ringwald }
683bf78aac6SMatthias Ringwald 
684bf78aac6SMatthias Ringwald static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){
685bf78aac6SMatthias Ringwald     // lookup att_server instance
686bf78aac6SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
687bf78aac6SMatthias Ringwald     if (!att_server) return;
688bf78aac6SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
689bf78aac6SMatthias 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);
6906a540790SMatthias Ringwald 
691bf78aac6SMatthias Ringwald     // check if bonded
692bf78aac6SMatthias Ringwald     if (le_device_index < 0) return;
6936a540790SMatthias Ringwald 
694bf78aac6SMatthias Ringwald     // get btstack_tlv
695bf78aac6SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
696bf78aac6SMatthias Ringwald     void * tlv_context;
697bf78aac6SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
698bf78aac6SMatthias Ringwald     if (!tlv_impl) return;
6996a540790SMatthias Ringwald 
700bf78aac6SMatthias Ringwald     // update ccc tag
701bf78aac6SMatthias Ringwald     int index;
7026a540790SMatthias Ringwald     uint32_t highest_seq_nr = 0;
7036a540790SMatthias Ringwald     uint32_t lowest_seq_nr = 0;
7046a540790SMatthias Ringwald     uint32_t tag_for_lowest_seq_nr = 0;
7056a540790SMatthias Ringwald     uint32_t tag_for_empty = 0;
7066a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
7076afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
708bf78aac6SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
7096a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
7106a540790SMatthias Ringwald 
7116a540790SMatthias Ringwald         // empty/invalid tag
7126a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)){
7136a540790SMatthias Ringwald             tag_for_empty = tag;
714bf78aac6SMatthias Ringwald             continue;
715bf78aac6SMatthias Ringwald         }
7166a540790SMatthias Ringwald         // update highest seq nr
7176a540790SMatthias Ringwald         if (entry.seq_nr > highest_seq_nr){
7186a540790SMatthias Ringwald             highest_seq_nr = entry.seq_nr;
7196a540790SMatthias Ringwald         }
7206a540790SMatthias Ringwald         // find entry with lowest seq nr
7216a540790SMatthias Ringwald         if ((tag_for_lowest_seq_nr == 0) || (entry.seq_nr < lowest_seq_nr)){
7226a540790SMatthias Ringwald             tag_for_lowest_seq_nr = tag;
7236a540790SMatthias Ringwald             lowest_seq_nr = entry.seq_nr;
7246a540790SMatthias Ringwald         }
7256a540790SMatthias Ringwald 
7266a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
7276a540790SMatthias Ringwald         if (entry.att_handle   != att_handle)      continue;
7286a540790SMatthias Ringwald 
7296a540790SMatthias Ringwald         // found matching entry
73001ac2008SMatthias Ringwald         if (value){
731bf78aac6SMatthias Ringwald             // update
7326a540790SMatthias Ringwald             if (entry.value == value) {
733760ad805SMatthias Ringwald                 log_info("CCC Index %u: Up-to-date", index);
734760ad805SMatthias Ringwald                 return;
735760ad805SMatthias Ringwald             }
7366a540790SMatthias Ringwald             entry.value = value;
7376a540790SMatthias Ringwald             entry.seq_nr = highest_seq_nr + 1;
738760ad805SMatthias Ringwald             log_info("CCC Index %u: Store", index);
7396a540790SMatthias Ringwald             tlv_impl->store_tag(tlv_context, tag, (const uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
74001ac2008SMatthias Ringwald         } else {
74101ac2008SMatthias Ringwald             // delete
742760ad805SMatthias Ringwald             log_info("CCC Index %u: Delete", index);
74301ac2008SMatthias Ringwald             tlv_impl->delete_tag(tlv_context, tag);
74401ac2008SMatthias Ringwald         }
745bf78aac6SMatthias Ringwald         return;
746bf78aac6SMatthias Ringwald     }
7476a540790SMatthias Ringwald 
748faf2b6c3SMatthias Ringwald     log_info("tag_for_empy %"PRIx32", tag_for_lowest_seq_nr %"PRIx32, tag_for_empty, tag_for_lowest_seq_nr);
7496a540790SMatthias Ringwald 
7506a540790SMatthias Ringwald     if (value == 0){
7516a540790SMatthias Ringwald         // done
7526a540790SMatthias Ringwald         return;
7536a540790SMatthias Ringwald     }
7546a540790SMatthias Ringwald 
7556a540790SMatthias Ringwald     uint32_t tag_to_use = 0;
7566a540790SMatthias Ringwald     if (tag_for_empty){
7576a540790SMatthias Ringwald         tag_to_use = tag_for_empty;
7586a540790SMatthias Ringwald     } else if (tag_for_lowest_seq_nr){
7596a540790SMatthias Ringwald         tag_to_use = tag_for_lowest_seq_nr;
7606a540790SMatthias Ringwald     } else {
7616a540790SMatthias Ringwald         // should not happen
7626a540790SMatthias Ringwald         return;
7636a540790SMatthias Ringwald     }
764bf78aac6SMatthias Ringwald     // store ccc tag
7656a540790SMatthias Ringwald     entry.seq_nr       = highest_seq_nr + 1;
7666a540790SMatthias Ringwald     entry.device_index = le_device_index;
7676a540790SMatthias Ringwald     entry.att_handle   = att_handle;
7686a540790SMatthias Ringwald     entry.value        = value;
7696a540790SMatthias Ringwald     tlv_impl->store_tag(tlv_context, tag_to_use, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
770bf78aac6SMatthias Ringwald }
771bf78aac6SMatthias Ringwald 
772760ad805SMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server){
773760ad805SMatthias Ringwald     if (!att_server) return;
774760ad805SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
775760ad805SMatthias Ringwald     log_info("Clear CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
776760ad805SMatthias Ringwald     // check if bonded
777760ad805SMatthias Ringwald     if (le_device_index < 0) return;
778760ad805SMatthias Ringwald     // get btstack_tlv
779760ad805SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
780760ad805SMatthias Ringwald     void * tlv_context;
781760ad805SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
782760ad805SMatthias Ringwald     if (!tlv_impl) return;
783760ad805SMatthias Ringwald     // get all ccc tag
784760ad805SMatthias Ringwald     int index;
7856a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
7866afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
787760ad805SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
7886a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
7896a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
7906a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
791760ad805SMatthias Ringwald         // delete entry
792760ad805SMatthias Ringwald         log_info("CCC Index %u: Delete", index);
793760ad805SMatthias Ringwald         tlv_impl->delete_tag(tlv_context, tag);
794760ad805SMatthias Ringwald     }
795760ad805SMatthias Ringwald }
796760ad805SMatthias Ringwald 
79701ac2008SMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server){
79801ac2008SMatthias Ringwald     if (!att_server) return;
79901ac2008SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
80001ac2008SMatthias Ringwald     log_info("Restore CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
8011b7acd0dSMatthias Ringwald     // check if bonded
8021b7acd0dSMatthias Ringwald     if (le_device_index < 0) return;
80301ac2008SMatthias Ringwald     // get btstack_tlv
80401ac2008SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
80501ac2008SMatthias Ringwald     void * tlv_context;
80601ac2008SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
80701ac2008SMatthias Ringwald     if (!tlv_impl) return;
80801ac2008SMatthias Ringwald     // get all ccc tag
80901ac2008SMatthias Ringwald     int index;
8106a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
8116afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
81201ac2008SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
8136a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
8146a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
8156a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
81601ac2008SMatthias Ringwald         // simulate write callback
8176a540790SMatthias Ringwald         uint16_t attribute_handle = entry.att_handle;
81801ac2008SMatthias Ringwald         uint8_t  value[2];
8196a540790SMatthias Ringwald         little_endian_store_16(value, 0, entry.value);
82001ac2008SMatthias Ringwald         att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
82101ac2008SMatthias Ringwald         if (!callback) continue;
8226a540790SMatthias Ringwald         log_info("CCC Index %u: Set Attribute handle 0x%04x to value 0x%04x", index, attribute_handle, entry.value );
82301ac2008SMatthias Ringwald         (*callback)(att_server->connection.con_handle, attribute_handle, ATT_TRANSACTION_MODE_NONE, 0, value, sizeof(value));
82401ac2008SMatthias Ringwald     }
82501ac2008SMatthias Ringwald }
82601ac2008SMatthias Ringwald 
827bf78aac6SMatthias Ringwald // persistent CCC writes
828bf78aac6SMatthias Ringwald // ---------------------
8293d71c7a4SMatthias Ringwald 
8303d71c7a4SMatthias Ringwald // gatt service management
8313d71c7a4SMatthias Ringwald static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){
8323d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
8333d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
8343d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
8353d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
8363d71c7a4SMatthias Ringwald         if (handler->start_handle > handle) continue;
8373d71c7a4SMatthias Ringwald         if (handler->end_handle   < handle) continue;
8383d71c7a4SMatthias Ringwald         return handler;
8393d71c7a4SMatthias Ringwald     }
8403d71c7a4SMatthias Ringwald     return NULL;
8413d71c7a4SMatthias Ringwald }
8423d71c7a4SMatthias Ringwald static att_read_callback_t att_server_read_callback_for_handle(uint16_t handle){
8433d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
8443d71c7a4SMatthias Ringwald     if (handler) return handler->read_callback;
8453d71c7a4SMatthias Ringwald     return att_server_client_read_callback;
8463d71c7a4SMatthias Ringwald }
8473d71c7a4SMatthias Ringwald 
8483d71c7a4SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){
8493d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
8503d71c7a4SMatthias Ringwald     if (handler) return handler->write_callback;
8513d71c7a4SMatthias Ringwald     return att_server_client_write_callback;
8523d71c7a4SMatthias Ringwald }
8533d71c7a4SMatthias Ringwald 
854*5d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle){
855*5d07396bSMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
856*5d07396bSMatthias Ringwald     if (handler) return handler->packet_handler;
857*5d07396bSMatthias Ringwald     return att_client_packet_handler;
858*5d07396bSMatthias Ringwald }
859*5d07396bSMatthias Ringwald 
8603d71c7a4SMatthias Ringwald static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){
8613d71c7a4SMatthias Ringwald     // notify all callbacks
8623d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
8633d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
8643d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
8653d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
8663d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
8673d71c7a4SMatthias Ringwald         (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
8683d71c7a4SMatthias Ringwald     }
8693d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return;
8703d71c7a4SMatthias Ringwald     (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
8713d71c7a4SMatthias Ringwald }
8723d71c7a4SMatthias Ringwald 
8733d71c7a4SMatthias Ringwald // returns first reported error or 0
8743d71c7a4SMatthias Ringwald static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){
8753d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
8763d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
8773d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
8783d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
8793d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
8803d71c7a4SMatthias Ringwald         uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
8813d71c7a4SMatthias Ringwald         if (error_code) return error_code;
8823d71c7a4SMatthias Ringwald     }
8833d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return 0;
8843d71c7a4SMatthias Ringwald     return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
8853d71c7a4SMatthias Ringwald }
8863d71c7a4SMatthias Ringwald 
8873d71c7a4SMatthias 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){
8883d71c7a4SMatthias Ringwald     att_read_callback_t callback = att_server_read_callback_for_handle(attribute_handle);
88901ac2008SMatthias Ringwald     if (!callback) return 0;
8903d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, offset, buffer, buffer_size);
8913d71c7a4SMatthias Ringwald }
8923d71c7a4SMatthias Ringwald 
8933d71c7a4SMatthias 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){
8943d71c7a4SMatthias Ringwald     switch (transaction_mode){
8953d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_VALIDATE:
8963d71c7a4SMatthias Ringwald             return att_validate_prepared_write(con_handle);
8973d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_EXECUTE:
8983d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_CANCEL:
8993d71c7a4SMatthias Ringwald             att_notify_write_callbacks(con_handle, transaction_mode);
9003d71c7a4SMatthias Ringwald             return 0;
9013d71c7a4SMatthias Ringwald         default:
9023d71c7a4SMatthias Ringwald             break;
9033d71c7a4SMatthias Ringwald     }
9043d71c7a4SMatthias Ringwald 
905bf78aac6SMatthias Ringwald     // track CCC writes
906bf78aac6SMatthias Ringwald     if (att_is_persistent_ccc(attribute_handle) && offset == 0 && buffer_size == 2){
907bf78aac6SMatthias Ringwald         att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0));
908bf78aac6SMatthias Ringwald     }
909bf78aac6SMatthias Ringwald 
91001ac2008SMatthias Ringwald     att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
91101ac2008SMatthias Ringwald     if (!callback) return 0;
9123d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size);
9133d71c7a4SMatthias Ringwald }
9143d71c7a4SMatthias Ringwald 
9153d71c7a4SMatthias Ringwald /**
9163d71c7a4SMatthias Ringwald  * @brief register read/write callbacks for specific handle range
9173d71c7a4SMatthias Ringwald  * @param att_service_handler_t
9183d71c7a4SMatthias Ringwald  */
9193d71c7a4SMatthias Ringwald void att_server_register_service_handler(att_service_handler_t * handler){
9203d71c7a4SMatthias Ringwald     if (att_service_handler_for_handle(handler->start_handle) ||
9213d71c7a4SMatthias Ringwald         att_service_handler_for_handle(handler->end_handle)){
9223d71c7a4SMatthias Ringwald         log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle);
9233d71c7a4SMatthias Ringwald         return;
9243d71c7a4SMatthias Ringwald     }
9253d71c7a4SMatthias Ringwald     btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler);
9263d71c7a4SMatthias Ringwald }
9273d71c7a4SMatthias Ringwald 
9283deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){
9293deb3ec6SMatthias Ringwald 
9303d71c7a4SMatthias Ringwald     // store callbacks
9313d71c7a4SMatthias Ringwald     att_server_client_read_callback  = read_callback;
9323d71c7a4SMatthias Ringwald     att_server_client_write_callback = write_callback;
9333d71c7a4SMatthias Ringwald 
9341a389cdcSMatthias Ringwald     // register for HCI Events
935d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &att_event_packet_handler;
9361a389cdcSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
9371a389cdcSMatthias Ringwald 
938406722e4SMatthias Ringwald     // register for SM events
939d9a7306aSMatthias Ringwald     sm_event_callback_registration.callback = &att_event_packet_handler;
940406722e4SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
9413deb3ec6SMatthias Ringwald 
9421a389cdcSMatthias Ringwald     // and L2CAP ATT Server PDUs
9433deb3ec6SMatthias Ringwald     att_dispatch_register_server(att_packet_handler);
9443deb3ec6SMatthias Ringwald 
9453deb3ec6SMatthias Ringwald     att_set_db(db);
9463d71c7a4SMatthias Ringwald     att_set_read_callback(att_server_read_callback);
9473d71c7a4SMatthias Ringwald     att_set_write_callback(att_server_write_callback);
9483deb3ec6SMatthias Ringwald 
9493deb3ec6SMatthias Ringwald }
9503deb3ec6SMatthias Ringwald 
9513deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){
9523deb3ec6SMatthias Ringwald     att_client_packet_handler = handler;
9533deb3ec6SMatthias Ringwald }
9543deb3ec6SMatthias Ringwald 
955cbbb12d9SMatthias Ringwald 
956cbbb12d9SMatthias Ringwald // to be deprecated
957c141988cSMatthias Ringwald int  att_server_can_send_packet_now(hci_con_handle_t con_handle){
95818707219SMatthias Ringwald     return att_dispatch_server_can_send_now(con_handle);
9591b96b007SMatthias Ringwald }
9601b96b007SMatthias Ringwald 
961cbbb12d9SMatthias Ringwald int att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
962cbbb12d9SMatthias Ringwald     return att_server_request_to_send_notification(callback_registration, con_handle);
9633deb3ec6SMatthias Ringwald }
9643deb3ec6SMatthias Ringwald 
965cbbb12d9SMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){
966cbbb12d9SMatthias Ringwald     att_client_waiting_for_can_send_registration.callback = &att_emit_can_send_now_event;
967cbbb12d9SMatthias Ringwald     att_server_request_to_send_notification(&att_client_waiting_for_can_send_registration, con_handle);
968cbbb12d9SMatthias Ringwald }
969cbbb12d9SMatthias Ringwald // end of deprecated
970cbbb12d9SMatthias Ringwald 
971cbbb12d9SMatthias Ringwald int att_server_request_to_send_notification(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
972969a5bbaSMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
973969a5bbaSMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
974cbbb12d9SMatthias Ringwald     btstack_linked_list_add_tail(&att_server->notification_requests, (btstack_linked_item_t*) callback_registration);
975cbbb12d9SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
976cbbb12d9SMatthias Ringwald     return ERROR_CODE_SUCCESS;
977cbbb12d9SMatthias Ringwald }
978cbbb12d9SMatthias Ringwald 
979cbbb12d9SMatthias Ringwald int att_server_request_to_send_indication(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
980cbbb12d9SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
981cbbb12d9SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
982cbbb12d9SMatthias Ringwald     btstack_linked_list_add_tail(&att_server->indication_requests, (btstack_linked_item_t*) callback_registration);
983bb38f057SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
984969a5bbaSMatthias Ringwald     return ERROR_CODE_SUCCESS;
985bb38f057SMatthias Ringwald }
986bb38f057SMatthias Ringwald 
987c141988cSMatthias Ringwald int att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){
98818707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
98918707219SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
99018707219SMatthias Ringwald 
99118707219SMatthias Ringwald     if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
9923deb3ec6SMatthias Ringwald 
9933deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
9943deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
99518707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_notification(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
99618707219SMatthias Ringwald 	return l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
9973deb3ec6SMatthias Ringwald }
9983deb3ec6SMatthias Ringwald 
999c141988cSMatthias Ringwald int att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){
100018707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
100118707219SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
100218707219SMatthias Ringwald 
1003eaac31e8SMatthias Ringwald     if (att_server->value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS;
100418707219SMatthias Ringwald     if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
10053deb3ec6SMatthias Ringwald 
10063deb3ec6SMatthias Ringwald     // track indication
100718707219SMatthias Ringwald     att_server->value_indication_handle = attribute_handle;
100818707219SMatthias Ringwald     btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout);
100918707219SMatthias Ringwald     btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS);
101018707219SMatthias Ringwald     btstack_run_loop_add_timer(&att_server->value_indication_timer);
10113deb3ec6SMatthias Ringwald 
10123deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
10133deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
101418707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_indication(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
101518707219SMatthias Ringwald 	l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
10163deb3ec6SMatthias Ringwald     return 0;
10173deb3ec6SMatthias Ringwald }
1018