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