xref: /btstack/src/ble/att_server.c (revision 26b42dcf160d9d6f335cdeb586248e2316a7bd05)
13deb3ec6SMatthias Ringwald /*
23deb3ec6SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
33deb3ec6SMatthias Ringwald  *
43deb3ec6SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
53deb3ec6SMatthias Ringwald  * modification, are permitted provided that the following conditions
63deb3ec6SMatthias Ringwald  * are met:
73deb3ec6SMatthias Ringwald  *
83deb3ec6SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
93deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
103deb3ec6SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
113deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
123deb3ec6SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
133deb3ec6SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
143deb3ec6SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
153deb3ec6SMatthias Ringwald  *    from this software without specific prior written permission.
163deb3ec6SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
173deb3ec6SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
183deb3ec6SMatthias Ringwald  *    monetary gain.
193deb3ec6SMatthias Ringwald  *
203deb3ec6SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
213deb3ec6SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
223deb3ec6SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
253deb3ec6SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
263deb3ec6SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
273deb3ec6SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
283deb3ec6SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
293deb3ec6SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
303deb3ec6SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313deb3ec6SMatthias Ringwald  * SUCH DAMAGE.
323deb3ec6SMatthias Ringwald  *
333deb3ec6SMatthias Ringwald  * Please inquire about commercial licensing options at
343deb3ec6SMatthias Ringwald  * [email protected]
353deb3ec6SMatthias Ringwald  *
363deb3ec6SMatthias Ringwald  */
373deb3ec6SMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "att_server.c"
39ab2c6ae4SMatthias Ringwald 
403deb3ec6SMatthias Ringwald 
413deb3ec6SMatthias Ringwald //
423deb3ec6SMatthias Ringwald // ATT Server Globals
433deb3ec6SMatthias Ringwald //
443deb3ec6SMatthias Ringwald 
453deb3ec6SMatthias Ringwald #include <stdint.h>
463deb3ec6SMatthias Ringwald #include <string.h>
473deb3ec6SMatthias Ringwald #include <inttypes.h>
483deb3ec6SMatthias Ringwald 
497907f069SMatthias Ringwald #include "btstack_config.h"
503deb3ec6SMatthias Ringwald 
51296289e3SMatthias Ringwald #include "ble/att_dispatch.h"
5259c6af15SMatthias Ringwald #include "ble/att_db.h"
5359c6af15SMatthias Ringwald #include "ble/att_server.h"
5459c6af15SMatthias Ringwald #include "ble/core.h"
5559c6af15SMatthias Ringwald #include "ble/le_device_db.h"
5659c6af15SMatthias Ringwald #include "ble/sm.h"
5716ece135SMatthias Ringwald #include "btstack_debug.h"
580e2df43fSMatthias Ringwald #include "btstack_event.h"
593deb3ec6SMatthias Ringwald #include "btstack_memory.h"
600e2df43fSMatthias Ringwald #include "btstack_run_loop.h"
6159c6af15SMatthias Ringwald #include "gap.h"
623deb3ec6SMatthias Ringwald #include "hci.h"
633deb3ec6SMatthias Ringwald #include "hci_dump.h"
643deb3ec6SMatthias Ringwald #include "l2cap.h"
65bf78aac6SMatthias Ringwald #include "btstack_tlv.h"
66d1a1f6a4SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
67d1a1f6a4SMatthias Ringwald #include "ble/sm.h"
68d1a1f6a4SMatthias Ringwald #endif
69bf78aac6SMatthias Ringwald 
7003f53d81SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT
7103f53d81SMatthias Ringwald #include <stdio.h>
7203f53d81SMatthias Ringwald #endif
7303f53d81SMatthias Ringwald 
746afb9acaSMatthias Ringwald #ifndef NVN_NUM_GATT_SERVER_CCC
756afb9acaSMatthias Ringwald #define NVN_NUM_GATT_SERVER_CCC 20
76bf78aac6SMatthias Ringwald #endif
773deb3ec6SMatthias Ringwald 
78cd45e970SMatthias Ringwald static void att_run_for_context(att_server_t * att_server, att_connection_t * att_connection);
7901ac2008SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle);
805d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle);
8148364364SMatthias Ringwald static void att_server_handle_can_send_now(void);
829a1f55eaSMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server, att_connection_t * att_connection);
83fe3d0cbaSMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server);
84*26b42dcfSMatthias Ringwald static void att_server_handle_att_pdu(att_server_t * att_server, att_connection_t * att_connection, uint8_t * packet, uint16_t size);
853deb3ec6SMatthias Ringwald 
863551936eSMatthias Ringwald typedef enum {
8738b893aaSMilanka Ringwald     ATT_SERVER_RUN_PHASE_1_REQUESTS = 0,
883551936eSMatthias Ringwald     ATT_SERVER_RUN_PHASE_2_INDICATIONS,
893551936eSMatthias Ringwald     ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS,
903551936eSMatthias Ringwald } att_server_run_phase_t;
913551936eSMatthias Ringwald 
926a540790SMatthias Ringwald //
936a540790SMatthias Ringwald typedef struct {
946a540790SMatthias Ringwald     uint32_t seq_nr;
956a540790SMatthias Ringwald     uint16_t att_handle;
966a540790SMatthias Ringwald     uint8_t  value;
976a540790SMatthias Ringwald     uint8_t  device_index;
986a540790SMatthias Ringwald } persistent_ccc_entry_t;
996a540790SMatthias Ringwald 
10018707219SMatthias Ringwald // global
1011a389cdcSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
102406722e4SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration;
10335d7f2dfSMilanka Ringwald static btstack_packet_handler_t               att_client_packet_handler;
1043d71c7a4SMatthias Ringwald static btstack_linked_list_t                  service_handlers;
1055f141511SMatthias Ringwald static btstack_context_callback_registration_t att_client_waiting_for_can_send_registration;
10618707219SMatthias Ringwald 
1073d71c7a4SMatthias Ringwald static att_read_callback_t                    att_server_client_read_callback;
1083d71c7a4SMatthias Ringwald static att_write_callback_t                   att_server_client_write_callback;
1093d71c7a4SMatthias Ringwald 
1106438547aSMatthias Ringwald // round robin
1116438547aSMatthias Ringwald static hci_con_handle_t att_server_last_can_send_now = HCI_CON_HANDLE_INVALID;
112bf78aac6SMatthias Ringwald 
11302b78fc8SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
11426567ee7SMatthias 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){
11518707219SMatthias Ringwald     btstack_linked_list_iterator_t it;
11618707219SMatthias Ringwald     hci_connections_get_iterator(&it);
11718707219SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
11826567ee7SMatthias Ringwald         hci_connection_t * hci_connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
11926567ee7SMatthias Ringwald         if (hci_connection->att_server.state == state) {
12026567ee7SMatthias Ringwald             *att_server_out     = &hci_connection->att_server;
12126567ee7SMatthias Ringwald             *att_connection_out = &hci_connection->att_connection;
12226567ee7SMatthias Ringwald             return true;
12318707219SMatthias Ringwald         }
12426567ee7SMatthias Ringwald     }
12526567ee7SMatthias Ringwald     return false;
12618707219SMatthias Ringwald }
12702b78fc8SMatthias Ringwald #endif
128bb38f057SMatthias Ringwald 
1293f9b77dbSMatthias Ringwald static void att_server_request_can_send_now(att_server_t * att_server, att_connection_t * att_connection ){
13023079b39SMatthias Ringwald     switch (att_server->bearer_type){
13123079b39SMatthias Ringwald         case ATT_BEARER_UNENHANCED_LE:
132b6df12b6SMatthias Ringwald             att_dispatch_server_request_can_send_now_event(att_connection->con_handle);
13323079b39SMatthias Ringwald             break;
13423079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
13523079b39SMatthias Ringwald         case ATT_BEARER_UNENHANCED_CLASSIC:
13623079b39SMatthias Ringwald             l2cap_request_can_send_now_event(att_server->l2cap_cid);
13723079b39SMatthias Ringwald             break;
13823079b39SMatthias Ringwald #endif
13923079b39SMatthias Ringwald         default:
14023079b39SMatthias Ringwald             btstack_unreachable();
14123079b39SMatthias Ringwald             break;
14223079b39SMatthias Ringwald     }
143c8c6e9b4SMatthias Ringwald }
144c8c6e9b4SMatthias Ringwald 
14537a8059dSMatthias Ringwald static bool att_server_can_send_packet(att_server_t * att_server, att_connection_t * att_connection){
14623079b39SMatthias Ringwald     switch (att_server->bearer_type) {
14723079b39SMatthias Ringwald         case ATT_BEARER_UNENHANCED_LE:
1481979f09cSMatthias Ringwald             return att_dispatch_server_can_send_now(att_connection->con_handle) != 0;
14923079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
15023079b39SMatthias Ringwald         case ATT_BEARER_UNENHANCED_CLASSIC:
15123079b39SMatthias Ringwald             return l2cap_can_send_packet_now(att_server->l2cap_cid) != 0;
15223079b39SMatthias Ringwald #endif
15323079b39SMatthias Ringwald         default:
15423079b39SMatthias Ringwald             btstack_unreachable();
15523079b39SMatthias Ringwald             break;
15623079b39SMatthias Ringwald     }
15723079b39SMatthias Ringwald     return false;
158c0f26e1cSMatthias Ringwald }
159c0f26e1cSMatthias Ringwald 
1603deb3ec6SMatthias Ringwald static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){
1615d07396bSMatthias Ringwald     btstack_packet_handler_t packet_handler = att_server_packet_handler_for_handle(attribute_handle);
1625d07396bSMatthias Ringwald     if (!packet_handler) return;
1633deb3ec6SMatthias Ringwald 
1643deb3ec6SMatthias Ringwald     uint8_t event[7];
1653deb3ec6SMatthias Ringwald     int pos = 0;
1665611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE;
1674ea43905SMatthias Ringwald     event[pos++] = sizeof(event) - 2u;
1683deb3ec6SMatthias Ringwald     event[pos++] = status;
169f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, client_handle);
1703deb3ec6SMatthias Ringwald     pos += 2;
171f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, attribute_handle);
172a444112bSMatthias Ringwald     (*packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1733deb3ec6SMatthias Ringwald }
1743deb3ec6SMatthias Ringwald 
1755d07396bSMatthias Ringwald static void att_emit_event_to_all(const uint8_t * event, uint16_t size){
1765d07396bSMatthias Ringwald     // dispatch to app level handler
1779305033eSMatthias Ringwald     if (att_client_packet_handler != NULL){
1785d07396bSMatthias Ringwald         (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size);
1795d07396bSMatthias Ringwald     }
1803deb3ec6SMatthias Ringwald 
1815d07396bSMatthias Ringwald     // dispatch to service handlers
1825d07396bSMatthias Ringwald     btstack_linked_list_iterator_t it;
1835d07396bSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
1845d07396bSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1855d07396bSMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
1865d07396bSMatthias Ringwald         if (!handler->packet_handler) continue;
1875d07396bSMatthias Ringwald         (*handler->packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size);
1885d07396bSMatthias Ringwald     }
1895d07396bSMatthias Ringwald }
1905d07396bSMatthias Ringwald 
1915d07396bSMatthias Ringwald static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){
1923deb3ec6SMatthias Ringwald     uint8_t event[6];
1933deb3ec6SMatthias Ringwald     int pos = 0;
1945611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE;
1954ea43905SMatthias Ringwald     event[pos++] = sizeof(event) - 2u;
196fc64f94aSMatthias Ringwald     little_endian_store_16(event, pos, con_handle);
1973deb3ec6SMatthias Ringwald     pos += 2;
198f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, mtu);
1995d07396bSMatthias Ringwald 
2005d07396bSMatthias Ringwald     // also dispatch to GATT Clients
201b12646c5SJakob Krantz     att_dispatch_server_mtu_exchanged(con_handle, mtu);
2025d07396bSMatthias Ringwald 
2035d07396bSMatthias Ringwald     // dispatch to app level handler and service handlers
2045d07396bSMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
2053deb3ec6SMatthias Ringwald }
2063deb3ec6SMatthias Ringwald 
2075f141511SMatthias Ringwald static void att_emit_can_send_now_event(void * context){
2085f141511SMatthias Ringwald     UNUSED(context);
2093c97b242SMatthias Ringwald     if (!att_client_packet_handler) return;
2103c97b242SMatthias Ringwald 
2111b96b007SMatthias Ringwald     uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0};
2121b96b007SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
2131b96b007SMatthias Ringwald }
2141b96b007SMatthias Ringwald 
2156d2214cbSMatthias Ringwald static void att_emit_connected_event(att_server_t * att_server,  att_connection_t * att_connection){
216ff1bec95SMatthias Ringwald     uint8_t event[11];
217ff1bec95SMatthias Ringwald     int pos = 0;
218ff1bec95SMatthias Ringwald     event[pos++] = ATT_EVENT_CONNECTED;
2194ea43905SMatthias Ringwald     event[pos++] = sizeof(event) - 2u;
220ff1bec95SMatthias Ringwald     event[pos++] = att_server->peer_addr_type;
221ff1bec95SMatthias Ringwald     reverse_bd_addr(att_server->peer_address, &event[pos]);
222ff1bec95SMatthias Ringwald     pos += 6;
223b6df12b6SMatthias Ringwald     little_endian_store_16(event, pos, att_connection->con_handle);
224ff1bec95SMatthias Ringwald     pos += 2;
225ff1bec95SMatthias Ringwald 
226ff1bec95SMatthias Ringwald     // dispatch to app level handler and service handlers
227ff1bec95SMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
228ff1bec95SMatthias Ringwald }
229ff1bec95SMatthias Ringwald 
230ff1bec95SMatthias Ringwald 
2316187ad4cSMatthias Ringwald static void att_emit_disconnected_event(uint16_t con_handle){
232ff1bec95SMatthias Ringwald     uint8_t event[4];
233ff1bec95SMatthias Ringwald     int pos = 0;
234ff1bec95SMatthias Ringwald     event[pos++] = ATT_EVENT_DISCONNECTED;
2354ea43905SMatthias Ringwald     event[pos++] = sizeof(event) - 2u;
2366187ad4cSMatthias Ringwald     little_endian_store_16(event, pos, con_handle);
237ff1bec95SMatthias Ringwald     pos += 2;
238ff1bec95SMatthias Ringwald 
239ff1bec95SMatthias Ringwald     // dispatch to app level handler and service handlers
240ff1bec95SMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
241ff1bec95SMatthias Ringwald }
242ff1bec95SMatthias Ringwald 
243ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){
24454178543SMatthias Ringwald     void * context = btstack_run_loop_get_timer_context(ts);
24554178543SMatthias Ringwald     hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
24668591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
24768591e36SMatthias Ringwald     if (!hci_connection) return;
24868591e36SMatthias Ringwald     // @note: after a transaction timeout, no more requests shall be sent over this ATT Bearer
249bce48a26SMatthias Ringwald     // (that's why we don't reset the value_indication_handle)
250b6df12b6SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
25118707219SMatthias Ringwald     uint16_t att_handle = att_server->value_indication_handle;
252388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
25338b893aaSMilanka Ringwald     att_handle_value_indication_notify_client((uint8_t)ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_connection->con_handle, att_handle);
2543deb3ec6SMatthias Ringwald }
2553deb3ec6SMatthias Ringwald 
2563deb3ec6SMatthias Ringwald static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2579ec2630cSMatthias Ringwald 
2587dad9ff8SMatthias Ringwald     UNUSED(channel); // ok: there is no channel
2597dad9ff8SMatthias Ringwald     UNUSED(size);    // ok: handling own l2cap events
2603deb3ec6SMatthias Ringwald 
26118707219SMatthias Ringwald     att_server_t * att_server;
262b6df12b6SMatthias Ringwald     att_connection_t * att_connection;
26318707219SMatthias Ringwald     hci_con_handle_t con_handle;
26468591e36SMatthias Ringwald     hci_connection_t * hci_connection;
26570dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
26670dca4d4SMatthias Ringwald     bd_addr_t address;
26768591e36SMatthias Ringwald     btstack_linked_list_iterator_t it;
26870dca4d4SMatthias Ringwald #endif
26918707219SMatthias Ringwald 
2703deb3ec6SMatthias Ringwald     switch (packet_type) {
2713deb3ec6SMatthias Ringwald 
2723deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
2730e2df43fSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
2743deb3ec6SMatthias Ringwald 
27570dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
27670dca4d4SMatthias Ringwald                 case L2CAP_EVENT_INCOMING_CONNECTION:
27770dca4d4SMatthias Ringwald                     l2cap_event_incoming_connection_get_address(packet, address);
27870dca4d4SMatthias Ringwald                     l2cap_accept_connection(channel);
27936de8547SMatthias Ringwald                     log_info("Accept incoming connection from %s", bd_addr_to_str(address));
28070dca4d4SMatthias Ringwald                     break;
28170dca4d4SMatthias Ringwald                 case L2CAP_EVENT_CHANNEL_OPENED:
282122b723eSMatthias Ringwald                     con_handle = l2cap_event_channel_opened_get_handle(packet);
28368591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
28468591e36SMatthias Ringwald                     if (!hci_connection) break;
28568591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
286122b723eSMatthias Ringwald                     // store connection info
28723079b39SMatthias Ringwald                     att_server->bearer_type = ATT_BEARER_UNENHANCED_CLASSIC;
288f16129ceSMatthias Ringwald                     att_server->peer_addr_type = BD_ADDR_TYPE_ACL;
289122b723eSMatthias Ringwald                     l2cap_event_channel_opened_get_address(packet, att_server->peer_address);
290388fa7c4SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
291b6df12b6SMatthias Ringwald                     att_connection->con_handle = con_handle;
29270dca4d4SMatthias Ringwald                     att_server->l2cap_cid = l2cap_event_channel_opened_get_local_cid(packet);
293122b723eSMatthias Ringwald                     // reset connection properties
294122b723eSMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
295b6df12b6SMatthias Ringwald                     att_connection->mtu = l2cap_event_channel_opened_get_remote_mtu(packet);
296b6df12b6SMatthias Ringwald                     att_connection->max_mtu = l2cap_max_mtu();
297b6df12b6SMatthias Ringwald                     if (att_connection->max_mtu > ATT_REQUEST_BUFFER_SIZE){
298b6df12b6SMatthias Ringwald                         att_connection->max_mtu = ATT_REQUEST_BUFFER_SIZE;
299122b723eSMatthias Ringwald                     }
300df33ee14SMatthias Ringwald 
301b6df12b6SMatthias Ringwald                     log_info("Connection opened %s, l2cap cid %04x, mtu %u", bd_addr_to_str(address), att_server->l2cap_cid, att_connection->mtu);
302df33ee14SMatthias Ringwald 
303914988a1SMatthias Ringwald                     // update security params
304b6df12b6SMatthias Ringwald                     att_connection->encryption_key_size = gap_encryption_key_size(con_handle);
305b6df12b6SMatthias Ringwald                     att_connection->authenticated = gap_authenticated(con_handle);
306b6df12b6SMatthias Ringwald                     att_connection->secure_connection = gap_secure_connection(con_handle);
307914988a1SMatthias Ringwald                     log_info("encrypted key size %u, authenticated %u, secure connection %u",
308b6df12b6SMatthias Ringwald                         att_connection->encryption_key_size, att_connection->authenticated, att_connection->secure_connection);
309df33ee14SMatthias Ringwald 
310df33ee14SMatthias Ringwald                     // notify connection opened
3116d2214cbSMatthias Ringwald                     att_emit_connected_event(att_server, att_connection);
312df33ee14SMatthias Ringwald 
313914988a1SMatthias Ringwald                     // restore persisten ccc if encrypted
314914988a1SMatthias Ringwald                     if ( gap_security_level(con_handle) >= LEVEL_2){
3159a1f55eaSMatthias Ringwald                         att_server_persistent_ccc_restore(att_server, att_connection);
316914988a1SMatthias Ringwald                     }
317122b723eSMatthias Ringwald                     // TODO: what to do about le device db?
318122b723eSMatthias Ringwald                     att_server->pairing_active = 0;
31970dca4d4SMatthias Ringwald                     break;
32048364364SMatthias Ringwald                 case L2CAP_EVENT_CAN_SEND_NOW:
32148364364SMatthias Ringwald                     att_server_handle_can_send_now();
32248364364SMatthias Ringwald                     break;
32348364364SMatthias Ringwald 
32470dca4d4SMatthias Ringwald #endif
3253deb3ec6SMatthias Ringwald                 case HCI_EVENT_LE_META:
3263deb3ec6SMatthias Ringwald                     switch (packet[2]) {
3273deb3ec6SMatthias Ringwald                         case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
32818707219SMatthias Ringwald                             con_handle = little_endian_read_16(packet, 4);
32968591e36SMatthias Ringwald                             hci_connection = hci_connection_for_handle(con_handle);
33068591e36SMatthias Ringwald                             if (!hci_connection) break;
33168591e36SMatthias Ringwald                             att_server = &hci_connection->att_server;
3323deb3ec6SMatthias Ringwald                         	// store connection info
33318707219SMatthias Ringwald                         	att_server->peer_addr_type = packet[7];
33418707219SMatthias Ringwald                             reverse_bd_addr(&packet[8], att_server->peer_address);
335388fa7c4SMatthias Ringwald                             att_connection = &hci_connection->att_connection;
336b6df12b6SMatthias Ringwald                             att_connection->con_handle = con_handle;
3373deb3ec6SMatthias Ringwald                             // reset connection properties
33818707219SMatthias Ringwald                             att_server->state = ATT_SERVER_IDLE;
33923079b39SMatthias Ringwald                             att_server->bearer_type = ATT_BEARER_UNENHANCED_LE;
340b6df12b6SMatthias Ringwald                             att_connection->mtu = ATT_DEFAULT_MTU;
341b6df12b6SMatthias Ringwald                             att_connection->max_mtu = l2cap_max_le_mtu();
342b6df12b6SMatthias Ringwald                             if (att_connection->max_mtu > ATT_REQUEST_BUFFER_SIZE){
343b6df12b6SMatthias Ringwald                                 att_connection->max_mtu = ATT_REQUEST_BUFFER_SIZE;
34499c58ad0SMatthias Ringwald                             }
34538b893aaSMilanka Ringwald                             att_connection->encryption_key_size = 0u;
34638b893aaSMilanka Ringwald                             att_connection->authenticated = 0u;
34738b893aaSMilanka Ringwald 		                	att_connection->authorized = 0u;
348b2c1e52cSMatthias Ringwald                             // workaround: identity resolving can already be complete, at least store result
349b2c1e52cSMatthias Ringwald                             att_server->ir_le_device_db_index = sm_le_device_index(con_handle);
35038b893aaSMilanka Ringwald                             att_server->ir_lookup_active = 0u;
35138b893aaSMilanka Ringwald                             att_server->pairing_active = 0u;
352ff1bec95SMatthias Ringwald                             // notify all - old
3535d07396bSMatthias Ringwald                             att_emit_event_to_all(packet, size);
354ff1bec95SMatthias Ringwald                             // notify all - new
3556d2214cbSMatthias Ringwald                             att_emit_connected_event(att_server, att_connection);
3563deb3ec6SMatthias Ringwald                             break;
3573deb3ec6SMatthias Ringwald 
3583deb3ec6SMatthias Ringwald                         default:
3593deb3ec6SMatthias Ringwald                             break;
3603deb3ec6SMatthias Ringwald                     }
3613deb3ec6SMatthias Ringwald                     break;
3623deb3ec6SMatthias Ringwald 
3633deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_CHANGE:
3648601e696SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_CHANGE_V2:
3653deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE:
3663deb3ec6SMatthias Ringwald                 	// check handle
36718707219SMatthias Ringwald                     con_handle = little_endian_read_16(packet, 3);
36868591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
36968591e36SMatthias Ringwald                     if (!hci_connection) break;
370914988a1SMatthias Ringwald                     if (gap_get_connection_type(con_handle) != GAP_CONNECTION_LE) break;
371914988a1SMatthias Ringwald                     // update security params
37268591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
373388fa7c4SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
374b6df12b6SMatthias Ringwald                     att_connection->encryption_key_size = gap_encryption_key_size(con_handle);
375ff3f1026SMatthias Ringwald                     att_connection->authenticated = gap_authenticated(con_handle) ? 1 : 0;
376f461b3dfSMatthias Ringwald                     att_connection->secure_connection = gap_secure_connection(con_handle) ? 1 : 0;
377914988a1SMatthias Ringwald                     log_info("encrypted key size %u, authenticated %u, secure connection %u",
378b6df12b6SMatthias Ringwald                         att_connection->encryption_key_size, att_connection->authenticated, att_connection->secure_connection);
379914988a1SMatthias Ringwald                     if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){
3804097998aSMatthias Ringwald                         // restore CCC values when encrypted for LE Connections
38122bfc225SMilanka Ringwald                         if (hci_event_encryption_change_get_encryption_enabled(packet) != 0){
3829a1f55eaSMatthias Ringwald                             att_server_persistent_ccc_restore(att_server, att_connection);
38301ac2008SMatthias Ringwald                         }
38401ac2008SMatthias Ringwald                     }
385cd45e970SMatthias Ringwald                     att_run_for_context(att_server, att_connection);
3863deb3ec6SMatthias Ringwald                     break;
3873deb3ec6SMatthias Ringwald 
3883deb3ec6SMatthias Ringwald                 case HCI_EVENT_DISCONNECTION_COMPLETE:
38970a390c7SMatthias Ringwald                     // check handle
39018707219SMatthias Ringwald                     con_handle = hci_event_disconnection_complete_get_connection_handle(packet);
39168591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
39268591e36SMatthias Ringwald                     if (!hci_connection) break;
39368591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
394388fa7c4SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
395b6df12b6SMatthias Ringwald                     att_clear_transaction_queue(att_connection);
396b6df12b6SMatthias Ringwald                     att_connection->con_handle = 0;
397760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
39818707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
39938b893aaSMilanka Ringwald                     if (att_server->value_indication_handle != 0u){
40027328ecbSMatthias Ringwald                         btstack_run_loop_remove_timer(&att_server->value_indication_timer);
401bce48a26SMatthias Ringwald                         uint16_t att_handle = att_server->value_indication_handle;
40238b893aaSMilanka Ringwald                         att_server->value_indication_handle = 0u; // reset error state
40338b893aaSMilanka Ringwald                         att_handle_value_indication_notify_client((uint8_t)ATT_HANDLE_VALUE_INDICATION_DISCONNECT, att_connection->con_handle, att_handle);
404bce48a26SMatthias Ringwald                     }
405ff1bec95SMatthias Ringwald                     // notify all - new
4066187ad4cSMatthias Ringwald                     att_emit_disconnected_event(con_handle);
407ff1bec95SMatthias Ringwald                     // notify all - old
4085d07396bSMatthias Ringwald                     att_emit_event_to_all(packet, size);
4093deb3ec6SMatthias Ringwald                     break;
4103deb3ec6SMatthias Ringwald 
411760ad805SMatthias Ringwald                 // Identity Resolving
4125611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_STARTED:
41318707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_started_get_handle(packet);
41468591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
41568591e36SMatthias Ringwald                     if (!hci_connection) break;
41668591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
4175611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED");
41830c8e114SMatthias Ringwald                     att_server->ir_lookup_active = 1;
4193deb3ec6SMatthias Ringwald                     break;
4205611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
421760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
42268591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
42368591e36SMatthias Ringwald                     if (!hci_connection) return;
42468591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
42530c8e114SMatthias Ringwald                     att_server->ir_lookup_active = 0;
4261b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index(packet);
4271b7acd0dSMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED");
428cd45e970SMatthias Ringwald                     att_run_for_context(att_server, att_connection);
4293deb3ec6SMatthias Ringwald                     break;
4305611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_FAILED:
43118707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_failed_get_handle(packet);
43268591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
43368591e36SMatthias Ringwald                     if (!hci_connection) break;
43468591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
4355611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED");
43630c8e114SMatthias Ringwald                     att_server->ir_lookup_active = 0;
43718707219SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
438cd45e970SMatthias Ringwald                     att_run_for_context(att_server, att_connection);
4393deb3ec6SMatthias Ringwald                     break;
440760ad805SMatthias Ringwald 
441760ad805SMatthias Ringwald                 // Pairing started - delete stored CCC values
442760ad805SMatthias Ringwald                 // - assumes pairing indicates either new device or re-pairing, in both cases there should be no stored CCC values
443760ad805SMatthias Ringwald                 // - assumes that all events have the con handle as the first field
444760ad805SMatthias Ringwald                 case SM_EVENT_JUST_WORKS_REQUEST:
445760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
446760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_INPUT_NUMBER:
447760ad805SMatthias Ringwald                 case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
448760ad805SMatthias Ringwald                     con_handle = sm_event_just_works_request_get_handle(packet);
44968591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
45068591e36SMatthias Ringwald                     if (!hci_connection) break;
45168591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
452760ad805SMatthias Ringwald                     att_server->pairing_active = 1;
453760ad805SMatthias Ringwald                     log_info("SM Pairing started");
454760ad805SMatthias Ringwald                     if (att_server->ir_le_device_db_index < 0) break;
455cd45e970SMatthias Ringwald                     att_run_for_context(att_server, att_connection);
456760ad805SMatthias Ringwald                     // index not valid anymore
457760ad805SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
458760ad805SMatthias Ringwald                     break;
459760ad805SMatthias Ringwald 
4601b7acd0dSMatthias Ringwald                 // Bonding completed
461760ad805SMatthias Ringwald                 case SM_EVENT_IDENTITY_CREATED:
462760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
46368591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
46468591e36SMatthias Ringwald                     if (!hci_connection) return;
46568591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
466760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
4671b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_created_get_index(packet);
468cd45e970SMatthias Ringwald                     att_run_for_context(att_server, att_connection);
4691b7acd0dSMatthias Ringwald                     break;
4701b7acd0dSMatthias Ringwald 
4711b7acd0dSMatthias Ringwald                 // Pairing complete (with/without bonding=storing of pairing information)
4721b7acd0dSMatthias Ringwald                 case SM_EVENT_PAIRING_COMPLETE:
4731b7acd0dSMatthias Ringwald                     con_handle = sm_event_pairing_complete_get_handle(packet);
47468591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
47568591e36SMatthias Ringwald                     if (!hci_connection) return;
47668591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
4771b7acd0dSMatthias Ringwald                     att_server->pairing_active = 0;
478cd45e970SMatthias Ringwald                     att_run_for_context(att_server, att_connection);
479760ad805SMatthias Ringwald                     break;
480760ad805SMatthias Ringwald 
481760ad805SMatthias Ringwald                 // Authorization
4825611a760SMatthias Ringwald                 case SM_EVENT_AUTHORIZATION_RESULT: {
48318707219SMatthias Ringwald                     con_handle = sm_event_authorization_result_get_handle(packet);
48468591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
48568591e36SMatthias Ringwald                     if (!hci_connection) break;
48668591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
487388fa7c4SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
488b6df12b6SMatthias Ringwald                     att_connection->authorized = sm_event_authorization_result_get_authorization_result(packet);
4893f9b77dbSMatthias Ringwald                     att_server_request_can_send_now(att_server, att_connection);
4903deb3ec6SMatthias Ringwald                 	break;
4913deb3ec6SMatthias Ringwald                 }
4923deb3ec6SMatthias Ringwald                 default:
4933deb3ec6SMatthias Ringwald                     break;
4943deb3ec6SMatthias Ringwald             }
49565b44ffdSMatthias Ringwald             break;
49670dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
49770dca4d4SMatthias Ringwald         case L2CAP_DATA_PACKET:
49868591e36SMatthias Ringwald             hci_connections_get_iterator(&it);
49968591e36SMatthias Ringwald             while(btstack_linked_list_iterator_has_next(&it)){
500388fa7c4SMatthias Ringwald                 hci_connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
501388fa7c4SMatthias Ringwald                 att_server = &hci_connection->att_server;
502*26b42dcfSMatthias Ringwald                 att_connection = &hci_connection->att_connection;
50368591e36SMatthias Ringwald                 if (att_server->l2cap_cid == channel) {
504*26b42dcfSMatthias Ringwald                     att_server_handle_att_pdu(att_server, att_connection, packet, size);
50568591e36SMatthias Ringwald                     break;
50668591e36SMatthias Ringwald                 }
50768591e36SMatthias Ringwald             }
50870dca4d4SMatthias Ringwald             break;
50970dca4d4SMatthias Ringwald #endif
51041772f37SMatthias Ringwald 
51165b44ffdSMatthias Ringwald         default:
51265b44ffdSMatthias Ringwald             break;
5133deb3ec6SMatthias Ringwald     }
5143deb3ec6SMatthias Ringwald }
5153deb3ec6SMatthias Ringwald 
516d415948cSMatthias Ringwald static uint8_t att_server_send_prepared(const att_server_t *att_server, const att_connection_t *att_connection, uint16_t size) {
517d415948cSMatthias Ringwald     uint8_t status = ERROR_CODE_SUCCESS;
518d415948cSMatthias Ringwald     switch (att_server->bearer_type) {
519d415948cSMatthias Ringwald         case ATT_BEARER_UNENHANCED_LE:
520d415948cSMatthias Ringwald             status = l2cap_send_prepared_connectionless(att_connection->con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
521d415948cSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
522d415948cSMatthias Ringwald         case ATT_BEARER_UNENHANCED_CLASSIC:
523d415948cSMatthias Ringwald             status = l2cap_send_prepared(att_server->l2cap_cid, size);
524d415948cSMatthias Ringwald             break;
525d415948cSMatthias Ringwald #endif
526d415948cSMatthias Ringwald         default:
527d415948cSMatthias Ringwald             btstack_unreachable();
528d415948cSMatthias Ringwald             break;
529d415948cSMatthias Ringwald     }
530d415948cSMatthias Ringwald     return status;
531d415948cSMatthias Ringwald }
532d415948cSMatthias Ringwald 
5337a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
5343deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
53526567ee7SMatthias Ringwald     att_server_t * att_server = NULL;
53626567ee7SMatthias Ringwald     att_connection_t * att_connection = NULL;
53726567ee7SMatthias Ringwald     bool found = att_server_connections_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION, &att_server, &att_connection);
5383deb3ec6SMatthias Ringwald 
5393deb3ec6SMatthias Ringwald     uint8_t hash_flipped[8];
5409c80e4ccSMatthias Ringwald     reverse_64(hash, hash_flipped);
54126567ee7SMatthias Ringwald     if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8) != 0){
5423deb3ec6SMatthias Ringwald         log_info("ATT Signed Write, invalid signature");
54303f53d81SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT
54403f53d81SMatthias Ringwald         printf("ATT Signed Write, invalid signature\n");
54503f53d81SMatthias Ringwald #endif
54618707219SMatthias Ringwald         att_server->state = ATT_SERVER_IDLE;
5473deb3ec6SMatthias Ringwald         return;
5483deb3ec6SMatthias Ringwald     }
5493deb3ec6SMatthias Ringwald     log_info("ATT Signed Write, valid signature");
55003f53d81SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT
55103f53d81SMatthias Ringwald     printf("ATT Signed Write, valid signature\n");
55203f53d81SMatthias Ringwald #endif
5533deb3ec6SMatthias Ringwald 
5543deb3ec6SMatthias Ringwald     // update sequence number
55518707219SMatthias Ringwald     uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
55618707219SMatthias Ringwald     le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1);
55718707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
5583f9b77dbSMatthias Ringwald     att_server_request_can_send_now(att_server, att_connection);
5593deb3ec6SMatthias Ringwald }
5607a766ebfSMatthias Ringwald #endif
56188a48dd8SMatthias Ringwald 
56218707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED
563b06f2579SMatthias Ringwald // pre: can send now
564b06f2579SMatthias Ringwald // returns: 1 if packet was sent
5659fe63f3aSMatthias Ringwald static int att_server_process_validated_request(att_server_t * att_server, att_connection_t * att_connection){
566b06f2579SMatthias Ringwald 
567b06f2579SMatthias Ringwald     l2cap_reserve_packet_buffer();
568b06f2579SMatthias Ringwald     uint8_t * att_response_buffer = l2cap_get_outgoing_buffer();
569b6df12b6SMatthias Ringwald     uint16_t  att_response_size   = att_handle_request(att_connection, att_server->request_buffer, att_server->request_size, att_response_buffer);
570b06f2579SMatthias Ringwald 
571beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
572c1ab6cc1SMatthias Ringwald     if ((att_response_size == ATT_READ_RESPONSE_PENDING) || (att_response_size == ATT_INTERNAL_WRITE_RESPONSE_PENDING)){
573e404a688SMatthias Ringwald         // update state
574beb20288SMatthias Ringwald         att_server->state = ATT_SERVER_RESPONSE_PENDING;
575e404a688SMatthias Ringwald 
57656c3a347SMatthias Ringwald         // callback with handle ATT_READ_RESPONSE_PENDING for reads
57756c3a347SMatthias Ringwald         if (att_response_size == ATT_READ_RESPONSE_PENDING){
578b6df12b6SMatthias Ringwald             att_server_client_read_callback(att_connection->con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0);
57956c3a347SMatthias Ringwald         }
580e8e7403eSMatthias Ringwald 
581e8e7403eSMatthias Ringwald         // free reserved buffer
582e8e7403eSMatthias Ringwald         l2cap_release_packet_buffer();
583e8e7403eSMatthias Ringwald         return 0;
584e404a688SMatthias Ringwald     }
585e404a688SMatthias Ringwald #endif
586e404a688SMatthias Ringwald 
587b06f2579SMatthias Ringwald     // intercept "insufficient authorization" for authenticated connections to allow for user authorization
5884ea43905SMatthias Ringwald     if ((att_response_size     >= 4u)
589b06f2579SMatthias Ringwald     && (att_response_buffer[0] == ATT_ERROR_RESPONSE)
590b06f2579SMatthias Ringwald     && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION)
59138b893aaSMilanka Ringwald     && (att_connection->authenticated != 0u)){
592b06f2579SMatthias Ringwald 
593b6df12b6SMatthias Ringwald         switch (gap_authorization_state(att_connection->con_handle)){
594b06f2579SMatthias Ringwald             case AUTHORIZATION_UNKNOWN:
595b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
596b6df12b6SMatthias Ringwald                 sm_request_pairing(att_connection->con_handle);
597b06f2579SMatthias Ringwald                 return 0;
598b06f2579SMatthias Ringwald             case AUTHORIZATION_PENDING:
599b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
600b06f2579SMatthias Ringwald                 return 0;
601b06f2579SMatthias Ringwald             default:
602b06f2579SMatthias Ringwald                 break;
603b06f2579SMatthias Ringwald         }
604b06f2579SMatthias Ringwald     }
605b06f2579SMatthias Ringwald 
60618707219SMatthias Ringwald     att_server->state = ATT_SERVER_IDLE;
6074ea43905SMatthias Ringwald     if (att_response_size == 0u) {
608b06f2579SMatthias Ringwald         l2cap_release_packet_buffer();
609b06f2579SMatthias Ringwald         return 0;
610b06f2579SMatthias Ringwald     }
611b06f2579SMatthias Ringwald 
612d415948cSMatthias Ringwald     (void) att_server_send_prepared(att_server, att_connection, att_response_size);
613b06f2579SMatthias Ringwald 
614b06f2579SMatthias Ringwald     // notify client about MTU exchange result
615b06f2579SMatthias Ringwald     if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){
616b6df12b6SMatthias Ringwald         att_emit_mtu_event(att_connection->con_handle, att_connection->mtu);
617b06f2579SMatthias Ringwald     }
618b06f2579SMatthias Ringwald     return 1;
619b06f2579SMatthias Ringwald }
620b06f2579SMatthias Ringwald 
621beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
6229816429dSMatthias Ringwald uint8_t att_server_response_ready(hci_con_handle_t con_handle){
62368591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
62468591e36SMatthias Ringwald     if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
62568591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
6263f9b77dbSMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
627beb20288SMatthias Ringwald     if (att_server->state != ATT_SERVER_RESPONSE_PENDING)   return ERROR_CODE_COMMAND_DISALLOWED;
628e404a688SMatthias Ringwald 
629e404a688SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
6303f9b77dbSMatthias Ringwald     att_server_request_can_send_now(att_server, att_connection);
631e404a688SMatthias Ringwald     return ERROR_CODE_SUCCESS;
632e404a688SMatthias Ringwald }
633e404a688SMatthias Ringwald #endif
634e404a688SMatthias Ringwald 
635cd45e970SMatthias Ringwald static void att_run_for_context(att_server_t * att_server, att_connection_t * att_connection){
63618707219SMatthias Ringwald     switch (att_server->state){
6373deb3ec6SMatthias Ringwald         case ATT_SERVER_REQUEST_RECEIVED:
63823079b39SMatthias Ringwald             switch (att_server->bearer_type){
63923079b39SMatthias Ringwald                 case ATT_BEARER_UNENHANCED_LE:
6400234fbbcSMatthias Ringwald                     // wait until re-encryption as central is complete
64123079b39SMatthias Ringwald                     if (gap_reconnect_security_setup_active(att_connection->con_handle)) {
64223079b39SMatthias Ringwald                         return;
64323079b39SMatthias Ringwald                     };
64423079b39SMatthias Ringwald                     break;
64523079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
64623079b39SMatthias Ringwald                 case ATT_BEARER_UNENHANCED_CLASSIC:
64723079b39SMatthias Ringwald                     // ok
64823079b39SMatthias Ringwald                     break;
64923079b39SMatthias Ringwald #endif
65023079b39SMatthias Ringwald                 default:
65123079b39SMatthias Ringwald                     btstack_unreachable();
65223079b39SMatthias Ringwald                     break;
65348364364SMatthias Ringwald             }
6540234fbbcSMatthias Ringwald 
655760ad805SMatthias Ringwald             // wait until pairing is complete
656760ad805SMatthias Ringwald             if (att_server->pairing_active) break;
657760ad805SMatthias Ringwald 
6587a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
65918707219SMatthias Ringwald             if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){
6603deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write!");
6613deb3ec6SMatthias Ringwald                 if (!sm_cmac_ready()) {
6623deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, sm_cmac engine not ready. Abort");
66318707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
6643deb3ec6SMatthias Ringwald                     return;
6653deb3ec6SMatthias Ringwald                 }
66618707219SMatthias Ringwald                 if (att_server->request_size < (3 + 12)) {
6673deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, request to short. Abort.");
66818707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
6693deb3ec6SMatthias Ringwald                     return;
6703deb3ec6SMatthias Ringwald                 }
67118707219SMatthias Ringwald                 if (att_server->ir_lookup_active){
6723deb3ec6SMatthias Ringwald                     return;
6733deb3ec6SMatthias Ringwald                 }
67418707219SMatthias Ringwald                 if (att_server->ir_le_device_db_index < 0){
6753deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, CSRK not available");
67618707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
6773deb3ec6SMatthias Ringwald                     return;
6783deb3ec6SMatthias Ringwald                 }
6793deb3ec6SMatthias Ringwald 
6803deb3ec6SMatthias Ringwald                 // check counter
68118707219SMatthias Ringwald                 uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
68218707219SMatthias Ringwald                 uint32_t counter_db     = le_device_db_remote_counter_get(att_server->ir_le_device_db_index);
6833deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet);
6843deb3ec6SMatthias Ringwald                 if (counter_packet < counter_db){
6853deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, db reports higher counter, abort");
68618707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
6873deb3ec6SMatthias Ringwald                     return;
6883deb3ec6SMatthias Ringwald                 }
6893deb3ec6SMatthias Ringwald 
6903deb3ec6SMatthias Ringwald                 // signature is { sequence counter, secure hash }
6913deb3ec6SMatthias Ringwald                 sm_key_t csrk;
69218707219SMatthias Ringwald                 le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk);
69318707219SMatthias Ringwald                 att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION;
6943deb3ec6SMatthias Ringwald                 log_info("Orig Signature: ");
69518707219SMatthias Ringwald                 log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8);
69618707219SMatthias Ringwald                 uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1);
69718707219SMatthias 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);
6983deb3ec6SMatthias Ringwald                 return;
6993deb3ec6SMatthias Ringwald             }
7007a766ebfSMatthias Ringwald #endif
701b06f2579SMatthias Ringwald             // move on
70218707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
7033f9b77dbSMatthias Ringwald             att_server_request_can_send_now(att_server, att_connection);
704b06f2579SMatthias Ringwald             break;
70588a48dd8SMatthias Ringwald 
7063deb3ec6SMatthias Ringwald         default:
7073deb3ec6SMatthias Ringwald             break;
7083deb3ec6SMatthias Ringwald     }
7093deb3ec6SMatthias Ringwald }
7103deb3ec6SMatthias Ringwald 
7111979f09cSMatthias Ringwald static bool att_server_data_ready_for_phase(att_server_t * att_server,  att_server_run_phase_t phase){
71290f03c80SMatthias Ringwald     switch (phase){
71390f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
71490f03c80SMatthias Ringwald             return att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
71590f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
71638b893aaSMilanka Ringwald              return (!btstack_linked_list_empty(&att_server->indication_requests) && (att_server->value_indication_handle == 0u));
71790f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
71890f03c80SMatthias Ringwald             return (!btstack_linked_list_empty(&att_server->notification_requests));
7197bbeb3adSMilanka Ringwald         default:
7207bbeb3adSMilanka Ringwald             btstack_assert(false);
7211979f09cSMatthias Ringwald             return false;
72290f03c80SMatthias Ringwald     }
7237bbeb3adSMilanka Ringwald }
72490f03c80SMatthias Ringwald 
7259d6c0193SMatthias 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){
72690f03c80SMatthias Ringwald     btstack_context_callback_registration_t * client;
72790f03c80SMatthias Ringwald     switch (phase){
72890f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
7299fe63f3aSMatthias Ringwald             att_server_process_validated_request(att_server, att_connection);
73090f03c80SMatthias Ringwald             break;
73190f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
73290f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->indication_requests;
73390f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->indication_requests, (btstack_linked_item_t *) client);
73490f03c80SMatthias Ringwald             client->callback(client->context);
73590f03c80SMatthias Ringwald             break;
73690f03c80SMatthias Ringwald        case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
73790f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->notification_requests;
73890f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->notification_requests, (btstack_linked_item_t *) client);
73990f03c80SMatthias Ringwald             client->callback(client->context);
74090f03c80SMatthias Ringwald             break;
7417bbeb3adSMilanka Ringwald         default:
7427bbeb3adSMilanka Ringwald             btstack_assert(false);
7437bbeb3adSMilanka Ringwald             break;
74490f03c80SMatthias Ringwald     }
74590f03c80SMatthias Ringwald }
74690f03c80SMatthias Ringwald 
7477eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){
748b06f2579SMatthias Ringwald 
7496438547aSMatthias Ringwald     hci_con_handle_t last_send_con_handle = HCI_CON_HANDLE_INVALID;
75068591e36SMatthias Ringwald     hci_connection_t * request_hci_connection   = NULL;
7511979f09cSMatthias Ringwald     bool can_send_now = true;
75238b893aaSMilanka Ringwald     uint8_t phase_index;
7536438547aSMatthias Ringwald 
7544395a000SMatthias Ringwald     for (phase_index = ATT_SERVER_RUN_PHASE_1_REQUESTS; phase_index <= ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS; phase_index++){
7554395a000SMatthias Ringwald         att_server_run_phase_t phase = (att_server_run_phase_t) phase_index;
7566438547aSMatthias Ringwald         hci_con_handle_t skip_connections_until = att_server_last_can_send_now;
757ff3cc4a5SMatthias Ringwald         while (true){
7586438547aSMatthias Ringwald             btstack_linked_list_iterator_t it;
75918707219SMatthias Ringwald             hci_connections_get_iterator(&it);
76018707219SMatthias Ringwald             while(btstack_linked_list_iterator_has_next(&it)){
76118707219SMatthias Ringwald                 hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
76218707219SMatthias Ringwald                 att_server_t * att_server = &connection->att_server;
763388fa7c4SMatthias Ringwald                 att_connection_t * att_connection = &connection->att_connection;
7646438547aSMatthias Ringwald 
7651979f09cSMatthias Ringwald                 bool data_ready = att_server_data_ready_for_phase(att_server, phase);
7666438547aSMatthias Ringwald 
767b6df12b6SMatthias 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);
7686438547aSMatthias Ringwald 
7696438547aSMatthias Ringwald                 // skip until last sender found (which is also skipped)
7706438547aSMatthias Ringwald                 if (skip_connections_until != HCI_CON_HANDLE_INVALID){
77168591e36SMatthias Ringwald                     if (data_ready && (request_hci_connection == NULL)){
77268591e36SMatthias Ringwald                         request_hci_connection = connection;
7736438547aSMatthias Ringwald                     }
774b6df12b6SMatthias Ringwald                     if (skip_connections_until == att_connection->con_handle){
7756438547aSMatthias Ringwald                         skip_connections_until = HCI_CON_HANDLE_INVALID;
7766438547aSMatthias Ringwald                     }
7776438547aSMatthias Ringwald                     continue;
7786438547aSMatthias Ringwald                 };
7796438547aSMatthias Ringwald 
78090f03c80SMatthias Ringwald                 if (data_ready){
781969a5bbaSMatthias Ringwald                     if (can_send_now){
7829d6c0193SMatthias Ringwald                         att_server_trigger_send_for_phase(att_server, att_connection, phase);
783b6df12b6SMatthias Ringwald                         last_send_con_handle = att_connection->con_handle;
78437a8059dSMatthias Ringwald                         can_send_now = att_server_can_send_packet(att_server, att_connection);
78590f03c80SMatthias Ringwald                         data_ready = att_server_data_ready_for_phase(att_server, phase);
78668591e36SMatthias Ringwald                         if (data_ready && (request_hci_connection == NULL)){
78768591e36SMatthias Ringwald                             request_hci_connection = connection;
788cbbb12d9SMatthias Ringwald                         }
789cbbb12d9SMatthias Ringwald                     } else {
79068591e36SMatthias Ringwald                         request_hci_connection = connection;
791f29a88d8SMatthias Ringwald                         break;
792cbbb12d9SMatthias Ringwald                     }
793cbbb12d9SMatthias Ringwald                 }
7943deb3ec6SMatthias Ringwald             }
7953deb3ec6SMatthias Ringwald 
7966438547aSMatthias Ringwald             // stop skipping (handles disconnect by last send connection)
7976438547aSMatthias Ringwald             skip_connections_until = HCI_CON_HANDLE_INVALID;
7986438547aSMatthias Ringwald 
7993551936eSMatthias Ringwald             // Exit loop, if we cannot send
800969a5bbaSMatthias Ringwald             if (!can_send_now) break;
801969a5bbaSMatthias Ringwald 
802969a5bbaSMatthias Ringwald             // Exit loop, if we can send but there are also no further request
80368591e36SMatthias Ringwald             if (request_hci_connection == NULL) break;
804969a5bbaSMatthias Ringwald 
805969a5bbaSMatthias Ringwald             // Finally, if we still can send and there are requests, just try again
80668591e36SMatthias Ringwald             request_hci_connection = NULL;
807969a5bbaSMatthias Ringwald         }
8086438547aSMatthias Ringwald         // update last send con handle for round robin
8096438547aSMatthias Ringwald         if (last_send_con_handle != HCI_CON_HANDLE_INVALID){
8106438547aSMatthias Ringwald             att_server_last_can_send_now = last_send_con_handle;
8116438547aSMatthias Ringwald         }
8123551936eSMatthias Ringwald     }
813969a5bbaSMatthias Ringwald 
81468591e36SMatthias Ringwald     if (request_hci_connection == NULL) return;
8153f9b77dbSMatthias Ringwald 
8163f9b77dbSMatthias Ringwald     att_server_t * att_server = &request_hci_connection->att_server;
8173f9b77dbSMatthias Ringwald     att_connection_t * att_connection = &request_hci_connection->att_connection;
8183f9b77dbSMatthias Ringwald     att_server_request_can_send_now(att_server, att_connection);
819969a5bbaSMatthias Ringwald }
820969a5bbaSMatthias Ringwald 
821*26b42dcfSMatthias Ringwald static void att_server_handle_att_pdu(att_server_t * att_server, att_connection_t * att_connection, uint8_t * packet, uint16_t size){
82218707219SMatthias Ringwald 
823838bd521SMatthias Ringwald     uint8_t opcode  = packet[0u];
82438b893aaSMilanka Ringwald     uint8_t method  = opcode & 0x03fu;
825ed68ae9dSMilanka Ringwald     bool invalid = method > ATT_MULTIPLE_HANDLE_VALUE_NTF;
82638b893aaSMilanka Ringwald     bool command = (opcode & 0x40u) != 0u;
827838bd521SMatthias Ringwald 
828838bd521SMatthias Ringwald     // ignore invalid commands
829838bd521SMatthias Ringwald     if (invalid && command){
830838bd521SMatthias Ringwald         return;
831838bd521SMatthias Ringwald     }
832838bd521SMatthias Ringwald 
8333deb3ec6SMatthias Ringwald     // handle value indication confirms
83438b893aaSMilanka Ringwald     if ((opcode == ATT_HANDLE_VALUE_CONFIRMATION) && (att_server->value_indication_handle != 0u)){
83518707219SMatthias Ringwald         btstack_run_loop_remove_timer(&att_server->value_indication_timer);
83618707219SMatthias Ringwald         uint16_t att_handle = att_server->value_indication_handle;
83738b893aaSMilanka Ringwald         att_server->value_indication_handle = 0u;
83838b893aaSMilanka Ringwald         att_handle_value_indication_notify_client(0u, att_connection->con_handle, att_handle);
8393f9b77dbSMatthias Ringwald         att_server_request_can_send_now(att_server, att_connection);
8403deb3ec6SMatthias Ringwald         return;
8413deb3ec6SMatthias Ringwald     }
8423deb3ec6SMatthias Ringwald 
8436428eb5aSMatthias Ringwald     // directly process command
8446428eb5aSMatthias Ringwald     // note: signed write cannot be handled directly as authentication needs to be verified
845838bd521SMatthias Ringwald     if (opcode == ATT_WRITE_COMMAND){
846b6df12b6SMatthias Ringwald         att_handle_request(att_connection, packet, size, NULL);
8476428eb5aSMatthias Ringwald         return;
8486428eb5aSMatthias Ringwald     }
8496428eb5aSMatthias Ringwald 
8503deb3ec6SMatthias Ringwald     // check size
85118707219SMatthias Ringwald     if (size > sizeof(att_server->request_buffer)) {
852e0463bdcSMatthias 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));
8536428eb5aSMatthias Ringwald         return;
8546428eb5aSMatthias Ringwald     }
8553deb3ec6SMatthias Ringwald 
856e0463bdcSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
857e0463bdcSMatthias Ringwald     // abort signed write validation if a new request comes in (but finish previous signed write if possible)
858e0463bdcSMatthias Ringwald     if (att_server->state == ATT_SERVER_W4_SIGNED_WRITE_VALIDATION){
859e0463bdcSMatthias Ringwald         if (packet[0] == ATT_SIGNED_WRITE_COMMAND){
860e0463bdcSMatthias Ringwald             log_info("skip new signed write request as previous is in validation");
861e0463bdcSMatthias Ringwald             return;
862e0463bdcSMatthias Ringwald         } else {
863e0463bdcSMatthias Ringwald             log_info("abort signed write validation to process new request");
864e0463bdcSMatthias Ringwald             att_server->state = ATT_SERVER_IDLE;
865e0463bdcSMatthias Ringwald         }
866e0463bdcSMatthias Ringwald     }
867e0463bdcSMatthias Ringwald #endif
8683deb3ec6SMatthias Ringwald     // last request still in processing?
86918707219SMatthias Ringwald     if (att_server->state != ATT_SERVER_IDLE){
870e0463bdcSMatthias Ringwald         log_info("skip att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state);
8716428eb5aSMatthias Ringwald         return;
8726428eb5aSMatthias Ringwald     }
8733deb3ec6SMatthias Ringwald 
8743deb3ec6SMatthias Ringwald     // store request
87518707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED;
87618707219SMatthias Ringwald     att_server->request_size = size;
8776535961aSMatthias Ringwald     (void)memcpy(att_server->request_buffer, packet, size);
8783deb3ec6SMatthias Ringwald 
879cd45e970SMatthias Ringwald     att_run_for_context(att_server, att_connection);
88041772f37SMatthias Ringwald }
88141772f37SMatthias Ringwald 
88241772f37SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
88368591e36SMatthias Ringwald     hci_connection_t * hci_connection;
884b6df12b6SMatthias Ringwald     att_connection_t * att_connection;
885*26b42dcfSMatthias Ringwald     att_server_t     * att_server;
88641772f37SMatthias Ringwald 
88741772f37SMatthias Ringwald     switch (packet_type){
88841772f37SMatthias Ringwald         case HCI_EVENT_PACKET:
88941772f37SMatthias Ringwald             switch (packet[0]){
89041772f37SMatthias Ringwald                 case L2CAP_EVENT_CAN_SEND_NOW:
89141772f37SMatthias Ringwald                     att_server_handle_can_send_now();
89241772f37SMatthias Ringwald                     break;
89341772f37SMatthias Ringwald                 case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
89441772f37SMatthias Ringwald                     // GATT client has negotiated the mtu for this connection
89568591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(handle);
89668591e36SMatthias Ringwald                     if (!hci_connection) break;
897388fa7c4SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
898b6df12b6SMatthias Ringwald                     att_connection->mtu = little_endian_read_16(packet, 4);
89941772f37SMatthias Ringwald                     break;
90041772f37SMatthias Ringwald                 default:
90141772f37SMatthias Ringwald                     break;
90241772f37SMatthias Ringwald             }
90341772f37SMatthias Ringwald             break;
90441772f37SMatthias Ringwald 
90541772f37SMatthias Ringwald         case ATT_DATA_PACKET:
90641772f37SMatthias Ringwald             log_debug("ATT Packet, handle 0x%04x", handle);
90768591e36SMatthias Ringwald             hci_connection = hci_connection_for_handle(handle);
90868591e36SMatthias Ringwald             if (!hci_connection) break;
90941772f37SMatthias Ringwald 
910*26b42dcfSMatthias Ringwald             att_server = &hci_connection->att_server;
911*26b42dcfSMatthias Ringwald             att_connection = &hci_connection->att_connection;
912*26b42dcfSMatthias Ringwald             att_server_handle_att_pdu(att_server, att_connection, packet, size);
913372d62c4SMatthias Ringwald             break;
9147bbeb3adSMilanka Ringwald 
9157bbeb3adSMilanka Ringwald         default:
9167bbeb3adSMilanka Ringwald             break;
917372d62c4SMatthias Ringwald     }
9183deb3ec6SMatthias Ringwald }
9193deb3ec6SMatthias Ringwald 
920bf78aac6SMatthias Ringwald // ---------------------
921bf78aac6SMatthias Ringwald // persistent CCC writes
922bf78aac6SMatthias Ringwald static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){
9234ea43905SMatthias Ringwald     return ('B' << 24u) | ('T' << 16u) | ('C' << 8u) | index;
924bf78aac6SMatthias Ringwald }
925bf78aac6SMatthias Ringwald 
926bf78aac6SMatthias Ringwald static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){
927bf78aac6SMatthias Ringwald     // lookup att_server instance
92868591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
92968591e36SMatthias Ringwald     if (!hci_connection) return;
93068591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
931bf78aac6SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
932bf78aac6SMatthias 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);
9336a540790SMatthias Ringwald 
934bf78aac6SMatthias Ringwald     // check if bonded
935bf78aac6SMatthias Ringwald     if (le_device_index < 0) return;
9366a540790SMatthias Ringwald 
937bf78aac6SMatthias Ringwald     // get btstack_tlv
938bf78aac6SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
939bf78aac6SMatthias Ringwald     void * tlv_context;
940bf78aac6SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
941bf78aac6SMatthias Ringwald     if (!tlv_impl) return;
9426a540790SMatthias Ringwald 
943bf78aac6SMatthias Ringwald     // update ccc tag
944bf78aac6SMatthias Ringwald     int index;
9456a540790SMatthias Ringwald     uint32_t highest_seq_nr = 0;
9466a540790SMatthias Ringwald     uint32_t lowest_seq_nr = 0;
9476a540790SMatthias Ringwald     uint32_t tag_for_lowest_seq_nr = 0;
9486a540790SMatthias Ringwald     uint32_t tag_for_empty = 0;
9496a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
9506afb9acaSMatthias Ringwald     for (index=0; index<NVN_NUM_GATT_SERVER_CCC; index++){
951bf78aac6SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
9526a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
9536a540790SMatthias Ringwald 
9546a540790SMatthias Ringwald         // empty/invalid tag
9556a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)){
9566a540790SMatthias Ringwald             tag_for_empty = tag;
957bf78aac6SMatthias Ringwald             continue;
958bf78aac6SMatthias Ringwald         }
9596a540790SMatthias Ringwald         // update highest seq nr
9606a540790SMatthias Ringwald         if (entry.seq_nr > highest_seq_nr){
9616a540790SMatthias Ringwald             highest_seq_nr = entry.seq_nr;
9626a540790SMatthias Ringwald         }
9636a540790SMatthias Ringwald         // find entry with lowest seq nr
9644ea43905SMatthias Ringwald         if ((tag_for_lowest_seq_nr == 0u) || (entry.seq_nr < lowest_seq_nr)){
9656a540790SMatthias Ringwald             tag_for_lowest_seq_nr = tag;
9666a540790SMatthias Ringwald             lowest_seq_nr = entry.seq_nr;
9676a540790SMatthias Ringwald         }
9686a540790SMatthias Ringwald 
9696a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
9706a540790SMatthias Ringwald         if (entry.att_handle   != att_handle)      continue;
9716a540790SMatthias Ringwald 
9726a540790SMatthias Ringwald         // found matching entry
9739305033eSMatthias Ringwald         if (value != 0){
974bf78aac6SMatthias Ringwald             // update
9756a540790SMatthias Ringwald             if (entry.value == value) {
976760ad805SMatthias Ringwald                 log_info("CCC Index %u: Up-to-date", index);
977760ad805SMatthias Ringwald                 return;
978760ad805SMatthias Ringwald             }
979348fc806SMatthias Ringwald             entry.value = (uint8_t) value;
9804ea43905SMatthias Ringwald             entry.seq_nr = highest_seq_nr + 1u;
981760ad805SMatthias Ringwald             log_info("CCC Index %u: Store", index);
9820fe46bc1SMatthias Ringwald             int result = tlv_impl->store_tag(tlv_context, tag, (const uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
9830fe46bc1SMatthias Ringwald             if (result != 0){
9840fe46bc1SMatthias Ringwald                 log_error("Store tag index %u failed", index);
9850fe46bc1SMatthias Ringwald             }
98601ac2008SMatthias Ringwald         } else {
98701ac2008SMatthias Ringwald             // delete
988760ad805SMatthias Ringwald             log_info("CCC Index %u: Delete", index);
98901ac2008SMatthias Ringwald             tlv_impl->delete_tag(tlv_context, tag);
99001ac2008SMatthias Ringwald         }
991bf78aac6SMatthias Ringwald         return;
992bf78aac6SMatthias Ringwald     }
9936a540790SMatthias Ringwald 
9940fe46bc1SMatthias Ringwald     log_info("tag_for_empty %"PRIx32", tag_for_lowest_seq_nr %"PRIx32, tag_for_empty, tag_for_lowest_seq_nr);
9956a540790SMatthias Ringwald 
9964ea43905SMatthias Ringwald     if (value == 0u){
9976a540790SMatthias Ringwald         // done
9986a540790SMatthias Ringwald         return;
9996a540790SMatthias Ringwald     }
10006a540790SMatthias Ringwald 
100138b893aaSMilanka Ringwald     uint32_t tag_to_use = 0u;
100238b893aaSMilanka Ringwald     if (tag_for_empty != 0u){
10036a540790SMatthias Ringwald         tag_to_use = tag_for_empty;
10046a540790SMatthias Ringwald     } else if (tag_for_lowest_seq_nr){
10056a540790SMatthias Ringwald         tag_to_use = tag_for_lowest_seq_nr;
10066a540790SMatthias Ringwald     } else {
10076a540790SMatthias Ringwald         // should not happen
10086a540790SMatthias Ringwald         return;
10096a540790SMatthias Ringwald     }
1010bf78aac6SMatthias Ringwald     // store ccc tag
10114ea43905SMatthias Ringwald     entry.seq_nr       = highest_seq_nr + 1u;
10126a540790SMatthias Ringwald     entry.device_index = le_device_index;
10136a540790SMatthias Ringwald     entry.att_handle   = att_handle;
1014348fc806SMatthias Ringwald     entry.value        = (uint8_t) value;
10150fe46bc1SMatthias Ringwald     int result = tlv_impl->store_tag(tlv_context, tag_to_use, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
10160fe46bc1SMatthias Ringwald     if (result != 0){
10170fe46bc1SMatthias Ringwald         log_error("Store tag index %u failed", index);
10180fe46bc1SMatthias Ringwald     }
1019bf78aac6SMatthias Ringwald }
1020bf78aac6SMatthias Ringwald 
1021fe3d0cbaSMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server){
1022760ad805SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
1023760ad805SMatthias Ringwald     log_info("Clear CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
1024760ad805SMatthias Ringwald     // check if bonded
1025760ad805SMatthias Ringwald     if (le_device_index < 0) return;
1026760ad805SMatthias Ringwald     // get btstack_tlv
1027760ad805SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
1028760ad805SMatthias Ringwald     void * tlv_context;
1029760ad805SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
1030760ad805SMatthias Ringwald     if (!tlv_impl) return;
1031760ad805SMatthias Ringwald     // get all ccc tag
1032760ad805SMatthias Ringwald     int index;
10336a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
10346afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
1035760ad805SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
10366a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
10376a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
10386a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
1039760ad805SMatthias Ringwald         // delete entry
1040760ad805SMatthias Ringwald         log_info("CCC Index %u: Delete", index);
1041760ad805SMatthias Ringwald         tlv_impl->delete_tag(tlv_context, tag);
1042760ad805SMatthias Ringwald     }
1043760ad805SMatthias Ringwald }
1044760ad805SMatthias Ringwald 
10459a1f55eaSMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server, att_connection_t * att_connection){
104601ac2008SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
104701ac2008SMatthias Ringwald     log_info("Restore CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
10481b7acd0dSMatthias Ringwald     // check if bonded
10491b7acd0dSMatthias Ringwald     if (le_device_index < 0) return;
105001ac2008SMatthias Ringwald     // get btstack_tlv
105101ac2008SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
105201ac2008SMatthias Ringwald     void * tlv_context;
105301ac2008SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
105401ac2008SMatthias Ringwald     if (!tlv_impl) return;
105501ac2008SMatthias Ringwald     // get all ccc tag
105601ac2008SMatthias Ringwald     int index;
10576a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
10586afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
105901ac2008SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
10606a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
10616a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
10626a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
106301ac2008SMatthias Ringwald         // simulate write callback
10646a540790SMatthias Ringwald         uint16_t attribute_handle = entry.att_handle;
106501ac2008SMatthias Ringwald         uint8_t  value[2];
10666a540790SMatthias Ringwald         little_endian_store_16(value, 0, entry.value);
106701ac2008SMatthias Ringwald         att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
106801ac2008SMatthias Ringwald         if (!callback) continue;
10696a540790SMatthias Ringwald         log_info("CCC Index %u: Set Attribute handle 0x%04x to value 0x%04x", index, attribute_handle, entry.value );
1070b6df12b6SMatthias Ringwald         (*callback)(att_connection->con_handle, attribute_handle, ATT_TRANSACTION_MODE_NONE, 0, value, sizeof(value));
107101ac2008SMatthias Ringwald     }
107201ac2008SMatthias Ringwald }
107301ac2008SMatthias Ringwald 
1074bf78aac6SMatthias Ringwald // persistent CCC writes
1075bf78aac6SMatthias Ringwald // ---------------------
10763d71c7a4SMatthias Ringwald 
10773d71c7a4SMatthias Ringwald // gatt service management
10783d71c7a4SMatthias Ringwald static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){
10793d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
10803d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
10813d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
10823d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
10833d71c7a4SMatthias Ringwald         if (handler->start_handle > handle) continue;
10843d71c7a4SMatthias Ringwald         if (handler->end_handle   < handle) continue;
10853d71c7a4SMatthias Ringwald         return handler;
10863d71c7a4SMatthias Ringwald     }
10873d71c7a4SMatthias Ringwald     return NULL;
10883d71c7a4SMatthias Ringwald }
10893d71c7a4SMatthias Ringwald static att_read_callback_t att_server_read_callback_for_handle(uint16_t handle){
10903d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
10919305033eSMatthias Ringwald     if (handler != NULL) return handler->read_callback;
10923d71c7a4SMatthias Ringwald     return att_server_client_read_callback;
10933d71c7a4SMatthias Ringwald }
10943d71c7a4SMatthias Ringwald 
10953d71c7a4SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){
10963d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
10979305033eSMatthias Ringwald     if (handler != NULL) return handler->write_callback;
10983d71c7a4SMatthias Ringwald     return att_server_client_write_callback;
10993d71c7a4SMatthias Ringwald }
11003d71c7a4SMatthias Ringwald 
11015d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle){
11025d07396bSMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
11039305033eSMatthias Ringwald     if (handler != NULL) return handler->packet_handler;
11045d07396bSMatthias Ringwald     return att_client_packet_handler;
11055d07396bSMatthias Ringwald }
11065d07396bSMatthias Ringwald 
11073d71c7a4SMatthias Ringwald static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){
11083d71c7a4SMatthias Ringwald     // notify all callbacks
11093d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
11103d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
11113d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
11123d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
11133d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
11143d71c7a4SMatthias Ringwald         (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
11153d71c7a4SMatthias Ringwald     }
11163d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return;
11173d71c7a4SMatthias Ringwald     (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
11183d71c7a4SMatthias Ringwald }
11193d71c7a4SMatthias Ringwald 
11203d71c7a4SMatthias Ringwald // returns first reported error or 0
11213d71c7a4SMatthias Ringwald static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){
11223d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
11233d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
11243d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
11253d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
11263d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
11273d71c7a4SMatthias Ringwald         uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
112838b893aaSMilanka Ringwald         if (error_code != 0u) return error_code;
11293d71c7a4SMatthias Ringwald     }
11303d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return 0;
11313d71c7a4SMatthias Ringwald     return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
11323d71c7a4SMatthias Ringwald }
11333d71c7a4SMatthias Ringwald 
11343d71c7a4SMatthias 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){
11353d71c7a4SMatthias Ringwald     att_read_callback_t callback = att_server_read_callback_for_handle(attribute_handle);
113601ac2008SMatthias Ringwald     if (!callback) return 0;
11373d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, offset, buffer, buffer_size);
11383d71c7a4SMatthias Ringwald }
11393d71c7a4SMatthias Ringwald 
11403d71c7a4SMatthias 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){
11413d71c7a4SMatthias Ringwald     switch (transaction_mode){
11423d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_VALIDATE:
11433d71c7a4SMatthias Ringwald             return att_validate_prepared_write(con_handle);
11443d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_EXECUTE:
11453d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_CANCEL:
11463d71c7a4SMatthias Ringwald             att_notify_write_callbacks(con_handle, transaction_mode);
11473d71c7a4SMatthias Ringwald             return 0;
11483d71c7a4SMatthias Ringwald         default:
11493d71c7a4SMatthias Ringwald             break;
11503d71c7a4SMatthias Ringwald     }
11513d71c7a4SMatthias Ringwald 
1152bf78aac6SMatthias Ringwald     // track CCC writes
11534ea43905SMatthias Ringwald     if (att_is_persistent_ccc(attribute_handle) && (offset == 0u) && (buffer_size == 2u)){
1154bf78aac6SMatthias Ringwald         att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0));
1155bf78aac6SMatthias Ringwald     }
1156bf78aac6SMatthias Ringwald 
115701ac2008SMatthias Ringwald     att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
115801ac2008SMatthias Ringwald     if (!callback) return 0;
11593d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size);
11603d71c7a4SMatthias Ringwald }
11613d71c7a4SMatthias Ringwald 
11623d71c7a4SMatthias Ringwald /**
11633d71c7a4SMatthias Ringwald  * @brief register read/write callbacks for specific handle range
11643d71c7a4SMatthias Ringwald  * @param att_service_handler_t
11653d71c7a4SMatthias Ringwald  */
11663d71c7a4SMatthias Ringwald void att_server_register_service_handler(att_service_handler_t * handler){
1167ec8f5414SMilanka Ringwald     bool att_server_registered = false;
1168ec8f5414SMilanka Ringwald     if (att_service_handler_for_handle(handler->start_handle)){
1169ec8f5414SMilanka Ringwald         att_server_registered = true;
1170ec8f5414SMilanka Ringwald     }
1171ec8f5414SMilanka Ringwald 
1172ec8f5414SMilanka Ringwald     if (att_service_handler_for_handle(handler->end_handle)){
1173ec8f5414SMilanka Ringwald         att_server_registered = true;
1174ec8f5414SMilanka Ringwald     }
1175ec8f5414SMilanka Ringwald 
1176ec8f5414SMilanka Ringwald     if (att_server_registered){
11773d71c7a4SMatthias Ringwald         log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle);
11783d71c7a4SMatthias Ringwald         return;
11793d71c7a4SMatthias Ringwald     }
11803d71c7a4SMatthias Ringwald     btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler);
11813d71c7a4SMatthias Ringwald }
11823d71c7a4SMatthias Ringwald 
11833deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){
11843deb3ec6SMatthias Ringwald 
11853d71c7a4SMatthias Ringwald     // store callbacks
11863d71c7a4SMatthias Ringwald     att_server_client_read_callback  = read_callback;
11873d71c7a4SMatthias Ringwald     att_server_client_write_callback = write_callback;
11883d71c7a4SMatthias Ringwald 
11891a389cdcSMatthias Ringwald     // register for HCI Events
1190d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &att_event_packet_handler;
11911a389cdcSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
11921a389cdcSMatthias Ringwald 
1193406722e4SMatthias Ringwald     // register for SM events
1194d9a7306aSMatthias Ringwald     sm_event_callback_registration.callback = &att_event_packet_handler;
1195406722e4SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
11963deb3ec6SMatthias Ringwald 
11971a389cdcSMatthias Ringwald     // and L2CAP ATT Server PDUs
11983deb3ec6SMatthias Ringwald     att_dispatch_register_server(att_packet_handler);
11993deb3ec6SMatthias Ringwald 
120070dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
120170dca4d4SMatthias Ringwald     // setup l2cap service
1202f2372b44SMatthias Ringwald     l2cap_register_service(&att_event_packet_handler, PSM_ATT, 0xffff, gap_get_security_level());
120370dca4d4SMatthias Ringwald #endif
120470dca4d4SMatthias Ringwald 
12053deb3ec6SMatthias Ringwald     att_set_db(db);
12063d71c7a4SMatthias Ringwald     att_set_read_callback(att_server_read_callback);
12073d71c7a4SMatthias Ringwald     att_set_write_callback(att_server_write_callback);
12083deb3ec6SMatthias Ringwald }
12093deb3ec6SMatthias Ringwald 
12103deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){
12113deb3ec6SMatthias Ringwald     att_client_packet_handler = handler;
12123deb3ec6SMatthias Ringwald }
12133deb3ec6SMatthias Ringwald 
1214cbbb12d9SMatthias Ringwald 
1215cbbb12d9SMatthias Ringwald // to be deprecated
1216c141988cSMatthias Ringwald int  att_server_can_send_packet_now(hci_con_handle_t con_handle){
121768591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
121868591e36SMatthias Ringwald     if (!hci_connection) return 0;
121937a8059dSMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
122037a8059dSMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
122137a8059dSMatthias Ringwald     return att_server_can_send_packet(att_server, att_connection);
12221b96b007SMatthias Ringwald }
12231b96b007SMatthias Ringwald 
12249816429dSMatthias Ringwald uint8_t att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
1225cbbb12d9SMatthias Ringwald     return att_server_request_to_send_notification(callback_registration, con_handle);
12263deb3ec6SMatthias Ringwald }
12273deb3ec6SMatthias Ringwald 
1228cbbb12d9SMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){
1229cbbb12d9SMatthias Ringwald     att_client_waiting_for_can_send_registration.callback = &att_emit_can_send_now_event;
1230cbbb12d9SMatthias Ringwald     att_server_request_to_send_notification(&att_client_waiting_for_can_send_registration, con_handle);
1231cbbb12d9SMatthias Ringwald }
1232cbbb12d9SMatthias Ringwald // end of deprecated
1233cbbb12d9SMatthias Ringwald 
12349816429dSMatthias Ringwald uint8_t att_server_request_to_send_notification(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
123568591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
123668591e36SMatthias Ringwald     if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
123768591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
12383f9b77dbSMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
1239c37df6f6SMatthias Ringwald     bool added = btstack_linked_list_add_tail(&att_server->notification_requests, (btstack_linked_item_t*) callback_registration);
12403f9b77dbSMatthias Ringwald     att_server_request_can_send_now(att_server, att_connection);
1241c37df6f6SMatthias Ringwald     if (added){
1242cbbb12d9SMatthias Ringwald         return ERROR_CODE_SUCCESS;
1243c37df6f6SMatthias Ringwald     } else {
1244c37df6f6SMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1245c37df6f6SMatthias Ringwald     }
1246cbbb12d9SMatthias Ringwald }
1247cbbb12d9SMatthias Ringwald 
12489816429dSMatthias Ringwald uint8_t att_server_request_to_send_indication(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
124968591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
125068591e36SMatthias Ringwald     if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
125168591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
12523f9b77dbSMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
1253c37df6f6SMatthias Ringwald     bool added = btstack_linked_list_add_tail(&att_server->indication_requests, (btstack_linked_item_t*) callback_registration);
12543f9b77dbSMatthias Ringwald     att_server_request_can_send_now(att_server, att_connection);
1255c37df6f6SMatthias Ringwald     if (added){
1256969a5bbaSMatthias Ringwald         return ERROR_CODE_SUCCESS;
1257c37df6f6SMatthias Ringwald     } else {
1258c37df6f6SMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1259c37df6f6SMatthias Ringwald     }
1260bb38f057SMatthias Ringwald }
1261bb38f057SMatthias Ringwald 
12629816429dSMatthias Ringwald uint8_t att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){
126368591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
126468591e36SMatthias Ringwald     if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
126537a8059dSMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
1266388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
126768591e36SMatthias Ringwald 
126837a8059dSMatthias Ringwald     if (!att_server_can_send_packet(att_server, att_connection)) return BTSTACK_ACL_BUFFERS_FULL;
12693deb3ec6SMatthias Ringwald 
12703deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
12713deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
1272b6df12b6SMatthias Ringwald     uint16_t size = att_prepare_handle_value_notification(att_connection, attribute_handle, value, value_len, packet_buffer);
1273d415948cSMatthias Ringwald 
1274d415948cSMatthias Ringwald     return att_server_send_prepared(att_server, att_connection, size);
12753deb3ec6SMatthias Ringwald }
12763deb3ec6SMatthias Ringwald 
12779816429dSMatthias Ringwald uint8_t att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){
127868591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
127968591e36SMatthias Ringwald     if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
128068591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
1281388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
128218707219SMatthias Ringwald 
128338b893aaSMilanka Ringwald     if (att_server->value_indication_handle != 0u) return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS;
128437a8059dSMatthias Ringwald     if (!att_server_can_send_packet(att_server, att_connection)) return BTSTACK_ACL_BUFFERS_FULL;
12853deb3ec6SMatthias Ringwald 
12863deb3ec6SMatthias Ringwald     // track indication
128718707219SMatthias Ringwald     att_server->value_indication_handle = attribute_handle;
128818707219SMatthias Ringwald     btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout);
128918707219SMatthias Ringwald     btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS);
129018707219SMatthias Ringwald     btstack_run_loop_add_timer(&att_server->value_indication_timer);
12913deb3ec6SMatthias Ringwald 
12923deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
12933deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
1294b6df12b6SMatthias Ringwald     uint16_t size = att_prepare_handle_value_indication(att_connection, attribute_handle, value, value_len, packet_buffer);
129523079b39SMatthias Ringwald 
1296d415948cSMatthias Ringwald     return att_server_send_prepared(att_server, att_connection, size);
12973deb3ec6SMatthias Ringwald }
12988f9132b5SMilanka Ringwald 
12998f9132b5SMilanka Ringwald uint16_t att_server_get_mtu(hci_con_handle_t con_handle){
130068591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
130168591e36SMatthias Ringwald     if (!hci_connection) return 0;
1302388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
1303b6df12b6SMatthias Ringwald     return att_connection->mtu;
13048f9132b5SMilanka Ringwald }
130535d7f2dfSMilanka Ringwald 
130635d7f2dfSMilanka Ringwald void att_server_deinit(void){
130735d7f2dfSMilanka Ringwald     att_server_client_read_callback  = NULL;
130835d7f2dfSMilanka Ringwald     att_server_client_write_callback = NULL;
130935d7f2dfSMilanka Ringwald     att_client_packet_handler = NULL;
131035d7f2dfSMilanka Ringwald     service_handlers = NULL;
131135d7f2dfSMilanka Ringwald }
1312