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