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