xref: /btstack/src/ble/att_server.c (revision 41772f378faf602227ade7f2a5d43e717c4e5aca)
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);
79*41772f37SMatthias 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);
303760ad805SMatthias Ringwald                             att_server->pairing_active = 0;
304ff1bec95SMatthias Ringwald                             // notify all - old
3055d07396bSMatthias Ringwald                             att_emit_event_to_all(packet, size);
306ff1bec95SMatthias Ringwald                             // notify all - new
307ff1bec95SMatthias Ringwald                             att_emit_connected_event(att_server);
3083deb3ec6SMatthias Ringwald                             break;
3093deb3ec6SMatthias Ringwald 
3103deb3ec6SMatthias Ringwald                         default:
3113deb3ec6SMatthias Ringwald                             break;
3123deb3ec6SMatthias Ringwald                     }
3133deb3ec6SMatthias Ringwald                     break;
3143deb3ec6SMatthias Ringwald 
3153deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_CHANGE:
3163deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE:
3173deb3ec6SMatthias Ringwald                 	// check handle
31818707219SMatthias Ringwald                     con_handle = little_endian_read_16(packet, 3);
31918707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
32018707219SMatthias Ringwald                     if (!att_server) break;
3219c6e867eSMatthias Ringwald                     att_server->connection.encryption_key_size = gap_encryption_key_size(con_handle);
3229c6e867eSMatthias Ringwald                     att_server->connection.authenticated = gap_authenticated(con_handle);
32313eb7322SMatthias Ringwald                     att_server->connection.secure_connection = gap_secure_connection(con_handle);
32413eb7322SMatthias Ringwald                     log_info("encrypted key size %u, authenticated %u, secure connectipon %u",
32513eb7322SMatthias Ringwald                         att_server->connection.encryption_key_size, att_server->connection.authenticated, att_server->connection.secure_connection);
32601ac2008SMatthias Ringwald                     if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){
32701ac2008SMatthias Ringwald                         // restore CCC values when encrypted
32801ac2008SMatthias Ringwald                         if (hci_event_encryption_change_get_encryption_enabled(packet)){
32901ac2008SMatthias Ringwald                             att_server_persistent_ccc_restore(att_server);
33001ac2008SMatthias Ringwald                         }
33101ac2008SMatthias Ringwald                     }
3320234fbbcSMatthias Ringwald                     att_run_for_context(att_server);
3333deb3ec6SMatthias Ringwald                     break;
3343deb3ec6SMatthias Ringwald 
3353deb3ec6SMatthias Ringwald                 case HCI_EVENT_DISCONNECTION_COMPLETE:
33670a390c7SMatthias Ringwald                     // check handle
33718707219SMatthias Ringwald                     con_handle = hci_event_disconnection_complete_get_connection_handle(packet);
33818707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
33918707219SMatthias Ringwald                     if (!att_server) break;
34018707219SMatthias Ringwald                     att_clear_transaction_queue(&att_server->connection);
34118707219SMatthias Ringwald                     att_server->connection.con_handle = 0;
342760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
34318707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
344bce48a26SMatthias Ringwald                     if (att_server->value_indication_handle){
34527328ecbSMatthias Ringwald                         btstack_run_loop_remove_timer(&att_server->value_indication_timer);
346bce48a26SMatthias Ringwald                         uint16_t att_handle = att_server->value_indication_handle;
347bce48a26SMatthias Ringwald                         att_server->value_indication_handle = 0; // reset error state
348bce48a26SMatthias Ringwald                         att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_DISCONNECT, att_server->connection.con_handle, att_handle);
349bce48a26SMatthias Ringwald                     }
350ff1bec95SMatthias Ringwald                     // notify all - new
3516187ad4cSMatthias Ringwald                     att_emit_disconnected_event(con_handle);
352ff1bec95SMatthias Ringwald                     // notify all - old
3535d07396bSMatthias Ringwald                     att_emit_event_to_all(packet, size);
3543deb3ec6SMatthias Ringwald                     break;
3553deb3ec6SMatthias Ringwald 
356760ad805SMatthias Ringwald                 // Identity Resolving
3575611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_STARTED:
35818707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_started_get_handle(packet);
35918707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
36018707219SMatthias Ringwald                     if (!att_server) break;
3615611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED");
36218707219SMatthias Ringwald                     att_server->ir_lookup_active = 1;
3633deb3ec6SMatthias Ringwald                     break;
3645611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
365760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
366760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
367760ad805SMatthias Ringwald                     if (!att_server) return;
3681b7acd0dSMatthias Ringwald                     att_server->ir_lookup_active = 0;
3691b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index(packet);
3701b7acd0dSMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED");
3711b7acd0dSMatthias Ringwald                     att_run_for_context(att_server);
3723deb3ec6SMatthias Ringwald                     break;
3735611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_FAILED:
37418707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_failed_get_handle(packet);
37518707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
37618707219SMatthias Ringwald                     if (!att_server) break;
3775611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED");
37818707219SMatthias Ringwald                     att_server->ir_lookup_active = 0;
37918707219SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
38018707219SMatthias Ringwald                     att_run_for_context(att_server);
3813deb3ec6SMatthias Ringwald                     break;
382760ad805SMatthias Ringwald 
383760ad805SMatthias Ringwald                 // Pairing started - delete stored CCC values
384760ad805SMatthias Ringwald                 // - assumes pairing indicates either new device or re-pairing, in both cases there should be no stored CCC values
385760ad805SMatthias Ringwald                 // - assumes that all events have the con handle as the first field
386760ad805SMatthias Ringwald                 case SM_EVENT_JUST_WORKS_REQUEST:
387760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
388760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_INPUT_NUMBER:
389760ad805SMatthias Ringwald                 case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
390760ad805SMatthias Ringwald                     con_handle = sm_event_just_works_request_get_handle(packet);
391760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
392760ad805SMatthias Ringwald                     if (!att_server) break;
393760ad805SMatthias Ringwald                     att_server->pairing_active = 1;
394760ad805SMatthias Ringwald                     log_info("SM Pairing started");
395760ad805SMatthias Ringwald                     if (att_server->ir_le_device_db_index < 0) break;
396760ad805SMatthias Ringwald                     att_server_persistent_ccc_clear(att_server);
397760ad805SMatthias Ringwald                     // index not valid anymore
398760ad805SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
399760ad805SMatthias Ringwald                     break;
400760ad805SMatthias Ringwald 
4011b7acd0dSMatthias Ringwald                 // Bonding completed
402760ad805SMatthias Ringwald                 case SM_EVENT_IDENTITY_CREATED:
403760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
404760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
405760ad805SMatthias Ringwald                     if (!att_server) return;
406760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
4071b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_created_get_index(packet);
4081b7acd0dSMatthias Ringwald                     att_run_for_context(att_server);
4091b7acd0dSMatthias Ringwald                     break;
4101b7acd0dSMatthias Ringwald 
4111b7acd0dSMatthias Ringwald                 // Pairing complete (with/without bonding=storing of pairing information)
4121b7acd0dSMatthias Ringwald                 case SM_EVENT_PAIRING_COMPLETE:
4131b7acd0dSMatthias Ringwald                     con_handle = sm_event_pairing_complete_get_handle(packet);
4141b7acd0dSMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
4151b7acd0dSMatthias Ringwald                     if (!att_server) return;
4161b7acd0dSMatthias Ringwald                     att_server->pairing_active = 0;
4171b7acd0dSMatthias Ringwald                     att_run_for_context(att_server);
418760ad805SMatthias Ringwald                     break;
419760ad805SMatthias Ringwald 
420760ad805SMatthias Ringwald                 // Authorization
4215611a760SMatthias Ringwald                 case SM_EVENT_AUTHORIZATION_RESULT: {
42218707219SMatthias Ringwald                     con_handle = sm_event_authorization_result_get_handle(packet);
42318707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
42418707219SMatthias Ringwald                     if (!att_server) break;
42518707219SMatthias Ringwald                     att_server->connection.authorized = sm_event_authorization_result_get_authorization_result(packet);
42618707219SMatthias Ringwald                     att_dispatch_server_request_can_send_now_event(con_handle);
4273deb3ec6SMatthias Ringwald                 	break;
4283deb3ec6SMatthias Ringwald                 }
4293deb3ec6SMatthias Ringwald                 default:
4303deb3ec6SMatthias Ringwald                     break;
4313deb3ec6SMatthias Ringwald             }
43265b44ffdSMatthias Ringwald             break;
43370dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
43470dca4d4SMatthias Ringwald         case L2CAP_DATA_PACKET:
43570dca4d4SMatthias Ringwald             att_server = att_server_for_l2cap_cid(channel);
43670dca4d4SMatthias Ringwald             if (!att_server) break;
437*41772f37SMatthias Ringwald 
438*41772f37SMatthias Ringwald             att_server_handle_att_pdu(att_server, packet, size);
43970dca4d4SMatthias Ringwald             break;
44070dca4d4SMatthias Ringwald #endif
441*41772f37SMatthias Ringwald 
44265b44ffdSMatthias Ringwald         default:
44365b44ffdSMatthias Ringwald             break;
4443deb3ec6SMatthias Ringwald     }
4453deb3ec6SMatthias Ringwald }
4463deb3ec6SMatthias Ringwald 
4477a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
4483deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
4493deb3ec6SMatthias Ringwald 
45018707219SMatthias Ringwald     att_server_t * att_server = att_server_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION);
45118707219SMatthias Ringwald     if (!att_server) return;
4523deb3ec6SMatthias Ringwald 
4533deb3ec6SMatthias Ringwald     uint8_t hash_flipped[8];
4549c80e4ccSMatthias Ringwald     reverse_64(hash, hash_flipped);
45518707219SMatthias Ringwald     if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8)){
4563deb3ec6SMatthias Ringwald         log_info("ATT Signed Write, invalid signature");
45718707219SMatthias Ringwald         att_server->state = ATT_SERVER_IDLE;
4583deb3ec6SMatthias Ringwald         return;
4593deb3ec6SMatthias Ringwald     }
4603deb3ec6SMatthias Ringwald     log_info("ATT Signed Write, valid signature");
4613deb3ec6SMatthias Ringwald 
4623deb3ec6SMatthias Ringwald     // update sequence number
46318707219SMatthias Ringwald     uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
46418707219SMatthias Ringwald     le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1);
46518707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
46618707219SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
4673deb3ec6SMatthias Ringwald }
4687a766ebfSMatthias Ringwald #endif
46988a48dd8SMatthias Ringwald 
47018707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED
471b06f2579SMatthias Ringwald // pre: can send now
472b06f2579SMatthias Ringwald // returns: 1 if packet was sent
47318707219SMatthias Ringwald static int att_server_process_validated_request(att_server_t * att_server){
474b06f2579SMatthias Ringwald 
475b06f2579SMatthias Ringwald     l2cap_reserve_packet_buffer();
476b06f2579SMatthias Ringwald     uint8_t * att_response_buffer = l2cap_get_outgoing_buffer();
47718707219SMatthias Ringwald     uint16_t  att_response_size   = att_handle_request(&att_server->connection, att_server->request_buffer, att_server->request_size, att_response_buffer);
478b06f2579SMatthias Ringwald 
479beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
48056c3a347SMatthias Ringwald     if (att_response_size == ATT_READ_RESPONSE_PENDING || att_response_size == ATT_INTERNAL_WRITE_RESPONSE_PENDING){
481e404a688SMatthias Ringwald         // update state
482beb20288SMatthias Ringwald         att_server->state = ATT_SERVER_RESPONSE_PENDING;
483e404a688SMatthias Ringwald 
48456c3a347SMatthias Ringwald         // callback with handle ATT_READ_RESPONSE_PENDING for reads
48556c3a347SMatthias Ringwald         if (att_response_size == ATT_READ_RESPONSE_PENDING){
486e404a688SMatthias Ringwald             att_server_client_read_callback(att_server->connection.con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0);
48756c3a347SMatthias Ringwald         }
488e8e7403eSMatthias Ringwald 
489e8e7403eSMatthias Ringwald         // free reserved buffer
490e8e7403eSMatthias Ringwald         l2cap_release_packet_buffer();
491e8e7403eSMatthias Ringwald         return 0;
492e404a688SMatthias Ringwald     }
493e404a688SMatthias Ringwald #endif
494e404a688SMatthias Ringwald 
495b06f2579SMatthias Ringwald     // intercept "insufficient authorization" for authenticated connections to allow for user authorization
496b06f2579SMatthias Ringwald     if ((att_response_size     >= 4)
497b06f2579SMatthias Ringwald     && (att_response_buffer[0] == ATT_ERROR_RESPONSE)
498b06f2579SMatthias Ringwald     && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION)
49918707219SMatthias Ringwald     && (att_server->connection.authenticated)){
500b06f2579SMatthias Ringwald 
5019c6e867eSMatthias Ringwald         switch (gap_authorization_state(att_server->connection.con_handle)){
502b06f2579SMatthias Ringwald             case AUTHORIZATION_UNKNOWN:
503b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
50418707219SMatthias Ringwald                 sm_request_pairing(att_server->connection.con_handle);
505b06f2579SMatthias Ringwald                 return 0;
506b06f2579SMatthias Ringwald             case AUTHORIZATION_PENDING:
507b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
508b06f2579SMatthias Ringwald                 return 0;
509b06f2579SMatthias Ringwald             default:
510b06f2579SMatthias Ringwald                 break;
511b06f2579SMatthias Ringwald         }
512b06f2579SMatthias Ringwald     }
513b06f2579SMatthias Ringwald 
51418707219SMatthias Ringwald     att_server->state = ATT_SERVER_IDLE;
515b06f2579SMatthias Ringwald     if (att_response_size == 0) {
516b06f2579SMatthias Ringwald         l2cap_release_packet_buffer();
517b06f2579SMatthias Ringwald         return 0;
518b06f2579SMatthias Ringwald     }
519b06f2579SMatthias Ringwald 
52018707219SMatthias Ringwald     l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size);
521b06f2579SMatthias Ringwald 
522b06f2579SMatthias Ringwald     // notify client about MTU exchange result
523b06f2579SMatthias Ringwald     if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){
52418707219SMatthias Ringwald         att_emit_mtu_event(att_server->connection.con_handle, att_server->connection.mtu);
525b06f2579SMatthias Ringwald     }
526b06f2579SMatthias Ringwald     return 1;
527b06f2579SMatthias Ringwald }
528b06f2579SMatthias Ringwald 
529beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
530beb20288SMatthias Ringwald int att_server_response_ready(hci_con_handle_t con_handle){
531e404a688SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
532e404a688SMatthias Ringwald     if (!att_server)                                        return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
533beb20288SMatthias Ringwald     if (att_server->state != ATT_SERVER_RESPONSE_PENDING)   return ERROR_CODE_COMMAND_DISALLOWED;
534e404a688SMatthias Ringwald 
535e404a688SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
536e404a688SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
537e404a688SMatthias Ringwald     return ERROR_CODE_SUCCESS;
538e404a688SMatthias Ringwald }
539e404a688SMatthias Ringwald #endif
540e404a688SMatthias Ringwald 
54118707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server){
54218707219SMatthias Ringwald     switch (att_server->state){
5433deb3ec6SMatthias Ringwald         case ATT_SERVER_REQUEST_RECEIVED:
544760ad805SMatthias Ringwald 
5450234fbbcSMatthias Ringwald             // wait until re-encryption as central is complete
5460234fbbcSMatthias Ringwald             if (gap_reconnect_security_setup_active(att_server->connection.con_handle)) break;
5470234fbbcSMatthias Ringwald 
548760ad805SMatthias Ringwald             // wait until pairing is complete
549760ad805SMatthias Ringwald             if (att_server->pairing_active) break;
550760ad805SMatthias Ringwald 
5517a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
55218707219SMatthias Ringwald             if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){
5533deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write!");
5543deb3ec6SMatthias Ringwald                 if (!sm_cmac_ready()) {
5553deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, sm_cmac engine not ready. Abort");
55618707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
5573deb3ec6SMatthias Ringwald                     return;
5583deb3ec6SMatthias Ringwald                 }
55918707219SMatthias Ringwald                 if (att_server->request_size < (3 + 12)) {
5603deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, request to short. Abort.");
56118707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
5623deb3ec6SMatthias Ringwald                     return;
5633deb3ec6SMatthias Ringwald                 }
56418707219SMatthias Ringwald                 if (att_server->ir_lookup_active){
5653deb3ec6SMatthias Ringwald                     return;
5663deb3ec6SMatthias Ringwald                 }
56718707219SMatthias Ringwald                 if (att_server->ir_le_device_db_index < 0){
5683deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, CSRK not available");
56918707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
5703deb3ec6SMatthias Ringwald                     return;
5713deb3ec6SMatthias Ringwald                 }
5723deb3ec6SMatthias Ringwald 
5733deb3ec6SMatthias Ringwald                 // check counter
57418707219SMatthias Ringwald                 uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
57518707219SMatthias Ringwald                 uint32_t counter_db     = le_device_db_remote_counter_get(att_server->ir_le_device_db_index);
5763deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet);
5773deb3ec6SMatthias Ringwald                 if (counter_packet < counter_db){
5783deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, db reports higher counter, abort");
57918707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
5803deb3ec6SMatthias Ringwald                     return;
5813deb3ec6SMatthias Ringwald                 }
5823deb3ec6SMatthias Ringwald 
5833deb3ec6SMatthias Ringwald                 // signature is { sequence counter, secure hash }
5843deb3ec6SMatthias Ringwald                 sm_key_t csrk;
58518707219SMatthias Ringwald                 le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk);
58618707219SMatthias Ringwald                 att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION;
5873deb3ec6SMatthias Ringwald                 log_info("Orig Signature: ");
58818707219SMatthias Ringwald                 log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8);
58918707219SMatthias Ringwald                 uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1);
59018707219SMatthias 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);
5913deb3ec6SMatthias Ringwald                 return;
5923deb3ec6SMatthias Ringwald             }
5937a766ebfSMatthias Ringwald #endif
594b06f2579SMatthias Ringwald             // move on
59518707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
59618707219SMatthias Ringwald             att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
597b06f2579SMatthias Ringwald             break;
59888a48dd8SMatthias Ringwald 
5993deb3ec6SMatthias Ringwald         default:
6003deb3ec6SMatthias Ringwald             break;
6013deb3ec6SMatthias Ringwald     }
6023deb3ec6SMatthias Ringwald }
6033deb3ec6SMatthias Ringwald 
60490f03c80SMatthias Ringwald static int att_server_data_ready_for_phase(att_server_t * att_server,  att_server_run_phase_t phase){
60590f03c80SMatthias Ringwald     switch (phase){
60690f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
60790f03c80SMatthias Ringwald             return att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
60890f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
60990f03c80SMatthias Ringwald              return (!btstack_linked_list_empty(&att_server->indication_requests) && att_server->value_indication_handle == 0);
61090f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
61190f03c80SMatthias Ringwald             return (!btstack_linked_list_empty(&att_server->notification_requests));
61290f03c80SMatthias Ringwald     }
613fa5e3464SMatthias Ringwald     // avoid warning
614fa5e3464SMatthias Ringwald     return 0;
61590f03c80SMatthias Ringwald }
61690f03c80SMatthias Ringwald 
61790f03c80SMatthias Ringwald static void att_server_trigger_send_for_phase(att_server_t * att_server,  att_server_run_phase_t phase){
61890f03c80SMatthias Ringwald     btstack_context_callback_registration_t * client;
61990f03c80SMatthias Ringwald     switch (phase){
62090f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
62190f03c80SMatthias Ringwald             att_server_process_validated_request(att_server);
62290f03c80SMatthias Ringwald             break;
62390f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
62490f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->indication_requests;
62590f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->indication_requests, (btstack_linked_item_t *) client);
62690f03c80SMatthias Ringwald             client->callback(client->context);
62790f03c80SMatthias Ringwald             break;
62890f03c80SMatthias Ringwald        case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
62990f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->notification_requests;
63090f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->notification_requests, (btstack_linked_item_t *) client);
63190f03c80SMatthias Ringwald             client->callback(client->context);
63290f03c80SMatthias Ringwald             break;
63390f03c80SMatthias Ringwald     }
63490f03c80SMatthias Ringwald }
63590f03c80SMatthias Ringwald 
6367eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){
637b06f2579SMatthias Ringwald 
638374a288aSMatthias Ringwald     hci_con_handle_t request_con_handle   = HCI_CON_HANDLE_INVALID;
6396438547aSMatthias Ringwald     hci_con_handle_t last_send_con_handle = HCI_CON_HANDLE_INVALID;
6406438547aSMatthias Ringwald     int can_send_now = 1;
6414395a000SMatthias Ringwald     int phase_index;
6426438547aSMatthias Ringwald 
6434395a000SMatthias Ringwald     for (phase_index = ATT_SERVER_RUN_PHASE_1_REQUESTS; phase_index <= ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS; phase_index++){
6444395a000SMatthias Ringwald         att_server_run_phase_t phase = (att_server_run_phase_t) phase_index;
6456438547aSMatthias Ringwald         hci_con_handle_t skip_connections_until = att_server_last_can_send_now;
646374a288aSMatthias Ringwald         while (1){
6476438547aSMatthias Ringwald             btstack_linked_list_iterator_t it;
64818707219SMatthias Ringwald             hci_connections_get_iterator(&it);
64918707219SMatthias Ringwald             while(btstack_linked_list_iterator_has_next(&it)){
65018707219SMatthias Ringwald                 hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
65118707219SMatthias Ringwald                 att_server_t * att_server = &connection->att_server;
6526438547aSMatthias Ringwald 
65390f03c80SMatthias Ringwald                 int data_ready = att_server_data_ready_for_phase(att_server, phase);
6546438547aSMatthias Ringwald 
6556438547aSMatthias 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);
6566438547aSMatthias Ringwald 
6576438547aSMatthias Ringwald                 // skip until last sender found (which is also skipped)
6586438547aSMatthias Ringwald                 if (skip_connections_until != HCI_CON_HANDLE_INVALID){
6596438547aSMatthias Ringwald                     if (data_ready && request_con_handle == HCI_CON_HANDLE_INVALID){
6606438547aSMatthias Ringwald                         request_con_handle = att_server->connection.con_handle;
6616438547aSMatthias Ringwald                     }
6626438547aSMatthias Ringwald                     if (skip_connections_until == att_server->connection.con_handle){
6636438547aSMatthias Ringwald                         skip_connections_until = HCI_CON_HANDLE_INVALID;
6646438547aSMatthias Ringwald                     }
6656438547aSMatthias Ringwald                     continue;
6666438547aSMatthias Ringwald                 };
6676438547aSMatthias Ringwald 
66890f03c80SMatthias Ringwald                 if (data_ready){
669969a5bbaSMatthias Ringwald                     if (can_send_now){
67090f03c80SMatthias Ringwald                         att_server_trigger_send_for_phase(att_server, phase);
6716438547aSMatthias Ringwald                         last_send_con_handle = att_server->connection.con_handle;
672969a5bbaSMatthias Ringwald                         can_send_now = att_dispatch_server_can_send_now(att_server->connection.con_handle);
67390f03c80SMatthias Ringwald                         data_ready = att_server_data_ready_for_phase(att_server, phase);
6746438547aSMatthias Ringwald                         if (data_ready && request_con_handle == HCI_CON_HANDLE_INVALID){
675cbbb12d9SMatthias Ringwald                             request_con_handle = att_server->connection.con_handle;
676cbbb12d9SMatthias Ringwald                         }
677cbbb12d9SMatthias Ringwald                     } else {
678f29a88d8SMatthias Ringwald                         request_con_handle = att_server->connection.con_handle;
679f29a88d8SMatthias Ringwald                         break;
680cbbb12d9SMatthias Ringwald                     }
681cbbb12d9SMatthias Ringwald                 }
6823deb3ec6SMatthias Ringwald             }
6833deb3ec6SMatthias Ringwald 
6846438547aSMatthias Ringwald             // stop skipping (handles disconnect by last send connection)
6856438547aSMatthias Ringwald             skip_connections_until = HCI_CON_HANDLE_INVALID;
6866438547aSMatthias Ringwald 
6873551936eSMatthias Ringwald             // Exit loop, if we cannot send
688969a5bbaSMatthias Ringwald             if (!can_send_now) break;
689969a5bbaSMatthias Ringwald 
690969a5bbaSMatthias Ringwald             // Exit loop, if we can send but there are also no further request
691969a5bbaSMatthias Ringwald             if (request_con_handle == HCI_CON_HANDLE_INVALID) break;
692969a5bbaSMatthias Ringwald 
693969a5bbaSMatthias Ringwald             // Finally, if we still can send and there are requests, just try again
694969a5bbaSMatthias Ringwald             request_con_handle = HCI_CON_HANDLE_INVALID;
695969a5bbaSMatthias Ringwald         }
6966438547aSMatthias Ringwald         // update last send con handle for round robin
6976438547aSMatthias Ringwald         if (last_send_con_handle != HCI_CON_HANDLE_INVALID){
6986438547aSMatthias Ringwald             att_server_last_can_send_now = last_send_con_handle;
6996438547aSMatthias Ringwald         }
7003551936eSMatthias Ringwald     }
701969a5bbaSMatthias Ringwald 
702969a5bbaSMatthias Ringwald     if (request_con_handle == HCI_CON_HANDLE_INVALID) return;
703969a5bbaSMatthias Ringwald     att_dispatch_server_request_can_send_now_event(request_con_handle);
704969a5bbaSMatthias Ringwald }
705969a5bbaSMatthias Ringwald 
706*41772f37SMatthias Ringwald static void att_server_request_can_send_now(att_server_t * att_server){
707*41772f37SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
708*41772f37SMatthias Ringwald     if (att_server->l2cap_cid != 0){
709*41772f37SMatthias Ringwald         l2cap_request_can_send_now_event(att_server->l2cap_cid);
710*41772f37SMatthias Ringwald         return;
711bd87a16eSMatthias Ringwald     }
712*41772f37SMatthias Ringwald #endif
713*41772f37SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
714*41772f37SMatthias Ringwald }
7153deb3ec6SMatthias Ringwald 
716*41772f37SMatthias Ringwald static void att_server_handle_att_pdu(att_server_t * att_server, uint8_t * packet, uint16_t size){
71718707219SMatthias Ringwald 
7183deb3ec6SMatthias Ringwald     // handle value indication confirms
71918707219SMatthias Ringwald     if (packet[0] == ATT_HANDLE_VALUE_CONFIRMATION && att_server->value_indication_handle){
72018707219SMatthias Ringwald         btstack_run_loop_remove_timer(&att_server->value_indication_timer);
72118707219SMatthias Ringwald         uint16_t att_handle = att_server->value_indication_handle;
72218707219SMatthias Ringwald         att_server->value_indication_handle = 0;
72318707219SMatthias Ringwald         att_handle_value_indication_notify_client(0, att_server->connection.con_handle, att_handle);
724*41772f37SMatthias Ringwald         att_server_request_can_send_now(att_server);
7253deb3ec6SMatthias Ringwald         return;
7263deb3ec6SMatthias Ringwald     }
7273deb3ec6SMatthias Ringwald 
7286428eb5aSMatthias Ringwald     // directly process command
7296428eb5aSMatthias Ringwald     // note: signed write cannot be handled directly as authentication needs to be verified
7306428eb5aSMatthias Ringwald     if (packet[0] == ATT_WRITE_COMMAND){
73118707219SMatthias Ringwald         att_handle_request(&att_server->connection, packet, size, 0);
7326428eb5aSMatthias Ringwald         return;
7336428eb5aSMatthias Ringwald     }
7346428eb5aSMatthias Ringwald 
7353deb3ec6SMatthias Ringwald     // check size
73618707219SMatthias Ringwald     if (size > sizeof(att_server->request_buffer)) {
737e0463bdcSMatthias 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));
7386428eb5aSMatthias Ringwald         return;
7396428eb5aSMatthias Ringwald     }
7403deb3ec6SMatthias Ringwald 
741e0463bdcSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
742e0463bdcSMatthias Ringwald     // abort signed write validation if a new request comes in (but finish previous signed write if possible)
743e0463bdcSMatthias Ringwald     if (att_server->state == ATT_SERVER_W4_SIGNED_WRITE_VALIDATION){
744e0463bdcSMatthias Ringwald         if (packet[0] == ATT_SIGNED_WRITE_COMMAND){
745e0463bdcSMatthias Ringwald             log_info("skip new signed write request as previous is in validation");
746e0463bdcSMatthias Ringwald             return;
747e0463bdcSMatthias Ringwald         } else {
748e0463bdcSMatthias Ringwald             log_info("abort signed write validation to process new request");
749e0463bdcSMatthias Ringwald             att_server->state = ATT_SERVER_IDLE;
750e0463bdcSMatthias Ringwald         }
751e0463bdcSMatthias Ringwald     }
752e0463bdcSMatthias Ringwald #endif
7533deb3ec6SMatthias Ringwald     // last request still in processing?
75418707219SMatthias Ringwald     if (att_server->state != ATT_SERVER_IDLE){
755e0463bdcSMatthias Ringwald         log_info("skip att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state);
7566428eb5aSMatthias Ringwald         return;
7576428eb5aSMatthias Ringwald     }
7583deb3ec6SMatthias Ringwald 
7593deb3ec6SMatthias Ringwald     // store request
76018707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED;
76118707219SMatthias Ringwald     att_server->request_size = size;
76218707219SMatthias Ringwald     memcpy(att_server->request_buffer, packet, size);
7633deb3ec6SMatthias Ringwald 
76418707219SMatthias Ringwald     att_run_for_context(att_server);
765*41772f37SMatthias Ringwald }
766*41772f37SMatthias Ringwald 
767*41772f37SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
768*41772f37SMatthias Ringwald     att_server_t * att_server;
769*41772f37SMatthias Ringwald 
770*41772f37SMatthias Ringwald     switch (packet_type){
771*41772f37SMatthias Ringwald         case HCI_EVENT_PACKET:
772*41772f37SMatthias Ringwald             switch (packet[0]){
773*41772f37SMatthias Ringwald                 case L2CAP_EVENT_CAN_SEND_NOW:
774*41772f37SMatthias Ringwald                     att_server_handle_can_send_now();
775*41772f37SMatthias Ringwald                     break;
776*41772f37SMatthias Ringwald                 case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
777*41772f37SMatthias Ringwald                     // GATT client has negotiated the mtu for this connection
778*41772f37SMatthias Ringwald                     att_server = att_server_for_handle(handle);
779*41772f37SMatthias Ringwald                     if (!att_server) break;
780*41772f37SMatthias Ringwald                     att_server->connection.mtu = little_endian_read_16(packet, 4);
781*41772f37SMatthias Ringwald                     break;
782*41772f37SMatthias Ringwald                 default:
783*41772f37SMatthias Ringwald                     break;
784*41772f37SMatthias Ringwald             }
785*41772f37SMatthias Ringwald             break;
786*41772f37SMatthias Ringwald 
787*41772f37SMatthias Ringwald         case ATT_DATA_PACKET:
788*41772f37SMatthias Ringwald             log_debug("ATT Packet, handle 0x%04x", handle);
789*41772f37SMatthias Ringwald             att_server = att_server_for_handle(handle);
790*41772f37SMatthias Ringwald             if (!att_server) break;
791*41772f37SMatthias Ringwald 
792*41772f37SMatthias Ringwald             att_server_handle_att_pdu(att_server, packet, size);
793372d62c4SMatthias Ringwald             break;
794372d62c4SMatthias Ringwald     }
7953deb3ec6SMatthias Ringwald }
7963deb3ec6SMatthias Ringwald 
797bf78aac6SMatthias Ringwald // ---------------------
798bf78aac6SMatthias Ringwald // persistent CCC writes
799bf78aac6SMatthias Ringwald static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){
800bf78aac6SMatthias Ringwald     return 'B' << 24 | 'T' << 16 | 'C' << 8 | index;
801bf78aac6SMatthias Ringwald }
802bf78aac6SMatthias Ringwald 
803bf78aac6SMatthias Ringwald static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){
804bf78aac6SMatthias Ringwald     // lookup att_server instance
805bf78aac6SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
806bf78aac6SMatthias Ringwald     if (!att_server) return;
807bf78aac6SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
808bf78aac6SMatthias 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);
8096a540790SMatthias Ringwald 
810bf78aac6SMatthias Ringwald     // check if bonded
811bf78aac6SMatthias Ringwald     if (le_device_index < 0) return;
8126a540790SMatthias Ringwald 
813bf78aac6SMatthias Ringwald     // get btstack_tlv
814bf78aac6SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
815bf78aac6SMatthias Ringwald     void * tlv_context;
816bf78aac6SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
817bf78aac6SMatthias Ringwald     if (!tlv_impl) return;
8186a540790SMatthias Ringwald 
819bf78aac6SMatthias Ringwald     // update ccc tag
820bf78aac6SMatthias Ringwald     int index;
8216a540790SMatthias Ringwald     uint32_t highest_seq_nr = 0;
8226a540790SMatthias Ringwald     uint32_t lowest_seq_nr = 0;
8236a540790SMatthias Ringwald     uint32_t tag_for_lowest_seq_nr = 0;
8246a540790SMatthias Ringwald     uint32_t tag_for_empty = 0;
8256a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
8266afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
827bf78aac6SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
8286a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
8296a540790SMatthias Ringwald 
8306a540790SMatthias Ringwald         // empty/invalid tag
8316a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)){
8326a540790SMatthias Ringwald             tag_for_empty = tag;
833bf78aac6SMatthias Ringwald             continue;
834bf78aac6SMatthias Ringwald         }
8356a540790SMatthias Ringwald         // update highest seq nr
8366a540790SMatthias Ringwald         if (entry.seq_nr > highest_seq_nr){
8376a540790SMatthias Ringwald             highest_seq_nr = entry.seq_nr;
8386a540790SMatthias Ringwald         }
8396a540790SMatthias Ringwald         // find entry with lowest seq nr
8406a540790SMatthias Ringwald         if ((tag_for_lowest_seq_nr == 0) || (entry.seq_nr < lowest_seq_nr)){
8416a540790SMatthias Ringwald             tag_for_lowest_seq_nr = tag;
8426a540790SMatthias Ringwald             lowest_seq_nr = entry.seq_nr;
8436a540790SMatthias Ringwald         }
8446a540790SMatthias Ringwald 
8456a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
8466a540790SMatthias Ringwald         if (entry.att_handle   != att_handle)      continue;
8476a540790SMatthias Ringwald 
8486a540790SMatthias Ringwald         // found matching entry
84901ac2008SMatthias Ringwald         if (value){
850bf78aac6SMatthias Ringwald             // update
8516a540790SMatthias Ringwald             if (entry.value == value) {
852760ad805SMatthias Ringwald                 log_info("CCC Index %u: Up-to-date", index);
853760ad805SMatthias Ringwald                 return;
854760ad805SMatthias Ringwald             }
8556a540790SMatthias Ringwald             entry.value = value;
8566a540790SMatthias Ringwald             entry.seq_nr = highest_seq_nr + 1;
857760ad805SMatthias Ringwald             log_info("CCC Index %u: Store", index);
8586a540790SMatthias Ringwald             tlv_impl->store_tag(tlv_context, tag, (const uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
85901ac2008SMatthias Ringwald         } else {
86001ac2008SMatthias Ringwald             // delete
861760ad805SMatthias Ringwald             log_info("CCC Index %u: Delete", index);
86201ac2008SMatthias Ringwald             tlv_impl->delete_tag(tlv_context, tag);
86301ac2008SMatthias Ringwald         }
864bf78aac6SMatthias Ringwald         return;
865bf78aac6SMatthias Ringwald     }
8666a540790SMatthias Ringwald 
867faf2b6c3SMatthias Ringwald     log_info("tag_for_empy %"PRIx32", tag_for_lowest_seq_nr %"PRIx32, tag_for_empty, tag_for_lowest_seq_nr);
8686a540790SMatthias Ringwald 
8696a540790SMatthias Ringwald     if (value == 0){
8706a540790SMatthias Ringwald         // done
8716a540790SMatthias Ringwald         return;
8726a540790SMatthias Ringwald     }
8736a540790SMatthias Ringwald 
8746a540790SMatthias Ringwald     uint32_t tag_to_use = 0;
8756a540790SMatthias Ringwald     if (tag_for_empty){
8766a540790SMatthias Ringwald         tag_to_use = tag_for_empty;
8776a540790SMatthias Ringwald     } else if (tag_for_lowest_seq_nr){
8786a540790SMatthias Ringwald         tag_to_use = tag_for_lowest_seq_nr;
8796a540790SMatthias Ringwald     } else {
8806a540790SMatthias Ringwald         // should not happen
8816a540790SMatthias Ringwald         return;
8826a540790SMatthias Ringwald     }
883bf78aac6SMatthias Ringwald     // store ccc tag
8846a540790SMatthias Ringwald     entry.seq_nr       = highest_seq_nr + 1;
8856a540790SMatthias Ringwald     entry.device_index = le_device_index;
8866a540790SMatthias Ringwald     entry.att_handle   = att_handle;
8876a540790SMatthias Ringwald     entry.value        = value;
8886a540790SMatthias Ringwald     tlv_impl->store_tag(tlv_context, tag_to_use, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
889bf78aac6SMatthias Ringwald }
890bf78aac6SMatthias Ringwald 
891760ad805SMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server){
892760ad805SMatthias Ringwald     if (!att_server) return;
893760ad805SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
894760ad805SMatthias Ringwald     log_info("Clear CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
895760ad805SMatthias Ringwald     // check if bonded
896760ad805SMatthias Ringwald     if (le_device_index < 0) return;
897760ad805SMatthias Ringwald     // get btstack_tlv
898760ad805SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
899760ad805SMatthias Ringwald     void * tlv_context;
900760ad805SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
901760ad805SMatthias Ringwald     if (!tlv_impl) return;
902760ad805SMatthias Ringwald     // get all ccc tag
903760ad805SMatthias Ringwald     int index;
9046a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
9056afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
906760ad805SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
9076a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
9086a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
9096a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
910760ad805SMatthias Ringwald         // delete entry
911760ad805SMatthias Ringwald         log_info("CCC Index %u: Delete", index);
912760ad805SMatthias Ringwald         tlv_impl->delete_tag(tlv_context, tag);
913760ad805SMatthias Ringwald     }
914760ad805SMatthias Ringwald }
915760ad805SMatthias Ringwald 
91601ac2008SMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server){
91701ac2008SMatthias Ringwald     if (!att_server) return;
91801ac2008SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
91901ac2008SMatthias Ringwald     log_info("Restore CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
9201b7acd0dSMatthias Ringwald     // check if bonded
9211b7acd0dSMatthias Ringwald     if (le_device_index < 0) return;
92201ac2008SMatthias Ringwald     // get btstack_tlv
92301ac2008SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
92401ac2008SMatthias Ringwald     void * tlv_context;
92501ac2008SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
92601ac2008SMatthias Ringwald     if (!tlv_impl) return;
92701ac2008SMatthias Ringwald     // get all ccc tag
92801ac2008SMatthias Ringwald     int index;
9296a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
9306afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
93101ac2008SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
9326a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
9336a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
9346a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
93501ac2008SMatthias Ringwald         // simulate write callback
9366a540790SMatthias Ringwald         uint16_t attribute_handle = entry.att_handle;
93701ac2008SMatthias Ringwald         uint8_t  value[2];
9386a540790SMatthias Ringwald         little_endian_store_16(value, 0, entry.value);
93901ac2008SMatthias Ringwald         att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
94001ac2008SMatthias Ringwald         if (!callback) continue;
9416a540790SMatthias Ringwald         log_info("CCC Index %u: Set Attribute handle 0x%04x to value 0x%04x", index, attribute_handle, entry.value );
94201ac2008SMatthias Ringwald         (*callback)(att_server->connection.con_handle, attribute_handle, ATT_TRANSACTION_MODE_NONE, 0, value, sizeof(value));
94301ac2008SMatthias Ringwald     }
94401ac2008SMatthias Ringwald }
94501ac2008SMatthias Ringwald 
946bf78aac6SMatthias Ringwald // persistent CCC writes
947bf78aac6SMatthias Ringwald // ---------------------
9483d71c7a4SMatthias Ringwald 
9493d71c7a4SMatthias Ringwald // gatt service management
9503d71c7a4SMatthias Ringwald static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){
9513d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
9523d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
9533d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
9543d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
9553d71c7a4SMatthias Ringwald         if (handler->start_handle > handle) continue;
9563d71c7a4SMatthias Ringwald         if (handler->end_handle   < handle) continue;
9573d71c7a4SMatthias Ringwald         return handler;
9583d71c7a4SMatthias Ringwald     }
9593d71c7a4SMatthias Ringwald     return NULL;
9603d71c7a4SMatthias Ringwald }
9613d71c7a4SMatthias Ringwald static att_read_callback_t att_server_read_callback_for_handle(uint16_t handle){
9623d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
9633d71c7a4SMatthias Ringwald     if (handler) return handler->read_callback;
9643d71c7a4SMatthias Ringwald     return att_server_client_read_callback;
9653d71c7a4SMatthias Ringwald }
9663d71c7a4SMatthias Ringwald 
9673d71c7a4SMatthias Ringwald static att_write_callback_t att_server_write_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->write_callback;
9703d71c7a4SMatthias Ringwald     return att_server_client_write_callback;
9713d71c7a4SMatthias Ringwald }
9723d71c7a4SMatthias Ringwald 
9735d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle){
9745d07396bSMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
9755d07396bSMatthias Ringwald     if (handler) return handler->packet_handler;
9765d07396bSMatthias Ringwald     return att_client_packet_handler;
9775d07396bSMatthias Ringwald }
9785d07396bSMatthias Ringwald 
9793d71c7a4SMatthias Ringwald static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){
9803d71c7a4SMatthias Ringwald     // notify all callbacks
9813d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
9823d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
9833d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
9843d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
9853d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
9863d71c7a4SMatthias Ringwald         (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
9873d71c7a4SMatthias Ringwald     }
9883d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return;
9893d71c7a4SMatthias Ringwald     (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
9903d71c7a4SMatthias Ringwald }
9913d71c7a4SMatthias Ringwald 
9923d71c7a4SMatthias Ringwald // returns first reported error or 0
9933d71c7a4SMatthias Ringwald static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){
9943d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
9953d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
9963d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
9973d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
9983d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
9993d71c7a4SMatthias Ringwald         uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
10003d71c7a4SMatthias Ringwald         if (error_code) return error_code;
10013d71c7a4SMatthias Ringwald     }
10023d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return 0;
10033d71c7a4SMatthias Ringwald     return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
10043d71c7a4SMatthias Ringwald }
10053d71c7a4SMatthias Ringwald 
10063d71c7a4SMatthias 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){
10073d71c7a4SMatthias Ringwald     att_read_callback_t callback = att_server_read_callback_for_handle(attribute_handle);
100801ac2008SMatthias Ringwald     if (!callback) return 0;
10093d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, offset, buffer, buffer_size);
10103d71c7a4SMatthias Ringwald }
10113d71c7a4SMatthias Ringwald 
10123d71c7a4SMatthias 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){
10133d71c7a4SMatthias Ringwald     switch (transaction_mode){
10143d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_VALIDATE:
10153d71c7a4SMatthias Ringwald             return att_validate_prepared_write(con_handle);
10163d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_EXECUTE:
10173d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_CANCEL:
10183d71c7a4SMatthias Ringwald             att_notify_write_callbacks(con_handle, transaction_mode);
10193d71c7a4SMatthias Ringwald             return 0;
10203d71c7a4SMatthias Ringwald         default:
10213d71c7a4SMatthias Ringwald             break;
10223d71c7a4SMatthias Ringwald     }
10233d71c7a4SMatthias Ringwald 
1024bf78aac6SMatthias Ringwald     // track CCC writes
1025bf78aac6SMatthias Ringwald     if (att_is_persistent_ccc(attribute_handle) && offset == 0 && buffer_size == 2){
1026bf78aac6SMatthias Ringwald         att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0));
1027bf78aac6SMatthias Ringwald     }
1028bf78aac6SMatthias Ringwald 
102901ac2008SMatthias Ringwald     att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
103001ac2008SMatthias Ringwald     if (!callback) return 0;
10313d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size);
10323d71c7a4SMatthias Ringwald }
10333d71c7a4SMatthias Ringwald 
10343d71c7a4SMatthias Ringwald /**
10353d71c7a4SMatthias Ringwald  * @brief register read/write callbacks for specific handle range
10363d71c7a4SMatthias Ringwald  * @param att_service_handler_t
10373d71c7a4SMatthias Ringwald  */
10383d71c7a4SMatthias Ringwald void att_server_register_service_handler(att_service_handler_t * handler){
10393d71c7a4SMatthias Ringwald     if (att_service_handler_for_handle(handler->start_handle) ||
10403d71c7a4SMatthias Ringwald         att_service_handler_for_handle(handler->end_handle)){
10413d71c7a4SMatthias Ringwald         log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle);
10423d71c7a4SMatthias Ringwald         return;
10433d71c7a4SMatthias Ringwald     }
10443d71c7a4SMatthias Ringwald     btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler);
10453d71c7a4SMatthias Ringwald }
10463d71c7a4SMatthias Ringwald 
10473deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){
10483deb3ec6SMatthias Ringwald 
10493d71c7a4SMatthias Ringwald     // store callbacks
10503d71c7a4SMatthias Ringwald     att_server_client_read_callback  = read_callback;
10513d71c7a4SMatthias Ringwald     att_server_client_write_callback = write_callback;
10523d71c7a4SMatthias Ringwald 
10531a389cdcSMatthias Ringwald     // register for HCI Events
1054d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &att_event_packet_handler;
10551a389cdcSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
10561a389cdcSMatthias Ringwald 
1057406722e4SMatthias Ringwald     // register for SM events
1058d9a7306aSMatthias Ringwald     sm_event_callback_registration.callback = &att_event_packet_handler;
1059406722e4SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
10603deb3ec6SMatthias Ringwald 
10611a389cdcSMatthias Ringwald     // and L2CAP ATT Server PDUs
10623deb3ec6SMatthias Ringwald     att_dispatch_register_server(att_packet_handler);
10633deb3ec6SMatthias Ringwald 
106470dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
106570dca4d4SMatthias Ringwald     // setup l2cap service
106670dca4d4SMatthias Ringwald     l2cap_register_service(&att_event_packet_handler, PSM_ATT, 0xffff, LEVEL_2);
106770dca4d4SMatthias Ringwald #endif
106870dca4d4SMatthias Ringwald 
10693deb3ec6SMatthias Ringwald     att_set_db(db);
10703d71c7a4SMatthias Ringwald     att_set_read_callback(att_server_read_callback);
10713d71c7a4SMatthias Ringwald     att_set_write_callback(att_server_write_callback);
10723deb3ec6SMatthias Ringwald }
10733deb3ec6SMatthias Ringwald 
10743deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){
10753deb3ec6SMatthias Ringwald     att_client_packet_handler = handler;
10763deb3ec6SMatthias Ringwald }
10773deb3ec6SMatthias Ringwald 
1078cbbb12d9SMatthias Ringwald 
1079cbbb12d9SMatthias Ringwald // to be deprecated
1080c141988cSMatthias Ringwald int  att_server_can_send_packet_now(hci_con_handle_t con_handle){
108118707219SMatthias Ringwald     return att_dispatch_server_can_send_now(con_handle);
10821b96b007SMatthias Ringwald }
10831b96b007SMatthias Ringwald 
1084cbbb12d9SMatthias Ringwald int att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
1085cbbb12d9SMatthias Ringwald     return att_server_request_to_send_notification(callback_registration, con_handle);
10863deb3ec6SMatthias Ringwald }
10873deb3ec6SMatthias Ringwald 
1088cbbb12d9SMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){
1089cbbb12d9SMatthias Ringwald     att_client_waiting_for_can_send_registration.callback = &att_emit_can_send_now_event;
1090cbbb12d9SMatthias Ringwald     att_server_request_to_send_notification(&att_client_waiting_for_can_send_registration, con_handle);
1091cbbb12d9SMatthias Ringwald }
1092cbbb12d9SMatthias Ringwald // end of deprecated
1093cbbb12d9SMatthias Ringwald 
1094cbbb12d9SMatthias Ringwald int att_server_request_to_send_notification(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
1095969a5bbaSMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
1096969a5bbaSMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1097cbbb12d9SMatthias Ringwald     btstack_linked_list_add_tail(&att_server->notification_requests, (btstack_linked_item_t*) callback_registration);
1098cbbb12d9SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
1099cbbb12d9SMatthias Ringwald     return ERROR_CODE_SUCCESS;
1100cbbb12d9SMatthias Ringwald }
1101cbbb12d9SMatthias Ringwald 
1102cbbb12d9SMatthias Ringwald int att_server_request_to_send_indication(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
1103cbbb12d9SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
1104cbbb12d9SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1105cbbb12d9SMatthias Ringwald     btstack_linked_list_add_tail(&att_server->indication_requests, (btstack_linked_item_t*) callback_registration);
1106bb38f057SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
1107969a5bbaSMatthias Ringwald     return ERROR_CODE_SUCCESS;
1108bb38f057SMatthias Ringwald }
1109bb38f057SMatthias Ringwald 
11103bb3035fSMilanka Ringwald int att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){
111118707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
111218707219SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
111318707219SMatthias Ringwald 
111418707219SMatthias Ringwald     if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
11153deb3ec6SMatthias Ringwald 
11163deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
11173deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
111818707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_notification(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
111918707219SMatthias Ringwald 	return l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
11203deb3ec6SMatthias Ringwald }
11213deb3ec6SMatthias Ringwald 
11223bb3035fSMilanka Ringwald int att_server_indicate(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;
112518707219SMatthias Ringwald 
1126eaac31e8SMatthias Ringwald     if (att_server->value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS;
112718707219SMatthias Ringwald     if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
11283deb3ec6SMatthias Ringwald 
11293deb3ec6SMatthias Ringwald     // track indication
113018707219SMatthias Ringwald     att_server->value_indication_handle = attribute_handle;
113118707219SMatthias Ringwald     btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout);
113218707219SMatthias Ringwald     btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS);
113318707219SMatthias Ringwald     btstack_run_loop_add_timer(&att_server->value_indication_timer);
11343deb3ec6SMatthias Ringwald 
11353deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
11363deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
113718707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_indication(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
113818707219SMatthias Ringwald 	l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
11393deb3ec6SMatthias Ringwald     return 0;
11403deb3ec6SMatthias Ringwald }
11418f9132b5SMilanka Ringwald 
11428f9132b5SMilanka Ringwald uint16_t att_server_get_mtu(hci_con_handle_t con_handle){
11438f9132b5SMilanka Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
11448f9132b5SMilanka Ringwald     if (!att_server) return 0;
11458f9132b5SMilanka Ringwald     return att_server->connection.mtu;
11468f9132b5SMilanka Ringwald }
1147