xref: /btstack/src/ble/att_server.c (revision 1979f09cf045e87f55a9cd8067e8ef902cc8d78b)
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 
7468591e36SMatthias Ringwald static void att_run_for_context(hci_connection_t * hci_connection);
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);
7868591e36SMatthias Ringwald static void att_server_persistent_ccc_restore(hci_connection_t * hci_connection);
7968591e36SMatthias Ringwald static void att_server_persistent_ccc_clear(hci_connection_t * hci_connection);
8068591e36SMatthias Ringwald static void att_server_handle_att_pdu(hci_connection_t * hci_connection, 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 
10902b78fc8SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
11068591e36SMatthias Ringwald static hci_connection_t * hci_connection_for_state(att_server_state_t state){
11118707219SMatthias Ringwald     btstack_linked_list_iterator_t it;
11218707219SMatthias Ringwald     hci_connections_get_iterator(&it);
11318707219SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
11418707219SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
11518707219SMatthias Ringwald         att_server_t * att_server = &connection->att_server;
11668591e36SMatthias Ringwald         if (att_server->state == state) return connection;
11718707219SMatthias Ringwald     }
11818707219SMatthias Ringwald     return NULL;
11918707219SMatthias Ringwald }
12002b78fc8SMatthias Ringwald #endif
121bb38f057SMatthias Ringwald 
12268591e36SMatthias Ringwald static void att_server_request_can_send_now(hci_connection_t * hci_connection){
123c8c6e9b4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
124b6df12b6SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
125c8c6e9b4SMatthias Ringwald     if (att_server->l2cap_cid != 0){
126c8c6e9b4SMatthias Ringwald         l2cap_request_can_send_now_event(att_server->l2cap_cid);
127c8c6e9b4SMatthias Ringwald         return;
128c8c6e9b4SMatthias Ringwald     }
129c8c6e9b4SMatthias Ringwald #endif
130388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
131b6df12b6SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(att_connection->con_handle);
132c8c6e9b4SMatthias Ringwald }
133c8c6e9b4SMatthias Ringwald 
134*1979f09cSMatthias Ringwald static bool att_server_can_send_packet(hci_connection_t * hci_connection){
135c0f26e1cSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
136b6df12b6SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
137c0f26e1cSMatthias Ringwald     if (att_server->l2cap_cid != 0){
138*1979f09cSMatthias Ringwald         return l2cap_can_send_packet_now(att_server->l2cap_cid) != 0;
139c0f26e1cSMatthias Ringwald     }
140c0f26e1cSMatthias Ringwald #endif
141388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
142*1979f09cSMatthias Ringwald     return att_dispatch_server_can_send_now(att_connection->con_handle) != 0;
143c0f26e1cSMatthias Ringwald }
144c0f26e1cSMatthias Ringwald 
1453deb3ec6SMatthias Ringwald static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){
1465d07396bSMatthias Ringwald     btstack_packet_handler_t packet_handler = att_server_packet_handler_for_handle(attribute_handle);
1475d07396bSMatthias Ringwald     if (!packet_handler) return;
1483deb3ec6SMatthias Ringwald 
1493deb3ec6SMatthias Ringwald     uint8_t event[7];
1503deb3ec6SMatthias Ringwald     int pos = 0;
1515611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE;
1524ea43905SMatthias Ringwald     event[pos++] = sizeof(event) - 2u;
1533deb3ec6SMatthias Ringwald     event[pos++] = status;
154f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, client_handle);
1553deb3ec6SMatthias Ringwald     pos += 2;
156f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, attribute_handle);
157a444112bSMatthias Ringwald     (*packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1583deb3ec6SMatthias Ringwald }
1593deb3ec6SMatthias Ringwald 
1605d07396bSMatthias Ringwald static void att_emit_event_to_all(const uint8_t * event, uint16_t size){
1615d07396bSMatthias Ringwald     // dispatch to app level handler
1629305033eSMatthias Ringwald     if (att_client_packet_handler != NULL){
1635d07396bSMatthias Ringwald         (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size);
1645d07396bSMatthias Ringwald     }
1653deb3ec6SMatthias Ringwald 
1665d07396bSMatthias Ringwald     // dispatch to service handlers
1675d07396bSMatthias Ringwald     btstack_linked_list_iterator_t it;
1685d07396bSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
1695d07396bSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1705d07396bSMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
1715d07396bSMatthias Ringwald         if (!handler->packet_handler) continue;
1725d07396bSMatthias Ringwald         (*handler->packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size);
1735d07396bSMatthias Ringwald     }
1745d07396bSMatthias Ringwald }
1755d07396bSMatthias Ringwald 
1765d07396bSMatthias Ringwald static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){
1773deb3ec6SMatthias Ringwald     uint8_t event[6];
1783deb3ec6SMatthias Ringwald     int pos = 0;
1795611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE;
1804ea43905SMatthias Ringwald     event[pos++] = sizeof(event) - 2u;
181fc64f94aSMatthias Ringwald     little_endian_store_16(event, pos, con_handle);
1823deb3ec6SMatthias Ringwald     pos += 2;
183f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, mtu);
1845d07396bSMatthias Ringwald 
1855d07396bSMatthias Ringwald     // also dispatch to GATT Clients
186b12646c5SJakob Krantz     att_dispatch_server_mtu_exchanged(con_handle, mtu);
1875d07396bSMatthias Ringwald 
1885d07396bSMatthias Ringwald     // dispatch to app level handler and service handlers
1895d07396bSMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
1903deb3ec6SMatthias Ringwald }
1913deb3ec6SMatthias Ringwald 
1925f141511SMatthias Ringwald static void att_emit_can_send_now_event(void * context){
1935f141511SMatthias Ringwald     UNUSED(context);
1943c97b242SMatthias Ringwald     if (!att_client_packet_handler) return;
1953c97b242SMatthias Ringwald 
1961b96b007SMatthias Ringwald     uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0};
1971b96b007SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1981b96b007SMatthias Ringwald }
1991b96b007SMatthias Ringwald 
20068591e36SMatthias Ringwald static void att_emit_connected_event(hci_connection_t * hci_connection){
20168591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
202388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
203ff1bec95SMatthias Ringwald     uint8_t event[11];
204ff1bec95SMatthias Ringwald     int pos = 0;
205ff1bec95SMatthias Ringwald     event[pos++] = ATT_EVENT_CONNECTED;
2064ea43905SMatthias Ringwald     event[pos++] = sizeof(event) - 2u;
207ff1bec95SMatthias Ringwald     event[pos++] = att_server->peer_addr_type;
208ff1bec95SMatthias Ringwald     reverse_bd_addr(att_server->peer_address, &event[pos]);
209ff1bec95SMatthias Ringwald     pos += 6;
210b6df12b6SMatthias Ringwald     little_endian_store_16(event, pos, att_connection->con_handle);
211ff1bec95SMatthias Ringwald     pos += 2;
212ff1bec95SMatthias Ringwald 
213ff1bec95SMatthias Ringwald     // dispatch to app level handler and service handlers
214ff1bec95SMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
215ff1bec95SMatthias Ringwald }
216ff1bec95SMatthias Ringwald 
217ff1bec95SMatthias Ringwald 
2186187ad4cSMatthias Ringwald static void att_emit_disconnected_event(uint16_t con_handle){
219ff1bec95SMatthias Ringwald     uint8_t event[4];
220ff1bec95SMatthias Ringwald     int pos = 0;
221ff1bec95SMatthias Ringwald     event[pos++] = ATT_EVENT_DISCONNECTED;
2224ea43905SMatthias Ringwald     event[pos++] = sizeof(event) - 2u;
2236187ad4cSMatthias Ringwald     little_endian_store_16(event, pos, 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 
230ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){
23154178543SMatthias Ringwald     void * context = btstack_run_loop_get_timer_context(ts);
23254178543SMatthias Ringwald     hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
23368591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
23468591e36SMatthias Ringwald     if (!hci_connection) return;
23568591e36SMatthias Ringwald     // @note: after a transaction timeout, no more requests shall be sent over this ATT Bearer
236bce48a26SMatthias Ringwald     // (that's why we don't reset the value_indication_handle)
237b6df12b6SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
23818707219SMatthias Ringwald     uint16_t att_handle = att_server->value_indication_handle;
239388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
240b6df12b6SMatthias Ringwald     att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_connection->con_handle, att_handle);
2413deb3ec6SMatthias Ringwald }
2423deb3ec6SMatthias Ringwald 
2433deb3ec6SMatthias Ringwald static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2449ec2630cSMatthias Ringwald 
2457dad9ff8SMatthias Ringwald     UNUSED(channel); // ok: there is no channel
2467dad9ff8SMatthias Ringwald     UNUSED(size);    // ok: handling own l2cap events
2473deb3ec6SMatthias Ringwald 
24818707219SMatthias Ringwald     att_server_t * att_server;
249b6df12b6SMatthias Ringwald     att_connection_t * att_connection;
25018707219SMatthias Ringwald     hci_con_handle_t con_handle;
25168591e36SMatthias Ringwald     hci_connection_t * hci_connection;
25270dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
25370dca4d4SMatthias Ringwald     bd_addr_t address;
25468591e36SMatthias Ringwald     btstack_linked_list_iterator_t it;
25570dca4d4SMatthias Ringwald #endif
25618707219SMatthias Ringwald 
2573deb3ec6SMatthias Ringwald     switch (packet_type) {
2583deb3ec6SMatthias Ringwald 
2593deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
2600e2df43fSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
2613deb3ec6SMatthias Ringwald 
26270dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
26370dca4d4SMatthias Ringwald                 case L2CAP_EVENT_INCOMING_CONNECTION:
26470dca4d4SMatthias Ringwald                     l2cap_event_incoming_connection_get_address(packet, address);
26570dca4d4SMatthias Ringwald                     l2cap_accept_connection(channel);
26636de8547SMatthias Ringwald                     log_info("Accept incoming connection from %s", bd_addr_to_str(address));
26770dca4d4SMatthias Ringwald                     break;
26870dca4d4SMatthias Ringwald                 case L2CAP_EVENT_CHANNEL_OPENED:
269122b723eSMatthias Ringwald                     con_handle = l2cap_event_channel_opened_get_handle(packet);
27068591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
27168591e36SMatthias Ringwald                     if (!hci_connection) break;
27268591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
273122b723eSMatthias Ringwald                     // store connection info
274f16129ceSMatthias Ringwald                     att_server->peer_addr_type = BD_ADDR_TYPE_ACL;
275122b723eSMatthias Ringwald                     l2cap_event_channel_opened_get_address(packet, att_server->peer_address);
276388fa7c4SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
277b6df12b6SMatthias Ringwald                     att_connection->con_handle = con_handle;
27870dca4d4SMatthias Ringwald                     att_server->l2cap_cid = l2cap_event_channel_opened_get_local_cid(packet);
279122b723eSMatthias Ringwald                     // reset connection properties
280122b723eSMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
281b6df12b6SMatthias Ringwald                     att_connection->mtu = l2cap_event_channel_opened_get_remote_mtu(packet);
282b6df12b6SMatthias Ringwald                     att_connection->max_mtu = l2cap_max_mtu();
283b6df12b6SMatthias Ringwald                     if (att_connection->max_mtu > ATT_REQUEST_BUFFER_SIZE){
284b6df12b6SMatthias Ringwald                         att_connection->max_mtu = ATT_REQUEST_BUFFER_SIZE;
285122b723eSMatthias Ringwald                     }
286df33ee14SMatthias Ringwald 
287b6df12b6SMatthias Ringwald                     log_info("Connection opened %s, l2cap cid %04x, mtu %u", bd_addr_to_str(address), att_server->l2cap_cid, att_connection->mtu);
288df33ee14SMatthias Ringwald 
289914988a1SMatthias Ringwald                     // update security params
290b6df12b6SMatthias Ringwald                     att_connection->encryption_key_size = gap_encryption_key_size(con_handle);
291b6df12b6SMatthias Ringwald                     att_connection->authenticated = gap_authenticated(con_handle);
292b6df12b6SMatthias Ringwald                     att_connection->secure_connection = gap_secure_connection(con_handle);
293914988a1SMatthias Ringwald                     log_info("encrypted key size %u, authenticated %u, secure connection %u",
294b6df12b6SMatthias Ringwald                         att_connection->encryption_key_size, att_connection->authenticated, att_connection->secure_connection);
295df33ee14SMatthias Ringwald 
296df33ee14SMatthias Ringwald                     // notify connection opened
29768591e36SMatthias Ringwald                     att_emit_connected_event(hci_connection);
298df33ee14SMatthias Ringwald 
299914988a1SMatthias Ringwald                     // restore persisten ccc if encrypted
300914988a1SMatthias Ringwald                     if ( gap_security_level(con_handle) >= LEVEL_2){
30168591e36SMatthias Ringwald                         att_server_persistent_ccc_restore(hci_connection);
302914988a1SMatthias Ringwald                     }
303122b723eSMatthias Ringwald                     // TODO: what to do about le device db?
304122b723eSMatthias Ringwald                     att_server->pairing_active = 0;
30570dca4d4SMatthias Ringwald                     break;
30648364364SMatthias Ringwald                 case L2CAP_EVENT_CAN_SEND_NOW:
30748364364SMatthias Ringwald                     att_server_handle_can_send_now();
30848364364SMatthias Ringwald                     break;
30948364364SMatthias Ringwald 
31070dca4d4SMatthias Ringwald #endif
3113deb3ec6SMatthias Ringwald                 case HCI_EVENT_LE_META:
3123deb3ec6SMatthias Ringwald                     switch (packet[2]) {
3133deb3ec6SMatthias Ringwald                         case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
31418707219SMatthias Ringwald                             con_handle = little_endian_read_16(packet, 4);
31568591e36SMatthias Ringwald                             hci_connection = hci_connection_for_handle(con_handle);
31668591e36SMatthias Ringwald                             if (!hci_connection) break;
31768591e36SMatthias Ringwald                             att_server = &hci_connection->att_server;
3183deb3ec6SMatthias Ringwald                         	// store connection info
31918707219SMatthias Ringwald                         	att_server->peer_addr_type = packet[7];
32018707219SMatthias Ringwald                             reverse_bd_addr(&packet[8], att_server->peer_address);
321388fa7c4SMatthias Ringwald                             att_connection = &hci_connection->att_connection;
322b6df12b6SMatthias Ringwald                             att_connection->con_handle = con_handle;
3233deb3ec6SMatthias Ringwald                             // reset connection properties
32418707219SMatthias Ringwald                             att_server->state = ATT_SERVER_IDLE;
325b6df12b6SMatthias Ringwald                             att_connection->mtu = ATT_DEFAULT_MTU;
326b6df12b6SMatthias Ringwald                             att_connection->max_mtu = l2cap_max_le_mtu();
327b6df12b6SMatthias Ringwald                             if (att_connection->max_mtu > ATT_REQUEST_BUFFER_SIZE){
328b6df12b6SMatthias Ringwald                                 att_connection->max_mtu = ATT_REQUEST_BUFFER_SIZE;
32999c58ad0SMatthias Ringwald                             }
330b6df12b6SMatthias Ringwald                             att_connection->encryption_key_size = 0;
331b6df12b6SMatthias Ringwald                             att_connection->authenticated = 0;
332b6df12b6SMatthias Ringwald 		                	att_connection->authorized = 0;
333b2c1e52cSMatthias Ringwald                             // workaround: identity resolving can already be complete, at least store result
334b2c1e52cSMatthias Ringwald                             att_server->ir_le_device_db_index = sm_le_device_index(con_handle);
33530c8e114SMatthias Ringwald                             att_server->ir_lookup_active = 0;
336760ad805SMatthias Ringwald                             att_server->pairing_active = 0;
337ff1bec95SMatthias Ringwald                             // notify all - old
3385d07396bSMatthias Ringwald                             att_emit_event_to_all(packet, size);
339ff1bec95SMatthias Ringwald                             // notify all - new
34068591e36SMatthias Ringwald                             att_emit_connected_event(hci_connection);
3413deb3ec6SMatthias Ringwald                             break;
3423deb3ec6SMatthias Ringwald 
3433deb3ec6SMatthias Ringwald                         default:
3443deb3ec6SMatthias Ringwald                             break;
3453deb3ec6SMatthias Ringwald                     }
3463deb3ec6SMatthias Ringwald                     break;
3473deb3ec6SMatthias Ringwald 
3483deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_CHANGE:
3493deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE:
3503deb3ec6SMatthias Ringwald                 	// check handle
35118707219SMatthias Ringwald                     con_handle = little_endian_read_16(packet, 3);
35268591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
35368591e36SMatthias Ringwald                     if (!hci_connection) break;
354914988a1SMatthias Ringwald                     if (gap_get_connection_type(con_handle) != GAP_CONNECTION_LE) break;
355914988a1SMatthias Ringwald                     // update security params
35668591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
357388fa7c4SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
358b6df12b6SMatthias Ringwald                     att_connection->encryption_key_size = gap_encryption_key_size(con_handle);
359b6df12b6SMatthias Ringwald                     att_connection->authenticated = gap_authenticated(con_handle);
360b6df12b6SMatthias Ringwald                     att_connection->secure_connection = gap_secure_connection(con_handle);
361914988a1SMatthias Ringwald                     log_info("encrypted key size %u, authenticated %u, secure connection %u",
362b6df12b6SMatthias Ringwald                         att_connection->encryption_key_size, att_connection->authenticated, att_connection->secure_connection);
363914988a1SMatthias Ringwald                     if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){
3644097998aSMatthias Ringwald                         // restore CCC values when encrypted for LE Connections
36501ac2008SMatthias Ringwald                         if (hci_event_encryption_change_get_encryption_enabled(packet)){
36668591e36SMatthias Ringwald                             att_server_persistent_ccc_restore(hci_connection);
36701ac2008SMatthias Ringwald                         }
36801ac2008SMatthias Ringwald                     }
36968591e36SMatthias Ringwald                     att_run_for_context(hci_connection);
3703deb3ec6SMatthias Ringwald                     break;
3713deb3ec6SMatthias Ringwald 
3723deb3ec6SMatthias Ringwald                 case HCI_EVENT_DISCONNECTION_COMPLETE:
37370a390c7SMatthias Ringwald                     // check handle
37418707219SMatthias Ringwald                     con_handle = hci_event_disconnection_complete_get_connection_handle(packet);
37568591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
37668591e36SMatthias Ringwald                     if (!hci_connection) break;
37768591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
378388fa7c4SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
379b6df12b6SMatthias Ringwald                     att_clear_transaction_queue(att_connection);
380b6df12b6SMatthias Ringwald                     att_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
387b6df12b6SMatthias Ringwald                         att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_DISCONNECT, att_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);
39868591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
39968591e36SMatthias Ringwald                     if (!hci_connection) break;
40068591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
4015611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED");
40230c8e114SMatthias Ringwald                     att_server->ir_lookup_active = 1;
4033deb3ec6SMatthias Ringwald                     break;
4045611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
405760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
40668591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
40768591e36SMatthias Ringwald                     if (!hci_connection) return;
40868591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
40930c8e114SMatthias Ringwald                     att_server->ir_lookup_active = 0;
4101b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index(packet);
4111b7acd0dSMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED");
41268591e36SMatthias Ringwald                     att_run_for_context(hci_connection);
4133deb3ec6SMatthias Ringwald                     break;
4145611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_FAILED:
41518707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_failed_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_FAILED");
42030c8e114SMatthias Ringwald                     att_server->ir_lookup_active = 0;
42118707219SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
42268591e36SMatthias Ringwald                     att_run_for_context(hci_connection);
4233deb3ec6SMatthias Ringwald                     break;
424760ad805SMatthias Ringwald 
425760ad805SMatthias Ringwald                 // Pairing started - delete stored CCC values
426760ad805SMatthias Ringwald                 // - assumes pairing indicates either new device or re-pairing, in both cases there should be no stored CCC values
427760ad805SMatthias Ringwald                 // - assumes that all events have the con handle as the first field
428760ad805SMatthias Ringwald                 case SM_EVENT_JUST_WORKS_REQUEST:
429760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
430760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_INPUT_NUMBER:
431760ad805SMatthias Ringwald                 case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
432760ad805SMatthias Ringwald                     con_handle = sm_event_just_works_request_get_handle(packet);
43368591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
43468591e36SMatthias Ringwald                     if (!hci_connection) break;
43568591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
436760ad805SMatthias Ringwald                     att_server->pairing_active = 1;
437760ad805SMatthias Ringwald                     log_info("SM Pairing started");
438760ad805SMatthias Ringwald                     if (att_server->ir_le_device_db_index < 0) break;
43968591e36SMatthias Ringwald                     att_server_persistent_ccc_clear(hci_connection);
440760ad805SMatthias Ringwald                     // index not valid anymore
441760ad805SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
442760ad805SMatthias Ringwald                     break;
443760ad805SMatthias Ringwald 
4441b7acd0dSMatthias Ringwald                 // Bonding completed
445760ad805SMatthias Ringwald                 case SM_EVENT_IDENTITY_CREATED:
446760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
44768591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
44868591e36SMatthias Ringwald                     if (!hci_connection) return;
44968591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
450760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
4511b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_created_get_index(packet);
45268591e36SMatthias Ringwald                     att_run_for_context(hci_connection);
4531b7acd0dSMatthias Ringwald                     break;
4541b7acd0dSMatthias Ringwald 
4551b7acd0dSMatthias Ringwald                 // Pairing complete (with/without bonding=storing of pairing information)
4561b7acd0dSMatthias Ringwald                 case SM_EVENT_PAIRING_COMPLETE:
4571b7acd0dSMatthias Ringwald                     con_handle = sm_event_pairing_complete_get_handle(packet);
45868591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
45968591e36SMatthias Ringwald                     if (!hci_connection) return;
46068591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
4611b7acd0dSMatthias Ringwald                     att_server->pairing_active = 0;
46268591e36SMatthias Ringwald                     att_run_for_context(hci_connection);
463760ad805SMatthias Ringwald                     break;
464760ad805SMatthias Ringwald 
465760ad805SMatthias Ringwald                 // Authorization
4665611a760SMatthias Ringwald                 case SM_EVENT_AUTHORIZATION_RESULT: {
46718707219SMatthias Ringwald                     con_handle = sm_event_authorization_result_get_handle(packet);
46868591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
46968591e36SMatthias Ringwald                     if (!hci_connection) break;
47068591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
471388fa7c4SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
472b6df12b6SMatthias Ringwald                     att_connection->authorized = sm_event_authorization_result_get_authorization_result(packet);
47368591e36SMatthias Ringwald                     att_server_request_can_send_now(hci_connection);
4743deb3ec6SMatthias Ringwald                 	break;
4753deb3ec6SMatthias Ringwald                 }
4763deb3ec6SMatthias Ringwald                 default:
4773deb3ec6SMatthias Ringwald                     break;
4783deb3ec6SMatthias Ringwald             }
47965b44ffdSMatthias Ringwald             break;
48070dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
48170dca4d4SMatthias Ringwald         case L2CAP_DATA_PACKET:
48268591e36SMatthias Ringwald             hci_connections_get_iterator(&it);
48368591e36SMatthias Ringwald             while(btstack_linked_list_iterator_has_next(&it)){
484388fa7c4SMatthias Ringwald                 hci_connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
485388fa7c4SMatthias Ringwald                 att_server = &hci_connection->att_server;
48668591e36SMatthias Ringwald                 if (att_server->l2cap_cid == channel) {
487388fa7c4SMatthias Ringwald                     att_server_handle_att_pdu(hci_connection, packet, size);
48868591e36SMatthias Ringwald                     break;
48968591e36SMatthias Ringwald                 }
49068591e36SMatthias Ringwald             }
49170dca4d4SMatthias Ringwald             break;
49270dca4d4SMatthias Ringwald #endif
49341772f37SMatthias Ringwald 
49465b44ffdSMatthias Ringwald         default:
49565b44ffdSMatthias Ringwald             break;
4963deb3ec6SMatthias Ringwald     }
4973deb3ec6SMatthias Ringwald }
4983deb3ec6SMatthias Ringwald 
4997a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
5003deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
5013deb3ec6SMatthias Ringwald 
50268591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION);
50368591e36SMatthias Ringwald     if (!hci_connection) return;
50468591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
5053deb3ec6SMatthias Ringwald 
5063deb3ec6SMatthias Ringwald     uint8_t hash_flipped[8];
5079c80e4ccSMatthias Ringwald     reverse_64(hash, hash_flipped);
50818707219SMatthias Ringwald     if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8)){
5093deb3ec6SMatthias Ringwald         log_info("ATT Signed Write, invalid signature");
51018707219SMatthias Ringwald         att_server->state = ATT_SERVER_IDLE;
5113deb3ec6SMatthias Ringwald         return;
5123deb3ec6SMatthias Ringwald     }
5133deb3ec6SMatthias Ringwald     log_info("ATT Signed Write, valid signature");
5143deb3ec6SMatthias Ringwald 
5153deb3ec6SMatthias Ringwald     // update sequence number
51618707219SMatthias Ringwald     uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
51718707219SMatthias Ringwald     le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1);
51818707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
51968591e36SMatthias Ringwald     att_server_request_can_send_now(hci_connection);
5203deb3ec6SMatthias Ringwald }
5217a766ebfSMatthias Ringwald #endif
52288a48dd8SMatthias Ringwald 
52318707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED
524b06f2579SMatthias Ringwald // pre: can send now
525b06f2579SMatthias Ringwald // returns: 1 if packet was sent
52668591e36SMatthias Ringwald static int att_server_process_validated_request(hci_connection_t * hci_connection){
52768591e36SMatthias Ringwald     att_server_t * att_server = & hci_connection->att_server;
528388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
529b06f2579SMatthias Ringwald 
530b06f2579SMatthias Ringwald     l2cap_reserve_packet_buffer();
531b06f2579SMatthias Ringwald     uint8_t * att_response_buffer = l2cap_get_outgoing_buffer();
532b6df12b6SMatthias Ringwald     uint16_t  att_response_size   = att_handle_request(att_connection, att_server->request_buffer, att_server->request_size, att_response_buffer);
533b06f2579SMatthias Ringwald 
534beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
535c1ab6cc1SMatthias Ringwald     if ((att_response_size == ATT_READ_RESPONSE_PENDING) || (att_response_size == ATT_INTERNAL_WRITE_RESPONSE_PENDING)){
536e404a688SMatthias Ringwald         // update state
537beb20288SMatthias Ringwald         att_server->state = ATT_SERVER_RESPONSE_PENDING;
538e404a688SMatthias Ringwald 
53956c3a347SMatthias Ringwald         // callback with handle ATT_READ_RESPONSE_PENDING for reads
54056c3a347SMatthias Ringwald         if (att_response_size == ATT_READ_RESPONSE_PENDING){
541b6df12b6SMatthias Ringwald             att_server_client_read_callback(att_connection->con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0);
54256c3a347SMatthias Ringwald         }
543e8e7403eSMatthias Ringwald 
544e8e7403eSMatthias Ringwald         // free reserved buffer
545e8e7403eSMatthias Ringwald         l2cap_release_packet_buffer();
546e8e7403eSMatthias Ringwald         return 0;
547e404a688SMatthias Ringwald     }
548e404a688SMatthias Ringwald #endif
549e404a688SMatthias Ringwald 
550b06f2579SMatthias Ringwald     // intercept "insufficient authorization" for authenticated connections to allow for user authorization
5514ea43905SMatthias Ringwald     if ((att_response_size     >= 4u)
552b06f2579SMatthias Ringwald     && (att_response_buffer[0] == ATT_ERROR_RESPONSE)
553b06f2579SMatthias Ringwald     && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION)
554b6df12b6SMatthias Ringwald     && (att_connection->authenticated)){
555b06f2579SMatthias Ringwald 
556b6df12b6SMatthias Ringwald         switch (gap_authorization_state(att_connection->con_handle)){
557b06f2579SMatthias Ringwald             case AUTHORIZATION_UNKNOWN:
558b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
559b6df12b6SMatthias Ringwald                 sm_request_pairing(att_connection->con_handle);
560b06f2579SMatthias Ringwald                 return 0;
561b06f2579SMatthias Ringwald             case AUTHORIZATION_PENDING:
562b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
563b06f2579SMatthias Ringwald                 return 0;
564b06f2579SMatthias Ringwald             default:
565b06f2579SMatthias Ringwald                 break;
566b06f2579SMatthias Ringwald         }
567b06f2579SMatthias Ringwald     }
568b06f2579SMatthias Ringwald 
56918707219SMatthias Ringwald     att_server->state = ATT_SERVER_IDLE;
5704ea43905SMatthias Ringwald     if (att_response_size == 0u) {
571b06f2579SMatthias Ringwald         l2cap_release_packet_buffer();
572b06f2579SMatthias Ringwald         return 0;
573b06f2579SMatthias Ringwald     }
574b06f2579SMatthias Ringwald 
57548364364SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
57648364364SMatthias Ringwald     if (att_server->l2cap_cid != 0){
57748364364SMatthias Ringwald         l2cap_send_prepared(att_server->l2cap_cid, att_response_size);
57848364364SMatthias Ringwald     } else
57948364364SMatthias Ringwald #endif
58048364364SMatthias Ringwald     {
581b6df12b6SMatthias Ringwald         l2cap_send_prepared_connectionless(att_connection->con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size);
58248364364SMatthias Ringwald     }
583b06f2579SMatthias Ringwald 
584b06f2579SMatthias Ringwald     // notify client about MTU exchange result
585b06f2579SMatthias Ringwald     if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){
586b6df12b6SMatthias Ringwald         att_emit_mtu_event(att_connection->con_handle, att_connection->mtu);
587b06f2579SMatthias Ringwald     }
588b06f2579SMatthias Ringwald     return 1;
589b06f2579SMatthias Ringwald }
590b06f2579SMatthias Ringwald 
591beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
592beb20288SMatthias Ringwald int att_server_response_ready(hci_con_handle_t con_handle){
59368591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
59468591e36SMatthias Ringwald     if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
59568591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
59668591e36SMatthias Ringwald 
597beb20288SMatthias Ringwald     if (att_server->state != ATT_SERVER_RESPONSE_PENDING)   return ERROR_CODE_COMMAND_DISALLOWED;
598e404a688SMatthias Ringwald 
599e404a688SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
60068591e36SMatthias Ringwald     att_server_request_can_send_now(hci_connection);
601e404a688SMatthias Ringwald     return ERROR_CODE_SUCCESS;
602e404a688SMatthias Ringwald }
603e404a688SMatthias Ringwald #endif
604e404a688SMatthias Ringwald 
60568591e36SMatthias Ringwald static void att_run_for_context(hci_connection_t * hci_connection){
60668591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
607388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
60818707219SMatthias Ringwald     switch (att_server->state){
6093deb3ec6SMatthias Ringwald         case ATT_SERVER_REQUEST_RECEIVED:
610760ad805SMatthias Ringwald 
61148364364SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
61248364364SMatthias Ringwald             if (att_server->l2cap_cid != 0){
61348364364SMatthias Ringwald                 // ok
61448364364SMatthias Ringwald             } else
61548364364SMatthias Ringwald #endif
61648364364SMatthias Ringwald             {
6170234fbbcSMatthias Ringwald                 // wait until re-encryption as central is complete
618b6df12b6SMatthias Ringwald                 if (gap_reconnect_security_setup_active(att_connection->con_handle)) break;
61948364364SMatthias Ringwald             }
6200234fbbcSMatthias Ringwald 
621760ad805SMatthias Ringwald             // wait until pairing is complete
622760ad805SMatthias Ringwald             if (att_server->pairing_active) break;
623760ad805SMatthias Ringwald 
6247a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
62518707219SMatthias Ringwald             if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){
6263deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write!");
6273deb3ec6SMatthias Ringwald                 if (!sm_cmac_ready()) {
6283deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, sm_cmac engine not ready. Abort");
62918707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
6303deb3ec6SMatthias Ringwald                     return;
6313deb3ec6SMatthias Ringwald                 }
63218707219SMatthias Ringwald                 if (att_server->request_size < (3 + 12)) {
6333deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, request to short. Abort.");
63418707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
6353deb3ec6SMatthias Ringwald                     return;
6363deb3ec6SMatthias Ringwald                 }
63718707219SMatthias Ringwald                 if (att_server->ir_lookup_active){
6383deb3ec6SMatthias Ringwald                     return;
6393deb3ec6SMatthias Ringwald                 }
64018707219SMatthias Ringwald                 if (att_server->ir_le_device_db_index < 0){
6413deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, CSRK not available");
64218707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
6433deb3ec6SMatthias Ringwald                     return;
6443deb3ec6SMatthias Ringwald                 }
6453deb3ec6SMatthias Ringwald 
6463deb3ec6SMatthias Ringwald                 // check counter
64718707219SMatthias Ringwald                 uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
64818707219SMatthias Ringwald                 uint32_t counter_db     = le_device_db_remote_counter_get(att_server->ir_le_device_db_index);
6493deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet);
6503deb3ec6SMatthias Ringwald                 if (counter_packet < counter_db){
6513deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, db reports higher counter, abort");
65218707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
6533deb3ec6SMatthias Ringwald                     return;
6543deb3ec6SMatthias Ringwald                 }
6553deb3ec6SMatthias Ringwald 
6563deb3ec6SMatthias Ringwald                 // signature is { sequence counter, secure hash }
6573deb3ec6SMatthias Ringwald                 sm_key_t csrk;
65818707219SMatthias Ringwald                 le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk);
65918707219SMatthias Ringwald                 att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION;
6603deb3ec6SMatthias Ringwald                 log_info("Orig Signature: ");
66118707219SMatthias Ringwald                 log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8);
66218707219SMatthias Ringwald                 uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1);
66318707219SMatthias 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);
6643deb3ec6SMatthias Ringwald                 return;
6653deb3ec6SMatthias Ringwald             }
6667a766ebfSMatthias Ringwald #endif
667b06f2579SMatthias Ringwald             // move on
66818707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
66968591e36SMatthias Ringwald             att_server_request_can_send_now(hci_connection);
670b06f2579SMatthias Ringwald             break;
67188a48dd8SMatthias Ringwald 
6723deb3ec6SMatthias Ringwald         default:
6733deb3ec6SMatthias Ringwald             break;
6743deb3ec6SMatthias Ringwald     }
6753deb3ec6SMatthias Ringwald }
6763deb3ec6SMatthias Ringwald 
677*1979f09cSMatthias Ringwald static bool att_server_data_ready_for_phase(att_server_t * att_server,  att_server_run_phase_t phase){
67890f03c80SMatthias Ringwald     switch (phase){
67990f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
68090f03c80SMatthias Ringwald             return att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
68190f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
682c1ab6cc1SMatthias Ringwald              return (!btstack_linked_list_empty(&att_server->indication_requests) && (att_server->value_indication_handle == 0));
68390f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
68490f03c80SMatthias Ringwald             return (!btstack_linked_list_empty(&att_server->notification_requests));
6857bbeb3adSMilanka Ringwald         default:
6867bbeb3adSMilanka Ringwald             btstack_assert(false);
687*1979f09cSMatthias Ringwald             return false;
68890f03c80SMatthias Ringwald     }
6897bbeb3adSMilanka Ringwald }
69090f03c80SMatthias Ringwald 
69168591e36SMatthias Ringwald static void att_server_trigger_send_for_phase(hci_connection_t * hci_connection,  att_server_run_phase_t phase){
69290f03c80SMatthias Ringwald     btstack_context_callback_registration_t * client;
69368591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
69490f03c80SMatthias Ringwald     switch (phase){
69590f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
69668591e36SMatthias Ringwald             att_server_process_validated_request(hci_connection);
69790f03c80SMatthias Ringwald             break;
69890f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
69990f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->indication_requests;
70090f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->indication_requests, (btstack_linked_item_t *) client);
70190f03c80SMatthias Ringwald             client->callback(client->context);
70290f03c80SMatthias Ringwald             break;
70390f03c80SMatthias Ringwald        case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
70490f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->notification_requests;
70590f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->notification_requests, (btstack_linked_item_t *) client);
70690f03c80SMatthias Ringwald             client->callback(client->context);
70790f03c80SMatthias Ringwald             break;
7087bbeb3adSMilanka Ringwald         default:
7097bbeb3adSMilanka Ringwald             btstack_assert(false);
7107bbeb3adSMilanka Ringwald             break;
71190f03c80SMatthias Ringwald     }
71290f03c80SMatthias Ringwald }
71390f03c80SMatthias Ringwald 
7147eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){
715b06f2579SMatthias Ringwald 
7166438547aSMatthias Ringwald     hci_con_handle_t last_send_con_handle = HCI_CON_HANDLE_INVALID;
71768591e36SMatthias Ringwald     hci_connection_t * request_hci_connection   = NULL;
718*1979f09cSMatthias Ringwald     bool can_send_now = true;
7194395a000SMatthias Ringwald     int phase_index;
7206438547aSMatthias Ringwald 
7214395a000SMatthias Ringwald     for (phase_index = ATT_SERVER_RUN_PHASE_1_REQUESTS; phase_index <= ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS; phase_index++){
7224395a000SMatthias Ringwald         att_server_run_phase_t phase = (att_server_run_phase_t) phase_index;
7236438547aSMatthias Ringwald         hci_con_handle_t skip_connections_until = att_server_last_can_send_now;
724ff3cc4a5SMatthias Ringwald         while (true){
7256438547aSMatthias Ringwald             btstack_linked_list_iterator_t it;
72618707219SMatthias Ringwald             hci_connections_get_iterator(&it);
72718707219SMatthias Ringwald             while(btstack_linked_list_iterator_has_next(&it)){
72818707219SMatthias Ringwald                 hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
72918707219SMatthias Ringwald                 att_server_t * att_server = &connection->att_server;
730388fa7c4SMatthias Ringwald                 att_connection_t * att_connection = &connection->att_connection;
7316438547aSMatthias Ringwald 
732*1979f09cSMatthias Ringwald                 bool data_ready = att_server_data_ready_for_phase(att_server, phase);
7336438547aSMatthias Ringwald 
734b6df12b6SMatthias 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);
7356438547aSMatthias Ringwald 
7366438547aSMatthias Ringwald                 // skip until last sender found (which is also skipped)
7376438547aSMatthias Ringwald                 if (skip_connections_until != HCI_CON_HANDLE_INVALID){
73868591e36SMatthias Ringwald                     if (data_ready && (request_hci_connection == NULL)){
73968591e36SMatthias Ringwald                         request_hci_connection = connection;
7406438547aSMatthias Ringwald                     }
741b6df12b6SMatthias Ringwald                     if (skip_connections_until == att_connection->con_handle){
7426438547aSMatthias Ringwald                         skip_connections_until = HCI_CON_HANDLE_INVALID;
7436438547aSMatthias Ringwald                     }
7446438547aSMatthias Ringwald                     continue;
7456438547aSMatthias Ringwald                 };
7466438547aSMatthias Ringwald 
74790f03c80SMatthias Ringwald                 if (data_ready){
748969a5bbaSMatthias Ringwald                     if (can_send_now){
74968591e36SMatthias Ringwald                         att_server_trigger_send_for_phase(connection, phase);
750b6df12b6SMatthias Ringwald                         last_send_con_handle = att_connection->con_handle;
75168591e36SMatthias Ringwald                         can_send_now = att_server_can_send_packet(connection);
75290f03c80SMatthias Ringwald                         data_ready = att_server_data_ready_for_phase(att_server, phase);
75368591e36SMatthias Ringwald                         if (data_ready && (request_hci_connection == NULL)){
75468591e36SMatthias Ringwald                             request_hci_connection = connection;
755cbbb12d9SMatthias Ringwald                         }
756cbbb12d9SMatthias Ringwald                     } else {
75768591e36SMatthias Ringwald                         request_hci_connection = connection;
758f29a88d8SMatthias Ringwald                         break;
759cbbb12d9SMatthias Ringwald                     }
760cbbb12d9SMatthias Ringwald                 }
7613deb3ec6SMatthias Ringwald             }
7623deb3ec6SMatthias Ringwald 
7636438547aSMatthias Ringwald             // stop skipping (handles disconnect by last send connection)
7646438547aSMatthias Ringwald             skip_connections_until = HCI_CON_HANDLE_INVALID;
7656438547aSMatthias Ringwald 
7663551936eSMatthias Ringwald             // Exit loop, if we cannot send
767969a5bbaSMatthias Ringwald             if (!can_send_now) break;
768969a5bbaSMatthias Ringwald 
769969a5bbaSMatthias Ringwald             // Exit loop, if we can send but there are also no further request
77068591e36SMatthias Ringwald             if (request_hci_connection == NULL) break;
771969a5bbaSMatthias Ringwald 
772969a5bbaSMatthias Ringwald             // Finally, if we still can send and there are requests, just try again
77368591e36SMatthias Ringwald             request_hci_connection = NULL;
774969a5bbaSMatthias Ringwald         }
7756438547aSMatthias Ringwald         // update last send con handle for round robin
7766438547aSMatthias Ringwald         if (last_send_con_handle != HCI_CON_HANDLE_INVALID){
7776438547aSMatthias Ringwald             att_server_last_can_send_now = last_send_con_handle;
7786438547aSMatthias Ringwald         }
7793551936eSMatthias Ringwald     }
780969a5bbaSMatthias Ringwald 
78168591e36SMatthias Ringwald     if (request_hci_connection == NULL) return;
78268591e36SMatthias Ringwald     att_server_request_can_send_now(request_hci_connection);
783969a5bbaSMatthias Ringwald }
784969a5bbaSMatthias Ringwald 
78568591e36SMatthias Ringwald static void att_server_handle_att_pdu(hci_connection_t * hci_connection, uint8_t * packet, uint16_t size){
78668591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
787388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
78818707219SMatthias Ringwald 
7893deb3ec6SMatthias Ringwald     // handle value indication confirms
790c1ab6cc1SMatthias Ringwald     if ((packet[0] == ATT_HANDLE_VALUE_CONFIRMATION) && att_server->value_indication_handle){
79118707219SMatthias Ringwald         btstack_run_loop_remove_timer(&att_server->value_indication_timer);
79218707219SMatthias Ringwald         uint16_t att_handle = att_server->value_indication_handle;
79318707219SMatthias Ringwald         att_server->value_indication_handle = 0;
794b6df12b6SMatthias Ringwald         att_handle_value_indication_notify_client(0, att_connection->con_handle, att_handle);
79568591e36SMatthias Ringwald         att_server_request_can_send_now(hci_connection);
7963deb3ec6SMatthias Ringwald         return;
7973deb3ec6SMatthias Ringwald     }
7983deb3ec6SMatthias Ringwald 
7996428eb5aSMatthias Ringwald     // directly process command
8006428eb5aSMatthias Ringwald     // note: signed write cannot be handled directly as authentication needs to be verified
8016428eb5aSMatthias Ringwald     if (packet[0] == ATT_WRITE_COMMAND){
802b6df12b6SMatthias Ringwald         att_handle_request(att_connection, packet, size, NULL);
8036428eb5aSMatthias Ringwald         return;
8046428eb5aSMatthias Ringwald     }
8056428eb5aSMatthias Ringwald 
8063deb3ec6SMatthias Ringwald     // check size
80718707219SMatthias Ringwald     if (size > sizeof(att_server->request_buffer)) {
808e0463bdcSMatthias 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));
8096428eb5aSMatthias Ringwald         return;
8106428eb5aSMatthias Ringwald     }
8113deb3ec6SMatthias Ringwald 
812e0463bdcSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
813e0463bdcSMatthias Ringwald     // abort signed write validation if a new request comes in (but finish previous signed write if possible)
814e0463bdcSMatthias Ringwald     if (att_server->state == ATT_SERVER_W4_SIGNED_WRITE_VALIDATION){
815e0463bdcSMatthias Ringwald         if (packet[0] == ATT_SIGNED_WRITE_COMMAND){
816e0463bdcSMatthias Ringwald             log_info("skip new signed write request as previous is in validation");
817e0463bdcSMatthias Ringwald             return;
818e0463bdcSMatthias Ringwald         } else {
819e0463bdcSMatthias Ringwald             log_info("abort signed write validation to process new request");
820e0463bdcSMatthias Ringwald             att_server->state = ATT_SERVER_IDLE;
821e0463bdcSMatthias Ringwald         }
822e0463bdcSMatthias Ringwald     }
823e0463bdcSMatthias Ringwald #endif
8243deb3ec6SMatthias Ringwald     // last request still in processing?
82518707219SMatthias Ringwald     if (att_server->state != ATT_SERVER_IDLE){
826e0463bdcSMatthias Ringwald         log_info("skip att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state);
8276428eb5aSMatthias Ringwald         return;
8286428eb5aSMatthias Ringwald     }
8293deb3ec6SMatthias Ringwald 
8303deb3ec6SMatthias Ringwald     // store request
83118707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED;
83218707219SMatthias Ringwald     att_server->request_size = size;
8336535961aSMatthias Ringwald     (void)memcpy(att_server->request_buffer, packet, size);
8343deb3ec6SMatthias Ringwald 
83568591e36SMatthias Ringwald     att_run_for_context(hci_connection);
83641772f37SMatthias Ringwald }
83741772f37SMatthias Ringwald 
83841772f37SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
83968591e36SMatthias Ringwald     hci_connection_t * hci_connection;
840b6df12b6SMatthias Ringwald     att_connection_t * att_connection;
84141772f37SMatthias Ringwald 
84241772f37SMatthias Ringwald     switch (packet_type){
84341772f37SMatthias Ringwald         case HCI_EVENT_PACKET:
84441772f37SMatthias Ringwald             switch (packet[0]){
84541772f37SMatthias Ringwald                 case L2CAP_EVENT_CAN_SEND_NOW:
84641772f37SMatthias Ringwald                     att_server_handle_can_send_now();
84741772f37SMatthias Ringwald                     break;
84841772f37SMatthias Ringwald                 case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
84941772f37SMatthias Ringwald                     // GATT client has negotiated the mtu for this connection
85068591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(handle);
85168591e36SMatthias Ringwald                     if (!hci_connection) break;
852388fa7c4SMatthias Ringwald                     att_connection = &hci_connection->att_connection;
853b6df12b6SMatthias Ringwald                     att_connection->mtu = little_endian_read_16(packet, 4);
85441772f37SMatthias Ringwald                     break;
85541772f37SMatthias Ringwald                 default:
85641772f37SMatthias Ringwald                     break;
85741772f37SMatthias Ringwald             }
85841772f37SMatthias Ringwald             break;
85941772f37SMatthias Ringwald 
86041772f37SMatthias Ringwald         case ATT_DATA_PACKET:
86141772f37SMatthias Ringwald             log_debug("ATT Packet, handle 0x%04x", handle);
86268591e36SMatthias Ringwald             hci_connection = hci_connection_for_handle(handle);
86368591e36SMatthias Ringwald             if (!hci_connection) break;
86441772f37SMatthias Ringwald 
86568591e36SMatthias Ringwald             att_server_handle_att_pdu(hci_connection, packet, size);
866372d62c4SMatthias Ringwald             break;
8677bbeb3adSMilanka Ringwald 
8687bbeb3adSMilanka Ringwald         default:
8697bbeb3adSMilanka Ringwald             break;
870372d62c4SMatthias Ringwald     }
8713deb3ec6SMatthias Ringwald }
8723deb3ec6SMatthias Ringwald 
873bf78aac6SMatthias Ringwald // ---------------------
874bf78aac6SMatthias Ringwald // persistent CCC writes
875bf78aac6SMatthias Ringwald static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){
8764ea43905SMatthias Ringwald     return ('B' << 24u) | ('T' << 16u) | ('C' << 8u) | index;
877bf78aac6SMatthias Ringwald }
878bf78aac6SMatthias Ringwald 
879bf78aac6SMatthias Ringwald static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){
880bf78aac6SMatthias Ringwald     // lookup att_server instance
88168591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
88268591e36SMatthias Ringwald     if (!hci_connection) return;
88368591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
884bf78aac6SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
885bf78aac6SMatthias 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);
8866a540790SMatthias Ringwald 
887bf78aac6SMatthias Ringwald     // check if bonded
888bf78aac6SMatthias Ringwald     if (le_device_index < 0) return;
8896a540790SMatthias Ringwald 
890bf78aac6SMatthias Ringwald     // get btstack_tlv
891bf78aac6SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
892bf78aac6SMatthias Ringwald     void * tlv_context;
893bf78aac6SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
894bf78aac6SMatthias Ringwald     if (!tlv_impl) return;
8956a540790SMatthias Ringwald 
896bf78aac6SMatthias Ringwald     // update ccc tag
897bf78aac6SMatthias Ringwald     int index;
8986a540790SMatthias Ringwald     uint32_t highest_seq_nr = 0;
8996a540790SMatthias Ringwald     uint32_t lowest_seq_nr = 0;
9006a540790SMatthias Ringwald     uint32_t tag_for_lowest_seq_nr = 0;
9016a540790SMatthias Ringwald     uint32_t tag_for_empty = 0;
9026a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
9036afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
904bf78aac6SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
9056a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
9066a540790SMatthias Ringwald 
9076a540790SMatthias Ringwald         // empty/invalid tag
9086a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)){
9096a540790SMatthias Ringwald             tag_for_empty = tag;
910bf78aac6SMatthias Ringwald             continue;
911bf78aac6SMatthias Ringwald         }
9126a540790SMatthias Ringwald         // update highest seq nr
9136a540790SMatthias Ringwald         if (entry.seq_nr > highest_seq_nr){
9146a540790SMatthias Ringwald             highest_seq_nr = entry.seq_nr;
9156a540790SMatthias Ringwald         }
9166a540790SMatthias Ringwald         // find entry with lowest seq nr
9174ea43905SMatthias Ringwald         if ((tag_for_lowest_seq_nr == 0u) || (entry.seq_nr < lowest_seq_nr)){
9186a540790SMatthias Ringwald             tag_for_lowest_seq_nr = tag;
9196a540790SMatthias Ringwald             lowest_seq_nr = entry.seq_nr;
9206a540790SMatthias Ringwald         }
9216a540790SMatthias Ringwald 
9226a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
9236a540790SMatthias Ringwald         if (entry.att_handle   != att_handle)      continue;
9246a540790SMatthias Ringwald 
9256a540790SMatthias Ringwald         // found matching entry
9269305033eSMatthias Ringwald         if (value != 0){
927bf78aac6SMatthias Ringwald             // update
9286a540790SMatthias Ringwald             if (entry.value == value) {
929760ad805SMatthias Ringwald                 log_info("CCC Index %u: Up-to-date", index);
930760ad805SMatthias Ringwald                 return;
931760ad805SMatthias Ringwald             }
9326a540790SMatthias Ringwald             entry.value = value;
9334ea43905SMatthias Ringwald             entry.seq_nr = highest_seq_nr + 1u;
934760ad805SMatthias Ringwald             log_info("CCC Index %u: Store", index);
9350fe46bc1SMatthias Ringwald             int result = tlv_impl->store_tag(tlv_context, tag, (const uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
9360fe46bc1SMatthias Ringwald             if (result != 0){
9370fe46bc1SMatthias Ringwald                 log_error("Store tag index %u failed", index);
9380fe46bc1SMatthias Ringwald             }
93901ac2008SMatthias Ringwald         } else {
94001ac2008SMatthias Ringwald             // delete
941760ad805SMatthias Ringwald             log_info("CCC Index %u: Delete", index);
94201ac2008SMatthias Ringwald             tlv_impl->delete_tag(tlv_context, tag);
94301ac2008SMatthias Ringwald         }
944bf78aac6SMatthias Ringwald         return;
945bf78aac6SMatthias Ringwald     }
9466a540790SMatthias Ringwald 
9470fe46bc1SMatthias Ringwald     log_info("tag_for_empty %"PRIx32", tag_for_lowest_seq_nr %"PRIx32, tag_for_empty, tag_for_lowest_seq_nr);
9486a540790SMatthias Ringwald 
9494ea43905SMatthias Ringwald     if (value == 0u){
9506a540790SMatthias Ringwald         // done
9516a540790SMatthias Ringwald         return;
9526a540790SMatthias Ringwald     }
9536a540790SMatthias Ringwald 
9546a540790SMatthias Ringwald     uint32_t tag_to_use = 0;
9559305033eSMatthias Ringwald     if (tag_for_empty != 0){
9566a540790SMatthias Ringwald         tag_to_use = tag_for_empty;
9576a540790SMatthias Ringwald     } else if (tag_for_lowest_seq_nr){
9586a540790SMatthias Ringwald         tag_to_use = tag_for_lowest_seq_nr;
9596a540790SMatthias Ringwald     } else {
9606a540790SMatthias Ringwald         // should not happen
9616a540790SMatthias Ringwald         return;
9626a540790SMatthias Ringwald     }
963bf78aac6SMatthias Ringwald     // store ccc tag
9644ea43905SMatthias Ringwald     entry.seq_nr       = highest_seq_nr + 1u;
9656a540790SMatthias Ringwald     entry.device_index = le_device_index;
9666a540790SMatthias Ringwald     entry.att_handle   = att_handle;
9676a540790SMatthias Ringwald     entry.value        = value;
9680fe46bc1SMatthias Ringwald     int result = tlv_impl->store_tag(tlv_context, tag_to_use, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
9690fe46bc1SMatthias Ringwald     if (result != 0){
9700fe46bc1SMatthias Ringwald         log_error("Store tag index %u failed", index);
9710fe46bc1SMatthias Ringwald     }
972bf78aac6SMatthias Ringwald }
973bf78aac6SMatthias Ringwald 
97468591e36SMatthias Ringwald static void att_server_persistent_ccc_clear(hci_connection_t * hci_connection){
97568591e36SMatthias Ringwald     if (!hci_connection) return;
97668591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
97768591e36SMatthias Ringwald 
978760ad805SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
979760ad805SMatthias Ringwald     log_info("Clear CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
980760ad805SMatthias Ringwald     // check if bonded
981760ad805SMatthias Ringwald     if (le_device_index < 0) return;
982760ad805SMatthias Ringwald     // get btstack_tlv
983760ad805SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
984760ad805SMatthias Ringwald     void * tlv_context;
985760ad805SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
986760ad805SMatthias Ringwald     if (!tlv_impl) return;
987760ad805SMatthias Ringwald     // get all ccc tag
988760ad805SMatthias Ringwald     int index;
9896a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
9906afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
991760ad805SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
9926a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
9936a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
9946a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
995760ad805SMatthias Ringwald         // delete entry
996760ad805SMatthias Ringwald         log_info("CCC Index %u: Delete", index);
997760ad805SMatthias Ringwald         tlv_impl->delete_tag(tlv_context, tag);
998760ad805SMatthias Ringwald     }
999760ad805SMatthias Ringwald }
1000760ad805SMatthias Ringwald 
100168591e36SMatthias Ringwald static void att_server_persistent_ccc_restore(hci_connection_t * hci_connection){
100268591e36SMatthias Ringwald     if (!hci_connection) return;
100368591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
1004388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
100568591e36SMatthias Ringwald 
100601ac2008SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
100701ac2008SMatthias Ringwald     log_info("Restore CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
10081b7acd0dSMatthias Ringwald     // check if bonded
10091b7acd0dSMatthias Ringwald     if (le_device_index < 0) return;
101001ac2008SMatthias Ringwald     // get btstack_tlv
101101ac2008SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
101201ac2008SMatthias Ringwald     void * tlv_context;
101301ac2008SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
101401ac2008SMatthias Ringwald     if (!tlv_impl) return;
101501ac2008SMatthias Ringwald     // get all ccc tag
101601ac2008SMatthias Ringwald     int index;
10176a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
10186afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
101901ac2008SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
10206a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
10216a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
10226a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
102301ac2008SMatthias Ringwald         // simulate write callback
10246a540790SMatthias Ringwald         uint16_t attribute_handle = entry.att_handle;
102501ac2008SMatthias Ringwald         uint8_t  value[2];
10266a540790SMatthias Ringwald         little_endian_store_16(value, 0, entry.value);
102701ac2008SMatthias Ringwald         att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
102801ac2008SMatthias Ringwald         if (!callback) continue;
10296a540790SMatthias Ringwald         log_info("CCC Index %u: Set Attribute handle 0x%04x to value 0x%04x", index, attribute_handle, entry.value );
1030b6df12b6SMatthias Ringwald         (*callback)(att_connection->con_handle, attribute_handle, ATT_TRANSACTION_MODE_NONE, 0, value, sizeof(value));
103101ac2008SMatthias Ringwald     }
103201ac2008SMatthias Ringwald }
103301ac2008SMatthias Ringwald 
1034bf78aac6SMatthias Ringwald // persistent CCC writes
1035bf78aac6SMatthias Ringwald // ---------------------
10363d71c7a4SMatthias Ringwald 
10373d71c7a4SMatthias Ringwald // gatt service management
10383d71c7a4SMatthias Ringwald static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){
10393d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
10403d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
10413d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
10423d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
10433d71c7a4SMatthias Ringwald         if (handler->start_handle > handle) continue;
10443d71c7a4SMatthias Ringwald         if (handler->end_handle   < handle) continue;
10453d71c7a4SMatthias Ringwald         return handler;
10463d71c7a4SMatthias Ringwald     }
10473d71c7a4SMatthias Ringwald     return NULL;
10483d71c7a4SMatthias Ringwald }
10493d71c7a4SMatthias Ringwald static att_read_callback_t att_server_read_callback_for_handle(uint16_t handle){
10503d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
10519305033eSMatthias Ringwald     if (handler != NULL) return handler->read_callback;
10523d71c7a4SMatthias Ringwald     return att_server_client_read_callback;
10533d71c7a4SMatthias Ringwald }
10543d71c7a4SMatthias Ringwald 
10553d71c7a4SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){
10563d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
10579305033eSMatthias Ringwald     if (handler != NULL) return handler->write_callback;
10583d71c7a4SMatthias Ringwald     return att_server_client_write_callback;
10593d71c7a4SMatthias Ringwald }
10603d71c7a4SMatthias Ringwald 
10615d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle){
10625d07396bSMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
10639305033eSMatthias Ringwald     if (handler != NULL) return handler->packet_handler;
10645d07396bSMatthias Ringwald     return att_client_packet_handler;
10655d07396bSMatthias Ringwald }
10665d07396bSMatthias Ringwald 
10673d71c7a4SMatthias Ringwald static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){
10683d71c7a4SMatthias Ringwald     // notify all callbacks
10693d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
10703d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
10713d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
10723d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
10733d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
10743d71c7a4SMatthias Ringwald         (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
10753d71c7a4SMatthias Ringwald     }
10763d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return;
10773d71c7a4SMatthias Ringwald     (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
10783d71c7a4SMatthias Ringwald }
10793d71c7a4SMatthias Ringwald 
10803d71c7a4SMatthias Ringwald // returns first reported error or 0
10813d71c7a4SMatthias Ringwald static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){
10823d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
10833d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
10843d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
10853d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
10863d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
10873d71c7a4SMatthias Ringwald         uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
10889305033eSMatthias Ringwald         if (error_code != 0) return error_code;
10893d71c7a4SMatthias Ringwald     }
10903d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return 0;
10913d71c7a4SMatthias Ringwald     return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
10923d71c7a4SMatthias Ringwald }
10933d71c7a4SMatthias Ringwald 
10943d71c7a4SMatthias 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){
10953d71c7a4SMatthias Ringwald     att_read_callback_t callback = att_server_read_callback_for_handle(attribute_handle);
109601ac2008SMatthias Ringwald     if (!callback) return 0;
10973d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, offset, buffer, buffer_size);
10983d71c7a4SMatthias Ringwald }
10993d71c7a4SMatthias Ringwald 
11003d71c7a4SMatthias 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){
11013d71c7a4SMatthias Ringwald     switch (transaction_mode){
11023d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_VALIDATE:
11033d71c7a4SMatthias Ringwald             return att_validate_prepared_write(con_handle);
11043d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_EXECUTE:
11053d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_CANCEL:
11063d71c7a4SMatthias Ringwald             att_notify_write_callbacks(con_handle, transaction_mode);
11073d71c7a4SMatthias Ringwald             return 0;
11083d71c7a4SMatthias Ringwald         default:
11093d71c7a4SMatthias Ringwald             break;
11103d71c7a4SMatthias Ringwald     }
11113d71c7a4SMatthias Ringwald 
1112bf78aac6SMatthias Ringwald     // track CCC writes
11134ea43905SMatthias Ringwald     if (att_is_persistent_ccc(attribute_handle) && (offset == 0u) && (buffer_size == 2u)){
1114bf78aac6SMatthias Ringwald         att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0));
1115bf78aac6SMatthias Ringwald     }
1116bf78aac6SMatthias Ringwald 
111701ac2008SMatthias Ringwald     att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
111801ac2008SMatthias Ringwald     if (!callback) return 0;
11193d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size);
11203d71c7a4SMatthias Ringwald }
11213d71c7a4SMatthias Ringwald 
11223d71c7a4SMatthias Ringwald /**
11233d71c7a4SMatthias Ringwald  * @brief register read/write callbacks for specific handle range
11243d71c7a4SMatthias Ringwald  * @param att_service_handler_t
11253d71c7a4SMatthias Ringwald  */
11263d71c7a4SMatthias Ringwald void att_server_register_service_handler(att_service_handler_t * handler){
11273d71c7a4SMatthias Ringwald     if (att_service_handler_for_handle(handler->start_handle) ||
11283d71c7a4SMatthias Ringwald         att_service_handler_for_handle(handler->end_handle)){
11293d71c7a4SMatthias Ringwald         log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle);
11303d71c7a4SMatthias Ringwald         return;
11313d71c7a4SMatthias Ringwald     }
11323d71c7a4SMatthias Ringwald     btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler);
11333d71c7a4SMatthias Ringwald }
11343d71c7a4SMatthias Ringwald 
11353deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){
11363deb3ec6SMatthias Ringwald 
11373d71c7a4SMatthias Ringwald     // store callbacks
11383d71c7a4SMatthias Ringwald     att_server_client_read_callback  = read_callback;
11393d71c7a4SMatthias Ringwald     att_server_client_write_callback = write_callback;
11403d71c7a4SMatthias Ringwald 
11411a389cdcSMatthias Ringwald     // register for HCI Events
1142d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &att_event_packet_handler;
11431a389cdcSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
11441a389cdcSMatthias Ringwald 
1145406722e4SMatthias Ringwald     // register for SM events
1146d9a7306aSMatthias Ringwald     sm_event_callback_registration.callback = &att_event_packet_handler;
1147406722e4SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
11483deb3ec6SMatthias Ringwald 
11491a389cdcSMatthias Ringwald     // and L2CAP ATT Server PDUs
11503deb3ec6SMatthias Ringwald     att_dispatch_register_server(att_packet_handler);
11513deb3ec6SMatthias Ringwald 
115270dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
115370dca4d4SMatthias Ringwald     // setup l2cap service
115470dca4d4SMatthias Ringwald     l2cap_register_service(&att_event_packet_handler, PSM_ATT, 0xffff, LEVEL_2);
115570dca4d4SMatthias Ringwald #endif
115670dca4d4SMatthias Ringwald 
11573deb3ec6SMatthias Ringwald     att_set_db(db);
11583d71c7a4SMatthias Ringwald     att_set_read_callback(att_server_read_callback);
11593d71c7a4SMatthias Ringwald     att_set_write_callback(att_server_write_callback);
11603deb3ec6SMatthias Ringwald }
11613deb3ec6SMatthias Ringwald 
11623deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){
11633deb3ec6SMatthias Ringwald     att_client_packet_handler = handler;
11643deb3ec6SMatthias Ringwald }
11653deb3ec6SMatthias Ringwald 
1166cbbb12d9SMatthias Ringwald 
1167cbbb12d9SMatthias Ringwald // to be deprecated
1168c141988cSMatthias Ringwald int  att_server_can_send_packet_now(hci_con_handle_t con_handle){
116968591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
117068591e36SMatthias Ringwald     if (!hci_connection) return 0;
117168591e36SMatthias Ringwald     return att_server_can_send_packet(hci_connection);
11721b96b007SMatthias Ringwald }
11731b96b007SMatthias Ringwald 
1174cbbb12d9SMatthias Ringwald int att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
1175cbbb12d9SMatthias Ringwald     return att_server_request_to_send_notification(callback_registration, con_handle);
11763deb3ec6SMatthias Ringwald }
11773deb3ec6SMatthias Ringwald 
1178cbbb12d9SMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){
1179cbbb12d9SMatthias Ringwald     att_client_waiting_for_can_send_registration.callback = &att_emit_can_send_now_event;
1180cbbb12d9SMatthias Ringwald     att_server_request_to_send_notification(&att_client_waiting_for_can_send_registration, con_handle);
1181cbbb12d9SMatthias Ringwald }
1182cbbb12d9SMatthias Ringwald // end of deprecated
1183cbbb12d9SMatthias Ringwald 
1184cbbb12d9SMatthias Ringwald int att_server_request_to_send_notification(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
118568591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
118668591e36SMatthias Ringwald     if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
118768591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
1188c37df6f6SMatthias Ringwald     bool added = btstack_linked_list_add_tail(&att_server->notification_requests, (btstack_linked_item_t*) callback_registration);
118968591e36SMatthias Ringwald     att_server_request_can_send_now(hci_connection);
1190c37df6f6SMatthias Ringwald     if (added){
1191cbbb12d9SMatthias Ringwald         return ERROR_CODE_SUCCESS;
1192c37df6f6SMatthias Ringwald     } else {
1193c37df6f6SMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1194c37df6f6SMatthias Ringwald     }
1195cbbb12d9SMatthias Ringwald }
1196cbbb12d9SMatthias Ringwald 
1197cbbb12d9SMatthias Ringwald int att_server_request_to_send_indication(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
119868591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
119968591e36SMatthias Ringwald     if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
120068591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
1201c37df6f6SMatthias Ringwald     bool added = btstack_linked_list_add_tail(&att_server->indication_requests, (btstack_linked_item_t*) callback_registration);
120268591e36SMatthias Ringwald     att_server_request_can_send_now(hci_connection);
1203c37df6f6SMatthias Ringwald     if (added){
1204969a5bbaSMatthias Ringwald         return ERROR_CODE_SUCCESS;
1205c37df6f6SMatthias Ringwald     } else {
1206c37df6f6SMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1207c37df6f6SMatthias Ringwald     }
1208bb38f057SMatthias Ringwald }
1209bb38f057SMatthias Ringwald 
12103bb3035fSMilanka Ringwald int att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){
121168591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
121268591e36SMatthias Ringwald     if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1213388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
121468591e36SMatthias Ringwald 
121568591e36SMatthias Ringwald     if (!att_server_can_send_packet(hci_connection)) return BTSTACK_ACL_BUFFERS_FULL;
12163deb3ec6SMatthias Ringwald 
12173deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
12183deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
1219b6df12b6SMatthias Ringwald     uint16_t size = att_prepare_handle_value_notification(att_connection, attribute_handle, value, value_len, packet_buffer);
1220b6df12b6SMatthias Ringwald 	return l2cap_send_prepared_connectionless(att_connection->con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
12213deb3ec6SMatthias Ringwald }
12223deb3ec6SMatthias Ringwald 
12233bb3035fSMilanka Ringwald int att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){
122468591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
122568591e36SMatthias Ringwald     if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
122668591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
1227388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
122818707219SMatthias Ringwald 
1229eaac31e8SMatthias Ringwald     if (att_server->value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS;
123068591e36SMatthias Ringwald     if (!att_server_can_send_packet(hci_connection)) return BTSTACK_ACL_BUFFERS_FULL;
12313deb3ec6SMatthias Ringwald 
12323deb3ec6SMatthias Ringwald     // track indication
123318707219SMatthias Ringwald     att_server->value_indication_handle = attribute_handle;
123418707219SMatthias Ringwald     btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout);
123518707219SMatthias Ringwald     btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS);
123618707219SMatthias Ringwald     btstack_run_loop_add_timer(&att_server->value_indication_timer);
12373deb3ec6SMatthias Ringwald 
12383deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
12393deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
1240b6df12b6SMatthias Ringwald     uint16_t size = att_prepare_handle_value_indication(att_connection, attribute_handle, value, value_len, packet_buffer);
1241b6df12b6SMatthias Ringwald 	l2cap_send_prepared_connectionless(att_connection->con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
12423deb3ec6SMatthias Ringwald     return 0;
12433deb3ec6SMatthias Ringwald }
12448f9132b5SMilanka Ringwald 
12458f9132b5SMilanka Ringwald uint16_t att_server_get_mtu(hci_con_handle_t con_handle){
124668591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
124768591e36SMatthias Ringwald     if (!hci_connection) return 0;
1248388fa7c4SMatthias Ringwald     att_connection_t * att_connection = &hci_connection->att_connection;
1249b6df12b6SMatthias Ringwald     return att_connection->mtu;
12508f9132b5SMilanka Ringwald }
1251