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