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