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