Home | History | Annotate | Download | only in av
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2004-2012 Broadcom Corporation
      4  *
      5  *  Licensed under the Apache License, Version 2.0 (the "License");
      6  *  you may not use this file except in compliance with the License.
      7  *  You may obtain a copy of the License at:
      8  *
      9  *  http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  *
     17  ******************************************************************************/
     18 
     19 /******************************************************************************
     20  *
     21  *  This file contains action functions for advanced audio/video main state
     22  *  machine.
     23  *
     24  ******************************************************************************/
     25 
     26 #include "bt_target.h"
     27 #if defined(BTA_AV_INCLUDED) && (BTA_AV_INCLUDED == TRUE)
     28 
     29 #include <string.h>
     30 #include "bta_av_api.h"
     31 #include "bta_av_int.h"
     32 #include "avdt_api.h"
     33 #include "bd.h"
     34 #include "utl.h"
     35 #include "l2c_api.h"
     36 #if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
     37 #include "bta_ar_api.h"
     38 #endif
     39 
     40 /*****************************************************************************
     41 **  Constants
     42 *****************************************************************************/
     43 /* the timer in milliseconds to wait for open req after setconfig for incoming connections */
     44 #ifndef BTA_AV_SIG_TIME_VAL
     45 #define BTA_AV_SIG_TIME_VAL 8000
     46 #endif
     47 
     48 /* In millisec to wait for signalling from SNK when it is initiated from SNK.   */
     49 /* If not, we will start signalling from SRC.                                   */
     50 #ifndef BTA_AV_ACP_SIG_TIME_VAL
     51 #define BTA_AV_ACP_SIG_TIME_VAL 2000
     52 #endif
     53 
     54 static void bta_av_acp_sig_timer_cback (TIMER_LIST_ENT *p_tle);
     55 
     56 /*******************************************************************************
     57 **
     58 ** Function         bta_av_get_rcb_by_shdl
     59 **
     60 ** Description      find the RCB associated with the given SCB handle.
     61 **
     62 ** Returns          tBTA_AV_RCB
     63 **
     64 *******************************************************************************/
     65 tBTA_AV_RCB * bta_av_get_rcb_by_shdl(UINT8 shdl)
     66 {
     67     tBTA_AV_RCB *p_rcb = NULL;
     68     int         i;
     69 
     70     for (i=0; i<BTA_AV_NUM_RCB; i++)
     71     {
     72         if (bta_av_cb.rcb[i].shdl == shdl && bta_av_cb.rcb[i].handle != BTA_AV_RC_HANDLE_NONE)
     73         {
     74             p_rcb = &bta_av_cb.rcb[i];
     75             break;
     76         }
     77     }
     78     return p_rcb;
     79 }
     80 #define BTA_AV_STS_NO_RSP       0xFF    /* a number not used by tAVRC_STS */
     81 
     82 /*******************************************************************************
     83 **
     84 ** Function         bta_av_del_rc
     85 **
     86 ** Description      delete the given AVRC handle.
     87 **
     88 ** Returns          void
     89 **
     90 *******************************************************************************/
     91 void bta_av_del_rc(tBTA_AV_RCB *p_rcb)
     92 {
     93     tBTA_AV_SCB  *p_scb;
     94     UINT8        rc_handle;      /* connected AVRCP handle */
     95 
     96     if(p_rcb->handle != BTA_AV_RC_HANDLE_NONE)
     97     {
     98         if(p_rcb->shdl)
     99         {
    100             p_scb = bta_av_cb.p_scb[p_rcb->shdl - 1];
    101             if(p_scb)
    102             {
    103                 APPL_TRACE_DEBUG3("bta_av_del_rc shdl:%d, srch:%d rc_handle:%d", p_rcb->shdl,
    104                                   p_scb->rc_handle, p_rcb->handle);
    105                 if(p_scb->rc_handle == p_rcb->handle)
    106                     p_scb->rc_handle = BTA_AV_RC_HANDLE_NONE;
    107                 /* just in case the RC timer is active
    108                 if(bta_av_cb.features & BTA_AV_FEAT_RCCT && p_scb->chnl == BTA_AV_CHNL_AUDIO) */
    109                     bta_sys_stop_timer(&p_scb->timer);
    110             }
    111         }
    112 
    113         APPL_TRACE_EVENT4("bta_av_del_rc  handle: %d status=0x%x, rc_acp_handle:%d, idx:%d",
    114             p_rcb->handle, p_rcb->status, bta_av_cb.rc_acp_handle, bta_av_cb.rc_acp_idx);
    115         rc_handle = p_rcb->handle;
    116         if(!(p_rcb->status & BTA_AV_RC_CONN_MASK) ||
    117             ((p_rcb->status & BTA_AV_RC_ROLE_MASK) == BTA_AV_RC_ROLE_INT) )
    118         {
    119             p_rcb->status = 0;
    120             p_rcb->handle = BTA_AV_RC_HANDLE_NONE;
    121             p_rcb->shdl = 0;
    122             p_rcb->lidx = 0;
    123         }
    124         /* else ACP && connected. do not clear the handle yet */
    125         AVRC_Close(rc_handle);
    126         if (rc_handle == bta_av_cb.rc_acp_handle)
    127             bta_av_cb.rc_acp_handle = BTA_AV_RC_HANDLE_NONE;
    128         APPL_TRACE_EVENT4("end del_rc handle: %d status=0x%x, rc_acp_handle:%d, lidx:%d",
    129             p_rcb->handle, p_rcb->status, bta_av_cb.rc_acp_handle, p_rcb->lidx);
    130     }
    131 }
    132 
    133 
    134 /*******************************************************************************
    135 **
    136 ** Function         bta_av_close_all_rc
    137 **
    138 ** Description      close the all AVRC handle.
    139 **
    140 ** Returns          void
    141 **
    142 *******************************************************************************/
    143 static void bta_av_close_all_rc(tBTA_AV_CB *p_cb)
    144 {
    145     int i;
    146 
    147     for(i=0; i<BTA_AV_NUM_RCB; i++)
    148     {
    149         if ((p_cb->disabling == TRUE) || (bta_av_cb.rcb[i].shdl != 0))
    150             bta_av_del_rc(&bta_av_cb.rcb[i]);
    151     }
    152 }
    153 
    154 /*******************************************************************************
    155 **
    156 ** Function         bta_av_del_sdp_rec
    157 **
    158 ** Description      delete the given SDP record handle.
    159 **
    160 ** Returns          void
    161 **
    162 *******************************************************************************/
    163 static void bta_av_del_sdp_rec(UINT32 *p_sdp_handle)
    164 {
    165     if(*p_sdp_handle != 0)
    166     {
    167         SDP_DeleteRecord(*p_sdp_handle);
    168         *p_sdp_handle = 0;
    169     }
    170 }
    171 
    172 /*******************************************************************************
    173 **
    174 ** Function         bta_av_avrc_sdp_cback
    175 **
    176 ** Description      AVRCP service discovery callback.
    177 **
    178 ** Returns          void
    179 **
    180 *******************************************************************************/
    181 static void bta_av_avrc_sdp_cback(UINT16 status)
    182 {
    183     BT_HDR *p_msg;
    184 
    185     if ((p_msg = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
    186     {
    187         p_msg->event = BTA_AV_SDP_AVRC_DISC_EVT;
    188         bta_sys_sendmsg(p_msg);
    189     }
    190 }
    191 
    192 /*******************************************************************************
    193 **
    194 ** Function         bta_av_rc_ctrl_cback
    195 **
    196 ** Description      AVRCP control callback.
    197 **
    198 ** Returns          void
    199 **
    200 *******************************************************************************/
    201 static void bta_av_rc_ctrl_cback(UINT8 handle, UINT8 event, UINT16 result, BD_ADDR peer_addr)
    202 {
    203     tBTA_AV_RC_CONN_CHG *p_msg;
    204     UINT16 msg_event = 0;
    205 
    206 #if (defined(BTA_AV_MIN_DEBUG_TRACES) && BTA_AV_MIN_DEBUG_TRACES == TRUE)
    207     APPL_TRACE_EVENT2("rc_ctrl handle: %d event=0x%x", handle, event);
    208 #else
    209     APPL_TRACE_EVENT2("bta_av_rc_ctrl_cback handle: %d event=0x%x", handle, event);
    210 #endif
    211     if (event == AVRC_OPEN_IND_EVT)
    212     {
    213         /* save handle of opened connection
    214         bta_av_cb.rc_handle = handle;*/
    215 
    216         msg_event = BTA_AV_AVRC_OPEN_EVT;
    217     }
    218     else if (event == AVRC_CLOSE_IND_EVT)
    219     {
    220         msg_event = BTA_AV_AVRC_CLOSE_EVT;
    221     }
    222 
    223     if (msg_event)
    224     {
    225         if ((p_msg = (tBTA_AV_RC_CONN_CHG *) GKI_getbuf(sizeof(tBTA_AV_RC_CONN_CHG))) != NULL)
    226         {
    227             p_msg->hdr.event = msg_event;
    228             p_msg->handle    = handle;
    229             if(peer_addr)
    230                 bdcpy(p_msg->peer_addr, peer_addr);
    231             bta_sys_sendmsg(p_msg);
    232         }
    233     }
    234 }
    235 
    236 /*******************************************************************************
    237 **
    238 ** Function         bta_av_rc_msg_cback
    239 **
    240 ** Description      AVRCP message callback.
    241 **
    242 ** Returns          void
    243 **
    244 *******************************************************************************/
    245 static void bta_av_rc_msg_cback(UINT8 handle, UINT8 label, UINT8 opcode, tAVRC_MSG *p_msg)
    246 {
    247     tBTA_AV_RC_MSG  *p_buf;
    248     UINT8           *p_data = NULL;
    249     UINT8           **p_p_data = NULL;
    250     UINT16          data_len = 0;
    251 
    252 #if (defined(BTA_AV_MIN_DEBUG_TRACES) && BTA_AV_MIN_DEBUG_TRACES == TRUE)
    253     APPL_TRACE_ERROR2("rc_msg handle: %d opcode=0x%x", handle, opcode);
    254 #else
    255     APPL_TRACE_EVENT2("bta_av_rc_msg_cback handle: %d opcode=0x%x", handle, opcode);
    256 #endif
    257     /* determine size of buffer we need */
    258     if (opcode == AVRC_OP_VENDOR && p_msg->vendor.p_vendor_data != NULL)
    259     {
    260         p_data = p_msg->vendor.p_vendor_data;
    261         p_p_data = &p_msg->vendor.p_vendor_data;
    262         data_len = (UINT16) p_msg->vendor.vendor_len;
    263     }
    264     else if (opcode == AVRC_OP_PASS_THRU && p_msg->pass.p_pass_data != NULL)
    265     {
    266         p_data = p_msg->pass.p_pass_data;
    267         p_p_data = &p_msg->pass.p_pass_data;
    268         data_len = (UINT16) p_msg->pass.pass_len;
    269     }
    270 
    271     if ((p_buf = (tBTA_AV_RC_MSG *) GKI_getbuf((UINT16) (sizeof(tBTA_AV_RC_MSG) + data_len))) != NULL)
    272     {
    273         p_buf->hdr.event = BTA_AV_AVRC_MSG_EVT;
    274         p_buf->handle = handle;
    275         p_buf->label = label;
    276         p_buf->opcode = opcode;
    277         memcpy(&p_buf->msg, p_msg, sizeof(tAVRC_MSG));
    278         if (p_data != NULL)
    279         {
    280             memcpy((UINT8 *)(p_buf + 1), p_data, data_len);
    281             *p_p_data = (UINT8 *)(p_buf + 1);
    282         }
    283         bta_sys_sendmsg(p_buf);
    284     }
    285 }
    286 
    287 /*******************************************************************************
    288 **
    289 ** Function         bta_av_rc_create
    290 **
    291 ** Description      alloc RCB and call AVRC_Open
    292 **
    293 ** Returns          the created rc handle
    294 **
    295 *******************************************************************************/
    296 UINT8 bta_av_rc_create(tBTA_AV_CB *p_cb, UINT8 role, UINT8 shdl, UINT8 lidx)
    297 {
    298     tAVRC_CONN_CB ccb;
    299     BD_ADDR_PTR   bda = (BD_ADDR_PTR)bd_addr_any;
    300     UINT8         status = BTA_AV_RC_ROLE_ACP;
    301     tBTA_AV_SCB  *p_scb = p_cb->p_scb[shdl - 1];
    302     int i;
    303     UINT8   rc_handle;
    304     tBTA_AV_RCB *p_rcb;
    305 
    306     if(role == AVCT_INT)
    307     {
    308         bda = p_scb->peer_addr;
    309         status = BTA_AV_RC_ROLE_INT;
    310     }
    311     else
    312     {
    313         if ((p_rcb = bta_av_get_rcb_by_shdl(shdl)) != NULL )
    314         {
    315             APPL_TRACE_ERROR1("bta_av_rc_create ACP handle exist for shdl:%d", shdl);
    316             return p_rcb->handle;
    317         }
    318     }
    319 
    320     ccb.p_ctrl_cback = bta_av_rc_ctrl_cback;
    321     ccb.p_msg_cback = bta_av_rc_msg_cback;
    322     ccb.company_id = p_bta_av_cfg->company_id;
    323     ccb.conn = role;
    324     /* note: BTA_AV_FEAT_RCTG = AVRC_CT_TARGET, BTA_AV_FEAT_RCCT = AVRC_CT_CONTROL */
    325     ccb.control = p_cb->features & (BTA_AV_FEAT_RCTG | BTA_AV_FEAT_RCCT | AVRC_CT_PASSIVE);
    326 
    327 
    328     if (AVRC_Open(&rc_handle, &ccb, bda) != AVRC_SUCCESS)
    329         return BTA_AV_RC_HANDLE_NONE;
    330 
    331     i = rc_handle;
    332     p_rcb = &p_cb->rcb[i];
    333 
    334     if (p_rcb->handle != BTA_AV_RC_HANDLE_NONE)
    335     {
    336         APPL_TRACE_ERROR1("bta_av_rc_create found duplicated handle:%d", rc_handle);
    337     }
    338 
    339     p_rcb->handle = rc_handle;
    340     p_rcb->status = status;
    341     p_rcb->shdl = shdl;
    342     p_rcb->lidx = lidx;
    343     p_rcb->peer_features = 0;
    344     if(lidx == (BTA_AV_NUM_LINKS + 1))
    345     {
    346         /* this LIDX is reserved for the AVRCP ACP connection */
    347         p_cb->rc_acp_handle = p_rcb->handle;
    348         p_cb->rc_acp_idx = (i + 1);
    349         APPL_TRACE_DEBUG2("rc_acp_handle:%d idx:%d", p_cb->rc_acp_handle, p_cb->rc_acp_idx);
    350     }
    351     APPL_TRACE_DEBUG6("create %d, role: %d, shdl:%d, rc_handle:%d, lidx:%d, status:0x%x",
    352         i, role, shdl, p_rcb->handle, lidx, p_rcb->status);
    353 
    354     return rc_handle;
    355 }
    356 
    357 /*******************************************************************************
    358 **
    359 ** Function         bta_av_valid_group_navi_msg
    360 **
    361 ** Description      Check if it is Group Navigation Msg for Metadata
    362 **
    363 ** Returns          BTA_AV_RSP_ACCEPT or BTA_AV_RSP_NOT_IMPL.
    364 **
    365 *******************************************************************************/
    366 static tBTA_AV_CODE bta_av_group_navi_supported(UINT8 len, UINT8 *p_data, BOOLEAN is_inquiry)
    367 {
    368     tBTA_AV_CODE ret=BTA_AV_RSP_NOT_IMPL;
    369     UINT8 *p_ptr = p_data;
    370     UINT16 u16;
    371     UINT32 u32;
    372 
    373     if (p_bta_av_cfg->avrc_group && len == BTA_GROUP_NAVI_MSG_OP_DATA_LEN)
    374     {
    375         BTA_AV_BE_STREAM_TO_CO_ID(u32, p_ptr);
    376         BE_STREAM_TO_UINT16(u16, p_ptr);
    377 
    378         if (u32 == AVRC_CO_METADATA)
    379         {
    380             if (is_inquiry)
    381             {
    382                 if (u16 <= AVRC_PDU_PREV_GROUP)
    383                     ret = BTA_AV_RSP_IMPL_STBL;
    384             }
    385             else
    386             {
    387                 if (u16 <= AVRC_PDU_PREV_GROUP)
    388                     ret = BTA_AV_RSP_ACCEPT;
    389                 else
    390                     ret = BTA_AV_RSP_REJ;
    391             }
    392         }
    393     }
    394 
    395     return ret;
    396 }
    397 
    398 /*******************************************************************************
    399 **
    400 ** Function         bta_av_op_supported
    401 **
    402 ** Description      Check if remote control operation is supported.
    403 **
    404 ** Returns          BTA_AV_RSP_ACCEPT of supported, BTA_AV_RSP_NOT_IMPL if not.
    405 **
    406 *******************************************************************************/
    407 static tBTA_AV_CODE bta_av_op_supported(tBTA_AV_RC rc_id, BOOLEAN is_inquiry)
    408 {
    409     tBTA_AV_CODE ret_code = BTA_AV_RSP_NOT_IMPL;
    410 
    411     if (p_bta_av_rc_id)
    412     {
    413         if (is_inquiry)
    414         {
    415             if (p_bta_av_rc_id[rc_id >> 4] & (1 << (rc_id & 0x0F)))
    416             {
    417                 ret_code = BTA_AV_RSP_IMPL_STBL;
    418             }
    419         }
    420         else
    421         {
    422             if (p_bta_av_rc_id[rc_id >> 4] & (1 << (rc_id & 0x0F)))
    423             {
    424                 ret_code = BTA_AV_RSP_ACCEPT;
    425             }
    426             else if ((p_bta_av_cfg->rc_pass_rsp == BTA_AV_RSP_INTERIM) && p_bta_av_rc_id_ac)
    427             {
    428                 if (p_bta_av_rc_id_ac[rc_id >> 4] & (1 << (rc_id & 0x0F)))
    429                 {
    430                     ret_code = BTA_AV_RSP_INTERIM;
    431                 }
    432             }
    433         }
    434 
    435     }
    436     return ret_code;
    437 }
    438 
    439 /*******************************************************************************
    440 **
    441 ** Function         bta_av_find_lcb
    442 **
    443 ** Description      Given BD_addr, find the associated LCB.
    444 **
    445 ** Returns          NULL, if not found.
    446 **
    447 *******************************************************************************/
    448 tBTA_AV_LCB * bta_av_find_lcb(BD_ADDR addr, UINT8 op)
    449 {
    450     tBTA_AV_CB   *p_cb = &bta_av_cb;
    451     int     xx;
    452     UINT8   mask;
    453     tBTA_AV_LCB *p_lcb = NULL;
    454 
    455     for(xx=0; xx<BTA_AV_NUM_LINKS; xx++)
    456     {
    457         mask = 1 << xx; /* the used mask for this lcb */
    458         if((mask & p_cb->conn_lcb) && 0 ==( bdcmp(p_cb->lcb[xx].addr, addr)))
    459         {
    460             p_lcb = &p_cb->lcb[xx];
    461             if(op == BTA_AV_LCB_FREE)
    462             {
    463                 p_cb->conn_lcb &= ~mask; /* clear the connect mask */
    464                 APPL_TRACE_DEBUG1("conn_lcb: 0x%x", p_cb->conn_lcb);
    465             }
    466             break;
    467         }
    468     }
    469     return p_lcb;
    470 }
    471 
    472 /*******************************************************************************
    473 **
    474 ** Function         bta_av_rc_opened
    475 **
    476 ** Description      Set AVRCP state to opened.
    477 **
    478 ** Returns          void
    479 **
    480 *******************************************************************************/
    481 void bta_av_rc_opened(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
    482 {
    483     tBTA_AV_RC_OPEN rc_open;
    484     tBTA_AV_SCB     *p_scb;
    485     int         i;
    486     UINT8       shdl = 0;
    487     tBTA_AV_LCB *p_lcb;
    488     tBTA_AV_RCB *p_rcb;
    489     UINT8       tmp;
    490     UINT8       disc = 0;
    491 
    492     /* find the SCB & stop the timer */
    493     for(i=0; i<BTA_AV_NUM_STRS; i++)
    494     {
    495         p_scb = p_cb->p_scb[i];
    496         if(p_scb && bdcmp(p_scb->peer_addr, p_data->rc_conn_chg.peer_addr) == 0)
    497         {
    498             p_scb->rc_handle = p_data->rc_conn_chg.handle;
    499             APPL_TRACE_DEBUG2("bta_av_rc_opened shdl:%d, srch %d", i + 1, p_scb->rc_handle);
    500             shdl = i+1;
    501             APPL_TRACE_ERROR1("use_rc:%d", p_scb->use_rc);
    502             bta_sys_stop_timer(&p_scb->timer);
    503             disc = p_scb->hndl;
    504             break;
    505         }
    506     }
    507 
    508     i = p_data->rc_conn_chg.handle;
    509     if (p_cb->rcb[i].handle == BTA_AV_RC_HANDLE_NONE)
    510     {
    511         APPL_TRACE_ERROR1("not a valid handle:%d any more", i);
    512         return;
    513     }
    514 
    515 
    516     if (p_cb->rcb[i].lidx == (BTA_AV_NUM_LINKS + 1) && shdl != 0)
    517     {
    518         /* rc is opened on the RC only ACP channel, but is for a specific
    519          * SCB -> need to switch RCBs */
    520         p_rcb = bta_av_get_rcb_by_shdl(shdl);
    521         if (p_rcb)
    522         {
    523             p_rcb->shdl = p_cb->rcb[i].shdl;
    524             tmp         = p_rcb->lidx;
    525             p_rcb->lidx = p_cb->rcb[i].lidx;
    526             p_cb->rcb[i].lidx = tmp;
    527             p_cb->rc_acp_handle = p_rcb->handle;
    528             p_cb->rc_acp_idx = (p_rcb - p_cb->rcb) + 1;
    529             APPL_TRACE_DEBUG2("switching RCB rc_acp_handle:%d idx:%d",
    530                                p_cb->rc_acp_handle, p_cb->rc_acp_idx);
    531         }
    532     }
    533 
    534     p_cb->rcb[i].shdl = shdl;
    535     rc_open.rc_handle = i;
    536     APPL_TRACE_ERROR4("bta_av_rc_opened rcb[%d] shdl:%d lidx:%d/%d",
    537             i, shdl, p_cb->rcb[i].lidx, p_cb->lcb[BTA_AV_NUM_LINKS].lidx);
    538     p_cb->rcb[i].status |= BTA_AV_RC_CONN_MASK;
    539 
    540     if(!shdl && 0 == p_cb->lcb[BTA_AV_NUM_LINKS].lidx)
    541     {
    542         /* no associated SCB -> connected to an RC only device
    543          * update the index to the extra LCB */
    544         p_lcb = &p_cb->lcb[BTA_AV_NUM_LINKS];
    545         bdcpy(p_lcb->addr, p_data->rc_conn_chg.peer_addr);
    546         APPL_TRACE_DEBUG6("rc_only bd_addr:%02x-%02x-%02x-%02x-%02x-%02x",
    547                       p_lcb->addr[0], p_lcb->addr[1],
    548                       p_lcb->addr[2], p_lcb->addr[3],
    549                       p_lcb->addr[4], p_lcb->addr[5]);
    550         p_lcb->lidx = BTA_AV_NUM_LINKS + 1;
    551             p_cb->rcb[i].lidx = p_lcb->lidx;
    552         p_lcb->conn_msk = 1;
    553         APPL_TRACE_ERROR3("rcb[%d].lidx=%d, lcb.conn_msk=x%x",
    554             i, p_cb->rcb[i].lidx, p_lcb->conn_msk);
    555         disc = p_data->rc_conn_chg.handle|BTA_AV_CHNL_MSK;
    556     }
    557 
    558     bdcpy(rc_open.peer_addr, p_data->rc_conn_chg.peer_addr);
    559     rc_open.peer_features = p_cb->rcb[i].peer_features;
    560     rc_open.status = BTA_AV_SUCCESS;
    561     APPL_TRACE_DEBUG2("local features:x%x peer_features:x%x", p_cb->features,
    562                       rc_open.peer_features);
    563     if(rc_open.peer_features == 0)
    564     {
    565         /* we have not done SDP on peer RC capabilities.
    566          * peer must have initiated the RC connection */
    567         rc_open.peer_features = BTA_AV_FEAT_RCCT;
    568         bta_av_rc_disc(disc);
    569     }
    570     (*p_cb->p_cback)(BTA_AV_RC_OPEN_EVT, (tBTA_AV *) &rc_open);
    571 
    572 }
    573 
    574 
    575 /*******************************************************************************
    576 **
    577 ** Function         bta_av_rc_remote_cmd
    578 **
    579 ** Description      Send an AVRCP remote control command.
    580 **
    581 ** Returns          void
    582 **
    583 *******************************************************************************/
    584 void bta_av_rc_remote_cmd(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
    585 {
    586     tBTA_AV_RCB    *p_rcb;
    587     if (p_cb->features & BTA_AV_FEAT_RCCT)
    588     {
    589         if(p_data->hdr.layer_specific < BTA_AV_NUM_RCB)
    590         {
    591             p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
    592             if(p_rcb->status & BTA_AV_RC_CONN_MASK)
    593             {
    594                 AVRC_PassCmd(p_rcb->handle, p_data->api_remote_cmd.label,
    595                      &p_data->api_remote_cmd.msg);
    596             }
    597         }
    598     }
    599 }
    600 
    601 /*******************************************************************************
    602 **
    603 ** Function         bta_av_rc_vendor_cmd
    604 **
    605 ** Description      Send an AVRCP vendor specific command.
    606 **
    607 ** Returns          void
    608 **
    609 *******************************************************************************/
    610 void bta_av_rc_vendor_cmd(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
    611 {
    612     tBTA_AV_RCB    *p_rcb;
    613     if ( (p_cb->features & (BTA_AV_FEAT_RCCT | BTA_AV_FEAT_VENDOR)) ==
    614          (BTA_AV_FEAT_RCCT | BTA_AV_FEAT_VENDOR))
    615     {
    616         if(p_data->hdr.layer_specific < BTA_AV_NUM_RCB)
    617         {
    618             p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
    619             AVRC_VendorCmd(p_rcb->handle, p_data->api_vendor.label, &p_data->api_vendor.msg);
    620         }
    621     }
    622 }
    623 
    624 /*******************************************************************************
    625 **
    626 ** Function         bta_av_rc_vendor_rsp
    627 **
    628 ** Description      Send an AVRCP vendor specific response.
    629 **
    630 ** Returns          void
    631 **
    632 *******************************************************************************/
    633 void bta_av_rc_vendor_rsp(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
    634 {
    635     tBTA_AV_RCB    *p_rcb;
    636     if ( (p_cb->features & (BTA_AV_FEAT_RCTG | BTA_AV_FEAT_VENDOR)) ==
    637          (BTA_AV_FEAT_RCTG | BTA_AV_FEAT_VENDOR))
    638     {
    639         if(p_data->hdr.layer_specific < BTA_AV_NUM_RCB)
    640         {
    641             p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
    642             AVRC_VendorRsp(p_rcb->handle, p_data->api_vendor.label, &p_data->api_vendor.msg);
    643         }
    644     }
    645 }
    646 
    647 /*******************************************************************************
    648 **
    649 ** Function         bta_av_rc_meta_rsp
    650 **
    651 ** Description      Send an AVRCP metadata/advanced control command/response.
    652 **
    653 ** Returns          void
    654 **
    655 *******************************************************************************/
    656 void bta_av_rc_meta_rsp(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
    657 {
    658     tBTA_AV_RCB *p_rcb;
    659     BOOLEAN         free = TRUE;
    660 
    661     if ((p_cb->features & BTA_AV_FEAT_METADATA) && (p_data->hdr.layer_specific < BTA_AV_NUM_RCB))
    662     {
    663         if ((p_data->api_meta_rsp.is_rsp && (p_cb->features & BTA_AV_FEAT_RCTG)) ||
    664             (!p_data->api_meta_rsp.is_rsp && (p_cb->features & BTA_AV_FEAT_RCCT)) )
    665         {
    666             p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
    667             AVRC_MsgReq(p_rcb->handle, p_data->api_meta_rsp.label, p_data->api_meta_rsp.rsp_code,
    668                                       p_data->api_meta_rsp.p_pkt);
    669             free = FALSE;
    670         }
    671     }
    672 
    673     if (free)
    674         GKI_freebuf (p_data->api_meta_rsp.p_pkt);
    675 }
    676 
    677 /*******************************************************************************
    678 **
    679 ** Function         bta_av_rc_free_rsp
    680 **
    681 ** Description      free an AVRCP metadata command buffer.
    682 **
    683 ** Returns          void
    684 **
    685 *******************************************************************************/
    686 void bta_av_rc_free_rsp (tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
    687 {
    688     GKI_freebuf (p_data->api_meta_rsp.p_pkt);
    689 }
    690 
    691 /*******************************************************************************
    692 **
    693 ** Function         bta_av_rc_meta_req
    694 **
    695 ** Description      Send an AVRCP metadata command.
    696 **
    697 ** Returns          void
    698 **
    699 *******************************************************************************/
    700 void bta_av_rc_free_msg (tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
    701 {
    702 }
    703 
    704 
    705 
    706 /*******************************************************************************
    707 **
    708 ** Function         bta_av_chk_notif_evt_id
    709 **
    710 ** Description      make sure the requested player id is valid.
    711 **
    712 ** Returns          BTA_AV_STS_NO_RSP, if no error
    713 **
    714 *******************************************************************************/
    715 static tAVRC_STS bta_av_chk_notif_evt_id(tAVRC_MSG_VENDOR *p_vendor)
    716 {
    717     tAVRC_STS   status = BTA_AV_STS_NO_RSP;
    718     UINT8       xx;
    719     UINT16      u16;
    720     UINT8       *p = p_vendor->p_vendor_data + 2;
    721 
    722     BE_STREAM_TO_UINT16 (u16, p);
    723     /* double check the fixed length */
    724     if ((u16 != 5) || (p_vendor->vendor_len != 9))
    725     {
    726         status = AVRC_STS_INTERNAL_ERR;
    727     }
    728     else
    729     {
    730         /* make sure the player_id is valid */
    731         for (xx=0; xx<p_bta_av_cfg->num_evt_ids; xx++)
    732         {
    733             if (*p == p_bta_av_cfg->p_meta_evt_ids[xx])
    734             {
    735                 break;
    736             }
    737         }
    738         if (xx == p_bta_av_cfg->num_evt_ids)
    739         {
    740             status = AVRC_STS_BAD_PARAM;
    741         }
    742     }
    743 
    744     return status;
    745 }
    746 
    747 /*******************************************************************************
    748 **
    749 ** Function         bta_av_proc_meta_cmd
    750 **
    751 ** Description      Process an AVRCP metadata command from the peer.
    752 **
    753 ** Returns          TRUE to respond immediately
    754 **
    755 *******************************************************************************/
    756 tBTA_AV_EVT bta_av_proc_meta_cmd(tAVRC_RESPONSE  *p_rc_rsp, tBTA_AV_RC_MSG *p_msg, UINT8 *p_ctype)
    757 {
    758     tBTA_AV_EVT evt = BTA_AV_META_MSG_EVT;
    759     UINT8       u8, pdu, *p;
    760     UINT16      u16;
    761     tAVRC_MSG_VENDOR    *p_vendor = &p_msg->msg.vendor;
    762 
    763 #if (AVRC_METADATA_INCLUDED == TRUE)
    764 
    765     pdu = *(p_vendor->p_vendor_data);
    766     p_rc_rsp->pdu = pdu;
    767     *p_ctype = AVRC_RSP_REJ;
    768     /* Metadata messages only use PANEL sub-unit type */
    769     if (p_vendor->hdr.subunit_type != AVRC_SUB_PANEL)
    770     {
    771         APPL_TRACE_DEBUG0("SUBUNIT must be PANEL");
    772         /* reject it */
    773         evt=0;
    774         p_vendor->hdr.ctype = BTA_AV_RSP_NOT_IMPL;
    775         AVRC_VendorRsp(p_msg->handle, p_msg->label, &p_msg->msg.vendor);
    776     }
    777     else if (!AVRC_IsValidAvcType(pdu, p_vendor->hdr.ctype) )
    778     {
    779         APPL_TRACE_DEBUG2("Invalid pdu/ctype: 0x%x, %d", pdu, p_vendor->hdr.ctype);
    780         /* reject invalid message without reporting to app */
    781         evt = 0;
    782         p_rc_rsp->rsp.status = AVRC_STS_BAD_CMD;
    783     }
    784     else
    785     {
    786         switch (pdu)
    787         {
    788         case AVRC_PDU_GET_CAPABILITIES:
    789             /* process GetCapabilities command without reporting the event to app */
    790             evt = 0;
    791             u8 = *(p_vendor->p_vendor_data + 4);
    792             p = p_vendor->p_vendor_data + 2;
    793             p_rc_rsp->get_caps.capability_id = u8;
    794             BE_STREAM_TO_UINT16 (u16, p);
    795             if ((u16 != 1) || (p_vendor->vendor_len != 5))
    796             {
    797                 p_rc_rsp->get_caps.status = AVRC_STS_INTERNAL_ERR;
    798             }
    799             else
    800             {
    801                 p_rc_rsp->get_caps.status = AVRC_STS_NO_ERROR;
    802                 if (u8 == AVRC_CAP_COMPANY_ID)
    803                 {
    804                     *p_ctype = AVRC_RSP_IMPL_STBL;
    805                     p_rc_rsp->get_caps.count = p_bta_av_cfg->num_co_ids;
    806                     memcpy(p_rc_rsp->get_caps.param.company_id, p_bta_av_cfg->p_meta_co_ids,
    807                            (p_bta_av_cfg->num_co_ids << 2));
    808                 }
    809                 else if (u8 == AVRC_CAP_EVENTS_SUPPORTED)
    810                 {
    811                     *p_ctype = AVRC_RSP_IMPL_STBL;
    812                     p_rc_rsp->get_caps.count = p_bta_av_cfg->num_evt_ids;
    813                     memcpy(p_rc_rsp->get_caps.param.event_id, p_bta_av_cfg->p_meta_evt_ids,
    814                            p_bta_av_cfg->num_evt_ids);
    815                 }
    816                 else
    817                 {
    818                     APPL_TRACE_DEBUG1("Invalid capability ID: 0x%x", u8);
    819                     /* reject - unknown capability ID */
    820                     p_rc_rsp->get_caps.status = AVRC_STS_BAD_PARAM;
    821                 }
    822             }
    823             break;
    824 
    825 
    826         case AVRC_PDU_REGISTER_NOTIFICATION:
    827             /* make sure the event_id is implemented */
    828             p_rc_rsp->rsp.status = bta_av_chk_notif_evt_id (p_vendor);
    829             if (p_rc_rsp->rsp.status != BTA_AV_STS_NO_RSP)
    830                 evt = 0;
    831             break;
    832 
    833         }
    834     }
    835 #else
    836     APPL_TRACE_DEBUG0("AVRCP 1.3 Metadata not supporteed. Reject command.");
    837     /* reject invalid message without reporting to app */
    838     evt = 0;
    839     p_rc_rsp->rsp.status = AVRC_STS_BAD_CMD;
    840 #endif
    841 
    842     return evt;
    843 }
    844 
    845 
    846 /*******************************************************************************
    847 **
    848 ** Function         bta_av_rc_msg
    849 **
    850 ** Description      Process an AVRCP message from the peer.
    851 **
    852 ** Returns          void
    853 **
    854 *******************************************************************************/
    855 void bta_av_rc_msg(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
    856 {
    857     tBTA_AV_EVT evt = 0;
    858     tBTA_AV     av;
    859     BT_HDR      *p_pkt = NULL;
    860     tAVRC_MSG_VENDOR    *p_vendor = &p_data->rc_msg.msg.vendor;
    861     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);
    862 #if (AVRC_METADATA_INCLUDED == TRUE)
    863     tAVRC_STS   res;
    864     UINT8       ctype;
    865     tAVRC_RESPONSE  rc_rsp;
    866 
    867     rc_rsp.rsp.status = BTA_AV_STS_NO_RSP;
    868 #endif
    869 
    870     if (p_data->rc_msg.opcode == AVRC_OP_PASS_THRU)
    871     {
    872     /* if this is a pass thru command */
    873         if ((p_data->rc_msg.msg.hdr.ctype == AVRC_CMD_CTRL) ||
    874             (p_data->rc_msg.msg.hdr.ctype == AVRC_CMD_SPEC_INQ) ||
    875             (p_data->rc_msg.msg.hdr.ctype == AVRC_CMD_GEN_INQ)
    876             )
    877         {
    878         /* check if operation is supported */
    879             if (p_data->rc_msg.msg.pass.op_id == AVRC_ID_VENDOR)
    880             {
    881                 p_data->rc_msg.msg.hdr.ctype = BTA_AV_RSP_NOT_IMPL;
    882 #if (AVRC_METADATA_INCLUDED == TRUE)
    883                 if (p_cb->features & BTA_AV_FEAT_METADATA)
    884                     p_data->rc_msg.msg.hdr.ctype =
    885                         bta_av_group_navi_supported(p_data->rc_msg.msg.pass.pass_len,
    886                         p_data->rc_msg.msg.pass.p_pass_data, is_inquiry);
    887 #endif
    888             }
    889             else
    890             {
    891                 p_data->rc_msg.msg.hdr.ctype = bta_av_op_supported(p_data->rc_msg.msg.pass.op_id, is_inquiry);
    892             }
    893 
    894             APPL_TRACE_DEBUG1("ctype %d",p_data->rc_msg.msg.hdr.ctype)
    895 
    896             /* send response */
    897             if (p_data->rc_msg.msg.hdr.ctype != BTA_AV_RSP_INTERIM)
    898                 AVRC_PassRsp(p_data->rc_msg.handle, p_data->rc_msg.label, &p_data->rc_msg.msg.pass);
    899 
    900             /* set up for callback if supported */
    901             if (p_data->rc_msg.msg.hdr.ctype == BTA_AV_RSP_ACCEPT || p_data->rc_msg.msg.hdr.ctype == BTA_AV_RSP_INTERIM)
    902             {
    903                 evt = BTA_AV_REMOTE_CMD_EVT;
    904                 av.remote_cmd.rc_id = p_data->rc_msg.msg.pass.op_id;
    905                 av.remote_cmd.key_state = p_data->rc_msg.msg.pass.state;
    906                 av.remote_cmd.p_data = p_data->rc_msg.msg.pass.p_pass_data;
    907                 av.remote_cmd.len = p_data->rc_msg.msg.pass.pass_len;
    908                 memcpy(&av.remote_cmd.hdr, &p_data->rc_msg.msg.hdr, sizeof (tAVRC_HDR));
    909                 av.remote_cmd.label = p_data->rc_msg.label;
    910             }
    911         }
    912         /* else if this is a pass thru response */
    913         else if (p_data->rc_msg.msg.hdr.ctype >= AVRC_RSP_ACCEPT)
    914         {
    915             /* set up for callback */
    916             evt = BTA_AV_REMOTE_RSP_EVT;
    917             av.remote_rsp.rc_id = p_data->rc_msg.msg.pass.op_id;
    918             av.remote_rsp.key_state = p_data->rc_msg.msg.pass.state;
    919             av.remote_rsp.rsp_code = p_data->rc_msg.msg.hdr.ctype;
    920             av.remote_rsp.label = p_data->rc_msg.label;
    921         }
    922         /* must be a bad ctype -> reject*/
    923         else
    924         {
    925             p_data->rc_msg.msg.hdr.ctype = BTA_AV_RSP_REJ;
    926             AVRC_PassRsp(p_data->rc_msg.handle, p_data->rc_msg.label, &p_data->rc_msg.msg.pass);
    927         }
    928     }
    929     /* else if this is a vendor specific command or response */
    930     else if (p_data->rc_msg.opcode == AVRC_OP_VENDOR)
    931     {
    932         /* set up for callback */
    933         av.vendor_cmd.code = p_data->rc_msg.msg.hdr.ctype;
    934         av.vendor_cmd.company_id = p_vendor->company_id;
    935         av.vendor_cmd.label = p_data->rc_msg.label;
    936         av.vendor_cmd.p_data = p_vendor->p_vendor_data;
    937         av.vendor_cmd.len = p_vendor->vendor_len;
    938 
    939         /* if configured to support vendor specific and it's a command */
    940         if ((p_cb->features & BTA_AV_FEAT_VENDOR)  &&
    941             p_data->rc_msg.msg.hdr.ctype <= AVRC_CMD_GEN_INQ)
    942         {
    943 #if (AVRC_METADATA_INCLUDED == TRUE)
    944             if ((p_cb->features & BTA_AV_FEAT_METADATA) &&
    945                (p_vendor->company_id == AVRC_CO_METADATA))
    946             {
    947                 av.meta_msg.p_msg = &p_data->rc_msg.msg;
    948                 evt = bta_av_proc_meta_cmd (&rc_rsp, &p_data->rc_msg, &ctype);
    949             }
    950             else
    951 #endif
    952                 evt = BTA_AV_VENDOR_CMD_EVT;
    953         }
    954         /* else if configured to support vendor specific and it's a response */
    955         else if ((p_cb->features & BTA_AV_FEAT_VENDOR) &&
    956                  p_data->rc_msg.msg.hdr.ctype >= AVRC_RSP_ACCEPT)
    957         {
    958 #if (AVRC_METADATA_INCLUDED == TRUE)
    959             if ((p_cb->features & BTA_AV_FEAT_METADATA) &&
    960                (p_vendor->company_id == AVRC_CO_METADATA))
    961             {
    962                 av.meta_msg.p_msg = &p_data->rc_msg.msg;
    963                 evt = BTA_AV_META_MSG_EVT;
    964             }
    965             else
    966 #endif
    967                 evt = BTA_AV_VENDOR_RSP_EVT;
    968 
    969         }
    970         /* else if not configured to support vendor specific and it's a command */
    971         else if (!(p_cb->features & BTA_AV_FEAT_VENDOR)  &&
    972             p_data->rc_msg.msg.hdr.ctype <= AVRC_CMD_GEN_INQ)
    973         {
    974            if(p_data->rc_msg.msg.vendor.p_vendor_data[0] == AVRC_PDU_INVALID)
    975            {
    976            /* reject it */
    977               p_data->rc_msg.msg.hdr.ctype = BTA_AV_RSP_REJ;
    978               p_data->rc_msg.msg.vendor.p_vendor_data[4] = AVRC_STS_BAD_CMD;
    979            }
    980            else
    981               p_data->rc_msg.msg.hdr.ctype = BTA_AV_RSP_NOT_IMPL;
    982            AVRC_VendorRsp(p_data->rc_msg.handle, p_data->rc_msg.label, &p_data->rc_msg.msg.vendor);
    983         }
    984     }
    985 #if (AVRC_METADATA_INCLUDED == TRUE)
    986     if (evt == 0 && rc_rsp.rsp.status != BTA_AV_STS_NO_RSP)
    987     {
    988         if (!p_pkt)
    989         {
    990             rc_rsp.rsp.opcode = p_data->rc_msg.opcode;
    991             res = AVRC_BldResponse (0, &rc_rsp, &p_pkt);
    992         }
    993         if (p_pkt)
    994             AVRC_MsgReq (p_data->rc_msg.handle, p_data->rc_msg.label, ctype, p_pkt);
    995     }
    996 #endif
    997 
    998     /* call callback */
    999     if (evt != 0)
   1000     {
   1001         av.remote_cmd.rc_handle = p_data->rc_msg.handle;
   1002         (*p_cb->p_cback)(evt, &av);
   1003     }
   1004 }
   1005 
   1006 /*******************************************************************************
   1007 **
   1008 ** Function         bta_av_rc_close
   1009 **
   1010 ** Description      close the specified AVRC handle.
   1011 **
   1012 ** Returns          void
   1013 **
   1014 *******************************************************************************/
   1015 void bta_av_rc_close (tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
   1016 {
   1017     UINT16 handle = p_data->hdr.layer_specific;
   1018     tBTA_AV_SCB  *p_scb;
   1019     tBTA_AV_RCB *p_rcb;
   1020 
   1021     if(handle < BTA_AV_NUM_RCB)
   1022     {
   1023         p_rcb = &p_cb->rcb[handle];
   1024 
   1025         APPL_TRACE_DEBUG2("bta_av_rc_close handle: %d, status=0x%x", p_rcb->handle, p_rcb->status);
   1026         if(p_rcb->handle != BTA_AV_RC_HANDLE_NONE)
   1027         {
   1028             if(p_rcb->shdl)
   1029             {
   1030                 p_scb = bta_av_cb.p_scb[p_rcb->shdl - 1];
   1031                 if(p_scb)
   1032                 {
   1033                     /* just in case the RC timer is active
   1034                     if(bta_av_cb.features & BTA_AV_FEAT_RCCT &&
   1035                        p_scb->chnl == BTA_AV_CHNL_AUDIO) */
   1036                         bta_sys_stop_timer(&p_scb->timer);
   1037                 }
   1038             }
   1039 
   1040             AVRC_Close(p_rcb->handle);
   1041         }
   1042     }
   1043 }
   1044 
   1045 /*******************************************************************************
   1046 **
   1047 ** Function         bta_av_get_shdl
   1048 **
   1049 ** Returns          The index to p_scb[]
   1050 **
   1051 *******************************************************************************/
   1052 static UINT8 bta_av_get_shdl(tBTA_AV_SCB *p_scb)
   1053 {
   1054     int     i;
   1055     UINT8   shdl = 0;
   1056     /* find the SCB & stop the timer */
   1057     for(i=0; i<BTA_AV_NUM_STRS; i++)
   1058     {
   1059         if(p_scb == bta_av_cb.p_scb[i])
   1060         {
   1061             shdl = i+1;
   1062             break;
   1063         }
   1064     }
   1065     return shdl;
   1066 }
   1067 
   1068 /*******************************************************************************
   1069 **
   1070 ** Function         bta_av_stream_chg
   1071 **
   1072 ** Description      audio streaming status changed.
   1073 **
   1074 ** Returns          void
   1075 **
   1076 *******************************************************************************/
   1077 void bta_av_stream_chg(tBTA_AV_SCB *p_scb, BOOLEAN started)
   1078 {
   1079     UINT8   started_msk;
   1080     int     i;
   1081     UINT8   *p_streams;
   1082     BOOLEAN no_streams = FALSE;
   1083     tBTA_AV_SCB *p_scbi;
   1084 
   1085     started_msk = BTA_AV_HNDL_TO_MSK(p_scb->hdi);
   1086     APPL_TRACE_DEBUG3 ("bta_av_stream_chg started:%d started_msk:x%x chnl:x%x", started,
   1087                                                   started_msk, p_scb->chnl);
   1088     if (BTA_AV_CHNL_AUDIO == p_scb->chnl)
   1089         p_streams = &bta_av_cb.audio_streams;
   1090     else
   1091         p_streams = &bta_av_cb.video_streams;
   1092 
   1093     if (started)
   1094     {
   1095         /* Let L2CAP know this channel is processed with high priority */
   1096         L2CA_SetAclPriority(p_scb->peer_addr, L2CAP_PRIORITY_HIGH);
   1097         (*p_streams) |= started_msk;
   1098     }
   1099     else
   1100     {
   1101         (*p_streams) &= ~started_msk;
   1102     }
   1103 
   1104     if (!started)
   1105     {
   1106         i=0;
   1107         if (BTA_AV_CHNL_AUDIO == p_scb->chnl)
   1108         {
   1109             if (bta_av_cb.video_streams == 0)
   1110                 no_streams = TRUE;
   1111         }
   1112         else
   1113         {
   1114             no_streams = TRUE;
   1115             if ( bta_av_cb.audio_streams )
   1116             {
   1117                 for (; i<BTA_AV_NUM_STRS; i++)
   1118                 {
   1119                     p_scbi = bta_av_cb.p_scb[i];
   1120                     /* scb is used and started */
   1121                     if ( p_scbi && (bta_av_cb.audio_streams & BTA_AV_HNDL_TO_MSK(i))
   1122                         && bdcmp(p_scbi->peer_addr, p_scb->peer_addr) == 0)
   1123                     {
   1124                         no_streams = FALSE;
   1125                         break;
   1126                     }
   1127                 }
   1128 
   1129             }
   1130         }
   1131 
   1132         APPL_TRACE_DEBUG4 ("no_streams:%d i:%d, audio_streams:x%x, video_streams:x%x", no_streams, i,
   1133                            bta_av_cb.audio_streams, bta_av_cb.video_streams);
   1134         if (no_streams)
   1135         {
   1136             /* Let L2CAP know this channel is processed with low priority */
   1137             L2CA_SetAclPriority(p_scb->peer_addr, L2CAP_PRIORITY_NORMAL);
   1138         }
   1139     }
   1140 }
   1141 
   1142 
   1143 /*******************************************************************************
   1144 **
   1145 ** Function         bta_av_conn_chg
   1146 **
   1147 ** Description      connetion status changed.
   1148 **                  Open an AVRCP acceptor channel, if new conn.
   1149 **
   1150 ** Returns          void
   1151 **
   1152 *******************************************************************************/
   1153 void bta_av_conn_chg(tBTA_AV_DATA *p_data)
   1154 {
   1155     tBTA_AV_CB   *p_cb = &bta_av_cb;
   1156     tBTA_AV_SCB     *p_scb;
   1157     tBTA_AV_SCB     *p_scbi;
   1158     UINT8   mask;
   1159     UINT8   conn_msk;
   1160     UINT8   old_msk;
   1161     int i;
   1162     int index = (p_data->hdr.layer_specific & BTA_AV_HNDL_MSK) - 1;
   1163     tBTA_AV_LCB *p_lcb;
   1164     tBTA_AV_LCB *p_lcb_rc;
   1165     tBTA_AV_RCB *p_rcb, *p_rcb2;
   1166     BOOLEAN     chk_restore = FALSE;
   1167 
   1168     p_scb = p_cb->p_scb[index];
   1169 
   1170     mask = BTA_AV_HNDL_TO_MSK(index);
   1171     p_lcb = bta_av_find_lcb(p_data->conn_chg.peer_addr, BTA_AV_LCB_FIND);
   1172     conn_msk = 1 << (index + 1);
   1173     if(p_data->conn_chg.is_up)
   1174     {
   1175         /* set the conned mask for this channel */
   1176         if(p_scb)
   1177         {
   1178             if(p_lcb)
   1179             {
   1180                 p_lcb->conn_msk |= conn_msk;
   1181                 for (i=0; i<BTA_AV_NUM_RCB; i++)
   1182                 {
   1183                     if (bta_av_cb.rcb[i].lidx == p_lcb->lidx)
   1184                     {
   1185                         bta_av_cb.rcb[i].shdl = index + 1;
   1186                         APPL_TRACE_DEBUG5("conn_chg up[%d]: %d, status=0x%x, shdl:%d, lidx:%d", i,
   1187                                           bta_av_cb.rcb[i].handle, bta_av_cb.rcb[i].status,
   1188                                           bta_av_cb.rcb[i].shdl, bta_av_cb.rcb[i].lidx);
   1189                         break;
   1190                     }
   1191                 }
   1192             }
   1193             if (p_scb->chnl == BTA_AV_CHNL_AUDIO)
   1194             {
   1195                 old_msk = p_cb->conn_audio;
   1196                 p_cb->conn_audio |= mask;
   1197             }
   1198             else
   1199             {
   1200                 old_msk = p_cb->conn_video;
   1201                 p_cb->conn_video |= mask;
   1202             }
   1203 
   1204             if ((old_msk & mask) == 0)
   1205             {
   1206                 /* increase the audio open count, if not set yet */
   1207                 bta_av_cb.audio_open_cnt++;
   1208             }
   1209 
   1210 
   1211             APPL_TRACE_DEBUG2("rc_acp_handle:%d rc_acp_idx:%d", p_cb->rc_acp_handle, p_cb->rc_acp_idx);
   1212             /* check if the AVRCP ACP channel is already connected */
   1213             if(p_lcb && p_cb->rc_acp_handle != BTA_AV_RC_HANDLE_NONE && p_cb->rc_acp_idx)
   1214             {
   1215                 p_lcb_rc = &p_cb->lcb[BTA_AV_NUM_LINKS];
   1216                 APPL_TRACE_DEBUG1("rc_acp is connected && conn_chg on same addr p_lcb_rc->conn_msk:x%x",
   1217                                   p_lcb_rc->conn_msk);
   1218                 /* check if the RC is connected to the scb addr */
   1219                 APPL_TRACE_DEBUG6 ("p_lcb_rc->addr: %02x:%02x:%02x:%02x:%02x:%02x",
   1220                        p_lcb_rc->addr[0], p_lcb_rc->addr[1], p_lcb_rc->addr[2], p_lcb_rc->addr[3],
   1221                        p_lcb_rc->addr[4], p_lcb_rc->addr[5]);
   1222                 APPL_TRACE_DEBUG6 ("conn_chg.peer_addr: %02x:%02x:%02x:%02x:%02x:%02x",
   1223                        p_data->conn_chg.peer_addr[0], p_data->conn_chg.peer_addr[1],
   1224                        p_data->conn_chg.peer_addr[2],
   1225                        p_data->conn_chg.peer_addr[3], p_data->conn_chg.peer_addr[4],
   1226                        p_data->conn_chg.peer_addr[5]);
   1227                 if (p_lcb_rc->conn_msk && bdcmp(p_lcb_rc->addr, p_data->conn_chg.peer_addr) == 0)
   1228                 {
   1229                     /* AVRCP is already connected.
   1230                      * need to update the association betwen SCB and RCB */
   1231                     p_lcb_rc->conn_msk = 0; /* indicate RC ONLY is not connected */
   1232                     p_lcb_rc->lidx = 0;
   1233                     p_scb->rc_handle = p_cb->rc_acp_handle;
   1234                     p_rcb = &p_cb->rcb[p_cb->rc_acp_idx - 1];
   1235                     p_rcb->shdl = bta_av_get_shdl(p_scb);
   1236                     APPL_TRACE_DEBUG3("update rc_acp shdl:%d/%d srch:%d", index + 1, p_rcb->shdl,
   1237                                       p_scb->rc_handle );
   1238 
   1239                     p_rcb2 = bta_av_get_rcb_by_shdl(p_rcb->shdl);
   1240                     if (p_rcb2)
   1241                     {
   1242                         /* found the RCB that was created to associated with this SCB */
   1243                         p_cb->rc_acp_handle = p_rcb2->handle;
   1244                         p_cb->rc_acp_idx = (p_rcb2 - p_cb->rcb) + 1;
   1245                         APPL_TRACE_DEBUG2("new rc_acp_handle:%d, idx:%d", p_cb->rc_acp_handle,
   1246                                            p_cb->rc_acp_idx);
   1247                         p_rcb2->lidx = (BTA_AV_NUM_LINKS + 1);
   1248                         APPL_TRACE_DEBUG3("rc2 handle:%d lidx:%d/%d",p_rcb2->handle, p_rcb2->lidx,
   1249                                           p_cb->lcb[p_rcb2->lidx-1].lidx);
   1250                     }
   1251                     p_rcb->lidx = p_lcb->lidx;
   1252                     APPL_TRACE_DEBUG3("rc handle:%d lidx:%d/%d",p_rcb->handle, p_rcb->lidx,
   1253                                       p_cb->lcb[p_rcb->lidx-1].lidx);
   1254                 }
   1255             }
   1256         }
   1257     }
   1258     else
   1259     {
   1260         if ((p_cb->conn_audio & mask) && bta_av_cb.audio_open_cnt)
   1261         {
   1262             /* this channel is still marked as open. decrease the count */
   1263             bta_av_cb.audio_open_cnt--;
   1264         }
   1265 
   1266         /* clear the conned mask for this channel */
   1267         p_cb->conn_audio &= ~mask;
   1268         p_cb->conn_video &= ~mask;
   1269         if(p_scb)
   1270         {
   1271             /* the stream is closed.
   1272              * clear the peer address, so it would not mess up the AVRCP for the next round of operation */
   1273             bdcpy(p_scb->peer_addr, bd_addr_null);
   1274             if(p_scb->chnl == BTA_AV_CHNL_AUDIO)
   1275             {
   1276                 if(p_lcb)
   1277                 {
   1278                     p_lcb->conn_msk &= ~conn_msk;
   1279                 }
   1280                 /* audio channel is down. make sure the INT channel is down */
   1281                 /* just in case the RC timer is active
   1282                 if(p_cb->features & BTA_AV_FEAT_RCCT) */
   1283                 {
   1284                     bta_sys_stop_timer(&p_scb->timer);
   1285                 }
   1286                 /* one audio channel goes down. check if we need to restore high priority */
   1287                 chk_restore = TRUE;
   1288             }
   1289         }
   1290 
   1291         APPL_TRACE_DEBUG1("bta_av_conn_chg shdl:%d", index + 1);
   1292         for (i=0; i<BTA_AV_NUM_RCB; i++)
   1293         {
   1294             APPL_TRACE_DEBUG5("conn_chg dn[%d]: %d, status=0x%x, shdl:%d, lidx:%d", i,
   1295                               bta_av_cb.rcb[i].handle, bta_av_cb.rcb[i].status,
   1296                               bta_av_cb.rcb[i].shdl, bta_av_cb.rcb[i].lidx);
   1297             if(bta_av_cb.rcb[i].shdl == index + 1)
   1298             {
   1299                 bta_av_del_rc(&bta_av_cb.rcb[i]);
   1300                 break;
   1301             }
   1302         }
   1303 
   1304         if(p_cb->conn_audio == 0 && p_cb->conn_video == 0)
   1305         {
   1306             /* if both channels are not connected,
   1307              * close all RC channels */
   1308             bta_av_close_all_rc(p_cb);
   1309         }
   1310 
   1311         /* if the AVRCP is no longer listening, create the listening channel */
   1312         if (bta_av_cb.rc_acp_handle == BTA_AV_RC_HANDLE_NONE && bta_av_cb.features & BTA_AV_FEAT_RCTG)
   1313             bta_av_rc_create(&bta_av_cb, AVCT_ACP, 0, BTA_AV_NUM_LINKS + 1);
   1314     }
   1315 
   1316     APPL_TRACE_DEBUG6("bta_av_conn_chg audio:%x video:%x up:%d conn_msk:0x%x chk_restore:%d audio_open_cnt:%d",
   1317         p_cb->conn_audio, p_cb->conn_video, p_data->conn_chg.is_up, conn_msk, chk_restore, p_cb->audio_open_cnt);
   1318 
   1319     if (chk_restore)
   1320     {
   1321         if (p_cb->audio_open_cnt == 1)
   1322         {
   1323             /* one audio channel goes down and there's one audio channel remains open.
   1324              * restore the switch role in default link policy */
   1325             bta_sys_set_default_policy(BTA_ID_AV, HCI_ENABLE_MASTER_SLAVE_SWITCH);
   1326             /* allow role switch, if this is the last connection */
   1327             bta_av_restore_switch();
   1328         }
   1329         if (p_cb->audio_open_cnt)
   1330         {
   1331             /* adjust flush timeout settings to longer period */
   1332             for (i=0; i<BTA_AV_NUM_STRS; i++)
   1333             {
   1334                 p_scbi = bta_av_cb.p_scb[i];
   1335                 if (p_scbi && p_scbi->chnl == BTA_AV_CHNL_AUDIO && p_scbi->co_started)
   1336                 {
   1337                     /* may need to update the flush timeout of this already started stream */
   1338                     if (p_scbi->co_started != bta_av_cb.audio_open_cnt)
   1339                     {
   1340                         p_scbi->co_started = bta_av_cb.audio_open_cnt;
   1341                         L2CA_SetFlushTimeout(p_scbi->peer_addr, p_bta_av_cfg->p_audio_flush_to[p_scbi->co_started - 1] );
   1342                     }
   1343                 }
   1344             }
   1345         }
   1346     }
   1347 }
   1348 
   1349 /*******************************************************************************
   1350 **
   1351 ** Function         bta_av_disable
   1352 **
   1353 ** Description      disable AV.
   1354 **
   1355 ** Returns          void
   1356 **
   1357 *******************************************************************************/
   1358 void bta_av_disable(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
   1359 {
   1360     BT_HDR  hdr;
   1361     UINT16  xx;
   1362 
   1363     p_cb->disabling = TRUE;
   1364 
   1365     bta_av_close_all_rc(p_cb);
   1366 
   1367     utl_freebuf((void **) &p_cb->p_disc_db);
   1368 
   1369     /* disable audio/video - de-register all channels,
   1370      * expect BTA_AV_DEREG_COMP_EVT when deregister is complete */
   1371     for(xx=0; xx<BTA_AV_NUM_STRS; xx++)
   1372     {
   1373         hdr.layer_specific = xx + 1;
   1374         bta_av_api_deregister((tBTA_AV_DATA *)&hdr);
   1375     }
   1376 }
   1377 
   1378 /*******************************************************************************
   1379 **
   1380 ** Function         bta_av_api_disconnect
   1381 **
   1382 ** Description      .
   1383 **
   1384 ** Returns          void
   1385 **
   1386 *******************************************************************************/
   1387 void bta_av_api_disconnect(tBTA_AV_DATA *p_data)
   1388 {
   1389     AVDT_DisconnectReq(p_data->api_discnt.bd_addr, bta_av_conn_cback);
   1390     bta_sys_stop_timer(&bta_av_cb.sig_tmr);
   1391 }
   1392 
   1393 /*******************************************************************************
   1394 **
   1395 ** Function         bta_av_sig_chg
   1396 **
   1397 ** Description      process AVDT signal channel up/down.
   1398 **
   1399 ** Returns          void
   1400 **
   1401 *******************************************************************************/
   1402 void bta_av_sig_chg(tBTA_AV_DATA *p_data)
   1403 {
   1404     UINT16 event = p_data->str_msg.hdr.layer_specific;
   1405     tBTA_AV_CB   *p_cb = &bta_av_cb;
   1406     int     xx;
   1407     UINT8   mask;
   1408     tBTA_AV_LCB *p_lcb = NULL;
   1409 
   1410     APPL_TRACE_DEBUG1("bta_av_sig_chg event: %d", event);
   1411     if(event == AVDT_CONNECT_IND_EVT)
   1412     {
   1413         p_lcb = bta_av_find_lcb(p_data->str_msg.bd_addr, BTA_AV_LCB_FIND);
   1414         if(!p_lcb)
   1415         {
   1416             /* if the address does not have an LCB yet, alloc one */
   1417             for(xx=0; xx<BTA_AV_NUM_LINKS; xx++)
   1418             {
   1419                 mask = 1 << xx;
   1420                 APPL_TRACE_DEBUG1("conn_lcb: 0x%x", p_cb->conn_lcb);
   1421 
   1422                 /* look for a p_lcb with its p_scb registered */
   1423                 if((!(mask & p_cb->conn_lcb)) && (p_cb->p_scb[xx] != NULL))
   1424                 {
   1425                     p_lcb = &p_cb->lcb[xx];
   1426                     p_lcb->lidx = xx + 1;
   1427                     bdcpy(p_lcb->addr, p_data->str_msg.bd_addr);
   1428                     p_lcb->conn_msk = 0; /* clear the connect mask */
   1429                     /* start listening when the signal channel is open */
   1430                     if (p_cb->features & BTA_AV_FEAT_RCTG)
   1431                     {
   1432                         bta_av_rc_create(p_cb, AVCT_ACP, 0, p_lcb->lidx);
   1433                     }
   1434                     /* this entry is not used yet. */
   1435                     p_cb->conn_lcb |= mask;     /* mark it as used */
   1436                     APPL_TRACE_DEBUG1("start sig timer %d", p_data->hdr.offset);
   1437                     if (p_data->hdr.offset == AVDT_ACP)
   1438                     {
   1439                         APPL_TRACE_DEBUG1("Incoming L2CAP acquired, set state as incoming", NULL);
   1440                         bdcpy(p_cb->p_scb[xx]->peer_addr, p_data->str_msg.bd_addr);
   1441                         p_cb->p_scb[xx]->use_rc = TRUE;     /* allowing RC for incoming connection */
   1442                         bta_av_ssm_execute(p_cb->p_scb[xx], BTA_AV_ACP_CONNECT_EVT, p_data);
   1443 
   1444                         /* The Pending Event should be sent as soon as the L2CAP signalling channel
   1445                          * is set up, which is NOW. Earlier this was done only after
   1446                          * BTA_AV_SIG_TIME_VAL milliseconds.
   1447                          * The following function shall send the event and start the recurring timer
   1448                          */
   1449                         bta_av_sig_timer(NULL);
   1450 
   1451                         /* Possible collision : need to avoid outgoing processing while the timer is running */
   1452                         p_cb->p_scb[xx]->coll_mask = BTA_AV_COLL_INC_TMR;
   1453 
   1454                         p_cb->acp_sig_tmr.param = (UINT32)xx;
   1455                         p_cb->acp_sig_tmr.p_cback = (TIMER_CBACK*)&bta_av_acp_sig_timer_cback;
   1456                         bta_sys_start_timer(&p_cb->acp_sig_tmr, 0, BTA_AV_ACP_SIG_TIME_VAL);
   1457                     }
   1458                     break;
   1459                 }
   1460             }
   1461 
   1462             /* check if we found something */
   1463             if (xx == BTA_AV_NUM_LINKS)
   1464             {
   1465                 /* We do not have scb for this avdt connection.     */
   1466                 /* Silently close the connection.                   */
   1467                 APPL_TRACE_ERROR0("av scb not available for avdt connection");
   1468                 AVDT_DisconnectReq (p_data->str_msg.bd_addr, NULL);
   1469                 return;
   1470             }
   1471         }
   1472     }
   1473 #if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
   1474     else if (event == BTA_AR_AVDT_CONN_EVT)
   1475     {
   1476         bta_sys_stop_timer(&bta_av_cb.sig_tmr);
   1477     }
   1478 #endif
   1479     else
   1480     {
   1481         /* disconnected. */
   1482         p_lcb = bta_av_find_lcb(p_data->str_msg.bd_addr, BTA_AV_LCB_FREE);
   1483         if(p_lcb && p_lcb->conn_msk)
   1484         {
   1485             APPL_TRACE_DEBUG1("conn_msk: 0x%x", p_lcb->conn_msk);
   1486             /* clean up ssm  */
   1487             for(xx=0; xx < BTA_AV_NUM_STRS; xx++)
   1488             {
   1489                 mask = 1 << (xx + 1);
   1490                 if ((mask & p_lcb->conn_msk) && (p_cb->p_scb[xx]) &&
   1491                     (bdcmp(p_cb->p_scb[xx]->peer_addr, p_data->str_msg.bd_addr) == 0))
   1492                 {
   1493                     bta_av_ssm_execute(p_cb->p_scb[xx], BTA_AV_AVDT_DISCONNECT_EVT, NULL);
   1494                 }
   1495             }
   1496         }
   1497     }
   1498     APPL_TRACE_DEBUG1("conn_lcb: 0x%x", p_cb->conn_lcb);
   1499 }
   1500 
   1501 /*******************************************************************************
   1502 **
   1503 ** Function         bta_av_sig_timer
   1504 **
   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 BTA_AV_SIG_TIME_VAL
   1508 **
   1509 ** Returns          void
   1510 **
   1511 *******************************************************************************/
   1512 void bta_av_sig_timer(tBTA_AV_DATA *p_data)
   1513 {
   1514     tBTA_AV_CB   *p_cb = &bta_av_cb;
   1515     int     xx;
   1516     UINT8   mask;
   1517     tBTA_AV_LCB *p_lcb = NULL;
   1518     tBTA_AV_PEND pend;
   1519 
   1520     APPL_TRACE_DEBUG0("bta_av_sig_timer");
   1521     for(xx=0; xx<BTA_AV_NUM_LINKS; xx++)
   1522     {
   1523         mask = 1 << xx;
   1524         if(mask & p_cb->conn_lcb)
   1525         {
   1526             /* this entry is used. check if it is connected */
   1527             p_lcb = &p_cb->lcb[xx];
   1528             if(!p_lcb->conn_msk)
   1529             {
   1530                 bta_sys_start_timer(&p_cb->sig_tmr, BTA_AV_SIG_TIMER_EVT, BTA_AV_SIG_TIME_VAL);
   1531                 bdcpy(pend.bd_addr, p_lcb->addr);
   1532                 (*p_cb->p_cback)(BTA_AV_PENDING_EVT, (tBTA_AV *) &pend);
   1533             }
   1534         }
   1535     }
   1536 }
   1537 
   1538 /*******************************************************************************
   1539 **
   1540 ** Function         bta_av_acp_sig_timer_cback
   1541 **
   1542 ** Description      Process the timeout when SRC is accepting connection
   1543 **                  and SNK did not start signalling.
   1544 **
   1545 ** Returns          void
   1546 **
   1547 *******************************************************************************/
   1548 static void bta_av_acp_sig_timer_cback (TIMER_LIST_ENT *p_tle)
   1549 {
   1550     UINT8   inx = (UINT8)p_tle->param;
   1551     tBTA_AV_CB  *p_cb = &bta_av_cb;
   1552     tBTA_AV_SCB *p_scb = p_cb->p_scb[inx];
   1553     tBTA_AV_API_OPEN  *p_buf;
   1554 
   1555     if (p_scb)
   1556     {
   1557         APPL_TRACE_DEBUG1("bta_av_acp_sig_timer_cback, coll_mask = 0x%02X", p_scb->coll_mask);
   1558 
   1559         if (p_scb->coll_mask & BTA_AV_COLL_INC_TMR)
   1560         {
   1561             p_scb->coll_mask &= ~BTA_AV_COLL_INC_TMR;
   1562 
   1563             if (bta_av_is_scb_opening(p_scb))
   1564             {
   1565                 if (p_scb->p_disc_db)
   1566                 {
   1567                     /* We are still doing SDP. Run the timer again. */
   1568                     p_scb->coll_mask |= BTA_AV_COLL_INC_TMR;
   1569 
   1570                     p_cb->acp_sig_tmr.param = (UINT32)inx;
   1571                     p_cb->acp_sig_tmr.p_cback = (TIMER_CBACK *)&bta_av_acp_sig_timer_cback;
   1572                     bta_sys_start_timer(&p_cb->acp_sig_tmr, 0, BTA_AV_ACP_SIG_TIME_VAL);
   1573                 }
   1574                 else
   1575                 {
   1576                     /* SNK did not start signalling, resume signalling process. */
   1577                     bta_av_discover_req (p_scb, NULL);
   1578                 }
   1579             }
   1580             else if (bta_av_is_scb_incoming(p_scb))
   1581             {
   1582                 /* Stay in incoming state if SNK does not start signalling */
   1583 
   1584                 /* API open was called right after SNK opened L2C connection. */
   1585                 if (p_scb->coll_mask & BTA_AV_COLL_API_CALLED)
   1586                 {
   1587                     p_scb->coll_mask &= ~BTA_AV_COLL_API_CALLED;
   1588 
   1589                     /* BTA_AV_API_OPEN_EVT */
   1590                     if ((p_buf = (tBTA_AV_API_OPEN *) GKI_getbuf(sizeof(tBTA_AV_API_OPEN))) != NULL)
   1591                     {
   1592                         memcpy(p_buf, &(p_scb->open_api), sizeof(tBTA_AV_API_OPEN));
   1593                         bta_sys_sendmsg(p_buf);
   1594                     }
   1595                 }
   1596             }
   1597         }
   1598     }
   1599 }
   1600 
   1601 /*******************************************************************************
   1602 **
   1603 ** Function         bta_av_check_peer_features
   1604 **
   1605 ** Description      check supported features on the peer device from the SDP record
   1606 **                  and return the feature mask
   1607 **
   1608 ** Returns          tBTA_AV_FEAT peer device feature mask
   1609 **
   1610 *******************************************************************************/
   1611 tBTA_AV_FEAT bta_av_check_peer_features (UINT16 service_uuid)
   1612 {
   1613     tBTA_AV_FEAT peer_features = 0;
   1614     tBTA_AV_CB   *p_cb = &bta_av_cb;
   1615     tSDP_DISC_REC       *p_rec = NULL;
   1616     tSDP_DISC_ATTR      *p_attr;
   1617     UINT16              peer_rc_version=0;
   1618     UINT16              categories = 0;
   1619 
   1620     APPL_TRACE_DEBUG1("bta_av_check_peer_features service_uuid:x%x", service_uuid);
   1621     /* loop through all records we found */
   1622     while (TRUE)
   1623     {
   1624         /* get next record; if none found, we're done */
   1625         if ((p_rec = SDP_FindServiceInDb(p_cb->p_disc_db, service_uuid, p_rec)) == NULL)
   1626         {
   1627             break;
   1628         }
   1629 
   1630         if (( SDP_FindAttributeInRec(p_rec, ATTR_ID_SERVICE_CLASS_ID_LIST)) != NULL)
   1631         {
   1632             /* find peer features */
   1633             if (SDP_FindServiceInDb(p_cb->p_disc_db, UUID_SERVCLASS_AV_REMOTE_CONTROL, NULL))
   1634             {
   1635                 peer_features |= BTA_AV_FEAT_RCCT;
   1636             }
   1637             if (SDP_FindServiceInDb(p_cb->p_disc_db, UUID_SERVCLASS_AV_REM_CTRL_TARGET, NULL))
   1638             {
   1639                 peer_features |= BTA_AV_FEAT_RCTG;
   1640             }
   1641         }
   1642 
   1643         if (( SDP_FindAttributeInRec(p_rec, ATTR_ID_BT_PROFILE_DESC_LIST)) != NULL)
   1644         {
   1645             /* get profile version (if failure, version parameter is not updated) */
   1646             SDP_FindProfileVersionInRec(p_rec, UUID_SERVCLASS_AV_REMOTE_CONTROL, &peer_rc_version);
   1647             APPL_TRACE_DEBUG1("peer_rc_version 0x%x", peer_rc_version);
   1648 
   1649             if (peer_rc_version >= AVRC_REV_1_3)
   1650                 peer_features |= (BTA_AV_FEAT_VENDOR | BTA_AV_FEAT_METADATA);
   1651 
   1652             if (peer_rc_version >= AVRC_REV_1_4)
   1653             {
   1654                 peer_features |= (BTA_AV_FEAT_ADV_CTRL);
   1655                 /* get supported categories */
   1656                 if ((p_attr = SDP_FindAttributeInRec(p_rec,
   1657                                 ATTR_ID_SUPPORTED_FEATURES)) != NULL)
   1658                 {
   1659                     categories = p_attr->attr_value.v.u16;
   1660                     if (categories & AVRC_SUPF_CT_BROWSE)
   1661                         peer_features |= (BTA_AV_FEAT_BROWSE);
   1662                 }
   1663             }
   1664         }
   1665     }
   1666     APPL_TRACE_DEBUG1("peer_features:x%x", peer_features);
   1667     return peer_features;
   1668 }
   1669 
   1670 /*******************************************************************************
   1671 **
   1672 ** Function         bta_av_rc_disc_done
   1673 **
   1674 ** Description      Handle AVRCP service discovery results.  If matching
   1675 **                  service found, open AVRCP connection.
   1676 **
   1677 ** Returns          void
   1678 **
   1679 *******************************************************************************/
   1680 void bta_av_rc_disc_done(tBTA_AV_DATA *p_data)
   1681 {
   1682     tBTA_AV_CB   *p_cb = &bta_av_cb;
   1683     tBTA_AV_SCB  *p_scb = NULL;
   1684     tBTA_AV_LCB  *p_lcb;
   1685     tBTA_AV_RC_OPEN rc_open;
   1686     tBTA_AV_RC_FEAT rc_feat;
   1687     UINT8               rc_handle;
   1688     tBTA_AV_FEAT        peer_features;  /* peer features mask */
   1689 
   1690     APPL_TRACE_DEBUG1("bta_av_rc_disc_done disc:x%x", p_cb->disc);
   1691     if (!p_cb->disc)
   1692     {
   1693         return;
   1694     }
   1695 
   1696     if ((p_cb->disc & BTA_AV_CHNL_MSK) == BTA_AV_CHNL_MSK)
   1697     {
   1698         /* this is the rc handle/index to tBTA_AV_RCB */
   1699         rc_handle = p_cb->disc & (~BTA_AV_CHNL_MSK);
   1700     }
   1701     else
   1702     {
   1703         p_scb = p_cb->p_scb[(p_cb->disc & BTA_AV_HNDL_MSK) - 1];
   1704         if (p_scb)
   1705             rc_handle = p_scb->rc_handle;
   1706         else
   1707         {
   1708             p_cb->disc = 0;
   1709             return;
   1710         }
   1711     }
   1712 
   1713     APPL_TRACE_DEBUG1("rc_handle %d", rc_handle);
   1714     /* check peer version and whether support CT and TG role */
   1715     peer_features = bta_av_check_peer_features (UUID_SERVCLASS_AV_REMOTE_CONTROL);
   1716     if ((p_cb->features & BTA_AV_FEAT_ADV_CTRL) && ((peer_features&BTA_AV_FEAT_ADV_CTRL) == 0))
   1717     {
   1718         /* if we support advance control and peer does not, check their support on TG role
   1719          * some implementation uses 1.3 on CT ans 1.4 on TG */
   1720         peer_features |= bta_av_check_peer_features (UUID_SERVCLASS_AV_REM_CTRL_TARGET);
   1721     }
   1722 
   1723     p_cb->disc = 0;
   1724     utl_freebuf((void **) &p_cb->p_disc_db);
   1725 
   1726     APPL_TRACE_DEBUG2("peer_features 0x%x, features 0x%x", peer_features, p_cb->features);
   1727 
   1728     /* if we have no rc connection */
   1729     if (rc_handle == BTA_AV_RC_HANDLE_NONE)
   1730     {
   1731         if (p_scb)
   1732         {
   1733             /* if peer remote control service matches ours and USE_RC is TRUE */
   1734             if ((((p_cb->features & BTA_AV_FEAT_RCCT) && (peer_features & BTA_AV_FEAT_RCTG)) ||
   1735                  ((p_cb->features & BTA_AV_FEAT_RCTG) && (peer_features & BTA_AV_FEAT_RCCT))) )
   1736             {
   1737                 p_lcb = bta_av_find_lcb(p_scb->peer_addr, BTA_AV_LCB_FIND);
   1738                 if(p_lcb)
   1739                 {
   1740                     rc_handle = bta_av_rc_create(p_cb, AVCT_INT, (UINT8)(p_scb->hdi + 1), p_lcb->lidx);
   1741                     p_cb->rcb[rc_handle].peer_features = peer_features;
   1742                 }
   1743 #if (BT_USE_TRACES == TRUE || BT_TRACE_APPL == TRUE)
   1744                 else
   1745                 {
   1746                     APPL_TRACE_ERROR0("can not find LCB!!");
   1747                 }
   1748 #endif
   1749             }
   1750             else if(p_scb->use_rc)
   1751             {
   1752                 /* can not find AVRC on peer device. report failure */
   1753                 p_scb->use_rc = FALSE;
   1754                 bdcpy(rc_open.peer_addr, p_scb->peer_addr);
   1755                 rc_open.peer_features = 0;
   1756                 rc_open.status = BTA_AV_FAIL_SDP;
   1757                 (*p_cb->p_cback)(BTA_AV_RC_OPEN_EVT, (tBTA_AV *) &rc_open);
   1758             }
   1759         }
   1760     }
   1761     else
   1762     {
   1763         p_cb->rcb[rc_handle].peer_features = peer_features;
   1764         rc_feat.rc_handle =  rc_handle;
   1765         rc_feat.peer_features = peer_features;
   1766         (*p_cb->p_cback)(BTA_AV_RC_FEAT_EVT, (tBTA_AV *) &rc_feat);
   1767     }
   1768 }
   1769 
   1770 /*******************************************************************************
   1771 **
   1772 ** Function         bta_av_rc_closed
   1773 **
   1774 ** Description      Set AVRCP state to closed.
   1775 **
   1776 ** Returns          void
   1777 **
   1778 *******************************************************************************/
   1779 void bta_av_rc_closed(tBTA_AV_DATA *p_data)
   1780 {
   1781     tBTA_AV_CB   *p_cb = &bta_av_cb;
   1782     tBTA_AV_RC_CLOSE rc_close;
   1783     tBTA_AV_RC_CONN_CHG *p_msg = (tBTA_AV_RC_CONN_CHG *)p_data;
   1784     tBTA_AV_RCB    *p_rcb;
   1785     tBTA_AV_SCB    *p_scb;
   1786     int i;
   1787     BOOLEAN conn = FALSE;
   1788     tBTA_AV_LCB *p_lcb;
   1789 
   1790     rc_close.rc_handle = BTA_AV_RC_HANDLE_NONE;
   1791     APPL_TRACE_DEBUG1("bta_av_rc_closed rc_handle:%d", p_msg->handle);
   1792     for(i=0; i<BTA_AV_NUM_RCB; i++)
   1793     {
   1794         p_rcb = &p_cb->rcb[i];
   1795         APPL_TRACE_DEBUG3("bta_av_rc_closed rcb[%d] rc_handle:%d, status=0x%x", i, p_rcb->handle, p_rcb->status);
   1796         if(p_rcb->handle == p_msg->handle)
   1797         {
   1798             rc_close.rc_handle = i;
   1799             p_rcb->status &= ~BTA_AV_RC_CONN_MASK;
   1800             p_rcb->peer_features = 0;
   1801             APPL_TRACE_DEBUG2("       shdl:%d, lidx:%d", p_rcb->shdl, p_rcb->lidx);
   1802             if(p_rcb->shdl)
   1803             {
   1804                 p_scb = bta_av_cb.p_scb[p_rcb->shdl - 1];
   1805                 if(p_scb)
   1806                 {
   1807                     bdcpy(rc_close.peer_addr, p_scb->peer_addr);
   1808                     if(p_scb->rc_handle == p_rcb->handle)
   1809                         p_scb->rc_handle = BTA_AV_RC_HANDLE_NONE;
   1810                     APPL_TRACE_DEBUG2("shdl:%d, srch:%d", p_rcb->shdl, p_scb->rc_handle);
   1811                 }
   1812                 p_rcb->shdl = 0;
   1813             }
   1814             else if(p_rcb->lidx == (BTA_AV_NUM_LINKS + 1) )
   1815             {
   1816                 /* if the RCB uses the extra LCB, use the addr for event and clean it */
   1817                 p_lcb = &p_cb->lcb[BTA_AV_NUM_LINKS];
   1818                 bdcpy(rc_close.peer_addr, p_msg->peer_addr);
   1819                 APPL_TRACE_DEBUG6("rc_only closed bd_addr:%02x-%02x-%02x-%02x-%02x-%02x",
   1820                               p_msg->peer_addr[0], p_msg->peer_addr[1],
   1821                               p_msg->peer_addr[2], p_msg->peer_addr[3],
   1822                               p_msg->peer_addr[4], p_msg->peer_addr[5]);
   1823                 p_lcb->conn_msk = 0;
   1824                 p_lcb->lidx = 0;
   1825             }
   1826             p_rcb->lidx = 0;
   1827 
   1828             if((p_rcb->status & BTA_AV_RC_ROLE_MASK) == BTA_AV_RC_ROLE_INT)
   1829             {
   1830                 /* AVCT CCB is deallocated */
   1831                 p_rcb->handle = BTA_AV_RC_HANDLE_NONE;
   1832                 p_rcb->status = 0;
   1833             }
   1834             else
   1835             {
   1836                 /* AVCT CCB is still there. dealloc */
   1837                 bta_av_del_rc(p_rcb);
   1838 
   1839                 /* if the AVRCP is no longer listening, create the listening channel */
   1840                 if (bta_av_cb.rc_acp_handle == BTA_AV_RC_HANDLE_NONE && bta_av_cb.features & BTA_AV_FEAT_RCTG)
   1841                     bta_av_rc_create(&bta_av_cb, AVCT_ACP, 0, BTA_AV_NUM_LINKS + 1);
   1842             }
   1843         }
   1844         else if((p_rcb->handle != BTA_AV_RC_HANDLE_NONE) && (p_rcb->status & BTA_AV_RC_CONN_MASK))
   1845         {
   1846             /* at least one channel is still connected */
   1847             conn = TRUE;
   1848         }
   1849     }
   1850 
   1851     if(!conn)
   1852     {
   1853         /* no AVRC channels are connected, go back to INIT state */
   1854         bta_av_sm_execute(p_cb, BTA_AV_AVRC_NONE_EVT, NULL);
   1855     }
   1856 
   1857     if (rc_close.rc_handle == BTA_AV_RC_HANDLE_NONE)
   1858     {
   1859         rc_close.rc_handle = p_msg->handle;
   1860         bdcpy(rc_close.peer_addr, p_msg->peer_addr);
   1861     }
   1862     (*p_cb->p_cback)(BTA_AV_RC_CLOSE_EVT, (tBTA_AV *) &rc_close);
   1863 }
   1864 
   1865 /*******************************************************************************
   1866 **
   1867 ** Function         bta_av_rc_disc
   1868 **
   1869 ** Description      start AVRC SDP discovery.
   1870 **
   1871 ** Returns          void
   1872 **
   1873 *******************************************************************************/
   1874 void bta_av_rc_disc(UINT8 disc)
   1875 {
   1876     tBTA_AV_CB   *p_cb = &bta_av_cb;
   1877     tAVRC_SDP_DB_PARAMS db_params;
   1878       UINT16              attr_list[] = {ATTR_ID_SERVICE_CLASS_ID_LIST,
   1879                                        ATTR_ID_BT_PROFILE_DESC_LIST,
   1880                                        ATTR_ID_SUPPORTED_FEATURES};
   1881     UINT8       hdi;
   1882     tBTA_AV_SCB *p_scb;
   1883     UINT8       *p_addr = NULL;
   1884     UINT8       rc_handle;
   1885 
   1886     APPL_TRACE_DEBUG2("bta_av_rc_disc 0x%x, %d", disc, bta_av_cb.disc);
   1887     if ((bta_av_cb.disc != 0) || (disc == 0))
   1888         return;
   1889 
   1890     if ((disc & BTA_AV_CHNL_MSK) == BTA_AV_CHNL_MSK)
   1891     {
   1892         /* this is the rc handle/index to tBTA_AV_RCB */
   1893         rc_handle = disc & (~BTA_AV_CHNL_MSK);
   1894         if (p_cb->rcb[rc_handle].lidx)
   1895         {
   1896             p_addr = p_cb->lcb[p_cb->rcb[rc_handle].lidx-1].addr;
   1897         }
   1898     }
   1899     else
   1900     {
   1901         hdi = (disc & BTA_AV_HNDL_MSK) - 1;
   1902         p_scb = p_cb->p_scb[hdi];
   1903 
   1904         if (p_scb)
   1905         {
   1906             APPL_TRACE_DEBUG1("rc_handle %d", p_scb->rc_handle);
   1907             p_addr = p_scb->peer_addr;
   1908         }
   1909     }
   1910 
   1911     if (p_addr)
   1912     {
   1913         /* allocate discovery database */
   1914         if (p_cb->p_disc_db == NULL)
   1915         {
   1916             p_cb->p_disc_db = (tSDP_DISCOVERY_DB *) GKI_getbuf(BTA_AV_DISC_BUF_SIZE);
   1917         }
   1918 
   1919         if (p_cb->p_disc_db)
   1920         {
   1921             /* set up parameters */
   1922             db_params.db_len = BTA_AV_DISC_BUF_SIZE;
   1923             db_params.num_attr = 3;
   1924             db_params.p_db = p_cb->p_disc_db;
   1925             db_params.p_attrs = attr_list;
   1926 
   1927             /* searching for UUID_SERVCLASS_AV_REMOTE_CONTROL gets both TG and CT */
   1928             if (AVRC_FindService(UUID_SERVCLASS_AV_REMOTE_CONTROL, p_addr, &db_params,
   1929                             bta_av_avrc_sdp_cback) == AVRC_SUCCESS)
   1930             {
   1931                 p_cb->disc = disc;
   1932                 APPL_TRACE_DEBUG1("disc %d", p_cb->disc);
   1933             }
   1934         }
   1935     }
   1936 }
   1937 
   1938 /*******************************************************************************
   1939 **
   1940 ** Function         bta_av_dereg_comp
   1941 **
   1942 ** Description      deregister complete. free the stream control block.
   1943 **
   1944 ** Returns          void
   1945 **
   1946 *******************************************************************************/
   1947 void bta_av_dereg_comp(tBTA_AV_DATA *p_data)
   1948 {
   1949     tBTA_AV_CB   *p_cb = &bta_av_cb;
   1950     tBTA_AV_SCB  *p_scb;
   1951     tBTA_UTL_COD    cod;
   1952     UINT8   mask;
   1953     BT_HDR  *p_buf;
   1954 
   1955     /* find the stream control block */
   1956     p_scb = bta_av_hndl_to_scb(p_data->hdr.layer_specific);
   1957 
   1958     if(p_scb)
   1959     {
   1960         APPL_TRACE_DEBUG2("deregistered %d(h%d)", p_scb->chnl, p_scb->hndl);
   1961         mask = BTA_AV_HNDL_TO_MSK(p_scb->hdi);
   1962         if(p_scb->chnl == BTA_AV_CHNL_AUDIO)
   1963         {
   1964             p_cb->reg_audio  &= ~mask;
   1965             if ((p_cb->conn_audio & mask) && bta_av_cb.audio_open_cnt)
   1966             {
   1967                 /* this channel is still marked as open. decrease the count */
   1968                 bta_av_cb.audio_open_cnt--;
   1969             }
   1970             p_cb->conn_audio &= ~mask;
   1971 
   1972             if (p_scb->q_tag == BTA_AV_Q_TAG_STREAM)
   1973             {
   1974             /* make sure no buffers are in q_info.a2d */
   1975             while((p_buf = (BT_HDR*)GKI_dequeue (&p_scb->q_info.a2d)) != NULL)
   1976                 GKI_freebuf(p_buf);
   1977             }
   1978 
   1979             /* remove the A2DP SDP record, if no more audio stream is left */
   1980             if(!p_cb->reg_audio)
   1981             {
   1982 #if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
   1983                 bta_ar_dereg_avrc (UUID_SERVCLASS_AV_REMOTE_CONTROL, BTA_ID_AV);
   1984 #endif
   1985                 bta_av_del_sdp_rec(&p_cb->sdp_a2d_handle);
   1986                 bta_sys_remove_uuid(UUID_SERVCLASS_AUDIO_SOURCE);
   1987             }
   1988         }
   1989         else
   1990         {
   1991             p_cb->reg_video  &= ~mask;
   1992             /* make sure that this channel is not connected */
   1993             p_cb->conn_video &= ~mask;
   1994             /* remove the VDP SDP record, (only one video stream at most) */
   1995             bta_av_del_sdp_rec(&p_cb->sdp_vdp_handle);
   1996             bta_sys_remove_uuid(UUID_SERVCLASS_VIDEO_SOURCE);
   1997         }
   1998 
   1999         /* make sure that the timer is not active */
   2000         bta_sys_stop_timer(&p_scb->timer);
   2001         utl_freebuf((void **)&p_cb->p_scb[p_scb->hdi]);
   2002     }
   2003 
   2004     APPL_TRACE_DEBUG3("audio 0x%x, video: 0x%x, disable:%d",
   2005         p_cb->reg_audio, p_cb->reg_video, p_cb->disabling);
   2006     /* if no stream control block is active */
   2007     if((p_cb->reg_audio + p_cb->reg_video) == 0)
   2008     {
   2009 #if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
   2010         /* deregister from AVDT */
   2011         bta_ar_dereg_avdt(BTA_ID_AV);
   2012 
   2013         /* deregister from AVCT */
   2014         bta_ar_dereg_avrc (UUID_SERVCLASS_AV_REM_CTRL_TARGET, BTA_ID_AV);
   2015         bta_ar_dereg_avct(BTA_ID_AV);
   2016 #endif
   2017 
   2018         if(p_cb->disabling)
   2019         {
   2020             p_cb->disabling     = FALSE;
   2021             bta_av_cb.features  = 0;
   2022         }
   2023 
   2024         /* Clear the Capturing service class bit */
   2025         cod.service = BTM_COD_SERVICE_CAPTURING;
   2026         utl_set_device_class(&cod, BTA_UTL_CLR_COD_SERVICE_CLASS);
   2027     }
   2028 }
   2029 #endif /* BTA_AV_INCLUDED */
   2030