1 /* att_internal.h - Attribute protocol handling */
2 
3 /*
4  * Copyright (c) 2015-2016 Intel Corporation
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #define BT_EATT_PSM		0x27
10 #define BT_ATT_DEFAULT_LE_MTU	23
11 #define BT_ATT_TIMEOUT		K_SECONDS(30)
12 
13 #if BT_L2CAP_RX_MTU < CONFIG_BT_L2CAP_TX_MTU
14 #define BT_ATT_MTU BT_L2CAP_RX_MTU
15 #else
16 #define BT_ATT_MTU CONFIG_BT_L2CAP_TX_MTU
17 #endif
18 
19 struct bt_att_hdr {
20 	u8_t  code;
21 } __packed;
22 
23 #define BT_ATT_OP_ERROR_RSP			0x01
24 struct bt_att_error_rsp {
25 	u8_t  request;
26 	u16_t handle;
27 	u8_t  error;
28 } __packed;
29 
30 #define BT_ATT_OP_MTU_REQ			0x02
31 struct bt_att_exchange_mtu_req {
32 	u16_t mtu;
33 } __packed;
34 
35 #define BT_ATT_OP_MTU_RSP			0x03
36 struct bt_att_exchange_mtu_rsp {
37 	u16_t mtu;
38 } __packed;
39 
40 /* Find Information Request */
41 #define BT_ATT_OP_FIND_INFO_REQ			0x04
42 struct bt_att_find_info_req {
43 	u16_t start_handle;
44 	u16_t end_handle;
45 } __packed;
46 
47 /* Format field values for BT_ATT_OP_FIND_INFO_RSP */
48 #define BT_ATT_INFO_16				0x01
49 #define BT_ATT_INFO_128				0x02
50 
51 struct bt_att_info_16 {
52 	u16_t handle;
53 	u16_t uuid;
54 } __packed;
55 
56 struct bt_att_info_128 {
57 	u16_t handle;
58 	u8_t  uuid[16];
59 } __packed;
60 
61 /* Find Information Response */
62 #define BT_ATT_OP_FIND_INFO_RSP			0x05
63 struct bt_att_find_info_rsp {
64 	u8_t  format;
65 	u8_t  info[0];
66 } __packed;
67 
68 /* Find By Type Value Request */
69 #define BT_ATT_OP_FIND_TYPE_REQ			0x06
70 struct bt_att_find_type_req {
71 	u16_t start_handle;
72 	u16_t end_handle;
73 	u16_t type;
74 	u8_t  value[0];
75 } __packed;
76 
77 struct bt_att_handle_group {
78 	u16_t start_handle;
79 	u16_t end_handle;
80 } __packed;
81 
82 /* Find By Type Value Response */
83 #define BT_ATT_OP_FIND_TYPE_RSP			0x07
84 struct bt_att_find_type_rsp {
85 	struct bt_att_handle_group list[0];
86 } __packed;
87 
88 /* Read By Type Request */
89 #define BT_ATT_OP_READ_TYPE_REQ			0x08
90 struct bt_att_read_type_req {
91 	u16_t start_handle;
92 	u16_t end_handle;
93 	u8_t  uuid[0];
94 } __packed;
95 
96 struct bt_att_data {
97 	u16_t handle;
98 	u8_t  value[0];
99 } __packed;
100 
101 /* Read By Type Response */
102 #define BT_ATT_OP_READ_TYPE_RSP			0x09
103 struct bt_att_read_type_rsp {
104 	u8_t  len;
105 	struct bt_att_data data[0];
106 } __packed;
107 
108 /* Read Request */
109 #define BT_ATT_OP_READ_REQ			0x0a
110 struct bt_att_read_req {
111 	u16_t handle;
112 } __packed;
113 
114 /* Read Response */
115 #define BT_ATT_OP_READ_RSP			0x0b
116 struct bt_att_read_rsp {
117 	u8_t  value[0];
118 } __packed;
119 
120 /* Read Blob Request */
121 #define BT_ATT_OP_READ_BLOB_REQ			0x0c
122 struct bt_att_read_blob_req {
123 	u16_t handle;
124 	u16_t offset;
125 } __packed;
126 
127 /* Read Blob Response */
128 #define BT_ATT_OP_READ_BLOB_RSP			0x0d
129 struct bt_att_read_blob_rsp {
130 	u8_t  value[0];
131 } __packed;
132 
133 /* Read Multiple Request */
134 #define BT_ATT_READ_MULT_MIN_LEN_REQ		0x04
135 
136 #define BT_ATT_OP_READ_MULT_REQ			0x0e
137 struct bt_att_read_mult_req {
138 	u16_t handles[0];
139 } __packed;
140 
141 /* Read Multiple Respose */
142 #define BT_ATT_OP_READ_MULT_RSP			0x0f
143 struct bt_att_read_mult_rsp {
144 	u8_t  value[0];
145 } __packed;
146 
147 /* Read by Group Type Request */
148 #define BT_ATT_OP_READ_GROUP_REQ		0x10
149 struct bt_att_read_group_req {
150 	u16_t start_handle;
151 	u16_t end_handle;
152 	u8_t  uuid[0];
153 } __packed;
154 
155 struct bt_att_group_data {
156 	u16_t start_handle;
157 	u16_t end_handle;
158 	u8_t  value[0];
159 } __packed;
160 
161 /* Read by Group Type Response */
162 #define BT_ATT_OP_READ_GROUP_RSP		0x11
163 struct bt_att_read_group_rsp {
164 	u8_t  len;
165 	struct bt_att_group_data data[0];
166 } __packed;
167 
168 /* Write Request */
169 #define BT_ATT_OP_WRITE_REQ			0x12
170 struct bt_att_write_req {
171 	u16_t handle;
172 	u8_t  value[0];
173 } __packed;
174 
175 /* Write Response */
176 #define BT_ATT_OP_WRITE_RSP			0x13
177 
178 /* Prepare Write Request */
179 #define BT_ATT_OP_PREPARE_WRITE_REQ		0x16
180 struct bt_att_prepare_write_req {
181 	u16_t handle;
182 	u16_t offset;
183 	u8_t  value[0];
184 } __packed;
185 
186 /* Prepare Write Respond */
187 #define BT_ATT_OP_PREPARE_WRITE_RSP		0x17
188 struct bt_att_prepare_write_rsp {
189 	u16_t handle;
190 	u16_t offset;
191 	u8_t  value[0];
192 } __packed;
193 
194 /* Execute Write Request */
195 #define BT_ATT_FLAG_CANCEL			0x00
196 #define BT_ATT_FLAG_EXEC			0x01
197 
198 #define BT_ATT_OP_EXEC_WRITE_REQ		0x18
199 struct bt_att_exec_write_req {
200 	u8_t  flags;
201 } __packed;
202 
203 /* Execute Write Response */
204 #define BT_ATT_OP_EXEC_WRITE_RSP		0x19
205 
206 /* Handle Value Notification */
207 #define BT_ATT_OP_NOTIFY			0x1b
208 struct bt_att_notify {
209 	u16_t handle;
210 	u8_t  value[0];
211 } __packed;
212 
213 /* Handle Value Indication */
214 #define BT_ATT_OP_INDICATE			0x1d
215 struct bt_att_indicate {
216 	u16_t handle;
217 	u8_t  value[0];
218 } __packed;
219 
220 /* Handle Value Confirm */
221 #define BT_ATT_OP_CONFIRM			0x1e
222 
223 struct bt_att_signature {
224 	u8_t  value[12];
225 } __packed;
226 
227 #define BT_ATT_OP_READ_MULT_VL_REQ		0x20
228 struct bt_att_read_mult_vl_req {
229 	u16_t handles[0];
230 } __packed;
231 
232 /* Read Multiple Respose */
233 #define BT_ATT_OP_READ_MULT_VL_RSP		0x21
234 struct bt_att_read_mult_vl_rsp {
235 	u16_t len;
236 	u8_t  value[0];
237 } __packed;
238 
239 /* Handle Multiple Value Notification */
240 #define BT_ATT_OP_NOTIFY_MULT			0x23
241 struct bt_att_notify_mult {
242 	u16_t handle;
243 	u16_t len;
244 	u8_t  value[0];
245 } __packed;
246 
247 /* Write Command */
248 #define BT_ATT_OP_WRITE_CMD			0x52
249 struct bt_att_write_cmd {
250 	u16_t handle;
251 	u8_t  value[0];
252 } __packed;
253 
254 /* Signed Write Command */
255 #define BT_ATT_OP_SIGNED_WRITE_CMD		0xd2
256 struct bt_att_signed_write_cmd {
257 	u16_t handle;
258 	u8_t  value[0];
259 } __packed;
260 
261 typedef void (*bt_att_func_t)(struct bt_conn *conn, u8_t err,
262 			      const void *pdu, u16_t length,
263 			      void *user_data);
264 typedef void (*bt_att_destroy_t)(void *user_data);
265 
266 /* ATT request context */
267 struct bt_att_req {
268 	sys_snode_t node;
269 	bt_att_func_t func;
270 	bt_att_destroy_t destroy;
271 	struct net_buf_simple_state state;
272 	struct net_buf *buf;
273 #if defined(CONFIG_BT_SMP)
274 	bool retrying;
275 #endif /* CONFIG_BT_SMP */
276 	void *user_data;
277 	bool used;
278 };
279 
280 void att_sent(struct bt_conn *conn, void *user_data);
281 
282 void bt_att_init(void);
283 u16_t bt_att_get_mtu(struct bt_conn *conn);
284 struct net_buf *bt_att_create_pdu(struct bt_conn *conn, u8_t op,
285 				  size_t len);
286 
287 /* Allocate a new request */
288 struct bt_att_req *bt_att_req_alloc(k_timeout_t timeout);
289 
290 /* Free a request */
291 void bt_att_req_free(struct bt_att_req *req);
292 
293 /* Send ATT PDU over a connection */
294 int bt_att_send(struct bt_conn *conn, struct net_buf *buf, bt_conn_tx_cb_t cb,
295 		void *user_data);
296 
297 /* Send ATT Request over a connection */
298 int bt_att_req_send(struct bt_conn *conn, struct bt_att_req *req);
299 
300 /* Cancel ATT request */
301 void bt_att_req_cancel(struct bt_conn *conn, struct bt_att_req *req);
302 
303 /* Connect EATT channels */
304 int bt_eatt_connect(struct bt_conn *conn, u8_t num_channels);
305 
306 /* Disconnect EATT channels */
307 int bt_eatt_disconnect(struct bt_conn *conn);
308