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