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