xref: /btstack/src/ble/gatt_client.c (revision 45f2a74090e2efe8c4f7bd3d8c3d5729d5d45ec3)
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 // returns 1 if packet was sent
817 static int gatt_client_run_for_peripheral( gatt_client_t * peripheral){
818     // log_info("- handle_peripheral_list, mtu state %u, client state %u", peripheral->mtu_state, peripheral->gatt_client_state);
819 
820     switch (peripheral->mtu_state) {
821         case SEND_MTU_EXCHANGE:
822             peripheral->mtu_state = SENT_MTU_EXCHANGE;
823             att_exchange_mtu_request(peripheral->con_handle);
824             return 1;
825         case SENT_MTU_EXCHANGE:
826             return 0;
827         default:
828             break;
829     }
830 
831     if (peripheral->send_confirmation){
832         peripheral->send_confirmation = 0;
833         att_confirmation(peripheral->con_handle);
834         return 1;
835     }
836 
837     // check MTU for writes
838     switch (peripheral->gatt_client_state){
839         case P_W2_SEND_WRITE_CHARACTERISTIC_VALUE:
840         case P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR:
841             if (peripheral->attribute_length <= peripheral_mtu(peripheral) - 3) break;
842             log_error("gatt_client_run: value len %u > MTU %u - 3\n", peripheral->attribute_length, peripheral_mtu(peripheral));
843             gatt_client_handle_transaction_complete(peripheral);
844             emit_gatt_complete_event(peripheral, ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LENGTH);
845             return 0;
846         default:
847             break;
848     }
849 
850     // log_info("gatt_client_state %u", peripheral->gatt_client_state);
851     switch (peripheral->gatt_client_state){
852         case P_W2_SEND_SERVICE_QUERY:
853             peripheral->gatt_client_state = P_W4_SERVICE_QUERY_RESULT;
854             send_gatt_services_request(peripheral);
855             return 1;
856 
857         case P_W2_SEND_SERVICE_WITH_UUID_QUERY:
858             peripheral->gatt_client_state = P_W4_SERVICE_WITH_UUID_RESULT;
859             send_gatt_services_by_uuid_request(peripheral);
860             return 1;
861 
862         case P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY:
863             peripheral->gatt_client_state = P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT;
864             send_gatt_characteristic_request(peripheral);
865             return 1;
866 
867         case P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY:
868             peripheral->gatt_client_state = P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT;
869             send_gatt_characteristic_request(peripheral);
870             return 1;
871 
872         case P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY:
873             peripheral->gatt_client_state = P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT;
874             send_gatt_characteristic_descriptor_request(peripheral);
875             return 1;
876 
877         case P_W2_SEND_INCLUDED_SERVICE_QUERY:
878             peripheral->gatt_client_state = P_W4_INCLUDED_SERVICE_QUERY_RESULT;
879             send_gatt_included_service_request(peripheral);
880             return 1;
881 
882         case P_W2_SEND_INCLUDED_SERVICE_WITH_UUID_QUERY:
883             peripheral->gatt_client_state = P_W4_INCLUDED_SERVICE_UUID_WITH_QUERY_RESULT;
884             send_gatt_included_service_uuid_request(peripheral);
885             return 1;
886 
887         case P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY:
888             peripheral->gatt_client_state = P_W4_READ_CHARACTERISTIC_VALUE_RESULT;
889             send_gatt_read_characteristic_value_request(peripheral);
890             return 1;
891 
892         case P_W2_SEND_READ_BLOB_QUERY:
893             peripheral->gatt_client_state = P_W4_READ_BLOB_RESULT;
894             send_gatt_read_blob_request(peripheral);
895             return 1;
896 
897         case P_W2_SEND_READ_BY_TYPE_REQUEST:
898             peripheral->gatt_client_state = P_W4_READ_BY_TYPE_RESPONSE;
899             send_gatt_read_by_type_request(peripheral);
900             return 1;
901 
902         case P_W2_SEND_READ_MULTIPLE_REQUEST:
903             peripheral->gatt_client_state = P_W4_READ_MULTIPLE_RESPONSE;
904             send_gatt_read_multiple_request(peripheral);
905             return 1;
906 
907         case P_W2_SEND_WRITE_CHARACTERISTIC_VALUE:
908             peripheral->gatt_client_state = P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT;
909             send_gatt_write_attribute_value_request(peripheral);
910             return 1;
911 
912         case P_W2_PREPARE_WRITE:
913             peripheral->gatt_client_state = P_W4_PREPARE_WRITE_RESULT;
914             send_gatt_prepare_write_request(peripheral);
915             return 1;
916 
917         case P_W2_PREPARE_WRITE_SINGLE:
918             peripheral->gatt_client_state = P_W4_PREPARE_WRITE_SINGLE_RESULT;
919             send_gatt_prepare_write_request(peripheral);
920             return 1;
921 
922         case P_W2_PREPARE_RELIABLE_WRITE:
923             peripheral->gatt_client_state = P_W4_PREPARE_RELIABLE_WRITE_RESULT;
924             send_gatt_prepare_write_request(peripheral);
925             return 1;
926 
927         case P_W2_EXECUTE_PREPARED_WRITE:
928             peripheral->gatt_client_state = P_W4_EXECUTE_PREPARED_WRITE_RESULT;
929             send_gatt_execute_write_request(peripheral);
930             return 1;
931 
932         case P_W2_CANCEL_PREPARED_WRITE:
933             peripheral->gatt_client_state = P_W4_CANCEL_PREPARED_WRITE_RESULT;
934             send_gatt_cancel_prepared_write_request(peripheral);
935             return 1;
936 
937         case P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH:
938             peripheral->gatt_client_state = P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT;
939             send_gatt_cancel_prepared_write_request(peripheral);
940             return 1;
941 
942         case P_W2_SEND_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY:
943             peripheral->gatt_client_state = P_W4_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT;
944             send_gatt_read_client_characteristic_configuration_request(peripheral);
945             return 1;
946 
947         case P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY:
948             peripheral->gatt_client_state = P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT;
949             send_gatt_read_characteristic_descriptor_request(peripheral);
950             return 1;
951 
952         case P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY:
953             peripheral->gatt_client_state = P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT;
954             send_gatt_read_blob_request(peripheral);
955             return 1;
956 
957         case P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR:
958             peripheral->gatt_client_state = P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT;
959             send_gatt_write_attribute_value_request(peripheral);
960             return 1;
961 
962         case P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION:
963             peripheral->gatt_client_state = P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT;
964             send_gatt_write_client_characteristic_configuration_request(peripheral);
965             return 1;
966 
967         case P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR:
968             peripheral->gatt_client_state = P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT;
969             send_gatt_prepare_write_request(peripheral);
970             return 1;
971 
972         case P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR:
973             peripheral->gatt_client_state = P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT;
974             send_gatt_execute_write_request(peripheral);
975             return 1;
976 
977 #ifdef ENABLE_LE_SIGNED_WRITE
978         case P_W4_CMAC_READY:
979             if (sm_cmac_ready()){
980                 sm_key_t csrk;
981                 le_device_db_local_csrk_get(peripheral->le_device_index, csrk);
982                 uint32_t sign_counter = le_device_db_local_counter_get(peripheral->le_device_index);
983                 peripheral->gatt_client_state = P_W4_CMAC_RESULT;
984                 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);
985             }
986             return 0;
987 
988         case P_W2_SEND_SIGNED_WRITE: {
989             peripheral->gatt_client_state = P_W4_SEND_SINGED_WRITE_DONE;
990             // bump local signing counter
991             uint32_t sign_counter = le_device_db_local_counter_get(peripheral->le_device_index);
992             le_device_db_local_counter_set(peripheral->le_device_index, sign_counter + 1);
993 
994             send_gatt_signed_write_request(peripheral, sign_counter);
995             peripheral->gatt_client_state = P_READY;
996             // finally, notifiy client that write is complete
997             gatt_client_handle_transaction_complete(peripheral);
998             return 1;
999         }
1000 #endif
1001 
1002         default:
1003             break;
1004     }
1005     return 0;
1006 }
1007 
1008 static void gatt_client_run(void){
1009     btstack_linked_item_t *it;
1010     for (it = (btstack_linked_item_t *) gatt_client_connections; it ; it = it->next){
1011         gatt_client_t * peripheral = (gatt_client_t *) it;
1012         if (!att_dispatch_client_can_send_now(peripheral->con_handle)) {
1013             att_dispatch_client_request_can_send_now_event(peripheral->con_handle);
1014             return;
1015         }
1016         int packet_sent = gatt_client_run_for_peripheral(peripheral);
1017         if (packet_sent){
1018             // request new permission
1019             att_dispatch_client_request_can_send_now_event(peripheral->con_handle);
1020             // requeue client for fairness and exit
1021             // note: iterator has become invalid
1022             btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) peripheral);
1023             btstack_linked_list_add_tail(&gatt_client_connections, (btstack_linked_item_t *) peripheral);
1024             return;
1025         }
1026     }
1027 }
1028 
1029 static void gatt_client_report_error_if_pending(gatt_client_t *peripheral, uint8_t error_code) {
1030     if (is_ready(peripheral)) return;
1031     gatt_client_handle_transaction_complete(peripheral);
1032     emit_gatt_complete_event(peripheral, error_code);
1033 }
1034 
1035 static void gatt_client_hci_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1036     UNUSED(channel);    // ok: handling own l2cap events
1037     UNUSED(size);       // ok: there is no channel
1038 
1039     if (packet_type != HCI_EVENT_PACKET) return;
1040 
1041     switch (hci_event_packet_get_type(packet)) {
1042         case HCI_EVENT_DISCONNECTION_COMPLETE:
1043         {
1044             log_info("GATT Client: HCI_EVENT_DISCONNECTION_COMPLETE");
1045             hci_con_handle_t con_handle = little_endian_read_16(packet,3);
1046             gatt_client_t * peripheral = get_gatt_client_context_for_handle(con_handle);
1047             if (!peripheral) break;
1048             gatt_client_report_error_if_pending(peripheral, ATT_ERROR_HCI_DISCONNECT_RECEIVED);
1049 
1050             btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) peripheral);
1051             btstack_memory_gatt_client_free(peripheral);
1052             break;
1053         }
1054         default:
1055             break;
1056     }
1057 
1058     gatt_client_run();
1059 }
1060 
1061 static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
1062 
1063     if (packet_type == HCI_EVENT_PACKET && packet[0] == L2CAP_EVENT_CAN_SEND_NOW){
1064         gatt_client_run();
1065     }
1066 
1067     if (packet_type != ATT_DATA_PACKET) return;
1068 
1069     // special cases: notifications don't need a context while indications motivate creating one
1070     gatt_client_t * peripheral;
1071     switch (packet[0]){
1072         case ATT_HANDLE_VALUE_NOTIFICATION:
1073             report_gatt_notification(handle, little_endian_read_16(packet,1), &packet[3], size-3);
1074             return;
1075         case ATT_HANDLE_VALUE_INDICATION:
1076             peripheral = provide_context_for_conn_handle(handle);
1077             break;
1078         default:
1079             peripheral = get_gatt_client_context_for_handle(handle);
1080             break;
1081     }
1082 
1083     if (!peripheral) return;
1084 
1085     switch (packet[0]){
1086         // att_server has negotiated the mtu for this connection
1087         case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
1088         {
1089             peripheral->mtu = little_endian_read_16(packet, 4);
1090             break;
1091         }
1092         case ATT_EXCHANGE_MTU_RESPONSE:
1093         {
1094             uint16_t remote_rx_mtu = little_endian_read_16(packet, 1);
1095             uint16_t local_rx_mtu = l2cap_max_le_mtu();
1096             peripheral->mtu = remote_rx_mtu < local_rx_mtu ? remote_rx_mtu : local_rx_mtu;
1097             peripheral->mtu_state = MTU_EXCHANGED;
1098             emit_gatt_mtu_exchanged_result_event(peripheral, peripheral->mtu);
1099             break;
1100         }
1101         case ATT_READ_BY_GROUP_TYPE_RESPONSE:
1102             switch(peripheral->gatt_client_state){
1103                 case P_W4_SERVICE_QUERY_RESULT:
1104                     report_gatt_services(peripheral, packet, size);
1105                     trigger_next_service_query(peripheral, get_last_result_handle_from_service_list(packet, size));
1106                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1107                     break;
1108                 default:
1109                     break;
1110             }
1111             break;
1112         case ATT_HANDLE_VALUE_INDICATION:
1113             report_gatt_indication(handle, little_endian_read_16(packet,1), &packet[3], size-3);
1114             peripheral->send_confirmation = 1;
1115             break;
1116 
1117         case ATT_READ_BY_TYPE_RESPONSE:
1118             switch (peripheral->gatt_client_state){
1119                 case P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT:
1120                     report_gatt_characteristics(peripheral, packet, size);
1121                     trigger_next_characteristic_query(peripheral, get_last_result_handle_from_characteristics_list(packet, size));
1122                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done, or by ATT_ERROR
1123                     break;
1124                 case P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT:
1125                     report_gatt_characteristics(peripheral, packet, size);
1126                     trigger_next_characteristic_query(peripheral, get_last_result_handle_from_characteristics_list(packet, size));
1127                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done, or by ATT_ERROR
1128                     break;
1129                 case P_W4_INCLUDED_SERVICE_QUERY_RESULT:
1130                 {
1131                     uint16_t uuid16 = 0;
1132                     uint16_t pair_size = packet[1];
1133 
1134                     if (pair_size < 7){
1135                         // UUIDs not available, query first included service
1136                         peripheral->start_group_handle = little_endian_read_16(packet, 2); // ready for next query
1137                         peripheral->query_start_handle = little_endian_read_16(packet, 4);
1138                         peripheral->query_end_handle = little_endian_read_16(packet,6);
1139                         peripheral->gatt_client_state = P_W2_SEND_INCLUDED_SERVICE_WITH_UUID_QUERY;
1140                         break;
1141                     }
1142 
1143                     uint16_t offset;
1144                     for (offset = 2; offset < size; offset += pair_size){
1145                         uint16_t include_handle = little_endian_read_16(packet, offset);
1146                         peripheral->query_start_handle = little_endian_read_16(packet,offset+2);
1147                         peripheral->query_end_handle = little_endian_read_16(packet,offset+4);
1148                         uuid16 = little_endian_read_16(packet, offset+6);
1149                         report_gatt_included_service_uuid16(peripheral, include_handle, uuid16);
1150                     }
1151 
1152                     trigger_next_included_service_query(peripheral, get_last_result_handle_from_included_services_list(packet, size));
1153                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1154                     break;
1155                 }
1156                 case P_W4_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT:
1157                     peripheral->client_characteristic_configuration_handle = little_endian_read_16(packet, 2);
1158                     peripheral->gatt_client_state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION;
1159                     break;
1160                 case P_W4_READ_BY_TYPE_RESPONSE: {
1161                     uint16_t pair_size = packet[1];
1162                     uint16_t offset;
1163                     uint16_t last_result_handle = 0;
1164                     for (offset = 2; offset < size ; offset += pair_size){
1165                         uint16_t value_handle = little_endian_read_16(packet, offset);
1166                         report_gatt_characteristic_value(peripheral, value_handle, &packet[offset+2], pair_size-2);
1167                         last_result_handle = value_handle;
1168                     }
1169                     trigger_next_read_by_type_query(peripheral, last_result_handle);
1170                     break;
1171                 }
1172                 default:
1173                     break;
1174             }
1175             break;
1176         case ATT_READ_RESPONSE:
1177             switch (peripheral->gatt_client_state){
1178                 case P_W4_INCLUDED_SERVICE_UUID_WITH_QUERY_RESULT: {
1179                     uint8_t uuid128[16];
1180                     reverse_128(&packet[1], uuid128);
1181                     report_gatt_included_service_uuid128(peripheral, peripheral->start_group_handle, uuid128);
1182                     trigger_next_included_service_query(peripheral, peripheral->start_group_handle);
1183                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1184                     break;
1185                 }
1186                 case P_W4_READ_CHARACTERISTIC_VALUE_RESULT:
1187                     gatt_client_handle_transaction_complete(peripheral);
1188                     report_gatt_characteristic_value(peripheral, peripheral->attribute_handle, &packet[1], size-1);
1189                     emit_gatt_complete_event(peripheral, 0);
1190                     break;
1191 
1192                 case P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT:{
1193                     gatt_client_handle_transaction_complete(peripheral);
1194                     report_gatt_characteristic_descriptor(peripheral, peripheral->attribute_handle, &packet[1], size-1, 0);
1195                     emit_gatt_complete_event(peripheral, 0);
1196                     break;
1197                 }
1198                 default:
1199                     break;
1200             }
1201             break;
1202 
1203         case ATT_FIND_BY_TYPE_VALUE_RESPONSE:
1204         {
1205             uint8_t pair_size = 4;
1206             int i;
1207             uint16_t start_group_handle;
1208             uint16_t   end_group_handle= 0xffff; // asserts GATT_EVENT_QUERY_COMPLETE is emitted if no results
1209             for (i = 1; i<size; i+=pair_size){
1210                 start_group_handle = little_endian_read_16(packet,i);
1211                 end_group_handle = little_endian_read_16(packet,i+2);
1212                 emit_gatt_service_query_result_event(peripheral, start_group_handle, end_group_handle, peripheral->uuid128);
1213             }
1214             trigger_next_service_by_uuid_query(peripheral, end_group_handle);
1215             // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1216             break;
1217         }
1218         case ATT_FIND_INFORMATION_REPLY:
1219         {
1220             uint8_t pair_size = 4;
1221             if (packet[1] == 2){
1222                 pair_size = 18;
1223             }
1224             uint16_t last_descriptor_handle = little_endian_read_16(packet, size - pair_size);
1225             report_gatt_all_characteristic_descriptors(peripheral, &packet[2], size-2, pair_size);
1226             trigger_next_characteristic_descriptor_query(peripheral, last_descriptor_handle);
1227             // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1228             break;
1229         }
1230 
1231         case ATT_WRITE_RESPONSE:
1232             switch (peripheral->gatt_client_state){
1233                 case P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT:
1234                     gatt_client_handle_transaction_complete(peripheral);
1235                     emit_gatt_complete_event(peripheral, 0);
1236                     break;
1237                 case P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT:
1238                     gatt_client_handle_transaction_complete(peripheral);
1239                     emit_gatt_complete_event(peripheral, 0);
1240                     break;
1241                 case P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
1242                     gatt_client_handle_transaction_complete(peripheral);
1243                     emit_gatt_complete_event(peripheral, 0);
1244                     break;
1245                 default:
1246                     break;
1247             }
1248             break;
1249 
1250         case ATT_READ_BLOB_RESPONSE:{
1251             uint16_t received_blob_length = size-1;
1252             switch(peripheral->gatt_client_state){
1253                 case P_W4_READ_BLOB_RESULT:
1254                     report_gatt_long_characteristic_value_blob(peripheral, peripheral->attribute_handle, &packet[1], received_blob_length, peripheral->attribute_offset);
1255                     trigger_next_blob_query(peripheral, P_W2_SEND_READ_BLOB_QUERY, received_blob_length);
1256                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1257                     break;
1258                 case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT:
1259                     report_gatt_long_characteristic_descriptor(peripheral, peripheral->attribute_handle,
1260                                                           &packet[1], received_blob_length,
1261                                                           peripheral->attribute_offset);
1262                     trigger_next_blob_query(peripheral, P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY, received_blob_length);
1263                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1264                     break;
1265                 default:
1266                     break;
1267             }
1268             break;
1269         }
1270         case ATT_PREPARE_WRITE_RESPONSE:
1271             switch (peripheral->gatt_client_state){
1272                 case P_W4_PREPARE_WRITE_SINGLE_RESULT:
1273                     gatt_client_handle_transaction_complete(peripheral);
1274                     if (is_value_valid(peripheral, packet, size)){
1275                         emit_gatt_complete_event(peripheral, 0);
1276                     } else {
1277                         emit_gatt_complete_event(peripheral, ATT_ERROR_DATA_MISMATCH);
1278                     }
1279                     break;
1280 
1281                 case P_W4_PREPARE_WRITE_RESULT:{
1282                     peripheral->attribute_offset = little_endian_read_16(packet, 3);
1283                     trigger_next_prepare_write_query(peripheral, P_W2_PREPARE_WRITE, P_W2_EXECUTE_PREPARED_WRITE);
1284                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1285                     break;
1286                 }
1287                 case P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:{
1288                     peripheral->attribute_offset = little_endian_read_16(packet, 3);
1289                     trigger_next_prepare_write_query(peripheral, P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR, P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR);
1290                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1291                     break;
1292                 }
1293                 case P_W4_PREPARE_RELIABLE_WRITE_RESULT:{
1294                     if (is_value_valid(peripheral, packet, size)){
1295                         peripheral->attribute_offset = little_endian_read_16(packet, 3);
1296                         trigger_next_prepare_write_query(peripheral, P_W2_PREPARE_RELIABLE_WRITE, P_W2_EXECUTE_PREPARED_WRITE);
1297                         // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1298                         break;
1299                     }
1300                     peripheral->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH;
1301                     break;
1302                 }
1303                 default:
1304                     break;
1305             }
1306             break;
1307 
1308         case ATT_EXECUTE_WRITE_RESPONSE:
1309             switch (peripheral->gatt_client_state){
1310                 case P_W4_EXECUTE_PREPARED_WRITE_RESULT:
1311                     gatt_client_handle_transaction_complete(peripheral);
1312                     emit_gatt_complete_event(peripheral, 0);
1313                     break;
1314                 case P_W4_CANCEL_PREPARED_WRITE_RESULT:
1315                     gatt_client_handle_transaction_complete(peripheral);
1316                     emit_gatt_complete_event(peripheral, 0);
1317                     break;
1318                 case P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT:
1319                     gatt_client_handle_transaction_complete(peripheral);
1320                     emit_gatt_complete_event(peripheral, ATT_ERROR_DATA_MISMATCH);
1321                     break;
1322                 case P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
1323                     gatt_client_handle_transaction_complete(peripheral);
1324                     emit_gatt_complete_event(peripheral, 0);
1325                     break;
1326                 default:
1327                     break;
1328 
1329             }
1330             break;
1331 
1332         case ATT_READ_MULTIPLE_RESPONSE:
1333             switch(peripheral->gatt_client_state){
1334                 case P_W4_READ_MULTIPLE_RESPONSE:
1335                     report_gatt_characteristic_value(peripheral, 0, &packet[1], size-1);
1336                     gatt_client_handle_transaction_complete(peripheral);
1337                     emit_gatt_complete_event(peripheral, 0);
1338                     break;
1339                 default:
1340                     break;
1341             }
1342             break;
1343 
1344         case ATT_ERROR_RESPONSE:
1345 
1346             switch (packet[4]){
1347                 case ATT_ERROR_ATTRIBUTE_NOT_FOUND: {
1348                     switch(peripheral->gatt_client_state){
1349                         case P_W4_SERVICE_QUERY_RESULT:
1350                         case P_W4_SERVICE_WITH_UUID_RESULT:
1351                         case P_W4_INCLUDED_SERVICE_QUERY_RESULT:
1352                         case P_W4_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT:
1353                             gatt_client_handle_transaction_complete(peripheral);
1354                             emit_gatt_complete_event(peripheral, 0);
1355                             break;
1356                         case P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT:
1357                         case P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT:
1358                             characteristic_end_found(peripheral, peripheral->end_group_handle);
1359                             gatt_client_handle_transaction_complete(peripheral);
1360                             emit_gatt_complete_event(peripheral, 0);
1361                             break;
1362                         case P_W4_READ_BY_TYPE_RESPONSE:
1363                             gatt_client_handle_transaction_complete(peripheral);
1364                             if (peripheral->start_group_handle == peripheral->query_start_handle){
1365                                 emit_gatt_complete_event(peripheral, ATT_ERROR_ATTRIBUTE_NOT_FOUND);
1366                             } else {
1367                                 emit_gatt_complete_event(peripheral, 0);
1368                             }
1369                             break;
1370                         default:
1371                             gatt_client_report_error_if_pending(peripheral, packet[4]);
1372                             break;
1373                     }
1374                     break;
1375                 }
1376                 default:
1377                     gatt_client_report_error_if_pending(peripheral, packet[4]);
1378                     break;
1379             }
1380             break;
1381 
1382         default:
1383             log_info("ATT Handler, unhandled response type 0x%02x", packet[0]);
1384             break;
1385     }
1386     gatt_client_run();
1387 }
1388 
1389 #ifdef ENABLE_LE_SIGNED_WRITE
1390 static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
1391     btstack_linked_list_iterator_t it;
1392     btstack_linked_list_iterator_init(&it, &gatt_client_connections);
1393     while (btstack_linked_list_iterator_has_next(&it)){
1394         gatt_client_t * peripheral = (gatt_client_t *) btstack_linked_list_iterator_next(&it);
1395         if (peripheral->gatt_client_state == P_W4_CMAC_RESULT){
1396             // store result
1397             memcpy(peripheral->cmac, hash, 8);
1398             // reverse_64(hash, peripheral->cmac);
1399             peripheral->gatt_client_state = P_W2_SEND_SIGNED_WRITE;
1400             gatt_client_run();
1401             return;
1402         }
1403     }
1404 }
1405 
1406 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){
1407     gatt_client_t * peripheral = provide_context_for_conn_handle(con_handle);
1408     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
1409     peripheral->le_device_index = sm_le_device_index(con_handle);
1410     if (peripheral->le_device_index < 0) return GATT_CLIENT_IN_WRONG_STATE; // device lookup not done / no stored bonding information
1411 
1412     peripheral->callback = callback;
1413     peripheral->attribute_handle = handle;
1414     peripheral->attribute_length = message_len;
1415     peripheral->attribute_value = message;
1416     peripheral->gatt_client_state = P_W4_CMAC_READY;
1417 
1418     gatt_client_run();
1419     return 0;
1420 }
1421 #endif
1422 
1423 uint8_t gatt_client_discover_primary_services(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
1424     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1425     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1426     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
1427 
1428     peripheral->callback = callback;
1429     peripheral->start_group_handle = 0x0001;
1430     peripheral->end_group_handle   = 0xffff;
1431     peripheral->gatt_client_state = P_W2_SEND_SERVICE_QUERY;
1432     peripheral->uuid16 = 0;
1433     gatt_client_run();
1434     return 0;
1435 }
1436 
1437 
1438 uint8_t gatt_client_discover_primary_services_by_uuid16(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t uuid16){
1439     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1440 
1441     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1442     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
1443 
1444     peripheral->callback = callback;
1445     peripheral->start_group_handle = 0x0001;
1446     peripheral->end_group_handle   = 0xffff;
1447     peripheral->gatt_client_state = P_W2_SEND_SERVICE_WITH_UUID_QUERY;
1448     peripheral->uuid16 = uuid16;
1449     uuid_add_bluetooth_prefix((uint8_t*) &(peripheral->uuid128), peripheral->uuid16);
1450     gatt_client_run();
1451     return 0;
1452 }
1453 
1454 uint8_t gatt_client_discover_primary_services_by_uuid128(btstack_packet_handler_t callback, hci_con_handle_t con_handle, const uint8_t * uuid128){
1455     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1456 
1457     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1458     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
1459 
1460     peripheral->callback = callback;
1461     peripheral->start_group_handle = 0x0001;
1462     peripheral->end_group_handle   = 0xffff;
1463     peripheral->uuid16 = 0;
1464     memcpy(peripheral->uuid128, uuid128, 16);
1465     peripheral->gatt_client_state = P_W2_SEND_SERVICE_WITH_UUID_QUERY;
1466     gatt_client_run();
1467     return 0;
1468 }
1469 
1470 uint8_t gatt_client_discover_characteristics_for_service(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_service_t *service){
1471     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1472 
1473     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1474     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
1475 
1476     peripheral->callback = callback;
1477     peripheral->start_group_handle = service->start_group_handle;
1478     peripheral->end_group_handle   = service->end_group_handle;
1479     peripheral->filter_with_uuid = 0;
1480     peripheral->characteristic_start_handle = 0;
1481     peripheral->gatt_client_state = P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY;
1482     gatt_client_run();
1483     return 0;
1484 }
1485 
1486 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){
1487     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1488 
1489     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1490     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
1491 
1492     peripheral->callback = callback;
1493     peripheral->start_group_handle = service->start_group_handle;
1494     peripheral->end_group_handle   = service->end_group_handle;
1495     peripheral->gatt_client_state = P_W2_SEND_INCLUDED_SERVICE_QUERY;
1496 
1497     gatt_client_run();
1498     return 0;
1499 }
1500 
1501 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){
1502     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1503 
1504     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1505     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
1506 
1507     peripheral->callback = callback;
1508     peripheral->start_group_handle = start_handle;
1509     peripheral->end_group_handle   = end_handle;
1510     peripheral->filter_with_uuid = 1;
1511     peripheral->uuid16 = uuid16;
1512     uuid_add_bluetooth_prefix((uint8_t*) &(peripheral->uuid128), uuid16);
1513     peripheral->characteristic_start_handle = 0;
1514     peripheral->gatt_client_state = P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY;
1515 
1516     gatt_client_run();
1517     return 0;
1518 }
1519 
1520 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){
1521     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1522 
1523     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1524     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
1525 
1526     peripheral->callback = callback;
1527     peripheral->start_group_handle = start_handle;
1528     peripheral->end_group_handle   = end_handle;
1529     peripheral->filter_with_uuid = 1;
1530     peripheral->uuid16 = 0;
1531     memcpy(peripheral->uuid128, uuid128, 16);
1532     peripheral->characteristic_start_handle = 0;
1533     peripheral->gatt_client_state = P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY;
1534 
1535     gatt_client_run();
1536     return 0;
1537 }
1538 
1539 
1540 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){
1541     return gatt_client_discover_characteristics_for_handle_range_by_uuid16(callback, handle, service->start_group_handle, service->end_group_handle, uuid16);
1542 }
1543 
1544 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){
1545     return gatt_client_discover_characteristics_for_handle_range_by_uuid128(callback, handle, service->start_group_handle, service->end_group_handle, uuid128);
1546 }
1547 
1548 uint8_t gatt_client_discover_characteristic_descriptors(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t *characteristic){
1549     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1550 
1551     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1552     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
1553 
1554     if (characteristic->value_handle == characteristic->end_handle){
1555         emit_gatt_complete_event(peripheral, 0);
1556         return 0;
1557     }
1558     peripheral->callback = callback;
1559     peripheral->start_group_handle = characteristic->value_handle + 1;
1560     peripheral->end_group_handle   = characteristic->end_handle;
1561     peripheral->gatt_client_state = P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY;
1562 
1563     gatt_client_run();
1564     return 0;
1565 }
1566 
1567 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){
1568     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1569 
1570     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1571     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
1572 
1573     peripheral->callback = callback;
1574     peripheral->attribute_handle = value_handle;
1575     peripheral->attribute_offset = 0;
1576     peripheral->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY;
1577     gatt_client_run();
1578     return 0;
1579 }
1580 
1581 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){
1582     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1583 
1584     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1585     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
1586 
1587     peripheral->callback = callback;
1588     peripheral->start_group_handle = start_handle;
1589     peripheral->end_group_handle = end_handle;
1590     peripheral->query_start_handle = start_handle;
1591     peripheral->query_end_handle = end_handle;
1592     peripheral->uuid16 = uuid16;
1593     uuid_add_bluetooth_prefix((uint8_t*) &(peripheral->uuid128), uuid16);
1594     peripheral->gatt_client_state = P_W2_SEND_READ_BY_TYPE_REQUEST;
1595     gatt_client_run();
1596     return 0;
1597 }
1598 
1599 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){
1600     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1601 
1602     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1603     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
1604 
1605     peripheral->callback = callback;
1606     peripheral->start_group_handle = start_handle;
1607     peripheral->end_group_handle = end_handle;
1608     peripheral->query_start_handle = start_handle;
1609     peripheral->query_end_handle = end_handle;
1610     peripheral->uuid16 = 0;
1611     memcpy(peripheral->uuid128, uuid128, 16);
1612     peripheral->gatt_client_state = P_W2_SEND_READ_BY_TYPE_REQUEST;
1613     gatt_client_run();
1614     return 0;
1615 }
1616 
1617 
1618 uint8_t gatt_client_read_value_of_characteristic(btstack_packet_handler_t callback, uint16_t handle, gatt_client_characteristic_t *characteristic){
1619     return gatt_client_read_value_of_characteristic_using_value_handle(callback, handle, characteristic->value_handle);
1620 }
1621 
1622 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){
1623     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1624 
1625     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1626     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
1627 
1628     peripheral->callback = callback;
1629     peripheral->attribute_handle = characteristic_value_handle;
1630     peripheral->attribute_offset = offset;
1631     peripheral->gatt_client_state = P_W2_SEND_READ_BLOB_QUERY;
1632     gatt_client_run();
1633     return 0;
1634 }
1635 
1636 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){
1637     return gatt_client_read_long_value_of_characteristic_using_value_handle_with_offset(callback, con_handle, characteristic_value_handle, 0);
1638 }
1639 
1640 uint8_t gatt_client_read_long_value_of_characteristic(btstack_packet_handler_t callback, uint16_t handle, gatt_client_characteristic_t *characteristic){
1641     return gatt_client_read_long_value_of_characteristic_using_value_handle(callback, handle, characteristic->value_handle);
1642 }
1643 
1644 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){
1645     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1646 
1647     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1648     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
1649 
1650     peripheral->callback = callback;
1651     peripheral->read_multiple_handle_count = num_value_handles;
1652     peripheral->read_multiple_handles = value_handles;
1653     peripheral->gatt_client_state = P_W2_SEND_READ_MULTIPLE_REQUEST;
1654     gatt_client_run();
1655     return 0;
1656 }
1657 
1658 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){
1659     gatt_client_t * peripheral = provide_context_for_conn_handle(con_handle);
1660 
1661     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1662     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
1663 
1664     if (value_length > peripheral_mtu(peripheral) - 3) return GATT_CLIENT_VALUE_TOO_LONG;
1665     if (!att_dispatch_client_can_send_now(peripheral->con_handle)) return GATT_CLIENT_BUSY;
1666 
1667     att_write_request(ATT_WRITE_COMMAND, peripheral->con_handle, value_handle, value_length, value);
1668     return 0;
1669 }
1670 
1671 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){
1672     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1673 
1674     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1675     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
1676 
1677     peripheral->callback = callback;
1678     peripheral->attribute_handle = value_handle;
1679     peripheral->attribute_length = value_length;
1680     peripheral->attribute_value = data;
1681     peripheral->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_VALUE;
1682     gatt_client_run();
1683     return 0;
1684 }
1685 
1686 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){
1687     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1688 
1689     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1690     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
1691 
1692     peripheral->callback = callback;
1693     peripheral->attribute_handle = value_handle;
1694     peripheral->attribute_length = value_length;
1695     peripheral->attribute_offset = offset;
1696     peripheral->attribute_value = data;
1697     peripheral->gatt_client_state = P_W2_PREPARE_WRITE;
1698     gatt_client_run();
1699     return 0;
1700 }
1701 
1702 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){
1703     return gatt_client_write_long_value_of_characteristic_with_offset(callback, con_handle, value_handle, 0, value_length, value);
1704 }
1705 
1706 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){
1707     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1708 
1709     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1710     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
1711 
1712     peripheral->callback = callback;
1713     peripheral->attribute_handle = value_handle;
1714     peripheral->attribute_length = value_length;
1715     peripheral->attribute_offset = 0;
1716     peripheral->attribute_value = value;
1717     peripheral->gatt_client_state = P_W2_PREPARE_RELIABLE_WRITE;
1718     gatt_client_run();
1719     return 0;
1720 }
1721 
1722 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){
1723     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1724 
1725     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1726     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
1727 
1728     if ( (configuration & GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION) &&
1729         (characteristic->properties & ATT_PROPERTY_NOTIFY) == 0) {
1730         log_info("gatt_client_write_client_characteristic_configuration: GATT_CLIENT_CHARACTERISTIC_NOTIFICATION_NOT_SUPPORTED");
1731         return GATT_CLIENT_CHARACTERISTIC_NOTIFICATION_NOT_SUPPORTED;
1732     } else if ( (configuration & GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_INDICATION) &&
1733                (characteristic->properties & ATT_PROPERTY_INDICATE) == 0){
1734         log_info("gatt_client_write_client_characteristic_configuration: GATT_CLIENT_CHARACTERISTIC_INDICATION_NOT_SUPPORTED");
1735         return GATT_CLIENT_CHARACTERISTIC_INDICATION_NOT_SUPPORTED;
1736     }
1737 
1738     peripheral->callback = callback;
1739     peripheral->start_group_handle = characteristic->value_handle;
1740     peripheral->end_group_handle = characteristic->end_handle;
1741     little_endian_store_16(peripheral->client_characteristic_configuration_value, 0, configuration);
1742 
1743     peripheral->gatt_client_state = P_W2_SEND_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY;
1744     gatt_client_run();
1745     return 0;
1746 }
1747 
1748 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){
1749     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1750 
1751     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1752     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
1753 
1754     peripheral->callback = callback;
1755     peripheral->attribute_handle = descriptor_handle;
1756 
1757     peripheral->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY;
1758     gatt_client_run();
1759     return 0;
1760 }
1761 
1762 uint8_t gatt_client_read_characteristic_descriptor(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_descriptor_t * descriptor){
1763     return gatt_client_read_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle);
1764 }
1765 
1766 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){
1767     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1768 
1769     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1770     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
1771 
1772     peripheral->callback = callback;
1773     peripheral->attribute_handle = descriptor_handle;
1774     peripheral->attribute_offset = offset;
1775     peripheral->gatt_client_state = P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY;
1776     gatt_client_run();
1777     return 0;
1778 }
1779 
1780 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){
1781     return gatt_client_read_long_characteristic_descriptor_using_descriptor_handle_with_offset(callback, con_handle, descriptor_handle, 0);
1782 }
1783 
1784 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){
1785     return gatt_client_read_long_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle);
1786 }
1787 
1788 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){
1789     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1790 
1791     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1792     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
1793 
1794     peripheral->callback = callback;
1795     peripheral->attribute_handle = descriptor_handle;
1796     peripheral->attribute_length = length;
1797     peripheral->attribute_offset = 0;
1798     peripheral->attribute_value = data;
1799     peripheral->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR;
1800     gatt_client_run();
1801     return 0;
1802 }
1803 
1804 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){
1805     return gatt_client_write_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle, length, value);
1806 }
1807 
1808 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){
1809     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1810 
1811     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1812     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
1813 
1814     peripheral->callback = callback;
1815     peripheral->attribute_handle = descriptor_handle;
1816     peripheral->attribute_length = length;
1817     peripheral->attribute_offset = offset;
1818     peripheral->attribute_value = data;
1819     peripheral->gatt_client_state = P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR;
1820     gatt_client_run();
1821     return 0;
1822 }
1823 
1824 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){
1825     return gatt_client_write_long_characteristic_descriptor_using_descriptor_handle_with_offset(callback, con_handle, descriptor_handle, 0, length, data );
1826 }
1827 
1828 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){
1829     return gatt_client_write_long_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle, length, value);
1830 }
1831 
1832 /**
1833  * @brief -> gatt complete event
1834  */
1835 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){
1836     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1837 
1838     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1839     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
1840 
1841     peripheral->callback = callback;
1842     peripheral->attribute_handle = attribute_handle;
1843     peripheral->attribute_length = length;
1844     peripheral->attribute_offset = offset;
1845     peripheral->attribute_value = data;
1846     peripheral->gatt_client_state = P_W2_PREPARE_WRITE_SINGLE;
1847     gatt_client_run();
1848     return 0;
1849 }
1850 
1851 /**
1852  * @brief -> gatt complete event
1853  */
1854 uint8_t gatt_client_execute_write(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
1855     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1856 
1857     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1858     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
1859 
1860     peripheral->callback = callback;
1861     peripheral->gatt_client_state = P_W2_EXECUTE_PREPARED_WRITE;
1862     gatt_client_run();
1863     return 0;
1864 }
1865 
1866 /**
1867  * @brief -> gatt complete event
1868  */
1869 uint8_t gatt_client_cancel_write(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
1870     gatt_client_t * peripheral = provide_context_for_conn_handle_and_start_timer(con_handle);
1871 
1872     if (!peripheral) return BTSTACK_MEMORY_ALLOC_FAILED;
1873     if (!is_ready(peripheral)) return GATT_CLIENT_IN_WRONG_STATE;
1874 
1875     peripheral->callback = callback;
1876     peripheral->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE;
1877     gatt_client_run();
1878     return 0;
1879 }
1880 
1881 void gatt_client_deserialize_service(const uint8_t *packet, int offset, gatt_client_service_t *service){
1882     service->start_group_handle = little_endian_read_16(packet, offset);
1883     service->end_group_handle = little_endian_read_16(packet, offset + 2);
1884     reverse_128(&packet[offset + 4], service->uuid128);
1885     if (uuid_has_bluetooth_prefix(service->uuid128)){
1886         service->uuid16 = big_endian_read_32(service->uuid128, 0);
1887     }
1888 }
1889 
1890 void gatt_client_deserialize_characteristic(const uint8_t * packet, int offset, gatt_client_characteristic_t * characteristic){
1891     characteristic->start_handle = little_endian_read_16(packet, offset);
1892     characteristic->value_handle = little_endian_read_16(packet, offset + 2);
1893     characteristic->end_handle = little_endian_read_16(packet, offset + 4);
1894     characteristic->properties = little_endian_read_16(packet, offset + 6);
1895     reverse_128(&packet[offset+8], characteristic->uuid128);
1896     if (uuid_has_bluetooth_prefix(characteristic->uuid128)){
1897         characteristic->uuid16 = big_endian_read_32(characteristic->uuid128, 0);
1898     }
1899 }
1900 
1901 void gatt_client_deserialize_characteristic_descriptor(const uint8_t * packet, int offset, gatt_client_characteristic_descriptor_t * descriptor){
1902     descriptor->handle = little_endian_read_16(packet, offset);
1903     reverse_128(&packet[offset+2], descriptor->uuid128);
1904     if (uuid_has_bluetooth_prefix(descriptor->uuid128)){
1905         descriptor->uuid16 = big_endian_read_32(descriptor->uuid128, 0);
1906     }
1907 }
1908 
1909 void gatt_client_send_mtu_negotiation(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
1910     gatt_client_t * context = provide_context_for_conn_handle(con_handle);
1911     if (!context) return;
1912     if (context->mtu_state == MTU_AUTO_EXCHANGE_DISABLED){
1913         context->callback = callback;
1914         context->mtu_state = SEND_MTU_EXCHANGE;
1915         gatt_client_run();
1916     }
1917 }
1918