xref: /btstack/src/ble/gatt_client.h (revision 6897da5c53aac5b1f90f41b5b15d0bd43d61dfff)
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 /**
39  * @title GATT Client
40  *
41  */
42 
43 #ifndef btstack_gatt_client_h
44 #define btstack_gatt_client_h
45 
46 #include "hci.h"
47 
48 // spec defines 100 ms, PTS might indicate an error if we sent after 100 ms
49 #define GATT_CLIENT_COLLISION_BACKOFF_MS 150
50 #if defined __cplusplus
51 extern "C" {
52 #endif
53 
54 typedef enum {
55     P_READY,
56     P_W2_EMIT_QUERY_COMPLETE_EVENT,
57     P_W2_SEND_SERVICE_QUERY,
58     P_W4_SERVICE_QUERY_RESULT,
59     P_W2_SEND_SERVICE_WITH_UUID_QUERY,
60     P_W4_SERVICE_WITH_UUID_RESULT,
61 
62     P_W2_SEND_ALL_CHARACTERISTICS_OF_SERVICE_QUERY,
63     P_W4_ALL_CHARACTERISTICS_OF_SERVICE_QUERY_RESULT,
64     P_W2_SEND_CHARACTERISTIC_WITH_UUID_QUERY,
65     P_W4_CHARACTERISTIC_WITH_UUID_QUERY_RESULT,
66 
67     P_W2_SEND_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY,
68     P_W4_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT,
69 
70     P_W2_SEND_INCLUDED_SERVICE_QUERY,
71     P_W4_INCLUDED_SERVICE_QUERY_RESULT,
72     P_W2_SEND_INCLUDED_SERVICE_WITH_UUID_QUERY,
73     P_W4_INCLUDED_SERVICE_UUID_WITH_QUERY_RESULT,
74 
75     P_W2_SEND_READ_CHARACTERISTIC_VALUE_QUERY,
76     P_W4_READ_CHARACTERISTIC_VALUE_RESULT,
77 
78     P_W2_SEND_READ_BLOB_QUERY,
79     P_W4_READ_BLOB_RESULT,
80 
81     P_W2_SEND_READ_BY_TYPE_REQUEST,
82     P_W4_READ_BY_TYPE_RESPONSE,
83 
84     P_W2_SEND_READ_MULTIPLE_REQUEST,
85     P_W4_READ_MULTIPLE_RESPONSE,
86 
87     P_W2_SEND_READ_MULTIPLE_VARIABLE_REQUEST,
88     P_W4_READ_MULTIPLE_VARIABLE_RESPONSE,
89 
90     P_W2_SEND_WRITE_CHARACTERISTIC_VALUE,
91     P_W4_WRITE_CHARACTERISTIC_VALUE_RESULT,
92 
93     P_W2_PREPARE_WRITE,
94     P_W4_PREPARE_WRITE_RESULT,
95     P_W2_PREPARE_RELIABLE_WRITE,
96     P_W4_PREPARE_RELIABLE_WRITE_RESULT,
97 
98     P_W2_EXECUTE_PREPARED_WRITE,
99     P_W4_EXECUTE_PREPARED_WRITE_RESULT,
100     P_W2_CANCEL_PREPARED_WRITE,
101     P_W4_CANCEL_PREPARED_WRITE_RESULT,
102     P_W2_CANCEL_PREPARED_WRITE_DATA_MISMATCH,
103     P_W4_CANCEL_PREPARED_WRITE_DATA_MISMATCH_RESULT,
104 
105 #ifdef ENABLE_GATT_FIND_INFORMATION_FOR_CCC_DISCOVERY
106     P_W2_SEND_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY,
107     P_W4_FIND_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT,
108 #else
109     P_W2_SEND_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY,
110     P_W4_READ_CLIENT_CHARACTERISTIC_CONFIGURATION_QUERY_RESULT,
111 #endif
112     P_W2_WRITE_CLIENT_CHARACTERISTIC_CONFIGURATION,
113     P_W4_CLIENT_CHARACTERISTIC_CONFIGURATION_RESULT,
114 
115     P_W2_SEND_READ_CHARACTERISTIC_DESCRIPTOR_QUERY,
116     P_W4_READ_CHARACTERISTIC_DESCRIPTOR_RESULT,
117 
118     P_W2_SEND_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_QUERY,
119     P_W4_READ_BLOB_CHARACTERISTIC_DESCRIPTOR_RESULT,
120 
121     P_W2_SEND_WRITE_CHARACTERISTIC_DESCRIPTOR,
122     P_W4_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT,
123 
124     // all long writes use this
125     P_W2_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR,
126     P_W4_PREPARE_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT,
127     P_W2_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR,
128     P_W4_EXECUTE_PREPARED_WRITE_CHARACTERISTIC_DESCRIPTOR_RESULT,
129 
130     // gatt reliable write API use this (manual version of the above)
131     P_W2_PREPARE_WRITE_SINGLE,
132     P_W4_PREPARE_WRITE_SINGLE_RESULT,
133 
134     P_W4_IDENTITY_RESOLVING,
135     P_W4_CMAC_READY,
136     P_W4_CMAC_RESULT,
137     P_W2_SEND_SIGNED_WRITE,
138     P_W4_SEND_SIGNED_WRITE_DONE,
139 
140     P_W2_SDP_QUERY,
141     P_W4_SDP_QUERY,
142     P_W2_L2CAP_CONNECT,
143     P_W4_L2CAP_CONNECTION,
144     P_W2_EMIT_CONNECTED,
145     P_L2CAP_CLOSED,
146 } gatt_client_state_t;
147 
148 
149 typedef enum{
150     SEND_MTU_EXCHANGE,
151     SENT_MTU_EXCHANGE,
152     MTU_EXCHANGED,
153     MTU_AUTO_EXCHANGE_DISABLED
154 } gatt_client_mtu_t;
155 
156 typedef enum {
157     GATT_CLIENT_SERVICE_DISCOVER_W2_SEND,
158     GATT_CLIENT_SERVICE_DISCOVER_W4_DONE,
159     GATT_CLIENT_SERVICE_DISCOVER_CHARACTERISTICS_W2_SEND,
160     GATT_CLIENT_SERVICE_DISCOVER_CHARACTERISTICS_W4_DONE,
161     GATT_CLIENT_SERVICE_SERVICE_CHANGED_WRITE_CCCD_W2_SEND,
162     GATT_CLIENT_SERVICE_SERVICE_CHANGED_WRITE_CCCD_W4_DONE,
163     GATT_CLIENT_SERVICE_DATABASE_HASH_READ_W2_SEND,
164     GATT_CLIENT_SERVICE_DATABASE_HASH_READ_W4_DONE,
165     GATT_CLIENT_SERVICE_DATABASE_HASH_WRITE_CCCD_W2_SEND,
166     GATT_CLIENT_SERVICE_DATABASE_HASH_WRITE_CCCD_W4_DONE,
167     GATT_CLIENT_SERVICE_DONE,
168 } gatt_client_service_state_t;
169 
170 #ifdef ENABLE_GATT_OVER_EATT
171 typedef enum {
172     GATT_CLIENT_EATT_IDLE,
173     GATT_CLIENT_EATT_DISCOVER_GATT_SERVICE_W2_SEND,
174     GATT_CLIENT_EATT_DISCOVER_GATT_SERVICE_W4_DONE,
175     GATT_CLIENT_EATT_READ_SERVER_SUPPORTED_FEATURES_W2_SEND,
176     GATT_CLIENT_EATT_READ_SERVER_SUPPORTED_FEATURES_W4_DONE,
177     GATT_CLIENT_EATT_FIND_CLIENT_SUPPORTED_FEATURES_W2_SEND,
178     GATT_CLIENT_EATT_FIND_CLIENT_SUPPORTED_FEATURES_W4_DONE,
179     GATT_CLIENT_EATT_WRITE_ClIENT_SUPPORTED_FEATURES_W2_SEND,
180     GATT_CLIENT_EATT_WRITE_ClIENT_SUPPORTED_FEATURES_W4_DONE,
181     GATT_CLIENT_EATT_L2CAP_SETUP,
182     GATT_CLIENT_EATT_READY,
183 } gatt_client_eatt_state_t;
184 #endif
185 
186 typedef struct gatt_client{
187     btstack_linked_item_t    item;
188 
189     gatt_client_state_t state;
190 
191     // user callback
192     btstack_packet_handler_t callback;
193 
194     // can write without response callback
195     btstack_packet_handler_t write_without_response_callback;
196 
197     // can write without response requests
198     btstack_linked_list_t write_without_response_requests;
199 
200     // regular gatt query requests
201     btstack_linked_list_t query_requests;
202 
203     hci_con_handle_t con_handle;
204 
205     att_bearer_type_t bearer_type;
206 
207 #if defined(ENABLE_GATT_OVER_CLASSIC) || defined(ENABLE_GATT_OVER_EATT)
208     uint16_t  l2cap_cid;
209     bd_addr_t addr;
210     bd_addr_type_t  addr_type;
211 #endif
212 
213 #ifdef ENABLE_GATT_OVER_CLASSIC
214     uint16_t  l2cap_psm;
215     btstack_context_callback_registration_t callback_request;
216 #endif
217 
218 #ifdef ENABLE_GATT_OVER_EATT
219     gatt_client_eatt_state_t eatt_state;
220     btstack_linked_list_t eatt_clients;
221     uint8_t * eatt_storage_buffer;
222     uint16_t eatt_storage_size;
223     uint8_t  eatt_num_clients;
224     uint8_t  gatt_server_supported_features;
225     uint16_t gatt_client_supported_features_handle;
226 #endif
227 
228     uint16_t          mtu;
229     gatt_client_mtu_t mtu_state;
230 
231     uint16_t uuid16;
232     uint8_t  uuid128[16];
233 
234     uint16_t start_group_handle;
235     uint16_t end_group_handle;
236 
237     uint16_t query_start_handle;
238     uint16_t query_end_handle;
239 
240     uint8_t  characteristic_properties;
241     uint16_t characteristic_start_handle;
242 
243     uint16_t attribute_handle;
244     uint16_t attribute_offset;
245     uint16_t attribute_length;
246     uint8_t* attribute_value;
247 
248     // read multiple characteristic values
249     uint16_t    read_multiple_handle_count;
250     uint16_t  * read_multiple_handles;
251 
252     uint16_t client_characteristic_configuration_handle;
253     uint8_t  client_characteristic_configuration_value[2];
254 
255     bool     filter_with_uuid;
256     bool     send_confirmation;
257 
258     int      le_device_index;
259     uint8_t  cmac[8];
260 
261     btstack_timer_source_t gc_timeout;
262 
263     uint8_t  security_counter;
264     bool     wait_for_authentication_complete;
265     uint8_t  pending_error_code;
266 
267     bool     reencryption_active;
268     uint8_t  reencryption_result;
269 
270     gap_security_level_t security_level;
271 
272     // GATT Service Changes
273     gatt_client_service_state_t gatt_service_state;
274     uint16_t                    gatt_service_start_group_handle;
275     uint16_t                    gatt_service_end_group_handle;
276     // - Service Changed
277     uint16_t                    gatt_service_changed_value_handle;
278     uint16_t                    gatt_service_changed_cccd_handle;
279     uint16_t                    gatt_service_changed_end_handle;
280     // - Database Hash
281     uint16_t                    gatt_service_database_hash_value_handle;
282     uint16_t                    gatt_service_database_hash_cccd_handle;
283     uint16_t                    gatt_service_database_hash_end_handle;
284 
285 } gatt_client_t;
286 
287 typedef struct gatt_client_notification {
288     btstack_linked_item_t    item;
289     btstack_packet_handler_t callback;
290     hci_con_handle_t con_handle;
291     uint16_t attribute_handle;
292 } gatt_client_notification_t;
293 
294 /* API_START */
295 
296 typedef struct {
297     uint16_t start_group_handle;
298     uint16_t end_group_handle;
299     uint16_t uuid16;
300     uint8_t  uuid128[16];
301 } gatt_client_service_t;
302 
303 typedef struct {
304     uint16_t start_handle;
305     uint16_t value_handle;
306     uint16_t end_handle;
307     uint16_t properties;
308     uint16_t uuid16;
309     uint8_t  uuid128[16];
310 } gatt_client_characteristic_t;
311 
312 typedef struct {
313     uint16_t handle;
314     uint16_t uuid16;
315     uint8_t  uuid128[16];
316 } gatt_client_characteristic_descriptor_t;
317 
318 /**
319  * @brief Set up GATT client.
320  */
321 void gatt_client_init(void);
322 
323 /**
324  * @brief Set minimum required security level for GATT Client
325  * @note  The Bluetooth specification makes the GATT Server responsible to check for security.
326  *        This allows an attacker to spoof an existing device with a GATT Servers, but skip the authentication part.
327  *        If your application is exchanging sensitive data with a remote device, you would need to manually check
328  *        the security level before sending/receive such data.
329  *        With level > 0, the GATT Client triggers authentication for all GATT Requests and defers any exchange
330  *        until the required security level is established.
331  *        gatt_client_request_can_write_without_response_event does not trigger authentication
332  *        gatt_client_request_to_write_without_response does not trigger authentication
333  *  @pram level, default LEVEL_0 (no encryption required)
334  */
335 void gatt_client_set_required_security_level(gap_security_level_t level);
336 
337 /**
338  * @brief Connect to remote GATT Server over Classic (BR/EDR) Connection
339  *        GATT_EVENT_CONNECTED with status and con_handle for other API functions
340  *        is emitted on connection complete.
341  * @note requires ENABLE_GATT_OVER_CLASSIC.
342  * @param addr
343  * @return status
344  */
345 uint8_t gatt_client_classic_connect(btstack_packet_handler_t callback, bd_addr_t addr);
346 
347 /**
348  * @brief Disconnect o Classic (BR/EDR) connection to a remote GATT Server
349  * @note requires ENABLE_GATT_OVER_CLASSIC
350  * @param con_handle
351  * @return status
352  */
353 uint8_t gatt_client_classic_disconnect(btstack_packet_handler_t callback, hci_con_handle_t con_handle);
354 
355 /**
356  * @brief Setup Enhanced LE Bearer with up to 5 channels on existing LE connection
357  * @param callback for GATT_EVENT_CONNECTED and GATT_EVENT_DISCONNECTED events
358  * @param con_handle
359  * @param num_channels
360  * @param storage_buffer for L2CAP connection
361  * @param storage_size - each channel requires (2 * ATT MTU) + 10 bytes
362  * @return
363  */
364 uint8_t gatt_client_le_enhanced_connect(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint8_t num_channels, uint8_t * storage_buffer, uint16_t storage_size);
365 
366 /**
367  * @brief MTU is available after the first query has completed. If status is equal to ERROR_CODE_SUCCESS, it returns the real value,
368  * otherwise the default value ATT_DEFAULT_MTU (see bluetooth.h).
369  * @param  con_handle
370  * @param  mtu
371  * @return status ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER                  if no HCI connection for con_handle is found
372  *                BTSTACK_MEMORY_ALLOC_FAILED                               if no GATT client for con_handle could be allocated
373  *                GATT_CLIENT_IN_WRONG_STATE                                if MTU is not exchanged and MTU auto-exchange is disabled
374  *                ERROR_CODE_SUCCESS                                        if query is successfully registered
375  */
376 uint8_t gatt_client_get_mtu(hci_con_handle_t con_handle, uint16_t * mtu);
377 
378 /**
379  * @brief Sets whether a MTU Exchange Request shall be automatically send before the
380  * first attribute read request is send. Default is enabled.
381  * @param enabled
382  */
383 void gatt_client_mtu_enable_auto_negotiation(uint8_t enabled);
384 
385 /**
386  * @brief Sends a MTU Exchange Request, this allows for the client to exchange MTU
387  * when gatt_client_mtu_enable_auto_negotiation is disabled.
388  * @param  callback
389  * @param  con_handle
390  */
391 void gatt_client_send_mtu_negotiation(btstack_packet_handler_t callback, hci_con_handle_t con_handle);
392 
393 /**
394  * @brief Returns 1 if the GATT client is ready to receive a query. It is used with daemon.
395  * @param  con_handle
396  * @return is_ready_status     0 - if no GATT client for con_handle is found, or is not ready, otherwise 1
397  */
398 int gatt_client_is_ready(hci_con_handle_t con_handle);
399 
400 /**
401  * @brief Discovers all primary services.
402  * For each found service a GATT_EVENT_SERVICE_QUERY_RESULT event will be emitted.
403  * The GATT_EVENT_QUERY_COMPLETE event marks the end of discovery.
404  * @param  callback
405  * @param  con_handle
406  * @return status BTSTACK_MEMORY_ALLOC_FAILED, if no GATT client for con_handle is found
407  *                GATT_CLIENT_IN_WRONG_STATE , if GATT client is not ready
408  *                ERROR_CODE_SUCCESS         , if query is successfully registered
409  */
410 uint8_t gatt_client_discover_primary_services(btstack_packet_handler_t callback, hci_con_handle_t con_handle);
411 
412 /**
413  * @brief Discovers all secondary services.
414  * For each found service a GATT_EVENT_SERVICE_QUERY_RESULT event will be emitted.
415  * The GATT_EVENT_QUERY_COMPLETE event marks the end of discovery.
416  * @param  callback
417  * @param  con_handle
418  * @return status BTSTACK_MEMORY_ALLOC_FAILED, if no GATT client for con_handle is found
419  *                GATT_CLIENT_IN_WRONG_STATE , if GATT client is not ready
420  *                ERROR_CODE_SUCCESS         , if query is successfully registered
421  */
422 uint8_t gatt_client_discover_secondary_services(btstack_packet_handler_t callback, hci_con_handle_t con_handle);
423 
424 /**
425  * @brief Discovers a specific primary service given its UUID. This service may exist multiple times.
426  * For each found service a GATT_EVENT_SERVICE_QUERY_RESULT event will be emitted.
427  * The GATT_EVENT_QUERY_COMPLETE event marks the end of discovery.
428  * @param callback
429  * @param con_handle
430  * @param uuid16
431  * @return status BTSTACK_MEMORY_ALLOC_FAILED, if no GATT client for con_handle is found
432  *                GATT_CLIENT_IN_WRONG_STATE , if GATT client is not ready
433  *                ERROR_CODE_SUCCESS         , if query is successfully registered
434  */
435 uint8_t gatt_client_discover_primary_services_by_uuid16(btstack_packet_handler_t callback, hci_con_handle_t con_handle, uint16_t uuid16);
436 
437 /**
438  * @brief Discovers a specific primary service given its UUID. This service may exist multiple times.
439  * For each found service a GATT_EVENT_SERVICE_QUERY_RESULT event will be emitted.
440  * The GATT_EVENT_QUERY_COMPLETE event marks the end of discovery.
441  * @param  callback
442  * @param  con_handle
443  * @param  uuid128
444  * @return status BTSTACK_MEMORY_ALLOC_FAILED, if no GATT client for con_handle is found
445  *                GATT_CLIENT_IN_WRONG_STATE , if GATT client is not ready
446  *                ERROR_CODE_SUCCESS         , if query is successfully registered
447  */
448 uint8_t gatt_client_discover_primary_services_by_uuid128(btstack_packet_handler_t callback, hci_con_handle_t con_handle, const uint8_t * uuid128);
449 
450 /**
451  * @brief Finds included services within the specified service.
452  * For each found included service a GATT_EVENT_INCLUDED_SERVICE_QUERY_RESULT event will be emitted.
453  * The GATT_EVENT_QUERY_COMPLETE event marks the end of discovery.
454  * Information about included service type (primary/secondary) can be retrieved either by sending
455  * an ATT find information request for the returned start group handle
456  * (returning the handle and the UUID for primary or secondary service) or by comparing the service
457  * to the list of all primary services.
458  * @param  callback
459  * @param  con_handle
460  * @param  service
461  * @return status BTSTACK_MEMORY_ALLOC_FAILED, if no GATT client for con_handle is found
462  *                GATT_CLIENT_IN_WRONG_STATE , if GATT client is not ready
463  *                ERROR_CODE_SUCCESS         , if query is successfully registered
464  */
465 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);
466 
467 /**
468  * @brief Discovers all characteristics within the specified service.
469  * For each found characteristic a GATT_EVENT_CHARACTERISTIC_QUERY_RESULT event will be emited.
470  * The GATT_EVENT_QUERY_COMPLETE event marks the end of discovery.
471  * @param  callback
472  * @param  con_handle
473  * @param  service
474  * @return status BTSTACK_MEMORY_ALLOC_FAILED, if no GATT client for con_handle is found
475  *                GATT_CLIENT_IN_WRONG_STATE , if GATT client is not ready
476  *                ERROR_CODE_SUCCESS         , if query is successfully registered
477  */
478 uint8_t gatt_client_discover_characteristics_for_service(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_service_t * service);
479 
480 /**
481  * @brief The following four functions are used to discover all characteristics within
482  * the specified service or handle range, and return those that match the given UUID.
483  *
484  * For each found characteristic a GATT_EVENT_CHARACTERISTIC_QUERY_RESULT event will emitted.
485  * The GATT_EVENT_QUERY_COMPLETE event marks the end of discovery.
486  * @param  callback
487  * @param  con_handle
488  * @param  start_handle
489  * @param  end_handle
490  * @param  uuid16
491  * @return status BTSTACK_MEMORY_ALLOC_FAILED, if no GATT client for con_handle is found
492  *                GATT_CLIENT_IN_WRONG_STATE , if GATT client is not ready
493  *                ERROR_CODE_SUCCESS         , if query is successfully registered
494  */
495 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);
496 
497 /**
498  * @brief The following four functions are used to discover all characteristics within the
499  * specified service or handle range, and return those that match the given UUID.
500  * For each found characteristic a GATT_EVENT_CHARACTERISTIC_QUERY_RESULT event will emitted.
501  * The GATT_EVENT_QUERY_COMPLETE event marks the end of discovery.
502  * @param  callback
503  * @param  con_handle
504  * @param  start_handle
505  * @param  end_handle
506  * @param  uuid128
507  * @return status BTSTACK_MEMORY_ALLOC_FAILED, if no GATT client for con_handle is found
508  *                GATT_CLIENT_IN_WRONG_STATE , if GATT client is not ready
509  *                ERROR_CODE_SUCCESS         , if query is successfully registered
510  */
511 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);
512 
513 /**
514  * @brief The following four functions are used to discover all characteristics within the
515  * specified service or handle range, and return those that match the given UUID.
516  * For each found characteristic a GATT_EVENT_CHARACTERISTIC_QUERY_RESULT event will emitted.
517  * The GATT_EVENT_QUERY_COMPLETE event marks the end of discovery.
518  * @param  callback
519  * @param  con_handle
520  * @param  service
521  * @param  uuid16
522  * @return status BTSTACK_MEMORY_ALLOC_FAILED, if no GATT client for con_handle is found
523  *                GATT_CLIENT_IN_WRONG_STATE , if GATT client is not ready
524  *                ERROR_CODE_SUCCESS         , if query is successfully registered
525  */
526 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);
527 
528 /**
529  * @brief The following four functions are used to discover all characteristics within the
530  * specified service or handle range, and return those that match the given UUID.
531  * For each found characteristic a GATT_EVENT_CHARACTERISTIC_QUERY_RESULT event will emitted.
532  * The GATT_EVENT_QUERY_COMPLETE event marks the end of discovery.
533  * @param  callback
534  * @param  con_handle
535  * @param  service
536  * @param  uuid128
537  * @return status BTSTACK_MEMORY_ALLOC_FAILED, if no GATT client for con_handle is found
538  *                GATT_CLIENT_IN_WRONG_STATE , if GATT client is not ready
539  *                ERROR_CODE_SUCCESS         , if query is successfully registered
540  */
541 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);
542 
543 /**
544  * @brief Discovers attribute handle and UUID of a characteristic descriptor within the specified characteristic.
545  * For each found descriptor a GATT_EVENT_ALL_CHARACTERISTIC_DESCRIPTORS_QUERY_RESULT event will be emitted.
546  *
547  * The GATT_EVENT_QUERY_COMPLETE event marks the end of discovery.
548  * @param  callback
549  * @param  con_handle
550  * @param  characteristic
551  * @return status BTSTACK_MEMORY_ALLOC_FAILED, if no GATT client for con_handle is found
552  *                GATT_CLIENT_IN_WRONG_STATE , if GATT client is not ready
553  *                ERROR_CODE_SUCCESS         , if query is successfully registered
554  */
555 uint8_t gatt_client_discover_characteristic_descriptors(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic);
556 
557 /**
558  * @brief Reads the characteristic value using the characteristic's value handle.
559  * If the characteristic value is found a GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT event will be emitted.
560  * The GATT_EVENT_QUERY_COMPLETE event marks the end of read.
561  * @param  callback
562  * @param  con_handle
563  * @param  characteristic
564  * @return status BTSTACK_MEMORY_ALLOC_FAILED, if no GATT client for con_handle is found
565  *                GATT_CLIENT_IN_WRONG_STATE , if GATT client is not ready
566  *                ERROR_CODE_SUCCESS         , if query is successfully registered
567  */
568 uint8_t gatt_client_read_value_of_characteristic(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_t * characteristic);
569 
570 /**
571  * @brief Reads the characteristic value using the characteristic's value handle.
572  * If the characteristic value is found a GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT event will be emitted.
573  * The GATT_EVENT_QUERY_COMPLETE event marks the end of read.
574  * @param  callback
575  * @param  con_handle
576  * @param  value_handle
577  * @return status BTSTACK_MEMORY_ALLOC_FAILED, if no GATT client for con_handle is found
578  *                GATT_CLIENT_IN_WRONG_STATE , if GATT client is not ready
579  *                ERROR_CODE_SUCCESS         , if query is successfully registered
580  */
581 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);
582 
583 /**
584  * @brief Reads the characteric value of all characteristics with the uuid.
585  * For each characteristic value found a GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT event will be emitted.
586  * The GATT_EVENT_QUERY_COMPLETE event marks the end of read.
587  * @param  callback
588  * @param  con_handle
589  * @param  start_handle
590  * @param  end_handle
591  * @param  uuid16
592  * @return status BTSTACK_MEMORY_ALLOC_FAILED, if no GATT client for con_handle is found
593  *                GATT_CLIENT_IN_WRONG_STATE , if GATT client is not ready
594  *                ERROR_CODE_SUCCESS         , if query is successfully registered
595  */
596 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);
597 
598 /**
599  * @brief Reads the characteric value of all characteristics with the uuid.
600  * For each characteristic value found a GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT event will be emitted.
601  * The GATT_EVENT_QUERY_COMPLETE event marks the end of read.
602  * @param  callback
603  * @param  con_handle
604  * @param  start_handle
605  * @param  end_handle
606  * @param  uuid128
607  * @return status BTSTACK_MEMORY_ALLOC_FAILED, if no GATT client for con_handle is found
608  *                GATT_CLIENT_IN_WRONG_STATE , if GATT client is not ready
609  *                ERROR_CODE_SUCCESS         , if query is successfully registered
610  */
611 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);
612 
613 /**
614  * @brief Reads the long characteristic value using the characteristic's value handle.
615  * The value will be returned in several blobs.
616  * For each blob, a GATT_EVENT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT event with updated value offset will be emitted.
617  * The GATT_EVENT_QUERY_COMPLETE event marks the end of read.
618  * @param  callback
619  * @param  con_handle
620  * @param  characteristic
621  * @return status BTSTACK_MEMORY_ALLOC_FAILED, if no GATT client for con_handle is found
622  *                GATT_CLIENT_IN_WRONG_STATE , if GATT client is not ready
623  *                ERROR_CODE_SUCCESS         , if query is successfully registered
624  */
625 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);
626 
627 /**
628  * @brief Reads the long characteristic value using the characteristic's value handle.
629  * The value will be returned in several blobs.
630  * For each blob, a GATT_EVENT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT event with updated value offset will be emitted.
631  * The GATT_EVENT_QUERY_COMPLETE event marks the end of read.
632  * @param  callback
633  * @param  con_handle
634  * @param  value_handle
635  * @return status BTSTACK_MEMORY_ALLOC_FAILED, if no GATT client for con_handle is found
636  *                GATT_CLIENT_IN_WRONG_STATE , if GATT client is not ready
637  *                ERROR_CODE_SUCCESS         , if query is successfully registered
638  */
639 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);
640 
641 /**
642  * @brief Reads the long characteristic value using the characteristic's value handle.
643  * The value will be returned in several blobs.
644  * For each blob, a GATT_EVENT_LONG_CHARACTERISTIC_VALUE_QUERY_RESULT event with updated value offset will be emitted.
645  * The GATT_EVENT_QUERY_COMPLETE event marks the end of read.
646  * @param  callback
647  * @param  con_handle
648  * @param  value_handle
649  * @param  offset
650  * @return status BTSTACK_MEMORY_ALLOC_FAILED, if no GATT client for con_handle is found
651  *                GATT_CLIENT_IN_WRONG_STATE , if GATT client is not ready
652  *                ERROR_CODE_SUCCESS         , if query is successfully registered
653  */
654 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);
655 
656 /*
657  * @brief Read multiple characteristic values.
658  * The all results are emitted via single GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT event,
659  * followed by the GATT_EVENT_QUERY_COMPLETE event, which marks the end of read.
660  * @param  callback
661  * @param  con_handle
662  * @param  num_value_handles
663  * @param  value_handles list of handles
664  * @return status BTSTACK_MEMORY_ALLOC_FAILED, if no GATT client for con_handle is found
665  *                GATT_CLIENT_IN_WRONG_STATE , if GATT client is not ready
666  *                ERROR_CODE_SUCCESS         , if query is successfully registered
667  */
668 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);
669 
670 /*
671  * @brief Read multiple varaible characteristic values. Only supported over LE Enhanced Bearer
672  * The all results are emitted via single GATT_EVENT_CHARACTERISTIC_VALUE_QUERY_RESULT event,
673  * followed by the GATT_EVENT_QUERY_COMPLETE event, which marks the end of read.
674  * @param  callback
675  * @param  con_handle
676  * @param  num_value_handles
677  * @param  value_handles list of handles
678  * @return status BTSTACK_MEMORY_ALLOC_FAILED, if no GATT client for con_handle is found
679  *                GATT_CLIENT_IN_WRONG_STATE , if GATT client is not ready
680  *                ERROR_CODE_SUCCESS         , if query is successfully registered
681  */
682 uint8_t gatt_client_read_multiple_variable_characteristic_values(btstack_packet_handler_t callback, hci_con_handle_t con_handle, int num_value_handles, uint16_t * value_handles);
683 
684 /**
685  * @brief Writes the characteristic value using the characteristic's value handle without
686  * an acknowledgment that the write was successfully performed.
687  * @param  con_handle
688  * @param  value_handle
689  * @param  value_length
690  * @param  value is copied on success and does not need to be retained
691  * @return status BTSTACK_MEMORY_ALLOC_FAILED, if no GATT client for con_handle is found
692  *                GATT_CLIENT_IN_WRONG_STATE , if GATT client is not ready
693  *                BTSTACK_ACL_BUFFERS_FULL   , if L2CAP cannot send, there are no free ACL slots
694  *                ERROR_CODE_SUCCESS         , if query is successfully registered
695  */
696 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);
697 
698 /**
699  * @brief Writes the authenticated characteristic value using the characteristic's value handle
700  * without an acknowledgment that the write was successfully performed.
701  * @note GATT_EVENT_QUERY_COMPLETE is emitted with ATT_ERROR_SUCCESS for success,
702  * or ATT_ERROR_BONDING_INFORMATION_MISSING if there is no bonding information stored.
703  * @param  callback
704  * @param  con_handle
705  * @param  value_handle
706  * @param  message_len
707  * @param  message is not copied, make sure memory is accessible until write is done
708  * @return status ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER                  if no HCI connection for con_handle is found
709  *                BTSTACK_MEMORY_ALLOC_FAILED                               if no GATT client for con_handle could be allocated
710  *                GATT_CLIENT_IN_WRONG_STATE                                if GATT client is not ready
711  *                ERROR_CODE_SUCCESS                                        if query is successfully registered
712  */
713 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);
714 
715 /**
716  * @brief Writes the characteristic value using the characteristic's value handle.
717  * The GATT_EVENT_QUERY_COMPLETE event marks the end of write.
718  * The write is successfully performed, if the event's att_status field is set to
719  * ATT_ERROR_SUCCESS (see bluetooth.h for ATT_ERROR codes).
720  * @param  callback
721  * @param  con_handle
722  * @param  value_handle
723  * @param  value_length
724  * @param  value is not copied, make sure memory is accessible until write is done, i.e. GATT_EVENT_QUERY_COMPLETE is received
725  * @return status ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER                  if no HCI connection for con_handle is found
726  *                BTSTACK_MEMORY_ALLOC_FAILED                               if no GATT client for con_handle could be allocated
727  *                GATT_CLIENT_IN_WRONG_STATE                                if GATT client is not ready
728  *                ERROR_CODE_SUCCESS                                        if query is successfully registered
729  */
730 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);
731 
732 /**
733  * @brief Writes the characteristic value using the characteristic's value handle.
734  * The GATT_EVENT_QUERY_COMPLETE event marks the end of write.
735  * The write is successfully performed if the event's att_status field is set to ATT_ERROR_SUCCESS (see bluetooth.h for ATT_ERROR codes).
736  * @param  callback
737  * @param  con_handle
738  * @param  value_handle
739  * @param  value_length
740  * @param  value is not copied, make sure memory is accessible until write is done, i.e. GATT_EVENT_QUERY_COMPLETE is received
741  * @return status ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER                  if no HCI connection for con_handle is found
742  *                BTSTACK_MEMORY_ALLOC_FAILED                               if no GATT client for con_handle could be allocated
743  *                GATT_CLIENT_IN_WRONG_STATE                                if GATT client is not ready
744  *                ERROR_CODE_SUCCESS                                        if query is successfully registered
745  */
746 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);
747 
748 /**
749  * @brief Writes the characteristic value using the characteristic's value handle.
750  * The GATT_EVENT_QUERY_COMPLETE event marks the end of write.
751  * The write is successfully performed if the event's att_status field is set to ATT_ERROR_SUCCESS (see bluetooth.h for ATT_ERROR codes).
752  * @param  callback
753  * @param  con_handle
754  * @param  value_handle
755  * @param  offset of value
756  * @param  value_length
757  * @param  value is not copied, make sure memory is accessible until write is done, i.e. GATT_EVENT_QUERY_COMPLETE is received
758  * @return status ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER                  if no HCI connection for con_handle is found
759  *                BTSTACK_MEMORY_ALLOC_FAILED                               if no GATT client for con_handle could be allocated
760  *                GATT_CLIENT_IN_WRONG_STATE                                if GATT client is not ready
761  *                ERROR_CODE_SUCCESS                                        if query is successfully registered
762  */
763 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);
764 
765 /**
766  * @brief Writes of the long characteristic value using the characteristic's value handle.
767  * It uses server response to validate that the write was correctly received.
768  * The GATT_EVENT_QUERY_COMPLETE EVENT marks the end of write.
769  * The write is successfully performed, if the event's att_status field is set to ATT_ERROR_SUCCESS (see bluetooth.h for ATT_ERROR codes).
770  * @param  callback
771  * @param  con_handle
772  * @param  value_handle
773  * @param  value_length
774  * @param  value is not copied, make sure memory is accessible until write is done, i.e. GATT_EVENT_QUERY_COMPLETE is received
775  * @return status ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER                  if no HCI connection for con_handle is found
776  *                BTSTACK_MEMORY_ALLOC_FAILED                               if no GATT client for con_handle could be allocated
777  *                GATT_CLIENT_IN_WRONG_STATE                                if GATT client is not ready
778  *                ERROR_CODE_SUCCESS                                        if query is successfully registered
779  */
780 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);
781 
782 /**
783  * @brief Reads the characteristic descriptor using its handle.
784  * If the characteristic descriptor is found, a GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT event will be emitted.
785  * The GATT_EVENT_QUERY_COMPLETE event marks the end of read.
786  * @param  callback
787  * @param  con_handle
788  * @param  descriptor
789  * @return status ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER                  if no HCI connection for con_handle is found
790  *                BTSTACK_MEMORY_ALLOC_FAILED                               if no GATT client for con_handle could be allocated
791  *                GATT_CLIENT_IN_WRONG_STATE                                if GATT client is not ready
792  *                ERROR_CODE_SUCCESS                                        if query is successfully registered
793  */
794 uint8_t gatt_client_read_characteristic_descriptor(btstack_packet_handler_t callback, hci_con_handle_t con_handle, gatt_client_characteristic_descriptor_t * descriptor);
795 
796 /**
797  * @brief Reads the characteristic descriptor using its handle.
798  * If the characteristic descriptor is found, a GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT event will be emitted.
799  * The GATT_EVENT_QUERY_COMPLETE event marks the end of read.
800  * @param  callback
801  * @param  con_handle
802  * @param  descriptor
803  * @return status ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER                  if no HCI connection for con_handle is found
804  *                BTSTACK_MEMORY_ALLOC_FAILED                               if no GATT client for con_handle could be allocated
805  *                GATT_CLIENT_IN_WRONG_STATE                                if GATT client is not ready
806  *                ERROR_CODE_SUCCESS                                        if query is successfully registered
807  */
808 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);
809 
810 /**
811  * @brief Reads the long characteristic descriptor using its handle. It will be returned in several blobs.
812  * For each blob, a GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT event will be emitted.
813  * The GATT_EVENT_QUERY_COMPLETE event marks the end of read.
814  * @param  callback
815  * @param  con_handle
816  * @param  descriptor
817  * @return status ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER                  if no HCI connection for con_handle is found
818  *                BTSTACK_MEMORY_ALLOC_FAILED                               if no GATT client for con_handle could be allocated
819  *                GATT_CLIENT_IN_WRONG_STATE                                if GATT client is not ready
820  *                ERROR_CODE_SUCCESS                                        if query is successfully registered
821  */
822 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);
823 
824 /**
825  * @brief Reads the long characteristic descriptor using its handle. It will be returned in several blobs.
826  * For each blob, a GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT event will be emitted.
827  * The GATT_EVENT_QUERY_COMPLETE event marks the end of read.
828  * @param  callback
829  * @param  con_handle
830  * @param  descriptor_handle
831  * @return status ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER                  if no HCI connection for con_handle is found
832  *                BTSTACK_MEMORY_ALLOC_FAILED                               if no GATT client for con_handle could be allocated
833  *                GATT_CLIENT_IN_WRONG_STATE                                if GATT client is not ready
834  *                ERROR_CODE_SUCCESS                                        if query is successfully registered
835  */
836 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);
837 
838 /**
839  * @brief Reads the long characteristic descriptor using its handle. It will be returned in several blobs.
840  * For each blob, a GATT_EVENT_CHARACTERISTIC_DESCRIPTOR_QUERY_RESULT event will be emitted.
841  * The GATT_EVENT_QUERY_COMPLETE event marks the end of read.
842  * @param  callback
843  * @param  con_handle
844  * @param  descriptor_handle
845  * @param  offset
846  * @return status ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER                  if no HCI connection for con_handle is found
847  *                BTSTACK_MEMORY_ALLOC_FAILED                               if no GATT client for con_handle could be allocated
848  *                GATT_CLIENT_IN_WRONG_STATE                                if GATT client is not ready
849  *                ERROR_CODE_SUCCESS                                        if query is successfully registered
850  */
851 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);
852 
853 /**
854  * @brief Writes the characteristic descriptor using its handle.
855  * The GATT_EVENT_QUERY_COMPLETE event marks the end of write.
856  * The write is successfully performed if the event's att_status field is set to ATT_ERROR_SUCCESS (see bluetooth.h for ATT_ERROR codes).
857  * @param  callback
858  * @param  con_handle
859  * @param  descriptor
860  * @param  value_length
861  * @param  value is not copied, make sure memory is accessible until write is done, i.e. GATT_EVENT_QUERY_COMPLETE is received
862  * @return status ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER                  if no HCI connection for con_handle is found
863  *                BTSTACK_MEMORY_ALLOC_FAILED                               if no GATT client for con_handle could be allocated
864  *                GATT_CLIENT_IN_WRONG_STATE                                if GATT client is not ready
865  *                ERROR_CODE_SUCCESS                                        if query is successfully registered
866  */
867 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);
868 
869 /**
870  * @brief Writes the characteristic descriptor using its handle.
871  * The GATT_EVENT_QUERY_COMPLETE event marks the end of write.
872  * The write is successfully performed if the event's att_status field is set to ATT_ERROR_SUCCESS (see bluetooth.h for ATT_ERROR codes).
873  * @param  callback
874  * @param  con_handle
875  * @param  descriptor_handle
876  * @param  value_length
877  * @param  value is not copied, make sure memory is accessible until write is done, i.e. GATT_EVENT_QUERY_COMPLETE is received
878  * @return status ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER                  if no HCI connection for con_handle is found
879  *                BTSTACK_MEMORY_ALLOC_FAILED                               if no GATT client for con_handle could be allocated
880  *                GATT_CLIENT_IN_WRONG_STATE                                if GATT client is not ready
881  *                ERROR_CODE_SUCCESS                                        if query is successfully registered
882  */
883 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);
884 
885 /**
886  * @brief Writes the characteristic descriptor using its handle.
887  * The GATT_EVENT_QUERY_COMPLETE event marks the end of write.
888  * The write is successfully performed if the event's att_status field is set to ATT_ERROR_SUCCESS (see bluetooth.h for ATT_ERROR codes).
889  * @param  callback
890  * @param  con_handle
891  * @param  descriptor
892  * @param  value_length
893  * @param  value is not copied, make sure memory is accessible until write is done, i.e. GATT_EVENT_QUERY_COMPLETE is received
894  * @return status ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER                  if no HCI connection for con_handle is found
895  *                BTSTACK_MEMORY_ALLOC_FAILED                               if no GATT client for con_handle could be allocated
896  *                GATT_CLIENT_IN_WRONG_STATE                                if GATT client is not ready
897  *                ERROR_CODE_SUCCESS                                        if query is successfully registered
898  */
899 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);
900 
901 /**
902  * @brief Writes the characteristic descriptor using its handle.
903  * The GATT_EVENT_QUERY_COMPLETE event marks the end of write.
904  * The write is successfully performed if the event's att_status field is set to ATT_ERROR_SUCCESS (see bluetooth.h for ATT_ERROR codes).
905  * @param  callback
906  * @param  con_handle
907  * @param  descriptor_handle
908  * @param  value_length
909  * @param  value is not copied, make sure memory is accessible until write is done, i.e. GATT_EVENT_QUERY_COMPLETE is received
910  * @return status ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER                  if no HCI connection for con_handle is found
911  *                BTSTACK_MEMORY_ALLOC_FAILED                               if no GATT client for con_handle could be allocated
912  *                GATT_CLIENT_IN_WRONG_STATE                                if GATT client is not ready
913  *                ERROR_CODE_SUCCESS                                        if query is successfully registered
914  */
915 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);
916 
917 /**
918  * @brief Writes the characteristic descriptor using its handle.
919  * The GATT_EVENT_QUERY_COMPLETE event marks the end of write.
920  * The write is successfully performed if the event's att_status field is set to ATT_ERROR_SUCCESS (see bluetooth.h for ATT_ERROR codes).
921  * @param  callback
922  * @param  con_handle
923  * @param  descriptor_handle
924  * @param  offset of value
925  * @param  value_length
926  * @param  value is not copied, make sure memory is accessible until write is done, i.e. GATT_EVENT_QUERY_COMPLETE is received
927  * @return status ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER                  if no HCI connection for con_handle is found
928  *                BTSTACK_MEMORY_ALLOC_FAILED                               if no GATT client for con_handle could be allocated
929  *                GATT_CLIENT_IN_WRONG_STATE                                if GATT client is not ready
930  *                ERROR_CODE_SUCCESS                                        if query is successfully registered
931  */
932 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);
933 
934 /**
935  * @brief Writes the client characteristic configuration of the specified characteristic.
936  * It is used to subscribe for notifications or indications of the characteristic value.
937  * For notifications or indications specify: GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION
938  * resp. GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_INDICATION as configuration value.
939  * The GATT_EVENT_QUERY_COMPLETE event marks the end of write.
940  * The write is successfully performed if the event's att_status field is set to ATT_ERROR_SUCCESS (see bluetooth.h for ATT_ERROR codes).
941  * @param  callback
942  * @param  con_handle
943  * @param  characteristic
944  * @param  configuration                                                    GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_NOTIFICATION, GATT_CLIENT_CHARACTERISTICS_CONFIGURATION_INDICATION
945  * @return status ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER                  if no HCI connection for con_handle is found
946  *                BTSTACK_MEMORY_ALLOC_FAILED                               if no GATT client for con_handle could be allocated
947  *                GATT_CLIENT_IN_WRONG_STATE                                if GATT client is not ready
948  *                GATT_CLIENT_CHARACTERISTIC_NOTIFICATION_NOT_SUPPORTED     if configuring notification, but characteristic has no notification property set
949  *                GATT_CLIENT_CHARACTERISTIC_INDICATION_NOT_SUPPORTED       if configuring indication, but characteristic has no indication property set
950  *                ERROR_CODE_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE         if configuration is invalid
951  *                ERROR_CODE_SUCCESS                                        if query is successfully registered
952  */
953 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);
954 
955 /**
956  * @brief Register for changes to the Service Changed and Database Hash Characteristics of the remote GATT Service
957  * *
958  * When configured, GATT_EVENT_QUERY_COMPLETE event is emitted
959  * If supported, the Database Hash is read as well
960  *
961  * Requires ENABLE_GATT_CLIENT_SERVICE_CHANGED
962  *
963  * @param callback
964  */
965 void gatt_client_add_service_changed_handler(btstack_packet_callback_registration_t * callback);
966 
967 /**
968  * @brief Remove callback for service changes
969  *
970  * Requires ENABLE_GATT_CLIENT_SERVICE_CHANGED
971  *
972  * @param callback
973  */
974 void gatt_client_remove_service_changed_handler(btstack_packet_callback_registration_t * callback);
975 
976 /**
977  * @brief Register for notifications and indications of a characteristic enabled by
978  * the gatt_client_write_client_characteristic_configuration function.
979  * @param notification struct used to store registration
980  * @param callback
981  * @param con_handle or GATT_CLIENT_ANY_CONNECTION to receive updates from all connected devices
982  * @param characteristic or NULL to receive updates for all characteristics
983  */
984 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);
985 
986 /**
987  * @brief Stop listening to characteristic value updates registered with
988  * the gatt_client_listen_for_characteristic_value_updates function.
989  * @param notification struct used in gatt_client_listen_for_characteristic_value_updates
990  */
991 void gatt_client_stop_listening_for_characteristic_value_updates(gatt_client_notification_t * notification);
992 
993 /**
994  * @brief Transactional write. It can be called as many times as it is needed to write the characteristics within the same transaction.
995  * Call the gatt_client_execute_write function to commit the transaction.
996  * @param  callback
997  * @param  con_handle
998  * @param  attribute_handle
999  * @param  offset of value
1000  * @param  value_length
1001  * @param  value is not copied, make sure memory is accessible until write is done, i.e. GATT_EVENT_QUERY_COMPLETE is received
1002  */
1003 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);
1004 
1005 /**
1006  * @brief Commit transactional write. GATT_EVENT_QUERY_COMPLETE is received.
1007  * @param  callback
1008  * @param  con_handle
1009  * @return status
1010  */
1011 uint8_t gatt_client_execute_write(btstack_packet_handler_t callback, hci_con_handle_t con_handle);
1012 
1013 /**
1014  * @brief Abort transactional write. GATT_EVENT_QUERY_COMPLETE is received.
1015  * @param  callback
1016  * @param  con_handle
1017  * @return status
1018  */
1019 uint8_t gatt_client_cancel_write(btstack_packet_handler_t callback, hci_con_handle_t con_handle);
1020 
1021 /**
1022  * @brief Request callback when regular gatt query can be sent
1023  * @note callback might happen during call to this function
1024  * @param callback_registration to point to callback function and context information
1025  * @param con_handle
1026  * @return ERROR_CODE_SUCCESS if ok, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER if handle unknown, and ERROR_CODE_COMMAND_DISALLOWED if callback already registered
1027  */
1028 uint8_t gatt_client_request_to_send_gatt_query(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle);
1029 
1030 /**
1031  * @brief Remove queued callback for regular gatt queries, to be used on disconnect for example
1032  * @param callback_registration
1033  * @param con_handle
1034  * @return ERROR_CODE_SUCCESS if ok
1035  */
1036 uint8_t gatt_client_remove_gatt_query(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle);
1037 
1038 /**
1039  * @brief Request callback when writing characteristic value without response is possible
1040  * @note callback might happen during call to this function
1041  * @param callback_registration to point to callback function and context information
1042  * @param con_handle
1043  * @return ERROR_CODE_SUCCESS if ok, ERROR_CODE_UNKNOWN_CONNECTION_IDENTIFIER if handle unknown, and ERROR_CODE_COMMAND_DISALLOWED if callback already registered
1044  */
1045 uint8_t gatt_client_request_to_write_without_response(btstack_context_callback_registration_t * callback_registration, hci_con_handle_t con_handle);
1046 
1047 
1048 // the following functions are marked as deprecated and will be removed eventually
1049 /**
1050  * @brief Requests GATT_EVENT_CAN_WRITE_WITHOUT_RESPONSE that guarantees
1051  * a single successful gatt_client_write_value_of_characteristic_without_response call.
1052  * @deprecated please use gatt_client_request_to_write_without_response instead
1053  * @param  callback
1054  * @param  con_handle
1055  * @return status
1056  */
1057 uint8_t gatt_client_request_can_write_without_response_event(btstack_packet_handler_t callback, hci_con_handle_t con_handle);
1058 
1059 
1060 /* API_END */
1061 
1062 // used by generated btstack_event.c
1063 
1064 void gatt_client_deserialize_service(const uint8_t *packet, int offset, gatt_client_service_t * service);
1065 void gatt_client_deserialize_characteristic(const uint8_t * packet, int offset, gatt_client_characteristic_t * characteristic);
1066 void gatt_client_deserialize_characteristic_descriptor(const uint8_t * packet, int offset, gatt_client_characteristic_descriptor_t * descriptor);
1067 
1068 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1069 void gatt_client_att_packet_handler_fuzz(uint8_t packet_type, uint16_t handle, uint8_t *packet, uint16_t size);
1070 uint8_t gatt_client_get_client(hci_con_handle_t con_handle, gatt_client_t ** gatt_client);
1071 #endif
1072 
1073 #if defined __cplusplus
1074 }
1075 #endif
1076 
1077 #endif
1078