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