xref: /btstack/src/ble/att_server.c (revision c0f26e1cb28368abbe56c35baff72aa15eead594)
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 
140c8c6e9b4SMatthias Ringwald static void att_server_request_can_send_now(att_server_t * att_server){
141c8c6e9b4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
142c8c6e9b4SMatthias Ringwald     if (att_server->l2cap_cid != 0){
143c8c6e9b4SMatthias Ringwald         l2cap_request_can_send_now_event(att_server->l2cap_cid);
144c8c6e9b4SMatthias Ringwald         return;
145c8c6e9b4SMatthias Ringwald     }
146c8c6e9b4SMatthias Ringwald #endif
147c8c6e9b4SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
148c8c6e9b4SMatthias Ringwald }
149c8c6e9b4SMatthias Ringwald 
150*c0f26e1cSMatthias Ringwald static int att_server_can_send_packet(att_server_t * att_server){
151*c0f26e1cSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
152*c0f26e1cSMatthias Ringwald     if (att_server->l2cap_cid != 0){
153*c0f26e1cSMatthias Ringwald         return l2cap_can_send_packet_now(att_server->l2cap_cid);
154*c0f26e1cSMatthias Ringwald     }
155*c0f26e1cSMatthias Ringwald #endif
156*c0f26e1cSMatthias Ringwald     return att_dispatch_server_can_send_now(att_server->connection.con_handle);
157*c0f26e1cSMatthias Ringwald }
158*c0f26e1cSMatthias Ringwald 
1593deb3ec6SMatthias Ringwald static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){
1605d07396bSMatthias Ringwald     btstack_packet_handler_t packet_handler = att_server_packet_handler_for_handle(attribute_handle);
1615d07396bSMatthias Ringwald     if (!packet_handler) return;
1623deb3ec6SMatthias Ringwald 
1633deb3ec6SMatthias Ringwald     uint8_t event[7];
1643deb3ec6SMatthias Ringwald     int pos = 0;
1655611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE;
1663deb3ec6SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
1673deb3ec6SMatthias Ringwald     event[pos++] = status;
168f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, client_handle);
1693deb3ec6SMatthias Ringwald     pos += 2;
170f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, attribute_handle);
171a444112bSMatthias Ringwald     (*packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1723deb3ec6SMatthias Ringwald }
1733deb3ec6SMatthias Ringwald 
1745d07396bSMatthias Ringwald static void att_emit_event_to_all(const uint8_t * event, uint16_t size){
1755d07396bSMatthias Ringwald     // dispatch to app level handler
1765d07396bSMatthias Ringwald     if (att_client_packet_handler){
1775d07396bSMatthias Ringwald         (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size);
1785d07396bSMatthias Ringwald     }
1793deb3ec6SMatthias Ringwald 
1805d07396bSMatthias Ringwald     // dispatch to service handlers
1815d07396bSMatthias Ringwald     btstack_linked_list_iterator_t it;
1825d07396bSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
1835d07396bSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1845d07396bSMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
1855d07396bSMatthias Ringwald         if (!handler->packet_handler) continue;
1865d07396bSMatthias Ringwald         (*handler->packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size);
1875d07396bSMatthias Ringwald     }
1885d07396bSMatthias Ringwald }
1895d07396bSMatthias Ringwald 
1905d07396bSMatthias Ringwald static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){
1913deb3ec6SMatthias Ringwald     uint8_t event[6];
1923deb3ec6SMatthias Ringwald     int pos = 0;
1935611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE;
1943deb3ec6SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
195fc64f94aSMatthias Ringwald     little_endian_store_16(event, pos, con_handle);
1963deb3ec6SMatthias Ringwald     pos += 2;
197f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, mtu);
1985d07396bSMatthias Ringwald 
1995d07396bSMatthias Ringwald     // also dispatch to GATT Clients
200b12646c5SJakob Krantz     att_dispatch_server_mtu_exchanged(con_handle, mtu);
2015d07396bSMatthias Ringwald 
2025d07396bSMatthias Ringwald     // dispatch to app level handler and service handlers
2035d07396bSMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
2043deb3ec6SMatthias Ringwald }
2053deb3ec6SMatthias Ringwald 
2065f141511SMatthias Ringwald static void att_emit_can_send_now_event(void * context){
2075f141511SMatthias Ringwald     UNUSED(context);
2083c97b242SMatthias Ringwald     if (!att_client_packet_handler) return;
2093c97b242SMatthias Ringwald 
2101b96b007SMatthias Ringwald     uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0};
2111b96b007SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
2121b96b007SMatthias Ringwald }
2131b96b007SMatthias Ringwald 
214ff1bec95SMatthias Ringwald static void att_emit_connected_event(att_server_t * att_server){
215ff1bec95SMatthias Ringwald     uint8_t event[11];
216ff1bec95SMatthias Ringwald     int pos = 0;
217ff1bec95SMatthias Ringwald     event[pos++] = ATT_EVENT_CONNECTED;
218ff1bec95SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
219ff1bec95SMatthias Ringwald     event[pos++] = att_server->peer_addr_type;
220ff1bec95SMatthias Ringwald     reverse_bd_addr(att_server->peer_address, &event[pos]);
221ff1bec95SMatthias Ringwald     pos += 6;
222ff1bec95SMatthias Ringwald     little_endian_store_16(event, pos, att_server->connection.con_handle);
223ff1bec95SMatthias Ringwald     pos += 2;
224ff1bec95SMatthias Ringwald 
225ff1bec95SMatthias Ringwald     // dispatch to app level handler and service handlers
226ff1bec95SMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
227ff1bec95SMatthias Ringwald }
228ff1bec95SMatthias Ringwald 
229ff1bec95SMatthias Ringwald 
2306187ad4cSMatthias Ringwald static void att_emit_disconnected_event(uint16_t con_handle){
231ff1bec95SMatthias Ringwald     uint8_t event[4];
232ff1bec95SMatthias Ringwald     int pos = 0;
233ff1bec95SMatthias Ringwald     event[pos++] = ATT_EVENT_DISCONNECTED;
234ff1bec95SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
2356187ad4cSMatthias Ringwald     little_endian_store_16(event, pos, con_handle);
236ff1bec95SMatthias Ringwald     pos += 2;
237ff1bec95SMatthias Ringwald 
238ff1bec95SMatthias Ringwald     // dispatch to app level handler and service handlers
239ff1bec95SMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
240ff1bec95SMatthias Ringwald }
241ff1bec95SMatthias Ringwald 
242ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){
24354178543SMatthias Ringwald     void * context = btstack_run_loop_get_timer_context(ts);
24454178543SMatthias Ringwald     hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
24518707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
24618707219SMatthias Ringwald     if (!att_server) return;
247bce48a26SMatthias Ringwald     // @note: after a transcation timeout, no more requests shall be sent over this ATT Bearer
248bce48a26SMatthias Ringwald     // (that's why we don't reset the value_indication_handle)
24918707219SMatthias Ringwald     uint16_t att_handle = att_server->value_indication_handle;
25018707219SMatthias Ringwald     att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_server->connection.con_handle, att_handle);
2513deb3ec6SMatthias Ringwald }
2523deb3ec6SMatthias Ringwald 
2533deb3ec6SMatthias Ringwald static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2549ec2630cSMatthias Ringwald 
2557dad9ff8SMatthias Ringwald     UNUSED(channel); // ok: there is no channel
2567dad9ff8SMatthias Ringwald     UNUSED(size);    // ok: handling own l2cap events
2573deb3ec6SMatthias Ringwald 
25818707219SMatthias Ringwald     att_server_t * att_server;
25918707219SMatthias Ringwald     hci_con_handle_t con_handle;
26070dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
26170dca4d4SMatthias Ringwald     bd_addr_t address;
26270dca4d4SMatthias Ringwald #endif
26318707219SMatthias Ringwald 
2643deb3ec6SMatthias Ringwald     switch (packet_type) {
2653deb3ec6SMatthias Ringwald 
2663deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
2670e2df43fSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
2683deb3ec6SMatthias Ringwald 
26970dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
27070dca4d4SMatthias Ringwald                 case L2CAP_EVENT_INCOMING_CONNECTION:
27170dca4d4SMatthias Ringwald                     l2cap_event_incoming_connection_get_address(packet, address);
27270dca4d4SMatthias Ringwald                     l2cap_accept_connection(channel);
27370dca4d4SMatthias Ringwald                     printf("Accept incoming connection from %s\n", bd_addr_to_str(address));
27470dca4d4SMatthias Ringwald                     break;
27570dca4d4SMatthias Ringwald                 case L2CAP_EVENT_CHANNEL_OPENED:
276122b723eSMatthias Ringwald                     con_handle = l2cap_event_channel_opened_get_handle(packet);
277122b723eSMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
27870dca4d4SMatthias Ringwald                     if (!att_server) break;
279122b723eSMatthias Ringwald                     // store connection info
280122b723eSMatthias Ringwald                     att_server->peer_addr_type = BD_ADDR_TYPE_CLASSIC;
281122b723eSMatthias Ringwald                     l2cap_event_channel_opened_get_address(packet, att_server->peer_address);
282122b723eSMatthias Ringwald                     att_server->connection.con_handle = con_handle;
28370dca4d4SMatthias Ringwald                     att_server->l2cap_cid = l2cap_event_channel_opened_get_local_cid(packet);
284122b723eSMatthias Ringwald                     // reset connection properties
285122b723eSMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
286122b723eSMatthias Ringwald                     att_server->connection.mtu = l2cap_event_channel_opened_get_remote_mtu(packet);
287122b723eSMatthias Ringwald                     att_server->connection.max_mtu = l2cap_max_mtu();
288122b723eSMatthias Ringwald                     if (att_server->connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){
289122b723eSMatthias Ringwald                         att_server->connection.max_mtu = ATT_REQUEST_BUFFER_SIZE;
290122b723eSMatthias Ringwald                     }
291122b723eSMatthias Ringwald                     // TODO: use security level from underlying HCI connection
292122b723eSMatthias Ringwald                     att_server->connection.encryption_key_size = 0;
293122b723eSMatthias Ringwald                     att_server->connection.authenticated = 0;
294122b723eSMatthias Ringwald                     att_server->connection.authorized = 0;
295122b723eSMatthias Ringwald                     // TODO: what to do about le device db?
296122b723eSMatthias Ringwald                     att_server->pairing_active = 0;
297122b723eSMatthias Ringwald                     printf("Connection opened %s, l2cap cid %04x, \n", bd_addr_to_str(address), att_server->l2cap_cid);
29870dca4d4SMatthias Ringwald                     break;
29970dca4d4SMatthias Ringwald #endif
3003deb3ec6SMatthias Ringwald                 case HCI_EVENT_LE_META:
3013deb3ec6SMatthias Ringwald                     switch (packet[2]) {
3023deb3ec6SMatthias Ringwald                         case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
30318707219SMatthias Ringwald                             con_handle = little_endian_read_16(packet, 4);
30418707219SMatthias Ringwald                             att_server = att_server_for_handle(con_handle);
30518707219SMatthias Ringwald                             if (!att_server) break;
3063deb3ec6SMatthias Ringwald                         	// store connection info
30718707219SMatthias Ringwald                         	att_server->peer_addr_type = packet[7];
30818707219SMatthias Ringwald                             reverse_bd_addr(&packet[8], att_server->peer_address);
30918707219SMatthias Ringwald                             att_server->connection.con_handle = con_handle;
3103deb3ec6SMatthias Ringwald                             // reset connection properties
31118707219SMatthias Ringwald                             att_server->state = ATT_SERVER_IDLE;
31218707219SMatthias Ringwald                             att_server->connection.mtu = ATT_DEFAULT_MTU;
31318707219SMatthias Ringwald                             att_server->connection.max_mtu = l2cap_max_le_mtu();
31418707219SMatthias Ringwald                             if (att_server->connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){
31518707219SMatthias Ringwald                                 att_server->connection.max_mtu = ATT_REQUEST_BUFFER_SIZE;
31699c58ad0SMatthias Ringwald                             }
31718707219SMatthias Ringwald                             att_server->connection.encryption_key_size = 0;
31818707219SMatthias Ringwald                             att_server->connection.authenticated = 0;
31918707219SMatthias Ringwald 		                	att_server->connection.authorized = 0;
320b2c1e52cSMatthias Ringwald                             // workaround: identity resolving can already be complete, at least store result
321b2c1e52cSMatthias Ringwald                             att_server->ir_le_device_db_index = sm_le_device_index(con_handle);
322ed053a35SMatthias Ringwald                             att_server->ir_lookup_active = 0;
323760ad805SMatthias Ringwald                             att_server->pairing_active = 0;
324ff1bec95SMatthias Ringwald                             // notify all - old
3255d07396bSMatthias Ringwald                             att_emit_event_to_all(packet, size);
326ff1bec95SMatthias Ringwald                             // notify all - new
327ff1bec95SMatthias Ringwald                             att_emit_connected_event(att_server);
3283deb3ec6SMatthias Ringwald                             break;
3293deb3ec6SMatthias Ringwald 
3303deb3ec6SMatthias Ringwald                         default:
3313deb3ec6SMatthias Ringwald                             break;
3323deb3ec6SMatthias Ringwald                     }
3333deb3ec6SMatthias Ringwald                     break;
3343deb3ec6SMatthias Ringwald 
3353deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_CHANGE:
3363deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE:
3373deb3ec6SMatthias Ringwald                 	// check handle
33818707219SMatthias Ringwald                     con_handle = little_endian_read_16(packet, 3);
33918707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
34018707219SMatthias Ringwald                     if (!att_server) break;
3419c6e867eSMatthias Ringwald                     att_server->connection.encryption_key_size = gap_encryption_key_size(con_handle);
3429c6e867eSMatthias Ringwald                     att_server->connection.authenticated = gap_authenticated(con_handle);
34313eb7322SMatthias Ringwald                     att_server->connection.secure_connection = gap_secure_connection(con_handle);
34413eb7322SMatthias Ringwald                     log_info("encrypted key size %u, authenticated %u, secure connectipon %u",
34513eb7322SMatthias Ringwald                         att_server->connection.encryption_key_size, att_server->connection.authenticated, att_server->connection.secure_connection);
34601ac2008SMatthias Ringwald                     if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){
34701ac2008SMatthias Ringwald                         // restore CCC values when encrypted
34801ac2008SMatthias Ringwald                         if (hci_event_encryption_change_get_encryption_enabled(packet)){
34901ac2008SMatthias Ringwald                             att_server_persistent_ccc_restore(att_server);
35001ac2008SMatthias Ringwald                         }
35101ac2008SMatthias Ringwald                     }
3520234fbbcSMatthias Ringwald                     att_run_for_context(att_server);
3533deb3ec6SMatthias Ringwald                     break;
3543deb3ec6SMatthias Ringwald 
3553deb3ec6SMatthias Ringwald                 case HCI_EVENT_DISCONNECTION_COMPLETE:
35670a390c7SMatthias Ringwald                     // check handle
35718707219SMatthias Ringwald                     con_handle = hci_event_disconnection_complete_get_connection_handle(packet);
35818707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
35918707219SMatthias Ringwald                     if (!att_server) break;
36018707219SMatthias Ringwald                     att_clear_transaction_queue(&att_server->connection);
36118707219SMatthias Ringwald                     att_server->connection.con_handle = 0;
362760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
36318707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
364bce48a26SMatthias Ringwald                     if (att_server->value_indication_handle){
36527328ecbSMatthias Ringwald                         btstack_run_loop_remove_timer(&att_server->value_indication_timer);
366bce48a26SMatthias Ringwald                         uint16_t att_handle = att_server->value_indication_handle;
367bce48a26SMatthias Ringwald                         att_server->value_indication_handle = 0; // reset error state
368bce48a26SMatthias Ringwald                         att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_DISCONNECT, att_server->connection.con_handle, att_handle);
369bce48a26SMatthias Ringwald                     }
370ff1bec95SMatthias Ringwald                     // notify all - new
3716187ad4cSMatthias Ringwald                     att_emit_disconnected_event(con_handle);
372ff1bec95SMatthias Ringwald                     // notify all - old
3735d07396bSMatthias Ringwald                     att_emit_event_to_all(packet, size);
3743deb3ec6SMatthias Ringwald                     break;
3753deb3ec6SMatthias Ringwald 
376760ad805SMatthias Ringwald                 // Identity Resolving
3775611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_STARTED:
37818707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_started_get_handle(packet);
37918707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
38018707219SMatthias Ringwald                     if (!att_server) break;
3815611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED");
38218707219SMatthias Ringwald                     att_server->ir_lookup_active = 1;
3833deb3ec6SMatthias Ringwald                     break;
3845611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
385760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
386760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
387760ad805SMatthias Ringwald                     if (!att_server) return;
3881b7acd0dSMatthias Ringwald                     att_server->ir_lookup_active = 0;
3891b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index(packet);
3901b7acd0dSMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED");
3911b7acd0dSMatthias Ringwald                     att_run_for_context(att_server);
3923deb3ec6SMatthias Ringwald                     break;
3935611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_FAILED:
39418707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_failed_get_handle(packet);
39518707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
39618707219SMatthias Ringwald                     if (!att_server) break;
3975611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED");
39818707219SMatthias Ringwald                     att_server->ir_lookup_active = 0;
39918707219SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
40018707219SMatthias Ringwald                     att_run_for_context(att_server);
4013deb3ec6SMatthias Ringwald                     break;
402760ad805SMatthias Ringwald 
403760ad805SMatthias Ringwald                 // Pairing started - delete stored CCC values
404760ad805SMatthias Ringwald                 // - assumes pairing indicates either new device or re-pairing, in both cases there should be no stored CCC values
405760ad805SMatthias Ringwald                 // - assumes that all events have the con handle as the first field
406760ad805SMatthias Ringwald                 case SM_EVENT_JUST_WORKS_REQUEST:
407760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
408760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_INPUT_NUMBER:
409760ad805SMatthias Ringwald                 case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
410760ad805SMatthias Ringwald                     con_handle = sm_event_just_works_request_get_handle(packet);
411760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
412760ad805SMatthias Ringwald                     if (!att_server) break;
413760ad805SMatthias Ringwald                     att_server->pairing_active = 1;
414760ad805SMatthias Ringwald                     log_info("SM Pairing started");
415760ad805SMatthias Ringwald                     if (att_server->ir_le_device_db_index < 0) break;
416760ad805SMatthias Ringwald                     att_server_persistent_ccc_clear(att_server);
417760ad805SMatthias Ringwald                     // index not valid anymore
418760ad805SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
419760ad805SMatthias Ringwald                     break;
420760ad805SMatthias Ringwald 
4211b7acd0dSMatthias Ringwald                 // Bonding completed
422760ad805SMatthias Ringwald                 case SM_EVENT_IDENTITY_CREATED:
423760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
424760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
425760ad805SMatthias Ringwald                     if (!att_server) return;
426760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
4271b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_created_get_index(packet);
4281b7acd0dSMatthias Ringwald                     att_run_for_context(att_server);
4291b7acd0dSMatthias Ringwald                     break;
4301b7acd0dSMatthias Ringwald 
4311b7acd0dSMatthias Ringwald                 // Pairing complete (with/without bonding=storing of pairing information)
4321b7acd0dSMatthias Ringwald                 case SM_EVENT_PAIRING_COMPLETE:
4331b7acd0dSMatthias Ringwald                     con_handle = sm_event_pairing_complete_get_handle(packet);
4341b7acd0dSMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
4351b7acd0dSMatthias Ringwald                     if (!att_server) return;
4361b7acd0dSMatthias Ringwald                     att_server->pairing_active = 0;
4371b7acd0dSMatthias Ringwald                     att_run_for_context(att_server);
438760ad805SMatthias Ringwald                     break;
439760ad805SMatthias Ringwald 
440760ad805SMatthias Ringwald                 // Authorization
4415611a760SMatthias Ringwald                 case SM_EVENT_AUTHORIZATION_RESULT: {
44218707219SMatthias Ringwald                     con_handle = sm_event_authorization_result_get_handle(packet);
44318707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
44418707219SMatthias Ringwald                     if (!att_server) break;
44518707219SMatthias Ringwald                     att_server->connection.authorized = sm_event_authorization_result_get_authorization_result(packet);
446c8c6e9b4SMatthias Ringwald                     att_server_request_can_send_now(att_server);
4473deb3ec6SMatthias Ringwald                 	break;
4483deb3ec6SMatthias Ringwald                 }
4493deb3ec6SMatthias Ringwald                 default:
4503deb3ec6SMatthias Ringwald                     break;
4513deb3ec6SMatthias Ringwald             }
45265b44ffdSMatthias Ringwald             break;
45370dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
45470dca4d4SMatthias Ringwald         case L2CAP_DATA_PACKET:
45570dca4d4SMatthias Ringwald             att_server = att_server_for_l2cap_cid(channel);
45670dca4d4SMatthias Ringwald             if (!att_server) break;
45741772f37SMatthias Ringwald 
45841772f37SMatthias Ringwald             att_server_handle_att_pdu(att_server, packet, size);
45970dca4d4SMatthias Ringwald             break;
46070dca4d4SMatthias Ringwald #endif
46141772f37SMatthias Ringwald 
46265b44ffdSMatthias Ringwald         default:
46365b44ffdSMatthias Ringwald             break;
4643deb3ec6SMatthias Ringwald     }
4653deb3ec6SMatthias Ringwald }
4663deb3ec6SMatthias Ringwald 
4677a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
4683deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
4693deb3ec6SMatthias Ringwald 
47018707219SMatthias Ringwald     att_server_t * att_server = att_server_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION);
47118707219SMatthias Ringwald     if (!att_server) return;
4723deb3ec6SMatthias Ringwald 
4733deb3ec6SMatthias Ringwald     uint8_t hash_flipped[8];
4749c80e4ccSMatthias Ringwald     reverse_64(hash, hash_flipped);
47518707219SMatthias Ringwald     if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8)){
4763deb3ec6SMatthias Ringwald         log_info("ATT Signed Write, invalid signature");
47718707219SMatthias Ringwald         att_server->state = ATT_SERVER_IDLE;
4783deb3ec6SMatthias Ringwald         return;
4793deb3ec6SMatthias Ringwald     }
4803deb3ec6SMatthias Ringwald     log_info("ATT Signed Write, valid signature");
4813deb3ec6SMatthias Ringwald 
4823deb3ec6SMatthias Ringwald     // update sequence number
48318707219SMatthias Ringwald     uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
48418707219SMatthias Ringwald     le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1);
48518707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
486c8c6e9b4SMatthias Ringwald     att_server_request_can_send_now(att_server);
4873deb3ec6SMatthias Ringwald }
4887a766ebfSMatthias Ringwald #endif
48988a48dd8SMatthias Ringwald 
49018707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED
491b06f2579SMatthias Ringwald // pre: can send now
492b06f2579SMatthias Ringwald // returns: 1 if packet was sent
49318707219SMatthias Ringwald static int att_server_process_validated_request(att_server_t * att_server){
494b06f2579SMatthias Ringwald 
495b06f2579SMatthias Ringwald     l2cap_reserve_packet_buffer();
496b06f2579SMatthias Ringwald     uint8_t * att_response_buffer = l2cap_get_outgoing_buffer();
49718707219SMatthias Ringwald     uint16_t  att_response_size   = att_handle_request(&att_server->connection, att_server->request_buffer, att_server->request_size, att_response_buffer);
498b06f2579SMatthias Ringwald 
499beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
50056c3a347SMatthias Ringwald     if (att_response_size == ATT_READ_RESPONSE_PENDING || att_response_size == ATT_INTERNAL_WRITE_RESPONSE_PENDING){
501e404a688SMatthias Ringwald         // update state
502beb20288SMatthias Ringwald         att_server->state = ATT_SERVER_RESPONSE_PENDING;
503e404a688SMatthias Ringwald 
50456c3a347SMatthias Ringwald         // callback with handle ATT_READ_RESPONSE_PENDING for reads
50556c3a347SMatthias Ringwald         if (att_response_size == ATT_READ_RESPONSE_PENDING){
506e404a688SMatthias Ringwald             att_server_client_read_callback(att_server->connection.con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0);
50756c3a347SMatthias Ringwald         }
508e8e7403eSMatthias Ringwald 
509e8e7403eSMatthias Ringwald         // free reserved buffer
510e8e7403eSMatthias Ringwald         l2cap_release_packet_buffer();
511e8e7403eSMatthias Ringwald         return 0;
512e404a688SMatthias Ringwald     }
513e404a688SMatthias Ringwald #endif
514e404a688SMatthias Ringwald 
515b06f2579SMatthias Ringwald     // intercept "insufficient authorization" for authenticated connections to allow for user authorization
516b06f2579SMatthias Ringwald     if ((att_response_size     >= 4)
517b06f2579SMatthias Ringwald     && (att_response_buffer[0] == ATT_ERROR_RESPONSE)
518b06f2579SMatthias Ringwald     && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION)
51918707219SMatthias Ringwald     && (att_server->connection.authenticated)){
520b06f2579SMatthias Ringwald 
5219c6e867eSMatthias Ringwald         switch (gap_authorization_state(att_server->connection.con_handle)){
522b06f2579SMatthias Ringwald             case AUTHORIZATION_UNKNOWN:
523b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
52418707219SMatthias Ringwald                 sm_request_pairing(att_server->connection.con_handle);
525b06f2579SMatthias Ringwald                 return 0;
526b06f2579SMatthias Ringwald             case AUTHORIZATION_PENDING:
527b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
528b06f2579SMatthias Ringwald                 return 0;
529b06f2579SMatthias Ringwald             default:
530b06f2579SMatthias Ringwald                 break;
531b06f2579SMatthias Ringwald         }
532b06f2579SMatthias Ringwald     }
533b06f2579SMatthias Ringwald 
53418707219SMatthias Ringwald     att_server->state = ATT_SERVER_IDLE;
535b06f2579SMatthias Ringwald     if (att_response_size == 0) {
536b06f2579SMatthias Ringwald         l2cap_release_packet_buffer();
537b06f2579SMatthias Ringwald         return 0;
538b06f2579SMatthias Ringwald     }
539b06f2579SMatthias Ringwald 
54018707219SMatthias Ringwald     l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size);
541b06f2579SMatthias Ringwald 
542b06f2579SMatthias Ringwald     // notify client about MTU exchange result
543b06f2579SMatthias Ringwald     if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){
54418707219SMatthias Ringwald         att_emit_mtu_event(att_server->connection.con_handle, att_server->connection.mtu);
545b06f2579SMatthias Ringwald     }
546b06f2579SMatthias Ringwald     return 1;
547b06f2579SMatthias Ringwald }
548b06f2579SMatthias Ringwald 
549beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
550beb20288SMatthias Ringwald int att_server_response_ready(hci_con_handle_t con_handle){
551e404a688SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
552e404a688SMatthias Ringwald     if (!att_server)                                        return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
553beb20288SMatthias Ringwald     if (att_server->state != ATT_SERVER_RESPONSE_PENDING)   return ERROR_CODE_COMMAND_DISALLOWED;
554e404a688SMatthias Ringwald 
555e404a688SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
556c8c6e9b4SMatthias Ringwald     att_server_request_can_send_now(att_server);
557e404a688SMatthias Ringwald     return ERROR_CODE_SUCCESS;
558e404a688SMatthias Ringwald }
559e404a688SMatthias Ringwald #endif
560e404a688SMatthias Ringwald 
56118707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server){
56218707219SMatthias Ringwald     switch (att_server->state){
5633deb3ec6SMatthias Ringwald         case ATT_SERVER_REQUEST_RECEIVED:
564760ad805SMatthias Ringwald 
5650234fbbcSMatthias Ringwald             // wait until re-encryption as central is complete
5660234fbbcSMatthias Ringwald             if (gap_reconnect_security_setup_active(att_server->connection.con_handle)) break;
5670234fbbcSMatthias Ringwald 
568760ad805SMatthias Ringwald             // wait until pairing is complete
569760ad805SMatthias Ringwald             if (att_server->pairing_active) break;
570760ad805SMatthias Ringwald 
5717a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
57218707219SMatthias Ringwald             if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){
5733deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write!");
5743deb3ec6SMatthias Ringwald                 if (!sm_cmac_ready()) {
5753deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, sm_cmac engine not ready. Abort");
57618707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
5773deb3ec6SMatthias Ringwald                     return;
5783deb3ec6SMatthias Ringwald                 }
57918707219SMatthias Ringwald                 if (att_server->request_size < (3 + 12)) {
5803deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, request to short. Abort.");
58118707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
5823deb3ec6SMatthias Ringwald                     return;
5833deb3ec6SMatthias Ringwald                 }
58418707219SMatthias Ringwald                 if (att_server->ir_lookup_active){
5853deb3ec6SMatthias Ringwald                     return;
5863deb3ec6SMatthias Ringwald                 }
58718707219SMatthias Ringwald                 if (att_server->ir_le_device_db_index < 0){
5883deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, CSRK not available");
58918707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
5903deb3ec6SMatthias Ringwald                     return;
5913deb3ec6SMatthias Ringwald                 }
5923deb3ec6SMatthias Ringwald 
5933deb3ec6SMatthias Ringwald                 // check counter
59418707219SMatthias Ringwald                 uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
59518707219SMatthias Ringwald                 uint32_t counter_db     = le_device_db_remote_counter_get(att_server->ir_le_device_db_index);
5963deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet);
5973deb3ec6SMatthias Ringwald                 if (counter_packet < counter_db){
5983deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, db reports higher counter, abort");
59918707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
6003deb3ec6SMatthias Ringwald                     return;
6013deb3ec6SMatthias Ringwald                 }
6023deb3ec6SMatthias Ringwald 
6033deb3ec6SMatthias Ringwald                 // signature is { sequence counter, secure hash }
6043deb3ec6SMatthias Ringwald                 sm_key_t csrk;
60518707219SMatthias Ringwald                 le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk);
60618707219SMatthias Ringwald                 att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION;
6073deb3ec6SMatthias Ringwald                 log_info("Orig Signature: ");
60818707219SMatthias Ringwald                 log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8);
60918707219SMatthias Ringwald                 uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1);
61018707219SMatthias 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);
6113deb3ec6SMatthias Ringwald                 return;
6123deb3ec6SMatthias Ringwald             }
6137a766ebfSMatthias Ringwald #endif
614b06f2579SMatthias Ringwald             // move on
61518707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
616ed053a35SMatthias Ringwald             att_server_request_can_send_now(att_server);
617b06f2579SMatthias Ringwald             break;
61888a48dd8SMatthias Ringwald 
6193deb3ec6SMatthias Ringwald         default:
6203deb3ec6SMatthias Ringwald             break;
6213deb3ec6SMatthias Ringwald     }
6223deb3ec6SMatthias Ringwald }
6233deb3ec6SMatthias Ringwald 
62490f03c80SMatthias Ringwald static int att_server_data_ready_for_phase(att_server_t * att_server,  att_server_run_phase_t phase){
62590f03c80SMatthias Ringwald     switch (phase){
62690f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
62790f03c80SMatthias Ringwald             return att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
62890f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
62990f03c80SMatthias Ringwald              return (!btstack_linked_list_empty(&att_server->indication_requests) && att_server->value_indication_handle == 0);
63090f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
63190f03c80SMatthias Ringwald             return (!btstack_linked_list_empty(&att_server->notification_requests));
63290f03c80SMatthias Ringwald     }
633fa5e3464SMatthias Ringwald     // avoid warning
634fa5e3464SMatthias Ringwald     return 0;
63590f03c80SMatthias Ringwald }
63690f03c80SMatthias Ringwald 
63790f03c80SMatthias Ringwald static void att_server_trigger_send_for_phase(att_server_t * att_server,  att_server_run_phase_t phase){
63890f03c80SMatthias Ringwald     btstack_context_callback_registration_t * client;
63990f03c80SMatthias Ringwald     switch (phase){
64090f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
64190f03c80SMatthias Ringwald             att_server_process_validated_request(att_server);
64290f03c80SMatthias Ringwald             break;
64390f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
64490f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->indication_requests;
64590f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->indication_requests, (btstack_linked_item_t *) client);
64690f03c80SMatthias Ringwald             client->callback(client->context);
64790f03c80SMatthias Ringwald             break;
64890f03c80SMatthias Ringwald        case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
64990f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->notification_requests;
65090f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->notification_requests, (btstack_linked_item_t *) client);
65190f03c80SMatthias Ringwald             client->callback(client->context);
65290f03c80SMatthias Ringwald             break;
65390f03c80SMatthias Ringwald     }
65490f03c80SMatthias Ringwald }
65590f03c80SMatthias Ringwald 
6567eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){
657b06f2579SMatthias Ringwald 
6586438547aSMatthias Ringwald     hci_con_handle_t last_send_con_handle = HCI_CON_HANDLE_INVALID;
65969d32c56SMatthias Ringwald     att_server_t *   request_att_server   = NULL;
6606438547aSMatthias Ringwald     int can_send_now = 1;
6614395a000SMatthias Ringwald     int phase_index;
6626438547aSMatthias Ringwald 
6634395a000SMatthias Ringwald     for (phase_index = ATT_SERVER_RUN_PHASE_1_REQUESTS; phase_index <= ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS; phase_index++){
6644395a000SMatthias Ringwald         att_server_run_phase_t phase = (att_server_run_phase_t) phase_index;
6656438547aSMatthias Ringwald         hci_con_handle_t skip_connections_until = att_server_last_can_send_now;
666374a288aSMatthias Ringwald         while (1){
6676438547aSMatthias Ringwald             btstack_linked_list_iterator_t it;
66818707219SMatthias Ringwald             hci_connections_get_iterator(&it);
66918707219SMatthias Ringwald             while(btstack_linked_list_iterator_has_next(&it)){
67018707219SMatthias Ringwald                 hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
67118707219SMatthias Ringwald                 att_server_t * att_server = &connection->att_server;
6726438547aSMatthias Ringwald 
67390f03c80SMatthias Ringwald                 int data_ready = att_server_data_ready_for_phase(att_server, phase);
6746438547aSMatthias Ringwald 
6756438547aSMatthias 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);
6766438547aSMatthias Ringwald 
6776438547aSMatthias Ringwald                 // skip until last sender found (which is also skipped)
6786438547aSMatthias Ringwald                 if (skip_connections_until != HCI_CON_HANDLE_INVALID){
679b6335864SMatthias Ringwald                     if (data_ready && request_att_server == NULL){
68069d32c56SMatthias Ringwald                         request_att_server = att_server;
6816438547aSMatthias Ringwald                     }
6826438547aSMatthias Ringwald                     if (skip_connections_until == att_server->connection.con_handle){
6836438547aSMatthias Ringwald                         skip_connections_until = HCI_CON_HANDLE_INVALID;
6846438547aSMatthias Ringwald                     }
6856438547aSMatthias Ringwald                     continue;
6866438547aSMatthias Ringwald                 };
6876438547aSMatthias Ringwald 
68890f03c80SMatthias Ringwald                 if (data_ready){
689969a5bbaSMatthias Ringwald                     if (can_send_now){
69090f03c80SMatthias Ringwald                         att_server_trigger_send_for_phase(att_server, phase);
6916438547aSMatthias Ringwald                         last_send_con_handle = att_server->connection.con_handle;
692*c0f26e1cSMatthias Ringwald                         can_send_now = att_server_can_send_packet(att_server);
69390f03c80SMatthias Ringwald                         data_ready = att_server_data_ready_for_phase(att_server, phase);
694b6335864SMatthias Ringwald                         if (data_ready && request_att_server == NULL){
69569d32c56SMatthias Ringwald                             request_att_server = att_server;
696cbbb12d9SMatthias Ringwald                         }
697cbbb12d9SMatthias Ringwald                     } else {
69869d32c56SMatthias Ringwald                         request_att_server = att_server;
699f29a88d8SMatthias Ringwald                         break;
700cbbb12d9SMatthias Ringwald                     }
701cbbb12d9SMatthias Ringwald                 }
7023deb3ec6SMatthias Ringwald             }
7033deb3ec6SMatthias Ringwald 
7046438547aSMatthias Ringwald             // stop skipping (handles disconnect by last send connection)
7056438547aSMatthias Ringwald             skip_connections_until = HCI_CON_HANDLE_INVALID;
7066438547aSMatthias Ringwald 
7073551936eSMatthias Ringwald             // Exit loop, if we cannot send
708969a5bbaSMatthias Ringwald             if (!can_send_now) break;
709969a5bbaSMatthias Ringwald 
710969a5bbaSMatthias Ringwald             // Exit loop, if we can send but there are also no further request
711b6335864SMatthias Ringwald             if (request_att_server == NULL) break;
712969a5bbaSMatthias Ringwald 
713969a5bbaSMatthias Ringwald             // Finally, if we still can send and there are requests, just try again
71469d32c56SMatthias Ringwald             request_att_server = NULL;
715969a5bbaSMatthias Ringwald         }
7166438547aSMatthias Ringwald         // update last send con handle for round robin
7176438547aSMatthias Ringwald         if (last_send_con_handle != HCI_CON_HANDLE_INVALID){
7186438547aSMatthias Ringwald             att_server_last_can_send_now = last_send_con_handle;
7196438547aSMatthias Ringwald         }
7203551936eSMatthias Ringwald     }
721969a5bbaSMatthias Ringwald 
722b6335864SMatthias Ringwald     if (request_att_server == NULL) return;
72369d32c56SMatthias Ringwald     att_server_request_can_send_now(request_att_server);
724969a5bbaSMatthias Ringwald }
725969a5bbaSMatthias Ringwald 
72641772f37SMatthias Ringwald static void att_server_handle_att_pdu(att_server_t * att_server, uint8_t * packet, uint16_t size){
72718707219SMatthias Ringwald 
7283deb3ec6SMatthias Ringwald     // handle value indication confirms
72918707219SMatthias Ringwald     if (packet[0] == ATT_HANDLE_VALUE_CONFIRMATION && att_server->value_indication_handle){
73018707219SMatthias Ringwald         btstack_run_loop_remove_timer(&att_server->value_indication_timer);
73118707219SMatthias Ringwald         uint16_t att_handle = att_server->value_indication_handle;
73218707219SMatthias Ringwald         att_server->value_indication_handle = 0;
73318707219SMatthias Ringwald         att_handle_value_indication_notify_client(0, att_server->connection.con_handle, att_handle);
73441772f37SMatthias Ringwald         att_server_request_can_send_now(att_server);
7353deb3ec6SMatthias Ringwald         return;
7363deb3ec6SMatthias Ringwald     }
7373deb3ec6SMatthias Ringwald 
7386428eb5aSMatthias Ringwald     // directly process command
7396428eb5aSMatthias Ringwald     // note: signed write cannot be handled directly as authentication needs to be verified
7406428eb5aSMatthias Ringwald     if (packet[0] == ATT_WRITE_COMMAND){
74118707219SMatthias Ringwald         att_handle_request(&att_server->connection, packet, size, 0);
7426428eb5aSMatthias Ringwald         return;
7436428eb5aSMatthias Ringwald     }
7446428eb5aSMatthias Ringwald 
7453deb3ec6SMatthias Ringwald     // check size
74618707219SMatthias Ringwald     if (size > sizeof(att_server->request_buffer)) {
747e0463bdcSMatthias 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));
7486428eb5aSMatthias Ringwald         return;
7496428eb5aSMatthias Ringwald     }
7503deb3ec6SMatthias Ringwald 
751e0463bdcSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
752e0463bdcSMatthias Ringwald     // abort signed write validation if a new request comes in (but finish previous signed write if possible)
753e0463bdcSMatthias Ringwald     if (att_server->state == ATT_SERVER_W4_SIGNED_WRITE_VALIDATION){
754e0463bdcSMatthias Ringwald         if (packet[0] == ATT_SIGNED_WRITE_COMMAND){
755e0463bdcSMatthias Ringwald             log_info("skip new signed write request as previous is in validation");
756e0463bdcSMatthias Ringwald             return;
757e0463bdcSMatthias Ringwald         } else {
758e0463bdcSMatthias Ringwald             log_info("abort signed write validation to process new request");
759e0463bdcSMatthias Ringwald             att_server->state = ATT_SERVER_IDLE;
760e0463bdcSMatthias Ringwald         }
761e0463bdcSMatthias Ringwald     }
762e0463bdcSMatthias Ringwald #endif
7633deb3ec6SMatthias Ringwald     // last request still in processing?
76418707219SMatthias Ringwald     if (att_server->state != ATT_SERVER_IDLE){
765e0463bdcSMatthias Ringwald         log_info("skip att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state);
7666428eb5aSMatthias Ringwald         return;
7676428eb5aSMatthias Ringwald     }
7683deb3ec6SMatthias Ringwald 
7693deb3ec6SMatthias Ringwald     // store request
77018707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED;
77118707219SMatthias Ringwald     att_server->request_size = size;
77218707219SMatthias Ringwald     memcpy(att_server->request_buffer, packet, size);
7733deb3ec6SMatthias Ringwald 
77418707219SMatthias Ringwald     att_run_for_context(att_server);
77541772f37SMatthias Ringwald }
77641772f37SMatthias Ringwald 
77741772f37SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
77841772f37SMatthias Ringwald     att_server_t * att_server;
77941772f37SMatthias Ringwald 
78041772f37SMatthias Ringwald     switch (packet_type){
78141772f37SMatthias Ringwald         case HCI_EVENT_PACKET:
78241772f37SMatthias Ringwald             switch (packet[0]){
78341772f37SMatthias Ringwald                 case L2CAP_EVENT_CAN_SEND_NOW:
78441772f37SMatthias Ringwald                     att_server_handle_can_send_now();
78541772f37SMatthias Ringwald                     break;
78641772f37SMatthias Ringwald                 case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
78741772f37SMatthias Ringwald                     // GATT client has negotiated the mtu for this connection
78841772f37SMatthias Ringwald                     att_server = att_server_for_handle(handle);
78941772f37SMatthias Ringwald                     if (!att_server) break;
79041772f37SMatthias Ringwald                     att_server->connection.mtu = little_endian_read_16(packet, 4);
79141772f37SMatthias Ringwald                     break;
79241772f37SMatthias Ringwald                 default:
79341772f37SMatthias Ringwald                     break;
79441772f37SMatthias Ringwald             }
79541772f37SMatthias Ringwald             break;
79641772f37SMatthias Ringwald 
79741772f37SMatthias Ringwald         case ATT_DATA_PACKET:
79841772f37SMatthias Ringwald             log_debug("ATT Packet, handle 0x%04x", handle);
79941772f37SMatthias Ringwald             att_server = att_server_for_handle(handle);
80041772f37SMatthias Ringwald             if (!att_server) break;
80141772f37SMatthias Ringwald 
80241772f37SMatthias Ringwald             att_server_handle_att_pdu(att_server, packet, size);
803372d62c4SMatthias Ringwald             break;
804372d62c4SMatthias Ringwald     }
8053deb3ec6SMatthias Ringwald }
8063deb3ec6SMatthias Ringwald 
807bf78aac6SMatthias Ringwald // ---------------------
808bf78aac6SMatthias Ringwald // persistent CCC writes
809bf78aac6SMatthias Ringwald static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){
810bf78aac6SMatthias Ringwald     return 'B' << 24 | 'T' << 16 | 'C' << 8 | index;
811bf78aac6SMatthias Ringwald }
812bf78aac6SMatthias Ringwald 
813bf78aac6SMatthias Ringwald static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){
814bf78aac6SMatthias Ringwald     // lookup att_server instance
815bf78aac6SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
816bf78aac6SMatthias Ringwald     if (!att_server) return;
817bf78aac6SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
818bf78aac6SMatthias 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);
8196a540790SMatthias Ringwald 
820bf78aac6SMatthias Ringwald     // check if bonded
821bf78aac6SMatthias Ringwald     if (le_device_index < 0) return;
8226a540790SMatthias Ringwald 
823bf78aac6SMatthias Ringwald     // get btstack_tlv
824bf78aac6SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
825bf78aac6SMatthias Ringwald     void * tlv_context;
826bf78aac6SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
827bf78aac6SMatthias Ringwald     if (!tlv_impl) return;
8286a540790SMatthias Ringwald 
829bf78aac6SMatthias Ringwald     // update ccc tag
830bf78aac6SMatthias Ringwald     int index;
8316a540790SMatthias Ringwald     uint32_t highest_seq_nr = 0;
8326a540790SMatthias Ringwald     uint32_t lowest_seq_nr = 0;
8336a540790SMatthias Ringwald     uint32_t tag_for_lowest_seq_nr = 0;
8346a540790SMatthias Ringwald     uint32_t tag_for_empty = 0;
8356a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
8366afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
837bf78aac6SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
8386a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
8396a540790SMatthias Ringwald 
8406a540790SMatthias Ringwald         // empty/invalid tag
8416a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)){
8426a540790SMatthias Ringwald             tag_for_empty = tag;
843bf78aac6SMatthias Ringwald             continue;
844bf78aac6SMatthias Ringwald         }
8456a540790SMatthias Ringwald         // update highest seq nr
8466a540790SMatthias Ringwald         if (entry.seq_nr > highest_seq_nr){
8476a540790SMatthias Ringwald             highest_seq_nr = entry.seq_nr;
8486a540790SMatthias Ringwald         }
8496a540790SMatthias Ringwald         // find entry with lowest seq nr
8506a540790SMatthias Ringwald         if ((tag_for_lowest_seq_nr == 0) || (entry.seq_nr < lowest_seq_nr)){
8516a540790SMatthias Ringwald             tag_for_lowest_seq_nr = tag;
8526a540790SMatthias Ringwald             lowest_seq_nr = entry.seq_nr;
8536a540790SMatthias Ringwald         }
8546a540790SMatthias Ringwald 
8556a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
8566a540790SMatthias Ringwald         if (entry.att_handle   != att_handle)      continue;
8576a540790SMatthias Ringwald 
8586a540790SMatthias Ringwald         // found matching entry
85901ac2008SMatthias Ringwald         if (value){
860bf78aac6SMatthias Ringwald             // update
8616a540790SMatthias Ringwald             if (entry.value == value) {
862760ad805SMatthias Ringwald                 log_info("CCC Index %u: Up-to-date", index);
863760ad805SMatthias Ringwald                 return;
864760ad805SMatthias Ringwald             }
8656a540790SMatthias Ringwald             entry.value = value;
8666a540790SMatthias Ringwald             entry.seq_nr = highest_seq_nr + 1;
867760ad805SMatthias Ringwald             log_info("CCC Index %u: Store", index);
8686a540790SMatthias Ringwald             tlv_impl->store_tag(tlv_context, tag, (const uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
86901ac2008SMatthias Ringwald         } else {
87001ac2008SMatthias Ringwald             // delete
871760ad805SMatthias Ringwald             log_info("CCC Index %u: Delete", index);
87201ac2008SMatthias Ringwald             tlv_impl->delete_tag(tlv_context, tag);
87301ac2008SMatthias Ringwald         }
874bf78aac6SMatthias Ringwald         return;
875bf78aac6SMatthias Ringwald     }
8766a540790SMatthias Ringwald 
877faf2b6c3SMatthias Ringwald     log_info("tag_for_empy %"PRIx32", tag_for_lowest_seq_nr %"PRIx32, tag_for_empty, tag_for_lowest_seq_nr);
8786a540790SMatthias Ringwald 
8796a540790SMatthias Ringwald     if (value == 0){
8806a540790SMatthias Ringwald         // done
8816a540790SMatthias Ringwald         return;
8826a540790SMatthias Ringwald     }
8836a540790SMatthias Ringwald 
8846a540790SMatthias Ringwald     uint32_t tag_to_use = 0;
8856a540790SMatthias Ringwald     if (tag_for_empty){
8866a540790SMatthias Ringwald         tag_to_use = tag_for_empty;
8876a540790SMatthias Ringwald     } else if (tag_for_lowest_seq_nr){
8886a540790SMatthias Ringwald         tag_to_use = tag_for_lowest_seq_nr;
8896a540790SMatthias Ringwald     } else {
8906a540790SMatthias Ringwald         // should not happen
8916a540790SMatthias Ringwald         return;
8926a540790SMatthias Ringwald     }
893bf78aac6SMatthias Ringwald     // store ccc tag
8946a540790SMatthias Ringwald     entry.seq_nr       = highest_seq_nr + 1;
8956a540790SMatthias Ringwald     entry.device_index = le_device_index;
8966a540790SMatthias Ringwald     entry.att_handle   = att_handle;
8976a540790SMatthias Ringwald     entry.value        = value;
8986a540790SMatthias Ringwald     tlv_impl->store_tag(tlv_context, tag_to_use, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
899bf78aac6SMatthias Ringwald }
900bf78aac6SMatthias Ringwald 
901760ad805SMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server){
902760ad805SMatthias Ringwald     if (!att_server) return;
903760ad805SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
904760ad805SMatthias Ringwald     log_info("Clear CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
905760ad805SMatthias Ringwald     // check if bonded
906760ad805SMatthias Ringwald     if (le_device_index < 0) return;
907760ad805SMatthias Ringwald     // get btstack_tlv
908760ad805SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
909760ad805SMatthias Ringwald     void * tlv_context;
910760ad805SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
911760ad805SMatthias Ringwald     if (!tlv_impl) return;
912760ad805SMatthias Ringwald     // get all ccc tag
913760ad805SMatthias Ringwald     int index;
9146a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
9156afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
916760ad805SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
9176a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
9186a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
9196a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
920760ad805SMatthias Ringwald         // delete entry
921760ad805SMatthias Ringwald         log_info("CCC Index %u: Delete", index);
922760ad805SMatthias Ringwald         tlv_impl->delete_tag(tlv_context, tag);
923760ad805SMatthias Ringwald     }
924760ad805SMatthias Ringwald }
925760ad805SMatthias Ringwald 
92601ac2008SMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server){
92701ac2008SMatthias Ringwald     if (!att_server) return;
92801ac2008SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
92901ac2008SMatthias Ringwald     log_info("Restore CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
9301b7acd0dSMatthias Ringwald     // check if bonded
9311b7acd0dSMatthias Ringwald     if (le_device_index < 0) return;
93201ac2008SMatthias Ringwald     // get btstack_tlv
93301ac2008SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
93401ac2008SMatthias Ringwald     void * tlv_context;
93501ac2008SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
93601ac2008SMatthias Ringwald     if (!tlv_impl) return;
93701ac2008SMatthias Ringwald     // get all ccc tag
93801ac2008SMatthias Ringwald     int index;
9396a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
9406afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
94101ac2008SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
9426a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
9436a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
9446a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
94501ac2008SMatthias Ringwald         // simulate write callback
9466a540790SMatthias Ringwald         uint16_t attribute_handle = entry.att_handle;
94701ac2008SMatthias Ringwald         uint8_t  value[2];
9486a540790SMatthias Ringwald         little_endian_store_16(value, 0, entry.value);
94901ac2008SMatthias Ringwald         att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
95001ac2008SMatthias Ringwald         if (!callback) continue;
9516a540790SMatthias Ringwald         log_info("CCC Index %u: Set Attribute handle 0x%04x to value 0x%04x", index, attribute_handle, entry.value );
95201ac2008SMatthias Ringwald         (*callback)(att_server->connection.con_handle, attribute_handle, ATT_TRANSACTION_MODE_NONE, 0, value, sizeof(value));
95301ac2008SMatthias Ringwald     }
95401ac2008SMatthias Ringwald }
95501ac2008SMatthias Ringwald 
956bf78aac6SMatthias Ringwald // persistent CCC writes
957bf78aac6SMatthias Ringwald // ---------------------
9583d71c7a4SMatthias Ringwald 
9593d71c7a4SMatthias Ringwald // gatt service management
9603d71c7a4SMatthias Ringwald static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){
9613d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
9623d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
9633d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
9643d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
9653d71c7a4SMatthias Ringwald         if (handler->start_handle > handle) continue;
9663d71c7a4SMatthias Ringwald         if (handler->end_handle   < handle) continue;
9673d71c7a4SMatthias Ringwald         return handler;
9683d71c7a4SMatthias Ringwald     }
9693d71c7a4SMatthias Ringwald     return NULL;
9703d71c7a4SMatthias Ringwald }
9713d71c7a4SMatthias Ringwald static att_read_callback_t att_server_read_callback_for_handle(uint16_t handle){
9723d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
9733d71c7a4SMatthias Ringwald     if (handler) return handler->read_callback;
9743d71c7a4SMatthias Ringwald     return att_server_client_read_callback;
9753d71c7a4SMatthias Ringwald }
9763d71c7a4SMatthias Ringwald 
9773d71c7a4SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){
9783d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
9793d71c7a4SMatthias Ringwald     if (handler) return handler->write_callback;
9803d71c7a4SMatthias Ringwald     return att_server_client_write_callback;
9813d71c7a4SMatthias Ringwald }
9823d71c7a4SMatthias Ringwald 
9835d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle){
9845d07396bSMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
9855d07396bSMatthias Ringwald     if (handler) return handler->packet_handler;
9865d07396bSMatthias Ringwald     return att_client_packet_handler;
9875d07396bSMatthias Ringwald }
9885d07396bSMatthias Ringwald 
9893d71c7a4SMatthias Ringwald static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){
9903d71c7a4SMatthias Ringwald     // notify all callbacks
9913d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
9923d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
9933d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
9943d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
9953d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
9963d71c7a4SMatthias Ringwald         (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
9973d71c7a4SMatthias Ringwald     }
9983d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return;
9993d71c7a4SMatthias Ringwald     (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
10003d71c7a4SMatthias Ringwald }
10013d71c7a4SMatthias Ringwald 
10023d71c7a4SMatthias Ringwald // returns first reported error or 0
10033d71c7a4SMatthias Ringwald static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){
10043d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
10053d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
10063d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
10073d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
10083d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
10093d71c7a4SMatthias Ringwald         uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
10103d71c7a4SMatthias Ringwald         if (error_code) return error_code;
10113d71c7a4SMatthias Ringwald     }
10123d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return 0;
10133d71c7a4SMatthias Ringwald     return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
10143d71c7a4SMatthias Ringwald }
10153d71c7a4SMatthias Ringwald 
10163d71c7a4SMatthias 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){
10173d71c7a4SMatthias Ringwald     att_read_callback_t callback = att_server_read_callback_for_handle(attribute_handle);
101801ac2008SMatthias Ringwald     if (!callback) return 0;
10193d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, offset, buffer, buffer_size);
10203d71c7a4SMatthias Ringwald }
10213d71c7a4SMatthias Ringwald 
10223d71c7a4SMatthias 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){
10233d71c7a4SMatthias Ringwald     switch (transaction_mode){
10243d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_VALIDATE:
10253d71c7a4SMatthias Ringwald             return att_validate_prepared_write(con_handle);
10263d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_EXECUTE:
10273d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_CANCEL:
10283d71c7a4SMatthias Ringwald             att_notify_write_callbacks(con_handle, transaction_mode);
10293d71c7a4SMatthias Ringwald             return 0;
10303d71c7a4SMatthias Ringwald         default:
10313d71c7a4SMatthias Ringwald             break;
10323d71c7a4SMatthias Ringwald     }
10333d71c7a4SMatthias Ringwald 
1034bf78aac6SMatthias Ringwald     // track CCC writes
1035bf78aac6SMatthias Ringwald     if (att_is_persistent_ccc(attribute_handle) && offset == 0 && buffer_size == 2){
1036bf78aac6SMatthias Ringwald         att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0));
1037bf78aac6SMatthias Ringwald     }
1038bf78aac6SMatthias Ringwald 
103901ac2008SMatthias Ringwald     att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
104001ac2008SMatthias Ringwald     if (!callback) return 0;
10413d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size);
10423d71c7a4SMatthias Ringwald }
10433d71c7a4SMatthias Ringwald 
10443d71c7a4SMatthias Ringwald /**
10453d71c7a4SMatthias Ringwald  * @brief register read/write callbacks for specific handle range
10463d71c7a4SMatthias Ringwald  * @param att_service_handler_t
10473d71c7a4SMatthias Ringwald  */
10483d71c7a4SMatthias Ringwald void att_server_register_service_handler(att_service_handler_t * handler){
10493d71c7a4SMatthias Ringwald     if (att_service_handler_for_handle(handler->start_handle) ||
10503d71c7a4SMatthias Ringwald         att_service_handler_for_handle(handler->end_handle)){
10513d71c7a4SMatthias Ringwald         log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle);
10523d71c7a4SMatthias Ringwald         return;
10533d71c7a4SMatthias Ringwald     }
10543d71c7a4SMatthias Ringwald     btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler);
10553d71c7a4SMatthias Ringwald }
10563d71c7a4SMatthias Ringwald 
10573deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){
10583deb3ec6SMatthias Ringwald 
10593d71c7a4SMatthias Ringwald     // store callbacks
10603d71c7a4SMatthias Ringwald     att_server_client_read_callback  = read_callback;
10613d71c7a4SMatthias Ringwald     att_server_client_write_callback = write_callback;
10623d71c7a4SMatthias Ringwald 
10631a389cdcSMatthias Ringwald     // register for HCI Events
1064d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &att_event_packet_handler;
10651a389cdcSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
10661a389cdcSMatthias Ringwald 
1067406722e4SMatthias Ringwald     // register for SM events
1068d9a7306aSMatthias Ringwald     sm_event_callback_registration.callback = &att_event_packet_handler;
1069406722e4SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
10703deb3ec6SMatthias Ringwald 
10711a389cdcSMatthias Ringwald     // and L2CAP ATT Server PDUs
10723deb3ec6SMatthias Ringwald     att_dispatch_register_server(att_packet_handler);
10733deb3ec6SMatthias Ringwald 
107470dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
107570dca4d4SMatthias Ringwald     // setup l2cap service
107670dca4d4SMatthias Ringwald     l2cap_register_service(&att_event_packet_handler, PSM_ATT, 0xffff, LEVEL_2);
107770dca4d4SMatthias Ringwald #endif
107870dca4d4SMatthias Ringwald 
10793deb3ec6SMatthias Ringwald     att_set_db(db);
10803d71c7a4SMatthias Ringwald     att_set_read_callback(att_server_read_callback);
10813d71c7a4SMatthias Ringwald     att_set_write_callback(att_server_write_callback);
10823deb3ec6SMatthias Ringwald }
10833deb3ec6SMatthias Ringwald 
10843deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){
10853deb3ec6SMatthias Ringwald     att_client_packet_handler = handler;
10863deb3ec6SMatthias Ringwald }
10873deb3ec6SMatthias Ringwald 
1088cbbb12d9SMatthias Ringwald 
1089cbbb12d9SMatthias Ringwald // to be deprecated
1090c141988cSMatthias Ringwald int  att_server_can_send_packet_now(hci_con_handle_t con_handle){
1091*c0f26e1cSMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
1092*c0f26e1cSMatthias Ringwald     if (!att_server) return 0;
1093*c0f26e1cSMatthias Ringwald     return att_server_can_send_packet(att_server);
10941b96b007SMatthias Ringwald }
10951b96b007SMatthias Ringwald 
1096cbbb12d9SMatthias Ringwald int att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
1097cbbb12d9SMatthias Ringwald     return att_server_request_to_send_notification(callback_registration, con_handle);
10983deb3ec6SMatthias Ringwald }
10993deb3ec6SMatthias Ringwald 
1100cbbb12d9SMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){
1101cbbb12d9SMatthias Ringwald     att_client_waiting_for_can_send_registration.callback = &att_emit_can_send_now_event;
1102cbbb12d9SMatthias Ringwald     att_server_request_to_send_notification(&att_client_waiting_for_can_send_registration, con_handle);
1103cbbb12d9SMatthias Ringwald }
1104cbbb12d9SMatthias Ringwald // end of deprecated
1105cbbb12d9SMatthias Ringwald 
1106cbbb12d9SMatthias Ringwald int att_server_request_to_send_notification(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
1107969a5bbaSMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
1108969a5bbaSMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1109cbbb12d9SMatthias Ringwald     btstack_linked_list_add_tail(&att_server->notification_requests, (btstack_linked_item_t*) callback_registration);
1110c8c6e9b4SMatthias Ringwald     att_server_request_can_send_now(att_server);
1111cbbb12d9SMatthias Ringwald     return ERROR_CODE_SUCCESS;
1112cbbb12d9SMatthias Ringwald }
1113cbbb12d9SMatthias Ringwald 
1114cbbb12d9SMatthias Ringwald int att_server_request_to_send_indication(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
1115cbbb12d9SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
1116cbbb12d9SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1117cbbb12d9SMatthias Ringwald     btstack_linked_list_add_tail(&att_server->indication_requests, (btstack_linked_item_t*) callback_registration);
1118c8c6e9b4SMatthias Ringwald     att_server_request_can_send_now(att_server);
1119969a5bbaSMatthias Ringwald     return ERROR_CODE_SUCCESS;
1120bb38f057SMatthias Ringwald }
1121bb38f057SMatthias Ringwald 
11223bb3035fSMilanka Ringwald int att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){
112318707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
112418707219SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1125*c0f26e1cSMatthias Ringwald     if (!att_server_can_send_packet(att_server)) return BTSTACK_ACL_BUFFERS_FULL;
11263deb3ec6SMatthias Ringwald 
11273deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
11283deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
112918707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_notification(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
113018707219SMatthias Ringwald 	return l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
11313deb3ec6SMatthias Ringwald }
11323deb3ec6SMatthias Ringwald 
11333bb3035fSMilanka Ringwald int att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){
113418707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
113518707219SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
113618707219SMatthias Ringwald 
1137eaac31e8SMatthias Ringwald     if (att_server->value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS;
1138*c0f26e1cSMatthias Ringwald     if (!att_server_can_send_packet(att_server)) return BTSTACK_ACL_BUFFERS_FULL;
11393deb3ec6SMatthias Ringwald 
11403deb3ec6SMatthias Ringwald     // track indication
114118707219SMatthias Ringwald     att_server->value_indication_handle = attribute_handle;
114218707219SMatthias Ringwald     btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout);
114318707219SMatthias Ringwald     btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS);
114418707219SMatthias Ringwald     btstack_run_loop_add_timer(&att_server->value_indication_timer);
11453deb3ec6SMatthias Ringwald 
11463deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
11473deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
114818707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_indication(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
114918707219SMatthias Ringwald 	l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
11503deb3ec6SMatthias Ringwald     return 0;
11513deb3ec6SMatthias Ringwald }
11528f9132b5SMilanka Ringwald 
11538f9132b5SMilanka Ringwald uint16_t att_server_get_mtu(hci_con_handle_t con_handle){
11548f9132b5SMilanka Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
11558f9132b5SMilanka Ringwald     if (!att_server) return 0;
11568f9132b5SMilanka Ringwald     return att_server->connection.mtu;
11578f9132b5SMilanka Ringwald }
1158