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