xref: /btstack/src/ble/att_server.c (revision be4ca859bf6e8a6f64e8cee0d8749d6bef5c9c38)
13deb3ec6SMatthias Ringwald /*
23deb3ec6SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
33deb3ec6SMatthias Ringwald  *
43deb3ec6SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
53deb3ec6SMatthias Ringwald  * modification, are permitted provided that the following conditions
63deb3ec6SMatthias Ringwald  * are met:
73deb3ec6SMatthias Ringwald  *
83deb3ec6SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
93deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
103deb3ec6SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
113deb3ec6SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
123deb3ec6SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
133deb3ec6SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
143deb3ec6SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
153deb3ec6SMatthias Ringwald  *    from this software without specific prior written permission.
163deb3ec6SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
173deb3ec6SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
183deb3ec6SMatthias Ringwald  *    monetary gain.
193deb3ec6SMatthias Ringwald  *
203deb3ec6SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
213deb3ec6SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
223deb3ec6SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
253deb3ec6SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
263deb3ec6SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
273deb3ec6SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
283deb3ec6SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
293deb3ec6SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
303deb3ec6SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
313deb3ec6SMatthias Ringwald  * SUCH DAMAGE.
323deb3ec6SMatthias Ringwald  *
333deb3ec6SMatthias Ringwald  * Please inquire about commercial licensing options at
343deb3ec6SMatthias Ringwald  * [email protected]
353deb3ec6SMatthias Ringwald  *
363deb3ec6SMatthias Ringwald  */
373deb3ec6SMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "att_server.c"
39ab2c6ae4SMatthias Ringwald 
403deb3ec6SMatthias Ringwald 
413deb3ec6SMatthias Ringwald //
423deb3ec6SMatthias Ringwald // ATT Server Globals
433deb3ec6SMatthias Ringwald //
443deb3ec6SMatthias Ringwald 
453deb3ec6SMatthias Ringwald #include <stdint.h>
463deb3ec6SMatthias Ringwald #include <string.h>
473deb3ec6SMatthias Ringwald #include <inttypes.h>
483deb3ec6SMatthias Ringwald 
497907f069SMatthias Ringwald #include "btstack_config.h"
503deb3ec6SMatthias Ringwald 
51296289e3SMatthias Ringwald #include "ble/att_dispatch.h"
5259c6af15SMatthias Ringwald #include "ble/att_db.h"
5359c6af15SMatthias Ringwald #include "ble/att_server.h"
5459c6af15SMatthias Ringwald #include "ble/core.h"
5559c6af15SMatthias Ringwald #include "ble/le_device_db.h"
5659c6af15SMatthias Ringwald #include "ble/sm.h"
5716ece135SMatthias Ringwald #include "btstack_debug.h"
580e2df43fSMatthias Ringwald #include "btstack_event.h"
593deb3ec6SMatthias Ringwald #include "btstack_memory.h"
600e2df43fSMatthias Ringwald #include "btstack_run_loop.h"
6159c6af15SMatthias Ringwald #include "gap.h"
623deb3ec6SMatthias Ringwald #include "hci.h"
633deb3ec6SMatthias Ringwald #include "hci_dump.h"
643deb3ec6SMatthias Ringwald #include "l2cap.h"
65bf78aac6SMatthias Ringwald #include "btstack_tlv.h"
66d1a1f6a4SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
67d1a1f6a4SMatthias Ringwald #include "ble/sm.h"
6820d4358dSMatthias Ringwald #include "bluetooth_psm.h"
6920d4358dSMatthias Ringwald 
70d1a1f6a4SMatthias Ringwald #endif
71bf78aac6SMatthias Ringwald 
7203f53d81SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT
7303f53d81SMatthias Ringwald #include <stdio.h>
7403f53d81SMatthias Ringwald #endif
7503f53d81SMatthias Ringwald 
766afb9acaSMatthias Ringwald #ifndef NVN_NUM_GATT_SERVER_CCC
776afb9acaSMatthias Ringwald #define NVN_NUM_GATT_SERVER_CCC 20
78bf78aac6SMatthias Ringwald #endif
793deb3ec6SMatthias Ringwald 
80cd45e970SMatthias Ringwald static void att_run_for_context(att_server_t * att_server, att_connection_t * att_connection);
8101ac2008SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle);
825d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle);
8348364364SMatthias Ringwald static void att_server_handle_can_send_now(void);
849a1f55eaSMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server, att_connection_t * att_connection);
85fe3d0cbaSMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server);
8626b42dcfSMatthias Ringwald static void att_server_handle_att_pdu(att_server_t * att_server, att_connection_t * att_connection, uint8_t * packet, uint16_t size);
873deb3ec6SMatthias Ringwald 
883551936eSMatthias Ringwald typedef enum {
8938b893aaSMilanka Ringwald     ATT_SERVER_RUN_PHASE_1_REQUESTS = 0,
903551936eSMatthias Ringwald     ATT_SERVER_RUN_PHASE_2_INDICATIONS,
913551936eSMatthias Ringwald     ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS,
923551936eSMatthias Ringwald } att_server_run_phase_t;
933551936eSMatthias Ringwald 
946a540790SMatthias Ringwald //
956a540790SMatthias Ringwald typedef struct {
966a540790SMatthias Ringwald     uint32_t seq_nr;
976a540790SMatthias Ringwald     uint16_t att_handle;
986a540790SMatthias Ringwald     uint8_t  value;
996a540790SMatthias Ringwald     uint8_t  device_index;
1006a540790SMatthias Ringwald } persistent_ccc_entry_t;
1016a540790SMatthias Ringwald 
10218707219SMatthias Ringwald // global
1031a389cdcSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
104406722e4SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration;
10535d7f2dfSMilanka Ringwald static btstack_packet_handler_t               att_client_packet_handler;
1063d71c7a4SMatthias Ringwald static btstack_linked_list_t                  service_handlers;
1075f141511SMatthias Ringwald static btstack_context_callback_registration_t att_client_waiting_for_can_send_registration;
10818707219SMatthias Ringwald 
1093d71c7a4SMatthias Ringwald static att_read_callback_t                    att_server_client_read_callback;
1103d71c7a4SMatthias Ringwald static att_write_callback_t                   att_server_client_write_callback;
1113d71c7a4SMatthias Ringwald 
1126438547aSMatthias Ringwald // round robin
1136438547aSMatthias Ringwald static hci_con_handle_t att_server_last_can_send_now = HCI_CON_HANDLE_INVALID;
114bf78aac6SMatthias Ringwald 
11502b78fc8SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
11626567ee7SMatthias Ringwald static bool att_server_connections_for_state(att_server_state_t state, att_server_t ** att_server_out, att_connection_t ** att_connection_out){
11718707219SMatthias Ringwald     btstack_linked_list_iterator_t it;
11818707219SMatthias Ringwald     hci_connections_get_iterator(&it);
11918707219SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
12026567ee7SMatthias Ringwald         hci_connection_t * hci_connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
12126567ee7SMatthias Ringwald         if (hci_connection->att_server.state == state) {
12226567ee7SMatthias Ringwald             *att_server_out     = &hci_connection->att_server;
12326567ee7SMatthias Ringwald             *att_connection_out = &hci_connection->att_connection;
12426567ee7SMatthias Ringwald             return true;
12518707219SMatthias Ringwald         }
12626567ee7SMatthias Ringwald     }
12726567ee7SMatthias Ringwald     return false;
12818707219SMatthias Ringwald }
12902b78fc8SMatthias Ringwald #endif
130bb38f057SMatthias Ringwald 
1313f9b77dbSMatthias Ringwald static void att_server_request_can_send_now(att_server_t * att_server, att_connection_t * att_connection ){
13223079b39SMatthias Ringwald     switch (att_server->bearer_type){
13323079b39SMatthias Ringwald         case ATT_BEARER_UNENHANCED_LE:
134b6df12b6SMatthias Ringwald             att_dispatch_server_request_can_send_now_event(att_connection->con_handle);
13523079b39SMatthias Ringwald             break;
13623079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
13723079b39SMatthias Ringwald         case ATT_BEARER_UNENHANCED_CLASSIC:
13823079b39SMatthias Ringwald             l2cap_request_can_send_now_event(att_server->l2cap_cid);
13923079b39SMatthias Ringwald             break;
14023079b39SMatthias Ringwald #endif
14123079b39SMatthias Ringwald         default:
14223079b39SMatthias Ringwald             btstack_unreachable();
14323079b39SMatthias Ringwald             break;
14423079b39SMatthias Ringwald     }
145c8c6e9b4SMatthias Ringwald }
146c8c6e9b4SMatthias Ringwald 
14737a8059dSMatthias Ringwald static bool att_server_can_send_packet(att_server_t * att_server, att_connection_t * att_connection){
14823079b39SMatthias Ringwald     switch (att_server->bearer_type) {
14923079b39SMatthias Ringwald         case ATT_BEARER_UNENHANCED_LE:
1501979f09cSMatthias Ringwald             return att_dispatch_server_can_send_now(att_connection->con_handle) != 0;
15123079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
15223079b39SMatthias Ringwald         case ATT_BEARER_UNENHANCED_CLASSIC:
15323079b39SMatthias Ringwald             return l2cap_can_send_packet_now(att_server->l2cap_cid) != 0;
15423079b39SMatthias Ringwald #endif
15523079b39SMatthias Ringwald         default:
15623079b39SMatthias Ringwald             btstack_unreachable();
15723079b39SMatthias Ringwald             break;
15823079b39SMatthias Ringwald     }
15923079b39SMatthias Ringwald     return false;
160c0f26e1cSMatthias Ringwald }
161c0f26e1cSMatthias Ringwald 
1623deb3ec6SMatthias Ringwald static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){
1635d07396bSMatthias Ringwald     btstack_packet_handler_t packet_handler = att_server_packet_handler_for_handle(attribute_handle);
1645d07396bSMatthias Ringwald     if (!packet_handler) return;
1653deb3ec6SMatthias Ringwald 
1663deb3ec6SMatthias Ringwald     uint8_t event[7];
1673deb3ec6SMatthias Ringwald     int pos = 0;
1685611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE;
1694ea43905SMatthias Ringwald     event[pos++] = sizeof(event) - 2u;
1703deb3ec6SMatthias Ringwald     event[pos++] = status;
171f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, client_handle);
1723deb3ec6SMatthias Ringwald     pos += 2;
173f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, attribute_handle);
174a444112bSMatthias Ringwald     (*packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1753deb3ec6SMatthias Ringwald }
1763deb3ec6SMatthias Ringwald 
1775d07396bSMatthias Ringwald static void att_emit_event_to_all(const uint8_t * event, uint16_t size){
1785d07396bSMatthias Ringwald     // dispatch to app level handler
1799305033eSMatthias Ringwald     if (att_client_packet_handler != NULL){
1805d07396bSMatthias Ringwald         (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size);
1815d07396bSMatthias Ringwald     }
1823deb3ec6SMatthias Ringwald 
1835d07396bSMatthias Ringwald     // dispatch to service handlers
1845d07396bSMatthias Ringwald     btstack_linked_list_iterator_t it;
1855d07396bSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
1865d07396bSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1875d07396bSMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
1885d07396bSMatthias Ringwald         if (!handler->packet_handler) continue;
1895d07396bSMatthias Ringwald         (*handler->packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size);
1905d07396bSMatthias Ringwald     }
1915d07396bSMatthias Ringwald }
1925d07396bSMatthias Ringwald 
1935d07396bSMatthias Ringwald static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){
1943deb3ec6SMatthias Ringwald     uint8_t event[6];
1953deb3ec6SMatthias Ringwald     int pos = 0;
1965611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE;
1974ea43905SMatthias Ringwald     event[pos++] = sizeof(event) - 2u;
198fc64f94aSMatthias Ringwald     little_endian_store_16(event, pos, con_handle);
1993deb3ec6SMatthias Ringwald     pos += 2;
200f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, mtu);
2015d07396bSMatthias Ringwald 
2025d07396bSMatthias Ringwald     // also dispatch to GATT Clients
203b12646c5SJakob Krantz     att_dispatch_server_mtu_exchanged(con_handle, mtu);
2045d07396bSMatthias Ringwald 
2055d07396bSMatthias Ringwald     // dispatch to app level handler and service handlers
2065d07396bSMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
2073deb3ec6SMatthias Ringwald }
2083deb3ec6SMatthias Ringwald 
2095f141511SMatthias Ringwald static void att_emit_can_send_now_event(void * context){
2105f141511SMatthias Ringwald     UNUSED(context);
2113c97b242SMatthias Ringwald     if (!att_client_packet_handler) return;
2123c97b242SMatthias Ringwald 
2131b96b007SMatthias Ringwald     uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0};
2141b96b007SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
2151b96b007SMatthias Ringwald }
2161b96b007SMatthias Ringwald 
2176d2214cbSMatthias Ringwald static void att_emit_connected_event(att_server_t * att_server,  att_connection_t * att_connection){
218ff1bec95SMatthias Ringwald     uint8_t event[11];
219ff1bec95SMatthias Ringwald     int pos = 0;
220ff1bec95SMatthias Ringwald     event[pos++] = ATT_EVENT_CONNECTED;
2214ea43905SMatthias Ringwald     event[pos++] = sizeof(event) - 2u;
222ff1bec95SMatthias Ringwald     event[pos++] = att_server->peer_addr_type;
223ff1bec95SMatthias Ringwald     reverse_bd_addr(att_server->peer_address, &event[pos]);
224ff1bec95SMatthias Ringwald     pos += 6;
225b6df12b6SMatthias Ringwald     little_endian_store_16(event, pos, att_connection->con_handle);
226ff1bec95SMatthias Ringwald     pos += 2;
227ff1bec95SMatthias Ringwald 
228ff1bec95SMatthias Ringwald     // dispatch to app level handler and service handlers
229ff1bec95SMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
230ff1bec95SMatthias Ringwald }
231ff1bec95SMatthias Ringwald 
232ff1bec95SMatthias Ringwald 
2336187ad4cSMatthias Ringwald static void att_emit_disconnected_event(uint16_t con_handle){
234ff1bec95SMatthias Ringwald     uint8_t event[4];
235ff1bec95SMatthias Ringwald     int pos = 0;
236ff1bec95SMatthias Ringwald     event[pos++] = ATT_EVENT_DISCONNECTED;
2374ea43905SMatthias Ringwald     event[pos++] = sizeof(event) - 2u;
2386187ad4cSMatthias Ringwald     little_endian_store_16(event, pos, con_handle);
239ff1bec95SMatthias Ringwald     pos += 2;
240ff1bec95SMatthias Ringwald 
241ff1bec95SMatthias Ringwald     // dispatch to app level handler and service handlers
242ff1bec95SMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
243ff1bec95SMatthias Ringwald }
244ff1bec95SMatthias Ringwald 
245ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){
24654178543SMatthias Ringwald     void * context = btstack_run_loop_get_timer_context(ts);
24754178543SMatthias Ringwald     hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
24868591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
24968591e36SMatthias Ringwald     if (!hci_connection) return;
25068591e36SMatthias Ringwald     // @note: after a transaction timeout, no more requests shall be sent over this ATT Bearer
251bce48a26SMatthias Ringwald     // (that's why we don't reset the value_indication_handle)
252b6df12b6SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
25318707219SMatthias Ringwald     uint16_t att_handle = att_server->value_indication_handle;
254388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
25538b893aaSMilanka Ringwald     att_handle_value_indication_notify_client((uint8_t)ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_connection->con_handle, att_handle);
2563deb3ec6SMatthias Ringwald }
2573deb3ec6SMatthias Ringwald 
2583deb3ec6SMatthias Ringwald static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2599ec2630cSMatthias Ringwald 
2607dad9ff8SMatthias Ringwald     UNUSED(channel); // ok: there is no channel
2617dad9ff8SMatthias Ringwald     UNUSED(size);    // ok: handling own l2cap events
2623deb3ec6SMatthias Ringwald 
26318707219SMatthias Ringwald     att_server_t * att_server;
264b6df12b6SMatthias Ringwald     att_connection_t * att_connection;
26518707219SMatthias Ringwald     hci_con_handle_t con_handle;
26668591e36SMatthias Ringwald     hci_connection_t * hci_connection;
26770dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
26870dca4d4SMatthias Ringwald     bd_addr_t address;
26968591e36SMatthias Ringwald     btstack_linked_list_iterator_t it;
27070dca4d4SMatthias Ringwald #endif
27118707219SMatthias Ringwald 
2723deb3ec6SMatthias Ringwald     switch (packet_type) {
2733deb3ec6SMatthias Ringwald 
2743deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
2750e2df43fSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
2763deb3ec6SMatthias Ringwald 
27770dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
27870dca4d4SMatthias Ringwald                 case L2CAP_EVENT_INCOMING_CONNECTION:
27970dca4d4SMatthias Ringwald                     l2cap_event_incoming_connection_get_address(packet, address);
28070dca4d4SMatthias Ringwald                     l2cap_accept_connection(channel);
28136de8547SMatthias Ringwald                     log_info("Accept incoming connection from %s", bd_addr_to_str(address));
28270dca4d4SMatthias Ringwald                     break;
28370dca4d4SMatthias Ringwald                 case L2CAP_EVENT_CHANNEL_OPENED:
284122b723eSMatthias Ringwald                     con_handle = l2cap_event_channel_opened_get_handle(packet);
28568591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
28668591e36SMatthias Ringwald                     if (!hci_connection) break;
28768591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
288122b723eSMatthias Ringwald                     // store connection info
28923079b39SMatthias Ringwald                     att_server->bearer_type = ATT_BEARER_UNENHANCED_CLASSIC;
290f16129ceSMatthias Ringwald                     att_server->peer_addr_type = BD_ADDR_TYPE_ACL;
291122b723eSMatthias Ringwald                     l2cap_event_channel_opened_get_address(packet, att_server->peer_address);
292388fa7c4SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
293b6df12b6SMatthias Ringwald                     att_connection->con_handle = con_handle;
29470dca4d4SMatthias Ringwald                     att_server->l2cap_cid = l2cap_event_channel_opened_get_local_cid(packet);
295122b723eSMatthias Ringwald                     // reset connection properties
296122b723eSMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
297b6df12b6SMatthias Ringwald                     att_connection->mtu = l2cap_event_channel_opened_get_remote_mtu(packet);
298b6df12b6SMatthias Ringwald                     att_connection->max_mtu = l2cap_max_mtu();
299b6df12b6SMatthias Ringwald                     if (att_connection->max_mtu > ATT_REQUEST_BUFFER_SIZE){
300b6df12b6SMatthias Ringwald                         att_connection->max_mtu = ATT_REQUEST_BUFFER_SIZE;
301122b723eSMatthias Ringwald                     }
302df33ee14SMatthias Ringwald 
303b6df12b6SMatthias Ringwald                     log_info("Connection opened %s, l2cap cid %04x, mtu %u", bd_addr_to_str(address), att_server->l2cap_cid, att_connection->mtu);
304df33ee14SMatthias Ringwald 
305914988a1SMatthias Ringwald                     // update security params
306b6df12b6SMatthias Ringwald                     att_connection->encryption_key_size = gap_encryption_key_size(con_handle);
307b6df12b6SMatthias Ringwald                     att_connection->authenticated = gap_authenticated(con_handle);
308b6df12b6SMatthias Ringwald                     att_connection->secure_connection = gap_secure_connection(con_handle);
309914988a1SMatthias Ringwald                     log_info("encrypted key size %u, authenticated %u, secure connection %u",
310b6df12b6SMatthias Ringwald                         att_connection->encryption_key_size, att_connection->authenticated, att_connection->secure_connection);
311df33ee14SMatthias Ringwald 
312df33ee14SMatthias Ringwald                     // notify connection opened
3136d2214cbSMatthias Ringwald                     att_emit_connected_event(att_server, att_connection);
314df33ee14SMatthias Ringwald 
315914988a1SMatthias Ringwald                     // restore persisten ccc if encrypted
316914988a1SMatthias Ringwald                     if ( gap_security_level(con_handle) >= LEVEL_2){
3179a1f55eaSMatthias Ringwald                         att_server_persistent_ccc_restore(att_server, att_connection);
318914988a1SMatthias Ringwald                     }
319122b723eSMatthias Ringwald                     // TODO: what to do about le device db?
320122b723eSMatthias Ringwald                     att_server->pairing_active = 0;
32170dca4d4SMatthias Ringwald                     break;
32248364364SMatthias Ringwald                 case L2CAP_EVENT_CAN_SEND_NOW:
32348364364SMatthias Ringwald                     att_server_handle_can_send_now();
32448364364SMatthias Ringwald                     break;
32548364364SMatthias Ringwald 
32670dca4d4SMatthias Ringwald #endif
3273deb3ec6SMatthias Ringwald                 case HCI_EVENT_LE_META:
3283deb3ec6SMatthias Ringwald                     switch (packet[2]) {
3293deb3ec6SMatthias Ringwald                         case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
33018707219SMatthias Ringwald                             con_handle = little_endian_read_16(packet, 4);
33168591e36SMatthias Ringwald                             hci_connection = hci_connection_for_handle(con_handle);
33268591e36SMatthias Ringwald                             if (!hci_connection) break;
33368591e36SMatthias Ringwald                             att_server = &hci_connection->att_server;
3343deb3ec6SMatthias Ringwald                         	// store connection info
33518707219SMatthias Ringwald                         	att_server->peer_addr_type = packet[7];
33618707219SMatthias Ringwald                             reverse_bd_addr(&packet[8], att_server->peer_address);
337388fa7c4SMatthias Ringwald                             att_connection = &hci_connection->att_connection;
338b6df12b6SMatthias Ringwald                             att_connection->con_handle = con_handle;
3393deb3ec6SMatthias Ringwald                             // reset connection properties
34018707219SMatthias Ringwald                             att_server->state = ATT_SERVER_IDLE;
34123079b39SMatthias Ringwald                             att_server->bearer_type = ATT_BEARER_UNENHANCED_LE;
342b6df12b6SMatthias Ringwald                             att_connection->mtu = ATT_DEFAULT_MTU;
343b6df12b6SMatthias Ringwald                             att_connection->max_mtu = l2cap_max_le_mtu();
344b6df12b6SMatthias Ringwald                             if (att_connection->max_mtu > ATT_REQUEST_BUFFER_SIZE){
345b6df12b6SMatthias Ringwald                                 att_connection->max_mtu = ATT_REQUEST_BUFFER_SIZE;
34699c58ad0SMatthias Ringwald                             }
34738b893aaSMilanka Ringwald                             att_connection->encryption_key_size = 0u;
34838b893aaSMilanka Ringwald                             att_connection->authenticated = 0u;
34938b893aaSMilanka Ringwald 		                	att_connection->authorized = 0u;
350b2c1e52cSMatthias Ringwald                             // workaround: identity resolving can already be complete, at least store result
351b2c1e52cSMatthias Ringwald                             att_server->ir_le_device_db_index = sm_le_device_index(con_handle);
35238b893aaSMilanka Ringwald                             att_server->ir_lookup_active = 0u;
35338b893aaSMilanka Ringwald                             att_server->pairing_active = 0u;
354ff1bec95SMatthias Ringwald                             // notify all - old
3555d07396bSMatthias Ringwald                             att_emit_event_to_all(packet, size);
356ff1bec95SMatthias Ringwald                             // notify all - new
3576d2214cbSMatthias Ringwald                             att_emit_connected_event(att_server, att_connection);
3583deb3ec6SMatthias Ringwald                             break;
3593deb3ec6SMatthias Ringwald 
3603deb3ec6SMatthias Ringwald                         default:
3613deb3ec6SMatthias Ringwald                             break;
3623deb3ec6SMatthias Ringwald                     }
3633deb3ec6SMatthias Ringwald                     break;
3643deb3ec6SMatthias Ringwald 
3653deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_CHANGE:
3668601e696SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_CHANGE_V2:
3673deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE:
3683deb3ec6SMatthias Ringwald                 	// check handle
36918707219SMatthias Ringwald                     con_handle = little_endian_read_16(packet, 3);
37068591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
37168591e36SMatthias Ringwald                     if (!hci_connection) break;
372914988a1SMatthias Ringwald                     if (gap_get_connection_type(con_handle) != GAP_CONNECTION_LE) break;
373914988a1SMatthias Ringwald                     // update security params
37468591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
375388fa7c4SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
376b6df12b6SMatthias Ringwald                     att_connection->encryption_key_size = gap_encryption_key_size(con_handle);
377ff3f1026SMatthias Ringwald                     att_connection->authenticated = gap_authenticated(con_handle) ? 1 : 0;
378f461b3dfSMatthias Ringwald                     att_connection->secure_connection = gap_secure_connection(con_handle) ? 1 : 0;
379914988a1SMatthias Ringwald                     log_info("encrypted key size %u, authenticated %u, secure connection %u",
380b6df12b6SMatthias Ringwald                         att_connection->encryption_key_size, att_connection->authenticated, att_connection->secure_connection);
381914988a1SMatthias Ringwald                     if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){
3824097998aSMatthias Ringwald                         // restore CCC values when encrypted for LE Connections
38322bfc225SMilanka Ringwald                         if (hci_event_encryption_change_get_encryption_enabled(packet) != 0){
3849a1f55eaSMatthias Ringwald                             att_server_persistent_ccc_restore(att_server, att_connection);
38501ac2008SMatthias Ringwald                         }
38601ac2008SMatthias Ringwald                     }
387cd45e970SMatthias Ringwald                     att_run_for_context(att_server, att_connection);
3883deb3ec6SMatthias Ringwald                     break;
3893deb3ec6SMatthias Ringwald 
3903deb3ec6SMatthias Ringwald                 case HCI_EVENT_DISCONNECTION_COMPLETE:
39170a390c7SMatthias Ringwald                     // check handle
39218707219SMatthias Ringwald                     con_handle = hci_event_disconnection_complete_get_connection_handle(packet);
39368591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
39468591e36SMatthias Ringwald                     if (!hci_connection) break;
39568591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
396388fa7c4SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
397b6df12b6SMatthias Ringwald                     att_clear_transaction_queue(att_connection);
398b6df12b6SMatthias Ringwald                     att_connection->con_handle = 0;
399760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
40018707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
40138b893aaSMilanka Ringwald                     if (att_server->value_indication_handle != 0u){
40227328ecbSMatthias Ringwald                         btstack_run_loop_remove_timer(&att_server->value_indication_timer);
403bce48a26SMatthias Ringwald                         uint16_t att_handle = att_server->value_indication_handle;
40438b893aaSMilanka Ringwald                         att_server->value_indication_handle = 0u; // reset error state
40538b893aaSMilanka Ringwald                         att_handle_value_indication_notify_client((uint8_t)ATT_HANDLE_VALUE_INDICATION_DISCONNECT, att_connection->con_handle, att_handle);
406bce48a26SMatthias Ringwald                     }
407ff1bec95SMatthias Ringwald                     // notify all - new
4086187ad4cSMatthias Ringwald                     att_emit_disconnected_event(con_handle);
409ff1bec95SMatthias Ringwald                     // notify all - old
4105d07396bSMatthias Ringwald                     att_emit_event_to_all(packet, size);
4113deb3ec6SMatthias Ringwald                     break;
4123deb3ec6SMatthias Ringwald 
413760ad805SMatthias Ringwald                 // Identity Resolving
4145611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_STARTED:
41518707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_started_get_handle(packet);
41668591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
41768591e36SMatthias Ringwald                     if (!hci_connection) break;
41868591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
4195611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED");
42030c8e114SMatthias Ringwald                     att_server->ir_lookup_active = 1;
4213deb3ec6SMatthias Ringwald                     break;
4225611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
423760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
42468591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
42568591e36SMatthias Ringwald                     if (!hci_connection) return;
42668591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
42730c8e114SMatthias Ringwald                     att_server->ir_lookup_active = 0;
4281b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index(packet);
4291b7acd0dSMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED");
430cd45e970SMatthias Ringwald                     att_run_for_context(att_server, att_connection);
4313deb3ec6SMatthias Ringwald                     break;
4325611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_FAILED:
43318707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_failed_get_handle(packet);
43468591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
43568591e36SMatthias Ringwald                     if (!hci_connection) break;
43668591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
4375611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED");
43830c8e114SMatthias Ringwald                     att_server->ir_lookup_active = 0;
43918707219SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
440cd45e970SMatthias Ringwald                     att_run_for_context(att_server, att_connection);
4413deb3ec6SMatthias Ringwald                     break;
442760ad805SMatthias Ringwald 
443760ad805SMatthias Ringwald                 // Pairing started - delete stored CCC values
444760ad805SMatthias Ringwald                 // - assumes pairing indicates either new device or re-pairing, in both cases there should be no stored CCC values
445760ad805SMatthias Ringwald                 // - assumes that all events have the con handle as the first field
446760ad805SMatthias Ringwald                 case SM_EVENT_JUST_WORKS_REQUEST:
447760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
448760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_INPUT_NUMBER:
449760ad805SMatthias Ringwald                 case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
450760ad805SMatthias Ringwald                     con_handle = sm_event_just_works_request_get_handle(packet);
45168591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
45268591e36SMatthias Ringwald                     if (!hci_connection) break;
45368591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
454760ad805SMatthias Ringwald                     att_server->pairing_active = 1;
455760ad805SMatthias Ringwald                     log_info("SM Pairing started");
456760ad805SMatthias Ringwald                     if (att_server->ir_le_device_db_index < 0) break;
457cd45e970SMatthias Ringwald                     att_run_for_context(att_server, att_connection);
458760ad805SMatthias Ringwald                     // index not valid anymore
459760ad805SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
460760ad805SMatthias Ringwald                     break;
461760ad805SMatthias Ringwald 
4621b7acd0dSMatthias Ringwald                 // Bonding completed
463760ad805SMatthias Ringwald                 case SM_EVENT_IDENTITY_CREATED:
464760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
46568591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
46668591e36SMatthias Ringwald                     if (!hci_connection) return;
46768591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
468760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
4691b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_created_get_index(packet);
470cd45e970SMatthias Ringwald                     att_run_for_context(att_server, att_connection);
4711b7acd0dSMatthias Ringwald                     break;
4721b7acd0dSMatthias Ringwald 
4731b7acd0dSMatthias Ringwald                 // Pairing complete (with/without bonding=storing of pairing information)
4741b7acd0dSMatthias Ringwald                 case SM_EVENT_PAIRING_COMPLETE:
4751b7acd0dSMatthias Ringwald                     con_handle = sm_event_pairing_complete_get_handle(packet);
47668591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
47768591e36SMatthias Ringwald                     if (!hci_connection) return;
47868591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
4791b7acd0dSMatthias Ringwald                     att_server->pairing_active = 0;
480cd45e970SMatthias Ringwald                     att_run_for_context(att_server, att_connection);
481760ad805SMatthias Ringwald                     break;
482760ad805SMatthias Ringwald 
483760ad805SMatthias Ringwald                 // Authorization
4845611a760SMatthias Ringwald                 case SM_EVENT_AUTHORIZATION_RESULT: {
48518707219SMatthias Ringwald                     con_handle = sm_event_authorization_result_get_handle(packet);
48668591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
48768591e36SMatthias Ringwald                     if (!hci_connection) break;
48868591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
489388fa7c4SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
490b6df12b6SMatthias Ringwald                     att_connection->authorized = sm_event_authorization_result_get_authorization_result(packet);
4913f9b77dbSMatthias Ringwald                     att_server_request_can_send_now(att_server, att_connection);
4923deb3ec6SMatthias Ringwald                 	break;
4933deb3ec6SMatthias Ringwald                 }
4943deb3ec6SMatthias Ringwald                 default:
4953deb3ec6SMatthias Ringwald                     break;
4963deb3ec6SMatthias Ringwald             }
49765b44ffdSMatthias Ringwald             break;
49870dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
49970dca4d4SMatthias Ringwald         case L2CAP_DATA_PACKET:
50068591e36SMatthias Ringwald             hci_connections_get_iterator(&it);
50168591e36SMatthias Ringwald             while(btstack_linked_list_iterator_has_next(&it)){
502388fa7c4SMatthias Ringwald                 hci_connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
503388fa7c4SMatthias Ringwald                 att_server = &hci_connection->att_server;
50426b42dcfSMatthias Ringwald                 att_connection = &hci_connection->att_connection;
50568591e36SMatthias Ringwald                 if (att_server->l2cap_cid == channel) {
50626b42dcfSMatthias Ringwald                     att_server_handle_att_pdu(att_server, att_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 
5180f70c52eSMatthias Ringwald static uint8_t
5190f70c52eSMatthias Ringwald att_server_send_prepared(const att_server_t *att_server, const att_connection_t *att_connection, uint8_t *buffer,
5200f70c52eSMatthias Ringwald                          uint16_t size) {
521d415948cSMatthias Ringwald     uint8_t status = ERROR_CODE_SUCCESS;
522d415948cSMatthias Ringwald     switch (att_server->bearer_type) {
523d415948cSMatthias Ringwald         case ATT_BEARER_UNENHANCED_LE:
524d415948cSMatthias Ringwald             status = l2cap_send_prepared_connectionless(att_connection->con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
525d415948cSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
526d415948cSMatthias Ringwald         case ATT_BEARER_UNENHANCED_CLASSIC:
527d415948cSMatthias Ringwald             status = l2cap_send_prepared(att_server->l2cap_cid, size);
528d415948cSMatthias Ringwald             break;
529d415948cSMatthias Ringwald #endif
5300f70c52eSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
5310f70c52eSMatthias Ringwald         case ATT_BEARER_ENHANCED_LE:
5320f70c52eSMatthias Ringwald             btstack_assert(buffer != NULL);
5330f70c52eSMatthias Ringwald             status = l2cap_send(att_server->l2cap_cid, buffer, size);
5340f70c52eSMatthias Ringwald             break;
5350f70c52eSMatthias Ringwald #endif
536d415948cSMatthias Ringwald         default:
537d415948cSMatthias Ringwald             btstack_unreachable();
538d415948cSMatthias Ringwald             break;
539d415948cSMatthias Ringwald     }
540d415948cSMatthias Ringwald     return status;
541d415948cSMatthias Ringwald }
542d415948cSMatthias Ringwald 
5437a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
5443deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
54526567ee7SMatthias Ringwald     att_server_t * att_server = NULL;
54626567ee7SMatthias Ringwald     att_connection_t * att_connection = NULL;
54726567ee7SMatthias Ringwald     bool found = att_server_connections_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION, &att_server, &att_connection);
5483deb3ec6SMatthias Ringwald 
5493deb3ec6SMatthias Ringwald     uint8_t hash_flipped[8];
5509c80e4ccSMatthias Ringwald     reverse_64(hash, hash_flipped);
55126567ee7SMatthias Ringwald     if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8) != 0){
5523deb3ec6SMatthias Ringwald         log_info("ATT Signed Write, invalid signature");
55303f53d81SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT
55403f53d81SMatthias Ringwald         printf("ATT Signed Write, invalid signature\n");
55503f53d81SMatthias Ringwald #endif
55618707219SMatthias Ringwald         att_server->state = ATT_SERVER_IDLE;
5573deb3ec6SMatthias Ringwald         return;
5583deb3ec6SMatthias Ringwald     }
5593deb3ec6SMatthias Ringwald     log_info("ATT Signed Write, valid signature");
56003f53d81SMatthias Ringwald #ifdef ENABLE_TESTING_SUPPORT
56103f53d81SMatthias Ringwald     printf("ATT Signed Write, valid signature\n");
56203f53d81SMatthias Ringwald #endif
5633deb3ec6SMatthias Ringwald 
5643deb3ec6SMatthias Ringwald     // update sequence number
56518707219SMatthias Ringwald     uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
56618707219SMatthias Ringwald     le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1);
56718707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
5683f9b77dbSMatthias Ringwald     att_server_request_can_send_now(att_server, att_connection);
5693deb3ec6SMatthias Ringwald }
5707a766ebfSMatthias Ringwald #endif
57188a48dd8SMatthias Ringwald 
57218707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED
573b06f2579SMatthias Ringwald // pre: can send now
574b06f2579SMatthias Ringwald // returns: 1 if packet was sent
5759fe63f3aSMatthias Ringwald static int att_server_process_validated_request(att_server_t * att_server, att_connection_t * att_connection){
576b06f2579SMatthias Ringwald 
577b06f2579SMatthias Ringwald     l2cap_reserve_packet_buffer();
578b06f2579SMatthias Ringwald     uint8_t * att_response_buffer = l2cap_get_outgoing_buffer();
579b6df12b6SMatthias Ringwald     uint16_t  att_response_size   = att_handle_request(att_connection, att_server->request_buffer, att_server->request_size, att_response_buffer);
580b06f2579SMatthias Ringwald 
581beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
582c1ab6cc1SMatthias Ringwald     if ((att_response_size == ATT_READ_RESPONSE_PENDING) || (att_response_size == ATT_INTERNAL_WRITE_RESPONSE_PENDING)){
583e404a688SMatthias Ringwald         // update state
584beb20288SMatthias Ringwald         att_server->state = ATT_SERVER_RESPONSE_PENDING;
585e404a688SMatthias Ringwald 
58656c3a347SMatthias Ringwald         // callback with handle ATT_READ_RESPONSE_PENDING for reads
58756c3a347SMatthias Ringwald         if (att_response_size == ATT_READ_RESPONSE_PENDING){
588b6df12b6SMatthias Ringwald             att_server_client_read_callback(att_connection->con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0);
58956c3a347SMatthias Ringwald         }
590e8e7403eSMatthias Ringwald 
591e8e7403eSMatthias Ringwald         // free reserved buffer
592e8e7403eSMatthias Ringwald         l2cap_release_packet_buffer();
593e8e7403eSMatthias Ringwald         return 0;
594e404a688SMatthias Ringwald     }
595e404a688SMatthias Ringwald #endif
596e404a688SMatthias Ringwald 
597b06f2579SMatthias Ringwald     // intercept "insufficient authorization" for authenticated connections to allow for user authorization
5984ea43905SMatthias Ringwald     if ((att_response_size     >= 4u)
599b06f2579SMatthias Ringwald     && (att_response_buffer[0] == ATT_ERROR_RESPONSE)
600b06f2579SMatthias Ringwald     && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION)
60138b893aaSMilanka Ringwald     && (att_connection->authenticated != 0u)){
602b06f2579SMatthias Ringwald 
603b6df12b6SMatthias Ringwald         switch (gap_authorization_state(att_connection->con_handle)){
604b06f2579SMatthias Ringwald             case AUTHORIZATION_UNKNOWN:
605b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
606b6df12b6SMatthias Ringwald                 sm_request_pairing(att_connection->con_handle);
607b06f2579SMatthias Ringwald                 return 0;
608b06f2579SMatthias Ringwald             case AUTHORIZATION_PENDING:
609b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
610b06f2579SMatthias Ringwald                 return 0;
611b06f2579SMatthias Ringwald             default:
612b06f2579SMatthias Ringwald                 break;
613b06f2579SMatthias Ringwald         }
614b06f2579SMatthias Ringwald     }
615b06f2579SMatthias Ringwald 
61618707219SMatthias Ringwald     att_server->state = ATT_SERVER_IDLE;
6174ea43905SMatthias Ringwald     if (att_response_size == 0u) {
618b06f2579SMatthias Ringwald         l2cap_release_packet_buffer();
619b06f2579SMatthias Ringwald         return 0;
620b06f2579SMatthias Ringwald     }
621b06f2579SMatthias Ringwald 
6220f70c52eSMatthias Ringwald     (void) att_server_send_prepared(att_server, att_connection, NULL, att_response_size);
623b06f2579SMatthias Ringwald 
624b06f2579SMatthias Ringwald     // notify client about MTU exchange result
625b06f2579SMatthias Ringwald     if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){
626b6df12b6SMatthias Ringwald         att_emit_mtu_event(att_connection->con_handle, att_connection->mtu);
627b06f2579SMatthias Ringwald     }
628b06f2579SMatthias Ringwald     return 1;
629b06f2579SMatthias Ringwald }
630b06f2579SMatthias Ringwald 
631beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
6329816429dSMatthias Ringwald uint8_t att_server_response_ready(hci_con_handle_t con_handle){
63368591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
63468591e36SMatthias Ringwald     if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
63568591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
6363f9b77dbSMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
637beb20288SMatthias Ringwald     if (att_server->state != ATT_SERVER_RESPONSE_PENDING)   return ERROR_CODE_COMMAND_DISALLOWED;
638e404a688SMatthias Ringwald 
639e404a688SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
6403f9b77dbSMatthias Ringwald     att_server_request_can_send_now(att_server, att_connection);
641e404a688SMatthias Ringwald     return ERROR_CODE_SUCCESS;
642e404a688SMatthias Ringwald }
643e404a688SMatthias Ringwald #endif
644e404a688SMatthias Ringwald 
645cd45e970SMatthias Ringwald static void att_run_for_context(att_server_t * att_server, att_connection_t * att_connection){
64618707219SMatthias Ringwald     switch (att_server->state){
6473deb3ec6SMatthias Ringwald         case ATT_SERVER_REQUEST_RECEIVED:
64823079b39SMatthias Ringwald             switch (att_server->bearer_type){
64923079b39SMatthias Ringwald                 case ATT_BEARER_UNENHANCED_LE:
6500234fbbcSMatthias Ringwald                     // wait until re-encryption as central is complete
65123079b39SMatthias Ringwald                     if (gap_reconnect_security_setup_active(att_connection->con_handle)) {
65223079b39SMatthias Ringwald                         return;
65323079b39SMatthias Ringwald                     };
65423079b39SMatthias Ringwald                     break;
65523079b39SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
65623079b39SMatthias Ringwald                 case ATT_BEARER_UNENHANCED_CLASSIC:
65723079b39SMatthias Ringwald                     // ok
65823079b39SMatthias Ringwald                     break;
65923079b39SMatthias Ringwald #endif
66023079b39SMatthias Ringwald                 default:
66123079b39SMatthias Ringwald                     btstack_unreachable();
66223079b39SMatthias Ringwald                     break;
66348364364SMatthias Ringwald             }
6640234fbbcSMatthias Ringwald 
665760ad805SMatthias Ringwald             // wait until pairing is complete
666760ad805SMatthias Ringwald             if (att_server->pairing_active) break;
667760ad805SMatthias Ringwald 
6687a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
66918707219SMatthias Ringwald             if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){
6703deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write!");
6713deb3ec6SMatthias Ringwald                 if (!sm_cmac_ready()) {
6723deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, sm_cmac engine not ready. Abort");
67318707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
6743deb3ec6SMatthias Ringwald                     return;
6753deb3ec6SMatthias Ringwald                 }
67618707219SMatthias Ringwald                 if (att_server->request_size < (3 + 12)) {
6773deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, request to short. Abort.");
67818707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
6793deb3ec6SMatthias Ringwald                     return;
6803deb3ec6SMatthias Ringwald                 }
68118707219SMatthias Ringwald                 if (att_server->ir_lookup_active){
6823deb3ec6SMatthias Ringwald                     return;
6833deb3ec6SMatthias Ringwald                 }
68418707219SMatthias Ringwald                 if (att_server->ir_le_device_db_index < 0){
6853deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, CSRK not available");
68618707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
6873deb3ec6SMatthias Ringwald                     return;
6883deb3ec6SMatthias Ringwald                 }
6893deb3ec6SMatthias Ringwald 
6903deb3ec6SMatthias Ringwald                 // check counter
69118707219SMatthias Ringwald                 uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
69218707219SMatthias Ringwald                 uint32_t counter_db     = le_device_db_remote_counter_get(att_server->ir_le_device_db_index);
6933deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet);
6943deb3ec6SMatthias Ringwald                 if (counter_packet < counter_db){
6953deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, db reports higher counter, abort");
69618707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
6973deb3ec6SMatthias Ringwald                     return;
6983deb3ec6SMatthias Ringwald                 }
6993deb3ec6SMatthias Ringwald 
7003deb3ec6SMatthias Ringwald                 // signature is { sequence counter, secure hash }
7013deb3ec6SMatthias Ringwald                 sm_key_t csrk;
70218707219SMatthias Ringwald                 le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk);
70318707219SMatthias Ringwald                 att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION;
7043deb3ec6SMatthias Ringwald                 log_info("Orig Signature: ");
70518707219SMatthias Ringwald                 log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8);
70618707219SMatthias Ringwald                 uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1);
70718707219SMatthias 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);
7083deb3ec6SMatthias Ringwald                 return;
7093deb3ec6SMatthias Ringwald             }
7107a766ebfSMatthias Ringwald #endif
711b06f2579SMatthias Ringwald             // move on
71218707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
7133f9b77dbSMatthias Ringwald             att_server_request_can_send_now(att_server, att_connection);
714b06f2579SMatthias Ringwald             break;
71588a48dd8SMatthias Ringwald 
7163deb3ec6SMatthias Ringwald         default:
7173deb3ec6SMatthias Ringwald             break;
7183deb3ec6SMatthias Ringwald     }
7193deb3ec6SMatthias Ringwald }
7203deb3ec6SMatthias Ringwald 
7211979f09cSMatthias Ringwald static bool att_server_data_ready_for_phase(att_server_t * att_server,  att_server_run_phase_t phase){
72290f03c80SMatthias Ringwald     switch (phase){
72390f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
72490f03c80SMatthias Ringwald             return att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
72590f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
72638b893aaSMilanka Ringwald              return (!btstack_linked_list_empty(&att_server->indication_requests) && (att_server->value_indication_handle == 0u));
72790f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
72890f03c80SMatthias Ringwald             return (!btstack_linked_list_empty(&att_server->notification_requests));
7297bbeb3adSMilanka Ringwald         default:
7307bbeb3adSMilanka Ringwald             btstack_assert(false);
7311979f09cSMatthias Ringwald             return false;
73290f03c80SMatthias Ringwald     }
7337bbeb3adSMilanka Ringwald }
73490f03c80SMatthias Ringwald 
7359d6c0193SMatthias Ringwald static void att_server_trigger_send_for_phase(att_server_t * att_server, att_connection_t * att_connection, att_server_run_phase_t phase){
73690f03c80SMatthias Ringwald     btstack_context_callback_registration_t * client;
73790f03c80SMatthias Ringwald     switch (phase){
73890f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
7390f70c52eSMatthias Ringwald             att_server_process_validated_request(att_server, att_connection, NULL);
74090f03c80SMatthias Ringwald             break;
74190f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
74290f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->indication_requests;
74390f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->indication_requests, (btstack_linked_item_t *) client);
74490f03c80SMatthias Ringwald             client->callback(client->context);
74590f03c80SMatthias Ringwald             break;
74690f03c80SMatthias Ringwald        case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
74790f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->notification_requests;
74890f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->notification_requests, (btstack_linked_item_t *) client);
74990f03c80SMatthias Ringwald             client->callback(client->context);
75090f03c80SMatthias Ringwald             break;
7517bbeb3adSMilanka Ringwald         default:
7527bbeb3adSMilanka Ringwald             btstack_assert(false);
7537bbeb3adSMilanka Ringwald             break;
75490f03c80SMatthias Ringwald     }
75590f03c80SMatthias Ringwald }
75690f03c80SMatthias Ringwald 
7577eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){
758b06f2579SMatthias Ringwald 
7596438547aSMatthias Ringwald     hci_con_handle_t last_send_con_handle = HCI_CON_HANDLE_INVALID;
76068591e36SMatthias Ringwald     hci_connection_t * request_hci_connection   = NULL;
7611979f09cSMatthias Ringwald     bool can_send_now = true;
76238b893aaSMilanka Ringwald     uint8_t phase_index;
7636438547aSMatthias Ringwald 
7644395a000SMatthias Ringwald     for (phase_index = ATT_SERVER_RUN_PHASE_1_REQUESTS; phase_index <= ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS; phase_index++){
7654395a000SMatthias Ringwald         att_server_run_phase_t phase = (att_server_run_phase_t) phase_index;
7666438547aSMatthias Ringwald         hci_con_handle_t skip_connections_until = att_server_last_can_send_now;
767ff3cc4a5SMatthias Ringwald         while (true){
7686438547aSMatthias Ringwald             btstack_linked_list_iterator_t it;
76918707219SMatthias Ringwald             hci_connections_get_iterator(&it);
77018707219SMatthias Ringwald             while(btstack_linked_list_iterator_has_next(&it)){
77118707219SMatthias Ringwald                 hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
77218707219SMatthias Ringwald                 att_server_t * att_server = &connection->att_server;
773388fa7c4SMatthias Ringwald                 att_connection_t * att_connection = &connection->att_connection;
7746438547aSMatthias Ringwald 
7751979f09cSMatthias Ringwald                 bool data_ready = att_server_data_ready_for_phase(att_server, phase);
7766438547aSMatthias Ringwald 
777b6df12b6SMatthias 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);
7786438547aSMatthias Ringwald 
7796438547aSMatthias Ringwald                 // skip until last sender found (which is also skipped)
7806438547aSMatthias Ringwald                 if (skip_connections_until != HCI_CON_HANDLE_INVALID){
78168591e36SMatthias Ringwald                     if (data_ready && (request_hci_connection == NULL)){
78268591e36SMatthias Ringwald                         request_hci_connection = connection;
7836438547aSMatthias Ringwald                     }
784b6df12b6SMatthias Ringwald                     if (skip_connections_until == att_connection->con_handle){
7856438547aSMatthias Ringwald                         skip_connections_until = HCI_CON_HANDLE_INVALID;
7866438547aSMatthias Ringwald                     }
7876438547aSMatthias Ringwald                     continue;
7886438547aSMatthias Ringwald                 };
7896438547aSMatthias Ringwald 
79090f03c80SMatthias Ringwald                 if (data_ready){
791969a5bbaSMatthias Ringwald                     if (can_send_now){
7929d6c0193SMatthias Ringwald                         att_server_trigger_send_for_phase(att_server, att_connection, phase);
793b6df12b6SMatthias Ringwald                         last_send_con_handle = att_connection->con_handle;
79437a8059dSMatthias Ringwald                         can_send_now = att_server_can_send_packet(att_server, att_connection);
79590f03c80SMatthias Ringwald                         data_ready = att_server_data_ready_for_phase(att_server, phase);
79668591e36SMatthias Ringwald                         if (data_ready && (request_hci_connection == NULL)){
79768591e36SMatthias Ringwald                             request_hci_connection = connection;
798cbbb12d9SMatthias Ringwald                         }
799cbbb12d9SMatthias Ringwald                     } else {
80068591e36SMatthias Ringwald                         request_hci_connection = connection;
801f29a88d8SMatthias Ringwald                         break;
802cbbb12d9SMatthias Ringwald                     }
803cbbb12d9SMatthias Ringwald                 }
8043deb3ec6SMatthias Ringwald             }
8053deb3ec6SMatthias Ringwald 
8066438547aSMatthias Ringwald             // stop skipping (handles disconnect by last send connection)
8076438547aSMatthias Ringwald             skip_connections_until = HCI_CON_HANDLE_INVALID;
8086438547aSMatthias Ringwald 
8093551936eSMatthias Ringwald             // Exit loop, if we cannot send
810969a5bbaSMatthias Ringwald             if (!can_send_now) break;
811969a5bbaSMatthias Ringwald 
812969a5bbaSMatthias Ringwald             // Exit loop, if we can send but there are also no further request
81368591e36SMatthias Ringwald             if (request_hci_connection == NULL) break;
814969a5bbaSMatthias Ringwald 
815969a5bbaSMatthias Ringwald             // Finally, if we still can send and there are requests, just try again
81668591e36SMatthias Ringwald             request_hci_connection = NULL;
817969a5bbaSMatthias Ringwald         }
8186438547aSMatthias Ringwald         // update last send con handle for round robin
8196438547aSMatthias Ringwald         if (last_send_con_handle != HCI_CON_HANDLE_INVALID){
8206438547aSMatthias Ringwald             att_server_last_can_send_now = last_send_con_handle;
8216438547aSMatthias Ringwald         }
8223551936eSMatthias Ringwald     }
823969a5bbaSMatthias Ringwald 
82468591e36SMatthias Ringwald     if (request_hci_connection == NULL) return;
8253f9b77dbSMatthias Ringwald 
8263f9b77dbSMatthias Ringwald     att_server_t * att_server = &request_hci_connection->att_server;
8273f9b77dbSMatthias Ringwald     att_connection_t * att_connection = &request_hci_connection->att_connection;
8283f9b77dbSMatthias Ringwald     att_server_request_can_send_now(att_server, att_connection);
829969a5bbaSMatthias Ringwald }
830969a5bbaSMatthias Ringwald 
83126b42dcfSMatthias Ringwald static void att_server_handle_att_pdu(att_server_t * att_server, att_connection_t * att_connection, uint8_t * packet, uint16_t size){
83218707219SMatthias Ringwald 
833838bd521SMatthias Ringwald     uint8_t opcode  = packet[0u];
83438b893aaSMilanka Ringwald     uint8_t method  = opcode & 0x03fu;
835ed68ae9dSMilanka Ringwald     bool invalid = method > ATT_MULTIPLE_HANDLE_VALUE_NTF;
83638b893aaSMilanka Ringwald     bool command = (opcode & 0x40u) != 0u;
837838bd521SMatthias Ringwald 
838838bd521SMatthias Ringwald     // ignore invalid commands
839838bd521SMatthias Ringwald     if (invalid && command){
840838bd521SMatthias Ringwald         return;
841838bd521SMatthias Ringwald     }
842838bd521SMatthias Ringwald 
8433deb3ec6SMatthias Ringwald     // handle value indication confirms
84438b893aaSMilanka Ringwald     if ((opcode == ATT_HANDLE_VALUE_CONFIRMATION) && (att_server->value_indication_handle != 0u)){
84518707219SMatthias Ringwald         btstack_run_loop_remove_timer(&att_server->value_indication_timer);
84618707219SMatthias Ringwald         uint16_t att_handle = att_server->value_indication_handle;
84738b893aaSMilanka Ringwald         att_server->value_indication_handle = 0u;
84838b893aaSMilanka Ringwald         att_handle_value_indication_notify_client(0u, att_connection->con_handle, att_handle);
8493f9b77dbSMatthias Ringwald         att_server_request_can_send_now(att_server, att_connection);
8503deb3ec6SMatthias Ringwald         return;
8513deb3ec6SMatthias Ringwald     }
8523deb3ec6SMatthias Ringwald 
8536428eb5aSMatthias Ringwald     // directly process command
8546428eb5aSMatthias Ringwald     // note: signed write cannot be handled directly as authentication needs to be verified
855838bd521SMatthias Ringwald     if (opcode == ATT_WRITE_COMMAND){
856b6df12b6SMatthias Ringwald         att_handle_request(att_connection, packet, size, NULL);
8576428eb5aSMatthias Ringwald         return;
8586428eb5aSMatthias Ringwald     }
8596428eb5aSMatthias Ringwald 
8603deb3ec6SMatthias Ringwald     // check size
86118707219SMatthias Ringwald     if (size > sizeof(att_server->request_buffer)) {
862e0463bdcSMatthias 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));
8636428eb5aSMatthias Ringwald         return;
8646428eb5aSMatthias Ringwald     }
8653deb3ec6SMatthias Ringwald 
866e0463bdcSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
867e0463bdcSMatthias Ringwald     // abort signed write validation if a new request comes in (but finish previous signed write if possible)
868e0463bdcSMatthias Ringwald     if (att_server->state == ATT_SERVER_W4_SIGNED_WRITE_VALIDATION){
869e0463bdcSMatthias Ringwald         if (packet[0] == ATT_SIGNED_WRITE_COMMAND){
870e0463bdcSMatthias Ringwald             log_info("skip new signed write request as previous is in validation");
871e0463bdcSMatthias Ringwald             return;
872e0463bdcSMatthias Ringwald         } else {
873e0463bdcSMatthias Ringwald             log_info("abort signed write validation to process new request");
874e0463bdcSMatthias Ringwald             att_server->state = ATT_SERVER_IDLE;
875e0463bdcSMatthias Ringwald         }
876e0463bdcSMatthias Ringwald     }
877e0463bdcSMatthias Ringwald #endif
8783deb3ec6SMatthias Ringwald     // last request still in processing?
87918707219SMatthias Ringwald     if (att_server->state != ATT_SERVER_IDLE){
880e0463bdcSMatthias Ringwald         log_info("skip att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state);
8816428eb5aSMatthias Ringwald         return;
8826428eb5aSMatthias Ringwald     }
8833deb3ec6SMatthias Ringwald 
8843deb3ec6SMatthias Ringwald     // store request
88518707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED;
88618707219SMatthias Ringwald     att_server->request_size = size;
8876535961aSMatthias Ringwald     (void)memcpy(att_server->request_buffer, packet, size);
8883deb3ec6SMatthias Ringwald 
889cd45e970SMatthias Ringwald     att_run_for_context(att_server, att_connection);
89041772f37SMatthias Ringwald }
89141772f37SMatthias Ringwald 
89241772f37SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
89368591e36SMatthias Ringwald     hci_connection_t * hci_connection;
894b6df12b6SMatthias Ringwald     att_connection_t * att_connection;
89526b42dcfSMatthias Ringwald     att_server_t     * att_server;
89641772f37SMatthias Ringwald 
89741772f37SMatthias Ringwald     switch (packet_type){
89841772f37SMatthias Ringwald         case HCI_EVENT_PACKET:
89941772f37SMatthias Ringwald             switch (packet[0]){
90041772f37SMatthias Ringwald                 case L2CAP_EVENT_CAN_SEND_NOW:
90141772f37SMatthias Ringwald                     att_server_handle_can_send_now();
90241772f37SMatthias Ringwald                     break;
90341772f37SMatthias Ringwald                 case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
90441772f37SMatthias Ringwald                     // GATT client has negotiated the mtu for this connection
90568591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(handle);
90668591e36SMatthias Ringwald                     if (!hci_connection) break;
907388fa7c4SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
908b6df12b6SMatthias Ringwald                     att_connection->mtu = little_endian_read_16(packet, 4);
90941772f37SMatthias Ringwald                     break;
91041772f37SMatthias Ringwald                 default:
91141772f37SMatthias Ringwald                     break;
91241772f37SMatthias Ringwald             }
91341772f37SMatthias Ringwald             break;
91441772f37SMatthias Ringwald 
91541772f37SMatthias Ringwald         case ATT_DATA_PACKET:
91641772f37SMatthias Ringwald             log_debug("ATT Packet, handle 0x%04x", handle);
91768591e36SMatthias Ringwald             hci_connection = hci_connection_for_handle(handle);
91868591e36SMatthias Ringwald             if (!hci_connection) break;
91941772f37SMatthias Ringwald 
92026b42dcfSMatthias Ringwald             att_server = &hci_connection->att_server;
92126b42dcfSMatthias Ringwald             att_connection = &hci_connection->att_connection;
92226b42dcfSMatthias Ringwald             att_server_handle_att_pdu(att_server, att_connection, packet, size);
923372d62c4SMatthias Ringwald             break;
9247bbeb3adSMilanka Ringwald 
9257bbeb3adSMilanka Ringwald         default:
9267bbeb3adSMilanka Ringwald             break;
927372d62c4SMatthias Ringwald     }
9283deb3ec6SMatthias Ringwald }
9293deb3ec6SMatthias Ringwald 
930bf78aac6SMatthias Ringwald // ---------------------
931bf78aac6SMatthias Ringwald // persistent CCC writes
932bf78aac6SMatthias Ringwald static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){
9334ea43905SMatthias Ringwald     return ('B' << 24u) | ('T' << 16u) | ('C' << 8u) | index;
934bf78aac6SMatthias Ringwald }
935bf78aac6SMatthias Ringwald 
936bf78aac6SMatthias Ringwald static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){
937bf78aac6SMatthias Ringwald     // lookup att_server instance
93868591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
93968591e36SMatthias Ringwald     if (!hci_connection) return;
94068591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
941bf78aac6SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
942bf78aac6SMatthias 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);
9436a540790SMatthias Ringwald 
944bf78aac6SMatthias Ringwald     // check if bonded
945bf78aac6SMatthias Ringwald     if (le_device_index < 0) return;
9466a540790SMatthias Ringwald 
947bf78aac6SMatthias Ringwald     // get btstack_tlv
948bf78aac6SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
949bf78aac6SMatthias Ringwald     void * tlv_context;
950bf78aac6SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
951bf78aac6SMatthias Ringwald     if (!tlv_impl) return;
9526a540790SMatthias Ringwald 
953bf78aac6SMatthias Ringwald     // update ccc tag
954bf78aac6SMatthias Ringwald     int index;
9556a540790SMatthias Ringwald     uint32_t highest_seq_nr = 0;
9566a540790SMatthias Ringwald     uint32_t lowest_seq_nr = 0;
9576a540790SMatthias Ringwald     uint32_t tag_for_lowest_seq_nr = 0;
9586a540790SMatthias Ringwald     uint32_t tag_for_empty = 0;
9596a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
9606afb9acaSMatthias Ringwald     for (index=0; index<NVN_NUM_GATT_SERVER_CCC; index++){
961bf78aac6SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
9626a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
9636a540790SMatthias Ringwald 
9646a540790SMatthias Ringwald         // empty/invalid tag
9656a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)){
9666a540790SMatthias Ringwald             tag_for_empty = tag;
967bf78aac6SMatthias Ringwald             continue;
968bf78aac6SMatthias Ringwald         }
9696a540790SMatthias Ringwald         // update highest seq nr
9706a540790SMatthias Ringwald         if (entry.seq_nr > highest_seq_nr){
9716a540790SMatthias Ringwald             highest_seq_nr = entry.seq_nr;
9726a540790SMatthias Ringwald         }
9736a540790SMatthias Ringwald         // find entry with lowest seq nr
9744ea43905SMatthias Ringwald         if ((tag_for_lowest_seq_nr == 0u) || (entry.seq_nr < lowest_seq_nr)){
9756a540790SMatthias Ringwald             tag_for_lowest_seq_nr = tag;
9766a540790SMatthias Ringwald             lowest_seq_nr = entry.seq_nr;
9776a540790SMatthias Ringwald         }
9786a540790SMatthias Ringwald 
9796a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
9806a540790SMatthias Ringwald         if (entry.att_handle   != att_handle)      continue;
9816a540790SMatthias Ringwald 
9826a540790SMatthias Ringwald         // found matching entry
9839305033eSMatthias Ringwald         if (value != 0){
984bf78aac6SMatthias Ringwald             // update
9856a540790SMatthias Ringwald             if (entry.value == value) {
986760ad805SMatthias Ringwald                 log_info("CCC Index %u: Up-to-date", index);
987760ad805SMatthias Ringwald                 return;
988760ad805SMatthias Ringwald             }
989348fc806SMatthias Ringwald             entry.value = (uint8_t) value;
9904ea43905SMatthias Ringwald             entry.seq_nr = highest_seq_nr + 1u;
991760ad805SMatthias Ringwald             log_info("CCC Index %u: Store", index);
9920fe46bc1SMatthias Ringwald             int result = tlv_impl->store_tag(tlv_context, tag, (const uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
9930fe46bc1SMatthias Ringwald             if (result != 0){
9940fe46bc1SMatthias Ringwald                 log_error("Store tag index %u failed", index);
9950fe46bc1SMatthias Ringwald             }
99601ac2008SMatthias Ringwald         } else {
99701ac2008SMatthias Ringwald             // delete
998760ad805SMatthias Ringwald             log_info("CCC Index %u: Delete", index);
99901ac2008SMatthias Ringwald             tlv_impl->delete_tag(tlv_context, tag);
100001ac2008SMatthias Ringwald         }
1001bf78aac6SMatthias Ringwald         return;
1002bf78aac6SMatthias Ringwald     }
10036a540790SMatthias Ringwald 
10040fe46bc1SMatthias Ringwald     log_info("tag_for_empty %"PRIx32", tag_for_lowest_seq_nr %"PRIx32, tag_for_empty, tag_for_lowest_seq_nr);
10056a540790SMatthias Ringwald 
10064ea43905SMatthias Ringwald     if (value == 0u){
10076a540790SMatthias Ringwald         // done
10086a540790SMatthias Ringwald         return;
10096a540790SMatthias Ringwald     }
10106a540790SMatthias Ringwald 
101138b893aaSMilanka Ringwald     uint32_t tag_to_use = 0u;
101238b893aaSMilanka Ringwald     if (tag_for_empty != 0u){
10136a540790SMatthias Ringwald         tag_to_use = tag_for_empty;
10146a540790SMatthias Ringwald     } else if (tag_for_lowest_seq_nr){
10156a540790SMatthias Ringwald         tag_to_use = tag_for_lowest_seq_nr;
10166a540790SMatthias Ringwald     } else {
10176a540790SMatthias Ringwald         // should not happen
10186a540790SMatthias Ringwald         return;
10196a540790SMatthias Ringwald     }
1020bf78aac6SMatthias Ringwald     // store ccc tag
10214ea43905SMatthias Ringwald     entry.seq_nr       = highest_seq_nr + 1u;
10226a540790SMatthias Ringwald     entry.device_index = le_device_index;
10236a540790SMatthias Ringwald     entry.att_handle   = att_handle;
1024348fc806SMatthias Ringwald     entry.value        = (uint8_t) value;
10250fe46bc1SMatthias Ringwald     int result = tlv_impl->store_tag(tlv_context, tag_to_use, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
10260fe46bc1SMatthias Ringwald     if (result != 0){
10270fe46bc1SMatthias Ringwald         log_error("Store tag index %u failed", index);
10280fe46bc1SMatthias Ringwald     }
1029bf78aac6SMatthias Ringwald }
1030bf78aac6SMatthias Ringwald 
1031fe3d0cbaSMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server){
1032760ad805SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
1033760ad805SMatthias Ringwald     log_info("Clear CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
1034760ad805SMatthias Ringwald     // check if bonded
1035760ad805SMatthias Ringwald     if (le_device_index < 0) return;
1036760ad805SMatthias Ringwald     // get btstack_tlv
1037760ad805SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
1038760ad805SMatthias Ringwald     void * tlv_context;
1039760ad805SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
1040760ad805SMatthias Ringwald     if (!tlv_impl) return;
1041760ad805SMatthias Ringwald     // get all ccc tag
1042760ad805SMatthias Ringwald     int index;
10436a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
10446afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
1045760ad805SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
10466a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
10476a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
10486a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
1049760ad805SMatthias Ringwald         // delete entry
1050760ad805SMatthias Ringwald         log_info("CCC Index %u: Delete", index);
1051760ad805SMatthias Ringwald         tlv_impl->delete_tag(tlv_context, tag);
1052760ad805SMatthias Ringwald     }
1053760ad805SMatthias Ringwald }
1054760ad805SMatthias Ringwald 
10559a1f55eaSMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server, att_connection_t * att_connection){
105601ac2008SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
105701ac2008SMatthias Ringwald     log_info("Restore CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
10581b7acd0dSMatthias Ringwald     // check if bonded
10591b7acd0dSMatthias Ringwald     if (le_device_index < 0) return;
106001ac2008SMatthias Ringwald     // get btstack_tlv
106101ac2008SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
106201ac2008SMatthias Ringwald     void * tlv_context;
106301ac2008SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
106401ac2008SMatthias Ringwald     if (!tlv_impl) return;
106501ac2008SMatthias Ringwald     // get all ccc tag
106601ac2008SMatthias Ringwald     int index;
10676a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
10686afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
106901ac2008SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
10706a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
10716a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
10726a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
107301ac2008SMatthias Ringwald         // simulate write callback
10746a540790SMatthias Ringwald         uint16_t attribute_handle = entry.att_handle;
107501ac2008SMatthias Ringwald         uint8_t  value[2];
10766a540790SMatthias Ringwald         little_endian_store_16(value, 0, entry.value);
107701ac2008SMatthias Ringwald         att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
107801ac2008SMatthias Ringwald         if (!callback) continue;
10796a540790SMatthias Ringwald         log_info("CCC Index %u: Set Attribute handle 0x%04x to value 0x%04x", index, attribute_handle, entry.value );
1080b6df12b6SMatthias Ringwald         (*callback)(att_connection->con_handle, attribute_handle, ATT_TRANSACTION_MODE_NONE, 0, value, sizeof(value));
108101ac2008SMatthias Ringwald     }
108201ac2008SMatthias Ringwald }
108301ac2008SMatthias Ringwald 
1084bf78aac6SMatthias Ringwald // persistent CCC writes
1085bf78aac6SMatthias Ringwald // ---------------------
10863d71c7a4SMatthias Ringwald 
10873d71c7a4SMatthias Ringwald // gatt service management
10883d71c7a4SMatthias Ringwald static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){
10893d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
10903d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
10913d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
10923d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
10933d71c7a4SMatthias Ringwald         if (handler->start_handle > handle) continue;
10943d71c7a4SMatthias Ringwald         if (handler->end_handle   < handle) continue;
10953d71c7a4SMatthias Ringwald         return handler;
10963d71c7a4SMatthias Ringwald     }
10973d71c7a4SMatthias Ringwald     return NULL;
10983d71c7a4SMatthias Ringwald }
10993d71c7a4SMatthias Ringwald static att_read_callback_t att_server_read_callback_for_handle(uint16_t handle){
11003d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
11019305033eSMatthias Ringwald     if (handler != NULL) return handler->read_callback;
11023d71c7a4SMatthias Ringwald     return att_server_client_read_callback;
11033d71c7a4SMatthias Ringwald }
11043d71c7a4SMatthias Ringwald 
11053d71c7a4SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){
11063d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
11079305033eSMatthias Ringwald     if (handler != NULL) return handler->write_callback;
11083d71c7a4SMatthias Ringwald     return att_server_client_write_callback;
11093d71c7a4SMatthias Ringwald }
11103d71c7a4SMatthias Ringwald 
11115d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle){
11125d07396bSMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
11139305033eSMatthias Ringwald     if (handler != NULL) return handler->packet_handler;
11145d07396bSMatthias Ringwald     return att_client_packet_handler;
11155d07396bSMatthias Ringwald }
11165d07396bSMatthias Ringwald 
11173d71c7a4SMatthias Ringwald static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){
11183d71c7a4SMatthias Ringwald     // notify all callbacks
11193d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
11203d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
11213d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
11223d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
11233d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
11243d71c7a4SMatthias Ringwald         (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
11253d71c7a4SMatthias Ringwald     }
11263d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return;
11273d71c7a4SMatthias Ringwald     (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
11283d71c7a4SMatthias Ringwald }
11293d71c7a4SMatthias Ringwald 
11303d71c7a4SMatthias Ringwald // returns first reported error or 0
11313d71c7a4SMatthias Ringwald static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){
11323d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
11333d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
11343d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
11353d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
11363d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
11373d71c7a4SMatthias Ringwald         uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
113838b893aaSMilanka Ringwald         if (error_code != 0u) return error_code;
11393d71c7a4SMatthias Ringwald     }
11403d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return 0;
11413d71c7a4SMatthias Ringwald     return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
11423d71c7a4SMatthias Ringwald }
11433d71c7a4SMatthias Ringwald 
11443d71c7a4SMatthias 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){
11453d71c7a4SMatthias Ringwald     att_read_callback_t callback = att_server_read_callback_for_handle(attribute_handle);
114601ac2008SMatthias Ringwald     if (!callback) return 0;
11473d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, offset, buffer, buffer_size);
11483d71c7a4SMatthias Ringwald }
11493d71c7a4SMatthias Ringwald 
11503d71c7a4SMatthias 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){
11513d71c7a4SMatthias Ringwald     switch (transaction_mode){
11523d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_VALIDATE:
11533d71c7a4SMatthias Ringwald             return att_validate_prepared_write(con_handle);
11543d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_EXECUTE:
11553d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_CANCEL:
11563d71c7a4SMatthias Ringwald             att_notify_write_callbacks(con_handle, transaction_mode);
11573d71c7a4SMatthias Ringwald             return 0;
11583d71c7a4SMatthias Ringwald         default:
11593d71c7a4SMatthias Ringwald             break;
11603d71c7a4SMatthias Ringwald     }
11613d71c7a4SMatthias Ringwald 
1162bf78aac6SMatthias Ringwald     // track CCC writes
11634ea43905SMatthias Ringwald     if (att_is_persistent_ccc(attribute_handle) && (offset == 0u) && (buffer_size == 2u)){
1164bf78aac6SMatthias Ringwald         att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0));
1165bf78aac6SMatthias Ringwald     }
1166bf78aac6SMatthias Ringwald 
116701ac2008SMatthias Ringwald     att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
116801ac2008SMatthias Ringwald     if (!callback) return 0;
11693d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size);
11703d71c7a4SMatthias Ringwald }
11713d71c7a4SMatthias Ringwald 
11723d71c7a4SMatthias Ringwald /**
11733d71c7a4SMatthias Ringwald  * @brief register read/write callbacks for specific handle range
11743d71c7a4SMatthias Ringwald  * @param att_service_handler_t
11753d71c7a4SMatthias Ringwald  */
11763d71c7a4SMatthias Ringwald void att_server_register_service_handler(att_service_handler_t * handler){
1177ec8f5414SMilanka Ringwald     bool att_server_registered = false;
1178ec8f5414SMilanka Ringwald     if (att_service_handler_for_handle(handler->start_handle)){
1179ec8f5414SMilanka Ringwald         att_server_registered = true;
1180ec8f5414SMilanka Ringwald     }
1181ec8f5414SMilanka Ringwald 
1182ec8f5414SMilanka Ringwald     if (att_service_handler_for_handle(handler->end_handle)){
1183ec8f5414SMilanka Ringwald         att_server_registered = true;
1184ec8f5414SMilanka Ringwald     }
1185ec8f5414SMilanka Ringwald 
1186ec8f5414SMilanka Ringwald     if (att_server_registered){
11873d71c7a4SMatthias Ringwald         log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle);
11883d71c7a4SMatthias Ringwald         return;
11893d71c7a4SMatthias Ringwald     }
11903d71c7a4SMatthias Ringwald     btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler);
11913d71c7a4SMatthias Ringwald }
11923d71c7a4SMatthias Ringwald 
11933deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){
11943deb3ec6SMatthias Ringwald 
11953d71c7a4SMatthias Ringwald     // store callbacks
11963d71c7a4SMatthias Ringwald     att_server_client_read_callback  = read_callback;
11973d71c7a4SMatthias Ringwald     att_server_client_write_callback = write_callback;
11983d71c7a4SMatthias Ringwald 
11991a389cdcSMatthias Ringwald     // register for HCI Events
1200d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &att_event_packet_handler;
12011a389cdcSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
12021a389cdcSMatthias Ringwald 
1203406722e4SMatthias Ringwald     // register for SM events
1204d9a7306aSMatthias Ringwald     sm_event_callback_registration.callback = &att_event_packet_handler;
1205406722e4SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
12063deb3ec6SMatthias Ringwald 
12071a389cdcSMatthias Ringwald     // and L2CAP ATT Server PDUs
12083deb3ec6SMatthias Ringwald     att_dispatch_register_server(att_packet_handler);
12093deb3ec6SMatthias Ringwald 
121070dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
121170dca4d4SMatthias Ringwald     // setup l2cap service
1212f2372b44SMatthias Ringwald     l2cap_register_service(&att_event_packet_handler, PSM_ATT, 0xffff, gap_get_security_level());
121370dca4d4SMatthias Ringwald #endif
121470dca4d4SMatthias Ringwald 
12153deb3ec6SMatthias Ringwald     att_set_db(db);
12163d71c7a4SMatthias Ringwald     att_set_read_callback(att_server_read_callback);
12173d71c7a4SMatthias Ringwald     att_set_write_callback(att_server_write_callback);
12183deb3ec6SMatthias Ringwald }
12193deb3ec6SMatthias Ringwald 
12203deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){
12213deb3ec6SMatthias Ringwald     att_client_packet_handler = handler;
12223deb3ec6SMatthias Ringwald }
12233deb3ec6SMatthias Ringwald 
1224cbbb12d9SMatthias Ringwald 
1225cbbb12d9SMatthias Ringwald // to be deprecated
1226c141988cSMatthias Ringwald int  att_server_can_send_packet_now(hci_con_handle_t con_handle){
122768591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
122868591e36SMatthias Ringwald     if (!hci_connection) return 0;
122937a8059dSMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
123037a8059dSMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
123137a8059dSMatthias Ringwald     return att_server_can_send_packet(att_server, att_connection);
12321b96b007SMatthias Ringwald }
12331b96b007SMatthias Ringwald 
12349816429dSMatthias Ringwald uint8_t att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
1235cbbb12d9SMatthias Ringwald     return att_server_request_to_send_notification(callback_registration, con_handle);
12363deb3ec6SMatthias Ringwald }
12373deb3ec6SMatthias Ringwald 
1238cbbb12d9SMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){
1239cbbb12d9SMatthias Ringwald     att_client_waiting_for_can_send_registration.callback = &att_emit_can_send_now_event;
1240cbbb12d9SMatthias Ringwald     att_server_request_to_send_notification(&att_client_waiting_for_can_send_registration, con_handle);
1241cbbb12d9SMatthias Ringwald }
1242cbbb12d9SMatthias Ringwald // end of deprecated
1243cbbb12d9SMatthias Ringwald 
12449816429dSMatthias Ringwald uint8_t att_server_request_to_send_notification(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
124568591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
124668591e36SMatthias Ringwald     if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
124768591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
12483f9b77dbSMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
1249c37df6f6SMatthias Ringwald     bool added = btstack_linked_list_add_tail(&att_server->notification_requests, (btstack_linked_item_t*) callback_registration);
12503f9b77dbSMatthias Ringwald     att_server_request_can_send_now(att_server, att_connection);
1251c37df6f6SMatthias Ringwald     if (added){
1252cbbb12d9SMatthias Ringwald         return ERROR_CODE_SUCCESS;
1253c37df6f6SMatthias Ringwald     } else {
1254c37df6f6SMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1255c37df6f6SMatthias Ringwald     }
1256cbbb12d9SMatthias Ringwald }
1257cbbb12d9SMatthias Ringwald 
12589816429dSMatthias Ringwald uint8_t att_server_request_to_send_indication(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
125968591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
126068591e36SMatthias Ringwald     if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
126168591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
12623f9b77dbSMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
1263c37df6f6SMatthias Ringwald     bool added = btstack_linked_list_add_tail(&att_server->indication_requests, (btstack_linked_item_t*) callback_registration);
12643f9b77dbSMatthias Ringwald     att_server_request_can_send_now(att_server, att_connection);
1265c37df6f6SMatthias Ringwald     if (added){
1266969a5bbaSMatthias Ringwald         return ERROR_CODE_SUCCESS;
1267c37df6f6SMatthias Ringwald     } else {
1268c37df6f6SMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1269c37df6f6SMatthias Ringwald     }
1270bb38f057SMatthias Ringwald }
1271bb38f057SMatthias Ringwald 
12729816429dSMatthias Ringwald uint8_t att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){
127368591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
127468591e36SMatthias Ringwald     if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
127537a8059dSMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
1276388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
127768591e36SMatthias Ringwald 
127837a8059dSMatthias Ringwald     if (!att_server_can_send_packet(att_server, att_connection)) return BTSTACK_ACL_BUFFERS_FULL;
12793deb3ec6SMatthias Ringwald 
12803deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
12813deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
1282b6df12b6SMatthias Ringwald     uint16_t size = att_prepare_handle_value_notification(att_connection, attribute_handle, value, value_len, packet_buffer);
1283d415948cSMatthias Ringwald 
12840f70c52eSMatthias Ringwald     return att_server_send_prepared(att_server, att_connection, NULL, size);
12853deb3ec6SMatthias Ringwald }
12863deb3ec6SMatthias Ringwald 
12879816429dSMatthias Ringwald uint8_t att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){
128868591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
128968591e36SMatthias Ringwald     if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
129068591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
1291388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
129218707219SMatthias Ringwald 
129338b893aaSMilanka Ringwald     if (att_server->value_indication_handle != 0u) return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS;
129437a8059dSMatthias Ringwald     if (!att_server_can_send_packet(att_server, att_connection)) return BTSTACK_ACL_BUFFERS_FULL;
12953deb3ec6SMatthias Ringwald 
12963deb3ec6SMatthias Ringwald     // track indication
129718707219SMatthias Ringwald     att_server->value_indication_handle = attribute_handle;
129818707219SMatthias Ringwald     btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout);
129918707219SMatthias Ringwald     btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS);
130018707219SMatthias Ringwald     btstack_run_loop_add_timer(&att_server->value_indication_timer);
13013deb3ec6SMatthias Ringwald 
13023deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
13033deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
1304b6df12b6SMatthias Ringwald     uint16_t size = att_prepare_handle_value_indication(att_connection, attribute_handle, value, value_len, packet_buffer);
130523079b39SMatthias Ringwald 
13060f70c52eSMatthias Ringwald     return att_server_send_prepared(att_server, att_connection, NULL, size);
13073deb3ec6SMatthias Ringwald }
13088f9132b5SMilanka Ringwald 
13098f9132b5SMilanka Ringwald uint16_t att_server_get_mtu(hci_con_handle_t con_handle){
131068591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
131168591e36SMatthias Ringwald     if (!hci_connection) return 0;
1312388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
1313b6df12b6SMatthias Ringwald     return att_connection->mtu;
13148f9132b5SMilanka Ringwald }
131535d7f2dfSMilanka Ringwald 
131635d7f2dfSMilanka Ringwald void att_server_deinit(void){
131735d7f2dfSMilanka Ringwald     att_server_client_read_callback  = NULL;
131835d7f2dfSMilanka Ringwald     att_server_client_write_callback = NULL;
131935d7f2dfSMilanka Ringwald     att_client_packet_handler = NULL;
132035d7f2dfSMilanka Ringwald     service_handlers = NULL;
132135d7f2dfSMilanka Ringwald }
132220d4358dSMatthias Ringwald 
132320d4358dSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
132420d4358dSMatthias Ringwald static uint16_t att_server_eatt_receive_buffer_size;
132520d4358dSMatthias Ringwald typedef struct {
132620d4358dSMatthias Ringwald     btstack_linked_item_t item;
132720d4358dSMatthias Ringwald     uint16_t l2cap_cid;
1328*be4ca859SMatthias Ringwald     att_server_t     att_server;
1329*be4ca859SMatthias Ringwald     att_connection_t att_connection;
1330735b0222SMatthias Ringwald     uint8_t * receive_buffer;
133120d4358dSMatthias Ringwald } att_server_eatt_bearer_t;
1332*be4ca859SMatthias Ringwald 
13336b236cb0SMatthias Ringwald static btstack_linked_list_t att_server_eatt_bearer_pool;
13346b236cb0SMatthias Ringwald static btstack_linked_list_t att_server_eatt_bearer_active;
133520d4358dSMatthias Ringwald 
1336523bb989SMatthias Ringwald static att_server_eatt_bearer_t * att_server_eatt_bearer_for_cid(uint16_t cid){
1337523bb989SMatthias Ringwald     btstack_linked_list_iterator_t it;
1338523bb989SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &att_server_eatt_bearer_active);
1339523bb989SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
1340523bb989SMatthias Ringwald         att_server_eatt_bearer_t * eatt_bearer = (att_server_eatt_bearer_t *) btstack_linked_list_iterator_next(&it);
1341523bb989SMatthias Ringwald         if (eatt_bearer->l2cap_cid == cid) {
1342523bb989SMatthias Ringwald             return eatt_bearer;
1343523bb989SMatthias Ringwald         }
1344523bb989SMatthias Ringwald     }
1345523bb989SMatthias Ringwald     return NULL;
1346523bb989SMatthias Ringwald }
1347523bb989SMatthias Ringwald 
134820d4358dSMatthias Ringwald static void att_server_eatt_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
134920d4358dSMatthias Ringwald     uint16_t cid;
135020d4358dSMatthias Ringwald     uint8_t status;
135120d4358dSMatthias Ringwald     uint16_t remote_mtu;
135220d4358dSMatthias Ringwald 
135320d4358dSMatthias Ringwald     uint8_t i;
135420d4358dSMatthias Ringwald     uint8_t num_requested_bearers;
135520d4358dSMatthias Ringwald     uint8_t num_accepted_bearers;
135620d4358dSMatthias Ringwald     uint8_t initial_credits = 5;
135720d4358dSMatthias Ringwald     uint8_t * receive_buffers[5];
135820d4358dSMatthias Ringwald     uint16_t cids[5];
135920d4358dSMatthias Ringwald     att_server_eatt_bearer_t * eatt_bearers[5];
1360523bb989SMatthias Ringwald     att_server_eatt_bearer_t * eatt_bearer;
136120d4358dSMatthias Ringwald 
136220d4358dSMatthias Ringwald     switch (packet_type) {
136320d4358dSMatthias Ringwald 
136420d4358dSMatthias Ringwald         case L2CAP_DATA_PACKET:
136520d4358dSMatthias Ringwald             printf("L2CAP data cid 0x%02x len %u: ", channel, size);
136620d4358dSMatthias Ringwald             printf_hexdump(packet, size);
1367735b0222SMatthias Ringwald 
1368735b0222SMatthias Ringwald             // TODO: copy packet into request buffer
1369735b0222SMatthias Ringwald 
137020d4358dSMatthias Ringwald             break;
137120d4358dSMatthias Ringwald 
137220d4358dSMatthias Ringwald         case HCI_EVENT_PACKET:
137320d4358dSMatthias Ringwald             switch (packet[0]) {
137420d4358dSMatthias Ringwald 
137520d4358dSMatthias Ringwald                 case L2CAP_EVENT_CAN_SEND_NOW:
137620d4358dSMatthias Ringwald                     break;
137720d4358dSMatthias Ringwald 
137820d4358dSMatthias Ringwald                 case L2CAP_EVENT_PACKET_SENT:
137920d4358dSMatthias Ringwald                     cid = l2cap_event_packet_sent_get_local_cid(packet);
1380*be4ca859SMatthias Ringwald                     eatt_bearer = att_server_eatt_bearer_for_cid(cid);
1381*be4ca859SMatthias Ringwald                     btstack_assert(eatt_bearers != NULL);
1382*be4ca859SMatthias Ringwald                     // cache request
138320d4358dSMatthias Ringwald                     printf("L2CAP: LE Data Channel Packet sent0x%02x\n", cid);
138420d4358dSMatthias Ringwald                     break;
138520d4358dSMatthias Ringwald 
138620d4358dSMatthias Ringwald                 case L2CAP_EVENT_ECBM_INCOMING_CONNECTION:
138720d4358dSMatthias Ringwald                     cid = l2cap_event_ecbm_incoming_connection_get_local_cid(packet);
138820d4358dSMatthias Ringwald                     num_requested_bearers = l2cap_event_ecbm_incoming_connection_get_num_channels(packet);
138920d4358dSMatthias Ringwald                     for (i = 0; i < num_requested_bearers; i++){
13906b236cb0SMatthias Ringwald                         eatt_bearers[i] = (att_server_eatt_bearer_t *) btstack_linked_list_get_first_item(&att_server_eatt_bearer_pool);
139120d4358dSMatthias Ringwald                         if (eatt_bearers[i] == NULL) {
139220d4358dSMatthias Ringwald                             break;
139320d4358dSMatthias Ringwald                         }
1394*be4ca859SMatthias Ringwald                         eatt_bearers[i]->att_connection.con_handle = l2cap_event_ecbm_incoming_connection_get_handle(packet);
1395735b0222SMatthias Ringwald                         receive_buffers[i] = eatt_bearers[i]->receive_buffer;
13966b236cb0SMatthias Ringwald                         btstack_linked_list_add(&att_server_eatt_bearer_active, (btstack_linked_item_t *) eatt_bearers[i]);
139720d4358dSMatthias Ringwald                     }
139820d4358dSMatthias Ringwald                     num_accepted_bearers = i;
139920d4358dSMatthias Ringwald                     status = l2cap_ecbm_accept_channels(cid, num_accepted_bearers, initial_credits, att_server_eatt_receive_buffer_size, receive_buffers, cids);
140020d4358dSMatthias Ringwald                     btstack_assert(status == ERROR_CODE_SUCCESS);
14016b236cb0SMatthias Ringwald                     for (i=0;i<num_accepted_bearers;i++){
14026b236cb0SMatthias Ringwald                         eatt_bearers[i]->l2cap_cid = cids[i];
14036b236cb0SMatthias Ringwald                     }
140420d4358dSMatthias Ringwald                     log_info("requested %u, accepted %u", num_requested_bearers, num_accepted_bearers);
140520d4358dSMatthias Ringwald                     break;
140620d4358dSMatthias Ringwald 
140720d4358dSMatthias Ringwald                 case L2CAP_EVENT_ECBM_CHANNEL_OPENED:
140820d4358dSMatthias Ringwald                     cid = l2cap_event_ecbm_channel_opened_get_local_cid(packet);
140920d4358dSMatthias Ringwald                     status = l2cap_event_channel_opened_get_status(packet);
141020d4358dSMatthias Ringwald                     remote_mtu = l2cap_event_channel_opened_get_remote_mtu(packet);
1411*be4ca859SMatthias Ringwald                     eatt_bearer = att_server_eatt_bearer_for_cid(cid);
1412*be4ca859SMatthias Ringwald                     btstack_assert(eatt_bearer != NULL);
1413*be4ca859SMatthias Ringwald                     eatt_bearer->att_connection.max_mtu = remote_mtu;
141420d4358dSMatthias Ringwald                     log_info("L2CAP_EVENT_ECBM_CHANNEL_OPENED - cid 0x%04x mtu %u, status 0x%02x", cid, remote_mtu, status);
141520d4358dSMatthias Ringwald                     break;
141620d4358dSMatthias Ringwald 
141720d4358dSMatthias Ringwald                 case L2CAP_EVENT_ECBM_RECONFIGURED:
141820d4358dSMatthias Ringwald                     break;
141920d4358dSMatthias Ringwald 
1420523bb989SMatthias Ringwald                 case L2CAP_EVENT_CHANNEL_CLOSED:
1421523bb989SMatthias Ringwald                     eatt_bearer = att_server_eatt_bearer_for_cid(l2cap_event_channel_closed_get_local_cid(packet));
1422523bb989SMatthias Ringwald                     btstack_assert(eatt_bearers != NULL);
1423523bb989SMatthias Ringwald 
1424523bb989SMatthias Ringwald                     // TODO: finalize - abort queued writes
1425523bb989SMatthias Ringwald 
1426523bb989SMatthias Ringwald                     btstack_linked_list_remove(&att_server_eatt_bearer_active, (btstack_linked_item_t  *) eatt_bearers);
1427523bb989SMatthias Ringwald                     btstack_linked_list_add(&att_server_eatt_bearer_pool, (btstack_linked_item_t  *) eatt_bearer);
1428523bb989SMatthias Ringwald                     break;
142920d4358dSMatthias Ringwald                 default:
143020d4358dSMatthias Ringwald                     break;
143120d4358dSMatthias Ringwald             }
143220d4358dSMatthias Ringwald     }
143320d4358dSMatthias Ringwald }
143420d4358dSMatthias Ringwald 
143520d4358dSMatthias Ringwald // create eatt bearers
143620d4358dSMatthias Ringwald uint8_t att_server_eatt_init(uint8_t num_eatt_bearers, uint8_t * storage_buffer, uint16_t storage_size){
143720d4358dSMatthias Ringwald     uint16_t size_for_structs = num_eatt_bearers * sizeof(att_server_eatt_bearer_t);
143820d4358dSMatthias Ringwald     if (storage_size < size_for_structs) {
143920d4358dSMatthias Ringwald         return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED;
144020d4358dSMatthias Ringwald     }
144120d4358dSMatthias Ringwald 
144220d4358dSMatthias Ringwald     // TODO: The minimum ATT_MTU for an Enhanced ATT bearer is 64 octets.
144320d4358dSMatthias Ringwald 
144420d4358dSMatthias Ringwald     memset(storage_buffer, 0, storage_size);
1445*be4ca859SMatthias Ringwald     att_server_eatt_receive_buffer_size = ((storage_size - size_for_structs) / num_eatt_bearers);
1446735b0222SMatthias Ringwald     uint8_t * bearer_buffer = &storage_buffer[size_for_structs];
144720d4358dSMatthias Ringwald     uint8_t i;
144820d4358dSMatthias Ringwald     att_server_eatt_bearer_t * eatt_bearer = (att_server_eatt_bearer_t *) storage_buffer;
1449*be4ca859SMatthias Ringwald     log_info("%u EATT bearers with receive buffer size %u",
1450*be4ca859SMatthias Ringwald              num_eatt_bearers, att_server_eatt_receive_buffer_size);
145120d4358dSMatthias Ringwald     for (i=0;i<num_eatt_bearers;i++){
1452*be4ca859SMatthias Ringwald         eatt_bearer->att_connection.con_handle = HCI_CON_HANDLE_INVALID;
1453735b0222SMatthias Ringwald         eatt_bearer->receive_buffer = bearer_buffer;
1454735b0222SMatthias Ringwald         bearer_buffer += att_server_eatt_receive_buffer_size;
14556b236cb0SMatthias Ringwald         btstack_linked_list_add(&att_server_eatt_bearer_pool, (btstack_linked_item_t *) eatt_bearer);
145620d4358dSMatthias Ringwald         eatt_bearer++;
145720d4358dSMatthias Ringwald     }
145820d4358dSMatthias Ringwald     // TODO: define minimum EATT MTU
145920d4358dSMatthias Ringwald     l2cap_ecbm_register_service(att_server_eatt_handler, BLUETOOTH_PSM_EATT, 64, 0);
146020d4358dSMatthias Ringwald 
146120d4358dSMatthias Ringwald     return 0;
146220d4358dSMatthias Ringwald }
146320d4358dSMatthias Ringwald #endif