1 /* att_internal.h - Attribute protocol handling */
2 
3 #include <zephyr/bluetooth/l2cap.h>
4 
5 /*
6  * Copyright (c) 2015-2016 Intel Corporation
7  *
8  * SPDX-License-Identifier: Apache-2.0
9  */
10 
11 #define BT_EATT_PSM		0x27
12 #define BT_ATT_DEFAULT_LE_MTU	23
13 #define BT_ATT_TIMEOUT_SEC	30
14 #define BT_ATT_TIMEOUT		K_SECONDS(BT_ATT_TIMEOUT_SEC)
15 
16 /* Local ATT Rx MTU
17  *
18  * This is the local 'Client Rx MTU'/'Server Rx MTU'. Core v5.3 Vol 3 Part F
19  * 3.4.2.2 requires that they are equal.
20  *
21  * The local ATT Server Rx MTU is limited to BT_L2CAP_TX_MTU because the GATT
22  * long attribute read protocol (Core v5.3 Vol 3 Part G 4.8.3) treats the ATT
23  * MTU as a promise about the read size. This requires the server to negotiate
24  * the ATT_MTU down to what it is actually able to send. This will unfortunately
25  * also limit how much the client is allowed to send.
26  */
27 #define BT_LOCAL_ATT_MTU_EATT MIN(BT_L2CAP_SDU_RX_MTU, BT_L2CAP_SDU_TX_MTU)
28 #define BT_LOCAL_ATT_MTU_UATT MIN(BT_L2CAP_RX_MTU, BT_L2CAP_TX_MTU)
29 
30 #define BT_ATT_BUF_SIZE MAX(BT_LOCAL_ATT_MTU_UATT, BT_LOCAL_ATT_MTU_EATT)
31 
32 struct bt_att_hdr {
33 	uint8_t  code;
34 } __packed;
35 
36 #define BT_ATT_OP_ERROR_RSP			0x01
37 struct bt_att_error_rsp {
38 	uint8_t  request;
39 	uint16_t handle;
40 	uint8_t  error;
41 } __packed;
42 
43 #define BT_ATT_OP_MTU_REQ			0x02
44 struct bt_att_exchange_mtu_req {
45 	uint16_t mtu;
46 } __packed;
47 
48 #define BT_ATT_OP_MTU_RSP			0x03
49 struct bt_att_exchange_mtu_rsp {
50 	uint16_t mtu;
51 } __packed;
52 
53 /* Find Information Request */
54 #define BT_ATT_OP_FIND_INFO_REQ			0x04
55 struct bt_att_find_info_req {
56 	uint16_t start_handle;
57 	uint16_t end_handle;
58 } __packed;
59 
60 /* Format field values for BT_ATT_OP_FIND_INFO_RSP */
61 #define BT_ATT_INFO_16				0x01
62 #define BT_ATT_INFO_128				0x02
63 
64 struct bt_att_info_16 {
65 	uint16_t handle;
66 	uint16_t uuid;
67 } __packed;
68 
69 struct bt_att_info_128 {
70 	uint16_t handle;
71 	uint8_t  uuid[16];
72 } __packed;
73 
74 /* Find Information Response */
75 #define BT_ATT_OP_FIND_INFO_RSP			0x05
76 struct bt_att_find_info_rsp {
77 	uint8_t  format;
78 	uint8_t  info[0];
79 } __packed;
80 
81 /* Find By Type Value Request */
82 #define BT_ATT_OP_FIND_TYPE_REQ			0x06
83 struct bt_att_find_type_req {
84 	uint16_t start_handle;
85 	uint16_t end_handle;
86 	uint16_t type;
87 	uint8_t  value[0];
88 } __packed;
89 
90 struct bt_att_handle_group {
91 	uint16_t start_handle;
92 	uint16_t end_handle;
93 } __packed;
94 
95 /* Find By Type Value Response */
96 #define BT_ATT_OP_FIND_TYPE_RSP			0x07
97 struct bt_att_find_type_rsp {
98 	struct bt_att_handle_group list[0];
99 } __packed;
100 
101 /* Read By Type Request */
102 #define BT_ATT_OP_READ_TYPE_REQ			0x08
103 struct bt_att_read_type_req {
104 	uint16_t start_handle;
105 	uint16_t end_handle;
106 	uint8_t  uuid[0];
107 } __packed;
108 
109 struct bt_att_data {
110 	uint16_t handle;
111 	uint8_t  value[0];
112 } __packed;
113 
114 /* Read By Type Response */
115 #define BT_ATT_OP_READ_TYPE_RSP			0x09
116 struct bt_att_read_type_rsp {
117 	uint8_t  len;
118 	struct bt_att_data data[0];
119 } __packed;
120 
121 /* Read Request */
122 #define BT_ATT_OP_READ_REQ			0x0a
123 struct bt_att_read_req {
124 	uint16_t handle;
125 } __packed;
126 
127 /* Read Response */
128 #define BT_ATT_OP_READ_RSP			0x0b
129 struct bt_att_read_rsp {
130 	uint8_t  value[0];
131 } __packed;
132 
133 /* Read Blob Request */
134 #define BT_ATT_OP_READ_BLOB_REQ			0x0c
135 struct bt_att_read_blob_req {
136 	uint16_t handle;
137 	uint16_t offset;
138 } __packed;
139 
140 /* Read Blob Response */
141 #define BT_ATT_OP_READ_BLOB_RSP			0x0d
142 struct bt_att_read_blob_rsp {
143 	uint8_t  value[0];
144 } __packed;
145 
146 /* Read Multiple Request */
147 #define BT_ATT_READ_MULT_MIN_LEN_REQ		0x04
148 
149 #define BT_ATT_OP_READ_MULT_REQ			0x0e
150 struct bt_att_read_mult_req {
151 	uint16_t handles[0];
152 } __packed;
153 
154 /* Read Multiple Response */
155 #define BT_ATT_OP_READ_MULT_RSP			0x0f
156 struct bt_att_read_mult_rsp {
157 	uint8_t  value[0];
158 } __packed;
159 
160 /* Read by Group Type Request */
161 #define BT_ATT_OP_READ_GROUP_REQ		0x10
162 struct bt_att_read_group_req {
163 	uint16_t start_handle;
164 	uint16_t end_handle;
165 	uint8_t  uuid[0];
166 } __packed;
167 
168 struct bt_att_group_data {
169 	uint16_t start_handle;
170 	uint16_t end_handle;
171 	uint8_t  value[0];
172 } __packed;
173 
174 /* Read by Group Type Response */
175 #define BT_ATT_OP_READ_GROUP_RSP		0x11
176 struct bt_att_read_group_rsp {
177 	uint8_t  len;
178 	struct bt_att_group_data data[0];
179 } __packed;
180 
181 /* Write Request */
182 #define BT_ATT_OP_WRITE_REQ			0x12
183 struct bt_att_write_req {
184 	uint16_t handle;
185 	uint8_t  value[0];
186 } __packed;
187 
188 /* Write Response */
189 #define BT_ATT_OP_WRITE_RSP			0x13
190 
191 /* Prepare Write Request */
192 #define BT_ATT_OP_PREPARE_WRITE_REQ		0x16
193 struct bt_att_prepare_write_req {
194 	uint16_t handle;
195 	uint16_t offset;
196 	uint8_t  value[0];
197 } __packed;
198 
199 /* Prepare Write Respond */
200 #define BT_ATT_OP_PREPARE_WRITE_RSP		0x17
201 struct bt_att_prepare_write_rsp {
202 	uint16_t handle;
203 	uint16_t offset;
204 	uint8_t  value[0];
205 } __packed;
206 
207 /* Execute Write Request */
208 #define BT_ATT_FLAG_CANCEL			0x00
209 #define BT_ATT_FLAG_EXEC			0x01
210 
211 #define BT_ATT_OP_EXEC_WRITE_REQ		0x18
212 struct bt_att_exec_write_req {
213 	uint8_t  flags;
214 } __packed;
215 
216 /* Execute Write Response */
217 #define BT_ATT_OP_EXEC_WRITE_RSP		0x19
218 
219 /* Handle Value Notification */
220 #define BT_ATT_OP_NOTIFY			0x1b
221 struct bt_att_notify {
222 	uint16_t handle;
223 	uint8_t  value[0];
224 } __packed;
225 
226 /* Handle Value Indication */
227 #define BT_ATT_OP_INDICATE			0x1d
228 struct bt_att_indicate {
229 	uint16_t handle;
230 	uint8_t  value[0];
231 } __packed;
232 
233 /* Handle Value Confirm */
234 #define BT_ATT_OP_CONFIRM			0x1e
235 
236 struct bt_att_signature {
237 	uint8_t  value[12];
238 } __packed;
239 
240 #define BT_ATT_OP_READ_MULT_VL_REQ		0x20
241 struct bt_att_read_mult_vl_req {
242 	uint16_t handles[0];
243 } __packed;
244 
245 /* Read Multiple Response */
246 #define BT_ATT_OP_READ_MULT_VL_RSP		0x21
247 struct bt_att_read_mult_vl_rsp {
248 	uint16_t len;
249 	uint8_t  value[0];
250 } __packed;
251 
252 /* Handle Multiple Value Notification */
253 #define BT_ATT_OP_NOTIFY_MULT			0x23
254 struct bt_att_notify_mult {
255 	uint16_t handle;
256 	uint16_t len;
257 	uint8_t  value[0];
258 } __packed;
259 
260 /* Write Command */
261 #define BT_ATT_OP_WRITE_CMD			0x52
262 struct bt_att_write_cmd {
263 	uint16_t handle;
264 	uint8_t  value[0];
265 } __packed;
266 
267 /* Signed Write Command */
268 #define BT_ATT_OP_SIGNED_WRITE_CMD		0xd2
269 struct bt_att_signed_write_cmd {
270 	uint16_t handle;
271 	uint8_t  value[0];
272 } __packed;
273 
274 typedef void (*bt_att_func_t)(struct bt_conn *conn, int err,
275 			      const void *pdu, uint16_t length,
276 			      void *user_data);
277 
278 typedef int (*bt_att_encode_t)(struct net_buf *buf, size_t len,
279 			       void *user_data);
280 
281 /* ATT request context */
282 struct bt_att_req {
283 	sys_snode_t node;
284 	bt_att_func_t func;
285 	struct net_buf *buf;
286 #if defined(CONFIG_BT_SMP)
287 	bt_att_encode_t encode;
288 	uint8_t retrying : 1;
289 	uint8_t att_op;
290 	size_t len;
291 #endif /* CONFIG_BT_SMP */
292 	void *user_data;
293 };
294 
295 void bt_att_init(void);
296 uint16_t bt_att_get_mtu(struct bt_conn *conn);
297 uint16_t bt_att_get_uatt_mtu(struct bt_conn *conn);
298 struct net_buf *bt_att_create_pdu(struct bt_conn *conn, uint8_t op,
299 				  size_t len);
300 
301 /* Allocate a new request */
302 struct bt_att_req *bt_att_req_alloc(k_timeout_t timeout);
303 
304 /* Free a request */
305 void bt_att_req_free(struct bt_att_req *req);
306 
307 /* Send ATT PDU over a connection */
308 int bt_att_send(struct bt_conn *conn, struct net_buf *buf);
309 
310 /* Send ATT Request over a connection */
311 int bt_att_req_send(struct bt_conn *conn, struct bt_att_req *req);
312 
313 /* Cancel ATT request */
314 void bt_att_req_cancel(struct bt_conn *conn, struct bt_att_req *req);
315 
316 /* Disconnect EATT channels */
317 int bt_eatt_disconnect(struct bt_conn *conn);
318 
319 /** @brief Find a pending ATT request by its user_data pointer.
320  *  @param conn The connection the request was issued on.
321  *  @param user_data The pointer value to look for.
322  *  @return The found request. NULL if not found.
323  */
324 struct bt_att_req *bt_att_find_req_by_user_data(struct bt_conn *conn, const void *user_data);
325 
326 /* Checks if only the fixed ATT channel is connected */
327 bool bt_att_fixed_chan_only(struct bt_conn *conn);
328 
329 /* Clear the out of sync flag on all channels */
330 void bt_att_clear_out_of_sync_sent(struct bt_conn *conn);
331 
332 /* Check if BT_ATT_ERR_DB_OUT_OF_SYNC has been sent on the fixed ATT channel */
333 bool bt_att_out_of_sync_sent_on_fixed(struct bt_conn *conn);
334 
335 typedef void (*bt_gatt_complete_func_t) (struct bt_conn *conn, void *user_data);
336 void bt_att_set_tx_meta_data(struct net_buf *buf, bt_gatt_complete_func_t func, void *user_data,
337 			     enum bt_att_chan_opt chan_opt);
338 void bt_att_increment_tx_meta_data_attr_count(struct net_buf *buf, uint16_t attr_count);
339 
340 bool bt_att_tx_meta_data_match(const struct net_buf *buf, bt_gatt_complete_func_t func,
341 			       const void *user_data, enum bt_att_chan_opt chan_opt);
342 
343 #if defined(CONFIG_BT_EATT)
344 #define BT_ATT_CHAN_OPT(_params) (_params)->chan_opt
345 #else
346 #define BT_ATT_CHAN_OPT(_params) BT_ATT_CHAN_OPT_UNENHANCED_ONLY
347 #endif /* CONFIG_BT_EATT */
348 
349 bool bt_att_chan_opt_valid(struct bt_conn *conn, enum bt_att_chan_opt chan_opt);
350 
351 void bt_gatt_req_set_mtu(struct bt_att_req *req, uint16_t mtu);
352