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