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 /** Configuration value. */
740 uint16_t value;
741 };
742
743 /** Internal representation of CCC value */
744 struct _bt_gatt_ccc {
745 /** Configuration for each connection */
746 struct bt_gatt_ccc_cfg cfg[BT_GATT_CCC_MAX];
747
748 /** Highest value of all connected peer's subscriptions */
749 uint16_t value;
750
751 /** @brief CCC attribute changed callback
752 *
753 * @param attr The attribute that's changed value
754 * @param value New value
755 */
756 void (*cfg_changed)(const struct bt_gatt_attr *attr, uint16_t value);
757
758 /** @brief CCC attribute write validation callback
759 *
760 * @param conn The connection that is requesting to write
761 * @param attr The attribute that's being written
762 * @param value CCC value to write
763 *
764 * @return Number of bytes to write, or in case of an error
765 * BT_GATT_ERR() with a specific error code.
766 */
767 ssize_t (*cfg_write)(struct bt_conn *conn,
768 const struct bt_gatt_attr *attr, uint16_t value);
769
770 /** @brief CCC attribute match handler
771 *
772 * Indicate if it is OK to send a notification or indication
773 * to the subscriber.
774 *
775 * @param conn The connection that is being checked
776 * @param attr The attribute that's being checked
777 *
778 * @return true if application has approved notification/indication,
779 * false if application does not approve.
780 */
781 bool (*cfg_match)(struct bt_conn *conn,
782 const struct bt_gatt_attr *attr);
783 };
784
785 /** @brief Read Client Characteristic Configuration Attribute helper.
786 *
787 * Read CCC attribute value from local database storing the result into buffer
788 * after encoding it.
789 *
790 * @note Only use this with attributes which user_data is a _bt_gatt_ccc.
791 *
792 * @param conn Connection object.
793 * @param attr Attribute to read.
794 * @param buf Buffer to store the value read.
795 * @param len Buffer length.
796 * @param offset Start offset.
797 *
798 * @return number of bytes read in case of success or negative values in
799 * case of error.
800 */
801 ssize_t bt_gatt_attr_read_ccc(struct bt_conn *conn,
802 const struct bt_gatt_attr *attr, void *buf,
803 uint16_t len, uint16_t offset);
804
805 /** @brief Write Client Characteristic Configuration Attribute helper.
806 *
807 * Write value in the buffer into CCC attribute.
808 *
809 * @note Only use this with attributes which user_data is a _bt_gatt_ccc.
810 *
811 * @param conn Connection object.
812 * @param attr Attribute to read.
813 * @param buf Buffer to store the value read.
814 * @param len Buffer length.
815 * @param offset Start offset.
816 * @param flags Write flags.
817 *
818 * @return number of bytes written in case of success or negative values in
819 * case of error.
820 */
821 ssize_t bt_gatt_attr_write_ccc(struct bt_conn *conn,
822 const struct bt_gatt_attr *attr, const void *buf,
823 uint16_t len, uint16_t offset, uint8_t flags);
824
825
826 /**
827 * @brief Initialize Client Characteristic Configuration Declaration Macro.
828 *
829 * Helper macro to initialize a Managed CCC attribute value.
830 *
831 * @param _changed Configuration changed callback.
832 * @param _write Configuration write callback.
833 * @param _match Configuration match callback.
834 */
835 #define BT_GATT_CCC_INITIALIZER(_changed, _write, _match) \
836 { \
837 .cfg = {}, \
838 .cfg_changed = _changed, \
839 .cfg_write = _write, \
840 .cfg_match = _match, \
841 }
842
843 /**
844 * @brief Managed Client Characteristic Configuration Declaration Macro.
845 *
846 * Helper macro to declare a Managed CCC attribute.
847 *
848 * @param _ccc CCC attribute user data, shall point to a _bt_gatt_ccc.
849 * @param _perm CCC access permissions,
850 * a bitmap of @ref bt_gatt_perm values.
851 */
852 #define BT_GATT_CCC_MANAGED(_ccc, _perm) \
853 BT_GATT_ATTRIBUTE(BT_UUID_GATT_CCC, _perm, \
854 bt_gatt_attr_read_ccc, bt_gatt_attr_write_ccc, \
855 _ccc)
856
857 /**
858 * @brief Client Characteristic Configuration Declaration Macro.
859 *
860 * Helper macro to declare a CCC attribute.
861 *
862 * @param _changed Configuration changed callback.
863 * @param _perm CCC access permissions,
864 * a bitmap of @ref bt_gatt_perm values.
865 */
866 #define BT_GATT_CCC(_changed, _perm) \
867 BT_GATT_CCC_MANAGED(((struct _bt_gatt_ccc[]) \
868 {BT_GATT_CCC_INITIALIZER(_changed, NULL, NULL)}), _perm)
869
870 /** @brief Read Characteristic Extended Properties Attribute helper
871 *
872 * Read CEP attribute value from local database storing the result into buffer
873 * after encoding it.
874 *
875 * @note Only use this with attributes which user_data is a bt_gatt_cep.
876 *
877 * @param conn Connection object
878 * @param attr Attribute to read
879 * @param buf Buffer to store the value read
880 * @param len Buffer length
881 * @param offset Start offset
882 *
883 * @return number of bytes read in case of success or negative values in
884 * case of error.
885 */
886 ssize_t bt_gatt_attr_read_cep(struct bt_conn *conn,
887 const struct bt_gatt_attr *attr, void *buf,
888 uint16_t len, uint16_t offset);
889
890 /**
891 * @brief Characteristic Extended Properties Declaration Macro.
892 *
893 * Helper macro to declare a CEP attribute.
894 *
895 * @param _value Pointer to a struct bt_gatt_cep.
896 */
897 #define BT_GATT_CEP(_value) \
898 BT_GATT_DESCRIPTOR(BT_UUID_GATT_CEP, BT_GATT_PERM_READ, \
899 bt_gatt_attr_read_cep, NULL, (void *)_value)
900
901 /** @brief Read Characteristic User Description Descriptor Attribute helper
902 *
903 * Read CUD attribute value from local database storing the result into buffer
904 * after encoding it.
905 *
906 * @note Only use this with attributes which user_data is a NULL-terminated C
907 * string.
908 *
909 * @param conn Connection object
910 * @param attr Attribute to read
911 * @param buf Buffer to store the value read
912 * @param len Buffer length
913 * @param offset Start offset
914 *
915 * @return number of bytes read in case of success or negative values in
916 * case of error.
917 */
918 ssize_t bt_gatt_attr_read_cud(struct bt_conn *conn,
919 const struct bt_gatt_attr *attr, void *buf,
920 uint16_t len, uint16_t offset);
921
922 /**
923 * @brief Characteristic User Format Descriptor Declaration Macro.
924 *
925 * Helper macro to declare a CUD attribute.
926 *
927 * @param _value User description NULL-terminated C string.
928 * @param _perm Descriptor attribute access permissions,
929 * a bitmap of @ref bt_gatt_perm values.
930 */
931 #define BT_GATT_CUD(_value, _perm) \
932 BT_GATT_DESCRIPTOR(BT_UUID_GATT_CUD, _perm, bt_gatt_attr_read_cud, \
933 NULL, (void *)_value)
934
935 /** @brief Read Characteristic Presentation format Descriptor Attribute helper
936 *
937 * Read CPF attribute value from local database storing the result into buffer
938 * after encoding it.
939 *
940 * @note Only use this with attributes which user_data is a bt_gatt_pf.
941 *
942 * @param conn Connection object
943 * @param attr Attribute to read
944 * @param buf Buffer to store the value read
945 * @param len Buffer length
946 * @param offset Start offset
947 *
948 * @return number of bytes read in case of success or negative values in
949 * case of error.
950 */
951 ssize_t bt_gatt_attr_read_cpf(struct bt_conn *conn,
952 const struct bt_gatt_attr *attr, void *buf,
953 uint16_t len, uint16_t offset);
954
955 /**
956 * @brief Characteristic Presentation Format Descriptor Declaration Macro.
957 *
958 * Helper macro to declare a CPF attribute.
959 *
960 * @param _value Pointer to a struct bt_gatt_cpf.
961 */
962 #define BT_GATT_CPF(_value) \
963 BT_GATT_DESCRIPTOR(BT_UUID_GATT_CPF, BT_GATT_PERM_READ, \
964 bt_gatt_attr_read_cpf, NULL, (void *)_value)
965
966 /**
967 * @brief Descriptor Declaration Macro.
968 *
969 * Helper macro to declare a descriptor attribute.
970 *
971 * @param _uuid Descriptor attribute uuid.
972 * @param _perm Descriptor attribute access permissions,
973 * a bitmap of @ref bt_gatt_perm values.
974 * @param _read Descriptor attribute read callback
975 * (@ref bt_gatt_attr_read_func_t).
976 * @param _write Descriptor attribute write callback
977 * (@ref bt_gatt_attr_write_func_t).
978 * @param _user_data Descriptor attribute user data.
979 */
980 #define BT_GATT_DESCRIPTOR(_uuid, _perm, _read, _write, _user_data) \
981 BT_GATT_ATTRIBUTE(_uuid, _perm, _read, _write, _user_data)
982
983 /**
984 * @brief Attribute Declaration Macro.
985 *
986 * Helper macro to declare an attribute.
987 *
988 * @param _uuid Attribute uuid.
989 * @param _perm Attribute access permissions,
990 * a bitmap of @ref bt_gatt_perm values.
991 * @param _read Attribute read callback (@ref bt_gatt_attr_read_func_t).
992 * @param _write Attribute write callback (@ref bt_gatt_attr_write_func_t).
993 * @param _user_data Attribute user data.
994 */
995 #define BT_GATT_ATTRIBUTE(_uuid, _perm, _read, _write, _user_data) \
996 { \
997 .uuid = _uuid, \
998 .read = _read, \
999 .write = _write, \
1000 .user_data = _user_data, \
1001 .handle = 0, \
1002 .perm = _perm, \
1003 }
1004
1005 /** @brief Notification complete result callback.
1006 *
1007 * @param conn Connection object.
1008 * @param user_data Data passed in by the user.
1009 */
1010 typedef void (*bt_gatt_complete_func_t) (struct bt_conn *conn, void *user_data);
1011
1012 struct bt_gatt_notify_params {
1013 /** @brief Notification Attribute UUID type
1014 *
1015 * Optional, use to search for an attribute with matching UUID when
1016 * the attribute object pointer is not known.
1017 */
1018 const struct bt_uuid *uuid;
1019 /** @brief Notification Attribute object
1020 *
1021 * Optional if uuid is provided, in this case it will be used as start
1022 * range to search for the attribute with the given UUID.
1023 */
1024 const struct bt_gatt_attr *attr;
1025 /** Notification Value data */
1026 const void *data;
1027 /** Notification Value length */
1028 uint16_t len;
1029 /** Notification Value callback */
1030 bt_gatt_complete_func_t func;
1031 /** Notification Value callback user data */
1032 void *user_data;
1033 #if defined(CONFIG_BT_EATT)
1034 enum bt_att_chan_opt chan_opt;
1035 #endif /* CONFIG_BT_EATT */
1036 };
1037
1038 /** @brief Notify attribute value change.
1039 *
1040 * This function works in the same way as @ref bt_gatt_notify.
1041 * With the addition that after sending the notification the
1042 * callback function will be called.
1043 *
1044 * The callback is run from System Workqueue context.
1045 * When called from the System Workqueue context this API will not wait for
1046 * resources for the callback but instead return an error.
1047 * The number of pending callbacks can be increased with the
1048 * @kconfig{CONFIG_BT_CONN_TX_MAX} option.
1049 *
1050 * Alternatively it is possible to notify by UUID by setting it on the
1051 * parameters, when using this method the attribute if provided is used as the
1052 * start range when looking up for possible matches.
1053 *
1054 * @param conn Connection object.
1055 * @param params Notification parameters.
1056 *
1057 * @return 0 in case of success or negative value in case of error.
1058 */
1059 int bt_gatt_notify_cb(struct bt_conn *conn,
1060 struct bt_gatt_notify_params *params);
1061
1062 /** @brief Send multiple notifications in a single PDU.
1063 *
1064 * The GATT Server will send a single ATT_MULTIPLE_HANDLE_VALUE_NTF PDU
1065 * containing all the notifications passed to this API.
1066 *
1067 * All `params` must have the same `func` and `user_data` (due to
1068 * implementation limitation). But `func(user_data)` will be invoked for each
1069 * parameter.
1070 *
1071 * As this API may block to wait for Bluetooth Host resources, it is not
1072 * recommended to call it from a cooperative thread or a Bluetooth callback.
1073 *
1074 * The peer's GATT Client must write to this device's Client Supported Features
1075 * attribute and set the bit for Multiple Handle Value Notifications before
1076 * this API can be used.
1077 *
1078 * Only use this API to force the use of the ATT_MULTIPLE_HANDLE_VALUE_NTF PDU.
1079 * For standard applications, `bt_gatt_notify_cb` is preferred, as it will use
1080 * this PDU if supported and automatically fallback to ATT_HANDLE_VALUE_NTF
1081 * when not supported by the peer.
1082 *
1083 * This API has an additional limitation: it only accepts valid attribute
1084 * references and not UUIDs like `bt_gatt_notify` and `bt_gatt_notify_cb`.
1085 *
1086 * @param conn
1087 * Target client.
1088 * Notifying all connected clients by passing `NULL` is not yet supported,
1089 * please use `bt_gatt_notify` instead.
1090 * @param num_params
1091 * Element count of `params` array. Has to be greater than 1.
1092 * @param params
1093 * Array of notification parameters. It is okay to free this after calling
1094 * this function.
1095 *
1096 * @retval 0
1097 * Success. The PDU is queued for sending.
1098 * @retval -EINVAL
1099 * - One of the attribute handles is invalid.
1100 * - Only one parameter was passed. This API expects 2 or more.
1101 * - Not all `func` were equal or not all `user_data` were equal.
1102 * - One of the characteristics is not notifiable.
1103 * - An UUID was passed in one of the parameters.
1104 * @retval -ERANGE
1105 * - The notifications cannot all fit in a single ATT_MULTIPLE_HANDLE_VALUE_NTF.
1106 * - They exceed the MTU of all open ATT bearers.
1107 * @retval -EPERM
1108 * The connection has a lower security level than required by one of the
1109 * attributes.
1110 * @retval -EOPNOTSUPP
1111 * The peer hasn't yet communicated that it supports this PDU type.
1112 */
1113 int bt_gatt_notify_multiple(struct bt_conn *conn,
1114 uint16_t num_params,
1115 struct bt_gatt_notify_params params[]);
1116
1117 /** @brief Notify attribute value change.
1118 *
1119 * Send notification of attribute value change, if connection is NULL notify
1120 * all peer that have notification enabled via CCC otherwise do a direct
1121 * notification only the given connection.
1122 *
1123 * The attribute object on the parameters can be the so called Characteristic
1124 * Declaration, which is usually declared with BT_GATT_CHARACTERISTIC followed
1125 * by BT_GATT_CCC, or the Characteristic Value Declaration which is
1126 * automatically created after the Characteristic Declaration when using
1127 * BT_GATT_CHARACTERISTIC.
1128 *
1129 * @param conn Connection object.
1130 * @param attr Characteristic or Characteristic Value attribute.
1131 * @param data Pointer to Attribute data.
1132 * @param len Attribute value length.
1133 *
1134 * @return 0 in case of success or negative value in case of error.
1135 */
bt_gatt_notify(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * data,uint16_t len)1136 static inline int bt_gatt_notify(struct bt_conn *conn,
1137 const struct bt_gatt_attr *attr,
1138 const void *data, uint16_t len)
1139 {
1140 struct bt_gatt_notify_params params;
1141
1142 memset(¶ms, 0, sizeof(params));
1143
1144 params.attr = attr;
1145 params.data = data;
1146 params.len = len;
1147 #if defined(CONFIG_BT_EATT)
1148 params.chan_opt = BT_ATT_CHAN_OPT_NONE;
1149 #endif /* CONFIG_BT_EATT */
1150
1151 return bt_gatt_notify_cb(conn, ¶ms);
1152 }
1153
1154 /** @brief Notify attribute value change by UUID.
1155 *
1156 * Send notification of attribute value change, if connection is NULL notify
1157 * all peer that have notification enabled via CCC otherwise do a direct
1158 * notification only on the given connection.
1159 *
1160 * The attribute object is the starting point for the search of the UUID.
1161 *
1162 * @param conn Connection object.
1163 * @param uuid The UUID. If the server contains multiple services with the same
1164 * UUID, then the first occurrence, starting from the attr given,
1165 * is used.
1166 * @param attr Pointer to an attribute that serves as the starting point for
1167 * the search of a match for the UUID.
1168 * @param data Pointer to Attribute data.
1169 * @param len Attribute value length.
1170 *
1171 * @return 0 in case of success or negative value in case of error.
1172 */
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)1173 static inline int bt_gatt_notify_uuid(struct bt_conn *conn,
1174 const struct bt_uuid *uuid,
1175 const struct bt_gatt_attr *attr,
1176 const void *data, uint16_t len)
1177 {
1178 struct bt_gatt_notify_params params;
1179
1180 memset(¶ms, 0, sizeof(params));
1181
1182 params.uuid = uuid;
1183 params.attr = attr;
1184 params.data = data;
1185 params.len = len;
1186 #if defined(CONFIG_BT_EATT)
1187 params.chan_opt = BT_ATT_CHAN_OPT_NONE;
1188 #endif /* CONFIG_BT_EATT */
1189
1190 return bt_gatt_notify_cb(conn, ¶ms);
1191 }
1192
1193 /* Forward declaration of the bt_gatt_indicate_params structure */
1194 struct bt_gatt_indicate_params;
1195
1196 /** @typedef bt_gatt_indicate_func_t
1197 * @brief Indication complete result callback.
1198 *
1199 * @param conn Connection object.
1200 * @param params Indication params object.
1201 * @param err ATT error code
1202 */
1203 typedef void (*bt_gatt_indicate_func_t)(struct bt_conn *conn,
1204 struct bt_gatt_indicate_params *params,
1205 uint8_t err);
1206
1207 typedef void (*bt_gatt_indicate_params_destroy_t)(
1208 struct bt_gatt_indicate_params *params);
1209
1210 /** @brief GATT Indicate Value parameters */
1211 struct bt_gatt_indicate_params {
1212 /** @brief Indicate Attribute UUID type
1213 *
1214 * Optional, use to search for an attribute with matching UUID when
1215 * the attribute object pointer is not known.
1216 */
1217 const struct bt_uuid *uuid;
1218 /** @brief Indicate Attribute object
1219 *
1220 * Optional if uuid is provided, in this case it will be used as start
1221 * range to search for the attribute with the given UUID.
1222 */
1223 const struct bt_gatt_attr *attr;
1224 /** Indicate Value callback */
1225 bt_gatt_indicate_func_t func;
1226 /** Indicate operation complete callback */
1227 bt_gatt_indicate_params_destroy_t destroy;
1228 /** Indicate Value data*/
1229 const void *data;
1230 /** Indicate Value length*/
1231 uint16_t len;
1232 /** Private reference counter */
1233 uint8_t _ref;
1234 #if defined(CONFIG_BT_EATT)
1235 enum bt_att_chan_opt chan_opt;
1236 #endif /* CONFIG_BT_EATT */
1237 };
1238
1239 /** @brief Indicate attribute value change.
1240 *
1241 * Send an indication of attribute value change. if connection is NULL
1242 * indicate all peer that have notification enabled via CCC otherwise do a
1243 * direct indication only the given connection.
1244 *
1245 * The attribute object on the parameters can be the so called Characteristic
1246 * Declaration, which is usually declared with BT_GATT_CHARACTERISTIC followed
1247 * by BT_GATT_CCC, or the Characteristic Value Declaration which is
1248 * automatically created after the Characteristic Declaration when using
1249 * BT_GATT_CHARACTERISTIC.
1250 *
1251 * Alternatively it is possible to indicate by UUID by setting it on the
1252 * parameters, when using this method the attribute if provided is used as the
1253 * start range when looking up for possible matches.
1254 *
1255 * @note This procedure is asynchronous therefore the parameters need to
1256 * remains valid while it is active. The procedure is active until
1257 * the destroy callback is run.
1258 *
1259 * @param conn Connection object.
1260 * @param params Indicate parameters.
1261 *
1262 * @return 0 in case of success or negative value in case of error.
1263 */
1264 int bt_gatt_indicate(struct bt_conn *conn,
1265 struct bt_gatt_indicate_params *params);
1266
1267
1268 /** @brief Check if connection have subscribed to attribute
1269 *
1270 * Check if connection has subscribed to attribute value change.
1271 *
1272 * The attribute object can be the so called Characteristic Declaration,
1273 * which is usually declared with BT_GATT_CHARACTERISTIC followed
1274 * by BT_GATT_CCC, or the Characteristic Value Declaration which is
1275 * automatically created after the Characteristic Declaration when using
1276 * BT_GATT_CHARACTERISTIC, or the Client Characteristic Configuration
1277 * Descriptor (CCCD) which is created by BT_GATT_CCC.
1278 *
1279 * @param conn Connection object.
1280 * @param attr Attribute object.
1281 * @param ccc_type The subscription type, @ref BT_GATT_CCC_NOTIFY and/or
1282 * @ref BT_GATT_CCC_INDICATE.
1283 *
1284 * @return true if the attribute object has been subscribed.
1285 */
1286 bool bt_gatt_is_subscribed(struct bt_conn *conn,
1287 const struct bt_gatt_attr *attr, uint16_t ccc_type);
1288
1289 /** @brief Get ATT MTU for a connection
1290 *
1291 * Get negotiated ATT connection MTU, note that this does not equal the largest
1292 * amount of attribute data that can be transferred within a single packet.
1293 *
1294 * @param conn Connection object.
1295 *
1296 * @return MTU in bytes
1297 */
1298 uint16_t bt_gatt_get_mtu(struct bt_conn *conn);
1299
1300 /** @} */
1301
1302 /**
1303 * @defgroup bt_gatt_client GATT Client APIs
1304 * @ingroup bt_gatt
1305 * @{
1306 */
1307
1308 /** @brief GATT Exchange MTU parameters */
1309 struct bt_gatt_exchange_params {
1310 /** Response callback */
1311 void (*func)(struct bt_conn *conn, uint8_t err,
1312 struct bt_gatt_exchange_params *params);
1313 };
1314
1315 /** @brief Exchange MTU
1316 *
1317 * This client procedure can be used to set the MTU to the maximum possible
1318 * size the buffers can hold.
1319 *
1320 * @note Shall only be used once per connection.
1321 *
1322 * The Response comes in callback @p params->func. The callback is run from
1323 * the context specified by 'config BT_RECV_CONTEXT'.
1324 * @p params must remain valid until start of callback.
1325 *
1326 * This function will block while the ATT request queue is full, except when
1327 * called from the BT RX thread, as this would cause a deadlock.
1328 *
1329 * @param conn Connection object.
1330 * @param params Exchange MTU parameters.
1331 *
1332 * @retval 0 Successfully queued request. Will call @p params->func on
1333 * resolution.
1334 *
1335 * @retval -ENOMEM ATT request queue is full and blocking would cause deadlock.
1336 * Allow a pending request to resolve before retrying, or call this function
1337 * outside the BT RX thread to get blocking behavior. Queue size is controlled
1338 * by @kconfig{CONFIG_BT_L2CAP_TX_BUF_COUNT}.
1339 *
1340 * @retval -EALREADY The MTU exchange procedure has been already performed.
1341 */
1342 int bt_gatt_exchange_mtu(struct bt_conn *conn,
1343 struct bt_gatt_exchange_params *params);
1344
1345 struct bt_gatt_discover_params;
1346
1347 /** @typedef bt_gatt_discover_func_t
1348 * @brief Discover attribute callback function.
1349 *
1350 * @param conn Connection object.
1351 * @param attr Attribute found, or NULL if not found.
1352 * @param params Discovery parameters given.
1353 *
1354 * If discovery procedure has completed this callback will be called with
1355 * attr set to NULL. This will not happen if procedure was stopped by returning
1356 * BT_GATT_ITER_STOP.
1357 *
1358 * The attribute object as well as its UUID and value objects are temporary and
1359 * must be copied to in order to cache its information.
1360 * Only the following fields of the attribute contains valid information:
1361 * - uuid UUID representing the type of attribute.
1362 * - handle Handle in the remote database.
1363 * - user_data The value of the attribute, if the discovery type maps to an
1364 * ATT operation that provides this information. NULL otherwise.
1365 * See below.
1366 *
1367 * The effective type of @c attr->user_data is determined by @c params. Note
1368 * that the fields @c params->type and @c params->uuid are left unchanged by
1369 * the discovery procedure.
1370 *
1371 * @c params->type | @c params->uuid | Type of @c attr->user_data
1372 * -------------------------------------|-------------------------|---------------------------
1373 * @ref BT_GATT_DISCOVER_PRIMARY | any | @ref bt_gatt_service_val
1374 * @ref BT_GATT_DISCOVER_SECONDARY | any | @ref bt_gatt_service_val
1375 * @ref BT_GATT_DISCOVER_INCLUDE | any | @ref bt_gatt_include
1376 * @ref BT_GATT_DISCOVER_CHARACTERISTIC | any | @ref bt_gatt_chrc
1377 * @ref BT_GATT_DISCOVER_STD_CHAR_DESC | @ref BT_UUID_GATT_CEP | @ref bt_gatt_cep
1378 * @ref BT_GATT_DISCOVER_STD_CHAR_DESC | @ref BT_UUID_GATT_CCC | @ref bt_gatt_ccc
1379 * @ref BT_GATT_DISCOVER_STD_CHAR_DESC | @ref BT_UUID_GATT_SCC | @ref bt_gatt_scc
1380 * @ref BT_GATT_DISCOVER_STD_CHAR_DESC | @ref BT_UUID_GATT_CPF | @ref bt_gatt_cpf
1381 * @ref BT_GATT_DISCOVER_DESCRIPTOR | any | NULL
1382 * @ref BT_GATT_DISCOVER_ATTRIBUTE | any | NULL
1383 *
1384 * Also consider if using read-by-type instead of discovery is more convenient.
1385 * See @ref bt_gatt_read with @ref bt_gatt_read_params.handle_count set to
1386 * @c 0.
1387 *
1388 * @return BT_GATT_ITER_CONTINUE to continue discovery procedure.
1389 * @return BT_GATT_ITER_STOP to stop discovery procedure.
1390 */
1391 typedef uint8_t (*bt_gatt_discover_func_t)(struct bt_conn *conn,
1392 const struct bt_gatt_attr *attr,
1393 struct bt_gatt_discover_params *params);
1394
1395 /** GATT Discover types */
1396 enum {
1397 /** Discover Primary Services. */
1398 BT_GATT_DISCOVER_PRIMARY,
1399 /** Discover Secondary Services. */
1400 BT_GATT_DISCOVER_SECONDARY,
1401 /** Discover Included Services. */
1402 BT_GATT_DISCOVER_INCLUDE,
1403 /** @brief Discover Characteristic Values.
1404 *
1405 * Discover Characteristic Value and its properties.
1406 */
1407 BT_GATT_DISCOVER_CHARACTERISTIC,
1408 /** @brief Discover Descriptors.
1409 *
1410 * Discover Attributes which are not services or characteristics.
1411 *
1412 * @note The use of this type of discover is not recommended for
1413 * discovering in ranges across multiple services/characteristics
1414 * as it may incur in extra round trips.
1415 */
1416 BT_GATT_DISCOVER_DESCRIPTOR,
1417 /** @brief Discover Attributes.
1418 *
1419 * Discover Attributes of any type.
1420 *
1421 * @note The use of this type of discover is not recommended for
1422 * discovering in ranges across multiple services/characteristics
1423 * as it may incur in more round trips.
1424 */
1425 BT_GATT_DISCOVER_ATTRIBUTE,
1426 /** @brief Discover standard characteristic descriptor values.
1427 *
1428 * Discover standard characteristic descriptor values and their
1429 * properties.
1430 * Supported descriptors:
1431 * - Characteristic Extended Properties
1432 * - Client Characteristic Configuration
1433 * - Server Characteristic Configuration
1434 * - Characteristic Presentation Format
1435 */
1436 BT_GATT_DISCOVER_STD_CHAR_DESC,
1437 };
1438
1439 /** @brief GATT Discover Attributes parameters */
1440 struct bt_gatt_discover_params {
1441 /** Discover UUID type */
1442 const struct bt_uuid *uuid;
1443 /** Discover attribute callback */
1444 bt_gatt_discover_func_t func;
1445 union {
1446 struct {
1447 /** Include service attribute declaration handle */
1448 uint16_t attr_handle;
1449 /** Included service start handle */
1450 uint16_t start_handle;
1451 /** Included service end handle */
1452 uint16_t end_handle;
1453 } _included;
1454 /** Discover start handle */
1455 uint16_t start_handle;
1456 };
1457 /** Discover end handle */
1458 uint16_t end_handle;
1459 /** Discover type */
1460 uint8_t type;
1461 #if defined(CONFIG_BT_GATT_AUTO_DISCOVER_CCC) || defined(__DOXYGEN__)
1462 /** Only for stack-internal use, used for automatic discovery. */
1463 struct bt_gatt_subscribe_params *sub_params;
1464 #endif /* defined(CONFIG_BT_GATT_AUTO_DISCOVER_CCC) || defined(__DOXYGEN__) */
1465 #if defined(CONFIG_BT_EATT)
1466 enum bt_att_chan_opt chan_opt;
1467 #endif /* CONFIG_BT_EATT */
1468 };
1469
1470 /** @brief GATT Discover function
1471 *
1472 * This procedure is used by a client to discover attributes on a server.
1473 *
1474 * Primary Service Discovery: Procedure allows to discover primary services
1475 * either by Discover All Primary Services or
1476 * Discover Primary Services by Service UUID.
1477 * Include Service Discovery: Procedure allows to discover all Include Services
1478 * within specified range.
1479 * Characteristic Discovery: Procedure allows to discover all characteristics
1480 * within specified handle range as well as
1481 * discover characteristics with specified UUID.
1482 * Descriptors Discovery: Procedure allows to discover all characteristic
1483 * descriptors within specified range.
1484 *
1485 * For each attribute found the callback is called which can then decide
1486 * whether to continue discovering or stop.
1487 *
1488 * The Response comes in callback @p params->func. The callback is run from
1489 * the BT RX thread. @p params must remain valid until start of callback where
1490 * iter `attr` is `NULL` or callback will return `BT_GATT_ITER_STOP`.
1491 *
1492 * This function will block while the ATT request queue is full, except when
1493 * called from the BT RX thread, as this would cause a deadlock.
1494 *
1495 * @param conn Connection object.
1496 * @param params Discover parameters.
1497 *
1498 * @retval 0 Successfully queued request. Will call @p params->func on
1499 * resolution.
1500 *
1501 * @retval -ENOMEM ATT request queue is full and blocking would cause deadlock.
1502 * Allow a pending request to resolve before retrying, or call this function
1503 * outside the BT RX thread to get blocking behavior. Queue size is controlled
1504 * by @kconfig{CONFIG_BT_L2CAP_TX_BUF_COUNT}.
1505 */
1506 int bt_gatt_discover(struct bt_conn *conn,
1507 struct bt_gatt_discover_params *params);
1508
1509 struct bt_gatt_read_params;
1510
1511 /** @typedef bt_gatt_read_func_t
1512 * @brief Read callback function
1513 *
1514 * When reading using by_uuid, `params->start_handle` is the attribute handle
1515 * for this `data` item.
1516 *
1517 * @param conn Connection object.
1518 * @param err ATT error code.
1519 * @param params Read parameters used.
1520 * @param data Attribute value data. NULL means read has completed.
1521 * @param length Attribute value length.
1522 *
1523 * @return BT_GATT_ITER_CONTINUE if should continue to the next attribute.
1524 * @return BT_GATT_ITER_STOP to stop.
1525 */
1526 typedef uint8_t (*bt_gatt_read_func_t)(struct bt_conn *conn, uint8_t err,
1527 struct bt_gatt_read_params *params,
1528 const void *data, uint16_t length);
1529
1530 /** @brief GATT Read parameters */
1531 struct bt_gatt_read_params {
1532 /** Read attribute callback. */
1533 bt_gatt_read_func_t func;
1534 /** If equals to 1 single.handle and single.offset are used.
1535 * If greater than 1 multiple.handles are used.
1536 * If equals to 0 by_uuid is used for Read Using Characteristic UUID.
1537 */
1538 size_t handle_count;
1539 union {
1540 struct {
1541 /** Attribute handle. */
1542 uint16_t handle;
1543 /** Attribute data offset. */
1544 uint16_t offset;
1545 } single;
1546 struct {
1547 /** Attribute handles to read with Read Multiple
1548 * Characteristic Values.
1549 */
1550 uint16_t *handles;
1551 /** If true use Read Multiple Variable Length
1552 * Characteristic Values procedure.
1553 * The values of the set of attributes may be of
1554 * variable or unknown length.
1555 * If false use Read Multiple Characteristic Values
1556 * procedure.
1557 * The values of the set of attributes must be of a
1558 * known fixed length, with the exception of the last
1559 * value that can have a variable length.
1560 */
1561 bool variable;
1562 } multiple;
1563 struct {
1564 /** First requested handle number. */
1565 uint16_t start_handle;
1566 /** Last requested handle number. */
1567 uint16_t end_handle;
1568 /** 2 or 16 octet UUID. */
1569 const struct bt_uuid *uuid;
1570 } by_uuid;
1571 };
1572 #if defined(CONFIG_BT_EATT)
1573 enum bt_att_chan_opt chan_opt;
1574 #endif /* CONFIG_BT_EATT */
1575 };
1576
1577 /** @brief Read Attribute Value by handle
1578 *
1579 * This procedure read the attribute value and return it to the callback.
1580 *
1581 * When reading attributes by UUID the callback can be called multiple times
1582 * depending on how many instances of given the UUID exists with the
1583 * start_handle being updated for each instance.
1584 *
1585 * If an instance does contain a long value which cannot be read entirely the
1586 * caller will need to read the remaining data separately using the handle and
1587 * offset.
1588 *
1589 * The Response comes in callback @p params->func. The callback is run from
1590 * the context specified by 'config BT_RECV_CONTEXT'.
1591 * @p params must remain valid until start of callback.
1592 *
1593 * This function will block while the ATT request queue is full, except when
1594 * called from the BT RX thread, as this would cause a deadlock.
1595 *
1596 * @param conn Connection object.
1597 * @param params Read parameters.
1598 *
1599 * @retval 0 Successfully queued request. Will call @p params->func on
1600 * resolution.
1601 *
1602 * @retval -ENOMEM ATT request queue is full and blocking would cause deadlock.
1603 * Allow a pending request to resolve before retrying, or call this function
1604 * outside the BT RX thread to get blocking behavior. Queue size is controlled
1605 * by @kconfig{CONFIG_BT_L2CAP_TX_BUF_COUNT}.
1606 */
1607 int bt_gatt_read(struct bt_conn *conn, struct bt_gatt_read_params *params);
1608
1609 struct bt_gatt_write_params;
1610
1611 /** @typedef bt_gatt_write_func_t
1612 * @brief Write callback function
1613 *
1614 * @param conn Connection object.
1615 * @param err ATT error code.
1616 * @param params Write parameters used.
1617 */
1618 typedef void (*bt_gatt_write_func_t)(struct bt_conn *conn, uint8_t err,
1619 struct bt_gatt_write_params *params);
1620
1621 /** @brief GATT Write parameters */
1622 struct bt_gatt_write_params {
1623 /** Response callback */
1624 bt_gatt_write_func_t func;
1625 /** Attribute handle */
1626 uint16_t handle;
1627 /** Attribute data offset */
1628 uint16_t offset;
1629 /** Data to be written */
1630 const void *data;
1631 /** Length of the data */
1632 uint16_t length;
1633 #if defined(CONFIG_BT_EATT)
1634 enum bt_att_chan_opt chan_opt;
1635 #endif /* CONFIG_BT_EATT */
1636 };
1637
1638 /** @brief Write Attribute Value by handle
1639 *
1640 * The Response comes in callback @p params->func. The callback is run from
1641 * the context specified by 'config BT_RECV_CONTEXT'.
1642 * @p params must remain valid until start of callback.
1643 *
1644 * This function will block while the ATT request queue is full, except when
1645 * called from Bluetooth event context. When called from Bluetooth context,
1646 * this function will instead instead return `-ENOMEM` if it would block to
1647 * avoid a deadlock.
1648 *
1649 * @param conn Connection object.
1650 * @param params Write parameters.
1651 *
1652 * @retval 0 Successfully queued request. Will call @p params->func on
1653 * resolution.
1654 *
1655 * @retval -ENOMEM ATT request queue is full and blocking would cause deadlock.
1656 * Allow a pending request to resolve before retrying, or call this function
1657 * outside Bluetooth event context to get blocking behavior. Queue size is
1658 * controlled by @kconfig{CONFIG_BT_L2CAP_TX_BUF_COUNT}.
1659 */
1660 int bt_gatt_write(struct bt_conn *conn, struct bt_gatt_write_params *params);
1661
1662 /** @brief Write Attribute Value by handle without response with callback.
1663 *
1664 * This function works in the same way as @ref bt_gatt_write_without_response.
1665 * With the addition that after sending the write the callback function will be
1666 * called.
1667 *
1668 * The callback is run from System Workqueue context.
1669 * When called from the System Workqueue context this API will not wait for
1670 * resources for the callback but instead return an error.
1671 * The number of pending callbacks can be increased with the
1672 * @kconfig{CONFIG_BT_CONN_TX_MAX} option.
1673 *
1674 * @note By using a callback it also disable the internal flow control
1675 * which would prevent sending multiple commands without waiting for
1676 * their transmissions to complete, so if that is required the caller
1677 * shall not submit more data until the callback is called.
1678 *
1679 * This function will block while the ATT request queue is full, except when
1680 * called from the BT RX thread, as this would cause a deadlock.
1681 *
1682 * @param conn Connection object.
1683 * @param handle Attribute handle.
1684 * @param data Data to be written.
1685 * @param length Data length.
1686 * @param sign Whether to sign data
1687 * @param func Transmission complete callback.
1688 * @param user_data User data to be passed back to callback.
1689 *
1690 * @retval 0 Successfully queued request.
1691 *
1692 * @retval -ENOMEM ATT request queue is full and blocking would cause deadlock.
1693 * Allow a pending request to resolve before retrying, or call this function
1694 * outside the BT RX thread to get blocking behavior. Queue size is controlled
1695 * by @kconfig{CONFIG_BT_L2CAP_TX_BUF_COUNT}.
1696 */
1697 int bt_gatt_write_without_response_cb(struct bt_conn *conn, uint16_t handle,
1698 const void *data, uint16_t length,
1699 bool sign, bt_gatt_complete_func_t func,
1700 void *user_data);
1701
1702 /** @brief Write Attribute Value by handle without response
1703 *
1704 * This procedure write the attribute value without requiring an
1705 * acknowledgment that the write was successfully performed
1706 *
1707 * This function will block while the ATT request queue is full, except when
1708 * called from the BT RX thread, as this would cause a deadlock.
1709 *
1710 * @param conn Connection object.
1711 * @param handle Attribute handle.
1712 * @param data Data to be written.
1713 * @param length Data length.
1714 * @param sign Whether to sign data
1715 *
1716 * @retval 0 Successfully queued request.
1717 *
1718 * @retval -ENOMEM ATT request queue is full and blocking would cause deadlock.
1719 * Allow a pending request to resolve before retrying, or call this function
1720 * outside the BT RX thread to get blocking behavior. Queue size is controlled
1721 * by @kconfig{CONFIG_BT_L2CAP_TX_BUF_COUNT}.
1722 */
bt_gatt_write_without_response(struct bt_conn * conn,uint16_t handle,const void * data,uint16_t length,bool sign)1723 static inline int bt_gatt_write_without_response(struct bt_conn *conn,
1724 uint16_t handle, const void *data,
1725 uint16_t length, bool sign)
1726 {
1727 return bt_gatt_write_without_response_cb(conn, handle, data, length,
1728 sign, NULL, NULL);
1729 }
1730
1731 struct bt_gatt_subscribe_params;
1732
1733 /** @typedef bt_gatt_notify_func_t
1734 * @brief Notification callback function
1735 *
1736 * In the case of an empty notification, the @p data pointer will be non-NULL
1737 * while the @p length will be 0, which is due to the special case where
1738 * a @p data NULL pointer means unsubscribed.
1739 *
1740 * @param conn Connection object. May be NULL, indicating that the peer is
1741 * being unpaired
1742 * @param params Subscription parameters.
1743 * @param data Attribute value data. If NULL then subscription was removed.
1744 * @param length Attribute value length.
1745 *
1746 * @return BT_GATT_ITER_CONTINUE to continue receiving value notifications.
1747 * BT_GATT_ITER_STOP to unsubscribe from value notifications.
1748 */
1749 typedef uint8_t (*bt_gatt_notify_func_t)(struct bt_conn *conn,
1750 struct bt_gatt_subscribe_params *params,
1751 const void *data, uint16_t length);
1752
1753 /** @typedef bt_gatt_subscribe_func_t
1754 * @brief Subscription callback function
1755 *
1756 * @param conn Connection object.
1757 * @param err ATT error code.
1758 * @param params Subscription parameters used.
1759 */
1760 typedef void (*bt_gatt_subscribe_func_t)(struct bt_conn *conn, uint8_t err,
1761 struct bt_gatt_subscribe_params *params);
1762
1763 /** Subscription flags */
1764 enum {
1765 /** @brief Persistence flag
1766 *
1767 * If set, indicates that the subscription is not saved
1768 * on the GATT server side. Therefore, upon disconnection,
1769 * the subscription will be automatically removed
1770 * from the client's subscriptions list and
1771 * when the client reconnects, it will have to
1772 * issue a new subscription.
1773 */
1774 BT_GATT_SUBSCRIBE_FLAG_VOLATILE,
1775
1776 /** @brief No resubscribe flag
1777 *
1778 * By default when BT_GATT_SUBSCRIBE_FLAG_VOLATILE is unset, the
1779 * subscription will be automatically renewed when the client
1780 * reconnects, as a workaround for GATT servers that do not persist
1781 * subscriptions.
1782 *
1783 * This flag will disable the automatic resubscription. It is useful
1784 * if the application layer knows that the GATT server remembers
1785 * subscriptions from previous connections and wants to avoid renewing
1786 * the subscriptions.
1787 */
1788 BT_GATT_SUBSCRIBE_FLAG_NO_RESUB,
1789
1790 /** @brief Write pending flag
1791 *
1792 * If set, indicates write operation is pending waiting remote end to
1793 * respond.
1794 *
1795 * @note Internal use only.
1796 */
1797 BT_GATT_SUBSCRIBE_FLAG_WRITE_PENDING,
1798
1799 /** @brief Sent flag
1800 *
1801 * If set, indicates that a subscription request (CCC write) has
1802 * already been sent in the active connection.
1803 *
1804 * Used to avoid sending subscription requests multiple times when the
1805 * @kconfig{CONFIG_BT_GATT_AUTO_RESUBSCRIBE} quirk is enabled.
1806 *
1807 * @note Internal use only.
1808 */
1809 BT_GATT_SUBSCRIBE_FLAG_SENT,
1810
1811 BT_GATT_SUBSCRIBE_NUM_FLAGS
1812 };
1813
1814 /** @brief GATT Subscribe parameters */
1815 struct bt_gatt_subscribe_params {
1816 /** Notification value callback */
1817 bt_gatt_notify_func_t notify;
1818 /** Subscribe CCC write request response callback
1819 * If given, called with the subscription parameters given when subscribing
1820 */
1821 bt_gatt_subscribe_func_t subscribe;
1822
1823 /** @deprecated{subscribe CCC write response callback} */
1824 bt_gatt_write_func_t write;
1825 /** Subscribe value handle */
1826 uint16_t value_handle;
1827 /** Subscribe CCC handle */
1828 uint16_t ccc_handle;
1829 #if defined(CONFIG_BT_GATT_AUTO_DISCOVER_CCC) || defined(__DOXYGEN__)
1830 /** Subscribe End handle (for automatic discovery) */
1831 uint16_t end_handle;
1832 /** Discover parameters used when ccc_handle = 0 */
1833 struct bt_gatt_discover_params *disc_params;
1834 #endif /* defined(CONFIG_BT_GATT_AUTO_DISCOVER_CCC) || defined(__DOXYGEN__) */
1835 /** Subscribe value */
1836 uint16_t value;
1837 #if defined(CONFIG_BT_SMP)
1838 /** Minimum required security for received notification. Notifications
1839 * and indications received over a connection with a lower security
1840 * level are silently discarded.
1841 */
1842 bt_security_t min_security;
1843 #endif
1844 /** Subscription flags */
1845 ATOMIC_DEFINE(flags, BT_GATT_SUBSCRIBE_NUM_FLAGS);
1846
1847 sys_snode_t node;
1848 #if defined(CONFIG_BT_EATT)
1849 enum bt_att_chan_opt chan_opt;
1850 #endif /* CONFIG_BT_EATT */
1851 };
1852
1853 /** @brief Subscribe Attribute Value Notification
1854 *
1855 * This procedure subscribe to value notification using the Client
1856 * Characteristic Configuration handle.
1857 * If notification received subscribe value callback is called to return
1858 * notified value. One may then decide whether to unsubscribe directly from
1859 * this callback. Notification callback with NULL data will not be called if
1860 * subscription was removed by this method.
1861 *
1862 * The Response comes in callback @p params->subscribe. The callback is run from
1863 * the context specified by 'config BT_RECV_CONTEXT'.
1864 * The Notification callback @p params->notify is also called from the BT RX
1865 * thread.
1866 *
1867 * @note Notifications are asynchronous therefore the @p params must remain
1868 * valid while subscribed and cannot be reused for additional subscriptions
1869 * whilst active.
1870 *
1871 * This function will block while the ATT request queue is full, except when
1872 * called from the BT RX thread, as this would cause a deadlock.
1873 *
1874 * @param conn Connection object.
1875 * @param params Subscribe parameters.
1876 *
1877 * @retval 0 Successfully queued request. Will call @p params->write on
1878 * resolution.
1879 *
1880 * @retval -ENOMEM ATT request queue is full and blocking would cause deadlock.
1881 * Allow a pending request to resolve before retrying, or call this function
1882 * outside the BT RX thread to get blocking behavior. Queue size is controlled
1883 * by @kconfig{CONFIG_BT_L2CAP_TX_BUF_COUNT}.
1884 */
1885 int bt_gatt_subscribe(struct bt_conn *conn,
1886 struct bt_gatt_subscribe_params *params);
1887
1888 /** @brief Resubscribe Attribute Value Notification subscription
1889 *
1890 * Resubscribe to Attribute Value Notification when already subscribed from a
1891 * previous connection. The GATT server will remember subscription from
1892 * previous connections when bonded, so resubscribing can be done without
1893 * performing a new subscribe procedure after a power cycle.
1894 *
1895 * @note Notifications are asynchronous therefore the parameters need to
1896 * remain valid while subscribed.
1897 *
1898 * @param id Local identity (in most cases BT_ID_DEFAULT).
1899 * @param peer Remote address.
1900 * @param params Subscribe parameters.
1901 *
1902 * @return 0 in case of success or negative value in case of error.
1903 */
1904 int bt_gatt_resubscribe(uint8_t id, const bt_addr_le_t *peer,
1905 struct bt_gatt_subscribe_params *params);
1906
1907 /** @brief Unsubscribe Attribute Value Notification
1908 *
1909 * This procedure unsubscribe to value notification using the Client
1910 * Characteristic Configuration handle. Notification callback with NULL data
1911 * will be called if subscription was removed by this call, until then the
1912 * parameters cannot be reused.
1913 *
1914 * The Response comes in callback @p params->func. The callback is run from
1915 * the BT RX thread.
1916 *
1917 * This function will block while the ATT request queue is full, except when
1918 * called from the BT RX thread, as this would cause a deadlock.
1919 *
1920 * @param conn Connection object.
1921 * @param params Subscribe parameters.
1922 *
1923 * @retval 0 Successfully queued request. Will call @p params->write on
1924 * resolution.
1925 *
1926 * @retval -ENOMEM ATT request queue is full and blocking would cause deadlock.
1927 * Allow a pending request to resolve before retrying, or call this function
1928 * outside the BT RX thread to get blocking behavior. Queue size is controlled
1929 * by @kconfig{CONFIG_BT_L2CAP_TX_BUF_COUNT}.
1930 */
1931 int bt_gatt_unsubscribe(struct bt_conn *conn,
1932 struct bt_gatt_subscribe_params *params);
1933
1934 /** @brief Try to cancel the first pending request identified by @p params.
1935 *
1936 * This function does not release @p params for reuse. The usual callbacks
1937 * for the request still apply. A successful cancel simulates a
1938 * #BT_ATT_ERR_UNLIKELY response from the server.
1939 *
1940 * This function can cancel the following request functions:
1941 * - #bt_gatt_exchange_mtu
1942 * - #bt_gatt_discover
1943 * - #bt_gatt_read
1944 * - #bt_gatt_write
1945 * - #bt_gatt_subscribe
1946 * - #bt_gatt_unsubscribe
1947 *
1948 * @param conn The connection the request was issued on.
1949 * @param params The address `params` used in the request function call.
1950 */
1951 void bt_gatt_cancel(struct bt_conn *conn, void *params);
1952
1953 /** @} */
1954
1955 #ifdef __cplusplus
1956 }
1957 #endif
1958
1959 /**
1960 * @}
1961 */
1962
1963 #endif /* ZEPHYR_INCLUDE_BLUETOOTH_GATT_H_ */
1964