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