xref: /btstack/src/ble/att_server.c (revision 370c8b58f8b53615b2803205fc68e6ee89ef65ac)
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"
57be75fc08SMatthias Ringwald #include "bluetooth_psm.h"
5816ece135SMatthias Ringwald #include "btstack_debug.h"
590e2df43fSMatthias Ringwald #include "btstack_event.h"
603deb3ec6SMatthias Ringwald #include "btstack_memory.h"
610e2df43fSMatthias Ringwald #include "btstack_run_loop.h"
6259c6af15SMatthias Ringwald #include "gap.h"
633deb3ec6SMatthias Ringwald #include "hci.h"
643deb3ec6SMatthias Ringwald #include "hci_dump.h"
653deb3ec6SMatthias Ringwald #include "l2cap.h"
66bf78aac6SMatthias Ringwald #include "btstack_tlv.h"
67d1a1f6a4SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
68d1a1f6a4SMatthias Ringwald #include "ble/sm.h"
6920d4358dSMatthias Ringwald #include "bluetooth_psm.h"
7020d4358dSMatthias Ringwald 
71d1a1f6a4SMatthias Ringwald #endif
72bf78aac6SMatthias Ringwald 
7303f53d81SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT
7403f53d81SMatthias Ringwald #include <stdio.h>
7503f53d81SMatthias Ringwald #endif
7603f53d81SMatthias Ringwald 
776afb9acaSMatthias Ringwald #ifndef NVN_NUM_GATT_SERVER_CCC
786afb9acaSMatthias Ringwald #define NVN_NUM_GATT_SERVER_CCC 20
79bf78aac6SMatthias Ringwald #endif
803deb3ec6SMatthias Ringwald 
81c61037ccSMatthias Ringwald #define ATT_SERVICE_FLAGS_DELAYED_RESPONSE (1<<0u)
82c61037ccSMatthias Ringwald 
83cd45e970SMatthias Ringwald static void att_run_for_context(att_server_t * att_server, att_connection_t * att_connection);
8401ac2008SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle);
855d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle);
8648364364SMatthias Ringwald static void att_server_handle_can_send_now(void);
879a1f55eaSMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server, att_connection_t * att_connection);
88fe3d0cbaSMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server);
8926b42dcfSMatthias Ringwald static void att_server_handle_att_pdu(att_server_t * att_server, att_connection_t * att_connection, uint8_t * packet, uint16_t size);
903deb3ec6SMatthias Ringwald 
913551936eSMatthias Ringwald typedef enum {
9238b893aaSMilanka Ringwald     ATT_SERVER_RUN_PHASE_1_REQUESTS = 0,
933551936eSMatthias Ringwald     ATT_SERVER_RUN_PHASE_2_INDICATIONS,
943551936eSMatthias Ringwald     ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS,
953551936eSMatthias Ringwald } att_server_run_phase_t;
963551936eSMatthias Ringwald 
976a540790SMatthias Ringwald //
986a540790SMatthias Ringwald typedef struct {
996a540790SMatthias Ringwald     uint32_t seq_nr;
1006a540790SMatthias Ringwald     uint16_t att_handle;
1016a540790SMatthias Ringwald     uint8_t  value;
1026a540790SMatthias Ringwald     uint8_t  device_index;
1036a540790SMatthias Ringwald } persistent_ccc_entry_t;
1046a540790SMatthias Ringwald 
10518707219SMatthias Ringwald // global
1061a389cdcSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
107406722e4SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration;
10835d7f2dfSMilanka Ringwald static btstack_packet_handler_t               att_client_packet_handler;
1093d71c7a4SMatthias Ringwald static btstack_linked_list_t                  service_handlers;
1105f141511SMatthias Ringwald static btstack_context_callback_registration_t att_client_waiting_for_can_send_registration;
11118707219SMatthias Ringwald 
1123d71c7a4SMatthias Ringwald static att_read_callback_t                    att_server_client_read_callback;
1133d71c7a4SMatthias Ringwald static att_write_callback_t                   att_server_client_write_callback;
1143d71c7a4SMatthias Ringwald 
1156438547aSMatthias Ringwald // round robin
1166438547aSMatthias Ringwald static hci_con_handle_t att_server_last_can_send_now = HCI_CON_HANDLE_INVALID;
117bf78aac6SMatthias Ringwald 
118c61037ccSMatthias Ringwald static uint8_t att_server_flags;
119c61037ccSMatthias Ringwald 
120c2cbe117SMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
121c2cbe117SMatthias Ringwald typedef struct {
122c2cbe117SMatthias Ringwald     btstack_linked_item_t item;
123c2cbe117SMatthias Ringwald     att_server_t     att_server;
124c2cbe117SMatthias Ringwald     att_connection_t att_connection;
125c2cbe117SMatthias Ringwald     uint8_t * receive_buffer;
126c2cbe117SMatthias Ringwald     uint8_t * send_buffer;
127c2cbe117SMatthias Ringwald } att_server_eatt_bearer_t;
12840f8d8f2SMatthias Ringwald static att_server_eatt_bearer_t * att_server_eatt_bearer_for_con_handle(hci_con_handle_t con_handle);
129c2cbe117SMatthias Ringwald static btstack_linked_list_t att_server_eatt_bearer_pool;
130c2cbe117SMatthias Ringwald static btstack_linked_list_t att_server_eatt_bearer_active;
131c2cbe117SMatthias Ringwald #endif
132c2cbe117SMatthias Ringwald 
13302b78fc8SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
13426567ee7SMatthias 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){
13518707219SMatthias Ringwald     btstack_linked_list_iterator_t it;
13618707219SMatthias Ringwald     hci_connections_get_iterator(&it);
13718707219SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
13826567ee7SMatthias Ringwald         hci_connection_t * hci_connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
13926567ee7SMatthias Ringwald         if (hci_connection->att_server.state == state) {
14026567ee7SMatthias Ringwald             *att_server_out     = &hci_connection->att_server;
14126567ee7SMatthias Ringwald             *att_connection_out = &hci_connection->att_connection;
14226567ee7SMatthias Ringwald             return true;
14318707219SMatthias Ringwald         }
14426567ee7SMatthias Ringwald     }
145c2cbe117SMatthias Ringwald 
146c2cbe117SMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
147c2cbe117SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &att_server_eatt_bearer_active);
148c2cbe117SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
149c2cbe117SMatthias Ringwald         att_server_eatt_bearer_t * eatt_bearer = (att_server_eatt_bearer_t *) btstack_linked_list_iterator_next(&it);
150c2cbe117SMatthias Ringwald         if (eatt_bearer->att_server.state == state) {
151c2cbe117SMatthias Ringwald             *att_server_out     = &eatt_bearer->att_server;
152c2cbe117SMatthias Ringwald             *att_connection_out = &eatt_bearer->att_connection;
153c2cbe117SMatthias Ringwald             return true;
154c2cbe117SMatthias Ringwald         }
155c2cbe117SMatthias Ringwald     }
156c2cbe117SMatthias Ringwald #endif
157c2cbe117SMatthias Ringwald 
15826567ee7SMatthias Ringwald     return false;
15918707219SMatthias Ringwald }
16002b78fc8SMatthias Ringwald #endif
161bb38f057SMatthias Ringwald 
1623f9b77dbSMatthias Ringwald static void att_server_request_can_send_now(att_server_t * att_server, att_connection_t * att_connection ){
16323079b39SMatthias Ringwald     switch (att_server->bearer_type){
16423079b39SMatthias Ringwald         case ATT_BEARER_UNENHANCED_LE:
16523079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
16623079b39SMatthias Ringwald         case ATT_BEARER_UNENHANCED_CLASSIC:
1672954c397SMatthias Ringwald             /* fall through */
1682954c397SMatthias Ringwald #endif
169328bed13SMatthias Ringwald             att_dispatch_server_request_can_send_now_event(att_connection->con_handle);
170328bed13SMatthias Ringwald             break;
1712954c397SMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
1722954c397SMatthias Ringwald         case ATT_BEARER_ENHANCED_LE:
17323079b39SMatthias Ringwald             l2cap_request_can_send_now_event(att_server->l2cap_cid);
17423079b39SMatthias Ringwald             break;
17523079b39SMatthias Ringwald #endif
17623079b39SMatthias Ringwald         default:
17723079b39SMatthias Ringwald             btstack_unreachable();
17823079b39SMatthias Ringwald             break;
17923079b39SMatthias Ringwald     }
180c8c6e9b4SMatthias Ringwald }
181c8c6e9b4SMatthias Ringwald 
18237a8059dSMatthias Ringwald static bool att_server_can_send_packet(att_server_t * att_server, att_connection_t * att_connection){
18323079b39SMatthias Ringwald     switch (att_server->bearer_type) {
18423079b39SMatthias Ringwald         case ATT_BEARER_UNENHANCED_LE:
18523079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
18623079b39SMatthias Ringwald         case ATT_BEARER_UNENHANCED_CLASSIC:
1872954c397SMatthias Ringwald             /* fall through */
1882954c397SMatthias Ringwald #endif
189328bed13SMatthias Ringwald             return att_dispatch_server_can_send_now(att_connection->con_handle);
1902954c397SMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
1912954c397SMatthias Ringwald         case ATT_BEARER_ENHANCED_LE:
1922954c397SMatthias Ringwald             return l2cap_can_send_packet_now(att_server->l2cap_cid);
19323079b39SMatthias Ringwald #endif
19423079b39SMatthias Ringwald         default:
19523079b39SMatthias Ringwald             btstack_unreachable();
19623079b39SMatthias Ringwald             break;
19723079b39SMatthias Ringwald     }
19823079b39SMatthias Ringwald     return false;
199c0f26e1cSMatthias Ringwald }
200c0f26e1cSMatthias Ringwald 
2013deb3ec6SMatthias Ringwald static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){
2025d07396bSMatthias Ringwald     btstack_packet_handler_t packet_handler = att_server_packet_handler_for_handle(attribute_handle);
2035d07396bSMatthias Ringwald     if (!packet_handler) return;
2043deb3ec6SMatthias Ringwald 
2053deb3ec6SMatthias Ringwald     uint8_t event[7];
2063deb3ec6SMatthias Ringwald     int pos = 0;
2075611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE;
2084ea43905SMatthias Ringwald     event[pos++] = sizeof(event) - 2u;
2093deb3ec6SMatthias Ringwald     event[pos++] = status;
210f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, client_handle);
2113deb3ec6SMatthias Ringwald     pos += 2;
212f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, attribute_handle);
213a444112bSMatthias Ringwald     (*packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
2143deb3ec6SMatthias Ringwald }
2153deb3ec6SMatthias Ringwald 
2165d07396bSMatthias Ringwald static void att_emit_event_to_all(const uint8_t * event, uint16_t size){
2175d07396bSMatthias Ringwald     // dispatch to app level handler
2189305033eSMatthias Ringwald     if (att_client_packet_handler != NULL){
2195d07396bSMatthias Ringwald         (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size);
2205d07396bSMatthias Ringwald     }
2213deb3ec6SMatthias Ringwald 
2225d07396bSMatthias Ringwald     // dispatch to service handlers
2235d07396bSMatthias Ringwald     btstack_linked_list_iterator_t it;
2245d07396bSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
2255d07396bSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
2265d07396bSMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
2275d07396bSMatthias Ringwald         if (!handler->packet_handler) continue;
2285d07396bSMatthias Ringwald         (*handler->packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size);
2295d07396bSMatthias Ringwald     }
2305d07396bSMatthias Ringwald }
2315d07396bSMatthias Ringwald 
2325d07396bSMatthias Ringwald static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){
2333deb3ec6SMatthias Ringwald     uint8_t event[6];
2343deb3ec6SMatthias Ringwald     int pos = 0;
2355611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE;
2364ea43905SMatthias Ringwald     event[pos++] = sizeof(event) - 2u;
237fc64f94aSMatthias Ringwald     little_endian_store_16(event, pos, con_handle);
2383deb3ec6SMatthias Ringwald     pos += 2;
239f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, mtu);
2405d07396bSMatthias Ringwald 
2415d07396bSMatthias Ringwald     // also dispatch to GATT Clients
242b12646c5SJakob Krantz     att_dispatch_server_mtu_exchanged(con_handle, mtu);
2435d07396bSMatthias Ringwald 
2445d07396bSMatthias Ringwald     // dispatch to app level handler and service handlers
2455d07396bSMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
2463deb3ec6SMatthias Ringwald }
2473deb3ec6SMatthias Ringwald 
2485f141511SMatthias Ringwald static void att_emit_can_send_now_event(void * context){
2495f141511SMatthias Ringwald     UNUSED(context);
2503c97b242SMatthias Ringwald     if (!att_client_packet_handler) return;
2513c97b242SMatthias Ringwald 
2521b96b007SMatthias Ringwald     uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0};
2531b96b007SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
2541b96b007SMatthias Ringwald }
2551b96b007SMatthias Ringwald 
2566d2214cbSMatthias Ringwald static void att_emit_connected_event(att_server_t * att_server,  att_connection_t * att_connection){
257ff1bec95SMatthias Ringwald     uint8_t event[11];
258ff1bec95SMatthias Ringwald     int pos = 0;
259ff1bec95SMatthias Ringwald     event[pos++] = ATT_EVENT_CONNECTED;
2604ea43905SMatthias Ringwald     event[pos++] = sizeof(event) - 2u;
261ff1bec95SMatthias Ringwald     event[pos++] = att_server->peer_addr_type;
262ff1bec95SMatthias Ringwald     reverse_bd_addr(att_server->peer_address, &event[pos]);
263ff1bec95SMatthias Ringwald     pos += 6;
264b6df12b6SMatthias Ringwald     little_endian_store_16(event, pos, att_connection->con_handle);
265ff1bec95SMatthias Ringwald     pos += 2;
266ff1bec95SMatthias Ringwald 
267ff1bec95SMatthias Ringwald     // dispatch to app level handler and service handlers
268ff1bec95SMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
269ff1bec95SMatthias Ringwald }
270ff1bec95SMatthias Ringwald 
271ff1bec95SMatthias Ringwald 
2726187ad4cSMatthias Ringwald static void att_emit_disconnected_event(uint16_t con_handle){
273ff1bec95SMatthias Ringwald     uint8_t event[4];
274ff1bec95SMatthias Ringwald     int pos = 0;
275ff1bec95SMatthias Ringwald     event[pos++] = ATT_EVENT_DISCONNECTED;
2764ea43905SMatthias Ringwald     event[pos++] = sizeof(event) - 2u;
2776187ad4cSMatthias Ringwald     little_endian_store_16(event, pos, con_handle);
278ff1bec95SMatthias Ringwald     pos += 2;
279ff1bec95SMatthias Ringwald 
280ff1bec95SMatthias Ringwald     // dispatch to app level handler and service handlers
281ff1bec95SMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
282ff1bec95SMatthias Ringwald }
283ff1bec95SMatthias Ringwald 
284ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){
28554178543SMatthias Ringwald     void * context = btstack_run_loop_get_timer_context(ts);
28654178543SMatthias Ringwald     hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
28768591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
28868591e36SMatthias Ringwald     if (!hci_connection) return;
28968591e36SMatthias Ringwald     // @note: after a transaction timeout, no more requests shall be sent over this ATT Bearer
290bce48a26SMatthias Ringwald     // (that's why we don't reset the value_indication_handle)
291b6df12b6SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
29218707219SMatthias Ringwald     uint16_t att_handle = att_server->value_indication_handle;
293388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
29438b893aaSMilanka Ringwald     att_handle_value_indication_notify_client((uint8_t)ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_connection->con_handle, att_handle);
2953deb3ec6SMatthias Ringwald }
2963deb3ec6SMatthias Ringwald 
29752e211deSMatthias Ringwald static void att_server_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2989ec2630cSMatthias Ringwald 
2997dad9ff8SMatthias Ringwald     UNUSED(channel); // ok: there is no channel
3007dad9ff8SMatthias Ringwald     UNUSED(size);    // ok: handling own l2cap events
3013deb3ec6SMatthias Ringwald 
30218707219SMatthias Ringwald     att_server_t * att_server;
303b6df12b6SMatthias Ringwald     att_connection_t * att_connection;
30418707219SMatthias Ringwald     hci_con_handle_t con_handle;
30568591e36SMatthias Ringwald     hci_connection_t * hci_connection;
30618707219SMatthias Ringwald 
3073deb3ec6SMatthias Ringwald     switch (packet_type) {
3083deb3ec6SMatthias Ringwald 
3093deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
3100e2df43fSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
3119d1eff91SMatthias Ringwald                 case HCI_EVENT_META_GAP:
3129d1eff91SMatthias Ringwald                     switch (hci_event_gap_meta_get_subevent_code(packet)) {
3139d1eff91SMatthias Ringwald                         case GAP_SUBEVENT_LE_CONNECTION_COMPLETE:
3149d1eff91SMatthias Ringwald                             con_handle = gap_subevent_le_connection_complete_get_connection_handle(packet);
31568591e36SMatthias Ringwald                             hci_connection = hci_connection_for_handle(con_handle);
31668591e36SMatthias Ringwald                             if (!hci_connection) break;
31768591e36SMatthias Ringwald                             att_server = &hci_connection->att_server;
3183deb3ec6SMatthias Ringwald                         	// store connection info
3199d1eff91SMatthias Ringwald                         	att_server->peer_addr_type = gap_subevent_le_connection_complete_get_peer_address_type(packet);
3209d1eff91SMatthias Ringwald                             gap_subevent_le_connection_complete_get_peer_address(packet, att_server->peer_address);
321388fa7c4SMatthias Ringwald                             att_connection = &hci_connection->att_connection;
322b6df12b6SMatthias Ringwald                             att_connection->con_handle = con_handle;
3233deb3ec6SMatthias Ringwald                             // reset connection properties
32418707219SMatthias Ringwald                             att_server->state = ATT_SERVER_IDLE;
32523079b39SMatthias Ringwald                             att_server->bearer_type = ATT_BEARER_UNENHANCED_LE;
326b6df12b6SMatthias Ringwald                             att_connection->mtu = ATT_DEFAULT_MTU;
327b6df12b6SMatthias Ringwald                             att_connection->max_mtu = l2cap_max_le_mtu();
328b6df12b6SMatthias Ringwald                             if (att_connection->max_mtu > ATT_REQUEST_BUFFER_SIZE){
329b6df12b6SMatthias Ringwald                                 att_connection->max_mtu = ATT_REQUEST_BUFFER_SIZE;
33099c58ad0SMatthias Ringwald                             }
33138b893aaSMilanka Ringwald                             att_connection->encryption_key_size = 0u;
33238b893aaSMilanka Ringwald                             att_connection->authenticated = 0u;
33338b893aaSMilanka Ringwald 		                	att_connection->authorized = 0u;
334b2c1e52cSMatthias Ringwald                             // workaround: identity resolving can already be complete, at least store result
335b2c1e52cSMatthias Ringwald                             att_server->ir_le_device_db_index = sm_le_device_index(con_handle);
3364ab47674SMatthias Ringwald                             att_server->ir_lookup_active = false;
3374ab47674SMatthias Ringwald                             att_server->pairing_active = false;
338ff1bec95SMatthias Ringwald                             // notify all - new
3396d2214cbSMatthias Ringwald                             att_emit_connected_event(att_server, att_connection);
3403deb3ec6SMatthias Ringwald                             break;
3413deb3ec6SMatthias Ringwald                         default:
3423deb3ec6SMatthias Ringwald                             break;
3433deb3ec6SMatthias Ringwald                     }
3443deb3ec6SMatthias Ringwald                     break;
3453deb3ec6SMatthias Ringwald 
34694e69787SMatthias Ringwald                 case HCI_EVENT_LE_META:
34794e69787SMatthias Ringwald                     switch (hci_event_le_meta_get_subevent_code(packet)) {
34894e69787SMatthias Ringwald                         case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
34994e69787SMatthias Ringwald                             // forward LE Connection Complete event to keep backward compatibility
35094e69787SMatthias Ringwald                             // deprecated: please register hci handler with hci_add_event_handler or handle ATT_EVENT_CONNECTED
35194e69787SMatthias Ringwald                             att_emit_event_to_all(packet, size);
35294e69787SMatthias Ringwald                             break;
35394e69787SMatthias Ringwald                         default:
35494e69787SMatthias Ringwald                             break;
35594e69787SMatthias Ringwald                     }
35694e69787SMatthias Ringwald                     break;
35794e69787SMatthias Ringwald 
3583deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_CHANGE:
3598601e696SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_CHANGE_V2:
3603deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE:
3613deb3ec6SMatthias Ringwald                 	// check handle
36218707219SMatthias Ringwald                     con_handle = little_endian_read_16(packet, 3);
36368591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
36468591e36SMatthias Ringwald                     if (!hci_connection) break;
365914988a1SMatthias Ringwald                     if (gap_get_connection_type(con_handle) != GAP_CONNECTION_LE) break;
366914988a1SMatthias Ringwald                     // update security params
36768591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
368388fa7c4SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
369b6df12b6SMatthias Ringwald                     att_connection->encryption_key_size = gap_encryption_key_size(con_handle);
370ff3f1026SMatthias Ringwald                     att_connection->authenticated = gap_authenticated(con_handle) ? 1 : 0;
371f461b3dfSMatthias Ringwald                     att_connection->secure_connection = gap_secure_connection(con_handle) ? 1 : 0;
372914988a1SMatthias Ringwald                     log_info("encrypted key size %u, authenticated %u, secure connection %u",
373b6df12b6SMatthias Ringwald                         att_connection->encryption_key_size, att_connection->authenticated, att_connection->secure_connection);
374914988a1SMatthias Ringwald                     if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){
3754097998aSMatthias Ringwald                         // restore CCC values when encrypted for LE Connections
37622bfc225SMilanka Ringwald                         if (hci_event_encryption_change_get_encryption_enabled(packet) != 0){
3779a1f55eaSMatthias Ringwald                             att_server_persistent_ccc_restore(att_server, att_connection);
37801ac2008SMatthias Ringwald                         }
37901ac2008SMatthias Ringwald                     }
380cd45e970SMatthias Ringwald                     att_run_for_context(att_server, att_connection);
3813deb3ec6SMatthias Ringwald                     break;
3823deb3ec6SMatthias Ringwald 
3833deb3ec6SMatthias Ringwald                 case HCI_EVENT_DISCONNECTION_COMPLETE:
38470a390c7SMatthias Ringwald                     // check handle
38518707219SMatthias Ringwald                     con_handle = hci_event_disconnection_complete_get_connection_handle(packet);
38668591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
38768591e36SMatthias Ringwald                     if (!hci_connection) break;
38868591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
389388fa7c4SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
390b6df12b6SMatthias Ringwald                     att_clear_transaction_queue(att_connection);
391b6df12b6SMatthias Ringwald                     att_connection->con_handle = 0;
3924ab47674SMatthias Ringwald                     att_server->pairing_active = false;
39318707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
39438b893aaSMilanka Ringwald                     if (att_server->value_indication_handle != 0u){
39527328ecbSMatthias Ringwald                         btstack_run_loop_remove_timer(&att_server->value_indication_timer);
396bce48a26SMatthias Ringwald                         uint16_t att_handle = att_server->value_indication_handle;
39738b893aaSMilanka Ringwald                         att_server->value_indication_handle = 0u; // reset error state
39838b893aaSMilanka Ringwald                         att_handle_value_indication_notify_client((uint8_t)ATT_HANDLE_VALUE_INDICATION_DISCONNECT, att_connection->con_handle, att_handle);
399bce48a26SMatthias Ringwald                     }
400ff1bec95SMatthias Ringwald                     // notify all - new
4016187ad4cSMatthias Ringwald                     att_emit_disconnected_event(con_handle);
402ff1bec95SMatthias Ringwald                     // notify all - old
4035d07396bSMatthias Ringwald                     att_emit_event_to_all(packet, size);
4043deb3ec6SMatthias Ringwald                     break;
4053deb3ec6SMatthias Ringwald 
406760ad805SMatthias Ringwald                 // Identity Resolving
4075611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_STARTED:
40818707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_started_get_handle(packet);
40968591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
41068591e36SMatthias Ringwald                     if (!hci_connection) break;
41168591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
4125611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED");
4134ab47674SMatthias Ringwald                     att_server->ir_lookup_active = true;
4143deb3ec6SMatthias Ringwald                     break;
4155611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
416760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
41768591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
41868591e36SMatthias Ringwald                     if (!hci_connection) return;
419acb2a577SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
42068591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
4214ab47674SMatthias Ringwald                     att_server->ir_lookup_active = false;
4221b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index(packet);
4231b7acd0dSMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED");
424cd45e970SMatthias Ringwald                     att_run_for_context(att_server, att_connection);
4253deb3ec6SMatthias Ringwald                     break;
4265611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_FAILED:
42718707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_failed_get_handle(packet);
42868591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
42968591e36SMatthias Ringwald                     if (!hci_connection) break;
430acb2a577SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
43168591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
4325611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED");
4334ab47674SMatthias Ringwald                     att_server->ir_lookup_active = false;
43418707219SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
435cd45e970SMatthias Ringwald                     att_run_for_context(att_server, att_connection);
4363deb3ec6SMatthias Ringwald                     break;
437760ad805SMatthias Ringwald 
438760ad805SMatthias Ringwald                 // Pairing started - delete stored CCC values
439760ad805SMatthias Ringwald                 // - assumes pairing indicates either new device or re-pairing, in both cases there should be no stored CCC values
440760ad805SMatthias Ringwald                 // - assumes that all events have the con handle as the first field
441760ad805SMatthias Ringwald                 case SM_EVENT_JUST_WORKS_REQUEST:
442760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
443760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_INPUT_NUMBER:
444760ad805SMatthias Ringwald                 case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
445760ad805SMatthias Ringwald                     con_handle = sm_event_just_works_request_get_handle(packet);
44668591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
44768591e36SMatthias Ringwald                     if (!hci_connection) break;
44868591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
449760ad805SMatthias Ringwald                     log_info("SM Pairing started");
4504ab47674SMatthias Ringwald                     att_server->pairing_active = true;
451760ad805SMatthias Ringwald                     if (att_server->ir_le_device_db_index < 0) break;
45201c0691bSMatthias Ringwald                     att_server_persistent_ccc_clear(att_server);
453760ad805SMatthias Ringwald                     // index not valid anymore
454760ad805SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
455760ad805SMatthias Ringwald                     break;
456760ad805SMatthias Ringwald 
4571b7acd0dSMatthias Ringwald                 // Bonding completed
458760ad805SMatthias Ringwald                 case SM_EVENT_IDENTITY_CREATED:
459760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
46068591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
46168591e36SMatthias Ringwald                     if (!hci_connection) return;
462acb2a577SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
46368591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
4644ab47674SMatthias Ringwald                     att_server->pairing_active = false;
4651b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_created_get_index(packet);
466cd45e970SMatthias Ringwald                     att_run_for_context(att_server, att_connection);
4671b7acd0dSMatthias Ringwald                     break;
4681b7acd0dSMatthias Ringwald 
4691b7acd0dSMatthias Ringwald                 // Pairing complete (with/without bonding=storing of pairing information)
4701b7acd0dSMatthias Ringwald                 case SM_EVENT_PAIRING_COMPLETE:
4711b7acd0dSMatthias Ringwald                     con_handle = sm_event_pairing_complete_get_handle(packet);
47268591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
47368591e36SMatthias Ringwald                     if (!hci_connection) return;
474acb2a577SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
47568591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
4764ab47674SMatthias Ringwald                     att_server->pairing_active = false;
477cd45e970SMatthias Ringwald                     att_run_for_context(att_server, att_connection);
478760ad805SMatthias Ringwald                     break;
479760ad805SMatthias Ringwald 
480760ad805SMatthias Ringwald                 // Authorization
4815611a760SMatthias Ringwald                 case SM_EVENT_AUTHORIZATION_RESULT: {
48218707219SMatthias Ringwald                     con_handle = sm_event_authorization_result_get_handle(packet);
48368591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
48468591e36SMatthias Ringwald                     if (!hci_connection) break;
485388fa7c4SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
486acb2a577SMatthias Ringwald                     att_server = &hci_connection->att_server;
487b6df12b6SMatthias Ringwald                     att_connection->authorized = sm_event_authorization_result_get_authorization_result(packet);
4883f9b77dbSMatthias Ringwald                     att_server_request_can_send_now(att_server, att_connection);
4893deb3ec6SMatthias Ringwald                 	break;
4903deb3ec6SMatthias Ringwald                 }
4913deb3ec6SMatthias Ringwald                 default:
4923deb3ec6SMatthias Ringwald                     break;
4933deb3ec6SMatthias Ringwald             }
49465b44ffdSMatthias Ringwald             break;
49565b44ffdSMatthias Ringwald         default:
49665b44ffdSMatthias Ringwald             break;
4973deb3ec6SMatthias Ringwald     }
4983deb3ec6SMatthias Ringwald }
4993deb3ec6SMatthias Ringwald 
5000f70c52eSMatthias Ringwald static uint8_t
5010f70c52eSMatthias Ringwald att_server_send_prepared(const att_server_t *att_server, const att_connection_t *att_connection, uint8_t *buffer,
5020f70c52eSMatthias Ringwald                          uint16_t size) {
5033548b7cbSDirk Helbig     UNUSED(buffer);
504d415948cSMatthias Ringwald     uint8_t status = ERROR_CODE_SUCCESS;
505d415948cSMatthias Ringwald     switch (att_server->bearer_type) {
506d415948cSMatthias Ringwald         case ATT_BEARER_UNENHANCED_LE:
507d415948cSMatthias Ringwald             status = l2cap_send_prepared_connectionless(att_connection->con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
50840f8d8f2SMatthias Ringwald             break;
509d415948cSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
510d415948cSMatthias Ringwald         case ATT_BEARER_UNENHANCED_CLASSIC:
511d415948cSMatthias Ringwald             status = l2cap_send_prepared(att_server->l2cap_cid, size);
512d415948cSMatthias Ringwald             break;
513d415948cSMatthias Ringwald #endif
5140f70c52eSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
5150f70c52eSMatthias Ringwald         case ATT_BEARER_ENHANCED_LE:
5160f70c52eSMatthias Ringwald             btstack_assert(buffer != NULL);
5170f70c52eSMatthias Ringwald             status = l2cap_send(att_server->l2cap_cid, buffer, size);
5180f70c52eSMatthias Ringwald             break;
5190f70c52eSMatthias Ringwald #endif
520d415948cSMatthias Ringwald         default:
521d415948cSMatthias Ringwald             btstack_unreachable();
522d415948cSMatthias Ringwald             break;
523d415948cSMatthias Ringwald     }
524d415948cSMatthias Ringwald     return status;
525d415948cSMatthias Ringwald }
526d415948cSMatthias Ringwald 
5277a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
5283deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
52926567ee7SMatthias Ringwald     att_server_t * att_server = NULL;
53026567ee7SMatthias Ringwald     att_connection_t * att_connection = NULL;
53126567ee7SMatthias Ringwald     bool found = att_server_connections_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION, &att_server, &att_connection);
5323deb3ec6SMatthias Ringwald 
533c2cbe117SMatthias Ringwald     if (found == false){
534c2cbe117SMatthias Ringwald         return;
535c2cbe117SMatthias Ringwald     }
536c2cbe117SMatthias Ringwald 
5373deb3ec6SMatthias Ringwald     uint8_t hash_flipped[8];
5389c80e4ccSMatthias Ringwald     reverse_64(hash, hash_flipped);
53926567ee7SMatthias Ringwald     if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8) != 0){
5403deb3ec6SMatthias Ringwald         log_info("ATT Signed Write, invalid signature");
54103f53d81SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT
54203f53d81SMatthias Ringwald         printf("ATT Signed Write, invalid signature\n");
54303f53d81SMatthias Ringwald #endif
54418707219SMatthias Ringwald         att_server->state = ATT_SERVER_IDLE;
5453deb3ec6SMatthias Ringwald         return;
5463deb3ec6SMatthias Ringwald     }
5473deb3ec6SMatthias Ringwald     log_info("ATT Signed Write, valid signature");
54803f53d81SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT
54903f53d81SMatthias Ringwald     printf("ATT Signed Write, valid signature\n");
55003f53d81SMatthias Ringwald #endif
5513deb3ec6SMatthias Ringwald 
5523deb3ec6SMatthias Ringwald     // update sequence number
55318707219SMatthias Ringwald     uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
55418707219SMatthias Ringwald     le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1);
55518707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
5563f9b77dbSMatthias Ringwald     att_server_request_can_send_now(att_server, att_connection);
5573deb3ec6SMatthias Ringwald }
5587a766ebfSMatthias Ringwald #endif
55988a48dd8SMatthias Ringwald 
560c5867c46SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
561c5867c46SMatthias Ringwald static void att_server_handle_response_pending(att_server_t *att_server, const att_connection_t *att_connection,
562c5867c46SMatthias Ringwald                                                const uint8_t *eatt_buffer,
563c5867c46SMatthias Ringwald                                                uint16_t att_response_size) {
564c5867c46SMatthias Ringwald     // update state
565c5867c46SMatthias Ringwald     att_server->state = ATT_SERVER_RESPONSE_PENDING;
566c5867c46SMatthias Ringwald 
567c5867c46SMatthias Ringwald     // callback with handle ATT_READ_RESPONSE_PENDING for reads
568c5867c46SMatthias Ringwald     if (att_response_size == ATT_READ_RESPONSE_PENDING){
569c5867c46SMatthias Ringwald         // notify services that returned response pending
570c5867c46SMatthias Ringwald         btstack_linked_list_iterator_t it;
571c5867c46SMatthias Ringwald         btstack_linked_list_iterator_init(&it, &service_handlers);
572c5867c46SMatthias Ringwald         while (btstack_linked_list_iterator_has_next(&it)) {
573c5867c46SMatthias Ringwald             att_service_handler_t *handler = (att_service_handler_t *) btstack_linked_list_iterator_next(&it);
574c5867c46SMatthias Ringwald             if ((handler->flags & ATT_SERVICE_FLAGS_DELAYED_RESPONSE) != 0){
575c5867c46SMatthias Ringwald                 handler->flags &= ~ATT_SERVICE_FLAGS_DELAYED_RESPONSE;
576c5867c46SMatthias Ringwald                 handler->read_callback(att_connection->con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0);
577c5867c46SMatthias Ringwald             }
578c5867c46SMatthias Ringwald         }
579c5867c46SMatthias Ringwald         // notify main read callback if it returned response pending
580c5867c46SMatthias Ringwald         if ((att_server_flags & ATT_SERVICE_FLAGS_DELAYED_RESPONSE) != 0){
581c5867c46SMatthias Ringwald             // flag was set by read callback
582c5867c46SMatthias Ringwald             btstack_assert(att_server_client_read_callback != NULL);
583c5867c46SMatthias Ringwald             (*att_server_client_read_callback)(att_connection->con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0);
584c5867c46SMatthias Ringwald         }
585c5867c46SMatthias Ringwald     }
58653a869daSMatthias Ringwald 
58753a869daSMatthias Ringwald     // free reserved buffer - might trigger next read/write
58853a869daSMatthias Ringwald     if (eatt_buffer == NULL){
58953a869daSMatthias Ringwald         l2cap_release_packet_buffer();
59053a869daSMatthias Ringwald     }
591c5867c46SMatthias Ringwald }
592c5867c46SMatthias Ringwald #endif
593c5867c46SMatthias Ringwald 
59418707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED
595b06f2579SMatthias Ringwald // pre: can send now
5969988bc8aSMatthias Ringwald // uses l2cap outgoing buffer if no eatt_buffer provided
597b06f2579SMatthias Ringwald // returns: 1 if packet was sent
5989988bc8aSMatthias Ringwald static int
5999988bc8aSMatthias Ringwald att_server_process_validated_request(att_server_t *att_server, att_connection_t *att_connection, uint8_t *eatt_buffer) {
600b06f2579SMatthias Ringwald 
6019988bc8aSMatthias Ringwald     uint8_t * att_response_buffer;
6029988bc8aSMatthias Ringwald     if (eatt_buffer != NULL){
6039988bc8aSMatthias Ringwald         att_response_buffer = eatt_buffer;
6049988bc8aSMatthias Ringwald     } else {
605b06f2579SMatthias Ringwald         l2cap_reserve_packet_buffer();
6069988bc8aSMatthias Ringwald         att_response_buffer = l2cap_get_outgoing_buffer();
6079988bc8aSMatthias Ringwald     }
6089988bc8aSMatthias Ringwald 
6097a293e22SMatthias Ringwald     uint16_t  att_response_size = 0;
6107a293e22SMatthias Ringwald     // Send Error Response for MTU Request over connection-oriented channel
6117a293e22SMatthias Ringwald     if ((att_server->bearer_type != ATT_BEARER_UNENHANCED_LE) && (att_server->request_buffer[0] == ATT_EXCHANGE_MTU_REQUEST)){
6127a293e22SMatthias Ringwald         att_response_size = 5;
6137a293e22SMatthias Ringwald         att_response_buffer[0] = ATT_ERROR_RESPONSE;
6147a293e22SMatthias Ringwald         att_response_buffer[1] = ATT_EXCHANGE_MTU_REQUEST;
6157a293e22SMatthias Ringwald         att_response_buffer[2] = 0;
6167a293e22SMatthias Ringwald         att_response_buffer[3] = 0;
6177a293e22SMatthias Ringwald         att_response_buffer[4] = ATT_ERROR_REQUEST_NOT_SUPPORTED;
6187a293e22SMatthias Ringwald     } else {
6197a293e22SMatthias Ringwald         att_response_size = att_handle_request(att_connection, att_server->request_buffer, att_server->request_size, att_response_buffer);
6207a293e22SMatthias Ringwald     }
621b06f2579SMatthias Ringwald 
622beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
623c1ab6cc1SMatthias Ringwald     if ((att_response_size == ATT_READ_RESPONSE_PENDING) || (att_response_size == ATT_INTERNAL_WRITE_RESPONSE_PENDING)){
624c5867c46SMatthias Ringwald         att_server_handle_response_pending(att_server, att_connection, eatt_buffer, att_response_size);
625e8e7403eSMatthias Ringwald         return 0;
626e404a688SMatthias Ringwald     }
627e404a688SMatthias Ringwald #endif
628e404a688SMatthias Ringwald 
629b06f2579SMatthias Ringwald     // intercept "insufficient authorization" for authenticated connections to allow for user authorization
6304ea43905SMatthias Ringwald     if ((att_response_size     >= 4u)
631b06f2579SMatthias Ringwald     && (att_response_buffer[0] == ATT_ERROR_RESPONSE)
632b06f2579SMatthias Ringwald     && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION)
63338b893aaSMilanka Ringwald     && (att_connection->authenticated != 0u)){
634b06f2579SMatthias Ringwald 
635b6df12b6SMatthias Ringwald         switch (gap_authorization_state(att_connection->con_handle)){
636b06f2579SMatthias Ringwald             case AUTHORIZATION_UNKNOWN:
6379988bc8aSMatthias Ringwald                 if (eatt_buffer == NULL){
638b06f2579SMatthias Ringwald                     l2cap_release_packet_buffer();
6399988bc8aSMatthias Ringwald                 }
640b6df12b6SMatthias Ringwald                 sm_request_pairing(att_connection->con_handle);
641b06f2579SMatthias Ringwald                 return 0;
642b06f2579SMatthias Ringwald             case AUTHORIZATION_PENDING:
6439988bc8aSMatthias Ringwald                 if (eatt_buffer == NULL){
644b06f2579SMatthias Ringwald                     l2cap_release_packet_buffer();
6459988bc8aSMatthias Ringwald                 }
646b06f2579SMatthias Ringwald                 return 0;
647b06f2579SMatthias Ringwald             default:
648b06f2579SMatthias Ringwald                 break;
649b06f2579SMatthias Ringwald         }
650b06f2579SMatthias Ringwald     }
651b06f2579SMatthias Ringwald 
65218707219SMatthias Ringwald     att_server->state = ATT_SERVER_IDLE;
6534ea43905SMatthias Ringwald     if (att_response_size == 0u) {
6549988bc8aSMatthias Ringwald         if (eatt_buffer == NULL){
655b06f2579SMatthias Ringwald             l2cap_release_packet_buffer();
6569988bc8aSMatthias Ringwald         }
657b06f2579SMatthias Ringwald         return 0;
658b06f2579SMatthias Ringwald     }
659b06f2579SMatthias Ringwald 
6609988bc8aSMatthias Ringwald     (void) att_server_send_prepared(att_server, att_connection, eatt_buffer, att_response_size);
661b06f2579SMatthias Ringwald 
662b06f2579SMatthias Ringwald     // notify client about MTU exchange result
663b06f2579SMatthias Ringwald     if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){
664b6df12b6SMatthias Ringwald         att_emit_mtu_event(att_connection->con_handle, att_connection->mtu);
665b06f2579SMatthias Ringwald     }
666b06f2579SMatthias Ringwald     return 1;
667b06f2579SMatthias Ringwald }
668b06f2579SMatthias Ringwald 
669beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
6709816429dSMatthias Ringwald uint8_t att_server_response_ready(hci_con_handle_t con_handle){
67168591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
67268591e36SMatthias Ringwald     if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
67368591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
6743f9b77dbSMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
675beb20288SMatthias Ringwald     if (att_server->state != ATT_SERVER_RESPONSE_PENDING)   return ERROR_CODE_COMMAND_DISALLOWED;
676e404a688SMatthias Ringwald 
677e404a688SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
6783f9b77dbSMatthias Ringwald     att_server_request_can_send_now(att_server, att_connection);
679e404a688SMatthias Ringwald     return ERROR_CODE_SUCCESS;
680e404a688SMatthias Ringwald }
681e404a688SMatthias Ringwald #endif
682e404a688SMatthias Ringwald 
683cd45e970SMatthias Ringwald static void att_run_for_context(att_server_t * att_server, att_connection_t * att_connection){
68418707219SMatthias Ringwald     switch (att_server->state){
6853deb3ec6SMatthias Ringwald         case ATT_SERVER_REQUEST_RECEIVED:
68623079b39SMatthias Ringwald             switch (att_server->bearer_type){
68723079b39SMatthias Ringwald                 case ATT_BEARER_UNENHANCED_LE:
6880234fbbcSMatthias Ringwald                     // wait until re-encryption as central is complete
68923079b39SMatthias Ringwald                     if (gap_reconnect_security_setup_active(att_connection->con_handle)) {
69023079b39SMatthias Ringwald                         return;
69123079b39SMatthias Ringwald                     };
69223079b39SMatthias Ringwald                     break;
6932954c397SMatthias Ringwald #if defined(ENABLE_GATT_OVER_CLASSIC) || defined (ENABLE_GATT_OVER_EATT)
69423079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
69523079b39SMatthias Ringwald                 case ATT_BEARER_UNENHANCED_CLASSIC:
6962954c397SMatthias Ringwald                     /* fall through */
6972954c397SMatthias Ringwald #endif
6982954c397SMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
6992954c397SMatthias Ringwald                 case ATT_BEARER_ENHANCED_LE:
7002954c397SMatthias Ringwald #endif
70123079b39SMatthias Ringwald                     // ok
70223079b39SMatthias Ringwald                     break;
70323079b39SMatthias Ringwald #endif
70423079b39SMatthias Ringwald                 default:
70523079b39SMatthias Ringwald                     btstack_unreachable();
70623079b39SMatthias Ringwald                     break;
70748364364SMatthias Ringwald             }
7080234fbbcSMatthias Ringwald 
709760ad805SMatthias Ringwald             // wait until pairing is complete
710760ad805SMatthias Ringwald             if (att_server->pairing_active) break;
711760ad805SMatthias Ringwald 
7127a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
71318707219SMatthias Ringwald             if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){
7143deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write!");
7153deb3ec6SMatthias Ringwald                 if (!sm_cmac_ready()) {
7163deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, sm_cmac engine not ready. Abort");
71718707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
7183deb3ec6SMatthias Ringwald                     return;
7193deb3ec6SMatthias Ringwald                 }
72018707219SMatthias Ringwald                 if (att_server->request_size < (3 + 12)) {
7213deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, request to short. Abort.");
72218707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
7233deb3ec6SMatthias Ringwald                     return;
7243deb3ec6SMatthias Ringwald                 }
72518707219SMatthias Ringwald                 if (att_server->ir_lookup_active){
7263deb3ec6SMatthias Ringwald                     return;
7273deb3ec6SMatthias Ringwald                 }
72818707219SMatthias Ringwald                 if (att_server->ir_le_device_db_index < 0){
7293deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, CSRK not available");
73018707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
7313deb3ec6SMatthias Ringwald                     return;
7323deb3ec6SMatthias Ringwald                 }
7333deb3ec6SMatthias Ringwald 
7343deb3ec6SMatthias Ringwald                 // check counter
73518707219SMatthias Ringwald                 uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
73618707219SMatthias Ringwald                 uint32_t counter_db     = le_device_db_remote_counter_get(att_server->ir_le_device_db_index);
7373deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet);
7383deb3ec6SMatthias Ringwald                 if (counter_packet < counter_db){
7393deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, db reports higher counter, abort");
74018707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
7413deb3ec6SMatthias Ringwald                     return;
7423deb3ec6SMatthias Ringwald                 }
7433deb3ec6SMatthias Ringwald 
7443deb3ec6SMatthias Ringwald                 // signature is { sequence counter, secure hash }
7453deb3ec6SMatthias Ringwald                 sm_key_t csrk;
74618707219SMatthias Ringwald                 le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk);
74718707219SMatthias Ringwald                 att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION;
7483deb3ec6SMatthias Ringwald                 log_info("Orig Signature: ");
74918707219SMatthias Ringwald                 log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8);
75018707219SMatthias Ringwald                 uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1);
75118707219SMatthias 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);
7523deb3ec6SMatthias Ringwald                 return;
7533deb3ec6SMatthias Ringwald             }
7547a766ebfSMatthias Ringwald #endif
755b06f2579SMatthias Ringwald             // move on
75618707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
7573f9b77dbSMatthias Ringwald             att_server_request_can_send_now(att_server, att_connection);
758b06f2579SMatthias Ringwald             break;
75988a48dd8SMatthias Ringwald 
7603deb3ec6SMatthias Ringwald         default:
7613deb3ec6SMatthias Ringwald             break;
7623deb3ec6SMatthias Ringwald     }
7633deb3ec6SMatthias Ringwald }
7643deb3ec6SMatthias Ringwald 
7651979f09cSMatthias Ringwald static bool att_server_data_ready_for_phase(att_server_t * att_server,  att_server_run_phase_t phase){
76690f03c80SMatthias Ringwald     switch (phase){
76790f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
76890f03c80SMatthias Ringwald             return att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
76990f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
77038b893aaSMilanka Ringwald              return (!btstack_linked_list_empty(&att_server->indication_requests) && (att_server->value_indication_handle == 0u));
77190f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
77290f03c80SMatthias Ringwald             return (!btstack_linked_list_empty(&att_server->notification_requests));
7737bbeb3adSMilanka Ringwald         default:
7747bbeb3adSMilanka Ringwald             btstack_assert(false);
7751979f09cSMatthias Ringwald             return false;
77690f03c80SMatthias Ringwald     }
7777bbeb3adSMilanka Ringwald }
77890f03c80SMatthias Ringwald 
7799d6c0193SMatthias 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){
78090f03c80SMatthias Ringwald     btstack_context_callback_registration_t * client;
78190f03c80SMatthias Ringwald     switch (phase){
78290f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
7830f70c52eSMatthias Ringwald             att_server_process_validated_request(att_server, att_connection, NULL);
78490f03c80SMatthias Ringwald             break;
78590f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
78690f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->indication_requests;
78790f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->indication_requests, (btstack_linked_item_t *) client);
78890f03c80SMatthias Ringwald             client->callback(client->context);
78990f03c80SMatthias Ringwald             break;
79090f03c80SMatthias Ringwald        case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
79190f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->notification_requests;
79290f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->notification_requests, (btstack_linked_item_t *) client);
79390f03c80SMatthias Ringwald             client->callback(client->context);
79490f03c80SMatthias Ringwald             break;
7957bbeb3adSMilanka Ringwald         default:
7967bbeb3adSMilanka Ringwald             btstack_assert(false);
7977bbeb3adSMilanka Ringwald             break;
79890f03c80SMatthias Ringwald     }
79990f03c80SMatthias Ringwald }
80090f03c80SMatthias Ringwald 
8017eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){
802b06f2579SMatthias Ringwald 
8036438547aSMatthias Ringwald     hci_con_handle_t last_send_con_handle = HCI_CON_HANDLE_INVALID;
80468591e36SMatthias Ringwald     hci_connection_t * request_hci_connection   = NULL;
8051979f09cSMatthias Ringwald     bool can_send_now = true;
80638b893aaSMilanka Ringwald     uint8_t phase_index;
8076438547aSMatthias Ringwald 
8084395a000SMatthias Ringwald     for (phase_index = ATT_SERVER_RUN_PHASE_1_REQUESTS; phase_index <= ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS; phase_index++){
8094395a000SMatthias Ringwald         att_server_run_phase_t phase = (att_server_run_phase_t) phase_index;
8106438547aSMatthias Ringwald         hci_con_handle_t skip_connections_until = att_server_last_can_send_now;
811ff3cc4a5SMatthias Ringwald         while (true){
8126438547aSMatthias Ringwald             btstack_linked_list_iterator_t it;
81318707219SMatthias Ringwald             hci_connections_get_iterator(&it);
81418707219SMatthias Ringwald             while(btstack_linked_list_iterator_has_next(&it)){
81518707219SMatthias Ringwald                 hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
81618707219SMatthias Ringwald                 att_server_t * att_server = &connection->att_server;
817388fa7c4SMatthias Ringwald                 att_connection_t * att_connection = &connection->att_connection;
8186438547aSMatthias Ringwald 
8191979f09cSMatthias Ringwald                 bool data_ready = att_server_data_ready_for_phase(att_server, phase);
8206438547aSMatthias Ringwald 
821b6df12b6SMatthias 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);
8226438547aSMatthias Ringwald 
8236438547aSMatthias Ringwald                 // skip until last sender found (which is also skipped)
8246438547aSMatthias Ringwald                 if (skip_connections_until != HCI_CON_HANDLE_INVALID){
82568591e36SMatthias Ringwald                     if (data_ready && (request_hci_connection == NULL)){
82668591e36SMatthias Ringwald                         request_hci_connection = connection;
8276438547aSMatthias Ringwald                     }
828b6df12b6SMatthias Ringwald                     if (skip_connections_until == att_connection->con_handle){
8296438547aSMatthias Ringwald                         skip_connections_until = HCI_CON_HANDLE_INVALID;
8306438547aSMatthias Ringwald                     }
8316438547aSMatthias Ringwald                     continue;
8326438547aSMatthias Ringwald                 };
8336438547aSMatthias Ringwald 
83490f03c80SMatthias Ringwald                 if (data_ready){
835969a5bbaSMatthias Ringwald                     if (can_send_now){
8369d6c0193SMatthias Ringwald                         att_server_trigger_send_for_phase(att_server, att_connection, phase);
837b6df12b6SMatthias Ringwald                         last_send_con_handle = att_connection->con_handle;
83837a8059dSMatthias Ringwald                         can_send_now = att_server_can_send_packet(att_server, att_connection);
83990f03c80SMatthias Ringwald                         data_ready = att_server_data_ready_for_phase(att_server, phase);
84068591e36SMatthias Ringwald                         if (data_ready && (request_hci_connection == NULL)){
84168591e36SMatthias Ringwald                             request_hci_connection = connection;
842cbbb12d9SMatthias Ringwald                         }
843cbbb12d9SMatthias Ringwald                     } else {
84468591e36SMatthias Ringwald                         request_hci_connection = connection;
845f29a88d8SMatthias Ringwald                         break;
846cbbb12d9SMatthias Ringwald                     }
847cbbb12d9SMatthias Ringwald                 }
8483deb3ec6SMatthias Ringwald             }
8493deb3ec6SMatthias Ringwald 
8506438547aSMatthias Ringwald             // stop skipping (handles disconnect by last send connection)
8516438547aSMatthias Ringwald             skip_connections_until = HCI_CON_HANDLE_INVALID;
8526438547aSMatthias Ringwald 
8533551936eSMatthias Ringwald             // Exit loop, if we cannot send
854969a5bbaSMatthias Ringwald             if (!can_send_now) break;
855969a5bbaSMatthias Ringwald 
856969a5bbaSMatthias Ringwald             // Exit loop, if we can send but there are also no further request
85768591e36SMatthias Ringwald             if (request_hci_connection == NULL) break;
858969a5bbaSMatthias Ringwald 
859969a5bbaSMatthias Ringwald             // Finally, if we still can send and there are requests, just try again
86068591e36SMatthias Ringwald             request_hci_connection = NULL;
861969a5bbaSMatthias Ringwald         }
8626438547aSMatthias Ringwald         // update last send con handle for round robin
8636438547aSMatthias Ringwald         if (last_send_con_handle != HCI_CON_HANDLE_INVALID){
8646438547aSMatthias Ringwald             att_server_last_can_send_now = last_send_con_handle;
8656438547aSMatthias Ringwald         }
8663551936eSMatthias Ringwald     }
867969a5bbaSMatthias Ringwald 
86868591e36SMatthias Ringwald     if (request_hci_connection == NULL) return;
8693f9b77dbSMatthias Ringwald 
8703f9b77dbSMatthias Ringwald     att_server_t * att_server = &request_hci_connection->att_server;
8713f9b77dbSMatthias Ringwald     att_connection_t * att_connection = &request_hci_connection->att_connection;
8723f9b77dbSMatthias Ringwald     att_server_request_can_send_now(att_server, att_connection);
873969a5bbaSMatthias Ringwald }
874969a5bbaSMatthias Ringwald 
87526b42dcfSMatthias Ringwald static void att_server_handle_att_pdu(att_server_t * att_server, att_connection_t * att_connection, uint8_t * packet, uint16_t size){
87618707219SMatthias Ringwald 
877838bd521SMatthias Ringwald     uint8_t opcode  = packet[0u];
87838b893aaSMilanka Ringwald     uint8_t method  = opcode & 0x03fu;
879ed68ae9dSMilanka Ringwald     bool invalid = method > ATT_MULTIPLE_HANDLE_VALUE_NTF;
88038b893aaSMilanka Ringwald     bool command = (opcode & 0x40u) != 0u;
881838bd521SMatthias Ringwald 
882838bd521SMatthias Ringwald     // ignore invalid commands
883838bd521SMatthias Ringwald     if (invalid && command){
884838bd521SMatthias Ringwald         return;
885838bd521SMatthias Ringwald     }
886838bd521SMatthias Ringwald 
8873deb3ec6SMatthias Ringwald     // handle value indication confirms
88838b893aaSMilanka Ringwald     if ((opcode == ATT_HANDLE_VALUE_CONFIRMATION) && (att_server->value_indication_handle != 0u)){
88918707219SMatthias Ringwald         btstack_run_loop_remove_timer(&att_server->value_indication_timer);
89018707219SMatthias Ringwald         uint16_t att_handle = att_server->value_indication_handle;
89138b893aaSMilanka Ringwald         att_server->value_indication_handle = 0u;
89238b893aaSMilanka Ringwald         att_handle_value_indication_notify_client(0u, att_connection->con_handle, att_handle);
8933f9b77dbSMatthias Ringwald         att_server_request_can_send_now(att_server, att_connection);
8943deb3ec6SMatthias Ringwald         return;
8953deb3ec6SMatthias Ringwald     }
8963deb3ec6SMatthias Ringwald 
8976428eb5aSMatthias Ringwald     // directly process command
8986428eb5aSMatthias Ringwald     // note: signed write cannot be handled directly as authentication needs to be verified
899838bd521SMatthias Ringwald     if (opcode == ATT_WRITE_COMMAND){
900b6df12b6SMatthias Ringwald         att_handle_request(att_connection, packet, size, NULL);
9016428eb5aSMatthias Ringwald         return;
9026428eb5aSMatthias Ringwald     }
9036428eb5aSMatthias Ringwald 
9043deb3ec6SMatthias Ringwald     // check size
90518707219SMatthias Ringwald     if (size > sizeof(att_server->request_buffer)) {
906e0463bdcSMatthias 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));
9076428eb5aSMatthias Ringwald         return;
9086428eb5aSMatthias Ringwald     }
9093deb3ec6SMatthias Ringwald 
910e0463bdcSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
911e0463bdcSMatthias Ringwald     // abort signed write validation if a new request comes in (but finish previous signed write if possible)
912e0463bdcSMatthias Ringwald     if (att_server->state == ATT_SERVER_W4_SIGNED_WRITE_VALIDATION){
913e0463bdcSMatthias Ringwald         if (packet[0] == ATT_SIGNED_WRITE_COMMAND){
914e0463bdcSMatthias Ringwald             log_info("skip new signed write request as previous is in validation");
915e0463bdcSMatthias Ringwald             return;
916e0463bdcSMatthias Ringwald         } else {
917e0463bdcSMatthias Ringwald             log_info("abort signed write validation to process new request");
918e0463bdcSMatthias Ringwald             att_server->state = ATT_SERVER_IDLE;
919e0463bdcSMatthias Ringwald         }
920e0463bdcSMatthias Ringwald     }
921e0463bdcSMatthias Ringwald #endif
9223deb3ec6SMatthias Ringwald     // last request still in processing?
92318707219SMatthias Ringwald     if (att_server->state != ATT_SERVER_IDLE){
924e0463bdcSMatthias Ringwald         log_info("skip att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state);
9256428eb5aSMatthias Ringwald         return;
9266428eb5aSMatthias Ringwald     }
9273deb3ec6SMatthias Ringwald 
9283deb3ec6SMatthias Ringwald     // store request
92918707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED;
93018707219SMatthias Ringwald     att_server->request_size = size;
9316535961aSMatthias Ringwald     (void)memcpy(att_server->request_buffer, packet, size);
9323deb3ec6SMatthias Ringwald 
933cd45e970SMatthias Ringwald     att_run_for_context(att_server, att_connection);
93441772f37SMatthias Ringwald }
93541772f37SMatthias Ringwald 
93652e211deSMatthias Ringwald static void att_server_dispatch_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
93768591e36SMatthias Ringwald     hci_connection_t * hci_connection;
938b6df12b6SMatthias Ringwald     att_connection_t * att_connection;
93926b42dcfSMatthias Ringwald     att_server_t     * att_server;
94052e211deSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
94152e211deSMatthias Ringwald     hci_con_handle_t con_handle;
94252e211deSMatthias Ringwald     bd_addr_t        address;
94352e211deSMatthias Ringwald     btstack_linked_list_iterator_t it;
94452e211deSMatthias Ringwald #endif
94541772f37SMatthias Ringwald 
94641772f37SMatthias Ringwald     switch (packet_type){
94741772f37SMatthias Ringwald         case HCI_EVENT_PACKET:
94841772f37SMatthias Ringwald             switch (packet[0]){
94952e211deSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
95052e211deSMatthias Ringwald                 case L2CAP_EVENT_CHANNEL_OPENED:
95152e211deSMatthias Ringwald                     con_handle = l2cap_event_channel_opened_get_handle(packet);
95252e211deSMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
95352e211deSMatthias Ringwald                     if (!hci_connection) break;
95452e211deSMatthias Ringwald                     att_server = &hci_connection->att_server;
95552e211deSMatthias Ringwald                     // store connection info
95652e211deSMatthias Ringwald                     att_server->bearer_type = ATT_BEARER_UNENHANCED_CLASSIC;
95752e211deSMatthias Ringwald                     att_server->peer_addr_type = BD_ADDR_TYPE_ACL;
95852e211deSMatthias Ringwald                     l2cap_event_channel_opened_get_address(packet, att_server->peer_address);
95952e211deSMatthias Ringwald                     att_connection = &hci_connection->att_connection;
96052e211deSMatthias Ringwald                     att_connection->con_handle = con_handle;
96152e211deSMatthias Ringwald                     // reset connection properties
96252e211deSMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
96352e211deSMatthias Ringwald                     att_connection->mtu = l2cap_event_channel_opened_get_remote_mtu(packet);
96452e211deSMatthias Ringwald                     att_connection->max_mtu = l2cap_max_mtu();
96552e211deSMatthias Ringwald                     if (att_connection->max_mtu > ATT_REQUEST_BUFFER_SIZE){
96652e211deSMatthias Ringwald                         att_connection->max_mtu = ATT_REQUEST_BUFFER_SIZE;
96752e211deSMatthias Ringwald                     }
96852e211deSMatthias Ringwald 
96952e211deSMatthias Ringwald                     log_info("Connection opened %s, l2cap cid %04x, mtu %u", bd_addr_to_str(address), att_server->l2cap_cid, att_connection->mtu);
97052e211deSMatthias Ringwald 
97152e211deSMatthias Ringwald                     // update security params
97252e211deSMatthias Ringwald                     att_connection->encryption_key_size = gap_encryption_key_size(con_handle);
97352e211deSMatthias Ringwald                     att_connection->authenticated = gap_authenticated(con_handle);
97452e211deSMatthias Ringwald                     att_connection->secure_connection = gap_secure_connection(con_handle);
97552e211deSMatthias Ringwald                     log_info("encrypted key size %u, authenticated %u, secure connection %u",
97652e211deSMatthias Ringwald                              att_connection->encryption_key_size, att_connection->authenticated, att_connection->secure_connection);
97752e211deSMatthias Ringwald 
97852e211deSMatthias Ringwald                     // notify connection opened
97952e211deSMatthias Ringwald                     att_emit_connected_event(att_server, att_connection);
98052e211deSMatthias Ringwald 
98152e211deSMatthias Ringwald                     // restore persisten ccc if encrypted
98252e211deSMatthias Ringwald                     if ( gap_security_level(con_handle) >= LEVEL_2){
98352e211deSMatthias Ringwald                         att_server_persistent_ccc_restore(att_server, att_connection);
98452e211deSMatthias Ringwald                     }
98552e211deSMatthias Ringwald                     // TODO: what to do about le device db?
98652e211deSMatthias Ringwald                     att_server->pairing_active = 0;
98752e211deSMatthias Ringwald                     break;
98852e211deSMatthias Ringwald #endif
98941772f37SMatthias Ringwald                 case L2CAP_EVENT_CAN_SEND_NOW:
99041772f37SMatthias Ringwald                     att_server_handle_can_send_now();
99141772f37SMatthias Ringwald                     break;
99241772f37SMatthias Ringwald                 case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
99341772f37SMatthias Ringwald                     // GATT client has negotiated the mtu for this connection
99452e211deSMatthias Ringwald                     hci_connection = hci_connection_for_handle(channel);
99568591e36SMatthias Ringwald                     if (!hci_connection) break;
996388fa7c4SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
997b6df12b6SMatthias Ringwald                     att_connection->mtu = little_endian_read_16(packet, 4);
99841772f37SMatthias Ringwald                     break;
99941772f37SMatthias Ringwald                 default:
100041772f37SMatthias Ringwald                     break;
100141772f37SMatthias Ringwald             }
100241772f37SMatthias Ringwald             break;
100341772f37SMatthias Ringwald 
100441772f37SMatthias Ringwald         case ATT_DATA_PACKET:
100552e211deSMatthias Ringwald             log_debug("ATT Packet, handle 0x%04x", channel);
100652e211deSMatthias Ringwald             hci_connection = hci_connection_for_handle(channel);
100768591e36SMatthias Ringwald             if (!hci_connection) break;
100841772f37SMatthias Ringwald 
100926b42dcfSMatthias Ringwald             att_server = &hci_connection->att_server;
101026b42dcfSMatthias Ringwald             att_connection = &hci_connection->att_connection;
101126b42dcfSMatthias Ringwald             att_server_handle_att_pdu(att_server, att_connection, packet, size);
1012372d62c4SMatthias Ringwald             break;
10137bbeb3adSMilanka Ringwald 
101452e211deSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
101552e211deSMatthias Ringwald         case L2CAP_DATA_PACKET:
101652e211deSMatthias Ringwald             hci_connections_get_iterator(&it);
101752e211deSMatthias Ringwald             while(btstack_linked_list_iterator_has_next(&it)){
101852e211deSMatthias Ringwald                 hci_connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
101952e211deSMatthias Ringwald                 att_server = &hci_connection->att_server;
102052e211deSMatthias Ringwald                 att_connection = &hci_connection->att_connection;
102152e211deSMatthias Ringwald                 if (att_server->l2cap_cid == channel) {
102252e211deSMatthias Ringwald                     att_server_handle_att_pdu(att_server, att_connection, packet, size);
102352e211deSMatthias Ringwald                     break;
102452e211deSMatthias Ringwald                 }
102552e211deSMatthias Ringwald             }
102652e211deSMatthias Ringwald             break;
102752e211deSMatthias Ringwald #endif
102852e211deSMatthias Ringwald 
10297bbeb3adSMilanka Ringwald         default:
10307bbeb3adSMilanka Ringwald             break;
1031372d62c4SMatthias Ringwald     }
10323deb3ec6SMatthias Ringwald }
10333deb3ec6SMatthias Ringwald 
1034bf78aac6SMatthias Ringwald // ---------------------
1035bf78aac6SMatthias Ringwald // persistent CCC writes
1036bf78aac6SMatthias Ringwald static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){
10374ea43905SMatthias Ringwald     return ('B' << 24u) | ('T' << 16u) | ('C' << 8u) | index;
1038bf78aac6SMatthias Ringwald }
1039bf78aac6SMatthias Ringwald 
1040bf78aac6SMatthias Ringwald static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){
1041bf78aac6SMatthias Ringwald     // lookup att_server instance
104268591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
104368591e36SMatthias Ringwald     if (!hci_connection) return;
104468591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
1045bf78aac6SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
1046bf78aac6SMatthias 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);
10476a540790SMatthias Ringwald 
1048bf78aac6SMatthias Ringwald     // check if bonded
1049bf78aac6SMatthias Ringwald     if (le_device_index < 0) return;
10506a540790SMatthias Ringwald 
1051bf78aac6SMatthias Ringwald     // get btstack_tlv
1052bf78aac6SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
1053bf78aac6SMatthias Ringwald     void * tlv_context;
1054bf78aac6SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
1055bf78aac6SMatthias Ringwald     if (!tlv_impl) return;
10566a540790SMatthias Ringwald 
1057bf78aac6SMatthias Ringwald     // update ccc tag
1058bf78aac6SMatthias Ringwald     int index;
10596a540790SMatthias Ringwald     uint32_t highest_seq_nr = 0;
10606a540790SMatthias Ringwald     uint32_t lowest_seq_nr = 0;
10616a540790SMatthias Ringwald     uint32_t tag_for_lowest_seq_nr = 0;
10626a540790SMatthias Ringwald     uint32_t tag_for_empty = 0;
10636a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
10646afb9acaSMatthias Ringwald     for (index=0; index<NVN_NUM_GATT_SERVER_CCC; index++){
1065bf78aac6SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
10666a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
10676a540790SMatthias Ringwald 
10686a540790SMatthias Ringwald         // empty/invalid tag
10696a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)){
10706a540790SMatthias Ringwald             tag_for_empty = tag;
1071bf78aac6SMatthias Ringwald             continue;
1072bf78aac6SMatthias Ringwald         }
10736a540790SMatthias Ringwald         // update highest seq nr
10746a540790SMatthias Ringwald         if (entry.seq_nr > highest_seq_nr){
10756a540790SMatthias Ringwald             highest_seq_nr = entry.seq_nr;
10766a540790SMatthias Ringwald         }
10776a540790SMatthias Ringwald         // find entry with lowest seq nr
10784ea43905SMatthias Ringwald         if ((tag_for_lowest_seq_nr == 0u) || (entry.seq_nr < lowest_seq_nr)){
10796a540790SMatthias Ringwald             tag_for_lowest_seq_nr = tag;
10806a540790SMatthias Ringwald             lowest_seq_nr = entry.seq_nr;
10816a540790SMatthias Ringwald         }
10826a540790SMatthias Ringwald 
10836a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
10846a540790SMatthias Ringwald         if (entry.att_handle   != att_handle)      continue;
10856a540790SMatthias Ringwald 
10866a540790SMatthias Ringwald         // found matching entry
10879305033eSMatthias Ringwald         if (value != 0){
1088bf78aac6SMatthias Ringwald             // update
10896a540790SMatthias Ringwald             if (entry.value == value) {
1090760ad805SMatthias Ringwald                 log_info("CCC Index %u: Up-to-date", index);
1091760ad805SMatthias Ringwald                 return;
1092760ad805SMatthias Ringwald             }
1093348fc806SMatthias Ringwald             entry.value = (uint8_t) value;
10944ea43905SMatthias Ringwald             entry.seq_nr = highest_seq_nr + 1u;
1095760ad805SMatthias Ringwald             log_info("CCC Index %u: Store", index);
10960fe46bc1SMatthias Ringwald             int result = tlv_impl->store_tag(tlv_context, tag, (const 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             }
110001ac2008SMatthias Ringwald         } else {
110101ac2008SMatthias Ringwald             // delete
1102760ad805SMatthias Ringwald             log_info("CCC Index %u: Delete", index);
110301ac2008SMatthias Ringwald             tlv_impl->delete_tag(tlv_context, tag);
110401ac2008SMatthias Ringwald         }
1105bf78aac6SMatthias Ringwald         return;
1106bf78aac6SMatthias Ringwald     }
11076a540790SMatthias Ringwald 
11080fe46bc1SMatthias Ringwald     log_info("tag_for_empty %"PRIx32", tag_for_lowest_seq_nr %"PRIx32, tag_for_empty, tag_for_lowest_seq_nr);
11096a540790SMatthias Ringwald 
11104ea43905SMatthias Ringwald     if (value == 0u){
11116a540790SMatthias Ringwald         // done
11126a540790SMatthias Ringwald         return;
11136a540790SMatthias Ringwald     }
11146a540790SMatthias Ringwald 
111538b893aaSMilanka Ringwald     uint32_t tag_to_use = 0u;
111638b893aaSMilanka Ringwald     if (tag_for_empty != 0u){
11176a540790SMatthias Ringwald         tag_to_use = tag_for_empty;
1118f688bdb8SMatthias Ringwald     } else if (tag_for_lowest_seq_nr != 0){
11196a540790SMatthias Ringwald         tag_to_use = tag_for_lowest_seq_nr;
11206a540790SMatthias Ringwald     } else {
11216a540790SMatthias Ringwald         // should not happen
11226a540790SMatthias Ringwald         return;
11236a540790SMatthias Ringwald     }
1124bf78aac6SMatthias Ringwald     // store ccc tag
11254ea43905SMatthias Ringwald     entry.seq_nr       = highest_seq_nr + 1u;
11266a540790SMatthias Ringwald     entry.device_index = le_device_index;
11276a540790SMatthias Ringwald     entry.att_handle   = att_handle;
1128348fc806SMatthias Ringwald     entry.value        = (uint8_t) value;
11290fe46bc1SMatthias Ringwald     int result = tlv_impl->store_tag(tlv_context, tag_to_use, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
11300fe46bc1SMatthias Ringwald     if (result != 0){
11310fe46bc1SMatthias Ringwald         log_error("Store tag index %u failed", index);
11320fe46bc1SMatthias Ringwald     }
1133bf78aac6SMatthias Ringwald }
1134bf78aac6SMatthias Ringwald 
1135fe3d0cbaSMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server){
1136760ad805SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
1137760ad805SMatthias Ringwald     log_info("Clear CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
1138760ad805SMatthias Ringwald     // check if bonded
1139760ad805SMatthias Ringwald     if (le_device_index < 0) return;
1140760ad805SMatthias Ringwald     // get btstack_tlv
1141760ad805SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
1142760ad805SMatthias Ringwald     void * tlv_context;
1143760ad805SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
1144760ad805SMatthias Ringwald     if (!tlv_impl) return;
1145760ad805SMatthias Ringwald     // get all ccc tag
1146760ad805SMatthias Ringwald     int index;
11476a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
11486afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
1149760ad805SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
11506a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
11516a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
11526a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
1153760ad805SMatthias Ringwald         // delete entry
1154760ad805SMatthias Ringwald         log_info("CCC Index %u: Delete", index);
1155760ad805SMatthias Ringwald         tlv_impl->delete_tag(tlv_context, tag);
1156760ad805SMatthias Ringwald     }
1157760ad805SMatthias Ringwald }
1158760ad805SMatthias Ringwald 
11599a1f55eaSMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server, att_connection_t * att_connection){
116001ac2008SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
116101ac2008SMatthias Ringwald     log_info("Restore CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
11621b7acd0dSMatthias Ringwald     // check if bonded
11631b7acd0dSMatthias Ringwald     if (le_device_index < 0) return;
116401ac2008SMatthias Ringwald     // get btstack_tlv
116501ac2008SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
116601ac2008SMatthias Ringwald     void * tlv_context;
116701ac2008SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
116801ac2008SMatthias Ringwald     if (!tlv_impl) return;
116901ac2008SMatthias Ringwald     // get all ccc tag
117001ac2008SMatthias Ringwald     int index;
11716a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
11726afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
117301ac2008SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
11746a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
11756a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
11766a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
117701ac2008SMatthias Ringwald         // simulate write callback
11786a540790SMatthias Ringwald         uint16_t attribute_handle = entry.att_handle;
117901ac2008SMatthias Ringwald         uint8_t  value[2];
11806a540790SMatthias Ringwald         little_endian_store_16(value, 0, entry.value);
118101ac2008SMatthias Ringwald         att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
118201ac2008SMatthias Ringwald         if (!callback) continue;
11836a540790SMatthias Ringwald         log_info("CCC Index %u: Set Attribute handle 0x%04x to value 0x%04x", index, attribute_handle, entry.value );
1184b6df12b6SMatthias Ringwald         (*callback)(att_connection->con_handle, attribute_handle, ATT_TRANSACTION_MODE_NONE, 0, value, sizeof(value));
118501ac2008SMatthias Ringwald     }
118601ac2008SMatthias Ringwald }
118701ac2008SMatthias Ringwald 
1188bf78aac6SMatthias Ringwald // persistent CCC writes
1189bf78aac6SMatthias Ringwald // ---------------------
11903d71c7a4SMatthias Ringwald 
11913d71c7a4SMatthias Ringwald // gatt service management
11923d71c7a4SMatthias Ringwald static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){
11933d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
11943d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
11953d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
11963d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
11973d71c7a4SMatthias Ringwald         if (handler->start_handle > handle) continue;
11983d71c7a4SMatthias Ringwald         if (handler->end_handle   < handle) continue;
11993d71c7a4SMatthias Ringwald         return handler;
12003d71c7a4SMatthias Ringwald     }
12013d71c7a4SMatthias Ringwald     return NULL;
12023d71c7a4SMatthias Ringwald }
12033d71c7a4SMatthias Ringwald 
12043d71c7a4SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){
12053d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
12069305033eSMatthias Ringwald     if (handler != NULL) return handler->write_callback;
12073d71c7a4SMatthias Ringwald     return att_server_client_write_callback;
12083d71c7a4SMatthias Ringwald }
12093d71c7a4SMatthias Ringwald 
12105d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle){
12115d07396bSMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
12129305033eSMatthias Ringwald     if (handler != NULL) return handler->packet_handler;
12135d07396bSMatthias Ringwald     return att_client_packet_handler;
12145d07396bSMatthias Ringwald }
12155d07396bSMatthias Ringwald 
12163d71c7a4SMatthias Ringwald static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){
12173d71c7a4SMatthias Ringwald     // notify all callbacks
12183d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
12193d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
12203d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
12213d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
12223d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
12233d71c7a4SMatthias Ringwald         (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
12243d71c7a4SMatthias Ringwald     }
12253d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return;
12263d71c7a4SMatthias Ringwald     (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
12273d71c7a4SMatthias Ringwald }
12283d71c7a4SMatthias Ringwald 
12293d71c7a4SMatthias Ringwald // returns first reported error or 0
12303d71c7a4SMatthias Ringwald static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){
12313d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
12323d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
12333d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
12343d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
12353d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
12363d71c7a4SMatthias Ringwald         uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
123738b893aaSMilanka Ringwald         if (error_code != 0u) return error_code;
12383d71c7a4SMatthias Ringwald     }
12393d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return 0;
12403d71c7a4SMatthias Ringwald     return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
12413d71c7a4SMatthias Ringwald }
12423d71c7a4SMatthias Ringwald 
12433d71c7a4SMatthias 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){
1244c61037ccSMatthias Ringwald     att_service_handler_t * service = att_service_handler_for_handle(attribute_handle);
1245c61037ccSMatthias Ringwald     att_read_callback_t read_callback = (service != NULL) ? service->read_callback : att_server_client_read_callback;
1246c61037ccSMatthias Ringwald     uint16_t result = 0;
1247c61037ccSMatthias Ringwald     if (read_callback != NULL){
1248c61037ccSMatthias Ringwald         result = (*read_callback)(con_handle, attribute_handle, offset, buffer, buffer_size);
1249c61037ccSMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
1250c61037ccSMatthias Ringwald         if (result == ATT_READ_RESPONSE_PENDING){
1251c61037ccSMatthias Ringwald             if (service == NULL){
1252c61037ccSMatthias Ringwald                 att_server_flags |= ATT_SERVICE_FLAGS_DELAYED_RESPONSE;
1253c61037ccSMatthias Ringwald             } else {
1254c61037ccSMatthias Ringwald                 service->flags   |= ATT_SERVICE_FLAGS_DELAYED_RESPONSE;
1255c61037ccSMatthias Ringwald             }
1256c61037ccSMatthias Ringwald         }
1257c61037ccSMatthias Ringwald #endif
1258c61037ccSMatthias Ringwald     }
1259c61037ccSMatthias Ringwald     return result;
12603d71c7a4SMatthias Ringwald }
12613d71c7a4SMatthias Ringwald 
12623d71c7a4SMatthias 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){
12633d71c7a4SMatthias Ringwald     switch (transaction_mode){
12643d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_VALIDATE:
12653d71c7a4SMatthias Ringwald             return att_validate_prepared_write(con_handle);
12663d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_EXECUTE:
12673d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_CANCEL:
12683d71c7a4SMatthias Ringwald             att_notify_write_callbacks(con_handle, transaction_mode);
12693d71c7a4SMatthias Ringwald             return 0;
12703d71c7a4SMatthias Ringwald         default:
12713d71c7a4SMatthias Ringwald             break;
12723d71c7a4SMatthias Ringwald     }
12733d71c7a4SMatthias Ringwald 
1274bf78aac6SMatthias Ringwald     // track CCC writes
12754ea43905SMatthias Ringwald     if (att_is_persistent_ccc(attribute_handle) && (offset == 0u) && (buffer_size == 2u)){
1276bf78aac6SMatthias Ringwald         att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0));
1277bf78aac6SMatthias Ringwald     }
1278bf78aac6SMatthias Ringwald 
127901ac2008SMatthias Ringwald     att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
128001ac2008SMatthias Ringwald     if (!callback) return 0;
12813d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size);
12823d71c7a4SMatthias Ringwald }
12833d71c7a4SMatthias Ringwald 
12843d71c7a4SMatthias Ringwald /**
12853d71c7a4SMatthias Ringwald  * @brief register read/write callbacks for specific handle range
12863d71c7a4SMatthias Ringwald  * @param att_service_handler_t
12873d71c7a4SMatthias Ringwald  */
12883d71c7a4SMatthias Ringwald void att_server_register_service_handler(att_service_handler_t * handler){
1289ec8f5414SMilanka Ringwald     bool att_server_registered = false;
1290f688bdb8SMatthias Ringwald     if (att_service_handler_for_handle(handler->start_handle) != NULL){
1291ec8f5414SMilanka Ringwald         att_server_registered = true;
1292ec8f5414SMilanka Ringwald     }
1293ec8f5414SMilanka Ringwald 
1294f688bdb8SMatthias Ringwald     if (att_service_handler_for_handle(handler->end_handle) != NULL){
1295ec8f5414SMilanka Ringwald         att_server_registered = true;
1296ec8f5414SMilanka Ringwald     }
1297ec8f5414SMilanka Ringwald 
1298ec8f5414SMilanka Ringwald     if (att_server_registered){
12993d71c7a4SMatthias Ringwald         log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle);
13003d71c7a4SMatthias Ringwald         return;
13013d71c7a4SMatthias Ringwald     }
130285878f9dSMatthias Ringwald 
130385878f9dSMatthias Ringwald     handler->flags = 0;
13043d71c7a4SMatthias Ringwald     btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler);
13053d71c7a4SMatthias Ringwald }
13063d71c7a4SMatthias Ringwald 
13073deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){
13083deb3ec6SMatthias Ringwald 
13093d71c7a4SMatthias Ringwald     // store callbacks
13103d71c7a4SMatthias Ringwald     att_server_client_read_callback  = read_callback;
13113d71c7a4SMatthias Ringwald     att_server_client_write_callback = write_callback;
13123d71c7a4SMatthias Ringwald 
13131a389cdcSMatthias Ringwald     // register for HCI Events
131452e211deSMatthias Ringwald     hci_event_callback_registration.callback = &att_server_event_packet_handler;
13151a389cdcSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
13161a389cdcSMatthias Ringwald 
1317406722e4SMatthias Ringwald     // register for SM events
131852e211deSMatthias Ringwald     sm_event_callback_registration.callback = &att_server_event_packet_handler;
1319406722e4SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
13203deb3ec6SMatthias Ringwald 
13211a389cdcSMatthias Ringwald     // and L2CAP ATT Server PDUs
132252e211deSMatthias Ringwald     att_dispatch_register_server(att_server_dispatch_packet_handler);
13233deb3ec6SMatthias Ringwald 
132470dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
132570dca4d4SMatthias Ringwald     // setup l2cap service
13266bf42d1fSMatthias Ringwald     att_dispatch_classic_register_service();
132770dca4d4SMatthias Ringwald #endif
132870dca4d4SMatthias Ringwald 
13293deb3ec6SMatthias Ringwald     att_set_db(db);
13303d71c7a4SMatthias Ringwald     att_set_read_callback(att_server_read_callback);
13313d71c7a4SMatthias Ringwald     att_set_write_callback(att_server_write_callback);
13323deb3ec6SMatthias Ringwald }
13333deb3ec6SMatthias Ringwald 
13343deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){
13353deb3ec6SMatthias Ringwald     att_client_packet_handler = handler;
13363deb3ec6SMatthias Ringwald }
13373deb3ec6SMatthias Ringwald 
1338cbbb12d9SMatthias Ringwald 
1339cbbb12d9SMatthias Ringwald // to be deprecated
1340c141988cSMatthias Ringwald int  att_server_can_send_packet_now(hci_con_handle_t con_handle){
134168591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
134268591e36SMatthias Ringwald     if (!hci_connection) return 0;
134337a8059dSMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
134437a8059dSMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
134537a8059dSMatthias Ringwald     return att_server_can_send_packet(att_server, att_connection);
13461b96b007SMatthias Ringwald }
13471b96b007SMatthias Ringwald 
13489816429dSMatthias Ringwald uint8_t att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
1349cbbb12d9SMatthias Ringwald     return att_server_request_to_send_notification(callback_registration, con_handle);
13503deb3ec6SMatthias Ringwald }
13513deb3ec6SMatthias Ringwald 
1352cbbb12d9SMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){
1353cbbb12d9SMatthias Ringwald     att_client_waiting_for_can_send_registration.callback = &att_emit_can_send_now_event;
1354cbbb12d9SMatthias Ringwald     att_server_request_to_send_notification(&att_client_waiting_for_can_send_registration, con_handle);
1355cbbb12d9SMatthias Ringwald }
1356cbbb12d9SMatthias Ringwald // end of deprecated
1357cbbb12d9SMatthias Ringwald 
13589816429dSMatthias Ringwald uint8_t att_server_request_to_send_notification(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
135968591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
136068591e36SMatthias Ringwald     if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
136168591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
13623f9b77dbSMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
1363c37df6f6SMatthias Ringwald     bool added = btstack_linked_list_add_tail(&att_server->notification_requests, (btstack_linked_item_t*) callback_registration);
13643f9b77dbSMatthias Ringwald     att_server_request_can_send_now(att_server, att_connection);
1365c37df6f6SMatthias Ringwald     if (added){
1366cbbb12d9SMatthias Ringwald         return ERROR_CODE_SUCCESS;
1367c37df6f6SMatthias Ringwald     } else {
1368c37df6f6SMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1369c37df6f6SMatthias Ringwald     }
1370cbbb12d9SMatthias Ringwald }
1371cbbb12d9SMatthias Ringwald 
13729816429dSMatthias Ringwald uint8_t att_server_request_to_send_indication(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
137368591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
137468591e36SMatthias Ringwald     if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
137568591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
13763f9b77dbSMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
1377c37df6f6SMatthias Ringwald     bool added = btstack_linked_list_add_tail(&att_server->indication_requests, (btstack_linked_item_t*) callback_registration);
13783f9b77dbSMatthias Ringwald     att_server_request_can_send_now(att_server, att_connection);
1379c37df6f6SMatthias Ringwald     if (added){
1380969a5bbaSMatthias Ringwald         return ERROR_CODE_SUCCESS;
1381c37df6f6SMatthias Ringwald     } else {
1382c37df6f6SMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1383c37df6f6SMatthias Ringwald     }
1384bb38f057SMatthias Ringwald }
1385bb38f057SMatthias Ringwald 
1386d74c7b2bSMatthias 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){
1387d74c7b2bSMatthias Ringwald 
1388d74c7b2bSMatthias Ringwald     att_server_t *     att_server = NULL;
1389d74c7b2bSMatthias Ringwald     att_connection_t * att_connection = NULL;
1390d74c7b2bSMatthias Ringwald     uint8_t *          packet_buffer = NULL;
1391d74c7b2bSMatthias Ringwald 
1392d74c7b2bSMatthias Ringwald     // prefer enhanced bearer
1393d74c7b2bSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
1394d74c7b2bSMatthias Ringwald     att_server_eatt_bearer_t * eatt_bearer = att_server_eatt_bearer_for_con_handle(con_handle);
1395d74c7b2bSMatthias Ringwald     if (eatt_bearer != NULL){
1396d74c7b2bSMatthias Ringwald         att_server     = &eatt_bearer->att_server;
1397d74c7b2bSMatthias Ringwald         att_connection = &eatt_bearer->att_connection;
1398d74c7b2bSMatthias Ringwald         packet_buffer  = eatt_bearer->send_buffer;
1399d74c7b2bSMatthias Ringwald     } else
1400d74c7b2bSMatthias Ringwald #endif
1401d74c7b2bSMatthias Ringwald     {
1402d74c7b2bSMatthias Ringwald         hci_connection_t *hci_connection = hci_connection_for_handle(con_handle);
1403d74c7b2bSMatthias Ringwald         if (hci_connection != NULL) {
1404d74c7b2bSMatthias Ringwald             att_server     = &hci_connection->att_server;
1405d74c7b2bSMatthias Ringwald             att_connection = &hci_connection->att_connection;
1406d74c7b2bSMatthias Ringwald         }
1407d74c7b2bSMatthias Ringwald     }
1408d74c7b2bSMatthias Ringwald 
1409d74c7b2bSMatthias Ringwald     if (att_server == NULL) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1410d74c7b2bSMatthias Ringwald     if (!att_server_can_send_packet(att_server, att_connection)) return BTSTACK_ACL_BUFFERS_FULL;
1411d74c7b2bSMatthias Ringwald 
1412d74c7b2bSMatthias Ringwald     if (packet_buffer == NULL){
1413d74c7b2bSMatthias Ringwald         l2cap_reserve_packet_buffer();
1414d74c7b2bSMatthias Ringwald         packet_buffer = l2cap_get_outgoing_buffer();
1415d74c7b2bSMatthias Ringwald     }
1416d74c7b2bSMatthias Ringwald 
1417d74c7b2bSMatthias Ringwald     *out_att_connection = att_connection;
1418d74c7b2bSMatthias Ringwald     *out_att_server = att_server;
1419d74c7b2bSMatthias Ringwald     *out_packet_buffer = packet_buffer;
1420d74c7b2bSMatthias Ringwald     return ERROR_CODE_SUCCESS;
1421d74c7b2bSMatthias Ringwald }
1422d74c7b2bSMatthias Ringwald 
14239816429dSMatthias Ringwald uint8_t att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){
14245683a8e4SMatthias Ringwald     att_server_t * att_server = NULL;
14255683a8e4SMatthias Ringwald     att_connection_t * att_connection = NULL;
14265683a8e4SMatthias Ringwald     uint8_t * packet_buffer = NULL;
142768591e36SMatthias Ringwald 
14285683a8e4SMatthias Ringwald     uint8_t status = att_server_prepare_server_message(con_handle, &att_server, &att_connection, &packet_buffer);
14295683a8e4SMatthias Ringwald     if (status != ERROR_CODE_SUCCESS){
14305683a8e4SMatthias Ringwald         return status;
14315683a8e4SMatthias Ringwald     }
14323deb3ec6SMatthias Ringwald 
1433b6df12b6SMatthias Ringwald     uint16_t size = att_prepare_handle_value_notification(att_connection, attribute_handle, value, value_len, packet_buffer);
1434d415948cSMatthias Ringwald 
14350f70c52eSMatthias Ringwald     return att_server_send_prepared(att_server, att_connection, NULL, size);
14363deb3ec6SMatthias Ringwald }
14373deb3ec6SMatthias Ringwald 
143840f8d8f2SMatthias Ringwald /**
143940f8d8f2SMatthias Ringwald  * @brief notify client about multiple attribute value changes
144040f8d8f2SMatthias Ringwald  * @param con_handle
144140f8d8f2SMatthias Ringwald  * @param num_attributes
144240f8d8f2SMatthias Ringwald  * @param attribute_handles[]
144340f8d8f2SMatthias Ringwald  * @param values_data[]
144440f8d8f2SMatthias Ringwald  * @param values_len[]
144540f8d8f2SMatthias Ringwald  * @return 0 if ok, error otherwise
144640f8d8f2SMatthias Ringwald  */
144740f8d8f2SMatthias Ringwald uint8_t att_server_multiple_notify(hci_con_handle_t con_handle, uint8_t num_attributes,
144840f8d8f2SMatthias Ringwald                                    const uint16_t * attribute_handles, const uint8_t ** values_data, const uint16_t * values_len){
144940f8d8f2SMatthias Ringwald 
145040f8d8f2SMatthias Ringwald     att_server_t * att_server = NULL;
145140f8d8f2SMatthias Ringwald     att_connection_t * att_connection = NULL;
145240f8d8f2SMatthias Ringwald     uint8_t * packet_buffer = NULL;
145340f8d8f2SMatthias Ringwald 
1454d74c7b2bSMatthias Ringwald     uint8_t status = att_server_prepare_server_message(con_handle, &att_server, &att_connection, &packet_buffer);
1455d74c7b2bSMatthias Ringwald     if (status != ERROR_CODE_SUCCESS){
1456d74c7b2bSMatthias Ringwald         return status;
145740f8d8f2SMatthias Ringwald     }
145840f8d8f2SMatthias Ringwald 
145940f8d8f2SMatthias Ringwald     uint16_t size = att_prepare_handle_value_multiple_notification(att_connection, num_attributes, attribute_handles, values_data, values_len, packet_buffer);
146040f8d8f2SMatthias Ringwald 
146140f8d8f2SMatthias Ringwald     return att_server_send_prepared(att_server, att_connection, packet_buffer, size);
146240f8d8f2SMatthias Ringwald }
146340f8d8f2SMatthias Ringwald 
14649816429dSMatthias Ringwald uint8_t att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){
146518707219SMatthias Ringwald 
14665683a8e4SMatthias Ringwald     att_server_t * att_server = NULL;
14675683a8e4SMatthias Ringwald     att_connection_t * att_connection = NULL;
14685683a8e4SMatthias Ringwald     uint8_t * packet_buffer = NULL;
14695683a8e4SMatthias Ringwald 
14705683a8e4SMatthias Ringwald     uint8_t status = att_server_prepare_server_message(con_handle, &att_server, &att_connection, &packet_buffer);
14715683a8e4SMatthias Ringwald     if (status != ERROR_CODE_SUCCESS){
14725683a8e4SMatthias Ringwald         return status;
14735683a8e4SMatthias Ringwald     }
14745683a8e4SMatthias Ringwald 
14755683a8e4SMatthias Ringwald     if (att_server->value_indication_handle != 0u) {
14765683a8e4SMatthias Ringwald         // free reserved packet buffer
14775683a8e4SMatthias Ringwald         if (att_server->bearer_type == ATT_BEARER_ENHANCED_LE){
14785683a8e4SMatthias Ringwald             l2cap_release_packet_buffer();
14795683a8e4SMatthias Ringwald         }
14805683a8e4SMatthias Ringwald         return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS;
14815683a8e4SMatthias Ringwald     }
14823deb3ec6SMatthias Ringwald 
14833deb3ec6SMatthias Ringwald     // track indication
148418707219SMatthias Ringwald     att_server->value_indication_handle = attribute_handle;
148518707219SMatthias Ringwald     btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout);
148618707219SMatthias Ringwald     btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS);
148718707219SMatthias Ringwald     btstack_run_loop_add_timer(&att_server->value_indication_timer);
14883deb3ec6SMatthias Ringwald 
1489b6df12b6SMatthias Ringwald     uint16_t size = att_prepare_handle_value_indication(att_connection, attribute_handle, value, value_len, packet_buffer);
149023079b39SMatthias Ringwald 
14910f70c52eSMatthias Ringwald     return att_server_send_prepared(att_server, att_connection, NULL, size);
14923deb3ec6SMatthias Ringwald }
14938f9132b5SMilanka Ringwald 
14948f9132b5SMilanka Ringwald uint16_t att_server_get_mtu(hci_con_handle_t con_handle){
149568591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
149668591e36SMatthias Ringwald     if (!hci_connection) return 0;
1497388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
1498b6df12b6SMatthias Ringwald     return att_connection->mtu;
14998f9132b5SMilanka Ringwald }
150035d7f2dfSMilanka Ringwald 
150135d7f2dfSMilanka Ringwald void att_server_deinit(void){
150235d7f2dfSMilanka Ringwald     att_server_client_read_callback  = NULL;
150335d7f2dfSMilanka Ringwald     att_server_client_write_callback = NULL;
150435d7f2dfSMilanka Ringwald     att_client_packet_handler = NULL;
150535d7f2dfSMilanka Ringwald     service_handlers = NULL;
1506c61037ccSMatthias Ringwald     att_server_flags = 0;
150735d7f2dfSMilanka Ringwald }
150820d4358dSMatthias Ringwald 
150920d4358dSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
15108d3bb68fSMatthias Ringwald 
151127afb094SMatthias Ringwald #define MAX_NR_EATT_CHANNELS 5
151227afb094SMatthias Ringwald 
151320d4358dSMatthias Ringwald static uint16_t att_server_eatt_receive_buffer_size;
15148d3bb68fSMatthias Ringwald static uint16_t att_server_eatt_send_buffer_size;
151520d4358dSMatthias Ringwald 
1516523bb989SMatthias Ringwald static att_server_eatt_bearer_t * att_server_eatt_bearer_for_cid(uint16_t cid){
1517523bb989SMatthias Ringwald     btstack_linked_list_iterator_t it;
1518523bb989SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &att_server_eatt_bearer_active);
1519523bb989SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
1520523bb989SMatthias Ringwald         att_server_eatt_bearer_t * eatt_bearer = (att_server_eatt_bearer_t *) btstack_linked_list_iterator_next(&it);
1521f786dd08SMatthias Ringwald         if (eatt_bearer->att_server.l2cap_cid == cid) {
1522523bb989SMatthias Ringwald             return eatt_bearer;
1523523bb989SMatthias Ringwald         }
1524523bb989SMatthias Ringwald     }
1525523bb989SMatthias Ringwald     return NULL;
1526523bb989SMatthias Ringwald }
1527523bb989SMatthias Ringwald 
152840f8d8f2SMatthias Ringwald static att_server_eatt_bearer_t * att_server_eatt_bearer_for_con_handle(hci_con_handle_t con_handle){
152940f8d8f2SMatthias Ringwald     btstack_linked_list_iterator_t it;
153040f8d8f2SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &att_server_eatt_bearer_active);
153140f8d8f2SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
153240f8d8f2SMatthias Ringwald         att_server_eatt_bearer_t * eatt_bearer = (att_server_eatt_bearer_t *) btstack_linked_list_iterator_next(&it);
153340f8d8f2SMatthias Ringwald         if (eatt_bearer->att_connection.con_handle == con_handle) {
153440f8d8f2SMatthias Ringwald             return eatt_bearer;
153540f8d8f2SMatthias Ringwald         }
153640f8d8f2SMatthias Ringwald     }
153740f8d8f2SMatthias Ringwald     return NULL;
153840f8d8f2SMatthias Ringwald }
153940f8d8f2SMatthias Ringwald 
154020d4358dSMatthias Ringwald static void att_server_eatt_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
154120d4358dSMatthias Ringwald     uint16_t cid;
154220d4358dSMatthias Ringwald     uint8_t status;
154320d4358dSMatthias Ringwald     uint16_t remote_mtu;
154420d4358dSMatthias Ringwald 
154520d4358dSMatthias Ringwald     uint8_t i;
154620d4358dSMatthias Ringwald     uint8_t num_requested_bearers;
154720d4358dSMatthias Ringwald     uint8_t num_accepted_bearers;
154827afb094SMatthias Ringwald     uint16_t initial_credits = L2CAP_LE_AUTOMATIC_CREDITS;
154927afb094SMatthias Ringwald     uint8_t * receive_buffers[MAX_NR_EATT_CHANNELS];
155027afb094SMatthias Ringwald     uint16_t cids[MAX_NR_EATT_CHANNELS];
155127afb094SMatthias Ringwald     att_server_eatt_bearer_t * eatt_bearers[MAX_NR_EATT_CHANNELS];
1552523bb989SMatthias Ringwald     att_server_eatt_bearer_t * eatt_bearer;
15532954c397SMatthias Ringwald     att_server_t * att_server;
15542954c397SMatthias Ringwald     att_connection_t * att_connection;
15559828b48aSMatthias Ringwald     hci_con_handle_t con_handle;
15569828b48aSMatthias Ringwald     hci_connection_t * hci_connection;
155720d4358dSMatthias Ringwald 
155820d4358dSMatthias Ringwald     switch (packet_type) {
155920d4358dSMatthias Ringwald 
156020d4358dSMatthias Ringwald         case L2CAP_DATA_PACKET:
1561f786dd08SMatthias Ringwald             eatt_bearer = att_server_eatt_bearer_for_cid(channel);
1562f786dd08SMatthias Ringwald             btstack_assert(eatt_bearer != NULL);
1563f786dd08SMatthias Ringwald             att_server = &eatt_bearer->att_server;
1564f786dd08SMatthias Ringwald             att_connection = &eatt_bearer->att_connection;
1565f786dd08SMatthias Ringwald             att_server_handle_att_pdu(att_server, att_connection, packet, size);
156620d4358dSMatthias Ringwald             break;
156720d4358dSMatthias Ringwald 
156820d4358dSMatthias Ringwald         case HCI_EVENT_PACKET:
156920d4358dSMatthias Ringwald             switch (packet[0]) {
157020d4358dSMatthias Ringwald 
157120d4358dSMatthias Ringwald                 case L2CAP_EVENT_CAN_SEND_NOW:
1572f786dd08SMatthias Ringwald                     cid = l2cap_event_packet_sent_get_local_cid(packet);
1573f786dd08SMatthias Ringwald                     eatt_bearer = att_server_eatt_bearer_for_cid(cid);
1574f786dd08SMatthias Ringwald                     btstack_assert(eatt_bearer != NULL);
1575f786dd08SMatthias Ringwald                     att_server = &eatt_bearer->att_server;
1576f786dd08SMatthias Ringwald                     att_connection = &eatt_bearer->att_connection;
1577f786dd08SMatthias Ringwald                     // only used for EATT request responses
1578f786dd08SMatthias Ringwald                     btstack_assert(att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED);
1579f786dd08SMatthias Ringwald                     att_server_process_validated_request(att_server, att_connection, eatt_bearer->send_buffer);
158020d4358dSMatthias Ringwald                     break;
158120d4358dSMatthias Ringwald 
158220d4358dSMatthias Ringwald                 case L2CAP_EVENT_PACKET_SENT:
158320d4358dSMatthias Ringwald                     cid = l2cap_event_packet_sent_get_local_cid(packet);
1584be4ca859SMatthias Ringwald                     eatt_bearer = att_server_eatt_bearer_for_cid(cid);
1585f786dd08SMatthias Ringwald                     btstack_assert(eatt_bearer != NULL);
15862954c397SMatthias Ringwald                     att_server = &eatt_bearer->att_server;
15872954c397SMatthias Ringwald                     att_connection = &eatt_bearer->att_connection;
15882954c397SMatthias Ringwald                     att_server_handle_att_pdu(att_server, att_connection, packet, size);
158920d4358dSMatthias Ringwald                     break;
159020d4358dSMatthias Ringwald 
159120d4358dSMatthias Ringwald                 case L2CAP_EVENT_ECBM_INCOMING_CONNECTION:
159220d4358dSMatthias Ringwald                     cid = l2cap_event_ecbm_incoming_connection_get_local_cid(packet);
15939828b48aSMatthias Ringwald 
15949828b48aSMatthias Ringwald                     // reject if outgoing l2cap connection active, L2CAP/TIM/BV-01-C
15959828b48aSMatthias Ringwald                     con_handle = l2cap_event_ecbm_incoming_connection_get_handle(packet);
15969828b48aSMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
15979828b48aSMatthias Ringwald                     btstack_assert(hci_connection != NULL);
15989828b48aSMatthias Ringwald                     if (hci_connection->att_server.eatt_outgoing_active) {
15999828b48aSMatthias Ringwald                         hci_connection->att_server.incoming_connection_request = true;
16009828b48aSMatthias Ringwald                         l2cap_ecbm_decline_channels(cid, L2CAP_ECBM_CONNECTION_RESULT_SOME_REFUSED_INSUFFICIENT_RESOURCES_AVAILABLE );
16019828b48aSMatthias Ringwald                         log_info("Decline incoming connection from %s", bd_addr_to_str(hci_connection->address));
16029828b48aSMatthias Ringwald                     } else {
160320d4358dSMatthias Ringwald                         num_requested_bearers = l2cap_event_ecbm_incoming_connection_get_num_channels(packet);
160420d4358dSMatthias Ringwald                         for (i = 0; i < num_requested_bearers; i++){
1605c2cbe117SMatthias Ringwald                             eatt_bearers[i] = (att_server_eatt_bearer_t *) btstack_linked_list_pop(&att_server_eatt_bearer_pool);
160620d4358dSMatthias Ringwald                             if (eatt_bearers[i] == NULL) {
160720d4358dSMatthias Ringwald                                 break;
160820d4358dSMatthias Ringwald                             }
1609be4ca859SMatthias Ringwald                             eatt_bearers[i]->att_connection.con_handle = l2cap_event_ecbm_incoming_connection_get_handle(packet);
1610f786dd08SMatthias Ringwald                             eatt_bearers[i]->att_server.bearer_type = ATT_BEARER_ENHANCED_LE;
1611735b0222SMatthias Ringwald                             receive_buffers[i] = eatt_bearers[i]->receive_buffer;
16126b236cb0SMatthias Ringwald                             btstack_linked_list_add(&att_server_eatt_bearer_active, (btstack_linked_item_t *) eatt_bearers[i]);
161320d4358dSMatthias Ringwald                         }
161420d4358dSMatthias Ringwald                         num_accepted_bearers = i;
161520d4358dSMatthias Ringwald                         status = l2cap_ecbm_accept_channels(cid, num_accepted_bearers, initial_credits, att_server_eatt_receive_buffer_size, receive_buffers, cids);
161620d4358dSMatthias Ringwald                         btstack_assert(status == ERROR_CODE_SUCCESS);
1617c2cbe117SMatthias Ringwald                         log_info("requested %u, accepted %u", num_requested_bearers, num_accepted_bearers);
16186b236cb0SMatthias Ringwald                         for (i=0;i<num_accepted_bearers;i++){
1619c2cbe117SMatthias Ringwald                             log_info("eatt l2cap cid: 0x%04x", cids[i]);
1620f786dd08SMatthias Ringwald                             eatt_bearers[i]->att_server.l2cap_cid = cids[i];
16216b236cb0SMatthias Ringwald                         }
16229828b48aSMatthias Ringwald                     }
162320d4358dSMatthias Ringwald                     break;
162420d4358dSMatthias Ringwald 
162520d4358dSMatthias Ringwald                 case L2CAP_EVENT_ECBM_CHANNEL_OPENED:
162620d4358dSMatthias Ringwald                     cid         = l2cap_event_ecbm_channel_opened_get_local_cid(packet);
1627c2cbe117SMatthias Ringwald                     status      = l2cap_event_ecbm_channel_opened_get_status(packet);
1628c2cbe117SMatthias Ringwald                     remote_mtu  = l2cap_event_ecbm_channel_opened_get_remote_mtu(packet);
1629be4ca859SMatthias Ringwald                     eatt_bearer = att_server_eatt_bearer_for_cid(cid);
1630be4ca859SMatthias Ringwald                     btstack_assert(eatt_bearer != NULL);
1631c2cbe117SMatthias Ringwald                     eatt_bearer->att_connection.mtu_exchanged = true;
1632c2cbe117SMatthias Ringwald                     eatt_bearer->att_connection.mtu = remote_mtu;
1633be4ca859SMatthias Ringwald                     eatt_bearer->att_connection.max_mtu = remote_mtu;
163420d4358dSMatthias Ringwald                     log_info("L2CAP_EVENT_ECBM_CHANNEL_OPENED - cid 0x%04x mtu %u, status 0x%02x", cid, remote_mtu, status);
163520d4358dSMatthias Ringwald                     break;
163620d4358dSMatthias Ringwald 
163720d4358dSMatthias Ringwald                 case L2CAP_EVENT_ECBM_RECONFIGURED:
163820d4358dSMatthias Ringwald                     break;
163920d4358dSMatthias Ringwald 
1640523bb989SMatthias Ringwald                 case L2CAP_EVENT_CHANNEL_CLOSED:
1641523bb989SMatthias Ringwald                     eatt_bearer = att_server_eatt_bearer_for_cid(l2cap_event_channel_closed_get_local_cid(packet));
1642*370c8b58SMatthias Ringwald                     btstack_assert(eatt_bearer != NULL);
1643523bb989SMatthias Ringwald 
1644523bb989SMatthias Ringwald                     // TODO: finalize - abort queued writes
1645523bb989SMatthias Ringwald 
164627afb094SMatthias Ringwald                     btstack_linked_list_remove(&att_server_eatt_bearer_active, (btstack_linked_item_t  *) eatt_bearer);
1647523bb989SMatthias Ringwald                     btstack_linked_list_add(&att_server_eatt_bearer_pool, (btstack_linked_item_t  *) eatt_bearer);
1648523bb989SMatthias Ringwald                     break;
164920d4358dSMatthias Ringwald                 default:
165020d4358dSMatthias Ringwald                     break;
165120d4358dSMatthias Ringwald             }
165227afb094SMatthias Ringwald             break;
165327afb094SMatthias Ringwald         default:
165427afb094SMatthias Ringwald             btstack_unreachable();
165527afb094SMatthias Ringwald             break;
165620d4358dSMatthias Ringwald     }
165720d4358dSMatthias Ringwald }
165820d4358dSMatthias Ringwald 
165920d4358dSMatthias Ringwald // create eatt bearers
166020d4358dSMatthias Ringwald uint8_t att_server_eatt_init(uint8_t num_eatt_bearers, uint8_t * storage_buffer, uint16_t storage_size){
166120d4358dSMatthias Ringwald     uint16_t size_for_structs = num_eatt_bearers * sizeof(att_server_eatt_bearer_t);
166220d4358dSMatthias Ringwald     if (storage_size < size_for_structs) {
166320d4358dSMatthias Ringwald         return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED;
166420d4358dSMatthias Ringwald     }
166520d4358dSMatthias Ringwald 
166620d4358dSMatthias Ringwald     // TODO: The minimum ATT_MTU for an Enhanced ATT bearer is 64 octets.
166720d4358dSMatthias Ringwald 
166820d4358dSMatthias Ringwald     memset(storage_buffer, 0, storage_size);
16698d3bb68fSMatthias Ringwald     uint16_t buffer_size_per_bearer = ((storage_size - size_for_structs) / num_eatt_bearers);
16708d3bb68fSMatthias Ringwald     att_server_eatt_receive_buffer_size = buffer_size_per_bearer / 2;
16718d3bb68fSMatthias Ringwald     att_server_eatt_send_buffer_size    = buffer_size_per_bearer / 2;
1672735b0222SMatthias Ringwald     uint8_t * bearer_buffer = &storage_buffer[size_for_structs];
167320d4358dSMatthias Ringwald     uint8_t i;
167420d4358dSMatthias Ringwald     att_server_eatt_bearer_t * eatt_bearer = (att_server_eatt_bearer_t *) storage_buffer;
1675be4ca859SMatthias Ringwald     log_info("%u EATT bearers with receive buffer size %u",
1676be4ca859SMatthias Ringwald              num_eatt_bearers, att_server_eatt_receive_buffer_size);
167720d4358dSMatthias Ringwald     for (i=0;i<num_eatt_bearers;i++){
1678be4ca859SMatthias Ringwald         eatt_bearer->att_connection.con_handle = HCI_CON_HANDLE_INVALID;
1679735b0222SMatthias Ringwald         eatt_bearer->receive_buffer = bearer_buffer;
1680735b0222SMatthias Ringwald         bearer_buffer += att_server_eatt_receive_buffer_size;
16818d3bb68fSMatthias Ringwald         eatt_bearer->send_buffer = bearer_buffer;
16828d3bb68fSMatthias Ringwald         bearer_buffer += att_server_eatt_send_buffer_size;
16836b236cb0SMatthias Ringwald         btstack_linked_list_add(&att_server_eatt_bearer_pool, (btstack_linked_item_t *) eatt_bearer);
168420d4358dSMatthias Ringwald         eatt_bearer++;
168520d4358dSMatthias Ringwald     }
168620d4358dSMatthias Ringwald     // TODO: define minimum EATT MTU
1687*370c8b58SMatthias Ringwald     l2cap_ecbm_register_service(att_server_eatt_handler, BLUETOOTH_PSM_EATT, 64, LEVEL_2, false);
168820d4358dSMatthias Ringwald 
168920d4358dSMatthias Ringwald     return 0;
169020d4358dSMatthias Ringwald }
169120d4358dSMatthias Ringwald #endif
1692