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