xref: /btstack/src/ble/att_server.c (revision 8f9132b5820a4260818ec15355dfa6d05a6056ca)
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 
38ab2c6ae4SMatthias 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 <stdio.h>
473deb3ec6SMatthias Ringwald #include <stdlib.h>
483deb3ec6SMatthias Ringwald #include <string.h>
493deb3ec6SMatthias Ringwald #include <inttypes.h>
503deb3ec6SMatthias Ringwald 
517907f069SMatthias Ringwald #include "btstack_config.h"
523deb3ec6SMatthias Ringwald 
5359c6af15SMatthias Ringwald #include "att_dispatch.h"
5459c6af15SMatthias Ringwald #include "ble/att_db.h"
5559c6af15SMatthias Ringwald #include "ble/att_server.h"
5659c6af15SMatthias Ringwald #include "ble/core.h"
5759c6af15SMatthias Ringwald #include "ble/le_device_db.h"
5859c6af15SMatthias Ringwald #include "ble/sm.h"
5916ece135SMatthias Ringwald #include "btstack_debug.h"
600e2df43fSMatthias Ringwald #include "btstack_event.h"
613deb3ec6SMatthias Ringwald #include "btstack_memory.h"
620e2df43fSMatthias Ringwald #include "btstack_run_loop.h"
6359c6af15SMatthias Ringwald #include "gap.h"
643deb3ec6SMatthias Ringwald #include "hci.h"
653deb3ec6SMatthias Ringwald #include "hci_dump.h"
663deb3ec6SMatthias Ringwald #include "l2cap.h"
67bf78aac6SMatthias Ringwald #include "btstack_tlv.h"
68d1a1f6a4SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
69d1a1f6a4SMatthias Ringwald #include "ble/sm.h"
70d1a1f6a4SMatthias Ringwald #endif
71bf78aac6SMatthias Ringwald 
726afb9acaSMatthias Ringwald #ifndef NVN_NUM_GATT_SERVER_CCC
736afb9acaSMatthias Ringwald #define NVN_NUM_GATT_SERVER_CCC 20
74bf78aac6SMatthias Ringwald #endif
753deb3ec6SMatthias Ringwald 
7618707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server);
7701ac2008SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle);
785d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle);
7901ac2008SMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server);
80760ad805SMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server);
813deb3ec6SMatthias Ringwald 
823551936eSMatthias Ringwald typedef enum {
833551936eSMatthias Ringwald     ATT_SERVER_RUN_PHASE_1_REQUESTS,
843551936eSMatthias Ringwald     ATT_SERVER_RUN_PHASE_2_INDICATIONS,
853551936eSMatthias Ringwald     ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS,
863551936eSMatthias Ringwald } att_server_run_phase_t;
873551936eSMatthias Ringwald 
886a540790SMatthias Ringwald //
896a540790SMatthias Ringwald typedef struct {
906a540790SMatthias Ringwald     uint32_t seq_nr;
916a540790SMatthias Ringwald     uint16_t att_handle;
926a540790SMatthias Ringwald     uint8_t  value;
936a540790SMatthias Ringwald     uint8_t  device_index;
946a540790SMatthias Ringwald } persistent_ccc_entry_t;
956a540790SMatthias Ringwald 
9618707219SMatthias Ringwald // global
971a389cdcSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
98406722e4SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration;
993deb3ec6SMatthias Ringwald static btstack_packet_handler_t               att_client_packet_handler = NULL;
1003d71c7a4SMatthias Ringwald static btstack_linked_list_t                  service_handlers;
1015f141511SMatthias Ringwald static btstack_context_callback_registration_t att_client_waiting_for_can_send_registration;
10218707219SMatthias Ringwald 
1033d71c7a4SMatthias Ringwald static att_read_callback_t                    att_server_client_read_callback;
1043d71c7a4SMatthias Ringwald static att_write_callback_t                   att_server_client_write_callback;
1053d71c7a4SMatthias Ringwald 
1066438547aSMatthias Ringwald // round robin
1076438547aSMatthias Ringwald static hci_con_handle_t att_server_last_can_send_now = HCI_CON_HANDLE_INVALID;
108bf78aac6SMatthias Ringwald 
10918707219SMatthias Ringwald static att_server_t * att_server_for_handle(hci_con_handle_t con_handle){
11018707219SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
11118707219SMatthias Ringwald     if (!hci_connection) return NULL;
11218707219SMatthias Ringwald     return &hci_connection->att_server;
11318707219SMatthias Ringwald }
11418707219SMatthias Ringwald 
11502b78fc8SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
11618707219SMatthias Ringwald static att_server_t * att_server_for_state(att_server_state_t state){
11718707219SMatthias Ringwald     btstack_linked_list_iterator_t it;
11818707219SMatthias Ringwald     hci_connections_get_iterator(&it);
11918707219SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
12018707219SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
12118707219SMatthias Ringwald         att_server_t * att_server = &connection->att_server;
12218707219SMatthias Ringwald         if (att_server->state == state) return att_server;
12318707219SMatthias Ringwald     }
12418707219SMatthias Ringwald     return NULL;
12518707219SMatthias Ringwald }
12602b78fc8SMatthias Ringwald #endif
127bb38f057SMatthias Ringwald 
1283deb3ec6SMatthias Ringwald static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){
1295d07396bSMatthias Ringwald     btstack_packet_handler_t packet_handler = att_server_packet_handler_for_handle(attribute_handle);
1305d07396bSMatthias Ringwald     if (!packet_handler) return;
1313deb3ec6SMatthias Ringwald 
1323deb3ec6SMatthias Ringwald     uint8_t event[7];
1333deb3ec6SMatthias Ringwald     int pos = 0;
1345611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE;
1353deb3ec6SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
1363deb3ec6SMatthias Ringwald     event[pos++] = status;
137f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, client_handle);
1383deb3ec6SMatthias Ringwald     pos += 2;
139f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, attribute_handle);
1403deb3ec6SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1413deb3ec6SMatthias Ringwald }
1423deb3ec6SMatthias Ringwald 
1435d07396bSMatthias Ringwald static void att_emit_event_to_all(const uint8_t * event, uint16_t size){
1445d07396bSMatthias Ringwald     // dispatch to app level handler
1455d07396bSMatthias Ringwald     if (att_client_packet_handler){
1465d07396bSMatthias Ringwald         (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size);
1475d07396bSMatthias Ringwald     }
1483deb3ec6SMatthias Ringwald 
1495d07396bSMatthias Ringwald     // dispatch to service handlers
1505d07396bSMatthias Ringwald     btstack_linked_list_iterator_t it;
1515d07396bSMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
1525d07396bSMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
1535d07396bSMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
1545d07396bSMatthias Ringwald         if (!handler->packet_handler) continue;
1555d07396bSMatthias Ringwald         (*handler->packet_handler)(HCI_EVENT_PACKET, 0, (uint8_t*) event, size);
1565d07396bSMatthias Ringwald     }
1575d07396bSMatthias Ringwald }
1585d07396bSMatthias Ringwald 
1595d07396bSMatthias Ringwald static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){
1603deb3ec6SMatthias Ringwald     uint8_t event[6];
1613deb3ec6SMatthias Ringwald     int pos = 0;
1625611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE;
1633deb3ec6SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
164fc64f94aSMatthias Ringwald     little_endian_store_16(event, pos, con_handle);
1653deb3ec6SMatthias Ringwald     pos += 2;
166f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, mtu);
1675d07396bSMatthias Ringwald 
1685d07396bSMatthias Ringwald     // also dispatch to GATT Clients
169b12646c5SJakob Krantz     att_dispatch_server_mtu_exchanged(con_handle, mtu);
1705d07396bSMatthias Ringwald 
1715d07396bSMatthias Ringwald     // dispatch to app level handler and service handlers
1725d07396bSMatthias Ringwald     att_emit_event_to_all(&event[0], sizeof(event));
1733deb3ec6SMatthias Ringwald }
1743deb3ec6SMatthias Ringwald 
1755f141511SMatthias Ringwald static void att_emit_can_send_now_event(void * context){
1765f141511SMatthias Ringwald     UNUSED(context);
1773c97b242SMatthias Ringwald     if (!att_client_packet_handler) return;
1783c97b242SMatthias Ringwald 
1791b96b007SMatthias Ringwald     uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0};
1801b96b007SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1811b96b007SMatthias Ringwald }
1821b96b007SMatthias Ringwald 
183ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){
18454178543SMatthias Ringwald     void * context = btstack_run_loop_get_timer_context(ts);
18554178543SMatthias Ringwald     hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
18618707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
18718707219SMatthias Ringwald     if (!att_server) return;
188bce48a26SMatthias Ringwald     // @note: after a transcation timeout, no more requests shall be sent over this ATT Bearer
189bce48a26SMatthias Ringwald     // (that's why we don't reset the value_indication_handle)
19018707219SMatthias Ringwald     uint16_t att_handle = att_server->value_indication_handle;
19118707219SMatthias Ringwald     att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_server->connection.con_handle, att_handle);
1923deb3ec6SMatthias Ringwald }
1933deb3ec6SMatthias Ringwald 
1943deb3ec6SMatthias Ringwald static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1959ec2630cSMatthias Ringwald 
1967dad9ff8SMatthias Ringwald     UNUSED(channel); // ok: there is no channel
1977dad9ff8SMatthias Ringwald     UNUSED(size);    // ok: handling own l2cap events
1983deb3ec6SMatthias Ringwald 
19918707219SMatthias Ringwald     att_server_t * att_server;
20018707219SMatthias Ringwald     hci_con_handle_t con_handle;
20118707219SMatthias Ringwald 
2023deb3ec6SMatthias Ringwald     switch (packet_type) {
2033deb3ec6SMatthias Ringwald 
2043deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
2050e2df43fSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
2063deb3ec6SMatthias Ringwald 
2073deb3ec6SMatthias Ringwald                 case HCI_EVENT_LE_META:
2083deb3ec6SMatthias Ringwald                     switch (packet[2]) {
2093deb3ec6SMatthias Ringwald                         case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
21018707219SMatthias Ringwald                             con_handle = little_endian_read_16(packet, 4);
21118707219SMatthias Ringwald                             att_server = att_server_for_handle(con_handle);
21218707219SMatthias Ringwald                             if (!att_server) break;
2133deb3ec6SMatthias Ringwald                         	// store connection info
21418707219SMatthias Ringwald                         	att_server->peer_addr_type = packet[7];
21518707219SMatthias Ringwald                             reverse_bd_addr(&packet[8], att_server->peer_address);
21618707219SMatthias Ringwald                             att_server->connection.con_handle = con_handle;
2173deb3ec6SMatthias Ringwald                             // reset connection properties
21818707219SMatthias Ringwald                             att_server->state = ATT_SERVER_IDLE;
21918707219SMatthias Ringwald                             att_server->connection.mtu = ATT_DEFAULT_MTU;
22018707219SMatthias Ringwald                             att_server->connection.max_mtu = l2cap_max_le_mtu();
22118707219SMatthias Ringwald                             if (att_server->connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){
22218707219SMatthias Ringwald                                 att_server->connection.max_mtu = ATT_REQUEST_BUFFER_SIZE;
22399c58ad0SMatthias Ringwald                             }
22418707219SMatthias Ringwald                             att_server->connection.encryption_key_size = 0;
22518707219SMatthias Ringwald                             att_server->connection.authenticated = 0;
22618707219SMatthias Ringwald 		                	att_server->connection.authorized = 0;
227b2c1e52cSMatthias Ringwald                             // workaround: identity resolving can already be complete, at least store result
228b2c1e52cSMatthias Ringwald                             att_server->ir_le_device_db_index = sm_le_device_index(con_handle);
229760ad805SMatthias Ringwald                             att_server->pairing_active = 0;
2305d07396bSMatthias Ringwald                             // notify all
2315d07396bSMatthias Ringwald                             att_emit_event_to_all(packet, size);
2323deb3ec6SMatthias Ringwald                             break;
2333deb3ec6SMatthias Ringwald 
2343deb3ec6SMatthias Ringwald                         default:
2353deb3ec6SMatthias Ringwald                             break;
2363deb3ec6SMatthias Ringwald                     }
2373deb3ec6SMatthias Ringwald                     break;
2383deb3ec6SMatthias Ringwald 
2393deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_CHANGE:
2403deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE:
2413deb3ec6SMatthias Ringwald                 	// check handle
24218707219SMatthias Ringwald                     con_handle = little_endian_read_16(packet, 3);
24318707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
24418707219SMatthias Ringwald                     if (!att_server) break;
2459c6e867eSMatthias Ringwald                     att_server->connection.encryption_key_size = gap_encryption_key_size(con_handle);
2469c6e867eSMatthias Ringwald                     att_server->connection.authenticated = gap_authenticated(con_handle);
24701ac2008SMatthias Ringwald                     if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){
24801ac2008SMatthias Ringwald                         // restore CCC values when encrypted
24901ac2008SMatthias Ringwald                         if (hci_event_encryption_change_get_encryption_enabled(packet)){
25001ac2008SMatthias Ringwald                             att_server_persistent_ccc_restore(att_server);
25101ac2008SMatthias Ringwald                         }
25201ac2008SMatthias Ringwald                     }
2530234fbbcSMatthias Ringwald                     att_run_for_context(att_server);
2543deb3ec6SMatthias Ringwald                     break;
2553deb3ec6SMatthias Ringwald 
2563deb3ec6SMatthias Ringwald                 case HCI_EVENT_DISCONNECTION_COMPLETE:
25770a390c7SMatthias Ringwald                     // check handle
25818707219SMatthias Ringwald                     con_handle = hci_event_disconnection_complete_get_connection_handle(packet);
25918707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
26018707219SMatthias Ringwald                     if (!att_server) break;
26118707219SMatthias Ringwald                     att_clear_transaction_queue(&att_server->connection);
26218707219SMatthias Ringwald                     att_server->connection.con_handle = 0;
263760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
26418707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
265bce48a26SMatthias Ringwald                     if (att_server->value_indication_handle){
26627328ecbSMatthias Ringwald                         btstack_run_loop_remove_timer(&att_server->value_indication_timer);
267bce48a26SMatthias Ringwald                         uint16_t att_handle = att_server->value_indication_handle;
268bce48a26SMatthias Ringwald                         att_server->value_indication_handle = 0; // reset error state
269bce48a26SMatthias Ringwald                         att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_DISCONNECT, att_server->connection.con_handle, att_handle);
270bce48a26SMatthias Ringwald                     }
2715d07396bSMatthias Ringwald                     // notify all
2725d07396bSMatthias Ringwald                     att_emit_event_to_all(packet, size);
2733deb3ec6SMatthias Ringwald                     break;
2743deb3ec6SMatthias Ringwald 
275760ad805SMatthias Ringwald                 // Identity Resolving
2765611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_STARTED:
27718707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_started_get_handle(packet);
27818707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
27918707219SMatthias Ringwald                     if (!att_server) break;
2805611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED");
28118707219SMatthias Ringwald                     att_server->ir_lookup_active = 1;
2823deb3ec6SMatthias Ringwald                     break;
2835611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
284760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
285760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
286760ad805SMatthias Ringwald                     if (!att_server) return;
2871b7acd0dSMatthias Ringwald                     att_server->ir_lookup_active = 0;
2881b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index(packet);
2891b7acd0dSMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED");
2901b7acd0dSMatthias Ringwald                     att_run_for_context(att_server);
2913deb3ec6SMatthias Ringwald                     break;
2925611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_FAILED:
29318707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_failed_get_handle(packet);
29418707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
29518707219SMatthias Ringwald                     if (!att_server) break;
2965611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED");
29718707219SMatthias Ringwald                     att_server->ir_lookup_active = 0;
29818707219SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
29918707219SMatthias Ringwald                     att_run_for_context(att_server);
3003deb3ec6SMatthias Ringwald                     break;
301760ad805SMatthias Ringwald 
302760ad805SMatthias Ringwald                 // Pairing started - delete stored CCC values
303760ad805SMatthias Ringwald                 // - assumes pairing indicates either new device or re-pairing, in both cases there should be no stored CCC values
304760ad805SMatthias Ringwald                 // - assumes that all events have the con handle as the first field
305760ad805SMatthias Ringwald                 case SM_EVENT_JUST_WORKS_REQUEST:
306760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
307760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_INPUT_NUMBER:
308760ad805SMatthias Ringwald                 case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
309760ad805SMatthias Ringwald                     con_handle = sm_event_just_works_request_get_handle(packet);
310760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
311760ad805SMatthias Ringwald                     if (!att_server) break;
312760ad805SMatthias Ringwald                     att_server->pairing_active = 1;
313760ad805SMatthias Ringwald                     log_info("SM Pairing started");
314760ad805SMatthias Ringwald                     if (att_server->ir_le_device_db_index < 0) break;
315760ad805SMatthias Ringwald                     att_server_persistent_ccc_clear(att_server);
316760ad805SMatthias Ringwald                     // index not valid anymore
317760ad805SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
318760ad805SMatthias Ringwald                     break;
319760ad805SMatthias Ringwald 
3201b7acd0dSMatthias Ringwald                 // Bonding completed
321760ad805SMatthias Ringwald                 case SM_EVENT_IDENTITY_CREATED:
322760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
323760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
324760ad805SMatthias Ringwald                     if (!att_server) return;
325760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
3261b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_created_get_index(packet);
3271b7acd0dSMatthias Ringwald                     att_run_for_context(att_server);
3281b7acd0dSMatthias Ringwald                     break;
3291b7acd0dSMatthias Ringwald 
3301b7acd0dSMatthias Ringwald                 // Pairing complete (with/without bonding=storing of pairing information)
3311b7acd0dSMatthias Ringwald                 case SM_EVENT_PAIRING_COMPLETE:
3321b7acd0dSMatthias Ringwald                     con_handle = sm_event_pairing_complete_get_handle(packet);
3331b7acd0dSMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
3341b7acd0dSMatthias Ringwald                     if (!att_server) return;
3351b7acd0dSMatthias Ringwald                     att_server->pairing_active = 0;
3361b7acd0dSMatthias Ringwald                     att_run_for_context(att_server);
337760ad805SMatthias Ringwald                     break;
338760ad805SMatthias Ringwald 
339760ad805SMatthias Ringwald                 // Authorization
3405611a760SMatthias Ringwald                 case SM_EVENT_AUTHORIZATION_RESULT: {
34118707219SMatthias Ringwald                     con_handle = sm_event_authorization_result_get_handle(packet);
34218707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
34318707219SMatthias Ringwald                     if (!att_server) break;
34418707219SMatthias Ringwald                     att_server->connection.authorized = sm_event_authorization_result_get_authorization_result(packet);
34518707219SMatthias Ringwald                     att_dispatch_server_request_can_send_now_event(con_handle);
3463deb3ec6SMatthias Ringwald                 	break;
3473deb3ec6SMatthias Ringwald                 }
3483deb3ec6SMatthias Ringwald                 default:
3493deb3ec6SMatthias Ringwald                     break;
3503deb3ec6SMatthias Ringwald             }
35165b44ffdSMatthias Ringwald             break;
35265b44ffdSMatthias Ringwald         default:
35365b44ffdSMatthias Ringwald             break;
3543deb3ec6SMatthias Ringwald     }
3553deb3ec6SMatthias Ringwald }
3563deb3ec6SMatthias Ringwald 
3577a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
3583deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
3593deb3ec6SMatthias Ringwald 
36018707219SMatthias Ringwald     att_server_t * att_server = att_server_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION);
36118707219SMatthias Ringwald     if (!att_server) return;
3623deb3ec6SMatthias Ringwald 
3633deb3ec6SMatthias Ringwald     uint8_t hash_flipped[8];
3649c80e4ccSMatthias Ringwald     reverse_64(hash, hash_flipped);
36518707219SMatthias Ringwald     if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8)){
3663deb3ec6SMatthias Ringwald         log_info("ATT Signed Write, invalid signature");
36718707219SMatthias Ringwald         att_server->state = ATT_SERVER_IDLE;
3683deb3ec6SMatthias Ringwald         return;
3693deb3ec6SMatthias Ringwald     }
3703deb3ec6SMatthias Ringwald     log_info("ATT Signed Write, valid signature");
3713deb3ec6SMatthias Ringwald 
3723deb3ec6SMatthias Ringwald     // update sequence number
37318707219SMatthias Ringwald     uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
37418707219SMatthias Ringwald     le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1);
37518707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
37618707219SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
3773deb3ec6SMatthias Ringwald }
3787a766ebfSMatthias Ringwald #endif
37988a48dd8SMatthias Ringwald 
38018707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED
381b06f2579SMatthias Ringwald // pre: can send now
382b06f2579SMatthias Ringwald // returns: 1 if packet was sent
38318707219SMatthias Ringwald static int att_server_process_validated_request(att_server_t * att_server){
384b06f2579SMatthias Ringwald 
385b06f2579SMatthias Ringwald     l2cap_reserve_packet_buffer();
386b06f2579SMatthias Ringwald     uint8_t * att_response_buffer = l2cap_get_outgoing_buffer();
38718707219SMatthias Ringwald     uint16_t  att_response_size   = att_handle_request(&att_server->connection, att_server->request_buffer, att_server->request_size, att_response_buffer);
388b06f2579SMatthias Ringwald 
389beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
39056c3a347SMatthias Ringwald     if (att_response_size == ATT_READ_RESPONSE_PENDING || att_response_size == ATT_INTERNAL_WRITE_RESPONSE_PENDING){
391e404a688SMatthias Ringwald         // update state
392beb20288SMatthias Ringwald         att_server->state = ATT_SERVER_RESPONSE_PENDING;
393e404a688SMatthias Ringwald 
39456c3a347SMatthias Ringwald         // callback with handle ATT_READ_RESPONSE_PENDING for reads
39556c3a347SMatthias Ringwald         if (att_response_size == ATT_READ_RESPONSE_PENDING){
396e404a688SMatthias Ringwald             att_server_client_read_callback(att_server->connection.con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0);
39756c3a347SMatthias Ringwald         }
398e8e7403eSMatthias Ringwald 
399e8e7403eSMatthias Ringwald         // free reserved buffer
400e8e7403eSMatthias Ringwald         l2cap_release_packet_buffer();
401e8e7403eSMatthias Ringwald         return 0;
402e404a688SMatthias Ringwald     }
403e404a688SMatthias Ringwald #endif
404e404a688SMatthias Ringwald 
405b06f2579SMatthias Ringwald     // intercept "insufficient authorization" for authenticated connections to allow for user authorization
406b06f2579SMatthias Ringwald     if ((att_response_size     >= 4)
407b06f2579SMatthias Ringwald     && (att_response_buffer[0] == ATT_ERROR_RESPONSE)
408b06f2579SMatthias Ringwald     && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION)
40918707219SMatthias Ringwald     && (att_server->connection.authenticated)){
410b06f2579SMatthias Ringwald 
4119c6e867eSMatthias Ringwald         switch (gap_authorization_state(att_server->connection.con_handle)){
412b06f2579SMatthias Ringwald             case AUTHORIZATION_UNKNOWN:
413b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
41418707219SMatthias Ringwald                 sm_request_pairing(att_server->connection.con_handle);
415b06f2579SMatthias Ringwald                 return 0;
416b06f2579SMatthias Ringwald             case AUTHORIZATION_PENDING:
417b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
418b06f2579SMatthias Ringwald                 return 0;
419b06f2579SMatthias Ringwald             default:
420b06f2579SMatthias Ringwald                 break;
421b06f2579SMatthias Ringwald         }
422b06f2579SMatthias Ringwald     }
423b06f2579SMatthias Ringwald 
42418707219SMatthias Ringwald     att_server->state = ATT_SERVER_IDLE;
425b06f2579SMatthias Ringwald     if (att_response_size == 0) {
426b06f2579SMatthias Ringwald         l2cap_release_packet_buffer();
427b06f2579SMatthias Ringwald         return 0;
428b06f2579SMatthias Ringwald     }
429b06f2579SMatthias Ringwald 
43018707219SMatthias Ringwald     l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size);
431b06f2579SMatthias Ringwald 
432b06f2579SMatthias Ringwald     // notify client about MTU exchange result
433b06f2579SMatthias Ringwald     if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){
43418707219SMatthias Ringwald         att_emit_mtu_event(att_server->connection.con_handle, att_server->connection.mtu);
435b06f2579SMatthias Ringwald     }
436b06f2579SMatthias Ringwald     return 1;
437b06f2579SMatthias Ringwald }
438b06f2579SMatthias Ringwald 
439beb20288SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
440beb20288SMatthias Ringwald int att_server_response_ready(hci_con_handle_t con_handle){
441e404a688SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
442e404a688SMatthias Ringwald     if (!att_server)                                        return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
443beb20288SMatthias Ringwald     if (att_server->state != ATT_SERVER_RESPONSE_PENDING)   return ERROR_CODE_COMMAND_DISALLOWED;
444e404a688SMatthias Ringwald 
445e404a688SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
446e404a688SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
447e404a688SMatthias Ringwald     return ERROR_CODE_SUCCESS;
448e404a688SMatthias Ringwald }
449e404a688SMatthias Ringwald #endif
450e404a688SMatthias Ringwald 
45118707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server){
45218707219SMatthias Ringwald     switch (att_server->state){
4533deb3ec6SMatthias Ringwald         case ATT_SERVER_REQUEST_RECEIVED:
454760ad805SMatthias Ringwald 
4550234fbbcSMatthias Ringwald             // wait until re-encryption as central is complete
4560234fbbcSMatthias Ringwald             if (gap_reconnect_security_setup_active(att_server->connection.con_handle)) break;
4570234fbbcSMatthias Ringwald 
458760ad805SMatthias Ringwald             // wait until pairing is complete
459760ad805SMatthias Ringwald             if (att_server->pairing_active) break;
460760ad805SMatthias Ringwald 
4617a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
46218707219SMatthias Ringwald             if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){
4633deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write!");
4643deb3ec6SMatthias Ringwald                 if (!sm_cmac_ready()) {
4653deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, sm_cmac engine not ready. Abort");
46618707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
4673deb3ec6SMatthias Ringwald                     return;
4683deb3ec6SMatthias Ringwald                 }
46918707219SMatthias Ringwald                 if (att_server->request_size < (3 + 12)) {
4703deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, request to short. Abort.");
47118707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
4723deb3ec6SMatthias Ringwald                     return;
4733deb3ec6SMatthias Ringwald                 }
47418707219SMatthias Ringwald                 if (att_server->ir_lookup_active){
4753deb3ec6SMatthias Ringwald                     return;
4763deb3ec6SMatthias Ringwald                 }
47718707219SMatthias Ringwald                 if (att_server->ir_le_device_db_index < 0){
4783deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, CSRK not available");
47918707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
4803deb3ec6SMatthias Ringwald                     return;
4813deb3ec6SMatthias Ringwald                 }
4823deb3ec6SMatthias Ringwald 
4833deb3ec6SMatthias Ringwald                 // check counter
48418707219SMatthias Ringwald                 uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
48518707219SMatthias Ringwald                 uint32_t counter_db     = le_device_db_remote_counter_get(att_server->ir_le_device_db_index);
4863deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet);
4873deb3ec6SMatthias Ringwald                 if (counter_packet < counter_db){
4883deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, db reports higher counter, abort");
48918707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
4903deb3ec6SMatthias Ringwald                     return;
4913deb3ec6SMatthias Ringwald                 }
4923deb3ec6SMatthias Ringwald 
4933deb3ec6SMatthias Ringwald                 // signature is { sequence counter, secure hash }
4943deb3ec6SMatthias Ringwald                 sm_key_t csrk;
49518707219SMatthias Ringwald                 le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk);
49618707219SMatthias Ringwald                 att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION;
4973deb3ec6SMatthias Ringwald                 log_info("Orig Signature: ");
49818707219SMatthias Ringwald                 log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8);
49918707219SMatthias Ringwald                 uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1);
50018707219SMatthias 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);
5013deb3ec6SMatthias Ringwald                 return;
5023deb3ec6SMatthias Ringwald             }
5037a766ebfSMatthias Ringwald #endif
504b06f2579SMatthias Ringwald             // move on
50518707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
50618707219SMatthias Ringwald             att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
507b06f2579SMatthias Ringwald             break;
50888a48dd8SMatthias Ringwald 
5093deb3ec6SMatthias Ringwald         default:
5103deb3ec6SMatthias Ringwald             break;
5113deb3ec6SMatthias Ringwald     }
5123deb3ec6SMatthias Ringwald }
5133deb3ec6SMatthias Ringwald 
51490f03c80SMatthias Ringwald static int att_server_data_ready_for_phase(att_server_t * att_server,  att_server_run_phase_t phase){
51590f03c80SMatthias Ringwald     switch (phase){
51690f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
51790f03c80SMatthias Ringwald             return att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
51890f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
51990f03c80SMatthias Ringwald              return (!btstack_linked_list_empty(&att_server->indication_requests) && att_server->value_indication_handle == 0);
52090f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
52190f03c80SMatthias Ringwald             return (!btstack_linked_list_empty(&att_server->notification_requests));
52290f03c80SMatthias Ringwald     }
523fa5e3464SMatthias Ringwald     // avoid warning
524fa5e3464SMatthias Ringwald     return 0;
52590f03c80SMatthias Ringwald }
52690f03c80SMatthias Ringwald 
52790f03c80SMatthias Ringwald static void att_server_trigger_send_for_phase(att_server_t * att_server,  att_server_run_phase_t phase){
52890f03c80SMatthias Ringwald     btstack_context_callback_registration_t * client;
52990f03c80SMatthias Ringwald     switch (phase){
53090f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
53190f03c80SMatthias Ringwald             att_server_process_validated_request(att_server);
53290f03c80SMatthias Ringwald             break;
53390f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
53490f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->indication_requests;
53590f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->indication_requests, (btstack_linked_item_t *) client);
53690f03c80SMatthias Ringwald             client->callback(client->context);
53790f03c80SMatthias Ringwald             break;
53890f03c80SMatthias Ringwald        case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
53990f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->notification_requests;
54090f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->notification_requests, (btstack_linked_item_t *) client);
54190f03c80SMatthias Ringwald             client->callback(client->context);
54290f03c80SMatthias Ringwald             break;
54390f03c80SMatthias Ringwald     }
54490f03c80SMatthias Ringwald }
54590f03c80SMatthias Ringwald 
5467eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){
547b06f2579SMatthias Ringwald 
548374a288aSMatthias Ringwald     hci_con_handle_t request_con_handle   = HCI_CON_HANDLE_INVALID;
5496438547aSMatthias Ringwald     hci_con_handle_t last_send_con_handle = HCI_CON_HANDLE_INVALID;
5506438547aSMatthias Ringwald     int can_send_now = 1;
5514395a000SMatthias Ringwald     int phase_index;
5526438547aSMatthias Ringwald 
5534395a000SMatthias Ringwald     for (phase_index = ATT_SERVER_RUN_PHASE_1_REQUESTS; phase_index <= ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS; phase_index++){
5544395a000SMatthias Ringwald         att_server_run_phase_t phase = (att_server_run_phase_t) phase_index;
5556438547aSMatthias Ringwald         hci_con_handle_t skip_connections_until = att_server_last_can_send_now;
556374a288aSMatthias Ringwald         while (1){
5576438547aSMatthias Ringwald             btstack_linked_list_iterator_t it;
55818707219SMatthias Ringwald             hci_connections_get_iterator(&it);
55918707219SMatthias Ringwald             while(btstack_linked_list_iterator_has_next(&it)){
56018707219SMatthias Ringwald                 hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
56118707219SMatthias Ringwald                 att_server_t * att_server = &connection->att_server;
5626438547aSMatthias Ringwald 
56390f03c80SMatthias Ringwald                 int data_ready = att_server_data_ready_for_phase(att_server, phase);
5646438547aSMatthias Ringwald 
5656438547aSMatthias 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);
5666438547aSMatthias Ringwald 
5676438547aSMatthias Ringwald                 // skip until last sender found (which is also skipped)
5686438547aSMatthias Ringwald                 if (skip_connections_until != HCI_CON_HANDLE_INVALID){
5696438547aSMatthias Ringwald                     if (data_ready && request_con_handle == HCI_CON_HANDLE_INVALID){
5706438547aSMatthias Ringwald                         request_con_handle = att_server->connection.con_handle;
5716438547aSMatthias Ringwald                     }
5726438547aSMatthias Ringwald                     if (skip_connections_until == att_server->connection.con_handle){
5736438547aSMatthias Ringwald                         skip_connections_until = HCI_CON_HANDLE_INVALID;
5746438547aSMatthias Ringwald                     }
5756438547aSMatthias Ringwald                     continue;
5766438547aSMatthias Ringwald                 };
5776438547aSMatthias Ringwald 
57890f03c80SMatthias Ringwald                 if (data_ready){
579969a5bbaSMatthias Ringwald                     if (can_send_now){
58090f03c80SMatthias Ringwald                         att_server_trigger_send_for_phase(att_server, phase);
5816438547aSMatthias Ringwald                         last_send_con_handle = att_server->connection.con_handle;
582969a5bbaSMatthias Ringwald                         can_send_now = att_dispatch_server_can_send_now(att_server->connection.con_handle);
58390f03c80SMatthias Ringwald                         data_ready = att_server_data_ready_for_phase(att_server, phase);
5846438547aSMatthias Ringwald                         if (data_ready && request_con_handle == HCI_CON_HANDLE_INVALID){
585cbbb12d9SMatthias Ringwald                             request_con_handle = att_server->connection.con_handle;
586cbbb12d9SMatthias Ringwald                         }
587cbbb12d9SMatthias Ringwald                     } else {
588f29a88d8SMatthias Ringwald                         request_con_handle = att_server->connection.con_handle;
589f29a88d8SMatthias Ringwald                         break;
590cbbb12d9SMatthias Ringwald                     }
591cbbb12d9SMatthias Ringwald                 }
5923deb3ec6SMatthias Ringwald             }
5933deb3ec6SMatthias Ringwald 
5946438547aSMatthias Ringwald             // stop skipping (handles disconnect by last send connection)
5956438547aSMatthias Ringwald             skip_connections_until = HCI_CON_HANDLE_INVALID;
5966438547aSMatthias Ringwald 
5973551936eSMatthias Ringwald             // Exit loop, if we cannot send
598969a5bbaSMatthias Ringwald             if (!can_send_now) break;
599969a5bbaSMatthias Ringwald 
600969a5bbaSMatthias Ringwald             // Exit loop, if we can send but there are also no further request
601969a5bbaSMatthias Ringwald             if (request_con_handle == HCI_CON_HANDLE_INVALID) break;
602969a5bbaSMatthias Ringwald 
603969a5bbaSMatthias Ringwald             // Finally, if we still can send and there are requests, just try again
604969a5bbaSMatthias Ringwald             request_con_handle = HCI_CON_HANDLE_INVALID;
605969a5bbaSMatthias Ringwald         }
6066438547aSMatthias Ringwald         // update last send con handle for round robin
6076438547aSMatthias Ringwald         if (last_send_con_handle != HCI_CON_HANDLE_INVALID){
6086438547aSMatthias Ringwald             att_server_last_can_send_now = last_send_con_handle;
6096438547aSMatthias Ringwald         }
6103551936eSMatthias Ringwald     }
611969a5bbaSMatthias Ringwald 
612969a5bbaSMatthias Ringwald     if (request_con_handle == HCI_CON_HANDLE_INVALID) return;
613969a5bbaSMatthias Ringwald     att_dispatch_server_request_can_send_now_event(request_con_handle);
614969a5bbaSMatthias Ringwald }
615969a5bbaSMatthias Ringwald 
61688a48dd8SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
61718707219SMatthias Ringwald     att_server_t * att_server;
61818707219SMatthias Ringwald 
61988a48dd8SMatthias Ringwald     switch (packet_type){
62088a48dd8SMatthias Ringwald         case HCI_EVENT_PACKET:
621bd87a16eSMatthias Ringwald             switch (packet[0]){
622bd87a16eSMatthias Ringwald                 case L2CAP_EVENT_CAN_SEND_NOW:
6234d2be802SMatthias Ringwald                     att_server_handle_can_send_now();
624372d62c4SMatthias Ringwald                     break;
625bd87a16eSMatthias Ringwald                 case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
626bd87a16eSMatthias Ringwald                     // GATT client has negotiated the mtu for this connection
6276155bae0SMatthias Ringwald                     att_server = att_server_for_handle(handle);
6286155bae0SMatthias Ringwald                     if (!att_server) break;
629bd87a16eSMatthias Ringwald                     att_server->connection.mtu = little_endian_read_16(packet, 4);
630bd87a16eSMatthias Ringwald                     break;
631bd87a16eSMatthias Ringwald                 default:
632bd87a16eSMatthias Ringwald                     break;
633bd87a16eSMatthias Ringwald             }
634bd87a16eSMatthias Ringwald             break;
6353deb3ec6SMatthias Ringwald 
636372d62c4SMatthias Ringwald         case ATT_DATA_PACKET:
637f50ef7ddSMatthias Ringwald             log_debug("ATT Packet, handle 0x%04x", handle);
63818707219SMatthias Ringwald             att_server = att_server_for_handle(handle);
63918707219SMatthias Ringwald             if (!att_server) break;
64018707219SMatthias Ringwald 
6413deb3ec6SMatthias Ringwald             // handle value indication confirms
64218707219SMatthias Ringwald             if (packet[0] == ATT_HANDLE_VALUE_CONFIRMATION && att_server->value_indication_handle){
64318707219SMatthias Ringwald                 btstack_run_loop_remove_timer(&att_server->value_indication_timer);
64418707219SMatthias Ringwald                 uint16_t att_handle = att_server->value_indication_handle;
64518707219SMatthias Ringwald                 att_server->value_indication_handle = 0;
64618707219SMatthias Ringwald                 att_handle_value_indication_notify_client(0, att_server->connection.con_handle, att_handle);
647cbbb12d9SMatthias Ringwald                 att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
6483deb3ec6SMatthias Ringwald                 return;
6493deb3ec6SMatthias Ringwald             }
6503deb3ec6SMatthias Ringwald 
6516428eb5aSMatthias Ringwald             // directly process command
6526428eb5aSMatthias Ringwald             // note: signed write cannot be handled directly as authentication needs to be verified
6536428eb5aSMatthias Ringwald             if (packet[0] == ATT_WRITE_COMMAND){
65418707219SMatthias Ringwald                 att_handle_request(&att_server->connection, packet, size, 0);
6556428eb5aSMatthias Ringwald                 return;
6566428eb5aSMatthias Ringwald             }
6576428eb5aSMatthias Ringwald 
6583deb3ec6SMatthias Ringwald             // check size
65918707219SMatthias Ringwald             if (size > sizeof(att_server->request_buffer)) {
66018707219SMatthias Ringwald                 log_info("att_packet_handler: dropping att pdu 0x%02x as size %u > att_server->request_buffer %u", packet[0], size, (int) sizeof(att_server->request_buffer));
6616428eb5aSMatthias Ringwald                 return;
6626428eb5aSMatthias Ringwald             }
6633deb3ec6SMatthias Ringwald 
6643deb3ec6SMatthias Ringwald             // last request still in processing?
66518707219SMatthias Ringwald             if (att_server->state != ATT_SERVER_IDLE){
66618707219SMatthias Ringwald                 log_info("att_packet_handler: skipping att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state);
6676428eb5aSMatthias Ringwald                 return;
6686428eb5aSMatthias Ringwald             }
6693deb3ec6SMatthias Ringwald 
6703deb3ec6SMatthias Ringwald             // store request
67118707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED;
67218707219SMatthias Ringwald             att_server->request_size = size;
67318707219SMatthias Ringwald             memcpy(att_server->request_buffer, packet, size);
6743deb3ec6SMatthias Ringwald 
67518707219SMatthias Ringwald             att_run_for_context(att_server);
676372d62c4SMatthias Ringwald             break;
677372d62c4SMatthias Ringwald     }
6783deb3ec6SMatthias Ringwald }
6793deb3ec6SMatthias Ringwald 
680bf78aac6SMatthias Ringwald // ---------------------
681bf78aac6SMatthias Ringwald // persistent CCC writes
682bf78aac6SMatthias Ringwald static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){
683bf78aac6SMatthias Ringwald     return 'B' << 24 | 'T' << 16 | 'C' << 8 | index;
684bf78aac6SMatthias Ringwald }
685bf78aac6SMatthias Ringwald 
686bf78aac6SMatthias Ringwald static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){
687bf78aac6SMatthias Ringwald     // lookup att_server instance
688bf78aac6SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
689bf78aac6SMatthias Ringwald     if (!att_server) return;
690bf78aac6SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
691bf78aac6SMatthias 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);
6926a540790SMatthias Ringwald 
693bf78aac6SMatthias Ringwald     // check if bonded
694bf78aac6SMatthias Ringwald     if (le_device_index < 0) return;
6956a540790SMatthias Ringwald 
696bf78aac6SMatthias Ringwald     // get btstack_tlv
697bf78aac6SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
698bf78aac6SMatthias Ringwald     void * tlv_context;
699bf78aac6SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
700bf78aac6SMatthias Ringwald     if (!tlv_impl) return;
7016a540790SMatthias Ringwald 
702bf78aac6SMatthias Ringwald     // update ccc tag
703bf78aac6SMatthias Ringwald     int index;
7046a540790SMatthias Ringwald     uint32_t highest_seq_nr = 0;
7056a540790SMatthias Ringwald     uint32_t lowest_seq_nr = 0;
7066a540790SMatthias Ringwald     uint32_t tag_for_lowest_seq_nr = 0;
7076a540790SMatthias Ringwald     uint32_t tag_for_empty = 0;
7086a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
7096afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
710bf78aac6SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
7116a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
7126a540790SMatthias Ringwald 
7136a540790SMatthias Ringwald         // empty/invalid tag
7146a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)){
7156a540790SMatthias Ringwald             tag_for_empty = tag;
716bf78aac6SMatthias Ringwald             continue;
717bf78aac6SMatthias Ringwald         }
7186a540790SMatthias Ringwald         // update highest seq nr
7196a540790SMatthias Ringwald         if (entry.seq_nr > highest_seq_nr){
7206a540790SMatthias Ringwald             highest_seq_nr = entry.seq_nr;
7216a540790SMatthias Ringwald         }
7226a540790SMatthias Ringwald         // find entry with lowest seq nr
7236a540790SMatthias Ringwald         if ((tag_for_lowest_seq_nr == 0) || (entry.seq_nr < lowest_seq_nr)){
7246a540790SMatthias Ringwald             tag_for_lowest_seq_nr = tag;
7256a540790SMatthias Ringwald             lowest_seq_nr = entry.seq_nr;
7266a540790SMatthias Ringwald         }
7276a540790SMatthias Ringwald 
7286a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
7296a540790SMatthias Ringwald         if (entry.att_handle   != att_handle)      continue;
7306a540790SMatthias Ringwald 
7316a540790SMatthias Ringwald         // found matching entry
73201ac2008SMatthias Ringwald         if (value){
733bf78aac6SMatthias Ringwald             // update
7346a540790SMatthias Ringwald             if (entry.value == value) {
735760ad805SMatthias Ringwald                 log_info("CCC Index %u: Up-to-date", index);
736760ad805SMatthias Ringwald                 return;
737760ad805SMatthias Ringwald             }
7386a540790SMatthias Ringwald             entry.value = value;
7396a540790SMatthias Ringwald             entry.seq_nr = highest_seq_nr + 1;
740760ad805SMatthias Ringwald             log_info("CCC Index %u: Store", index);
7416a540790SMatthias Ringwald             tlv_impl->store_tag(tlv_context, tag, (const uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
74201ac2008SMatthias Ringwald         } else {
74301ac2008SMatthias Ringwald             // delete
744760ad805SMatthias Ringwald             log_info("CCC Index %u: Delete", index);
74501ac2008SMatthias Ringwald             tlv_impl->delete_tag(tlv_context, tag);
74601ac2008SMatthias Ringwald         }
747bf78aac6SMatthias Ringwald         return;
748bf78aac6SMatthias Ringwald     }
7496a540790SMatthias Ringwald 
750faf2b6c3SMatthias Ringwald     log_info("tag_for_empy %"PRIx32", tag_for_lowest_seq_nr %"PRIx32, tag_for_empty, tag_for_lowest_seq_nr);
7516a540790SMatthias Ringwald 
7526a540790SMatthias Ringwald     if (value == 0){
7536a540790SMatthias Ringwald         // done
7546a540790SMatthias Ringwald         return;
7556a540790SMatthias Ringwald     }
7566a540790SMatthias Ringwald 
7576a540790SMatthias Ringwald     uint32_t tag_to_use = 0;
7586a540790SMatthias Ringwald     if (tag_for_empty){
7596a540790SMatthias Ringwald         tag_to_use = tag_for_empty;
7606a540790SMatthias Ringwald     } else if (tag_for_lowest_seq_nr){
7616a540790SMatthias Ringwald         tag_to_use = tag_for_lowest_seq_nr;
7626a540790SMatthias Ringwald     } else {
7636a540790SMatthias Ringwald         // should not happen
7646a540790SMatthias Ringwald         return;
7656a540790SMatthias Ringwald     }
766bf78aac6SMatthias Ringwald     // store ccc tag
7676a540790SMatthias Ringwald     entry.seq_nr       = highest_seq_nr + 1;
7686a540790SMatthias Ringwald     entry.device_index = le_device_index;
7696a540790SMatthias Ringwald     entry.att_handle   = att_handle;
7706a540790SMatthias Ringwald     entry.value        = value;
7716a540790SMatthias Ringwald     tlv_impl->store_tag(tlv_context, tag_to_use, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
772bf78aac6SMatthias Ringwald }
773bf78aac6SMatthias Ringwald 
774760ad805SMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server){
775760ad805SMatthias Ringwald     if (!att_server) return;
776760ad805SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
777760ad805SMatthias Ringwald     log_info("Clear CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
778760ad805SMatthias Ringwald     // check if bonded
779760ad805SMatthias Ringwald     if (le_device_index < 0) return;
780760ad805SMatthias Ringwald     // get btstack_tlv
781760ad805SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
782760ad805SMatthias Ringwald     void * tlv_context;
783760ad805SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
784760ad805SMatthias Ringwald     if (!tlv_impl) return;
785760ad805SMatthias Ringwald     // get all ccc tag
786760ad805SMatthias Ringwald     int index;
7876a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
7886afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
789760ad805SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
7906a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
7916a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
7926a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
793760ad805SMatthias Ringwald         // delete entry
794760ad805SMatthias Ringwald         log_info("CCC Index %u: Delete", index);
795760ad805SMatthias Ringwald         tlv_impl->delete_tag(tlv_context, tag);
796760ad805SMatthias Ringwald     }
797760ad805SMatthias Ringwald }
798760ad805SMatthias Ringwald 
79901ac2008SMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server){
80001ac2008SMatthias Ringwald     if (!att_server) return;
80101ac2008SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
80201ac2008SMatthias Ringwald     log_info("Restore CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
8031b7acd0dSMatthias Ringwald     // check if bonded
8041b7acd0dSMatthias Ringwald     if (le_device_index < 0) return;
80501ac2008SMatthias Ringwald     // get btstack_tlv
80601ac2008SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
80701ac2008SMatthias Ringwald     void * tlv_context;
80801ac2008SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
80901ac2008SMatthias Ringwald     if (!tlv_impl) return;
81001ac2008SMatthias Ringwald     // get all ccc tag
81101ac2008SMatthias Ringwald     int index;
8126a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
8136afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
81401ac2008SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
8156a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
8166a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
8176a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
81801ac2008SMatthias Ringwald         // simulate write callback
8196a540790SMatthias Ringwald         uint16_t attribute_handle = entry.att_handle;
82001ac2008SMatthias Ringwald         uint8_t  value[2];
8216a540790SMatthias Ringwald         little_endian_store_16(value, 0, entry.value);
82201ac2008SMatthias Ringwald         att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
82301ac2008SMatthias Ringwald         if (!callback) continue;
8246a540790SMatthias Ringwald         log_info("CCC Index %u: Set Attribute handle 0x%04x to value 0x%04x", index, attribute_handle, entry.value );
82501ac2008SMatthias Ringwald         (*callback)(att_server->connection.con_handle, attribute_handle, ATT_TRANSACTION_MODE_NONE, 0, value, sizeof(value));
82601ac2008SMatthias Ringwald     }
82701ac2008SMatthias Ringwald }
82801ac2008SMatthias Ringwald 
829bf78aac6SMatthias Ringwald // persistent CCC writes
830bf78aac6SMatthias Ringwald // ---------------------
8313d71c7a4SMatthias Ringwald 
8323d71c7a4SMatthias Ringwald // gatt service management
8333d71c7a4SMatthias Ringwald static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){
8343d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
8353d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
8363d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
8373d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
8383d71c7a4SMatthias Ringwald         if (handler->start_handle > handle) continue;
8393d71c7a4SMatthias Ringwald         if (handler->end_handle   < handle) continue;
8403d71c7a4SMatthias Ringwald         return handler;
8413d71c7a4SMatthias Ringwald     }
8423d71c7a4SMatthias Ringwald     return NULL;
8433d71c7a4SMatthias Ringwald }
8443d71c7a4SMatthias Ringwald static att_read_callback_t att_server_read_callback_for_handle(uint16_t handle){
8453d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
8463d71c7a4SMatthias Ringwald     if (handler) return handler->read_callback;
8473d71c7a4SMatthias Ringwald     return att_server_client_read_callback;
8483d71c7a4SMatthias Ringwald }
8493d71c7a4SMatthias Ringwald 
8503d71c7a4SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){
8513d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
8523d71c7a4SMatthias Ringwald     if (handler) return handler->write_callback;
8533d71c7a4SMatthias Ringwald     return att_server_client_write_callback;
8543d71c7a4SMatthias Ringwald }
8553d71c7a4SMatthias Ringwald 
8565d07396bSMatthias Ringwald static btstack_packet_handler_t att_server_packet_handler_for_handle(uint16_t handle){
8575d07396bSMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
8585d07396bSMatthias Ringwald     if (handler) return handler->packet_handler;
8595d07396bSMatthias Ringwald     return att_client_packet_handler;
8605d07396bSMatthias Ringwald }
8615d07396bSMatthias Ringwald 
8623d71c7a4SMatthias Ringwald static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){
8633d71c7a4SMatthias Ringwald     // notify all callbacks
8643d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
8653d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
8663d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
8673d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
8683d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
8693d71c7a4SMatthias Ringwald         (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
8703d71c7a4SMatthias Ringwald     }
8713d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return;
8723d71c7a4SMatthias Ringwald     (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
8733d71c7a4SMatthias Ringwald }
8743d71c7a4SMatthias Ringwald 
8753d71c7a4SMatthias Ringwald // returns first reported error or 0
8763d71c7a4SMatthias Ringwald static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){
8773d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
8783d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
8793d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
8803d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
8813d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
8823d71c7a4SMatthias Ringwald         uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
8833d71c7a4SMatthias Ringwald         if (error_code) return error_code;
8843d71c7a4SMatthias Ringwald     }
8853d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return 0;
8863d71c7a4SMatthias Ringwald     return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
8873d71c7a4SMatthias Ringwald }
8883d71c7a4SMatthias Ringwald 
8893d71c7a4SMatthias 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){
8903d71c7a4SMatthias Ringwald     att_read_callback_t callback = att_server_read_callback_for_handle(attribute_handle);
89101ac2008SMatthias Ringwald     if (!callback) return 0;
8923d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, offset, buffer, buffer_size);
8933d71c7a4SMatthias Ringwald }
8943d71c7a4SMatthias Ringwald 
8953d71c7a4SMatthias 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){
8963d71c7a4SMatthias Ringwald     switch (transaction_mode){
8973d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_VALIDATE:
8983d71c7a4SMatthias Ringwald             return att_validate_prepared_write(con_handle);
8993d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_EXECUTE:
9003d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_CANCEL:
9013d71c7a4SMatthias Ringwald             att_notify_write_callbacks(con_handle, transaction_mode);
9023d71c7a4SMatthias Ringwald             return 0;
9033d71c7a4SMatthias Ringwald         default:
9043d71c7a4SMatthias Ringwald             break;
9053d71c7a4SMatthias Ringwald     }
9063d71c7a4SMatthias Ringwald 
907bf78aac6SMatthias Ringwald     // track CCC writes
908bf78aac6SMatthias Ringwald     if (att_is_persistent_ccc(attribute_handle) && offset == 0 && buffer_size == 2){
909bf78aac6SMatthias Ringwald         att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0));
910bf78aac6SMatthias Ringwald     }
911bf78aac6SMatthias Ringwald 
91201ac2008SMatthias Ringwald     att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
91301ac2008SMatthias Ringwald     if (!callback) return 0;
9143d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size);
9153d71c7a4SMatthias Ringwald }
9163d71c7a4SMatthias Ringwald 
9173d71c7a4SMatthias Ringwald /**
9183d71c7a4SMatthias Ringwald  * @brief register read/write callbacks for specific handle range
9193d71c7a4SMatthias Ringwald  * @param att_service_handler_t
9203d71c7a4SMatthias Ringwald  */
9213d71c7a4SMatthias Ringwald void att_server_register_service_handler(att_service_handler_t * handler){
9223d71c7a4SMatthias Ringwald     if (att_service_handler_for_handle(handler->start_handle) ||
9233d71c7a4SMatthias Ringwald         att_service_handler_for_handle(handler->end_handle)){
9243d71c7a4SMatthias Ringwald         log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle);
9253d71c7a4SMatthias Ringwald         return;
9263d71c7a4SMatthias Ringwald     }
9273d71c7a4SMatthias Ringwald     btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler);
9283d71c7a4SMatthias Ringwald }
9293d71c7a4SMatthias Ringwald 
9303deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){
9313deb3ec6SMatthias Ringwald 
9323d71c7a4SMatthias Ringwald     // store callbacks
9333d71c7a4SMatthias Ringwald     att_server_client_read_callback  = read_callback;
9343d71c7a4SMatthias Ringwald     att_server_client_write_callback = write_callback;
9353d71c7a4SMatthias Ringwald 
9361a389cdcSMatthias Ringwald     // register for HCI Events
937d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &att_event_packet_handler;
9381a389cdcSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
9391a389cdcSMatthias Ringwald 
940406722e4SMatthias Ringwald     // register for SM events
941d9a7306aSMatthias Ringwald     sm_event_callback_registration.callback = &att_event_packet_handler;
942406722e4SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
9433deb3ec6SMatthias Ringwald 
9441a389cdcSMatthias Ringwald     // and L2CAP ATT Server PDUs
9453deb3ec6SMatthias Ringwald     att_dispatch_register_server(att_packet_handler);
9463deb3ec6SMatthias Ringwald 
9473deb3ec6SMatthias Ringwald     att_set_db(db);
9483d71c7a4SMatthias Ringwald     att_set_read_callback(att_server_read_callback);
9493d71c7a4SMatthias Ringwald     att_set_write_callback(att_server_write_callback);
9503deb3ec6SMatthias Ringwald 
9513deb3ec6SMatthias Ringwald }
9523deb3ec6SMatthias Ringwald 
9533deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){
9543deb3ec6SMatthias Ringwald     att_client_packet_handler = handler;
9553deb3ec6SMatthias Ringwald }
9563deb3ec6SMatthias Ringwald 
957cbbb12d9SMatthias Ringwald 
958cbbb12d9SMatthias Ringwald // to be deprecated
959c141988cSMatthias Ringwald int  att_server_can_send_packet_now(hci_con_handle_t con_handle){
96018707219SMatthias Ringwald     return att_dispatch_server_can_send_now(con_handle);
9611b96b007SMatthias Ringwald }
9621b96b007SMatthias Ringwald 
963cbbb12d9SMatthias Ringwald int att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
964cbbb12d9SMatthias Ringwald     return att_server_request_to_send_notification(callback_registration, con_handle);
9653deb3ec6SMatthias Ringwald }
9663deb3ec6SMatthias Ringwald 
967cbbb12d9SMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){
968cbbb12d9SMatthias Ringwald     att_client_waiting_for_can_send_registration.callback = &att_emit_can_send_now_event;
969cbbb12d9SMatthias Ringwald     att_server_request_to_send_notification(&att_client_waiting_for_can_send_registration, con_handle);
970cbbb12d9SMatthias Ringwald }
971cbbb12d9SMatthias Ringwald // end of deprecated
972cbbb12d9SMatthias Ringwald 
973cbbb12d9SMatthias Ringwald int att_server_request_to_send_notification(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
974969a5bbaSMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
975969a5bbaSMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
976cbbb12d9SMatthias Ringwald     btstack_linked_list_add_tail(&att_server->notification_requests, (btstack_linked_item_t*) callback_registration);
977cbbb12d9SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
978cbbb12d9SMatthias Ringwald     return ERROR_CODE_SUCCESS;
979cbbb12d9SMatthias Ringwald }
980cbbb12d9SMatthias Ringwald 
981cbbb12d9SMatthias Ringwald int att_server_request_to_send_indication(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
982cbbb12d9SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
983cbbb12d9SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
984cbbb12d9SMatthias Ringwald     btstack_linked_list_add_tail(&att_server->indication_requests, (btstack_linked_item_t*) callback_registration);
985bb38f057SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
986969a5bbaSMatthias Ringwald     return ERROR_CODE_SUCCESS;
987bb38f057SMatthias Ringwald }
988bb38f057SMatthias Ringwald 
9893bb3035fSMilanka Ringwald int att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){
99018707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
99118707219SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
99218707219SMatthias Ringwald 
99318707219SMatthias Ringwald     if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
9943deb3ec6SMatthias Ringwald 
9953deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
9963deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
99718707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_notification(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
99818707219SMatthias Ringwald 	return l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
9993deb3ec6SMatthias Ringwald }
10003deb3ec6SMatthias Ringwald 
10013bb3035fSMilanka Ringwald int att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, const uint8_t *value, uint16_t value_len){
100218707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
100318707219SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
100418707219SMatthias Ringwald 
1005eaac31e8SMatthias Ringwald     if (att_server->value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS;
100618707219SMatthias Ringwald     if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
10073deb3ec6SMatthias Ringwald 
10083deb3ec6SMatthias Ringwald     // track indication
100918707219SMatthias Ringwald     att_server->value_indication_handle = attribute_handle;
101018707219SMatthias Ringwald     btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout);
101118707219SMatthias Ringwald     btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS);
101218707219SMatthias Ringwald     btstack_run_loop_add_timer(&att_server->value_indication_timer);
10133deb3ec6SMatthias Ringwald 
10143deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
10153deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
101618707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_indication(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
101718707219SMatthias Ringwald 	l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
10183deb3ec6SMatthias Ringwald     return 0;
10193deb3ec6SMatthias Ringwald }
1020*8f9132b5SMilanka Ringwald 
1021*8f9132b5SMilanka Ringwald uint16_t att_server_get_mtu(hci_con_handle_t con_handle){
1022*8f9132b5SMilanka Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
1023*8f9132b5SMilanka Ringwald     if (!att_server) return 0;
1024*8f9132b5SMilanka Ringwald     return att_server->connection.mtu;
1025*8f9132b5SMilanka Ringwald }