xref: /btstack/src/ble/att_db.c (revision c824d78c0a34df89b57d535abafcc7dacf30bb06)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
24  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define BTSTACK_FILE__ "att_db.c"
39 
40 #include <string.h>
41 
42 #include "ble/att_db.h"
43 #include "ble/core.h"
44 #include "bluetooth.h"
45 #include "btstack_debug.h"
46 #include "btstack_util.h"
47 
48 // check for ENABLE_ATT_DELAYED_READ_RESPONSE -> ENABLE_ATT_DELAYED_RESPONSE,
49 #ifdef ENABLE_ATT_DELAYED_READ_RESPONSE
50     #error "ENABLE_ATT_DELAYED_READ_RESPONSE was replaced by ENABLE_ATT_DELAYED_RESPONSE. Please update btstack_config.h"
51 #endif
52 
53 typedef enum {
54     ATT_READ,
55     ATT_WRITE,
56 } att_operation_t;
57 
58 
59 static int is_Bluetooth_Base_UUID(uint8_t const *uuid){
60     // Bluetooth Base UUID 00000000-0000-1000-8000-00805F9B34FB in little endian
61     static const uint8_t bluetooth_base_uuid[] = { 0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
62 
63     if (memcmp(&uuid[0],  &bluetooth_base_uuid[0], 12) != 0){
64         return false;
65     }
66     if (memcmp(&uuid[14], &bluetooth_base_uuid[14], 2) != 0){
67         return false;
68     }
69     return true;
70 
71 }
72 
73 static uint16_t uuid16_from_uuid(uint16_t uuid_len, uint8_t * uuid){
74     if (uuid_len == 2u){
75         return little_endian_read_16(uuid, 0u);
76     }
77     if (!is_Bluetooth_Base_UUID(uuid)){
78         return 0;
79     }
80     return little_endian_read_16(uuid, 12);
81 }
82 
83 // ATT Database
84 
85 // new java-style iterator
86 typedef struct att_iterator {
87     // private
88     uint8_t const * att_ptr;
89     // public
90     uint16_t size;
91     uint16_t flags;
92     uint16_t handle;
93     uint8_t  const * uuid;
94     uint16_t value_len;
95     uint8_t  const * value;
96 } att_iterator_t;
97 
98 static void att_persistent_ccc_cache(att_iterator_t * it);
99 
100 static uint8_t const * att_database = NULL;
101 static att_read_callback_t  att_read_callback  = NULL;
102 static att_write_callback_t att_write_callback = NULL;
103 static int      att_prepare_write_error_code   = 0;
104 static uint16_t att_prepare_write_error_handle = 0x0000;
105 
106 // single cache for att_is_persistent_ccc - stores flags before write callback
107 static uint16_t att_persistent_ccc_handle;
108 static uint16_t att_persistent_ccc_uuid16;
109 
110 static void att_iterator_init(att_iterator_t *it){
111     it->att_ptr = att_database;
112 }
113 
114 static bool att_iterator_has_next(att_iterator_t *it){
115     return it->att_ptr != NULL;
116 }
117 
118 static void att_iterator_fetch_next(att_iterator_t *it){
119     it->size   = little_endian_read_16(it->att_ptr, 0);
120     if (it->size == 0u){
121         it->flags = 0;
122         it->handle = 0;
123         it->uuid = NULL;
124         it->value_len = 0;
125         it->value = NULL;
126         it->att_ptr = NULL;
127         return;
128     }
129     it->flags  = little_endian_read_16(it->att_ptr, 2);
130     it->handle = little_endian_read_16(it->att_ptr, 4);
131     it->uuid   = &it->att_ptr[6];
132     // handle 128 bit UUIDs
133     if ((it->flags & (uint16_t)ATT_PROPERTY_UUID128) != 0u){
134         it->value_len = it->size - 22u;
135         it->value  = &it->att_ptr[22];
136     } else {
137         it->value_len = it->size - 8u;
138         it->value  = &it->att_ptr[8];
139     }
140     // advance AFTER setting values
141     it->att_ptr += it->size;
142 }
143 
144 static int att_iterator_match_uuid16(att_iterator_t *it, uint16_t uuid){
145     if (it->handle == 0u){
146         return 0u;
147     }
148     if (it->flags & (uint16_t)ATT_PROPERTY_UUID128){
149         if (!is_Bluetooth_Base_UUID(it->uuid)){
150             return 0;
151         }
152         return little_endian_read_16(it->uuid, 12) == uuid;
153     }
154     return little_endian_read_16(it->uuid, 0)  == uuid;
155 }
156 
157 static int att_iterator_match_uuid(att_iterator_t *it, uint8_t *uuid, uint16_t uuid_len){
158     if (it->handle == 0u){
159         return 0u;
160     }
161     // input: UUID16
162     if (uuid_len == 2u) {
163         return att_iterator_match_uuid16(it, little_endian_read_16(uuid, 0));
164     }
165     // input and db: UUID128
166     if ((it->flags & (uint16_t)ATT_PROPERTY_UUID128) != 0u){
167         return memcmp(it->uuid, uuid, 16) == 0;
168     }
169     // input: UUID128, db: UUID16
170     if (!is_Bluetooth_Base_UUID(uuid)){
171         return 0;
172     }
173     return little_endian_read_16(uuid, 12) == little_endian_read_16(it->uuid, 0);
174 }
175 
176 
177 static int att_find_handle(att_iterator_t *it, uint16_t handle){
178     if (handle == 0u){
179         return 0u;
180     }
181     att_iterator_init(it);
182     while (att_iterator_has_next(it)){
183         att_iterator_fetch_next(it);
184         if (it->handle != handle){
185             continue;
186         }
187         return 1;
188     }
189     return 0;
190 }
191 
192 // experimental client API
193 uint16_t att_uuid_for_handle(uint16_t attribute_handle){
194     att_iterator_t it;
195     int ok = att_find_handle(&it, attribute_handle);
196     if (!ok){
197         return 0;
198     }
199     if ((it.flags & (uint16_t)ATT_PROPERTY_UUID128) != 0u){
200         return 0u;
201     }
202     return little_endian_read_16(it.uuid, 0);
203 }
204 // end of client API
205 
206 static void att_update_value_len(att_iterator_t *it, hci_con_handle_t con_handle){
207     if ((it->flags & (uint16_t)ATT_PROPERTY_DYNAMIC) == 0u){
208         return;
209     }
210     it->value_len = (*att_read_callback)(con_handle, it->handle, 0, NULL, 0);
211     return;
212 }
213 
214 // copy attribute value from offset into buffer with given size
215 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){
216 
217     // DYNAMIC
218     if ((it->flags & (uint16_t)ATT_PROPERTY_DYNAMIC) != 0u){
219         return (*att_read_callback)(con_handle, it->handle, offset, buffer, buffer_size);
220     }
221 
222     // STATIC
223     uint16_t bytes_to_copy = btstack_min(it->value_len - offset, buffer_size);
224     (void)memcpy(buffer, it->value, bytes_to_copy);
225     return bytes_to_copy;
226 }
227 
228 void att_set_db(uint8_t const * db){
229     // validate db version
230     if (db == NULL){
231         return;
232     }
233     if (*db != (uint8_t)ATT_DB_VERSION){
234         log_error("ATT DB version differs, please regenerate .h from .gatt file or update att_db_util.c");
235         return;
236     }
237     log_info("att_set_db %p", db);
238     // ignore db version
239     att_database = &db[1];
240 }
241 
242 void att_set_read_callback(att_read_callback_t callback){
243     att_read_callback = callback;
244 }
245 
246 void att_set_write_callback(att_write_callback_t callback){
247     att_write_callback = callback;
248 }
249 
250 void att_dump_attributes(void){
251     att_iterator_t it;
252     att_iterator_init(&it);
253     uint8_t uuid128[16];
254     log_info("att_dump_attributes, table %p", att_database);
255     while (att_iterator_has_next(&it)){
256         att_iterator_fetch_next(&it);
257         if (it.handle == 0u) {
258             log_info("Handle: END");
259             return;
260         }
261         log_info("Handle: 0x%04x, flags: 0x%04x, uuid: ", it.handle, it.flags);
262         if ((it.flags & (uint16_t)ATT_PROPERTY_UUID128) != 0u){
263             reverse_128(it.uuid, uuid128);
264             log_info("%s", uuid128_to_str(uuid128));
265         } else {
266             log_info("%04x", little_endian_read_16(it.uuid, 0));
267         }
268         log_info(", value_len: %u, value: ", it.value_len);
269         log_info_hexdump(it.value, it.value_len);
270     }
271 }
272 
273 static void att_prepare_write_reset(void){
274     att_prepare_write_error_code = 0;
275     att_prepare_write_error_handle = 0x0000;
276 }
277 
278 static void att_prepare_write_update_errors(uint8_t error_code, uint16_t handle){
279     // first ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LENGTH has highest priority
280     if ((error_code == (uint8_t)ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LENGTH) && (error_code != (uint8_t)att_prepare_write_error_code)){
281         att_prepare_write_error_code = error_code;
282         att_prepare_write_error_handle = handle;
283         return;
284     }
285     // first ATT_ERROR_INVALID_OFFSET is next
286     if ((error_code == (uint8_t)ATT_ERROR_INVALID_OFFSET) && (att_prepare_write_error_code == 0)){
287         att_prepare_write_error_code = error_code;
288         att_prepare_write_error_handle = handle;
289         return;
290     }
291 }
292 
293 static uint16_t setup_error(uint8_t * response_buffer, uint8_t request_opcode, uint16_t handle, uint8_t error_code){
294     response_buffer[0] = (uint8_t)ATT_ERROR_RESPONSE;
295     response_buffer[1] = request_opcode;
296     little_endian_store_16(response_buffer, 2, handle);
297     response_buffer[4] = error_code;
298     return 5;
299 }
300 
301 static inline uint16_t setup_error_read_not_permitted(uint8_t * response_buffer, uint8_t request_opcode, uint16_t start_handle){
302     return setup_error(response_buffer, request_opcode, start_handle, ATT_ERROR_READ_NOT_PERMITTED);
303 }
304 
305 static inline uint16_t setup_error_write_not_permitted(uint8_t * response_buffer, uint8_t request, uint16_t start_handle){
306     return setup_error(response_buffer, request, start_handle, ATT_ERROR_WRITE_NOT_PERMITTED);
307 }
308 
309 static inline uint16_t setup_error_atribute_not_found(uint8_t * response_buffer, uint8_t request_opcode, uint16_t start_handle){
310     return setup_error(response_buffer, request_opcode, start_handle, ATT_ERROR_ATTRIBUTE_NOT_FOUND);
311 }
312 
313 static inline uint16_t setup_error_invalid_handle(uint8_t * response_buffer, uint8_t request_opcode, uint16_t handle){
314     return setup_error(response_buffer, request_opcode, handle, ATT_ERROR_INVALID_HANDLE);
315 }
316 
317 static inline uint16_t setup_error_invalid_offset(uint8_t * response_buffer, uint8_t request_opcode, uint16_t handle){
318     return setup_error(response_buffer, request_opcode, handle, ATT_ERROR_INVALID_OFFSET);
319 }
320 
321 static inline uint16_t setup_error_invalid_pdu(uint8_t *response_buffer, uint8_t request_opcode) {
322     return setup_error(response_buffer, request_opcode, 0, ATT_ERROR_INVALID_PDU);
323 }
324 
325 struct att_security_settings {
326     uint8_t required_security_level;
327     bool    requires_secure_connection;
328 };
329 
330 static void att_validate_security_get_settings(struct att_security_settings * security_settings, att_operation_t operation, att_iterator_t *it){
331     security_settings->required_security_level = 0u;
332     security_settings->requires_secure_connection = false;
333     switch (operation){
334         case ATT_READ:
335             if ((it->flags & (uint16_t)ATT_PROPERTY_READ_PERMISSION_BIT_0) != 0u){
336                 security_settings->required_security_level |= 1u;
337             }
338             if ((it->flags & (uint16_t)ATT_PROPERTY_READ_PERMISSION_BIT_1) != 0u){
339                 security_settings->required_security_level |= 2u;
340             }
341             if ((it->flags & (uint16_t)ATT_PROPERTY_READ_PERMISSION_SC) != 0u){
342                 security_settings->requires_secure_connection = true;
343             }
344             break;
345         case ATT_WRITE:
346             if ((it->flags & (uint16_t)ATT_PROPERTY_WRITE_PERMISSION_BIT_0) != 0u){
347                 security_settings->required_security_level |= 1u;
348             }
349             if ((it->flags & (uint16_t)ATT_PROPERTY_WRITE_PERMISSION_BIT_1) != 0u){
350                 security_settings->required_security_level |= 2u;
351             }
352             if ((it->flags & (uint16_t)ATT_PROPERTY_WRITE_PERMISSION_SC) != 0u){
353                 security_settings->requires_secure_connection = true;
354             }
355             break;
356         default:
357             btstack_assert(false);
358             break;
359     }
360 }
361 
362 static uint8_t att_validate_security(att_connection_t * att_connection, att_operation_t operation, att_iterator_t * it){
363     struct att_security_settings security_settings;
364     att_validate_security_get_settings(&security_settings, operation, it);
365 
366     uint8_t required_encryption_size = (uint8_t)(it->flags >> 12);
367     if (required_encryption_size != 0u){
368         required_encryption_size++;   // store -1 to fit into 4 bit
369     }
370     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",
371         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);
372 
373     bool sc_missing = security_settings.requires_secure_connection && (att_connection->secure_connection == 0u);
374     switch (security_settings.required_security_level){
375         case ATT_SECURITY_AUTHORIZED:
376             if ((att_connection->authorized == 0u) || sc_missing){
377                 return ATT_ERROR_INSUFFICIENT_AUTHORIZATION;
378             }
379             /* fall through */
380         case ATT_SECURITY_AUTHENTICATED:
381             if ((att_connection->authenticated == 0u) || sc_missing){
382                 return ATT_ERROR_INSUFFICIENT_AUTHENTICATION;
383             }
384             /* fall through */
385         case ATT_SECURITY_ENCRYPTED:
386             if ((required_encryption_size > 0u) && ((att_connection->encryption_key_size == 0u) || sc_missing)){
387                 return ATT_ERROR_INSUFFICIENT_ENCRYPTION;
388             }
389             if (required_encryption_size > att_connection->encryption_key_size){
390                 return ATT_ERROR_INSUFFICIENT_ENCRYPTION_KEY_SIZE;
391             }
392             break;
393         default:
394             break;
395     }
396     return ATT_ERROR_SUCCESS;
397 }
398 
399 //
400 // MARK: ATT_EXCHANGE_MTU_REQUEST
401 //
402 static uint16_t handle_exchange_mtu_request(att_connection_t * att_connection, uint8_t * request_buffer,  uint16_t request_len,
403                                          uint8_t * response_buffer){
404 
405     if (request_len != 3u){
406         return setup_error_invalid_pdu(response_buffer, ATT_EXCHANGE_MTU_REQUEST);
407     }
408 
409     uint16_t client_rx_mtu = little_endian_read_16(request_buffer, 1);
410 
411     // find min(local max mtu, remote mtu) >= ATT_DEFAULT_MTU and use as mtu for this connection
412     uint16_t min_mtu = btstack_min(client_rx_mtu, att_connection->max_mtu);
413     uint16_t new_mtu = btstack_max(ATT_DEFAULT_MTU, min_mtu);
414     att_connection->mtu_exchanged = true;
415     att_connection->mtu = new_mtu;
416 
417     response_buffer[0] = ATT_EXCHANGE_MTU_RESPONSE;
418     little_endian_store_16(response_buffer, 1, att_connection->mtu);
419     return 3;
420 }
421 
422 
423 //
424 // MARK: ATT_FIND_INFORMATION_REQUEST
425 //
426 // TODO: handle other types then GATT_PRIMARY_SERVICE_UUID and GATT_SECONDARY_SERVICE_UUID
427 //
428 static uint16_t handle_find_information_request2(att_connection_t * att_connection, uint8_t * response_buffer, uint16_t response_buffer_size,
429                                            uint16_t start_handle, uint16_t end_handle){
430 
431     UNUSED(att_connection);
432 
433     log_info("ATT_FIND_INFORMATION_REQUEST: from %04X to %04X", start_handle, end_handle);
434     uint8_t request_type = ATT_FIND_INFORMATION_REQUEST;
435 
436     if ((start_handle > end_handle) || (start_handle == 0u)){
437         return setup_error_invalid_handle(response_buffer, request_type, start_handle);
438     }
439 
440     uint16_t offset   = 1;
441     uint16_t uuid_len = 0;
442 
443     att_iterator_t it;
444     att_iterator_init(&it);
445     while (att_iterator_has_next(&it)){
446         att_iterator_fetch_next(&it);
447         if (!it.handle){
448             break;
449         }
450         if (it.handle > end_handle){
451             break;
452         }
453         if (it.handle < start_handle){
454             continue;
455         }
456 
457         // log_info("Handle 0x%04x", it.handle);
458 
459         uint16_t this_uuid_len = (it.flags & (uint16_t)ATT_PROPERTY_UUID128) ? 16u : 2u;
460 
461         // check if value has same len as last one if not first result
462         if (offset > 1u){
463             if (this_uuid_len != uuid_len) {
464                 break;
465             }
466         }
467 
468         // first
469         if (offset == 1u) {
470             uuid_len = this_uuid_len;
471             // set format field
472             response_buffer[offset] = (it.flags & (uint16_t)ATT_PROPERTY_UUID128) ? 0x02u : 0x01u;
473             offset++;
474         }
475 
476         // space?
477         if ((offset + 2u + uuid_len) > response_buffer_size){
478             break;
479         }
480 
481         // store
482         little_endian_store_16(response_buffer, offset, it.handle);
483         offset += 2u;
484 
485         (void)memcpy(response_buffer + offset, it.uuid, uuid_len);
486         offset += uuid_len;
487     }
488 
489     if (offset == 1u){
490         return setup_error_atribute_not_found(response_buffer, request_type, start_handle);
491     }
492 
493     response_buffer[0] = ATT_FIND_INFORMATION_REPLY;
494     return offset;
495 }
496 
497 static uint16_t handle_find_information_request(att_connection_t * att_connection, uint8_t * request_buffer,  uint16_t request_len,
498                                          uint8_t * response_buffer, uint16_t response_buffer_size){
499 
500     if (request_len != 5u){
501         return setup_error_invalid_pdu(response_buffer, ATT_FIND_INFORMATION_REQUEST);
502     }
503 
504     uint16_t start_handle = little_endian_read_16(request_buffer, 1);
505     uint16_t end_handle = little_endian_read_16(request_buffer, 3);
506     return handle_find_information_request2(att_connection, response_buffer, response_buffer_size, start_handle, end_handle);
507 }
508 
509 //
510 // MARK: ATT_FIND_BY_TYPE_VALUE
511 //
512 // "Only attributes with attribute handles between and including the Starting Handle parameter
513 // and the Ending Handle parameter that match the requested attri- bute type and the attribute
514 // value that have sufficient permissions to allow reading will be returned" -> (1)
515 //
516 // TODO: handle other types then GATT_PRIMARY_SERVICE_UUID and GATT_SECONDARY_SERVICE_UUID
517 //
518 // NOTE: doesn't handle DYNAMIC values
519 // NOTE: only supports 16 bit UUIDs
520 //
521 static uint16_t handle_find_by_type_value_request(att_connection_t * att_connection, uint8_t * request_buffer,  uint16_t request_len,
522                                            uint8_t * response_buffer, uint16_t response_buffer_size){
523     UNUSED(att_connection);
524 
525     if (request_len < 7u){
526         return setup_error_invalid_pdu(response_buffer, ATT_FIND_BY_TYPE_VALUE_REQUEST);
527     }
528 
529     // parse request
530     uint16_t start_handle = little_endian_read_16(request_buffer, 1);
531     uint16_t end_handle = little_endian_read_16(request_buffer, 3);
532     uint16_t attribute_type = little_endian_read_16(request_buffer, 5);
533     const uint8_t *attribute_value = &request_buffer[7];
534     uint16_t attribute_len = request_len - 7u;
535 
536     log_info("ATT_FIND_BY_TYPE_VALUE_REQUEST: from %04X to %04X, type %04X, value: ", start_handle, end_handle, attribute_type);
537     log_info_hexdump(attribute_value, attribute_len);
538     uint8_t request_type = ATT_FIND_BY_TYPE_VALUE_REQUEST;
539 
540     if ((start_handle > end_handle) || (start_handle == 0u)){
541         return setup_error_invalid_handle(response_buffer, request_type, start_handle);
542     }
543 
544     uint16_t offset      = 1;
545     bool in_group        = false;
546     uint16_t prev_handle = 0;
547 
548     att_iterator_t it;
549     att_iterator_init(&it);
550     while (att_iterator_has_next(&it)){
551         att_iterator_fetch_next(&it);
552 
553         if ((it.handle != 0u) && (it.handle < start_handle)){
554             continue;
555         }
556         if (it.handle > end_handle){
557         break;  // (1)
558     }
559 
560         // close current tag, if within a group and a new service definition starts or we reach end of att db
561         if (in_group &&
562             ((it.handle == 0u) || att_iterator_match_uuid16(&it, GATT_PRIMARY_SERVICE_UUID) || att_iterator_match_uuid16(&it, GATT_SECONDARY_SERVICE_UUID))){
563 
564             log_info("End of group, handle 0x%04x", prev_handle);
565             little_endian_store_16(response_buffer, offset, prev_handle);
566             offset += 2u;
567             in_group = false;
568 
569             // check if space for another handle pair available
570             if ((offset + 4u) > response_buffer_size){
571                 break;
572             }
573         }
574 
575         // keep track of previous handle
576         prev_handle = it.handle;
577 
578         // does current attribute match
579         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)){
580             log_info("Begin of group, handle 0x%04x", it.handle);
581             little_endian_store_16(response_buffer, offset, it.handle);
582             offset += 2u;
583             in_group = true;
584         }
585     }
586 
587     if (offset == 1u){
588         return setup_error_atribute_not_found(response_buffer, request_type, start_handle);
589     }
590 
591     response_buffer[0] = ATT_FIND_BY_TYPE_VALUE_RESPONSE;
592     return offset;
593 }
594 
595 //
596 // MARK: ATT_READ_BY_TYPE_REQUEST
597 //
598 static uint16_t handle_read_by_type_request2(att_connection_t * att_connection, uint8_t * response_buffer, uint16_t response_buffer_size,
599                                       uint16_t start_handle, uint16_t end_handle,
600                                       uint16_t attribute_type_len, uint8_t * attribute_type){
601 
602     log_info("ATT_READ_BY_TYPE_REQUEST: from %04X to %04X, type: ", start_handle, end_handle);
603     log_info_hexdump(attribute_type, attribute_type_len);
604     uint8_t request_type = ATT_READ_BY_TYPE_REQUEST;
605 
606     if ((start_handle > end_handle) || (start_handle == 0u)){
607         return setup_error_invalid_handle(response_buffer, request_type, start_handle);
608     }
609 
610     uint16_t offset   = 1;
611     uint16_t pair_len = 0;
612 
613     att_iterator_t it;
614     att_iterator_init(&it);
615     uint8_t error_code = 0;
616     uint16_t first_matching_but_unreadable_handle = 0;
617 
618     while (att_iterator_has_next(&it)){
619         att_iterator_fetch_next(&it);
620 
621         if ((it.handle == 0u ) || (it.handle > end_handle)){
622             break;
623         }
624 
625         // does current attribute match
626         if ((it.handle < start_handle) || !att_iterator_match_uuid(&it, attribute_type, attribute_type_len)){
627             continue;
628         }
629 
630         // skip handles that cannot be read but remember that there has been at least one
631         if ((it.flags & (uint16_t)ATT_PROPERTY_READ) == 0u) {
632             if (first_matching_but_unreadable_handle == 0u) {
633                 first_matching_but_unreadable_handle = it.handle;
634             }
635             continue;
636         }
637 
638         // check security requirements
639         error_code = att_validate_security(att_connection, ATT_READ, &it);
640         if (error_code != 0u){
641             break;
642         }
643 
644         att_update_value_len(&it, att_connection->con_handle);
645 
646 #ifdef ENABLE_ATT_DELAYED_RESPONSE
647         if (it.value_len == (uint16_t)ATT_READ_RESPONSE_PENDING){
648             return ATT_READ_RESPONSE_PENDING;
649         }
650 #endif
651 
652         // allow to return ATT Error Code in ATT Read Callback
653         if (it.value_len > (uint16_t)ATT_READ_ERROR_CODE_OFFSET){
654             error_code =  (uint8_t)(it.value_len - (uint16_t)ATT_READ_ERROR_CODE_OFFSET);
655             break;
656         }
657 
658         // check if value has same len as last one
659         uint16_t this_pair_len = 2u + it.value_len;
660         if ((offset > 1u) && (pair_len != this_pair_len)) {
661             break;
662         }
663 
664         // first
665         if (offset == 1u) {
666             pair_len = this_pair_len;
667             response_buffer[offset] = (uint8_t) pair_len;
668             offset++;
669         }
670 
671         // space?
672         if ((offset + pair_len) > response_buffer_size) {
673             if (offset > 2u){
674                 break;
675             }
676             it.value_len = response_buffer_size - 4u;
677             response_buffer[1u] = 2u + it.value_len;
678         }
679 
680         // store
681         little_endian_store_16(response_buffer, offset, it.handle);
682         offset += 2u;
683         uint16_t bytes_copied = att_copy_value(&it, 0, response_buffer + offset, it.value_len, att_connection->con_handle);
684         offset += bytes_copied;
685     }
686 
687     // at least one attribute could be read
688     if (offset > 1u){
689         response_buffer[0] = ATT_READ_BY_TYPE_RESPONSE;
690         return offset;
691     }
692 
693     // first attribute had an error
694     if (error_code != 0u){
695         return setup_error(response_buffer, request_type, start_handle, error_code);
696     }
697 
698     // no other errors, but all found attributes had been non-readable
699     if (first_matching_but_unreadable_handle != 0u){
700         return setup_error_read_not_permitted(response_buffer, request_type, first_matching_but_unreadable_handle);
701     }
702 
703     // attribute not found
704     return setup_error_atribute_not_found(response_buffer, request_type, start_handle);
705 }
706 
707 static uint16_t handle_read_by_type_request(att_connection_t * att_connection, uint8_t * request_buffer,  uint16_t request_len,
708                                      uint8_t * response_buffer, uint16_t response_buffer_size){
709 
710     uint16_t attribute_type_len;
711     switch (request_len){
712         case 7:
713             attribute_type_len = 2;
714             break;
715         case 21:
716             attribute_type_len = 16;
717             break;
718         default:
719             return setup_error_invalid_pdu(response_buffer, ATT_READ_BY_TYPE_REQUEST);
720     }
721 
722     uint16_t start_handle = little_endian_read_16(request_buffer, 1);
723     uint16_t end_handle = little_endian_read_16(request_buffer, 3);
724     return handle_read_by_type_request2(att_connection, response_buffer, response_buffer_size, start_handle, end_handle, attribute_type_len, &request_buffer[5]);
725 }
726 
727 //
728 // MARK: ATT_READ_BY_TYPE_REQUEST
729 //
730 static uint16_t handle_read_request2(att_connection_t * att_connection, uint8_t * response_buffer, uint16_t response_buffer_size, uint16_t handle){
731 
732     log_info("ATT_READ_REQUEST: handle %04x", handle);
733     uint8_t request_type = ATT_READ_REQUEST;
734 
735     att_iterator_t it;
736     int ok = att_find_handle(&it, handle);
737     if (!ok){
738         return setup_error_invalid_handle(response_buffer, request_type, handle);
739     }
740 
741     // check if handle can be read
742     if ((it.flags & (uint16_t)ATT_PROPERTY_READ) == 0u) {
743         return setup_error_read_not_permitted(response_buffer, request_type, handle);
744     }
745 
746     // check security requirements
747     uint8_t error_code = att_validate_security(att_connection, ATT_READ, &it);
748     if (error_code != 0u) {
749         return setup_error(response_buffer, request_type, handle, error_code);
750     }
751 
752     att_update_value_len(&it, att_connection->con_handle);
753 
754 #ifdef ENABLE_ATT_DELAYED_RESPONSE
755     if (it.value_len == (uint16_t)ATT_READ_RESPONSE_PENDING){
756         return ATT_READ_RESPONSE_PENDING;
757     }
758 #endif
759 
760     // allow to return ATT Error Code in ATT Read Callback
761     if (it.value_len > (uint16_t)ATT_READ_ERROR_CODE_OFFSET){
762         error_code = (uint8_t)(it.value_len - (uint16_t)ATT_READ_ERROR_CODE_OFFSET);
763         return setup_error(response_buffer, request_type, handle, error_code);
764     }
765 
766     // store
767     uint16_t offset   = 1;
768     uint16_t bytes_copied = att_copy_value(&it, 0, response_buffer + offset, response_buffer_size - offset, att_connection->con_handle);
769     offset += bytes_copied;
770 
771     response_buffer[0] = ATT_READ_RESPONSE;
772     return offset;
773 }
774 
775 static uint16_t handle_read_request(att_connection_t * att_connection, uint8_t * request_buffer,  uint16_t request_len,
776                              uint8_t * response_buffer, uint16_t response_buffer_size){
777 
778     if (request_len != 3u){
779         return setup_error_invalid_pdu(response_buffer, ATT_READ_REQUEST);
780     }
781 
782     uint16_t handle = little_endian_read_16(request_buffer, 1);
783     return handle_read_request2(att_connection, response_buffer, response_buffer_size, handle);
784 }
785 
786 //s
787 // MARK: ATT_READ_BLOB_REQUEST 0x0c
788 //
789 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){
790     log_info("ATT_READ_BLOB_REQUEST: handle %04x, offset %u", handle, value_offset);
791     uint8_t request_type = ATT_READ_BLOB_REQUEST;
792 
793     att_iterator_t it;
794     int ok = att_find_handle(&it, handle);
795     if (!ok){
796         return setup_error_invalid_handle(response_buffer, request_type, handle);
797     }
798 
799     // check if handle can be read
800     if ((it.flags & (uint16_t)ATT_PROPERTY_READ) == 0u) {
801         return setup_error_read_not_permitted(response_buffer, request_type, handle);
802     }
803 
804     // check security requirements
805     uint8_t error_code = att_validate_security(att_connection, ATT_READ, &it);
806     if (error_code != 0u) {
807         return setup_error(response_buffer, request_type, handle, error_code);
808     }
809 
810     att_update_value_len(&it, att_connection->con_handle);
811 
812 #ifdef ENABLE_ATT_DELAYED_RESPONSE
813     if (it.value_len == (uint16_t)ATT_READ_RESPONSE_PENDING){
814         return ATT_READ_RESPONSE_PENDING;
815     }
816 #endif
817 
818     // allow to return ATT Error Code in ATT Read Callback
819     if (it.value_len > (uint16_t)ATT_READ_ERROR_CODE_OFFSET){
820         error_code = (uint8_t)(it.value_len - (uint16_t)ATT_READ_ERROR_CODE_OFFSET);
821         return setup_error(response_buffer, request_type, handle, error_code);
822     }
823 
824     if (value_offset > it.value_len){
825         return setup_error_invalid_offset(response_buffer, request_type, handle);
826     }
827 
828     // prepare response
829     response_buffer[0] = ATT_READ_BLOB_RESPONSE;
830     uint16_t offset   = 1;
831 
832     // fetch more data if available
833     if (value_offset < it.value_len){
834         uint16_t bytes_copied = att_copy_value(&it, value_offset, &response_buffer[offset], response_buffer_size - offset, att_connection->con_handle);
835         offset += bytes_copied;
836     }
837     return offset;
838 }
839 
840 static uint16_t handle_read_blob_request(att_connection_t * att_connection, uint8_t * request_buffer,  uint16_t request_len,
841                                   uint8_t * response_buffer, uint16_t response_buffer_size){
842 
843     if (request_len != 5u){
844         return setup_error_invalid_pdu(response_buffer, ATT_READ_BLOB_REQUEST);
845     }
846 
847     uint16_t handle = little_endian_read_16(request_buffer, 1);
848     uint16_t value_offset = little_endian_read_16(request_buffer, 3);
849     return handle_read_blob_request2(att_connection, response_buffer, response_buffer_size, handle, value_offset);
850 }
851 
852 //
853 // MARK: ATT_READ_MULTIPLE_REQUEST 0x0e
854 //
855 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){
856     log_info("ATT_READ_MULTIPLE_REQUEST: num handles %u", num_handles);
857     uint8_t request_type = ATT_READ_MULTIPLE_REQUEST;
858 
859     uint16_t offset   = 1;
860 
861     uint16_t i;
862     uint8_t  error_code = 0;
863     uint16_t handle = 0;
864 
865 #ifdef ENABLE_ATT_DELAYED_RESPONSE
866     bool read_request_pending = false;
867 #endif
868 
869     for (i=0; i<num_handles; i++){
870         handle = little_endian_read_16(handles, i << 1);
871 
872         if (handle == 0u){
873             return setup_error_invalid_handle(response_buffer, request_type, handle);
874         }
875 
876         att_iterator_t it;
877 
878         int ok = att_find_handle(&it, handle);
879         if (!ok){
880             return setup_error_invalid_handle(response_buffer, request_type, handle);
881         }
882 
883         // check if handle can be read
884         if ((it.flags & (uint16_t)ATT_PROPERTY_READ) == 0u) {
885             error_code = (uint8_t)ATT_ERROR_READ_NOT_PERMITTED;
886             break;
887         }
888 
889         // check security requirements
890         error_code = att_validate_security(att_connection, ATT_READ, &it);
891         if (error_code != 0u){
892             break;
893         }
894 
895         att_update_value_len(&it, att_connection->con_handle);
896 
897 #ifdef ENABLE_ATT_DELAYED_RESPONSE
898         if (it.value_len == (uint16_t)ATT_READ_RESPONSE_PENDING) {
899             read_request_pending = true;
900         }
901         if (read_request_pending){
902             continue;
903         }
904 #endif
905 
906         // allow to return ATT Error Code in ATT Read Callback
907         if (it.value_len > (uint16_t)ATT_READ_ERROR_CODE_OFFSET){
908             error_code = (uint8_t)(it.value_len - (uint16_t)ATT_READ_ERROR_CODE_OFFSET);
909             break;
910         }
911 
912         // store
913         uint16_t bytes_copied = att_copy_value(&it, 0, response_buffer + offset, response_buffer_size - offset, att_connection->con_handle);
914         offset += bytes_copied;
915     }
916 
917     if (error_code != 0u){
918         return setup_error(response_buffer, request_type, handle, error_code);
919     }
920 
921     response_buffer[0] = (uint8_t)ATT_READ_MULTIPLE_RESPONSE;
922     return offset;
923 }
924 static uint16_t handle_read_multiple_request(att_connection_t * att_connection, uint8_t * request_buffer,  uint16_t request_len,
925                                       uint8_t * response_buffer, uint16_t response_buffer_size){
926 
927     // 1 byte opcode + two or more attribute handles (2 bytes each)
928     if ( (request_len < 5u) || ((request_len & 1u) == 0u) ){
929         return setup_error_invalid_pdu(response_buffer, ATT_READ_MULTIPLE_REQUEST);
930     }
931 
932     int num_handles = (request_len - 1u) >> 1u;
933     return handle_read_multiple_request2(att_connection, response_buffer, response_buffer_size, num_handles, &request_buffer[1]);
934 }
935 
936 //
937 // MARK: ATT_READ_BY_GROUP_TYPE_REQUEST 0x10
938 //
939 // Only handles GATT_PRIMARY_SERVICE_UUID and GATT_SECONDARY_SERVICE_UUID
940 // Core v4.0, vol 3, part g, 2.5.3
941 // "The «Primary Service» and «Secondary Service» grouping types may be used in the Read By Group Type Request.
942 //  The «Characteristic» grouping type shall not be used in the ATT Read By Group Type Request."
943 //
944 // NOTE: doesn't handle DYNAMIC values
945 //
946 // NOTE: we don't check for security as PRIMARY and SECONDAY SERVICE definition shouldn't be protected
947 // Core 4.0, vol 3, part g, 8.1
948 // "The list of services and characteristics that a device supports is not considered private or
949 //  confidential information, and therefore the Service and Characteristic Discovery procedures
950 //  shall always be permitted. "
951 //
952 static uint16_t handle_read_by_group_type_request2(att_connection_t * att_connection, uint8_t * response_buffer, uint16_t response_buffer_size,
953                                             uint16_t start_handle, uint16_t end_handle,
954                                             uint16_t attribute_type_len, uint8_t * attribute_type){
955 
956     UNUSED(att_connection);
957 
958     log_info("ATT_READ_BY_GROUP_TYPE_REQUEST: from %04X to %04X, buffer size %u, type: ", start_handle, end_handle, response_buffer_size);
959     log_info_hexdump(attribute_type, attribute_type_len);
960     uint8_t request_type = ATT_READ_BY_GROUP_TYPE_REQUEST;
961 
962     if ((start_handle > end_handle) || (start_handle == 0u)){
963         return setup_error_invalid_handle(response_buffer, request_type, start_handle);
964     }
965 
966     // assert UUID is primary or secondary service uuid
967     uint16_t uuid16 = uuid16_from_uuid(attribute_type_len, attribute_type);
968     if ((uuid16 != (uint16_t)GATT_PRIMARY_SERVICE_UUID) && (uuid16 != (uint16_t)GATT_SECONDARY_SERVICE_UUID)){
969         return setup_error(response_buffer, request_type, start_handle, ATT_ERROR_UNSUPPORTED_GROUP_TYPE);
970     }
971 
972     uint16_t offset   = 1;
973     uint16_t pair_len = 0;
974     bool     in_group = false;
975     uint16_t group_start_handle = 0;
976     uint8_t const * group_start_value = NULL;
977     uint16_t prev_handle = 0;
978 
979     att_iterator_t it;
980     att_iterator_init(&it);
981     while (att_iterator_has_next(&it)){
982         att_iterator_fetch_next(&it);
983 
984         if ((it.handle != 0u) && (it.handle < start_handle)){
985             continue;
986         }
987         if (it.handle > end_handle){
988             break;  // (1)
989         }
990 
991         // log_info("Handle 0x%04x", it.handle);
992 
993         // close current tag, if within a group and a new service definition starts or we reach end of att db
994         if (in_group &&
995             ((it.handle == 0u) || att_iterator_match_uuid16(&it, GATT_PRIMARY_SERVICE_UUID) || att_iterator_match_uuid16(&it, GATT_SECONDARY_SERVICE_UUID))){
996             // log_info("End of group, handle 0x%04x, val_len: %u", prev_handle, pair_len - 4);
997 
998             little_endian_store_16(response_buffer, offset, group_start_handle);
999             offset += 2u;
1000             little_endian_store_16(response_buffer, offset, prev_handle);
1001             offset += 2u;
1002             (void)memcpy(response_buffer + offset, group_start_value,
1003                          pair_len - 4u);
1004             offset += pair_len - 4u;
1005             in_group = false;
1006 
1007             // check if space for another handle pair available
1008             if ((offset + pair_len) > response_buffer_size){
1009                 break;
1010             }
1011         }
1012 
1013         // keep track of previous handle
1014         prev_handle = it.handle;
1015 
1016         // does current attribute match
1017         // log_info("compare: %04x == %04x", *(uint16_t*) context->attribute_type, *(uint16_t*) uuid);
1018         if ((it.handle != 0u) && att_iterator_match_uuid(&it, attribute_type, attribute_type_len)) {
1019 
1020             // check if value has same len as last one
1021             uint16_t this_pair_len = 4u + it.value_len;
1022             if (offset > 1u){
1023                 if (this_pair_len != pair_len) {
1024                     break;
1025                 }
1026             }
1027 
1028             // log_info("Begin of group, handle 0x%04x", it.handle);
1029 
1030             // first
1031             if (offset == 1u) {
1032                 pair_len = this_pair_len;
1033                 response_buffer[offset] = (uint8_t) this_pair_len;
1034                 offset++;
1035             }
1036 
1037             group_start_handle = it.handle;
1038             group_start_value  = it.value;
1039             in_group = true;
1040         }
1041     }
1042 
1043     if (offset == 1u){
1044         return setup_error_atribute_not_found(response_buffer, request_type, start_handle);
1045     }
1046 
1047     response_buffer[0] = ATT_READ_BY_GROUP_TYPE_RESPONSE;
1048     return offset;
1049 }
1050 
1051 static uint16_t handle_read_by_group_type_request(att_connection_t * att_connection, uint8_t * request_buffer,  uint16_t request_len,
1052                                            uint8_t * response_buffer, uint16_t response_buffer_size){
1053     uint16_t attribute_type_len;
1054     switch (request_len){
1055         case 7:
1056             attribute_type_len = 2;
1057             break;
1058         case 21:
1059             attribute_type_len = 16;
1060             break;
1061         default:
1062             return setup_error_invalid_pdu(response_buffer, ATT_READ_BY_GROUP_TYPE_REQUEST);
1063     }
1064 
1065     uint16_t start_handle = little_endian_read_16(request_buffer, 1);
1066     uint16_t end_handle = little_endian_read_16(request_buffer, 3);
1067     return handle_read_by_group_type_request2(att_connection, response_buffer, response_buffer_size, start_handle, end_handle, attribute_type_len, &request_buffer[5]);
1068 }
1069 
1070 //
1071 // MARK: ATT_WRITE_REQUEST 0x12
1072 static uint16_t handle_write_request(att_connection_t * att_connection, uint8_t * request_buffer,  uint16_t request_len,
1073                               uint8_t * response_buffer, uint16_t response_buffer_size){
1074 
1075     UNUSED(response_buffer_size);
1076 
1077     if (request_len < 3u){
1078         return setup_error_invalid_pdu(response_buffer, ATT_WRITE_REQUEST);
1079     }
1080 
1081     uint8_t request_type = ATT_WRITE_REQUEST;
1082 
1083     uint16_t handle = little_endian_read_16(request_buffer, 1);
1084     att_iterator_t it;
1085     int ok = att_find_handle(&it, handle);
1086     if (!ok) {
1087         return setup_error_invalid_handle(response_buffer, request_type, handle);
1088     }
1089     if (att_write_callback == NULL) {
1090         return setup_error_write_not_permitted(response_buffer, request_type, handle);
1091     }
1092     if ((it.flags & (uint16_t)ATT_PROPERTY_WRITE) == 0u) {
1093         return setup_error_write_not_permitted(response_buffer, request_type, handle);
1094     }
1095     if ((it.flags & (uint16_t)ATT_PROPERTY_DYNAMIC) == 0u) {
1096         return setup_error_write_not_permitted(response_buffer, request_type, handle);
1097     }
1098     // check security requirements
1099     int error_code = att_validate_security(att_connection, ATT_WRITE, &it);
1100     if (error_code != 0) {
1101         return setup_error(response_buffer, request_type, handle, error_code);
1102     }
1103     att_persistent_ccc_cache(&it);
1104     error_code = (*att_write_callback)(att_connection->con_handle, handle, ATT_TRANSACTION_MODE_NONE, 0u, request_buffer + 3u, request_len - 3u);
1105 
1106 #ifdef ENABLE_ATT_DELAYED_RESPONSE
1107     if (error_code == ATT_ERROR_WRITE_RESPONSE_PENDING){
1108         return ATT_INTERNAL_WRITE_RESPONSE_PENDING;
1109     }
1110 #endif
1111 
1112     if (error_code != 0) {
1113         return setup_error(response_buffer, request_type, handle, error_code);
1114     }
1115     response_buffer[0] = (uint8_t)ATT_WRITE_RESPONSE;
1116     return 1;
1117 }
1118 
1119 //
1120 // MARK: ATT_PREPARE_WRITE_REQUEST 0x16
1121 static uint16_t handle_prepare_write_request(att_connection_t * att_connection, uint8_t * request_buffer,  uint16_t request_len,
1122                                       uint8_t * response_buffer, uint16_t response_buffer_size){
1123 
1124     uint8_t request_type = ATT_PREPARE_WRITE_REQUEST;
1125 
1126     if (request_len < 5u){
1127         return setup_error_invalid_pdu(response_buffer, request_type);
1128     }
1129 
1130     uint16_t handle = little_endian_read_16(request_buffer, 1);
1131     uint16_t offset = little_endian_read_16(request_buffer, 3);
1132     if (att_write_callback == NULL) {
1133         return setup_error_write_not_permitted(response_buffer, request_type, handle);
1134     }
1135     att_iterator_t it;
1136     if (att_find_handle(&it, handle) == 0) {
1137         return setup_error_invalid_handle(response_buffer, request_type, handle);
1138     }
1139     if ((it.flags & (uint16_t)ATT_PROPERTY_WRITE) == 0u) {
1140         return setup_error_write_not_permitted(response_buffer, request_type, handle);
1141     }
1142     if ((it.flags & (uint16_t)ATT_PROPERTY_DYNAMIC) == 0u) {
1143         return setup_error_write_not_permitted(response_buffer, request_type, handle);
1144     }
1145     // check security requirements
1146     int error_code = att_validate_security(att_connection, ATT_WRITE, &it);
1147     if (error_code != 0) {
1148         return setup_error(response_buffer, request_type, handle, error_code);
1149     }
1150 
1151     error_code = (*att_write_callback)(att_connection->con_handle, handle, ATT_TRANSACTION_MODE_ACTIVE, offset, request_buffer + 5u, request_len - 5u);
1152     switch (error_code){
1153         case 0:
1154             break;
1155         case ATT_ERROR_INVALID_OFFSET:
1156         case ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LENGTH:
1157             // postpone to execute write request
1158             att_prepare_write_update_errors(error_code, handle);
1159             break;
1160 #ifdef ENABLE_ATT_DELAYED_RESPONSE
1161         case ATT_ERROR_WRITE_RESPONSE_PENDING:
1162             return ATT_INTERNAL_WRITE_RESPONSE_PENDING;
1163 #endif
1164         default:
1165             return setup_error(response_buffer, request_type, handle, error_code);
1166     }
1167 
1168     // response: echo request
1169     uint16_t bytes_to_echo = btstack_min(request_len, response_buffer_size);
1170     (void)memcpy(response_buffer, request_buffer, bytes_to_echo);
1171     response_buffer[0] = ATT_PREPARE_WRITE_RESPONSE;
1172     return request_len;
1173 }
1174 
1175 /*
1176  * @brief transcation queue of prepared writes, e.g., after disconnect
1177  */
1178 void att_clear_transaction_queue(att_connection_t * att_connection){
1179     (*att_write_callback)(att_connection->con_handle, 0, ATT_TRANSACTION_MODE_CANCEL, 0, NULL, 0);
1180 }
1181 
1182 // MARK: ATT_EXECUTE_WRITE_REQUEST 0x18
1183 // NOTE: security has been verified by handle_prepare_write_request
1184 static uint16_t handle_execute_write_request(att_connection_t * att_connection, uint8_t * request_buffer,  uint16_t request_len,
1185                                       uint8_t * response_buffer, uint16_t response_buffer_size){
1186 
1187     UNUSED(response_buffer_size);
1188 
1189     uint8_t request_type = ATT_EXECUTE_WRITE_REQUEST;
1190 
1191     if (request_len < 2u){
1192         return setup_error_invalid_pdu(response_buffer, request_type);
1193     }
1194 
1195     if (att_write_callback == NULL) {
1196         return setup_error_write_not_permitted(response_buffer, request_type, 0);
1197     }
1198 
1199     if (request_buffer[1]) {
1200         // validate queued write
1201         if (att_prepare_write_error_code == 0){
1202             att_prepare_write_error_code = (*att_write_callback)(att_connection->con_handle, 0, ATT_TRANSACTION_MODE_VALIDATE, 0, NULL, 0);
1203         }
1204 #ifdef ENABLE_ATT_DELAYED_RESPONSE
1205         if (att_prepare_write_error_code == ATT_ERROR_WRITE_RESPONSE_PENDING){
1206             return ATT_INTERNAL_WRITE_RESPONSE_PENDING;
1207         }
1208 #endif
1209         // deliver queued errors
1210         if (att_prepare_write_error_code != 0){
1211             att_clear_transaction_queue(att_connection);
1212             uint8_t  error_code = att_prepare_write_error_code;
1213             uint16_t handle     = att_prepare_write_error_handle;
1214             att_prepare_write_reset();
1215             return setup_error(response_buffer, request_type, handle, error_code);
1216         }
1217         att_write_callback(att_connection->con_handle, 0, ATT_TRANSACTION_MODE_EXECUTE, 0, NULL, 0);
1218     } else {
1219         att_clear_transaction_queue(att_connection);
1220     }
1221     response_buffer[0] = ATT_EXECUTE_WRITE_RESPONSE;
1222     return 1;
1223 }
1224 
1225 // MARK: ATT_WRITE_COMMAND 0x52
1226 // Core 4.0, vol 3, part F, 3.4.5.3
1227 // "No Error Response or Write Response shall be sent in response to this command"
1228 static void handle_write_command(att_connection_t * att_connection, uint8_t * request_buffer,  uint16_t request_len, uint16_t required_flags){
1229 
1230     if (request_len < 3u){
1231         return;
1232     }
1233 
1234     uint16_t handle = little_endian_read_16(request_buffer, 1);
1235     if (att_write_callback == NULL){
1236         return;
1237     }
1238 
1239     att_iterator_t it;
1240     int ok = att_find_handle(&it, handle);
1241     if (!ok){
1242         return;
1243     }
1244     if ((it.flags & (uint16_t)ATT_PROPERTY_DYNAMIC) == 0u){
1245         return;
1246     }
1247     if ((it.flags & required_flags) == 0u){
1248         return;
1249     }
1250     if (att_validate_security(att_connection, ATT_WRITE, &it)){
1251         return;
1252     }
1253     att_persistent_ccc_cache(&it);
1254     (*att_write_callback)(att_connection->con_handle, handle, ATT_TRANSACTION_MODE_NONE, 0u, request_buffer + 3u, request_len - 3u);
1255 }
1256 
1257 // MARK: helper for ATT_HANDLE_VALUE_NOTIFICATION and ATT_HANDLE_VALUE_INDICATION
1258 static uint16_t prepare_handle_value(att_connection_t * att_connection,
1259                                      uint16_t handle,
1260                                      const uint8_t *value,
1261                                      uint16_t value_len,
1262                                      uint8_t * response_buffer){
1263     little_endian_store_16(response_buffer, 1, handle);
1264     uint16_t bytes_to_copy = btstack_min(value_len, att_connection->mtu - 3u);
1265     (void)memcpy(&response_buffer[3], value, bytes_to_copy);
1266     return value_len + 3u;
1267 }
1268 
1269 // MARK: ATT_HANDLE_VALUE_NOTIFICATION 0x1b
1270 uint16_t att_prepare_handle_value_notification(att_connection_t * att_connection,
1271                                                uint16_t attribute_handle,
1272                                                const uint8_t *value,
1273                                                uint16_t value_len,
1274                                                uint8_t * response_buffer){
1275 
1276     response_buffer[0] = ATT_HANDLE_VALUE_NOTIFICATION;
1277     return prepare_handle_value(att_connection, attribute_handle, value, value_len, response_buffer);
1278 }
1279 
1280 // MARK: ATT_HANDLE_VALUE_INDICATION 0x1d
1281 uint16_t att_prepare_handle_value_indication(att_connection_t * att_connection,
1282                                              uint16_t attribute_handle,
1283                                              const uint8_t *value,
1284                                              uint16_t value_len,
1285                                              uint8_t * response_buffer){
1286 
1287     response_buffer[0] = ATT_HANDLE_VALUE_INDICATION;
1288     return prepare_handle_value(att_connection, attribute_handle, value, value_len, response_buffer);
1289 }
1290 
1291 // MARK: Dispatcher
1292 uint16_t att_handle_request(att_connection_t * att_connection,
1293                             uint8_t * request_buffer,
1294                             uint16_t request_len,
1295                             uint8_t * response_buffer){
1296     uint16_t response_len = 0;
1297     const uint16_t response_buffer_size = att_connection->mtu;
1298     const uint8_t  request_opcode = request_buffer[0];
1299 
1300     switch (request_opcode){
1301         case ATT_EXCHANGE_MTU_REQUEST:
1302             response_len = handle_exchange_mtu_request(att_connection, request_buffer, request_len, response_buffer);
1303             break;
1304         case ATT_FIND_INFORMATION_REQUEST:
1305             response_len = handle_find_information_request(att_connection, request_buffer, request_len,response_buffer, response_buffer_size);
1306             break;
1307         case ATT_FIND_BY_TYPE_VALUE_REQUEST:
1308             response_len = handle_find_by_type_value_request(att_connection, request_buffer, request_len, response_buffer, response_buffer_size);
1309             break;
1310         case ATT_READ_BY_TYPE_REQUEST:
1311             response_len = handle_read_by_type_request(att_connection, request_buffer, request_len, response_buffer, response_buffer_size);
1312             break;
1313         case ATT_READ_REQUEST:
1314             response_len = handle_read_request(att_connection, request_buffer, request_len, response_buffer, response_buffer_size);
1315             break;
1316         case ATT_READ_BLOB_REQUEST:
1317             response_len = handle_read_blob_request(att_connection, request_buffer, request_len, response_buffer, response_buffer_size);
1318             break;
1319         case ATT_READ_MULTIPLE_REQUEST:
1320             response_len = handle_read_multiple_request(att_connection, request_buffer, request_len, response_buffer, response_buffer_size);
1321             break;
1322         case ATT_READ_BY_GROUP_TYPE_REQUEST:
1323             response_len = handle_read_by_group_type_request(att_connection, request_buffer, request_len, response_buffer, response_buffer_size);
1324             break;
1325         case ATT_WRITE_REQUEST:
1326             response_len = handle_write_request(att_connection, request_buffer, request_len, response_buffer, response_buffer_size);
1327             break;
1328         case ATT_PREPARE_WRITE_REQUEST:
1329             response_len = handle_prepare_write_request(att_connection, request_buffer, request_len, response_buffer, response_buffer_size);
1330             break;
1331         case ATT_EXECUTE_WRITE_REQUEST:
1332             response_len = handle_execute_write_request(att_connection, request_buffer, request_len, response_buffer, response_buffer_size);
1333             break;
1334         case ATT_WRITE_COMMAND:
1335             handle_write_command(att_connection, request_buffer, request_len, ATT_PROPERTY_WRITE_WITHOUT_RESPONSE);
1336             break;
1337 #ifdef ENABLE_LE_SIGNED_WRITE
1338         case ATT_SIGNED_WRITE_COMMAND:
1339             handle_write_command(att_connection, request_buffer, request_len, ATT_PROPERTY_AUTHENTICATED_SIGNED_WRITE);
1340             break;
1341 #endif
1342         default:
1343             response_len = setup_error(response_buffer, request_opcode, 0, ATT_ERROR_REQUEST_NOT_SUPPORTED);
1344             break;
1345     }
1346     return response_len;
1347 }
1348 
1349 // returns 1 if service found. only primary service.
1350 bool gatt_server_get_handle_range_for_service_with_uuid16(uint16_t uuid16, uint16_t * start_handle, uint16_t * end_handle){
1351     bool in_group    = false;
1352     uint16_t prev_handle = 0;
1353     uint16_t service_start = 0;
1354 
1355     uint8_t attribute_value[2];
1356     int attribute_len = sizeof(attribute_value);
1357     little_endian_store_16(attribute_value, 0, uuid16);
1358 
1359     att_iterator_t it;
1360     att_iterator_init(&it);
1361     while (att_iterator_has_next(&it)){
1362         att_iterator_fetch_next(&it);
1363         int new_service_started = att_iterator_match_uuid16(&it, GATT_PRIMARY_SERVICE_UUID) || att_iterator_match_uuid16(&it, GATT_SECONDARY_SERVICE_UUID);
1364 
1365         // close current tag, if within a group and a new service definition starts or we reach end of att db
1366         if (in_group &&
1367             ((it.handle == 0u) || new_service_started)){
1368             in_group = false;
1369             // check range
1370             if ((service_start >= *start_handle) && (prev_handle <= *end_handle)){
1371                 *start_handle = service_start;
1372                 *end_handle = prev_handle;
1373                 return true;
1374             }
1375         }
1376 
1377         // keep track of previous handle
1378         prev_handle = it.handle;
1379 
1380         // check if found
1381         if ( (it.handle != 0u) && new_service_started && (attribute_len == it.value_len) && (memcmp(attribute_value, it.value, it.value_len) == 0)){
1382             service_start = it.handle;
1383             in_group = true;
1384         }
1385     }
1386     return false;
1387 }
1388 
1389 // returns false if not found
1390 uint16_t gatt_server_get_value_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t uuid16){
1391     att_iterator_t it;
1392     att_iterator_init(&it);
1393     while (att_iterator_has_next(&it)){
1394         att_iterator_fetch_next(&it);
1395         if ((it.handle != 0u) && (it.handle < start_handle)){
1396             continue;
1397         }
1398         if (it.handle > end_handle){
1399             break;  // (1)
1400         }
1401         if (it.handle == 0u){
1402             break;
1403         }
1404         if (att_iterator_match_uuid16(&it, uuid16)){
1405             return it.handle;
1406         }
1407     }
1408     return 0;
1409 }
1410 
1411 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){
1412     att_iterator_t it;
1413     att_iterator_init(&it);
1414     bool characteristic_found = false;
1415     while (att_iterator_has_next(&it)){
1416         att_iterator_fetch_next(&it);
1417         if ((it.handle != 0u) && (it.handle < start_handle)){
1418             continue;
1419         }
1420         if (it.handle > end_handle){
1421             break;  // (1)
1422         }
1423         if (it.handle == 0u){
1424             break;
1425         }
1426         if (att_iterator_match_uuid16(&it, characteristic_uuid16)){
1427             characteristic_found = true;
1428             continue;
1429         }
1430         if (att_iterator_match_uuid16(&it, GATT_PRIMARY_SERVICE_UUID)
1431          || att_iterator_match_uuid16(&it, GATT_SECONDARY_SERVICE_UUID)
1432          || att_iterator_match_uuid16(&it, GATT_CHARACTERISTICS_UUID)){
1433             if (characteristic_found){
1434                 break;
1435             }
1436             continue;
1437         }
1438         if (characteristic_found && att_iterator_match_uuid16(&it, descriptor_uuid16)){
1439             return it.handle;
1440         }
1441     }
1442     return 0;
1443 }
1444 
1445 // returns 0 if not found
1446 uint16_t gatt_server_get_client_configuration_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t characteristic_uuid16){
1447     return gatt_server_get_descriptor_handle_for_characteristic_with_uuid16(start_handle, end_handle, characteristic_uuid16, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION);
1448 }
1449 // returns 0 if not found
1450 
1451 uint16_t gatt_server_get_server_configuration_handle_for_characteristic_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t characteristic_uuid16){
1452     return gatt_server_get_descriptor_handle_for_characteristic_with_uuid16(start_handle, end_handle, characteristic_uuid16, GATT_SERVER_CHARACTERISTICS_CONFIGURATION);
1453 }
1454 
1455 // returns true if service found. only primary service.
1456 bool gatt_server_get_handle_range_for_service_with_uuid128(const uint8_t * uuid128, uint16_t * start_handle, uint16_t * end_handle){
1457     bool in_group    = false;
1458     uint16_t prev_handle = 0;
1459 
1460     uint8_t attribute_value[16];
1461     uint16_t attribute_len = (uint16_t)sizeof(attribute_value);
1462     reverse_128(uuid128, attribute_value);
1463 
1464     att_iterator_t it;
1465     att_iterator_init(&it);
1466     while (att_iterator_has_next(&it)){
1467         att_iterator_fetch_next(&it);
1468         int new_service_started = att_iterator_match_uuid16(&it, GATT_PRIMARY_SERVICE_UUID) || att_iterator_match_uuid16(&it, GATT_SECONDARY_SERVICE_UUID);
1469 
1470         // close current tag, if within a group and a new service definition starts or we reach end of att db
1471         if (in_group &&
1472             ((it.handle == 0u) || new_service_started)){
1473             *end_handle = prev_handle;
1474             return true;
1475         }
1476 
1477         // keep track of previous handle
1478         prev_handle = it.handle;
1479 
1480         // check if found
1481         if ( (it.handle != 0u) && new_service_started && (attribute_len == it.value_len) && (memcmp(attribute_value, it.value, it.value_len) == 0)){
1482             *start_handle = it.handle;
1483             in_group = true;
1484         }
1485     }
1486     return false;
1487 }
1488 
1489 // returns 0 if not found
1490 uint16_t gatt_server_get_value_handle_for_characteristic_with_uuid128(uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128){
1491     uint8_t attribute_value[16];
1492     reverse_128(uuid128, attribute_value);
1493     att_iterator_t it;
1494     att_iterator_init(&it);
1495     while (att_iterator_has_next(&it)){
1496         att_iterator_fetch_next(&it);
1497         if ((it.handle != 0u) && (it.handle < start_handle)){
1498             continue;
1499         }
1500         if (it.handle > end_handle){
1501             break;  // (1)
1502         }
1503         if (it.handle == 0u){
1504             break;
1505         }
1506         if (att_iterator_match_uuid(&it, attribute_value, 16)){
1507             return it.handle;
1508         }
1509     }
1510     return 0;
1511 }
1512 
1513 // returns 0 if not found
1514 uint16_t gatt_server_get_client_configuration_handle_for_characteristic_with_uuid128(uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128){
1515     uint8_t attribute_value[16];
1516     reverse_128(uuid128, attribute_value);
1517     att_iterator_t it;
1518     att_iterator_init(&it);
1519     int characteristic_found = 0;
1520     while (att_iterator_has_next(&it)){
1521         att_iterator_fetch_next(&it);
1522         if ((it.handle != 0u) && (it.handle < start_handle)){
1523             continue;
1524         }
1525         if (it.handle > end_handle){
1526             break;  // (1)
1527         }
1528         if (it.handle == 0u){
1529             break;
1530         }
1531         if (att_iterator_match_uuid(&it, attribute_value, 16)){
1532             characteristic_found = 1;
1533             continue;
1534         }
1535         if (att_iterator_match_uuid16(&it, GATT_PRIMARY_SERVICE_UUID)
1536          || att_iterator_match_uuid16(&it, GATT_SECONDARY_SERVICE_UUID)
1537          || att_iterator_match_uuid16(&it, GATT_CHARACTERISTICS_UUID)){
1538             if (characteristic_found){
1539                 break;
1540             }
1541             continue;
1542         }
1543         if (characteristic_found && att_iterator_match_uuid16(&it, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION)){
1544             return it.handle;
1545         }
1546     }
1547     return 0;
1548 }
1549 
1550 
1551 bool gatt_server_get_included_service_with_uuid16(uint16_t start_handle, uint16_t end_handle, uint16_t uuid16,
1552     uint16_t * out_included_service_handle, uint16_t * out_included_service_start_handle, uint16_t * out_included_service_end_handle){
1553 
1554     att_iterator_t it;
1555     att_iterator_init(&it);
1556     while (att_iterator_has_next(&it)){
1557         att_iterator_fetch_next(&it);
1558         if ((it.handle != 0u) && (it.handle < start_handle)){
1559             continue;
1560         }
1561         if (it.handle > end_handle){
1562             break;  // (1)
1563         }
1564         if (it.handle == 0u){
1565             break;
1566         }
1567         if ((it.value_len == 6) && (att_iterator_match_uuid16(&it, GATT_INCLUDE_SERVICE_UUID))){
1568             if (little_endian_read_16(it.value, 4) == uuid16){
1569                 *out_included_service_handle = it.handle;
1570                 *out_included_service_start_handle = little_endian_read_16(it.value, 0);
1571                 *out_included_service_end_handle = little_endian_read_16(it.value, 2);
1572                 return true;
1573             }
1574         }
1575     }
1576     return false;
1577 }
1578 
1579 // 1-item cache to optimize query during write_callback
1580 static void att_persistent_ccc_cache(att_iterator_t * it){
1581     att_persistent_ccc_handle = it->handle;
1582     if (it->flags & (uint16_t)ATT_PROPERTY_UUID128){
1583         att_persistent_ccc_uuid16 = 0u;
1584     } else {
1585         att_persistent_ccc_uuid16 = little_endian_read_16(it->uuid, 0);
1586     }
1587 }
1588 
1589 bool att_is_persistent_ccc(uint16_t handle){
1590     if (handle != att_persistent_ccc_handle){
1591         att_iterator_t it;
1592         int ok = att_find_handle(&it, handle);
1593         if (!ok){
1594             return false;
1595         }
1596         att_persistent_ccc_cache(&it);
1597     }
1598     return att_persistent_ccc_uuid16 == (uint16_t)GATT_CLIENT_CHARACTERISTICS_CONFIGURATION;
1599 }
1600 
1601 // att_read_callback helpers
1602 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){
1603     btstack_assert(blob != NULL);
1604 
1605     if (buffer != NULL){
1606         uint16_t bytes_to_copy = 0;
1607         if (blob_size >= offset){
1608             bytes_to_copy = btstack_min(blob_size - offset, buffer_size);
1609             (void)memcpy(buffer, &blob[offset], bytes_to_copy);
1610         }
1611         return bytes_to_copy;
1612     } else {
1613         return blob_size;
1614     }
1615 }
1616 
1617 uint16_t att_read_callback_handle_little_endian_32(uint32_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
1618     uint8_t value_buffer[4];
1619     little_endian_store_32(value_buffer, 0, value);
1620     return att_read_callback_handle_blob(value_buffer, sizeof(value_buffer), offset, buffer, buffer_size);
1621 }
1622 
1623 uint16_t att_read_callback_handle_little_endian_16(uint16_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
1624     uint8_t value_buffer[2];
1625     little_endian_store_16(value_buffer, 0, value);
1626     return att_read_callback_handle_blob(value_buffer, sizeof(value_buffer), offset, buffer, buffer_size);
1627 }
1628 
1629 uint16_t att_read_callback_handle_byte(uint8_t value, uint16_t offset, uint8_t * buffer, uint16_t buffer_size){
1630     uint8_t value_buffer[1];
1631     value_buffer[0] = value;
1632     return att_read_callback_handle_blob(value_buffer, sizeof(value_buffer), offset, buffer, buffer_size);
1633 }
1634 
1635 
1636 #ifdef ENABLE_BTP
1637 
1638 // start of auto-PTS testing code, not used in production
1639 // LCOV_EXCL_START
1640 #include "btp.h"
1641 
1642 static uint8_t btp_permissions_for_flags(uint16_t flags){
1643 
1644     // see BT_GATT_PERM_*
1645     // https://docs.zephyrproject.org/latest/reference/bluetooth/gatt.html
1646     // set bit indicates requirement, e.g. BTP_GATT_PERM_READ_AUTHN requires authenticated connection
1647 
1648     uint8_t permissions = 0;
1649 
1650     uint8_t read_security_level = 0;
1651     uint8_t write_security_level = 0;
1652     if (flags & (uint16_t)ATT_PROPERTY_READ){
1653         if (flags & (uint16_t)ATT_PROPERTY_READ_PERMISSION_BIT_0) {
1654             read_security_level |= 1;
1655         }
1656         if (flags & (uint16_t)ATT_PROPERTY_READ_PERMISSION_BIT_1) {
1657             read_security_level |= 2;
1658         }
1659         if (read_security_level == ATT_SECURITY_AUTHORIZED) {
1660             permissions |= BTP_GATT_PERM_READ_AUTHZ;
1661         }
1662         if (read_security_level == ATT_SECURITY_AUTHENTICATED) {
1663             permissions |= BTP_GATT_PERM_READ_AUTHN;
1664         }
1665         if (read_security_level == ATT_SECURITY_ENCRYPTED) {
1666             permissions |= BTP_GATT_PERM_READ_ENC;
1667         }
1668         if (read_security_level == ATT_SECURITY_NONE) {
1669             permissions |= BTP_GATT_PERM_READ;
1670         }
1671     }
1672     if (flags & (ATT_PROPERTY_WRITE | ATT_PROPERTY_WRITE_WITHOUT_RESPONSE)){
1673         if (flags & (uint16_t)ATT_PROPERTY_WRITE_PERMISSION_BIT_0) {
1674             write_security_level |= 1;
1675         }
1676         if (flags & (uint16_t)ATT_PROPERTY_WRITE_PERMISSION_BIT_1) {
1677             write_security_level |= 2;
1678         }
1679         if (write_security_level == ATT_SECURITY_AUTHORIZED) {
1680             permissions |= BTP_GATT_PERM_WRITE_AUTHZ;
1681         }
1682         if (write_security_level == ATT_SECURITY_AUTHENTICATED) {
1683             permissions |= BTP_GATT_PERM_WRITE_AUTHN;
1684         }
1685         if (write_security_level == ATT_SECURITY_ENCRYPTED) {
1686             permissions |= BTP_GATT_PERM_WRITE_ENC;
1687         }
1688         if (write_security_level == ATT_SECURITY_NONE) {
1689             permissions |= BTP_GATT_PERM_WRITE;
1690         }
1691     }
1692     return permissions;
1693 }
1694 
1695 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){
1696     log_info("btp_att_get_attributes_by_uuid16 %04x from 0x%04x to 0x%04x, db %p", uuid16, start_handle, end_handle, att_database);
1697     att_dump_attributes();
1698 
1699     uint8_t num_attributes = 0;
1700     uint16_t pos = 1;
1701 
1702     att_iterator_t  it;
1703     att_iterator_init(&it);
1704     while (att_iterator_has_next(&it) && ((pos + 6) < response_buffer_size)){
1705         att_iterator_fetch_next(&it);
1706         log_info("handle %04x", it.handle);
1707         if (it.handle == 0){
1708             break;
1709         }
1710         if (it.handle < start_handle){
1711             continue;
1712         }
1713         if (it.handle > end_handle){
1714             break;
1715         }
1716         if ((uuid16 == 0) || att_iterator_match_uuid16(&it, uuid16)){
1717             little_endian_store_16(response_buffer, pos, it.handle);
1718             pos += 2;
1719             response_buffer[pos++] = btp_permissions_for_flags(it.flags);
1720             response_buffer[pos++] = 2;
1721             little_endian_store_16(response_buffer, pos, uuid16);
1722             pos += 2;
1723             num_attributes++;
1724         }
1725     }
1726     response_buffer[0] = num_attributes;
1727     return pos;
1728 }
1729 
1730 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){
1731     uint8_t num_attributes = 0;
1732     uint16_t pos = 1;
1733     att_iterator_t  it;
1734     att_iterator_init(&it);
1735     while (att_iterator_has_next(&it) && ((pos + 20) < response_buffer_size)){
1736         att_iterator_fetch_next(&it);
1737         if (it.handle == 0){
1738             break;
1739         }
1740         if (it.handle < start_handle){
1741             continue;
1742         }
1743         if (it.handle > end_handle){
1744             break;
1745         }
1746         if (att_iterator_match_uuid(&it, (uint8_t*) uuid128, 16)){
1747             little_endian_store_16(response_buffer, pos, it.handle);
1748             pos += 2;
1749             response_buffer[pos++] = btp_permissions_for_flags(it.flags);
1750             response_buffer[pos++] = 16;
1751             reverse_128(uuid128, &response_buffer[pos]);
1752             pos += 16;
1753             num_attributes++;
1754         }
1755     }
1756     response_buffer[0] = num_attributes;
1757     return pos;
1758 }
1759 
1760 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){
1761     att_iterator_t it;
1762     int ok = att_find_handle(&it, attribute_handle);
1763     if (!ok){
1764         return 0;
1765     }
1766 
1767     uint16_t pos = 0;
1768     // field: ATT_Response - simulate READ operation on given connection
1769     response_buffer[pos++] = att_validate_security(att_connection, ATT_READ, &it);
1770     // fetch len
1771     // assume: con handle not relevant here, else, it needs to get passed in
1772     // att_update_value_len(&it, HCI_CON_HANDLE_INVALID);
1773     uint16_t bytes_to_copy = btstack_min( response_buffer_size - 3, it.value_len);
1774     little_endian_store_16(response_buffer, pos, bytes_to_copy);
1775     pos += 2;
1776     // get value - only works for non-dynamic data
1777     if (it.value){
1778         memcpy(&response_buffer[pos], it.value, bytes_to_copy);
1779         pos += bytes_to_copy;
1780     }
1781     return pos;
1782 }
1783 // LCOV_EXCL_STOP
1784 #endif
1785