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