bta_av_api.cc revision f2af1c42ccb2f642b241c2261b42d0be61d45438
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(BD_ADDR 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  bdcpy(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(BD_ADDR 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  bdcpy(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
281  bta_sys_sendmsg(p_buf);
282}
283
284/*******************************************************************************
285 *
286 * Function         BTA_AvReconfig
287 *
288 * Description      Reconfigure the audio/video stream.
289 *                  If suspend is true, this function tries the
290 *                  suspend/reconfigure procedure first.
291 *                  If suspend is false or when suspend/reconfigure fails,
292 *                  this function closes and re-opens the AVDT connection.
293 *
294 * Returns          void
295 *
296 ******************************************************************************/
297void BTA_AvReconfig(tBTA_AV_HNDL hndl, bool suspend, uint8_t sep_info_idx,
298                    uint8_t* p_codec_info, uint8_t num_protect,
299                    const uint8_t* p_protect_info) {
300  tBTA_AV_API_RCFG* p_buf =
301      (tBTA_AV_API_RCFG*)osi_malloc(sizeof(tBTA_AV_API_RCFG) + num_protect);
302
303  p_buf->hdr.layer_specific = hndl;
304  p_buf->hdr.event = BTA_AV_API_RECONFIG_EVT;
305  p_buf->num_protect = num_protect;
306  p_buf->suspend = suspend;
307  p_buf->sep_info_idx = sep_info_idx;
308  p_buf->p_protect_info = (uint8_t*)(p_buf + 1);
309  memcpy(p_buf->codec_info, p_codec_info, AVDT_CODEC_SIZE);
310  memcpy(p_buf->p_protect_info, p_protect_info, num_protect);
311
312  bta_sys_sendmsg(p_buf);
313}
314
315/*******************************************************************************
316 *
317 * Function         BTA_AvProtectReq
318 *
319 * Description      Send a content protection request.  This function can only
320 *                  be used if AV is enabled with feature BTA_AV_FEAT_PROTECT.
321 *
322 * Returns          void
323 *
324 ******************************************************************************/
325void BTA_AvProtectReq(tBTA_AV_HNDL hndl, uint8_t* p_data, uint16_t len) {
326  tBTA_AV_API_PROTECT_REQ* p_buf = (tBTA_AV_API_PROTECT_REQ*)osi_malloc(
327      sizeof(tBTA_AV_API_PROTECT_REQ) + len);
328
329  p_buf->hdr.layer_specific = hndl;
330  p_buf->hdr.event = BTA_AV_API_PROTECT_REQ_EVT;
331  p_buf->len = len;
332  if (p_data == NULL) {
333    p_buf->p_data = NULL;
334  } else {
335    p_buf->p_data = (uint8_t*)(p_buf + 1);
336    memcpy(p_buf->p_data, p_data, len);
337  }
338
339  bta_sys_sendmsg(p_buf);
340}
341
342/*******************************************************************************
343 *
344 * Function         BTA_AvProtectRsp
345 *
346 * Description      Send a content protection response.  This function must
347 *                  be called if a BTA_AV_PROTECT_REQ_EVT is received.
348 *                  This function can only be used if AV is enabled with
349 *                  feature BTA_AV_FEAT_PROTECT.
350 *
351 * Returns          void
352 *
353 ******************************************************************************/
354void BTA_AvProtectRsp(tBTA_AV_HNDL hndl, uint8_t error_code, uint8_t* p_data,
355                      uint16_t len) {
356  tBTA_AV_API_PROTECT_RSP* p_buf = (tBTA_AV_API_PROTECT_RSP*)osi_malloc(
357      sizeof(tBTA_AV_API_PROTECT_RSP) + len);
358
359  p_buf->hdr.layer_specific = hndl;
360  p_buf->hdr.event = BTA_AV_API_PROTECT_RSP_EVT;
361  p_buf->len = len;
362  p_buf->error_code = error_code;
363  if (p_data == NULL) {
364    p_buf->p_data = NULL;
365  } else {
366    p_buf->p_data = (uint8_t*)(p_buf + 1);
367    memcpy(p_buf->p_data, p_data, len);
368  }
369
370  bta_sys_sendmsg(p_buf);
371}
372
373/*******************************************************************************
374 *
375 * Function         BTA_AvRemoteCmd
376 *
377 * Description      Send a remote control command.  This function can only
378 *                  be used if AV is enabled with feature BTA_AV_FEAT_RCCT.
379 *
380 * Returns          void
381 *
382 ******************************************************************************/
383void BTA_AvRemoteCmd(uint8_t rc_handle, uint8_t label, tBTA_AV_RC rc_id,
384                     tBTA_AV_STATE key_state) {
385  tBTA_AV_API_REMOTE_CMD* p_buf =
386      (tBTA_AV_API_REMOTE_CMD*)osi_malloc(sizeof(tBTA_AV_API_REMOTE_CMD));
387
388  p_buf->hdr.event = BTA_AV_API_REMOTE_CMD_EVT;
389  p_buf->hdr.layer_specific = rc_handle;
390  p_buf->msg.op_id = rc_id;
391  p_buf->msg.state = key_state;
392  p_buf->msg.p_pass_data = NULL;
393  p_buf->msg.pass_len = 0;
394  p_buf->label = label;
395
396  bta_sys_sendmsg(p_buf);
397}
398
399/*******************************************************************************
400 *
401 * Function         BTA_AvRemoteVendorUniqueCmd
402 *
403 * Description      Send a remote control command with Vendor Unique rc_id.
404 *                  This function can only be used if AV is enabled with
405 *                  feature BTA_AV_FEAT_RCCT.
406 *
407 * Returns          void
408 *
409 ******************************************************************************/
410void BTA_AvRemoteVendorUniqueCmd(uint8_t rc_handle, uint8_t label,
411                                 tBTA_AV_STATE key_state, uint8_t* p_msg,
412                                 uint8_t buf_len) {
413  tBTA_AV_API_REMOTE_CMD* p_buf = (tBTA_AV_API_REMOTE_CMD*)osi_malloc(
414      sizeof(tBTA_AV_API_REMOTE_CMD) + buf_len);
415
416  p_buf->label = label;
417  p_buf->hdr.event = BTA_AV_API_REMOTE_CMD_EVT;
418  p_buf->hdr.layer_specific = rc_handle;
419  p_buf->msg.op_id = AVRC_ID_VENDOR;
420  p_buf->msg.state = key_state;
421  p_buf->msg.pass_len = buf_len;
422  if (p_msg == NULL) {
423    p_buf->msg.p_pass_data = NULL;
424  } else {
425    p_buf->msg.p_pass_data = (uint8_t*)(p_buf + 1);
426    memcpy(p_buf->msg.p_pass_data, p_msg, buf_len);
427  }
428  bta_sys_sendmsg(p_buf);
429}
430
431/*******************************************************************************
432 *
433 * Function         BTA_AvVendorCmd
434 *
435 * Description      Send a vendor dependent remote control command.  This
436 *                  function can only be used if AV is enabled with feature
437 *                  BTA_AV_FEAT_VENDOR.
438 *
439 * Returns          void
440 *
441 ******************************************************************************/
442void BTA_AvVendorCmd(uint8_t rc_handle, uint8_t label, tBTA_AV_CODE cmd_code,
443                     uint8_t* p_data, uint16_t len) {
444  tBTA_AV_API_VENDOR* p_buf =
445      (tBTA_AV_API_VENDOR*)osi_malloc(sizeof(tBTA_AV_API_VENDOR) + len);
446
447  p_buf->hdr.event = BTA_AV_API_VENDOR_CMD_EVT;
448  p_buf->hdr.layer_specific = rc_handle;
449  p_buf->msg.hdr.ctype = cmd_code;
450  p_buf->msg.hdr.subunit_type = AVRC_SUB_PANEL;
451  p_buf->msg.hdr.subunit_id = 0;
452  p_buf->msg.company_id = p_bta_av_cfg->company_id;
453  p_buf->label = label;
454  p_buf->msg.vendor_len = len;
455  if (p_data == NULL) {
456    p_buf->msg.p_vendor_data = NULL;
457  } else {
458    p_buf->msg.p_vendor_data = (uint8_t*)(p_buf + 1);
459    memcpy(p_buf->msg.p_vendor_data, p_data, len);
460  }
461
462  bta_sys_sendmsg(p_buf);
463}
464
465/*******************************************************************************
466 *
467 * Function         BTA_AvVendorRsp
468 *
469 * Description      Send a vendor dependent remote control response.
470 *                  This function must be called if a BTA_AV_VENDOR_CMD_EVT
471 *                  is received. This function can only be used if AV is
472 *                  enabled with feature BTA_AV_FEAT_VENDOR.
473 *
474 * Returns          void
475 *
476 ******************************************************************************/
477void BTA_AvVendorRsp(uint8_t rc_handle, uint8_t label, tBTA_AV_CODE rsp_code,
478                     uint8_t* p_data, uint16_t len, uint32_t company_id) {
479  tBTA_AV_API_VENDOR* p_buf =
480      (tBTA_AV_API_VENDOR*)osi_malloc(sizeof(tBTA_AV_API_VENDOR) + len);
481
482  p_buf->hdr.event = BTA_AV_API_VENDOR_RSP_EVT;
483  p_buf->hdr.layer_specific = rc_handle;
484  p_buf->msg.hdr.ctype = rsp_code;
485  p_buf->msg.hdr.subunit_type = AVRC_SUB_PANEL;
486  p_buf->msg.hdr.subunit_id = 0;
487  if (company_id)
488    p_buf->msg.company_id = company_id;
489  else
490    p_buf->msg.company_id = p_bta_av_cfg->company_id;
491  p_buf->label = label;
492  p_buf->msg.vendor_len = len;
493  if (p_data == NULL) {
494    p_buf->msg.p_vendor_data = NULL;
495  } else {
496    p_buf->msg.p_vendor_data = (uint8_t*)(p_buf + 1);
497    memcpy(p_buf->msg.p_vendor_data, p_data, len);
498  }
499
500  bta_sys_sendmsg(p_buf);
501}
502
503/*******************************************************************************
504 *
505 * Function         BTA_AvOpenRc
506 *
507 * Description      Open an AVRCP connection toward the device with the
508 *                  specified handle
509 *
510 * Returns          void
511 *
512 ******************************************************************************/
513void BTA_AvOpenRc(tBTA_AV_HNDL handle) {
514  tBTA_AV_API_OPEN_RC* p_buf =
515      (tBTA_AV_API_OPEN_RC*)osi_malloc(sizeof(tBTA_AV_API_OPEN_RC));
516
517  p_buf->hdr.event = BTA_AV_API_RC_OPEN_EVT;
518  p_buf->hdr.layer_specific = handle;
519
520  bta_sys_sendmsg(p_buf);
521}
522
523/*******************************************************************************
524 *
525 * Function         BTA_AvCloseRc
526 *
527 * Description      Close an AVRCP connection
528 *
529 * Returns          void
530 *
531 ******************************************************************************/
532void BTA_AvCloseRc(uint8_t rc_handle) {
533  tBTA_AV_API_CLOSE_RC* p_buf =
534      (tBTA_AV_API_CLOSE_RC*)osi_malloc(sizeof(tBTA_AV_API_CLOSE_RC));
535
536  p_buf->hdr.event = BTA_AV_API_RC_CLOSE_EVT;
537  p_buf->hdr.layer_specific = rc_handle;
538
539  bta_sys_sendmsg(p_buf);
540}
541
542/*******************************************************************************
543 *
544 * Function         BTA_AvMetaRsp
545 *
546 * Description      Send a Metadata/Advanced Control response. The message
547 *                  contained in p_pkt can be composed with AVRC utility
548 *                  functions.
549 *                  This function can only be used if AV is enabled with feature
550 *                  BTA_AV_FEAT_METADATA.
551 *
552 * Returns          void
553 *
554 ******************************************************************************/
555void BTA_AvMetaRsp(uint8_t rc_handle, uint8_t label, tBTA_AV_CODE rsp_code,
556                   BT_HDR* p_pkt) {
557  tBTA_AV_API_META_RSP* p_buf =
558      (tBTA_AV_API_META_RSP*)osi_malloc(sizeof(tBTA_AV_API_META_RSP));
559
560  p_buf->hdr.event = BTA_AV_API_META_RSP_EVT;
561  p_buf->hdr.layer_specific = rc_handle;
562  p_buf->rsp_code = rsp_code;
563  p_buf->p_pkt = p_pkt;
564  p_buf->is_rsp = true;
565  p_buf->label = label;
566
567  bta_sys_sendmsg(p_buf);
568}
569
570/*******************************************************************************
571 *
572 * Function         BTA_AvMetaCmd
573 *
574 * Description      Send a Metadata/Advanced Control command. The message
575*contained
576 *                  in p_pkt can be composed with AVRC utility functions.
577 *                  This function can only be used if AV is enabled with feature
578 *                  BTA_AV_FEAT_METADATA.
579 *                  This message is sent only when the peer supports the TG
580*role.
581*8                  The only command makes sense right now is the absolute
582*volume command.
583 *
584 * Returns          void
585 *
586 ******************************************************************************/
587void BTA_AvMetaCmd(uint8_t rc_handle, uint8_t label, tBTA_AV_CMD cmd_code,
588                   BT_HDR* p_pkt) {
589  tBTA_AV_API_META_RSP* p_buf =
590      (tBTA_AV_API_META_RSP*)osi_malloc(sizeof(tBTA_AV_API_META_RSP));
591
592  p_buf->hdr.event = BTA_AV_API_META_RSP_EVT;
593  p_buf->hdr.layer_specific = rc_handle;
594  p_buf->p_pkt = p_pkt;
595  p_buf->rsp_code = cmd_code;
596  p_buf->is_rsp = false;
597  p_buf->label = label;
598
599  bta_sys_sendmsg(p_buf);
600}
601