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 struct bt_avdtp_sep *sep;
429 struct net_buf *rsp_buf;
430 uint8_t avdtp_err_code = 0;
431
432 sep = avdtp_get_cmd_sep(buf, &avdtp_err_code);
433 avdtp_sep_lock(sep);
434
435 if (sep == NULL) {
436 err = -ENOTSUP;
437 } else if (!reconfig && session->ops->set_configuration_ind == NULL) {
438 err = -ENOTSUP;
439 } else if (reconfig && session->ops->re_configuration_ind == NULL) {
440 err = -ENOTSUP;
441 } else {
442 uint8_t expected_state;
443
444 if (reconfig) {
445 expected_state = AVDTP_OPEN | AVDTP_OPENING;
446 } else {
447 expected_state = AVDTP_IDLE;
448 }
449
450 if (!(sep->state & expected_state)) {
451 err = -ENOTSUP;
452 avdtp_err_code = BT_AVDTP_BAD_STATE;
453 } else if (buf->len >= 1U) {
454 uint8_t int_seid;
455
456 /* INT Stream Endpoint ID */
457 int_seid = net_buf_pull_u8(buf) >> 2;
458
459 if (!reconfig) {
460 err = session->ops->set_configuration_ind(session, sep, int_seid,
461 buf, &avdtp_err_code);
462 } else {
463 err = session->ops->re_configuration_ind(session, sep, int_seid,
464 buf, &avdtp_err_code);
465 }
466 } else {
467 LOG_WRN("Invalid INT SEID");
468 err = -ENOTSUP;
469 avdtp_err_code = BT_AVDTP_BAD_LENGTH;
470 }
471 }
472
473 rsp_buf = avdtp_create_reply_pdu(
474 err ? BT_AVDTP_REJECT : BT_AVDTP_ACCEPT, BT_AVDTP_PACKET_TYPE_SINGLE,
475 reconfig ? BT_AVDTP_RECONFIGURE : BT_AVDTP_SET_CONFIGURATION, tid);
476 if (!rsp_buf) {
477 avdtp_sep_unlock(sep);
478 return;
479 }
480
481 if (err) {
482 if (avdtp_err_code == 0) {
483 avdtp_err_code = BT_AVDTP_BAD_ACP_SEID;
484 }
485
486 LOG_DBG("set configuration err code:%d", avdtp_err_code);
487 /* Service Category: Media Codec */
488 net_buf_add_u8(rsp_buf, BT_AVDTP_SERVICE_MEDIA_CODEC);
489 /* ERROR CODE */
490 net_buf_add_u8(rsp_buf, avdtp_err_code);
491 }
492
493 err = bt_l2cap_chan_send(&session->br_chan.chan, rsp_buf);
494 if (err) {
495 net_buf_unref(rsp_buf);
496 LOG_ERR("Error:L2CAP send fail - result = %d", err);
497 }
498
499 if (!reconfig && !err && !avdtp_err_code) {
500 bt_avdtp_set_state(sep, AVDTP_CONFIGURED);
501 }
502
503 avdtp_sep_unlock(sep);
504 }
505
avdtp_process_configuration_rsp(struct bt_avdtp * session,struct net_buf * buf,uint8_t msg_type,bool reconfig)506 static void avdtp_process_configuration_rsp(struct bt_avdtp *session, struct net_buf *buf,
507 uint8_t msg_type, bool reconfig)
508 {
509 struct bt_avdtp_req *req = session->req;
510
511 if (req == NULL) {
512 return;
513 }
514
515 k_work_cancel_delayable(&session->timeout_work);
516
517 if (msg_type == BT_AVDTP_ACCEPT) {
518 if (!reconfig) {
519 bt_avdtp_set_state_lock(SET_CONF_REQ(req)->sep, AVDTP_CONFIGURED);
520 }
521 } else if (msg_type == BT_AVDTP_REJECT) {
522 if (buf->len < 1U) {
523 LOG_WRN("Invalid RSP frame");
524 req->status = BT_AVDTP_BAD_LENGTH;
525 } else {
526 /* Service Category */
527 net_buf_pull_u8(buf);
528 }
529 }
530
531 if (req->status == BT_AVDTP_SUCCESS) {
532 avdtp_set_status(req, buf, msg_type);
533 }
534
535 bt_avdtp_clear_req(session);
536
537 if (req->func != NULL) {
538 req->func(req, NULL);
539 }
540 }
541
avdtp_set_configuration_cmd(struct bt_avdtp * session,struct net_buf * buf,uint8_t tid)542 static void avdtp_set_configuration_cmd(struct bt_avdtp *session, struct net_buf *buf, uint8_t tid)
543 {
544 avdtp_process_configuration_cmd(session, buf, tid, false);
545 }
546
avdtp_set_configuration_rsp(struct bt_avdtp * session,struct net_buf * buf,uint8_t msg_type)547 static void avdtp_set_configuration_rsp(struct bt_avdtp *session, struct net_buf *buf,
548 uint8_t msg_type)
549 {
550 avdtp_process_configuration_rsp(session, buf, msg_type, false);
551 }
552
avdtp_re_configure_cmd(struct bt_avdtp * session,struct net_buf * buf,uint8_t tid)553 static void avdtp_re_configure_cmd(struct bt_avdtp *session, struct net_buf *buf, uint8_t tid)
554 {
555 avdtp_process_configuration_cmd(session, buf, tid, true);
556 }
557
avdtp_re_configure_rsp(struct bt_avdtp * session,struct net_buf * buf,uint8_t msg_type)558 static void avdtp_re_configure_rsp(struct bt_avdtp *session, struct net_buf *buf, uint8_t msg_type)
559 {
560 avdtp_process_configuration_rsp(session, buf, msg_type, true);
561 }
562
avdtp_open_cmd(struct bt_avdtp * session,struct net_buf * buf,uint8_t tid)563 static void avdtp_open_cmd(struct bt_avdtp *session, struct net_buf *buf, uint8_t tid)
564 {
565 int err = 0;
566 struct bt_avdtp_sep *sep;
567 struct net_buf *rsp_buf;
568 uint8_t avdtp_err_code = 0;
569
570 sep = avdtp_get_cmd_sep(buf, &avdtp_err_code);
571 avdtp_sep_lock(sep);
572
573 if ((sep == NULL) || (session->ops->open_ind == NULL)) {
574 err = -ENOTSUP;
575 } else {
576 if (sep->state != AVDTP_CONFIGURED) {
577 err = -ENOTSUP;
578 avdtp_err_code = BT_AVDTP_BAD_STATE;
579 } else {
580 err = session->ops->open_ind(session, sep, &avdtp_err_code);
581 }
582 }
583
584 rsp_buf = avdtp_create_reply_pdu(err ? BT_AVDTP_REJECT : BT_AVDTP_ACCEPT,
585 BT_AVDTP_PACKET_TYPE_SINGLE, BT_AVDTP_OPEN, tid);
586 if (!rsp_buf) {
587 avdtp_sep_unlock(sep);
588 return;
589 }
590
591 if (err) {
592 if (avdtp_err_code == 0) {
593 avdtp_err_code = BT_AVDTP_BAD_ACP_SEID;
594 }
595
596 LOG_DBG("open_ind err code:%d", avdtp_err_code);
597 net_buf_add_u8(rsp_buf, avdtp_err_code);
598 } else {
599 session->current_sep = sep;
600 }
601
602 err = bt_l2cap_chan_send(&session->br_chan.chan, rsp_buf);
603 if (err) {
604 net_buf_unref(rsp_buf);
605 LOG_ERR("Error:L2CAP send fail - result = %d", err);
606 }
607
608 if (!err && !avdtp_err_code) {
609 bt_avdtp_set_state(sep, AVDTP_OPENING);
610 }
611
612 avdtp_sep_unlock(sep);
613 }
614
avdtp_open_rsp(struct bt_avdtp * session,struct net_buf * buf,uint8_t msg_type)615 static void avdtp_open_rsp(struct bt_avdtp *session, struct net_buf *buf, uint8_t msg_type)
616 {
617 struct bt_avdtp_req *req = session->req;
618
619 if (req == NULL) {
620 return;
621 }
622
623 k_work_cancel_delayable(&session->timeout_work);
624 avdtp_set_status(req, buf, msg_type);
625
626 if (req->status == BT_AVDTP_SUCCESS) {
627 bt_avdtp_set_state_lock(CTRL_REQ(req)->sep, AVDTP_OPENING);
628
629 /* wait the media l2cap is established */
630 if (!avdtp_media_connect(session, CTRL_REQ(req)->sep)) {
631 return;
632 }
633 }
634
635 if (req->status != BT_AVDTP_SUCCESS) {
636 bt_avdtp_clear_req(session);
637
638 if (req->func != NULL) {
639 req->func(req, NULL);
640 }
641 }
642 }
643
avdtp_handle_reject(struct net_buf * buf,struct bt_avdtp_req * req)644 static void avdtp_handle_reject(struct net_buf *buf, struct bt_avdtp_req *req)
645 {
646 if (buf->len > 1U) {
647 uint8_t acp_seid;
648
649 acp_seid = net_buf_pull_u8(buf);
650
651 if (acp_seid != CTRL_REQ(req)->acp_stream_ep_id) {
652 req->status = BT_AVDTP_BAD_ACP_SEID;
653 }
654 } else {
655 req->status = BT_AVDTP_BAD_LENGTH;
656 }
657 }
658
avdtp_start_cmd(struct bt_avdtp * session,struct net_buf * buf,uint8_t tid)659 static void avdtp_start_cmd(struct bt_avdtp *session, struct net_buf *buf, uint8_t tid)
660 {
661 int err = 0;
662 struct bt_avdtp_sep *sep;
663 struct net_buf *rsp_buf;
664 uint8_t avdtp_err_code = 0;
665
666 sep = avdtp_get_cmd_sep(buf, &avdtp_err_code);
667 avdtp_sep_lock(sep);
668
669 if ((sep == NULL) || (session->ops->start_ind == NULL)) {
670 err = -ENOTSUP;
671 } else {
672 if (sep->state != AVDTP_OPEN) {
673 err = -ENOTSUP;
674 avdtp_err_code = BT_AVDTP_BAD_STATE;
675 } else {
676 err = session->ops->start_ind(session, sep, &avdtp_err_code);
677 }
678 }
679
680 rsp_buf = avdtp_create_reply_pdu(err ? BT_AVDTP_REJECT : BT_AVDTP_ACCEPT,
681 BT_AVDTP_PACKET_TYPE_SINGLE, BT_AVDTP_START, tid);
682 if (!rsp_buf) {
683 avdtp_sep_unlock(sep);
684 return;
685 }
686
687 if (err) {
688 if (avdtp_err_code == 0) {
689 avdtp_err_code = BT_AVDTP_BAD_ACP_SEID;
690 }
691
692 LOG_DBG("start err code:%d", avdtp_err_code);
693 net_buf_add_u8(rsp_buf, avdtp_err_code);
694 }
695
696 err = bt_l2cap_chan_send(&session->br_chan.chan, rsp_buf);
697 if (err) {
698 net_buf_unref(rsp_buf);
699 LOG_ERR("Error:L2CAP send fail - result = %d", err);
700 }
701
702 if (!err && !avdtp_err_code) {
703 bt_avdtp_set_state(sep, AVDTP_STREAMING);
704 }
705
706 avdtp_sep_unlock(sep);
707 }
708
avdtp_start_rsp(struct bt_avdtp * session,struct net_buf * buf,uint8_t msg_type)709 static void avdtp_start_rsp(struct bt_avdtp *session, struct net_buf *buf, uint8_t msg_type)
710 {
711 struct bt_avdtp_req *req = session->req;
712
713 if (req == NULL) {
714 return;
715 }
716
717 k_work_cancel_delayable(&session->timeout_work);
718
719 if (msg_type == BT_AVDTP_ACCEPT) {
720 bt_avdtp_set_state_lock(CTRL_REQ(req)->sep, AVDTP_STREAMING);
721 } else if (msg_type == BT_AVDTP_REJECT) {
722 avdtp_handle_reject(buf, req);
723 }
724
725 if (req->status == BT_AVDTP_SUCCESS) {
726 avdtp_set_status(req, buf, msg_type);
727 }
728
729 bt_avdtp_clear_req(session);
730
731 if (req->func != NULL) {
732 req->func(req, NULL);
733 }
734 }
735
avdtp_close_cmd(struct bt_avdtp * session,struct net_buf * buf,uint8_t tid)736 static void avdtp_close_cmd(struct bt_avdtp *session, struct net_buf *buf, uint8_t tid)
737 {
738 int err = 0;
739 struct bt_avdtp_sep *sep;
740 struct net_buf *rsp_buf;
741 uint8_t avdtp_err_code = 0;
742
743 sep = avdtp_get_cmd_sep(buf, &avdtp_err_code);
744 avdtp_sep_lock(sep);
745
746 if ((sep == NULL) || (session->ops->close_ind == NULL)) {
747 err = -ENOTSUP;
748 } else {
749 if (!(sep->state & (AVDTP_OPEN | AVDTP_STREAMING))) {
750 err = -ENOTSUP;
751 avdtp_err_code = BT_AVDTP_BAD_STATE;
752 } else {
753 err = session->ops->close_ind(session, sep, &avdtp_err_code);
754 }
755 }
756
757 rsp_buf = avdtp_create_reply_pdu(err ? BT_AVDTP_REJECT : BT_AVDTP_ACCEPT,
758 BT_AVDTP_PACKET_TYPE_SINGLE, BT_AVDTP_CLOSE, tid);
759 if (!rsp_buf) {
760 avdtp_sep_unlock(sep);
761 return;
762 }
763
764 if (err) {
765 if (avdtp_err_code == 0) {
766 avdtp_err_code = BT_AVDTP_BAD_ACP_SEID;
767 }
768
769 LOG_DBG("close err code:%d", avdtp_err_code);
770 net_buf_add_u8(rsp_buf, avdtp_err_code);
771 } else {
772 bt_avdtp_set_state(sep, AVDTP_CLOSING);
773 }
774
775 err = bt_l2cap_chan_send(&session->br_chan.chan, rsp_buf);
776 if (err) {
777 net_buf_unref(rsp_buf);
778 LOG_ERR("Error:L2CAP send fail - result = %d", err);
779 }
780
781 if (!err && !avdtp_err_code) {
782 bt_avdtp_set_state(sep, AVDTP_IDLE);
783 }
784
785 avdtp_sep_unlock(sep);
786 }
787
avdtp_close_rsp(struct bt_avdtp * session,struct net_buf * buf,uint8_t msg_type)788 static void avdtp_close_rsp(struct bt_avdtp *session, struct net_buf *buf, uint8_t msg_type)
789 {
790 struct bt_avdtp_req *req = session->req;
791
792 if (req == NULL) {
793 return;
794 }
795
796 k_work_cancel_delayable(&session->timeout_work);
797 avdtp_set_status(req, buf, msg_type);
798
799 if (req->status == BT_AVDTP_SUCCESS) {
800 bt_avdtp_set_state_lock(CTRL_REQ(req)->sep, AVDTP_CLOSING);
801
802 if (!avdtp_media_disconnect(CTRL_REQ(req)->sep)) {
803 return;
804 }
805 }
806
807 bt_avdtp_clear_req(session);
808
809 if (req->func != NULL) {
810 req->func(req, NULL);
811 }
812 }
813
avdtp_suspend_cmd(struct bt_avdtp * session,struct net_buf * buf,uint8_t tid)814 static void avdtp_suspend_cmd(struct bt_avdtp *session, struct net_buf *buf, uint8_t tid)
815 {
816 int err = 0;
817 struct bt_avdtp_sep *sep;
818 struct net_buf *rsp_buf;
819 uint8_t avdtp_err_code = 0;
820
821 sep = avdtp_get_cmd_sep(buf, &avdtp_err_code);
822 avdtp_sep_lock(sep);
823
824 if ((sep == NULL) || (session->ops->suspend_ind == NULL)) {
825 err = -ENOTSUP;
826 } else {
827 if (sep->state != AVDTP_STREAMING) {
828 err = -ENOTSUP;
829 avdtp_err_code = BT_AVDTP_BAD_STATE;
830 } else {
831 err = session->ops->suspend_ind(session, sep, &avdtp_err_code);
832 }
833 }
834
835 rsp_buf = avdtp_create_reply_pdu(err ? BT_AVDTP_REJECT : BT_AVDTP_ACCEPT,
836 BT_AVDTP_PACKET_TYPE_SINGLE, BT_AVDTP_SUSPEND, tid);
837 if (!rsp_buf) {
838 avdtp_sep_unlock(sep);
839 return;
840 }
841
842 if (err) {
843 if (avdtp_err_code == 0) {
844 avdtp_err_code = BT_AVDTP_BAD_ACP_SEID;
845 }
846
847 LOG_DBG("suspend err code:%d", avdtp_err_code);
848 net_buf_add_u8(rsp_buf, avdtp_err_code);
849 }
850
851 err = bt_l2cap_chan_send(&session->br_chan.chan, rsp_buf);
852 if (err) {
853 net_buf_unref(rsp_buf);
854 LOG_ERR("Error:L2CAP send fail - result = %d", err);
855 }
856
857 if (!err && !avdtp_err_code) {
858 bt_avdtp_set_state(sep, AVDTP_OPEN);
859 }
860
861 avdtp_sep_unlock(sep);
862 }
863
avdtp_suspend_rsp(struct bt_avdtp * session,struct net_buf * buf,uint8_t msg_type)864 static void avdtp_suspend_rsp(struct bt_avdtp *session, struct net_buf *buf, uint8_t msg_type)
865 {
866 struct bt_avdtp_req *req = session->req;
867
868 if (req == NULL) {
869 return;
870 }
871
872 k_work_cancel_delayable(&session->timeout_work);
873
874 if (msg_type == BT_AVDTP_ACCEPT) {
875 bt_avdtp_set_state_lock(CTRL_REQ(req)->sep, AVDTP_OPEN);
876 } else if (msg_type == BT_AVDTP_REJECT) {
877 avdtp_handle_reject(buf, req);
878 }
879
880 if (req->status == BT_AVDTP_SUCCESS) {
881 avdtp_set_status(req, buf, msg_type);
882 }
883
884 bt_avdtp_clear_req(session);
885
886 if (req->func != NULL) {
887 req->func(req, NULL);
888 }
889 }
890
avdtp_abort_cmd(struct bt_avdtp * session,struct net_buf * buf,uint8_t tid)891 static void avdtp_abort_cmd(struct bt_avdtp *session, struct net_buf *buf, uint8_t tid)
892 {
893 int err = 0;
894 struct bt_avdtp_sep *sep;
895 struct net_buf *rsp_buf;
896 uint8_t avdtp_err_code = 0;
897
898 sep = avdtp_get_cmd_sep(buf, &avdtp_err_code);
899 avdtp_sep_lock(sep);
900
901 if ((sep == NULL) || (session->ops->abort_ind == NULL)) {
902 err = -ENOTSUP;
903 } else {
904 /* all current sep state is OK for abort operation */
905 err = session->ops->abort_ind(session, sep, &avdtp_err_code);
906 }
907
908 rsp_buf = avdtp_create_reply_pdu(err ? BT_AVDTP_REJECT : BT_AVDTP_ACCEPT,
909 BT_AVDTP_PACKET_TYPE_SINGLE, BT_AVDTP_ABORT, tid);
910 if (!rsp_buf) {
911 avdtp_sep_unlock(sep);
912 return;
913 }
914
915 if (err) {
916 if (avdtp_err_code == 0) {
917 avdtp_err_code = BT_AVDTP_BAD_ACP_SEID;
918 }
919
920 LOG_DBG("abort err code:%d", avdtp_err_code);
921 net_buf_add_u8(rsp_buf, avdtp_err_code);
922 }
923
924 err = bt_l2cap_chan_send(&session->br_chan.chan, rsp_buf);
925 if (err) {
926 net_buf_unref(rsp_buf);
927 LOG_ERR("Error:L2CAP send fail - result = %d", err);
928 }
929
930 if (!err && !avdtp_err_code) {
931 if ((sep->state & (AVDTP_OPEN | AVDTP_STREAMING)) &&
932 (sep->chan.state == BT_L2CAP_CONNECTED)) {
933 bt_avdtp_set_state(sep, AVDTP_ABORTING);
934 } else {
935 bt_avdtp_set_state(sep, AVDTP_IDLE);
936 }
937 }
938
939 avdtp_sep_unlock(sep);
940 }
941
avdtp_abort_rsp(struct bt_avdtp * session,struct net_buf * buf,uint8_t msg_type)942 static void avdtp_abort_rsp(struct bt_avdtp *session, struct net_buf *buf, uint8_t msg_type)
943 {
944 struct bt_avdtp_req *req = session->req;
945
946 if (req == NULL) {
947 return;
948 }
949
950 k_work_cancel_delayable(&session->timeout_work);
951
952 if (msg_type == BT_AVDTP_ACCEPT) {
953 uint8_t pre_state = CTRL_REQ(req)->sep->state;
954
955 bt_avdtp_set_state_lock(CTRL_REQ(req)->sep, AVDTP_ABORTING);
956
957 /* release stream */
958 if (pre_state & (AVDTP_OPEN | AVDTP_STREAMING)) {
959 avdtp_media_disconnect(CTRL_REQ(req)->sep);
960 }
961
962 /* For abort, make sure the state revert to IDLE state after
963 * releasing l2cap channel.
964 */
965 bt_avdtp_set_state_lock(CTRL_REQ(req)->sep, AVDTP_IDLE);
966 } else if (msg_type == BT_AVDTP_REJECT) {
967 avdtp_handle_reject(buf, req);
968 }
969
970 if (req->status == BT_AVDTP_SUCCESS) {
971 avdtp_set_status(req, buf, msg_type);
972 }
973
974 bt_avdtp_clear_req(session);
975
976 if (req->func != NULL) {
977 req->func(req, NULL);
978 }
979 }
980
981 /* Timeout handler */
avdtp_timeout(struct k_work * work)982 static void avdtp_timeout(struct k_work *work)
983 {
984 struct bt_avdtp_req *req = (AVDTP_KWORK(work))->req;
985
986 /* add process code */
987 /* Gracefully Disconnect the Signalling and streaming L2cap chann*/
988 if (req) {
989 LOG_DBG("Failed Signal_id = %d", req->sig);
990
991 switch (req->sig) {
992 case BT_AVDTP_DISCOVER:
993 case BT_AVDTP_GET_CAPABILITIES:
994 case BT_AVDTP_SET_CONFIGURATION:
995 case BT_AVDTP_RECONFIGURE:
996 case BT_AVDTP_OPEN:
997 case BT_AVDTP_CLOSE:
998 case BT_AVDTP_START:
999 case BT_AVDTP_SUSPEND:
1000 req->status = BT_AVDTP_TIME_OUT;
1001 req->func(req, NULL);
1002 break;
1003 default:
1004 break;
1005 }
1006
1007 AVDTP_KWORK(work)->req = NULL;
1008 }
1009 }
1010
avdtp_send(struct bt_avdtp * session,struct net_buf * buf,struct bt_avdtp_req * req)1011 static int avdtp_send(struct bt_avdtp *session, struct net_buf *buf, struct bt_avdtp_req *req)
1012 {
1013 int result;
1014 struct bt_avdtp_single_sig_hdr *hdr;
1015
1016 avdtp_lock(session);
1017
1018 if (session->req != NULL) {
1019 avdtp_unlock(session);
1020 return -EBUSY;
1021 }
1022
1023 session->req = req;
1024 avdtp_unlock(session);
1025 hdr = (struct bt_avdtp_single_sig_hdr *)buf->data;
1026
1027 result = bt_l2cap_chan_send(&session->br_chan.chan, buf);
1028 if (result < 0) {
1029 LOG_ERR("Error:L2CAP send fail - result = %d", result);
1030 bt_avdtp_clear_req(session);
1031 return result;
1032 }
1033
1034 /*Save the sent request*/
1035 req->sig = AVDTP_GET_SIG_ID(hdr->signal_id);
1036 req->tid = AVDTP_GET_TR_ID(hdr->hdr);
1037 LOG_DBG("sig 0x%02X, tid 0x%02X", req->sig, req->tid);
1038
1039 /* Init the timer */
1040 k_work_init_delayable(&session->timeout_work, avdtp_timeout);
1041 /* Start timeout work */
1042 k_work_reschedule(&session->timeout_work, AVDTP_TIMEOUT);
1043 return result;
1044 }
1045
avdtp_create_pdu(uint8_t msg_type,uint8_t pkt_type,uint8_t sig_id)1046 static struct net_buf *avdtp_create_pdu(uint8_t msg_type, uint8_t pkt_type, uint8_t sig_id)
1047 {
1048 struct net_buf *buf;
1049 static uint8_t tid;
1050 struct bt_avdtp_single_sig_hdr *hdr;
1051
1052 LOG_DBG("");
1053
1054 buf = bt_l2cap_create_pdu(NULL, 0);
1055
1056 hdr = net_buf_add(buf, sizeof(*hdr));
1057
1058 hdr->hdr = (msg_type | pkt_type << AVDTP_PKT_POSITION | tid++ << AVDTP_TID_POSITION);
1059 tid %= 16; /* Loop for 16*/
1060 hdr->signal_id = sig_id & AVDTP_SIGID_MASK;
1061
1062 LOG_DBG("hdr = 0x%02X, Signal_ID = 0x%02X", hdr->hdr, hdr->signal_id);
1063 return buf;
1064 }
1065
1066 /* L2CAP Interface callbacks */
bt_avdtp_l2cap_connected(struct bt_l2cap_chan * chan)1067 void bt_avdtp_l2cap_connected(struct bt_l2cap_chan *chan)
1068 {
1069 struct bt_avdtp *session;
1070
1071 if (!chan) {
1072 LOG_ERR("Invalid AVDTP chan");
1073 return;
1074 }
1075
1076 session = AVDTP_CHAN(chan);
1077 LOG_DBG("chan %p session %p", chan, session);
1078
1079 /* notify a2dp connection result */
1080 session->ops->connected(session);
1081 }
1082
bt_avdtp_l2cap_disconnected(struct bt_l2cap_chan * chan)1083 void bt_avdtp_l2cap_disconnected(struct bt_l2cap_chan *chan)
1084 {
1085 struct bt_avdtp *session = AVDTP_CHAN(chan);
1086
1087 LOG_DBG("chan %p session %p", chan, session);
1088 session->br_chan.chan.conn = NULL;
1089 /* Clear the Pending req if set*/
1090 if (session->req) {
1091 struct bt_avdtp_req *req = session->req;
1092
1093 req->status = BT_AVDTP_BAD_STATE;
1094 bt_avdtp_clear_req(session);
1095
1096 if (req->func != NULL) {
1097 req->func(req, NULL);
1098 }
1099 }
1100
1101 /* notify a2dp disconnect */
1102 session->ops->disconnected(session);
1103 }
1104
1105 void (*cmd_handler[])(struct bt_avdtp *session, struct net_buf *buf, uint8_t tid) = {
1106 avdtp_discover_cmd, /* BT_AVDTP_DISCOVER */
1107 avdtp_get_capabilities_cmd, /* BT_AVDTP_GET_CAPABILITIES */
1108 avdtp_set_configuration_cmd, /* BT_AVDTP_SET_CONFIGURATION */
1109 NULL, /* BT_AVDTP_GET_CONFIGURATION */
1110 avdtp_re_configure_cmd, /* BT_AVDTP_RECONFIGURE */
1111 avdtp_open_cmd, /* BT_AVDTP_OPEN */
1112 avdtp_start_cmd, /* BT_AVDTP_START */
1113 avdtp_close_cmd, /* BT_AVDTP_CLOSE */
1114 avdtp_suspend_cmd, /* BT_AVDTP_SUSPEND */
1115 avdtp_abort_cmd, /* BT_AVDTP_ABORT */
1116 NULL, /* BT_AVDTP_SECURITY_CONTROL */
1117 NULL, /* BT_AVDTP_GET_ALL_CAPABILITIES */
1118 NULL, /* BT_AVDTP_DELAYREPORT */
1119 };
1120
1121 void (*rsp_handler[])(struct bt_avdtp *session, struct net_buf *buf, uint8_t msg_type) = {
1122 avdtp_discover_rsp, /* BT_AVDTP_DISCOVER */
1123 avdtp_get_capabilities_rsp, /* BT_AVDTP_GET_CAPABILITIES */
1124 avdtp_set_configuration_rsp, /* BT_AVDTP_SET_CONFIGURATION */
1125 NULL, /* BT_AVDTP_GET_CONFIGURATION */
1126 avdtp_re_configure_rsp, /* BT_AVDTP_RECONFIGURE */
1127 avdtp_open_rsp, /* BT_AVDTP_OPEN */
1128 avdtp_start_rsp, /* BT_AVDTP_START */
1129 avdtp_close_rsp, /* BT_AVDTP_CLOSE */
1130 avdtp_suspend_rsp, /* BT_AVDTP_SUSPEND */
1131 avdtp_abort_rsp, /* BT_AVDTP_ABORT */
1132 NULL, /* BT_AVDTP_SECURITY_CONTROL */
1133 NULL, /* BT_AVDTP_GET_ALL_CAPABILITIES */
1134 NULL, /* BT_AVDTP_DELAYREPORT */
1135 };
1136
bt_avdtp_l2cap_recv(struct bt_l2cap_chan * chan,struct net_buf * buf)1137 int bt_avdtp_l2cap_recv(struct bt_l2cap_chan *chan, struct net_buf *buf)
1138 {
1139 struct bt_avdtp_single_sig_hdr *hdr;
1140 struct bt_avdtp *session = AVDTP_CHAN(chan);
1141 uint8_t msgtype, pack_type, sigid, tid;
1142 struct net_buf *rsp_buf;
1143 int err;
1144
1145 if (buf->len < sizeof(*hdr)) {
1146 LOG_ERR("Recvd Wrong AVDTP Header");
1147 return -EINVAL;
1148 }
1149
1150 hdr = net_buf_pull_mem(buf, sizeof(*hdr));
1151 pack_type = AVDTP_GET_PKT_TYPE(hdr->hdr);
1152 msgtype = AVDTP_GET_MSG_TYPE(hdr->hdr);
1153 sigid = AVDTP_GET_SIG_ID(hdr->signal_id);
1154 tid = AVDTP_GET_TR_ID(hdr->hdr);
1155
1156 LOG_DBG("pack_type[0x%02x] msg_type[0x%02x] sig_id[0x%02x] tid[0x%02x]", pack_type, msgtype,
1157 sigid, tid);
1158
1159 /* TODO: only support single packet now */
1160 if (pack_type != BT_AVDTP_PACKET_TYPE_SINGLE) {
1161 if (pack_type == BT_AVDTP_PACKET_TYPE_START) {
1162 if (buf->len < 1U) {
1163 return -EINVAL;
1164 }
1165
1166 sigid = net_buf_pull_u8(buf);
1167 goto send_reject;
1168 }
1169
1170 /* discard the continue packet and end packet */
1171 return -EINVAL;
1172 }
1173
1174 if (msgtype == BT_AVDTP_CMD) {
1175 if (sigid != 0U && sigid <= BT_AVDTP_DELAYREPORT &&
1176 cmd_handler[sigid - 1U] != NULL) {
1177 cmd_handler[sigid - 1U](session, buf, tid);
1178 return 0;
1179 }
1180 /* goto send_reject; */
1181 } else {
1182 /* validate if the response is expected*/
1183 if (session->req == NULL) {
1184 LOG_DBG("Unexpected peer response");
1185 return -EINVAL;
1186 }
1187
1188 if (session->req->sig != sigid || session->req->tid != tid) {
1189 LOG_DBG("Peer mismatch resp, expected sig[0x%02x]"
1190 "tid[0x%02x]",
1191 session->req->sig, session->req->tid);
1192 return -EINVAL;
1193 }
1194
1195 if (sigid != 0U && sigid <= BT_AVDTP_DELAYREPORT &&
1196 cmd_handler[sigid - 1U] != NULL) {
1197 rsp_handler[sigid - 1U](session, buf, msgtype);
1198 return 0;
1199 }
1200
1201 /* discard unsupported response packet */
1202 return -EINVAL;
1203 }
1204
1205 send_reject:
1206 rsp_buf = avdtp_create_reply_pdu(BT_AVDTP_GEN_REJECT, BT_AVDTP_PACKET_TYPE_SINGLE, sigid,
1207 tid);
1208 if (!rsp_buf) {
1209 LOG_ERR("Error: No Buff available");
1210 return -EINVAL;
1211 }
1212
1213 err = bt_l2cap_chan_send(&session->br_chan.chan, rsp_buf);
1214 if (err < 0) {
1215 net_buf_unref(rsp_buf);
1216 LOG_ERR("Error:L2CAP send fail - result = %d", err);
1217 }
1218
1219 return err;
1220 }
1221
1222 static const struct bt_l2cap_chan_ops signal_chan_ops = {
1223 .connected = bt_avdtp_l2cap_connected,
1224 .disconnected = bt_avdtp_l2cap_disconnected,
1225 .recv = bt_avdtp_l2cap_recv,
1226 };
1227
1228 /*A2DP Layer interface */
bt_avdtp_connect(struct bt_conn * conn,struct bt_avdtp * session)1229 int bt_avdtp_connect(struct bt_conn *conn, struct bt_avdtp *session)
1230 {
1231 if (!session) {
1232 return -EINVAL;
1233 }
1234
1235 /* there are headsets that initiate the AVDTP signal l2cap connection
1236 * at the same time when DUT initiates the same l2cap connection.
1237 * Use the `conn` to check whether the l2cap creation is already started.
1238 * The whole `session` is cleared by upper layer if it is new l2cap connection.
1239 */
1240 k_sem_take(&avdtp_sem_lock, K_FOREVER);
1241
1242 if (session->br_chan.chan.conn != NULL) {
1243 k_sem_give(&avdtp_sem_lock);
1244 return -ENOMEM;
1245 }
1246
1247 session->br_chan.chan.conn = conn;
1248 k_sem_give(&avdtp_sem_lock);
1249 /* Locking semaphore initialized to 1 (unlocked) */
1250 k_sem_init(&session->sem_lock, 1, 1);
1251 session->br_chan.rx.mtu = BT_L2CAP_RX_MTU;
1252 session->br_chan.chan.ops = &signal_chan_ops;
1253 session->br_chan.required_sec_level = BT_SECURITY_L2;
1254
1255 return bt_l2cap_chan_connect(conn, &session->br_chan.chan, BT_L2CAP_PSM_AVDTP);
1256 }
1257
bt_avdtp_disconnect(struct bt_avdtp * session)1258 int bt_avdtp_disconnect(struct bt_avdtp *session)
1259 {
1260 if (!session) {
1261 return -EINVAL;
1262 }
1263
1264 LOG_DBG("session %p", session);
1265
1266 return bt_l2cap_chan_disconnect(&session->br_chan.chan);
1267 }
1268
bt_avdtp_l2cap_accept(struct bt_conn * conn,struct bt_l2cap_server * server,struct bt_l2cap_chan ** chan)1269 int bt_avdtp_l2cap_accept(struct bt_conn *conn, struct bt_l2cap_server *server,
1270 struct bt_l2cap_chan **chan)
1271 {
1272 struct bt_avdtp *session = NULL;
1273 int result;
1274
1275 LOG_DBG("conn %p", conn);
1276 /* Get the AVDTP session from upper layer */
1277 result = event_cb->accept(conn, &session);
1278 if (result < 0) {
1279 return result;
1280 }
1281
1282 /* there are headsets that initiate the AVDTP signal l2cap connection
1283 * at the same time when DUT initiates the same l2cap connection.
1284 * Use the `conn` to check whether the l2cap creation is already started.
1285 * The whole `session` is cleared by upper layer if it is new l2cap connection.
1286 */
1287 k_sem_take(&avdtp_sem_lock, K_FOREVER);
1288
1289 if (session->br_chan.chan.conn == NULL) {
1290 session->br_chan.chan.conn = conn;
1291 k_sem_give(&avdtp_sem_lock);
1292 /* Locking semaphore initialized to 1 (unlocked) */
1293 k_sem_init(&session->sem_lock, 1, 1);
1294 session->br_chan.chan.ops = &signal_chan_ops;
1295 session->br_chan.rx.mtu = BT_L2CAP_RX_MTU;
1296 *chan = &session->br_chan.chan;
1297 } else {
1298 k_sem_give(&avdtp_sem_lock);
1299
1300 /* get the current opening endpoint */
1301 if (session->current_sep != NULL) {
1302 session->current_sep->session = session;
1303 session->current_sep->chan.chan.ops = &stream_chan_ops;
1304 session->current_sep->chan.rx.mtu = BT_L2CAP_RX_MTU;
1305 session->current_sep->chan.required_sec_level = BT_SECURITY_L2;
1306 *chan = &session->current_sep->chan.chan;
1307 session->current_sep = NULL;
1308 } else {
1309 return -ENOMEM;
1310 }
1311 }
1312
1313 return 0;
1314 }
1315
1316 /* Application will register its callback */
bt_avdtp_register(struct bt_avdtp_event_cb * cb)1317 int bt_avdtp_register(struct bt_avdtp_event_cb *cb)
1318 {
1319 LOG_DBG("");
1320
1321 if (event_cb) {
1322 return -EALREADY;
1323 }
1324
1325 event_cb = cb;
1326
1327 return 0;
1328 }
1329
bt_avdtp_register_sep(uint8_t media_type,uint8_t sep_type,struct bt_avdtp_sep * sep)1330 int bt_avdtp_register_sep(uint8_t media_type, uint8_t sep_type, struct bt_avdtp_sep *sep)
1331 {
1332 LOG_DBG("");
1333
1334 static uint8_t bt_avdtp_sep = BT_AVDTP_MIN_SEID;
1335
1336 if (!sep) {
1337 return -EIO;
1338 }
1339
1340 if (bt_avdtp_sep == BT_AVDTP_MAX_SEID) {
1341 return -EIO;
1342 }
1343
1344 k_sem_take(&avdtp_sem_lock, K_FOREVER);
1345 /* the id allocation need be locked to protect it */
1346 sep->sep_info.id = bt_avdtp_sep++;
1347 sep->sep_info.inuse = 0U;
1348 sep->sep_info.media_type = media_type;
1349 sep->sep_info.tsep = sep_type;
1350 /* Locking semaphore initialized to 1 (unlocked) */
1351 k_sem_init(&sep->sem_lock, 1, 1);
1352 bt_avdtp_set_state_lock(sep, AVDTP_IDLE);
1353
1354 sys_slist_append(&seps, &sep->_node);
1355 k_sem_give(&avdtp_sem_lock);
1356
1357 return 0;
1358 }
1359
1360 /* init function */
bt_avdtp_init(void)1361 int bt_avdtp_init(void)
1362 {
1363 int err;
1364 static struct bt_l2cap_server avdtp_l2cap = {
1365 .psm = BT_L2CAP_PSM_AVDTP,
1366 .sec_level = BT_SECURITY_L2,
1367 .accept = bt_avdtp_l2cap_accept,
1368 };
1369
1370 LOG_DBG("");
1371
1372 /* Register AVDTP PSM with L2CAP */
1373 err = bt_l2cap_br_server_register(&avdtp_l2cap);
1374 if (err < 0) {
1375 LOG_ERR("AVDTP L2CAP Registration failed %d", err);
1376 }
1377
1378 return err;
1379 }
1380
1381 /* AVDTP Discover Request */
bt_avdtp_discover(struct bt_avdtp * session,struct bt_avdtp_discover_params * param)1382 int bt_avdtp_discover(struct bt_avdtp *session, struct bt_avdtp_discover_params *param)
1383 {
1384 struct net_buf *buf;
1385 int err;
1386
1387 LOG_DBG("");
1388 if (!param || !session) {
1389 LOG_DBG("Error: Callback/Session not valid");
1390 return -EINVAL;
1391 }
1392
1393 buf = avdtp_create_pdu(BT_AVDTP_CMD, BT_AVDTP_PACKET_TYPE_SINGLE, BT_AVDTP_DISCOVER);
1394 if (!buf) {
1395 LOG_ERR("Error: No Buff available");
1396 return -ENOMEM;
1397 }
1398
1399 err = avdtp_send(session, buf, ¶m->req);
1400 if (err) {
1401 net_buf_unref(buf);
1402 }
1403
1404 return err;
1405 }
1406
bt_avdtp_parse_sep(struct net_buf * buf,struct bt_avdtp_sep_info * sep_info)1407 int bt_avdtp_parse_sep(struct net_buf *buf, struct bt_avdtp_sep_info *sep_info)
1408 {
1409 struct bt_avdtp_sep_data *sep_data;
1410
1411 if ((sep_info != NULL) && (buf != NULL)) {
1412 if (buf->len >= sizeof(*sep_data)) {
1413 sep_data = net_buf_pull_mem(buf, sizeof(*sep_data));
1414 sep_info->inuse = sep_data->inuse;
1415 sep_info->id = sep_data->id;
1416 sep_info->tsep = sep_data->tsep;
1417 sep_info->media_type = sep_data->media_type;
1418 return 0;
1419 }
1420 }
1421
1422 return -EINVAL;
1423 }
1424
1425 /* AVDTP Get Capabilities Request */
bt_avdtp_get_capabilities(struct bt_avdtp * session,struct bt_avdtp_get_capabilities_params * param)1426 int bt_avdtp_get_capabilities(struct bt_avdtp *session,
1427 struct bt_avdtp_get_capabilities_params *param)
1428 {
1429 struct net_buf *buf;
1430 int err;
1431
1432 LOG_DBG("");
1433 if (!param || !session) {
1434 LOG_DBG("Error: Callback/Session not valid");
1435 return -EINVAL;
1436 }
1437
1438 buf = avdtp_create_pdu(BT_AVDTP_CMD, BT_AVDTP_PACKET_TYPE_SINGLE,
1439 BT_AVDTP_GET_CAPABILITIES);
1440 if (!buf) {
1441 LOG_ERR("Error: No Buff available");
1442 return -ENOMEM;
1443 }
1444
1445 /* Body of the message */
1446 net_buf_add_u8(buf, (param->stream_endpoint_id << 2U));
1447
1448 err = avdtp_send(session, buf, ¶m->req);
1449 if (err) {
1450 net_buf_unref(buf);
1451 }
1452
1453 return err;
1454 }
1455
bt_avdtp_parse_capability_codec(struct net_buf * buf,uint8_t * codec_type,uint8_t ** codec_info_element,uint16_t * codec_info_element_len)1456 int bt_avdtp_parse_capability_codec(struct net_buf *buf, uint8_t *codec_type,
1457 uint8_t **codec_info_element, uint16_t *codec_info_element_len)
1458 {
1459 uint8_t data;
1460 uint8_t length;
1461
1462 if (!buf) {
1463 LOG_DBG("Error: buf not valid");
1464 return -EINVAL;
1465 }
1466
1467 while (buf->len) {
1468 data = net_buf_pull_u8(buf);
1469 switch (data) {
1470 case BT_AVDTP_SERVICE_MEDIA_TRANSPORT:
1471 case BT_AVDTP_SERVICE_REPORTING:
1472 case BT_AVDTP_SERVICE_MEDIA_RECOVERY:
1473 case BT_AVDTP_SERVICE_CONTENT_PROTECTION:
1474 case BT_AVDTP_SERVICE_HEADER_COMPRESSION:
1475 case BT_AVDTP_SERVICE_MULTIPLEXING:
1476 case BT_AVDTP_SERVICE_DELAY_REPORTING:
1477 if (buf->len < 1U) {
1478 return -EINVAL;
1479 }
1480
1481 length = net_buf_pull_u8(buf);
1482 if (length > 0) {
1483 if (buf->len < length) {
1484 return -EINVAL;
1485 }
1486
1487 net_buf_pull_mem(buf, length);
1488 }
1489 break;
1490
1491 case BT_AVDTP_SERVICE_MEDIA_CODEC:
1492 if (buf->len < 1U) {
1493 return -EINVAL;
1494 }
1495
1496 length = net_buf_pull_u8(buf);
1497 if (buf->len < length) {
1498 return -EINVAL;
1499 }
1500
1501 if (length > 3) {
1502 data = net_buf_pull_u8(buf);
1503 if (data == BT_AVDTP_AUDIO) {
1504 data = net_buf_pull_u8(buf);
1505 *codec_type = data;
1506 *codec_info_element_len = (length - 2);
1507 *codec_info_element =
1508 net_buf_pull_mem(buf, (*codec_info_element_len));
1509 return 0;
1510 }
1511 }
1512 break;
1513
1514 default:
1515 break;
1516 }
1517 }
1518 return -EINVAL;
1519 }
1520
avdtp_process_configure_command(struct bt_avdtp * session,uint8_t cmd,struct bt_avdtp_set_configuration_params * param)1521 static int avdtp_process_configure_command(struct bt_avdtp *session, uint8_t cmd,
1522 struct bt_avdtp_set_configuration_params *param)
1523 {
1524 struct net_buf *buf;
1525 int err;
1526
1527 LOG_DBG("");
1528 if (!param || !session) {
1529 LOG_DBG("Error: Callback/Session not valid");
1530 return -EINVAL;
1531 }
1532
1533 buf = avdtp_create_pdu(BT_AVDTP_CMD, BT_AVDTP_PACKET_TYPE_SINGLE, cmd);
1534 if (!buf) {
1535 LOG_ERR("Error: No Buff available");
1536 return -ENOMEM;
1537 }
1538
1539 /* Body of the message */
1540 /* ACP Stream Endpoint ID */
1541 net_buf_add_u8(buf, (param->acp_stream_ep_id << 2U));
1542 /* INT Stream Endpoint ID */
1543 net_buf_add_u8(buf, (param->int_stream_endpoint_id << 2U));
1544 /* Service Category: Media Transport */
1545 net_buf_add_u8(buf, BT_AVDTP_SERVICE_MEDIA_TRANSPORT);
1546 /* LOSC */
1547 net_buf_add_u8(buf, 0);
1548 /* Service Category: Media Codec */
1549 net_buf_add_u8(buf, BT_AVDTP_SERVICE_MEDIA_CODEC);
1550 /* LOSC */
1551 net_buf_add_u8(buf, param->codec_specific_ie_len + 2);
1552 /* Media Type */
1553 net_buf_add_u8(buf, param->media_type << 4U);
1554 /* Media Codec Type */
1555 net_buf_add_u8(buf, param->media_codec_type);
1556 /* Codec Info Element */
1557 net_buf_add_mem(buf, param->codec_specific_ie, param->codec_specific_ie_len);
1558
1559 err = avdtp_send(session, buf, ¶m->req);
1560 if (err) {
1561 net_buf_unref(buf);
1562 }
1563
1564 return err;
1565 }
1566
bt_avdtp_set_configuration(struct bt_avdtp * session,struct bt_avdtp_set_configuration_params * param)1567 int bt_avdtp_set_configuration(struct bt_avdtp *session,
1568 struct bt_avdtp_set_configuration_params *param)
1569 {
1570 if (!param || !session || !param->sep) {
1571 LOG_DBG("Error: parameters not valid");
1572 return -EINVAL;
1573 }
1574
1575 if (param->sep->state != AVDTP_IDLE) {
1576 return -EINVAL;
1577 }
1578
1579 return avdtp_process_configure_command(session, BT_AVDTP_SET_CONFIGURATION, param);
1580 }
1581
bt_avdtp_reconfigure(struct bt_avdtp * session,struct bt_avdtp_set_configuration_params * param)1582 int bt_avdtp_reconfigure(struct bt_avdtp *session, struct bt_avdtp_set_configuration_params *param)
1583 {
1584 if (!param || !session || !param->sep) {
1585 LOG_DBG("Error: parameters not valid");
1586 return -EINVAL;
1587 }
1588
1589 if (param->sep->state != AVDTP_OPEN) {
1590 return -EINVAL;
1591 }
1592
1593 return avdtp_process_configure_command(session, BT_AVDTP_RECONFIGURE, param);
1594 }
1595
bt_avdtp_ctrl(struct bt_avdtp * session,struct bt_avdtp_ctrl_params * param,uint8_t ctrl,uint8_t check_state)1596 static int bt_avdtp_ctrl(struct bt_avdtp *session, struct bt_avdtp_ctrl_params *param, uint8_t ctrl,
1597 uint8_t check_state)
1598 {
1599 struct net_buf *buf;
1600 int err;
1601
1602 LOG_DBG("");
1603 if (!param || !session || !param->sep) {
1604 LOG_DBG("Error: parameters not valid");
1605 return -EINVAL;
1606 }
1607
1608 if (!(param->sep->state & check_state)) {
1609 return -EINVAL;
1610 }
1611
1612 buf = avdtp_create_pdu(BT_AVDTP_CMD, BT_AVDTP_PACKET_TYPE_SINGLE, ctrl);
1613 if (!buf) {
1614 LOG_ERR("Error: No Buff available");
1615 return -ENOMEM;
1616 }
1617
1618 /* Body of the message */
1619 /* ACP Stream Endpoint ID */
1620 net_buf_add_u8(buf, (param->acp_stream_ep_id << 2U));
1621
1622 err = avdtp_send(session, buf, ¶m->req);
1623 if (err) {
1624 net_buf_unref(buf);
1625 }
1626
1627 return err;
1628 }
1629
bt_avdtp_open(struct bt_avdtp * session,struct bt_avdtp_ctrl_params * param)1630 int bt_avdtp_open(struct bt_avdtp *session, struct bt_avdtp_ctrl_params *param)
1631 {
1632 return bt_avdtp_ctrl(session, param, BT_AVDTP_OPEN, AVDTP_CONFIGURED);
1633 }
1634
bt_avdtp_close(struct bt_avdtp * session,struct bt_avdtp_ctrl_params * param)1635 int bt_avdtp_close(struct bt_avdtp *session, struct bt_avdtp_ctrl_params *param)
1636 {
1637 return bt_avdtp_ctrl(session, param, BT_AVDTP_CLOSE, AVDTP_OPEN | AVDTP_STREAMING);
1638 }
1639
bt_avdtp_start(struct bt_avdtp * session,struct bt_avdtp_ctrl_params * param)1640 int bt_avdtp_start(struct bt_avdtp *session, struct bt_avdtp_ctrl_params *param)
1641 {
1642 int err;
1643
1644 err = bt_avdtp_ctrl(session, param, BT_AVDTP_START, AVDTP_OPEN);
1645 if (!err && param->sep->sep_info.tsep == BT_AVDTP_SINK) {
1646 bt_avdtp_set_state_lock(param->sep, AVDTP_STREAMING);
1647 }
1648
1649 return err;
1650 }
1651
bt_avdtp_suspend(struct bt_avdtp * session,struct bt_avdtp_ctrl_params * param)1652 int bt_avdtp_suspend(struct bt_avdtp *session, struct bt_avdtp_ctrl_params *param)
1653 {
1654 return bt_avdtp_ctrl(session, param, BT_AVDTP_SUSPEND, AVDTP_STREAMING);
1655 }
1656
bt_avdtp_abort(struct bt_avdtp * session,struct bt_avdtp_ctrl_params * param)1657 int bt_avdtp_abort(struct bt_avdtp *session, struct bt_avdtp_ctrl_params *param)
1658 {
1659 return bt_avdtp_ctrl(session, param, BT_AVDTP_ABORT,
1660 AVDTP_CONFIGURED | AVDTP_OPENING | AVDTP_OPEN | AVDTP_STREAMING |
1661 AVDTP_CLOSING);
1662 }
1663
bt_avdtp_send_media_data(struct bt_avdtp_sep * sep,struct net_buf * buf)1664 int bt_avdtp_send_media_data(struct bt_avdtp_sep *sep, struct net_buf *buf)
1665 {
1666 int err;
1667
1668 if (sep->state != AVDTP_STREAMING || sep->sep_info.tsep != BT_AVDTP_SOURCE) {
1669 return -EIO;
1670 }
1671
1672 err = bt_l2cap_chan_send(&sep->chan.chan, buf);
1673 if (err < 0) {
1674 LOG_ERR("Error:L2CAP send fail - err = %d", err);
1675 return err;
1676 }
1677
1678 return err;
1679 }
1680
bt_avdtp_get_media_mtu(struct bt_avdtp_sep * sep)1681 uint32_t bt_avdtp_get_media_mtu(struct bt_avdtp_sep *sep)
1682 {
1683 return sep->chan.tx.mtu;
1684 }
1685