xref: /btstack/src/ble/gatt_client.c (revision 1aa9e3e881e80275d21a99a71d9aa1ff99a8772b)
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             uint16_t remote_rx_mtu = little_endian_read_16(packet, 1);
1423             uint16_t local_rx_mtu = l2cap_max_le_mtu();
1424             uint16_t mtu = (remote_rx_mtu < local_rx_mtu) ? remote_rx_mtu : local_rx_mtu;
1425 
1426             // set gatt client mtu
1427             gatt_client->mtu = mtu;
1428             gatt_client->mtu_state = MTU_EXCHANGED;
1429 
1430             // set per connection mtu state - for fixed channel
1431             hci_connection_t *hci_connection = hci_connection_for_handle(gatt_client->con_handle);
1432             hci_connection->att_connection.mtu = gatt_client->mtu;
1433             hci_connection->att_connection.mtu_exchanged = true;
1434 
1435             emit_gatt_mtu_exchanged_result_event(gatt_client, gatt_client->mtu);
1436             break;
1437         }
1438         case ATT_READ_BY_GROUP_TYPE_RESPONSE:
1439             switch (gatt_client->gatt_client_state) {
1440                 case P_W4_SERVICE_QUERY_RESULT:
1441                     report_gatt_services(gatt_client, packet, size);
1442                     trigger_next_service_query(gatt_client, get_last_result_handle_from_service_list(packet, size));
1443                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1444                     break;
1445                 default:
1446                     break;
1447             }
1448             break;
1449         case ATT_HANDLE_VALUE_NOTIFICATION:
1450             if (size < 3u) return;
1451             report_gatt_notification(gatt_client, little_endian_read_16(packet, 1u), &packet[3], size - 3u);
1452             return;
1453         case ATT_HANDLE_VALUE_INDICATION:
1454             if (size < 3u) break;
1455             report_gatt_indication(gatt_client, little_endian_read_16(packet, 1u), &packet[3], size - 3u);
1456             gatt_client->send_confirmation = 1;
1457             break;
1458 
1459         case ATT_READ_BY_TYPE_RESPONSE:
1460             switch (gatt_client->gatt_client_state) {
1461                 case P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT:
1462                     report_gatt_characteristics(gatt_client, packet, size);
1463                     trigger_next_characteristic_query(gatt_client,
1464                                                       get_last_result_handle_from_characteristics_list(packet, size));
1465                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done, or by ATT_ERROR
1466                     break;
1467                 case P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT:
1468                     report_gatt_characteristics(gatt_client, packet, size);
1469                     trigger_next_characteristic_query(gatt_client,
1470                                                       get_last_result_handle_from_characteristics_list(packet, size));
1471                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done, or by ATT_ERROR
1472                     break;
1473                 case P_W4_INCLUDED_SERVICE_QUERY_RESULT: {
1474                     if (size < 2u) break;
1475                     uint16_t uuid16 = 0;
1476                     uint16_t pair_size = packet[1];
1477 
1478                     if (pair_size == 6u) {
1479                         if (size < 8u) break;
1480                         // UUIDs not available, query first included service
1481                         gatt_client->start_group_handle = little_endian_read_16(packet, 2); // ready for next query
1482                         gatt_client->query_start_handle = little_endian_read_16(packet, 4);
1483                         gatt_client->query_end_handle = little_endian_read_16(packet, 6);
1484                         gatt_client->gatt_client_state = P_W2_SEND_INCLUDED_SERVICE_WITH_UUID_QUERY;
1485                         break;
1486                     }
1487 
1488                     if (pair_size != 8u) break;
1489 
1490                     // UUIDs included, report all of them
1491                     uint16_t offset;
1492                     for (offset = 2u; (offset + 8u) <= size; offset += pair_size) {
1493                         uint16_t include_handle = little_endian_read_16(packet, offset);
1494                         gatt_client->query_start_handle = little_endian_read_16(packet, offset + 2u);
1495                         gatt_client->query_end_handle = little_endian_read_16(packet, offset + 4u);
1496                         uuid16 = little_endian_read_16(packet, offset + 6u);
1497                         report_gatt_included_service_uuid16(gatt_client, include_handle, uuid16);
1498                     }
1499 
1500                     trigger_next_included_service_query(gatt_client,
1501                                                         get_last_result_handle_from_included_services_list(packet,
1502                                                                                                            size));
1503                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1504                     break;
1505                 }
1506 #ifndef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY
1507                 case P_W4_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT:
1508                     gatt_client->client_characteristic_configuration_handle = little_endian_read_16(packet, 2);
1509                     gatt_client->gatt_client_state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION;
1510                     break;
1511 #endif
1512                 case P_W4_READ_BY_TYPE_RESPONSE: {
1513                     uint16_t pair_size = packet[1];
1514                     // set last result handle to last valid handle, only used if pair_size invalid
1515                     uint16_t last_result_handle = 0xffff;
1516                     if (pair_size > 2) {
1517                         uint16_t offset;
1518                         for (offset = 2; offset < size; offset += pair_size) {
1519                             uint16_t value_handle = little_endian_read_16(packet, offset);
1520                             report_gatt_characteristic_value(gatt_client, value_handle, &packet[offset + 2u],
1521                                                              pair_size - 2u);
1522                             last_result_handle = value_handle;
1523                         }
1524                     }
1525                     trigger_next_read_by_type_query(gatt_client, last_result_handle);
1526                     break;
1527                 }
1528                 default:
1529                     break;
1530             }
1531             break;
1532         case ATT_READ_RESPONSE:
1533             switch (gatt_client->gatt_client_state) {
1534                 case P_W4_INCLUDED_SERVICE_UUID_WITH_QUERY_RESULT:
1535                     if (size >= 17) {
1536                         uint8_t uuid128[16];
1537                         reverse_128(&packet[1], uuid128);
1538                         report_gatt_included_service_uuid128(gatt_client, gatt_client->start_group_handle, uuid128);
1539                     }
1540                     trigger_next_included_service_query(gatt_client, gatt_client->start_group_handle);
1541                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1542                     break;
1543 
1544                 case P_W4_READ_CHARACTERISTIC_VALUE_RESULT:
1545                     gatt_client_handle_transaction_complete(gatt_client);
1546                     report_gatt_characteristic_value(gatt_client, gatt_client->attribute_handle, &packet[1], size - 1u);
1547                     emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1548                     break;
1549 
1550                 case P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT:
1551                     gatt_client_handle_transaction_complete(gatt_client);
1552                     report_gatt_characteristic_descriptor(gatt_client, gatt_client->attribute_handle, &packet[1],
1553                                                           size - 1u, 0u);
1554                     emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1555                     break;
1556 
1557                     // Use ATT_READ_REQUEST for first blob of Read Long Characteristic
1558                 case P_W4_READ_BLOB_RESULT:
1559                     report_gatt_long_characteristic_value_blob(gatt_client, gatt_client->attribute_handle, &packet[1],
1560                                                                size - 1u, gatt_client->attribute_offset);
1561                     trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_QUERY, size - 1u);
1562                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1563                     break;
1564 
1565                     // Use ATT_READ_REQUEST for first blob of Read Long Characteristic Descriptor
1566                 case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT:
1567                     report_gatt_long_characteristic_descriptor(gatt_client, gatt_client->attribute_handle, &packet[1],
1568                                                                size - 1u, gatt_client->attribute_offset);
1569                     trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY,
1570                                             size - 1u);
1571                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1572                     break;
1573 
1574                 default:
1575                     break;
1576             }
1577             break;
1578 
1579         case ATT_FIND_BY_TYPE_VALUE_RESPONSE: {
1580             uint8_t pair_size = 4;
1581             int i;
1582             uint16_t start_group_handle;
1583             uint16_t end_group_handle = 0xffff; // asserts GATT_EVENT_QUERY_COMPLETE is emitted if no results
1584             for (i = 1u; (i + pair_size) <= size; i += pair_size) {
1585                 start_group_handle = little_endian_read_16(packet, i);
1586                 end_group_handle = little_endian_read_16(packet, i + 2);
1587                 emit_gatt_service_query_result_event(gatt_client, start_group_handle, end_group_handle,
1588                                                      gatt_client->uuid128);
1589             }
1590             trigger_next_service_by_uuid_query(gatt_client, end_group_handle);
1591             // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1592             break;
1593         }
1594         case ATT_FIND_INFORMATION_REPLY: {
1595             if (size < 2u) break;
1596 
1597             uint8_t pair_size = 4;
1598             if (packet[1u] == 2u) {
1599                 pair_size = 18;
1600             }
1601             uint16_t offset = 2;
1602 
1603             if (size < (pair_size + offset)) break;
1604             uint16_t last_descriptor_handle = little_endian_read_16(packet, size - pair_size);
1605 
1606 #ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY
1607             log_info("ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY, state %x", gatt_client->gatt_client_state);
1608             if (gatt_client->gatt_client_state == P_W4_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT){
1609                 // iterate over descriptors looking for CCC
1610                 if (pair_size == 4){
1611                     while ((offset + 4) <= size){
1612                         uint16_t uuid16 = little_endian_read_16(packet, offset + 2);
1613                         if (uuid16 == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION){
1614                             gatt_client->client_characteristic_configuration_handle = little_endian_read_16(packet, offset);
1615                             gatt_client->gatt_client_state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION;
1616                             log_info("CCC found %x", gatt_client->client_characteristic_configuration_handle);
1617                             break;
1618                         }
1619                         offset += pair_size;
1620                     }
1621                 }
1622                 if (is_query_done(gatt_client, last_descriptor_handle)){
1623 
1624                 } else {
1625                     // next
1626                     gatt_client->start_group_handle = last_descriptor_handle + 1;
1627                     gatt_client->gatt_client_state = P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY;
1628                 }
1629                 break;
1630             }
1631 #endif
1632             report_gatt_all_characteristic_descriptors(gatt_client, &packet[2], size - 2u, pair_size);
1633             trigger_next_characteristic_descriptor_query(gatt_client, last_descriptor_handle);
1634             // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1635             break;
1636         }
1637 
1638         case ATT_WRITE_RESPONSE:
1639             switch (gatt_client->gatt_client_state) {
1640                 case P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT:
1641                     gatt_client_handle_transaction_complete(gatt_client);
1642                     emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1643                     break;
1644                 case P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT:
1645                     gatt_client_handle_transaction_complete(gatt_client);
1646                     emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1647                     break;
1648                 case P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
1649                     gatt_client_handle_transaction_complete(gatt_client);
1650                     emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1651                     break;
1652                 default:
1653                     break;
1654             }
1655             break;
1656 
1657         case ATT_READ_BLOB_RESPONSE: {
1658             uint16_t received_blob_length = size - 1u;
1659             switch (gatt_client->gatt_client_state) {
1660                 case P_W4_READ_BLOB_RESULT:
1661                     report_gatt_long_characteristic_value_blob(gatt_client, gatt_client->attribute_handle, &packet[1],
1662                                                                received_blob_length, gatt_client->attribute_offset);
1663                     trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_QUERY, received_blob_length);
1664                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1665                     break;
1666                 case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT:
1667                     report_gatt_long_characteristic_descriptor(gatt_client, gatt_client->attribute_handle,
1668                                                                &packet[1], received_blob_length,
1669                                                                gatt_client->attribute_offset);
1670                     trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY,
1671                                             received_blob_length);
1672                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1673                     break;
1674                 default:
1675                     break;
1676             }
1677             break;
1678         }
1679         case ATT_PREPARE_WRITE_RESPONSE:
1680             switch (gatt_client->gatt_client_state) {
1681                 case P_W4_PREPARE_WRITE_SINGLE_RESULT:
1682                     gatt_client_handle_transaction_complete(gatt_client);
1683                     if (is_value_valid(gatt_client, packet, size)) {
1684                         emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1685                     } else {
1686                         emit_gatt_complete_event(gatt_client, ATT_ERROR_DATA_MISMATCH);
1687                     }
1688                     break;
1689 
1690                 case P_W4_PREPARE_WRITE_RESULT: {
1691                     gatt_client->attribute_offset = little_endian_read_16(packet, 3);
1692                     trigger_next_prepare_write_query(gatt_client, P_W2_PREPARE_WRITE, P_W2_EXECUTE_PREPARED_WRITE);
1693                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1694                     break;
1695                 }
1696                 case P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT: {
1697                     gatt_client->attribute_offset = little_endian_read_16(packet, 3);
1698                     trigger_next_prepare_write_query(gatt_client, P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR,
1699                                                      P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR);
1700                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1701                     break;
1702                 }
1703                 case P_W4_PREPARE_RELIABLE_WRITE_RESULT: {
1704                     if (is_value_valid(gatt_client, packet, size)) {
1705                         gatt_client->attribute_offset = little_endian_read_16(packet, 3);
1706                         trigger_next_prepare_write_query(gatt_client, P_W2_PREPARE_RELIABLE_WRITE,
1707                                                          P_W2_EXECUTE_PREPARED_WRITE);
1708                         // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1709                         break;
1710                     }
1711                     gatt_client->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH;
1712                     break;
1713                 }
1714                 default:
1715                     break;
1716             }
1717             break;
1718 
1719         case ATT_EXECUTE_WRITE_RESPONSE:
1720             switch (gatt_client->gatt_client_state) {
1721                 case P_W4_EXECUTE_PREPARED_WRITE_RESULT:
1722                     gatt_client_handle_transaction_complete(gatt_client);
1723                     emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1724                     break;
1725                 case P_W4_CANCEL_PREPARED_WRITE_RESULT:
1726                     gatt_client_handle_transaction_complete(gatt_client);
1727                     emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1728                     break;
1729                 case P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT:
1730                     gatt_client_handle_transaction_complete(gatt_client);
1731                     emit_gatt_complete_event(gatt_client, ATT_ERROR_DATA_MISMATCH);
1732                     break;
1733                 case P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
1734                     gatt_client_handle_transaction_complete(gatt_client);
1735                     emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1736                     break;
1737                 default:
1738                     break;
1739 
1740             }
1741             break;
1742 
1743         case ATT_READ_MULTIPLE_RESPONSE:
1744             switch (gatt_client->gatt_client_state) {
1745                 case P_W4_READ_MULTIPLE_RESPONSE:
1746                     report_gatt_characteristic_value(gatt_client, 0u, &packet[1], size - 1u);
1747                     gatt_client_handle_transaction_complete(gatt_client);
1748                     emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1749                     break;
1750                 default:
1751                     break;
1752             }
1753             break;
1754 
1755         case ATT_ERROR_RESPONSE:
1756             if (size < 5u) return;
1757             error_code = packet[4];
1758             switch (error_code) {
1759                 case ATT_ERROR_ATTRIBUTE_NOT_FOUND: {
1760                     switch (gatt_client->gatt_client_state) {
1761                         case P_W4_SERVICE_QUERY_RESULT:
1762                         case P_W4_SERVICE_WITH_UUID_RESULT:
1763                         case P_W4_INCLUDED_SERVICE_QUERY_RESULT:
1764                         case P_W4_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT:
1765                             gatt_client_handle_transaction_complete(gatt_client);
1766                             emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1767                             break;
1768                         case P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT:
1769                         case P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT:
1770                             characteristic_end_found(gatt_client, gatt_client->end_group_handle);
1771                             gatt_client_handle_transaction_complete(gatt_client);
1772                             emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1773                             break;
1774                         case P_W4_READ_BY_TYPE_RESPONSE:
1775                             gatt_client_handle_transaction_complete(gatt_client);
1776                             if (gatt_client->start_group_handle == gatt_client->query_start_handle) {
1777                                 emit_gatt_complete_event(gatt_client, ATT_ERROR_ATTRIBUTE_NOT_FOUND);
1778                             } else {
1779                                 emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1780                             }
1781                             break;
1782                         default:
1783                             gatt_client_report_error_if_pending(gatt_client, error_code);
1784                             break;
1785                     }
1786                     break;
1787                 }
1788 
1789 #ifdef ENABLE_GATT_CLIENT_PAIRING
1790 
1791                     case ATT_ERROR_INSUFFICIENT_AUTHENTICATION:
1792                     case ATT_ERROR_INSUFFICIENT_ENCRYPTION_KEY_SIZE:
1793                     case ATT_ERROR_INSUFFICIENT_ENCRYPTION: {
1794 
1795                         // security too low
1796                         if (gatt_client->security_counter > 0) {
1797                             gatt_client_report_error_if_pending(gatt_client, error_code);
1798                             break;
1799                         }
1800                         // start security
1801                         gatt_client->security_counter++;
1802 
1803                         // setup action
1804                         int retry = 1;
1805                         switch (gatt_client->gatt_client_state){
1806                             case P_W4_READ_CHARACTERISTIC_VALUE_RESULT:
1807                                 gatt_client->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY ;
1808                                 break;
1809                             case P_W4_READ_BLOB_RESULT:
1810                                 gatt_client->gatt_client_state = P_W2_SEND_READ_BLOB_QUERY;
1811                                 break;
1812                             case P_W4_READ_BY_TYPE_RESPONSE:
1813                                 gatt_client->gatt_client_state = P_W2_SEND_READ_BY_TYPE_REQUEST;
1814                                 break;
1815                             case P_W4_READ_MULTIPLE_RESPONSE:
1816                                 gatt_client->gatt_client_state = P_W2_SEND_READ_MULTIPLE_REQUEST;
1817                                 break;
1818                             case P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT:
1819                                 gatt_client->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_VALUE;
1820                                 break;
1821                             case P_W4_PREPARE_WRITE_RESULT:
1822                                 gatt_client->gatt_client_state = P_W2_PREPARE_WRITE;
1823                                 break;
1824                             case P_W4_PREPARE_WRITE_SINGLE_RESULT:
1825                                 gatt_client->gatt_client_state = P_W2_PREPARE_WRITE_SINGLE;
1826                                 break;
1827                             case P_W4_PREPARE_RELIABLE_WRITE_RESULT:
1828                                 gatt_client->gatt_client_state = P_W2_PREPARE_RELIABLE_WRITE;
1829                                 break;
1830                             case P_W4_EXECUTE_PREPARED_WRITE_RESULT:
1831                                 gatt_client->gatt_client_state = P_W2_EXECUTE_PREPARED_WRITE;
1832                                 break;
1833                             case P_W4_CANCEL_PREPARED_WRITE_RESULT:
1834                                 gatt_client->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE;
1835                                 break;
1836                             case P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT:
1837                                 gatt_client->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH;
1838                                 break;
1839                             case P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT:
1840                                 gatt_client->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY;
1841                                 break;
1842                             case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT:
1843                                 gatt_client->gatt_client_state = P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY;
1844                                 break;
1845                             case P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
1846                                 gatt_client->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR;
1847                                 break;
1848                             case P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT:
1849                                 gatt_client->gatt_client_state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION;
1850                                 break;
1851                             case P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
1852                                 gatt_client->gatt_client_state = P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR;
1853                                 break;
1854                             case P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
1855                                 gatt_client->gatt_client_state = P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR;
1856                                 break;
1857 #ifdef ENABLE_LE_SIGNED_WRITE
1858                             case P_W4_SEND_SINGED_WRITE_DONE:
1859                                 gatt_client->gatt_client_state = P_W2_SEND_SIGNED_WRITE;
1860                                 break;
1861 #endif
1862                             default:
1863                                 log_info("retry not supported for state %x", gatt_client->gatt_client_state);
1864                                 retry = 0;
1865                                 break;
1866                         }
1867 
1868                         if (!retry) {
1869                             gatt_client_report_error_if_pending(gatt_client, error_code);
1870                             break;
1871                         }
1872 
1873                         log_info("security error, start pairing");
1874 
1875                         // start pairing for higher security level
1876                         gatt_client->wait_for_authentication_complete = 1;
1877                         gatt_client->pending_error_code = error_code;
1878                         sm_request_pairing(gatt_client->con_handle);
1879                         break;
1880                     }
1881 #endif
1882 
1883                     // nothing we can do about that
1884                 case ATT_ERROR_INSUFFICIENT_AUTHORIZATION:
1885                 default:
1886                     gatt_client_report_error_if_pending(gatt_client, error_code);
1887                     break;
1888             }
1889             break;
1890 
1891         default:
1892             log_info("ATT Handler, unhandled response type 0x%02x", packet[0]);
1893             break;
1894     }
1895 }
1896 
1897 static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size) {
1898     gatt_client_t *gatt_client;
1899     if (size < 1u) return;
1900 
1901     if (packet_type == HCI_EVENT_PACKET) {
1902         switch (packet[0]) {
1903             case L2CAP_EVENT_CAN_SEND_NOW:
1904                 gatt_client_run();
1905                 break;
1906                 // att_server has negotiated the mtu for this connection, cache if context exists
1907             case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
1908                 if (size < 6u) break;
1909                 gatt_client = gatt_client_get_context_for_handle(handle);
1910                 if (gatt_client == NULL) break;
1911                 gatt_client->mtu = little_endian_read_16(packet, 4);
1912                 break;
1913             default:
1914                 break;
1915         }
1916         return;
1917     }
1918 
1919     if (packet_type != ATT_DATA_PACKET) return;
1920 
1921     // special cases: notifications & indications motivate creating context
1922     switch (packet[0]) {
1923         case ATT_HANDLE_VALUE_NOTIFICATION:
1924         case ATT_HANDLE_VALUE_INDICATION:
1925             gatt_client_provide_context_for_handle(handle, &gatt_client);
1926             break;
1927         default:
1928             gatt_client = gatt_client_get_context_for_handle(handle);
1929             break;
1930     }
1931 
1932     if (gatt_client != NULL) {
1933         gatt_client_handle_att_response(gatt_client, packet, size);
1934         gatt_client_run();
1935     }
1936 }
1937 
1938 #ifdef ENABLE_LE_SIGNED_WRITE
1939 static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
1940     btstack_linked_list_iterator_t it;
1941     btstack_linked_list_iterator_init(&it, &gatt_client_connections);
1942     while (btstack_linked_list_iterator_has_next(&it)){
1943         gatt_client_t * gatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it);
1944         if (gatt_client->gatt_client_state == P_W4_CMAC_RESULT){
1945             // store result
1946             (void)memcpy(gatt_client->cmac, hash, 8);
1947             // reverse_64(hash, gatt_client->cmac);
1948             gatt_client->gatt_client_state = P_W2_SEND_SIGNED_WRITE;
1949             gatt_client_run();
1950             return;
1951         }
1952     }
1953 }
1954 
1955 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){
1956     gatt_client_t * gatt_client;
1957     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
1958     if (status != ERROR_CODE_SUCCESS){
1959         return status;
1960     }
1961     if (is_ready(gatt_client) == 0){
1962         return GATT_CLIENT_IN_WRONG_STATE;
1963     }
1964 
1965     gatt_client->callback = callback;
1966     gatt_client->attribute_handle = value_handle;
1967     gatt_client->attribute_length = message_len;
1968     gatt_client->attribute_value = message;
1969     gatt_client->gatt_client_state = P_W4_IDENTITY_RESOLVING;
1970     gatt_client_run();
1971     return ERROR_CODE_SUCCESS;
1972 }
1973 #endif
1974 
1975 uint8_t gatt_client_discover_primary_services(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
1976     gatt_client_t * gatt_client;
1977     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
1978     if (status != ERROR_CODE_SUCCESS){
1979         return status;
1980     }
1981     if (is_ready(gatt_client) == 0){
1982         return GATT_CLIENT_IN_WRONG_STATE;
1983     }
1984 
1985     gatt_client->callback = callback;
1986     gatt_client->start_group_handle = 0x0001;
1987     gatt_client->end_group_handle   = 0xffff;
1988     gatt_client->gatt_client_state = P_W2_SEND_SERVICE_QUERY;
1989     gatt_client->uuid16 = GATT_PRIMARY_SERVICE_UUID;
1990     gatt_client_run();
1991     return ERROR_CODE_SUCCESS;
1992 }
1993 
1994 uint8_t gatt_client_discover_secondary_services(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
1995     gatt_client_t * gatt_client;
1996     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
1997     if (status != ERROR_CODE_SUCCESS){
1998         return status;
1999     }
2000     if (is_ready(gatt_client) == 0){
2001         return GATT_CLIENT_IN_WRONG_STATE;
2002     }
2003 
2004     gatt_client->callback = callback;
2005     gatt_client->start_group_handle = 0x0001;
2006     gatt_client->end_group_handle   = 0xffff;
2007     gatt_client->gatt_client_state = P_W2_SEND_SERVICE_QUERY;
2008     gatt_client->uuid16 = GATT_SECONDARY_SERVICE_UUID;
2009     gatt_client_run();
2010     return ERROR_CODE_SUCCESS;
2011 }
2012 
2013 uint8_t gatt_client_discover_primary_services_by_uuid16(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t uuid16){
2014     gatt_client_t * gatt_client;
2015     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2016     if (status != ERROR_CODE_SUCCESS){
2017         return status;
2018     }
2019     if (is_ready(gatt_client) == 0){
2020         return GATT_CLIENT_IN_WRONG_STATE;
2021     }
2022 
2023     gatt_client->callback = callback;
2024     gatt_client->start_group_handle = 0x0001;
2025     gatt_client->end_group_handle   = 0xffff;
2026     gatt_client->gatt_client_state = P_W2_SEND_SERVICE_WITH_UUID_QUERY;
2027     gatt_client->uuid16 = uuid16;
2028     uuid_add_bluetooth_prefix((uint8_t*) &(gatt_client->uuid128), gatt_client->uuid16);
2029     gatt_client_run();
2030     return ERROR_CODE_SUCCESS;
2031 }
2032 
2033 uint8_t gatt_client_discover_primary_services_by_uuid128(btstack_packet_handler_t callback, hci_con_handle_t con_handle, const uint8_t * uuid128){
2034     gatt_client_t * gatt_client;
2035     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2036     if (status != ERROR_CODE_SUCCESS){
2037         return status;
2038     }
2039     if (is_ready(gatt_client) == 0){
2040         return GATT_CLIENT_IN_WRONG_STATE;
2041     }
2042 
2043     gatt_client->callback = callback;
2044     gatt_client->start_group_handle = 0x0001;
2045     gatt_client->end_group_handle   = 0xffff;
2046     gatt_client->uuid16 = 0;
2047     (void)memcpy(gatt_client->uuid128, uuid128, 16);
2048     gatt_client->gatt_client_state = P_W2_SEND_SERVICE_WITH_UUID_QUERY;
2049     gatt_client_run();
2050     return ERROR_CODE_SUCCESS;
2051 }
2052 
2053 uint8_t gatt_client_discover_characteristics_for_service(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_service_t * service){
2054     gatt_client_t * gatt_client;
2055     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2056     if (status != ERROR_CODE_SUCCESS){
2057         return status;
2058     }
2059     if (is_ready(gatt_client) == 0){
2060         return GATT_CLIENT_IN_WRONG_STATE;
2061     }
2062 
2063     gatt_client->callback = callback;
2064     gatt_client->start_group_handle = service->start_group_handle;
2065     gatt_client->end_group_handle   = service->end_group_handle;
2066     gatt_client->filter_with_uuid = 0;
2067     gatt_client->characteristic_start_handle = 0;
2068     gatt_client->gatt_client_state = P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY;
2069     gatt_client_run();
2070     return ERROR_CODE_SUCCESS;
2071 }
2072 
2073 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){
2074     gatt_client_t * gatt_client;
2075     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2076     if (status != ERROR_CODE_SUCCESS){
2077         return status;
2078     }
2079     if (is_ready(gatt_client) == 0){
2080         return GATT_CLIENT_IN_WRONG_STATE;
2081     }
2082     gatt_client->callback = callback;
2083     gatt_client->start_group_handle = service->start_group_handle;
2084     gatt_client->end_group_handle   = service->end_group_handle;
2085     gatt_client->gatt_client_state = P_W2_SEND_INCLUDED_SERVICE_QUERY;
2086 
2087     gatt_client_run();
2088     return ERROR_CODE_SUCCESS;
2089 }
2090 
2091 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){
2092     gatt_client_t * gatt_client;
2093     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2094     if (status != ERROR_CODE_SUCCESS){
2095         return status;
2096     }
2097     if (is_ready(gatt_client) == 0){
2098         return GATT_CLIENT_IN_WRONG_STATE;
2099     }
2100 
2101     gatt_client->callback = callback;
2102     gatt_client->start_group_handle = start_handle;
2103     gatt_client->end_group_handle   = end_handle;
2104     gatt_client->filter_with_uuid = 1;
2105     gatt_client->uuid16 = uuid16;
2106     uuid_add_bluetooth_prefix((uint8_t*) &(gatt_client->uuid128), uuid16);
2107     gatt_client->characteristic_start_handle = 0;
2108     gatt_client->gatt_client_state = P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY;
2109     gatt_client_run();
2110     return ERROR_CODE_SUCCESS;
2111 }
2112 
2113 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){
2114     gatt_client_t * gatt_client;
2115     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2116     if (status != ERROR_CODE_SUCCESS){
2117         return status;
2118     }
2119     if (is_ready(gatt_client) == 0){
2120         return GATT_CLIENT_IN_WRONG_STATE;
2121     }
2122 
2123     gatt_client->callback = callback;
2124     gatt_client->start_group_handle = start_handle;
2125     gatt_client->end_group_handle   = end_handle;
2126     gatt_client->filter_with_uuid = 1;
2127     gatt_client->uuid16 = 0;
2128     (void)memcpy(gatt_client->uuid128, uuid128, 16);
2129     gatt_client->characteristic_start_handle = 0;
2130     gatt_client->gatt_client_state = P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY;
2131     gatt_client_run();
2132     return ERROR_CODE_SUCCESS;
2133 }
2134 
2135 
2136 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){
2137     return gatt_client_discover_characteristics_for_handle_range_by_uuid16(callback, con_handle, service->start_group_handle, service->end_group_handle, uuid16);
2138 }
2139 
2140 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){
2141     return gatt_client_discover_characteristics_for_handle_range_by_uuid128(callback, con_handle, service->start_group_handle, service->end_group_handle, uuid128);
2142 }
2143 
2144 uint8_t gatt_client_discover_characteristic_descriptors(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic){
2145     gatt_client_t * gatt_client;
2146     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2147     if (status != ERROR_CODE_SUCCESS){
2148         return status;
2149     }
2150     if (is_ready(gatt_client) == 0){
2151         return GATT_CLIENT_IN_WRONG_STATE;
2152     }
2153 
2154     if (characteristic->value_handle == characteristic->end_handle){
2155         emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
2156         return ERROR_CODE_SUCCESS;
2157     }
2158     gatt_client->callback = callback;
2159     gatt_client->start_group_handle = characteristic->value_handle + 1u;
2160     gatt_client->end_group_handle   = characteristic->end_handle;
2161     gatt_client->gatt_client_state = P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY;
2162     gatt_client_run();
2163     return ERROR_CODE_SUCCESS;
2164 }
2165 
2166 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){
2167     gatt_client_t * gatt_client;
2168     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2169     if (status != ERROR_CODE_SUCCESS){
2170         return status;
2171     }
2172     if (is_ready(gatt_client) == 0){
2173         return GATT_CLIENT_IN_WRONG_STATE;
2174     }
2175 
2176     gatt_client->callback = callback;
2177     gatt_client->attribute_handle = value_handle;
2178     gatt_client->attribute_offset = 0;
2179     gatt_client->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY;
2180     gatt_client_run();
2181     return ERROR_CODE_SUCCESS;
2182 }
2183 
2184 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){
2185     gatt_client_t * gatt_client;
2186     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2187     if (status != ERROR_CODE_SUCCESS){
2188         return status;
2189     }
2190     if (is_ready(gatt_client) == 0){
2191         return GATT_CLIENT_IN_WRONG_STATE;
2192     }
2193 
2194     gatt_client->callback = callback;
2195     gatt_client->start_group_handle = start_handle;
2196     gatt_client->end_group_handle = end_handle;
2197     gatt_client->query_start_handle = start_handle;
2198     gatt_client->query_end_handle = end_handle;
2199     gatt_client->uuid16 = uuid16;
2200     uuid_add_bluetooth_prefix((uint8_t*) &(gatt_client->uuid128), uuid16);
2201     gatt_client->gatt_client_state = P_W2_SEND_READ_BY_TYPE_REQUEST;
2202     gatt_client_run();
2203     return ERROR_CODE_SUCCESS;
2204 }
2205 
2206 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){
2207     gatt_client_t * gatt_client;
2208     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2209     if (status != ERROR_CODE_SUCCESS){
2210         return status;
2211     }
2212     if (is_ready(gatt_client) == 0){
2213         return GATT_CLIENT_IN_WRONG_STATE;
2214     }
2215 
2216     gatt_client->callback = callback;
2217     gatt_client->start_group_handle = start_handle;
2218     gatt_client->end_group_handle = end_handle;
2219     gatt_client->query_start_handle = start_handle;
2220     gatt_client->query_end_handle = end_handle;
2221     gatt_client->uuid16 = 0;
2222     (void)memcpy(gatt_client->uuid128, uuid128, 16);
2223     gatt_client->gatt_client_state = P_W2_SEND_READ_BY_TYPE_REQUEST;
2224     gatt_client_run();
2225     return ERROR_CODE_SUCCESS;
2226 }
2227 
2228 
2229 uint8_t gatt_client_read_value_of_characteristic(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic){
2230     return gatt_client_read_value_of_characteristic_using_value_handle(callback, con_handle, characteristic->value_handle);
2231 }
2232 
2233 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){
2234     gatt_client_t * gatt_client;
2235     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2236     if (status != ERROR_CODE_SUCCESS){
2237         return status;
2238     }
2239     if (is_ready(gatt_client) == 0){
2240         return GATT_CLIENT_IN_WRONG_STATE;
2241     }
2242 
2243     gatt_client->callback = callback;
2244     gatt_client->attribute_handle = value_handle;
2245     gatt_client->attribute_offset = offset;
2246     gatt_client->gatt_client_state = P_W2_SEND_READ_BLOB_QUERY;
2247     gatt_client_run();
2248     return ERROR_CODE_SUCCESS;
2249 }
2250 
2251 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){
2252     return gatt_client_read_long_value_of_characteristic_using_value_handle_with_offset(callback, con_handle, value_handle, 0);
2253 }
2254 
2255 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){
2256     return gatt_client_read_long_value_of_characteristic_using_value_handle(callback, con_handle, characteristic->value_handle);
2257 }
2258 
2259 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){
2260     gatt_client_t * gatt_client;
2261     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2262     if (status != ERROR_CODE_SUCCESS){
2263         return status;
2264     }
2265     if (is_ready(gatt_client) == 0){
2266         return GATT_CLIENT_IN_WRONG_STATE;
2267     }
2268 
2269     gatt_client->callback = callback;
2270     gatt_client->read_multiple_handle_count = num_value_handles;
2271     gatt_client->read_multiple_handles = value_handles;
2272     gatt_client->gatt_client_state = P_W2_SEND_READ_MULTIPLE_REQUEST;
2273     gatt_client_run();
2274     return ERROR_CODE_SUCCESS;
2275 }
2276 
2277 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){
2278     gatt_client_t * gatt_client;
2279     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
2280     if (status != ERROR_CODE_SUCCESS){
2281         return status;
2282     }
2283 
2284     if (value_length > (gatt_client->mtu - 3u)) return GATT_CLIENT_VALUE_TOO_LONG;
2285     if (!att_dispatch_client_can_send_now(gatt_client->con_handle)) return GATT_CLIENT_BUSY;
2286 
2287     return att_write_request(gatt_client, ATT_WRITE_COMMAND, value_handle, value_length, value);
2288 }
2289 
2290 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){
2291     gatt_client_t * gatt_client;
2292     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2293     if (status != ERROR_CODE_SUCCESS){
2294         return status;
2295     }
2296     if (is_ready(gatt_client) == 0){
2297         return GATT_CLIENT_IN_WRONG_STATE;
2298     }
2299 
2300     gatt_client->callback = callback;
2301     gatt_client->attribute_handle = value_handle;
2302     gatt_client->attribute_length = value_length;
2303     gatt_client->attribute_value = value;
2304     gatt_client->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_VALUE;
2305     gatt_client_run();
2306     return ERROR_CODE_SUCCESS;
2307 }
2308 
2309 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){
2310     gatt_client_t * gatt_client;
2311     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2312     if (status != ERROR_CODE_SUCCESS){
2313         return status;
2314     }
2315     if (is_ready(gatt_client) == 0){
2316         return GATT_CLIENT_IN_WRONG_STATE;
2317     }
2318 
2319     gatt_client->callback = callback;
2320     gatt_client->attribute_handle = value_handle;
2321     gatt_client->attribute_length = value_length;
2322     gatt_client->attribute_offset = offset;
2323     gatt_client->attribute_value = value;
2324     gatt_client->gatt_client_state = P_W2_PREPARE_WRITE;
2325     gatt_client_run();
2326     return ERROR_CODE_SUCCESS;
2327 }
2328 
2329 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){
2330     return gatt_client_write_long_value_of_characteristic_with_offset(callback, con_handle, value_handle, 0, value_length, value);
2331 }
2332 
2333 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){
2334     gatt_client_t * gatt_client;
2335     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2336     if (status != ERROR_CODE_SUCCESS){
2337         return status;
2338     }
2339     if (is_ready(gatt_client) == 0){
2340         return GATT_CLIENT_IN_WRONG_STATE;
2341     }
2342 
2343     gatt_client->callback = callback;
2344     gatt_client->attribute_handle = value_handle;
2345     gatt_client->attribute_length = value_length;
2346     gatt_client->attribute_offset = 0;
2347     gatt_client->attribute_value = value;
2348     gatt_client->gatt_client_state = P_W2_PREPARE_RELIABLE_WRITE;
2349     gatt_client_run();
2350     return ERROR_CODE_SUCCESS;
2351 }
2352 
2353 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){
2354     gatt_client_t * gatt_client;
2355     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2356     if (status != ERROR_CODE_SUCCESS){
2357         return status;
2358     }
2359     if (is_ready(gatt_client) == 0){
2360         return GATT_CLIENT_IN_WRONG_STATE;
2361     }
2362 
2363     if ( (configuration & GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION) &&
2364         ((characteristic->properties & ATT_PROPERTY_NOTIFY) == 0u)) {
2365         log_info("gatt_client_write_client_characteristic_configuration: GATT_CLIENT_CHARACTERISTIC_NOTIFICATION_NOT_SUPPORTED");
2366         return GATT_CLIENT_CHARACTERISTIC_NOTIFICATION_NOT_SUPPORTED;
2367     } else if ( (configuration & GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_INDICATION) &&
2368                ((characteristic->properties & ATT_PROPERTY_INDICATE) == 0u)){
2369         log_info("gatt_client_write_client_characteristic_configuration: GATT_CLIENT_CHARACTERISTIC_INDICATION_NOT_SUPPORTED");
2370         return GATT_CLIENT_CHARACTERISTIC_INDICATION_NOT_SUPPORTED;
2371     }
2372 
2373     gatt_client->callback = callback;
2374     gatt_client->start_group_handle = characteristic->value_handle;
2375     gatt_client->end_group_handle = characteristic->end_handle;
2376     little_endian_store_16(gatt_client->client_characteristic_configuration_value, 0, configuration);
2377 
2378 #ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY
2379     gatt_client->gatt_client_state = P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY;
2380 #else
2381     gatt_client->gatt_client_state = P_W2_SEND_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY;
2382 #endif
2383     gatt_client_run();
2384     return ERROR_CODE_SUCCESS;
2385 }
2386 
2387 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){
2388     gatt_client_t * gatt_client;
2389     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2390     if (status != ERROR_CODE_SUCCESS){
2391         return status;
2392     }
2393     if (is_ready(gatt_client) == 0){
2394         return GATT_CLIENT_IN_WRONG_STATE;
2395     }
2396 
2397     gatt_client->callback = callback;
2398     gatt_client->attribute_handle = descriptor_handle;
2399 
2400     gatt_client->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY;
2401     gatt_client_run();
2402     return ERROR_CODE_SUCCESS;
2403 }
2404 
2405 uint8_t gatt_client_read_characteristic_descriptor(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_descriptor_t * descriptor){
2406     return gatt_client_read_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle);
2407 }
2408 
2409 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){
2410     gatt_client_t * gatt_client;
2411     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2412     if (status != ERROR_CODE_SUCCESS){
2413         return status;
2414     }
2415     if (is_ready(gatt_client) == 0){
2416         return GATT_CLIENT_IN_WRONG_STATE;
2417     }
2418 
2419     gatt_client->callback = callback;
2420     gatt_client->attribute_handle = descriptor_handle;
2421     gatt_client->attribute_offset = offset;
2422     gatt_client->gatt_client_state = P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY;
2423     gatt_client_run();
2424     return ERROR_CODE_SUCCESS;
2425 }
2426 
2427 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){
2428     return gatt_client_read_long_characteristic_descriptor_using_descriptor_handle_with_offset(callback, con_handle, descriptor_handle, 0);
2429 }
2430 
2431 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){
2432     return gatt_client_read_long_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle);
2433 }
2434 
2435 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){
2436     gatt_client_t * gatt_client;
2437     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2438     if (status != ERROR_CODE_SUCCESS){
2439         return status;
2440     }
2441     if (is_ready(gatt_client) == 0){
2442         return GATT_CLIENT_IN_WRONG_STATE;
2443     }
2444 
2445     gatt_client->callback = callback;
2446     gatt_client->attribute_handle = descriptor_handle;
2447     gatt_client->attribute_length = value_length;
2448     gatt_client->attribute_offset = 0;
2449     gatt_client->attribute_value = value;
2450     gatt_client->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR;
2451     gatt_client_run();
2452     return ERROR_CODE_SUCCESS;
2453 }
2454 
2455 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){
2456     return gatt_client_write_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle, value_length, value);
2457 }
2458 
2459 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){
2460     gatt_client_t * gatt_client;
2461     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2462     if (status != ERROR_CODE_SUCCESS){
2463         return status;
2464     }
2465     if (is_ready(gatt_client) == 0){
2466         return GATT_CLIENT_IN_WRONG_STATE;
2467     }
2468 
2469     gatt_client->callback = callback;
2470     gatt_client->attribute_handle = descriptor_handle;
2471     gatt_client->attribute_length = value_length;
2472     gatt_client->attribute_offset = offset;
2473     gatt_client->attribute_value = value;
2474     gatt_client->gatt_client_state = P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR;
2475     gatt_client_run();
2476     return ERROR_CODE_SUCCESS;
2477 }
2478 
2479 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){
2480     return gatt_client_write_long_characteristic_descriptor_using_descriptor_handle_with_offset(callback, con_handle, descriptor_handle, 0, value_length, value);
2481 }
2482 
2483 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){
2484     return gatt_client_write_long_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle, value_length, value);
2485 }
2486 
2487 /**
2488  * @brief -> gatt complete event
2489  */
2490 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){
2491     gatt_client_t * gatt_client;
2492     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2493     if (status != ERROR_CODE_SUCCESS){
2494         return status;
2495     }
2496     if (is_ready(gatt_client) == 0){
2497         return GATT_CLIENT_IN_WRONG_STATE;
2498     }
2499 
2500     gatt_client->callback = callback;
2501     gatt_client->attribute_handle = attribute_handle;
2502     gatt_client->attribute_length = value_length;
2503     gatt_client->attribute_offset = offset;
2504     gatt_client->attribute_value = value;
2505     gatt_client->gatt_client_state = P_W2_PREPARE_WRITE_SINGLE;
2506     gatt_client_run();
2507     return ERROR_CODE_SUCCESS;
2508 }
2509 
2510 /**
2511  * @brief -> gatt complete event
2512  */
2513 uint8_t gatt_client_execute_write(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
2514     gatt_client_t * gatt_client;
2515     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2516     if (status != ERROR_CODE_SUCCESS){
2517         return status;
2518     }
2519     if (is_ready(gatt_client) == 0){
2520         return GATT_CLIENT_IN_WRONG_STATE;
2521     }
2522 
2523     gatt_client->callback = callback;
2524     gatt_client->gatt_client_state = P_W2_EXECUTE_PREPARED_WRITE;
2525     gatt_client_run();
2526     return ERROR_CODE_SUCCESS;
2527 }
2528 
2529 /**
2530  * @brief -> gatt complete event
2531  */
2532 uint8_t gatt_client_cancel_write(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
2533     gatt_client_t * gatt_client;
2534     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2535     if (status != ERROR_CODE_SUCCESS){
2536         return status;
2537     }
2538     if (is_ready(gatt_client) == 0){
2539         return GATT_CLIENT_IN_WRONG_STATE;
2540     }
2541 
2542     gatt_client->callback = callback;
2543     gatt_client->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE;
2544     gatt_client_run();
2545     return ERROR_CODE_SUCCESS;
2546 }
2547 
2548 void gatt_client_deserialize_service(const uint8_t *packet, int offset, gatt_client_service_t * service){
2549     service->start_group_handle = little_endian_read_16(packet, offset);
2550     service->end_group_handle = little_endian_read_16(packet, offset + 2);
2551     reverse_128(&packet[offset + 4], service->uuid128);
2552     if (uuid_has_bluetooth_prefix(service->uuid128)){
2553         service->uuid16 = big_endian_read_32(service->uuid128, 0);
2554     } else {
2555         service->uuid16 = 0;
2556     }
2557 }
2558 
2559 void gatt_client_deserialize_characteristic(const uint8_t * packet, int offset, gatt_client_characteristic_t * characteristic){
2560     characteristic->start_handle = little_endian_read_16(packet, offset);
2561     characteristic->value_handle = little_endian_read_16(packet, offset + 2);
2562     characteristic->end_handle = little_endian_read_16(packet, offset + 4);
2563     characteristic->properties = little_endian_read_16(packet, offset + 6);
2564     reverse_128(&packet[offset+8], characteristic->uuid128);
2565     if (uuid_has_bluetooth_prefix(characteristic->uuid128)){
2566         characteristic->uuid16 = big_endian_read_32(characteristic->uuid128, 0);
2567     } else {
2568         characteristic->uuid16 = 0;
2569     }
2570 }
2571 
2572 void gatt_client_deserialize_characteristic_descriptor(const uint8_t * packet, int offset, gatt_client_characteristic_descriptor_t * descriptor){
2573     descriptor->handle = little_endian_read_16(packet, offset);
2574     reverse_128(&packet[offset+2], descriptor->uuid128);
2575     if (uuid_has_bluetooth_prefix(descriptor->uuid128)){
2576         descriptor->uuid16 = big_endian_read_32(descriptor->uuid128, 0);
2577     } else {
2578         descriptor->uuid16 = 0;
2579     }
2580 }
2581 
2582 void gatt_client_send_mtu_negotiation(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
2583     gatt_client_t * gatt_client;
2584     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
2585     if (status != ERROR_CODE_SUCCESS){
2586         return;
2587     }
2588     if (gatt_client->mtu_state == MTU_AUTO_EXCHANGE_DISABLED){
2589         gatt_client->callback = callback;
2590         gatt_client->mtu_state = SEND_MTU_EXCHANGE;
2591         gatt_client_run();
2592     }
2593 }
2594 
2595 uint8_t gatt_client_request_to_write_without_response(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
2596     gatt_client_t * gatt_client;
2597     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
2598     if (status != ERROR_CODE_SUCCESS){
2599         return status;
2600     }
2601     bool added = btstack_linked_list_add_tail(&gatt_client->write_without_response_requests, (btstack_linked_item_t*) callback_registration);
2602     att_dispatch_client_request_can_send_now_event(gatt_client->con_handle);
2603     if (added){
2604         return ERROR_CODE_SUCCESS;
2605     } else {
2606         return ERROR_CODE_COMMAND_DISALLOWED;
2607     }
2608 }
2609 
2610 uint8_t gatt_client_request_to_send_gatt_query(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
2611     gatt_client_t * gatt_client;
2612     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
2613     if (status != ERROR_CODE_SUCCESS){
2614         return status;
2615     }
2616     bool added = btstack_linked_list_add_tail(&gatt_client->query_requests, (btstack_linked_item_t*) callback_registration);
2617     gatt_client_notify_can_send_query(gatt_client);
2618     if (added){
2619         return ERROR_CODE_SUCCESS;
2620     } else {
2621         return ERROR_CODE_COMMAND_DISALLOWED;
2622     }
2623 }
2624 
2625 uint8_t gatt_client_request_can_write_without_response_event(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
2626     gatt_client_t * gatt_client;
2627     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
2628     if (status != ERROR_CODE_SUCCESS){
2629         return status;
2630     }
2631     if (gatt_client->write_without_response_callback != NULL){
2632         return GATT_CLIENT_IN_WRONG_STATE;
2633     }
2634     gatt_client->write_without_response_callback = callback;
2635     att_dispatch_client_request_can_send_now_event(gatt_client->con_handle);
2636     return ERROR_CODE_SUCCESS;
2637 }
2638 
2639 #ifdef ENABLE_GATT_OVER_CLASSIC
2640 
2641 #include "hci_event.h"
2642 
2643 // single active SDP query
2644 static gatt_client_t * gatt_client_classic_active_sdp_query;
2645 
2646 // macos protocol descriptor list requires 16 bytes
2647 static uint8_t gatt_client_classic_sdp_buffer[32];
2648 
2649 static const hci_event_t gatt_client_connected = {
2650         GATT_EVENT_CONNECTED, 0, "1BH"
2651 };
2652 
2653 static gatt_client_t * gatt_client_get_context_for_classic_addr(bd_addr_t addr){
2654     btstack_linked_item_t *it;
2655     for (it = (btstack_linked_item_t *) gatt_client_connections; it != NULL; it = it->next){
2656         gatt_client_t * gatt_client = (gatt_client_t *) it;
2657         if (memcmp(gatt_client->addr, addr, 6) == 0){
2658             return gatt_client;
2659         }
2660     }
2661     return NULL;
2662 }
2663 
2664 static gatt_client_t * gatt_client_get_context_for_l2cap_cid(uint16_t l2cap_cid){
2665     btstack_linked_item_t *it;
2666     for (it = (btstack_linked_item_t *) gatt_client_connections; it != NULL; it = it->next){
2667         gatt_client_t * gatt_client = (gatt_client_t *) it;
2668         if (gatt_client->l2cap_cid == l2cap_cid){
2669             return gatt_client;
2670         }
2671     }
2672     return NULL;
2673 }
2674 
2675 static void gatt_client_classic_handle_connected(gatt_client_t * gatt_client, uint8_t status){
2676     bd_addr_t addr;
2677     memcpy(addr, gatt_client->addr, 6);
2678     hci_con_handle_t con_handle = gatt_client->con_handle;
2679     btstack_packet_handler_t callback = gatt_client->callback;
2680     if (status != ERROR_CODE_SUCCESS){
2681         btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) gatt_client);
2682         btstack_memory_gatt_client_free(gatt_client);
2683     }
2684     uint8_t buffer[20];
2685     uint16_t len = hci_event_create_from_template_and_arguments(buffer, sizeof(buffer), &gatt_client_connected, status, addr,
2686                                                                 con_handle);
2687     (*callback)(HCI_EVENT_PACKET, 0, buffer, len);
2688 }
2689 
2690 static void gatt_client_l2cap_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2691     gatt_client_t * gatt_client = NULL;
2692     uint8_t status;
2693     switch (packet_type){
2694         case HCI_EVENT_PACKET:
2695             switch (hci_event_packet_get_type(packet)) {
2696                 case L2CAP_EVENT_CHANNEL_OPENED:
2697                     status = l2cap_event_channel_opened_get_status(packet);
2698                     gatt_client = gatt_client_get_context_for_l2cap_cid(l2cap_event_channel_opened_get_local_cid(packet));
2699                     btstack_assert(gatt_client != NULL);
2700                     gatt_client->con_handle = l2cap_event_channel_opened_get_handle(packet);
2701                     gatt_client_classic_handle_connected(gatt_client, status);
2702                 case L2CAP_EVENT_CHANNEL_CLOSED:
2703                     // TODO:
2704                     break;
2705                 default:
2706                     break;
2707             }
2708             break;
2709         case L2CAP_DATA_PACKET:
2710             gatt_client = gatt_client_get_context_for_l2cap_cid(channel);
2711             gatt_client = gatt_client_get_context_for_l2cap_cid(l2cap_event_channel_opened_get_local_cid(packet));
2712             btstack_assert(gatt_client != NULL);
2713             log_info("l2cap data received");
2714             break;
2715     }
2716 }
2717 
2718 static void gatt_client_handle_sdp_client_query_attribute_value(gatt_client_t * connection, uint8_t *packet){
2719     des_iterator_t des_list_it;
2720     des_iterator_t prot_it;
2721 
2722     if (sdp_event_query_attribute_byte_get_attribute_length(packet) <= sizeof(gatt_client_classic_sdp_buffer)) {
2723         gatt_client_classic_sdp_buffer[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet);
2724         if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)) {
2725             switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) {
2726                 case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST:
2727                     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)) {
2728                         uint8_t       *des_element;
2729                         uint8_t       *element;
2730                         uint32_t       uuid;
2731 
2732                         if (des_iterator_get_type(&des_list_it) != DE_DES) continue;
2733 
2734                         des_element = des_iterator_get_element(&des_list_it);
2735                         des_iterator_init(&prot_it, des_element);
2736                         element = des_iterator_get_element(&prot_it);
2737 
2738                         if (de_get_element_type(element) != DE_UUID) continue;
2739 
2740                         uuid = de_get_uuid32(element);
2741                         des_iterator_next(&prot_it);
2742                         // we assume that the even if there are both roles supported, remote device uses the same psm and avdtp version for both
2743                         switch (uuid){
2744                             case BLUETOOTH_PROTOCOL_L2CAP:
2745                                 if (!des_iterator_has_more(&prot_it)) continue;
2746                                 de_element_get_uint16(des_iterator_get_element(&prot_it), &connection->l2cap_psm);
2747                                 break;
2748                             default:
2749                                 break;
2750                         }
2751                     }
2752                     break;
2753 
2754                 default:
2755                     break;
2756             }
2757         }
2758     }
2759 }
2760 
2761 static void gatt_client_classic_sdp_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
2762     gatt_client_t * gatt_client = gatt_client_classic_active_sdp_query;
2763     btstack_assert(gatt_client != NULL);
2764     uint8_t status;
2765 
2766     // TODO: handle sdp events, get l2cap psm
2767     switch (hci_event_packet_get_type(packet)){
2768         case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
2769             gatt_client_handle_sdp_client_query_attribute_value(gatt_client, packet);
2770             // TODO:
2771             return;
2772         case SDP_EVENT_QUERY_COMPLETE:
2773             status = sdp_event_query_complete_get_status(packet);
2774             gatt_client_classic_active_sdp_query = NULL;
2775             log_info("l2cap psm: %0x, status %02x", gatt_client->l2cap_psm, status);
2776             if (status != ERROR_CODE_SUCCESS) break;
2777             if (gatt_client->l2cap_psm == 0) {
2778                 status = SDP_SERVICE_NOT_FOUND;
2779                 break;
2780             }
2781             break;
2782         default:
2783             btstack_assert(false);
2784             return;
2785     }
2786 
2787     // done
2788     if (status == ERROR_CODE_SUCCESS){
2789         gatt_client->gatt_client_state = P_W4_L2CAP_CONNECTION;
2790         status = l2cap_create_channel(gatt_client_l2cap_handler, gatt_client->addr, gatt_client->l2cap_psm, 0xffff,
2791                              &gatt_client->l2cap_cid);
2792     }
2793     if (status != ERROR_CODE_SUCCESS) {
2794         gatt_client_classic_handle_connected(gatt_client, status);
2795     }
2796 }
2797 
2798 static void gatt_client_classic_sdp_start(void * context){
2799     gatt_client_classic_active_sdp_query = (gatt_client_t *) context;
2800     gatt_client_classic_active_sdp_query->gatt_client_state = P_W4_SDP_QUERY;
2801     sdp_client_query_uuid16(gatt_client_classic_sdp_handler, gatt_client_classic_active_sdp_query->addr, ORG_BLUETOOTH_SERVICE_GENERIC_ATTRIBUTE);
2802 }
2803 
2804 uint8_t gatt_client_classic_connect(btstack_packet_handler_t callback, bd_addr_t addr){
2805     gatt_client_t * gatt_client = gatt_client_get_context_for_classic_addr(addr);
2806     if (gatt_client != NULL){
2807         return ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS;
2808     }
2809     gatt_client = btstack_memory_gatt_client_get();
2810     if (gatt_client == NULL){
2811         return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED;
2812     }
2813     // init state
2814     gatt_client->con_handle = HCI_CON_HANDLE_INVALID;
2815     memcpy(gatt_client->addr, addr, 6);
2816     gatt_client->mtu = ATT_DEFAULT_MTU;
2817     gatt_client->security_level = LEVEL_0;
2818     if (gatt_client_mtu_exchange_enabled){
2819         gatt_client->mtu_state = SEND_MTU_EXCHANGE;
2820     } else {
2821         gatt_client->mtu_state = MTU_AUTO_EXCHANGE_DISABLED;
2822     }
2823     gatt_client->gatt_client_state = P_W2_SDP_QUERY;
2824     gatt_client->sdp_query_request.callback = &gatt_client_classic_sdp_start;
2825     gatt_client->sdp_query_request.context = gatt_client;
2826     gatt_client->callback = callback;
2827     btstack_linked_list_add(&gatt_client_connections, (btstack_linked_item_t*)gatt_client);
2828     sdp_client_register_query_callback(&gatt_client->sdp_query_request);
2829     return ERROR_CODE_COMMAND_DISALLOWED;
2830 }
2831 
2832 uint8_t gatt_client_classic_disconnect(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
2833     gatt_client_t * gatt_client = gatt_client_get_context_for_handle(con_handle);
2834     if (gatt_client == NULL){
2835         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2836     }
2837     gatt_client->callback = callback;
2838     return l2cap_disconnect(gatt_client->l2cap_cid);
2839 }
2840 #endif
2841 
2842 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
2843 void gatt_client_att_packet_handler_fuzz(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
2844     gatt_client_att_packet_handler(packet_type, handle, packet, size);
2845 }
2846 
2847 uint8_t gatt_client_get_client(hci_con_handle_t con_handle, gatt_client_t ** out_gatt_client){
2848     uint8_t status = gatt_client_provide_context_for_handle(con_handle, out_gatt_client);
2849     return status;
2850 }
2851 #endif
2852