1/******************************************************************************
2 *
3 *  Copyright (C) 2011-2012 Broadcom Corporation
4 *
5 *  Licensed under the Apache License, Version 2.0 (the "License");
6 *  you may not use this file except in compliance with the License.
7 *  You may obtain a copy of the License at:
8 *
9 *  http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 *
17 ******************************************************************************/
18
19/******************************************************************************
20 *
21 *  This is the implementation of the API for the advanced audio/video (AV)
22 *  subsystem of BTA, Broadcom's Bluetooth application layer for mobile
23 *  phones.
24 *
25 ******************************************************************************/
26
27#include <base/logging.h>
28
29#include "bt_target.h"
30
31#include <string.h>
32#include "bt_common.h"
33#include "bta_api.h"
34#include "bta_av_api.h"
35#include "bta_av_int.h"
36#include "bta_sys.h"
37
38#include "osi/include/allocator.h"
39
40/*****************************************************************************
41 *  Constants
42 ****************************************************************************/
43
44static const tBTA_SYS_REG bta_av_reg = {bta_av_hdl_event, BTA_AvDisable};
45
46/*******************************************************************************
47 *
48 * Function         BTA_AvEnable
49 *
50 * Description      Enable the advanced audio/video service. When the enable
51 *                  operation is complete the callback function will be
52 *                  called with a BTA_AV_ENABLE_EVT. This function must
53 *                  be called before other function in the AV API are
54 *                  called.
55 *
56 * Returns          void
57 *
58 ******************************************************************************/
59void BTA_AvEnable(tBTA_SEC sec_mask, tBTA_AV_FEAT features,
60                  tBTA_AV_CBACK* p_cback) {
61  tBTA_AV_API_ENABLE* p_buf =
62      (tBTA_AV_API_ENABLE*)osi_malloc(sizeof(tBTA_AV_API_ENABLE));
63
64  /* register with BTA system manager */
65  bta_sys_register(BTA_ID_AV, &bta_av_reg);
66
67  p_buf->hdr.event = BTA_AV_API_ENABLE_EVT;
68  p_buf->p_cback = p_cback;
69  p_buf->features = features;
70  p_buf->sec_mask = sec_mask;
71
72  bta_sys_sendmsg(p_buf);
73}
74
75/*******************************************************************************
76 *
77 * Function         BTA_AvDisable
78 *
79 * Description      Disable the advanced audio/video service.
80 *
81 * Returns          void
82 *
83 ******************************************************************************/
84void BTA_AvDisable(void) {
85  BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
86
87  bta_sys_deregister(BTA_ID_AV);
88  p_buf->event = BTA_AV_API_DISABLE_EVT;
89
90  bta_sys_sendmsg(p_buf);
91}
92
93/*******************************************************************************
94 *
95 * Function         BTA_AvRegister
96 *
97 * Description      Register the audio or video service to stack. When the
98 *                  operation is complete the callback function will be
99 *                  called with a BTA_AV_REGISTER_EVT. This function must
100 *                  be called before AVDT stream is open.
101 *
102 *
103 * Returns          void
104 *
105 ******************************************************************************/
106void BTA_AvRegister(tBTA_AV_CHNL chnl, const char* p_service_name,
107                    uint8_t app_id, tBTA_AV_SINK_DATA_CBACK* p_sink_data_cback,
108                    uint16_t service_uuid) {
109  tBTA_AV_API_REG* p_buf =
110      (tBTA_AV_API_REG*)osi_malloc(sizeof(tBTA_AV_API_REG));
111
112  p_buf->hdr.layer_specific = chnl;
113  p_buf->hdr.event = BTA_AV_API_REGISTER_EVT;
114  if (p_service_name)
115    strlcpy(p_buf->p_service_name, p_service_name, BTA_SERVICE_NAME_LEN);
116  else
117    p_buf->p_service_name[0] = 0;
118  p_buf->app_id = app_id;
119  p_buf->p_app_sink_data_cback = p_sink_data_cback;
120  p_buf->service_uuid = service_uuid;
121
122  bta_sys_sendmsg(p_buf);
123}
124
125/*******************************************************************************
126 *
127 * Function         BTA_AvDeregister
128 *
129 * Description      Deregister the audio or video service
130 *
131 * Returns          void
132 *
133 ******************************************************************************/
134void BTA_AvDeregister(tBTA_AV_HNDL hndl) {
135  BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
136
137  p_buf->layer_specific = hndl;
138  p_buf->event = BTA_AV_API_DEREGISTER_EVT;
139
140  bta_sys_sendmsg(p_buf);
141}
142
143/*******************************************************************************
144 *
145 * Function         BTA_AvOpen
146 *
147 * Description      Opens an advanced audio/video connection to a peer device.
148 *                  When connection is open callback function is called
149 *                  with a BTA_AV_OPEN_EVT.
150 *
151 * Returns          void
152 *
153 ******************************************************************************/
154void BTA_AvOpen(const RawAddress& bd_addr, tBTA_AV_HNDL handle, bool use_rc,
155                tBTA_SEC sec_mask, uint16_t uuid) {
156  tBTA_AV_API_OPEN* p_buf =
157      (tBTA_AV_API_OPEN*)osi_malloc(sizeof(tBTA_AV_API_OPEN));
158
159  p_buf->hdr.event = BTA_AV_API_OPEN_EVT;
160  p_buf->hdr.layer_specific = handle;
161  p_buf->bd_addr = bd_addr;
162  p_buf->use_rc = use_rc;
163  p_buf->sec_mask = sec_mask;
164  p_buf->switch_res = BTA_AV_RS_NONE;
165  p_buf->uuid = uuid;
166
167  bta_sys_sendmsg(p_buf);
168}
169
170/*******************************************************************************
171 *
172 * Function         BTA_AvClose
173 *
174 * Description      Close the current streams.
175 *
176 * Returns          void
177 *
178 ******************************************************************************/
179void BTA_AvClose(tBTA_AV_HNDL handle) {
180  BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
181
182  p_buf->event = BTA_AV_API_CLOSE_EVT;
183  p_buf->layer_specific = handle;
184
185  bta_sys_sendmsg(p_buf);
186}
187
188/*******************************************************************************
189 *
190 * Function         BTA_AvDisconnect
191 *
192 * Description      Close the connection to the address.
193 *
194 * Returns          void
195 *
196 ******************************************************************************/
197void BTA_AvDisconnect(const RawAddress& bd_addr) {
198  tBTA_AV_API_DISCNT* p_buf =
199      (tBTA_AV_API_DISCNT*)osi_malloc(sizeof(tBTA_AV_API_DISCNT));
200
201  p_buf->hdr.event = BTA_AV_API_DISCONNECT_EVT;
202  p_buf->bd_addr = bd_addr;
203
204  bta_sys_sendmsg(p_buf);
205}
206
207/*******************************************************************************
208 *
209 * Function         BTA_AvStart
210 *
211 * Description      Start audio/video stream data transfer.
212 *
213 * Returns          void
214 *
215 ******************************************************************************/
216void BTA_AvStart(void) {
217  BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
218
219  p_buf->event = BTA_AV_API_START_EVT;
220
221  bta_sys_sendmsg(p_buf);
222}
223
224/*******************************************************************************
225 *
226 * Function         BTA_AvOffloadStart
227 *
228 * Description      Start a2dp audio offloading.
229 *
230 * Returns          void
231 *
232 ******************************************************************************/
233void BTA_AvOffloadStart(tBTA_AV_HNDL hndl) {
234  BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
235
236  p_buf->event = BTA_AV_API_OFFLOAD_START_EVT;
237  p_buf->layer_specific = hndl;
238
239  bta_sys_sendmsg(p_buf);
240}
241
242/*******************************************************************************
243 *
244 * Function         BTA_AvOffloadStartRsp
245 *
246 * Description      Response from vendor lib for A2DP Offload Start request.
247 *
248 * Returns          void
249 *
250 ******************************************************************************/
251void BTA_AvOffloadStartRsp(tBTA_AV_HNDL hndl, tBTA_AV_STATUS status) {
252  tBTA_AV_API_STATUS_RSP* p_buf =
253      (tBTA_AV_API_STATUS_RSP*)osi_malloc(sizeof(tBTA_AV_API_STATUS_RSP));
254
255  p_buf->hdr.event = BTA_AV_API_OFFLOAD_START_RSP_EVT;
256  p_buf->hdr.layer_specific = hndl;
257  p_buf->status = status;
258
259  bta_sys_sendmsg(p_buf);
260}
261
262/*******************************************************************************
263 *
264 * Function         BTA_AvStop
265 *
266 * Description      Stop audio/video stream data transfer.
267 *                  If suspend is true, this function sends AVDT suspend signal
268 *                  to the connected peer(s).
269 *
270 * Returns          void
271 *
272 ******************************************************************************/
273void BTA_AvStop(bool suspend) {
274  tBTA_AV_API_STOP* p_buf =
275      (tBTA_AV_API_STOP*)osi_malloc(sizeof(tBTA_AV_API_STOP));
276
277  p_buf->hdr.event = BTA_AV_API_STOP_EVT;
278  p_buf->flush = true;
279  p_buf->suspend = suspend;
280  p_buf->reconfig_stop = false;
281
282  bta_sys_sendmsg(p_buf);
283}
284
285/*******************************************************************************
286 *
287 * Function         BTA_AvReconfig
288 *
289 * Description      Reconfigure the audio/video stream.
290 *                  If suspend is true, this function tries the
291 *                  suspend/reconfigure procedure first.
292 *                  If suspend is false or when suspend/reconfigure fails,
293 *                  this function closes and re-opens the AVDT connection.
294 *
295 * Returns          void
296 *
297 ******************************************************************************/
298void BTA_AvReconfig(tBTA_AV_HNDL hndl, bool suspend, uint8_t sep_info_idx,
299                    uint8_t* p_codec_info, uint8_t num_protect,
300                    const uint8_t* p_protect_info) {
301  tBTA_AV_API_RCFG* p_buf =
302      (tBTA_AV_API_RCFG*)osi_malloc(sizeof(tBTA_AV_API_RCFG) + num_protect);
303
304  p_buf->hdr.layer_specific = hndl;
305  p_buf->hdr.event = BTA_AV_API_RECONFIG_EVT;
306  p_buf->num_protect = num_protect;
307  p_buf->suspend = suspend;
308  p_buf->sep_info_idx = sep_info_idx;
309  p_buf->p_protect_info = (uint8_t*)(p_buf + 1);
310  memcpy(p_buf->codec_info, p_codec_info, AVDT_CODEC_SIZE);
311  memcpy(p_buf->p_protect_info, p_protect_info, num_protect);
312
313  bta_sys_sendmsg(p_buf);
314}
315
316/*******************************************************************************
317 *
318 * Function         BTA_AvProtectReq
319 *
320 * Description      Send a content protection request.  This function can only
321 *                  be used if AV is enabled with feature BTA_AV_FEAT_PROTECT.
322 *
323 * Returns          void
324 *
325 ******************************************************************************/
326void BTA_AvProtectReq(tBTA_AV_HNDL hndl, uint8_t* p_data, uint16_t len) {
327  tBTA_AV_API_PROTECT_REQ* p_buf = (tBTA_AV_API_PROTECT_REQ*)osi_malloc(
328      sizeof(tBTA_AV_API_PROTECT_REQ) + len);
329
330  p_buf->hdr.layer_specific = hndl;
331  p_buf->hdr.event = BTA_AV_API_PROTECT_REQ_EVT;
332  p_buf->len = len;
333  if (p_data == NULL) {
334    p_buf->p_data = NULL;
335  } else {
336    p_buf->p_data = (uint8_t*)(p_buf + 1);
337    memcpy(p_buf->p_data, p_data, len);
338  }
339
340  bta_sys_sendmsg(p_buf);
341}
342
343/*******************************************************************************
344 *
345 * Function         BTA_AvProtectRsp
346 *
347 * Description      Send a content protection response.  This function must
348 *                  be called if a BTA_AV_PROTECT_REQ_EVT is received.
349 *                  This function can only be used if AV is enabled with
350 *                  feature BTA_AV_FEAT_PROTECT.
351 *
352 * Returns          void
353 *
354 ******************************************************************************/
355void BTA_AvProtectRsp(tBTA_AV_HNDL hndl, uint8_t error_code, uint8_t* p_data,
356                      uint16_t len) {
357  tBTA_AV_API_PROTECT_RSP* p_buf = (tBTA_AV_API_PROTECT_RSP*)osi_malloc(
358      sizeof(tBTA_AV_API_PROTECT_RSP) + len);
359
360  p_buf->hdr.layer_specific = hndl;
361  p_buf->hdr.event = BTA_AV_API_PROTECT_RSP_EVT;
362  p_buf->len = len;
363  p_buf->error_code = error_code;
364  if (p_data == NULL) {
365    p_buf->p_data = NULL;
366  } else {
367    p_buf->p_data = (uint8_t*)(p_buf + 1);
368    memcpy(p_buf->p_data, p_data, len);
369  }
370
371  bta_sys_sendmsg(p_buf);
372}
373
374/*******************************************************************************
375 *
376 * Function         BTA_AvRemoteCmd
377 *
378 * Description      Send a remote control command.  This function can only
379 *                  be used if AV is enabled with feature BTA_AV_FEAT_RCCT.
380 *
381 * Returns          void
382 *
383 ******************************************************************************/
384void BTA_AvRemoteCmd(uint8_t rc_handle, uint8_t label, tBTA_AV_RC rc_id,
385                     tBTA_AV_STATE key_state) {
386  tBTA_AV_API_REMOTE_CMD* p_buf =
387      (tBTA_AV_API_REMOTE_CMD*)osi_malloc(sizeof(tBTA_AV_API_REMOTE_CMD));
388
389  p_buf->hdr.event = BTA_AV_API_REMOTE_CMD_EVT;
390  p_buf->hdr.layer_specific = rc_handle;
391  p_buf->msg.op_id = rc_id;
392  p_buf->msg.state = key_state;
393  p_buf->msg.p_pass_data = NULL;
394  p_buf->msg.pass_len = 0;
395  p_buf->label = label;
396
397  bta_sys_sendmsg(p_buf);
398}
399
400/*******************************************************************************
401 *
402 * Function         BTA_AvRemoteVendorUniqueCmd
403 *
404 * Description      Send a remote control command with Vendor Unique rc_id.
405 *                  This function can only be used if AV is enabled with
406 *                  feature BTA_AV_FEAT_RCCT.
407 *
408 * Returns          void
409 *
410 ******************************************************************************/
411void BTA_AvRemoteVendorUniqueCmd(uint8_t rc_handle, uint8_t label,
412                                 tBTA_AV_STATE key_state, uint8_t* p_msg,
413                                 uint8_t buf_len) {
414  tBTA_AV_API_REMOTE_CMD* p_buf = (tBTA_AV_API_REMOTE_CMD*)osi_malloc(
415      sizeof(tBTA_AV_API_REMOTE_CMD) + buf_len);
416
417  p_buf->label = label;
418  p_buf->hdr.event = BTA_AV_API_REMOTE_CMD_EVT;
419  p_buf->hdr.layer_specific = rc_handle;
420  p_buf->msg.op_id = AVRC_ID_VENDOR;
421  p_buf->msg.state = key_state;
422  p_buf->msg.pass_len = buf_len;
423  if (p_msg == NULL) {
424    p_buf->msg.p_pass_data = NULL;
425  } else {
426    p_buf->msg.p_pass_data = (uint8_t*)(p_buf + 1);
427    memcpy(p_buf->msg.p_pass_data, p_msg, buf_len);
428  }
429  bta_sys_sendmsg(p_buf);
430}
431
432/*******************************************************************************
433 *
434 * Function         BTA_AvVendorCmd
435 *
436 * Description      Send a vendor dependent remote control command.  This
437 *                  function can only be used if AV is enabled with feature
438 *                  BTA_AV_FEAT_VENDOR.
439 *
440 * Returns          void
441 *
442 ******************************************************************************/
443void BTA_AvVendorCmd(uint8_t rc_handle, uint8_t label, tBTA_AV_CODE cmd_code,
444                     uint8_t* p_data, uint16_t len) {
445  tBTA_AV_API_VENDOR* p_buf =
446      (tBTA_AV_API_VENDOR*)osi_malloc(sizeof(tBTA_AV_API_VENDOR) + len);
447
448  p_buf->hdr.event = BTA_AV_API_VENDOR_CMD_EVT;
449  p_buf->hdr.layer_specific = rc_handle;
450  p_buf->msg.hdr.ctype = cmd_code;
451  p_buf->msg.hdr.subunit_type = AVRC_SUB_PANEL;
452  p_buf->msg.hdr.subunit_id = 0;
453  p_buf->msg.company_id = p_bta_av_cfg->company_id;
454  p_buf->label = label;
455  p_buf->msg.vendor_len = len;
456  if (p_data == NULL) {
457    p_buf->msg.p_vendor_data = NULL;
458  } else {
459    p_buf->msg.p_vendor_data = (uint8_t*)(p_buf + 1);
460    memcpy(p_buf->msg.p_vendor_data, p_data, len);
461  }
462
463  bta_sys_sendmsg(p_buf);
464}
465
466/*******************************************************************************
467 *
468 * Function         BTA_AvVendorRsp
469 *
470 * Description      Send a vendor dependent remote control response.
471 *                  This function must be called if a BTA_AV_VENDOR_CMD_EVT
472 *                  is received. This function can only be used if AV is
473 *                  enabled with feature BTA_AV_FEAT_VENDOR.
474 *
475 * Returns          void
476 *
477 ******************************************************************************/
478void BTA_AvVendorRsp(uint8_t rc_handle, uint8_t label, tBTA_AV_CODE rsp_code,
479                     uint8_t* p_data, uint16_t len, uint32_t company_id) {
480  tBTA_AV_API_VENDOR* p_buf =
481      (tBTA_AV_API_VENDOR*)osi_malloc(sizeof(tBTA_AV_API_VENDOR) + len);
482
483  p_buf->hdr.event = BTA_AV_API_VENDOR_RSP_EVT;
484  p_buf->hdr.layer_specific = rc_handle;
485  p_buf->msg.hdr.ctype = rsp_code;
486  p_buf->msg.hdr.subunit_type = AVRC_SUB_PANEL;
487  p_buf->msg.hdr.subunit_id = 0;
488  if (company_id)
489    p_buf->msg.company_id = company_id;
490  else
491    p_buf->msg.company_id = p_bta_av_cfg->company_id;
492  p_buf->label = label;
493  p_buf->msg.vendor_len = len;
494  if (p_data == NULL) {
495    p_buf->msg.p_vendor_data = NULL;
496  } else {
497    p_buf->msg.p_vendor_data = (uint8_t*)(p_buf + 1);
498    memcpy(p_buf->msg.p_vendor_data, p_data, len);
499  }
500
501  bta_sys_sendmsg(p_buf);
502}
503
504/*******************************************************************************
505 *
506 * Function         BTA_AvOpenRc
507 *
508 * Description      Open an AVRCP connection toward the device with the
509 *                  specified handle
510 *
511 * Returns          void
512 *
513 ******************************************************************************/
514void BTA_AvOpenRc(tBTA_AV_HNDL handle) {
515  tBTA_AV_API_OPEN_RC* p_buf =
516      (tBTA_AV_API_OPEN_RC*)osi_malloc(sizeof(tBTA_AV_API_OPEN_RC));
517
518  p_buf->hdr.event = BTA_AV_API_RC_OPEN_EVT;
519  p_buf->hdr.layer_specific = handle;
520
521  bta_sys_sendmsg(p_buf);
522}
523
524/*******************************************************************************
525 *
526 * Function         BTA_AvCloseRc
527 *
528 * Description      Close an AVRCP connection
529 *
530 * Returns          void
531 *
532 ******************************************************************************/
533void BTA_AvCloseRc(uint8_t rc_handle) {
534  tBTA_AV_API_CLOSE_RC* p_buf =
535      (tBTA_AV_API_CLOSE_RC*)osi_malloc(sizeof(tBTA_AV_API_CLOSE_RC));
536
537  p_buf->hdr.event = BTA_AV_API_RC_CLOSE_EVT;
538  p_buf->hdr.layer_specific = rc_handle;
539
540  bta_sys_sendmsg(p_buf);
541}
542
543/*******************************************************************************
544 *
545 * Function         BTA_AvMetaRsp
546 *
547 * Description      Send a Metadata/Advanced Control response. The message
548 *                  contained in p_pkt can be composed with AVRC utility
549 *                  functions.
550 *                  This function can only be used if AV is enabled with feature
551 *                  BTA_AV_FEAT_METADATA.
552 *
553 * Returns          void
554 *
555 ******************************************************************************/
556void BTA_AvMetaRsp(uint8_t rc_handle, uint8_t label, tBTA_AV_CODE rsp_code,
557                   BT_HDR* p_pkt) {
558  tBTA_AV_API_META_RSP* p_buf =
559      (tBTA_AV_API_META_RSP*)osi_malloc(sizeof(tBTA_AV_API_META_RSP));
560
561  p_buf->hdr.event = BTA_AV_API_META_RSP_EVT;
562  p_buf->hdr.layer_specific = rc_handle;
563  p_buf->rsp_code = rsp_code;
564  p_buf->p_pkt = p_pkt;
565  p_buf->is_rsp = true;
566  p_buf->label = label;
567
568  bta_sys_sendmsg(p_buf);
569}
570
571/*******************************************************************************
572 *
573 * Function         BTA_AvMetaCmd
574 *
575 * Description      Send a Metadata/Advanced Control command. The message
576*contained
577 *                  in p_pkt can be composed with AVRC utility functions.
578 *                  This function can only be used if AV is enabled with feature
579 *                  BTA_AV_FEAT_METADATA.
580 *                  This message is sent only when the peer supports the TG
581*role.
582*8                  The only command makes sense right now is the absolute
583*volume command.
584 *
585 * Returns          void
586 *
587 ******************************************************************************/
588void BTA_AvMetaCmd(uint8_t rc_handle, uint8_t label, tBTA_AV_CMD cmd_code,
589                   BT_HDR* p_pkt) {
590  tBTA_AV_API_META_RSP* p_buf =
591      (tBTA_AV_API_META_RSP*)osi_malloc(sizeof(tBTA_AV_API_META_RSP));
592
593  p_buf->hdr.event = BTA_AV_API_META_RSP_EVT;
594  p_buf->hdr.layer_specific = rc_handle;
595  p_buf->p_pkt = p_pkt;
596  p_buf->rsp_code = cmd_code;
597  p_buf->is_rsp = false;
598  p_buf->label = label;
599
600  bta_sys_sendmsg(p_buf);
601}
602