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