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