xref: /btstack/src/ble/att_server.c (revision df33ee14b9f63dc416a03be0b0798dcd5ecec883)
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
233deb3ec6SMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
243deb3ec6SMatthias Ringwald  * RINGWALD 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 
5159c6af15SMatthias Ringwald #include "att_dispatch.h"
5259c6af15SMatthias Ringwald #include "ble/att_db.h"
5359c6af15SMatthias Ringwald #include "ble/att_server.h"
5459c6af15SMatthias Ringwald #include "ble/core.h"
5559c6af15SMatthias Ringwald #include "ble/le_device_db.h"
5659c6af15SMatthias Ringwald #include "ble/sm.h"
5716ece135SMatthias Ringwald #include "btstack_debug.h"
580e2df43fSMatthias Ringwald #include "btstack_event.h"
593deb3ec6SMatthias Ringwald #include "btstack_memory.h"
600e2df43fSMatthias Ringwald #include "btstack_run_loop.h"
6159c6af15SMatthias Ringwald #include "gap.h"
623deb3ec6SMatthias Ringwald #include "hci.h"
633deb3ec6SMatthias Ringwald #include "hci_dump.h"
643deb3ec6SMatthias Ringwald #include "l2cap.h"
65bf78aac6SMatthias Ringwald #include "btstack_tlv.h"
66d1a1f6a4SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
67d1a1f6a4SMatthias Ringwald #include "ble/sm.h"
68d1a1f6a4SMatthias Ringwald #endif
69bf78aac6SMatthias Ringwald 
706afb9acaSMatthias Ringwald #ifndef NVN_NUM_GATT_SERVER_CCC
716afb9acaSMatthias Ringwald #define NVN_NUM_GATT_SERVER_CCC 20
72bf78aac6SMatthias Ringwald #endif
733deb3ec6SMatthias Ringwald 
7418707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server);
7501ac2008SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle);
765d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle);
7748364364SMatthias Ringwald static void att_server_handle_can_send_now(void);
7801ac2008SMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server);
79760ad805SMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server);
8041772f37SMatthias Ringwald static void att_server_handle_att_pdu(att_server_t * att_server, uint8_t * packet, uint16_t size);
813deb3ec6SMatthias Ringwald 
823551936eSMatthias Ringwald typedef enum {
833551936eSMatthias Ringwald     ATT_SERVER_RUN_PHASE_1_REQUESTS,
843551936eSMatthias Ringwald     ATT_SERVER_RUN_PHASE_2_INDICATIONS,
853551936eSMatthias Ringwald     ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS,
863551936eSMatthias Ringwald } att_server_run_phase_t;
873551936eSMatthias Ringwald 
886a540790SMatthias Ringwald //
896a540790SMatthias Ringwald typedef struct {
906a540790SMatthias Ringwald     uint32_t seq_nr;
916a540790SMatthias Ringwald     uint16_t att_handle;
926a540790SMatthias Ringwald     uint8_t  value;
936a540790SMatthias Ringwald     uint8_t  device_index;
946a540790SMatthias Ringwald } persistent_ccc_entry_t;
956a540790SMatthias Ringwald 
9618707219SMatthias Ringwald // global
971a389cdcSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
98406722e4SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration;
993deb3ec6SMatthias Ringwald static btstack_packet_handler_t               att_client_packet_handler = NULL;
1003d71c7a4SMatthias Ringwald static btstack_linked_list_t                  service_handlers;
1015f141511SMatthias Ringwald static btstack_context_callback_registration_t att_client_waiting_for_can_send_registration;
10218707219SMatthias Ringwald 
1033d71c7a4SMatthias Ringwald static att_read_callback_t                    att_server_client_read_callback;
1043d71c7a4SMatthias Ringwald static att_write_callback_t                   att_server_client_write_callback;
1053d71c7a4SMatthias Ringwald 
1066438547aSMatthias Ringwald // round robin
1076438547aSMatthias Ringwald static hci_con_handle_t att_server_last_can_send_now = HCI_CON_HANDLE_INVALID;
108bf78aac6SMatthias Ringwald 
10918707219SMatthias Ringwald static att_server_t * att_server_for_handle(hci_con_handle_t con_handle){
11018707219SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
11118707219SMatthias Ringwald     if (!hci_connection) return NULL;
11218707219SMatthias Ringwald     return &hci_connection->att_server;
11318707219SMatthias Ringwald }
11418707219SMatthias Ringwald 
11570dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
11670dca4d4SMatthias Ringwald static att_server_t * att_server_for_l2cap_cid(uint16_t l2cap_cid){
11770dca4d4SMatthias Ringwald     btstack_linked_list_iterator_t it;
11870dca4d4SMatthias Ringwald     hci_connections_get_iterator(&it);
11970dca4d4SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
12070dca4d4SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
12170dca4d4SMatthias Ringwald         att_server_t * att_server = &connection->att_server;
12270dca4d4SMatthias Ringwald         if (att_server->l2cap_cid == l2cap_cid) return att_server;
12370dca4d4SMatthias Ringwald     }
12470dca4d4SMatthias Ringwald     return NULL;
12570dca4d4SMatthias Ringwald }
12670dca4d4SMatthias Ringwald #endif
12770dca4d4SMatthias Ringwald 
12802b78fc8SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
12918707219SMatthias Ringwald static att_server_t * att_server_for_state(att_server_state_t state){
13018707219SMatthias Ringwald     btstack_linked_list_iterator_t it;
13118707219SMatthias Ringwald     hci_connections_get_iterator(&it);
13218707219SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
13318707219SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
13418707219SMatthias Ringwald         att_server_t * att_server = &connection->att_server;
13518707219SMatthias Ringwald         if (att_server->state == state) return att_server;
13618707219SMatthias Ringwald     }
13718707219SMatthias Ringwald     return NULL;
13818707219SMatthias Ringwald }
13902b78fc8SMatthias Ringwald #endif
140bb38f057SMatthias Ringwald 
141c8c6e9b4SMatthias Ringwald static void att_server_request_can_send_now(att_server_t * att_server){
142c8c6e9b4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
143c8c6e9b4SMatthias Ringwald     if (att_server->l2cap_cid != 0){
144c8c6e9b4SMatthias Ringwald         l2cap_request_can_send_now_event(att_server->l2cap_cid);
145c8c6e9b4SMatthias Ringwald         return;
146c8c6e9b4SMatthias Ringwald     }
147c8c6e9b4SMatthias Ringwald #endif
148c8c6e9b4SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
149c8c6e9b4SMatthias Ringwald }
150c8c6e9b4SMatthias Ringwald 
151c0f26e1cSMatthias Ringwald static int att_server_can_send_packet(att_server_t * att_server){
152c0f26e1cSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
153c0f26e1cSMatthias Ringwald     if (att_server->l2cap_cid != 0){
154c0f26e1cSMatthias Ringwald         return l2cap_can_send_packet_now(att_server->l2cap_cid);
155c0f26e1cSMatthias Ringwald     }
156c0f26e1cSMatthias Ringwald #endif
157c0f26e1cSMatthias Ringwald     return att_dispatch_server_can_send_now(att_server->connection.con_handle);
158c0f26e1cSMatthias Ringwald }
159c0f26e1cSMatthias Ringwald 
1603deb3ec6SMatthias Ringwald static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){
1615d07396bSMatthias Ringwald     btstack_packet_handler_t packet_handler = att_server_packet_handler_for_handle(attribute_handle);
1625d07396bSMatthias Ringwald     if (!packet_handler) return;
1633deb3ec6SMatthias Ringwald 
1643deb3ec6SMatthias Ringwald     uint8_t event[7];
1653deb3ec6SMatthias Ringwald     int pos = 0;
1665611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE;
1673deb3ec6SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
1683deb3ec6SMatthias Ringwald     event[pos++] = status;
169f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, client_handle);
1703deb3ec6SMatthias Ringwald     pos += 2;
171f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, attribute_handle);
172a444112bSMatthias Ringwald     (*packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1733deb3ec6SMatthias Ringwald }
1743deb3ec6SMatthias Ringwald 
1755d07396bSMatthias Ringwald static void att_emit_event_to_all(const uint8_t * event, uint16_t size){
1765d07396bSMatthias Ringwald     // dispatch to app level handler
1775d07396bSMatthias Ringwald     if (att_client_packet_handler){
1785d07396bSMatthias Ringwald         (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size);
1795d07396bSMatthias Ringwald     }
1803deb3ec6SMatthias Ringwald 
1815d07396bSMatthias Ringwald     // dispatch to service handlers
1825d07396bSMatthias Ringwald     btstack_linked_list_iterator_t it;
1835d07396bSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
1845d07396bSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1855d07396bSMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
1865d07396bSMatthias Ringwald         if (!handler->packet_handler) continue;
1875d07396bSMatthias Ringwald         (*handler->packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size);
1885d07396bSMatthias Ringwald     }
1895d07396bSMatthias Ringwald }
1905d07396bSMatthias Ringwald 
1915d07396bSMatthias Ringwald static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){
1923deb3ec6SMatthias Ringwald     uint8_t event[6];
1933deb3ec6SMatthias Ringwald     int pos = 0;
1945611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE;
1953deb3ec6SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
196fc64f94aSMatthias Ringwald     little_endian_store_16(event, pos, con_handle);
1973deb3ec6SMatthias Ringwald     pos += 2;
198f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, mtu);
1995d07396bSMatthias Ringwald 
2005d07396bSMatthias Ringwald     // also dispatch to GATT Clients
201b12646c5SJakob Krantz     att_dispatch_server_mtu_exchanged(con_handle, mtu);
2025d07396bSMatthias Ringwald 
2035d07396bSMatthias Ringwald     // dispatch to app level handler and service handlers
2045d07396bSMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
2053deb3ec6SMatthias Ringwald }
2063deb3ec6SMatthias Ringwald 
2075f141511SMatthias Ringwald static void att_emit_can_send_now_event(void * context){
2085f141511SMatthias Ringwald     UNUSED(context);
2093c97b242SMatthias Ringwald     if (!att_client_packet_handler) return;
2103c97b242SMatthias Ringwald 
2111b96b007SMatthias Ringwald     uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0};
2121b96b007SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
2131b96b007SMatthias Ringwald }
2141b96b007SMatthias Ringwald 
215ff1bec95SMatthias Ringwald static void att_emit_connected_event(att_server_t * att_server){
216ff1bec95SMatthias Ringwald     uint8_t event[11];
217ff1bec95SMatthias Ringwald     int pos = 0;
218ff1bec95SMatthias Ringwald     event[pos++] = ATT_EVENT_CONNECTED;
219ff1bec95SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
220ff1bec95SMatthias Ringwald     event[pos++] = att_server->peer_addr_type;
221ff1bec95SMatthias Ringwald     reverse_bd_addr(att_server->peer_address, &event[pos]);
222ff1bec95SMatthias Ringwald     pos += 6;
223ff1bec95SMatthias Ringwald     little_endian_store_16(event, pos, att_server->connection.con_handle);
224ff1bec95SMatthias Ringwald     pos += 2;
225ff1bec95SMatthias Ringwald 
226ff1bec95SMatthias Ringwald     // dispatch to app level handler and service handlers
227ff1bec95SMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
228ff1bec95SMatthias Ringwald }
229ff1bec95SMatthias Ringwald 
230ff1bec95SMatthias Ringwald 
2316187ad4cSMatthias Ringwald static void att_emit_disconnected_event(uint16_t con_handle){
232ff1bec95SMatthias Ringwald     uint8_t event[4];
233ff1bec95SMatthias Ringwald     int pos = 0;
234ff1bec95SMatthias Ringwald     event[pos++] = ATT_EVENT_DISCONNECTED;
235ff1bec95SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
2366187ad4cSMatthias Ringwald     little_endian_store_16(event, pos, con_handle);
237ff1bec95SMatthias Ringwald     pos += 2;
238ff1bec95SMatthias Ringwald 
239ff1bec95SMatthias Ringwald     // dispatch to app level handler and service handlers
240ff1bec95SMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
241ff1bec95SMatthias Ringwald }
242ff1bec95SMatthias Ringwald 
243ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){
24454178543SMatthias Ringwald     void * context = btstack_run_loop_get_timer_context(ts);
24554178543SMatthias Ringwald     hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
24618707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
24718707219SMatthias Ringwald     if (!att_server) return;
248bce48a26SMatthias Ringwald     // @note: after a transcation timeout, no more requests shall be sent over this ATT Bearer
249bce48a26SMatthias Ringwald     // (that's why we don't reset the value_indication_handle)
25018707219SMatthias Ringwald     uint16_t att_handle = att_server->value_indication_handle;
25118707219SMatthias Ringwald     att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_server->connection.con_handle, att_handle);
2523deb3ec6SMatthias Ringwald }
2533deb3ec6SMatthias Ringwald 
2543deb3ec6SMatthias Ringwald static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2559ec2630cSMatthias Ringwald 
2567dad9ff8SMatthias Ringwald     UNUSED(channel); // ok: there is no channel
2577dad9ff8SMatthias Ringwald     UNUSED(size);    // ok: handling own l2cap events
2583deb3ec6SMatthias Ringwald 
25918707219SMatthias Ringwald     att_server_t * att_server;
26018707219SMatthias Ringwald     hci_con_handle_t con_handle;
26170dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
26270dca4d4SMatthias Ringwald     bd_addr_t address;
26370dca4d4SMatthias Ringwald #endif
26418707219SMatthias Ringwald 
2653deb3ec6SMatthias Ringwald     switch (packet_type) {
2663deb3ec6SMatthias Ringwald 
2673deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
2680e2df43fSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
2693deb3ec6SMatthias Ringwald 
27070dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
27170dca4d4SMatthias Ringwald                 case L2CAP_EVENT_INCOMING_CONNECTION:
27270dca4d4SMatthias Ringwald                     l2cap_event_incoming_connection_get_address(packet, address);
27370dca4d4SMatthias Ringwald                     l2cap_accept_connection(channel);
27436de8547SMatthias Ringwald                     log_info("Accept incoming connection from %s", bd_addr_to_str(address));
27570dca4d4SMatthias Ringwald                     break;
27670dca4d4SMatthias Ringwald                 case L2CAP_EVENT_CHANNEL_OPENED:
277122b723eSMatthias Ringwald                     con_handle = l2cap_event_channel_opened_get_handle(packet);
278122b723eSMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
27970dca4d4SMatthias Ringwald                     if (!att_server) break;
280122b723eSMatthias Ringwald                     // store connection info
281122b723eSMatthias Ringwald                     att_server->peer_addr_type = BD_ADDR_TYPE_CLASSIC;
282122b723eSMatthias Ringwald                     l2cap_event_channel_opened_get_address(packet, att_server->peer_address);
283122b723eSMatthias Ringwald                     att_server->connection.con_handle = con_handle;
28470dca4d4SMatthias Ringwald                     att_server->l2cap_cid = l2cap_event_channel_opened_get_local_cid(packet);
285122b723eSMatthias Ringwald                     // reset connection properties
286122b723eSMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
287122b723eSMatthias Ringwald                     att_server->connection.mtu = l2cap_event_channel_opened_get_remote_mtu(packet);
288122b723eSMatthias Ringwald                     att_server->connection.max_mtu = l2cap_max_mtu();
289122b723eSMatthias Ringwald                     if (att_server->connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){
290122b723eSMatthias Ringwald                         att_server->connection.max_mtu = ATT_REQUEST_BUFFER_SIZE;
291122b723eSMatthias Ringwald                     }
292*df33ee14SMatthias Ringwald 
293*df33ee14SMatthias Ringwald                     log_info("Connection opened %s, l2cap cid %04x, mtu %u", bd_addr_to_str(address), att_server->l2cap_cid, att_server->connection.mtu);
294*df33ee14SMatthias Ringwald 
295914988a1SMatthias Ringwald                     // update security params
296914988a1SMatthias Ringwald                     att_server->connection.encryption_key_size = gap_encryption_key_size(con_handle);
297914988a1SMatthias Ringwald                     att_server->connection.authenticated = gap_authenticated(con_handle);
298914988a1SMatthias Ringwald                     att_server->connection.secure_connection = gap_secure_connection(con_handle);
299914988a1SMatthias Ringwald                     log_info("encrypted key size %u, authenticated %u, secure connection %u",
300914988a1SMatthias Ringwald                         att_server->connection.encryption_key_size, att_server->connection.authenticated, att_server->connection.secure_connection);
301*df33ee14SMatthias Ringwald 
302*df33ee14SMatthias Ringwald                     // notify connection opened
303*df33ee14SMatthias Ringwald                     att_emit_connected_event(att_server);
304*df33ee14SMatthias Ringwald 
305914988a1SMatthias Ringwald                     // restore persisten ccc if encrypted
306914988a1SMatthias Ringwald                     if ( gap_security_level(con_handle) >= LEVEL_2){
307914988a1SMatthias Ringwald                         att_server_persistent_ccc_restore(att_server);
308914988a1SMatthias Ringwald                     }
309122b723eSMatthias Ringwald                     // TODO: what to do about le device db?
310122b723eSMatthias Ringwald                     att_server->pairing_active = 0;
31170dca4d4SMatthias Ringwald                     break;
31248364364SMatthias Ringwald                 case L2CAP_EVENT_CAN_SEND_NOW:
31348364364SMatthias Ringwald                     att_server_handle_can_send_now();
31448364364SMatthias Ringwald                     break;
31548364364SMatthias Ringwald 
31670dca4d4SMatthias Ringwald #endif
3173deb3ec6SMatthias Ringwald                 case HCI_EVENT_LE_META:
3183deb3ec6SMatthias Ringwald                     switch (packet[2]) {
3193deb3ec6SMatthias Ringwald                         case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
32018707219SMatthias Ringwald                             con_handle = little_endian_read_16(packet, 4);
32118707219SMatthias Ringwald                             att_server = att_server_for_handle(con_handle);
32218707219SMatthias Ringwald                             if (!att_server) break;
3233deb3ec6SMatthias Ringwald                         	// store connection info
32418707219SMatthias Ringwald                         	att_server->peer_addr_type = packet[7];
32518707219SMatthias Ringwald                             reverse_bd_addr(&packet[8], att_server->peer_address);
32618707219SMatthias Ringwald                             att_server->connection.con_handle = con_handle;
3273deb3ec6SMatthias Ringwald                             // reset connection properties
32818707219SMatthias Ringwald                             att_server->state = ATT_SERVER_IDLE;
32918707219SMatthias Ringwald                             att_server->connection.mtu = ATT_DEFAULT_MTU;
33018707219SMatthias Ringwald                             att_server->connection.max_mtu = l2cap_max_le_mtu();
33118707219SMatthias Ringwald                             if (att_server->connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){
33218707219SMatthias Ringwald                                 att_server->connection.max_mtu = ATT_REQUEST_BUFFER_SIZE;
33399c58ad0SMatthias Ringwald                             }
33418707219SMatthias Ringwald                             att_server->connection.encryption_key_size = 0;
33518707219SMatthias Ringwald                             att_server->connection.authenticated = 0;
33618707219SMatthias Ringwald 		                	att_server->connection.authorized = 0;
337b2c1e52cSMatthias Ringwald                             // workaround: identity resolving can already be complete, at least store result
338b2c1e52cSMatthias Ringwald                             att_server->ir_le_device_db_index = sm_le_device_index(con_handle);
339ed053a35SMatthias Ringwald                             att_server->ir_lookup_active = 0;
340760ad805SMatthias Ringwald                             att_server->pairing_active = 0;
341ff1bec95SMatthias Ringwald                             // notify all - old
3425d07396bSMatthias Ringwald                             att_emit_event_to_all(packet, size);
343ff1bec95SMatthias Ringwald                             // notify all - new
344ff1bec95SMatthias Ringwald                             att_emit_connected_event(att_server);
3453deb3ec6SMatthias Ringwald                             break;
3463deb3ec6SMatthias Ringwald 
3473deb3ec6SMatthias Ringwald                         default:
3483deb3ec6SMatthias Ringwald                             break;
3493deb3ec6SMatthias Ringwald                     }
3503deb3ec6SMatthias Ringwald                     break;
3513deb3ec6SMatthias Ringwald 
3523deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_CHANGE:
3533deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE:
3543deb3ec6SMatthias Ringwald                 	// check handle
35518707219SMatthias Ringwald                     con_handle = little_endian_read_16(packet, 3);
35618707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
35718707219SMatthias Ringwald                     if (!att_server) break;
358914988a1SMatthias Ringwald                     if (gap_get_connection_type(con_handle) != GAP_CONNECTION_LE) break;
359914988a1SMatthias Ringwald                     // update security params
3609c6e867eSMatthias Ringwald                     att_server->connection.encryption_key_size = gap_encryption_key_size(con_handle);
3619c6e867eSMatthias Ringwald                     att_server->connection.authenticated = gap_authenticated(con_handle);
36213eb7322SMatthias Ringwald                     att_server->connection.secure_connection = gap_secure_connection(con_handle);
363914988a1SMatthias Ringwald                     log_info("encrypted key size %u, authenticated %u, secure connection %u",
36413eb7322SMatthias Ringwald                         att_server->connection.encryption_key_size, att_server->connection.authenticated, att_server->connection.secure_connection);
365914988a1SMatthias Ringwald                     if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){
3664097998aSMatthias Ringwald                         // restore CCC values when encrypted for LE Connections
36701ac2008SMatthias Ringwald                         if (hci_event_encryption_change_get_encryption_enabled(packet)){
36801ac2008SMatthias Ringwald                             att_server_persistent_ccc_restore(att_server);
36901ac2008SMatthias Ringwald                         }
37001ac2008SMatthias Ringwald                     }
3710234fbbcSMatthias Ringwald                     att_run_for_context(att_server);
3723deb3ec6SMatthias Ringwald                     break;
3733deb3ec6SMatthias Ringwald 
3743deb3ec6SMatthias Ringwald                 case HCI_EVENT_DISCONNECTION_COMPLETE:
37570a390c7SMatthias Ringwald                     // check handle
37618707219SMatthias Ringwald                     con_handle = hci_event_disconnection_complete_get_connection_handle(packet);
37718707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
37818707219SMatthias Ringwald                     if (!att_server) break;
37918707219SMatthias Ringwald                     att_clear_transaction_queue(&att_server->connection);
38018707219SMatthias Ringwald                     att_server->connection.con_handle = 0;
381760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
38218707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
383bce48a26SMatthias Ringwald                     if (att_server->value_indication_handle){
38427328ecbSMatthias Ringwald                         btstack_run_loop_remove_timer(&att_server->value_indication_timer);
385bce48a26SMatthias Ringwald                         uint16_t att_handle = att_server->value_indication_handle;
386bce48a26SMatthias Ringwald                         att_server->value_indication_handle = 0; // reset error state
387bce48a26SMatthias Ringwald                         att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_DISCONNECT, att_server->connection.con_handle, att_handle);
388bce48a26SMatthias Ringwald                     }
389ff1bec95SMatthias Ringwald                     // notify all - new
3906187ad4cSMatthias Ringwald                     att_emit_disconnected_event(con_handle);
391ff1bec95SMatthias Ringwald                     // notify all - old
3925d07396bSMatthias Ringwald                     att_emit_event_to_all(packet, size);
3933deb3ec6SMatthias Ringwald                     break;
3943deb3ec6SMatthias Ringwald 
395760ad805SMatthias Ringwald                 // Identity Resolving
3965611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_STARTED:
39718707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_started_get_handle(packet);
39818707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
39918707219SMatthias Ringwald                     if (!att_server) break;
4005611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED");
40118707219SMatthias Ringwald                     att_server->ir_lookup_active = 1;
4023deb3ec6SMatthias Ringwald                     break;
4035611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
404760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
405760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
406760ad805SMatthias Ringwald                     if (!att_server) return;
4071b7acd0dSMatthias Ringwald                     att_server->ir_lookup_active = 0;
4081b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index(packet);
4091b7acd0dSMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED");
4101b7acd0dSMatthias Ringwald                     att_run_for_context(att_server);
4113deb3ec6SMatthias Ringwald                     break;
4125611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_FAILED:
41318707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_failed_get_handle(packet);
41418707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
41518707219SMatthias Ringwald                     if (!att_server) break;
4165611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED");
41718707219SMatthias Ringwald                     att_server->ir_lookup_active = 0;
41818707219SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
41918707219SMatthias Ringwald                     att_run_for_context(att_server);
4203deb3ec6SMatthias Ringwald                     break;
421760ad805SMatthias Ringwald 
422760ad805SMatthias Ringwald                 // Pairing started - delete stored CCC values
423760ad805SMatthias Ringwald                 // - assumes pairing indicates either new device or re-pairing, in both cases there should be no stored CCC values
424760ad805SMatthias Ringwald                 // - assumes that all events have the con handle as the first field
425760ad805SMatthias Ringwald                 case SM_EVENT_JUST_WORKS_REQUEST:
426760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
427760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_INPUT_NUMBER:
428760ad805SMatthias Ringwald                 case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
429760ad805SMatthias Ringwald                     con_handle = sm_event_just_works_request_get_handle(packet);
430760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
431760ad805SMatthias Ringwald                     if (!att_server) break;
432760ad805SMatthias Ringwald                     att_server->pairing_active = 1;
433760ad805SMatthias Ringwald                     log_info("SM Pairing started");
434760ad805SMatthias Ringwald                     if (att_server->ir_le_device_db_index < 0) break;
435760ad805SMatthias Ringwald                     att_server_persistent_ccc_clear(att_server);
436760ad805SMatthias Ringwald                     // index not valid anymore
437760ad805SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
438760ad805SMatthias Ringwald                     break;
439760ad805SMatthias Ringwald 
4401b7acd0dSMatthias Ringwald                 // Bonding completed
441760ad805SMatthias Ringwald                 case SM_EVENT_IDENTITY_CREATED:
442760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
443760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
444760ad805SMatthias Ringwald                     if (!att_server) return;
445760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
4461b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_created_get_index(packet);
4471b7acd0dSMatthias Ringwald                     att_run_for_context(att_server);
4481b7acd0dSMatthias Ringwald                     break;
4491b7acd0dSMatthias Ringwald 
4501b7acd0dSMatthias Ringwald                 // Pairing complete (with/without bonding=storing of pairing information)
4511b7acd0dSMatthias Ringwald                 case SM_EVENT_PAIRING_COMPLETE:
4521b7acd0dSMatthias Ringwald                     con_handle = sm_event_pairing_complete_get_handle(packet);
4531b7acd0dSMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
4541b7acd0dSMatthias Ringwald                     if (!att_server) return;
4551b7acd0dSMatthias Ringwald                     att_server->pairing_active = 0;
4561b7acd0dSMatthias Ringwald                     att_run_for_context(att_server);
457760ad805SMatthias Ringwald                     break;
458760ad805SMatthias Ringwald 
459760ad805SMatthias Ringwald                 // Authorization
4605611a760SMatthias Ringwald                 case SM_EVENT_AUTHORIZATION_RESULT: {
46118707219SMatthias Ringwald                     con_handle = sm_event_authorization_result_get_handle(packet);
46218707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
46318707219SMatthias Ringwald                     if (!att_server) break;
46418707219SMatthias Ringwald                     att_server->connection.authorized = sm_event_authorization_result_get_authorization_result(packet);
465c8c6e9b4SMatthias Ringwald                     att_server_request_can_send_now(att_server);
4663deb3ec6SMatthias Ringwald                 	break;
4673deb3ec6SMatthias Ringwald                 }
4683deb3ec6SMatthias Ringwald                 default:
4693deb3ec6SMatthias Ringwald                     break;
4703deb3ec6SMatthias Ringwald             }
47165b44ffdSMatthias Ringwald             break;
47270dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
47370dca4d4SMatthias Ringwald         case L2CAP_DATA_PACKET:
47470dca4d4SMatthias Ringwald             att_server = att_server_for_l2cap_cid(channel);
47570dca4d4SMatthias Ringwald             if (!att_server) break;
47641772f37SMatthias Ringwald 
47741772f37SMatthias Ringwald             att_server_handle_att_pdu(att_server, packet, size);
47870dca4d4SMatthias Ringwald             break;
47970dca4d4SMatthias Ringwald #endif
48041772f37SMatthias Ringwald 
48165b44ffdSMatthias Ringwald         default:
48265b44ffdSMatthias Ringwald             break;
4833deb3ec6SMatthias Ringwald     }
4843deb3ec6SMatthias Ringwald }
4853deb3ec6SMatthias Ringwald 
4867a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
4873deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
4883deb3ec6SMatthias Ringwald 
48918707219SMatthias Ringwald     att_server_t * att_server = att_server_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION);
49018707219SMatthias Ringwald     if (!att_server) return;
4913deb3ec6SMatthias Ringwald 
4923deb3ec6SMatthias Ringwald     uint8_t hash_flipped[8];
4939c80e4ccSMatthias Ringwald     reverse_64(hash, hash_flipped);
49418707219SMatthias Ringwald     if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8)){
4953deb3ec6SMatthias Ringwald         log_info("ATT Signed Write, invalid signature");
49618707219SMatthias Ringwald         att_server->state = ATT_SERVER_IDLE;
4973deb3ec6SMatthias Ringwald         return;
4983deb3ec6SMatthias Ringwald     }
4993deb3ec6SMatthias Ringwald     log_info("ATT Signed Write, valid signature");
5003deb3ec6SMatthias Ringwald 
5013deb3ec6SMatthias Ringwald     // update sequence number
50218707219SMatthias Ringwald     uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
50318707219SMatthias Ringwald     le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1);
50418707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
505c8c6e9b4SMatthias Ringwald     att_server_request_can_send_now(att_server);
5063deb3ec6SMatthias Ringwald }
5077a766ebfSMatthias Ringwald #endif
50888a48dd8SMatthias Ringwald 
50918707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED
510b06f2579SMatthias Ringwald // pre: can send now
511b06f2579SMatthias Ringwald // returns: 1 if packet was sent
51218707219SMatthias Ringwald static int att_server_process_validated_request(att_server_t * att_server){
513b06f2579SMatthias Ringwald 
514b06f2579SMatthias Ringwald     l2cap_reserve_packet_buffer();
515b06f2579SMatthias Ringwald     uint8_t * att_response_buffer = l2cap_get_outgoing_buffer();
51618707219SMatthias Ringwald     uint16_t  att_response_size   = att_handle_request(&att_server->connection, att_server->request_buffer, att_server->request_size, att_response_buffer);
517b06f2579SMatthias Ringwald 
518beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
51956c3a347SMatthias Ringwald     if (att_response_size == ATT_READ_RESPONSE_PENDING || att_response_size == ATT_INTERNAL_WRITE_RESPONSE_PENDING){
520e404a688SMatthias Ringwald         // update state
521beb20288SMatthias Ringwald         att_server->state = ATT_SERVER_RESPONSE_PENDING;
522e404a688SMatthias Ringwald 
52356c3a347SMatthias Ringwald         // callback with handle ATT_READ_RESPONSE_PENDING for reads
52456c3a347SMatthias Ringwald         if (att_response_size == ATT_READ_RESPONSE_PENDING){
525e404a688SMatthias Ringwald             att_server_client_read_callback(att_server->connection.con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0);
52656c3a347SMatthias Ringwald         }
527e8e7403eSMatthias Ringwald 
528e8e7403eSMatthias Ringwald         // free reserved buffer
529e8e7403eSMatthias Ringwald         l2cap_release_packet_buffer();
530e8e7403eSMatthias Ringwald         return 0;
531e404a688SMatthias Ringwald     }
532e404a688SMatthias Ringwald #endif
533e404a688SMatthias Ringwald 
534b06f2579SMatthias Ringwald     // intercept "insufficient authorization" for authenticated connections to allow for user authorization
535b06f2579SMatthias Ringwald     if ((att_response_size     >= 4)
536b06f2579SMatthias Ringwald     && (att_response_buffer[0] == ATT_ERROR_RESPONSE)
537b06f2579SMatthias Ringwald     && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION)
53818707219SMatthias Ringwald     && (att_server->connection.authenticated)){
539b06f2579SMatthias Ringwald 
5409c6e867eSMatthias Ringwald         switch (gap_authorization_state(att_server->connection.con_handle)){
541b06f2579SMatthias Ringwald             case AUTHORIZATION_UNKNOWN:
542b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
54318707219SMatthias Ringwald                 sm_request_pairing(att_server->connection.con_handle);
544b06f2579SMatthias Ringwald                 return 0;
545b06f2579SMatthias Ringwald             case AUTHORIZATION_PENDING:
546b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
547b06f2579SMatthias Ringwald                 return 0;
548b06f2579SMatthias Ringwald             default:
549b06f2579SMatthias Ringwald                 break;
550b06f2579SMatthias Ringwald         }
551b06f2579SMatthias Ringwald     }
552b06f2579SMatthias Ringwald 
55318707219SMatthias Ringwald     att_server->state = ATT_SERVER_IDLE;
554b06f2579SMatthias Ringwald     if (att_response_size == 0) {
555b06f2579SMatthias Ringwald         l2cap_release_packet_buffer();
556b06f2579SMatthias Ringwald         return 0;
557b06f2579SMatthias Ringwald     }
558b06f2579SMatthias Ringwald 
55948364364SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
56048364364SMatthias Ringwald     if (att_server->l2cap_cid != 0){
56148364364SMatthias Ringwald         l2cap_send_prepared(att_server->l2cap_cid, att_response_size);
56248364364SMatthias Ringwald     } else
56348364364SMatthias Ringwald #endif
56448364364SMatthias Ringwald     {
56518707219SMatthias Ringwald         l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size);
56648364364SMatthias Ringwald     }
567b06f2579SMatthias Ringwald 
568b06f2579SMatthias Ringwald     // notify client about MTU exchange result
569b06f2579SMatthias Ringwald     if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){
57018707219SMatthias Ringwald         att_emit_mtu_event(att_server->connection.con_handle, att_server->connection.mtu);
571b06f2579SMatthias Ringwald     }
572b06f2579SMatthias Ringwald     return 1;
573b06f2579SMatthias Ringwald }
574b06f2579SMatthias Ringwald 
575beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
576beb20288SMatthias Ringwald int att_server_response_ready(hci_con_handle_t con_handle){
577e404a688SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
578e404a688SMatthias Ringwald     if (!att_server)                                        return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
579beb20288SMatthias Ringwald     if (att_server->state != ATT_SERVER_RESPONSE_PENDING)   return ERROR_CODE_COMMAND_DISALLOWED;
580e404a688SMatthias Ringwald 
581e404a688SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
582c8c6e9b4SMatthias Ringwald     att_server_request_can_send_now(att_server);
583e404a688SMatthias Ringwald     return ERROR_CODE_SUCCESS;
584e404a688SMatthias Ringwald }
585e404a688SMatthias Ringwald #endif
586e404a688SMatthias Ringwald 
58718707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server){
58818707219SMatthias Ringwald     switch (att_server->state){
5893deb3ec6SMatthias Ringwald         case ATT_SERVER_REQUEST_RECEIVED:
590760ad805SMatthias Ringwald 
59148364364SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
59248364364SMatthias Ringwald             if (att_server->l2cap_cid != 0){
59348364364SMatthias Ringwald                 // ok
59448364364SMatthias Ringwald             } else
59548364364SMatthias Ringwald #endif
59648364364SMatthias Ringwald             {
5970234fbbcSMatthias Ringwald                 // wait until re-encryption as central is complete
5980234fbbcSMatthias Ringwald                 if (gap_reconnect_security_setup_active(att_server->connection.con_handle)) break;
59948364364SMatthias Ringwald             }
6000234fbbcSMatthias Ringwald 
601760ad805SMatthias Ringwald             // wait until pairing is complete
602760ad805SMatthias Ringwald             if (att_server->pairing_active) break;
603760ad805SMatthias Ringwald 
6047a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
60518707219SMatthias Ringwald             if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){
6063deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write!");
6073deb3ec6SMatthias Ringwald                 if (!sm_cmac_ready()) {
6083deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, sm_cmac engine not ready. Abort");
60918707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
6103deb3ec6SMatthias Ringwald                     return;
6113deb3ec6SMatthias Ringwald                 }
61218707219SMatthias Ringwald                 if (att_server->request_size < (3 + 12)) {
6133deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, request to short. Abort.");
61418707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
6153deb3ec6SMatthias Ringwald                     return;
6163deb3ec6SMatthias Ringwald                 }
61718707219SMatthias Ringwald                 if (att_server->ir_lookup_active){
6183deb3ec6SMatthias Ringwald                     return;
6193deb3ec6SMatthias Ringwald                 }
62018707219SMatthias Ringwald                 if (att_server->ir_le_device_db_index < 0){
6213deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, CSRK not available");
62218707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
6233deb3ec6SMatthias Ringwald                     return;
6243deb3ec6SMatthias Ringwald                 }
6253deb3ec6SMatthias Ringwald 
6263deb3ec6SMatthias Ringwald                 // check counter
62718707219SMatthias Ringwald                 uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
62818707219SMatthias Ringwald                 uint32_t counter_db     = le_device_db_remote_counter_get(att_server->ir_le_device_db_index);
6293deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet);
6303deb3ec6SMatthias Ringwald                 if (counter_packet < counter_db){
6313deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, db reports higher counter, abort");
63218707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
6333deb3ec6SMatthias Ringwald                     return;
6343deb3ec6SMatthias Ringwald                 }
6353deb3ec6SMatthias Ringwald 
6363deb3ec6SMatthias Ringwald                 // signature is { sequence counter, secure hash }
6373deb3ec6SMatthias Ringwald                 sm_key_t csrk;
63818707219SMatthias Ringwald                 le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk);
63918707219SMatthias Ringwald                 att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION;
6403deb3ec6SMatthias Ringwald                 log_info("Orig Signature: ");
64118707219SMatthias Ringwald                 log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8);
64218707219SMatthias Ringwald                 uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1);
64318707219SMatthias 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);
6443deb3ec6SMatthias Ringwald                 return;
6453deb3ec6SMatthias Ringwald             }
6467a766ebfSMatthias Ringwald #endif
647b06f2579SMatthias Ringwald             // move on
64818707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
649ed053a35SMatthias Ringwald             att_server_request_can_send_now(att_server);
650b06f2579SMatthias Ringwald             break;
65188a48dd8SMatthias Ringwald 
6523deb3ec6SMatthias Ringwald         default:
6533deb3ec6SMatthias Ringwald             break;
6543deb3ec6SMatthias Ringwald     }
6553deb3ec6SMatthias Ringwald }
6563deb3ec6SMatthias Ringwald 
65790f03c80SMatthias Ringwald static int att_server_data_ready_for_phase(att_server_t * att_server,  att_server_run_phase_t phase){
65890f03c80SMatthias Ringwald     switch (phase){
65990f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
66090f03c80SMatthias Ringwald             return att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
66190f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
66290f03c80SMatthias Ringwald              return (!btstack_linked_list_empty(&att_server->indication_requests) && att_server->value_indication_handle == 0);
66390f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
66490f03c80SMatthias Ringwald             return (!btstack_linked_list_empty(&att_server->notification_requests));
66590f03c80SMatthias Ringwald     }
666fa5e3464SMatthias Ringwald     // avoid warning
667fa5e3464SMatthias Ringwald     return 0;
66890f03c80SMatthias Ringwald }
66990f03c80SMatthias Ringwald 
67090f03c80SMatthias Ringwald static void att_server_trigger_send_for_phase(att_server_t * att_server,  att_server_run_phase_t phase){
67190f03c80SMatthias Ringwald     btstack_context_callback_registration_t * client;
67290f03c80SMatthias Ringwald     switch (phase){
67390f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
67490f03c80SMatthias Ringwald             att_server_process_validated_request(att_server);
67590f03c80SMatthias Ringwald             break;
67690f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
67790f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->indication_requests;
67890f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->indication_requests, (btstack_linked_item_t *) client);
67990f03c80SMatthias Ringwald             client->callback(client->context);
68090f03c80SMatthias Ringwald             break;
68190f03c80SMatthias Ringwald        case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
68290f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->notification_requests;
68390f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->notification_requests, (btstack_linked_item_t *) client);
68490f03c80SMatthias Ringwald             client->callback(client->context);
68590f03c80SMatthias Ringwald             break;
68690f03c80SMatthias Ringwald     }
68790f03c80SMatthias Ringwald }
68890f03c80SMatthias Ringwald 
6897eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){
690b06f2579SMatthias Ringwald 
6916438547aSMatthias Ringwald     hci_con_handle_t last_send_con_handle = HCI_CON_HANDLE_INVALID;
69269d32c56SMatthias Ringwald     att_server_t *   request_att_server   = NULL;
6936438547aSMatthias Ringwald     int can_send_now = 1;
6944395a000SMatthias Ringwald     int phase_index;
6956438547aSMatthias Ringwald 
6964395a000SMatthias Ringwald     for (phase_index = ATT_SERVER_RUN_PHASE_1_REQUESTS; phase_index <= ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS; phase_index++){
6974395a000SMatthias Ringwald         att_server_run_phase_t phase = (att_server_run_phase_t) phase_index;
6986438547aSMatthias Ringwald         hci_con_handle_t skip_connections_until = att_server_last_can_send_now;
699374a288aSMatthias Ringwald         while (1){
7006438547aSMatthias Ringwald             btstack_linked_list_iterator_t it;
70118707219SMatthias Ringwald             hci_connections_get_iterator(&it);
70218707219SMatthias Ringwald             while(btstack_linked_list_iterator_has_next(&it)){
70318707219SMatthias Ringwald                 hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
70418707219SMatthias Ringwald                 att_server_t * att_server = &connection->att_server;
7056438547aSMatthias Ringwald 
70690f03c80SMatthias Ringwald                 int data_ready = att_server_data_ready_for_phase(att_server, phase);
7076438547aSMatthias Ringwald 
7086438547aSMatthias Ringwald                 // log_debug("phase %u, handle 0x%04x, skip until 0x%04x, data ready %u", phase, att_server->connection.con_handle, skip_connections_until, data_ready);
7096438547aSMatthias Ringwald 
7106438547aSMatthias Ringwald                 // skip until last sender found (which is also skipped)
7116438547aSMatthias Ringwald                 if (skip_connections_until != HCI_CON_HANDLE_INVALID){
712b6335864SMatthias Ringwald                     if (data_ready && request_att_server == NULL){
71369d32c56SMatthias Ringwald                         request_att_server = att_server;
7146438547aSMatthias Ringwald                     }
7156438547aSMatthias Ringwald                     if (skip_connections_until == att_server->connection.con_handle){
7166438547aSMatthias Ringwald                         skip_connections_until = HCI_CON_HANDLE_INVALID;
7176438547aSMatthias Ringwald                     }
7186438547aSMatthias Ringwald                     continue;
7196438547aSMatthias Ringwald                 };
7206438547aSMatthias Ringwald 
72190f03c80SMatthias Ringwald                 if (data_ready){
722969a5bbaSMatthias Ringwald                     if (can_send_now){
72390f03c80SMatthias Ringwald                         att_server_trigger_send_for_phase(att_server, phase);
7246438547aSMatthias Ringwald                         last_send_con_handle = att_server->connection.con_handle;
725c0f26e1cSMatthias Ringwald                         can_send_now = att_server_can_send_packet(att_server);
72690f03c80SMatthias Ringwald                         data_ready = att_server_data_ready_for_phase(att_server, phase);
727b6335864SMatthias Ringwald                         if (data_ready && request_att_server == NULL){
72869d32c56SMatthias Ringwald                             request_att_server = att_server;
729cbbb12d9SMatthias Ringwald                         }
730cbbb12d9SMatthias Ringwald                     } else {
73169d32c56SMatthias Ringwald                         request_att_server = att_server;
732f29a88d8SMatthias Ringwald                         break;
733cbbb12d9SMatthias Ringwald                     }
734cbbb12d9SMatthias Ringwald                 }
7353deb3ec6SMatthias Ringwald             }
7363deb3ec6SMatthias Ringwald 
7376438547aSMatthias Ringwald             // stop skipping (handles disconnect by last send connection)
7386438547aSMatthias Ringwald             skip_connections_until = HCI_CON_HANDLE_INVALID;
7396438547aSMatthias Ringwald 
7403551936eSMatthias Ringwald             // Exit loop, if we cannot send
741969a5bbaSMatthias Ringwald             if (!can_send_now) break;
742969a5bbaSMatthias Ringwald 
743969a5bbaSMatthias Ringwald             // Exit loop, if we can send but there are also no further request
744b6335864SMatthias Ringwald             if (request_att_server == NULL) break;
745969a5bbaSMatthias Ringwald 
746969a5bbaSMatthias Ringwald             // Finally, if we still can send and there are requests, just try again
74769d32c56SMatthias Ringwald             request_att_server = NULL;
748969a5bbaSMatthias Ringwald         }
7496438547aSMatthias Ringwald         // update last send con handle for round robin
7506438547aSMatthias Ringwald         if (last_send_con_handle != HCI_CON_HANDLE_INVALID){
7516438547aSMatthias Ringwald             att_server_last_can_send_now = last_send_con_handle;
7526438547aSMatthias Ringwald         }
7533551936eSMatthias Ringwald     }
754969a5bbaSMatthias Ringwald 
755b6335864SMatthias Ringwald     if (request_att_server == NULL) return;
75669d32c56SMatthias Ringwald     att_server_request_can_send_now(request_att_server);
757969a5bbaSMatthias Ringwald }
758969a5bbaSMatthias Ringwald 
75941772f37SMatthias Ringwald static void att_server_handle_att_pdu(att_server_t * att_server, uint8_t * packet, uint16_t size){
76018707219SMatthias Ringwald 
7613deb3ec6SMatthias Ringwald     // handle value indication confirms
76218707219SMatthias Ringwald     if (packet[0] == ATT_HANDLE_VALUE_CONFIRMATION && att_server->value_indication_handle){
76318707219SMatthias Ringwald         btstack_run_loop_remove_timer(&att_server->value_indication_timer);
76418707219SMatthias Ringwald         uint16_t att_handle = att_server->value_indication_handle;
76518707219SMatthias Ringwald         att_server->value_indication_handle = 0;
76618707219SMatthias Ringwald         att_handle_value_indication_notify_client(0, att_server->connection.con_handle, att_handle);
76741772f37SMatthias Ringwald         att_server_request_can_send_now(att_server);
7683deb3ec6SMatthias Ringwald         return;
7693deb3ec6SMatthias Ringwald     }
7703deb3ec6SMatthias Ringwald 
7716428eb5aSMatthias Ringwald     // directly process command
7726428eb5aSMatthias Ringwald     // note: signed write cannot be handled directly as authentication needs to be verified
7736428eb5aSMatthias Ringwald     if (packet[0] == ATT_WRITE_COMMAND){
77418707219SMatthias Ringwald         att_handle_request(&att_server->connection, packet, size, 0);
7756428eb5aSMatthias Ringwald         return;
7766428eb5aSMatthias Ringwald     }
7776428eb5aSMatthias Ringwald 
7783deb3ec6SMatthias Ringwald     // check size
77918707219SMatthias Ringwald     if (size > sizeof(att_server->request_buffer)) {
780e0463bdcSMatthias 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));
7816428eb5aSMatthias Ringwald         return;
7826428eb5aSMatthias Ringwald     }
7833deb3ec6SMatthias Ringwald 
784e0463bdcSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
785e0463bdcSMatthias Ringwald     // abort signed write validation if a new request comes in (but finish previous signed write if possible)
786e0463bdcSMatthias Ringwald     if (att_server->state == ATT_SERVER_W4_SIGNED_WRITE_VALIDATION){
787e0463bdcSMatthias Ringwald         if (packet[0] == ATT_SIGNED_WRITE_COMMAND){
788e0463bdcSMatthias Ringwald             log_info("skip new signed write request as previous is in validation");
789e0463bdcSMatthias Ringwald             return;
790e0463bdcSMatthias Ringwald         } else {
791e0463bdcSMatthias Ringwald             log_info("abort signed write validation to process new request");
792e0463bdcSMatthias Ringwald             att_server->state = ATT_SERVER_IDLE;
793e0463bdcSMatthias Ringwald         }
794e0463bdcSMatthias Ringwald     }
795e0463bdcSMatthias Ringwald #endif
7963deb3ec6SMatthias Ringwald     // last request still in processing?
79718707219SMatthias Ringwald     if (att_server->state != ATT_SERVER_IDLE){
798e0463bdcSMatthias Ringwald         log_info("skip att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state);
7996428eb5aSMatthias Ringwald         return;
8006428eb5aSMatthias Ringwald     }
8013deb3ec6SMatthias Ringwald 
8023deb3ec6SMatthias Ringwald     // store request
80318707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED;
80418707219SMatthias Ringwald     att_server->request_size = size;
80518707219SMatthias Ringwald     memcpy(att_server->request_buffer, packet, size);
8063deb3ec6SMatthias Ringwald 
80718707219SMatthias Ringwald     att_run_for_context(att_server);
80841772f37SMatthias Ringwald }
80941772f37SMatthias Ringwald 
81041772f37SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
81141772f37SMatthias Ringwald     att_server_t * att_server;
81241772f37SMatthias Ringwald 
81341772f37SMatthias Ringwald     switch (packet_type){
81441772f37SMatthias Ringwald         case HCI_EVENT_PACKET:
81541772f37SMatthias Ringwald             switch (packet[0]){
81641772f37SMatthias Ringwald                 case L2CAP_EVENT_CAN_SEND_NOW:
81741772f37SMatthias Ringwald                     att_server_handle_can_send_now();
81841772f37SMatthias Ringwald                     break;
81941772f37SMatthias Ringwald                 case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
82041772f37SMatthias Ringwald                     // GATT client has negotiated the mtu for this connection
82141772f37SMatthias Ringwald                     att_server = att_server_for_handle(handle);
82241772f37SMatthias Ringwald                     if (!att_server) break;
82341772f37SMatthias Ringwald                     att_server->connection.mtu = little_endian_read_16(packet, 4);
82441772f37SMatthias Ringwald                     break;
82541772f37SMatthias Ringwald                 default:
82641772f37SMatthias Ringwald                     break;
82741772f37SMatthias Ringwald             }
82841772f37SMatthias Ringwald             break;
82941772f37SMatthias Ringwald 
83041772f37SMatthias Ringwald         case ATT_DATA_PACKET:
83141772f37SMatthias Ringwald             log_debug("ATT Packet, handle 0x%04x", handle);
83241772f37SMatthias Ringwald             att_server = att_server_for_handle(handle);
83341772f37SMatthias Ringwald             if (!att_server) break;
83441772f37SMatthias Ringwald 
83541772f37SMatthias Ringwald             att_server_handle_att_pdu(att_server, packet, size);
836372d62c4SMatthias Ringwald             break;
837372d62c4SMatthias Ringwald     }
8383deb3ec6SMatthias Ringwald }
8393deb3ec6SMatthias Ringwald 
840bf78aac6SMatthias Ringwald // ---------------------
841bf78aac6SMatthias Ringwald // persistent CCC writes
842bf78aac6SMatthias Ringwald static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){
843bf78aac6SMatthias Ringwald     return 'B' << 24 | 'T' << 16 | 'C' << 8 | index;
844bf78aac6SMatthias Ringwald }
845bf78aac6SMatthias Ringwald 
846bf78aac6SMatthias Ringwald static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){
847bf78aac6SMatthias Ringwald     // lookup att_server instance
848bf78aac6SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
849bf78aac6SMatthias Ringwald     if (!att_server) return;
850bf78aac6SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
851bf78aac6SMatthias 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);
8526a540790SMatthias Ringwald 
853bf78aac6SMatthias Ringwald     // check if bonded
854bf78aac6SMatthias Ringwald     if (le_device_index < 0) return;
8556a540790SMatthias Ringwald 
856bf78aac6SMatthias Ringwald     // get btstack_tlv
857bf78aac6SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
858bf78aac6SMatthias Ringwald     void * tlv_context;
859bf78aac6SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
860bf78aac6SMatthias Ringwald     if (!tlv_impl) return;
8616a540790SMatthias Ringwald 
862bf78aac6SMatthias Ringwald     // update ccc tag
863bf78aac6SMatthias Ringwald     int index;
8646a540790SMatthias Ringwald     uint32_t highest_seq_nr = 0;
8656a540790SMatthias Ringwald     uint32_t lowest_seq_nr = 0;
8666a540790SMatthias Ringwald     uint32_t tag_for_lowest_seq_nr = 0;
8676a540790SMatthias Ringwald     uint32_t tag_for_empty = 0;
8686a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
8696afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
870bf78aac6SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
8716a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
8726a540790SMatthias Ringwald 
8736a540790SMatthias Ringwald         // empty/invalid tag
8746a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)){
8756a540790SMatthias Ringwald             tag_for_empty = tag;
876bf78aac6SMatthias Ringwald             continue;
877bf78aac6SMatthias Ringwald         }
8786a540790SMatthias Ringwald         // update highest seq nr
8796a540790SMatthias Ringwald         if (entry.seq_nr > highest_seq_nr){
8806a540790SMatthias Ringwald             highest_seq_nr = entry.seq_nr;
8816a540790SMatthias Ringwald         }
8826a540790SMatthias Ringwald         // find entry with lowest seq nr
8836a540790SMatthias Ringwald         if ((tag_for_lowest_seq_nr == 0) || (entry.seq_nr < lowest_seq_nr)){
8846a540790SMatthias Ringwald             tag_for_lowest_seq_nr = tag;
8856a540790SMatthias Ringwald             lowest_seq_nr = entry.seq_nr;
8866a540790SMatthias Ringwald         }
8876a540790SMatthias Ringwald 
8886a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
8896a540790SMatthias Ringwald         if (entry.att_handle   != att_handle)      continue;
8906a540790SMatthias Ringwald 
8916a540790SMatthias Ringwald         // found matching entry
89201ac2008SMatthias Ringwald         if (value){
893bf78aac6SMatthias Ringwald             // update
8946a540790SMatthias Ringwald             if (entry.value == value) {
895760ad805SMatthias Ringwald                 log_info("CCC Index %u: Up-to-date", index);
896760ad805SMatthias Ringwald                 return;
897760ad805SMatthias Ringwald             }
8986a540790SMatthias Ringwald             entry.value = value;
8996a540790SMatthias Ringwald             entry.seq_nr = highest_seq_nr + 1;
900760ad805SMatthias Ringwald             log_info("CCC Index %u: Store", index);
9016a540790SMatthias Ringwald             tlv_impl->store_tag(tlv_context, tag, (const uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
90201ac2008SMatthias Ringwald         } else {
90301ac2008SMatthias Ringwald             // delete
904760ad805SMatthias Ringwald             log_info("CCC Index %u: Delete", index);
90501ac2008SMatthias Ringwald             tlv_impl->delete_tag(tlv_context, tag);
90601ac2008SMatthias Ringwald         }
907bf78aac6SMatthias Ringwald         return;
908bf78aac6SMatthias Ringwald     }
9096a540790SMatthias Ringwald 
910faf2b6c3SMatthias Ringwald     log_info("tag_for_empy %"PRIx32", tag_for_lowest_seq_nr %"PRIx32, tag_for_empty, tag_for_lowest_seq_nr);
9116a540790SMatthias Ringwald 
9126a540790SMatthias Ringwald     if (value == 0){
9136a540790SMatthias Ringwald         // done
9146a540790SMatthias Ringwald         return;
9156a540790SMatthias Ringwald     }
9166a540790SMatthias Ringwald 
9176a540790SMatthias Ringwald     uint32_t tag_to_use = 0;
9186a540790SMatthias Ringwald     if (tag_for_empty){
9196a540790SMatthias Ringwald         tag_to_use = tag_for_empty;
9206a540790SMatthias Ringwald     } else if (tag_for_lowest_seq_nr){
9216a540790SMatthias Ringwald         tag_to_use = tag_for_lowest_seq_nr;
9226a540790SMatthias Ringwald     } else {
9236a540790SMatthias Ringwald         // should not happen
9246a540790SMatthias Ringwald         return;
9256a540790SMatthias Ringwald     }
926bf78aac6SMatthias Ringwald     // store ccc tag
9276a540790SMatthias Ringwald     entry.seq_nr       = highest_seq_nr + 1;
9286a540790SMatthias Ringwald     entry.device_index = le_device_index;
9296a540790SMatthias Ringwald     entry.att_handle   = att_handle;
9306a540790SMatthias Ringwald     entry.value        = value;
9316a540790SMatthias Ringwald     tlv_impl->store_tag(tlv_context, tag_to_use, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
932bf78aac6SMatthias Ringwald }
933bf78aac6SMatthias Ringwald 
934760ad805SMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server){
935760ad805SMatthias Ringwald     if (!att_server) return;
936760ad805SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
937760ad805SMatthias Ringwald     log_info("Clear CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
938760ad805SMatthias Ringwald     // check if bonded
939760ad805SMatthias Ringwald     if (le_device_index < 0) return;
940760ad805SMatthias Ringwald     // get btstack_tlv
941760ad805SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
942760ad805SMatthias Ringwald     void * tlv_context;
943760ad805SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
944760ad805SMatthias Ringwald     if (!tlv_impl) return;
945760ad805SMatthias Ringwald     // get all ccc tag
946760ad805SMatthias Ringwald     int index;
9476a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
9486afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
949760ad805SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
9506a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
9516a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
9526a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
953760ad805SMatthias Ringwald         // delete entry
954760ad805SMatthias Ringwald         log_info("CCC Index %u: Delete", index);
955760ad805SMatthias Ringwald         tlv_impl->delete_tag(tlv_context, tag);
956760ad805SMatthias Ringwald     }
957760ad805SMatthias Ringwald }
958760ad805SMatthias Ringwald 
95901ac2008SMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server){
96001ac2008SMatthias Ringwald     if (!att_server) return;
96101ac2008SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
96201ac2008SMatthias Ringwald     log_info("Restore CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
9631b7acd0dSMatthias Ringwald     // check if bonded
9641b7acd0dSMatthias Ringwald     if (le_device_index < 0) return;
96501ac2008SMatthias Ringwald     // get btstack_tlv
96601ac2008SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
96701ac2008SMatthias Ringwald     void * tlv_context;
96801ac2008SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
96901ac2008SMatthias Ringwald     if (!tlv_impl) return;
97001ac2008SMatthias Ringwald     // get all ccc tag
97101ac2008SMatthias Ringwald     int index;
9726a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
9736afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
97401ac2008SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
9756a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
9766a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
9776a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
97801ac2008SMatthias Ringwald         // simulate write callback
9796a540790SMatthias Ringwald         uint16_t attribute_handle = entry.att_handle;
98001ac2008SMatthias Ringwald         uint8_t  value[2];
9816a540790SMatthias Ringwald         little_endian_store_16(value, 0, entry.value);
98201ac2008SMatthias Ringwald         att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
98301ac2008SMatthias Ringwald         if (!callback) continue;
9846a540790SMatthias Ringwald         log_info("CCC Index %u: Set Attribute handle 0x%04x to value 0x%04x", index, attribute_handle, entry.value );
98501ac2008SMatthias Ringwald         (*callback)(att_server->connection.con_handle, attribute_handle, ATT_TRANSACTION_MODE_NONE, 0, value, sizeof(value));
98601ac2008SMatthias Ringwald     }
98701ac2008SMatthias Ringwald }
98801ac2008SMatthias Ringwald 
989bf78aac6SMatthias Ringwald // persistent CCC writes
990bf78aac6SMatthias Ringwald // ---------------------
9913d71c7a4SMatthias Ringwald 
9923d71c7a4SMatthias Ringwald // gatt service management
9933d71c7a4SMatthias Ringwald static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){
9943d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
9953d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
9963d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
9973d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
9983d71c7a4SMatthias Ringwald         if (handler->start_handle > handle) continue;
9993d71c7a4SMatthias Ringwald         if (handler->end_handle   < handle) continue;
10003d71c7a4SMatthias Ringwald         return handler;
10013d71c7a4SMatthias Ringwald     }
10023d71c7a4SMatthias Ringwald     return NULL;
10033d71c7a4SMatthias Ringwald }
10043d71c7a4SMatthias Ringwald static att_read_callback_t att_server_read_callback_for_handle(uint16_t handle){
10053d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
10063d71c7a4SMatthias Ringwald     if (handler) return handler->read_callback;
10073d71c7a4SMatthias Ringwald     return att_server_client_read_callback;
10083d71c7a4SMatthias Ringwald }
10093d71c7a4SMatthias Ringwald 
10103d71c7a4SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){
10113d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
10123d71c7a4SMatthias Ringwald     if (handler) return handler->write_callback;
10133d71c7a4SMatthias Ringwald     return att_server_client_write_callback;
10143d71c7a4SMatthias Ringwald }
10153d71c7a4SMatthias Ringwald 
10165d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle){
10175d07396bSMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
10185d07396bSMatthias Ringwald     if (handler) return handler->packet_handler;
10195d07396bSMatthias Ringwald     return att_client_packet_handler;
10205d07396bSMatthias Ringwald }
10215d07396bSMatthias Ringwald 
10223d71c7a4SMatthias Ringwald static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){
10233d71c7a4SMatthias Ringwald     // notify all callbacks
10243d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
10253d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
10263d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
10273d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
10283d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
10293d71c7a4SMatthias Ringwald         (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
10303d71c7a4SMatthias Ringwald     }
10313d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return;
10323d71c7a4SMatthias Ringwald     (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
10333d71c7a4SMatthias Ringwald }
10343d71c7a4SMatthias Ringwald 
10353d71c7a4SMatthias Ringwald // returns first reported error or 0
10363d71c7a4SMatthias Ringwald static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){
10373d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
10383d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
10393d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
10403d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
10413d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
10423d71c7a4SMatthias Ringwald         uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
10433d71c7a4SMatthias Ringwald         if (error_code) return error_code;
10443d71c7a4SMatthias Ringwald     }
10453d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return 0;
10463d71c7a4SMatthias Ringwald     return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
10473d71c7a4SMatthias Ringwald }
10483d71c7a4SMatthias Ringwald 
10493d71c7a4SMatthias 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){
10503d71c7a4SMatthias Ringwald     att_read_callback_t callback = att_server_read_callback_for_handle(attribute_handle);
105101ac2008SMatthias Ringwald     if (!callback) return 0;
10523d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, offset, buffer, buffer_size);
10533d71c7a4SMatthias Ringwald }
10543d71c7a4SMatthias Ringwald 
10553d71c7a4SMatthias 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){
10563d71c7a4SMatthias Ringwald     switch (transaction_mode){
10573d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_VALIDATE:
10583d71c7a4SMatthias Ringwald             return att_validate_prepared_write(con_handle);
10593d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_EXECUTE:
10603d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_CANCEL:
10613d71c7a4SMatthias Ringwald             att_notify_write_callbacks(con_handle, transaction_mode);
10623d71c7a4SMatthias Ringwald             return 0;
10633d71c7a4SMatthias Ringwald         default:
10643d71c7a4SMatthias Ringwald             break;
10653d71c7a4SMatthias Ringwald     }
10663d71c7a4SMatthias Ringwald 
1067bf78aac6SMatthias Ringwald     // track CCC writes
1068bf78aac6SMatthias Ringwald     if (att_is_persistent_ccc(attribute_handle) && offset == 0 && buffer_size == 2){
1069bf78aac6SMatthias Ringwald         att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0));
1070bf78aac6SMatthias Ringwald     }
1071bf78aac6SMatthias Ringwald 
107201ac2008SMatthias Ringwald     att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
107301ac2008SMatthias Ringwald     if (!callback) return 0;
10743d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size);
10753d71c7a4SMatthias Ringwald }
10763d71c7a4SMatthias Ringwald 
10773d71c7a4SMatthias Ringwald /**
10783d71c7a4SMatthias Ringwald  * @brief register read/write callbacks for specific handle range
10793d71c7a4SMatthias Ringwald  * @param att_service_handler_t
10803d71c7a4SMatthias Ringwald  */
10813d71c7a4SMatthias Ringwald void att_server_register_service_handler(att_service_handler_t * handler){
10823d71c7a4SMatthias Ringwald     if (att_service_handler_for_handle(handler->start_handle) ||
10833d71c7a4SMatthias Ringwald         att_service_handler_for_handle(handler->end_handle)){
10843d71c7a4SMatthias Ringwald         log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle);
10853d71c7a4SMatthias Ringwald         return;
10863d71c7a4SMatthias Ringwald     }
10873d71c7a4SMatthias Ringwald     btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler);
10883d71c7a4SMatthias Ringwald }
10893d71c7a4SMatthias Ringwald 
10903deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){
10913deb3ec6SMatthias Ringwald 
10923d71c7a4SMatthias Ringwald     // store callbacks
10933d71c7a4SMatthias Ringwald     att_server_client_read_callback  = read_callback;
10943d71c7a4SMatthias Ringwald     att_server_client_write_callback = write_callback;
10953d71c7a4SMatthias Ringwald 
10961a389cdcSMatthias Ringwald     // register for HCI Events
1097d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &att_event_packet_handler;
10981a389cdcSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
10991a389cdcSMatthias Ringwald 
1100406722e4SMatthias Ringwald     // register for SM events
1101d9a7306aSMatthias Ringwald     sm_event_callback_registration.callback = &att_event_packet_handler;
1102406722e4SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
11033deb3ec6SMatthias Ringwald 
11041a389cdcSMatthias Ringwald     // and L2CAP ATT Server PDUs
11053deb3ec6SMatthias Ringwald     att_dispatch_register_server(att_packet_handler);
11063deb3ec6SMatthias Ringwald 
110770dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
110870dca4d4SMatthias Ringwald     // setup l2cap service
110970dca4d4SMatthias Ringwald     l2cap_register_service(&att_event_packet_handler, PSM_ATT, 0xffff, LEVEL_2);
111070dca4d4SMatthias Ringwald #endif
111170dca4d4SMatthias Ringwald 
11123deb3ec6SMatthias Ringwald     att_set_db(db);
11133d71c7a4SMatthias Ringwald     att_set_read_callback(att_server_read_callback);
11143d71c7a4SMatthias Ringwald     att_set_write_callback(att_server_write_callback);
11153deb3ec6SMatthias Ringwald }
11163deb3ec6SMatthias Ringwald 
11173deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){
11183deb3ec6SMatthias Ringwald     att_client_packet_handler = handler;
11193deb3ec6SMatthias Ringwald }
11203deb3ec6SMatthias Ringwald 
1121cbbb12d9SMatthias Ringwald 
1122cbbb12d9SMatthias Ringwald // to be deprecated
1123c141988cSMatthias Ringwald int  att_server_can_send_packet_now(hci_con_handle_t con_handle){
1124c0f26e1cSMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
1125c0f26e1cSMatthias Ringwald     if (!att_server) return 0;
1126c0f26e1cSMatthias Ringwald     return att_server_can_send_packet(att_server);
11271b96b007SMatthias Ringwald }
11281b96b007SMatthias Ringwald 
1129cbbb12d9SMatthias Ringwald int att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
1130cbbb12d9SMatthias Ringwald     return att_server_request_to_send_notification(callback_registration, con_handle);
11313deb3ec6SMatthias Ringwald }
11323deb3ec6SMatthias Ringwald 
1133cbbb12d9SMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){
1134cbbb12d9SMatthias Ringwald     att_client_waiting_for_can_send_registration.callback = &att_emit_can_send_now_event;
1135cbbb12d9SMatthias Ringwald     att_server_request_to_send_notification(&att_client_waiting_for_can_send_registration, con_handle);
1136cbbb12d9SMatthias Ringwald }
1137cbbb12d9SMatthias Ringwald // end of deprecated
1138cbbb12d9SMatthias Ringwald 
1139cbbb12d9SMatthias Ringwald int att_server_request_to_send_notification(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
1140969a5bbaSMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
1141969a5bbaSMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1142cbbb12d9SMatthias Ringwald     btstack_linked_list_add_tail(&att_server->notification_requests, (btstack_linked_item_t*) callback_registration);
1143c8c6e9b4SMatthias Ringwald     att_server_request_can_send_now(att_server);
1144cbbb12d9SMatthias Ringwald     return ERROR_CODE_SUCCESS;
1145cbbb12d9SMatthias Ringwald }
1146cbbb12d9SMatthias Ringwald 
1147cbbb12d9SMatthias Ringwald int att_server_request_to_send_indication(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
1148cbbb12d9SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
1149cbbb12d9SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1150cbbb12d9SMatthias Ringwald     btstack_linked_list_add_tail(&att_server->indication_requests, (btstack_linked_item_t*) callback_registration);
1151c8c6e9b4SMatthias Ringwald     att_server_request_can_send_now(att_server);
1152969a5bbaSMatthias Ringwald     return ERROR_CODE_SUCCESS;
1153bb38f057SMatthias Ringwald }
1154bb38f057SMatthias Ringwald 
11553bb3035fSMilanka Ringwald int att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){
115618707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
115718707219SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1158c0f26e1cSMatthias Ringwald     if (!att_server_can_send_packet(att_server)) return BTSTACK_ACL_BUFFERS_FULL;
11593deb3ec6SMatthias Ringwald 
11603deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
11613deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
116218707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_notification(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
116318707219SMatthias Ringwald 	return l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
11643deb3ec6SMatthias Ringwald }
11653deb3ec6SMatthias Ringwald 
11663bb3035fSMilanka Ringwald int att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){
116718707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
116818707219SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
116918707219SMatthias Ringwald 
1170eaac31e8SMatthias Ringwald     if (att_server->value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS;
1171c0f26e1cSMatthias Ringwald     if (!att_server_can_send_packet(att_server)) return BTSTACK_ACL_BUFFERS_FULL;
11723deb3ec6SMatthias Ringwald 
11733deb3ec6SMatthias Ringwald     // track indication
117418707219SMatthias Ringwald     att_server->value_indication_handle = attribute_handle;
117518707219SMatthias Ringwald     btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout);
117618707219SMatthias Ringwald     btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS);
117718707219SMatthias Ringwald     btstack_run_loop_add_timer(&att_server->value_indication_timer);
11783deb3ec6SMatthias Ringwald 
11793deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
11803deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
118118707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_indication(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
118218707219SMatthias Ringwald 	l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
11833deb3ec6SMatthias Ringwald     return 0;
11843deb3ec6SMatthias Ringwald }
11858f9132b5SMilanka Ringwald 
11868f9132b5SMilanka Ringwald uint16_t att_server_get_mtu(hci_con_handle_t con_handle){
11878f9132b5SMilanka Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
11888f9132b5SMilanka Ringwald     if (!att_server) return 0;
11898f9132b5SMilanka Ringwald     return att_server->connection.mtu;
11908f9132b5SMilanka Ringwald }
1191