xref: /btstack/src/ble/att_server.c (revision 23079b390e4d869b8a8cf38b24afafc74565b05e)
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 
7868591e36SMatthias Ringwald static void att_run_for_context(hci_connection_t * hci_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);
8268591e36SMatthias Ringwald static void att_server_persistent_ccc_restore(hci_connection_t * hci_connection);
8368591e36SMatthias Ringwald static void att_server_persistent_ccc_clear(hci_connection_t * hci_connection);
8468591e36SMatthias Ringwald static void att_server_handle_att_pdu(hci_connection_t * hci_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
11468591e36SMatthias Ringwald static hci_connection_t * hci_connection_for_state(att_server_state_t state){
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)){
11818707219SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
11918707219SMatthias Ringwald         att_server_t * att_server = &connection->att_server;
12068591e36SMatthias Ringwald         if (att_server->state == state) return connection;
12118707219SMatthias Ringwald     }
12218707219SMatthias Ringwald     return NULL;
12318707219SMatthias Ringwald }
12402b78fc8SMatthias Ringwald #endif
125bb38f057SMatthias Ringwald 
12668591e36SMatthias Ringwald static void att_server_request_can_send_now(hci_connection_t * hci_connection){
127388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
128*23079b39SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
129*23079b39SMatthias Ringwald     switch (att_server->bearer_type){
130*23079b39SMatthias Ringwald         case ATT_BEARER_UNENHANCED_LE:
131b6df12b6SMatthias Ringwald             att_dispatch_server_request_can_send_now_event(att_connection->con_handle);
132*23079b39SMatthias Ringwald             break;
133*23079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
134*23079b39SMatthias Ringwald         case ATT_BEARER_UNENHANCED_CLASSIC:
135*23079b39SMatthias Ringwald             l2cap_request_can_send_now_event(att_server->l2cap_cid);
136*23079b39SMatthias Ringwald             break;
137*23079b39SMatthias Ringwald #endif
138*23079b39SMatthias Ringwald         default:
139*23079b39SMatthias Ringwald             btstack_unreachable();
140*23079b39SMatthias Ringwald             break;
141*23079b39SMatthias Ringwald     }
142c8c6e9b4SMatthias Ringwald }
143c8c6e9b4SMatthias Ringwald 
1441979f09cSMatthias Ringwald static bool att_server_can_send_packet(hci_connection_t * hci_connection){
145388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
146*23079b39SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
147*23079b39SMatthias Ringwald     switch (att_server->bearer_type) {
148*23079b39SMatthias Ringwald         case ATT_BEARER_UNENHANCED_LE:
1491979f09cSMatthias Ringwald             return att_dispatch_server_can_send_now(att_connection->con_handle) != 0;
150*23079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
151*23079b39SMatthias Ringwald         case ATT_BEARER_UNENHANCED_CLASSIC:
152*23079b39SMatthias Ringwald             return l2cap_can_send_packet_now(att_server->l2cap_cid) != 0;
153*23079b39SMatthias Ringwald #endif
154*23079b39SMatthias Ringwald         default:
155*23079b39SMatthias Ringwald             btstack_unreachable();
156*23079b39SMatthias Ringwald             break;
157*23079b39SMatthias Ringwald     }
158*23079b39SMatthias Ringwald     return false;
159c0f26e1cSMatthias Ringwald }
160c0f26e1cSMatthias Ringwald 
1613deb3ec6SMatthias Ringwald static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){
1625d07396bSMatthias Ringwald     btstack_packet_handler_t packet_handler = att_server_packet_handler_for_handle(attribute_handle);
1635d07396bSMatthias Ringwald     if (!packet_handler) return;
1643deb3ec6SMatthias Ringwald 
1653deb3ec6SMatthias Ringwald     uint8_t event[7];
1663deb3ec6SMatthias Ringwald     int pos = 0;
1675611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE;
1684ea43905SMatthias Ringwald     event[pos++] = sizeof(event) - 2u;
1693deb3ec6SMatthias Ringwald     event[pos++] = status;
170f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, client_handle);
1713deb3ec6SMatthias Ringwald     pos += 2;
172f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, attribute_handle);
173a444112bSMatthias Ringwald     (*packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1743deb3ec6SMatthias Ringwald }
1753deb3ec6SMatthias Ringwald 
1765d07396bSMatthias Ringwald static void att_emit_event_to_all(const uint8_t * event, uint16_t size){
1775d07396bSMatthias Ringwald     // dispatch to app level handler
1789305033eSMatthias Ringwald     if (att_client_packet_handler != NULL){
1795d07396bSMatthias Ringwald         (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size);
1805d07396bSMatthias Ringwald     }
1813deb3ec6SMatthias Ringwald 
1825d07396bSMatthias Ringwald     // dispatch to service handlers
1835d07396bSMatthias Ringwald     btstack_linked_list_iterator_t it;
1845d07396bSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
1855d07396bSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1865d07396bSMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
1875d07396bSMatthias Ringwald         if (!handler->packet_handler) continue;
1885d07396bSMatthias Ringwald         (*handler->packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size);
1895d07396bSMatthias Ringwald     }
1905d07396bSMatthias Ringwald }
1915d07396bSMatthias Ringwald 
1925d07396bSMatthias Ringwald static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){
1933deb3ec6SMatthias Ringwald     uint8_t event[6];
1943deb3ec6SMatthias Ringwald     int pos = 0;
1955611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE;
1964ea43905SMatthias Ringwald     event[pos++] = sizeof(event) - 2u;
197fc64f94aSMatthias Ringwald     little_endian_store_16(event, pos, con_handle);
1983deb3ec6SMatthias Ringwald     pos += 2;
199f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, mtu);
2005d07396bSMatthias Ringwald 
2015d07396bSMatthias Ringwald     // also dispatch to GATT Clients
202b12646c5SJakob Krantz     att_dispatch_server_mtu_exchanged(con_handle, mtu);
2035d07396bSMatthias Ringwald 
2045d07396bSMatthias Ringwald     // dispatch to app level handler and service handlers
2055d07396bSMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
2063deb3ec6SMatthias Ringwald }
2073deb3ec6SMatthias Ringwald 
2085f141511SMatthias Ringwald static void att_emit_can_send_now_event(void * context){
2095f141511SMatthias Ringwald     UNUSED(context);
2103c97b242SMatthias Ringwald     if (!att_client_packet_handler) return;
2113c97b242SMatthias Ringwald 
2121b96b007SMatthias Ringwald     uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0};
2131b96b007SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
2141b96b007SMatthias Ringwald }
2151b96b007SMatthias Ringwald 
21668591e36SMatthias Ringwald static void att_emit_connected_event(hci_connection_t * hci_connection){
21768591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
218388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
219ff1bec95SMatthias Ringwald     uint8_t event[11];
220ff1bec95SMatthias Ringwald     int pos = 0;
221ff1bec95SMatthias Ringwald     event[pos++] = ATT_EVENT_CONNECTED;
2224ea43905SMatthias Ringwald     event[pos++] = sizeof(event) - 2u;
223ff1bec95SMatthias Ringwald     event[pos++] = att_server->peer_addr_type;
224ff1bec95SMatthias Ringwald     reverse_bd_addr(att_server->peer_address, &event[pos]);
225ff1bec95SMatthias Ringwald     pos += 6;
226b6df12b6SMatthias Ringwald     little_endian_store_16(event, pos, att_connection->con_handle);
227ff1bec95SMatthias Ringwald     pos += 2;
228ff1bec95SMatthias Ringwald 
229ff1bec95SMatthias Ringwald     // dispatch to app level handler and service handlers
230ff1bec95SMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
231ff1bec95SMatthias Ringwald }
232ff1bec95SMatthias Ringwald 
233ff1bec95SMatthias Ringwald 
2346187ad4cSMatthias Ringwald static void att_emit_disconnected_event(uint16_t con_handle){
235ff1bec95SMatthias Ringwald     uint8_t event[4];
236ff1bec95SMatthias Ringwald     int pos = 0;
237ff1bec95SMatthias Ringwald     event[pos++] = ATT_EVENT_DISCONNECTED;
2384ea43905SMatthias Ringwald     event[pos++] = sizeof(event) - 2u;
2396187ad4cSMatthias Ringwald     little_endian_store_16(event, pos, con_handle);
240ff1bec95SMatthias Ringwald     pos += 2;
241ff1bec95SMatthias Ringwald 
242ff1bec95SMatthias Ringwald     // dispatch to app level handler and service handlers
243ff1bec95SMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
244ff1bec95SMatthias Ringwald }
245ff1bec95SMatthias Ringwald 
246ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){
24754178543SMatthias Ringwald     void * context = btstack_run_loop_get_timer_context(ts);
24854178543SMatthias Ringwald     hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
24968591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
25068591e36SMatthias Ringwald     if (!hci_connection) return;
25168591e36SMatthias Ringwald     // @note: after a transaction timeout, no more requests shall be sent over this ATT Bearer
252bce48a26SMatthias Ringwald     // (that's why we don't reset the value_indication_handle)
253b6df12b6SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
25418707219SMatthias Ringwald     uint16_t att_handle = att_server->value_indication_handle;
255388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
25638b893aaSMilanka Ringwald     att_handle_value_indication_notify_client((uint8_t)ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_connection->con_handle, att_handle);
2573deb3ec6SMatthias Ringwald }
2583deb3ec6SMatthias Ringwald 
2593deb3ec6SMatthias Ringwald static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2609ec2630cSMatthias Ringwald 
2617dad9ff8SMatthias Ringwald     UNUSED(channel); // ok: there is no channel
2627dad9ff8SMatthias Ringwald     UNUSED(size);    // ok: handling own l2cap events
2633deb3ec6SMatthias Ringwald 
26418707219SMatthias Ringwald     att_server_t * att_server;
265b6df12b6SMatthias Ringwald     att_connection_t * att_connection;
26618707219SMatthias Ringwald     hci_con_handle_t con_handle;
26768591e36SMatthias Ringwald     hci_connection_t * hci_connection;
26870dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
26970dca4d4SMatthias Ringwald     bd_addr_t address;
27068591e36SMatthias Ringwald     btstack_linked_list_iterator_t it;
27170dca4d4SMatthias Ringwald #endif
27218707219SMatthias Ringwald 
2733deb3ec6SMatthias Ringwald     switch (packet_type) {
2743deb3ec6SMatthias Ringwald 
2753deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
2760e2df43fSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
2773deb3ec6SMatthias Ringwald 
27870dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
27970dca4d4SMatthias Ringwald                 case L2CAP_EVENT_INCOMING_CONNECTION:
28070dca4d4SMatthias Ringwald                     l2cap_event_incoming_connection_get_address(packet, address);
28170dca4d4SMatthias Ringwald                     l2cap_accept_connection(channel);
28236de8547SMatthias Ringwald                     log_info("Accept incoming connection from %s", bd_addr_to_str(address));
28370dca4d4SMatthias Ringwald                     break;
28470dca4d4SMatthias Ringwald                 case L2CAP_EVENT_CHANNEL_OPENED:
285122b723eSMatthias Ringwald                     con_handle = l2cap_event_channel_opened_get_handle(packet);
28668591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
28768591e36SMatthias Ringwald                     if (!hci_connection) break;
28868591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
289122b723eSMatthias Ringwald                     // store connection info
290*23079b39SMatthias Ringwald                     att_server->bearer_type = ATT_BEARER_UNENHANCED_CLASSIC;
291f16129ceSMatthias Ringwald                     att_server->peer_addr_type = BD_ADDR_TYPE_ACL;
292122b723eSMatthias Ringwald                     l2cap_event_channel_opened_get_address(packet, att_server->peer_address);
293388fa7c4SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
294b6df12b6SMatthias Ringwald                     att_connection->con_handle = con_handle;
29570dca4d4SMatthias Ringwald                     att_server->l2cap_cid = l2cap_event_channel_opened_get_local_cid(packet);
296122b723eSMatthias Ringwald                     // reset connection properties
297122b723eSMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
298b6df12b6SMatthias Ringwald                     att_connection->mtu = l2cap_event_channel_opened_get_remote_mtu(packet);
299b6df12b6SMatthias Ringwald                     att_connection->max_mtu = l2cap_max_mtu();
300b6df12b6SMatthias Ringwald                     if (att_connection->max_mtu > ATT_REQUEST_BUFFER_SIZE){
301b6df12b6SMatthias Ringwald                         att_connection->max_mtu = ATT_REQUEST_BUFFER_SIZE;
302122b723eSMatthias Ringwald                     }
303df33ee14SMatthias Ringwald 
304b6df12b6SMatthias Ringwald                     log_info("Connection opened %s, l2cap cid %04x, mtu %u", bd_addr_to_str(address), att_server->l2cap_cid, att_connection->mtu);
305df33ee14SMatthias Ringwald 
306914988a1SMatthias Ringwald                     // update security params
307b6df12b6SMatthias Ringwald                     att_connection->encryption_key_size = gap_encryption_key_size(con_handle);
308b6df12b6SMatthias Ringwald                     att_connection->authenticated = gap_authenticated(con_handle);
309b6df12b6SMatthias Ringwald                     att_connection->secure_connection = gap_secure_connection(con_handle);
310914988a1SMatthias Ringwald                     log_info("encrypted key size %u, authenticated %u, secure connection %u",
311b6df12b6SMatthias Ringwald                         att_connection->encryption_key_size, att_connection->authenticated, att_connection->secure_connection);
312df33ee14SMatthias Ringwald 
313df33ee14SMatthias Ringwald                     // notify connection opened
31468591e36SMatthias Ringwald                     att_emit_connected_event(hci_connection);
315df33ee14SMatthias Ringwald 
316914988a1SMatthias Ringwald                     // restore persisten ccc if encrypted
317914988a1SMatthias Ringwald                     if ( gap_security_level(con_handle) >= LEVEL_2){
31868591e36SMatthias Ringwald                         att_server_persistent_ccc_restore(hci_connection);
319914988a1SMatthias Ringwald                     }
320122b723eSMatthias Ringwald                     // TODO: what to do about le device db?
321122b723eSMatthias Ringwald                     att_server->pairing_active = 0;
32270dca4d4SMatthias Ringwald                     break;
32348364364SMatthias Ringwald                 case L2CAP_EVENT_CAN_SEND_NOW:
32448364364SMatthias Ringwald                     att_server_handle_can_send_now();
32548364364SMatthias Ringwald                     break;
32648364364SMatthias Ringwald 
32770dca4d4SMatthias Ringwald #endif
3283deb3ec6SMatthias Ringwald                 case HCI_EVENT_LE_META:
3293deb3ec6SMatthias Ringwald                     switch (packet[2]) {
3303deb3ec6SMatthias Ringwald                         case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
33118707219SMatthias Ringwald                             con_handle = little_endian_read_16(packet, 4);
33268591e36SMatthias Ringwald                             hci_connection = hci_connection_for_handle(con_handle);
33368591e36SMatthias Ringwald                             if (!hci_connection) break;
33468591e36SMatthias Ringwald                             att_server = &hci_connection->att_server;
3353deb3ec6SMatthias Ringwald                         	// store connection info
33618707219SMatthias Ringwald                         	att_server->peer_addr_type = packet[7];
33718707219SMatthias Ringwald                             reverse_bd_addr(&packet[8], att_server->peer_address);
338388fa7c4SMatthias Ringwald                             att_connection = &hci_connection->att_connection;
339b6df12b6SMatthias Ringwald                             att_connection->con_handle = con_handle;
3403deb3ec6SMatthias Ringwald                             // reset connection properties
34118707219SMatthias Ringwald                             att_server->state = ATT_SERVER_IDLE;
342*23079b39SMatthias Ringwald                             att_server->bearer_type = ATT_BEARER_UNENHANCED_LE;
343b6df12b6SMatthias Ringwald                             att_connection->mtu = ATT_DEFAULT_MTU;
344b6df12b6SMatthias Ringwald                             att_connection->max_mtu = l2cap_max_le_mtu();
345b6df12b6SMatthias Ringwald                             if (att_connection->max_mtu > ATT_REQUEST_BUFFER_SIZE){
346b6df12b6SMatthias Ringwald                                 att_connection->max_mtu = ATT_REQUEST_BUFFER_SIZE;
34799c58ad0SMatthias Ringwald                             }
34838b893aaSMilanka Ringwald                             att_connection->encryption_key_size = 0u;
34938b893aaSMilanka Ringwald                             att_connection->authenticated = 0u;
35038b893aaSMilanka Ringwald 		                	att_connection->authorized = 0u;
351b2c1e52cSMatthias Ringwald                             // workaround: identity resolving can already be complete, at least store result
352b2c1e52cSMatthias Ringwald                             att_server->ir_le_device_db_index = sm_le_device_index(con_handle);
35338b893aaSMilanka Ringwald                             att_server->ir_lookup_active = 0u;
35438b893aaSMilanka Ringwald                             att_server->pairing_active = 0u;
355ff1bec95SMatthias Ringwald                             // notify all - old
3565d07396bSMatthias Ringwald                             att_emit_event_to_all(packet, size);
357ff1bec95SMatthias Ringwald                             // notify all - new
35868591e36SMatthias Ringwald                             att_emit_connected_event(hci_connection);
3593deb3ec6SMatthias Ringwald                             break;
3603deb3ec6SMatthias Ringwald 
3613deb3ec6SMatthias Ringwald                         default:
3623deb3ec6SMatthias Ringwald                             break;
3633deb3ec6SMatthias Ringwald                     }
3643deb3ec6SMatthias Ringwald                     break;
3653deb3ec6SMatthias Ringwald 
3663deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_CHANGE:
3678601e696SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_CHANGE_V2:
3683deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE:
3693deb3ec6SMatthias Ringwald                 	// check handle
37018707219SMatthias Ringwald                     con_handle = little_endian_read_16(packet, 3);
37168591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
37268591e36SMatthias Ringwald                     if (!hci_connection) break;
373914988a1SMatthias Ringwald                     if (gap_get_connection_type(con_handle) != GAP_CONNECTION_LE) break;
374914988a1SMatthias Ringwald                     // update security params
37568591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
376388fa7c4SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
377b6df12b6SMatthias Ringwald                     att_connection->encryption_key_size = gap_encryption_key_size(con_handle);
378ff3f1026SMatthias Ringwald                     att_connection->authenticated = gap_authenticated(con_handle) ? 1 : 0;
379f461b3dfSMatthias Ringwald                     att_connection->secure_connection = gap_secure_connection(con_handle) ? 1 : 0;
380914988a1SMatthias Ringwald                     log_info("encrypted key size %u, authenticated %u, secure connection %u",
381b6df12b6SMatthias Ringwald                         att_connection->encryption_key_size, att_connection->authenticated, att_connection->secure_connection);
382914988a1SMatthias Ringwald                     if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){
3834097998aSMatthias Ringwald                         // restore CCC values when encrypted for LE Connections
38422bfc225SMilanka Ringwald                         if (hci_event_encryption_change_get_encryption_enabled(packet) != 0){
38568591e36SMatthias Ringwald                             att_server_persistent_ccc_restore(hci_connection);
38601ac2008SMatthias Ringwald                         }
38701ac2008SMatthias Ringwald                     }
38868591e36SMatthias Ringwald                     att_run_for_context(hci_connection);
3893deb3ec6SMatthias Ringwald                     break;
3903deb3ec6SMatthias Ringwald 
3913deb3ec6SMatthias Ringwald                 case HCI_EVENT_DISCONNECTION_COMPLETE:
39270a390c7SMatthias Ringwald                     // check handle
39318707219SMatthias Ringwald                     con_handle = hci_event_disconnection_complete_get_connection_handle(packet);
39468591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
39568591e36SMatthias Ringwald                     if (!hci_connection) break;
39668591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
397388fa7c4SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
398b6df12b6SMatthias Ringwald                     att_clear_transaction_queue(att_connection);
399b6df12b6SMatthias Ringwald                     att_connection->con_handle = 0;
400760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
40118707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
40238b893aaSMilanka Ringwald                     if (att_server->value_indication_handle != 0u){
40327328ecbSMatthias Ringwald                         btstack_run_loop_remove_timer(&att_server->value_indication_timer);
404bce48a26SMatthias Ringwald                         uint16_t att_handle = att_server->value_indication_handle;
40538b893aaSMilanka Ringwald                         att_server->value_indication_handle = 0u; // reset error state
40638b893aaSMilanka Ringwald                         att_handle_value_indication_notify_client((uint8_t)ATT_HANDLE_VALUE_INDICATION_DISCONNECT, att_connection->con_handle, att_handle);
407bce48a26SMatthias Ringwald                     }
408ff1bec95SMatthias Ringwald                     // notify all - new
4096187ad4cSMatthias Ringwald                     att_emit_disconnected_event(con_handle);
410ff1bec95SMatthias Ringwald                     // notify all - old
4115d07396bSMatthias Ringwald                     att_emit_event_to_all(packet, size);
4123deb3ec6SMatthias Ringwald                     break;
4133deb3ec6SMatthias Ringwald 
414760ad805SMatthias Ringwald                 // Identity Resolving
4155611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_STARTED:
41618707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_started_get_handle(packet);
41768591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
41868591e36SMatthias Ringwald                     if (!hci_connection) break;
41968591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
4205611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED");
42130c8e114SMatthias Ringwald                     att_server->ir_lookup_active = 1;
4223deb3ec6SMatthias Ringwald                     break;
4235611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
424760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
42568591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
42668591e36SMatthias Ringwald                     if (!hci_connection) return;
42768591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
42830c8e114SMatthias Ringwald                     att_server->ir_lookup_active = 0;
4291b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index(packet);
4301b7acd0dSMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED");
43168591e36SMatthias Ringwald                     att_run_for_context(hci_connection);
4323deb3ec6SMatthias Ringwald                     break;
4335611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_FAILED:
43418707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_failed_get_handle(packet);
43568591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
43668591e36SMatthias Ringwald                     if (!hci_connection) break;
43768591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
4385611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED");
43930c8e114SMatthias Ringwald                     att_server->ir_lookup_active = 0;
44018707219SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
44168591e36SMatthias Ringwald                     att_run_for_context(hci_connection);
4423deb3ec6SMatthias Ringwald                     break;
443760ad805SMatthias Ringwald 
444760ad805SMatthias Ringwald                 // Pairing started - delete stored CCC values
445760ad805SMatthias Ringwald                 // - assumes pairing indicates either new device or re-pairing, in both cases there should be no stored CCC values
446760ad805SMatthias Ringwald                 // - assumes that all events have the con handle as the first field
447760ad805SMatthias Ringwald                 case SM_EVENT_JUST_WORKS_REQUEST:
448760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
449760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_INPUT_NUMBER:
450760ad805SMatthias Ringwald                 case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
451760ad805SMatthias Ringwald                     con_handle = sm_event_just_works_request_get_handle(packet);
45268591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
45368591e36SMatthias Ringwald                     if (!hci_connection) break;
45468591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
455760ad805SMatthias Ringwald                     att_server->pairing_active = 1;
456760ad805SMatthias Ringwald                     log_info("SM Pairing started");
457760ad805SMatthias Ringwald                     if (att_server->ir_le_device_db_index < 0) break;
45868591e36SMatthias Ringwald                     att_server_persistent_ccc_clear(hci_connection);
459760ad805SMatthias Ringwald                     // index not valid anymore
460760ad805SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
461760ad805SMatthias Ringwald                     break;
462760ad805SMatthias Ringwald 
4631b7acd0dSMatthias Ringwald                 // Bonding completed
464760ad805SMatthias Ringwald                 case SM_EVENT_IDENTITY_CREATED:
465760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
46668591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
46768591e36SMatthias Ringwald                     if (!hci_connection) return;
46868591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
469760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
4701b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_created_get_index(packet);
47168591e36SMatthias Ringwald                     att_run_for_context(hci_connection);
4721b7acd0dSMatthias Ringwald                     break;
4731b7acd0dSMatthias Ringwald 
4741b7acd0dSMatthias Ringwald                 // Pairing complete (with/without bonding=storing of pairing information)
4751b7acd0dSMatthias Ringwald                 case SM_EVENT_PAIRING_COMPLETE:
4761b7acd0dSMatthias Ringwald                     con_handle = sm_event_pairing_complete_get_handle(packet);
47768591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
47868591e36SMatthias Ringwald                     if (!hci_connection) return;
47968591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
4801b7acd0dSMatthias Ringwald                     att_server->pairing_active = 0;
48168591e36SMatthias Ringwald                     att_run_for_context(hci_connection);
482760ad805SMatthias Ringwald                     break;
483760ad805SMatthias Ringwald 
484760ad805SMatthias Ringwald                 // Authorization
4855611a760SMatthias Ringwald                 case SM_EVENT_AUTHORIZATION_RESULT: {
48618707219SMatthias Ringwald                     con_handle = sm_event_authorization_result_get_handle(packet);
48768591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
48868591e36SMatthias Ringwald                     if (!hci_connection) break;
48968591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
490388fa7c4SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
491b6df12b6SMatthias Ringwald                     att_connection->authorized = sm_event_authorization_result_get_authorization_result(packet);
49268591e36SMatthias Ringwald                     att_server_request_can_send_now(hci_connection);
4933deb3ec6SMatthias Ringwald                 	break;
4943deb3ec6SMatthias Ringwald                 }
4953deb3ec6SMatthias Ringwald                 default:
4963deb3ec6SMatthias Ringwald                     break;
4973deb3ec6SMatthias Ringwald             }
49865b44ffdSMatthias Ringwald             break;
49970dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
50070dca4d4SMatthias Ringwald         case L2CAP_DATA_PACKET:
50168591e36SMatthias Ringwald             hci_connections_get_iterator(&it);
50268591e36SMatthias Ringwald             while(btstack_linked_list_iterator_has_next(&it)){
503388fa7c4SMatthias Ringwald                 hci_connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
504388fa7c4SMatthias Ringwald                 att_server = &hci_connection->att_server;
50568591e36SMatthias Ringwald                 if (att_server->l2cap_cid == channel) {
506388fa7c4SMatthias Ringwald                     att_server_handle_att_pdu(hci_connection, packet, size);
50768591e36SMatthias Ringwald                     break;
50868591e36SMatthias Ringwald                 }
50968591e36SMatthias Ringwald             }
51070dca4d4SMatthias Ringwald             break;
51170dca4d4SMatthias Ringwald #endif
51241772f37SMatthias Ringwald 
51365b44ffdSMatthias Ringwald         default:
51465b44ffdSMatthias Ringwald             break;
5153deb3ec6SMatthias Ringwald     }
5163deb3ec6SMatthias Ringwald }
5173deb3ec6SMatthias Ringwald 
5187a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
5193deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
5203deb3ec6SMatthias Ringwald 
52168591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION);
52268591e36SMatthias Ringwald     if (!hci_connection) return;
52368591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
5243deb3ec6SMatthias Ringwald 
5253deb3ec6SMatthias Ringwald     uint8_t hash_flipped[8];
5269c80e4ccSMatthias Ringwald     reverse_64(hash, hash_flipped);
52718707219SMatthias Ringwald     if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8)){
5283deb3ec6SMatthias Ringwald         log_info("ATT Signed Write, invalid signature");
52903f53d81SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT
53003f53d81SMatthias Ringwald         printf("ATT Signed Write, invalid signature\n");
53103f53d81SMatthias Ringwald #endif
53218707219SMatthias Ringwald         att_server->state = ATT_SERVER_IDLE;
5333deb3ec6SMatthias Ringwald         return;
5343deb3ec6SMatthias Ringwald     }
5353deb3ec6SMatthias Ringwald     log_info("ATT Signed Write, valid signature");
53603f53d81SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT
53703f53d81SMatthias Ringwald     printf("ATT Signed Write, valid signature\n");
53803f53d81SMatthias Ringwald #endif
5393deb3ec6SMatthias Ringwald 
5403deb3ec6SMatthias Ringwald     // update sequence number
54118707219SMatthias Ringwald     uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
54218707219SMatthias Ringwald     le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1);
54318707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
54468591e36SMatthias Ringwald     att_server_request_can_send_now(hci_connection);
5453deb3ec6SMatthias Ringwald }
5467a766ebfSMatthias Ringwald #endif
54788a48dd8SMatthias Ringwald 
54818707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED
549b06f2579SMatthias Ringwald // pre: can send now
550b06f2579SMatthias Ringwald // returns: 1 if packet was sent
55168591e36SMatthias Ringwald static int att_server_process_validated_request(hci_connection_t * hci_connection){
55268591e36SMatthias Ringwald     att_server_t * att_server = & hci_connection->att_server;
553388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
554b06f2579SMatthias Ringwald 
555b06f2579SMatthias Ringwald     l2cap_reserve_packet_buffer();
556b06f2579SMatthias Ringwald     uint8_t * att_response_buffer = l2cap_get_outgoing_buffer();
557b6df12b6SMatthias Ringwald     uint16_t  att_response_size   = att_handle_request(att_connection, att_server->request_buffer, att_server->request_size, att_response_buffer);
558b06f2579SMatthias Ringwald 
559beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
560c1ab6cc1SMatthias Ringwald     if ((att_response_size == ATT_READ_RESPONSE_PENDING) || (att_response_size == ATT_INTERNAL_WRITE_RESPONSE_PENDING)){
561e404a688SMatthias Ringwald         // update state
562beb20288SMatthias Ringwald         att_server->state = ATT_SERVER_RESPONSE_PENDING;
563e404a688SMatthias Ringwald 
56456c3a347SMatthias Ringwald         // callback with handle ATT_READ_RESPONSE_PENDING for reads
56556c3a347SMatthias Ringwald         if (att_response_size == ATT_READ_RESPONSE_PENDING){
566b6df12b6SMatthias Ringwald             att_server_client_read_callback(att_connection->con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0);
56756c3a347SMatthias Ringwald         }
568e8e7403eSMatthias Ringwald 
569e8e7403eSMatthias Ringwald         // free reserved buffer
570e8e7403eSMatthias Ringwald         l2cap_release_packet_buffer();
571e8e7403eSMatthias Ringwald         return 0;
572e404a688SMatthias Ringwald     }
573e404a688SMatthias Ringwald #endif
574e404a688SMatthias Ringwald 
575b06f2579SMatthias Ringwald     // intercept "insufficient authorization" for authenticated connections to allow for user authorization
5764ea43905SMatthias Ringwald     if ((att_response_size     >= 4u)
577b06f2579SMatthias Ringwald     && (att_response_buffer[0] == ATT_ERROR_RESPONSE)
578b06f2579SMatthias Ringwald     && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION)
57938b893aaSMilanka Ringwald     && (att_connection->authenticated != 0u)){
580b06f2579SMatthias Ringwald 
581b6df12b6SMatthias Ringwald         switch (gap_authorization_state(att_connection->con_handle)){
582b06f2579SMatthias Ringwald             case AUTHORIZATION_UNKNOWN:
583b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
584b6df12b6SMatthias Ringwald                 sm_request_pairing(att_connection->con_handle);
585b06f2579SMatthias Ringwald                 return 0;
586b06f2579SMatthias Ringwald             case AUTHORIZATION_PENDING:
587b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
588b06f2579SMatthias Ringwald                 return 0;
589b06f2579SMatthias Ringwald             default:
590b06f2579SMatthias Ringwald                 break;
591b06f2579SMatthias Ringwald         }
592b06f2579SMatthias Ringwald     }
593b06f2579SMatthias Ringwald 
59418707219SMatthias Ringwald     att_server->state = ATT_SERVER_IDLE;
5954ea43905SMatthias Ringwald     if (att_response_size == 0u) {
596b06f2579SMatthias Ringwald         l2cap_release_packet_buffer();
597b06f2579SMatthias Ringwald         return 0;
598b06f2579SMatthias Ringwald     }
599b06f2579SMatthias Ringwald 
600*23079b39SMatthias Ringwald     switch (att_server->bearer_type){
601*23079b39SMatthias Ringwald         case ATT_BEARER_UNENHANCED_LE:
602b6df12b6SMatthias Ringwald             l2cap_send_prepared_connectionless(att_connection->con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size);
603*23079b39SMatthias Ringwald             break;
604*23079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
605*23079b39SMatthias Ringwald         case ATT_BEARER_UNENHANCED_CLASSIC:
606*23079b39SMatthias Ringwald             l2cap_send_prepared(att_server->l2cap_cid, att_response_size);
607*23079b39SMatthias Ringwald             break;
608*23079b39SMatthias Ringwald #endif
609*23079b39SMatthias Ringwald         default:
610*23079b39SMatthias Ringwald             btstack_unreachable();
611*23079b39SMatthias Ringwald             break;
61248364364SMatthias Ringwald     }
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;
62668591e36SMatthias Ringwald 
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;
63068591e36SMatthias Ringwald     att_server_request_can_send_now(hci_connection);
631e404a688SMatthias Ringwald     return ERROR_CODE_SUCCESS;
632e404a688SMatthias Ringwald }
633e404a688SMatthias Ringwald #endif
634e404a688SMatthias Ringwald 
63568591e36SMatthias Ringwald static void att_run_for_context(hci_connection_t * hci_connection){
63668591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
637388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
63818707219SMatthias Ringwald     switch (att_server->state){
6393deb3ec6SMatthias Ringwald         case ATT_SERVER_REQUEST_RECEIVED:
640*23079b39SMatthias Ringwald             switch (att_server->bearer_type){
641*23079b39SMatthias Ringwald                 case ATT_BEARER_UNENHANCED_LE:
6420234fbbcSMatthias Ringwald                     // wait until re-encryption as central is complete
643*23079b39SMatthias Ringwald                     if (gap_reconnect_security_setup_active(att_connection->con_handle)) {
644*23079b39SMatthias Ringwald                         return;
645*23079b39SMatthias Ringwald                     };
646*23079b39SMatthias Ringwald                     break;
647*23079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
648*23079b39SMatthias Ringwald                 case ATT_BEARER_UNENHANCED_CLASSIC:
649*23079b39SMatthias Ringwald                     // ok
650*23079b39SMatthias Ringwald                     break;
651*23079b39SMatthias Ringwald #endif
652*23079b39SMatthias Ringwald                 default:
653*23079b39SMatthias Ringwald                     btstack_unreachable();
654*23079b39SMatthias Ringwald                     break;
65548364364SMatthias Ringwald             }
6560234fbbcSMatthias Ringwald 
657760ad805SMatthias Ringwald             // wait until pairing is complete
658760ad805SMatthias Ringwald             if (att_server->pairing_active) break;
659760ad805SMatthias Ringwald 
6607a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
66118707219SMatthias Ringwald             if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){
6623deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write!");
6633deb3ec6SMatthias Ringwald                 if (!sm_cmac_ready()) {
6643deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, sm_cmac engine not ready. Abort");
66518707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
6663deb3ec6SMatthias Ringwald                     return;
6673deb3ec6SMatthias Ringwald                 }
66818707219SMatthias Ringwald                 if (att_server->request_size < (3 + 12)) {
6693deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, request to short. Abort.");
67018707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
6713deb3ec6SMatthias Ringwald                     return;
6723deb3ec6SMatthias Ringwald                 }
67318707219SMatthias Ringwald                 if (att_server->ir_lookup_active){
6743deb3ec6SMatthias Ringwald                     return;
6753deb3ec6SMatthias Ringwald                 }
67618707219SMatthias Ringwald                 if (att_server->ir_le_device_db_index < 0){
6773deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, CSRK not available");
67818707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
6793deb3ec6SMatthias Ringwald                     return;
6803deb3ec6SMatthias Ringwald                 }
6813deb3ec6SMatthias Ringwald 
6823deb3ec6SMatthias Ringwald                 // check counter
68318707219SMatthias Ringwald                 uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
68418707219SMatthias Ringwald                 uint32_t counter_db     = le_device_db_remote_counter_get(att_server->ir_le_device_db_index);
6853deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet);
6863deb3ec6SMatthias Ringwald                 if (counter_packet < counter_db){
6873deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, db reports higher counter, abort");
68818707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
6893deb3ec6SMatthias Ringwald                     return;
6903deb3ec6SMatthias Ringwald                 }
6913deb3ec6SMatthias Ringwald 
6923deb3ec6SMatthias Ringwald                 // signature is { sequence counter, secure hash }
6933deb3ec6SMatthias Ringwald                 sm_key_t csrk;
69418707219SMatthias Ringwald                 le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk);
69518707219SMatthias Ringwald                 att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION;
6963deb3ec6SMatthias Ringwald                 log_info("Orig Signature: ");
69718707219SMatthias Ringwald                 log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8);
69818707219SMatthias Ringwald                 uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1);
69918707219SMatthias 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);
7003deb3ec6SMatthias Ringwald                 return;
7013deb3ec6SMatthias Ringwald             }
7027a766ebfSMatthias Ringwald #endif
703b06f2579SMatthias Ringwald             // move on
70418707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
70568591e36SMatthias Ringwald             att_server_request_can_send_now(hci_connection);
706b06f2579SMatthias Ringwald             break;
70788a48dd8SMatthias Ringwald 
7083deb3ec6SMatthias Ringwald         default:
7093deb3ec6SMatthias Ringwald             break;
7103deb3ec6SMatthias Ringwald     }
7113deb3ec6SMatthias Ringwald }
7123deb3ec6SMatthias Ringwald 
7131979f09cSMatthias Ringwald static bool att_server_data_ready_for_phase(att_server_t * att_server,  att_server_run_phase_t phase){
71490f03c80SMatthias Ringwald     switch (phase){
71590f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
71690f03c80SMatthias Ringwald             return att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
71790f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
71838b893aaSMilanka Ringwald              return (!btstack_linked_list_empty(&att_server->indication_requests) && (att_server->value_indication_handle == 0u));
71990f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
72090f03c80SMatthias Ringwald             return (!btstack_linked_list_empty(&att_server->notification_requests));
7217bbeb3adSMilanka Ringwald         default:
7227bbeb3adSMilanka Ringwald             btstack_assert(false);
7231979f09cSMatthias Ringwald             return false;
72490f03c80SMatthias Ringwald     }
7257bbeb3adSMilanka Ringwald }
72690f03c80SMatthias Ringwald 
72768591e36SMatthias Ringwald static void att_server_trigger_send_for_phase(hci_connection_t * hci_connection,  att_server_run_phase_t phase){
72890f03c80SMatthias Ringwald     btstack_context_callback_registration_t * client;
72968591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
73090f03c80SMatthias Ringwald     switch (phase){
73190f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
73268591e36SMatthias Ringwald             att_server_process_validated_request(hci_connection);
73390f03c80SMatthias Ringwald             break;
73490f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
73590f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->indication_requests;
73690f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->indication_requests, (btstack_linked_item_t *) client);
73790f03c80SMatthias Ringwald             client->callback(client->context);
73890f03c80SMatthias Ringwald             break;
73990f03c80SMatthias Ringwald        case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
74090f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->notification_requests;
74190f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->notification_requests, (btstack_linked_item_t *) client);
74290f03c80SMatthias Ringwald             client->callback(client->context);
74390f03c80SMatthias Ringwald             break;
7447bbeb3adSMilanka Ringwald         default:
7457bbeb3adSMilanka Ringwald             btstack_assert(false);
7467bbeb3adSMilanka Ringwald             break;
74790f03c80SMatthias Ringwald     }
74890f03c80SMatthias Ringwald }
74990f03c80SMatthias Ringwald 
7507eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){
751b06f2579SMatthias Ringwald 
7526438547aSMatthias Ringwald     hci_con_handle_t last_send_con_handle = HCI_CON_HANDLE_INVALID;
75368591e36SMatthias Ringwald     hci_connection_t * request_hci_connection   = NULL;
7541979f09cSMatthias Ringwald     bool can_send_now = true;
75538b893aaSMilanka Ringwald     uint8_t phase_index;
7566438547aSMatthias Ringwald 
7574395a000SMatthias Ringwald     for (phase_index = ATT_SERVER_RUN_PHASE_1_REQUESTS; phase_index <= ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS; phase_index++){
7584395a000SMatthias Ringwald         att_server_run_phase_t phase = (att_server_run_phase_t) phase_index;
7596438547aSMatthias Ringwald         hci_con_handle_t skip_connections_until = att_server_last_can_send_now;
760ff3cc4a5SMatthias Ringwald         while (true){
7616438547aSMatthias Ringwald             btstack_linked_list_iterator_t it;
76218707219SMatthias Ringwald             hci_connections_get_iterator(&it);
76318707219SMatthias Ringwald             while(btstack_linked_list_iterator_has_next(&it)){
76418707219SMatthias Ringwald                 hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
76518707219SMatthias Ringwald                 att_server_t * att_server = &connection->att_server;
766388fa7c4SMatthias Ringwald                 att_connection_t * att_connection = &connection->att_connection;
7676438547aSMatthias Ringwald 
7681979f09cSMatthias Ringwald                 bool data_ready = att_server_data_ready_for_phase(att_server, phase);
7696438547aSMatthias Ringwald 
770b6df12b6SMatthias 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);
7716438547aSMatthias Ringwald 
7726438547aSMatthias Ringwald                 // skip until last sender found (which is also skipped)
7736438547aSMatthias Ringwald                 if (skip_connections_until != HCI_CON_HANDLE_INVALID){
77468591e36SMatthias Ringwald                     if (data_ready && (request_hci_connection == NULL)){
77568591e36SMatthias Ringwald                         request_hci_connection = connection;
7766438547aSMatthias Ringwald                     }
777b6df12b6SMatthias Ringwald                     if (skip_connections_until == att_connection->con_handle){
7786438547aSMatthias Ringwald                         skip_connections_until = HCI_CON_HANDLE_INVALID;
7796438547aSMatthias Ringwald                     }
7806438547aSMatthias Ringwald                     continue;
7816438547aSMatthias Ringwald                 };
7826438547aSMatthias Ringwald 
78390f03c80SMatthias Ringwald                 if (data_ready){
784969a5bbaSMatthias Ringwald                     if (can_send_now){
78568591e36SMatthias Ringwald                         att_server_trigger_send_for_phase(connection, phase);
786b6df12b6SMatthias Ringwald                         last_send_con_handle = att_connection->con_handle;
78768591e36SMatthias Ringwald                         can_send_now = att_server_can_send_packet(connection);
78890f03c80SMatthias Ringwald                         data_ready = att_server_data_ready_for_phase(att_server, phase);
78968591e36SMatthias Ringwald                         if (data_ready && (request_hci_connection == NULL)){
79068591e36SMatthias Ringwald                             request_hci_connection = connection;
791cbbb12d9SMatthias Ringwald                         }
792cbbb12d9SMatthias Ringwald                     } else {
79368591e36SMatthias Ringwald                         request_hci_connection = connection;
794f29a88d8SMatthias Ringwald                         break;
795cbbb12d9SMatthias Ringwald                     }
796cbbb12d9SMatthias Ringwald                 }
7973deb3ec6SMatthias Ringwald             }
7983deb3ec6SMatthias Ringwald 
7996438547aSMatthias Ringwald             // stop skipping (handles disconnect by last send connection)
8006438547aSMatthias Ringwald             skip_connections_until = HCI_CON_HANDLE_INVALID;
8016438547aSMatthias Ringwald 
8023551936eSMatthias Ringwald             // Exit loop, if we cannot send
803969a5bbaSMatthias Ringwald             if (!can_send_now) break;
804969a5bbaSMatthias Ringwald 
805969a5bbaSMatthias Ringwald             // Exit loop, if we can send but there are also no further request
80668591e36SMatthias Ringwald             if (request_hci_connection == NULL) break;
807969a5bbaSMatthias Ringwald 
808969a5bbaSMatthias Ringwald             // Finally, if we still can send and there are requests, just try again
80968591e36SMatthias Ringwald             request_hci_connection = NULL;
810969a5bbaSMatthias Ringwald         }
8116438547aSMatthias Ringwald         // update last send con handle for round robin
8126438547aSMatthias Ringwald         if (last_send_con_handle != HCI_CON_HANDLE_INVALID){
8136438547aSMatthias Ringwald             att_server_last_can_send_now = last_send_con_handle;
8146438547aSMatthias Ringwald         }
8153551936eSMatthias Ringwald     }
816969a5bbaSMatthias Ringwald 
81768591e36SMatthias Ringwald     if (request_hci_connection == NULL) return;
81868591e36SMatthias Ringwald     att_server_request_can_send_now(request_hci_connection);
819969a5bbaSMatthias Ringwald }
820969a5bbaSMatthias Ringwald 
82168591e36SMatthias Ringwald static void att_server_handle_att_pdu(hci_connection_t * hci_connection, uint8_t * packet, uint16_t size){
82268591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
823388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
82418707219SMatthias Ringwald 
825838bd521SMatthias Ringwald     uint8_t opcode  = packet[0u];
82638b893aaSMilanka Ringwald     uint8_t method  = opcode & 0x03fu;
827ed68ae9dSMilanka Ringwald     bool invalid = method > ATT_MULTIPLE_HANDLE_VALUE_NTF;
82838b893aaSMilanka Ringwald     bool command = (opcode & 0x40u) != 0u;
829838bd521SMatthias Ringwald 
830838bd521SMatthias Ringwald     // ignore invalid commands
831838bd521SMatthias Ringwald     if (invalid && command){
832838bd521SMatthias Ringwald         return;
833838bd521SMatthias Ringwald     }
834838bd521SMatthias Ringwald 
8353deb3ec6SMatthias Ringwald     // handle value indication confirms
83638b893aaSMilanka Ringwald     if ((opcode == ATT_HANDLE_VALUE_CONFIRMATION) && (att_server->value_indication_handle != 0u)){
83718707219SMatthias Ringwald         btstack_run_loop_remove_timer(&att_server->value_indication_timer);
83818707219SMatthias Ringwald         uint16_t att_handle = att_server->value_indication_handle;
83938b893aaSMilanka Ringwald         att_server->value_indication_handle = 0u;
84038b893aaSMilanka Ringwald         att_handle_value_indication_notify_client(0u, att_connection->con_handle, att_handle);
84168591e36SMatthias Ringwald         att_server_request_can_send_now(hci_connection);
8423deb3ec6SMatthias Ringwald         return;
8433deb3ec6SMatthias Ringwald     }
8443deb3ec6SMatthias Ringwald 
8456428eb5aSMatthias Ringwald     // directly process command
8466428eb5aSMatthias Ringwald     // note: signed write cannot be handled directly as authentication needs to be verified
847838bd521SMatthias Ringwald     if (opcode == ATT_WRITE_COMMAND){
848b6df12b6SMatthias Ringwald         att_handle_request(att_connection, packet, size, NULL);
8496428eb5aSMatthias Ringwald         return;
8506428eb5aSMatthias Ringwald     }
8516428eb5aSMatthias Ringwald 
8523deb3ec6SMatthias Ringwald     // check size
85318707219SMatthias Ringwald     if (size > sizeof(att_server->request_buffer)) {
854e0463bdcSMatthias 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));
8556428eb5aSMatthias Ringwald         return;
8566428eb5aSMatthias Ringwald     }
8573deb3ec6SMatthias Ringwald 
858e0463bdcSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
859e0463bdcSMatthias Ringwald     // abort signed write validation if a new request comes in (but finish previous signed write if possible)
860e0463bdcSMatthias Ringwald     if (att_server->state == ATT_SERVER_W4_SIGNED_WRITE_VALIDATION){
861e0463bdcSMatthias Ringwald         if (packet[0] == ATT_SIGNED_WRITE_COMMAND){
862e0463bdcSMatthias Ringwald             log_info("skip new signed write request as previous is in validation");
863e0463bdcSMatthias Ringwald             return;
864e0463bdcSMatthias Ringwald         } else {
865e0463bdcSMatthias Ringwald             log_info("abort signed write validation to process new request");
866e0463bdcSMatthias Ringwald             att_server->state = ATT_SERVER_IDLE;
867e0463bdcSMatthias Ringwald         }
868e0463bdcSMatthias Ringwald     }
869e0463bdcSMatthias Ringwald #endif
8703deb3ec6SMatthias Ringwald     // last request still in processing?
87118707219SMatthias Ringwald     if (att_server->state != ATT_SERVER_IDLE){
872e0463bdcSMatthias Ringwald         log_info("skip att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state);
8736428eb5aSMatthias Ringwald         return;
8746428eb5aSMatthias Ringwald     }
8753deb3ec6SMatthias Ringwald 
8763deb3ec6SMatthias Ringwald     // store request
87718707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED;
87818707219SMatthias Ringwald     att_server->request_size = size;
8796535961aSMatthias Ringwald     (void)memcpy(att_server->request_buffer, packet, size);
8803deb3ec6SMatthias Ringwald 
88168591e36SMatthias Ringwald     att_run_for_context(hci_connection);
88241772f37SMatthias Ringwald }
88341772f37SMatthias Ringwald 
88441772f37SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
88568591e36SMatthias Ringwald     hci_connection_t * hci_connection;
886b6df12b6SMatthias Ringwald     att_connection_t * att_connection;
88741772f37SMatthias Ringwald 
88841772f37SMatthias Ringwald     switch (packet_type){
88941772f37SMatthias Ringwald         case HCI_EVENT_PACKET:
89041772f37SMatthias Ringwald             switch (packet[0]){
89141772f37SMatthias Ringwald                 case L2CAP_EVENT_CAN_SEND_NOW:
89241772f37SMatthias Ringwald                     att_server_handle_can_send_now();
89341772f37SMatthias Ringwald                     break;
89441772f37SMatthias Ringwald                 case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
89541772f37SMatthias Ringwald                     // GATT client has negotiated the mtu for this connection
89668591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(handle);
89768591e36SMatthias Ringwald                     if (!hci_connection) break;
898388fa7c4SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
899b6df12b6SMatthias Ringwald                     att_connection->mtu = little_endian_read_16(packet, 4);
90041772f37SMatthias Ringwald                     break;
90141772f37SMatthias Ringwald                 default:
90241772f37SMatthias Ringwald                     break;
90341772f37SMatthias Ringwald             }
90441772f37SMatthias Ringwald             break;
90541772f37SMatthias Ringwald 
90641772f37SMatthias Ringwald         case ATT_DATA_PACKET:
90741772f37SMatthias Ringwald             log_debug("ATT Packet, handle 0x%04x", handle);
90868591e36SMatthias Ringwald             hci_connection = hci_connection_for_handle(handle);
90968591e36SMatthias Ringwald             if (!hci_connection) break;
91041772f37SMatthias Ringwald 
91168591e36SMatthias Ringwald             att_server_handle_att_pdu(hci_connection, packet, size);
912372d62c4SMatthias Ringwald             break;
9137bbeb3adSMilanka Ringwald 
9147bbeb3adSMilanka Ringwald         default:
9157bbeb3adSMilanka Ringwald             break;
916372d62c4SMatthias Ringwald     }
9173deb3ec6SMatthias Ringwald }
9183deb3ec6SMatthias Ringwald 
919bf78aac6SMatthias Ringwald // ---------------------
920bf78aac6SMatthias Ringwald // persistent CCC writes
921bf78aac6SMatthias Ringwald static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){
9224ea43905SMatthias Ringwald     return ('B' << 24u) | ('T' << 16u) | ('C' << 8u) | index;
923bf78aac6SMatthias Ringwald }
924bf78aac6SMatthias Ringwald 
925bf78aac6SMatthias Ringwald static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){
926bf78aac6SMatthias Ringwald     // lookup att_server instance
92768591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
92868591e36SMatthias Ringwald     if (!hci_connection) return;
92968591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
930bf78aac6SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
931bf78aac6SMatthias 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);
9326a540790SMatthias Ringwald 
933bf78aac6SMatthias Ringwald     // check if bonded
934bf78aac6SMatthias Ringwald     if (le_device_index < 0) return;
9356a540790SMatthias Ringwald 
936bf78aac6SMatthias Ringwald     // get btstack_tlv
937bf78aac6SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
938bf78aac6SMatthias Ringwald     void * tlv_context;
939bf78aac6SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
940bf78aac6SMatthias Ringwald     if (!tlv_impl) return;
9416a540790SMatthias Ringwald 
942bf78aac6SMatthias Ringwald     // update ccc tag
943bf78aac6SMatthias Ringwald     int index;
9446a540790SMatthias Ringwald     uint32_t highest_seq_nr = 0;
9456a540790SMatthias Ringwald     uint32_t lowest_seq_nr = 0;
9466a540790SMatthias Ringwald     uint32_t tag_for_lowest_seq_nr = 0;
9476a540790SMatthias Ringwald     uint32_t tag_for_empty = 0;
9486a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
9496afb9acaSMatthias Ringwald     for (index=0; index<NVN_NUM_GATT_SERVER_CCC; index++){
950bf78aac6SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
9516a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
9526a540790SMatthias Ringwald 
9536a540790SMatthias Ringwald         // empty/invalid tag
9546a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)){
9556a540790SMatthias Ringwald             tag_for_empty = tag;
956bf78aac6SMatthias Ringwald             continue;
957bf78aac6SMatthias Ringwald         }
9586a540790SMatthias Ringwald         // update highest seq nr
9596a540790SMatthias Ringwald         if (entry.seq_nr > highest_seq_nr){
9606a540790SMatthias Ringwald             highest_seq_nr = entry.seq_nr;
9616a540790SMatthias Ringwald         }
9626a540790SMatthias Ringwald         // find entry with lowest seq nr
9634ea43905SMatthias Ringwald         if ((tag_for_lowest_seq_nr == 0u) || (entry.seq_nr < lowest_seq_nr)){
9646a540790SMatthias Ringwald             tag_for_lowest_seq_nr = tag;
9656a540790SMatthias Ringwald             lowest_seq_nr = entry.seq_nr;
9666a540790SMatthias Ringwald         }
9676a540790SMatthias Ringwald 
9686a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
9696a540790SMatthias Ringwald         if (entry.att_handle   != att_handle)      continue;
9706a540790SMatthias Ringwald 
9716a540790SMatthias Ringwald         // found matching entry
9729305033eSMatthias Ringwald         if (value != 0){
973bf78aac6SMatthias Ringwald             // update
9746a540790SMatthias Ringwald             if (entry.value == value) {
975760ad805SMatthias Ringwald                 log_info("CCC Index %u: Up-to-date", index);
976760ad805SMatthias Ringwald                 return;
977760ad805SMatthias Ringwald             }
978348fc806SMatthias Ringwald             entry.value = (uint8_t) value;
9794ea43905SMatthias Ringwald             entry.seq_nr = highest_seq_nr + 1u;
980760ad805SMatthias Ringwald             log_info("CCC Index %u: Store", index);
9810fe46bc1SMatthias Ringwald             int result = tlv_impl->store_tag(tlv_context, tag, (const uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
9820fe46bc1SMatthias Ringwald             if (result != 0){
9830fe46bc1SMatthias Ringwald                 log_error("Store tag index %u failed", index);
9840fe46bc1SMatthias Ringwald             }
98501ac2008SMatthias Ringwald         } else {
98601ac2008SMatthias Ringwald             // delete
987760ad805SMatthias Ringwald             log_info("CCC Index %u: Delete", index);
98801ac2008SMatthias Ringwald             tlv_impl->delete_tag(tlv_context, tag);
98901ac2008SMatthias Ringwald         }
990bf78aac6SMatthias Ringwald         return;
991bf78aac6SMatthias Ringwald     }
9926a540790SMatthias Ringwald 
9930fe46bc1SMatthias Ringwald     log_info("tag_for_empty %"PRIx32", tag_for_lowest_seq_nr %"PRIx32, tag_for_empty, tag_for_lowest_seq_nr);
9946a540790SMatthias Ringwald 
9954ea43905SMatthias Ringwald     if (value == 0u){
9966a540790SMatthias Ringwald         // done
9976a540790SMatthias Ringwald         return;
9986a540790SMatthias Ringwald     }
9996a540790SMatthias Ringwald 
100038b893aaSMilanka Ringwald     uint32_t tag_to_use = 0u;
100138b893aaSMilanka Ringwald     if (tag_for_empty != 0u){
10026a540790SMatthias Ringwald         tag_to_use = tag_for_empty;
10036a540790SMatthias Ringwald     } else if (tag_for_lowest_seq_nr){
10046a540790SMatthias Ringwald         tag_to_use = tag_for_lowest_seq_nr;
10056a540790SMatthias Ringwald     } else {
10066a540790SMatthias Ringwald         // should not happen
10076a540790SMatthias Ringwald         return;
10086a540790SMatthias Ringwald     }
1009bf78aac6SMatthias Ringwald     // store ccc tag
10104ea43905SMatthias Ringwald     entry.seq_nr       = highest_seq_nr + 1u;
10116a540790SMatthias Ringwald     entry.device_index = le_device_index;
10126a540790SMatthias Ringwald     entry.att_handle   = att_handle;
1013348fc806SMatthias Ringwald     entry.value        = (uint8_t) value;
10140fe46bc1SMatthias Ringwald     int result = tlv_impl->store_tag(tlv_context, tag_to_use, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
10150fe46bc1SMatthias Ringwald     if (result != 0){
10160fe46bc1SMatthias Ringwald         log_error("Store tag index %u failed", index);
10170fe46bc1SMatthias Ringwald     }
1018bf78aac6SMatthias Ringwald }
1019bf78aac6SMatthias Ringwald 
102068591e36SMatthias Ringwald static void att_server_persistent_ccc_clear(hci_connection_t * hci_connection){
102168591e36SMatthias Ringwald     if (!hci_connection) return;
102268591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
102368591e36SMatthias Ringwald 
1024760ad805SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
1025760ad805SMatthias Ringwald     log_info("Clear CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
1026760ad805SMatthias Ringwald     // check if bonded
1027760ad805SMatthias Ringwald     if (le_device_index < 0) return;
1028760ad805SMatthias Ringwald     // get btstack_tlv
1029760ad805SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
1030760ad805SMatthias Ringwald     void * tlv_context;
1031760ad805SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
1032760ad805SMatthias Ringwald     if (!tlv_impl) return;
1033760ad805SMatthias Ringwald     // get all ccc tag
1034760ad805SMatthias Ringwald     int index;
10356a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
10366afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
1037760ad805SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
10386a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
10396a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
10406a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
1041760ad805SMatthias Ringwald         // delete entry
1042760ad805SMatthias Ringwald         log_info("CCC Index %u: Delete", index);
1043760ad805SMatthias Ringwald         tlv_impl->delete_tag(tlv_context, tag);
1044760ad805SMatthias Ringwald     }
1045760ad805SMatthias Ringwald }
1046760ad805SMatthias Ringwald 
104768591e36SMatthias Ringwald static void att_server_persistent_ccc_restore(hci_connection_t * hci_connection){
104868591e36SMatthias Ringwald     if (!hci_connection) return;
104968591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
1050388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
105168591e36SMatthias Ringwald 
105201ac2008SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
105301ac2008SMatthias Ringwald     log_info("Restore CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
10541b7acd0dSMatthias Ringwald     // check if bonded
10551b7acd0dSMatthias Ringwald     if (le_device_index < 0) return;
105601ac2008SMatthias Ringwald     // get btstack_tlv
105701ac2008SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
105801ac2008SMatthias Ringwald     void * tlv_context;
105901ac2008SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
106001ac2008SMatthias Ringwald     if (!tlv_impl) return;
106101ac2008SMatthias Ringwald     // get all ccc tag
106201ac2008SMatthias Ringwald     int index;
10636a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
10646afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
106501ac2008SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
10666a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
10676a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
10686a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
106901ac2008SMatthias Ringwald         // simulate write callback
10706a540790SMatthias Ringwald         uint16_t attribute_handle = entry.att_handle;
107101ac2008SMatthias Ringwald         uint8_t  value[2];
10726a540790SMatthias Ringwald         little_endian_store_16(value, 0, entry.value);
107301ac2008SMatthias Ringwald         att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
107401ac2008SMatthias Ringwald         if (!callback) continue;
10756a540790SMatthias Ringwald         log_info("CCC Index %u: Set Attribute handle 0x%04x to value 0x%04x", index, attribute_handle, entry.value );
1076b6df12b6SMatthias Ringwald         (*callback)(att_connection->con_handle, attribute_handle, ATT_TRANSACTION_MODE_NONE, 0, value, sizeof(value));
107701ac2008SMatthias Ringwald     }
107801ac2008SMatthias Ringwald }
107901ac2008SMatthias Ringwald 
1080bf78aac6SMatthias Ringwald // persistent CCC writes
1081bf78aac6SMatthias Ringwald // ---------------------
10823d71c7a4SMatthias Ringwald 
10833d71c7a4SMatthias Ringwald // gatt service management
10843d71c7a4SMatthias Ringwald static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){
10853d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
10863d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
10873d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
10883d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
10893d71c7a4SMatthias Ringwald         if (handler->start_handle > handle) continue;
10903d71c7a4SMatthias Ringwald         if (handler->end_handle   < handle) continue;
10913d71c7a4SMatthias Ringwald         return handler;
10923d71c7a4SMatthias Ringwald     }
10933d71c7a4SMatthias Ringwald     return NULL;
10943d71c7a4SMatthias Ringwald }
10953d71c7a4SMatthias Ringwald static att_read_callback_t att_server_read_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->read_callback;
10983d71c7a4SMatthias Ringwald     return att_server_client_read_callback;
10993d71c7a4SMatthias Ringwald }
11003d71c7a4SMatthias Ringwald 
11013d71c7a4SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){
11023d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
11039305033eSMatthias Ringwald     if (handler != NULL) return handler->write_callback;
11043d71c7a4SMatthias Ringwald     return att_server_client_write_callback;
11053d71c7a4SMatthias Ringwald }
11063d71c7a4SMatthias Ringwald 
11075d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle){
11085d07396bSMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
11099305033eSMatthias Ringwald     if (handler != NULL) return handler->packet_handler;
11105d07396bSMatthias Ringwald     return att_client_packet_handler;
11115d07396bSMatthias Ringwald }
11125d07396bSMatthias Ringwald 
11133d71c7a4SMatthias Ringwald static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){
11143d71c7a4SMatthias Ringwald     // notify all callbacks
11153d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
11163d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
11173d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
11183d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
11193d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
11203d71c7a4SMatthias Ringwald         (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
11213d71c7a4SMatthias Ringwald     }
11223d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return;
11233d71c7a4SMatthias Ringwald     (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
11243d71c7a4SMatthias Ringwald }
11253d71c7a4SMatthias Ringwald 
11263d71c7a4SMatthias Ringwald // returns first reported error or 0
11273d71c7a4SMatthias Ringwald static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){
11283d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
11293d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
11303d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
11313d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
11323d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
11333d71c7a4SMatthias Ringwald         uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
113438b893aaSMilanka Ringwald         if (error_code != 0u) return error_code;
11353d71c7a4SMatthias Ringwald     }
11363d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return 0;
11373d71c7a4SMatthias Ringwald     return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
11383d71c7a4SMatthias Ringwald }
11393d71c7a4SMatthias Ringwald 
11403d71c7a4SMatthias 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){
11413d71c7a4SMatthias Ringwald     att_read_callback_t callback = att_server_read_callback_for_handle(attribute_handle);
114201ac2008SMatthias Ringwald     if (!callback) return 0;
11433d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, offset, buffer, buffer_size);
11443d71c7a4SMatthias Ringwald }
11453d71c7a4SMatthias Ringwald 
11463d71c7a4SMatthias 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){
11473d71c7a4SMatthias Ringwald     switch (transaction_mode){
11483d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_VALIDATE:
11493d71c7a4SMatthias Ringwald             return att_validate_prepared_write(con_handle);
11503d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_EXECUTE:
11513d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_CANCEL:
11523d71c7a4SMatthias Ringwald             att_notify_write_callbacks(con_handle, transaction_mode);
11533d71c7a4SMatthias Ringwald             return 0;
11543d71c7a4SMatthias Ringwald         default:
11553d71c7a4SMatthias Ringwald             break;
11563d71c7a4SMatthias Ringwald     }
11573d71c7a4SMatthias Ringwald 
1158bf78aac6SMatthias Ringwald     // track CCC writes
11594ea43905SMatthias Ringwald     if (att_is_persistent_ccc(attribute_handle) && (offset == 0u) && (buffer_size == 2u)){
1160bf78aac6SMatthias Ringwald         att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0));
1161bf78aac6SMatthias Ringwald     }
1162bf78aac6SMatthias Ringwald 
116301ac2008SMatthias Ringwald     att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
116401ac2008SMatthias Ringwald     if (!callback) return 0;
11653d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size);
11663d71c7a4SMatthias Ringwald }
11673d71c7a4SMatthias Ringwald 
11683d71c7a4SMatthias Ringwald /**
11693d71c7a4SMatthias Ringwald  * @brief register read/write callbacks for specific handle range
11703d71c7a4SMatthias Ringwald  * @param att_service_handler_t
11713d71c7a4SMatthias Ringwald  */
11723d71c7a4SMatthias Ringwald void att_server_register_service_handler(att_service_handler_t * handler){
1173ec8f5414SMilanka Ringwald     bool att_server_registered = false;
1174ec8f5414SMilanka Ringwald     if (att_service_handler_for_handle(handler->start_handle)){
1175ec8f5414SMilanka Ringwald         att_server_registered = true;
1176ec8f5414SMilanka Ringwald     }
1177ec8f5414SMilanka Ringwald 
1178ec8f5414SMilanka Ringwald     if (att_service_handler_for_handle(handler->end_handle)){
1179ec8f5414SMilanka Ringwald         att_server_registered = true;
1180ec8f5414SMilanka Ringwald     }
1181ec8f5414SMilanka Ringwald 
1182ec8f5414SMilanka Ringwald     if (att_server_registered){
11833d71c7a4SMatthias Ringwald         log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle);
11843d71c7a4SMatthias Ringwald         return;
11853d71c7a4SMatthias Ringwald     }
11863d71c7a4SMatthias Ringwald     btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler);
11873d71c7a4SMatthias Ringwald }
11883d71c7a4SMatthias Ringwald 
11893deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){
11903deb3ec6SMatthias Ringwald 
11913d71c7a4SMatthias Ringwald     // store callbacks
11923d71c7a4SMatthias Ringwald     att_server_client_read_callback  = read_callback;
11933d71c7a4SMatthias Ringwald     att_server_client_write_callback = write_callback;
11943d71c7a4SMatthias Ringwald 
11951a389cdcSMatthias Ringwald     // register for HCI Events
1196d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &att_event_packet_handler;
11971a389cdcSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
11981a389cdcSMatthias Ringwald 
1199406722e4SMatthias Ringwald     // register for SM events
1200d9a7306aSMatthias Ringwald     sm_event_callback_registration.callback = &att_event_packet_handler;
1201406722e4SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
12023deb3ec6SMatthias Ringwald 
12031a389cdcSMatthias Ringwald     // and L2CAP ATT Server PDUs
12043deb3ec6SMatthias Ringwald     att_dispatch_register_server(att_packet_handler);
12053deb3ec6SMatthias Ringwald 
120670dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
120770dca4d4SMatthias Ringwald     // setup l2cap service
1208f2372b44SMatthias Ringwald     l2cap_register_service(&att_event_packet_handler, PSM_ATT, 0xffff, gap_get_security_level());
120970dca4d4SMatthias Ringwald #endif
121070dca4d4SMatthias Ringwald 
12113deb3ec6SMatthias Ringwald     att_set_db(db);
12123d71c7a4SMatthias Ringwald     att_set_read_callback(att_server_read_callback);
12133d71c7a4SMatthias Ringwald     att_set_write_callback(att_server_write_callback);
12143deb3ec6SMatthias Ringwald }
12153deb3ec6SMatthias Ringwald 
12163deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){
12173deb3ec6SMatthias Ringwald     att_client_packet_handler = handler;
12183deb3ec6SMatthias Ringwald }
12193deb3ec6SMatthias Ringwald 
1220cbbb12d9SMatthias Ringwald 
1221cbbb12d9SMatthias Ringwald // to be deprecated
1222c141988cSMatthias Ringwald int  att_server_can_send_packet_now(hci_con_handle_t con_handle){
122368591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
122468591e36SMatthias Ringwald     if (!hci_connection) return 0;
122568591e36SMatthias Ringwald     return att_server_can_send_packet(hci_connection);
12261b96b007SMatthias Ringwald }
12271b96b007SMatthias Ringwald 
12289816429dSMatthias Ringwald uint8_t att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
1229cbbb12d9SMatthias Ringwald     return att_server_request_to_send_notification(callback_registration, con_handle);
12303deb3ec6SMatthias Ringwald }
12313deb3ec6SMatthias Ringwald 
1232cbbb12d9SMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){
1233cbbb12d9SMatthias Ringwald     att_client_waiting_for_can_send_registration.callback = &att_emit_can_send_now_event;
1234cbbb12d9SMatthias Ringwald     att_server_request_to_send_notification(&att_client_waiting_for_can_send_registration, con_handle);
1235cbbb12d9SMatthias Ringwald }
1236cbbb12d9SMatthias Ringwald // end of deprecated
1237cbbb12d9SMatthias Ringwald 
12389816429dSMatthias Ringwald uint8_t att_server_request_to_send_notification(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
123968591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
124068591e36SMatthias Ringwald     if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
124168591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
1242c37df6f6SMatthias Ringwald     bool added = btstack_linked_list_add_tail(&att_server->notification_requests, (btstack_linked_item_t*) callback_registration);
124368591e36SMatthias Ringwald     att_server_request_can_send_now(hci_connection);
1244c37df6f6SMatthias Ringwald     if (added){
1245cbbb12d9SMatthias Ringwald         return ERROR_CODE_SUCCESS;
1246c37df6f6SMatthias Ringwald     } else {
1247c37df6f6SMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1248c37df6f6SMatthias Ringwald     }
1249cbbb12d9SMatthias Ringwald }
1250cbbb12d9SMatthias Ringwald 
12519816429dSMatthias Ringwald uint8_t att_server_request_to_send_indication(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
125268591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
125368591e36SMatthias Ringwald     if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
125468591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
1255c37df6f6SMatthias Ringwald     bool added = btstack_linked_list_add_tail(&att_server->indication_requests, (btstack_linked_item_t*) callback_registration);
125668591e36SMatthias Ringwald     att_server_request_can_send_now(hci_connection);
1257c37df6f6SMatthias Ringwald     if (added){
1258969a5bbaSMatthias Ringwald         return ERROR_CODE_SUCCESS;
1259c37df6f6SMatthias Ringwald     } else {
1260c37df6f6SMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1261c37df6f6SMatthias Ringwald     }
1262bb38f057SMatthias Ringwald }
1263bb38f057SMatthias Ringwald 
12649816429dSMatthias Ringwald uint8_t att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){
126568591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
126668591e36SMatthias Ringwald     if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1267388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
126868591e36SMatthias Ringwald 
126968591e36SMatthias Ringwald     if (!att_server_can_send_packet(hci_connection)) return BTSTACK_ACL_BUFFERS_FULL;
12703deb3ec6SMatthias Ringwald 
12713deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
12723deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
1273b6df12b6SMatthias Ringwald     uint16_t size = att_prepare_handle_value_notification(att_connection, attribute_handle, value, value_len, packet_buffer);
1274*23079b39SMatthias Ringwald 
1275*23079b39SMatthias Ringwald     uint8_t status = ERROR_CODE_SUCCESS;
12761676a2b8SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
1277*23079b39SMatthias Ringwald     switch (att_server->bearer_type) {
1278*23079b39SMatthias Ringwald         case ATT_BEARER_UNENHANCED_LE:
1279*23079b39SMatthias Ringwald             status = l2cap_send_prepared_connectionless(att_connection->con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
1280*23079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
1281*23079b39SMatthias Ringwald         case ATT_BEARER_UNENHANCED_CLASSIC:
1282*23079b39SMatthias Ringwald             status = l2cap_send_prepared(att_server->l2cap_cid, size);
12831676a2b8SMatthias Ringwald #endif
1284*23079b39SMatthias Ringwald         default:
1285*23079b39SMatthias Ringwald             btstack_unreachable();
1286*23079b39SMatthias Ringwald             break;
1287*23079b39SMatthias Ringwald     }
1288*23079b39SMatthias Ringwald     return status;
12893deb3ec6SMatthias Ringwald }
12903deb3ec6SMatthias Ringwald 
12919816429dSMatthias Ringwald uint8_t att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){
129268591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
129368591e36SMatthias Ringwald     if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
129468591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
1295388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
129618707219SMatthias Ringwald 
129738b893aaSMilanka Ringwald     if (att_server->value_indication_handle != 0u) return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS;
129868591e36SMatthias Ringwald     if (!att_server_can_send_packet(hci_connection)) return BTSTACK_ACL_BUFFERS_FULL;
12993deb3ec6SMatthias Ringwald 
13003deb3ec6SMatthias Ringwald     // track indication
130118707219SMatthias Ringwald     att_server->value_indication_handle = attribute_handle;
130218707219SMatthias Ringwald     btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout);
130318707219SMatthias Ringwald     btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS);
130418707219SMatthias Ringwald     btstack_run_loop_add_timer(&att_server->value_indication_timer);
13053deb3ec6SMatthias Ringwald 
13063deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
13073deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
1308b6df12b6SMatthias Ringwald     uint16_t size = att_prepare_handle_value_indication(att_connection, attribute_handle, value, value_len, packet_buffer);
1309*23079b39SMatthias Ringwald 
1310*23079b39SMatthias Ringwald     uint8_t status = ERROR_CODE_SUCCESS;
1311*23079b39SMatthias Ringwald     switch (att_server->bearer_type) {
1312*23079b39SMatthias Ringwald         case ATT_BEARER_UNENHANCED_LE:
1313*23079b39SMatthias Ringwald             status = l2cap_send_prepared_connectionless(att_connection->con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
13141676a2b8SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
1315*23079b39SMatthias Ringwald         case ATT_BEARER_UNENHANCED_CLASSIC:
1316*23079b39SMatthias Ringwald             status = l2cap_send_prepared(att_server->l2cap_cid, size);;
13171676a2b8SMatthias Ringwald #endif
1318*23079b39SMatthias Ringwald         default:
1319*23079b39SMatthias Ringwald             btstack_unreachable();
1320*23079b39SMatthias Ringwald             break;
1321*23079b39SMatthias Ringwald     }
1322*23079b39SMatthias Ringwald     return status;
13233deb3ec6SMatthias Ringwald }
13248f9132b5SMilanka Ringwald 
13258f9132b5SMilanka Ringwald uint16_t att_server_get_mtu(hci_con_handle_t con_handle){
132668591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
132768591e36SMatthias Ringwald     if (!hci_connection) return 0;
1328388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
1329b6df12b6SMatthias Ringwald     return att_connection->mtu;
13308f9132b5SMilanka Ringwald }
133135d7f2dfSMilanka Ringwald 
133235d7f2dfSMilanka Ringwald void att_server_deinit(void){
133335d7f2dfSMilanka Ringwald     att_server_client_read_callback  = NULL;
133435d7f2dfSMilanka Ringwald     att_server_client_write_callback = NULL;
133535d7f2dfSMilanka Ringwald     att_client_packet_handler = NULL;
133635d7f2dfSMilanka Ringwald     service_handlers = NULL;
133735d7f2dfSMilanka Ringwald }
1338