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