xref: /btstack/src/ble/att_server.c (revision 68591e36e97ed1ccb3a86672f9f79114713a1e1c)
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 
74*68591e36SMatthias 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);
78*68591e36SMatthias Ringwald static void att_server_persistent_ccc_restore(hci_connection_t * hci_connection);
79*68591e36SMatthias Ringwald static void att_server_persistent_ccc_clear(hci_connection_t * hci_connection);
80*68591e36SMatthias 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
110*68591e36SMatthias 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;
116*68591e36SMatthias Ringwald         if (att_server->state == state) return connection;
11718707219SMatthias Ringwald     }
11818707219SMatthias Ringwald     return NULL;
11918707219SMatthias Ringwald }
12002b78fc8SMatthias Ringwald #endif
121bb38f057SMatthias Ringwald 
122*68591e36SMatthias Ringwald static void att_server_request_can_send_now(hci_connection_t * hci_connection){
123*68591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
124c8c6e9b4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
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
130c8c6e9b4SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
131c8c6e9b4SMatthias Ringwald }
132c8c6e9b4SMatthias Ringwald 
133*68591e36SMatthias Ringwald static int att_server_can_send_packet(hci_connection_t * hci_connection){
134*68591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
135c0f26e1cSMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
136c0f26e1cSMatthias Ringwald     if (att_server->l2cap_cid != 0){
137c0f26e1cSMatthias Ringwald         return l2cap_can_send_packet_now(att_server->l2cap_cid);
138c0f26e1cSMatthias Ringwald     }
139c0f26e1cSMatthias Ringwald #endif
140c0f26e1cSMatthias Ringwald     return att_dispatch_server_can_send_now(att_server->connection.con_handle);
141c0f26e1cSMatthias Ringwald }
142c0f26e1cSMatthias Ringwald 
1433deb3ec6SMatthias Ringwald static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){
1445d07396bSMatthias Ringwald     btstack_packet_handler_t packet_handler = att_server_packet_handler_for_handle(attribute_handle);
1455d07396bSMatthias Ringwald     if (!packet_handler) return;
1463deb3ec6SMatthias Ringwald 
1473deb3ec6SMatthias Ringwald     uint8_t event[7];
1483deb3ec6SMatthias Ringwald     int pos = 0;
1495611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE;
1504ea43905SMatthias Ringwald     event[pos++] = sizeof(event) - 2u;
1513deb3ec6SMatthias Ringwald     event[pos++] = status;
152f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, client_handle);
1533deb3ec6SMatthias Ringwald     pos += 2;
154f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, attribute_handle);
155a444112bSMatthias Ringwald     (*packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1563deb3ec6SMatthias Ringwald }
1573deb3ec6SMatthias Ringwald 
1585d07396bSMatthias Ringwald static void att_emit_event_to_all(const uint8_t * event, uint16_t size){
1595d07396bSMatthias Ringwald     // dispatch to app level handler
1605d07396bSMatthias Ringwald     if (att_client_packet_handler){
1615d07396bSMatthias Ringwald         (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size);
1625d07396bSMatthias Ringwald     }
1633deb3ec6SMatthias Ringwald 
1645d07396bSMatthias Ringwald     // dispatch to service handlers
1655d07396bSMatthias Ringwald     btstack_linked_list_iterator_t it;
1665d07396bSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
1675d07396bSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1685d07396bSMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
1695d07396bSMatthias Ringwald         if (!handler->packet_handler) continue;
1705d07396bSMatthias Ringwald         (*handler->packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size);
1715d07396bSMatthias Ringwald     }
1725d07396bSMatthias Ringwald }
1735d07396bSMatthias Ringwald 
1745d07396bSMatthias Ringwald static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){
1753deb3ec6SMatthias Ringwald     uint8_t event[6];
1763deb3ec6SMatthias Ringwald     int pos = 0;
1775611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE;
1784ea43905SMatthias Ringwald     event[pos++] = sizeof(event) - 2u;
179fc64f94aSMatthias Ringwald     little_endian_store_16(event, pos, con_handle);
1803deb3ec6SMatthias Ringwald     pos += 2;
181f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, mtu);
1825d07396bSMatthias Ringwald 
1835d07396bSMatthias Ringwald     // also dispatch to GATT Clients
184b12646c5SJakob Krantz     att_dispatch_server_mtu_exchanged(con_handle, mtu);
1855d07396bSMatthias Ringwald 
1865d07396bSMatthias Ringwald     // dispatch to app level handler and service handlers
1875d07396bSMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
1883deb3ec6SMatthias Ringwald }
1893deb3ec6SMatthias Ringwald 
1905f141511SMatthias Ringwald static void att_emit_can_send_now_event(void * context){
1915f141511SMatthias Ringwald     UNUSED(context);
1923c97b242SMatthias Ringwald     if (!att_client_packet_handler) return;
1933c97b242SMatthias Ringwald 
1941b96b007SMatthias Ringwald     uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0};
1951b96b007SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1961b96b007SMatthias Ringwald }
1971b96b007SMatthias Ringwald 
198*68591e36SMatthias Ringwald static void att_emit_connected_event(hci_connection_t * hci_connection){
199*68591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
200ff1bec95SMatthias Ringwald     uint8_t event[11];
201ff1bec95SMatthias Ringwald     int pos = 0;
202ff1bec95SMatthias Ringwald     event[pos++] = ATT_EVENT_CONNECTED;
2034ea43905SMatthias Ringwald     event[pos++] = sizeof(event) - 2u;
204ff1bec95SMatthias Ringwald     event[pos++] = att_server->peer_addr_type;
205ff1bec95SMatthias Ringwald     reverse_bd_addr(att_server->peer_address, &event[pos]);
206ff1bec95SMatthias Ringwald     pos += 6;
207ff1bec95SMatthias Ringwald     little_endian_store_16(event, pos, att_server->connection.con_handle);
208ff1bec95SMatthias Ringwald     pos += 2;
209ff1bec95SMatthias Ringwald 
210ff1bec95SMatthias Ringwald     // dispatch to app level handler and service handlers
211ff1bec95SMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
212ff1bec95SMatthias Ringwald }
213ff1bec95SMatthias Ringwald 
214ff1bec95SMatthias Ringwald 
2156187ad4cSMatthias Ringwald static void att_emit_disconnected_event(uint16_t con_handle){
216ff1bec95SMatthias Ringwald     uint8_t event[4];
217ff1bec95SMatthias Ringwald     int pos = 0;
218ff1bec95SMatthias Ringwald     event[pos++] = ATT_EVENT_DISCONNECTED;
2194ea43905SMatthias Ringwald     event[pos++] = sizeof(event) - 2u;
2206187ad4cSMatthias Ringwald     little_endian_store_16(event, pos, con_handle);
221ff1bec95SMatthias Ringwald     pos += 2;
222ff1bec95SMatthias Ringwald 
223ff1bec95SMatthias Ringwald     // dispatch to app level handler and service handlers
224ff1bec95SMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
225ff1bec95SMatthias Ringwald }
226ff1bec95SMatthias Ringwald 
227ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){
22854178543SMatthias Ringwald     void * context = btstack_run_loop_get_timer_context(ts);
22954178543SMatthias Ringwald     hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
230*68591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
231*68591e36SMatthias Ringwald     if (!hci_connection) return;
232*68591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
233*68591e36SMatthias Ringwald     // @note: after a transaction timeout, no more requests shall be sent over this ATT Bearer
234bce48a26SMatthias Ringwald     // (that's why we don't reset the value_indication_handle)
23518707219SMatthias Ringwald     uint16_t att_handle = att_server->value_indication_handle;
23618707219SMatthias Ringwald     att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_server->connection.con_handle, att_handle);
2373deb3ec6SMatthias Ringwald }
2383deb3ec6SMatthias Ringwald 
2393deb3ec6SMatthias Ringwald static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2409ec2630cSMatthias Ringwald 
2417dad9ff8SMatthias Ringwald     UNUSED(channel); // ok: there is no channel
2427dad9ff8SMatthias Ringwald     UNUSED(size);    // ok: handling own l2cap events
2433deb3ec6SMatthias Ringwald 
24418707219SMatthias Ringwald     att_server_t * att_server;
24518707219SMatthias Ringwald     hci_con_handle_t con_handle;
246*68591e36SMatthias Ringwald     hci_connection_t * hci_connection;
24770dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
24870dca4d4SMatthias Ringwald     bd_addr_t address;
249*68591e36SMatthias Ringwald     btstack_linked_list_iterator_t it;
25070dca4d4SMatthias Ringwald #endif
25118707219SMatthias Ringwald 
2523deb3ec6SMatthias Ringwald     switch (packet_type) {
2533deb3ec6SMatthias Ringwald 
2543deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
2550e2df43fSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
2563deb3ec6SMatthias Ringwald 
25770dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
25870dca4d4SMatthias Ringwald                 case L2CAP_EVENT_INCOMING_CONNECTION:
25970dca4d4SMatthias Ringwald                     l2cap_event_incoming_connection_get_address(packet, address);
26070dca4d4SMatthias Ringwald                     l2cap_accept_connection(channel);
26136de8547SMatthias Ringwald                     log_info("Accept incoming connection from %s", bd_addr_to_str(address));
26270dca4d4SMatthias Ringwald                     break;
26370dca4d4SMatthias Ringwald                 case L2CAP_EVENT_CHANNEL_OPENED:
264122b723eSMatthias Ringwald                     con_handle = l2cap_event_channel_opened_get_handle(packet);
265*68591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
266*68591e36SMatthias Ringwald                     if (!hci_connection) break;
267*68591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
268122b723eSMatthias Ringwald                     // store connection info
269f16129ceSMatthias Ringwald                     att_server->peer_addr_type = BD_ADDR_TYPE_ACL;
270122b723eSMatthias Ringwald                     l2cap_event_channel_opened_get_address(packet, att_server->peer_address);
271122b723eSMatthias Ringwald                     att_server->connection.con_handle = con_handle;
27270dca4d4SMatthias Ringwald                     att_server->l2cap_cid = l2cap_event_channel_opened_get_local_cid(packet);
273122b723eSMatthias Ringwald                     // reset connection properties
274122b723eSMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
275122b723eSMatthias Ringwald                     att_server->connection.mtu = l2cap_event_channel_opened_get_remote_mtu(packet);
276122b723eSMatthias Ringwald                     att_server->connection.max_mtu = l2cap_max_mtu();
277122b723eSMatthias Ringwald                     if (att_server->connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){
278122b723eSMatthias Ringwald                         att_server->connection.max_mtu = ATT_REQUEST_BUFFER_SIZE;
279122b723eSMatthias Ringwald                     }
280df33ee14SMatthias Ringwald 
281df33ee14SMatthias Ringwald                     log_info("Connection opened %s, l2cap cid %04x, mtu %u", bd_addr_to_str(address), att_server->l2cap_cid, att_server->connection.mtu);
282df33ee14SMatthias Ringwald 
283914988a1SMatthias Ringwald                     // update security params
284914988a1SMatthias Ringwald                     att_server->connection.encryption_key_size = gap_encryption_key_size(con_handle);
285914988a1SMatthias Ringwald                     att_server->connection.authenticated = gap_authenticated(con_handle);
286914988a1SMatthias Ringwald                     att_server->connection.secure_connection = gap_secure_connection(con_handle);
287914988a1SMatthias Ringwald                     log_info("encrypted key size %u, authenticated %u, secure connection %u",
288914988a1SMatthias Ringwald                         att_server->connection.encryption_key_size, att_server->connection.authenticated, att_server->connection.secure_connection);
289df33ee14SMatthias Ringwald 
290df33ee14SMatthias Ringwald                     // notify connection opened
291*68591e36SMatthias Ringwald                     att_emit_connected_event(hci_connection);
292df33ee14SMatthias Ringwald 
293914988a1SMatthias Ringwald                     // restore persisten ccc if encrypted
294914988a1SMatthias Ringwald                     if ( gap_security_level(con_handle) >= LEVEL_2){
295*68591e36SMatthias Ringwald                         att_server_persistent_ccc_restore(hci_connection);
296914988a1SMatthias Ringwald                     }
297122b723eSMatthias Ringwald                     // TODO: what to do about le device db?
298122b723eSMatthias Ringwald                     att_server->pairing_active = 0;
29970dca4d4SMatthias Ringwald                     break;
30048364364SMatthias Ringwald                 case L2CAP_EVENT_CAN_SEND_NOW:
30148364364SMatthias Ringwald                     att_server_handle_can_send_now();
30248364364SMatthias Ringwald                     break;
30348364364SMatthias Ringwald 
30470dca4d4SMatthias Ringwald #endif
3053deb3ec6SMatthias Ringwald                 case HCI_EVENT_LE_META:
3063deb3ec6SMatthias Ringwald                     switch (packet[2]) {
3073deb3ec6SMatthias Ringwald                         case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
30818707219SMatthias Ringwald                             con_handle = little_endian_read_16(packet, 4);
309*68591e36SMatthias Ringwald                             hci_connection = hci_connection_for_handle(con_handle);
310*68591e36SMatthias Ringwald                             if (!hci_connection) break;
311*68591e36SMatthias Ringwald                             att_server = &hci_connection->att_server;
3123deb3ec6SMatthias Ringwald                         	// store connection info
31318707219SMatthias Ringwald                         	att_server->peer_addr_type = packet[7];
31418707219SMatthias Ringwald                             reverse_bd_addr(&packet[8], att_server->peer_address);
31518707219SMatthias Ringwald                             att_server->connection.con_handle = con_handle;
3163deb3ec6SMatthias Ringwald                             // reset connection properties
31718707219SMatthias Ringwald                             att_server->state = ATT_SERVER_IDLE;
31818707219SMatthias Ringwald                             att_server->connection.mtu = ATT_DEFAULT_MTU;
31918707219SMatthias Ringwald                             att_server->connection.max_mtu = l2cap_max_le_mtu();
32018707219SMatthias Ringwald                             if (att_server->connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){
32118707219SMatthias Ringwald                                 att_server->connection.max_mtu = ATT_REQUEST_BUFFER_SIZE;
32299c58ad0SMatthias Ringwald                             }
32318707219SMatthias Ringwald                             att_server->connection.encryption_key_size = 0;
32418707219SMatthias Ringwald                             att_server->connection.authenticated = 0;
32518707219SMatthias Ringwald 		                	att_server->connection.authorized = 0;
326b2c1e52cSMatthias Ringwald                             // workaround: identity resolving can already be complete, at least store result
327b2c1e52cSMatthias Ringwald                             att_server->ir_le_device_db_index = sm_le_device_index(con_handle);
32830c8e114SMatthias Ringwald                             att_server->ir_lookup_active = 0;
329760ad805SMatthias Ringwald                             att_server->pairing_active = 0;
330ff1bec95SMatthias Ringwald                             // notify all - old
3315d07396bSMatthias Ringwald                             att_emit_event_to_all(packet, size);
332ff1bec95SMatthias Ringwald                             // notify all - new
333*68591e36SMatthias Ringwald                             att_emit_connected_event(hci_connection);
3343deb3ec6SMatthias Ringwald                             break;
3353deb3ec6SMatthias Ringwald 
3363deb3ec6SMatthias Ringwald                         default:
3373deb3ec6SMatthias Ringwald                             break;
3383deb3ec6SMatthias Ringwald                     }
3393deb3ec6SMatthias Ringwald                     break;
3403deb3ec6SMatthias Ringwald 
3413deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_CHANGE:
3423deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE:
3433deb3ec6SMatthias Ringwald                 	// check handle
34418707219SMatthias Ringwald                     con_handle = little_endian_read_16(packet, 3);
345*68591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
346*68591e36SMatthias Ringwald                     if (!hci_connection) break;
347914988a1SMatthias Ringwald                     if (gap_get_connection_type(con_handle) != GAP_CONNECTION_LE) break;
348914988a1SMatthias Ringwald                     // update security params
349*68591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
3509c6e867eSMatthias Ringwald                     att_server->connection.encryption_key_size = gap_encryption_key_size(con_handle);
3519c6e867eSMatthias Ringwald                     att_server->connection.authenticated = gap_authenticated(con_handle);
35213eb7322SMatthias Ringwald                     att_server->connection.secure_connection = gap_secure_connection(con_handle);
353914988a1SMatthias Ringwald                     log_info("encrypted key size %u, authenticated %u, secure connection %u",
35413eb7322SMatthias Ringwald                         att_server->connection.encryption_key_size, att_server->connection.authenticated, att_server->connection.secure_connection);
355914988a1SMatthias Ringwald                     if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){
3564097998aSMatthias Ringwald                         // restore CCC values when encrypted for LE Connections
35701ac2008SMatthias Ringwald                         if (hci_event_encryption_change_get_encryption_enabled(packet)){
358*68591e36SMatthias Ringwald                             att_server_persistent_ccc_restore(hci_connection);
35901ac2008SMatthias Ringwald                         }
36001ac2008SMatthias Ringwald                     }
361*68591e36SMatthias Ringwald                     att_run_for_context(hci_connection);
3623deb3ec6SMatthias Ringwald                     break;
3633deb3ec6SMatthias Ringwald 
3643deb3ec6SMatthias Ringwald                 case HCI_EVENT_DISCONNECTION_COMPLETE:
36570a390c7SMatthias Ringwald                     // check handle
36618707219SMatthias Ringwald                     con_handle = hci_event_disconnection_complete_get_connection_handle(packet);
367*68591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
368*68591e36SMatthias Ringwald                     if (!hci_connection) break;
369*68591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
37018707219SMatthias Ringwald                     att_clear_transaction_queue(&att_server->connection);
37118707219SMatthias Ringwald                     att_server->connection.con_handle = 0;
372760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
37318707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
374bce48a26SMatthias Ringwald                     if (att_server->value_indication_handle){
37527328ecbSMatthias Ringwald                         btstack_run_loop_remove_timer(&att_server->value_indication_timer);
376bce48a26SMatthias Ringwald                         uint16_t att_handle = att_server->value_indication_handle;
377bce48a26SMatthias Ringwald                         att_server->value_indication_handle = 0; // reset error state
378bce48a26SMatthias Ringwald                         att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_DISCONNECT, att_server->connection.con_handle, att_handle);
379bce48a26SMatthias Ringwald                     }
380ff1bec95SMatthias Ringwald                     // notify all - new
3816187ad4cSMatthias Ringwald                     att_emit_disconnected_event(con_handle);
382ff1bec95SMatthias Ringwald                     // notify all - old
3835d07396bSMatthias Ringwald                     att_emit_event_to_all(packet, size);
3843deb3ec6SMatthias Ringwald                     break;
3853deb3ec6SMatthias Ringwald 
386760ad805SMatthias Ringwald                 // Identity Resolving
3875611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_STARTED:
38818707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_started_get_handle(packet);
389*68591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
390*68591e36SMatthias Ringwald                     if (!hci_connection) break;
391*68591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
3925611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED");
39330c8e114SMatthias Ringwald                     att_server->ir_lookup_active = 1;
3943deb3ec6SMatthias Ringwald                     break;
3955611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
396760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
397*68591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
398*68591e36SMatthias Ringwald                     if (!hci_connection) return;
399*68591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
40030c8e114SMatthias Ringwald                     att_server->ir_lookup_active = 0;
4011b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index(packet);
4021b7acd0dSMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED");
403*68591e36SMatthias Ringwald                     att_run_for_context(hci_connection);
4043deb3ec6SMatthias Ringwald                     break;
4055611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_FAILED:
40618707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_failed_get_handle(packet);
407*68591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
408*68591e36SMatthias Ringwald                     if (!hci_connection) break;
409*68591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
4105611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED");
41130c8e114SMatthias Ringwald                     att_server->ir_lookup_active = 0;
41218707219SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
413*68591e36SMatthias Ringwald                     att_run_for_context(hci_connection);
4143deb3ec6SMatthias Ringwald                     break;
415760ad805SMatthias Ringwald 
416760ad805SMatthias Ringwald                 // Pairing started - delete stored CCC values
417760ad805SMatthias Ringwald                 // - assumes pairing indicates either new device or re-pairing, in both cases there should be no stored CCC values
418760ad805SMatthias Ringwald                 // - assumes that all events have the con handle as the first field
419760ad805SMatthias Ringwald                 case SM_EVENT_JUST_WORKS_REQUEST:
420760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
421760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_INPUT_NUMBER:
422760ad805SMatthias Ringwald                 case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
423760ad805SMatthias Ringwald                     con_handle = sm_event_just_works_request_get_handle(packet);
424*68591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
425*68591e36SMatthias Ringwald                     if (!hci_connection) break;
426*68591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
427760ad805SMatthias Ringwald                     att_server->pairing_active = 1;
428760ad805SMatthias Ringwald                     log_info("SM Pairing started");
429760ad805SMatthias Ringwald                     if (att_server->ir_le_device_db_index < 0) break;
430*68591e36SMatthias Ringwald                     att_server_persistent_ccc_clear(hci_connection);
431760ad805SMatthias Ringwald                     // index not valid anymore
432760ad805SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
433760ad805SMatthias Ringwald                     break;
434760ad805SMatthias Ringwald 
4351b7acd0dSMatthias Ringwald                 // Bonding completed
436760ad805SMatthias Ringwald                 case SM_EVENT_IDENTITY_CREATED:
437760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
438*68591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
439*68591e36SMatthias Ringwald                     if (!hci_connection) return;
440*68591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
441760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
4421b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_created_get_index(packet);
443*68591e36SMatthias Ringwald                     att_run_for_context(hci_connection);
4441b7acd0dSMatthias Ringwald                     break;
4451b7acd0dSMatthias Ringwald 
4461b7acd0dSMatthias Ringwald                 // Pairing complete (with/without bonding=storing of pairing information)
4471b7acd0dSMatthias Ringwald                 case SM_EVENT_PAIRING_COMPLETE:
4481b7acd0dSMatthias Ringwald                     con_handle = sm_event_pairing_complete_get_handle(packet);
449*68591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
450*68591e36SMatthias Ringwald                     if (!hci_connection) return;
451*68591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
4521b7acd0dSMatthias Ringwald                     att_server->pairing_active = 0;
453*68591e36SMatthias Ringwald                     att_run_for_context(hci_connection);
454760ad805SMatthias Ringwald                     break;
455760ad805SMatthias Ringwald 
456760ad805SMatthias Ringwald                 // Authorization
4575611a760SMatthias Ringwald                 case SM_EVENT_AUTHORIZATION_RESULT: {
45818707219SMatthias Ringwald                     con_handle = sm_event_authorization_result_get_handle(packet);
459*68591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(con_handle);
460*68591e36SMatthias Ringwald                     if (!hci_connection) break;
461*68591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
46218707219SMatthias Ringwald                     att_server->connection.authorized = sm_event_authorization_result_get_authorization_result(packet);
463*68591e36SMatthias Ringwald                     att_server_request_can_send_now(hci_connection);
4643deb3ec6SMatthias Ringwald                 	break;
4653deb3ec6SMatthias Ringwald                 }
4663deb3ec6SMatthias Ringwald                 default:
4673deb3ec6SMatthias Ringwald                     break;
4683deb3ec6SMatthias Ringwald             }
46965b44ffdSMatthias Ringwald             break;
47070dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
47170dca4d4SMatthias Ringwald         case L2CAP_DATA_PACKET:
472*68591e36SMatthias Ringwald             hci_connections_get_iterator(&it);
473*68591e36SMatthias Ringwald             while(btstack_linked_list_iterator_has_next(&it)){
474*68591e36SMatthias Ringwald                 hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
475*68591e36SMatthias Ringwald                 att_server = &connection->att_server;
476*68591e36SMatthias Ringwald                 if (att_server->l2cap_cid == channel) {
477*68591e36SMatthias Ringwald                     att_server_handle_att_pdu(connection, packet, size);
478*68591e36SMatthias Ringwald                     break;
479*68591e36SMatthias Ringwald                 }
480*68591e36SMatthias Ringwald             }
48170dca4d4SMatthias Ringwald             break;
48270dca4d4SMatthias Ringwald #endif
48341772f37SMatthias Ringwald 
48465b44ffdSMatthias Ringwald         default:
48565b44ffdSMatthias Ringwald             break;
4863deb3ec6SMatthias Ringwald     }
4873deb3ec6SMatthias Ringwald }
4883deb3ec6SMatthias Ringwald 
4897a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
4903deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
4913deb3ec6SMatthias Ringwald 
492*68591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION);
493*68591e36SMatthias Ringwald     if (!hci_connection) return;
494*68591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
4953deb3ec6SMatthias Ringwald 
4963deb3ec6SMatthias Ringwald     uint8_t hash_flipped[8];
4979c80e4ccSMatthias Ringwald     reverse_64(hash, hash_flipped);
49818707219SMatthias Ringwald     if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8)){
4993deb3ec6SMatthias Ringwald         log_info("ATT Signed Write, invalid signature");
50018707219SMatthias Ringwald         att_server->state = ATT_SERVER_IDLE;
5013deb3ec6SMatthias Ringwald         return;
5023deb3ec6SMatthias Ringwald     }
5033deb3ec6SMatthias Ringwald     log_info("ATT Signed Write, valid signature");
5043deb3ec6SMatthias Ringwald 
5053deb3ec6SMatthias Ringwald     // update sequence number
50618707219SMatthias Ringwald     uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
50718707219SMatthias Ringwald     le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1);
50818707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
509*68591e36SMatthias Ringwald     att_server_request_can_send_now(hci_connection);
5103deb3ec6SMatthias Ringwald }
5117a766ebfSMatthias Ringwald #endif
51288a48dd8SMatthias Ringwald 
51318707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED
514b06f2579SMatthias Ringwald // pre: can send now
515b06f2579SMatthias Ringwald // returns: 1 if packet was sent
516*68591e36SMatthias Ringwald static int att_server_process_validated_request(hci_connection_t * hci_connection){
517*68591e36SMatthias Ringwald     att_server_t * att_server = & hci_connection->att_server;
518b06f2579SMatthias Ringwald 
519b06f2579SMatthias Ringwald     l2cap_reserve_packet_buffer();
520b06f2579SMatthias Ringwald     uint8_t * att_response_buffer = l2cap_get_outgoing_buffer();
52118707219SMatthias Ringwald     uint16_t  att_response_size   = att_handle_request(&att_server->connection, att_server->request_buffer, att_server->request_size, att_response_buffer);
522b06f2579SMatthias Ringwald 
523beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
524c1ab6cc1SMatthias Ringwald     if ((att_response_size == ATT_READ_RESPONSE_PENDING) || (att_response_size == ATT_INTERNAL_WRITE_RESPONSE_PENDING)){
525e404a688SMatthias Ringwald         // update state
526beb20288SMatthias Ringwald         att_server->state = ATT_SERVER_RESPONSE_PENDING;
527e404a688SMatthias Ringwald 
52856c3a347SMatthias Ringwald         // callback with handle ATT_READ_RESPONSE_PENDING for reads
52956c3a347SMatthias Ringwald         if (att_response_size == ATT_READ_RESPONSE_PENDING){
530e404a688SMatthias Ringwald             att_server_client_read_callback(att_server->connection.con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0);
53156c3a347SMatthias Ringwald         }
532e8e7403eSMatthias Ringwald 
533e8e7403eSMatthias Ringwald         // free reserved buffer
534e8e7403eSMatthias Ringwald         l2cap_release_packet_buffer();
535e8e7403eSMatthias Ringwald         return 0;
536e404a688SMatthias Ringwald     }
537e404a688SMatthias Ringwald #endif
538e404a688SMatthias Ringwald 
539b06f2579SMatthias Ringwald     // intercept "insufficient authorization" for authenticated connections to allow for user authorization
5404ea43905SMatthias Ringwald     if ((att_response_size     >= 4u)
541b06f2579SMatthias Ringwald     && (att_response_buffer[0] == ATT_ERROR_RESPONSE)
542b06f2579SMatthias Ringwald     && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION)
54318707219SMatthias Ringwald     && (att_server->connection.authenticated)){
544b06f2579SMatthias Ringwald 
5459c6e867eSMatthias Ringwald         switch (gap_authorization_state(att_server->connection.con_handle)){
546b06f2579SMatthias Ringwald             case AUTHORIZATION_UNKNOWN:
547b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
54818707219SMatthias Ringwald                 sm_request_pairing(att_server->connection.con_handle);
549b06f2579SMatthias Ringwald                 return 0;
550b06f2579SMatthias Ringwald             case AUTHORIZATION_PENDING:
551b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
552b06f2579SMatthias Ringwald                 return 0;
553b06f2579SMatthias Ringwald             default:
554b06f2579SMatthias Ringwald                 break;
555b06f2579SMatthias Ringwald         }
556b06f2579SMatthias Ringwald     }
557b06f2579SMatthias Ringwald 
55818707219SMatthias Ringwald     att_server->state = ATT_SERVER_IDLE;
5594ea43905SMatthias Ringwald     if (att_response_size == 0u) {
560b06f2579SMatthias Ringwald         l2cap_release_packet_buffer();
561b06f2579SMatthias Ringwald         return 0;
562b06f2579SMatthias Ringwald     }
563b06f2579SMatthias Ringwald 
56448364364SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
56548364364SMatthias Ringwald     if (att_server->l2cap_cid != 0){
56648364364SMatthias Ringwald         l2cap_send_prepared(att_server->l2cap_cid, att_response_size);
56748364364SMatthias Ringwald     } else
56848364364SMatthias Ringwald #endif
56948364364SMatthias Ringwald     {
57018707219SMatthias Ringwald         l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size);
57148364364SMatthias Ringwald     }
572b06f2579SMatthias Ringwald 
573b06f2579SMatthias Ringwald     // notify client about MTU exchange result
574b06f2579SMatthias Ringwald     if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){
57518707219SMatthias Ringwald         att_emit_mtu_event(att_server->connection.con_handle, att_server->connection.mtu);
576b06f2579SMatthias Ringwald     }
577b06f2579SMatthias Ringwald     return 1;
578b06f2579SMatthias Ringwald }
579b06f2579SMatthias Ringwald 
580beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
581beb20288SMatthias Ringwald int att_server_response_ready(hci_con_handle_t con_handle){
582*68591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
583*68591e36SMatthias Ringwald     if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
584*68591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
585*68591e36SMatthias Ringwald 
586beb20288SMatthias Ringwald     if (att_server->state != ATT_SERVER_RESPONSE_PENDING)   return ERROR_CODE_COMMAND_DISALLOWED;
587e404a688SMatthias Ringwald 
588e404a688SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
589*68591e36SMatthias Ringwald     att_server_request_can_send_now(hci_connection);
590e404a688SMatthias Ringwald     return ERROR_CODE_SUCCESS;
591e404a688SMatthias Ringwald }
592e404a688SMatthias Ringwald #endif
593e404a688SMatthias Ringwald 
594*68591e36SMatthias Ringwald static void att_run_for_context(hci_connection_t * hci_connection){
595*68591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
59618707219SMatthias Ringwald     switch (att_server->state){
5973deb3ec6SMatthias Ringwald         case ATT_SERVER_REQUEST_RECEIVED:
598760ad805SMatthias Ringwald 
59948364364SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
60048364364SMatthias Ringwald             if (att_server->l2cap_cid != 0){
60148364364SMatthias Ringwald                 // ok
60248364364SMatthias Ringwald             } else
60348364364SMatthias Ringwald #endif
60448364364SMatthias Ringwald             {
6050234fbbcSMatthias Ringwald                 // wait until re-encryption as central is complete
6060234fbbcSMatthias Ringwald                 if (gap_reconnect_security_setup_active(att_server->connection.con_handle)) break;
60748364364SMatthias Ringwald             }
6080234fbbcSMatthias Ringwald 
609760ad805SMatthias Ringwald             // wait until pairing is complete
610760ad805SMatthias Ringwald             if (att_server->pairing_active) break;
611760ad805SMatthias Ringwald 
6127a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
61318707219SMatthias Ringwald             if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){
6143deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write!");
6153deb3ec6SMatthias Ringwald                 if (!sm_cmac_ready()) {
6163deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, sm_cmac engine not ready. Abort");
61718707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
6183deb3ec6SMatthias Ringwald                     return;
6193deb3ec6SMatthias Ringwald                 }
62018707219SMatthias Ringwald                 if (att_server->request_size < (3 + 12)) {
6213deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, request to short. Abort.");
62218707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
6233deb3ec6SMatthias Ringwald                     return;
6243deb3ec6SMatthias Ringwald                 }
62518707219SMatthias Ringwald                 if (att_server->ir_lookup_active){
6263deb3ec6SMatthias Ringwald                     return;
6273deb3ec6SMatthias Ringwald                 }
62818707219SMatthias Ringwald                 if (att_server->ir_le_device_db_index < 0){
6293deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, CSRK not available");
63018707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
6313deb3ec6SMatthias Ringwald                     return;
6323deb3ec6SMatthias Ringwald                 }
6333deb3ec6SMatthias Ringwald 
6343deb3ec6SMatthias Ringwald                 // check counter
63518707219SMatthias Ringwald                 uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
63618707219SMatthias Ringwald                 uint32_t counter_db     = le_device_db_remote_counter_get(att_server->ir_le_device_db_index);
6373deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet);
6383deb3ec6SMatthias Ringwald                 if (counter_packet < counter_db){
6393deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, db reports higher counter, abort");
64018707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
6413deb3ec6SMatthias Ringwald                     return;
6423deb3ec6SMatthias Ringwald                 }
6433deb3ec6SMatthias Ringwald 
6443deb3ec6SMatthias Ringwald                 // signature is { sequence counter, secure hash }
6453deb3ec6SMatthias Ringwald                 sm_key_t csrk;
64618707219SMatthias Ringwald                 le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk);
64718707219SMatthias Ringwald                 att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION;
6483deb3ec6SMatthias Ringwald                 log_info("Orig Signature: ");
64918707219SMatthias Ringwald                 log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8);
65018707219SMatthias Ringwald                 uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1);
65118707219SMatthias 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);
6523deb3ec6SMatthias Ringwald                 return;
6533deb3ec6SMatthias Ringwald             }
6547a766ebfSMatthias Ringwald #endif
655b06f2579SMatthias Ringwald             // move on
65618707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
657*68591e36SMatthias Ringwald             att_server_request_can_send_now(hci_connection);
658b06f2579SMatthias Ringwald             break;
65988a48dd8SMatthias Ringwald 
6603deb3ec6SMatthias Ringwald         default:
6613deb3ec6SMatthias Ringwald             break;
6623deb3ec6SMatthias Ringwald     }
6633deb3ec6SMatthias Ringwald }
6643deb3ec6SMatthias Ringwald 
66590f03c80SMatthias Ringwald static int att_server_data_ready_for_phase(att_server_t * att_server,  att_server_run_phase_t phase){
66690f03c80SMatthias Ringwald     switch (phase){
66790f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
66890f03c80SMatthias Ringwald             return att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
66990f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
670c1ab6cc1SMatthias Ringwald              return (!btstack_linked_list_empty(&att_server->indication_requests) && (att_server->value_indication_handle == 0));
67190f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
67290f03c80SMatthias Ringwald             return (!btstack_linked_list_empty(&att_server->notification_requests));
6737bbeb3adSMilanka Ringwald         default:
6747bbeb3adSMilanka Ringwald             btstack_assert(false);
675fa5e3464SMatthias Ringwald             return 0;
67690f03c80SMatthias Ringwald     }
6777bbeb3adSMilanka Ringwald }
67890f03c80SMatthias Ringwald 
679*68591e36SMatthias Ringwald static void att_server_trigger_send_for_phase(hci_connection_t * hci_connection,  att_server_run_phase_t phase){
68090f03c80SMatthias Ringwald     btstack_context_callback_registration_t * client;
681*68591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
68290f03c80SMatthias Ringwald     switch (phase){
68390f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
684*68591e36SMatthias Ringwald             att_server_process_validated_request(hci_connection);
68590f03c80SMatthias Ringwald             break;
68690f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
68790f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->indication_requests;
68890f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->indication_requests, (btstack_linked_item_t *) client);
68990f03c80SMatthias Ringwald             client->callback(client->context);
69090f03c80SMatthias Ringwald             break;
69190f03c80SMatthias Ringwald        case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
69290f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->notification_requests;
69390f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->notification_requests, (btstack_linked_item_t *) client);
69490f03c80SMatthias Ringwald             client->callback(client->context);
69590f03c80SMatthias Ringwald             break;
6967bbeb3adSMilanka Ringwald         default:
6977bbeb3adSMilanka Ringwald             btstack_assert(false);
6987bbeb3adSMilanka Ringwald             break;
69990f03c80SMatthias Ringwald     }
70090f03c80SMatthias Ringwald }
70190f03c80SMatthias Ringwald 
7027eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){
703b06f2579SMatthias Ringwald 
7046438547aSMatthias Ringwald     hci_con_handle_t last_send_con_handle = HCI_CON_HANDLE_INVALID;
705*68591e36SMatthias Ringwald     hci_connection_t * request_hci_connection   = NULL;
7066438547aSMatthias Ringwald     int can_send_now = 1;
7074395a000SMatthias Ringwald     int phase_index;
7086438547aSMatthias Ringwald 
7094395a000SMatthias Ringwald     for (phase_index = ATT_SERVER_RUN_PHASE_1_REQUESTS; phase_index <= ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS; phase_index++){
7104395a000SMatthias Ringwald         att_server_run_phase_t phase = (att_server_run_phase_t) phase_index;
7116438547aSMatthias Ringwald         hci_con_handle_t skip_connections_until = att_server_last_can_send_now;
712ff3cc4a5SMatthias Ringwald         while (true){
7136438547aSMatthias Ringwald             btstack_linked_list_iterator_t it;
71418707219SMatthias Ringwald             hci_connections_get_iterator(&it);
71518707219SMatthias Ringwald             while(btstack_linked_list_iterator_has_next(&it)){
71618707219SMatthias Ringwald                 hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
71718707219SMatthias Ringwald                 att_server_t * att_server = &connection->att_server;
7186438547aSMatthias Ringwald 
71990f03c80SMatthias Ringwald                 int data_ready = att_server_data_ready_for_phase(att_server, phase);
7206438547aSMatthias Ringwald 
7216438547aSMatthias Ringwald                 // log_debug("phase %u, handle 0x%04x, skip until 0x%04x, data ready %u", phase, att_server->connection.con_handle, skip_connections_until, data_ready);
7226438547aSMatthias Ringwald 
7236438547aSMatthias Ringwald                 // skip until last sender found (which is also skipped)
7246438547aSMatthias Ringwald                 if (skip_connections_until != HCI_CON_HANDLE_INVALID){
725*68591e36SMatthias Ringwald                     if (data_ready && (request_hci_connection == NULL)){
726*68591e36SMatthias Ringwald                         request_hci_connection = connection;
7276438547aSMatthias Ringwald                     }
7286438547aSMatthias Ringwald                     if (skip_connections_until == att_server->connection.con_handle){
7296438547aSMatthias Ringwald                         skip_connections_until = HCI_CON_HANDLE_INVALID;
7306438547aSMatthias Ringwald                     }
7316438547aSMatthias Ringwald                     continue;
7326438547aSMatthias Ringwald                 };
7336438547aSMatthias Ringwald 
73490f03c80SMatthias Ringwald                 if (data_ready){
735969a5bbaSMatthias Ringwald                     if (can_send_now){
736*68591e36SMatthias Ringwald                         att_server_trigger_send_for_phase(connection, phase);
7376438547aSMatthias Ringwald                         last_send_con_handle = att_server->connection.con_handle;
738*68591e36SMatthias Ringwald                         can_send_now = att_server_can_send_packet(connection);
73990f03c80SMatthias Ringwald                         data_ready = att_server_data_ready_for_phase(att_server, phase);
740*68591e36SMatthias Ringwald                         if (data_ready && (request_hci_connection == NULL)){
741*68591e36SMatthias Ringwald                             request_hci_connection = connection;
742cbbb12d9SMatthias Ringwald                         }
743cbbb12d9SMatthias Ringwald                     } else {
744*68591e36SMatthias Ringwald                         request_hci_connection = connection;
745f29a88d8SMatthias Ringwald                         break;
746cbbb12d9SMatthias Ringwald                     }
747cbbb12d9SMatthias Ringwald                 }
7483deb3ec6SMatthias Ringwald             }
7493deb3ec6SMatthias Ringwald 
7506438547aSMatthias Ringwald             // stop skipping (handles disconnect by last send connection)
7516438547aSMatthias Ringwald             skip_connections_until = HCI_CON_HANDLE_INVALID;
7526438547aSMatthias Ringwald 
7533551936eSMatthias Ringwald             // Exit loop, if we cannot send
754969a5bbaSMatthias Ringwald             if (!can_send_now) break;
755969a5bbaSMatthias Ringwald 
756969a5bbaSMatthias Ringwald             // Exit loop, if we can send but there are also no further request
757*68591e36SMatthias Ringwald             if (request_hci_connection == NULL) break;
758969a5bbaSMatthias Ringwald 
759969a5bbaSMatthias Ringwald             // Finally, if we still can send and there are requests, just try again
760*68591e36SMatthias Ringwald             request_hci_connection = NULL;
761969a5bbaSMatthias Ringwald         }
7626438547aSMatthias Ringwald         // update last send con handle for round robin
7636438547aSMatthias Ringwald         if (last_send_con_handle != HCI_CON_HANDLE_INVALID){
7646438547aSMatthias Ringwald             att_server_last_can_send_now = last_send_con_handle;
7656438547aSMatthias Ringwald         }
7663551936eSMatthias Ringwald     }
767969a5bbaSMatthias Ringwald 
768*68591e36SMatthias Ringwald     if (request_hci_connection == NULL) return;
769*68591e36SMatthias Ringwald     att_server_request_can_send_now(request_hci_connection);
770969a5bbaSMatthias Ringwald }
771969a5bbaSMatthias Ringwald 
772*68591e36SMatthias Ringwald static void att_server_handle_att_pdu(hci_connection_t * hci_connection, uint8_t * packet, uint16_t size){
773*68591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
77418707219SMatthias Ringwald 
7753deb3ec6SMatthias Ringwald     // handle value indication confirms
776c1ab6cc1SMatthias Ringwald     if ((packet[0] == ATT_HANDLE_VALUE_CONFIRMATION) && att_server->value_indication_handle){
77718707219SMatthias Ringwald         btstack_run_loop_remove_timer(&att_server->value_indication_timer);
77818707219SMatthias Ringwald         uint16_t att_handle = att_server->value_indication_handle;
77918707219SMatthias Ringwald         att_server->value_indication_handle = 0;
78018707219SMatthias Ringwald         att_handle_value_indication_notify_client(0, att_server->connection.con_handle, att_handle);
781*68591e36SMatthias Ringwald         att_server_request_can_send_now(hci_connection);
7823deb3ec6SMatthias Ringwald         return;
7833deb3ec6SMatthias Ringwald     }
7843deb3ec6SMatthias Ringwald 
7856428eb5aSMatthias Ringwald     // directly process command
7866428eb5aSMatthias Ringwald     // note: signed write cannot be handled directly as authentication needs to be verified
7876428eb5aSMatthias Ringwald     if (packet[0] == ATT_WRITE_COMMAND){
7886e791780SMatthias Ringwald         att_handle_request(&att_server->connection, packet, size, NULL);
7896428eb5aSMatthias Ringwald         return;
7906428eb5aSMatthias Ringwald     }
7916428eb5aSMatthias Ringwald 
7923deb3ec6SMatthias Ringwald     // check size
79318707219SMatthias Ringwald     if (size > sizeof(att_server->request_buffer)) {
794e0463bdcSMatthias 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));
7956428eb5aSMatthias Ringwald         return;
7966428eb5aSMatthias Ringwald     }
7973deb3ec6SMatthias Ringwald 
798e0463bdcSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
799e0463bdcSMatthias Ringwald     // abort signed write validation if a new request comes in (but finish previous signed write if possible)
800e0463bdcSMatthias Ringwald     if (att_server->state == ATT_SERVER_W4_SIGNED_WRITE_VALIDATION){
801e0463bdcSMatthias Ringwald         if (packet[0] == ATT_SIGNED_WRITE_COMMAND){
802e0463bdcSMatthias Ringwald             log_info("skip new signed write request as previous is in validation");
803e0463bdcSMatthias Ringwald             return;
804e0463bdcSMatthias Ringwald         } else {
805e0463bdcSMatthias Ringwald             log_info("abort signed write validation to process new request");
806e0463bdcSMatthias Ringwald             att_server->state = ATT_SERVER_IDLE;
807e0463bdcSMatthias Ringwald         }
808e0463bdcSMatthias Ringwald     }
809e0463bdcSMatthias Ringwald #endif
8103deb3ec6SMatthias Ringwald     // last request still in processing?
81118707219SMatthias Ringwald     if (att_server->state != ATT_SERVER_IDLE){
812e0463bdcSMatthias Ringwald         log_info("skip att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state);
8136428eb5aSMatthias Ringwald         return;
8146428eb5aSMatthias Ringwald     }
8153deb3ec6SMatthias Ringwald 
8163deb3ec6SMatthias Ringwald     // store request
81718707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED;
81818707219SMatthias Ringwald     att_server->request_size = size;
8196535961aSMatthias Ringwald     (void)memcpy(att_server->request_buffer, packet, size);
8203deb3ec6SMatthias Ringwald 
821*68591e36SMatthias Ringwald     att_run_for_context(hci_connection);
82241772f37SMatthias Ringwald }
82341772f37SMatthias Ringwald 
82441772f37SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
82541772f37SMatthias Ringwald     att_server_t * att_server;
826*68591e36SMatthias Ringwald     hci_connection_t * hci_connection;
82741772f37SMatthias Ringwald 
82841772f37SMatthias Ringwald     switch (packet_type){
82941772f37SMatthias Ringwald         case HCI_EVENT_PACKET:
83041772f37SMatthias Ringwald             switch (packet[0]){
83141772f37SMatthias Ringwald                 case L2CAP_EVENT_CAN_SEND_NOW:
83241772f37SMatthias Ringwald                     att_server_handle_can_send_now();
83341772f37SMatthias Ringwald                     break;
83441772f37SMatthias Ringwald                 case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
83541772f37SMatthias Ringwald                     // GATT client has negotiated the mtu for this connection
836*68591e36SMatthias Ringwald                     hci_connection = hci_connection_for_handle(handle);
837*68591e36SMatthias Ringwald                     if (!hci_connection) break;
838*68591e36SMatthias Ringwald                     att_server = &hci_connection->att_server;
83941772f37SMatthias Ringwald                     att_server->connection.mtu = little_endian_read_16(packet, 4);
84041772f37SMatthias Ringwald                     break;
84141772f37SMatthias Ringwald                 default:
84241772f37SMatthias Ringwald                     break;
84341772f37SMatthias Ringwald             }
84441772f37SMatthias Ringwald             break;
84541772f37SMatthias Ringwald 
84641772f37SMatthias Ringwald         case ATT_DATA_PACKET:
84741772f37SMatthias Ringwald             log_debug("ATT Packet, handle 0x%04x", handle);
848*68591e36SMatthias Ringwald             hci_connection = hci_connection_for_handle(handle);
849*68591e36SMatthias Ringwald             if (!hci_connection) break;
85041772f37SMatthias Ringwald 
851*68591e36SMatthias Ringwald             att_server_handle_att_pdu(hci_connection, packet, size);
852372d62c4SMatthias Ringwald             break;
8537bbeb3adSMilanka Ringwald 
8547bbeb3adSMilanka Ringwald         default:
8557bbeb3adSMilanka Ringwald             break;
856372d62c4SMatthias Ringwald     }
8573deb3ec6SMatthias Ringwald }
8583deb3ec6SMatthias Ringwald 
859bf78aac6SMatthias Ringwald // ---------------------
860bf78aac6SMatthias Ringwald // persistent CCC writes
861bf78aac6SMatthias Ringwald static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){
8624ea43905SMatthias Ringwald     return ('B' << 24u) | ('T' << 16u) | ('C' << 8u) | index;
863bf78aac6SMatthias Ringwald }
864bf78aac6SMatthias Ringwald 
865bf78aac6SMatthias Ringwald static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){
866bf78aac6SMatthias Ringwald     // lookup att_server instance
867*68591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
868*68591e36SMatthias Ringwald     if (!hci_connection) return;
869*68591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
870bf78aac6SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
871bf78aac6SMatthias 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);
8726a540790SMatthias Ringwald 
873bf78aac6SMatthias Ringwald     // check if bonded
874bf78aac6SMatthias Ringwald     if (le_device_index < 0) return;
8756a540790SMatthias Ringwald 
876bf78aac6SMatthias Ringwald     // get btstack_tlv
877bf78aac6SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
878bf78aac6SMatthias Ringwald     void * tlv_context;
879bf78aac6SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
880bf78aac6SMatthias Ringwald     if (!tlv_impl) return;
8816a540790SMatthias Ringwald 
882bf78aac6SMatthias Ringwald     // update ccc tag
883bf78aac6SMatthias Ringwald     int index;
8846a540790SMatthias Ringwald     uint32_t highest_seq_nr = 0;
8856a540790SMatthias Ringwald     uint32_t lowest_seq_nr = 0;
8866a540790SMatthias Ringwald     uint32_t tag_for_lowest_seq_nr = 0;
8876a540790SMatthias Ringwald     uint32_t tag_for_empty = 0;
8886a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
8896afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
890bf78aac6SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
8916a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
8926a540790SMatthias Ringwald 
8936a540790SMatthias Ringwald         // empty/invalid tag
8946a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)){
8956a540790SMatthias Ringwald             tag_for_empty = tag;
896bf78aac6SMatthias Ringwald             continue;
897bf78aac6SMatthias Ringwald         }
8986a540790SMatthias Ringwald         // update highest seq nr
8996a540790SMatthias Ringwald         if (entry.seq_nr > highest_seq_nr){
9006a540790SMatthias Ringwald             highest_seq_nr = entry.seq_nr;
9016a540790SMatthias Ringwald         }
9026a540790SMatthias Ringwald         // find entry with lowest seq nr
9034ea43905SMatthias Ringwald         if ((tag_for_lowest_seq_nr == 0u) || (entry.seq_nr < lowest_seq_nr)){
9046a540790SMatthias Ringwald             tag_for_lowest_seq_nr = tag;
9056a540790SMatthias Ringwald             lowest_seq_nr = entry.seq_nr;
9066a540790SMatthias Ringwald         }
9076a540790SMatthias Ringwald 
9086a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
9096a540790SMatthias Ringwald         if (entry.att_handle   != att_handle)      continue;
9106a540790SMatthias Ringwald 
9116a540790SMatthias Ringwald         // found matching entry
91201ac2008SMatthias Ringwald         if (value){
913bf78aac6SMatthias Ringwald             // update
9146a540790SMatthias Ringwald             if (entry.value == value) {
915760ad805SMatthias Ringwald                 log_info("CCC Index %u: Up-to-date", index);
916760ad805SMatthias Ringwald                 return;
917760ad805SMatthias Ringwald             }
9186a540790SMatthias Ringwald             entry.value = value;
9194ea43905SMatthias Ringwald             entry.seq_nr = highest_seq_nr + 1u;
920760ad805SMatthias Ringwald             log_info("CCC Index %u: Store", index);
9210fe46bc1SMatthias Ringwald             int result = tlv_impl->store_tag(tlv_context, tag, (const uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
9220fe46bc1SMatthias Ringwald             if (result != 0){
9230fe46bc1SMatthias Ringwald                 log_error("Store tag index %u failed", index);
9240fe46bc1SMatthias Ringwald             }
92501ac2008SMatthias Ringwald         } else {
92601ac2008SMatthias Ringwald             // delete
927760ad805SMatthias Ringwald             log_info("CCC Index %u: Delete", index);
92801ac2008SMatthias Ringwald             tlv_impl->delete_tag(tlv_context, tag);
92901ac2008SMatthias Ringwald         }
930bf78aac6SMatthias Ringwald         return;
931bf78aac6SMatthias Ringwald     }
9326a540790SMatthias Ringwald 
9330fe46bc1SMatthias Ringwald     log_info("tag_for_empty %"PRIx32", tag_for_lowest_seq_nr %"PRIx32, tag_for_empty, tag_for_lowest_seq_nr);
9346a540790SMatthias Ringwald 
9354ea43905SMatthias Ringwald     if (value == 0u){
9366a540790SMatthias Ringwald         // done
9376a540790SMatthias Ringwald         return;
9386a540790SMatthias Ringwald     }
9396a540790SMatthias Ringwald 
9406a540790SMatthias Ringwald     uint32_t tag_to_use = 0;
9416a540790SMatthias Ringwald     if (tag_for_empty){
9426a540790SMatthias Ringwald         tag_to_use = tag_for_empty;
9436a540790SMatthias Ringwald     } else if (tag_for_lowest_seq_nr){
9446a540790SMatthias Ringwald         tag_to_use = tag_for_lowest_seq_nr;
9456a540790SMatthias Ringwald     } else {
9466a540790SMatthias Ringwald         // should not happen
9476a540790SMatthias Ringwald         return;
9486a540790SMatthias Ringwald     }
949bf78aac6SMatthias Ringwald     // store ccc tag
9504ea43905SMatthias Ringwald     entry.seq_nr       = highest_seq_nr + 1u;
9516a540790SMatthias Ringwald     entry.device_index = le_device_index;
9526a540790SMatthias Ringwald     entry.att_handle   = att_handle;
9536a540790SMatthias Ringwald     entry.value        = value;
9540fe46bc1SMatthias Ringwald     int result = tlv_impl->store_tag(tlv_context, tag_to_use, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
9550fe46bc1SMatthias Ringwald     if (result != 0){
9560fe46bc1SMatthias Ringwald         log_error("Store tag index %u failed", index);
9570fe46bc1SMatthias Ringwald     }
958bf78aac6SMatthias Ringwald }
959bf78aac6SMatthias Ringwald 
960*68591e36SMatthias Ringwald static void att_server_persistent_ccc_clear(hci_connection_t * hci_connection){
961*68591e36SMatthias Ringwald     if (!hci_connection) return;
962*68591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
963*68591e36SMatthias Ringwald 
964760ad805SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
965760ad805SMatthias Ringwald     log_info("Clear CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
966760ad805SMatthias Ringwald     // check if bonded
967760ad805SMatthias Ringwald     if (le_device_index < 0) return;
968760ad805SMatthias Ringwald     // get btstack_tlv
969760ad805SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
970760ad805SMatthias Ringwald     void * tlv_context;
971760ad805SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
972760ad805SMatthias Ringwald     if (!tlv_impl) return;
973760ad805SMatthias Ringwald     // get all ccc tag
974760ad805SMatthias Ringwald     int index;
9756a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
9766afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
977760ad805SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
9786a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
9796a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
9806a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
981760ad805SMatthias Ringwald         // delete entry
982760ad805SMatthias Ringwald         log_info("CCC Index %u: Delete", index);
983760ad805SMatthias Ringwald         tlv_impl->delete_tag(tlv_context, tag);
984760ad805SMatthias Ringwald     }
985760ad805SMatthias Ringwald }
986760ad805SMatthias Ringwald 
987*68591e36SMatthias Ringwald static void att_server_persistent_ccc_restore(hci_connection_t * hci_connection){
988*68591e36SMatthias Ringwald     if (!hci_connection) return;
989*68591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
990*68591e36SMatthias Ringwald 
99101ac2008SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
99201ac2008SMatthias Ringwald     log_info("Restore CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
9931b7acd0dSMatthias Ringwald     // check if bonded
9941b7acd0dSMatthias Ringwald     if (le_device_index < 0) return;
99501ac2008SMatthias Ringwald     // get btstack_tlv
99601ac2008SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
99701ac2008SMatthias Ringwald     void * tlv_context;
99801ac2008SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
99901ac2008SMatthias Ringwald     if (!tlv_impl) return;
100001ac2008SMatthias Ringwald     // get all ccc tag
100101ac2008SMatthias Ringwald     int index;
10026a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
10036afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
100401ac2008SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
10056a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
10066a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
10076a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
100801ac2008SMatthias Ringwald         // simulate write callback
10096a540790SMatthias Ringwald         uint16_t attribute_handle = entry.att_handle;
101001ac2008SMatthias Ringwald         uint8_t  value[2];
10116a540790SMatthias Ringwald         little_endian_store_16(value, 0, entry.value);
101201ac2008SMatthias Ringwald         att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
101301ac2008SMatthias Ringwald         if (!callback) continue;
10146a540790SMatthias Ringwald         log_info("CCC Index %u: Set Attribute handle 0x%04x to value 0x%04x", index, attribute_handle, entry.value );
101501ac2008SMatthias Ringwald         (*callback)(att_server->connection.con_handle, attribute_handle, ATT_TRANSACTION_MODE_NONE, 0, value, sizeof(value));
101601ac2008SMatthias Ringwald     }
101701ac2008SMatthias Ringwald }
101801ac2008SMatthias Ringwald 
1019bf78aac6SMatthias Ringwald // persistent CCC writes
1020bf78aac6SMatthias Ringwald // ---------------------
10213d71c7a4SMatthias Ringwald 
10223d71c7a4SMatthias Ringwald // gatt service management
10233d71c7a4SMatthias Ringwald static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){
10243d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
10253d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
10263d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
10273d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
10283d71c7a4SMatthias Ringwald         if (handler->start_handle > handle) continue;
10293d71c7a4SMatthias Ringwald         if (handler->end_handle   < handle) continue;
10303d71c7a4SMatthias Ringwald         return handler;
10313d71c7a4SMatthias Ringwald     }
10323d71c7a4SMatthias Ringwald     return NULL;
10333d71c7a4SMatthias Ringwald }
10343d71c7a4SMatthias Ringwald static att_read_callback_t att_server_read_callback_for_handle(uint16_t handle){
10353d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
10363d71c7a4SMatthias Ringwald     if (handler) return handler->read_callback;
10373d71c7a4SMatthias Ringwald     return att_server_client_read_callback;
10383d71c7a4SMatthias Ringwald }
10393d71c7a4SMatthias Ringwald 
10403d71c7a4SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){
10413d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
10423d71c7a4SMatthias Ringwald     if (handler) return handler->write_callback;
10433d71c7a4SMatthias Ringwald     return att_server_client_write_callback;
10443d71c7a4SMatthias Ringwald }
10453d71c7a4SMatthias Ringwald 
10465d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle){
10475d07396bSMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
10485d07396bSMatthias Ringwald     if (handler) return handler->packet_handler;
10495d07396bSMatthias Ringwald     return att_client_packet_handler;
10505d07396bSMatthias Ringwald }
10515d07396bSMatthias Ringwald 
10523d71c7a4SMatthias Ringwald static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){
10533d71c7a4SMatthias Ringwald     // notify all callbacks
10543d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
10553d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
10563d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
10573d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
10583d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
10593d71c7a4SMatthias Ringwald         (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
10603d71c7a4SMatthias Ringwald     }
10613d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return;
10623d71c7a4SMatthias Ringwald     (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
10633d71c7a4SMatthias Ringwald }
10643d71c7a4SMatthias Ringwald 
10653d71c7a4SMatthias Ringwald // returns first reported error or 0
10663d71c7a4SMatthias Ringwald static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){
10673d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
10683d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
10693d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
10703d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
10713d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
10723d71c7a4SMatthias Ringwald         uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
10733d71c7a4SMatthias Ringwald         if (error_code) return error_code;
10743d71c7a4SMatthias Ringwald     }
10753d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return 0;
10763d71c7a4SMatthias Ringwald     return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
10773d71c7a4SMatthias Ringwald }
10783d71c7a4SMatthias Ringwald 
10793d71c7a4SMatthias 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){
10803d71c7a4SMatthias Ringwald     att_read_callback_t callback = att_server_read_callback_for_handle(attribute_handle);
108101ac2008SMatthias Ringwald     if (!callback) return 0;
10823d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, offset, buffer, buffer_size);
10833d71c7a4SMatthias Ringwald }
10843d71c7a4SMatthias Ringwald 
10853d71c7a4SMatthias 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){
10863d71c7a4SMatthias Ringwald     switch (transaction_mode){
10873d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_VALIDATE:
10883d71c7a4SMatthias Ringwald             return att_validate_prepared_write(con_handle);
10893d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_EXECUTE:
10903d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_CANCEL:
10913d71c7a4SMatthias Ringwald             att_notify_write_callbacks(con_handle, transaction_mode);
10923d71c7a4SMatthias Ringwald             return 0;
10933d71c7a4SMatthias Ringwald         default:
10943d71c7a4SMatthias Ringwald             break;
10953d71c7a4SMatthias Ringwald     }
10963d71c7a4SMatthias Ringwald 
1097bf78aac6SMatthias Ringwald     // track CCC writes
10984ea43905SMatthias Ringwald     if (att_is_persistent_ccc(attribute_handle) && (offset == 0u) && (buffer_size == 2u)){
1099bf78aac6SMatthias Ringwald         att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0));
1100bf78aac6SMatthias Ringwald     }
1101bf78aac6SMatthias Ringwald 
110201ac2008SMatthias Ringwald     att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
110301ac2008SMatthias Ringwald     if (!callback) return 0;
11043d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size);
11053d71c7a4SMatthias Ringwald }
11063d71c7a4SMatthias Ringwald 
11073d71c7a4SMatthias Ringwald /**
11083d71c7a4SMatthias Ringwald  * @brief register read/write callbacks for specific handle range
11093d71c7a4SMatthias Ringwald  * @param att_service_handler_t
11103d71c7a4SMatthias Ringwald  */
11113d71c7a4SMatthias Ringwald void att_server_register_service_handler(att_service_handler_t * handler){
11123d71c7a4SMatthias Ringwald     if (att_service_handler_for_handle(handler->start_handle) ||
11133d71c7a4SMatthias Ringwald         att_service_handler_for_handle(handler->end_handle)){
11143d71c7a4SMatthias Ringwald         log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle);
11153d71c7a4SMatthias Ringwald         return;
11163d71c7a4SMatthias Ringwald     }
11173d71c7a4SMatthias Ringwald     btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler);
11183d71c7a4SMatthias Ringwald }
11193d71c7a4SMatthias Ringwald 
11203deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){
11213deb3ec6SMatthias Ringwald 
11223d71c7a4SMatthias Ringwald     // store callbacks
11233d71c7a4SMatthias Ringwald     att_server_client_read_callback  = read_callback;
11243d71c7a4SMatthias Ringwald     att_server_client_write_callback = write_callback;
11253d71c7a4SMatthias Ringwald 
11261a389cdcSMatthias Ringwald     // register for HCI Events
1127d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &att_event_packet_handler;
11281a389cdcSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
11291a389cdcSMatthias Ringwald 
1130406722e4SMatthias Ringwald     // register for SM events
1131d9a7306aSMatthias Ringwald     sm_event_callback_registration.callback = &att_event_packet_handler;
1132406722e4SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
11333deb3ec6SMatthias Ringwald 
11341a389cdcSMatthias Ringwald     // and L2CAP ATT Server PDUs
11353deb3ec6SMatthias Ringwald     att_dispatch_register_server(att_packet_handler);
11363deb3ec6SMatthias Ringwald 
113770dca4d4SMatthias Ringwald #ifdef ENABLE_GATT_OVER_CLASSIC
113870dca4d4SMatthias Ringwald     // setup l2cap service
113970dca4d4SMatthias Ringwald     l2cap_register_service(&att_event_packet_handler, PSM_ATT, 0xffff, LEVEL_2);
114070dca4d4SMatthias Ringwald #endif
114170dca4d4SMatthias Ringwald 
11423deb3ec6SMatthias Ringwald     att_set_db(db);
11433d71c7a4SMatthias Ringwald     att_set_read_callback(att_server_read_callback);
11443d71c7a4SMatthias Ringwald     att_set_write_callback(att_server_write_callback);
11453deb3ec6SMatthias Ringwald }
11463deb3ec6SMatthias Ringwald 
11473deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){
11483deb3ec6SMatthias Ringwald     att_client_packet_handler = handler;
11493deb3ec6SMatthias Ringwald }
11503deb3ec6SMatthias Ringwald 
1151cbbb12d9SMatthias Ringwald 
1152cbbb12d9SMatthias Ringwald // to be deprecated
1153c141988cSMatthias Ringwald int  att_server_can_send_packet_now(hci_con_handle_t con_handle){
1154*68591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
1155*68591e36SMatthias Ringwald     if (!hci_connection) return 0;
1156*68591e36SMatthias Ringwald     return att_server_can_send_packet(hci_connection);
11571b96b007SMatthias Ringwald }
11581b96b007SMatthias Ringwald 
1159cbbb12d9SMatthias Ringwald int att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
1160cbbb12d9SMatthias Ringwald     return att_server_request_to_send_notification(callback_registration, con_handle);
11613deb3ec6SMatthias Ringwald }
11623deb3ec6SMatthias Ringwald 
1163cbbb12d9SMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){
1164cbbb12d9SMatthias Ringwald     att_client_waiting_for_can_send_registration.callback = &att_emit_can_send_now_event;
1165cbbb12d9SMatthias Ringwald     att_server_request_to_send_notification(&att_client_waiting_for_can_send_registration, con_handle);
1166cbbb12d9SMatthias Ringwald }
1167cbbb12d9SMatthias Ringwald // end of deprecated
1168cbbb12d9SMatthias Ringwald 
1169cbbb12d9SMatthias Ringwald int att_server_request_to_send_notification(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
1170*68591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
1171*68591e36SMatthias Ringwald     if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1172*68591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
1173c37df6f6SMatthias Ringwald     bool added = btstack_linked_list_add_tail(&att_server->notification_requests, (btstack_linked_item_t*) callback_registration);
1174*68591e36SMatthias Ringwald     att_server_request_can_send_now(hci_connection);
1175c37df6f6SMatthias Ringwald     if (added){
1176cbbb12d9SMatthias Ringwald         return ERROR_CODE_SUCCESS;
1177c37df6f6SMatthias Ringwald     } else {
1178c37df6f6SMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1179c37df6f6SMatthias Ringwald     }
1180cbbb12d9SMatthias Ringwald }
1181cbbb12d9SMatthias Ringwald 
1182cbbb12d9SMatthias Ringwald int att_server_request_to_send_indication(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
1183*68591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
1184*68591e36SMatthias Ringwald     if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1185*68591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
1186c37df6f6SMatthias Ringwald     bool added = btstack_linked_list_add_tail(&att_server->indication_requests, (btstack_linked_item_t*) callback_registration);
1187*68591e36SMatthias Ringwald     att_server_request_can_send_now(hci_connection);
1188c37df6f6SMatthias Ringwald     if (added){
1189969a5bbaSMatthias Ringwald         return ERROR_CODE_SUCCESS;
1190c37df6f6SMatthias Ringwald     } else {
1191c37df6f6SMatthias Ringwald         return ERROR_CODE_COMMAND_DISALLOWED;
1192c37df6f6SMatthias Ringwald     }
1193bb38f057SMatthias Ringwald }
1194bb38f057SMatthias Ringwald 
11953bb3035fSMilanka Ringwald int att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){
1196*68591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
1197*68591e36SMatthias Ringwald     if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1198*68591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
1199*68591e36SMatthias Ringwald 
1200*68591e36SMatthias Ringwald     if (!att_server_can_send_packet(hci_connection)) return BTSTACK_ACL_BUFFERS_FULL;
12013deb3ec6SMatthias Ringwald 
12023deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
12033deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
120418707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_notification(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
120518707219SMatthias Ringwald 	return l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
12063deb3ec6SMatthias Ringwald }
12073deb3ec6SMatthias Ringwald 
12083bb3035fSMilanka Ringwald int att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){
1209*68591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
1210*68591e36SMatthias Ringwald     if (!hci_connection) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
1211*68591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
121218707219SMatthias Ringwald 
1213eaac31e8SMatthias Ringwald     if (att_server->value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS;
1214*68591e36SMatthias Ringwald     if (!att_server_can_send_packet(hci_connection)) return BTSTACK_ACL_BUFFERS_FULL;
12153deb3ec6SMatthias Ringwald 
12163deb3ec6SMatthias Ringwald     // track indication
121718707219SMatthias Ringwald     att_server->value_indication_handle = attribute_handle;
121818707219SMatthias Ringwald     btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout);
121918707219SMatthias Ringwald     btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS);
122018707219SMatthias Ringwald     btstack_run_loop_add_timer(&att_server->value_indication_timer);
12213deb3ec6SMatthias Ringwald 
12223deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
12233deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
122418707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_indication(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
122518707219SMatthias Ringwald 	l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
12263deb3ec6SMatthias Ringwald     return 0;
12273deb3ec6SMatthias Ringwald }
12288f9132b5SMilanka Ringwald 
12298f9132b5SMilanka Ringwald uint16_t att_server_get_mtu(hci_con_handle_t con_handle){
1230*68591e36SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
1231*68591e36SMatthias Ringwald     if (!hci_connection) return 0;
1232*68591e36SMatthias Ringwald     att_server_t * att_server = &hci_connection->att_server;
12338f9132b5SMilanka Ringwald     return att_server->connection.mtu;
12348f9132b5SMilanka Ringwald }
1235