Line data Source code
1 1 : /** @file
2 : * @brief Attribute Protocol handling.
3 : */
4 :
5 : /*
6 : * Copyright (c) 2016 Intel Corporation
7 : *
8 : * SPDX-License-Identifier: Apache-2.0
9 : */
10 : #ifndef ZEPHYR_INCLUDE_BLUETOOTH_ATT_H_
11 : #define ZEPHYR_INCLUDE_BLUETOOTH_ATT_H_
12 :
13 : /**
14 : * @brief Attribute Protocol (ATT)
15 : * @defgroup bt_att Attribute Protocol (ATT)
16 : * @ingroup bluetooth
17 : * @{
18 : */
19 :
20 : #include <stdint.h>
21 : #include <stddef.h>
22 :
23 : #include <zephyr/bluetooth/conn.h>
24 : #include <zephyr/sys/slist.h>
25 : #include <zephyr/sys/util_macro.h>
26 :
27 : #ifdef __cplusplus
28 : extern "C" {
29 : #endif
30 :
31 : /* Error codes for Error response PDU
32 : *
33 : * Defined by The Bluetooth Core Specification, Version 5.4, Vol 3, Part F, Section 3.4.1.1
34 : */
35 : /** The ATT operation was successful */
36 1 : #define BT_ATT_ERR_SUCCESS 0x00
37 : /** The attribute handle given was not valid on the server */
38 1 : #define BT_ATT_ERR_INVALID_HANDLE 0x01
39 : /** The attribute cannot be read */
40 1 : #define BT_ATT_ERR_READ_NOT_PERMITTED 0x02
41 : /** The attribute cannot be written */
42 1 : #define BT_ATT_ERR_WRITE_NOT_PERMITTED 0x03
43 : /** The attribute PDU was invalid */
44 1 : #define BT_ATT_ERR_INVALID_PDU 0x04
45 : /** The attribute requires authentication before it can be read or written */
46 1 : #define BT_ATT_ERR_AUTHENTICATION 0x05
47 : /** The ATT Server does not support the request received from the client */
48 1 : #define BT_ATT_ERR_NOT_SUPPORTED 0x06
49 : /** Offset specified was past the end of the attribute */
50 1 : #define BT_ATT_ERR_INVALID_OFFSET 0x07
51 : /** The attribute requires authorization before it can be read or written */
52 1 : #define BT_ATT_ERR_AUTHORIZATION 0x08
53 : /** Too many prepare writes have been queued */
54 1 : #define BT_ATT_ERR_PREPARE_QUEUE_FULL 0x09
55 : /** No attribute found within the given attribute handle range */
56 1 : #define BT_ATT_ERR_ATTRIBUTE_NOT_FOUND 0x0a
57 : /** The attribute cannot be read using the ATT_READ_BLOB_REQ PDU */
58 1 : #define BT_ATT_ERR_ATTRIBUTE_NOT_LONG 0x0b
59 : /** The Encryption Key Size used for encrypting this link is too short */
60 1 : #define BT_ATT_ERR_ENCRYPTION_KEY_SIZE 0x0c
61 : /** The attribute value length is invalid for the operation */
62 1 : #define BT_ATT_ERR_INVALID_ATTRIBUTE_LEN 0x0d
63 : /**
64 : * @brief The attribute request that was requested has encountered an error that was unlikely
65 : *
66 : * The attribute request could therefore not be completed as requested
67 : */
68 1 : #define BT_ATT_ERR_UNLIKELY 0x0e
69 : /** The attribute requires encryption before it can be read or written */
70 1 : #define BT_ATT_ERR_INSUFFICIENT_ENCRYPTION 0x0f
71 : /**
72 : * @brief The attribute type is not a supported grouping attribute
73 : *
74 : * The attribute type is not a supported grouping attribute as defined by a higher layer
75 : * specification.
76 : */
77 1 : #define BT_ATT_ERR_UNSUPPORTED_GROUP_TYPE 0x10
78 : /** Insufficient Resources to complete the request */
79 1 : #define BT_ATT_ERR_INSUFFICIENT_RESOURCES 0x11
80 : /** The server requests the client to rediscover the database */
81 1 : #define BT_ATT_ERR_DB_OUT_OF_SYNC 0x12
82 : /** The attribute parameter value was not allowed */
83 1 : #define BT_ATT_ERR_VALUE_NOT_ALLOWED 0x13
84 :
85 : /* Common Profile Error Codes
86 : *
87 : * Defined by the Supplement to the Bluetooth Core Specification (CSS), v11, Part B, Section 1.2.
88 : */
89 : /** Write Request Rejected */
90 1 : #define BT_ATT_ERR_WRITE_REQ_REJECTED 0xfc
91 : /** Client Characteristic Configuration Descriptor Improperly Configured */
92 1 : #define BT_ATT_ERR_CCC_IMPROPER_CONF 0xfd
93 : /** Procedure Already in Progress */
94 1 : #define BT_ATT_ERR_PROCEDURE_IN_PROGRESS 0xfe
95 : /** Out of Range */
96 1 : #define BT_ATT_ERR_OUT_OF_RANGE 0xff
97 :
98 : /* Version 5.2, Vol 3, Part F, 3.2.9 defines maximum attribute length to 512 */
99 0 : #define BT_ATT_MAX_ATTRIBUTE_LEN 512
100 :
101 : /* Handle 0x0000 is reserved for future use */
102 0 : #define BT_ATT_FIRST_ATTRIBUTE_HANDLE 0x0001
103 : /* 0xffff is defined as the maximum, and thus last, valid attribute handle */
104 0 : #define BT_ATT_LAST_ATTRIBUTE_HANDLE 0xffff
105 :
106 : /** Converts a ATT error to string.
107 : *
108 : * The error codes are described in the Bluetooth Core specification,
109 : * Vol 3, Part F, Section 3.4.1.1 and in
110 : * The Supplement to the Bluetooth Core Specification (CSS), v11,
111 : * Part B, Section 1.2.
112 : *
113 : * The ATT and GATT documentation found in Vol 4, Part F and
114 : * Part G describe when the different error codes are used.
115 : *
116 : * See also the defined BT_ATT_ERR_* macros.
117 : *
118 : * @return The string representation of the ATT error code.
119 : * If @kconfig{CONFIG_BT_ATT_ERR_TO_STR} is not enabled,
120 : * this just returns the empty string
121 : */
122 : #if defined(CONFIG_BT_ATT_ERR_TO_STR)
123 : const char *bt_att_err_to_str(uint8_t att_err);
124 : #else
125 : #include <zephyr/toolchain.h>
126 :
127 1 : static inline const char *bt_att_err_to_str(uint8_t att_err)
128 : {
129 : ARG_UNUSED(att_err);
130 :
131 : return "";
132 : }
133 : #endif
134 :
135 : #if defined(CONFIG_BT_EATT)
136 : #if defined(CONFIG_BT_TESTING)
137 :
138 : int bt_eatt_disconnect_one(struct bt_conn *conn);
139 :
140 : /* Reconfigure all EATT channels on connection */
141 : int bt_eatt_reconfigure(struct bt_conn *conn, uint16_t mtu);
142 :
143 : #endif /* CONFIG_BT_TESTING */
144 :
145 : /** @brief Connect Enhanced ATT channels
146 : *
147 : * Sends a series of Credit Based Connection Requests to connect @p num_channels
148 : * Enhanced ATT channels. The peer may have limited resources and fewer channels
149 : * may be created.
150 : *
151 : * @param conn The connection to send the request on
152 : * @param num_channels The number of Enhanced ATT bearers to request.
153 : * Must be in the range 1 - @kconfig{CONFIG_BT_EATT_MAX}, inclusive.
154 : *
155 : * @return 0 in case of success or negative value in case of error.
156 : * @retval -EINVAL if @p num_channels is not in the allowed range or @p conn is NULL.
157 : * @retval -ENOMEM if less than @p num_channels are allocated.
158 : * @retval 0 in case of success
159 : */
160 1 : int bt_eatt_connect(struct bt_conn *conn, size_t num_channels);
161 :
162 : /** @brief Get number of EATT channels connected.
163 : *
164 : * @param conn The connection to get the number of EATT channels for.
165 : *
166 : * @return The number of EATT channels connected.
167 : * Returns 0 if @p conn is NULL or not connected.
168 : */
169 1 : size_t bt_eatt_count(struct bt_conn *conn);
170 :
171 : #endif /* CONFIG_BT_EATT */
172 :
173 : /** @brief ATT channel option bit field values.
174 : * @note @ref BT_ATT_CHAN_OPT_UNENHANCED_ONLY and @ref BT_ATT_CHAN_OPT_ENHANCED_ONLY are mutually
175 : * exclusive and both bits may not be set.
176 : */
177 1 : enum bt_att_chan_opt {
178 : /** Both Enhanced and Unenhanced channels can be used */
179 : BT_ATT_CHAN_OPT_NONE = 0x0,
180 : /** Only Unenhanced channels will be used */
181 : BT_ATT_CHAN_OPT_UNENHANCED_ONLY = BIT(0),
182 : /** Only Enhanced channels will be used */
183 : BT_ATT_CHAN_OPT_ENHANCED_ONLY = BIT(1),
184 : };
185 :
186 : #ifdef __cplusplus
187 : }
188 : #endif
189 :
190 : /**
191 : * @}
192 : */
193 :
194 : #endif /* ZEPHYR_INCLUDE_BLUETOOTH_ATT_H_ */
|