1 /******************************************************************************
3 * Copyright 2004-2016 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 file contains action functions for advanced audio/video main state
24 ******************************************************************************/
26 #define LOG_TAG "bt_bta_av"
30 #include "bt_target.h" // Must be first to define build configuration
32 #include "bta/av/bta_av_int.h"
33 #include "bta/include/bta_ar_api.h"
34 #include "bta/include/utl.h"
35 #include "btif/avrcp/avrcp_service.h"
36 #include "osi/include/allocator.h"
37 #include "osi/include/log.h"
38 #include "osi/include/osi.h" // UNUSED_ATTR
39 #include "osi/include/properties.h"
40 #include "stack/include/acl_api.h"
41 #include "stack/include/l2c_api.h"
42 #include "types/raw_address.h"
44 /*****************************************************************************
46 ****************************************************************************/
47 /* the timeout to wait for open req after setconfig for incoming connections */
48 #ifndef BTA_AV_SIGNALLING_TIMEOUT_MS
49 #define BTA_AV_SIGNALLING_TIMEOUT_MS (8 * 1000) /* 8 seconds */
52 /* Time to wait for signalling from SNK when it is initiated from SNK. */
53 /* If not, we will start signalling from SRC. */
54 #ifndef BTA_AV_ACCEPT_SIGNALLING_TIMEOUT_MS
55 #define BTA_AV_ACCEPT_SIGNALLING_TIMEOUT_MS (2 * 1000) /* 2 seconds */
58 static void bta_av_accept_signalling_timer_cback(void* data);
60 #ifndef AVRC_MIN_META_CMD_LEN
61 #define AVRC_MIN_META_CMD_LEN 20
64 /*******************************************************************************
66 * Function bta_av_get_rcb_by_shdl
68 * Description find the RCB associated with the given SCB handle.
72 ******************************************************************************/
73 tBTA_AV_RCB* bta_av_get_rcb_by_shdl(uint8_t shdl) {
74 tBTA_AV_RCB* p_rcb = NULL;
77 for (i = 0; i < BTA_AV_NUM_RCB; i++) {
78 if (bta_av_cb.rcb[i].shdl == shdl &&
79 bta_av_cb.rcb[i].handle != BTA_AV_RC_HANDLE_NONE) {
80 p_rcb = &bta_av_cb.rcb[i];
86 #define BTA_AV_STS_NO_RSP 0xFF /* a number not used by tAVRC_STS */
88 /*******************************************************************************
90 * Function bta_av_del_rc
92 * Description delete the given AVRC handle.
96 ******************************************************************************/
97 void bta_av_del_rc(tBTA_AV_RCB* p_rcb) {
99 uint8_t rc_handle; /* connected AVRCP handle */
102 if (p_rcb->handle != BTA_AV_RC_HANDLE_NONE) {
104 /* Validate array index*/
105 if ((p_rcb->shdl - 1) < BTA_AV_NUM_STRS) {
106 p_scb = bta_av_cb.p_scb[p_rcb->shdl - 1];
109 APPL_TRACE_DEBUG("%s: shdl:%d, srch:%d rc_handle:%d", __func__,
110 p_rcb->shdl, p_scb->rc_handle, p_rcb->handle);
111 if (p_scb->rc_handle == p_rcb->handle)
112 p_scb->rc_handle = BTA_AV_RC_HANDLE_NONE;
113 /* just in case the RC timer is active
114 if (bta_av_cb.features & BTA_AV_FEAT_RCCT && p_scb->chnl ==
115 BTA_AV_CHNL_AUDIO) */
116 alarm_cancel(p_scb->avrc_ct_timer);
120 APPL_TRACE_EVENT("%s: handle: %d status=0x%x, rc_acp_handle:%d, idx:%d",
121 __func__, p_rcb->handle, p_rcb->status,
122 bta_av_cb.rc_acp_handle, bta_av_cb.rc_acp_idx);
123 rc_handle = p_rcb->handle;
124 if (!(p_rcb->status & BTA_AV_RC_CONN_MASK) ||
125 ((p_rcb->status & BTA_AV_RC_ROLE_MASK) == BTA_AV_RC_ROLE_INT)) {
127 p_rcb->handle = BTA_AV_RC_HANDLE_NONE;
131 /* else ACP && connected. do not clear the handle yet */
132 AVRC_Close(rc_handle);
133 if (rc_handle == bta_av_cb.rc_acp_handle)
134 bta_av_cb.rc_acp_handle = BTA_AV_RC_HANDLE_NONE;
136 "%s: end del_rc handle: %d status=0x%x, rc_acp_handle:%d, lidx:%d",
137 __func__, p_rcb->handle, p_rcb->status, bta_av_cb.rc_acp_handle,
142 /*******************************************************************************
144 * Function bta_av_close_all_rc
146 * Description close the all AVRC handle.
150 ******************************************************************************/
151 static void bta_av_close_all_rc(tBTA_AV_CB* p_cb) {
154 for (i = 0; i < BTA_AV_NUM_RCB; i++) {
155 if ((p_cb->disabling) || (bta_av_cb.rcb[i].shdl != 0))
156 bta_av_del_rc(&bta_av_cb.rcb[i]);
160 /*******************************************************************************
162 * Function bta_av_del_sdp_rec
164 * Description delete the given SDP record handle.
168 ******************************************************************************/
169 static void bta_av_del_sdp_rec(uint32_t* p_sdp_handle) {
170 if (*p_sdp_handle != 0) {
171 SDP_DeleteRecord(*p_sdp_handle);
176 /*******************************************************************************
178 * Function bta_av_avrc_sdp_cback
180 * Description AVRCP service discovery callback.
184 ******************************************************************************/
185 static void bta_av_avrc_sdp_cback(UNUSED_ATTR uint16_t status) {
186 BT_HDR_RIGID* p_msg = (BT_HDR_RIGID*)osi_malloc(sizeof(BT_HDR_RIGID));
188 p_msg->event = BTA_AV_SDP_AVRC_DISC_EVT;
190 bta_sys_sendmsg(p_msg);
193 /*******************************************************************************
195 * Function bta_av_rc_ctrl_cback
197 * Description AVRCP control callback.
201 ******************************************************************************/
202 static void bta_av_rc_ctrl_cback(uint8_t handle, uint8_t event,
203 UNUSED_ATTR uint16_t result,
204 const RawAddress* peer_addr) {
205 uint16_t msg_event = 0;
207 APPL_TRACE_EVENT("%s: handle: %d event=0x%x", __func__, handle, event);
208 if (event == AVRC_OPEN_IND_EVT) {
209 /* save handle of opened connection
210 bta_av_cb.rc_handle = handle;*/
212 msg_event = BTA_AV_AVRC_OPEN_EVT;
213 } else if (event == AVRC_CLOSE_IND_EVT) {
214 msg_event = BTA_AV_AVRC_CLOSE_EVT;
215 } else if (event == AVRC_BROWSE_OPEN_IND_EVT) {
216 msg_event = BTA_AV_AVRC_BROWSE_OPEN_EVT;
217 } else if (event == AVRC_BROWSE_CLOSE_IND_EVT) {
218 msg_event = BTA_AV_AVRC_BROWSE_CLOSE_EVT;
222 tBTA_AV_RC_CONN_CHG* p_msg =
223 (tBTA_AV_RC_CONN_CHG*)osi_malloc(sizeof(tBTA_AV_RC_CONN_CHG));
224 p_msg->hdr.event = msg_event;
225 p_msg->handle = handle;
226 if (peer_addr) p_msg->peer_addr = *peer_addr;
227 bta_sys_sendmsg(p_msg);
231 /*******************************************************************************
233 * Function bta_av_rc_msg_cback
235 * Description AVRCP message callback.
239 ******************************************************************************/
240 static void bta_av_rc_msg_cback(uint8_t handle, uint8_t label, uint8_t opcode,
242 uint8_t* p_data_src = NULL;
243 uint16_t data_len = 0;
245 APPL_TRACE_DEBUG("%s: handle: %u opcode=0x%x", __func__, handle, opcode);
247 /* Copy avrc packet into BTA message buffer (for sending to BTA state machine)
250 /* Get size of payload data (for vendor and passthrough messages only; for
252 * messages, use zero-copy) */
253 if (opcode == AVRC_OP_VENDOR && p_msg->vendor.p_vendor_data != NULL) {
254 p_data_src = p_msg->vendor.p_vendor_data;
255 data_len = (uint16_t)p_msg->vendor.vendor_len;
256 } else if (opcode == AVRC_OP_PASS_THRU && p_msg->pass.p_pass_data != NULL) {
257 p_data_src = p_msg->pass.p_pass_data;
258 data_len = (uint16_t)p_msg->pass.pass_len;
261 /* Create a copy of the message */
262 tBTA_AV_RC_MSG* p_buf =
263 (tBTA_AV_RC_MSG*)osi_malloc(sizeof(tBTA_AV_RC_MSG) + data_len);
265 p_buf->hdr.event = BTA_AV_AVRC_MSG_EVT;
266 p_buf->handle = handle;
267 p_buf->label = label;
268 p_buf->opcode = opcode;
269 memcpy(&p_buf->msg, p_msg, sizeof(tAVRC_MSG));
270 /* Copy the data payload, and set the pointer to it */
271 if (p_data_src != NULL) {
272 uint8_t* p_data_dst = (uint8_t*)(p_buf + 1);
273 memcpy(p_data_dst, p_data_src, data_len);
275 /* Update bta message buffer to point to payload data */
276 /* (Note AVRC_OP_BROWSING uses zero-copy: p_buf->msg.browse.p_browse_data
277 * already points to original avrc buffer) */
278 if (opcode == AVRC_OP_VENDOR)
279 p_buf->msg.vendor.p_vendor_data = p_data_dst;
280 else if (opcode == AVRC_OP_PASS_THRU)
281 p_buf->msg.pass.p_pass_data = p_data_dst;
284 if (opcode == AVRC_OP_BROWSE) {
285 /* set p_pkt to NULL, so avrc would not free the buffer */
286 p_msg->browse.p_browse_pkt = NULL;
289 bta_sys_sendmsg(p_buf);
292 /*******************************************************************************
294 * Function bta_av_rc_create
296 * Description alloc RCB and call AVRC_Open
298 * Returns the created rc handle
300 ******************************************************************************/
301 uint8_t bta_av_rc_create(tBTA_AV_CB* p_cb, uint8_t role, uint8_t shdl,
303 if (is_new_avrcp_enabled()) {
304 LOG_INFO("Skipping RC creation for the old AVRCP profile");
305 return BTA_AV_RC_HANDLE_NONE;
309 RawAddress bda = RawAddress::kAny;
310 uint8_t status = BTA_AV_RC_ROLE_ACP;
315 if (role == AVCT_INT) {
316 // Can't grab a stream control block that doesn't have a valid handle
319 "%s: Can't grab stream control block for shdl = %d -> index = %d",
320 __func__, shdl, shdl - 1);
321 return BTA_AV_RC_HANDLE_NONE;
323 tBTA_AV_SCB* p_scb = p_cb->p_scb[shdl - 1];
324 bda = p_scb->PeerAddress();
325 status = BTA_AV_RC_ROLE_INT;
327 p_rcb = bta_av_get_rcb_by_shdl(shdl);
329 APPL_TRACE_ERROR("%s: ACP handle exist for shdl:%d", __func__, shdl);
331 return p_rcb->handle;
335 ccb.ctrl_cback = base::Bind(bta_av_rc_ctrl_cback);
336 ccb.msg_cback = base::Bind(bta_av_rc_msg_cback);
337 ccb.company_id = p_bta_av_cfg->company_id;
339 /* note: BTA_AV_FEAT_RCTG = AVRC_CT_TARGET, BTA_AV_FEAT_RCCT = AVRC_CT_CONTROL
341 ccb.control = p_cb->features & (BTA_AV_FEAT_RCTG | BTA_AV_FEAT_RCCT |
342 BTA_AV_FEAT_METADATA | AVRC_CT_PASSIVE);
344 if (AVRC_Open(&rc_handle, &ccb, bda) != AVRC_SUCCESS)
345 return BTA_AV_RC_HANDLE_NONE;
348 p_rcb = &p_cb->rcb[i];
350 if (p_rcb->handle != BTA_AV_RC_HANDLE_NONE) {
351 APPL_TRACE_ERROR("%s: found duplicated handle:%d", __func__, rc_handle);
354 p_rcb->handle = rc_handle;
355 p_rcb->status = status;
358 p_rcb->peer_features = 0;
359 p_rcb->cover_art_psm = 0;
360 if (lidx == (BTA_AV_NUM_LINKS + 1)) {
361 /* this LIDX is reserved for the AVRCP ACP connection */
362 p_cb->rc_acp_handle = p_rcb->handle;
363 p_cb->rc_acp_idx = (i + 1);
364 APPL_TRACE_DEBUG("%s: rc_acp_handle:%d idx:%d", __func__,
365 p_cb->rc_acp_handle, p_cb->rc_acp_idx);
368 "%s: create %d, role: %d, shdl:%d, rc_handle:%d, lidx:%d, status:0x%x",
369 __func__, i, role, shdl, p_rcb->handle, lidx, p_rcb->status);
374 /*******************************************************************************
376 * Function bta_av_valid_group_navi_msg
378 * Description Check if it is Group Navigation Msg for Metadata
380 * Returns AVRC_RSP_ACCEPT or AVRC_RSP_NOT_IMPL
382 ******************************************************************************/
383 static tBTA_AV_CODE bta_av_group_navi_supported(uint8_t len, uint8_t* p_data,
385 tBTA_AV_CODE ret = AVRC_RSP_NOT_IMPL;
386 uint8_t* p_ptr = p_data;
390 if (p_bta_av_cfg->avrc_group && len == BTA_GROUP_NAVI_MSG_OP_DATA_LEN) {
391 BTA_AV_BE_STREAM_TO_CO_ID(u32, p_ptr);
392 BE_STREAM_TO_UINT16(u16, p_ptr);
394 if (u32 == AVRC_CO_METADATA) {
396 if (u16 <= AVRC_PDU_PREV_GROUP) ret = AVRC_RSP_IMPL_STBL;
398 if (u16 <= AVRC_PDU_PREV_GROUP)
399 ret = AVRC_RSP_ACCEPT;
409 /*******************************************************************************
411 * Function bta_av_op_supported
413 * Description Check if remote control operation is supported.
415 * Returns AVRC_RSP_ACCEPT of supported, AVRC_RSP_NOT_IMPL if not.
417 ******************************************************************************/
418 static tBTA_AV_CODE bta_av_op_supported(tBTA_AV_RC rc_id, bool is_inquiry) {
419 tBTA_AV_CODE ret_code = AVRC_RSP_NOT_IMPL;
421 if (p_bta_av_rc_id) {
423 if (p_bta_av_rc_id[rc_id >> 4] & (1 << (rc_id & 0x0F))) {
424 ret_code = AVRC_RSP_IMPL_STBL;
427 if (p_bta_av_rc_id[rc_id >> 4] & (1 << (rc_id & 0x0F))) {
428 ret_code = AVRC_RSP_ACCEPT;
429 } else if ((p_bta_av_cfg->rc_pass_rsp == AVRC_RSP_INTERIM) &&
431 if (p_bta_av_rc_id_ac[rc_id >> 4] & (1 << (rc_id & 0x0F))) {
432 ret_code = AVRC_RSP_INTERIM;
440 /*******************************************************************************
442 * Function bta_av_find_lcb
444 * Description Given BD_addr, find the associated LCB.
446 * Returns NULL, if not found.
448 ******************************************************************************/
449 tBTA_AV_LCB* bta_av_find_lcb(const RawAddress& addr, uint8_t op) {
450 tBTA_AV_CB* p_cb = &bta_av_cb;
453 tBTA_AV_LCB* p_lcb = NULL;
455 APPL_TRACE_DEBUG("%s: address: %s op:%d", __func__, addr.ToString().c_str(),
457 for (xx = 0; xx < BTA_AV_NUM_LINKS; xx++) {
458 mask = 1 << xx; /* the used mask for this lcb */
459 if ((mask & p_cb->conn_lcb) && p_cb->lcb[xx].addr == addr) {
460 p_lcb = &p_cb->lcb[xx];
461 if (op == BTA_AV_LCB_FREE) {
462 p_cb->conn_lcb &= ~mask; /* clear the connect mask */
463 APPL_TRACE_DEBUG("%s: conn_lcb: 0x%x", __func__, p_cb->conn_lcb);
471 /*******************************************************************************
473 * Function bta_av_rc_opened
475 * Description Set AVRCP state to opened.
479 ******************************************************************************/
480 void bta_av_rc_opened(tBTA_AV_CB* p_cb, tBTA_AV_DATA* p_data) {
481 tBTA_AV_RC_OPEN rc_open;
490 /* find the SCB & stop the timer */
491 for (i = 0; i < BTA_AV_NUM_STRS; i++) {
492 p_scb = p_cb->p_scb[i];
493 if (p_scb && p_scb->PeerAddress() == p_data->rc_conn_chg.peer_addr) {
494 p_scb->rc_handle = p_data->rc_conn_chg.handle;
495 APPL_TRACE_DEBUG("%s: shdl:%d, srch %d", __func__, i + 1,
498 LOG_INFO("%s: allow incoming AVRCP connections:%d", __func__,
500 alarm_cancel(p_scb->avrc_ct_timer);
506 i = p_data->rc_conn_chg.handle;
507 if (p_cb->rcb[i].handle == BTA_AV_RC_HANDLE_NONE) {
508 APPL_TRACE_ERROR("%s: not a valid handle:%d any more", __func__, i);
512 APPL_TRACE_DEBUG("%s: local features %d peer features %d", __func__,
513 p_cb->features, p_cb->rcb[i].peer_features);
515 /* listen to browsing channel when the connection is open,
516 * if peer initiated AVRCP connection and local device supports browsing
518 AVRC_OpenBrowse(p_data->rc_conn_chg.handle, AVCT_ACP);
520 if (p_cb->rcb[i].lidx == (BTA_AV_NUM_LINKS + 1) && shdl != 0) {
521 /* rc is opened on the RC only ACP channel, but is for a specific
522 * SCB -> need to switch RCBs */
523 p_rcb = bta_av_get_rcb_by_shdl(shdl);
525 p_rcb->shdl = p_cb->rcb[i].shdl;
527 p_rcb->lidx = p_cb->rcb[i].lidx;
528 p_cb->rcb[i].lidx = tmp;
529 p_cb->rc_acp_handle = p_rcb->handle;
530 p_cb->rc_acp_idx = (p_rcb - p_cb->rcb) + 1;
531 APPL_TRACE_DEBUG("%s: switching RCB rc_acp_handle:%d idx:%d", __func__,
532 p_cb->rc_acp_handle, p_cb->rc_acp_idx);
536 p_cb->rcb[i].shdl = shdl;
537 rc_open.rc_handle = i;
538 APPL_TRACE_ERROR("%s: rcb[%d] shdl:%d lidx:%d/%d", __func__, i, shdl,
539 p_cb->rcb[i].lidx, p_cb->lcb[BTA_AV_NUM_LINKS].lidx);
540 p_cb->rcb[i].status |= BTA_AV_RC_CONN_MASK;
542 if (!shdl && 0 == p_cb->lcb[BTA_AV_NUM_LINKS].lidx) {
543 /* no associated SCB -> connected to an RC only device
544 * update the index to the extra LCB */
545 p_lcb = &p_cb->lcb[BTA_AV_NUM_LINKS];
546 p_lcb->addr = p_data->rc_conn_chg.peer_addr;
547 p_lcb->lidx = BTA_AV_NUM_LINKS + 1;
548 p_cb->rcb[i].lidx = p_lcb->lidx;
550 APPL_TRACE_ERROR("%s: bd_addr: %s rcb[%d].lidx=%d, lcb.conn_msk=x%x",
551 __func__, p_lcb->addr.ToString().c_str(), i,
552 p_cb->rcb[i].lidx, p_lcb->conn_msk);
553 disc = p_data->rc_conn_chg.handle | BTA_AV_CHNL_MSK;
556 rc_open.peer_addr = p_data->rc_conn_chg.peer_addr;
557 rc_open.peer_features = p_cb->rcb[i].peer_features;
558 rc_open.cover_art_psm = p_cb->rcb[i].cover_art_psm;
559 rc_open.status = BTA_AV_SUCCESS;
560 APPL_TRACE_DEBUG("%s: local features:x%x peer_features:x%x", __func__,
561 p_cb->features, rc_open.peer_features);
562 APPL_TRACE_DEBUG("%s: cover art psm:x%x", __func__, rc_open.cover_art_psm);
563 if (rc_open.peer_features == 0) {
564 /* we have not done SDP on peer RC capabilities.
565 * peer must have initiated the RC connection */
566 if (p_cb->features & BTA_AV_FEAT_RCCT)
567 rc_open.peer_features |= BTA_AV_FEAT_RCTG;
568 if (p_cb->features & BTA_AV_FEAT_RCTG)
569 rc_open.peer_features |= BTA_AV_FEAT_RCCT;
571 bta_av_rc_disc(disc);
574 bta_av_data.rc_open = rc_open;
575 (*p_cb->p_cback)(BTA_AV_RC_OPEN_EVT, &bta_av_data);
577 /* if local initiated AVRCP connection and both peer and locals device support
578 * browsing channel, open the browsing channel now
579 * TODO (sanketa): Some TG would not broadcast browse feature hence check
581 if ((p_cb->features & BTA_AV_FEAT_BROWSE) &&
582 (rc_open.peer_features & BTA_AV_FEAT_BROWSE) &&
583 ((p_cb->rcb[i].status & BTA_AV_RC_ROLE_MASK) == BTA_AV_RC_ROLE_INT)) {
584 APPL_TRACE_DEBUG("%s: opening AVRC Browse channel", __func__);
585 AVRC_OpenBrowse(p_data->rc_conn_chg.handle, AVCT_INT);
589 /*******************************************************************************
591 * Function bta_av_rc_remote_cmd
593 * Description Send an AVRCP remote control command.
597 ******************************************************************************/
598 void bta_av_rc_remote_cmd(tBTA_AV_CB* p_cb, tBTA_AV_DATA* p_data) {
600 if (p_cb->features & BTA_AV_FEAT_RCCT) {
601 if (p_data->hdr.layer_specific < BTA_AV_NUM_RCB) {
602 p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
603 if (p_rcb->status & BTA_AV_RC_CONN_MASK) {
604 AVRC_PassCmd(p_rcb->handle, p_data->api_remote_cmd.label,
605 &p_data->api_remote_cmd.msg);
611 /*******************************************************************************
613 * Function bta_av_rc_vendor_cmd
615 * Description Send an AVRCP vendor specific command.
619 ******************************************************************************/
620 void bta_av_rc_vendor_cmd(tBTA_AV_CB* p_cb, tBTA_AV_DATA* p_data) {
622 if ((p_cb->features & (BTA_AV_FEAT_RCCT | BTA_AV_FEAT_VENDOR)) ==
623 (BTA_AV_FEAT_RCCT | BTA_AV_FEAT_VENDOR)) {
624 if (p_data->hdr.layer_specific < BTA_AV_NUM_RCB) {
625 p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
626 AVRC_VendorCmd(p_rcb->handle, p_data->api_vendor.label,
627 &p_data->api_vendor.msg);
632 /*******************************************************************************
634 * Function bta_av_rc_vendor_rsp
636 * Description Send an AVRCP vendor specific response.
640 ******************************************************************************/
641 void bta_av_rc_vendor_rsp(tBTA_AV_CB* p_cb, tBTA_AV_DATA* p_data) {
643 if ((p_cb->features & (BTA_AV_FEAT_RCTG | BTA_AV_FEAT_VENDOR)) ==
644 (BTA_AV_FEAT_RCTG | BTA_AV_FEAT_VENDOR)) {
645 if (p_data->hdr.layer_specific < BTA_AV_NUM_RCB) {
646 p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
647 AVRC_VendorRsp(p_rcb->handle, p_data->api_vendor.label,
648 &p_data->api_vendor.msg);
653 /*******************************************************************************
655 * Function bta_av_rc_meta_rsp
657 * Description Send an AVRCP metadata/advanced control command/response.
661 ******************************************************************************/
662 void bta_av_rc_meta_rsp(tBTA_AV_CB* p_cb, tBTA_AV_DATA* p_data) {
666 if ((p_cb->features & BTA_AV_FEAT_METADATA) &&
667 (p_data->hdr.layer_specific < BTA_AV_NUM_RCB)) {
668 if ((p_data->api_meta_rsp.is_rsp && (p_cb->features & BTA_AV_FEAT_RCTG)) ||
669 (!p_data->api_meta_rsp.is_rsp && (p_cb->features & BTA_AV_FEAT_RCCT))) {
670 p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
671 if (p_rcb->handle != BTA_AV_RC_HANDLE_NONE) {
672 AVRC_MsgReq(p_rcb->handle, p_data->api_meta_rsp.label,
673 p_data->api_meta_rsp.rsp_code, p_data->api_meta_rsp.p_pkt);
679 if (do_free) osi_free_and_reset((void**)&p_data->api_meta_rsp.p_pkt);
682 /*******************************************************************************
684 * Function bta_av_rc_free_rsp
686 * Description free an AVRCP metadata command buffer.
690 ******************************************************************************/
691 void bta_av_rc_free_rsp(UNUSED_ATTR tBTA_AV_CB* p_cb, tBTA_AV_DATA* p_data) {
692 osi_free_and_reset((void**)&p_data->api_meta_rsp.p_pkt);
695 /*******************************************************************************
697 * Function bta_av_rc_free_browse_msg
699 * Description free an AVRCP browse message buffer.
703 ******************************************************************************/
704 void bta_av_rc_free_browse_msg(UNUSED_ATTR tBTA_AV_CB* p_cb,
705 tBTA_AV_DATA* p_data) {
706 if (p_data->rc_msg.opcode == AVRC_OP_BROWSE) {
707 osi_free_and_reset((void**)&p_data->rc_msg.msg.browse.p_browse_pkt);
711 /*******************************************************************************
713 * Function bta_av_chk_notif_evt_id
715 * Description make sure the requested player id is valid.
717 * Returns BTA_AV_STS_NO_RSP, if no error
719 ******************************************************************************/
720 static tAVRC_STS bta_av_chk_notif_evt_id(tAVRC_MSG_VENDOR* p_vendor) {
721 tAVRC_STS status = BTA_AV_STS_NO_RSP;
724 uint8_t* p = p_vendor->p_vendor_data + 2;
726 BE_STREAM_TO_UINT16(u16, p);
727 /* double check the fixed length */
728 if ((u16 != 5) || (p_vendor->vendor_len != 9)) {
729 status = AVRC_STS_INTERNAL_ERR;
731 /* make sure the player_id is valid */
732 for (xx = 0; xx < p_bta_av_cfg->num_evt_ids; xx++) {
733 if (*p == p_bta_av_cfg->p_meta_evt_ids[xx]) {
737 if (xx == p_bta_av_cfg->num_evt_ids) {
738 status = AVRC_STS_BAD_PARAM;
745 /*******************************************************************************
747 * Function bta_av_proc_meta_cmd
749 * Description Process an AVRCP metadata command from the peer.
751 * Returns true to respond immediately
753 ******************************************************************************/
754 tBTA_AV_EVT bta_av_proc_meta_cmd(tAVRC_RESPONSE* p_rc_rsp,
755 tBTA_AV_RC_MSG* p_msg, uint8_t* p_ctype) {
756 tBTA_AV_EVT evt = BTA_AV_META_MSG_EVT;
759 tAVRC_MSG_VENDOR* p_vendor = &p_msg->msg.vendor;
761 pdu = *(p_vendor->p_vendor_data);
763 *p_ctype = AVRC_RSP_REJ;
765 /* Check to ansure a valid minimum meta data length */
766 if ((AVRC_MIN_META_CMD_LEN + p_vendor->vendor_len) > AVRC_META_CMD_BUF_SIZE) {
768 p_rc_rsp->rsp.status = AVRC_STS_BAD_PARAM;
769 APPL_TRACE_ERROR("%s: Invalid meta-command length: %d", __func__,
770 p_vendor->vendor_len);
774 /* Metadata messages only use PANEL sub-unit type */
775 if (p_vendor->hdr.subunit_type != AVRC_SUB_PANEL) {
776 APPL_TRACE_DEBUG("%s: SUBUNIT must be PANEL", __func__);
779 p_vendor->hdr.ctype = AVRC_RSP_NOT_IMPL;
780 p_vendor->vendor_len = 0;
781 p_rc_rsp->rsp.status = AVRC_STS_BAD_PARAM;
782 } else if (!AVRC_IsValidAvcType(pdu, p_vendor->hdr.ctype)) {
783 APPL_TRACE_DEBUG("%s: Invalid pdu/ctype: 0x%x, %d", __func__, pdu,
784 p_vendor->hdr.ctype);
785 /* reject invalid message without reporting to app */
787 p_rc_rsp->rsp.status = AVRC_STS_BAD_CMD;
790 case AVRC_PDU_GET_CAPABILITIES:
791 /* process GetCapabilities command without reporting the event to app */
793 if (p_vendor->vendor_len != 5) {
794 android_errorWriteLog(0x534e4554, "111893951");
795 p_rc_rsp->get_caps.status = AVRC_STS_INTERNAL_ERR;
798 u8 = *(p_vendor->p_vendor_data + 4);
799 p = p_vendor->p_vendor_data + 2;
800 p_rc_rsp->get_caps.capability_id = u8;
801 BE_STREAM_TO_UINT16(u16, p);
803 p_rc_rsp->get_caps.status = AVRC_STS_INTERNAL_ERR;
805 p_rc_rsp->get_caps.status = AVRC_STS_NO_ERROR;
806 if (u8 == AVRC_CAP_COMPANY_ID) {
807 *p_ctype = AVRC_RSP_IMPL_STBL;
808 p_rc_rsp->get_caps.count = p_bta_av_cfg->num_co_ids;
809 memcpy(p_rc_rsp->get_caps.param.company_id,
810 p_bta_av_cfg->p_meta_co_ids,
811 (p_bta_av_cfg->num_co_ids << 2));
812 } else if (u8 == AVRC_CAP_EVENTS_SUPPORTED) {
813 *p_ctype = AVRC_RSP_IMPL_STBL;
814 p_rc_rsp->get_caps.count = p_bta_av_cfg->num_evt_ids;
815 memcpy(p_rc_rsp->get_caps.param.event_id,
816 p_bta_av_cfg->p_meta_evt_ids, p_bta_av_cfg->num_evt_ids);
818 APPL_TRACE_DEBUG("%s: Invalid capability ID: 0x%x", __func__, u8);
819 /* reject - unknown capability ID */
820 p_rc_rsp->get_caps.status = AVRC_STS_BAD_PARAM;
825 case AVRC_PDU_REGISTER_NOTIFICATION:
826 /* make sure the event_id is implemented */
827 p_rc_rsp->rsp.status = bta_av_chk_notif_evt_id(p_vendor);
828 if (p_rc_rsp->rsp.status != BTA_AV_STS_NO_RSP) evt = 0;
836 /*******************************************************************************
838 * Function bta_av_rc_msg
840 * Description Process an AVRCP message from the peer.
844 ******************************************************************************/
845 void bta_av_rc_msg(tBTA_AV_CB* p_cb, tBTA_AV_DATA* p_data) {
848 BT_HDR* p_pkt = NULL;
849 tAVRC_MSG_VENDOR* p_vendor = &p_data->rc_msg.msg.vendor;
850 bool is_inquiry = ((p_data->rc_msg.msg.hdr.ctype == AVRC_CMD_SPEC_INQ) ||
851 p_data->rc_msg.msg.hdr.ctype == AVRC_CMD_GEN_INQ);
853 tAVRC_RESPONSE rc_rsp;
855 rc_rsp.rsp.status = BTA_AV_STS_NO_RSP;
857 if (NULL == p_data) {
858 APPL_TRACE_ERROR("%s: Message from peer with no data", __func__);
862 APPL_TRACE_DEBUG("%s: opcode=%x, ctype=%x", __func__, p_data->rc_msg.opcode,
863 p_data->rc_msg.msg.hdr.ctype);
865 if (p_data->rc_msg.opcode == AVRC_OP_PASS_THRU) {
866 /* if this is a pass thru command */
867 if ((p_data->rc_msg.msg.hdr.ctype == AVRC_CMD_CTRL) ||
868 (p_data->rc_msg.msg.hdr.ctype == AVRC_CMD_SPEC_INQ) ||
869 (p_data->rc_msg.msg.hdr.ctype == AVRC_CMD_GEN_INQ)) {
870 /* check if operation is supported */
871 char avrcp_ct_support[PROPERTY_VALUE_MAX];
872 osi_property_get("bluetooth.pts.avrcp_ct.support", avrcp_ct_support,
874 if (p_data->rc_msg.msg.pass.op_id == AVRC_ID_VENDOR) {
875 p_data->rc_msg.msg.hdr.ctype = AVRC_RSP_NOT_IMPL;
876 if (p_cb->features & BTA_AV_FEAT_METADATA)
877 p_data->rc_msg.msg.hdr.ctype = bta_av_group_navi_supported(
878 p_data->rc_msg.msg.pass.pass_len,
879 p_data->rc_msg.msg.pass.p_pass_data, is_inquiry);
880 } else if (((p_data->rc_msg.msg.pass.op_id == AVRC_ID_VOL_UP) ||
881 (p_data->rc_msg.msg.pass.op_id == AVRC_ID_VOL_DOWN)) &&
882 !strcmp(avrcp_ct_support, "true")) {
883 p_data->rc_msg.msg.hdr.ctype = AVRC_RSP_ACCEPT;
885 p_data->rc_msg.msg.hdr.ctype =
886 bta_av_op_supported(p_data->rc_msg.msg.pass.op_id, is_inquiry);
889 APPL_TRACE_DEBUG("%s: ctype %d", __func__, p_data->rc_msg.msg.hdr.ctype)
892 if (p_data->rc_msg.msg.hdr.ctype != AVRC_RSP_INTERIM)
893 AVRC_PassRsp(p_data->rc_msg.handle, p_data->rc_msg.label,
894 &p_data->rc_msg.msg.pass);
896 /* set up for callback if supported */
897 if (p_data->rc_msg.msg.hdr.ctype == AVRC_RSP_ACCEPT ||
898 p_data->rc_msg.msg.hdr.ctype == AVRC_RSP_INTERIM) {
899 evt = BTA_AV_REMOTE_CMD_EVT;
900 av.remote_cmd.rc_id = p_data->rc_msg.msg.pass.op_id;
901 av.remote_cmd.key_state = p_data->rc_msg.msg.pass.state;
902 av.remote_cmd.p_data = p_data->rc_msg.msg.pass.p_pass_data;
903 av.remote_cmd.len = p_data->rc_msg.msg.pass.pass_len;
904 memcpy(&av.remote_cmd.hdr, &p_data->rc_msg.msg.hdr, sizeof(tAVRC_HDR));
905 av.remote_cmd.label = p_data->rc_msg.label;
908 /* else if this is a pass thru response */
909 /* id response type is not impl, we have to release label */
910 else if (p_data->rc_msg.msg.hdr.ctype >= AVRC_RSP_NOT_IMPL) {
911 /* set up for callback */
912 evt = BTA_AV_REMOTE_RSP_EVT;
913 av.remote_rsp.rc_id = p_data->rc_msg.msg.pass.op_id;
914 av.remote_rsp.key_state = p_data->rc_msg.msg.pass.state;
915 av.remote_rsp.rsp_code = p_data->rc_msg.msg.hdr.ctype;
916 av.remote_rsp.label = p_data->rc_msg.label;
918 /* If this response is for vendor unique command */
919 if ((p_data->rc_msg.msg.pass.op_id == AVRC_ID_VENDOR) &&
920 (p_data->rc_msg.msg.pass.pass_len > 0)) {
921 av.remote_rsp.p_data =
922 (uint8_t*)osi_malloc(p_data->rc_msg.msg.pass.pass_len);
923 APPL_TRACE_DEBUG("%s: Vendor Unique data len = %d", __func__,
924 p_data->rc_msg.msg.pass.pass_len);
925 memcpy(av.remote_rsp.p_data, p_data->rc_msg.msg.pass.p_pass_data,
926 p_data->rc_msg.msg.pass.pass_len);
929 /* must be a bad ctype -> reject*/
931 p_data->rc_msg.msg.hdr.ctype = AVRC_RSP_REJ;
932 AVRC_PassRsp(p_data->rc_msg.handle, p_data->rc_msg.label,
933 &p_data->rc_msg.msg.pass);
936 /* else if this is a vendor specific command or response */
937 else if (p_data->rc_msg.opcode == AVRC_OP_VENDOR) {
938 /* set up for callback */
939 av.vendor_cmd.code = p_data->rc_msg.msg.hdr.ctype;
940 av.vendor_cmd.company_id = p_vendor->company_id;
941 av.vendor_cmd.label = p_data->rc_msg.label;
942 av.vendor_cmd.p_data = p_vendor->p_vendor_data;
943 av.vendor_cmd.len = p_vendor->vendor_len;
945 /* if configured to support vendor specific and it's a command */
946 if ((p_cb->features & BTA_AV_FEAT_VENDOR) &&
947 p_data->rc_msg.msg.hdr.ctype <= AVRC_CMD_GEN_INQ) {
948 if ((p_cb->features & BTA_AV_FEAT_METADATA) &&
949 (p_vendor->company_id == AVRC_CO_METADATA)) {
950 av.meta_msg.p_msg = &p_data->rc_msg.msg;
951 rc_rsp.rsp.status = BTA_AV_STS_NO_RSP;
952 evt = bta_av_proc_meta_cmd(&rc_rsp, &p_data->rc_msg, &ctype);
954 evt = BTA_AV_VENDOR_CMD_EVT;
956 } else if ((p_cb->features & BTA_AV_FEAT_VENDOR) &&
957 p_data->rc_msg.msg.hdr.ctype >= AVRC_RSP_NOT_IMPL) {
958 /* else if configured to support vendor specific and it's a response */
959 if ((p_cb->features & BTA_AV_FEAT_METADATA) &&
960 (p_vendor->company_id == AVRC_CO_METADATA)) {
961 av.meta_msg.p_msg = &p_data->rc_msg.msg;
962 evt = BTA_AV_META_MSG_EVT;
964 evt = BTA_AV_VENDOR_RSP_EVT;
966 } else if (!(p_cb->features & BTA_AV_FEAT_VENDOR) &&
967 p_data->rc_msg.msg.hdr.ctype <= AVRC_CMD_GEN_INQ) {
968 /* else if not configured to support vendor specific and it's a command */
969 if (p_data->rc_msg.msg.vendor.p_vendor_data[0] == AVRC_PDU_INVALID) {
971 p_data->rc_msg.msg.hdr.ctype = AVRC_RSP_REJ;
972 p_data->rc_msg.msg.vendor.p_vendor_data[4] = AVRC_STS_BAD_CMD;
974 p_data->rc_msg.msg.hdr.ctype = AVRC_RSP_NOT_IMPL;
976 AVRC_VendorRsp(p_data->rc_msg.handle, p_data->rc_msg.label,
977 &p_data->rc_msg.msg.vendor);
979 } else if (p_data->rc_msg.opcode == AVRC_OP_BROWSE) {
980 /* set up for callback */
981 av.meta_msg.rc_handle = p_data->rc_msg.handle;
982 av.meta_msg.company_id = p_vendor->company_id;
983 av.meta_msg.code = p_data->rc_msg.msg.hdr.ctype;
984 av.meta_msg.label = p_data->rc_msg.label;
985 av.meta_msg.p_msg = &p_data->rc_msg.msg;
986 av.meta_msg.p_data = p_data->rc_msg.msg.browse.p_browse_data;
987 av.meta_msg.len = p_data->rc_msg.msg.browse.browse_len;
988 evt = BTA_AV_META_MSG_EVT;
991 if (evt == 0 && rc_rsp.rsp.status != BTA_AV_STS_NO_RSP) {
993 rc_rsp.rsp.opcode = p_data->rc_msg.opcode;
994 AVRC_BldResponse(0, &rc_rsp, &p_pkt);
997 AVRC_MsgReq(p_data->rc_msg.handle, p_data->rc_msg.label, ctype, p_pkt);
1002 av.remote_cmd.rc_handle = p_data->rc_msg.handle;
1003 (*p_cb->p_cback)(evt, &av);
1004 /* If browsing message, then free the browse message buffer */
1005 bta_av_rc_free_browse_msg(p_cb, p_data);
1009 /*******************************************************************************
1011 * Function bta_av_rc_close
1013 * Description close the specified AVRC handle.
1017 ******************************************************************************/
1018 void bta_av_rc_close(tBTA_AV_CB* p_cb, tBTA_AV_DATA* p_data) {
1019 uint16_t handle = p_data->hdr.layer_specific;
1023 if (handle < BTA_AV_NUM_RCB) {
1024 p_rcb = &p_cb->rcb[handle];
1026 APPL_TRACE_DEBUG("%s: handle: %d, status=0x%x", __func__, p_rcb->handle,
1028 if (p_rcb->handle != BTA_AV_RC_HANDLE_NONE) {
1030 p_scb = bta_av_cb.p_scb[p_rcb->shdl - 1];
1032 /* just in case the RC timer is active
1033 if (bta_av_cb.features & BTA_AV_FEAT_RCCT &&
1034 p_scb->chnl == BTA_AV_CHNL_AUDIO) */
1035 alarm_cancel(p_scb->avrc_ct_timer);
1039 AVRC_Close(p_rcb->handle);
1044 /*******************************************************************************
1046 * Function bta_av_rc_browse_close
1048 * Description Empty placeholder.
1052 ******************************************************************************/
1053 void bta_av_rc_browse_close(tBTA_AV_CB* p_cb, tBTA_AV_DATA* p_data) {
1054 APPL_TRACE_WARNING("%s: empty placeholder does nothing!", __func__);
1057 /*******************************************************************************
1059 * Function bta_av_get_shdl
1061 * Returns The index to p_scb[]
1063 ******************************************************************************/
1064 static uint8_t bta_av_get_shdl(tBTA_AV_SCB* p_scb) {
1067 /* find the SCB & stop the timer */
1068 for (i = 0; i < BTA_AV_NUM_STRS; i++) {
1069 if (p_scb == bta_av_cb.p_scb[i]) {
1077 /*******************************************************************************
1079 * Function bta_av_stream_chg
1081 * Description audio streaming status changed.
1085 ******************************************************************************/
1086 void bta_av_stream_chg(tBTA_AV_SCB* p_scb, bool started) {
1087 uint8_t started_msk = BTA_AV_HNDL_TO_MSK(p_scb->hdi);
1089 APPL_TRACE_DEBUG("%s: peer %s started:%s started_msk:0x%x", __func__,
1090 p_scb->PeerAddress().ToString().c_str(),
1091 logbool(started).c_str(), started_msk);
1094 /* Let L2CAP know this channel is processed with high priority */
1095 L2CA_SetAclPriority(p_scb->PeerAddress(), L2CAP_PRIORITY_HIGH);
1097 /* Let L2CAP know this channel is processed with low priority */
1098 L2CA_SetAclPriority(p_scb->PeerAddress(), L2CAP_PRIORITY_NORMAL);
1102 /*******************************************************************************
1104 * Function bta_av_conn_chg
1106 * Description connetion status changed.
1107 * Open an AVRCP acceptor channel, if new conn.
1111 ******************************************************************************/
1112 void bta_av_conn_chg(tBTA_AV_DATA* p_data) {
1113 tBTA_AV_CB* p_cb = &bta_av_cb;
1114 tBTA_AV_SCB* p_scb = NULL;
1115 tBTA_AV_SCB* p_scbi;
1120 int index = (p_data->hdr.layer_specific & BTA_AV_HNDL_MSK) - 1;
1122 tBTA_AV_LCB* p_lcb_rc;
1123 tBTA_AV_RCB *p_rcb, *p_rcb2;
1124 bool chk_restore = false;
1126 /* Validate array index*/
1127 if (index < BTA_AV_NUM_STRS) {
1128 p_scb = p_cb->p_scb[index];
1130 mask = BTA_AV_HNDL_TO_MSK(index);
1131 p_lcb = bta_av_find_lcb(p_data->conn_chg.peer_addr, BTA_AV_LCB_FIND);
1132 conn_msk = 1 << (index + 1);
1133 if (p_data->conn_chg.is_up) {
1134 /* set the conned mask for this channel */
1137 p_lcb->conn_msk |= conn_msk;
1138 for (i = 0; i < BTA_AV_NUM_RCB; i++) {
1139 if (bta_av_cb.rcb[i].lidx == p_lcb->lidx) {
1140 bta_av_cb.rcb[i].shdl = index + 1;
1142 "%s: conn_chg up[%d]: %d, status=0x%x, shdl:%d, lidx:%d",
1143 __func__, i, bta_av_cb.rcb[i].handle, bta_av_cb.rcb[i].status,
1144 bta_av_cb.rcb[i].shdl, bta_av_cb.rcb[i].lidx);
1149 old_msk = p_cb->conn_audio;
1150 p_cb->conn_audio |= mask;
1152 if ((old_msk & mask) == 0) {
1153 /* increase the audio open count, if not set yet */
1154 bta_av_cb.audio_open_cnt++;
1157 APPL_TRACE_DEBUG("%s: rc_acp_handle:%d rc_acp_idx:%d", __func__,
1158 p_cb->rc_acp_handle, p_cb->rc_acp_idx);
1159 /* check if the AVRCP ACP channel is already connected */
1160 if (p_lcb && p_cb->rc_acp_handle != BTA_AV_RC_HANDLE_NONE &&
1162 p_lcb_rc = &p_cb->lcb[BTA_AV_NUM_LINKS];
1164 "%s: rc_acp is connected && conn_chg on same addr "
1165 "p_lcb_rc->conn_msk:x%x",
1166 __func__, p_lcb_rc->conn_msk);
1167 /* check if the RC is connected to the scb addr */
1168 LOG_INFO("%s: p_lcb_rc->addr: %s conn_chg.peer_addr: %s", __func__,
1169 p_lcb_rc->addr.ToString().c_str(),
1170 p_data->conn_chg.peer_addr.ToString().c_str());
1172 if (p_lcb_rc->conn_msk &&
1173 p_lcb_rc->addr == p_data->conn_chg.peer_addr) {
1174 /* AVRCP is already connected.
1175 * need to update the association betwen SCB and RCB */
1176 p_lcb_rc->conn_msk = 0; /* indicate RC ONLY is not connected */
1178 p_scb->rc_handle = p_cb->rc_acp_handle;
1179 p_rcb = &p_cb->rcb[p_cb->rc_acp_idx - 1];
1180 p_rcb->shdl = bta_av_get_shdl(p_scb);
1181 APPL_TRACE_DEBUG("%s: update rc_acp shdl:%d/%d srch:%d", __func__,
1182 index + 1, p_rcb->shdl, p_scb->rc_handle);
1184 p_rcb2 = bta_av_get_rcb_by_shdl(p_rcb->shdl);
1186 /* found the RCB that was created to associated with this SCB */
1187 p_cb->rc_acp_handle = p_rcb2->handle;
1188 p_cb->rc_acp_idx = (p_rcb2 - p_cb->rcb) + 1;
1189 APPL_TRACE_DEBUG("%s: new rc_acp_handle:%d, idx:%d", __func__,
1190 p_cb->rc_acp_handle, p_cb->rc_acp_idx);
1191 p_rcb2->lidx = (BTA_AV_NUM_LINKS + 1);
1192 APPL_TRACE_DEBUG("%s: rc2 handle:%d lidx:%d/%d", __func__,
1193 p_rcb2->handle, p_rcb2->lidx,
1194 p_cb->lcb[p_rcb2->lidx - 1].lidx);
1196 p_rcb->lidx = p_lcb->lidx;
1197 APPL_TRACE_DEBUG("%s: rc handle:%d lidx:%d/%d", __func__,
1198 p_rcb->handle, p_rcb->lidx,
1199 p_cb->lcb[p_rcb->lidx - 1].lidx);
1204 if ((p_cb->conn_audio & mask) && bta_av_cb.audio_open_cnt) {
1205 /* this channel is still marked as open. decrease the count */
1206 bta_av_cb.audio_open_cnt--;
1209 /* clear the conned mask for this channel */
1210 p_cb->conn_audio &= ~mask;
1212 // The stream is closed. Clear the state.
1213 p_scb->OnDisconnected();
1214 if (p_scb->chnl == BTA_AV_CHNL_AUDIO) {
1216 p_lcb->conn_msk &= ~conn_msk;
1218 /* audio channel is down. make sure the INT channel is down */
1219 /* just in case the RC timer is active
1220 if (p_cb->features & BTA_AV_FEAT_RCCT) */
1221 { alarm_cancel(p_scb->avrc_ct_timer); }
1222 /* one audio channel goes down. check if we need to restore high
1228 APPL_TRACE_DEBUG("%s: shdl:%d", __func__, index + 1);
1229 for (i = 0; i < BTA_AV_NUM_RCB; i++) {
1230 APPL_TRACE_DEBUG("%s: conn_chg dn[%d]: %d, status=0x%x, shdl:%d, lidx:%d",
1231 __func__, i, bta_av_cb.rcb[i].handle,
1232 bta_av_cb.rcb[i].status, bta_av_cb.rcb[i].shdl,
1233 bta_av_cb.rcb[i].lidx);
1234 if (bta_av_cb.rcb[i].shdl == index + 1) {
1235 bta_av_del_rc(&bta_av_cb.rcb[i]);
1236 /* since the connection is already down and info was removed, clean
1238 bta_av_cb.rcb[i].shdl = 0;
1243 if (p_cb->conn_audio == 0) {
1244 /* if both channels are not connected,
1245 * close all RC channels */
1246 bta_av_close_all_rc(p_cb);
1249 /* if the AVRCP is no longer listening, create the listening channel */
1250 if (bta_av_cb.rc_acp_handle == BTA_AV_RC_HANDLE_NONE &&
1251 bta_av_cb.features & BTA_AV_FEAT_RCTG)
1252 bta_av_rc_create(&bta_av_cb, AVCT_ACP, 0, BTA_AV_NUM_LINKS + 1);
1256 "%s: audio:%x up:%d conn_msk:0x%x chk_restore:%d "
1257 "audio_open_cnt:%d",
1258 __func__, p_cb->conn_audio, p_data->conn_chg.is_up, conn_msk, chk_restore,
1259 p_cb->audio_open_cnt);
1262 if (p_cb->audio_open_cnt == 1) {
1263 /* one audio channel goes down and there's one audio channel remains open.
1264 * restore the switch role in default link policy */
1265 BTM_default_unblock_role_switch();
1266 bta_av_restore_switch();
1268 if (p_cb->audio_open_cnt) {
1269 /* adjust flush timeout settings to longer period */
1270 for (i = 0; i < BTA_AV_NUM_STRS; i++) {
1271 p_scbi = bta_av_cb.p_scb[i];
1272 if (p_scbi && p_scbi->chnl == BTA_AV_CHNL_AUDIO && p_scbi->co_started) {
1273 /* may need to update the flush timeout of this already started stream
1275 if (p_scbi->co_started != bta_av_cb.audio_open_cnt) {
1276 p_scbi->co_started = bta_av_cb.audio_open_cnt;
1284 /*******************************************************************************
1286 * Function bta_av_disable
1288 * Description disable AV.
1292 ******************************************************************************/
1293 void bta_av_disable(tBTA_AV_CB* p_cb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
1295 bool disabling_in_progress = false;
1298 p_cb->disabling = true;
1300 bta_av_close_all_rc(p_cb);
1302 osi_free_and_reset((void**)&p_cb->p_disc_db);
1304 /* disable audio/video - de-register all channels,
1305 * expect BTA_AV_DEREG_COMP_EVT when deregister is complete */
1306 for (xx = 0; xx < BTA_AV_NUM_STRS; xx++) {
1307 if (p_cb->p_scb[xx] != NULL) {
1308 hdr.layer_specific = xx + 1;
1309 bta_av_api_deregister((tBTA_AV_DATA*)&hdr);
1310 disabling_in_progress = true;
1313 // Since All channels are deregistering by API_DEREGISTER, the DEREG_COMP_EVT
1314 // would come first before API_DISABLE if there is no connections, and it is
1315 // no needed to setup this disabling flag.
1316 p_cb->disabling = disabling_in_progress;
1318 alarm_free(p_cb->link_signalling_timer);
1319 p_cb->link_signalling_timer = NULL;
1320 alarm_free(p_cb->accept_signalling_timer);
1321 p_cb->accept_signalling_timer = NULL;
1324 /*******************************************************************************
1326 * Function bta_av_api_disconnect
1332 ******************************************************************************/
1333 void bta_av_api_disconnect(tBTA_AV_DATA* p_data) {
1334 AVDT_DisconnectReq(p_data->api_discnt.bd_addr, bta_av_conn_cback);
1335 alarm_cancel(bta_av_cb.link_signalling_timer);
1339 * Find the index for the free LCB entry to use.
1341 * The selection order is:
1342 * (1) Find the index if there is already SCB entry for the peer address
1343 * (2) If there is no SCB entry for the peer address, find the first
1344 * SCB entry that is not assigned.
1346 * @param peer_address the peer address to use
1347 * @return the index for the free LCB entry to use or BTA_AV_NUM_LINKS
1348 * if no entry is found
1350 static uint8_t bta_av_find_lcb_index_by_scb_and_address(
1351 const RawAddress& peer_address) {
1352 APPL_TRACE_DEBUG("%s: peer_address: %s conn_lcb: 0x%x", __func__,
1353 peer_address.ToString().c_str(), bta_av_cb.conn_lcb);
1355 // Find the index if there is already SCB entry for the peer address
1356 for (uint8_t index = 0; index < BTA_AV_NUM_LINKS; index++) {
1357 uint8_t mask = 1 << index;
1358 if (mask & bta_av_cb.conn_lcb) {
1361 tBTA_AV_SCB* p_scb = bta_av_cb.p_scb[index];
1362 if (p_scb == nullptr) {
1365 if (p_scb->PeerAddress() == peer_address) {
1370 // Find the first SCB entry that is not assigned.
1371 for (uint8_t index = 0; index < BTA_AV_NUM_LINKS; index++) {
1372 uint8_t mask = 1 << index;
1373 if (mask & bta_av_cb.conn_lcb) {
1376 tBTA_AV_SCB* p_scb = bta_av_cb.p_scb[index];
1377 if (p_scb == nullptr) {
1380 if (!p_scb->IsAssigned()) {
1385 return BTA_AV_NUM_LINKS;
1388 /*******************************************************************************
1390 * Function bta_av_sig_chg
1392 * Description process AVDT signal channel up/down.
1396 ******************************************************************************/
1397 void bta_av_sig_chg(tBTA_AV_DATA* p_data) {
1398 uint16_t event = p_data->str_msg.hdr.layer_specific;
1399 tBTA_AV_CB* p_cb = &bta_av_cb;
1402 tBTA_AV_LCB* p_lcb = NULL;
1404 APPL_TRACE_DEBUG("%s: event: %d", __func__, event);
1405 if (event == AVDT_CONNECT_IND_EVT) {
1406 APPL_TRACE_DEBUG("%s: AVDT_CONNECT_IND_EVT: peer %s", __func__,
1407 p_data->str_msg.bd_addr.ToString().c_str());
1409 p_lcb = bta_av_find_lcb(p_data->str_msg.bd_addr, BTA_AV_LCB_FIND);
1411 /* if the address does not have an LCB yet, alloc one */
1412 xx = bta_av_find_lcb_index_by_scb_and_address(p_data->str_msg.bd_addr);
1414 /* check if we found something */
1415 if (xx >= BTA_AV_NUM_LINKS) {
1416 /* We do not have scb for this avdt connection. */
1417 /* Silently close the connection. */
1418 APPL_TRACE_ERROR("%s: av scb not available for avdt connection for %s",
1419 __func__, p_data->str_msg.bd_addr.ToString().c_str());
1420 AVDT_DisconnectReq(p_data->str_msg.bd_addr, NULL);
1423 LOG_INFO("%s: AVDT_CONNECT_IND_EVT: peer %s selected lcb_index %d",
1424 __func__, p_data->str_msg.bd_addr.ToString().c_str(), xx);
1426 tBTA_AV_SCB* p_scb = p_cb->p_scb[xx];
1428 p_lcb = &p_cb->lcb[xx];
1429 p_lcb->lidx = xx + 1;
1430 p_lcb->addr = p_data->str_msg.bd_addr;
1431 p_lcb->conn_msk = 0; /* clear the connect mask */
1432 /* start listening when the signal channel is open */
1433 if (p_cb->features & BTA_AV_FEAT_RCTG) {
1434 bta_av_rc_create(p_cb, AVCT_ACP, 0, p_lcb->lidx);
1436 /* this entry is not used yet. */
1437 p_cb->conn_lcb |= mask; /* mark it as used */
1438 APPL_TRACE_DEBUG("%s: start sig timer %d", __func__, p_data->hdr.offset);
1439 if (p_data->hdr.offset == AVDT_ACP) {
1440 APPL_TRACE_DEBUG("%s: Incoming L2CAP acquired, set state as incoming",
1442 p_scb->OnConnected(p_data->str_msg.bd_addr);
1443 p_scb->use_rc = true; /* allowing RC for incoming connection */
1444 bta_av_ssm_execute(p_scb, BTA_AV_ACP_CONNECT_EVT, p_data);
1446 /* The Pending Event should be sent as soon as the L2CAP signalling
1448 * is set up, which is NOW. Earlier this was done only after
1449 * BTA_AV_SIGNALLING_TIMEOUT_MS.
1450 * The following function shall send the event and start the
1453 bta_av_signalling_timer(NULL);
1455 APPL_TRACE_DEBUG("%s: Re-start timer for AVDTP service", __func__);
1456 bta_sys_conn_open(BTA_ID_AV, p_scb->app_id, p_scb->PeerAddress());
1457 /* Possible collision : need to avoid outgoing processing while the
1458 * timer is running */
1459 p_scb->coll_mask = BTA_AV_COLL_INC_TMR;
1461 p_cb->accept_signalling_timer, BTA_AV_ACCEPT_SIGNALLING_TIMEOUT_MS,
1462 bta_av_accept_signalling_timer_cback, UINT_TO_PTR(xx));
1466 else if (event == BTA_AR_AVDT_CONN_EVT) {
1467 alarm_cancel(bta_av_cb.link_signalling_timer);
1471 APPL_TRACE_DEBUG("%s: bta_av_cb.conn_lcb=0x%x", __func__,
1472 bta_av_cb.conn_lcb);
1474 p_lcb = bta_av_find_lcb(p_data->str_msg.bd_addr, BTA_AV_LCB_FREE);
1475 if (p_lcb && (p_lcb->conn_msk || bta_av_cb.conn_lcb)) {
1476 APPL_TRACE_DEBUG("%s: conn_msk: 0x%x", __func__, p_lcb->conn_msk);
1478 for (xx = 0; xx < BTA_AV_NUM_STRS; xx++) {
1479 if (p_cb->p_scb[xx] &&
1480 p_cb->p_scb[xx]->PeerAddress() == p_data->str_msg.bd_addr) {
1481 APPL_TRACE_DEBUG("%s: Closing timer for AVDTP service", __func__);
1482 bta_sys_conn_close(BTA_ID_AV, p_cb->p_scb[xx]->app_id,
1483 p_cb->p_scb[xx]->PeerAddress());
1485 mask = 1 << (xx + 1);
1486 if (((mask & p_lcb->conn_msk) || bta_av_cb.conn_lcb) &&
1488 p_cb->p_scb[xx]->PeerAddress() == p_data->str_msg.bd_addr) {
1489 APPL_TRACE_WARNING("%s: Sending AVDT_DISCONNECT_EVT peer_addr=%s",
1491 p_cb->p_scb[xx]->PeerAddress().ToString().c_str());
1492 bta_av_ssm_execute(p_cb->p_scb[xx], BTA_AV_AVDT_DISCONNECT_EVT, NULL);
1497 APPL_TRACE_DEBUG("%s: bta_av_cb.conn_lcb=0x%x after sig_chg", __func__,
1501 /*******************************************************************************
1503 * Function bta_av_signalling_timer
1505 * Description process the signal channel timer. This timer is started
1506 * when the AVDTP signal channel is connected. If no profile
1507 * is connected, the timer goes off every
1508 * BTA_AV_SIGNALLING_TIMEOUT_MS.
1512 ******************************************************************************/
1513 void bta_av_signalling_timer(UNUSED_ATTR tBTA_AV_DATA* p_data) {
1514 tBTA_AV_CB* p_cb = &bta_av_cb;
1517 tBTA_AV_LCB* p_lcb = NULL;
1519 APPL_TRACE_DEBUG("%s: conn_lcb=0x%x", __func__, p_cb->conn_lcb);
1520 for (xx = 0; xx < BTA_AV_NUM_LINKS; xx++) {
1521 p_lcb = &p_cb->lcb[xx];
1524 "%s: index=%d conn_lcb=0x%x peer=%s conn_mask=0x%x lidx=%d", __func__,
1525 xx, p_cb->conn_lcb, p_lcb->addr.ToString().c_str(), p_lcb->conn_msk,
1527 if (mask & p_cb->conn_lcb) {
1528 /* this entry is used. check if it is connected */
1529 if (!p_lcb->conn_msk) {
1530 bta_sys_start_timer(p_cb->link_signalling_timer,
1531 BTA_AV_SIGNALLING_TIMEOUT_MS,
1532 BTA_AV_SIGNALLING_TIMER_EVT, 0);
1534 pend.bd_addr = p_lcb->addr;
1535 tBTA_AV bta_av_data;
1536 bta_av_data.pend = pend;
1538 "%s: BTA_AV_PENDING_EVT for %s index=%d conn_mask=0x%x lidx=%d",
1539 __func__, pend.bd_addr.ToString().c_str(), xx, p_lcb->conn_msk,
1541 (*p_cb->p_cback)(BTA_AV_PENDING_EVT, &bta_av_data);
1547 /*******************************************************************************
1549 * Function bta_av_accept_signalling_timer_cback
1551 * Description Process the timeout when SRC is accepting connection
1552 * and SNK did not start signalling.
1556 ******************************************************************************/
1557 static void bta_av_accept_signalling_timer_cback(void* data) {
1558 uint32_t inx = PTR_TO_UINT(data);
1559 tBTA_AV_CB* p_cb = &bta_av_cb;
1560 tBTA_AV_SCB* p_scb = NULL;
1561 if (inx < BTA_AV_NUM_STRS) {
1562 p_scb = p_cb->p_scb[inx];
1565 APPL_TRACE_DEBUG("%s: coll_mask=0x%02x", __func__, p_scb->coll_mask);
1567 if (p_scb->coll_mask & BTA_AV_COLL_INC_TMR) {
1568 p_scb->coll_mask &= ~BTA_AV_COLL_INC_TMR;
1570 if (bta_av_is_scb_opening(p_scb)) {
1571 APPL_TRACE_DEBUG("%s: stream state opening: SDP started = %d", __func__,
1572 p_scb->sdp_discovery_started);
1573 if (p_scb->sdp_discovery_started) {
1574 /* We are still doing SDP. Run the timer again. */
1575 p_scb->coll_mask |= BTA_AV_COLL_INC_TMR;
1577 alarm_set_on_mloop(p_cb->accept_signalling_timer,
1578 BTA_AV_ACCEPT_SIGNALLING_TIMEOUT_MS,
1579 bta_av_accept_signalling_timer_cback,
1582 /* SNK did not start signalling, resume signalling process. */
1583 bta_av_discover_req(p_scb, NULL);
1585 } else if (bta_av_is_scb_incoming(p_scb)) {
1586 /* Stay in incoming state if SNK does not start signalling */
1588 APPL_TRACE_DEBUG("%s: stream state incoming", __func__);
1589 /* API open was called right after SNK opened L2C connection. */
1590 if (p_scb->coll_mask & BTA_AV_COLL_API_CALLED) {
1591 p_scb->coll_mask &= ~BTA_AV_COLL_API_CALLED;
1593 /* BTA_AV_API_OPEN_EVT */
1594 tBTA_AV_API_OPEN* p_buf =
1595 (tBTA_AV_API_OPEN*)osi_malloc(sizeof(tBTA_AV_API_OPEN));
1596 memcpy(p_buf, &(p_scb->open_api), sizeof(tBTA_AV_API_OPEN));
1597 bta_sys_sendmsg(p_buf);
1604 /*******************************************************************************
1606 * Function bta_av_check_peer_features
1608 * Description check supported features on the peer device from the SDP
1609 * record and return the feature mask
1611 * Returns tBTA_AV_FEAT peer device feature mask
1613 ******************************************************************************/
1614 tBTA_AV_FEAT bta_av_check_peer_features(uint16_t service_uuid) {
1615 tBTA_AV_FEAT peer_features = 0;
1616 tBTA_AV_CB* p_cb = &bta_av_cb;
1617 tSDP_DISC_REC* p_rec = NULL;
1618 tSDP_DISC_ATTR* p_attr;
1619 uint16_t peer_rc_version = 0;
1620 uint16_t categories = 0;
1622 APPL_TRACE_DEBUG("%s: service_uuid:x%x", __func__, service_uuid);
1623 /* loop through all records we found */
1625 /* get next record; if none found, we're done */
1626 p_rec = SDP_FindServiceInDb(p_cb->p_disc_db, service_uuid, p_rec);
1627 if (p_rec == NULL) {
1631 if ((SDP_FindAttributeInRec(p_rec, ATTR_ID_SERVICE_CLASS_ID_LIST)) !=
1633 /* find peer features */
1634 if (SDP_FindServiceInDb(p_cb->p_disc_db, UUID_SERVCLASS_AV_REMOTE_CONTROL,
1636 peer_features |= BTA_AV_FEAT_RCCT;
1638 if (SDP_FindServiceInDb(p_cb->p_disc_db,
1639 UUID_SERVCLASS_AV_REM_CTRL_TARGET, NULL)) {
1640 peer_features |= BTA_AV_FEAT_RCTG;
1644 if ((SDP_FindAttributeInRec(p_rec, ATTR_ID_BT_PROFILE_DESC_LIST)) != NULL) {
1645 /* get profile version (if failure, version parameter is not updated) */
1646 SDP_FindProfileVersionInRec(p_rec, UUID_SERVCLASS_AV_REMOTE_CONTROL,
1648 APPL_TRACE_DEBUG("%s: peer_rc_version 0x%x", __func__, peer_rc_version);
1650 if (peer_rc_version >= AVRC_REV_1_3)
1651 peer_features |= (BTA_AV_FEAT_VENDOR | BTA_AV_FEAT_METADATA);
1653 if (peer_rc_version >= AVRC_REV_1_4) {
1654 /* get supported categories */
1655 p_attr = SDP_FindAttributeInRec(p_rec, ATTR_ID_SUPPORTED_FEATURES);
1656 if (p_attr != NULL) {
1657 categories = p_attr->attr_value.v.u16;
1658 if (categories & AVRC_SUPF_CT_CAT2)
1659 peer_features |= (BTA_AV_FEAT_ADV_CTRL);
1660 if (categories & AVRC_SUPF_CT_BROWSE)
1661 peer_features |= (BTA_AV_FEAT_BROWSE);
1666 APPL_TRACE_DEBUG("%s: peer_features:x%x", __func__, peer_features);
1667 return peer_features;
1670 /*******************************************************************************
1672 * Function bta_avk_check_peer_features
1674 * Description check supported features on the peer device from the SDP
1675 * record and return the feature mask
1677 * Returns tBTA_AV_FEAT peer device feature mask
1679 ******************************************************************************/
1680 tBTA_AV_FEAT bta_avk_check_peer_features(uint16_t service_uuid) {
1681 tBTA_AV_FEAT peer_features = 0;
1682 tBTA_AV_CB* p_cb = &bta_av_cb;
1684 APPL_TRACE_DEBUG("%s: service_uuid:x%x", __func__, service_uuid);
1686 /* loop through all records we found */
1687 tSDP_DISC_REC* p_rec =
1688 SDP_FindServiceInDb(p_cb->p_disc_db, service_uuid, NULL);
1690 APPL_TRACE_DEBUG("%s: found Service record for x%x", __func__,
1693 if ((SDP_FindAttributeInRec(p_rec, ATTR_ID_SERVICE_CLASS_ID_LIST)) !=
1695 /* find peer features */
1696 if (SDP_FindServiceInDb(p_cb->p_disc_db, UUID_SERVCLASS_AV_REMOTE_CONTROL,
1698 peer_features |= BTA_AV_FEAT_RCCT;
1700 if (SDP_FindServiceInDb(p_cb->p_disc_db,
1701 UUID_SERVCLASS_AV_REM_CTRL_TARGET, NULL)) {
1702 peer_features |= BTA_AV_FEAT_RCTG;
1706 if ((SDP_FindAttributeInRec(p_rec, ATTR_ID_BT_PROFILE_DESC_LIST)) != NULL) {
1707 /* get profile version (if failure, version parameter is not updated) */
1708 uint16_t peer_rc_version = 0;
1709 bool val = SDP_FindProfileVersionInRec(
1710 p_rec, UUID_SERVCLASS_AV_REMOTE_CONTROL, &peer_rc_version);
1711 APPL_TRACE_DEBUG("%s: peer_rc_version for TG 0x%x, profile_found %d",
1712 __func__, peer_rc_version, val);
1714 if (peer_rc_version >= AVRC_REV_1_3)
1715 peer_features |= (BTA_AV_FEAT_VENDOR | BTA_AV_FEAT_METADATA);
1717 /* Get supported features */
1718 tSDP_DISC_ATTR* p_attr =
1719 SDP_FindAttributeInRec(p_rec, ATTR_ID_SUPPORTED_FEATURES);
1720 if (p_attr != NULL) {
1721 uint16_t categories = p_attr->attr_value.v.u16;
1723 * Though Absolute Volume came after in 1.4 and above, but there are
1724 * few devices in market which supports absolute Volume and they are
1725 * still 1.3. To avoid IOP issuses with those devices, we check for
1726 * 1.3 as minimum version
1728 if (peer_rc_version >= AVRC_REV_1_3) {
1729 if (categories & AVRC_SUPF_TG_CAT2)
1730 peer_features |= (BTA_AV_FEAT_ADV_CTRL);
1731 if (categories & AVRC_SUPF_TG_APP_SETTINGS)
1732 peer_features |= (BTA_AV_FEAT_APP_SETTING);
1733 if (categories & AVRC_SUPF_TG_BROWSE)
1734 peer_features |= (BTA_AV_FEAT_BROWSE);
1737 /* AVRCP Cover Artwork over BIP */
1738 if (peer_rc_version >= AVRC_REV_1_6) {
1739 if (service_uuid == UUID_SERVCLASS_AV_REM_CTRL_TARGET &&
1740 categories & AVRC_SUPF_TG_PLAYER_COVER_ART)
1741 peer_features |= (BTA_AV_FEAT_COVER_ARTWORK);
1745 /* get next record; if none found, we're done */
1746 p_rec = SDP_FindServiceInDb(p_cb->p_disc_db, service_uuid, p_rec);
1748 APPL_TRACE_DEBUG("%s: peer_features:x%x", __func__, peer_features);
1749 return peer_features;
1752 /******************************************************************************
1754 * Function bta_avk_get_cover_art_psm
1756 * Description Get the PSM associated with the AVRCP Target cover art
1759 * Returns uint16_t PSM value used to get cover artwork, or 0x0000 if
1760 * one does not exist.
1762 *****************************************************************************/
1763 uint16_t bta_avk_get_cover_art_psm() {
1764 APPL_TRACE_DEBUG("%s: searching for cover art psm", __func__);
1765 /* Cover Art L2CAP PSM is only available on a target device */
1766 tBTA_AV_CB* p_cb = &bta_av_cb;
1767 tSDP_DISC_REC* p_rec =
1768 SDP_FindServiceInDb(p_cb->p_disc_db, UUID_SERVCLASS_AV_REM_CTRL_TARGET,
1771 tSDP_DISC_ATTR* p_attr =
1772 (SDP_FindAttributeInRec(p_rec, ATTR_ID_ADDITION_PROTO_DESC_LISTS));
1774 * If we have the Additional Protocol Description Lists attribute then we
1775 * specifically want the list that is an L2CAP protocol leading to OBEX.
1776 * Because the is a case where cover art is supported and browsing isn't
1777 * we need to check each list for the one we want.
1779 * This means we need to do drop down into the protocol list and do a
1780 * "for each protocol, for each protocol element, for each protocol element
1781 * list parameter, if the parameter is L2CAP then find the PSM associated
1782 * with it, then make sure we see OBEX in that same protocol"
1784 if (p_attr != NULL && SDP_DISC_ATTR_TYPE(p_attr->attr_len_type)
1785 == DATA_ELE_SEQ_DESC_TYPE) {
1786 // Point to first in List of protocols (i.e [(L2CAP -> AVCTP),
1787 // (L2CAP -> OBEX)])
1788 tSDP_DISC_ATTR* p_protocol_list = p_attr->attr_value.v.p_sub_attr;
1789 while (p_protocol_list != NULL) {
1790 if (SDP_DISC_ATTR_TYPE(p_protocol_list->attr_len_type)
1791 == DATA_ELE_SEQ_DESC_TYPE) {
1792 // Point to fist in list of protocol elements (i.e. [L2CAP, AVCTP])
1793 tSDP_DISC_ATTR* p_protocol =
1794 p_protocol_list->attr_value.v.p_sub_attr;
1795 bool protocol_has_obex = false;
1796 bool protocol_has_l2cap = false;
1797 uint16_t psm = 0x0000;
1798 while (p_protocol) {
1799 if (SDP_DISC_ATTR_TYPE(p_protocol->attr_len_type)
1800 == DATA_ELE_SEQ_DESC_TYPE) {
1801 // Point to first item protocol parameters list (i.e [UUID=L2CAP,
1803 tSDP_DISC_ATTR* p_protocol_param =
1804 p_protocol->attr_value.v.p_sub_attr;
1806 * Currently there's only ever one UUID and one parameter. L2cap
1807 * has a single PSM, AVCTP has a version and OBEX has nothing.
1808 * Change this if that ever changes.
1810 uint16_t protocol_uuid = 0;
1811 uint16_t protocol_param = 0;
1812 while (p_protocol_param) {
1813 uint16_t param_type =
1814 SDP_DISC_ATTR_TYPE(p_protocol_param->attr_len_type);
1815 uint16_t param_len =
1816 SDP_DISC_ATTR_LEN(p_protocol_param->attr_len_type);
1817 if (param_type == UUID_DESC_TYPE) {
1818 protocol_uuid = p_protocol_param->attr_value.v.u16;
1819 } else if (param_type == UINT_DESC_TYPE) {
1820 protocol_param = (param_len == 2)
1821 ? p_protocol_param->attr_value.v.u16
1822 : p_protocol_param->attr_value.v.u8;
1823 } /* else dont care */
1824 p_protocol_param = p_protocol_param->p_next_attr; // next
1826 // If we've found L2CAP then the parameter is a PSM
1827 if (protocol_uuid == UUID_PROTOCOL_L2CAP) {
1828 protocol_has_l2cap = true;
1829 psm = protocol_param;
1830 } else if (protocol_uuid == UUID_PROTOCOL_OBEX) {
1831 protocol_has_obex = true;
1834 // If this protocol has l2cap and obex then we're found the BIP PSM
1835 if (protocol_has_l2cap && protocol_has_obex) {
1836 APPL_TRACE_DEBUG("%s: found psm 0x%x", __func__, psm);
1839 p_protocol = p_protocol->p_next_attr; // next protocol element
1842 p_protocol_list = p_protocol_list->p_next_attr; // next protocol
1845 /* get next record; if none found, we're done */
1846 p_rec = SDP_FindServiceInDb(p_cb->p_disc_db,
1847 UUID_SERVCLASS_AV_REM_CTRL_TARGET, p_rec);
1849 /* L2CAP PSM range is 0x1000-0xFFFF so 0x0000 is safe default invalid */
1850 APPL_TRACE_DEBUG("%s: could not find a BIP psm", __func__);
1854 /*******************************************************************************
1856 * Function bta_av_rc_disc_done
1858 * Description Handle AVRCP service discovery results. If matching
1859 * service found, open AVRCP connection.
1863 ******************************************************************************/
1864 void bta_av_rc_disc_done(UNUSED_ATTR tBTA_AV_DATA* p_data) {
1865 tBTA_AV_CB* p_cb = &bta_av_cb;
1866 tBTA_AV_SCB* p_scb = NULL;
1869 tBTA_AV_FEAT peer_features = 0; /* peer features mask */
1870 uint16_t cover_art_psm = 0x0000;
1872 APPL_TRACE_DEBUG("%s: bta_av_rc_disc_done disc:x%x", __func__, p_cb->disc);
1877 if ((p_cb->disc & BTA_AV_CHNL_MSK) == BTA_AV_CHNL_MSK) {
1878 /* this is the rc handle/index to tBTA_AV_RCB */
1879 rc_handle = p_cb->disc & (~BTA_AV_CHNL_MSK);
1881 /* Validate array index*/
1882 if (((p_cb->disc & BTA_AV_HNDL_MSK) - 1) < BTA_AV_NUM_STRS) {
1883 p_scb = p_cb->p_scb[(p_cb->disc & BTA_AV_HNDL_MSK) - 1];
1886 rc_handle = p_scb->rc_handle;
1893 APPL_TRACE_DEBUG("%s: rc_handle %d", __func__, rc_handle);
1894 #if (BTA_AV_SINK_INCLUDED == TRUE)
1895 if (p_cb->sdp_a2dp_snk_handle) {
1896 /* This is Sink + CT + TG(Abs Vol) */
1898 bta_avk_check_peer_features(UUID_SERVCLASS_AV_REM_CTRL_TARGET);
1899 APPL_TRACE_DEBUG("%s: populating rem ctrl target features %d", __func__,
1901 if (BTA_AV_FEAT_ADV_CTRL &
1902 bta_avk_check_peer_features(UUID_SERVCLASS_AV_REMOTE_CONTROL))
1903 peer_features |= (BTA_AV_FEAT_ADV_CTRL | BTA_AV_FEAT_RCCT);
1905 if (peer_features & BTA_AV_FEAT_COVER_ARTWORK)
1906 cover_art_psm = bta_avk_get_cover_art_psm();
1908 APPL_TRACE_DEBUG("%s: populating rem ctrl target bip psm 0x%x", __func__,
1912 if (p_cb->sdp_a2dp_handle) {
1913 /* check peer version and whether support CT and TG role */
1915 bta_av_check_peer_features(UUID_SERVCLASS_AV_REMOTE_CONTROL);
1916 if ((p_cb->features & BTA_AV_FEAT_ADV_CTRL) &&
1917 ((peer_features & BTA_AV_FEAT_ADV_CTRL) == 0)) {
1918 /* if we support advance control and peer does not, check their support on
1920 * some implementation uses 1.3 on CT ans 1.4 on TG */
1922 bta_av_check_peer_features(UUID_SERVCLASS_AV_REM_CTRL_TARGET);
1925 /* Change our features if the remote AVRCP version is 1.3 or less */
1926 tSDP_DISC_REC* p_rec = nullptr;
1927 p_rec = SDP_FindServiceInDb(p_cb->p_disc_db,
1928 UUID_SERVCLASS_AV_REMOTE_CONTROL, p_rec);
1929 if (p_rec != NULL &&
1930 SDP_FindAttributeInRec(p_rec, ATTR_ID_BT_PROFILE_DESC_LIST) != NULL) {
1931 /* get profile version (if failure, version parameter is not updated) */
1932 uint16_t peer_rc_version = 0xFFFF; // Don't change the AVRCP version
1933 SDP_FindProfileVersionInRec(p_rec, UUID_SERVCLASS_AV_REMOTE_CONTROL,
1935 if (peer_rc_version <= AVRC_REV_1_3) {
1936 APPL_TRACE_DEBUG("%s: Using AVRCP 1.3 Capabilities with remote device",
1938 p_bta_av_cfg = &bta_av_cfg_compatibility;
1944 osi_free_and_reset((void**)&p_cb->p_disc_db);
1946 APPL_TRACE_DEBUG("%s: peer_features 0x%x, features 0x%x", __func__,
1947 peer_features, p_cb->features);
1949 /* if we have no rc connection */
1950 if (rc_handle == BTA_AV_RC_HANDLE_NONE) {
1952 /* if peer remote control service matches ours and USE_RC is true */
1953 if ((((p_cb->features & BTA_AV_FEAT_RCCT) &&
1954 (peer_features & BTA_AV_FEAT_RCTG)) ||
1955 ((p_cb->features & BTA_AV_FEAT_RCTG) &&
1956 (peer_features & BTA_AV_FEAT_RCCT)))) {
1957 p_lcb = bta_av_find_lcb(p_scb->PeerAddress(), BTA_AV_LCB_FIND);
1959 rc_handle = bta_av_rc_create(p_cb, AVCT_INT,
1960 (uint8_t)(p_scb->hdi + 1), p_lcb->lidx);
1961 p_cb->rcb[rc_handle].peer_features = peer_features;
1962 p_cb->rcb[rc_handle].cover_art_psm = cover_art_psm;
1964 APPL_TRACE_ERROR("%s: can not find LCB!!", __func__);
1966 } else if (p_scb->use_rc) {
1967 /* can not find AVRC on peer device. report failure */
1968 p_scb->use_rc = false;
1969 tBTA_AV_RC_OPEN rc_open;
1970 rc_open.peer_addr = p_scb->PeerAddress();
1971 rc_open.peer_features = 0;
1972 rc_open.cover_art_psm = 0;
1973 rc_open.status = BTA_AV_FAIL_SDP;
1974 tBTA_AV bta_av_data;
1975 bta_av_data.rc_open = rc_open;
1976 (*p_cb->p_cback)(BTA_AV_RC_OPEN_EVT, &bta_av_data);
1980 tBTA_AV_RC_FEAT rc_feat;
1981 p_cb->rcb[rc_handle].peer_features = peer_features;
1982 rc_feat.rc_handle = rc_handle;
1983 rc_feat.peer_features = peer_features;
1984 if (p_scb == NULL) {
1986 * In case scb is not created by the time we are done with SDP
1987 * we still need to send RC feature event. So we need to get BD
1988 * from Message. Note that lidx is 1 based not 0 based
1990 rc_feat.peer_addr = p_cb->lcb[p_cb->rcb[rc_handle].lidx - 1].addr;
1992 rc_feat.peer_addr = p_scb->PeerAddress();
1995 tBTA_AV bta_av_feat;
1996 bta_av_feat.rc_feat = rc_feat;
1997 (*p_cb->p_cback)(BTA_AV_RC_FEAT_EVT, &bta_av_feat);
2000 APPL_TRACE_DEBUG("%s: Send PSM data", __func__);
2001 tBTA_AV_RC_PSM rc_psm;
2002 p_cb->rcb[rc_handle].cover_art_psm = cover_art_psm;
2003 rc_psm.rc_handle = rc_handle;
2004 rc_psm.cover_art_psm = cover_art_psm;
2005 if (p_scb == NULL) {
2006 rc_psm.peer_addr = p_cb->lcb[p_cb->rcb[rc_handle].lidx - 1].addr;
2008 rc_psm.peer_addr = p_scb->PeerAddress();
2011 APPL_TRACE_DEBUG("%s: rc_psm = 0x%x", __func__, rc_psm.cover_art_psm);
2014 bta_av_psm.rc_cover_art_psm = rc_psm;
2015 (*p_cb->p_cback)(BTA_AV_RC_PSM_EVT, &bta_av_psm);
2019 /*******************************************************************************
2021 * Function bta_av_rc_closed
2023 * Description Set AVRCP state to closed.
2027 ******************************************************************************/
2028 void bta_av_rc_closed(tBTA_AV_DATA* p_data) {
2029 tBTA_AV_CB* p_cb = &bta_av_cb;
2030 tBTA_AV_RC_CLOSE rc_close;
2031 tBTA_AV_RC_CONN_CHG* p_msg = (tBTA_AV_RC_CONN_CHG*)p_data;
2038 rc_close.rc_handle = BTA_AV_RC_HANDLE_NONE;
2040 APPL_TRACE_DEBUG("%s: rc_handle:%d", __func__, p_msg->handle);
2041 for (i = 0; i < BTA_AV_NUM_RCB; i++) {
2042 p_rcb = &p_cb->rcb[i];
2043 APPL_TRACE_DEBUG("%s: rcb[%d] rc_handle:%d, status=0x%x", __func__, i,
2044 p_rcb->handle, p_rcb->status);
2045 if (p_rcb->handle == p_msg->handle) {
2046 rc_close.rc_handle = i;
2047 p_rcb->status &= ~BTA_AV_RC_CONN_MASK;
2048 p_rcb->peer_features = 0;
2049 p_rcb->cover_art_psm = 0;
2050 APPL_TRACE_DEBUG("%s: shdl:%d, lidx:%d", __func__, p_rcb->shdl,
2053 if ((p_rcb->shdl - 1) < BTA_AV_NUM_STRS) {
2054 p_scb = bta_av_cb.p_scb[p_rcb->shdl - 1];
2057 rc_close.peer_addr = p_scb->PeerAddress();
2058 if (p_scb->rc_handle == p_rcb->handle)
2059 p_scb->rc_handle = BTA_AV_RC_HANDLE_NONE;
2060 APPL_TRACE_DEBUG("%s: shdl:%d, srch:%d", __func__, p_rcb->shdl,
2064 } else if (p_rcb->lidx == (BTA_AV_NUM_LINKS + 1)) {
2065 /* if the RCB uses the extra LCB, use the addr for event and clean it */
2066 p_lcb = &p_cb->lcb[BTA_AV_NUM_LINKS];
2067 rc_close.peer_addr = p_msg->peer_addr;
2068 LOG_INFO("%s: rc_only closed bd_addr: %s", __func__,
2069 p_msg->peer_addr.ToString().c_str());
2070 p_lcb->conn_msk = 0;
2075 if ((p_rcb->status & BTA_AV_RC_ROLE_MASK) == BTA_AV_RC_ROLE_INT) {
2076 /* AVCT CCB is deallocated */
2077 p_rcb->handle = BTA_AV_RC_HANDLE_NONE;
2080 /* AVCT CCB is still there. dealloc */
2081 bta_av_del_rc(p_rcb);
2083 } else if ((p_rcb->handle != BTA_AV_RC_HANDLE_NONE) &&
2084 (p_rcb->status & BTA_AV_RC_CONN_MASK)) {
2085 /* at least one channel is still connected */
2091 /* no AVRC channels are connected, go back to INIT state */
2092 bta_av_sm_execute(p_cb, BTA_AV_AVRC_NONE_EVT, NULL);
2095 if (rc_close.rc_handle == BTA_AV_RC_HANDLE_NONE) {
2096 rc_close.rc_handle = p_msg->handle;
2097 rc_close.peer_addr = p_msg->peer_addr;
2099 tBTA_AV bta_av_data;
2100 bta_av_data.rc_close = rc_close;
2101 (*p_cb->p_cback)(BTA_AV_RC_CLOSE_EVT, &bta_av_data);
2102 if (bta_av_cb.rc_acp_handle == BTA_AV_RC_HANDLE_NONE
2103 && bta_av_cb.features & BTA_AV_FEAT_RCTG)
2104 bta_av_rc_create(&bta_av_cb, AVCT_ACP, 0, BTA_AV_NUM_LINKS + 1);
2107 /*******************************************************************************
2109 * Function bta_av_rc_browse_opened
2111 * Description AVRC browsing channel is opened
2115 ******************************************************************************/
2116 void bta_av_rc_browse_opened(tBTA_AV_DATA* p_data) {
2117 tBTA_AV_CB* p_cb = &bta_av_cb;
2118 tBTA_AV_RC_CONN_CHG* p_msg = (tBTA_AV_RC_CONN_CHG*)p_data;
2119 tBTA_AV_RC_BROWSE_OPEN rc_browse_open;
2121 LOG_INFO("%s: peer_addr: %s rc_handle:%d", __func__,
2122 p_msg->peer_addr.ToString().c_str(), p_msg->handle);
2124 rc_browse_open.status = BTA_AV_SUCCESS;
2125 rc_browse_open.rc_handle = p_msg->handle;
2126 rc_browse_open.peer_addr = p_msg->peer_addr;
2128 tBTA_AV bta_av_data;
2129 bta_av_data.rc_browse_open = rc_browse_open;
2130 (*p_cb->p_cback)(BTA_AV_RC_BROWSE_OPEN_EVT, &bta_av_data);
2133 /*******************************************************************************
2135 * Function bta_av_rc_browse_closed
2137 * Description AVRC browsing channel is closed
2141 ******************************************************************************/
2142 void bta_av_rc_browse_closed(tBTA_AV_DATA* p_data) {
2143 tBTA_AV_CB* p_cb = &bta_av_cb;
2144 tBTA_AV_RC_CONN_CHG* p_msg = (tBTA_AV_RC_CONN_CHG*)p_data;
2145 tBTA_AV_RC_BROWSE_CLOSE rc_browse_close;
2147 LOG_INFO("%s: peer_addr: %s rc_handle:%d", __func__,
2148 p_msg->peer_addr.ToString().c_str(), p_msg->handle);
2150 rc_browse_close.rc_handle = p_msg->handle;
2151 rc_browse_close.peer_addr = p_msg->peer_addr;
2153 tBTA_AV bta_av_data;
2154 bta_av_data.rc_browse_close = rc_browse_close;
2155 (*p_cb->p_cback)(BTA_AV_RC_BROWSE_CLOSE_EVT, &bta_av_data);
2158 /*******************************************************************************
2160 * Function bta_av_rc_disc
2162 * Description start AVRC SDP discovery.
2166 ******************************************************************************/
2167 void bta_av_rc_disc(uint8_t disc) {
2168 tBTA_AV_CB* p_cb = &bta_av_cb;
2169 tAVRC_SDP_DB_PARAMS db_params;
2170 uint16_t attr_list[] = {ATTR_ID_SERVICE_CLASS_ID_LIST,
2171 ATTR_ID_BT_PROFILE_DESC_LIST,
2172 ATTR_ID_SUPPORTED_FEATURES,
2173 ATTR_ID_ADDITION_PROTO_DESC_LISTS};
2176 RawAddress peer_addr = RawAddress::kEmpty;
2179 APPL_TRACE_DEBUG("%s: disc: 0x%x, bta_av_cb.disc: 0x%x", __func__, disc,
2181 if ((bta_av_cb.disc != 0) || (disc == 0)) return;
2183 if ((disc & BTA_AV_CHNL_MSK) == BTA_AV_CHNL_MSK) {
2184 /* this is the rc handle/index to tBTA_AV_RCB */
2185 rc_handle = disc & (~BTA_AV_CHNL_MSK);
2186 if (p_cb->rcb[rc_handle].lidx) {
2187 peer_addr = p_cb->lcb[p_cb->rcb[rc_handle].lidx - 1].addr;
2190 hdi = (disc & BTA_AV_HNDL_MSK) - 1;
2191 p_scb = p_cb->p_scb[hdi];
2194 APPL_TRACE_DEBUG("%s: rc_handle %d", __func__, p_scb->rc_handle);
2195 peer_addr = p_scb->PeerAddress();
2199 if (!peer_addr.IsEmpty()) {
2200 /* allocate discovery database */
2201 if (p_cb->p_disc_db == NULL)
2202 p_cb->p_disc_db = (tSDP_DISCOVERY_DB*)osi_malloc(BTA_AV_DISC_BUF_SIZE);
2204 /* set up parameters */
2205 db_params.db_len = BTA_AV_DISC_BUF_SIZE;
2206 db_params.num_attr = sizeof(attr_list) / sizeof(uint16_t);
2207 db_params.p_db = p_cb->p_disc_db;
2208 db_params.p_attrs = attr_list;
2210 /* searching for UUID_SERVCLASS_AV_REMOTE_CONTROL gets both TG and CT */
2211 if (AVRC_FindService(UUID_SERVCLASS_AV_REMOTE_CONTROL, peer_addr,
2213 base::Bind(bta_av_avrc_sdp_cback)) == AVRC_SUCCESS) {
2215 APPL_TRACE_DEBUG("%s: disc 0x%x", __func__, p_cb->disc);
2220 /*******************************************************************************
2222 * Function bta_av_dereg_comp
2224 * Description deregister complete. free the stream control block.
2228 ******************************************************************************/
2229 void bta_av_dereg_comp(tBTA_AV_DATA* p_data) {
2230 tBTA_AV_CB* p_cb = &bta_av_cb;
2236 /* find the stream control block */
2237 p_scb = bta_av_hndl_to_scb(p_data->hdr.layer_specific);
2240 APPL_TRACE_DEBUG("%s: deregistered %d(h%d)", __func__, p_scb->chnl,
2242 mask = BTA_AV_HNDL_TO_MSK(p_scb->hdi);
2243 p_cb->reg_audio &= ~mask;
2244 if ((p_cb->conn_audio & mask) && p_cb->audio_open_cnt) {
2245 /* this channel is still marked as open. decrease the count */
2246 p_cb->audio_open_cnt--;
2248 p_cb->conn_audio &= ~mask;
2250 if (p_scb->q_tag == BTA_AV_Q_TAG_STREAM && p_scb->a2dp_list) {
2251 /* make sure no buffers are in a2dp_list */
2252 while (!list_is_empty(p_scb->a2dp_list)) {
2253 p_buf = (BT_HDR*)list_front(p_scb->a2dp_list);
2254 list_remove(p_scb->a2dp_list, p_buf);
2259 /* remove the A2DP SDP record, if no more audio stream is left */
2260 if (!p_cb->reg_audio) {
2262 /* Only remove the SDP record if we're the ones that created it */
2263 if (is_new_avrcp_enabled()) {
2264 APPL_TRACE_DEBUG("%s: newavrcp is the owner of the AVRCP Target SDP "
2265 "record. Don't dereg the SDP record", __func__);
2267 APPL_TRACE_DEBUG("%s: newavrcp is not enabled. Remove SDP record",
2269 bta_ar_dereg_avrc(UUID_SERVCLASS_AV_REMOTE_CONTROL);
2272 if (p_cb->sdp_a2dp_handle) {
2273 bta_av_del_sdp_rec(&p_cb->sdp_a2dp_handle);
2274 p_cb->sdp_a2dp_handle = 0;
2275 bta_sys_remove_uuid(UUID_SERVCLASS_AUDIO_SOURCE);
2278 #if (BTA_AV_SINK_INCLUDED == TRUE)
2279 if (p_cb->sdp_a2dp_snk_handle) {
2280 bta_av_del_sdp_rec(&p_cb->sdp_a2dp_snk_handle);
2281 p_cb->sdp_a2dp_snk_handle = 0;
2282 bta_sys_remove_uuid(UUID_SERVCLASS_AUDIO_SINK);
2287 bta_av_free_scb(p_scb);
2290 APPL_TRACE_DEBUG("%s: audio 0x%x, disable:%d", __func__, p_cb->reg_audio,
2292 /* if no stream control block is active */
2293 if (p_cb->reg_audio == 0) {
2294 /* deregister from AVDT */
2295 bta_ar_dereg_avdt();
2297 /* deregister from AVCT */
2298 bta_ar_dereg_avrc(UUID_SERVCLASS_AV_REM_CTRL_TARGET);
2299 bta_ar_dereg_avct();
2301 if (p_cb->disabling) {
2302 p_cb->disabling = false;
2303 // reset enabling parameters
2308 /* Clear the Capturing service class bit */
2309 cod.service = BTM_COD_SERVICE_CAPTURING;
2310 utl_set_device_class(&cod, BTA_UTL_CLR_COD_SERVICE_CLASS);