xref: /btstack/src/ble/att_db.c (revision c6d8b8168b34299995423fa16e16b23f3c5e486a)
1591423b2SMatthias Ringwald /*
2591423b2SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3591423b2SMatthias Ringwald  *
4591423b2SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5591423b2SMatthias Ringwald  * modification, are permitted provided that the following conditions
6591423b2SMatthias Ringwald  * are met:
7591423b2SMatthias Ringwald  *
8591423b2SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9591423b2SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10591423b2SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11591423b2SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12591423b2SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13591423b2SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14591423b2SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15591423b2SMatthias Ringwald  *    from this software without specific prior written permission.
16591423b2SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17591423b2SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18591423b2SMatthias Ringwald  *    monetary gain.
19591423b2SMatthias Ringwald  *
20591423b2SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21591423b2SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22591423b2SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25591423b2SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26591423b2SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27591423b2SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28591423b2SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29591423b2SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30591423b2SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31591423b2SMatthias Ringwald  * SUCH DAMAGE.
32591423b2SMatthias Ringwald  *
33591423b2SMatthias Ringwald  * Please inquire about commercial licensing options at
34591423b2SMatthias Ringwald  * [email protected]
35591423b2SMatthias Ringwald  *
36591423b2SMatthias Ringwald  */
37591423b2SMatthias Ringwald 
38e501bae0SMatthias Ringwald #define BTSTACK_FILE__ "att_db.c"
39ab2c6ae4SMatthias Ringwald 
40591423b2SMatthias Ringwald #include <string.h>
41591423b2SMatthias Ringwald 
42591423b2SMatthias Ringwald #include "ble/att_db.h"
4359c6af15SMatthias Ringwald #include "ble/core.h"
448ac574d6SMatthias Ringwald #include "bluetooth.h"
45591423b2SMatthias Ringwald #include "btstack_debug.h"
46591423b2SMatthias Ringwald #include "btstack_util.h"
47591423b2SMatthias Ringwald 
4884536330SMatthias Ringwald // check for ENABLE_ATT_DELAYED_READ_RESPONSE -> ENABLE_ATT_DELAYED_RESPONSE,
4984536330SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_READ_RESPONSE
5084536330SMatthias Ringwald     #error "ENABLE_ATT_DELAYED_READ_RESPONSE was replaced by ENABLE_ATT_DELAYED_RESPONSE. Please update btstack_config.h"
5184536330SMatthias Ringwald #endif
5284536330SMatthias Ringwald 
539d622fdeSMatthias Ringwald typedef enum {
549d622fdeSMatthias Ringwald     ATT_READ,
559d622fdeSMatthias Ringwald     ATT_WRITE,
569d622fdeSMatthias Ringwald } att_operation_t;
579d622fdeSMatthias Ringwald 
58591423b2SMatthias Ringwald 
is_Bluetooth_Base_UUID(uint8_t const * uuid)595f5dcb67SMatthias Ringwald static bool is_Bluetooth_Base_UUID(uint8_t const *uuid){
608334d3d8SMatthias Ringwald     // Bluetooth Base UUID 00000000-0000-1000-8000-00805F9B34FB in little endian
618334d3d8SMatthias Ringwald     static const uint8_t bluetooth_base_uuid[] = { 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
628334d3d8SMatthias Ringwald 
639f7e3af1SMilanka Ringwald     if (memcmp(&uuid[0],  &bluetooth_base_uuid[0], 12) != 0){
649f7e3af1SMilanka Ringwald         return false;
659f7e3af1SMilanka Ringwald     }
669f7e3af1SMilanka Ringwald     if (memcmp(&uuid[14], &bluetooth_base_uuid[14], 2) != 0){
679f7e3af1SMilanka Ringwald         return false;
689f7e3af1SMilanka Ringwald     }
694d5915b3SMatthias Ringwald     return true;
70591423b2SMatthias Ringwald 
71591423b2SMatthias Ringwald }
72591423b2SMatthias Ringwald 
uuid16_from_uuid(uint16_t uuid_len,uint8_t * uuid)73591423b2SMatthias Ringwald static uint16_t uuid16_from_uuid(uint16_t uuid_len, uint8_t * uuid){
749f7e3af1SMilanka Ringwald     if (uuid_len == 2u){
759f7e3af1SMilanka Ringwald         return little_endian_read_16(uuid, 0u);
769f7e3af1SMilanka Ringwald     }
779f7e3af1SMilanka Ringwald     if (!is_Bluetooth_Base_UUID(uuid)){
789f7e3af1SMilanka Ringwald         return 0;
799f7e3af1SMilanka Ringwald     }
80591423b2SMatthias Ringwald     return little_endian_read_16(uuid, 12);
81591423b2SMatthias Ringwald }
82591423b2SMatthias Ringwald 
83591423b2SMatthias Ringwald // ATT Database
848b1d64c6SMatthias Ringwald 
85591423b2SMatthias Ringwald // new java-style iterator
86591423b2SMatthias Ringwald typedef struct att_iterator {
87591423b2SMatthias Ringwald     // private
88591423b2SMatthias Ringwald     uint8_t const * att_ptr;
89591423b2SMatthias Ringwald     // public
90591423b2SMatthias Ringwald     uint16_t size;
91591423b2SMatthias Ringwald     uint16_t flags;
92591423b2SMatthias Ringwald     uint16_t handle;
93591423b2SMatthias Ringwald     uint8_t  const * uuid;
94591423b2SMatthias Ringwald     uint16_t value_len;
95591423b2SMatthias Ringwald     uint8_t  const * value;
96591423b2SMatthias Ringwald } att_iterator_t;
97591423b2SMatthias Ringwald 
98fd75a1c5SMatthias Ringwald static void att_persistent_ccc_cache(att_iterator_t * it);
9951e0c94cSMatthias Ringwald 
100b45b7749SMilanka Ringwald static uint8_t const * att_database = NULL;
10151e0c94cSMatthias Ringwald static att_read_callback_t  att_read_callback  = NULL;
10251e0c94cSMatthias Ringwald static att_write_callback_t att_write_callback = NULL;
10356c3a347SMatthias Ringwald static int      att_prepare_write_error_code   = 0;
10451e0c94cSMatthias Ringwald static uint16_t att_prepare_write_error_handle = 0x0000;
10551e0c94cSMatthias Ringwald 
10651e0c94cSMatthias Ringwald // single cache for att_is_persistent_ccc - stores flags before write callback
10751e0c94cSMatthias Ringwald static uint16_t att_persistent_ccc_handle;
108fd75a1c5SMatthias Ringwald static uint16_t att_persistent_ccc_uuid16;
10951e0c94cSMatthias Ringwald 
att_iterator_init(att_iterator_t * it)110591423b2SMatthias Ringwald static void att_iterator_init(att_iterator_t *it){
111b45b7749SMilanka Ringwald     it->att_ptr = att_database;
112591423b2SMatthias Ringwald }
113591423b2SMatthias Ringwald 
att_iterator_has_next(att_iterator_t * it)1144d5915b3SMatthias Ringwald static bool att_iterator_has_next(att_iterator_t *it){
115591423b2SMatthias Ringwald     return it->att_ptr != NULL;
116591423b2SMatthias Ringwald }
117591423b2SMatthias Ringwald 
att_iterator_fetch_next(att_iterator_t * it)118591423b2SMatthias Ringwald static void att_iterator_fetch_next(att_iterator_t *it){
119591423b2SMatthias Ringwald     it->size   = little_endian_read_16(it->att_ptr, 0);
1204ea43905SMatthias Ringwald     if (it->size == 0u){
121591423b2SMatthias Ringwald         it->flags = 0;
122591423b2SMatthias Ringwald         it->handle = 0;
123591423b2SMatthias Ringwald         it->uuid = NULL;
124591423b2SMatthias Ringwald         it->value_len = 0;
125591423b2SMatthias Ringwald         it->value = NULL;
126591423b2SMatthias Ringwald         it->att_ptr = NULL;
127591423b2SMatthias Ringwald         return;
128591423b2SMatthias Ringwald     }
129591423b2SMatthias Ringwald     it->flags  = little_endian_read_16(it->att_ptr, 2);
130591423b2SMatthias Ringwald     it->handle = little_endian_read_16(it->att_ptr, 4);
131591423b2SMatthias Ringwald     it->uuid   = &it->att_ptr[6];
132591423b2SMatthias Ringwald     // handle 128 bit UUIDs
133edc4e664SMilanka Ringwald     if ((it->flags & (uint16_t)ATT_PROPERTY_UUID128) != 0u){
1344ea43905SMatthias Ringwald         it->value_len = it->size - 22u;
135591423b2SMatthias Ringwald         it->value  = &it->att_ptr[22];
136591423b2SMatthias Ringwald     } else {
1374ea43905SMatthias Ringwald         it->value_len = it->size - 8u;
138591423b2SMatthias Ringwald         it->value  = &it->att_ptr[8];
139591423b2SMatthias Ringwald     }
140591423b2SMatthias Ringwald     // advance AFTER setting values
141591423b2SMatthias Ringwald     it->att_ptr += it->size;
142591423b2SMatthias Ringwald }
143591423b2SMatthias Ringwald 
att_iterator_match_uuid16(att_iterator_t * it,uint16_t uuid)1445f5dcb67SMatthias Ringwald static bool att_iterator_match_uuid16(att_iterator_t *it, uint16_t uuid){
1459f7e3af1SMilanka Ringwald     if (it->handle == 0u){
1465f5dcb67SMatthias Ringwald         return false;
1479f7e3af1SMilanka Ringwald     }
1485f5dcb67SMatthias Ringwald     if ((it->flags & (uint16_t)ATT_PROPERTY_UUID128) != 0u){
1499f7e3af1SMilanka Ringwald         if (!is_Bluetooth_Base_UUID(it->uuid)){
1505f5dcb67SMatthias Ringwald             return false;
1519f7e3af1SMilanka Ringwald         }
152591423b2SMatthias Ringwald         return little_endian_read_16(it->uuid, 12) == uuid;
153591423b2SMatthias Ringwald     }
154591423b2SMatthias Ringwald     return little_endian_read_16(it->uuid, 0)  == uuid;
155591423b2SMatthias Ringwald }
156591423b2SMatthias Ringwald 
att_iterator_match_uuid(att_iterator_t * it,uint8_t * uuid,uint16_t uuid_len)1575f5dcb67SMatthias Ringwald static bool att_iterator_match_uuid(att_iterator_t *it, uint8_t *uuid, uint16_t uuid_len){
1589f7e3af1SMilanka Ringwald     if (it->handle == 0u){
1595f5dcb67SMatthias Ringwald         return false;
1609f7e3af1SMilanka Ringwald     }
161591423b2SMatthias Ringwald     // input: UUID16
1624ea43905SMatthias Ringwald     if (uuid_len == 2u) {
163591423b2SMatthias Ringwald         return att_iterator_match_uuid16(it, little_endian_read_16(uuid, 0));
164591423b2SMatthias Ringwald     }
165591423b2SMatthias Ringwald     // input and db: UUID128
166edc4e664SMilanka Ringwald     if ((it->flags & (uint16_t)ATT_PROPERTY_UUID128) != 0u){
167591423b2SMatthias Ringwald         return memcmp(it->uuid, uuid, 16) == 0;
168591423b2SMatthias Ringwald     }
169591423b2SMatthias Ringwald     // input: UUID128, db: UUID16
1709f7e3af1SMilanka Ringwald     if (!is_Bluetooth_Base_UUID(uuid)){
1715f5dcb67SMatthias Ringwald         return false;
1729f7e3af1SMilanka Ringwald     }
173591423b2SMatthias Ringwald     return little_endian_read_16(uuid, 12) == little_endian_read_16(it->uuid, 0);
174591423b2SMatthias Ringwald }
175591423b2SMatthias Ringwald 
176591423b2SMatthias Ringwald 
att_find_handle(att_iterator_t * it,uint16_t handle)1775f5dcb67SMatthias Ringwald static bool att_find_handle(att_iterator_t *it, uint16_t handle){
1789f7e3af1SMilanka Ringwald     if (handle == 0u){
1795f5dcb67SMatthias Ringwald         return false;
1809f7e3af1SMilanka Ringwald     }
181591423b2SMatthias Ringwald     att_iterator_init(it);
182591423b2SMatthias Ringwald     while (att_iterator_has_next(it)){
183591423b2SMatthias Ringwald         att_iterator_fetch_next(it);
1845f5dcb67SMatthias Ringwald         if (it->handle == handle){
1855f5dcb67SMatthias Ringwald             return true;
1869f7e3af1SMilanka Ringwald         }
187591423b2SMatthias Ringwald     }
1885f5dcb67SMatthias Ringwald     return false;
189591423b2SMatthias Ringwald }
190591423b2SMatthias Ringwald 
191591423b2SMatthias Ringwald // experimental client API
att_uuid_for_handle(uint16_t attribute_handle)192fc64f94aSMatthias Ringwald uint16_t att_uuid_for_handle(uint16_t attribute_handle){
193591423b2SMatthias Ringwald     att_iterator_t it;
1945f5dcb67SMatthias Ringwald     bool ok = att_find_handle(&it, attribute_handle);
1959f7e3af1SMilanka Ringwald     if (!ok){
1966f74cd09SMilanka Ringwald         return 0u;
1979f7e3af1SMilanka Ringwald     }
1989f7e3af1SMilanka Ringwald     if ((it.flags & (uint16_t)ATT_PROPERTY_UUID128) != 0u){
1999f7e3af1SMilanka Ringwald         return 0u;
2009f7e3af1SMilanka Ringwald     }
201591423b2SMatthias Ringwald     return little_endian_read_16(it.uuid, 0);
202591423b2SMatthias Ringwald }
2036f74cd09SMilanka Ringwald 
gatt_server_get_const_value_for_handle(uint16_t attribute_handle,uint16_t * out_value_len)2046f74cd09SMilanka Ringwald const uint8_t * gatt_server_get_const_value_for_handle(uint16_t attribute_handle, uint16_t * out_value_len){
2056f74cd09SMilanka Ringwald     att_iterator_t it;
2065f5dcb67SMatthias Ringwald     bool ok = att_find_handle(&it, attribute_handle);
2076f74cd09SMilanka Ringwald     if (!ok){
20834464d0cSMatthias Ringwald         return NULL;
2096f74cd09SMilanka Ringwald     }
2106f74cd09SMilanka Ringwald     if ((it.flags & (uint16_t)ATT_PROPERTY_DYNAMIC) != 0u){
21134464d0cSMatthias Ringwald         return NULL;
2126f74cd09SMilanka Ringwald     }
2136f74cd09SMilanka Ringwald     *out_value_len = it.value_len;
2146f74cd09SMilanka Ringwald     return it.value;
2156f74cd09SMilanka Ringwald }
2166f74cd09SMilanka Ringwald 
217591423b2SMatthias Ringwald // end of client API
218591423b2SMatthias Ringwald 
att_update_value_len(att_iterator_t * it,uint16_t offset,hci_con_handle_t con_handle)219b63105b5SMilanka Ringwald static void att_update_value_len(att_iterator_t *it, uint16_t offset, hci_con_handle_t con_handle) {
2209f7e3af1SMilanka Ringwald     if ((it->flags & (uint16_t)ATT_PROPERTY_DYNAMIC) == 0u){
2219f7e3af1SMilanka Ringwald         return;
2229f7e3af1SMilanka Ringwald     }
223b63105b5SMilanka Ringwald     it->value_len = (*att_read_callback)(con_handle, it->handle, offset, NULL, 0);
224591423b2SMatthias Ringwald     return;
225591423b2SMatthias Ringwald }
226591423b2SMatthias Ringwald 
227591423b2SMatthias Ringwald // copy attribute value from offset into buffer with given size
att_copy_value(att_iterator_t * it,uint16_t offset,uint8_t * buffer,uint16_t buffer_size,hci_con_handle_t con_handle)228711e6c80SMatthias Ringwald static int att_copy_value(att_iterator_t *it, uint16_t offset, uint8_t * buffer, uint16_t buffer_size, hci_con_handle_t con_handle){
229591423b2SMatthias Ringwald 
230591423b2SMatthias Ringwald     // DYNAMIC
231edc4e664SMilanka Ringwald     if ((it->flags & (uint16_t)ATT_PROPERTY_DYNAMIC) != 0u){
2323d71c7a4SMatthias Ringwald         return (*att_read_callback)(con_handle, it->handle, offset, buffer, buffer_size);
233591423b2SMatthias Ringwald     }
234591423b2SMatthias Ringwald 
235591423b2SMatthias Ringwald     // STATIC
23627c566a6SMilanka Ringwald     uint16_t bytes_to_copy = btstack_min(it->value_len - offset, buffer_size);
2376535961aSMatthias Ringwald     (void)memcpy(buffer, it->value, bytes_to_copy);
238591423b2SMatthias Ringwald     return bytes_to_copy;
239591423b2SMatthias Ringwald }
240591423b2SMatthias Ringwald 
att_set_db(uint8_t const * db)241591423b2SMatthias Ringwald void att_set_db(uint8_t const * db){
242fd1be25dSMatthias Ringwald     // validate db version
2439f7e3af1SMilanka Ringwald     if (db == NULL){
2449f7e3af1SMilanka Ringwald         return;
2459f7e3af1SMilanka Ringwald     }
246edc4e664SMilanka Ringwald     if (*db != (uint8_t)ATT_DB_VERSION){
247fd1be25dSMatthias Ringwald         log_error("ATT DB version differs, please regenerate .h from .gatt file or update att_db_util.c");
248fd1be25dSMatthias Ringwald         return;
249fd1be25dSMatthias Ringwald     }
250e7733bf2SMatthias Ringwald     log_info("att_set_db %p", db);
25141f9be70SMatthias Ringwald     // ignore db version
252b45b7749SMilanka Ringwald     att_database = &db[1];
253591423b2SMatthias Ringwald }
254591423b2SMatthias Ringwald 
att_set_read_callback(att_read_callback_t callback)255591423b2SMatthias Ringwald void att_set_read_callback(att_read_callback_t callback){
256591423b2SMatthias Ringwald     att_read_callback = callback;
257591423b2SMatthias Ringwald }
258591423b2SMatthias Ringwald 
att_set_write_callback(att_write_callback_t callback)259591423b2SMatthias Ringwald void att_set_write_callback(att_write_callback_t callback){
260591423b2SMatthias Ringwald     att_write_callback = callback;
261591423b2SMatthias Ringwald }
262591423b2SMatthias Ringwald 
att_dump_attributes(void)263591423b2SMatthias Ringwald void att_dump_attributes(void){
264591423b2SMatthias Ringwald     att_iterator_t it;
265591423b2SMatthias Ringwald     att_iterator_init(&it);
266591423b2SMatthias Ringwald     uint8_t uuid128[16];
267b45b7749SMilanka Ringwald     log_info("att_dump_attributes, table %p", att_database);
268591423b2SMatthias Ringwald     while (att_iterator_has_next(&it)){
269591423b2SMatthias Ringwald         att_iterator_fetch_next(&it);
2704ea43905SMatthias Ringwald         if (it.handle == 0u) {
271591423b2SMatthias Ringwald             log_info("Handle: END");
272591423b2SMatthias Ringwald             return;
273591423b2SMatthias Ringwald         }
274591423b2SMatthias Ringwald         log_info("Handle: 0x%04x, flags: 0x%04x, uuid: ", it.handle, it.flags);
275edc4e664SMilanka Ringwald         if ((it.flags & (uint16_t)ATT_PROPERTY_UUID128) != 0u){
276591423b2SMatthias Ringwald             reverse_128(it.uuid, uuid128);
277591423b2SMatthias Ringwald             log_info("%s", uuid128_to_str(uuid128));
278591423b2SMatthias Ringwald         } else {
279591423b2SMatthias Ringwald             log_info("%04x", little_endian_read_16(it.uuid, 0));
280591423b2SMatthias Ringwald         }
281591423b2SMatthias Ringwald         log_info(", value_len: %u, value: ", it.value_len);
282591423b2SMatthias Ringwald         log_info_hexdump(it.value, it.value_len);
283591423b2SMatthias Ringwald     }
284591423b2SMatthias Ringwald }
285591423b2SMatthias Ringwald 
att_prepare_write_reset(void)286591423b2SMatthias Ringwald static void att_prepare_write_reset(void){
287591423b2SMatthias Ringwald     att_prepare_write_error_code = 0;
288591423b2SMatthias Ringwald     att_prepare_write_error_handle = 0x0000;
289591423b2SMatthias Ringwald }
290591423b2SMatthias Ringwald 
att_prepare_write_update_errors(uint8_t error_code,uint16_t handle)291591423b2SMatthias Ringwald static void att_prepare_write_update_errors(uint8_t error_code, uint16_t handle){
292591423b2SMatthias Ringwald     // first ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LENGTH has highest priority
29338b893aaSMilanka Ringwald     if ((error_code == (uint8_t)ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LENGTH) && (error_code != (uint8_t)att_prepare_write_error_code)){
294591423b2SMatthias Ringwald         att_prepare_write_error_code = error_code;
295591423b2SMatthias Ringwald         att_prepare_write_error_handle = handle;
296591423b2SMatthias Ringwald         return;
297591423b2SMatthias Ringwald     }
298591423b2SMatthias Ringwald     // first ATT_ERROR_INVALID_OFFSET is next
299edc4e664SMilanka Ringwald     if ((error_code == (uint8_t)ATT_ERROR_INVALID_OFFSET) && (att_prepare_write_error_code == 0)){
300591423b2SMatthias Ringwald         att_prepare_write_error_code = error_code;
301591423b2SMatthias Ringwald         att_prepare_write_error_handle = handle;
302591423b2SMatthias Ringwald         return;
303591423b2SMatthias Ringwald     }
304591423b2SMatthias Ringwald }
305591423b2SMatthias Ringwald 
setup_error(uint8_t * response_buffer,uint8_t request_opcode,uint16_t handle,uint8_t error_code)30638460e25SMatthias Ringwald static uint16_t setup_error(uint8_t * response_buffer, uint8_t request_opcode, uint16_t handle, uint8_t error_code){
307edc4e664SMilanka Ringwald     response_buffer[0] = (uint8_t)ATT_ERROR_RESPONSE;
30838460e25SMatthias Ringwald     response_buffer[1] = request_opcode;
309591423b2SMatthias Ringwald     little_endian_store_16(response_buffer, 2, handle);
310591423b2SMatthias Ringwald     response_buffer[4] = error_code;
311591423b2SMatthias Ringwald     return 5;
312591423b2SMatthias Ringwald }
313591423b2SMatthias Ringwald 
setup_error_read_not_permitted(uint8_t * response_buffer,uint8_t request_opcode,uint16_t start_handle)31438460e25SMatthias Ringwald static inline uint16_t setup_error_read_not_permitted(uint8_t * response_buffer, uint8_t request_opcode, uint16_t start_handle){
31538460e25SMatthias Ringwald     return setup_error(response_buffer, request_opcode, start_handle, ATT_ERROR_READ_NOT_PERMITTED);
316591423b2SMatthias Ringwald }
317591423b2SMatthias Ringwald 
setup_error_write_not_permitted(uint8_t * response_buffer,uint8_t request,uint16_t start_handle)31838460e25SMatthias Ringwald static inline uint16_t setup_error_write_not_permitted(uint8_t * response_buffer, uint8_t request, uint16_t start_handle){
319591423b2SMatthias Ringwald     return setup_error(response_buffer, request, start_handle, ATT_ERROR_WRITE_NOT_PERMITTED);
320591423b2SMatthias Ringwald }
321591423b2SMatthias Ringwald 
setup_error_atribute_not_found(uint8_t * response_buffer,uint8_t request_opcode,uint16_t start_handle)32238460e25SMatthias Ringwald static inline uint16_t setup_error_atribute_not_found(uint8_t * response_buffer, uint8_t request_opcode, uint16_t start_handle){
32338460e25SMatthias Ringwald     return setup_error(response_buffer, request_opcode, start_handle, ATT_ERROR_ATTRIBUTE_NOT_FOUND);
324591423b2SMatthias Ringwald }
325591423b2SMatthias Ringwald 
setup_error_invalid_handle(uint8_t * response_buffer,uint8_t request_opcode,uint16_t handle)32638460e25SMatthias Ringwald static inline uint16_t setup_error_invalid_handle(uint8_t * response_buffer, uint8_t request_opcode, uint16_t handle){
32738460e25SMatthias Ringwald     return setup_error(response_buffer, request_opcode, handle, ATT_ERROR_INVALID_HANDLE);
328591423b2SMatthias Ringwald }
329591423b2SMatthias Ringwald 
setup_error_invalid_offset(uint8_t * response_buffer,uint8_t request_opcode,uint16_t handle)33038460e25SMatthias Ringwald static inline uint16_t setup_error_invalid_offset(uint8_t * response_buffer, uint8_t request_opcode, uint16_t handle){
33138460e25SMatthias Ringwald     return setup_error(response_buffer, request_opcode, handle, ATT_ERROR_INVALID_OFFSET);
332591423b2SMatthias Ringwald }
333591423b2SMatthias Ringwald 
setup_error_invalid_pdu(uint8_t * response_buffer,uint8_t request_opcode)33438460e25SMatthias Ringwald static inline uint16_t setup_error_invalid_pdu(uint8_t *response_buffer, uint8_t request_opcode) {
33538460e25SMatthias Ringwald     return setup_error(response_buffer, request_opcode, 0, ATT_ERROR_INVALID_PDU);
3369b49c4f4SMatthias Ringwald }
3379b49c4f4SMatthias Ringwald 
338b6e793c0SMatthias Ringwald struct att_security_settings {
339b6e793c0SMatthias Ringwald     uint8_t required_security_level;
340b6e793c0SMatthias Ringwald     bool    requires_secure_connection;
341b6e793c0SMatthias Ringwald };
342b6e793c0SMatthias Ringwald 
att_validate_security_get_settings(struct att_security_settings * security_settings,att_operation_t operation,att_iterator_t * it)343b6e793c0SMatthias Ringwald static void att_validate_security_get_settings(struct att_security_settings * security_settings, att_operation_t operation, att_iterator_t *it){
344edc4e664SMilanka Ringwald     security_settings->required_security_level = 0u;
345b6e793c0SMatthias Ringwald     security_settings->requires_secure_connection = false;
3461dd680d7SMatthias Ringwald     switch (operation){
3471dd680d7SMatthias Ringwald         case ATT_READ:
348edc4e664SMilanka Ringwald             if ((it->flags & (uint16_t)ATT_PROPERTY_READ_PERMISSION_BIT_0) != 0u){
349edc4e664SMilanka Ringwald                 security_settings->required_security_level |= 1u;
3501dd680d7SMatthias Ringwald             }
351edc4e664SMilanka Ringwald             if ((it->flags & (uint16_t)ATT_PROPERTY_READ_PERMISSION_BIT_1) != 0u){
352edc4e664SMilanka Ringwald                 security_settings->required_security_level |= 2u;
3531dd680d7SMatthias Ringwald             }
354edc4e664SMilanka Ringwald             if ((it->flags & (uint16_t)ATT_PROPERTY_READ_PERMISSION_SC) != 0u){
355b6e793c0SMatthias Ringwald                 security_settings->requires_secure_connection = true;
35696304e93SMatthias Ringwald             }
3571dd680d7SMatthias Ringwald             break;
3581dd680d7SMatthias Ringwald         case ATT_WRITE:
359edc4e664SMilanka Ringwald             if ((it->flags & (uint16_t)ATT_PROPERTY_WRITE_PERMISSION_BIT_0) != 0u){
360edc4e664SMilanka Ringwald                 security_settings->required_security_level |= 1u;
3611dd680d7SMatthias Ringwald             }
362edc4e664SMilanka Ringwald             if ((it->flags & (uint16_t)ATT_PROPERTY_WRITE_PERMISSION_BIT_1) != 0u){
363edc4e664SMilanka Ringwald                 security_settings->required_security_level |= 2u;
3641dd680d7SMatthias Ringwald             }
365edc4e664SMilanka Ringwald             if ((it->flags & (uint16_t)ATT_PROPERTY_WRITE_PERMISSION_SC) != 0u){
366b6e793c0SMatthias Ringwald                 security_settings->requires_secure_connection = true;
36796304e93SMatthias Ringwald             }
3681dd680d7SMatthias Ringwald             break;
3697bbeb3adSMilanka Ringwald         default:
3707bbeb3adSMilanka Ringwald             btstack_assert(false);
3717bbeb3adSMilanka Ringwald             break;
3721dd680d7SMatthias Ringwald     }
373b6e793c0SMatthias Ringwald }
374b6e793c0SMatthias Ringwald 
att_validate_security(att_connection_t * att_connection,att_operation_t operation,att_iterator_t * it)375b6e793c0SMatthias Ringwald static uint8_t att_validate_security(att_connection_t * att_connection, att_operation_t operation, att_iterator_t * it){
376b6e793c0SMatthias Ringwald     struct att_security_settings security_settings;
377b6e793c0SMatthias Ringwald     att_validate_security_get_settings(&security_settings, operation, it);
3781dd680d7SMatthias Ringwald 
379edc4e664SMilanka Ringwald     uint8_t required_encryption_size = (uint8_t)(it->flags >> 12);
3809f7e3af1SMilanka Ringwald     if (required_encryption_size != 0u){
3819f7e3af1SMilanka Ringwald         required_encryption_size++;   // store -1 to fit into 4 bit
3829f7e3af1SMilanka Ringwald     }
3832ea28401SMatthias Ringwald     log_debug("att_validate_security. flags 0x%04x (=> security level %u, key size %u) authorized %u, authenticated %u, encryption_key_size %u, secure connection %u",
38483dbe303SMatthias Ringwald         it->flags, security_settings.required_security_level, required_encryption_size, att_connection->authorized, att_connection->authenticated, att_connection->encryption_key_size, att_connection->secure_connection);
3851dd680d7SMatthias Ringwald 
386b6e793c0SMatthias Ringwald     bool sc_missing = security_settings.requires_secure_connection && (att_connection->secure_connection == 0u);
387b6e793c0SMatthias Ringwald     switch (security_settings.required_security_level){
3882ea28401SMatthias Ringwald         case ATT_SECURITY_AUTHORIZED:
3894ea43905SMatthias Ringwald             if ((att_connection->authorized == 0u) || sc_missing){
390591423b2SMatthias Ringwald                 return ATT_ERROR_INSUFFICIENT_AUTHORIZATION;
391591423b2SMatthias Ringwald             }
392cf373d3aSMatthias Ringwald             /* fall through */
3932ea28401SMatthias Ringwald         case ATT_SECURITY_AUTHENTICATED:
3944ea43905SMatthias Ringwald             if ((att_connection->authenticated == 0u) || sc_missing){
3951dd680d7SMatthias Ringwald                 return ATT_ERROR_INSUFFICIENT_AUTHENTICATION;
3961dd680d7SMatthias Ringwald             }
397cf373d3aSMatthias Ringwald             /* fall through */
3982ea28401SMatthias Ringwald         case ATT_SECURITY_ENCRYPTED:
3994ea43905SMatthias Ringwald             if ((required_encryption_size > 0u) && ((att_connection->encryption_key_size == 0u) || sc_missing)){
400591423b2SMatthias Ringwald                 return ATT_ERROR_INSUFFICIENT_ENCRYPTION;
401591423b2SMatthias Ringwald             }
402591423b2SMatthias Ringwald             if (required_encryption_size > att_connection->encryption_key_size){
403591423b2SMatthias Ringwald                 return ATT_ERROR_INSUFFICIENT_ENCRYPTION_KEY_SIZE;
404591423b2SMatthias Ringwald             }
4052ea28401SMatthias Ringwald             break;
4062ea28401SMatthias Ringwald         default:
4072ea28401SMatthias Ringwald             break;
4081dd680d7SMatthias Ringwald     }
409602e97cdSMilanka Ringwald     return ATT_ERROR_SUCCESS;
410591423b2SMatthias Ringwald }
411591423b2SMatthias Ringwald 
412591423b2SMatthias Ringwald //
413591423b2SMatthias Ringwald // MARK: ATT_EXCHANGE_MTU_REQUEST
414591423b2SMatthias Ringwald //
handle_exchange_mtu_request(att_connection_t * att_connection,uint8_t * request_buffer,uint16_t request_len,uint8_t * response_buffer)415591423b2SMatthias Ringwald static uint16_t handle_exchange_mtu_request(att_connection_t * att_connection, uint8_t * request_buffer,  uint16_t request_len,
416591423b2SMatthias Ringwald                                          uint8_t * response_buffer){
417591423b2SMatthias Ringwald 
4189f7e3af1SMilanka Ringwald     if (request_len != 3u){
4199f7e3af1SMilanka Ringwald         return setup_error_invalid_pdu(response_buffer, ATT_EXCHANGE_MTU_REQUEST);
4209f7e3af1SMilanka Ringwald     }
4219ec2630cSMatthias Ringwald 
422591423b2SMatthias Ringwald     uint16_t client_rx_mtu = little_endian_read_16(request_buffer, 1);
423591423b2SMatthias Ringwald 
424d60453d1SMatthias Ringwald     // find min(local max mtu, remote mtu) >= ATT_DEFAULT_MTU and use as mtu for this connection
425d60453d1SMatthias Ringwald     uint16_t min_mtu = btstack_min(client_rx_mtu, att_connection->max_mtu);
426d60453d1SMatthias Ringwald     uint16_t new_mtu = btstack_max(ATT_DEFAULT_MTU, min_mtu);
427dde9ff1eSMatthias Ringwald     att_connection->mtu_exchanged = true;
428d60453d1SMatthias Ringwald     att_connection->mtu = new_mtu;
429591423b2SMatthias Ringwald 
430591423b2SMatthias Ringwald     response_buffer[0] = ATT_EXCHANGE_MTU_RESPONSE;
431591423b2SMatthias Ringwald     little_endian_store_16(response_buffer, 1, att_connection->mtu);
432591423b2SMatthias Ringwald     return 3;
433591423b2SMatthias Ringwald }
434591423b2SMatthias Ringwald 
435591423b2SMatthias Ringwald 
436591423b2SMatthias Ringwald //
437591423b2SMatthias Ringwald // MARK: ATT_FIND_INFORMATION_REQUEST
438591423b2SMatthias Ringwald //
439591423b2SMatthias Ringwald // TODO: handle other types then GATT_PRIMARY_SERVICE_UUID and GATT_SECONDARY_SERVICE_UUID
440591423b2SMatthias Ringwald //
handle_find_information_request2(att_connection_t * att_connection,uint8_t * response_buffer,uint16_t response_buffer_size,uint16_t start_handle,uint16_t end_handle)441591423b2SMatthias Ringwald static uint16_t handle_find_information_request2(att_connection_t * att_connection, uint8_t * response_buffer, uint16_t response_buffer_size,
442591423b2SMatthias Ringwald                                            uint16_t start_handle, uint16_t end_handle){
443591423b2SMatthias Ringwald 
4449ec2630cSMatthias Ringwald     UNUSED(att_connection);
4459ec2630cSMatthias Ringwald 
446591423b2SMatthias Ringwald     log_info("ATT_FIND_INFORMATION_REQUEST: from %04X to %04X", start_handle, end_handle);
447591423b2SMatthias Ringwald     uint8_t request_type = ATT_FIND_INFORMATION_REQUEST;
448591423b2SMatthias Ringwald 
4494ea43905SMatthias Ringwald     if ((start_handle > end_handle) || (start_handle == 0u)){
450591423b2SMatthias Ringwald         return setup_error_invalid_handle(response_buffer, request_type, start_handle);
451591423b2SMatthias Ringwald     }
452591423b2SMatthias Ringwald 
453591423b2SMatthias Ringwald     uint16_t offset   = 1;
454591423b2SMatthias Ringwald     uint16_t uuid_len = 0;
455591423b2SMatthias Ringwald 
456591423b2SMatthias Ringwald     att_iterator_t it;
457591423b2SMatthias Ringwald     att_iterator_init(&it);
458591423b2SMatthias Ringwald     while (att_iterator_has_next(&it)){
459591423b2SMatthias Ringwald         att_iterator_fetch_next(&it);
4609f7e3af1SMilanka Ringwald         if (!it.handle){
4619f7e3af1SMilanka Ringwald             break;
4629f7e3af1SMilanka Ringwald         }
4639f7e3af1SMilanka Ringwald         if (it.handle > end_handle){
4649f7e3af1SMilanka Ringwald             break;
4659f7e3af1SMilanka Ringwald         }
4669f7e3af1SMilanka Ringwald         if (it.handle < start_handle){
4679f7e3af1SMilanka Ringwald             continue;
4689f7e3af1SMilanka Ringwald         }
469591423b2SMatthias Ringwald 
470591423b2SMatthias Ringwald         // log_info("Handle 0x%04x", it.handle);
471591423b2SMatthias Ringwald 
472edc4e664SMilanka Ringwald         uint16_t this_uuid_len = (it.flags & (uint16_t)ATT_PROPERTY_UUID128) ? 16u : 2u;
473591423b2SMatthias Ringwald 
474591423b2SMatthias Ringwald         // check if value has same len as last one if not first result
4754ea43905SMatthias Ringwald         if (offset > 1u){
476591423b2SMatthias Ringwald             if (this_uuid_len != uuid_len) {
477591423b2SMatthias Ringwald                 break;
478591423b2SMatthias Ringwald             }
479591423b2SMatthias Ringwald         }
480591423b2SMatthias Ringwald 
481591423b2SMatthias Ringwald         // first
4824ea43905SMatthias Ringwald         if (offset == 1u) {
483591423b2SMatthias Ringwald             uuid_len = this_uuid_len;
484591423b2SMatthias Ringwald             // set format field
485edc4e664SMilanka Ringwald             response_buffer[offset] = (it.flags & (uint16_t)ATT_PROPERTY_UUID128) ? 0x02u : 0x01u;
486591423b2SMatthias Ringwald             offset++;
487591423b2SMatthias Ringwald         }
488591423b2SMatthias Ringwald 
489591423b2SMatthias Ringwald         // space?
4909f7e3af1SMilanka Ringwald         if ((offset + 2u + uuid_len) > response_buffer_size){
4919f7e3af1SMilanka Ringwald             break;
4929f7e3af1SMilanka Ringwald         }
493591423b2SMatthias Ringwald 
494591423b2SMatthias Ringwald         // store
495591423b2SMatthias Ringwald         little_endian_store_16(response_buffer, offset, it.handle);
4964ea43905SMatthias Ringwald         offset += 2u;
497591423b2SMatthias Ringwald 
4986535961aSMatthias Ringwald         (void)memcpy(response_buffer + offset, it.uuid, uuid_len);
499591423b2SMatthias Ringwald         offset += uuid_len;
500591423b2SMatthias Ringwald     }
501591423b2SMatthias Ringwald 
5024ea43905SMatthias Ringwald     if (offset == 1u){
503591423b2SMatthias Ringwald         return setup_error_atribute_not_found(response_buffer, request_type, start_handle);
504591423b2SMatthias Ringwald     }
505591423b2SMatthias Ringwald 
506591423b2SMatthias Ringwald     response_buffer[0] = ATT_FIND_INFORMATION_REPLY;
507591423b2SMatthias Ringwald     return offset;
508591423b2SMatthias Ringwald }
509591423b2SMatthias Ringwald 
handle_find_information_request(att_connection_t * att_connection,uint8_t * request_buffer,uint16_t request_len,uint8_t * response_buffer,uint16_t response_buffer_size)510591423b2SMatthias Ringwald static uint16_t handle_find_information_request(att_connection_t * att_connection, uint8_t * request_buffer,  uint16_t request_len,
511591423b2SMatthias Ringwald                                          uint8_t * response_buffer, uint16_t response_buffer_size){
5129b49c4f4SMatthias Ringwald 
5139f7e3af1SMilanka Ringwald     if (request_len != 5u){
5149f7e3af1SMilanka Ringwald         return setup_error_invalid_pdu(response_buffer, ATT_FIND_INFORMATION_REQUEST);
5159f7e3af1SMilanka Ringwald     }
5169b49c4f4SMatthias Ringwald 
5179b49c4f4SMatthias Ringwald     uint16_t start_handle = little_endian_read_16(request_buffer, 1);
5189b49c4f4SMatthias Ringwald     uint16_t end_handle = little_endian_read_16(request_buffer, 3);
5199b49c4f4SMatthias Ringwald     return handle_find_information_request2(att_connection, response_buffer, response_buffer_size, start_handle, end_handle);
520591423b2SMatthias Ringwald }
521591423b2SMatthias Ringwald 
522591423b2SMatthias Ringwald //
523591423b2SMatthias Ringwald // MARK: ATT_FIND_BY_TYPE_VALUE
524591423b2SMatthias Ringwald //
525591423b2SMatthias Ringwald // "Only attributes with attribute handles between and including the Starting Handle parameter
526591423b2SMatthias Ringwald // and the Ending Handle parameter that match the requested attri- bute type and the attribute
527591423b2SMatthias Ringwald // value that have sufficient permissions to allow reading will be returned" -> (1)
528591423b2SMatthias Ringwald //
529591423b2SMatthias Ringwald // TODO: handle other types then GATT_PRIMARY_SERVICE_UUID and GATT_SECONDARY_SERVICE_UUID
530591423b2SMatthias Ringwald //
531591423b2SMatthias Ringwald // NOTE: doesn't handle DYNAMIC values
532591423b2SMatthias Ringwald // NOTE: only supports 16 bit UUIDs
533591423b2SMatthias Ringwald //
handle_find_by_type_value_request(att_connection_t * att_connection,uint8_t * request_buffer,uint16_t request_len,uint8_t * response_buffer,uint16_t response_buffer_size)534883180d3SMatthias Ringwald static uint16_t handle_find_by_type_value_request(att_connection_t * att_connection, uint8_t * request_buffer,  uint16_t request_len,
535883180d3SMatthias Ringwald                                            uint8_t * response_buffer, uint16_t response_buffer_size){
5369ec2630cSMatthias Ringwald     UNUSED(att_connection);
5379ec2630cSMatthias Ringwald 
5389f7e3af1SMilanka Ringwald     if (request_len < 7u){
5399f7e3af1SMilanka Ringwald         return setup_error_invalid_pdu(response_buffer, ATT_FIND_BY_TYPE_VALUE_REQUEST);
5409f7e3af1SMilanka Ringwald     }
5419b49c4f4SMatthias Ringwald 
542883180d3SMatthias Ringwald     // parse request
543883180d3SMatthias Ringwald     uint16_t start_handle = little_endian_read_16(request_buffer, 1);
544883180d3SMatthias Ringwald     uint16_t end_handle = little_endian_read_16(request_buffer, 3);
545883180d3SMatthias Ringwald     uint16_t attribute_type = little_endian_read_16(request_buffer, 5);
546883180d3SMatthias Ringwald     const uint8_t *attribute_value = &request_buffer[7];
5474ea43905SMatthias Ringwald     uint16_t attribute_len = request_len - 7u;
548883180d3SMatthias Ringwald 
549591423b2SMatthias Ringwald     log_info("ATT_FIND_BY_TYPE_VALUE_REQUEST: from %04X to %04X, type %04X, value: ", start_handle, end_handle, attribute_type);
550591423b2SMatthias Ringwald     log_info_hexdump(attribute_value, attribute_len);
551591423b2SMatthias Ringwald     uint8_t request_type = ATT_FIND_BY_TYPE_VALUE_REQUEST;
552591423b2SMatthias Ringwald 
5534ea43905SMatthias Ringwald     if ((start_handle > end_handle) || (start_handle == 0u)){
554591423b2SMatthias Ringwald         return setup_error_invalid_handle(response_buffer, request_type, start_handle);
555591423b2SMatthias Ringwald     }
556591423b2SMatthias Ringwald 
557591423b2SMatthias Ringwald     uint16_t offset      = 1;
55829b68171SMilanka Ringwald     bool in_group        = false;
559591423b2SMatthias Ringwald     uint16_t prev_handle = 0;
560591423b2SMatthias Ringwald 
561591423b2SMatthias Ringwald     att_iterator_t it;
562591423b2SMatthias Ringwald     att_iterator_init(&it);
563591423b2SMatthias Ringwald     while (att_iterator_has_next(&it)){
564591423b2SMatthias Ringwald         att_iterator_fetch_next(&it);
565591423b2SMatthias Ringwald 
5669f7e3af1SMilanka Ringwald         if ((it.handle != 0u) && (it.handle < start_handle)){
5679f7e3af1SMilanka Ringwald             continue;
5689f7e3af1SMilanka Ringwald         }
5699f7e3af1SMilanka Ringwald         if (it.handle > end_handle){
5709f7e3af1SMilanka Ringwald         break;  // (1)
5719f7e3af1SMilanka Ringwald     }
572591423b2SMatthias Ringwald 
573591423b2SMatthias Ringwald         // close current tag, if within a group and a new service definition starts or we reach end of att db
574591423b2SMatthias Ringwald         if (in_group &&
5754ea43905SMatthias Ringwald             ((it.handle == 0u) || att_iterator_match_uuid16(&it, GATT_PRIMARY_SERVICE_UUID) || att_iterator_match_uuid16(&it, GATT_SECONDARY_SERVICE_UUID))){
576591423b2SMatthias Ringwald 
577591423b2SMatthias Ringwald             log_info("End of group, handle 0x%04x", prev_handle);
578591423b2SMatthias Ringwald             little_endian_store_16(response_buffer, offset, prev_handle);
5794ea43905SMatthias Ringwald             offset += 2u;
58029b68171SMilanka Ringwald             in_group = false;
581591423b2SMatthias Ringwald 
582591423b2SMatthias Ringwald             // check if space for another handle pair available
5834ea43905SMatthias Ringwald             if ((offset + 4u) > response_buffer_size){
584591423b2SMatthias Ringwald                 break;
585591423b2SMatthias Ringwald             }
586591423b2SMatthias Ringwald         }
587591423b2SMatthias Ringwald 
588591423b2SMatthias Ringwald         // keep track of previous handle
589591423b2SMatthias Ringwald         prev_handle = it.handle;
590591423b2SMatthias Ringwald 
591591423b2SMatthias Ringwald         // does current attribute match
59238b893aaSMilanka Ringwald         if ((it.handle != 0u) && att_iterator_match_uuid16(&it, attribute_type) && (attribute_len == it.value_len) && (memcmp(attribute_value, it.value, it.value_len) == 0)){
593591423b2SMatthias Ringwald             log_info("Begin of group, handle 0x%04x", it.handle);
594591423b2SMatthias Ringwald             little_endian_store_16(response_buffer, offset, it.handle);
5954ea43905SMatthias Ringwald             offset += 2u;
59629b68171SMilanka Ringwald             in_group = true;
597591423b2SMatthias Ringwald         }
598591423b2SMatthias Ringwald     }
599591423b2SMatthias Ringwald 
6004ea43905SMatthias Ringwald     if (offset == 1u){
601591423b2SMatthias Ringwald         return setup_error_atribute_not_found(response_buffer, request_type, start_handle);
602591423b2SMatthias Ringwald     }
603591423b2SMatthias Ringwald 
604591423b2SMatthias Ringwald     response_buffer[0] = ATT_FIND_BY_TYPE_VALUE_RESPONSE;
605591423b2SMatthias Ringwald     return offset;
606591423b2SMatthias Ringwald }
607591423b2SMatthias Ringwald 
608591423b2SMatthias Ringwald //
609591423b2SMatthias Ringwald // MARK: ATT_READ_BY_TYPE_REQUEST
610591423b2SMatthias Ringwald //
handle_read_by_type_request2(att_connection_t * att_connection,uint8_t * response_buffer,uint16_t response_buffer_size,uint16_t start_handle,uint16_t end_handle,uint16_t attribute_type_len,uint8_t * attribute_type)611591423b2SMatthias Ringwald static uint16_t handle_read_by_type_request2(att_connection_t * att_connection, uint8_t * response_buffer, uint16_t response_buffer_size,
612591423b2SMatthias Ringwald                                       uint16_t start_handle, uint16_t end_handle,
613591423b2SMatthias Ringwald                                       uint16_t attribute_type_len, uint8_t * attribute_type){
614591423b2SMatthias Ringwald 
615591423b2SMatthias Ringwald     log_info("ATT_READ_BY_TYPE_REQUEST: from %04X to %04X, type: ", start_handle, end_handle);
616591423b2SMatthias Ringwald     log_info_hexdump(attribute_type, attribute_type_len);
617591423b2SMatthias Ringwald     uint8_t request_type = ATT_READ_BY_TYPE_REQUEST;
618591423b2SMatthias Ringwald 
6194ea43905SMatthias Ringwald     if ((start_handle > end_handle) || (start_handle == 0u)){
620591423b2SMatthias Ringwald         return setup_error_invalid_handle(response_buffer, request_type, start_handle);
621591423b2SMatthias Ringwald     }
622591423b2SMatthias Ringwald 
623591423b2SMatthias Ringwald     uint16_t offset   = 1;
624591423b2SMatthias Ringwald     uint16_t pair_len = 0;
625591423b2SMatthias Ringwald 
626591423b2SMatthias Ringwald     att_iterator_t it;
627591423b2SMatthias Ringwald     att_iterator_init(&it);
628591423b2SMatthias Ringwald     uint8_t error_code = 0;
629591423b2SMatthias Ringwald     uint16_t first_matching_but_unreadable_handle = 0;
630591423b2SMatthias Ringwald 
631591423b2SMatthias Ringwald     while (att_iterator_has_next(&it)){
632591423b2SMatthias Ringwald         att_iterator_fetch_next(&it);
633591423b2SMatthias Ringwald 
6349f7e3af1SMilanka Ringwald         if ((it.handle == 0u ) || (it.handle > end_handle)){
6359f7e3af1SMilanka Ringwald             break;
6369f7e3af1SMilanka Ringwald         }
6373b9a211cSMatthias Ringwald 
638591423b2SMatthias Ringwald         // does current attribute match
6399f7e3af1SMilanka Ringwald         if ((it.handle < start_handle) || !att_iterator_match_uuid(&it, attribute_type, attribute_type_len)){
6409f7e3af1SMilanka Ringwald             continue;
6419f7e3af1SMilanka Ringwald         }
642591423b2SMatthias Ringwald 
6433b9a211cSMatthias Ringwald         // skip handles that cannot be read but remember that there has been at least one
644edc4e664SMilanka Ringwald         if ((it.flags & (uint16_t)ATT_PROPERTY_READ) == 0u) {
6454ea43905SMatthias Ringwald             if (first_matching_but_unreadable_handle == 0u) {
646591423b2SMatthias Ringwald                 first_matching_but_unreadable_handle = it.handle;
647591423b2SMatthias Ringwald             }
648591423b2SMatthias Ringwald             continue;
649591423b2SMatthias Ringwald         }
650591423b2SMatthias Ringwald 
651591423b2SMatthias Ringwald         // check security requirements
6529d622fdeSMatthias Ringwald         error_code = att_validate_security(att_connection, ATT_READ, &it);
6539f7e3af1SMilanka Ringwald         if (error_code != 0u){
6549f7e3af1SMilanka Ringwald             break;
6559f7e3af1SMilanka Ringwald         }
656591423b2SMatthias Ringwald 
657b63105b5SMilanka Ringwald         att_update_value_len(&it, 0, att_connection->con_handle);
658591423b2SMatthias Ringwald 
6594f8235e6SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
660edc4e664SMilanka Ringwald         if (it.value_len == (uint16_t)ATT_READ_RESPONSE_PENDING){
6613b9a211cSMatthias Ringwald             return ATT_READ_RESPONSE_PENDING;
662e404a688SMatthias Ringwald         }
663e404a688SMatthias Ringwald #endif
664e404a688SMatthias Ringwald 
6650ff1f3d8SMatthias Ringwald         // allow to return ATT Error Code in ATT Read Callback
666edc4e664SMilanka Ringwald         if (it.value_len > (uint16_t)ATT_READ_ERROR_CODE_OFFSET){
667edc4e664SMilanka Ringwald             error_code =  (uint8_t)(it.value_len - (uint16_t)ATT_READ_ERROR_CODE_OFFSET);
6680ff1f3d8SMatthias Ringwald             break;
6690ff1f3d8SMatthias Ringwald         }
6700ff1f3d8SMatthias Ringwald 
671591423b2SMatthias Ringwald         // check if value has same len as last one
6724ea43905SMatthias Ringwald         uint16_t this_pair_len = 2u + it.value_len;
6734ea43905SMatthias Ringwald         if ((offset > 1u) && (pair_len != this_pair_len)) {
674591423b2SMatthias Ringwald             break;
675591423b2SMatthias Ringwald         }
676591423b2SMatthias Ringwald 
677591423b2SMatthias Ringwald         // first
6784ea43905SMatthias Ringwald         if (offset == 1u) {
679591423b2SMatthias Ringwald             pair_len = this_pair_len;
68038460e25SMatthias Ringwald             response_buffer[offset] = (uint8_t) pair_len;
681591423b2SMatthias Ringwald             offset++;
682591423b2SMatthias Ringwald         }
683591423b2SMatthias Ringwald 
684591423b2SMatthias Ringwald         // space?
685c1ab6cc1SMatthias Ringwald         if ((offset + pair_len) > response_buffer_size) {
6869f7e3af1SMilanka Ringwald             if (offset > 2u){
6879f7e3af1SMilanka Ringwald                 break;
6889f7e3af1SMilanka Ringwald             }
6894ea43905SMatthias Ringwald             it.value_len = response_buffer_size - 4u;
6904ea43905SMatthias Ringwald             response_buffer[1u] = 2u + it.value_len;
691591423b2SMatthias Ringwald         }
692591423b2SMatthias Ringwald 
693591423b2SMatthias Ringwald         // store
694591423b2SMatthias Ringwald         little_endian_store_16(response_buffer, offset, it.handle);
6954ea43905SMatthias Ringwald         offset += 2u;
696591423b2SMatthias Ringwald         uint16_t bytes_copied = att_copy_value(&it, 0, response_buffer + offset, it.value_len, att_connection->con_handle);
697591423b2SMatthias Ringwald         offset += bytes_copied;
698591423b2SMatthias Ringwald     }
699591423b2SMatthias Ringwald 
700591423b2SMatthias Ringwald     // at least one attribute could be read
7014ea43905SMatthias Ringwald     if (offset > 1u){
702591423b2SMatthias Ringwald         response_buffer[0] = ATT_READ_BY_TYPE_RESPONSE;
703591423b2SMatthias Ringwald         return offset;
704591423b2SMatthias Ringwald     }
705591423b2SMatthias Ringwald 
706591423b2SMatthias Ringwald     // first attribute had an error
7074ea43905SMatthias Ringwald     if (error_code != 0u){
708591423b2SMatthias Ringwald         return setup_error(response_buffer, request_type, start_handle, error_code);
709591423b2SMatthias Ringwald     }
710591423b2SMatthias Ringwald 
711591423b2SMatthias Ringwald     // no other errors, but all found attributes had been non-readable
7124ea43905SMatthias Ringwald     if (first_matching_but_unreadable_handle != 0u){
713591423b2SMatthias Ringwald         return setup_error_read_not_permitted(response_buffer, request_type, first_matching_but_unreadable_handle);
714591423b2SMatthias Ringwald     }
715591423b2SMatthias Ringwald 
716591423b2SMatthias Ringwald     // attribute not found
717591423b2SMatthias Ringwald     return setup_error_atribute_not_found(response_buffer, request_type, start_handle);
718591423b2SMatthias Ringwald }
719591423b2SMatthias Ringwald 
handle_read_by_type_request(att_connection_t * att_connection,uint8_t * request_buffer,uint16_t request_len,uint8_t * response_buffer,uint16_t response_buffer_size)720591423b2SMatthias Ringwald static uint16_t handle_read_by_type_request(att_connection_t * att_connection, uint8_t * request_buffer,  uint16_t request_len,
721591423b2SMatthias Ringwald                                      uint8_t * response_buffer, uint16_t response_buffer_size){
7229b49c4f4SMatthias Ringwald 
7239b49c4f4SMatthias Ringwald     uint16_t attribute_type_len;
7249b49c4f4SMatthias Ringwald     switch (request_len){
7259b49c4f4SMatthias Ringwald         case 7:
726591423b2SMatthias Ringwald             attribute_type_len = 2;
7279b49c4f4SMatthias Ringwald             break;
7289b49c4f4SMatthias Ringwald         case 21:
729591423b2SMatthias Ringwald             attribute_type_len = 16;
7309b49c4f4SMatthias Ringwald             break;
7319b49c4f4SMatthias Ringwald         default:
7329b49c4f4SMatthias Ringwald             return setup_error_invalid_pdu(response_buffer, ATT_READ_BY_TYPE_REQUEST);
733591423b2SMatthias Ringwald     }
7349b49c4f4SMatthias Ringwald 
7359b49c4f4SMatthias Ringwald     uint16_t start_handle = little_endian_read_16(request_buffer, 1);
7369b49c4f4SMatthias Ringwald     uint16_t end_handle = little_endian_read_16(request_buffer, 3);
7379b49c4f4SMatthias Ringwald     return handle_read_by_type_request2(att_connection, response_buffer, response_buffer_size, start_handle, end_handle, attribute_type_len, &request_buffer[5]);
738591423b2SMatthias Ringwald }
739591423b2SMatthias Ringwald 
740591423b2SMatthias Ringwald //
741591423b2SMatthias Ringwald // MARK: ATT_READ_BY_TYPE_REQUEST
742591423b2SMatthias Ringwald //
handle_read_request2(att_connection_t * att_connection,uint8_t * response_buffer,uint16_t response_buffer_size,uint16_t handle)743591423b2SMatthias Ringwald static uint16_t handle_read_request2(att_connection_t * att_connection, uint8_t * response_buffer, uint16_t response_buffer_size, uint16_t handle){
744591423b2SMatthias Ringwald 
745591423b2SMatthias Ringwald     log_info("ATT_READ_REQUEST: handle %04x", handle);
746591423b2SMatthias Ringwald     uint8_t request_type = ATT_READ_REQUEST;
747591423b2SMatthias Ringwald 
748591423b2SMatthias Ringwald     att_iterator_t it;
7495f5dcb67SMatthias Ringwald     bool ok = att_find_handle(&it, handle);
750591423b2SMatthias Ringwald     if (!ok){
751591423b2SMatthias Ringwald         return setup_error_invalid_handle(response_buffer, request_type, handle);
752591423b2SMatthias Ringwald     }
753591423b2SMatthias Ringwald 
754591423b2SMatthias Ringwald     // check if handle can be read
755edc4e664SMilanka Ringwald     if ((it.flags & (uint16_t)ATT_PROPERTY_READ) == 0u) {
756591423b2SMatthias Ringwald         return setup_error_read_not_permitted(response_buffer, request_type, handle);
757591423b2SMatthias Ringwald     }
758591423b2SMatthias Ringwald 
759591423b2SMatthias Ringwald     // check security requirements
7609d622fdeSMatthias Ringwald     uint8_t error_code = att_validate_security(att_connection, ATT_READ, &it);
761edc4e664SMilanka Ringwald     if (error_code != 0u) {
762591423b2SMatthias Ringwald         return setup_error(response_buffer, request_type, handle, error_code);
763591423b2SMatthias Ringwald     }
764591423b2SMatthias Ringwald 
765b63105b5SMilanka Ringwald     att_update_value_len(&it, 0, att_connection->con_handle);
766591423b2SMatthias Ringwald 
7674f8235e6SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
7689f7e3af1SMilanka Ringwald     if (it.value_len == (uint16_t)ATT_READ_RESPONSE_PENDING){
7699f7e3af1SMilanka Ringwald         return ATT_READ_RESPONSE_PENDING;
7709f7e3af1SMilanka Ringwald     }
771e404a688SMatthias Ringwald #endif
772e404a688SMatthias Ringwald 
7730ff1f3d8SMatthias Ringwald     // allow to return ATT Error Code in ATT Read Callback
774edc4e664SMilanka Ringwald     if (it.value_len > (uint16_t)ATT_READ_ERROR_CODE_OFFSET){
775edc4e664SMilanka Ringwald         error_code = (uint8_t)(it.value_len - (uint16_t)ATT_READ_ERROR_CODE_OFFSET);
7760ff1f3d8SMatthias Ringwald         return setup_error(response_buffer, request_type, handle, error_code);
7770ff1f3d8SMatthias Ringwald     }
7780ff1f3d8SMatthias Ringwald 
779591423b2SMatthias Ringwald     // store
7806098e908SMatthias Ringwald     uint16_t offset   = 1;
7816098e908SMatthias Ringwald     uint16_t bytes_copied = att_copy_value(&it, 0, response_buffer + offset, response_buffer_size - offset, att_connection->con_handle);
782591423b2SMatthias Ringwald     offset += bytes_copied;
783591423b2SMatthias Ringwald 
784591423b2SMatthias Ringwald     response_buffer[0] = ATT_READ_RESPONSE;
785591423b2SMatthias Ringwald     return offset;
786591423b2SMatthias Ringwald }
787591423b2SMatthias Ringwald 
handle_read_request(att_connection_t * att_connection,uint8_t * request_buffer,uint16_t request_len,uint8_t * response_buffer,uint16_t response_buffer_size)788591423b2SMatthias Ringwald static uint16_t handle_read_request(att_connection_t * att_connection, uint8_t * request_buffer,  uint16_t request_len,
789591423b2SMatthias Ringwald                              uint8_t * response_buffer, uint16_t response_buffer_size){
7909b49c4f4SMatthias Ringwald 
7919f7e3af1SMilanka Ringwald     if (request_len != 3u){
7929f7e3af1SMilanka Ringwald         return setup_error_invalid_pdu(response_buffer, ATT_READ_REQUEST);
7939f7e3af1SMilanka Ringwald     }
7949b49c4f4SMatthias Ringwald 
7959b49c4f4SMatthias Ringwald     uint16_t handle = little_endian_read_16(request_buffer, 1);
7969b49c4f4SMatthias Ringwald     return handle_read_request2(att_connection, response_buffer, response_buffer_size, handle);
797591423b2SMatthias Ringwald }
798591423b2SMatthias Ringwald 
7999b49c4f4SMatthias Ringwald //s
800591423b2SMatthias Ringwald // MARK: ATT_READ_BLOB_REQUEST 0x0c
801591423b2SMatthias Ringwald //
handle_read_blob_request2(att_connection_t * att_connection,uint8_t * response_buffer,uint16_t response_buffer_size,uint16_t handle,uint16_t value_offset)802591423b2SMatthias Ringwald static uint16_t handle_read_blob_request2(att_connection_t * att_connection, uint8_t * response_buffer, uint16_t response_buffer_size, uint16_t handle, uint16_t value_offset){
803591423b2SMatthias Ringwald     log_info("ATT_READ_BLOB_REQUEST: handle %04x, offset %u", handle, value_offset);
804591423b2SMatthias Ringwald     uint8_t request_type = ATT_READ_BLOB_REQUEST;
805591423b2SMatthias Ringwald 
806591423b2SMatthias Ringwald     att_iterator_t it;
8075f5dcb67SMatthias Ringwald     bool ok = att_find_handle(&it, handle);
808591423b2SMatthias Ringwald     if (!ok){
809591423b2SMatthias Ringwald         return setup_error_invalid_handle(response_buffer, request_type, handle);
810591423b2SMatthias Ringwald     }
811591423b2SMatthias Ringwald 
812591423b2SMatthias Ringwald     // check if handle can be read
813edc4e664SMilanka Ringwald     if ((it.flags & (uint16_t)ATT_PROPERTY_READ) == 0u) {
814591423b2SMatthias Ringwald         return setup_error_read_not_permitted(response_buffer, request_type, handle);
815591423b2SMatthias Ringwald     }
816591423b2SMatthias Ringwald 
817591423b2SMatthias Ringwald     // check security requirements
8189d622fdeSMatthias Ringwald     uint8_t error_code = att_validate_security(att_connection, ATT_READ, &it);
819edc4e664SMilanka Ringwald     if (error_code != 0u) {
820591423b2SMatthias Ringwald         return setup_error(response_buffer, request_type, handle, error_code);
821591423b2SMatthias Ringwald     }
822591423b2SMatthias Ringwald 
823b63105b5SMilanka Ringwald     att_update_value_len(&it, value_offset, att_connection->con_handle);
824591423b2SMatthias Ringwald 
8254f8235e6SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
8269f7e3af1SMilanka Ringwald     if (it.value_len == (uint16_t)ATT_READ_RESPONSE_PENDING){
8279f7e3af1SMilanka Ringwald         return ATT_READ_RESPONSE_PENDING;
8289f7e3af1SMilanka Ringwald     }
829e404a688SMatthias Ringwald #endif
830e404a688SMatthias Ringwald 
8310ff1f3d8SMatthias Ringwald     // allow to return ATT Error Code in ATT Read Callback
832edc4e664SMilanka Ringwald     if (it.value_len > (uint16_t)ATT_READ_ERROR_CODE_OFFSET){
833edc4e664SMilanka Ringwald         error_code = (uint8_t)(it.value_len - (uint16_t)ATT_READ_ERROR_CODE_OFFSET);
8340ff1f3d8SMatthias Ringwald         return setup_error(response_buffer, request_type, handle, error_code);
8350ff1f3d8SMatthias Ringwald     }
8360ff1f3d8SMatthias Ringwald 
837591423b2SMatthias Ringwald     if (value_offset > it.value_len){
838591423b2SMatthias Ringwald         return setup_error_invalid_offset(response_buffer, request_type, handle);
839591423b2SMatthias Ringwald     }
840591423b2SMatthias Ringwald 
841af1b7cdcSMatthias Ringwald     // prepare response
842591423b2SMatthias Ringwald     response_buffer[0] = ATT_READ_BLOB_RESPONSE;
843af1b7cdcSMatthias Ringwald     uint16_t offset   = 1;
844af1b7cdcSMatthias Ringwald 
845af1b7cdcSMatthias Ringwald     // fetch more data if available
846af1b7cdcSMatthias Ringwald     if (value_offset < it.value_len){
847af1b7cdcSMatthias Ringwald         uint16_t bytes_copied = att_copy_value(&it, value_offset, &response_buffer[offset], response_buffer_size - offset, att_connection->con_handle);
848af1b7cdcSMatthias Ringwald         offset += bytes_copied;
849af1b7cdcSMatthias Ringwald     }
850591423b2SMatthias Ringwald     return offset;
851591423b2SMatthias Ringwald }
852591423b2SMatthias Ringwald 
handle_read_blob_request(att_connection_t * att_connection,uint8_t * request_buffer,uint16_t request_len,uint8_t * response_buffer,uint16_t response_buffer_size)853591423b2SMatthias Ringwald static uint16_t handle_read_blob_request(att_connection_t * att_connection, uint8_t * request_buffer,  uint16_t request_len,
854591423b2SMatthias Ringwald                                   uint8_t * response_buffer, uint16_t response_buffer_size){
8559b49c4f4SMatthias Ringwald 
8569f7e3af1SMilanka Ringwald     if (request_len != 5u){
8579f7e3af1SMilanka Ringwald         return setup_error_invalid_pdu(response_buffer, ATT_READ_BLOB_REQUEST);
8589f7e3af1SMilanka Ringwald     }
8599b49c4f4SMatthias Ringwald 
8609b49c4f4SMatthias Ringwald     uint16_t handle = little_endian_read_16(request_buffer, 1);
8619b49c4f4SMatthias Ringwald     uint16_t value_offset = little_endian_read_16(request_buffer, 3);
8629b49c4f4SMatthias Ringwald     return handle_read_blob_request2(att_connection, response_buffer, response_buffer_size, handle, value_offset);
863591423b2SMatthias Ringwald }
864591423b2SMatthias Ringwald 
865591423b2SMatthias Ringwald //
866591423b2SMatthias Ringwald // MARK: ATT_READ_MULTIPLE_REQUEST 0x0e
8672c4384ebSMatthias Ringwald // MARK: ATT_READ_MULTIPLE_REQUEST 0x20
868591423b2SMatthias Ringwald //
handle_read_multiple_request2(att_connection_t * att_connection,uint8_t * response_buffer,uint16_t response_buffer_size,uint16_t num_handles,uint8_t * handles,bool store_length)8692c4384ebSMatthias Ringwald static uint16_t handle_read_multiple_request2(att_connection_t * att_connection, uint8_t * response_buffer, uint16_t response_buffer_size, uint16_t num_handles, uint8_t * handles, bool store_length){
8702c4384ebSMatthias Ringwald     log_info("ATT_READ_MULTIPLE_(VARIABLE_)REQUEST: num handles %u", num_handles);
8712c4384ebSMatthias Ringwald     uint8_t request_type  = store_length ? ATT_READ_MULTIPLE_VARIABLE_REQ : ATT_READ_MULTIPLE_REQUEST;
8722c4384ebSMatthias Ringwald     uint8_t response_type = store_length ? ATT_READ_MULTIPLE_VARIABLE_RSP : ATT_READ_MULTIPLE_RESPONSE;
873591423b2SMatthias Ringwald     uint16_t offset   = 1;
874591423b2SMatthias Ringwald 
875edc4e664SMilanka Ringwald     uint16_t i;
876591423b2SMatthias Ringwald     uint8_t  error_code = 0;
877591423b2SMatthias Ringwald     uint16_t handle = 0;
878e404a688SMatthias Ringwald 
8794f8235e6SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
8804d5915b3SMatthias Ringwald     bool read_request_pending = false;
881e404a688SMatthias Ringwald #endif
882e404a688SMatthias Ringwald 
883591423b2SMatthias Ringwald     for (i=0; i<num_handles; i++){
884591423b2SMatthias Ringwald         handle = little_endian_read_16(handles, i << 1);
885591423b2SMatthias Ringwald 
8864ea43905SMatthias Ringwald         if (handle == 0u){
887591423b2SMatthias Ringwald             return setup_error_invalid_handle(response_buffer, request_type, handle);
888591423b2SMatthias Ringwald         }
889591423b2SMatthias Ringwald 
890591423b2SMatthias Ringwald         att_iterator_t it;
891591423b2SMatthias Ringwald 
8925f5dcb67SMatthias Ringwald         bool ok = att_find_handle(&it, handle);
893591423b2SMatthias Ringwald         if (!ok){
894591423b2SMatthias Ringwald             return setup_error_invalid_handle(response_buffer, request_type, handle);
895591423b2SMatthias Ringwald         }
896591423b2SMatthias Ringwald 
897591423b2SMatthias Ringwald         // check if handle can be read
898edc4e664SMilanka Ringwald         if ((it.flags & (uint16_t)ATT_PROPERTY_READ) == 0u) {
899edc4e664SMilanka Ringwald             error_code = (uint8_t)ATT_ERROR_READ_NOT_PERMITTED;
900591423b2SMatthias Ringwald             break;
901591423b2SMatthias Ringwald         }
902591423b2SMatthias Ringwald 
903591423b2SMatthias Ringwald         // check security requirements
9049d622fdeSMatthias Ringwald         error_code = att_validate_security(att_connection, ATT_READ, &it);
9059f7e3af1SMilanka Ringwald         if (error_code != 0u){
9069f7e3af1SMilanka Ringwald             break;
9079f7e3af1SMilanka Ringwald         }
908eeeae295SMatthias Ringwald 
909b63105b5SMilanka Ringwald         att_update_value_len(&it, 0, att_connection->con_handle);
910591423b2SMatthias Ringwald 
9114f8235e6SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
912edc4e664SMilanka Ringwald         if (it.value_len == (uint16_t)ATT_READ_RESPONSE_PENDING) {
9134d5915b3SMatthias Ringwald             read_request_pending = true;
914e404a688SMatthias Ringwald         }
9159f7e3af1SMilanka Ringwald         if (read_request_pending){
9169f7e3af1SMilanka Ringwald             continue;
9179f7e3af1SMilanka Ringwald         }
918e404a688SMatthias Ringwald #endif
919e404a688SMatthias Ringwald 
9200ff1f3d8SMatthias Ringwald         // allow to return ATT Error Code in ATT Read Callback
921edc4e664SMilanka Ringwald         if (it.value_len > (uint16_t)ATT_READ_ERROR_CODE_OFFSET){
922edc4e664SMilanka Ringwald             error_code = (uint8_t)(it.value_len - (uint16_t)ATT_READ_ERROR_CODE_OFFSET);
9230ff1f3d8SMatthias Ringwald             break;
9240ff1f3d8SMatthias Ringwald         }
9250ff1f3d8SMatthias Ringwald 
9262c4384ebSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
9272c4384ebSMatthias Ringwald         // assert that at least Value Length can be stored
9282c4384ebSMatthias Ringwald         if (store_length && ((offset + 2) >= response_buffer_size)){
9292c4384ebSMatthias Ringwald             break;
9302c4384ebSMatthias Ringwald         }
9312c4384ebSMatthias Ringwald         // skip length field
9322c4384ebSMatthias Ringwald         uint16_t offset_value_length = offset;
9332c4384ebSMatthias Ringwald         if (store_length){
9342c4384ebSMatthias Ringwald             offset += 2;
9352c4384ebSMatthias Ringwald         }
9362c4384ebSMatthias Ringwald #endif
9372c4384ebSMatthias Ringwald         // store data
9386098e908SMatthias Ringwald         uint16_t bytes_copied = att_copy_value(&it, 0, response_buffer + offset, response_buffer_size - offset, att_connection->con_handle);
939591423b2SMatthias Ringwald         offset += bytes_copied;
9402c4384ebSMatthias Ringwald #ifdef ENABLE_GATT_OVER_EATT
9412c4384ebSMatthias Ringwald         // set length field
9422c4384ebSMatthias Ringwald         if (store_length) {
9432c4384ebSMatthias Ringwald             little_endian_store_16(response_buffer, offset_value_length, bytes_copied);
9442c4384ebSMatthias Ringwald         }
9452c4384ebSMatthias Ringwald #endif
946591423b2SMatthias Ringwald     }
947591423b2SMatthias Ringwald 
948edc4e664SMilanka Ringwald     if (error_code != 0u){
949591423b2SMatthias Ringwald         return setup_error(response_buffer, request_type, handle, error_code);
950591423b2SMatthias Ringwald     }
951591423b2SMatthias Ringwald 
9522c4384ebSMatthias Ringwald     response_buffer[0] = (uint8_t)response_type;
953591423b2SMatthias Ringwald     return offset;
954591423b2SMatthias Ringwald }
9552c4384ebSMatthias Ringwald 
9562c4384ebSMatthias Ringwald static uint16_t
handle_read_multiple_request(att_connection_t * att_connection,uint8_t * request_buffer,uint16_t request_len,uint8_t * response_buffer,uint16_t response_buffer_size,bool store_length)9572c4384ebSMatthias Ringwald handle_read_multiple_request(att_connection_t *att_connection, uint8_t *request_buffer, uint16_t request_len,
9582c4384ebSMatthias Ringwald                              uint8_t *response_buffer, uint16_t response_buffer_size, bool store_length) {
9592c4384ebSMatthias Ringwald 
9602c4384ebSMatthias Ringwald     uint8_t request_type = store_length ? ATT_READ_MULTIPLE_VARIABLE_REQ : ATT_READ_MULTIPLE_REQUEST;
9619b49c4f4SMatthias Ringwald 
9629b49c4f4SMatthias Ringwald     // 1 byte opcode + two or more attribute handles (2 bytes each)
9639f7e3af1SMilanka Ringwald     if ( (request_len < 5u) || ((request_len & 1u) == 0u) ){
9642c4384ebSMatthias Ringwald         return setup_error_invalid_pdu(response_buffer, request_type);
9659f7e3af1SMilanka Ringwald     }
9669b49c4f4SMatthias Ringwald 
9672c4384ebSMatthias Ringwald     uint8_t num_handles = (request_len - 1u) >> 1u;
9682c4384ebSMatthias Ringwald     return handle_read_multiple_request2(att_connection, response_buffer, response_buffer_size, num_handles,
9692c4384ebSMatthias Ringwald                                          &request_buffer[1], store_length);
970591423b2SMatthias Ringwald }
971591423b2SMatthias Ringwald 
972591423b2SMatthias Ringwald //
973591423b2SMatthias Ringwald // MARK: ATT_READ_BY_GROUP_TYPE_REQUEST 0x10
974591423b2SMatthias Ringwald //
975591423b2SMatthias Ringwald // Only handles GATT_PRIMARY_SERVICE_UUID and GATT_SECONDARY_SERVICE_UUID
976591423b2SMatthias Ringwald // Core v4.0, vol 3, part g, 2.5.3
977591423b2SMatthias Ringwald // "The «Primary Service» and «Secondary Service» grouping types may be used in the Read By Group Type Request.
978591423b2SMatthias Ringwald //  The «Characteristic» grouping type shall not be used in the ATT Read By Group Type Request."
979591423b2SMatthias Ringwald //
980591423b2SMatthias Ringwald // NOTE: doesn't handle DYNAMIC values
981591423b2SMatthias Ringwald //
982591423b2SMatthias Ringwald // NOTE: we don't check for security as PRIMARY and SECONDAY SERVICE definition shouldn't be protected
983591423b2SMatthias Ringwald // Core 4.0, vol 3, part g, 8.1
984591423b2SMatthias Ringwald // "The list of services and characteristics that a device supports is not considered private or
985591423b2SMatthias Ringwald //  confidential information, and therefore the Service and Characteristic Discovery procedures
986591423b2SMatthias Ringwald //  shall always be permitted. "
987591423b2SMatthias Ringwald //
handle_read_by_group_type_request2(att_connection_t * att_connection,uint8_t * response_buffer,uint16_t response_buffer_size,uint16_t start_handle,uint16_t end_handle,uint16_t attribute_type_len,uint8_t * attribute_type)988591423b2SMatthias Ringwald static uint16_t handle_read_by_group_type_request2(att_connection_t * att_connection, uint8_t * response_buffer, uint16_t response_buffer_size,
989591423b2SMatthias Ringwald                                             uint16_t start_handle, uint16_t end_handle,
990591423b2SMatthias Ringwald                                             uint16_t attribute_type_len, uint8_t * attribute_type){
991591423b2SMatthias Ringwald 
9929ec2630cSMatthias Ringwald     UNUSED(att_connection);
9939ec2630cSMatthias Ringwald 
994591423b2SMatthias Ringwald     log_info("ATT_READ_BY_GROUP_TYPE_REQUEST: from %04X to %04X, buffer size %u, type: ", start_handle, end_handle, response_buffer_size);
995591423b2SMatthias Ringwald     log_info_hexdump(attribute_type, attribute_type_len);
996591423b2SMatthias Ringwald     uint8_t request_type = ATT_READ_BY_GROUP_TYPE_REQUEST;
997591423b2SMatthias Ringwald 
9984ea43905SMatthias Ringwald     if ((start_handle > end_handle) || (start_handle == 0u)){
999591423b2SMatthias Ringwald         return setup_error_invalid_handle(response_buffer, request_type, start_handle);
1000591423b2SMatthias Ringwald     }
1001591423b2SMatthias Ringwald 
1002591423b2SMatthias Ringwald     // assert UUID is primary or secondary service uuid
1003591423b2SMatthias Ringwald     uint16_t uuid16 = uuid16_from_uuid(attribute_type_len, attribute_type);
1004edc4e664SMilanka Ringwald     if ((uuid16 != (uint16_t)GATT_PRIMARY_SERVICE_UUID) && (uuid16 != (uint16_t)GATT_SECONDARY_SERVICE_UUID)){
1005591423b2SMatthias Ringwald         return setup_error(response_buffer, request_type, start_handle, ATT_ERROR_UNSUPPORTED_GROUP_TYPE);
1006591423b2SMatthias Ringwald     }
1007591423b2SMatthias Ringwald 
1008591423b2SMatthias Ringwald     uint16_t offset   = 1;
1009591423b2SMatthias Ringwald     uint16_t pair_len = 0;
101029b68171SMilanka Ringwald     bool     in_group = false;
1011591423b2SMatthias Ringwald     uint16_t group_start_handle = 0;
1012591423b2SMatthias Ringwald     uint8_t const * group_start_value = NULL;
1013591423b2SMatthias Ringwald     uint16_t prev_handle = 0;
1014591423b2SMatthias Ringwald 
1015591423b2SMatthias Ringwald     att_iterator_t it;
1016591423b2SMatthias Ringwald     att_iterator_init(&it);
1017591423b2SMatthias Ringwald     while (att_iterator_has_next(&it)){
1018591423b2SMatthias Ringwald         att_iterator_fetch_next(&it);
1019591423b2SMatthias Ringwald 
10209f7e3af1SMilanka Ringwald         if ((it.handle != 0u) && (it.handle < start_handle)){
10219f7e3af1SMilanka Ringwald             continue;
10229f7e3af1SMilanka Ringwald         }
10239f7e3af1SMilanka Ringwald         if (it.handle > end_handle){
10249f7e3af1SMilanka Ringwald             break;  // (1)
10259f7e3af1SMilanka Ringwald         }
1026591423b2SMatthias Ringwald 
1027591423b2SMatthias Ringwald         // log_info("Handle 0x%04x", it.handle);
1028591423b2SMatthias Ringwald 
1029591423b2SMatthias Ringwald         // close current tag, if within a group and a new service definition starts or we reach end of att db
1030591423b2SMatthias Ringwald         if (in_group &&
10314ea43905SMatthias Ringwald             ((it.handle == 0u) || att_iterator_match_uuid16(&it, GATT_PRIMARY_SERVICE_UUID) || att_iterator_match_uuid16(&it, GATT_SECONDARY_SERVICE_UUID))){
1032591423b2SMatthias Ringwald             // log_info("End of group, handle 0x%04x, val_len: %u", prev_handle, pair_len - 4);
1033591423b2SMatthias Ringwald 
1034591423b2SMatthias Ringwald             little_endian_store_16(response_buffer, offset, group_start_handle);
10354ea43905SMatthias Ringwald             offset += 2u;
1036591423b2SMatthias Ringwald             little_endian_store_16(response_buffer, offset, prev_handle);
10374ea43905SMatthias Ringwald             offset += 2u;
10386535961aSMatthias Ringwald             (void)memcpy(response_buffer + offset, group_start_value,
10394ea43905SMatthias Ringwald                          pair_len - 4u);
10404ea43905SMatthias Ringwald             offset += pair_len - 4u;
104129b68171SMilanka Ringwald             in_group = false;
1042591423b2SMatthias Ringwald 
1043591423b2SMatthias Ringwald             // check if space for another handle pair available
1044c1ab6cc1SMatthias Ringwald             if ((offset + pair_len) > response_buffer_size){
1045591423b2SMatthias Ringwald                 break;
1046591423b2SMatthias Ringwald             }
1047591423b2SMatthias Ringwald         }
1048591423b2SMatthias Ringwald 
1049591423b2SMatthias Ringwald         // keep track of previous handle
1050591423b2SMatthias Ringwald         prev_handle = it.handle;
1051591423b2SMatthias Ringwald 
1052591423b2SMatthias Ringwald         // does current attribute match
1053591423b2SMatthias Ringwald         // log_info("compare: %04x == %04x", *(uint16_t*) context->attribute_type, *(uint16_t*) uuid);
105438b893aaSMilanka Ringwald         if ((it.handle != 0u) && att_iterator_match_uuid(&it, attribute_type, attribute_type_len)) {
1055591423b2SMatthias Ringwald 
1056591423b2SMatthias Ringwald             // check if value has same len as last one
10574ea43905SMatthias Ringwald             uint16_t this_pair_len = 4u + it.value_len;
10584ea43905SMatthias Ringwald             if (offset > 1u){
1059591423b2SMatthias Ringwald                 if (this_pair_len != pair_len) {
1060591423b2SMatthias Ringwald                     break;
1061591423b2SMatthias Ringwald                 }
1062591423b2SMatthias Ringwald             }
1063591423b2SMatthias Ringwald 
1064591423b2SMatthias Ringwald             // log_info("Begin of group, handle 0x%04x", it.handle);
1065591423b2SMatthias Ringwald 
1066591423b2SMatthias Ringwald             // first
10674ea43905SMatthias Ringwald             if (offset == 1u) {
1068591423b2SMatthias Ringwald                 pair_len = this_pair_len;
106938460e25SMatthias Ringwald                 response_buffer[offset] = (uint8_t) this_pair_len;
1070591423b2SMatthias Ringwald                 offset++;
1071591423b2SMatthias Ringwald             }
1072591423b2SMatthias Ringwald 
1073591423b2SMatthias Ringwald             group_start_handle = it.handle;
1074591423b2SMatthias Ringwald             group_start_value  = it.value;
107529b68171SMilanka Ringwald             in_group = true;
1076591423b2SMatthias Ringwald         }
1077591423b2SMatthias Ringwald     }
1078591423b2SMatthias Ringwald 
10794ea43905SMatthias Ringwald     if (offset == 1u){
1080591423b2SMatthias Ringwald         return setup_error_atribute_not_found(response_buffer, request_type, start_handle);
1081591423b2SMatthias Ringwald     }
1082591423b2SMatthias Ringwald 
1083591423b2SMatthias Ringwald     response_buffer[0] = ATT_READ_BY_GROUP_TYPE_RESPONSE;
1084591423b2SMatthias Ringwald     return offset;
1085591423b2SMatthias Ringwald }
10869f7e3af1SMilanka Ringwald 
handle_read_by_group_type_request(att_connection_t * att_connection,uint8_t * request_buffer,uint16_t request_len,uint8_t * response_buffer,uint16_t response_buffer_size)1087591423b2SMatthias Ringwald static uint16_t handle_read_by_group_type_request(att_connection_t * att_connection, uint8_t * request_buffer,  uint16_t request_len,
1088591423b2SMatthias Ringwald                                            uint8_t * response_buffer, uint16_t response_buffer_size){
10899b49c4f4SMatthias Ringwald     uint16_t attribute_type_len;
10909b49c4f4SMatthias Ringwald     switch (request_len){
10919b49c4f4SMatthias Ringwald         case 7:
1092591423b2SMatthias Ringwald             attribute_type_len = 2;
10939b49c4f4SMatthias Ringwald             break;
10949b49c4f4SMatthias Ringwald         case 21:
1095591423b2SMatthias Ringwald             attribute_type_len = 16;
10969b49c4f4SMatthias Ringwald             break;
10979b49c4f4SMatthias Ringwald         default:
10989b49c4f4SMatthias Ringwald             return setup_error_invalid_pdu(response_buffer, ATT_READ_BY_GROUP_TYPE_REQUEST);
1099591423b2SMatthias Ringwald     }
11009b49c4f4SMatthias Ringwald 
11019b49c4f4SMatthias Ringwald     uint16_t start_handle = little_endian_read_16(request_buffer, 1);
11029b49c4f4SMatthias Ringwald     uint16_t end_handle = little_endian_read_16(request_buffer, 3);
11039b49c4f4SMatthias Ringwald     return handle_read_by_group_type_request2(att_connection, response_buffer, response_buffer_size, start_handle, end_handle, attribute_type_len, &request_buffer[5]);
1104591423b2SMatthias Ringwald }
1105591423b2SMatthias Ringwald 
1106591423b2SMatthias Ringwald //
1107591423b2SMatthias Ringwald // MARK: ATT_WRITE_REQUEST 0x12
handle_write_request(att_connection_t * att_connection,uint8_t * request_buffer,uint16_t request_len,uint8_t * response_buffer,uint16_t response_buffer_size)1108591423b2SMatthias Ringwald static uint16_t handle_write_request(att_connection_t * att_connection, uint8_t * request_buffer,  uint16_t request_len,
1109591423b2SMatthias Ringwald                               uint8_t * response_buffer, uint16_t response_buffer_size){
1110591423b2SMatthias Ringwald 
11117e23d0c0SMatthias Ringwald     UNUSED(response_buffer_size);
11127e23d0c0SMatthias Ringwald 
11139f7e3af1SMilanka Ringwald     if (request_len < 3u){
11149f7e3af1SMilanka Ringwald         return setup_error_invalid_pdu(response_buffer, ATT_WRITE_REQUEST);
11159f7e3af1SMilanka Ringwald     }
11169ec2630cSMatthias Ringwald 
1117591423b2SMatthias Ringwald     uint8_t request_type = ATT_WRITE_REQUEST;
1118591423b2SMatthias Ringwald 
1119591423b2SMatthias Ringwald     uint16_t handle = little_endian_read_16(request_buffer, 1);
1120591423b2SMatthias Ringwald     att_iterator_t it;
11215f5dcb67SMatthias Ringwald     bool ok = att_find_handle(&it, handle);
1122591423b2SMatthias Ringwald     if (!ok) {
1123591423b2SMatthias Ringwald         return setup_error_invalid_handle(response_buffer, request_type, handle);
1124591423b2SMatthias Ringwald     }
1125602e97cdSMilanka Ringwald     if (att_write_callback == NULL) {
1126591423b2SMatthias Ringwald         return setup_error_write_not_permitted(response_buffer, request_type, handle);
1127591423b2SMatthias Ringwald     }
1128edc4e664SMilanka Ringwald     if ((it.flags & (uint16_t)ATT_PROPERTY_WRITE) == 0u) {
1129591423b2SMatthias Ringwald         return setup_error_write_not_permitted(response_buffer, request_type, handle);
1130591423b2SMatthias Ringwald     }
1131edc4e664SMilanka Ringwald     if ((it.flags & (uint16_t)ATT_PROPERTY_DYNAMIC) == 0u) {
1132591423b2SMatthias Ringwald         return setup_error_write_not_permitted(response_buffer, request_type, handle);
1133591423b2SMatthias Ringwald     }
1134591423b2SMatthias Ringwald     // check security requirements
113556c3a347SMatthias Ringwald     int error_code = att_validate_security(att_connection, ATT_WRITE, &it);
11369305033eSMatthias Ringwald     if (error_code != 0) {
1137591423b2SMatthias Ringwald         return setup_error(response_buffer, request_type, handle, error_code);
1138591423b2SMatthias Ringwald     }
1139fd75a1c5SMatthias Ringwald     att_persistent_ccc_cache(&it);
11404ea43905SMatthias Ringwald     error_code = (*att_write_callback)(att_connection->con_handle, handle, ATT_TRANSACTION_MODE_NONE, 0u, request_buffer + 3u, request_len - 3u);
11414f8235e6SMatthias Ringwald 
11424f8235e6SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
11439f7e3af1SMilanka Ringwald     if (error_code == ATT_ERROR_WRITE_RESPONSE_PENDING){
11449f7e3af1SMilanka Ringwald         return ATT_INTERNAL_WRITE_RESPONSE_PENDING;
11459f7e3af1SMilanka Ringwald     }
11464f8235e6SMatthias Ringwald #endif
11474f8235e6SMatthias Ringwald 
114838b893aaSMilanka Ringwald     if (error_code != 0) {
1149591423b2SMatthias Ringwald         return setup_error(response_buffer, request_type, handle, error_code);
1150591423b2SMatthias Ringwald     }
115138b893aaSMilanka Ringwald     response_buffer[0] = (uint8_t)ATT_WRITE_RESPONSE;
1152591423b2SMatthias Ringwald     return 1;
1153591423b2SMatthias Ringwald }
1154591423b2SMatthias Ringwald 
1155591423b2SMatthias Ringwald //
1156591423b2SMatthias Ringwald // MARK: ATT_PREPARE_WRITE_REQUEST 0x16
handle_prepare_write_request(att_connection_t * att_connection,uint8_t * request_buffer,uint16_t request_len,uint8_t * response_buffer,uint16_t response_buffer_size)1157591423b2SMatthias Ringwald static uint16_t handle_prepare_write_request(att_connection_t * att_connection, uint8_t * request_buffer,  uint16_t request_len,
1158591423b2SMatthias Ringwald                                       uint8_t * response_buffer, uint16_t response_buffer_size){
1159591423b2SMatthias Ringwald 
1160591423b2SMatthias Ringwald     uint8_t request_type = ATT_PREPARE_WRITE_REQUEST;
1161591423b2SMatthias Ringwald 
11629f7e3af1SMilanka Ringwald     if (request_len < 5u){
11639f7e3af1SMilanka Ringwald         return setup_error_invalid_pdu(response_buffer, request_type);
11649f7e3af1SMilanka Ringwald     }
11659b49c4f4SMatthias Ringwald 
1166591423b2SMatthias Ringwald     uint16_t handle = little_endian_read_16(request_buffer, 1);
1167591423b2SMatthias Ringwald     uint16_t offset = little_endian_read_16(request_buffer, 3);
1168602e97cdSMilanka Ringwald     if (att_write_callback == NULL) {
1169591423b2SMatthias Ringwald         return setup_error_write_not_permitted(response_buffer, request_type, handle);
1170591423b2SMatthias Ringwald     }
1171591423b2SMatthias Ringwald     att_iterator_t it;
11725f5dcb67SMatthias Ringwald     if (att_find_handle(&it, handle) == false) {
1173591423b2SMatthias Ringwald         return setup_error_invalid_handle(response_buffer, request_type, handle);
1174591423b2SMatthias Ringwald     }
1175edc4e664SMilanka Ringwald     if ((it.flags & (uint16_t)ATT_PROPERTY_WRITE) == 0u) {
1176591423b2SMatthias Ringwald         return setup_error_write_not_permitted(response_buffer, request_type, handle);
1177591423b2SMatthias Ringwald     }
1178edc4e664SMilanka Ringwald     if ((it.flags & (uint16_t)ATT_PROPERTY_DYNAMIC) == 0u) {
1179591423b2SMatthias Ringwald         return setup_error_write_not_permitted(response_buffer, request_type, handle);
1180591423b2SMatthias Ringwald     }
1181591423b2SMatthias Ringwald     // check security requirements
118256c3a347SMatthias Ringwald     int error_code = att_validate_security(att_connection, ATT_WRITE, &it);
118338b893aaSMilanka Ringwald     if (error_code != 0) {
1184591423b2SMatthias Ringwald         return setup_error(response_buffer, request_type, handle, error_code);
1185591423b2SMatthias Ringwald     }
1186591423b2SMatthias Ringwald 
11874ea43905SMatthias Ringwald     error_code = (*att_write_callback)(att_connection->con_handle, handle, ATT_TRANSACTION_MODE_ACTIVE, offset, request_buffer + 5u, request_len - 5u);
1188591423b2SMatthias Ringwald     switch (error_code){
1189591423b2SMatthias Ringwald         case 0:
1190591423b2SMatthias Ringwald             break;
1191591423b2SMatthias Ringwald         case ATT_ERROR_INVALID_OFFSET:
1192591423b2SMatthias Ringwald         case ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LENGTH:
1193591423b2SMatthias Ringwald             // postpone to execute write request
1194591423b2SMatthias Ringwald             att_prepare_write_update_errors(error_code, handle);
1195591423b2SMatthias Ringwald             break;
119656c3a347SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
119756c3a347SMatthias Ringwald         case ATT_ERROR_WRITE_RESPONSE_PENDING:
119856c3a347SMatthias Ringwald             return ATT_INTERNAL_WRITE_RESPONSE_PENDING;
119956c3a347SMatthias Ringwald #endif
1200591423b2SMatthias Ringwald         default:
1201591423b2SMatthias Ringwald             return setup_error(response_buffer, request_type, handle, error_code);
1202591423b2SMatthias Ringwald     }
1203591423b2SMatthias Ringwald 
1204591423b2SMatthias Ringwald     // response: echo request
12057e23d0c0SMatthias Ringwald     uint16_t bytes_to_echo = btstack_min(request_len, response_buffer_size);
12067e23d0c0SMatthias Ringwald     (void)memcpy(response_buffer, request_buffer, bytes_to_echo);
1207591423b2SMatthias Ringwald     response_buffer[0] = ATT_PREPARE_WRITE_RESPONSE;
1208591423b2SMatthias Ringwald     return request_len;
1209591423b2SMatthias Ringwald }
1210591423b2SMatthias Ringwald 
1211591423b2SMatthias Ringwald /*
1212591423b2SMatthias Ringwald  * @brief transcation queue of prepared writes, e.g., after disconnect
1213591423b2SMatthias Ringwald  */
att_clear_transaction_queue(att_connection_t * att_connection)1214591423b2SMatthias Ringwald void att_clear_transaction_queue(att_connection_t * att_connection){
12153d71c7a4SMatthias Ringwald     (*att_write_callback)(att_connection->con_handle, 0, ATT_TRANSACTION_MODE_CANCEL, 0, NULL, 0);
1216591423b2SMatthias Ringwald }
1217591423b2SMatthias Ringwald 
1218591423b2SMatthias Ringwald // MARK: ATT_EXECUTE_WRITE_REQUEST 0x18
1219591423b2SMatthias Ringwald // NOTE: security has been verified by handle_prepare_write_request
handle_execute_write_request(att_connection_t * att_connection,uint8_t * request_buffer,uint16_t request_len,uint8_t * response_buffer,uint16_t response_buffer_size)1220591423b2SMatthias Ringwald static uint16_t handle_execute_write_request(att_connection_t * att_connection, uint8_t * request_buffer,  uint16_t request_len,
1221591423b2SMatthias Ringwald                                       uint8_t * response_buffer, uint16_t response_buffer_size){
1222591423b2SMatthias Ringwald 
12239ec2630cSMatthias Ringwald     UNUSED(response_buffer_size);
12249ec2630cSMatthias Ringwald 
1225591423b2SMatthias Ringwald     uint8_t request_type = ATT_EXECUTE_WRITE_REQUEST;
12269b49c4f4SMatthias Ringwald 
12279f7e3af1SMilanka Ringwald     if (request_len < 2u){
12289f7e3af1SMilanka Ringwald         return setup_error_invalid_pdu(response_buffer, request_type);
12299f7e3af1SMilanka Ringwald     }
12309b49c4f4SMatthias Ringwald 
123142dfcacaSMatthias Ringwald     if (att_write_callback == NULL) {
123242dfcacaSMatthias Ringwald         return setup_error_write_not_permitted(response_buffer, request_type, 0);
123342dfcacaSMatthias Ringwald     }
123442dfcacaSMatthias Ringwald 
1235*c6d8b816SMatthias Ringwald     if (request_buffer[1] != 0u) {
12369b31df63SMatthias Ringwald         // validate queued write
12379b31df63SMatthias Ringwald         if (att_prepare_write_error_code == 0){
12383d71c7a4SMatthias Ringwald             att_prepare_write_error_code = (*att_write_callback)(att_connection->con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
12399b31df63SMatthias Ringwald         }
124056c3a347SMatthias Ringwald #ifdef ENABLE_ATT_DELAYED_RESPONSE
12419f7e3af1SMilanka Ringwald         if (att_prepare_write_error_code == ATT_ERROR_WRITE_RESPONSE_PENDING){
12429f7e3af1SMilanka Ringwald             return ATT_INTERNAL_WRITE_RESPONSE_PENDING;
12439f7e3af1SMilanka Ringwald         }
124456c3a347SMatthias Ringwald #endif
1245591423b2SMatthias Ringwald         // deliver queued errors
12469305033eSMatthias Ringwald         if (att_prepare_write_error_code != 0){
1247591423b2SMatthias Ringwald             att_clear_transaction_queue(att_connection);
1248591423b2SMatthias Ringwald             uint8_t  error_code = att_prepare_write_error_code;
1249591423b2SMatthias Ringwald             uint16_t handle     = att_prepare_write_error_handle;
1250591423b2SMatthias Ringwald             att_prepare_write_reset();
1251591423b2SMatthias Ringwald             return setup_error(response_buffer, request_type, handle, error_code);
1252591423b2SMatthias Ringwald         }
12533d71c7a4SMatthias Ringwald         att_write_callback(att_connection->con_handle, 0, ATT_TRANSACTION_MODE_EXECUTE, 0, NULL, 0);
1254591423b2SMatthias Ringwald     } else {
1255591423b2SMatthias Ringwald         att_clear_transaction_queue(att_connection);
1256591423b2SMatthias Ringwald     }
1257591423b2SMatthias Ringwald     response_buffer[0] = ATT_EXECUTE_WRITE_RESPONSE;
1258591423b2SMatthias Ringwald     return 1;
1259591423b2SMatthias Ringwald }
1260591423b2SMatthias Ringwald 
1261591423b2SMatthias Ringwald // MARK: ATT_WRITE_COMMAND 0x52
1262591423b2SMatthias Ringwald // Core 4.0, vol 3, part F, 3.4.5.3
1263591423b2SMatthias Ringwald // "No Error Response or Write Response shall be sent in response to this command"
handle_write_command(att_connection_t * att_connection,uint8_t * request_buffer,uint16_t request_len,uint16_t required_flags)126415406929SMatthias Ringwald static void handle_write_command(att_connection_t * att_connection, uint8_t * request_buffer,  uint16_t request_len, uint16_t required_flags){
12659ec2630cSMatthias Ringwald 
12669f7e3af1SMilanka Ringwald     if (request_len < 3u){
12679f7e3af1SMilanka Ringwald         return;
12689f7e3af1SMilanka Ringwald     }
12699b49c4f4SMatthias Ringwald 
1270591423b2SMatthias Ringwald     uint16_t handle = little_endian_read_16(request_buffer, 1);
12719f7e3af1SMilanka Ringwald     if (att_write_callback == NULL){
12729f7e3af1SMilanka Ringwald         return;
12739f7e3af1SMilanka Ringwald     }
12748ac574d6SMatthias Ringwald 
1275591423b2SMatthias Ringwald     att_iterator_t it;
12765f5dcb67SMatthias Ringwald     bool ok = att_find_handle(&it, handle);
12779f7e3af1SMilanka Ringwald     if (!ok){
12789f7e3af1SMilanka Ringwald         return;
12799f7e3af1SMilanka Ringwald     }
12809f7e3af1SMilanka Ringwald     if ((it.flags & (uint16_t)ATT_PROPERTY_DYNAMIC) == 0u){
12819f7e3af1SMilanka Ringwald         return;
12829f7e3af1SMilanka Ringwald     }
12839f7e3af1SMilanka Ringwald     if ((it.flags & required_flags) == 0u){
12849f7e3af1SMilanka Ringwald         return;
12859f7e3af1SMilanka Ringwald     }
1286f688bdb8SMatthias Ringwald     if (att_validate_security(att_connection, ATT_WRITE, &it) != ATT_ERROR_SUCCESS){
12879f7e3af1SMilanka Ringwald         return;
12889f7e3af1SMilanka Ringwald     }
1289fd75a1c5SMatthias Ringwald     att_persistent_ccc_cache(&it);
12904ea43905SMatthias Ringwald     (*att_write_callback)(att_connection->con_handle, handle, ATT_TRANSACTION_MODE_NONE, 0u, request_buffer + 3u, request_len - 3u);
1291591423b2SMatthias Ringwald }
1292591423b2SMatthias Ringwald 
1293591423b2SMatthias Ringwald // MARK: helper for ATT_HANDLE_VALUE_NOTIFICATION and ATT_HANDLE_VALUE_INDICATION
prepare_handle_value(att_connection_t * att_connection,uint16_t handle,const uint8_t * value,uint16_t value_len,uint8_t * response_buffer)1294591423b2SMatthias Ringwald static uint16_t prepare_handle_value(att_connection_t * att_connection,
1295591423b2SMatthias Ringwald                                      uint16_t handle,
12963bb3035fSMilanka Ringwald                                      const uint8_t *value,
1297591423b2SMatthias Ringwald                                      uint16_t value_len,
1298591423b2SMatthias Ringwald                                      uint8_t * response_buffer){
1299591423b2SMatthias Ringwald     little_endian_store_16(response_buffer, 1, handle);
130041f9be70SMatthias Ringwald     uint16_t bytes_to_copy = btstack_min(value_len, att_connection->mtu - 3u);
130141f9be70SMatthias Ringwald     (void)memcpy(&response_buffer[3], value, bytes_to_copy);
13024ea43905SMatthias Ringwald     return value_len + 3u;
1303591423b2SMatthias Ringwald }
1304591423b2SMatthias Ringwald 
1305591423b2SMatthias Ringwald // MARK: ATT_HANDLE_VALUE_NOTIFICATION 0x1b
att_prepare_handle_value_notification(att_connection_t * att_connection,uint16_t attribute_handle,const uint8_t * value,uint16_t value_len,uint8_t * response_buffer)1306591423b2SMatthias Ringwald uint16_t att_prepare_handle_value_notification(att_connection_t * att_connection,
1307b45b7749SMilanka Ringwald                                                uint16_t attribute_handle,
13083bb3035fSMilanka Ringwald                                                const uint8_t *value,
1309591423b2SMatthias Ringwald                                                uint16_t value_len,
1310591423b2SMatthias Ringwald                                                uint8_t * response_buffer){
1311591423b2SMatthias Ringwald 
1312591423b2SMatthias Ringwald     response_buffer[0] = ATT_HANDLE_VALUE_NOTIFICATION;
1313b45b7749SMilanka Ringwald     return prepare_handle_value(att_connection, attribute_handle, value, value_len, response_buffer);
1314591423b2SMatthias Ringwald }
1315591423b2SMatthias Ringwald 
13162cbb1bf1SMatthias Ringwald // MARK: ATT_MULTIPLE_HANDLE_VALUE_NTF 0x23u
att_prepare_handle_value_multiple_notification(att_connection_t * att_connection,uint8_t num_attributes,const uint16_t * attribute_handles,const uint8_t ** values_data,const uint16_t * values_len,uint8_t * response_buffer)13172cbb1bf1SMatthias Ringwald uint16_t att_prepare_handle_value_multiple_notification(att_connection_t * att_connection,
13182cbb1bf1SMatthias Ringwald                                                uint8_t num_attributes,
13192cbb1bf1SMatthias Ringwald                                                const uint16_t * attribute_handles,
13202cbb1bf1SMatthias Ringwald                                                const uint8_t ** values_data,
13212cbb1bf1SMatthias Ringwald                                                const uint16_t * values_len,
13222cbb1bf1SMatthias Ringwald                                                uint8_t * response_buffer){
13232cbb1bf1SMatthias Ringwald 
13242cbb1bf1SMatthias Ringwald     response_buffer[0] = ATT_MULTIPLE_HANDLE_VALUE_NTF;
13252cbb1bf1SMatthias Ringwald     uint8_t i;
13262cbb1bf1SMatthias Ringwald     uint16_t offset = 1;
13272cbb1bf1SMatthias Ringwald     uint16_t response_buffer_size = att_connection->mtu - 3u;
13282cbb1bf1SMatthias Ringwald     for (i = 0; i < num_attributes; i++) {
13292cbb1bf1SMatthias Ringwald         uint16_t value_len = values_len[i];
1330*c6d8b816SMatthias Ringwald         if ((offset + 4u + value_len) > response_buffer_size){
13312cbb1bf1SMatthias Ringwald             break;
13322cbb1bf1SMatthias Ringwald         }
13332cbb1bf1SMatthias Ringwald         little_endian_store_16(response_buffer, offset, attribute_handles[i]);
1334*c6d8b816SMatthias Ringwald         offset += 2u;
13352cbb1bf1SMatthias Ringwald         little_endian_store_16(response_buffer, offset, value_len);
1336*c6d8b816SMatthias Ringwald         offset += 2u;
13372cbb1bf1SMatthias Ringwald         (void) memcpy(&response_buffer[offset], values_data[i], value_len);
13382cbb1bf1SMatthias Ringwald         offset += value_len;
13392cbb1bf1SMatthias Ringwald     }
13402cbb1bf1SMatthias Ringwald     return offset;
13412cbb1bf1SMatthias Ringwald }
13422cbb1bf1SMatthias Ringwald 
1343591423b2SMatthias Ringwald // MARK: ATT_HANDLE_VALUE_INDICATION 0x1d
att_prepare_handle_value_indication(att_connection_t * att_connection,uint16_t attribute_handle,const uint8_t * value,uint16_t value_len,uint8_t * response_buffer)1344591423b2SMatthias Ringwald uint16_t att_prepare_handle_value_indication(att_connection_t * att_connection,
1345b45b7749SMilanka Ringwald                                              uint16_t attribute_handle,
13463bb3035fSMilanka Ringwald                                              const uint8_t *value,
1347591423b2SMatthias Ringwald                                              uint16_t value_len,
1348591423b2SMatthias Ringwald                                              uint8_t * response_buffer){
1349591423b2SMatthias Ringwald 
1350591423b2SMatthias Ringwald     response_buffer[0] = ATT_HANDLE_VALUE_INDICATION;
1351b45b7749SMilanka Ringwald     return prepare_handle_value(att_connection, attribute_handle, value, value_len, response_buffer);
1352591423b2SMatthias Ringwald }
1353591423b2SMatthias Ringwald 
1354591423b2SMatthias Ringwald // MARK: Dispatcher
att_handle_request(att_connection_t * att_connection,uint8_t * request_buffer,uint16_t request_len,uint8_t * response_buffer)1355591423b2SMatthias Ringwald uint16_t att_handle_request(att_connection_t * att_connection,
1356591423b2SMatthias Ringwald                             uint8_t * request_buffer,
1357591423b2SMatthias Ringwald                             uint16_t request_len,
1358591423b2SMatthias Ringwald                             uint8_t * response_buffer){
1359591423b2SMatthias Ringwald     uint16_t response_len = 0;
1360838bd521SMatthias Ringwald     const uint16_t response_buffer_size = att_connection->mtu;
1361838bd521SMatthias Ringwald     const uint8_t  request_opcode = request_buffer[0];
1362591423b2SMatthias Ringwald 
1363838bd521SMatthias Ringwald     switch (request_opcode){
1364591423b2SMatthias Ringwald         case ATT_EXCHANGE_MTU_REQUEST:
1365591423b2SMatthias Ringwald             response_len = handle_exchange_mtu_request(att_connection, request_buffer, request_len, response_buffer);
1366591423b2SMatthias Ringwald             break;
1367591423b2SMatthias Ringwald         case ATT_FIND_INFORMATION_REQUEST:
1368591423b2SMatthias Ringwald             response_len = handle_find_information_request(att_connection, request_buffer, request_len,response_buffer, response_buffer_size);
1369591423b2SMatthias Ringwald             break;
1370591423b2SMatthias Ringwald         case ATT_FIND_BY_TYPE_VALUE_REQUEST:
1371591423b2SMatthias Ringwald             response_len = handle_find_by_type_value_request(att_connection, request_buffer, request_len, response_buffer, response_buffer_size);
1372591423b2SMatthias Ringwald             break;
1373591423b2SMatthias Ringwald         case ATT_READ_BY_TYPE_REQUEST:
1374591423b2SMatthias Ringwald             response_len = handle_read_by_type_request(att_connection, request_buffer, request_len, response_buffer, response_buffer_size);
1375591423b2SMatthias Ringwald             break;
1376591423b2SMatthias Ringwald         case ATT_READ_REQUEST:
1377591423b2SMatthias Ringwald             response_len = handle_read_request(att_connection, request_buffer, request_len, response_buffer, response_buffer_size);
1378591423b2SMatthias Ringwald             break;
1379591423b2SMatthias Ringwald         case ATT_READ_BLOB_REQUEST:
1380591423b2SMatthias Ringwald             response_len = handle_read_blob_request(att_connection, request_buffer, request_len, response_buffer, response_buffer_size);
1381591423b2SMatthias Ringwald             break;
1382591423b2SMatthias Ringwald         case ATT_READ_MULTIPLE_REQUEST:
13832c4384ebSMatthias Ringwald             response_len = handle_read_multiple_request(att_connection, request_buffer, request_len, response_buffer,
13842c4384ebSMatthias Ringwald                                                         response_buffer_size, false);
13852c4384ebSMatthias Ringwald             break;
13862c4384ebSMatthias Ringwald         case ATT_READ_MULTIPLE_VARIABLE_REQ:
13872c4384ebSMatthias Ringwald             response_len = handle_read_multiple_request(att_connection, request_buffer, request_len, response_buffer,
13882c4384ebSMatthias Ringwald                                                         response_buffer_size, true);
1389591423b2SMatthias Ringwald             break;
1390591423b2SMatthias Ringwald         case ATT_READ_BY_GROUP_TYPE_REQUEST:
1391591423b2SMatthias Ringwald             response_len = handle_read_by_group_type_request(att_connection, request_buffer, request_len, response_buffer, response_buffer_size);
1392591423b2SMatthias Ringwald             break;
1393591423b2SMatthias Ringwald         case ATT_WRITE_REQUEST:
1394591423b2SMatthias Ringwald             response_len = handle_write_request(att_connection, request_buffer, request_len, response_buffer, response_buffer_size);
1395591423b2SMatthias Ringwald             break;
1396591423b2SMatthias Ringwald         case ATT_PREPARE_WRITE_REQUEST:
1397591423b2SMatthias Ringwald             response_len = handle_prepare_write_request(att_connection, request_buffer, request_len, response_buffer, response_buffer_size);
1398591423b2SMatthias Ringwald             break;
1399591423b2SMatthias Ringwald         case ATT_EXECUTE_WRITE_REQUEST:
1400591423b2SMatthias Ringwald             response_len = handle_execute_write_request(att_connection, request_buffer, request_len, response_buffer, response_buffer_size);
1401591423b2SMatthias Ringwald             break;
1402591423b2SMatthias Ringwald         case ATT_WRITE_COMMAND:
140315406929SMatthias Ringwald             handle_write_command(att_connection, request_buffer, request_len, ATT_PROPERTY_WRITE_WITHOUT_RESPONSE);
1404591423b2SMatthias Ringwald             break;
14057a766ebfSMatthias Ringwald #ifdef ENABLE_LE_SIGNED_WRITE
1406591423b2SMatthias Ringwald         case ATT_SIGNED_WRITE_COMMAND:
140715406929SMatthias Ringwald             handle_write_command(att_connection, request_buffer, request_len, ATT_PROPERTY_AUTHENTICATED_SIGNED_WRITE);
1408591423b2SMatthias Ringwald             break;
14097a766ebfSMatthias Ringwald #endif
1410591423b2SMatthias Ringwald         default:
1411838bd521SMatthias Ringwald             response_len = setup_error(response_buffer, request_opcode, 0, ATT_ERROR_REQUEST_NOT_SUPPORTED);
1412591423b2SMatthias Ringwald             break;
1413591423b2SMatthias Ringwald     }
1414591423b2SMatthias Ringwald     return response_len;
1415591423b2SMatthias Ringwald }
1416591423b2SMatthias Ringwald 
1417660ce368SMatthias Ringwald // returns 1 if service found. only primary service.
gatt_server_get_handle_range_for_service_with_uuid16(uint16_t uuid16,uint16_t * start_handle,uint16_t * end_handle)1418c436b760SMilanka Ringwald bool gatt_server_get_handle_range_for_service_with_uuid16(uint16_t uuid16, uint16_t * start_handle, uint16_t * end_handle){
141929b68171SMilanka Ringwald     bool in_group    = false;
1420660ce368SMatthias Ringwald     uint16_t prev_handle = 0;
14219d506991SMatthias Ringwald     uint16_t service_start = 0;
1422660ce368SMatthias Ringwald 
1423660ce368SMatthias Ringwald     uint8_t attribute_value[2];
1424*c6d8b816SMatthias Ringwald     const uint16_t attribute_len = sizeof(attribute_value);
1425660ce368SMatthias Ringwald     little_endian_store_16(attribute_value, 0, uuid16);
1426660ce368SMatthias Ringwald 
1427660ce368SMatthias Ringwald     att_iterator_t it;
1428660ce368SMatthias Ringwald     att_iterator_init(&it);
1429660ce368SMatthias Ringwald     while (att_iterator_has_next(&it)){
1430660ce368SMatthias Ringwald         att_iterator_fetch_next(&it);
1431660ce368SMatthias Ringwald         int new_service_started = att_iterator_match_uuid16(&it, GATT_PRIMARY_SERVICE_UUID) || att_iterator_match_uuid16(&it, GATT_SECONDARY_SERVICE_UUID);
1432660ce368SMatthias Ringwald 
1433660ce368SMatthias Ringwald         // close current tag, if within a group and a new service definition starts or we reach end of att db
1434660ce368SMatthias Ringwald         if (in_group &&
14354ea43905SMatthias Ringwald             ((it.handle == 0u) || new_service_started)){
14366607f8f0SMilanka Ringwald             in_group = false;
14376607f8f0SMilanka Ringwald             // check range
14386607f8f0SMilanka Ringwald             if ((service_start >= *start_handle) && (prev_handle <= *end_handle)){
14396607f8f0SMilanka Ringwald                 *start_handle = service_start;
1440660ce368SMatthias Ringwald                 *end_handle = prev_handle;
14414d5915b3SMatthias Ringwald                 return true;
1442660ce368SMatthias Ringwald             }
14436607f8f0SMilanka Ringwald         }
1444660ce368SMatthias Ringwald 
1445660ce368SMatthias Ringwald         // keep track of previous handle
1446660ce368SMatthias Ringwald         prev_handle = it.handle;
1447660ce368SMatthias Ringwald 
1448660ce368SMatthias Ringwald         // check if found
144938b893aaSMilanka Ringwald         if ( (it.handle != 0u) && new_service_started && (attribute_len == it.value_len) && (memcmp(attribute_value, it.value, it.value_len) == 0)){
14506607f8f0SMilanka Ringwald             service_start = it.handle;
14514d5915b3SMatthias Ringwald             in_group = true;
1452660ce368SMatthias Ringwald         }
1453660ce368SMatthias Ringwald     }
14544d5915b3SMatthias Ringwald     return false;
1455660ce368SMatthias Ringwald }
1456660ce368SMatthias Ringwald 
14574d5915b3SMatthias Ringwald // returns false if not found
gatt_server_get_value_handle_for_characteristic_with_uuid16(uint16_t start_handle,uint16_t end_handle,uint16_t uuid16)1458660ce368SMatthias Ringwald uint16_t gatt_server_get_value_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t uuid16){
1459660ce368SMatthias Ringwald     att_iterator_t it;
1460660ce368SMatthias Ringwald     att_iterator_init(&it);
1461660ce368SMatthias Ringwald     while (att_iterator_has_next(&it)){
1462660ce368SMatthias Ringwald         att_iterator_fetch_next(&it);
14639f7e3af1SMilanka Ringwald         if ((it.handle != 0u) && (it.handle < start_handle)){
14649f7e3af1SMilanka Ringwald             continue;
14659f7e3af1SMilanka Ringwald         }
14669f7e3af1SMilanka Ringwald         if (it.handle > end_handle){
14679f7e3af1SMilanka Ringwald             break;  // (1)
14689f7e3af1SMilanka Ringwald         }
14699f7e3af1SMilanka Ringwald         if (it.handle == 0u){
14709f7e3af1SMilanka Ringwald             break;
14719f7e3af1SMilanka Ringwald         }
14729f7e3af1SMilanka Ringwald         if (att_iterator_match_uuid16(&it, uuid16)){
14739f7e3af1SMilanka Ringwald             return it.handle;
14749f7e3af1SMilanka Ringwald         }
1475660ce368SMatthias Ringwald     }
1476660ce368SMatthias Ringwald     return 0;
1477660ce368SMatthias Ringwald }
1478660ce368SMatthias Ringwald 
gatt_server_get_descriptor_handle_for_characteristic_with_uuid16(uint16_t start_handle,uint16_t end_handle,uint16_t characteristic_uuid16,uint16_t descriptor_uuid16)1479c7774db7SMilanka Ringwald uint16_t gatt_server_get_descriptor_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t characteristic_uuid16, uint16_t descriptor_uuid16){
1480660ce368SMatthias Ringwald     att_iterator_t it;
1481660ce368SMatthias Ringwald     att_iterator_init(&it);
14821979f09cSMatthias Ringwald     bool characteristic_found = false;
1483660ce368SMatthias Ringwald     while (att_iterator_has_next(&it)){
1484660ce368SMatthias Ringwald         att_iterator_fetch_next(&it);
14859f7e3af1SMilanka Ringwald         if ((it.handle != 0u) && (it.handle < start_handle)){
14869f7e3af1SMilanka Ringwald             continue;
14879f7e3af1SMilanka Ringwald         }
14889f7e3af1SMilanka Ringwald         if (it.handle > end_handle){
14899f7e3af1SMilanka Ringwald             break;  // (1)
14909f7e3af1SMilanka Ringwald         }
14919f7e3af1SMilanka Ringwald         if (it.handle == 0u){
14929f7e3af1SMilanka Ringwald             break;
14939f7e3af1SMilanka Ringwald         }
1494c7774db7SMilanka Ringwald         if (att_iterator_match_uuid16(&it, characteristic_uuid16)){
14951979f09cSMatthias Ringwald             characteristic_found = true;
1496660ce368SMatthias Ringwald             continue;
1497660ce368SMatthias Ringwald         }
1498660ce368SMatthias Ringwald         if (att_iterator_match_uuid16(&it, GATT_PRIMARY_SERVICE_UUID)
1499660ce368SMatthias Ringwald          || att_iterator_match_uuid16(&it, GATT_SECONDARY_SERVICE_UUID)
1500660ce368SMatthias Ringwald          || att_iterator_match_uuid16(&it, GATT_CHARACTERISTICS_UUID)){
15019f7e3af1SMilanka Ringwald             if (characteristic_found){
15029f7e3af1SMilanka Ringwald                 break;
15039f7e3af1SMilanka Ringwald             }
1504660ce368SMatthias Ringwald             continue;
1505660ce368SMatthias Ringwald         }
1506c7774db7SMilanka Ringwald         if (characteristic_found && att_iterator_match_uuid16(&it, descriptor_uuid16)){
1507660ce368SMatthias Ringwald             return it.handle;
1508660ce368SMatthias Ringwald         }
1509660ce368SMatthias Ringwald     }
1510660ce368SMatthias Ringwald     return 0;
1511660ce368SMatthias Ringwald }
1512660ce368SMatthias Ringwald 
1513c7774db7SMilanka Ringwald // returns 0 if not found
gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(uint16_t start_handle,uint16_t end_handle,uint16_t characteristic_uuid16)1514c7774db7SMilanka Ringwald uint16_t gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t characteristic_uuid16){
1515c7774db7SMilanka Ringwald     return gatt_server_get_descriptor_handle_for_characteristic_with_uuid16(start_handle, end_handle, characteristic_uuid16, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION);
1516c7774db7SMilanka Ringwald }
1517c7774db7SMilanka Ringwald // returns 0 if not found
1518c7774db7SMilanka Ringwald 
gatt_server_get_server_configuration_handle_for_characteristic_with_uuid16(uint16_t start_handle,uint16_t end_handle,uint16_t characteristic_uuid16)1519c7774db7SMilanka Ringwald uint16_t gatt_server_get_server_configuration_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t characteristic_uuid16){
1520c7774db7SMilanka Ringwald     return gatt_server_get_descriptor_handle_for_characteristic_with_uuid16(start_handle, end_handle, characteristic_uuid16, GATT_SERVER_CHARACTERISTICS_CONFIGURATION);
1521c7774db7SMilanka Ringwald }
1522c7774db7SMilanka Ringwald 
1523f76262a5SMilanka Ringwald // returns true if service found. only primary service.
gatt_server_get_handle_range_for_service_with_uuid128(const uint8_t * uuid128,uint16_t * start_handle,uint16_t * end_handle)1524f76262a5SMilanka Ringwald bool gatt_server_get_handle_range_for_service_with_uuid128(const uint8_t * uuid128, uint16_t * start_handle, uint16_t * end_handle){
1525f76262a5SMilanka Ringwald     bool in_group    = false;
152615f22304SMatthias Ringwald     uint16_t prev_handle = 0;
152715f22304SMatthias Ringwald 
152815f22304SMatthias Ringwald     uint8_t attribute_value[16];
152938b893aaSMilanka Ringwald     uint16_t attribute_len = (uint16_t)sizeof(attribute_value);
153015f22304SMatthias Ringwald     reverse_128(uuid128, attribute_value);
153115f22304SMatthias Ringwald 
153215f22304SMatthias Ringwald     att_iterator_t it;
153315f22304SMatthias Ringwald     att_iterator_init(&it);
153415f22304SMatthias Ringwald     while (att_iterator_has_next(&it)){
153515f22304SMatthias Ringwald         att_iterator_fetch_next(&it);
153615f22304SMatthias Ringwald         int new_service_started = att_iterator_match_uuid16(&it, GATT_PRIMARY_SERVICE_UUID) || att_iterator_match_uuid16(&it, GATT_SECONDARY_SERVICE_UUID);
153715f22304SMatthias Ringwald 
153815f22304SMatthias Ringwald         // close current tag, if within a group and a new service definition starts or we reach end of att db
153915f22304SMatthias Ringwald         if (in_group &&
15404ea43905SMatthias Ringwald             ((it.handle == 0u) || new_service_started)){
154115f22304SMatthias Ringwald             *end_handle = prev_handle;
1542f76262a5SMilanka Ringwald             return true;
154315f22304SMatthias Ringwald         }
154415f22304SMatthias Ringwald 
154515f22304SMatthias Ringwald         // keep track of previous handle
154615f22304SMatthias Ringwald         prev_handle = it.handle;
154715f22304SMatthias Ringwald 
154815f22304SMatthias Ringwald         // check if found
1549edc4e664SMilanka Ringwald         if ( (it.handle != 0u) && new_service_started && (attribute_len == it.value_len) && (memcmp(attribute_value, it.value, it.value_len) == 0)){
155015f22304SMatthias Ringwald             *start_handle = it.handle;
1551f76262a5SMilanka Ringwald             in_group = true;
155215f22304SMatthias Ringwald         }
155315f22304SMatthias Ringwald     }
1554f76262a5SMilanka Ringwald     return false;
155515f22304SMatthias Ringwald }
155615f22304SMatthias Ringwald 
155715f22304SMatthias Ringwald // returns 0 if not found
gatt_server_get_value_handle_for_characteristic_with_uuid128(uint16_t start_handle,uint16_t end_handle,const uint8_t * uuid128)155815f22304SMatthias Ringwald uint16_t gatt_server_get_value_handle_for_characteristic_with_uuid128(uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128){
155915f22304SMatthias Ringwald     uint8_t attribute_value[16];
156015f22304SMatthias Ringwald     reverse_128(uuid128, attribute_value);
156115f22304SMatthias Ringwald     att_iterator_t it;
156215f22304SMatthias Ringwald     att_iterator_init(&it);
156315f22304SMatthias Ringwald     while (att_iterator_has_next(&it)){
156415f22304SMatthias Ringwald         att_iterator_fetch_next(&it);
15659f7e3af1SMilanka Ringwald         if ((it.handle != 0u) && (it.handle < start_handle)){
15669f7e3af1SMilanka Ringwald             continue;
15679f7e3af1SMilanka Ringwald         }
15689f7e3af1SMilanka Ringwald         if (it.handle > end_handle){
15699f7e3af1SMilanka Ringwald             break;  // (1)
15709f7e3af1SMilanka Ringwald         }
15719f7e3af1SMilanka Ringwald         if (it.handle == 0u){
15729f7e3af1SMilanka Ringwald             break;
15739f7e3af1SMilanka Ringwald         }
15749f7e3af1SMilanka Ringwald         if (att_iterator_match_uuid(&it, attribute_value, 16)){
15759f7e3af1SMilanka Ringwald             return it.handle;
15769f7e3af1SMilanka Ringwald         }
157715f22304SMatthias Ringwald     }
157815f22304SMatthias Ringwald     return 0;
157915f22304SMatthias Ringwald }
158015f22304SMatthias Ringwald 
158115f22304SMatthias Ringwald // returns 0 if not found
gatt_server_get_client_configuration_handle_for_characteristic_with_uuid128(uint16_t start_handle,uint16_t end_handle,const uint8_t * uuid128)158215f22304SMatthias Ringwald uint16_t gatt_server_get_client_configuration_handle_for_characteristic_with_uuid128(uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128){
158315f22304SMatthias Ringwald     uint8_t attribute_value[16];
158415f22304SMatthias Ringwald     reverse_128(uuid128, attribute_value);
158515f22304SMatthias Ringwald     att_iterator_t it;
158615f22304SMatthias Ringwald     att_iterator_init(&it);
15875f5dcb67SMatthias Ringwald     bool characteristic_found = false;
158815f22304SMatthias Ringwald     while (att_iterator_has_next(&it)){
158915f22304SMatthias Ringwald         att_iterator_fetch_next(&it);
15909f7e3af1SMilanka Ringwald         if ((it.handle != 0u) && (it.handle < start_handle)){
15919f7e3af1SMilanka Ringwald             continue;
15929f7e3af1SMilanka Ringwald         }
15939f7e3af1SMilanka Ringwald         if (it.handle > end_handle){
15949f7e3af1SMilanka Ringwald             break;  // (1)
15959f7e3af1SMilanka Ringwald         }
15969f7e3af1SMilanka Ringwald         if (it.handle == 0u){
15979f7e3af1SMilanka Ringwald             break;
15989f7e3af1SMilanka Ringwald         }
159915f22304SMatthias Ringwald         if (att_iterator_match_uuid(&it, attribute_value, 16)){
16005f5dcb67SMatthias Ringwald             characteristic_found = true;
160115f22304SMatthias Ringwald             continue;
160215f22304SMatthias Ringwald         }
160315f22304SMatthias Ringwald         if (att_iterator_match_uuid16(&it, GATT_PRIMARY_SERVICE_UUID)
160415f22304SMatthias Ringwald          || att_iterator_match_uuid16(&it, GATT_SECONDARY_SERVICE_UUID)
160515f22304SMatthias Ringwald          || att_iterator_match_uuid16(&it, GATT_CHARACTERISTICS_UUID)){
16069f7e3af1SMilanka Ringwald             if (characteristic_found){
16079f7e3af1SMilanka Ringwald                 break;
16089f7e3af1SMilanka Ringwald             }
160915f22304SMatthias Ringwald             continue;
161015f22304SMatthias Ringwald         }
1611b58c6eb9SMilanka Ringwald         if (characteristic_found && att_iterator_match_uuid16(&it, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION)){
161215f22304SMatthias Ringwald             return it.handle;
161315f22304SMatthias Ringwald         }
161415f22304SMatthias Ringwald     }
161515f22304SMatthias Ringwald     return 0;
161615f22304SMatthias Ringwald }
161715f22304SMatthias Ringwald 
161815f22304SMatthias Ringwald 
gatt_server_get_included_service_with_uuid16(uint16_t start_handle,uint16_t end_handle,uint16_t uuid16,uint16_t * out_included_service_handle,uint16_t * out_included_service_start_handle,uint16_t * out_included_service_end_handle)161932e1ff05SMilanka Ringwald bool gatt_server_get_included_service_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t uuid16,
162032e1ff05SMilanka Ringwald     uint16_t * out_included_service_handle, uint16_t * out_included_service_start_handle, uint16_t * out_included_service_end_handle){
162132e1ff05SMilanka Ringwald 
162232e1ff05SMilanka Ringwald     att_iterator_t it;
162332e1ff05SMilanka Ringwald     att_iterator_init(&it);
162432e1ff05SMilanka Ringwald     while (att_iterator_has_next(&it)){
162532e1ff05SMilanka Ringwald         att_iterator_fetch_next(&it);
162632e1ff05SMilanka Ringwald         if ((it.handle != 0u) && (it.handle < start_handle)){
162732e1ff05SMilanka Ringwald             continue;
162832e1ff05SMilanka Ringwald         }
162932e1ff05SMilanka Ringwald         if (it.handle > end_handle){
163032e1ff05SMilanka Ringwald             break;  // (1)
163132e1ff05SMilanka Ringwald         }
163232e1ff05SMilanka Ringwald         if (it.handle == 0u){
163332e1ff05SMilanka Ringwald             break;
163432e1ff05SMilanka Ringwald         }
1635*c6d8b816SMatthias Ringwald         if ((it.value_len == 6u) && (att_iterator_match_uuid16(&it, GATT_INCLUDE_SERVICE_UUID))){
163632e1ff05SMilanka Ringwald             if (little_endian_read_16(it.value, 4) == uuid16){
163732e1ff05SMilanka Ringwald                 *out_included_service_handle = it.handle;
163832e1ff05SMilanka Ringwald                 *out_included_service_start_handle = little_endian_read_16(it.value, 0);
163932e1ff05SMilanka Ringwald                 *out_included_service_end_handle = little_endian_read_16(it.value, 2);
164032e1ff05SMilanka Ringwald                 return true;
164132e1ff05SMilanka Ringwald             }
164232e1ff05SMilanka Ringwald         }
164332e1ff05SMilanka Ringwald     }
164432e1ff05SMilanka Ringwald     return false;
164532e1ff05SMilanka Ringwald }
164632e1ff05SMilanka Ringwald 
16478b1d64c6SMatthias Ringwald // 1-item cache to optimize query during write_callback
att_persistent_ccc_cache(att_iterator_t * it)1648fd75a1c5SMatthias Ringwald static void att_persistent_ccc_cache(att_iterator_t * it){
1649fd75a1c5SMatthias Ringwald     att_persistent_ccc_handle = it->handle;
1650f688bdb8SMatthias Ringwald     if ((it->flags & (uint16_t)ATT_PROPERTY_UUID128) != 0u){
1651edc4e664SMilanka Ringwald         att_persistent_ccc_uuid16 = 0u;
1652fd75a1c5SMatthias Ringwald     } else {
1653fd75a1c5SMatthias Ringwald         att_persistent_ccc_uuid16 = little_endian_read_16(it->uuid, 0);
1654fd75a1c5SMatthias Ringwald     }
16558b1d64c6SMatthias Ringwald }
16568b1d64c6SMatthias Ringwald 
att_is_persistent_ccc(uint16_t handle)16574d5915b3SMatthias Ringwald bool att_is_persistent_ccc(uint16_t handle){
1658fd75a1c5SMatthias Ringwald     if (handle != att_persistent_ccc_handle){
16598b1d64c6SMatthias Ringwald         att_iterator_t it;
16605f5dcb67SMatthias Ringwald         bool ok = att_find_handle(&it, handle);
16619f7e3af1SMilanka Ringwald         if (!ok){
16629f7e3af1SMilanka Ringwald             return false;
16639f7e3af1SMilanka Ringwald         }
1664fd75a1c5SMatthias Ringwald         att_persistent_ccc_cache(&it);
16658b1d64c6SMatthias Ringwald     }
1666c15a35beSMatthias Ringwald     switch (att_persistent_ccc_uuid16){
1667c15a35beSMatthias Ringwald         case GATT_CLIENT_CHARACTERISTICS_CONFIGURATION:
1668c15a35beSMatthias Ringwald         case GATT_CLIENT_SUPPORTED_FEATURES:
1669c15a35beSMatthias Ringwald             return true;
1670c15a35beSMatthias Ringwald         default:
1671c15a35beSMatthias Ringwald             return false;
1672c15a35beSMatthias Ringwald     }
16738b1d64c6SMatthias Ringwald }
16748b1d64c6SMatthias Ringwald 
1675bfd413f0SMatthias Ringwald // att_read_callback helpers
att_read_callback_handle_blob(const uint8_t * blob,uint16_t blob_size,uint16_t offset,uint8_t * buffer,uint16_t buffer_size)1676bfd413f0SMatthias Ringwald uint16_t att_read_callback_handle_blob(const uint8_t * blob, uint16_t blob_size, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
1677c3b0d2daSMilanka Ringwald     btstack_assert(blob != NULL);
1678c3b0d2daSMilanka Ringwald 
16799305033eSMatthias Ringwald     if (buffer != NULL){
1680c3b0d2daSMilanka Ringwald         uint16_t bytes_to_copy = 0;
1681c3b0d2daSMilanka Ringwald         if (blob_size >= offset){
1682c3b0d2daSMilanka Ringwald             bytes_to_copy = btstack_min(blob_size - offset, buffer_size);
16836535961aSMatthias Ringwald             (void)memcpy(buffer, &blob[offset], bytes_to_copy);
1684c3b0d2daSMilanka Ringwald         }
1685bfd413f0SMatthias Ringwald         return bytes_to_copy;
1686bfd413f0SMatthias Ringwald     } else {
1687bfd413f0SMatthias Ringwald         return blob_size;
1688bfd413f0SMatthias Ringwald     }
1689bfd413f0SMatthias Ringwald }
1690bfd413f0SMatthias Ringwald 
att_read_callback_handle_little_endian_32(uint32_t value,uint16_t offset,uint8_t * buffer,uint16_t buffer_size)1691bfd413f0SMatthias Ringwald uint16_t att_read_callback_handle_little_endian_32(uint32_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
1692bfd413f0SMatthias Ringwald     uint8_t value_buffer[4];
1693bfd413f0SMatthias Ringwald     little_endian_store_32(value_buffer, 0, value);
1694bfd413f0SMatthias Ringwald     return att_read_callback_handle_blob(value_buffer, sizeof(value_buffer), offset, buffer, buffer_size);
1695bfd413f0SMatthias Ringwald }
1696bfd413f0SMatthias Ringwald 
att_read_callback_handle_little_endian_16(uint16_t value,uint16_t offset,uint8_t * buffer,uint16_t buffer_size)1697bfd413f0SMatthias Ringwald uint16_t att_read_callback_handle_little_endian_16(uint16_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
1698bfd413f0SMatthias Ringwald     uint8_t value_buffer[2];
1699bfd413f0SMatthias Ringwald     little_endian_store_16(value_buffer, 0, value);
1700bfd413f0SMatthias Ringwald     return att_read_callback_handle_blob(value_buffer, sizeof(value_buffer), offset, buffer, buffer_size);
1701bfd413f0SMatthias Ringwald }
1702bfd413f0SMatthias Ringwald 
att_read_callback_handle_byte(uint8_t value,uint16_t offset,uint8_t * buffer,uint16_t buffer_size)1703bfd413f0SMatthias Ringwald uint16_t att_read_callback_handle_byte(uint8_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
1704bfd413f0SMatthias Ringwald     uint8_t value_buffer[1];
1705bfd413f0SMatthias Ringwald     value_buffer[0] = value;
1706bfd413f0SMatthias Ringwald     return att_read_callback_handle_blob(value_buffer, sizeof(value_buffer), offset, buffer, buffer_size);
1707bfd413f0SMatthias Ringwald }
170844127f43SMatthias Ringwald 
170944127f43SMatthias Ringwald 
1710c3b0d2daSMilanka Ringwald #ifdef ENABLE_BTP
1711c3b0d2daSMilanka Ringwald 
1712c3b0d2daSMilanka Ringwald // start of auto-PTS testing code, not used in production
1713d8d5d864SMilanka Ringwald // LCOV_EXCL_START
171444127f43SMatthias Ringwald #include "btp.h"
171544127f43SMatthias Ringwald 
btp_permissions_for_flags(uint16_t flags)171644127f43SMatthias Ringwald static uint8_t btp_permissions_for_flags(uint16_t flags){
171744127f43SMatthias Ringwald 
171844127f43SMatthias Ringwald     // see BT_GATT_PERM_*
171944127f43SMatthias Ringwald     // https://docs.zephyrproject.org/latest/reference/bluetooth/gatt.html
172044127f43SMatthias Ringwald     // set bit indicates requirement, e.g. BTP_GATT_PERM_READ_AUTHN requires authenticated connection
172144127f43SMatthias Ringwald 
172244127f43SMatthias Ringwald     uint8_t permissions = 0;
172344127f43SMatthias Ringwald 
172444127f43SMatthias Ringwald     uint8_t read_security_level = 0;
172544127f43SMatthias Ringwald     uint8_t write_security_level = 0;
1726edc4e664SMilanka Ringwald     if (flags & (uint16_t)ATT_PROPERTY_READ){
1727edc4e664SMilanka Ringwald         if (flags & (uint16_t)ATT_PROPERTY_READ_PERMISSION_BIT_0) {
172844127f43SMatthias Ringwald             read_security_level |= 1;
172944127f43SMatthias Ringwald         }
1730edc4e664SMilanka Ringwald         if (flags & (uint16_t)ATT_PROPERTY_READ_PERMISSION_BIT_1) {
173144127f43SMatthias Ringwald             read_security_level |= 2;
173244127f43SMatthias Ringwald         }
1733d386e180SMatthias Ringwald         if (read_security_level == ATT_SECURITY_AUTHORIZED) {
173444127f43SMatthias Ringwald             permissions |= BTP_GATT_PERM_READ_AUTHZ;
173544127f43SMatthias Ringwald         }
1736d386e180SMatthias Ringwald         if (read_security_level == ATT_SECURITY_AUTHENTICATED) {
173744127f43SMatthias Ringwald             permissions |= BTP_GATT_PERM_READ_AUTHN;
173844127f43SMatthias Ringwald         }
1739d386e180SMatthias Ringwald         if (read_security_level == ATT_SECURITY_ENCRYPTED) {
174044127f43SMatthias Ringwald             permissions |= BTP_GATT_PERM_READ_ENC;
174144127f43SMatthias Ringwald         }
1742d386e180SMatthias Ringwald         if (read_security_level == ATT_SECURITY_NONE) {
174344127f43SMatthias Ringwald             permissions |= BTP_GATT_PERM_READ;
174444127f43SMatthias Ringwald         }
174544127f43SMatthias Ringwald     }
174644127f43SMatthias Ringwald     if (flags & (ATT_PROPERTY_WRITE | ATT_PROPERTY_WRITE_WITHOUT_RESPONSE)){
1747edc4e664SMilanka Ringwald         if (flags & (uint16_t)ATT_PROPERTY_WRITE_PERMISSION_BIT_0) {
174844127f43SMatthias Ringwald             write_security_level |= 1;
174944127f43SMatthias Ringwald         }
1750edc4e664SMilanka Ringwald         if (flags & (uint16_t)ATT_PROPERTY_WRITE_PERMISSION_BIT_1) {
175144127f43SMatthias Ringwald             write_security_level |= 2;
175244127f43SMatthias Ringwald         }
1753d386e180SMatthias Ringwald         if (write_security_level == ATT_SECURITY_AUTHORIZED) {
175444127f43SMatthias Ringwald             permissions |= BTP_GATT_PERM_WRITE_AUTHZ;
175544127f43SMatthias Ringwald         }
1756d386e180SMatthias Ringwald         if (write_security_level == ATT_SECURITY_AUTHENTICATED) {
175744127f43SMatthias Ringwald             permissions |= BTP_GATT_PERM_WRITE_AUTHN;
175844127f43SMatthias Ringwald         }
1759d386e180SMatthias Ringwald         if (write_security_level == ATT_SECURITY_ENCRYPTED) {
176044127f43SMatthias Ringwald             permissions |= BTP_GATT_PERM_WRITE_ENC;
176144127f43SMatthias Ringwald         }
1762d386e180SMatthias Ringwald         if (write_security_level == ATT_SECURITY_NONE) {
176344127f43SMatthias Ringwald             permissions |= BTP_GATT_PERM_WRITE;
176444127f43SMatthias Ringwald         }
176544127f43SMatthias Ringwald     }
176644127f43SMatthias Ringwald     return permissions;
176744127f43SMatthias Ringwald }
176844127f43SMatthias Ringwald 
btp_att_get_attributes_by_uuid16(uint16_t start_handle,uint16_t end_handle,uint16_t uuid16,uint8_t * response_buffer,uint16_t response_buffer_size)176944127f43SMatthias Ringwald uint16_t btp_att_get_attributes_by_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t uuid16, uint8_t * response_buffer, uint16_t response_buffer_size){
1770b45b7749SMilanka Ringwald     log_info("btp_att_get_attributes_by_uuid16 %04x from 0x%04x to 0x%04x, db %p", uuid16, start_handle, end_handle, att_database);
1771e7733bf2SMatthias Ringwald     att_dump_attributes();
1772e7733bf2SMatthias Ringwald 
177344127f43SMatthias Ringwald     uint8_t num_attributes = 0;
177444127f43SMatthias Ringwald     uint16_t pos = 1;
1775e7733bf2SMatthias Ringwald 
177644127f43SMatthias Ringwald     att_iterator_t  it;
177744127f43SMatthias Ringwald     att_iterator_init(&it);
177844127f43SMatthias Ringwald     while (att_iterator_has_next(&it) && ((pos + 6) < response_buffer_size)){
177944127f43SMatthias Ringwald         att_iterator_fetch_next(&it);
1780e7733bf2SMatthias Ringwald         log_info("handle %04x", it.handle);
17819f7e3af1SMilanka Ringwald         if (it.handle == 0){
17829f7e3af1SMilanka Ringwald             break;
17839f7e3af1SMilanka Ringwald         }
17849f7e3af1SMilanka Ringwald         if (it.handle < start_handle){
17859f7e3af1SMilanka Ringwald             continue;
17869f7e3af1SMilanka Ringwald         }
17879f7e3af1SMilanka Ringwald         if (it.handle > end_handle){
17889f7e3af1SMilanka Ringwald             break;
17899f7e3af1SMilanka Ringwald         }
179059f775cfSMatthias Ringwald         if ((uuid16 == 0) || att_iterator_match_uuid16(&it, uuid16)){
179144127f43SMatthias Ringwald             little_endian_store_16(response_buffer, pos, it.handle);
179244127f43SMatthias Ringwald             pos += 2;
179344127f43SMatthias Ringwald             response_buffer[pos++] = btp_permissions_for_flags(it.flags);
179444127f43SMatthias Ringwald             response_buffer[pos++] = 2;
179544127f43SMatthias Ringwald             little_endian_store_16(response_buffer, pos, uuid16);
179644127f43SMatthias Ringwald             pos += 2;
179744127f43SMatthias Ringwald             num_attributes++;
179844127f43SMatthias Ringwald         }
179944127f43SMatthias Ringwald     }
180044127f43SMatthias Ringwald     response_buffer[0] = num_attributes;
180144127f43SMatthias Ringwald     return pos;
180244127f43SMatthias Ringwald }
180344127f43SMatthias Ringwald 
btp_att_get_attributes_by_uuid128(uint16_t start_handle,uint16_t end_handle,const uint8_t * uuid128,uint8_t * response_buffer,uint16_t response_buffer_size)180444127f43SMatthias Ringwald uint16_t btp_att_get_attributes_by_uuid128(uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128, uint8_t * response_buffer, uint16_t response_buffer_size){
180544127f43SMatthias Ringwald     uint8_t num_attributes = 0;
180644127f43SMatthias Ringwald     uint16_t pos = 1;
180744127f43SMatthias Ringwald     att_iterator_t  it;
180844127f43SMatthias Ringwald     att_iterator_init(&it);
180944127f43SMatthias Ringwald     while (att_iterator_has_next(&it) && ((pos + 20) < response_buffer_size)){
181044127f43SMatthias Ringwald         att_iterator_fetch_next(&it);
18119f7e3af1SMilanka Ringwald         if (it.handle == 0){
18129f7e3af1SMilanka Ringwald             break;
18139f7e3af1SMilanka Ringwald         }
18149f7e3af1SMilanka Ringwald         if (it.handle < start_handle){
18159f7e3af1SMilanka Ringwald             continue;
18169f7e3af1SMilanka Ringwald         }
18179f7e3af1SMilanka Ringwald         if (it.handle > end_handle){
18189f7e3af1SMilanka Ringwald             break;
18199f7e3af1SMilanka Ringwald         }
182044127f43SMatthias Ringwald         if (att_iterator_match_uuid(&it, (uint8_t*) uuid128, 16)){
182144127f43SMatthias Ringwald             little_endian_store_16(response_buffer, pos, it.handle);
182244127f43SMatthias Ringwald             pos += 2;
182344127f43SMatthias Ringwald             response_buffer[pos++] = btp_permissions_for_flags(it.flags);
182444127f43SMatthias Ringwald             response_buffer[pos++] = 16;
182544127f43SMatthias Ringwald             reverse_128(uuid128, &response_buffer[pos]);
182644127f43SMatthias Ringwald             pos += 16;
182744127f43SMatthias Ringwald             num_attributes++;
182844127f43SMatthias Ringwald         }
182944127f43SMatthias Ringwald     }
183044127f43SMatthias Ringwald     response_buffer[0] = num_attributes;
183144127f43SMatthias Ringwald     return pos;
183244127f43SMatthias Ringwald }
183344127f43SMatthias Ringwald 
btp_att_get_attribute_value(att_connection_t * att_connection,uint16_t attribute_handle,uint8_t * response_buffer,uint16_t response_buffer_size)18346de29f25SMatthias Ringwald uint16_t btp_att_get_attribute_value(att_connection_t * att_connection, uint16_t attribute_handle, uint8_t * response_buffer, uint16_t response_buffer_size){
183544127f43SMatthias Ringwald     att_iterator_t it;
18365f5dcb67SMatthias Ringwald     bool ok = att_find_handle(&it, attribute_handle);
18379f7e3af1SMilanka Ringwald     if (!ok){
18389f7e3af1SMilanka Ringwald         return 0;
18399f7e3af1SMilanka Ringwald     }
184044127f43SMatthias Ringwald 
184144127f43SMatthias Ringwald     uint16_t pos = 0;
18426de29f25SMatthias Ringwald     // field: ATT_Response - simulate READ operation on given connection
18436de29f25SMatthias Ringwald     response_buffer[pos++] = att_validate_security(att_connection, ATT_READ, &it);
184444127f43SMatthias Ringwald     // fetch len
184544127f43SMatthias Ringwald     // assume: con handle not relevant here, else, it needs to get passed in
184644127f43SMatthias Ringwald     // att_update_value_len(&it, HCI_CON_HANDLE_INVALID);
184744127f43SMatthias Ringwald     uint16_t bytes_to_copy = btstack_min( response_buffer_size - 3, it.value_len);
184844127f43SMatthias Ringwald     little_endian_store_16(response_buffer, pos, bytes_to_copy);
184944127f43SMatthias Ringwald     pos += 2;
185044127f43SMatthias Ringwald     // get value - only works for non-dynamic data
185144127f43SMatthias Ringwald     if (it.value){
185244127f43SMatthias Ringwald         memcpy(&response_buffer[pos], it.value, bytes_to_copy);
185344127f43SMatthias Ringwald         pos += bytes_to_copy;
185444127f43SMatthias Ringwald     }
185544127f43SMatthias Ringwald     return pos;
185644127f43SMatthias Ringwald }
1857d8d5d864SMilanka Ringwald // LCOV_EXCL_STOP
185844127f43SMatthias Ringwald #endif
1859