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