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