xref: /btstack/src/ble/att_server.c (revision fa5e3464655894dcb2606444fca382b824e7ac69)
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);
7801ac2008SMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server);
79760ad805SMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server);
803deb3ec6SMatthias Ringwald 
813551936eSMatthias Ringwald typedef enum {
823551936eSMatthias Ringwald     ATT_SERVER_RUN_PHASE_1_REQUESTS,
833551936eSMatthias Ringwald     ATT_SERVER_RUN_PHASE_2_INDICATIONS,
843551936eSMatthias Ringwald     ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS,
853551936eSMatthias Ringwald } att_server_run_phase_t;
863551936eSMatthias Ringwald 
876a540790SMatthias Ringwald //
886a540790SMatthias Ringwald typedef struct {
896a540790SMatthias Ringwald     uint32_t seq_nr;
906a540790SMatthias Ringwald     uint16_t att_handle;
916a540790SMatthias Ringwald     uint8_t  value;
926a540790SMatthias Ringwald     uint8_t  device_index;
936a540790SMatthias Ringwald } persistent_ccc_entry_t;
946a540790SMatthias Ringwald 
9518707219SMatthias Ringwald // global
961a389cdcSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
97406722e4SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration;
983deb3ec6SMatthias Ringwald static btstack_packet_handler_t               att_client_packet_handler = NULL;
993d71c7a4SMatthias Ringwald static btstack_linked_list_t                  service_handlers;
1005f141511SMatthias Ringwald static btstack_context_callback_registration_t att_client_waiting_for_can_send_registration;
10118707219SMatthias Ringwald 
1023d71c7a4SMatthias Ringwald static att_read_callback_t                    att_server_client_read_callback;
1033d71c7a4SMatthias Ringwald static att_write_callback_t                   att_server_client_write_callback;
1043d71c7a4SMatthias Ringwald 
1056438547aSMatthias Ringwald // round robin
1066438547aSMatthias Ringwald static hci_con_handle_t att_server_last_can_send_now = HCI_CON_HANDLE_INVALID;
107bf78aac6SMatthias Ringwald 
10818707219SMatthias Ringwald static att_server_t * att_server_for_handle(hci_con_handle_t con_handle){
10918707219SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
11018707219SMatthias Ringwald     if (!hci_connection) return NULL;
11118707219SMatthias Ringwald     return &hci_connection->att_server;
11218707219SMatthias Ringwald }
11318707219SMatthias Ringwald 
11402b78fc8SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
11518707219SMatthias Ringwald static att_server_t * att_server_for_state(att_server_state_t state){
11618707219SMatthias Ringwald     btstack_linked_list_iterator_t it;
11718707219SMatthias Ringwald     hci_connections_get_iterator(&it);
11818707219SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
11918707219SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
12018707219SMatthias Ringwald         att_server_t * att_server = &connection->att_server;
12118707219SMatthias Ringwald         if (att_server->state == state) return att_server;
12218707219SMatthias Ringwald     }
12318707219SMatthias Ringwald     return NULL;
12418707219SMatthias Ringwald }
12502b78fc8SMatthias Ringwald #endif
126bb38f057SMatthias Ringwald 
1273deb3ec6SMatthias Ringwald static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){
1283deb3ec6SMatthias Ringwald     if (!att_client_packet_handler) return;
1293deb3ec6SMatthias Ringwald 
1303deb3ec6SMatthias Ringwald     uint8_t event[7];
1313deb3ec6SMatthias Ringwald     int pos = 0;
1325611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE;
1333deb3ec6SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
1343deb3ec6SMatthias Ringwald     event[pos++] = status;
135f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, client_handle);
1363deb3ec6SMatthias Ringwald     pos += 2;
137f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, attribute_handle);
1383deb3ec6SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1393deb3ec6SMatthias Ringwald }
1403deb3ec6SMatthias Ringwald 
141fc64f94aSMatthias Ringwald static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){
1423deb3ec6SMatthias Ringwald     if (!att_client_packet_handler) return;
1433deb3ec6SMatthias Ringwald 
1443deb3ec6SMatthias Ringwald     uint8_t event[6];
1453deb3ec6SMatthias Ringwald     int pos = 0;
1465611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE;
1473deb3ec6SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
148fc64f94aSMatthias Ringwald     little_endian_store_16(event, pos, con_handle);
1493deb3ec6SMatthias Ringwald     pos += 2;
150f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, mtu);
151b12646c5SJakob Krantz     att_dispatch_server_mtu_exchanged(con_handle, mtu);
1523deb3ec6SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1533deb3ec6SMatthias Ringwald }
1543deb3ec6SMatthias Ringwald 
1555f141511SMatthias Ringwald static void att_emit_can_send_now_event(void * context){
1565f141511SMatthias Ringwald     UNUSED(context);
1573c97b242SMatthias Ringwald     if (!att_client_packet_handler) return;
1583c97b242SMatthias Ringwald 
1591b96b007SMatthias Ringwald     uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0};
1601b96b007SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1611b96b007SMatthias Ringwald }
1621b96b007SMatthias Ringwald 
163ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){
16454178543SMatthias Ringwald     void * context = btstack_run_loop_get_timer_context(ts);
16554178543SMatthias Ringwald     hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
16618707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
16718707219SMatthias Ringwald     if (!att_server) return;
16818707219SMatthias Ringwald     uint16_t att_handle = att_server->value_indication_handle;
16918707219SMatthias Ringwald     att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_server->connection.con_handle, att_handle);
1703deb3ec6SMatthias Ringwald }
1713deb3ec6SMatthias Ringwald 
1723deb3ec6SMatthias Ringwald static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1739ec2630cSMatthias Ringwald 
1747dad9ff8SMatthias Ringwald     UNUSED(channel); // ok: there is no channel
1757dad9ff8SMatthias Ringwald     UNUSED(size);    // ok: handling own l2cap events
1763deb3ec6SMatthias Ringwald 
17718707219SMatthias Ringwald     att_server_t * att_server;
17818707219SMatthias Ringwald     hci_con_handle_t con_handle;
17918707219SMatthias Ringwald 
1803deb3ec6SMatthias Ringwald     switch (packet_type) {
1813deb3ec6SMatthias Ringwald 
1823deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1830e2df43fSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
1843deb3ec6SMatthias Ringwald 
1853deb3ec6SMatthias Ringwald                 case HCI_EVENT_LE_META:
1863deb3ec6SMatthias Ringwald                     switch (packet[2]) {
1873deb3ec6SMatthias Ringwald                         case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
18818707219SMatthias Ringwald                             con_handle = little_endian_read_16(packet, 4);
18918707219SMatthias Ringwald                             att_server = att_server_for_handle(con_handle);
19018707219SMatthias Ringwald                             if (!att_server) break;
1913deb3ec6SMatthias Ringwald                         	// store connection info
19218707219SMatthias Ringwald                         	att_server->peer_addr_type = packet[7];
19318707219SMatthias Ringwald                             reverse_bd_addr(&packet[8], att_server->peer_address);
19418707219SMatthias Ringwald                             att_server->connection.con_handle = con_handle;
1953deb3ec6SMatthias Ringwald                             // reset connection properties
19618707219SMatthias Ringwald                             att_server->state = ATT_SERVER_IDLE;
19718707219SMatthias Ringwald                             att_server->connection.mtu = ATT_DEFAULT_MTU;
19818707219SMatthias Ringwald                             att_server->connection.max_mtu = l2cap_max_le_mtu();
19918707219SMatthias Ringwald                             if (att_server->connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){
20018707219SMatthias Ringwald                                 att_server->connection.max_mtu = ATT_REQUEST_BUFFER_SIZE;
20199c58ad0SMatthias Ringwald                             }
20218707219SMatthias Ringwald                             att_server->connection.encryption_key_size = 0;
20318707219SMatthias Ringwald                             att_server->connection.authenticated = 0;
20418707219SMatthias Ringwald 		                	att_server->connection.authorized = 0;
205b2c1e52cSMatthias Ringwald                             // workaround: identity resolving can already be complete, at least store result
206b2c1e52cSMatthias Ringwald                             att_server->ir_le_device_db_index = sm_le_device_index(con_handle);
207760ad805SMatthias Ringwald                             att_server->pairing_active = 0;
2083deb3ec6SMatthias Ringwald                             break;
2093deb3ec6SMatthias Ringwald 
2103deb3ec6SMatthias Ringwald                         default:
2113deb3ec6SMatthias Ringwald                             break;
2123deb3ec6SMatthias Ringwald                     }
2133deb3ec6SMatthias Ringwald                     break;
2143deb3ec6SMatthias Ringwald 
2153deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_CHANGE:
2163deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE:
2173deb3ec6SMatthias Ringwald                 	// check handle
21818707219SMatthias Ringwald                     con_handle = little_endian_read_16(packet, 3);
21918707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
22018707219SMatthias Ringwald                     if (!att_server) break;
2219c6e867eSMatthias Ringwald                     att_server->connection.encryption_key_size = gap_encryption_key_size(con_handle);
2229c6e867eSMatthias Ringwald                     att_server->connection.authenticated = gap_authenticated(con_handle);
22301ac2008SMatthias Ringwald                     if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){
22401ac2008SMatthias Ringwald                         // restore CCC values when encrypted
22501ac2008SMatthias Ringwald                         if (hci_event_encryption_change_get_encryption_enabled(packet)){
22601ac2008SMatthias Ringwald                             att_server_persistent_ccc_restore(att_server);
22701ac2008SMatthias Ringwald                         }
22801ac2008SMatthias Ringwald                     }
2290234fbbcSMatthias Ringwald                     att_run_for_context(att_server);
2303deb3ec6SMatthias Ringwald                     break;
2313deb3ec6SMatthias Ringwald 
2323deb3ec6SMatthias Ringwald                 case HCI_EVENT_DISCONNECTION_COMPLETE:
23370a390c7SMatthias Ringwald                     // check handle
23418707219SMatthias Ringwald                     con_handle = hci_event_disconnection_complete_get_connection_handle(packet);
23518707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
23618707219SMatthias Ringwald                     if (!att_server) break;
23718707219SMatthias Ringwald                     att_clear_transaction_queue(&att_server->connection);
23818707219SMatthias Ringwald                     att_server->connection.con_handle = 0;
23918707219SMatthias Ringwald                     att_server->value_indication_handle = 0; // reset error state
240760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
24118707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
2423deb3ec6SMatthias Ringwald                     break;
2433deb3ec6SMatthias Ringwald 
244760ad805SMatthias Ringwald                 // Identity Resolving
2455611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_STARTED:
24618707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_started_get_handle(packet);
24718707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
24818707219SMatthias Ringwald                     if (!att_server) break;
2495611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED");
25018707219SMatthias Ringwald                     att_server->ir_lookup_active = 1;
2513deb3ec6SMatthias Ringwald                     break;
2525611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
253760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
254760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
255760ad805SMatthias Ringwald                     if (!att_server) return;
2561b7acd0dSMatthias Ringwald                     att_server->ir_lookup_active = 0;
2571b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_resolving_succeeded_get_index(packet);
2581b7acd0dSMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED");
2591b7acd0dSMatthias Ringwald                     att_run_for_context(att_server);
2603deb3ec6SMatthias Ringwald                     break;
2615611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_FAILED:
26218707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_failed_get_handle(packet);
26318707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
26418707219SMatthias Ringwald                     if (!att_server) break;
2655611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED");
26618707219SMatthias Ringwald                     att_server->ir_lookup_active = 0;
26718707219SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
26818707219SMatthias Ringwald                     att_run_for_context(att_server);
2693deb3ec6SMatthias Ringwald                     break;
270760ad805SMatthias Ringwald 
271760ad805SMatthias Ringwald                 // Pairing started - delete stored CCC values
272760ad805SMatthias Ringwald                 // - assumes pairing indicates either new device or re-pairing, in both cases there should be no stored CCC values
273760ad805SMatthias Ringwald                 // - assumes that all events have the con handle as the first field
274760ad805SMatthias Ringwald                 case SM_EVENT_JUST_WORKS_REQUEST:
275760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_DISPLAY_NUMBER:
276760ad805SMatthias Ringwald                 case SM_EVENT_PASSKEY_INPUT_NUMBER:
277760ad805SMatthias Ringwald                 case SM_EVENT_NUMERIC_COMPARISON_REQUEST:
278760ad805SMatthias Ringwald                     con_handle = sm_event_just_works_request_get_handle(packet);
279760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
280760ad805SMatthias Ringwald                     if (!att_server) break;
281760ad805SMatthias Ringwald                     att_server->pairing_active = 1;
282760ad805SMatthias Ringwald                     log_info("SM Pairing started");
283760ad805SMatthias Ringwald                     if (att_server->ir_le_device_db_index < 0) break;
284760ad805SMatthias Ringwald                     att_server_persistent_ccc_clear(att_server);
285760ad805SMatthias Ringwald                     // index not valid anymore
286760ad805SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
287760ad805SMatthias Ringwald                     break;
288760ad805SMatthias Ringwald 
2891b7acd0dSMatthias Ringwald                 // Bonding completed
290760ad805SMatthias Ringwald                 case SM_EVENT_IDENTITY_CREATED:
291760ad805SMatthias Ringwald                     con_handle = sm_event_identity_created_get_handle(packet);
292760ad805SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
293760ad805SMatthias Ringwald                     if (!att_server) return;
294760ad805SMatthias Ringwald                     att_server->pairing_active = 0;
2951b7acd0dSMatthias Ringwald                     att_server->ir_le_device_db_index = sm_event_identity_created_get_index(packet);
2961b7acd0dSMatthias Ringwald                     att_run_for_context(att_server);
2971b7acd0dSMatthias Ringwald                     break;
2981b7acd0dSMatthias Ringwald 
2991b7acd0dSMatthias Ringwald                 // Pairing complete (with/without bonding=storing of pairing information)
3001b7acd0dSMatthias Ringwald                 case SM_EVENT_PAIRING_COMPLETE:
3011b7acd0dSMatthias Ringwald                     con_handle = sm_event_pairing_complete_get_handle(packet);
3021b7acd0dSMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
3031b7acd0dSMatthias Ringwald                     if (!att_server) return;
3041b7acd0dSMatthias Ringwald                     att_server->pairing_active = 0;
3051b7acd0dSMatthias Ringwald                     att_run_for_context(att_server);
306760ad805SMatthias Ringwald                     break;
307760ad805SMatthias Ringwald 
308760ad805SMatthias Ringwald                 // Authorization
3095611a760SMatthias Ringwald                 case SM_EVENT_AUTHORIZATION_RESULT: {
31018707219SMatthias Ringwald                     con_handle = sm_event_authorization_result_get_handle(packet);
31118707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
31218707219SMatthias Ringwald                     if (!att_server) break;
31318707219SMatthias Ringwald                     att_server->connection.authorized = sm_event_authorization_result_get_authorization_result(packet);
31418707219SMatthias Ringwald                     att_dispatch_server_request_can_send_now_event(con_handle);
3153deb3ec6SMatthias Ringwald                 	break;
3163deb3ec6SMatthias Ringwald                 }
3173deb3ec6SMatthias Ringwald                 default:
3183deb3ec6SMatthias Ringwald                     break;
3193deb3ec6SMatthias Ringwald             }
32065b44ffdSMatthias Ringwald             break;
32165b44ffdSMatthias Ringwald         default:
32265b44ffdSMatthias Ringwald             break;
3233deb3ec6SMatthias Ringwald     }
3243deb3ec6SMatthias Ringwald }
3253deb3ec6SMatthias Ringwald 
3267a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
3273deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
3283deb3ec6SMatthias Ringwald 
32918707219SMatthias Ringwald     att_server_t * att_server = att_server_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION);
33018707219SMatthias Ringwald     if (!att_server) return;
3313deb3ec6SMatthias Ringwald 
3323deb3ec6SMatthias Ringwald     uint8_t hash_flipped[8];
3339c80e4ccSMatthias Ringwald     reverse_64(hash, hash_flipped);
33418707219SMatthias Ringwald     if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8)){
3353deb3ec6SMatthias Ringwald         log_info("ATT Signed Write, invalid signature");
33618707219SMatthias Ringwald         att_server->state = ATT_SERVER_IDLE;
3373deb3ec6SMatthias Ringwald         return;
3383deb3ec6SMatthias Ringwald     }
3393deb3ec6SMatthias Ringwald     log_info("ATT Signed Write, valid signature");
3403deb3ec6SMatthias Ringwald 
3413deb3ec6SMatthias Ringwald     // update sequence number
34218707219SMatthias Ringwald     uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
34318707219SMatthias Ringwald     le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1);
34418707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
34518707219SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
3463deb3ec6SMatthias Ringwald }
3477a766ebfSMatthias Ringwald #endif
34888a48dd8SMatthias Ringwald 
34918707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED
350b06f2579SMatthias Ringwald // pre: can send now
351b06f2579SMatthias Ringwald // returns: 1 if packet was sent
35218707219SMatthias Ringwald static int att_server_process_validated_request(att_server_t * att_server){
353b06f2579SMatthias Ringwald 
354b06f2579SMatthias Ringwald     l2cap_reserve_packet_buffer();
355b06f2579SMatthias Ringwald     uint8_t * att_response_buffer = l2cap_get_outgoing_buffer();
35618707219SMatthias Ringwald     uint16_t  att_response_size   = att_handle_request(&att_server->connection, att_server->request_buffer, att_server->request_size, att_response_buffer);
357b06f2579SMatthias Ringwald 
358e404a688SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_READ_RESPONSE
359e404a688SMatthias Ringwald     if (att_response_size == ATT_READ_RESPONSE_PENDING){
360e404a688SMatthias Ringwald         // update state
361e404a688SMatthias Ringwald         att_server->state = ATT_SERVER_READ_RESPONSE_PENDING;
362e404a688SMatthias Ringwald 
363e404a688SMatthias Ringwald         // callback with handle ATT_READ_RESPONSE_PENDING
364e404a688SMatthias Ringwald         att_server_client_read_callback(att_server->connection.con_handle, ATT_READ_RESPONSE_PENDING, 0, NULL, 0);
365e8e7403eSMatthias Ringwald 
366e8e7403eSMatthias Ringwald         // free reserved buffer
367e8e7403eSMatthias Ringwald         l2cap_release_packet_buffer();
368e8e7403eSMatthias Ringwald         return 0;
369e404a688SMatthias Ringwald     }
370e404a688SMatthias Ringwald #endif
371e404a688SMatthias Ringwald 
372b06f2579SMatthias Ringwald     // intercept "insufficient authorization" for authenticated connections to allow for user authorization
373b06f2579SMatthias Ringwald     if ((att_response_size     >= 4)
374b06f2579SMatthias Ringwald     && (att_response_buffer[0] == ATT_ERROR_RESPONSE)
375b06f2579SMatthias Ringwald     && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION)
37618707219SMatthias Ringwald     && (att_server->connection.authenticated)){
377b06f2579SMatthias Ringwald 
3789c6e867eSMatthias Ringwald         switch (gap_authorization_state(att_server->connection.con_handle)){
379b06f2579SMatthias Ringwald             case AUTHORIZATION_UNKNOWN:
380b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
38118707219SMatthias Ringwald                 sm_request_pairing(att_server->connection.con_handle);
382b06f2579SMatthias Ringwald                 return 0;
383b06f2579SMatthias Ringwald             case AUTHORIZATION_PENDING:
384b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
385b06f2579SMatthias Ringwald                 return 0;
386b06f2579SMatthias Ringwald             default:
387b06f2579SMatthias Ringwald                 break;
388b06f2579SMatthias Ringwald         }
389b06f2579SMatthias Ringwald     }
390b06f2579SMatthias Ringwald 
39118707219SMatthias Ringwald     att_server->state = ATT_SERVER_IDLE;
392b06f2579SMatthias Ringwald     if (att_response_size == 0) {
393b06f2579SMatthias Ringwald         l2cap_release_packet_buffer();
394b06f2579SMatthias Ringwald         return 0;
395b06f2579SMatthias Ringwald     }
396b06f2579SMatthias Ringwald 
39718707219SMatthias Ringwald     l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size);
398b06f2579SMatthias Ringwald 
399b06f2579SMatthias Ringwald     // notify client about MTU exchange result
400b06f2579SMatthias Ringwald     if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){
40118707219SMatthias Ringwald         att_emit_mtu_event(att_server->connection.con_handle, att_server->connection.mtu);
402b06f2579SMatthias Ringwald     }
403b06f2579SMatthias Ringwald     return 1;
404b06f2579SMatthias Ringwald }
405b06f2579SMatthias Ringwald 
406e404a688SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_READ_RESPONSE
407e404a688SMatthias Ringwald int att_server_read_response_ready(hci_con_handle_t con_handle){
408e404a688SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
409e404a688SMatthias Ringwald     if (!att_server)                                             return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
410e404a688SMatthias Ringwald     if (att_server->state != ATT_SERVER_READ_RESPONSE_PENDING)   return ERROR_CODE_COMMAND_DISALLOWED;
411e404a688SMatthias Ringwald 
412e404a688SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
413e404a688SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
414e404a688SMatthias Ringwald     return ERROR_CODE_SUCCESS;
415e404a688SMatthias Ringwald }
416e404a688SMatthias Ringwald #endif
417e404a688SMatthias Ringwald 
41818707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server){
41918707219SMatthias Ringwald     switch (att_server->state){
4203deb3ec6SMatthias Ringwald         case ATT_SERVER_REQUEST_RECEIVED:
421760ad805SMatthias Ringwald 
4220234fbbcSMatthias Ringwald             // wait until re-encryption as central is complete
4230234fbbcSMatthias Ringwald             if (gap_reconnect_security_setup_active(att_server->connection.con_handle)) break;
4240234fbbcSMatthias Ringwald 
425760ad805SMatthias Ringwald             // wait until pairing is complete
426760ad805SMatthias Ringwald             if (att_server->pairing_active) break;
427760ad805SMatthias Ringwald 
4287a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
42918707219SMatthias Ringwald             if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){
4303deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write!");
4313deb3ec6SMatthias Ringwald                 if (!sm_cmac_ready()) {
4323deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, sm_cmac engine not ready. Abort");
43318707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
4343deb3ec6SMatthias Ringwald                     return;
4353deb3ec6SMatthias Ringwald                 }
43618707219SMatthias Ringwald                 if (att_server->request_size < (3 + 12)) {
4373deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, request to short. Abort.");
43818707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
4393deb3ec6SMatthias Ringwald                     return;
4403deb3ec6SMatthias Ringwald                 }
44118707219SMatthias Ringwald                 if (att_server->ir_lookup_active){
4423deb3ec6SMatthias Ringwald                     return;
4433deb3ec6SMatthias Ringwald                 }
44418707219SMatthias Ringwald                 if (att_server->ir_le_device_db_index < 0){
4453deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, CSRK not available");
44618707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
4473deb3ec6SMatthias Ringwald                     return;
4483deb3ec6SMatthias Ringwald                 }
4493deb3ec6SMatthias Ringwald 
4503deb3ec6SMatthias Ringwald                 // check counter
45118707219SMatthias Ringwald                 uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
45218707219SMatthias Ringwald                 uint32_t counter_db     = le_device_db_remote_counter_get(att_server->ir_le_device_db_index);
4533deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet);
4543deb3ec6SMatthias Ringwald                 if (counter_packet < counter_db){
4553deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, db reports higher counter, abort");
45618707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
4573deb3ec6SMatthias Ringwald                     return;
4583deb3ec6SMatthias Ringwald                 }
4593deb3ec6SMatthias Ringwald 
4603deb3ec6SMatthias Ringwald                 // signature is { sequence counter, secure hash }
4613deb3ec6SMatthias Ringwald                 sm_key_t csrk;
46218707219SMatthias Ringwald                 le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk);
46318707219SMatthias Ringwald                 att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION;
4643deb3ec6SMatthias Ringwald                 log_info("Orig Signature: ");
46518707219SMatthias Ringwald                 log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8);
46618707219SMatthias Ringwald                 uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1);
46718707219SMatthias 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);
4683deb3ec6SMatthias Ringwald                 return;
4693deb3ec6SMatthias Ringwald             }
4707a766ebfSMatthias Ringwald #endif
471b06f2579SMatthias Ringwald             // move on
47218707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
47318707219SMatthias Ringwald             att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
474b06f2579SMatthias Ringwald             break;
47588a48dd8SMatthias Ringwald 
4763deb3ec6SMatthias Ringwald         default:
4773deb3ec6SMatthias Ringwald             break;
4783deb3ec6SMatthias Ringwald     }
4793deb3ec6SMatthias Ringwald }
4803deb3ec6SMatthias Ringwald 
48190f03c80SMatthias Ringwald static int att_server_data_ready_for_phase(att_server_t * att_server,  att_server_run_phase_t phase){
48290f03c80SMatthias Ringwald     switch (phase){
48390f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
48490f03c80SMatthias Ringwald             return att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
48590f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
48690f03c80SMatthias Ringwald              return (!btstack_linked_list_empty(&att_server->indication_requests) && att_server->value_indication_handle == 0);
48790f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
48890f03c80SMatthias Ringwald             return (!btstack_linked_list_empty(&att_server->notification_requests));
48990f03c80SMatthias Ringwald     }
490*fa5e3464SMatthias Ringwald     // avoid warning
491*fa5e3464SMatthias Ringwald     return 0;
49290f03c80SMatthias Ringwald }
49390f03c80SMatthias Ringwald 
49490f03c80SMatthias Ringwald static void att_server_trigger_send_for_phase(att_server_t * att_server,  att_server_run_phase_t phase){
49590f03c80SMatthias Ringwald     btstack_context_callback_registration_t * client;
49690f03c80SMatthias Ringwald     switch (phase){
49790f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_1_REQUESTS:
49890f03c80SMatthias Ringwald             att_server_process_validated_request(att_server);
49990f03c80SMatthias Ringwald             break;
50090f03c80SMatthias Ringwald         case ATT_SERVER_RUN_PHASE_2_INDICATIONS:
50190f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->indication_requests;
50290f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->indication_requests, (btstack_linked_item_t *) client);
50390f03c80SMatthias Ringwald             client->callback(client->context);
50490f03c80SMatthias Ringwald             break;
50590f03c80SMatthias Ringwald        case ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS:
50690f03c80SMatthias Ringwald             client = (btstack_context_callback_registration_t*) att_server->notification_requests;
50790f03c80SMatthias Ringwald             btstack_linked_list_remove(&att_server->notification_requests, (btstack_linked_item_t *) client);
50890f03c80SMatthias Ringwald             client->callback(client->context);
50990f03c80SMatthias Ringwald             break;
51090f03c80SMatthias Ringwald     }
51190f03c80SMatthias Ringwald }
51290f03c80SMatthias Ringwald 
5137eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){
514b06f2579SMatthias Ringwald 
515374a288aSMatthias Ringwald     hci_con_handle_t request_con_handle   = HCI_CON_HANDLE_INVALID;
5166438547aSMatthias Ringwald     hci_con_handle_t last_send_con_handle = HCI_CON_HANDLE_INVALID;
5176438547aSMatthias Ringwald     int can_send_now = 1;
5183551936eSMatthias Ringwald     int phase;
5196438547aSMatthias Ringwald 
5203551936eSMatthias Ringwald     for (phase = ATT_SERVER_RUN_PHASE_1_REQUESTS; phase <= ATT_SERVER_RUN_PHASE_3_NOTIFICATIONS; phase++){
5216438547aSMatthias Ringwald         hci_con_handle_t skip_connections_until = att_server_last_can_send_now;
522374a288aSMatthias Ringwald         while (1){
5236438547aSMatthias Ringwald             btstack_linked_list_iterator_t it;
52418707219SMatthias Ringwald             hci_connections_get_iterator(&it);
52518707219SMatthias Ringwald             while(btstack_linked_list_iterator_has_next(&it)){
52618707219SMatthias Ringwald                 hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
52718707219SMatthias Ringwald                 att_server_t * att_server = &connection->att_server;
5286438547aSMatthias Ringwald 
52990f03c80SMatthias Ringwald                 int data_ready = att_server_data_ready_for_phase(att_server, phase);
5306438547aSMatthias Ringwald 
5316438547aSMatthias 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);
5326438547aSMatthias Ringwald 
5336438547aSMatthias Ringwald                 // skip until last sender found (which is also skipped)
5346438547aSMatthias Ringwald                 if (skip_connections_until != HCI_CON_HANDLE_INVALID){
5356438547aSMatthias Ringwald                     if (data_ready && request_con_handle == HCI_CON_HANDLE_INVALID){
5366438547aSMatthias Ringwald                         request_con_handle = att_server->connection.con_handle;
5376438547aSMatthias Ringwald                     }
5386438547aSMatthias Ringwald                     if (skip_connections_until == att_server->connection.con_handle){
5396438547aSMatthias Ringwald                         skip_connections_until = HCI_CON_HANDLE_INVALID;
5406438547aSMatthias Ringwald                     }
5416438547aSMatthias Ringwald                     continue;
5426438547aSMatthias Ringwald                 };
5436438547aSMatthias Ringwald 
54490f03c80SMatthias Ringwald                 if (data_ready){
545969a5bbaSMatthias Ringwald                     if (can_send_now){
54690f03c80SMatthias Ringwald                         att_server_trigger_send_for_phase(att_server, phase);
5476438547aSMatthias Ringwald                         last_send_con_handle = att_server->connection.con_handle;
548969a5bbaSMatthias Ringwald                         can_send_now = att_dispatch_server_can_send_now(att_server->connection.con_handle);
54990f03c80SMatthias Ringwald                         data_ready = att_server_data_ready_for_phase(att_server, phase);
5506438547aSMatthias Ringwald                         if (data_ready && request_con_handle == HCI_CON_HANDLE_INVALID){
551cbbb12d9SMatthias Ringwald                             request_con_handle = att_server->connection.con_handle;
552cbbb12d9SMatthias Ringwald                         }
553cbbb12d9SMatthias Ringwald                     } else {
554f29a88d8SMatthias Ringwald                         request_con_handle = att_server->connection.con_handle;
555f29a88d8SMatthias Ringwald                         break;
556cbbb12d9SMatthias Ringwald                     }
557cbbb12d9SMatthias Ringwald                 }
5583deb3ec6SMatthias Ringwald             }
5593deb3ec6SMatthias Ringwald 
5606438547aSMatthias Ringwald             // stop skipping (handles disconnect by last send connection)
5616438547aSMatthias Ringwald             skip_connections_until = HCI_CON_HANDLE_INVALID;
5626438547aSMatthias Ringwald 
5633551936eSMatthias Ringwald             // Exit loop, if we cannot send
564969a5bbaSMatthias Ringwald             if (!can_send_now) break;
565969a5bbaSMatthias Ringwald 
566969a5bbaSMatthias Ringwald             // Exit loop, if we can send but there are also no further request
567969a5bbaSMatthias Ringwald             if (request_con_handle == HCI_CON_HANDLE_INVALID) break;
568969a5bbaSMatthias Ringwald 
569969a5bbaSMatthias Ringwald             // Finally, if we still can send and there are requests, just try again
570969a5bbaSMatthias Ringwald             request_con_handle = HCI_CON_HANDLE_INVALID;
571969a5bbaSMatthias Ringwald         }
5726438547aSMatthias Ringwald         // update last send con handle for round robin
5736438547aSMatthias Ringwald         if (last_send_con_handle != HCI_CON_HANDLE_INVALID){
5746438547aSMatthias Ringwald             att_server_last_can_send_now = last_send_con_handle;
5756438547aSMatthias Ringwald         }
5763551936eSMatthias Ringwald     }
577969a5bbaSMatthias Ringwald 
578969a5bbaSMatthias Ringwald     if (request_con_handle == HCI_CON_HANDLE_INVALID) return;
579969a5bbaSMatthias Ringwald     att_dispatch_server_request_can_send_now_event(request_con_handle);
580969a5bbaSMatthias Ringwald }
581969a5bbaSMatthias Ringwald 
58288a48dd8SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
58318707219SMatthias Ringwald 
58418707219SMatthias Ringwald     att_server_t * att_server;
58518707219SMatthias Ringwald 
58688a48dd8SMatthias Ringwald     switch (packet_type){
58788a48dd8SMatthias Ringwald 
58888a48dd8SMatthias Ringwald         case HCI_EVENT_PACKET:
589bd87a16eSMatthias Ringwald             switch (packet[0]){
590bd87a16eSMatthias Ringwald                 case L2CAP_EVENT_CAN_SEND_NOW:
5914d2be802SMatthias Ringwald                     att_server_handle_can_send_now();
592372d62c4SMatthias Ringwald                     break;
593bd87a16eSMatthias Ringwald                 case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
594bd87a16eSMatthias Ringwald                     // GATT client has negotiated the mtu for this connection
5956155bae0SMatthias Ringwald                     att_server = att_server_for_handle(handle);
5966155bae0SMatthias Ringwald                     if (!att_server) break;
597bd87a16eSMatthias Ringwald                     att_server->connection.mtu = little_endian_read_16(packet, 4);
598bd87a16eSMatthias Ringwald                     break;
599bd87a16eSMatthias Ringwald                 default:
600bd87a16eSMatthias Ringwald                     break;
601bd87a16eSMatthias Ringwald             }
602bd87a16eSMatthias Ringwald             break;
6033deb3ec6SMatthias Ringwald 
604372d62c4SMatthias Ringwald         case ATT_DATA_PACKET:
605f50ef7ddSMatthias Ringwald             log_debug("ATT Packet, handle 0x%04x", handle);
60618707219SMatthias Ringwald             att_server = att_server_for_handle(handle);
60718707219SMatthias Ringwald             if (!att_server) break;
60818707219SMatthias Ringwald 
6093deb3ec6SMatthias Ringwald             // handle value indication confirms
61018707219SMatthias Ringwald             if (packet[0] == ATT_HANDLE_VALUE_CONFIRMATION && att_server->value_indication_handle){
61118707219SMatthias Ringwald                 btstack_run_loop_remove_timer(&att_server->value_indication_timer);
61218707219SMatthias Ringwald                 uint16_t att_handle = att_server->value_indication_handle;
61318707219SMatthias Ringwald                 att_server->value_indication_handle = 0;
61418707219SMatthias Ringwald                 att_handle_value_indication_notify_client(0, att_server->connection.con_handle, att_handle);
615cbbb12d9SMatthias Ringwald                 att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
6163deb3ec6SMatthias Ringwald                 return;
6173deb3ec6SMatthias Ringwald             }
6183deb3ec6SMatthias Ringwald 
6196428eb5aSMatthias Ringwald             // directly process command
6206428eb5aSMatthias Ringwald             // note: signed write cannot be handled directly as authentication needs to be verified
6216428eb5aSMatthias Ringwald             if (packet[0] == ATT_WRITE_COMMAND){
62218707219SMatthias Ringwald                 att_handle_request(&att_server->connection, packet, size, 0);
6236428eb5aSMatthias Ringwald                 return;
6246428eb5aSMatthias Ringwald             }
6256428eb5aSMatthias Ringwald 
6263deb3ec6SMatthias Ringwald             // check size
62718707219SMatthias Ringwald             if (size > sizeof(att_server->request_buffer)) {
62818707219SMatthias 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));
6296428eb5aSMatthias Ringwald                 return;
6306428eb5aSMatthias Ringwald             }
6313deb3ec6SMatthias Ringwald 
6323deb3ec6SMatthias Ringwald             // last request still in processing?
63318707219SMatthias Ringwald             if (att_server->state != ATT_SERVER_IDLE){
63418707219SMatthias Ringwald                 log_info("att_packet_handler: skipping att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state);
6356428eb5aSMatthias Ringwald                 return;
6366428eb5aSMatthias Ringwald             }
6373deb3ec6SMatthias Ringwald 
6383deb3ec6SMatthias Ringwald             // store request
63918707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED;
64018707219SMatthias Ringwald             att_server->request_size = size;
64118707219SMatthias Ringwald             memcpy(att_server->request_buffer, packet, size);
6423deb3ec6SMatthias Ringwald 
64318707219SMatthias Ringwald             att_run_for_context(att_server);
644372d62c4SMatthias Ringwald             break;
645372d62c4SMatthias Ringwald     }
6463deb3ec6SMatthias Ringwald }
6473deb3ec6SMatthias Ringwald 
648bf78aac6SMatthias Ringwald // ---------------------
649bf78aac6SMatthias Ringwald // persistent CCC writes
650bf78aac6SMatthias Ringwald static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){
651bf78aac6SMatthias Ringwald     return 'B' << 24 | 'T' << 16 | 'C' << 8 | index;
652bf78aac6SMatthias Ringwald }
653bf78aac6SMatthias Ringwald 
654bf78aac6SMatthias Ringwald static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){
655bf78aac6SMatthias Ringwald     // lookup att_server instance
656bf78aac6SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
657bf78aac6SMatthias Ringwald     if (!att_server) return;
658bf78aac6SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
659bf78aac6SMatthias 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);
6606a540790SMatthias Ringwald 
661bf78aac6SMatthias Ringwald     // check if bonded
662bf78aac6SMatthias Ringwald     if (le_device_index < 0) return;
6636a540790SMatthias Ringwald 
664bf78aac6SMatthias Ringwald     // get btstack_tlv
665bf78aac6SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
666bf78aac6SMatthias Ringwald     void * tlv_context;
667bf78aac6SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
668bf78aac6SMatthias Ringwald     if (!tlv_impl) return;
6696a540790SMatthias Ringwald 
670bf78aac6SMatthias Ringwald     // update ccc tag
671bf78aac6SMatthias Ringwald     int index;
6726a540790SMatthias Ringwald     uint32_t highest_seq_nr = 0;
6736a540790SMatthias Ringwald     uint32_t lowest_seq_nr = 0;
6746a540790SMatthias Ringwald     uint32_t tag_for_lowest_seq_nr = 0;
6756a540790SMatthias Ringwald     uint32_t tag_for_empty = 0;
6766a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
6776afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
678bf78aac6SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
6796a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
6806a540790SMatthias Ringwald 
6816a540790SMatthias Ringwald         // empty/invalid tag
6826a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)){
6836a540790SMatthias Ringwald             tag_for_empty = tag;
684bf78aac6SMatthias Ringwald             continue;
685bf78aac6SMatthias Ringwald         }
6866a540790SMatthias Ringwald         // update highest seq nr
6876a540790SMatthias Ringwald         if (entry.seq_nr > highest_seq_nr){
6886a540790SMatthias Ringwald             highest_seq_nr = entry.seq_nr;
6896a540790SMatthias Ringwald         }
6906a540790SMatthias Ringwald         // find entry with lowest seq nr
6916a540790SMatthias Ringwald         if ((tag_for_lowest_seq_nr == 0) || (entry.seq_nr < lowest_seq_nr)){
6926a540790SMatthias Ringwald             tag_for_lowest_seq_nr = tag;
6936a540790SMatthias Ringwald             lowest_seq_nr = entry.seq_nr;
6946a540790SMatthias Ringwald         }
6956a540790SMatthias Ringwald 
6966a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
6976a540790SMatthias Ringwald         if (entry.att_handle   != att_handle)      continue;
6986a540790SMatthias Ringwald 
6996a540790SMatthias Ringwald         // found matching entry
70001ac2008SMatthias Ringwald         if (value){
701bf78aac6SMatthias Ringwald             // update
7026a540790SMatthias Ringwald             if (entry.value == value) {
703760ad805SMatthias Ringwald                 log_info("CCC Index %u: Up-to-date", index);
704760ad805SMatthias Ringwald                 return;
705760ad805SMatthias Ringwald             }
7066a540790SMatthias Ringwald             entry.value = value;
7076a540790SMatthias Ringwald             entry.seq_nr = highest_seq_nr + 1;
708760ad805SMatthias Ringwald             log_info("CCC Index %u: Store", index);
7096a540790SMatthias Ringwald             tlv_impl->store_tag(tlv_context, tag, (const uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
71001ac2008SMatthias Ringwald         } else {
71101ac2008SMatthias Ringwald             // delete
712760ad805SMatthias Ringwald             log_info("CCC Index %u: Delete", index);
71301ac2008SMatthias Ringwald             tlv_impl->delete_tag(tlv_context, tag);
71401ac2008SMatthias Ringwald         }
715bf78aac6SMatthias Ringwald         return;
716bf78aac6SMatthias Ringwald     }
7176a540790SMatthias Ringwald 
718faf2b6c3SMatthias Ringwald     log_info("tag_for_empy %"PRIx32", tag_for_lowest_seq_nr %"PRIx32, tag_for_empty, tag_for_lowest_seq_nr);
7196a540790SMatthias Ringwald 
7206a540790SMatthias Ringwald     if (value == 0){
7216a540790SMatthias Ringwald         // done
7226a540790SMatthias Ringwald         return;
7236a540790SMatthias Ringwald     }
7246a540790SMatthias Ringwald 
7256a540790SMatthias Ringwald     uint32_t tag_to_use = 0;
7266a540790SMatthias Ringwald     if (tag_for_empty){
7276a540790SMatthias Ringwald         tag_to_use = tag_for_empty;
7286a540790SMatthias Ringwald     } else if (tag_for_lowest_seq_nr){
7296a540790SMatthias Ringwald         tag_to_use = tag_for_lowest_seq_nr;
7306a540790SMatthias Ringwald     } else {
7316a540790SMatthias Ringwald         // should not happen
7326a540790SMatthias Ringwald         return;
7336a540790SMatthias Ringwald     }
734bf78aac6SMatthias Ringwald     // store ccc tag
7356a540790SMatthias Ringwald     entry.seq_nr       = highest_seq_nr + 1;
7366a540790SMatthias Ringwald     entry.device_index = le_device_index;
7376a540790SMatthias Ringwald     entry.att_handle   = att_handle;
7386a540790SMatthias Ringwald     entry.value        = value;
7396a540790SMatthias Ringwald     tlv_impl->store_tag(tlv_context, tag_to_use, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
740bf78aac6SMatthias Ringwald }
741bf78aac6SMatthias Ringwald 
742760ad805SMatthias Ringwald static void att_server_persistent_ccc_clear(att_server_t * att_server){
743760ad805SMatthias Ringwald     if (!att_server) return;
744760ad805SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
745760ad805SMatthias Ringwald     log_info("Clear CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
746760ad805SMatthias Ringwald     // check if bonded
747760ad805SMatthias Ringwald     if (le_device_index < 0) return;
748760ad805SMatthias Ringwald     // get btstack_tlv
749760ad805SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
750760ad805SMatthias Ringwald     void * tlv_context;
751760ad805SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
752760ad805SMatthias Ringwald     if (!tlv_impl) return;
753760ad805SMatthias Ringwald     // get all ccc tag
754760ad805SMatthias Ringwald     int index;
7556a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
7566afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
757760ad805SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
7586a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
7596a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
7606a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
761760ad805SMatthias Ringwald         // delete entry
762760ad805SMatthias Ringwald         log_info("CCC Index %u: Delete", index);
763760ad805SMatthias Ringwald         tlv_impl->delete_tag(tlv_context, tag);
764760ad805SMatthias Ringwald     }
765760ad805SMatthias Ringwald }
766760ad805SMatthias Ringwald 
76701ac2008SMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server){
76801ac2008SMatthias Ringwald     if (!att_server) return;
76901ac2008SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
77001ac2008SMatthias Ringwald     log_info("Restore CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
7711b7acd0dSMatthias Ringwald     // check if bonded
7721b7acd0dSMatthias Ringwald     if (le_device_index < 0) return;
77301ac2008SMatthias Ringwald     // get btstack_tlv
77401ac2008SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
77501ac2008SMatthias Ringwald     void * tlv_context;
77601ac2008SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
77701ac2008SMatthias Ringwald     if (!tlv_impl) return;
77801ac2008SMatthias Ringwald     // get all ccc tag
77901ac2008SMatthias Ringwald     int index;
7806a540790SMatthias Ringwald     persistent_ccc_entry_t entry;
7816afb9acaSMatthias Ringwald     for (index=0;index<NVN_NUM_GATT_SERVER_CCC;index++){
78201ac2008SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
7836a540790SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, (uint8_t *) &entry, sizeof(persistent_ccc_entry_t));
7846a540790SMatthias Ringwald         if (len != sizeof(persistent_ccc_entry_t)) continue;
7856a540790SMatthias Ringwald         if (entry.device_index != le_device_index) continue;
78601ac2008SMatthias Ringwald         // simulate write callback
7876a540790SMatthias Ringwald         uint16_t attribute_handle = entry.att_handle;
78801ac2008SMatthias Ringwald         uint8_t  value[2];
7896a540790SMatthias Ringwald         little_endian_store_16(value, 0, entry.value);
79001ac2008SMatthias Ringwald         att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
79101ac2008SMatthias Ringwald         if (!callback) continue;
7926a540790SMatthias Ringwald         log_info("CCC Index %u: Set Attribute handle 0x%04x to value 0x%04x", index, attribute_handle, entry.value );
79301ac2008SMatthias Ringwald         (*callback)(att_server->connection.con_handle, attribute_handle, ATT_TRANSACTION_MODE_NONE, 0, value, sizeof(value));
79401ac2008SMatthias Ringwald     }
79501ac2008SMatthias Ringwald }
79601ac2008SMatthias Ringwald 
797bf78aac6SMatthias Ringwald // persistent CCC writes
798bf78aac6SMatthias Ringwald // ---------------------
7993d71c7a4SMatthias Ringwald 
8003d71c7a4SMatthias Ringwald // gatt service management
8013d71c7a4SMatthias Ringwald static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){
8023d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
8033d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
8043d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
8053d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
8063d71c7a4SMatthias Ringwald         if (handler->start_handle > handle) continue;
8073d71c7a4SMatthias Ringwald         if (handler->end_handle   < handle) continue;
8083d71c7a4SMatthias Ringwald         return handler;
8093d71c7a4SMatthias Ringwald     }
8103d71c7a4SMatthias Ringwald     return NULL;
8113d71c7a4SMatthias Ringwald }
8123d71c7a4SMatthias Ringwald static att_read_callback_t att_server_read_callback_for_handle(uint16_t handle){
8133d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
8143d71c7a4SMatthias Ringwald     if (handler) return handler->read_callback;
8153d71c7a4SMatthias Ringwald     return att_server_client_read_callback;
8163d71c7a4SMatthias Ringwald }
8173d71c7a4SMatthias Ringwald 
8183d71c7a4SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){
8193d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
8203d71c7a4SMatthias Ringwald     if (handler) return handler->write_callback;
8213d71c7a4SMatthias Ringwald     return att_server_client_write_callback;
8223d71c7a4SMatthias Ringwald }
8233d71c7a4SMatthias Ringwald 
8243d71c7a4SMatthias Ringwald static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){
8253d71c7a4SMatthias Ringwald     // notify all callbacks
8263d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
8273d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
8283d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
8293d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
8303d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
8313d71c7a4SMatthias Ringwald         (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
8323d71c7a4SMatthias Ringwald     }
8333d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return;
8343d71c7a4SMatthias Ringwald     (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
8353d71c7a4SMatthias Ringwald }
8363d71c7a4SMatthias Ringwald 
8373d71c7a4SMatthias Ringwald // returns first reported error or 0
8383d71c7a4SMatthias Ringwald static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){
8393d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
8403d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
8413d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
8423d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
8433d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
8443d71c7a4SMatthias Ringwald         uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
8453d71c7a4SMatthias Ringwald         if (error_code) return error_code;
8463d71c7a4SMatthias Ringwald     }
8473d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return 0;
8483d71c7a4SMatthias Ringwald     return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
8493d71c7a4SMatthias Ringwald }
8503d71c7a4SMatthias Ringwald 
8513d71c7a4SMatthias 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){
8523d71c7a4SMatthias Ringwald     att_read_callback_t callback = att_server_read_callback_for_handle(attribute_handle);
85301ac2008SMatthias Ringwald     if (!callback) return 0;
8543d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, offset, buffer, buffer_size);
8553d71c7a4SMatthias Ringwald }
8563d71c7a4SMatthias Ringwald 
8573d71c7a4SMatthias 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){
8583d71c7a4SMatthias Ringwald     switch (transaction_mode){
8593d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_VALIDATE:
8603d71c7a4SMatthias Ringwald             return att_validate_prepared_write(con_handle);
8613d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_EXECUTE:
8623d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_CANCEL:
8633d71c7a4SMatthias Ringwald             att_notify_write_callbacks(con_handle, transaction_mode);
8643d71c7a4SMatthias Ringwald             return 0;
8653d71c7a4SMatthias Ringwald         default:
8663d71c7a4SMatthias Ringwald             break;
8673d71c7a4SMatthias Ringwald     }
8683d71c7a4SMatthias Ringwald 
869bf78aac6SMatthias Ringwald     // track CCC writes
870bf78aac6SMatthias Ringwald     if (att_is_persistent_ccc(attribute_handle) && offset == 0 && buffer_size == 2){
871bf78aac6SMatthias Ringwald         att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0));
872bf78aac6SMatthias Ringwald     }
873bf78aac6SMatthias Ringwald 
87401ac2008SMatthias Ringwald     att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
87501ac2008SMatthias Ringwald     if (!callback) return 0;
8763d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size);
8773d71c7a4SMatthias Ringwald }
8783d71c7a4SMatthias Ringwald 
8793d71c7a4SMatthias Ringwald /**
8803d71c7a4SMatthias Ringwald  * @brief register read/write callbacks for specific handle range
8813d71c7a4SMatthias Ringwald  * @param att_service_handler_t
8823d71c7a4SMatthias Ringwald  */
8833d71c7a4SMatthias Ringwald void att_server_register_service_handler(att_service_handler_t * handler){
8843d71c7a4SMatthias Ringwald     if (att_service_handler_for_handle(handler->start_handle) ||
8853d71c7a4SMatthias Ringwald         att_service_handler_for_handle(handler->end_handle)){
8863d71c7a4SMatthias Ringwald         log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle);
8873d71c7a4SMatthias Ringwald         return;
8883d71c7a4SMatthias Ringwald     }
8893d71c7a4SMatthias Ringwald     btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler);
8903d71c7a4SMatthias Ringwald }
8913d71c7a4SMatthias Ringwald 
8923deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){
8933deb3ec6SMatthias Ringwald 
8943d71c7a4SMatthias Ringwald     // store callbacks
8953d71c7a4SMatthias Ringwald     att_server_client_read_callback  = read_callback;
8963d71c7a4SMatthias Ringwald     att_server_client_write_callback = write_callback;
8973d71c7a4SMatthias Ringwald 
8981a389cdcSMatthias Ringwald     // register for HCI Events
899d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &att_event_packet_handler;
9001a389cdcSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
9011a389cdcSMatthias Ringwald 
902406722e4SMatthias Ringwald     // register for SM events
903d9a7306aSMatthias Ringwald     sm_event_callback_registration.callback = &att_event_packet_handler;
904406722e4SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
9053deb3ec6SMatthias Ringwald 
9061a389cdcSMatthias Ringwald     // and L2CAP ATT Server PDUs
9073deb3ec6SMatthias Ringwald     att_dispatch_register_server(att_packet_handler);
9083deb3ec6SMatthias Ringwald 
9093deb3ec6SMatthias Ringwald     att_set_db(db);
9103d71c7a4SMatthias Ringwald     att_set_read_callback(att_server_read_callback);
9113d71c7a4SMatthias Ringwald     att_set_write_callback(att_server_write_callback);
9123deb3ec6SMatthias Ringwald 
9133deb3ec6SMatthias Ringwald }
9143deb3ec6SMatthias Ringwald 
9153deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){
9163deb3ec6SMatthias Ringwald     att_client_packet_handler = handler;
9173deb3ec6SMatthias Ringwald }
9183deb3ec6SMatthias Ringwald 
919cbbb12d9SMatthias Ringwald 
920cbbb12d9SMatthias Ringwald // to be deprecated
921c141988cSMatthias Ringwald int  att_server_can_send_packet_now(hci_con_handle_t con_handle){
92218707219SMatthias Ringwald     return att_dispatch_server_can_send_now(con_handle);
9231b96b007SMatthias Ringwald }
9241b96b007SMatthias Ringwald 
925cbbb12d9SMatthias Ringwald int att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
926cbbb12d9SMatthias Ringwald     return att_server_request_to_send_notification(callback_registration, con_handle);
9273deb3ec6SMatthias Ringwald }
9283deb3ec6SMatthias Ringwald 
929cbbb12d9SMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){
930cbbb12d9SMatthias Ringwald     att_client_waiting_for_can_send_registration.callback = &att_emit_can_send_now_event;
931cbbb12d9SMatthias Ringwald     att_server_request_to_send_notification(&att_client_waiting_for_can_send_registration, con_handle);
932cbbb12d9SMatthias Ringwald }
933cbbb12d9SMatthias Ringwald // end of deprecated
934cbbb12d9SMatthias Ringwald 
935cbbb12d9SMatthias Ringwald int att_server_request_to_send_notification(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
936969a5bbaSMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
937969a5bbaSMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
938cbbb12d9SMatthias Ringwald     btstack_linked_list_add_tail(&att_server->notification_requests, (btstack_linked_item_t*) callback_registration);
939cbbb12d9SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
940cbbb12d9SMatthias Ringwald     return ERROR_CODE_SUCCESS;
941cbbb12d9SMatthias Ringwald }
942cbbb12d9SMatthias Ringwald 
943cbbb12d9SMatthias Ringwald int att_server_request_to_send_indication(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
944cbbb12d9SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
945cbbb12d9SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
946cbbb12d9SMatthias Ringwald     btstack_linked_list_add_tail(&att_server->indication_requests, (btstack_linked_item_t*) callback_registration);
947bb38f057SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
948969a5bbaSMatthias Ringwald     return ERROR_CODE_SUCCESS;
949bb38f057SMatthias Ringwald }
950bb38f057SMatthias Ringwald 
951c141988cSMatthias Ringwald int att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){
95218707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
95318707219SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
95418707219SMatthias Ringwald 
95518707219SMatthias Ringwald     if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
9563deb3ec6SMatthias Ringwald 
9573deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
9583deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
95918707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_notification(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
96018707219SMatthias Ringwald 	return l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
9613deb3ec6SMatthias Ringwald }
9623deb3ec6SMatthias Ringwald 
963c141988cSMatthias Ringwald int att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){
96418707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
96518707219SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
96618707219SMatthias Ringwald 
967eaac31e8SMatthias Ringwald     if (att_server->value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS;
96818707219SMatthias Ringwald     if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
9693deb3ec6SMatthias Ringwald 
9703deb3ec6SMatthias Ringwald     // track indication
97118707219SMatthias Ringwald     att_server->value_indication_handle = attribute_handle;
97218707219SMatthias Ringwald     btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout);
97318707219SMatthias Ringwald     btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS);
97418707219SMatthias Ringwald     btstack_run_loop_add_timer(&att_server->value_indication_timer);
9753deb3ec6SMatthias Ringwald 
9763deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
9773deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
97818707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_indication(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
97918707219SMatthias Ringwald 	l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
9803deb3ec6SMatthias Ringwald     return 0;
9813deb3ec6SMatthias Ringwald }
982