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