1 /******************************************************************************
3 * Copyright (C) 2004-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 file contains action functions for advanced audio/video main state
24 ******************************************************************************/
26 #include "bt_target.h"
27 #if defined(BTA_AV_INCLUDED) && (BTA_AV_INCLUDED == TRUE)
30 #include "bta_av_api.h"
31 #include "bta_av_int.h"
37 #if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
38 #include "bta_ar_api.h"
41 /*****************************************************************************
43 *****************************************************************************/
44 /* the timer in milliseconds to wait for open req after setconfig for incoming connections */
45 #ifndef BTA_AV_SIG_TIME_VAL
46 #define BTA_AV_SIG_TIME_VAL 8000
49 /* In millisec to wait for signalling from SNK when it is initiated from SNK. */
50 /* If not, we will start signalling from SRC. */
51 #ifndef BTA_AV_ACP_SIG_TIME_VAL
52 #define BTA_AV_ACP_SIG_TIME_VAL 2000
55 static void bta_av_acp_sig_timer_cback (TIMER_LIST_ENT *p_tle);
57 /*******************************************************************************
59 ** Function bta_av_get_rcb_by_shdl
61 ** Description find the RCB associated with the given SCB handle.
63 ** Returns tBTA_AV_RCB
65 *******************************************************************************/
66 tBTA_AV_RCB * bta_av_get_rcb_by_shdl(UINT8 shdl)
68 tBTA_AV_RCB *p_rcb = NULL;
71 for (i=0; i<BTA_AV_NUM_RCB; i++)
73 if (bta_av_cb.rcb[i].shdl == shdl && bta_av_cb.rcb[i].handle != BTA_AV_RC_HANDLE_NONE)
75 p_rcb = &bta_av_cb.rcb[i];
81 #define BTA_AV_STS_NO_RSP 0xFF /* a number not used by tAVRC_STS */
83 /*******************************************************************************
85 ** Function bta_av_del_rc
87 ** Description delete the given AVRC handle.
91 *******************************************************************************/
92 void bta_av_del_rc(tBTA_AV_RCB *p_rcb)
95 UINT8 rc_handle; /* connected AVRCP handle */
97 if(p_rcb->handle != BTA_AV_RC_HANDLE_NONE)
101 p_scb = bta_av_cb.p_scb[p_rcb->shdl - 1];
104 APPL_TRACE_DEBUG("bta_av_del_rc shdl:%d, srch:%d rc_handle:%d", p_rcb->shdl,
105 p_scb->rc_handle, p_rcb->handle);
106 if(p_scb->rc_handle == p_rcb->handle)
107 p_scb->rc_handle = BTA_AV_RC_HANDLE_NONE;
108 /* just in case the RC timer is active
109 if(bta_av_cb.features & BTA_AV_FEAT_RCCT && p_scb->chnl == BTA_AV_CHNL_AUDIO) */
110 bta_sys_stop_timer(&p_scb->timer);
114 APPL_TRACE_EVENT("bta_av_del_rc handle: %d status=0x%x, rc_acp_handle:%d, idx:%d",
115 p_rcb->handle, p_rcb->status, bta_av_cb.rc_acp_handle, bta_av_cb.rc_acp_idx);
116 rc_handle = p_rcb->handle;
117 if(!(p_rcb->status & BTA_AV_RC_CONN_MASK) ||
118 ((p_rcb->status & BTA_AV_RC_ROLE_MASK) == BTA_AV_RC_ROLE_INT) )
121 p_rcb->handle = BTA_AV_RC_HANDLE_NONE;
125 /* else ACP && connected. do not clear the handle yet */
126 AVRC_Close(rc_handle);
127 if (rc_handle == bta_av_cb.rc_acp_handle)
128 bta_av_cb.rc_acp_handle = BTA_AV_RC_HANDLE_NONE;
129 APPL_TRACE_EVENT("end del_rc handle: %d status=0x%x, rc_acp_handle:%d, lidx:%d",
130 p_rcb->handle, p_rcb->status, bta_av_cb.rc_acp_handle, p_rcb->lidx);
135 /*******************************************************************************
137 ** Function bta_av_close_all_rc
139 ** Description close the all AVRC handle.
143 *******************************************************************************/
144 static void bta_av_close_all_rc(tBTA_AV_CB *p_cb)
148 for(i=0; i<BTA_AV_NUM_RCB; i++)
150 if ((p_cb->disabling == TRUE) || (bta_av_cb.rcb[i].shdl != 0))
151 bta_av_del_rc(&bta_av_cb.rcb[i]);
155 /*******************************************************************************
157 ** Function bta_av_del_sdp_rec
159 ** Description delete the given SDP record handle.
163 *******************************************************************************/
164 static void bta_av_del_sdp_rec(UINT32 *p_sdp_handle)
166 if(*p_sdp_handle != 0)
168 SDP_DeleteRecord(*p_sdp_handle);
173 /*******************************************************************************
175 ** Function bta_av_avrc_sdp_cback
177 ** Description AVRCP service discovery callback.
181 *******************************************************************************/
182 static void bta_av_avrc_sdp_cback(UINT16 status)
187 if ((p_msg = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
189 p_msg->event = BTA_AV_SDP_AVRC_DISC_EVT;
190 bta_sys_sendmsg(p_msg);
194 /*******************************************************************************
196 ** Function bta_av_rc_ctrl_cback
198 ** Description AVRCP control callback.
202 *******************************************************************************/
203 static void bta_av_rc_ctrl_cback(UINT8 handle, UINT8 event, UINT16 result, BD_ADDR peer_addr)
205 tBTA_AV_RC_CONN_CHG *p_msg;
206 UINT16 msg_event = 0;
209 #if (defined(BTA_AV_MIN_DEBUG_TRACES) && BTA_AV_MIN_DEBUG_TRACES == TRUE)
210 APPL_TRACE_EVENT("rc_ctrl handle: %d event=0x%x", handle, event);
212 APPL_TRACE_EVENT("bta_av_rc_ctrl_cback handle: %d event=0x%x", handle, event);
214 if (event == AVRC_OPEN_IND_EVT)
216 /* save handle of opened connection
217 bta_av_cb.rc_handle = handle;*/
219 msg_event = BTA_AV_AVRC_OPEN_EVT;
221 else if (event == AVRC_CLOSE_IND_EVT)
223 msg_event = BTA_AV_AVRC_CLOSE_EVT;
228 if ((p_msg = (tBTA_AV_RC_CONN_CHG *) GKI_getbuf(sizeof(tBTA_AV_RC_CONN_CHG))) != NULL)
230 p_msg->hdr.event = msg_event;
231 p_msg->handle = handle;
233 bdcpy(p_msg->peer_addr, peer_addr);
234 bta_sys_sendmsg(p_msg);
239 /*******************************************************************************
241 ** Function bta_av_rc_msg_cback
243 ** Description AVRCP message callback.
247 *******************************************************************************/
248 static void bta_av_rc_msg_cback(UINT8 handle, UINT8 label, UINT8 opcode, tAVRC_MSG *p_msg)
250 tBTA_AV_RC_MSG *p_buf;
251 UINT8 *p_data = NULL;
252 UINT8 **p_p_data = NULL;
255 #if (defined(BTA_AV_MIN_DEBUG_TRACES) && BTA_AV_MIN_DEBUG_TRACES == TRUE)
256 APPL_TRACE_ERROR("rc_msg handle: %d opcode=0x%x", handle, opcode);
258 APPL_TRACE_EVENT("bta_av_rc_msg_cback handle: %d opcode=0x%x", handle, opcode);
260 /* determine size of buffer we need */
261 if (opcode == AVRC_OP_VENDOR && p_msg->vendor.p_vendor_data != NULL)
263 p_data = p_msg->vendor.p_vendor_data;
264 p_p_data = &p_msg->vendor.p_vendor_data;
265 data_len = (UINT16) p_msg->vendor.vendor_len;
267 else if (opcode == AVRC_OP_PASS_THRU && p_msg->pass.p_pass_data != NULL)
269 p_data = p_msg->pass.p_pass_data;
270 p_p_data = &p_msg->pass.p_pass_data;
271 data_len = (UINT16) p_msg->pass.pass_len;
274 if ((p_buf = (tBTA_AV_RC_MSG *) GKI_getbuf((UINT16) (sizeof(tBTA_AV_RC_MSG) + data_len))) != NULL)
276 p_buf->hdr.event = BTA_AV_AVRC_MSG_EVT;
277 p_buf->handle = handle;
278 p_buf->label = label;
279 p_buf->opcode = opcode;
280 memcpy(&p_buf->msg, p_msg, sizeof(tAVRC_MSG));
283 memcpy((UINT8 *)(p_buf + 1), p_data, data_len);
284 *p_p_data = (UINT8 *)(p_buf + 1);
286 bta_sys_sendmsg(p_buf);
290 /*******************************************************************************
292 ** Function bta_av_rc_create
294 ** Description alloc RCB and call AVRC_Open
296 ** Returns the created rc handle
298 *******************************************************************************/
299 UINT8 bta_av_rc_create(tBTA_AV_CB *p_cb, UINT8 role, UINT8 shdl, UINT8 lidx)
302 BD_ADDR_PTR bda = (BD_ADDR_PTR)bd_addr_any;
303 UINT8 status = BTA_AV_RC_ROLE_ACP;
304 tBTA_AV_SCB *p_scb = p_cb->p_scb[shdl - 1];
311 bda = p_scb->peer_addr;
312 status = BTA_AV_RC_ROLE_INT;
316 if ((p_rcb = bta_av_get_rcb_by_shdl(shdl)) != NULL )
318 APPL_TRACE_ERROR("bta_av_rc_create ACP handle exist for shdl:%d", shdl);
319 return p_rcb->handle;
323 ccb.p_ctrl_cback = bta_av_rc_ctrl_cback;
324 ccb.p_msg_cback = bta_av_rc_msg_cback;
325 ccb.company_id = p_bta_av_cfg->company_id;
327 /* note: BTA_AV_FEAT_RCTG = AVRC_CT_TARGET, BTA_AV_FEAT_RCCT = AVRC_CT_CONTROL */
328 ccb.control = p_cb->features & (BTA_AV_FEAT_RCTG | BTA_AV_FEAT_RCCT | AVRC_CT_PASSIVE);
331 if (AVRC_Open(&rc_handle, &ccb, bda) != AVRC_SUCCESS)
332 return BTA_AV_RC_HANDLE_NONE;
335 p_rcb = &p_cb->rcb[i];
337 if (p_rcb->handle != BTA_AV_RC_HANDLE_NONE)
339 APPL_TRACE_ERROR("bta_av_rc_create found duplicated handle:%d", rc_handle);
342 p_rcb->handle = rc_handle;
343 p_rcb->status = status;
346 p_rcb->peer_features = 0;
347 if(lidx == (BTA_AV_NUM_LINKS + 1))
349 /* this LIDX is reserved for the AVRCP ACP connection */
350 p_cb->rc_acp_handle = p_rcb->handle;
351 p_cb->rc_acp_idx = (i + 1);
352 APPL_TRACE_DEBUG("rc_acp_handle:%d idx:%d", p_cb->rc_acp_handle, p_cb->rc_acp_idx);
354 APPL_TRACE_DEBUG("create %d, role: %d, shdl:%d, rc_handle:%d, lidx:%d, status:0x%x",
355 i, role, shdl, p_rcb->handle, lidx, p_rcb->status);
360 /*******************************************************************************
362 ** Function bta_av_valid_group_navi_msg
364 ** Description Check if it is Group Navigation Msg for Metadata
366 ** Returns BTA_AV_RSP_ACCEPT or BTA_AV_RSP_NOT_IMPL.
368 *******************************************************************************/
369 static tBTA_AV_CODE bta_av_group_navi_supported(UINT8 len, UINT8 *p_data, BOOLEAN is_inquiry)
371 tBTA_AV_CODE ret=BTA_AV_RSP_NOT_IMPL;
372 UINT8 *p_ptr = p_data;
376 if (p_bta_av_cfg->avrc_group && len == BTA_GROUP_NAVI_MSG_OP_DATA_LEN)
378 BTA_AV_BE_STREAM_TO_CO_ID(u32, p_ptr);
379 BE_STREAM_TO_UINT16(u16, p_ptr);
381 if (u32 == AVRC_CO_METADATA)
385 if (u16 <= AVRC_PDU_PREV_GROUP)
386 ret = BTA_AV_RSP_IMPL_STBL;
390 if (u16 <= AVRC_PDU_PREV_GROUP)
391 ret = BTA_AV_RSP_ACCEPT;
393 ret = BTA_AV_RSP_REJ;
401 /*******************************************************************************
403 ** Function bta_av_op_supported
405 ** Description Check if remote control operation is supported.
407 ** Returns BTA_AV_RSP_ACCEPT of supported, BTA_AV_RSP_NOT_IMPL if not.
409 *******************************************************************************/
410 static tBTA_AV_CODE bta_av_op_supported(tBTA_AV_RC rc_id, BOOLEAN is_inquiry)
412 tBTA_AV_CODE ret_code = BTA_AV_RSP_NOT_IMPL;
418 if (p_bta_av_rc_id[rc_id >> 4] & (1 << (rc_id & 0x0F)))
420 ret_code = BTA_AV_RSP_IMPL_STBL;
425 if (p_bta_av_rc_id[rc_id >> 4] & (1 << (rc_id & 0x0F)))
427 ret_code = BTA_AV_RSP_ACCEPT;
429 else if ((p_bta_av_cfg->rc_pass_rsp == BTA_AV_RSP_INTERIM) && p_bta_av_rc_id_ac)
431 if (p_bta_av_rc_id_ac[rc_id >> 4] & (1 << (rc_id & 0x0F)))
433 ret_code = BTA_AV_RSP_INTERIM;
442 /*******************************************************************************
444 ** Function bta_av_find_lcb
446 ** Description Given BD_addr, find the associated LCB.
448 ** Returns NULL, if not found.
450 *******************************************************************************/
451 tBTA_AV_LCB * bta_av_find_lcb(BD_ADDR addr, UINT8 op)
453 tBTA_AV_CB *p_cb = &bta_av_cb;
456 tBTA_AV_LCB *p_lcb = NULL;
458 for(xx=0; xx<BTA_AV_NUM_LINKS; xx++)
460 mask = 1 << xx; /* the used mask for this lcb */
461 if((mask & p_cb->conn_lcb) && 0 ==( bdcmp(p_cb->lcb[xx].addr, addr)))
463 p_lcb = &p_cb->lcb[xx];
464 if(op == BTA_AV_LCB_FREE)
466 p_cb->conn_lcb &= ~mask; /* clear the connect mask */
467 APPL_TRACE_DEBUG("conn_lcb: 0x%x", p_cb->conn_lcb);
475 /*******************************************************************************
477 ** Function bta_av_rc_opened
479 ** Description Set AVRCP state to opened.
483 *******************************************************************************/
484 void bta_av_rc_opened(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
486 tBTA_AV_RC_OPEN rc_open;
495 /* find the SCB & stop the timer */
496 for(i=0; i<BTA_AV_NUM_STRS; i++)
498 p_scb = p_cb->p_scb[i];
499 if(p_scb && bdcmp(p_scb->peer_addr, p_data->rc_conn_chg.peer_addr) == 0)
501 p_scb->rc_handle = p_data->rc_conn_chg.handle;
502 APPL_TRACE_DEBUG("bta_av_rc_opened shdl:%d, srch %d", i + 1, p_scb->rc_handle);
504 APPL_TRACE_ERROR("use_rc:%d", p_scb->use_rc);
505 bta_sys_stop_timer(&p_scb->timer);
511 i = p_data->rc_conn_chg.handle;
512 if (p_cb->rcb[i].handle == BTA_AV_RC_HANDLE_NONE)
514 APPL_TRACE_ERROR("not a valid handle:%d any more", i);
519 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);
526 p_rcb->shdl = p_cb->rcb[i].shdl;
528 p_rcb->lidx = p_cb->rcb[i].lidx;
529 p_cb->rcb[i].lidx = tmp;
530 p_cb->rc_acp_handle = p_rcb->handle;
531 p_cb->rc_acp_idx = (p_rcb - p_cb->rcb) + 1;
532 APPL_TRACE_DEBUG("switching RCB rc_acp_handle:%d idx:%d",
533 p_cb->rc_acp_handle, p_cb->rc_acp_idx);
537 p_cb->rcb[i].shdl = shdl;
538 rc_open.rc_handle = i;
539 APPL_TRACE_ERROR("bta_av_rc_opened rcb[%d] shdl:%d lidx:%d/%d",
540 i, shdl, p_cb->rcb[i].lidx, p_cb->lcb[BTA_AV_NUM_LINKS].lidx);
541 p_cb->rcb[i].status |= BTA_AV_RC_CONN_MASK;
543 if(!shdl && 0 == p_cb->lcb[BTA_AV_NUM_LINKS].lidx)
545 /* no associated SCB -> connected to an RC only device
546 * update the index to the extra LCB */
547 p_lcb = &p_cb->lcb[BTA_AV_NUM_LINKS];
548 bdcpy(p_lcb->addr, p_data->rc_conn_chg.peer_addr);
549 APPL_TRACE_DEBUG("rc_only bd_addr:%02x-%02x-%02x-%02x-%02x-%02x",
550 p_lcb->addr[0], p_lcb->addr[1],
551 p_lcb->addr[2], p_lcb->addr[3],
552 p_lcb->addr[4], p_lcb->addr[5]);
553 p_lcb->lidx = BTA_AV_NUM_LINKS + 1;
554 p_cb->rcb[i].lidx = p_lcb->lidx;
556 APPL_TRACE_ERROR("rcb[%d].lidx=%d, lcb.conn_msk=x%x",
557 i, p_cb->rcb[i].lidx, p_lcb->conn_msk);
558 disc = p_data->rc_conn_chg.handle|BTA_AV_CHNL_MSK;
561 bdcpy(rc_open.peer_addr, p_data->rc_conn_chg.peer_addr);
562 rc_open.peer_features = p_cb->rcb[i].peer_features;
563 rc_open.status = BTA_AV_SUCCESS;
564 APPL_TRACE_DEBUG("local features:x%x peer_features:x%x", p_cb->features,
565 rc_open.peer_features);
566 if(rc_open.peer_features == 0)
568 /* we have not done SDP on peer RC capabilities.
569 * peer must have initiated the RC connection */
570 rc_open.peer_features = BTA_AV_FEAT_RCCT;
571 bta_av_rc_disc(disc);
573 (*p_cb->p_cback)(BTA_AV_RC_OPEN_EVT, (tBTA_AV *) &rc_open);
578 /*******************************************************************************
580 ** Function bta_av_rc_remote_cmd
582 ** Description Send an AVRCP remote control command.
586 *******************************************************************************/
587 void bta_av_rc_remote_cmd(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
590 if (p_cb->features & BTA_AV_FEAT_RCCT)
592 if(p_data->hdr.layer_specific < BTA_AV_NUM_RCB)
594 p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
595 if(p_rcb->status & BTA_AV_RC_CONN_MASK)
597 AVRC_PassCmd(p_rcb->handle, p_data->api_remote_cmd.label,
598 &p_data->api_remote_cmd.msg);
604 /*******************************************************************************
606 ** Function bta_av_rc_vendor_cmd
608 ** Description Send an AVRCP vendor specific command.
612 *******************************************************************************/
613 void bta_av_rc_vendor_cmd(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
616 if ( (p_cb->features & (BTA_AV_FEAT_RCCT | BTA_AV_FEAT_VENDOR)) ==
617 (BTA_AV_FEAT_RCCT | BTA_AV_FEAT_VENDOR))
619 if(p_data->hdr.layer_specific < BTA_AV_NUM_RCB)
621 p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
622 AVRC_VendorCmd(p_rcb->handle, p_data->api_vendor.label, &p_data->api_vendor.msg);
627 /*******************************************************************************
629 ** Function bta_av_rc_vendor_rsp
631 ** Description Send an AVRCP vendor specific response.
635 *******************************************************************************/
636 void bta_av_rc_vendor_rsp(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
639 if ( (p_cb->features & (BTA_AV_FEAT_RCTG | BTA_AV_FEAT_VENDOR)) ==
640 (BTA_AV_FEAT_RCTG | BTA_AV_FEAT_VENDOR))
642 if(p_data->hdr.layer_specific < BTA_AV_NUM_RCB)
644 p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
645 AVRC_VendorRsp(p_rcb->handle, p_data->api_vendor.label, &p_data->api_vendor.msg);
650 /*******************************************************************************
652 ** Function bta_av_rc_meta_rsp
654 ** Description Send an AVRCP metadata/advanced control command/response.
658 *******************************************************************************/
659 void bta_av_rc_meta_rsp(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
664 if ((p_cb->features & BTA_AV_FEAT_METADATA) && (p_data->hdr.layer_specific < BTA_AV_NUM_RCB))
666 if ((p_data->api_meta_rsp.is_rsp && (p_cb->features & BTA_AV_FEAT_RCTG)) ||
667 (!p_data->api_meta_rsp.is_rsp && (p_cb->features & BTA_AV_FEAT_RCCT)) )
669 p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
670 if (p_rcb->handle != BTA_AV_RC_HANDLE_NONE) {
671 AVRC_MsgReq(p_rcb->handle, p_data->api_meta_rsp.label,
672 p_data->api_meta_rsp.rsp_code,
673 p_data->api_meta_rsp.p_pkt);
680 GKI_freebuf (p_data->api_meta_rsp.p_pkt);
683 /*******************************************************************************
685 ** Function bta_av_rc_free_rsp
687 ** Description free an AVRCP metadata command buffer.
691 *******************************************************************************/
692 void bta_av_rc_free_rsp (tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
696 GKI_freebuf (p_data->api_meta_rsp.p_pkt);
699 /*******************************************************************************
701 ** Function bta_av_rc_meta_req
703 ** Description Send an AVRCP metadata command.
707 *******************************************************************************/
708 void bta_av_rc_free_msg (tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
716 /*******************************************************************************
718 ** Function bta_av_chk_notif_evt_id
720 ** Description make sure the requested player id is valid.
722 ** Returns BTA_AV_STS_NO_RSP, if no error
724 *******************************************************************************/
725 static tAVRC_STS bta_av_chk_notif_evt_id(tAVRC_MSG_VENDOR *p_vendor)
727 tAVRC_STS status = BTA_AV_STS_NO_RSP;
730 UINT8 *p = p_vendor->p_vendor_data + 2;
732 BE_STREAM_TO_UINT16 (u16, p);
733 /* double check the fixed length */
734 if ((u16 != 5) || (p_vendor->vendor_len != 9))
736 status = AVRC_STS_INTERNAL_ERR;
740 /* make sure the player_id is valid */
741 for (xx=0; xx<p_bta_av_cfg->num_evt_ids; xx++)
743 if (*p == p_bta_av_cfg->p_meta_evt_ids[xx])
748 if (xx == p_bta_av_cfg->num_evt_ids)
750 status = AVRC_STS_BAD_PARAM;
757 /*******************************************************************************
759 ** Function bta_av_proc_meta_cmd
761 ** Description Process an AVRCP metadata command from the peer.
763 ** Returns TRUE to respond immediately
765 *******************************************************************************/
766 tBTA_AV_EVT bta_av_proc_meta_cmd(tAVRC_RESPONSE *p_rc_rsp, tBTA_AV_RC_MSG *p_msg, UINT8 *p_ctype)
768 tBTA_AV_EVT evt = BTA_AV_META_MSG_EVT;
771 tAVRC_MSG_VENDOR *p_vendor = &p_msg->msg.vendor;
773 #if (AVRC_METADATA_INCLUDED == TRUE)
775 pdu = *(p_vendor->p_vendor_data);
777 *p_ctype = AVRC_RSP_REJ;
778 /* Metadata messages only use PANEL sub-unit type */
779 if (p_vendor->hdr.subunit_type != AVRC_SUB_PANEL)
781 APPL_TRACE_DEBUG("SUBUNIT must be PANEL");
784 p_vendor->hdr.ctype = BTA_AV_RSP_NOT_IMPL;
785 AVRC_VendorRsp(p_msg->handle, p_msg->label, &p_msg->msg.vendor);
787 else if (!AVRC_IsValidAvcType(pdu, p_vendor->hdr.ctype) )
789 APPL_TRACE_DEBUG("Invalid pdu/ctype: 0x%x, %d", pdu, p_vendor->hdr.ctype);
790 /* reject invalid message without reporting to app */
792 p_rc_rsp->rsp.status = AVRC_STS_BAD_CMD;
798 case AVRC_PDU_GET_CAPABILITIES:
799 /* process GetCapabilities command without reporting the event to app */
801 u8 = *(p_vendor->p_vendor_data + 4);
802 p = p_vendor->p_vendor_data + 2;
803 p_rc_rsp->get_caps.capability_id = u8;
804 BE_STREAM_TO_UINT16 (u16, p);
805 if ((u16 != 1) || (p_vendor->vendor_len != 5))
807 p_rc_rsp->get_caps.status = AVRC_STS_INTERNAL_ERR;
811 p_rc_rsp->get_caps.status = AVRC_STS_NO_ERROR;
812 if (u8 == AVRC_CAP_COMPANY_ID)
814 *p_ctype = AVRC_RSP_IMPL_STBL;
815 p_rc_rsp->get_caps.count = p_bta_av_cfg->num_co_ids;
816 memcpy(p_rc_rsp->get_caps.param.company_id, p_bta_av_cfg->p_meta_co_ids,
817 (p_bta_av_cfg->num_co_ids << 2));
819 else if (u8 == AVRC_CAP_EVENTS_SUPPORTED)
821 *p_ctype = AVRC_RSP_IMPL_STBL;
822 p_rc_rsp->get_caps.count = p_bta_av_cfg->num_evt_ids;
823 memcpy(p_rc_rsp->get_caps.param.event_id, p_bta_av_cfg->p_meta_evt_ids,
824 p_bta_av_cfg->num_evt_ids);
828 APPL_TRACE_DEBUG("Invalid capability ID: 0x%x", u8);
829 /* reject - unknown capability ID */
830 p_rc_rsp->get_caps.status = AVRC_STS_BAD_PARAM;
836 case AVRC_PDU_REGISTER_NOTIFICATION:
837 /* make sure the event_id is implemented */
838 p_rc_rsp->rsp.status = bta_av_chk_notif_evt_id (p_vendor);
839 if (p_rc_rsp->rsp.status != BTA_AV_STS_NO_RSP)
846 APPL_TRACE_DEBUG("AVRCP 1.3 Metadata not supporteed. Reject command.");
847 /* reject invalid message without reporting to app */
849 p_rc_rsp->rsp.status = AVRC_STS_BAD_CMD;
856 /*******************************************************************************
858 ** Function bta_av_rc_msg
860 ** Description Process an AVRCP message from the peer.
864 *******************************************************************************/
865 void bta_av_rc_msg(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
869 BT_HDR *p_pkt = NULL;
870 tAVRC_MSG_VENDOR *p_vendor = &p_data->rc_msg.msg.vendor;
871 BOOLEAN is_inquiry = ((p_data->rc_msg.msg.hdr.ctype == AVRC_CMD_SPEC_INQ) || p_data->rc_msg.msg.hdr.ctype == AVRC_CMD_GEN_INQ);
872 #if (AVRC_METADATA_INCLUDED == TRUE)
875 tAVRC_RESPONSE rc_rsp;
877 rc_rsp.rsp.status = BTA_AV_STS_NO_RSP;
880 if (p_data->rc_msg.opcode == AVRC_OP_PASS_THRU)
882 /* if this is a pass thru command */
883 if ((p_data->rc_msg.msg.hdr.ctype == AVRC_CMD_CTRL) ||
884 (p_data->rc_msg.msg.hdr.ctype == AVRC_CMD_SPEC_INQ) ||
885 (p_data->rc_msg.msg.hdr.ctype == AVRC_CMD_GEN_INQ)
888 /* check if operation is supported */
889 if (p_data->rc_msg.msg.pass.op_id == AVRC_ID_VENDOR)
891 p_data->rc_msg.msg.hdr.ctype = BTA_AV_RSP_NOT_IMPL;
892 #if (AVRC_METADATA_INCLUDED == TRUE)
893 if (p_cb->features & BTA_AV_FEAT_METADATA)
894 p_data->rc_msg.msg.hdr.ctype =
895 bta_av_group_navi_supported(p_data->rc_msg.msg.pass.pass_len,
896 p_data->rc_msg.msg.pass.p_pass_data, is_inquiry);
901 p_data->rc_msg.msg.hdr.ctype = bta_av_op_supported(p_data->rc_msg.msg.pass.op_id, is_inquiry);
904 APPL_TRACE_DEBUG("ctype %d",p_data->rc_msg.msg.hdr.ctype)
907 if (p_data->rc_msg.msg.hdr.ctype != BTA_AV_RSP_INTERIM)
908 AVRC_PassRsp(p_data->rc_msg.handle, p_data->rc_msg.label, &p_data->rc_msg.msg.pass);
910 /* set up for callback if supported */
911 if (p_data->rc_msg.msg.hdr.ctype == BTA_AV_RSP_ACCEPT || p_data->rc_msg.msg.hdr.ctype == BTA_AV_RSP_INTERIM)
913 evt = BTA_AV_REMOTE_CMD_EVT;
914 av.remote_cmd.rc_id = p_data->rc_msg.msg.pass.op_id;
915 av.remote_cmd.key_state = p_data->rc_msg.msg.pass.state;
916 av.remote_cmd.p_data = p_data->rc_msg.msg.pass.p_pass_data;
917 av.remote_cmd.len = p_data->rc_msg.msg.pass.pass_len;
918 memcpy(&av.remote_cmd.hdr, &p_data->rc_msg.msg.hdr, sizeof (tAVRC_HDR));
919 av.remote_cmd.label = p_data->rc_msg.label;
922 /* else if this is a pass thru response */
923 else if (p_data->rc_msg.msg.hdr.ctype >= AVRC_RSP_ACCEPT)
925 /* set up for callback */
926 evt = BTA_AV_REMOTE_RSP_EVT;
927 av.remote_rsp.rc_id = p_data->rc_msg.msg.pass.op_id;
928 av.remote_rsp.key_state = p_data->rc_msg.msg.pass.state;
929 av.remote_rsp.rsp_code = p_data->rc_msg.msg.hdr.ctype;
930 av.remote_rsp.label = p_data->rc_msg.label;
932 /* must be a bad ctype -> reject*/
935 p_data->rc_msg.msg.hdr.ctype = BTA_AV_RSP_REJ;
936 AVRC_PassRsp(p_data->rc_msg.handle, p_data->rc_msg.label, &p_data->rc_msg.msg.pass);
939 /* else if this is a vendor specific command or response */
940 else if (p_data->rc_msg.opcode == AVRC_OP_VENDOR)
942 /* set up for callback */
943 av.vendor_cmd.code = p_data->rc_msg.msg.hdr.ctype;
944 av.vendor_cmd.company_id = p_vendor->company_id;
945 av.vendor_cmd.label = p_data->rc_msg.label;
946 av.vendor_cmd.p_data = p_vendor->p_vendor_data;
947 av.vendor_cmd.len = p_vendor->vendor_len;
949 /* if configured to support vendor specific and it's a command */
950 if ((p_cb->features & BTA_AV_FEAT_VENDOR) &&
951 p_data->rc_msg.msg.hdr.ctype <= AVRC_CMD_GEN_INQ)
953 #if (AVRC_METADATA_INCLUDED == TRUE)
954 if ((p_cb->features & BTA_AV_FEAT_METADATA) &&
955 (p_vendor->company_id == AVRC_CO_METADATA))
957 av.meta_msg.p_msg = &p_data->rc_msg.msg;
958 evt = bta_av_proc_meta_cmd (&rc_rsp, &p_data->rc_msg, &ctype);
962 evt = BTA_AV_VENDOR_CMD_EVT;
964 /* else if configured to support vendor specific and it's a response */
965 else if ((p_cb->features & BTA_AV_FEAT_VENDOR) &&
966 p_data->rc_msg.msg.hdr.ctype >= AVRC_RSP_ACCEPT)
968 #if (AVRC_METADATA_INCLUDED == TRUE)
969 if ((p_cb->features & BTA_AV_FEAT_METADATA) &&
970 (p_vendor->company_id == AVRC_CO_METADATA))
972 av.meta_msg.p_msg = &p_data->rc_msg.msg;
973 evt = BTA_AV_META_MSG_EVT;
977 evt = BTA_AV_VENDOR_RSP_EVT;
980 /* else if not configured to support vendor specific and it's a command */
981 else if (!(p_cb->features & BTA_AV_FEAT_VENDOR) &&
982 p_data->rc_msg.msg.hdr.ctype <= AVRC_CMD_GEN_INQ)
984 if(p_data->rc_msg.msg.vendor.p_vendor_data[0] == AVRC_PDU_INVALID)
987 p_data->rc_msg.msg.hdr.ctype = BTA_AV_RSP_REJ;
988 p_data->rc_msg.msg.vendor.p_vendor_data[4] = AVRC_STS_BAD_CMD;
991 p_data->rc_msg.msg.hdr.ctype = BTA_AV_RSP_NOT_IMPL;
992 AVRC_VendorRsp(p_data->rc_msg.handle, p_data->rc_msg.label, &p_data->rc_msg.msg.vendor);
995 #if (AVRC_METADATA_INCLUDED == TRUE)
996 if (evt == 0 && rc_rsp.rsp.status != BTA_AV_STS_NO_RSP)
1000 rc_rsp.rsp.opcode = p_data->rc_msg.opcode;
1001 res = AVRC_BldResponse (0, &rc_rsp, &p_pkt);
1004 AVRC_MsgReq (p_data->rc_msg.handle, p_data->rc_msg.label, ctype, p_pkt);
1011 av.remote_cmd.rc_handle = p_data->rc_msg.handle;
1012 (*p_cb->p_cback)(evt, &av);
1016 /*******************************************************************************
1018 ** Function bta_av_rc_close
1020 ** Description close the specified AVRC handle.
1024 *******************************************************************************/
1025 void bta_av_rc_close (tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
1027 UINT16 handle = p_data->hdr.layer_specific;
1031 if(handle < BTA_AV_NUM_RCB)
1033 p_rcb = &p_cb->rcb[handle];
1035 APPL_TRACE_DEBUG("bta_av_rc_close handle: %d, status=0x%x", p_rcb->handle, p_rcb->status);
1036 if(p_rcb->handle != BTA_AV_RC_HANDLE_NONE)
1040 p_scb = bta_av_cb.p_scb[p_rcb->shdl - 1];
1043 /* just in case the RC timer is active
1044 if(bta_av_cb.features & BTA_AV_FEAT_RCCT &&
1045 p_scb->chnl == BTA_AV_CHNL_AUDIO) */
1046 bta_sys_stop_timer(&p_scb->timer);
1050 AVRC_Close(p_rcb->handle);
1055 /*******************************************************************************
1057 ** Function bta_av_get_shdl
1059 ** Returns The index to p_scb[]
1061 *******************************************************************************/
1062 static UINT8 bta_av_get_shdl(tBTA_AV_SCB *p_scb)
1066 /* find the SCB & stop the timer */
1067 for(i=0; i<BTA_AV_NUM_STRS; i++)
1069 if(p_scb == bta_av_cb.p_scb[i])
1078 /*******************************************************************************
1080 ** Function bta_av_stream_chg
1082 ** Description audio streaming status changed.
1086 *******************************************************************************/
1087 void bta_av_stream_chg(tBTA_AV_SCB *p_scb, BOOLEAN started)
1092 BOOLEAN no_streams = FALSE;
1093 tBTA_AV_SCB *p_scbi;
1095 started_msk = BTA_AV_HNDL_TO_MSK(p_scb->hdi);
1096 APPL_TRACE_DEBUG ("bta_av_stream_chg started:%d started_msk:x%x chnl:x%x", started,
1097 started_msk, p_scb->chnl);
1098 if (BTA_AV_CHNL_AUDIO == p_scb->chnl)
1099 p_streams = &bta_av_cb.audio_streams;
1101 p_streams = &bta_av_cb.video_streams;
1105 /* Let L2CAP know this channel is processed with high priority */
1106 L2CA_SetAclPriority(p_scb->peer_addr, L2CAP_PRIORITY_HIGH);
1107 (*p_streams) |= started_msk;
1111 (*p_streams) &= ~started_msk;
1117 if (BTA_AV_CHNL_AUDIO == p_scb->chnl)
1119 if (bta_av_cb.video_streams == 0)
1125 if ( bta_av_cb.audio_streams )
1127 for (; i<BTA_AV_NUM_STRS; i++)
1129 p_scbi = bta_av_cb.p_scb[i];
1130 /* scb is used and started */
1131 if ( p_scbi && (bta_av_cb.audio_streams & BTA_AV_HNDL_TO_MSK(i))
1132 && bdcmp(p_scbi->peer_addr, p_scb->peer_addr) == 0)
1142 APPL_TRACE_DEBUG ("no_streams:%d i:%d, audio_streams:x%x, video_streams:x%x", no_streams, i,
1143 bta_av_cb.audio_streams, bta_av_cb.video_streams);
1146 /* Let L2CAP know this channel is processed with low priority */
1147 L2CA_SetAclPriority(p_scb->peer_addr, L2CAP_PRIORITY_NORMAL);
1153 /*******************************************************************************
1155 ** Function bta_av_conn_chg
1157 ** Description connetion status changed.
1158 ** Open an AVRCP acceptor channel, if new conn.
1162 *******************************************************************************/
1163 void bta_av_conn_chg(tBTA_AV_DATA *p_data)
1165 tBTA_AV_CB *p_cb = &bta_av_cb;
1167 tBTA_AV_SCB *p_scbi;
1172 int index = (p_data->hdr.layer_specific & BTA_AV_HNDL_MSK) - 1;
1174 tBTA_AV_LCB *p_lcb_rc;
1175 tBTA_AV_RCB *p_rcb, *p_rcb2;
1176 BOOLEAN chk_restore = FALSE;
1178 p_scb = p_cb->p_scb[index];
1180 mask = BTA_AV_HNDL_TO_MSK(index);
1181 p_lcb = bta_av_find_lcb(p_data->conn_chg.peer_addr, BTA_AV_LCB_FIND);
1182 conn_msk = 1 << (index + 1);
1183 if(p_data->conn_chg.is_up)
1185 /* set the conned mask for this channel */
1190 p_lcb->conn_msk |= conn_msk;
1191 for (i=0; i<BTA_AV_NUM_RCB; i++)
1193 if (bta_av_cb.rcb[i].lidx == p_lcb->lidx)
1195 bta_av_cb.rcb[i].shdl = index + 1;
1196 APPL_TRACE_DEBUG("conn_chg up[%d]: %d, status=0x%x, shdl:%d, lidx:%d", i,
1197 bta_av_cb.rcb[i].handle, bta_av_cb.rcb[i].status,
1198 bta_av_cb.rcb[i].shdl, bta_av_cb.rcb[i].lidx);
1203 if (p_scb->chnl == BTA_AV_CHNL_AUDIO)
1205 old_msk = p_cb->conn_audio;
1206 p_cb->conn_audio |= mask;
1210 old_msk = p_cb->conn_video;
1211 p_cb->conn_video |= mask;
1214 if ((old_msk & mask) == 0)
1216 /* increase the audio open count, if not set yet */
1217 bta_av_cb.audio_open_cnt++;
1221 APPL_TRACE_DEBUG("rc_acp_handle:%d rc_acp_idx:%d", p_cb->rc_acp_handle, p_cb->rc_acp_idx);
1222 /* check if the AVRCP ACP channel is already connected */
1223 if(p_lcb && p_cb->rc_acp_handle != BTA_AV_RC_HANDLE_NONE && p_cb->rc_acp_idx)
1225 p_lcb_rc = &p_cb->lcb[BTA_AV_NUM_LINKS];
1226 APPL_TRACE_DEBUG("rc_acp is connected && conn_chg on same addr p_lcb_rc->conn_msk:x%x",
1227 p_lcb_rc->conn_msk);
1228 /* check if the RC is connected to the scb addr */
1229 APPL_TRACE_DEBUG ("p_lcb_rc->addr: %02x:%02x:%02x:%02x:%02x:%02x",
1230 p_lcb_rc->addr[0], p_lcb_rc->addr[1], p_lcb_rc->addr[2], p_lcb_rc->addr[3],
1231 p_lcb_rc->addr[4], p_lcb_rc->addr[5]);
1232 APPL_TRACE_DEBUG ("conn_chg.peer_addr: %02x:%02x:%02x:%02x:%02x:%02x",
1233 p_data->conn_chg.peer_addr[0], p_data->conn_chg.peer_addr[1],
1234 p_data->conn_chg.peer_addr[2],
1235 p_data->conn_chg.peer_addr[3], p_data->conn_chg.peer_addr[4],
1236 p_data->conn_chg.peer_addr[5]);
1237 if (p_lcb_rc->conn_msk && bdcmp(p_lcb_rc->addr, p_data->conn_chg.peer_addr) == 0)
1239 /* AVRCP is already connected.
1240 * need to update the association betwen SCB and RCB */
1241 p_lcb_rc->conn_msk = 0; /* indicate RC ONLY is not connected */
1243 p_scb->rc_handle = p_cb->rc_acp_handle;
1244 p_rcb = &p_cb->rcb[p_cb->rc_acp_idx - 1];
1245 p_rcb->shdl = bta_av_get_shdl(p_scb);
1246 APPL_TRACE_DEBUG("update rc_acp shdl:%d/%d srch:%d", index + 1, p_rcb->shdl,
1249 p_rcb2 = bta_av_get_rcb_by_shdl(p_rcb->shdl);
1252 /* found the RCB that was created to associated with this SCB */
1253 p_cb->rc_acp_handle = p_rcb2->handle;
1254 p_cb->rc_acp_idx = (p_rcb2 - p_cb->rcb) + 1;
1255 APPL_TRACE_DEBUG("new rc_acp_handle:%d, idx:%d", p_cb->rc_acp_handle,
1257 p_rcb2->lidx = (BTA_AV_NUM_LINKS + 1);
1258 APPL_TRACE_DEBUG("rc2 handle:%d lidx:%d/%d",p_rcb2->handle, p_rcb2->lidx,
1259 p_cb->lcb[p_rcb2->lidx-1].lidx);
1261 p_rcb->lidx = p_lcb->lidx;
1262 APPL_TRACE_DEBUG("rc handle:%d lidx:%d/%d",p_rcb->handle, p_rcb->lidx,
1263 p_cb->lcb[p_rcb->lidx-1].lidx);
1270 if ((p_cb->conn_audio & mask) && bta_av_cb.audio_open_cnt)
1272 /* this channel is still marked as open. decrease the count */
1273 bta_av_cb.audio_open_cnt--;
1276 /* clear the conned mask for this channel */
1277 p_cb->conn_audio &= ~mask;
1278 p_cb->conn_video &= ~mask;
1281 /* the stream is closed.
1282 * clear the peer address, so it would not mess up the AVRCP for the next round of operation */
1283 bdcpy(p_scb->peer_addr, bd_addr_null);
1284 if(p_scb->chnl == BTA_AV_CHNL_AUDIO)
1288 p_lcb->conn_msk &= ~conn_msk;
1290 /* audio channel is down. make sure the INT channel is down */
1291 /* just in case the RC timer is active
1292 if(p_cb->features & BTA_AV_FEAT_RCCT) */
1294 bta_sys_stop_timer(&p_scb->timer);
1296 /* one audio channel goes down. check if we need to restore high priority */
1301 APPL_TRACE_DEBUG("bta_av_conn_chg shdl:%d", index + 1);
1302 for (i=0; i<BTA_AV_NUM_RCB; i++)
1304 APPL_TRACE_DEBUG("conn_chg dn[%d]: %d, status=0x%x, shdl:%d, lidx:%d", i,
1305 bta_av_cb.rcb[i].handle, bta_av_cb.rcb[i].status,
1306 bta_av_cb.rcb[i].shdl, bta_av_cb.rcb[i].lidx);
1307 if(bta_av_cb.rcb[i].shdl == index + 1)
1309 bta_av_del_rc(&bta_av_cb.rcb[i]);
1314 if(p_cb->conn_audio == 0 && p_cb->conn_video == 0)
1316 /* if both channels are not connected,
1317 * close all RC channels */
1318 bta_av_close_all_rc(p_cb);
1321 /* if the AVRCP is no longer listening, create the listening channel */
1322 if (bta_av_cb.rc_acp_handle == BTA_AV_RC_HANDLE_NONE && bta_av_cb.features & BTA_AV_FEAT_RCTG)
1323 bta_av_rc_create(&bta_av_cb, AVCT_ACP, 0, BTA_AV_NUM_LINKS + 1);
1326 APPL_TRACE_DEBUG("bta_av_conn_chg audio:%x video:%x up:%d conn_msk:0x%x chk_restore:%d audio_open_cnt:%d",
1327 p_cb->conn_audio, p_cb->conn_video, p_data->conn_chg.is_up, conn_msk, chk_restore, p_cb->audio_open_cnt);
1331 if (p_cb->audio_open_cnt == 1)
1333 /* one audio channel goes down and there's one audio channel remains open.
1334 * restore the switch role in default link policy */
1335 bta_sys_set_default_policy(BTA_ID_AV, HCI_ENABLE_MASTER_SLAVE_SWITCH);
1336 /* allow role switch, if this is the last connection */
1337 bta_av_restore_switch();
1339 if (p_cb->audio_open_cnt)
1341 /* adjust flush timeout settings to longer period */
1342 for (i=0; i<BTA_AV_NUM_STRS; i++)
1344 p_scbi = bta_av_cb.p_scb[i];
1345 if (p_scbi && p_scbi->chnl == BTA_AV_CHNL_AUDIO && p_scbi->co_started)
1347 /* may need to update the flush timeout of this already started stream */
1348 if (p_scbi->co_started != bta_av_cb.audio_open_cnt)
1350 p_scbi->co_started = bta_av_cb.audio_open_cnt;
1351 L2CA_SetFlushTimeout(p_scbi->peer_addr, p_bta_av_cfg->p_audio_flush_to[p_scbi->co_started - 1] );
1359 /*******************************************************************************
1361 ** Function bta_av_disable
1363 ** Description disable AV.
1367 *******************************************************************************/
1368 void bta_av_disable(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
1374 p_cb->disabling = TRUE;
1376 bta_av_close_all_rc(p_cb);
1378 utl_freebuf((void **) &p_cb->p_disc_db);
1380 /* disable audio/video - de-register all channels,
1381 * expect BTA_AV_DEREG_COMP_EVT when deregister is complete */
1382 for(xx=0; xx<BTA_AV_NUM_STRS; xx++)
1384 hdr.layer_specific = xx + 1;
1385 bta_av_api_deregister((tBTA_AV_DATA *)&hdr);
1389 /*******************************************************************************
1391 ** Function bta_av_api_disconnect
1397 *******************************************************************************/
1398 void bta_av_api_disconnect(tBTA_AV_DATA *p_data)
1400 AVDT_DisconnectReq(p_data->api_discnt.bd_addr, bta_av_conn_cback);
1401 bta_sys_stop_timer(&bta_av_cb.sig_tmr);
1404 /*******************************************************************************
1406 ** Function bta_av_sig_chg
1408 ** Description process AVDT signal channel up/down.
1412 *******************************************************************************/
1413 void bta_av_sig_chg(tBTA_AV_DATA *p_data)
1415 UINT16 event = p_data->str_msg.hdr.layer_specific;
1416 tBTA_AV_CB *p_cb = &bta_av_cb;
1419 tBTA_AV_LCB *p_lcb = NULL;
1421 APPL_TRACE_DEBUG("bta_av_sig_chg event: %d", event);
1422 if(event == AVDT_CONNECT_IND_EVT)
1424 p_lcb = bta_av_find_lcb(p_data->str_msg.bd_addr, BTA_AV_LCB_FIND);
1427 /* if the address does not have an LCB yet, alloc one */
1428 for(xx=0; xx<BTA_AV_NUM_LINKS; xx++)
1431 APPL_TRACE_DEBUG("conn_lcb: 0x%x", p_cb->conn_lcb);
1433 /* look for a p_lcb with its p_scb registered */
1434 if((!(mask & p_cb->conn_lcb)) && (p_cb->p_scb[xx] != NULL))
1436 p_lcb = &p_cb->lcb[xx];
1437 p_lcb->lidx = xx + 1;
1438 bdcpy(p_lcb->addr, p_data->str_msg.bd_addr);
1439 p_lcb->conn_msk = 0; /* clear the connect mask */
1440 /* start listening when the signal channel is open */
1441 if (p_cb->features & BTA_AV_FEAT_RCTG)
1443 bta_av_rc_create(p_cb, AVCT_ACP, 0, p_lcb->lidx);
1445 /* this entry is not used yet. */
1446 p_cb->conn_lcb |= mask; /* mark it as used */
1447 APPL_TRACE_DEBUG("start sig timer %d", p_data->hdr.offset);
1448 if (p_data->hdr.offset == AVDT_ACP)
1450 APPL_TRACE_DEBUG("Incoming L2CAP acquired, set state as incoming", NULL);
1451 bdcpy(p_cb->p_scb[xx]->peer_addr, p_data->str_msg.bd_addr);
1452 p_cb->p_scb[xx]->use_rc = TRUE; /* allowing RC for incoming connection */
1453 bta_av_ssm_execute(p_cb->p_scb[xx], BTA_AV_ACP_CONNECT_EVT, p_data);
1455 /* The Pending Event should be sent as soon as the L2CAP signalling channel
1456 * is set up, which is NOW. Earlier this was done only after
1457 * BTA_AV_SIG_TIME_VAL milliseconds.
1458 * The following function shall send the event and start the recurring timer
1460 bta_av_sig_timer(NULL);
1462 /* Possible collision : need to avoid outgoing processing while the timer is running */
1463 p_cb->p_scb[xx]->coll_mask = BTA_AV_COLL_INC_TMR;
1465 p_cb->acp_sig_tmr.param = (UINT32)xx;
1466 p_cb->acp_sig_tmr.p_cback = (TIMER_CBACK*)&bta_av_acp_sig_timer_cback;
1467 bta_sys_start_timer(&p_cb->acp_sig_tmr, 0, BTA_AV_ACP_SIG_TIME_VAL);
1473 /* check if we found something */
1474 if (xx == BTA_AV_NUM_LINKS)
1476 /* We do not have scb for this avdt connection. */
1477 /* Silently close the connection. */
1478 APPL_TRACE_ERROR("av scb not available for avdt connection");
1479 AVDT_DisconnectReq (p_data->str_msg.bd_addr, NULL);
1484 #if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
1485 else if (event == BTA_AR_AVDT_CONN_EVT)
1487 bta_sys_stop_timer(&bta_av_cb.sig_tmr);
1493 p_lcb = bta_av_find_lcb(p_data->str_msg.bd_addr, BTA_AV_LCB_FREE);
1494 if(p_lcb && p_lcb->conn_msk)
1496 APPL_TRACE_DEBUG("conn_msk: 0x%x", p_lcb->conn_msk);
1498 for(xx=0; xx < BTA_AV_NUM_STRS; xx++)
1500 mask = 1 << (xx + 1);
1501 if ((mask & p_lcb->conn_msk) && (p_cb->p_scb[xx]) &&
1502 (bdcmp(p_cb->p_scb[xx]->peer_addr, p_data->str_msg.bd_addr) == 0))
1504 bta_av_ssm_execute(p_cb->p_scb[xx], BTA_AV_AVDT_DISCONNECT_EVT, NULL);
1509 APPL_TRACE_DEBUG("conn_lcb: 0x%x", p_cb->conn_lcb);
1512 /*******************************************************************************
1514 ** Function bta_av_sig_timer
1516 ** Description process the signal channel timer. This timer is started
1517 ** when the AVDTP signal channel is connected. If no profile
1518 ** is connected, the timer goes off every BTA_AV_SIG_TIME_VAL
1522 *******************************************************************************/
1523 void bta_av_sig_timer(tBTA_AV_DATA *p_data)
1525 tBTA_AV_CB *p_cb = &bta_av_cb;
1528 tBTA_AV_LCB *p_lcb = NULL;
1532 APPL_TRACE_DEBUG("bta_av_sig_timer");
1533 for(xx=0; xx<BTA_AV_NUM_LINKS; xx++)
1536 if(mask & p_cb->conn_lcb)
1538 /* this entry is used. check if it is connected */
1539 p_lcb = &p_cb->lcb[xx];
1540 if(!p_lcb->conn_msk)
1542 bta_sys_start_timer(&p_cb->sig_tmr, BTA_AV_SIG_TIMER_EVT, BTA_AV_SIG_TIME_VAL);
1543 bdcpy(pend.bd_addr, p_lcb->addr);
1544 (*p_cb->p_cback)(BTA_AV_PENDING_EVT, (tBTA_AV *) &pend);
1550 /*******************************************************************************
1552 ** Function bta_av_acp_sig_timer_cback
1554 ** Description Process the timeout when SRC is accepting connection
1555 ** and SNK did not start signalling.
1559 *******************************************************************************/
1560 static void bta_av_acp_sig_timer_cback (TIMER_LIST_ENT *p_tle)
1562 UINT8 inx = (UINT8)p_tle->param;
1563 tBTA_AV_CB *p_cb = &bta_av_cb;
1564 tBTA_AV_SCB *p_scb = p_cb->p_scb[inx];
1565 tBTA_AV_API_OPEN *p_buf;
1569 APPL_TRACE_DEBUG("bta_av_acp_sig_timer_cback, coll_mask = 0x%02X", p_scb->coll_mask);
1571 if (p_scb->coll_mask & BTA_AV_COLL_INC_TMR)
1573 p_scb->coll_mask &= ~BTA_AV_COLL_INC_TMR;
1575 if (bta_av_is_scb_opening(p_scb))
1577 if (p_scb->p_disc_db)
1579 /* We are still doing SDP. Run the timer again. */
1580 p_scb->coll_mask |= BTA_AV_COLL_INC_TMR;
1582 p_cb->acp_sig_tmr.param = (UINT32)inx;
1583 p_cb->acp_sig_tmr.p_cback = (TIMER_CBACK *)&bta_av_acp_sig_timer_cback;
1584 bta_sys_start_timer(&p_cb->acp_sig_tmr, 0, BTA_AV_ACP_SIG_TIME_VAL);
1588 /* SNK did not start signalling, resume signalling process. */
1589 bta_av_discover_req (p_scb, NULL);
1592 else if (bta_av_is_scb_incoming(p_scb))
1594 /* Stay in incoming state if SNK does not start signalling */
1596 /* API open was called right after SNK opened L2C connection. */
1597 if (p_scb->coll_mask & BTA_AV_COLL_API_CALLED)
1599 p_scb->coll_mask &= ~BTA_AV_COLL_API_CALLED;
1601 /* BTA_AV_API_OPEN_EVT */
1602 if ((p_buf = (tBTA_AV_API_OPEN *) GKI_getbuf(sizeof(tBTA_AV_API_OPEN))) != NULL)
1604 memcpy(p_buf, &(p_scb->open_api), sizeof(tBTA_AV_API_OPEN));
1605 bta_sys_sendmsg(p_buf);
1613 /*******************************************************************************
1615 ** Function bta_av_check_peer_features
1617 ** Description check supported features on the peer device from the SDP record
1618 ** and return the feature mask
1620 ** Returns tBTA_AV_FEAT peer device feature mask
1622 *******************************************************************************/
1623 tBTA_AV_FEAT bta_av_check_peer_features (UINT16 service_uuid)
1625 tBTA_AV_FEAT peer_features = 0;
1626 tBTA_AV_CB *p_cb = &bta_av_cb;
1627 tSDP_DISC_REC *p_rec = NULL;
1628 tSDP_DISC_ATTR *p_attr;
1629 UINT16 peer_rc_version=0;
1630 UINT16 categories = 0;
1632 APPL_TRACE_DEBUG("bta_av_check_peer_features service_uuid:x%x", service_uuid);
1633 /* loop through all records we found */
1636 /* get next record; if none found, we're done */
1637 if ((p_rec = SDP_FindServiceInDb(p_cb->p_disc_db, service_uuid, p_rec)) == NULL)
1642 if (( SDP_FindAttributeInRec(p_rec, ATTR_ID_SERVICE_CLASS_ID_LIST)) != NULL)
1644 /* find peer features */
1645 if (SDP_FindServiceInDb(p_cb->p_disc_db, UUID_SERVCLASS_AV_REMOTE_CONTROL, NULL))
1647 peer_features |= BTA_AV_FEAT_RCCT;
1649 if (SDP_FindServiceInDb(p_cb->p_disc_db, UUID_SERVCLASS_AV_REM_CTRL_TARGET, NULL))
1651 peer_features |= BTA_AV_FEAT_RCTG;
1655 if (( SDP_FindAttributeInRec(p_rec, ATTR_ID_BT_PROFILE_DESC_LIST)) != NULL)
1657 /* get profile version (if failure, version parameter is not updated) */
1658 SDP_FindProfileVersionInRec(p_rec, UUID_SERVCLASS_AV_REMOTE_CONTROL, &peer_rc_version);
1659 APPL_TRACE_DEBUG("peer_rc_version 0x%x", peer_rc_version);
1661 if (peer_rc_version >= AVRC_REV_1_3)
1662 peer_features |= (BTA_AV_FEAT_VENDOR | BTA_AV_FEAT_METADATA);
1664 if (peer_rc_version >= AVRC_REV_1_4)
1666 peer_features |= (BTA_AV_FEAT_ADV_CTRL);
1667 /* get supported categories */
1668 if ((p_attr = SDP_FindAttributeInRec(p_rec,
1669 ATTR_ID_SUPPORTED_FEATURES)) != NULL)
1671 categories = p_attr->attr_value.v.u16;
1672 if (categories & AVRC_SUPF_CT_BROWSE)
1673 peer_features |= (BTA_AV_FEAT_BROWSE);
1678 APPL_TRACE_DEBUG("peer_features:x%x", peer_features);
1679 return peer_features;
1682 /*******************************************************************************
1684 ** Function bta_av_rc_disc_done
1686 ** Description Handle AVRCP service discovery results. If matching
1687 ** service found, open AVRCP connection.
1691 *******************************************************************************/
1692 void bta_av_rc_disc_done(tBTA_AV_DATA *p_data)
1694 tBTA_AV_CB *p_cb = &bta_av_cb;
1695 tBTA_AV_SCB *p_scb = NULL;
1697 tBTA_AV_RC_OPEN rc_open;
1698 tBTA_AV_RC_FEAT rc_feat;
1700 tBTA_AV_FEAT peer_features; /* peer features mask */
1703 APPL_TRACE_DEBUG("bta_av_rc_disc_done disc:x%x", p_cb->disc);
1709 if ((p_cb->disc & BTA_AV_CHNL_MSK) == BTA_AV_CHNL_MSK)
1711 /* this is the rc handle/index to tBTA_AV_RCB */
1712 rc_handle = p_cb->disc & (~BTA_AV_CHNL_MSK);
1716 p_scb = p_cb->p_scb[(p_cb->disc & BTA_AV_HNDL_MSK) - 1];
1718 rc_handle = p_scb->rc_handle;
1726 APPL_TRACE_DEBUG("rc_handle %d", rc_handle);
1727 /* check peer version and whether support CT and TG role */
1728 peer_features = bta_av_check_peer_features (UUID_SERVCLASS_AV_REMOTE_CONTROL);
1729 if ((p_cb->features & BTA_AV_FEAT_ADV_CTRL) && ((peer_features&BTA_AV_FEAT_ADV_CTRL) == 0))
1731 /* if we support advance control and peer does not, check their support on TG role
1732 * some implementation uses 1.3 on CT ans 1.4 on TG */
1733 peer_features |= bta_av_check_peer_features (UUID_SERVCLASS_AV_REM_CTRL_TARGET);
1737 utl_freebuf((void **) &p_cb->p_disc_db);
1739 APPL_TRACE_DEBUG("peer_features 0x%x, features 0x%x", peer_features, p_cb->features);
1741 /* if we have no rc connection */
1742 if (rc_handle == BTA_AV_RC_HANDLE_NONE)
1746 /* if peer remote control service matches ours and USE_RC is TRUE */
1747 if ((((p_cb->features & BTA_AV_FEAT_RCCT) && (peer_features & BTA_AV_FEAT_RCTG)) ||
1748 ((p_cb->features & BTA_AV_FEAT_RCTG) && (peer_features & BTA_AV_FEAT_RCCT))) )
1750 p_lcb = bta_av_find_lcb(p_scb->peer_addr, BTA_AV_LCB_FIND);
1753 rc_handle = bta_av_rc_create(p_cb, AVCT_INT, (UINT8)(p_scb->hdi + 1), p_lcb->lidx);
1754 p_cb->rcb[rc_handle].peer_features = peer_features;
1756 #if (BT_USE_TRACES == TRUE || BT_TRACE_APPL == TRUE)
1759 APPL_TRACE_ERROR("can not find LCB!!");
1763 else if(p_scb->use_rc)
1765 /* can not find AVRC on peer device. report failure */
1766 p_scb->use_rc = FALSE;
1767 bdcpy(rc_open.peer_addr, p_scb->peer_addr);
1768 rc_open.peer_features = 0;
1769 rc_open.status = BTA_AV_FAIL_SDP;
1770 (*p_cb->p_cback)(BTA_AV_RC_OPEN_EVT, (tBTA_AV *) &rc_open);
1776 p_cb->rcb[rc_handle].peer_features = peer_features;
1777 rc_feat.rc_handle = rc_handle;
1778 rc_feat.peer_features = peer_features;
1779 (*p_cb->p_cback)(BTA_AV_RC_FEAT_EVT, (tBTA_AV *) &rc_feat);
1783 /*******************************************************************************
1785 ** Function bta_av_rc_closed
1787 ** Description Set AVRCP state to closed.
1791 *******************************************************************************/
1792 void bta_av_rc_closed(tBTA_AV_DATA *p_data)
1794 tBTA_AV_CB *p_cb = &bta_av_cb;
1795 tBTA_AV_RC_CLOSE rc_close;
1796 tBTA_AV_RC_CONN_CHG *p_msg = (tBTA_AV_RC_CONN_CHG *)p_data;
1800 BOOLEAN conn = FALSE;
1803 rc_close.rc_handle = BTA_AV_RC_HANDLE_NONE;
1804 APPL_TRACE_DEBUG("bta_av_rc_closed rc_handle:%d", p_msg->handle);
1805 for(i=0; i<BTA_AV_NUM_RCB; i++)
1807 p_rcb = &p_cb->rcb[i];
1808 APPL_TRACE_DEBUG("bta_av_rc_closed rcb[%d] rc_handle:%d, status=0x%x", i, p_rcb->handle, p_rcb->status);
1809 if(p_rcb->handle == p_msg->handle)
1811 rc_close.rc_handle = i;
1812 p_rcb->status &= ~BTA_AV_RC_CONN_MASK;
1813 p_rcb->peer_features = 0;
1814 APPL_TRACE_DEBUG(" shdl:%d, lidx:%d", p_rcb->shdl, p_rcb->lidx);
1817 p_scb = bta_av_cb.p_scb[p_rcb->shdl - 1];
1820 bdcpy(rc_close.peer_addr, p_scb->peer_addr);
1821 if(p_scb->rc_handle == p_rcb->handle)
1822 p_scb->rc_handle = BTA_AV_RC_HANDLE_NONE;
1823 APPL_TRACE_DEBUG("shdl:%d, srch:%d", p_rcb->shdl, p_scb->rc_handle);
1827 else if(p_rcb->lidx == (BTA_AV_NUM_LINKS + 1) )
1829 /* if the RCB uses the extra LCB, use the addr for event and clean it */
1830 p_lcb = &p_cb->lcb[BTA_AV_NUM_LINKS];
1831 bdcpy(rc_close.peer_addr, p_msg->peer_addr);
1832 APPL_TRACE_DEBUG("rc_only closed bd_addr:%02x-%02x-%02x-%02x-%02x-%02x",
1833 p_msg->peer_addr[0], p_msg->peer_addr[1],
1834 p_msg->peer_addr[2], p_msg->peer_addr[3],
1835 p_msg->peer_addr[4], p_msg->peer_addr[5]);
1836 p_lcb->conn_msk = 0;
1841 if((p_rcb->status & BTA_AV_RC_ROLE_MASK) == BTA_AV_RC_ROLE_INT)
1843 /* AVCT CCB is deallocated */
1844 p_rcb->handle = BTA_AV_RC_HANDLE_NONE;
1849 /* AVCT CCB is still there. dealloc */
1850 bta_av_del_rc(p_rcb);
1852 /* if the AVRCP is no longer listening, create the listening channel */
1853 if (bta_av_cb.rc_acp_handle == BTA_AV_RC_HANDLE_NONE && bta_av_cb.features & BTA_AV_FEAT_RCTG)
1854 bta_av_rc_create(&bta_av_cb, AVCT_ACP, 0, BTA_AV_NUM_LINKS + 1);
1857 else if((p_rcb->handle != BTA_AV_RC_HANDLE_NONE) && (p_rcb->status & BTA_AV_RC_CONN_MASK))
1859 /* at least one channel is still connected */
1866 /* no AVRC channels are connected, go back to INIT state */
1867 bta_av_sm_execute(p_cb, BTA_AV_AVRC_NONE_EVT, NULL);
1870 if (rc_close.rc_handle == BTA_AV_RC_HANDLE_NONE)
1872 rc_close.rc_handle = p_msg->handle;
1873 bdcpy(rc_close.peer_addr, p_msg->peer_addr);
1875 (*p_cb->p_cback)(BTA_AV_RC_CLOSE_EVT, (tBTA_AV *) &rc_close);
1878 /*******************************************************************************
1880 ** Function bta_av_rc_disc
1882 ** Description start AVRC SDP discovery.
1886 *******************************************************************************/
1887 void bta_av_rc_disc(UINT8 disc)
1889 tBTA_AV_CB *p_cb = &bta_av_cb;
1890 tAVRC_SDP_DB_PARAMS db_params;
1891 UINT16 attr_list[] = {ATTR_ID_SERVICE_CLASS_ID_LIST,
1892 ATTR_ID_BT_PROFILE_DESC_LIST,
1893 ATTR_ID_SUPPORTED_FEATURES};
1896 UINT8 *p_addr = NULL;
1899 APPL_TRACE_DEBUG("bta_av_rc_disc 0x%x, %d", disc, bta_av_cb.disc);
1900 if ((bta_av_cb.disc != 0) || (disc == 0))
1903 if ((disc & BTA_AV_CHNL_MSK) == BTA_AV_CHNL_MSK)
1905 /* this is the rc handle/index to tBTA_AV_RCB */
1906 rc_handle = disc & (~BTA_AV_CHNL_MSK);
1907 if (p_cb->rcb[rc_handle].lidx)
1909 p_addr = p_cb->lcb[p_cb->rcb[rc_handle].lidx-1].addr;
1914 hdi = (disc & BTA_AV_HNDL_MSK) - 1;
1915 p_scb = p_cb->p_scb[hdi];
1919 APPL_TRACE_DEBUG("rc_handle %d", p_scb->rc_handle);
1920 p_addr = p_scb->peer_addr;
1926 /* allocate discovery database */
1927 if (p_cb->p_disc_db == NULL)
1929 p_cb->p_disc_db = (tSDP_DISCOVERY_DB *) GKI_getbuf(BTA_AV_DISC_BUF_SIZE);
1932 if (p_cb->p_disc_db)
1934 /* set up parameters */
1935 db_params.db_len = BTA_AV_DISC_BUF_SIZE;
1936 db_params.num_attr = 3;
1937 db_params.p_db = p_cb->p_disc_db;
1938 db_params.p_attrs = attr_list;
1940 /* searching for UUID_SERVCLASS_AV_REMOTE_CONTROL gets both TG and CT */
1941 if (AVRC_FindService(UUID_SERVCLASS_AV_REMOTE_CONTROL, p_addr, &db_params,
1942 bta_av_avrc_sdp_cback) == AVRC_SUCCESS)
1945 APPL_TRACE_DEBUG("disc %d", p_cb->disc);
1951 /*******************************************************************************
1953 ** Function bta_av_dereg_comp
1955 ** Description deregister complete. free the stream control block.
1959 *******************************************************************************/
1960 void bta_av_dereg_comp(tBTA_AV_DATA *p_data)
1962 tBTA_AV_CB *p_cb = &bta_av_cb;
1968 /* find the stream control block */
1969 p_scb = bta_av_hndl_to_scb(p_data->hdr.layer_specific);
1973 APPL_TRACE_DEBUG("deregistered %d(h%d)", p_scb->chnl, p_scb->hndl);
1974 mask = BTA_AV_HNDL_TO_MSK(p_scb->hdi);
1975 if(p_scb->chnl == BTA_AV_CHNL_AUDIO)
1977 p_cb->reg_audio &= ~mask;
1978 if ((p_cb->conn_audio & mask) && bta_av_cb.audio_open_cnt)
1980 /* this channel is still marked as open. decrease the count */
1981 bta_av_cb.audio_open_cnt--;
1983 p_cb->conn_audio &= ~mask;
1985 if (p_scb->q_tag == BTA_AV_Q_TAG_STREAM) {
1986 /* make sure no buffers are in q_info.a2d */
1987 while (!list_is_empty(p_scb->q_info.a2d_list)) {
1988 p_buf = (BT_HDR*)list_front(p_scb->q_info.a2d_list);
1989 list_remove(p_scb->q_info.a2d_list, p_buf);
1994 /* remove the A2DP SDP record, if no more audio stream is left */
1995 if(!p_cb->reg_audio)
1997 #if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
1998 bta_ar_dereg_avrc (UUID_SERVCLASS_AV_REMOTE_CONTROL, BTA_ID_AV);
2000 bta_av_del_sdp_rec(&p_cb->sdp_a2d_handle);
2001 bta_sys_remove_uuid(UUID_SERVCLASS_AUDIO_SOURCE);
2003 #if (BTA_AV_SINK_INCLUDED == TRUE)
2004 bta_av_del_sdp_rec(&p_cb->sdp_a2d_snk_handle);
2005 bta_sys_remove_uuid(UUID_SERVCLASS_AUDIO_SINK);
2011 p_cb->reg_video &= ~mask;
2012 /* make sure that this channel is not connected */
2013 p_cb->conn_video &= ~mask;
2014 /* remove the VDP SDP record, (only one video stream at most) */
2015 bta_av_del_sdp_rec(&p_cb->sdp_vdp_handle);
2016 bta_sys_remove_uuid(UUID_SERVCLASS_VIDEO_SOURCE);
2019 /* make sure that the timer is not active */
2020 bta_sys_stop_timer(&p_scb->timer);
2021 utl_freebuf((void **)&p_cb->p_scb[p_scb->hdi]);
2024 APPL_TRACE_DEBUG("audio 0x%x, video: 0x%x, disable:%d",
2025 p_cb->reg_audio, p_cb->reg_video, p_cb->disabling);
2026 /* if no stream control block is active */
2027 if((p_cb->reg_audio + p_cb->reg_video) == 0)
2029 #if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
2030 /* deregister from AVDT */
2031 bta_ar_dereg_avdt(BTA_ID_AV);
2033 /* deregister from AVCT */
2034 bta_ar_dereg_avrc (UUID_SERVCLASS_AV_REM_CTRL_TARGET, BTA_ID_AV);
2035 bta_ar_dereg_avct(BTA_ID_AV);
2040 p_cb->disabling = FALSE;
2041 bta_av_cb.features = 0;
2044 /* Clear the Capturing service class bit */
2045 cod.service = BTM_COD_SERVICE_CAPTURING;
2046 utl_set_device_class(&cod, BTA_UTL_CLR_COD_SERVICE_CLASS);
2049 #endif /* BTA_AV_INCLUDED */