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