• 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 file contains action functions for advanced audio/video stream
22  *  state machine. these functions are shared by both audio and video
23  *  streams.
24  *
25  ******************************************************************************/
26 
27 #include "bt_target.h"
28 #if defined(BTA_AV_INCLUDED) && (BTA_AV_INCLUDED == TRUE)
29 
30 #include <string.h>
31 #include "bta_av_int.h"
32 #include "avdt_api.h"
33 #include "bd.h"
34 #include "utl.h"
35 #include "l2c_api.h"
36 #include "l2cdefs.h"
37 #if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
38 #include "bta_ar_api.h"
39 #endif
40 
41 /*****************************************************************************
42 **  Constants
43 *****************************************************************************/
44 
45 /* the delay time in milliseconds to start service discovery on AVRCP */
46 #ifndef BTA_AV_RC_DISC_TIME_VAL
47 #define BTA_AV_RC_DISC_TIME_VAL     3500
48 #endif
49 
50 /* the timer in milliseconds to guard against link busy and AVDT_CloseReq failed to be sent */
51 #ifndef BTA_AV_CLOSE_REQ_TIME_VAL
52 #define BTA_AV_CLOSE_REQ_TIME_VAL   4000
53 #endif
54 
55 /* number to retry on reconfigure failure - some headsets requirs this number to be more than 1 */
56 #ifndef BTA_AV_RECONFIG_RETRY
57 #define BTA_AV_RECONFIG_RETRY       6
58 #endif
59 
60 /* state machine states */
61 enum
62 {
63     BTA_AV_INIT_SST,
64     BTA_AV_INCOMING_SST,
65     BTA_AV_OPENING_SST,
66     BTA_AV_OPEN_SST,
67     BTA_AV_RCFG_SST,
68     BTA_AV_CLOSING_SST
69 };
70 
71 
72 /* the call out functions for audio stream */
73 const tBTA_AV_CO_FUNCTS bta_av_a2d_cos =
74 {
75     bta_av_co_audio_init,
76     bta_av_co_audio_disc_res,
77     bta_av_co_audio_getconfig,
78     bta_av_co_audio_setconfig,
79     bta_av_co_audio_open,
80     bta_av_co_audio_close,
81     bta_av_co_audio_start,
82     bta_av_co_audio_stop,
83     bta_av_co_audio_src_data_path,
84     bta_av_co_audio_delay
85 };
86 
87 /* ssm action functions for audio stream */
88 const tBTA_AV_SACT bta_av_a2d_action[] =
89 {
90     bta_av_do_disc_a2d,     /* BTA_AV_DO_DISC  */
91     bta_av_cleanup,         /* BTA_AV_CLEANUP */
92     bta_av_free_sdb,        /* BTA_AV_FREE_SDB */
93     bta_av_config_ind,      /* BTA_AV_CONFIG_IND */
94     bta_av_disconnect_req,  /* BTA_AV_DISCONNECT_REQ */
95     bta_av_security_req,    /* BTA_AV_SECURITY_REQ */
96     bta_av_security_rsp,    /* BTA_AV_SECURITY_RSP */
97     bta_av_setconfig_rsp,   /* BTA_AV_SETCONFIG_RSP */
98     bta_av_st_rc_timer,     /* BTA_AV_ST_RC_TIMER */
99     bta_av_str_opened,      /* BTA_AV_STR_OPENED */
100     bta_av_security_ind,    /* BTA_AV_SECURITY_IND */
101     bta_av_security_cfm,    /* BTA_AV_SECURITY_CFM */
102     bta_av_do_close,        /* BTA_AV_DO_CLOSE */
103     bta_av_connect_req,     /* BTA_AV_CONNECT_REQ */
104     bta_av_sdp_failed,      /* BTA_AV_SDP_FAILED */
105     bta_av_disc_results,    /* BTA_AV_DISC_RESULTS */
106     bta_av_disc_res_as_acp, /* BTA_AV_DISC_RES_AS_ACP */
107     bta_av_open_failed,     /* BTA_AV_OPEN_FAILED */
108     bta_av_getcap_results,  /* BTA_AV_GETCAP_RESULTS */
109     bta_av_setconfig_rej,   /* BTA_AV_SETCONFIG_REJ */
110     bta_av_discover_req,    /* BTA_AV_DISCOVER_REQ */
111     bta_av_conn_failed,     /* BTA_AV_CONN_FAILED */
112     bta_av_do_start,        /* BTA_AV_DO_START */
113     bta_av_str_stopped,     /* BTA_AV_STR_STOPPED */
114     bta_av_reconfig,        /* BTA_AV_RECONFIG */
115     bta_av_data_path,       /* BTA_AV_DATA_PATH */
116     bta_av_start_ok,        /* BTA_AV_START_OK */
117     bta_av_start_failed,    /* BTA_AV_START_FAILED */
118     bta_av_str_closed,      /* BTA_AV_STR_CLOSED */
119     bta_av_clr_cong,        /* BTA_AV_CLR_CONG */
120     bta_av_suspend_cfm,     /* BTA_AV_SUSPEND_CFM */
121     bta_av_rcfg_str_ok,     /* BTA_AV_RCFG_STR_OK */
122     bta_av_rcfg_failed,     /* BTA_AV_RCFG_FAILED */
123     bta_av_rcfg_connect,    /* BTA_AV_RCFG_CONNECT */
124     bta_av_rcfg_discntd,    /* BTA_AV_RCFG_DISCNTD */
125     bta_av_suspend_cont,    /* BTA_AV_SUSPEND_CONT */
126     bta_av_rcfg_cfm,        /* BTA_AV_RCFG_CFM */
127     bta_av_rcfg_open,       /* BTA_AV_RCFG_OPEN */
128     bta_av_security_rej,    /* BTA_AV_SECURITY_REJ */
129     bta_av_open_rc,         /* BTA_AV_OPEN_RC */
130     bta_av_chk_2nd_start,   /* BTA_AV_CHK_2ND_START */
131     bta_av_save_caps,       /* BTA_AV_SAVE_CAPS */
132     bta_av_set_use_rc,      /* BTA_AV_SET_USE_RC */
133     bta_av_cco_close,       /* BTA_AV_CCO_CLOSE */
134     bta_av_switch_role,     /* BTA_AV_SWITCH_ROLE */
135     bta_av_role_res,        /* BTA_AV_ROLE_RES */
136     bta_av_delay_co,        /* BTA_AV_DELAY_CO */
137     bta_av_open_at_inc,     /* BTA_AV_OPEN_AT_INC */
138     NULL
139 };
140 
141 /* these tables translate AVDT events to SSM events */
142 static const UINT16 bta_av_stream_evt_ok[] = {
143     BTA_AV_STR_DISC_OK_EVT,         /* AVDT_DISCOVER_CFM_EVT */
144     BTA_AV_STR_GETCAP_OK_EVT,       /* AVDT_GETCAP_CFM_EVT */
145     BTA_AV_STR_OPEN_OK_EVT,         /* AVDT_OPEN_CFM_EVT */
146     BTA_AV_STR_OPEN_OK_EVT,         /* AVDT_OPEN_IND_EVT */
147     BTA_AV_STR_CONFIG_IND_EVT,      /* AVDT_CONFIG_IND_EVT */
148     BTA_AV_STR_START_OK_EVT,        /* AVDT_START_CFM_EVT */
149     BTA_AV_STR_START_OK_EVT,        /* AVDT_START_IND_EVT */
150     BTA_AV_STR_SUSPEND_CFM_EVT,     /* AVDT_SUSPEND_CFM_EVT */
151     BTA_AV_STR_SUSPEND_CFM_EVT,     /* AVDT_SUSPEND_IND_EVT */
152     BTA_AV_STR_CLOSE_EVT,           /* AVDT_CLOSE_CFM_EVT */
153     BTA_AV_STR_CLOSE_EVT,           /* AVDT_CLOSE_IND_EVT */
154     BTA_AV_STR_RECONFIG_CFM_EVT,    /* AVDT_RECONFIG_CFM_EVT */
155     0,                              /* AVDT_RECONFIG_IND_EVT */
156     BTA_AV_STR_SECURITY_CFM_EVT,    /* AVDT_SECURITY_CFM_EVT */
157     BTA_AV_STR_SECURITY_IND_EVT,    /* AVDT_SECURITY_IND_EVT */
158     BTA_AV_STR_WRITE_CFM_EVT,       /* AVDT_WRITE_CFM_EVT */
159     BTA_AV_AVDT_CONNECT_EVT,        /* AVDT_CONNECT_IND_EVT */
160     BTA_AV_AVDT_DISCONNECT_EVT,     /* AVDT_DISCONNECT_IND_EVT */
161 #if (AVDT_REPORTING == TRUE)
162     BTA_AV_AVDT_RPT_CONN_EVT,       /* AVDT_REPORT_CONN_EVT */
163     BTA_AV_AVDT_RPT_CONN_EVT,       /* AVDT_REPORT_DISCONN_EVT */
164 #endif
165     BTA_AV_AVDT_DELAY_RPT_EVT,      /* AVDT_DELAY_REPORT_EVT */
166     0                               /* AVDT_DELAY_REPORT_CFM_EVT */
167 };
168 
169 static const UINT16 bta_av_stream_evt_fail[] = {
170     BTA_AV_STR_DISC_FAIL_EVT,       /* AVDT_DISCOVER_CFM_EVT */
171     BTA_AV_STR_GETCAP_FAIL_EVT,     /* AVDT_GETCAP_CFM_EVT */
172     BTA_AV_STR_OPEN_FAIL_EVT,       /* AVDT_OPEN_CFM_EVT */
173     BTA_AV_STR_OPEN_OK_EVT,         /* AVDT_OPEN_IND_EVT */
174     BTA_AV_STR_CONFIG_IND_EVT,      /* AVDT_CONFIG_IND_EVT */
175     BTA_AV_STR_START_FAIL_EVT,      /* AVDT_START_CFM_EVT */
176     BTA_AV_STR_START_OK_EVT,        /* AVDT_START_IND_EVT */
177     BTA_AV_STR_SUSPEND_CFM_EVT,     /* AVDT_SUSPEND_CFM_EVT */
178     BTA_AV_STR_SUSPEND_CFM_EVT,     /* AVDT_SUSPEND_IND_EVT */
179     BTA_AV_STR_CLOSE_EVT,           /* AVDT_CLOSE_CFM_EVT */
180     BTA_AV_STR_CLOSE_EVT,           /* AVDT_CLOSE_IND_EVT */
181     BTA_AV_STR_RECONFIG_CFM_EVT,    /* AVDT_RECONFIG_CFM_EVT */
182     0,                              /* AVDT_RECONFIG_IND_EVT */
183     BTA_AV_STR_SECURITY_CFM_EVT,    /* AVDT_SECURITY_CFM_EVT */
184     BTA_AV_STR_SECURITY_IND_EVT,    /* AVDT_SECURITY_IND_EVT */
185     BTA_AV_STR_WRITE_CFM_EVT,       /* AVDT_WRITE_CFM_EVT */
186     BTA_AV_AVDT_CONNECT_EVT,        /* AVDT_CONNECT_IND_EVT */
187     BTA_AV_AVDT_DISCONNECT_EVT,     /* AVDT_DISCONNECT_IND_EVT */
188 #if (AVDT_REPORTING == TRUE)
189     BTA_AV_AVDT_RPT_CONN_EVT,       /* AVDT_REPORT_CONN_EVT */
190     BTA_AV_AVDT_RPT_CONN_EVT,       /* AVDT_REPORT_DISCONN_EVT */
191 #endif
192     BTA_AV_AVDT_DELAY_RPT_EVT,      /* AVDT_DELAY_REPORT_EVT */
193     0                               /* AVDT_DELAY_REPORT_CFM_EVT */
194 };
195 
196 static void bta_av_stream0_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data);
197 static void bta_av_stream1_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data);
198 #if BTA_AV_NUM_STRS > 2
199 static void bta_av_stream2_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data);
200 #endif
201 #if BTA_AV_NUM_STRS > 3
202 static void bta_av_stream3_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data);
203 #endif
204 #if BTA_AV_NUM_STRS > 4
205 static void bta_av_stream4_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data);
206 #endif
207 #if BTA_AV_NUM_STRS > 5
208 static void bta_av_stream5_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data);
209 #endif
210 /* the array of callback functions to receive events from AVDT control channel */
211 tAVDT_CTRL_CBACK * const bta_av_dt_cback[] =
212 {
213     bta_av_stream0_cback
214     ,bta_av_stream1_cback
215 #if BTA_AV_NUM_STRS > 2
216     ,bta_av_stream2_cback
217 #endif
218 #if BTA_AV_NUM_STRS > 3
219     ,bta_av_stream3_cback
220 #endif
221 #if BTA_AV_NUM_STRS > 4
222     ,bta_av_stream4_cback
223 #endif
224 #if BTA_AV_NUM_STRS > 5
225     ,bta_av_stream5_cback
226 #endif
227 };
228 
229 /*******************************************************************************
230 **
231 ** Function         bta_av_save_addr
232 **
233 ** Description      copy the bd_addr and maybe reset the supported flags
234 **
235 **
236 ** Returns          void
237 **
238 *******************************************************************************/
bta_av_save_addr(tBTA_AV_SCB * p_scb,const BD_ADDR b)239 static void bta_av_save_addr(tBTA_AV_SCB *p_scb, const BD_ADDR b)
240 {
241     APPL_TRACE_DEBUG2("bta_av_save_addr r:%d, s:%d",
242         p_scb->recfg_sup, p_scb->suspend_sup);
243     if(bdcmp(p_scb->peer_addr, b) != 0)
244     {
245         APPL_TRACE_ERROR0("reset flags");
246         /* a new addr, reset the supported flags */
247         p_scb->recfg_sup    = TRUE;
248         p_scb->suspend_sup  = TRUE;
249     }
250 
251     /* do this copy anyway, just in case the first addr matches
252      * the control block one by accident */
253     bdcpy(p_scb->peer_addr, b);
254 }
255 
256 /*******************************************************************************
257 **
258 ** Function         bta_av_st_rc_timer
259 **
260 ** Description      start the AVRC timer if no RC connection & CT is supported &
261 **                  RC is used or
262 **                  as ACP (we do not really know if we want AVRC)
263 **
264 ** Returns          void
265 **
266 *******************************************************************************/
bta_av_st_rc_timer(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)267 void bta_av_st_rc_timer(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
268 {
269 
270     APPL_TRACE_DEBUG2("bta_av_st_rc_timer rc_handle:%d, use_rc: %d",
271         p_scb->rc_handle, p_scb->use_rc);
272     /* for outgoing RC connection as INT/CT */
273     if( (p_scb->rc_handle == BTA_AV_RC_HANDLE_NONE) &&
274         /*(bta_av_cb.features & BTA_AV_FEAT_RCCT) &&*/
275         (p_scb->use_rc == TRUE || (p_scb->role & BTA_AV_ROLE_AD_ACP)) )
276     {
277         if ((p_scb->wait & BTA_AV_WAIT_ROLE_SW_BITS) == 0)
278             bta_sys_start_timer(&p_scb->timer, BTA_AV_AVRC_TIMER_EVT, BTA_AV_RC_DISC_TIME_VAL);
279         else
280             p_scb->wait |= BTA_AV_WAIT_CHECK_RC;
281     }
282 
283 }
284 
285 /*******************************************************************************
286 **
287 ** Function         bta_av_next_getcap
288 **
289 ** Description      The function gets the capabilities of the next available
290 **                  stream found in the discovery results.
291 **
292 ** Returns          TRUE if we sent request to AVDT, FALSE otherwise.
293 **
294 *******************************************************************************/
bta_av_next_getcap(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)295 static BOOLEAN bta_av_next_getcap(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
296 {
297     int     i;
298     tAVDT_GETCAP_REQ    *p_req;
299     BOOLEAN     sent_cmd = FALSE;
300 
301     for (i = p_scb->sep_info_idx; i < p_scb->num_seps; i++)
302     {
303         /* steam not in use, is a sink, and is the right media type (audio/video) */
304         if ((p_scb->sep_info[i].in_use == FALSE) &&
305             (p_scb->sep_info[i].tsep == AVDT_TSEP_SNK) &&
306             (p_scb->sep_info[i].media_type == p_scb->media_type))
307         {
308             p_scb->sep_info_idx = i;
309 
310             /* we got a stream; get its capabilities */
311             if (p_scb->p_cap == NULL)
312             {
313                 p_scb->p_cap = (tAVDT_CFG *) GKI_getbuf(sizeof(tAVDT_CFG));
314             }
315             if (p_scb->p_cap == NULL)
316             {
317                 i = p_scb->num_seps;
318                 break;
319             }
320             if (p_scb->avdt_version >= AVDT_VERSION_SYNC)
321             {
322                 p_req = AVDT_GetAllCapReq;
323             }
324             else
325             {
326                 p_req = AVDT_GetCapReq;
327             }
328             (*p_req)(p_scb->peer_addr,
329                            p_scb->sep_info[i].seid,
330                            p_scb->p_cap, bta_av_dt_cback[p_scb->hdi]);
331             sent_cmd = TRUE;
332             break;
333         }
334     }
335 
336     /* if no streams available then stream open fails */
337     if (!sent_cmd)
338     {
339         bta_av_ssm_execute(p_scb, BTA_AV_STR_GETCAP_FAIL_EVT, p_data);
340     }
341 
342     return sent_cmd;
343 
344 }
345 
346 /*******************************************************************************
347 **
348 ** Function         bta_av_proc_stream_evt
349 **
350 ** Description      Utility function to compose stream events.
351 **
352 ** Returns          void
353 **
354 *******************************************************************************/
bta_av_proc_stream_evt(UINT8 handle,BD_ADDR bd_addr,UINT8 event,tAVDT_CTRL * p_data,int index)355 void bta_av_proc_stream_evt(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data, int index)
356 {
357     tBTA_AV_STR_MSG     *p_msg;
358     UINT16              sec_len = 0;
359     tBTA_AV_SCB         *p_scb = bta_av_cb.p_scb[index];
360     int                 xx;
361 
362     if (p_data)
363     {
364         if (event == AVDT_SECURITY_IND_EVT)
365         {
366             sec_len = (p_data->security_ind.len < BTA_AV_SECURITY_MAX_LEN) ?
367                        p_data->security_ind.len : BTA_AV_SECURITY_MAX_LEN;
368         }
369         else if (event == AVDT_SECURITY_CFM_EVT && p_data->hdr.err_code == 0)
370         {
371             sec_len = (p_data->security_cfm.len < BTA_AV_SECURITY_MAX_LEN) ?
372                        p_data->security_cfm.len : BTA_AV_SECURITY_MAX_LEN;
373         }
374     }
375 
376     if (p_scb && (p_msg = (tBTA_AV_STR_MSG *) GKI_getbuf((UINT16) (sizeof(tBTA_AV_STR_MSG) + sec_len))) != NULL)
377     {
378 
379         /* copy event data, bd addr, and handle to event message buffer */
380         p_msg->hdr.offset = 0;
381 
382         if (bd_addr != NULL)
383         {
384             bdcpy(p_msg->bd_addr, bd_addr);
385             APPL_TRACE_DEBUG6("  bd_addr:%02x-%02x-%02x-%02x-%02x-%02x",
386                           bd_addr[0], bd_addr[1],
387                           bd_addr[2], bd_addr[3],
388                           bd_addr[4], bd_addr[5]);
389         }
390 
391         if (p_data != NULL)
392         {
393             memcpy(&p_msg->msg, p_data, sizeof (tAVDT_CTRL));
394             /* copy config params to event message buffer */
395             switch (event)
396             {
397             case AVDT_CONFIG_IND_EVT:
398             /* We might have 2 SEP signallings(A2DP + VDP) with one peer device on one L2CAP.
399              * If we already have a signalling connection with the bd_addr and the streaming
400              * SST is at INIT state, change it to INCOMING state to handle the signalling
401              * from the 2nd SEP.                                                                */
402             if ((bta_av_find_lcb(bd_addr, BTA_AV_LCB_FIND) != NULL) && (bta_av_is_scb_init(p_scb)))
403             {
404                 bta_av_set_scb_sst_incoming (p_scb);
405 
406                 /* When ACP_CONNECT_EVT was received, we put first available scb to incoming state.
407                  * Later when we receive AVDT_CONFIG_IND_EVT, we use a new p_scb and set its state to
408                  * incoming which we do it above.
409                  * We also have to set the old p_scb state to init to be used later             */
410                 for (xx = 0; xx < BTA_AV_NUM_STRS; xx++)
411                 {
412                     if ((bta_av_cb.p_scb[xx]) && (xx != index))
413                     {
414                         if (bta_av_cb.p_scb[xx]->state == BTA_AV_INCOMING_SST)
415                         {
416                             bta_av_cb.p_scb[xx]->state = BTA_AV_INIT_SST;
417                             bta_av_cb.p_scb[xx]->coll_mask = 0;
418                             break;
419                         }
420                     }
421                 }
422             }
423 
424             memcpy(&p_msg->cfg, p_data->config_ind.p_cfg, sizeof(tAVDT_CFG));
425             break;
426 
427             case AVDT_SECURITY_IND_EVT:
428                 p_msg->msg.security_ind.p_data = (UINT8 *) (p_msg + 1);
429                 memcpy(p_msg->msg.security_ind.p_data, p_data->security_ind.p_data, sec_len);
430                 break;
431 
432             case AVDT_SECURITY_CFM_EVT:
433                 p_msg->msg.security_cfm.p_data = (UINT8 *) (p_msg + 1);
434                 if (p_data->hdr.err_code == 0)
435                 {
436                     memcpy(p_msg->msg.security_cfm.p_data, p_data->security_cfm.p_data, sec_len);
437                 }
438                 break;
439             case AVDT_SUSPEND_IND_EVT:
440                     p_msg->msg.hdr.err_code = 0;
441                 break;
442 
443             default:
444                 break;
445             }
446         }
447         else
448             p_msg->msg.hdr.err_code = 0;
449 
450         /* look up application event */
451         if ((p_data == NULL) || (p_data->hdr.err_code == 0))
452         {
453             p_msg->hdr.event = bta_av_stream_evt_ok[event];
454         }
455         else
456         {
457             p_msg->hdr.event = bta_av_stream_evt_fail[event];
458         }
459 
460         p_msg->initiator = FALSE;
461         if (event == AVDT_SUSPEND_CFM_EVT)
462             p_msg->initiator = TRUE;
463 
464         APPL_TRACE_EVENT1("hndl:x%x", p_scb->hndl);
465         p_msg->hdr.layer_specific = p_scb->hndl;
466         p_msg->handle   = handle;
467         p_msg->avdt_event = event;
468         bta_sys_sendmsg(p_msg);
469     }
470 
471 /* coverity[var_deref_model]: Variable "p_data" tracked as NULL was passed to function "bta_av_conn_cback" that dereferences it.
472  * false-positive: bta_av_conn_cback only processes AVDT_CONNECT_IND_EVT and AVDT_DISCONNECT_IND_EVT event
473  *                 these 2 events always have associated p_data
474  */
475     bta_av_conn_cback(handle, bd_addr, event, p_data);
476 }
477 
478 /*******************************************************************************
479 **
480 ** Function         bta_av_stream0_cback
481 **
482 ** Description      This is the AVDTP callback function for stream events.
483 **
484 ** Returns          void
485 **
486 *******************************************************************************/
bta_av_stream0_cback(UINT8 handle,BD_ADDR bd_addr,UINT8 event,tAVDT_CTRL * p_data)487 static void bta_av_stream0_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
488 {
489     APPL_TRACE_EVENT2("bta_av_stream0_cback avdt_handle: %d event=0x%x", handle, event);
490     bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 0);
491 }
492 
493 /*******************************************************************************
494 **
495 ** Function         bta_av_stream1_cback
496 **
497 ** Description      This is the AVDTP callback function for stream events.
498 **
499 ** Returns          void
500 **
501 *******************************************************************************/
bta_av_stream1_cback(UINT8 handle,BD_ADDR bd_addr,UINT8 event,tAVDT_CTRL * p_data)502 static void bta_av_stream1_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
503 {
504     APPL_TRACE_EVENT2("bta_av_stream1_cback avdt_handle: %d event=0x%x", handle, event);
505     bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 1);
506 }
507 
508 #if BTA_AV_NUM_STRS > 2
509 /*******************************************************************************
510 **
511 ** Function         bta_av_stream2_cback
512 **
513 ** Description      This is the AVDTP callback function for stream events.
514 **
515 ** Returns          void
516 **
517 *******************************************************************************/
bta_av_stream2_cback(UINT8 handle,BD_ADDR bd_addr,UINT8 event,tAVDT_CTRL * p_data)518 static void bta_av_stream2_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
519 {
520     APPL_TRACE_EVENT2("bta_av_stream2_cback avdt_handle: %d event=0x%x", handle, event);
521     bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 2);
522 }
523 #endif
524 
525 #if BTA_AV_NUM_STRS > 3
526 /*******************************************************************************
527 **
528 ** Function         bta_av_stream3_cback
529 **
530 ** Description      This is the AVDTP callback function for stream events.
531 **
532 ** Returns          void
533 **
534 *******************************************************************************/
bta_av_stream3_cback(UINT8 handle,BD_ADDR bd_addr,UINT8 event,tAVDT_CTRL * p_data)535 static void bta_av_stream3_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
536 {
537     APPL_TRACE_EVENT2("bta_av_stream3_cback avdt_handle: %d event=0x%x", handle, event);
538     bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 3);
539 }
540 #endif
541 
542 /*******************************************************************************
543 **
544 ** Function         bta_av_stream4_cback
545 **
546 ** Description      This is the AVDTP callback function for stream events.
547 **
548 ** Returns          void
549 **
550 *******************************************************************************/
551 #if BTA_AV_NUM_STRS > 4
bta_av_stream4_cback(UINT8 handle,BD_ADDR bd_addr,UINT8 event,tAVDT_CTRL * p_data)552 static void bta_av_stream4_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
553 {
554     APPL_TRACE_EVENT2("bta_av_stream4_cback avdt_handle: %d event=0x%x", handle, event);
555     bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 4);
556 }
557 #endif
558 
559 /*******************************************************************************
560 **
561 ** Function         bta_av_stream5_cback
562 **
563 ** Description      This is the AVDTP callback function for stream events.
564 **
565 ** Returns          void
566 **
567 *******************************************************************************/
568 #if BTA_AV_NUM_STRS > 5
bta_av_stream5_cback(UINT8 handle,BD_ADDR bd_addr,UINT8 event,tAVDT_CTRL * p_data)569 static void bta_av_stream5_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
570 {
571     APPL_TRACE_EVENT2("bta_av_stream5_cback avdt_handle: %d event=0x%x", handle, event);
572     bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 5);
573 }
574 #endif
575 
576 /*******************************************************************************
577 **
578 ** Function         bta_av_a2d_sdp_cback
579 **
580 ** Description      A2DP service discovery callback.
581 **
582 ** Returns          void
583 **
584 *******************************************************************************/
bta_av_a2d_sdp_cback(BOOLEAN found,tA2D_Service * p_service)585 static void bta_av_a2d_sdp_cback(BOOLEAN found, tA2D_Service *p_service)
586 {
587     tBTA_AV_SDP_RES *p_msg;
588     tBTA_AV_SCB     *p_scb;
589 
590     if ((p_msg = (tBTA_AV_SDP_RES *) GKI_getbuf(sizeof(tBTA_AV_SDP_RES))) != NULL)
591     {
592         p_msg->hdr.event = (found) ? BTA_AV_SDP_DISC_OK_EVT : BTA_AV_SDP_DISC_FAIL_EVT;
593 
594         p_scb = bta_av_hndl_to_scb(bta_av_cb.handle);
595         if (p_scb)
596         {
597             if (found && (p_service != NULL))
598                 p_scb->avdt_version = p_service->avdt_version;
599             else
600                 p_scb->avdt_version = 0x00;
601 
602             p_msg->hdr.layer_specific = bta_av_cb.handle;
603             bta_sys_sendmsg(p_msg);
604         }
605         else
606         {
607             APPL_TRACE_ERROR1 ("bta_av_a2d_sdp_cback, no scb found for handle(0x%x)", bta_av_cb.handle);
608         }
609     }
610 }
611 
612 /*******************************************************************************
613 **
614 ** Function         bta_av_adjust_seps_idx
615 **
616 ** Description      adjust the sep_idx
617 **
618 ** Returns
619 **
620 *******************************************************************************/
bta_av_adjust_seps_idx(tBTA_AV_SCB * p_scb)621 static void bta_av_adjust_seps_idx(tBTA_AV_SCB *p_scb)
622 {
623     int             xx;
624 
625     APPL_TRACE_DEBUG1("bta_av_adjust_seps_idx codec_type: %d", p_scb->codec_type);
626     for(xx=0; xx<BTA_AV_MAX_SEPS; xx++)
627     {
628         APPL_TRACE_DEBUG2("av_handle: %d codec_type: %d",
629             p_scb->seps[xx].av_handle, p_scb->seps[xx].codec_type);
630         if(p_scb->seps[xx].av_handle && p_scb->codec_type == p_scb->seps[xx].codec_type)
631         {
632             p_scb->sep_idx      = xx;
633             p_scb->avdt_handle  = p_scb->seps[xx].av_handle;
634             break;
635         }
636     }
637 }
638 
639 /*******************************************************************************
640 **
641 ** Function         bta_av_switch_role
642 **
643 ** Description      Switch role was not started and a timer was started.
644 **                  another attempt to switch role now - still opening.
645 **
646 ** Returns          void
647 **
648 *******************************************************************************/
bta_av_switch_role(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)649 void bta_av_switch_role (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
650 {
651     tBTA_AV_RS_RES      switch_res = BTA_AV_RS_NONE;
652     tBTA_AV_API_OPEN  *p_buf = &p_scb->q_info.open;
653 
654     APPL_TRACE_DEBUG1("bta_av_switch_role wait:x%x", p_scb->wait);
655     if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_RES_START)
656         p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RETRY;
657 
658     /* clear the masks set when the timer is started */
659     p_scb->wait &= ~(BTA_AV_WAIT_ROLE_SW_RES_OPEN|BTA_AV_WAIT_ROLE_SW_RES_START);
660 
661     if (p_scb->q_tag == BTA_AV_Q_TAG_OPEN)
662     {
663         if (bta_av_switch_if_needed(p_scb) || !bta_av_link_role_ok(p_scb, A2D_SET_MULTL_BIT))
664         {
665             p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RES_OPEN;
666         }
667         else
668         {
669             /* this should not happen in theory. Just in case...
670              * continue to do_disc_a2d */
671             switch_res = BTA_AV_RS_DONE;
672         }
673     }
674     else
675     {
676         /* report failure on OPEN */
677         switch_res = BTA_AV_RS_FAIL;
678     }
679 
680     if (switch_res != BTA_AV_RS_NONE)
681     {
682         if (bta_av_cb.rs_idx == (p_scb->hdi + 1))
683         {
684             bta_av_cb.rs_idx = 0;
685         }
686         p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_RETRY;
687         p_scb->q_tag = 0;
688         p_buf->switch_res = switch_res;
689         bta_av_do_disc_a2d(p_scb, (tBTA_AV_DATA *)p_buf);
690     }
691 }
692 
693 /*******************************************************************************
694 **
695 ** Function         bta_av_role_res
696 **
697 ** Description      Handle the role changed event
698 **
699 **
700 ** Returns          void
701 **
702 *******************************************************************************/
bta_av_role_res(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)703 void bta_av_role_res (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
704 {
705     BOOLEAN         initiator = FALSE;
706     tBTA_AV_START   start;
707     tBTA_AV_OPEN    av_open;
708 
709     APPL_TRACE_DEBUG3("bta_av_role_res q_tag:%d, wait:x%x, role:x%x", p_scb->q_tag, p_scb->wait, p_scb->role);
710     if (p_scb->role & BTA_AV_ROLE_START_INT)
711         initiator = TRUE;
712 
713     if (p_scb->q_tag == BTA_AV_Q_TAG_START)
714     {
715         if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_STARTED)
716         {
717             p_scb->role &= ~BTA_AV_ROLE_START_INT;
718             p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
719             if (p_data->role_res.hci_status != HCI_SUCCESS)
720             {
721                 bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
722                 /* start failed because of role switch. */
723                 start.chnl   = p_scb->chnl;
724                 start.status = BTA_AV_FAIL_ROLE;
725                 start.hndl   = p_scb->hndl;
726                 start.initiator = initiator;
727                 (*bta_av_cb.p_cback)(BTA_AV_START_EVT, (tBTA_AV *) &start);
728             }
729             else
730             {
731                 bta_av_start_ok(p_scb, p_data);
732             }
733         }
734         else if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_RES_START)
735             p_scb->wait |= BTA_AV_WAIT_ROLE_SW_FAILED;
736     }
737     else if (p_scb->q_tag == BTA_AV_Q_TAG_OPEN)
738     {
739         if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_RES_OPEN)
740         {
741             p_scb->role &= ~BTA_AV_ROLE_START_INT;
742             p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
743 
744             if (p_data->role_res.hci_status != HCI_SUCCESS)
745             {
746                 /* Open failed because of role switch. */
747                 bdcpy(av_open.bd_addr, p_scb->peer_addr);
748                 av_open.chnl   = p_scb->chnl;
749                 av_open.hndl   = p_scb->hndl;
750                 start.status = BTA_AV_FAIL_ROLE;
751                 (*bta_av_cb.p_cback)(BTA_AV_OPEN_EVT, (tBTA_AV *)&av_open);
752             }
753             else
754             {
755                 /* Continue av open process */
756                 p_scb->q_info.open.switch_res = BTA_AV_RS_DONE;
757                 bta_av_do_disc_a2d (p_scb, (tBTA_AV_DATA *)&(p_scb->q_info.open));
758             }
759         }
760         else
761         {
762             APPL_TRACE_WARNING2 ("Unexpected role switch event: q_tag = %d wait = %d", p_scb->q_tag, p_scb->wait);
763         }
764     }
765 
766     APPL_TRACE_DEBUG2("wait:x%x, role:x%x", p_scb->wait, p_scb->role);
767 }
768 
769 /*******************************************************************************
770 **
771 ** Function         bta_av_delay_co
772 **
773 ** Description      Call the delay call-out function to report the delay report
774 **                  from SNK
775 **
776 ** Returns          void
777 **
778 *******************************************************************************/
bta_av_delay_co(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)779 void bta_av_delay_co (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
780 {
781     p_scb->p_cos->delay(p_scb->hndl, p_data->str_msg.msg.delay_rpt_cmd.delay);
782 }
783 
784 /*******************************************************************************
785 **
786 ** Function         bta_av_do_disc_a2d
787 **
788 ** Description      Do service discovery for A2DP.
789 **
790 ** Returns          void
791 **
792 *******************************************************************************/
bta_av_do_disc_a2d(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)793 void bta_av_do_disc_a2d (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
794 {
795     BOOLEAN     ok_continue = FALSE;
796     tA2D_SDP_DB_PARAMS  db_params;
797     UINT16              attr_list[] = {ATTR_ID_SERVICE_CLASS_ID_LIST,
798                                        ATTR_ID_PROTOCOL_DESC_LIST,
799                                        ATTR_ID_BT_PROFILE_DESC_LIST};
800 
801     APPL_TRACE_DEBUG3("bta_av_do_disc_a2d use_rc: %d rs:%d, oc:%d",
802         p_data->api_open.use_rc, p_data->api_open.switch_res, bta_av_cb.audio_open_cnt);
803 
804     memcpy (&(p_scb->open_api), &(p_data->api_open), sizeof(tBTA_AV_API_OPEN));
805 
806     switch(p_data->api_open.switch_res)
807     {
808     case BTA_AV_RS_NONE:
809         if (bta_av_switch_if_needed(p_scb) || !bta_av_link_role_ok(p_scb, A2D_SET_MULTL_BIT))
810         {
811             /* waiting for role switch result. save the api to control block */
812             memcpy(&p_scb->q_info.open, &p_data->api_open, sizeof(tBTA_AV_API_OPEN));
813             p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RES_OPEN;
814             p_scb->q_tag = BTA_AV_Q_TAG_OPEN;
815         }
816         else
817         {
818             ok_continue = TRUE;
819         }
820         break;
821 
822     case BTA_AV_RS_FAIL:
823         /* report a new failure event  */
824         p_scb->open_status = BTA_AV_FAIL_ROLE;
825         bta_av_ssm_execute(p_scb, BTA_AV_SDP_DISC_FAIL_EVT, NULL);
826         break;
827 
828     case BTA_AV_RS_OK:
829         p_data = (tBTA_AV_DATA *)&p_scb->q_info.open;
830         /* continue to open if link role is ok */
831         if (bta_av_link_role_ok(p_scb, A2D_SET_MULTL_BIT))
832         {
833             ok_continue = TRUE;
834         }
835         else
836         {
837             p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RES_OPEN;
838         }
839         break;
840 
841     case BTA_AV_RS_DONE:
842         ok_continue = TRUE;
843         break;
844     }
845 
846     APPL_TRACE_DEBUG3("ok_continue: %d wait:x%x, q_tag: %d", ok_continue, p_scb->wait, p_scb->q_tag);
847     if (!ok_continue)
848         return;
849 
850     /* clear the role switch bits */
851     p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
852 
853     if (p_scb->wait & BTA_AV_WAIT_CHECK_RC)
854     {
855         p_scb->wait &= ~BTA_AV_WAIT_CHECK_RC;
856         bta_sys_start_timer(&p_scb->timer, BTA_AV_AVRC_TIMER_EVT, BTA_AV_RC_DISC_TIME_VAL);
857     }
858 
859     if (bta_av_cb.features & BTA_AV_FEAT_MASTER)
860     {
861     L2CA_SetDesireRole(L2CAP_ROLE_DISALLOW_SWITCH);
862 
863     if (bta_av_cb.audio_open_cnt == 1)
864     {
865         /* there's already an A2DP connection. do not allow switch */
866         bta_sys_clear_default_policy(BTA_ID_AV, HCI_ENABLE_MASTER_SLAVE_SWITCH);
867     }
868     }
869     /* store peer addr other parameters */
870     bta_av_save_addr(p_scb, p_data->api_open.bd_addr);
871     p_scb->sec_mask = p_data->api_open.sec_mask;
872     p_scb->use_rc = p_data->api_open.use_rc;
873 
874     bta_sys_app_open(BTA_ID_AV, p_scb->app_id, p_scb->peer_addr);
875 
876     /* allocate discovery database */
877     if (p_scb->p_disc_db == NULL)
878     {
879         p_scb->p_disc_db = (tSDP_DISCOVERY_DB *) GKI_getbuf(BTA_AV_DISC_BUF_SIZE);
880     }
881 
882     /* only one A2D find service is active at a time */
883     bta_av_cb.handle = p_scb->hndl;
884 
885     if(p_scb->p_disc_db)
886     {
887         /* set up parameters */
888         db_params.db_len = BTA_AV_DISC_BUF_SIZE;
889         db_params.num_attr = 3;
890         db_params.p_db = p_scb->p_disc_db;
891         db_params.p_attrs = attr_list;
892 
893         if(A2D_FindService(UUID_SERVCLASS_AUDIO_SINK, p_scb->peer_addr, &db_params,
894                         bta_av_a2d_sdp_cback) == A2D_SUCCESS)
895         {
896             return;
897         }
898     }
899 
900     /* when the code reaches here, either the DB is NULL
901      * or A2D_FindService is not successful */
902     bta_av_a2d_sdp_cback(FALSE, NULL);
903 }
904 
905 /*******************************************************************************
906 **
907 ** Function         bta_av_cleanup
908 **
909 ** Description      cleanup AV stream control block.
910 **
911 ** Returns          void
912 **
913 *******************************************************************************/
bta_av_cleanup(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)914 void bta_av_cleanup(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
915 {
916     tBTA_AV_CONN_CHG msg;
917     int             xx;
918     UINT8           role = BTA_AV_ROLE_AD_INT;
919 
920     APPL_TRACE_DEBUG0("bta_av_cleanup");
921 
922     /* free any buffers */
923     utl_freebuf((void **) &p_scb->p_cap);
924     utl_freebuf((void **) &p_scb->p_disc_db);
925     p_scb->avdt_version = 0;
926 
927     /* initialize some control block variables */
928     p_scb->open_status = BTA_AV_SUCCESS;
929 
930     /* if de-registering shut everything down */
931     msg.hdr.layer_specific  = p_scb->hndl;
932     p_scb->started  = FALSE;
933     p_scb->cong = FALSE;
934     p_scb->role = role;
935     p_scb->cur_psc_mask = 0;
936     p_scb->wait = 0;
937     p_scb->num_disc_snks = 0;
938     bta_sys_stop_timer(&p_scb->timer);
939     if (p_scb->deregistring)
940     {
941         /* remove stream */
942         for(xx=0; xx<BTA_AV_MAX_SEPS; xx++)
943         {
944             if(p_scb->seps[xx].av_handle)
945                 AVDT_RemoveStream(p_scb->seps[xx].av_handle);
946             p_scb->seps[xx].av_handle = 0;
947         }
948 
949         bta_av_dereg_comp((tBTA_AV_DATA *) &msg);
950     }
951     else
952     {
953         /* report stream closed to main SM */
954         msg.is_up = FALSE;
955         bdcpy(msg.peer_addr, p_scb->peer_addr);
956         bta_av_conn_chg((tBTA_AV_DATA *) &msg);
957     }
958 }
959 
960 /*******************************************************************************
961 **
962 ** Function         bta_av_free_sdb
963 **
964 ** Description      Free service discovery db buffer.
965 **
966 ** Returns          void
967 **
968 *******************************************************************************/
bta_av_free_sdb(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)969 void bta_av_free_sdb(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
970 {
971     utl_freebuf((void **) &p_scb->p_disc_db);
972 }
973 
974 /*******************************************************************************
975 **
976 ** Function         bta_av_config_ind
977 **
978 ** Description      Handle a stream configuration indication from the peer.
979 **
980 ** Returns          void
981 **
982 *******************************************************************************/
bta_av_config_ind(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)983 void bta_av_config_ind (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
984 {
985     tBTA_AV_CI_SETCONFIG setconfig;
986     tAVDT_SEP_INFO       *p_info;
987     tAVDT_CFG            *p_evt_cfg = &p_data->str_msg.cfg;
988     UINT8   psc_mask = (p_evt_cfg->psc_mask | p_scb->cfg.psc_mask);
989 
990     p_scb->avdt_label = p_data->str_msg.msg.hdr.label;
991     memcpy(p_scb->cfg.codec_info, p_evt_cfg->codec_info, AVDT_CODEC_SIZE);
992     p_scb->codec_type = p_evt_cfg->codec_info[BTA_AV_CODEC_TYPE_IDX];
993     bta_av_save_addr(p_scb, p_data->str_msg.bd_addr);
994 
995     /* Clear collision mask */
996     p_scb->coll_mask = 0;
997     bta_sys_stop_timer(&bta_av_cb.acp_sig_tmr);
998 
999     /* if no codec parameters in configuration, fail */
1000     if ((p_evt_cfg->num_codec == 0) ||
1001     /* or the peer requests for a service we do not support */
1002         ((psc_mask != p_scb->cfg.psc_mask) &&
1003         (psc_mask != (p_scb->cfg.psc_mask&~AVDT_PSC_DELAY_RPT))) )
1004     {
1005         setconfig.hndl      = p_scb->hndl; /* we may not need this */
1006         setconfig.err_code  = AVDT_ERR_UNSUP_CFG;
1007         bta_av_ssm_execute(p_scb, BTA_AV_CI_SETCONFIG_FAIL_EVT, (tBTA_AV_DATA *) &setconfig);
1008     }
1009     else
1010     {
1011         p_info = &p_scb->sep_info[0];
1012         p_info->in_use = 0;
1013         p_info->media_type = p_scb->media_type;
1014         p_info->seid = p_data->str_msg.msg.config_ind.int_seid;
1015         p_info->tsep = AVDT_TSEP_SNK;
1016         p_scb->role      |= BTA_AV_ROLE_AD_ACP;
1017         p_scb->cur_psc_mask = p_evt_cfg->psc_mask;
1018         if (bta_av_cb.features & BTA_AV_FEAT_RCTG)
1019             p_scb->use_rc = TRUE;
1020         else
1021             p_scb->use_rc = FALSE;
1022 
1023         p_scb->num_seps  = 1;
1024         p_scb->sep_info_idx = 0;
1025         APPL_TRACE_DEBUG3("bta_av_config_ind: SEID: %d use_rc: %d cur_psc_mask:0x%x", p_info->seid, p_scb->use_rc, p_scb->cur_psc_mask);
1026 
1027         p_scb->p_cos->setcfg(p_scb->hndl, p_scb->codec_type,
1028                              p_evt_cfg->codec_info,
1029                              p_info->seid,
1030                              p_scb->peer_addr,
1031                              p_evt_cfg->num_protect,
1032                              p_evt_cfg->protect_info);
1033     }
1034 }
1035 
1036 /*******************************************************************************
1037 **
1038 ** Function         bta_av_disconnect_req
1039 **
1040 ** Description      Disconnect AVDTP connection.
1041 **
1042 ** Returns          void
1043 **
1044 *******************************************************************************/
bta_av_disconnect_req(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1045 void bta_av_disconnect_req (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1046 {
1047     tBTA_AV_RCB *p_rcb;
1048     APPL_TRACE_DEBUG1("bta_av_disconnect_req conn_lcb: 0x%x", bta_av_cb.conn_lcb);
1049 
1050     bta_sys_stop_timer(&bta_av_cb.sig_tmr);
1051     bta_sys_stop_timer(&p_scb->timer);
1052     if(bta_av_cb.conn_lcb)
1053     {
1054         p_rcb = bta_av_get_rcb_by_shdl((UINT8)(p_scb->hdi + 1));
1055         if (p_rcb)
1056             bta_av_del_rc(p_rcb);
1057         AVDT_DisconnectReq(p_scb->peer_addr, bta_av_dt_cback[p_scb->hdi]);
1058     }
1059     else
1060     {
1061         bta_av_ssm_execute(p_scb, BTA_AV_AVDT_DISCONNECT_EVT, NULL);
1062     }
1063 }
1064 
1065 /*******************************************************************************
1066 **
1067 ** Function         bta_av_security_req
1068 **
1069 ** Description      Send an AVDTP security request.
1070 **
1071 ** Returns          void
1072 **
1073 *******************************************************************************/
bta_av_security_req(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1074 void bta_av_security_req (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1075 {
1076     if (bta_av_cb.features & BTA_AV_FEAT_PROTECT)
1077     {
1078         AVDT_SecurityReq(p_scb->avdt_handle, p_data->api_protect_req.p_data,
1079                          p_data->api_protect_req.len);
1080     }
1081 }
1082 
1083 /*******************************************************************************
1084 **
1085 ** Function         bta_av_security_rsp
1086 **
1087 ** Description      Send an AVDTP security response.
1088 **
1089 ** Returns          void
1090 **
1091 *******************************************************************************/
bta_av_security_rsp(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1092 void bta_av_security_rsp (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1093 {
1094     if (bta_av_cb.features & BTA_AV_FEAT_PROTECT)
1095     {
1096         AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label, p_data->api_protect_rsp.error_code,
1097                          p_data->api_protect_rsp.p_data, p_data->api_protect_rsp.len);
1098     }
1099     else
1100     {
1101         AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label, AVDT_ERR_NSC,
1102                      NULL, 0);
1103     }
1104 }
1105 
1106 /*******************************************************************************
1107 **
1108 ** Function         bta_av_setconfig_rsp
1109 **
1110 ** Description      setconfig is OK
1111 **
1112 ** Returns          void
1113 **
1114 *******************************************************************************/
bta_av_setconfig_rsp(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1115 void bta_av_setconfig_rsp (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1116 {
1117     UINT8   num = p_data->ci_setconfig.num_seid + 1;
1118     UINT8   *p_seid = p_data->ci_setconfig.p_seid;
1119     int     i;
1120 
1121     /* we like this codec_type. find the sep_idx */
1122     bta_av_adjust_seps_idx(p_scb);
1123     APPL_TRACE_DEBUG2("bta_av_setconfig_rsp: sep_idx: %d cur_psc_mask:0x%x", p_scb->sep_idx, p_scb->cur_psc_mask);
1124     AVDT_ConfigRsp(p_scb->avdt_handle, p_scb->avdt_label, p_data->ci_setconfig.err_code,
1125                    p_data->ci_setconfig.category);
1126 
1127     bta_sys_stop_timer(&bta_av_cb.sig_tmr);
1128 
1129     if(p_data->ci_setconfig.err_code == AVDT_SUCCESS)
1130     {
1131         p_scb->wait = BTA_AV_WAIT_ACP_CAPS_ON;
1132         if(p_data->ci_setconfig.recfg_needed)
1133             p_scb->role |= BTA_AV_ROLE_SUSPEND_OPT;
1134         APPL_TRACE_ERROR3("bta_av_setconfig_rsp recfg_needed:%d role:x%x num:%d",
1135             p_data->ci_setconfig.recfg_needed, p_scb->role, num);
1136         /* callout module tells BTA the number of "good" SEPs and their SEIDs.
1137          * getcap on these SEID */
1138         p_scb->num_seps = num;
1139 
1140         if (p_scb->cur_psc_mask & AVDT_PSC_DELAY_RPT)
1141             p_scb->avdt_version = AVDT_VERSION_SYNC;
1142 
1143 
1144         if (p_scb->codec_type == BTA_AV_CODEC_SBC || num > 1)
1145         {
1146             /* if SBC is used by the SNK as INT, discover req is not sent in bta_av_config_ind.
1147              * call disc_res now */
1148             p_scb->p_cos->disc_res(p_scb->hndl, num, num, p_scb->peer_addr);
1149         }
1150         else
1151         {
1152             /* we do not know the peer device and it is using non-SBC codec
1153              * we need to know all the SEPs on SNK */
1154             bta_av_discover_req(p_scb, NULL);
1155             return;
1156         }
1157 
1158         for (i = 1; i < num; i++)
1159         {
1160             APPL_TRACE_DEBUG2("sep_info[%d] SEID: %d", i, p_seid[i-1]);
1161             /* initialize the sep_info[] to get capabilities */
1162             p_scb->sep_info[i].in_use = FALSE;
1163             p_scb->sep_info[i].tsep = AVDT_TSEP_SNK;
1164             p_scb->sep_info[i].media_type = p_scb->media_type;
1165             p_scb->sep_info[i].seid = p_seid[i-1];
1166         }
1167         bta_av_next_getcap(p_scb, p_data);
1168     }
1169 }
1170 
1171 /*******************************************************************************
1172 **
1173 ** Function         bta_av_str_opened
1174 **
1175 ** Description      Stream opened OK (incoming/outgoing).
1176 **
1177 ** Returns          void
1178 **
1179 *******************************************************************************/
bta_av_str_opened(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1180 void bta_av_str_opened (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1181 {
1182     tBTA_AV_CONN_CHG msg;
1183     tBTA_AV_OPEN    open;
1184     UINT8 *p;
1185     UINT16 mtu;
1186 
1187     msg.hdr.layer_specific = p_scb->hndl;
1188     msg.is_up = TRUE;
1189     bdcpy(msg.peer_addr, p_scb->peer_addr);
1190     p_scb->l2c_cid      = AVDT_GetL2CapChannel(p_scb->avdt_handle);
1191     bta_av_conn_chg((tBTA_AV_DATA *) &msg);
1192     /* set the congestion flag, so AV would not send media packets by accident */
1193     p_scb->cong = TRUE;
1194 
1195 
1196     p_scb->stream_mtu = p_data->str_msg.msg.open_ind.peer_mtu - AVDT_MEDIA_HDR_SIZE;
1197     mtu = bta_av_chk_mtu(p_scb, p_scb->stream_mtu);
1198     APPL_TRACE_DEBUG3("bta_av_str_opened l2c_cid: 0x%x stream_mtu: %d mtu: %d",
1199         p_scb->l2c_cid, p_scb->stream_mtu, mtu);
1200     if(mtu == 0 || mtu > p_scb->stream_mtu)
1201         mtu = p_scb->stream_mtu;
1202 
1203     /* Set the media channel as medium priority */
1204     L2CA_SetTxPriority(p_scb->l2c_cid, L2CAP_CHNL_PRIORITY_MEDIUM);
1205     L2CA_SetChnlFlushability (p_scb->l2c_cid, TRUE);
1206 
1207     bta_sys_conn_open(BTA_ID_AV, p_scb->app_id, p_scb->peer_addr);
1208     memset(&p_scb->q_info, 0, sizeof(tBTA_AV_Q_INFO));
1209 
1210     p_scb->l2c_bufs = 0;
1211     p_scb->p_cos->open(p_scb->hndl,
1212         p_scb->codec_type, p_scb->cfg.codec_info, mtu);
1213 
1214     {
1215         /* TODO check if other audio channel is open.
1216          * If yes, check if reconfig is needed
1217          * Rigt now we do not do this kind of checking.
1218          * BTA-AV is INT for 2nd audio connection.
1219          * The application needs to make sure the current codec_info is proper.
1220          * If one audio connection is open and another SNK attempts to connect to AV,
1221          * the connection will be rejected.
1222          */
1223         /* check if other audio channel is started. If yes, start */
1224         bdcpy(open.bd_addr, p_scb->peer_addr);
1225         open.chnl   = p_scb->chnl;
1226         open.hndl   = p_scb->hndl;
1227         open.status = BTA_AV_SUCCESS;
1228         open.starting = bta_av_chk_start(p_scb);
1229         open.edr    = 0;
1230         if( NULL != (p = BTM_ReadRemoteFeatures(p_scb->peer_addr)))
1231         {
1232             if(HCI_EDR_ACL_2MPS_SUPPORTED(p))
1233                 open.edr |= BTA_AV_EDR_2MBPS;
1234             if(HCI_EDR_ACL_3MPS_SUPPORTED(p))
1235                 open.edr |= BTA_AV_EDR_3MBPS;
1236         }
1237 #if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
1238         bta_ar_avdt_conn(BTA_ID_AV, open.bd_addr);
1239 #endif
1240         (*bta_av_cb.p_cback)(BTA_AV_OPEN_EVT, (tBTA_AV *) &open);
1241         if(open.starting)
1242         {
1243             bta_av_ssm_execute(p_scb, BTA_AV_AP_START_EVT, NULL);
1244         }
1245     }
1246 }
1247 
1248 /*******************************************************************************
1249 **
1250 ** Function         bta_av_security_ind
1251 **
1252 ** Description      Handle an AVDTP security indication.
1253 **
1254 ** Returns          void
1255 **
1256 *******************************************************************************/
bta_av_security_ind(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1257 void bta_av_security_ind (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1258 {
1259     tBTA_AV_PROTECT_REQ protect_req;
1260 
1261     p_scb->avdt_label = p_data->str_msg.msg.hdr.label;
1262 
1263     if (bta_av_cb.features & BTA_AV_FEAT_PROTECT)
1264     {
1265         protect_req.chnl    = p_scb->chnl;
1266         protect_req.hndl    = p_scb->hndl;
1267         /*
1268         APPL_TRACE_EVENT1("sec ind handle: x%x", protect_req.hndl);
1269         */
1270         protect_req.p_data  = p_data->str_msg.msg.security_ind.p_data;
1271         protect_req.len     = p_data->str_msg.msg.security_ind.len;
1272 
1273         (*bta_av_cb.p_cback)(BTA_AV_PROTECT_REQ_EVT, (tBTA_AV *) &protect_req);
1274     }
1275     /* app doesn't support security indication; respond with failure */
1276     else
1277     {
1278         AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label, AVDT_ERR_NSC, NULL, 0);
1279     }
1280 }
1281 
1282 /*******************************************************************************
1283 **
1284 ** Function         bta_av_security_cfm
1285 **
1286 ** Description      Handle an AVDTP security confirm.
1287 **
1288 ** Returns          void
1289 **
1290 *******************************************************************************/
bta_av_security_cfm(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1291 void bta_av_security_cfm (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1292 {
1293     tBTA_AV_PROTECT_RSP protect_rsp;
1294 
1295     if (bta_av_cb.features & BTA_AV_FEAT_PROTECT)
1296     {
1297         protect_rsp.chnl    = p_scb->chnl;
1298         protect_rsp.hndl    = p_scb->hndl;
1299         protect_rsp.p_data  = p_data->str_msg.msg.security_cfm.p_data;
1300         protect_rsp.len     = p_data->str_msg.msg.security_cfm.len;
1301         protect_rsp.err_code= p_data->str_msg.msg.hdr.err_code;
1302 
1303         (*bta_av_cb.p_cback)(BTA_AV_PROTECT_RSP_EVT, (tBTA_AV *) &protect_rsp);
1304     }
1305 }
1306 
1307 /*******************************************************************************
1308 **
1309 ** Function         bta_av_do_close
1310 **
1311 ** Description      Close stream.
1312 **
1313 ** Returns          void
1314 **
1315 *******************************************************************************/
bta_av_do_close(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1316 void bta_av_do_close (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1317 {
1318     /* stop stream if started */
1319     if (p_scb->co_started)
1320     {
1321         bta_av_str_stopped(p_scb, NULL);
1322     }
1323     bta_sys_stop_timer(&bta_av_cb.sig_tmr);
1324 
1325     /* close stream */
1326     p_scb->started = FALSE;
1327 
1328     /* drop the buffers queued in L2CAP */
1329     L2CA_FlushChannel (p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
1330 
1331     AVDT_CloseReq(p_scb->avdt_handle);
1332     /* just in case that the link is congested, link is flow controled by peer or
1333      * for whatever reason the the close request can not be sent in time.
1334      * when this timer expires, AVDT_DisconnectReq will be called to disconnect the link
1335      */
1336     bta_sys_start_timer(&p_scb->timer,
1337                         (UINT16)BTA_AV_API_CLOSE_EVT,
1338                         BTA_AV_CLOSE_REQ_TIME_VAL);
1339 
1340 }
1341 
1342 /*******************************************************************************
1343 **
1344 ** Function         bta_av_connect_req
1345 **
1346 ** Description      Connect AVDTP connection.
1347 **
1348 ** Returns          void
1349 **
1350 *******************************************************************************/
bta_av_connect_req(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1351 void bta_av_connect_req (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1352 {
1353     utl_freebuf((void **) &p_scb->p_disc_db);
1354 
1355     if (p_scb->coll_mask & BTA_AV_COLL_INC_TMR)
1356     {
1357         /* SNK initiated L2C connection while SRC was doing SDP.    */
1358         /* Wait until timeout to check if SNK starts signalling.    */
1359         APPL_TRACE_EVENT1("bta_av_connect_req: coll_mask = 0x%2X", p_scb->coll_mask);
1360         return;
1361     }
1362 
1363     AVDT_ConnectReq(p_scb->peer_addr, p_scb->sec_mask, bta_av_dt_cback[p_scb->hdi]);
1364 }
1365 
1366 /*******************************************************************************
1367 **
1368 ** Function         bta_av_sdp_failed
1369 **
1370 ** Description      Service discovery failed.
1371 **
1372 ** Returns          void
1373 **
1374 *******************************************************************************/
bta_av_sdp_failed(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1375 void bta_av_sdp_failed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1376 {
1377     if (!p_scb->open_status)
1378         p_scb->open_status = BTA_AV_FAIL_SDP;
1379 
1380     utl_freebuf((void **) &p_scb->p_disc_db);
1381     bta_av_str_closed(p_scb, p_data);
1382 }
1383 
1384 /*******************************************************************************
1385 **
1386 ** Function         bta_av_disc_results
1387 **
1388 ** Description      Handle the AVDTP discover results.  Search through the
1389 **                  results and find the first available stream, and get
1390 **                  its capabilities.
1391 **
1392 ** Returns          void
1393 **
1394 *******************************************************************************/
bta_av_disc_results(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1395 void bta_av_disc_results (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1396 {
1397     UINT8 num_snks = 0, i;
1398 
1399     /* store number of stream endpoints returned */
1400     p_scb->num_seps = p_data->str_msg.msg.discover_cfm.num_seps;
1401 
1402     for (i = 0; i < p_scb->num_seps; i++)
1403     {
1404         /* steam not in use, is a sink, and is audio */
1405         if ((p_scb->sep_info[i].in_use == FALSE) &&
1406             (p_scb->sep_info[i].tsep == AVDT_TSEP_SNK) &&
1407             (p_scb->sep_info[i].media_type == p_scb->media_type))
1408         {
1409             num_snks++;
1410         }
1411     }
1412 
1413     p_scb->p_cos->disc_res(p_scb->hndl, p_scb->num_seps, num_snks, p_scb->peer_addr);
1414     p_scb->num_disc_snks = num_snks;
1415 
1416     /* if we got any */
1417     if (p_scb->num_seps > 0)
1418     {
1419         /* initialize index into discovery results */
1420         p_scb->sep_info_idx = 0;
1421 
1422         /* get the capabilities of the first available stream */
1423         bta_av_next_getcap(p_scb, p_data);
1424     }
1425     /* else we got discover response but with no streams; we're done */
1426     else
1427     {
1428         bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, p_data);
1429     }
1430 }
1431 
1432 /*******************************************************************************
1433 **
1434 ** Function         bta_av_disc_res_as_acp
1435 **
1436 ** Description      Handle the AVDTP discover results.  Search through the
1437 **                  results and find the first available stream, and get
1438 **                  its capabilities.
1439 **
1440 ** Returns          void
1441 **
1442 *******************************************************************************/
bta_av_disc_res_as_acp(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1443 void bta_av_disc_res_as_acp (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1444 {
1445     UINT8 num_snks = 0, i;
1446 
1447     /* store number of stream endpoints returned */
1448     p_scb->num_seps = p_data->str_msg.msg.discover_cfm.num_seps;
1449 
1450 
1451 
1452     for (i = 0; i < p_scb->num_seps; i++)
1453     {
1454         /* steam is a sink, and is audio */
1455         if ((p_scb->sep_info[i].tsep == AVDT_TSEP_SNK) &&
1456             (p_scb->sep_info[i].media_type == p_scb->media_type))
1457         {
1458             p_scb->sep_info[i].in_use = FALSE;
1459             num_snks++;
1460         }
1461     }
1462 
1463     p_scb->p_cos->disc_res(p_scb->hndl, p_scb->num_seps, num_snks, p_scb->peer_addr);
1464     p_scb->num_disc_snks = num_snks;
1465 
1466     /* if we got any */
1467     if (p_scb->num_seps > 0)
1468     {
1469         /* initialize index into discovery results */
1470         p_scb->sep_info_idx = 0;
1471 
1472         /* get the capabilities of the first available stream */
1473         bta_av_next_getcap(p_scb, p_data);
1474     }
1475     /* else we got discover response but with no streams; we're done */
1476     else
1477     {
1478         bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, p_data);
1479     }
1480 }
1481 
1482 /*******************************************************************************
1483 **
1484 ** Function         bta_av_save_caps
1485 **
1486 ** Description      report the SNK SEP capabilities to application
1487 **
1488 ** Returns          void
1489 **
1490 *******************************************************************************/
bta_av_save_caps(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1491 void bta_av_save_caps(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1492 {
1493     tAVDT_CFG   cfg;
1494     tAVDT_SEP_INFO  *p_info = &p_scb->sep_info[p_scb->sep_info_idx];
1495     UINT8       old_wait = p_scb->wait;
1496     BOOLEAN     getcap_done = FALSE;
1497 
1498     APPL_TRACE_DEBUG3("bta_av_save_caps num_seps:%d sep_info_idx:%d wait:x%x",
1499         p_scb->num_seps, p_scb->sep_info_idx, p_scb->wait);
1500     memcpy(&cfg, p_scb->p_cap, sizeof(tAVDT_CFG));
1501     /* let application know the capability of the SNK */
1502     p_scb->p_cos->getcfg(p_scb->hndl, cfg.codec_info[BTA_AV_CODEC_TYPE_IDX],
1503         cfg.codec_info, &p_scb->sep_info_idx, p_info->seid,
1504         &cfg.num_protect, cfg.protect_info);
1505 
1506     p_scb->sep_info_idx++;
1507     if(p_scb->num_seps > p_scb->sep_info_idx)
1508     {
1509         /* Some devices have seps at the end of the discover list, which is not */
1510         /* matching media type(video not audio).                                */
1511         /* In this case, we are done with getcap without sending another        */
1512         /* request to AVDT.                                                     */
1513         if (!bta_av_next_getcap(p_scb, p_data))
1514             getcap_done = TRUE;
1515     }
1516     else
1517         getcap_done = TRUE;
1518 
1519     if (getcap_done)
1520     {
1521         /* we are done getting capabilities. restore the p_cb->sep_info_idx */
1522         p_scb->sep_info_idx = 0;
1523         p_scb->wait &= ~(BTA_AV_WAIT_ACP_CAPS_ON|BTA_AV_WAIT_ACP_CAPS_STARTED);
1524         if (old_wait & BTA_AV_WAIT_ACP_CAPS_STARTED)
1525         {
1526             bta_av_start_ok (p_scb, NULL);
1527         }
1528     }
1529 }
1530 
1531 /*******************************************************************************
1532 **
1533 ** Function         bta_av_set_use_rc
1534 **
1535 ** Description      set to use AVRC for this stream control block.
1536 **
1537 ** Returns          void
1538 **
1539 *******************************************************************************/
bta_av_set_use_rc(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1540 void bta_av_set_use_rc (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1541 {
1542     p_scb->use_rc = TRUE;
1543 }
1544 
1545 /*******************************************************************************
1546 **
1547 ** Function         bta_av_cco_close
1548 **
1549 ** Description      call close call-out function.
1550 **
1551 ** Returns          void
1552 **
1553 *******************************************************************************/
bta_av_cco_close(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1554 void bta_av_cco_close (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1555 {
1556     UINT16 mtu;
1557     mtu = bta_av_chk_mtu(p_scb, BTA_AV_MAX_A2DP_MTU);
1558 
1559     p_scb->p_cos->close(p_scb->hndl, p_scb->codec_type, mtu);
1560 }
1561 
1562 /*******************************************************************************
1563 **
1564 ** Function         bta_av_open_failed
1565 **
1566 ** Description      Failed to open an AVDT stream
1567 **
1568 ** Returns          void
1569 **
1570 *******************************************************************************/
bta_av_open_failed(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1571 void bta_av_open_failed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1572 {
1573     p_scb->open_status = BTA_AV_FAIL_STREAM;
1574     bta_av_cco_close(p_scb, p_data);
1575     AVDT_DisconnectReq(p_scb->peer_addr, bta_av_dt_cback[p_scb->hdi]);
1576 }
1577 
1578 /*******************************************************************************
1579 **
1580 ** Function         bta_av_getcap_results
1581 **
1582 ** Description      Handle the AVDTP get capabilities results.  Check the codec
1583 **                  type and see if it matches ours.  If it does not, get the
1584 **                  capabilities of the next stream, if any.
1585 **
1586 ** Returns          void
1587 **
1588 *******************************************************************************/
bta_av_getcap_results(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1589 void bta_av_getcap_results (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1590 {
1591     tAVDT_CFG   cfg;
1592     UINT8       media_type;
1593     tAVDT_SEP_INFO  *p_info = &p_scb->sep_info[p_scb->sep_info_idx];
1594 
1595     memcpy(&cfg, &p_scb->cfg, sizeof(tAVDT_CFG));
1596     cfg.num_codec = 1;
1597     cfg.num_protect = p_scb->p_cap->num_protect;
1598     memcpy(cfg.codec_info, p_scb->p_cap->codec_info, AVDT_CODEC_SIZE);
1599     memcpy(cfg.protect_info, p_scb->p_cap->protect_info, AVDT_PROTECT_SIZE);
1600     media_type = p_scb->p_cap->codec_info[BTA_AV_MEDIA_TYPE_IDX] >> 4;
1601 
1602     APPL_TRACE_DEBUG1("num_codec %d", p_scb->p_cap->num_codec);
1603     APPL_TRACE_DEBUG2("media type x%x, x%x", media_type, p_scb->media_type);
1604 #if AVDT_MULTIPLEXING == TRUE
1605     APPL_TRACE_DEBUG2("mux x%x, x%x", cfg.mux_mask, p_scb->p_cap->mux_mask);
1606 #endif
1607 
1608     /* if codec present and we get a codec configuration */
1609     if ((p_scb->p_cap->num_codec != 0) &&
1610         (media_type == p_scb->media_type) &&
1611         (p_scb->p_cos->getcfg(p_scb->hndl, p_scb->p_cap->codec_info[BTA_AV_CODEC_TYPE_IDX],
1612             cfg.codec_info, &p_scb->sep_info_idx, p_info->seid,
1613             &cfg.num_protect, cfg.protect_info) == 0))
1614     {
1615 #if AVDT_MULTIPLEXING == TRUE
1616         cfg.mux_mask &= p_scb->p_cap->mux_mask;
1617         APPL_TRACE_DEBUG1("mux_mask used x%x", cfg.mux_mask);
1618 #endif
1619         /* save copy of codec type and configuration */
1620         p_scb->codec_type = cfg.codec_info[BTA_AV_CODEC_TYPE_IDX];
1621         memcpy(&p_scb->cfg, &cfg, sizeof(tAVDT_CFG));
1622         bta_av_adjust_seps_idx(p_scb);
1623         /* use only the services peer supports */
1624         cfg.psc_mask &= p_scb->p_cap->psc_mask;
1625         p_scb->cur_psc_mask = cfg.psc_mask;
1626 
1627         /* open the stream */
1628         AVDT_OpenReq(p_scb->seps[p_scb->sep_idx].av_handle, p_scb->peer_addr,
1629                      p_scb->sep_info[p_scb->sep_info_idx].seid, &cfg);
1630 
1631         if (!bta_av_is_rcfg_sst(p_scb))
1632         {
1633             /* free capabilities buffer */
1634             utl_freebuf((void **) &p_scb->p_cap);
1635         }
1636     }
1637     else
1638     {
1639         /* try the next stream, if any */
1640         p_scb->sep_info_idx++;
1641         bta_av_next_getcap(p_scb, p_data);
1642     }
1643 
1644 }
1645 
1646 /*******************************************************************************
1647 **
1648 ** Function         bta_av_setconfig_rej
1649 **
1650 ** Description      Send AVDTP set config reject.
1651 **
1652 ** Returns          void
1653 **
1654 *******************************************************************************/
bta_av_setconfig_rej(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1655 void bta_av_setconfig_rej (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1656 {
1657     tBTA_AV_REJECT reject;
1658 
1659     APPL_TRACE_DEBUG0("bta_av_setconfig_rej");
1660     AVDT_ConfigRsp(p_data->str_msg.handle, p_data->str_msg.msg.hdr.label, AVDT_ERR_BAD_STATE, 0);
1661     bdcpy(reject.bd_addr, p_data->str_msg.bd_addr);
1662     reject.hndl = p_scb->hndl;
1663     (*bta_av_cb.p_cback)(BTA_AV_REJECT_EVT, (tBTA_AV *) &reject);
1664 }
1665 
1666 /*******************************************************************************
1667 **
1668 ** Function         bta_av_discover_req
1669 **
1670 ** Description      Send an AVDTP discover request to the peer.
1671 **
1672 ** Returns          void
1673 **
1674 *******************************************************************************/
bta_av_discover_req(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1675 void bta_av_discover_req (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1676 {
1677     /* send avdtp discover request */
1678 
1679     AVDT_DiscoverReq(p_scb->peer_addr, p_scb->sep_info, BTA_AV_NUM_SEPS, bta_av_dt_cback[p_scb->hdi]);
1680 }
1681 
1682 /*******************************************************************************
1683 **
1684 ** Function         bta_av_conn_failed
1685 **
1686 ** Description      AVDTP connection failed.
1687 **
1688 ** Returns          void
1689 **
1690 *******************************************************************************/
bta_av_conn_failed(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1691 void bta_av_conn_failed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1692 {
1693     p_scb->open_status = BTA_AV_FAIL_STREAM;
1694     bta_av_str_closed(p_scb, p_data);
1695 }
1696 
1697 /*******************************************************************************
1698 **
1699 ** Function         bta_av_do_start
1700 **
1701 ** Description      Start stream.
1702 **
1703 ** Returns          void
1704 **
1705 *******************************************************************************/
bta_av_do_start(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1706 void bta_av_do_start (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1707 {
1708     UINT8 policy = HCI_ENABLE_SNIFF_MODE;
1709     UINT8       cur_role;
1710 
1711     APPL_TRACE_DEBUG3("bta_av_do_start sco_occupied:%d, role:x%x, started:%d", bta_av_cb.sco_occupied, p_scb->role, p_scb->started);
1712     if (bta_av_cb.sco_occupied)
1713     {
1714         bta_av_start_failed(p_scb, p_data);
1715         return;
1716     }
1717 
1718     /* disallow role switch during streaming, only if we are the master role
1719      * i.e. allow role switch, if we are slave.
1720      * It would not hurt us, if the peer device wants us to be master */
1721     if ((BTM_GetRole (p_scb->peer_addr, &cur_role) == BTM_SUCCESS) &&
1722         (cur_role == BTM_ROLE_MASTER) )
1723     {
1724         policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
1725     }
1726 
1727     bta_sys_clear_policy(BTA_ID_AV, policy, p_scb->peer_addr);
1728 
1729     if ((p_scb->started == FALSE) && ((p_scb->role & BTA_AV_ROLE_START_INT) == 0))
1730     {
1731         p_scb->role |= BTA_AV_ROLE_START_INT;
1732         bta_sys_busy(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
1733 
1734         AVDT_StartReq(&p_scb->avdt_handle, 1);
1735     }
1736     else
1737     {
1738         bta_av_start_ok(p_scb, NULL);
1739     }
1740     APPL_TRACE_DEBUG2("started %d role:x%x", p_scb->started, p_scb->role);
1741 }
1742 
1743 /*******************************************************************************
1744 **
1745 ** Function         bta_av_str_stopped
1746 **
1747 ** Description      Stream stopped.
1748 **
1749 ** Returns          void
1750 **
1751 *******************************************************************************/
bta_av_str_stopped(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1752 void bta_av_str_stopped (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1753 {
1754     tBTA_AV_SUSPEND suspend_rsp;
1755     UINT8   start = p_scb->started;
1756     BOOLEAN sus_evt = TRUE;
1757     BT_HDR  *p_buf;
1758     UINT8 policy = HCI_ENABLE_SNIFF_MODE;
1759 
1760     APPL_TRACE_ERROR2("bta_av_str_stopped:audio_open_cnt=%d, p_data %x",
1761             bta_av_cb.audio_open_cnt, p_data);
1762 
1763     bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
1764     if ((bta_av_cb.features & BTA_AV_FEAT_MASTER) == 0 || bta_av_cb.audio_open_cnt == 1)
1765         policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
1766     bta_sys_set_policy(BTA_ID_AV, policy, p_scb->peer_addr);
1767 
1768     if(p_scb->co_started)
1769     {
1770         bta_av_stream_chg(p_scb, FALSE);
1771         p_scb->co_started = FALSE;
1772 
1773         p_scb->p_cos->stop(p_scb->hndl, p_scb->codec_type);
1774         L2CA_SetFlushTimeout(p_scb->peer_addr, L2CAP_DEFAULT_FLUSH_TO);
1775     }
1776 
1777     /* if q_info.a2d is not empty, drop it now */
1778     if(BTA_AV_CHNL_AUDIO == p_scb->chnl)
1779     {
1780         while((p_buf = (BT_HDR*)GKI_dequeue (&p_scb->q_info.a2d)) != NULL)
1781         GKI_freebuf(p_buf);
1782 
1783     /* drop the audio buffers queued in L2CAP */
1784         if(p_data && p_data->api_stop.flush)
1785             L2CA_FlushChannel (p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
1786     }
1787 
1788     suspend_rsp.chnl = p_scb->chnl;
1789     suspend_rsp.hndl = p_scb->hndl;
1790 
1791     if (p_data && p_data->api_stop.suspend)
1792     {
1793         APPL_TRACE_DEBUG2("suspending: %d, sup:%d", start, p_scb->suspend_sup);
1794         if ((start)  && (p_scb->suspend_sup))
1795         {
1796             sus_evt = FALSE;
1797             p_scb->l2c_bufs = 0;
1798             AVDT_SuspendReq(&p_scb->avdt_handle, 1);
1799         }
1800 
1801         if(sus_evt)
1802         {
1803             suspend_rsp.status = BTA_AV_SUCCESS;
1804             suspend_rsp.initiator = TRUE;
1805             (*bta_av_cb.p_cback)(BTA_AV_SUSPEND_EVT, (tBTA_AV *) &suspend_rsp);
1806         }
1807     }
1808     else
1809     {
1810         suspend_rsp.status = BTA_AV_SUCCESS;
1811         suspend_rsp.initiator = TRUE;
1812         APPL_TRACE_EVENT1("bta_av_str_stopped status %d", suspend_rsp.status);
1813 
1814         (*bta_av_cb.p_cback)(BTA_AV_STOP_EVT, (tBTA_AV *) &suspend_rsp);
1815     }
1816 }
1817 
1818 /*******************************************************************************
1819 **
1820 ** Function         bta_av_reconfig
1821 **
1822 ** Description      process the reconfigure request.
1823 **                  save the parameter in control block and
1824 **                  suspend, reconfigure or close the stream
1825 **
1826 ** Returns          void
1827 **
1828 *******************************************************************************/
bta_av_reconfig(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1829 void bta_av_reconfig (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1830 {
1831     tAVDT_CFG   *p_cfg;
1832     tBTA_AV_API_STOP    stop;
1833     tBTA_AV_RECONFIG    evt;
1834     tBTA_AV_API_RCFG    *p_rcfg = &p_data->api_reconfig;
1835 
1836     APPL_TRACE_DEBUG4("bta_av_reconfig r:%d, s:%d idx: %d (o:%d)",
1837         p_scb->recfg_sup, p_scb->suspend_sup,
1838         p_scb->rcfg_idx, p_scb->sep_info_idx);
1839 
1840     p_scb->num_recfg = 0;
1841     /* store the new configuration in control block */
1842     if (p_scb->p_cap == NULL)
1843     {
1844         p_scb->p_cap = (tAVDT_CFG *) GKI_getbuf(sizeof(tAVDT_CFG));
1845     }
1846     if((p_cfg = p_scb->p_cap) == NULL)
1847     {
1848         /* report failure */
1849         evt.status = BTA_AV_FAIL_RESOURCES;
1850         evt.chnl   = p_scb->chnl;
1851         evt.hndl   = p_scb->hndl;
1852         (*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, (tBTA_AV *)&evt);
1853 
1854         /* this event is not possible in this state.
1855          * use it to bring the SSM back to open state */
1856         bta_av_ssm_execute(p_scb, BTA_AV_SDP_DISC_OK_EVT, NULL);
1857         return;
1858     }
1859 
1860     /*if(bta_av_cb.features & BTA_AV_FEAT_RCCT)*/
1861         bta_sys_stop_timer(&p_scb->timer);
1862 
1863     memcpy(p_cfg, &p_scb->cfg, sizeof(tAVDT_CFG));
1864     p_cfg->num_protect = p_rcfg->num_protect;
1865     memcpy(p_cfg->codec_info, p_rcfg->codec_info, AVDT_CODEC_SIZE);
1866     memcpy(p_cfg->protect_info, p_rcfg->p_protect_info, p_rcfg->num_protect);
1867     p_scb->rcfg_idx = p_rcfg->sep_info_idx;
1868     p_scb->p_cap->psc_mask = p_scb->cur_psc_mask;
1869 
1870     /* if the requested index differs from the current one, we can only close/open */
1871     if ((p_scb->rcfg_idx == p_scb->sep_info_idx) &&
1872         (p_rcfg->suspend)&& (p_scb->recfg_sup) && (p_scb->suspend_sup))
1873     {
1874         if(p_scb->started)
1875         {
1876             stop.flush   = FALSE;
1877             stop.suspend = TRUE;
1878             bta_av_str_stopped(p_scb, (tBTA_AV_DATA *)&stop);
1879         }
1880         else
1881         {
1882             APPL_TRACE_DEBUG0("Reconfig");
1883             AVDT_ReconfigReq(p_scb->avdt_handle, p_scb->p_cap);
1884             p_scb->p_cap->psc_mask = p_scb->cur_psc_mask;
1885         }
1886     }
1887     else
1888     {
1889         /* close the stream */
1890         APPL_TRACE_DEBUG1("close/open num_protect: %d", p_cfg->num_protect);
1891         if(p_scb->started)
1892             bta_av_str_stopped(p_scb, NULL);
1893             p_scb->started = FALSE;
1894 
1895             /* drop the buffers queued in L2CAP */
1896             L2CA_FlushChannel (p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
1897 
1898             AVDT_CloseReq(p_scb->avdt_handle);
1899 
1900     }
1901 }
1902 
1903 /*******************************************************************************
1904 **
1905 ** Function         bta_av_data_path
1906 **
1907 ** Description      Handle stream data path.
1908 **
1909 ** Returns          void
1910 **
1911 *******************************************************************************/
bta_av_data_path(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1912 void bta_av_data_path (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1913 {
1914     BT_HDR  *p_buf;
1915     UINT32  data_len;
1916     UINT32  timestamp;
1917     BOOLEAN new_buf = FALSE;
1918     UINT8   m_pt = 0x60 | p_scb->codec_type;
1919 
1920     if (!p_scb->cong)
1921     {
1922         /*
1923         APPL_TRACE_ERROR1("q: %d", p_scb->l2c_bufs);
1924         */
1925         //Always get the current number of bufs que'd up
1926         p_scb->l2c_bufs = (UINT8)L2CA_FlushChannel (p_scb->l2c_cid, L2CAP_FLUSH_CHANS_GET);
1927 
1928         p_buf = (BT_HDR *)GKI_dequeue (&p_scb->q_info.a2d);
1929         if(p_buf)
1930         {
1931             /* use q_info.a2d data, read the timestamp */
1932             timestamp = *(UINT32 *)(p_buf + 1);
1933         }
1934         else
1935         {
1936             new_buf = TRUE;
1937             /* q_info.a2d empty, call co_data, dup data to other channels */
1938             p_buf = (BT_HDR *)p_scb->p_cos->data(p_scb->codec_type, &data_len,
1939                                              &timestamp);
1940 
1941             if (p_buf)
1942             {
1943                 /* use the offset area for the time stamp */
1944                 *(UINT32 *)(p_buf + 1) = timestamp;
1945 
1946                 /* dup the data to other channels */
1947                 bta_av_dup_audio_buf(p_scb, p_buf);
1948             }
1949         }
1950 
1951         if(p_buf)
1952         {
1953             if(p_scb->l2c_bufs < (BTA_AV_QUEUE_DATA_CHK_NUM))
1954             {
1955                 /* there's a buffer, just queue it to L2CAP */
1956                 /*  There's no need to increment it here, it is always read from L2CAP see above */
1957                 /* p_scb->l2c_bufs++; */
1958                 /*
1959                 APPL_TRACE_ERROR1("qw: %d", p_scb->l2c_bufs);
1960                 */
1961                 AVDT_WriteReq(p_scb->avdt_handle, p_buf, timestamp, m_pt);
1962                 p_scb->cong = TRUE;
1963             }
1964             else
1965             {
1966                 /* there's a buffer, but L2CAP does not seem to be moving data */
1967                 if(new_buf)
1968                 {
1969                     /* just got this buffer from co_data,
1970                      * put it in queue */
1971                     GKI_enqueue(&p_scb->q_info.a2d, p_buf);
1972                 }
1973                 else
1974                 {
1975                     /* just dequeue it from the q_info.a2d */
1976                     if(p_scb->q_info.a2d.count < 3)
1977                     {
1978                         /* put it back to the queue */
1979                         GKI_enqueue_head (&p_scb->q_info.a2d, p_buf);
1980                     }
1981                     else
1982                     {
1983                         /* too many buffers in q_info.a2d, drop it. */
1984                         bta_av_co_audio_drop(p_scb->hndl);
1985                         GKI_freebuf(p_buf);
1986                     }
1987                 }
1988             }
1989         }
1990     }
1991 }
1992 
1993 /*******************************************************************************
1994 **
1995 ** Function         bta_av_start_ok
1996 **
1997 ** Description      Stream started.
1998 **
1999 ** Returns          void
2000 **
2001 *******************************************************************************/
bta_av_start_ok(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2002 void bta_av_start_ok (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2003 {
2004     tBTA_AV_START   start;
2005     tBTA_AV_API_STOP stop;
2006     BOOLEAN         initiator = FALSE;
2007     BOOLEAN         suspend = FALSE;
2008     UINT16          flush_to;
2009     UINT8           new_role = p_scb->role;
2010     BT_HDR          hdr;
2011 
2012     APPL_TRACE_DEBUG2("bta_av_start_ok wait:x%x, role:x%x", p_scb->wait, p_scb->role);
2013 
2014     p_scb->started = TRUE;
2015     if (p_scb->sco_suspend)
2016     {
2017         p_scb->sco_suspend = FALSE;
2018     }
2019 
2020     if (new_role & BTA_AV_ROLE_START_INT)
2021         initiator = TRUE;
2022 
2023     if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_FAILED)
2024     {
2025         /* role switch has failed */
2026         p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_FAILED;
2027         p_data = (tBTA_AV_DATA *)&hdr;
2028         hdr.offset = BTA_AV_RS_FAIL;
2029     }
2030     APPL_TRACE_DEBUG1("wait:x%x", p_scb->wait);
2031 
2032     if (p_data && (p_data->hdr.offset != BTA_AV_RS_NONE))
2033     {
2034         p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
2035         if (p_data->hdr.offset == BTA_AV_RS_FAIL)
2036         {
2037             bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
2038             start.chnl   = p_scb->chnl;
2039             start.status = BTA_AV_FAIL_ROLE;
2040             start.hndl   = p_scb->hndl;
2041             start.initiator = initiator;
2042             (*bta_av_cb.p_cback)(BTA_AV_START_EVT, (tBTA_AV *) &start);
2043             return;
2044         }
2045     }
2046 
2047     if (!bta_av_link_role_ok(p_scb, A2D_SET_ONE_BIT))
2048         p_scb->q_tag = BTA_AV_Q_TAG_START;
2049     else
2050     {
2051         /* The wait flag may be set here while we are already master on the link */
2052         /* this could happen if a role switch complete event occurred during reconfig */
2053         /* if we are now master on the link, there is no need to wait for the role switch, */
2054         /* complete anymore so we can clear the wait for role switch flag */
2055         p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
2056     }
2057 
2058     if (p_scb->wait & (BTA_AV_WAIT_ROLE_SW_RES_OPEN|BTA_AV_WAIT_ROLE_SW_RES_START))
2059     {
2060         p_scb->wait |= BTA_AV_WAIT_ROLE_SW_STARTED;
2061         p_scb->q_tag = BTA_AV_Q_TAG_START;
2062     }
2063 
2064     if (p_scb->wait & BTA_AV_WAIT_ACP_CAPS_ON)
2065     {
2066         p_scb->wait |= BTA_AV_WAIT_ACP_CAPS_STARTED;
2067     }
2068 
2069     if (p_scb->wait)
2070     {
2071         APPL_TRACE_DEBUG2("wait:x%x q_tag:%d- not started", p_scb->wait, p_scb->q_tag);
2072         return;
2073     }
2074 
2075     /* tell role manager to check M/S role */
2076     bta_sys_conn_open(BTA_ID_AV, p_scb->app_id, p_scb->peer_addr);
2077 
2078     bta_sys_busy(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
2079 
2080     if(p_scb->media_type == AVDT_MEDIA_AUDIO)
2081     {
2082         /* in normal logic, conns should be bta_av_cb.audio_count - 1,
2083          * However, bta_av_stream_chg is not called to increase bta_av_cb.audio_count yet.
2084          * If the code were to be re-arranged for some reasons, this number may need to be changed
2085          */
2086         p_scb->co_started = bta_av_cb.audio_open_cnt;
2087         flush_to = p_bta_av_cfg->p_audio_flush_to[p_scb->co_started - 1];
2088     }
2089     else
2090     {
2091         flush_to = p_bta_av_cfg->video_flush_to;
2092     }
2093     L2CA_SetFlushTimeout(p_scb->peer_addr, flush_to );
2094 
2095     /* clear the congestion flag */
2096     p_scb->cong = FALSE;
2097 
2098     if (new_role & BTA_AV_ROLE_START_INT)
2099     {
2100         new_role &= ~BTA_AV_ROLE_START_INT;
2101     }
2102     else if ((new_role & BTA_AV_ROLE_AD_ACP) && (new_role & BTA_AV_ROLE_SUSPEND_OPT))
2103     {
2104         suspend = TRUE;
2105     }
2106 
2107     if (!suspend)
2108     {
2109         p_scb->q_tag = BTA_AV_Q_TAG_STREAM;
2110         bta_av_stream_chg(p_scb, TRUE);
2111     }
2112 
2113     {
2114         p_scb->role = new_role;
2115         p_scb->role &= ~BTA_AV_ROLE_AD_ACP;
2116         p_scb->role &= ~BTA_AV_ROLE_SUSPEND_OPT;
2117 
2118         p_scb->p_cos->start(p_scb->hndl, p_scb->codec_type);
2119         p_scb->co_started = TRUE;
2120 
2121         APPL_TRACE_DEBUG3("bta_av_start_ok suspending: %d, role:x%x, init %d",
2122             suspend, p_scb->role, initiator);
2123 
2124         start.suspending = suspend;
2125         start.initiator = initiator;
2126         start.chnl   = p_scb->chnl;
2127         start.status = BTA_AV_SUCCESS;
2128         start.hndl   = p_scb->hndl;
2129         (*bta_av_cb.p_cback)(BTA_AV_START_EVT, (tBTA_AV *) &start);
2130 
2131         if(suspend)
2132         {
2133             p_scb->role |= BTA_AV_ROLE_SUSPEND;
2134             p_scb->cong = TRUE;  /* do not allow the media data to go through */
2135             /* do not duplicate the media packets to this channel */
2136             p_scb->p_cos->stop(p_scb->hndl, p_scb->codec_type);
2137             p_scb->co_started = FALSE;
2138             stop.flush   = FALSE;
2139             stop.suspend = TRUE;
2140             bta_av_ssm_execute(p_scb, BTA_AV_AP_STOP_EVT, (tBTA_AV_DATA *)&stop);
2141         }
2142     }
2143 }
2144 
2145 /*******************************************************************************
2146 **
2147 ** Function         bta_av_start_failed
2148 **
2149 ** Description      Stream start failed.
2150 **
2151 ** Returns          void
2152 **
2153 *******************************************************************************/
bta_av_start_failed(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2154 void bta_av_start_failed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2155 {
2156     tBTA_AV_START   start;
2157 
2158     if(p_scb->started == FALSE && p_scb->co_started == FALSE)
2159     {
2160         /* if start failed, clear role */
2161         p_scb->role &= ~BTA_AV_ROLE_START_INT;
2162 
2163         bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
2164         start.chnl   = p_scb->chnl;
2165         start.status = BTA_AV_FAIL;
2166         start.initiator = TRUE;
2167         start.hndl   = p_scb->hndl;
2168         (*bta_av_cb.p_cback)(BTA_AV_START_EVT, (tBTA_AV *) &start);
2169     }
2170 
2171     bta_sys_set_policy(BTA_ID_AV, (HCI_ENABLE_SNIFF_MODE|HCI_ENABLE_MASTER_SLAVE_SWITCH), p_scb->peer_addr);
2172     p_scb->sco_suspend = FALSE;
2173 }
2174 
2175 /*******************************************************************************
2176 **
2177 ** Function         bta_av_str_closed
2178 **
2179 ** Description      Stream closed.
2180 **
2181 ** Returns          void
2182 **
2183 *******************************************************************************/
bta_av_str_closed(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2184 void bta_av_str_closed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2185 {
2186     tBTA_AV     data;
2187     tBTA_AV_EVT event;
2188     UINT16      mtu;
2189     UINT8 policy = HCI_ENABLE_SNIFF_MODE;
2190 
2191     if ((bta_av_cb.features & BTA_AV_FEAT_MASTER) == 0 || bta_av_cb.audio_open_cnt == 1)
2192         policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
2193     bta_sys_set_policy(BTA_ID_AV, policy, p_scb->peer_addr);
2194     if (bta_av_cb.audio_open_cnt <= 1)
2195     {
2196         /* last connection - restore the allow switch flag */
2197         L2CA_SetDesireRole(L2CAP_ROLE_ALLOW_SWITCH);
2198     }
2199 
2200     if (p_scb->open_status)
2201     {
2202         /* must be failure when opening the stream */
2203         bdcpy(data.open.bd_addr, p_scb->peer_addr);
2204         data.open.status = p_scb->open_status;
2205         data.open.chnl   = p_scb->chnl;
2206         data.open.hndl   = p_scb->hndl;
2207         event = BTA_AV_OPEN_EVT;
2208         p_scb->open_status = BTA_AV_SUCCESS;
2209 
2210         bta_sys_conn_close(BTA_ID_AV, p_scb->app_id, p_scb->peer_addr);
2211         bta_av_cleanup(p_scb, p_data);
2212         (*bta_av_cb.p_cback)(event, &data);
2213     }
2214     else
2215     {
2216         /* do stop if we were started */
2217         if (p_scb->co_started)
2218         {
2219             bta_av_str_stopped(p_scb, NULL);
2220         }
2221 
2222         /* Update common mtu shared by remaining connectons */
2223         mtu = bta_av_chk_mtu(p_scb, BTA_AV_MAX_A2DP_MTU);
2224 
2225         {
2226             p_scb->p_cos->close(p_scb->hndl, p_scb->codec_type, mtu);
2227             data.close.chnl = p_scb->chnl;
2228             data.close.hndl = p_scb->hndl;
2229             event = BTA_AV_CLOSE_EVT;
2230 
2231             bta_sys_conn_close(BTA_ID_AV, p_scb->app_id, p_scb->peer_addr);
2232             bta_av_cleanup(p_scb, p_data);
2233             (*bta_av_cb.p_cback)(event, &data);
2234         }
2235     }
2236 }
2237 
2238 /*******************************************************************************
2239 **
2240 ** Function         bta_av_clr_cong
2241 **
2242 ** Description      Clear stream congestion flag.
2243 **
2244 ** Returns          void
2245 **
2246 *******************************************************************************/
bta_av_clr_cong(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2247 void bta_av_clr_cong (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2248 {
2249     if(p_scb->co_started)
2250         p_scb->cong = FALSE;
2251 }
2252 
2253 /*******************************************************************************
2254 **
2255 ** Function         bta_av_suspend_cfm
2256 **
2257 ** Description      process the suspend response
2258 **
2259 ** Returns          void
2260 **
2261 *******************************************************************************/
bta_av_suspend_cfm(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2262 void bta_av_suspend_cfm (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2263 {
2264     tBTA_AV_SUSPEND suspend_rsp;
2265     UINT8           err_code = p_data->str_msg.msg.hdr.err_code;
2266     UINT8 policy = HCI_ENABLE_SNIFF_MODE;
2267 
2268     APPL_TRACE_DEBUG2 ("bta_av_suspend_cfm:audio_open_cnt = %d, err_code = %d",
2269         bta_av_cb.audio_open_cnt, err_code);
2270 
2271     suspend_rsp.status = BTA_AV_SUCCESS;
2272     if(err_code)
2273     {
2274         p_scb->suspend_sup = FALSE;
2275         suspend_rsp.status = BTA_AV_FAIL;
2276 
2277         APPL_TRACE_ERROR0 ("bta_av_suspend_cfm: suspend failed, closing connection");
2278 
2279         /* SUSPEND failed. Close connection. */
2280         bta_av_ssm_execute(p_scb, BTA_AV_API_CLOSE_EVT, NULL);
2281     }
2282     else
2283     {
2284         /* only set started to FALSE when suspend is successful */
2285         p_scb->started = FALSE;
2286     }
2287 
2288     if(p_scb->role & BTA_AV_ROLE_SUSPEND)
2289     {
2290         p_scb->role &= ~BTA_AV_ROLE_SUSPEND;
2291         p_scb->cong = FALSE;
2292     }
2293 
2294     bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
2295     if ((bta_av_cb.features & BTA_AV_FEAT_MASTER) == 0 || bta_av_cb.audio_open_cnt == 1)
2296         policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
2297     bta_sys_set_policy(BTA_ID_AV, policy, p_scb->peer_addr);
2298 
2299     /* in case that we received suspend_ind, we may need to call co_stop here */
2300     if(p_scb->co_started)
2301     {
2302         bta_av_stream_chg(p_scb, FALSE);
2303 
2304         {
2305             p_scb->co_started = FALSE;
2306             p_scb->p_cos->stop(p_scb->hndl, p_scb->codec_type);
2307         }
2308         L2CA_SetFlushTimeout(p_scb->peer_addr, L2CAP_DEFAULT_FLUSH_TO);
2309     }
2310 
2311     {
2312         suspend_rsp.chnl = p_scb->chnl;
2313         suspend_rsp.hndl = p_scb->hndl;
2314         suspend_rsp.initiator = p_data->str_msg.initiator;
2315         (*bta_av_cb.p_cback)(BTA_AV_SUSPEND_EVT, (tBTA_AV *) &suspend_rsp);
2316     }
2317 }
2318 
2319 /*******************************************************************************
2320 **
2321 ** Function         bta_av_rcfg_str_ok
2322 **
2323 ** Description      report reconfigure successful
2324 **
2325 ** Returns          void
2326 **
2327 *******************************************************************************/
bta_av_rcfg_str_ok(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2328 void bta_av_rcfg_str_ok (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2329 {
2330     tBTA_AV_RECONFIG    evt;
2331 
2332     p_scb->l2c_cid      = AVDT_GetL2CapChannel(p_scb->avdt_handle);
2333     APPL_TRACE_DEBUG1("bta_av_rcfg_str_ok: l2c_cid: %d", p_scb->l2c_cid);
2334 
2335     /* rc listen */
2336     bta_av_st_rc_timer(p_scb, NULL);
2337     utl_freebuf((void **)&p_scb->p_cap);
2338 
2339     /* No need to keep the role bits once reconfig is done. */
2340     p_scb->role &= ~BTA_AV_ROLE_AD_ACP;
2341     p_scb->role &= ~BTA_AV_ROLE_SUSPEND_OPT;
2342     p_scb->role &= ~BTA_AV_ROLE_START_INT;
2343 
2344     {
2345         /* reconfigure success  */
2346         evt.status = BTA_AV_SUCCESS;
2347         evt.chnl   = p_scb->chnl;
2348         evt.hndl   = p_scb->hndl;
2349         (*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, (tBTA_AV *)&evt);
2350     }
2351 }
2352 
2353 /*******************************************************************************
2354 **
2355 ** Function         bta_av_rcfg_failed
2356 **
2357 ** Description      process reconfigure failed
2358 **
2359 ** Returns          void
2360 **
2361 *******************************************************************************/
bta_av_rcfg_failed(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2362 void bta_av_rcfg_failed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2363 {
2364     tBTA_AV_RECONFIG evt;
2365 
2366     APPL_TRACE_DEBUG2("bta_av_rcfg_failed num_recfg: %d, conn_lcb:0x%x",
2367         p_scb->num_recfg, bta_av_cb.conn_lcb);
2368     if(p_scb->num_recfg > BTA_AV_RECONFIG_RETRY)
2369     {
2370         bta_av_cco_close(p_scb, p_data);
2371         /* report failure */
2372         evt.status = BTA_AV_FAIL_STREAM;
2373         evt.chnl   = p_scb->chnl;
2374         evt.hndl   = p_scb->hndl;
2375         (*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, (tBTA_AV *)&evt);
2376         /* go to closing state */
2377         bta_av_ssm_execute(p_scb, BTA_AV_API_CLOSE_EVT, NULL);
2378     }
2379     else
2380     {
2381         /* open failed. try again */
2382         p_scb->num_recfg++;
2383         if(bta_av_cb.conn_lcb)
2384         {
2385             AVDT_DisconnectReq(p_scb->peer_addr, bta_av_dt_cback[p_scb->hdi]);
2386         }
2387         else
2388         {
2389             bta_av_connect_req(p_scb, NULL);
2390         }
2391     }
2392 }
2393 
2394 /*******************************************************************************
2395 **
2396 ** Function         bta_av_rcfg_connect
2397 **
2398 ** Description      stream closed. reconnect the stream
2399 **
2400 ** Returns          void
2401 **
2402 *******************************************************************************/
bta_av_rcfg_connect(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2403 void bta_av_rcfg_connect (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2404 {
2405     p_scb->cong    = FALSE;
2406     p_scb->num_recfg++;
2407     APPL_TRACE_DEBUG1("bta_av_rcfg_connect num_recfg: %d", p_scb->num_recfg);
2408     if(p_scb->num_recfg > BTA_AV_RECONFIG_RETRY)
2409     {
2410         /* let bta_av_rcfg_failed report fail */
2411         bta_av_rcfg_failed(p_scb, NULL);
2412     }
2413     else
2414         AVDT_ConnectReq(p_scb->peer_addr, p_scb->sec_mask, bta_av_dt_cback[p_scb->hdi]);
2415 }
2416 
2417 /*******************************************************************************
2418 **
2419 ** Function         bta_av_rcfg_discntd
2420 **
2421 ** Description      AVDT disconnected. reconnect the stream
2422 **
2423 ** Returns          void
2424 **
2425 *******************************************************************************/
bta_av_rcfg_discntd(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2426 void bta_av_rcfg_discntd (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2427 {
2428     tBTA_AV_RECONFIG    evt;
2429 
2430     APPL_TRACE_DEBUG1("bta_av_rcfg_discntd num_recfg: %d", p_scb->num_recfg);
2431     p_scb->num_recfg++;
2432     if(p_scb->num_recfg > BTA_AV_RECONFIG_RETRY)
2433     {
2434         /* report failure */
2435         evt.status = BTA_AV_FAIL_STREAM;
2436         evt.chnl   = p_scb->chnl;
2437         evt.hndl   = p_scb->hndl;
2438         (*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, (tBTA_AV *)&evt);
2439         /* report close event & go to init state */
2440         bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, NULL);
2441     }
2442     else
2443         AVDT_ConnectReq(p_scb->peer_addr, p_scb->sec_mask, bta_av_dt_cback[p_scb->hdi]);
2444 }
2445 
2446 /*******************************************************************************
2447 **
2448 ** Function         bta_av_suspend_cont
2449 **
2450 ** Description      received the suspend response.
2451 **                  continue to reconfigure the stream
2452 **
2453 ** Returns          void
2454 **
2455 *******************************************************************************/
bta_av_suspend_cont(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2456 void bta_av_suspend_cont (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2457 {
2458     UINT8       err_code = p_data->str_msg.msg.hdr.err_code;
2459     tBTA_AV_RECONFIG    evt;
2460 
2461     p_scb->started = FALSE;
2462     p_scb->cong    = FALSE;
2463     if(err_code)
2464     {
2465         if(AVDT_ERR_CONNECT == err_code)
2466         {
2467             /* report failure */
2468             evt.status = BTA_AV_FAIL;
2469             (*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, (tBTA_AV *)&evt);
2470             bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, NULL);
2471         }
2472         else
2473         {
2474             APPL_TRACE_ERROR0("suspend rejected, try close");
2475             p_scb->suspend_sup = FALSE;
2476 
2477             /* drop the buffers queued in L2CAP */
2478             L2CA_FlushChannel (p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
2479 
2480             AVDT_CloseReq(p_scb->avdt_handle);
2481         }
2482     }
2483     else
2484     {
2485         APPL_TRACE_DEBUG0("bta_av_suspend_cont calling AVDT_ReconfigReq");
2486         /* reconfig the stream */
2487 
2488         AVDT_ReconfigReq(p_scb->avdt_handle, p_scb->p_cap);
2489         p_scb->p_cap->psc_mask = p_scb->cur_psc_mask;
2490     }
2491 }
2492 
2493 /*******************************************************************************
2494 **
2495 ** Function         bta_av_rcfg_cfm
2496 **
2497 ** Description      if reconfigure is successful, report the event
2498 **                  otherwise, close the stream.
2499 **
2500 ** Returns          void
2501 **
2502 *******************************************************************************/
bta_av_rcfg_cfm(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2503 void bta_av_rcfg_cfm (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2504 {
2505     UINT8   err_code = p_data->str_msg.msg.hdr.err_code;
2506 
2507     /*
2508     APPL_TRACE_DEBUG0("bta_av_rcfg_cfm");
2509     */
2510     if(err_code)
2511     {
2512         APPL_TRACE_ERROR0("reconfig rejected, try close");
2513         p_scb->recfg_sup = FALSE;
2514         /* started flag is FALSE when reconfigure command is sent */
2515         /* drop the buffers queued in L2CAP */
2516         L2CA_FlushChannel (p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
2517         AVDT_CloseReq(p_scb->avdt_handle);
2518     }
2519     else
2520     {
2521         /* take the SSM back to OPEN state */
2522         bta_av_ssm_execute(p_scb, BTA_AV_STR_OPEN_OK_EVT, NULL);
2523     }
2524 }
2525 
2526 /*******************************************************************************
2527 **
2528 ** Function         bta_av_rcfg_open
2529 **
2530 ** Description      AVDT is connected. open the stream with the new configuration
2531 **
2532 ** Returns          void
2533 **
2534 *******************************************************************************/
bta_av_rcfg_open(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2535 void bta_av_rcfg_open (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2536 {
2537     APPL_TRACE_DEBUG1("bta_av_rcfg_open, num_disc_snks = %d", p_scb->num_disc_snks);
2538 
2539     if (p_scb->num_disc_snks == 0)
2540     {
2541         /* Need to update call-out module so that it will be ready for discover */
2542         p_scb->p_cos->stop(p_scb->hndl, p_scb->codec_type);
2543 
2544         /* send avdtp discover request */
2545         AVDT_DiscoverReq(p_scb->peer_addr, p_scb->sep_info, BTA_AV_NUM_SEPS, bta_av_dt_cback[p_scb->hdi]);
2546     }
2547     else
2548     {
2549         p_scb->codec_type = p_scb->p_cap->codec_info[BTA_AV_CODEC_TYPE_IDX];
2550         memcpy(p_scb->cfg.codec_info, p_scb->p_cap->codec_info, AVDT_CODEC_SIZE);
2551         /* we may choose to use a different SEP at reconfig.
2552          * adjust the sep_idx now */
2553         bta_av_adjust_seps_idx(p_scb);
2554 
2555         /* open the stream with the new config */
2556         p_scb->sep_info_idx = p_scb->rcfg_idx;
2557         AVDT_OpenReq(p_scb->avdt_handle, p_scb->peer_addr,
2558                      p_scb->sep_info[p_scb->sep_info_idx].seid, p_scb->p_cap);
2559     }
2560 
2561 }
2562 
2563 /*******************************************************************************
2564 **
2565 ** Function         bta_av_security_rej
2566 **
2567 ** Description      Send an AVDTP security reject.
2568 **
2569 ** Returns          void
2570 **
2571 *******************************************************************************/
bta_av_security_rej(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2572 void bta_av_security_rej (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2573 {
2574     AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label, AVDT_ERR_BAD_STATE,
2575                      NULL, 0);
2576 }
2577 
2578 /*******************************************************************************
2579 **
2580 ** Function         bta_av_chk_2nd_start
2581 **
2582 ** Description      check if this is 2nd stream and if it needs to be started.
2583 **                  This function needs to be kept very similar to bta_av_chk_start
2584 **
2585 ** Returns          void
2586 **
2587 *******************************************************************************/
bta_av_chk_2nd_start(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2588 void bta_av_chk_2nd_start (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2589 {
2590     tBTA_AV_SCB *p_scbi;
2591     int i;
2592     BOOLEAN new_started = FALSE;
2593 
2594 
2595     if ((p_scb->chnl == BTA_AV_CHNL_AUDIO) && (bta_av_cb.audio_open_cnt >= 2))
2596     {
2597         /* more than one audio channel is connected */
2598         if (!(p_scb->role & BTA_AV_ROLE_SUSPEND_OPT))
2599         {
2600             /* this channel does not need to be reconfigured.
2601              * if there is other channel streaming, start the stream now */
2602             for(i=0; i<BTA_AV_NUM_STRS; i++)
2603             {
2604                 p_scbi = bta_av_cb.p_scb[i];
2605                 if(p_scbi && p_scbi->chnl == BTA_AV_CHNL_AUDIO && p_scbi->co_started)
2606                 {
2607                     if (!new_started)
2608                     {
2609                         /* start the new stream */
2610                         new_started = TRUE;
2611                         bta_av_ssm_execute(p_scb, BTA_AV_AP_START_EVT, NULL);
2612                     }
2613                     /* may need to update the flush timeout of this already started stream */
2614                     if (p_scbi->co_started != bta_av_cb.audio_open_cnt)
2615                     {
2616                         p_scbi->co_started = bta_av_cb.audio_open_cnt;
2617                         L2CA_SetFlushTimeout(p_scbi->peer_addr, p_bta_av_cfg->p_audio_flush_to[p_scbi->co_started - 1] );
2618                     }
2619                 }
2620             }
2621         }
2622     }
2623 }
2624 
2625 /*******************************************************************************
2626 **
2627 ** Function         bta_av_open_rc
2628 **
2629 ** Description      Send a message to main SM to open RC channel.
2630 **
2631 ** Returns          void
2632 **
2633 *******************************************************************************/
bta_av_open_rc(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2634 void bta_av_open_rc (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2635 {
2636     tBTA_AV_START   start;
2637 
2638     APPL_TRACE_DEBUG3("bta_av_open_rc use_rc: %d, wait: x%x role:x%x", p_scb->use_rc, p_scb->wait, p_scb->role);
2639     if ((p_scb->wait & BTA_AV_WAIT_ROLE_SW_BITS) && (p_scb->q_tag == BTA_AV_Q_TAG_START))
2640     {
2641         /* waiting for role switch for some reason & the timer expires */
2642         if (!bta_av_link_role_ok(p_scb, A2D_SET_ONE_BIT))
2643         {
2644             APPL_TRACE_ERROR0 ("failed to start streaming for role management reasons!!");
2645             bta_sys_stop_timer(&p_scb->timer);
2646             start.chnl   = p_scb->chnl;
2647             start.status = BTA_AV_FAIL_ROLE;
2648             start.initiator = TRUE;
2649             start.hndl   = p_scb->hndl;
2650             p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
2651             bta_av_cb.rs_idx = 0;
2652             (*bta_av_cb.p_cback)(BTA_AV_START_EVT, (tBTA_AV *) &start);
2653         }
2654         else
2655         {
2656             /* role switch is done. continue to start streaming */
2657             bta_av_cb.rs_idx = 0;
2658             p_data->hdr.offset = BTA_AV_RS_OK;
2659             bta_av_start_ok (p_scb, p_data);
2660         }
2661         return;
2662     }
2663 
2664     if(p_scb->use_rc == TRUE || (p_scb->role & BTA_AV_ROLE_AD_ACP) )
2665     {
2666         if(bta_av_cb.disc)
2667         {
2668             /* AVRC discover db is in use */
2669             if(p_scb->rc_handle == BTA_AV_RC_HANDLE_NONE)
2670             {
2671                 /* AVRC channel is not connected. delay a little bit */
2672                 if ((p_scb->wait & BTA_AV_WAIT_ROLE_SW_BITS) == 0)
2673                     bta_sys_start_timer(&p_scb->timer, BTA_AV_AVRC_TIMER_EVT, BTA_AV_RC_DISC_TIME_VAL);
2674                 else
2675                     p_scb->wait |= BTA_AV_WAIT_CHECK_RC;
2676             }
2677         }
2678         else
2679         {
2680             /* use main SM for AVRC SDP activities */
2681             bta_av_rc_disc((UINT8)(p_scb->hdi + 1));
2682         }
2683     }
2684     else
2685     {
2686         if(BTA_AV_RC_HANDLE_NONE != p_scb->rc_handle)
2687         {
2688             /* the open API said that this handle does not want a RC connection.
2689              * disconnect it now */
2690             AVRC_Close(p_scb->rc_handle);
2691         }
2692     }
2693 }
2694 
2695 /*******************************************************************************
2696 **
2697 ** Function         bta_av_open_at_inc
2698 **
2699 ** Description      This function is called if API open is called by application
2700 **                  while state-machine is at incoming state.
2701 **
2702 ** Returns          void
2703 **
2704 *******************************************************************************/
bta_av_open_at_inc(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2705 void bta_av_open_at_inc (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2706 {
2707     tBTA_AV_API_OPEN  *p_buf;
2708 
2709     memcpy (&(p_scb->open_api), &(p_data->api_open), sizeof(tBTA_AV_API_OPEN));
2710 
2711     if (p_scb->coll_mask & BTA_AV_COLL_INC_TMR)
2712     {
2713         p_scb->coll_mask |= BTA_AV_COLL_API_CALLED;
2714 
2715         /* API open will be handled at timeout if SNK did not start signalling. */
2716         /* API open will be ignored if SNK starts signalling.                   */
2717     }
2718     else
2719     {
2720         /* SNK did not start signalling, API was called N seconds timeout. */
2721         /* We need to switch to INIT state and start opening connection. */
2722         p_scb->coll_mask = 0;
2723         bta_av_set_scb_sst_init (p_scb);
2724 
2725         if ((p_buf = (tBTA_AV_API_OPEN *) GKI_getbuf(sizeof(tBTA_AV_API_OPEN))) != NULL)
2726         {
2727             memcpy(p_buf, &(p_scb->open_api), sizeof(tBTA_AV_API_OPEN));
2728             bta_sys_sendmsg(p_buf);
2729         }
2730     }
2731 }
2732 
2733 #endif /* BTA_AV_INCLUDED */
2734