xref: /btstack/src/ble/att_server.c (revision e62c502c9d5e95fdc8d57bb8810264ff36f0ef46)
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"
68bf78aac6SMatthias Ringwald 
69bf78aac6SMatthias Ringwald #ifndef GATT_SERVER_NUM_PERSISTENT_CCC
70bf78aac6SMatthias Ringwald #define GATT_SERVER_NUM_PERSISTENT_CCC 20
71bf78aac6SMatthias Ringwald #endif
723deb3ec6SMatthias Ringwald 
7318707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server);
7401ac2008SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle);
7501ac2008SMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server);
763deb3ec6SMatthias Ringwald 
7718707219SMatthias Ringwald // global
781a389cdcSMatthias Ringwald static btstack_packet_callback_registration_t hci_event_callback_registration;
79406722e4SMatthias Ringwald static btstack_packet_callback_registration_t sm_event_callback_registration;
803deb3ec6SMatthias Ringwald static btstack_packet_handler_t               att_client_packet_handler = NULL;
81bb38f057SMatthias Ringwald static btstack_linked_list_t                  can_send_now_clients;
823d71c7a4SMatthias Ringwald static btstack_linked_list_t                  service_handlers;
8318707219SMatthias Ringwald static uint8_t                                att_client_waiting_for_can_send;
8418707219SMatthias Ringwald 
853d71c7a4SMatthias Ringwald static att_read_callback_t                    att_server_client_read_callback;
863d71c7a4SMatthias Ringwald static att_write_callback_t                   att_server_client_write_callback;
873d71c7a4SMatthias Ringwald 
88bf78aac6SMatthias Ringwald // track CCC 1-entry cache
89bf78aac6SMatthias Ringwald // static att_server_t *    att_persistent_ccc_server;
90bf78aac6SMatthias Ringwald // static hci_con_handle_t  att_persistent_ccc_con_handle;
91bf78aac6SMatthias Ringwald 
9218707219SMatthias Ringwald static att_server_t * att_server_for_handle(hci_con_handle_t con_handle){
9318707219SMatthias Ringwald     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
9418707219SMatthias Ringwald     if (!hci_connection) return NULL;
9518707219SMatthias Ringwald     return &hci_connection->att_server;
9618707219SMatthias Ringwald }
9718707219SMatthias Ringwald 
9802b78fc8SMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
9918707219SMatthias Ringwald static att_server_t * att_server_for_state(att_server_state_t state){
10018707219SMatthias Ringwald     btstack_linked_list_iterator_t it;
10118707219SMatthias Ringwald     hci_connections_get_iterator(&it);
10218707219SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
10318707219SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
10418707219SMatthias Ringwald         att_server_t * att_server = &connection->att_server;
10518707219SMatthias Ringwald         if (att_server->state == state) return att_server;
10618707219SMatthias Ringwald     }
10718707219SMatthias Ringwald     return NULL;
10818707219SMatthias Ringwald }
10902b78fc8SMatthias Ringwald #endif
110bb38f057SMatthias Ringwald 
1113deb3ec6SMatthias Ringwald static void att_handle_value_indication_notify_client(uint8_t status, uint16_t client_handle, uint16_t attribute_handle){
1123deb3ec6SMatthias Ringwald     if (!att_client_packet_handler) return;
1133deb3ec6SMatthias Ringwald 
1143deb3ec6SMatthias Ringwald     uint8_t event[7];
1153deb3ec6SMatthias Ringwald     int pos = 0;
1165611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_HANDLE_VALUE_INDICATION_COMPLETE;
1173deb3ec6SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
1183deb3ec6SMatthias Ringwald     event[pos++] = status;
119f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, client_handle);
1203deb3ec6SMatthias Ringwald     pos += 2;
121f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, attribute_handle);
1223deb3ec6SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1233deb3ec6SMatthias Ringwald }
1243deb3ec6SMatthias Ringwald 
125fc64f94aSMatthias Ringwald static void att_emit_mtu_event(hci_con_handle_t con_handle, uint16_t mtu){
1263deb3ec6SMatthias Ringwald     if (!att_client_packet_handler) return;
1273deb3ec6SMatthias Ringwald 
1283deb3ec6SMatthias Ringwald     uint8_t event[6];
1293deb3ec6SMatthias Ringwald     int pos = 0;
1305611a760SMatthias Ringwald     event[pos++] = ATT_EVENT_MTU_EXCHANGE_COMPLETE;
1313deb3ec6SMatthias Ringwald     event[pos++] = sizeof(event) - 2;
132fc64f94aSMatthias Ringwald     little_endian_store_16(event, pos, con_handle);
1333deb3ec6SMatthias Ringwald     pos += 2;
134f8fbdce0SMatthias Ringwald     little_endian_store_16(event, pos, mtu);
1353deb3ec6SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1363deb3ec6SMatthias Ringwald }
1373deb3ec6SMatthias Ringwald 
1381b96b007SMatthias Ringwald static void att_emit_can_send_now_event(void){
1393c97b242SMatthias Ringwald     if (!att_client_packet_handler) return;
1403c97b242SMatthias Ringwald 
1411b96b007SMatthias Ringwald     uint8_t event[] = { ATT_EVENT_CAN_SEND_NOW, 0};
1421b96b007SMatthias Ringwald     (*att_client_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
1431b96b007SMatthias Ringwald }
1441b96b007SMatthias Ringwald 
145ec820d77SMatthias Ringwald static void att_handle_value_indication_timeout(btstack_timer_source_t *ts){
14654178543SMatthias Ringwald     void * context = btstack_run_loop_get_timer_context(ts);
14754178543SMatthias Ringwald     hci_con_handle_t con_handle = (hci_con_handle_t) (uintptr_t) context;
14818707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
14918707219SMatthias Ringwald     if (!att_server) return;
15018707219SMatthias Ringwald     uint16_t att_handle = att_server->value_indication_handle;
15118707219SMatthias Ringwald     att_handle_value_indication_notify_client(ATT_HANDLE_VALUE_INDICATION_TIMEOUT, att_server->connection.con_handle, att_handle);
1523deb3ec6SMatthias Ringwald }
1533deb3ec6SMatthias Ringwald 
154*e62c502cSMatthias Ringwald static void att_device_identified(hci_con_handle_t con_handle, uint8_t index){
155*e62c502cSMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
156*e62c502cSMatthias Ringwald     if (!att_server) return;
157*e62c502cSMatthias Ringwald     att_server->ir_lookup_active = 0;
158*e62c502cSMatthias Ringwald     att_server->ir_le_device_db_index = index;
159*e62c502cSMatthias Ringwald     log_info("Remote device with conn 0%04x registered with index id %u", (int) con_handle, att_server->ir_le_device_db_index);
160*e62c502cSMatthias Ringwald     att_run_for_context(att_server);
161*e62c502cSMatthias Ringwald }
162*e62c502cSMatthias Ringwald 
1633deb3ec6SMatthias Ringwald static void att_event_packet_handler (uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1649ec2630cSMatthias Ringwald 
1657dad9ff8SMatthias Ringwald     UNUSED(channel); // ok: there is no channel
1667dad9ff8SMatthias Ringwald     UNUSED(size);    // ok: handling own l2cap events
1673deb3ec6SMatthias Ringwald 
16818707219SMatthias Ringwald     att_server_t * att_server;
16918707219SMatthias Ringwald     hci_con_handle_t con_handle;
17018707219SMatthias Ringwald 
1713deb3ec6SMatthias Ringwald     switch (packet_type) {
1723deb3ec6SMatthias Ringwald 
1733deb3ec6SMatthias Ringwald         case HCI_EVENT_PACKET:
1740e2df43fSMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
1753deb3ec6SMatthias Ringwald 
1763deb3ec6SMatthias Ringwald                 case HCI_EVENT_LE_META:
1773deb3ec6SMatthias Ringwald                     switch (packet[2]) {
1783deb3ec6SMatthias Ringwald                         case HCI_SUBEVENT_LE_CONNECTION_COMPLETE:
17918707219SMatthias Ringwald                             con_handle = little_endian_read_16(packet, 4);
18018707219SMatthias Ringwald                             att_server = att_server_for_handle(con_handle);
18118707219SMatthias Ringwald                             if (!att_server) break;
1823deb3ec6SMatthias Ringwald                         	// store connection info
18318707219SMatthias Ringwald                         	att_server->peer_addr_type = packet[7];
18418707219SMatthias Ringwald                             reverse_bd_addr(&packet[8], att_server->peer_address);
18518707219SMatthias Ringwald                             att_server->connection.con_handle = con_handle;
1863deb3ec6SMatthias Ringwald                             // reset connection properties
18718707219SMatthias Ringwald                             att_server->state = ATT_SERVER_IDLE;
18818707219SMatthias Ringwald                             att_server->connection.mtu = ATT_DEFAULT_MTU;
18918707219SMatthias Ringwald                             att_server->connection.max_mtu = l2cap_max_le_mtu();
19018707219SMatthias Ringwald                             if (att_server->connection.max_mtu > ATT_REQUEST_BUFFER_SIZE){
19118707219SMatthias Ringwald                                 att_server->connection.max_mtu = ATT_REQUEST_BUFFER_SIZE;
19299c58ad0SMatthias Ringwald                             }
19318707219SMatthias Ringwald                             att_server->connection.encryption_key_size = 0;
19418707219SMatthias Ringwald                             att_server->connection.authenticated = 0;
19518707219SMatthias Ringwald 		                	att_server->connection.authorized = 0;
19618707219SMatthias Ringwald                             att_server->ir_le_device_db_index = -1;
1973deb3ec6SMatthias Ringwald                             break;
1983deb3ec6SMatthias Ringwald 
1993deb3ec6SMatthias Ringwald                         default:
2003deb3ec6SMatthias Ringwald                             break;
2013deb3ec6SMatthias Ringwald                     }
2023deb3ec6SMatthias Ringwald                     break;
2033deb3ec6SMatthias Ringwald 
2043deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_CHANGE:
2053deb3ec6SMatthias Ringwald                 case HCI_EVENT_ENCRYPTION_KEY_REFRESH_COMPLETE:
2063deb3ec6SMatthias Ringwald                 	// check handle
20718707219SMatthias Ringwald                     con_handle = little_endian_read_16(packet, 3);
20818707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
20918707219SMatthias Ringwald                     if (!att_server) break;
21018707219SMatthias Ringwald                 	att_server->connection.encryption_key_size = sm_encryption_key_size(con_handle);
21118707219SMatthias Ringwald                 	att_server->connection.authenticated = sm_authenticated(con_handle);
21201ac2008SMatthias Ringwald                     if (hci_event_packet_get_type(packet) == HCI_EVENT_ENCRYPTION_CHANGE){
21301ac2008SMatthias Ringwald                         // restore CCC values when encrypted
21401ac2008SMatthias Ringwald                         if (hci_event_encryption_change_get_encryption_enabled(packet)){
21501ac2008SMatthias Ringwald                             att_server_persistent_ccc_restore(att_server);
21601ac2008SMatthias Ringwald                         }
21701ac2008SMatthias Ringwald                     }
2183deb3ec6SMatthias Ringwald                 	break;
2193deb3ec6SMatthias Ringwald 
2203deb3ec6SMatthias Ringwald                 case HCI_EVENT_DISCONNECTION_COMPLETE:
22170a390c7SMatthias Ringwald                     // check handle
22218707219SMatthias Ringwald                     con_handle = hci_event_disconnection_complete_get_connection_handle(packet);
22318707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
22418707219SMatthias Ringwald                     if (!att_server) break;
22518707219SMatthias Ringwald                     att_clear_transaction_queue(&att_server->connection);
22618707219SMatthias Ringwald                     att_server->connection.con_handle = 0;
22718707219SMatthias Ringwald                     att_server->value_indication_handle = 0; // reset error state
22818707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
2293deb3ec6SMatthias Ringwald                     break;
2303deb3ec6SMatthias Ringwald 
2315611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_STARTED:
23218707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_started_get_handle(packet);
23318707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
23418707219SMatthias Ringwald                     if (!att_server) break;
2355611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_STARTED");
23618707219SMatthias Ringwald                     att_server->ir_lookup_active = 1;
2373deb3ec6SMatthias Ringwald                     break;
2385611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
239*e62c502cSMatthias Ringwald                     att_device_identified(sm_event_identity_resolving_succeeded_get_handle(packet), sm_event_identity_resolving_succeeded_get_index(packet));
240*e62c502cSMatthias Ringwald                     break;
241*e62c502cSMatthias Ringwald                 case SM_EVENT_IDENTITY_CREATED:
242*e62c502cSMatthias Ringwald                     att_device_identified(sm_event_identity_created_get_handle(packet), sm_event_identity_created_get_index(packet));
2433deb3ec6SMatthias Ringwald                     break;
2445611a760SMatthias Ringwald                 case SM_EVENT_IDENTITY_RESOLVING_FAILED:
24518707219SMatthias Ringwald                     con_handle = sm_event_identity_resolving_failed_get_handle(packet);
24618707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
24718707219SMatthias Ringwald                     if (!att_server) break;
2485611a760SMatthias Ringwald                     log_info("SM_EVENT_IDENTITY_RESOLVING_FAILED");
24918707219SMatthias Ringwald                     att_server->ir_lookup_active = 0;
25018707219SMatthias Ringwald                     att_server->ir_le_device_db_index = -1;
25118707219SMatthias Ringwald                     att_run_for_context(att_server);
2523deb3ec6SMatthias Ringwald                     break;
2535611a760SMatthias Ringwald                 case SM_EVENT_AUTHORIZATION_RESULT: {
25418707219SMatthias Ringwald                     con_handle = sm_event_authorization_result_get_handle(packet);
25518707219SMatthias Ringwald                     att_server = att_server_for_handle(con_handle);
25618707219SMatthias Ringwald                     if (!att_server) break;
25718707219SMatthias Ringwald                     att_server->connection.authorized = sm_event_authorization_result_get_authorization_result(packet);
25818707219SMatthias Ringwald                     att_dispatch_server_request_can_send_now_event(con_handle);
2593deb3ec6SMatthias Ringwald                 	break;
2603deb3ec6SMatthias Ringwald                 }
2613deb3ec6SMatthias Ringwald                 default:
2623deb3ec6SMatthias Ringwald                     break;
2633deb3ec6SMatthias Ringwald             }
26465b44ffdSMatthias Ringwald             break;
26565b44ffdSMatthias Ringwald         default:
26665b44ffdSMatthias Ringwald             break;
2673deb3ec6SMatthias Ringwald     }
2683deb3ec6SMatthias Ringwald }
2693deb3ec6SMatthias Ringwald 
2707a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
2713deb3ec6SMatthias Ringwald static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
2723deb3ec6SMatthias Ringwald 
27318707219SMatthias Ringwald     att_server_t * att_server = att_server_for_state(ATT_SERVER_W4_SIGNED_WRITE_VALIDATION);
27418707219SMatthias Ringwald     if (!att_server) return;
2753deb3ec6SMatthias Ringwald 
2763deb3ec6SMatthias Ringwald     uint8_t hash_flipped[8];
2779c80e4ccSMatthias Ringwald     reverse_64(hash, hash_flipped);
27818707219SMatthias Ringwald     if (memcmp(hash_flipped, &att_server->request_buffer[att_server->request_size-8], 8)){
2793deb3ec6SMatthias Ringwald         log_info("ATT Signed Write, invalid signature");
28018707219SMatthias Ringwald         att_server->state = ATT_SERVER_IDLE;
2813deb3ec6SMatthias Ringwald         return;
2823deb3ec6SMatthias Ringwald     }
2833deb3ec6SMatthias Ringwald     log_info("ATT Signed Write, valid signature");
2843deb3ec6SMatthias Ringwald 
2853deb3ec6SMatthias Ringwald     // update sequence number
28618707219SMatthias Ringwald     uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
28718707219SMatthias Ringwald     le_device_db_remote_counter_set(att_server->ir_le_device_db_index, counter_packet+1);
28818707219SMatthias Ringwald     att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
28918707219SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
2903deb3ec6SMatthias Ringwald }
2917a766ebfSMatthias Ringwald #endif
29288a48dd8SMatthias Ringwald 
29318707219SMatthias Ringwald // pre: att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED
294b06f2579SMatthias Ringwald // pre: can send now
295b06f2579SMatthias Ringwald // returns: 1 if packet was sent
29618707219SMatthias Ringwald static int att_server_process_validated_request(att_server_t * att_server){
297b06f2579SMatthias Ringwald 
298b06f2579SMatthias Ringwald     l2cap_reserve_packet_buffer();
299b06f2579SMatthias Ringwald     uint8_t * att_response_buffer = l2cap_get_outgoing_buffer();
30018707219SMatthias Ringwald     uint16_t  att_response_size   = att_handle_request(&att_server->connection, att_server->request_buffer, att_server->request_size, att_response_buffer);
301b06f2579SMatthias Ringwald 
302b06f2579SMatthias Ringwald     // intercept "insufficient authorization" for authenticated connections to allow for user authorization
303b06f2579SMatthias Ringwald     if ((att_response_size     >= 4)
304b06f2579SMatthias Ringwald     && (att_response_buffer[0] == ATT_ERROR_RESPONSE)
305b06f2579SMatthias Ringwald     && (att_response_buffer[4] == ATT_ERROR_INSUFFICIENT_AUTHORIZATION)
30618707219SMatthias Ringwald     && (att_server->connection.authenticated)){
307b06f2579SMatthias Ringwald 
30818707219SMatthias Ringwald         switch (sm_authorization_state(att_server->connection.con_handle)){
309b06f2579SMatthias Ringwald             case AUTHORIZATION_UNKNOWN:
310b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
31118707219SMatthias Ringwald                 sm_request_pairing(att_server->connection.con_handle);
312b06f2579SMatthias Ringwald                 return 0;
313b06f2579SMatthias Ringwald             case AUTHORIZATION_PENDING:
314b06f2579SMatthias Ringwald                 l2cap_release_packet_buffer();
315b06f2579SMatthias Ringwald                 return 0;
316b06f2579SMatthias Ringwald             default:
317b06f2579SMatthias Ringwald                 break;
318b06f2579SMatthias Ringwald         }
319b06f2579SMatthias Ringwald     }
320b06f2579SMatthias Ringwald 
32118707219SMatthias Ringwald     att_server->state = ATT_SERVER_IDLE;
322b06f2579SMatthias Ringwald     if (att_response_size == 0) {
323b06f2579SMatthias Ringwald         l2cap_release_packet_buffer();
324b06f2579SMatthias Ringwald         return 0;
325b06f2579SMatthias Ringwald     }
326b06f2579SMatthias Ringwald 
32718707219SMatthias Ringwald     l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, att_response_size);
328b06f2579SMatthias Ringwald 
329b06f2579SMatthias Ringwald     // notify client about MTU exchange result
330b06f2579SMatthias Ringwald     if (att_response_buffer[0] == ATT_EXCHANGE_MTU_RESPONSE){
33118707219SMatthias Ringwald         att_emit_mtu_event(att_server->connection.con_handle, att_server->connection.mtu);
332b06f2579SMatthias Ringwald     }
333b06f2579SMatthias Ringwald     return 1;
334b06f2579SMatthias Ringwald }
335b06f2579SMatthias Ringwald 
33618707219SMatthias Ringwald static void att_run_for_context(att_server_t * att_server){
33718707219SMatthias Ringwald     switch (att_server->state){
3383deb3ec6SMatthias Ringwald         case ATT_SERVER_REQUEST_RECEIVED:
3397a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
34018707219SMatthias Ringwald             if (att_server->request_buffer[0] == ATT_SIGNED_WRITE_COMMAND){
3413deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write!");
3423deb3ec6SMatthias Ringwald                 if (!sm_cmac_ready()) {
3433deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, sm_cmac engine not ready. Abort");
34418707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
3453deb3ec6SMatthias Ringwald                     return;
3463deb3ec6SMatthias Ringwald                 }
34718707219SMatthias Ringwald                 if (att_server->request_size < (3 + 12)) {
3483deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, request to short. Abort.");
34918707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
3503deb3ec6SMatthias Ringwald                     return;
3513deb3ec6SMatthias Ringwald                 }
35218707219SMatthias Ringwald                 if (att_server->ir_lookup_active){
3533deb3ec6SMatthias Ringwald                     return;
3543deb3ec6SMatthias Ringwald                 }
35518707219SMatthias Ringwald                 if (att_server->ir_le_device_db_index < 0){
3563deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, CSRK not available");
35718707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
3583deb3ec6SMatthias Ringwald                     return;
3593deb3ec6SMatthias Ringwald                 }
3603deb3ec6SMatthias Ringwald 
3613deb3ec6SMatthias Ringwald                 // check counter
36218707219SMatthias Ringwald                 uint32_t counter_packet = little_endian_read_32(att_server->request_buffer, att_server->request_size-12);
36318707219SMatthias Ringwald                 uint32_t counter_db     = le_device_db_remote_counter_get(att_server->ir_le_device_db_index);
3643deb3ec6SMatthias Ringwald                 log_info("ATT Signed Write, DB counter %"PRIu32", packet counter %"PRIu32, counter_db, counter_packet);
3653deb3ec6SMatthias Ringwald                 if (counter_packet < counter_db){
3663deb3ec6SMatthias Ringwald                     log_info("ATT Signed Write, db reports higher counter, abort");
36718707219SMatthias Ringwald                     att_server->state = ATT_SERVER_IDLE;
3683deb3ec6SMatthias Ringwald                     return;
3693deb3ec6SMatthias Ringwald                 }
3703deb3ec6SMatthias Ringwald 
3713deb3ec6SMatthias Ringwald                 // signature is { sequence counter, secure hash }
3723deb3ec6SMatthias Ringwald                 sm_key_t csrk;
37318707219SMatthias Ringwald                 le_device_db_remote_csrk_get(att_server->ir_le_device_db_index, csrk);
37418707219SMatthias Ringwald                 att_server->state = ATT_SERVER_W4_SIGNED_WRITE_VALIDATION;
3753deb3ec6SMatthias Ringwald                 log_info("Orig Signature: ");
37618707219SMatthias Ringwald                 log_info_hexdump( &att_server->request_buffer[att_server->request_size-8], 8);
37718707219SMatthias Ringwald                 uint16_t attribute_handle = little_endian_read_16(att_server->request_buffer, 1);
37818707219SMatthias 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);
3793deb3ec6SMatthias Ringwald                 return;
3803deb3ec6SMatthias Ringwald             }
3817a766ebfSMatthias Ringwald #endif
382b06f2579SMatthias Ringwald             // move on
38318707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED;
38418707219SMatthias Ringwald             att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
385b06f2579SMatthias Ringwald             break;
38688a48dd8SMatthias Ringwald 
3873deb3ec6SMatthias Ringwald         default:
3883deb3ec6SMatthias Ringwald             break;
3893deb3ec6SMatthias Ringwald     }
3903deb3ec6SMatthias Ringwald }
3913deb3ec6SMatthias Ringwald 
3927eb16ee8SMatthias Ringwald static void att_server_handle_can_send_now(void){
393b06f2579SMatthias Ringwald 
394b06f2579SMatthias Ringwald     // NOTE: we get l2cap fixed channel instead of con_handle
395b06f2579SMatthias Ringwald 
39618707219SMatthias Ringwald     btstack_linked_list_iterator_t it;
39718707219SMatthias Ringwald     hci_connections_get_iterator(&it);
39818707219SMatthias Ringwald     while(btstack_linked_list_iterator_has_next(&it)){
39918707219SMatthias Ringwald         hci_connection_t * connection = (hci_connection_t *) btstack_linked_list_iterator_next(&it);
40018707219SMatthias Ringwald         att_server_t * att_server = &connection->att_server;
40118707219SMatthias Ringwald         if (att_server->state == ATT_SERVER_REQUEST_RECEIVED_AND_VALIDATED){
40218707219SMatthias Ringwald             int sent = att_server_process_validated_request(att_server);
403bb38f057SMatthias Ringwald             if (sent && (att_client_waiting_for_can_send || !btstack_linked_list_empty(&can_send_now_clients))){
40418707219SMatthias Ringwald                 att_dispatch_server_request_can_send_now_event(att_server->connection.con_handle);
4053deb3ec6SMatthias Ringwald                 return;
4063deb3ec6SMatthias Ringwald             }
4073deb3ec6SMatthias Ringwald         }
40818707219SMatthias Ringwald     }
4093deb3ec6SMatthias Ringwald 
410bb38f057SMatthias Ringwald     while (!btstack_linked_list_empty(&can_send_now_clients)){
411bb38f057SMatthias Ringwald         // handle first client
412bb38f057SMatthias Ringwald         btstack_context_callback_registration_t * client = (btstack_context_callback_registration_t*) can_send_now_clients;
41318707219SMatthias Ringwald         hci_con_handle_t con_handle = (uintptr_t) client->context;
414bb38f057SMatthias Ringwald         btstack_linked_list_remove(&can_send_now_clients, (btstack_linked_item_t *) client);
415bb38f057SMatthias Ringwald         client->callback(client->context);
416bb38f057SMatthias Ringwald 
417bb38f057SMatthias Ringwald         // request again if needed
41818707219SMatthias Ringwald         if (!att_dispatch_server_can_send_now(con_handle)){
419bb38f057SMatthias Ringwald             if (!btstack_linked_list_empty(&can_send_now_clients) || att_client_waiting_for_can_send){
42018707219SMatthias Ringwald                 att_dispatch_server_request_can_send_now_event(con_handle);
421bb38f057SMatthias Ringwald             }
422bb38f057SMatthias Ringwald             return;
423bb38f057SMatthias Ringwald         }
424bb38f057SMatthias Ringwald     }
425bb38f057SMatthias Ringwald 
426b06f2579SMatthias Ringwald     if (att_client_waiting_for_can_send){
427b06f2579SMatthias Ringwald         att_client_waiting_for_can_send = 0;
428b06f2579SMatthias Ringwald         att_emit_can_send_now_event();
4293deb3ec6SMatthias Ringwald     }
4303deb3ec6SMatthias Ringwald }
4313deb3ec6SMatthias Ringwald 
43288a48dd8SMatthias Ringwald static void att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
43318707219SMatthias Ringwald 
43418707219SMatthias Ringwald     att_server_t * att_server;
43518707219SMatthias Ringwald 
43688a48dd8SMatthias Ringwald     switch (packet_type){
43788a48dd8SMatthias Ringwald 
43888a48dd8SMatthias Ringwald         case HCI_EVENT_PACKET:
43988a48dd8SMatthias Ringwald             if (packet[0] != L2CAP_EVENT_CAN_SEND_NOW) break;
4404d2be802SMatthias Ringwald             att_server_handle_can_send_now();
441372d62c4SMatthias Ringwald             break;
4423deb3ec6SMatthias Ringwald 
443372d62c4SMatthias Ringwald         case ATT_DATA_PACKET:
44418707219SMatthias Ringwald             log_info("ATT Packet, handle 0x%04x", handle);
44518707219SMatthias Ringwald             att_server = att_server_for_handle(handle);
44618707219SMatthias Ringwald             if (!att_server) break;
44718707219SMatthias Ringwald 
4483deb3ec6SMatthias Ringwald             // handle value indication confirms
44918707219SMatthias Ringwald             if (packet[0] == ATT_HANDLE_VALUE_CONFIRMATION && att_server->value_indication_handle){
45018707219SMatthias Ringwald                 btstack_run_loop_remove_timer(&att_server->value_indication_timer);
45118707219SMatthias Ringwald                 uint16_t att_handle = att_server->value_indication_handle;
45218707219SMatthias Ringwald                 att_server->value_indication_handle = 0;
45318707219SMatthias Ringwald                 att_handle_value_indication_notify_client(0, att_server->connection.con_handle, att_handle);
4543deb3ec6SMatthias Ringwald                 return;
4553deb3ec6SMatthias Ringwald             }
4563deb3ec6SMatthias Ringwald 
4576428eb5aSMatthias Ringwald             // directly process command
4586428eb5aSMatthias Ringwald             // note: signed write cannot be handled directly as authentication needs to be verified
4596428eb5aSMatthias Ringwald             if (packet[0] == ATT_WRITE_COMMAND){
46018707219SMatthias Ringwald                 att_handle_request(&att_server->connection, packet, size, 0);
4616428eb5aSMatthias Ringwald                 return;
4626428eb5aSMatthias Ringwald             }
4636428eb5aSMatthias Ringwald 
4643deb3ec6SMatthias Ringwald             // check size
46518707219SMatthias Ringwald             if (size > sizeof(att_server->request_buffer)) {
46618707219SMatthias 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));
4676428eb5aSMatthias Ringwald                 return;
4686428eb5aSMatthias Ringwald             }
4693deb3ec6SMatthias Ringwald 
4703deb3ec6SMatthias Ringwald             // last request still in processing?
47118707219SMatthias Ringwald             if (att_server->state != ATT_SERVER_IDLE){
47218707219SMatthias Ringwald                 log_info("att_packet_handler: skipping att pdu 0x%02x as server not idle (state %u)", packet[0], att_server->state);
4736428eb5aSMatthias Ringwald                 return;
4746428eb5aSMatthias Ringwald             }
4753deb3ec6SMatthias Ringwald 
4763deb3ec6SMatthias Ringwald             // store request
47718707219SMatthias Ringwald             att_server->state = ATT_SERVER_REQUEST_RECEIVED;
47818707219SMatthias Ringwald             att_server->request_size = size;
47918707219SMatthias Ringwald             memcpy(att_server->request_buffer, packet, size);
4803deb3ec6SMatthias Ringwald 
48118707219SMatthias Ringwald             att_run_for_context(att_server);
482372d62c4SMatthias Ringwald             break;
483372d62c4SMatthias Ringwald     }
4843deb3ec6SMatthias Ringwald }
4853deb3ec6SMatthias Ringwald 
486bf78aac6SMatthias Ringwald // ---------------------
487bf78aac6SMatthias Ringwald // persistent CCC writes
488bf78aac6SMatthias Ringwald // TLV value format: le_device_index(8), attribute_handle(16), value (8)
489bf78aac6SMatthias Ringwald static uint32_t att_server_persistent_ccc_tag_for_index(uint8_t index){
490bf78aac6SMatthias Ringwald     return 'B' << 24 | 'T' << 16 | 'C' << 8 | index;
491bf78aac6SMatthias Ringwald }
492bf78aac6SMatthias Ringwald 
493bf78aac6SMatthias Ringwald static void att_server_persistent_ccc_write(hci_con_handle_t con_handle, uint16_t att_handle, uint16_t value){
494bf78aac6SMatthias Ringwald     // lookup att_server instance
495bf78aac6SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
496bf78aac6SMatthias Ringwald     if (!att_server) return;
497bf78aac6SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
498bf78aac6SMatthias 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);
499bf78aac6SMatthias Ringwald     // check if bonded
500bf78aac6SMatthias Ringwald     if (le_device_index < 0) return;
501bf78aac6SMatthias Ringwald     // get btstack_tlv
502bf78aac6SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
503bf78aac6SMatthias Ringwald     void * tlv_context;
504bf78aac6SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
505bf78aac6SMatthias Ringwald     if (!tlv_impl) return;
506bf78aac6SMatthias Ringwald     // update ccc tag
507bf78aac6SMatthias Ringwald     int index;
508bf78aac6SMatthias Ringwald     uint8_t buffer[4];
509bf78aac6SMatthias Ringwald     int free_index = -1;
510bf78aac6SMatthias Ringwald     for (index=0;index<GATT_SERVER_NUM_PERSISTENT_CCC;index++){
511bf78aac6SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
512bf78aac6SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, buffer, sizeof(buffer));
513bf78aac6SMatthias Ringwald         if (len == 0){
514bf78aac6SMatthias Ringwald             free_index = index;
515bf78aac6SMatthias Ringwald             continue;
516bf78aac6SMatthias Ringwald         }
517bf78aac6SMatthias Ringwald         if (len != sizeof(buffer)) continue;
518bf78aac6SMatthias Ringwald         if (buffer[0] != le_device_index) continue;
519bf78aac6SMatthias Ringwald         if (little_endian_read_16(buffer, 1) != att_handle) continue;
52001ac2008SMatthias Ringwald         if (value){
521bf78aac6SMatthias Ringwald             // update
52201ac2008SMatthias Ringwald             if (buffer[3] == value) return;
523bf78aac6SMatthias Ringwald             buffer[3] = value;
524bf78aac6SMatthias Ringwald             tlv_impl->store_tag(tlv_context, tag, buffer, sizeof(buffer));
52501ac2008SMatthias Ringwald         } else {
52601ac2008SMatthias Ringwald             // delete
52701ac2008SMatthias Ringwald             tlv_impl->delete_tag(tlv_context, tag);
52801ac2008SMatthias Ringwald         }
529bf78aac6SMatthias Ringwald         return;
530bf78aac6SMatthias Ringwald     }
531bf78aac6SMatthias Ringwald     if (free_index < 0) return;
532bf78aac6SMatthias Ringwald     // store ccc tag
533bf78aac6SMatthias Ringwald     buffer[0] = le_device_index;
534bf78aac6SMatthias Ringwald     little_endian_store_16(buffer, 1, att_handle);
535bf78aac6SMatthias Ringwald     buffer[3] = value;
536bf78aac6SMatthias Ringwald     uint32_t tag = att_server_persistent_ccc_tag_for_index(free_index);
537bf78aac6SMatthias Ringwald     tlv_impl->store_tag(tlv_context, tag, buffer, sizeof(buffer));
538bf78aac6SMatthias Ringwald }
539bf78aac6SMatthias Ringwald 
54001ac2008SMatthias Ringwald static void att_server_persistent_ccc_restore(att_server_t * att_server){
54101ac2008SMatthias Ringwald     if (!att_server) return;
54201ac2008SMatthias Ringwald     int le_device_index = att_server->ir_le_device_db_index;
54301ac2008SMatthias Ringwald     log_info("Restore CCC values of remote %s, le device id %d", bd_addr_to_str(att_server->peer_address), le_device_index);
54401ac2008SMatthias Ringwald     // get btstack_tlv
54501ac2008SMatthias Ringwald     const btstack_tlv_t * tlv_impl = NULL;
54601ac2008SMatthias Ringwald     void * tlv_context;
54701ac2008SMatthias Ringwald     btstack_tlv_get_instance(&tlv_impl, &tlv_context);
54801ac2008SMatthias Ringwald     if (!tlv_impl) return;
54901ac2008SMatthias Ringwald     // get all ccc tag
55001ac2008SMatthias Ringwald     int index;
55101ac2008SMatthias Ringwald     uint8_t buffer[4];
55201ac2008SMatthias Ringwald     for (index=0;index<GATT_SERVER_NUM_PERSISTENT_CCC;index++){
55301ac2008SMatthias Ringwald         uint32_t tag = att_server_persistent_ccc_tag_for_index(index);
55401ac2008SMatthias Ringwald         int len = tlv_impl->get_tag(tlv_context, tag, buffer, sizeof(buffer));
55501ac2008SMatthias Ringwald         if (len != sizeof(buffer)) continue;
55601ac2008SMatthias Ringwald         if (buffer[0] != le_device_index) continue;
55701ac2008SMatthias Ringwald         // simulate write callback
55801ac2008SMatthias Ringwald         uint16_t attribute_handle = little_endian_read_16(buffer, 1);
55901ac2008SMatthias Ringwald         uint8_t  value[2];
56001ac2008SMatthias Ringwald         little_endian_store_16(value, 0, buffer[3]);
56101ac2008SMatthias Ringwald         att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
56201ac2008SMatthias Ringwald         if (!callback) continue;
56301ac2008SMatthias Ringwald         log_info("Attribute handle 0x%04x, value 0x%04x", attribute_handle, buffer[3]);
56401ac2008SMatthias Ringwald         (*callback)(att_server->connection.con_handle, attribute_handle, ATT_TRANSACTION_MODE_NONE, 0, value, sizeof(value));
56501ac2008SMatthias Ringwald     }
56601ac2008SMatthias Ringwald }
56701ac2008SMatthias Ringwald 
568bf78aac6SMatthias Ringwald // persistent CCC writes
569bf78aac6SMatthias Ringwald // ---------------------
5703d71c7a4SMatthias Ringwald 
5713d71c7a4SMatthias Ringwald // gatt service management
5723d71c7a4SMatthias Ringwald static att_service_handler_t * att_service_handler_for_handle(uint16_t handle){
5733d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
5743d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
5753d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
5763d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
5773d71c7a4SMatthias Ringwald         if (handler->start_handle > handle) continue;
5783d71c7a4SMatthias Ringwald         if (handler->end_handle   < handle) continue;
5793d71c7a4SMatthias Ringwald         return handler;
5803d71c7a4SMatthias Ringwald     }
5813d71c7a4SMatthias Ringwald     return NULL;
5823d71c7a4SMatthias Ringwald }
5833d71c7a4SMatthias Ringwald static att_read_callback_t att_server_read_callback_for_handle(uint16_t handle){
5843d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
5853d71c7a4SMatthias Ringwald     if (handler) return handler->read_callback;
5863d71c7a4SMatthias Ringwald     return att_server_client_read_callback;
5873d71c7a4SMatthias Ringwald }
5883d71c7a4SMatthias Ringwald 
5893d71c7a4SMatthias Ringwald static att_write_callback_t att_server_write_callback_for_handle(uint16_t handle){
5903d71c7a4SMatthias Ringwald     att_service_handler_t * handler = att_service_handler_for_handle(handle);
5913d71c7a4SMatthias Ringwald     if (handler) return handler->write_callback;
5923d71c7a4SMatthias Ringwald     return att_server_client_write_callback;
5933d71c7a4SMatthias Ringwald }
5943d71c7a4SMatthias Ringwald 
5953d71c7a4SMatthias Ringwald static void att_notify_write_callbacks(hci_con_handle_t con_handle, uint16_t transaction_mode){
5963d71c7a4SMatthias Ringwald     // notify all callbacks
5973d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
5983d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
5993d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
6003d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
6013d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
6023d71c7a4SMatthias Ringwald         (*handler->write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
6033d71c7a4SMatthias Ringwald     }
6043d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return;
6053d71c7a4SMatthias Ringwald     (*att_server_client_write_callback)(con_handle, 0, transaction_mode, 0, NULL, 0);
6063d71c7a4SMatthias Ringwald }
6073d71c7a4SMatthias Ringwald 
6083d71c7a4SMatthias Ringwald // returns first reported error or 0
6093d71c7a4SMatthias Ringwald static uint8_t att_validate_prepared_write(hci_con_handle_t con_handle){
6103d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_t it;
6113d71c7a4SMatthias Ringwald     btstack_linked_list_iterator_init(&it, &service_handlers);
6123d71c7a4SMatthias Ringwald     while (btstack_linked_list_iterator_has_next(&it)){
6133d71c7a4SMatthias Ringwald         att_service_handler_t * handler = (att_service_handler_t*) btstack_linked_list_iterator_next(&it);
6143d71c7a4SMatthias Ringwald         if (!handler->write_callback) continue;
6153d71c7a4SMatthias Ringwald         uint8_t error_code = (*handler->write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
6163d71c7a4SMatthias Ringwald         if (error_code) return error_code;
6173d71c7a4SMatthias Ringwald     }
6183d71c7a4SMatthias Ringwald     if (!att_server_client_write_callback) return 0;
6193d71c7a4SMatthias Ringwald     return (*att_server_client_write_callback)(con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
6203d71c7a4SMatthias Ringwald }
6213d71c7a4SMatthias Ringwald 
6223d71c7a4SMatthias 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){
6233d71c7a4SMatthias Ringwald     att_read_callback_t callback = att_server_read_callback_for_handle(attribute_handle);
62401ac2008SMatthias Ringwald     if (!callback) return 0;
6253d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, offset, buffer, buffer_size);
6263d71c7a4SMatthias Ringwald }
6273d71c7a4SMatthias Ringwald 
6283d71c7a4SMatthias 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){
6293d71c7a4SMatthias Ringwald     switch (transaction_mode){
6303d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_VALIDATE:
6313d71c7a4SMatthias Ringwald             return att_validate_prepared_write(con_handle);
6323d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_EXECUTE:
6333d71c7a4SMatthias Ringwald         case ATT_TRANSACTION_MODE_CANCEL:
6343d71c7a4SMatthias Ringwald             att_notify_write_callbacks(con_handle, transaction_mode);
6353d71c7a4SMatthias Ringwald             return 0;
6363d71c7a4SMatthias Ringwald         default:
6373d71c7a4SMatthias Ringwald             break;
6383d71c7a4SMatthias Ringwald     }
6393d71c7a4SMatthias Ringwald 
640bf78aac6SMatthias Ringwald     // track CCC writes
641bf78aac6SMatthias Ringwald     if (att_is_persistent_ccc(attribute_handle) && offset == 0 && buffer_size == 2){
642bf78aac6SMatthias Ringwald         att_server_persistent_ccc_write(con_handle, attribute_handle, little_endian_read_16(buffer, 0));
643bf78aac6SMatthias Ringwald     }
644bf78aac6SMatthias Ringwald 
64501ac2008SMatthias Ringwald     att_write_callback_t callback = att_server_write_callback_for_handle(attribute_handle);
64601ac2008SMatthias Ringwald     if (!callback) return 0;
6473d71c7a4SMatthias Ringwald     return (*callback)(con_handle, attribute_handle, transaction_mode, offset, buffer, buffer_size);
6483d71c7a4SMatthias Ringwald }
6493d71c7a4SMatthias Ringwald 
6503d71c7a4SMatthias Ringwald /**
6513d71c7a4SMatthias Ringwald  * @brief register read/write callbacks for specific handle range
6523d71c7a4SMatthias Ringwald  * @param att_service_handler_t
6533d71c7a4SMatthias Ringwald  */
6543d71c7a4SMatthias Ringwald void att_server_register_service_handler(att_service_handler_t * handler){
6553d71c7a4SMatthias Ringwald     if (att_service_handler_for_handle(handler->start_handle) ||
6563d71c7a4SMatthias Ringwald         att_service_handler_for_handle(handler->end_handle)){
6573d71c7a4SMatthias Ringwald         log_error("handler for range 0x%04x-0x%04x already registered", handler->start_handle, handler->end_handle);
6583d71c7a4SMatthias Ringwald         return;
6593d71c7a4SMatthias Ringwald     }
6603d71c7a4SMatthias Ringwald     btstack_linked_list_add(&service_handlers, (btstack_linked_item_t*) handler);
6613d71c7a4SMatthias Ringwald }
6623d71c7a4SMatthias Ringwald 
6633deb3ec6SMatthias Ringwald void att_server_init(uint8_t const * db, att_read_callback_t read_callback, att_write_callback_t write_callback){
6643deb3ec6SMatthias Ringwald 
6653d71c7a4SMatthias Ringwald     // store callbacks
6663d71c7a4SMatthias Ringwald     att_server_client_read_callback  = read_callback;
6673d71c7a4SMatthias Ringwald     att_server_client_write_callback = write_callback;
6683d71c7a4SMatthias Ringwald 
6691a389cdcSMatthias Ringwald     // register for HCI Events
670d9a7306aSMatthias Ringwald     hci_event_callback_registration.callback = &att_event_packet_handler;
6711a389cdcSMatthias Ringwald     hci_add_event_handler(&hci_event_callback_registration);
6721a389cdcSMatthias Ringwald 
673406722e4SMatthias Ringwald     // register for SM events
674d9a7306aSMatthias Ringwald     sm_event_callback_registration.callback = &att_event_packet_handler;
675406722e4SMatthias Ringwald     sm_add_event_handler(&sm_event_callback_registration);
6763deb3ec6SMatthias Ringwald 
6771a389cdcSMatthias Ringwald     // and L2CAP ATT Server PDUs
6783deb3ec6SMatthias Ringwald     att_dispatch_register_server(att_packet_handler);
6793deb3ec6SMatthias Ringwald 
6803deb3ec6SMatthias Ringwald     att_set_db(db);
6813d71c7a4SMatthias Ringwald     att_set_read_callback(att_server_read_callback);
6823d71c7a4SMatthias Ringwald     att_set_write_callback(att_server_write_callback);
6833deb3ec6SMatthias Ringwald 
6843deb3ec6SMatthias Ringwald }
6853deb3ec6SMatthias Ringwald 
6863deb3ec6SMatthias Ringwald void att_server_register_packet_handler(btstack_packet_handler_t handler){
6873deb3ec6SMatthias Ringwald     att_client_packet_handler = handler;
6883deb3ec6SMatthias Ringwald }
6893deb3ec6SMatthias Ringwald 
690c141988cSMatthias Ringwald int  att_server_can_send_packet_now(hci_con_handle_t con_handle){
69118707219SMatthias Ringwald 	return att_dispatch_server_can_send_now(con_handle);
6921b96b007SMatthias Ringwald }
6931b96b007SMatthias Ringwald 
694c141988cSMatthias Ringwald void att_server_request_can_send_now_event(hci_con_handle_t con_handle){
695d9d23054SMatthias Ringwald     log_debug("att_server_request_can_send_now_event 0x%04x", con_handle);
6961b96b007SMatthias Ringwald     att_client_waiting_for_can_send = 1;
69718707219SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
6983deb3ec6SMatthias Ringwald }
6993deb3ec6SMatthias Ringwald 
700bb38f057SMatthias Ringwald void att_server_register_can_send_now_callback(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
701bb38f057SMatthias Ringwald     // check if can send already
702bb38f057SMatthias Ringwald     if (att_dispatch_server_can_send_now(con_handle)){
703bb38f057SMatthias Ringwald         callback_registration->callback(callback_registration->context);
704bb38f057SMatthias Ringwald         return;
705bb38f057SMatthias Ringwald     }
70618707219SMatthias Ringwald     callback_registration->context = (void*)(uintptr_t) con_handle;
707bb38f057SMatthias Ringwald     btstack_linked_list_add_tail(&can_send_now_clients, (btstack_linked_item_t*) callback_registration);
708bb38f057SMatthias Ringwald     att_dispatch_server_request_can_send_now_event(con_handle);
709bb38f057SMatthias Ringwald }
710bb38f057SMatthias Ringwald 
71118707219SMatthias Ringwald 
712c141988cSMatthias Ringwald int att_server_notify(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){
71318707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
71418707219SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
71518707219SMatthias Ringwald 
71618707219SMatthias Ringwald     if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
7173deb3ec6SMatthias Ringwald 
7183deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
7193deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
72018707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_notification(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
72118707219SMatthias Ringwald 	return l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
7223deb3ec6SMatthias Ringwald }
7233deb3ec6SMatthias Ringwald 
724c141988cSMatthias Ringwald int att_server_indicate(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t *value, uint16_t value_len){
72518707219SMatthias Ringwald     att_server_t * att_server = att_server_for_handle(con_handle);
72618707219SMatthias Ringwald     if (!att_server) return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
72718707219SMatthias Ringwald 
728eaac31e8SMatthias Ringwald     if (att_server->value_indication_handle) return ATT_HANDLE_VALUE_INDICATION_IN_PROGRESS;
72918707219SMatthias Ringwald     if (!att_dispatch_server_can_send_now(con_handle)) return BTSTACK_ACL_BUFFERS_FULL;
7303deb3ec6SMatthias Ringwald 
7313deb3ec6SMatthias Ringwald     // track indication
73218707219SMatthias Ringwald     att_server->value_indication_handle = attribute_handle;
73318707219SMatthias Ringwald     btstack_run_loop_set_timer_handler(&att_server->value_indication_timer, att_handle_value_indication_timeout);
73418707219SMatthias Ringwald     btstack_run_loop_set_timer(&att_server->value_indication_timer, ATT_TRANSACTION_TIMEOUT_MS);
73518707219SMatthias Ringwald     btstack_run_loop_add_timer(&att_server->value_indication_timer);
7363deb3ec6SMatthias Ringwald 
7373deb3ec6SMatthias Ringwald     l2cap_reserve_packet_buffer();
7383deb3ec6SMatthias Ringwald     uint8_t * packet_buffer = l2cap_get_outgoing_buffer();
73918707219SMatthias Ringwald     uint16_t size = att_prepare_handle_value_indication(&att_server->connection, attribute_handle, value, value_len, packet_buffer);
74018707219SMatthias Ringwald 	l2cap_send_prepared_connectionless(att_server->connection.con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, size);
7413deb3ec6SMatthias Ringwald     return 0;
7423deb3ec6SMatthias Ringwald }
743