xref: /btstack/src/ble/gatt_client.c (revision 2eaf98e75b0d017b1cf9b76badc55b9bffd2c354)
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 BLUEKITCHEN
24  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 #define BTSTACK_FILE__ "gatt_client.c"
39 
40 #include <stdint.h>
41 #include <string.h>
42 #include <stddef.h>
43 
44 #include "btstack_config.h"
45 
46 #include "ble/att_dispatch.h"
47 #include "ble/att_db.h"
48 #include "ble/gatt_client.h"
49 #include "ble/le_device_db.h"
50 #include "ble/sm.h"
51 #include "btstack_debug.h"
52 #include "btstack_event.h"
53 #include "btstack_memory.h"
54 #include "btstack_run_loop.h"
55 #include "btstack_util.h"
56 #include "hci.h"
57 #include "hci_dump.h"
58 #include "l2cap.h"
59 #include "classic/sdp_client.h"
60 #include "bluetooth_gatt.h"
61 #include "bluetooth_sdp.h"
62 #include "classic/sdp_util.h"
63 
64 static btstack_linked_list_t gatt_client_connections;
65 static btstack_linked_list_t gatt_client_value_listeners;
66 static btstack_packet_callback_registration_t hci_event_callback_registration;
67 static btstack_packet_callback_registration_t sm_event_callback_registration;
68 
69 // GATT Client Configuration
70 static bool                 gatt_client_mtu_exchange_enabled;
71 static gap_security_level_t gatt_client_required_security_level;
72 
73 static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size);
74 static void gatt_client_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size);
75 static void gatt_client_report_error_if_pending(gatt_client_t *gatt_client, uint8_t att_error_code);
76 
77 #ifdef ENABLE_LE_SIGNED_WRITE
78 static void att_signed_write_handle_cmac_result(uint8_t hash[8]);
79 #endif
80 
81 void gatt_client_init(void){
82     gatt_client_connections = NULL;
83 
84     // default configuration
85     gatt_client_mtu_exchange_enabled    = true;
86     gatt_client_required_security_level = LEVEL_0;
87 
88     // register for HCI Events
89     hci_event_callback_registration.callback = &gatt_client_event_packet_handler;
90     hci_add_event_handler(&hci_event_callback_registration);
91 
92     // register for SM Events
93     sm_event_callback_registration.callback = &gatt_client_event_packet_handler;
94     sm_add_event_handler(&sm_event_callback_registration);
95 
96     // and ATT Client PDUs
97     att_dispatch_register_client(gatt_client_att_packet_handler);
98 }
99 
100 void gatt_client_set_required_security_level(gap_security_level_t level){
101     gatt_client_required_security_level = level;
102 }
103 
104 static gatt_client_t * gatt_client_for_timer(btstack_timer_source_t * ts){
105     btstack_linked_list_iterator_t it;
106     btstack_linked_list_iterator_init(&it, &gatt_client_connections);
107     while (btstack_linked_list_iterator_has_next(&it)){
108         gatt_client_t * gatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it);
109         if (&gatt_client->gc_timeout == ts) {
110             return gatt_client;
111         }
112     }
113     return NULL;
114 }
115 
116 static void gatt_client_timeout_handler(btstack_timer_source_t * timer){
117     gatt_client_t * gatt_client = gatt_client_for_timer(timer);
118     if (gatt_client == NULL) return;
119     log_info("GATT client timeout handle, handle 0x%02x", gatt_client->con_handle);
120     gatt_client_report_error_if_pending(gatt_client, ATT_ERROR_TIMEOUT);
121 }
122 
123 static void gatt_client_timeout_start(gatt_client_t * gatt_client){
124     log_info("GATT client timeout start, handle 0x%02x", gatt_client->con_handle);
125     btstack_run_loop_remove_timer(&gatt_client->gc_timeout);
126     btstack_run_loop_set_timer_handler(&gatt_client->gc_timeout, gatt_client_timeout_handler);
127     btstack_run_loop_set_timer(&gatt_client->gc_timeout, 30000); // 30 seconds sm timeout
128     btstack_run_loop_add_timer(&gatt_client->gc_timeout);
129 }
130 
131 static void gatt_client_timeout_stop(gatt_client_t * gatt_client){
132     log_info("GATT client timeout stop, handle 0x%02x", gatt_client->con_handle);
133     btstack_run_loop_remove_timer(&gatt_client->gc_timeout);
134 }
135 
136 static gap_security_level_t gatt_client_le_security_level_for_connection(hci_con_handle_t con_handle){
137     uint8_t encryption_key_size = gap_encryption_key_size(con_handle);
138     if (encryption_key_size == 0) return LEVEL_0;
139 
140     bool authenticated = gap_authenticated(con_handle);
141     if (!authenticated) return LEVEL_2;
142 
143     return encryption_key_size == 16 ? LEVEL_4 : LEVEL_3;
144 }
145 
146 static gatt_client_t * gatt_client_get_context_for_handle(uint16_t handle){
147     btstack_linked_item_t *it;
148     for (it = (btstack_linked_item_t *) gatt_client_connections; it != NULL; it = it->next){
149         gatt_client_t * gatt_client = (gatt_client_t *) it;
150         if (gatt_client->con_handle == handle){
151             return gatt_client;
152         }
153     }
154     return NULL;
155 }
156 
157 
158 // @return gatt_client context
159 // returns existing one, or tries to setup new one
160 static uint8_t gatt_client_provide_context_for_handle(hci_con_handle_t con_handle, gatt_client_t ** out_gatt_client){
161     gatt_client_t * gatt_client = gatt_client_get_context_for_handle(con_handle);
162 
163     if (gatt_client != NULL){
164         *out_gatt_client = gatt_client;
165         return ERROR_CODE_SUCCESS;
166     }
167 
168     // bail if no such hci connection
169     hci_connection_t * hci_connection = hci_connection_for_handle(con_handle);
170     if (hci_connection == NULL){
171         log_error("No connection for handle 0x%04x", con_handle);
172         *out_gatt_client = NULL;
173         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
174     }
175 
176     gatt_client = btstack_memory_gatt_client_get();
177     if (gatt_client == NULL){
178         *out_gatt_client = NULL;
179         return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED;
180     }
181     // init state
182     gatt_client->con_handle = con_handle;
183     gatt_client->mtu = ATT_DEFAULT_MTU;
184     gatt_client->security_level = gatt_client_le_security_level_for_connection(con_handle);
185     if (gatt_client_mtu_exchange_enabled){
186         gatt_client->mtu_state = SEND_MTU_EXCHANGE;
187     } else {
188         gatt_client->mtu_state = MTU_AUTO_EXCHANGE_DISABLED;
189     }
190     gatt_client->gatt_client_state = P_READY;
191     btstack_linked_list_add(&gatt_client_connections, (btstack_linked_item_t*)gatt_client);
192 
193     // get unenhanced att bearer state
194     if (hci_connection->att_connection.mtu_exchanged){
195         gatt_client->mtu = hci_connection->att_connection.mtu;
196         gatt_client->mtu_state = MTU_EXCHANGED;
197     }
198     *out_gatt_client = gatt_client;
199     return ERROR_CODE_SUCCESS;
200 }
201 
202 static uint8_t gatt_client_provide_context_for_handle_and_start_timer(hci_con_handle_t con_handle, gatt_client_t ** out_gatt_client){
203     uint8_t status = gatt_client_provide_context_for_handle(con_handle, out_gatt_client);
204     if (status != ERROR_CODE_SUCCESS){
205         return status;
206     }
207     gatt_client_timeout_start(*out_gatt_client);
208     return status;
209 }
210 
211 static bool is_ready(gatt_client_t * gatt_client){
212     return gatt_client->gatt_client_state == P_READY;
213 }
214 
215 int gatt_client_is_ready(hci_con_handle_t con_handle){
216     gatt_client_t * gatt_client;
217     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
218     if (status != ERROR_CODE_SUCCESS){
219         return 0;
220     }
221     return is_ready(gatt_client) ? 1 : 0;
222 }
223 
224 void gatt_client_mtu_enable_auto_negotiation(uint8_t enabled){
225     gatt_client_mtu_exchange_enabled = enabled != 0;
226 }
227 
228 uint8_t gatt_client_get_mtu(hci_con_handle_t con_handle, uint16_t * mtu){
229     gatt_client_t * gatt_client;
230     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
231     if (status != ERROR_CODE_SUCCESS){
232         return status;
233     }
234 
235     if ((gatt_client->mtu_state == MTU_EXCHANGED) || (gatt_client->mtu_state == MTU_AUTO_EXCHANGE_DISABLED)){
236         *mtu = gatt_client->mtu;
237         return ERROR_CODE_SUCCESS;
238     }
239     *mtu = ATT_DEFAULT_MTU;
240     return GATT_CLIENT_IN_WRONG_STATE;
241 }
242 
243 // precondition: can_send_packet_now == TRUE
244 static uint8_t gatt_client_send(gatt_client_t * gatt_client, uint16_t len){
245 #ifdef ENABLE_GATT_OVER_CLASSIC
246     if (gatt_client->l2cap_psm){
247         return l2cap_send_prepared(gatt_client->l2cap_cid, len);
248     }
249 #endif
250     return l2cap_send_prepared_connectionless(gatt_client->con_handle, L2CAP_CID_ATTRIBUTE_PROTOCOL, len);
251 }
252 
253 // precondition: can_send_packet_now == TRUE
254 static uint8_t att_confirmation(gatt_client_t * gatt_client) {
255     l2cap_reserve_packet_buffer();
256 
257     uint8_t * request = l2cap_get_outgoing_buffer();
258     request[0] = ATT_HANDLE_VALUE_CONFIRMATION;
259 
260     return gatt_client_send(gatt_client, 1);
261 }
262 
263 // precondition: can_send_packet_now == TRUE
264 static uint8_t att_find_information_request(gatt_client_t *gatt_client, uint8_t request_type, uint16_t start_handle,
265                                             uint16_t end_handle) {
266     l2cap_reserve_packet_buffer();
267     uint8_t * request = l2cap_get_outgoing_buffer();
268 
269     request[0] = request_type;
270     little_endian_store_16(request, 1, start_handle);
271     little_endian_store_16(request, 3, end_handle);
272 
273     return gatt_client_send(gatt_client, 5);
274 }
275 
276 // precondition: can_send_packet_now == TRUE
277 static uint8_t
278 att_find_by_type_value_request(gatt_client_t *gatt_client, uint8_t request_type, uint16_t attribute_group_type,
279                                uint16_t start_handle, uint16_t end_handle, uint8_t *value, uint16_t value_size) {
280     l2cap_reserve_packet_buffer();
281     uint8_t * request = l2cap_get_outgoing_buffer();
282 
283     request[0] = request_type;
284     little_endian_store_16(request, 1, start_handle);
285     little_endian_store_16(request, 3, end_handle);
286     little_endian_store_16(request, 5, attribute_group_type);
287     (void)memcpy(&request[7], value, value_size);
288 
289     return gatt_client_send(gatt_client, 7u + value_size);
290 }
291 
292 // precondition: can_send_packet_now == TRUE
293 static uint8_t
294 att_read_by_type_or_group_request_for_uuid16(gatt_client_t *gatt_client, uint8_t request_type, uint16_t uuid16,
295                                              uint16_t start_handle, uint16_t end_handle) {
296     l2cap_reserve_packet_buffer();
297     uint8_t * request = l2cap_get_outgoing_buffer();
298 
299     request[0] = request_type;
300     little_endian_store_16(request, 1, start_handle);
301     little_endian_store_16(request, 3, end_handle);
302     little_endian_store_16(request, 5, uuid16);
303 
304     return gatt_client_send(gatt_client, 7);
305 }
306 
307 // precondition: can_send_packet_now == TRUE
308 static uint8_t
309 att_read_by_type_or_group_request_for_uuid128(gatt_client_t *gatt_client, uint8_t request_type, const uint8_t *uuid128,
310                                               uint16_t start_handle, uint16_t end_handle) {
311     l2cap_reserve_packet_buffer();
312     uint8_t * request = l2cap_get_outgoing_buffer();
313 
314     request[0] = request_type;
315     little_endian_store_16(request, 1, start_handle);
316     little_endian_store_16(request, 3, end_handle);
317     reverse_128(uuid128, &request[5]);
318 
319     return gatt_client_send(gatt_client, 21);
320 }
321 
322 // precondition: can_send_packet_now == TRUE
323 static uint8_t att_read_request(gatt_client_t *gatt_client, uint8_t request_type, uint16_t attribute_handle) {
324     l2cap_reserve_packet_buffer();
325     uint8_t * request = l2cap_get_outgoing_buffer();
326 
327     request[0] = request_type;
328     little_endian_store_16(request, 1, attribute_handle);
329 
330     return gatt_client_send(gatt_client, 3);
331 }
332 
333 // precondition: can_send_packet_now == TRUE
334 static uint8_t att_read_blob_request(gatt_client_t *gatt_client, uint8_t request_type, uint16_t attribute_handle,
335                                      uint16_t value_offset) {
336     l2cap_reserve_packet_buffer();
337     uint8_t * request = l2cap_get_outgoing_buffer();
338     request[0] = request_type;
339     little_endian_store_16(request, 1, attribute_handle);
340     little_endian_store_16(request, 3, value_offset);
341 
342     return gatt_client_send(gatt_client, 5);
343 }
344 
345 static uint8_t
346 att_read_multiple_request(gatt_client_t *gatt_client, uint16_t num_value_handles, uint16_t *value_handles) {
347     l2cap_reserve_packet_buffer();
348     uint8_t * request = l2cap_get_outgoing_buffer();
349     request[0] = ATT_READ_MULTIPLE_REQUEST;
350     int i;
351     int offset = 1;
352     for (i=0;i<num_value_handles;i++){
353         little_endian_store_16(request, offset, value_handles[i]);
354         offset += 2;
355     }
356 
357     return gatt_client_send(gatt_client, offset);
358 }
359 
360 #ifdef ENABLE_LE_SIGNED_WRITE
361 // precondition: can_send_packet_now == TRUE
362 static uint8_t att_signed_write_request(gatt_client_t *gatt_client, uint16_t request_type, uint16_t attribute_handle,
363                                         uint16_t value_length, uint8_t *value, uint32_t sign_counter, uint8_t sgn[8]) {
364     l2cap_reserve_packet_buffer();
365     uint8_t * request = l2cap_get_outgoing_buffer();
366     request[0] = request_type;
367     little_endian_store_16(request, 1, attribute_handle);
368     (void)memcpy(&request[3], value, value_length);
369     little_endian_store_32(request, 3 + value_length, sign_counter);
370     reverse_64(sgn, &request[3 + value_length + 4]);
371 
372     return gatt_client_send(gatt_client, 3 + value_length + 12);
373 }
374 #endif
375 
376 // precondition: can_send_packet_now == TRUE
377 static uint8_t
378 att_write_request(gatt_client_t *gatt_client, uint8_t request_type, uint16_t attribute_handle, uint16_t value_length,
379                   uint8_t *value) {
380     l2cap_reserve_packet_buffer();
381     uint8_t * request = l2cap_get_outgoing_buffer();
382     request[0] = request_type;
383     little_endian_store_16(request, 1, attribute_handle);
384     (void)memcpy(&request[3], value, value_length);
385 
386     return gatt_client_send(gatt_client, 3u + value_length);
387 }
388 
389 // precondition: can_send_packet_now == TRUE
390 static uint8_t att_execute_write_request(gatt_client_t *gatt_client, uint8_t request_type, uint8_t execute_write) {
391     l2cap_reserve_packet_buffer();
392     uint8_t * request = l2cap_get_outgoing_buffer();
393     request[0] = request_type;
394     request[1] = execute_write;
395 
396     return gatt_client_send(gatt_client, 2);
397 }
398 
399 // precondition: can_send_packet_now == TRUE
400 static uint8_t att_prepare_write_request(gatt_client_t *gatt_client, uint8_t request_type, uint16_t attribute_handle,
401                                          uint16_t value_offset, uint16_t blob_length, uint8_t *value) {
402     l2cap_reserve_packet_buffer();
403     uint8_t * request = l2cap_get_outgoing_buffer();
404     request[0] = request_type;
405     little_endian_store_16(request, 1, attribute_handle);
406     little_endian_store_16(request, 3, value_offset);
407     (void)memcpy(&request[5], &value[value_offset], blob_length);
408 
409     return gatt_client_send(gatt_client,  5u + blob_length);
410 }
411 
412 static uint8_t att_exchange_mtu_request(gatt_client_t *gatt_client) {
413     uint16_t mtu = l2cap_max_le_mtu();
414     l2cap_reserve_packet_buffer();
415     uint8_t * request = l2cap_get_outgoing_buffer();
416     request[0] = ATT_EXCHANGE_MTU_REQUEST;
417     little_endian_store_16(request, 1, mtu);
418 
419     return gatt_client_send(gatt_client, 3);
420 }
421 
422 static uint16_t write_blob_length(gatt_client_t * gatt_client){
423     uint16_t max_blob_length = gatt_client->mtu - 5u;
424     if (gatt_client->attribute_offset >= gatt_client->attribute_length) {
425         return 0;
426     }
427     uint16_t rest_length = gatt_client->attribute_length - gatt_client->attribute_offset;
428     if (max_blob_length > rest_length){
429         return rest_length;
430     }
431     return max_blob_length;
432 }
433 
434 static void send_gatt_services_request(gatt_client_t *gatt_client){
435     att_read_by_type_or_group_request_for_uuid16(gatt_client, ATT_READ_BY_GROUP_TYPE_REQUEST,
436                                                  gatt_client->uuid16, gatt_client->start_group_handle,
437                                                  gatt_client->end_group_handle);
438 }
439 
440 static void send_gatt_by_uuid_request(gatt_client_t *gatt_client, uint16_t attribute_group_type){
441     if (gatt_client->uuid16){
442         uint8_t uuid16[2];
443         little_endian_store_16(uuid16, 0, gatt_client->uuid16);
444         att_find_by_type_value_request(gatt_client, ATT_FIND_BY_TYPE_VALUE_REQUEST, attribute_group_type,
445                                        gatt_client->start_group_handle, gatt_client->end_group_handle, uuid16, 2);
446         return;
447     }
448     uint8_t uuid128[16];
449     reverse_128(gatt_client->uuid128, uuid128);
450     att_find_by_type_value_request(gatt_client, ATT_FIND_BY_TYPE_VALUE_REQUEST, attribute_group_type,
451                                    gatt_client->start_group_handle, gatt_client->end_group_handle, uuid128, 16);
452 }
453 
454 static void send_gatt_services_by_uuid_request(gatt_client_t *gatt_client){
455     send_gatt_by_uuid_request(gatt_client, GATT_PRIMARY_SERVICE_UUID);
456 }
457 
458 static void send_gatt_included_service_uuid_request(gatt_client_t *gatt_client){
459     att_read_request(gatt_client, ATT_READ_REQUEST, gatt_client->query_start_handle);
460 }
461 
462 static void send_gatt_included_service_request(gatt_client_t *gatt_client){
463     att_read_by_type_or_group_request_for_uuid16(gatt_client, ATT_READ_BY_TYPE_REQUEST,
464                                                  GATT_INCLUDE_SERVICE_UUID, gatt_client->start_group_handle,
465                                                  gatt_client->end_group_handle);
466 }
467 
468 static void send_gatt_characteristic_request(gatt_client_t *gatt_client){
469     att_read_by_type_or_group_request_for_uuid16(gatt_client, ATT_READ_BY_TYPE_REQUEST,
470                                                  GATT_CHARACTERISTICS_UUID, gatt_client->start_group_handle,
471                                                  gatt_client->end_group_handle);
472 }
473 
474 static void send_gatt_characteristic_descriptor_request(gatt_client_t *gatt_client){
475     att_find_information_request(gatt_client, ATT_FIND_INFORMATION_REQUEST, gatt_client->start_group_handle,
476                                  gatt_client->end_group_handle);
477 }
478 
479 static void send_gatt_read_characteristic_value_request(gatt_client_t *gatt_client){
480     att_read_request(gatt_client, ATT_READ_REQUEST, gatt_client->attribute_handle);
481 }
482 
483 static void send_gatt_read_by_type_request(gatt_client_t * gatt_client){
484     if (gatt_client->uuid16){
485         att_read_by_type_or_group_request_for_uuid16(gatt_client, ATT_READ_BY_TYPE_REQUEST,
486                                                      gatt_client->uuid16, gatt_client->start_group_handle,
487                                                      gatt_client->end_group_handle);
488     } else {
489         att_read_by_type_or_group_request_for_uuid128(gatt_client, ATT_READ_BY_TYPE_REQUEST,
490                                                       gatt_client->uuid128, gatt_client->start_group_handle,
491                                                       gatt_client->end_group_handle);
492     }
493 }
494 
495 static void send_gatt_read_blob_request(gatt_client_t *gatt_client){
496     if (gatt_client->attribute_offset == 0){
497         att_read_request(gatt_client, ATT_READ_REQUEST, gatt_client->attribute_handle);
498     } else {
499         att_read_blob_request(gatt_client, ATT_READ_BLOB_REQUEST, gatt_client->attribute_handle,
500                               gatt_client->attribute_offset);
501     }
502 }
503 
504 static void send_gatt_read_multiple_request(gatt_client_t * gatt_client){
505     att_read_multiple_request(gatt_client, gatt_client->read_multiple_handle_count, gatt_client->read_multiple_handles);
506 }
507 
508 static void send_gatt_write_attribute_value_request(gatt_client_t * gatt_client){
509     att_write_request(gatt_client, ATT_WRITE_REQUEST, gatt_client->attribute_handle, gatt_client->attribute_length,
510                       gatt_client->attribute_value);
511 }
512 
513 static void send_gatt_write_client_characteristic_configuration_request(gatt_client_t * gatt_client){
514     att_write_request(gatt_client, ATT_WRITE_REQUEST, gatt_client->client_characteristic_configuration_handle, 2,
515                       gatt_client->client_characteristic_configuration_value);
516 }
517 
518 static void send_gatt_prepare_write_request(gatt_client_t * gatt_client){
519     att_prepare_write_request(gatt_client, ATT_PREPARE_WRITE_REQUEST, gatt_client->attribute_handle,
520                               gatt_client->attribute_offset, write_blob_length(gatt_client),
521                               gatt_client->attribute_value);
522 }
523 
524 static void send_gatt_execute_write_request(gatt_client_t * gatt_client){
525     att_execute_write_request(gatt_client, ATT_EXECUTE_WRITE_REQUEST, 1);
526 }
527 
528 static void send_gatt_cancel_prepared_write_request(gatt_client_t * gatt_client){
529     att_execute_write_request(gatt_client, ATT_EXECUTE_WRITE_REQUEST, 0);
530 }
531 
532 #ifndef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY
533 static void send_gatt_read_client_characteristic_configuration_request(gatt_client_t * gatt_client){
534     att_read_by_type_or_group_request_for_uuid16(gatt_client, ATT_READ_BY_TYPE_REQUEST,
535                                                  GATT_CLIENT_CHARACTERISTICS_CONFIGURATION,
536                                                  gatt_client->start_group_handle, gatt_client->end_group_handle);
537 }
538 #endif
539 
540 static void send_gatt_read_characteristic_descriptor_request(gatt_client_t * gatt_client){
541     att_read_request(gatt_client, ATT_READ_REQUEST, gatt_client->attribute_handle);
542 }
543 
544 #ifdef ENABLE_LE_SIGNED_WRITE
545 static void send_gatt_signed_write_request(gatt_client_t * gatt_client, uint32_t sign_counter){
546     att_signed_write_request(gatt_client, ATT_SIGNED_WRITE_COMMAND, gatt_client->attribute_handle,
547                              gatt_client->attribute_length, gatt_client->attribute_value, sign_counter,
548                              gatt_client->cmac);
549 }
550 #endif
551 
552 static uint16_t get_last_result_handle_from_service_list(uint8_t * packet, uint16_t size){
553     if (size < 2) return 0xffff;
554     uint8_t attr_length = packet[1];
555     if ((2 + attr_length) > size) return 0xffff;
556     return little_endian_read_16(packet, size - attr_length + 2u);
557 }
558 
559 static uint16_t get_last_result_handle_from_characteristics_list(uint8_t * packet, uint16_t size){
560     if (size < 2) return 0xffff;
561     uint8_t attr_length = packet[1];
562     if ((2 + attr_length) > size) return 0xffff;
563     return little_endian_read_16(packet, size - attr_length + 3u);
564 }
565 
566 static uint16_t get_last_result_handle_from_included_services_list(uint8_t * packet, uint16_t size){
567     if (size < 2) return 0xffff;
568     uint8_t attr_length = packet[1];
569     if ((2 + attr_length) > size) return 0xffff;
570     return little_endian_read_16(packet, size - attr_length);
571 }
572 
573 static void gatt_client_notify_can_send_query(gatt_client_t * gatt_client){
574     while (gatt_client->gatt_client_state == P_READY){
575         btstack_context_callback_registration_t * callback = (btstack_context_callback_registration_t *) btstack_linked_list_pop(&gatt_client->query_requests);
576         if (callback == NULL) {
577             return;
578         }
579         (*callback->callback)(callback->context);
580     }
581 }
582 
583 static void gatt_client_handle_transaction_complete(gatt_client_t * gatt_client){
584     gatt_client->gatt_client_state = P_READY;
585     gatt_client_timeout_stop(gatt_client);
586     gatt_client_notify_can_send_query(gatt_client);
587 }
588 
589 static void emit_event_new(btstack_packet_handler_t callback, uint8_t * packet, uint16_t size){
590     if (!callback) return;
591     hci_dump_packet(HCI_EVENT_PACKET, 1, packet, size);
592     (*callback)(HCI_EVENT_PACKET, 0, packet, size);
593 }
594 
595 void gatt_client_listen_for_characteristic_value_updates(gatt_client_notification_t * notification, btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic){
596     notification->callback = callback;
597     notification->con_handle = con_handle;
598     if (characteristic == NULL){
599         notification->attribute_handle = GATT_CLIENT_ANY_VALUE_HANDLE;
600     } else {
601         notification->attribute_handle = characteristic->value_handle;
602     }
603     btstack_linked_list_add(&gatt_client_value_listeners, (btstack_linked_item_t*) notification);
604 }
605 
606 void gatt_client_stop_listening_for_characteristic_value_updates(gatt_client_notification_t * notification){
607     btstack_linked_list_remove(&gatt_client_value_listeners, (btstack_linked_item_t*) notification);
608 }
609 
610 static void emit_event_to_registered_listeners(hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t * packet, uint16_t size){
611     btstack_linked_list_iterator_t it;
612     btstack_linked_list_iterator_init(&it, &gatt_client_value_listeners);
613     while (btstack_linked_list_iterator_has_next(&it)){
614         gatt_client_notification_t * notification = (gatt_client_notification_t*) btstack_linked_list_iterator_next(&it);
615         if ((notification->con_handle       != GATT_CLIENT_ANY_CONNECTION)   && (notification->con_handle       != con_handle)) continue;
616         if ((notification->attribute_handle != GATT_CLIENT_ANY_VALUE_HANDLE) && (notification->attribute_handle != attribute_handle)) continue;
617         (*notification->callback)(HCI_EVENT_PACKET, 0, packet, size);
618     }
619 }
620 
621 static void emit_gatt_complete_event(gatt_client_t * gatt_client, uint8_t att_status){
622     // @format H1
623     uint8_t packet[5];
624     packet[0] = GATT_EVENT_QUERY_COMPLETE;
625     packet[1] = 3;
626     little_endian_store_16(packet, 2, gatt_client->con_handle);
627     packet[4] = att_status;
628     emit_event_new(gatt_client->callback, packet, sizeof(packet));
629 }
630 
631 static void emit_gatt_service_query_result_event(gatt_client_t * gatt_client, uint16_t start_group_handle, uint16_t end_group_handle, const uint8_t * uuid128){
632     // @format HX
633     uint8_t packet[24];
634     packet[0] = GATT_EVENT_SERVICE_QUERY_RESULT;
635     packet[1] = sizeof(packet) - 2u;
636     little_endian_store_16(packet, 2, gatt_client->con_handle);
637     ///
638     little_endian_store_16(packet, 4, start_group_handle);
639     little_endian_store_16(packet, 6, end_group_handle);
640     reverse_128(uuid128, &packet[8]);
641     emit_event_new(gatt_client->callback, packet, sizeof(packet));
642 }
643 
644 static void emit_gatt_included_service_query_result_event(gatt_client_t * gatt_client, uint16_t include_handle, uint16_t start_group_handle, uint16_t end_group_handle, const uint8_t * uuid128){
645     // @format HX
646     uint8_t packet[26];
647     packet[0] = GATT_EVENT_INCLUDED_SERVICE_QUERY_RESULT;
648     packet[1] = sizeof(packet) - 2u;
649     little_endian_store_16(packet, 2, gatt_client->con_handle);
650     ///
651     little_endian_store_16(packet, 4, include_handle);
652     //
653     little_endian_store_16(packet, 6, start_group_handle);
654     little_endian_store_16(packet, 8, end_group_handle);
655     reverse_128(uuid128, &packet[10]);
656     emit_event_new(gatt_client->callback, packet, sizeof(packet));
657 }
658 
659 static void emit_gatt_characteristic_query_result_event(gatt_client_t * gatt_client, uint16_t start_handle, uint16_t value_handle, uint16_t end_handle,
660                                                         uint16_t properties, const uint8_t * uuid128){
661     // @format HY
662     uint8_t packet[28];
663     packet[0] = GATT_EVENT_CHARACTERISTIC_QUERY_RESULT;
664     packet[1] = sizeof(packet) - 2u;
665     little_endian_store_16(packet, 2, gatt_client->con_handle);
666     ///
667     little_endian_store_16(packet, 4,  start_handle);
668     little_endian_store_16(packet, 6,  value_handle);
669     little_endian_store_16(packet, 8,  end_handle);
670     little_endian_store_16(packet, 10, properties);
671     reverse_128(uuid128, &packet[12]);
672     emit_event_new(gatt_client->callback, packet, sizeof(packet));
673 }
674 
675 static void emit_gatt_all_characteristic_descriptors_result_event(
676         gatt_client_t * gatt_client, uint16_t descriptor_handle, const uint8_t * uuid128){
677     // @format HZ
678     uint8_t packet[22];
679     packet[0] = GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT;
680     packet[1] = sizeof(packet) - 2u;
681     little_endian_store_16(packet, 2, gatt_client->con_handle);
682     ///
683     little_endian_store_16(packet, 4,  descriptor_handle);
684     reverse_128(uuid128, &packet[6]);
685     emit_event_new(gatt_client->callback, packet, sizeof(packet));
686 }
687 
688 static void emit_gatt_mtu_exchanged_result_event(gatt_client_t * gatt_client, uint16_t new_mtu){
689     // @format H2
690     uint8_t packet[6];
691     packet[0] = GATT_EVENT_MTU;
692     packet[1] = sizeof(packet) - 2u;
693     little_endian_store_16(packet, 2, gatt_client->con_handle);
694     little_endian_store_16(packet, 4, new_mtu);
695     att_dispatch_client_mtu_exchanged(gatt_client->con_handle, new_mtu);
696     emit_event_new(gatt_client->callback, packet, sizeof(packet));
697 }
698 ///
699 static void report_gatt_services(gatt_client_t * gatt_client, uint8_t * packet, uint16_t size){
700     if (size < 2) return;
701     uint8_t attr_length = packet[1];
702     uint8_t uuid_length = attr_length - 4u;
703 
704     int i;
705     for (i = 2; (i+attr_length) <= size; i += attr_length){
706         uint16_t start_group_handle = little_endian_read_16(packet,i);
707         uint16_t end_group_handle   = little_endian_read_16(packet,i+2);
708         uint8_t  uuid128[16];
709         uint16_t uuid16 = 0;
710 
711         if (uuid_length == 2u){
712             uuid16 = little_endian_read_16(packet, i+4);
713             uuid_add_bluetooth_prefix((uint8_t*) &uuid128, uuid16);
714         } else if (uuid_length == 16u) {
715             reverse_128(&packet[i+4], uuid128);
716         } else {
717             return;
718         }
719         emit_gatt_service_query_result_event(gatt_client, start_group_handle, end_group_handle, uuid128);
720     }
721 }
722 
723 // helper
724 static void characteristic_start_found(gatt_client_t * gatt_client, uint16_t start_handle, uint8_t properties, uint16_t value_handle, uint8_t * uuid, uint16_t uuid_length){
725     uint8_t uuid128[16];
726     uint16_t uuid16 = 0;
727     if (uuid_length == 2u){
728         uuid16 = little_endian_read_16(uuid, 0);
729         uuid_add_bluetooth_prefix((uint8_t*) uuid128, uuid16);
730     } else if (uuid_length == 16u){
731         reverse_128(uuid, uuid128);
732     } else {
733         return;
734     }
735 
736     if (gatt_client->filter_with_uuid && (memcmp(gatt_client->uuid128, uuid128, 16) != 0)) return;
737 
738     gatt_client->characteristic_properties = properties;
739     gatt_client->characteristic_start_handle = start_handle;
740     gatt_client->attribute_handle = value_handle;
741 
742     if (gatt_client->filter_with_uuid) return;
743 
744     gatt_client->uuid16 = uuid16;
745     (void)memcpy(gatt_client->uuid128, uuid128, 16);
746 }
747 
748 static void characteristic_end_found(gatt_client_t * gatt_client, uint16_t end_handle){
749     // TODO: stop searching if filter and uuid found
750 
751     if (!gatt_client->characteristic_start_handle) return;
752 
753     emit_gatt_characteristic_query_result_event(gatt_client, gatt_client->characteristic_start_handle, gatt_client->attribute_handle,
754                                                 end_handle, gatt_client->characteristic_properties, gatt_client->uuid128);
755 
756     gatt_client->characteristic_start_handle = 0;
757 }
758 
759 static void report_gatt_characteristics(gatt_client_t * gatt_client, uint8_t * packet, uint16_t size){
760     if (size < 2u) return;
761     uint8_t attr_length = packet[1];
762     if ((attr_length != 7u) && (attr_length != 21u)) return;
763     uint8_t uuid_length = attr_length - 5u;
764     int i;
765     for (i = 2u; (i + attr_length) <= size; i += attr_length){
766         uint16_t start_handle = little_endian_read_16(packet, i);
767         uint8_t  properties = packet[i+2];
768         uint16_t value_handle = little_endian_read_16(packet, i+3);
769         characteristic_end_found(gatt_client, start_handle - 1u);
770         characteristic_start_found(gatt_client, start_handle, properties, value_handle, &packet[i + 5], uuid_length);
771     }
772 }
773 
774 static void report_gatt_included_service_uuid16(gatt_client_t * gatt_client, uint16_t include_handle, uint16_t uuid16){
775     uint8_t normalized_uuid128[16];
776     uuid_add_bluetooth_prefix(normalized_uuid128, uuid16);
777     emit_gatt_included_service_query_result_event(gatt_client, include_handle, gatt_client->query_start_handle,
778                                                   gatt_client->query_end_handle, normalized_uuid128);
779 }
780 
781 static void report_gatt_included_service_uuid128(gatt_client_t * gatt_client, uint16_t include_handle, const uint8_t * uuid128){
782     emit_gatt_included_service_query_result_event(gatt_client, include_handle, gatt_client->query_start_handle,
783                                                   gatt_client->query_end_handle, uuid128);
784 }
785 
786 // @return packet pointer
787 // @note assume that value is part of an l2cap buffer - overwrite HCI + L2CAP packet headers
788 static const int characteristic_value_event_header_size = 8;
789 static uint8_t * setup_characteristic_value_packet(uint8_t type, hci_con_handle_t con_handle, uint16_t attribute_handle, uint8_t * value, uint16_t length){
790 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
791     // copy value into test packet for testing
792     static uint8_t packet[1000];
793     memcpy(&packet[8], value, length);
794 #else
795     // before the value inside the ATT PDU
796     uint8_t * packet = value - characteristic_value_event_header_size;
797 #endif
798     packet[0] = type;
799     packet[1] = characteristic_value_event_header_size - 2 + length;
800     little_endian_store_16(packet, 2, con_handle);
801     little_endian_store_16(packet, 4, attribute_handle);
802     little_endian_store_16(packet, 6, length);
803     return packet;
804 }
805 
806 // @return packet pointer
807 // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes
808 static const int long_characteristic_value_event_header_size = 10;
809 static uint8_t * setup_long_characteristic_value_packet(uint8_t type, hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t offset, uint8_t * value, uint16_t length){
810 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
811     // avoid using pre ATT headers.
812     return NULL;
813 #endif
814 #if defined(HCI_INCOMING_PRE_BUFFER_SIZE) && (HCI_INCOMING_PRE_BUFFER_SIZE >= 10 - 8) // L2CAP Header (4) - ACL Header (4)
815     // before the value inside the ATT PDU
816     uint8_t * packet = value - long_characteristic_value_event_header_size;
817     packet[0] = type;
818     packet[1] = long_characteristic_value_event_header_size - 2 + length;
819     little_endian_store_16(packet, 2, con_handle);
820     little_endian_store_16(packet, 4, attribute_handle);
821     little_endian_store_16(packet, 6, offset);
822     little_endian_store_16(packet, 8, length);
823     return packet;
824 #else
825     log_error("HCI_INCOMING_PRE_BUFFER_SIZE >= 2 required for long characteristic reads");
826     return NULL;
827 #endif
828 }
829 
830 // test if notification/indication should be delivered to application (BLESA)
831 static bool gatt_client_accept_server_message(gatt_client_t *gatt_client) {
832 #ifdef ENABLE_LE_PROACTIVE_AUTHENTICATION
833 	// ignore messages until re-encryption is complete
834     if (gap_reconnect_security_setup_active(gatt_client->con_handle)) return false;
835 
836 	// after that ignore if bonded but not encrypted
837 	return !gap_bonded(gatt_client->con_handle) || (gap_encryption_key_size(gatt_client->con_handle) > 0);
838 #else
839     UNUSED(gatt_client);
840 	return true;
841 #endif
842 }
843 
844 
845 // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes
846 static void report_gatt_notification(gatt_client_t *gatt_client, uint16_t value_handle, uint8_t *value, int length) {
847 	if (!gatt_client_accept_server_message(gatt_client)) return;
848     uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_NOTIFICATION, gatt_client->con_handle, value_handle, value, length);
849     emit_event_to_registered_listeners(gatt_client->con_handle, value_handle, packet, characteristic_value_event_header_size + length);
850 }
851 
852 // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes
853 static void report_gatt_indication(gatt_client_t *gatt_client, uint16_t value_handle, uint8_t *value, int length) {
854 	if (!gatt_client_accept_server_message(gatt_client)) return;
855     uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_INDICATION, gatt_client->con_handle, value_handle, value, length);
856     emit_event_to_registered_listeners(gatt_client->con_handle, value_handle, packet, characteristic_value_event_header_size + length);
857 }
858 
859 // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes
860 static void report_gatt_characteristic_value(gatt_client_t * gatt_client, uint16_t attribute_handle, uint8_t * value, uint16_t length){
861     uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT, gatt_client->con_handle, attribute_handle, value, length);
862     emit_event_new(gatt_client->callback, packet, characteristic_value_event_header_size + length);
863 }
864 
865 // @note assume that value is part of an l2cap buffer - overwrite parts of the HCI/L2CAP/ATT packet (4/4/3) bytes
866 static void report_gatt_long_characteristic_value_blob(gatt_client_t * gatt_client, uint16_t attribute_handle, uint8_t * blob, uint16_t blob_length, int value_offset){
867     uint8_t * packet = setup_long_characteristic_value_packet(GATT_EVENT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT, gatt_client->con_handle, attribute_handle, value_offset, blob, blob_length);
868     if (!packet) return;
869     emit_event_new(gatt_client->callback, packet, blob_length + long_characteristic_value_event_header_size);
870 }
871 
872 static void report_gatt_characteristic_descriptor(gatt_client_t * gatt_client, uint16_t descriptor_handle, uint8_t *value, uint16_t value_length, uint16_t value_offset){
873     UNUSED(value_offset);
874     uint8_t * packet = setup_characteristic_value_packet(GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT, gatt_client->con_handle, descriptor_handle, value, value_length);
875     emit_event_new(gatt_client->callback, packet, value_length + 8u);
876 }
877 
878 static void report_gatt_long_characteristic_descriptor(gatt_client_t * gatt_client, uint16_t descriptor_handle, uint8_t *blob, uint16_t blob_length, uint16_t value_offset){
879     uint8_t * packet = setup_long_characteristic_value_packet(GATT_EVENT_LONG_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT, gatt_client->con_handle, descriptor_handle, value_offset, blob, blob_length);
880     if (!packet) return;
881     emit_event_new(gatt_client->callback, packet, blob_length + long_characteristic_value_event_header_size);
882 }
883 
884 static void report_gatt_all_characteristic_descriptors(gatt_client_t * gatt_client, uint8_t * packet, uint16_t size, uint16_t pair_size){
885     int i;
886     for (i = 0u; (i + pair_size) <= size; i += pair_size){
887         uint16_t descriptor_handle = little_endian_read_16(packet,i);
888         uint8_t uuid128[16];
889         uint16_t uuid16 = 0;
890         if (pair_size == 4u){
891             uuid16 = little_endian_read_16(packet,i+2);
892             uuid_add_bluetooth_prefix(uuid128, uuid16);
893         } else {
894             reverse_128(&packet[i+2], uuid128);
895         }
896         emit_gatt_all_characteristic_descriptors_result_event(gatt_client, descriptor_handle, uuid128);
897     }
898 
899 }
900 
901 static int is_query_done(gatt_client_t * gatt_client, uint16_t last_result_handle){
902     return last_result_handle >= gatt_client->end_group_handle;
903 }
904 
905 static void trigger_next_query(gatt_client_t * gatt_client, uint16_t last_result_handle, gatt_client_state_t next_query_state){
906     if (is_query_done(gatt_client, last_result_handle)){
907         gatt_client_handle_transaction_complete(gatt_client);
908         emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
909         return;
910     }
911     // next
912     gatt_client->start_group_handle = last_result_handle + 1u;
913     gatt_client->gatt_client_state = next_query_state;
914 }
915 
916 static void trigger_next_included_service_query(gatt_client_t * gatt_client, uint16_t last_result_handle){
917     trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_INCLUDED_SERVICE_QUERY);
918 }
919 
920 static void trigger_next_service_query(gatt_client_t * gatt_client, uint16_t last_result_handle){
921     trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_SERVICE_QUERY);
922 }
923 
924 static void trigger_next_service_by_uuid_query(gatt_client_t * gatt_client, uint16_t last_result_handle){
925     trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_SERVICE_WITH_UUID_QUERY);
926 }
927 
928 static void trigger_next_characteristic_query(gatt_client_t * gatt_client, uint16_t last_result_handle){
929     if (is_query_done(gatt_client, last_result_handle)){
930         // report last characteristic
931         characteristic_end_found(gatt_client, gatt_client->end_group_handle);
932     }
933     trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY);
934 }
935 
936 static void trigger_next_characteristic_descriptor_query(gatt_client_t * gatt_client, uint16_t last_result_handle){
937     trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY);
938 }
939 
940 static void trigger_next_read_by_type_query(gatt_client_t * gatt_client, uint16_t last_result_handle){
941     trigger_next_query(gatt_client, last_result_handle, P_W2_SEND_READ_BY_TYPE_REQUEST);
942 }
943 
944 static void trigger_next_prepare_write_query(gatt_client_t * gatt_client, gatt_client_state_t next_query_state, gatt_client_state_t done_state){
945     gatt_client->attribute_offset += write_blob_length(gatt_client);
946     uint16_t next_blob_length =  write_blob_length(gatt_client);
947 
948     if (next_blob_length == 0u){
949         gatt_client->gatt_client_state = done_state;
950         return;
951     }
952     gatt_client->gatt_client_state = next_query_state;
953 }
954 
955 static void trigger_next_blob_query(gatt_client_t * gatt_client, gatt_client_state_t next_query_state, uint16_t received_blob_length){
956 
957     uint16_t max_blob_length = gatt_client->mtu - 1u;
958     if (received_blob_length < max_blob_length){
959         gatt_client_handle_transaction_complete(gatt_client);
960         emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
961         return;
962     }
963 
964     gatt_client->attribute_offset += received_blob_length;
965     gatt_client->gatt_client_state = next_query_state;
966 }
967 
968 
969 static int is_value_valid(gatt_client_t *gatt_client, uint8_t *packet, uint16_t size){
970     uint16_t attribute_handle = little_endian_read_16(packet, 1);
971     uint16_t value_offset = little_endian_read_16(packet, 3);
972 
973     if (gatt_client->attribute_handle != attribute_handle) return 0;
974     if (gatt_client->attribute_offset != value_offset) return 0;
975     return memcmp(&gatt_client->attribute_value[gatt_client->attribute_offset], &packet[5], size - 5u) == 0u;
976 }
977 
978 // returns 1 if packet was sent
979 static bool gatt_client_run_for_gatt_client(gatt_client_t * gatt_client){
980 
981     // wait until re-encryption is complete
982     if (gap_reconnect_security_setup_active(gatt_client->con_handle)) return false;
983 
984     // wait until re-encryption is complete
985     if (gatt_client->reencryption_active) return false;
986 
987     // wait until pairing complete (either reactive authentication or due to required security level)
988     if (gatt_client->wait_for_authentication_complete) return false;
989 
990     bool client_request_pending = gatt_client->gatt_client_state != P_READY;
991 
992     // verify security level for Mandatory Authentication over LE
993     bool check_security = true;
994 #ifdef ENABLE_GATT_OVER_CLASSIC
995     if (gatt_client->l2cap_psm != 0){
996         check_security = false;
997     }
998 #endif
999     if (client_request_pending && (gatt_client_required_security_level > gatt_client->security_level) && check_security){
1000         log_info("Trigger pairing, current security level %u, required %u\n", gatt_client->security_level, gatt_client_required_security_level);
1001         gatt_client->wait_for_authentication_complete = 1;
1002         // set att error code for pairing failure based on required level
1003         switch (gatt_client_required_security_level){
1004             case LEVEL_4:
1005             case LEVEL_3:
1006                 gatt_client->pending_error_code = ATT_ERROR_INSUFFICIENT_AUTHENTICATION;
1007                 break;
1008             default:
1009                 gatt_client->pending_error_code = ATT_ERROR_INSUFFICIENT_ENCRYPTION;
1010                 break;
1011         }
1012         sm_request_pairing(gatt_client->con_handle);
1013         // sm probably just sent a pdu
1014         return true;
1015     }
1016 
1017     switch (gatt_client->mtu_state) {
1018         case SEND_MTU_EXCHANGE:
1019             gatt_client->mtu_state = SENT_MTU_EXCHANGE;
1020             att_exchange_mtu_request(gatt_client);
1021             return true;
1022         case SENT_MTU_EXCHANGE:
1023             return false;
1024         default:
1025             break;
1026     }
1027 
1028     if (gatt_client->send_confirmation){
1029         gatt_client->send_confirmation = 0;
1030         att_confirmation(gatt_client);
1031         return true;
1032     }
1033 
1034     // check MTU for writes
1035     switch (gatt_client->gatt_client_state){
1036         case P_W2_SEND_WRITE_CHARACTERISTIC_VALUE:
1037         case P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR:
1038             if (gatt_client->attribute_length <= (gatt_client->mtu - 3u)) break;
1039             log_error("gatt_client_run: value len %u > MTU %u - 3\n", gatt_client->attribute_length,gatt_client->mtu);
1040             gatt_client_handle_transaction_complete(gatt_client);
1041             emit_gatt_complete_event(gatt_client, ATT_ERROR_INVALID_ATTRIBUTE_VALUE_LENGTH);
1042             return false;
1043         default:
1044             break;
1045     }
1046 
1047     switch (gatt_client->gatt_client_state){
1048         case P_W2_SEND_SERVICE_QUERY:
1049             gatt_client->gatt_client_state = P_W4_SERVICE_QUERY_RESULT;
1050             send_gatt_services_request(gatt_client);
1051             return true;
1052 
1053         case P_W2_SEND_SERVICE_WITH_UUID_QUERY:
1054             gatt_client->gatt_client_state = P_W4_SERVICE_WITH_UUID_RESULT;
1055             send_gatt_services_by_uuid_request(gatt_client);
1056             return true;
1057 
1058         case P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY:
1059             gatt_client->gatt_client_state = P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT;
1060             send_gatt_characteristic_request(gatt_client);
1061             return true;
1062 
1063         case P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY:
1064             gatt_client->gatt_client_state = P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT;
1065             send_gatt_characteristic_request(gatt_client);
1066             return true;
1067 
1068         case P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY:
1069             gatt_client->gatt_client_state = P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT;
1070             send_gatt_characteristic_descriptor_request(gatt_client);
1071             return true;
1072 
1073         case P_W2_SEND_INCLUDED_SERVICE_QUERY:
1074             gatt_client->gatt_client_state = P_W4_INCLUDED_SERVICE_QUERY_RESULT;
1075             send_gatt_included_service_request(gatt_client);
1076             return true;
1077 
1078         case P_W2_SEND_INCLUDED_SERVICE_WITH_UUID_QUERY:
1079             gatt_client->gatt_client_state = P_W4_INCLUDED_SERVICE_UUID_WITH_QUERY_RESULT;
1080             send_gatt_included_service_uuid_request(gatt_client);
1081             return true;
1082 
1083         case P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY:
1084             gatt_client->gatt_client_state = P_W4_READ_CHARACTERISTIC_VALUE_RESULT;
1085             send_gatt_read_characteristic_value_request(gatt_client);
1086             return true;
1087 
1088         case P_W2_SEND_READ_BLOB_QUERY:
1089             gatt_client->gatt_client_state = P_W4_READ_BLOB_RESULT;
1090             send_gatt_read_blob_request(gatt_client);
1091             return true;
1092 
1093         case P_W2_SEND_READ_BY_TYPE_REQUEST:
1094             gatt_client->gatt_client_state = P_W4_READ_BY_TYPE_RESPONSE;
1095             send_gatt_read_by_type_request(gatt_client);
1096             return true;
1097 
1098         case P_W2_SEND_READ_MULTIPLE_REQUEST:
1099             gatt_client->gatt_client_state = P_W4_READ_MULTIPLE_RESPONSE;
1100             send_gatt_read_multiple_request(gatt_client);
1101             return true;
1102 
1103         case P_W2_SEND_WRITE_CHARACTERISTIC_VALUE:
1104             gatt_client->gatt_client_state = P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT;
1105             send_gatt_write_attribute_value_request(gatt_client);
1106             return true;
1107 
1108         case P_W2_PREPARE_WRITE:
1109             gatt_client->gatt_client_state = P_W4_PREPARE_WRITE_RESULT;
1110             send_gatt_prepare_write_request(gatt_client);
1111             return true;
1112 
1113         case P_W2_PREPARE_WRITE_SINGLE:
1114             gatt_client->gatt_client_state = P_W4_PREPARE_WRITE_SINGLE_RESULT;
1115             send_gatt_prepare_write_request(gatt_client);
1116             return true;
1117 
1118         case P_W2_PREPARE_RELIABLE_WRITE:
1119             gatt_client->gatt_client_state = P_W4_PREPARE_RELIABLE_WRITE_RESULT;
1120             send_gatt_prepare_write_request(gatt_client);
1121             return true;
1122 
1123         case P_W2_EXECUTE_PREPARED_WRITE:
1124             gatt_client->gatt_client_state = P_W4_EXECUTE_PREPARED_WRITE_RESULT;
1125             send_gatt_execute_write_request(gatt_client);
1126             return true;
1127 
1128         case P_W2_CANCEL_PREPARED_WRITE:
1129             gatt_client->gatt_client_state = P_W4_CANCEL_PREPARED_WRITE_RESULT;
1130             send_gatt_cancel_prepared_write_request(gatt_client);
1131             return true;
1132 
1133         case P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH:
1134             gatt_client->gatt_client_state = P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT;
1135             send_gatt_cancel_prepared_write_request(gatt_client);
1136             return true;
1137 
1138 #ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY
1139         case P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY:
1140             // use Find Information
1141             gatt_client->gatt_client_state = P_W4_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT;
1142             send_gatt_characteristic_descriptor_request(gatt_client);
1143 #else
1144         case P_W2_SEND_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY:
1145             // Use Read By Type
1146             gatt_client->gatt_client_state = P_W4_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT;
1147             send_gatt_read_client_characteristic_configuration_request(gatt_client);
1148 #endif
1149             return true;
1150 
1151         case P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY:
1152             gatt_client->gatt_client_state = P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT;
1153             send_gatt_read_characteristic_descriptor_request(gatt_client);
1154             return true;
1155 
1156         case P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY:
1157             gatt_client->gatt_client_state = P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT;
1158             send_gatt_read_blob_request(gatt_client);
1159             return true;
1160 
1161         case P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR:
1162             gatt_client->gatt_client_state = P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT;
1163             send_gatt_write_attribute_value_request(gatt_client);
1164             return true;
1165 
1166         case P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION:
1167             gatt_client->gatt_client_state = P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT;
1168             send_gatt_write_client_characteristic_configuration_request(gatt_client);
1169             return true;
1170 
1171         case P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR:
1172             gatt_client->gatt_client_state = P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT;
1173             send_gatt_prepare_write_request(gatt_client);
1174             return true;
1175 
1176         case P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR:
1177             gatt_client->gatt_client_state = P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT;
1178             send_gatt_execute_write_request(gatt_client);
1179             return true;
1180 
1181 #ifdef ENABLE_LE_SIGNED_WRITE
1182         case P_W4_IDENTITY_RESOLVING:
1183             log_info("P_W4_IDENTITY_RESOLVING - state %x", sm_identity_resolving_state(gatt_client->con_handle));
1184             switch (sm_identity_resolving_state(gatt_client->con_handle)){
1185                 case IRK_LOOKUP_SUCCEEDED:
1186                     gatt_client->le_device_index = sm_le_device_index(gatt_client->con_handle);
1187                     gatt_client->gatt_client_state = P_W4_CMAC_READY;
1188                     break;
1189                 case IRK_LOOKUP_FAILED:
1190                     gatt_client_handle_transaction_complete(gatt_client);
1191                     emit_gatt_complete_event(gatt_client, ATT_ERROR_BONDING_INFORMATION_MISSING);
1192                     return false;
1193                 default:
1194                     return false;
1195             }
1196 
1197             /* Fall through */
1198 
1199         case P_W4_CMAC_READY:
1200             if (sm_cmac_ready()){
1201                 sm_key_t csrk;
1202                 le_device_db_local_csrk_get(gatt_client->le_device_index, csrk);
1203                 uint32_t sign_counter = le_device_db_local_counter_get(gatt_client->le_device_index);
1204                 gatt_client->gatt_client_state = P_W4_CMAC_RESULT;
1205                 sm_cmac_signed_write_start(csrk, ATT_SIGNED_WRITE_COMMAND, gatt_client->attribute_handle, gatt_client->attribute_length, gatt_client->attribute_value, sign_counter, att_signed_write_handle_cmac_result);
1206             }
1207             return false;
1208 
1209         case P_W2_SEND_SIGNED_WRITE: {
1210             gatt_client->gatt_client_state = P_W4_SEND_SINGED_WRITE_DONE;
1211             // bump local signing counter
1212             uint32_t sign_counter = le_device_db_local_counter_get(gatt_client->le_device_index);
1213             le_device_db_local_counter_set(gatt_client->le_device_index, sign_counter + 1);
1214             // send signed write command
1215             send_gatt_signed_write_request(gatt_client, sign_counter);
1216             // finally, notifiy client that write is complete
1217             gatt_client_handle_transaction_complete(gatt_client);
1218             emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1219             return true;
1220         }
1221 #endif
1222         default:
1223             break;
1224     }
1225 
1226     // write without response callback
1227     btstack_context_callback_registration_t * callback =
1228             (btstack_context_callback_registration_t *) btstack_linked_list_pop(&gatt_client->write_without_response_requests);
1229     if (callback != NULL){
1230         (*callback->callback)(callback->context);
1231         return true;
1232     }
1233 
1234     // requested can send now old
1235     if (gatt_client->write_without_response_callback){
1236         btstack_packet_handler_t packet_handler = gatt_client->write_without_response_callback;
1237         gatt_client->write_without_response_callback = NULL;
1238         uint8_t event[4];
1239         event[0] = GATT_EVENT_CAN_WRITE_WITHOUT_RESPONSE;
1240         event[1] = sizeof(event) - 2u;
1241         little_endian_store_16(event, 2, gatt_client->con_handle);
1242         packet_handler(HCI_EVENT_PACKET, gatt_client->con_handle, event, sizeof(event));
1243         return true; // to trigger requeueing (even if higher layer didn't sent)
1244     }
1245 
1246     return false;
1247 }
1248 
1249 static void gatt_client_run(void){
1250     btstack_linked_item_t *it;
1251     for (it = (btstack_linked_item_t *) gatt_client_connections; it != NULL; it = it->next){
1252         gatt_client_t * gatt_client = (gatt_client_t *) it;
1253 #ifdef ENABLE_GATT_OVER_CLASSIC
1254         if (gatt_client->con_handle == HCI_CON_HANDLE_INVALID) {
1255             continue;
1256         }
1257 
1258         // handle GATT over BR/EDR
1259         if (gatt_client->l2cap_psm != 0){
1260             if (l2cap_can_send_packet_now(gatt_client->l2cap_cid) == false){
1261                 l2cap_request_can_send_now_event(gatt_client->l2cap_cid);
1262                 return;
1263             }
1264             bool packet_sent = gatt_client_run_for_gatt_client(gatt_client);
1265             if (packet_sent){
1266                 // request new permission
1267                 att_dispatch_client_request_can_send_now_event(gatt_client->con_handle);
1268                 // requeue client for fairness and exit
1269                 // note: iterator has become invalid
1270                 btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) gatt_client);
1271                 btstack_linked_list_add_tail(&gatt_client_connections, (btstack_linked_item_t *) gatt_client);
1272                 return;
1273             }
1274         }
1275         continue;
1276 #endif
1277         // handle GATT over LE
1278         if (!att_dispatch_client_can_send_now(gatt_client->con_handle)) {
1279             att_dispatch_client_request_can_send_now_event(gatt_client->con_handle);
1280             return;
1281         }
1282         bool packet_sent = gatt_client_run_for_gatt_client(gatt_client);
1283         if (packet_sent){
1284             // request new permission
1285             att_dispatch_client_request_can_send_now_event(gatt_client->con_handle);
1286             // requeue client for fairness and exit
1287             // note: iterator has become invalid
1288             btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) gatt_client);
1289             btstack_linked_list_add_tail(&gatt_client_connections, (btstack_linked_item_t *) gatt_client);
1290             return;
1291         }
1292     }
1293 }
1294 
1295 static void gatt_client_report_error_if_pending(gatt_client_t *gatt_client, uint8_t att_error_code) {
1296     if (is_ready(gatt_client) == 1) return;
1297     gatt_client_handle_transaction_complete(gatt_client);
1298     emit_gatt_complete_event(gatt_client, att_error_code);
1299 }
1300 
1301 static void gatt_client_handle_reencryption_complete(const uint8_t * packet){
1302     hci_con_handle_t con_handle = sm_event_reencryption_complete_get_handle(packet);
1303     gatt_client_t * gatt_client = gatt_client_get_context_for_handle(con_handle);
1304     if (gatt_client == NULL) return;
1305 
1306     // update security level
1307     gatt_client->security_level = gatt_client_le_security_level_for_connection(con_handle);
1308 
1309     gatt_client->reencryption_result = sm_event_reencryption_complete_get_status(packet);
1310     gatt_client->reencryption_active = false;
1311     gatt_client->wait_for_authentication_complete = 0;
1312 
1313     if (gatt_client->gatt_client_state == P_READY) return;
1314 
1315     switch (sm_event_reencryption_complete_get_status(packet)){
1316         case ERROR_CODE_SUCCESS:
1317             log_info("re-encryption success, retry operation");
1318             break;
1319         case ERROR_CODE_AUTHENTICATION_FAILURE:
1320         case ERROR_CODE_PIN_OR_KEY_MISSING:
1321 #if defined(ENABLE_GATT_CLIENT_PAIRING) && !defined(ENABLE_LE_PROACTIVE_AUTHENTICATION)
1322             if (gatt_client_required_security_level == LEVEL_0) {
1323                 // re-encryption failed for reactive authentication with pairing and we have a pending client request
1324                 // => try to resolve it by deleting bonding information if we started pairing before
1325                 // delete bonding information
1326                 int le_device_db_index = sm_le_device_index(gatt_client->con_handle);
1327                 btstack_assert(le_device_db_index >= 0);
1328                 log_info("reactive auth with pairing: delete bonding and start pairing");
1329 #ifdef ENABLE_LE_PRIVACY_ADDRESS_RESOLUTION
1330                 hci_remove_le_device_db_entry_from_resolving_list((uint16_t) le_device_db_index);
1331 #endif
1332                 le_device_db_remove(le_device_db_index);
1333                 // trigger pairing again
1334                 sm_request_pairing(gatt_client->con_handle);
1335                 break;
1336             }
1337 #endif
1338             // report bonding information missing
1339             gatt_client_handle_transaction_complete(gatt_client);
1340             emit_gatt_complete_event(gatt_client, ATT_ERROR_BONDING_INFORMATION_MISSING);
1341             break;
1342         default:
1343             // report bonding information missing
1344             gatt_client_handle_transaction_complete(gatt_client);
1345             emit_gatt_complete_event(gatt_client, gatt_client->pending_error_code);
1346             break;
1347     }
1348 }
1349 
1350 static void gatt_client_handle_disconnection_complete(const uint8_t * packet){
1351     log_info("GATT Client: HCI_EVENT_DISCONNECTION_COMPLETE");
1352     hci_con_handle_t con_handle = little_endian_read_16(packet,3);
1353     gatt_client_t * gatt_client = gatt_client_get_context_for_handle(con_handle);
1354     if (gatt_client == NULL) return;
1355 
1356     gatt_client_report_error_if_pending(gatt_client, ATT_ERROR_HCI_DISCONNECT_RECEIVED);
1357     gatt_client_timeout_stop(gatt_client);
1358     btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) gatt_client);
1359     btstack_memory_gatt_client_free(gatt_client);
1360 }
1361 
1362 static void gatt_client_event_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
1363     UNUSED(channel);    // ok: handling own l2cap events
1364     UNUSED(size);       // ok: there is no channel
1365 
1366     if (packet_type != HCI_EVENT_PACKET) return;
1367 
1368     hci_con_handle_t con_handle;
1369     gatt_client_t * gatt_client;
1370     switch (hci_event_packet_get_type(packet)) {
1371         case HCI_EVENT_DISCONNECTION_COMPLETE:
1372             gatt_client_handle_disconnection_complete(packet);
1373             break;
1374 
1375         // Pairing complete (with/without bonding=storing of pairing information)
1376         case SM_EVENT_PAIRING_COMPLETE:
1377             con_handle = sm_event_pairing_complete_get_handle(packet);
1378             gatt_client = gatt_client_get_context_for_handle(con_handle);
1379             if (gatt_client == NULL) break;
1380 
1381             // update security level
1382             gatt_client->security_level = gatt_client_le_security_level_for_connection(con_handle);
1383 
1384             if (gatt_client->wait_for_authentication_complete){
1385                 gatt_client->wait_for_authentication_complete = 0;
1386                 if (sm_event_pairing_complete_get_status(packet)){
1387                     log_info("pairing failed, report previous error 0x%x", gatt_client->pending_error_code);
1388                     gatt_client_report_error_if_pending(gatt_client, gatt_client->pending_error_code);
1389                 } else {
1390                     log_info("pairing success, retry operation");
1391                 }
1392             }
1393             break;
1394 
1395 #ifdef ENABLE_LE_SIGNED_WRITE
1396         // Identity Resolving completed (no code, gatt_client_run will continue)
1397         case SM_EVENT_IDENTITY_RESOLVING_SUCCEEDED:
1398         case SM_EVENT_IDENTITY_RESOLVING_FAILED:
1399             break;
1400 #endif
1401 
1402         // re-encryption started
1403         case SM_EVENT_REENCRYPTION_STARTED:
1404             con_handle = sm_event_reencryption_complete_get_handle(packet);
1405             gatt_client = gatt_client_get_context_for_handle(con_handle);
1406             if (gatt_client == NULL) break;
1407 
1408             gatt_client->reencryption_active = true;
1409             gatt_client->reencryption_result = ERROR_CODE_SUCCESS;
1410             break;
1411 
1412         // re-encryption complete
1413         case SM_EVENT_REENCRYPTION_COMPLETE:
1414             gatt_client_handle_reencryption_complete(packet);
1415             break;
1416         default:
1417             break;
1418     }
1419 
1420     gatt_client_run();
1421 }
1422 
1423 static void gatt_client_handle_att_response(gatt_client_t * gatt_client, uint8_t * packet, uint16_t size) {
1424     uint8_t error_code;
1425     switch (packet[0]) {
1426         case ATT_EXCHANGE_MTU_RESPONSE: {
1427             if (size < 3u) break;
1428             bool update_att_mtu = true;
1429             uint16_t remote_rx_mtu = little_endian_read_16(packet, 1);
1430             uint16_t local_rx_mtu = l2cap_max_le_mtu();
1431 #ifdef ENABLE_GATT_OVER_CLASSIC
1432             if (gatt_client->l2cap_psm != 0){
1433                 local_rx_mtu = gatt_client->mtu;
1434             } else {
1435                 update_att_mtu = false;
1436             }
1437 #endif
1438             uint16_t mtu = (remote_rx_mtu < local_rx_mtu) ? remote_rx_mtu : local_rx_mtu;
1439 
1440             // set gatt client mtu
1441             gatt_client->mtu = mtu;
1442             gatt_client->mtu_state = MTU_EXCHANGED;
1443 
1444             if (update_att_mtu){
1445                 // set per connection mtu state - for fixed channel
1446                 hci_connection_t *hci_connection = hci_connection_for_handle(gatt_client->con_handle);
1447                 hci_connection->att_connection.mtu = gatt_client->mtu;
1448                 hci_connection->att_connection.mtu_exchanged = true;
1449             }
1450             emit_gatt_mtu_exchanged_result_event(gatt_client, gatt_client->mtu);
1451             break;
1452         }
1453         case ATT_READ_BY_GROUP_TYPE_RESPONSE:
1454             switch (gatt_client->gatt_client_state) {
1455                 case P_W4_SERVICE_QUERY_RESULT:
1456                     report_gatt_services(gatt_client, packet, size);
1457                     trigger_next_service_query(gatt_client, get_last_result_handle_from_service_list(packet, size));
1458                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1459                     break;
1460                 default:
1461                     break;
1462             }
1463             break;
1464         case ATT_HANDLE_VALUE_NOTIFICATION:
1465             if (size < 3u) return;
1466             report_gatt_notification(gatt_client, little_endian_read_16(packet, 1u), &packet[3], size - 3u);
1467             return;
1468         case ATT_HANDLE_VALUE_INDICATION:
1469             if (size < 3u) break;
1470             report_gatt_indication(gatt_client, little_endian_read_16(packet, 1u), &packet[3], size - 3u);
1471             gatt_client->send_confirmation = 1;
1472             break;
1473 
1474         case ATT_READ_BY_TYPE_RESPONSE:
1475             switch (gatt_client->gatt_client_state) {
1476                 case P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT:
1477                     report_gatt_characteristics(gatt_client, packet, size);
1478                     trigger_next_characteristic_query(gatt_client,
1479                                                       get_last_result_handle_from_characteristics_list(packet, size));
1480                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done, or by ATT_ERROR
1481                     break;
1482                 case P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT:
1483                     report_gatt_characteristics(gatt_client, packet, size);
1484                     trigger_next_characteristic_query(gatt_client,
1485                                                       get_last_result_handle_from_characteristics_list(packet, size));
1486                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done, or by ATT_ERROR
1487                     break;
1488                 case P_W4_INCLUDED_SERVICE_QUERY_RESULT: {
1489                     if (size < 2u) break;
1490                     uint16_t uuid16 = 0;
1491                     uint16_t pair_size = packet[1];
1492 
1493                     if (pair_size == 6u) {
1494                         if (size < 8u) break;
1495                         // UUIDs not available, query first included service
1496                         gatt_client->start_group_handle = little_endian_read_16(packet, 2); // ready for next query
1497                         gatt_client->query_start_handle = little_endian_read_16(packet, 4);
1498                         gatt_client->query_end_handle = little_endian_read_16(packet, 6);
1499                         gatt_client->gatt_client_state = P_W2_SEND_INCLUDED_SERVICE_WITH_UUID_QUERY;
1500                         break;
1501                     }
1502 
1503                     if (pair_size != 8u) break;
1504 
1505                     // UUIDs included, report all of them
1506                     uint16_t offset;
1507                     for (offset = 2u; (offset + 8u) <= size; offset += pair_size) {
1508                         uint16_t include_handle = little_endian_read_16(packet, offset);
1509                         gatt_client->query_start_handle = little_endian_read_16(packet, offset + 2u);
1510                         gatt_client->query_end_handle = little_endian_read_16(packet, offset + 4u);
1511                         uuid16 = little_endian_read_16(packet, offset + 6u);
1512                         report_gatt_included_service_uuid16(gatt_client, include_handle, uuid16);
1513                     }
1514 
1515                     trigger_next_included_service_query(gatt_client,
1516                                                         get_last_result_handle_from_included_services_list(packet,
1517                                                                                                            size));
1518                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1519                     break;
1520                 }
1521 #ifndef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY
1522                 case P_W4_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT:
1523                     gatt_client->client_characteristic_configuration_handle = little_endian_read_16(packet, 2);
1524                     gatt_client->gatt_client_state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION;
1525                     break;
1526 #endif
1527                 case P_W4_READ_BY_TYPE_RESPONSE: {
1528                     uint16_t pair_size = packet[1];
1529                     // set last result handle to last valid handle, only used if pair_size invalid
1530                     uint16_t last_result_handle = 0xffff;
1531                     if (pair_size > 2) {
1532                         uint16_t offset;
1533                         for (offset = 2; offset < size; offset += pair_size) {
1534                             uint16_t value_handle = little_endian_read_16(packet, offset);
1535                             report_gatt_characteristic_value(gatt_client, value_handle, &packet[offset + 2u],
1536                                                              pair_size - 2u);
1537                             last_result_handle = value_handle;
1538                         }
1539                     }
1540                     trigger_next_read_by_type_query(gatt_client, last_result_handle);
1541                     break;
1542                 }
1543                 default:
1544                     break;
1545             }
1546             break;
1547         case ATT_READ_RESPONSE:
1548             switch (gatt_client->gatt_client_state) {
1549                 case P_W4_INCLUDED_SERVICE_UUID_WITH_QUERY_RESULT:
1550                     if (size >= 17) {
1551                         uint8_t uuid128[16];
1552                         reverse_128(&packet[1], uuid128);
1553                         report_gatt_included_service_uuid128(gatt_client, gatt_client->start_group_handle, uuid128);
1554                     }
1555                     trigger_next_included_service_query(gatt_client, gatt_client->start_group_handle);
1556                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1557                     break;
1558 
1559                 case P_W4_READ_CHARACTERISTIC_VALUE_RESULT:
1560                     gatt_client_handle_transaction_complete(gatt_client);
1561                     report_gatt_characteristic_value(gatt_client, gatt_client->attribute_handle, &packet[1], size - 1u);
1562                     emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1563                     break;
1564 
1565                 case P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT:
1566                     gatt_client_handle_transaction_complete(gatt_client);
1567                     report_gatt_characteristic_descriptor(gatt_client, gatt_client->attribute_handle, &packet[1],
1568                                                           size - 1u, 0u);
1569                     emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1570                     break;
1571 
1572                     // Use ATT_READ_REQUEST for first blob of Read Long Characteristic
1573                 case P_W4_READ_BLOB_RESULT:
1574                     report_gatt_long_characteristic_value_blob(gatt_client, gatt_client->attribute_handle, &packet[1],
1575                                                                size - 1u, gatt_client->attribute_offset);
1576                     trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_QUERY, size - 1u);
1577                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1578                     break;
1579 
1580                     // Use ATT_READ_REQUEST for first blob of Read Long Characteristic Descriptor
1581                 case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT:
1582                     report_gatt_long_characteristic_descriptor(gatt_client, gatt_client->attribute_handle, &packet[1],
1583                                                                size - 1u, gatt_client->attribute_offset);
1584                     trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY,
1585                                             size - 1u);
1586                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1587                     break;
1588 
1589                 default:
1590                     break;
1591             }
1592             break;
1593 
1594         case ATT_FIND_BY_TYPE_VALUE_RESPONSE: {
1595             uint8_t pair_size = 4;
1596             int i;
1597             uint16_t start_group_handle;
1598             uint16_t end_group_handle = 0xffff; // asserts GATT_EVENT_QUERY_COMPLETE is emitted if no results
1599             for (i = 1u; (i + pair_size) <= size; i += pair_size) {
1600                 start_group_handle = little_endian_read_16(packet, i);
1601                 end_group_handle = little_endian_read_16(packet, i + 2);
1602                 emit_gatt_service_query_result_event(gatt_client, start_group_handle, end_group_handle,
1603                                                      gatt_client->uuid128);
1604             }
1605             trigger_next_service_by_uuid_query(gatt_client, end_group_handle);
1606             // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1607             break;
1608         }
1609         case ATT_FIND_INFORMATION_REPLY: {
1610             if (size < 2u) break;
1611 
1612             uint8_t pair_size = 4;
1613             if (packet[1u] == 2u) {
1614                 pair_size = 18;
1615             }
1616             uint16_t offset = 2;
1617 
1618             if (size < (pair_size + offset)) break;
1619             uint16_t last_descriptor_handle = little_endian_read_16(packet, size - pair_size);
1620 
1621 #ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY
1622             log_info("ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY, state %x", gatt_client->gatt_client_state);
1623             if (gatt_client->gatt_client_state == P_W4_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT){
1624                 // iterate over descriptors looking for CCC
1625                 if (pair_size == 4){
1626                     while ((offset + 4) <= size){
1627                         uint16_t uuid16 = little_endian_read_16(packet, offset + 2);
1628                         if (uuid16 == GATT_CLIENT_CHARACTERISTICS_CONFIGURATION){
1629                             gatt_client->client_characteristic_configuration_handle = little_endian_read_16(packet, offset);
1630                             gatt_client->gatt_client_state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION;
1631                             log_info("CCC found %x", gatt_client->client_characteristic_configuration_handle);
1632                             break;
1633                         }
1634                         offset += pair_size;
1635                     }
1636                 }
1637                 if (is_query_done(gatt_client, last_descriptor_handle)){
1638 
1639                 } else {
1640                     // next
1641                     gatt_client->start_group_handle = last_descriptor_handle + 1;
1642                     gatt_client->gatt_client_state = P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY;
1643                 }
1644                 break;
1645             }
1646 #endif
1647             report_gatt_all_characteristic_descriptors(gatt_client, &packet[2], size - 2u, pair_size);
1648             trigger_next_characteristic_descriptor_query(gatt_client, last_descriptor_handle);
1649             // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1650             break;
1651         }
1652 
1653         case ATT_WRITE_RESPONSE:
1654             switch (gatt_client->gatt_client_state) {
1655                 case P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT:
1656                     gatt_client_handle_transaction_complete(gatt_client);
1657                     emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1658                     break;
1659                 case P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT:
1660                     gatt_client_handle_transaction_complete(gatt_client);
1661                     emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1662                     break;
1663                 case P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
1664                     gatt_client_handle_transaction_complete(gatt_client);
1665                     emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1666                     break;
1667                 default:
1668                     break;
1669             }
1670             break;
1671 
1672         case ATT_READ_BLOB_RESPONSE: {
1673             uint16_t received_blob_length = size - 1u;
1674             switch (gatt_client->gatt_client_state) {
1675                 case P_W4_READ_BLOB_RESULT:
1676                     report_gatt_long_characteristic_value_blob(gatt_client, gatt_client->attribute_handle, &packet[1],
1677                                                                received_blob_length, gatt_client->attribute_offset);
1678                     trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_QUERY, received_blob_length);
1679                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1680                     break;
1681                 case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT:
1682                     report_gatt_long_characteristic_descriptor(gatt_client, gatt_client->attribute_handle,
1683                                                                &packet[1], received_blob_length,
1684                                                                gatt_client->attribute_offset);
1685                     trigger_next_blob_query(gatt_client, P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY,
1686                                             received_blob_length);
1687                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1688                     break;
1689                 default:
1690                     break;
1691             }
1692             break;
1693         }
1694         case ATT_PREPARE_WRITE_RESPONSE:
1695             switch (gatt_client->gatt_client_state) {
1696                 case P_W4_PREPARE_WRITE_SINGLE_RESULT:
1697                     gatt_client_handle_transaction_complete(gatt_client);
1698                     if (is_value_valid(gatt_client, packet, size)) {
1699                         emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1700                     } else {
1701                         emit_gatt_complete_event(gatt_client, ATT_ERROR_DATA_MISMATCH);
1702                     }
1703                     break;
1704 
1705                 case P_W4_PREPARE_WRITE_RESULT: {
1706                     gatt_client->attribute_offset = little_endian_read_16(packet, 3);
1707                     trigger_next_prepare_write_query(gatt_client, P_W2_PREPARE_WRITE, P_W2_EXECUTE_PREPARED_WRITE);
1708                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1709                     break;
1710                 }
1711                 case P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT: {
1712                     gatt_client->attribute_offset = little_endian_read_16(packet, 3);
1713                     trigger_next_prepare_write_query(gatt_client, P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR,
1714                                                      P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR);
1715                     // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1716                     break;
1717                 }
1718                 case P_W4_PREPARE_RELIABLE_WRITE_RESULT: {
1719                     if (is_value_valid(gatt_client, packet, size)) {
1720                         gatt_client->attribute_offset = little_endian_read_16(packet, 3);
1721                         trigger_next_prepare_write_query(gatt_client, P_W2_PREPARE_RELIABLE_WRITE,
1722                                                          P_W2_EXECUTE_PREPARED_WRITE);
1723                         // GATT_EVENT_QUERY_COMPLETE is emitted by trigger_next_xxx when done
1724                         break;
1725                     }
1726                     gatt_client->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH;
1727                     break;
1728                 }
1729                 default:
1730                     break;
1731             }
1732             break;
1733 
1734         case ATT_EXECUTE_WRITE_RESPONSE:
1735             switch (gatt_client->gatt_client_state) {
1736                 case P_W4_EXECUTE_PREPARED_WRITE_RESULT:
1737                     gatt_client_handle_transaction_complete(gatt_client);
1738                     emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1739                     break;
1740                 case P_W4_CANCEL_PREPARED_WRITE_RESULT:
1741                     gatt_client_handle_transaction_complete(gatt_client);
1742                     emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1743                     break;
1744                 case P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT:
1745                     gatt_client_handle_transaction_complete(gatt_client);
1746                     emit_gatt_complete_event(gatt_client, ATT_ERROR_DATA_MISMATCH);
1747                     break;
1748                 case P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
1749                     gatt_client_handle_transaction_complete(gatt_client);
1750                     emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1751                     break;
1752                 default:
1753                     break;
1754 
1755             }
1756             break;
1757 
1758         case ATT_READ_MULTIPLE_RESPONSE:
1759             switch (gatt_client->gatt_client_state) {
1760                 case P_W4_READ_MULTIPLE_RESPONSE:
1761                     report_gatt_characteristic_value(gatt_client, 0u, &packet[1], size - 1u);
1762                     gatt_client_handle_transaction_complete(gatt_client);
1763                     emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1764                     break;
1765                 default:
1766                     break;
1767             }
1768             break;
1769 
1770         case ATT_ERROR_RESPONSE:
1771             if (size < 5u) return;
1772             error_code = packet[4];
1773             switch (error_code) {
1774                 case ATT_ERROR_ATTRIBUTE_NOT_FOUND: {
1775                     switch (gatt_client->gatt_client_state) {
1776                         case P_W4_SERVICE_QUERY_RESULT:
1777                         case P_W4_SERVICE_WITH_UUID_RESULT:
1778                         case P_W4_INCLUDED_SERVICE_QUERY_RESULT:
1779                         case P_W4_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT:
1780                             gatt_client_handle_transaction_complete(gatt_client);
1781                             emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1782                             break;
1783                         case P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT:
1784                         case P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT:
1785                             characteristic_end_found(gatt_client, gatt_client->end_group_handle);
1786                             gatt_client_handle_transaction_complete(gatt_client);
1787                             emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1788                             break;
1789                         case P_W4_READ_BY_TYPE_RESPONSE:
1790                             gatt_client_handle_transaction_complete(gatt_client);
1791                             if (gatt_client->start_group_handle == gatt_client->query_start_handle) {
1792                                 emit_gatt_complete_event(gatt_client, ATT_ERROR_ATTRIBUTE_NOT_FOUND);
1793                             } else {
1794                                 emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
1795                             }
1796                             break;
1797                         default:
1798                             gatt_client_report_error_if_pending(gatt_client, error_code);
1799                             break;
1800                     }
1801                     break;
1802                 }
1803 
1804 #ifdef ENABLE_GATT_CLIENT_PAIRING
1805 
1806                     case ATT_ERROR_INSUFFICIENT_AUTHENTICATION:
1807                     case ATT_ERROR_INSUFFICIENT_ENCRYPTION_KEY_SIZE:
1808                     case ATT_ERROR_INSUFFICIENT_ENCRYPTION: {
1809 
1810                         // security too low
1811                         if (gatt_client->security_counter > 0) {
1812                             gatt_client_report_error_if_pending(gatt_client, error_code);
1813                             break;
1814                         }
1815                         // start security
1816                         gatt_client->security_counter++;
1817 
1818                         // setup action
1819                         int retry = 1;
1820                         switch (gatt_client->gatt_client_state){
1821                             case P_W4_READ_CHARACTERISTIC_VALUE_RESULT:
1822                                 gatt_client->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY ;
1823                                 break;
1824                             case P_W4_READ_BLOB_RESULT:
1825                                 gatt_client->gatt_client_state = P_W2_SEND_READ_BLOB_QUERY;
1826                                 break;
1827                             case P_W4_READ_BY_TYPE_RESPONSE:
1828                                 gatt_client->gatt_client_state = P_W2_SEND_READ_BY_TYPE_REQUEST;
1829                                 break;
1830                             case P_W4_READ_MULTIPLE_RESPONSE:
1831                                 gatt_client->gatt_client_state = P_W2_SEND_READ_MULTIPLE_REQUEST;
1832                                 break;
1833                             case P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT:
1834                                 gatt_client->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_VALUE;
1835                                 break;
1836                             case P_W4_PREPARE_WRITE_RESULT:
1837                                 gatt_client->gatt_client_state = P_W2_PREPARE_WRITE;
1838                                 break;
1839                             case P_W4_PREPARE_WRITE_SINGLE_RESULT:
1840                                 gatt_client->gatt_client_state = P_W2_PREPARE_WRITE_SINGLE;
1841                                 break;
1842                             case P_W4_PREPARE_RELIABLE_WRITE_RESULT:
1843                                 gatt_client->gatt_client_state = P_W2_PREPARE_RELIABLE_WRITE;
1844                                 break;
1845                             case P_W4_EXECUTE_PREPARED_WRITE_RESULT:
1846                                 gatt_client->gatt_client_state = P_W2_EXECUTE_PREPARED_WRITE;
1847                                 break;
1848                             case P_W4_CANCEL_PREPARED_WRITE_RESULT:
1849                                 gatt_client->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE;
1850                                 break;
1851                             case P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT:
1852                                 gatt_client->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH;
1853                                 break;
1854                             case P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT:
1855                                 gatt_client->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY;
1856                                 break;
1857                             case P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT:
1858                                 gatt_client->gatt_client_state = P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY;
1859                                 break;
1860                             case P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
1861                                 gatt_client->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR;
1862                                 break;
1863                             case P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT:
1864                                 gatt_client->gatt_client_state = P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION;
1865                                 break;
1866                             case P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
1867                                 gatt_client->gatt_client_state = P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR;
1868                                 break;
1869                             case P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT:
1870                                 gatt_client->gatt_client_state = P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR;
1871                                 break;
1872 #ifdef ENABLE_LE_SIGNED_WRITE
1873                             case P_W4_SEND_SINGED_WRITE_DONE:
1874                                 gatt_client->gatt_client_state = P_W2_SEND_SIGNED_WRITE;
1875                                 break;
1876 #endif
1877                             default:
1878                                 log_info("retry not supported for state %x", gatt_client->gatt_client_state);
1879                                 retry = 0;
1880                                 break;
1881                         }
1882 
1883                         if (!retry) {
1884                             gatt_client_report_error_if_pending(gatt_client, error_code);
1885                             break;
1886                         }
1887 
1888                         log_info("security error, start pairing");
1889 
1890                         // start pairing for higher security level
1891                         gatt_client->wait_for_authentication_complete = 1;
1892                         gatt_client->pending_error_code = error_code;
1893                         sm_request_pairing(gatt_client->con_handle);
1894                         break;
1895                     }
1896 #endif
1897 
1898                     // nothing we can do about that
1899                 case ATT_ERROR_INSUFFICIENT_AUTHORIZATION:
1900                 default:
1901                     gatt_client_report_error_if_pending(gatt_client, error_code);
1902                     break;
1903             }
1904             break;
1905 
1906         default:
1907             log_info("ATT Handler, unhandled response type 0x%02x", packet[0]);
1908             break;
1909     }
1910 }
1911 
1912 static void gatt_client_att_packet_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size) {
1913     gatt_client_t *gatt_client;
1914     if (size < 1u) return;
1915 
1916     if (packet_type == HCI_EVENT_PACKET) {
1917         switch (packet[0]) {
1918             case L2CAP_EVENT_CAN_SEND_NOW:
1919                 gatt_client_run();
1920                 break;
1921                 // att_server has negotiated the mtu for this connection, cache if context exists
1922             case ATT_EVENT_MTU_EXCHANGE_COMPLETE:
1923                 if (size < 6u) break;
1924                 gatt_client = gatt_client_get_context_for_handle(handle);
1925                 if (gatt_client == NULL) break;
1926                 gatt_client->mtu = little_endian_read_16(packet, 4);
1927                 break;
1928             default:
1929                 break;
1930         }
1931         return;
1932     }
1933 
1934     if (packet_type != ATT_DATA_PACKET) return;
1935 
1936     // special cases: notifications & indications motivate creating context
1937     switch (packet[0]) {
1938         case ATT_HANDLE_VALUE_NOTIFICATION:
1939         case ATT_HANDLE_VALUE_INDICATION:
1940             gatt_client_provide_context_for_handle(handle, &gatt_client);
1941             break;
1942         default:
1943             gatt_client = gatt_client_get_context_for_handle(handle);
1944             break;
1945     }
1946 
1947     if (gatt_client != NULL) {
1948         gatt_client_handle_att_response(gatt_client, packet, size);
1949         gatt_client_run();
1950     }
1951 }
1952 
1953 #ifdef ENABLE_LE_SIGNED_WRITE
1954 static void att_signed_write_handle_cmac_result(uint8_t hash[8]){
1955     btstack_linked_list_iterator_t it;
1956     btstack_linked_list_iterator_init(&it, &gatt_client_connections);
1957     while (btstack_linked_list_iterator_has_next(&it)){
1958         gatt_client_t * gatt_client = (gatt_client_t *) btstack_linked_list_iterator_next(&it);
1959         if (gatt_client->gatt_client_state == P_W4_CMAC_RESULT){
1960             // store result
1961             (void)memcpy(gatt_client->cmac, hash, 8);
1962             // reverse_64(hash, gatt_client->cmac);
1963             gatt_client->gatt_client_state = P_W2_SEND_SIGNED_WRITE;
1964             gatt_client_run();
1965             return;
1966         }
1967     }
1968 }
1969 
1970 uint8_t gatt_client_signed_write_without_response(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle, uint16_t message_len, uint8_t * message){
1971     gatt_client_t * gatt_client;
1972     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
1973     if (status != ERROR_CODE_SUCCESS){
1974         return status;
1975     }
1976     if (is_ready(gatt_client) == 0){
1977         return GATT_CLIENT_IN_WRONG_STATE;
1978     }
1979 
1980     gatt_client->callback = callback;
1981     gatt_client->attribute_handle = value_handle;
1982     gatt_client->attribute_length = message_len;
1983     gatt_client->attribute_value = message;
1984     gatt_client->gatt_client_state = P_W4_IDENTITY_RESOLVING;
1985     gatt_client_run();
1986     return ERROR_CODE_SUCCESS;
1987 }
1988 #endif
1989 
1990 uint8_t gatt_client_discover_primary_services(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
1991     gatt_client_t * gatt_client;
1992     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
1993     if (status != ERROR_CODE_SUCCESS){
1994         return status;
1995     }
1996     if (is_ready(gatt_client) == 0){
1997         return GATT_CLIENT_IN_WRONG_STATE;
1998     }
1999 
2000     gatt_client->callback = callback;
2001     gatt_client->start_group_handle = 0x0001;
2002     gatt_client->end_group_handle   = 0xffff;
2003     gatt_client->gatt_client_state = P_W2_SEND_SERVICE_QUERY;
2004     gatt_client->uuid16 = GATT_PRIMARY_SERVICE_UUID;
2005     gatt_client_run();
2006     return ERROR_CODE_SUCCESS;
2007 }
2008 
2009 uint8_t gatt_client_discover_secondary_services(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
2010     gatt_client_t * gatt_client;
2011     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2012     if (status != ERROR_CODE_SUCCESS){
2013         return status;
2014     }
2015     if (is_ready(gatt_client) == 0){
2016         return GATT_CLIENT_IN_WRONG_STATE;
2017     }
2018 
2019     gatt_client->callback = callback;
2020     gatt_client->start_group_handle = 0x0001;
2021     gatt_client->end_group_handle   = 0xffff;
2022     gatt_client->gatt_client_state = P_W2_SEND_SERVICE_QUERY;
2023     gatt_client->uuid16 = GATT_SECONDARY_SERVICE_UUID;
2024     gatt_client_run();
2025     return ERROR_CODE_SUCCESS;
2026 }
2027 
2028 uint8_t gatt_client_discover_primary_services_by_uuid16(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t uuid16){
2029     gatt_client_t * gatt_client;
2030     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2031     if (status != ERROR_CODE_SUCCESS){
2032         return status;
2033     }
2034     if (is_ready(gatt_client) == 0){
2035         return GATT_CLIENT_IN_WRONG_STATE;
2036     }
2037 
2038     gatt_client->callback = callback;
2039     gatt_client->start_group_handle = 0x0001;
2040     gatt_client->end_group_handle   = 0xffff;
2041     gatt_client->gatt_client_state = P_W2_SEND_SERVICE_WITH_UUID_QUERY;
2042     gatt_client->uuid16 = uuid16;
2043     uuid_add_bluetooth_prefix((uint8_t*) &(gatt_client->uuid128), gatt_client->uuid16);
2044     gatt_client_run();
2045     return ERROR_CODE_SUCCESS;
2046 }
2047 
2048 uint8_t gatt_client_discover_primary_services_by_uuid128(btstack_packet_handler_t callback, hci_con_handle_t con_handle, const uint8_t * uuid128){
2049     gatt_client_t * gatt_client;
2050     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2051     if (status != ERROR_CODE_SUCCESS){
2052         return status;
2053     }
2054     if (is_ready(gatt_client) == 0){
2055         return GATT_CLIENT_IN_WRONG_STATE;
2056     }
2057 
2058     gatt_client->callback = callback;
2059     gatt_client->start_group_handle = 0x0001;
2060     gatt_client->end_group_handle   = 0xffff;
2061     gatt_client->uuid16 = 0;
2062     (void)memcpy(gatt_client->uuid128, uuid128, 16);
2063     gatt_client->gatt_client_state = P_W2_SEND_SERVICE_WITH_UUID_QUERY;
2064     gatt_client_run();
2065     return ERROR_CODE_SUCCESS;
2066 }
2067 
2068 uint8_t gatt_client_discover_characteristics_for_service(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_service_t * service){
2069     gatt_client_t * gatt_client;
2070     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2071     if (status != ERROR_CODE_SUCCESS){
2072         return status;
2073     }
2074     if (is_ready(gatt_client) == 0){
2075         return GATT_CLIENT_IN_WRONG_STATE;
2076     }
2077 
2078     gatt_client->callback = callback;
2079     gatt_client->start_group_handle = service->start_group_handle;
2080     gatt_client->end_group_handle   = service->end_group_handle;
2081     gatt_client->filter_with_uuid = 0;
2082     gatt_client->characteristic_start_handle = 0;
2083     gatt_client->gatt_client_state = P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY;
2084     gatt_client_run();
2085     return ERROR_CODE_SUCCESS;
2086 }
2087 
2088 uint8_t gatt_client_find_included_services_for_service(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_service_t * service){
2089     gatt_client_t * gatt_client;
2090     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2091     if (status != ERROR_CODE_SUCCESS){
2092         return status;
2093     }
2094     if (is_ready(gatt_client) == 0){
2095         return GATT_CLIENT_IN_WRONG_STATE;
2096     }
2097     gatt_client->callback = callback;
2098     gatt_client->start_group_handle = service->start_group_handle;
2099     gatt_client->end_group_handle   = service->end_group_handle;
2100     gatt_client->gatt_client_state = P_W2_SEND_INCLUDED_SERVICE_QUERY;
2101 
2102     gatt_client_run();
2103     return ERROR_CODE_SUCCESS;
2104 }
2105 
2106 uint8_t gatt_client_discover_characteristics_for_handle_range_by_uuid16(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t start_handle, uint16_t end_handle, uint16_t uuid16){
2107     gatt_client_t * gatt_client;
2108     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2109     if (status != ERROR_CODE_SUCCESS){
2110         return status;
2111     }
2112     if (is_ready(gatt_client) == 0){
2113         return GATT_CLIENT_IN_WRONG_STATE;
2114     }
2115 
2116     gatt_client->callback = callback;
2117     gatt_client->start_group_handle = start_handle;
2118     gatt_client->end_group_handle   = end_handle;
2119     gatt_client->filter_with_uuid = 1;
2120     gatt_client->uuid16 = uuid16;
2121     uuid_add_bluetooth_prefix((uint8_t*) &(gatt_client->uuid128), uuid16);
2122     gatt_client->characteristic_start_handle = 0;
2123     gatt_client->gatt_client_state = P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY;
2124     gatt_client_run();
2125     return ERROR_CODE_SUCCESS;
2126 }
2127 
2128 uint8_t gatt_client_discover_characteristics_for_handle_range_by_uuid128(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128){
2129     gatt_client_t * gatt_client;
2130     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2131     if (status != ERROR_CODE_SUCCESS){
2132         return status;
2133     }
2134     if (is_ready(gatt_client) == 0){
2135         return GATT_CLIENT_IN_WRONG_STATE;
2136     }
2137 
2138     gatt_client->callback = callback;
2139     gatt_client->start_group_handle = start_handle;
2140     gatt_client->end_group_handle   = end_handle;
2141     gatt_client->filter_with_uuid = 1;
2142     gatt_client->uuid16 = 0;
2143     (void)memcpy(gatt_client->uuid128, uuid128, 16);
2144     gatt_client->characteristic_start_handle = 0;
2145     gatt_client->gatt_client_state = P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY;
2146     gatt_client_run();
2147     return ERROR_CODE_SUCCESS;
2148 }
2149 
2150 
2151 uint8_t gatt_client_discover_characteristics_for_service_by_uuid16(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_service_t * service, uint16_t uuid16){
2152     return gatt_client_discover_characteristics_for_handle_range_by_uuid16(callback, con_handle, service->start_group_handle, service->end_group_handle, uuid16);
2153 }
2154 
2155 uint8_t gatt_client_discover_characteristics_for_service_by_uuid128(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_service_t * service, const uint8_t * uuid128){
2156     return gatt_client_discover_characteristics_for_handle_range_by_uuid128(callback, con_handle, service->start_group_handle, service->end_group_handle, uuid128);
2157 }
2158 
2159 uint8_t gatt_client_discover_characteristic_descriptors(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic){
2160     gatt_client_t * gatt_client;
2161     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2162     if (status != ERROR_CODE_SUCCESS){
2163         return status;
2164     }
2165     if (is_ready(gatt_client) == 0){
2166         return GATT_CLIENT_IN_WRONG_STATE;
2167     }
2168 
2169     if (characteristic->value_handle == characteristic->end_handle){
2170         emit_gatt_complete_event(gatt_client, ATT_ERROR_SUCCESS);
2171         return ERROR_CODE_SUCCESS;
2172     }
2173     gatt_client->callback = callback;
2174     gatt_client->start_group_handle = characteristic->value_handle + 1u;
2175     gatt_client->end_group_handle   = characteristic->end_handle;
2176     gatt_client->gatt_client_state = P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY;
2177     gatt_client_run();
2178     return ERROR_CODE_SUCCESS;
2179 }
2180 
2181 uint8_t gatt_client_read_value_of_characteristic_using_value_handle(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle){
2182     gatt_client_t * gatt_client;
2183     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2184     if (status != ERROR_CODE_SUCCESS){
2185         return status;
2186     }
2187     if (is_ready(gatt_client) == 0){
2188         return GATT_CLIENT_IN_WRONG_STATE;
2189     }
2190 
2191     gatt_client->callback = callback;
2192     gatt_client->attribute_handle = value_handle;
2193     gatt_client->attribute_offset = 0;
2194     gatt_client->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY;
2195     gatt_client_run();
2196     return ERROR_CODE_SUCCESS;
2197 }
2198 
2199 uint8_t gatt_client_read_value_of_characteristics_by_uuid16(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t start_handle, uint16_t end_handle, uint16_t uuid16){
2200     gatt_client_t * gatt_client;
2201     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2202     if (status != ERROR_CODE_SUCCESS){
2203         return status;
2204     }
2205     if (is_ready(gatt_client) == 0){
2206         return GATT_CLIENT_IN_WRONG_STATE;
2207     }
2208 
2209     gatt_client->callback = callback;
2210     gatt_client->start_group_handle = start_handle;
2211     gatt_client->end_group_handle = end_handle;
2212     gatt_client->query_start_handle = start_handle;
2213     gatt_client->query_end_handle = end_handle;
2214     gatt_client->uuid16 = uuid16;
2215     uuid_add_bluetooth_prefix((uint8_t*) &(gatt_client->uuid128), uuid16);
2216     gatt_client->gatt_client_state = P_W2_SEND_READ_BY_TYPE_REQUEST;
2217     gatt_client_run();
2218     return ERROR_CODE_SUCCESS;
2219 }
2220 
2221 uint8_t gatt_client_read_value_of_characteristics_by_uuid128(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t start_handle, uint16_t end_handle, const uint8_t * uuid128){
2222     gatt_client_t * gatt_client;
2223     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2224     if (status != ERROR_CODE_SUCCESS){
2225         return status;
2226     }
2227     if (is_ready(gatt_client) == 0){
2228         return GATT_CLIENT_IN_WRONG_STATE;
2229     }
2230 
2231     gatt_client->callback = callback;
2232     gatt_client->start_group_handle = start_handle;
2233     gatt_client->end_group_handle = end_handle;
2234     gatt_client->query_start_handle = start_handle;
2235     gatt_client->query_end_handle = end_handle;
2236     gatt_client->uuid16 = 0;
2237     (void)memcpy(gatt_client->uuid128, uuid128, 16);
2238     gatt_client->gatt_client_state = P_W2_SEND_READ_BY_TYPE_REQUEST;
2239     gatt_client_run();
2240     return ERROR_CODE_SUCCESS;
2241 }
2242 
2243 
2244 uint8_t gatt_client_read_value_of_characteristic(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic){
2245     return gatt_client_read_value_of_characteristic_using_value_handle(callback, con_handle, characteristic->value_handle);
2246 }
2247 
2248 uint8_t gatt_client_read_long_value_of_characteristic_using_value_handle_with_offset(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle, uint16_t offset){
2249     gatt_client_t * gatt_client;
2250     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2251     if (status != ERROR_CODE_SUCCESS){
2252         return status;
2253     }
2254     if (is_ready(gatt_client) == 0){
2255         return GATT_CLIENT_IN_WRONG_STATE;
2256     }
2257 
2258     gatt_client->callback = callback;
2259     gatt_client->attribute_handle = value_handle;
2260     gatt_client->attribute_offset = offset;
2261     gatt_client->gatt_client_state = P_W2_SEND_READ_BLOB_QUERY;
2262     gatt_client_run();
2263     return ERROR_CODE_SUCCESS;
2264 }
2265 
2266 uint8_t gatt_client_read_long_value_of_characteristic_using_value_handle(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle){
2267     return gatt_client_read_long_value_of_characteristic_using_value_handle_with_offset(callback, con_handle, value_handle, 0);
2268 }
2269 
2270 uint8_t gatt_client_read_long_value_of_characteristic(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic){
2271     return gatt_client_read_long_value_of_characteristic_using_value_handle(callback, con_handle, characteristic->value_handle);
2272 }
2273 
2274 uint8_t gatt_client_read_multiple_characteristic_values(btstack_packet_handler_t callback, hci_con_handle_t con_handle, int num_value_handles, uint16_t * value_handles){
2275     gatt_client_t * gatt_client;
2276     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2277     if (status != ERROR_CODE_SUCCESS){
2278         return status;
2279     }
2280     if (is_ready(gatt_client) == 0){
2281         return GATT_CLIENT_IN_WRONG_STATE;
2282     }
2283 
2284     gatt_client->callback = callback;
2285     gatt_client->read_multiple_handle_count = num_value_handles;
2286     gatt_client->read_multiple_handles = value_handles;
2287     gatt_client->gatt_client_state = P_W2_SEND_READ_MULTIPLE_REQUEST;
2288     gatt_client_run();
2289     return ERROR_CODE_SUCCESS;
2290 }
2291 
2292 uint8_t gatt_client_write_value_of_characteristic_without_response(hci_con_handle_t con_handle, uint16_t value_handle, uint16_t value_length, uint8_t * value){
2293     gatt_client_t * gatt_client;
2294     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
2295     if (status != ERROR_CODE_SUCCESS){
2296         return status;
2297     }
2298 
2299     if (value_length > (gatt_client->mtu - 3u)) return GATT_CLIENT_VALUE_TOO_LONG;
2300     if (!att_dispatch_client_can_send_now(gatt_client->con_handle)) return GATT_CLIENT_BUSY;
2301 
2302     return att_write_request(gatt_client, ATT_WRITE_COMMAND, value_handle, value_length, value);
2303 }
2304 
2305 uint8_t gatt_client_write_value_of_characteristic(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle, uint16_t value_length, uint8_t * value){
2306     gatt_client_t * gatt_client;
2307     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2308     if (status != ERROR_CODE_SUCCESS){
2309         return status;
2310     }
2311     if (is_ready(gatt_client) == 0){
2312         return GATT_CLIENT_IN_WRONG_STATE;
2313     }
2314 
2315     gatt_client->callback = callback;
2316     gatt_client->attribute_handle = value_handle;
2317     gatt_client->attribute_length = value_length;
2318     gatt_client->attribute_value = value;
2319     gatt_client->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_VALUE;
2320     gatt_client_run();
2321     return ERROR_CODE_SUCCESS;
2322 }
2323 
2324 uint8_t gatt_client_write_long_value_of_characteristic_with_offset(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle, uint16_t offset, uint16_t value_length, uint8_t * value){
2325     gatt_client_t * gatt_client;
2326     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2327     if (status != ERROR_CODE_SUCCESS){
2328         return status;
2329     }
2330     if (is_ready(gatt_client) == 0){
2331         return GATT_CLIENT_IN_WRONG_STATE;
2332     }
2333 
2334     gatt_client->callback = callback;
2335     gatt_client->attribute_handle = value_handle;
2336     gatt_client->attribute_length = value_length;
2337     gatt_client->attribute_offset = offset;
2338     gatt_client->attribute_value = value;
2339     gatt_client->gatt_client_state = P_W2_PREPARE_WRITE;
2340     gatt_client_run();
2341     return ERROR_CODE_SUCCESS;
2342 }
2343 
2344 uint8_t gatt_client_write_long_value_of_characteristic(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle, uint16_t value_length, uint8_t * value){
2345     return gatt_client_write_long_value_of_characteristic_with_offset(callback, con_handle, value_handle, 0, value_length, value);
2346 }
2347 
2348 uint8_t gatt_client_reliable_write_long_value_of_characteristic(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t value_handle, uint16_t value_length, uint8_t * value){
2349     gatt_client_t * gatt_client;
2350     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2351     if (status != ERROR_CODE_SUCCESS){
2352         return status;
2353     }
2354     if (is_ready(gatt_client) == 0){
2355         return GATT_CLIENT_IN_WRONG_STATE;
2356     }
2357 
2358     gatt_client->callback = callback;
2359     gatt_client->attribute_handle = value_handle;
2360     gatt_client->attribute_length = value_length;
2361     gatt_client->attribute_offset = 0;
2362     gatt_client->attribute_value = value;
2363     gatt_client->gatt_client_state = P_W2_PREPARE_RELIABLE_WRITE;
2364     gatt_client_run();
2365     return ERROR_CODE_SUCCESS;
2366 }
2367 
2368 uint8_t gatt_client_write_client_characteristic_configuration(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic, uint16_t configuration){
2369     gatt_client_t * gatt_client;
2370     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2371     if (status != ERROR_CODE_SUCCESS){
2372         return status;
2373     }
2374     if (is_ready(gatt_client) == 0){
2375         return GATT_CLIENT_IN_WRONG_STATE;
2376     }
2377 
2378     if ( (configuration & GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION) &&
2379         ((characteristic->properties & ATT_PROPERTY_NOTIFY) == 0u)) {
2380         log_info("gatt_client_write_client_characteristic_configuration: GATT_CLIENT_CHARACTERISTIC_NOTIFICATION_NOT_SUPPORTED");
2381         return GATT_CLIENT_CHARACTERISTIC_NOTIFICATION_NOT_SUPPORTED;
2382     } else if ( (configuration & GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_INDICATION) &&
2383                ((characteristic->properties & ATT_PROPERTY_INDICATE) == 0u)){
2384         log_info("gatt_client_write_client_characteristic_configuration: GATT_CLIENT_CHARACTERISTIC_INDICATION_NOT_SUPPORTED");
2385         return GATT_CLIENT_CHARACTERISTIC_INDICATION_NOT_SUPPORTED;
2386     }
2387 
2388     gatt_client->callback = callback;
2389     gatt_client->start_group_handle = characteristic->value_handle;
2390     gatt_client->end_group_handle = characteristic->end_handle;
2391     little_endian_store_16(gatt_client->client_characteristic_configuration_value, 0, configuration);
2392 
2393 #ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY
2394     gatt_client->gatt_client_state = P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY;
2395 #else
2396     gatt_client->gatt_client_state = P_W2_SEND_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY;
2397 #endif
2398     gatt_client_run();
2399     return ERROR_CODE_SUCCESS;
2400 }
2401 
2402 uint8_t gatt_client_read_characteristic_descriptor_using_descriptor_handle(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t descriptor_handle){
2403     gatt_client_t * gatt_client;
2404     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2405     if (status != ERROR_CODE_SUCCESS){
2406         return status;
2407     }
2408     if (is_ready(gatt_client) == 0){
2409         return GATT_CLIENT_IN_WRONG_STATE;
2410     }
2411 
2412     gatt_client->callback = callback;
2413     gatt_client->attribute_handle = descriptor_handle;
2414 
2415     gatt_client->gatt_client_state = P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY;
2416     gatt_client_run();
2417     return ERROR_CODE_SUCCESS;
2418 }
2419 
2420 uint8_t gatt_client_read_characteristic_descriptor(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_descriptor_t * descriptor){
2421     return gatt_client_read_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle);
2422 }
2423 
2424 uint8_t gatt_client_read_long_characteristic_descriptor_using_descriptor_handle_with_offset(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t descriptor_handle, uint16_t offset){
2425     gatt_client_t * gatt_client;
2426     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2427     if (status != ERROR_CODE_SUCCESS){
2428         return status;
2429     }
2430     if (is_ready(gatt_client) == 0){
2431         return GATT_CLIENT_IN_WRONG_STATE;
2432     }
2433 
2434     gatt_client->callback = callback;
2435     gatt_client->attribute_handle = descriptor_handle;
2436     gatt_client->attribute_offset = offset;
2437     gatt_client->gatt_client_state = P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY;
2438     gatt_client_run();
2439     return ERROR_CODE_SUCCESS;
2440 }
2441 
2442 uint8_t gatt_client_read_long_characteristic_descriptor_using_descriptor_handle(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t descriptor_handle){
2443     return gatt_client_read_long_characteristic_descriptor_using_descriptor_handle_with_offset(callback, con_handle, descriptor_handle, 0);
2444 }
2445 
2446 uint8_t gatt_client_read_long_characteristic_descriptor(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_descriptor_t * descriptor){
2447     return gatt_client_read_long_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle);
2448 }
2449 
2450 uint8_t gatt_client_write_characteristic_descriptor_using_descriptor_handle(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t descriptor_handle, uint16_t value_length, uint8_t * value){
2451     gatt_client_t * gatt_client;
2452     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2453     if (status != ERROR_CODE_SUCCESS){
2454         return status;
2455     }
2456     if (is_ready(gatt_client) == 0){
2457         return GATT_CLIENT_IN_WRONG_STATE;
2458     }
2459 
2460     gatt_client->callback = callback;
2461     gatt_client->attribute_handle = descriptor_handle;
2462     gatt_client->attribute_length = value_length;
2463     gatt_client->attribute_offset = 0;
2464     gatt_client->attribute_value = value;
2465     gatt_client->gatt_client_state = P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR;
2466     gatt_client_run();
2467     return ERROR_CODE_SUCCESS;
2468 }
2469 
2470 uint8_t gatt_client_write_characteristic_descriptor(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_descriptor_t * descriptor, uint16_t value_length, uint8_t * value){
2471     return gatt_client_write_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle, value_length, value);
2472 }
2473 
2474 uint8_t gatt_client_write_long_characteristic_descriptor_using_descriptor_handle_with_offset(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t descriptor_handle, uint16_t offset, uint16_t value_length, uint8_t * value){
2475     gatt_client_t * gatt_client;
2476     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2477     if (status != ERROR_CODE_SUCCESS){
2478         return status;
2479     }
2480     if (is_ready(gatt_client) == 0){
2481         return GATT_CLIENT_IN_WRONG_STATE;
2482     }
2483 
2484     gatt_client->callback = callback;
2485     gatt_client->attribute_handle = descriptor_handle;
2486     gatt_client->attribute_length = value_length;
2487     gatt_client->attribute_offset = offset;
2488     gatt_client->attribute_value = value;
2489     gatt_client->gatt_client_state = P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR;
2490     gatt_client_run();
2491     return ERROR_CODE_SUCCESS;
2492 }
2493 
2494 uint8_t gatt_client_write_long_characteristic_descriptor_using_descriptor_handle(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t descriptor_handle, uint16_t value_length, uint8_t * value){
2495     return gatt_client_write_long_characteristic_descriptor_using_descriptor_handle_with_offset(callback, con_handle, descriptor_handle, 0, value_length, value);
2496 }
2497 
2498 uint8_t gatt_client_write_long_characteristic_descriptor(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_descriptor_t * descriptor, uint16_t value_length, uint8_t * value){
2499     return gatt_client_write_long_characteristic_descriptor_using_descriptor_handle(callback, con_handle, descriptor->handle, value_length, value);
2500 }
2501 
2502 /**
2503  * @brief -> gatt complete event
2504  */
2505 uint8_t gatt_client_prepare_write(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t attribute_handle, uint16_t offset, uint16_t value_length, uint8_t * value){
2506     gatt_client_t * gatt_client;
2507     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2508     if (status != ERROR_CODE_SUCCESS){
2509         return status;
2510     }
2511     if (is_ready(gatt_client) == 0){
2512         return GATT_CLIENT_IN_WRONG_STATE;
2513     }
2514 
2515     gatt_client->callback = callback;
2516     gatt_client->attribute_handle = attribute_handle;
2517     gatt_client->attribute_length = value_length;
2518     gatt_client->attribute_offset = offset;
2519     gatt_client->attribute_value = value;
2520     gatt_client->gatt_client_state = P_W2_PREPARE_WRITE_SINGLE;
2521     gatt_client_run();
2522     return ERROR_CODE_SUCCESS;
2523 }
2524 
2525 /**
2526  * @brief -> gatt complete event
2527  */
2528 uint8_t gatt_client_execute_write(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
2529     gatt_client_t * gatt_client;
2530     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2531     if (status != ERROR_CODE_SUCCESS){
2532         return status;
2533     }
2534     if (is_ready(gatt_client) == 0){
2535         return GATT_CLIENT_IN_WRONG_STATE;
2536     }
2537 
2538     gatt_client->callback = callback;
2539     gatt_client->gatt_client_state = P_W2_EXECUTE_PREPARED_WRITE;
2540     gatt_client_run();
2541     return ERROR_CODE_SUCCESS;
2542 }
2543 
2544 /**
2545  * @brief -> gatt complete event
2546  */
2547 uint8_t gatt_client_cancel_write(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
2548     gatt_client_t * gatt_client;
2549     uint8_t status = gatt_client_provide_context_for_handle_and_start_timer(con_handle, &gatt_client);
2550     if (status != ERROR_CODE_SUCCESS){
2551         return status;
2552     }
2553     if (is_ready(gatt_client) == 0){
2554         return GATT_CLIENT_IN_WRONG_STATE;
2555     }
2556 
2557     gatt_client->callback = callback;
2558     gatt_client->gatt_client_state = P_W2_CANCEL_PREPARED_WRITE;
2559     gatt_client_run();
2560     return ERROR_CODE_SUCCESS;
2561 }
2562 
2563 void gatt_client_deserialize_service(const uint8_t *packet, int offset, gatt_client_service_t * service){
2564     service->start_group_handle = little_endian_read_16(packet, offset);
2565     service->end_group_handle = little_endian_read_16(packet, offset + 2);
2566     reverse_128(&packet[offset + 4], service->uuid128);
2567     if (uuid_has_bluetooth_prefix(service->uuid128)){
2568         service->uuid16 = big_endian_read_32(service->uuid128, 0);
2569     } else {
2570         service->uuid16 = 0;
2571     }
2572 }
2573 
2574 void gatt_client_deserialize_characteristic(const uint8_t * packet, int offset, gatt_client_characteristic_t * characteristic){
2575     characteristic->start_handle = little_endian_read_16(packet, offset);
2576     characteristic->value_handle = little_endian_read_16(packet, offset + 2);
2577     characteristic->end_handle = little_endian_read_16(packet, offset + 4);
2578     characteristic->properties = little_endian_read_16(packet, offset + 6);
2579     reverse_128(&packet[offset+8], characteristic->uuid128);
2580     if (uuid_has_bluetooth_prefix(characteristic->uuid128)){
2581         characteristic->uuid16 = big_endian_read_32(characteristic->uuid128, 0);
2582     } else {
2583         characteristic->uuid16 = 0;
2584     }
2585 }
2586 
2587 void gatt_client_deserialize_characteristic_descriptor(const uint8_t * packet, int offset, gatt_client_characteristic_descriptor_t * descriptor){
2588     descriptor->handle = little_endian_read_16(packet, offset);
2589     reverse_128(&packet[offset+2], descriptor->uuid128);
2590     if (uuid_has_bluetooth_prefix(descriptor->uuid128)){
2591         descriptor->uuid16 = big_endian_read_32(descriptor->uuid128, 0);
2592     } else {
2593         descriptor->uuid16 = 0;
2594     }
2595 }
2596 
2597 void gatt_client_send_mtu_negotiation(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
2598     gatt_client_t * gatt_client;
2599     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
2600     if (status != ERROR_CODE_SUCCESS){
2601         return;
2602     }
2603     if (gatt_client->mtu_state == MTU_AUTO_EXCHANGE_DISABLED){
2604         gatt_client->callback = callback;
2605         gatt_client->mtu_state = SEND_MTU_EXCHANGE;
2606         gatt_client_run();
2607     }
2608 }
2609 
2610 uint8_t gatt_client_request_to_write_without_response(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
2611     gatt_client_t * gatt_client;
2612     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
2613     if (status != ERROR_CODE_SUCCESS){
2614         return status;
2615     }
2616     bool added = btstack_linked_list_add_tail(&gatt_client->write_without_response_requests, (btstack_linked_item_t*) callback_registration);
2617     att_dispatch_client_request_can_send_now_event(gatt_client->con_handle);
2618     if (added){
2619         return ERROR_CODE_SUCCESS;
2620     } else {
2621         return ERROR_CODE_COMMAND_DISALLOWED;
2622     }
2623 }
2624 
2625 uint8_t gatt_client_request_to_send_gatt_query(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle){
2626     gatt_client_t * gatt_client;
2627     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
2628     if (status != ERROR_CODE_SUCCESS){
2629         return status;
2630     }
2631     bool added = btstack_linked_list_add_tail(&gatt_client->query_requests, (btstack_linked_item_t*) callback_registration);
2632     gatt_client_notify_can_send_query(gatt_client);
2633     if (added){
2634         return ERROR_CODE_SUCCESS;
2635     } else {
2636         return ERROR_CODE_COMMAND_DISALLOWED;
2637     }
2638 }
2639 
2640 uint8_t gatt_client_request_can_write_without_response_event(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
2641     gatt_client_t * gatt_client;
2642     uint8_t status = gatt_client_provide_context_for_handle(con_handle, &gatt_client);
2643     if (status != ERROR_CODE_SUCCESS){
2644         return status;
2645     }
2646     if (gatt_client->write_without_response_callback != NULL){
2647         return GATT_CLIENT_IN_WRONG_STATE;
2648     }
2649     gatt_client->write_without_response_callback = callback;
2650     att_dispatch_client_request_can_send_now_event(gatt_client->con_handle);
2651     return ERROR_CODE_SUCCESS;
2652 }
2653 
2654 #ifdef ENABLE_GATT_OVER_CLASSIC
2655 
2656 #include "hci_event.h"
2657 
2658 // single active SDP query
2659 static gatt_client_t * gatt_client_classic_active_sdp_query;
2660 
2661 // macos protocol descriptor list requires 16 bytes
2662 static uint8_t gatt_client_classic_sdp_buffer[32];
2663 
2664 static const hci_event_t gatt_client_connected = {
2665         GATT_EVENT_CONNECTED, 0, "1BH"
2666 };
2667 
2668 static const hci_event_t gatt_client_disconnected = {
2669         GATT_EVENT_DISCONNECTED, 0, "H"
2670 };
2671 
2672 static gatt_client_t * gatt_client_get_context_for_classic_addr(bd_addr_t addr){
2673     btstack_linked_item_t *it;
2674     for (it = (btstack_linked_item_t *) gatt_client_connections; it != NULL; it = it->next){
2675         gatt_client_t * gatt_client = (gatt_client_t *) it;
2676         if (memcmp(gatt_client->addr, addr, 6) == 0){
2677             return gatt_client;
2678         }
2679     }
2680     return NULL;
2681 }
2682 
2683 static gatt_client_t * gatt_client_get_context_for_l2cap_cid(uint16_t l2cap_cid){
2684     btstack_linked_item_t *it;
2685     for (it = (btstack_linked_item_t *) gatt_client_connections; it != NULL; it = it->next){
2686         gatt_client_t * gatt_client = (gatt_client_t *) it;
2687         if (gatt_client->l2cap_cid == l2cap_cid){
2688             return gatt_client;
2689         }
2690     }
2691     return NULL;
2692 }
2693 
2694 static void gatt_client_classic_handle_connected(gatt_client_t * gatt_client, uint8_t status){
2695     bd_addr_t addr;
2696     memcpy(addr, gatt_client->addr, 6);
2697     hci_con_handle_t con_handle = gatt_client->con_handle;
2698     btstack_packet_handler_t callback = gatt_client->callback;
2699     if (status != ERROR_CODE_SUCCESS){
2700         btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) gatt_client);
2701         btstack_memory_gatt_client_free(gatt_client);
2702     }
2703     uint8_t buffer[20];
2704     uint16_t len = hci_event_create_from_template_and_arguments(buffer, sizeof(buffer), &gatt_client_connected, status, addr,
2705                                                                 con_handle);
2706     (*callback)(HCI_EVENT_PACKET, 0, buffer, len);
2707 }
2708 
2709 static void gatt_client_classic_handle_disconnected(gatt_client_t * gatt_client){
2710 
2711     gatt_client_report_error_if_pending(gatt_client, ATT_ERROR_HCI_DISCONNECT_RECEIVED);
2712     gatt_client_timeout_stop(gatt_client);
2713 
2714     hci_con_handle_t con_handle = gatt_client->con_handle;
2715     btstack_packet_handler_t callback = gatt_client->callback;
2716     btstack_linked_list_remove(&gatt_client_connections, (btstack_linked_item_t *) gatt_client);
2717     btstack_memory_gatt_client_free(gatt_client);
2718 
2719     uint8_t buffer[20];
2720     uint16_t len = hci_event_create_from_template_and_arguments(buffer, sizeof(buffer), &gatt_client_disconnected, con_handle);
2721     (*callback)(HCI_EVENT_PACKET, 0, buffer, len);
2722 }
2723 
2724 static void gatt_client_l2cap_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
2725     gatt_client_t * gatt_client = NULL;
2726     uint8_t status;
2727     switch (packet_type){
2728         case HCI_EVENT_PACKET:
2729             switch (hci_event_packet_get_type(packet)) {
2730                 case L2CAP_EVENT_CHANNEL_OPENED:
2731                     status = l2cap_event_channel_opened_get_status(packet);
2732                     gatt_client = gatt_client_get_context_for_l2cap_cid(l2cap_event_channel_opened_get_local_cid(packet));
2733                     btstack_assert(gatt_client != NULL);
2734                     // if status != 0, gatt_client will be discarded
2735                     gatt_client->gatt_client_state = P_READY;
2736                     gatt_client->con_handle = l2cap_event_channel_opened_get_handle(packet);
2737                     gatt_client->mtu = l2cap_event_channel_opened_get_remote_mtu(packet);
2738                     gatt_client_classic_handle_connected(gatt_client, status);
2739                     break;
2740                 case L2CAP_EVENT_CHANNEL_CLOSED:
2741                     gatt_client = gatt_client_get_context_for_l2cap_cid(l2cap_event_channel_closed_get_local_cid(packet));
2742                     gatt_client_classic_handle_disconnected(gatt_client);
2743                     break;
2744                 default:
2745                     break;
2746             }
2747             break;
2748         case L2CAP_DATA_PACKET:
2749             gatt_client = gatt_client_get_context_for_l2cap_cid(channel);
2750             btstack_assert(gatt_client != NULL);
2751             gatt_client_handle_att_response(gatt_client, packet, size);
2752             gatt_client_run();
2753             break;
2754     }
2755 }
2756 
2757 static void gatt_client_handle_sdp_client_query_attribute_value(gatt_client_t * connection, uint8_t *packet){
2758     des_iterator_t des_list_it;
2759     des_iterator_t prot_it;
2760 
2761     if (sdp_event_query_attribute_byte_get_attribute_length(packet) <= sizeof(gatt_client_classic_sdp_buffer)) {
2762         gatt_client_classic_sdp_buffer[sdp_event_query_attribute_byte_get_data_offset(packet)] = sdp_event_query_attribute_byte_get_data(packet);
2763         if ((uint16_t)(sdp_event_query_attribute_byte_get_data_offset(packet)+1) == sdp_event_query_attribute_byte_get_attribute_length(packet)) {
2764             switch(sdp_event_query_attribute_byte_get_attribute_id(packet)) {
2765                 case BLUETOOTH_ATTRIBUTE_PROTOCOL_DESCRIPTOR_LIST:
2766                     for (des_iterator_init(&des_list_it, gatt_client_classic_sdp_buffer); des_iterator_has_more(&des_list_it); des_iterator_next(&des_list_it)) {
2767                         uint8_t       *des_element;
2768                         uint8_t       *element;
2769                         uint32_t       uuid;
2770 
2771                         if (des_iterator_get_type(&des_list_it) != DE_DES) continue;
2772 
2773                         des_element = des_iterator_get_element(&des_list_it);
2774                         des_iterator_init(&prot_it, des_element);
2775                         element = des_iterator_get_element(&prot_it);
2776 
2777                         if (de_get_element_type(element) != DE_UUID) continue;
2778 
2779                         uuid = de_get_uuid32(element);
2780                         des_iterator_next(&prot_it);
2781                         // we assume that the even if there are both roles supported, remote device uses the same psm and avdtp version for both
2782                         switch (uuid){
2783                             case BLUETOOTH_PROTOCOL_L2CAP:
2784                                 if (!des_iterator_has_more(&prot_it)) continue;
2785                                 de_element_get_uint16(des_iterator_get_element(&prot_it), &connection->l2cap_psm);
2786                                 break;
2787                             default:
2788                                 break;
2789                         }
2790                     }
2791                     break;
2792 
2793                 default:
2794                     break;
2795             }
2796         }
2797     }
2798 }
2799 
2800 static void gatt_client_classic_sdp_handler(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
2801     gatt_client_t * gatt_client = gatt_client_classic_active_sdp_query;
2802     btstack_assert(gatt_client != NULL);
2803     uint8_t status;
2804 
2805     // TODO: handle sdp events, get l2cap psm
2806     switch (hci_event_packet_get_type(packet)){
2807         case SDP_EVENT_QUERY_ATTRIBUTE_VALUE:
2808             gatt_client_handle_sdp_client_query_attribute_value(gatt_client, packet);
2809             // TODO:
2810             return;
2811         case SDP_EVENT_QUERY_COMPLETE:
2812             status = sdp_event_query_complete_get_status(packet);
2813             gatt_client_classic_active_sdp_query = NULL;
2814             log_info("l2cap psm: %0x, status %02x", gatt_client->l2cap_psm, status);
2815             if (status != ERROR_CODE_SUCCESS) break;
2816             if (gatt_client->l2cap_psm == 0) {
2817                 status = SDP_SERVICE_NOT_FOUND;
2818                 break;
2819             }
2820             break;
2821         default:
2822             btstack_assert(false);
2823             return;
2824     }
2825 
2826     // done
2827     if (status == ERROR_CODE_SUCCESS){
2828         gatt_client->gatt_client_state = P_W4_L2CAP_CONNECTION;
2829         status = l2cap_create_channel(gatt_client_l2cap_handler, gatt_client->addr, gatt_client->l2cap_psm, 0xffff,
2830                              &gatt_client->l2cap_cid);
2831     }
2832     if (status != ERROR_CODE_SUCCESS) {
2833         gatt_client_classic_handle_connected(gatt_client, status);
2834     }
2835 }
2836 
2837 static void gatt_client_classic_sdp_start(void * context){
2838     gatt_client_classic_active_sdp_query = (gatt_client_t *) context;
2839     gatt_client_classic_active_sdp_query->gatt_client_state = P_W4_SDP_QUERY;
2840     sdp_client_query_uuid16(gatt_client_classic_sdp_handler, gatt_client_classic_active_sdp_query->addr, ORG_BLUETOOTH_SERVICE_GENERIC_ATTRIBUTE);
2841 }
2842 
2843 uint8_t gatt_client_classic_connect(btstack_packet_handler_t callback, bd_addr_t addr){
2844     gatt_client_t * gatt_client = gatt_client_get_context_for_classic_addr(addr);
2845     if (gatt_client != NULL){
2846         return ERROR_CODE_ACL_CONNECTION_ALREADY_EXISTS;
2847     }
2848     gatt_client = btstack_memory_gatt_client_get();
2849     if (gatt_client == NULL){
2850         return ERROR_CODE_MEMORY_CAPACITY_EXCEEDED;
2851     }
2852     // init state
2853     gatt_client->con_handle = HCI_CON_HANDLE_INVALID;
2854     memcpy(gatt_client->addr, addr, 6);
2855     gatt_client->mtu = ATT_DEFAULT_MTU;
2856     gatt_client->security_level = LEVEL_0;
2857     gatt_client->mtu_state = MTU_AUTO_EXCHANGE_DISABLED;
2858     gatt_client->gatt_client_state = P_W2_SDP_QUERY;
2859     gatt_client->sdp_query_request.callback = &gatt_client_classic_sdp_start;
2860     gatt_client->sdp_query_request.context = gatt_client;
2861     gatt_client->callback = callback;
2862     btstack_linked_list_add(&gatt_client_connections, (btstack_linked_item_t*)gatt_client);
2863     sdp_client_register_query_callback(&gatt_client->sdp_query_request);
2864     return ERROR_CODE_COMMAND_DISALLOWED;
2865 }
2866 
2867 uint8_t gatt_client_classic_disconnect(btstack_packet_handler_t callback, hci_con_handle_t con_handle){
2868     gatt_client_t * gatt_client = gatt_client_get_context_for_handle(con_handle);
2869     if (gatt_client == NULL){
2870         return ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER;
2871     }
2872     gatt_client->callback = callback;
2873     return l2cap_disconnect(gatt_client->l2cap_cid);
2874 }
2875 #endif
2876 
2877 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
2878 void gatt_client_att_packet_handler_fuzz(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size){
2879     gatt_client_att_packet_handler(packet_type, handle, packet, size);
2880 }
2881 
2882 uint8_t gatt_client_get_client(hci_con_handle_t con_handle, gatt_client_t ** out_gatt_client){
2883     uint8_t status = gatt_client_provide_context_for_handle(con_handle, out_gatt_client);
2884     return status;
2885 }
2886 #endif
2887