1 /*
2  * Audio Video Distribution Protocol
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  */
7 
8 #include <zephyr/kernel.h>
9 #include <string.h>
10 #include <strings.h>
11 #include <errno.h>
12 #include <zephyr/sys/atomic.h>
13 #include <zephyr/sys/byteorder.h>
14 #include <zephyr/sys/util.h>
15 
16 #include <zephyr/bluetooth/hci.h>
17 #include <zephyr/bluetooth/bluetooth.h>
18 #include <zephyr/bluetooth/l2cap.h>
19 #include <zephyr/bluetooth/classic/avdtp.h>
20 
21 #include "host/hci_core.h"
22 #include "host/conn_internal.h"
23 #include "l2cap_br_internal.h"
24 #include "avdtp_internal.h"
25 
26 #define LOG_LEVEL CONFIG_BT_AVDTP_LOG_LEVEL
27 #include <zephyr/logging/log.h>
28 LOG_MODULE_REGISTER(bt_avdtp);
29 
30 #define AVDTP_MSG_POISTION 0x00
31 #define AVDTP_PKT_POSITION 0x02
32 #define AVDTP_TID_POSITION 0x04
33 #define AVDTP_SIGID_MASK   0x3f
34 
35 #define AVDTP_GET_TR_ID(hdr)    ((hdr & 0xf0) >> AVDTP_TID_POSITION)
36 #define AVDTP_GET_MSG_TYPE(hdr) (hdr & 0x03)
37 #define AVDTP_GET_PKT_TYPE(hdr) ((hdr & 0x0c) >> AVDTP_PKT_POSITION)
38 #define AVDTP_GET_SIG_ID(s)     (s & AVDTP_SIGID_MASK)
39 
40 static struct bt_avdtp_event_cb *event_cb;
41 static sys_slist_t seps;
42 
43 #define AVDTP_CHAN(_ch) CONTAINER_OF(_ch, struct bt_avdtp, br_chan.chan)
44 
45 #define AVDTP_KWORK(_work)                                                                         \
46 	CONTAINER_OF(CONTAINER_OF(_work, struct k_work_delayable, work), struct bt_avdtp,          \
47 		     timeout_work)
48 
49 #define DISCOVER_REQ(_req) CONTAINER_OF(_req, struct bt_avdtp_discover_params, req)
50 #define GET_CAP_REQ(_req)  CONTAINER_OF(_req, struct bt_avdtp_get_capabilities_params, req)
51 #define SET_CONF_REQ(_req) CONTAINER_OF(_req, struct bt_avdtp_set_configuration_params, req)
52 #define CTRL_REQ(_req)     CONTAINER_OF(_req, struct bt_avdtp_ctrl_params, req)
53 
54 #define AVDTP_TIMEOUT K_SECONDS(6)
55 
56 K_SEM_DEFINE(avdtp_sem_lock, 1U, 1U);
57 
58 enum sep_state {
59 	AVDTP_IDLE = BIT(0),
60 	AVDTP_CONFIGURED = BIT(1),
61 	/* establishing the transport sessions. */
62 	AVDTP_OPENING = BIT(2),
63 	AVDTP_OPEN = BIT(3),
64 	AVDTP_STREAMING = BIT(4),
65 	AVDTP_CLOSING = BIT(5),
66 	AVDTP_ABORTING = BIT(6),
67 };
68 
avdtp_lock(struct bt_avdtp * session)69 static void avdtp_lock(struct bt_avdtp *session)
70 {
71 	k_sem_take(&session->sem_lock, K_FOREVER);
72 }
73 
avdtp_unlock(struct bt_avdtp * session)74 static void avdtp_unlock(struct bt_avdtp *session)
75 {
76 	k_sem_give(&session->sem_lock);
77 }
78 
avdtp_sep_lock(struct bt_avdtp_sep * sep)79 static void avdtp_sep_lock(struct bt_avdtp_sep *sep)
80 {
81 	if (sep != NULL) {
82 		k_sem_take(&sep->sem_lock, K_FOREVER);
83 	}
84 }
85 
avdtp_sep_unlock(struct bt_avdtp_sep * sep)86 static void avdtp_sep_unlock(struct bt_avdtp_sep *sep)
87 {
88 	if (sep != NULL) {
89 		k_sem_give(&sep->sem_lock);
90 	}
91 }
92 
bt_avdtp_set_state(struct bt_avdtp_sep * sep,uint8_t state)93 static void bt_avdtp_set_state(struct bt_avdtp_sep *sep, uint8_t state)
94 {
95 	sep->state = state;
96 
97 	if (state != AVDTP_IDLE) {
98 		sep->sep_info.inuse = 1U;
99 	} else {
100 		sep->sep_info.inuse = 0U;
101 	}
102 }
103 
bt_avdtp_set_state_lock(struct bt_avdtp_sep * sep,uint8_t state)104 static void bt_avdtp_set_state_lock(struct bt_avdtp_sep *sep, uint8_t state)
105 {
106 	avdtp_sep_lock(sep);
107 	bt_avdtp_set_state(sep, state);
108 	avdtp_sep_unlock(sep);
109 }
110 
bt_avdtp_clear_req(struct bt_avdtp * session)111 static inline void bt_avdtp_clear_req(struct bt_avdtp *session)
112 {
113 	avdtp_lock(session);
114 	session->req = NULL;
115 	avdtp_unlock(session);
116 }
117 
118 /* L2CAP Interface callbacks */
bt_avdtp_media_l2cap_connected(struct bt_l2cap_chan * chan)119 void bt_avdtp_media_l2cap_connected(struct bt_l2cap_chan *chan)
120 {
121 	struct bt_avdtp *session;
122 	struct bt_avdtp_sep *sep = CONTAINER_OF(chan, struct bt_avdtp_sep, chan.chan);
123 
124 	if (!chan) {
125 		LOG_ERR("Invalid AVDTP chan");
126 		return;
127 	}
128 
129 	session = sep->session;
130 	if (session == NULL) {
131 		return;
132 	}
133 
134 	LOG_DBG("chan %p session %p", chan, session);
135 	bt_avdtp_set_state_lock(sep, AVDTP_OPEN);
136 
137 	if (session->req != NULL) {
138 		struct bt_avdtp_req *req = session->req;
139 
140 		req->status = BT_AVDTP_SUCCESS;
141 		bt_avdtp_clear_req(session);
142 
143 		if (req->func != NULL) {
144 			req->func(req, NULL);
145 		}
146 	}
147 }
148 
bt_avdtp_media_l2cap_disconnected(struct bt_l2cap_chan * chan)149 void bt_avdtp_media_l2cap_disconnected(struct bt_l2cap_chan *chan)
150 {
151 	struct bt_avdtp *session;
152 	struct bt_avdtp_sep *sep = CONTAINER_OF(chan, struct bt_avdtp_sep, chan.chan);
153 
154 	session = sep->session;
155 	if (session == NULL) {
156 		return;
157 	}
158 
159 	LOG_DBG("chan %p", chan);
160 	chan->conn = NULL;
161 	avdtp_sep_lock(sep);
162 
163 	if ((sep->state == AVDTP_CLOSING) && (session->req != NULL) &&
164 	    (session->req->sig == BT_AVDTP_CLOSE)) {
165 		/* closing the stream */
166 		struct bt_avdtp_req *req = session->req;
167 
168 		bt_avdtp_set_state(sep, AVDTP_IDLE);
169 		avdtp_sep_unlock(sep);
170 		req->status = BT_AVDTP_SUCCESS;
171 		bt_avdtp_clear_req(session);
172 
173 		if (req->func != NULL) {
174 			req->func(req, NULL);
175 		}
176 	} else if (sep->state > AVDTP_OPENING) {
177 		bt_avdtp_set_state(sep, AVDTP_IDLE);
178 		avdtp_sep_unlock(sep);
179 		/* the l2cap is disconnected by other unexpected reasons */
180 		session->ops->stream_l2cap_disconnected(session, sep);
181 	} else {
182 		avdtp_sep_unlock(sep);
183 	}
184 }
185 
bt_avdtp_media_l2cap_recv(struct bt_l2cap_chan * chan,struct net_buf * buf)186 int bt_avdtp_media_l2cap_recv(struct bt_l2cap_chan *chan, struct net_buf *buf)
187 {
188 	/* media data is received */
189 	struct bt_avdtp_sep *sep = CONTAINER_OF(chan, struct bt_avdtp_sep, chan.chan);
190 
191 	if (sep->media_data_cb != NULL) {
192 		sep->media_data_cb(sep, buf);
193 	}
194 	return 0;
195 }
196 
197 static const struct bt_l2cap_chan_ops stream_chan_ops = {
198 	.connected = bt_avdtp_media_l2cap_connected,
199 	.disconnected = bt_avdtp_media_l2cap_disconnected,
200 	.recv = bt_avdtp_media_l2cap_recv,
201 };
202 
avdtp_media_connect(struct bt_avdtp * session,struct bt_avdtp_sep * sep)203 static int avdtp_media_connect(struct bt_avdtp *session, struct bt_avdtp_sep *sep)
204 {
205 	if (!session) {
206 		return -EINVAL;
207 	}
208 
209 	sep->session = session;
210 	sep->chan.rx.mtu = BT_L2CAP_RX_MTU;
211 	sep->chan.chan.ops = &stream_chan_ops;
212 	sep->chan.required_sec_level = BT_SECURITY_L2;
213 
214 	return bt_l2cap_chan_connect(session->br_chan.chan.conn, &sep->chan.chan,
215 				     BT_L2CAP_PSM_AVDTP);
216 }
217 
avdtp_media_disconnect(struct bt_avdtp_sep * sep)218 static int avdtp_media_disconnect(struct bt_avdtp_sep *sep)
219 {
220 	if (sep == NULL || sep->chan.chan.conn == NULL || sep->chan.chan.ops == NULL) {
221 		return -EINVAL;
222 	}
223 
224 	return bt_l2cap_chan_disconnect(&sep->chan.chan);
225 }
226 
avdtp_create_reply_pdu(uint8_t msg_type,uint8_t pkt_type,uint8_t sig_id,uint8_t tid)227 static struct net_buf *avdtp_create_reply_pdu(uint8_t msg_type, uint8_t pkt_type, uint8_t sig_id,
228 					      uint8_t tid)
229 {
230 	struct net_buf *buf;
231 	struct bt_avdtp_single_sig_hdr *hdr;
232 
233 	LOG_DBG("");
234 
235 	buf = bt_l2cap_create_pdu(NULL, 0);
236 	if (!buf) {
237 		LOG_ERR("Error: No Buff available");
238 		return NULL;
239 	}
240 
241 	hdr = net_buf_add(buf, sizeof(*hdr));
242 
243 	hdr->hdr = (msg_type | pkt_type << AVDTP_PKT_POSITION | tid << AVDTP_TID_POSITION);
244 	hdr->signal_id = sig_id & AVDTP_SIGID_MASK;
245 
246 	LOG_DBG("hdr = 0x%02X, Signal_ID = 0x%02X", hdr->hdr, hdr->signal_id);
247 	return buf;
248 }
249 
avdtp_set_status(struct bt_avdtp_req * req,struct net_buf * buf,uint8_t msg_type)250 static void avdtp_set_status(struct bt_avdtp_req *req, struct net_buf *buf, uint8_t msg_type)
251 {
252 	if (msg_type == BT_AVDTP_ACCEPT) {
253 		req->status = BT_AVDTP_SUCCESS;
254 	} else if (msg_type == BT_AVDTP_REJECT) {
255 		if (buf->len >= 1U) {
256 			req->status = net_buf_pull_u8(buf);
257 		} else {
258 			LOG_WRN("Invalid RSP frame");
259 			req->status = BT_AVDTP_BAD_LENGTH;
260 		}
261 	} else if (msg_type == BT_AVDTP_GEN_REJECT) {
262 		req->status = BT_AVDTP_NOT_SUPPORTED_COMMAND;
263 	} else {
264 		req->status = BT_AVDTP_BAD_HEADER_FORMAT;
265 	}
266 }
267 
avdtp_discover_cmd(struct bt_avdtp * session,struct net_buf * buf,uint8_t tid)268 static void avdtp_discover_cmd(struct bt_avdtp *session, struct net_buf *buf, uint8_t tid)
269 {
270 	int err;
271 	struct bt_avdtp_sep *sep;
272 	struct net_buf *rsp_buf;
273 	uint8_t error_code = 0;
274 
275 	if (session->ops->discovery_ind == NULL) {
276 		err = -ENOTSUP;
277 	} else {
278 		err = session->ops->discovery_ind(session, &error_code);
279 	}
280 
281 	rsp_buf = avdtp_create_reply_pdu(err ? BT_AVDTP_REJECT : BT_AVDTP_ACCEPT,
282 					 BT_AVDTP_PACKET_TYPE_SINGLE, BT_AVDTP_DISCOVER, tid);
283 	if (!rsp_buf) {
284 		return;
285 	}
286 
287 	if (err) {
288 		if (error_code == 0) {
289 			error_code = BT_AVDTP_BAD_STATE;
290 		}
291 
292 		LOG_DBG("discover err code:%d", error_code);
293 		net_buf_add_u8(rsp_buf, error_code);
294 	} else {
295 		struct bt_avdtp_sep_data sep_data;
296 
297 		SYS_SLIST_FOR_EACH_CONTAINER(&seps, sep, _node) {
298 			memset(&sep_data, 0, sizeof(sep_data));
299 			sep_data.inuse = sep->sep_info.inuse;
300 			sep_data.id = sep->sep_info.id;
301 			sep_data.tsep = sep->sep_info.tsep;
302 			sep_data.media_type = sep->sep_info.media_type;
303 			net_buf_add_mem(rsp_buf, &sep_data, sizeof(sep_data));
304 		}
305 	}
306 
307 	err = bt_l2cap_chan_send(&session->br_chan.chan, rsp_buf);
308 	if (err < 0) {
309 		net_buf_unref(rsp_buf);
310 		LOG_ERR("Error:L2CAP send fail - result = %d", err);
311 		return;
312 	}
313 }
314 
avdtp_discover_rsp(struct bt_avdtp * session,struct net_buf * buf,uint8_t msg_type)315 static void avdtp_discover_rsp(struct bt_avdtp *session, struct net_buf *buf, uint8_t msg_type)
316 {
317 	struct bt_avdtp_req *req = session->req;
318 
319 	if (req == NULL) {
320 		return;
321 	}
322 
323 	k_work_cancel_delayable(&session->timeout_work);
324 	avdtp_set_status(req, buf, msg_type);
325 	bt_avdtp_clear_req(session);
326 
327 	if (req->func != NULL) {
328 		req->func(req, buf);
329 	}
330 }
331 
avdtp_get_sep(uint8_t stream_endpoint_id)332 static struct bt_avdtp_sep *avdtp_get_sep(uint8_t stream_endpoint_id)
333 {
334 	struct bt_avdtp_sep *sep = NULL;
335 
336 	SYS_SLIST_FOR_EACH_CONTAINER(&seps, sep, _node) {
337 		if (sep->sep_info.id == stream_endpoint_id) {
338 			break;
339 		}
340 	}
341 
342 	return sep;
343 }
344 
avdtp_get_cmd_sep(struct net_buf * buf,uint8_t * error_code)345 static struct bt_avdtp_sep *avdtp_get_cmd_sep(struct net_buf *buf, uint8_t *error_code)
346 {
347 	struct bt_avdtp_sep *sep;
348 
349 	if (buf->len < 1U) {
350 		*error_code = BT_AVDTP_BAD_LENGTH;
351 		LOG_WRN("Invalid ACP SEID");
352 		return NULL;
353 	}
354 
355 	sep = avdtp_get_sep(net_buf_pull_u8(buf) >> 2);
356 	return sep;
357 }
358 
avdtp_get_capabilities_cmd(struct bt_avdtp * session,struct net_buf * buf,uint8_t tid)359 static void avdtp_get_capabilities_cmd(struct bt_avdtp *session, struct net_buf *buf, uint8_t tid)
360 {
361 	int err = 0;
362 	struct net_buf *rsp_buf;
363 	struct bt_avdtp_sep *sep;
364 	uint8_t error_code = 0;
365 
366 	sep = avdtp_get_cmd_sep(buf, &error_code);
367 
368 	if ((sep == NULL) || (session->ops->get_capabilities_ind == NULL)) {
369 		err = -ENOTSUP;
370 	} else {
371 		rsp_buf = avdtp_create_reply_pdu(BT_AVDTP_ACCEPT, BT_AVDTP_PACKET_TYPE_SINGLE,
372 						 BT_AVDTP_GET_CAPABILITIES, tid);
373 		if (!rsp_buf) {
374 			return;
375 		}
376 
377 		err = session->ops->get_capabilities_ind(session, sep, rsp_buf, &error_code);
378 		if (err) {
379 			net_buf_unref(rsp_buf);
380 		}
381 	}
382 
383 	if (err) {
384 		rsp_buf = avdtp_create_reply_pdu(BT_AVDTP_REJECT, BT_AVDTP_PACKET_TYPE_SINGLE,
385 						 BT_AVDTP_GET_CAPABILITIES, tid);
386 		if (!rsp_buf) {
387 			return;
388 		}
389 
390 		if (error_code == 0) {
391 			error_code = BT_AVDTP_BAD_ACP_SEID;
392 		}
393 
394 		LOG_DBG("get cap err code:%d", error_code);
395 		net_buf_add_u8(rsp_buf, error_code);
396 	}
397 
398 	err = bt_l2cap_chan_send(&session->br_chan.chan, rsp_buf);
399 	if (err < 0) {
400 		net_buf_unref(rsp_buf);
401 		LOG_ERR("Error:L2CAP send fail - result = %d", err);
402 		return;
403 	}
404 }
405 
avdtp_get_capabilities_rsp(struct bt_avdtp * session,struct net_buf * buf,uint8_t msg_type)406 static void avdtp_get_capabilities_rsp(struct bt_avdtp *session, struct net_buf *buf,
407 				       uint8_t msg_type)
408 {
409 	struct bt_avdtp_req *req = session->req;
410 
411 	if (req == NULL) {
412 		return;
413 	}
414 
415 	k_work_cancel_delayable(&session->timeout_work);
416 	avdtp_set_status(req, buf, msg_type);
417 	bt_avdtp_clear_req(session);
418 
419 	if (req->func != NULL) {
420 		req->func(req, buf);
421 	}
422 }
423 
avdtp_process_configuration_cmd(struct bt_avdtp * session,struct net_buf * buf,uint8_t tid,bool reconfig)424 static void avdtp_process_configuration_cmd(struct bt_avdtp *session, struct net_buf *buf,
425 					    uint8_t tid, bool reconfig)
426 {
427 	int err = 0;
428 	int ret;
429 	struct bt_avdtp_sep *sep;
430 	struct net_buf *rsp_buf;
431 	uint8_t error_code = 0;
432 
433 	sep = avdtp_get_cmd_sep(buf, &error_code);
434 	avdtp_sep_lock(sep);
435 
436 	if (sep == NULL) {
437 		err = -ENOTSUP;
438 	} else if (!reconfig && session->ops->set_configuration_ind == NULL) {
439 		err = -ENOTSUP;
440 	} else if (reconfig && session->ops->re_configuration_ind == NULL) {
441 		err = -ENOTSUP;
442 	} else {
443 		uint8_t expected_state;
444 
445 		if (reconfig) {
446 			expected_state = AVDTP_OPEN | AVDTP_OPENING;
447 		} else {
448 			expected_state = AVDTP_IDLE;
449 		}
450 
451 		if (!(sep->state & expected_state)) {
452 			err = -ENOTSUP;
453 			error_code = BT_AVDTP_BAD_STATE;
454 		} else if (buf->len >= 1U) {
455 			uint8_t int_seid;
456 
457 			/* INT Stream Endpoint ID */
458 			int_seid = net_buf_pull_u8(buf) >> 2;
459 
460 			if (!reconfig) {
461 				err = session->ops->set_configuration_ind(session, sep, int_seid,
462 									  buf, &error_code);
463 			} else {
464 				err = session->ops->re_configuration_ind(session, sep, int_seid,
465 									 buf, &error_code);
466 			}
467 		} else {
468 			LOG_WRN("Invalid INT SEID");
469 			err = -ENOTSUP;
470 			error_code = BT_AVDTP_BAD_LENGTH;
471 		}
472 	}
473 
474 	rsp_buf = avdtp_create_reply_pdu(
475 		err ? BT_AVDTP_REJECT : BT_AVDTP_ACCEPT, BT_AVDTP_PACKET_TYPE_SINGLE,
476 		reconfig ? BT_AVDTP_RECONFIGURE : BT_AVDTP_SET_CONFIGURATION, tid);
477 	if (!rsp_buf) {
478 		avdtp_sep_unlock(sep);
479 		return;
480 	}
481 
482 	if (err) {
483 		if (error_code == 0) {
484 			error_code = BT_AVDTP_BAD_ACP_SEID;
485 		}
486 
487 		LOG_DBG("set configuration err code:%d", error_code);
488 		/* Service Category: Media Codec */
489 		net_buf_add_u8(rsp_buf, BT_AVDTP_SERVICE_MEDIA_CODEC);
490 		/* Length Of Service Capability */
491 		net_buf_add_u8(rsp_buf, 0);
492 		/* ERROR CODE */
493 		net_buf_add_u8(rsp_buf, error_code);
494 	}
495 
496 	ret = bt_l2cap_chan_send(&session->br_chan.chan, rsp_buf);
497 	if (ret) {
498 		net_buf_unref(rsp_buf);
499 		LOG_ERR("Error:L2CAP send fail - result = %d", ret);
500 	}
501 
502 	if (!reconfig && !err && !ret) {
503 		bt_avdtp_set_state(sep, AVDTP_CONFIGURED);
504 	}
505 
506 	avdtp_sep_unlock(sep);
507 }
508 
avdtp_process_configuration_rsp(struct bt_avdtp * session,struct net_buf * buf,uint8_t msg_type,bool reconfig)509 static void avdtp_process_configuration_rsp(struct bt_avdtp *session, struct net_buf *buf,
510 					    uint8_t msg_type, bool reconfig)
511 {
512 	struct bt_avdtp_req *req = session->req;
513 
514 	if (req == NULL) {
515 		return;
516 	}
517 
518 	k_work_cancel_delayable(&session->timeout_work);
519 
520 	if (msg_type == BT_AVDTP_ACCEPT) {
521 		if (!reconfig) {
522 			bt_avdtp_set_state_lock(SET_CONF_REQ(req)->sep, AVDTP_CONFIGURED);
523 		}
524 	} else if (msg_type == BT_AVDTP_REJECT) {
525 		if (buf->len < 1U) {
526 			LOG_WRN("Invalid RSP frame");
527 			req->status = BT_AVDTP_BAD_LENGTH;
528 		} else {
529 			/* Service Category */
530 			net_buf_pull_u8(buf);
531 		}
532 	}
533 
534 	if (req->status == BT_AVDTP_SUCCESS) {
535 		avdtp_set_status(req, buf, msg_type);
536 	}
537 
538 	bt_avdtp_clear_req(session);
539 
540 	if (req->func != NULL) {
541 		req->func(req, NULL);
542 	}
543 }
544 
avdtp_set_configuration_cmd(struct bt_avdtp * session,struct net_buf * buf,uint8_t tid)545 static void avdtp_set_configuration_cmd(struct bt_avdtp *session, struct net_buf *buf, uint8_t tid)
546 {
547 	avdtp_process_configuration_cmd(session, buf, tid, false);
548 }
549 
avdtp_set_configuration_rsp(struct bt_avdtp * session,struct net_buf * buf,uint8_t msg_type)550 static void avdtp_set_configuration_rsp(struct bt_avdtp *session, struct net_buf *buf,
551 					uint8_t msg_type)
552 {
553 	avdtp_process_configuration_rsp(session, buf, msg_type, false);
554 }
555 
avdtp_re_configure_cmd(struct bt_avdtp * session,struct net_buf * buf,uint8_t tid)556 static void avdtp_re_configure_cmd(struct bt_avdtp *session, struct net_buf *buf, uint8_t tid)
557 {
558 	avdtp_process_configuration_cmd(session, buf, tid, true);
559 }
560 
avdtp_re_configure_rsp(struct bt_avdtp * session,struct net_buf * buf,uint8_t msg_type)561 static void avdtp_re_configure_rsp(struct bt_avdtp *session, struct net_buf *buf, uint8_t msg_type)
562 {
563 	avdtp_process_configuration_rsp(session, buf, msg_type, true);
564 }
565 
avdtp_open_cmd(struct bt_avdtp * session,struct net_buf * buf,uint8_t tid)566 static void avdtp_open_cmd(struct bt_avdtp *session, struct net_buf *buf, uint8_t tid)
567 {
568 	int err = 0;
569 	int ret;
570 	struct bt_avdtp_sep *sep;
571 	struct net_buf *rsp_buf;
572 	uint8_t error_code = 0;
573 
574 	sep = avdtp_get_cmd_sep(buf, &error_code);
575 	avdtp_sep_lock(sep);
576 
577 	if ((sep == NULL) || (session->ops->open_ind == NULL)) {
578 		err = -ENOTSUP;
579 	} else {
580 		if (sep->state != AVDTP_CONFIGURED) {
581 			err = -ENOTSUP;
582 			error_code = BT_AVDTP_BAD_STATE;
583 		} else {
584 			err = session->ops->open_ind(session, sep, &error_code);
585 		}
586 	}
587 
588 	rsp_buf = avdtp_create_reply_pdu(err ? BT_AVDTP_REJECT : BT_AVDTP_ACCEPT,
589 					 BT_AVDTP_PACKET_TYPE_SINGLE, BT_AVDTP_OPEN, tid);
590 	if (!rsp_buf) {
591 		avdtp_sep_unlock(sep);
592 		return;
593 	}
594 
595 	if (err) {
596 		if (error_code == 0) {
597 			error_code = BT_AVDTP_BAD_ACP_SEID;
598 		}
599 
600 		LOG_DBG("open_ind err code:%d", error_code);
601 		net_buf_add_u8(rsp_buf, error_code);
602 	} else {
603 		session->current_sep = sep;
604 	}
605 
606 	ret = bt_l2cap_chan_send(&session->br_chan.chan, rsp_buf);
607 	if (ret) {
608 		net_buf_unref(rsp_buf);
609 		LOG_ERR("Error:L2CAP send fail - result = %d", ret);
610 	}
611 
612 	if (!err && !ret) {
613 		bt_avdtp_set_state(sep, AVDTP_OPENING);
614 	}
615 
616 	avdtp_sep_unlock(sep);
617 }
618 
avdtp_open_rsp(struct bt_avdtp * session,struct net_buf * buf,uint8_t msg_type)619 static void avdtp_open_rsp(struct bt_avdtp *session, struct net_buf *buf, uint8_t msg_type)
620 {
621 	struct bt_avdtp_req *req = session->req;
622 
623 	if (req == NULL) {
624 		return;
625 	}
626 
627 	k_work_cancel_delayable(&session->timeout_work);
628 	avdtp_set_status(req, buf, msg_type);
629 
630 	if (req->status == BT_AVDTP_SUCCESS) {
631 		bt_avdtp_set_state_lock(CTRL_REQ(req)->sep, AVDTP_OPENING);
632 
633 		/* wait the media l2cap is established */
634 		if (!avdtp_media_connect(session, CTRL_REQ(req)->sep)) {
635 			return;
636 		}
637 	}
638 
639 	if (req->status != BT_AVDTP_SUCCESS) {
640 		bt_avdtp_clear_req(session);
641 
642 		if (req->func != NULL) {
643 			req->func(req, NULL);
644 		}
645 	}
646 }
647 
avdtp_handle_reject(struct net_buf * buf,struct bt_avdtp_req * req)648 static void avdtp_handle_reject(struct net_buf *buf, struct bt_avdtp_req *req)
649 {
650 	if (buf->len > 1U) {
651 		uint8_t acp_seid;
652 
653 		acp_seid = net_buf_pull_u8(buf);
654 
655 		if (acp_seid != CTRL_REQ(req)->acp_stream_ep_id) {
656 			req->status = BT_AVDTP_BAD_ACP_SEID;
657 		}
658 	} else {
659 		req->status = BT_AVDTP_BAD_LENGTH;
660 	}
661 }
662 
avdtp_start_cmd(struct bt_avdtp * session,struct net_buf * buf,uint8_t tid)663 static void avdtp_start_cmd(struct bt_avdtp *session, struct net_buf *buf, uint8_t tid)
664 {
665 	int err = 0;
666 	int ret;
667 	struct bt_avdtp_sep *sep;
668 	struct net_buf *rsp_buf;
669 	uint8_t error_code = 0;
670 
671 	sep = avdtp_get_cmd_sep(buf, &error_code);
672 	avdtp_sep_lock(sep);
673 
674 	if ((sep == NULL) || (session->ops->start_ind == NULL)) {
675 		err = -ENOTSUP;
676 	} else {
677 		if (sep->state != AVDTP_OPEN) {
678 			err = -ENOTSUP;
679 			error_code = BT_AVDTP_BAD_STATE;
680 		} else {
681 			err = session->ops->start_ind(session, sep, &error_code);
682 		}
683 	}
684 
685 	rsp_buf = avdtp_create_reply_pdu(err ? BT_AVDTP_REJECT : BT_AVDTP_ACCEPT,
686 					 BT_AVDTP_PACKET_TYPE_SINGLE, BT_AVDTP_START, tid);
687 	if (!rsp_buf) {
688 		avdtp_sep_unlock(sep);
689 		return;
690 	}
691 
692 	if (err) {
693 		if (error_code == 0) {
694 			error_code = BT_AVDTP_BAD_ACP_SEID;
695 		}
696 
697 		LOG_DBG("start err code:%d", error_code);
698 		net_buf_add_u8(rsp_buf, error_code);
699 	}
700 
701 	ret = bt_l2cap_chan_send(&session->br_chan.chan, rsp_buf);
702 	if (ret) {
703 		net_buf_unref(rsp_buf);
704 		LOG_ERR("Error:L2CAP send fail - result = %d", ret);
705 	}
706 
707 	if (!err && !ret) {
708 		bt_avdtp_set_state(sep, AVDTP_STREAMING);
709 	}
710 
711 	avdtp_sep_unlock(sep);
712 }
713 
avdtp_start_rsp(struct bt_avdtp * session,struct net_buf * buf,uint8_t msg_type)714 static void avdtp_start_rsp(struct bt_avdtp *session, struct net_buf *buf, uint8_t msg_type)
715 {
716 	struct bt_avdtp_req *req = session->req;
717 
718 	if (req == NULL) {
719 		return;
720 	}
721 
722 	k_work_cancel_delayable(&session->timeout_work);
723 
724 	if (msg_type == BT_AVDTP_ACCEPT) {
725 		bt_avdtp_set_state_lock(CTRL_REQ(req)->sep, AVDTP_STREAMING);
726 	} else if (msg_type == BT_AVDTP_REJECT) {
727 		avdtp_handle_reject(buf, req);
728 	}
729 
730 	if (req->status == BT_AVDTP_SUCCESS) {
731 		avdtp_set_status(req, buf, msg_type);
732 	}
733 
734 	bt_avdtp_clear_req(session);
735 
736 	if (req->func != NULL) {
737 		req->func(req, NULL);
738 	}
739 }
740 
avdtp_close_cmd(struct bt_avdtp * session,struct net_buf * buf,uint8_t tid)741 static void avdtp_close_cmd(struct bt_avdtp *session, struct net_buf *buf, uint8_t tid)
742 {
743 	int err = 0;
744 	int ret;
745 	struct bt_avdtp_sep *sep;
746 	struct net_buf *rsp_buf;
747 	uint8_t error_code = 0;
748 
749 	sep = avdtp_get_cmd_sep(buf, &error_code);
750 	avdtp_sep_lock(sep);
751 
752 	if ((sep == NULL) || (session->ops->close_ind == NULL)) {
753 		err = -ENOTSUP;
754 	} else {
755 		if (!(sep->state & (AVDTP_OPEN | AVDTP_STREAMING))) {
756 			err = -ENOTSUP;
757 			error_code = BT_AVDTP_BAD_STATE;
758 		} else {
759 			err = session->ops->close_ind(session, sep, &error_code);
760 		}
761 	}
762 
763 	rsp_buf = avdtp_create_reply_pdu(err ? BT_AVDTP_REJECT : BT_AVDTP_ACCEPT,
764 					 BT_AVDTP_PACKET_TYPE_SINGLE, BT_AVDTP_CLOSE, tid);
765 	if (!rsp_buf) {
766 		avdtp_sep_unlock(sep);
767 		return;
768 	}
769 
770 	if (err) {
771 		if (error_code == 0) {
772 			error_code = BT_AVDTP_BAD_ACP_SEID;
773 		}
774 
775 		LOG_DBG("close err code:%d", error_code);
776 		net_buf_add_u8(rsp_buf, error_code);
777 	} else {
778 		bt_avdtp_set_state(sep, AVDTP_CLOSING);
779 	}
780 
781 	ret = bt_l2cap_chan_send(&session->br_chan.chan, rsp_buf);
782 	if (ret) {
783 		net_buf_unref(rsp_buf);
784 		LOG_ERR("Error:L2CAP send fail - result = %d", ret);
785 	}
786 
787 	if (!err && !ret) {
788 		bt_avdtp_set_state(sep, AVDTP_IDLE);
789 	}
790 
791 	avdtp_sep_unlock(sep);
792 }
793 
avdtp_close_rsp(struct bt_avdtp * session,struct net_buf * buf,uint8_t msg_type)794 static void avdtp_close_rsp(struct bt_avdtp *session, struct net_buf *buf, uint8_t msg_type)
795 {
796 	struct bt_avdtp_req *req = session->req;
797 
798 	if (req == NULL) {
799 		return;
800 	}
801 
802 	k_work_cancel_delayable(&session->timeout_work);
803 	avdtp_set_status(req, buf, msg_type);
804 
805 	if (req->status == BT_AVDTP_SUCCESS) {
806 		bt_avdtp_set_state_lock(CTRL_REQ(req)->sep, AVDTP_CLOSING);
807 
808 		if (!avdtp_media_disconnect(CTRL_REQ(req)->sep)) {
809 			return;
810 		}
811 	}
812 
813 	bt_avdtp_clear_req(session);
814 
815 	if (req->func != NULL) {
816 		req->func(req, NULL);
817 	}
818 }
819 
avdtp_suspend_cmd(struct bt_avdtp * session,struct net_buf * buf,uint8_t tid)820 static void avdtp_suspend_cmd(struct bt_avdtp *session, struct net_buf *buf, uint8_t tid)
821 {
822 	int err = 0;
823 	int ret;
824 	struct bt_avdtp_sep *sep;
825 	struct net_buf *rsp_buf;
826 	uint8_t error_code = 0;
827 
828 	sep = avdtp_get_cmd_sep(buf, &error_code);
829 	avdtp_sep_lock(sep);
830 
831 	if ((sep == NULL) || (session->ops->suspend_ind == NULL)) {
832 		err = -ENOTSUP;
833 	} else {
834 		if (sep->state != AVDTP_STREAMING) {
835 			err = -ENOTSUP;
836 			error_code = BT_AVDTP_BAD_STATE;
837 		} else {
838 			err = session->ops->suspend_ind(session, sep, &error_code);
839 		}
840 	}
841 
842 	rsp_buf = avdtp_create_reply_pdu(err ? BT_AVDTP_REJECT : BT_AVDTP_ACCEPT,
843 					 BT_AVDTP_PACKET_TYPE_SINGLE, BT_AVDTP_SUSPEND, tid);
844 	if (!rsp_buf) {
845 		avdtp_sep_unlock(sep);
846 		return;
847 	}
848 
849 	if (err) {
850 		if (error_code == 0) {
851 			error_code = BT_AVDTP_BAD_ACP_SEID;
852 		}
853 
854 		LOG_DBG("suspend err code:%d", error_code);
855 		net_buf_add_u8(rsp_buf, error_code);
856 	}
857 
858 	ret = bt_l2cap_chan_send(&session->br_chan.chan, rsp_buf);
859 	if (ret) {
860 		net_buf_unref(rsp_buf);
861 		LOG_ERR("Error:L2CAP send fail - result = %d", ret);
862 	}
863 
864 	if (!err && !ret) {
865 		bt_avdtp_set_state(sep, AVDTP_OPEN);
866 	}
867 
868 	avdtp_sep_unlock(sep);
869 }
870 
avdtp_suspend_rsp(struct bt_avdtp * session,struct net_buf * buf,uint8_t msg_type)871 static void avdtp_suspend_rsp(struct bt_avdtp *session, struct net_buf *buf, uint8_t msg_type)
872 {
873 	struct bt_avdtp_req *req = session->req;
874 
875 	if (req == NULL) {
876 		return;
877 	}
878 
879 	k_work_cancel_delayable(&session->timeout_work);
880 
881 	if (msg_type == BT_AVDTP_ACCEPT) {
882 		bt_avdtp_set_state_lock(CTRL_REQ(req)->sep, AVDTP_OPEN);
883 	} else if (msg_type == BT_AVDTP_REJECT) {
884 		avdtp_handle_reject(buf, req);
885 	}
886 
887 	if (req->status == BT_AVDTP_SUCCESS) {
888 		avdtp_set_status(req, buf, msg_type);
889 	}
890 
891 	bt_avdtp_clear_req(session);
892 
893 	if (req->func != NULL) {
894 		req->func(req, NULL);
895 	}
896 }
897 
avdtp_abort_cmd(struct bt_avdtp * session,struct net_buf * buf,uint8_t tid)898 static void avdtp_abort_cmd(struct bt_avdtp *session, struct net_buf *buf, uint8_t tid)
899 {
900 	int err = 0;
901 	int ret;
902 	struct bt_avdtp_sep *sep;
903 	struct net_buf *rsp_buf;
904 	uint8_t error_code = 0;
905 
906 	sep = avdtp_get_cmd_sep(buf, &error_code);
907 	avdtp_sep_lock(sep);
908 
909 	if ((sep == NULL) || (session->ops->abort_ind == NULL)) {
910 		err = -ENOTSUP;
911 	} else {
912 		/* all current sep state is OK for abort operation */
913 		err = session->ops->abort_ind(session, sep, &error_code);
914 	}
915 
916 	rsp_buf = avdtp_create_reply_pdu(err ? BT_AVDTP_REJECT : BT_AVDTP_ACCEPT,
917 					 BT_AVDTP_PACKET_TYPE_SINGLE, BT_AVDTP_ABORT, tid);
918 	if (!rsp_buf) {
919 		avdtp_sep_unlock(sep);
920 		return;
921 	}
922 
923 	if (err) {
924 		if (error_code == 0) {
925 			error_code = BT_AVDTP_BAD_ACP_SEID;
926 		}
927 
928 		LOG_DBG("abort err code:%d", error_code);
929 		net_buf_add_u8(rsp_buf, error_code);
930 	}
931 
932 	ret = bt_l2cap_chan_send(&session->br_chan.chan, rsp_buf);
933 	if (ret) {
934 		net_buf_unref(rsp_buf);
935 		LOG_ERR("Error:L2CAP send fail - result = %d", ret);
936 	}
937 
938 	if (!err && !ret) {
939 		if ((sep->state & (AVDTP_OPEN | AVDTP_STREAMING)) &&
940 		    (sep->chan.state == BT_L2CAP_CONNECTED)) {
941 			bt_avdtp_set_state(sep, AVDTP_ABORTING);
942 		} else {
943 			bt_avdtp_set_state(sep, AVDTP_IDLE);
944 		}
945 	}
946 
947 	avdtp_sep_unlock(sep);
948 }
949 
avdtp_abort_rsp(struct bt_avdtp * session,struct net_buf * buf,uint8_t msg_type)950 static void avdtp_abort_rsp(struct bt_avdtp *session, struct net_buf *buf, uint8_t msg_type)
951 {
952 	struct bt_avdtp_req *req = session->req;
953 
954 	if (req == NULL) {
955 		return;
956 	}
957 
958 	k_work_cancel_delayable(&session->timeout_work);
959 
960 	if (msg_type == BT_AVDTP_ACCEPT) {
961 		uint8_t pre_state = CTRL_REQ(req)->sep->state;
962 
963 		bt_avdtp_set_state_lock(CTRL_REQ(req)->sep, AVDTP_ABORTING);
964 
965 		/* release stream */
966 		if (pre_state & (AVDTP_OPEN | AVDTP_STREAMING)) {
967 			avdtp_media_disconnect(CTRL_REQ(req)->sep);
968 		}
969 
970 		/* For abort, make sure the state revert to IDLE state after
971 		 * releasing l2cap channel.
972 		 */
973 		bt_avdtp_set_state_lock(CTRL_REQ(req)->sep, AVDTP_IDLE);
974 	} else if (msg_type == BT_AVDTP_REJECT) {
975 		avdtp_handle_reject(buf, req);
976 	}
977 
978 	if (req->status == BT_AVDTP_SUCCESS) {
979 		avdtp_set_status(req, buf, msg_type);
980 	}
981 
982 	bt_avdtp_clear_req(session);
983 
984 	if (req->func != NULL) {
985 		req->func(req, NULL);
986 	}
987 }
988 
989 /* Timeout handler */
avdtp_timeout(struct k_work * work)990 static void avdtp_timeout(struct k_work *work)
991 {
992 	struct bt_avdtp_req *req = (AVDTP_KWORK(work))->req;
993 
994 	/* add process code */
995 	/* Gracefully Disconnect the Signalling and streaming L2cap chann*/
996 	if (req) {
997 		LOG_DBG("Failed Signal_id = %d", req->sig);
998 
999 		switch (req->sig) {
1000 		case BT_AVDTP_DISCOVER:
1001 		case BT_AVDTP_GET_CAPABILITIES:
1002 		case BT_AVDTP_SET_CONFIGURATION:
1003 		case BT_AVDTP_RECONFIGURE:
1004 		case BT_AVDTP_OPEN:
1005 		case BT_AVDTP_CLOSE:
1006 		case BT_AVDTP_START:
1007 		case BT_AVDTP_SUSPEND:
1008 			req->status = BT_AVDTP_TIME_OUT;
1009 			req->func(req, NULL);
1010 			break;
1011 		default:
1012 			break;
1013 		}
1014 
1015 		AVDTP_KWORK(work)->req = NULL;
1016 	}
1017 }
1018 
avdtp_send(struct bt_avdtp * session,struct net_buf * buf,struct bt_avdtp_req * req)1019 static int avdtp_send(struct bt_avdtp *session, struct net_buf *buf, struct bt_avdtp_req *req)
1020 {
1021 	int result;
1022 	struct bt_avdtp_single_sig_hdr *hdr;
1023 
1024 	avdtp_lock(session);
1025 
1026 	if (session->req != NULL) {
1027 		avdtp_unlock(session);
1028 		return -EBUSY;
1029 	}
1030 
1031 	session->req = req;
1032 	avdtp_unlock(session);
1033 	hdr = (struct bt_avdtp_single_sig_hdr *)buf->data;
1034 
1035 	result = bt_l2cap_chan_send(&session->br_chan.chan, buf);
1036 	if (result < 0) {
1037 		LOG_ERR("Error:L2CAP send fail - result = %d", result);
1038 		bt_avdtp_clear_req(session);
1039 		return result;
1040 	}
1041 
1042 	/*Save the sent request*/
1043 	req->sig = AVDTP_GET_SIG_ID(hdr->signal_id);
1044 	req->tid = AVDTP_GET_TR_ID(hdr->hdr);
1045 	LOG_DBG("sig 0x%02X, tid 0x%02X", req->sig, req->tid);
1046 
1047 	/* Init the timer */
1048 	k_work_init_delayable(&session->timeout_work, avdtp_timeout);
1049 	/* Start timeout work */
1050 	k_work_reschedule(&session->timeout_work, AVDTP_TIMEOUT);
1051 	return result;
1052 }
1053 
avdtp_create_pdu(uint8_t msg_type,uint8_t pkt_type,uint8_t sig_id)1054 static struct net_buf *avdtp_create_pdu(uint8_t msg_type, uint8_t pkt_type, uint8_t sig_id)
1055 {
1056 	struct net_buf *buf;
1057 	static uint8_t tid;
1058 	struct bt_avdtp_single_sig_hdr *hdr;
1059 
1060 	LOG_DBG("");
1061 
1062 	buf = bt_l2cap_create_pdu(NULL, 0);
1063 
1064 	hdr = net_buf_add(buf, sizeof(*hdr));
1065 
1066 	hdr->hdr = (msg_type | pkt_type << AVDTP_PKT_POSITION | tid++ << AVDTP_TID_POSITION);
1067 	tid %= 16; /* Loop for 16*/
1068 	hdr->signal_id = sig_id & AVDTP_SIGID_MASK;
1069 
1070 	LOG_DBG("hdr = 0x%02X, Signal_ID = 0x%02X", hdr->hdr, hdr->signal_id);
1071 	return buf;
1072 }
1073 
1074 /* L2CAP Interface callbacks */
bt_avdtp_l2cap_connected(struct bt_l2cap_chan * chan)1075 void bt_avdtp_l2cap_connected(struct bt_l2cap_chan *chan)
1076 {
1077 	struct bt_avdtp *session;
1078 
1079 	if (!chan) {
1080 		LOG_ERR("Invalid AVDTP chan");
1081 		return;
1082 	}
1083 
1084 	session = AVDTP_CHAN(chan);
1085 	LOG_DBG("chan %p session %p", chan, session);
1086 
1087 	/* notify a2dp connection result */
1088 	session->ops->connected(session);
1089 }
1090 
bt_avdtp_l2cap_disconnected(struct bt_l2cap_chan * chan)1091 void bt_avdtp_l2cap_disconnected(struct bt_l2cap_chan *chan)
1092 {
1093 	struct bt_avdtp *session = AVDTP_CHAN(chan);
1094 
1095 	LOG_DBG("chan %p session %p", chan, session);
1096 	session->br_chan.chan.conn = NULL;
1097 	/* Clear the Pending req if set*/
1098 	if (session->req) {
1099 		struct bt_avdtp_req *req = session->req;
1100 
1101 		req->status = BT_AVDTP_BAD_STATE;
1102 		bt_avdtp_clear_req(session);
1103 
1104 		if (req->func != NULL) {
1105 			req->func(req, NULL);
1106 		}
1107 	}
1108 
1109 	/* notify a2dp disconnect */
1110 	session->ops->disconnected(session);
1111 }
1112 
1113 void (*cmd_handler[])(struct bt_avdtp *session, struct net_buf *buf, uint8_t tid) = {
1114 	avdtp_discover_cmd,          /* BT_AVDTP_DISCOVER */
1115 	avdtp_get_capabilities_cmd,  /* BT_AVDTP_GET_CAPABILITIES */
1116 	avdtp_set_configuration_cmd, /* BT_AVDTP_SET_CONFIGURATION */
1117 	NULL,                        /* BT_AVDTP_GET_CONFIGURATION */
1118 	avdtp_re_configure_cmd,      /* BT_AVDTP_RECONFIGURE */
1119 	avdtp_open_cmd,              /* BT_AVDTP_OPEN */
1120 	avdtp_start_cmd,             /* BT_AVDTP_START */
1121 	avdtp_close_cmd,             /* BT_AVDTP_CLOSE */
1122 	avdtp_suspend_cmd,           /* BT_AVDTP_SUSPEND */
1123 	avdtp_abort_cmd,             /* BT_AVDTP_ABORT */
1124 	NULL,                        /* BT_AVDTP_SECURITY_CONTROL */
1125 	NULL,                        /* BT_AVDTP_GET_ALL_CAPABILITIES */
1126 	NULL,                        /* BT_AVDTP_DELAYREPORT */
1127 };
1128 
1129 void (*rsp_handler[])(struct bt_avdtp *session, struct net_buf *buf, uint8_t msg_type) = {
1130 	avdtp_discover_rsp,          /* BT_AVDTP_DISCOVER */
1131 	avdtp_get_capabilities_rsp,  /* BT_AVDTP_GET_CAPABILITIES */
1132 	avdtp_set_configuration_rsp, /* BT_AVDTP_SET_CONFIGURATION */
1133 	NULL,                        /* BT_AVDTP_GET_CONFIGURATION */
1134 	avdtp_re_configure_rsp,      /* BT_AVDTP_RECONFIGURE */
1135 	avdtp_open_rsp,              /* BT_AVDTP_OPEN */
1136 	avdtp_start_rsp,             /* BT_AVDTP_START */
1137 	avdtp_close_rsp,             /* BT_AVDTP_CLOSE */
1138 	avdtp_suspend_rsp,           /* BT_AVDTP_SUSPEND */
1139 	avdtp_abort_rsp,             /* BT_AVDTP_ABORT */
1140 	NULL,                        /* BT_AVDTP_SECURITY_CONTROL */
1141 	NULL,                        /* BT_AVDTP_GET_ALL_CAPABILITIES */
1142 	NULL,                        /* BT_AVDTP_DELAYREPORT */
1143 };
1144 
bt_avdtp_l2cap_recv(struct bt_l2cap_chan * chan,struct net_buf * buf)1145 int bt_avdtp_l2cap_recv(struct bt_l2cap_chan *chan, struct net_buf *buf)
1146 {
1147 	struct bt_avdtp_single_sig_hdr *hdr;
1148 	struct bt_avdtp *session = AVDTP_CHAN(chan);
1149 	uint8_t msgtype, pack_type, sigid, tid;
1150 	struct net_buf *rsp_buf;
1151 	int err;
1152 
1153 	if (buf->len < sizeof(*hdr)) {
1154 		LOG_ERR("Recvd Wrong AVDTP Header");
1155 		return -EINVAL;
1156 	}
1157 
1158 	hdr = net_buf_pull_mem(buf, sizeof(*hdr));
1159 	pack_type = AVDTP_GET_PKT_TYPE(hdr->hdr);
1160 	msgtype = AVDTP_GET_MSG_TYPE(hdr->hdr);
1161 	sigid = AVDTP_GET_SIG_ID(hdr->signal_id);
1162 	tid = AVDTP_GET_TR_ID(hdr->hdr);
1163 
1164 	LOG_DBG("pack_type[0x%02x] msg_type[0x%02x] sig_id[0x%02x] tid[0x%02x]", pack_type, msgtype,
1165 		sigid, tid);
1166 
1167 	/* TODO: only support single packet now */
1168 	if (pack_type != BT_AVDTP_PACKET_TYPE_SINGLE) {
1169 		if (pack_type == BT_AVDTP_PACKET_TYPE_START) {
1170 			if (buf->len < 1U) {
1171 				return -EINVAL;
1172 			}
1173 
1174 			sigid = net_buf_pull_u8(buf);
1175 			goto send_reject;
1176 		}
1177 
1178 		/* discard the continue packet and end packet */
1179 		return -EINVAL;
1180 	}
1181 
1182 	if (msgtype == BT_AVDTP_CMD) {
1183 		if (sigid != 0U && sigid <= BT_AVDTP_DELAYREPORT &&
1184 		    cmd_handler[sigid - 1U] != NULL) {
1185 			cmd_handler[sigid - 1U](session, buf, tid);
1186 			return 0;
1187 		}
1188 		/* goto send_reject; */
1189 	} else {
1190 		/* validate if the response is expected*/
1191 		if (session->req == NULL) {
1192 			LOG_DBG("Unexpected peer response");
1193 			return -EINVAL;
1194 		}
1195 
1196 		if (session->req->sig != sigid || session->req->tid != tid) {
1197 			LOG_DBG("Peer mismatch resp, expected sig[0x%02x]"
1198 				"tid[0x%02x]",
1199 				session->req->sig, session->req->tid);
1200 			return -EINVAL;
1201 		}
1202 
1203 		if (sigid != 0U && sigid <= BT_AVDTP_DELAYREPORT &&
1204 		    cmd_handler[sigid - 1U] != NULL) {
1205 			rsp_handler[sigid - 1U](session, buf, msgtype);
1206 			return 0;
1207 		}
1208 
1209 		/* discard unsupported response packet */
1210 		return -EINVAL;
1211 	}
1212 
1213 send_reject:
1214 	rsp_buf = avdtp_create_reply_pdu(BT_AVDTP_GEN_REJECT, BT_AVDTP_PACKET_TYPE_SINGLE, sigid,
1215 					 tid);
1216 	if (!rsp_buf) {
1217 		LOG_ERR("Error: No Buff available");
1218 		return -EINVAL;
1219 	}
1220 
1221 	err = bt_l2cap_chan_send(&session->br_chan.chan, rsp_buf);
1222 	if (err < 0) {
1223 		net_buf_unref(rsp_buf);
1224 		LOG_ERR("Error:L2CAP send fail - result = %d", err);
1225 	}
1226 
1227 	return err;
1228 }
1229 
1230 static const struct bt_l2cap_chan_ops signal_chan_ops = {
1231 	.connected = bt_avdtp_l2cap_connected,
1232 	.disconnected = bt_avdtp_l2cap_disconnected,
1233 	.recv = bt_avdtp_l2cap_recv,
1234 };
1235 
1236 /*A2DP Layer interface */
bt_avdtp_connect(struct bt_conn * conn,struct bt_avdtp * session)1237 int bt_avdtp_connect(struct bt_conn *conn, struct bt_avdtp *session)
1238 {
1239 	if (!session) {
1240 		return -EINVAL;
1241 	}
1242 
1243 	/* there are headsets that initiate the AVDTP signal l2cap connection
1244 	 * at the same time when DUT initiates the same l2cap connection.
1245 	 * Use the `conn` to check whether the l2cap creation is already started.
1246 	 * The whole `session` is cleared by upper layer if it is new l2cap connection.
1247 	 */
1248 	k_sem_take(&avdtp_sem_lock, K_FOREVER);
1249 
1250 	if (session->br_chan.chan.conn != NULL) {
1251 		k_sem_give(&avdtp_sem_lock);
1252 		return -ENOMEM;
1253 	}
1254 
1255 	session->br_chan.chan.conn = conn;
1256 	k_sem_give(&avdtp_sem_lock);
1257 	/* Locking semaphore initialized to 1 (unlocked) */
1258 	k_sem_init(&session->sem_lock, 1, 1);
1259 	session->br_chan.rx.mtu = BT_L2CAP_RX_MTU;
1260 	session->br_chan.chan.ops = &signal_chan_ops;
1261 	session->br_chan.required_sec_level = BT_SECURITY_L2;
1262 
1263 	return bt_l2cap_chan_connect(conn, &session->br_chan.chan, BT_L2CAP_PSM_AVDTP);
1264 }
1265 
bt_avdtp_disconnect(struct bt_avdtp * session)1266 int bt_avdtp_disconnect(struct bt_avdtp *session)
1267 {
1268 	if (!session) {
1269 		return -EINVAL;
1270 	}
1271 
1272 	LOG_DBG("session %p", session);
1273 
1274 	return bt_l2cap_chan_disconnect(&session->br_chan.chan);
1275 }
1276 
bt_avdtp_l2cap_accept(struct bt_conn * conn,struct bt_l2cap_server * server,struct bt_l2cap_chan ** chan)1277 int bt_avdtp_l2cap_accept(struct bt_conn *conn, struct bt_l2cap_server *server,
1278 			  struct bt_l2cap_chan **chan)
1279 {
1280 	struct bt_avdtp *session = NULL;
1281 	int result;
1282 
1283 	LOG_DBG("conn %p", conn);
1284 	/* Get the AVDTP session from upper layer */
1285 	result = event_cb->accept(conn, &session);
1286 	if (result < 0) {
1287 		return result;
1288 	}
1289 
1290 	/* there are headsets that initiate the AVDTP signal l2cap connection
1291 	 * at the same time when DUT initiates the same l2cap connection.
1292 	 * Use the `conn` to check whether the l2cap creation is already started.
1293 	 * The whole `session` is cleared by upper layer if it is new l2cap connection.
1294 	 */
1295 	k_sem_take(&avdtp_sem_lock, K_FOREVER);
1296 
1297 	if (session->br_chan.chan.conn == NULL) {
1298 		session->br_chan.chan.conn = conn;
1299 		k_sem_give(&avdtp_sem_lock);
1300 		/* Locking semaphore initialized to 1 (unlocked) */
1301 		k_sem_init(&session->sem_lock, 1, 1);
1302 		session->br_chan.chan.ops = &signal_chan_ops;
1303 		session->br_chan.rx.mtu = BT_L2CAP_RX_MTU;
1304 		*chan = &session->br_chan.chan;
1305 	} else {
1306 		k_sem_give(&avdtp_sem_lock);
1307 
1308 		/* get the current opening endpoint */
1309 		if (session->current_sep != NULL) {
1310 			session->current_sep->session = session;
1311 			session->current_sep->chan.chan.ops = &stream_chan_ops;
1312 			session->current_sep->chan.rx.mtu = BT_L2CAP_RX_MTU;
1313 			session->current_sep->chan.required_sec_level = BT_SECURITY_L2;
1314 			*chan = &session->current_sep->chan.chan;
1315 			session->current_sep = NULL;
1316 		} else {
1317 			return -ENOMEM;
1318 		}
1319 	}
1320 
1321 	return 0;
1322 }
1323 
1324 /* Application will register its callback */
bt_avdtp_register(struct bt_avdtp_event_cb * cb)1325 int bt_avdtp_register(struct bt_avdtp_event_cb *cb)
1326 {
1327 	LOG_DBG("");
1328 
1329 	if (event_cb) {
1330 		return -EALREADY;
1331 	}
1332 
1333 	event_cb = cb;
1334 
1335 	return 0;
1336 }
1337 
bt_avdtp_register_sep(uint8_t media_type,uint8_t sep_type,struct bt_avdtp_sep * sep)1338 int bt_avdtp_register_sep(uint8_t media_type, uint8_t sep_type, struct bt_avdtp_sep *sep)
1339 {
1340 	LOG_DBG("");
1341 
1342 	static uint8_t bt_avdtp_sep = BT_AVDTP_MIN_SEID;
1343 
1344 	if (!sep) {
1345 		return -EIO;
1346 	}
1347 
1348 	if (bt_avdtp_sep == BT_AVDTP_MAX_SEID) {
1349 		return -EIO;
1350 	}
1351 
1352 	k_sem_take(&avdtp_sem_lock, K_FOREVER);
1353 	/* the id allocation need be locked to protect it */
1354 	sep->sep_info.id = bt_avdtp_sep++;
1355 	sep->sep_info.inuse = 0U;
1356 	sep->sep_info.media_type = media_type;
1357 	sep->sep_info.tsep = sep_type;
1358 	/* Locking semaphore initialized to 1 (unlocked) */
1359 	k_sem_init(&sep->sem_lock, 1, 1);
1360 	bt_avdtp_set_state_lock(sep, AVDTP_IDLE);
1361 
1362 	sys_slist_append(&seps, &sep->_node);
1363 	k_sem_give(&avdtp_sem_lock);
1364 
1365 	return 0;
1366 }
1367 
1368 /* init function */
bt_avdtp_init(void)1369 int bt_avdtp_init(void)
1370 {
1371 	int err;
1372 	static struct bt_l2cap_server avdtp_l2cap = {
1373 		.psm = BT_L2CAP_PSM_AVDTP,
1374 		.sec_level = BT_SECURITY_L2,
1375 		.accept = bt_avdtp_l2cap_accept,
1376 	};
1377 
1378 	LOG_DBG("");
1379 
1380 	/* Register AVDTP PSM with L2CAP */
1381 	err = bt_l2cap_br_server_register(&avdtp_l2cap);
1382 	if (err < 0) {
1383 		LOG_ERR("AVDTP L2CAP Registration failed %d", err);
1384 	}
1385 
1386 	return err;
1387 }
1388 
1389 /* AVDTP Discover Request */
bt_avdtp_discover(struct bt_avdtp * session,struct bt_avdtp_discover_params * param)1390 int bt_avdtp_discover(struct bt_avdtp *session, struct bt_avdtp_discover_params *param)
1391 {
1392 	struct net_buf *buf;
1393 	int err;
1394 
1395 	LOG_DBG("");
1396 	if (!param || !session) {
1397 		LOG_DBG("Error: Callback/Session not valid");
1398 		return -EINVAL;
1399 	}
1400 
1401 	buf = avdtp_create_pdu(BT_AVDTP_CMD, BT_AVDTP_PACKET_TYPE_SINGLE, BT_AVDTP_DISCOVER);
1402 	if (!buf) {
1403 		LOG_ERR("Error: No Buff available");
1404 		return -ENOMEM;
1405 	}
1406 
1407 	err = avdtp_send(session, buf, &param->req);
1408 	if (err) {
1409 		net_buf_unref(buf);
1410 	}
1411 
1412 	return err;
1413 }
1414 
bt_avdtp_parse_sep(struct net_buf * buf,struct bt_avdtp_sep_info * sep_info)1415 int bt_avdtp_parse_sep(struct net_buf *buf, struct bt_avdtp_sep_info *sep_info)
1416 {
1417 	struct bt_avdtp_sep_data *sep_data;
1418 
1419 	if ((sep_info != NULL) && (buf != NULL)) {
1420 		if (buf->len >= sizeof(*sep_data)) {
1421 			sep_data = net_buf_pull_mem(buf, sizeof(*sep_data));
1422 			sep_info->inuse = sep_data->inuse;
1423 			sep_info->id = sep_data->id;
1424 			sep_info->tsep = sep_data->tsep;
1425 			sep_info->media_type = sep_data->media_type;
1426 			return 0;
1427 		}
1428 	}
1429 
1430 	return -EINVAL;
1431 }
1432 
1433 /* AVDTP Get Capabilities Request */
bt_avdtp_get_capabilities(struct bt_avdtp * session,struct bt_avdtp_get_capabilities_params * param)1434 int bt_avdtp_get_capabilities(struct bt_avdtp *session,
1435 			      struct bt_avdtp_get_capabilities_params *param)
1436 {
1437 	struct net_buf *buf;
1438 	int err;
1439 
1440 	LOG_DBG("");
1441 	if (!param || !session) {
1442 		LOG_DBG("Error: Callback/Session not valid");
1443 		return -EINVAL;
1444 	}
1445 
1446 	buf = avdtp_create_pdu(BT_AVDTP_CMD, BT_AVDTP_PACKET_TYPE_SINGLE,
1447 			       BT_AVDTP_GET_CAPABILITIES);
1448 	if (!buf) {
1449 		LOG_ERR("Error: No Buff available");
1450 		return -ENOMEM;
1451 	}
1452 
1453 	/* Body of the message */
1454 	net_buf_add_u8(buf, (param->stream_endpoint_id << 2U));
1455 
1456 	err = avdtp_send(session, buf, &param->req);
1457 	if (err) {
1458 		net_buf_unref(buf);
1459 	}
1460 
1461 	return err;
1462 }
1463 
bt_avdtp_parse_capability_codec(struct net_buf * buf,uint8_t * codec_type,uint8_t ** codec_info_element,uint16_t * codec_info_element_len)1464 int bt_avdtp_parse_capability_codec(struct net_buf *buf, uint8_t *codec_type,
1465 				    uint8_t **codec_info_element, uint16_t *codec_info_element_len)
1466 {
1467 	uint8_t data;
1468 	uint8_t length;
1469 
1470 	if (!buf) {
1471 		LOG_DBG("Error: buf not valid");
1472 		return -EINVAL;
1473 	}
1474 
1475 	while (buf->len) {
1476 		data = net_buf_pull_u8(buf);
1477 		switch (data) {
1478 		case BT_AVDTP_SERVICE_MEDIA_TRANSPORT:
1479 		case BT_AVDTP_SERVICE_REPORTING:
1480 		case BT_AVDTP_SERVICE_MEDIA_RECOVERY:
1481 		case BT_AVDTP_SERVICE_CONTENT_PROTECTION:
1482 		case BT_AVDTP_SERVICE_HEADER_COMPRESSION:
1483 		case BT_AVDTP_SERVICE_MULTIPLEXING:
1484 		case BT_AVDTP_SERVICE_DELAY_REPORTING:
1485 			if (buf->len < 1U) {
1486 				return -EINVAL;
1487 			}
1488 
1489 			length = net_buf_pull_u8(buf);
1490 			if (length > 0) {
1491 				if (buf->len < length) {
1492 					return -EINVAL;
1493 				}
1494 
1495 				net_buf_pull_mem(buf, length);
1496 			}
1497 			break;
1498 
1499 		case BT_AVDTP_SERVICE_MEDIA_CODEC:
1500 			if (buf->len < 1U) {
1501 				return -EINVAL;
1502 			}
1503 
1504 			length = net_buf_pull_u8(buf);
1505 			if (buf->len < length) {
1506 				return -EINVAL;
1507 			}
1508 
1509 			if (length > 3) {
1510 				data = net_buf_pull_u8(buf);
1511 				if (data == BT_AVDTP_AUDIO) {
1512 					data = net_buf_pull_u8(buf);
1513 					*codec_type = data;
1514 					*codec_info_element_len = (length - 2);
1515 					*codec_info_element =
1516 						net_buf_pull_mem(buf, (*codec_info_element_len));
1517 					return 0;
1518 				}
1519 			}
1520 			break;
1521 
1522 		default:
1523 			break;
1524 		}
1525 	}
1526 	return -EINVAL;
1527 }
1528 
avdtp_process_configure_command(struct bt_avdtp * session,uint8_t cmd,struct bt_avdtp_set_configuration_params * param)1529 static int avdtp_process_configure_command(struct bt_avdtp *session, uint8_t cmd,
1530 					   struct bt_avdtp_set_configuration_params *param)
1531 {
1532 	struct net_buf *buf;
1533 	int err;
1534 
1535 	LOG_DBG("");
1536 	if (!param || !session) {
1537 		LOG_DBG("Error: Callback/Session not valid");
1538 		return -EINVAL;
1539 	}
1540 
1541 	buf = avdtp_create_pdu(BT_AVDTP_CMD, BT_AVDTP_PACKET_TYPE_SINGLE, cmd);
1542 	if (!buf) {
1543 		LOG_ERR("Error: No Buff available");
1544 		return -ENOMEM;
1545 	}
1546 
1547 	/* Body of the message */
1548 	/* ACP Stream Endpoint ID */
1549 	net_buf_add_u8(buf, (param->acp_stream_ep_id << 2U));
1550 	/* INT Stream Endpoint ID */
1551 	net_buf_add_u8(buf, (param->int_stream_endpoint_id << 2U));
1552 	/* Service Category: Media Transport */
1553 	net_buf_add_u8(buf, BT_AVDTP_SERVICE_MEDIA_TRANSPORT);
1554 	/* LOSC */
1555 	net_buf_add_u8(buf, 0);
1556 	/* Service Category: Media Codec */
1557 	net_buf_add_u8(buf, BT_AVDTP_SERVICE_MEDIA_CODEC);
1558 	/* LOSC */
1559 	net_buf_add_u8(buf, param->codec_specific_ie_len + 2);
1560 	/* Media Type */
1561 	net_buf_add_u8(buf, param->media_type << 4U);
1562 	/* Media Codec Type */
1563 	net_buf_add_u8(buf, param->media_codec_type);
1564 	/* Codec Info Element */
1565 	net_buf_add_mem(buf, param->codec_specific_ie, param->codec_specific_ie_len);
1566 
1567 	err = avdtp_send(session, buf, &param->req);
1568 	if (err) {
1569 		net_buf_unref(buf);
1570 	}
1571 
1572 	return err;
1573 }
1574 
bt_avdtp_set_configuration(struct bt_avdtp * session,struct bt_avdtp_set_configuration_params * param)1575 int bt_avdtp_set_configuration(struct bt_avdtp *session,
1576 			       struct bt_avdtp_set_configuration_params *param)
1577 {
1578 	if (!param || !session || !param->sep) {
1579 		LOG_DBG("Error: parameters not valid");
1580 		return -EINVAL;
1581 	}
1582 
1583 	if (param->sep->state != AVDTP_IDLE) {
1584 		return -EINVAL;
1585 	}
1586 
1587 	return avdtp_process_configure_command(session, BT_AVDTP_SET_CONFIGURATION, param);
1588 }
1589 
bt_avdtp_reconfigure(struct bt_avdtp * session,struct bt_avdtp_set_configuration_params * param)1590 int bt_avdtp_reconfigure(struct bt_avdtp *session, struct bt_avdtp_set_configuration_params *param)
1591 {
1592 	if (!param || !session || !param->sep) {
1593 		LOG_DBG("Error: parameters not valid");
1594 		return -EINVAL;
1595 	}
1596 
1597 	if (param->sep->state != AVDTP_OPEN) {
1598 		return -EINVAL;
1599 	}
1600 
1601 	return avdtp_process_configure_command(session, BT_AVDTP_RECONFIGURE, param);
1602 }
1603 
bt_avdtp_ctrl(struct bt_avdtp * session,struct bt_avdtp_ctrl_params * param,uint8_t ctrl,uint8_t check_state)1604 static int bt_avdtp_ctrl(struct bt_avdtp *session, struct bt_avdtp_ctrl_params *param, uint8_t ctrl,
1605 			 uint8_t check_state)
1606 {
1607 	struct net_buf *buf;
1608 	int err;
1609 
1610 	LOG_DBG("");
1611 	if (!param || !session || !param->sep) {
1612 		LOG_DBG("Error: parameters not valid");
1613 		return -EINVAL;
1614 	}
1615 
1616 	if (!(param->sep->state & check_state)) {
1617 		return -EINVAL;
1618 	}
1619 
1620 	buf = avdtp_create_pdu(BT_AVDTP_CMD, BT_AVDTP_PACKET_TYPE_SINGLE, ctrl);
1621 	if (!buf) {
1622 		LOG_ERR("Error: No Buff available");
1623 		return -ENOMEM;
1624 	}
1625 
1626 	/* Body of the message */
1627 	/* ACP Stream Endpoint ID */
1628 	net_buf_add_u8(buf, (param->acp_stream_ep_id << 2U));
1629 
1630 	err = avdtp_send(session, buf, &param->req);
1631 	if (err) {
1632 		net_buf_unref(buf);
1633 	}
1634 
1635 	return err;
1636 }
1637 
bt_avdtp_open(struct bt_avdtp * session,struct bt_avdtp_ctrl_params * param)1638 int bt_avdtp_open(struct bt_avdtp *session, struct bt_avdtp_ctrl_params *param)
1639 {
1640 	return bt_avdtp_ctrl(session, param, BT_AVDTP_OPEN, AVDTP_CONFIGURED);
1641 }
1642 
bt_avdtp_close(struct bt_avdtp * session,struct bt_avdtp_ctrl_params * param)1643 int bt_avdtp_close(struct bt_avdtp *session, struct bt_avdtp_ctrl_params *param)
1644 {
1645 	return bt_avdtp_ctrl(session, param, BT_AVDTP_CLOSE, AVDTP_OPEN | AVDTP_STREAMING);
1646 }
1647 
bt_avdtp_start(struct bt_avdtp * session,struct bt_avdtp_ctrl_params * param)1648 int bt_avdtp_start(struct bt_avdtp *session, struct bt_avdtp_ctrl_params *param)
1649 {
1650 	int err;
1651 
1652 	err = bt_avdtp_ctrl(session, param, BT_AVDTP_START, AVDTP_OPEN);
1653 	if (!err && param->sep->sep_info.tsep == BT_AVDTP_SINK) {
1654 		bt_avdtp_set_state_lock(param->sep, AVDTP_STREAMING);
1655 	}
1656 
1657 	return err;
1658 }
1659 
bt_avdtp_suspend(struct bt_avdtp * session,struct bt_avdtp_ctrl_params * param)1660 int bt_avdtp_suspend(struct bt_avdtp *session, struct bt_avdtp_ctrl_params *param)
1661 {
1662 	return bt_avdtp_ctrl(session, param, BT_AVDTP_SUSPEND, AVDTP_STREAMING);
1663 }
1664 
bt_avdtp_abort(struct bt_avdtp * session,struct bt_avdtp_ctrl_params * param)1665 int bt_avdtp_abort(struct bt_avdtp *session, struct bt_avdtp_ctrl_params *param)
1666 {
1667 	return bt_avdtp_ctrl(session, param, BT_AVDTP_ABORT,
1668 			     AVDTP_CONFIGURED | AVDTP_OPENING | AVDTP_OPEN | AVDTP_STREAMING |
1669 				     AVDTP_CLOSING);
1670 }
1671 
bt_avdtp_send_media_data(struct bt_avdtp_sep * sep,struct net_buf * buf)1672 int bt_avdtp_send_media_data(struct bt_avdtp_sep *sep, struct net_buf *buf)
1673 {
1674 	int err;
1675 
1676 	if (sep->state != AVDTP_STREAMING || sep->sep_info.tsep != BT_AVDTP_SOURCE) {
1677 		return -EIO;
1678 	}
1679 
1680 	err = bt_l2cap_chan_send(&sep->chan.chan, buf);
1681 	if (err < 0) {
1682 		LOG_ERR("Error:L2CAP send fail - err = %d", err);
1683 		return err;
1684 	}
1685 
1686 	return err;
1687 }
1688 
bt_avdtp_get_media_mtu(struct bt_avdtp_sep * sep)1689 uint32_t bt_avdtp_get_media_mtu(struct bt_avdtp_sep *sep)
1690 {
1691 	return sep->chan.tx.mtu;
1692 }
1693