]> granicus.if.org Git - esp-idf/commitdiff
Merge branch 'master' into feature/btdm_avrc
authorwangmengyang <wangmengyang@espressif.com>
Mon, 13 Mar 2017 08:21:41 +0000 (16:21 +0800)
committerwangmengyang <wangmengyang@espressif.com>
Mon, 13 Mar 2017 08:21:41 +0000 (16:21 +0800)
1  2 
components/bt/bluedroid/bta/av/bta_av_act.c
components/bt/bluedroid/bta/include/bta_api.h
components/bt/bluedroid/btc/core/btc_profile_queue.c
components/bt/bluedroid/btc/core/btc_storage.c
components/bt/bluedroid/btc/profile/std/a2dp/btc_avk.c
components/bt/bluedroid/btc/profile/std/avrc/btc_avrc.c
components/bt/bluedroid/osi/include/thread.h
components/bt/bluedroid/stack/include/gatt_api.h

index b1b0e5805b10e8df7cce9084681d70a0bdd25d1e,0000000000000000000000000000000000000000..8c9840e4b8e12e1b5161eafcceeea2e0bad17cf6
mode 100755,000000..100755
--- /dev/null
@@@ -1,2068 -1,0 +1,2068 @@@
-             LOG_INFO("%s allow incoming AVRCP connections:%d", __func__, p_scb->use_rc);
 +/******************************************************************************
 + *
 + *  Copyright (C) 2004-2012 Broadcom Corporation
 + *
 + *  Licensed under the Apache License, Version 2.0 (the "License");
 + *  you may not use this file except in compliance with the License.
 + *  You may obtain a copy of the License at:
 + *
 + *  http://www.apache.org/licenses/LICENSE-2.0
 + *
 + *  Unless required by applicable law or agreed to in writing, software
 + *  distributed under the License is distributed on an "AS IS" BASIS,
 + *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 + *  See the License for the specific language governing permissions and
 + *  limitations under the License.
 + *
 + ******************************************************************************/
 +
 +/******************************************************************************
 + *
 + *  This file contains action functions for advanced audio/video main state
 + *  machine.
 + *
 + ******************************************************************************/
 +
 +#include "bt_target.h"
 +#if defined(BTA_AV_INCLUDED) && (BTA_AV_INCLUDED == TRUE)
 +
 +#include <string.h>
 +#include "bta_av_api.h"
 +#include "bta_av_int.h"
 +#include "avdt_api.h"
 +#include "utl.h"
 +#include "l2c_api.h"
 +// #include "osi/include/list.h"
 +#include "list.h"
 +#if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
 +#include "bta_ar_api.h"
 +#endif
 +
 +#define LOG_TAG "bt_bta_av"
 +// #include "osi/include/log.h"
 +#include "bt_trace.h"
 +
 +/*****************************************************************************
 +**  Constants
 +*****************************************************************************/
 +/* the timer in milliseconds to wait for open req after setconfig for incoming connections */
 +#ifndef BTA_AV_SIG_TIME_VAL
 +#define BTA_AV_SIG_TIME_VAL 8000
 +#endif
 +
 +/* In millisec to wait for signalling from SNK when it is initiated from SNK.   */
 +/* If not, we will start signalling from SRC.                                   */
 +#ifndef BTA_AV_ACP_SIG_TIME_VAL
 +#define BTA_AV_ACP_SIG_TIME_VAL 2000
 +#endif
 +
 +static void bta_av_acp_sig_timer_cback (TIMER_LIST_ENT *p_tle);
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_get_rcb_by_shdl
 +**
 +** Description      find the RCB associated with the given SCB handle.
 +**
 +** Returns          tBTA_AV_RCB
 +**
 +*******************************************************************************/
 +tBTA_AV_RCB * bta_av_get_rcb_by_shdl(UINT8 shdl)
 +{
 +    tBTA_AV_RCB *p_rcb = NULL;
 +    int         i;
 +
 +    for (i=0; i<BTA_AV_NUM_RCB; i++)
 +    {
 +        if (bta_av_cb.rcb[i].shdl == shdl && bta_av_cb.rcb[i].handle != BTA_AV_RC_HANDLE_NONE)
 +        {
 +            p_rcb = &bta_av_cb.rcb[i];
 +            break;
 +        }
 +    }
 +    return p_rcb;
 +}
 +#define BTA_AV_STS_NO_RSP       0xFF    /* a number not used by tAVRC_STS */
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_del_rc
 +**
 +** Description      delete the given AVRC handle.
 +**
 +** Returns          void
 +**
 +*******************************************************************************/
 +void bta_av_del_rc(tBTA_AV_RCB *p_rcb)
 +{
 +    tBTA_AV_SCB  *p_scb;
 +    UINT8        rc_handle;      /* connected AVRCP handle */
 +
 +    p_scb = NULL;
 +    if(p_rcb->handle != BTA_AV_RC_HANDLE_NONE)
 +    {
 +        if(p_rcb->shdl)
 +        {
 +            /* Validate array index*/
 +            if ((p_rcb->shdl - 1) < BTA_AV_NUM_STRS)
 +            {
 +                p_scb = bta_av_cb.p_scb[p_rcb->shdl - 1];
 +            }
 +            if(p_scb)
 +            {
 +                APPL_TRACE_DEBUG("bta_av_del_rc shdl:%d, srch:%d rc_handle:%d", p_rcb->shdl,
 +                                  p_scb->rc_handle, p_rcb->handle);
 +                if(p_scb->rc_handle == p_rcb->handle)
 +                    p_scb->rc_handle = BTA_AV_RC_HANDLE_NONE;
 +                /* just in case the RC timer is active
 +                if(bta_av_cb.features & BTA_AV_FEAT_RCCT && p_scb->chnl == BTA_AV_CHNL_AUDIO) */
 +                    bta_sys_stop_timer(&p_scb->timer);
 +            }
 +        }
 +
 +        APPL_TRACE_EVENT("bta_av_del_rc  handle: %d status=0x%x, rc_acp_handle:%d, idx:%d",
 +            p_rcb->handle, p_rcb->status, bta_av_cb.rc_acp_handle, bta_av_cb.rc_acp_idx);
 +        rc_handle = p_rcb->handle;
 +        if(!(p_rcb->status & BTA_AV_RC_CONN_MASK) ||
 +            ((p_rcb->status & BTA_AV_RC_ROLE_MASK) == BTA_AV_RC_ROLE_INT) )
 +        {
 +            p_rcb->status = 0;
 +            p_rcb->handle = BTA_AV_RC_HANDLE_NONE;
 +            p_rcb->shdl = 0;
 +            p_rcb->lidx = 0;
 +        }
 +        /* else ACP && connected. do not clear the handle yet */
 +        AVRC_Close(rc_handle);
 +        if (rc_handle == bta_av_cb.rc_acp_handle)
 +            bta_av_cb.rc_acp_handle = BTA_AV_RC_HANDLE_NONE;
 +        APPL_TRACE_EVENT("end del_rc handle: %d status=0x%x, rc_acp_handle:%d, lidx:%d",
 +            p_rcb->handle, p_rcb->status, bta_av_cb.rc_acp_handle, p_rcb->lidx);
 +    }
 +}
 +
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_close_all_rc
 +**
 +** Description      close the all AVRC handle.
 +**
 +** Returns          void
 +**
 +*******************************************************************************/
 +static void bta_av_close_all_rc(tBTA_AV_CB *p_cb)
 +{
 +    int i;
 +
 +    for(i=0; i<BTA_AV_NUM_RCB; i++)
 +    {
 +        if ((p_cb->disabling == TRUE) || (bta_av_cb.rcb[i].shdl != 0))
 +            bta_av_del_rc(&bta_av_cb.rcb[i]);
 +    }
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_del_sdp_rec
 +**
 +** Description      delete the given SDP record handle.
 +**
 +** Returns          void
 +**
 +*******************************************************************************/
 +static void bta_av_del_sdp_rec(UINT32 *p_sdp_handle)
 +{
 +    if(*p_sdp_handle != 0)
 +    {
 +        SDP_DeleteRecord(*p_sdp_handle);
 +        *p_sdp_handle = 0;
 +    }
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_avrc_sdp_cback
 +**
 +** Description      AVRCP service discovery callback.
 +**
 +** Returns          void
 +**
 +*******************************************************************************/
 +static void bta_av_avrc_sdp_cback(UINT16 status)
 +{
 +    BT_HDR *p_msg;
 +    UNUSED(status);
 +
 +    if ((p_msg = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL)
 +    {
 +        p_msg->event = BTA_AV_SDP_AVRC_DISC_EVT;
 +        bta_sys_sendmsg(p_msg);
 +    }
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_rc_ctrl_cback
 +**
 +** Description      AVRCP control callback.
 +**
 +** Returns          void
 +**
 +*******************************************************************************/
 +static void bta_av_rc_ctrl_cback(UINT8 handle, UINT8 event, UINT16 result, BD_ADDR peer_addr)
 +{
 +    tBTA_AV_RC_CONN_CHG *p_msg;
 +    UINT16 msg_event = 0;
 +    UNUSED(result);
 +
 +#if (defined(BTA_AV_MIN_DEBUG_TRACES) && BTA_AV_MIN_DEBUG_TRACES == TRUE)
 +    APPL_TRACE_EVENT("rc_ctrl handle: %d event=0x%x", handle, event);
 +#else
 +    APPL_TRACE_EVENT("bta_av_rc_ctrl_cback handle: %d event=0x%x", handle, event);
 +#endif
 +    if (event == AVRC_OPEN_IND_EVT)
 +    {
 +        /* save handle of opened connection
 +        bta_av_cb.rc_handle = handle;*/
 +
 +        msg_event = BTA_AV_AVRC_OPEN_EVT;
 +    }
 +    else if (event == AVRC_CLOSE_IND_EVT)
 +    {
 +        msg_event = BTA_AV_AVRC_CLOSE_EVT;
 +    }
 +
 +    if (msg_event)
 +    {
 +        if ((p_msg = (tBTA_AV_RC_CONN_CHG *) GKI_getbuf(sizeof(tBTA_AV_RC_CONN_CHG))) != NULL)
 +        {
 +            p_msg->hdr.event = msg_event;
 +            p_msg->handle    = handle;
 +            if(peer_addr)
 +                bdcpy(p_msg->peer_addr, peer_addr);
 +            bta_sys_sendmsg(p_msg);
 +        }
 +    }
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_rc_msg_cback
 +**
 +** Description      AVRCP message callback.
 +**
 +** Returns          void
 +**
 +*******************************************************************************/
 +static void bta_av_rc_msg_cback(UINT8 handle, UINT8 label, UINT8 opcode, tAVRC_MSG *p_msg)
 +{
 +    UINT8           *p_data_src = NULL;
 +    UINT16          data_len = 0;
 +
 +    APPL_TRACE_DEBUG("%s handle: %u opcode=0x%x", __func__, handle, opcode);
 +
 +    /* Determine the size of the buffer we need */
 +    if (opcode == AVRC_OP_VENDOR && p_msg->vendor.p_vendor_data != NULL) {
 +        p_data_src = p_msg->vendor.p_vendor_data;
 +        data_len = (UINT16) p_msg->vendor.vendor_len;
 +    } else if (opcode == AVRC_OP_PASS_THRU && p_msg->pass.p_pass_data != NULL) {
 +        p_data_src = p_msg->pass.p_pass_data;
 +        data_len = (UINT16) p_msg->pass.pass_len;
 +    }
 +
 +    /* Create a copy of the message */
 +    tBTA_AV_RC_MSG *p_buf =
 +        (tBTA_AV_RC_MSG *)GKI_getbuf((UINT16)(sizeof(tBTA_AV_RC_MSG) + data_len));
 +    if (p_buf != NULL) {
 +        p_buf->hdr.event = BTA_AV_AVRC_MSG_EVT;
 +        p_buf->handle = handle;
 +        p_buf->label = label;
 +        p_buf->opcode = opcode;
 +        memcpy(&p_buf->msg, p_msg, sizeof(tAVRC_MSG));
 +        /* Copy the data payload, and set the pointer to it */
 +        if (p_data_src != NULL) {
 +            UINT8 *p_data_dst = (UINT8 *)(p_buf + 1);
 +            memcpy(p_data_dst, p_data_src, data_len);
 +            if (opcode == AVRC_OP_VENDOR)
 +                p_buf->msg.vendor.p_vendor_data = p_data_dst;
 +            else if (opcode == AVRC_OP_PASS_THRU)
 +                p_buf->msg.pass.p_pass_data = p_data_dst;
 +        }
 +        bta_sys_sendmsg(p_buf);
 +    }
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_rc_create
 +**
 +** Description      alloc RCB and call AVRC_Open
 +**
 +** Returns          the created rc handle
 +**
 +*******************************************************************************/
 +UINT8 bta_av_rc_create(tBTA_AV_CB *p_cb, UINT8 role, UINT8 shdl, UINT8 lidx)
 +{
 +    tAVRC_CONN_CB ccb;
 +    BD_ADDR_PTR   bda = (BD_ADDR_PTR)bd_addr_any;
 +    UINT8         status = BTA_AV_RC_ROLE_ACP;
 +    tBTA_AV_SCB  *p_scb = p_cb->p_scb[shdl - 1];
 +    int i;
 +    UINT8   rc_handle;
 +    tBTA_AV_RCB *p_rcb;
 +
 +    if(role == AVCT_INT)
 +    {
 +        bda = p_scb->peer_addr;
 +        status = BTA_AV_RC_ROLE_INT;
 +    }
 +    else
 +    {
 +        if ((p_rcb = bta_av_get_rcb_by_shdl(shdl)) != NULL )
 +        {
 +            APPL_TRACE_ERROR("bta_av_rc_create ACP handle exist for shdl:%d", shdl);
 +            return p_rcb->handle;
 +        }
 +    }
 +
 +    ccb.p_ctrl_cback = bta_av_rc_ctrl_cback;
 +    ccb.p_msg_cback = bta_av_rc_msg_cback;
 +    ccb.company_id = p_bta_av_cfg->company_id;
 +    ccb.conn = role;
 +    /* note: BTA_AV_FEAT_RCTG = AVRC_CT_TARGET, BTA_AV_FEAT_RCCT = AVRC_CT_CONTROL */
 +    ccb.control = p_cb->features & (BTA_AV_FEAT_RCTG | BTA_AV_FEAT_RCCT | AVRC_CT_PASSIVE);
 +
 +
 +    if (AVRC_Open(&rc_handle, &ccb, bda) != AVRC_SUCCESS)
 +        return BTA_AV_RC_HANDLE_NONE;
 +
 +    i = rc_handle;
 +    p_rcb = &p_cb->rcb[i];
 +
 +    if (p_rcb->handle != BTA_AV_RC_HANDLE_NONE)
 +    {
 +        APPL_TRACE_ERROR("bta_av_rc_create found duplicated handle:%d", rc_handle);
 +    }
 +
 +    p_rcb->handle = rc_handle;
 +    p_rcb->status = status;
 +    p_rcb->shdl = shdl;
 +    p_rcb->lidx = lidx;
 +    p_rcb->peer_features = 0;
 +    if(lidx == (BTA_AV_NUM_LINKS + 1))
 +    {
 +        /* this LIDX is reserved for the AVRCP ACP connection */
 +        p_cb->rc_acp_handle = p_rcb->handle;
 +        p_cb->rc_acp_idx = (i + 1);
 +        APPL_TRACE_DEBUG("rc_acp_handle:%d idx:%d", p_cb->rc_acp_handle, p_cb->rc_acp_idx);
 +    }
 +    APPL_TRACE_DEBUG("create %d, role: %d, shdl:%d, rc_handle:%d, lidx:%d, status:0x%x",
 +        i, role, shdl, p_rcb->handle, lidx, p_rcb->status);
 +
 +    return rc_handle;
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_valid_group_navi_msg
 +**
 +** Description      Check if it is Group Navigation Msg for Metadata
 +**
 +** Returns          BTA_AV_RSP_ACCEPT or BTA_AV_RSP_NOT_IMPL.
 +**
 +*******************************************************************************/
 +static tBTA_AV_CODE bta_av_group_navi_supported(UINT8 len, UINT8 *p_data, BOOLEAN is_inquiry)
 +{
 +    tBTA_AV_CODE ret=BTA_AV_RSP_NOT_IMPL;
 +    UINT8 *p_ptr = p_data;
 +    UINT16 u16;
 +    UINT32 u32;
 +
 +    if (p_bta_av_cfg->avrc_group && len == BTA_GROUP_NAVI_MSG_OP_DATA_LEN)
 +    {
 +        BTA_AV_BE_STREAM_TO_CO_ID(u32, p_ptr);
 +        BE_STREAM_TO_UINT16(u16, p_ptr);
 +
 +        if (u32 == AVRC_CO_METADATA)
 +        {
 +            if (is_inquiry)
 +            {
 +                if (u16 <= AVRC_PDU_PREV_GROUP)
 +                    ret = BTA_AV_RSP_IMPL_STBL;
 +            }
 +            else
 +            {
 +                if (u16 <= AVRC_PDU_PREV_GROUP)
 +                    ret = BTA_AV_RSP_ACCEPT;
 +                else
 +                    ret = BTA_AV_RSP_REJ;
 +            }
 +        }
 +    }
 +
 +    return ret;
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_op_supported
 +**
 +** Description      Check if remote control operation is supported.
 +**
 +** Returns          BTA_AV_RSP_ACCEPT of supported, BTA_AV_RSP_NOT_IMPL if not.
 +**
 +*******************************************************************************/
 +static tBTA_AV_CODE bta_av_op_supported(tBTA_AV_RC rc_id, BOOLEAN is_inquiry)
 +{
 +    tBTA_AV_CODE ret_code = BTA_AV_RSP_NOT_IMPL;
 +
 +    if (p_bta_av_rc_id)
 +    {
 +        if (is_inquiry)
 +        {
 +            if (p_bta_av_rc_id[rc_id >> 4] & (1 << (rc_id & 0x0F)))
 +            {
 +                ret_code = BTA_AV_RSP_IMPL_STBL;
 +            }
 +        }
 +        else
 +        {
 +            if (p_bta_av_rc_id[rc_id >> 4] & (1 << (rc_id & 0x0F)))
 +            {
 +                ret_code = BTA_AV_RSP_ACCEPT;
 +            }
 +            else if ((p_bta_av_cfg->rc_pass_rsp == BTA_AV_RSP_INTERIM) && p_bta_av_rc_id_ac)
 +            {
 +                if (p_bta_av_rc_id_ac[rc_id >> 4] & (1 << (rc_id & 0x0F)))
 +                {
 +                    ret_code = BTA_AV_RSP_INTERIM;
 +                }
 +            }
 +        }
 +
 +    }
 +    return ret_code;
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_find_lcb
 +**
 +** Description      Given BD_addr, find the associated LCB.
 +**
 +** Returns          NULL, if not found.
 +**
 +*******************************************************************************/
 +tBTA_AV_LCB * bta_av_find_lcb(BD_ADDR addr, UINT8 op)
 +{
 +    tBTA_AV_CB   *p_cb = &bta_av_cb;
 +    int     xx;
 +    UINT8   mask;
 +    tBTA_AV_LCB *p_lcb = NULL;
 +
 +    for(xx=0; xx<BTA_AV_NUM_LINKS; xx++)
 +    {
 +        mask = 1 << xx; /* the used mask for this lcb */
 +        if((mask & p_cb->conn_lcb) && 0 ==( bdcmp(p_cb->lcb[xx].addr, addr)))
 +        {
 +            p_lcb = &p_cb->lcb[xx];
 +            if(op == BTA_AV_LCB_FREE)
 +            {
 +                p_cb->conn_lcb &= ~mask; /* clear the connect mask */
 +                APPL_TRACE_DEBUG("conn_lcb: 0x%x", p_cb->conn_lcb);
 +            }
 +            break;
 +        }
 +    }
 +    return p_lcb;
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_rc_opened
 +**
 +** Description      Set AVRCP state to opened.
 +**
 +** Returns          void
 +**
 +*******************************************************************************/
 +void bta_av_rc_opened(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
 +{
 +    tBTA_AV_RC_OPEN rc_open;
 +    tBTA_AV_SCB     *p_scb;
 +    int         i;
 +    UINT8       shdl = 0;
 +    tBTA_AV_LCB *p_lcb;
 +    tBTA_AV_RCB *p_rcb;
 +    UINT8       tmp;
 +    UINT8       disc = 0;
 +
 +    /* find the SCB & stop the timer */
 +    for(i=0; i<BTA_AV_NUM_STRS; i++)
 +    {
 +        p_scb = p_cb->p_scb[i];
 +        if(p_scb && bdcmp(p_scb->peer_addr, p_data->rc_conn_chg.peer_addr) == 0)
 +        {
 +            p_scb->rc_handle = p_data->rc_conn_chg.handle;
 +            APPL_TRACE_DEBUG("bta_av_rc_opened shdl:%d, srch %d", i + 1, p_scb->rc_handle);
 +            shdl = i+1;
++            APPL_TRACE_DEBUG("%s allow incoming AVRCP connections:%d", __func__, p_scb->use_rc);
 +            bta_sys_stop_timer(&p_scb->timer);
 +            disc = p_scb->hndl;
 +            break;
 +        }
 +    }
 +
 +    i = p_data->rc_conn_chg.handle;
 +    if (p_cb->rcb[i].handle == BTA_AV_RC_HANDLE_NONE)
 +    {
 +        APPL_TRACE_ERROR("not a valid handle:%d any more", i);
 +        return;
 +    }
 +
 +
 +    if (p_cb->rcb[i].lidx == (BTA_AV_NUM_LINKS + 1) && shdl != 0)
 +    {
 +        /* rc is opened on the RC only ACP channel, but is for a specific
 +         * SCB -> need to switch RCBs */
 +        p_rcb = bta_av_get_rcb_by_shdl(shdl);
 +        if (p_rcb)
 +        {
 +            p_rcb->shdl = p_cb->rcb[i].shdl;
 +            tmp         = p_rcb->lidx;
 +            p_rcb->lidx = p_cb->rcb[i].lidx;
 +            p_cb->rcb[i].lidx = tmp;
 +            p_cb->rc_acp_handle = p_rcb->handle;
 +            p_cb->rc_acp_idx = (p_rcb - p_cb->rcb) + 1;
 +            APPL_TRACE_DEBUG("switching RCB rc_acp_handle:%d idx:%d",
 +                               p_cb->rc_acp_handle, p_cb->rc_acp_idx);
 +        }
 +    }
 +
 +    p_cb->rcb[i].shdl = shdl;
 +    rc_open.rc_handle = i;
 +    APPL_TRACE_ERROR("bta_av_rc_opened rcb[%d] shdl:%d lidx:%d/%d",
 +            i, shdl, p_cb->rcb[i].lidx, p_cb->lcb[BTA_AV_NUM_LINKS].lidx);
 +    p_cb->rcb[i].status |= BTA_AV_RC_CONN_MASK;
 +
 +    if(!shdl && 0 == p_cb->lcb[BTA_AV_NUM_LINKS].lidx)
 +    {
 +        /* no associated SCB -> connected to an RC only device
 +         * update the index to the extra LCB */
 +        p_lcb = &p_cb->lcb[BTA_AV_NUM_LINKS];
 +        bdcpy(p_lcb->addr, p_data->rc_conn_chg.peer_addr);
 +        APPL_TRACE_DEBUG("rc_only bd_addr:%02x-%02x-%02x-%02x-%02x-%02x",
 +                      p_lcb->addr[0], p_lcb->addr[1],
 +                      p_lcb->addr[2], p_lcb->addr[3],
 +                      p_lcb->addr[4], p_lcb->addr[5]);
 +        p_lcb->lidx = BTA_AV_NUM_LINKS + 1;
 +            p_cb->rcb[i].lidx = p_lcb->lidx;
 +        p_lcb->conn_msk = 1;
 +        APPL_TRACE_ERROR("rcb[%d].lidx=%d, lcb.conn_msk=x%x",
 +            i, p_cb->rcb[i].lidx, p_lcb->conn_msk);
 +        disc = p_data->rc_conn_chg.handle|BTA_AV_CHNL_MSK;
 +    }
 +
 +    bdcpy(rc_open.peer_addr, p_data->rc_conn_chg.peer_addr);
 +    rc_open.peer_features = p_cb->rcb[i].peer_features;
 +    rc_open.status = BTA_AV_SUCCESS;
 +    APPL_TRACE_DEBUG("local features:x%x peer_features:x%x", p_cb->features,
 +                      rc_open.peer_features);
 +    if(rc_open.peer_features == 0)
 +    {
 +        /* we have not done SDP on peer RC capabilities.
 +         * peer must have initiated the RC connection */
 +        rc_open.peer_features = BTA_AV_FEAT_RCCT;
 +        bta_av_rc_disc(disc);
 +    }
 +    (*p_cb->p_cback)(BTA_AV_RC_OPEN_EVT, (tBTA_AV *) &rc_open);
 +
 +}
 +
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_rc_remote_cmd
 +**
 +** Description      Send an AVRCP remote control command.
 +**
 +** Returns          void
 +**
 +*******************************************************************************/
 +void bta_av_rc_remote_cmd(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
 +{
 +    tBTA_AV_RCB    *p_rcb;
 +    if (p_cb->features & BTA_AV_FEAT_RCCT)
 +    {
 +        if(p_data->hdr.layer_specific < BTA_AV_NUM_RCB)
 +        {
 +            p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
 +            if(p_rcb->status & BTA_AV_RC_CONN_MASK)
 +            {
 +                AVRC_PassCmd(p_rcb->handle, p_data->api_remote_cmd.label,
 +                     &p_data->api_remote_cmd.msg);
 +            }
 +        }
 +    }
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_rc_vendor_cmd
 +**
 +** Description      Send an AVRCP vendor specific command.
 +**
 +** Returns          void
 +**
 +*******************************************************************************/
 +void bta_av_rc_vendor_cmd(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
 +{
 +    tBTA_AV_RCB    *p_rcb;
 +    if ( (p_cb->features & (BTA_AV_FEAT_RCCT | BTA_AV_FEAT_VENDOR)) ==
 +         (BTA_AV_FEAT_RCCT | BTA_AV_FEAT_VENDOR))
 +    {
 +        if(p_data->hdr.layer_specific < BTA_AV_NUM_RCB)
 +        {
 +            p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
 +            AVRC_VendorCmd(p_rcb->handle, p_data->api_vendor.label, &p_data->api_vendor.msg);
 +        }
 +    }
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_rc_vendor_rsp
 +**
 +** Description      Send an AVRCP vendor specific response.
 +**
 +** Returns          void
 +**
 +*******************************************************************************/
 +void bta_av_rc_vendor_rsp(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
 +{
 +    tBTA_AV_RCB    *p_rcb;
 +    if ( (p_cb->features & (BTA_AV_FEAT_RCTG | BTA_AV_FEAT_VENDOR)) ==
 +         (BTA_AV_FEAT_RCTG | BTA_AV_FEAT_VENDOR))
 +    {
 +        if(p_data->hdr.layer_specific < BTA_AV_NUM_RCB)
 +        {
 +            p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
 +            AVRC_VendorRsp(p_rcb->handle, p_data->api_vendor.label, &p_data->api_vendor.msg);
 +        }
 +    }
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_rc_meta_rsp
 +**
 +** Description      Send an AVRCP metadata/advanced control command/response.
 +**
 +** Returns          void
 +**
 +*******************************************************************************/
 +void bta_av_rc_meta_rsp(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
 +{
 +    tBTA_AV_RCB *p_rcb;
 +    BOOLEAN         do_free = TRUE;
 +
 +    if ((p_cb->features & BTA_AV_FEAT_METADATA) && (p_data->hdr.layer_specific < BTA_AV_NUM_RCB))
 +    {
 +        if ((p_data->api_meta_rsp.is_rsp && (p_cb->features & BTA_AV_FEAT_RCTG)) ||
 +            (!p_data->api_meta_rsp.is_rsp && (p_cb->features & BTA_AV_FEAT_RCCT)) )
 +        {
 +            p_rcb = &p_cb->rcb[p_data->hdr.layer_specific];
 +            if (p_rcb->handle != BTA_AV_RC_HANDLE_NONE) {
 +                AVRC_MsgReq(p_rcb->handle, p_data->api_meta_rsp.label,
 +                            p_data->api_meta_rsp.rsp_code,
 +                            p_data->api_meta_rsp.p_pkt);
 +                do_free = FALSE;
 +            }
 +        }
 +    }
 +
 +    if (do_free)
 +        GKI_freebuf (p_data->api_meta_rsp.p_pkt);
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_rc_free_rsp
 +**
 +** Description      free an AVRCP metadata command buffer.
 +**
 +** Returns          void
 +**
 +*******************************************************************************/
 +void bta_av_rc_free_rsp (tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
 +{
 +    UNUSED(p_cb);
 +
 +    GKI_freebuf (p_data->api_meta_rsp.p_pkt);
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_rc_meta_req
 +**
 +** Description      Send an AVRCP metadata command.
 +**
 +** Returns          void
 +**
 +*******************************************************************************/
 +void bta_av_rc_free_msg (tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
 +{
 +    UNUSED(p_cb);
 +    UNUSED(p_data);
 +}
 +
 +
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_chk_notif_evt_id
 +**
 +** Description      make sure the requested player id is valid.
 +**
 +** Returns          BTA_AV_STS_NO_RSP, if no error
 +**
 +*******************************************************************************/
 +static tAVRC_STS bta_av_chk_notif_evt_id(tAVRC_MSG_VENDOR *p_vendor)
 +{
 +    tAVRC_STS   status = BTA_AV_STS_NO_RSP;
 +    UINT8       xx;
 +    UINT16      u16;
 +    UINT8       *p = p_vendor->p_vendor_data + 2;
 +
 +    BE_STREAM_TO_UINT16 (u16, p);
 +    /* double check the fixed length */
 +    if ((u16 != 5) || (p_vendor->vendor_len != 9))
 +    {
 +        status = AVRC_STS_INTERNAL_ERR;
 +    }
 +    else
 +    {
 +        /* make sure the player_id is valid */
 +        for (xx=0; xx<p_bta_av_cfg->num_evt_ids; xx++)
 +        {
 +            if (*p == p_bta_av_cfg->p_meta_evt_ids[xx])
 +            {
 +                break;
 +            }
 +        }
 +        if (xx == p_bta_av_cfg->num_evt_ids)
 +        {
 +            status = AVRC_STS_BAD_PARAM;
 +        }
 +    }
 +
 +    return status;
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_proc_meta_cmd
 +**
 +** Description      Process an AVRCP metadata command from the peer.
 +**
 +** Returns          TRUE to respond immediately
 +**
 +*******************************************************************************/
 +tBTA_AV_EVT bta_av_proc_meta_cmd(tAVRC_RESPONSE  *p_rc_rsp, tBTA_AV_RC_MSG *p_msg, UINT8 *p_ctype)
 +{
 +    tBTA_AV_EVT evt = BTA_AV_META_MSG_EVT;
 +    UINT8       u8, pdu, *p;
 +    UINT16      u16;
 +    tAVRC_MSG_VENDOR    *p_vendor = &p_msg->msg.vendor;
 +
 +#if (AVRC_METADATA_INCLUDED == TRUE)
 +
 +    pdu = *(p_vendor->p_vendor_data);
 +    p_rc_rsp->pdu = pdu;
 +    *p_ctype = AVRC_RSP_REJ;
 +    /* Metadata messages only use PANEL sub-unit type */
 +    if (p_vendor->hdr.subunit_type != AVRC_SUB_PANEL)
 +    {
 +        APPL_TRACE_DEBUG("SUBUNIT must be PANEL");
 +        /* reject it */
 +        evt=0;
 +        p_vendor->hdr.ctype = BTA_AV_RSP_NOT_IMPL;
 +        AVRC_VendorRsp(p_msg->handle, p_msg->label, &p_msg->msg.vendor);
 +    }
 +    else if (!AVRC_IsValidAvcType(pdu, p_vendor->hdr.ctype) )
 +    {
 +        APPL_TRACE_DEBUG("Invalid pdu/ctype: 0x%x, %d", pdu, p_vendor->hdr.ctype);
 +        /* reject invalid message without reporting to app */
 +        evt = 0;
 +        p_rc_rsp->rsp.status = AVRC_STS_BAD_CMD;
 +    }
 +    else
 +    {
 +        switch (pdu)
 +        {
 +        case AVRC_PDU_GET_CAPABILITIES:
 +            /* process GetCapabilities command without reporting the event to app */
 +            evt = 0;
 +            u8 = *(p_vendor->p_vendor_data + 4);
 +            p = p_vendor->p_vendor_data + 2;
 +            p_rc_rsp->get_caps.capability_id = u8;
 +            BE_STREAM_TO_UINT16 (u16, p);
 +            if ((u16 != 1) || (p_vendor->vendor_len != 5))
 +            {
 +                p_rc_rsp->get_caps.status = AVRC_STS_INTERNAL_ERR;
 +            }
 +            else
 +            {
 +                p_rc_rsp->get_caps.status = AVRC_STS_NO_ERROR;
 +                if (u8 == AVRC_CAP_COMPANY_ID)
 +                {
 +                    *p_ctype = AVRC_RSP_IMPL_STBL;
 +                    p_rc_rsp->get_caps.count = p_bta_av_cfg->num_co_ids;
 +                    memcpy(p_rc_rsp->get_caps.param.company_id, p_bta_av_cfg->p_meta_co_ids,
 +                           (p_bta_av_cfg->num_co_ids << 2));
 +                }
 +                else if (u8 == AVRC_CAP_EVENTS_SUPPORTED)
 +                {
 +                    *p_ctype = AVRC_RSP_IMPL_STBL;
 +                    p_rc_rsp->get_caps.count = p_bta_av_cfg->num_evt_ids;
 +                    memcpy(p_rc_rsp->get_caps.param.event_id, p_bta_av_cfg->p_meta_evt_ids,
 +                           p_bta_av_cfg->num_evt_ids);
 +                }
 +                else
 +                {
 +                    APPL_TRACE_DEBUG("Invalid capability ID: 0x%x", u8);
 +                    /* reject - unknown capability ID */
 +                    p_rc_rsp->get_caps.status = AVRC_STS_BAD_PARAM;
 +                }
 +            }
 +            break;
 +
 +
 +        case AVRC_PDU_REGISTER_NOTIFICATION:
 +            /* make sure the event_id is implemented */
 +            p_rc_rsp->rsp.status = bta_av_chk_notif_evt_id (p_vendor);
 +            if (p_rc_rsp->rsp.status != BTA_AV_STS_NO_RSP)
 +                evt = 0;
 +            break;
 +
 +        }
 +    }
 +#else
 +    APPL_TRACE_DEBUG("AVRCP 1.3 Metadata not supporteed. Reject command.");
 +    /* reject invalid message without reporting to app */
 +    evt = 0;
 +    p_rc_rsp->rsp.status = AVRC_STS_BAD_CMD;
 +#endif
 +
 +    return evt;
 +}
 +
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_rc_msg
 +**
 +** Description      Process an AVRCP message from the peer.
 +**
 +** Returns          void
 +**
 +*******************************************************************************/
 +void bta_av_rc_msg(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
 +{
 +    tBTA_AV_EVT evt = 0;
 +    tBTA_AV     av;
 +    BT_HDR      *p_pkt = NULL;
 +    tAVRC_MSG_VENDOR    *p_vendor = &p_data->rc_msg.msg.vendor;
 +    BOOLEAN is_inquiry = ((p_data->rc_msg.msg.hdr.ctype == AVRC_CMD_SPEC_INQ) || p_data->rc_msg.msg.hdr.ctype == AVRC_CMD_GEN_INQ);
 +#if (AVRC_METADATA_INCLUDED == TRUE)
 +    UINT8       ctype = 0;
 +    tAVRC_RESPONSE  rc_rsp;
 +
 +    rc_rsp.rsp.status = BTA_AV_STS_NO_RSP;
 +#endif
 +
 +    if (p_data->rc_msg.opcode == AVRC_OP_PASS_THRU)
 +    {
 +    /* if this is a pass thru command */
 +        if ((p_data->rc_msg.msg.hdr.ctype == AVRC_CMD_CTRL) ||
 +            (p_data->rc_msg.msg.hdr.ctype == AVRC_CMD_SPEC_INQ) ||
 +            (p_data->rc_msg.msg.hdr.ctype == AVRC_CMD_GEN_INQ)
 +            )
 +        {
 +        /* check if operation is supported */
 +            if (p_data->rc_msg.msg.pass.op_id == AVRC_ID_VENDOR)
 +            {
 +                p_data->rc_msg.msg.hdr.ctype = BTA_AV_RSP_NOT_IMPL;
 +#if (AVRC_METADATA_INCLUDED == TRUE)
 +                if (p_cb->features & BTA_AV_FEAT_METADATA)
 +                    p_data->rc_msg.msg.hdr.ctype =
 +                        bta_av_group_navi_supported(p_data->rc_msg.msg.pass.pass_len,
 +                        p_data->rc_msg.msg.pass.p_pass_data, is_inquiry);
 +#endif
 +            }
 +            else
 +            {
 +                p_data->rc_msg.msg.hdr.ctype = bta_av_op_supported(p_data->rc_msg.msg.pass.op_id, is_inquiry);
 +            }
 +
 +            APPL_TRACE_DEBUG("ctype %d",p_data->rc_msg.msg.hdr.ctype)
 +
 +            /* send response */
 +            if (p_data->rc_msg.msg.hdr.ctype != BTA_AV_RSP_INTERIM)
 +                AVRC_PassRsp(p_data->rc_msg.handle, p_data->rc_msg.label, &p_data->rc_msg.msg.pass);
 +
 +            /* set up for callback if supported */
 +            if (p_data->rc_msg.msg.hdr.ctype == BTA_AV_RSP_ACCEPT || p_data->rc_msg.msg.hdr.ctype == BTA_AV_RSP_INTERIM)
 +            {
 +                evt = BTA_AV_REMOTE_CMD_EVT;
 +                av.remote_cmd.rc_id = p_data->rc_msg.msg.pass.op_id;
 +                av.remote_cmd.key_state = p_data->rc_msg.msg.pass.state;
 +                av.remote_cmd.p_data = p_data->rc_msg.msg.pass.p_pass_data;
 +                av.remote_cmd.len = p_data->rc_msg.msg.pass.pass_len;
 +                memcpy(&av.remote_cmd.hdr, &p_data->rc_msg.msg.hdr, sizeof (tAVRC_HDR));
 +                av.remote_cmd.label = p_data->rc_msg.label;
 +            }
 +        }
 +        /* else if this is a pass thru response */
 +        else if (p_data->rc_msg.msg.hdr.ctype >= AVRC_RSP_ACCEPT)
 +        {
 +            /* set up for callback */
 +            evt = BTA_AV_REMOTE_RSP_EVT;
 +            av.remote_rsp.rc_id = p_data->rc_msg.msg.pass.op_id;
 +            av.remote_rsp.key_state = p_data->rc_msg.msg.pass.state;
 +            av.remote_rsp.rsp_code = p_data->rc_msg.msg.hdr.ctype;
 +            av.remote_rsp.label = p_data->rc_msg.label;
 +        }
 +        /* must be a bad ctype -> reject*/
 +        else
 +        {
 +            p_data->rc_msg.msg.hdr.ctype = BTA_AV_RSP_REJ;
 +            AVRC_PassRsp(p_data->rc_msg.handle, p_data->rc_msg.label, &p_data->rc_msg.msg.pass);
 +        }
 +    }
 +    /* else if this is a vendor specific command or response */
 +    else if (p_data->rc_msg.opcode == AVRC_OP_VENDOR)
 +    {
 +        /* set up for callback */
 +        av.vendor_cmd.code = p_data->rc_msg.msg.hdr.ctype;
 +        av.vendor_cmd.company_id = p_vendor->company_id;
 +        av.vendor_cmd.label = p_data->rc_msg.label;
 +        av.vendor_cmd.p_data = p_vendor->p_vendor_data;
 +        av.vendor_cmd.len = p_vendor->vendor_len;
 +
 +        /* if configured to support vendor specific and it's a command */
 +        if ((p_cb->features & BTA_AV_FEAT_VENDOR)  &&
 +            p_data->rc_msg.msg.hdr.ctype <= AVRC_CMD_GEN_INQ)
 +        {
 +#if (AVRC_METADATA_INCLUDED == TRUE)
 +            if ((p_cb->features & BTA_AV_FEAT_METADATA) &&
 +               (p_vendor->company_id == AVRC_CO_METADATA))
 +            {
 +                av.meta_msg.p_msg = &p_data->rc_msg.msg;
 +                evt = bta_av_proc_meta_cmd (&rc_rsp, &p_data->rc_msg, &ctype);
 +            }
 +            else
 +#endif
 +                evt = BTA_AV_VENDOR_CMD_EVT;
 +        }
 +        /* else if configured to support vendor specific and it's a response */
 +        else if ((p_cb->features & BTA_AV_FEAT_VENDOR) &&
 +                 p_data->rc_msg.msg.hdr.ctype >= AVRC_RSP_ACCEPT)
 +        {
 +#if (AVRC_METADATA_INCLUDED == TRUE)
 +            if ((p_cb->features & BTA_AV_FEAT_METADATA) &&
 +               (p_vendor->company_id == AVRC_CO_METADATA))
 +            {
 +                av.meta_msg.p_msg = &p_data->rc_msg.msg;
 +                evt = BTA_AV_META_MSG_EVT;
 +            }
 +            else
 +#endif
 +                evt = BTA_AV_VENDOR_RSP_EVT;
 +
 +        }
 +        /* else if not configured to support vendor specific and it's a command */
 +        else if (!(p_cb->features & BTA_AV_FEAT_VENDOR)  &&
 +            p_data->rc_msg.msg.hdr.ctype <= AVRC_CMD_GEN_INQ)
 +        {
 +           if(p_data->rc_msg.msg.vendor.p_vendor_data[0] == AVRC_PDU_INVALID)
 +           {
 +           /* reject it */
 +              p_data->rc_msg.msg.hdr.ctype = BTA_AV_RSP_REJ;
 +              p_data->rc_msg.msg.vendor.p_vendor_data[4] = AVRC_STS_BAD_CMD;
 +           }
 +           else
 +              p_data->rc_msg.msg.hdr.ctype = BTA_AV_RSP_NOT_IMPL;
 +           AVRC_VendorRsp(p_data->rc_msg.handle, p_data->rc_msg.label, &p_data->rc_msg.msg.vendor);
 +        }
 +    }
 +#if (AVRC_METADATA_INCLUDED == TRUE)
 +    if (evt == 0 && rc_rsp.rsp.status != BTA_AV_STS_NO_RSP)
 +    {
 +        if (!p_pkt)
 +        {
 +            rc_rsp.rsp.opcode = p_data->rc_msg.opcode;
 +            AVRC_BldResponse (0, &rc_rsp, &p_pkt);
 +        }
 +        if (p_pkt)
 +            AVRC_MsgReq (p_data->rc_msg.handle, p_data->rc_msg.label, ctype, p_pkt);
 +    }
 +#endif
 +
 +    /* call callback */
 +    if (evt != 0)
 +    {
 +        av.remote_cmd.rc_handle = p_data->rc_msg.handle;
 +        (*p_cb->p_cback)(evt, &av);
 +    }
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_rc_close
 +**
 +** Description      close the specified AVRC handle.
 +**
 +** Returns          void
 +**
 +*******************************************************************************/
 +void bta_av_rc_close (tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
 +{
 +    UINT16 handle = p_data->hdr.layer_specific;
 +    tBTA_AV_SCB  *p_scb;
 +    tBTA_AV_RCB *p_rcb;
 +
 +    if(handle < BTA_AV_NUM_RCB)
 +    {
 +        p_rcb = &p_cb->rcb[handle];
 +
 +        APPL_TRACE_DEBUG("bta_av_rc_close handle: %d, status=0x%x", p_rcb->handle, p_rcb->status);
 +        if(p_rcb->handle != BTA_AV_RC_HANDLE_NONE)
 +        {
 +            if(p_rcb->shdl)
 +            {
 +                p_scb = bta_av_cb.p_scb[p_rcb->shdl - 1];
 +                if(p_scb)
 +                {
 +                    /* just in case the RC timer is active
 +                    if(bta_av_cb.features & BTA_AV_FEAT_RCCT &&
 +                       p_scb->chnl == BTA_AV_CHNL_AUDIO) */
 +                        bta_sys_stop_timer(&p_scb->timer);
 +                }
 +            }
 +
 +            AVRC_Close(p_rcb->handle);
 +        }
 +    }
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_get_shdl
 +**
 +** Returns          The index to p_scb[]
 +**
 +*******************************************************************************/
 +static UINT8 bta_av_get_shdl(tBTA_AV_SCB *p_scb)
 +{
 +    int     i;
 +    UINT8   shdl = 0;
 +    /* find the SCB & stop the timer */
 +    for(i=0; i<BTA_AV_NUM_STRS; i++)
 +    {
 +        if(p_scb == bta_av_cb.p_scb[i])
 +        {
 +            shdl = i+1;
 +            break;
 +        }
 +    }
 +    return shdl;
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_stream_chg
 +**
 +** Description      audio streaming status changed.
 +**
 +** Returns          void
 +**
 +*******************************************************************************/
 +void bta_av_stream_chg(tBTA_AV_SCB *p_scb, BOOLEAN started)
 +{
 +    UINT8   started_msk;
 +    int     i;
 +    UINT8   *p_streams;
 +    BOOLEAN no_streams = FALSE;
 +    tBTA_AV_SCB *p_scbi;
 +
 +    started_msk = BTA_AV_HNDL_TO_MSK(p_scb->hdi);
 +    APPL_TRACE_DEBUG ("bta_av_stream_chg started:%d started_msk:x%x chnl:x%x", started,
 +                                                  started_msk, p_scb->chnl);
 +    if (BTA_AV_CHNL_AUDIO == p_scb->chnl)
 +        p_streams = &bta_av_cb.audio_streams;
 +    else
 +        p_streams = &bta_av_cb.video_streams;
 +
 +    if (started)
 +    {
 +        /* Let L2CAP know this channel is processed with high priority */
 +        L2CA_SetAclPriority(p_scb->peer_addr, L2CAP_PRIORITY_HIGH);
 +        (*p_streams) |= started_msk;
 +    }
 +    else
 +    {
 +        (*p_streams) &= ~started_msk;
 +    }
 +
 +    if (!started)
 +    {
 +        i=0;
 +        if (BTA_AV_CHNL_AUDIO == p_scb->chnl)
 +        {
 +            if (bta_av_cb.video_streams == 0)
 +                no_streams = TRUE;
 +        }
 +        else
 +        {
 +            no_streams = TRUE;
 +            if ( bta_av_cb.audio_streams )
 +            {
 +                for (; i<BTA_AV_NUM_STRS; i++)
 +                {
 +                    p_scbi = bta_av_cb.p_scb[i];
 +                    /* scb is used and started */
 +                    if ( p_scbi && (bta_av_cb.audio_streams & BTA_AV_HNDL_TO_MSK(i))
 +                        && bdcmp(p_scbi->peer_addr, p_scb->peer_addr) == 0)
 +                    {
 +                        no_streams = FALSE;
 +                        break;
 +                    }
 +                }
 +
 +            }
 +        }
 +
 +        APPL_TRACE_DEBUG ("no_streams:%d i:%d, audio_streams:x%x, video_streams:x%x", no_streams, i,
 +                           bta_av_cb.audio_streams, bta_av_cb.video_streams);
 +        if (no_streams)
 +        {
 +            /* Let L2CAP know this channel is processed with low priority */
 +            L2CA_SetAclPriority(p_scb->peer_addr, L2CAP_PRIORITY_NORMAL);
 +        }
 +    }
 +}
 +
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_conn_chg
 +**
 +** Description      connetion status changed.
 +**                  Open an AVRCP acceptor channel, if new conn.
 +**
 +** Returns          void
 +**
 +*******************************************************************************/
 +void bta_av_conn_chg(tBTA_AV_DATA *p_data)
 +{
 +    tBTA_AV_CB   *p_cb = &bta_av_cb;
 +    tBTA_AV_SCB     *p_scb = NULL;
 +    tBTA_AV_SCB     *p_scbi;
 +    UINT8   mask;
 +    UINT8   conn_msk;
 +    UINT8   old_msk;
 +    int i;
 +    int index = (p_data->hdr.layer_specific & BTA_AV_HNDL_MSK) - 1;
 +    tBTA_AV_LCB *p_lcb;
 +    tBTA_AV_LCB *p_lcb_rc;
 +    tBTA_AV_RCB *p_rcb, *p_rcb2;
 +    BOOLEAN     chk_restore = FALSE;
 +
 +    /* Validate array index*/
 +    if (index < BTA_AV_NUM_STRS)
 +    {
 +        p_scb = p_cb->p_scb[index];
 +    }
 +    mask = BTA_AV_HNDL_TO_MSK(index);
 +    p_lcb = bta_av_find_lcb(p_data->conn_chg.peer_addr, BTA_AV_LCB_FIND);
 +    conn_msk = 1 << (index + 1);
 +    if(p_data->conn_chg.is_up)
 +    {
 +        /* set the conned mask for this channel */
 +        if(p_scb)
 +        {
 +            if(p_lcb)
 +            {
 +                p_lcb->conn_msk |= conn_msk;
 +                for (i=0; i<BTA_AV_NUM_RCB; i++)
 +                {
 +                    if (bta_av_cb.rcb[i].lidx == p_lcb->lidx)
 +                    {
 +                        bta_av_cb.rcb[i].shdl = index + 1;
 +                        APPL_TRACE_DEBUG("conn_chg up[%d]: %d, status=0x%x, shdl:%d, lidx:%d", i,
 +                                          bta_av_cb.rcb[i].handle, bta_av_cb.rcb[i].status,
 +                                          bta_av_cb.rcb[i].shdl, bta_av_cb.rcb[i].lidx);
 +                        break;
 +                    }
 +                }
 +            }
 +            if (p_scb->chnl == BTA_AV_CHNL_AUDIO)
 +            {
 +                old_msk = p_cb->conn_audio;
 +                p_cb->conn_audio |= mask;
 +            }
 +            else
 +            {
 +                old_msk = p_cb->conn_video;
 +                p_cb->conn_video |= mask;
 +            }
 +
 +            if ((old_msk & mask) == 0)
 +            {
 +                /* increase the audio open count, if not set yet */
 +                bta_av_cb.audio_open_cnt++;
 +            }
 +
 +
 +            APPL_TRACE_DEBUG("rc_acp_handle:%d rc_acp_idx:%d", p_cb->rc_acp_handle, p_cb->rc_acp_idx);
 +            /* check if the AVRCP ACP channel is already connected */
 +            if(p_lcb && p_cb->rc_acp_handle != BTA_AV_RC_HANDLE_NONE && p_cb->rc_acp_idx)
 +            {
 +                p_lcb_rc = &p_cb->lcb[BTA_AV_NUM_LINKS];
 +                APPL_TRACE_DEBUG("rc_acp is connected && conn_chg on same addr p_lcb_rc->conn_msk:x%x",
 +                                  p_lcb_rc->conn_msk);
 +                /* check if the RC is connected to the scb addr */
 +                APPL_TRACE_DEBUG ("p_lcb_rc->addr: %02x:%02x:%02x:%02x:%02x:%02x",
 +                       p_lcb_rc->addr[0], p_lcb_rc->addr[1], p_lcb_rc->addr[2], p_lcb_rc->addr[3],
 +                       p_lcb_rc->addr[4], p_lcb_rc->addr[5]);
 +                APPL_TRACE_DEBUG ("conn_chg.peer_addr: %02x:%02x:%02x:%02x:%02x:%02x",
 +                       p_data->conn_chg.peer_addr[0], p_data->conn_chg.peer_addr[1],
 +                       p_data->conn_chg.peer_addr[2],
 +                       p_data->conn_chg.peer_addr[3], p_data->conn_chg.peer_addr[4],
 +                       p_data->conn_chg.peer_addr[5]);
 +                if (p_lcb_rc->conn_msk && bdcmp(p_lcb_rc->addr, p_data->conn_chg.peer_addr) == 0)
 +                {
 +                    /* AVRCP is already connected.
 +                     * need to update the association betwen SCB and RCB */
 +                    p_lcb_rc->conn_msk = 0; /* indicate RC ONLY is not connected */
 +                    p_lcb_rc->lidx = 0;
 +                    p_scb->rc_handle = p_cb->rc_acp_handle;
 +                    p_rcb = &p_cb->rcb[p_cb->rc_acp_idx - 1];
 +                    p_rcb->shdl = bta_av_get_shdl(p_scb);
 +                    APPL_TRACE_DEBUG("update rc_acp shdl:%d/%d srch:%d", index + 1, p_rcb->shdl,
 +                                      p_scb->rc_handle );
 +
 +                    p_rcb2 = bta_av_get_rcb_by_shdl(p_rcb->shdl);
 +                    if (p_rcb2)
 +                    {
 +                        /* found the RCB that was created to associated with this SCB */
 +                        p_cb->rc_acp_handle = p_rcb2->handle;
 +                        p_cb->rc_acp_idx = (p_rcb2 - p_cb->rcb) + 1;
 +                        APPL_TRACE_DEBUG("new rc_acp_handle:%d, idx:%d", p_cb->rc_acp_handle,
 +                                           p_cb->rc_acp_idx);
 +                        p_rcb2->lidx = (BTA_AV_NUM_LINKS + 1);
 +                        APPL_TRACE_DEBUG("rc2 handle:%d lidx:%d/%d",p_rcb2->handle, p_rcb2->lidx,
 +                                          p_cb->lcb[p_rcb2->lidx-1].lidx);
 +                    }
 +                    p_rcb->lidx = p_lcb->lidx;
 +                    APPL_TRACE_DEBUG("rc handle:%d lidx:%d/%d",p_rcb->handle, p_rcb->lidx,
 +                                      p_cb->lcb[p_rcb->lidx-1].lidx);
 +                }
 +            }
 +        }
 +    }
 +    else
 +    {
 +        if ((p_cb->conn_audio & mask) && bta_av_cb.audio_open_cnt)
 +        {
 +            /* this channel is still marked as open. decrease the count */
 +            bta_av_cb.audio_open_cnt--;
 +        }
 +
 +        /* clear the conned mask for this channel */
 +        p_cb->conn_audio &= ~mask;
 +        p_cb->conn_video &= ~mask;
 +        if(p_scb)
 +        {
 +            /* the stream is closed.
 +             * clear the peer address, so it would not mess up the AVRCP for the next round of operation */
 +            bdcpy(p_scb->peer_addr, bd_addr_null);
 +            if(p_scb->chnl == BTA_AV_CHNL_AUDIO)
 +            {
 +                if(p_lcb)
 +                {
 +                    p_lcb->conn_msk &= ~conn_msk;
 +                }
 +                /* audio channel is down. make sure the INT channel is down */
 +                /* just in case the RC timer is active
 +                if(p_cb->features & BTA_AV_FEAT_RCCT) */
 +                {
 +                    bta_sys_stop_timer(&p_scb->timer);
 +                }
 +                /* one audio channel goes down. check if we need to restore high priority */
 +                chk_restore = TRUE;
 +            }
 +        }
 +
 +        APPL_TRACE_DEBUG("bta_av_conn_chg shdl:%d", index + 1);
 +        for (i=0; i<BTA_AV_NUM_RCB; i++)
 +        {
 +            APPL_TRACE_DEBUG("conn_chg dn[%d]: %d, status=0x%x, shdl:%d, lidx:%d", i,
 +                              bta_av_cb.rcb[i].handle, bta_av_cb.rcb[i].status,
 +                              bta_av_cb.rcb[i].shdl, bta_av_cb.rcb[i].lidx);
 +            if(bta_av_cb.rcb[i].shdl == index + 1)
 +            {
 +                bta_av_del_rc(&bta_av_cb.rcb[i]);
 +                break;
 +            }
 +        }
 +
 +        if(p_cb->conn_audio == 0 && p_cb->conn_video == 0)
 +        {
 +            /* if both channels are not connected,
 +             * close all RC channels */
 +            bta_av_close_all_rc(p_cb);
 +        }
 +
 +        /* if the AVRCP is no longer listening, create the listening channel */
 +        if (bta_av_cb.rc_acp_handle == BTA_AV_RC_HANDLE_NONE && bta_av_cb.features & BTA_AV_FEAT_RCTG)
 +            bta_av_rc_create(&bta_av_cb, AVCT_ACP, 0, BTA_AV_NUM_LINKS + 1);
 +    }
 +
 +    APPL_TRACE_DEBUG("bta_av_conn_chg audio:%x video:%x up:%d conn_msk:0x%x chk_restore:%d audio_open_cnt:%d",
 +        p_cb->conn_audio, p_cb->conn_video, p_data->conn_chg.is_up, conn_msk, chk_restore, p_cb->audio_open_cnt);
 +
 +    if (chk_restore)
 +    {
 +        if (p_cb->audio_open_cnt == 1)
 +        {
 +            /* one audio channel goes down and there's one audio channel remains open.
 +             * restore the switch role in default link policy */
 +            bta_sys_set_default_policy(BTA_ID_AV, HCI_ENABLE_MASTER_SLAVE_SWITCH);
 +            /* allow role switch, if this is the last connection */
 +            bta_av_restore_switch();
 +        }
 +        if (p_cb->audio_open_cnt)
 +        {
 +            /* adjust flush timeout settings to longer period */
 +            for (i=0; i<BTA_AV_NUM_STRS; i++)
 +            {
 +                p_scbi = bta_av_cb.p_scb[i];
 +                if (p_scbi && p_scbi->chnl == BTA_AV_CHNL_AUDIO && p_scbi->co_started)
 +                {
 +                    /* may need to update the flush timeout of this already started stream */
 +                    if (p_scbi->co_started != bta_av_cb.audio_open_cnt)
 +                    {
 +                        p_scbi->co_started = bta_av_cb.audio_open_cnt;
 +                        L2CA_SetFlushTimeout(p_scbi->peer_addr, p_bta_av_cfg->p_audio_flush_to[p_scbi->co_started - 1] );
 +                    }
 +                }
 +            }
 +        }
 +    }
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_disable
 +**
 +** Description      disable AV.
 +**
 +** Returns          void
 +**
 +*******************************************************************************/
 +void bta_av_disable(tBTA_AV_CB *p_cb, tBTA_AV_DATA *p_data)
 +{
 +    BT_HDR  hdr;
 +    UINT16  xx;
 +    UNUSED(p_data);
 +
 +    p_cb->disabling = TRUE;
 +
 +    bta_av_close_all_rc(p_cb);
 +
 +    utl_freebuf((void **) &p_cb->p_disc_db);
 +
 +    /* disable audio/video - de-register all channels,
 +     * expect BTA_AV_DEREG_COMP_EVT when deregister is complete */
 +    for(xx=0; xx<BTA_AV_NUM_STRS; xx++)
 +    {
 +        hdr.layer_specific = xx + 1;
 +        bta_av_api_deregister((tBTA_AV_DATA *)&hdr);
 +    }
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_api_disconnect
 +**
 +** Description      .
 +**
 +** Returns          void
 +**
 +*******************************************************************************/
 +void bta_av_api_disconnect(tBTA_AV_DATA *p_data)
 +{
 +    AVDT_DisconnectReq(p_data->api_discnt.bd_addr, bta_av_conn_cback);
 +    bta_sys_stop_timer(&bta_av_cb.sig_tmr);
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_sig_chg
 +**
 +** Description      process AVDT signal channel up/down.
 +**
 +** Returns          void
 +**
 +*******************************************************************************/
 +void bta_av_sig_chg(tBTA_AV_DATA *p_data)
 +{
 +    UINT16 event = p_data->str_msg.hdr.layer_specific;
 +    tBTA_AV_CB   *p_cb = &bta_av_cb;
 +    int     xx;
 +    UINT8   mask;
 +    tBTA_AV_LCB *p_lcb = NULL;
 +
 +    APPL_TRACE_DEBUG("bta_av_sig_chg event: %d", event);
 +    if(event == AVDT_CONNECT_IND_EVT)
 +    {
 +        p_lcb = bta_av_find_lcb(p_data->str_msg.bd_addr, BTA_AV_LCB_FIND);
 +        if(!p_lcb)
 +        {
 +            /* if the address does not have an LCB yet, alloc one */
 +            for(xx=0; xx<BTA_AV_NUM_LINKS; xx++)
 +            {
 +                mask = 1 << xx;
 +                APPL_TRACE_DEBUG("conn_lcb: 0x%x", p_cb->conn_lcb);
 +
 +                /* look for a p_lcb with its p_scb registered */
 +                if((!(mask & p_cb->conn_lcb)) && (p_cb->p_scb[xx] != NULL))
 +                {
 +                    p_lcb = &p_cb->lcb[xx];
 +                    p_lcb->lidx = xx + 1;
 +                    bdcpy(p_lcb->addr, p_data->str_msg.bd_addr);
 +                    p_lcb->conn_msk = 0; /* clear the connect mask */
 +                    /* start listening when the signal channel is open */
 +                    if (p_cb->features & BTA_AV_FEAT_RCTG)
 +                    {
 +                        bta_av_rc_create(p_cb, AVCT_ACP, 0, p_lcb->lidx);
 +                    }
 +                    /* this entry is not used yet. */
 +                    p_cb->conn_lcb |= mask;     /* mark it as used */
 +                    APPL_TRACE_DEBUG("start sig timer %d", p_data->hdr.offset);
 +                    if (p_data->hdr.offset == AVDT_ACP)
 +                    {
 +                        APPL_TRACE_DEBUG("Incoming L2CAP acquired, set state as incoming");
 +                        bdcpy(p_cb->p_scb[xx]->peer_addr, p_data->str_msg.bd_addr);
 +                        p_cb->p_scb[xx]->use_rc = TRUE;     /* allowing RC for incoming connection */
 +                        bta_av_ssm_execute(p_cb->p_scb[xx], BTA_AV_ACP_CONNECT_EVT, p_data);
 +
 +                        /* The Pending Event should be sent as soon as the L2CAP signalling channel
 +                         * is set up, which is NOW. Earlier this was done only after
 +                         * BTA_AV_SIG_TIME_VAL milliseconds.
 +                         * The following function shall send the event and start the recurring timer
 +                         */
 +                        bta_av_sig_timer(NULL);
 +
 +                        /* Possible collision : need to avoid outgoing processing while the timer is running */
 +                        p_cb->p_scb[xx]->coll_mask = BTA_AV_COLL_INC_TMR;
 +
 +                        p_cb->acp_sig_tmr.param = (UINT32)xx;
 +                        p_cb->acp_sig_tmr.p_cback = (TIMER_CBACK*)&bta_av_acp_sig_timer_cback;
 +                        bta_sys_start_timer(&p_cb->acp_sig_tmr, 0, BTA_AV_ACP_SIG_TIME_VAL);
 +                    }
 +                    break;
 +                }
 +            }
 +
 +            /* check if we found something */
 +            if (xx == BTA_AV_NUM_LINKS)
 +            {
 +                /* We do not have scb for this avdt connection.     */
 +                /* Silently close the connection.                   */
 +                APPL_TRACE_ERROR("av scb not available for avdt connection");
 +                AVDT_DisconnectReq (p_data->str_msg.bd_addr, NULL);
 +                return;
 +            }
 +        }
 +    }
 +#if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
 +    else if (event == BTA_AR_AVDT_CONN_EVT)
 +    {
 +        bta_sys_stop_timer(&bta_av_cb.sig_tmr);
 +    }
 +#endif
 +    else
 +    {
 +        /* disconnected. */
 +        p_lcb = bta_av_find_lcb(p_data->str_msg.bd_addr, BTA_AV_LCB_FREE);
 +        if(p_lcb && p_lcb->conn_msk)
 +        {
 +            APPL_TRACE_DEBUG("conn_msk: 0x%x", p_lcb->conn_msk);
 +            /* clean up ssm  */
 +            for(xx=0; xx < BTA_AV_NUM_STRS; xx++)
 +            {
 +                mask = 1 << (xx + 1);
 +                if ((mask & p_lcb->conn_msk) && (p_cb->p_scb[xx]) &&
 +                    (bdcmp(p_cb->p_scb[xx]->peer_addr, p_data->str_msg.bd_addr) == 0))
 +                {
 +                    p_cb->p_scb[xx]->disc_rsn = p_data->str_msg.hdr.offset;
 +                    bta_av_ssm_execute(p_cb->p_scb[xx], BTA_AV_AVDT_DISCONNECT_EVT, NULL);
 +                }
 +            }
 +        }
 +    }
 +    APPL_TRACE_DEBUG("conn_lcb: 0x%x", p_cb->conn_lcb);
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_sig_timer
 +**
 +** Description      process the signal channel timer. This timer is started
 +**                  when the AVDTP signal channel is connected. If no profile
 +**                  is connected, the timer goes off every BTA_AV_SIG_TIME_VAL
 +**
 +** Returns          void
 +**
 +*******************************************************************************/
 +void bta_av_sig_timer(tBTA_AV_DATA *p_data)
 +{
 +    tBTA_AV_CB   *p_cb = &bta_av_cb;
 +    int     xx;
 +    UINT8   mask;
 +    tBTA_AV_LCB *p_lcb = NULL;
 +    tBTA_AV_PEND pend;
 +    UNUSED(p_data);
 +
 +    APPL_TRACE_DEBUG("bta_av_sig_timer");
 +    for(xx=0; xx<BTA_AV_NUM_LINKS; xx++)
 +    {
 +        mask = 1 << xx;
 +        if(mask & p_cb->conn_lcb)
 +        {
 +            /* this entry is used. check if it is connected */
 +            p_lcb = &p_cb->lcb[xx];
 +            if(!p_lcb->conn_msk)
 +            {
 +                bta_sys_start_timer(&p_cb->sig_tmr, BTA_AV_SIG_TIMER_EVT, BTA_AV_SIG_TIME_VAL);
 +                bdcpy(pend.bd_addr, p_lcb->addr);
 +                (*p_cb->p_cback)(BTA_AV_PENDING_EVT, (tBTA_AV *) &pend);
 +            }
 +        }
 +    }
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_acp_sig_timer_cback
 +**
 +** Description      Process the timeout when SRC is accepting connection
 +**                  and SNK did not start signalling.
 +**
 +** Returns          void
 +**
 +*******************************************************************************/
 +static void bta_av_acp_sig_timer_cback (TIMER_LIST_ENT *p_tle)
 +{
 +    UINT8   inx = (UINT8)p_tle->param;
 +    tBTA_AV_CB  *p_cb = &bta_av_cb;
 +    tBTA_AV_SCB *p_scb = NULL;
 +    tBTA_AV_API_OPEN  *p_buf;
 +    if (inx < BTA_AV_NUM_STRS)
 +    {
 +        p_scb = p_cb->p_scb[inx];
 +    }
 +    if (p_scb)
 +    {
 +        APPL_TRACE_DEBUG("bta_av_acp_sig_timer_cback, coll_mask = 0x%02X", p_scb->coll_mask);
 +
 +        if (p_scb->coll_mask & BTA_AV_COLL_INC_TMR)
 +        {
 +            p_scb->coll_mask &= ~BTA_AV_COLL_INC_TMR;
 +
 +            if (bta_av_is_scb_opening(p_scb))
 +            {
 +                if (p_scb->p_disc_db)
 +                {
 +                    /* We are still doing SDP. Run the timer again. */
 +                    p_scb->coll_mask |= BTA_AV_COLL_INC_TMR;
 +
 +                    p_cb->acp_sig_tmr.param = (UINT32)inx;
 +                    p_cb->acp_sig_tmr.p_cback = (TIMER_CBACK *)&bta_av_acp_sig_timer_cback;
 +                    bta_sys_start_timer(&p_cb->acp_sig_tmr, 0, BTA_AV_ACP_SIG_TIME_VAL);
 +                }
 +                else
 +                {
 +                    /* SNK did not start signalling, resume signalling process. */
 +                    bta_av_discover_req (p_scb, NULL);
 +                }
 +            }
 +            else if (bta_av_is_scb_incoming(p_scb))
 +            {
 +                /* Stay in incoming state if SNK does not start signalling */
 +
 +                /* API open was called right after SNK opened L2C connection. */
 +                if (p_scb->coll_mask & BTA_AV_COLL_API_CALLED)
 +                {
 +                    p_scb->coll_mask &= ~BTA_AV_COLL_API_CALLED;
 +
 +                    /* BTA_AV_API_OPEN_EVT */
 +                    if ((p_buf = (tBTA_AV_API_OPEN *) GKI_getbuf(sizeof(tBTA_AV_API_OPEN))) != NULL)
 +                    {
 +                        memcpy(p_buf, &(p_scb->open_api), sizeof(tBTA_AV_API_OPEN));
 +                        bta_sys_sendmsg(p_buf);
 +                    }
 +                }
 +            }
 +        }
 +    }
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_check_peer_features
 +**
 +** Description      check supported features on the peer device from the SDP record
 +**                  and return the feature mask
 +**
 +** Returns          tBTA_AV_FEAT peer device feature mask
 +**
 +*******************************************************************************/
 +tBTA_AV_FEAT bta_av_check_peer_features (UINT16 service_uuid)
 +{
 +    tBTA_AV_FEAT peer_features = 0;
 +    tBTA_AV_CB   *p_cb = &bta_av_cb;
 +    tSDP_DISC_REC       *p_rec = NULL;
 +    tSDP_DISC_ATTR      *p_attr;
 +    UINT16              peer_rc_version=0;
 +    UINT16              categories = 0;
 +
 +    APPL_TRACE_DEBUG("bta_av_check_peer_features service_uuid:x%x", service_uuid);
 +    /* loop through all records we found */
 +    while (TRUE)
 +    {
 +        /* get next record; if none found, we're done */
 +        if ((p_rec = SDP_FindServiceInDb(p_cb->p_disc_db, service_uuid, p_rec)) == NULL)
 +        {
 +            break;
 +        }
 +
 +        if (( SDP_FindAttributeInRec(p_rec, ATTR_ID_SERVICE_CLASS_ID_LIST)) != NULL)
 +        {
 +            /* find peer features */
 +            if (SDP_FindServiceInDb(p_cb->p_disc_db, UUID_SERVCLASS_AV_REMOTE_CONTROL, NULL))
 +            {
 +                peer_features |= BTA_AV_FEAT_RCCT;
 +            }
 +            if (SDP_FindServiceInDb(p_cb->p_disc_db, UUID_SERVCLASS_AV_REM_CTRL_TARGET, NULL))
 +            {
 +                peer_features |= BTA_AV_FEAT_RCTG;
 +            }
 +        }
 +
 +        if (( SDP_FindAttributeInRec(p_rec, ATTR_ID_BT_PROFILE_DESC_LIST)) != NULL)
 +        {
 +            /* get profile version (if failure, version parameter is not updated) */
 +            SDP_FindProfileVersionInRec(p_rec, UUID_SERVCLASS_AV_REMOTE_CONTROL, &peer_rc_version);
 +            APPL_TRACE_DEBUG("peer_rc_version 0x%x", peer_rc_version);
 +
 +            if (peer_rc_version >= AVRC_REV_1_3)
 +                peer_features |= (BTA_AV_FEAT_VENDOR | BTA_AV_FEAT_METADATA);
 +
 +            if (peer_rc_version >= AVRC_REV_1_4)
 +            {
 +                peer_features |= (BTA_AV_FEAT_ADV_CTRL);
 +                /* get supported categories */
 +                if ((p_attr = SDP_FindAttributeInRec(p_rec,
 +                                ATTR_ID_SUPPORTED_FEATURES)) != NULL)
 +                {
 +                    categories = p_attr->attr_value.v.u16;
 +                    if (categories & AVRC_SUPF_CT_BROWSE)
 +                        peer_features |= (BTA_AV_FEAT_BROWSE);
 +                }
 +            }
 +        }
 +    }
 +    APPL_TRACE_DEBUG("peer_features:x%x", peer_features);
 +    return peer_features;
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_rc_disc_done
 +**
 +** Description      Handle AVRCP service discovery results.  If matching
 +**                  service found, open AVRCP connection.
 +**
 +** Returns          void
 +**
 +*******************************************************************************/
 +void bta_av_rc_disc_done(tBTA_AV_DATA *p_data)
 +{
 +    tBTA_AV_CB   *p_cb = &bta_av_cb;
 +    tBTA_AV_SCB  *p_scb = NULL;
 +    tBTA_AV_LCB  *p_lcb;
 +    tBTA_AV_RC_OPEN rc_open;
 +    tBTA_AV_RC_FEAT rc_feat;
 +    UINT8               rc_handle;
 +    tBTA_AV_FEAT        peer_features;  /* peer features mask */
 +    UNUSED(p_data);
 +
 +    APPL_TRACE_DEBUG("bta_av_rc_disc_done disc:x%x", p_cb->disc);
 +    if (!p_cb->disc)
 +    {
 +        return;
 +    }
 +
 +    if ((p_cb->disc & BTA_AV_CHNL_MSK) == BTA_AV_CHNL_MSK)
 +    {
 +        /* this is the rc handle/index to tBTA_AV_RCB */
 +        rc_handle = p_cb->disc & (~BTA_AV_CHNL_MSK);
 +    }
 +    else
 +    {
 +        /* Validate array index*/
 +        if (((p_cb->disc & BTA_AV_HNDL_MSK) - 1) < BTA_AV_NUM_STRS)
 +        {
 +            p_scb = p_cb->p_scb[(p_cb->disc & BTA_AV_HNDL_MSK) - 1];
 +        }
 +        if (p_scb)
 +            rc_handle = p_scb->rc_handle;
 +        else
 +        {
 +            p_cb->disc = 0;
 +            return;
 +        }
 +    }
 +
 +    APPL_TRACE_DEBUG("rc_handle %d", rc_handle);
 +    /* check peer version and whether support CT and TG role */
 +    peer_features = bta_av_check_peer_features (UUID_SERVCLASS_AV_REMOTE_CONTROL);
 +    if ((p_cb->features & BTA_AV_FEAT_ADV_CTRL) && ((peer_features&BTA_AV_FEAT_ADV_CTRL) == 0))
 +    {
 +        /* if we support advance control and peer does not, check their support on TG role
 +         * some implementation uses 1.3 on CT ans 1.4 on TG */
 +        peer_features |= bta_av_check_peer_features (UUID_SERVCLASS_AV_REM_CTRL_TARGET);
 +    }
 +
 +    p_cb->disc = 0;
 +    utl_freebuf((void **) &p_cb->p_disc_db);
 +
 +    APPL_TRACE_DEBUG("peer_features 0x%x, features 0x%x", peer_features, p_cb->features);
 +
 +    /* if we have no rc connection */
 +    if (rc_handle == BTA_AV_RC_HANDLE_NONE)
 +    {
 +        if (p_scb)
 +        {
 +            /* if peer remote control service matches ours and USE_RC is TRUE */
 +            if ((((p_cb->features & BTA_AV_FEAT_RCCT) && (peer_features & BTA_AV_FEAT_RCTG)) ||
 +                 ((p_cb->features & BTA_AV_FEAT_RCTG) && (peer_features & BTA_AV_FEAT_RCCT))) )
 +            {
 +                p_lcb = bta_av_find_lcb(p_scb->peer_addr, BTA_AV_LCB_FIND);
 +                if(p_lcb)
 +                {
 +                    rc_handle = bta_av_rc_create(p_cb, AVCT_INT, (UINT8)(p_scb->hdi + 1), p_lcb->lidx);
 +                    p_cb->rcb[rc_handle].peer_features = peer_features;
 +                }
 +#if (BT_USE_TRACES == TRUE || BT_TRACE_APPL == TRUE)
 +                else
 +                {
 +                    APPL_TRACE_ERROR("can not find LCB!!");
 +                }
 +#endif
 +            }
 +            else if(p_scb->use_rc)
 +            {
 +                /* can not find AVRC on peer device. report failure */
 +                p_scb->use_rc = FALSE;
 +                bdcpy(rc_open.peer_addr, p_scb->peer_addr);
 +                rc_open.peer_features = 0;
 +                rc_open.status = BTA_AV_FAIL_SDP;
 +                (*p_cb->p_cback)(BTA_AV_RC_OPEN_EVT, (tBTA_AV *) &rc_open);
 +            }
 +        }
 +    }
 +    else
 +    {
 +        p_cb->rcb[rc_handle].peer_features = peer_features;
 +        rc_feat.rc_handle =  rc_handle;
 +        rc_feat.peer_features = peer_features;
 +        (*p_cb->p_cback)(BTA_AV_RC_FEAT_EVT, (tBTA_AV *) &rc_feat);
 +    }
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_rc_closed
 +**
 +** Description      Set AVRCP state to closed.
 +**
 +** Returns          void
 +**
 +*******************************************************************************/
 +void bta_av_rc_closed(tBTA_AV_DATA *p_data)
 +{
 +    tBTA_AV_CB   *p_cb = &bta_av_cb;
 +    tBTA_AV_RC_CLOSE rc_close;
 +    tBTA_AV_RC_CONN_CHG *p_msg = (tBTA_AV_RC_CONN_CHG *)p_data;
 +    tBTA_AV_RCB    *p_rcb;
 +    tBTA_AV_SCB    *p_scb;
 +    int i;
 +    BOOLEAN conn = FALSE;
 +    tBTA_AV_LCB *p_lcb;
 +
 +    rc_close.rc_handle = BTA_AV_RC_HANDLE_NONE;
 +    p_scb = NULL;
 +    APPL_TRACE_DEBUG("bta_av_rc_closed rc_handle:%d", p_msg->handle);
 +    for(i=0; i<BTA_AV_NUM_RCB; i++)
 +    {
 +        p_rcb = &p_cb->rcb[i];
 +        APPL_TRACE_DEBUG("bta_av_rc_closed rcb[%d] rc_handle:%d, status=0x%x", i, p_rcb->handle, p_rcb->status);
 +        if(p_rcb->handle == p_msg->handle)
 +        {
 +            rc_close.rc_handle = i;
 +            p_rcb->status &= ~BTA_AV_RC_CONN_MASK;
 +            p_rcb->peer_features = 0;
 +            APPL_TRACE_DEBUG("       shdl:%d, lidx:%d", p_rcb->shdl, p_rcb->lidx);
 +            if(p_rcb->shdl)
 +            {
 +                if ((p_rcb->shdl - 1) < BTA_AV_NUM_STRS)
 +                {
 +                    p_scb = bta_av_cb.p_scb[p_rcb->shdl - 1];
 +                }
 +                if(p_scb)
 +                {
 +                    bdcpy(rc_close.peer_addr, p_scb->peer_addr);
 +                    if(p_scb->rc_handle == p_rcb->handle)
 +                        p_scb->rc_handle = BTA_AV_RC_HANDLE_NONE;
 +                    APPL_TRACE_DEBUG("shdl:%d, srch:%d", p_rcb->shdl, p_scb->rc_handle);
 +                }
 +                p_rcb->shdl = 0;
 +            }
 +            else if(p_rcb->lidx == (BTA_AV_NUM_LINKS + 1) )
 +            {
 +                /* if the RCB uses the extra LCB, use the addr for event and clean it */
 +                p_lcb = &p_cb->lcb[BTA_AV_NUM_LINKS];
 +                bdcpy(rc_close.peer_addr, p_msg->peer_addr);
 +                APPL_TRACE_DEBUG("rc_only closed bd_addr:%02x-%02x-%02x-%02x-%02x-%02x",
 +                              p_msg->peer_addr[0], p_msg->peer_addr[1],
 +                              p_msg->peer_addr[2], p_msg->peer_addr[3],
 +                              p_msg->peer_addr[4], p_msg->peer_addr[5]);
 +                p_lcb->conn_msk = 0;
 +                p_lcb->lidx = 0;
 +            }
 +            p_rcb->lidx = 0;
 +
 +            if((p_rcb->status & BTA_AV_RC_ROLE_MASK) == BTA_AV_RC_ROLE_INT)
 +            {
 +                /* AVCT CCB is deallocated */
 +                p_rcb->handle = BTA_AV_RC_HANDLE_NONE;
 +                p_rcb->status = 0;
 +            }
 +            else
 +            {
 +                /* AVCT CCB is still there. dealloc */
 +                bta_av_del_rc(p_rcb);
 +
 +                /* if the AVRCP is no longer listening, create the listening channel */
 +                if (bta_av_cb.rc_acp_handle == BTA_AV_RC_HANDLE_NONE && bta_av_cb.features & BTA_AV_FEAT_RCTG)
 +                    bta_av_rc_create(&bta_av_cb, AVCT_ACP, 0, BTA_AV_NUM_LINKS + 1);
 +            }
 +        }
 +        else if((p_rcb->handle != BTA_AV_RC_HANDLE_NONE) && (p_rcb->status & BTA_AV_RC_CONN_MASK))
 +        {
 +            /* at least one channel is still connected */
 +            conn = TRUE;
 +        }
 +    }
 +
 +    if(!conn)
 +    {
 +        /* no AVRC channels are connected, go back to INIT state */
 +        bta_av_sm_execute(p_cb, BTA_AV_AVRC_NONE_EVT, NULL);
 +    }
 +
 +    if (rc_close.rc_handle == BTA_AV_RC_HANDLE_NONE)
 +    {
 +        rc_close.rc_handle = p_msg->handle;
 +        bdcpy(rc_close.peer_addr, p_msg->peer_addr);
 +    }
 +    (*p_cb->p_cback)(BTA_AV_RC_CLOSE_EVT, (tBTA_AV *) &rc_close);
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_rc_disc
 +**
 +** Description      start AVRC SDP discovery.
 +**
 +** Returns          void
 +**
 +*******************************************************************************/
 +void bta_av_rc_disc(UINT8 disc)
 +{
 +    tBTA_AV_CB   *p_cb = &bta_av_cb;
 +    tAVRC_SDP_DB_PARAMS db_params;
 +      UINT16              attr_list[] = {ATTR_ID_SERVICE_CLASS_ID_LIST,
 +                                       ATTR_ID_BT_PROFILE_DESC_LIST,
 +                                       ATTR_ID_SUPPORTED_FEATURES};
 +    UINT8       hdi;
 +    tBTA_AV_SCB *p_scb;
 +    UINT8       *p_addr = NULL;
 +    UINT8       rc_handle;
 +
 +    APPL_TRACE_DEBUG("bta_av_rc_disc 0x%x, %d", disc, bta_av_cb.disc);
 +    if ((bta_av_cb.disc != 0) || (disc == 0))
 +        return;
 +
 +    if ((disc & BTA_AV_CHNL_MSK) == BTA_AV_CHNL_MSK)
 +    {
 +        /* this is the rc handle/index to tBTA_AV_RCB */
 +        rc_handle = disc & (~BTA_AV_CHNL_MSK);
 +        if (p_cb->rcb[rc_handle].lidx)
 +        {
 +            p_addr = p_cb->lcb[p_cb->rcb[rc_handle].lidx-1].addr;
 +        }
 +    }
 +    else
 +    {
 +        hdi = (disc & BTA_AV_HNDL_MSK) - 1;
 +        p_scb = p_cb->p_scb[hdi];
 +
 +        if (p_scb)
 +        {
 +            APPL_TRACE_DEBUG("rc_handle %d", p_scb->rc_handle);
 +            p_addr = p_scb->peer_addr;
 +        }
 +    }
 +
 +    if (p_addr)
 +    {
 +        /* allocate discovery database */
 +        if (p_cb->p_disc_db == NULL)
 +        {
 +            p_cb->p_disc_db = (tSDP_DISCOVERY_DB *) GKI_getbuf(BTA_AV_DISC_BUF_SIZE);
 +        }
 +
 +        if (p_cb->p_disc_db)
 +        {
 +            /* set up parameters */
 +            db_params.db_len = BTA_AV_DISC_BUF_SIZE;
 +            db_params.num_attr = 3;
 +            db_params.p_db = p_cb->p_disc_db;
 +            db_params.p_attrs = attr_list;
 +
 +            /* searching for UUID_SERVCLASS_AV_REMOTE_CONTROL gets both TG and CT */
 +            if (AVRC_FindService(UUID_SERVCLASS_AV_REMOTE_CONTROL, p_addr, &db_params,
 +                            bta_av_avrc_sdp_cback) == AVRC_SUCCESS)
 +            {
 +                p_cb->disc = disc;
 +                APPL_TRACE_DEBUG("disc %d", p_cb->disc);
 +            }
 +        }
 +    }
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         bta_av_dereg_comp
 +**
 +** Description      deregister complete. free the stream control block.
 +**
 +** Returns          void
 +**
 +*******************************************************************************/
 +void bta_av_dereg_comp(tBTA_AV_DATA *p_data)
 +{
 +    tBTA_AV_CB   *p_cb = &bta_av_cb;
 +    tBTA_AV_SCB  *p_scb;
 +    tBTA_UTL_COD    cod;
 +    UINT8   mask;
 +    BT_HDR  *p_buf;
 +
 +    /* find the stream control block */
 +    p_scb = bta_av_hndl_to_scb(p_data->hdr.layer_specific);
 +
 +    if(p_scb)
 +    {
 +        APPL_TRACE_DEBUG("deregistered %d(h%d)", p_scb->chnl, p_scb->hndl);
 +        mask = BTA_AV_HNDL_TO_MSK(p_scb->hdi);
 +        if(p_scb->chnl == BTA_AV_CHNL_AUDIO)
 +        {
 +            p_cb->reg_audio  &= ~mask;
 +            if ((p_cb->conn_audio & mask) && bta_av_cb.audio_open_cnt)
 +            {
 +                /* this channel is still marked as open. decrease the count */
 +                bta_av_cb.audio_open_cnt--;
 +            }
 +            p_cb->conn_audio &= ~mask;
 +
 +            if (p_scb->q_tag == BTA_AV_Q_TAG_STREAM && p_scb->a2d_list) {
 +                /* make sure no buffers are in a2d_list */
 +                while (!list_is_empty(p_scb->a2d_list)) {
 +                    p_buf = (BT_HDR*)list_front(p_scb->a2d_list);
 +                    list_remove(p_scb->a2d_list, p_buf);
 +                    GKI_freebuf(p_buf);
 +                }
 +            }
 +
 +            /* remove the A2DP SDP record, if no more audio stream is left */
 +            if(!p_cb->reg_audio)
 +            {
 +#if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
 +                bta_ar_dereg_avrc (UUID_SERVCLASS_AV_REMOTE_CONTROL, BTA_ID_AV);
 +#endif
 +                bta_av_del_sdp_rec(&p_cb->sdp_a2d_handle);
 +                bta_sys_remove_uuid(UUID_SERVCLASS_AUDIO_SOURCE);
 +
 +#if (BTA_AV_SINK_INCLUDED == TRUE)
 +                bta_av_del_sdp_rec(&p_cb->sdp_a2d_snk_handle);
 +                bta_sys_remove_uuid(UUID_SERVCLASS_AUDIO_SINK);
 +#endif
 +            }
 +        }
 +        else
 +        {
 +            p_cb->reg_video  &= ~mask;
 +            /* make sure that this channel is not connected */
 +            p_cb->conn_video &= ~mask;
 +            /* remove the VDP SDP record, (only one video stream at most) */
 +            bta_av_del_sdp_rec(&p_cb->sdp_vdp_handle);
 +            bta_sys_remove_uuid(UUID_SERVCLASS_VIDEO_SOURCE);
 +        }
 +
 +        /* make sure that the timer is not active */
 +        bta_sys_stop_timer(&p_scb->timer);
 +        utl_freebuf((void **)&p_cb->p_scb[p_scb->hdi]);
 +    }
 +
 +    APPL_TRACE_DEBUG("audio 0x%x, video: 0x%x, disable:%d",
 +        p_cb->reg_audio, p_cb->reg_video, p_cb->disabling);
 +    /* if no stream control block is active */
 +    if((p_cb->reg_audio + p_cb->reg_video) == 0)
 +    {
 +#if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
 +        /* deregister from AVDT */
 +        bta_ar_dereg_avdt(BTA_ID_AV);
 +
 +        /* deregister from AVCT */
 +        bta_ar_dereg_avrc (UUID_SERVCLASS_AV_REM_CTRL_TARGET, BTA_ID_AV);
 +        bta_ar_dereg_avct(BTA_ID_AV);
 +#endif
 +
 +        if(p_cb->disabling)
 +        {
 +            p_cb->disabling     = FALSE;
 +            bta_av_cb.features  = 0;
 +        }
 +
 +        /* Clear the Capturing service class bit */
 +        cod.service = BTM_COD_SERVICE_CAPTURING;
 +        utl_set_device_class(&cod, BTA_UTL_CLR_COD_SERVICE_CLASS);
 +    }
 +}
 +#endif /* BTA_AV_INCLUDED */
index 9d45db62c91d84dbb3951a9aeab9c0f54d74988d,0000000000000000000000000000000000000000..64406af4f00ca78a5bb8308d7ab8428237633223
mode 100644,000000..100644
--- /dev/null
@@@ -1,169 -1,0 +1,169 @@@
-             LOG_INFO("%s dropping duplicate connect request for uuid: %04x", __func__, p_param->uuid);
 +// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
 +//
 +// Licensed under the Apache License, Version 2.0 (the "License");
 +// you may not use this file except in compliance with the License.
 +// You may obtain a copy of the License at
 +
 +//     http://www.apache.org/licenses/LICENSE-2.0
 +//
 +// Unless required by applicable law or agreed to in writing, software
 +// distributed under the License is distributed on an "AS IS" BASIS,
 +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 +// See the License for the specific language governing permissions and
 +// limitations under the License.
 +
 +#include <string.h>
 +#include "esp_bt_main.h"
 +#include "bt_trace.h"
 +#include "bt_defs.h"
 +
 +#include "btc_profile_queue.h"
 +#include "gki.h"
 +#include "list.h"
 +#include "allocator.h"
 +
 +/*******************************************************************************
 +**  Local type definitions
 +*******************************************************************************/
 +/*******************************************************************************
 +**  Static variables
 +*******************************************************************************/
 +
 +static list_t *connect_queue;
 +
 +static const size_t MAX_REASONABLE_REQUESTS = 10;
 +
 +/*******************************************************************************
 +**  Queue helper functions
 +*******************************************************************************/
 +
 +static void queue_int_add(connect_node_t *p_param)
 +{
 +    if (!connect_queue) {
 +        connect_queue = list_new(osi_free_func);
 +        assert(connect_queue != NULL);
 +    }
 +
 +    // Sanity check to make sure we're not leaking connection requests
 +    assert(list_length(connect_queue) < MAX_REASONABLE_REQUESTS);
 +
 +    for (const list_node_t *node = list_begin(connect_queue); node != list_end(connect_queue); node = list_next(node)) {
 +        if (((connect_node_t *)list_node(node))->uuid == p_param->uuid) {
++            LOG_DEBUG("%s dropping duplicate connect request for uuid: %04x", __func__, p_param->uuid);
 +            return;
 +        }
 +    }
 +
 +    connect_node_t *p_node = osi_malloc(sizeof(connect_node_t));
 +    assert(p_node != NULL);
 +    memcpy(p_node, p_param, sizeof(connect_node_t));
 +    list_append(connect_queue, p_node);
 +}
 +
 +static void queue_int_advance()
 +{
 +    if (connect_queue && !list_is_empty(connect_queue)) {
 +        list_remove(connect_queue, list_front(connect_queue));
 +    }
 +}
 +
 +void btc_profile_queue_handler(btc_msg_t *msg)
 +{
 +    btc_prf_que_args_t *arg = (btc_prf_que_args_t *)(msg->arg);
 +    switch (msg->act) {
 +    case BTC_PRF_QUE_CONNECT:
 +        queue_int_add(&(arg->connect_node));
 +        break;
 +
 +    case BTC_PRF_QUE_ADVANCE:
 +        queue_int_advance();
 +        break;
 +    }
 +    
 +    if (esp_bluedroid_get_status() == ESP_BLUEDROID_STATUS_ENABLED) {
 +        btc_queue_connect_next();
 +    }
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         btc_queue_connect
 +**
 +** Description      Add a new connection to the queue and trigger the next
 +**                  scheduled connection.
 +**
 +** Returns          BT_STATUS_SUCCESS if successful
 +**
 +*******************************************************************************/
 +bt_status_t btc_queue_connect(uint16_t uuid, const bt_bdaddr_t *bda, btc_connect_cb_t connect_cb)
 +{
 +    btc_msg_t msg;
 +    btc_prf_que_args_t arg;
 +
 +    msg.sig = BTC_SIG_API_CALL;
 +    msg.pid = BTC_PID_PRF_QUE;
 +    msg.act = BTC_PRF_QUE_CONNECT;
 +
 +    memset(&arg, 0, sizeof(btc_prf_que_args_t));
 +    memcpy(&arg.connect_node.bda, bda, sizeof(bt_bdaddr_t));
 +    arg.connect_node.uuid = uuid;
 +    arg.connect_node.connect_cb = connect_cb;
 +
 +    return btc_transfer_context(&msg, &arg, sizeof(btc_prf_que_args_t), NULL);
 +}
 +/*******************************************************************************
 +**
 +** Function         btc_queue_advance
 +**
 +** Description      Clear the queue's busy status and advance to the next
 +**                  scheduled connection.
 +**
 +** Returns          void
 +**
 +*******************************************************************************/
 +void btc_queue_advance(void)
 +{
 +    btc_msg_t msg;
 +
 +    msg.sig = BTC_SIG_API_CALL;
 +    msg.pid = BTC_PID_PRF_QUE;
 +    msg.act = BTC_PRF_QUE_ADVANCE;
 +
 +    btc_transfer_context(&msg, NULL, 0, NULL);
 +}
 +
 +// This function dispatches the next pending connect request. It is called from
 +// stack_manager when the stack comes up.
 +bt_status_t btc_queue_connect_next(void)
 +{
 +    if (!connect_queue || list_is_empty(connect_queue)) {
 +        return BT_STATUS_FAIL;
 +    }
 +
 +    connect_node_t *p_head = list_front(connect_queue);
 +
 +    // If the queue is currently busy, we return success anyway,
 +    // since the connection has been queued...
 +    if (p_head->busy) {
 +        return BT_STATUS_SUCCESS;
 +    }
 +
 +    p_head->busy = true;
 +    return p_head->connect_cb(&p_head->bda, p_head->uuid);
 +}
 +
 +
 +/*******************************************************************************
 +**
 +** Function         btc_queue_release
 +**
 +** Description      Free up all the queue nodes and set the queue head to NULL
 +**
 +** Returns          void
 +**
 +*******************************************************************************/
 +void btc_queue_release(void)
 +{
 +    list_free(connect_queue);
 +    connect_queue = NULL;
 +}
index 57634fc04df5289aa428aa7896fdbaa15322f264,0000000000000000000000000000000000000000..f3ae7a8b531eef407b3c6ab3bd267042f0fa274f
mode 100644,000000..100644
--- /dev/null
@@@ -1,158 -1,0 +1,158 @@@
-     LOG_INFO("add to storage: Remote device:%s\n", bdstr);
 +// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
 +//
 +// Licensed under the Apache License, Version 2.0 (the "License");
 +// you may not use this file except in compliance with the License.
 +// You may obtain a copy of the License at
 +
 +//     http://www.apache.org/licenses/LICENSE-2.0
 +//
 +// Unless required by applicable law or agreed to in writing, software
 +// distributed under the License is distributed on an "AS IS" BASIS,
 +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 +// See the License for the specific language governing permissions and
 +// limitations under the License.
 +
 +#include "btc_storage.h"
 +#include "btc_util.h"
 +#include "osi.h"
 +#include "bt_trace.h"
 +#include "esp_system.h"
 +#include "bta_api.h"
 +#include "bdaddr.h"
 +#include "btc_config.h"
 +
 +/*******************************************************************************
 +**
 +** Function         btc_storage_add_bonded_device
 +**
 +** Description      BTIF storage API - Adds the newly bonded device to NVRAM
 +**                  along with the link-key, Key type and Pin key length
 +**
 +** Returns          BT_STATUS_SUCCESS if the store was successful,
 +**                  BT_STATUS_FAIL otherwise
 +**
 +*******************************************************************************/
 +
 +bt_status_t btc_storage_add_bonded_device(bt_bdaddr_t *remote_bd_addr,
 +        LINK_KEY link_key,
 +        uint8_t key_type,
 +        uint8_t pin_length)
 +{
 +    bdstr_t bdstr;
 +
 +    bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
-     LOG_INFO("Storage add rslt %d\n", ret);
++    LOG_DEBUG("add to storage: Remote device:%s\n", bdstr);
 +
 +    int ret = btc_config_set_int(bdstr, "LinkKeyType", (int)key_type);
 +    ret &= btc_config_set_int(bdstr, "PinLength", (int)pin_length);
 +    ret &= btc_config_set_bin(bdstr, "LinkKey", link_key, sizeof(LINK_KEY));
 +    /* write bonded info immediately */
 +    btc_config_flush();
-         LOG_INFO("Remote device:%s\n", name);
++    LOG_DEBUG("Storage add rslt %d\n", ret);
 +    return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         btc_in_fetch_bonded_devices
 +**
 +** Description      Internal helper function to fetch the bonded devices
 +**                  from NVRAM
 +**
 +** Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
 +**
 +*******************************************************************************/
 +static bt_status_t btc_in_fetch_bonded_devices(int add)
 +{
 +    BOOLEAN bt_linkkey_file_found = FALSE;
 +
 +    for (const btc_config_section_iter_t *iter = btc_config_section_begin(); iter != btc_config_section_end(); iter = btc_config_section_next(iter)) {
 +        const char *name = btc_config_section_name(iter);
 +        if (!string_is_bdaddr(name)) {
 +            continue;
 +        }
 +
-             LOG_INFO("Remote device:%s, no link key\n", name);
++        LOG_DEBUG("Remote device:%s\n", name);
 +        LINK_KEY link_key;
 +        size_t size = sizeof(link_key);
 +        if (btc_config_get_bin(name, "LinkKey", link_key, &size)) {
 +            int linkkey_type;
 +            if (btc_config_get_int(name, "LinkKeyType", &linkkey_type)) {
 +                //int pin_len;
 +                //btc_config_get_int(name, "PinLength", &pin_len))
 +                bt_bdaddr_t bd_addr;
 +                string_to_bdaddr(name, &bd_addr);
 +                if (add) {
 +                    DEV_CLASS dev_class = {0, 0, 0};
 +                    int cod;
 +                    int pin_length = 0;
 +                    if (btc_config_get_int(name, "DevClass", &cod)) {
 +                        uint2devclass((UINT32)cod, dev_class);
 +                    }
 +                    btc_config_get_int(name, "PinLength", &pin_length);
 +                    BTA_DmAddDevice(bd_addr.address, dev_class, link_key, 0, 0,
 +                                    (UINT8)linkkey_type, 0, pin_length);
 +                }
 +                bt_linkkey_file_found = TRUE;
 +            } else {
 +                LOG_ERROR("bounded device:%s, LinkKeyType or PinLength is invalid\n", name);
 +            }
 +        }
 +        if (!bt_linkkey_file_found) {
-     LOG_INFO("Storage load rslt %d\n", status);
++            LOG_DEBUG("Remote device:%s, no link key\n", name);
 +        }
 +    }
 +    return BT_STATUS_SUCCESS;
 +}
 +
 +
 +/*******************************************************************************
 +**
 +** Function         btc_storage_load_bonded_devices
 +**
 +** Description      BTC storage API - Loads all the bonded devices from NVRAM
 +**                  and adds to the BTA.
 +**                  Additionally, this API also invokes the adaper_properties_cb
 +**                  and remote_device_properties_cb for each of the bonded devices.
 +**
 +** Returns          BT_STATUS_SUCCESS if successful, BT_STATUS_FAIL otherwise
 +**
 +*******************************************************************************/
 +bt_status_t btc_storage_load_bonded_devices(void)
 +{
 +    bt_status_t status;
 +    status = btc_in_fetch_bonded_devices(1);
-     LOG_INFO("Add to storage: Remote device:%s\n", bdstr);
++    LOG_DEBUG("Storage load rslt %d\n", status);
 +    return status;
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         btc_storage_remove_bonded_device
 +**
 +** Description      BTC storage API - Deletes the bonded device from NVRAM
 +**
 +** Returns          BT_STATUS_SUCCESS if the deletion was successful,
 +**                  BT_STATUS_FAIL otherwise
 +**
 +*******************************************************************************/
 +bt_status_t btc_storage_remove_bonded_device(bt_bdaddr_t *remote_bd_addr)
 +{
 +    bdstr_t bdstr;
 +    bdaddr_to_string(remote_bd_addr, bdstr, sizeof(bdstr));
++    LOG_DEBUG("Add to storage: Remote device:%s\n", bdstr);
 +
 +    int ret = 1;
 +    if (btc_config_exist(bdstr, "LinkKeyType")) {
 +        ret &= btc_config_remove(bdstr, "LinkKeyType");
 +    }
 +    if (btc_config_exist(bdstr, "PinLength")) {
 +        ret &= btc_config_remove(bdstr, "PinLength");
 +    }
 +    if (btc_config_exist(bdstr, "LinkKey")) {
 +        ret &= btc_config_remove(bdstr, "LinkKey");
 +    }
 +    /* write bonded info immediately */
 +    btc_config_flush();
 +    return ret ? BT_STATUS_SUCCESS : BT_STATUS_FAIL;
 +}
index 90b1c95f270aac5bf25c9004cb601e89a6da1100,0000000000000000000000000000000000000000..4e037b3c8c80ad5912c764021d5379044b9a35c1
mode 100644,000000..100644
--- /dev/null
@@@ -1,1242 -1,0 +1,1242 @@@
-             APPL_TRACE_WARNING("reconfig done BTA_AVstart()\n");
 +// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
 +//
 +// Licensed under the Apache License, Version 2.0 (the "License");
 +// you may not use this file except in compliance with the License.
 +// You may obtain a copy of the License at
 +
 +//     http://www.apache.org/licenses/LICENSE-2.0
 +//
 +// Unless required by applicable law or agreed to in writing, software
 +// distributed under the License is distributed on an "AS IS" BASIS,
 +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 +// See the License for the specific language governing permissions and
 +// limitations under the License.
 +
 +/*****************************************************************************
 + *
 + *  Filename:      btc_avk.c
 + *
 + *  Description:   AV implementation
 + *
 + *****************************************************************************/
 +#include <string.h>
 +#include "bt_trace.h"
 +#include "bt_defs.h"
 +#include "esp_bt_defs.h"
 +#include "esp_a2dp_api.h"
 +#include "allocator.h"
 +#include "btc_dm.h"
 +#include "btc_av.h"
 +#include "btc_avrc.h"
 +#include "btc_util.h"
 +#include "btc_profile_queue.h"
 +#include "bta_api.h"
 +#include "btc_media.h"
 +#include "bta_av_api.h"
 +#include "gki.h"
 +#include "btu.h"
 +#include "bt_utils.h"
 +#include "btc_common.h"
 +#include "btc_manage.h"
 +
 +/*****************************************************************************
 +**  Constants & Macros
 +******************************************************************************/
 +#define BTC_AV_SERVICE_NAME "Advanced Audio"
 +
 +#define BTC_TIMEOUT_AV_OPEN_ON_RC_SECS  2
 +
 +typedef enum {
 +    BTC_AV_STATE_IDLE = 0x0,
 +    BTC_AV_STATE_OPENING,
 +    BTC_AV_STATE_OPENED,
 +    BTC_AV_STATE_STARTED,
 +    BTC_AV_STATE_CLOSING
 +} btc_av_state_t;
 +
 +/* Should not need dedicated suspend state as actual actions are no
 +   different than open state. Suspend flags are needed however to prevent
 +   media task from trying to restart stream during remote suspend or while
 +   we are in the process of a local suspend */
 +
 +#define BTC_AV_FLAG_LOCAL_SUSPEND_PENDING 0x1
 +#define BTC_AV_FLAG_REMOTE_SUSPEND        0x2
 +#define BTC_AV_FLAG_PENDING_START         0x4
 +#define BTC_AV_FLAG_PENDING_STOP          0x8
 +
 +/*****************************************************************************
 +**  Local type definitions
 +******************************************************************************/
 +
 +typedef struct {
 +    tBTA_AV_HNDL bta_handle;
 +    bt_bdaddr_t peer_bda;
 +    btc_sm_handle_t sm_handle;
 +    UINT8 flags;
 +    tBTA_AV_EDR edr;
 +    UINT8   peer_sep;  /* sep type of peer device */
 +} btc_av_cb_t;
 +
 +typedef struct {
 +    bt_bdaddr_t *target_bda;
 +    uint16_t uuid;
 +} btc_av_connect_req_t;
 +
 +/*****************************************************************************
 +**  Static variables
 +******************************************************************************/
 +static btc_av_cb_t btc_av_cb = {0};
 +static TIMER_LIST_ENT tle_av_open_on_rc;
 +
 +/* both interface and media task needs to be ready to alloc incoming request */
 +#define CHECK_BTAV_INIT() do \
 +{ \
 +    assert (btc_av_cb.sm_handle != NULL); \
 +} while (0)
 +
 +
 +/* Helper macro to avoid code duplication in the state machine handlers */
 +#define CHECK_RC_EVENT(e, d) \
 +    case BTA_AV_RC_OPEN_EVT: \
 +    case BTA_AV_RC_CLOSE_EVT: \
 +    case BTA_AV_REMOTE_CMD_EVT: \
 +    case BTA_AV_VENDOR_CMD_EVT: \
 +    case BTA_AV_META_MSG_EVT: \
 +    case BTA_AV_RC_FEAT_EVT: \
 +    case BTA_AV_REMOTE_RSP_EVT: \
 +    { \
 +         btc_rc_handler(e, d);\
 +    }break; \
 +
 +static BOOLEAN btc_av_state_idle_handler(btc_sm_event_t event, void *data);
 +static BOOLEAN btc_av_state_opening_handler(btc_sm_event_t event, void *data);
 +static BOOLEAN btc_av_state_opened_handler(btc_sm_event_t event, void *data);
 +static BOOLEAN btc_av_state_started_handler(btc_sm_event_t event, void *data);
 +static BOOLEAN btc_av_state_closing_handler(btc_sm_event_t event, void *data);
 +
 +static const btc_sm_handler_t btc_av_state_handlers[] = {
 +    btc_av_state_idle_handler,
 +    btc_av_state_opening_handler,
 +    btc_av_state_opened_handler,
 +    btc_av_state_started_handler,
 +    btc_av_state_closing_handler
 +};
 +
 +static void btc_av_event_free_data(btc_sm_event_t event, void *p_data);
 +
 +/*************************************************************************
 +** Extern functions
 +*************************************************************************/
 +
 +extern tBTA_AV_CO_FUNCTS bta_av_a2d_cos;
 +/*****************************************************************************
 +** Local helper functions
 +******************************************************************************/
 +static inline void btc_a2d_cb_to_app(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param)
 +{
 +    esp_a2d_cb_t btc_a2d_cb = (esp_a2d_cb_t)btc_profile_cb_get(BTC_PID_A2DP);
 +    if (btc_a2d_cb) {
 +      btc_a2d_cb(event, param);
 +    }
 +}
 +
 +static const char *dump_av_sm_state_name(btc_av_state_t state)
 +{
 +    switch (state) {
 +        CASE_RETURN_STR(BTC_AV_STATE_IDLE)
 +        CASE_RETURN_STR(BTC_AV_STATE_OPENING)
 +        CASE_RETURN_STR(BTC_AV_STATE_OPENED)
 +        CASE_RETURN_STR(BTC_AV_STATE_STARTED)
 +        CASE_RETURN_STR(BTC_AV_STATE_CLOSING)
 +    default: return "UNKNOWN_STATE";
 +    }
 +}
 +
 +static const char *dump_av_sm_event_name(btc_av_sm_event_t event)
 +{
 +    switch ((int)event) {
 +        CASE_RETURN_STR(BTA_AV_ENABLE_EVT)
 +        CASE_RETURN_STR(BTA_AV_REGISTER_EVT)
 +        CASE_RETURN_STR(BTA_AV_OPEN_EVT)
 +        CASE_RETURN_STR(BTA_AV_CLOSE_EVT)
 +        CASE_RETURN_STR(BTA_AV_START_EVT)
 +        CASE_RETURN_STR(BTA_AV_STOP_EVT)
 +        CASE_RETURN_STR(BTA_AV_PROTECT_REQ_EVT)
 +        CASE_RETURN_STR(BTA_AV_PROTECT_RSP_EVT)
 +        CASE_RETURN_STR(BTA_AV_RC_OPEN_EVT)
 +        CASE_RETURN_STR(BTA_AV_RC_CLOSE_EVT)
 +        CASE_RETURN_STR(BTA_AV_REMOTE_CMD_EVT)
 +        CASE_RETURN_STR(BTA_AV_REMOTE_RSP_EVT)
 +        CASE_RETURN_STR(BTA_AV_VENDOR_CMD_EVT)
 +        CASE_RETURN_STR(BTA_AV_VENDOR_RSP_EVT)
 +        CASE_RETURN_STR(BTA_AV_RECONFIG_EVT)
 +        CASE_RETURN_STR(BTA_AV_SUSPEND_EVT)
 +        CASE_RETURN_STR(BTA_AV_PENDING_EVT)
 +        CASE_RETURN_STR(BTA_AV_META_MSG_EVT)
 +        CASE_RETURN_STR(BTA_AV_REJECT_EVT)
 +        CASE_RETURN_STR(BTA_AV_RC_FEAT_EVT)
 +        CASE_RETURN_STR(BTC_SM_ENTER_EVT)
 +        CASE_RETURN_STR(BTC_SM_EXIT_EVT)
 +        CASE_RETURN_STR(BTC_AV_CONNECT_REQ_EVT)
 +        CASE_RETURN_STR(BTC_AV_DISCONNECT_REQ_EVT)
 +        CASE_RETURN_STR(BTC_AV_START_STREAM_REQ_EVT)
 +        CASE_RETURN_STR(BTC_AV_STOP_STREAM_REQ_EVT)
 +        CASE_RETURN_STR(BTC_AV_SUSPEND_STREAM_REQ_EVT)
 +        CASE_RETURN_STR(BTC_AV_SINK_CONFIG_REQ_EVT)
 +    default: return "UNKNOWN_EVENT";
 +    }
 +}
 +
 +/****************************************************************************
 +**  Local helper functions
 +*****************************************************************************/
 +/*******************************************************************************
 +**
 +** Function         btc_initiate_av_open_tmr_hdlr
 +**
 +** Description      Timer to trigger AV open if the remote headset establishes
 +**                  RC connection w/o AV connection. The timer is needed to IOP
 +**                  with headsets that do establish AV after RC connection.
 +**
 +** Returns          void
 +**
 +*******************************************************************************/
 +static void btc_initiate_av_open_tmr_hdlr(TIMER_LIST_ENT *tle)
 +{
 +    BD_ADDR peer_addr;
 +    UNUSED(tle);
 +    btc_av_connect_req_t connect_req;
 +    UNUSED(tle);
 +    /* is there at least one RC connection - There should be */
 +    if (btc_rc_get_connected_peer(peer_addr)) {
 +        LOG_DEBUG("%s Issuing connect to the remote RC peer", __FUNCTION__);
 +        /* In case of AVRCP connection request, we will initiate SRC connection */
 +        connect_req.target_bda = (bt_bdaddr_t *)&peer_addr;
 +        connect_req.uuid = UUID_SERVCLASS_AUDIO_SOURCE;
 +        btc_sm_dispatch(btc_av_cb.sm_handle, BTC_AV_CONNECT_REQ_EVT, (char *)&connect_req);
 +    } else {
 +        LOG_ERROR("%s No connected RC peers", __FUNCTION__);
 +    }
 +}
 +
 +/*****************************************************************************
 +**  Static functions
 +******************************************************************************/
 +static void btc_report_connection_state(esp_a2d_connection_state_t state, bt_bdaddr_t *bd_addr, int disc_rsn)
 +{
 +    esp_a2d_cb_param_t param;
 +    memset(&param, 0, sizeof(esp_a2d_cb_param_t));
 +    
 +    param.conn_stat.state = state;
 +    if (bd_addr) {
 +        memcpy(param.conn_stat.remote_bda, bd_addr, sizeof(esp_bd_addr_t));
 +    }
 +    if (state == ESP_A2D_CONNECTION_STATE_DISCONNECTED) {
 +        param.conn_stat.disc_rsn = (disc_rsn == 0) ? ESP_A2D_DISC_RSN_NORMAL :
 +            ESP_A2D_DISC_RSN_ABNORMAL;
 +    }
 +    btc_a2d_cb_to_app(ESP_A2D_CONNECTION_STATE_EVT, &param);
 +}
 +
 +static void btc_report_audio_state(esp_a2d_audio_state_t state, bt_bdaddr_t *bd_addr)
 +{
 +    esp_a2d_cb_param_t param;
 +    memset(&param, 0, sizeof(esp_a2d_cb_param_t));
 +
 +    param.audio_stat.state = state;
 +    if (bd_addr) {
 +        memcpy(param.audio_stat.remote_bda, bd_addr, sizeof(esp_bd_addr_t));
 +    }
 +    btc_a2d_cb_to_app(ESP_A2D_AUDIO_STATE_EVT, &param);
 +}
 +
 +/*****************************************************************************
 +**
 +** Function     btc_av_state_idle_handler
 +**
 +** Description  State managing disconnected AV link
 +**
 +** Returns      TRUE if event was processed, FALSE otherwise
 +**
 +*******************************************************************************/
 +
 +static BOOLEAN btc_av_state_idle_handler(btc_sm_event_t event, void *p_data)
 +{
 +    LOG_DEBUG("%s event:%s flags %x\n", __FUNCTION__,
 +                     dump_av_sm_event_name(event), btc_av_cb.flags);
 +
 +    switch (event) {
 +    case BTC_SM_ENTER_EVT:
 +        /* clear the peer_bda */
 +        memset(&btc_av_cb.peer_bda, 0, sizeof(bt_bdaddr_t));
 +        btc_av_cb.flags = 0;
 +        btc_av_cb.edr = 0;
 +        btc_a2dp_on_idle();
 +        break;
 +
 +    case BTC_SM_EXIT_EVT:
 +        break;
 +
 +    case BTA_AV_ENABLE_EVT:
 +        break;
 +
 +    case BTA_AV_REGISTER_EVT:
 +        btc_av_cb.bta_handle = ((tBTA_AV *)p_data)->registr.hndl;
 +        break;
 +
 +    case BTA_AV_PENDING_EVT:
 +    case BTC_AV_CONNECT_REQ_EVT: {
 +        if (event == BTC_AV_CONNECT_REQ_EVT) {
 +            memcpy(&btc_av_cb.peer_bda, ((btc_av_connect_req_t *)p_data)->target_bda,
 +                   sizeof(bt_bdaddr_t));
 +            BTA_AvOpen(btc_av_cb.peer_bda.address, btc_av_cb.bta_handle,
 +                       TRUE, BTA_SEC_AUTHENTICATE, ((btc_av_connect_req_t *)p_data)->uuid);
 +        } else if (event == BTA_AV_PENDING_EVT) {
 +            bdcpy(btc_av_cb.peer_bda.address, ((tBTA_AV *)p_data)->pend.bd_addr);
 +            BTA_AvOpen(btc_av_cb.peer_bda.address, btc_av_cb.bta_handle,
 +                       TRUE, BTA_SEC_AUTHENTICATE, UUID_SERVCLASS_AUDIO_SOURCE);
 +        }
 +        btc_sm_change_state(btc_av_cb.sm_handle, BTC_AV_STATE_OPENING);
 +    } break;
 +
 +    case BTA_AV_RC_OPEN_EVT:
 +        /* IOP_FIX: Jabra 620 only does RC open without AV open whenever it connects. So
 +         * as per the AV WP, an AVRC connection cannot exist without an AV connection. Therefore,
 +         * we initiate an AV connection if an RC_OPEN_EVT is received when we are in AV_CLOSED state.
 +         * We initiate the AV connection after a small 3s timeout to avoid any collisions from the
 +         * headsets, as some headsets initiate the AVRC connection first and then
 +         * immediately initiate the AV connection
 +         *
 +         * TODO: We may need to do this only on an AVRCP Play. FixMe
 +         */
 +
 +        LOG_DEBUG("BTA_AV_RC_OPEN_EVT received w/o AV");
 +        memset(&tle_av_open_on_rc, 0, sizeof(tle_av_open_on_rc));
 +        tle_av_open_on_rc.param = (UINT32)btc_initiate_av_open_tmr_hdlr;
 +        btu_start_timer(&tle_av_open_on_rc, BTU_TTYPE_USER_FUNC,
 +                        BTC_TIMEOUT_AV_OPEN_ON_RC_SECS);
 +        btc_rc_handler(event, p_data);
 +        break;
 +
 +    case BTA_AV_REMOTE_CMD_EVT:
 +    case BTA_AV_VENDOR_CMD_EVT:
 +    case BTA_AV_META_MSG_EVT:
 +    case BTA_AV_RC_FEAT_EVT:
 +    case BTA_AV_REMOTE_RSP_EVT:
 +        btc_rc_handler(event, (tBTA_AV *)p_data);
 +        break;
 +
 +    case BTA_AV_RC_CLOSE_EVT:
 +        if (tle_av_open_on_rc.in_use) {
 +            LOG_DEBUG("BTA_AV_RC_CLOSE_EVT: Stopping AV timer.");
 +            btu_stop_timer(&tle_av_open_on_rc);
 +        }
 +        btc_rc_handler(event, p_data);
 +        break;
 +
 +    default:
 +        LOG_WARN("%s : unhandled event:%s\n", __FUNCTION__,
 +                           dump_av_sm_event_name(event));
 +        return FALSE;
 +
 +    }
 +
 +    return TRUE;
 +}
 +/*****************************************************************************
 +**
 +** Function        btc_av_state_opening_handler
 +**
 +** Description     Intermediate state managing events during establishment
 +**                 of avdtp channel
 +**
 +** Returns         TRUE if event was processed, FALSE otherwise
 +**
 +*******************************************************************************/
 +
 +static BOOLEAN btc_av_state_opening_handler(btc_sm_event_t event, void *p_data)
 +{
 +    LOG_DEBUG("%s event:%s flags %x\n", __FUNCTION__,
 +                     dump_av_sm_event_name(event), btc_av_cb.flags);
 +
 +    switch (event) {
 +    case BTC_SM_ENTER_EVT:
 +        /* inform the application that we are entering connecting state */
 +        btc_report_connection_state(ESP_A2D_CONNECTION_STATE_CONNECTING, &(btc_av_cb.peer_bda), 0);
 +        break;
 +
 +    case BTC_SM_EXIT_EVT:
 +        break;
 +
 +    case BTA_AV_REJECT_EVT:
 +        LOG_DEBUG(" Received  BTA_AV_REJECT_EVT \n");
 +        btc_report_connection_state(ESP_A2D_CONNECTION_STATE_DISCONNECTED, &(btc_av_cb.peer_bda), 0);
 +        btc_sm_change_state(btc_av_cb.sm_handle, BTC_AV_STATE_IDLE);
 +        break;
 +
 +    case BTA_AV_OPEN_EVT: {
 +        tBTA_AV *p_bta_data = (tBTA_AV *)p_data;
 +        esp_a2d_connection_state_t state;
 +        btc_sm_state_t av_state;
 +        LOG_DEBUG("status:%d, edr 0x%x\n", p_bta_data->open.status,
 +                         p_bta_data->open.edr);
 +
 +        if (p_bta_data->open.status == BTA_AV_SUCCESS) {
 +            state = ESP_A2D_CONNECTION_STATE_CONNECTED;
 +            av_state = BTC_AV_STATE_OPENED;
 +            btc_av_cb.edr = p_bta_data->open.edr;
 +
 +            btc_av_cb.peer_sep = p_bta_data->open.sep;
 +            btc_a2dp_set_peer_sep(p_bta_data->open.sep);
 +        } else {
 +            LOG_WARN("BTA_AV_OPEN_EVT::FAILED status: %d\n",
 +                               p_bta_data->open.status );
 +            state = ESP_A2D_CONNECTION_STATE_DISCONNECTED;
 +            av_state  = BTC_AV_STATE_IDLE;
 +        }
 +
 +        /* inform the application of the event */
 +        btc_report_connection_state(state, &(btc_av_cb.peer_bda), 0);
 +        /* change state to open/idle based on the status */
 +        btc_sm_change_state(btc_av_cb.sm_handle, av_state);
 +        if (btc_av_cb.peer_sep == AVDT_TSEP_SRC) {
 +            /* Bring up AVRCP connection too */
 +            BTA_AvOpenRc(btc_av_cb.bta_handle);
 +        }
 +        btc_queue_advance();
 +    } break;
 +
 +    case BTC_AV_SINK_CONFIG_REQ_EVT: {
 +        if (btc_av_cb.peer_sep == AVDT_TSEP_SRC) {
 +            esp_a2d_cb_param_t param;
 +            memcpy(param.audio_cfg.remote_bda, &btc_av_cb.peer_bda, sizeof(esp_bd_addr_t));
 +            memcpy(&param.audio_cfg.mcc, p_data, sizeof(esp_a2d_mcc_t));
 +            btc_a2d_cb_to_app(ESP_A2D_AUDIO_CFG_EVT, &param);
 +        }
 +    } break;
 +
 +    case BTC_AV_CONNECT_REQ_EVT:
 +        // Check for device, if same device which moved to opening then ignore callback
 +        if (memcmp ((bt_bdaddr_t *)p_data, &(btc_av_cb.peer_bda),
 +                    sizeof(btc_av_cb.peer_bda)) == 0) {
 +            LOG_DEBUG("%s: Same device moved to Opening state,ignore Connect Req\n", __func__);
 +            btc_queue_advance();
 +            break;
 +        } else {
 +            LOG_DEBUG("%s: Moved from idle by Incoming Connection request\n", __func__);
 +            btc_report_connection_state(ESP_A2D_CONNECTION_STATE_DISCONNECTED, (bt_bdaddr_t *)p_data, 0);
 +            btc_queue_advance();
 +            break;
 +        }
 +
 +    case BTA_AV_PENDING_EVT:
 +        // Check for device, if same device which moved to opening then ignore callback
 +        if (memcmp (((tBTA_AV *)p_data)->pend.bd_addr, &(btc_av_cb.peer_bda),
 +                    sizeof(btc_av_cb.peer_bda)) == 0) {
 +            LOG_DEBUG("%s: Same device moved to Opening state,ignore Pending Req\n", __func__);
 +            break;
 +        } else {
 +            LOG_DEBUG("%s: Moved from idle by outgoing Connection request\n", __func__);
 +            BTA_AvDisconnect(((tBTA_AV *)p_data)->pend.bd_addr);
 +            break;
 +        }
 +
 +        CHECK_RC_EVENT(event, p_data);
 +
 +    default:
 +        LOG_WARN("%s : unhandled event:%s\n", __FUNCTION__,
 +                           dump_av_sm_event_name(event));
 +        return FALSE;
 +
 +    }
 +    return TRUE;
 +}
 +
 +
 +/*****************************************************************************
 +**
 +** Function        btc_av_state_closing_handler
 +**
 +** Description     Intermediate state managing events during closing
 +**                 of avdtp channel
 +**
 +** Returns         TRUE if event was processed, FALSE otherwise
 +**
 +*******************************************************************************/
 +
 +static BOOLEAN btc_av_state_closing_handler(btc_sm_event_t event, void *p_data)
 +{
 +    LOG_DEBUG("%s event:%s flags %x\n", __FUNCTION__,
 +                     dump_av_sm_event_name(event), btc_av_cb.flags);
 +
 +    switch (event) {
 +    case BTC_SM_ENTER_EVT:
 +        if (btc_av_cb.peer_sep == AVDT_TSEP_SRC) {
 +            btc_a2dp_set_rx_flush(TRUE);
 +        }
 +        break;
 +
 +    case BTA_AV_STOP_EVT:
 +    case BTC_AV_STOP_STREAM_REQ_EVT:
 +        if (btc_av_cb.peer_sep == AVDT_TSEP_SRC) {
 +            btc_a2dp_set_rx_flush(TRUE);
 +        }
 +
 +        btc_a2dp_on_stopped(NULL);
 +        break;
 +
 +    case BTC_SM_EXIT_EVT:
 +        break;
 +
 +    case BTA_AV_CLOSE_EVT: {
 +        tBTA_AV_CLOSE *close = (tBTA_AV_CLOSE *)p_data;
 +        /* inform the application that we are disconnecting */
 +        btc_report_connection_state(ESP_A2D_CONNECTION_STATE_DISCONNECTED, &(btc_av_cb.peer_bda),
 +                                     close->disc_rsn);
 +
 +        btc_sm_change_state(btc_av_cb.sm_handle, BTC_AV_STATE_IDLE);
 +        break;
 +    }
 +
 +    /* Handle the RC_CLOSE event for the cleanup */
 +    case BTA_AV_RC_CLOSE_EVT:
 +        btc_rc_handler(event, (tBTA_AV *)p_data);
 +        break;
 +
 +    default:
 +        LOG_WARN("%s : unhandled event:%s\n", __FUNCTION__,
 +                           dump_av_sm_event_name(event));
 +        return FALSE;
 +    }
 +    return TRUE;
 +}
 +
 +
 +/*****************************************************************************
 +**
 +** Function     btc_av_state_opened_handler
 +**
 +** Description  Handles AV events while AVDTP is in OPEN state
 +**
 +** Returns      TRUE if event was processed, FALSE otherwise
 +**
 +*******************************************************************************/
 +
 +static BOOLEAN btc_av_state_opened_handler(btc_sm_event_t event, void *p_data)
 +{
 +    tBTA_AV *p_av = (tBTA_AV *)p_data;
 +
 +    LOG_DEBUG("%s event:%s flags %x\n", __FUNCTION__,
 +                     dump_av_sm_event_name(event), btc_av_cb.flags);
 +
 +    if ( (event == BTA_AV_REMOTE_CMD_EVT) && (btc_av_cb.flags & BTC_AV_FLAG_REMOTE_SUSPEND) &&
 +            (p_av->remote_cmd.rc_id == BTA_AV_RC_PLAY) ) {
 +        LOG_INFO("%s: Resetting remote suspend flag on RC PLAY\n", __FUNCTION__);
 +        btc_av_cb.flags &= ~BTC_AV_FLAG_REMOTE_SUSPEND;
 +    }
 +
 +    switch (event) {
 +    case BTC_SM_ENTER_EVT:
 +        btc_av_cb.flags &= ~BTC_AV_FLAG_PENDING_STOP;
 +        btc_av_cb.flags &= ~BTC_AV_FLAG_PENDING_START;
 +        break;
 +
 +    case BTC_SM_EXIT_EVT:
 +        btc_av_cb.flags &= ~BTC_AV_FLAG_PENDING_START;
 +        break;
 +
 +    case BTC_AV_START_STREAM_REQ_EVT:
 +        BTA_AvStart();
 +        btc_av_cb.flags |= BTC_AV_FLAG_PENDING_START;
 +        break;
 +
 +    case BTA_AV_START_EVT: {
 +        LOG_INFO("BTA_AV_START_EVT status %d, suspending %d, init %d\n",
 +                         p_av->start.status, p_av->start.suspending, p_av->start.initiator);
 +
 +        if ((p_av->start.status == BTA_SUCCESS) && (p_av->start.suspending == TRUE)) {
 +            return TRUE;
 +        }
 +
 +        /* remain in open state if status failed */
 +        if (p_av->start.status != BTA_AV_SUCCESS) {
 +            return FALSE;
 +        }
 +
 +        if (btc_av_cb.peer_sep == AVDT_TSEP_SRC) {
 +            btc_a2dp_set_rx_flush(FALSE); /*  remove flush state, ready for streaming*/
 +        }
 +
 +        /* change state to started, send acknowledgement if start is pending */
 +        if (btc_av_cb.flags & BTC_AV_FLAG_PENDING_START) {
 +
 +            /* pending start flag will be cleared when exit current state */
 +        }
 +        btc_sm_change_state(btc_av_cb.sm_handle, BTC_AV_STATE_STARTED);
 +
 +    } break;
 +
 +    case BTC_AV_DISCONNECT_REQ_EVT:
 +        BTA_AvClose(btc_av_cb.bta_handle);
 +        if (btc_av_cb.peer_sep == AVDT_TSEP_SRC) {
 +            BTA_AvCloseRc(btc_av_cb.bta_handle);
 +        }
 +
 +        /* inform the application that we are disconnecting */
 +        btc_report_connection_state(ESP_A2D_CONNECTION_STATE_DISCONNECTING, &(btc_av_cb.peer_bda), 0);
 +        break;
 +
 +    case BTA_AV_CLOSE_EVT: {
 +        /* avdtp link is closed */
 +        btc_a2dp_on_stopped(NULL);
 +
 +        tBTA_AV_CLOSE *close = (tBTA_AV_CLOSE *)p_data;
 +        /* inform the application that we are disconnected */
 +        btc_report_connection_state(ESP_A2D_CONNECTION_STATE_DISCONNECTED, &(btc_av_cb.peer_bda),
 +                                     close->disc_rsn);
 +
 +        /* change state to idle, send acknowledgement if start is pending */
 +        if (btc_av_cb.flags & BTC_AV_FLAG_PENDING_START) {
 +            /* pending start flag will be cleared when exit current state */
 +        }
 +        btc_sm_change_state(btc_av_cb.sm_handle, BTC_AV_STATE_IDLE);
 +        break;
 +    }
 +
 +    case BTA_AV_RECONFIG_EVT:
 +        if ((btc_av_cb.flags & BTC_AV_FLAG_PENDING_START) &&
 +                (p_av->reconfig.status == BTA_AV_SUCCESS)) {
-             APPL_TRACE_ERROR("ERROR dump_codec_info A2D_ParsSbcInfo fail:%d\n", a2d_status);
++            LOG_WARN("reconfig done BTA_AVstart()\n");
 +            BTA_AvStart();
 +        } else if (btc_av_cb.flags & BTC_AV_FLAG_PENDING_START) {
 +            btc_av_cb.flags &= ~BTC_AV_FLAG_PENDING_START;
 +        }
 +        break;
 +
 +    case BTC_AV_CONNECT_REQ_EVT:
 +        if (memcmp ((bt_bdaddr_t *)p_data, &(btc_av_cb.peer_bda),
 +                    sizeof(btc_av_cb.peer_bda)) == 0) {
 +            LOG_DEBUG("%s: Ignore BTC_AVCONNECT_REQ_EVT for same device\n", __func__);
 +        } else {
 +            LOG_DEBUG("%s: Moved to opened by Other Incoming Conn req\n", __func__);
 +            btc_report_connection_state(ESP_A2D_CONNECTION_STATE_DISCONNECTED,
 +                                         (bt_bdaddr_t *)p_data, ESP_A2D_DISC_RSN_NORMAL);
 +        }
 +        btc_queue_advance();
 +        break;
 +
 +        CHECK_RC_EVENT(event, p_data);
 +
 +    default:
 +        LOG_WARN("%s : unhandled event:%s\n", __FUNCTION__,
 +                           dump_av_sm_event_name(event));
 +        return FALSE;
 +
 +    }
 +    return TRUE;
 +}
 +
 +/*****************************************************************************
 +**
 +** Function     btc_av_state_started_handler
 +**
 +** Description  Handles AV events while A2DP stream is started
 +**
 +** Returns      TRUE if event was processed, FALSE otherwise
 +**
 +*******************************************************************************/
 +
 +static BOOLEAN btc_av_state_started_handler(btc_sm_event_t event, void *p_data)
 +{
 +    tBTA_AV *p_av = (tBTA_AV *)p_data;
 +
 +    LOG_DEBUG("%s event:%s flags %x\n", __FUNCTION__,
 +                     dump_av_sm_event_name(event), btc_av_cb.flags);
 +
 +    switch (event) {
 +    case BTC_SM_ENTER_EVT:
 +
 +        /* we are again in started state, clear any remote suspend flags */
 +        btc_av_cb.flags &= ~BTC_AV_FLAG_REMOTE_SUSPEND;
 +
 +        btc_report_audio_state(ESP_A2D_AUDIO_STATE_STARTED, &(btc_av_cb.peer_bda));
 +
 +        /* increase the a2dp consumer task priority temporarily when start
 +        ** audio playing, to avoid overflow the audio packet queue. */
 +        adjust_priority_a2dp(TRUE);
 +
 +        break;
 +
 +    case BTC_SM_EXIT_EVT:
 +        /* restore the a2dp consumer task priority when stop audio playing. */
 +        adjust_priority_a2dp(FALSE);
 +
 +        break;
 +
 +    case BTC_AV_START_STREAM_REQ_EVT:
 +        break;
 +
 +    /* fixme -- use suspend = true always to work around issue with BTA AV */
 +    case BTC_AV_STOP_STREAM_REQ_EVT:
 +    case BTC_AV_SUSPEND_STREAM_REQ_EVT:
 +
 +        /* set pending flag to ensure btc task is not trying to restart
 +           stream while suspend is in progress */
 +        btc_av_cb.flags |= BTC_AV_FLAG_LOCAL_SUSPEND_PENDING;
 +
 +        /* if we were remotely suspended but suspend locally, local suspend
 +           always overrides */
 +        btc_av_cb.flags &= ~BTC_AV_FLAG_REMOTE_SUSPEND;
 +
 +        if (btc_av_cb.peer_sep == AVDT_TSEP_SRC) {
 +            btc_a2dp_set_rx_flush(TRUE);
 +            btc_a2dp_on_stopped(NULL);
 +        }
 +
 +        BTA_AvStop(TRUE);
 +        break;
 +
 +    case BTC_AV_DISCONNECT_REQ_EVT:
 +
 +        /* request avdtp to close */
 +        BTA_AvClose(btc_av_cb.bta_handle);
 +        if (btc_av_cb.peer_sep == AVDT_TSEP_SRC) {
 +            BTA_AvCloseRc(btc_av_cb.bta_handle);
 +        }
 +
 +        /* inform the application that we are disconnecting */
 +        btc_report_connection_state(ESP_A2D_CONNECTION_STATE_DISCONNECTING, &(btc_av_cb.peer_bda), 0);
 +
 +        /* wait in closing state until fully closed */
 +        btc_sm_change_state(btc_av_cb.sm_handle, BTC_AV_STATE_CLOSING);
 +        break;
 +
 +    case BTA_AV_SUSPEND_EVT:
 +
 +        LOG_INFO("BTA_AV_SUSPEND_EVT status %d, init %d\n",
 +                         p_av->suspend.status, p_av->suspend.initiator);
 +
 +        /* a2dp suspended, stop media task until resumed */
 +        btc_a2dp_on_suspended(&p_av->suspend);
 +
 +        /* if not successful, remain in current state */
 +        if (p_av->suspend.status != BTA_AV_SUCCESS) {
 +            btc_av_cb.flags &= ~BTC_AV_FLAG_LOCAL_SUSPEND_PENDING;
 +
 +            return FALSE;
 +        }
 +
 +        if (p_av->suspend.initiator != TRUE) {
 +            /* remote suspend, notify HAL and await audioflinger to
 +               suspend/stop stream */
 +
 +            /* set remote suspend flag to block media task from restarting
 +               stream only if we did not already initiate a local suspend */
 +            if ((btc_av_cb.flags & BTC_AV_FLAG_LOCAL_SUSPEND_PENDING) == 0) {
 +                btc_av_cb.flags |= BTC_AV_FLAG_REMOTE_SUSPEND;
 +            }
 +
 +            btc_report_audio_state(ESP_A2D_AUDIO_STATE_REMOTE_SUSPEND, &(btc_av_cb.peer_bda));
 +        } else {
 +            btc_report_audio_state(ESP_A2D_AUDIO_STATE_STOPPED, &(btc_av_cb.peer_bda));
 +        }
 +
 +        btc_sm_change_state(btc_av_cb.sm_handle, BTC_AV_STATE_OPENED);
 +
 +        /* suspend completed and state changed, clear pending status */
 +        btc_av_cb.flags &= ~BTC_AV_FLAG_LOCAL_SUSPEND_PENDING;
 +        break;
 +
 +    case BTA_AV_STOP_EVT:
 +
 +        btc_av_cb.flags |= BTC_AV_FLAG_PENDING_STOP;
 +        btc_a2dp_on_stopped(&p_av->suspend);
 +
 +        btc_report_audio_state(ESP_A2D_AUDIO_STATE_STOPPED, &(btc_av_cb.peer_bda));
 +
 +        /* if stop was successful, change state to open */
 +        if (p_av->suspend.status == BTA_AV_SUCCESS) {
 +            btc_sm_change_state(btc_av_cb.sm_handle, BTC_AV_STATE_OPENED);
 +        }
 +
 +        break;
 +
 +    case BTA_AV_CLOSE_EVT: {
 +
 +        btc_av_cb.flags |= BTC_AV_FLAG_PENDING_STOP;
 +
 +        /* avdtp link is closed */
 +        btc_a2dp_on_stopped(NULL);
 +        
 +        tBTA_AV_CLOSE *close = (tBTA_AV_CLOSE *)p_data;
 +        /* inform the application that we are disconnected */
 +        btc_report_connection_state(ESP_A2D_CONNECTION_STATE_DISCONNECTED, &(btc_av_cb.peer_bda),
 +                                     close->disc_rsn);
 +
 +        btc_sm_change_state(btc_av_cb.sm_handle, BTC_AV_STATE_IDLE);
 +        break;
 +    }
 +    
 +    CHECK_RC_EVENT(event, p_data);
 +
 +    default:
 +        LOG_WARN("%s : unhandled event:%s\n", __FUNCTION__,
 +                           dump_av_sm_event_name(event));
 +        return FALSE;
 +
 +    }
 +    return TRUE;
 +}
 +
 +/*****************************************************************************
 +**  Local event handlers
 +******************************************************************************/
 +
 +void btc_av_event_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src)
 +{
 +    tBTA_AV *av_src = (tBTA_AV *)p_src;
 +    tBTA_AV *av_dest = (tBTA_AV *)p_dest;
 +
 +    // First copy the structure
 +    memcpy(p_dest, p_src, sizeof(tBTA_AV));
 +
 +    switch (msg->act) {
 +    case BTA_AV_META_MSG_EVT:
 +        if (av_src->meta_msg.p_data && av_src->meta_msg.len) {
 +            av_dest->meta_msg.p_data = osi_calloc(av_src->meta_msg.len);
 +            assert(av_dest->meta_msg.p_data);
 +            memcpy(av_dest->meta_msg.p_data, av_src->meta_msg.p_data, av_src->meta_msg.len);
 +        }
 +
 +        if (av_src->meta_msg.p_msg) {
 +            av_dest->meta_msg.p_msg = osi_calloc(sizeof(tAVRC_MSG));
 +            assert(av_dest->meta_msg.p_msg);
 +            memcpy(av_dest->meta_msg.p_msg, av_src->meta_msg.p_msg, sizeof(tAVRC_MSG));
 +
 +            if (av_src->meta_msg.p_msg->vendor.p_vendor_data &&
 +                    av_src->meta_msg.p_msg->vendor.vendor_len) {
 +                av_dest->meta_msg.p_msg->vendor.p_vendor_data = osi_calloc(
 +                            av_src->meta_msg.p_msg->vendor.vendor_len);
 +                assert(av_dest->meta_msg.p_msg->vendor.p_vendor_data);
 +                memcpy(av_dest->meta_msg.p_msg->vendor.p_vendor_data,
 +                       av_src->meta_msg.p_msg->vendor.p_vendor_data,
 +                       av_src->meta_msg.p_msg->vendor.vendor_len);
 +            }
 +        }
 +        break;
 +
 +    default:
 +        break;
 +    }
 +}
 +
 +static void btc_av_event_free_data(btc_sm_event_t event, void *p_data)
 +{
 +    switch (event) {
 +    case BTA_AV_META_MSG_EVT: {
 +        tBTA_AV *av = (tBTA_AV *)p_data;
 +        if (av->meta_msg.p_data) {
 +            osi_free(av->meta_msg.p_data);
 +        }
 +
 +        if (av->meta_msg.p_msg) {
 +            if (av->meta_msg.p_msg->vendor.p_vendor_data) {
 +                osi_free(av->meta_msg.p_msg->vendor.p_vendor_data);
 +            }
 +            osi_free(av->meta_msg.p_msg);
 +        }
 +    }
 +    break;
 +
 +    default:
 +        break;
 +    }
 +}
 +
 +static void bte_av_callback(tBTA_AV_EVT event, tBTA_AV *p_data)
 +{
 +    bt_status_t stat;
 +    btc_msg_t msg;
 +
 +    msg.sig = BTC_SIG_API_CB;
 +    msg.pid = BTC_PID_A2DP;
 +    msg.act = (uint8_t) event;
 +    stat = btc_transfer_context(&msg, p_data, sizeof(tBTA_AV), btc_av_event_deep_copy);
 +
 +    if (stat) {
 +        LOG_ERROR("%s transfer failed\n", __func__);
 +    }
 +}
 +
 +static void bte_av_media_callback(tBTA_AV_EVT event, tBTA_AV_MEDIA *p_data)
 +{
 +    btc_sm_state_t state;
 +    UINT8 que_len;
 +    tA2D_STATUS a2d_status;
 +    tA2D_SBC_CIE sbc_cie;
 +
 +    if (event == BTA_AV_MEDIA_DATA_EVT) { /* Switch to BTC_MEDIA context */
 +        state = btc_sm_get_state(btc_av_cb.sm_handle);
 +        if ( (state == BTC_AV_STATE_STARTED) || /* send SBC packets only in Started State */
 +                (state == BTC_AV_STATE_OPENED) ) {
 +            que_len = btc_media_sink_enque_buf((BT_HDR *)p_data);
 +            LOG_DEBUG(" Packets in Que %d\n", que_len);
 +        } else {
 +            return;
 +        }
 +    }
 +
 +    if (event == BTA_AV_MEDIA_SINK_CFG_EVT) {
 +        /* send a command to BT Media Task */
 +        btc_reset_decoder((UINT8 *)p_data);
 +
 +      /* currently only supportes SBC */
 +        a2d_status = A2D_ParsSbcInfo(&sbc_cie, (UINT8 *)p_data, FALSE);
 +        if (a2d_status == A2D_SUCCESS) {
 +            btc_msg_t msg;
 +            btc_av_args_t arg;
 +            
 +            msg.sig = BTC_SIG_API_CB;
 +            msg.pid = BTC_PID_A2DP;
 +            msg.act = BTC_AV_SINK_CONFIG_REQ_EVT;
 +
 +            memset(&arg, 0, sizeof(btc_av_args_t));
 +            arg.mcc.type = ESP_A2D_MCT_SBC;
 +            memcpy(&(arg.mcc.cie), (uint8_t *)p_data + 3, ESP_A2D_CIE_LEN_SBC);
 +            btc_transfer_context(&msg, &arg, sizeof(btc_av_args_t), NULL);
 +        } else {
-     LOG_INFO("%s\n", __FUNCTION__);
++            LOG_ERROR("ERROR dump_codec_info A2D_ParsSbcInfo fail:%d\n", a2d_status);
 +        }
 +    }
 +}
 +/*******************************************************************************
 +**
 +** Function         btc_av_init
 +**
 +** Description      Initializes btc AV if not already done
 +**
 +** Returns          bt_status_t
 +**
 +*******************************************************************************/
 +
 +bt_status_t btc_av_init(void)
 +{
 +    if (btc_av_cb.sm_handle == NULL) {
 +        if (!btc_a2dp_start_media_task()) {
 +            return BT_STATUS_FAIL;
 +        }
 +
 +        /* Also initialize the AV state machine */
 +        btc_av_cb.sm_handle =
 +            btc_sm_init((const btc_sm_handler_t *)btc_av_state_handlers, BTC_AV_STATE_IDLE);
 +
 +        btc_dm_enable_service(BTA_A2DP_SOURCE_SERVICE_ID);
 +#if (BTA_AV_SINK_INCLUDED == TRUE)
 +        btc_dm_enable_service(BTA_A2DP_SINK_SERVICE_ID);
 +#endif
 +
 +        btc_a2dp_on_init();
 +    }
 +
 +    return BT_STATUS_SUCCESS;
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         init_sink
 +**
 +** Description      Initializes the AV interface for sink mode
 +**
 +** Returns          bt_status_t
 +**
 +*******************************************************************************/
 +bt_status_t btc_a2d_sink_init(void)
 +{
 +    LOG_DEBUG("%s()\n", __func__);
 +
 +    return btc_av_init();
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         connect
 +**
 +** Description      Establishes the AV signalling channel with the remote headset
 +**
 +** Returns          bt_status_t
 +**
 +*******************************************************************************/
 +
 +static bt_status_t connect_int(bt_bdaddr_t *bd_addr, uint16_t uuid)
 +{
 +    btc_av_connect_req_t connect_req;
 +    connect_req.target_bda = bd_addr;
 +    connect_req.uuid = uuid;
-     LOG_INFO("%s\n", __FUNCTION__);
++    LOG_DEBUG("%s\n", __FUNCTION__);
 +
 +    btc_sm_dispatch(btc_av_cb.sm_handle, BTC_AV_CONNECT_REQ_EVT, (char *)&connect_req);
 +
 +    return BT_STATUS_SUCCESS;
 +}
 +
 +bt_status_t btc_a2d_sink_connect(bt_bdaddr_t* remote_bda)
 +{
 +    LOG_DEBUG("%s\n", __FUNCTION__);
 +    CHECK_BTAV_INIT();
 +
 +    return btc_queue_connect(UUID_SERVCLASS_AUDIO_SINK, remote_bda, connect_int);
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         cleanup
 +**
 +** Description      Shuts down the AV interface and does the cleanup
 +**
 +** Returns          None
 +**
 +*******************************************************************************/
 +static void btc_a2d_sink_deinit(void)
 +{
++    LOG_DEBUG("%s\n", __FUNCTION__);
 +
 +    btc_a2dp_stop_media_task();
 +
 +    btc_dm_disable_service(BTA_A2DP_SOURCE_SERVICE_ID);
 +#if (BTA_AV_SINK_INCLUDED == TRUE)
 +    btc_dm_disable_service(BTA_A2DP_SINK_SERVICE_ID);
 +#endif
 +
 +    /* Also shut down the AV state machine */
 +    btc_sm_shutdown(btc_av_cb.sm_handle);
 +    btc_av_cb.sm_handle = NULL;
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         btc_av_get_sm_handle
 +**
 +** Description      Fetches current av SM handle
 +**
 +** Returns          None
 +**
 +*******************************************************************************/
 +
 +btc_sm_handle_t btc_av_get_sm_handle(void)
 +{
 +    return btc_av_cb.sm_handle;
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         btc_av_stream_ready
 +**
 +** Description      Checks whether AV is ready for starting a stream
 +**
 +** Returns          None
 +**
 +*******************************************************************************/
 +
 +BOOLEAN btc_av_stream_ready(void)
 +{
 +    btc_sm_state_t state = btc_sm_get_state(btc_av_cb.sm_handle);
 +
 +    LOG_DEBUG("btc_av_stream_ready : sm hdl %d, state %d, flags %x\n",
 +                     (int)btc_av_cb.sm_handle, state, btc_av_cb.flags);
 +
 +    /* check if we are remotely suspended or stop is pending */
 +    if (btc_av_cb.flags & (BTC_AV_FLAG_REMOTE_SUSPEND | BTC_AV_FLAG_PENDING_STOP)) {
 +        return FALSE;
 +    }
 +
 +    return (state == BTC_AV_STATE_OPENED);
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         btc_av_stream_started_ready
 +**
 +** Description      Checks whether AV ready for media start in streaming state
 +**
 +** Returns          None
 +**
 +*******************************************************************************/
 +
 +BOOLEAN btc_av_stream_started_ready(void)
 +{
 +    btc_sm_state_t state = btc_sm_get_state(btc_av_cb.sm_handle);
 +
 +    LOG_DEBUG("btc_av_stream_started : sm hdl %d, state %d, flags %x\n",
 +                     (int)btc_av_cb.sm_handle, state, btc_av_cb.flags);
 +
 +    /* disallow media task to start if we have pending actions */
 +    if (btc_av_cb.flags & (BTC_AV_FLAG_LOCAL_SUSPEND_PENDING | BTC_AV_FLAG_REMOTE_SUSPEND
 +                            | BTC_AV_FLAG_PENDING_STOP)) {
 +        return FALSE;
 +    }
 +
 +    return (state == BTC_AV_STATE_STARTED);
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         btc_dispatch_sm_event
 +**
 +** Description      Send event to AV statemachine
 +**
 +** Returns          None
 +**
 +*******************************************************************************/
 +
 +/* used to pass events to AV statemachine from other tasks */
 +void btc_dispatch_sm_event(btc_av_sm_event_t event, void *p_data, int len)
 +{
 +    btc_msg_t msg;
 +    msg.sig = BTC_SIG_API_CALL;
 +    msg.pid = BTC_PID_A2DP;
 +    msg.act = event;
 +    btc_transfer_context(&msg, p_data, len, NULL);
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         btc_av_execute_service
 +**
 +** Description      Initializes/Shuts down the service
 +**
 +** Returns          BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise
 +**
 +*******************************************************************************/
 +bt_status_t btc_av_execute_service(BOOLEAN b_enable)
 +{
 +    if (b_enable) {
 +        /* TODO: Removed BTA_SEC_AUTHORIZE since the Java/App does not
 +         * handle this request in order to allow incoming connections to succeed.
 +         * We need to put this back once support for this is added */
 +
 +        /* Added BTA_AV_FEAT_NO_SCO_SSPD - this ensures that the BTA does not
 +         * auto-suspend av streaming on AG events(SCO or Call). The suspend shall
 +         * be initiated by the app/audioflinger layers */
 +        BTA_AvEnable(BTA_SEC_AUTHENTICATE, (BTA_AV_FEAT_NO_SCO_SSPD)
 +                   // | BTA_AV_FEAT_RCTG | BTA_AV_FEAT_METADATA | BTA_AV_FEAT_VENDOR
 +                   | BTA_AV_FEAT_RCCT | BTA_AV_FEAT_ADV_CTRL,
 +                     bte_av_callback);
 +        BTA_AvRegister(BTA_AV_CHNL_AUDIO, BTC_AV_SERVICE_NAME, 0, bte_av_media_callback, &bta_av_a2d_cos);
 +    } else {
 +        BTA_AvDeregister(btc_av_cb.bta_handle);
 +        BTA_AvDisable();
 +    }
 +    return BT_STATUS_SUCCESS;
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         btc_av_sink_execute_service
 +**
 +** Description      Initializes/Shuts down the service
 +**
 +** Returns          BT_STATUS_SUCCESS on success, BT_STATUS_FAIL otherwise
 +**
 +*******************************************************************************/
 +bt_status_t btc_av_sink_execute_service(BOOLEAN b_enable)
 +{
 +#if (BTA_AV_SINK_INCLUDED == TRUE)
 +    BTA_AvEnable_Sink(b_enable);
 +#endif
 +    return BT_STATUS_SUCCESS;
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         btc_av_is_connected
 +**
 +** Description      Checks if av has a connected sink
 +**
 +** Returns          BOOLEAN
 +**
 +*******************************************************************************/
 +BOOLEAN btc_av_is_connected(void)
 +{
 +    btc_sm_state_t state = btc_sm_get_state(btc_av_cb.sm_handle);
 +    return ((state == BTC_AV_STATE_OPENED) || (state ==  BTC_AV_STATE_STARTED));
 +}
 +
 +/*******************************************************************************
 +**
 +** Function         btc_av_is_peer_edr
 +**
 +** Description      Check if the connected a2dp device supports
 +**                  EDR or not. Only when connected this function
 +**                  will accurately provide a true capability of
 +**                  remote peer. If not connected it will always be false.
 +**
 +** Returns          TRUE if remote device is capable of EDR
 +**
 +*******************************************************************************/
 +BOOLEAN btc_av_is_peer_edr(void)
 +{
 +    BTC_ASSERTC(btc_av_is_connected(), "No active a2dp connection\n", 0);
 +
 +    if (btc_av_cb.edr) {
 +        return TRUE;
 +    } else {
 +        return FALSE;
 +    }
 +}
 +
 +/******************************************************************************
 +**
 +** Function        btc_av_clear_remote_suspend_flag
 +**
 +** Description     Clears btc_av_cb.flags if BTC_AV_FLAG_REMOTE_SUSPEND is set
 +**
 +** Returns          void
 +******************************************************************************/
 +void btc_av_clear_remote_suspend_flag(void)
 +{
 +    LOG_DEBUG("%s: flag :%x\n", __func__, btc_av_cb.flags);
 +    btc_av_cb.flags &= ~BTC_AV_FLAG_REMOTE_SUSPEND;
 +}
 +
 +void btc_a2dp_call_handler(btc_msg_t *msg)
 +{
 +    btc_av_args_t *arg = (btc_av_args_t *)(msg->arg);
 +    switch (msg->act) {
 +    case BTC_AV_SINK_CONFIG_REQ_EVT: {
 +        btc_sm_dispatch(btc_av_cb.sm_handle, msg->act, (void *)(msg->arg));
 +        break;
 +    }
 +    case BTC_AV_SINK_API_INIT_EVT: {
 +        btc_a2d_sink_init();
 +        // todo: callback to application
 +        break;
 +    }
 +    case BTC_AV_SINK_API_DEINIT_EVT: {
 +        btc_a2d_sink_deinit();
 +        // todo: callback to application
 +        break;
 +    }
 +    case BTC_AV_SINK_API_CONNECT_EVT: {
 +        btc_a2d_sink_connect(&arg->connect);
 +        // todo: callback to application
 +        break;
 +    }
 +    case BTC_AV_SINK_API_DISCONNECT_EVT: {
 +        CHECK_BTAV_INIT();
 +        btc_sm_dispatch(btc_av_cb.sm_handle, BTC_AV_DISCONNECT_REQ_EVT, NULL);
 +        break;
 +    }
 +    case BTC_AV_SINK_API_REG_DATA_CB_EVT: {
 +        btc_a2dp_sink_reg_data_cb(arg->data_cb);
 +        break;
 +    }
 +    default:
 +        LOG_WARN("%s : unhandled event: %d\n", __FUNCTION__, msg->act);
 +    }
 +}
 +
 +void btc_a2dp_cb_handler(btc_msg_t *msg)
 +{
 +    btc_sm_dispatch(btc_av_cb.sm_handle, msg->act, (void *)(msg->arg));
 +    btc_av_event_free_data(msg->act, msg->arg);
 +}
index 397c51c4bd22d88eafb20c78407f937416fc7009,0000000000000000000000000000000000000000..d4088d39115a41630c874f522bb8c6e5d1e122c6
mode 100644,000000..100644
--- /dev/null
@@@ -1,466 -1,0 +1,464 @@@
-     // todo: uncomment the following line when added the AVRC target role
-     // BTC_HAL_CBACK(bt_rc_callbacks, remote_features_cb, &rc_addr, rc_features)
 +// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
 +//
 +// Licensed under the Apache License, Version 2.0 (the "License");
 +// you may not use this file except in compliance with the License.
 +// You may obtain a copy of the License at
 +
 +//     http://www.apache.org/licenses/LICENSE-2.0
 +//
 +// Unless required by applicable law or agreed to in writing, software
 +// distributed under the License is distributed on an "AS IS" BASIS,
 +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 +// See the License for the specific language governing permissions and
 +// limitations under the License.
 +
 +/*****************************************************************************
 + *
 + *  Filename:      btc_avrc.c
 + *
 + *  Description:   Bluetooth AVRC implementation
 + *
 + *****************************************************************************/
 +#include <string.h>
 +#include "bta_api.h"
 +#include "bta_av_api.h"
 +#include "avrc_defs.h"
 +#include "gki.h"
 +
 +#include "btc_common.h"
 +#include "btc_util.h"
 +#include "btc_av.h"
 +#include "btc_avrc.h"
 +#include "btc_manage.h"
 +#include "esp_avrc_api.h"
 +
 +/*****************************************************************************
 +**  Constants & Macros
 +******************************************************************************/
 +/* for AVRC 1.4 need to change this */
 +#define MAX_RC_NOTIFICATIONS AVRC_EVT_APP_SETTING_CHANGE
 +
 +#define MAX_VOLUME 128
 +#define MAX_LABEL 16
 +#define MAX_TRANSACTIONS_PER_SESSION 16
 +#define MAX_CMD_QUEUE_LEN 8
 +
 +#define CHECK_ESP_RC_CONNECTED       do { \
 +        LOG_DEBUG("## %s ##", __FUNCTION__); \
 +        if (btc_rc_vb.rc_connected == FALSE) { \
 +            LOG_WARN("Function %s() called when RC is not connected", __FUNCTION__); \
 +          return ESP_ERR_INVALID_STATE; \
 +        } \
 +    } while (0)
 +
 +/*****************************************************************************
 +**  Local type definitions
 +******************************************************************************/
 +typedef struct {
 +    UINT8 bNotify;
 +    UINT8 label;
 +} btc_rc_reg_notifications_t;
 +
 +typedef struct
 +{
 +    UINT8   label;
 +    UINT8   ctype;
 +    BOOLEAN is_rsp_pending;
 +} btc_rc_cmd_ctxt_t;
 +
 +/* TODO : Merge btc_rc_reg_notifications_t and btc_rc_cmd_ctxt_t to a single struct */
 +typedef struct {
 +    BOOLEAN                     rc_connected;
 +    UINT8                       rc_handle;
 +    tBTA_AV_FEAT                rc_features;
 +    BD_ADDR                     rc_addr;
 +    UINT16                      rc_pending_play;
 +    btc_rc_cmd_ctxt_t           rc_pdu_info[MAX_CMD_QUEUE_LEN];
 +    btc_rc_reg_notifications_t  rc_notif[MAX_RC_NOTIFICATIONS];
 +    unsigned int                rc_volume;
 +    uint8_t                     rc_vol_label;
 +} btc_rc_cb_t;
 +
 +typedef struct {
 +    BOOLEAN in_use;
 +    UINT8 lbl;
 +    UINT8 handle;
 +} rc_transaction_t;
 +
 +typedef struct
 +{
 +    pthread_mutex_t lbllock;
 +    rc_transaction_t transaction[MAX_TRANSACTIONS_PER_SESSION];
 +} rc_device_t;
 +
 +rc_device_t device;
 +
 +static void handle_rc_connect (tBTA_AV_RC_OPEN *p_rc_open);
 +static void handle_rc_disconnect (tBTA_AV_RC_CLOSE *p_rc_close);
 +static void handle_rc_passthrough_rsp ( tBTA_AV_REMOTE_RSP *p_remote_rsp);
 +
 +/*****************************************************************************
 +**  Static variables
 +******************************************************************************/
 +static btc_rc_cb_t btc_rc_vb;
 +
 +/*****************************************************************************
 +**  Externs
 +******************************************************************************/
 +/*****************************************************************************
 +**  Static functions
 +******************************************************************************/
 +static inline void btc_avrc_ct_cb_to_app(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param)
 +{
 +    esp_avrc_ct_cb_t btc_avrc_cb = (esp_avrc_ct_cb_t)btc_profile_cb_get(BTC_PID_AVRC);
 +    if (btc_avrc_cb) {
 +      btc_avrc_cb(event, param);
 +    }
 +}
 +
 +static void handle_rc_features(void)
 +{
 +    btrc_remote_features_t rc_features = BTRC_FEAT_NONE;
 +    bt_bdaddr_t rc_addr;
 +    bdcpy(rc_addr.address, btc_rc_vb.rc_addr);
 +
 +    // TODO(eisenbach): If devices need to be blacklisted for absolute
 +    // volume, it should be added to device/include/interop_database.h
 +    // For now, everything goes... If blacklisting is necessary, exclude
 +    // the following bit here:
 +    //    btc_rc_vb.rc_features &= ~BTA_AV_FEAT_ADV_CTRL;
 +
 +    if (btc_rc_vb.rc_features & BTA_AV_FEAT_BROWSE)
 +    {
 +        rc_features |= BTRC_FEAT_BROWSE;
 +    }
 +
 +    if ( (btc_rc_vb.rc_features & BTA_AV_FEAT_ADV_CTRL) &&
 +         (btc_rc_vb.rc_features & BTA_AV_FEAT_RCTG))
 +    {
 +        rc_features |= BTRC_FEAT_ABSOLUTE_VOLUME;
 +    }
 +
 +    if (btc_rc_vb.rc_features & BTA_AV_FEAT_METADATA)
 +    {
 +        rc_features |= BTRC_FEAT_METADATA;
 +    }
 +
 +    LOG_DEBUG("%s: rc_features=0x%x", __FUNCTION__, rc_features);
-     LOG_INFO("## %s ##", __FUNCTION__);
 +}
 +
 +
 +/***************************************************************************
 + *  Function       handle_rc_connect
 + *
 + *  - Argument:    tBTA_AV_RC_OPEN  RC open data structure
 + *
 + *  - Description: RC connection event handler
 + *
 + ***************************************************************************/
 +static void handle_rc_connect (tBTA_AV_RC_OPEN *p_rc_open)
 +{
 +    LOG_DEBUG("%s: rc_handle: %d", __FUNCTION__, p_rc_open->rc_handle);
 +#if (AVRC_CTLR_INCLUDED == TRUE)
 +    bt_bdaddr_t rc_addr;
 +#endif
 +
 +    if(p_rc_open->status == BTA_AV_SUCCESS)
 +    {
 +        //check if already some RC is connected
 +        if (btc_rc_vb.rc_connected)
 +        {
 +            LOG_ERROR("Got RC OPEN in connected state, Connected RC: %d \
 +                and Current RC: %d", btc_rc_vb.rc_handle,p_rc_open->rc_handle );
 +            if ((btc_rc_vb.rc_handle != p_rc_open->rc_handle)
 +                && (bdcmp(btc_rc_vb.rc_addr, p_rc_open->peer_addr)))
 +            {
 +                LOG_DEBUG("Got RC connected for some other handle");
 +                BTA_AvCloseRc(p_rc_open->rc_handle);
 +                return;
 +            }
 +        }
 +        memcpy(btc_rc_vb.rc_addr, p_rc_open->peer_addr, sizeof(BD_ADDR));
 +        btc_rc_vb.rc_features = p_rc_open->peer_features;
 +        btc_rc_vb.rc_vol_label = MAX_LABEL;
 +        btc_rc_vb.rc_volume = MAX_VOLUME;
 +
 +        btc_rc_vb.rc_connected = TRUE;
 +        btc_rc_vb.rc_handle = p_rc_open->rc_handle;
 +
 +        /* on locally initiated connection we will get remote features as part of connect */
 +        if (btc_rc_vb.rc_features != 0)
 +            handle_rc_features();
 +#if (AVRC_CTLR_INCLUDED == TRUE)
 +        bdcpy(rc_addr.address, btc_rc_vb.rc_addr);
 +        /* report connection state if device is AVRCP target */
 +        if (btc_rc_vb.rc_features & BTA_AV_FEAT_RCTG) {
 +          esp_avrc_ct_cb_param_t param;
 +          memset(&param, 0, sizeof(esp_avrc_ct_cb_param_t));
 +          param.conn_stat.connected = true;
 +          param.conn_stat.feat_mask = btc_rc_vb.rc_features;
 +          memcpy(param.conn_stat.remote_bda, &rc_addr, sizeof(esp_bd_addr_t));
 +          btc_avrc_ct_cb_to_app(ESP_AVRC_CT_CONNECTION_STATE_EVT, &param);
 +        }
 +#endif
 +    }
 +    else
 +    {
 +        LOG_ERROR("%s Connect failed with error code: %d",
 +            __FUNCTION__, p_rc_open->status);
 +        btc_rc_vb.rc_connected = FALSE;
 +    }
 +}
 +
 +/***************************************************************************
 + *  Function       handle_rc_disconnect
 + *
 + *  - Argument:    tBTA_AV_RC_CLOSE     RC close data structure
 + *
 + *  - Description: RC disconnection event handler
 + *
 + ***************************************************************************/
 +static void handle_rc_disconnect (tBTA_AV_RC_CLOSE *p_rc_close)
 +{
 +#if (AVRC_CTLR_INCLUDED == TRUE)
 +    bt_bdaddr_t rc_addr;
 +    tBTA_AV_FEAT features;
 +#endif
 +    LOG_DEBUG("%s: rc_handle: %d", __FUNCTION__, p_rc_close->rc_handle);
 +    if ((p_rc_close->rc_handle != btc_rc_vb.rc_handle)
 +        && (bdcmp(btc_rc_vb.rc_addr, p_rc_close->peer_addr)))
 +    {
 +        LOG_ERROR("Got disconnect of unknown device");
 +        return;
 +    }
 +
 +    btc_rc_vb.rc_handle = 0;
 +    btc_rc_vb.rc_connected = FALSE;
 +    memset(btc_rc_vb.rc_addr, 0, sizeof(BD_ADDR));
 +    memset(btc_rc_vb.rc_notif, 0, sizeof(btc_rc_vb.rc_notif));
 +#if (AVRC_CTLR_INCLUDED == TRUE)
 +    features = btc_rc_vb.rc_features;
 +#endif
 +    btc_rc_vb.rc_features = 0;
 +    btc_rc_vb.rc_vol_label=MAX_LABEL;
 +    btc_rc_vb.rc_volume=MAX_VOLUME;
 +#if (AVRC_CTLR_INCLUDED == TRUE)
 +    bdcpy(rc_addr.address, btc_rc_vb.rc_addr);
 +#endif
 +    memset(btc_rc_vb.rc_addr, 0, sizeof(BD_ADDR));
 +#if (AVRC_CTLR_INCLUDED == TRUE)
 +    /* report connection state if device is AVRCP target */
 +    if (features & BTA_AV_FEAT_RCTG) {
 +      esp_avrc_ct_cb_param_t param;
 +      memset(&param, 0, sizeof(esp_avrc_ct_cb_param_t));
 +      param.conn_stat.connected = false;
 +      memcpy(param.conn_stat.remote_bda, &rc_addr, sizeof(esp_bd_addr_t));
 +      btc_avrc_ct_cb_to_app(ESP_AVRC_CT_CONNECTION_STATE_EVT, &param);
 +    }
 +#endif
 +}
 +
 +/***************************************************************************
 + *  Function       handle_rc_passthrough_rsp
 + *
 + *  - Argument:    tBTA_AV_REMOTE_RSP passthrough command response
 + *
 + *  - Description: Remote control passthrough response handler
 + *
 + ***************************************************************************/
 +static void handle_rc_passthrough_rsp ( tBTA_AV_REMOTE_RSP *p_remote_rsp)
 +{
 +#if (AVRC_CTLR_INCLUDED == TRUE)
 +    const char *status;
 +    if (btc_rc_vb.rc_features & BTA_AV_FEAT_RCTG)
 +    {
 +        int key_state;
 +        if (p_remote_rsp->key_state == AVRC_STATE_RELEASE)
 +        {
 +            status = "released";
 +            key_state = 1;
 +        }
 +        else
 +        {
 +            status = "pressed";
 +            key_state = 0;
 +        }
 +
 +        LOG_DEBUG("%s: rc_id=%d status=%s", __FUNCTION__, p_remote_rsp->rc_id, status);
 +
 +      do {
 +          esp_avrc_ct_cb_param_t param;
 +          memset(&param, 0, sizeof(esp_avrc_ct_cb_param_t));
 +            param.psth_rsp.tl = p_remote_rsp->label;
 +          param.psth_rsp.key_code = p_remote_rsp->rc_id;
 +          param.psth_rsp.key_state = key_state;
 +          btc_avrc_ct_cb_to_app(ESP_AVRC_CT_PASSTHROUGH_RSP_EVT, &param);
 +      } while (0);
 +    }
 +    else
 +    {
 +        LOG_ERROR("%s DUT does not support AVRCP controller role", __FUNCTION__);
 +    }
 +#else
 +    LOG_ERROR("%s AVRCP controller role is not enabled", __FUNCTION__);
 +#endif
 +}
 +
 +
 +/***************************************************************************
 + **
 + ** Function       btc_rc_handler
 + **
 + ** Description    RC event handler
 + **
 + ***************************************************************************/
 +void btc_rc_handler(tBTA_AV_EVT event, tBTA_AV *p_data)
 +{
 +    LOG_DEBUG ("%s event:%s", __FUNCTION__, dump_rc_event(event));
 +    switch (event)
 +    {
 +        case BTA_AV_RC_OPEN_EVT:
 +        {
 +            LOG_DEBUG("Peer_features:%x", p_data->rc_open.peer_features);
 +            handle_rc_connect( &(p_data->rc_open) );
 +        }break;
 +
 +        case BTA_AV_RC_CLOSE_EVT:
 +        {
 +            handle_rc_disconnect( &(p_data->rc_close) );
 +        }break;
 +
 +#if (AVRC_CTLR_INCLUDED == TRUE)
 +        case BTA_AV_REMOTE_RSP_EVT:
 +        {
 +            LOG_DEBUG("RSP: rc_id:0x%x key_state:%d", p_data->remote_rsp.rc_id,
 +                      p_data->remote_rsp.key_state);
 +            handle_rc_passthrough_rsp( (&p_data->remote_rsp) );
 +        }
 +        break;
 +#endif
 +        case BTA_AV_RC_FEAT_EVT:
 +        {
 +            LOG_DEBUG("Peer_features:%x", p_data->rc_feat.peer_features);
 +            btc_rc_vb.rc_features = p_data->rc_feat.peer_features;
 +            handle_rc_features();
 +        }
 +        break;
 +
 +        // below events are not handled for now
 +        case BTA_AV_META_MSG_EVT:
 +        case BTA_AV_REMOTE_CMD_EVT:
 +        default:
 +            LOG_DEBUG("Unhandled RC event : 0x%x", event);
 +    }
 +}
 +
 +/***************************************************************************
 + **
 + ** Function       btc_rc_get_connected_peer
 + **
 + ** Description    Fetches the connected headset's BD_ADDR if any
 + **
 + ***************************************************************************/
 +BOOLEAN btc_rc_get_connected_peer(BD_ADDR peer_addr)
 +{
 +    if (btc_rc_vb.rc_connected == TRUE) {
 +        bdcpy(peer_addr, btc_rc_vb.rc_addr);
 +        return TRUE;
 +    }
 +    return FALSE;
 +}
 +
 +/************************************************************************************
 +**  AVRCP API Functions
 +************************************************************************************/
 +
 +/*******************************************************************************
 +**
 +** Function         btc_avrc_ct_init
 +**
 +** Description      Initializes the AVRC interface
 +**
 +** Returns          esp_err_t
 +**
 +*******************************************************************************/
 +static void btc_avrc_ct_init(void)
 +{
++    LOG_DEBUG("## %s ##", __FUNCTION__);
 +
 +    memset (&btc_rc_vb, 0, sizeof(btc_rc_vb));
 +    btc_rc_vb.rc_vol_label=MAX_LABEL;
 +    btc_rc_vb.rc_volume=MAX_VOLUME;
 +}
 +
 +
 +/***************************************************************************
 +**
 +** Function         cleanup_ctrl
 +**
 +** Description      Closes the AVRC Controller interface
 +**
 +** Returns          void
 +**
 +***************************************************************************/
 +static void btc_avrc_ct_deinit(void)
 +{
 +    LOG_INFO("## %s ##", __FUNCTION__);
 +
 +    memset(&btc_rc_vb, 0, sizeof(btc_rc_cb_t));
 +    LOG_INFO("## %s ## completed", __FUNCTION__);
 +}
 +
 +static bt_status_t btc_avrc_ct_send_passthrough_cmd(uint8_t tl, uint8_t key_code, uint8_t key_state)
 +{
 +    tAVRC_STS status = BT_STATUS_UNSUPPORTED;
 +    if (tl >= 16 ||
 +        key_state > ESP_AVRC_PT_CMD_STATE_RELEASED) {
 +        return ESP_ERR_INVALID_ARG;
 +    }
 +#if (AVRC_CTLR_INCLUDED == TRUE)
 +    CHECK_ESP_RC_CONNECTED;
 +    LOG_DEBUG("%s: key-code: %d, key-state: %d", __FUNCTION__,
 +                                                    key_code, key_state);
 +    if (btc_rc_vb.rc_features & BTA_AV_FEAT_RCTG)
 +    {
 +        BTA_AvRemoteCmd(btc_rc_vb.rc_handle, tl,
 +                        (tBTA_AV_RC)key_code, (tBTA_AV_STATE)key_state);
 +        status =  BT_STATUS_SUCCESS;
 +        LOG_INFO("%s: succesfully sent passthrough command to BTA", __FUNCTION__);
 +    }
 +    else
 +    {
 +        status = BT_STATUS_FAIL;
 +        LOG_DEBUG("%s: feature not supported", __FUNCTION__);
 +    }
 +#else
 +    LOG_DEBUG("%s: feature not enabled", __FUNCTION__);
 +#endif
 +
 +    return status;
 +}
 +
 +void btc_avrc_call_handler(btc_msg_t *msg)
 +{
 +    btc_avrc_args_t *arg = (btc_avrc_args_t *)(msg->arg);
 +    switch (msg->act) {
 +    case BTC_AVRC_CTRL_API_INIT_EVT: {
 +        btc_avrc_ct_init();
 +        // todo: callback to application
 +        break;
 +    }
 +    case BTC_AVRC_CTRL_API_DEINIT_EVT: {
 +        btc_avrc_ct_deinit();
 +        // todo: callback to application
 +        break;
 +    }
 +    case BTC_AVRC_CTRL_API_SND_PTCMD_EVT: {
 +        btc_avrc_ct_send_passthrough_cmd(arg->pt_cmd.tl, arg->pt_cmd.key_code, arg->pt_cmd.key_state);
 +        // todo: callback to application
 +        break;
 +    }
 +    default:
 +        LOG_WARN("%s : unhandled event: %d\n", __FUNCTION__, msg->act);
 +    }
 +}
index c9bd3c6db4f8e12d8c65aa2d9fb6019e3c05a855,01df95388952bcb665f987e1ba39a79133dcb12d..e297a6b2d22cdd33258851d5528ab01848e96bb1
@@@ -40,28 -40,28 +40,28 @@@ enum 
      SIG_PRF_WORK = 0xfd,
      SIG_BTU_START_UP = 0xfe,
      SIG_BTU_WORK = 0xff,
 -    SIG_BTIF_WORK = 0xff
 +    // SIG_BTIF_WORK = 0xff
  };
  
- #define HCI_HOST_TASK_STACK_SIZE      1500
- #define HCI_HOST_TASK_PRIO                    (configMAX_PRIORITIES - 2)
- #define HCI_HOST_TASK_NAME                    "hciHostT"
- #define HCI_HOST_QUEUE_NUM                    40
+ #define HCI_HOST_TASK_STACK_SIZE        (2048 + BT_TASK_EXTRA_STACK_SIZE)
+ #define HCI_HOST_TASK_PRIO              (configMAX_PRIORITIES - 2)
+ #define HCI_HOST_TASK_NAME              "hciHostT"
+ #define HCI_HOST_QUEUE_NUM              40
  
- #define HCI_H4_TASK_STACK_SIZE                1500
- #define HCI_H4_TASK_PRIO                      (configMAX_PRIORITIES - 3)
- #define HCI_H4_TASK_NAME                      "hciH4T"
- #define HCI_H4_QUEUE_NUM                      60
+ #define HCI_H4_TASK_STACK_SIZE          (2048 + BT_TASK_EXTRA_STACK_SIZE)
+ #define HCI_H4_TASK_PRIO                (configMAX_PRIORITIES - 3)
+ #define HCI_H4_TASK_NAME                "hciH4T"
+ #define HCI_H4_QUEUE_NUM                60
  
- #define BTU_TASK_STACK_SIZE                   4096
- #define BTU_TASK_PRIO                         (configMAX_PRIORITIES - 4)
- #define BTU_TASK_NAME                         "btuT"
- #define BTU_QUEUE_NUM                         50
+ #define BTU_TASK_STACK_SIZE             (3584 + BT_TASK_EXTRA_STACK_SIZE)
+ #define BTU_TASK_PRIO                   (configMAX_PRIORITIES - 4)
+ #define BTU_TASK_NAME                   "btuT"
+ #define BTU_QUEUE_NUM                   50
  
- #define BTC_TASK_STACK_SIZE           CONFIG_BTC_TASK_STACK_SIZE      //by menuconfig
- #define BTC_TASK_NAME                 "btcT"
- #define BTC_TASK_PRIO                 (configMAX_PRIORITIES - 5)
- #define BTC_TASK_QUEUE_NUM            20
+ #define BTC_TASK_STACK_SIZE             (CONFIG_BTC_TASK_STACK_SIZE + BT_TASK_EXTRA_STACK_SIZE)       //by menuconfig
+ #define BTC_TASK_NAME                   "btcT"
+ #define BTC_TASK_PRIO                   (configMAX_PRIORITIES - 5)
+ #define BTC_TASK_QUEUE_NUM              20
  
  void btu_task_post(uint32_t sig);
  void hci_host_task_post(void);