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