1 /** @file
2 * @brief Generic Attribute Profile handling.
3 */
4
5 /*
6 * Copyright (c) 2015-2016 Intel Corporation
7 *
8 * SPDX-License-Identifier: Apache-2.0
9 */
10 #ifndef ZEPHYR_INCLUDE_BLUETOOTH_GATT_H_
11 #define ZEPHYR_INCLUDE_BLUETOOTH_GATT_H_
12
13 /**
14 * @brief Generic Attribute Profile (GATT)
15 * @defgroup bt_gatt Generic Attribute Profile (GATT)
16 * @ingroup bluetooth
17 * @{
18 */
19
20 #include <stdint.h>
21 #include <stddef.h>
22
23 #include <sys/types.h>
24
25 #include <zephyr/sys/slist.h>
26 #include <zephyr/sys/util.h>
27 #include <zephyr/bluetooth/conn.h>
28 #include <zephyr/bluetooth/uuid.h>
29 #include <zephyr/bluetooth/att.h>
30 #include <zephyr/sys/iterable_sections.h>
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 /** GATT attribute permission bit field values */
37 enum bt_gatt_perm {
38 /** No operations supported, e.g. for notify-only */
39 BT_GATT_PERM_NONE = 0,
40
41 /** Attribute read permission. */
42 BT_GATT_PERM_READ = BIT(0),
43
44 /** Attribute write permission. */
45 BT_GATT_PERM_WRITE = BIT(1),
46
47 /** @brief Attribute read permission with encryption.
48 *
49 * If set, requires encryption for read access.
50 */
51 BT_GATT_PERM_READ_ENCRYPT = BIT(2),
52
53 /** @brief Attribute write permission with encryption.
54 *
55 * If set, requires encryption for write access.
56 */
57 BT_GATT_PERM_WRITE_ENCRYPT = BIT(3),
58
59 /** @brief Attribute read permission with authentication.
60 *
61 * If set, requires encryption using authenticated link-key for read
62 * access.
63 */
64 BT_GATT_PERM_READ_AUTHEN = BIT(4),
65
66 /** @brief Attribute write permission with authentication.
67 *
68 * If set, requires encryption using authenticated link-key for write
69 * access.
70 */
71 BT_GATT_PERM_WRITE_AUTHEN = BIT(5),
72
73 /** @brief Attribute prepare write permission.
74 *
75 * If set, allows prepare writes with use of BT_GATT_WRITE_FLAG_PREPARE
76 * passed to write callback.
77 */
78 BT_GATT_PERM_PREPARE_WRITE = BIT(6),
79
80 /** @brief Attribute read permission with LE Secure Connection encryption.
81 *
82 * If set, requires that LE Secure Connections is used for read access.
83 */
84 BT_GATT_PERM_READ_LESC = BIT(7),
85
86 /** @brief Attribute write permission with LE Secure Connection encryption.
87 *
88 * If set, requires that LE Secure Connections is used for write access.
89 */
90 BT_GATT_PERM_WRITE_LESC = BIT(8),
91 };
92
93 /**
94 * @brief Construct error return value for attribute read and write callbacks.
95 *
96 * @param _att_err ATT error code
97 *
98 * @return Appropriate error code for the attribute callbacks.
99 */
100 #define BT_GATT_ERR(_att_err) (-(_att_err))
101
102 /** GATT attribute write flags */
103 enum {
104 /** @brief Attribute prepare write flag
105 *
106 * If set, write callback should only check if the device is
107 * authorized but no data shall be written.
108 */
109 BT_GATT_WRITE_FLAG_PREPARE = BIT(0),
110
111 /** @brief Attribute write command flag
112 *
113 * If set, indicates that write operation is a command (Write without
114 * response) which doesn't generate any response.
115 */
116 BT_GATT_WRITE_FLAG_CMD = BIT(1),
117
118 /** @brief Attribute write execute flag
119 *
120 * If set, indicates that write operation is a execute, which indicates
121 * the end of a long write, and will come after 1 or more
122 * @ref BT_GATT_WRITE_FLAG_PREPARE.
123 */
124 BT_GATT_WRITE_FLAG_EXECUTE = BIT(2),
125 };
126
127 /* Forward declaration of GATT Attribute structure */
128 struct bt_gatt_attr;
129
130 /** @typedef bt_gatt_attr_read_func_t
131 * @brief Attribute read callback
132 *
133 * The callback can also be used locally to read the contents of the
134 * attribute in which case no connection will be set.
135 *
136 * @param conn The connection that is requesting to read
137 * @param attr The attribute that's being read
138 * @param buf Buffer to place the read result in
139 * @param len Length of data to read
140 * @param offset Offset to start reading from
141 *
142 * @return Number of bytes read, or in case of an error
143 * BT_GATT_ERR() with a specific BT_ATT_ERR_* error code.
144 */
145 typedef ssize_t (*bt_gatt_attr_read_func_t)(struct bt_conn *conn,
146 const struct bt_gatt_attr *attr,
147 void *buf, uint16_t len,
148 uint16_t offset);
149
150 /** @typedef bt_gatt_attr_write_func_t
151 * @brief Attribute write callback
152 *
153 * @param conn The connection that is requesting to write
154 * @param attr The attribute that's being written
155 * @param buf Buffer with the data to write
156 * @param len Number of bytes in the buffer
157 * @param offset Offset to start writing from
158 * @param flags Flags (BT_GATT_WRITE_FLAG_*)
159 *
160 * @return Number of bytes written, or in case of an error
161 * BT_GATT_ERR() with a specific BT_ATT_ERR_* error code.
162 */
163 typedef ssize_t (*bt_gatt_attr_write_func_t)(struct bt_conn *conn,
164 const struct bt_gatt_attr *attr,
165 const void *buf, uint16_t len,
166 uint16_t offset, uint8_t flags);
167
168 /** @brief GATT Attribute structure. */
169 struct bt_gatt_attr {
170 /** Attribute UUID */
171 const struct bt_uuid *uuid;
172 bt_gatt_attr_read_func_t read;
173 /** Attribute write callback */
174 bt_gatt_attr_write_func_t write;
175 /** Attribute user data */
176 void *user_data;
177 /** Attribute handle */
178 uint16_t handle;
179 /** @brief Attribute permissions.
180 *
181 * Will be 0 if returned from bt_gatt_discover().
182 */
183 uint16_t perm;
184 };
185
186 /** @brief GATT Service structure */
187 struct bt_gatt_service_static {
188 /** Service Attributes */
189 const struct bt_gatt_attr *attrs;
190 /** Service Attribute count */
191 size_t attr_count;
192 };
193
194 /** @brief GATT Service structure */
195 struct bt_gatt_service {
196 /** Service Attributes */
197 struct bt_gatt_attr *attrs;
198 /** Service Attribute count */
199 size_t attr_count;
200
201 sys_snode_t node;
202 };
203
204 /** @brief Service Attribute Value. */
205 struct bt_gatt_service_val {
206 /** Service UUID. */
207 const struct bt_uuid *uuid;
208 /** Service end handle. */
209 uint16_t end_handle;
210 };
211
212 /** @brief Include Attribute Value. */
213 struct bt_gatt_include {
214 /** Service UUID. */
215 const struct bt_uuid *uuid;
216 /** Service start handle. */
217 uint16_t start_handle;
218 /** Service end handle. */
219 uint16_t end_handle;
220 };
221
222 /** @brief GATT callback structure. */
223 struct bt_gatt_cb {
224 /** @brief The maximum ATT MTU on a connection has changed.
225 *
226 * This callback notifies the application that the maximum TX or RX
227 * ATT MTU has increased.
228 *
229 * @param conn Connection object.
230 * @param tx Updated TX ATT MTU.
231 * @param rx Updated RX ATT MTU.
232 */
233 void (*att_mtu_updated)(struct bt_conn *conn, uint16_t tx, uint16_t rx);
234
235 sys_snode_t node;
236 };
237
238 /** Characteristic Properties Bit field values */
239
240 /**
241 * @brief Characteristic broadcast property.
242 *
243 * If set, permits broadcasts of the Characteristic Value using Server
244 * Characteristic Configuration Descriptor.
245 */
246 #define BT_GATT_CHRC_BROADCAST 0x01
247 /**
248 * @brief Characteristic read property.
249 *
250 * If set, permits reads of the Characteristic Value.
251 */
252 #define BT_GATT_CHRC_READ 0x02
253 /**
254 * @brief Characteristic write without response property.
255 *
256 * If set, permits write of the Characteristic Value without response.
257 */
258 #define BT_GATT_CHRC_WRITE_WITHOUT_RESP 0x04
259 /**
260 * @brief Characteristic write with response property.
261 *
262 * If set, permits write of the Characteristic Value with response.
263 */
264 #define BT_GATT_CHRC_WRITE 0x08
265 /**
266 * @brief Characteristic notify property.
267 *
268 * If set, permits notifications of a Characteristic Value without
269 * acknowledgment.
270 */
271 #define BT_GATT_CHRC_NOTIFY 0x10
272 /**
273 * @brief Characteristic indicate property.
274 *
275 * If set, permits indications of a Characteristic Value with acknowledgment.
276 */
277 #define BT_GATT_CHRC_INDICATE 0x20
278 /**
279 * @brief Characteristic Authenticated Signed Writes property.
280 *
281 * If set, permits signed writes to the Characteristic Value.
282 */
283 #define BT_GATT_CHRC_AUTH 0x40
284 /**
285 * @brief Characteristic Extended Properties property.
286 *
287 * If set, additional characteristic properties are defined in the
288 * Characteristic Extended Properties Descriptor.
289 */
290 #define BT_GATT_CHRC_EXT_PROP 0x80
291
292 /** @brief Characteristic Attribute Value. */
293 struct bt_gatt_chrc {
294 /** Characteristic UUID. */
295 const struct bt_uuid *uuid;
296 /** Characteristic Value handle. */
297 uint16_t value_handle;
298 /** Characteristic properties. */
299 uint8_t properties;
300 };
301
302 /** Characteristic Extended Properties Bit field values */
303 #define BT_GATT_CEP_RELIABLE_WRITE 0x0001
304 #define BT_GATT_CEP_WRITABLE_AUX 0x0002
305
306 /** @brief Characteristic Extended Properties Attribute Value. */
307 struct bt_gatt_cep {
308 /** Characteristic Extended properties */
309 uint16_t properties;
310 };
311
312 /** Client Characteristic Configuration Values */
313
314 /**
315 * @brief Client Characteristic Configuration Notification.
316 *
317 * If set, changes to Characteristic Value shall be notified.
318 */
319 #define BT_GATT_CCC_NOTIFY 0x0001
320 /**
321 * @brief Client Characteristic Configuration Indication.
322 *
323 * If set, changes to Characteristic Value shall be indicated.
324 */
325 #define BT_GATT_CCC_INDICATE 0x0002
326
327 /** Client Characteristic Configuration Attribute Value */
328 struct bt_gatt_ccc {
329 /** Client Characteristic Configuration flags */
330 uint16_t flags;
331 };
332
333 /** Server Characteristic Configuration Values */
334
335 /**
336 * @brief Server Characteristic Configuration Broadcast
337 *
338 * If set, the characteristic value shall be broadcast in the advertising data
339 * when the server is advertising.
340 */
341 #define BT_GATT_SCC_BROADCAST 0x0001
342
343 /** Server Characteristic Configuration Attribute Value */
344 struct bt_gatt_scc {
345 /** Server Characteristic Configuration flags */
346 uint16_t flags;
347 };
348
349 /** @brief GATT Characteristic Presentation Format Attribute Value. */
350 struct bt_gatt_cpf {
351 /** Format of the value of the characteristic */
352 uint8_t format;
353 /** Exponent field to determine how the value of this characteristic is
354 * further formatted
355 */
356 int8_t exponent;
357 /** Unit of the characteristic */
358 uint16_t unit;
359 /** Name space of the description */
360 uint8_t name_space;
361 /** Description of the characteristic as defined in a higher layer profile */
362 uint16_t description;
363 };
364
365 /**
366 * @defgroup bt_gatt_server GATT Server APIs
367 * @ingroup bt_gatt
368 * @{
369 */
370
371 /** @brief Register GATT callbacks.
372 *
373 * Register callbacks to monitor the state of GATT.
374 *
375 * @param cb Callback struct.
376 */
377 void bt_gatt_cb_register(struct bt_gatt_cb *cb);
378
379 /** @brief Register GATT service.
380 *
381 * Register GATT service. Applications can make use of
382 * macros such as BT_GATT_PRIMARY_SERVICE, BT_GATT_CHARACTERISTIC,
383 * BT_GATT_DESCRIPTOR, etc.
384 *
385 * When using @kconfig{CONFIG_BT_SETTINGS} then all services that should have
386 * bond configuration loaded, i.e. CCC values, must be registered before
387 * calling @ref settings_load.
388 *
389 * When using @kconfig{CONFIG_BT_GATT_CACHING} and @kconfig{CONFIG_BT_SETTINGS}
390 * then all services that should be included in the GATT Database Hash
391 * calculation should be added before calling @ref settings_load.
392 * All services registered after settings_load will trigger a new database hash
393 * calculation and a new hash stored.
394 *
395 * There are two situations where this function can be called: either before
396 * `bt_init()` has been called, or after `settings_load()` has been called.
397 * Registering a service in the middle is not supported and will return an
398 * error.
399 *
400 * @param svc Service containing the available attributes
401 *
402 * @return 0 in case of success or negative value in case of error.
403 * @return -EAGAIN if `bt_init()` has been called but `settings_load()` hasn't yet.
404 */
405 int bt_gatt_service_register(struct bt_gatt_service *svc);
406
407 /** @brief Unregister GATT service.
408 *
409 * @param svc Service to be unregistered.
410 *
411 * @return 0 in case of success or negative value in case of error.
412 */
413 int bt_gatt_service_unregister(struct bt_gatt_service *svc);
414
415 /** @brief Check if GATT service is registered.
416 *
417 * @param svc Service to be checked.
418 *
419 * @return true if registered or false if not register.
420 */
421 bool bt_gatt_service_is_registered(const struct bt_gatt_service *svc);
422
423 enum {
424 BT_GATT_ITER_STOP = 0,
425 BT_GATT_ITER_CONTINUE,
426 };
427
428 /** @typedef bt_gatt_attr_func_t
429 * @brief Attribute iterator callback.
430 *
431 * @param attr Attribute found.
432 * @param handle Attribute handle found.
433 * @param user_data Data given.
434 *
435 * @return BT_GATT_ITER_CONTINUE if should continue to the next attribute.
436 * @return BT_GATT_ITER_STOP to stop.
437 */
438 typedef uint8_t (*bt_gatt_attr_func_t)(const struct bt_gatt_attr *attr,
439 uint16_t handle,
440 void *user_data);
441
442 /** @brief Attribute iterator by type.
443 *
444 * Iterate attributes in the given range matching given UUID and/or data.
445 *
446 * @param start_handle Start handle.
447 * @param end_handle End handle.
448 * @param uuid UUID to match, passing NULL skips UUID matching.
449 * @param attr_data Attribute data to match, passing NULL skips data matching.
450 * @param num_matches Number matches, passing 0 makes it unlimited.
451 * @param func Callback function.
452 * @param user_data Data to pass to the callback.
453 */
454 void bt_gatt_foreach_attr_type(uint16_t start_handle, uint16_t end_handle,
455 const struct bt_uuid *uuid,
456 const void *attr_data, uint16_t num_matches,
457 bt_gatt_attr_func_t func,
458 void *user_data);
459
460 /** @brief Attribute iterator.
461 *
462 * Iterate attributes in the given range.
463 *
464 * @param start_handle Start handle.
465 * @param end_handle End handle.
466 * @param func Callback function.
467 * @param user_data Data to pass to the callback.
468 */
bt_gatt_foreach_attr(uint16_t start_handle,uint16_t end_handle,bt_gatt_attr_func_t func,void * user_data)469 static inline void bt_gatt_foreach_attr(uint16_t start_handle, uint16_t end_handle,
470 bt_gatt_attr_func_t func,
471 void *user_data)
472 {
473 bt_gatt_foreach_attr_type(start_handle, end_handle, NULL, NULL, 0, func,
474 user_data);
475 }
476
477 /** @brief Iterate to the next attribute
478 *
479 * Iterate to the next attribute following a given attribute.
480 *
481 * @param attr Current Attribute.
482 *
483 * @return The next attribute or NULL if it cannot be found.
484 */
485 struct bt_gatt_attr *bt_gatt_attr_next(const struct bt_gatt_attr *attr);
486
487 /** @brief Find Attribute by UUID.
488 *
489 * Find the attribute with the matching UUID.
490 * To limit the search to a service set the attr to the service attributes and
491 * the attr_count to the service attribute count .
492 *
493 * @param attr Pointer to an attribute that serves as the starting point
494 * for the search of a match for the UUID.
495 * Passing NULL will search the entire range.
496 * @param attr_count The number of attributes from the starting point to
497 * search for a match for the UUID.
498 * Set to 0 to search until the end.
499 * @param uuid UUID to match.
500 */
501 struct bt_gatt_attr *bt_gatt_find_by_uuid(const struct bt_gatt_attr *attr,
502 uint16_t attr_count,
503 const struct bt_uuid *uuid);
504
505 /** @brief Get Attribute handle.
506 *
507 * @param attr Attribute object.
508 *
509 * @return Handle of the corresponding attribute or zero if the attribute
510 * could not be found.
511 */
512 uint16_t bt_gatt_attr_get_handle(const struct bt_gatt_attr *attr);
513
514 /** @brief Get the handle of the characteristic value descriptor.
515 *
516 * @param attr A Characteristic Attribute.
517 *
518 * @note The user_data of the attribute must of type @ref bt_gatt_chrc.
519 *
520 * @return the handle of the corresponding Characteristic Value. The value will
521 * be zero (the invalid handle) if @p attr was not a characteristic
522 * attribute.
523 */
524 uint16_t bt_gatt_attr_value_handle(const struct bt_gatt_attr *attr);
525
526 /** @brief Generic Read Attribute value helper.
527 *
528 * Read attribute value from local database storing the result into buffer.
529 *
530 * @param conn Connection object.
531 * @param attr Attribute to read.
532 * @param buf Buffer to store the value.
533 * @param buf_len Buffer length.
534 * @param offset Start offset.
535 * @param value Attribute value.
536 * @param value_len Length of the attribute value.
537 *
538 * @return number of bytes read in case of success or negative values in
539 * case of error.
540 */
541 ssize_t bt_gatt_attr_read(struct bt_conn *conn, const struct bt_gatt_attr *attr,
542 void *buf, uint16_t buf_len, uint16_t offset,
543 const void *value, uint16_t value_len);
544
545 /** @brief Read Service Attribute helper.
546 *
547 * Read service attribute value from local database storing the result into
548 * buffer after encoding it.
549 * @note Only use this with attributes which user_data is a bt_uuid.
550 *
551 * @param conn Connection object.
552 * @param attr Attribute to read.
553 * @param buf Buffer to store the value read.
554 * @param len Buffer length.
555 * @param offset Start offset.
556 *
557 * @return number of bytes read in case of success or negative values in
558 * case of error.
559 */
560 ssize_t bt_gatt_attr_read_service(struct bt_conn *conn,
561 const struct bt_gatt_attr *attr,
562 void *buf, uint16_t len, uint16_t offset);
563
564 /**
565 * @brief Statically define and register a service.
566 *
567 * Helper macro to statically define and register a service.
568 *
569 * @param _name Service name.
570 */
571 #define BT_GATT_SERVICE_DEFINE(_name, ...) \
572 const struct bt_gatt_attr attr_##_name[] = { __VA_ARGS__ }; \
573 const STRUCT_SECTION_ITERABLE(bt_gatt_service_static, _name) = \
574 BT_GATT_SERVICE(attr_##_name)
575
576 #define _BT_GATT_ATTRS_ARRAY_DEFINE(n, _instances, _attrs_def) \
577 static struct bt_gatt_attr attrs_##n[] = _attrs_def(_instances[n])
578
579 #define _BT_GATT_SERVICE_ARRAY_ITEM(_n, _) BT_GATT_SERVICE(attrs_##_n)
580
581 /**
582 * @brief Statically define service structure array.
583 *
584 * Helper macro to statically define service structure array. Each element
585 * of the array is linked to the service attribute array which is also
586 * defined in this scope using _attrs_def macro.
587 *
588 * @param _name Name of service structure array.
589 * @param _instances Array of instances to pass as user context to the
590 * attribute callbacks.
591 * @param _instance_num Number of elements in instance array.
592 * @param _attrs_def Macro provided by the user that defines attribute
593 * array for the service. This macro should accept single
594 * parameter which is the instance context.
595 */
596 #define BT_GATT_SERVICE_INSTANCE_DEFINE( \
597 _name, _instances, _instance_num, _attrs_def) \
598 BUILD_ASSERT(ARRAY_SIZE(_instances) == _instance_num, \
599 "The number of array elements does not match its size"); \
600 LISTIFY(_instance_num, _BT_GATT_ATTRS_ARRAY_DEFINE, (;), \
601 _instances, _attrs_def); \
602 static struct bt_gatt_service _name[] = { \
603 LISTIFY(_instance_num, _BT_GATT_SERVICE_ARRAY_ITEM, (,)) \
604 }
605
606 /**
607 * @brief Service Structure Declaration Macro.
608 *
609 * Helper macro to declare a service structure.
610 *
611 * @param _attrs Service attributes.
612 */
613 #define BT_GATT_SERVICE(_attrs) \
614 { \
615 .attrs = _attrs, \
616 .attr_count = ARRAY_SIZE(_attrs), \
617 }
618
619 /**
620 * @brief Primary Service Declaration Macro.
621 *
622 * Helper macro to declare a primary service attribute.
623 *
624 * @param _service Service attribute value.
625 */
626 #define BT_GATT_PRIMARY_SERVICE(_service) \
627 BT_GATT_ATTRIBUTE(BT_UUID_GATT_PRIMARY, BT_GATT_PERM_READ, \
628 bt_gatt_attr_read_service, NULL, _service)
629
630 /**
631 * @brief Secondary Service Declaration Macro.
632 *
633 * Helper macro to declare a secondary service attribute.
634 *
635 * @note A secondary service is only intended to be included from a primary
636 * service or another secondary service or other higher layer specification.
637 *
638 * @param _service Service attribute value.
639 */
640 #define BT_GATT_SECONDARY_SERVICE(_service) \
641 BT_GATT_ATTRIBUTE(BT_UUID_GATT_SECONDARY, BT_GATT_PERM_READ, \
642 bt_gatt_attr_read_service, NULL, _service)
643
644 /** @brief Read Include Attribute helper.
645 *
646 * Read include service attribute value from local database storing the result
647 * into buffer after encoding it.
648 * @note Only use this with attributes which user_data is a bt_gatt_include.
649 *
650 * @param conn Connection object.
651 * @param attr Attribute to read.
652 * @param buf Buffer to store the value read.
653 * @param len Buffer length.
654 * @param offset Start offset.
655 *
656 * @return number of bytes read in case of success or negative values in
657 * case of error.
658 */
659 ssize_t bt_gatt_attr_read_included(struct bt_conn *conn,
660 const struct bt_gatt_attr *attr,
661 void *buf, uint16_t len, uint16_t offset);
662
663 /**
664 * @brief Include Service Declaration Macro.
665 *
666 * Helper macro to declare database internal include service attribute.
667 *
668 * @param _service_incl the first service attribute of service to include
669 */
670 #define BT_GATT_INCLUDE_SERVICE(_service_incl) \
671 BT_GATT_ATTRIBUTE(BT_UUID_GATT_INCLUDE, BT_GATT_PERM_READ, \
672 bt_gatt_attr_read_included, NULL, _service_incl)
673
674 /** @brief Read Characteristic Attribute helper.
675 *
676 * Read characteristic attribute value from local database storing the result
677 * into buffer after encoding it.
678 * @note Only use this with attributes which user_data is a bt_gatt_chrc.
679 *
680 * @param conn Connection object.
681 * @param attr Attribute to read.
682 * @param buf Buffer to store the value read.
683 * @param len Buffer length.
684 * @param offset Start offset.
685 *
686 * @return number of bytes read in case of success or negative values in
687 * case of error.
688 */
689 ssize_t bt_gatt_attr_read_chrc(struct bt_conn *conn,
690 const struct bt_gatt_attr *attr, void *buf,
691 uint16_t len, uint16_t offset);
692
693 #define BT_GATT_CHRC_INIT(_uuid, _handle, _props) \
694 { \
695 .uuid = _uuid, \
696 .value_handle = _handle, \
697 .properties = _props, \
698 }
699
700 /**
701 * @brief Characteristic and Value Declaration Macro.
702 *
703 * Helper macro to declare a characteristic attribute along with its
704 * attribute value.
705 *
706 * @param _uuid Characteristic attribute uuid.
707 * @param _props Characteristic attribute properties,
708 * a bitmap of BT_GATT_CHRC_* macros.
709 * @param _perm Characteristic Attribute access permissions,
710 * a bitmap of @ref bt_gatt_perm values.
711 * @param _read Characteristic Attribute read callback
712 * (@ref bt_gatt_attr_read_func_t).
713 * @param _write Characteristic Attribute write callback
714 * (@ref bt_gatt_attr_write_func_t).
715 * @param _user_data Characteristic Attribute user data.
716 */
717 #define BT_GATT_CHARACTERISTIC(_uuid, _props, _perm, _read, _write, _user_data) \
718 BT_GATT_ATTRIBUTE(BT_UUID_GATT_CHRC, BT_GATT_PERM_READ, \
719 bt_gatt_attr_read_chrc, NULL, \
720 ((struct bt_gatt_chrc[]) { \
721 BT_GATT_CHRC_INIT(_uuid, 0U, _props), \
722 })), \
723 BT_GATT_ATTRIBUTE(_uuid, _perm, _read, _write, _user_data)
724
725 #if defined(CONFIG_BT_SETTINGS_CCC_LAZY_LOADING)
726 #define BT_GATT_CCC_MAX (CONFIG_BT_MAX_CONN)
727 #elif defined(CONFIG_BT_CONN)
728 #define BT_GATT_CCC_MAX (CONFIG_BT_MAX_PAIRED + CONFIG_BT_MAX_CONN)
729 #else
730 #define BT_GATT_CCC_MAX 0
731 #endif
732
733 /** @brief GATT CCC configuration entry. */
734 struct bt_gatt_ccc_cfg {
735 /** Local identity, BT_ID_DEFAULT in most cases. */
736 uint8_t id;
737 /** Remote peer address. */
738 bt_addr_le_t peer;
739 /**
740 * Separate storage for encrypted and unencrypted context. This
741 * indicate that the link was encrypted when the CCC was written.
742 */
743 bool link_encrypted;
744 /** Configuration value. */
745 uint16_t value;
746 };
747
748 /** Internal representation of CCC value */
749 struct _bt_gatt_ccc {
750 /** Configuration for each connection */
751 struct bt_gatt_ccc_cfg cfg[BT_GATT_CCC_MAX];
752
753 /** Highest value of all connected peer's subscriptions */
754 uint16_t value;
755
756 /** @brief CCC attribute changed callback
757 *
758 * @param attr The attribute that's changed value
759 * @param value New value
760 */
761 void (*cfg_changed)(const struct bt_gatt_attr *attr, uint16_t value);
762
763 /** @brief CCC attribute write validation callback
764 *
765 * @param conn The connection that is requesting to write
766 * @param attr The attribute that's being written
767 * @param value CCC value to write
768 *
769 * @return Number of bytes to write, or in case of an error
770 * BT_GATT_ERR() with a specific error code.
771 */
772 ssize_t (*cfg_write)(struct bt_conn *conn,
773 const struct bt_gatt_attr *attr, uint16_t value);
774
775 /** @brief CCC attribute match handler
776 *
777 * Indicate if it is OK to send a notification or indication
778 * to the subscriber.
779 *
780 * @param conn The connection that is being checked
781 * @param attr The attribute that's being checked
782 *
783 * @return true if application has approved notification/indication,
784 * false if application does not approve.
785 */
786 bool (*cfg_match)(struct bt_conn *conn,
787 const struct bt_gatt_attr *attr);
788 };
789
790 /** @brief Read Client Characteristic Configuration Attribute helper.
791 *
792 * Read CCC attribute value from local database storing the result into buffer
793 * after encoding it.
794 *
795 * @note Only use this with attributes which user_data is a _bt_gatt_ccc.
796 *
797 * @param conn Connection object.
798 * @param attr Attribute to read.
799 * @param buf Buffer to store the value read.
800 * @param len Buffer length.
801 * @param offset Start offset.
802 *
803 * @return number of bytes read in case of success or negative values in
804 * case of error.
805 */
806 ssize_t bt_gatt_attr_read_ccc(struct bt_conn *conn,
807 const struct bt_gatt_attr *attr, void *buf,
808 uint16_t len, uint16_t offset);
809
810 /** @brief Write Client Characteristic Configuration Attribute helper.
811 *
812 * Write value in the buffer into CCC attribute.
813 *
814 * @note Only use this with attributes which user_data is a _bt_gatt_ccc.
815 *
816 * @param conn Connection object.
817 * @param attr Attribute to read.
818 * @param buf Buffer to store the value read.
819 * @param len Buffer length.
820 * @param offset Start offset.
821 * @param flags Write flags.
822 *
823 * @return number of bytes written in case of success or negative values in
824 * case of error.
825 */
826 ssize_t bt_gatt_attr_write_ccc(struct bt_conn *conn,
827 const struct bt_gatt_attr *attr, const void *buf,
828 uint16_t len, uint16_t offset, uint8_t flags);
829
830
831 /**
832 * @brief Initialize Client Characteristic Configuration Declaration Macro.
833 *
834 * Helper macro to initialize a Managed CCC attribute value.
835 *
836 * @param _changed Configuration changed callback.
837 * @param _write Configuration write callback.
838 * @param _match Configuration match callback.
839 */
840 #define BT_GATT_CCC_INITIALIZER(_changed, _write, _match) \
841 { \
842 .cfg = {}, \
843 .cfg_changed = _changed, \
844 .cfg_write = _write, \
845 .cfg_match = _match, \
846 }
847
848 /**
849 * @brief Managed Client Characteristic Configuration Declaration Macro.
850 *
851 * Helper macro to declare a Managed CCC attribute.
852 *
853 * @param _ccc CCC attribute user data, shall point to a _bt_gatt_ccc.
854 * @param _perm CCC access permissions,
855 * a bitmap of @ref bt_gatt_perm values.
856 */
857 #define BT_GATT_CCC_MANAGED(_ccc, _perm) \
858 BT_GATT_ATTRIBUTE(BT_UUID_GATT_CCC, _perm, \
859 bt_gatt_attr_read_ccc, bt_gatt_attr_write_ccc, \
860 _ccc)
861
862 /**
863 * @brief Client Characteristic Configuration Declaration Macro.
864 *
865 * Helper macro to declare a CCC attribute.
866 *
867 * @param _changed Configuration changed callback.
868 * @param _perm CCC access permissions,
869 * a bitmap of @ref bt_gatt_perm values.
870 */
871 #define BT_GATT_CCC(_changed, _perm) \
872 BT_GATT_CCC_MANAGED(((struct _bt_gatt_ccc[]) \
873 {BT_GATT_CCC_INITIALIZER(_changed, NULL, NULL)}), _perm)
874
875 /** @brief Read Characteristic Extended Properties Attribute helper
876 *
877 * Read CEP attribute value from local database storing the result into buffer
878 * after encoding it.
879 *
880 * @note Only use this with attributes which user_data is a bt_gatt_cep.
881 *
882 * @param conn Connection object
883 * @param attr Attribute to read
884 * @param buf Buffer to store the value read
885 * @param len Buffer length
886 * @param offset Start offset
887 *
888 * @return number of bytes read in case of success or negative values in
889 * case of error.
890 */
891 ssize_t bt_gatt_attr_read_cep(struct bt_conn *conn,
892 const struct bt_gatt_attr *attr, void *buf,
893 uint16_t len, uint16_t offset);
894
895 /**
896 * @brief Characteristic Extended Properties Declaration Macro.
897 *
898 * Helper macro to declare a CEP attribute.
899 *
900 * @param _value Pointer to a struct bt_gatt_cep.
901 */
902 #define BT_GATT_CEP(_value) \
903 BT_GATT_DESCRIPTOR(BT_UUID_GATT_CEP, BT_GATT_PERM_READ, \
904 bt_gatt_attr_read_cep, NULL, (void *)_value)
905
906 /** @brief Read Characteristic User Description Descriptor Attribute helper
907 *
908 * Read CUD attribute value from local database storing the result into buffer
909 * after encoding it.
910 *
911 * @note Only use this with attributes which user_data is a NULL-terminated C
912 * string.
913 *
914 * @param conn Connection object
915 * @param attr Attribute to read
916 * @param buf Buffer to store the value read
917 * @param len Buffer length
918 * @param offset Start offset
919 *
920 * @return number of bytes read in case of success or negative values in
921 * case of error.
922 */
923 ssize_t bt_gatt_attr_read_cud(struct bt_conn *conn,
924 const struct bt_gatt_attr *attr, void *buf,
925 uint16_t len, uint16_t offset);
926
927 /**
928 * @brief Characteristic User Format Descriptor Declaration Macro.
929 *
930 * Helper macro to declare a CUD attribute.
931 *
932 * @param _value User description NULL-terminated C string.
933 * @param _perm Descriptor attribute access permissions,
934 * a bitmap of @ref bt_gatt_perm values.
935 */
936 #define BT_GATT_CUD(_value, _perm) \
937 BT_GATT_DESCRIPTOR(BT_UUID_GATT_CUD, _perm, bt_gatt_attr_read_cud, \
938 NULL, (void *)_value)
939
940 /** @brief Read Characteristic Presentation format Descriptor Attribute helper
941 *
942 * Read CPF attribute value from local database storing the result into buffer
943 * after encoding it.
944 *
945 * @note Only use this with attributes which user_data is a bt_gatt_pf.
946 *
947 * @param conn Connection object
948 * @param attr Attribute to read
949 * @param buf Buffer to store the value read
950 * @param len Buffer length
951 * @param offset Start offset
952 *
953 * @return number of bytes read in case of success or negative values in
954 * case of error.
955 */
956 ssize_t bt_gatt_attr_read_cpf(struct bt_conn *conn,
957 const struct bt_gatt_attr *attr, void *buf,
958 uint16_t len, uint16_t offset);
959
960 /**
961 * @brief Characteristic Presentation Format Descriptor Declaration Macro.
962 *
963 * Helper macro to declare a CPF attribute.
964 *
965 * @param _value Pointer to a struct bt_gatt_cpf.
966 */
967 #define BT_GATT_CPF(_value) \
968 BT_GATT_DESCRIPTOR(BT_UUID_GATT_CPF, BT_GATT_PERM_READ, \
969 bt_gatt_attr_read_cpf, NULL, (void *)_value)
970
971 /**
972 * @brief Descriptor Declaration Macro.
973 *
974 * Helper macro to declare a descriptor attribute.
975 *
976 * @param _uuid Descriptor attribute uuid.
977 * @param _perm Descriptor attribute access permissions,
978 * a bitmap of @ref bt_gatt_perm values.
979 * @param _read Descriptor attribute read callback
980 * (@ref bt_gatt_attr_read_func_t).
981 * @param _write Descriptor attribute write callback
982 * (@ref bt_gatt_attr_write_func_t).
983 * @param _user_data Descriptor attribute user data.
984 */
985 #define BT_GATT_DESCRIPTOR(_uuid, _perm, _read, _write, _user_data) \
986 BT_GATT_ATTRIBUTE(_uuid, _perm, _read, _write, _user_data)
987
988 /**
989 * @brief Attribute Declaration Macro.
990 *
991 * Helper macro to declare an attribute.
992 *
993 * @param _uuid Attribute uuid.
994 * @param _perm Attribute access permissions,
995 * a bitmap of @ref bt_gatt_perm values.
996 * @param _read Attribute read callback (@ref bt_gatt_attr_read_func_t).
997 * @param _write Attribute write callback (@ref bt_gatt_attr_write_func_t).
998 * @param _user_data Attribute user data.
999 */
1000 #define BT_GATT_ATTRIBUTE(_uuid, _perm, _read, _write, _user_data) \
1001 { \
1002 .uuid = _uuid, \
1003 .read = _read, \
1004 .write = _write, \
1005 .user_data = _user_data, \
1006 .handle = 0, \
1007 .perm = _perm, \
1008 }
1009
1010 /** @brief Notification complete result callback.
1011 *
1012 * @param conn Connection object.
1013 * @param user_data Data passed in by the user.
1014 */
1015 typedef void (*bt_gatt_complete_func_t) (struct bt_conn *conn, void *user_data);
1016
1017 struct bt_gatt_notify_params {
1018 /** @brief Notification Attribute UUID type
1019 *
1020 * Optional, use to search for an attribute with matching UUID when
1021 * the attribute object pointer is not known.
1022 */
1023 const struct bt_uuid *uuid;
1024 /** @brief Notification Attribute object
1025 *
1026 * Optional if uuid is provided, in this case it will be used as start
1027 * range to search for the attribute with the given UUID.
1028 */
1029 const struct bt_gatt_attr *attr;
1030 /** Notification Value data */
1031 const void *data;
1032 /** Notification Value length */
1033 uint16_t len;
1034 /** Notification Value callback */
1035 bt_gatt_complete_func_t func;
1036 /** Notification Value callback user data */
1037 void *user_data;
1038 #if defined(CONFIG_BT_EATT)
1039 enum bt_att_chan_opt chan_opt;
1040 #endif /* CONFIG_BT_EATT */
1041 };
1042
1043 /** @brief Notify attribute value change.
1044 *
1045 * This function works in the same way as @ref bt_gatt_notify.
1046 * With the addition that after sending the notification the
1047 * callback function will be called.
1048 *
1049 * The callback is run from System Workqueue context.
1050 * When called from the System Workqueue context this API will not wait for
1051 * resources for the callback but instead return an error.
1052 * The number of pending callbacks can be increased with the
1053 * @kconfig{CONFIG_BT_CONN_TX_MAX} option.
1054 *
1055 * Alternatively it is possible to notify by UUID by setting it on the
1056 * parameters, when using this method the attribute if provided is used as the
1057 * start range when looking up for possible matches.
1058 *
1059 * @param conn Connection object.
1060 * @param params Notification parameters.
1061 *
1062 * @return 0 in case of success or negative value in case of error.
1063 */
1064 int bt_gatt_notify_cb(struct bt_conn *conn,
1065 struct bt_gatt_notify_params *params);
1066
1067 /** @brief Send multiple notifications in a single PDU.
1068 *
1069 * The GATT Server will send a single ATT_MULTIPLE_HANDLE_VALUE_NTF PDU
1070 * containing all the notifications passed to this API.
1071 *
1072 * All `params` must have the same `func` and `user_data` (due to
1073 * implementation limitation). But `func(user_data)` will be invoked for each
1074 * parameter.
1075 *
1076 * As this API may block to wait for Bluetooth Host resources, it is not
1077 * recommended to call it from a cooperative thread or a Bluetooth callback.
1078 *
1079 * The peer's GATT Client must write to this device's Client Supported Features
1080 * attribute and set the bit for Multiple Handle Value Notifications before
1081 * this API can be used.
1082 *
1083 * Only use this API to force the use of the ATT_MULTIPLE_HANDLE_VALUE_NTF PDU.
1084 * For standard applications, `bt_gatt_notify_cb` is preferred, as it will use
1085 * this PDU if supported and automatically fallback to ATT_HANDLE_VALUE_NTF
1086 * when not supported by the peer.
1087 *
1088 * This API has an additional limitation: it only accepts valid attribute
1089 * references and not UUIDs like `bt_gatt_notify` and `bt_gatt_notify_cb`.
1090 *
1091 * @param conn
1092 * Target client.
1093 * Notifying all connected clients by passing `NULL` is not yet supported,
1094 * please use `bt_gatt_notify` instead.
1095 * @param num_params
1096 * Element count of `params` array. Has to be greater than 1.
1097 * @param params
1098 * Array of notification parameters. It is okay to free this after calling
1099 * this function.
1100 *
1101 * @retval 0
1102 * Success. The PDU is queued for sending.
1103 * @retval -EINVAL
1104 * - One of the attribute handles is invalid.
1105 * - Only one parameter was passed. This API expects 2 or more.
1106 * - Not all `func` were equal or not all `user_data` were equal.
1107 * - One of the characteristics is not notifiable.
1108 * - An UUID was passed in one of the parameters.
1109 * @retval -ERANGE
1110 * - The notifications cannot all fit in a single ATT_MULTIPLE_HANDLE_VALUE_NTF.
1111 * - They exceed the MTU of all open ATT bearers.
1112 * @retval -EPERM
1113 * The connection has a lower security level than required by one of the
1114 * attributes.
1115 * @retval -EOPNOTSUPP
1116 * The peer hasn't yet communicated that it supports this PDU type.
1117 */
1118 int bt_gatt_notify_multiple(struct bt_conn *conn,
1119 uint16_t num_params,
1120 struct bt_gatt_notify_params params[]);
1121
1122 /** @brief Notify attribute value change.
1123 *
1124 * Send notification of attribute value change, if connection is NULL notify
1125 * all peer that have notification enabled via CCC otherwise do a direct
1126 * notification only the given connection.
1127 *
1128 * The attribute object on the parameters can be the so called Characteristic
1129 * Declaration, which is usually declared with BT_GATT_CHARACTERISTIC followed
1130 * by BT_GATT_CCC, or the Characteristic Value Declaration which is
1131 * automatically created after the Characteristic Declaration when using
1132 * BT_GATT_CHARACTERISTIC.
1133 *
1134 * @param conn Connection object.
1135 * @param attr Characteristic or Characteristic Value attribute.
1136 * @param data Pointer to Attribute data.
1137 * @param len Attribute value length.
1138 *
1139 * @return 0 in case of success or negative value in case of error.
1140 */
bt_gatt_notify(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * data,uint16_t len)1141 static inline int bt_gatt_notify(struct bt_conn *conn,
1142 const struct bt_gatt_attr *attr,
1143 const void *data, uint16_t len)
1144 {
1145 struct bt_gatt_notify_params params;
1146
1147 memset(¶ms, 0, sizeof(params));
1148
1149 params.attr = attr;
1150 params.data = data;
1151 params.len = len;
1152 #if defined(CONFIG_BT_EATT)
1153 params.chan_opt = BT_ATT_CHAN_OPT_NONE;
1154 #endif /* CONFIG_BT_EATT */
1155
1156 return bt_gatt_notify_cb(conn, ¶ms);
1157 }
1158
1159 /** @brief Notify attribute value change by UUID.
1160 *
1161 * Send notification of attribute value change, if connection is NULL notify
1162 * all peer that have notification enabled via CCC otherwise do a direct
1163 * notification only on the given connection.
1164 *
1165 * The attribute object is the starting point for the search of the UUID.
1166 *
1167 * @param conn Connection object.
1168 * @param uuid The UUID. If the server contains multiple services with the same
1169 * UUID, then the first occurrence, starting from the attr given,
1170 * is used.
1171 * @param attr Pointer to an attribute that serves as the starting point for
1172 * the search of a match for the UUID.
1173 * @param data Pointer to Attribute data.
1174 * @param len Attribute value length.
1175 *
1176 * @return 0 in case of success or negative value in case of error.
1177 */
bt_gatt_notify_uuid(struct bt_conn * conn,const struct bt_uuid * uuid,const struct bt_gatt_attr * attr,const void * data,uint16_t len)1178 static inline int bt_gatt_notify_uuid(struct bt_conn *conn,
1179 const struct bt_uuid *uuid,
1180 const struct bt_gatt_attr *attr,
1181 const void *data, uint16_t len)
1182 {
1183 struct bt_gatt_notify_params params;
1184
1185 memset(¶ms, 0, sizeof(params));
1186
1187 params.uuid = uuid;
1188 params.attr = attr;
1189 params.data = data;
1190 params.len = len;
1191 #if defined(CONFIG_BT_EATT)
1192 params.chan_opt = BT_ATT_CHAN_OPT_NONE;
1193 #endif /* CONFIG_BT_EATT */
1194
1195 return bt_gatt_notify_cb(conn, ¶ms);
1196 }
1197
1198 /* Forward declaration of the bt_gatt_indicate_params structure */
1199 struct bt_gatt_indicate_params;
1200
1201 /** @typedef bt_gatt_indicate_func_t
1202 * @brief Indication complete result callback.
1203 *
1204 * @param conn Connection object.
1205 * @param params Indication params object.
1206 * @param err ATT error code
1207 */
1208 typedef void (*bt_gatt_indicate_func_t)(struct bt_conn *conn,
1209 struct bt_gatt_indicate_params *params,
1210 uint8_t err);
1211
1212 typedef void (*bt_gatt_indicate_params_destroy_t)(
1213 struct bt_gatt_indicate_params *params);
1214
1215 /** @brief GATT Indicate Value parameters */
1216 struct bt_gatt_indicate_params {
1217 /** @brief Indicate Attribute UUID type
1218 *
1219 * Optional, use to search for an attribute with matching UUID when
1220 * the attribute object pointer is not known.
1221 */
1222 const struct bt_uuid *uuid;
1223 /** @brief Indicate Attribute object
1224 *
1225 * Optional if uuid is provided, in this case it will be used as start
1226 * range to search for the attribute with the given UUID.
1227 */
1228 const struct bt_gatt_attr *attr;
1229 /** Indicate Value callback */
1230 bt_gatt_indicate_func_t func;
1231 /** Indicate operation complete callback */
1232 bt_gatt_indicate_params_destroy_t destroy;
1233 /** Indicate Value data*/
1234 const void *data;
1235 /** Indicate Value length*/
1236 uint16_t len;
1237 /** Private reference counter */
1238 uint8_t _ref;
1239 #if defined(CONFIG_BT_EATT)
1240 enum bt_att_chan_opt chan_opt;
1241 #endif /* CONFIG_BT_EATT */
1242 };
1243
1244 /** @brief Indicate attribute value change.
1245 *
1246 * Send an indication of attribute value change. if connection is NULL
1247 * indicate all peer that have notification enabled via CCC otherwise do a
1248 * direct indication only the given connection.
1249 *
1250 * The attribute object on the parameters can be the so called Characteristic
1251 * Declaration, which is usually declared with BT_GATT_CHARACTERISTIC followed
1252 * by BT_GATT_CCC, or the Characteristic Value Declaration which is
1253 * automatically created after the Characteristic Declaration when using
1254 * BT_GATT_CHARACTERISTIC.
1255 *
1256 * Alternatively it is possible to indicate by UUID by setting it on the
1257 * parameters, when using this method the attribute if provided is used as the
1258 * start range when looking up for possible matches.
1259 *
1260 * @note This procedure is asynchronous therefore the parameters need to
1261 * remains valid while it is active. The procedure is active until
1262 * the destroy callback is run.
1263 *
1264 * @param conn Connection object.
1265 * @param params Indicate parameters.
1266 *
1267 * @return 0 in case of success or negative value in case of error.
1268 */
1269 int bt_gatt_indicate(struct bt_conn *conn,
1270 struct bt_gatt_indicate_params *params);
1271
1272
1273 /** @brief Check if connection have subscribed to attribute
1274 *
1275 * Check if connection has subscribed to attribute value change.
1276 *
1277 * The attribute object can be the so called Characteristic Declaration,
1278 * which is usually declared with BT_GATT_CHARACTERISTIC followed
1279 * by BT_GATT_CCC, or the Characteristic Value Declaration which is
1280 * automatically created after the Characteristic Declaration when using
1281 * BT_GATT_CHARACTERISTIC, or the Client Characteristic Configuration
1282 * Descriptor (CCCD) which is created by BT_GATT_CCC.
1283 *
1284 * @param conn Connection object.
1285 * @param attr Attribute object.
1286 * @param ccc_type The subscription type, @ref BT_GATT_CCC_NOTIFY and/or
1287 * @ref BT_GATT_CCC_INDICATE.
1288 *
1289 * @return true if the attribute object has been subscribed.
1290 */
1291 bool bt_gatt_is_subscribed(struct bt_conn *conn,
1292 const struct bt_gatt_attr *attr, uint16_t ccc_type);
1293
1294 /** @brief Get ATT MTU for a connection
1295 *
1296 * Get negotiated ATT connection MTU, note that this does not equal the largest
1297 * amount of attribute data that can be transferred within a single packet.
1298 *
1299 * @param conn Connection object.
1300 *
1301 * @return MTU in bytes
1302 */
1303 uint16_t bt_gatt_get_mtu(struct bt_conn *conn);
1304
1305 /** @} */
1306
1307 /**
1308 * @defgroup bt_gatt_client GATT Client APIs
1309 * @ingroup bt_gatt
1310 * @{
1311 */
1312
1313 /** @brief GATT Exchange MTU parameters */
1314 struct bt_gatt_exchange_params {
1315 /** Response callback */
1316 void (*func)(struct bt_conn *conn, uint8_t err,
1317 struct bt_gatt_exchange_params *params);
1318 };
1319
1320 /** @brief Exchange MTU
1321 *
1322 * This client procedure can be used to set the MTU to the maximum possible
1323 * size the buffers can hold.
1324 *
1325 * @note Shall only be used once per connection.
1326 *
1327 * The Response comes in callback @p params->func. The callback is run from
1328 * the context specified by 'config BT_RECV_CONTEXT'.
1329 * @p params must remain valid until start of callback.
1330 *
1331 * This function will block while the ATT request queue is full, except when
1332 * called from the BT RX thread, as this would cause a deadlock.
1333 *
1334 * @param conn Connection object.
1335 * @param params Exchange MTU parameters.
1336 *
1337 * @retval 0 Successfully queued request. Will call @p params->func on
1338 * resolution.
1339 *
1340 * @retval -ENOMEM ATT request queue is full and blocking would cause deadlock.
1341 * Allow a pending request to resolve before retrying, or call this function
1342 * outside the BT RX thread to get blocking behavior. Queue size is controlled
1343 * by @kconfig{CONFIG_BT_L2CAP_TX_BUF_COUNT}.
1344 *
1345 * @retval -EALREADY The MTU exchange procedure has been already performed.
1346 */
1347 int bt_gatt_exchange_mtu(struct bt_conn *conn,
1348 struct bt_gatt_exchange_params *params);
1349
1350 struct bt_gatt_discover_params;
1351
1352 /** @typedef bt_gatt_discover_func_t
1353 * @brief Discover attribute callback function.
1354 *
1355 * @param conn Connection object.
1356 * @param attr Attribute found, or NULL if not found.
1357 * @param params Discovery parameters given.
1358 *
1359 * If discovery procedure has completed this callback will be called with
1360 * attr set to NULL. This will not happen if procedure was stopped by returning
1361 * BT_GATT_ITER_STOP.
1362 *
1363 * The attribute object as well as its UUID and value objects are temporary and
1364 * must be copied to in order to cache its information.
1365 * Only the following fields of the attribute contains valid information:
1366 * - uuid UUID representing the type of attribute.
1367 * - handle Handle in the remote database.
1368 * - user_data The value of the attribute, if the discovery type maps to an
1369 * ATT operation that provides this information. NULL otherwise.
1370 * See below.
1371 *
1372 * The effective type of @c attr->user_data is determined by @c params. Note
1373 * that the fields @c params->type and @c params->uuid are left unchanged by
1374 * the discovery procedure.
1375 *
1376 * @c params->type | @c params->uuid | Type of @c attr->user_data
1377 * -------------------------------------|-------------------------|---------------------------
1378 * @ref BT_GATT_DISCOVER_PRIMARY | any | @ref bt_gatt_service_val
1379 * @ref BT_GATT_DISCOVER_SECONDARY | any | @ref bt_gatt_service_val
1380 * @ref BT_GATT_DISCOVER_INCLUDE | any | @ref bt_gatt_include
1381 * @ref BT_GATT_DISCOVER_CHARACTERISTIC | any | @ref bt_gatt_chrc
1382 * @ref BT_GATT_DISCOVER_STD_CHAR_DESC | @ref BT_UUID_GATT_CEP | @ref bt_gatt_cep
1383 * @ref BT_GATT_DISCOVER_STD_CHAR_DESC | @ref BT_UUID_GATT_CCC | @ref bt_gatt_ccc
1384 * @ref BT_GATT_DISCOVER_STD_CHAR_DESC | @ref BT_UUID_GATT_SCC | @ref bt_gatt_scc
1385 * @ref BT_GATT_DISCOVER_STD_CHAR_DESC | @ref BT_UUID_GATT_CPF | @ref bt_gatt_cpf
1386 * @ref BT_GATT_DISCOVER_DESCRIPTOR | any | NULL
1387 * @ref BT_GATT_DISCOVER_ATTRIBUTE | any | NULL
1388 *
1389 * Also consider if using read-by-type instead of discovery is more convenient.
1390 * See @ref bt_gatt_read with @ref bt_gatt_read_params.handle_count set to
1391 * @c 0.
1392 *
1393 * @return BT_GATT_ITER_CONTINUE to continue discovery procedure.
1394 * @return BT_GATT_ITER_STOP to stop discovery procedure.
1395 */
1396 typedef uint8_t (*bt_gatt_discover_func_t)(struct bt_conn *conn,
1397 const struct bt_gatt_attr *attr,
1398 struct bt_gatt_discover_params *params);
1399
1400 /** GATT Discover types */
1401 enum {
1402 /** Discover Primary Services. */
1403 BT_GATT_DISCOVER_PRIMARY,
1404 /** Discover Secondary Services. */
1405 BT_GATT_DISCOVER_SECONDARY,
1406 /** Discover Included Services. */
1407 BT_GATT_DISCOVER_INCLUDE,
1408 /** @brief Discover Characteristic Values.
1409 *
1410 * Discover Characteristic Value and its properties.
1411 */
1412 BT_GATT_DISCOVER_CHARACTERISTIC,
1413 /** @brief Discover Descriptors.
1414 *
1415 * Discover Attributes which are not services or characteristics.
1416 *
1417 * @note The use of this type of discover is not recommended for
1418 * discovering in ranges across multiple services/characteristics
1419 * as it may incur in extra round trips.
1420 */
1421 BT_GATT_DISCOVER_DESCRIPTOR,
1422 /** @brief Discover Attributes.
1423 *
1424 * Discover Attributes of any type.
1425 *
1426 * @note The use of this type of discover is not recommended for
1427 * discovering in ranges across multiple services/characteristics
1428 * as it may incur in more round trips.
1429 */
1430 BT_GATT_DISCOVER_ATTRIBUTE,
1431 /** @brief Discover standard characteristic descriptor values.
1432 *
1433 * Discover standard characteristic descriptor values and their
1434 * properties.
1435 * Supported descriptors:
1436 * - Characteristic Extended Properties
1437 * - Client Characteristic Configuration
1438 * - Server Characteristic Configuration
1439 * - Characteristic Presentation Format
1440 */
1441 BT_GATT_DISCOVER_STD_CHAR_DESC,
1442 };
1443
1444 /** @brief GATT Discover Attributes parameters */
1445 struct bt_gatt_discover_params {
1446 /** Discover UUID type */
1447 const struct bt_uuid *uuid;
1448 /** Discover attribute callback */
1449 bt_gatt_discover_func_t func;
1450 union {
1451 struct {
1452 /** Include service attribute declaration handle */
1453 uint16_t attr_handle;
1454 /** Included service start handle */
1455 uint16_t start_handle;
1456 /** Included service end handle */
1457 uint16_t end_handle;
1458 } _included;
1459 /** Discover start handle */
1460 uint16_t start_handle;
1461 };
1462 /** Discover end handle */
1463 uint16_t end_handle;
1464 /** Discover type */
1465 uint8_t type;
1466 #if defined(CONFIG_BT_GATT_AUTO_DISCOVER_CCC)
1467 /** Only for stack-internal use, used for automatic discovery. */
1468 struct bt_gatt_subscribe_params *sub_params;
1469 #endif /* defined(CONFIG_BT_GATT_AUTO_DISCOVER_CCC) */
1470 #if defined(CONFIG_BT_EATT)
1471 enum bt_att_chan_opt chan_opt;
1472 #endif /* CONFIG_BT_EATT */
1473 };
1474
1475 /** @brief GATT Discover function
1476 *
1477 * This procedure is used by a client to discover attributes on a server.
1478 *
1479 * Primary Service Discovery: Procedure allows to discover primary services
1480 * either by Discover All Primary Services or
1481 * Discover Primary Services by Service UUID.
1482 * Include Service Discovery: Procedure allows to discover all Include Services
1483 * within specified range.
1484 * Characteristic Discovery: Procedure allows to discover all characteristics
1485 * within specified handle range as well as
1486 * discover characteristics with specified UUID.
1487 * Descriptors Discovery: Procedure allows to discover all characteristic
1488 * descriptors within specified range.
1489 *
1490 * For each attribute found the callback is called which can then decide
1491 * whether to continue discovering or stop.
1492 *
1493 * The Response comes in callback @p params->func. The callback is run from
1494 * the BT RX thread. @p params must remain valid until start of callback where
1495 * iter `attr` is `NULL` or callback will return `BT_GATT_ITER_STOP`.
1496 *
1497 * This function will block while the ATT request queue is full, except when
1498 * called from the BT RX thread, as this would cause a deadlock.
1499 *
1500 * @param conn Connection object.
1501 * @param params Discover parameters.
1502 *
1503 * @retval 0 Successfully queued request. Will call @p params->func on
1504 * resolution.
1505 *
1506 * @retval -ENOMEM ATT request queue is full and blocking would cause deadlock.
1507 * Allow a pending request to resolve before retrying, or call this function
1508 * outside the BT RX thread to get blocking behavior. Queue size is controlled
1509 * by @kconfig{CONFIG_BT_L2CAP_TX_BUF_COUNT}.
1510 */
1511 int bt_gatt_discover(struct bt_conn *conn,
1512 struct bt_gatt_discover_params *params);
1513
1514 struct bt_gatt_read_params;
1515
1516 /** @typedef bt_gatt_read_func_t
1517 * @brief Read callback function
1518 *
1519 * When reading using by_uuid, `params->start_handle` is the attribute handle
1520 * for this `data` item.
1521 *
1522 * @param conn Connection object.
1523 * @param err ATT error code.
1524 * @param params Read parameters used.
1525 * @param data Attribute value data. NULL means read has completed.
1526 * @param length Attribute value length.
1527 *
1528 * @return BT_GATT_ITER_CONTINUE if should continue to the next attribute.
1529 * @return BT_GATT_ITER_STOP to stop.
1530 */
1531 typedef uint8_t (*bt_gatt_read_func_t)(struct bt_conn *conn, uint8_t err,
1532 struct bt_gatt_read_params *params,
1533 const void *data, uint16_t length);
1534
1535 /** @brief GATT Read parameters */
1536 struct bt_gatt_read_params {
1537 /** Read attribute callback. */
1538 bt_gatt_read_func_t func;
1539 /** If equals to 1 single.handle and single.offset are used.
1540 * If greater than 1 multiple.handles are used.
1541 * If equals to 0 by_uuid is used for Read Using Characteristic UUID.
1542 */
1543 size_t handle_count;
1544 union {
1545 struct {
1546 /** Attribute handle. */
1547 uint16_t handle;
1548 /** Attribute data offset. */
1549 uint16_t offset;
1550 } single;
1551 struct {
1552 /** Attribute handles to read with Read Multiple
1553 * Characteristic Values.
1554 */
1555 uint16_t *handles;
1556 /** If true use Read Multiple Variable Length
1557 * Characteristic Values procedure.
1558 * The values of the set of attributes may be of
1559 * variable or unknown length.
1560 * If false use Read Multiple Characteristic Values
1561 * procedure.
1562 * The values of the set of attributes must be of a
1563 * known fixed length, with the exception of the last
1564 * value that can have a variable length.
1565 */
1566 bool variable;
1567 } multiple;
1568 struct {
1569 /** First requested handle number. */
1570 uint16_t start_handle;
1571 /** Last requested handle number. */
1572 uint16_t end_handle;
1573 /** 2 or 16 octet UUID. */
1574 const struct bt_uuid *uuid;
1575 } by_uuid;
1576 };
1577 #if defined(CONFIG_BT_EATT)
1578 enum bt_att_chan_opt chan_opt;
1579 #endif /* CONFIG_BT_EATT */
1580 };
1581
1582 /** @brief Read Attribute Value by handle
1583 *
1584 * This procedure read the attribute value and return it to the callback.
1585 *
1586 * When reading attributes by UUID the callback can be called multiple times
1587 * depending on how many instances of given the UUID exists with the
1588 * start_handle being updated for each instance.
1589 *
1590 * If an instance does contain a long value which cannot be read entirely the
1591 * caller will need to read the remaining data separately using the handle and
1592 * offset.
1593 *
1594 * The Response comes in callback @p params->func. The callback is run from
1595 * the context specified by 'config BT_RECV_CONTEXT'.
1596 * @p params must remain valid until start of callback.
1597 *
1598 * This function will block while the ATT request queue is full, except when
1599 * called from the BT RX thread, as this would cause a deadlock.
1600 *
1601 * @param conn Connection object.
1602 * @param params Read parameters.
1603 *
1604 * @retval 0 Successfully queued request. Will call @p params->func on
1605 * resolution.
1606 *
1607 * @retval -ENOMEM ATT request queue is full and blocking would cause deadlock.
1608 * Allow a pending request to resolve before retrying, or call this function
1609 * outside the BT RX thread to get blocking behavior. Queue size is controlled
1610 * by @kconfig{CONFIG_BT_L2CAP_TX_BUF_COUNT}.
1611 */
1612 int bt_gatt_read(struct bt_conn *conn, struct bt_gatt_read_params *params);
1613
1614 struct bt_gatt_write_params;
1615
1616 /** @typedef bt_gatt_write_func_t
1617 * @brief Write callback function
1618 *
1619 * @param conn Connection object.
1620 * @param err ATT error code.
1621 * @param params Write parameters used.
1622 */
1623 typedef void (*bt_gatt_write_func_t)(struct bt_conn *conn, uint8_t err,
1624 struct bt_gatt_write_params *params);
1625
1626 /** @brief GATT Write parameters */
1627 struct bt_gatt_write_params {
1628 /** Response callback */
1629 bt_gatt_write_func_t func;
1630 /** Attribute handle */
1631 uint16_t handle;
1632 /** Attribute data offset */
1633 uint16_t offset;
1634 /** Data to be written */
1635 const void *data;
1636 /** Length of the data */
1637 uint16_t length;
1638 #if defined(CONFIG_BT_EATT)
1639 enum bt_att_chan_opt chan_opt;
1640 #endif /* CONFIG_BT_EATT */
1641 };
1642
1643 /** @brief Write Attribute Value by handle
1644 *
1645 * The Response comes in callback @p params->func. The callback is run from
1646 * the context specified by 'config BT_RECV_CONTEXT'.
1647 * @p params must remain valid until start of callback.
1648 *
1649 * This function will block while the ATT request queue is full, except when
1650 * called from Bluetooth event context. When called from Bluetooth context,
1651 * this function will instead instead return `-ENOMEM` if it would block to
1652 * avoid a deadlock.
1653 *
1654 * @param conn Connection object.
1655 * @param params Write parameters.
1656 *
1657 * @retval 0 Successfully queued request. Will call @p params->func on
1658 * resolution.
1659 *
1660 * @retval -ENOMEM ATT request queue is full and blocking would cause deadlock.
1661 * Allow a pending request to resolve before retrying, or call this function
1662 * outside Bluetooth event context to get blocking behavior. Queue size is
1663 * controlled by @kconfig{CONFIG_BT_L2CAP_TX_BUF_COUNT}.
1664 */
1665 int bt_gatt_write(struct bt_conn *conn, struct bt_gatt_write_params *params);
1666
1667 /** @brief Write Attribute Value by handle without response with callback.
1668 *
1669 * This function works in the same way as @ref bt_gatt_write_without_response.
1670 * With the addition that after sending the write the callback function will be
1671 * called.
1672 *
1673 * The callback is run from System Workqueue context.
1674 * When called from the System Workqueue context this API will not wait for
1675 * resources for the callback but instead return an error.
1676 * The number of pending callbacks can be increased with the
1677 * @kconfig{CONFIG_BT_CONN_TX_MAX} option.
1678 *
1679 * @note By using a callback it also disable the internal flow control
1680 * which would prevent sending multiple commands without waiting for
1681 * their transmissions to complete, so if that is required the caller
1682 * shall not submit more data until the callback is called.
1683 *
1684 * This function will block while the ATT request queue is full, except when
1685 * called from the BT RX thread, as this would cause a deadlock.
1686 *
1687 * @param conn Connection object.
1688 * @param handle Attribute handle.
1689 * @param data Data to be written.
1690 * @param length Data length.
1691 * @param sign Whether to sign data
1692 * @param func Transmission complete callback.
1693 * @param user_data User data to be passed back to callback.
1694 *
1695 * @retval 0 Successfully queued request.
1696 *
1697 * @retval -ENOMEM ATT request queue is full and blocking would cause deadlock.
1698 * Allow a pending request to resolve before retrying, or call this function
1699 * outside the BT RX thread to get blocking behavior. Queue size is controlled
1700 * by @kconfig{CONFIG_BT_L2CAP_TX_BUF_COUNT}.
1701 */
1702 int bt_gatt_write_without_response_cb(struct bt_conn *conn, uint16_t handle,
1703 const void *data, uint16_t length,
1704 bool sign, bt_gatt_complete_func_t func,
1705 void *user_data);
1706
1707 /** @brief Write Attribute Value by handle without response
1708 *
1709 * This procedure write the attribute value without requiring an
1710 * acknowledgment that the write was successfully performed
1711 *
1712 * This function will block while the ATT request queue is full, except when
1713 * called from the BT RX thread, as this would cause a deadlock.
1714 *
1715 * @param conn Connection object.
1716 * @param handle Attribute handle.
1717 * @param data Data to be written.
1718 * @param length Data length.
1719 * @param sign Whether to sign data
1720 *
1721 * @retval 0 Successfully queued request.
1722 *
1723 * @retval -ENOMEM ATT request queue is full and blocking would cause deadlock.
1724 * Allow a pending request to resolve before retrying, or call this function
1725 * outside the BT RX thread to get blocking behavior. Queue size is controlled
1726 * by @kconfig{CONFIG_BT_L2CAP_TX_BUF_COUNT}.
1727 */
bt_gatt_write_without_response(struct bt_conn * conn,uint16_t handle,const void * data,uint16_t length,bool sign)1728 static inline int bt_gatt_write_without_response(struct bt_conn *conn,
1729 uint16_t handle, const void *data,
1730 uint16_t length, bool sign)
1731 {
1732 return bt_gatt_write_without_response_cb(conn, handle, data, length,
1733 sign, NULL, NULL);
1734 }
1735
1736 struct bt_gatt_subscribe_params;
1737
1738 /** @typedef bt_gatt_notify_func_t
1739 * @brief Notification callback function
1740 *
1741 * In the case of an empty notification, the @p data pointer will be non-NULL
1742 * while the @p length will be 0, which is due to the special case where
1743 * a @p data NULL pointer means unsubscribed.
1744 *
1745 * @param conn Connection object. May be NULL, indicating that the peer is
1746 * being unpaired
1747 * @param params Subscription parameters.
1748 * @param data Attribute value data. If NULL then subscription was removed.
1749 * @param length Attribute value length.
1750 *
1751 * @return BT_GATT_ITER_CONTINUE to continue receiving value notifications.
1752 * BT_GATT_ITER_STOP to unsubscribe from value notifications.
1753 */
1754 typedef uint8_t (*bt_gatt_notify_func_t)(struct bt_conn *conn,
1755 struct bt_gatt_subscribe_params *params,
1756 const void *data, uint16_t length);
1757
1758 /** @typedef bt_gatt_subscribe_func_t
1759 * @brief Subscription callback function
1760 *
1761 * @param conn Connection object.
1762 * @param err ATT error code.
1763 * @param params Subscription parameters used.
1764 */
1765 typedef void (*bt_gatt_subscribe_func_t)(struct bt_conn *conn, uint8_t err,
1766 struct bt_gatt_subscribe_params *params);
1767
1768 /** Subscription flags */
1769 enum {
1770 /** @brief Persistence flag
1771 *
1772 * If set, indicates that the subscription is not saved
1773 * on the GATT server side. Therefore, upon disconnection,
1774 * the subscription will be automatically removed
1775 * from the client's subscriptions list and
1776 * when the client reconnects, it will have to
1777 * issue a new subscription.
1778 */
1779 BT_GATT_SUBSCRIBE_FLAG_VOLATILE,
1780
1781 /** @brief No resubscribe flag
1782 *
1783 * By default when BT_GATT_SUBSCRIBE_FLAG_VOLATILE is unset, the
1784 * subscription will be automatically renewed when the client
1785 * reconnects, as a workaround for GATT servers that do not persist
1786 * subscriptions.
1787 *
1788 * This flag will disable the automatic resubscription. It is useful
1789 * if the application layer knows that the GATT server remembers
1790 * subscriptions from previous connections and wants to avoid renewing
1791 * the subscriptions.
1792 */
1793 BT_GATT_SUBSCRIBE_FLAG_NO_RESUB,
1794
1795 /** @brief Write pending flag
1796 *
1797 * If set, indicates write operation is pending waiting remote end to
1798 * respond.
1799 *
1800 * @note Internal use only.
1801 */
1802 BT_GATT_SUBSCRIBE_FLAG_WRITE_PENDING,
1803
1804 /** @brief Sent flag
1805 *
1806 * If set, indicates that a subscription request (CCC write) has
1807 * already been sent in the active connection.
1808 *
1809 * Used to avoid sending subscription requests multiple times when the
1810 * @kconfig{CONFIG_BT_GATT_AUTO_RESUBSCRIBE} quirk is enabled.
1811 *
1812 * @note Internal use only.
1813 */
1814 BT_GATT_SUBSCRIBE_FLAG_SENT,
1815
1816 BT_GATT_SUBSCRIBE_NUM_FLAGS
1817 };
1818
1819 /** @brief GATT Subscribe parameters */
1820 struct bt_gatt_subscribe_params {
1821 /** Notification value callback */
1822 bt_gatt_notify_func_t notify;
1823 /** Subscribe CCC write request response callback
1824 * If given, called with the subscription parameters given when subscribing
1825 */
1826 bt_gatt_subscribe_func_t subscribe;
1827
1828 /** @deprecated{subscribe CCC write response callback} */
1829 bt_gatt_write_func_t write;
1830 /** Subscribe value handle */
1831 uint16_t value_handle;
1832 /** Subscribe CCC handle */
1833 uint16_t ccc_handle;
1834 #if defined(CONFIG_BT_GATT_AUTO_DISCOVER_CCC)
1835 /** Subscribe End handle (for automatic discovery) */
1836 uint16_t end_handle;
1837 /** Discover parameters used when ccc_handle = 0 */
1838 struct bt_gatt_discover_params *disc_params;
1839 #endif /* CONFIG_BT_GATT_AUTO_DISCOVER_CCC */
1840 /** Subscribe value */
1841 uint16_t value;
1842 #if defined(CONFIG_BT_SMP)
1843 /** Minimum required security for received notification. Notifications
1844 * and indications received over a connection with a lower security
1845 * level are silently discarded.
1846 */
1847 bt_security_t min_security;
1848 #endif
1849 /** Subscription flags */
1850 ATOMIC_DEFINE(flags, BT_GATT_SUBSCRIBE_NUM_FLAGS);
1851
1852 sys_snode_t node;
1853 #if defined(CONFIG_BT_EATT)
1854 enum bt_att_chan_opt chan_opt;
1855 #endif /* CONFIG_BT_EATT */
1856 };
1857
1858 /** @brief Subscribe Attribute Value Notification
1859 *
1860 * This procedure subscribe to value notification using the Client
1861 * Characteristic Configuration handle.
1862 * If notification received subscribe value callback is called to return
1863 * notified value. One may then decide whether to unsubscribe directly from
1864 * this callback. Notification callback with NULL data will not be called if
1865 * subscription was removed by this method.
1866 *
1867 * The Response comes in callback @p params->subscribe. The callback is run from
1868 * the context specified by 'config BT_RECV_CONTEXT'.
1869 * @p params must remain valid until start of callback.
1870 * The Notification callback @p params->notify is also called from the BT RX
1871 * thread.
1872 *
1873 * @note Notifications are asynchronous therefore the parameters need to
1874 * remain valid while subscribed.
1875 *
1876 * This function will block while the ATT request queue is full, except when
1877 * called from the BT RX thread, as this would cause a deadlock.
1878 *
1879 * @param conn Connection object.
1880 * @param params Subscribe parameters.
1881 *
1882 * @retval 0 Successfully queued request. Will call @p params->write on
1883 * resolution.
1884 *
1885 * @retval -ENOMEM ATT request queue is full and blocking would cause deadlock.
1886 * Allow a pending request to resolve before retrying, or call this function
1887 * outside the BT RX thread to get blocking behavior. Queue size is controlled
1888 * by @kconfig{CONFIG_BT_L2CAP_TX_BUF_COUNT}.
1889 */
1890 int bt_gatt_subscribe(struct bt_conn *conn,
1891 struct bt_gatt_subscribe_params *params);
1892
1893 /** @brief Resubscribe Attribute Value Notification subscription
1894 *
1895 * Resubscribe to Attribute Value Notification when already subscribed from a
1896 * previous connection. The GATT server will remember subscription from
1897 * previous connections when bonded, so resubscribing can be done without
1898 * performing a new subscribe procedure after a power cycle.
1899 *
1900 * @note Notifications are asynchronous therefore the parameters need to
1901 * remain valid while subscribed.
1902 *
1903 * @param id Local identity (in most cases BT_ID_DEFAULT).
1904 * @param peer Remote address.
1905 * @param params Subscribe parameters.
1906 *
1907 * @return 0 in case of success or negative value in case of error.
1908 */
1909 int bt_gatt_resubscribe(uint8_t id, const bt_addr_le_t *peer,
1910 struct bt_gatt_subscribe_params *params);
1911
1912 /** @brief Unsubscribe Attribute Value Notification
1913 *
1914 * This procedure unsubscribe to value notification using the Client
1915 * Characteristic Configuration handle. Notification callback with NULL data
1916 * will be called if subscription was removed by this call, until then the
1917 * parameters cannot be reused.
1918 *
1919 * The Response comes in callback @p params->func. The callback is run from
1920 * the BT RX thread.
1921 *
1922 * This function will block while the ATT request queue is full, except when
1923 * called from the BT RX thread, as this would cause a deadlock.
1924 *
1925 * @param conn Connection object.
1926 * @param params Subscribe parameters.
1927 *
1928 * @retval 0 Successfully queued request. Will call @p params->write on
1929 * resolution.
1930 *
1931 * @retval -ENOMEM ATT request queue is full and blocking would cause deadlock.
1932 * Allow a pending request to resolve before retrying, or call this function
1933 * outside the BT RX thread to get blocking behavior. Queue size is controlled
1934 * by @kconfig{CONFIG_BT_L2CAP_TX_BUF_COUNT}.
1935 */
1936 int bt_gatt_unsubscribe(struct bt_conn *conn,
1937 struct bt_gatt_subscribe_params *params);
1938
1939 /** @brief Try to cancel the first pending request identified by @p params.
1940 *
1941 * This function does not release @p params for reuse. The usual callbacks
1942 * for the request still apply. A successful cancel simulates a
1943 * #BT_ATT_ERR_UNLIKELY response from the server.
1944 *
1945 * This function can cancel the following request functions:
1946 * - #bt_gatt_exchange_mtu
1947 * - #bt_gatt_discover
1948 * - #bt_gatt_read
1949 * - #bt_gatt_write
1950 * - #bt_gatt_subscribe
1951 * - #bt_gatt_unsubscribe
1952 *
1953 * @param conn The connection the request was issued on.
1954 * @param params The address `params` used in the request function call.
1955 */
1956 void bt_gatt_cancel(struct bt_conn *conn, void *params);
1957
1958 /** @} */
1959
1960 #ifdef __cplusplus
1961 }
1962 #endif
1963
1964 /**
1965 * @}
1966 */
1967
1968 #endif /* ZEPHYR_INCLUDE_BLUETOOTH_GATT_H_ */
1969