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