OSDN Git Service

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