Home | History | Annotate | Download | only in av
      1 /******************************************************************************
      2  *
      3  *  Copyright (C) 2004-2012 Broadcom Corporation
      4  *
      5  *  Licensed under the Apache License, Version 2.0 (the "License");
      6  *  you may not use this file except in compliance with the License.
      7  *  You may obtain a copy of the License at:
      8  *
      9  *  http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  *
     17  ******************************************************************************/
     18 
     19 /******************************************************************************
     20  *
     21  *  This file contains action functions for advanced audio/video stream
     22  *  state machine. these functions are shared by both audio and video
     23  *  streams.
     24  *
     25  ******************************************************************************/
     26 
     27 #include "bt_target.h"
     28 #if defined(BTA_AV_INCLUDED) && (BTA_AV_INCLUDED == TRUE)
     29 
     30 #include <assert.h>
     31 #include <string.h>
     32 
     33 #include "avdt_api.h"
     34 #include "bta_av_int.h"
     35 #include "bt_utils.h"
     36 #include "l2cdefs.h"
     37 #include "l2c_api.h"
     38 #include "osi/include/properties.h"
     39 #include "utl.h"
     40 #include "vendor.h"
     41 
     42 #if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
     43 #include "bta_ar_api.h"
     44 #endif
     45 
     46 /*****************************************************************************
     47 **  Constants
     48 *****************************************************************************/
     49 
     50 /* the delay time in milliseconds to start service discovery on AVRCP */
     51 #ifndef BTA_AV_RC_DISC_TIME_VAL
     52 #define BTA_AV_RC_DISC_TIME_VAL     3500
     53 #endif
     54 
     55 /* the timer in milliseconds to guard against link busy and AVDT_CloseReq failed to be sent */
     56 #ifndef BTA_AV_CLOSE_REQ_TIME_VAL
     57 #define BTA_AV_CLOSE_REQ_TIME_VAL   4000
     58 #endif
     59 
     60 /* number to retry on reconfigure failure - some headsets requirs this number to be more than 1 */
     61 #ifndef BTA_AV_RECONFIG_RETRY
     62 #define BTA_AV_RECONFIG_RETRY       6
     63 #endif
     64 
     65 /* ACL quota we are letting FW use for A2DP Offload Tx. */
     66 #define BTA_AV_A2DP_OFFLOAD_XMIT_QUOTA      4
     67 
     68 static void bta_av_st_rc_timer(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data);
     69 
     70 static const size_t SBC_MAX_BITPOOL_OFFSET = 6;
     71 static const size_t SBC_MAX_BITPOOL = 53;
     72 
     73 /* state machine states */
     74 enum
     75 {
     76     BTA_AV_INIT_SST,
     77     BTA_AV_INCOMING_SST,
     78     BTA_AV_OPENING_SST,
     79     BTA_AV_OPEN_SST,
     80     BTA_AV_RCFG_SST,
     81     BTA_AV_CLOSING_SST
     82 };
     83 
     84 
     85 /* the call out functions for audio stream */
     86 const tBTA_AV_CO_FUNCTS bta_av_a2d_cos =
     87 {
     88     bta_av_co_audio_init,
     89     bta_av_co_audio_disc_res,
     90     bta_av_co_audio_getconfig,
     91     bta_av_co_audio_setconfig,
     92     bta_av_co_audio_open,
     93     bta_av_co_audio_close,
     94     bta_av_co_audio_start,
     95     bta_av_co_audio_stop,
     96     bta_av_co_audio_src_data_path,
     97     bta_av_co_audio_delay
     98 };
     99 
    100 /* ssm action functions for audio stream */
    101 const tBTA_AV_SACT bta_av_a2d_action[] =
    102 {
    103     bta_av_do_disc_a2d,     /* BTA_AV_DO_DISC  */
    104     bta_av_cleanup,         /* BTA_AV_CLEANUP */
    105     bta_av_free_sdb,        /* BTA_AV_FREE_SDB */
    106     bta_av_config_ind,      /* BTA_AV_CONFIG_IND */
    107     bta_av_disconnect_req,  /* BTA_AV_DISCONNECT_REQ */
    108     bta_av_security_req,    /* BTA_AV_SECURITY_REQ */
    109     bta_av_security_rsp,    /* BTA_AV_SECURITY_RSP */
    110     bta_av_setconfig_rsp,   /* BTA_AV_SETCONFIG_RSP */
    111     bta_av_st_rc_timer,     /* BTA_AV_ST_RC_TIMER */
    112     bta_av_str_opened,      /* BTA_AV_STR_OPENED */
    113     bta_av_security_ind,    /* BTA_AV_SECURITY_IND */
    114     bta_av_security_cfm,    /* BTA_AV_SECURITY_CFM */
    115     bta_av_do_close,        /* BTA_AV_DO_CLOSE */
    116     bta_av_connect_req,     /* BTA_AV_CONNECT_REQ */
    117     bta_av_sdp_failed,      /* BTA_AV_SDP_FAILED */
    118     bta_av_disc_results,    /* BTA_AV_DISC_RESULTS */
    119     bta_av_disc_res_as_acp, /* BTA_AV_DISC_RES_AS_ACP */
    120     bta_av_open_failed,     /* BTA_AV_OPEN_FAILED */
    121     bta_av_getcap_results,  /* BTA_AV_GETCAP_RESULTS */
    122     bta_av_setconfig_rej,   /* BTA_AV_SETCONFIG_REJ */
    123     bta_av_discover_req,    /* BTA_AV_DISCOVER_REQ */
    124     bta_av_conn_failed,     /* BTA_AV_CONN_FAILED */
    125     bta_av_do_start,        /* BTA_AV_DO_START */
    126     bta_av_str_stopped,     /* BTA_AV_STR_STOPPED */
    127     bta_av_reconfig,        /* BTA_AV_RECONFIG */
    128     bta_av_data_path,       /* BTA_AV_DATA_PATH */
    129     bta_av_start_ok,        /* BTA_AV_START_OK */
    130     bta_av_start_failed,    /* BTA_AV_START_FAILED */
    131     bta_av_str_closed,      /* BTA_AV_STR_CLOSED */
    132     bta_av_clr_cong,        /* BTA_AV_CLR_CONG */
    133     bta_av_suspend_cfm,     /* BTA_AV_SUSPEND_CFM */
    134     bta_av_rcfg_str_ok,     /* BTA_AV_RCFG_STR_OK */
    135     bta_av_rcfg_failed,     /* BTA_AV_RCFG_FAILED */
    136     bta_av_rcfg_connect,    /* BTA_AV_RCFG_CONNECT */
    137     bta_av_rcfg_discntd,    /* BTA_AV_RCFG_DISCNTD */
    138     bta_av_suspend_cont,    /* BTA_AV_SUSPEND_CONT */
    139     bta_av_rcfg_cfm,        /* BTA_AV_RCFG_CFM */
    140     bta_av_rcfg_open,       /* BTA_AV_RCFG_OPEN */
    141     bta_av_security_rej,    /* BTA_AV_SECURITY_REJ */
    142     bta_av_open_rc,         /* BTA_AV_OPEN_RC */
    143     bta_av_chk_2nd_start,   /* BTA_AV_CHK_2ND_START */
    144     bta_av_save_caps,       /* BTA_AV_SAVE_CAPS */
    145     bta_av_set_use_rc,      /* BTA_AV_SET_USE_RC */
    146     bta_av_cco_close,       /* BTA_AV_CCO_CLOSE */
    147     bta_av_switch_role,     /* BTA_AV_SWITCH_ROLE */
    148     bta_av_role_res,        /* BTA_AV_ROLE_RES */
    149     bta_av_delay_co,        /* BTA_AV_DELAY_CO */
    150     bta_av_open_at_inc,     /* BTA_AV_OPEN_AT_INC */
    151     bta_av_offload_req,     /* BTA_AV_OFFLOAD_REQ */
    152     bta_av_offload_rsp,     /* BTA_AV_OFFLOAD_RSP */
    153     NULL
    154 };
    155 
    156 /* these tables translate AVDT events to SSM events */
    157 static const UINT16 bta_av_stream_evt_ok[] = {
    158     BTA_AV_STR_DISC_OK_EVT,         /* AVDT_DISCOVER_CFM_EVT */
    159     BTA_AV_STR_GETCAP_OK_EVT,       /* AVDT_GETCAP_CFM_EVT */
    160     BTA_AV_STR_OPEN_OK_EVT,         /* AVDT_OPEN_CFM_EVT */
    161     BTA_AV_STR_OPEN_OK_EVT,         /* AVDT_OPEN_IND_EVT */
    162     BTA_AV_STR_CONFIG_IND_EVT,      /* AVDT_CONFIG_IND_EVT */
    163     BTA_AV_STR_START_OK_EVT,        /* AVDT_START_CFM_EVT */
    164     BTA_AV_STR_START_OK_EVT,        /* AVDT_START_IND_EVT */
    165     BTA_AV_STR_SUSPEND_CFM_EVT,     /* AVDT_SUSPEND_CFM_EVT */
    166     BTA_AV_STR_SUSPEND_CFM_EVT,     /* AVDT_SUSPEND_IND_EVT */
    167     BTA_AV_STR_CLOSE_EVT,           /* AVDT_CLOSE_CFM_EVT */
    168     BTA_AV_STR_CLOSE_EVT,           /* AVDT_CLOSE_IND_EVT */
    169     BTA_AV_STR_RECONFIG_CFM_EVT,    /* AVDT_RECONFIG_CFM_EVT */
    170     0,                              /* AVDT_RECONFIG_IND_EVT */
    171     BTA_AV_STR_SECURITY_CFM_EVT,    /* AVDT_SECURITY_CFM_EVT */
    172     BTA_AV_STR_SECURITY_IND_EVT,    /* AVDT_SECURITY_IND_EVT */
    173     BTA_AV_STR_WRITE_CFM_EVT,       /* AVDT_WRITE_CFM_EVT */
    174     BTA_AV_AVDT_CONNECT_EVT,        /* AVDT_CONNECT_IND_EVT */
    175     BTA_AV_AVDT_DISCONNECT_EVT,     /* AVDT_DISCONNECT_IND_EVT */
    176 #if (AVDT_REPORTING == TRUE)
    177     BTA_AV_AVDT_RPT_CONN_EVT,       /* AVDT_REPORT_CONN_EVT */
    178     BTA_AV_AVDT_RPT_CONN_EVT,       /* AVDT_REPORT_DISCONN_EVT */
    179 #endif
    180     BTA_AV_AVDT_DELAY_RPT_EVT,      /* AVDT_DELAY_REPORT_EVT */
    181     0                               /* AVDT_DELAY_REPORT_CFM_EVT */
    182 };
    183 
    184 static const UINT16 bta_av_stream_evt_fail[] = {
    185     BTA_AV_STR_DISC_FAIL_EVT,       /* AVDT_DISCOVER_CFM_EVT */
    186     BTA_AV_STR_GETCAP_FAIL_EVT,     /* AVDT_GETCAP_CFM_EVT */
    187     BTA_AV_STR_OPEN_FAIL_EVT,       /* AVDT_OPEN_CFM_EVT */
    188     BTA_AV_STR_OPEN_OK_EVT,         /* AVDT_OPEN_IND_EVT */
    189     BTA_AV_STR_CONFIG_IND_EVT,      /* AVDT_CONFIG_IND_EVT */
    190     BTA_AV_STR_START_FAIL_EVT,      /* AVDT_START_CFM_EVT */
    191     BTA_AV_STR_START_OK_EVT,        /* AVDT_START_IND_EVT */
    192     BTA_AV_STR_SUSPEND_CFM_EVT,     /* AVDT_SUSPEND_CFM_EVT */
    193     BTA_AV_STR_SUSPEND_CFM_EVT,     /* AVDT_SUSPEND_IND_EVT */
    194     BTA_AV_STR_CLOSE_EVT,           /* AVDT_CLOSE_CFM_EVT */
    195     BTA_AV_STR_CLOSE_EVT,           /* AVDT_CLOSE_IND_EVT */
    196     BTA_AV_STR_RECONFIG_CFM_EVT,    /* AVDT_RECONFIG_CFM_EVT */
    197     0,                              /* AVDT_RECONFIG_IND_EVT */
    198     BTA_AV_STR_SECURITY_CFM_EVT,    /* AVDT_SECURITY_CFM_EVT */
    199     BTA_AV_STR_SECURITY_IND_EVT,    /* AVDT_SECURITY_IND_EVT */
    200     BTA_AV_STR_WRITE_CFM_EVT,       /* AVDT_WRITE_CFM_EVT */
    201     BTA_AV_AVDT_CONNECT_EVT,        /* AVDT_CONNECT_IND_EVT */
    202     BTA_AV_AVDT_DISCONNECT_EVT,     /* AVDT_DISCONNECT_IND_EVT */
    203 #if (AVDT_REPORTING == TRUE)
    204     BTA_AV_AVDT_RPT_CONN_EVT,       /* AVDT_REPORT_CONN_EVT */
    205     BTA_AV_AVDT_RPT_CONN_EVT,       /* AVDT_REPORT_DISCONN_EVT */
    206 #endif
    207     BTA_AV_AVDT_DELAY_RPT_EVT,      /* AVDT_DELAY_REPORT_EVT */
    208     0                               /* AVDT_DELAY_REPORT_CFM_EVT */
    209 };
    210 
    211 void bta_av_stream_data_cback(UINT8 handle, BT_HDR *p_pkt, UINT32 time_stamp, UINT8 m_pt);
    212 static void bta_av_stream0_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data);
    213 static void bta_av_stream1_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data);
    214 #if BTA_AV_NUM_STRS > 2
    215 static void bta_av_stream2_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data);
    216 #endif
    217 #if BTA_AV_NUM_STRS > 3
    218 static void bta_av_stream3_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data);
    219 #endif
    220 #if BTA_AV_NUM_STRS > 4
    221 static void bta_av_stream4_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data);
    222 #endif
    223 #if BTA_AV_NUM_STRS > 5
    224 static void bta_av_stream5_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data);
    225 #endif
    226 /* the array of callback functions to receive events from AVDT control channel */
    227 tAVDT_CTRL_CBACK * const bta_av_dt_cback[] =
    228 {
    229     bta_av_stream0_cback
    230     ,bta_av_stream1_cback
    231 #if BTA_AV_NUM_STRS > 2
    232     ,bta_av_stream2_cback
    233 #endif
    234 #if BTA_AV_NUM_STRS > 3
    235     ,bta_av_stream3_cback
    236 #endif
    237 #if BTA_AV_NUM_STRS > 4
    238     ,bta_av_stream4_cback
    239 #endif
    240 #if BTA_AV_NUM_STRS > 5
    241     ,bta_av_stream5_cback
    242 #endif
    243 };
    244 /***********************************************
    245 **
    246 ** Function         bta_get_scb_handle
    247 **
    248 ** Description      gives the registered AVDT handle.by checking with sep_type.
    249 **
    250 **
    251 ** Returns          void
    252 ***********************************************/
    253 static UINT8 bta_av_get_scb_handle(tBTA_AV_SCB *p_scb, UINT8 local_sep)
    254 {
    255     for (int i = 0; i < BTA_AV_MAX_SEPS; i++) {
    256         if ((p_scb->seps[i].tsep == local_sep) &&
    257             (p_scb->seps[i].codec_type == p_scb->codec_type))
    258             return (p_scb->seps[i].av_handle);
    259     }
    260     APPL_TRACE_DEBUG(" bta_av_get_scb_handle appropiate sep_type not found")
    261     return 0; /* return invalid handle */
    262 }
    263 
    264 /***********************************************
    265 **
    266 ** Function         bta_av_get_scb_sep_type
    267 **
    268 ** Description      gives the sep type by cross-checking with AVDT handle
    269 **
    270 **
    271 ** Returns          void
    272 ***********************************************/
    273 static UINT8 bta_av_get_scb_sep_type(tBTA_AV_SCB *p_scb, UINT8 tavdt_handle)
    274 {
    275     for (int i = 0; i < BTA_AV_MAX_SEPS; i++) {
    276         if (p_scb->seps[i].av_handle == tavdt_handle)
    277             return (p_scb->seps[i].tsep);
    278     }
    279     APPL_TRACE_DEBUG(" bta_av_get_scb_sep_type appropiate handle not found")
    280     return 3; /* return invalid sep type */
    281 }
    282 
    283 /*******************************************************************************
    284 **
    285 ** Function         bta_av_save_addr
    286 **
    287 ** Description      copy the bd_addr and maybe reset the supported flags
    288 **
    289 **
    290 ** Returns          void
    291 **
    292 *******************************************************************************/
    293 static void bta_av_save_addr(tBTA_AV_SCB *p_scb, const BD_ADDR b)
    294 {
    295     APPL_TRACE_DEBUG("bta_av_save_addr r:%d, s:%d",
    296         p_scb->recfg_sup, p_scb->suspend_sup);
    297     if(bdcmp(p_scb->peer_addr, b) != 0)
    298     {
    299         APPL_TRACE_ERROR("reset flags");
    300         /* a new addr, reset the supported flags */
    301         p_scb->recfg_sup    = TRUE;
    302         p_scb->suspend_sup  = TRUE;
    303     }
    304 
    305     /* do this copy anyway, just in case the first addr matches
    306      * the control block one by accident */
    307     bdcpy(p_scb->peer_addr, b);
    308 }
    309 
    310 /*******************************************************************************
    311 **
    312 ** Function         notify_start_failed
    313 **
    314 ** Description      notify up-layer AV start failed
    315 **
    316 **
    317 ** Returns          void
    318 **
    319 *******************************************************************************/
    320 static void notify_start_failed(tBTA_AV_SCB *p_scb)
    321 {
    322     tBTA_AV_START   start;
    323     /* if start failed, clear role */
    324     p_scb->role &= ~BTA_AV_ROLE_START_INT;
    325     start.chnl   = p_scb->chnl;
    326     start.status = BTA_AV_FAIL;
    327     start.initiator = TRUE;
    328     start.hndl   = p_scb->hndl;
    329     (*bta_av_cb.p_cback)(BTA_AV_START_EVT, (tBTA_AV *) &start);
    330 }
    331 
    332 /*******************************************************************************
    333 **
    334 ** Function         bta_av_st_rc_timer
    335 **
    336 ** Description      start the AVRC timer if no RC connection & CT is supported &
    337 **                  RC is used or
    338 **                  as ACP (we do not really know if we want AVRC)
    339 **
    340 ** Returns          void
    341 **
    342 *******************************************************************************/
    343 static void bta_av_st_rc_timer(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
    344 {
    345     UNUSED(p_data);
    346 
    347     APPL_TRACE_DEBUG("bta_av_st_rc_timer rc_handle:%d, use_rc: %d",
    348         p_scb->rc_handle, p_scb->use_rc);
    349     /* for outgoing RC connection as INT/CT */
    350     if ((p_scb->rc_handle == BTA_AV_RC_HANDLE_NONE) &&
    351         /* (bta_av_cb.features & BTA_AV_FEAT_RCCT) && */
    352         (p_scb->use_rc == TRUE || (p_scb->role & BTA_AV_ROLE_AD_ACP)))
    353     {
    354         if ((p_scb->wait & BTA_AV_WAIT_ROLE_SW_BITS) == 0) {
    355             bta_sys_start_timer(p_scb->avrc_ct_timer, BTA_AV_RC_DISC_TIME_VAL,
    356                                 BTA_AV_AVRC_TIMER_EVT, p_scb->hndl);
    357         } else {
    358             p_scb->wait |= BTA_AV_WAIT_CHECK_RC;
    359         }
    360     }
    361 
    362 }
    363 
    364 /*******************************************************************************
    365 **
    366 ** Function         bta_av_next_getcap
    367 **
    368 ** Description      The function gets the capabilities of the next available
    369 **                  stream found in the discovery results.
    370 **
    371 ** Returns          TRUE if we sent request to AVDT, FALSE otherwise.
    372 **
    373 *******************************************************************************/
    374 static BOOLEAN bta_av_next_getcap(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
    375 {
    376     int     i;
    377     tAVDT_GETCAP_REQ    *p_req;
    378     BOOLEAN     sent_cmd = FALSE;
    379     UINT16 uuid_int = p_scb->uuid_int;
    380     UINT8 sep_requested = 0;
    381 
    382     if(uuid_int == UUID_SERVCLASS_AUDIO_SOURCE)
    383        sep_requested = AVDT_TSEP_SNK;
    384     else if(uuid_int == UUID_SERVCLASS_AUDIO_SINK)
    385        sep_requested = AVDT_TSEP_SRC;
    386 
    387     for (i = p_scb->sep_info_idx; i < p_scb->num_seps; i++)
    388     {
    389         /* steam not in use, is a sink, and is the right media type (audio/video) */
    390         if ((p_scb->sep_info[i].in_use == FALSE) &&
    391             (p_scb->sep_info[i].tsep == sep_requested) &&
    392             (p_scb->sep_info[i].media_type == p_scb->media_type))
    393         {
    394             p_scb->sep_info_idx = i;
    395 
    396             /* we got a stream; get its capabilities */
    397             if (p_scb->p_cap == NULL)
    398                 p_scb->p_cap = (tAVDT_CFG *)osi_malloc(sizeof(tAVDT_CFG));
    399             if (p_scb->avdt_version >= AVDT_VERSION_SYNC)
    400             {
    401                 p_req = AVDT_GetAllCapReq;
    402             }
    403             else
    404             {
    405                 p_req = AVDT_GetCapReq;
    406             }
    407             (*p_req)(p_scb->peer_addr,
    408                            p_scb->sep_info[i].seid,
    409                            p_scb->p_cap, bta_av_dt_cback[p_scb->hdi]);
    410             sent_cmd = TRUE;
    411             break;
    412         }
    413     }
    414 
    415     /* if no streams available then stream open fails */
    416     if (!sent_cmd)
    417     {
    418         bta_av_ssm_execute(p_scb, BTA_AV_STR_GETCAP_FAIL_EVT, p_data);
    419     }
    420 
    421     return sent_cmd;
    422 
    423 }
    424 
    425 /*******************************************************************************
    426 **
    427 ** Function         bta_av_proc_stream_evt
    428 **
    429 ** Description      Utility function to compose stream events.
    430 **
    431 ** Returns          void
    432 **
    433 *******************************************************************************/
    434 static void bta_av_proc_stream_evt(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data, int index)
    435 {
    436     UINT16              sec_len = 0;
    437     tBTA_AV_SCB         *p_scb = bta_av_cb.p_scb[index];
    438 
    439     if (p_data)
    440     {
    441         if (event == AVDT_SECURITY_IND_EVT)
    442         {
    443             sec_len = (p_data->security_ind.len < BTA_AV_SECURITY_MAX_LEN) ?
    444                        p_data->security_ind.len : BTA_AV_SECURITY_MAX_LEN;
    445         }
    446         else if (event == AVDT_SECURITY_CFM_EVT && p_data->hdr.err_code == 0)
    447         {
    448             sec_len = (p_data->security_cfm.len < BTA_AV_SECURITY_MAX_LEN) ?
    449                        p_data->security_cfm.len : BTA_AV_SECURITY_MAX_LEN;
    450         }
    451     }
    452 
    453     if (p_scb) {
    454         tBTA_AV_STR_MSG *p_msg =
    455             (tBTA_AV_STR_MSG *)osi_malloc(sizeof(tBTA_AV_STR_MSG) + sec_len);
    456 
    457         /* copy event data, bd addr, and handle to event message buffer */
    458         p_msg->hdr.offset = 0;
    459 
    460         if (bd_addr != NULL)
    461         {
    462             bdcpy(p_msg->bd_addr, bd_addr);
    463             APPL_TRACE_DEBUG("  bd_addr:%02x-%02x-%02x-%02x-%02x-%02x",
    464                           bd_addr[0], bd_addr[1],
    465                           bd_addr[2], bd_addr[3],
    466                           bd_addr[4], bd_addr[5]);
    467         }
    468 
    469         if (p_data != NULL)
    470         {
    471             memcpy(&p_msg->msg, p_data, sizeof (tAVDT_CTRL));
    472             /* copy config params to event message buffer */
    473             switch (event)
    474             {
    475             case AVDT_RECONFIG_CFM_EVT:
    476             APPL_TRACE_DEBUG("reconfig cfm event codec info = 0x%06x-%06x-%06x-%02x",
    477                 (p_msg->msg.reconfig_cfm.p_cfg->codec_info[0]<<16)+(p_msg->msg.reconfig_cfm.p_cfg->codec_info[1]<<8)+p_msg->msg.reconfig_cfm.p_cfg->codec_info[2],
    478                 (p_msg->msg.reconfig_cfm.p_cfg->codec_info[3]<<16)+(p_msg->msg.reconfig_cfm.p_cfg->codec_info[4]<<8)+p_msg->msg.reconfig_cfm.p_cfg->codec_info[5],
    479                 (p_msg->msg.reconfig_cfm.p_cfg->codec_info[6]<<16)+(p_msg->msg.reconfig_cfm.p_cfg->codec_info[7]<<8)+p_msg->msg.reconfig_cfm.p_cfg->codec_info[8],
    480                 p_msg->msg.reconfig_cfm.p_cfg->codec_info[9]);
    481             break;
    482 
    483 
    484 
    485             case AVDT_CONFIG_IND_EVT:
    486             /* We might have 2 SEP signallings(A2DP + VDP) with one peer device on one L2CAP.
    487              * If we already have a signalling connection with the bd_addr and the streaming
    488              * SST is at INIT state, change it to INCOMING state to handle the signalling
    489              * from the 2nd SEP.                                                                */
    490             if ((bta_av_find_lcb(bd_addr, BTA_AV_LCB_FIND) != NULL) && (bta_av_is_scb_init(p_scb)))
    491             {
    492                 bta_av_set_scb_sst_incoming (p_scb);
    493 
    494                 /* When ACP_CONNECT_EVT was received, we put first available scb to incoming state.
    495                  * Later when we receive AVDT_CONFIG_IND_EVT, we use a new p_scb and set its state to
    496                  * incoming which we do it above.
    497                  * We also have to set the old p_scb state to init to be used later             */
    498                 for (int i = 0; i < BTA_AV_NUM_STRS; i++) {
    499                     if ((bta_av_cb.p_scb[i]) && (i != index)) {
    500                         if (bta_av_cb.p_scb[i]->state == BTA_AV_INCOMING_SST) {
    501                             bta_av_cb.p_scb[i]->state = BTA_AV_INIT_SST;
    502                             bta_av_cb.p_scb[i]->coll_mask = 0;
    503                             break;
    504                         }
    505                     }
    506                 }
    507             }
    508 
    509             memcpy(&p_msg->cfg, p_data->config_ind.p_cfg, sizeof(tAVDT_CFG));
    510             break;
    511 
    512             case AVDT_SECURITY_IND_EVT:
    513                 p_msg->msg.security_ind.p_data = (UINT8 *) (p_msg + 1);
    514                 memcpy(p_msg->msg.security_ind.p_data, p_data->security_ind.p_data, sec_len);
    515                 break;
    516 
    517             case AVDT_SECURITY_CFM_EVT:
    518                 p_msg->msg.security_cfm.p_data = (UINT8 *) (p_msg + 1);
    519                 if (p_data->hdr.err_code == 0)
    520                 {
    521                     memcpy(p_msg->msg.security_cfm.p_data, p_data->security_cfm.p_data, sec_len);
    522                 }
    523                 break;
    524             case AVDT_SUSPEND_IND_EVT:
    525                     p_msg->msg.hdr.err_code = 0;
    526                 break;
    527 
    528             default:
    529                 break;
    530             }
    531         }
    532         else
    533             p_msg->msg.hdr.err_code = 0;
    534 
    535         /* look up application event */
    536         if ((p_data == NULL) || (p_data->hdr.err_code == 0))
    537         {
    538             p_msg->hdr.event = bta_av_stream_evt_ok[event];
    539         }
    540         else
    541         {
    542             p_msg->hdr.event = bta_av_stream_evt_fail[event];
    543         }
    544 
    545         p_msg->initiator = FALSE;
    546         if (event == AVDT_SUSPEND_CFM_EVT)
    547             p_msg->initiator = TRUE;
    548 
    549         APPL_TRACE_VERBOSE("hndl:x%x", p_scb->hndl);
    550         p_msg->hdr.layer_specific = p_scb->hndl;
    551         p_msg->handle   = handle;
    552         p_msg->avdt_event = event;
    553         bta_sys_sendmsg(p_msg);
    554     }
    555 
    556 /* coverity[var_deref_model] */
    557 /* false-positive: bta_av_conn_cback only processes AVDT_CONNECT_IND_EVT and AVDT_DISCONNECT_IND_EVT event
    558  *                 these 2 events always have associated p_data */
    559     if (p_data)
    560     {
    561         bta_av_conn_cback(handle, bd_addr, event, p_data);
    562     }
    563     else
    564     {
    565         APPL_TRACE_ERROR("%s: p_data is null", __func__);
    566     }
    567 }
    568 
    569 /*******************************************************************************
    570 **
    571 ** Function         bta_av_stream_data_cback
    572 **
    573 ** Description      This is the AVDTP callback function for stream events.
    574 **
    575 ** Returns          void
    576 **
    577 *******************************************************************************/
    578 void bta_av_stream_data_cback(UINT8 handle, BT_HDR *p_pkt, UINT32 time_stamp, UINT8 m_pt)
    579 {
    580     int index = 0;
    581     tBTA_AV_SCB         *p_scb ;
    582     APPL_TRACE_DEBUG("bta_av_stream_data_cback avdt_handle: %d pkt_len=0x%x  ofst = 0x%x", handle,p_pkt->len,p_pkt->offset);
    583     APPL_TRACE_DEBUG(" Number of frames 0x%x",*((UINT8*)(p_pkt + 1) + p_pkt->offset));
    584     APPL_TRACE_DEBUG("Sequence Number 0x%x",p_pkt->layer_specific);
    585     /* Get  SCB  and correct sep type*/
    586     for(index = 0; index < BTA_AV_NUM_STRS;index ++ )
    587     {
    588         p_scb = bta_av_cb.p_scb[index];
    589         if((p_scb->avdt_handle == handle)&&(p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SNK))
    590             break;
    591     }
    592     if(index == BTA_AV_NUM_STRS) /* cannot find correct handler */
    593     {
    594         osi_free(p_pkt);
    595         return;
    596     }
    597     p_pkt->event = BTA_AV_MEDIA_DATA_EVT;
    598     p_scb->seps[p_scb->sep_idx].p_app_data_cback(BTA_AV_MEDIA_DATA_EVT, (tBTA_AV_MEDIA*)p_pkt);
    599     osi_free(p_pkt);  /* a copy of packet had been delivered, we free this buffer */
    600 }
    601 
    602 /*******************************************************************************
    603 **
    604 ** Function         bta_av_stream0_cback
    605 **
    606 ** Description      This is the AVDTP callback function for stream events.
    607 **
    608 ** Returns          void
    609 **
    610 *******************************************************************************/
    611 static void bta_av_stream0_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
    612 {
    613     APPL_TRACE_VERBOSE("bta_av_stream0_cback avdt_handle: %d event=0x%x", handle, event);
    614     bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 0);
    615 }
    616 
    617 /*******************************************************************************
    618 **
    619 ** Function         bta_av_stream1_cback
    620 **
    621 ** Description      This is the AVDTP callback function for stream events.
    622 **
    623 ** Returns          void
    624 **
    625 *******************************************************************************/
    626 static void bta_av_stream1_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
    627 {
    628     APPL_TRACE_EVENT("bta_av_stream1_cback avdt_handle: %d event=0x%x", handle, event);
    629     bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 1);
    630 }
    631 
    632 #if BTA_AV_NUM_STRS > 2
    633 /*******************************************************************************
    634 **
    635 ** Function         bta_av_stream2_cback
    636 **
    637 ** Description      This is the AVDTP callback function for stream events.
    638 **
    639 ** Returns          void
    640 **
    641 *******************************************************************************/
    642 static void bta_av_stream2_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
    643 {
    644     APPL_TRACE_EVENT("bta_av_stream2_cback avdt_handle: %d event=0x%x", handle, event);
    645     bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 2);
    646 }
    647 #endif
    648 
    649 #if BTA_AV_NUM_STRS > 3
    650 /*******************************************************************************
    651 **
    652 ** Function         bta_av_stream3_cback
    653 **
    654 ** Description      This is the AVDTP callback function for stream events.
    655 **
    656 ** Returns          void
    657 **
    658 *******************************************************************************/
    659 static void bta_av_stream3_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
    660 {
    661     APPL_TRACE_EVENT("bta_av_stream3_cback avdt_handle: %d event=0x%x", handle, event);
    662     bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 3);
    663 }
    664 #endif
    665 
    666 /*******************************************************************************
    667 **
    668 ** Function         bta_av_stream4_cback
    669 **
    670 ** Description      This is the AVDTP callback function for stream events.
    671 **
    672 ** Returns          void
    673 **
    674 *******************************************************************************/
    675 #if BTA_AV_NUM_STRS > 4
    676 static void bta_av_stream4_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
    677 {
    678     APPL_TRACE_EVENT("bta_av_stream4_cback avdt_handle: %d event=0x%x", handle, event);
    679     bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 4);
    680 }
    681 #endif
    682 
    683 /*******************************************************************************
    684 **
    685 ** Function         bta_av_stream5_cback
    686 **
    687 ** Description      This is the AVDTP callback function for stream events.
    688 **
    689 ** Returns          void
    690 **
    691 *******************************************************************************/
    692 #if BTA_AV_NUM_STRS > 5
    693 static void bta_av_stream5_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
    694 {
    695     APPL_TRACE_EVENT("bta_av_stream5_cback avdt_handle: %d event=0x%x", handle, event);
    696     bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 5);
    697 }
    698 #endif
    699 
    700 /*******************************************************************************
    701 **
    702 ** Function         bta_av_a2d_sdp_cback
    703 **
    704 ** Description      A2DP service discovery callback.
    705 **
    706 ** Returns          void
    707 **
    708 *******************************************************************************/
    709 static void bta_av_a2d_sdp_cback(BOOLEAN found, tA2D_Service *p_service)
    710 {
    711     tBTA_AV_SCB *p_scb = bta_av_hndl_to_scb(bta_av_cb.handle);
    712 
    713     if (p_scb == NULL) {
    714         APPL_TRACE_ERROR("%s, no scb found for handle(0x%x)", __func__,
    715                          bta_av_cb.handle);
    716         return;
    717     }
    718 
    719     tBTA_AV_SDP_RES *p_msg =
    720         (tBTA_AV_SDP_RES *)osi_malloc(sizeof(tBTA_AV_SDP_RES));
    721     p_msg->hdr.event = (found) ?
    722         BTA_AV_SDP_DISC_OK_EVT : BTA_AV_SDP_DISC_FAIL_EVT;
    723     if (found && (p_service != NULL))
    724         p_scb->avdt_version = p_service->avdt_version;
    725     else
    726         p_scb->avdt_version = 0x00;
    727     p_msg->hdr.layer_specific = bta_av_cb.handle;
    728 
    729     bta_sys_sendmsg(p_msg);
    730 }
    731 
    732 /*******************************************************************************
    733 **
    734 ** Function         bta_av_adjust_seps_idx
    735 **
    736 ** Description      adjust the sep_idx
    737 **
    738 ** Returns
    739 **
    740 *******************************************************************************/
    741 static void bta_av_adjust_seps_idx(tBTA_AV_SCB *p_scb, UINT8 avdt_handle)
    742 {
    743     APPL_TRACE_DEBUG("bta_av_adjust_seps_idx codec_type: %d", p_scb->codec_type);
    744     for (int i = 0; i < BTA_AV_MAX_SEPS; i++) {
    745         APPL_TRACE_DEBUG("av_handle: %d codec_type: %d",
    746             p_scb->seps[i].av_handle, p_scb->seps[i].codec_type);
    747         if((p_scb->seps[i].av_handle && p_scb->codec_type == p_scb->seps[i].codec_type)
    748             && (p_scb->seps[i].av_handle == avdt_handle))
    749         {
    750             p_scb->sep_idx = i;
    751             p_scb->avdt_handle = p_scb->seps[i].av_handle;
    752             break;
    753         }
    754     }
    755 }
    756 
    757 /*******************************************************************************
    758 **
    759 ** Function         bta_av_switch_role
    760 **
    761 ** Description      Switch role was not started and a timer was started.
    762 **                  another attempt to switch role now - still opening.
    763 **
    764 ** Returns          void
    765 **
    766 *******************************************************************************/
    767 void bta_av_switch_role (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
    768 {
    769     tBTA_AV_RS_RES      switch_res = BTA_AV_RS_NONE;
    770     tBTA_AV_API_OPEN  *p_buf = &p_scb->q_info.open;
    771     UNUSED(p_data);
    772 
    773     APPL_TRACE_DEBUG("bta_av_switch_role wait:x%x", p_scb->wait);
    774     if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_RES_START)
    775         p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RETRY;
    776 
    777     /* clear the masks set when the timer is started */
    778     p_scb->wait &= ~(BTA_AV_WAIT_ROLE_SW_RES_OPEN|BTA_AV_WAIT_ROLE_SW_RES_START);
    779 
    780     if (p_scb->q_tag == BTA_AV_Q_TAG_OPEN)
    781     {
    782         if (bta_av_switch_if_needed(p_scb) || !bta_av_link_role_ok(p_scb, A2D_SET_MULTL_BIT))
    783         {
    784             p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RES_OPEN;
    785         }
    786         else
    787         {
    788             /* this should not happen in theory. Just in case...
    789              * continue to do_disc_a2d */
    790             switch_res = BTA_AV_RS_DONE;
    791         }
    792     }
    793     else
    794     {
    795         /* report failure on OPEN */
    796         switch_res = BTA_AV_RS_FAIL;
    797     }
    798 
    799     if (switch_res != BTA_AV_RS_NONE)
    800     {
    801         if (bta_av_cb.rs_idx == (p_scb->hdi + 1))
    802         {
    803             bta_av_cb.rs_idx = 0;
    804         }
    805         p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_RETRY;
    806         p_scb->q_tag = 0;
    807         p_buf->switch_res = switch_res;
    808         bta_av_do_disc_a2d(p_scb, (tBTA_AV_DATA *)p_buf);
    809     }
    810 }
    811 
    812 /*******************************************************************************
    813 **
    814 ** Function         bta_av_role_res
    815 **
    816 ** Description      Handle the role changed event
    817 **
    818 **
    819 ** Returns          void
    820 **
    821 *******************************************************************************/
    822 void bta_av_role_res (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
    823 {
    824     BOOLEAN         initiator = FALSE;
    825     tBTA_AV_START   start;
    826     tBTA_AV_OPEN    av_open;
    827 
    828     APPL_TRACE_DEBUG("bta_av_role_res q_tag:%d, wait:x%x, role:x%x", p_scb->q_tag, p_scb->wait, p_scb->role);
    829     if (p_scb->role & BTA_AV_ROLE_START_INT)
    830         initiator = TRUE;
    831 
    832     if (p_scb->q_tag == BTA_AV_Q_TAG_START)
    833     {
    834         if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_STARTED)
    835         {
    836             p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
    837             if (p_data->role_res.hci_status != HCI_SUCCESS)
    838             {
    839                 p_scb->role &= ~BTA_AV_ROLE_START_INT;
    840                 bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
    841                 /* start failed because of role switch. */
    842                 start.chnl   = p_scb->chnl;
    843                 start.status = BTA_AV_FAIL_ROLE;
    844                 start.hndl   = p_scb->hndl;
    845                 start.initiator = initiator;
    846                 (*bta_av_cb.p_cback)(BTA_AV_START_EVT, (tBTA_AV *) &start);
    847             }
    848             else
    849             {
    850                 bta_av_start_ok(p_scb, p_data);
    851             }
    852         }
    853         else if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_RES_START)
    854             p_scb->wait |= BTA_AV_WAIT_ROLE_SW_FAILED;
    855     }
    856     else if (p_scb->q_tag == BTA_AV_Q_TAG_OPEN)
    857     {
    858         if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_RES_OPEN)
    859         {
    860             p_scb->role &= ~BTA_AV_ROLE_START_INT;
    861             p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
    862 
    863             if (p_data->role_res.hci_status != HCI_SUCCESS)
    864             {
    865                 /* Open failed because of role switch. */
    866                 bdcpy(av_open.bd_addr, p_scb->peer_addr);
    867                 av_open.chnl   = p_scb->chnl;
    868                 av_open.hndl   = p_scb->hndl;
    869                 start.status = BTA_AV_FAIL_ROLE;
    870                 if(p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SRC )
    871                     av_open.sep = AVDT_TSEP_SNK;
    872                 else if(p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SNK )
    873                     av_open.sep = AVDT_TSEP_SRC;
    874                 (*bta_av_cb.p_cback)(BTA_AV_OPEN_EVT, (tBTA_AV *)&av_open);
    875             }
    876             else
    877             {
    878                 /* Continue av open process */
    879                 p_scb->q_info.open.switch_res = BTA_AV_RS_DONE;
    880                 bta_av_do_disc_a2d (p_scb, (tBTA_AV_DATA *)&(p_scb->q_info.open));
    881             }
    882         }
    883         else
    884         {
    885             APPL_TRACE_WARNING ("Unexpected role switch event: q_tag = %d wait = %d", p_scb->q_tag, p_scb->wait);
    886         }
    887     }
    888 
    889     APPL_TRACE_DEBUG("wait:x%x, role:x%x", p_scb->wait, p_scb->role);
    890 }
    891 
    892 /*******************************************************************************
    893 **
    894 ** Function         bta_av_delay_co
    895 **
    896 ** Description      Call the delay call-out function to report the delay report
    897 **                  from SNK
    898 **
    899 ** Returns          void
    900 **
    901 *******************************************************************************/
    902 void bta_av_delay_co (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
    903 {
    904     p_scb->p_cos->delay(p_scb->hndl, p_data->str_msg.msg.delay_rpt_cmd.delay);
    905 }
    906 
    907 /*******************************************************************************
    908 **
    909 ** Function         bta_av_do_disc_a2d
    910 **
    911 ** Description      Do service discovery for A2DP.
    912 **
    913 ** Returns          void
    914 **
    915 *******************************************************************************/
    916 void bta_av_do_disc_a2d (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
    917 {
    918     BOOLEAN     ok_continue = FALSE;
    919     tA2D_SDP_DB_PARAMS  db_params;
    920     UINT16              attr_list[] = {ATTR_ID_SERVICE_CLASS_ID_LIST,
    921                                        ATTR_ID_PROTOCOL_DESC_LIST,
    922                                        ATTR_ID_BT_PROFILE_DESC_LIST};
    923     UINT16 sdp_uuid = 0; /* UUID for which SDP has to be done */
    924 
    925     APPL_TRACE_DEBUG("bta_av_do_disc_a2d use_rc: %d rs:%d, oc:%d",
    926         p_data->api_open.use_rc, p_data->api_open.switch_res, bta_av_cb.audio_open_cnt);
    927 
    928     memcpy (&(p_scb->open_api), &(p_data->api_open), sizeof(tBTA_AV_API_OPEN));
    929 
    930     switch(p_data->api_open.switch_res)
    931     {
    932     case BTA_AV_RS_NONE:
    933         if (bta_av_switch_if_needed(p_scb) || !bta_av_link_role_ok(p_scb, A2D_SET_MULTL_BIT))
    934         {
    935             /* waiting for role switch result. save the api to control block */
    936             memcpy(&p_scb->q_info.open, &p_data->api_open, sizeof(tBTA_AV_API_OPEN));
    937             p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RES_OPEN;
    938             p_scb->q_tag = BTA_AV_Q_TAG_OPEN;
    939         }
    940         else
    941         {
    942             ok_continue = TRUE;
    943         }
    944         break;
    945 
    946     case BTA_AV_RS_FAIL:
    947         /* report a new failure event  */
    948         p_scb->open_status = BTA_AV_FAIL_ROLE;
    949         bta_av_ssm_execute(p_scb, BTA_AV_SDP_DISC_FAIL_EVT, NULL);
    950         break;
    951 
    952     case BTA_AV_RS_OK:
    953         p_data = (tBTA_AV_DATA *)&p_scb->q_info.open;
    954         /* continue to open if link role is ok */
    955         if (bta_av_link_role_ok(p_scb, A2D_SET_MULTL_BIT))
    956         {
    957             ok_continue = TRUE;
    958         }
    959         else
    960         {
    961             p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RES_OPEN;
    962         }
    963         break;
    964 
    965     case BTA_AV_RS_DONE:
    966         ok_continue = TRUE;
    967         break;
    968     }
    969 
    970     APPL_TRACE_DEBUG("ok_continue: %d wait:x%x, q_tag: %d", ok_continue, p_scb->wait, p_scb->q_tag);
    971     if (!ok_continue)
    972         return;
    973 
    974     /* clear the role switch bits */
    975     p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
    976 
    977     if (p_scb->wait & BTA_AV_WAIT_CHECK_RC)
    978     {
    979         p_scb->wait &= ~BTA_AV_WAIT_CHECK_RC;
    980         bta_sys_start_timer(p_scb->avrc_ct_timer, BTA_AV_RC_DISC_TIME_VAL,
    981                             BTA_AV_AVRC_TIMER_EVT, p_scb->hndl);
    982     }
    983 
    984     if (bta_av_cb.features & BTA_AV_FEAT_MASTER)
    985     {
    986         L2CA_SetDesireRole(L2CAP_ROLE_DISALLOW_SWITCH);
    987 
    988         if (bta_av_cb.audio_open_cnt == 1)
    989         {
    990             /* there's already an A2DP connection. do not allow switch */
    991             bta_sys_clear_default_policy(BTA_ID_AV, HCI_ENABLE_MASTER_SLAVE_SWITCH);
    992         }
    993     }
    994     /* store peer addr other parameters */
    995     bta_av_save_addr(p_scb, p_data->api_open.bd_addr);
    996     p_scb->sec_mask = p_data->api_open.sec_mask;
    997     p_scb->use_rc = p_data->api_open.use_rc;
    998 
    999     bta_sys_app_open(BTA_ID_AV, p_scb->app_id, p_scb->peer_addr);
   1000 
   1001     if (p_scb->skip_sdp == TRUE)
   1002     {
   1003         tA2D_Service a2d_ser;
   1004         a2d_ser.avdt_version = AVDT_VERSION;
   1005         p_scb->skip_sdp = FALSE;
   1006         p_scb->uuid_int = p_data->api_open.uuid;
   1007         /* only one A2D find service is active at a time */
   1008         bta_av_cb.handle = p_scb->hndl;
   1009         APPL_TRACE_WARNING("%s: Skip Sdp for incoming A2dp connection", __func__);
   1010         bta_av_a2d_sdp_cback(TRUE, &a2d_ser);
   1011         return;
   1012     }
   1013 
   1014     /* only one A2D find service is active at a time */
   1015     bta_av_cb.handle = p_scb->hndl;
   1016 
   1017     /* set up parameters */
   1018     db_params.db_len = BTA_AV_DISC_BUF_SIZE;
   1019     db_params.num_attr = 3;
   1020     db_params.p_attrs = attr_list;
   1021     p_scb->uuid_int = p_data->api_open.uuid;
   1022     p_scb->sdp_discovery_started = TRUE;
   1023     if (p_scb->uuid_int == UUID_SERVCLASS_AUDIO_SINK)
   1024         sdp_uuid = UUID_SERVCLASS_AUDIO_SOURCE;
   1025     else if (p_scb->uuid_int == UUID_SERVCLASS_AUDIO_SOURCE)
   1026         sdp_uuid = UUID_SERVCLASS_AUDIO_SINK;
   1027 
   1028     APPL_TRACE_DEBUG("%s: uuid_int 0x%x, Doing SDP For 0x%x", __func__,
   1029                     p_scb->uuid_int, sdp_uuid);
   1030     if (A2D_FindService(sdp_uuid, p_scb->peer_addr, &db_params,
   1031                     bta_av_a2d_sdp_cback) == A2D_SUCCESS)
   1032         return;
   1033 
   1034     /* when the code reaches here, either the DB is NULL
   1035      * or A2D_FindService is not successful */
   1036     bta_av_a2d_sdp_cback(FALSE, NULL);
   1037 }
   1038 
   1039 /*******************************************************************************
   1040 **
   1041 ** Function         bta_av_cleanup
   1042 **
   1043 ** Description      cleanup AV stream control block.
   1044 **
   1045 ** Returns          void
   1046 **
   1047 *******************************************************************************/
   1048 void bta_av_cleanup(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   1049 {
   1050     tBTA_AV_CONN_CHG msg;
   1051     UINT8 role = BTA_AV_ROLE_AD_INT;
   1052     UNUSED(p_data);
   1053 
   1054     APPL_TRACE_DEBUG("bta_av_cleanup");
   1055 
   1056     /* free any buffers */
   1057     osi_free_and_reset((void **)&p_scb->p_cap);
   1058     p_scb->sdp_discovery_started = FALSE;
   1059     p_scb->avdt_version = 0;
   1060 
   1061     /* initialize some control block variables */
   1062     p_scb->open_status = BTA_AV_SUCCESS;
   1063 
   1064     /* if de-registering shut everything down */
   1065     msg.hdr.layer_specific  = p_scb->hndl;
   1066     p_scb->started  = FALSE;
   1067     p_scb->cong = FALSE;
   1068     p_scb->role = role;
   1069     p_scb->cur_psc_mask = 0;
   1070     p_scb->wait = 0;
   1071     p_scb->num_disc_snks = 0;
   1072     alarm_cancel(p_scb->avrc_ct_timer);
   1073 
   1074     vendor_get_interface()->send_command(
   1075         (vendor_opcode_t)BT_VND_OP_A2DP_OFFLOAD_STOP, (void*)&p_scb->l2c_cid);
   1076     if (p_scb->offload_start_pending) {
   1077         tBTA_AV_STATUS status = BTA_AV_FAIL_STREAM;
   1078         (*bta_av_cb.p_cback)(BTA_AV_OFFLOAD_START_RSP_EVT, (tBTA_AV *)&status);
   1079     }
   1080     p_scb->offload_start_pending = FALSE;
   1081 
   1082     p_scb->skip_sdp = FALSE;
   1083     if (p_scb->deregistring)
   1084     {
   1085         /* remove stream */
   1086         for (int i = 0; i < BTA_AV_MAX_SEPS; i++) {
   1087             if (p_scb->seps[i].av_handle)
   1088                 AVDT_RemoveStream(p_scb->seps[i].av_handle);
   1089             p_scb->seps[i].av_handle = 0;
   1090         }
   1091 
   1092         bta_av_dereg_comp((tBTA_AV_DATA *) &msg);
   1093     }
   1094     else
   1095     {
   1096         /* report stream closed to main SM */
   1097         msg.is_up = FALSE;
   1098         bdcpy(msg.peer_addr, p_scb->peer_addr);
   1099         bta_av_conn_chg((tBTA_AV_DATA *) &msg);
   1100     }
   1101 }
   1102 
   1103 /*******************************************************************************
   1104 **
   1105 ** Function         bta_av_free_sdb
   1106 **
   1107 ** Description      Free service discovery db buffer.
   1108 **
   1109 ** Returns          void
   1110 **
   1111 *******************************************************************************/
   1112 void bta_av_free_sdb(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   1113 {
   1114     UNUSED(p_data);
   1115     p_scb->sdp_discovery_started = FALSE;
   1116 }
   1117 
   1118 /*******************************************************************************
   1119 **
   1120 ** Function         bta_av_config_ind
   1121 **
   1122 ** Description      Handle a stream configuration indication from the peer.
   1123 **
   1124 ** Returns          void
   1125 **
   1126 *******************************************************************************/
   1127 void bta_av_config_ind (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   1128 {
   1129     tBTA_AV_CI_SETCONFIG setconfig;
   1130     tAVDT_SEP_INFO       *p_info;
   1131     tAVDT_CFG            *p_evt_cfg = &p_data->str_msg.cfg;
   1132     UINT8   psc_mask = (p_evt_cfg->psc_mask | p_scb->cfg.psc_mask);
   1133     UINT8 local_sep;    /* sep type of local handle on which connection was received */
   1134     tBTA_AV_STR_MSG  *p_msg = (tBTA_AV_STR_MSG *)p_data;
   1135     UNUSED(p_data);
   1136 
   1137     local_sep = bta_av_get_scb_sep_type(p_scb, p_msg->handle);
   1138     p_scb->avdt_label = p_data->str_msg.msg.hdr.label;
   1139     memcpy(p_scb->cfg.codec_info, p_evt_cfg->codec_info, AVDT_CODEC_SIZE);
   1140     p_scb->codec_type = p_evt_cfg->codec_info[BTA_AV_CODEC_TYPE_IDX];
   1141     bta_av_save_addr(p_scb, p_data->str_msg.bd_addr);
   1142 
   1143     /* Clear collision mask */
   1144     p_scb->coll_mask = 0;
   1145     alarm_cancel(bta_av_cb.accept_signalling_timer);
   1146 
   1147     /* if no codec parameters in configuration, fail */
   1148     if ((p_evt_cfg->num_codec == 0) ||
   1149     /* or the peer requests for a service we do not support */
   1150         ((psc_mask != p_scb->cfg.psc_mask) &&
   1151         (psc_mask != (p_scb->cfg.psc_mask&~AVDT_PSC_DELAY_RPT))) )
   1152     {
   1153         setconfig.hndl      = p_scb->hndl; /* we may not need this */
   1154         setconfig.err_code  = AVDT_ERR_UNSUP_CFG;
   1155         bta_av_ssm_execute(p_scb, BTA_AV_CI_SETCONFIG_FAIL_EVT, (tBTA_AV_DATA *) &setconfig);
   1156     }
   1157     else
   1158     {
   1159         p_info = &p_scb->sep_info[0];
   1160         p_info->in_use = 0;
   1161         p_info->media_type = p_scb->media_type;
   1162         p_info->seid = p_data->str_msg.msg.config_ind.int_seid;
   1163 
   1164         /* Sep type of Peer will be oppsite role to our local sep */
   1165         if (local_sep == AVDT_TSEP_SRC)
   1166             p_info->tsep = AVDT_TSEP_SNK;
   1167         else if (local_sep == AVDT_TSEP_SNK)
   1168             p_info->tsep = AVDT_TSEP_SRC;
   1169 
   1170         p_scb->role      |= BTA_AV_ROLE_AD_ACP;
   1171         p_scb->cur_psc_mask = p_evt_cfg->psc_mask;
   1172         if (bta_av_cb.features & BTA_AV_FEAT_RCTG)
   1173             p_scb->use_rc = TRUE;
   1174         else
   1175             p_scb->use_rc = FALSE;
   1176 
   1177         p_scb->num_seps  = 1;
   1178         p_scb->sep_info_idx = 0;
   1179         APPL_TRACE_DEBUG("bta_av_config_ind: SEID: %d use_rc: %d cur_psc_mask:0x%x", p_info->seid, p_scb->use_rc, p_scb->cur_psc_mask);
   1180         /*  in case of A2DP SINK this is the first time peer data is being sent to co functions */
   1181         if (local_sep == AVDT_TSEP_SNK)
   1182         {
   1183             p_scb->p_cos->setcfg(p_scb->hndl, p_scb->codec_type,
   1184                              p_evt_cfg->codec_info,
   1185                              p_info->seid,
   1186                              p_scb->peer_addr,
   1187                              p_evt_cfg->num_protect,
   1188                              p_evt_cfg->protect_info,
   1189                              AVDT_TSEP_SNK,
   1190                              p_msg->handle);
   1191         }
   1192         else
   1193         {
   1194             p_scb->p_cos->setcfg(p_scb->hndl, p_scb->codec_type,
   1195                              p_evt_cfg->codec_info,
   1196                              p_info->seid,
   1197                              p_scb->peer_addr,
   1198                              p_evt_cfg->num_protect,
   1199                              p_evt_cfg->protect_info,
   1200                              AVDT_TSEP_SRC,
   1201                              p_msg->handle);
   1202         }
   1203     }
   1204 }
   1205 
   1206 /*******************************************************************************
   1207 **
   1208 ** Function         bta_av_disconnect_req
   1209 **
   1210 ** Description      Disconnect AVDTP connection.
   1211 **
   1212 ** Returns          void
   1213 **
   1214 *******************************************************************************/
   1215 void bta_av_disconnect_req (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   1216 {
   1217     tBTA_AV_RCB *p_rcb;
   1218     UNUSED(p_data);
   1219 
   1220     APPL_TRACE_DEBUG("%s conn_lcb: 0x%x", __func__, bta_av_cb.conn_lcb);
   1221 
   1222     alarm_cancel(bta_av_cb.link_signalling_timer);
   1223     alarm_cancel(p_scb->avrc_ct_timer);
   1224 
   1225     if (bta_av_cb.conn_lcb) {
   1226         p_rcb = bta_av_get_rcb_by_shdl((UINT8)(p_scb->hdi + 1));
   1227         if (p_rcb)
   1228             bta_av_del_rc(p_rcb);
   1229         AVDT_DisconnectReq(p_scb->peer_addr, bta_av_dt_cback[p_scb->hdi]);
   1230     } else {
   1231         bta_av_ssm_execute(p_scb, BTA_AV_AVDT_DISCONNECT_EVT, NULL);
   1232     }
   1233 }
   1234 
   1235 /*******************************************************************************
   1236 **
   1237 ** Function         bta_av_security_req
   1238 **
   1239 ** Description      Send an AVDTP security request.
   1240 **
   1241 ** Returns          void
   1242 **
   1243 *******************************************************************************/
   1244 void bta_av_security_req (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   1245 {
   1246     if (bta_av_cb.features & BTA_AV_FEAT_PROTECT)
   1247     {
   1248         AVDT_SecurityReq(p_scb->avdt_handle, p_data->api_protect_req.p_data,
   1249                          p_data->api_protect_req.len);
   1250     }
   1251 }
   1252 
   1253 /*******************************************************************************
   1254 **
   1255 ** Function         bta_av_security_rsp
   1256 **
   1257 ** Description      Send an AVDTP security response.
   1258 **
   1259 ** Returns          void
   1260 **
   1261 *******************************************************************************/
   1262 void bta_av_security_rsp (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   1263 {
   1264     if (bta_av_cb.features & BTA_AV_FEAT_PROTECT)
   1265     {
   1266         AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label, p_data->api_protect_rsp.error_code,
   1267                          p_data->api_protect_rsp.p_data, p_data->api_protect_rsp.len);
   1268     }
   1269     else
   1270     {
   1271         AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label, AVDT_ERR_NSC,
   1272                      NULL, 0);
   1273     }
   1274 }
   1275 
   1276 /*******************************************************************************
   1277 **
   1278 ** Function         bta_av_setconfig_rsp
   1279 **
   1280 ** Description      setconfig is OK
   1281 **
   1282 ** Returns          void
   1283 **
   1284 *******************************************************************************/
   1285 void bta_av_setconfig_rsp (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   1286 {
   1287     UINT8   num = p_data->ci_setconfig.num_seid + 1;
   1288     UINT8   avdt_handle = p_data->ci_setconfig.avdt_handle;
   1289     UINT8   *p_seid = p_data->ci_setconfig.p_seid;
   1290     int     i;
   1291     UINT8   local_sep;
   1292 
   1293     /* we like this codec_type. find the sep_idx */
   1294     local_sep = bta_av_get_scb_sep_type(p_scb,avdt_handle);
   1295     bta_av_adjust_seps_idx(p_scb, avdt_handle);
   1296     APPL_TRACE_DEBUG("bta_av_setconfig_rsp: sep_idx: %d cur_psc_mask:0x%x", p_scb->sep_idx, p_scb->cur_psc_mask);
   1297 
   1298     if ((AVDT_TSEP_SNK == local_sep) && (p_data->ci_setconfig.err_code == AVDT_SUCCESS) &&
   1299                                      (p_scb->seps[p_scb->sep_idx].p_app_data_cback != NULL)) {
   1300         tBTA_AV_MEDIA av_sink_codec_info;
   1301         memcpy(av_sink_codec_info.avk_config.bd_addr,p_scb->peer_addr,sizeof(BD_ADDR));
   1302         av_sink_codec_info.avk_config.codec_info = p_scb->cfg.codec_info;
   1303         p_scb->seps[p_scb->sep_idx].p_app_data_cback(BTA_AV_MEDIA_SINK_CFG_EVT,
   1304                                               &av_sink_codec_info);
   1305     }
   1306 
   1307 
   1308     AVDT_ConfigRsp(p_scb->avdt_handle, p_scb->avdt_label, p_data->ci_setconfig.err_code,
   1309                    p_data->ci_setconfig.category);
   1310 
   1311     alarm_cancel(bta_av_cb.link_signalling_timer);
   1312 
   1313     if(p_data->ci_setconfig.err_code == AVDT_SUCCESS)
   1314     {
   1315         p_scb->wait = BTA_AV_WAIT_ACP_CAPS_ON;
   1316         if(p_data->ci_setconfig.recfg_needed)
   1317             p_scb->role |= BTA_AV_ROLE_SUSPEND_OPT;
   1318         APPL_TRACE_DEBUG("bta_av_setconfig_rsp recfg_needed:%d role:x%x num:%d",
   1319             p_data->ci_setconfig.recfg_needed, p_scb->role, num);
   1320         /* callout module tells BTA the number of "good" SEPs and their SEIDs.
   1321          * getcap on these SEID */
   1322         p_scb->num_seps = num;
   1323 
   1324         if (p_scb->cur_psc_mask & AVDT_PSC_DELAY_RPT)
   1325             p_scb->avdt_version = AVDT_VERSION_SYNC;
   1326 
   1327 
   1328         if (p_scb->codec_type == BTA_AV_CODEC_SBC || num > 1)
   1329         {
   1330             /* if SBC is used by the SNK as INT, discover req is not sent in bta_av_config_ind.
   1331                        * call disc_res now */
   1332            /* this is called in A2DP SRC path only, In case of SINK we don't need it  */
   1333             if (local_sep == AVDT_TSEP_SRC)
   1334                 p_scb->p_cos->disc_res(p_scb->hndl, num, num, 0, p_scb->peer_addr,
   1335                                                       UUID_SERVCLASS_AUDIO_SOURCE);
   1336         }
   1337         else
   1338         {
   1339             /* we do not know the peer device and it is using non-SBC codec
   1340              * we need to know all the SEPs on SNK */
   1341             bta_av_discover_req(p_scb, NULL);
   1342             return;
   1343         }
   1344 
   1345         for (i = 1; i < num; i++)
   1346         {
   1347             APPL_TRACE_DEBUG("sep_info[%d] SEID: %d", i, p_seid[i-1]);
   1348             /* initialize the sep_info[] to get capabilities */
   1349             p_scb->sep_info[i].in_use = FALSE;
   1350             p_scb->sep_info[i].tsep = AVDT_TSEP_SNK;
   1351             p_scb->sep_info[i].media_type = p_scb->media_type;
   1352             p_scb->sep_info[i].seid = p_seid[i-1];
   1353         }
   1354 
   1355         /* only in case of local sep as SRC we need to look for other SEPs, In case of SINK we don't */
   1356         if (local_sep == AVDT_TSEP_SRC)
   1357         {
   1358             /* Make sure UUID has been initialized... */
   1359             if (p_scb->uuid_int == 0)
   1360                 p_scb->uuid_int = p_scb->open_api.uuid;
   1361             bta_av_next_getcap(p_scb, p_data);
   1362         }
   1363     }
   1364 }
   1365 
   1366 /*******************************************************************************
   1367 **
   1368 ** Function         bta_av_str_opened
   1369 **
   1370 ** Description      Stream opened OK (incoming/outgoing).
   1371 **
   1372 ** Returns          void
   1373 **
   1374 *******************************************************************************/
   1375 void bta_av_str_opened (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   1376 {
   1377     tBTA_AV_CONN_CHG msg;
   1378     tBTA_AV_OPEN    open;
   1379     UINT8 *p;
   1380     UINT16 mtu;
   1381 
   1382     msg.hdr.layer_specific = p_scb->hndl;
   1383     msg.is_up = TRUE;
   1384     bdcpy(msg.peer_addr, p_scb->peer_addr);
   1385     p_scb->l2c_cid      = AVDT_GetL2CapChannel(p_scb->avdt_handle);
   1386     bta_av_conn_chg((tBTA_AV_DATA *) &msg);
   1387     /* set the congestion flag, so AV would not send media packets by accident */
   1388     p_scb->cong = TRUE;
   1389     p_scb->offload_start_pending = FALSE;
   1390 
   1391 
   1392     p_scb->stream_mtu = p_data->str_msg.msg.open_ind.peer_mtu - AVDT_MEDIA_HDR_SIZE;
   1393     mtu = bta_av_chk_mtu(p_scb, p_scb->stream_mtu);
   1394     APPL_TRACE_DEBUG("bta_av_str_opened l2c_cid: 0x%x stream_mtu: %d mtu: %d",
   1395         p_scb->l2c_cid, p_scb->stream_mtu, mtu);
   1396     if(mtu == 0 || mtu > p_scb->stream_mtu)
   1397         mtu = p_scb->stream_mtu;
   1398 
   1399     /* Set the media channel as medium priority */
   1400     L2CA_SetTxPriority(p_scb->l2c_cid, L2CAP_CHNL_PRIORITY_MEDIUM);
   1401     L2CA_SetChnlFlushability (p_scb->l2c_cid, TRUE);
   1402 
   1403     bta_sys_conn_open(BTA_ID_AV, p_scb->app_id, p_scb->peer_addr);
   1404     memset(&p_scb->q_info, 0, sizeof(tBTA_AV_Q_INFO));
   1405 
   1406     p_scb->l2c_bufs = 0;
   1407     p_scb->p_cos->open(p_scb->hndl,
   1408         p_scb->codec_type, p_scb->cfg.codec_info, mtu);
   1409 
   1410     {
   1411         /* TODO check if other audio channel is open.
   1412          * If yes, check if reconfig is needed
   1413          * Rigt now we do not do this kind of checking.
   1414          * BTA-AV is INT for 2nd audio connection.
   1415          * The application needs to make sure the current codec_info is proper.
   1416          * If one audio connection is open and another SNK attempts to connect to AV,
   1417          * the connection will be rejected.
   1418          */
   1419         /* check if other audio channel is started. If yes, start */
   1420         bdcpy(open.bd_addr, p_scb->peer_addr);
   1421         open.chnl   = p_scb->chnl;
   1422         open.hndl   = p_scb->hndl;
   1423         open.status = BTA_AV_SUCCESS;
   1424         open.starting = bta_av_chk_start(p_scb);
   1425         open.edr    = 0;
   1426         if( NULL != (p = BTM_ReadRemoteFeatures(p_scb->peer_addr)))
   1427         {
   1428             if(HCI_EDR_ACL_2MPS_SUPPORTED(p))
   1429                 open.edr |= BTA_AV_EDR_2MBPS;
   1430             if(HCI_EDR_ACL_3MPS_SUPPORTED(p))
   1431                 open.edr |= BTA_AV_EDR_3MBPS;
   1432         }
   1433 #if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
   1434         bta_ar_avdt_conn(BTA_ID_AV, open.bd_addr);
   1435 #endif
   1436         if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SRC )
   1437             open.sep = AVDT_TSEP_SNK;
   1438         else if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SNK )
   1439             open.sep = AVDT_TSEP_SRC;
   1440 
   1441         (*bta_av_cb.p_cback)(BTA_AV_OPEN_EVT, (tBTA_AV *) &open);
   1442         if(open.starting)
   1443         {
   1444             bta_av_ssm_execute(p_scb, BTA_AV_AP_START_EVT, NULL);
   1445         }
   1446     }
   1447 
   1448     // This code is used to pass PTS TC for AVDTP ABORT
   1449     char value[PROPERTY_VALUE_MAX] = {0};
   1450     if ((osi_property_get("bluetooth.pts.force_a2dp_abort", value, "false"))
   1451         && (!strcmp(value, "true")))
   1452     {
   1453         APPL_TRACE_ERROR ("%s: Calling AVDT_AbortReq", __func__);
   1454         AVDT_AbortReq(p_scb->avdt_handle);
   1455     }
   1456 }
   1457 
   1458 /*******************************************************************************
   1459 **
   1460 ** Function         bta_av_security_ind
   1461 **
   1462 ** Description      Handle an AVDTP security indication.
   1463 **
   1464 ** Returns          void
   1465 **
   1466 *******************************************************************************/
   1467 void bta_av_security_ind (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   1468 {
   1469     tBTA_AV_PROTECT_REQ protect_req;
   1470 
   1471     p_scb->avdt_label = p_data->str_msg.msg.hdr.label;
   1472 
   1473     if (bta_av_cb.features & BTA_AV_FEAT_PROTECT)
   1474     {
   1475         protect_req.chnl    = p_scb->chnl;
   1476         protect_req.hndl    = p_scb->hndl;
   1477         /*
   1478         APPL_TRACE_EVENT("sec ind handle: x%x", protect_req.hndl);
   1479         */
   1480         protect_req.p_data  = p_data->str_msg.msg.security_ind.p_data;
   1481         protect_req.len     = p_data->str_msg.msg.security_ind.len;
   1482 
   1483         (*bta_av_cb.p_cback)(BTA_AV_PROTECT_REQ_EVT, (tBTA_AV *) &protect_req);
   1484     }
   1485     /* app doesn't support security indication; respond with failure */
   1486     else
   1487     {
   1488         AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label, AVDT_ERR_NSC, NULL, 0);
   1489     }
   1490 }
   1491 
   1492 /*******************************************************************************
   1493 **
   1494 ** Function         bta_av_security_cfm
   1495 **
   1496 ** Description      Handle an AVDTP security confirm.
   1497 **
   1498 ** Returns          void
   1499 **
   1500 *******************************************************************************/
   1501 void bta_av_security_cfm (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   1502 {
   1503     tBTA_AV_PROTECT_RSP protect_rsp;
   1504 
   1505     if (bta_av_cb.features & BTA_AV_FEAT_PROTECT)
   1506     {
   1507         protect_rsp.chnl    = p_scb->chnl;
   1508         protect_rsp.hndl    = p_scb->hndl;
   1509         protect_rsp.p_data  = p_data->str_msg.msg.security_cfm.p_data;
   1510         protect_rsp.len     = p_data->str_msg.msg.security_cfm.len;
   1511         protect_rsp.err_code= p_data->str_msg.msg.hdr.err_code;
   1512 
   1513         (*bta_av_cb.p_cback)(BTA_AV_PROTECT_RSP_EVT, (tBTA_AV *) &protect_rsp);
   1514     }
   1515 }
   1516 
   1517 /*******************************************************************************
   1518 **
   1519 ** Function         bta_av_do_close
   1520 **
   1521 ** Description      Close stream.
   1522 **
   1523 ** Returns          void
   1524 **
   1525 *******************************************************************************/
   1526 void bta_av_do_close (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   1527 {
   1528     UNUSED(p_data);
   1529 
   1530     /* stop stream if started */
   1531     if (p_scb->co_started)
   1532     {
   1533         bta_av_str_stopped(p_scb, NULL);
   1534     }
   1535     alarm_cancel(bta_av_cb.link_signalling_timer);
   1536 
   1537     /* close stream */
   1538     p_scb->started = FALSE;
   1539 
   1540     /* drop the buffers queued in L2CAP */
   1541     L2CA_FlushChannel (p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
   1542 
   1543     AVDT_CloseReq(p_scb->avdt_handle);
   1544     /* just in case that the link is congested, link is flow controled by peer or
   1545      * for whatever reason the the close request can not be sent in time.
   1546      * when this timer expires, AVDT_DisconnectReq will be called to disconnect the link
   1547      */
   1548     bta_sys_start_timer(p_scb->avrc_ct_timer, BTA_AV_CLOSE_REQ_TIME_VAL,
   1549                         BTA_AV_API_CLOSE_EVT, p_scb->hndl);
   1550 }
   1551 
   1552 /*******************************************************************************
   1553 **
   1554 ** Function         bta_av_connect_req
   1555 **
   1556 ** Description      Connect AVDTP connection.
   1557 **
   1558 ** Returns          void
   1559 **
   1560 *******************************************************************************/
   1561 void bta_av_connect_req(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   1562 {
   1563     UNUSED(p_data);
   1564 
   1565     p_scb->sdp_discovery_started = FALSE;
   1566     if (p_scb->coll_mask & BTA_AV_COLL_INC_TMR)
   1567     {
   1568         /* SNK initiated L2C connection while SRC was doing SDP.    */
   1569         /* Wait until timeout to check if SNK starts signalling.    */
   1570         APPL_TRACE_EVENT("%s: coll_mask = 0x%2X", __func__, p_scb->coll_mask);
   1571         p_scb->coll_mask |= BTA_AV_COLL_API_CALLED;
   1572         APPL_TRACE_EVENT("%s: updated coll_mask = 0x%2X", __func__,
   1573                          p_scb->coll_mask);
   1574         return;
   1575     }
   1576 
   1577     AVDT_ConnectReq(p_scb->peer_addr, p_scb->sec_mask, bta_av_dt_cback[p_scb->hdi]);
   1578 }
   1579 
   1580 /*******************************************************************************
   1581 **
   1582 ** Function         bta_av_sdp_failed
   1583 **
   1584 ** Description      Service discovery failed.
   1585 **
   1586 ** Returns          void
   1587 **
   1588 *******************************************************************************/
   1589 void bta_av_sdp_failed(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   1590 {
   1591     if (!p_scb->open_status)
   1592         p_scb->open_status = BTA_AV_FAIL_SDP;
   1593 
   1594     p_scb->sdp_discovery_started = FALSE;
   1595     bta_av_str_closed(p_scb, p_data);
   1596 }
   1597 
   1598 /*******************************************************************************
   1599 **
   1600 ** Function         bta_av_disc_results
   1601 **
   1602 ** Description      Handle the AVDTP discover results.  Search through the
   1603 **                  results and find the first available stream, and get
   1604 **                  its capabilities.
   1605 **
   1606 ** Returns          void
   1607 **
   1608 *******************************************************************************/
   1609 void bta_av_disc_results (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   1610 {
   1611     UINT8 num_snks = 0, num_srcs =0, i;
   1612     /* our uuid in case we initiate connection */
   1613     UINT16 uuid_int = p_scb->uuid_int;
   1614 
   1615     APPL_TRACE_DEBUG(" initiator UUID 0x%x", uuid_int);
   1616     /* store number of stream endpoints returned */
   1617     p_scb->num_seps = p_data->str_msg.msg.discover_cfm.num_seps;
   1618 
   1619     for (i = 0; i < p_scb->num_seps; i++)
   1620     {
   1621         /* steam not in use, is a sink, and is audio */
   1622         if ((p_scb->sep_info[i].in_use == FALSE) &&
   1623             (p_scb->sep_info[i].media_type == p_scb->media_type))
   1624         {
   1625             if((p_scb->sep_info[i].tsep == AVDT_TSEP_SNK) &&
   1626                (uuid_int == UUID_SERVCLASS_AUDIO_SOURCE))
   1627                 num_snks++;
   1628 
   1629             if((p_scb->sep_info[i].tsep == AVDT_TSEP_SRC) &&
   1630                (uuid_int == UUID_SERVCLASS_AUDIO_SINK))
   1631                 num_srcs++;
   1632 
   1633         }
   1634     }
   1635 
   1636     p_scb->p_cos->disc_res(p_scb->hndl, p_scb->num_seps, num_snks, num_srcs, p_scb->peer_addr,
   1637                                                                                     uuid_int);
   1638     p_scb->num_disc_snks = num_snks;
   1639     p_scb->num_disc_srcs = num_srcs;
   1640 
   1641     /* if we got any */
   1642     if (p_scb->num_seps > 0)
   1643     {
   1644         /* initialize index into discovery results */
   1645         p_scb->sep_info_idx = 0;
   1646 
   1647         /* get the capabilities of the first available stream */
   1648         bta_av_next_getcap(p_scb, p_data);
   1649     }
   1650     /* else we got discover response but with no streams; we're done */
   1651     else
   1652     {
   1653         bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, p_data);
   1654     }
   1655 }
   1656 
   1657 /*******************************************************************************
   1658 **
   1659 ** Function         bta_av_disc_res_as_acp
   1660 **
   1661 ** Description      Handle the AVDTP discover results.  Search through the
   1662 **                  results and find the first available stream, and get
   1663 **                  its capabilities.
   1664 **
   1665 ** Returns          void
   1666 **
   1667 *******************************************************************************/
   1668 void bta_av_disc_res_as_acp (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   1669 {
   1670     UINT8 num_snks = 0, i;
   1671 
   1672     /* store number of stream endpoints returned */
   1673     p_scb->num_seps = p_data->str_msg.msg.discover_cfm.num_seps;
   1674 
   1675 
   1676 
   1677     for (i = 0; i < p_scb->num_seps; i++)
   1678     {
   1679         /* steam is a sink, and is audio */
   1680         if ((p_scb->sep_info[i].tsep == AVDT_TSEP_SNK) &&
   1681             (p_scb->sep_info[i].media_type == p_scb->media_type))
   1682         {
   1683             p_scb->sep_info[i].in_use = FALSE;
   1684             num_snks++;
   1685         }
   1686     }
   1687     p_scb->p_cos->disc_res(p_scb->hndl, p_scb->num_seps, num_snks, 0, p_scb->peer_addr,
   1688                            UUID_SERVCLASS_AUDIO_SOURCE);
   1689     p_scb->num_disc_snks = num_snks;
   1690     p_scb->num_disc_srcs = 0;
   1691 
   1692     /* if we got any */
   1693     if (p_scb->num_seps > 0)
   1694     {
   1695         /* initialize index into discovery results */
   1696         p_scb->sep_info_idx = 0;
   1697 
   1698         /* get the capabilities of the first available stream */
   1699         bta_av_next_getcap(p_scb, p_data);
   1700     }
   1701     /* else we got discover response but with no streams; we're done */
   1702     else
   1703     {
   1704         bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, p_data);
   1705     }
   1706 }
   1707 
   1708 /*******************************************************************************
   1709 **
   1710 ** Function         bta_av_save_caps
   1711 **
   1712 ** Description      report the SNK SEP capabilities to application
   1713 **
   1714 ** Returns          void
   1715 **
   1716 *******************************************************************************/
   1717 void bta_av_save_caps(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   1718 {
   1719     tAVDT_CFG   cfg;
   1720     tAVDT_SEP_INFO  *p_info = &p_scb->sep_info[p_scb->sep_info_idx];
   1721     UINT8       old_wait = p_scb->wait;
   1722     BOOLEAN     getcap_done = FALSE;
   1723 
   1724     APPL_TRACE_DEBUG("bta_av_save_caps num_seps:%d sep_info_idx:%d wait:x%x",
   1725         p_scb->num_seps, p_scb->sep_info_idx, p_scb->wait);
   1726     memcpy(&cfg, p_scb->p_cap, sizeof(tAVDT_CFG));
   1727     /* let application know the capability of the SNK */
   1728     p_scb->p_cos->getcfg(p_scb->hndl, cfg.codec_info[BTA_AV_CODEC_TYPE_IDX],
   1729         cfg.codec_info, &p_scb->sep_info_idx, p_info->seid,
   1730         &cfg.num_protect, cfg.protect_info);
   1731 
   1732     p_scb->sep_info_idx++;
   1733     if(p_scb->num_seps > p_scb->sep_info_idx)
   1734     {
   1735         /* Some devices have seps at the end of the discover list, which is not */
   1736         /* matching media type(video not audio).                                */
   1737         /* In this case, we are done with getcap without sending another        */
   1738         /* request to AVDT.                                                     */
   1739         if (!bta_av_next_getcap(p_scb, p_data))
   1740             getcap_done = TRUE;
   1741     }
   1742     else
   1743         getcap_done = TRUE;
   1744 
   1745     if (getcap_done)
   1746     {
   1747         /* we are done getting capabilities. restore the p_cb->sep_info_idx */
   1748         p_scb->sep_info_idx = 0;
   1749         p_scb->wait &= ~(BTA_AV_WAIT_ACP_CAPS_ON|BTA_AV_WAIT_ACP_CAPS_STARTED);
   1750         if (old_wait & BTA_AV_WAIT_ACP_CAPS_STARTED)
   1751         {
   1752             bta_av_start_ok (p_scb, NULL);
   1753         }
   1754     }
   1755 }
   1756 
   1757 /*******************************************************************************
   1758 **
   1759 ** Function         bta_av_set_use_rc
   1760 **
   1761 ** Description      set to use AVRC for this stream control block.
   1762 **
   1763 ** Returns          void
   1764 **
   1765 *******************************************************************************/
   1766 void bta_av_set_use_rc (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   1767 {
   1768     UNUSED(p_data);
   1769 
   1770     p_scb->use_rc = TRUE;
   1771 }
   1772 
   1773 /*******************************************************************************
   1774 **
   1775 ** Function         bta_av_cco_close
   1776 **
   1777 ** Description      call close call-out function.
   1778 **
   1779 ** Returns          void
   1780 **
   1781 *******************************************************************************/
   1782 void bta_av_cco_close (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   1783 {
   1784     UINT16 mtu;
   1785     UNUSED(p_data);
   1786 
   1787     mtu = bta_av_chk_mtu(p_scb, BTA_AV_MAX_A2DP_MTU);
   1788 
   1789     p_scb->p_cos->close(p_scb->hndl, p_scb->codec_type, mtu);
   1790 }
   1791 
   1792 /*******************************************************************************
   1793 **
   1794 ** Function         bta_av_open_failed
   1795 **
   1796 ** Description      Failed to open an AVDT stream
   1797 **
   1798 ** Returns          void
   1799 **
   1800 *******************************************************************************/
   1801 void bta_av_open_failed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   1802 {
   1803 
   1804     BOOLEAN is_av_opened = FALSE;
   1805     tBTA_AV_SCB * p_opened_scb = NULL;
   1806     UINT8 idx;
   1807     tBTA_AV_OPEN    open;
   1808 
   1809     APPL_TRACE_DEBUG("bta_av_open_failed");
   1810     p_scb->open_status = BTA_AV_FAIL_STREAM;
   1811     bta_av_cco_close(p_scb, p_data);
   1812 
   1813     /* check whether there is already an opened audio or video connection with the same device */
   1814     for (idx = 0; (idx < BTA_AV_NUM_STRS) && (is_av_opened == FALSE); idx++ )
   1815     {
   1816         p_opened_scb = bta_av_cb.p_scb[idx];
   1817         if (p_opened_scb && (p_opened_scb->state == BTA_AV_OPEN_SST) && (!bdcmp(p_opened_scb->peer_addr,p_scb->peer_addr )) )
   1818             is_av_opened = TRUE;
   1819 
   1820     }
   1821 
   1822     /* if there is already an active AV connnection with the same bd_addr,
   1823        don't send disconnect req, just report the open event with BTA_AV_FAIL_GET_CAP status */
   1824     if (is_av_opened == TRUE)
   1825     {
   1826         bdcpy(open.bd_addr, p_scb->peer_addr);
   1827         open.chnl   = p_scb->chnl;
   1828         open.hndl   = p_scb->hndl;
   1829         open.status = BTA_AV_FAIL_GET_CAP;
   1830         open.starting = bta_av_chk_start(p_scb);
   1831         open.edr    = 0;
   1832         /* set the state back to initial state */
   1833         bta_av_set_scb_sst_init(p_scb);
   1834 
   1835         if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SRC )
   1836             open.sep = AVDT_TSEP_SNK;
   1837         else if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SNK )
   1838             open.sep = AVDT_TSEP_SRC;
   1839 
   1840         (*bta_av_cb.p_cback)(BTA_AV_OPEN_EVT, (tBTA_AV *) &open);
   1841 
   1842     }
   1843     else
   1844     {
   1845         AVDT_DisconnectReq(p_scb->peer_addr, bta_av_dt_cback[p_scb->hdi]);
   1846     }
   1847 }
   1848 
   1849 
   1850 /*******************************************************************************
   1851 **
   1852 ** Function         bta_av_getcap_results
   1853 **
   1854 ** Description      Handle the AVDTP get capabilities results.  Check the codec
   1855 **                  type and see if it matches ours.  If it does not, get the
   1856 **                  capabilities of the next stream, if any.
   1857 **
   1858 ** Returns          void
   1859 **
   1860 *******************************************************************************/
   1861 void bta_av_getcap_results (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   1862 {
   1863     tAVDT_CFG   cfg;
   1864     UINT8       media_type;
   1865     tAVDT_SEP_INFO  *p_info = &p_scb->sep_info[p_scb->sep_info_idx];
   1866     UINT16 uuid_int; /* UUID for which connection was initiatied */
   1867 
   1868     memcpy(&cfg, &p_scb->cfg, sizeof(tAVDT_CFG));
   1869     cfg.num_codec = 1;
   1870     cfg.num_protect = p_scb->p_cap->num_protect;
   1871     memcpy(cfg.codec_info, p_scb->p_cap->codec_info, AVDT_CODEC_SIZE);
   1872     memcpy(cfg.protect_info, p_scb->p_cap->protect_info, AVDT_PROTECT_SIZE);
   1873     media_type = p_scb->p_cap->codec_info[BTA_AV_MEDIA_TYPE_IDX] >> 4;
   1874 
   1875     APPL_TRACE_DEBUG("num_codec %d", p_scb->p_cap->num_codec);
   1876     APPL_TRACE_DEBUG("media type x%x, x%x", media_type, p_scb->media_type);
   1877 #if AVDT_MULTIPLEXING == TRUE
   1878     APPL_TRACE_DEBUG("mux x%x, x%x", cfg.mux_mask, p_scb->p_cap->mux_mask);
   1879 #endif
   1880 
   1881     /* if codec present and we get a codec configuration */
   1882     if ((p_scb->p_cap->num_codec != 0) &&
   1883         (media_type == p_scb->media_type) &&
   1884         (p_scb->p_cos->getcfg(p_scb->hndl, p_scb->p_cap->codec_info[BTA_AV_CODEC_TYPE_IDX],
   1885             cfg.codec_info, &p_scb->sep_info_idx, p_info->seid,
   1886             &cfg.num_protect, cfg.protect_info) == 0))
   1887     {
   1888 #if AVDT_MULTIPLEXING == TRUE
   1889         cfg.mux_mask &= p_scb->p_cap->mux_mask;
   1890         APPL_TRACE_DEBUG("mux_mask used x%x", cfg.mux_mask);
   1891 #endif
   1892         /* save copy of codec type and configuration */
   1893         p_scb->codec_type = cfg.codec_info[BTA_AV_CODEC_TYPE_IDX];
   1894         memcpy(&p_scb->cfg, &cfg, sizeof(tAVDT_CFG));
   1895 
   1896         uuid_int = p_scb->uuid_int;
   1897         APPL_TRACE_DEBUG(" initiator UUID = 0x%x ", uuid_int);
   1898         if (uuid_int == UUID_SERVCLASS_AUDIO_SOURCE)
   1899             bta_av_adjust_seps_idx(p_scb, bta_av_get_scb_handle(p_scb, AVDT_TSEP_SRC));
   1900         else if (uuid_int == UUID_SERVCLASS_AUDIO_SINK)
   1901             bta_av_adjust_seps_idx(p_scb, bta_av_get_scb_handle(p_scb, AVDT_TSEP_SNK));
   1902 
   1903         /* use only the services peer supports */
   1904         cfg.psc_mask &= p_scb->p_cap->psc_mask;
   1905         p_scb->cur_psc_mask = cfg.psc_mask;
   1906 
   1907         if ((uuid_int == UUID_SERVCLASS_AUDIO_SINK) &&
   1908             (p_scb->seps[p_scb->sep_idx].p_app_data_cback != NULL))
   1909         {
   1910             APPL_TRACE_DEBUG("%s Configure Decoder for Sink Connection.", __func__);
   1911             tBTA_AV_MEDIA av_sink_codec_info;
   1912             memcpy(av_sink_codec_info.avk_config.bd_addr,p_scb->peer_addr,sizeof(BD_ADDR));
   1913             av_sink_codec_info.avk_config.codec_info = p_scb->cfg.codec_info;
   1914             p_scb->seps[p_scb->sep_idx].p_app_data_cback(BTA_AV_MEDIA_SINK_CFG_EVT,
   1915                                                          &av_sink_codec_info);
   1916         }
   1917 
   1918         if ((uuid_int == UUID_SERVCLASS_AUDIO_SOURCE) &&
   1919             (cfg.codec_info[SBC_MAX_BITPOOL_OFFSET] > SBC_MAX_BITPOOL))
   1920         {
   1921             APPL_TRACE_WARNING("%s max bitpool length received for SBC is out of range."
   1922                     "Clamping the codec bitpool configuration from %d to %d.", __func__,
   1923                     cfg.codec_info[SBC_MAX_BITPOOL_OFFSET], SBC_MAX_BITPOOL);
   1924             cfg.codec_info[SBC_MAX_BITPOOL_OFFSET] = SBC_MAX_BITPOOL;
   1925         }
   1926 
   1927         /* open the stream */
   1928         AVDT_OpenReq(p_scb->seps[p_scb->sep_idx].av_handle, p_scb->peer_addr,
   1929                      p_scb->sep_info[p_scb->sep_info_idx].seid, &cfg);
   1930 
   1931         if (!bta_av_is_rcfg_sst(p_scb))
   1932         {
   1933             /* free capabilities buffer */
   1934             osi_free_and_reset((void **)&p_scb->p_cap);
   1935         }
   1936     }
   1937     else
   1938     {
   1939         /* try the next stream, if any */
   1940         p_scb->sep_info_idx++;
   1941         bta_av_next_getcap(p_scb, p_data);
   1942     }
   1943 
   1944 }
   1945 
   1946 /*******************************************************************************
   1947 **
   1948 ** Function         bta_av_setconfig_rej
   1949 **
   1950 ** Description      Send AVDTP set config reject.
   1951 **
   1952 ** Returns          void
   1953 **
   1954 *******************************************************************************/
   1955 void bta_av_setconfig_rej (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   1956 {
   1957     tBTA_AV_REJECT reject;
   1958     UINT8   avdt_handle = p_data->ci_setconfig.avdt_handle;
   1959 
   1960     bta_av_adjust_seps_idx(p_scb, avdt_handle);
   1961     APPL_TRACE_DEBUG("bta_av_setconfig_rej: sep_idx: %d",p_scb->sep_idx);
   1962     AVDT_ConfigRsp(p_scb->avdt_handle, p_scb->avdt_label, AVDT_ERR_UNSUP_CFG, 0);
   1963 
   1964     bdcpy(reject.bd_addr, p_data->str_msg.bd_addr);
   1965     reject.hndl = p_scb->hndl;
   1966     (*bta_av_cb.p_cback)(BTA_AV_REJECT_EVT, (tBTA_AV *) &reject);
   1967 }
   1968 
   1969 /*******************************************************************************
   1970 **
   1971 ** Function         bta_av_discover_req
   1972 **
   1973 ** Description      Send an AVDTP discover request to the peer.
   1974 **
   1975 ** Returns          void
   1976 **
   1977 *******************************************************************************/
   1978 void bta_av_discover_req (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   1979 {
   1980     UNUSED(p_data);
   1981 
   1982     /* send avdtp discover request */
   1983 
   1984     AVDT_DiscoverReq(p_scb->peer_addr, p_scb->sep_info, BTA_AV_NUM_SEPS, bta_av_dt_cback[p_scb->hdi]);
   1985 }
   1986 
   1987 /*******************************************************************************
   1988 **
   1989 ** Function         bta_av_conn_failed
   1990 **
   1991 ** Description      AVDTP connection failed.
   1992 **
   1993 ** Returns          void
   1994 **
   1995 *******************************************************************************/
   1996 void bta_av_conn_failed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   1997 {
   1998     p_scb->open_status = BTA_AV_FAIL_STREAM;
   1999     bta_av_str_closed(p_scb, p_data);
   2000 }
   2001 
   2002 /*******************************************************************************
   2003 **
   2004 ** Function         bta_av_do_start
   2005 **
   2006 ** Description      Start stream.
   2007 **
   2008 ** Returns          void
   2009 **
   2010 *******************************************************************************/
   2011 void bta_av_do_start (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   2012 {
   2013     UINT8 policy = HCI_ENABLE_SNIFF_MODE;
   2014     UINT8       cur_role;
   2015 
   2016     APPL_TRACE_DEBUG("bta_av_do_start sco_occupied:%d, role:x%x, started:%d", bta_av_cb.sco_occupied, p_scb->role, p_scb->started);
   2017     if (bta_av_cb.sco_occupied)
   2018     {
   2019         bta_av_start_failed(p_scb, p_data);
   2020         return;
   2021     }
   2022 
   2023     /* disallow role switch during streaming, only if we are the master role
   2024      * i.e. allow role switch, if we are slave.
   2025      * It would not hurt us, if the peer device wants us to be master */
   2026     if ((BTM_GetRole (p_scb->peer_addr, &cur_role) == BTM_SUCCESS) &&
   2027         (cur_role == BTM_ROLE_MASTER) )
   2028     {
   2029         policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
   2030     }
   2031 
   2032     bta_sys_clear_policy(BTA_ID_AV, policy, p_scb->peer_addr);
   2033 
   2034     if ((p_scb->started == FALSE) && ((p_scb->role & BTA_AV_ROLE_START_INT) == 0))
   2035     {
   2036         p_scb->role |= BTA_AV_ROLE_START_INT;
   2037         bta_sys_busy(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
   2038 
   2039         AVDT_StartReq(&p_scb->avdt_handle, 1);
   2040     }
   2041     else if (p_scb->started)
   2042     {
   2043         p_scb->role |= BTA_AV_ROLE_START_INT;
   2044         if ( p_scb->wait == 0 ) {
   2045             if (p_scb->role & BTA_AV_ROLE_SUSPEND) {
   2046                 notify_start_failed(p_scb);
   2047             } else {
   2048                 bta_av_start_ok(p_scb, NULL);
   2049             }
   2050         }
   2051     }
   2052     APPL_TRACE_DEBUG("started %d role:x%x", p_scb->started, p_scb->role);
   2053 }
   2054 
   2055 /*******************************************************************************
   2056 **
   2057 ** Function         bta_av_str_stopped
   2058 **
   2059 ** Description      Stream stopped.
   2060 **
   2061 ** Returns          void
   2062 **
   2063 *******************************************************************************/
   2064 void bta_av_str_stopped (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   2065 {
   2066     tBTA_AV_SUSPEND suspend_rsp;
   2067     UINT8   start = p_scb->started;
   2068     BOOLEAN sus_evt = TRUE;
   2069     BT_HDR  *p_buf;
   2070     UINT8 policy = HCI_ENABLE_SNIFF_MODE;
   2071 
   2072     APPL_TRACE_ERROR("bta_av_str_stopped:audio_open_cnt=%d, p_data %x",
   2073             bta_av_cb.audio_open_cnt, p_data);
   2074 
   2075     bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
   2076     if ((bta_av_cb.features & BTA_AV_FEAT_MASTER) == 0 || bta_av_cb.audio_open_cnt == 1)
   2077         policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
   2078     bta_sys_set_policy(BTA_ID_AV, policy, p_scb->peer_addr);
   2079 
   2080     if (p_scb->co_started)
   2081     {
   2082       vendor_get_interface()->send_command(
   2083           (vendor_opcode_t)BT_VND_OP_A2DP_OFFLOAD_STOP,
   2084           (void*)&p_scb->l2c_cid);
   2085         if (p_scb->offload_start_pending) {
   2086             tBTA_AV_STATUS status = BTA_AV_FAIL_STREAM;
   2087             (*bta_av_cb.p_cback)(BTA_AV_OFFLOAD_START_RSP_EVT, (tBTA_AV *)&status);
   2088         }
   2089         p_scb->offload_start_pending = FALSE;
   2090 
   2091         bta_av_stream_chg(p_scb, FALSE);
   2092         p_scb->co_started = FALSE;
   2093 
   2094         p_scb->p_cos->stop(p_scb->hndl, p_scb->codec_type);
   2095         L2CA_SetFlushTimeout(p_scb->peer_addr, L2CAP_DEFAULT_FLUSH_TO);
   2096     }
   2097 
   2098     /* if q_info.a2d_list is not empty, drop it now */
   2099     if (BTA_AV_CHNL_AUDIO == p_scb->chnl) {
   2100         while (!list_is_empty(p_scb->a2d_list))
   2101         {
   2102             p_buf = (BT_HDR *)list_front(p_scb->a2d_list);
   2103             list_remove(p_scb->a2d_list, p_buf);
   2104             osi_free(p_buf);
   2105         }
   2106 
   2107     /* drop the audio buffers queued in L2CAP */
   2108         if (p_data && p_data->api_stop.flush)
   2109             L2CA_FlushChannel (p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
   2110     }
   2111 
   2112     suspend_rsp.chnl = p_scb->chnl;
   2113     suspend_rsp.hndl = p_scb->hndl;
   2114 
   2115     if (p_data && p_data->api_stop.suspend)
   2116     {
   2117         APPL_TRACE_DEBUG("suspending: %d, sup:%d", start, p_scb->suspend_sup);
   2118         if ((start)  && (p_scb->suspend_sup))
   2119         {
   2120             sus_evt = FALSE;
   2121             p_scb->l2c_bufs = 0;
   2122             AVDT_SuspendReq(&p_scb->avdt_handle, 1);
   2123         }
   2124 
   2125         /* send SUSPEND_EVT event only if not in reconfiguring state and sus_evt is TRUE*/
   2126         if ((sus_evt)&&(p_scb->state != BTA_AV_RCFG_SST))
   2127         {
   2128             suspend_rsp.status = BTA_AV_SUCCESS;
   2129             suspend_rsp.initiator = TRUE;
   2130             (*bta_av_cb.p_cback)(BTA_AV_SUSPEND_EVT, (tBTA_AV *) &suspend_rsp);
   2131         }
   2132     }
   2133     else
   2134     {
   2135         suspend_rsp.status = BTA_AV_SUCCESS;
   2136         suspend_rsp.initiator = TRUE;
   2137         APPL_TRACE_EVENT("bta_av_str_stopped status %d", suspend_rsp.status);
   2138 
   2139         /* send STOP_EVT event only if not in reconfiguring state */
   2140         if (p_scb->state != BTA_AV_RCFG_SST)
   2141         {
   2142             (*bta_av_cb.p_cback)(BTA_AV_STOP_EVT, (tBTA_AV *) &suspend_rsp);
   2143         }
   2144     }
   2145 }
   2146 
   2147 /*******************************************************************************
   2148 **
   2149 ** Function         bta_av_reconfig
   2150 **
   2151 ** Description      process the reconfigure request.
   2152 **                  save the parameter in control block and
   2153 **                  suspend, reconfigure or close the stream
   2154 **
   2155 ** Returns          void
   2156 **
   2157 *******************************************************************************/
   2158 void bta_av_reconfig (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   2159 {
   2160     tAVDT_CFG   *p_cfg;
   2161     tBTA_AV_API_STOP    stop;
   2162     tBTA_AV_API_RCFG    *p_rcfg = &p_data->api_reconfig;
   2163 
   2164     APPL_TRACE_DEBUG("bta_av_reconfig r:%d, s:%d idx: %d (o:%d)",
   2165         p_scb->recfg_sup, p_scb->suspend_sup,
   2166         p_scb->rcfg_idx, p_scb->sep_info_idx);
   2167 
   2168     p_scb->num_recfg = 0;
   2169     /* store the new configuration in control block */
   2170     if (p_scb->p_cap == NULL)
   2171         p_scb->p_cap = (tAVDT_CFG *)osi_malloc(sizeof(tAVDT_CFG));
   2172     p_cfg = p_scb->p_cap;
   2173 
   2174     alarm_cancel(p_scb->avrc_ct_timer);
   2175 
   2176     memcpy(p_cfg, &p_scb->cfg, sizeof(tAVDT_CFG));
   2177     p_cfg->num_protect = p_rcfg->num_protect;
   2178     memcpy(p_cfg->codec_info, p_rcfg->codec_info, AVDT_CODEC_SIZE);
   2179     memcpy(p_cfg->protect_info, p_rcfg->p_protect_info, p_rcfg->num_protect);
   2180     p_scb->rcfg_idx = p_rcfg->sep_info_idx;
   2181     p_scb->p_cap->psc_mask = p_scb->cur_psc_mask;
   2182 
   2183     /* if the requested index differs from the current one, we can only close/open */
   2184     if ((p_scb->rcfg_idx == p_scb->sep_info_idx) &&
   2185         (p_rcfg->suspend)&& (p_scb->recfg_sup) && (p_scb->suspend_sup))
   2186     {
   2187         if(p_scb->started)
   2188         {
   2189             stop.flush   = FALSE;
   2190             stop.suspend = TRUE;
   2191             bta_av_str_stopped(p_scb, (tBTA_AV_DATA *)&stop);
   2192         }
   2193         else
   2194         {
   2195             APPL_TRACE_DEBUG("Reconfig");
   2196             AVDT_ReconfigReq(p_scb->avdt_handle, p_scb->p_cap);
   2197             p_scb->p_cap->psc_mask = p_scb->cur_psc_mask;
   2198         }
   2199     }
   2200     else
   2201     {
   2202         /* close the stream */
   2203         APPL_TRACE_DEBUG("close/open num_protect: %d", p_cfg->num_protect);
   2204         if(p_scb->started)
   2205         {
   2206             bta_av_str_stopped(p_scb, NULL);
   2207             p_scb->started = FALSE;
   2208 
   2209             /* drop the buffers queued in L2CAP */
   2210             L2CA_FlushChannel (p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
   2211 
   2212             AVDT_CloseReq(p_scb->avdt_handle);
   2213         }
   2214     }
   2215 }
   2216 
   2217 /*******************************************************************************
   2218 **
   2219 ** Function         bta_av_data_path
   2220 **
   2221 ** Description      Handle stream data path.
   2222 **
   2223 ** Returns          void
   2224 **
   2225 *******************************************************************************/
   2226 void bta_av_data_path (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   2227 {
   2228     BT_HDR  *p_buf = NULL;
   2229     UINT32  data_len;
   2230     UINT32  timestamp;
   2231     BOOLEAN new_buf = FALSE;
   2232     UINT8   m_pt = 0x60 | p_scb->codec_type;
   2233     tAVDT_DATA_OPT_MASK     opt;
   2234     UNUSED(p_data);
   2235 
   2236     if (p_scb->cong)
   2237     {
   2238         return;
   2239     }
   2240 
   2241     /*
   2242     APPL_TRACE_ERROR("q: %d", p_scb->l2c_bufs);
   2243     */
   2244     //Always get the current number of bufs que'd up
   2245     p_scb->l2c_bufs = (UINT8)L2CA_FlushChannel (p_scb->l2c_cid, L2CAP_FLUSH_CHANS_GET);
   2246 
   2247     if (!list_is_empty(p_scb->a2d_list)) {
   2248         p_buf = (BT_HDR *)list_front(p_scb->a2d_list);
   2249         list_remove(p_scb->a2d_list, p_buf);
   2250          /* use q_info.a2d data, read the timestamp */
   2251         timestamp = *(UINT32 *)(p_buf + 1);
   2252     }
   2253     else
   2254     {
   2255         new_buf = TRUE;
   2256         /* a2d_list empty, call co_data, dup data to other channels */
   2257         p_buf = (BT_HDR *)p_scb->p_cos->data(p_scb->codec_type, &data_len,
   2258                                          &timestamp);
   2259 
   2260         if (p_buf)
   2261         {
   2262             /* use the offset area for the time stamp */
   2263             *(UINT32 *)(p_buf + 1) = timestamp;
   2264 
   2265             /* dup the data to other channels */
   2266             bta_av_dup_audio_buf(p_scb, p_buf);
   2267         }
   2268     }
   2269 
   2270     if(p_buf)
   2271     {
   2272         if(p_scb->l2c_bufs < (BTA_AV_QUEUE_DATA_CHK_NUM))
   2273         {
   2274             /* there's a buffer, just queue it to L2CAP */
   2275             /*  There's no need to increment it here, it is always read from L2CAP see above */
   2276             /* p_scb->l2c_bufs++; */
   2277             /*
   2278             APPL_TRACE_ERROR("qw: %d", p_scb->l2c_bufs);
   2279             */
   2280 
   2281             /* opt is a bit mask, it could have several options set */
   2282             opt = AVDT_DATA_OPT_NONE;
   2283             if (p_scb->no_rtp_hdr)
   2284             {
   2285                 opt |= AVDT_DATA_OPT_NO_RTP;
   2286             }
   2287 
   2288             AVDT_WriteReqOpt(p_scb->avdt_handle, p_buf, timestamp, m_pt, opt);
   2289             p_scb->cong = TRUE;
   2290         }
   2291         else
   2292         {
   2293             /* there's a buffer, but L2CAP does not seem to be moving data */
   2294             if(new_buf)
   2295             {
   2296                 /* just got this buffer from co_data,
   2297                  * put it in queue */
   2298                 list_append(p_scb->a2d_list, p_buf);
   2299             }
   2300             else
   2301             {
   2302                 /* just dequeue it from the a2d_list */
   2303                 if (list_length(p_scb->a2d_list) < 3) {
   2304                     /* put it back to the queue */
   2305                     list_prepend(p_scb->a2d_list, p_buf);
   2306                 }
   2307                 else
   2308                 {
   2309                     /* too many buffers in a2d_list, drop it. */
   2310                     bta_av_co_audio_drop(p_scb->hndl);
   2311                     osi_free(p_buf);
   2312                 }
   2313             }
   2314         }
   2315     }
   2316 }
   2317 
   2318 /*******************************************************************************
   2319 **
   2320 ** Function         bta_av_start_ok
   2321 **
   2322 ** Description      Stream started.
   2323 **
   2324 ** Returns          void
   2325 **
   2326 *******************************************************************************/
   2327 void bta_av_start_ok (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   2328 {
   2329     tBTA_AV_START   start;
   2330     tBTA_AV_API_STOP stop;
   2331     BOOLEAN         initiator = FALSE;
   2332     BOOLEAN         suspend = FALSE;
   2333     UINT16          flush_to;
   2334     UINT8           new_role = p_scb->role;
   2335     BT_HDR          hdr;
   2336     UINT8           policy = HCI_ENABLE_SNIFF_MODE;
   2337     UINT8           cur_role;
   2338 
   2339     APPL_TRACE_DEBUG("bta_av_start_ok wait:x%x, role:x%x", p_scb->wait, p_scb->role);
   2340 
   2341     p_scb->started = TRUE;
   2342     if (p_scb->sco_suspend)
   2343     {
   2344         p_scb->sco_suspend = FALSE;
   2345     }
   2346 
   2347     if (new_role & BTA_AV_ROLE_START_INT)
   2348         initiator = TRUE;
   2349 
   2350     /* for A2DP SINK we do not send get_caps */
   2351     if ((p_scb->avdt_handle == p_scb->seps[p_scb->sep_idx].av_handle)
   2352          &&(p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SNK))
   2353     {
   2354         p_scb->wait &= ~(BTA_AV_WAIT_ACP_CAPS_ON);
   2355         APPL_TRACE_DEBUG(" Local SEP type is SNK  new wait is 0x%x",p_scb->wait);
   2356     }
   2357     if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_FAILED)
   2358     {
   2359         /* role switch has failed */
   2360         p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_FAILED;
   2361         p_data = (tBTA_AV_DATA *)&hdr;
   2362         hdr.offset = BTA_AV_RS_FAIL;
   2363     }
   2364     APPL_TRACE_DEBUG("wait:x%x", p_scb->wait);
   2365 
   2366     if (p_data && (p_data->hdr.offset != BTA_AV_RS_NONE))
   2367     {
   2368         p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
   2369         if (p_data->hdr.offset == BTA_AV_RS_FAIL)
   2370         {
   2371             bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
   2372             start.chnl   = p_scb->chnl;
   2373             start.status = BTA_AV_FAIL_ROLE;
   2374             start.hndl   = p_scb->hndl;
   2375             start.initiator = initiator;
   2376             (*bta_av_cb.p_cback)(BTA_AV_START_EVT, (tBTA_AV *) &start);
   2377             return;
   2378         }
   2379     }
   2380 
   2381     if (!bta_av_link_role_ok(p_scb, A2D_SET_ONE_BIT))
   2382         p_scb->q_tag = BTA_AV_Q_TAG_START;
   2383     else
   2384     {
   2385         /* The wait flag may be set here while we are already master on the link */
   2386         /* this could happen if a role switch complete event occurred during reconfig */
   2387         /* if we are now master on the link, there is no need to wait for the role switch, */
   2388         /* complete anymore so we can clear the wait for role switch flag */
   2389         p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
   2390     }
   2391 
   2392     if (p_scb->wait & (BTA_AV_WAIT_ROLE_SW_RES_OPEN|BTA_AV_WAIT_ROLE_SW_RES_START))
   2393     {
   2394         p_scb->wait |= BTA_AV_WAIT_ROLE_SW_STARTED;
   2395         p_scb->q_tag = BTA_AV_Q_TAG_START;
   2396     }
   2397 
   2398     if (p_scb->wait)
   2399     {
   2400         APPL_TRACE_ERROR("wait:x%x q_tag:%d- not started", p_scb->wait, p_scb->q_tag);
   2401         /* Clear first bit of p_scb->wait and not to return from this point else
   2402          * HAL layer gets blocked. And if there is delay in Get Capability response as
   2403          * first bit of p_scb->wait is cleared hence it ensures bt_av_start_ok is not called
   2404          * again from bta_av_save_caps.
   2405         */
   2406         p_scb->wait &= ~BTA_AV_WAIT_ACP_CAPS_ON;
   2407     }
   2408 
   2409     /* tell role manager to check M/S role */
   2410     bta_sys_conn_open(BTA_ID_AV, p_scb->app_id, p_scb->peer_addr);
   2411 
   2412     bta_sys_busy(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
   2413 
   2414     if(p_scb->media_type == AVDT_MEDIA_AUDIO)
   2415     {
   2416         /* in normal logic, conns should be bta_av_cb.audio_count - 1,
   2417          * However, bta_av_stream_chg is not called to increase bta_av_cb.audio_count yet.
   2418          * If the code were to be re-arranged for some reasons, this number may need to be changed
   2419          */
   2420         p_scb->co_started = bta_av_cb.audio_open_cnt;
   2421         flush_to = p_bta_av_cfg->p_audio_flush_to[p_scb->co_started - 1];
   2422     }
   2423     else
   2424     {
   2425         flush_to = p_bta_av_cfg->video_flush_to;
   2426     }
   2427     L2CA_SetFlushTimeout(p_scb->peer_addr, flush_to );
   2428 
   2429     /* clear the congestion flag */
   2430     p_scb->cong = FALSE;
   2431 
   2432     if (new_role & BTA_AV_ROLE_START_INT)
   2433     {
   2434         new_role &= ~BTA_AV_ROLE_START_INT;
   2435     }
   2436     else if ((new_role & BTA_AV_ROLE_AD_ACP) && (new_role & BTA_AV_ROLE_SUSPEND_OPT))
   2437     {
   2438         suspend = TRUE;
   2439     }
   2440 
   2441     if (!suspend)
   2442     {
   2443         p_scb->q_tag = BTA_AV_Q_TAG_STREAM;
   2444         bta_av_stream_chg(p_scb, TRUE);
   2445     }
   2446 
   2447     {
   2448         /* If sink starts stream, disable sniff mode here */
   2449         if (!initiator)
   2450         {
   2451              /* If souce is the master role, disable role switch during streaming.
   2452              * Otherwise allow role switch, if source is slave.
   2453              * Because it would not hurt source, if the peer device wants source to be master */
   2454             if ((BTM_GetRole (p_scb->peer_addr, &cur_role) == BTM_SUCCESS) &&
   2455                 (cur_role == BTM_ROLE_MASTER) )
   2456             {
   2457                 policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
   2458             }
   2459 
   2460             bta_sys_clear_policy(BTA_ID_AV, policy, p_scb->peer_addr);
   2461         }
   2462 
   2463         p_scb->role = new_role;
   2464         p_scb->role &= ~BTA_AV_ROLE_AD_ACP;
   2465         p_scb->role &= ~BTA_AV_ROLE_SUSPEND_OPT;
   2466 
   2467         p_scb->no_rtp_hdr = FALSE;
   2468         p_scb->p_cos->start(p_scb->hndl, p_scb->codec_type, p_scb->cfg.codec_info, &p_scb->no_rtp_hdr);
   2469         p_scb->co_started = TRUE;
   2470 
   2471         APPL_TRACE_DEBUG("bta_av_start_ok suspending: %d, role:x%x, init %d",
   2472             suspend, p_scb->role, initiator);
   2473 
   2474         start.suspending = suspend;
   2475         start.initiator = initiator;
   2476         start.chnl   = p_scb->chnl;
   2477         start.status = BTA_AV_SUCCESS;
   2478         start.hndl   = p_scb->hndl;
   2479         (*bta_av_cb.p_cback)(BTA_AV_START_EVT, (tBTA_AV *) &start);
   2480 
   2481         if(suspend)
   2482         {
   2483             p_scb->role |= BTA_AV_ROLE_SUSPEND;
   2484             p_scb->cong = TRUE;  /* do not allow the media data to go through */
   2485             /* do not duplicate the media packets to this channel */
   2486             p_scb->p_cos->stop(p_scb->hndl, p_scb->codec_type);
   2487             p_scb->co_started = FALSE;
   2488             stop.flush   = FALSE;
   2489             stop.suspend = TRUE;
   2490             bta_av_ssm_execute(p_scb, BTA_AV_AP_STOP_EVT, (tBTA_AV_DATA *)&stop);
   2491         }
   2492     }
   2493 }
   2494 
   2495 /*******************************************************************************
   2496 **
   2497 ** Function         bta_av_start_failed
   2498 **
   2499 ** Description      Stream start failed.
   2500 **
   2501 ** Returns          void
   2502 **
   2503 *******************************************************************************/
   2504 void bta_av_start_failed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   2505 {
   2506     UNUSED(p_data);
   2507 
   2508     if(p_scb->started == FALSE && p_scb->co_started == FALSE)
   2509     {
   2510         bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
   2511         notify_start_failed(p_scb);
   2512     }
   2513 
   2514     bta_sys_set_policy(BTA_ID_AV, (HCI_ENABLE_SNIFF_MODE|HCI_ENABLE_MASTER_SLAVE_SWITCH), p_scb->peer_addr);
   2515     p_scb->sco_suspend = FALSE;
   2516 }
   2517 
   2518 /*******************************************************************************
   2519 **
   2520 ** Function         bta_av_str_closed
   2521 **
   2522 ** Description      Stream closed.
   2523 **
   2524 ** Returns          void
   2525 **
   2526 *******************************************************************************/
   2527 void bta_av_str_closed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   2528 {
   2529     tBTA_AV     data;
   2530     tBTA_AV_EVT event;
   2531     UINT16      mtu;
   2532     UINT8 policy = HCI_ENABLE_SNIFF_MODE;
   2533 
   2534     if ((bta_av_cb.features & BTA_AV_FEAT_MASTER) == 0 || bta_av_cb.audio_open_cnt == 1)
   2535         policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
   2536     bta_sys_set_policy(BTA_ID_AV, policy, p_scb->peer_addr);
   2537     if (bta_av_cb.audio_open_cnt <= 1)
   2538     {
   2539         /* last connection - restore the allow switch flag */
   2540         L2CA_SetDesireRole(L2CAP_ROLE_ALLOW_SWITCH);
   2541     }
   2542 
   2543     if (p_scb->open_status)
   2544     {
   2545         /* must be failure when opening the stream */
   2546         bdcpy(data.open.bd_addr, p_scb->peer_addr);
   2547         data.open.status = p_scb->open_status;
   2548         data.open.chnl   = p_scb->chnl;
   2549         data.open.hndl   = p_scb->hndl;
   2550 
   2551         if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SRC )
   2552             data.open.sep = AVDT_TSEP_SNK;
   2553         else if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SNK )
   2554             data.open.sep = AVDT_TSEP_SRC;
   2555 
   2556         event = BTA_AV_OPEN_EVT;
   2557         p_scb->open_status = BTA_AV_SUCCESS;
   2558 
   2559         bta_sys_conn_close(BTA_ID_AV, p_scb->app_id, p_scb->peer_addr);
   2560         bta_av_cleanup(p_scb, p_data);
   2561         (*bta_av_cb.p_cback)(event, &data);
   2562     }
   2563     else
   2564     {
   2565         /* do stop if we were started */
   2566         if (p_scb->co_started)
   2567         {
   2568             bta_av_str_stopped(p_scb, NULL);
   2569         }
   2570 
   2571         /* Update common mtu shared by remaining connectons */
   2572         mtu = bta_av_chk_mtu(p_scb, BTA_AV_MAX_A2DP_MTU);
   2573 
   2574         {
   2575             p_scb->p_cos->close(p_scb->hndl, p_scb->codec_type, mtu);
   2576             data.close.chnl = p_scb->chnl;
   2577             data.close.hndl = p_scb->hndl;
   2578             event = BTA_AV_CLOSE_EVT;
   2579 
   2580             bta_sys_conn_close(BTA_ID_AV, p_scb->app_id, p_scb->peer_addr);
   2581             bta_av_cleanup(p_scb, p_data);
   2582             (*bta_av_cb.p_cback)(event, &data);
   2583         }
   2584     }
   2585 }
   2586 
   2587 /*******************************************************************************
   2588 **
   2589 ** Function         bta_av_clr_cong
   2590 **
   2591 ** Description      Clear stream congestion flag.
   2592 **
   2593 ** Returns          void
   2594 **
   2595 *******************************************************************************/
   2596 void bta_av_clr_cong (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   2597 {
   2598     UNUSED(p_data);
   2599 
   2600     if(p_scb->co_started)
   2601         p_scb->cong = FALSE;
   2602 }
   2603 
   2604 /*******************************************************************************
   2605 **
   2606 ** Function         bta_av_suspend_cfm
   2607 **
   2608 ** Description      process the suspend response
   2609 **
   2610 ** Returns          void
   2611 **
   2612 *******************************************************************************/
   2613 void bta_av_suspend_cfm (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   2614 {
   2615     tBTA_AV_SUSPEND suspend_rsp;
   2616     UINT8           err_code = p_data->str_msg.msg.hdr.err_code;
   2617     UINT8 policy = HCI_ENABLE_SNIFF_MODE;
   2618 
   2619     APPL_TRACE_DEBUG ("bta_av_suspend_cfm:audio_open_cnt = %d, err_code = %d",
   2620         bta_av_cb.audio_open_cnt, err_code);
   2621 
   2622     if (p_scb->started == FALSE)
   2623     {
   2624         /* handle the condition where there is a collision of SUSPEND req from either side
   2625         ** Second SUSPEND req could be rejected. Do not treat this as a failure
   2626         */
   2627         APPL_TRACE_WARNING("bta_av_suspend_cfm: already suspended, ignore, err_code %d",
   2628                             err_code);
   2629         return;
   2630     }
   2631 
   2632     suspend_rsp.status = BTA_AV_SUCCESS;
   2633     if (err_code && (err_code != AVDT_ERR_BAD_STATE))
   2634     {
   2635          /* Disable suspend feature only with explicit rejection(not with timeout) */
   2636         if (err_code != AVDT_ERR_TIMEOUT)
   2637         {
   2638             p_scb->suspend_sup = FALSE;
   2639         }
   2640         suspend_rsp.status = BTA_AV_FAIL;
   2641 
   2642         APPL_TRACE_ERROR ("bta_av_suspend_cfm: suspend failed, closing connection");
   2643 
   2644         /* SUSPEND failed. Close connection. */
   2645         bta_av_ssm_execute(p_scb, BTA_AV_API_CLOSE_EVT, NULL);
   2646     }
   2647     else
   2648     {
   2649         /* only set started to FALSE when suspend is successful */
   2650         p_scb->started = FALSE;
   2651     }
   2652 
   2653     if (p_scb->role & BTA_AV_ROLE_SUSPEND)
   2654     {
   2655         p_scb->role &= ~BTA_AV_ROLE_SUSPEND;
   2656         p_scb->cong = FALSE;
   2657     }
   2658 
   2659     bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
   2660     if ((bta_av_cb.features & BTA_AV_FEAT_MASTER) == 0 || bta_av_cb.audio_open_cnt == 1)
   2661         policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
   2662     bta_sys_set_policy(BTA_ID_AV, policy, p_scb->peer_addr);
   2663 
   2664     /* in case that we received suspend_ind, we may need to call co_stop here */
   2665     if(p_scb->co_started)
   2666     {
   2667         vendor_get_interface()->send_command(
   2668             (vendor_opcode_t)BT_VND_OP_A2DP_OFFLOAD_STOP,
   2669             (void*)&p_scb->l2c_cid);
   2670         if (p_scb->offload_start_pending) {
   2671             tBTA_AV_STATUS status = BTA_AV_FAIL_STREAM;
   2672             (*bta_av_cb.p_cback)(BTA_AV_OFFLOAD_START_RSP_EVT, (tBTA_AV *)&status);
   2673         }
   2674         p_scb->offload_start_pending = FALSE;
   2675 
   2676         bta_av_stream_chg(p_scb, FALSE);
   2677 
   2678         {
   2679             p_scb->co_started = FALSE;
   2680             p_scb->p_cos->stop(p_scb->hndl, p_scb->codec_type);
   2681         }
   2682         L2CA_SetFlushTimeout(p_scb->peer_addr, L2CAP_DEFAULT_FLUSH_TO);
   2683     }
   2684 
   2685     {
   2686         suspend_rsp.chnl = p_scb->chnl;
   2687         suspend_rsp.hndl = p_scb->hndl;
   2688         suspend_rsp.initiator = p_data->str_msg.initiator;
   2689         (*bta_av_cb.p_cback)(BTA_AV_SUSPEND_EVT, (tBTA_AV *) &suspend_rsp);
   2690     }
   2691 }
   2692 
   2693 /*******************************************************************************
   2694 **
   2695 ** Function         bta_av_rcfg_str_ok
   2696 **
   2697 ** Description      report reconfigure successful
   2698 **
   2699 ** Returns          void
   2700 **
   2701 *******************************************************************************/
   2702 void bta_av_rcfg_str_ok (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   2703 {
   2704     tBTA_AV_RECONFIG    evt;
   2705     UNUSED(p_data);
   2706 
   2707     p_scb->l2c_cid      = AVDT_GetL2CapChannel(p_scb->avdt_handle);
   2708     APPL_TRACE_DEBUG("bta_av_rcfg_str_ok: l2c_cid: %d", p_scb->l2c_cid);
   2709 
   2710     /* rc listen */
   2711     bta_av_st_rc_timer(p_scb, NULL);
   2712     osi_free_and_reset((void **)&p_scb->p_cap);
   2713 
   2714     /* No need to keep the role bits once reconfig is done. */
   2715     p_scb->role &= ~BTA_AV_ROLE_AD_ACP;
   2716     p_scb->role &= ~BTA_AV_ROLE_SUSPEND_OPT;
   2717     p_scb->role &= ~BTA_AV_ROLE_START_INT;
   2718 
   2719     {
   2720         /* reconfigure success  */
   2721         evt.status = BTA_AV_SUCCESS;
   2722         evt.chnl   = p_scb->chnl;
   2723         evt.hndl   = p_scb->hndl;
   2724         (*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, (tBTA_AV *)&evt);
   2725     }
   2726 }
   2727 
   2728 /*******************************************************************************
   2729 **
   2730 ** Function         bta_av_rcfg_failed
   2731 **
   2732 ** Description      process reconfigure failed
   2733 **
   2734 ** Returns          void
   2735 **
   2736 *******************************************************************************/
   2737 void bta_av_rcfg_failed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   2738 {
   2739     tBTA_AV_RECONFIG evt;
   2740 
   2741     APPL_TRACE_DEBUG("bta_av_rcfg_failed num_recfg: %d, conn_lcb:0x%x",
   2742         p_scb->num_recfg, bta_av_cb.conn_lcb);
   2743     if(p_scb->num_recfg > BTA_AV_RECONFIG_RETRY)
   2744     {
   2745         bta_av_cco_close(p_scb, p_data);
   2746         /* report failure */
   2747         evt.status = BTA_AV_FAIL_STREAM;
   2748         evt.chnl   = p_scb->chnl;
   2749         evt.hndl   = p_scb->hndl;
   2750         (*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, (tBTA_AV *)&evt);
   2751         /* go to closing state */
   2752         bta_av_ssm_execute(p_scb, BTA_AV_API_CLOSE_EVT, NULL);
   2753     }
   2754     else
   2755     {
   2756         /* open failed. try again */
   2757         p_scb->num_recfg++;
   2758         if(bta_av_cb.conn_lcb)
   2759         {
   2760             AVDT_DisconnectReq(p_scb->peer_addr, bta_av_dt_cback[p_scb->hdi]);
   2761         }
   2762         else
   2763         {
   2764             bta_av_connect_req(p_scb, NULL);
   2765         }
   2766     }
   2767 }
   2768 
   2769 /*******************************************************************************
   2770 **
   2771 ** Function         bta_av_rcfg_connect
   2772 **
   2773 ** Description      stream closed. reconnect the stream
   2774 **
   2775 ** Returns          void
   2776 **
   2777 *******************************************************************************/
   2778 void bta_av_rcfg_connect (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   2779 {
   2780     UNUSED(p_data);
   2781 
   2782     p_scb->cong    = FALSE;
   2783     p_scb->num_recfg++;
   2784     APPL_TRACE_DEBUG("bta_av_rcfg_connect num_recfg: %d", p_scb->num_recfg);
   2785     if(p_scb->num_recfg > BTA_AV_RECONFIG_RETRY)
   2786     {
   2787         /* let bta_av_rcfg_failed report fail */
   2788         bta_av_rcfg_failed(p_scb, NULL);
   2789     }
   2790     else
   2791         AVDT_ConnectReq(p_scb->peer_addr, p_scb->sec_mask, bta_av_dt_cback[p_scb->hdi]);
   2792 }
   2793 
   2794 /*******************************************************************************
   2795 **
   2796 ** Function         bta_av_rcfg_discntd
   2797 **
   2798 ** Description      AVDT disconnected. reconnect the stream
   2799 **
   2800 ** Returns          void
   2801 **
   2802 *******************************************************************************/
   2803 void bta_av_rcfg_discntd (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   2804 {
   2805     tBTA_AV_RECONFIG    evt;
   2806     UNUSED(p_data);
   2807 
   2808     APPL_TRACE_DEBUG("bta_av_rcfg_discntd num_recfg: %d", p_scb->num_recfg);
   2809     p_scb->num_recfg++;
   2810     if(p_scb->num_recfg > BTA_AV_RECONFIG_RETRY)
   2811     {
   2812         /* report failure */
   2813         evt.status = BTA_AV_FAIL_STREAM;
   2814         evt.chnl   = p_scb->chnl;
   2815         evt.hndl   = p_scb->hndl;
   2816         (*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, (tBTA_AV *)&evt);
   2817         /* report close event & go to init state */
   2818         bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, NULL);
   2819     }
   2820     else
   2821         AVDT_ConnectReq(p_scb->peer_addr, p_scb->sec_mask, bta_av_dt_cback[p_scb->hdi]);
   2822 }
   2823 
   2824 /*******************************************************************************
   2825 **
   2826 ** Function         bta_av_suspend_cont
   2827 **
   2828 ** Description      received the suspend response.
   2829 **                  continue to reconfigure the stream
   2830 **
   2831 ** Returns          void
   2832 **
   2833 *******************************************************************************/
   2834 void bta_av_suspend_cont (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   2835 {
   2836     UINT8       err_code = p_data->str_msg.msg.hdr.err_code;
   2837     tBTA_AV_RECONFIG    evt;
   2838 
   2839     p_scb->started = FALSE;
   2840     p_scb->cong    = FALSE;
   2841     if (err_code)
   2842     {
   2843         if (AVDT_ERR_CONNECT == err_code)
   2844         {
   2845             /* report failure */
   2846             evt.status = BTA_AV_FAIL;
   2847             (*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, (tBTA_AV *)&evt);
   2848             bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, NULL);
   2849         }
   2850         else
   2851         {
   2852             APPL_TRACE_ERROR("suspend rejected, try close");
   2853              /* Disable suspend feature only with explicit rejection(not with timeout) */
   2854             if (err_code != AVDT_ERR_TIMEOUT)
   2855             {
   2856                 p_scb->suspend_sup = FALSE;
   2857             }
   2858             /* drop the buffers queued in L2CAP */
   2859             L2CA_FlushChannel (p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
   2860 
   2861             AVDT_CloseReq(p_scb->avdt_handle);
   2862         }
   2863     }
   2864     else
   2865     {
   2866         APPL_TRACE_DEBUG("bta_av_suspend_cont calling AVDT_ReconfigReq");
   2867         /* reconfig the stream */
   2868 
   2869         AVDT_ReconfigReq(p_scb->avdt_handle, p_scb->p_cap);
   2870         p_scb->p_cap->psc_mask = p_scb->cur_psc_mask;
   2871     }
   2872 }
   2873 
   2874 /*******************************************************************************
   2875 **
   2876 ** Function         bta_av_rcfg_cfm
   2877 **
   2878 ** Description      if reconfigure is successful, report the event
   2879 **                  otherwise, close the stream.
   2880 **
   2881 ** Returns          void
   2882 **
   2883 *******************************************************************************/
   2884 void bta_av_rcfg_cfm (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   2885 {
   2886     UINT8   err_code = p_data->str_msg.msg.hdr.err_code;
   2887 
   2888     /*
   2889     APPL_TRACE_DEBUG("bta_av_rcfg_cfm");
   2890     */
   2891     if (err_code)
   2892     {
   2893         APPL_TRACE_ERROR("reconfig rejected, try close");
   2894          /* Disable reconfiguration feature only with explicit rejection(not with timeout) */
   2895         if (err_code != AVDT_ERR_TIMEOUT)
   2896         {
   2897             p_scb->recfg_sup = FALSE;
   2898         }
   2899         /* started flag is FALSE when reconfigure command is sent */
   2900         /* drop the buffers queued in L2CAP */
   2901         L2CA_FlushChannel (p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
   2902         AVDT_CloseReq(p_scb->avdt_handle);
   2903     }
   2904     else
   2905     {
   2906         /* update the codec info after rcfg cfm */
   2907         memcpy(p_scb->cfg.codec_info,p_data->str_msg.msg.reconfig_cfm.p_cfg->codec_info,AVDT_CODEC_SIZE);
   2908         /* take the SSM back to OPEN state */
   2909         bta_av_ssm_execute(p_scb, BTA_AV_STR_OPEN_OK_EVT, NULL);
   2910     }
   2911 }
   2912 
   2913 /*******************************************************************************
   2914 **
   2915 ** Function         bta_av_rcfg_open
   2916 **
   2917 ** Description      AVDT is connected. open the stream with the new configuration
   2918 **
   2919 ** Returns          void
   2920 **
   2921 *******************************************************************************/
   2922 void bta_av_rcfg_open (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   2923 {
   2924     UNUSED(p_data);
   2925 
   2926 	APPL_TRACE_DEBUG("bta_av_rcfg_open, num_disc_snks = %d", p_scb->num_disc_snks);
   2927 
   2928     if (p_scb->num_disc_snks == 0)
   2929     {
   2930         /* Need to update call-out module so that it will be ready for discover */
   2931         p_scb->p_cos->stop(p_scb->hndl, p_scb->codec_type);
   2932 
   2933         /* send avdtp discover request */
   2934         AVDT_DiscoverReq(p_scb->peer_addr, p_scb->sep_info, BTA_AV_NUM_SEPS, bta_av_dt_cback[p_scb->hdi]);
   2935     }
   2936     else
   2937     {
   2938         p_scb->codec_type = p_scb->p_cap->codec_info[BTA_AV_CODEC_TYPE_IDX];
   2939         memcpy(p_scb->cfg.codec_info, p_scb->p_cap->codec_info, AVDT_CODEC_SIZE);
   2940         /* we may choose to use a different SEP at reconfig.
   2941          * adjust the sep_idx now */
   2942         bta_av_adjust_seps_idx(p_scb, bta_av_get_scb_handle(p_scb, AVDT_TSEP_SRC));
   2943 
   2944         /* open the stream with the new config */
   2945         p_scb->sep_info_idx = p_scb->rcfg_idx;
   2946         AVDT_OpenReq(p_scb->avdt_handle, p_scb->peer_addr,
   2947                      p_scb->sep_info[p_scb->sep_info_idx].seid, p_scb->p_cap);
   2948     }
   2949 
   2950 }
   2951 
   2952 /*******************************************************************************
   2953 **
   2954 ** Function         bta_av_security_rej
   2955 **
   2956 ** Description      Send an AVDTP security reject.
   2957 **
   2958 ** Returns          void
   2959 **
   2960 *******************************************************************************/
   2961 void bta_av_security_rej (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   2962 {
   2963     UNUSED(p_data);
   2964 
   2965     AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label, AVDT_ERR_BAD_STATE,
   2966                      NULL, 0);
   2967 }
   2968 
   2969 /*******************************************************************************
   2970 **
   2971 ** Function         bta_av_chk_2nd_start
   2972 **
   2973 ** Description      check if this is 2nd stream and if it needs to be started.
   2974 **                  This function needs to be kept very similar to bta_av_chk_start
   2975 **
   2976 ** Returns          void
   2977 **
   2978 *******************************************************************************/
   2979 void bta_av_chk_2nd_start (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   2980 {
   2981     tBTA_AV_SCB *p_scbi;
   2982     int i;
   2983     BOOLEAN new_started = FALSE;
   2984     UNUSED(p_data);
   2985 
   2986     if ((p_scb->chnl == BTA_AV_CHNL_AUDIO) && (bta_av_cb.audio_open_cnt >= 2))
   2987     {
   2988         /* more than one audio channel is connected */
   2989         if (!(p_scb->role & BTA_AV_ROLE_SUSPEND_OPT))
   2990         {
   2991             /* this channel does not need to be reconfigured.
   2992              * if there is other channel streaming, start the stream now */
   2993             for(i=0; i<BTA_AV_NUM_STRS; i++)
   2994             {
   2995                 p_scbi = bta_av_cb.p_scb[i];
   2996                 if(p_scbi && p_scbi->chnl == BTA_AV_CHNL_AUDIO && p_scbi->co_started)
   2997                 {
   2998                     if (!new_started)
   2999                     {
   3000                         /* start the new stream */
   3001                         new_started = TRUE;
   3002                         bta_av_ssm_execute(p_scb, BTA_AV_AP_START_EVT, NULL);
   3003                     }
   3004                     /* may need to update the flush timeout of this already started stream */
   3005                     if (p_scbi->co_started != bta_av_cb.audio_open_cnt)
   3006                     {
   3007                         p_scbi->co_started = bta_av_cb.audio_open_cnt;
   3008                         L2CA_SetFlushTimeout(p_scbi->peer_addr, p_bta_av_cfg->p_audio_flush_to[p_scbi->co_started - 1] );
   3009                     }
   3010                 }
   3011             }
   3012         }
   3013     }
   3014 }
   3015 
   3016 /*******************************************************************************
   3017 **
   3018 ** Function         bta_av_open_rc
   3019 **
   3020 ** Description      Send a message to main SM to open RC channel.
   3021 **
   3022 ** Returns          void
   3023 **
   3024 *******************************************************************************/
   3025 void bta_av_open_rc (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   3026 {
   3027     tBTA_AV_START   start;
   3028 
   3029     APPL_TRACE_DEBUG("bta_av_open_rc use_rc: %d, wait: x%x role:x%x", p_scb->use_rc, p_scb->wait, p_scb->role);
   3030     if ((p_scb->wait & BTA_AV_WAIT_ROLE_SW_BITS) && (p_scb->q_tag == BTA_AV_Q_TAG_START))
   3031     {
   3032         /* waiting for role switch for some reason & the timer expires */
   3033         if (!bta_av_link_role_ok(p_scb, A2D_SET_ONE_BIT))
   3034         {
   3035             APPL_TRACE_ERROR ("failed to start streaming for role management reasons!!");
   3036             alarm_cancel(p_scb->avrc_ct_timer);
   3037             start.chnl   = p_scb->chnl;
   3038             start.status = BTA_AV_FAIL_ROLE;
   3039             start.initiator = TRUE;
   3040             start.hndl   = p_scb->hndl;
   3041             p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
   3042             bta_av_cb.rs_idx = 0;
   3043             (*bta_av_cb.p_cback)(BTA_AV_START_EVT, (tBTA_AV *) &start);
   3044         }
   3045         else
   3046         {
   3047             /* role switch is done. continue to start streaming */
   3048             bta_av_cb.rs_idx = 0;
   3049             p_data->hdr.offset = BTA_AV_RS_OK;
   3050             bta_av_start_ok (p_scb, p_data);
   3051         }
   3052         return;
   3053     }
   3054 
   3055     if(p_scb->use_rc == TRUE || (p_scb->role & BTA_AV_ROLE_AD_ACP) )
   3056     {
   3057         if(bta_av_cb.disc)
   3058         {
   3059             /* AVRC discover db is in use */
   3060             if(p_scb->rc_handle == BTA_AV_RC_HANDLE_NONE)
   3061             {
   3062                 /* AVRC channel is not connected. delay a little bit */
   3063                 if ((p_scb->wait & BTA_AV_WAIT_ROLE_SW_BITS) == 0) {
   3064                     bta_sys_start_timer(p_scb->avrc_ct_timer,
   3065                                         BTA_AV_RC_DISC_TIME_VAL,
   3066                                         BTA_AV_AVRC_TIMER_EVT, p_scb->hndl);
   3067                 } else {
   3068                     p_scb->wait |= BTA_AV_WAIT_CHECK_RC;
   3069                 }
   3070             }
   3071         }
   3072         else
   3073         {
   3074             /* use main SM for AVRC SDP activities */
   3075             bta_av_rc_disc((UINT8)(p_scb->hdi + 1));
   3076         }
   3077     }
   3078     else
   3079     {
   3080         if(BTA_AV_RC_HANDLE_NONE != p_scb->rc_handle)
   3081         {
   3082             /* the open API said that this handle does not want a RC connection.
   3083              * disconnect it now */
   3084             AVRC_Close(p_scb->rc_handle);
   3085         }
   3086     }
   3087 }
   3088 
   3089 /*******************************************************************************
   3090 **
   3091 ** Function         bta_av_open_at_inc
   3092 **
   3093 ** Description      This function is called if API open is called by application
   3094 **                  while state-machine is at incoming state.
   3095 **
   3096 ** Returns          void
   3097 **
   3098 *******************************************************************************/
   3099 void bta_av_open_at_inc (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   3100 {
   3101     memcpy (&(p_scb->open_api), &(p_data->api_open), sizeof(tBTA_AV_API_OPEN));
   3102 
   3103     if (p_scb->coll_mask & BTA_AV_COLL_INC_TMR)
   3104     {
   3105         p_scb->coll_mask |= BTA_AV_COLL_API_CALLED;
   3106 
   3107         /* API open will be handled at timeout if SNK did not start signalling. */
   3108         /* API open will be ignored if SNK starts signalling.                   */
   3109     }
   3110     else
   3111     {
   3112         /* SNK did not start signalling, API was called N seconds timeout. */
   3113         /* We need to switch to INIT state and start opening connection. */
   3114         p_scb->coll_mask = 0;
   3115         bta_av_set_scb_sst_init(p_scb);
   3116 
   3117         tBTA_AV_API_OPEN *p_buf =
   3118             (tBTA_AV_API_OPEN *)osi_malloc(sizeof(tBTA_AV_API_OPEN));
   3119         memcpy(p_buf, &(p_scb->open_api), sizeof(tBTA_AV_API_OPEN));
   3120         p_scb->skip_sdp = TRUE;
   3121         bta_sys_sendmsg(p_buf);
   3122     }
   3123 }
   3124 
   3125 /*******************************************************************************
   3126 **
   3127 ** Function         bta_av_offload_req
   3128 **
   3129 ** Description      This function is called if application requests offload of
   3130 **                  a2dp audio.
   3131 **
   3132 ** Returns          void
   3133 **
   3134 *******************************************************************************/
   3135 void bta_av_offload_req(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   3136 {
   3137     tBTA_AV_STATUS status = BTA_AV_FAIL_RESOURCES;
   3138     UINT16 mtu = bta_av_chk_mtu(p_scb, p_scb->stream_mtu);
   3139 
   3140     APPL_TRACE_DEBUG("%s stream %s, audio channels open %d", __func__,
   3141                      p_scb->started ? "STARTED" : "STOPPED", bta_av_cb.audio_open_cnt);
   3142 
   3143     /* Check if stream has already been started. */
   3144     /* Support offload if only one audio source stream is open. */
   3145     if (p_scb->started != TRUE) {
   3146         status = BTA_AV_FAIL_STREAM;
   3147 
   3148     } else if (bta_av_cb.audio_open_cnt == 1 &&
   3149                p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SRC &&
   3150                p_scb->chnl == BTA_AV_CHNL_AUDIO) {
   3151 
   3152         bt_vendor_op_a2dp_offload_t a2dp_offload_start;
   3153 
   3154         if (L2CA_GetConnectionConfig(p_scb->l2c_cid, &a2dp_offload_start.acl_data_size,
   3155                                      &a2dp_offload_start.remote_cid, &a2dp_offload_start.lm_handle)) {
   3156 
   3157             APPL_TRACE_DEBUG("%s l2cmtu %d lcid 0x%02X rcid 0x%02X lm_handle 0x%02X", __func__,
   3158                              a2dp_offload_start.acl_data_size, p_scb->l2c_cid,
   3159                              a2dp_offload_start.remote_cid, a2dp_offload_start.lm_handle);
   3160 
   3161             a2dp_offload_start.bta_av_handle = p_scb->hndl;
   3162             a2dp_offload_start.xmit_quota = BTA_AV_A2DP_OFFLOAD_XMIT_QUOTA;
   3163             a2dp_offload_start.stream_mtu = (mtu < p_scb->stream_mtu) ? mtu : p_scb->stream_mtu;
   3164             a2dp_offload_start.local_cid = p_scb->l2c_cid;
   3165             a2dp_offload_start.is_flushable = TRUE;
   3166             a2dp_offload_start.stream_source = ((UINT32)(p_scb->cfg.codec_info[1] | p_scb->cfg.codec_info[2]));
   3167 
   3168             memcpy(a2dp_offload_start.codec_info, p_scb->cfg.codec_info,
   3169                    sizeof(a2dp_offload_start.codec_info));
   3170 
   3171             if (!vendor_get_interface()->send_command(
   3172                   (vendor_opcode_t)BT_VND_OP_A2DP_OFFLOAD_START,
   3173                   &a2dp_offload_start)) {
   3174                 status = BTA_AV_SUCCESS;
   3175                 p_scb->offload_start_pending = TRUE;
   3176             }
   3177         }
   3178     }
   3179 
   3180     if (status != BTA_AV_SUCCESS)
   3181         (*bta_av_cb.p_cback)(BTA_AV_OFFLOAD_START_RSP_EVT, (tBTA_AV *)&status);
   3182 }
   3183 
   3184 /*******************************************************************************
   3185 **
   3186 ** Function         bta_av_offload_rsp
   3187 **
   3188 ** Description      This function is called when the vendor lib responds to
   3189 **                  BT_VND_OP_A2DP_OFFLOAD_START.
   3190 **
   3191 ** Returns          void
   3192 **
   3193 *******************************************************************************/
   3194 void bta_av_offload_rsp(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
   3195 {
   3196     tBTA_AV_STATUS status = p_data->api_status_rsp.status;
   3197 
   3198     APPL_TRACE_DEBUG("%s stream %s status %s", __func__,
   3199                      p_scb->started ? "STARTED" : "STOPPED",
   3200                      status ? "FAIL" : "SUCCESS");
   3201 
   3202     /* Check if stream has already been started. */
   3203     if (status == BTA_AV_SUCCESS && p_scb->started != TRUE) {
   3204         status = BTA_AV_FAIL_STREAM;
   3205     }
   3206 
   3207     p_scb->offload_start_pending = FALSE;
   3208     (*bta_av_cb.p_cback)(BTA_AV_OFFLOAD_START_RSP_EVT, (tBTA_AV *)&status);
   3209 }
   3210 
   3211 #endif /* BTA_AV_INCLUDED */
   3212