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