xref: /btstack/src/ble/att_server.c (revision 52e211de8e1dd4e87992a8f5e5741513e3d5c3a1)
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
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH 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 
51296289e3SMatthias Ringwald #include "ble/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"
6820d4358dSMatthias Ringwald #include "bluetooth_psm.h"
6920d4358dSMatthias Ringwald 
70d1a1f6a4SMatthias Ringwald #endif
71bf78aac6SMatthias Ringwald 
7203f53d81SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT
7303f53d81SMatthias Ringwald #include <stdio.h>
7403f53d81SMatthias Ringwald #endif
7503f53d81SMatthias Ringwald 
766afb9acaSMatthias Ringwald #ifndef NVN_NUM_GATT_SERVER_CCC
776afb9acaSMatthias Ringwald #define NVN_NUM_GATT_SERVER_CCC 20
78bf78aac6SMatthias Ringwald #endif
793deb3ec6SMatthias Ringwald 
80cd45e970SMatthias Ringwald static void att_run_for_context(att_server_t * att_server, att_connection_t * att_connection);
8101ac2008SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle);
825d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle);
8348364364SMatthias Ringwald static void att_server_handle_can_send_now(void);
849a1f55eaSMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server, att_connection_t * att_connection);
85fe3d0cbaSMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server);
8626b42dcfSMatthias Ringwald static void att_server_handle_att_pdu(att_server_t * att_server, att_connection_t * att_connection, uint8_t * packet, uint16_t size);
873deb3ec6SMatthias Ringwald 
883551936eSMatthias Ringwald typedef enum {
8938b893aaSMilanka Ringwald     ATT_SERVER_RUN_PHASE_1_REQUESTS = 0,
903551936eSMatthias Ringwald     ATT_SERVER_RUN_PHASE_2_INDICATIONS,
913551936eSMatthias Ringwald     ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS,
923551936eSMatthias Ringwald } att_server_run_phase_t;
933551936eSMatthias Ringwald 
946a540790SMatthias Ringwald //
956a540790SMatthias Ringwald typedef struct {
966a540790SMatthias Ringwald     uint32_t seq_nr;
976a540790SMatthias Ringwald     uint16_t att_handle;
986a540790SMatthias Ringwald     uint8_t  value;
996a540790SMatthias Ringwald     uint8_t  device_index;
1006a540790SMatthias Ringwald } persistent_ccc_entry_t;
1016a540790SMatthias Ringwald 
10218707219SMatthias Ringwald // global
1031a389cdcSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
104406722e4SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration;
10535d7f2dfSMilanka Ringwald static btstack_packet_handler_t               att_client_packet_handler;
1063d71c7a4SMatthias Ringwald static btstack_linked_list_t                  service_handlers;
1075f141511SMatthias Ringwald static btstack_context_callback_registration_t att_client_waiting_for_can_send_registration;
10818707219SMatthias Ringwald 
1093d71c7a4SMatthias Ringwald static att_read_callback_t                    att_server_client_read_callback;
1103d71c7a4SMatthias Ringwald static att_write_callback_t                   att_server_client_write_callback;
1113d71c7a4SMatthias Ringwald 
1126438547aSMatthias Ringwald // round robin
1136438547aSMatthias Ringwald static hci_con_handle_t att_server_last_can_send_now = HCI_CON_HANDLE_INVALID;
114bf78aac6SMatthias Ringwald 
115c2cbe117SMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
116c2cbe117SMatthias Ringwald typedef struct {
117c2cbe117SMatthias Ringwald     btstack_linked_item_t item;
118c2cbe117SMatthias Ringwald     att_server_t     att_server;
119c2cbe117SMatthias Ringwald     att_connection_t att_connection;
120c2cbe117SMatthias Ringwald     uint8_t * receive_buffer;
121c2cbe117SMatthias Ringwald     uint8_t * send_buffer;
122c2cbe117SMatthias Ringwald } att_server_eatt_bearer_t;
12340f8d8f2SMatthias Ringwald static att_server_eatt_bearer_t * att_server_eatt_bearer_for_con_handle(hci_con_handle_t con_handle);
124c2cbe117SMatthias Ringwald static btstack_linked_list_t att_server_eatt_bearer_pool;
125c2cbe117SMatthias Ringwald static btstack_linked_list_t att_server_eatt_bearer_active;
126c2cbe117SMatthias Ringwald #endif
127c2cbe117SMatthias Ringwald 
12802b78fc8SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
12926567ee7SMatthias Ringwald static bool att_server_connections_for_state(att_server_state_t state, att_server_t ** att_server_out, att_connection_t ** att_connection_out){
13018707219SMatthias Ringwald     btstack_linked_list_iterator_t it;
13118707219SMatthias Ringwald     hci_connections_get_iterator(&it);
13218707219SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
13326567ee7SMatthias Ringwald         hci_connection_t * hci_connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
13426567ee7SMatthias Ringwald         if (hci_connection->att_server.state == state) {
13526567ee7SMatthias Ringwald             *att_server_out     = &hci_connection->att_server;
13626567ee7SMatthias Ringwald             *att_connection_out = &hci_connection->att_connection;
13726567ee7SMatthias Ringwald             return true;
13818707219SMatthias Ringwald         }
13926567ee7SMatthias Ringwald     }
140c2cbe117SMatthias Ringwald 
141c2cbe117SMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
142c2cbe117SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &att_server_eatt_bearer_active);
143c2cbe117SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
144c2cbe117SMatthias Ringwald         att_server_eatt_bearer_t * eatt_bearer = (att_server_eatt_bearer_t *) btstack_linked_list_iterator_next(&it);
145c2cbe117SMatthias Ringwald         if (eatt_bearer->att_server.state == state) {
146c2cbe117SMatthias Ringwald             *att_server_out     = &eatt_bearer->att_server;
147c2cbe117SMatthias Ringwald             *att_connection_out = &eatt_bearer->att_connection;
148c2cbe117SMatthias Ringwald             return true;
149c2cbe117SMatthias Ringwald         }
150c2cbe117SMatthias Ringwald     }
151c2cbe117SMatthias Ringwald #endif
152c2cbe117SMatthias Ringwald 
15326567ee7SMatthias Ringwald     return false;
15418707219SMatthias Ringwald }
15502b78fc8SMatthias Ringwald #endif
156bb38f057SMatthias Ringwald 
1573f9b77dbSMatthias Ringwald static void att_server_request_can_send_now(att_server_t * att_server, att_connection_t * att_connection ){
15823079b39SMatthias Ringwald     switch (att_server->bearer_type){
15923079b39SMatthias Ringwald         case ATT_BEARER_UNENHANCED_LE:
16023079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
16123079b39SMatthias Ringwald         case ATT_BEARER_UNENHANCED_CLASSIC:
1622954c397SMatthias Ringwald             /* fall through */
1632954c397SMatthias Ringwald #endif
164328bed13SMatthias Ringwald             att_dispatch_server_request_can_send_now_event(att_connection->con_handle);
165328bed13SMatthias Ringwald             break;
1662954c397SMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
1672954c397SMatthias Ringwald         case ATT_BEARER_ENHANCED_LE:
16823079b39SMatthias Ringwald             l2cap_request_can_send_now_event(att_server->l2cap_cid);
16923079b39SMatthias Ringwald             break;
17023079b39SMatthias Ringwald #endif
17123079b39SMatthias Ringwald         default:
17223079b39SMatthias Ringwald             btstack_unreachable();
17323079b39SMatthias Ringwald             break;
17423079b39SMatthias Ringwald     }
175c8c6e9b4SMatthias Ringwald }
176c8c6e9b4SMatthias Ringwald 
17737a8059dSMatthias Ringwald static bool att_server_can_send_packet(att_server_t * att_server, att_connection_t * att_connection){
17823079b39SMatthias Ringwald     switch (att_server->bearer_type) {
17923079b39SMatthias Ringwald         case ATT_BEARER_UNENHANCED_LE:
18023079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
18123079b39SMatthias Ringwald         case ATT_BEARER_UNENHANCED_CLASSIC:
1822954c397SMatthias Ringwald             /* fall through */
1832954c397SMatthias Ringwald #endif
184328bed13SMatthias Ringwald             return att_dispatch_server_can_send_now(att_connection->con_handle);
1852954c397SMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
1862954c397SMatthias Ringwald         case ATT_BEARER_ENHANCED_LE:
1872954c397SMatthias Ringwald             return l2cap_can_send_packet_now(att_server->l2cap_cid);
18823079b39SMatthias Ringwald #endif
18923079b39SMatthias Ringwald         default:
19023079b39SMatthias Ringwald             btstack_unreachable();
19123079b39SMatthias Ringwald             break;
19223079b39SMatthias Ringwald     }
19323079b39SMatthias Ringwald     return false;
194c0f26e1cSMatthias Ringwald }
195c0f26e1cSMatthias Ringwald 
1963deb3ec6SMatthias Ringwald static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){
1975d07396bSMatthias Ringwald     btstack_packet_handler_t packet_handler = att_server_packet_handler_for_handle(attribute_handle);
1985d07396bSMatthias Ringwald     if (!packet_handler) return;
1993deb3ec6SMatthias Ringwald 
2003deb3ec6SMatthias Ringwald     uint8_t event[7];
2013deb3ec6SMatthias Ringwald     int pos = 0;
2025611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE;
2034ea43905SMatthias Ringwald     event[pos++] = sizeof(event) - 2u;
2043deb3ec6SMatthias Ringwald     event[pos++] = status;
205f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, client_handle);
2063deb3ec6SMatthias Ringwald     pos += 2;
207f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, attribute_handle);
208a444112bSMatthias Ringwald     (*packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
2093deb3ec6SMatthias Ringwald }
2103deb3ec6SMatthias Ringwald 
2115d07396bSMatthias Ringwald static void att_emit_event_to_all(const uint8_t * event, uint16_t size){
2125d07396bSMatthias Ringwald     // dispatch to app level handler
2139305033eSMatthias Ringwald     if (att_client_packet_handler != NULL){
2145d07396bSMatthias Ringwald         (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size);
2155d07396bSMatthias Ringwald     }
2163deb3ec6SMatthias Ringwald 
2175d07396bSMatthias Ringwald     // dispatch to service handlers
2185d07396bSMatthias Ringwald     btstack_linked_list_iterator_t it;
2195d07396bSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
2205d07396bSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2215d07396bSMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
2225d07396bSMatthias Ringwald         if (!handler->packet_handler) continue;
2235d07396bSMatthias Ringwald         (*handler->packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size);
2245d07396bSMatthias Ringwald     }
2255d07396bSMatthias Ringwald }
2265d07396bSMatthias Ringwald 
2275d07396bSMatthias Ringwald static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){
2283deb3ec6SMatthias Ringwald     uint8_t event[6];
2293deb3ec6SMatthias Ringwald     int pos = 0;
2305611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE;
2314ea43905SMatthias Ringwald     event[pos++] = sizeof(event) - 2u;
232fc64f94aSMatthias Ringwald     little_endian_store_16(event, pos, con_handle);
2333deb3ec6SMatthias Ringwald     pos += 2;
234f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, mtu);
2355d07396bSMatthias Ringwald 
2365d07396bSMatthias Ringwald     // also dispatch to GATT Clients
237b12646c5SJakob Krantz     att_dispatch_server_mtu_exchanged(con_handle, mtu);
2385d07396bSMatthias Ringwald 
2395d07396bSMatthias Ringwald     // dispatch to app level handler and service handlers
2405d07396bSMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
2413deb3ec6SMatthias Ringwald }
2423deb3ec6SMatthias Ringwald 
2435f141511SMatthias Ringwald static void att_emit_can_send_now_event(void * context){
2445f141511SMatthias Ringwald     UNUSED(context);
2453c97b242SMatthias Ringwald     if (!att_client_packet_handler) return;
2463c97b242SMatthias Ringwald 
2471b96b007SMatthias Ringwald     uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0};
2481b96b007SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
2491b96b007SMatthias Ringwald }
2501b96b007SMatthias Ringwald 
2516d2214cbSMatthias Ringwald static void att_emit_connected_event(att_server_t * att_server,  att_connection_t * att_connection){
252ff1bec95SMatthias Ringwald     uint8_t event[11];
253ff1bec95SMatthias Ringwald     int pos = 0;
254ff1bec95SMatthias Ringwald     event[pos++] = ATT_EVENT_CONNECTED;
2554ea43905SMatthias Ringwald     event[pos++] = sizeof(event) - 2u;
256ff1bec95SMatthias Ringwald     event[pos++] = att_server->peer_addr_type;
257ff1bec95SMatthias Ringwald     reverse_bd_addr(att_server->peer_address, &event[pos]);
258ff1bec95SMatthias Ringwald     pos += 6;
259b6df12b6SMatthias Ringwald     little_endian_store_16(event, pos, att_connection->con_handle);
260ff1bec95SMatthias Ringwald     pos += 2;
261ff1bec95SMatthias Ringwald 
262ff1bec95SMatthias Ringwald     // dispatch to app level handler and service handlers
263ff1bec95SMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
264ff1bec95SMatthias Ringwald }
265ff1bec95SMatthias Ringwald 
266ff1bec95SMatthias Ringwald 
2676187ad4cSMatthias Ringwald static void att_emit_disconnected_event(uint16_t con_handle){
268ff1bec95SMatthias Ringwald     uint8_t event[4];
269ff1bec95SMatthias Ringwald     int pos = 0;
270ff1bec95SMatthias Ringwald     event[pos++] = ATT_EVENT_DISCONNECTED;
2714ea43905SMatthias Ringwald     event[pos++] = sizeof(event) - 2u;
2726187ad4cSMatthias Ringwald     little_endian_store_16(event, pos, con_handle);
273ff1bec95SMatthias Ringwald     pos += 2;
274ff1bec95SMatthias Ringwald 
275ff1bec95SMatthias Ringwald     // dispatch to app level handler and service handlers
276ff1bec95SMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
277ff1bec95SMatthias Ringwald }
278ff1bec95SMatthias Ringwald 
279ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){
28054178543SMatthias Ringwald     void * context = btstack_run_loop_get_timer_context(ts);
28154178543SMatthias Ringwald     hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
28268591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
28368591e36SMatthias Ringwald     if (!hci_connection) return;
28468591e36SMatthias Ringwald     // @note: after a transaction timeout, no more requests shall be sent over this ATT Bearer
285bce48a26SMatthias Ringwald     // (that's why we don't reset the value_indication_handle)
286b6df12b6SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
28718707219SMatthias Ringwald     uint16_t att_handle = att_server->value_indication_handle;
288388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
28938b893aaSMilanka Ringwald     att_handle_value_indication_notify_client((uint8_t)ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_connection->con_handle, att_handle);
2903deb3ec6SMatthias Ringwald }
2913deb3ec6SMatthias Ringwald 
292*52e211deSMatthias Ringwald static void att_server_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2939ec2630cSMatthias Ringwald 
2947dad9ff8SMatthias Ringwald     UNUSED(channel); // ok: there is no channel
2957dad9ff8SMatthias Ringwald     UNUSED(size);    // ok: handling own l2cap events
2963deb3ec6SMatthias Ringwald 
29718707219SMatthias Ringwald     att_server_t * att_server;
298b6df12b6SMatthias Ringwald     att_connection_t * att_connection;
29918707219SMatthias Ringwald     hci_con_handle_t con_handle;
30068591e36SMatthias Ringwald     hci_connection_t * hci_connection;
30170dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
30270dca4d4SMatthias Ringwald     bd_addr_t address;
30368591e36SMatthias Ringwald     btstack_linked_list_iterator_t it;
30470dca4d4SMatthias Ringwald #endif
30518707219SMatthias Ringwald 
3063deb3ec6SMatthias Ringwald     switch (packet_type) {
3073deb3ec6SMatthias Ringwald 
3083deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
3090e2df43fSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
3103deb3ec6SMatthias Ringwald                 case HCI_EVENT_LE_META:
3113deb3ec6SMatthias Ringwald                     switch (packet[2]) {
3123deb3ec6SMatthias Ringwald                         case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
31318707219SMatthias Ringwald                             con_handle = little_endian_read_16(packet, 4);
31468591e36SMatthias Ringwald                             hci_connection = hci_connection_for_handle(con_handle);
31568591e36SMatthias Ringwald                             if (!hci_connection) break;
31668591e36SMatthias Ringwald                             att_server = &hci_connection->att_server;
3173deb3ec6SMatthias Ringwald                         	// store connection info
31818707219SMatthias Ringwald                         	att_server->peer_addr_type = packet[7];
31918707219SMatthias Ringwald                             reverse_bd_addr(&packet[8], att_server->peer_address);
320388fa7c4SMatthias Ringwald                             att_connection = &hci_connection->att_connection;
321b6df12b6SMatthias Ringwald                             att_connection->con_handle = con_handle;
3223deb3ec6SMatthias Ringwald                             // reset connection properties
32318707219SMatthias Ringwald                             att_server->state = ATT_SERVER_IDLE;
32423079b39SMatthias Ringwald                             att_server->bearer_type = ATT_BEARER_UNENHANCED_LE;
325b6df12b6SMatthias Ringwald                             att_connection->mtu = ATT_DEFAULT_MTU;
326b6df12b6SMatthias Ringwald                             att_connection->max_mtu = l2cap_max_le_mtu();
327b6df12b6SMatthias Ringwald                             if (att_connection->max_mtu > ATT_REQUEST_BUFFER_SIZE){
328b6df12b6SMatthias Ringwald                                 att_connection->max_mtu = ATT_REQUEST_BUFFER_SIZE;
32999c58ad0SMatthias Ringwald                             }
33038b893aaSMilanka Ringwald                             att_connection->encryption_key_size = 0u;
33138b893aaSMilanka Ringwald                             att_connection->authenticated = 0u;
33238b893aaSMilanka Ringwald 		                	att_connection->authorized = 0u;
333b2c1e52cSMatthias Ringwald                             // workaround: identity resolving can already be complete, at least store result
334b2c1e52cSMatthias Ringwald                             att_server->ir_le_device_db_index = sm_le_device_index(con_handle);
33538b893aaSMilanka Ringwald                             att_server->ir_lookup_active = 0u;
33638b893aaSMilanka Ringwald                             att_server->pairing_active = 0u;
337ff1bec95SMatthias Ringwald                             // notify all - old
3385d07396bSMatthias Ringwald                             att_emit_event_to_all(packet, size);
339ff1bec95SMatthias Ringwald                             // notify all - new
3406d2214cbSMatthias Ringwald                             att_emit_connected_event(att_server, att_connection);
3413deb3ec6SMatthias Ringwald                             break;
3423deb3ec6SMatthias Ringwald 
3433deb3ec6SMatthias Ringwald                         default:
3443deb3ec6SMatthias Ringwald                             break;
3453deb3ec6SMatthias Ringwald                     }
3463deb3ec6SMatthias Ringwald                     break;
3473deb3ec6SMatthias Ringwald 
3483deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_CHANGE:
3498601e696SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_CHANGE_V2:
3503deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE:
3513deb3ec6SMatthias Ringwald                 	// check handle
35218707219SMatthias Ringwald                     con_handle = little_endian_read_16(packet, 3);
35368591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
35468591e36SMatthias Ringwald                     if (!hci_connection) break;
355914988a1SMatthias Ringwald                     if (gap_get_connection_type(con_handle) != GAP_CONNECTION_LE) break;
356914988a1SMatthias Ringwald                     // update security params
35768591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
358388fa7c4SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
359b6df12b6SMatthias Ringwald                     att_connection->encryption_key_size = gap_encryption_key_size(con_handle);
360ff3f1026SMatthias Ringwald                     att_connection->authenticated = gap_authenticated(con_handle) ? 1 : 0;
361f461b3dfSMatthias Ringwald                     att_connection->secure_connection = gap_secure_connection(con_handle) ? 1 : 0;
362914988a1SMatthias Ringwald                     log_info("encrypted key size %u, authenticated %u, secure connection %u",
363b6df12b6SMatthias Ringwald                         att_connection->encryption_key_size, att_connection->authenticated, att_connection->secure_connection);
364914988a1SMatthias Ringwald                     if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){
3654097998aSMatthias Ringwald                         // restore CCC values when encrypted for LE Connections
36622bfc225SMilanka Ringwald                         if (hci_event_encryption_change_get_encryption_enabled(packet) != 0){
3679a1f55eaSMatthias Ringwald                             att_server_persistent_ccc_restore(att_server, att_connection);
36801ac2008SMatthias Ringwald                         }
36901ac2008SMatthias Ringwald                     }
370cd45e970SMatthias Ringwald                     att_run_for_context(att_server, att_connection);
3713deb3ec6SMatthias Ringwald                     break;
3723deb3ec6SMatthias Ringwald 
3733deb3ec6SMatthias Ringwald                 case HCI_EVENT_DISCONNECTION_COMPLETE:
37470a390c7SMatthias Ringwald                     // check handle
37518707219SMatthias Ringwald                     con_handle = hci_event_disconnection_complete_get_connection_handle(packet);
37668591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
37768591e36SMatthias Ringwald                     if (!hci_connection) break;
37868591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
379388fa7c4SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
380b6df12b6SMatthias Ringwald                     att_clear_transaction_queue(att_connection);
381b6df12b6SMatthias Ringwald                     att_connection->con_handle = 0;
382760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
38318707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
38438b893aaSMilanka Ringwald                     if (att_server->value_indication_handle != 0u){
38527328ecbSMatthias Ringwald                         btstack_run_loop_remove_timer(&att_server->value_indication_timer);
386bce48a26SMatthias Ringwald                         uint16_t att_handle = att_server->value_indication_handle;
38738b893aaSMilanka Ringwald                         att_server->value_indication_handle = 0u; // reset error state
38838b893aaSMilanka Ringwald                         att_handle_value_indication_notify_client((uint8_t)ATT_HANDLE_VALUE_INDICATION_DISCONNECT, att_connection->con_handle, att_handle);
389bce48a26SMatthias Ringwald                     }
390ff1bec95SMatthias Ringwald                     // notify all - new
3916187ad4cSMatthias Ringwald                     att_emit_disconnected_event(con_handle);
392ff1bec95SMatthias Ringwald                     // notify all - old
3935d07396bSMatthias Ringwald                     att_emit_event_to_all(packet, size);
3943deb3ec6SMatthias Ringwald                     break;
3953deb3ec6SMatthias Ringwald 
396760ad805SMatthias Ringwald                 // Identity Resolving
3975611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_STARTED:
39818707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_started_get_handle(packet);
39968591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
40068591e36SMatthias Ringwald                     if (!hci_connection) break;
40168591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
4025611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED");
40330c8e114SMatthias Ringwald                     att_server->ir_lookup_active = 1;
4043deb3ec6SMatthias Ringwald                     break;
4055611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
406760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
40768591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
40868591e36SMatthias Ringwald                     if (!hci_connection) return;
409acb2a577SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
41068591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
41130c8e114SMatthias Ringwald                     att_server->ir_lookup_active = 0;
4121b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index(packet);
4131b7acd0dSMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED");
414cd45e970SMatthias Ringwald                     att_run_for_context(att_server, att_connection);
4153deb3ec6SMatthias Ringwald                     break;
4165611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_FAILED:
41718707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_failed_get_handle(packet);
41868591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
41968591e36SMatthias Ringwald                     if (!hci_connection) break;
420acb2a577SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
42168591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
4225611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED");
42330c8e114SMatthias Ringwald                     att_server->ir_lookup_active = 0;
42418707219SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
425cd45e970SMatthias Ringwald                     att_run_for_context(att_server, att_connection);
4263deb3ec6SMatthias Ringwald                     break;
427760ad805SMatthias Ringwald 
428760ad805SMatthias Ringwald                 // Pairing started - delete stored CCC values
429760ad805SMatthias Ringwald                 // - assumes pairing indicates either new device or re-pairing, in both cases there should be no stored CCC values
430760ad805SMatthias Ringwald                 // - assumes that all events have the con handle as the first field
431760ad805SMatthias Ringwald                 case SM_EVENT_JUST_WORKS_REQUEST:
432760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
433760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_INPUT_NUMBER:
434760ad805SMatthias Ringwald                 case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
435760ad805SMatthias Ringwald                     con_handle = sm_event_just_works_request_get_handle(packet);
43668591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
43768591e36SMatthias Ringwald                     if (!hci_connection) break;
43868591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
439760ad805SMatthias Ringwald                     log_info("SM Pairing started");
44001c0691bSMatthias Ringwald                     att_server->pairing_active = 1;
441760ad805SMatthias Ringwald                     if (att_server->ir_le_device_db_index < 0) break;
44201c0691bSMatthias Ringwald                     att_server_persistent_ccc_clear(att_server);
443760ad805SMatthias Ringwald                     // index not valid anymore
444760ad805SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
445760ad805SMatthias Ringwald                     break;
446760ad805SMatthias Ringwald 
4471b7acd0dSMatthias Ringwald                 // Bonding completed
448760ad805SMatthias Ringwald                 case SM_EVENT_IDENTITY_CREATED:
449760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
45068591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
45168591e36SMatthias Ringwald                     if (!hci_connection) return;
452acb2a577SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
45368591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
454760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
4551b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_created_get_index(packet);
456cd45e970SMatthias Ringwald                     att_run_for_context(att_server, att_connection);
4571b7acd0dSMatthias Ringwald                     break;
4581b7acd0dSMatthias Ringwald 
4591b7acd0dSMatthias Ringwald                 // Pairing complete (with/without bonding=storing of pairing information)
4601b7acd0dSMatthias Ringwald                 case SM_EVENT_PAIRING_COMPLETE:
4611b7acd0dSMatthias Ringwald                     con_handle = sm_event_pairing_complete_get_handle(packet);
46268591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
46368591e36SMatthias Ringwald                     if (!hci_connection) return;
464acb2a577SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
46568591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
4661b7acd0dSMatthias Ringwald                     att_server->pairing_active = 0;
467cd45e970SMatthias Ringwald                     att_run_for_context(att_server, att_connection);
468760ad805SMatthias Ringwald                     break;
469760ad805SMatthias Ringwald 
470760ad805SMatthias Ringwald                 // Authorization
4715611a760SMatthias Ringwald                 case SM_EVENT_AUTHORIZATION_RESULT: {
47218707219SMatthias Ringwald                     con_handle = sm_event_authorization_result_get_handle(packet);
47368591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
47468591e36SMatthias Ringwald                     if (!hci_connection) break;
475388fa7c4SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
476acb2a577SMatthias Ringwald                     att_server = &hci_connection->att_server;
477b6df12b6SMatthias Ringwald                     att_connection->authorized = sm_event_authorization_result_get_authorization_result(packet);
4783f9b77dbSMatthias Ringwald                     att_server_request_can_send_now(att_server, att_connection);
4793deb3ec6SMatthias Ringwald                 	break;
4803deb3ec6SMatthias Ringwald                 }
4813deb3ec6SMatthias Ringwald                 default:
4823deb3ec6SMatthias Ringwald                     break;
4833deb3ec6SMatthias Ringwald             }
48465b44ffdSMatthias Ringwald             break;
48565b44ffdSMatthias Ringwald         default:
48665b44ffdSMatthias Ringwald             break;
4873deb3ec6SMatthias Ringwald     }
4883deb3ec6SMatthias Ringwald }
4893deb3ec6SMatthias Ringwald 
4900f70c52eSMatthias Ringwald static uint8_t
4910f70c52eSMatthias Ringwald att_server_send_prepared(const att_server_t *att_server, const att_connection_t *att_connection, uint8_t *buffer,
4920f70c52eSMatthias Ringwald                          uint16_t size) {
493d415948cSMatthias Ringwald     uint8_t status = ERROR_CODE_SUCCESS;
494d415948cSMatthias Ringwald     switch (att_server->bearer_type) {
495d415948cSMatthias Ringwald         case ATT_BEARER_UNENHANCED_LE:
496d415948cSMatthias Ringwald             status = l2cap_send_prepared_connectionless(att_connection->con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
49740f8d8f2SMatthias Ringwald             break;
498d415948cSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
499d415948cSMatthias Ringwald         case ATT_BEARER_UNENHANCED_CLASSIC:
500d415948cSMatthias Ringwald             status = l2cap_send_prepared(att_server->l2cap_cid, size);
501d415948cSMatthias Ringwald             break;
502d415948cSMatthias Ringwald #endif
5030f70c52eSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
5040f70c52eSMatthias Ringwald         case ATT_BEARER_ENHANCED_LE:
5050f70c52eSMatthias Ringwald             btstack_assert(buffer != NULL);
5060f70c52eSMatthias Ringwald             status = l2cap_send(att_server->l2cap_cid, buffer, size);
5070f70c52eSMatthias Ringwald             break;
5080f70c52eSMatthias Ringwald #endif
509d415948cSMatthias Ringwald         default:
510d415948cSMatthias Ringwald             btstack_unreachable();
511d415948cSMatthias Ringwald             break;
512d415948cSMatthias Ringwald     }
513d415948cSMatthias Ringwald     return status;
514d415948cSMatthias Ringwald }
515d415948cSMatthias Ringwald 
5167a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
5173deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
51826567ee7SMatthias Ringwald     att_server_t * att_server = NULL;
51926567ee7SMatthias Ringwald     att_connection_t * att_connection = NULL;
52026567ee7SMatthias Ringwald     bool found = att_server_connections_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION, &att_server, &att_connection);
5213deb3ec6SMatthias Ringwald 
522c2cbe117SMatthias Ringwald     if (found == false){
523c2cbe117SMatthias Ringwald         return;
524c2cbe117SMatthias Ringwald     }
525c2cbe117SMatthias Ringwald 
5263deb3ec6SMatthias Ringwald     uint8_t hash_flipped[8];
5279c80e4ccSMatthias Ringwald     reverse_64(hash, hash_flipped);
52826567ee7SMatthias Ringwald     if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8) != 0){
5293deb3ec6SMatthias Ringwald         log_info("ATT Signed Write, invalid signature");
53003f53d81SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT
53103f53d81SMatthias Ringwald         printf("ATT Signed Write, invalid signature\n");
53203f53d81SMatthias Ringwald #endif
53318707219SMatthias Ringwald         att_server->state = ATT_SERVER_IDLE;
5343deb3ec6SMatthias Ringwald         return;
5353deb3ec6SMatthias Ringwald     }
5363deb3ec6SMatthias Ringwald     log_info("ATT Signed Write, valid signature");
53703f53d81SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT
53803f53d81SMatthias Ringwald     printf("ATT Signed Write, valid signature\n");
53903f53d81SMatthias Ringwald #endif
5403deb3ec6SMatthias Ringwald 
5413deb3ec6SMatthias Ringwald     // update sequence number
54218707219SMatthias Ringwald     uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
54318707219SMatthias Ringwald     le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1);
54418707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
5453f9b77dbSMatthias Ringwald     att_server_request_can_send_now(att_server, att_connection);
5463deb3ec6SMatthias Ringwald }
5477a766ebfSMatthias Ringwald #endif
54888a48dd8SMatthias Ringwald 
54918707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED
550b06f2579SMatthias Ringwald // pre: can send now
5519988bc8aSMatthias Ringwald // uses l2cap outgoing buffer if no eatt_buffer provided
552b06f2579SMatthias Ringwald // returns: 1 if packet was sent
5539988bc8aSMatthias Ringwald static int
5549988bc8aSMatthias Ringwald att_server_process_validated_request(att_server_t *att_server, att_connection_t *att_connection, uint8_t *eatt_buffer) {
555b06f2579SMatthias Ringwald 
5569988bc8aSMatthias Ringwald     uint8_t * att_response_buffer;
5579988bc8aSMatthias Ringwald     if (eatt_buffer != NULL){
5589988bc8aSMatthias Ringwald         att_response_buffer = eatt_buffer;
5599988bc8aSMatthias Ringwald     } else {
560b06f2579SMatthias Ringwald         l2cap_reserve_packet_buffer();
5619988bc8aSMatthias Ringwald         att_response_buffer = l2cap_get_outgoing_buffer();
5629988bc8aSMatthias Ringwald     }
5639988bc8aSMatthias Ringwald 
5647a293e22SMatthias Ringwald     uint16_t  att_response_size = 0;
5657a293e22SMatthias Ringwald     // Send Error Response for MTU Request over connection-oriented channel
5667a293e22SMatthias Ringwald     if ((att_server->bearer_type != ATT_BEARER_UNENHANCED_LE) && (att_server->request_buffer[0] == ATT_EXCHANGE_MTU_REQUEST)){
5677a293e22SMatthias Ringwald         att_response_size = 5;
5687a293e22SMatthias Ringwald         att_response_buffer[0] = ATT_ERROR_RESPONSE;
5697a293e22SMatthias Ringwald         att_response_buffer[1] = ATT_EXCHANGE_MTU_REQUEST;
5707a293e22SMatthias Ringwald         att_response_buffer[2] = 0;
5717a293e22SMatthias Ringwald         att_response_buffer[3] = 0;
5727a293e22SMatthias Ringwald         att_response_buffer[4] = ATT_ERROR_REQUEST_NOT_SUPPORTED;
5737a293e22SMatthias Ringwald     } else {
5747a293e22SMatthias Ringwald         att_response_size = att_handle_request(att_connection, att_server->request_buffer, att_server->request_size, att_response_buffer);
5757a293e22SMatthias Ringwald     }
576b06f2579SMatthias Ringwald 
577beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
578c1ab6cc1SMatthias Ringwald     if ((att_response_size == ATT_READ_RESPONSE_PENDING) || (att_response_size == ATT_INTERNAL_WRITE_RESPONSE_PENDING)){
579e404a688SMatthias Ringwald         // update state
580beb20288SMatthias Ringwald         att_server->state = ATT_SERVER_RESPONSE_PENDING;
581e404a688SMatthias Ringwald 
58256c3a347SMatthias Ringwald         // callback with handle ATT_READ_RESPONSE_PENDING for reads
58356c3a347SMatthias Ringwald         if (att_response_size == ATT_READ_RESPONSE_PENDING){
584b6df12b6SMatthias Ringwald             att_server_client_read_callback(att_connection->con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0);
58556c3a347SMatthias Ringwald         }
586e8e7403eSMatthias Ringwald 
587e8e7403eSMatthias Ringwald         // free reserved buffer
5889988bc8aSMatthias Ringwald         if (eatt_buffer == NULL){
589e8e7403eSMatthias Ringwald             l2cap_release_packet_buffer();
5909988bc8aSMatthias Ringwald         }
591e8e7403eSMatthias Ringwald         return 0;
592e404a688SMatthias Ringwald     }
593e404a688SMatthias Ringwald #endif
594e404a688SMatthias Ringwald 
595b06f2579SMatthias Ringwald     // intercept "insufficient authorization" for authenticated connections to allow for user authorization
5964ea43905SMatthias Ringwald     if ((att_response_size     >= 4u)
597b06f2579SMatthias Ringwald     && (att_response_buffer[0] == ATT_ERROR_RESPONSE)
598b06f2579SMatthias Ringwald     && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION)
59938b893aaSMilanka Ringwald     && (att_connection->authenticated != 0u)){
600b06f2579SMatthias Ringwald 
601b6df12b6SMatthias Ringwald         switch (gap_authorization_state(att_connection->con_handle)){
602b06f2579SMatthias Ringwald             case AUTHORIZATION_UNKNOWN:
6039988bc8aSMatthias Ringwald                 if (eatt_buffer == NULL){
604b06f2579SMatthias Ringwald                     l2cap_release_packet_buffer();
6059988bc8aSMatthias Ringwald                 }
606b6df12b6SMatthias Ringwald                 sm_request_pairing(att_connection->con_handle);
607b06f2579SMatthias Ringwald                 return 0;
608b06f2579SMatthias Ringwald             case AUTHORIZATION_PENDING:
6099988bc8aSMatthias Ringwald                 if (eatt_buffer == NULL){
610b06f2579SMatthias Ringwald                     l2cap_release_packet_buffer();
6119988bc8aSMatthias Ringwald                 }
612b06f2579SMatthias Ringwald                 return 0;
613b06f2579SMatthias Ringwald             default:
614b06f2579SMatthias Ringwald                 break;
615b06f2579SMatthias Ringwald         }
616b06f2579SMatthias Ringwald     }
617b06f2579SMatthias Ringwald 
61818707219SMatthias Ringwald     att_server->state = ATT_SERVER_IDLE;
6194ea43905SMatthias Ringwald     if (att_response_size == 0u) {
6209988bc8aSMatthias Ringwald         if (eatt_buffer == NULL){
621b06f2579SMatthias Ringwald             l2cap_release_packet_buffer();
6229988bc8aSMatthias Ringwald         }
623b06f2579SMatthias Ringwald         return 0;
624b06f2579SMatthias Ringwald     }
625b06f2579SMatthias Ringwald 
6269988bc8aSMatthias Ringwald     (void) att_server_send_prepared(att_server, att_connection, eatt_buffer, att_response_size);
627b06f2579SMatthias Ringwald 
628b06f2579SMatthias Ringwald     // notify client about MTU exchange result
629b06f2579SMatthias Ringwald     if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){
630b6df12b6SMatthias Ringwald         att_emit_mtu_event(att_connection->con_handle, att_connection->mtu);
631b06f2579SMatthias Ringwald     }
632b06f2579SMatthias Ringwald     return 1;
633b06f2579SMatthias Ringwald }
634b06f2579SMatthias Ringwald 
635beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
6369816429dSMatthias Ringwald uint8_t att_server_response_ready(hci_con_handle_t con_handle){
63768591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
63868591e36SMatthias Ringwald     if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
63968591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
6403f9b77dbSMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
641beb20288SMatthias Ringwald     if (att_server->state != ATT_SERVER_RESPONSE_PENDING)   return ERROR_CODE_COMMAND_DISALLOWED;
642e404a688SMatthias Ringwald 
643e404a688SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
6443f9b77dbSMatthias Ringwald     att_server_request_can_send_now(att_server, att_connection);
645e404a688SMatthias Ringwald     return ERROR_CODE_SUCCESS;
646e404a688SMatthias Ringwald }
647e404a688SMatthias Ringwald #endif
648e404a688SMatthias Ringwald 
649cd45e970SMatthias Ringwald static void att_run_for_context(att_server_t * att_server, att_connection_t * att_connection){
65018707219SMatthias Ringwald     switch (att_server->state){
6513deb3ec6SMatthias Ringwald         case ATT_SERVER_REQUEST_RECEIVED:
65223079b39SMatthias Ringwald             switch (att_server->bearer_type){
65323079b39SMatthias Ringwald                 case ATT_BEARER_UNENHANCED_LE:
6540234fbbcSMatthias Ringwald                     // wait until re-encryption as central is complete
65523079b39SMatthias Ringwald                     if (gap_reconnect_security_setup_active(att_connection->con_handle)) {
65623079b39SMatthias Ringwald                         return;
65723079b39SMatthias Ringwald                     };
65823079b39SMatthias Ringwald                     break;
6592954c397SMatthias Ringwald #if defined(ENABLE_GATT_OVER_CLASSIC) || defined (ENABLE_GATT_OVER_EATT)
66023079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
66123079b39SMatthias Ringwald                 case ATT_BEARER_UNENHANCED_CLASSIC:
6622954c397SMatthias Ringwald                     /* fall through */
6632954c397SMatthias Ringwald #endif
6642954c397SMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
6652954c397SMatthias Ringwald                 case ATT_BEARER_ENHANCED_LE:
6662954c397SMatthias Ringwald #endif
66723079b39SMatthias Ringwald                     // ok
66823079b39SMatthias Ringwald                     break;
66923079b39SMatthias Ringwald #endif
67023079b39SMatthias Ringwald                 default:
67123079b39SMatthias Ringwald                     btstack_unreachable();
67223079b39SMatthias Ringwald                     break;
67348364364SMatthias Ringwald             }
6740234fbbcSMatthias Ringwald 
675760ad805SMatthias Ringwald             // wait until pairing is complete
676760ad805SMatthias Ringwald             if (att_server->pairing_active) break;
677760ad805SMatthias Ringwald 
6787a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
67918707219SMatthias Ringwald             if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){
6803deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write!");
6813deb3ec6SMatthias Ringwald                 if (!sm_cmac_ready()) {
6823deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, sm_cmac engine not ready. Abort");
68318707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
6843deb3ec6SMatthias Ringwald                     return;
6853deb3ec6SMatthias Ringwald                 }
68618707219SMatthias Ringwald                 if (att_server->request_size < (3 + 12)) {
6873deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, request to short. Abort.");
68818707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
6893deb3ec6SMatthias Ringwald                     return;
6903deb3ec6SMatthias Ringwald                 }
69118707219SMatthias Ringwald                 if (att_server->ir_lookup_active){
6923deb3ec6SMatthias Ringwald                     return;
6933deb3ec6SMatthias Ringwald                 }
69418707219SMatthias Ringwald                 if (att_server->ir_le_device_db_index < 0){
6953deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, CSRK not available");
69618707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
6973deb3ec6SMatthias Ringwald                     return;
6983deb3ec6SMatthias Ringwald                 }
6993deb3ec6SMatthias Ringwald 
7003deb3ec6SMatthias Ringwald                 // check counter
70118707219SMatthias Ringwald                 uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
70218707219SMatthias Ringwald                 uint32_t counter_db     = le_device_db_remote_counter_get(att_server->ir_le_device_db_index);
7033deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet);
7043deb3ec6SMatthias Ringwald                 if (counter_packet < counter_db){
7053deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, db reports higher counter, abort");
70618707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
7073deb3ec6SMatthias Ringwald                     return;
7083deb3ec6SMatthias Ringwald                 }
7093deb3ec6SMatthias Ringwald 
7103deb3ec6SMatthias Ringwald                 // signature is { sequence counter, secure hash }
7113deb3ec6SMatthias Ringwald                 sm_key_t csrk;
71218707219SMatthias Ringwald                 le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk);
71318707219SMatthias Ringwald                 att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION;
7143deb3ec6SMatthias Ringwald                 log_info("Orig Signature: ");
71518707219SMatthias Ringwald                 log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8);
71618707219SMatthias Ringwald                 uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1);
71718707219SMatthias 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);
7183deb3ec6SMatthias Ringwald                 return;
7193deb3ec6SMatthias Ringwald             }
7207a766ebfSMatthias Ringwald #endif
721b06f2579SMatthias Ringwald             // move on
72218707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
7233f9b77dbSMatthias Ringwald             att_server_request_can_send_now(att_server, att_connection);
724b06f2579SMatthias Ringwald             break;
72588a48dd8SMatthias Ringwald 
7263deb3ec6SMatthias Ringwald         default:
7273deb3ec6SMatthias Ringwald             break;
7283deb3ec6SMatthias Ringwald     }
7293deb3ec6SMatthias Ringwald }
7303deb3ec6SMatthias Ringwald 
7311979f09cSMatthias Ringwald static bool att_server_data_ready_for_phase(att_server_t * att_server,  att_server_run_phase_t phase){
73290f03c80SMatthias Ringwald     switch (phase){
73390f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
73490f03c80SMatthias Ringwald             return att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
73590f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
73638b893aaSMilanka Ringwald              return (!btstack_linked_list_empty(&att_server->indication_requests) && (att_server->value_indication_handle == 0u));
73790f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
73890f03c80SMatthias Ringwald             return (!btstack_linked_list_empty(&att_server->notification_requests));
7397bbeb3adSMilanka Ringwald         default:
7407bbeb3adSMilanka Ringwald             btstack_assert(false);
7411979f09cSMatthias Ringwald             return false;
74290f03c80SMatthias Ringwald     }
7437bbeb3adSMilanka Ringwald }
74490f03c80SMatthias Ringwald 
7459d6c0193SMatthias Ringwald static void att_server_trigger_send_for_phase(att_server_t * att_server, att_connection_t * att_connection, att_server_run_phase_t phase){
74690f03c80SMatthias Ringwald     btstack_context_callback_registration_t * client;
74790f03c80SMatthias Ringwald     switch (phase){
74890f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
7490f70c52eSMatthias Ringwald             att_server_process_validated_request(att_server, att_connection, NULL);
75090f03c80SMatthias Ringwald             break;
75190f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
75290f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->indication_requests;
75390f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->indication_requests, (btstack_linked_item_t *) client);
75490f03c80SMatthias Ringwald             client->callback(client->context);
75590f03c80SMatthias Ringwald             break;
75690f03c80SMatthias Ringwald        case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
75790f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->notification_requests;
75890f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->notification_requests, (btstack_linked_item_t *) client);
75990f03c80SMatthias Ringwald             client->callback(client->context);
76090f03c80SMatthias Ringwald             break;
7617bbeb3adSMilanka Ringwald         default:
7627bbeb3adSMilanka Ringwald             btstack_assert(false);
7637bbeb3adSMilanka Ringwald             break;
76490f03c80SMatthias Ringwald     }
76590f03c80SMatthias Ringwald }
76690f03c80SMatthias Ringwald 
7677eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){
768b06f2579SMatthias Ringwald 
7696438547aSMatthias Ringwald     hci_con_handle_t last_send_con_handle = HCI_CON_HANDLE_INVALID;
77068591e36SMatthias Ringwald     hci_connection_t * request_hci_connection   = NULL;
7711979f09cSMatthias Ringwald     bool can_send_now = true;
77238b893aaSMilanka Ringwald     uint8_t phase_index;
7736438547aSMatthias Ringwald 
7744395a000SMatthias Ringwald     for (phase_index = ATT_SERVER_RUN_PHASE_1_REQUESTS; phase_index <= ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS; phase_index++){
7754395a000SMatthias Ringwald         att_server_run_phase_t phase = (att_server_run_phase_t) phase_index;
7766438547aSMatthias Ringwald         hci_con_handle_t skip_connections_until = att_server_last_can_send_now;
777ff3cc4a5SMatthias Ringwald         while (true){
7786438547aSMatthias Ringwald             btstack_linked_list_iterator_t it;
77918707219SMatthias Ringwald             hci_connections_get_iterator(&it);
78018707219SMatthias Ringwald             while(btstack_linked_list_iterator_has_next(&it)){
78118707219SMatthias Ringwald                 hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
78218707219SMatthias Ringwald                 att_server_t * att_server = &connection->att_server;
783388fa7c4SMatthias Ringwald                 att_connection_t * att_connection = &connection->att_connection;
7846438547aSMatthias Ringwald 
7851979f09cSMatthias Ringwald                 bool data_ready = att_server_data_ready_for_phase(att_server, phase);
7866438547aSMatthias Ringwald 
787b6df12b6SMatthias Ringwald                 // log_debug("phase %u, handle 0x%04x, skip until 0x%04x, data ready %u", phase, att_connection->con_handle, skip_connections_until, data_ready);
7886438547aSMatthias Ringwald 
7896438547aSMatthias Ringwald                 // skip until last sender found (which is also skipped)
7906438547aSMatthias Ringwald                 if (skip_connections_until != HCI_CON_HANDLE_INVALID){
79168591e36SMatthias Ringwald                     if (data_ready && (request_hci_connection == NULL)){
79268591e36SMatthias Ringwald                         request_hci_connection = connection;
7936438547aSMatthias Ringwald                     }
794b6df12b6SMatthias Ringwald                     if (skip_connections_until == att_connection->con_handle){
7956438547aSMatthias Ringwald                         skip_connections_until = HCI_CON_HANDLE_INVALID;
7966438547aSMatthias Ringwald                     }
7976438547aSMatthias Ringwald                     continue;
7986438547aSMatthias Ringwald                 };
7996438547aSMatthias Ringwald 
80090f03c80SMatthias Ringwald                 if (data_ready){
801969a5bbaSMatthias Ringwald                     if (can_send_now){
8029d6c0193SMatthias Ringwald                         att_server_trigger_send_for_phase(att_server, att_connection, phase);
803b6df12b6SMatthias Ringwald                         last_send_con_handle = att_connection->con_handle;
80437a8059dSMatthias Ringwald                         can_send_now = att_server_can_send_packet(att_server, att_connection);
80590f03c80SMatthias Ringwald                         data_ready = att_server_data_ready_for_phase(att_server, phase);
80668591e36SMatthias Ringwald                         if (data_ready && (request_hci_connection == NULL)){
80768591e36SMatthias Ringwald                             request_hci_connection = connection;
808cbbb12d9SMatthias Ringwald                         }
809cbbb12d9SMatthias Ringwald                     } else {
81068591e36SMatthias Ringwald                         request_hci_connection = connection;
811f29a88d8SMatthias Ringwald                         break;
812cbbb12d9SMatthias Ringwald                     }
813cbbb12d9SMatthias Ringwald                 }
8143deb3ec6SMatthias Ringwald             }
8153deb3ec6SMatthias Ringwald 
8166438547aSMatthias Ringwald             // stop skipping (handles disconnect by last send connection)
8176438547aSMatthias Ringwald             skip_connections_until = HCI_CON_HANDLE_INVALID;
8186438547aSMatthias Ringwald 
8193551936eSMatthias Ringwald             // Exit loop, if we cannot send
820969a5bbaSMatthias Ringwald             if (!can_send_now) break;
821969a5bbaSMatthias Ringwald 
822969a5bbaSMatthias Ringwald             // Exit loop, if we can send but there are also no further request
82368591e36SMatthias Ringwald             if (request_hci_connection == NULL) break;
824969a5bbaSMatthias Ringwald 
825969a5bbaSMatthias Ringwald             // Finally, if we still can send and there are requests, just try again
82668591e36SMatthias Ringwald             request_hci_connection = NULL;
827969a5bbaSMatthias Ringwald         }
8286438547aSMatthias Ringwald         // update last send con handle for round robin
8296438547aSMatthias Ringwald         if (last_send_con_handle != HCI_CON_HANDLE_INVALID){
8306438547aSMatthias Ringwald             att_server_last_can_send_now = last_send_con_handle;
8316438547aSMatthias Ringwald         }
8323551936eSMatthias Ringwald     }
833969a5bbaSMatthias Ringwald 
83468591e36SMatthias Ringwald     if (request_hci_connection == NULL) return;
8353f9b77dbSMatthias Ringwald 
8363f9b77dbSMatthias Ringwald     att_server_t * att_server = &request_hci_connection->att_server;
8373f9b77dbSMatthias Ringwald     att_connection_t * att_connection = &request_hci_connection->att_connection;
8383f9b77dbSMatthias Ringwald     att_server_request_can_send_now(att_server, att_connection);
839969a5bbaSMatthias Ringwald }
840969a5bbaSMatthias Ringwald 
84126b42dcfSMatthias Ringwald static void att_server_handle_att_pdu(att_server_t * att_server, att_connection_t * att_connection, uint8_t * packet, uint16_t size){
84218707219SMatthias Ringwald 
843838bd521SMatthias Ringwald     uint8_t opcode  = packet[0u];
84438b893aaSMilanka Ringwald     uint8_t method  = opcode & 0x03fu;
845ed68ae9dSMilanka Ringwald     bool invalid = method > ATT_MULTIPLE_HANDLE_VALUE_NTF;
84638b893aaSMilanka Ringwald     bool command = (opcode & 0x40u) != 0u;
847838bd521SMatthias Ringwald 
848838bd521SMatthias Ringwald     // ignore invalid commands
849838bd521SMatthias Ringwald     if (invalid && command){
850838bd521SMatthias Ringwald         return;
851838bd521SMatthias Ringwald     }
852838bd521SMatthias Ringwald 
8533deb3ec6SMatthias Ringwald     // handle value indication confirms
85438b893aaSMilanka Ringwald     if ((opcode == ATT_HANDLE_VALUE_CONFIRMATION) && (att_server->value_indication_handle != 0u)){
85518707219SMatthias Ringwald         btstack_run_loop_remove_timer(&att_server->value_indication_timer);
85618707219SMatthias Ringwald         uint16_t att_handle = att_server->value_indication_handle;
85738b893aaSMilanka Ringwald         att_server->value_indication_handle = 0u;
85838b893aaSMilanka Ringwald         att_handle_value_indication_notify_client(0u, att_connection->con_handle, att_handle);
8593f9b77dbSMatthias Ringwald         att_server_request_can_send_now(att_server, att_connection);
8603deb3ec6SMatthias Ringwald         return;
8613deb3ec6SMatthias Ringwald     }
8623deb3ec6SMatthias Ringwald 
8636428eb5aSMatthias Ringwald     // directly process command
8646428eb5aSMatthias Ringwald     // note: signed write cannot be handled directly as authentication needs to be verified
865838bd521SMatthias Ringwald     if (opcode == ATT_WRITE_COMMAND){
866b6df12b6SMatthias Ringwald         att_handle_request(att_connection, packet, size, NULL);
8676428eb5aSMatthias Ringwald         return;
8686428eb5aSMatthias Ringwald     }
8696428eb5aSMatthias Ringwald 
8703deb3ec6SMatthias Ringwald     // check size
87118707219SMatthias Ringwald     if (size > sizeof(att_server->request_buffer)) {
872e0463bdcSMatthias 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));
8736428eb5aSMatthias Ringwald         return;
8746428eb5aSMatthias Ringwald     }
8753deb3ec6SMatthias Ringwald 
876e0463bdcSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
877e0463bdcSMatthias Ringwald     // abort signed write validation if a new request comes in (but finish previous signed write if possible)
878e0463bdcSMatthias Ringwald     if (att_server->state == ATT_SERVER_W4_SIGNED_WRITE_VALIDATION){
879e0463bdcSMatthias Ringwald         if (packet[0] == ATT_SIGNED_WRITE_COMMAND){
880e0463bdcSMatthias Ringwald             log_info("skip new signed write request as previous is in validation");
881e0463bdcSMatthias Ringwald             return;
882e0463bdcSMatthias Ringwald         } else {
883e0463bdcSMatthias Ringwald             log_info("abort signed write validation to process new request");
884e0463bdcSMatthias Ringwald             att_server->state = ATT_SERVER_IDLE;
885e0463bdcSMatthias Ringwald         }
886e0463bdcSMatthias Ringwald     }
887e0463bdcSMatthias Ringwald #endif
8883deb3ec6SMatthias Ringwald     // last request still in processing?
88918707219SMatthias Ringwald     if (att_server->state != ATT_SERVER_IDLE){
890e0463bdcSMatthias Ringwald         log_info("skip att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state);
8916428eb5aSMatthias Ringwald         return;
8926428eb5aSMatthias Ringwald     }
8933deb3ec6SMatthias Ringwald 
8943deb3ec6SMatthias Ringwald     // store request
89518707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED;
89618707219SMatthias Ringwald     att_server->request_size = size;
8976535961aSMatthias Ringwald     (void)memcpy(att_server->request_buffer, packet, size);
8983deb3ec6SMatthias Ringwald 
899cd45e970SMatthias Ringwald     att_run_for_context(att_server, att_connection);
90041772f37SMatthias Ringwald }
90141772f37SMatthias Ringwald 
902*52e211deSMatthias Ringwald static void att_server_dispatch_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
90368591e36SMatthias Ringwald     hci_connection_t * hci_connection;
904b6df12b6SMatthias Ringwald     att_connection_t * att_connection;
90526b42dcfSMatthias Ringwald     att_server_t     * att_server;
906*52e211deSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
907*52e211deSMatthias Ringwald     hci_con_handle_t con_handle;
908*52e211deSMatthias Ringwald     bd_addr_t        address;
909*52e211deSMatthias Ringwald     btstack_linked_list_iterator_t it;
910*52e211deSMatthias Ringwald #endif
91141772f37SMatthias Ringwald 
91241772f37SMatthias Ringwald     switch (packet_type){
91341772f37SMatthias Ringwald         case HCI_EVENT_PACKET:
91441772f37SMatthias Ringwald             switch (packet[0]){
915*52e211deSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
916*52e211deSMatthias Ringwald                 case L2CAP_EVENT_CHANNEL_OPENED:
917*52e211deSMatthias Ringwald                     con_handle = l2cap_event_channel_opened_get_handle(packet);
918*52e211deSMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
919*52e211deSMatthias Ringwald                     if (!hci_connection) break;
920*52e211deSMatthias Ringwald                     att_server = &hci_connection->att_server;
921*52e211deSMatthias Ringwald                     // store connection info
922*52e211deSMatthias Ringwald                     att_server->bearer_type = ATT_BEARER_UNENHANCED_CLASSIC;
923*52e211deSMatthias Ringwald                     att_server->peer_addr_type = BD_ADDR_TYPE_ACL;
924*52e211deSMatthias Ringwald                     l2cap_event_channel_opened_get_address(packet, att_server->peer_address);
925*52e211deSMatthias Ringwald                     att_connection = &hci_connection->att_connection;
926*52e211deSMatthias Ringwald                     att_connection->con_handle = con_handle;
927*52e211deSMatthias Ringwald                     att_server->l2cap_cid = l2cap_event_channel_opened_get_local_cid(packet);
928*52e211deSMatthias Ringwald                     // reset connection properties
929*52e211deSMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
930*52e211deSMatthias Ringwald                     att_connection->mtu = l2cap_event_channel_opened_get_remote_mtu(packet);
931*52e211deSMatthias Ringwald                     att_connection->max_mtu = l2cap_max_mtu();
932*52e211deSMatthias Ringwald                     if (att_connection->max_mtu > ATT_REQUEST_BUFFER_SIZE){
933*52e211deSMatthias Ringwald                         att_connection->max_mtu = ATT_REQUEST_BUFFER_SIZE;
934*52e211deSMatthias Ringwald                     }
935*52e211deSMatthias Ringwald 
936*52e211deSMatthias Ringwald                     log_info("Connection opened %s, l2cap cid %04x, mtu %u", bd_addr_to_str(address), att_server->l2cap_cid, att_connection->mtu);
937*52e211deSMatthias Ringwald 
938*52e211deSMatthias Ringwald                     // update security params
939*52e211deSMatthias Ringwald                     att_connection->encryption_key_size = gap_encryption_key_size(con_handle);
940*52e211deSMatthias Ringwald                     att_connection->authenticated = gap_authenticated(con_handle);
941*52e211deSMatthias Ringwald                     att_connection->secure_connection = gap_secure_connection(con_handle);
942*52e211deSMatthias Ringwald                     log_info("encrypted key size %u, authenticated %u, secure connection %u",
943*52e211deSMatthias Ringwald                              att_connection->encryption_key_size, att_connection->authenticated, att_connection->secure_connection);
944*52e211deSMatthias Ringwald 
945*52e211deSMatthias Ringwald                     // notify connection opened
946*52e211deSMatthias Ringwald                     att_emit_connected_event(att_server, att_connection);
947*52e211deSMatthias Ringwald 
948*52e211deSMatthias Ringwald                     // restore persisten ccc if encrypted
949*52e211deSMatthias Ringwald                     if ( gap_security_level(con_handle) >= LEVEL_2){
950*52e211deSMatthias Ringwald                         att_server_persistent_ccc_restore(att_server, att_connection);
951*52e211deSMatthias Ringwald                     }
952*52e211deSMatthias Ringwald                     // TODO: what to do about le device db?
953*52e211deSMatthias Ringwald                     att_server->pairing_active = 0;
954*52e211deSMatthias Ringwald                     break;
955*52e211deSMatthias Ringwald #endif
95641772f37SMatthias Ringwald                 case L2CAP_EVENT_CAN_SEND_NOW:
95741772f37SMatthias Ringwald                     att_server_handle_can_send_now();
95841772f37SMatthias Ringwald                     break;
95941772f37SMatthias Ringwald                 case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
96041772f37SMatthias Ringwald                     // GATT client has negotiated the mtu for this connection
961*52e211deSMatthias Ringwald                     hci_connection = hci_connection_for_handle(channel);
96268591e36SMatthias Ringwald                     if (!hci_connection) break;
963388fa7c4SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
964b6df12b6SMatthias Ringwald                     att_connection->mtu = little_endian_read_16(packet, 4);
96541772f37SMatthias Ringwald                     break;
96641772f37SMatthias Ringwald                 default:
96741772f37SMatthias Ringwald                     break;
96841772f37SMatthias Ringwald             }
96941772f37SMatthias Ringwald             break;
97041772f37SMatthias Ringwald 
97141772f37SMatthias Ringwald         case ATT_DATA_PACKET:
972*52e211deSMatthias Ringwald             log_debug("ATT Packet, handle 0x%04x", channel);
973*52e211deSMatthias Ringwald             hci_connection = hci_connection_for_handle(channel);
97468591e36SMatthias Ringwald             if (!hci_connection) break;
97541772f37SMatthias Ringwald 
97626b42dcfSMatthias Ringwald             att_server = &hci_connection->att_server;
97726b42dcfSMatthias Ringwald             att_connection = &hci_connection->att_connection;
97826b42dcfSMatthias Ringwald             att_server_handle_att_pdu(att_server, att_connection, packet, size);
979372d62c4SMatthias Ringwald             break;
9807bbeb3adSMilanka Ringwald 
981*52e211deSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
982*52e211deSMatthias Ringwald         case L2CAP_DATA_PACKET:
983*52e211deSMatthias Ringwald             hci_connections_get_iterator(&it);
984*52e211deSMatthias Ringwald             while(btstack_linked_list_iterator_has_next(&it)){
985*52e211deSMatthias Ringwald                 hci_connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
986*52e211deSMatthias Ringwald                 att_server = &hci_connection->att_server;
987*52e211deSMatthias Ringwald                 att_connection = &hci_connection->att_connection;
988*52e211deSMatthias Ringwald                 if (att_server->l2cap_cid == channel) {
989*52e211deSMatthias Ringwald                     att_server_handle_att_pdu(att_server, att_connection, packet, size);
990*52e211deSMatthias Ringwald                     break;
991*52e211deSMatthias Ringwald                 }
992*52e211deSMatthias Ringwald             }
993*52e211deSMatthias Ringwald             break;
994*52e211deSMatthias Ringwald #endif
995*52e211deSMatthias Ringwald 
9967bbeb3adSMilanka Ringwald         default:
9977bbeb3adSMilanka Ringwald             break;
998372d62c4SMatthias Ringwald     }
9993deb3ec6SMatthias Ringwald }
10003deb3ec6SMatthias Ringwald 
1001bf78aac6SMatthias Ringwald // ---------------------
1002bf78aac6SMatthias Ringwald // persistent CCC writes
1003bf78aac6SMatthias Ringwald static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){
10044ea43905SMatthias Ringwald     return ('B' << 24u) | ('T' << 16u) | ('C' << 8u) | index;
1005bf78aac6SMatthias Ringwald }
1006bf78aac6SMatthias Ringwald 
1007bf78aac6SMatthias Ringwald static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){
1008bf78aac6SMatthias Ringwald     // lookup att_server instance
100968591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
101068591e36SMatthias Ringwald     if (!hci_connection) return;
101168591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
1012bf78aac6SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
1013bf78aac6SMatthias 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);
10146a540790SMatthias Ringwald 
1015bf78aac6SMatthias Ringwald     // check if bonded
1016bf78aac6SMatthias Ringwald     if (le_device_index < 0) return;
10176a540790SMatthias Ringwald 
1018bf78aac6SMatthias Ringwald     // get btstack_tlv
1019bf78aac6SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
1020bf78aac6SMatthias Ringwald     void * tlv_context;
1021bf78aac6SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
1022bf78aac6SMatthias Ringwald     if (!tlv_impl) return;
10236a540790SMatthias Ringwald 
1024bf78aac6SMatthias Ringwald     // update ccc tag
1025bf78aac6SMatthias Ringwald     int index;
10266a540790SMatthias Ringwald     uint32_t highest_seq_nr = 0;
10276a540790SMatthias Ringwald     uint32_t lowest_seq_nr = 0;
10286a540790SMatthias Ringwald     uint32_t tag_for_lowest_seq_nr = 0;
10296a540790SMatthias Ringwald     uint32_t tag_for_empty = 0;
10306a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
10316afb9acaSMatthias Ringwald     for (index=0; index<NVN_NUM_GATT_SERVER_CCC; index++){
1032bf78aac6SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
10336a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
10346a540790SMatthias Ringwald 
10356a540790SMatthias Ringwald         // empty/invalid tag
10366a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)){
10376a540790SMatthias Ringwald             tag_for_empty = tag;
1038bf78aac6SMatthias Ringwald             continue;
1039bf78aac6SMatthias Ringwald         }
10406a540790SMatthias Ringwald         // update highest seq nr
10416a540790SMatthias Ringwald         if (entry.seq_nr > highest_seq_nr){
10426a540790SMatthias Ringwald             highest_seq_nr = entry.seq_nr;
10436a540790SMatthias Ringwald         }
10446a540790SMatthias Ringwald         // find entry with lowest seq nr
10454ea43905SMatthias Ringwald         if ((tag_for_lowest_seq_nr == 0u) || (entry.seq_nr < lowest_seq_nr)){
10466a540790SMatthias Ringwald             tag_for_lowest_seq_nr = tag;
10476a540790SMatthias Ringwald             lowest_seq_nr = entry.seq_nr;
10486a540790SMatthias Ringwald         }
10496a540790SMatthias Ringwald 
10506a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
10516a540790SMatthias Ringwald         if (entry.att_handle   != att_handle)      continue;
10526a540790SMatthias Ringwald 
10536a540790SMatthias Ringwald         // found matching entry
10549305033eSMatthias Ringwald         if (value != 0){
1055bf78aac6SMatthias Ringwald             // update
10566a540790SMatthias Ringwald             if (entry.value == value) {
1057760ad805SMatthias Ringwald                 log_info("CCC Index %u: Up-to-date", index);
1058760ad805SMatthias Ringwald                 return;
1059760ad805SMatthias Ringwald             }
1060348fc806SMatthias Ringwald             entry.value = (uint8_t) value;
10614ea43905SMatthias Ringwald             entry.seq_nr = highest_seq_nr + 1u;
1062760ad805SMatthias Ringwald             log_info("CCC Index %u: Store", index);
10630fe46bc1SMatthias Ringwald             int result = tlv_impl->store_tag(tlv_context, tag, (const uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
10640fe46bc1SMatthias Ringwald             if (result != 0){
10650fe46bc1SMatthias Ringwald                 log_error("Store tag index %u failed", index);
10660fe46bc1SMatthias Ringwald             }
106701ac2008SMatthias Ringwald         } else {
106801ac2008SMatthias Ringwald             // delete
1069760ad805SMatthias Ringwald             log_info("CCC Index %u: Delete", index);
107001ac2008SMatthias Ringwald             tlv_impl->delete_tag(tlv_context, tag);
107101ac2008SMatthias Ringwald         }
1072bf78aac6SMatthias Ringwald         return;
1073bf78aac6SMatthias Ringwald     }
10746a540790SMatthias Ringwald 
10750fe46bc1SMatthias Ringwald     log_info("tag_for_empty %"PRIx32", tag_for_lowest_seq_nr %"PRIx32, tag_for_empty, tag_for_lowest_seq_nr);
10766a540790SMatthias Ringwald 
10774ea43905SMatthias Ringwald     if (value == 0u){
10786a540790SMatthias Ringwald         // done
10796a540790SMatthias Ringwald         return;
10806a540790SMatthias Ringwald     }
10816a540790SMatthias Ringwald 
108238b893aaSMilanka Ringwald     uint32_t tag_to_use = 0u;
108338b893aaSMilanka Ringwald     if (tag_for_empty != 0u){
10846a540790SMatthias Ringwald         tag_to_use = tag_for_empty;
10856a540790SMatthias Ringwald     } else if (tag_for_lowest_seq_nr){
10866a540790SMatthias Ringwald         tag_to_use = tag_for_lowest_seq_nr;
10876a540790SMatthias Ringwald     } else {
10886a540790SMatthias Ringwald         // should not happen
10896a540790SMatthias Ringwald         return;
10906a540790SMatthias Ringwald     }
1091bf78aac6SMatthias Ringwald     // store ccc tag
10924ea43905SMatthias Ringwald     entry.seq_nr       = highest_seq_nr + 1u;
10936a540790SMatthias Ringwald     entry.device_index = le_device_index;
10946a540790SMatthias Ringwald     entry.att_handle   = att_handle;
1095348fc806SMatthias Ringwald     entry.value        = (uint8_t) value;
10960fe46bc1SMatthias Ringwald     int result = tlv_impl->store_tag(tlv_context, tag_to_use, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
10970fe46bc1SMatthias Ringwald     if (result != 0){
10980fe46bc1SMatthias Ringwald         log_error("Store tag index %u failed", index);
10990fe46bc1SMatthias Ringwald     }
1100bf78aac6SMatthias Ringwald }
1101bf78aac6SMatthias Ringwald 
1102fe3d0cbaSMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server){
1103760ad805SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
1104760ad805SMatthias Ringwald     log_info("Clear CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
1105760ad805SMatthias Ringwald     // check if bonded
1106760ad805SMatthias Ringwald     if (le_device_index < 0) return;
1107760ad805SMatthias Ringwald     // get btstack_tlv
1108760ad805SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
1109760ad805SMatthias Ringwald     void * tlv_context;
1110760ad805SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
1111760ad805SMatthias Ringwald     if (!tlv_impl) return;
1112760ad805SMatthias Ringwald     // get all ccc tag
1113760ad805SMatthias Ringwald     int index;
11146a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
11156afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
1116760ad805SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
11176a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
11186a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
11196a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
1120760ad805SMatthias Ringwald         // delete entry
1121760ad805SMatthias Ringwald         log_info("CCC Index %u: Delete", index);
1122760ad805SMatthias Ringwald         tlv_impl->delete_tag(tlv_context, tag);
1123760ad805SMatthias Ringwald     }
1124760ad805SMatthias Ringwald }
1125760ad805SMatthias Ringwald 
11269a1f55eaSMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server, att_connection_t * att_connection){
112701ac2008SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
112801ac2008SMatthias Ringwald     log_info("Restore CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
11291b7acd0dSMatthias Ringwald     // check if bonded
11301b7acd0dSMatthias Ringwald     if (le_device_index < 0) return;
113101ac2008SMatthias Ringwald     // get btstack_tlv
113201ac2008SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
113301ac2008SMatthias Ringwald     void * tlv_context;
113401ac2008SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
113501ac2008SMatthias Ringwald     if (!tlv_impl) return;
113601ac2008SMatthias Ringwald     // get all ccc tag
113701ac2008SMatthias Ringwald     int index;
11386a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
11396afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
114001ac2008SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
11416a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
11426a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
11436a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
114401ac2008SMatthias Ringwald         // simulate write callback
11456a540790SMatthias Ringwald         uint16_t attribute_handle = entry.att_handle;
114601ac2008SMatthias Ringwald         uint8_t  value[2];
11476a540790SMatthias Ringwald         little_endian_store_16(value, 0, entry.value);
114801ac2008SMatthias Ringwald         att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
114901ac2008SMatthias Ringwald         if (!callback) continue;
11506a540790SMatthias Ringwald         log_info("CCC Index %u: Set Attribute handle 0x%04x to value 0x%04x", index, attribute_handle, entry.value );
1151b6df12b6SMatthias Ringwald         (*callback)(att_connection->con_handle, attribute_handle, ATT_TRANSACTION_MODE_NONE, 0, value, sizeof(value));
115201ac2008SMatthias Ringwald     }
115301ac2008SMatthias Ringwald }
115401ac2008SMatthias Ringwald 
1155bf78aac6SMatthias Ringwald // persistent CCC writes
1156bf78aac6SMatthias Ringwald // ---------------------
11573d71c7a4SMatthias Ringwald 
11583d71c7a4SMatthias Ringwald // gatt service management
11593d71c7a4SMatthias Ringwald static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){
11603d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
11613d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
11623d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
11633d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
11643d71c7a4SMatthias Ringwald         if (handler->start_handle > handle) continue;
11653d71c7a4SMatthias Ringwald         if (handler->end_handle   < handle) continue;
11663d71c7a4SMatthias Ringwald         return handler;
11673d71c7a4SMatthias Ringwald     }
11683d71c7a4SMatthias Ringwald     return NULL;
11693d71c7a4SMatthias Ringwald }
11703d71c7a4SMatthias Ringwald static att_read_callback_t att_server_read_callback_for_handle(uint16_t handle){
11713d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
11729305033eSMatthias Ringwald     if (handler != NULL) return handler->read_callback;
11733d71c7a4SMatthias Ringwald     return att_server_client_read_callback;
11743d71c7a4SMatthias Ringwald }
11753d71c7a4SMatthias Ringwald 
11763d71c7a4SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){
11773d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
11789305033eSMatthias Ringwald     if (handler != NULL) return handler->write_callback;
11793d71c7a4SMatthias Ringwald     return att_server_client_write_callback;
11803d71c7a4SMatthias Ringwald }
11813d71c7a4SMatthias Ringwald 
11825d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle){
11835d07396bSMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
11849305033eSMatthias Ringwald     if (handler != NULL) return handler->packet_handler;
11855d07396bSMatthias Ringwald     return att_client_packet_handler;
11865d07396bSMatthias Ringwald }
11875d07396bSMatthias Ringwald 
11883d71c7a4SMatthias Ringwald static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){
11893d71c7a4SMatthias Ringwald     // notify all callbacks
11903d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
11913d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
11923d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
11933d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
11943d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
11953d71c7a4SMatthias Ringwald         (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
11963d71c7a4SMatthias Ringwald     }
11973d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return;
11983d71c7a4SMatthias Ringwald     (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
11993d71c7a4SMatthias Ringwald }
12003d71c7a4SMatthias Ringwald 
12013d71c7a4SMatthias Ringwald // returns first reported error or 0
12023d71c7a4SMatthias Ringwald static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){
12033d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
12043d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
12053d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
12063d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
12073d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
12083d71c7a4SMatthias Ringwald         uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
120938b893aaSMilanka Ringwald         if (error_code != 0u) return error_code;
12103d71c7a4SMatthias Ringwald     }
12113d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return 0;
12123d71c7a4SMatthias Ringwald     return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
12133d71c7a4SMatthias Ringwald }
12143d71c7a4SMatthias Ringwald 
12153d71c7a4SMatthias 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){
12163d71c7a4SMatthias Ringwald     att_read_callback_t callback = att_server_read_callback_for_handle(attribute_handle);
121701ac2008SMatthias Ringwald     if (!callback) return 0;
12183d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, offset, buffer, buffer_size);
12193d71c7a4SMatthias Ringwald }
12203d71c7a4SMatthias Ringwald 
12213d71c7a4SMatthias 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){
12223d71c7a4SMatthias Ringwald     switch (transaction_mode){
12233d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_VALIDATE:
12243d71c7a4SMatthias Ringwald             return att_validate_prepared_write(con_handle);
12253d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_EXECUTE:
12263d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_CANCEL:
12273d71c7a4SMatthias Ringwald             att_notify_write_callbacks(con_handle, transaction_mode);
12283d71c7a4SMatthias Ringwald             return 0;
12293d71c7a4SMatthias Ringwald         default:
12303d71c7a4SMatthias Ringwald             break;
12313d71c7a4SMatthias Ringwald     }
12323d71c7a4SMatthias Ringwald 
1233bf78aac6SMatthias Ringwald     // track CCC writes
12344ea43905SMatthias Ringwald     if (att_is_persistent_ccc(attribute_handle) && (offset == 0u) && (buffer_size == 2u)){
1235bf78aac6SMatthias Ringwald         att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0));
1236bf78aac6SMatthias Ringwald     }
1237bf78aac6SMatthias Ringwald 
123801ac2008SMatthias Ringwald     att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
123901ac2008SMatthias Ringwald     if (!callback) return 0;
12403d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size);
12413d71c7a4SMatthias Ringwald }
12423d71c7a4SMatthias Ringwald 
12433d71c7a4SMatthias Ringwald /**
12443d71c7a4SMatthias Ringwald  * @brief register read/write callbacks for specific handle range
12453d71c7a4SMatthias Ringwald  * @param att_service_handler_t
12463d71c7a4SMatthias Ringwald  */
12473d71c7a4SMatthias Ringwald void att_server_register_service_handler(att_service_handler_t * handler){
1248ec8f5414SMilanka Ringwald     bool att_server_registered = false;
1249ec8f5414SMilanka Ringwald     if (att_service_handler_for_handle(handler->start_handle)){
1250ec8f5414SMilanka Ringwald         att_server_registered = true;
1251ec8f5414SMilanka Ringwald     }
1252ec8f5414SMilanka Ringwald 
1253ec8f5414SMilanka Ringwald     if (att_service_handler_for_handle(handler->end_handle)){
1254ec8f5414SMilanka Ringwald         att_server_registered = true;
1255ec8f5414SMilanka Ringwald     }
1256ec8f5414SMilanka Ringwald 
1257ec8f5414SMilanka Ringwald     if (att_server_registered){
12583d71c7a4SMatthias Ringwald         log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle);
12593d71c7a4SMatthias Ringwald         return;
12603d71c7a4SMatthias Ringwald     }
12613d71c7a4SMatthias Ringwald     btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler);
12623d71c7a4SMatthias Ringwald }
12633d71c7a4SMatthias Ringwald 
12643deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){
12653deb3ec6SMatthias Ringwald 
12663d71c7a4SMatthias Ringwald     // store callbacks
12673d71c7a4SMatthias Ringwald     att_server_client_read_callback  = read_callback;
12683d71c7a4SMatthias Ringwald     att_server_client_write_callback = write_callback;
12693d71c7a4SMatthias Ringwald 
12701a389cdcSMatthias Ringwald     // register for HCI Events
1271*52e211deSMatthias Ringwald     hci_event_callback_registration.callback = &att_server_event_packet_handler;
12721a389cdcSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
12731a389cdcSMatthias Ringwald 
1274406722e4SMatthias Ringwald     // register for SM events
1275*52e211deSMatthias Ringwald     sm_event_callback_registration.callback = &att_server_event_packet_handler;
1276406722e4SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
12773deb3ec6SMatthias Ringwald 
12781a389cdcSMatthias Ringwald     // and L2CAP ATT Server PDUs
1279*52e211deSMatthias Ringwald     att_dispatch_register_server(att_server_dispatch_packet_handler);
12803deb3ec6SMatthias Ringwald 
128170dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
128270dca4d4SMatthias Ringwald     // setup l2cap service
12836bf42d1fSMatthias Ringwald     att_dispatch_classic_register_service();
128470dca4d4SMatthias Ringwald #endif
128570dca4d4SMatthias Ringwald 
12863deb3ec6SMatthias Ringwald     att_set_db(db);
12873d71c7a4SMatthias Ringwald     att_set_read_callback(att_server_read_callback);
12883d71c7a4SMatthias Ringwald     att_set_write_callback(att_server_write_callback);
12893deb3ec6SMatthias Ringwald }
12903deb3ec6SMatthias Ringwald 
12913deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){
12923deb3ec6SMatthias Ringwald     att_client_packet_handler = handler;
12933deb3ec6SMatthias Ringwald }
12943deb3ec6SMatthias Ringwald 
1295cbbb12d9SMatthias Ringwald 
1296cbbb12d9SMatthias Ringwald // to be deprecated
1297c141988cSMatthias Ringwald int  att_server_can_send_packet_now(hci_con_handle_t con_handle){
129868591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
129968591e36SMatthias Ringwald     if (!hci_connection) return 0;
130037a8059dSMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
130137a8059dSMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
130237a8059dSMatthias Ringwald     return att_server_can_send_packet(att_server, att_connection);
13031b96b007SMatthias Ringwald }
13041b96b007SMatthias Ringwald 
13059816429dSMatthias Ringwald uint8_t att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
1306cbbb12d9SMatthias Ringwald     return att_server_request_to_send_notification(callback_registration, con_handle);
13073deb3ec6SMatthias Ringwald }
13083deb3ec6SMatthias Ringwald 
1309cbbb12d9SMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){
1310cbbb12d9SMatthias Ringwald     att_client_waiting_for_can_send_registration.callback = &att_emit_can_send_now_event;
1311cbbb12d9SMatthias Ringwald     att_server_request_to_send_notification(&att_client_waiting_for_can_send_registration, con_handle);
1312cbbb12d9SMatthias Ringwald }
1313cbbb12d9SMatthias Ringwald // end of deprecated
1314cbbb12d9SMatthias Ringwald 
13159816429dSMatthias Ringwald uint8_t att_server_request_to_send_notification(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
131668591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
131768591e36SMatthias Ringwald     if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
131868591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
13193f9b77dbSMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
1320c37df6f6SMatthias Ringwald     bool added = btstack_linked_list_add_tail(&att_server->notification_requests, (btstack_linked_item_t*) callback_registration);
13213f9b77dbSMatthias Ringwald     att_server_request_can_send_now(att_server, att_connection);
1322c37df6f6SMatthias Ringwald     if (added){
1323cbbb12d9SMatthias Ringwald         return ERROR_CODE_SUCCESS;
1324c37df6f6SMatthias Ringwald     } else {
1325c37df6f6SMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1326c37df6f6SMatthias Ringwald     }
1327cbbb12d9SMatthias Ringwald }
1328cbbb12d9SMatthias Ringwald 
13299816429dSMatthias Ringwald uint8_t att_server_request_to_send_indication(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
133068591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
133168591e36SMatthias Ringwald     if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
133268591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
13333f9b77dbSMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
1334c37df6f6SMatthias Ringwald     bool added = btstack_linked_list_add_tail(&att_server->indication_requests, (btstack_linked_item_t*) callback_registration);
13353f9b77dbSMatthias Ringwald     att_server_request_can_send_now(att_server, att_connection);
1336c37df6f6SMatthias Ringwald     if (added){
1337969a5bbaSMatthias Ringwald         return ERROR_CODE_SUCCESS;
1338c37df6f6SMatthias Ringwald     } else {
1339c37df6f6SMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1340c37df6f6SMatthias Ringwald     }
1341bb38f057SMatthias Ringwald }
1342bb38f057SMatthias Ringwald 
1343d74c7b2bSMatthias Ringwald static uint8_t att_server_prepare_server_message(hci_con_handle_t con_handle, att_server_t ** out_att_server, att_connection_t ** out_att_connection, uint8_t ** out_packet_buffer){
1344d74c7b2bSMatthias Ringwald 
1345d74c7b2bSMatthias Ringwald     att_server_t *     att_server = NULL;
1346d74c7b2bSMatthias Ringwald     att_connection_t * att_connection = NULL;
1347d74c7b2bSMatthias Ringwald     uint8_t *          packet_buffer = NULL;
1348d74c7b2bSMatthias Ringwald 
1349d74c7b2bSMatthias Ringwald     // prefer enhanced bearer
1350d74c7b2bSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
1351d74c7b2bSMatthias Ringwald     att_server_eatt_bearer_t * eatt_bearer = att_server_eatt_bearer_for_con_handle(con_handle);
1352d74c7b2bSMatthias Ringwald     if (eatt_bearer != NULL){
1353d74c7b2bSMatthias Ringwald         att_server     = &eatt_bearer->att_server;
1354d74c7b2bSMatthias Ringwald         att_connection = &eatt_bearer->att_connection;
1355d74c7b2bSMatthias Ringwald         packet_buffer  = eatt_bearer->send_buffer;
1356d74c7b2bSMatthias Ringwald     } else
1357d74c7b2bSMatthias Ringwald #endif
1358d74c7b2bSMatthias Ringwald     {
1359d74c7b2bSMatthias Ringwald         hci_connection_t *hci_connection = hci_connection_for_handle(con_handle);
1360d74c7b2bSMatthias Ringwald         if (hci_connection != NULL) {
1361d74c7b2bSMatthias Ringwald             att_server     = &hci_connection->att_server;
1362d74c7b2bSMatthias Ringwald             att_connection = &hci_connection->att_connection;
1363d74c7b2bSMatthias Ringwald         }
1364d74c7b2bSMatthias Ringwald     }
1365d74c7b2bSMatthias Ringwald 
1366d74c7b2bSMatthias Ringwald     if (att_server == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1367d74c7b2bSMatthias Ringwald     if (!att_server_can_send_packet(att_server, att_connection)) return BTSTACK_ACL_BUFFERS_FULL;
1368d74c7b2bSMatthias Ringwald 
1369d74c7b2bSMatthias Ringwald     if (packet_buffer == NULL){
1370d74c7b2bSMatthias Ringwald         l2cap_reserve_packet_buffer();
1371d74c7b2bSMatthias Ringwald         packet_buffer = l2cap_get_outgoing_buffer();
1372d74c7b2bSMatthias Ringwald     }
1373d74c7b2bSMatthias Ringwald 
1374d74c7b2bSMatthias Ringwald     *out_att_connection = att_connection;
1375d74c7b2bSMatthias Ringwald     *out_att_server = att_server;
1376d74c7b2bSMatthias Ringwald     *out_packet_buffer = packet_buffer;
1377d74c7b2bSMatthias Ringwald     return ERROR_CODE_SUCCESS;
1378d74c7b2bSMatthias Ringwald }
1379d74c7b2bSMatthias Ringwald 
13809816429dSMatthias Ringwald uint8_t att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){
13815683a8e4SMatthias Ringwald     att_server_t * att_server = NULL;
13825683a8e4SMatthias Ringwald     att_connection_t * att_connection = NULL;
13835683a8e4SMatthias Ringwald     uint8_t * packet_buffer = NULL;
138468591e36SMatthias Ringwald 
13855683a8e4SMatthias Ringwald     uint8_t status = att_server_prepare_server_message(con_handle, &att_server, &att_connection, &packet_buffer);
13865683a8e4SMatthias Ringwald     if (status != ERROR_CODE_SUCCESS){
13875683a8e4SMatthias Ringwald         return status;
13885683a8e4SMatthias Ringwald     }
13893deb3ec6SMatthias Ringwald 
1390b6df12b6SMatthias Ringwald     uint16_t size = att_prepare_handle_value_notification(att_connection, attribute_handle, value, value_len, packet_buffer);
1391d415948cSMatthias Ringwald 
13920f70c52eSMatthias Ringwald     return att_server_send_prepared(att_server, att_connection, NULL, size);
13933deb3ec6SMatthias Ringwald }
13943deb3ec6SMatthias Ringwald 
139540f8d8f2SMatthias Ringwald /**
139640f8d8f2SMatthias Ringwald  * @brief notify client about multiple attribute value changes
139740f8d8f2SMatthias Ringwald  * @param con_handle
139840f8d8f2SMatthias Ringwald  * @param num_attributes
139940f8d8f2SMatthias Ringwald  * @param attribute_handles[]
140040f8d8f2SMatthias Ringwald  * @param values_data[]
140140f8d8f2SMatthias Ringwald  * @param values_len[]
140240f8d8f2SMatthias Ringwald  * @return 0 if ok, error otherwise
140340f8d8f2SMatthias Ringwald  */
140440f8d8f2SMatthias Ringwald uint8_t att_server_multiple_notify(hci_con_handle_t con_handle, uint8_t num_attributes,
140540f8d8f2SMatthias Ringwald                                    const uint16_t * attribute_handles, const uint8_t ** values_data, const uint16_t * values_len){
140640f8d8f2SMatthias Ringwald 
140740f8d8f2SMatthias Ringwald     att_server_t * att_server = NULL;
140840f8d8f2SMatthias Ringwald     att_connection_t * att_connection = NULL;
140940f8d8f2SMatthias Ringwald     uint8_t * packet_buffer = NULL;
141040f8d8f2SMatthias Ringwald 
1411d74c7b2bSMatthias Ringwald     uint8_t status = att_server_prepare_server_message(con_handle, &att_server, &att_connection, &packet_buffer);
1412d74c7b2bSMatthias Ringwald     if (status != ERROR_CODE_SUCCESS){
1413d74c7b2bSMatthias Ringwald         return status;
141440f8d8f2SMatthias Ringwald     }
141540f8d8f2SMatthias Ringwald 
141640f8d8f2SMatthias Ringwald     uint16_t size = att_prepare_handle_value_multiple_notification(att_connection, num_attributes, attribute_handles, values_data, values_len, packet_buffer);
141740f8d8f2SMatthias Ringwald 
141840f8d8f2SMatthias Ringwald     return att_server_send_prepared(att_server, att_connection, packet_buffer, size);
141940f8d8f2SMatthias Ringwald }
142040f8d8f2SMatthias Ringwald 
14219816429dSMatthias Ringwald uint8_t att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){
142218707219SMatthias Ringwald 
14235683a8e4SMatthias Ringwald     att_server_t * att_server = NULL;
14245683a8e4SMatthias Ringwald     att_connection_t * att_connection = NULL;
14255683a8e4SMatthias Ringwald     uint8_t * packet_buffer = NULL;
14265683a8e4SMatthias Ringwald 
14275683a8e4SMatthias Ringwald     uint8_t status = att_server_prepare_server_message(con_handle, &att_server, &att_connection, &packet_buffer);
14285683a8e4SMatthias Ringwald     if (status != ERROR_CODE_SUCCESS){
14295683a8e4SMatthias Ringwald         return status;
14305683a8e4SMatthias Ringwald     }
14315683a8e4SMatthias Ringwald 
14325683a8e4SMatthias Ringwald     if (att_server->value_indication_handle != 0u) {
14335683a8e4SMatthias Ringwald         // free reserved packet buffer
14345683a8e4SMatthias Ringwald         if (att_server->bearer_type == ATT_BEARER_ENHANCED_LE){
14355683a8e4SMatthias Ringwald             l2cap_release_packet_buffer();
14365683a8e4SMatthias Ringwald         }
14375683a8e4SMatthias Ringwald         return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS;
14385683a8e4SMatthias Ringwald     }
14393deb3ec6SMatthias Ringwald 
14403deb3ec6SMatthias Ringwald     // track indication
144118707219SMatthias Ringwald     att_server->value_indication_handle = attribute_handle;
144218707219SMatthias Ringwald     btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout);
144318707219SMatthias Ringwald     btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS);
144418707219SMatthias Ringwald     btstack_run_loop_add_timer(&att_server->value_indication_timer);
14453deb3ec6SMatthias Ringwald 
1446b6df12b6SMatthias Ringwald     uint16_t size = att_prepare_handle_value_indication(att_connection, attribute_handle, value, value_len, packet_buffer);
144723079b39SMatthias Ringwald 
14480f70c52eSMatthias Ringwald     return att_server_send_prepared(att_server, att_connection, NULL, size);
14493deb3ec6SMatthias Ringwald }
14508f9132b5SMilanka Ringwald 
14518f9132b5SMilanka Ringwald uint16_t att_server_get_mtu(hci_con_handle_t con_handle){
145268591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
145368591e36SMatthias Ringwald     if (!hci_connection) return 0;
1454388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
1455b6df12b6SMatthias Ringwald     return att_connection->mtu;
14568f9132b5SMilanka Ringwald }
145735d7f2dfSMilanka Ringwald 
145835d7f2dfSMilanka Ringwald void att_server_deinit(void){
145935d7f2dfSMilanka Ringwald     att_server_client_read_callback  = NULL;
146035d7f2dfSMilanka Ringwald     att_server_client_write_callback = NULL;
146135d7f2dfSMilanka Ringwald     att_client_packet_handler = NULL;
146235d7f2dfSMilanka Ringwald     service_handlers = NULL;
146335d7f2dfSMilanka Ringwald }
146420d4358dSMatthias Ringwald 
146520d4358dSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
14668d3bb68fSMatthias Ringwald 
146727afb094SMatthias Ringwald #define MAX_NR_EATT_CHANNELS 5
146827afb094SMatthias Ringwald 
146920d4358dSMatthias Ringwald static uint16_t att_server_eatt_receive_buffer_size;
14708d3bb68fSMatthias Ringwald static uint16_t att_server_eatt_send_buffer_size;
147120d4358dSMatthias Ringwald 
1472523bb989SMatthias Ringwald static att_server_eatt_bearer_t * att_server_eatt_bearer_for_cid(uint16_t cid){
1473523bb989SMatthias Ringwald     btstack_linked_list_iterator_t it;
1474523bb989SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &att_server_eatt_bearer_active);
1475523bb989SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
1476523bb989SMatthias Ringwald         att_server_eatt_bearer_t * eatt_bearer = (att_server_eatt_bearer_t *) btstack_linked_list_iterator_next(&it);
1477f786dd08SMatthias Ringwald         if (eatt_bearer->att_server.l2cap_cid == cid) {
1478523bb989SMatthias Ringwald             return eatt_bearer;
1479523bb989SMatthias Ringwald         }
1480523bb989SMatthias Ringwald     }
1481523bb989SMatthias Ringwald     return NULL;
1482523bb989SMatthias Ringwald }
1483523bb989SMatthias Ringwald 
148440f8d8f2SMatthias Ringwald static att_server_eatt_bearer_t * att_server_eatt_bearer_for_con_handle(hci_con_handle_t con_handle){
148540f8d8f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
148640f8d8f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &att_server_eatt_bearer_active);
148740f8d8f2SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
148840f8d8f2SMatthias Ringwald         att_server_eatt_bearer_t * eatt_bearer = (att_server_eatt_bearer_t *) btstack_linked_list_iterator_next(&it);
148940f8d8f2SMatthias Ringwald         if (eatt_bearer->att_connection.con_handle == con_handle) {
149040f8d8f2SMatthias Ringwald             return eatt_bearer;
149140f8d8f2SMatthias Ringwald         }
149240f8d8f2SMatthias Ringwald     }
149340f8d8f2SMatthias Ringwald     return NULL;
149440f8d8f2SMatthias Ringwald }
149540f8d8f2SMatthias Ringwald 
149620d4358dSMatthias Ringwald static void att_server_eatt_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
149720d4358dSMatthias Ringwald     uint16_t cid;
149820d4358dSMatthias Ringwald     uint8_t status;
149920d4358dSMatthias Ringwald     uint16_t remote_mtu;
150020d4358dSMatthias Ringwald 
150120d4358dSMatthias Ringwald     uint8_t i;
150220d4358dSMatthias Ringwald     uint8_t num_requested_bearers;
150320d4358dSMatthias Ringwald     uint8_t num_accepted_bearers;
150427afb094SMatthias Ringwald     uint16_t initial_credits = L2CAP_LE_AUTOMATIC_CREDITS;
150527afb094SMatthias Ringwald     uint8_t * receive_buffers[MAX_NR_EATT_CHANNELS];
150627afb094SMatthias Ringwald     uint16_t cids[MAX_NR_EATT_CHANNELS];
150727afb094SMatthias Ringwald     att_server_eatt_bearer_t * eatt_bearers[MAX_NR_EATT_CHANNELS];
1508523bb989SMatthias Ringwald     att_server_eatt_bearer_t * eatt_bearer;
15092954c397SMatthias Ringwald     att_server_t * att_server;
15102954c397SMatthias Ringwald     att_connection_t * att_connection;
151120d4358dSMatthias Ringwald 
151220d4358dSMatthias Ringwald     switch (packet_type) {
151320d4358dSMatthias Ringwald 
151420d4358dSMatthias Ringwald         case L2CAP_DATA_PACKET:
1515f786dd08SMatthias Ringwald             eatt_bearer = att_server_eatt_bearer_for_cid(channel);
1516f786dd08SMatthias Ringwald             btstack_assert(eatt_bearer != NULL);
1517f786dd08SMatthias Ringwald             att_server = &eatt_bearer->att_server;
1518f786dd08SMatthias Ringwald             att_connection = &eatt_bearer->att_connection;
1519f786dd08SMatthias Ringwald             att_server_handle_att_pdu(att_server, att_connection, packet, size);
152020d4358dSMatthias Ringwald             break;
152120d4358dSMatthias Ringwald 
152220d4358dSMatthias Ringwald         case HCI_EVENT_PACKET:
152320d4358dSMatthias Ringwald             switch (packet[0]) {
152420d4358dSMatthias Ringwald 
152520d4358dSMatthias Ringwald                 case L2CAP_EVENT_CAN_SEND_NOW:
1526f786dd08SMatthias Ringwald                     cid = l2cap_event_packet_sent_get_local_cid(packet);
1527f786dd08SMatthias Ringwald                     eatt_bearer = att_server_eatt_bearer_for_cid(cid);
1528f786dd08SMatthias Ringwald                     btstack_assert(eatt_bearer != NULL);
1529f786dd08SMatthias Ringwald                     att_server = &eatt_bearer->att_server;
1530f786dd08SMatthias Ringwald                     att_connection = &eatt_bearer->att_connection;
1531f786dd08SMatthias Ringwald                     // only used for EATT request responses
1532f786dd08SMatthias Ringwald                     btstack_assert(att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED);
1533f786dd08SMatthias Ringwald                     att_server_process_validated_request(att_server, att_connection, eatt_bearer->send_buffer);
153420d4358dSMatthias Ringwald                     break;
153520d4358dSMatthias Ringwald 
153620d4358dSMatthias Ringwald                 case L2CAP_EVENT_PACKET_SENT:
153720d4358dSMatthias Ringwald                     cid = l2cap_event_packet_sent_get_local_cid(packet);
1538be4ca859SMatthias Ringwald                     eatt_bearer = att_server_eatt_bearer_for_cid(cid);
1539f786dd08SMatthias Ringwald                     btstack_assert(eatt_bearer != NULL);
15402954c397SMatthias Ringwald                     att_server = &eatt_bearer->att_server;
15412954c397SMatthias Ringwald                     att_connection = &eatt_bearer->att_connection;
15422954c397SMatthias Ringwald                     att_server_handle_att_pdu(att_server, att_connection, packet, size);
154320d4358dSMatthias Ringwald                     break;
154420d4358dSMatthias Ringwald 
154520d4358dSMatthias Ringwald                 case L2CAP_EVENT_ECBM_INCOMING_CONNECTION:
154620d4358dSMatthias Ringwald                     cid = l2cap_event_ecbm_incoming_connection_get_local_cid(packet);
154720d4358dSMatthias Ringwald                     num_requested_bearers = l2cap_event_ecbm_incoming_connection_get_num_channels(packet);
154820d4358dSMatthias Ringwald                     for (i = 0; i < num_requested_bearers; i++){
1549c2cbe117SMatthias Ringwald                         eatt_bearers[i] = (att_server_eatt_bearer_t *) btstack_linked_list_pop(&att_server_eatt_bearer_pool);
155020d4358dSMatthias Ringwald                         if (eatt_bearers[i] == NULL) {
155120d4358dSMatthias Ringwald                             break;
155220d4358dSMatthias Ringwald                         }
1553be4ca859SMatthias Ringwald                         eatt_bearers[i]->att_connection.con_handle = l2cap_event_ecbm_incoming_connection_get_handle(packet);
1554f786dd08SMatthias Ringwald                         eatt_bearers[i]->att_server.bearer_type = ATT_BEARER_ENHANCED_LE;
1555735b0222SMatthias Ringwald                         receive_buffers[i] = eatt_bearers[i]->receive_buffer;
15566b236cb0SMatthias Ringwald                         btstack_linked_list_add(&att_server_eatt_bearer_active, (btstack_linked_item_t *) eatt_bearers[i]);
155720d4358dSMatthias Ringwald                     }
155820d4358dSMatthias Ringwald                     num_accepted_bearers = i;
155920d4358dSMatthias Ringwald                     status = l2cap_ecbm_accept_channels(cid, num_accepted_bearers, initial_credits, att_server_eatt_receive_buffer_size, receive_buffers, cids);
156020d4358dSMatthias Ringwald                     btstack_assert(status == ERROR_CODE_SUCCESS);
1561c2cbe117SMatthias Ringwald                     log_info("requested %u, accepted %u", num_requested_bearers, num_accepted_bearers);
15626b236cb0SMatthias Ringwald                     for (i=0;i<num_accepted_bearers;i++){
1563c2cbe117SMatthias Ringwald                         log_info("eatt l2cap cid: 0x%04x", cids[i]);
1564f786dd08SMatthias Ringwald                         eatt_bearers[i]->att_server.l2cap_cid = cids[i];
15656b236cb0SMatthias Ringwald                     }
156620d4358dSMatthias Ringwald                     break;
156720d4358dSMatthias Ringwald 
156820d4358dSMatthias Ringwald                 case L2CAP_EVENT_ECBM_CHANNEL_OPENED:
156920d4358dSMatthias Ringwald                     cid         = l2cap_event_ecbm_channel_opened_get_local_cid(packet);
1570c2cbe117SMatthias Ringwald                     status      = l2cap_event_ecbm_channel_opened_get_status(packet);
1571c2cbe117SMatthias Ringwald                     remote_mtu  = l2cap_event_ecbm_channel_opened_get_remote_mtu(packet);
1572be4ca859SMatthias Ringwald                     eatt_bearer = att_server_eatt_bearer_for_cid(cid);
1573be4ca859SMatthias Ringwald                     btstack_assert(eatt_bearer != NULL);
1574c2cbe117SMatthias Ringwald                     eatt_bearer->att_connection.mtu_exchanged = true;
1575c2cbe117SMatthias Ringwald                     eatt_bearer->att_connection.mtu = remote_mtu;
1576be4ca859SMatthias Ringwald                     eatt_bearer->att_connection.max_mtu = remote_mtu;
157720d4358dSMatthias Ringwald                     log_info("L2CAP_EVENT_ECBM_CHANNEL_OPENED - cid 0x%04x mtu %u, status 0x%02x", cid, remote_mtu, status);
157820d4358dSMatthias Ringwald                     break;
157920d4358dSMatthias Ringwald 
158020d4358dSMatthias Ringwald                 case L2CAP_EVENT_ECBM_RECONFIGURED:
158120d4358dSMatthias Ringwald                     break;
158220d4358dSMatthias Ringwald 
1583523bb989SMatthias Ringwald                 case L2CAP_EVENT_CHANNEL_CLOSED:
1584523bb989SMatthias Ringwald                     eatt_bearer = att_server_eatt_bearer_for_cid(l2cap_event_channel_closed_get_local_cid(packet));
1585523bb989SMatthias Ringwald                     btstack_assert(eatt_bearers != NULL);
1586523bb989SMatthias Ringwald 
1587523bb989SMatthias Ringwald                     // TODO: finalize - abort queued writes
1588523bb989SMatthias Ringwald 
158927afb094SMatthias Ringwald                     btstack_linked_list_remove(&att_server_eatt_bearer_active, (btstack_linked_item_t  *) eatt_bearer);
1590523bb989SMatthias Ringwald                     btstack_linked_list_add(&att_server_eatt_bearer_pool, (btstack_linked_item_t  *) eatt_bearer);
1591523bb989SMatthias Ringwald                     break;
159220d4358dSMatthias Ringwald                 default:
159320d4358dSMatthias Ringwald                     break;
159420d4358dSMatthias Ringwald             }
159527afb094SMatthias Ringwald             break;
159627afb094SMatthias Ringwald         default:
159727afb094SMatthias Ringwald             btstack_unreachable();
159827afb094SMatthias Ringwald             break;
159920d4358dSMatthias Ringwald     }
160020d4358dSMatthias Ringwald }
160120d4358dSMatthias Ringwald 
160220d4358dSMatthias Ringwald // create eatt bearers
160320d4358dSMatthias Ringwald uint8_t att_server_eatt_init(uint8_t num_eatt_bearers, uint8_t * storage_buffer, uint16_t storage_size){
160420d4358dSMatthias Ringwald     uint16_t size_for_structs = num_eatt_bearers * sizeof(att_server_eatt_bearer_t);
160520d4358dSMatthias Ringwald     if (storage_size < size_for_structs) {
160620d4358dSMatthias Ringwald         return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED;
160720d4358dSMatthias Ringwald     }
160820d4358dSMatthias Ringwald 
160920d4358dSMatthias Ringwald     // TODO: The minimum ATT_MTU for an Enhanced ATT bearer is 64 octets.
161020d4358dSMatthias Ringwald 
161120d4358dSMatthias Ringwald     memset(storage_buffer, 0, storage_size);
16128d3bb68fSMatthias Ringwald     uint16_t buffer_size_per_bearer = ((storage_size - size_for_structs) / num_eatt_bearers);
16138d3bb68fSMatthias Ringwald     att_server_eatt_receive_buffer_size = buffer_size_per_bearer / 2;
16148d3bb68fSMatthias Ringwald     att_server_eatt_send_buffer_size    = buffer_size_per_bearer / 2;
1615735b0222SMatthias Ringwald     uint8_t * bearer_buffer = &storage_buffer[size_for_structs];
161620d4358dSMatthias Ringwald     uint8_t i;
161720d4358dSMatthias Ringwald     att_server_eatt_bearer_t * eatt_bearer = (att_server_eatt_bearer_t *) storage_buffer;
1618be4ca859SMatthias Ringwald     log_info("%u EATT bearers with receive buffer size %u",
1619be4ca859SMatthias Ringwald              num_eatt_bearers, att_server_eatt_receive_buffer_size);
162020d4358dSMatthias Ringwald     for (i=0;i<num_eatt_bearers;i++){
1621be4ca859SMatthias Ringwald         eatt_bearer->att_connection.con_handle = HCI_CON_HANDLE_INVALID;
1622735b0222SMatthias Ringwald         eatt_bearer->receive_buffer = bearer_buffer;
1623735b0222SMatthias Ringwald         bearer_buffer += att_server_eatt_receive_buffer_size;
16248d3bb68fSMatthias Ringwald         eatt_bearer->send_buffer = bearer_buffer;
16258d3bb68fSMatthias Ringwald         bearer_buffer += att_server_eatt_send_buffer_size;
16266b236cb0SMatthias Ringwald         btstack_linked_list_add(&att_server_eatt_bearer_pool, (btstack_linked_item_t *) eatt_bearer);
162720d4358dSMatthias Ringwald         eatt_bearer++;
162820d4358dSMatthias Ringwald     }
162920d4358dSMatthias Ringwald     // TODO: define minimum EATT MTU
1630989c5498SMatthias Ringwald     l2cap_ecbm_register_service(att_server_eatt_handler, BLUETOOTH_PSM_EATT, 64, 0, false);
163120d4358dSMatthias Ringwald 
163220d4358dSMatthias Ringwald     return 0;
163320d4358dSMatthias Ringwald }
163420d4358dSMatthias Ringwald #endif