xref: /btstack/src/ble/att_server.c (revision 70dca4d47d58b92caadb53781864b0b5ff44fd75)
13deb3ec6SMatthias Ringwald /*
23deb3ec6SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
33deb3ec6SMatthias Ringwald  *
43deb3ec6SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
53deb3ec6SMatthias Ringwald  * modification, are permitted provided that the following conditions
63deb3ec6SMatthias Ringwald  * are met:
73deb3ec6SMatthias Ringwald  *
83deb3ec6SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
93deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
103deb3ec6SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
113deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
123deb3ec6SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
133deb3ec6SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
143deb3ec6SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
153deb3ec6SMatthias Ringwald  *    from this software without specific prior written permission.
163deb3ec6SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
173deb3ec6SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
183deb3ec6SMatthias Ringwald  *    monetary gain.
193deb3ec6SMatthias Ringwald  *
203deb3ec6SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
213deb3ec6SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
223deb3ec6SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
233deb3ec6SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
243deb3ec6SMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
253deb3ec6SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
263deb3ec6SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
273deb3ec6SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
283deb3ec6SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
293deb3ec6SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
303deb3ec6SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313deb3ec6SMatthias Ringwald  * SUCH DAMAGE.
323deb3ec6SMatthias Ringwald  *
333deb3ec6SMatthias Ringwald  * Please inquire about commercial licensing options at
343deb3ec6SMatthias Ringwald  * [email protected]
353deb3ec6SMatthias Ringwald  *
363deb3ec6SMatthias Ringwald  */
373deb3ec6SMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "att_server.c"
39ab2c6ae4SMatthias Ringwald 
403deb3ec6SMatthias Ringwald 
413deb3ec6SMatthias Ringwald //
423deb3ec6SMatthias Ringwald // ATT Server Globals
433deb3ec6SMatthias Ringwald //
443deb3ec6SMatthias Ringwald 
453deb3ec6SMatthias Ringwald #include <stdint.h>
463deb3ec6SMatthias Ringwald #include <string.h>
473deb3ec6SMatthias Ringwald #include <inttypes.h>
483deb3ec6SMatthias Ringwald 
497907f069SMatthias Ringwald #include "btstack_config.h"
503deb3ec6SMatthias Ringwald 
5159c6af15SMatthias Ringwald #include "att_dispatch.h"
5259c6af15SMatthias Ringwald #include "ble/att_db.h"
5359c6af15SMatthias Ringwald #include "ble/att_server.h"
5459c6af15SMatthias Ringwald #include "ble/core.h"
5559c6af15SMatthias Ringwald #include "ble/le_device_db.h"
5659c6af15SMatthias Ringwald #include "ble/sm.h"
5716ece135SMatthias Ringwald #include "btstack_debug.h"
580e2df43fSMatthias Ringwald #include "btstack_event.h"
593deb3ec6SMatthias Ringwald #include "btstack_memory.h"
600e2df43fSMatthias Ringwald #include "btstack_run_loop.h"
6159c6af15SMatthias Ringwald #include "gap.h"
623deb3ec6SMatthias Ringwald #include "hci.h"
633deb3ec6SMatthias Ringwald #include "hci_dump.h"
643deb3ec6SMatthias Ringwald #include "l2cap.h"
65bf78aac6SMatthias Ringwald #include "btstack_tlv.h"
66d1a1f6a4SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
67d1a1f6a4SMatthias Ringwald #include "ble/sm.h"
68d1a1f6a4SMatthias Ringwald #endif
69bf78aac6SMatthias Ringwald 
706afb9acaSMatthias Ringwald #ifndef NVN_NUM_GATT_SERVER_CCC
716afb9acaSMatthias Ringwald #define NVN_NUM_GATT_SERVER_CCC 20
72bf78aac6SMatthias Ringwald #endif
733deb3ec6SMatthias Ringwald 
7418707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server);
7501ac2008SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle);
765d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle);
7701ac2008SMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server);
78760ad805SMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server);
793deb3ec6SMatthias Ringwald 
803551936eSMatthias Ringwald typedef enum {
813551936eSMatthias Ringwald     ATT_SERVER_RUN_PHASE_1_REQUESTS,
823551936eSMatthias Ringwald     ATT_SERVER_RUN_PHASE_2_INDICATIONS,
833551936eSMatthias Ringwald     ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS,
843551936eSMatthias Ringwald } att_server_run_phase_t;
853551936eSMatthias Ringwald 
866a540790SMatthias Ringwald //
876a540790SMatthias Ringwald typedef struct {
886a540790SMatthias Ringwald     uint32_t seq_nr;
896a540790SMatthias Ringwald     uint16_t att_handle;
906a540790SMatthias Ringwald     uint8_t  value;
916a540790SMatthias Ringwald     uint8_t  device_index;
926a540790SMatthias Ringwald } persistent_ccc_entry_t;
936a540790SMatthias Ringwald 
9418707219SMatthias Ringwald // global
951a389cdcSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
96406722e4SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration;
973deb3ec6SMatthias Ringwald static btstack_packet_handler_t               att_client_packet_handler = NULL;
983d71c7a4SMatthias Ringwald static btstack_linked_list_t                  service_handlers;
995f141511SMatthias Ringwald static btstack_context_callback_registration_t att_client_waiting_for_can_send_registration;
10018707219SMatthias Ringwald 
1013d71c7a4SMatthias Ringwald static att_read_callback_t                    att_server_client_read_callback;
1023d71c7a4SMatthias Ringwald static att_write_callback_t                   att_server_client_write_callback;
1033d71c7a4SMatthias Ringwald 
1046438547aSMatthias Ringwald // round robin
1056438547aSMatthias Ringwald static hci_con_handle_t att_server_last_can_send_now = HCI_CON_HANDLE_INVALID;
106bf78aac6SMatthias Ringwald 
10718707219SMatthias Ringwald static att_server_t * att_server_for_handle(hci_con_handle_t con_handle){
10818707219SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
10918707219SMatthias Ringwald     if (!hci_connection) return NULL;
11018707219SMatthias Ringwald     return &hci_connection->att_server;
11118707219SMatthias Ringwald }
11218707219SMatthias Ringwald 
113*70dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
114*70dca4d4SMatthias Ringwald static att_server_t * att_server_for_l2cap_cid(uint16_t l2cap_cid){
115*70dca4d4SMatthias Ringwald     btstack_linked_list_iterator_t it;
116*70dca4d4SMatthias Ringwald     hci_connections_get_iterator(&it);
117*70dca4d4SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
118*70dca4d4SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
119*70dca4d4SMatthias Ringwald         att_server_t * att_server = &connection->att_server;
120*70dca4d4SMatthias Ringwald         if (att_server->l2cap_cid == l2cap_cid) return att_server;
121*70dca4d4SMatthias Ringwald     }
122*70dca4d4SMatthias Ringwald     return NULL;
123*70dca4d4SMatthias Ringwald }
124*70dca4d4SMatthias Ringwald #endif
125*70dca4d4SMatthias Ringwald 
12602b78fc8SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
12718707219SMatthias Ringwald static att_server_t * att_server_for_state(att_server_state_t state){
12818707219SMatthias Ringwald     btstack_linked_list_iterator_t it;
12918707219SMatthias Ringwald     hci_connections_get_iterator(&it);
13018707219SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
13118707219SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
13218707219SMatthias Ringwald         att_server_t * att_server = &connection->att_server;
13318707219SMatthias Ringwald         if (att_server->state == state) return att_server;
13418707219SMatthias Ringwald     }
13518707219SMatthias Ringwald     return NULL;
13618707219SMatthias Ringwald }
13702b78fc8SMatthias Ringwald #endif
138bb38f057SMatthias Ringwald 
1393deb3ec6SMatthias Ringwald static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){
1405d07396bSMatthias Ringwald     btstack_packet_handler_t packet_handler = att_server_packet_handler_for_handle(attribute_handle);
1415d07396bSMatthias Ringwald     if (!packet_handler) return;
1423deb3ec6SMatthias Ringwald 
1433deb3ec6SMatthias Ringwald     uint8_t event[7];
1443deb3ec6SMatthias Ringwald     int pos = 0;
1455611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE;
1463deb3ec6SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
1473deb3ec6SMatthias Ringwald     event[pos++] = status;
148f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, client_handle);
1493deb3ec6SMatthias Ringwald     pos += 2;
150f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, attribute_handle);
151a444112bSMatthias Ringwald     (*packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1523deb3ec6SMatthias Ringwald }
1533deb3ec6SMatthias Ringwald 
1545d07396bSMatthias Ringwald static void att_emit_event_to_all(const uint8_t * event, uint16_t size){
1555d07396bSMatthias Ringwald     // dispatch to app level handler
1565d07396bSMatthias Ringwald     if (att_client_packet_handler){
1575d07396bSMatthias Ringwald         (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size);
1585d07396bSMatthias Ringwald     }
1593deb3ec6SMatthias Ringwald 
1605d07396bSMatthias Ringwald     // dispatch to service handlers
1615d07396bSMatthias Ringwald     btstack_linked_list_iterator_t it;
1625d07396bSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
1635d07396bSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1645d07396bSMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
1655d07396bSMatthias Ringwald         if (!handler->packet_handler) continue;
1665d07396bSMatthias Ringwald         (*handler->packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size);
1675d07396bSMatthias Ringwald     }
1685d07396bSMatthias Ringwald }
1695d07396bSMatthias Ringwald 
1705d07396bSMatthias Ringwald static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){
1713deb3ec6SMatthias Ringwald     uint8_t event[6];
1723deb3ec6SMatthias Ringwald     int pos = 0;
1735611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE;
1743deb3ec6SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
175fc64f94aSMatthias Ringwald     little_endian_store_16(event, pos, con_handle);
1763deb3ec6SMatthias Ringwald     pos += 2;
177f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, mtu);
1785d07396bSMatthias Ringwald 
1795d07396bSMatthias Ringwald     // also dispatch to GATT Clients
180b12646c5SJakob Krantz     att_dispatch_server_mtu_exchanged(con_handle, mtu);
1815d07396bSMatthias Ringwald 
1825d07396bSMatthias Ringwald     // dispatch to app level handler and service handlers
1835d07396bSMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
1843deb3ec6SMatthias Ringwald }
1853deb3ec6SMatthias Ringwald 
1865f141511SMatthias Ringwald static void att_emit_can_send_now_event(void * context){
1875f141511SMatthias Ringwald     UNUSED(context);
1883c97b242SMatthias Ringwald     if (!att_client_packet_handler) return;
1893c97b242SMatthias Ringwald 
1901b96b007SMatthias Ringwald     uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0};
1911b96b007SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1921b96b007SMatthias Ringwald }
1931b96b007SMatthias Ringwald 
194ff1bec95SMatthias Ringwald static void att_emit_connected_event(att_server_t * att_server){
195ff1bec95SMatthias Ringwald     uint8_t event[11];
196ff1bec95SMatthias Ringwald     int pos = 0;
197ff1bec95SMatthias Ringwald     event[pos++] = ATT_EVENT_CONNECTED;
198ff1bec95SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
199ff1bec95SMatthias Ringwald     event[pos++] = att_server->peer_addr_type;
200ff1bec95SMatthias Ringwald     reverse_bd_addr(att_server->peer_address, &event[pos]);
201ff1bec95SMatthias Ringwald     pos += 6;
202ff1bec95SMatthias Ringwald     little_endian_store_16(event, pos, att_server->connection.con_handle);
203ff1bec95SMatthias Ringwald     pos += 2;
204ff1bec95SMatthias Ringwald 
205ff1bec95SMatthias Ringwald     // dispatch to app level handler and service handlers
206ff1bec95SMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
207ff1bec95SMatthias Ringwald }
208ff1bec95SMatthias Ringwald 
209ff1bec95SMatthias Ringwald 
2106187ad4cSMatthias Ringwald static void att_emit_disconnected_event(uint16_t con_handle){
211ff1bec95SMatthias Ringwald     uint8_t event[4];
212ff1bec95SMatthias Ringwald     int pos = 0;
213ff1bec95SMatthias Ringwald     event[pos++] = ATT_EVENT_DISCONNECTED;
214ff1bec95SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
2156187ad4cSMatthias Ringwald     little_endian_store_16(event, pos, con_handle);
216ff1bec95SMatthias Ringwald     pos += 2;
217ff1bec95SMatthias Ringwald 
218ff1bec95SMatthias Ringwald     // dispatch to app level handler and service handlers
219ff1bec95SMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
220ff1bec95SMatthias Ringwald }
221ff1bec95SMatthias Ringwald 
222ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){
22354178543SMatthias Ringwald     void * context = btstack_run_loop_get_timer_context(ts);
22454178543SMatthias Ringwald     hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
22518707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
22618707219SMatthias Ringwald     if (!att_server) return;
227bce48a26SMatthias Ringwald     // @note: after a transcation timeout, no more requests shall be sent over this ATT Bearer
228bce48a26SMatthias Ringwald     // (that's why we don't reset the value_indication_handle)
22918707219SMatthias Ringwald     uint16_t att_handle = att_server->value_indication_handle;
23018707219SMatthias Ringwald     att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_server->connection.con_handle, att_handle);
2313deb3ec6SMatthias Ringwald }
2323deb3ec6SMatthias Ringwald 
2333deb3ec6SMatthias Ringwald static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2349ec2630cSMatthias Ringwald 
2357dad9ff8SMatthias Ringwald     UNUSED(channel); // ok: there is no channel
2367dad9ff8SMatthias Ringwald     UNUSED(size);    // ok: handling own l2cap events
2373deb3ec6SMatthias Ringwald 
23818707219SMatthias Ringwald     att_server_t * att_server;
23918707219SMatthias Ringwald     hci_con_handle_t con_handle;
240*70dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
241*70dca4d4SMatthias Ringwald     bd_addr_t address;
242*70dca4d4SMatthias Ringwald #endif
24318707219SMatthias Ringwald 
2443deb3ec6SMatthias Ringwald     switch (packet_type) {
2453deb3ec6SMatthias Ringwald 
2463deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
2470e2df43fSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
2483deb3ec6SMatthias Ringwald 
249*70dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
250*70dca4d4SMatthias Ringwald                 case L2CAP_EVENT_INCOMING_CONNECTION:
251*70dca4d4SMatthias Ringwald                     l2cap_event_incoming_connection_get_address(packet, address);
252*70dca4d4SMatthias Ringwald                     l2cap_accept_connection(channel);
253*70dca4d4SMatthias Ringwald                     printf("Accept incoming connection from %s\n", bd_addr_to_str(address));
254*70dca4d4SMatthias Ringwald                     break;
255*70dca4d4SMatthias Ringwald                 case L2CAP_EVENT_CHANNEL_OPENED:
256*70dca4d4SMatthias Ringwald                     l2cap_event_channel_opened_get_address(packet, address);
257*70dca4d4SMatthias Ringwald                     att_server = att_server_for_handle(l2cap_event_channel_opened_get_handle(packet));
258*70dca4d4SMatthias Ringwald                     if (!att_server) break;
259*70dca4d4SMatthias Ringwald                     att_server->l2cap_cid = l2cap_event_channel_opened_get_local_cid(packet);
260*70dca4d4SMatthias Ringwald                     printf("Connection opened %s, l2cap cid %04x\n", bd_addr_to_str(address), att_server->l2cap_cid);
261*70dca4d4SMatthias Ringwald                     break;
262*70dca4d4SMatthias Ringwald #endif
2633deb3ec6SMatthias Ringwald                 case HCI_EVENT_LE_META:
2643deb3ec6SMatthias Ringwald                     switch (packet[2]) {
2653deb3ec6SMatthias Ringwald                         case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
26618707219SMatthias Ringwald                             con_handle = little_endian_read_16(packet, 4);
26718707219SMatthias Ringwald                             att_server = att_server_for_handle(con_handle);
26818707219SMatthias Ringwald                             if (!att_server) break;
2693deb3ec6SMatthias Ringwald                         	// store connection info
27018707219SMatthias Ringwald                         	att_server->peer_addr_type = packet[7];
27118707219SMatthias Ringwald                             reverse_bd_addr(&packet[8], att_server->peer_address);
27218707219SMatthias Ringwald                             att_server->connection.con_handle = con_handle;
2733deb3ec6SMatthias Ringwald                             // reset connection properties
27418707219SMatthias Ringwald                             att_server->state = ATT_SERVER_IDLE;
27518707219SMatthias Ringwald                             att_server->connection.mtu = ATT_DEFAULT_MTU;
27618707219SMatthias Ringwald                             att_server->connection.max_mtu = l2cap_max_le_mtu();
27718707219SMatthias Ringwald                             if (att_server->connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){
27818707219SMatthias Ringwald                                 att_server->connection.max_mtu = ATT_REQUEST_BUFFER_SIZE;
27999c58ad0SMatthias Ringwald                             }
28018707219SMatthias Ringwald                             att_server->connection.encryption_key_size = 0;
28118707219SMatthias Ringwald                             att_server->connection.authenticated = 0;
28218707219SMatthias Ringwald 		                	att_server->connection.authorized = 0;
283b2c1e52cSMatthias Ringwald                             // workaround: identity resolving can already be complete, at least store result
284b2c1e52cSMatthias Ringwald                             att_server->ir_le_device_db_index = sm_le_device_index(con_handle);
285760ad805SMatthias Ringwald                             att_server->pairing_active = 0;
286ff1bec95SMatthias Ringwald                             // notify all - old
2875d07396bSMatthias Ringwald                             att_emit_event_to_all(packet, size);
288ff1bec95SMatthias Ringwald                             // notify all - new
289ff1bec95SMatthias Ringwald                             att_emit_connected_event(att_server);
2903deb3ec6SMatthias Ringwald                             break;
2913deb3ec6SMatthias Ringwald 
2923deb3ec6SMatthias Ringwald                         default:
2933deb3ec6SMatthias Ringwald                             break;
2943deb3ec6SMatthias Ringwald                     }
2953deb3ec6SMatthias Ringwald                     break;
2963deb3ec6SMatthias Ringwald 
2973deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_CHANGE:
2983deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE:
2993deb3ec6SMatthias Ringwald                 	// check handle
30018707219SMatthias Ringwald                     con_handle = little_endian_read_16(packet, 3);
30118707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
30218707219SMatthias Ringwald                     if (!att_server) break;
3039c6e867eSMatthias Ringwald                     att_server->connection.encryption_key_size = gap_encryption_key_size(con_handle);
3049c6e867eSMatthias Ringwald                     att_server->connection.authenticated = gap_authenticated(con_handle);
30513eb7322SMatthias Ringwald                     att_server->connection.secure_connection = gap_secure_connection(con_handle);
30613eb7322SMatthias Ringwald                     log_info("encrypted key size %u, authenticated %u, secure connectipon %u",
30713eb7322SMatthias Ringwald                         att_server->connection.encryption_key_size, att_server->connection.authenticated, att_server->connection.secure_connection);
30801ac2008SMatthias Ringwald                     if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){
30901ac2008SMatthias Ringwald                         // restore CCC values when encrypted
31001ac2008SMatthias Ringwald                         if (hci_event_encryption_change_get_encryption_enabled(packet)){
31101ac2008SMatthias Ringwald                             att_server_persistent_ccc_restore(att_server);
31201ac2008SMatthias Ringwald                         }
31301ac2008SMatthias Ringwald                     }
3140234fbbcSMatthias Ringwald                     att_run_for_context(att_server);
3153deb3ec6SMatthias Ringwald                     break;
3163deb3ec6SMatthias Ringwald 
3173deb3ec6SMatthias Ringwald                 case HCI_EVENT_DISCONNECTION_COMPLETE:
31870a390c7SMatthias Ringwald                     // check handle
31918707219SMatthias Ringwald                     con_handle = hci_event_disconnection_complete_get_connection_handle(packet);
32018707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
32118707219SMatthias Ringwald                     if (!att_server) break;
32218707219SMatthias Ringwald                     att_clear_transaction_queue(&att_server->connection);
32318707219SMatthias Ringwald                     att_server->connection.con_handle = 0;
324760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
32518707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
326bce48a26SMatthias Ringwald                     if (att_server->value_indication_handle){
32727328ecbSMatthias Ringwald                         btstack_run_loop_remove_timer(&att_server->value_indication_timer);
328bce48a26SMatthias Ringwald                         uint16_t att_handle = att_server->value_indication_handle;
329bce48a26SMatthias Ringwald                         att_server->value_indication_handle = 0; // reset error state
330bce48a26SMatthias Ringwald                         att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_DISCONNECT, att_server->connection.con_handle, att_handle);
331bce48a26SMatthias Ringwald                     }
332ff1bec95SMatthias Ringwald                     // notify all - new
3336187ad4cSMatthias Ringwald                     att_emit_disconnected_event(con_handle);
334ff1bec95SMatthias Ringwald                     // notify all - old
3355d07396bSMatthias Ringwald                     att_emit_event_to_all(packet, size);
3363deb3ec6SMatthias Ringwald                     break;
3373deb3ec6SMatthias Ringwald 
338760ad805SMatthias Ringwald                 // Identity Resolving
3395611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_STARTED:
34018707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_started_get_handle(packet);
34118707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
34218707219SMatthias Ringwald                     if (!att_server) break;
3435611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED");
34418707219SMatthias Ringwald                     att_server->ir_lookup_active = 1;
3453deb3ec6SMatthias Ringwald                     break;
3465611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
347760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
348760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
349760ad805SMatthias Ringwald                     if (!att_server) return;
3501b7acd0dSMatthias Ringwald                     att_server->ir_lookup_active = 0;
3511b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index(packet);
3521b7acd0dSMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED");
3531b7acd0dSMatthias Ringwald                     att_run_for_context(att_server);
3543deb3ec6SMatthias Ringwald                     break;
3555611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_FAILED:
35618707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_failed_get_handle(packet);
35718707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
35818707219SMatthias Ringwald                     if (!att_server) break;
3595611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED");
36018707219SMatthias Ringwald                     att_server->ir_lookup_active = 0;
36118707219SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
36218707219SMatthias Ringwald                     att_run_for_context(att_server);
3633deb3ec6SMatthias Ringwald                     break;
364760ad805SMatthias Ringwald 
365760ad805SMatthias Ringwald                 // Pairing started - delete stored CCC values
366760ad805SMatthias Ringwald                 // - assumes pairing indicates either new device or re-pairing, in both cases there should be no stored CCC values
367760ad805SMatthias Ringwald                 // - assumes that all events have the con handle as the first field
368760ad805SMatthias Ringwald                 case SM_EVENT_JUST_WORKS_REQUEST:
369760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
370760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_INPUT_NUMBER:
371760ad805SMatthias Ringwald                 case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
372760ad805SMatthias Ringwald                     con_handle = sm_event_just_works_request_get_handle(packet);
373760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
374760ad805SMatthias Ringwald                     if (!att_server) break;
375760ad805SMatthias Ringwald                     att_server->pairing_active = 1;
376760ad805SMatthias Ringwald                     log_info("SM Pairing started");
377760ad805SMatthias Ringwald                     if (att_server->ir_le_device_db_index < 0) break;
378760ad805SMatthias Ringwald                     att_server_persistent_ccc_clear(att_server);
379760ad805SMatthias Ringwald                     // index not valid anymore
380760ad805SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
381760ad805SMatthias Ringwald                     break;
382760ad805SMatthias Ringwald 
3831b7acd0dSMatthias Ringwald                 // Bonding completed
384760ad805SMatthias Ringwald                 case SM_EVENT_IDENTITY_CREATED:
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;
388760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
3891b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_created_get_index(packet);
3901b7acd0dSMatthias Ringwald                     att_run_for_context(att_server);
3911b7acd0dSMatthias Ringwald                     break;
3921b7acd0dSMatthias Ringwald 
3931b7acd0dSMatthias Ringwald                 // Pairing complete (with/without bonding=storing of pairing information)
3941b7acd0dSMatthias Ringwald                 case SM_EVENT_PAIRING_COMPLETE:
3951b7acd0dSMatthias Ringwald                     con_handle = sm_event_pairing_complete_get_handle(packet);
3961b7acd0dSMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
3971b7acd0dSMatthias Ringwald                     if (!att_server) return;
3981b7acd0dSMatthias Ringwald                     att_server->pairing_active = 0;
3991b7acd0dSMatthias Ringwald                     att_run_for_context(att_server);
400760ad805SMatthias Ringwald                     break;
401760ad805SMatthias Ringwald 
402760ad805SMatthias Ringwald                 // Authorization
4035611a760SMatthias Ringwald                 case SM_EVENT_AUTHORIZATION_RESULT: {
40418707219SMatthias Ringwald                     con_handle = sm_event_authorization_result_get_handle(packet);
40518707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
40618707219SMatthias Ringwald                     if (!att_server) break;
40718707219SMatthias Ringwald                     att_server->connection.authorized = sm_event_authorization_result_get_authorization_result(packet);
40818707219SMatthias Ringwald                     att_dispatch_server_request_can_send_now_event(con_handle);
4093deb3ec6SMatthias Ringwald                 	break;
4103deb3ec6SMatthias Ringwald                 }
4113deb3ec6SMatthias Ringwald                 default:
4123deb3ec6SMatthias Ringwald                     break;
4133deb3ec6SMatthias Ringwald             }
41465b44ffdSMatthias Ringwald             break;
415*70dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
416*70dca4d4SMatthias Ringwald         case L2CAP_DATA_PACKET:
417*70dca4d4SMatthias Ringwald             att_server = att_server_for_l2cap_cid(channel);
418*70dca4d4SMatthias Ringwald             if (!att_server) break;
419*70dca4d4SMatthias Ringwald             printf("ATT request (server %p): ", att_server);
420*70dca4d4SMatthias Ringwald             printf_hexdump(packet, size);
421*70dca4d4SMatthias Ringwald             break;
422*70dca4d4SMatthias Ringwald #endif
42365b44ffdSMatthias Ringwald         default:
42465b44ffdSMatthias Ringwald             break;
4253deb3ec6SMatthias Ringwald     }
4263deb3ec6SMatthias Ringwald }
4273deb3ec6SMatthias Ringwald 
4287a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
4293deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
4303deb3ec6SMatthias Ringwald 
43118707219SMatthias Ringwald     att_server_t * att_server = att_server_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION);
43218707219SMatthias Ringwald     if (!att_server) return;
4333deb3ec6SMatthias Ringwald 
4343deb3ec6SMatthias Ringwald     uint8_t hash_flipped[8];
4359c80e4ccSMatthias Ringwald     reverse_64(hash, hash_flipped);
43618707219SMatthias Ringwald     if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8)){
4373deb3ec6SMatthias Ringwald         log_info("ATT Signed Write, invalid signature");
43818707219SMatthias Ringwald         att_server->state = ATT_SERVER_IDLE;
4393deb3ec6SMatthias Ringwald         return;
4403deb3ec6SMatthias Ringwald     }
4413deb3ec6SMatthias Ringwald     log_info("ATT Signed Write, valid signature");
4423deb3ec6SMatthias Ringwald 
4433deb3ec6SMatthias Ringwald     // update sequence number
44418707219SMatthias Ringwald     uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
44518707219SMatthias Ringwald     le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1);
44618707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
44718707219SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
4483deb3ec6SMatthias Ringwald }
4497a766ebfSMatthias Ringwald #endif
45088a48dd8SMatthias Ringwald 
45118707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED
452b06f2579SMatthias Ringwald // pre: can send now
453b06f2579SMatthias Ringwald // returns: 1 if packet was sent
45418707219SMatthias Ringwald static int att_server_process_validated_request(att_server_t * att_server){
455b06f2579SMatthias Ringwald 
456b06f2579SMatthias Ringwald     l2cap_reserve_packet_buffer();
457b06f2579SMatthias Ringwald     uint8_t * att_response_buffer = l2cap_get_outgoing_buffer();
45818707219SMatthias Ringwald     uint16_t  att_response_size   = att_handle_request(&att_server->connection, att_server->request_buffer, att_server->request_size, att_response_buffer);
459b06f2579SMatthias Ringwald 
460beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
46156c3a347SMatthias Ringwald     if (att_response_size == ATT_READ_RESPONSE_PENDING || att_response_size == ATT_INTERNAL_WRITE_RESPONSE_PENDING){
462e404a688SMatthias Ringwald         // update state
463beb20288SMatthias Ringwald         att_server->state = ATT_SERVER_RESPONSE_PENDING;
464e404a688SMatthias Ringwald 
46556c3a347SMatthias Ringwald         // callback with handle ATT_READ_RESPONSE_PENDING for reads
46656c3a347SMatthias Ringwald         if (att_response_size == ATT_READ_RESPONSE_PENDING){
467e404a688SMatthias Ringwald             att_server_client_read_callback(att_server->connection.con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0);
46856c3a347SMatthias Ringwald         }
469e8e7403eSMatthias Ringwald 
470e8e7403eSMatthias Ringwald         // free reserved buffer
471e8e7403eSMatthias Ringwald         l2cap_release_packet_buffer();
472e8e7403eSMatthias Ringwald         return 0;
473e404a688SMatthias Ringwald     }
474e404a688SMatthias Ringwald #endif
475e404a688SMatthias Ringwald 
476b06f2579SMatthias Ringwald     // intercept "insufficient authorization" for authenticated connections to allow for user authorization
477b06f2579SMatthias Ringwald     if ((att_response_size     >= 4)
478b06f2579SMatthias Ringwald     && (att_response_buffer[0] == ATT_ERROR_RESPONSE)
479b06f2579SMatthias Ringwald     && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION)
48018707219SMatthias Ringwald     && (att_server->connection.authenticated)){
481b06f2579SMatthias Ringwald 
4829c6e867eSMatthias Ringwald         switch (gap_authorization_state(att_server->connection.con_handle)){
483b06f2579SMatthias Ringwald             case AUTHORIZATION_UNKNOWN:
484b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
48518707219SMatthias Ringwald                 sm_request_pairing(att_server->connection.con_handle);
486b06f2579SMatthias Ringwald                 return 0;
487b06f2579SMatthias Ringwald             case AUTHORIZATION_PENDING:
488b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
489b06f2579SMatthias Ringwald                 return 0;
490b06f2579SMatthias Ringwald             default:
491b06f2579SMatthias Ringwald                 break;
492b06f2579SMatthias Ringwald         }
493b06f2579SMatthias Ringwald     }
494b06f2579SMatthias Ringwald 
49518707219SMatthias Ringwald     att_server->state = ATT_SERVER_IDLE;
496b06f2579SMatthias Ringwald     if (att_response_size == 0) {
497b06f2579SMatthias Ringwald         l2cap_release_packet_buffer();
498b06f2579SMatthias Ringwald         return 0;
499b06f2579SMatthias Ringwald     }
500b06f2579SMatthias Ringwald 
50118707219SMatthias Ringwald     l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size);
502b06f2579SMatthias Ringwald 
503b06f2579SMatthias Ringwald     // notify client about MTU exchange result
504b06f2579SMatthias Ringwald     if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){
50518707219SMatthias Ringwald         att_emit_mtu_event(att_server->connection.con_handle, att_server->connection.mtu);
506b06f2579SMatthias Ringwald     }
507b06f2579SMatthias Ringwald     return 1;
508b06f2579SMatthias Ringwald }
509b06f2579SMatthias Ringwald 
510beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
511beb20288SMatthias Ringwald int att_server_response_ready(hci_con_handle_t con_handle){
512e404a688SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
513e404a688SMatthias Ringwald     if (!att_server)                                        return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
514beb20288SMatthias Ringwald     if (att_server->state != ATT_SERVER_RESPONSE_PENDING)   return ERROR_CODE_COMMAND_DISALLOWED;
515e404a688SMatthias Ringwald 
516e404a688SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
517e404a688SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
518e404a688SMatthias Ringwald     return ERROR_CODE_SUCCESS;
519e404a688SMatthias Ringwald }
520e404a688SMatthias Ringwald #endif
521e404a688SMatthias Ringwald 
52218707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server){
52318707219SMatthias Ringwald     switch (att_server->state){
5243deb3ec6SMatthias Ringwald         case ATT_SERVER_REQUEST_RECEIVED:
525760ad805SMatthias Ringwald 
5260234fbbcSMatthias Ringwald             // wait until re-encryption as central is complete
5270234fbbcSMatthias Ringwald             if (gap_reconnect_security_setup_active(att_server->connection.con_handle)) break;
5280234fbbcSMatthias Ringwald 
529760ad805SMatthias Ringwald             // wait until pairing is complete
530760ad805SMatthias Ringwald             if (att_server->pairing_active) break;
531760ad805SMatthias Ringwald 
5327a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
53318707219SMatthias Ringwald             if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){
5343deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write!");
5353deb3ec6SMatthias Ringwald                 if (!sm_cmac_ready()) {
5363deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, sm_cmac engine not ready. Abort");
53718707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
5383deb3ec6SMatthias Ringwald                     return;
5393deb3ec6SMatthias Ringwald                 }
54018707219SMatthias Ringwald                 if (att_server->request_size < (3 + 12)) {
5413deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, request to short. Abort.");
54218707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
5433deb3ec6SMatthias Ringwald                     return;
5443deb3ec6SMatthias Ringwald                 }
54518707219SMatthias Ringwald                 if (att_server->ir_lookup_active){
5463deb3ec6SMatthias Ringwald                     return;
5473deb3ec6SMatthias Ringwald                 }
54818707219SMatthias Ringwald                 if (att_server->ir_le_device_db_index < 0){
5493deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, CSRK not available");
55018707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
5513deb3ec6SMatthias Ringwald                     return;
5523deb3ec6SMatthias Ringwald                 }
5533deb3ec6SMatthias Ringwald 
5543deb3ec6SMatthias Ringwald                 // check counter
55518707219SMatthias Ringwald                 uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
55618707219SMatthias Ringwald                 uint32_t counter_db     = le_device_db_remote_counter_get(att_server->ir_le_device_db_index);
5573deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet);
5583deb3ec6SMatthias Ringwald                 if (counter_packet < counter_db){
5593deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, db reports higher counter, abort");
56018707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
5613deb3ec6SMatthias Ringwald                     return;
5623deb3ec6SMatthias Ringwald                 }
5633deb3ec6SMatthias Ringwald 
5643deb3ec6SMatthias Ringwald                 // signature is { sequence counter, secure hash }
5653deb3ec6SMatthias Ringwald                 sm_key_t csrk;
56618707219SMatthias Ringwald                 le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk);
56718707219SMatthias Ringwald                 att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION;
5683deb3ec6SMatthias Ringwald                 log_info("Orig Signature: ");
56918707219SMatthias Ringwald                 log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8);
57018707219SMatthias Ringwald                 uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1);
57118707219SMatthias 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);
5723deb3ec6SMatthias Ringwald                 return;
5733deb3ec6SMatthias Ringwald             }
5747a766ebfSMatthias Ringwald #endif
575b06f2579SMatthias Ringwald             // move on
57618707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
57718707219SMatthias Ringwald             att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
578b06f2579SMatthias Ringwald             break;
57988a48dd8SMatthias Ringwald 
5803deb3ec6SMatthias Ringwald         default:
5813deb3ec6SMatthias Ringwald             break;
5823deb3ec6SMatthias Ringwald     }
5833deb3ec6SMatthias Ringwald }
5843deb3ec6SMatthias Ringwald 
58590f03c80SMatthias Ringwald static int att_server_data_ready_for_phase(att_server_t * att_server,  att_server_run_phase_t phase){
58690f03c80SMatthias Ringwald     switch (phase){
58790f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
58890f03c80SMatthias Ringwald             return att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
58990f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
59090f03c80SMatthias Ringwald              return (!btstack_linked_list_empty(&att_server->indication_requests) && att_server->value_indication_handle == 0);
59190f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
59290f03c80SMatthias Ringwald             return (!btstack_linked_list_empty(&att_server->notification_requests));
59390f03c80SMatthias Ringwald     }
594fa5e3464SMatthias Ringwald     // avoid warning
595fa5e3464SMatthias Ringwald     return 0;
59690f03c80SMatthias Ringwald }
59790f03c80SMatthias Ringwald 
59890f03c80SMatthias Ringwald static void att_server_trigger_send_for_phase(att_server_t * att_server,  att_server_run_phase_t phase){
59990f03c80SMatthias Ringwald     btstack_context_callback_registration_t * client;
60090f03c80SMatthias Ringwald     switch (phase){
60190f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
60290f03c80SMatthias Ringwald             att_server_process_validated_request(att_server);
60390f03c80SMatthias Ringwald             break;
60490f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
60590f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->indication_requests;
60690f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->indication_requests, (btstack_linked_item_t *) client);
60790f03c80SMatthias Ringwald             client->callback(client->context);
60890f03c80SMatthias Ringwald             break;
60990f03c80SMatthias Ringwald        case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
61090f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->notification_requests;
61190f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->notification_requests, (btstack_linked_item_t *) client);
61290f03c80SMatthias Ringwald             client->callback(client->context);
61390f03c80SMatthias Ringwald             break;
61490f03c80SMatthias Ringwald     }
61590f03c80SMatthias Ringwald }
61690f03c80SMatthias Ringwald 
6177eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){
618b06f2579SMatthias Ringwald 
619374a288aSMatthias Ringwald     hci_con_handle_t request_con_handle   = HCI_CON_HANDLE_INVALID;
6206438547aSMatthias Ringwald     hci_con_handle_t last_send_con_handle = HCI_CON_HANDLE_INVALID;
6216438547aSMatthias Ringwald     int can_send_now = 1;
6224395a000SMatthias Ringwald     int phase_index;
6236438547aSMatthias Ringwald 
6244395a000SMatthias Ringwald     for (phase_index = ATT_SERVER_RUN_PHASE_1_REQUESTS; phase_index <= ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS; phase_index++){
6254395a000SMatthias Ringwald         att_server_run_phase_t phase = (att_server_run_phase_t) phase_index;
6266438547aSMatthias Ringwald         hci_con_handle_t skip_connections_until = att_server_last_can_send_now;
627374a288aSMatthias Ringwald         while (1){
6286438547aSMatthias Ringwald             btstack_linked_list_iterator_t it;
62918707219SMatthias Ringwald             hci_connections_get_iterator(&it);
63018707219SMatthias Ringwald             while(btstack_linked_list_iterator_has_next(&it)){
63118707219SMatthias Ringwald                 hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
63218707219SMatthias Ringwald                 att_server_t * att_server = &connection->att_server;
6336438547aSMatthias Ringwald 
63490f03c80SMatthias Ringwald                 int data_ready = att_server_data_ready_for_phase(att_server, phase);
6356438547aSMatthias Ringwald 
6366438547aSMatthias 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);
6376438547aSMatthias Ringwald 
6386438547aSMatthias Ringwald                 // skip until last sender found (which is also skipped)
6396438547aSMatthias Ringwald                 if (skip_connections_until != HCI_CON_HANDLE_INVALID){
6406438547aSMatthias Ringwald                     if (data_ready && request_con_handle == HCI_CON_HANDLE_INVALID){
6416438547aSMatthias Ringwald                         request_con_handle = att_server->connection.con_handle;
6426438547aSMatthias Ringwald                     }
6436438547aSMatthias Ringwald                     if (skip_connections_until == att_server->connection.con_handle){
6446438547aSMatthias Ringwald                         skip_connections_until = HCI_CON_HANDLE_INVALID;
6456438547aSMatthias Ringwald                     }
6466438547aSMatthias Ringwald                     continue;
6476438547aSMatthias Ringwald                 };
6486438547aSMatthias Ringwald 
64990f03c80SMatthias Ringwald                 if (data_ready){
650969a5bbaSMatthias Ringwald                     if (can_send_now){
65190f03c80SMatthias Ringwald                         att_server_trigger_send_for_phase(att_server, phase);
6526438547aSMatthias Ringwald                         last_send_con_handle = att_server->connection.con_handle;
653969a5bbaSMatthias Ringwald                         can_send_now = att_dispatch_server_can_send_now(att_server->connection.con_handle);
65490f03c80SMatthias Ringwald                         data_ready = att_server_data_ready_for_phase(att_server, phase);
6556438547aSMatthias Ringwald                         if (data_ready && request_con_handle == HCI_CON_HANDLE_INVALID){
656cbbb12d9SMatthias Ringwald                             request_con_handle = att_server->connection.con_handle;
657cbbb12d9SMatthias Ringwald                         }
658cbbb12d9SMatthias Ringwald                     } else {
659f29a88d8SMatthias Ringwald                         request_con_handle = att_server->connection.con_handle;
660f29a88d8SMatthias Ringwald                         break;
661cbbb12d9SMatthias Ringwald                     }
662cbbb12d9SMatthias Ringwald                 }
6633deb3ec6SMatthias Ringwald             }
6643deb3ec6SMatthias Ringwald 
6656438547aSMatthias Ringwald             // stop skipping (handles disconnect by last send connection)
6666438547aSMatthias Ringwald             skip_connections_until = HCI_CON_HANDLE_INVALID;
6676438547aSMatthias Ringwald 
6683551936eSMatthias Ringwald             // Exit loop, if we cannot send
669969a5bbaSMatthias Ringwald             if (!can_send_now) break;
670969a5bbaSMatthias Ringwald 
671969a5bbaSMatthias Ringwald             // Exit loop, if we can send but there are also no further request
672969a5bbaSMatthias Ringwald             if (request_con_handle == HCI_CON_HANDLE_INVALID) break;
673969a5bbaSMatthias Ringwald 
674969a5bbaSMatthias Ringwald             // Finally, if we still can send and there are requests, just try again
675969a5bbaSMatthias Ringwald             request_con_handle = HCI_CON_HANDLE_INVALID;
676969a5bbaSMatthias Ringwald         }
6776438547aSMatthias Ringwald         // update last send con handle for round robin
6786438547aSMatthias Ringwald         if (last_send_con_handle != HCI_CON_HANDLE_INVALID){
6796438547aSMatthias Ringwald             att_server_last_can_send_now = last_send_con_handle;
6806438547aSMatthias Ringwald         }
6813551936eSMatthias Ringwald     }
682969a5bbaSMatthias Ringwald 
683969a5bbaSMatthias Ringwald     if (request_con_handle == HCI_CON_HANDLE_INVALID) return;
684969a5bbaSMatthias Ringwald     att_dispatch_server_request_can_send_now_event(request_con_handle);
685969a5bbaSMatthias Ringwald }
686969a5bbaSMatthias Ringwald 
68788a48dd8SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
68818707219SMatthias Ringwald     att_server_t * att_server;
68918707219SMatthias Ringwald 
69088a48dd8SMatthias Ringwald     switch (packet_type){
69188a48dd8SMatthias Ringwald         case HCI_EVENT_PACKET:
692bd87a16eSMatthias Ringwald             switch (packet[0]){
693bd87a16eSMatthias Ringwald                 case L2CAP_EVENT_CAN_SEND_NOW:
6944d2be802SMatthias Ringwald                     att_server_handle_can_send_now();
695372d62c4SMatthias Ringwald                     break;
696bd87a16eSMatthias Ringwald                 case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
697bd87a16eSMatthias Ringwald                     // GATT client has negotiated the mtu for this connection
6986155bae0SMatthias Ringwald                     att_server = att_server_for_handle(handle);
6996155bae0SMatthias Ringwald                     if (!att_server) break;
700bd87a16eSMatthias Ringwald                     att_server->connection.mtu = little_endian_read_16(packet, 4);
701bd87a16eSMatthias Ringwald                     break;
702bd87a16eSMatthias Ringwald                 default:
703bd87a16eSMatthias Ringwald                     break;
704bd87a16eSMatthias Ringwald             }
705bd87a16eSMatthias Ringwald             break;
7063deb3ec6SMatthias Ringwald 
707372d62c4SMatthias Ringwald         case ATT_DATA_PACKET:
708f50ef7ddSMatthias Ringwald             log_debug("ATT Packet, handle 0x%04x", handle);
70918707219SMatthias Ringwald             att_server = att_server_for_handle(handle);
71018707219SMatthias Ringwald             if (!att_server) break;
71118707219SMatthias Ringwald 
7123deb3ec6SMatthias Ringwald             // handle value indication confirms
71318707219SMatthias Ringwald             if (packet[0] == ATT_HANDLE_VALUE_CONFIRMATION && att_server->value_indication_handle){
71418707219SMatthias Ringwald                 btstack_run_loop_remove_timer(&att_server->value_indication_timer);
71518707219SMatthias Ringwald                 uint16_t att_handle = att_server->value_indication_handle;
71618707219SMatthias Ringwald                 att_server->value_indication_handle = 0;
71718707219SMatthias Ringwald                 att_handle_value_indication_notify_client(0, att_server->connection.con_handle, att_handle);
718cbbb12d9SMatthias Ringwald                 att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
7193deb3ec6SMatthias Ringwald                 return;
7203deb3ec6SMatthias Ringwald             }
7213deb3ec6SMatthias Ringwald 
7226428eb5aSMatthias Ringwald             // directly process command
7236428eb5aSMatthias Ringwald             // note: signed write cannot be handled directly as authentication needs to be verified
7246428eb5aSMatthias Ringwald             if (packet[0] == ATT_WRITE_COMMAND){
72518707219SMatthias Ringwald                 att_handle_request(&att_server->connection, packet, size, 0);
7266428eb5aSMatthias Ringwald                 return;
7276428eb5aSMatthias Ringwald             }
7286428eb5aSMatthias Ringwald 
7293deb3ec6SMatthias Ringwald             // check size
73018707219SMatthias Ringwald             if (size > sizeof(att_server->request_buffer)) {
731e0463bdcSMatthias 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));
7326428eb5aSMatthias Ringwald                 return;
7336428eb5aSMatthias Ringwald             }
7343deb3ec6SMatthias Ringwald 
735e0463bdcSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
736e0463bdcSMatthias Ringwald             // abort signed write validation if a new request comes in (but finish previous signed write if possible)
737e0463bdcSMatthias Ringwald             if (att_server->state == ATT_SERVER_W4_SIGNED_WRITE_VALIDATION){
738e0463bdcSMatthias Ringwald                 if (packet[0] == ATT_SIGNED_WRITE_COMMAND){
739e0463bdcSMatthias Ringwald                     log_info("skip new signed write request as previous is in validation");
740e0463bdcSMatthias Ringwald                     return;
741e0463bdcSMatthias Ringwald                 } else {
742e0463bdcSMatthias Ringwald                     log_info("abort signed write validation to process new request");
743e0463bdcSMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
744e0463bdcSMatthias Ringwald                 }
745e0463bdcSMatthias Ringwald             }
746e0463bdcSMatthias Ringwald #endif
7473deb3ec6SMatthias Ringwald             // last request still in processing?
74818707219SMatthias Ringwald             if (att_server->state != ATT_SERVER_IDLE){
749e0463bdcSMatthias Ringwald                 log_info("skip att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state);
7506428eb5aSMatthias Ringwald                 return;
7516428eb5aSMatthias Ringwald             }
7523deb3ec6SMatthias Ringwald 
7533deb3ec6SMatthias Ringwald             // store request
75418707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED;
75518707219SMatthias Ringwald             att_server->request_size = size;
75618707219SMatthias Ringwald             memcpy(att_server->request_buffer, packet, size);
7573deb3ec6SMatthias Ringwald 
75818707219SMatthias Ringwald             att_run_for_context(att_server);
759372d62c4SMatthias Ringwald             break;
760372d62c4SMatthias Ringwald     }
7613deb3ec6SMatthias Ringwald }
7623deb3ec6SMatthias Ringwald 
763bf78aac6SMatthias Ringwald // ---------------------
764bf78aac6SMatthias Ringwald // persistent CCC writes
765bf78aac6SMatthias Ringwald static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){
766bf78aac6SMatthias Ringwald     return 'B' << 24 | 'T' << 16 | 'C' << 8 | index;
767bf78aac6SMatthias Ringwald }
768bf78aac6SMatthias Ringwald 
769bf78aac6SMatthias Ringwald static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){
770bf78aac6SMatthias Ringwald     // lookup att_server instance
771bf78aac6SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
772bf78aac6SMatthias Ringwald     if (!att_server) return;
773bf78aac6SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
774bf78aac6SMatthias 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);
7756a540790SMatthias Ringwald 
776bf78aac6SMatthias Ringwald     // check if bonded
777bf78aac6SMatthias Ringwald     if (le_device_index < 0) return;
7786a540790SMatthias Ringwald 
779bf78aac6SMatthias Ringwald     // get btstack_tlv
780bf78aac6SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
781bf78aac6SMatthias Ringwald     void * tlv_context;
782bf78aac6SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
783bf78aac6SMatthias Ringwald     if (!tlv_impl) return;
7846a540790SMatthias Ringwald 
785bf78aac6SMatthias Ringwald     // update ccc tag
786bf78aac6SMatthias Ringwald     int index;
7876a540790SMatthias Ringwald     uint32_t highest_seq_nr = 0;
7886a540790SMatthias Ringwald     uint32_t lowest_seq_nr = 0;
7896a540790SMatthias Ringwald     uint32_t tag_for_lowest_seq_nr = 0;
7906a540790SMatthias Ringwald     uint32_t tag_for_empty = 0;
7916a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
7926afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
793bf78aac6SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
7946a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
7956a540790SMatthias Ringwald 
7966a540790SMatthias Ringwald         // empty/invalid tag
7976a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)){
7986a540790SMatthias Ringwald             tag_for_empty = tag;
799bf78aac6SMatthias Ringwald             continue;
800bf78aac6SMatthias Ringwald         }
8016a540790SMatthias Ringwald         // update highest seq nr
8026a540790SMatthias Ringwald         if (entry.seq_nr > highest_seq_nr){
8036a540790SMatthias Ringwald             highest_seq_nr = entry.seq_nr;
8046a540790SMatthias Ringwald         }
8056a540790SMatthias Ringwald         // find entry with lowest seq nr
8066a540790SMatthias Ringwald         if ((tag_for_lowest_seq_nr == 0) || (entry.seq_nr < lowest_seq_nr)){
8076a540790SMatthias Ringwald             tag_for_lowest_seq_nr = tag;
8086a540790SMatthias Ringwald             lowest_seq_nr = entry.seq_nr;
8096a540790SMatthias Ringwald         }
8106a540790SMatthias Ringwald 
8116a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
8126a540790SMatthias Ringwald         if (entry.att_handle   != att_handle)      continue;
8136a540790SMatthias Ringwald 
8146a540790SMatthias Ringwald         // found matching entry
81501ac2008SMatthias Ringwald         if (value){
816bf78aac6SMatthias Ringwald             // update
8176a540790SMatthias Ringwald             if (entry.value == value) {
818760ad805SMatthias Ringwald                 log_info("CCC Index %u: Up-to-date", index);
819760ad805SMatthias Ringwald                 return;
820760ad805SMatthias Ringwald             }
8216a540790SMatthias Ringwald             entry.value = value;
8226a540790SMatthias Ringwald             entry.seq_nr = highest_seq_nr + 1;
823760ad805SMatthias Ringwald             log_info("CCC Index %u: Store", index);
8246a540790SMatthias Ringwald             tlv_impl->store_tag(tlv_context, tag, (const uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
82501ac2008SMatthias Ringwald         } else {
82601ac2008SMatthias Ringwald             // delete
827760ad805SMatthias Ringwald             log_info("CCC Index %u: Delete", index);
82801ac2008SMatthias Ringwald             tlv_impl->delete_tag(tlv_context, tag);
82901ac2008SMatthias Ringwald         }
830bf78aac6SMatthias Ringwald         return;
831bf78aac6SMatthias Ringwald     }
8326a540790SMatthias Ringwald 
833faf2b6c3SMatthias Ringwald     log_info("tag_for_empy %"PRIx32", tag_for_lowest_seq_nr %"PRIx32, tag_for_empty, tag_for_lowest_seq_nr);
8346a540790SMatthias Ringwald 
8356a540790SMatthias Ringwald     if (value == 0){
8366a540790SMatthias Ringwald         // done
8376a540790SMatthias Ringwald         return;
8386a540790SMatthias Ringwald     }
8396a540790SMatthias Ringwald 
8406a540790SMatthias Ringwald     uint32_t tag_to_use = 0;
8416a540790SMatthias Ringwald     if (tag_for_empty){
8426a540790SMatthias Ringwald         tag_to_use = tag_for_empty;
8436a540790SMatthias Ringwald     } else if (tag_for_lowest_seq_nr){
8446a540790SMatthias Ringwald         tag_to_use = tag_for_lowest_seq_nr;
8456a540790SMatthias Ringwald     } else {
8466a540790SMatthias Ringwald         // should not happen
8476a540790SMatthias Ringwald         return;
8486a540790SMatthias Ringwald     }
849bf78aac6SMatthias Ringwald     // store ccc tag
8506a540790SMatthias Ringwald     entry.seq_nr       = highest_seq_nr + 1;
8516a540790SMatthias Ringwald     entry.device_index = le_device_index;
8526a540790SMatthias Ringwald     entry.att_handle   = att_handle;
8536a540790SMatthias Ringwald     entry.value        = value;
8546a540790SMatthias Ringwald     tlv_impl->store_tag(tlv_context, tag_to_use, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
855bf78aac6SMatthias Ringwald }
856bf78aac6SMatthias Ringwald 
857760ad805SMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server){
858760ad805SMatthias Ringwald     if (!att_server) return;
859760ad805SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
860760ad805SMatthias Ringwald     log_info("Clear CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
861760ad805SMatthias Ringwald     // check if bonded
862760ad805SMatthias Ringwald     if (le_device_index < 0) return;
863760ad805SMatthias Ringwald     // get btstack_tlv
864760ad805SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
865760ad805SMatthias Ringwald     void * tlv_context;
866760ad805SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
867760ad805SMatthias Ringwald     if (!tlv_impl) return;
868760ad805SMatthias Ringwald     // get all ccc tag
869760ad805SMatthias Ringwald     int index;
8706a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
8716afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
872760ad805SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
8736a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
8746a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
8756a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
876760ad805SMatthias Ringwald         // delete entry
877760ad805SMatthias Ringwald         log_info("CCC Index %u: Delete", index);
878760ad805SMatthias Ringwald         tlv_impl->delete_tag(tlv_context, tag);
879760ad805SMatthias Ringwald     }
880760ad805SMatthias Ringwald }
881760ad805SMatthias Ringwald 
88201ac2008SMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server){
88301ac2008SMatthias Ringwald     if (!att_server) return;
88401ac2008SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
88501ac2008SMatthias Ringwald     log_info("Restore CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
8861b7acd0dSMatthias Ringwald     // check if bonded
8871b7acd0dSMatthias Ringwald     if (le_device_index < 0) return;
88801ac2008SMatthias Ringwald     // get btstack_tlv
88901ac2008SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
89001ac2008SMatthias Ringwald     void * tlv_context;
89101ac2008SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
89201ac2008SMatthias Ringwald     if (!tlv_impl) return;
89301ac2008SMatthias Ringwald     // get all ccc tag
89401ac2008SMatthias Ringwald     int index;
8956a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
8966afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
89701ac2008SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
8986a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
8996a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
9006a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
90101ac2008SMatthias Ringwald         // simulate write callback
9026a540790SMatthias Ringwald         uint16_t attribute_handle = entry.att_handle;
90301ac2008SMatthias Ringwald         uint8_t  value[2];
9046a540790SMatthias Ringwald         little_endian_store_16(value, 0, entry.value);
90501ac2008SMatthias Ringwald         att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
90601ac2008SMatthias Ringwald         if (!callback) continue;
9076a540790SMatthias Ringwald         log_info("CCC Index %u: Set Attribute handle 0x%04x to value 0x%04x", index, attribute_handle, entry.value );
90801ac2008SMatthias Ringwald         (*callback)(att_server->connection.con_handle, attribute_handle, ATT_TRANSACTION_MODE_NONE, 0, value, sizeof(value));
90901ac2008SMatthias Ringwald     }
91001ac2008SMatthias Ringwald }
91101ac2008SMatthias Ringwald 
912bf78aac6SMatthias Ringwald // persistent CCC writes
913bf78aac6SMatthias Ringwald // ---------------------
9143d71c7a4SMatthias Ringwald 
9153d71c7a4SMatthias Ringwald // gatt service management
9163d71c7a4SMatthias Ringwald static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){
9173d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
9183d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
9193d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
9203d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
9213d71c7a4SMatthias Ringwald         if (handler->start_handle > handle) continue;
9223d71c7a4SMatthias Ringwald         if (handler->end_handle   < handle) continue;
9233d71c7a4SMatthias Ringwald         return handler;
9243d71c7a4SMatthias Ringwald     }
9253d71c7a4SMatthias Ringwald     return NULL;
9263d71c7a4SMatthias Ringwald }
9273d71c7a4SMatthias Ringwald static att_read_callback_t att_server_read_callback_for_handle(uint16_t handle){
9283d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
9293d71c7a4SMatthias Ringwald     if (handler) return handler->read_callback;
9303d71c7a4SMatthias Ringwald     return att_server_client_read_callback;
9313d71c7a4SMatthias Ringwald }
9323d71c7a4SMatthias Ringwald 
9333d71c7a4SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){
9343d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
9353d71c7a4SMatthias Ringwald     if (handler) return handler->write_callback;
9363d71c7a4SMatthias Ringwald     return att_server_client_write_callback;
9373d71c7a4SMatthias Ringwald }
9383d71c7a4SMatthias Ringwald 
9395d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle){
9405d07396bSMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
9415d07396bSMatthias Ringwald     if (handler) return handler->packet_handler;
9425d07396bSMatthias Ringwald     return att_client_packet_handler;
9435d07396bSMatthias Ringwald }
9445d07396bSMatthias Ringwald 
9453d71c7a4SMatthias Ringwald static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){
9463d71c7a4SMatthias Ringwald     // notify all callbacks
9473d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
9483d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
9493d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
9503d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
9513d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
9523d71c7a4SMatthias Ringwald         (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
9533d71c7a4SMatthias Ringwald     }
9543d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return;
9553d71c7a4SMatthias Ringwald     (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
9563d71c7a4SMatthias Ringwald }
9573d71c7a4SMatthias Ringwald 
9583d71c7a4SMatthias Ringwald // returns first reported error or 0
9593d71c7a4SMatthias Ringwald static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){
9603d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
9613d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
9623d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
9633d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
9643d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
9653d71c7a4SMatthias Ringwald         uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
9663d71c7a4SMatthias Ringwald         if (error_code) return error_code;
9673d71c7a4SMatthias Ringwald     }
9683d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return 0;
9693d71c7a4SMatthias Ringwald     return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
9703d71c7a4SMatthias Ringwald }
9713d71c7a4SMatthias Ringwald 
9723d71c7a4SMatthias 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){
9733d71c7a4SMatthias Ringwald     att_read_callback_t callback = att_server_read_callback_for_handle(attribute_handle);
97401ac2008SMatthias Ringwald     if (!callback) return 0;
9753d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, offset, buffer, buffer_size);
9763d71c7a4SMatthias Ringwald }
9773d71c7a4SMatthias Ringwald 
9783d71c7a4SMatthias 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){
9793d71c7a4SMatthias Ringwald     switch (transaction_mode){
9803d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_VALIDATE:
9813d71c7a4SMatthias Ringwald             return att_validate_prepared_write(con_handle);
9823d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_EXECUTE:
9833d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_CANCEL:
9843d71c7a4SMatthias Ringwald             att_notify_write_callbacks(con_handle, transaction_mode);
9853d71c7a4SMatthias Ringwald             return 0;
9863d71c7a4SMatthias Ringwald         default:
9873d71c7a4SMatthias Ringwald             break;
9883d71c7a4SMatthias Ringwald     }
9893d71c7a4SMatthias Ringwald 
990bf78aac6SMatthias Ringwald     // track CCC writes
991bf78aac6SMatthias Ringwald     if (att_is_persistent_ccc(attribute_handle) && offset == 0 && buffer_size == 2){
992bf78aac6SMatthias Ringwald         att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0));
993bf78aac6SMatthias Ringwald     }
994bf78aac6SMatthias Ringwald 
99501ac2008SMatthias Ringwald     att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
99601ac2008SMatthias Ringwald     if (!callback) return 0;
9973d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size);
9983d71c7a4SMatthias Ringwald }
9993d71c7a4SMatthias Ringwald 
10003d71c7a4SMatthias Ringwald /**
10013d71c7a4SMatthias Ringwald  * @brief register read/write callbacks for specific handle range
10023d71c7a4SMatthias Ringwald  * @param att_service_handler_t
10033d71c7a4SMatthias Ringwald  */
10043d71c7a4SMatthias Ringwald void att_server_register_service_handler(att_service_handler_t * handler){
10053d71c7a4SMatthias Ringwald     if (att_service_handler_for_handle(handler->start_handle) ||
10063d71c7a4SMatthias Ringwald         att_service_handler_for_handle(handler->end_handle)){
10073d71c7a4SMatthias Ringwald         log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle);
10083d71c7a4SMatthias Ringwald         return;
10093d71c7a4SMatthias Ringwald     }
10103d71c7a4SMatthias Ringwald     btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler);
10113d71c7a4SMatthias Ringwald }
10123d71c7a4SMatthias Ringwald 
10133deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){
10143deb3ec6SMatthias Ringwald 
10153d71c7a4SMatthias Ringwald     // store callbacks
10163d71c7a4SMatthias Ringwald     att_server_client_read_callback  = read_callback;
10173d71c7a4SMatthias Ringwald     att_server_client_write_callback = write_callback;
10183d71c7a4SMatthias Ringwald 
10191a389cdcSMatthias Ringwald     // register for HCI Events
1020d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &att_event_packet_handler;
10211a389cdcSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
10221a389cdcSMatthias Ringwald 
1023406722e4SMatthias Ringwald     // register for SM events
1024d9a7306aSMatthias Ringwald     sm_event_callback_registration.callback = &att_event_packet_handler;
1025406722e4SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
10263deb3ec6SMatthias Ringwald 
10271a389cdcSMatthias Ringwald     // and L2CAP ATT Server PDUs
10283deb3ec6SMatthias Ringwald     att_dispatch_register_server(att_packet_handler);
10293deb3ec6SMatthias Ringwald 
1030*70dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
1031*70dca4d4SMatthias Ringwald     // setup l2cap service
1032*70dca4d4SMatthias Ringwald     l2cap_register_service(&att_event_packet_handler, PSM_ATT, 0xffff, LEVEL_2);
1033*70dca4d4SMatthias Ringwald #endif
1034*70dca4d4SMatthias Ringwald 
10353deb3ec6SMatthias Ringwald     att_set_db(db);
10363d71c7a4SMatthias Ringwald     att_set_read_callback(att_server_read_callback);
10373d71c7a4SMatthias Ringwald     att_set_write_callback(att_server_write_callback);
10383deb3ec6SMatthias Ringwald }
10393deb3ec6SMatthias Ringwald 
10403deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){
10413deb3ec6SMatthias Ringwald     att_client_packet_handler = handler;
10423deb3ec6SMatthias Ringwald }
10433deb3ec6SMatthias Ringwald 
1044cbbb12d9SMatthias Ringwald 
1045cbbb12d9SMatthias Ringwald // to be deprecated
1046c141988cSMatthias Ringwald int  att_server_can_send_packet_now(hci_con_handle_t con_handle){
104718707219SMatthias Ringwald     return att_dispatch_server_can_send_now(con_handle);
10481b96b007SMatthias Ringwald }
10491b96b007SMatthias Ringwald 
1050cbbb12d9SMatthias Ringwald int att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
1051cbbb12d9SMatthias Ringwald     return att_server_request_to_send_notification(callback_registration, con_handle);
10523deb3ec6SMatthias Ringwald }
10533deb3ec6SMatthias Ringwald 
1054cbbb12d9SMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){
1055cbbb12d9SMatthias Ringwald     att_client_waiting_for_can_send_registration.callback = &att_emit_can_send_now_event;
1056cbbb12d9SMatthias Ringwald     att_server_request_to_send_notification(&att_client_waiting_for_can_send_registration, con_handle);
1057cbbb12d9SMatthias Ringwald }
1058cbbb12d9SMatthias Ringwald // end of deprecated
1059cbbb12d9SMatthias Ringwald 
1060cbbb12d9SMatthias Ringwald int att_server_request_to_send_notification(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
1061969a5bbaSMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
1062969a5bbaSMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1063cbbb12d9SMatthias Ringwald     btstack_linked_list_add_tail(&att_server->notification_requests, (btstack_linked_item_t*) callback_registration);
1064cbbb12d9SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
1065cbbb12d9SMatthias Ringwald     return ERROR_CODE_SUCCESS;
1066cbbb12d9SMatthias Ringwald }
1067cbbb12d9SMatthias Ringwald 
1068cbbb12d9SMatthias Ringwald int att_server_request_to_send_indication(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
1069cbbb12d9SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
1070cbbb12d9SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1071cbbb12d9SMatthias Ringwald     btstack_linked_list_add_tail(&att_server->indication_requests, (btstack_linked_item_t*) callback_registration);
1072bb38f057SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
1073969a5bbaSMatthias Ringwald     return ERROR_CODE_SUCCESS;
1074bb38f057SMatthias Ringwald }
1075bb38f057SMatthias Ringwald 
10763bb3035fSMilanka Ringwald int att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){
107718707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
107818707219SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
107918707219SMatthias Ringwald 
108018707219SMatthias Ringwald     if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
10813deb3ec6SMatthias Ringwald 
10823deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
10833deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
108418707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_notification(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
108518707219SMatthias Ringwald 	return l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
10863deb3ec6SMatthias Ringwald }
10873deb3ec6SMatthias Ringwald 
10883bb3035fSMilanka Ringwald int att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){
108918707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
109018707219SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
109118707219SMatthias Ringwald 
1092eaac31e8SMatthias Ringwald     if (att_server->value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS;
109318707219SMatthias Ringwald     if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
10943deb3ec6SMatthias Ringwald 
10953deb3ec6SMatthias Ringwald     // track indication
109618707219SMatthias Ringwald     att_server->value_indication_handle = attribute_handle;
109718707219SMatthias Ringwald     btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout);
109818707219SMatthias Ringwald     btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS);
109918707219SMatthias Ringwald     btstack_run_loop_add_timer(&att_server->value_indication_timer);
11003deb3ec6SMatthias Ringwald 
11013deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
11023deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
110318707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_indication(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
110418707219SMatthias Ringwald 	l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
11053deb3ec6SMatthias Ringwald     return 0;
11063deb3ec6SMatthias Ringwald }
11078f9132b5SMilanka Ringwald 
11088f9132b5SMilanka Ringwald uint16_t att_server_get_mtu(hci_con_handle_t con_handle){
11098f9132b5SMilanka Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
11108f9132b5SMilanka Ringwald     if (!att_server) return 0;
11118f9132b5SMilanka Ringwald     return att_server->connection.mtu;
11128f9132b5SMilanka Ringwald }
1113