xref: /btstack/src/ble/att_server.c (revision 69d32c5615666525fac45bfb664d1a001b14dbdc)
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);
7941772f37SMatthias Ringwald static void att_server_handle_att_pdu(att_server_t * att_server, uint8_t * packet, uint16_t size);
803deb3ec6SMatthias Ringwald 
813551936eSMatthias Ringwald typedef enum {
823551936eSMatthias Ringwald     ATT_SERVER_RUN_PHASE_1_REQUESTS,
833551936eSMatthias Ringwald     ATT_SERVER_RUN_PHASE_2_INDICATIONS,
843551936eSMatthias Ringwald     ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS,
853551936eSMatthias Ringwald } att_server_run_phase_t;
863551936eSMatthias Ringwald 
876a540790SMatthias Ringwald //
886a540790SMatthias Ringwald typedef struct {
896a540790SMatthias Ringwald     uint32_t seq_nr;
906a540790SMatthias Ringwald     uint16_t att_handle;
916a540790SMatthias Ringwald     uint8_t  value;
926a540790SMatthias Ringwald     uint8_t  device_index;
936a540790SMatthias Ringwald } persistent_ccc_entry_t;
946a540790SMatthias Ringwald 
9518707219SMatthias Ringwald // global
961a389cdcSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
97406722e4SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration;
983deb3ec6SMatthias Ringwald static btstack_packet_handler_t               att_client_packet_handler = NULL;
993d71c7a4SMatthias Ringwald static btstack_linked_list_t                  service_handlers;
1005f141511SMatthias Ringwald static btstack_context_callback_registration_t att_client_waiting_for_can_send_registration;
10118707219SMatthias Ringwald 
1023d71c7a4SMatthias Ringwald static att_read_callback_t                    att_server_client_read_callback;
1033d71c7a4SMatthias Ringwald static att_write_callback_t                   att_server_client_write_callback;
1043d71c7a4SMatthias Ringwald 
1056438547aSMatthias Ringwald // round robin
1066438547aSMatthias Ringwald static hci_con_handle_t att_server_last_can_send_now = HCI_CON_HANDLE_INVALID;
107bf78aac6SMatthias Ringwald 
10818707219SMatthias Ringwald static att_server_t * att_server_for_handle(hci_con_handle_t con_handle){
10918707219SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
11018707219SMatthias Ringwald     if (!hci_connection) return NULL;
11118707219SMatthias Ringwald     return &hci_connection->att_server;
11218707219SMatthias Ringwald }
11318707219SMatthias Ringwald 
11470dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
11570dca4d4SMatthias Ringwald static att_server_t * att_server_for_l2cap_cid(uint16_t l2cap_cid){
11670dca4d4SMatthias Ringwald     btstack_linked_list_iterator_t it;
11770dca4d4SMatthias Ringwald     hci_connections_get_iterator(&it);
11870dca4d4SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
11970dca4d4SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
12070dca4d4SMatthias Ringwald         att_server_t * att_server = &connection->att_server;
12170dca4d4SMatthias Ringwald         if (att_server->l2cap_cid == l2cap_cid) return att_server;
12270dca4d4SMatthias Ringwald     }
12370dca4d4SMatthias Ringwald     return NULL;
12470dca4d4SMatthias Ringwald }
12570dca4d4SMatthias Ringwald #endif
12670dca4d4SMatthias Ringwald 
12702b78fc8SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
12818707219SMatthias Ringwald static att_server_t * att_server_for_state(att_server_state_t state){
12918707219SMatthias Ringwald     btstack_linked_list_iterator_t it;
13018707219SMatthias Ringwald     hci_connections_get_iterator(&it);
13118707219SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
13218707219SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
13318707219SMatthias Ringwald         att_server_t * att_server = &connection->att_server;
13418707219SMatthias Ringwald         if (att_server->state == state) return att_server;
13518707219SMatthias Ringwald     }
13618707219SMatthias Ringwald     return NULL;
13718707219SMatthias Ringwald }
13802b78fc8SMatthias Ringwald #endif
139bb38f057SMatthias Ringwald 
1403deb3ec6SMatthias Ringwald static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){
1415d07396bSMatthias Ringwald     btstack_packet_handler_t packet_handler = att_server_packet_handler_for_handle(attribute_handle);
1425d07396bSMatthias Ringwald     if (!packet_handler) return;
1433deb3ec6SMatthias Ringwald 
1443deb3ec6SMatthias Ringwald     uint8_t event[7];
1453deb3ec6SMatthias Ringwald     int pos = 0;
1465611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE;
1473deb3ec6SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
1483deb3ec6SMatthias Ringwald     event[pos++] = status;
149f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, client_handle);
1503deb3ec6SMatthias Ringwald     pos += 2;
151f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, attribute_handle);
152a444112bSMatthias Ringwald     (*packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1533deb3ec6SMatthias Ringwald }
1543deb3ec6SMatthias Ringwald 
1555d07396bSMatthias Ringwald static void att_emit_event_to_all(const uint8_t * event, uint16_t size){
1565d07396bSMatthias Ringwald     // dispatch to app level handler
1575d07396bSMatthias Ringwald     if (att_client_packet_handler){
1585d07396bSMatthias Ringwald         (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size);
1595d07396bSMatthias Ringwald     }
1603deb3ec6SMatthias Ringwald 
1615d07396bSMatthias Ringwald     // dispatch to service handlers
1625d07396bSMatthias Ringwald     btstack_linked_list_iterator_t it;
1635d07396bSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
1645d07396bSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1655d07396bSMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
1665d07396bSMatthias Ringwald         if (!handler->packet_handler) continue;
1675d07396bSMatthias Ringwald         (*handler->packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size);
1685d07396bSMatthias Ringwald     }
1695d07396bSMatthias Ringwald }
1705d07396bSMatthias Ringwald 
1715d07396bSMatthias Ringwald static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){
1723deb3ec6SMatthias Ringwald     uint8_t event[6];
1733deb3ec6SMatthias Ringwald     int pos = 0;
1745611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE;
1753deb3ec6SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
176fc64f94aSMatthias Ringwald     little_endian_store_16(event, pos, con_handle);
1773deb3ec6SMatthias Ringwald     pos += 2;
178f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, mtu);
1795d07396bSMatthias Ringwald 
1805d07396bSMatthias Ringwald     // also dispatch to GATT Clients
181b12646c5SJakob Krantz     att_dispatch_server_mtu_exchanged(con_handle, mtu);
1825d07396bSMatthias Ringwald 
1835d07396bSMatthias Ringwald     // dispatch to app level handler and service handlers
1845d07396bSMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
1853deb3ec6SMatthias Ringwald }
1863deb3ec6SMatthias Ringwald 
1875f141511SMatthias Ringwald static void att_emit_can_send_now_event(void * context){
1885f141511SMatthias Ringwald     UNUSED(context);
1893c97b242SMatthias Ringwald     if (!att_client_packet_handler) return;
1903c97b242SMatthias Ringwald 
1911b96b007SMatthias Ringwald     uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0};
1921b96b007SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1931b96b007SMatthias Ringwald }
1941b96b007SMatthias Ringwald 
195ff1bec95SMatthias Ringwald static void att_emit_connected_event(att_server_t * att_server){
196ff1bec95SMatthias Ringwald     uint8_t event[11];
197ff1bec95SMatthias Ringwald     int pos = 0;
198ff1bec95SMatthias Ringwald     event[pos++] = ATT_EVENT_CONNECTED;
199ff1bec95SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
200ff1bec95SMatthias Ringwald     event[pos++] = att_server->peer_addr_type;
201ff1bec95SMatthias Ringwald     reverse_bd_addr(att_server->peer_address, &event[pos]);
202ff1bec95SMatthias Ringwald     pos += 6;
203ff1bec95SMatthias Ringwald     little_endian_store_16(event, pos, att_server->connection.con_handle);
204ff1bec95SMatthias Ringwald     pos += 2;
205ff1bec95SMatthias Ringwald 
206ff1bec95SMatthias Ringwald     // dispatch to app level handler and service handlers
207ff1bec95SMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
208ff1bec95SMatthias Ringwald }
209ff1bec95SMatthias Ringwald 
210ff1bec95SMatthias Ringwald 
2116187ad4cSMatthias Ringwald static void att_emit_disconnected_event(uint16_t con_handle){
212ff1bec95SMatthias Ringwald     uint8_t event[4];
213ff1bec95SMatthias Ringwald     int pos = 0;
214ff1bec95SMatthias Ringwald     event[pos++] = ATT_EVENT_DISCONNECTED;
215ff1bec95SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
2166187ad4cSMatthias Ringwald     little_endian_store_16(event, pos, con_handle);
217ff1bec95SMatthias Ringwald     pos += 2;
218ff1bec95SMatthias Ringwald 
219ff1bec95SMatthias Ringwald     // dispatch to app level handler and service handlers
220ff1bec95SMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
221ff1bec95SMatthias Ringwald }
222ff1bec95SMatthias Ringwald 
223ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){
22454178543SMatthias Ringwald     void * context = btstack_run_loop_get_timer_context(ts);
22554178543SMatthias Ringwald     hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
22618707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
22718707219SMatthias Ringwald     if (!att_server) return;
228bce48a26SMatthias Ringwald     // @note: after a transcation timeout, no more requests shall be sent over this ATT Bearer
229bce48a26SMatthias Ringwald     // (that's why we don't reset the value_indication_handle)
23018707219SMatthias Ringwald     uint16_t att_handle = att_server->value_indication_handle;
23118707219SMatthias Ringwald     att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_server->connection.con_handle, att_handle);
2323deb3ec6SMatthias Ringwald }
2333deb3ec6SMatthias Ringwald 
2343deb3ec6SMatthias Ringwald static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2359ec2630cSMatthias Ringwald 
2367dad9ff8SMatthias Ringwald     UNUSED(channel); // ok: there is no channel
2377dad9ff8SMatthias Ringwald     UNUSED(size);    // ok: handling own l2cap events
2383deb3ec6SMatthias Ringwald 
23918707219SMatthias Ringwald     att_server_t * att_server;
24018707219SMatthias Ringwald     hci_con_handle_t con_handle;
24170dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
24270dca4d4SMatthias Ringwald     bd_addr_t address;
24370dca4d4SMatthias Ringwald #endif
24418707219SMatthias Ringwald 
2453deb3ec6SMatthias Ringwald     switch (packet_type) {
2463deb3ec6SMatthias Ringwald 
2473deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
2480e2df43fSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
2493deb3ec6SMatthias Ringwald 
25070dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
25170dca4d4SMatthias Ringwald                 case L2CAP_EVENT_INCOMING_CONNECTION:
25270dca4d4SMatthias Ringwald                     l2cap_event_incoming_connection_get_address(packet, address);
25370dca4d4SMatthias Ringwald                     l2cap_accept_connection(channel);
25470dca4d4SMatthias Ringwald                     printf("Accept incoming connection from %s\n", bd_addr_to_str(address));
25570dca4d4SMatthias Ringwald                     break;
25670dca4d4SMatthias Ringwald                 case L2CAP_EVENT_CHANNEL_OPENED:
257122b723eSMatthias Ringwald                     con_handle = l2cap_event_channel_opened_get_handle(packet);
258122b723eSMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
25970dca4d4SMatthias Ringwald                     if (!att_server) break;
260122b723eSMatthias Ringwald                     // store connection info
261122b723eSMatthias Ringwald                     att_server->peer_addr_type = BD_ADDR_TYPE_CLASSIC;
262122b723eSMatthias Ringwald                     l2cap_event_channel_opened_get_address(packet, att_server->peer_address);
263122b723eSMatthias Ringwald                     att_server->connection.con_handle = con_handle;
26470dca4d4SMatthias Ringwald                     att_server->l2cap_cid = l2cap_event_channel_opened_get_local_cid(packet);
265122b723eSMatthias Ringwald                     // reset connection properties
266122b723eSMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
267122b723eSMatthias Ringwald                     att_server->connection.mtu = l2cap_event_channel_opened_get_remote_mtu(packet);
268122b723eSMatthias Ringwald                     att_server->connection.max_mtu = l2cap_max_mtu();
269122b723eSMatthias Ringwald                     if (att_server->connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){
270122b723eSMatthias Ringwald                         att_server->connection.max_mtu = ATT_REQUEST_BUFFER_SIZE;
271122b723eSMatthias Ringwald                     }
272122b723eSMatthias Ringwald                     // TODO: use security level from underlying HCI connection
273122b723eSMatthias Ringwald                     att_server->connection.encryption_key_size = 0;
274122b723eSMatthias Ringwald                     att_server->connection.authenticated = 0;
275122b723eSMatthias Ringwald                     att_server->connection.authorized = 0;
276122b723eSMatthias Ringwald                     // TODO: what to do about le device db?
277122b723eSMatthias Ringwald                     att_server->pairing_active = 0;
278122b723eSMatthias Ringwald                     printf("Connection opened %s, l2cap cid %04x, \n", bd_addr_to_str(address), att_server->l2cap_cid);
27970dca4d4SMatthias Ringwald                     break;
28070dca4d4SMatthias Ringwald #endif
2813deb3ec6SMatthias Ringwald                 case HCI_EVENT_LE_META:
2823deb3ec6SMatthias Ringwald                     switch (packet[2]) {
2833deb3ec6SMatthias Ringwald                         case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
28418707219SMatthias Ringwald                             con_handle = little_endian_read_16(packet, 4);
28518707219SMatthias Ringwald                             att_server = att_server_for_handle(con_handle);
28618707219SMatthias Ringwald                             if (!att_server) break;
2873deb3ec6SMatthias Ringwald                         	// store connection info
28818707219SMatthias Ringwald                         	att_server->peer_addr_type = packet[7];
28918707219SMatthias Ringwald                             reverse_bd_addr(&packet[8], att_server->peer_address);
29018707219SMatthias Ringwald                             att_server->connection.con_handle = con_handle;
2913deb3ec6SMatthias Ringwald                             // reset connection properties
29218707219SMatthias Ringwald                             att_server->state = ATT_SERVER_IDLE;
29318707219SMatthias Ringwald                             att_server->connection.mtu = ATT_DEFAULT_MTU;
29418707219SMatthias Ringwald                             att_server->connection.max_mtu = l2cap_max_le_mtu();
29518707219SMatthias Ringwald                             if (att_server->connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){
29618707219SMatthias Ringwald                                 att_server->connection.max_mtu = ATT_REQUEST_BUFFER_SIZE;
29799c58ad0SMatthias Ringwald                             }
29818707219SMatthias Ringwald                             att_server->connection.encryption_key_size = 0;
29918707219SMatthias Ringwald                             att_server->connection.authenticated = 0;
30018707219SMatthias Ringwald 		                	att_server->connection.authorized = 0;
301b2c1e52cSMatthias Ringwald                             // workaround: identity resolving can already be complete, at least store result
302b2c1e52cSMatthias Ringwald                             att_server->ir_le_device_db_index = sm_le_device_index(con_handle);
303ed053a35SMatthias Ringwald                             att_server->ir_lookup_active = 0;
304760ad805SMatthias Ringwald                             att_server->pairing_active = 0;
305ff1bec95SMatthias Ringwald                             // notify all - old
3065d07396bSMatthias Ringwald                             att_emit_event_to_all(packet, size);
307ff1bec95SMatthias Ringwald                             // notify all - new
308ff1bec95SMatthias Ringwald                             att_emit_connected_event(att_server);
3093deb3ec6SMatthias Ringwald                             break;
3103deb3ec6SMatthias Ringwald 
3113deb3ec6SMatthias Ringwald                         default:
3123deb3ec6SMatthias Ringwald                             break;
3133deb3ec6SMatthias Ringwald                     }
3143deb3ec6SMatthias Ringwald                     break;
3153deb3ec6SMatthias Ringwald 
3163deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_CHANGE:
3173deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE:
3183deb3ec6SMatthias Ringwald                 	// check handle
31918707219SMatthias Ringwald                     con_handle = little_endian_read_16(packet, 3);
32018707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
32118707219SMatthias Ringwald                     if (!att_server) break;
3229c6e867eSMatthias Ringwald                     att_server->connection.encryption_key_size = gap_encryption_key_size(con_handle);
3239c6e867eSMatthias Ringwald                     att_server->connection.authenticated = gap_authenticated(con_handle);
32413eb7322SMatthias Ringwald                     att_server->connection.secure_connection = gap_secure_connection(con_handle);
32513eb7322SMatthias Ringwald                     log_info("encrypted key size %u, authenticated %u, secure connectipon %u",
32613eb7322SMatthias Ringwald                         att_server->connection.encryption_key_size, att_server->connection.authenticated, att_server->connection.secure_connection);
32701ac2008SMatthias Ringwald                     if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){
32801ac2008SMatthias Ringwald                         // restore CCC values when encrypted
32901ac2008SMatthias Ringwald                         if (hci_event_encryption_change_get_encryption_enabled(packet)){
33001ac2008SMatthias Ringwald                             att_server_persistent_ccc_restore(att_server);
33101ac2008SMatthias Ringwald                         }
33201ac2008SMatthias Ringwald                     }
3330234fbbcSMatthias Ringwald                     att_run_for_context(att_server);
3343deb3ec6SMatthias Ringwald                     break;
3353deb3ec6SMatthias Ringwald 
3363deb3ec6SMatthias Ringwald                 case HCI_EVENT_DISCONNECTION_COMPLETE:
33770a390c7SMatthias Ringwald                     // check handle
33818707219SMatthias Ringwald                     con_handle = hci_event_disconnection_complete_get_connection_handle(packet);
33918707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
34018707219SMatthias Ringwald                     if (!att_server) break;
34118707219SMatthias Ringwald                     att_clear_transaction_queue(&att_server->connection);
34218707219SMatthias Ringwald                     att_server->connection.con_handle = 0;
343760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
34418707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
345bce48a26SMatthias Ringwald                     if (att_server->value_indication_handle){
34627328ecbSMatthias Ringwald                         btstack_run_loop_remove_timer(&att_server->value_indication_timer);
347bce48a26SMatthias Ringwald                         uint16_t att_handle = att_server->value_indication_handle;
348bce48a26SMatthias Ringwald                         att_server->value_indication_handle = 0; // reset error state
349bce48a26SMatthias Ringwald                         att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_DISCONNECT, att_server->connection.con_handle, att_handle);
350bce48a26SMatthias Ringwald                     }
351ff1bec95SMatthias Ringwald                     // notify all - new
3526187ad4cSMatthias Ringwald                     att_emit_disconnected_event(con_handle);
353ff1bec95SMatthias Ringwald                     // notify all - old
3545d07396bSMatthias Ringwald                     att_emit_event_to_all(packet, size);
3553deb3ec6SMatthias Ringwald                     break;
3563deb3ec6SMatthias Ringwald 
357760ad805SMatthias Ringwald                 // Identity Resolving
3585611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_STARTED:
35918707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_started_get_handle(packet);
36018707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
36118707219SMatthias Ringwald                     if (!att_server) break;
3625611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED");
36318707219SMatthias Ringwald                     att_server->ir_lookup_active = 1;
3643deb3ec6SMatthias Ringwald                     break;
3655611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
366760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
367760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
368760ad805SMatthias Ringwald                     if (!att_server) return;
3691b7acd0dSMatthias Ringwald                     att_server->ir_lookup_active = 0;
3701b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index(packet);
3711b7acd0dSMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED");
3721b7acd0dSMatthias Ringwald                     att_run_for_context(att_server);
3733deb3ec6SMatthias Ringwald                     break;
3745611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_FAILED:
37518707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_failed_get_handle(packet);
37618707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
37718707219SMatthias Ringwald                     if (!att_server) break;
3785611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED");
37918707219SMatthias Ringwald                     att_server->ir_lookup_active = 0;
38018707219SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
38118707219SMatthias Ringwald                     att_run_for_context(att_server);
3823deb3ec6SMatthias Ringwald                     break;
383760ad805SMatthias Ringwald 
384760ad805SMatthias Ringwald                 // Pairing started - delete stored CCC values
385760ad805SMatthias Ringwald                 // - assumes pairing indicates either new device or re-pairing, in both cases there should be no stored CCC values
386760ad805SMatthias Ringwald                 // - assumes that all events have the con handle as the first field
387760ad805SMatthias Ringwald                 case SM_EVENT_JUST_WORKS_REQUEST:
388760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
389760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_INPUT_NUMBER:
390760ad805SMatthias Ringwald                 case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
391760ad805SMatthias Ringwald                     con_handle = sm_event_just_works_request_get_handle(packet);
392760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
393760ad805SMatthias Ringwald                     if (!att_server) break;
394760ad805SMatthias Ringwald                     att_server->pairing_active = 1;
395760ad805SMatthias Ringwald                     log_info("SM Pairing started");
396760ad805SMatthias Ringwald                     if (att_server->ir_le_device_db_index < 0) break;
397760ad805SMatthias Ringwald                     att_server_persistent_ccc_clear(att_server);
398760ad805SMatthias Ringwald                     // index not valid anymore
399760ad805SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
400760ad805SMatthias Ringwald                     break;
401760ad805SMatthias Ringwald 
4021b7acd0dSMatthias Ringwald                 // Bonding completed
403760ad805SMatthias Ringwald                 case SM_EVENT_IDENTITY_CREATED:
404760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
405760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
406760ad805SMatthias Ringwald                     if (!att_server) return;
407760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
4081b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_created_get_index(packet);
4091b7acd0dSMatthias Ringwald                     att_run_for_context(att_server);
4101b7acd0dSMatthias Ringwald                     break;
4111b7acd0dSMatthias Ringwald 
4121b7acd0dSMatthias Ringwald                 // Pairing complete (with/without bonding=storing of pairing information)
4131b7acd0dSMatthias Ringwald                 case SM_EVENT_PAIRING_COMPLETE:
4141b7acd0dSMatthias Ringwald                     con_handle = sm_event_pairing_complete_get_handle(packet);
4151b7acd0dSMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
4161b7acd0dSMatthias Ringwald                     if (!att_server) return;
4171b7acd0dSMatthias Ringwald                     att_server->pairing_active = 0;
4181b7acd0dSMatthias Ringwald                     att_run_for_context(att_server);
419760ad805SMatthias Ringwald                     break;
420760ad805SMatthias Ringwald 
421760ad805SMatthias Ringwald                 // Authorization
4225611a760SMatthias Ringwald                 case SM_EVENT_AUTHORIZATION_RESULT: {
42318707219SMatthias Ringwald                     con_handle = sm_event_authorization_result_get_handle(packet);
42418707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
42518707219SMatthias Ringwald                     if (!att_server) break;
42618707219SMatthias Ringwald                     att_server->connection.authorized = sm_event_authorization_result_get_authorization_result(packet);
42718707219SMatthias Ringwald                     att_dispatch_server_request_can_send_now_event(con_handle);
4283deb3ec6SMatthias Ringwald                 	break;
4293deb3ec6SMatthias Ringwald                 }
4303deb3ec6SMatthias Ringwald                 default:
4313deb3ec6SMatthias Ringwald                     break;
4323deb3ec6SMatthias Ringwald             }
43365b44ffdSMatthias Ringwald             break;
43470dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
43570dca4d4SMatthias Ringwald         case L2CAP_DATA_PACKET:
43670dca4d4SMatthias Ringwald             att_server = att_server_for_l2cap_cid(channel);
43770dca4d4SMatthias Ringwald             if (!att_server) break;
43841772f37SMatthias Ringwald 
43941772f37SMatthias Ringwald             att_server_handle_att_pdu(att_server, packet, size);
44070dca4d4SMatthias Ringwald             break;
44170dca4d4SMatthias Ringwald #endif
44241772f37SMatthias Ringwald 
44365b44ffdSMatthias Ringwald         default:
44465b44ffdSMatthias Ringwald             break;
4453deb3ec6SMatthias Ringwald     }
4463deb3ec6SMatthias Ringwald }
4473deb3ec6SMatthias Ringwald 
4487a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
4493deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
4503deb3ec6SMatthias Ringwald 
45118707219SMatthias Ringwald     att_server_t * att_server = att_server_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION);
45218707219SMatthias Ringwald     if (!att_server) return;
4533deb3ec6SMatthias Ringwald 
4543deb3ec6SMatthias Ringwald     uint8_t hash_flipped[8];
4559c80e4ccSMatthias Ringwald     reverse_64(hash, hash_flipped);
45618707219SMatthias Ringwald     if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8)){
4573deb3ec6SMatthias Ringwald         log_info("ATT Signed Write, invalid signature");
45818707219SMatthias Ringwald         att_server->state = ATT_SERVER_IDLE;
4593deb3ec6SMatthias Ringwald         return;
4603deb3ec6SMatthias Ringwald     }
4613deb3ec6SMatthias Ringwald     log_info("ATT Signed Write, valid signature");
4623deb3ec6SMatthias Ringwald 
4633deb3ec6SMatthias Ringwald     // update sequence number
46418707219SMatthias Ringwald     uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
46518707219SMatthias Ringwald     le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1);
46618707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
46718707219SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
4683deb3ec6SMatthias Ringwald }
4697a766ebfSMatthias Ringwald #endif
47088a48dd8SMatthias Ringwald 
47118707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED
472b06f2579SMatthias Ringwald // pre: can send now
473b06f2579SMatthias Ringwald // returns: 1 if packet was sent
47418707219SMatthias Ringwald static int att_server_process_validated_request(att_server_t * att_server){
475b06f2579SMatthias Ringwald 
476b06f2579SMatthias Ringwald     l2cap_reserve_packet_buffer();
477b06f2579SMatthias Ringwald     uint8_t * att_response_buffer = l2cap_get_outgoing_buffer();
47818707219SMatthias Ringwald     uint16_t  att_response_size   = att_handle_request(&att_server->connection, att_server->request_buffer, att_server->request_size, att_response_buffer);
479b06f2579SMatthias Ringwald 
480beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
48156c3a347SMatthias Ringwald     if (att_response_size == ATT_READ_RESPONSE_PENDING || att_response_size == ATT_INTERNAL_WRITE_RESPONSE_PENDING){
482e404a688SMatthias Ringwald         // update state
483beb20288SMatthias Ringwald         att_server->state = ATT_SERVER_RESPONSE_PENDING;
484e404a688SMatthias Ringwald 
48556c3a347SMatthias Ringwald         // callback with handle ATT_READ_RESPONSE_PENDING for reads
48656c3a347SMatthias Ringwald         if (att_response_size == ATT_READ_RESPONSE_PENDING){
487e404a688SMatthias Ringwald             att_server_client_read_callback(att_server->connection.con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0);
48856c3a347SMatthias Ringwald         }
489e8e7403eSMatthias Ringwald 
490e8e7403eSMatthias Ringwald         // free reserved buffer
491e8e7403eSMatthias Ringwald         l2cap_release_packet_buffer();
492e8e7403eSMatthias Ringwald         return 0;
493e404a688SMatthias Ringwald     }
494e404a688SMatthias Ringwald #endif
495e404a688SMatthias Ringwald 
496b06f2579SMatthias Ringwald     // intercept "insufficient authorization" for authenticated connections to allow for user authorization
497b06f2579SMatthias Ringwald     if ((att_response_size     >= 4)
498b06f2579SMatthias Ringwald     && (att_response_buffer[0] == ATT_ERROR_RESPONSE)
499b06f2579SMatthias Ringwald     && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION)
50018707219SMatthias Ringwald     && (att_server->connection.authenticated)){
501b06f2579SMatthias Ringwald 
5029c6e867eSMatthias Ringwald         switch (gap_authorization_state(att_server->connection.con_handle)){
503b06f2579SMatthias Ringwald             case AUTHORIZATION_UNKNOWN:
504b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
50518707219SMatthias Ringwald                 sm_request_pairing(att_server->connection.con_handle);
506b06f2579SMatthias Ringwald                 return 0;
507b06f2579SMatthias Ringwald             case AUTHORIZATION_PENDING:
508b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
509b06f2579SMatthias Ringwald                 return 0;
510b06f2579SMatthias Ringwald             default:
511b06f2579SMatthias Ringwald                 break;
512b06f2579SMatthias Ringwald         }
513b06f2579SMatthias Ringwald     }
514b06f2579SMatthias Ringwald 
51518707219SMatthias Ringwald     att_server->state = ATT_SERVER_IDLE;
516b06f2579SMatthias Ringwald     if (att_response_size == 0) {
517b06f2579SMatthias Ringwald         l2cap_release_packet_buffer();
518b06f2579SMatthias Ringwald         return 0;
519b06f2579SMatthias Ringwald     }
520b06f2579SMatthias Ringwald 
52118707219SMatthias Ringwald     l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size);
522b06f2579SMatthias Ringwald 
523b06f2579SMatthias Ringwald     // notify client about MTU exchange result
524b06f2579SMatthias Ringwald     if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){
52518707219SMatthias Ringwald         att_emit_mtu_event(att_server->connection.con_handle, att_server->connection.mtu);
526b06f2579SMatthias Ringwald     }
527b06f2579SMatthias Ringwald     return 1;
528b06f2579SMatthias Ringwald }
529b06f2579SMatthias Ringwald 
530beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
531beb20288SMatthias Ringwald int att_server_response_ready(hci_con_handle_t con_handle){
532e404a688SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
533e404a688SMatthias Ringwald     if (!att_server)                                        return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
534beb20288SMatthias Ringwald     if (att_server->state != ATT_SERVER_RESPONSE_PENDING)   return ERROR_CODE_COMMAND_DISALLOWED;
535e404a688SMatthias Ringwald 
536e404a688SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
537e404a688SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
538e404a688SMatthias Ringwald     return ERROR_CODE_SUCCESS;
539e404a688SMatthias Ringwald }
540e404a688SMatthias Ringwald #endif
541e404a688SMatthias Ringwald 
542ed053a35SMatthias Ringwald static void att_server_request_can_send_now(att_server_t * att_server){
543ed053a35SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
544ed053a35SMatthias Ringwald     if (att_server->l2cap_cid != 0){
545ed053a35SMatthias Ringwald         l2cap_request_can_send_now_event(att_server->l2cap_cid);
546ed053a35SMatthias Ringwald         return;
547ed053a35SMatthias Ringwald     }
548ed053a35SMatthias Ringwald #endif
549ed053a35SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
550ed053a35SMatthias Ringwald }
551ed053a35SMatthias Ringwald 
55218707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server){
55318707219SMatthias Ringwald     switch (att_server->state){
5543deb3ec6SMatthias Ringwald         case ATT_SERVER_REQUEST_RECEIVED:
555760ad805SMatthias Ringwald 
5560234fbbcSMatthias Ringwald             // wait until re-encryption as central is complete
5570234fbbcSMatthias Ringwald             if (gap_reconnect_security_setup_active(att_server->connection.con_handle)) break;
5580234fbbcSMatthias Ringwald 
559760ad805SMatthias Ringwald             // wait until pairing is complete
560760ad805SMatthias Ringwald             if (att_server->pairing_active) break;
561760ad805SMatthias Ringwald 
5627a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
56318707219SMatthias Ringwald             if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){
5643deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write!");
5653deb3ec6SMatthias Ringwald                 if (!sm_cmac_ready()) {
5663deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, sm_cmac engine not ready. Abort");
56718707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
5683deb3ec6SMatthias Ringwald                     return;
5693deb3ec6SMatthias Ringwald                 }
57018707219SMatthias Ringwald                 if (att_server->request_size < (3 + 12)) {
5713deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, request to short. Abort.");
57218707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
5733deb3ec6SMatthias Ringwald                     return;
5743deb3ec6SMatthias Ringwald                 }
57518707219SMatthias Ringwald                 if (att_server->ir_lookup_active){
5763deb3ec6SMatthias Ringwald                     return;
5773deb3ec6SMatthias Ringwald                 }
57818707219SMatthias Ringwald                 if (att_server->ir_le_device_db_index < 0){
5793deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, CSRK not available");
58018707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
5813deb3ec6SMatthias Ringwald                     return;
5823deb3ec6SMatthias Ringwald                 }
5833deb3ec6SMatthias Ringwald 
5843deb3ec6SMatthias Ringwald                 // check counter
58518707219SMatthias Ringwald                 uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
58618707219SMatthias Ringwald                 uint32_t counter_db     = le_device_db_remote_counter_get(att_server->ir_le_device_db_index);
5873deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet);
5883deb3ec6SMatthias Ringwald                 if (counter_packet < counter_db){
5893deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, db reports higher counter, abort");
59018707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
5913deb3ec6SMatthias Ringwald                     return;
5923deb3ec6SMatthias Ringwald                 }
5933deb3ec6SMatthias Ringwald 
5943deb3ec6SMatthias Ringwald                 // signature is { sequence counter, secure hash }
5953deb3ec6SMatthias Ringwald                 sm_key_t csrk;
59618707219SMatthias Ringwald                 le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk);
59718707219SMatthias Ringwald                 att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION;
5983deb3ec6SMatthias Ringwald                 log_info("Orig Signature: ");
59918707219SMatthias Ringwald                 log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8);
60018707219SMatthias Ringwald                 uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1);
60118707219SMatthias 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);
6023deb3ec6SMatthias Ringwald                 return;
6033deb3ec6SMatthias Ringwald             }
6047a766ebfSMatthias Ringwald #endif
605b06f2579SMatthias Ringwald             // move on
60618707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
607ed053a35SMatthias Ringwald             att_server_request_can_send_now(att_server);
608b06f2579SMatthias Ringwald             break;
60988a48dd8SMatthias Ringwald 
6103deb3ec6SMatthias Ringwald         default:
6113deb3ec6SMatthias Ringwald             break;
6123deb3ec6SMatthias Ringwald     }
6133deb3ec6SMatthias Ringwald }
6143deb3ec6SMatthias Ringwald 
61590f03c80SMatthias Ringwald static int att_server_data_ready_for_phase(att_server_t * att_server,  att_server_run_phase_t phase){
61690f03c80SMatthias Ringwald     switch (phase){
61790f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
61890f03c80SMatthias Ringwald             return att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
61990f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
62090f03c80SMatthias Ringwald              return (!btstack_linked_list_empty(&att_server->indication_requests) && att_server->value_indication_handle == 0);
62190f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
62290f03c80SMatthias Ringwald             return (!btstack_linked_list_empty(&att_server->notification_requests));
62390f03c80SMatthias Ringwald     }
624fa5e3464SMatthias Ringwald     // avoid warning
625fa5e3464SMatthias Ringwald     return 0;
62690f03c80SMatthias Ringwald }
62790f03c80SMatthias Ringwald 
62890f03c80SMatthias Ringwald static void att_server_trigger_send_for_phase(att_server_t * att_server,  att_server_run_phase_t phase){
62990f03c80SMatthias Ringwald     btstack_context_callback_registration_t * client;
63090f03c80SMatthias Ringwald     switch (phase){
63190f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
63290f03c80SMatthias Ringwald             att_server_process_validated_request(att_server);
63390f03c80SMatthias Ringwald             break;
63490f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
63590f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->indication_requests;
63690f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->indication_requests, (btstack_linked_item_t *) client);
63790f03c80SMatthias Ringwald             client->callback(client->context);
63890f03c80SMatthias Ringwald             break;
63990f03c80SMatthias Ringwald        case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
64090f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->notification_requests;
64190f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->notification_requests, (btstack_linked_item_t *) client);
64290f03c80SMatthias Ringwald             client->callback(client->context);
64390f03c80SMatthias Ringwald             break;
64490f03c80SMatthias Ringwald     }
64590f03c80SMatthias Ringwald }
64690f03c80SMatthias Ringwald 
6477eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){
648b06f2579SMatthias Ringwald 
649374a288aSMatthias Ringwald     hci_con_handle_t request_con_handle   = HCI_CON_HANDLE_INVALID;
6506438547aSMatthias Ringwald     hci_con_handle_t last_send_con_handle = HCI_CON_HANDLE_INVALID;
651*69d32c56SMatthias Ringwald     att_server_t *   request_att_server   = NULL;
6526438547aSMatthias Ringwald     int can_send_now = 1;
6534395a000SMatthias Ringwald     int phase_index;
6546438547aSMatthias Ringwald 
6554395a000SMatthias Ringwald     for (phase_index = ATT_SERVER_RUN_PHASE_1_REQUESTS; phase_index <= ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS; phase_index++){
6564395a000SMatthias Ringwald         att_server_run_phase_t phase = (att_server_run_phase_t) phase_index;
6576438547aSMatthias Ringwald         hci_con_handle_t skip_connections_until = att_server_last_can_send_now;
658374a288aSMatthias Ringwald         while (1){
6596438547aSMatthias Ringwald             btstack_linked_list_iterator_t it;
66018707219SMatthias Ringwald             hci_connections_get_iterator(&it);
66118707219SMatthias Ringwald             while(btstack_linked_list_iterator_has_next(&it)){
66218707219SMatthias Ringwald                 hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
66318707219SMatthias Ringwald                 att_server_t * att_server = &connection->att_server;
6646438547aSMatthias Ringwald 
66590f03c80SMatthias Ringwald                 int data_ready = att_server_data_ready_for_phase(att_server, phase);
6666438547aSMatthias Ringwald 
6676438547aSMatthias 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);
6686438547aSMatthias Ringwald 
6696438547aSMatthias Ringwald                 // skip until last sender found (which is also skipped)
6706438547aSMatthias Ringwald                 if (skip_connections_until != HCI_CON_HANDLE_INVALID){
6716438547aSMatthias Ringwald                     if (data_ready && request_con_handle == HCI_CON_HANDLE_INVALID){
6726438547aSMatthias Ringwald                         request_con_handle = att_server->connection.con_handle;
673*69d32c56SMatthias Ringwald                         request_att_server = att_server;
6746438547aSMatthias Ringwald                     }
6756438547aSMatthias Ringwald                     if (skip_connections_until == att_server->connection.con_handle){
6766438547aSMatthias Ringwald                         skip_connections_until = HCI_CON_HANDLE_INVALID;
6776438547aSMatthias Ringwald                     }
6786438547aSMatthias Ringwald                     continue;
6796438547aSMatthias Ringwald                 };
6806438547aSMatthias Ringwald 
68190f03c80SMatthias Ringwald                 if (data_ready){
682969a5bbaSMatthias Ringwald                     if (can_send_now){
68390f03c80SMatthias Ringwald                         att_server_trigger_send_for_phase(att_server, phase);
6846438547aSMatthias Ringwald                         last_send_con_handle = att_server->connection.con_handle;
685969a5bbaSMatthias Ringwald                         can_send_now = att_dispatch_server_can_send_now(att_server->connection.con_handle);
68690f03c80SMatthias Ringwald                         data_ready = att_server_data_ready_for_phase(att_server, phase);
6876438547aSMatthias Ringwald                         if (data_ready && request_con_handle == HCI_CON_HANDLE_INVALID){
688cbbb12d9SMatthias Ringwald                             request_con_handle = att_server->connection.con_handle;
689*69d32c56SMatthias Ringwald                             request_att_server = att_server;
690cbbb12d9SMatthias Ringwald                         }
691cbbb12d9SMatthias Ringwald                     } else {
692f29a88d8SMatthias Ringwald                         request_con_handle = att_server->connection.con_handle;
693*69d32c56SMatthias Ringwald                         request_att_server = att_server;
694f29a88d8SMatthias Ringwald                         break;
695cbbb12d9SMatthias Ringwald                     }
696cbbb12d9SMatthias Ringwald                 }
6973deb3ec6SMatthias Ringwald             }
6983deb3ec6SMatthias Ringwald 
6996438547aSMatthias Ringwald             // stop skipping (handles disconnect by last send connection)
7006438547aSMatthias Ringwald             skip_connections_until = HCI_CON_HANDLE_INVALID;
7016438547aSMatthias Ringwald 
7023551936eSMatthias Ringwald             // Exit loop, if we cannot send
703969a5bbaSMatthias Ringwald             if (!can_send_now) break;
704969a5bbaSMatthias Ringwald 
705969a5bbaSMatthias Ringwald             // Exit loop, if we can send but there are also no further request
706969a5bbaSMatthias Ringwald             if (request_con_handle == HCI_CON_HANDLE_INVALID) break;
707969a5bbaSMatthias Ringwald 
708969a5bbaSMatthias Ringwald             // Finally, if we still can send and there are requests, just try again
709969a5bbaSMatthias Ringwald             request_con_handle = HCI_CON_HANDLE_INVALID;
710*69d32c56SMatthias Ringwald             request_att_server = NULL;
711969a5bbaSMatthias Ringwald         }
7126438547aSMatthias Ringwald         // update last send con handle for round robin
7136438547aSMatthias Ringwald         if (last_send_con_handle != HCI_CON_HANDLE_INVALID){
7146438547aSMatthias Ringwald             att_server_last_can_send_now = last_send_con_handle;
7156438547aSMatthias Ringwald         }
7163551936eSMatthias Ringwald     }
717969a5bbaSMatthias Ringwald 
718969a5bbaSMatthias Ringwald     if (request_con_handle == HCI_CON_HANDLE_INVALID) return;
719*69d32c56SMatthias Ringwald     att_server_request_can_send_now(request_att_server);
720969a5bbaSMatthias Ringwald }
721969a5bbaSMatthias Ringwald 
72241772f37SMatthias Ringwald static void att_server_handle_att_pdu(att_server_t * att_server, uint8_t * packet, uint16_t size){
72318707219SMatthias Ringwald 
7243deb3ec6SMatthias Ringwald     // handle value indication confirms
72518707219SMatthias Ringwald     if (packet[0] == ATT_HANDLE_VALUE_CONFIRMATION && att_server->value_indication_handle){
72618707219SMatthias Ringwald         btstack_run_loop_remove_timer(&att_server->value_indication_timer);
72718707219SMatthias Ringwald         uint16_t att_handle = att_server->value_indication_handle;
72818707219SMatthias Ringwald         att_server->value_indication_handle = 0;
72918707219SMatthias Ringwald         att_handle_value_indication_notify_client(0, att_server->connection.con_handle, att_handle);
73041772f37SMatthias Ringwald         att_server_request_can_send_now(att_server);
7313deb3ec6SMatthias Ringwald         return;
7323deb3ec6SMatthias Ringwald     }
7333deb3ec6SMatthias Ringwald 
7346428eb5aSMatthias Ringwald     // directly process command
7356428eb5aSMatthias Ringwald     // note: signed write cannot be handled directly as authentication needs to be verified
7366428eb5aSMatthias Ringwald     if (packet[0] == ATT_WRITE_COMMAND){
73718707219SMatthias Ringwald         att_handle_request(&att_server->connection, packet, size, 0);
7386428eb5aSMatthias Ringwald         return;
7396428eb5aSMatthias Ringwald     }
7406428eb5aSMatthias Ringwald 
7413deb3ec6SMatthias Ringwald     // check size
74218707219SMatthias Ringwald     if (size > sizeof(att_server->request_buffer)) {
743e0463bdcSMatthias 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));
7446428eb5aSMatthias Ringwald         return;
7456428eb5aSMatthias Ringwald     }
7463deb3ec6SMatthias Ringwald 
747e0463bdcSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
748e0463bdcSMatthias Ringwald     // abort signed write validation if a new request comes in (but finish previous signed write if possible)
749e0463bdcSMatthias Ringwald     if (att_server->state == ATT_SERVER_W4_SIGNED_WRITE_VALIDATION){
750e0463bdcSMatthias Ringwald         if (packet[0] == ATT_SIGNED_WRITE_COMMAND){
751e0463bdcSMatthias Ringwald             log_info("skip new signed write request as previous is in validation");
752e0463bdcSMatthias Ringwald             return;
753e0463bdcSMatthias Ringwald         } else {
754e0463bdcSMatthias Ringwald             log_info("abort signed write validation to process new request");
755e0463bdcSMatthias Ringwald             att_server->state = ATT_SERVER_IDLE;
756e0463bdcSMatthias Ringwald         }
757e0463bdcSMatthias Ringwald     }
758e0463bdcSMatthias Ringwald #endif
7593deb3ec6SMatthias Ringwald     // last request still in processing?
76018707219SMatthias Ringwald     if (att_server->state != ATT_SERVER_IDLE){
761e0463bdcSMatthias Ringwald         log_info("skip att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state);
7626428eb5aSMatthias Ringwald         return;
7636428eb5aSMatthias Ringwald     }
7643deb3ec6SMatthias Ringwald 
7653deb3ec6SMatthias Ringwald     // store request
76618707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED;
76718707219SMatthias Ringwald     att_server->request_size = size;
76818707219SMatthias Ringwald     memcpy(att_server->request_buffer, packet, size);
7693deb3ec6SMatthias Ringwald 
77018707219SMatthias Ringwald     att_run_for_context(att_server);
77141772f37SMatthias Ringwald }
77241772f37SMatthias Ringwald 
77341772f37SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
77441772f37SMatthias Ringwald     att_server_t * att_server;
77541772f37SMatthias Ringwald 
77641772f37SMatthias Ringwald     switch (packet_type){
77741772f37SMatthias Ringwald         case HCI_EVENT_PACKET:
77841772f37SMatthias Ringwald             switch (packet[0]){
77941772f37SMatthias Ringwald                 case L2CAP_EVENT_CAN_SEND_NOW:
78041772f37SMatthias Ringwald                     att_server_handle_can_send_now();
78141772f37SMatthias Ringwald                     break;
78241772f37SMatthias Ringwald                 case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
78341772f37SMatthias Ringwald                     // GATT client has negotiated the mtu for this connection
78441772f37SMatthias Ringwald                     att_server = att_server_for_handle(handle);
78541772f37SMatthias Ringwald                     if (!att_server) break;
78641772f37SMatthias Ringwald                     att_server->connection.mtu = little_endian_read_16(packet, 4);
78741772f37SMatthias Ringwald                     break;
78841772f37SMatthias Ringwald                 default:
78941772f37SMatthias Ringwald                     break;
79041772f37SMatthias Ringwald             }
79141772f37SMatthias Ringwald             break;
79241772f37SMatthias Ringwald 
79341772f37SMatthias Ringwald         case ATT_DATA_PACKET:
79441772f37SMatthias Ringwald             log_debug("ATT Packet, handle 0x%04x", handle);
79541772f37SMatthias Ringwald             att_server = att_server_for_handle(handle);
79641772f37SMatthias Ringwald             if (!att_server) break;
79741772f37SMatthias Ringwald 
79841772f37SMatthias Ringwald             att_server_handle_att_pdu(att_server, packet, size);
799372d62c4SMatthias Ringwald             break;
800372d62c4SMatthias Ringwald     }
8013deb3ec6SMatthias Ringwald }
8023deb3ec6SMatthias Ringwald 
803bf78aac6SMatthias Ringwald // ---------------------
804bf78aac6SMatthias Ringwald // persistent CCC writes
805bf78aac6SMatthias Ringwald static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){
806bf78aac6SMatthias Ringwald     return 'B' << 24 | 'T' << 16 | 'C' << 8 | index;
807bf78aac6SMatthias Ringwald }
808bf78aac6SMatthias Ringwald 
809bf78aac6SMatthias Ringwald static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){
810bf78aac6SMatthias Ringwald     // lookup att_server instance
811bf78aac6SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
812bf78aac6SMatthias Ringwald     if (!att_server) return;
813bf78aac6SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
814bf78aac6SMatthias 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);
8156a540790SMatthias Ringwald 
816bf78aac6SMatthias Ringwald     // check if bonded
817bf78aac6SMatthias Ringwald     if (le_device_index < 0) return;
8186a540790SMatthias Ringwald 
819bf78aac6SMatthias Ringwald     // get btstack_tlv
820bf78aac6SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
821bf78aac6SMatthias Ringwald     void * tlv_context;
822bf78aac6SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
823bf78aac6SMatthias Ringwald     if (!tlv_impl) return;
8246a540790SMatthias Ringwald 
825bf78aac6SMatthias Ringwald     // update ccc tag
826bf78aac6SMatthias Ringwald     int index;
8276a540790SMatthias Ringwald     uint32_t highest_seq_nr = 0;
8286a540790SMatthias Ringwald     uint32_t lowest_seq_nr = 0;
8296a540790SMatthias Ringwald     uint32_t tag_for_lowest_seq_nr = 0;
8306a540790SMatthias Ringwald     uint32_t tag_for_empty = 0;
8316a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
8326afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
833bf78aac6SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
8346a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
8356a540790SMatthias Ringwald 
8366a540790SMatthias Ringwald         // empty/invalid tag
8376a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)){
8386a540790SMatthias Ringwald             tag_for_empty = tag;
839bf78aac6SMatthias Ringwald             continue;
840bf78aac6SMatthias Ringwald         }
8416a540790SMatthias Ringwald         // update highest seq nr
8426a540790SMatthias Ringwald         if (entry.seq_nr > highest_seq_nr){
8436a540790SMatthias Ringwald             highest_seq_nr = entry.seq_nr;
8446a540790SMatthias Ringwald         }
8456a540790SMatthias Ringwald         // find entry with lowest seq nr
8466a540790SMatthias Ringwald         if ((tag_for_lowest_seq_nr == 0) || (entry.seq_nr < lowest_seq_nr)){
8476a540790SMatthias Ringwald             tag_for_lowest_seq_nr = tag;
8486a540790SMatthias Ringwald             lowest_seq_nr = entry.seq_nr;
8496a540790SMatthias Ringwald         }
8506a540790SMatthias Ringwald 
8516a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
8526a540790SMatthias Ringwald         if (entry.att_handle   != att_handle)      continue;
8536a540790SMatthias Ringwald 
8546a540790SMatthias Ringwald         // found matching entry
85501ac2008SMatthias Ringwald         if (value){
856bf78aac6SMatthias Ringwald             // update
8576a540790SMatthias Ringwald             if (entry.value == value) {
858760ad805SMatthias Ringwald                 log_info("CCC Index %u: Up-to-date", index);
859760ad805SMatthias Ringwald                 return;
860760ad805SMatthias Ringwald             }
8616a540790SMatthias Ringwald             entry.value = value;
8626a540790SMatthias Ringwald             entry.seq_nr = highest_seq_nr + 1;
863760ad805SMatthias Ringwald             log_info("CCC Index %u: Store", index);
8646a540790SMatthias Ringwald             tlv_impl->store_tag(tlv_context, tag, (const uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
86501ac2008SMatthias Ringwald         } else {
86601ac2008SMatthias Ringwald             // delete
867760ad805SMatthias Ringwald             log_info("CCC Index %u: Delete", index);
86801ac2008SMatthias Ringwald             tlv_impl->delete_tag(tlv_context, tag);
86901ac2008SMatthias Ringwald         }
870bf78aac6SMatthias Ringwald         return;
871bf78aac6SMatthias Ringwald     }
8726a540790SMatthias Ringwald 
873faf2b6c3SMatthias Ringwald     log_info("tag_for_empy %"PRIx32", tag_for_lowest_seq_nr %"PRIx32, tag_for_empty, tag_for_lowest_seq_nr);
8746a540790SMatthias Ringwald 
8756a540790SMatthias Ringwald     if (value == 0){
8766a540790SMatthias Ringwald         // done
8776a540790SMatthias Ringwald         return;
8786a540790SMatthias Ringwald     }
8796a540790SMatthias Ringwald 
8806a540790SMatthias Ringwald     uint32_t tag_to_use = 0;
8816a540790SMatthias Ringwald     if (tag_for_empty){
8826a540790SMatthias Ringwald         tag_to_use = tag_for_empty;
8836a540790SMatthias Ringwald     } else if (tag_for_lowest_seq_nr){
8846a540790SMatthias Ringwald         tag_to_use = tag_for_lowest_seq_nr;
8856a540790SMatthias Ringwald     } else {
8866a540790SMatthias Ringwald         // should not happen
8876a540790SMatthias Ringwald         return;
8886a540790SMatthias Ringwald     }
889bf78aac6SMatthias Ringwald     // store ccc tag
8906a540790SMatthias Ringwald     entry.seq_nr       = highest_seq_nr + 1;
8916a540790SMatthias Ringwald     entry.device_index = le_device_index;
8926a540790SMatthias Ringwald     entry.att_handle   = att_handle;
8936a540790SMatthias Ringwald     entry.value        = value;
8946a540790SMatthias Ringwald     tlv_impl->store_tag(tlv_context, tag_to_use, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
895bf78aac6SMatthias Ringwald }
896bf78aac6SMatthias Ringwald 
897760ad805SMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server){
898760ad805SMatthias Ringwald     if (!att_server) return;
899760ad805SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
900760ad805SMatthias Ringwald     log_info("Clear CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
901760ad805SMatthias Ringwald     // check if bonded
902760ad805SMatthias Ringwald     if (le_device_index < 0) return;
903760ad805SMatthias Ringwald     // get btstack_tlv
904760ad805SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
905760ad805SMatthias Ringwald     void * tlv_context;
906760ad805SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
907760ad805SMatthias Ringwald     if (!tlv_impl) return;
908760ad805SMatthias Ringwald     // get all ccc tag
909760ad805SMatthias Ringwald     int index;
9106a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
9116afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
912760ad805SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
9136a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
9146a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
9156a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
916760ad805SMatthias Ringwald         // delete entry
917760ad805SMatthias Ringwald         log_info("CCC Index %u: Delete", index);
918760ad805SMatthias Ringwald         tlv_impl->delete_tag(tlv_context, tag);
919760ad805SMatthias Ringwald     }
920760ad805SMatthias Ringwald }
921760ad805SMatthias Ringwald 
92201ac2008SMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server){
92301ac2008SMatthias Ringwald     if (!att_server) return;
92401ac2008SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
92501ac2008SMatthias Ringwald     log_info("Restore CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
9261b7acd0dSMatthias Ringwald     // check if bonded
9271b7acd0dSMatthias Ringwald     if (le_device_index < 0) return;
92801ac2008SMatthias Ringwald     // get btstack_tlv
92901ac2008SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
93001ac2008SMatthias Ringwald     void * tlv_context;
93101ac2008SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
93201ac2008SMatthias Ringwald     if (!tlv_impl) return;
93301ac2008SMatthias Ringwald     // get all ccc tag
93401ac2008SMatthias Ringwald     int index;
9356a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
9366afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
93701ac2008SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
9386a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
9396a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
9406a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
94101ac2008SMatthias Ringwald         // simulate write callback
9426a540790SMatthias Ringwald         uint16_t attribute_handle = entry.att_handle;
94301ac2008SMatthias Ringwald         uint8_t  value[2];
9446a540790SMatthias Ringwald         little_endian_store_16(value, 0, entry.value);
94501ac2008SMatthias Ringwald         att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
94601ac2008SMatthias Ringwald         if (!callback) continue;
9476a540790SMatthias Ringwald         log_info("CCC Index %u: Set Attribute handle 0x%04x to value 0x%04x", index, attribute_handle, entry.value );
94801ac2008SMatthias Ringwald         (*callback)(att_server->connection.con_handle, attribute_handle, ATT_TRANSACTION_MODE_NONE, 0, value, sizeof(value));
94901ac2008SMatthias Ringwald     }
95001ac2008SMatthias Ringwald }
95101ac2008SMatthias Ringwald 
952bf78aac6SMatthias Ringwald // persistent CCC writes
953bf78aac6SMatthias Ringwald // ---------------------
9543d71c7a4SMatthias Ringwald 
9553d71c7a4SMatthias Ringwald // gatt service management
9563d71c7a4SMatthias Ringwald static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){
9573d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
9583d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
9593d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
9603d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
9613d71c7a4SMatthias Ringwald         if (handler->start_handle > handle) continue;
9623d71c7a4SMatthias Ringwald         if (handler->end_handle   < handle) continue;
9633d71c7a4SMatthias Ringwald         return handler;
9643d71c7a4SMatthias Ringwald     }
9653d71c7a4SMatthias Ringwald     return NULL;
9663d71c7a4SMatthias Ringwald }
9673d71c7a4SMatthias Ringwald static att_read_callback_t att_server_read_callback_for_handle(uint16_t handle){
9683d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
9693d71c7a4SMatthias Ringwald     if (handler) return handler->read_callback;
9703d71c7a4SMatthias Ringwald     return att_server_client_read_callback;
9713d71c7a4SMatthias Ringwald }
9723d71c7a4SMatthias Ringwald 
9733d71c7a4SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){
9743d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
9753d71c7a4SMatthias Ringwald     if (handler) return handler->write_callback;
9763d71c7a4SMatthias Ringwald     return att_server_client_write_callback;
9773d71c7a4SMatthias Ringwald }
9783d71c7a4SMatthias Ringwald 
9795d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle){
9805d07396bSMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
9815d07396bSMatthias Ringwald     if (handler) return handler->packet_handler;
9825d07396bSMatthias Ringwald     return att_client_packet_handler;
9835d07396bSMatthias Ringwald }
9845d07396bSMatthias Ringwald 
9853d71c7a4SMatthias Ringwald static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){
9863d71c7a4SMatthias Ringwald     // notify all callbacks
9873d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
9883d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
9893d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
9903d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
9913d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
9923d71c7a4SMatthias Ringwald         (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
9933d71c7a4SMatthias Ringwald     }
9943d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return;
9953d71c7a4SMatthias Ringwald     (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
9963d71c7a4SMatthias Ringwald }
9973d71c7a4SMatthias Ringwald 
9983d71c7a4SMatthias Ringwald // returns first reported error or 0
9993d71c7a4SMatthias Ringwald static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){
10003d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
10013d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
10023d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
10033d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
10043d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
10053d71c7a4SMatthias Ringwald         uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
10063d71c7a4SMatthias Ringwald         if (error_code) return error_code;
10073d71c7a4SMatthias Ringwald     }
10083d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return 0;
10093d71c7a4SMatthias Ringwald     return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
10103d71c7a4SMatthias Ringwald }
10113d71c7a4SMatthias Ringwald 
10123d71c7a4SMatthias 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){
10133d71c7a4SMatthias Ringwald     att_read_callback_t callback = att_server_read_callback_for_handle(attribute_handle);
101401ac2008SMatthias Ringwald     if (!callback) return 0;
10153d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, offset, buffer, buffer_size);
10163d71c7a4SMatthias Ringwald }
10173d71c7a4SMatthias Ringwald 
10183d71c7a4SMatthias 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){
10193d71c7a4SMatthias Ringwald     switch (transaction_mode){
10203d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_VALIDATE:
10213d71c7a4SMatthias Ringwald             return att_validate_prepared_write(con_handle);
10223d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_EXECUTE:
10233d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_CANCEL:
10243d71c7a4SMatthias Ringwald             att_notify_write_callbacks(con_handle, transaction_mode);
10253d71c7a4SMatthias Ringwald             return 0;
10263d71c7a4SMatthias Ringwald         default:
10273d71c7a4SMatthias Ringwald             break;
10283d71c7a4SMatthias Ringwald     }
10293d71c7a4SMatthias Ringwald 
1030bf78aac6SMatthias Ringwald     // track CCC writes
1031bf78aac6SMatthias Ringwald     if (att_is_persistent_ccc(attribute_handle) && offset == 0 && buffer_size == 2){
1032bf78aac6SMatthias Ringwald         att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0));
1033bf78aac6SMatthias Ringwald     }
1034bf78aac6SMatthias Ringwald 
103501ac2008SMatthias Ringwald     att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
103601ac2008SMatthias Ringwald     if (!callback) return 0;
10373d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size);
10383d71c7a4SMatthias Ringwald }
10393d71c7a4SMatthias Ringwald 
10403d71c7a4SMatthias Ringwald /**
10413d71c7a4SMatthias Ringwald  * @brief register read/write callbacks for specific handle range
10423d71c7a4SMatthias Ringwald  * @param att_service_handler_t
10433d71c7a4SMatthias Ringwald  */
10443d71c7a4SMatthias Ringwald void att_server_register_service_handler(att_service_handler_t * handler){
10453d71c7a4SMatthias Ringwald     if (att_service_handler_for_handle(handler->start_handle) ||
10463d71c7a4SMatthias Ringwald         att_service_handler_for_handle(handler->end_handle)){
10473d71c7a4SMatthias Ringwald         log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle);
10483d71c7a4SMatthias Ringwald         return;
10493d71c7a4SMatthias Ringwald     }
10503d71c7a4SMatthias Ringwald     btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler);
10513d71c7a4SMatthias Ringwald }
10523d71c7a4SMatthias Ringwald 
10533deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){
10543deb3ec6SMatthias Ringwald 
10553d71c7a4SMatthias Ringwald     // store callbacks
10563d71c7a4SMatthias Ringwald     att_server_client_read_callback  = read_callback;
10573d71c7a4SMatthias Ringwald     att_server_client_write_callback = write_callback;
10583d71c7a4SMatthias Ringwald 
10591a389cdcSMatthias Ringwald     // register for HCI Events
1060d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &att_event_packet_handler;
10611a389cdcSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
10621a389cdcSMatthias Ringwald 
1063406722e4SMatthias Ringwald     // register for SM events
1064d9a7306aSMatthias Ringwald     sm_event_callback_registration.callback = &att_event_packet_handler;
1065406722e4SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
10663deb3ec6SMatthias Ringwald 
10671a389cdcSMatthias Ringwald     // and L2CAP ATT Server PDUs
10683deb3ec6SMatthias Ringwald     att_dispatch_register_server(att_packet_handler);
10693deb3ec6SMatthias Ringwald 
107070dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
107170dca4d4SMatthias Ringwald     // setup l2cap service
107270dca4d4SMatthias Ringwald     l2cap_register_service(&att_event_packet_handler, PSM_ATT, 0xffff, LEVEL_2);
107370dca4d4SMatthias Ringwald #endif
107470dca4d4SMatthias Ringwald 
10753deb3ec6SMatthias Ringwald     att_set_db(db);
10763d71c7a4SMatthias Ringwald     att_set_read_callback(att_server_read_callback);
10773d71c7a4SMatthias Ringwald     att_set_write_callback(att_server_write_callback);
10783deb3ec6SMatthias Ringwald }
10793deb3ec6SMatthias Ringwald 
10803deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){
10813deb3ec6SMatthias Ringwald     att_client_packet_handler = handler;
10823deb3ec6SMatthias Ringwald }
10833deb3ec6SMatthias Ringwald 
1084cbbb12d9SMatthias Ringwald 
1085cbbb12d9SMatthias Ringwald // to be deprecated
1086c141988cSMatthias Ringwald int  att_server_can_send_packet_now(hci_con_handle_t con_handle){
108718707219SMatthias Ringwald     return att_dispatch_server_can_send_now(con_handle);
10881b96b007SMatthias Ringwald }
10891b96b007SMatthias Ringwald 
1090cbbb12d9SMatthias Ringwald int att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
1091cbbb12d9SMatthias Ringwald     return att_server_request_to_send_notification(callback_registration, con_handle);
10923deb3ec6SMatthias Ringwald }
10933deb3ec6SMatthias Ringwald 
1094cbbb12d9SMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){
1095cbbb12d9SMatthias Ringwald     att_client_waiting_for_can_send_registration.callback = &att_emit_can_send_now_event;
1096cbbb12d9SMatthias Ringwald     att_server_request_to_send_notification(&att_client_waiting_for_can_send_registration, con_handle);
1097cbbb12d9SMatthias Ringwald }
1098cbbb12d9SMatthias Ringwald // end of deprecated
1099cbbb12d9SMatthias Ringwald 
1100cbbb12d9SMatthias Ringwald int att_server_request_to_send_notification(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
1101969a5bbaSMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
1102969a5bbaSMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1103cbbb12d9SMatthias Ringwald     btstack_linked_list_add_tail(&att_server->notification_requests, (btstack_linked_item_t*) callback_registration);
1104cbbb12d9SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
1105cbbb12d9SMatthias Ringwald     return ERROR_CODE_SUCCESS;
1106cbbb12d9SMatthias Ringwald }
1107cbbb12d9SMatthias Ringwald 
1108cbbb12d9SMatthias Ringwald int att_server_request_to_send_indication(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
1109cbbb12d9SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
1110cbbb12d9SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1111cbbb12d9SMatthias Ringwald     btstack_linked_list_add_tail(&att_server->indication_requests, (btstack_linked_item_t*) callback_registration);
1112bb38f057SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
1113969a5bbaSMatthias Ringwald     return ERROR_CODE_SUCCESS;
1114bb38f057SMatthias Ringwald }
1115bb38f057SMatthias Ringwald 
11163bb3035fSMilanka Ringwald int att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){
111718707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
111818707219SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
111918707219SMatthias Ringwald 
112018707219SMatthias Ringwald     if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
11213deb3ec6SMatthias Ringwald 
11223deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
11233deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
112418707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_notification(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
112518707219SMatthias Ringwald 	return l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
11263deb3ec6SMatthias Ringwald }
11273deb3ec6SMatthias Ringwald 
11283bb3035fSMilanka Ringwald int att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){
112918707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
113018707219SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
113118707219SMatthias Ringwald 
1132eaac31e8SMatthias Ringwald     if (att_server->value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS;
113318707219SMatthias Ringwald     if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
11343deb3ec6SMatthias Ringwald 
11353deb3ec6SMatthias Ringwald     // track indication
113618707219SMatthias Ringwald     att_server->value_indication_handle = attribute_handle;
113718707219SMatthias Ringwald     btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout);
113818707219SMatthias Ringwald     btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS);
113918707219SMatthias Ringwald     btstack_run_loop_add_timer(&att_server->value_indication_timer);
11403deb3ec6SMatthias Ringwald 
11413deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
11423deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
114318707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_indication(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
114418707219SMatthias Ringwald 	l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
11453deb3ec6SMatthias Ringwald     return 0;
11463deb3ec6SMatthias Ringwald }
11478f9132b5SMilanka Ringwald 
11488f9132b5SMilanka Ringwald uint16_t att_server_get_mtu(hci_con_handle_t con_handle){
11498f9132b5SMilanka Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
11508f9132b5SMilanka Ringwald     if (!att_server) return 0;
11518f9132b5SMilanka Ringwald     return att_server->connection.mtu;
11528f9132b5SMilanka Ringwald }
1153