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