xref: /btstack/src/ble/gatt_client.c (revision 4601294989d1426f390e414b6614470fb0aee9a6)
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__ "gatt_client.c"
39 
40 #include <stdint.h>
41 #include <string.h>
42 #include <stddef.h>
43 
44 #include "btstack_config.h"
45 
46 #include "ble/att_dispatch.h"
47 #include "ble/att_db.h"
48 #include "ble/gatt_client.h"
49 #include "ble/le_device_db.h"
50 #include "ble/sm.h"
51 #include "btstack_debug.h"
52 #include "btstack_event.h"
53 #include "btstack_memory.h"
54 #include "btstack_run_loop.h"
55 #include "btstack_util.h"
56 #include "hci.h"
57 #include "hci_dump.h"
58 #include "l2cap.h"
59 #include "classic/sdp_client.h"
60 #include "bluetooth_gatt.h"
61 #include "bluetooth_sdp.h"
62 #include "classic/sdp_util.h"
63 
64 static btstack_linked_list_t gatt_client_connections;
65 static btstack_linked_list_t gatt_client_value_listeners;
66 static btstack_packet_callback_registration_t hci_event_callback_registration;
67 static btstack_packet_callback_registration_t sm_event_callback_registration;
68 
69 // GATT Client Configuration
70 static bool                 gatt_client_mtu_exchange_enabled;
71 static gap_security_level_t gatt_client_required_security_level;
72 
73 static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size);
74 static void gatt_client_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
75 static void gatt_client_report_error_if_pending(gatt_client_t *gatt_client, uint8_t att_error_code);
76 
77 #ifdef ENABLE_LE_SIGNED_WRITE
78 static void att_signed_write_handle_cmac_result(uint8_t hash[8]);
79 #endif
80 
81 void gatt_client_init(void){
82     gatt_client_connections = NULL;
83 
84     // default configuration
85     gatt_client_mtu_exchange_enabled    = true;
86     gatt_client_required_security_level = LEVEL_0;
87 
88     // register for HCI Events
89     hci_event_callback_registration.callback = &gatt_client_event_packet_handler;
90     hci_add_event_handler(&hci_event_callback_registration);
91 
92     // register for SM Events
93     sm_event_callback_registration.callback = &gatt_client_event_packet_handler;
94     sm_add_event_handler(&sm_event_callback_registration);
95 
96     // and ATT Client PDUs
97     att_dispatch_register_client(gatt_client_att_packet_handler);
98 }
99 
100 void gatt_client_set_required_security_level(gap_security_level_t level){
101     gatt_client_required_security_level = level;
102 }
103 
104 static gatt_client_t * gatt_client_for_timer(btstack_timer_source_t * ts){
105     btstack_linked_list_iterator_t it;
106     btstack_linked_list_iterator_init(&it, &gatt_client_connections);
107     while (btstack_linked_list_iterator_has_next(&it)){
108         gatt_client_t * gatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it);
109         if (&gatt_client->gc_timeout == ts) {
110             return gatt_client;
111         }
112     }
113     return NULL;
114 }
115 
116 static void gatt_client_timeout_handler(btstack_timer_source_t * timer){
117     gatt_client_t * gatt_client = gatt_client_for_timer(timer);
118     if (gatt_client == NULL) return;
119     log_info("GATT client timeout handle, handle 0x%02x", gatt_client->con_handle);
120     gatt_client_report_error_if_pending(gatt_client, ATT_ERROR_TIMEOUT);
121 }
122 
123 static void gatt_client_timeout_start(gatt_client_t * gatt_client){
124     log_info("GATT client timeout start, handle 0x%02x", gatt_client->con_handle);
125     btstack_run_loop_remove_timer(&gatt_client->gc_timeout);
126     btstack_run_loop_set_timer_handler(&gatt_client->gc_timeout, gatt_client_timeout_handler);
127     btstack_run_loop_set_timer(&gatt_client->gc_timeout, 30000); // 30 seconds sm timeout
128     btstack_run_loop_add_timer(&gatt_client->gc_timeout);
129 }
130 
131 static void gatt_client_timeout_stop(gatt_client_t * gatt_client){
132     log_info("GATT client timeout stop, handle 0x%02x", gatt_client->con_handle);
133     btstack_run_loop_remove_timer(&gatt_client->gc_timeout);
134 }
135 
136 static gap_security_level_t gatt_client_le_security_level_for_connection(hci_con_handle_t con_handle){
137     uint8_t encryption_key_size = gap_encryption_key_size(con_handle);
138     if (encryption_key_size == 0) return LEVEL_0;
139 
140     bool authenticated = gap_authenticated(con_handle);
141     if (!authenticated) return LEVEL_2;
142 
143     return encryption_key_size == 16 ? LEVEL_4 : LEVEL_3;
144 }
145 
146 static gatt_client_t * gatt_client_get_context_for_handle(uint16_t handle){
147     btstack_linked_item_t *it;
148     for (it = (btstack_linked_item_t *) gatt_client_connections; it != NULL; it = it->next){
149         gatt_client_t * gatt_client = (gatt_client_t *) it;
150         if (gatt_client->con_handle == handle){
151             return gatt_client;
152         }
153     }
154     return NULL;
155 }
156 
157 
158 // @return gatt_client context
159 // returns existing one, or tries to setup new one
160 static uint8_t gatt_client_provide_context_for_handle(hci_con_handle_t con_handle, gatt_client_t ** out_gatt_client){
161     gatt_client_t * gatt_client = gatt_client_get_context_for_handle(con_handle);
162 
163     if (gatt_client != NULL){
164         *out_gatt_client = gatt_client;
165         return ERROR_CODE_SUCCESS;
166     }
167 
168     // bail if no such hci connection
169     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
170     if (hci_connection == NULL){
171         log_error("No connection for handle 0x%04x", con_handle);
172         *out_gatt_client = NULL;
173         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
174     }
175 
176     gatt_client = btstack_memory_gatt_client_get();
177     if (gatt_client == NULL){
178         *out_gatt_client = NULL;
179         return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED;
180     }
181     // init state
182     gatt_client->bearer_type = ATT_BEARER_UNENHANCED_LE;
183     gatt_client->con_handle = con_handle;
184     gatt_client->mtu = ATT_DEFAULT_MTU;
185     gatt_client->security_level = gatt_client_le_security_level_for_connection(con_handle);
186     if (gatt_client_mtu_exchange_enabled){
187         gatt_client->mtu_state = SEND_MTU_EXCHANGE;
188     } else {
189         gatt_client->mtu_state = MTU_AUTO_EXCHANGE_DISABLED;
190     }
191     gatt_client->gatt_client_state = P_READY;
192     btstack_linked_list_add(&gatt_client_connections, (btstack_linked_item_t*)gatt_client);
193 
194     // get unenhanced att bearer state
195     if (hci_connection->att_connection.mtu_exchanged){
196         gatt_client->mtu = hci_connection->att_connection.mtu;
197         gatt_client->mtu_state = MTU_EXCHANGED;
198     }
199     *out_gatt_client = gatt_client;
200     return ERROR_CODE_SUCCESS;
201 }
202 
203 static bool is_ready(gatt_client_t * gatt_client){
204     return gatt_client->gatt_client_state == P_READY;
205 }
206 
207 static uint8_t gatt_client_provide_context_for_request(hci_con_handle_t con_handle, gatt_client_t ** out_gatt_client){
208     gatt_client_t * gatt_client = NULL;
209     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
210     if (status != ERROR_CODE_SUCCESS){
211         return status;
212     }
213 
214     if (is_ready(gatt_client) == 0){
215         return GATT_CLIENT_IN_WRONG_STATE;
216     }
217 
218     gatt_client_timeout_start(gatt_client);
219 
220     *out_gatt_client = gatt_client;
221 
222     return status;
223 }
224 
225 int gatt_client_is_ready(hci_con_handle_t con_handle){
226     gatt_client_t * gatt_client;
227     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
228     if (status != ERROR_CODE_SUCCESS){
229         return 0;
230     }
231     return is_ready(gatt_client) ? 1 : 0;
232 }
233 
234 void gatt_client_mtu_enable_auto_negotiation(uint8_t enabled){
235     gatt_client_mtu_exchange_enabled = enabled != 0;
236 }
237 
238 uint8_t gatt_client_get_mtu(hci_con_handle_t con_handle, uint16_t * mtu){
239     gatt_client_t * gatt_client;
240     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
241     if (status != ERROR_CODE_SUCCESS){
242         return status;
243     }
244 
245     if ((gatt_client->mtu_state == MTU_EXCHANGED) || (gatt_client->mtu_state == MTU_AUTO_EXCHANGE_DISABLED)){
246         *mtu = gatt_client->mtu;
247         return ERROR_CODE_SUCCESS;
248     }
249     *mtu = ATT_DEFAULT_MTU;
250     return GATT_CLIENT_IN_WRONG_STATE;
251 }
252 
253 static uint8_t *gatt_client_reserve_request_buffer(gatt_client_t *gatt_client) {
254     l2cap_reserve_packet_buffer();
255     return l2cap_get_outgoing_buffer();
256 }
257 
258 // precondition: can_send_packet_now == TRUE
259 static uint8_t gatt_client_send(gatt_client_t * gatt_client, uint16_t len){
260     switch (gatt_client->bearer_type){
261         case ATT_BEARER_UNENHANCED_LE:
262             return l2cap_send_prepared_connectionless(gatt_client->con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, len);
263 #ifdef ENABLE_GATT_OVER_CLASSIC
264         case ATT_BEARER_UNENHANCED_CLASSIC:
265             return l2cap_send_prepared(gatt_client->l2cap_cid, len);
266 #endif
267         default:
268             btstack_unreachable();
269             return ERROR_CODE_HARDWARE_FAILURE;
270     }
271 }
272 
273 // precondition: can_send_packet_now == TRUE
274 static uint8_t att_confirmation(gatt_client_t * gatt_client) {
275     uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
276 
277     request[0] = ATT_HANDLE_VALUE_CONFIRMATION;
278 
279     return gatt_client_send(gatt_client, 1);
280 }
281 
282 // precondition: can_send_packet_now == TRUE
283 static uint8_t att_find_information_request(gatt_client_t *gatt_client, uint8_t request_type, uint16_t start_handle,
284                                             uint16_t end_handle) {
285     uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
286 
287     request[0] = request_type;
288     little_endian_store_16(request, 1, start_handle);
289     little_endian_store_16(request, 3, end_handle);
290 
291     return gatt_client_send(gatt_client, 5);
292 }
293 
294 // precondition: can_send_packet_now == TRUE
295 static uint8_t
296 att_find_by_type_value_request(gatt_client_t *gatt_client, uint8_t request_type, uint16_t attribute_group_type,
297                                uint16_t start_handle, uint16_t end_handle, uint8_t *value, uint16_t value_size) {
298     uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
299     request[0] = request_type;
300 
301     little_endian_store_16(request, 1, start_handle);
302     little_endian_store_16(request, 3, end_handle);
303     little_endian_store_16(request, 5, attribute_group_type);
304     (void)memcpy(&request[7], value, value_size);
305 
306     return gatt_client_send(gatt_client, 7u + value_size);
307 }
308 
309 // precondition: can_send_packet_now == TRUE
310 static uint8_t
311 att_read_by_type_or_group_request_for_uuid16(gatt_client_t *gatt_client, uint8_t request_type, uint16_t uuid16,
312                                              uint16_t start_handle, uint16_t end_handle) {
313     uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
314 
315     request[0] = request_type;
316     little_endian_store_16(request, 1, start_handle);
317     little_endian_store_16(request, 3, end_handle);
318     little_endian_store_16(request, 5, uuid16);
319 
320     return gatt_client_send(gatt_client, 7);
321 }
322 
323 // precondition: can_send_packet_now == TRUE
324 static uint8_t
325 att_read_by_type_or_group_request_for_uuid128(gatt_client_t *gatt_client, uint8_t request_type, const uint8_t *uuid128,
326                                               uint16_t start_handle, uint16_t end_handle) {
327     uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
328 
329     request[0] = request_type;
330     little_endian_store_16(request, 1, start_handle);
331     little_endian_store_16(request, 3, end_handle);
332     reverse_128(uuid128, &request[5]);
333 
334     return gatt_client_send(gatt_client, 21);
335 }
336 
337 // precondition: can_send_packet_now == TRUE
338 static uint8_t att_read_request(gatt_client_t *gatt_client, uint8_t request_type, uint16_t attribute_handle) {
339     uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
340 
341     request[0] = request_type;
342     little_endian_store_16(request, 1, attribute_handle);
343 
344     return gatt_client_send(gatt_client, 3);
345 }
346 
347 // precondition: can_send_packet_now == TRUE
348 static uint8_t att_read_blob_request(gatt_client_t *gatt_client, uint8_t request_type, uint16_t attribute_handle,
349                                      uint16_t value_offset) {
350     uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
351 
352     request[0] = request_type;
353     little_endian_store_16(request, 1, attribute_handle);
354     little_endian_store_16(request, 3, value_offset);
355 
356     return gatt_client_send(gatt_client, 5);
357 }
358 
359 static uint8_t
360 att_read_multiple_request(gatt_client_t *gatt_client, uint16_t num_value_handles, uint16_t *value_handles) {
361     uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
362 
363     request[0] = ATT_READ_MULTIPLE_REQUEST;
364     uint16_t i;
365     uint16_t offset = 1;
366     for (i=0;i<num_value_handles;i++){
367         little_endian_store_16(request, offset, value_handles[i]);
368         offset += 2;
369     }
370 
371     return gatt_client_send(gatt_client, offset);
372 }
373 
374 #ifdef ENABLE_LE_SIGNED_WRITE
375 // precondition: can_send_packet_now == TRUE
376 static uint8_t att_signed_write_request(gatt_client_t *gatt_client, uint16_t request_type, uint16_t attribute_handle,
377                                         uint16_t value_length, uint8_t *value, uint32_t sign_counter, uint8_t sgn[8]) {
378     uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
379 
380     request[0] = request_type;
381     little_endian_store_16(request, 1, attribute_handle);
382     (void)memcpy(&request[3], value, value_length);
383     little_endian_store_32(request, 3 + value_length, sign_counter);
384     reverse_64(sgn, &request[3 + value_length + 4]);
385 
386     return gatt_client_send(gatt_client, 3 + value_length + 12);
387 }
388 #endif
389 
390 // precondition: can_send_packet_now == TRUE
391 static uint8_t
392 att_write_request(gatt_client_t *gatt_client, uint8_t request_type, uint16_t attribute_handle, uint16_t value_length,
393                   uint8_t *value) {
394     uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
395 
396     request[0] = request_type;
397     little_endian_store_16(request, 1, attribute_handle);
398     (void)memcpy(&request[3], value, value_length);
399 
400     return gatt_client_send(gatt_client, 3u + value_length);
401 }
402 
403 // precondition: can_send_packet_now == TRUE
404 static uint8_t att_execute_write_request(gatt_client_t *gatt_client, uint8_t request_type, uint8_t execute_write) {
405     uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
406 
407     request[0] = request_type;
408     request[1] = execute_write;
409 
410     return gatt_client_send(gatt_client, 2);
411 }
412 
413 // precondition: can_send_packet_now == TRUE
414 static uint8_t att_prepare_write_request(gatt_client_t *gatt_client, uint8_t request_type, uint16_t attribute_handle,
415                                          uint16_t value_offset, uint16_t blob_length, uint8_t *value) {
416     uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
417 
418     request[0] = request_type;
419     little_endian_store_16(request, 1, attribute_handle);
420     little_endian_store_16(request, 3, value_offset);
421     (void)memcpy(&request[5], &value[value_offset], blob_length);
422 
423     return gatt_client_send(gatt_client,  5u + blob_length);
424 }
425 
426 static uint8_t att_exchange_mtu_request(gatt_client_t *gatt_client) {
427     uint8_t *request = gatt_client_reserve_request_buffer(gatt_client);
428 
429     request[0] = ATT_EXCHANGE_MTU_REQUEST;
430     uint16_t mtu = l2cap_max_le_mtu();
431     little_endian_store_16(request, 1, mtu);
432 
433     return gatt_client_send(gatt_client, 3);
434 }
435 
436 static uint16_t write_blob_length(gatt_client_t * gatt_client){
437     uint16_t max_blob_length = gatt_client->mtu - 5u;
438     if (gatt_client->attribute_offset >= gatt_client->attribute_length) {
439         return 0;
440     }
441     uint16_t rest_length = gatt_client->attribute_length - gatt_client->attribute_offset;
442     if (max_blob_length > rest_length){
443         return rest_length;
444     }
445     return max_blob_length;
446 }
447 
448 static void send_gatt_services_request(gatt_client_t *gatt_client){
449     att_read_by_type_or_group_request_for_uuid16(gatt_client, ATT_READ_BY_GROUP_TYPE_REQUEST,
450                                                  gatt_client->uuid16, gatt_client->start_group_handle,
451                                                  gatt_client->end_group_handle);
452 }
453 
454 static void send_gatt_by_uuid_request(gatt_client_t *gatt_client, uint16_t attribute_group_type){
455     if (gatt_client->uuid16){
456         uint8_t uuid16[2];
457         little_endian_store_16(uuid16, 0, gatt_client->uuid16);
458         att_find_by_type_value_request(gatt_client, ATT_FIND_BY_TYPE_VALUE_REQUEST, attribute_group_type,
459                                        gatt_client->start_group_handle, gatt_client->end_group_handle, uuid16, 2);
460         return;
461     }
462     uint8_t uuid128[16];
463     reverse_128(gatt_client->uuid128, uuid128);
464     att_find_by_type_value_request(gatt_client, ATT_FIND_BY_TYPE_VALUE_REQUEST, attribute_group_type,
465                                    gatt_client->start_group_handle, gatt_client->end_group_handle, uuid128, 16);
466 }
467 
468 static void send_gatt_services_by_uuid_request(gatt_client_t *gatt_client){
469     send_gatt_by_uuid_request(gatt_client, GATT_PRIMARY_SERVICE_UUID);
470 }
471 
472 static void send_gatt_included_service_uuid_request(gatt_client_t *gatt_client){
473     att_read_request(gatt_client, ATT_READ_REQUEST, gatt_client->query_start_handle);
474 }
475 
476 static void send_gatt_included_service_request(gatt_client_t *gatt_client){
477     att_read_by_type_or_group_request_for_uuid16(gatt_client, ATT_READ_BY_TYPE_REQUEST,
478                                                  GATT_INCLUDE_SERVICE_UUID, gatt_client->start_group_handle,
479                                                  gatt_client->end_group_handle);
480 }
481 
482 static void send_gatt_characteristic_request(gatt_client_t *gatt_client){
483     att_read_by_type_or_group_request_for_uuid16(gatt_client, ATT_READ_BY_TYPE_REQUEST,
484                                                  GATT_CHARACTERISTICS_UUID, gatt_client->start_group_handle,
485                                                  gatt_client->end_group_handle);
486 }
487 
488 static void send_gatt_characteristic_descriptor_request(gatt_client_t *gatt_client){
489     att_find_information_request(gatt_client, ATT_FIND_INFORMATION_REQUEST, gatt_client->start_group_handle,
490                                  gatt_client->end_group_handle);
491 }
492 
493 static void send_gatt_read_characteristic_value_request(gatt_client_t *gatt_client){
494     att_read_request(gatt_client, ATT_READ_REQUEST, gatt_client->attribute_handle);
495 }
496 
497 static void send_gatt_read_by_type_request(gatt_client_t * gatt_client){
498     if (gatt_client->uuid16){
499         att_read_by_type_or_group_request_for_uuid16(gatt_client, ATT_READ_BY_TYPE_REQUEST,
500                                                      gatt_client->uuid16, gatt_client->start_group_handle,
501                                                      gatt_client->end_group_handle);
502     } else {
503         att_read_by_type_or_group_request_for_uuid128(gatt_client, ATT_READ_BY_TYPE_REQUEST,
504                                                       gatt_client->uuid128, gatt_client->start_group_handle,
505                                                       gatt_client->end_group_handle);
506     }
507 }
508 
509 static void send_gatt_read_blob_request(gatt_client_t *gatt_client){
510     if (gatt_client->attribute_offset == 0){
511         att_read_request(gatt_client, ATT_READ_REQUEST, gatt_client->attribute_handle);
512     } else {
513         att_read_blob_request(gatt_client, ATT_READ_BLOB_REQUEST, gatt_client->attribute_handle,
514                               gatt_client->attribute_offset);
515     }
516 }
517 
518 static void send_gatt_read_multiple_request(gatt_client_t * gatt_client){
519     att_read_multiple_request(gatt_client, gatt_client->read_multiple_handle_count, gatt_client->read_multiple_handles);
520 }
521 
522 static void send_gatt_write_attribute_value_request(gatt_client_t * gatt_client){
523     att_write_request(gatt_client, ATT_WRITE_REQUEST, gatt_client->attribute_handle, gatt_client->attribute_length,
524                       gatt_client->attribute_value);
525 }
526 
527 static void send_gatt_write_client_characteristic_configuration_request(gatt_client_t * gatt_client){
528     att_write_request(gatt_client, ATT_WRITE_REQUEST, gatt_client->client_characteristic_configuration_handle, 2,
529                       gatt_client->client_characteristic_configuration_value);
530 }
531 
532 static void send_gatt_prepare_write_request(gatt_client_t * gatt_client){
533     att_prepare_write_request(gatt_client, ATT_PREPARE_WRITE_REQUEST, gatt_client->attribute_handle,
534                               gatt_client->attribute_offset, write_blob_length(gatt_client),
535                               gatt_client->attribute_value);
536 }
537 
538 static void send_gatt_execute_write_request(gatt_client_t * gatt_client){
539     att_execute_write_request(gatt_client, ATT_EXECUTE_WRITE_REQUEST, 1);
540 }
541 
542 static void send_gatt_cancel_prepared_write_request(gatt_client_t * gatt_client){
543     att_execute_write_request(gatt_client, ATT_EXECUTE_WRITE_REQUEST, 0);
544 }
545 
546 #ifndef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY
547 static void send_gatt_read_client_characteristic_configuration_request(gatt_client_t * gatt_client){
548     att_read_by_type_or_group_request_for_uuid16(gatt_client, ATT_READ_BY_TYPE_REQUEST,
549                                                  GATT_CLIENT_CHARACTERISTICS_CONFIGURATION,
550                                                  gatt_client->start_group_handle, gatt_client->end_group_handle);
551 }
552 #endif
553 
554 static void send_gatt_read_characteristic_descriptor_request(gatt_client_t * gatt_client){
555     att_read_request(gatt_client, ATT_READ_REQUEST, gatt_client->attribute_handle);
556 }
557 
558 #ifdef ENABLE_LE_SIGNED_WRITE
559 static void send_gatt_signed_write_request(gatt_client_t * gatt_client, uint32_t sign_counter){
560     att_signed_write_request(gatt_client, ATT_SIGNED_WRITE_COMMAND, gatt_client->attribute_handle,
561                              gatt_client->attribute_length, gatt_client->attribute_value, sign_counter,
562                              gatt_client->cmac);
563 }
564 #endif
565 
566 static uint16_t get_last_result_handle_from_service_list(uint8_t * packet, uint16_t size){
567     if (size < 2) return 0xffff;
568     uint8_t attr_length = packet[1];
569     if ((2 + attr_length) > size) return 0xffff;
570     return little_endian_read_16(packet, size - attr_length + 2u);
571 }
572 
573 static uint16_t get_last_result_handle_from_characteristics_list(uint8_t * packet, uint16_t size){
574     if (size < 2) return 0xffff;
575     uint8_t attr_length = packet[1];
576     if ((2 + attr_length) > size) return 0xffff;
577     return little_endian_read_16(packet, size - attr_length + 3u);
578 }
579 
580 static uint16_t get_last_result_handle_from_included_services_list(uint8_t * packet, uint16_t size){
581     if (size < 2) return 0xffff;
582     uint8_t attr_length = packet[1];
583     if ((2 + attr_length) > size) return 0xffff;
584     return little_endian_read_16(packet, size - attr_length);
585 }
586 
587 static void gatt_client_notify_can_send_query(gatt_client_t * gatt_client){
588     while (gatt_client->gatt_client_state == P_READY){
589         btstack_context_callback_registration_t * callback = (btstack_context_callback_registration_t *) btstack_linked_list_pop(&gatt_client->query_requests);
590         if (callback == NULL) {
591             return;
592         }
593         (*callback->callback)(callback->context);
594     }
595 }
596 
597 static void gatt_client_handle_transaction_complete(gatt_client_t * gatt_client){
598     gatt_client->gatt_client_state = P_READY;
599     gatt_client_timeout_stop(gatt_client);
600     gatt_client_notify_can_send_query(gatt_client);
601 }
602 
603 static void emit_event_new(btstack_packet_handler_t callback, uint8_t * packet, uint16_t size){
604     if (!callback) return;
605     hci_dump_packet(HCI_EVENT_PACKET, 1, packet, size);
606     (*callback)(HCI_EVENT_PACKET, 0, packet, size);
607 }
608 
609 void gatt_client_listen_for_characteristic_value_updates(gatt_client_notification_t * notification, btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic){
610     notification->callback = callback;
611     notification->con_handle = con_handle;
612     if (characteristic == NULL){
613         notification->attribute_handle = GATT_CLIENT_ANY_VALUE_HANDLE;
614     } else {
615         notification->attribute_handle = characteristic->value_handle;
616     }
617     btstack_linked_list_add(&gatt_client_value_listeners, (btstack_linked_item_t*) notification);
618 }
619 
620 void gatt_client_stop_listening_for_characteristic_value_updates(gatt_client_notification_t * notification){
621     btstack_linked_list_remove(&gatt_client_value_listeners, (btstack_linked_item_t*) notification);
622 }
623 
624 static void emit_event_to_registered_listeners(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t * packet, uint16_t size){
625     btstack_linked_list_iterator_t it;
626     btstack_linked_list_iterator_init(&it, &gatt_client_value_listeners);
627     while (btstack_linked_list_iterator_has_next(&it)){
628         gatt_client_notification_t * notification = (gatt_client_notification_t*) btstack_linked_list_iterator_next(&it);
629         if ((notification->con_handle       != GATT_CLIENT_ANY_CONNECTION)   && (notification->con_handle       != con_handle)) continue;
630         if ((notification->attribute_handle != GATT_CLIENT_ANY_VALUE_HANDLE) && (notification->attribute_handle != attribute_handle)) continue;
631         (*notification->callback)(HCI_EVENT_PACKET, 0, packet, size);
632     }
633 }
634 
635 static void emit_gatt_complete_event(gatt_client_t * gatt_client, uint8_t att_status){
636     // @format H1
637     uint8_t packet[5];
638     packet[0] = GATT_EVENT_QUERY_COMPLETE;
639     packet[1] = 3;
640     little_endian_store_16(packet, 2, gatt_client->con_handle);
641     packet[4] = att_status;
642     emit_event_new(gatt_client->callback, packet, sizeof(packet));
643 }
644 
645 static void emit_gatt_service_query_result_event(gatt_client_t * gatt_client, uint16_t start_group_handle, uint16_t end_group_handle, const uint8_t * uuid128){
646     // @format HX
647     uint8_t packet[24];
648     packet[0] = GATT_EVENT_SERVICE_QUERY_RESULT;
649     packet[1] = sizeof(packet) - 2u;
650     little_endian_store_16(packet, 2, gatt_client->con_handle);
651     ///
652     little_endian_store_16(packet, 4, start_group_handle);
653     little_endian_store_16(packet, 6, end_group_handle);
654     reverse_128(uuid128, &packet[8]);
655     emit_event_new(gatt_client->callback, packet, sizeof(packet));
656 }
657 
658 static void emit_gatt_included_service_query_result_event(gatt_client_t * gatt_client, uint16_t include_handle, uint16_t start_group_handle, uint16_t end_group_handle, const uint8_t * uuid128){
659     // @format HX
660     uint8_t packet[26];
661     packet[0] = GATT_EVENT_INCLUDED_SERVICE_QUERY_RESULT;
662     packet[1] = sizeof(packet) - 2u;
663     little_endian_store_16(packet, 2, gatt_client->con_handle);
664     ///
665     little_endian_store_16(packet, 4, include_handle);
666     //
667     little_endian_store_16(packet, 6, start_group_handle);
668     little_endian_store_16(packet, 8, end_group_handle);
669     reverse_128(uuid128, &packet[10]);
670     emit_event_new(gatt_client->callback, packet, sizeof(packet));
671 }
672 
673 static void emit_gatt_characteristic_query_result_event(gatt_client_t * gatt_client, uint16_t start_handle, uint16_t value_handle, uint16_t end_handle,
674                                                         uint16_t properties, const uint8_t * uuid128){
675     // @format HY
676     uint8_t packet[28];
677     packet[0] = GATT_EVENT_CHARACTERISTIC_QUERY_RESULT;
678     packet[1] = sizeof(packet) - 2u;
679     little_endian_store_16(packet, 2, gatt_client->con_handle);
680     ///
681     little_endian_store_16(packet, 4,  start_handle);
682     little_endian_store_16(packet, 6,  value_handle);
683     little_endian_store_16(packet, 8,  end_handle);
684     little_endian_store_16(packet, 10, properties);
685     reverse_128(uuid128, &packet[12]);
686     emit_event_new(gatt_client->callback, packet, sizeof(packet));
687 }
688 
689 static void emit_gatt_all_characteristic_descriptors_result_event(
690         gatt_client_t * gatt_client, uint16_t descriptor_handle, const uint8_t * uuid128){
691     // @format HZ
692     uint8_t packet[22];
693     packet[0] = GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT;
694     packet[1] = sizeof(packet) - 2u;
695     little_endian_store_16(packet, 2, gatt_client->con_handle);
696     ///
697     little_endian_store_16(packet, 4,  descriptor_handle);
698     reverse_128(uuid128, &packet[6]);
699     emit_event_new(gatt_client->callback, packet, sizeof(packet));
700 }
701 
702 static void emit_gatt_mtu_exchanged_result_event(gatt_client_t * gatt_client, uint16_t new_mtu){
703     // @format H2
704     uint8_t packet[6];
705     packet[0] = GATT_EVENT_MTU;
706     packet[1] = sizeof(packet) - 2u;
707     little_endian_store_16(packet, 2, gatt_client->con_handle);
708     little_endian_store_16(packet, 4, new_mtu);
709     att_dispatch_client_mtu_exchanged(gatt_client->con_handle, new_mtu);
710     emit_event_new(gatt_client->callback, packet, sizeof(packet));
711 }
712 ///
713 static void report_gatt_services(gatt_client_t * gatt_client, uint8_t * packet, uint16_t size){
714     if (size < 2) return;
715     uint8_t attr_length = packet[1];
716     uint8_t uuid_length = attr_length - 4u;
717 
718     int i;
719     for (i = 2; (i+attr_length) <= size; i += attr_length){
720         uint16_t start_group_handle = little_endian_read_16(packet,i);
721         uint16_t end_group_handle   = little_endian_read_16(packet,i+2);
722         uint8_t  uuid128[16];
723         uint16_t uuid16 = 0;
724 
725         if (uuid_length == 2u){
726             uuid16 = little_endian_read_16(packet, i+4);
727             uuid_add_bluetooth_prefix((uint8_t*) &uuid128, uuid16);
728         } else if (uuid_length == 16u) {
729             reverse_128(&packet[i+4], uuid128);
730         } else {
731             return;
732         }
733         emit_gatt_service_query_result_event(gatt_client, start_group_handle, end_group_handle, uuid128);
734     }
735 }
736 
737 // helper
738 static void characteristic_start_found(gatt_client_t * gatt_client, uint16_t start_handle, uint8_t properties, uint16_t value_handle, uint8_t * uuid, uint16_t uuid_length){
739     uint8_t uuid128[16];
740     uint16_t uuid16 = 0;
741     if (uuid_length == 2u){
742         uuid16 = little_endian_read_16(uuid, 0);
743         uuid_add_bluetooth_prefix((uint8_t*) uuid128, uuid16);
744     } else if (uuid_length == 16u){
745         reverse_128(uuid, uuid128);
746     } else {
747         return;
748     }
749 
750     if (gatt_client->filter_with_uuid && (memcmp(gatt_client->uuid128, uuid128, 16) != 0)) return;
751 
752     gatt_client->characteristic_properties = properties;
753     gatt_client->characteristic_start_handle = start_handle;
754     gatt_client->attribute_handle = value_handle;
755 
756     if (gatt_client->filter_with_uuid) return;
757 
758     gatt_client->uuid16 = uuid16;
759     (void)memcpy(gatt_client->uuid128, uuid128, 16);
760 }
761 
762 static void characteristic_end_found(gatt_client_t * gatt_client, uint16_t end_handle){
763     // TODO: stop searching if filter and uuid found
764 
765     if (!gatt_client->characteristic_start_handle) return;
766 
767     emit_gatt_characteristic_query_result_event(gatt_client, gatt_client->characteristic_start_handle, gatt_client->attribute_handle,
768                                                 end_handle, gatt_client->characteristic_properties, gatt_client->uuid128);
769 
770     gatt_client->characteristic_start_handle = 0;
771 }
772 
773 static void report_gatt_characteristics(gatt_client_t * gatt_client, uint8_t * packet, uint16_t size){
774     if (size < 2u) return;
775     uint8_t attr_length = packet[1];
776     if ((attr_length != 7u) && (attr_length != 21u)) return;
777     uint8_t uuid_length = attr_length - 5u;
778     int i;
779     for (i = 2u; (i + attr_length) <= size; i += attr_length){
780         uint16_t start_handle = little_endian_read_16(packet, i);
781         uint8_t  properties = packet[i+2];
782         uint16_t value_handle = little_endian_read_16(packet, i+3);
783         characteristic_end_found(gatt_client, start_handle - 1u);
784         characteristic_start_found(gatt_client, start_handle, properties, value_handle, &packet[i + 5], uuid_length);
785     }
786 }
787 
788 static void report_gatt_included_service_uuid16(gatt_client_t * gatt_client, uint16_t include_handle, uint16_t uuid16){
789     uint8_t normalized_uuid128[16];
790     uuid_add_bluetooth_prefix(normalized_uuid128, uuid16);
791     emit_gatt_included_service_query_result_event(gatt_client, include_handle, gatt_client->query_start_handle,
792                                                   gatt_client->query_end_handle, normalized_uuid128);
793 }
794 
795 static void report_gatt_included_service_uuid128(gatt_client_t * gatt_client, uint16_t include_handle, const uint8_t * uuid128){
796     emit_gatt_included_service_query_result_event(gatt_client, include_handle, gatt_client->query_start_handle,
797                                                   gatt_client->query_end_handle, uuid128);
798 }
799 
800 // @return packet pointer
801 // @note assume that value is part of an l2cap buffer - overwrite HCI + L2CAP packet headers
802 static const int characteristic_value_event_header_size = 8;
803 static uint8_t * setup_characteristic_value_packet(uint8_t type, hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t * value, uint16_t length){
804 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
805     // copy value into test packet for testing
806     static uint8_t packet[1000];
807     memcpy(&packet[8], value, length);
808 #else
809     // before the value inside the ATT PDU
810     uint8_t * packet = value - characteristic_value_event_header_size;
811 #endif
812     packet[0] = type;
813     packet[1] = characteristic_value_event_header_size - 2 + length;
814     little_endian_store_16(packet, 2, con_handle);
815     little_endian_store_16(packet, 4, attribute_handle);
816     little_endian_store_16(packet, 6, length);
817     return packet;
818 }
819 
820 // @return packet pointer
821 // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes
822 static const int long_characteristic_value_event_header_size = 10;
823 static uint8_t * setup_long_characteristic_value_packet(uint8_t type, hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t offset, uint8_t * value, uint16_t length){
824 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
825     // avoid using pre ATT headers.
826     return NULL;
827 #endif
828 #if defined(HCI_INCOMING_PRE_BUFFER_SIZE) && (HCI_INCOMING_PRE_BUFFER_SIZE >= 10 - 8) // L2CAP Header (4) - ACL Header (4)
829     // before the value inside the ATT PDU
830     uint8_t * packet = value - long_characteristic_value_event_header_size;
831     packet[0] = type;
832     packet[1] = long_characteristic_value_event_header_size - 2 + length;
833     little_endian_store_16(packet, 2, con_handle);
834     little_endian_store_16(packet, 4, attribute_handle);
835     little_endian_store_16(packet, 6, offset);
836     little_endian_store_16(packet, 8, length);
837     return packet;
838 #else
839     log_error("HCI_INCOMING_PRE_BUFFER_SIZE >= 2 required for long characteristic reads");
840     return NULL;
841 #endif
842 }
843 
844 // test if notification/indication should be delivered to application (BLESA)
845 static bool gatt_client_accept_server_message(gatt_client_t *gatt_client) {
846 #ifdef ENABLE_LE_PROACTIVE_AUTHENTICATION
847 	// ignore messages until re-encryption is complete
848     if (gap_reconnect_security_setup_active(gatt_client->con_handle)) return false;
849 
850 	// after that ignore if bonded but not encrypted
851 	return !gap_bonded(gatt_client->con_handle) || (gap_encryption_key_size(gatt_client->con_handle) > 0);
852 #else
853     UNUSED(gatt_client);
854 	return true;
855 #endif
856 }
857 
858 
859 // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes
860 static void report_gatt_notification(gatt_client_t *gatt_client, uint16_t value_handle, uint8_t *value, int length) {
861 	if (!gatt_client_accept_server_message(gatt_client)) return;
862     uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_NOTIFICATION, gatt_client->con_handle, value_handle, value, length);
863     emit_event_to_registered_listeners(gatt_client->con_handle, value_handle, packet, characteristic_value_event_header_size + length);
864 }
865 
866 // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes
867 static void report_gatt_indication(gatt_client_t *gatt_client, uint16_t value_handle, uint8_t *value, int length) {
868 	if (!gatt_client_accept_server_message(gatt_client)) return;
869     uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_INDICATION, gatt_client->con_handle, value_handle, value, length);
870     emit_event_to_registered_listeners(gatt_client->con_handle, value_handle, packet, characteristic_value_event_header_size + length);
871 }
872 
873 // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes
874 static void report_gatt_characteristic_value(gatt_client_t * gatt_client, uint16_t attribute_handle, uint8_t * value, uint16_t length){
875     uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT, gatt_client->con_handle, attribute_handle, value, length);
876     emit_event_new(gatt_client->callback, packet, characteristic_value_event_header_size + length);
877 }
878 
879 // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes
880 static void report_gatt_long_characteristic_value_blob(gatt_client_t * gatt_client, uint16_t attribute_handle, uint8_t * blob, uint16_t blob_length, int value_offset){
881     uint8_t * packet = setup_long_characteristic_value_packet(GATT_EVENT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT, gatt_client->con_handle, attribute_handle, value_offset, blob, blob_length);
882     if (!packet) return;
883     emit_event_new(gatt_client->callback, packet, blob_length + long_characteristic_value_event_header_size);
884 }
885 
886 static void report_gatt_characteristic_descriptor(gatt_client_t * gatt_client, uint16_t descriptor_handle, uint8_t *value, uint16_t value_length, uint16_t value_offset){
887     UNUSED(value_offset);
888     uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT, gatt_client->con_handle, descriptor_handle, value, value_length);
889     emit_event_new(gatt_client->callback, packet, value_length + 8u);
890 }
891 
892 static void report_gatt_long_characteristic_descriptor(gatt_client_t * gatt_client, uint16_t descriptor_handle, uint8_t *blob, uint16_t blob_length, uint16_t value_offset){
893     uint8_t * packet = setup_long_characteristic_value_packet(GATT_EVENT_LONG_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT, gatt_client->con_handle, descriptor_handle, value_offset, blob, blob_length);
894     if (!packet) return;
895     emit_event_new(gatt_client->callback, packet, blob_length + long_characteristic_value_event_header_size);
896 }
897 
898 static void report_gatt_all_characteristic_descriptors(gatt_client_t * gatt_client, uint8_t * packet, uint16_t size, uint16_t pair_size){
899     int i;
900     for (i = 0u; (i + pair_size) <= size; i += pair_size){
901         uint16_t descriptor_handle = little_endian_read_16(packet,i);
902         uint8_t uuid128[16];
903         uint16_t uuid16 = 0;
904         if (pair_size == 4u){
905             uuid16 = little_endian_read_16(packet,i+2);
906             uuid_add_bluetooth_prefix(uuid128, uuid16);
907         } else {
908             reverse_128(&packet[i+2], uuid128);
909         }
910         emit_gatt_all_characteristic_descriptors_result_event(gatt_client, descriptor_handle, uuid128);
911     }
912 
913 }
914 
915 static int is_query_done(gatt_client_t * gatt_client, uint16_t last_result_handle){
916     return last_result_handle >= gatt_client->end_group_handle;
917 }
918 
919 static void trigger_next_query(gatt_client_t * gatt_client, uint16_t last_result_handle, gatt_client_state_t next_query_state){
920     if (is_query_done(gatt_client, last_result_handle)){
921         gatt_client_handle_transaction_complete(gatt_client);
922         emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
923         return;
924     }
925     // next
926     gatt_client->start_group_handle = last_result_handle + 1u;
927     gatt_client->gatt_client_state = next_query_state;
928 }
929 
930 static void trigger_next_included_service_query(gatt_client_t * gatt_client, uint16_t last_result_handle){
931     trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_INCLUDED_SERVICE_QUERY);
932 }
933 
934 static void trigger_next_service_query(gatt_client_t * gatt_client, uint16_t last_result_handle){
935     trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_SERVICE_QUERY);
936 }
937 
938 static void trigger_next_service_by_uuid_query(gatt_client_t * gatt_client, uint16_t last_result_handle){
939     trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_SERVICE_WITH_UUID_QUERY);
940 }
941 
942 static void trigger_next_characteristic_query(gatt_client_t * gatt_client, uint16_t last_result_handle){
943     if (is_query_done(gatt_client, last_result_handle)){
944         // report last characteristic
945         characteristic_end_found(gatt_client, gatt_client->end_group_handle);
946     }
947     trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY);
948 }
949 
950 static void trigger_next_characteristic_descriptor_query(gatt_client_t * gatt_client, uint16_t last_result_handle){
951     trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY);
952 }
953 
954 static void trigger_next_read_by_type_query(gatt_client_t * gatt_client, uint16_t last_result_handle){
955     trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_READ_BY_TYPE_REQUEST);
956 }
957 
958 static void trigger_next_prepare_write_query(gatt_client_t * gatt_client, gatt_client_state_t next_query_state, gatt_client_state_t done_state){
959     gatt_client->attribute_offset += write_blob_length(gatt_client);
960     uint16_t next_blob_length =  write_blob_length(gatt_client);
961 
962     if (next_blob_length == 0u){
963         gatt_client->gatt_client_state = done_state;
964         return;
965     }
966     gatt_client->gatt_client_state = next_query_state;
967 }
968 
969 static void trigger_next_blob_query(gatt_client_t * gatt_client, gatt_client_state_t next_query_state, uint16_t received_blob_length){
970 
971     uint16_t max_blob_length = gatt_client->mtu - 1u;
972     if (received_blob_length < max_blob_length){
973         gatt_client_handle_transaction_complete(gatt_client);
974         emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
975         return;
976     }
977 
978     gatt_client->attribute_offset += received_blob_length;
979     gatt_client->gatt_client_state = next_query_state;
980 }
981 
982 
983 static int is_value_valid(gatt_client_t *gatt_client, uint8_t *packet, uint16_t size){
984     uint16_t attribute_handle = little_endian_read_16(packet, 1);
985     uint16_t value_offset = little_endian_read_16(packet, 3);
986 
987     if (gatt_client->attribute_handle != attribute_handle) return 0;
988     if (gatt_client->attribute_offset != value_offset) return 0;
989     return memcmp(&gatt_client->attribute_value[gatt_client->attribute_offset], &packet[5], size - 5u) == 0u;
990 }
991 
992 #ifdef ENABLE_LE_SIGNED_WRITE
993 static void gatt_client_run_for_client_start_signed_write(gatt_client_t *gatt_client) {
994     sm_key_t csrk;
995     le_device_db_local_csrk_get(gatt_client->le_device_index, csrk);
996     uint32_t sign_counter = le_device_db_local_counter_get(gatt_client->le_device_index);
997     gatt_client->gatt_client_state = P_W4_CMAC_RESULT;
998     sm_cmac_signed_write_start(csrk, ATT_SIGNED_WRITE_COMMAND, gatt_client->attribute_handle, gatt_client->attribute_length, gatt_client->attribute_value, sign_counter, att_signed_write_handle_cmac_result);
999 }
1000 #endif
1001 
1002 // returns true if packet was sent
1003 static bool gatt_client_run_for_gatt_client(gatt_client_t * gatt_client){
1004 
1005     // wait until re-encryption is complete
1006     if (gap_reconnect_security_setup_active(gatt_client->con_handle)) return false;
1007 
1008     // wait until re-encryption is complete
1009     if (gatt_client->reencryption_active) return false;
1010 
1011     // wait until pairing complete (either reactive authentication or due to required security level)
1012     if (gatt_client->wait_for_authentication_complete) return false;
1013 
1014     bool client_request_pending = gatt_client->gatt_client_state != P_READY;
1015 
1016     // verify security level for Mandatory Authentication over LE
1017     bool check_security;
1018     switch (gatt_client->bearer_type){
1019         case ATT_BEARER_UNENHANCED_LE:
1020             check_security = true;
1021             break;
1022         default:
1023             check_security = false;
1024             break;
1025     }
1026     if (client_request_pending && (gatt_client_required_security_level > gatt_client->security_level) && check_security){
1027         log_info("Trigger pairing, current security level %u, required %u\n", gatt_client->security_level, gatt_client_required_security_level);
1028         gatt_client->wait_for_authentication_complete = 1;
1029         // set att error code for pairing failure based on required level
1030         switch (gatt_client_required_security_level){
1031             case LEVEL_4:
1032             case LEVEL_3:
1033                 gatt_client->pending_error_code = ATT_ERROR_INSUFFICIENT_AUTHENTICATION;
1034                 break;
1035             default:
1036                 gatt_client->pending_error_code = ATT_ERROR_INSUFFICIENT_ENCRYPTION;
1037                 break;
1038         }
1039         sm_request_pairing(gatt_client->con_handle);
1040         // sm probably just sent a pdu
1041         return true;
1042     }
1043 
1044     switch (gatt_client->mtu_state) {
1045         case SEND_MTU_EXCHANGE:
1046             gatt_client->mtu_state = SENT_MTU_EXCHANGE;
1047             att_exchange_mtu_request(gatt_client);
1048             return true;
1049         case SENT_MTU_EXCHANGE:
1050             return false;
1051         default:
1052             break;
1053     }
1054 
1055     if (gatt_client->send_confirmation){
1056         gatt_client->send_confirmation = 0;
1057         att_confirmation(gatt_client);
1058         return true;
1059     }
1060 
1061     // check MTU for writes
1062     switch (gatt_client->gatt_client_state){
1063         case P_W2_SEND_WRITE_CHARACTERISTIC_VALUE:
1064         case P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR:
1065             if (gatt_client->attribute_length <= (gatt_client->mtu - 3u)) break;
1066             log_error("gatt_client_run: value len %u > MTU %u - 3\n", gatt_client->attribute_length,gatt_client->mtu);
1067             gatt_client_handle_transaction_complete(gatt_client);
1068             emit_gatt_complete_event(gatt_client, ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LENGTH);
1069             return false;
1070         default:
1071             break;
1072     }
1073 
1074     bool packet_sent = true;
1075     bool done = true;
1076     switch (gatt_client->gatt_client_state){
1077         case P_W2_SEND_SERVICE_QUERY:
1078             gatt_client->gatt_client_state = P_W4_SERVICE_QUERY_RESULT;
1079             send_gatt_services_request(gatt_client);
1080             break;
1081 
1082         case P_W2_SEND_SERVICE_WITH_UUID_QUERY:
1083             gatt_client->gatt_client_state = P_W4_SERVICE_WITH_UUID_RESULT;
1084             send_gatt_services_by_uuid_request(gatt_client);
1085             break;
1086 
1087         case P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY:
1088             gatt_client->gatt_client_state = P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT;
1089             send_gatt_characteristic_request(gatt_client);
1090             break;
1091 
1092         case P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY:
1093             gatt_client->gatt_client_state = P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT;
1094             send_gatt_characteristic_request(gatt_client);
1095             break;
1096 
1097         case P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY:
1098             gatt_client->gatt_client_state = P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT;
1099             send_gatt_characteristic_descriptor_request(gatt_client);
1100             break;
1101 
1102         case P_W2_SEND_INCLUDED_SERVICE_QUERY:
1103             gatt_client->gatt_client_state = P_W4_INCLUDED_SERVICE_QUERY_RESULT;
1104             send_gatt_included_service_request(gatt_client);
1105             break;
1106 
1107         case P_W2_SEND_INCLUDED_SERVICE_WITH_UUID_QUERY:
1108             gatt_client->gatt_client_state = P_W4_INCLUDED_SERVICE_UUID_WITH_QUERY_RESULT;
1109             send_gatt_included_service_uuid_request(gatt_client);
1110             break;
1111 
1112         case P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY:
1113             gatt_client->gatt_client_state = P_W4_READ_CHARACTERISTIC_VALUE_RESULT;
1114             send_gatt_read_characteristic_value_request(gatt_client);
1115             break;
1116 
1117         case P_W2_SEND_READ_BLOB_QUERY:
1118             gatt_client->gatt_client_state = P_W4_READ_BLOB_RESULT;
1119             send_gatt_read_blob_request(gatt_client);
1120             break;
1121 
1122         case P_W2_SEND_READ_BY_TYPE_REQUEST:
1123             gatt_client->gatt_client_state = P_W4_READ_BY_TYPE_RESPONSE;
1124             send_gatt_read_by_type_request(gatt_client);
1125             break;
1126 
1127         case P_W2_SEND_READ_MULTIPLE_REQUEST:
1128             gatt_client->gatt_client_state = P_W4_READ_MULTIPLE_RESPONSE;
1129             send_gatt_read_multiple_request(gatt_client);
1130             break;
1131 
1132         case P_W2_SEND_WRITE_CHARACTERISTIC_VALUE:
1133             gatt_client->gatt_client_state = P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT;
1134             send_gatt_write_attribute_value_request(gatt_client);
1135             break;
1136 
1137         case P_W2_PREPARE_WRITE:
1138             gatt_client->gatt_client_state = P_W4_PREPARE_WRITE_RESULT;
1139             send_gatt_prepare_write_request(gatt_client);
1140             break;
1141 
1142         case P_W2_PREPARE_WRITE_SINGLE:
1143             gatt_client->gatt_client_state = P_W4_PREPARE_WRITE_SINGLE_RESULT;
1144             send_gatt_prepare_write_request(gatt_client);
1145             break;
1146 
1147         case P_W2_PREPARE_RELIABLE_WRITE:
1148             gatt_client->gatt_client_state = P_W4_PREPARE_RELIABLE_WRITE_RESULT;
1149             send_gatt_prepare_write_request(gatt_client);
1150             break;
1151 
1152         case P_W2_EXECUTE_PREPARED_WRITE:
1153             gatt_client->gatt_client_state = P_W4_EXECUTE_PREPARED_WRITE_RESULT;
1154             send_gatt_execute_write_request(gatt_client);
1155             break;
1156 
1157         case P_W2_CANCEL_PREPARED_WRITE:
1158             gatt_client->gatt_client_state = P_W4_CANCEL_PREPARED_WRITE_RESULT;
1159             send_gatt_cancel_prepared_write_request(gatt_client);
1160             break;
1161 
1162         case P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH:
1163             gatt_client->gatt_client_state = P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT;
1164             send_gatt_cancel_prepared_write_request(gatt_client);
1165             break;
1166 
1167 #ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY
1168         case P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY:
1169             // use Find Information
1170             gatt_client->gatt_client_state = P_W4_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT;
1171             send_gatt_characteristic_descriptor_request(gatt_client);
1172 #else
1173         case P_W2_SEND_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY:
1174             // Use Read By Type
1175             gatt_client->gatt_client_state = P_W4_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT;
1176             send_gatt_read_client_characteristic_configuration_request(gatt_client);
1177 #endif
1178             break;
1179 
1180         case P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY:
1181             gatt_client->gatt_client_state = P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT;
1182             send_gatt_read_characteristic_descriptor_request(gatt_client);
1183             break;
1184 
1185         case P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY:
1186             gatt_client->gatt_client_state = P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT;
1187             send_gatt_read_blob_request(gatt_client);
1188             break;
1189 
1190         case P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR:
1191             gatt_client->gatt_client_state = P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT;
1192             send_gatt_write_attribute_value_request(gatt_client);
1193             break;
1194 
1195         case P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION:
1196             gatt_client->gatt_client_state = P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT;
1197             send_gatt_write_client_characteristic_configuration_request(gatt_client);
1198             break;
1199 
1200         case P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR:
1201             gatt_client->gatt_client_state = P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT;
1202             send_gatt_prepare_write_request(gatt_client);
1203             break;
1204 
1205         case P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR:
1206             gatt_client->gatt_client_state = P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT;
1207             send_gatt_execute_write_request(gatt_client);
1208             break;
1209 
1210 #ifdef ENABLE_LE_SIGNED_WRITE
1211         case P_W4_IDENTITY_RESOLVING:
1212             log_info("P_W4_IDENTITY_RESOLVING - state %x", sm_identity_resolving_state(gatt_client->con_handle));
1213             switch (sm_identity_resolving_state(gatt_client->con_handle)){
1214                 case IRK_LOOKUP_SUCCEEDED:
1215                     gatt_client->le_device_index = sm_le_device_index(gatt_client->con_handle);
1216                     gatt_client->gatt_client_state = P_W4_CMAC_READY;
1217                     if (sm_cmac_ready()){
1218                         gatt_client_run_for_client_start_signed_write(gatt_client);
1219                     }
1220                     break;
1221                 case IRK_LOOKUP_FAILED:
1222                     gatt_client_handle_transaction_complete(gatt_client);
1223                     emit_gatt_complete_event(gatt_client, ATT_ERROR_BONDING_INFORMATION_MISSING);
1224                     break;
1225                 default:
1226                     break;
1227             }
1228             packet_sent = false;
1229             break;
1230 
1231         case P_W4_CMAC_READY:
1232             if (sm_cmac_ready()){
1233                 gatt_client_run_for_client_start_signed_write(gatt_client);
1234             }
1235             packet_sent = false;
1236             break;
1237 
1238         case P_W2_SEND_SIGNED_WRITE: {
1239             gatt_client->gatt_client_state = P_W4_SEND_SINGED_WRITE_DONE;
1240             // bump local signing counter
1241             uint32_t sign_counter = le_device_db_local_counter_get(gatt_client->le_device_index);
1242             le_device_db_local_counter_set(gatt_client->le_device_index, sign_counter + 1);
1243             // send signed write command
1244             send_gatt_signed_write_request(gatt_client, sign_counter);
1245             // finally, notifiy client that write is complete
1246             gatt_client_handle_transaction_complete(gatt_client);
1247             emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1248             break;
1249         }
1250 #endif
1251         default:
1252             done = false;
1253             break;
1254     }
1255 
1256     if (done){
1257         return packet_sent;
1258     }
1259 
1260     // write without response callback
1261     btstack_context_callback_registration_t * callback =
1262             (btstack_context_callback_registration_t *) btstack_linked_list_pop(&gatt_client->write_without_response_requests);
1263     if (callback != NULL){
1264         (*callback->callback)(callback->context);
1265         return true;
1266     }
1267 
1268     // requested can send now old
1269     if (gatt_client->write_without_response_callback){
1270         btstack_packet_handler_t packet_handler = gatt_client->write_without_response_callback;
1271         gatt_client->write_without_response_callback = NULL;
1272         uint8_t event[4];
1273         event[0] = GATT_EVENT_CAN_WRITE_WITHOUT_RESPONSE;
1274         event[1] = sizeof(event) - 2u;
1275         little_endian_store_16(event, 2, gatt_client->con_handle);
1276         packet_handler(HCI_EVENT_PACKET, gatt_client->con_handle, event, sizeof(event));
1277         return true; // to trigger requeueing (even if higher layer didn't sent)
1278     }
1279 
1280     return false;
1281 }
1282 
1283 static void gatt_client_run(void){
1284     btstack_linked_item_t *it;
1285     for (it = (btstack_linked_item_t *) gatt_client_connections; it != NULL; it = it->next){
1286         gatt_client_t * gatt_client = (gatt_client_t *) it;
1287         switch (gatt_client->bearer_type){
1288             case ATT_BEARER_UNENHANCED_LE:
1289                 if (!att_dispatch_client_can_send_now(gatt_client->con_handle)) {
1290                     att_dispatch_client_request_can_send_now_event(gatt_client->con_handle);
1291                     return;
1292                 }
1293                 bool packet_sent = gatt_client_run_for_gatt_client(gatt_client);
1294                 if (packet_sent){
1295                     // request new permission
1296                     att_dispatch_client_request_can_send_now_event(gatt_client->con_handle);
1297                     // requeue client for fairness and exit
1298                     // note: iterator has become invalid
1299                     btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) gatt_client);
1300                     btstack_linked_list_add_tail(&gatt_client_connections, (btstack_linked_item_t *) gatt_client);
1301                     return;
1302                 }
1303                 break;
1304 #ifdef ENABLE_GATT_OVER_CLASSIC
1305             case ATT_BEARER_UNENHANCED_CLASSIC:
1306                 if (gatt_client->con_handle == HCI_CON_HANDLE_INVALID) {
1307                     continue;
1308                 }
1309 
1310                 // handle GATT over BR/EDR
1311                 if (gatt_client->l2cap_psm != 0){
1312                     if (l2cap_can_send_packet_now(gatt_client->l2cap_cid) == false){
1313                         l2cap_request_can_send_now_event(gatt_client->l2cap_cid);
1314                         return;
1315                     }
1316                     bool packet_sent = gatt_client_run_for_gatt_client(gatt_client);
1317                     if (packet_sent){
1318                         // request new permission
1319                         att_dispatch_client_request_can_send_now_event(gatt_client->con_handle);
1320                         // requeue client for fairness and exit
1321                         // note: iterator has become invalid
1322                         btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) gatt_client);
1323                         btstack_linked_list_add_tail(&gatt_client_connections, (btstack_linked_item_t *) gatt_client);
1324                         return;
1325                     }
1326                 }
1327                 break;
1328 #endif
1329             default:
1330                 btstack_unreachable();
1331                 break;
1332         }
1333 
1334 
1335     }
1336 }
1337 
1338 static void gatt_client_report_error_if_pending(gatt_client_t *gatt_client, uint8_t att_error_code) {
1339     if (is_ready(gatt_client) == 1) return;
1340     gatt_client_handle_transaction_complete(gatt_client);
1341     emit_gatt_complete_event(gatt_client, att_error_code);
1342 }
1343 
1344 static void gatt_client_handle_reencryption_complete(const uint8_t * packet){
1345     hci_con_handle_t con_handle = sm_event_reencryption_complete_get_handle(packet);
1346     gatt_client_t * gatt_client = gatt_client_get_context_for_handle(con_handle);
1347     if (gatt_client == NULL) return;
1348 
1349     // update security level
1350     gatt_client->security_level = gatt_client_le_security_level_for_connection(con_handle);
1351 
1352     gatt_client->reencryption_result = sm_event_reencryption_complete_get_status(packet);
1353     gatt_client->reencryption_active = false;
1354     gatt_client->wait_for_authentication_complete = 0;
1355 
1356     if (gatt_client->gatt_client_state == P_READY) return;
1357 
1358     switch (sm_event_reencryption_complete_get_status(packet)){
1359         case ERROR_CODE_SUCCESS:
1360             log_info("re-encryption success, retry operation");
1361             break;
1362         case ERROR_CODE_AUTHENTICATION_FAILURE:
1363         case ERROR_CODE_PIN_OR_KEY_MISSING:
1364 #if defined(ENABLE_GATT_CLIENT_PAIRING) && !defined(ENABLE_LE_PROACTIVE_AUTHENTICATION)
1365             if (gatt_client_required_security_level == LEVEL_0) {
1366                 // re-encryption failed for reactive authentication with pairing and we have a pending client request
1367                 // => try to resolve it by deleting bonding information if we started pairing before
1368                 // delete bonding information
1369                 int le_device_db_index = sm_le_device_index(gatt_client->con_handle);
1370                 btstack_assert(le_device_db_index >= 0);
1371                 log_info("reactive auth with pairing: delete bonding and start pairing");
1372 #ifdef ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION
1373                 hci_remove_le_device_db_entry_from_resolving_list((uint16_t) le_device_db_index);
1374 #endif
1375                 le_device_db_remove(le_device_db_index);
1376                 // trigger pairing again
1377                 sm_request_pairing(gatt_client->con_handle);
1378                 break;
1379             }
1380 #endif
1381             // report bonding information missing
1382             gatt_client_handle_transaction_complete(gatt_client);
1383             emit_gatt_complete_event(gatt_client, ATT_ERROR_BONDING_INFORMATION_MISSING);
1384             break;
1385         default:
1386             // report bonding information missing
1387             gatt_client_handle_transaction_complete(gatt_client);
1388             emit_gatt_complete_event(gatt_client, gatt_client->pending_error_code);
1389             break;
1390     }
1391 }
1392 
1393 static void gatt_client_handle_disconnection_complete(const uint8_t * packet){
1394     log_info("GATT Client: HCI_EVENT_DISCONNECTION_COMPLETE");
1395     hci_con_handle_t con_handle = little_endian_read_16(packet,3);
1396     gatt_client_t * gatt_client = gatt_client_get_context_for_handle(con_handle);
1397     if (gatt_client == NULL) return;
1398 
1399     gatt_client_report_error_if_pending(gatt_client, ATT_ERROR_HCI_DISCONNECT_RECEIVED);
1400     gatt_client_timeout_stop(gatt_client);
1401     btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) gatt_client);
1402     btstack_memory_gatt_client_free(gatt_client);
1403 }
1404 
1405 static void gatt_client_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1406     UNUSED(channel);    // ok: handling own l2cap events
1407     UNUSED(size);       // ok: there is no channel
1408 
1409     if (packet_type != HCI_EVENT_PACKET) return;
1410 
1411     hci_con_handle_t con_handle;
1412     gatt_client_t * gatt_client;
1413     switch (hci_event_packet_get_type(packet)) {
1414         case HCI_EVENT_DISCONNECTION_COMPLETE:
1415             gatt_client_handle_disconnection_complete(packet);
1416             break;
1417 
1418         // Pairing complete (with/without bonding=storing of pairing information)
1419         case SM_EVENT_PAIRING_COMPLETE:
1420             con_handle = sm_event_pairing_complete_get_handle(packet);
1421             gatt_client = gatt_client_get_context_for_handle(con_handle);
1422             if (gatt_client == NULL) break;
1423 
1424             // update security level
1425             gatt_client->security_level = gatt_client_le_security_level_for_connection(con_handle);
1426 
1427             if (gatt_client->wait_for_authentication_complete){
1428                 gatt_client->wait_for_authentication_complete = 0;
1429                 if (sm_event_pairing_complete_get_status(packet)){
1430                     log_info("pairing failed, report previous error 0x%x", gatt_client->pending_error_code);
1431                     gatt_client_report_error_if_pending(gatt_client, gatt_client->pending_error_code);
1432                 } else {
1433                     log_info("pairing success, retry operation");
1434                 }
1435             }
1436             break;
1437 
1438 #ifdef ENABLE_LE_SIGNED_WRITE
1439         // Identity Resolving completed (no code, gatt_client_run will continue)
1440         case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
1441         case SM_EVENT_IDENTITY_RESOLVING_FAILED:
1442             break;
1443 #endif
1444 
1445         // re-encryption started
1446         case SM_EVENT_REENCRYPTION_STARTED:
1447             con_handle = sm_event_reencryption_complete_get_handle(packet);
1448             gatt_client = gatt_client_get_context_for_handle(con_handle);
1449             if (gatt_client == NULL) break;
1450 
1451             gatt_client->reencryption_active = true;
1452             gatt_client->reencryption_result = ERROR_CODE_SUCCESS;
1453             break;
1454 
1455         // re-encryption complete
1456         case SM_EVENT_REENCRYPTION_COMPLETE:
1457             gatt_client_handle_reencryption_complete(packet);
1458             break;
1459         default:
1460             break;
1461     }
1462 
1463     gatt_client_run();
1464 }
1465 
1466 static void gatt_client_handle_att_read_response(gatt_client_t *gatt_client, uint8_t *packet, uint16_t size) {
1467     switch (gatt_client->gatt_client_state) {
1468         case P_W4_INCLUDED_SERVICE_UUID_WITH_QUERY_RESULT:
1469             if (size >= 17) {
1470                 uint8_t uuid128[16];
1471                 reverse_128(&packet[1], uuid128);
1472                 report_gatt_included_service_uuid128(gatt_client, gatt_client->start_group_handle, uuid128);
1473             }
1474             trigger_next_included_service_query(gatt_client, gatt_client->start_group_handle);
1475             // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1476             break;
1477 
1478         case P_W4_READ_CHARACTERISTIC_VALUE_RESULT:
1479             gatt_client_handle_transaction_complete(gatt_client);
1480             report_gatt_characteristic_value(gatt_client, gatt_client->attribute_handle, &packet[1], size - 1u);
1481             emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1482             break;
1483 
1484         case P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT:
1485             gatt_client_handle_transaction_complete(gatt_client);
1486             report_gatt_characteristic_descriptor(gatt_client, gatt_client->attribute_handle, &packet[1],
1487                                                   size - 1u, 0u);
1488             emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1489             break;
1490 
1491             // Use ATT_READ_REQUEST for first blob of Read Long Characteristic
1492         case P_W4_READ_BLOB_RESULT:
1493             report_gatt_long_characteristic_value_blob(gatt_client, gatt_client->attribute_handle, &packet[1],
1494                                                        size - 1u, gatt_client->attribute_offset);
1495             trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_QUERY, size - 1u);
1496             // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1497             break;
1498 
1499             // Use ATT_READ_REQUEST for first blob of Read Long Characteristic Descriptor
1500         case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT:
1501             report_gatt_long_characteristic_descriptor(gatt_client, gatt_client->attribute_handle, &packet[1],
1502                                                        size - 1u, gatt_client->attribute_offset);
1503             trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY,
1504                                     size - 1u);
1505             // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1506             break;
1507 
1508         default:
1509             break;
1510     }
1511 }
1512 
1513 static void gatt_client_handle_att_read_by_type_response(gatt_client_t *gatt_client, uint8_t *packet, uint16_t size) {
1514     switch (gatt_client->gatt_client_state) {
1515         case P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT:
1516             report_gatt_characteristics(gatt_client, packet, size);
1517             trigger_next_characteristic_query(gatt_client,
1518                                               get_last_result_handle_from_characteristics_list(packet, size));
1519             // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done, or by ATT_ERROR
1520             break;
1521         case P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT:
1522             report_gatt_characteristics(gatt_client, packet, size);
1523             trigger_next_characteristic_query(gatt_client,
1524                                               get_last_result_handle_from_characteristics_list(packet, size));
1525             // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done, or by ATT_ERROR
1526             break;
1527         case P_W4_INCLUDED_SERVICE_QUERY_RESULT: {
1528             if (size < 2u) break;
1529             uint16_t uuid16 = 0;
1530             uint16_t pair_size = packet[1];
1531 
1532             if (pair_size == 6u) {
1533                 if (size < 8u) break;
1534                 // UUIDs not available, query first included service
1535                 gatt_client->start_group_handle = little_endian_read_16(packet, 2); // ready for next query
1536                 gatt_client->query_start_handle = little_endian_read_16(packet, 4);
1537                 gatt_client->query_end_handle = little_endian_read_16(packet, 6);
1538                 gatt_client->gatt_client_state = P_W2_SEND_INCLUDED_SERVICE_WITH_UUID_QUERY;
1539                 break;
1540             }
1541 
1542             if (pair_size != 8u) break;
1543 
1544             // UUIDs included, report all of them
1545             uint16_t offset;
1546             for (offset = 2u; (offset + 8u) <= size; offset += pair_size) {
1547                 uint16_t include_handle = little_endian_read_16(packet, offset);
1548                 gatt_client->query_start_handle = little_endian_read_16(packet, offset + 2u);
1549                 gatt_client->query_end_handle = little_endian_read_16(packet, offset + 4u);
1550                 uuid16 = little_endian_read_16(packet, offset + 6u);
1551                 report_gatt_included_service_uuid16(gatt_client, include_handle, uuid16);
1552             }
1553 
1554             trigger_next_included_service_query(gatt_client,
1555                                                 get_last_result_handle_from_included_services_list(packet,
1556                                                                                                    size));
1557             // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1558             break;
1559         }
1560 #ifndef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY
1561         case P_W4_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT:
1562             gatt_client->client_characteristic_configuration_handle = little_endian_read_16(packet, 2);
1563             gatt_client->gatt_client_state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION;
1564             break;
1565 #endif
1566         case P_W4_READ_BY_TYPE_RESPONSE: {
1567             uint16_t pair_size = packet[1];
1568             // set last result handle to last valid handle, only used if pair_size invalid
1569             uint16_t last_result_handle = 0xffff;
1570             if (pair_size > 2) {
1571                 uint16_t offset;
1572                 for (offset = 2; offset < size; offset += pair_size) {
1573                     uint16_t value_handle = little_endian_read_16(packet, offset);
1574                     report_gatt_characteristic_value(gatt_client, value_handle, &packet[offset + 2u],
1575                                                      pair_size - 2u);
1576                     last_result_handle = value_handle;
1577                 }
1578             }
1579             trigger_next_read_by_type_query(gatt_client, last_result_handle);
1580             break;
1581         }
1582         default:
1583             break;
1584     }
1585 }
1586 
1587 static void gatt_client_handle_att_write_response(gatt_client_t *gatt_client) {
1588     switch (gatt_client->gatt_client_state) {
1589         case P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT:
1590             gatt_client_handle_transaction_complete(gatt_client);
1591             emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1592             break;
1593         case P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT:
1594             gatt_client_handle_transaction_complete(gatt_client);
1595             emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1596             break;
1597         case P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
1598             gatt_client_handle_transaction_complete(gatt_client);
1599             emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1600             break;
1601         default:
1602             break;
1603     }
1604 }
1605 
1606 static void gatt_client_handle_att_response(gatt_client_t * gatt_client, uint8_t * packet, uint16_t size) {
1607     uint8_t error_code;
1608     switch (packet[0]) {
1609         case ATT_EXCHANGE_MTU_RESPONSE: {
1610             if (size < 3u) break;
1611             bool update_gatt_server_att_mtu = false;
1612             uint16_t remote_rx_mtu = little_endian_read_16(packet, 1);
1613             uint16_t local_rx_mtu = l2cap_max_le_mtu();
1614             switch (gatt_client->bearer_type){
1615                 case ATT_BEARER_UNENHANCED_LE:
1616                     update_gatt_server_att_mtu = true;
1617                     break;
1618 #ifdef ENABLE_GATT_OVER_CLASSIC
1619                 case ATT_BEARER_UNENHANCED_CLASSIC:
1620                     local_rx_mtu = gatt_client->mtu;
1621                     break;
1622 #endif
1623                 default:
1624                     btstack_unreachable();
1625                     break;
1626             }
1627 
1628             uint16_t mtu = (remote_rx_mtu < local_rx_mtu) ? remote_rx_mtu : local_rx_mtu;
1629 
1630             // set gatt client mtu
1631             gatt_client->mtu = mtu;
1632             gatt_client->mtu_state = MTU_EXCHANGED;
1633 
1634             if (update_gatt_server_att_mtu){
1635                 // set per connection mtu state - for fixed channel
1636                 hci_connection_t *hci_connection = hci_connection_for_handle(gatt_client->con_handle);
1637                 hci_connection->att_connection.mtu = gatt_client->mtu;
1638                 hci_connection->att_connection.mtu_exchanged = true;
1639             }
1640             emit_gatt_mtu_exchanged_result_event(gatt_client, gatt_client->mtu);
1641             break;
1642         }
1643         case ATT_READ_BY_GROUP_TYPE_RESPONSE:
1644             switch (gatt_client->gatt_client_state) {
1645                 case P_W4_SERVICE_QUERY_RESULT:
1646                     report_gatt_services(gatt_client, packet, size);
1647                     trigger_next_service_query(gatt_client, get_last_result_handle_from_service_list(packet, size));
1648                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1649                     break;
1650                 default:
1651                     break;
1652             }
1653             break;
1654         case ATT_HANDLE_VALUE_NOTIFICATION:
1655             if (size < 3u) return;
1656             report_gatt_notification(gatt_client, little_endian_read_16(packet, 1u), &packet[3], size - 3u);
1657             return;
1658         case ATT_HANDLE_VALUE_INDICATION:
1659             if (size < 3u) break;
1660             report_gatt_indication(gatt_client, little_endian_read_16(packet, 1u), &packet[3], size - 3u);
1661             gatt_client->send_confirmation = 1;
1662             break;
1663         case ATT_READ_BY_TYPE_RESPONSE:
1664             gatt_client_handle_att_read_by_type_response(gatt_client, packet, size);
1665             break;
1666         case ATT_READ_RESPONSE:
1667             gatt_client_handle_att_read_response(gatt_client, packet, size);
1668             break;
1669         case ATT_FIND_BY_TYPE_VALUE_RESPONSE: {
1670             uint8_t pair_size = 4;
1671             int i;
1672             uint16_t start_group_handle;
1673             uint16_t end_group_handle = 0xffff; // asserts GATT_EVENT_QUERY_COMPLETE is emitted if no results
1674             for (i = 1u; (i + pair_size) <= size; i += pair_size) {
1675                 start_group_handle = little_endian_read_16(packet, i);
1676                 end_group_handle = little_endian_read_16(packet, i + 2);
1677                 emit_gatt_service_query_result_event(gatt_client, start_group_handle, end_group_handle,
1678                                                      gatt_client->uuid128);
1679             }
1680             trigger_next_service_by_uuid_query(gatt_client, end_group_handle);
1681             // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1682             break;
1683         }
1684         case ATT_FIND_INFORMATION_REPLY: {
1685             if (size < 2u) break;
1686 
1687             uint8_t pair_size = 4;
1688             if (packet[1u] == 2u) {
1689                 pair_size = 18;
1690             }
1691             uint16_t offset = 2;
1692 
1693             if (size < (pair_size + offset)) break;
1694             uint16_t last_descriptor_handle = little_endian_read_16(packet, size - pair_size);
1695 
1696 #ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY
1697             log_info("ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY, state %x", gatt_client->gatt_client_state);
1698             if (gatt_client->gatt_client_state == P_W4_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT){
1699                 // iterate over descriptors looking for CCC
1700                 if (pair_size == 4){
1701                     while ((offset + 4) <= size){
1702                         uint16_t uuid16 = little_endian_read_16(packet, offset + 2);
1703                         if (uuid16 == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION){
1704                             gatt_client->client_characteristic_configuration_handle = little_endian_read_16(packet, offset);
1705                             gatt_client->gatt_client_state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION;
1706                             log_info("CCC found %x", gatt_client->client_characteristic_configuration_handle);
1707                             break;
1708                         }
1709                         offset += pair_size;
1710                     }
1711                 }
1712                 if (is_query_done(gatt_client, last_descriptor_handle)){
1713 
1714                 } else {
1715                     // next
1716                     gatt_client->start_group_handle = last_descriptor_handle + 1;
1717                     gatt_client->gatt_client_state = P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY;
1718                 }
1719                 break;
1720             }
1721 #endif
1722             report_gatt_all_characteristic_descriptors(gatt_client, &packet[2], size - 2u, pair_size);
1723             trigger_next_characteristic_descriptor_query(gatt_client, last_descriptor_handle);
1724             // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1725             break;
1726         }
1727 
1728         case ATT_WRITE_RESPONSE:
1729             gatt_client_handle_att_write_response(gatt_client);
1730             break;
1731 
1732         case ATT_READ_BLOB_RESPONSE: {
1733             uint16_t received_blob_length = size - 1u;
1734             switch (gatt_client->gatt_client_state) {
1735                 case P_W4_READ_BLOB_RESULT:
1736                     report_gatt_long_characteristic_value_blob(gatt_client, gatt_client->attribute_handle, &packet[1],
1737                                                                received_blob_length, gatt_client->attribute_offset);
1738                     trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_QUERY, received_blob_length);
1739                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1740                     break;
1741                 case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT:
1742                     report_gatt_long_characteristic_descriptor(gatt_client, gatt_client->attribute_handle,
1743                                                                &packet[1], received_blob_length,
1744                                                                gatt_client->attribute_offset);
1745                     trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY,
1746                                             received_blob_length);
1747                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1748                     break;
1749                 default:
1750                     break;
1751             }
1752             break;
1753         }
1754         case ATT_PREPARE_WRITE_RESPONSE:
1755             switch (gatt_client->gatt_client_state) {
1756                 case P_W4_PREPARE_WRITE_SINGLE_RESULT:
1757                     gatt_client_handle_transaction_complete(gatt_client);
1758                     if (is_value_valid(gatt_client, packet, size)) {
1759                         emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1760                     } else {
1761                         emit_gatt_complete_event(gatt_client, ATT_ERROR_DATA_MISMATCH);
1762                     }
1763                     break;
1764 
1765                 case P_W4_PREPARE_WRITE_RESULT: {
1766                     gatt_client->attribute_offset = little_endian_read_16(packet, 3);
1767                     trigger_next_prepare_write_query(gatt_client, P_W2_PREPARE_WRITE, P_W2_EXECUTE_PREPARED_WRITE);
1768                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1769                     break;
1770                 }
1771                 case P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT: {
1772                     gatt_client->attribute_offset = little_endian_read_16(packet, 3);
1773                     trigger_next_prepare_write_query(gatt_client, P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR,
1774                                                      P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR);
1775                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1776                     break;
1777                 }
1778                 case P_W4_PREPARE_RELIABLE_WRITE_RESULT: {
1779                     if (is_value_valid(gatt_client, packet, size)) {
1780                         gatt_client->attribute_offset = little_endian_read_16(packet, 3);
1781                         trigger_next_prepare_write_query(gatt_client, P_W2_PREPARE_RELIABLE_WRITE,
1782                                                          P_W2_EXECUTE_PREPARED_WRITE);
1783                         // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1784                         break;
1785                     }
1786                     gatt_client->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH;
1787                     break;
1788                 }
1789                 default:
1790                     break;
1791             }
1792             break;
1793 
1794         case ATT_EXECUTE_WRITE_RESPONSE:
1795             switch (gatt_client->gatt_client_state) {
1796                 case P_W4_EXECUTE_PREPARED_WRITE_RESULT:
1797                     gatt_client_handle_transaction_complete(gatt_client);
1798                     emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1799                     break;
1800                 case P_W4_CANCEL_PREPARED_WRITE_RESULT:
1801                     gatt_client_handle_transaction_complete(gatt_client);
1802                     emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1803                     break;
1804                 case P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT:
1805                     gatt_client_handle_transaction_complete(gatt_client);
1806                     emit_gatt_complete_event(gatt_client, ATT_ERROR_DATA_MISMATCH);
1807                     break;
1808                 case P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
1809                     gatt_client_handle_transaction_complete(gatt_client);
1810                     emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1811                     break;
1812                 default:
1813                     break;
1814 
1815             }
1816             break;
1817 
1818         case ATT_READ_MULTIPLE_RESPONSE:
1819             switch (gatt_client->gatt_client_state) {
1820                 case P_W4_READ_MULTIPLE_RESPONSE:
1821                     report_gatt_characteristic_value(gatt_client, 0u, &packet[1], size - 1u);
1822                     gatt_client_handle_transaction_complete(gatt_client);
1823                     emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1824                     break;
1825                 default:
1826                     break;
1827             }
1828             break;
1829 
1830         case ATT_ERROR_RESPONSE:
1831             if (size < 5u) return;
1832             error_code = packet[4];
1833             switch (error_code) {
1834                 case ATT_ERROR_ATTRIBUTE_NOT_FOUND: {
1835                     switch (gatt_client->gatt_client_state) {
1836                         case P_W4_SERVICE_QUERY_RESULT:
1837                         case P_W4_SERVICE_WITH_UUID_RESULT:
1838                         case P_W4_INCLUDED_SERVICE_QUERY_RESULT:
1839                         case P_W4_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT:
1840                             gatt_client_handle_transaction_complete(gatt_client);
1841                             emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1842                             break;
1843                         case P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT:
1844                         case P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT:
1845                             characteristic_end_found(gatt_client, gatt_client->end_group_handle);
1846                             gatt_client_handle_transaction_complete(gatt_client);
1847                             emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1848                             break;
1849                         case P_W4_READ_BY_TYPE_RESPONSE:
1850                             gatt_client_handle_transaction_complete(gatt_client);
1851                             if (gatt_client->start_group_handle == gatt_client->query_start_handle) {
1852                                 emit_gatt_complete_event(gatt_client, ATT_ERROR_ATTRIBUTE_NOT_FOUND);
1853                             } else {
1854                                 emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1855                             }
1856                             break;
1857                         default:
1858                             gatt_client_report_error_if_pending(gatt_client, error_code);
1859                             break;
1860                     }
1861                     break;
1862                 }
1863 
1864 #ifdef ENABLE_GATT_CLIENT_PAIRING
1865 
1866                     case ATT_ERROR_INSUFFICIENT_AUTHENTICATION:
1867                     case ATT_ERROR_INSUFFICIENT_ENCRYPTION_KEY_SIZE:
1868                     case ATT_ERROR_INSUFFICIENT_ENCRYPTION: {
1869 
1870                         // security too low
1871                         if (gatt_client->security_counter > 0) {
1872                             gatt_client_report_error_if_pending(gatt_client, error_code);
1873                             break;
1874                         }
1875                         // start security
1876                         gatt_client->security_counter++;
1877 
1878                         // setup action
1879                         int retry = 1;
1880                         switch (gatt_client->gatt_client_state){
1881                             case P_W4_READ_CHARACTERISTIC_VALUE_RESULT:
1882                                 gatt_client->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY ;
1883                                 break;
1884                             case P_W4_READ_BLOB_RESULT:
1885                                 gatt_client->gatt_client_state = P_W2_SEND_READ_BLOB_QUERY;
1886                                 break;
1887                             case P_W4_READ_BY_TYPE_RESPONSE:
1888                                 gatt_client->gatt_client_state = P_W2_SEND_READ_BY_TYPE_REQUEST;
1889                                 break;
1890                             case P_W4_READ_MULTIPLE_RESPONSE:
1891                                 gatt_client->gatt_client_state = P_W2_SEND_READ_MULTIPLE_REQUEST;
1892                                 break;
1893                             case P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT:
1894                                 gatt_client->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_VALUE;
1895                                 break;
1896                             case P_W4_PREPARE_WRITE_RESULT:
1897                                 gatt_client->gatt_client_state = P_W2_PREPARE_WRITE;
1898                                 break;
1899                             case P_W4_PREPARE_WRITE_SINGLE_RESULT:
1900                                 gatt_client->gatt_client_state = P_W2_PREPARE_WRITE_SINGLE;
1901                                 break;
1902                             case P_W4_PREPARE_RELIABLE_WRITE_RESULT:
1903                                 gatt_client->gatt_client_state = P_W2_PREPARE_RELIABLE_WRITE;
1904                                 break;
1905                             case P_W4_EXECUTE_PREPARED_WRITE_RESULT:
1906                                 gatt_client->gatt_client_state = P_W2_EXECUTE_PREPARED_WRITE;
1907                                 break;
1908                             case P_W4_CANCEL_PREPARED_WRITE_RESULT:
1909                                 gatt_client->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE;
1910                                 break;
1911                             case P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT:
1912                                 gatt_client->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH;
1913                                 break;
1914                             case P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT:
1915                                 gatt_client->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY;
1916                                 break;
1917                             case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT:
1918                                 gatt_client->gatt_client_state = P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY;
1919                                 break;
1920                             case P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
1921                                 gatt_client->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR;
1922                                 break;
1923                             case P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT:
1924                                 gatt_client->gatt_client_state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION;
1925                                 break;
1926                             case P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
1927                                 gatt_client->gatt_client_state = P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR;
1928                                 break;
1929                             case P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
1930                                 gatt_client->gatt_client_state = P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR;
1931                                 break;
1932 #ifdef ENABLE_LE_SIGNED_WRITE
1933                             case P_W4_SEND_SINGED_WRITE_DONE:
1934                                 gatt_client->gatt_client_state = P_W2_SEND_SIGNED_WRITE;
1935                                 break;
1936 #endif
1937                             default:
1938                                 log_info("retry not supported for state %x", gatt_client->gatt_client_state);
1939                                 retry = 0;
1940                                 break;
1941                         }
1942 
1943                         if (!retry) {
1944                             gatt_client_report_error_if_pending(gatt_client, error_code);
1945                             break;
1946                         }
1947 
1948                         log_info("security error, start pairing");
1949 
1950                         // start pairing for higher security level
1951                         gatt_client->wait_for_authentication_complete = 1;
1952                         gatt_client->pending_error_code = error_code;
1953                         sm_request_pairing(gatt_client->con_handle);
1954                         break;
1955                     }
1956 #endif
1957 
1958                     // nothing we can do about that
1959                 case ATT_ERROR_INSUFFICIENT_AUTHORIZATION:
1960                 default:
1961                     gatt_client_report_error_if_pending(gatt_client, error_code);
1962                     break;
1963             }
1964             break;
1965 
1966         default:
1967             log_info("ATT Handler, unhandled response type 0x%02x", packet[0]);
1968             break;
1969     }
1970 }
1971 
1972 static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size) {
1973     gatt_client_t *gatt_client;
1974     if (size < 1u) return;
1975 
1976     if (packet_type == HCI_EVENT_PACKET) {
1977         switch (packet[0]) {
1978             case L2CAP_EVENT_CAN_SEND_NOW:
1979                 gatt_client_run();
1980                 break;
1981                 // att_server has negotiated the mtu for this connection, cache if context exists
1982             case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
1983                 if (size < 6u) break;
1984                 gatt_client = gatt_client_get_context_for_handle(handle);
1985                 if (gatt_client == NULL) break;
1986                 gatt_client->mtu = little_endian_read_16(packet, 4);
1987                 break;
1988             default:
1989                 break;
1990         }
1991         return;
1992     }
1993 
1994     if (packet_type != ATT_DATA_PACKET) return;
1995 
1996     // special cases: notifications & indications motivate creating context
1997     switch (packet[0]) {
1998         case ATT_HANDLE_VALUE_NOTIFICATION:
1999         case ATT_HANDLE_VALUE_INDICATION:
2000             gatt_client_provide_context_for_handle(handle, &gatt_client);
2001             break;
2002         default:
2003             gatt_client = gatt_client_get_context_for_handle(handle);
2004             break;
2005     }
2006 
2007     if (gatt_client != NULL) {
2008         gatt_client_handle_att_response(gatt_client, packet, size);
2009         gatt_client_run();
2010     }
2011 }
2012 
2013 #ifdef ENABLE_LE_SIGNED_WRITE
2014 static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
2015     btstack_linked_list_iterator_t it;
2016     btstack_linked_list_iterator_init(&it, &gatt_client_connections);
2017     while (btstack_linked_list_iterator_has_next(&it)){
2018         gatt_client_t * gatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it);
2019         if (gatt_client->gatt_client_state == P_W4_CMAC_RESULT){
2020             // store result
2021             (void)memcpy(gatt_client->cmac, hash, 8);
2022             // reverse_64(hash, gatt_client->cmac);
2023             gatt_client->gatt_client_state = P_W2_SEND_SIGNED_WRITE;
2024             gatt_client_run();
2025             return;
2026         }
2027     }
2028 }
2029 
2030 uint8_t gatt_client_signed_write_without_response(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle, uint16_t message_len, uint8_t * message){
2031     gatt_client_t * gatt_client;
2032     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
2033     if (status != ERROR_CODE_SUCCESS){
2034         return status;
2035     }
2036     if (is_ready(gatt_client) == 0){
2037         return GATT_CLIENT_IN_WRONG_STATE;
2038     }
2039 
2040     gatt_client->callback = callback;
2041     gatt_client->attribute_handle = value_handle;
2042     gatt_client->attribute_length = message_len;
2043     gatt_client->attribute_value = message;
2044     gatt_client->gatt_client_state = P_W4_IDENTITY_RESOLVING;
2045     gatt_client_run();
2046     return ERROR_CODE_SUCCESS;
2047 }
2048 #endif
2049 
2050 uint8_t gatt_client_discover_primary_services(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
2051     gatt_client_t * gatt_client;
2052     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
2053     if (status != ERROR_CODE_SUCCESS){
2054         return status;
2055     }
2056 
2057     gatt_client->callback = callback;
2058     gatt_client->start_group_handle = 0x0001;
2059     gatt_client->end_group_handle   = 0xffff;
2060     gatt_client->gatt_client_state = P_W2_SEND_SERVICE_QUERY;
2061     gatt_client->uuid16 = GATT_PRIMARY_SERVICE_UUID;
2062     gatt_client_run();
2063     return ERROR_CODE_SUCCESS;
2064 }
2065 
2066 uint8_t gatt_client_discover_secondary_services(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
2067     gatt_client_t * gatt_client;
2068     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
2069     if (status != ERROR_CODE_SUCCESS){
2070         return status;
2071     }
2072 
2073     gatt_client->callback = callback;
2074     gatt_client->start_group_handle = 0x0001;
2075     gatt_client->end_group_handle   = 0xffff;
2076     gatt_client->gatt_client_state = P_W2_SEND_SERVICE_QUERY;
2077     gatt_client->uuid16 = GATT_SECONDARY_SERVICE_UUID;
2078     gatt_client_run();
2079     return ERROR_CODE_SUCCESS;
2080 }
2081 
2082 uint8_t gatt_client_discover_primary_services_by_uuid16(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t uuid16){
2083     gatt_client_t * gatt_client;
2084     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
2085     if (status != ERROR_CODE_SUCCESS){
2086         return status;
2087     }
2088 
2089     gatt_client->callback = callback;
2090     gatt_client->start_group_handle = 0x0001;
2091     gatt_client->end_group_handle   = 0xffff;
2092     gatt_client->gatt_client_state = P_W2_SEND_SERVICE_WITH_UUID_QUERY;
2093     gatt_client->uuid16 = uuid16;
2094     uuid_add_bluetooth_prefix((uint8_t*) &(gatt_client->uuid128), gatt_client->uuid16);
2095     gatt_client_run();
2096     return ERROR_CODE_SUCCESS;
2097 }
2098 
2099 uint8_t gatt_client_discover_primary_services_by_uuid128(btstack_packet_handler_t callback, hci_con_handle_t con_handle, const uint8_t * uuid128){
2100     gatt_client_t * gatt_client;
2101     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
2102     if (status != ERROR_CODE_SUCCESS){
2103         return status;
2104     }
2105 
2106     gatt_client->callback = callback;
2107     gatt_client->start_group_handle = 0x0001;
2108     gatt_client->end_group_handle   = 0xffff;
2109     gatt_client->uuid16 = 0;
2110     (void)memcpy(gatt_client->uuid128, uuid128, 16);
2111     gatt_client->gatt_client_state = P_W2_SEND_SERVICE_WITH_UUID_QUERY;
2112     gatt_client_run();
2113     return ERROR_CODE_SUCCESS;
2114 }
2115 
2116 uint8_t gatt_client_discover_characteristics_for_service(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_service_t * service){
2117     gatt_client_t * gatt_client;
2118     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
2119     if (status != ERROR_CODE_SUCCESS){
2120         return status;
2121     }
2122 
2123     gatt_client->callback = callback;
2124     gatt_client->start_group_handle = service->start_group_handle;
2125     gatt_client->end_group_handle   = service->end_group_handle;
2126     gatt_client->filter_with_uuid = 0;
2127     gatt_client->characteristic_start_handle = 0;
2128     gatt_client->gatt_client_state = P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY;
2129     gatt_client_run();
2130     return ERROR_CODE_SUCCESS;
2131 }
2132 
2133 uint8_t gatt_client_find_included_services_for_service(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_service_t * service){
2134     gatt_client_t * gatt_client;
2135     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
2136     if (status != ERROR_CODE_SUCCESS){
2137         return status;
2138     }
2139 
2140     gatt_client->callback = callback;
2141     gatt_client->start_group_handle = service->start_group_handle;
2142     gatt_client->end_group_handle   = service->end_group_handle;
2143     gatt_client->gatt_client_state = P_W2_SEND_INCLUDED_SERVICE_QUERY;
2144 
2145     gatt_client_run();
2146     return ERROR_CODE_SUCCESS;
2147 }
2148 
2149 uint8_t gatt_client_discover_characteristics_for_handle_range_by_uuid16(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t start_handle, uint16_t end_handle, uint16_t uuid16){
2150     gatt_client_t * gatt_client;
2151     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
2152     if (status != ERROR_CODE_SUCCESS){
2153         return status;
2154     }
2155 
2156     gatt_client->callback = callback;
2157     gatt_client->start_group_handle = start_handle;
2158     gatt_client->end_group_handle   = end_handle;
2159     gatt_client->filter_with_uuid = 1;
2160     gatt_client->uuid16 = uuid16;
2161     uuid_add_bluetooth_prefix((uint8_t*) &(gatt_client->uuid128), uuid16);
2162     gatt_client->characteristic_start_handle = 0;
2163     gatt_client->gatt_client_state = P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY;
2164     gatt_client_run();
2165     return ERROR_CODE_SUCCESS;
2166 }
2167 
2168 uint8_t gatt_client_discover_characteristics_for_handle_range_by_uuid128(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128){
2169     gatt_client_t * gatt_client;
2170     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
2171     if (status != ERROR_CODE_SUCCESS){
2172         return status;
2173     }
2174 
2175     gatt_client->callback = callback;
2176     gatt_client->start_group_handle = start_handle;
2177     gatt_client->end_group_handle   = end_handle;
2178     gatt_client->filter_with_uuid = 1;
2179     gatt_client->uuid16 = 0;
2180     (void)memcpy(gatt_client->uuid128, uuid128, 16);
2181     gatt_client->characteristic_start_handle = 0;
2182     gatt_client->gatt_client_state = P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY;
2183     gatt_client_run();
2184     return ERROR_CODE_SUCCESS;
2185 }
2186 
2187 
2188 uint8_t gatt_client_discover_characteristics_for_service_by_uuid16(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_service_t * service, uint16_t uuid16){
2189     return gatt_client_discover_characteristics_for_handle_range_by_uuid16(callback, con_handle, service->start_group_handle, service->end_group_handle, uuid16);
2190 }
2191 
2192 uint8_t gatt_client_discover_characteristics_for_service_by_uuid128(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_service_t * service, const uint8_t * uuid128){
2193     return gatt_client_discover_characteristics_for_handle_range_by_uuid128(callback, con_handle, service->start_group_handle, service->end_group_handle, uuid128);
2194 }
2195 
2196 uint8_t gatt_client_discover_characteristic_descriptors(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic){
2197     gatt_client_t * gatt_client;
2198     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
2199     if (status != ERROR_CODE_SUCCESS){
2200         return status;
2201     }
2202 
2203     if (characteristic->value_handle == characteristic->end_handle){
2204         emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
2205         return ERROR_CODE_SUCCESS;
2206     }
2207     gatt_client->callback = callback;
2208     gatt_client->start_group_handle = characteristic->value_handle + 1u;
2209     gatt_client->end_group_handle   = characteristic->end_handle;
2210     gatt_client->gatt_client_state = P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY;
2211     gatt_client_run();
2212     return ERROR_CODE_SUCCESS;
2213 }
2214 
2215 uint8_t gatt_client_read_value_of_characteristic_using_value_handle(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle){
2216     gatt_client_t * gatt_client;
2217     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
2218     if (status != ERROR_CODE_SUCCESS){
2219         return status;
2220     }
2221 
2222     gatt_client->callback = callback;
2223     gatt_client->attribute_handle = value_handle;
2224     gatt_client->attribute_offset = 0;
2225     gatt_client->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY;
2226     gatt_client_run();
2227     return ERROR_CODE_SUCCESS;
2228 }
2229 
2230 uint8_t gatt_client_read_value_of_characteristics_by_uuid16(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t start_handle, uint16_t end_handle, uint16_t uuid16){
2231     gatt_client_t * gatt_client;
2232     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
2233     if (status != ERROR_CODE_SUCCESS){
2234         return status;
2235     }
2236 
2237     gatt_client->callback = callback;
2238     gatt_client->start_group_handle = start_handle;
2239     gatt_client->end_group_handle = end_handle;
2240     gatt_client->query_start_handle = start_handle;
2241     gatt_client->query_end_handle = end_handle;
2242     gatt_client->uuid16 = uuid16;
2243     uuid_add_bluetooth_prefix((uint8_t*) &(gatt_client->uuid128), uuid16);
2244     gatt_client->gatt_client_state = P_W2_SEND_READ_BY_TYPE_REQUEST;
2245     gatt_client_run();
2246     return ERROR_CODE_SUCCESS;
2247 }
2248 
2249 uint8_t gatt_client_read_value_of_characteristics_by_uuid128(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128){
2250     gatt_client_t * gatt_client;
2251     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
2252     if (status != ERROR_CODE_SUCCESS){
2253         return status;
2254     }
2255 
2256     gatt_client->callback = callback;
2257     gatt_client->start_group_handle = start_handle;
2258     gatt_client->end_group_handle = end_handle;
2259     gatt_client->query_start_handle = start_handle;
2260     gatt_client->query_end_handle = end_handle;
2261     gatt_client->uuid16 = 0;
2262     (void)memcpy(gatt_client->uuid128, uuid128, 16);
2263     gatt_client->gatt_client_state = P_W2_SEND_READ_BY_TYPE_REQUEST;
2264     gatt_client_run();
2265     return ERROR_CODE_SUCCESS;
2266 }
2267 
2268 
2269 uint8_t gatt_client_read_value_of_characteristic(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic){
2270     return gatt_client_read_value_of_characteristic_using_value_handle(callback, con_handle, characteristic->value_handle);
2271 }
2272 
2273 uint8_t gatt_client_read_long_value_of_characteristic_using_value_handle_with_offset(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle, uint16_t offset){
2274     gatt_client_t * gatt_client;
2275     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
2276     if (status != ERROR_CODE_SUCCESS){
2277         return status;
2278     }
2279 
2280     gatt_client->callback = callback;
2281     gatt_client->attribute_handle = value_handle;
2282     gatt_client->attribute_offset = offset;
2283     gatt_client->gatt_client_state = P_W2_SEND_READ_BLOB_QUERY;
2284     gatt_client_run();
2285     return ERROR_CODE_SUCCESS;
2286 }
2287 
2288 uint8_t gatt_client_read_long_value_of_characteristic_using_value_handle(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle){
2289     return gatt_client_read_long_value_of_characteristic_using_value_handle_with_offset(callback, con_handle, value_handle, 0);
2290 }
2291 
2292 uint8_t gatt_client_read_long_value_of_characteristic(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic){
2293     return gatt_client_read_long_value_of_characteristic_using_value_handle(callback, con_handle, characteristic->value_handle);
2294 }
2295 
2296 uint8_t gatt_client_read_multiple_characteristic_values(btstack_packet_handler_t callback, hci_con_handle_t con_handle, int num_value_handles, uint16_t * value_handles){
2297     gatt_client_t * gatt_client;
2298     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
2299     if (status != ERROR_CODE_SUCCESS){
2300         return status;
2301     }
2302 
2303     gatt_client->callback = callback;
2304     gatt_client->read_multiple_handle_count = num_value_handles;
2305     gatt_client->read_multiple_handles = value_handles;
2306     gatt_client->gatt_client_state = P_W2_SEND_READ_MULTIPLE_REQUEST;
2307     gatt_client_run();
2308     return ERROR_CODE_SUCCESS;
2309 }
2310 
2311 uint8_t gatt_client_write_value_of_characteristic_without_response(hci_con_handle_t con_handle, uint16_t value_handle, uint16_t value_length, uint8_t * value){
2312     gatt_client_t * gatt_client;
2313     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
2314     if (status != ERROR_CODE_SUCCESS){
2315         return status;
2316     }
2317 
2318     if (value_length > (gatt_client->mtu - 3u)) return GATT_CLIENT_VALUE_TOO_LONG;
2319     if (!att_dispatch_client_can_send_now(gatt_client->con_handle)) return GATT_CLIENT_BUSY;
2320 
2321     return att_write_request(gatt_client, ATT_WRITE_COMMAND, value_handle, value_length, value);
2322 }
2323 
2324 uint8_t gatt_client_write_value_of_characteristic(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle, uint16_t value_length, uint8_t * value){
2325     gatt_client_t * gatt_client;
2326     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
2327     if (status != ERROR_CODE_SUCCESS){
2328         return status;
2329     }
2330 
2331     gatt_client->callback = callback;
2332     gatt_client->attribute_handle = value_handle;
2333     gatt_client->attribute_length = value_length;
2334     gatt_client->attribute_value = value;
2335     gatt_client->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_VALUE;
2336     gatt_client_run();
2337     return ERROR_CODE_SUCCESS;
2338 }
2339 
2340 uint8_t gatt_client_write_long_value_of_characteristic_with_offset(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle, uint16_t offset, uint16_t value_length, uint8_t * value){
2341     gatt_client_t * gatt_client;
2342     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
2343     if (status != ERROR_CODE_SUCCESS){
2344         return status;
2345     }
2346 
2347     gatt_client->callback = callback;
2348     gatt_client->attribute_handle = value_handle;
2349     gatt_client->attribute_length = value_length;
2350     gatt_client->attribute_offset = offset;
2351     gatt_client->attribute_value = value;
2352     gatt_client->gatt_client_state = P_W2_PREPARE_WRITE;
2353     gatt_client_run();
2354     return ERROR_CODE_SUCCESS;
2355 }
2356 
2357 uint8_t gatt_client_write_long_value_of_characteristic(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle, uint16_t value_length, uint8_t * value){
2358     return gatt_client_write_long_value_of_characteristic_with_offset(callback, con_handle, value_handle, 0, value_length, value);
2359 }
2360 
2361 uint8_t gatt_client_reliable_write_long_value_of_characteristic(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle, uint16_t value_length, uint8_t * value){
2362     gatt_client_t * gatt_client;
2363     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
2364     if (status != ERROR_CODE_SUCCESS){
2365         return status;
2366     }
2367 
2368     gatt_client->callback = callback;
2369     gatt_client->attribute_handle = value_handle;
2370     gatt_client->attribute_length = value_length;
2371     gatt_client->attribute_offset = 0;
2372     gatt_client->attribute_value = value;
2373     gatt_client->gatt_client_state = P_W2_PREPARE_RELIABLE_WRITE;
2374     gatt_client_run();
2375     return ERROR_CODE_SUCCESS;
2376 }
2377 
2378 uint8_t gatt_client_write_client_characteristic_configuration(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic, uint16_t configuration){
2379     gatt_client_t * gatt_client;
2380     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
2381     if (status != ERROR_CODE_SUCCESS){
2382         return status;
2383     }
2384 
2385     if ( (configuration & GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION) &&
2386         ((characteristic->properties & ATT_PROPERTY_NOTIFY) == 0u)) {
2387         log_info("gatt_client_write_client_characteristic_configuration: GATT_CLIENT_CHARACTERISTIC_NOTIFICATION_NOT_SUPPORTED");
2388         return GATT_CLIENT_CHARACTERISTIC_NOTIFICATION_NOT_SUPPORTED;
2389     } else if ( (configuration & GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_INDICATION) &&
2390                ((characteristic->properties & ATT_PROPERTY_INDICATE) == 0u)){
2391         log_info("gatt_client_write_client_characteristic_configuration: GATT_CLIENT_CHARACTERISTIC_INDICATION_NOT_SUPPORTED");
2392         return GATT_CLIENT_CHARACTERISTIC_INDICATION_NOT_SUPPORTED;
2393     }
2394 
2395     gatt_client->callback = callback;
2396     gatt_client->start_group_handle = characteristic->value_handle;
2397     gatt_client->end_group_handle = characteristic->end_handle;
2398     little_endian_store_16(gatt_client->client_characteristic_configuration_value, 0, configuration);
2399 
2400 #ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY
2401     gatt_client->gatt_client_state = P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY;
2402 #else
2403     gatt_client->gatt_client_state = P_W2_SEND_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY;
2404 #endif
2405     gatt_client_run();
2406     return ERROR_CODE_SUCCESS;
2407 }
2408 
2409 uint8_t gatt_client_read_characteristic_descriptor_using_descriptor_handle(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t descriptor_handle){
2410     gatt_client_t * gatt_client;
2411     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
2412     if (status != ERROR_CODE_SUCCESS){
2413         return status;
2414     }
2415 
2416     gatt_client->callback = callback;
2417     gatt_client->attribute_handle = descriptor_handle;
2418 
2419     gatt_client->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY;
2420     gatt_client_run();
2421     return ERROR_CODE_SUCCESS;
2422 }
2423 
2424 uint8_t gatt_client_read_characteristic_descriptor(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_descriptor_t * descriptor){
2425     return gatt_client_read_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle);
2426 }
2427 
2428 uint8_t gatt_client_read_long_characteristic_descriptor_using_descriptor_handle_with_offset(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t descriptor_handle, uint16_t offset){
2429     gatt_client_t * gatt_client;
2430     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
2431     if (status != ERROR_CODE_SUCCESS){
2432         return status;
2433     }
2434 
2435     gatt_client->callback = callback;
2436     gatt_client->attribute_handle = descriptor_handle;
2437     gatt_client->attribute_offset = offset;
2438     gatt_client->gatt_client_state = P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY;
2439     gatt_client_run();
2440     return ERROR_CODE_SUCCESS;
2441 }
2442 
2443 uint8_t gatt_client_read_long_characteristic_descriptor_using_descriptor_handle(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t descriptor_handle){
2444     return gatt_client_read_long_characteristic_descriptor_using_descriptor_handle_with_offset(callback, con_handle, descriptor_handle, 0);
2445 }
2446 
2447 uint8_t gatt_client_read_long_characteristic_descriptor(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_descriptor_t * descriptor){
2448     return gatt_client_read_long_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle);
2449 }
2450 
2451 uint8_t gatt_client_write_characteristic_descriptor_using_descriptor_handle(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t descriptor_handle, uint16_t value_length, uint8_t * value){
2452     gatt_client_t * gatt_client;
2453     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
2454     if (status != ERROR_CODE_SUCCESS){
2455         return status;
2456     }
2457 
2458     gatt_client->callback = callback;
2459     gatt_client->attribute_handle = descriptor_handle;
2460     gatt_client->attribute_length = value_length;
2461     gatt_client->attribute_offset = 0;
2462     gatt_client->attribute_value = value;
2463     gatt_client->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR;
2464     gatt_client_run();
2465     return ERROR_CODE_SUCCESS;
2466 }
2467 
2468 uint8_t gatt_client_write_characteristic_descriptor(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_descriptor_t * descriptor, uint16_t value_length, uint8_t * value){
2469     return gatt_client_write_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle, value_length, value);
2470 }
2471 
2472 uint8_t gatt_client_write_long_characteristic_descriptor_using_descriptor_handle_with_offset(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t descriptor_handle, uint16_t offset, uint16_t value_length, uint8_t * value){
2473     gatt_client_t * gatt_client;
2474     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
2475     if (status != ERROR_CODE_SUCCESS){
2476         return status;
2477     }
2478 
2479     gatt_client->callback = callback;
2480     gatt_client->attribute_handle = descriptor_handle;
2481     gatt_client->attribute_length = value_length;
2482     gatt_client->attribute_offset = offset;
2483     gatt_client->attribute_value = value;
2484     gatt_client->gatt_client_state = P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR;
2485     gatt_client_run();
2486     return ERROR_CODE_SUCCESS;
2487 }
2488 
2489 uint8_t gatt_client_write_long_characteristic_descriptor_using_descriptor_handle(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t descriptor_handle, uint16_t value_length, uint8_t * value){
2490     return gatt_client_write_long_characteristic_descriptor_using_descriptor_handle_with_offset(callback, con_handle, descriptor_handle, 0, value_length, value);
2491 }
2492 
2493 uint8_t gatt_client_write_long_characteristic_descriptor(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_descriptor_t * descriptor, uint16_t value_length, uint8_t * value){
2494     return gatt_client_write_long_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle, value_length, value);
2495 }
2496 
2497 /**
2498  * @brief -> gatt complete event
2499  */
2500 uint8_t gatt_client_prepare_write(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t offset, uint16_t value_length, uint8_t * value){
2501     gatt_client_t * gatt_client;
2502     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
2503     if (status != ERROR_CODE_SUCCESS){
2504         return status;
2505     }
2506 
2507     gatt_client->callback = callback;
2508     gatt_client->attribute_handle = attribute_handle;
2509     gatt_client->attribute_length = value_length;
2510     gatt_client->attribute_offset = offset;
2511     gatt_client->attribute_value = value;
2512     gatt_client->gatt_client_state = P_W2_PREPARE_WRITE_SINGLE;
2513     gatt_client_run();
2514     return ERROR_CODE_SUCCESS;
2515 }
2516 
2517 /**
2518  * @brief -> gatt complete event
2519  */
2520 uint8_t gatt_client_execute_write(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
2521     gatt_client_t * gatt_client;
2522     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
2523     if (status != ERROR_CODE_SUCCESS){
2524         return status;
2525     }
2526 
2527     gatt_client->callback = callback;
2528     gatt_client->gatt_client_state = P_W2_EXECUTE_PREPARED_WRITE;
2529     gatt_client_run();
2530     return ERROR_CODE_SUCCESS;
2531 }
2532 
2533 /**
2534  * @brief -> gatt complete event
2535  */
2536 uint8_t gatt_client_cancel_write(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
2537     gatt_client_t * gatt_client;
2538     uint8_t status = gatt_client_provide_context_for_request(con_handle, &gatt_client);
2539     if (status != ERROR_CODE_SUCCESS){
2540         return status;
2541     }
2542 
2543     gatt_client->callback = callback;
2544     gatt_client->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE;
2545     gatt_client_run();
2546     return ERROR_CODE_SUCCESS;
2547 }
2548 
2549 void gatt_client_deserialize_service(const uint8_t *packet, int offset, gatt_client_service_t * service){
2550     service->start_group_handle = little_endian_read_16(packet, offset);
2551     service->end_group_handle = little_endian_read_16(packet, offset + 2);
2552     reverse_128(&packet[offset + 4], service->uuid128);
2553     if (uuid_has_bluetooth_prefix(service->uuid128)){
2554         service->uuid16 = big_endian_read_32(service->uuid128, 0);
2555     } else {
2556         service->uuid16 = 0;
2557     }
2558 }
2559 
2560 void gatt_client_deserialize_characteristic(const uint8_t * packet, int offset, gatt_client_characteristic_t * characteristic){
2561     characteristic->start_handle = little_endian_read_16(packet, offset);
2562     characteristic->value_handle = little_endian_read_16(packet, offset + 2);
2563     characteristic->end_handle = little_endian_read_16(packet, offset + 4);
2564     characteristic->properties = little_endian_read_16(packet, offset + 6);
2565     reverse_128(&packet[offset+8], characteristic->uuid128);
2566     if (uuid_has_bluetooth_prefix(characteristic->uuid128)){
2567         characteristic->uuid16 = big_endian_read_32(characteristic->uuid128, 0);
2568     } else {
2569         characteristic->uuid16 = 0;
2570     }
2571 }
2572 
2573 void gatt_client_deserialize_characteristic_descriptor(const uint8_t * packet, int offset, gatt_client_characteristic_descriptor_t * descriptor){
2574     descriptor->handle = little_endian_read_16(packet, offset);
2575     reverse_128(&packet[offset+2], descriptor->uuid128);
2576     if (uuid_has_bluetooth_prefix(descriptor->uuid128)){
2577         descriptor->uuid16 = big_endian_read_32(descriptor->uuid128, 0);
2578     } else {
2579         descriptor->uuid16 = 0;
2580     }
2581 }
2582 
2583 void gatt_client_send_mtu_negotiation(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
2584     gatt_client_t * gatt_client;
2585     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
2586     if (status != ERROR_CODE_SUCCESS){
2587         return;
2588     }
2589     if (gatt_client->mtu_state == MTU_AUTO_EXCHANGE_DISABLED){
2590         gatt_client->callback = callback;
2591         gatt_client->mtu_state = SEND_MTU_EXCHANGE;
2592         gatt_client_run();
2593     }
2594 }
2595 
2596 uint8_t gatt_client_request_to_write_without_response(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
2597     gatt_client_t * gatt_client;
2598     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
2599     if (status != ERROR_CODE_SUCCESS){
2600         return status;
2601     }
2602     bool added = btstack_linked_list_add_tail(&gatt_client->write_without_response_requests, (btstack_linked_item_t*) callback_registration);
2603     att_dispatch_client_request_can_send_now_event(gatt_client->con_handle);
2604     if (added){
2605         return ERROR_CODE_SUCCESS;
2606     } else {
2607         return ERROR_CODE_COMMAND_DISALLOWED;
2608     }
2609 }
2610 
2611 uint8_t gatt_client_request_to_send_gatt_query(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
2612     gatt_client_t * gatt_client;
2613     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
2614     if (status != ERROR_CODE_SUCCESS){
2615         return status;
2616     }
2617     bool added = btstack_linked_list_add_tail(&gatt_client->query_requests, (btstack_linked_item_t*) callback_registration);
2618     gatt_client_notify_can_send_query(gatt_client);
2619     if (added){
2620         return ERROR_CODE_SUCCESS;
2621     } else {
2622         return ERROR_CODE_COMMAND_DISALLOWED;
2623     }
2624 }
2625 
2626 uint8_t gatt_client_request_can_write_without_response_event(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
2627     gatt_client_t * gatt_client;
2628     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
2629     if (status != ERROR_CODE_SUCCESS){
2630         return status;
2631     }
2632     if (gatt_client->write_without_response_callback != NULL){
2633         return GATT_CLIENT_IN_WRONG_STATE;
2634     }
2635     gatt_client->write_without_response_callback = callback;
2636     att_dispatch_client_request_can_send_now_event(gatt_client->con_handle);
2637     return ERROR_CODE_SUCCESS;
2638 }
2639 
2640 #ifdef ENABLE_GATT_OVER_CLASSIC
2641 
2642 #include "hci_event.h"
2643 
2644 // single active SDP query
2645 static gatt_client_t * gatt_client_classic_active_sdp_query;
2646 
2647 // macos protocol descriptor list requires 16 bytes
2648 static uint8_t gatt_client_classic_sdp_buffer[32];
2649 
2650 static const hci_event_t gatt_client_connected = {
2651         GATT_EVENT_CONNECTED, 0, "1BH"
2652 };
2653 
2654 static const hci_event_t gatt_client_disconnected = {
2655         GATT_EVENT_DISCONNECTED, 0, "H"
2656 };
2657 
2658 static gatt_client_t * gatt_client_get_context_for_classic_addr(bd_addr_t addr){
2659     btstack_linked_item_t *it;
2660     for (it = (btstack_linked_item_t *) gatt_client_connections; it != NULL; it = it->next){
2661         gatt_client_t * gatt_client = (gatt_client_t *) it;
2662         if (memcmp(gatt_client->addr, addr, 6) == 0){
2663             return gatt_client;
2664         }
2665     }
2666     return NULL;
2667 }
2668 
2669 static gatt_client_t * gatt_client_get_context_for_l2cap_cid(uint16_t l2cap_cid){
2670     btstack_linked_item_t *it;
2671     for (it = (btstack_linked_item_t *) gatt_client_connections; it != NULL; it = it->next){
2672         gatt_client_t * gatt_client = (gatt_client_t *) it;
2673         if (gatt_client->l2cap_cid == l2cap_cid){
2674             return gatt_client;
2675         }
2676     }
2677     return NULL;
2678 }
2679 
2680 static void gatt_client_classic_handle_connected(gatt_client_t * gatt_client, uint8_t status){
2681     bd_addr_t addr;
2682     // cppcheck-suppress uninitvar ; addr is reported as uninitialized although it's the destination of the memcpy
2683     memcpy(addr, gatt_client->addr, 6);
2684     hci_con_handle_t con_handle = gatt_client->con_handle;
2685     btstack_packet_handler_t callback = gatt_client->callback;
2686     if (status != ERROR_CODE_SUCCESS){
2687         btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) gatt_client);
2688         btstack_memory_gatt_client_free(gatt_client);
2689     }
2690     uint8_t buffer[20];
2691     uint16_t len = hci_event_create_from_template_and_arguments(buffer, sizeof(buffer), &gatt_client_connected, status, addr,
2692                                                                 con_handle);
2693     (*callback)(HCI_EVENT_PACKET, 0, buffer, len);
2694 }
2695 
2696 static void gatt_client_classic_handle_disconnected(gatt_client_t * gatt_client){
2697 
2698     gatt_client_report_error_if_pending(gatt_client, ATT_ERROR_HCI_DISCONNECT_RECEIVED);
2699     gatt_client_timeout_stop(gatt_client);
2700 
2701     hci_con_handle_t con_handle = gatt_client->con_handle;
2702     btstack_packet_handler_t callback = gatt_client->callback;
2703     btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) gatt_client);
2704     btstack_memory_gatt_client_free(gatt_client);
2705 
2706     uint8_t buffer[20];
2707     uint16_t len = hci_event_create_from_template_and_arguments(buffer, sizeof(buffer), &gatt_client_disconnected, con_handle);
2708     (*callback)(HCI_EVENT_PACKET, 0, buffer, len);
2709 }
2710 
2711 static void gatt_client_l2cap_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2712     gatt_client_t * gatt_client = NULL;
2713     uint8_t status;
2714     switch (packet_type){
2715         case HCI_EVENT_PACKET:
2716             switch (hci_event_packet_get_type(packet)) {
2717                 case L2CAP_EVENT_CHANNEL_OPENED:
2718                     status = l2cap_event_channel_opened_get_status(packet);
2719                     gatt_client = gatt_client_get_context_for_l2cap_cid(l2cap_event_channel_opened_get_local_cid(packet));
2720                     btstack_assert(gatt_client != NULL);
2721                     // if status != 0, gatt_client will be discarded
2722                     gatt_client->gatt_client_state = P_READY;
2723                     gatt_client->con_handle = l2cap_event_channel_opened_get_handle(packet);
2724                     gatt_client->mtu = l2cap_event_channel_opened_get_remote_mtu(packet);
2725                     gatt_client_classic_handle_connected(gatt_client, status);
2726                     break;
2727                 case L2CAP_EVENT_CHANNEL_CLOSED:
2728                     gatt_client = gatt_client_get_context_for_l2cap_cid(l2cap_event_channel_closed_get_local_cid(packet));
2729                     gatt_client_classic_handle_disconnected(gatt_client);
2730                     break;
2731                 default:
2732                     break;
2733             }
2734             break;
2735         case L2CAP_DATA_PACKET:
2736             gatt_client = gatt_client_get_context_for_l2cap_cid(channel);
2737             btstack_assert(gatt_client != NULL);
2738             gatt_client_handle_att_response(gatt_client, packet, size);
2739             gatt_client_run();
2740             break;
2741         default:
2742             break;
2743     }
2744 }
2745 
2746 static void gatt_client_handle_sdp_client_query_attribute_value(gatt_client_t * connection, uint8_t *packet){
2747     des_iterator_t des_list_it;
2748     des_iterator_t prot_it;
2749 
2750     if (sdp_event_query_attribute_byte_get_attribute_length(packet) <= sizeof(gatt_client_classic_sdp_buffer)) {
2751         gatt_client_classic_sdp_buffer[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet);
2752         if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)) {
2753             switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) {
2754                 case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST:
2755                     for (des_iterator_init(&des_list_it, gatt_client_classic_sdp_buffer); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
2756                         uint8_t       *des_element;
2757                         uint8_t       *element;
2758                         uint32_t       uuid;
2759 
2760                         if (des_iterator_get_type(&des_list_it) != DE_DES) continue;
2761 
2762                         des_element = des_iterator_get_element(&des_list_it);
2763                         des_iterator_init(&prot_it, des_element);
2764                         element = des_iterator_get_element(&prot_it);
2765 
2766                         if (de_get_element_type(element) != DE_UUID) continue;
2767 
2768                         uuid = de_get_uuid32(element);
2769                         des_iterator_next(&prot_it);
2770                         // we assume that the even if there are both roles supported, remote device uses the same psm and avdtp version for both
2771                         switch (uuid){
2772                             case BLUETOOTH_PROTOCOL_L2CAP:
2773                                 if (!des_iterator_has_more(&prot_it)) continue;
2774                                 de_element_get_uint16(des_iterator_get_element(&prot_it), &connection->l2cap_psm);
2775                                 break;
2776                             default:
2777                                 break;
2778                         }
2779                     }
2780                     break;
2781 
2782                 default:
2783                     break;
2784             }
2785         }
2786     }
2787 }
2788 
2789 static void gatt_client_classic_sdp_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
2790     gatt_client_t * gatt_client = gatt_client_classic_active_sdp_query;
2791     btstack_assert(gatt_client != NULL);
2792     uint8_t status;
2793 
2794     // TODO: handle sdp events, get l2cap psm
2795     switch (hci_event_packet_get_type(packet)){
2796         case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
2797             gatt_client_handle_sdp_client_query_attribute_value(gatt_client, packet);
2798             // TODO:
2799             return;
2800         case SDP_EVENT_QUERY_COMPLETE:
2801             status = sdp_event_query_complete_get_status(packet);
2802             gatt_client_classic_active_sdp_query = NULL;
2803             log_info("l2cap psm: %0x, status %02x", gatt_client->l2cap_psm, status);
2804             if (status != ERROR_CODE_SUCCESS) break;
2805             if (gatt_client->l2cap_psm == 0) {
2806                 status = SDP_SERVICE_NOT_FOUND;
2807                 break;
2808             }
2809             break;
2810         default:
2811             btstack_assert(false);
2812             return;
2813     }
2814 
2815     // done
2816     if (status == ERROR_CODE_SUCCESS){
2817         gatt_client->gatt_client_state = P_W4_L2CAP_CONNECTION;
2818         status = l2cap_create_channel(gatt_client_l2cap_handler, gatt_client->addr, gatt_client->l2cap_psm, 0xffff,
2819                              &gatt_client->l2cap_cid);
2820     }
2821     if (status != ERROR_CODE_SUCCESS) {
2822         gatt_client_classic_handle_connected(gatt_client, status);
2823     }
2824 }
2825 
2826 static void gatt_client_classic_sdp_start(void * context){
2827     gatt_client_classic_active_sdp_query = (gatt_client_t *) context;
2828     gatt_client_classic_active_sdp_query->gatt_client_state = P_W4_SDP_QUERY;
2829     sdp_client_query_uuid16(gatt_client_classic_sdp_handler, gatt_client_classic_active_sdp_query->addr, ORG_BLUETOOTH_SERVICE_GENERIC_ATTRIBUTE);
2830 }
2831 
2832 uint8_t gatt_client_classic_connect(btstack_packet_handler_t callback, bd_addr_t addr){
2833     gatt_client_t * gatt_client = gatt_client_get_context_for_classic_addr(addr);
2834     if (gatt_client != NULL){
2835         return ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS;
2836     }
2837     gatt_client = btstack_memory_gatt_client_get();
2838     if (gatt_client == NULL){
2839         return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED;
2840     }
2841     // init state
2842     gatt_client->bearer_type = ATT_BEARER_UNENHANCED_CLASSIC;
2843     gatt_client->con_handle = HCI_CON_HANDLE_INVALID;
2844     memcpy(gatt_client->addr, addr, 6);
2845     gatt_client->mtu = ATT_DEFAULT_MTU;
2846     gatt_client->security_level = LEVEL_0;
2847     gatt_client->mtu_state = MTU_AUTO_EXCHANGE_DISABLED;
2848     gatt_client->gatt_client_state = P_W2_SDP_QUERY;
2849     gatt_client->sdp_query_request.callback = &gatt_client_classic_sdp_start;
2850     gatt_client->sdp_query_request.context = gatt_client;
2851     gatt_client->callback = callback;
2852     btstack_linked_list_add(&gatt_client_connections, (btstack_linked_item_t*)gatt_client);
2853     sdp_client_register_query_callback(&gatt_client->sdp_query_request);
2854     return ERROR_CODE_SUCCESS;
2855 }
2856 
2857 uint8_t gatt_client_classic_disconnect(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
2858     gatt_client_t * gatt_client = gatt_client_get_context_for_handle(con_handle);
2859     if (gatt_client == NULL){
2860         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2861     }
2862     gatt_client->callback = callback;
2863     return l2cap_disconnect(gatt_client->l2cap_cid);
2864 }
2865 #endif
2866 
2867 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
2868 void gatt_client_att_packet_handler_fuzz(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
2869     gatt_client_att_packet_handler(packet_type, handle, packet, size);
2870 }
2871 
2872 uint8_t gatt_client_get_client(hci_con_handle_t con_handle, gatt_client_t ** out_gatt_client){
2873     uint8_t status = gatt_client_provide_context_for_handle(con_handle, out_gatt_client);
2874     return status;
2875 }
2876 #endif
2877