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 is the main implementation file for the BTA advanced audio/video.
     22  *
     23  ******************************************************************************/
     24 
     25 #define LOG_TAG "bt_bta_av"
     26 
     27 #include <base/logging.h>
     28 #include <string.h>
     29 
     30 #include "bt_target.h"
     31 #include "osi/include/log.h"
     32 #include "osi/include/osi.h"
     33 #include "osi/include/properties.h"
     34 
     35 #include "bta_av_co.h"
     36 #include "bta_av_int.h"
     37 #include "btif/include/btif_av_co.h"
     38 #include "l2c_api.h"
     39 #include "l2cdefs.h"
     40 #include "utl.h"
     41 
     42 #if (BTA_AR_INCLUDED == TRUE)
     43 #include "bta_ar_api.h"
     44 #endif
     45 
     46 /*****************************************************************************
     47  * Constants and types
     48  ****************************************************************************/
     49 
     50 #ifndef BTA_AV_RET_TOUT
     51 #define BTA_AV_RET_TOUT 4
     52 #endif
     53 
     54 #ifndef BTA_AV_SIG_TOUT
     55 #define BTA_AV_SIG_TOUT 4
     56 #endif
     57 
     58 #ifndef BTA_AV_IDLE_TOUT
     59 #define BTA_AV_IDLE_TOUT 10
     60 #endif
     61 
     62 /* the delay time in milliseconds to retry role switch */
     63 #ifndef BTA_AV_RS_TIME_VAL
     64 #define BTA_AV_RS_TIME_VAL 1000
     65 #endif
     66 
     67 #ifndef AVRCP_VERSION_PROPERTY
     68 #define AVRCP_VERSION_PROPERTY "persist.bluetooth.avrcpversion"
     69 #endif
     70 
     71 #ifndef AVRCP_1_6_STRING
     72 #define AVRCP_1_6_STRING "avrcp16"
     73 #endif
     74 
     75 #ifndef AVRCP_1_5_STRING
     76 #define AVRCP_1_5_STRING "avrcp15"
     77 #endif
     78 
     79 #ifndef AVRCP_1_4_STRING
     80 #define AVRCP_1_4_STRING "avrcp14"
     81 #endif
     82 
     83 /* state machine states */
     84 enum { BTA_AV_INIT_ST, BTA_AV_OPEN_ST };
     85 
     86 /* state machine action enumeration list */
     87 enum {
     88   BTA_AV_DISABLE,
     89   BTA_AV_RC_OPENED,
     90   BTA_AV_RC_REMOTE_CMD,
     91   BTA_AV_RC_VENDOR_CMD,
     92   BTA_AV_RC_VENDOR_RSP,
     93   BTA_AV_RC_FREE_RSP,
     94   BTA_AV_RC_FREE_BROWSE_MSG,
     95   BTA_AV_RC_META_RSP,
     96   BTA_AV_RC_MSG,
     97   BTA_AV_RC_CLOSE,
     98   BTA_AV_RC_BROWSE_CLOSE,
     99   BTA_AV_NUM_ACTIONS
    100 };
    101 
    102 #define BTA_AV_IGNORE BTA_AV_NUM_ACTIONS
    103 
    104 /* type for action functions */
    105 typedef void (*tBTA_AV_ACTION)(tBTA_AV_CB* p_cb, tBTA_AV_DATA* p_data);
    106 
    107 /* action functions */
    108 const tBTA_AV_ACTION bta_av_action[] = {
    109     bta_av_disable,
    110     bta_av_rc_opened,
    111     bta_av_rc_remote_cmd,
    112     bta_av_rc_vendor_cmd,
    113     bta_av_rc_vendor_rsp,
    114     bta_av_rc_free_rsp,
    115     bta_av_rc_free_browse_msg,
    116     bta_av_rc_meta_rsp,
    117     bta_av_rc_msg,
    118     bta_av_rc_close,
    119 };
    120 
    121 /* state table information */
    122 #define BTA_AV_ACTION_COL 0 /* position of actions */
    123 #define BTA_AV_NEXT_STATE 1 /* position of next state */
    124 #define BTA_AV_NUM_COLS 2   /* number of columns in state tables */
    125 
    126 /* state table for init state */
    127 static const uint8_t bta_av_st_init[][BTA_AV_NUM_COLS] = {
    128     /* Event                     Action 1                   Next state */
    129     /* API_DISABLE_EVT */ {BTA_AV_DISABLE, BTA_AV_INIT_ST},
    130     /* API_REMOTE_CMD_EVT */ {BTA_AV_IGNORE, BTA_AV_INIT_ST},
    131     /* API_VENDOR_CMD_EVT */ {BTA_AV_IGNORE, BTA_AV_INIT_ST},
    132     /* API_VENDOR_RSP_EVT */ {BTA_AV_IGNORE, BTA_AV_INIT_ST},
    133     /* API_META_RSP_EVT */ {BTA_AV_RC_FREE_RSP, BTA_AV_INIT_ST},
    134     /* API_RC_CLOSE_EVT */ {BTA_AV_IGNORE, BTA_AV_INIT_ST},
    135     /* AVRC_OPEN_EVT */ {BTA_AV_RC_OPENED, BTA_AV_OPEN_ST},
    136     /* AVRC_MSG_EVT */ {BTA_AV_RC_FREE_BROWSE_MSG, BTA_AV_INIT_ST},
    137     /* AVRC_NONE_EVT */ {BTA_AV_IGNORE, BTA_AV_INIT_ST},
    138 };
    139 
    140 /* state table for open state */
    141 static const uint8_t bta_av_st_open[][BTA_AV_NUM_COLS] = {
    142     /* Event                     Action 1                   Next state */
    143     /* API_DISABLE_EVT */ {BTA_AV_DISABLE, BTA_AV_INIT_ST},
    144     /* API_REMOTE_CMD_EVT */ {BTA_AV_RC_REMOTE_CMD, BTA_AV_OPEN_ST},
    145     /* API_VENDOR_CMD_EVT */ {BTA_AV_RC_VENDOR_CMD, BTA_AV_OPEN_ST},
    146     /* API_VENDOR_RSP_EVT */ {BTA_AV_RC_VENDOR_RSP, BTA_AV_OPEN_ST},
    147     /* API_META_RSP_EVT */ {BTA_AV_RC_META_RSP, BTA_AV_OPEN_ST},
    148     /* API_RC_CLOSE_EVT */ {BTA_AV_RC_CLOSE, BTA_AV_OPEN_ST},
    149     /* AVRC_OPEN_EVT */ {BTA_AV_RC_OPENED, BTA_AV_OPEN_ST},
    150     /* AVRC_MSG_EVT */ {BTA_AV_RC_MSG, BTA_AV_OPEN_ST},
    151     /* AVRC_NONE_EVT */ {BTA_AV_IGNORE, BTA_AV_INIT_ST},
    152 };
    153 
    154 /* type for state table */
    155 typedef const uint8_t (*tBTA_AV_ST_TBL)[BTA_AV_NUM_COLS];
    156 
    157 /* state table */
    158 static const tBTA_AV_ST_TBL bta_av_st_tbl[] = {bta_av_st_init, bta_av_st_open};
    159 
    160 typedef void (*tBTA_AV_NSM_ACT)(tBTA_AV_DATA* p_data);
    161 static void bta_av_api_enable(tBTA_AV_DATA* p_data);
    162 static void bta_av_api_register(tBTA_AV_DATA* p_data);
    163 static void bta_av_ci_data(tBTA_AV_DATA* p_data);
    164 #if (AVDT_REPORTING == TRUE)
    165 static void bta_av_rpc_conn(tBTA_AV_DATA* p_data);
    166 #endif
    167 static void bta_av_api_to_ssm(tBTA_AV_DATA* p_data);
    168 
    169 static void bta_av_sco_chg_cback(tBTA_SYS_CONN_STATUS status, uint8_t id,
    170                                  uint8_t app_id, BD_ADDR peer_addr);
    171 static void bta_av_sys_rs_cback(tBTA_SYS_CONN_STATUS status, uint8_t id,
    172                                 uint8_t app_id, BD_ADDR peer_addr);
    173 
    174 /* action functions */
    175 const tBTA_AV_NSM_ACT bta_av_nsm_act[] = {
    176     bta_av_api_enable,       /* BTA_AV_API_ENABLE_EVT */
    177     bta_av_api_register,     /* BTA_AV_API_REGISTER_EVT */
    178     bta_av_api_deregister,   /* BTA_AV_API_DEREGISTER_EVT */
    179     bta_av_api_disconnect,   /* BTA_AV_API_DISCONNECT_EVT */
    180     bta_av_ci_data,          /* BTA_AV_CI_SRC_DATA_READY_EVT */
    181     bta_av_sig_chg,          /* BTA_AV_SIG_CHG_EVT */
    182     bta_av_signalling_timer, /* BTA_AV_SIGNALLING_TIMER_EVT */
    183     bta_av_rc_disc_done,     /* BTA_AV_SDP_AVRC_DISC_EVT */
    184     bta_av_rc_closed,        /* BTA_AV_AVRC_CLOSE_EVT */
    185     bta_av_rc_browse_opened, /* BTA_AV_AVRC_BROWSE_OPEN_EVT */
    186     bta_av_rc_browse_closed, /* BTA_AV_AVRC_BROWSE_CLOSE_EVT */
    187     bta_av_conn_chg,         /* BTA_AV_CONN_CHG_EVT */
    188     bta_av_dereg_comp,       /* BTA_AV_DEREG_COMP_EVT */
    189 #if (AVDT_REPORTING == TRUE)
    190     bta_av_rpc_conn, /* BTA_AV_AVDT_RPT_CONN_EVT */
    191 #endif
    192     bta_av_api_to_ssm, /* BTA_AV_API_START_EVT */
    193     bta_av_api_to_ssm, /* BTA_AV_API_STOP_EVT */
    194 };
    195 
    196 /*****************************************************************************
    197  * Global data
    198  ****************************************************************************/
    199 
    200 /* AV control block */
    201 tBTA_AV_CB bta_av_cb;
    202 
    203 static const char* bta_av_st_code(uint8_t state);
    204 
    205 /*******************************************************************************
    206  *
    207  * Function         bta_av_api_enable
    208  *
    209  * Description      Handle an API enable event.
    210  *
    211  *
    212  * Returns          void
    213  *
    214  ******************************************************************************/
    215 static void bta_av_api_enable(tBTA_AV_DATA* p_data) {
    216   /* initialize control block */
    217   memset(&bta_av_cb, 0, sizeof(tBTA_AV_CB));
    218 
    219   for (int i = 0; i < BTA_AV_NUM_RCB; i++)
    220     bta_av_cb.rcb[i].handle = BTA_AV_RC_HANDLE_NONE;
    221 
    222   bta_av_cb.rc_acp_handle = BTA_AV_RC_HANDLE_NONE;
    223 
    224   /*
    225    * TODO: The "disable" event handling is missing - there we need
    226    * to alarm_free() the alarms below.
    227    */
    228   bta_av_cb.link_signalling_timer = alarm_new("bta_av.link_signalling_timer");
    229   bta_av_cb.accept_signalling_timer =
    230       alarm_new("bta_av.accept_signalling_timer");
    231 
    232   /* store parameters */
    233   bta_av_cb.p_cback = p_data->api_enable.p_cback;
    234   bta_av_cb.features = p_data->api_enable.features;
    235   bta_av_cb.sec_mask = p_data->api_enable.sec_mask;
    236 
    237   tBTA_AV_ENABLE enable;
    238   enable.features = bta_av_cb.features;
    239 
    240   /* Register for SCO change event */
    241   if (!(bta_av_cb.features & BTA_AV_FEAT_NO_SCO_SSPD)) {
    242     bta_sys_sco_register(bta_av_sco_chg_cback);
    243   }
    244 
    245   /* call callback with enable event */
    246   (*bta_av_cb.p_cback)(BTA_AV_ENABLE_EVT, (tBTA_AV*)&enable);
    247 }
    248 
    249 /*******************************************************************************
    250  *
    251  * Function         bta_av_addr_to_scb
    252  *
    253  * Description      find the stream control block by the peer addr
    254  *
    255  * Returns          void
    256  *
    257  ******************************************************************************/
    258 static tBTA_AV_SCB* bta_av_addr_to_scb(BD_ADDR bd_addr) {
    259   tBTA_AV_SCB* p_scb = NULL;
    260   int xx;
    261 
    262   for (xx = 0; xx < BTA_AV_NUM_STRS; xx++) {
    263     if (bta_av_cb.p_scb[xx]) {
    264       if (!bdcmp(bd_addr, bta_av_cb.p_scb[xx]->peer_addr)) {
    265         p_scb = bta_av_cb.p_scb[xx];
    266         break;
    267       }
    268     }
    269   }
    270   return p_scb;
    271 }
    272 
    273 /*******************************************************************************
    274  *
    275  * Function         bta_av_hndl_to_scb
    276  *
    277  * Description      find the stream control block by the handle
    278  *
    279  * Returns          void
    280  *
    281  ******************************************************************************/
    282 tBTA_AV_SCB* bta_av_hndl_to_scb(uint16_t handle) {
    283   tBTA_AV_HNDL hndl = (tBTA_AV_HNDL)handle;
    284   tBTA_AV_SCB* p_scb = NULL;
    285   uint8_t idx = (hndl & BTA_AV_HNDL_MSK);
    286 
    287   if (idx && (idx <= BTA_AV_NUM_STRS)) {
    288     p_scb = bta_av_cb.p_scb[idx - 1];
    289   }
    290   return p_scb;
    291 }
    292 
    293 /*******************************************************************************
    294  *
    295  * Function         bta_av_alloc_scb
    296  *
    297  * Description      allocate stream control block,
    298  *                  register the service to stack
    299  *                  create SDP record
    300  *
    301  * Returns          void
    302  *
    303  ******************************************************************************/
    304 static tBTA_AV_SCB* bta_av_alloc_scb(tBTA_AV_CHNL chnl) {
    305   tBTA_AV_SCB* p_ret = NULL;
    306   int xx;
    307   tBTA_AV_STATUS sts = BTA_AV_SUCCESS;
    308 
    309   if (chnl == BTA_AV_CHNL_VIDEO) {
    310     if (p_bta_av_cfg->p_act_tbl == NULL || p_bta_av_cfg->p_reg == NULL) {
    311       APPL_TRACE_ERROR("Video streaming not supported");
    312       sts = BTA_AV_FAIL;
    313     } else {
    314       /* allow only one Video channel */
    315       if (bta_av_cb.reg_video) {
    316         APPL_TRACE_ERROR("Already registered");
    317         sts = BTA_AV_FAIL;
    318       }
    319     }
    320   } else if (chnl != BTA_AV_CHNL_AUDIO) {
    321     APPL_TRACE_ERROR("bad channel: %d", chnl);
    322     sts = BTA_AV_FAIL;
    323   }
    324 
    325   if (sts == BTA_AV_SUCCESS) {
    326     for (xx = 0; xx < BTA_AV_NUM_STRS; xx++) {
    327       if (bta_av_cb.p_scb[xx] == NULL) {
    328         /* found an empty spot */
    329         p_ret = (tBTA_AV_SCB*)osi_calloc(sizeof(tBTA_AV_SCB));
    330         p_ret->rc_handle = BTA_AV_RC_HANDLE_NONE;
    331         p_ret->chnl = chnl;
    332         p_ret->hndl = (tBTA_AV_HNDL)((xx + 1) | chnl);
    333         p_ret->hdi = xx;
    334         p_ret->a2dp_list = list_new(NULL);
    335         p_ret->avrc_ct_timer = alarm_new("bta_av.avrc_ct_timer");
    336         bta_av_cb.p_scb[xx] = p_ret;
    337         break;
    338       }
    339     }
    340   }
    341   return p_ret;
    342 }
    343 
    344 /*******************************************************************************
    345  ******************************************************************************/
    346 void bta_av_conn_cback(UNUSED_ATTR uint8_t handle, BD_ADDR bd_addr,
    347                        uint8_t event, tAVDT_CTRL* p_data) {
    348   uint16_t evt = 0;
    349   tBTA_AV_SCB* p_scb = NULL;
    350 
    351 #if (BTA_AR_INCLUDED == TRUE)
    352   if (event == BTA_AR_AVDT_CONN_EVT || event == AVDT_CONNECT_IND_EVT ||
    353       event == AVDT_DISCONNECT_IND_EVT)
    354 #else
    355   if (event == AVDT_CONNECT_IND_EVT || event == AVDT_DISCONNECT_IND_EVT)
    356 #endif
    357   {
    358     evt = BTA_AV_SIG_CHG_EVT;
    359     if (event == AVDT_DISCONNECT_IND_EVT) {
    360       p_scb = bta_av_addr_to_scb(bd_addr);
    361     } else if (event == AVDT_CONNECT_IND_EVT) {
    362       APPL_TRACE_DEBUG("%s: CONN_IND is ACP:%d", __func__,
    363                        p_data->hdr.err_param);
    364     }
    365 
    366     tBTA_AV_STR_MSG* p_msg =
    367         (tBTA_AV_STR_MSG*)osi_malloc(sizeof(tBTA_AV_STR_MSG));
    368     p_msg->hdr.event = evt;
    369     p_msg->hdr.layer_specific = event;
    370     p_msg->hdr.offset = p_data->hdr.err_param;
    371     bdcpy(p_msg->bd_addr, bd_addr);
    372     if (p_scb) {
    373       APPL_TRACE_DEBUG("scb hndl x%x, role x%x", p_scb->hndl, p_scb->role);
    374     }
    375     APPL_TRACE_DEBUG("conn_cback bd_addr:%02x-%02x-%02x-%02x-%02x-%02x",
    376                      bd_addr[0], bd_addr[1], bd_addr[2], bd_addr[3], bd_addr[4],
    377                      bd_addr[5]);
    378     bta_sys_sendmsg(p_msg);
    379   }
    380 }
    381 
    382 #if (AVDT_REPORTING == TRUE)
    383 /*******************************************************************************
    384  *
    385  * Function         bta_av_a2dp_report_cback
    386  *
    387  * Description      A2DP report callback.
    388  *
    389  * Returns          void
    390  *
    391  ******************************************************************************/
    392 static void bta_av_a2dp_report_cback(UNUSED_ATTR uint8_t handle,
    393                                      UNUSED_ATTR AVDT_REPORT_TYPE type,
    394                                      UNUSED_ATTR tAVDT_REPORT_DATA* p_data) {
    395   /* Do not need to handle report data for now.
    396    * This empty function is here for conformance reasons. */
    397 }
    398 #endif
    399 
    400 /*******************************************************************************
    401  *
    402  * Function         bta_av_api_register
    403  *
    404  * Description      allocate stream control block,
    405  *                  register the service to stack
    406  *                  create SDP record
    407  *
    408  * Returns          void
    409  *
    410  ******************************************************************************/
    411 static void bta_av_api_register(tBTA_AV_DATA* p_data) {
    412   tBTA_AV_REGISTER registr;
    413   tBTA_AV_SCB* p_scb; /* stream control block */
    414   tAVDT_REG reg;
    415   tAVDT_CS cs;
    416   char* p_service_name;
    417   tBTA_UTL_COD cod;
    418 
    419   memset(&cs, 0, sizeof(tAVDT_CS));
    420 
    421   registr.status = BTA_AV_FAIL_RESOURCES;
    422   registr.app_id = p_data->api_reg.app_id;
    423   registr.chnl = (tBTA_AV_CHNL)p_data->hdr.layer_specific;
    424 
    425   uint16_t profile_initialized = p_data->api_reg.service_uuid;
    426   if (profile_initialized == UUID_SERVCLASS_AUDIO_SINK) {
    427     p_bta_av_cfg = (tBTA_AV_CFG*)&bta_avk_cfg;
    428   } else if (profile_initialized == UUID_SERVCLASS_AUDIO_SOURCE) {
    429     p_bta_av_cfg = (tBTA_AV_CFG*)&bta_av_cfg;
    430   }
    431 
    432   APPL_TRACE_DEBUG("%s: profile: 0x%x", __func__, profile_initialized);
    433   if (p_bta_av_cfg == NULL) {
    434     APPL_TRACE_ERROR("AV configuration is null!");
    435     return;
    436   }
    437 
    438   do {
    439     p_scb = bta_av_alloc_scb(registr.chnl);
    440     if (p_scb == NULL) {
    441       APPL_TRACE_ERROR("failed to alloc SCB");
    442       break;
    443     }
    444 
    445     registr.hndl = p_scb->hndl;
    446     p_scb->app_id = registr.app_id;
    447 
    448     /* initialize the stream control block */
    449     registr.status = BTA_AV_SUCCESS;
    450 
    451     if ((bta_av_cb.reg_audio + bta_av_cb.reg_video) == 0) {
    452       /* the first channel registered. register to AVDTP */
    453       reg.ctrl_mtu = p_bta_av_cfg->sig_mtu;
    454       reg.ret_tout = BTA_AV_RET_TOUT;
    455       reg.sig_tout = BTA_AV_SIG_TOUT;
    456       reg.idle_tout = BTA_AV_IDLE_TOUT;
    457       reg.sec_mask = bta_av_cb.sec_mask;
    458 #if (BTA_AR_INCLUDED == TRUE)
    459       bta_ar_reg_avdt(&reg, bta_av_conn_cback, BTA_ID_AV);
    460 #endif
    461       bta_sys_role_chg_register(&bta_av_sys_rs_cback);
    462 
    463       /* create remote control TG service if required */
    464       if (bta_av_cb.features & (BTA_AV_FEAT_RCTG)) {
    465 /* register with no authorization; let AVDTP use authorization instead */
    466 #if (BTA_AR_INCLUDED == TRUE)
    467 #if (BTA_AV_WITH_AVCTP_AUTHORIZATION == TRUE)
    468         bta_ar_reg_avct(p_bta_av_cfg->avrc_mtu, p_bta_av_cfg->avrc_br_mtu,
    469                         bta_av_cb.sec_mask, BTA_ID_AV);
    470 #else
    471         bta_ar_reg_avct(p_bta_av_cfg->avrc_mtu, p_bta_av_cfg->avrc_br_mtu,
    472                         (uint8_t)(bta_av_cb.sec_mask & (~BTA_SEC_AUTHORIZE)),
    473                         BTA_ID_AV);
    474 #endif
    475 
    476         /* For the Audio Sink role we support additional TG 1.3 to support
    477          * absolute volume.
    478          */
    479         uint16_t profile_version = AVRC_REV_1_0;
    480 
    481         if (profile_initialized == UUID_SERVCLASS_AUDIO_SOURCE) {
    482           // This check can override the AVRCP profile version with a property
    483           char avrcp_version[PROPERTY_VALUE_MAX] = {0};
    484           osi_property_get(AVRCP_VERSION_PROPERTY, avrcp_version,
    485                            AVRCP_1_4_STRING);
    486           LOG_INFO(LOG_TAG, "AVRCP version used for sdp: \"%s\"",
    487                    avrcp_version);
    488 
    489           if (!strncmp(AVRCP_1_6_STRING, avrcp_version,
    490                        sizeof(AVRCP_1_6_STRING))) {
    491             profile_version = AVRC_REV_1_6;
    492           } else if (!strncmp(AVRCP_1_5_STRING, avrcp_version,
    493                               sizeof(AVRCP_1_5_STRING))) {
    494             profile_version = AVRC_REV_1_5;
    495           } else {
    496             profile_version = AVRC_REV_1_4;
    497           }
    498         } else if (profile_initialized == UUID_SERVCLASS_AUDIO_SINK) {
    499           // Initialize AVRCP1.4 to provide Absolute Volume control.
    500           profile_version = AVRC_REV_1_4;
    501         }
    502 
    503         bta_ar_reg_avrc(
    504             UUID_SERVCLASS_AV_REM_CTRL_TARGET, "AV Remote Control Target", NULL,
    505             p_bta_av_cfg->avrc_tg_cat, BTA_ID_AV,
    506             (bta_av_cb.features & BTA_AV_FEAT_BROWSE), profile_version);
    507 #endif
    508       }
    509 
    510       /* Set the Capturing service class bit */
    511       if (profile_initialized == UUID_SERVCLASS_AUDIO_SOURCE)
    512         cod.service = BTM_COD_SERVICE_CAPTURING;
    513       else if (profile_initialized == UUID_SERVCLASS_AUDIO_SINK)
    514         cod.service = BTM_COD_SERVICE_RENDERING;
    515       utl_set_device_class(&cod, BTA_UTL_SET_COD_SERVICE_CLASS);
    516     } /* if 1st channel */
    517 
    518     /* get stream configuration and create stream */
    519     cs.cfg.num_codec = 1;
    520     cs.nsc_mask =
    521         AVDT_NSC_RECONFIG |
    522         ((bta_av_cb.features & BTA_AV_FEAT_PROTECT) ? 0 : AVDT_NSC_SECURITY);
    523     APPL_TRACE_DEBUG("nsc_mask: 0x%x", cs.nsc_mask);
    524 
    525     if (p_data->api_reg.p_service_name[0] == 0) {
    526       p_service_name = NULL;
    527     } else {
    528       p_service_name = p_data->api_reg.p_service_name;
    529     }
    530 
    531     p_scb->suspend_sup = true;
    532     p_scb->recfg_sup = true;
    533     p_scb->skip_sdp = false;
    534 
    535     cs.p_ctrl_cback = bta_av_dt_cback[p_scb->hdi];
    536     if (registr.chnl == BTA_AV_CHNL_AUDIO) {
    537       /* set up the audio stream control block */
    538       p_scb->p_act_tbl = (const tBTA_AV_ACT*)bta_av_a2dp_action;
    539       p_scb->p_cos = &bta_av_a2dp_cos;
    540       p_scb->media_type = AVDT_MEDIA_TYPE_AUDIO;
    541       cs.cfg.psc_mask = AVDT_PSC_TRANS;
    542       cs.media_type = AVDT_MEDIA_TYPE_AUDIO;
    543       cs.mtu = p_bta_av_cfg->audio_mtu;
    544       cs.flush_to = L2CAP_DEFAULT_FLUSH_TO;
    545       btav_a2dp_codec_index_t codec_index_min =
    546           BTAV_A2DP_CODEC_INDEX_SOURCE_MIN;
    547       btav_a2dp_codec_index_t codec_index_max =
    548           BTAV_A2DP_CODEC_INDEX_SOURCE_MAX;
    549 
    550 #if (AVDT_REPORTING == TRUE)
    551       if (bta_av_cb.features & BTA_AV_FEAT_REPORT) {
    552         cs.cfg.psc_mask |= AVDT_PSC_REPORT;
    553         cs.p_report_cback = bta_av_a2dp_report_cback;
    554       }
    555 #endif
    556       if (bta_av_cb.features & BTA_AV_FEAT_DELAY_RPT)
    557         cs.cfg.psc_mask |= AVDT_PSC_DELAY_RPT;
    558 
    559       if (profile_initialized == UUID_SERVCLASS_AUDIO_SOURCE) {
    560         cs.tsep = AVDT_TSEP_SRC;
    561         codec_index_min = BTAV_A2DP_CODEC_INDEX_SOURCE_MIN;
    562         codec_index_max = BTAV_A2DP_CODEC_INDEX_SOURCE_MAX;
    563       } else if (profile_initialized == UUID_SERVCLASS_AUDIO_SINK) {
    564         cs.tsep = AVDT_TSEP_SNK;
    565         cs.p_sink_data_cback = bta_av_sink_data_cback;
    566         codec_index_min = BTAV_A2DP_CODEC_INDEX_SINK_MIN;
    567         codec_index_max = BTAV_A2DP_CODEC_INDEX_SINK_MAX;
    568       }
    569 
    570       /* Initialize handles to zero */
    571       for (int xx = 0; xx < BTAV_A2DP_CODEC_INDEX_MAX; xx++) {
    572         p_scb->seps[xx].av_handle = 0;
    573       }
    574 
    575       /* keep the configuration in the stream control block */
    576       memcpy(&p_scb->cfg, &cs.cfg, sizeof(tAVDT_CFG));
    577       for (int i = codec_index_min; i < codec_index_max; i++) {
    578         btav_a2dp_codec_index_t codec_index =
    579             static_cast<btav_a2dp_codec_index_t>(i);
    580         if (!(*bta_av_a2dp_cos.init)(codec_index, &cs.cfg)) {
    581           continue;
    582         }
    583         if (AVDT_CreateStream(&p_scb->seps[codec_index].av_handle, &cs) !=
    584             AVDT_SUCCESS) {
    585           continue;
    586         }
    587         /* Save a copy of the codec */
    588         memcpy(p_scb->seps[codec_index].codec_info, cs.cfg.codec_info,
    589                AVDT_CODEC_SIZE);
    590         p_scb->seps[codec_index].tsep = cs.tsep;
    591         if (cs.tsep == AVDT_TSEP_SNK) {
    592           p_scb->seps[codec_index].p_app_sink_data_cback =
    593               p_data->api_reg.p_app_sink_data_cback;
    594         } else {
    595           /* In case of A2DP SOURCE we don't need a callback to
    596            * handle media packets.
    597            */
    598           p_scb->seps[codec_index].p_app_sink_data_cback = NULL;
    599         }
    600       }
    601 
    602       if (!bta_av_cb.reg_audio) {
    603         bta_av_cb.sdp_a2dp_handle = 0;
    604         bta_av_cb.sdp_a2dp_snk_handle = 0;
    605         if (profile_initialized == UUID_SERVCLASS_AUDIO_SOURCE) {
    606           /* create the SDP records on the 1st audio channel */
    607           bta_av_cb.sdp_a2dp_handle = SDP_CreateRecord();
    608           A2DP_AddRecord(UUID_SERVCLASS_AUDIO_SOURCE, p_service_name, NULL,
    609                          A2DP_SUPF_PLAYER, bta_av_cb.sdp_a2dp_handle);
    610           bta_sys_add_uuid(UUID_SERVCLASS_AUDIO_SOURCE);
    611         } else if (profile_initialized == UUID_SERVCLASS_AUDIO_SINK) {
    612 #if (BTA_AV_SINK_INCLUDED == TRUE)
    613           bta_av_cb.sdp_a2dp_snk_handle = SDP_CreateRecord();
    614           A2DP_AddRecord(UUID_SERVCLASS_AUDIO_SINK, p_service_name, NULL,
    615                          A2DP_SUPF_PLAYER, bta_av_cb.sdp_a2dp_snk_handle);
    616           bta_sys_add_uuid(UUID_SERVCLASS_AUDIO_SINK);
    617 #endif
    618         }
    619         /* start listening when A2DP is registered */
    620         if (bta_av_cb.features & BTA_AV_FEAT_RCTG)
    621           bta_av_rc_create(&bta_av_cb, AVCT_ACP, 0, BTA_AV_NUM_LINKS + 1);
    622 
    623         /* if the AV and AVK are both supported, it cannot support the CT role
    624          */
    625         if (bta_av_cb.features & (BTA_AV_FEAT_RCCT)) {
    626           /* if TG is not supported, we need to register to AVCT now */
    627           if ((bta_av_cb.features & (BTA_AV_FEAT_RCTG)) == 0) {
    628 #if (BTA_AR_INCLUDED == TRUE)
    629 #if (BTA_AV_WITH_AVCTP_AUTHORIZATION == TRUE)
    630             bta_ar_reg_avct(p_bta_av_cfg->avrc_mtu, p_bta_av_cfg->avrc_br_mtu,
    631                             bta_av_cb.sec_mask, BTA_ID_AV);
    632 #else
    633             bta_ar_reg_avct(
    634                 p_bta_av_cfg->avrc_mtu, p_bta_av_cfg->avrc_br_mtu,
    635                 (uint8_t)(bta_av_cb.sec_mask & (~BTA_SEC_AUTHORIZE)),
    636                 BTA_ID_AV);
    637 #endif
    638 #endif
    639             bta_av_rc_create(&bta_av_cb, AVCT_ACP, 0, BTA_AV_NUM_LINKS + 1);
    640           }
    641 #if (BTA_AR_INCLUDED == TRUE)
    642           /* create an SDP record as AVRC CT. We create 1.3 for SOURCE
    643            * because we rely on feature bits being scanned by external
    644            * devices more than the profile version itself.
    645            *
    646            * We create 1.4 for SINK since we support browsing.
    647            */
    648           if (profile_initialized == UUID_SERVCLASS_AUDIO_SOURCE) {
    649             bta_ar_reg_avrc(UUID_SERVCLASS_AV_REMOTE_CONTROL, NULL, NULL,
    650                             p_bta_av_cfg->avrc_ct_cat, BTA_ID_AV,
    651                             (bta_av_cb.features & BTA_AV_FEAT_BROWSE),
    652                             AVRC_REV_1_3);
    653           } else if (profile_initialized == UUID_SERVCLASS_AUDIO_SINK) {
    654             bta_ar_reg_avrc(UUID_SERVCLASS_AV_REMOTE_CONTROL, NULL, NULL,
    655                             p_bta_av_cfg->avrc_ct_cat, BTA_ID_AV,
    656                             (bta_av_cb.features & BTA_AV_FEAT_BROWSE),
    657                             AVRC_REV_1_6);
    658           }
    659 #endif
    660         }
    661       }
    662       bta_av_cb.reg_audio |= BTA_AV_HNDL_TO_MSK(p_scb->hdi);
    663       APPL_TRACE_DEBUG("reg_audio: 0x%x", bta_av_cb.reg_audio);
    664     } else {
    665       bta_av_cb.reg_video = BTA_AV_HNDL_TO_MSK(p_scb->hdi);
    666       bta_av_cb.sdp_vdp_handle = SDP_CreateRecord();
    667       /* register the video channel */
    668       /* no need to verify the function pointer here. it's verified prior */
    669       (*p_bta_av_cfg->p_reg)(&cs, p_service_name, p_scb);
    670     }
    671   } while (0);
    672 
    673   /* call callback with register event */
    674   (*bta_av_cb.p_cback)(BTA_AV_REGISTER_EVT, (tBTA_AV*)&registr);
    675 }
    676 
    677 /*******************************************************************************
    678  *
    679  * Function         bta_av_api_deregister
    680  *
    681  * Description      de-register a channel
    682  *
    683  *
    684  * Returns          void
    685  *
    686  ******************************************************************************/
    687 void bta_av_api_deregister(tBTA_AV_DATA* p_data) {
    688   tBTA_AV_SCB* p_scb = bta_av_hndl_to_scb(p_data->hdr.layer_specific);
    689 
    690   if (p_scb) {
    691     p_scb->deregistring = true;
    692     bta_av_ssm_execute(p_scb, BTA_AV_API_CLOSE_EVT, p_data);
    693   } else {
    694     bta_av_dereg_comp(p_data);
    695   }
    696 }
    697 
    698 /*******************************************************************************
    699  *
    700  * Function         bta_av_ci_data
    701  *
    702  * Description      Forward the BTA_AV_CI_SRC_DATA_READY_EVT to stream state
    703  *                  machine.
    704  *
    705  *
    706  * Returns          void
    707  *
    708  ******************************************************************************/
    709 static void bta_av_ci_data(tBTA_AV_DATA* p_data) {
    710   tBTA_AV_SCB* p_scb;
    711   int i;
    712   uint8_t chnl = (uint8_t)p_data->hdr.layer_specific;
    713 
    714   for (i = 0; i < BTA_AV_NUM_STRS; i++) {
    715     p_scb = bta_av_cb.p_scb[i];
    716 
    717     if (p_scb && p_scb->chnl == chnl) {
    718       bta_av_ssm_execute(p_scb, BTA_AV_SRC_DATA_READY_EVT, p_data);
    719     }
    720   }
    721 }
    722 
    723 /*******************************************************************************
    724  *
    725  * Function         bta_av_rpc_conn
    726  *
    727  * Description      report report channel open
    728  *
    729  * Returns          void
    730  *
    731  ******************************************************************************/
    732 #if (AVDT_REPORTING == TRUE)
    733 static void bta_av_rpc_conn(UNUSED_ATTR tBTA_AV_DATA* p_data) {}
    734 #endif
    735 
    736 /*******************************************************************************
    737  *
    738  * Function         bta_av_api_to_ssm
    739  *
    740  * Description      forward the API request to stream state machine
    741  *
    742  *
    743  * Returns          void
    744  *
    745  ******************************************************************************/
    746 static void bta_av_api_to_ssm(tBTA_AV_DATA* p_data) {
    747   int xx;
    748   uint16_t event =
    749       p_data->hdr.event - BTA_AV_FIRST_A2S_API_EVT + BTA_AV_FIRST_A2S_SSM_EVT;
    750 
    751   for (xx = 0; xx < BTA_AV_NUM_STRS; xx++) {
    752     bta_av_ssm_execute(bta_av_cb.p_scb[xx], event, p_data);
    753   }
    754 }
    755 
    756 /*******************************************************************************
    757  *
    758  * Function         bta_av_chk_start
    759  *
    760  * Description      if this is audio channel, check if more than one audio
    761  *                  channel is connected & already started.
    762  *
    763  * Returns          true, if need api_start
    764  *
    765  ******************************************************************************/
    766 bool bta_av_chk_start(tBTA_AV_SCB* p_scb) {
    767   bool start = false;
    768   tBTA_AV_SCB* p_scbi;
    769   int i;
    770 
    771   if (p_scb->chnl == BTA_AV_CHNL_AUDIO) {
    772     if ((bta_av_cb.audio_open_cnt >= 2) &&
    773         ((0 ==
    774           (p_scb->role & BTA_AV_ROLE_AD_ACP)) || /* Outgoing connection or   */
    775          (bta_av_cb.features &
    776           BTA_AV_FEAT_ACP_START))) /* auto-starting option     */
    777     {
    778       /* more than one audio channel is connected */
    779       /* if this is the 2nd stream as ACP, give INT a chance to issue the START
    780        * command */
    781       for (i = 0; i < BTA_AV_NUM_STRS; i++) {
    782         p_scbi = bta_av_cb.p_scb[i];
    783         if (p_scbi && p_scbi->chnl == BTA_AV_CHNL_AUDIO && p_scbi->co_started) {
    784           start = true;
    785           /* may need to update the flush timeout of this already started stream
    786            */
    787           if (p_scbi->co_started != bta_av_cb.audio_open_cnt) {
    788             p_scbi->co_started = bta_av_cb.audio_open_cnt;
    789             L2CA_SetFlushTimeout(
    790                 p_scbi->peer_addr,
    791                 p_bta_av_cfg->p_audio_flush_to[p_scbi->co_started - 1]);
    792           }
    793         }
    794       }
    795     }
    796   }
    797   return start;
    798 }
    799 
    800 /*******************************************************************************
    801  *
    802  * Function         bta_av_restore_switch
    803  *
    804  * Description      assume that the caller of this function already makes
    805  *                  sure that there's only one ACL connection left
    806  *
    807  * Returns          void
    808  *
    809  ******************************************************************************/
    810 void bta_av_restore_switch(void) {
    811   tBTA_AV_CB* p_cb = &bta_av_cb;
    812   int i;
    813   uint8_t mask;
    814 
    815   APPL_TRACE_DEBUG("reg_audio: 0x%x", bta_av_cb.reg_audio);
    816   for (i = 0; i < BTA_AV_NUM_STRS; i++) {
    817     mask = BTA_AV_HNDL_TO_MSK(i);
    818     if (p_cb->conn_audio == mask) {
    819       if (p_cb->p_scb[i]) {
    820         bta_sys_set_policy(BTA_ID_AV, HCI_ENABLE_MASTER_SLAVE_SWITCH,
    821                            p_cb->p_scb[i]->peer_addr);
    822       }
    823       break;
    824     }
    825   }
    826 }
    827 
    828 /*******************************************************************************
    829  *
    830  * Function         bta_av_sys_rs_cback
    831  *
    832  * Description      Receives the role change event from dm
    833  *
    834  * Returns          (BTA_SYS_ROLE_CHANGE, new_role, hci_status, p_bda)
    835  *
    836  ******************************************************************************/
    837 static void bta_av_sys_rs_cback(UNUSED_ATTR tBTA_SYS_CONN_STATUS status,
    838                                 uint8_t id, uint8_t app_id, BD_ADDR peer_addr) {
    839   int i;
    840   tBTA_AV_SCB* p_scb = NULL;
    841   uint8_t cur_role;
    842   uint8_t peer_idx = 0;
    843 
    844   APPL_TRACE_DEBUG("bta_av_sys_rs_cback: %d", bta_av_cb.rs_idx);
    845   for (i = 0; i < BTA_AV_NUM_STRS; i++) {
    846     /* loop through all the SCBs to find matching peer addresses and report the
    847      * role change event */
    848     /* note that more than one SCB (a2dp & vdp) maybe waiting for this event */
    849     p_scb = bta_av_cb.p_scb[i];
    850     if (p_scb && (bdcmp(peer_addr, p_scb->peer_addr) == 0)) {
    851       tBTA_AV_ROLE_RES* p_buf =
    852           (tBTA_AV_ROLE_RES*)osi_malloc(sizeof(tBTA_AV_ROLE_RES));
    853       APPL_TRACE_DEBUG("new_role:%d, hci_status:x%x hndl: x%x", id, app_id,
    854                        p_scb->hndl);
    855       /*
    856       if ((id != BTM_ROLE_MASTER) && (app_id != HCI_SUCCESS))
    857       {
    858           bta_sys_set_policy(BTA_ID_AV,
    859       (HCI_ENABLE_MASTER_SLAVE_SWITCH|HCI_ENABLE_SNIFF_MODE), p_scb->peer_addr);
    860       }
    861       */
    862       p_buf->hdr.event = BTA_AV_ROLE_CHANGE_EVT;
    863       p_buf->hdr.layer_specific = p_scb->hndl;
    864       p_buf->new_role = id;
    865       p_buf->hci_status = app_id;
    866       bta_sys_sendmsg(p_buf);
    867 
    868       peer_idx = p_scb->hdi + 1; /* Handle index for the peer_addr */
    869     }
    870   }
    871 
    872   /* restore role switch policy, if role switch failed */
    873   if ((HCI_SUCCESS != app_id) &&
    874       (BTM_GetRole(peer_addr, &cur_role) == BTM_SUCCESS) &&
    875       (cur_role == BTM_ROLE_SLAVE)) {
    876     bta_sys_set_policy(BTA_ID_AV, HCI_ENABLE_MASTER_SLAVE_SWITCH, peer_addr);
    877   }
    878 
    879   /* if BTA_AvOpen() was called for other device, which caused the role switch
    880    * of the peer_addr,  */
    881   /* we need to continue opening process for the BTA_AvOpen(). */
    882   if ((bta_av_cb.rs_idx != 0) && (bta_av_cb.rs_idx != peer_idx)) {
    883     if ((bta_av_cb.rs_idx - 1) < BTA_AV_NUM_STRS) {
    884       p_scb = bta_av_cb.p_scb[bta_av_cb.rs_idx - 1];
    885     }
    886     if (p_scb && p_scb->q_tag == BTA_AV_Q_TAG_OPEN) {
    887       APPL_TRACE_DEBUG("bta_av_sys_rs_cback: rs_idx(%d), hndl:x%x q_tag: %d",
    888                        bta_av_cb.rs_idx, p_scb->hndl, p_scb->q_tag);
    889 
    890       if (HCI_SUCCESS == app_id || HCI_ERR_NO_CONNECTION == app_id)
    891         p_scb->q_info.open.switch_res = BTA_AV_RS_OK;
    892       else
    893         p_scb->q_info.open.switch_res = BTA_AV_RS_FAIL;
    894 
    895       /* Continue av open process */
    896       bta_av_do_disc_a2dp(p_scb, (tBTA_AV_DATA*)&(p_scb->q_info.open));
    897     }
    898 
    899     bta_av_cb.rs_idx = 0;
    900   }
    901 }
    902 
    903 /*******************************************************************************
    904  *
    905  * Function         bta_av_sco_chg_cback
    906  *
    907  * Description      receive & process the SCO connection up/down event from sys.
    908  *                  call setup also triggers this callback, to suspend av before
    909  *                  SCO activity happens, or to resume av once call ends.
    910  *
    911  * Returns          void
    912  *
    913  ******************************************************************************/
    914 static void bta_av_sco_chg_cback(tBTA_SYS_CONN_STATUS status, uint8_t id,
    915                                  UNUSED_ATTR uint8_t app_id,
    916                                  UNUSED_ATTR BD_ADDR peer_addr) {
    917   tBTA_AV_SCB* p_scb;
    918   int i;
    919   tBTA_AV_API_STOP stop;
    920 
    921   APPL_TRACE_DEBUG("bta_av_sco_chg_cback:%d status:%d", id, status);
    922   if (id) {
    923     bta_av_cb.sco_occupied = true;
    924 
    925     /* either BTA_SYS_SCO_OPEN or BTA_SYS_SCO_CLOSE with remaining active SCO */
    926     for (i = 0; i < BTA_AV_NUM_STRS; i++) {
    927       p_scb = bta_av_cb.p_scb[i];
    928 
    929       if (p_scb && p_scb->co_started && (p_scb->sco_suspend == false)) {
    930         APPL_TRACE_DEBUG("suspending scb:%d", i);
    931         /* scb is used and started, not suspended automatically */
    932         p_scb->sco_suspend = true;
    933         stop.flush = false;
    934         stop.suspend = true;
    935         stop.reconfig_stop = false;
    936         bta_av_ssm_execute(p_scb, BTA_AV_AP_STOP_EVT, (tBTA_AV_DATA*)&stop);
    937       }
    938     }
    939   } else {
    940     bta_av_cb.sco_occupied = false;
    941 
    942     for (i = 0; i < BTA_AV_NUM_STRS; i++) {
    943       p_scb = bta_av_cb.p_scb[i];
    944 
    945       if (p_scb && p_scb->sco_suspend) /* scb is used and suspended for SCO */
    946       {
    947         APPL_TRACE_DEBUG("starting scb:%d", i);
    948         bta_av_ssm_execute(p_scb, BTA_AV_AP_START_EVT, NULL);
    949       }
    950     }
    951   }
    952 }
    953 
    954 /*******************************************************************************
    955  *
    956  * Function         bta_av_switch_if_needed
    957  *
    958  * Description      This function checks if there is another existing AV
    959  *                  channel that is local as slave role.
    960  *                  If so, role switch and remove it from link policy.
    961  *
    962  * Returns          true, if role switch is done
    963  *
    964  ******************************************************************************/
    965 bool bta_av_switch_if_needed(tBTA_AV_SCB* p_scb) {
    966   uint8_t role;
    967   bool needed = false;
    968   tBTA_AV_SCB* p_scbi;
    969   int i;
    970   uint8_t mask;
    971 
    972   for (i = 0; i < BTA_AV_NUM_STRS; i++) {
    973     mask = BTA_AV_HNDL_TO_MSK(i);
    974     p_scbi = bta_av_cb.p_scb[i];
    975     if (p_scbi && (p_scb->hdi != i) &&    /* not the original channel */
    976         ((bta_av_cb.conn_audio & mask) || /* connected audio */
    977          (bta_av_cb.conn_video & mask)))  /* connected video */
    978     {
    979       BTM_GetRole(p_scbi->peer_addr, &role);
    980       /* this channel is open - clear the role switch link policy for this link
    981        */
    982       if (BTM_ROLE_MASTER != role) {
    983         if (bta_av_cb.features & BTA_AV_FEAT_MASTER)
    984           bta_sys_clear_policy(BTA_ID_AV, HCI_ENABLE_MASTER_SLAVE_SWITCH,
    985                                p_scbi->peer_addr);
    986         if (BTM_CMD_STARTED !=
    987             BTM_SwitchRole(p_scbi->peer_addr, BTM_ROLE_MASTER, NULL)) {
    988           /* can not switch role on SCBI
    989            * start the timer on SCB - because this function is ONLY called when
    990            * SCB gets API_OPEN */
    991           bta_sys_start_timer(p_scb->avrc_ct_timer, BTA_AV_RS_TIME_VAL,
    992                               BTA_AV_AVRC_TIMER_EVT, p_scb->hndl);
    993         }
    994         needed = true;
    995         /* mark the original channel as waiting for RS result */
    996         bta_av_cb.rs_idx = p_scb->hdi + 1;
    997         break;
    998       }
    999     }
   1000   }
   1001   return needed;
   1002 }
   1003 
   1004 /*******************************************************************************
   1005  *
   1006  * Function         bta_av_link_role_ok
   1007  *
   1008  * Description      This function checks if the SCB has existing ACL connection
   1009  *                  If so, check if the link role fits the requirements.
   1010  *
   1011  * Returns          true, if role is ok
   1012  *
   1013  ******************************************************************************/
   1014 bool bta_av_link_role_ok(tBTA_AV_SCB* p_scb, uint8_t bits) {
   1015   uint8_t role;
   1016   bool is_ok = true;
   1017 
   1018   if (BTM_GetRole(p_scb->peer_addr, &role) == BTM_SUCCESS) {
   1019     LOG_INFO(LOG_TAG, "%s hndl:x%x role:%d conn_audio:x%x bits:%d features:x%x",
   1020              __func__, p_scb->hndl, role, bta_av_cb.conn_audio, bits,
   1021              bta_av_cb.features);
   1022     if (BTM_ROLE_MASTER != role &&
   1023         (A2DP_BitsSet(bta_av_cb.conn_audio) > bits ||
   1024          (bta_av_cb.features & BTA_AV_FEAT_MASTER))) {
   1025       if (bta_av_cb.features & BTA_AV_FEAT_MASTER)
   1026         bta_sys_clear_policy(BTA_ID_AV, HCI_ENABLE_MASTER_SLAVE_SWITCH,
   1027                              p_scb->peer_addr);
   1028 
   1029       if (BTM_CMD_STARTED !=
   1030           BTM_SwitchRole(p_scb->peer_addr, BTM_ROLE_MASTER, NULL)) {
   1031         /* can not switch role on SCB - start the timer on SCB */
   1032       }
   1033       is_ok = false;
   1034       p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RES_START;
   1035     }
   1036   }
   1037 
   1038   return is_ok;
   1039 }
   1040 
   1041 /*******************************************************************************
   1042  *
   1043  * Function         bta_av_chk_mtu
   1044  *
   1045  * Description      if this is audio channel, check if more than one audio
   1046  *                  channel is connected.
   1047  *
   1048  * Returns          The smallest mtu of the connected audio channels
   1049  *
   1050  ******************************************************************************/
   1051 uint16_t bta_av_chk_mtu(tBTA_AV_SCB* p_scb, UNUSED_ATTR uint16_t mtu) {
   1052   uint16_t ret_mtu = BTA_AV_MAX_A2DP_MTU;
   1053   tBTA_AV_SCB* p_scbi;
   1054   int i;
   1055   uint8_t mask;
   1056 
   1057   /* TODO_MV mess with the mtu according to the number of EDR/non-EDR headsets
   1058    */
   1059   if (p_scb->chnl == BTA_AV_CHNL_AUDIO) {
   1060     if (bta_av_cb.audio_open_cnt >= 2) {
   1061       /* more than one audio channel is connected */
   1062       for (i = 0; i < BTA_AV_NUM_STRS; i++) {
   1063         p_scbi = bta_av_cb.p_scb[i];
   1064         if ((p_scb != p_scbi) && p_scbi &&
   1065             (p_scbi->chnl == BTA_AV_CHNL_AUDIO)) {
   1066           mask = BTA_AV_HNDL_TO_MSK(i);
   1067           APPL_TRACE_DEBUG("[%d] mtu: %d, mask:0x%x", i, p_scbi->stream_mtu,
   1068                            mask);
   1069           if (bta_av_cb.conn_audio & mask) {
   1070             if (ret_mtu > p_scbi->stream_mtu) ret_mtu = p_scbi->stream_mtu;
   1071           }
   1072         }
   1073       }
   1074     }
   1075     APPL_TRACE_DEBUG("bta_av_chk_mtu audio count:%d, conn_audio:0x%x, ret:%d",
   1076                      bta_av_cb.audio_open_cnt, bta_av_cb.conn_audio, ret_mtu);
   1077   }
   1078   return ret_mtu;
   1079 }
   1080 
   1081 /*******************************************************************************
   1082  *
   1083  * Function         bta_av_dup_audio_buf
   1084  *
   1085  * Description      dup the audio data to the q_info.a2dp of other audio
   1086  *                  channels
   1087  *
   1088  * Returns          void
   1089  *
   1090  ******************************************************************************/
   1091 void bta_av_dup_audio_buf(tBTA_AV_SCB* p_scb, BT_HDR* p_buf) {
   1092   /* Test whether there is more than one audio channel connected */
   1093   if ((p_buf == NULL) || (bta_av_cb.audio_open_cnt < 2)) return;
   1094 
   1095   uint16_t copy_size = BT_HDR_SIZE + p_buf->len + p_buf->offset;
   1096   for (int i = 0; i < BTA_AV_NUM_STRS; i++) {
   1097     tBTA_AV_SCB* p_scbi = bta_av_cb.p_scb[i];
   1098 
   1099     if (i == p_scb->hdi) continue; /* Ignore the original channel */
   1100     if ((p_scbi == NULL) || !p_scbi->co_started)
   1101       continue; /* Ignore if SCB is not used or started */
   1102     if (!(bta_av_cb.conn_audio & BTA_AV_HNDL_TO_MSK(i)))
   1103       continue; /* Audio is not connected */
   1104 
   1105     /* Enqueue the data */
   1106     BT_HDR* p_new = (BT_HDR*)osi_malloc(copy_size);
   1107     memcpy(p_new, p_buf, copy_size);
   1108     list_append(p_scbi->a2dp_list, p_new);
   1109 
   1110     if (list_length(p_scbi->a2dp_list) > p_bta_av_cfg->audio_mqs) {
   1111       // Drop the oldest packet
   1112       bta_av_co_audio_drop(p_scbi->hndl);
   1113       BT_HDR* p_buf_drop = static_cast<BT_HDR*>(list_front(p_scbi->a2dp_list));
   1114       list_remove(p_scbi->a2dp_list, p_buf_drop);
   1115       osi_free(p_buf_drop);
   1116     }
   1117   }
   1118 }
   1119 
   1120 /*******************************************************************************
   1121  *
   1122  * Function         bta_av_sm_execute
   1123  *
   1124  * Description      State machine event handling function for AV
   1125  *
   1126  *
   1127  * Returns          void
   1128  *
   1129  ******************************************************************************/
   1130 void bta_av_sm_execute(tBTA_AV_CB* p_cb, uint16_t event, tBTA_AV_DATA* p_data) {
   1131   tBTA_AV_ST_TBL state_table;
   1132   uint8_t action;
   1133 
   1134   APPL_TRACE_EVENT("%s: AV event=0x%x(%s) state=%d(%s)", __func__, event,
   1135                    bta_av_evt_code(event), p_cb->state,
   1136                    bta_av_st_code(p_cb->state));
   1137 
   1138   /* look up the state table for the current state */
   1139   state_table = bta_av_st_tbl[p_cb->state];
   1140 
   1141   event &= 0x00FF;
   1142 
   1143   /* set next state */
   1144   p_cb->state = state_table[event][BTA_AV_NEXT_STATE];
   1145   APPL_TRACE_EVENT("next state=%d event offset:%d", p_cb->state, event);
   1146 
   1147   /* execute action functions */
   1148   action = state_table[event][BTA_AV_ACTION_COL];
   1149   if (action != BTA_AV_IGNORE) {
   1150     APPL_TRACE_EVENT("%s action executed %d", __func__, action);
   1151     (*bta_av_action[action])(p_cb, p_data);
   1152   }
   1153 }
   1154 
   1155 /*******************************************************************************
   1156  *
   1157  * Function         bta_av_hdl_event
   1158  *
   1159  * Description      Advanced audio/video main event handling function.
   1160  *
   1161  *
   1162  * Returns          bool
   1163  *
   1164  ******************************************************************************/
   1165 bool bta_av_hdl_event(BT_HDR* p_msg) {
   1166   if (p_msg->event > BTA_AV_LAST_EVT) {
   1167     return true; /* to free p_msg */
   1168   }
   1169   if (p_msg->event >= BTA_AV_FIRST_NSM_EVT) {
   1170     APPL_TRACE_VERBOSE("%s: AV nsm event=0x%x(%s)", __func__, p_msg->event,
   1171                        bta_av_evt_code(p_msg->event));
   1172     /* non state machine events */
   1173     (*bta_av_nsm_act[p_msg->event - BTA_AV_FIRST_NSM_EVT])(
   1174         (tBTA_AV_DATA*)p_msg);
   1175   } else if (p_msg->event >= BTA_AV_FIRST_SM_EVT &&
   1176              p_msg->event <= BTA_AV_LAST_SM_EVT) {
   1177     APPL_TRACE_VERBOSE("%s: AV sm event=0x%x(%s)", __func__, p_msg->event,
   1178                        bta_av_evt_code(p_msg->event));
   1179     /* state machine events */
   1180     bta_av_sm_execute(&bta_av_cb, p_msg->event, (tBTA_AV_DATA*)p_msg);
   1181   } else {
   1182     APPL_TRACE_VERBOSE("handle=0x%x", p_msg->layer_specific);
   1183     /* stream state machine events */
   1184     bta_av_ssm_execute(bta_av_hndl_to_scb(p_msg->layer_specific), p_msg->event,
   1185                        (tBTA_AV_DATA*)p_msg);
   1186   }
   1187   return true;
   1188 }
   1189 
   1190 /*****************************************************************************
   1191  *  Debug Functions
   1192  ****************************************************************************/
   1193 /*******************************************************************************
   1194  *
   1195  * Function         bta_av_st_code
   1196  *
   1197  * Description
   1198  *
   1199  * Returns          char *
   1200  *
   1201  ******************************************************************************/
   1202 static const char* bta_av_st_code(uint8_t state) {
   1203   switch (state) {
   1204     case BTA_AV_INIT_ST:
   1205       return "INIT";
   1206     case BTA_AV_OPEN_ST:
   1207       return "OPEN";
   1208     default:
   1209       return "unknown";
   1210   }
   1211 }
   1212 /*******************************************************************************
   1213  *
   1214  * Function         bta_av_evt_code
   1215  *
   1216  * Description
   1217  *
   1218  * Returns          char *
   1219  *
   1220  ******************************************************************************/
   1221 const char* bta_av_evt_code(uint16_t evt_code) {
   1222   switch (evt_code) {
   1223     case BTA_AV_API_DISABLE_EVT:
   1224       return "API_DISABLE";
   1225     case BTA_AV_API_REMOTE_CMD_EVT:
   1226       return "API_REMOTE_CMD";
   1227     case BTA_AV_API_VENDOR_CMD_EVT:
   1228       return "API_VENDOR_CMD";
   1229     case BTA_AV_API_VENDOR_RSP_EVT:
   1230       return "API_VENDOR_RSP";
   1231     case BTA_AV_API_META_RSP_EVT:
   1232       return "API_META_RSP_EVT";
   1233     case BTA_AV_API_RC_CLOSE_EVT:
   1234       return "API_RC_CLOSE";
   1235     case BTA_AV_AVRC_OPEN_EVT:
   1236       return "AVRC_OPEN";
   1237     case BTA_AV_AVRC_MSG_EVT:
   1238       return "AVRC_MSG";
   1239     case BTA_AV_AVRC_NONE_EVT:
   1240       return "AVRC_NONE";
   1241 
   1242     case BTA_AV_API_OPEN_EVT:
   1243       return "API_OPEN";
   1244     case BTA_AV_API_CLOSE_EVT:
   1245       return "API_CLOSE";
   1246     case BTA_AV_AP_START_EVT:
   1247       return "AP_START";
   1248     case BTA_AV_AP_STOP_EVT:
   1249       return "AP_STOP";
   1250     case BTA_AV_API_RECONFIG_EVT:
   1251       return "API_RECONFIG";
   1252     case BTA_AV_API_PROTECT_REQ_EVT:
   1253       return "API_PROTECT_REQ";
   1254     case BTA_AV_API_PROTECT_RSP_EVT:
   1255       return "API_PROTECT_RSP";
   1256     case BTA_AV_API_RC_OPEN_EVT:
   1257       return "API_RC_OPEN";
   1258     case BTA_AV_SRC_DATA_READY_EVT:
   1259       return "SRC_DATA_READY";
   1260     case BTA_AV_CI_SETCONFIG_OK_EVT:
   1261       return "CI_SETCONFIG_OK";
   1262     case BTA_AV_CI_SETCONFIG_FAIL_EVT:
   1263       return "CI_SETCONFIG_FAIL";
   1264     case BTA_AV_SDP_DISC_OK_EVT:
   1265       return "SDP_DISC_OK";
   1266     case BTA_AV_SDP_DISC_FAIL_EVT:
   1267       return "SDP_DISC_FAIL";
   1268     case BTA_AV_STR_DISC_OK_EVT:
   1269       return "STR_DISC_OK";
   1270     case BTA_AV_STR_DISC_FAIL_EVT:
   1271       return "STR_DISC_FAIL";
   1272     case BTA_AV_STR_GETCAP_OK_EVT:
   1273       return "STR_GETCAP_OK";
   1274     case BTA_AV_STR_GETCAP_FAIL_EVT:
   1275       return "STR_GETCAP_FAIL";
   1276     case BTA_AV_STR_OPEN_OK_EVT:
   1277       return "STR_OPEN_OK";
   1278     case BTA_AV_STR_OPEN_FAIL_EVT:
   1279       return "STR_OPEN_FAIL";
   1280     case BTA_AV_STR_START_OK_EVT:
   1281       return "STR_START_OK";
   1282     case BTA_AV_STR_START_FAIL_EVT:
   1283       return "STR_START_FAIL";
   1284     case BTA_AV_STR_CLOSE_EVT:
   1285       return "STR_CLOSE";
   1286     case BTA_AV_STR_CONFIG_IND_EVT:
   1287       return "STR_CONFIG_IND";
   1288     case BTA_AV_STR_SECURITY_IND_EVT:
   1289       return "STR_SECURITY_IND";
   1290     case BTA_AV_STR_SECURITY_CFM_EVT:
   1291       return "STR_SECURITY_CFM";
   1292     case BTA_AV_STR_WRITE_CFM_EVT:
   1293       return "STR_WRITE_CFM";
   1294     case BTA_AV_STR_SUSPEND_CFM_EVT:
   1295       return "STR_SUSPEND_CFM";
   1296     case BTA_AV_STR_RECONFIG_CFM_EVT:
   1297       return "STR_RECONFIG_CFM";
   1298     case BTA_AV_AVRC_TIMER_EVT:
   1299       return "AVRC_TIMER";
   1300     case BTA_AV_AVDT_CONNECT_EVT:
   1301       return "AVDT_CONNECT";
   1302     case BTA_AV_AVDT_DISCONNECT_EVT:
   1303       return "AVDT_DISCONNECT";
   1304     case BTA_AV_ROLE_CHANGE_EVT:
   1305       return "ROLE_CHANGE";
   1306     case BTA_AV_AVDT_DELAY_RPT_EVT:
   1307       return "AVDT_DELAY_RPT";
   1308     case BTA_AV_ACP_CONNECT_EVT:
   1309       return "ACP_CONNECT";
   1310 
   1311     case BTA_AV_API_ENABLE_EVT:
   1312       return "API_ENABLE";
   1313     case BTA_AV_API_REGISTER_EVT:
   1314       return "API_REG";
   1315     case BTA_AV_API_DEREGISTER_EVT:
   1316       return "API_DEREG";
   1317     case BTA_AV_API_DISCONNECT_EVT:
   1318       return "API_DISCNT";
   1319     case BTA_AV_CI_SRC_DATA_READY_EVT:
   1320       return "CI_DATA_READY";
   1321     case BTA_AV_SIG_CHG_EVT:
   1322       return "SIG_CHG";
   1323     case BTA_AV_SIGNALLING_TIMER_EVT:
   1324       return "SIGNALLING_TIMER";
   1325     case BTA_AV_SDP_AVRC_DISC_EVT:
   1326       return "SDP_AVRC_DISC";
   1327     case BTA_AV_AVRC_CLOSE_EVT:
   1328       return "AVRC_CLOSE";
   1329     case BTA_AV_AVRC_BROWSE_OPEN_EVT:
   1330       return "AVRC_BROWSE_OPEN";
   1331     case BTA_AV_AVRC_BROWSE_CLOSE_EVT:
   1332       return "AVRC_BROWSE_CLOSE";
   1333     case BTA_AV_CONN_CHG_EVT:
   1334       return "CONN_CHG";
   1335     case BTA_AV_DEREG_COMP_EVT:
   1336       return "DEREG_COMP";
   1337 #if (AVDT_REPORTING == TRUE)
   1338     case BTA_AV_AVDT_RPT_CONN_EVT:
   1339       return "RPT_CONN";
   1340 #endif
   1341     case BTA_AV_API_START_EVT:
   1342       return "API_START";
   1343     case BTA_AV_API_STOP_EVT:
   1344       return "API_STOP";
   1345     default:
   1346       return "unknown";
   1347   }
   1348 }
   1349