1 /******************************************************************************
3 * Copyright 2011-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 /******************************************************************************
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
25 ******************************************************************************/
27 #define LOG_TAG "bt_bta_av"
29 #include "bt_target.h" // Must be first to define build configuration
31 #include "bta/av/bta_av_int.h"
32 #include "osi/include/allocator.h"
33 #include "osi/include/compat.h"
34 #include "osi/include/log.h"
36 /*****************************************************************************
38 ****************************************************************************/
40 static const tBTA_SYS_REG bta_av_reg = {bta_av_hdl_event, BTA_AvDisable};
42 /*******************************************************************************
44 * Function BTA_AvEnable
46 * Description Enable the advanced audio/video service. When the enable
47 * operation is complete the callback function will be
48 * called with a BTA_AV_ENABLE_EVT. This function must
49 * be called before other function in the AV API are
54 ******************************************************************************/
55 void BTA_AvEnable(tBTA_AV_FEAT features, tBTA_AV_CBACK* p_cback) {
56 tBTA_AV_API_ENABLE* p_buf =
57 (tBTA_AV_API_ENABLE*)osi_malloc(sizeof(tBTA_AV_API_ENABLE));
59 /* register with BTA system manager */
60 bta_sys_register(BTA_ID_AV, &bta_av_reg);
62 p_buf->hdr.event = BTA_AV_API_ENABLE_EVT;
63 p_buf->p_cback = p_cback;
64 p_buf->features = features;
66 bta_sys_sendmsg(p_buf);
69 /*******************************************************************************
71 * Function BTA_AvDisable
73 * Description Disable the advanced audio/video service.
77 ******************************************************************************/
78 void BTA_AvDisable(void) {
79 BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
81 bta_sys_deregister(BTA_ID_AV);
82 p_buf->event = BTA_AV_API_DISABLE_EVT;
84 bta_sys_sendmsg(p_buf);
87 /*******************************************************************************
89 * Function BTA_AvRegister
91 * Description Register the audio or video service to stack. When the
92 * operation is complete the callback function will be
93 * called with a BTA_AV_REGISTER_EVT. This function must
94 * be called before AVDT stream is open.
99 ******************************************************************************/
100 void BTA_AvRegister(tBTA_AV_CHNL chnl, const char* p_service_name,
101 uint8_t app_id, tBTA_AV_SINK_DATA_CBACK* p_sink_data_cback,
102 uint16_t service_uuid) {
103 tBTA_AV_API_REG* p_buf =
104 (tBTA_AV_API_REG*)osi_malloc(sizeof(tBTA_AV_API_REG));
106 p_buf->hdr.layer_specific = chnl;
107 p_buf->hdr.event = BTA_AV_API_REGISTER_EVT;
109 strlcpy(p_buf->p_service_name, p_service_name, BTA_SERVICE_NAME_LEN);
111 p_buf->p_service_name[0] = 0;
112 p_buf->app_id = app_id;
113 p_buf->p_app_sink_data_cback = p_sink_data_cback;
114 p_buf->service_uuid = service_uuid;
116 bta_sys_sendmsg(p_buf);
119 /*******************************************************************************
121 * Function BTA_AvDeregister
123 * Description Deregister the audio or video service
127 ******************************************************************************/
128 void BTA_AvDeregister(tBTA_AV_HNDL hndl) {
129 BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
131 p_buf->layer_specific = hndl;
132 p_buf->event = BTA_AV_API_DEREGISTER_EVT;
134 bta_sys_sendmsg(p_buf);
137 /*******************************************************************************
139 * Function BTA_AvOpen
141 * Description Opens an advanced audio/video connection to a peer device.
142 * When connection is open callback function is called
143 * with a BTA_AV_OPEN_EVT.
147 ******************************************************************************/
148 void BTA_AvOpen(const RawAddress& bd_addr, tBTA_AV_HNDL handle, bool use_rc,
150 LOG_INFO("%s: peer %s bta_handle:0x%x use_rc=%s uuid=0x%x", __func__,
151 bd_addr.ToString().c_str(), handle, (use_rc) ? "true" : "false",
154 tBTA_AV_API_OPEN* p_buf =
155 (tBTA_AV_API_OPEN*)osi_malloc(sizeof(tBTA_AV_API_OPEN));
157 p_buf->hdr.event = BTA_AV_API_OPEN_EVT;
158 p_buf->hdr.layer_specific = handle;
159 p_buf->bd_addr = bd_addr;
160 p_buf->use_rc = use_rc;
161 p_buf->switch_res = BTA_AV_RS_NONE;
164 bta_sys_sendmsg(p_buf);
167 /*******************************************************************************
169 * Function BTA_AvClose
171 * Description Close the current streams.
175 ******************************************************************************/
176 void BTA_AvClose(tBTA_AV_HNDL handle) {
177 LOG_INFO("%s: bta_handle:0x%x", __func__, handle);
179 BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
181 p_buf->event = BTA_AV_API_CLOSE_EVT;
182 p_buf->layer_specific = handle;
184 bta_sys_sendmsg(p_buf);
187 /*******************************************************************************
189 * Function BTA_AvDisconnect
191 * Description Close the connection to the address.
195 ******************************************************************************/
196 void BTA_AvDisconnect(const RawAddress& bd_addr) {
197 LOG_INFO("%s: peer %s", __func__, bd_addr.ToString().c_str());
199 tBTA_AV_API_DISCNT* p_buf =
200 (tBTA_AV_API_DISCNT*)osi_malloc(sizeof(tBTA_AV_API_DISCNT));
202 p_buf->hdr.event = BTA_AV_API_DISCONNECT_EVT;
203 p_buf->bd_addr = bd_addr;
205 bta_sys_sendmsg(p_buf);
208 /*******************************************************************************
210 * Function BTA_AvStart
212 * Description Start audio/video stream data transfer.
216 ******************************************************************************/
217 void BTA_AvStart(tBTA_AV_HNDL handle) {
218 LOG_INFO("Starting audio/video stream data transfer bta_handle:%hhu", handle);
220 BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
222 p_buf->event = BTA_AV_API_START_EVT;
223 p_buf->layer_specific = handle;
225 bta_sys_sendmsg(p_buf);
228 /*******************************************************************************
230 * Function BTA_AvOffloadStart
232 * Description Start a2dp audio offloading.
236 ******************************************************************************/
237 void BTA_AvOffloadStart(tBTA_AV_HNDL hndl) {
238 LOG_INFO("%s: bta_handle=0x%x", __func__, hndl);
240 BT_HDR_RIGID* p_buf = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
242 p_buf->event = BTA_AV_API_OFFLOAD_START_EVT;
243 p_buf->layer_specific = hndl;
245 bta_sys_sendmsg(p_buf);
248 /*******************************************************************************
250 * Function BTA_AvOffloadStartRsp
252 * Description Response from vendor lib for A2DP Offload Start request.
256 ******************************************************************************/
257 void BTA_AvOffloadStartRsp(tBTA_AV_HNDL hndl, tBTA_AV_STATUS status) {
258 tBTA_AV_API_STATUS_RSP* p_buf =
259 (tBTA_AV_API_STATUS_RSP*)osi_malloc(sizeof(tBTA_AV_API_STATUS_RSP));
261 p_buf->hdr.event = BTA_AV_API_OFFLOAD_START_RSP_EVT;
262 p_buf->hdr.layer_specific = hndl;
263 p_buf->status = status;
265 bta_sys_sendmsg(p_buf);
268 /*******************************************************************************
270 * Function BTA_AvStop
272 * Description Stop audio/video stream data transfer.
273 * If suspend is true, this function sends AVDT suspend signal
274 * to the connected peer(s).
278 ******************************************************************************/
279 void BTA_AvStop(tBTA_AV_HNDL handle, bool suspend) {
280 LOG_INFO("%s: bta_handle=0x%x suspend=%s", __func__, handle,
281 logbool(suspend).c_str());
283 tBTA_AV_API_STOP* p_buf =
284 (tBTA_AV_API_STOP*)osi_malloc(sizeof(tBTA_AV_API_STOP));
286 p_buf->hdr.event = BTA_AV_API_STOP_EVT;
287 p_buf->hdr.layer_specific = handle;
289 p_buf->suspend = suspend;
290 p_buf->reconfig_stop = false;
292 bta_sys_sendmsg(p_buf);
295 /*******************************************************************************
297 * Function BTA_AvReconfig
299 * Description Reconfigure the audio/video stream.
300 * If suspend is true, this function tries the
301 * suspend/reconfigure procedure first.
302 * If suspend is false or when suspend/reconfigure fails,
303 * this function closes and re-opens the AVDT connection.
307 ******************************************************************************/
308 void BTA_AvReconfig(tBTA_AV_HNDL hndl, bool suspend, uint8_t sep_info_idx,
309 uint8_t* p_codec_info, uint8_t num_protect,
310 const uint8_t* p_protect_info) {
311 LOG_INFO("%s: bta_handle=0x%x suspend=%s sep_info_idx=%d", __func__, hndl,
312 logbool(suspend).c_str(), sep_info_idx);
314 tBTA_AV_API_RCFG* p_buf =
315 (tBTA_AV_API_RCFG*)osi_malloc(sizeof(tBTA_AV_API_RCFG) + num_protect);
317 p_buf->hdr.layer_specific = hndl;
318 p_buf->hdr.event = BTA_AV_API_RECONFIG_EVT;
319 p_buf->num_protect = num_protect;
320 p_buf->suspend = suspend;
321 p_buf->sep_info_idx = sep_info_idx;
322 p_buf->p_protect_info = (uint8_t*)(p_buf + 1);
323 memcpy(p_buf->codec_info, p_codec_info, AVDT_CODEC_SIZE);
324 memcpy(p_buf->p_protect_info, p_protect_info, num_protect);
326 bta_sys_sendmsg(p_buf);
329 /*******************************************************************************
331 * Function BTA_AvProtectReq
333 * Description Send a content protection request. This function can only
334 * be used if AV is enabled with feature BTA_AV_FEAT_PROTECT.
338 ******************************************************************************/
339 void BTA_AvProtectReq(tBTA_AV_HNDL hndl, uint8_t* p_data, uint16_t len) {
340 tBTA_AV_API_PROTECT_REQ* p_buf = (tBTA_AV_API_PROTECT_REQ*)osi_malloc(
341 sizeof(tBTA_AV_API_PROTECT_REQ) + len);
343 p_buf->hdr.layer_specific = hndl;
344 p_buf->hdr.event = BTA_AV_API_PROTECT_REQ_EVT;
346 if (p_data == NULL) {
347 p_buf->p_data = NULL;
349 p_buf->p_data = (uint8_t*)(p_buf + 1);
350 memcpy(p_buf->p_data, p_data, len);
353 bta_sys_sendmsg(p_buf);
356 /*******************************************************************************
358 * Function BTA_AvProtectRsp
360 * Description Send a content protection response. This function must
361 * be called if a BTA_AV_PROTECT_REQ_EVT is received.
362 * This function can only be used if AV is enabled with
363 * feature BTA_AV_FEAT_PROTECT.
367 ******************************************************************************/
368 void BTA_AvProtectRsp(tBTA_AV_HNDL hndl, uint8_t error_code, uint8_t* p_data,
370 tBTA_AV_API_PROTECT_RSP* p_buf = (tBTA_AV_API_PROTECT_RSP*)osi_malloc(
371 sizeof(tBTA_AV_API_PROTECT_RSP) + len);
373 p_buf->hdr.layer_specific = hndl;
374 p_buf->hdr.event = BTA_AV_API_PROTECT_RSP_EVT;
376 p_buf->error_code = error_code;
377 if (p_data == NULL) {
378 p_buf->p_data = NULL;
380 p_buf->p_data = (uint8_t*)(p_buf + 1);
381 memcpy(p_buf->p_data, p_data, len);
384 bta_sys_sendmsg(p_buf);
387 /*******************************************************************************
389 * Function BTA_AvRemoteCmd
391 * Description Send a remote control command. This function can only
392 * be used if AV is enabled with feature BTA_AV_FEAT_RCCT.
396 ******************************************************************************/
397 void BTA_AvRemoteCmd(uint8_t rc_handle, uint8_t label, tBTA_AV_RC rc_id,
398 tBTA_AV_STATE key_state) {
399 tBTA_AV_API_REMOTE_CMD* p_buf =
400 (tBTA_AV_API_REMOTE_CMD*)osi_malloc(sizeof(tBTA_AV_API_REMOTE_CMD));
402 p_buf->hdr.event = BTA_AV_API_REMOTE_CMD_EVT;
403 p_buf->hdr.layer_specific = rc_handle;
404 p_buf->msg.op_id = rc_id;
405 p_buf->msg.state = key_state;
406 p_buf->msg.p_pass_data = NULL;
407 p_buf->msg.pass_len = 0;
408 p_buf->label = label;
410 bta_sys_sendmsg(p_buf);
413 /*******************************************************************************
415 * Function BTA_AvRemoteVendorUniqueCmd
417 * Description Send a remote control command with Vendor Unique rc_id.
418 * This function can only be used if AV is enabled with
419 * feature BTA_AV_FEAT_RCCT.
423 ******************************************************************************/
424 void BTA_AvRemoteVendorUniqueCmd(uint8_t rc_handle, uint8_t label,
425 tBTA_AV_STATE key_state, uint8_t* p_msg,
427 tBTA_AV_API_REMOTE_CMD* p_buf = (tBTA_AV_API_REMOTE_CMD*)osi_malloc(
428 sizeof(tBTA_AV_API_REMOTE_CMD) + buf_len);
430 p_buf->label = label;
431 p_buf->hdr.event = BTA_AV_API_REMOTE_CMD_EVT;
432 p_buf->hdr.layer_specific = rc_handle;
433 p_buf->msg.op_id = AVRC_ID_VENDOR;
434 p_buf->msg.state = key_state;
435 p_buf->msg.pass_len = buf_len;
437 p_buf->msg.p_pass_data = NULL;
439 p_buf->msg.p_pass_data = (uint8_t*)(p_buf + 1);
440 memcpy(p_buf->msg.p_pass_data, p_msg, buf_len);
442 bta_sys_sendmsg(p_buf);
445 /*******************************************************************************
447 * Function BTA_AvVendorCmd
449 * Description Send a vendor dependent remote control command. This
450 * function can only be used if AV is enabled with feature
451 * BTA_AV_FEAT_VENDOR.
455 ******************************************************************************/
456 void BTA_AvVendorCmd(uint8_t rc_handle, uint8_t label, tBTA_AV_CODE cmd_code,
457 uint8_t* p_data, uint16_t len) {
458 tBTA_AV_API_VENDOR* p_buf =
459 (tBTA_AV_API_VENDOR*)osi_malloc(sizeof(tBTA_AV_API_VENDOR) + len);
461 p_buf->hdr.event = BTA_AV_API_VENDOR_CMD_EVT;
462 p_buf->hdr.layer_specific = rc_handle;
463 p_buf->msg.hdr.ctype = cmd_code;
464 p_buf->msg.hdr.subunit_type = AVRC_SUB_PANEL;
465 p_buf->msg.hdr.subunit_id = 0;
466 p_buf->msg.company_id = p_bta_av_cfg->company_id;
467 p_buf->label = label;
468 p_buf->msg.vendor_len = len;
469 if (p_data == NULL) {
470 p_buf->msg.p_vendor_data = NULL;
472 p_buf->msg.p_vendor_data = (uint8_t*)(p_buf + 1);
473 memcpy(p_buf->msg.p_vendor_data, p_data, len);
476 bta_sys_sendmsg(p_buf);
479 /*******************************************************************************
481 * Function BTA_AvVendorRsp
483 * Description Send a vendor dependent remote control response.
484 * This function must be called if a BTA_AV_VENDOR_CMD_EVT
485 * is received. This function can only be used if AV is
486 * enabled with feature BTA_AV_FEAT_VENDOR.
490 ******************************************************************************/
491 void BTA_AvVendorRsp(uint8_t rc_handle, uint8_t label, tBTA_AV_CODE rsp_code,
492 uint8_t* p_data, uint16_t len, uint32_t company_id) {
493 tBTA_AV_API_VENDOR* p_buf =
494 (tBTA_AV_API_VENDOR*)osi_malloc(sizeof(tBTA_AV_API_VENDOR) + len);
496 p_buf->hdr.event = BTA_AV_API_VENDOR_RSP_EVT;
497 p_buf->hdr.layer_specific = rc_handle;
498 p_buf->msg.hdr.ctype = rsp_code;
499 p_buf->msg.hdr.subunit_type = AVRC_SUB_PANEL;
500 p_buf->msg.hdr.subunit_id = 0;
502 p_buf->msg.company_id = company_id;
504 p_buf->msg.company_id = p_bta_av_cfg->company_id;
505 p_buf->label = label;
506 p_buf->msg.vendor_len = len;
507 if (p_data == NULL) {
508 p_buf->msg.p_vendor_data = NULL;
510 p_buf->msg.p_vendor_data = (uint8_t*)(p_buf + 1);
511 memcpy(p_buf->msg.p_vendor_data, p_data, len);
514 bta_sys_sendmsg(p_buf);
517 /*******************************************************************************
519 * Function BTA_AvOpenRc
521 * Description Open an AVRCP connection toward the device with the
526 ******************************************************************************/
527 void BTA_AvOpenRc(tBTA_AV_HNDL handle) {
528 tBTA_AV_API_OPEN_RC* p_buf =
529 (tBTA_AV_API_OPEN_RC*)osi_malloc(sizeof(tBTA_AV_API_OPEN_RC));
531 p_buf->hdr.event = BTA_AV_API_RC_OPEN_EVT;
532 p_buf->hdr.layer_specific = handle;
534 bta_sys_sendmsg(p_buf);
537 /*******************************************************************************
539 * Function BTA_AvCloseRc
541 * Description Close an AVRCP connection
545 ******************************************************************************/
546 void BTA_AvCloseRc(uint8_t rc_handle) {
547 tBTA_AV_API_CLOSE_RC* p_buf =
548 (tBTA_AV_API_CLOSE_RC*)osi_malloc(sizeof(tBTA_AV_API_CLOSE_RC));
550 p_buf->hdr.event = BTA_AV_API_RC_CLOSE_EVT;
551 p_buf->hdr.layer_specific = rc_handle;
553 bta_sys_sendmsg(p_buf);
556 /*******************************************************************************
558 * Function BTA_AvMetaRsp
560 * Description Send a Metadata/Advanced Control response. The message
561 * contained in p_pkt can be composed with AVRC utility
563 * This function can only be used if AV is enabled with feature
564 * BTA_AV_FEAT_METADATA.
568 ******************************************************************************/
569 void BTA_AvMetaRsp(uint8_t rc_handle, uint8_t label, tBTA_AV_CODE rsp_code,
571 tBTA_AV_API_META_RSP* p_buf =
572 (tBTA_AV_API_META_RSP*)osi_malloc(sizeof(tBTA_AV_API_META_RSP));
574 p_buf->hdr.event = BTA_AV_API_META_RSP_EVT;
575 p_buf->hdr.layer_specific = rc_handle;
576 p_buf->rsp_code = rsp_code;
577 p_buf->p_pkt = p_pkt;
578 p_buf->is_rsp = true;
579 p_buf->label = label;
581 bta_sys_sendmsg(p_buf);
584 /*******************************************************************************
586 * Function BTA_AvMetaCmd
588 * Description Send a Metadata/Advanced Control command. The message
590 * in p_pkt can be composed with AVRC utility functions.
591 * This function can only be used if AV is enabled with feature
592 * BTA_AV_FEAT_METADATA.
593 * This message is sent only when the peer supports the TG
595 *8 The only command makes sense right now is the absolute
600 ******************************************************************************/
601 void BTA_AvMetaCmd(uint8_t rc_handle, uint8_t label, tBTA_AV_CMD cmd_code,
603 tBTA_AV_API_META_RSP* p_buf =
604 (tBTA_AV_API_META_RSP*)osi_malloc(sizeof(tBTA_AV_API_META_RSP));
606 p_buf->hdr.event = BTA_AV_API_META_RSP_EVT;
607 p_buf->hdr.layer_specific = rc_handle;
608 p_buf->p_pkt = p_pkt;
609 p_buf->rsp_code = cmd_code;
610 p_buf->is_rsp = false;
611 p_buf->label = label;
613 bta_sys_sendmsg(p_buf);