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