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 #define BT_ATT_FIRST_ATTTRIBUTE_HANDLE __DEPRECATED_MACRO BT_ATT_FIRST_ATTRIBUTE_HANDLE
100 /* 0xffff is defined as the maximum, and thus last, valid attribute handle */
101 #define BT_ATT_LAST_ATTRIBUTE_HANDLE            0xffff
102 #define BT_ATT_LAST_ATTTRIBUTE_HANDLE __DEPRECATED_MACRO BT_ATT_LAST_ATTRIBUTE_HANDLE
103 
104 #if defined(CONFIG_BT_EATT)
105 #if defined(CONFIG_BT_TESTING)
106 
107 int bt_eatt_disconnect_one(struct bt_conn *conn);
108 
109 /* Reconfigure all EATT channels on connection */
110 int bt_eatt_reconfigure(struct bt_conn *conn, uint16_t mtu);
111 
112 #endif /* CONFIG_BT_TESTING */
113 
114 /** @brief Connect Enhanced ATT channels
115  *
116  * Sends a series of Credit Based Connection Requests to connect @p num_channels
117  * Enhanced ATT channels. The peer may have limited resources and fewer channels
118  * may be created.
119  *
120  * @param conn The connection to send the request on
121  * @param num_channels The number of Enhanced ATT beares to request.
122  * Must be in the range 1 - @kconfig{CONFIG_BT_EATT_MAX}, inclusive.
123  *
124  * @return 0 in case of success or negative value in case of error.
125  * @retval -EINVAL if @p num_channels is not in the allowed range or @p conn is NULL.
126  * @retval -ENOMEM if less than @p num_channels are allocated.
127  * @retval 0 in case of success
128  */
129 int bt_eatt_connect(struct bt_conn *conn, size_t num_channels);
130 
131 /** @brief Get number of EATT channels connected.
132  *
133  * @param conn The connection to get the number of EATT channels for.
134  *
135  * @return The number of EATT channels connected.
136  * Returns 0 if @p conn is NULL or not connected.
137  */
138 size_t bt_eatt_count(struct bt_conn *conn);
139 
140 #endif /* CONFIG_BT_EATT */
141 
142 /** @brief ATT channel option bit field values.
143  * @note @ref BT_ATT_CHAN_OPT_UNENHANCED_ONLY and @ref BT_ATT_CHAN_OPT_ENHANCED_ONLY are mutually
144  * exclusive and both bits may not be set.
145  */
146 enum bt_att_chan_opt {
147 	/** Both Enhanced and Unenhanced channels can be used  */
148 	BT_ATT_CHAN_OPT_NONE = 0x0,
149 	/** Only Unenhanced channels will be used  */
150 	BT_ATT_CHAN_OPT_UNENHANCED_ONLY = BIT(0),
151 	/** Only Enhanced channels will be used  */
152 	BT_ATT_CHAN_OPT_ENHANCED_ONLY = BIT(1),
153 };
154 
155 #ifdef __cplusplus
156 }
157 #endif
158 
159 /**
160  * @}
161  */
162 
163 #endif /* ZEPHYR_INCLUDE_BLUETOOTH_ATT_H_ */
164