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