1 /*
2 * Audio Video Distribution Protocol
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 */
7
8 #include <ble_os.h>
9 #include <string.h>
10 // #include <strings.h>
11 #include <bt_errno.h>
12 #include <atomic.h>
13 #include <misc/byteorder.h>
14 #include <misc/util.h>
15
16 #include <bluetooth/hci.h>
17 #include <bluetooth/bluetooth.h>
18 #include <bluetooth/l2cap.h>
19 #include <bluetooth/avdtp.h>
20 #ifdef CONFIG_BT_AVDTP
21
22 #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BT_DEBUG_AVDTP)
23 #define LOG_MODULE_NAME bt_avdtp
24 #include "common/log.h"
25
26 #include "hci_core.h"
27 #include "conn_internal.h"
28 #include "l2cap_internal.h"
29 #include "avdtp_internal.h"
30
31 #define AVDTP_MSG_POISTION 0x00
32 #define AVDTP_PKT_POSITION 0x02
33 #define AVDTP_TID_POSITION 0x04
34 #define AVDTP_SIGID_MASK 0x3f
35
36 #define AVDTP_GET_TR_ID(hdr) ((hdr & 0xf0) >> AVDTP_TID_POSITION)
37 #define AVDTP_GET_MSG_TYPE(hdr) (hdr & 0x03)
38 #define AVDTP_GET_PKT_TYPE(hdr) ((hdr & 0x0c) >> AVDTP_PKT_POSITION)
39 #define AVDTP_GET_SIG_ID(s) (s & AVDTP_SIGID_MASK)
40
41 static struct bt_avdtp_event_cb *event_cb;
42
43 static struct bt_avdtp_seid_lsep *lseps;
44
45 #define AVDTP_CHAN(_ch) CONTAINER_OF(_ch, struct bt_avdtp, br_chan.chan)
46
47 #define AVDTP_KWORK(_work) CONTAINER_OF(_work, struct bt_avdtp_req,\
48 timeout_work)
49
50 #define AVDTP_TIMEOUT K_SECONDS(6)
51
52 static const struct {
53 u8_t sig_id;
54 void (*func)(struct bt_avdtp *session, struct net_buf *buf,
55 u8_t msg_type);
56 } handler[] = {
57 };
58
avdtp_send(struct bt_avdtp * session,struct net_buf * buf,struct bt_avdtp_req * req)59 static int avdtp_send(struct bt_avdtp *session,
60 struct net_buf *buf, struct bt_avdtp_req *req)
61 {
62 int result;
63 struct bt_avdtp_single_sig_hdr *hdr;
64
65 hdr = (struct bt_avdtp_single_sig_hdr *)buf->data;
66
67 result = bt_l2cap_chan_send(&session->br_chan.chan, buf);
68 if (result < 0) {
69 BT_ERR("Error:L2CAP send fail - result = %d", result);
70 return result;
71 }
72
73 /*Save the sent request*/
74 req->sig = AVDTP_GET_SIG_ID(hdr->signal_id);
75 req->tid = AVDTP_GET_TR_ID(hdr->hdr);
76 BT_DBG("sig 0x%02X, tid 0x%02X", req->sig, req->tid);
77
78 session->req = req;
79 /* Start timeout work */
80 k_delayed_work_submit(&session->req->timeout_work, AVDTP_TIMEOUT);
81 return result;
82 }
83
avdtp_create_pdu(u8_t msg_type,u8_t pkt_type,u8_t sig_id)84 static struct net_buf *avdtp_create_pdu(u8_t msg_type,
85 u8_t pkt_type,
86 u8_t sig_id)
87 {
88 struct net_buf *buf;
89 static u8_t tid;
90 struct bt_avdtp_single_sig_hdr *hdr;
91
92 BT_DBG("");
93
94 buf = bt_l2cap_create_pdu(NULL, 0);
95
96 hdr = net_buf_add(buf, sizeof(*hdr));
97
98 hdr->hdr = (msg_type | pkt_type << AVDTP_PKT_POSITION |
99 tid++ << AVDTP_TID_POSITION);
100 tid %= 16; /* Loop for 16*/
101 hdr->signal_id = sig_id & AVDTP_SIGID_MASK;
102
103 BT_DBG("hdr = 0x%02X, Signal_ID = 0x%02X", hdr->hdr, hdr->signal_id);
104 return buf;
105 }
106
107 /* Timeout handler */
avdtp_timeout(struct k_work * work)108 static void avdtp_timeout(struct k_work *work)
109 {
110 BT_DBG("Failed Signal_id = %d", (AVDTP_KWORK(work))->sig);
111
112 /* Gracefully Disconnect the Signalling and streaming L2cap chann*/
113
114 }
115
116 /* L2CAP Interface callbacks */
bt_avdtp_l2cap_connected(struct bt_l2cap_chan * chan)117 void bt_avdtp_l2cap_connected(struct bt_l2cap_chan *chan)
118 {
119 struct bt_avdtp *session;
120
121 if (!chan) {
122 BT_ERR("Invalid AVDTP chan");
123 return;
124 }
125
126 session = AVDTP_CHAN(chan);
127 BT_DBG("chan %p session %p", chan, session);
128 /* Init the timer */
129 k_delayed_work_init(&session->req->timeout_work, avdtp_timeout);
130
131 }
132
bt_avdtp_l2cap_disconnected(struct bt_l2cap_chan * chan)133 void bt_avdtp_l2cap_disconnected(struct bt_l2cap_chan *chan)
134 {
135 struct bt_avdtp *session = AVDTP_CHAN(chan);
136
137 BT_DBG("chan %p session %p", chan, session);
138 session->br_chan.chan.conn = NULL;
139 /* Clear the Pending req if set*/
140 }
141
bt_avdtp_l2cap_encrypt_changed(struct bt_l2cap_chan * chan,u8_t status)142 void bt_avdtp_l2cap_encrypt_changed(struct bt_l2cap_chan *chan, u8_t status)
143 {
144 BT_DBG("");
145 }
146
bt_avdtp_l2cap_recv(struct bt_l2cap_chan * chan,struct net_buf * buf)147 int bt_avdtp_l2cap_recv(struct bt_l2cap_chan *chan, struct net_buf *buf)
148 {
149 struct bt_avdtp_single_sig_hdr *hdr;
150 struct bt_avdtp *session = AVDTP_CHAN(chan);
151 u8_t i, msgtype, sigid, tid;
152
153 if (buf->len < sizeof(*hdr)) {
154 BT_ERR("Recvd Wrong AVDTP Header");
155 return 0;
156 }
157
158 hdr = net_buf_pull_mem(buf, sizeof(*hdr));
159 msgtype = AVDTP_GET_MSG_TYPE(hdr->hdr);
160 sigid = AVDTP_GET_SIG_ID(hdr->signal_id);
161 tid = AVDTP_GET_TR_ID(hdr->hdr);
162
163 BT_DBG("msg_type[0x%02x] sig_id[0x%02x] tid[0x%02x]",
164 msgtype, sigid, tid);
165
166 /* validate if there is an outstanding resp expected*/
167 if (msgtype != BT_AVDTP_CMD) {
168 if (session->req == NULL) {
169 BT_DBG("Unexpected peer response");
170 return 0;
171 }
172
173 if (session->req->sig != sigid ||
174 session->req->tid != tid) {
175 BT_DBG("Peer mismatch resp, expected sig[0x%02x]"
176 "tid[0x%02x]", session->req->sig,
177 session->req->tid);
178 return 0;
179 }
180 }
181
182 for (i = 0U; i < ARRAY_SIZE(handler); i++) {
183 if (sigid == handler[i].sig_id) {
184 handler[i].func(session, buf, msgtype);
185 return 0;
186 }
187 }
188
189 return 0;
190 }
191
192 /*A2DP Layer interface */
bt_avdtp_connect(struct bt_conn * conn,struct bt_avdtp * session)193 int bt_avdtp_connect(struct bt_conn *conn, struct bt_avdtp *session)
194 {
195 static const struct bt_l2cap_chan_ops ops = {
196 .connected = bt_avdtp_l2cap_connected,
197 .disconnected = bt_avdtp_l2cap_disconnected,
198 .encrypt_change = bt_avdtp_l2cap_encrypt_changed,
199 .recv = bt_avdtp_l2cap_recv
200 };
201
202 if (!session) {
203 return -EINVAL;
204 }
205
206 session->br_chan.chan.ops = &ops;
207 session->br_chan.chan.required_sec_level = BT_SECURITY_L2;
208
209 return bt_l2cap_chan_connect(conn, &session->br_chan.chan,
210 BT_L2CAP_PSM_AVDTP);
211 }
212
bt_avdtp_disconnect(struct bt_avdtp * session)213 int bt_avdtp_disconnect(struct bt_avdtp *session)
214 {
215 if (!session) {
216 return -EINVAL;
217 }
218
219 BT_DBG("session %p", session);
220
221 return bt_l2cap_chan_disconnect(&session->br_chan.chan);
222 }
223
bt_avdtp_l2cap_accept(struct bt_conn * conn,struct bt_l2cap_chan ** chan)224 int bt_avdtp_l2cap_accept(struct bt_conn *conn, struct bt_l2cap_chan **chan)
225 {
226 struct bt_avdtp *session = NULL;
227 int result;
228 static const struct bt_l2cap_chan_ops ops = {
229 .connected = bt_avdtp_l2cap_connected,
230 .disconnected = bt_avdtp_l2cap_disconnected,
231 .recv = bt_avdtp_l2cap_recv,
232 };
233
234 BT_DBG("conn %p", conn);
235 /* Get the AVDTP session from upper layer */
236 result = event_cb->accept(conn, &session);
237 if (result < 0) {
238 return result;
239 }
240 session->br_chan.chan.ops = &ops;
241 session->br_chan.rx.mtu = BT_AVDTP_MAX_MTU;
242 *chan = &session->br_chan.chan;
243 return 0;
244 }
245
246 /* Application will register its callback */
bt_avdtp_register(struct bt_avdtp_event_cb * cb)247 int bt_avdtp_register(struct bt_avdtp_event_cb *cb)
248 {
249 BT_DBG("");
250
251 if (event_cb) {
252 return -EALREADY;
253 }
254
255 event_cb = cb;
256
257 return 0;
258 }
259
bt_avdtp_register_sep(u8_t media_type,u8_t role,struct bt_avdtp_seid_lsep * lsep)260 int bt_avdtp_register_sep(u8_t media_type, u8_t role,
261 struct bt_avdtp_seid_lsep *lsep)
262 {
263 BT_DBG("");
264
265 static u8_t bt_avdtp_seid = BT_AVDTP_MIN_SEID;
266
267 if (!lsep) {
268 return -EIO;
269 }
270
271 if (bt_avdtp_seid == BT_AVDTP_MAX_SEID) {
272 return -EIO;
273 }
274
275 lsep->sep.id = bt_avdtp_seid++;
276 lsep->sep.inuse = 0U;
277 lsep->sep.media_type = media_type;
278 lsep->sep.tsep = role;
279
280 lsep->next = lseps;
281 lseps = lsep;
282
283 return 0;
284 }
285
286 /* init function */
bt_avdtp_init(void)287 int bt_avdtp_init(void)
288 {
289 int err;
290 static struct bt_l2cap_server avdtp_l2cap = {
291 .psm = BT_L2CAP_PSM_AVDTP,
292 .sec_level = BT_SECURITY_L2,
293 .accept = bt_avdtp_l2cap_accept,
294 };
295
296 BT_DBG("");
297
298 /* Register AVDTP PSM with L2CAP */
299 err = bt_l2cap_br_server_register(&avdtp_l2cap);
300 if (err < 0) {
301 BT_ERR("AVDTP L2CAP Registration failed %d", err);
302 }
303
304 return err;
305 }
306
307 /* AVDTP Discover Request */
bt_avdtp_discover(struct bt_avdtp * session,struct bt_avdtp_discover_params * param)308 int bt_avdtp_discover(struct bt_avdtp *session,
309 struct bt_avdtp_discover_params *param)
310 {
311 struct net_buf *buf;
312
313 BT_DBG("");
314 if (!param || !session) {
315 BT_DBG("Error: Callback/Session not valid");
316 return -EINVAL;
317 }
318
319 buf = avdtp_create_pdu(BT_AVDTP_CMD,
320 BT_AVDTP_PACKET_TYPE_SINGLE,
321 BT_AVDTP_DISCOVER);
322 if (!buf) {
323 BT_ERR("Error: No Buff available");
324 return -ENOMEM;
325 }
326
327 /* Body of the message */
328
329 return avdtp_send(session, buf, ¶m->req);
330 }
331 #endif
332