xref: /btstack/src/ble/att_server.c (revision ff1bec95119e0653623c2fd88159810145fc961f)
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 
38e501bae0SMatthias 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 <string.h>
473deb3ec6SMatthias Ringwald #include <inttypes.h>
483deb3ec6SMatthias Ringwald 
497907f069SMatthias Ringwald #include "btstack_config.h"
503deb3ec6SMatthias Ringwald 
5159c6af15SMatthias Ringwald #include "att_dispatch.h"
5259c6af15SMatthias Ringwald #include "ble/att_db.h"
5359c6af15SMatthias Ringwald #include "ble/att_server.h"
5459c6af15SMatthias Ringwald #include "ble/core.h"
5559c6af15SMatthias Ringwald #include "ble/le_device_db.h"
5659c6af15SMatthias Ringwald #include "ble/sm.h"
5716ece135SMatthias Ringwald #include "btstack_debug.h"
580e2df43fSMatthias Ringwald #include "btstack_event.h"
593deb3ec6SMatthias Ringwald #include "btstack_memory.h"
600e2df43fSMatthias Ringwald #include "btstack_run_loop.h"
6159c6af15SMatthias Ringwald #include "gap.h"
623deb3ec6SMatthias Ringwald #include "hci.h"
633deb3ec6SMatthias Ringwald #include "hci_dump.h"
643deb3ec6SMatthias Ringwald #include "l2cap.h"
65bf78aac6SMatthias Ringwald #include "btstack_tlv.h"
66d1a1f6a4SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
67d1a1f6a4SMatthias Ringwald #include "ble/sm.h"
68d1a1f6a4SMatthias Ringwald #endif
69bf78aac6SMatthias Ringwald 
706afb9acaSMatthias Ringwald #ifndef NVN_NUM_GATT_SERVER_CCC
716afb9acaSMatthias Ringwald #define NVN_NUM_GATT_SERVER_CCC 20
72bf78aac6SMatthias Ringwald #endif
733deb3ec6SMatthias Ringwald 
7418707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server);
7501ac2008SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle);
765d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle);
7701ac2008SMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server);
78760ad805SMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server);
793deb3ec6SMatthias Ringwald 
803551936eSMatthias Ringwald typedef enum {
813551936eSMatthias Ringwald     ATT_SERVER_RUN_PHASE_1_REQUESTS,
823551936eSMatthias Ringwald     ATT_SERVER_RUN_PHASE_2_INDICATIONS,
833551936eSMatthias Ringwald     ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS,
843551936eSMatthias Ringwald } att_server_run_phase_t;
853551936eSMatthias Ringwald 
866a540790SMatthias Ringwald //
876a540790SMatthias Ringwald typedef struct {
886a540790SMatthias Ringwald     uint32_t seq_nr;
896a540790SMatthias Ringwald     uint16_t att_handle;
906a540790SMatthias Ringwald     uint8_t  value;
916a540790SMatthias Ringwald     uint8_t  device_index;
926a540790SMatthias Ringwald } persistent_ccc_entry_t;
936a540790SMatthias Ringwald 
9418707219SMatthias Ringwald // global
951a389cdcSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
96406722e4SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration;
973deb3ec6SMatthias Ringwald static btstack_packet_handler_t               att_client_packet_handler = NULL;
983d71c7a4SMatthias Ringwald static btstack_linked_list_t                  service_handlers;
995f141511SMatthias Ringwald static btstack_context_callback_registration_t att_client_waiting_for_can_send_registration;
10018707219SMatthias Ringwald 
1013d71c7a4SMatthias Ringwald static att_read_callback_t                    att_server_client_read_callback;
1023d71c7a4SMatthias Ringwald static att_write_callback_t                   att_server_client_write_callback;
1033d71c7a4SMatthias Ringwald 
1046438547aSMatthias Ringwald // round robin
1056438547aSMatthias Ringwald static hci_con_handle_t att_server_last_can_send_now = HCI_CON_HANDLE_INVALID;
106bf78aac6SMatthias Ringwald 
10718707219SMatthias Ringwald static att_server_t * att_server_for_handle(hci_con_handle_t con_handle){
10818707219SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
10918707219SMatthias Ringwald     if (!hci_connection) return NULL;
11018707219SMatthias Ringwald     return &hci_connection->att_server;
11118707219SMatthias Ringwald }
11218707219SMatthias Ringwald 
11302b78fc8SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
11418707219SMatthias Ringwald static att_server_t * att_server_for_state(att_server_state_t state){
11518707219SMatthias Ringwald     btstack_linked_list_iterator_t it;
11618707219SMatthias Ringwald     hci_connections_get_iterator(&it);
11718707219SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
11818707219SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
11918707219SMatthias Ringwald         att_server_t * att_server = &connection->att_server;
12018707219SMatthias Ringwald         if (att_server->state == state) return att_server;
12118707219SMatthias Ringwald     }
12218707219SMatthias Ringwald     return NULL;
12318707219SMatthias Ringwald }
12402b78fc8SMatthias Ringwald #endif
125bb38f057SMatthias Ringwald 
1263deb3ec6SMatthias Ringwald static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){
1275d07396bSMatthias Ringwald     btstack_packet_handler_t packet_handler = att_server_packet_handler_for_handle(attribute_handle);
1285d07396bSMatthias Ringwald     if (!packet_handler) return;
1293deb3ec6SMatthias Ringwald 
1303deb3ec6SMatthias Ringwald     uint8_t event[7];
1313deb3ec6SMatthias Ringwald     int pos = 0;
1325611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE;
1333deb3ec6SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
1343deb3ec6SMatthias Ringwald     event[pos++] = status;
135f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, client_handle);
1363deb3ec6SMatthias Ringwald     pos += 2;
137f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, attribute_handle);
138a444112bSMatthias Ringwald     (*packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1393deb3ec6SMatthias Ringwald }
1403deb3ec6SMatthias Ringwald 
1415d07396bSMatthias Ringwald static void att_emit_event_to_all(const uint8_t * event, uint16_t size){
1425d07396bSMatthias Ringwald     // dispatch to app level handler
1435d07396bSMatthias Ringwald     if (att_client_packet_handler){
1445d07396bSMatthias Ringwald         (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size);
1455d07396bSMatthias Ringwald     }
1463deb3ec6SMatthias Ringwald 
1475d07396bSMatthias Ringwald     // dispatch to service handlers
1485d07396bSMatthias Ringwald     btstack_linked_list_iterator_t it;
1495d07396bSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
1505d07396bSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1515d07396bSMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
1525d07396bSMatthias Ringwald         if (!handler->packet_handler) continue;
1535d07396bSMatthias Ringwald         (*handler->packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size);
1545d07396bSMatthias Ringwald     }
1555d07396bSMatthias Ringwald }
1565d07396bSMatthias Ringwald 
1575d07396bSMatthias Ringwald static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){
1583deb3ec6SMatthias Ringwald     uint8_t event[6];
1593deb3ec6SMatthias Ringwald     int pos = 0;
1605611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE;
1613deb3ec6SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
162fc64f94aSMatthias Ringwald     little_endian_store_16(event, pos, con_handle);
1633deb3ec6SMatthias Ringwald     pos += 2;
164f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, mtu);
1655d07396bSMatthias Ringwald 
1665d07396bSMatthias Ringwald     // also dispatch to GATT Clients
167b12646c5SJakob Krantz     att_dispatch_server_mtu_exchanged(con_handle, mtu);
1685d07396bSMatthias Ringwald 
1695d07396bSMatthias Ringwald     // dispatch to app level handler and service handlers
1705d07396bSMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
1713deb3ec6SMatthias Ringwald }
1723deb3ec6SMatthias Ringwald 
1735f141511SMatthias Ringwald static void att_emit_can_send_now_event(void * context){
1745f141511SMatthias Ringwald     UNUSED(context);
1753c97b242SMatthias Ringwald     if (!att_client_packet_handler) return;
1763c97b242SMatthias Ringwald 
1771b96b007SMatthias Ringwald     uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0};
1781b96b007SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1791b96b007SMatthias Ringwald }
1801b96b007SMatthias Ringwald 
181*ff1bec95SMatthias Ringwald static void att_emit_connected_event(att_server_t * att_server){
182*ff1bec95SMatthias Ringwald     uint8_t event[11];
183*ff1bec95SMatthias Ringwald     int pos = 0;
184*ff1bec95SMatthias Ringwald     event[pos++] = ATT_EVENT_CONNECTED;
185*ff1bec95SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
186*ff1bec95SMatthias Ringwald     event[pos++] = att_server->peer_addr_type;
187*ff1bec95SMatthias Ringwald     reverse_bd_addr(att_server->peer_address, &event[pos]);
188*ff1bec95SMatthias Ringwald     pos += 6;
189*ff1bec95SMatthias Ringwald     little_endian_store_16(event, pos, att_server->connection.con_handle);
190*ff1bec95SMatthias Ringwald     pos += 2;
191*ff1bec95SMatthias Ringwald 
192*ff1bec95SMatthias Ringwald     // dispatch to app level handler and service handlers
193*ff1bec95SMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
194*ff1bec95SMatthias Ringwald }
195*ff1bec95SMatthias Ringwald 
196*ff1bec95SMatthias Ringwald 
197*ff1bec95SMatthias Ringwald static void att_emit_disconnected_event(att_server_t * att_server){
198*ff1bec95SMatthias Ringwald     uint8_t event[4];
199*ff1bec95SMatthias Ringwald     int pos = 0;
200*ff1bec95SMatthias Ringwald     event[pos++] = ATT_EVENT_DISCONNECTED;
201*ff1bec95SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
202*ff1bec95SMatthias Ringwald     little_endian_store_16(event, pos, att_server->connection.con_handle);
203*ff1bec95SMatthias Ringwald     pos += 2;
204*ff1bec95SMatthias Ringwald 
205*ff1bec95SMatthias Ringwald     // dispatch to app level handler and service handlers
206*ff1bec95SMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
207*ff1bec95SMatthias Ringwald }
208*ff1bec95SMatthias Ringwald 
209ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){
21054178543SMatthias Ringwald     void * context = btstack_run_loop_get_timer_context(ts);
21154178543SMatthias Ringwald     hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
21218707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
21318707219SMatthias Ringwald     if (!att_server) return;
214bce48a26SMatthias Ringwald     // @note: after a transcation timeout, no more requests shall be sent over this ATT Bearer
215bce48a26SMatthias Ringwald     // (that's why we don't reset the value_indication_handle)
21618707219SMatthias Ringwald     uint16_t att_handle = att_server->value_indication_handle;
21718707219SMatthias Ringwald     att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_server->connection.con_handle, att_handle);
2183deb3ec6SMatthias Ringwald }
2193deb3ec6SMatthias Ringwald 
2203deb3ec6SMatthias Ringwald static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2219ec2630cSMatthias Ringwald 
2227dad9ff8SMatthias Ringwald     UNUSED(channel); // ok: there is no channel
2237dad9ff8SMatthias Ringwald     UNUSED(size);    // ok: handling own l2cap events
2243deb3ec6SMatthias Ringwald 
22518707219SMatthias Ringwald     att_server_t * att_server;
22618707219SMatthias Ringwald     hci_con_handle_t con_handle;
22718707219SMatthias Ringwald 
2283deb3ec6SMatthias Ringwald     switch (packet_type) {
2293deb3ec6SMatthias Ringwald 
2303deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
2310e2df43fSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
2323deb3ec6SMatthias Ringwald 
2333deb3ec6SMatthias Ringwald                 case HCI_EVENT_LE_META:
2343deb3ec6SMatthias Ringwald                     switch (packet[2]) {
2353deb3ec6SMatthias Ringwald                         case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
23618707219SMatthias Ringwald                             con_handle = little_endian_read_16(packet, 4);
23718707219SMatthias Ringwald                             att_server = att_server_for_handle(con_handle);
23818707219SMatthias Ringwald                             if (!att_server) break;
2393deb3ec6SMatthias Ringwald                         	// store connection info
24018707219SMatthias Ringwald                         	att_server->peer_addr_type = packet[7];
24118707219SMatthias Ringwald                             reverse_bd_addr(&packet[8], att_server->peer_address);
24218707219SMatthias Ringwald                             att_server->connection.con_handle = con_handle;
2433deb3ec6SMatthias Ringwald                             // reset connection properties
24418707219SMatthias Ringwald                             att_server->state = ATT_SERVER_IDLE;
24518707219SMatthias Ringwald                             att_server->connection.mtu = ATT_DEFAULT_MTU;
24618707219SMatthias Ringwald                             att_server->connection.max_mtu = l2cap_max_le_mtu();
24718707219SMatthias Ringwald                             if (att_server->connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){
24818707219SMatthias Ringwald                                 att_server->connection.max_mtu = ATT_REQUEST_BUFFER_SIZE;
24999c58ad0SMatthias Ringwald                             }
25018707219SMatthias Ringwald                             att_server->connection.encryption_key_size = 0;
25118707219SMatthias Ringwald                             att_server->connection.authenticated = 0;
25218707219SMatthias Ringwald 		                	att_server->connection.authorized = 0;
253b2c1e52cSMatthias Ringwald                             // workaround: identity resolving can already be complete, at least store result
254b2c1e52cSMatthias Ringwald                             att_server->ir_le_device_db_index = sm_le_device_index(con_handle);
255760ad805SMatthias Ringwald                             att_server->pairing_active = 0;
256*ff1bec95SMatthias Ringwald                             // notify all - old
2575d07396bSMatthias Ringwald                             att_emit_event_to_all(packet, size);
258*ff1bec95SMatthias Ringwald                             // notify all - new
259*ff1bec95SMatthias Ringwald                             att_emit_connected_event(att_server);
2603deb3ec6SMatthias Ringwald                             break;
2613deb3ec6SMatthias Ringwald 
2623deb3ec6SMatthias Ringwald                         default:
2633deb3ec6SMatthias Ringwald                             break;
2643deb3ec6SMatthias Ringwald                     }
2653deb3ec6SMatthias Ringwald                     break;
2663deb3ec6SMatthias Ringwald 
2673deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_CHANGE:
2683deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE:
2693deb3ec6SMatthias Ringwald                 	// check handle
27018707219SMatthias Ringwald                     con_handle = little_endian_read_16(packet, 3);
27118707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
27218707219SMatthias Ringwald                     if (!att_server) break;
2739c6e867eSMatthias Ringwald                     att_server->connection.encryption_key_size = gap_encryption_key_size(con_handle);
2749c6e867eSMatthias Ringwald                     att_server->connection.authenticated = gap_authenticated(con_handle);
27513eb7322SMatthias Ringwald                     att_server->connection.secure_connection = gap_secure_connection(con_handle);
27613eb7322SMatthias Ringwald                     log_info("encrypted key size %u, authenticated %u, secure connectipon %u",
27713eb7322SMatthias Ringwald                         att_server->connection.encryption_key_size, att_server->connection.authenticated, att_server->connection.secure_connection);
27801ac2008SMatthias Ringwald                     if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){
27901ac2008SMatthias Ringwald                         // restore CCC values when encrypted
28001ac2008SMatthias Ringwald                         if (hci_event_encryption_change_get_encryption_enabled(packet)){
28101ac2008SMatthias Ringwald                             att_server_persistent_ccc_restore(att_server);
28201ac2008SMatthias Ringwald                         }
28301ac2008SMatthias Ringwald                     }
2840234fbbcSMatthias Ringwald                     att_run_for_context(att_server);
2853deb3ec6SMatthias Ringwald                     break;
2863deb3ec6SMatthias Ringwald 
2873deb3ec6SMatthias Ringwald                 case HCI_EVENT_DISCONNECTION_COMPLETE:
28870a390c7SMatthias Ringwald                     // check handle
28918707219SMatthias Ringwald                     con_handle = hci_event_disconnection_complete_get_connection_handle(packet);
29018707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
29118707219SMatthias Ringwald                     if (!att_server) break;
29218707219SMatthias Ringwald                     att_clear_transaction_queue(&att_server->connection);
29318707219SMatthias Ringwald                     att_server->connection.con_handle = 0;
294760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
29518707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
296bce48a26SMatthias Ringwald                     if (att_server->value_indication_handle){
29727328ecbSMatthias Ringwald                         btstack_run_loop_remove_timer(&att_server->value_indication_timer);
298bce48a26SMatthias Ringwald                         uint16_t att_handle = att_server->value_indication_handle;
299bce48a26SMatthias Ringwald                         att_server->value_indication_handle = 0; // reset error state
300bce48a26SMatthias Ringwald                         att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_DISCONNECT, att_server->connection.con_handle, att_handle);
301bce48a26SMatthias Ringwald                     }
302*ff1bec95SMatthias Ringwald                     // notify all - new
303*ff1bec95SMatthias Ringwald                     att_emit_disconnected_event(att_server);
304*ff1bec95SMatthias Ringwald                     // notify all - old
3055d07396bSMatthias Ringwald                     att_emit_event_to_all(packet, size);
3063deb3ec6SMatthias Ringwald                     break;
3073deb3ec6SMatthias Ringwald 
308760ad805SMatthias Ringwald                 // Identity Resolving
3095611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_STARTED:
31018707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_started_get_handle(packet);
31118707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
31218707219SMatthias Ringwald                     if (!att_server) break;
3135611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED");
31418707219SMatthias Ringwald                     att_server->ir_lookup_active = 1;
3153deb3ec6SMatthias Ringwald                     break;
3165611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
317760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
318760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
319760ad805SMatthias Ringwald                     if (!att_server) return;
3201b7acd0dSMatthias Ringwald                     att_server->ir_lookup_active = 0;
3211b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index(packet);
3221b7acd0dSMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED");
3231b7acd0dSMatthias Ringwald                     att_run_for_context(att_server);
3243deb3ec6SMatthias Ringwald                     break;
3255611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_FAILED:
32618707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_failed_get_handle(packet);
32718707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
32818707219SMatthias Ringwald                     if (!att_server) break;
3295611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED");
33018707219SMatthias Ringwald                     att_server->ir_lookup_active = 0;
33118707219SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
33218707219SMatthias Ringwald                     att_run_for_context(att_server);
3333deb3ec6SMatthias Ringwald                     break;
334760ad805SMatthias Ringwald 
335760ad805SMatthias Ringwald                 // Pairing started - delete stored CCC values
336760ad805SMatthias Ringwald                 // - assumes pairing indicates either new device or re-pairing, in both cases there should be no stored CCC values
337760ad805SMatthias Ringwald                 // - assumes that all events have the con handle as the first field
338760ad805SMatthias Ringwald                 case SM_EVENT_JUST_WORKS_REQUEST:
339760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
340760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_INPUT_NUMBER:
341760ad805SMatthias Ringwald                 case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
342760ad805SMatthias Ringwald                     con_handle = sm_event_just_works_request_get_handle(packet);
343760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
344760ad805SMatthias Ringwald                     if (!att_server) break;
345760ad805SMatthias Ringwald                     att_server->pairing_active = 1;
346760ad805SMatthias Ringwald                     log_info("SM Pairing started");
347760ad805SMatthias Ringwald                     if (att_server->ir_le_device_db_index < 0) break;
348760ad805SMatthias Ringwald                     att_server_persistent_ccc_clear(att_server);
349760ad805SMatthias Ringwald                     // index not valid anymore
350760ad805SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
351760ad805SMatthias Ringwald                     break;
352760ad805SMatthias Ringwald 
3531b7acd0dSMatthias Ringwald                 // Bonding completed
354760ad805SMatthias Ringwald                 case SM_EVENT_IDENTITY_CREATED:
355760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
356760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
357760ad805SMatthias Ringwald                     if (!att_server) return;
358760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
3591b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_created_get_index(packet);
3601b7acd0dSMatthias Ringwald                     att_run_for_context(att_server);
3611b7acd0dSMatthias Ringwald                     break;
3621b7acd0dSMatthias Ringwald 
3631b7acd0dSMatthias Ringwald                 // Pairing complete (with/without bonding=storing of pairing information)
3641b7acd0dSMatthias Ringwald                 case SM_EVENT_PAIRING_COMPLETE:
3651b7acd0dSMatthias Ringwald                     con_handle = sm_event_pairing_complete_get_handle(packet);
3661b7acd0dSMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
3671b7acd0dSMatthias Ringwald                     if (!att_server) return;
3681b7acd0dSMatthias Ringwald                     att_server->pairing_active = 0;
3691b7acd0dSMatthias Ringwald                     att_run_for_context(att_server);
370760ad805SMatthias Ringwald                     break;
371760ad805SMatthias Ringwald 
372760ad805SMatthias Ringwald                 // Authorization
3735611a760SMatthias Ringwald                 case SM_EVENT_AUTHORIZATION_RESULT: {
37418707219SMatthias Ringwald                     con_handle = sm_event_authorization_result_get_handle(packet);
37518707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
37618707219SMatthias Ringwald                     if (!att_server) break;
37718707219SMatthias Ringwald                     att_server->connection.authorized = sm_event_authorization_result_get_authorization_result(packet);
37818707219SMatthias Ringwald                     att_dispatch_server_request_can_send_now_event(con_handle);
3793deb3ec6SMatthias Ringwald                 	break;
3803deb3ec6SMatthias Ringwald                 }
3813deb3ec6SMatthias Ringwald                 default:
3823deb3ec6SMatthias Ringwald                     break;
3833deb3ec6SMatthias Ringwald             }
38465b44ffdSMatthias Ringwald             break;
38565b44ffdSMatthias Ringwald         default:
38665b44ffdSMatthias Ringwald             break;
3873deb3ec6SMatthias Ringwald     }
3883deb3ec6SMatthias Ringwald }
3893deb3ec6SMatthias Ringwald 
3907a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
3913deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
3923deb3ec6SMatthias Ringwald 
39318707219SMatthias Ringwald     att_server_t * att_server = att_server_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION);
39418707219SMatthias Ringwald     if (!att_server) return;
3953deb3ec6SMatthias Ringwald 
3963deb3ec6SMatthias Ringwald     uint8_t hash_flipped[8];
3979c80e4ccSMatthias Ringwald     reverse_64(hash, hash_flipped);
39818707219SMatthias Ringwald     if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8)){
3993deb3ec6SMatthias Ringwald         log_info("ATT Signed Write, invalid signature");
40018707219SMatthias Ringwald         att_server->state = ATT_SERVER_IDLE;
4013deb3ec6SMatthias Ringwald         return;
4023deb3ec6SMatthias Ringwald     }
4033deb3ec6SMatthias Ringwald     log_info("ATT Signed Write, valid signature");
4043deb3ec6SMatthias Ringwald 
4053deb3ec6SMatthias Ringwald     // update sequence number
40618707219SMatthias Ringwald     uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
40718707219SMatthias Ringwald     le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1);
40818707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
40918707219SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
4103deb3ec6SMatthias Ringwald }
4117a766ebfSMatthias Ringwald #endif
41288a48dd8SMatthias Ringwald 
41318707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED
414b06f2579SMatthias Ringwald // pre: can send now
415b06f2579SMatthias Ringwald // returns: 1 if packet was sent
41618707219SMatthias Ringwald static int att_server_process_validated_request(att_server_t * att_server){
417b06f2579SMatthias Ringwald 
418b06f2579SMatthias Ringwald     l2cap_reserve_packet_buffer();
419b06f2579SMatthias Ringwald     uint8_t * att_response_buffer = l2cap_get_outgoing_buffer();
42018707219SMatthias Ringwald     uint16_t  att_response_size   = att_handle_request(&att_server->connection, att_server->request_buffer, att_server->request_size, att_response_buffer);
421b06f2579SMatthias Ringwald 
422beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
42356c3a347SMatthias Ringwald     if (att_response_size == ATT_READ_RESPONSE_PENDING || att_response_size == ATT_INTERNAL_WRITE_RESPONSE_PENDING){
424e404a688SMatthias Ringwald         // update state
425beb20288SMatthias Ringwald         att_server->state = ATT_SERVER_RESPONSE_PENDING;
426e404a688SMatthias Ringwald 
42756c3a347SMatthias Ringwald         // callback with handle ATT_READ_RESPONSE_PENDING for reads
42856c3a347SMatthias Ringwald         if (att_response_size == ATT_READ_RESPONSE_PENDING){
429e404a688SMatthias Ringwald             att_server_client_read_callback(att_server->connection.con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0);
43056c3a347SMatthias Ringwald         }
431e8e7403eSMatthias Ringwald 
432e8e7403eSMatthias Ringwald         // free reserved buffer
433e8e7403eSMatthias Ringwald         l2cap_release_packet_buffer();
434e8e7403eSMatthias Ringwald         return 0;
435e404a688SMatthias Ringwald     }
436e404a688SMatthias Ringwald #endif
437e404a688SMatthias Ringwald 
438b06f2579SMatthias Ringwald     // intercept "insufficient authorization" for authenticated connections to allow for user authorization
439b06f2579SMatthias Ringwald     if ((att_response_size     >= 4)
440b06f2579SMatthias Ringwald     && (att_response_buffer[0] == ATT_ERROR_RESPONSE)
441b06f2579SMatthias Ringwald     && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION)
44218707219SMatthias Ringwald     && (att_server->connection.authenticated)){
443b06f2579SMatthias Ringwald 
4449c6e867eSMatthias Ringwald         switch (gap_authorization_state(att_server->connection.con_handle)){
445b06f2579SMatthias Ringwald             case AUTHORIZATION_UNKNOWN:
446b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
44718707219SMatthias Ringwald                 sm_request_pairing(att_server->connection.con_handle);
448b06f2579SMatthias Ringwald                 return 0;
449b06f2579SMatthias Ringwald             case AUTHORIZATION_PENDING:
450b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
451b06f2579SMatthias Ringwald                 return 0;
452b06f2579SMatthias Ringwald             default:
453b06f2579SMatthias Ringwald                 break;
454b06f2579SMatthias Ringwald         }
455b06f2579SMatthias Ringwald     }
456b06f2579SMatthias Ringwald 
45718707219SMatthias Ringwald     att_server->state = ATT_SERVER_IDLE;
458b06f2579SMatthias Ringwald     if (att_response_size == 0) {
459b06f2579SMatthias Ringwald         l2cap_release_packet_buffer();
460b06f2579SMatthias Ringwald         return 0;
461b06f2579SMatthias Ringwald     }
462b06f2579SMatthias Ringwald 
46318707219SMatthias Ringwald     l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size);
464b06f2579SMatthias Ringwald 
465b06f2579SMatthias Ringwald     // notify client about MTU exchange result
466b06f2579SMatthias Ringwald     if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){
46718707219SMatthias Ringwald         att_emit_mtu_event(att_server->connection.con_handle, att_server->connection.mtu);
468b06f2579SMatthias Ringwald     }
469b06f2579SMatthias Ringwald     return 1;
470b06f2579SMatthias Ringwald }
471b06f2579SMatthias Ringwald 
472beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
473beb20288SMatthias Ringwald int att_server_response_ready(hci_con_handle_t con_handle){
474e404a688SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
475e404a688SMatthias Ringwald     if (!att_server)                                        return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
476beb20288SMatthias Ringwald     if (att_server->state != ATT_SERVER_RESPONSE_PENDING)   return ERROR_CODE_COMMAND_DISALLOWED;
477e404a688SMatthias Ringwald 
478e404a688SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
479e404a688SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
480e404a688SMatthias Ringwald     return ERROR_CODE_SUCCESS;
481e404a688SMatthias Ringwald }
482e404a688SMatthias Ringwald #endif
483e404a688SMatthias Ringwald 
48418707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server){
48518707219SMatthias Ringwald     switch (att_server->state){
4863deb3ec6SMatthias Ringwald         case ATT_SERVER_REQUEST_RECEIVED:
487760ad805SMatthias Ringwald 
4880234fbbcSMatthias Ringwald             // wait until re-encryption as central is complete
4890234fbbcSMatthias Ringwald             if (gap_reconnect_security_setup_active(att_server->connection.con_handle)) break;
4900234fbbcSMatthias Ringwald 
491760ad805SMatthias Ringwald             // wait until pairing is complete
492760ad805SMatthias Ringwald             if (att_server->pairing_active) break;
493760ad805SMatthias Ringwald 
4947a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
49518707219SMatthias Ringwald             if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){
4963deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write!");
4973deb3ec6SMatthias Ringwald                 if (!sm_cmac_ready()) {
4983deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, sm_cmac engine not ready. Abort");
49918707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
5003deb3ec6SMatthias Ringwald                     return;
5013deb3ec6SMatthias Ringwald                 }
50218707219SMatthias Ringwald                 if (att_server->request_size < (3 + 12)) {
5033deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, request to short. Abort.");
50418707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
5053deb3ec6SMatthias Ringwald                     return;
5063deb3ec6SMatthias Ringwald                 }
50718707219SMatthias Ringwald                 if (att_server->ir_lookup_active){
5083deb3ec6SMatthias Ringwald                     return;
5093deb3ec6SMatthias Ringwald                 }
51018707219SMatthias Ringwald                 if (att_server->ir_le_device_db_index < 0){
5113deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, CSRK not available");
51218707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
5133deb3ec6SMatthias Ringwald                     return;
5143deb3ec6SMatthias Ringwald                 }
5153deb3ec6SMatthias Ringwald 
5163deb3ec6SMatthias Ringwald                 // check counter
51718707219SMatthias Ringwald                 uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
51818707219SMatthias Ringwald                 uint32_t counter_db     = le_device_db_remote_counter_get(att_server->ir_le_device_db_index);
5193deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet);
5203deb3ec6SMatthias Ringwald                 if (counter_packet < counter_db){
5213deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, db reports higher counter, abort");
52218707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
5233deb3ec6SMatthias Ringwald                     return;
5243deb3ec6SMatthias Ringwald                 }
5253deb3ec6SMatthias Ringwald 
5263deb3ec6SMatthias Ringwald                 // signature is { sequence counter, secure hash }
5273deb3ec6SMatthias Ringwald                 sm_key_t csrk;
52818707219SMatthias Ringwald                 le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk);
52918707219SMatthias Ringwald                 att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION;
5303deb3ec6SMatthias Ringwald                 log_info("Orig Signature: ");
53118707219SMatthias Ringwald                 log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8);
53218707219SMatthias Ringwald                 uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1);
53318707219SMatthias 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);
5343deb3ec6SMatthias Ringwald                 return;
5353deb3ec6SMatthias Ringwald             }
5367a766ebfSMatthias Ringwald #endif
537b06f2579SMatthias Ringwald             // move on
53818707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
53918707219SMatthias Ringwald             att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
540b06f2579SMatthias Ringwald             break;
54188a48dd8SMatthias Ringwald 
5423deb3ec6SMatthias Ringwald         default:
5433deb3ec6SMatthias Ringwald             break;
5443deb3ec6SMatthias Ringwald     }
5453deb3ec6SMatthias Ringwald }
5463deb3ec6SMatthias Ringwald 
54790f03c80SMatthias Ringwald static int att_server_data_ready_for_phase(att_server_t * att_server,  att_server_run_phase_t phase){
54890f03c80SMatthias Ringwald     switch (phase){
54990f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
55090f03c80SMatthias Ringwald             return att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
55190f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
55290f03c80SMatthias Ringwald              return (!btstack_linked_list_empty(&att_server->indication_requests) && att_server->value_indication_handle == 0);
55390f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
55490f03c80SMatthias Ringwald             return (!btstack_linked_list_empty(&att_server->notification_requests));
55590f03c80SMatthias Ringwald     }
556fa5e3464SMatthias Ringwald     // avoid warning
557fa5e3464SMatthias Ringwald     return 0;
55890f03c80SMatthias Ringwald }
55990f03c80SMatthias Ringwald 
56090f03c80SMatthias Ringwald static void att_server_trigger_send_for_phase(att_server_t * att_server,  att_server_run_phase_t phase){
56190f03c80SMatthias Ringwald     btstack_context_callback_registration_t * client;
56290f03c80SMatthias Ringwald     switch (phase){
56390f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
56490f03c80SMatthias Ringwald             att_server_process_validated_request(att_server);
56590f03c80SMatthias Ringwald             break;
56690f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
56790f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->indication_requests;
56890f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->indication_requests, (btstack_linked_item_t *) client);
56990f03c80SMatthias Ringwald             client->callback(client->context);
57090f03c80SMatthias Ringwald             break;
57190f03c80SMatthias Ringwald        case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
57290f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->notification_requests;
57390f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->notification_requests, (btstack_linked_item_t *) client);
57490f03c80SMatthias Ringwald             client->callback(client->context);
57590f03c80SMatthias Ringwald             break;
57690f03c80SMatthias Ringwald     }
57790f03c80SMatthias Ringwald }
57890f03c80SMatthias Ringwald 
5797eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){
580b06f2579SMatthias Ringwald 
581374a288aSMatthias Ringwald     hci_con_handle_t request_con_handle   = HCI_CON_HANDLE_INVALID;
5826438547aSMatthias Ringwald     hci_con_handle_t last_send_con_handle = HCI_CON_HANDLE_INVALID;
5836438547aSMatthias Ringwald     int can_send_now = 1;
5844395a000SMatthias Ringwald     int phase_index;
5856438547aSMatthias Ringwald 
5864395a000SMatthias Ringwald     for (phase_index = ATT_SERVER_RUN_PHASE_1_REQUESTS; phase_index <= ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS; phase_index++){
5874395a000SMatthias Ringwald         att_server_run_phase_t phase = (att_server_run_phase_t) phase_index;
5886438547aSMatthias Ringwald         hci_con_handle_t skip_connections_until = att_server_last_can_send_now;
589374a288aSMatthias Ringwald         while (1){
5906438547aSMatthias Ringwald             btstack_linked_list_iterator_t it;
59118707219SMatthias Ringwald             hci_connections_get_iterator(&it);
59218707219SMatthias Ringwald             while(btstack_linked_list_iterator_has_next(&it)){
59318707219SMatthias Ringwald                 hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
59418707219SMatthias Ringwald                 att_server_t * att_server = &connection->att_server;
5956438547aSMatthias Ringwald 
59690f03c80SMatthias Ringwald                 int data_ready = att_server_data_ready_for_phase(att_server, phase);
5976438547aSMatthias Ringwald 
5986438547aSMatthias 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);
5996438547aSMatthias Ringwald 
6006438547aSMatthias Ringwald                 // skip until last sender found (which is also skipped)
6016438547aSMatthias Ringwald                 if (skip_connections_until != HCI_CON_HANDLE_INVALID){
6026438547aSMatthias Ringwald                     if (data_ready && request_con_handle == HCI_CON_HANDLE_INVALID){
6036438547aSMatthias Ringwald                         request_con_handle = att_server->connection.con_handle;
6046438547aSMatthias Ringwald                     }
6056438547aSMatthias Ringwald                     if (skip_connections_until == att_server->connection.con_handle){
6066438547aSMatthias Ringwald                         skip_connections_until = HCI_CON_HANDLE_INVALID;
6076438547aSMatthias Ringwald                     }
6086438547aSMatthias Ringwald                     continue;
6096438547aSMatthias Ringwald                 };
6106438547aSMatthias Ringwald 
61190f03c80SMatthias Ringwald                 if (data_ready){
612969a5bbaSMatthias Ringwald                     if (can_send_now){
61390f03c80SMatthias Ringwald                         att_server_trigger_send_for_phase(att_server, phase);
6146438547aSMatthias Ringwald                         last_send_con_handle = att_server->connection.con_handle;
615969a5bbaSMatthias Ringwald                         can_send_now = att_dispatch_server_can_send_now(att_server->connection.con_handle);
61690f03c80SMatthias Ringwald                         data_ready = att_server_data_ready_for_phase(att_server, phase);
6176438547aSMatthias Ringwald                         if (data_ready && request_con_handle == HCI_CON_HANDLE_INVALID){
618cbbb12d9SMatthias Ringwald                             request_con_handle = att_server->connection.con_handle;
619cbbb12d9SMatthias Ringwald                         }
620cbbb12d9SMatthias Ringwald                     } else {
621f29a88d8SMatthias Ringwald                         request_con_handle = att_server->connection.con_handle;
622f29a88d8SMatthias Ringwald                         break;
623cbbb12d9SMatthias Ringwald                     }
624cbbb12d9SMatthias Ringwald                 }
6253deb3ec6SMatthias Ringwald             }
6263deb3ec6SMatthias Ringwald 
6276438547aSMatthias Ringwald             // stop skipping (handles disconnect by last send connection)
6286438547aSMatthias Ringwald             skip_connections_until = HCI_CON_HANDLE_INVALID;
6296438547aSMatthias Ringwald 
6303551936eSMatthias Ringwald             // Exit loop, if we cannot send
631969a5bbaSMatthias Ringwald             if (!can_send_now) break;
632969a5bbaSMatthias Ringwald 
633969a5bbaSMatthias Ringwald             // Exit loop, if we can send but there are also no further request
634969a5bbaSMatthias Ringwald             if (request_con_handle == HCI_CON_HANDLE_INVALID) break;
635969a5bbaSMatthias Ringwald 
636969a5bbaSMatthias Ringwald             // Finally, if we still can send and there are requests, just try again
637969a5bbaSMatthias Ringwald             request_con_handle = HCI_CON_HANDLE_INVALID;
638969a5bbaSMatthias Ringwald         }
6396438547aSMatthias Ringwald         // update last send con handle for round robin
6406438547aSMatthias Ringwald         if (last_send_con_handle != HCI_CON_HANDLE_INVALID){
6416438547aSMatthias Ringwald             att_server_last_can_send_now = last_send_con_handle;
6426438547aSMatthias Ringwald         }
6433551936eSMatthias Ringwald     }
644969a5bbaSMatthias Ringwald 
645969a5bbaSMatthias Ringwald     if (request_con_handle == HCI_CON_HANDLE_INVALID) return;
646969a5bbaSMatthias Ringwald     att_dispatch_server_request_can_send_now_event(request_con_handle);
647969a5bbaSMatthias Ringwald }
648969a5bbaSMatthias Ringwald 
64988a48dd8SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
65018707219SMatthias Ringwald     att_server_t * att_server;
65118707219SMatthias Ringwald 
65288a48dd8SMatthias Ringwald     switch (packet_type){
65388a48dd8SMatthias Ringwald         case HCI_EVENT_PACKET:
654bd87a16eSMatthias Ringwald             switch (packet[0]){
655bd87a16eSMatthias Ringwald                 case L2CAP_EVENT_CAN_SEND_NOW:
6564d2be802SMatthias Ringwald                     att_server_handle_can_send_now();
657372d62c4SMatthias Ringwald                     break;
658bd87a16eSMatthias Ringwald                 case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
659bd87a16eSMatthias Ringwald                     // GATT client has negotiated the mtu for this connection
6606155bae0SMatthias Ringwald                     att_server = att_server_for_handle(handle);
6616155bae0SMatthias Ringwald                     if (!att_server) break;
662bd87a16eSMatthias Ringwald                     att_server->connection.mtu = little_endian_read_16(packet, 4);
663bd87a16eSMatthias Ringwald                     break;
664bd87a16eSMatthias Ringwald                 default:
665bd87a16eSMatthias Ringwald                     break;
666bd87a16eSMatthias Ringwald             }
667bd87a16eSMatthias Ringwald             break;
6683deb3ec6SMatthias Ringwald 
669372d62c4SMatthias Ringwald         case ATT_DATA_PACKET:
670f50ef7ddSMatthias Ringwald             log_debug("ATT Packet, handle 0x%04x", handle);
67118707219SMatthias Ringwald             att_server = att_server_for_handle(handle);
67218707219SMatthias Ringwald             if (!att_server) break;
67318707219SMatthias Ringwald 
6743deb3ec6SMatthias Ringwald             // handle value indication confirms
67518707219SMatthias Ringwald             if (packet[0] == ATT_HANDLE_VALUE_CONFIRMATION && att_server->value_indication_handle){
67618707219SMatthias Ringwald                 btstack_run_loop_remove_timer(&att_server->value_indication_timer);
67718707219SMatthias Ringwald                 uint16_t att_handle = att_server->value_indication_handle;
67818707219SMatthias Ringwald                 att_server->value_indication_handle = 0;
67918707219SMatthias Ringwald                 att_handle_value_indication_notify_client(0, att_server->connection.con_handle, att_handle);
680cbbb12d9SMatthias Ringwald                 att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
6813deb3ec6SMatthias Ringwald                 return;
6823deb3ec6SMatthias Ringwald             }
6833deb3ec6SMatthias Ringwald 
6846428eb5aSMatthias Ringwald             // directly process command
6856428eb5aSMatthias Ringwald             // note: signed write cannot be handled directly as authentication needs to be verified
6866428eb5aSMatthias Ringwald             if (packet[0] == ATT_WRITE_COMMAND){
68718707219SMatthias Ringwald                 att_handle_request(&att_server->connection, packet, size, 0);
6886428eb5aSMatthias Ringwald                 return;
6896428eb5aSMatthias Ringwald             }
6906428eb5aSMatthias Ringwald 
6913deb3ec6SMatthias Ringwald             // check size
69218707219SMatthias Ringwald             if (size > sizeof(att_server->request_buffer)) {
693e0463bdcSMatthias Ringwald                 log_info("drop att pdu 0x%02x as size %u > att_server->request_buffer %u", packet[0], size, (int) sizeof(att_server->request_buffer));
6946428eb5aSMatthias Ringwald                 return;
6956428eb5aSMatthias Ringwald             }
6963deb3ec6SMatthias Ringwald 
697e0463bdcSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
698e0463bdcSMatthias Ringwald             // abort signed write validation if a new request comes in (but finish previous signed write if possible)
699e0463bdcSMatthias Ringwald             if (att_server->state == ATT_SERVER_W4_SIGNED_WRITE_VALIDATION){
700e0463bdcSMatthias Ringwald                 if (packet[0] == ATT_SIGNED_WRITE_COMMAND){
701e0463bdcSMatthias Ringwald                     log_info("skip new signed write request as previous is in validation");
702e0463bdcSMatthias Ringwald                     return;
703e0463bdcSMatthias Ringwald                 } else {
704e0463bdcSMatthias Ringwald                     log_info("abort signed write validation to process new request");
705e0463bdcSMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
706e0463bdcSMatthias Ringwald                 }
707e0463bdcSMatthias Ringwald             }
708e0463bdcSMatthias Ringwald #endif
7093deb3ec6SMatthias Ringwald             // last request still in processing?
71018707219SMatthias Ringwald             if (att_server->state != ATT_SERVER_IDLE){
711e0463bdcSMatthias Ringwald                 log_info("skip att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state);
7126428eb5aSMatthias Ringwald                 return;
7136428eb5aSMatthias Ringwald             }
7143deb3ec6SMatthias Ringwald 
7153deb3ec6SMatthias Ringwald             // store request
71618707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED;
71718707219SMatthias Ringwald             att_server->request_size = size;
71818707219SMatthias Ringwald             memcpy(att_server->request_buffer, packet, size);
7193deb3ec6SMatthias Ringwald 
72018707219SMatthias Ringwald             att_run_for_context(att_server);
721372d62c4SMatthias Ringwald             break;
722372d62c4SMatthias Ringwald     }
7233deb3ec6SMatthias Ringwald }
7243deb3ec6SMatthias Ringwald 
725bf78aac6SMatthias Ringwald // ---------------------
726bf78aac6SMatthias Ringwald // persistent CCC writes
727bf78aac6SMatthias Ringwald static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){
728bf78aac6SMatthias Ringwald     return 'B' << 24 | 'T' << 16 | 'C' << 8 | index;
729bf78aac6SMatthias Ringwald }
730bf78aac6SMatthias Ringwald 
731bf78aac6SMatthias Ringwald static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){
732bf78aac6SMatthias Ringwald     // lookup att_server instance
733bf78aac6SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
734bf78aac6SMatthias Ringwald     if (!att_server) return;
735bf78aac6SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
736bf78aac6SMatthias 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);
7376a540790SMatthias Ringwald 
738bf78aac6SMatthias Ringwald     // check if bonded
739bf78aac6SMatthias Ringwald     if (le_device_index < 0) return;
7406a540790SMatthias Ringwald 
741bf78aac6SMatthias Ringwald     // get btstack_tlv
742bf78aac6SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
743bf78aac6SMatthias Ringwald     void * tlv_context;
744bf78aac6SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
745bf78aac6SMatthias Ringwald     if (!tlv_impl) return;
7466a540790SMatthias Ringwald 
747bf78aac6SMatthias Ringwald     // update ccc tag
748bf78aac6SMatthias Ringwald     int index;
7496a540790SMatthias Ringwald     uint32_t highest_seq_nr = 0;
7506a540790SMatthias Ringwald     uint32_t lowest_seq_nr = 0;
7516a540790SMatthias Ringwald     uint32_t tag_for_lowest_seq_nr = 0;
7526a540790SMatthias Ringwald     uint32_t tag_for_empty = 0;
7536a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
7546afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
755bf78aac6SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
7566a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
7576a540790SMatthias Ringwald 
7586a540790SMatthias Ringwald         // empty/invalid tag
7596a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)){
7606a540790SMatthias Ringwald             tag_for_empty = tag;
761bf78aac6SMatthias Ringwald             continue;
762bf78aac6SMatthias Ringwald         }
7636a540790SMatthias Ringwald         // update highest seq nr
7646a540790SMatthias Ringwald         if (entry.seq_nr > highest_seq_nr){
7656a540790SMatthias Ringwald             highest_seq_nr = entry.seq_nr;
7666a540790SMatthias Ringwald         }
7676a540790SMatthias Ringwald         // find entry with lowest seq nr
7686a540790SMatthias Ringwald         if ((tag_for_lowest_seq_nr == 0) || (entry.seq_nr < lowest_seq_nr)){
7696a540790SMatthias Ringwald             tag_for_lowest_seq_nr = tag;
7706a540790SMatthias Ringwald             lowest_seq_nr = entry.seq_nr;
7716a540790SMatthias Ringwald         }
7726a540790SMatthias Ringwald 
7736a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
7746a540790SMatthias Ringwald         if (entry.att_handle   != att_handle)      continue;
7756a540790SMatthias Ringwald 
7766a540790SMatthias Ringwald         // found matching entry
77701ac2008SMatthias Ringwald         if (value){
778bf78aac6SMatthias Ringwald             // update
7796a540790SMatthias Ringwald             if (entry.value == value) {
780760ad805SMatthias Ringwald                 log_info("CCC Index %u: Up-to-date", index);
781760ad805SMatthias Ringwald                 return;
782760ad805SMatthias Ringwald             }
7836a540790SMatthias Ringwald             entry.value = value;
7846a540790SMatthias Ringwald             entry.seq_nr = highest_seq_nr + 1;
785760ad805SMatthias Ringwald             log_info("CCC Index %u: Store", index);
7866a540790SMatthias Ringwald             tlv_impl->store_tag(tlv_context, tag, (const uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
78701ac2008SMatthias Ringwald         } else {
78801ac2008SMatthias Ringwald             // delete
789760ad805SMatthias Ringwald             log_info("CCC Index %u: Delete", index);
79001ac2008SMatthias Ringwald             tlv_impl->delete_tag(tlv_context, tag);
79101ac2008SMatthias Ringwald         }
792bf78aac6SMatthias Ringwald         return;
793bf78aac6SMatthias Ringwald     }
7946a540790SMatthias Ringwald 
795faf2b6c3SMatthias Ringwald     log_info("tag_for_empy %"PRIx32", tag_for_lowest_seq_nr %"PRIx32, tag_for_empty, tag_for_lowest_seq_nr);
7966a540790SMatthias Ringwald 
7976a540790SMatthias Ringwald     if (value == 0){
7986a540790SMatthias Ringwald         // done
7996a540790SMatthias Ringwald         return;
8006a540790SMatthias Ringwald     }
8016a540790SMatthias Ringwald 
8026a540790SMatthias Ringwald     uint32_t tag_to_use = 0;
8036a540790SMatthias Ringwald     if (tag_for_empty){
8046a540790SMatthias Ringwald         tag_to_use = tag_for_empty;
8056a540790SMatthias Ringwald     } else if (tag_for_lowest_seq_nr){
8066a540790SMatthias Ringwald         tag_to_use = tag_for_lowest_seq_nr;
8076a540790SMatthias Ringwald     } else {
8086a540790SMatthias Ringwald         // should not happen
8096a540790SMatthias Ringwald         return;
8106a540790SMatthias Ringwald     }
811bf78aac6SMatthias Ringwald     // store ccc tag
8126a540790SMatthias Ringwald     entry.seq_nr       = highest_seq_nr + 1;
8136a540790SMatthias Ringwald     entry.device_index = le_device_index;
8146a540790SMatthias Ringwald     entry.att_handle   = att_handle;
8156a540790SMatthias Ringwald     entry.value        = value;
8166a540790SMatthias Ringwald     tlv_impl->store_tag(tlv_context, tag_to_use, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
817bf78aac6SMatthias Ringwald }
818bf78aac6SMatthias Ringwald 
819760ad805SMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server){
820760ad805SMatthias Ringwald     if (!att_server) return;
821760ad805SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
822760ad805SMatthias Ringwald     log_info("Clear CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
823760ad805SMatthias Ringwald     // check if bonded
824760ad805SMatthias Ringwald     if (le_device_index < 0) return;
825760ad805SMatthias Ringwald     // get btstack_tlv
826760ad805SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
827760ad805SMatthias Ringwald     void * tlv_context;
828760ad805SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
829760ad805SMatthias Ringwald     if (!tlv_impl) return;
830760ad805SMatthias Ringwald     // get all ccc tag
831760ad805SMatthias Ringwald     int index;
8326a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
8336afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
834760ad805SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
8356a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
8366a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
8376a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
838760ad805SMatthias Ringwald         // delete entry
839760ad805SMatthias Ringwald         log_info("CCC Index %u: Delete", index);
840760ad805SMatthias Ringwald         tlv_impl->delete_tag(tlv_context, tag);
841760ad805SMatthias Ringwald     }
842760ad805SMatthias Ringwald }
843760ad805SMatthias Ringwald 
84401ac2008SMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server){
84501ac2008SMatthias Ringwald     if (!att_server) return;
84601ac2008SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
84701ac2008SMatthias Ringwald     log_info("Restore CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
8481b7acd0dSMatthias Ringwald     // check if bonded
8491b7acd0dSMatthias Ringwald     if (le_device_index < 0) return;
85001ac2008SMatthias Ringwald     // get btstack_tlv
85101ac2008SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
85201ac2008SMatthias Ringwald     void * tlv_context;
85301ac2008SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
85401ac2008SMatthias Ringwald     if (!tlv_impl) return;
85501ac2008SMatthias Ringwald     // get all ccc tag
85601ac2008SMatthias Ringwald     int index;
8576a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
8586afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
85901ac2008SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
8606a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
8616a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
8626a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
86301ac2008SMatthias Ringwald         // simulate write callback
8646a540790SMatthias Ringwald         uint16_t attribute_handle = entry.att_handle;
86501ac2008SMatthias Ringwald         uint8_t  value[2];
8666a540790SMatthias Ringwald         little_endian_store_16(value, 0, entry.value);
86701ac2008SMatthias Ringwald         att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
86801ac2008SMatthias Ringwald         if (!callback) continue;
8696a540790SMatthias Ringwald         log_info("CCC Index %u: Set Attribute handle 0x%04x to value 0x%04x", index, attribute_handle, entry.value );
87001ac2008SMatthias Ringwald         (*callback)(att_server->connection.con_handle, attribute_handle, ATT_TRANSACTION_MODE_NONE, 0, value, sizeof(value));
87101ac2008SMatthias Ringwald     }
87201ac2008SMatthias Ringwald }
87301ac2008SMatthias Ringwald 
874bf78aac6SMatthias Ringwald // persistent CCC writes
875bf78aac6SMatthias Ringwald // ---------------------
8763d71c7a4SMatthias Ringwald 
8773d71c7a4SMatthias Ringwald // gatt service management
8783d71c7a4SMatthias Ringwald static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){
8793d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
8803d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
8813d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
8823d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
8833d71c7a4SMatthias Ringwald         if (handler->start_handle > handle) continue;
8843d71c7a4SMatthias Ringwald         if (handler->end_handle   < handle) continue;
8853d71c7a4SMatthias Ringwald         return handler;
8863d71c7a4SMatthias Ringwald     }
8873d71c7a4SMatthias Ringwald     return NULL;
8883d71c7a4SMatthias Ringwald }
8893d71c7a4SMatthias Ringwald static att_read_callback_t att_server_read_callback_for_handle(uint16_t handle){
8903d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
8913d71c7a4SMatthias Ringwald     if (handler) return handler->read_callback;
8923d71c7a4SMatthias Ringwald     return att_server_client_read_callback;
8933d71c7a4SMatthias Ringwald }
8943d71c7a4SMatthias Ringwald 
8953d71c7a4SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){
8963d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
8973d71c7a4SMatthias Ringwald     if (handler) return handler->write_callback;
8983d71c7a4SMatthias Ringwald     return att_server_client_write_callback;
8993d71c7a4SMatthias Ringwald }
9003d71c7a4SMatthias Ringwald 
9015d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle){
9025d07396bSMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
9035d07396bSMatthias Ringwald     if (handler) return handler->packet_handler;
9045d07396bSMatthias Ringwald     return att_client_packet_handler;
9055d07396bSMatthias Ringwald }
9065d07396bSMatthias Ringwald 
9073d71c7a4SMatthias Ringwald static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){
9083d71c7a4SMatthias Ringwald     // notify all callbacks
9093d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
9103d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
9113d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
9123d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
9133d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
9143d71c7a4SMatthias Ringwald         (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
9153d71c7a4SMatthias Ringwald     }
9163d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return;
9173d71c7a4SMatthias Ringwald     (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
9183d71c7a4SMatthias Ringwald }
9193d71c7a4SMatthias Ringwald 
9203d71c7a4SMatthias Ringwald // returns first reported error or 0
9213d71c7a4SMatthias Ringwald static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){
9223d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
9233d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
9243d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
9253d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
9263d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
9273d71c7a4SMatthias Ringwald         uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
9283d71c7a4SMatthias Ringwald         if (error_code) return error_code;
9293d71c7a4SMatthias Ringwald     }
9303d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return 0;
9313d71c7a4SMatthias Ringwald     return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
9323d71c7a4SMatthias Ringwald }
9333d71c7a4SMatthias Ringwald 
9343d71c7a4SMatthias 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){
9353d71c7a4SMatthias Ringwald     att_read_callback_t callback = att_server_read_callback_for_handle(attribute_handle);
93601ac2008SMatthias Ringwald     if (!callback) return 0;
9373d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, offset, buffer, buffer_size);
9383d71c7a4SMatthias Ringwald }
9393d71c7a4SMatthias Ringwald 
9403d71c7a4SMatthias 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){
9413d71c7a4SMatthias Ringwald     switch (transaction_mode){
9423d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_VALIDATE:
9433d71c7a4SMatthias Ringwald             return att_validate_prepared_write(con_handle);
9443d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_EXECUTE:
9453d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_CANCEL:
9463d71c7a4SMatthias Ringwald             att_notify_write_callbacks(con_handle, transaction_mode);
9473d71c7a4SMatthias Ringwald             return 0;
9483d71c7a4SMatthias Ringwald         default:
9493d71c7a4SMatthias Ringwald             break;
9503d71c7a4SMatthias Ringwald     }
9513d71c7a4SMatthias Ringwald 
952bf78aac6SMatthias Ringwald     // track CCC writes
953bf78aac6SMatthias Ringwald     if (att_is_persistent_ccc(attribute_handle) && offset == 0 && buffer_size == 2){
954bf78aac6SMatthias Ringwald         att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0));
955bf78aac6SMatthias Ringwald     }
956bf78aac6SMatthias Ringwald 
95701ac2008SMatthias Ringwald     att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
95801ac2008SMatthias Ringwald     if (!callback) return 0;
9593d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size);
9603d71c7a4SMatthias Ringwald }
9613d71c7a4SMatthias Ringwald 
9623d71c7a4SMatthias Ringwald /**
9633d71c7a4SMatthias Ringwald  * @brief register read/write callbacks for specific handle range
9643d71c7a4SMatthias Ringwald  * @param att_service_handler_t
9653d71c7a4SMatthias Ringwald  */
9663d71c7a4SMatthias Ringwald void att_server_register_service_handler(att_service_handler_t * handler){
9673d71c7a4SMatthias Ringwald     if (att_service_handler_for_handle(handler->start_handle) ||
9683d71c7a4SMatthias Ringwald         att_service_handler_for_handle(handler->end_handle)){
9693d71c7a4SMatthias Ringwald         log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle);
9703d71c7a4SMatthias Ringwald         return;
9713d71c7a4SMatthias Ringwald     }
9723d71c7a4SMatthias Ringwald     btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler);
9733d71c7a4SMatthias Ringwald }
9743d71c7a4SMatthias Ringwald 
9753deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){
9763deb3ec6SMatthias Ringwald 
9773d71c7a4SMatthias Ringwald     // store callbacks
9783d71c7a4SMatthias Ringwald     att_server_client_read_callback  = read_callback;
9793d71c7a4SMatthias Ringwald     att_server_client_write_callback = write_callback;
9803d71c7a4SMatthias Ringwald 
9811a389cdcSMatthias Ringwald     // register for HCI Events
982d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &att_event_packet_handler;
9831a389cdcSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
9841a389cdcSMatthias Ringwald 
985406722e4SMatthias Ringwald     // register for SM events
986d9a7306aSMatthias Ringwald     sm_event_callback_registration.callback = &att_event_packet_handler;
987406722e4SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
9883deb3ec6SMatthias Ringwald 
9891a389cdcSMatthias Ringwald     // and L2CAP ATT Server PDUs
9903deb3ec6SMatthias Ringwald     att_dispatch_register_server(att_packet_handler);
9913deb3ec6SMatthias Ringwald 
9923deb3ec6SMatthias Ringwald     att_set_db(db);
9933d71c7a4SMatthias Ringwald     att_set_read_callback(att_server_read_callback);
9943d71c7a4SMatthias Ringwald     att_set_write_callback(att_server_write_callback);
9953deb3ec6SMatthias Ringwald 
9963deb3ec6SMatthias Ringwald }
9973deb3ec6SMatthias Ringwald 
9983deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){
9993deb3ec6SMatthias Ringwald     att_client_packet_handler = handler;
10003deb3ec6SMatthias Ringwald }
10013deb3ec6SMatthias Ringwald 
1002cbbb12d9SMatthias Ringwald 
1003cbbb12d9SMatthias Ringwald // to be deprecated
1004c141988cSMatthias Ringwald int  att_server_can_send_packet_now(hci_con_handle_t con_handle){
100518707219SMatthias Ringwald     return att_dispatch_server_can_send_now(con_handle);
10061b96b007SMatthias Ringwald }
10071b96b007SMatthias Ringwald 
1008cbbb12d9SMatthias Ringwald int att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
1009cbbb12d9SMatthias Ringwald     return att_server_request_to_send_notification(callback_registration, con_handle);
10103deb3ec6SMatthias Ringwald }
10113deb3ec6SMatthias Ringwald 
1012cbbb12d9SMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){
1013cbbb12d9SMatthias Ringwald     att_client_waiting_for_can_send_registration.callback = &att_emit_can_send_now_event;
1014cbbb12d9SMatthias Ringwald     att_server_request_to_send_notification(&att_client_waiting_for_can_send_registration, con_handle);
1015cbbb12d9SMatthias Ringwald }
1016cbbb12d9SMatthias Ringwald // end of deprecated
1017cbbb12d9SMatthias Ringwald 
1018cbbb12d9SMatthias Ringwald int att_server_request_to_send_notification(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
1019969a5bbaSMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
1020969a5bbaSMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1021cbbb12d9SMatthias Ringwald     btstack_linked_list_add_tail(&att_server->notification_requests, (btstack_linked_item_t*) callback_registration);
1022cbbb12d9SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
1023cbbb12d9SMatthias Ringwald     return ERROR_CODE_SUCCESS;
1024cbbb12d9SMatthias Ringwald }
1025cbbb12d9SMatthias Ringwald 
1026cbbb12d9SMatthias Ringwald int att_server_request_to_send_indication(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
1027cbbb12d9SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
1028cbbb12d9SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1029cbbb12d9SMatthias Ringwald     btstack_linked_list_add_tail(&att_server->indication_requests, (btstack_linked_item_t*) callback_registration);
1030bb38f057SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
1031969a5bbaSMatthias Ringwald     return ERROR_CODE_SUCCESS;
1032bb38f057SMatthias Ringwald }
1033bb38f057SMatthias Ringwald 
10343bb3035fSMilanka Ringwald int att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){
103518707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
103618707219SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
103718707219SMatthias Ringwald 
103818707219SMatthias Ringwald     if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
10393deb3ec6SMatthias Ringwald 
10403deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
10413deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
104218707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_notification(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
104318707219SMatthias Ringwald 	return l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
10443deb3ec6SMatthias Ringwald }
10453deb3ec6SMatthias Ringwald 
10463bb3035fSMilanka Ringwald int att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){
104718707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
104818707219SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
104918707219SMatthias Ringwald 
1050eaac31e8SMatthias Ringwald     if (att_server->value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS;
105118707219SMatthias Ringwald     if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
10523deb3ec6SMatthias Ringwald 
10533deb3ec6SMatthias Ringwald     // track indication
105418707219SMatthias Ringwald     att_server->value_indication_handle = attribute_handle;
105518707219SMatthias Ringwald     btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout);
105618707219SMatthias Ringwald     btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS);
105718707219SMatthias Ringwald     btstack_run_loop_add_timer(&att_server->value_indication_timer);
10583deb3ec6SMatthias Ringwald 
10593deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
10603deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
106118707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_indication(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
106218707219SMatthias Ringwald 	l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
10633deb3ec6SMatthias Ringwald     return 0;
10643deb3ec6SMatthias Ringwald }
10658f9132b5SMilanka Ringwald 
10668f9132b5SMilanka Ringwald uint16_t att_server_get_mtu(hci_con_handle_t con_handle){
10678f9132b5SMilanka Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
10688f9132b5SMilanka Ringwald     if (!att_server) return 0;
10698f9132b5SMilanka Ringwald     return att_server->connection.mtu;
10708f9132b5SMilanka Ringwald }
1071