1 /******************************************************************************
3 * Copyright (C) 2004-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
19 /******************************************************************************
21 * This file contains action functions for advanced audio/video stream
22 * state machine. these functions are shared by both audio and video
25 ******************************************************************************/
27 #include "bt_target.h"
28 #if defined(BTA_AV_INCLUDED) && (BTA_AV_INCLUDED == TRUE)
31 #include "bta_av_int.h"
37 #if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
38 #include "bta_ar_api.h"
41 /*****************************************************************************
43 *****************************************************************************/
45 /* the delay time in milliseconds to start service discovery on AVRCP */
46 #ifndef BTA_AV_RC_DISC_TIME_VAL
47 #define BTA_AV_RC_DISC_TIME_VAL 3500
50 /* the timer in milliseconds to guard against link busy and AVDT_CloseReq failed to be sent */
51 #ifndef BTA_AV_CLOSE_REQ_TIME_VAL
52 #define BTA_AV_CLOSE_REQ_TIME_VAL 4000
55 /* number to retry on reconfigure failure - some headsets requirs this number to be more than 1 */
56 #ifndef BTA_AV_RECONFIG_RETRY
57 #define BTA_AV_RECONFIG_RETRY 6
60 /* state machine states */
72 /* the call out functions for audio stream */
73 const tBTA_AV_CO_FUNCTS bta_av_a2d_cos =
76 bta_av_co_audio_disc_res,
77 bta_av_co_audio_getconfig,
78 bta_av_co_audio_setconfig,
80 bta_av_co_audio_close,
81 bta_av_co_audio_start,
83 bta_av_co_audio_src_data_path,
87 /* ssm action functions for audio stream */
88 const tBTA_AV_SACT bta_av_a2d_action[] =
90 bta_av_do_disc_a2d, /* BTA_AV_DO_DISC */
91 bta_av_cleanup, /* BTA_AV_CLEANUP */
92 bta_av_free_sdb, /* BTA_AV_FREE_SDB */
93 bta_av_config_ind, /* BTA_AV_CONFIG_IND */
94 bta_av_disconnect_req, /* BTA_AV_DISCONNECT_REQ */
95 bta_av_security_req, /* BTA_AV_SECURITY_REQ */
96 bta_av_security_rsp, /* BTA_AV_SECURITY_RSP */
97 bta_av_setconfig_rsp, /* BTA_AV_SETCONFIG_RSP */
98 bta_av_st_rc_timer, /* BTA_AV_ST_RC_TIMER */
99 bta_av_str_opened, /* BTA_AV_STR_OPENED */
100 bta_av_security_ind, /* BTA_AV_SECURITY_IND */
101 bta_av_security_cfm, /* BTA_AV_SECURITY_CFM */
102 bta_av_do_close, /* BTA_AV_DO_CLOSE */
103 bta_av_connect_req, /* BTA_AV_CONNECT_REQ */
104 bta_av_sdp_failed, /* BTA_AV_SDP_FAILED */
105 bta_av_disc_results, /* BTA_AV_DISC_RESULTS */
106 bta_av_disc_res_as_acp, /* BTA_AV_DISC_RES_AS_ACP */
107 bta_av_open_failed, /* BTA_AV_OPEN_FAILED */
108 bta_av_getcap_results, /* BTA_AV_GETCAP_RESULTS */
109 bta_av_setconfig_rej, /* BTA_AV_SETCONFIG_REJ */
110 bta_av_discover_req, /* BTA_AV_DISCOVER_REQ */
111 bta_av_conn_failed, /* BTA_AV_CONN_FAILED */
112 bta_av_do_start, /* BTA_AV_DO_START */
113 bta_av_str_stopped, /* BTA_AV_STR_STOPPED */
114 bta_av_reconfig, /* BTA_AV_RECONFIG */
115 bta_av_data_path, /* BTA_AV_DATA_PATH */
116 bta_av_start_ok, /* BTA_AV_START_OK */
117 bta_av_start_failed, /* BTA_AV_START_FAILED */
118 bta_av_str_closed, /* BTA_AV_STR_CLOSED */
119 bta_av_clr_cong, /* BTA_AV_CLR_CONG */
120 bta_av_suspend_cfm, /* BTA_AV_SUSPEND_CFM */
121 bta_av_rcfg_str_ok, /* BTA_AV_RCFG_STR_OK */
122 bta_av_rcfg_failed, /* BTA_AV_RCFG_FAILED */
123 bta_av_rcfg_connect, /* BTA_AV_RCFG_CONNECT */
124 bta_av_rcfg_discntd, /* BTA_AV_RCFG_DISCNTD */
125 bta_av_suspend_cont, /* BTA_AV_SUSPEND_CONT */
126 bta_av_rcfg_cfm, /* BTA_AV_RCFG_CFM */
127 bta_av_rcfg_open, /* BTA_AV_RCFG_OPEN */
128 bta_av_security_rej, /* BTA_AV_SECURITY_REJ */
129 bta_av_open_rc, /* BTA_AV_OPEN_RC */
130 bta_av_chk_2nd_start, /* BTA_AV_CHK_2ND_START */
131 bta_av_save_caps, /* BTA_AV_SAVE_CAPS */
132 bta_av_set_use_rc, /* BTA_AV_SET_USE_RC */
133 bta_av_cco_close, /* BTA_AV_CCO_CLOSE */
134 bta_av_switch_role, /* BTA_AV_SWITCH_ROLE */
135 bta_av_role_res, /* BTA_AV_ROLE_RES */
136 bta_av_delay_co, /* BTA_AV_DELAY_CO */
137 bta_av_open_at_inc, /* BTA_AV_OPEN_AT_INC */
141 /* these tables translate AVDT events to SSM events */
142 static const UINT16 bta_av_stream_evt_ok[] = {
143 BTA_AV_STR_DISC_OK_EVT, /* AVDT_DISCOVER_CFM_EVT */
144 BTA_AV_STR_GETCAP_OK_EVT, /* AVDT_GETCAP_CFM_EVT */
145 BTA_AV_STR_OPEN_OK_EVT, /* AVDT_OPEN_CFM_EVT */
146 BTA_AV_STR_OPEN_OK_EVT, /* AVDT_OPEN_IND_EVT */
147 BTA_AV_STR_CONFIG_IND_EVT, /* AVDT_CONFIG_IND_EVT */
148 BTA_AV_STR_START_OK_EVT, /* AVDT_START_CFM_EVT */
149 BTA_AV_STR_START_OK_EVT, /* AVDT_START_IND_EVT */
150 BTA_AV_STR_SUSPEND_CFM_EVT, /* AVDT_SUSPEND_CFM_EVT */
151 BTA_AV_STR_SUSPEND_CFM_EVT, /* AVDT_SUSPEND_IND_EVT */
152 BTA_AV_STR_CLOSE_EVT, /* AVDT_CLOSE_CFM_EVT */
153 BTA_AV_STR_CLOSE_EVT, /* AVDT_CLOSE_IND_EVT */
154 BTA_AV_STR_RECONFIG_CFM_EVT, /* AVDT_RECONFIG_CFM_EVT */
155 0, /* AVDT_RECONFIG_IND_EVT */
156 BTA_AV_STR_SECURITY_CFM_EVT, /* AVDT_SECURITY_CFM_EVT */
157 BTA_AV_STR_SECURITY_IND_EVT, /* AVDT_SECURITY_IND_EVT */
158 BTA_AV_STR_WRITE_CFM_EVT, /* AVDT_WRITE_CFM_EVT */
159 BTA_AV_AVDT_CONNECT_EVT, /* AVDT_CONNECT_IND_EVT */
160 BTA_AV_AVDT_DISCONNECT_EVT, /* AVDT_DISCONNECT_IND_EVT */
161 #if (AVDT_REPORTING == TRUE)
162 BTA_AV_AVDT_RPT_CONN_EVT, /* AVDT_REPORT_CONN_EVT */
163 BTA_AV_AVDT_RPT_CONN_EVT, /* AVDT_REPORT_DISCONN_EVT */
165 BTA_AV_AVDT_DELAY_RPT_EVT, /* AVDT_DELAY_REPORT_EVT */
166 0 /* AVDT_DELAY_REPORT_CFM_EVT */
169 static const UINT16 bta_av_stream_evt_fail[] = {
170 BTA_AV_STR_DISC_FAIL_EVT, /* AVDT_DISCOVER_CFM_EVT */
171 BTA_AV_STR_GETCAP_FAIL_EVT, /* AVDT_GETCAP_CFM_EVT */
172 BTA_AV_STR_OPEN_FAIL_EVT, /* AVDT_OPEN_CFM_EVT */
173 BTA_AV_STR_OPEN_OK_EVT, /* AVDT_OPEN_IND_EVT */
174 BTA_AV_STR_CONFIG_IND_EVT, /* AVDT_CONFIG_IND_EVT */
175 BTA_AV_STR_START_FAIL_EVT, /* AVDT_START_CFM_EVT */
176 BTA_AV_STR_START_OK_EVT, /* AVDT_START_IND_EVT */
177 BTA_AV_STR_SUSPEND_CFM_EVT, /* AVDT_SUSPEND_CFM_EVT */
178 BTA_AV_STR_SUSPEND_CFM_EVT, /* AVDT_SUSPEND_IND_EVT */
179 BTA_AV_STR_CLOSE_EVT, /* AVDT_CLOSE_CFM_EVT */
180 BTA_AV_STR_CLOSE_EVT, /* AVDT_CLOSE_IND_EVT */
181 BTA_AV_STR_RECONFIG_CFM_EVT, /* AVDT_RECONFIG_CFM_EVT */
182 0, /* AVDT_RECONFIG_IND_EVT */
183 BTA_AV_STR_SECURITY_CFM_EVT, /* AVDT_SECURITY_CFM_EVT */
184 BTA_AV_STR_SECURITY_IND_EVT, /* AVDT_SECURITY_IND_EVT */
185 BTA_AV_STR_WRITE_CFM_EVT, /* AVDT_WRITE_CFM_EVT */
186 BTA_AV_AVDT_CONNECT_EVT, /* AVDT_CONNECT_IND_EVT */
187 BTA_AV_AVDT_DISCONNECT_EVT, /* AVDT_DISCONNECT_IND_EVT */
188 #if (AVDT_REPORTING == TRUE)
189 BTA_AV_AVDT_RPT_CONN_EVT, /* AVDT_REPORT_CONN_EVT */
190 BTA_AV_AVDT_RPT_CONN_EVT, /* AVDT_REPORT_DISCONN_EVT */
192 BTA_AV_AVDT_DELAY_RPT_EVT, /* AVDT_DELAY_REPORT_EVT */
193 0 /* AVDT_DELAY_REPORT_CFM_EVT */
196 static void bta_av_stream0_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data);
197 static void bta_av_stream1_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data);
198 #if BTA_AV_NUM_STRS > 2
199 static void bta_av_stream2_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data);
201 #if BTA_AV_NUM_STRS > 3
202 static void bta_av_stream3_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data);
204 #if BTA_AV_NUM_STRS > 4
205 static void bta_av_stream4_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data);
207 #if BTA_AV_NUM_STRS > 5
208 static void bta_av_stream5_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data);
210 /* the array of callback functions to receive events from AVDT control channel */
211 tAVDT_CTRL_CBACK * const bta_av_dt_cback[] =
214 ,bta_av_stream1_cback
215 #if BTA_AV_NUM_STRS > 2
216 ,bta_av_stream2_cback
218 #if BTA_AV_NUM_STRS > 3
219 ,bta_av_stream3_cback
221 #if BTA_AV_NUM_STRS > 4
222 ,bta_av_stream4_cback
224 #if BTA_AV_NUM_STRS > 5
225 ,bta_av_stream5_cback
229 /*******************************************************************************
231 ** Function bta_av_save_addr
233 ** Description copy the bd_addr and maybe reset the supported flags
238 *******************************************************************************/
239 static void bta_av_save_addr(tBTA_AV_SCB *p_scb, const BD_ADDR b)
241 APPL_TRACE_DEBUG2("bta_av_save_addr r:%d, s:%d",
242 p_scb->recfg_sup, p_scb->suspend_sup);
243 if(bdcmp(p_scb->peer_addr, b) != 0)
245 APPL_TRACE_ERROR0("reset flags");
246 /* a new addr, reset the supported flags */
247 p_scb->recfg_sup = TRUE;
248 p_scb->suspend_sup = TRUE;
251 /* do this copy anyway, just in case the first addr matches
252 * the control block one by accident */
253 bdcpy(p_scb->peer_addr, b);
256 /*******************************************************************************
258 ** Function notify_start_failed
260 ** Description notify up-layer AV start failed
265 *******************************************************************************/
266 static void notify_start_failed(tBTA_AV_SCB *p_scb)
269 /* if start failed, clear role */
270 p_scb->role &= ~BTA_AV_ROLE_START_INT;
271 start.chnl = p_scb->chnl;
272 start.status = BTA_AV_FAIL;
273 start.initiator = TRUE;
274 start.hndl = p_scb->hndl;
275 (*bta_av_cb.p_cback)(BTA_AV_START_EVT, (tBTA_AV *) &start);
278 /*******************************************************************************
280 ** Function bta_av_st_rc_timer
282 ** Description start the AVRC timer if no RC connection & CT is supported &
284 ** as ACP (we do not really know if we want AVRC)
288 *******************************************************************************/
289 void bta_av_st_rc_timer(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
293 APPL_TRACE_DEBUG2("bta_av_st_rc_timer rc_handle:%d, use_rc: %d",
294 p_scb->rc_handle, p_scb->use_rc);
295 /* for outgoing RC connection as INT/CT */
296 if( (p_scb->rc_handle == BTA_AV_RC_HANDLE_NONE) &&
297 /*(bta_av_cb.features & BTA_AV_FEAT_RCCT) &&*/
298 (p_scb->use_rc == TRUE || (p_scb->role & BTA_AV_ROLE_AD_ACP)) )
300 if ((p_scb->wait & BTA_AV_WAIT_ROLE_SW_BITS) == 0)
301 bta_sys_start_timer(&p_scb->timer, BTA_AV_AVRC_TIMER_EVT, BTA_AV_RC_DISC_TIME_VAL);
303 p_scb->wait |= BTA_AV_WAIT_CHECK_RC;
308 /*******************************************************************************
310 ** Function bta_av_next_getcap
312 ** Description The function gets the capabilities of the next available
313 ** stream found in the discovery results.
315 ** Returns TRUE if we sent request to AVDT, FALSE otherwise.
317 *******************************************************************************/
318 static BOOLEAN bta_av_next_getcap(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
321 tAVDT_GETCAP_REQ *p_req;
322 BOOLEAN sent_cmd = FALSE;
324 for (i = p_scb->sep_info_idx; i < p_scb->num_seps; i++)
326 /* steam not in use, is a sink, and is the right media type (audio/video) */
327 if ((p_scb->sep_info[i].in_use == FALSE) &&
328 (p_scb->sep_info[i].tsep == AVDT_TSEP_SNK) &&
329 (p_scb->sep_info[i].media_type == p_scb->media_type))
331 p_scb->sep_info_idx = i;
333 /* we got a stream; get its capabilities */
334 if (p_scb->p_cap == NULL)
336 p_scb->p_cap = (tAVDT_CFG *) GKI_getbuf(sizeof(tAVDT_CFG));
338 if (p_scb->p_cap == NULL)
343 if (p_scb->avdt_version >= AVDT_VERSION_SYNC)
345 p_req = AVDT_GetAllCapReq;
349 p_req = AVDT_GetCapReq;
351 (*p_req)(p_scb->peer_addr,
352 p_scb->sep_info[i].seid,
353 p_scb->p_cap, bta_av_dt_cback[p_scb->hdi]);
359 /* if no streams available then stream open fails */
362 bta_av_ssm_execute(p_scb, BTA_AV_STR_GETCAP_FAIL_EVT, p_data);
369 /*******************************************************************************
371 ** Function bta_av_proc_stream_evt
373 ** Description Utility function to compose stream events.
377 *******************************************************************************/
378 void bta_av_proc_stream_evt(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data, int index)
380 tBTA_AV_STR_MSG *p_msg;
382 tBTA_AV_SCB *p_scb = bta_av_cb.p_scb[index];
387 if (event == AVDT_SECURITY_IND_EVT)
389 sec_len = (p_data->security_ind.len < BTA_AV_SECURITY_MAX_LEN) ?
390 p_data->security_ind.len : BTA_AV_SECURITY_MAX_LEN;
392 else if (event == AVDT_SECURITY_CFM_EVT && p_data->hdr.err_code == 0)
394 sec_len = (p_data->security_cfm.len < BTA_AV_SECURITY_MAX_LEN) ?
395 p_data->security_cfm.len : BTA_AV_SECURITY_MAX_LEN;
399 if (p_scb && (p_msg = (tBTA_AV_STR_MSG *) GKI_getbuf((UINT16) (sizeof(tBTA_AV_STR_MSG) + sec_len))) != NULL)
402 /* copy event data, bd addr, and handle to event message buffer */
403 p_msg->hdr.offset = 0;
407 bdcpy(p_msg->bd_addr, bd_addr);
408 APPL_TRACE_DEBUG6(" bd_addr:%02x-%02x-%02x-%02x-%02x-%02x",
409 bd_addr[0], bd_addr[1],
410 bd_addr[2], bd_addr[3],
411 bd_addr[4], bd_addr[5]);
416 memcpy(&p_msg->msg, p_data, sizeof (tAVDT_CTRL));
417 /* copy config params to event message buffer */
420 case AVDT_RECONFIG_CFM_EVT:
421 APPL_TRACE_DEBUG4("reconfig cfm event codec info = 0x%06x-%06x-%06x-%02x",
422 (p_msg->msg.reconfig_cfm.p_cfg->codec_info[0]<<16)+(p_msg->msg.reconfig_cfm.p_cfg->codec_info[1]<<8)+p_msg->msg.reconfig_cfm.p_cfg->codec_info[2],
423 (p_msg->msg.reconfig_cfm.p_cfg->codec_info[3]<<16)+(p_msg->msg.reconfig_cfm.p_cfg->codec_info[4]<<8)+p_msg->msg.reconfig_cfm.p_cfg->codec_info[5],
424 (p_msg->msg.reconfig_cfm.p_cfg->codec_info[6]<<16)+(p_msg->msg.reconfig_cfm.p_cfg->codec_info[7]<<8)+p_msg->msg.reconfig_cfm.p_cfg->codec_info[8],
425 p_msg->msg.reconfig_cfm.p_cfg->codec_info[9]);
430 case AVDT_CONFIG_IND_EVT:
431 /* We might have 2 SEP signallings(A2DP + VDP) with one peer device on one L2CAP.
432 * If we already have a signalling connection with the bd_addr and the streaming
433 * SST is at INIT state, change it to INCOMING state to handle the signalling
434 * from the 2nd SEP. */
435 if ((bta_av_find_lcb(bd_addr, BTA_AV_LCB_FIND) != NULL) && (bta_av_is_scb_init(p_scb)))
437 bta_av_set_scb_sst_incoming (p_scb);
439 /* When ACP_CONNECT_EVT was received, we put first available scb to incoming state.
440 * Later when we receive AVDT_CONFIG_IND_EVT, we use a new p_scb and set its state to
441 * incoming which we do it above.
442 * We also have to set the old p_scb state to init to be used later */
443 for (xx = 0; xx < BTA_AV_NUM_STRS; xx++)
445 if ((bta_av_cb.p_scb[xx]) && (xx != index))
447 if (bta_av_cb.p_scb[xx]->state == BTA_AV_INCOMING_SST)
449 bta_av_cb.p_scb[xx]->state = BTA_AV_INIT_SST;
450 bta_av_cb.p_scb[xx]->coll_mask = 0;
457 memcpy(&p_msg->cfg, p_data->config_ind.p_cfg, sizeof(tAVDT_CFG));
460 case AVDT_SECURITY_IND_EVT:
461 p_msg->msg.security_ind.p_data = (UINT8 *) (p_msg + 1);
462 memcpy(p_msg->msg.security_ind.p_data, p_data->security_ind.p_data, sec_len);
465 case AVDT_SECURITY_CFM_EVT:
466 p_msg->msg.security_cfm.p_data = (UINT8 *) (p_msg + 1);
467 if (p_data->hdr.err_code == 0)
469 memcpy(p_msg->msg.security_cfm.p_data, p_data->security_cfm.p_data, sec_len);
472 case AVDT_SUSPEND_IND_EVT:
473 p_msg->msg.hdr.err_code = 0;
481 p_msg->msg.hdr.err_code = 0;
483 /* look up application event */
484 if ((p_data == NULL) || (p_data->hdr.err_code == 0))
486 p_msg->hdr.event = bta_av_stream_evt_ok[event];
490 p_msg->hdr.event = bta_av_stream_evt_fail[event];
493 p_msg->initiator = FALSE;
494 if (event == AVDT_SUSPEND_CFM_EVT)
495 p_msg->initiator = TRUE;
497 APPL_TRACE_VERBOSE1("hndl:x%x", p_scb->hndl);
498 p_msg->hdr.layer_specific = p_scb->hndl;
499 p_msg->handle = handle;
500 p_msg->avdt_event = event;
501 bta_sys_sendmsg(p_msg);
504 /* coverity[var_deref_model] */
505 /* false-positive: bta_av_conn_cback only processes AVDT_CONNECT_IND_EVT and AVDT_DISCONNECT_IND_EVT event
506 * these 2 events always have associated p_data */
507 bta_av_conn_cback(handle, bd_addr, event, p_data);
510 /*******************************************************************************
512 ** Function bta_av_stream0_cback
514 ** Description This is the AVDTP callback function for stream events.
518 *******************************************************************************/
519 static void bta_av_stream0_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
521 APPL_TRACE_VERBOSE2("bta_av_stream0_cback avdt_handle: %d event=0x%x", handle, event);
522 bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 0);
525 /*******************************************************************************
527 ** Function bta_av_stream1_cback
529 ** Description This is the AVDTP callback function for stream events.
533 *******************************************************************************/
534 static void bta_av_stream1_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
536 APPL_TRACE_EVENT2("bta_av_stream1_cback avdt_handle: %d event=0x%x", handle, event);
537 bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 1);
540 #if BTA_AV_NUM_STRS > 2
541 /*******************************************************************************
543 ** Function bta_av_stream2_cback
545 ** Description This is the AVDTP callback function for stream events.
549 *******************************************************************************/
550 static void bta_av_stream2_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
552 APPL_TRACE_EVENT2("bta_av_stream2_cback avdt_handle: %d event=0x%x", handle, event);
553 bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 2);
557 #if BTA_AV_NUM_STRS > 3
558 /*******************************************************************************
560 ** Function bta_av_stream3_cback
562 ** Description This is the AVDTP callback function for stream events.
566 *******************************************************************************/
567 static void bta_av_stream3_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
569 APPL_TRACE_EVENT2("bta_av_stream3_cback avdt_handle: %d event=0x%x", handle, event);
570 bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 3);
574 /*******************************************************************************
576 ** Function bta_av_stream4_cback
578 ** Description This is the AVDTP callback function for stream events.
582 *******************************************************************************/
583 #if BTA_AV_NUM_STRS > 4
584 static void bta_av_stream4_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
586 APPL_TRACE_EVENT2("bta_av_stream4_cback avdt_handle: %d event=0x%x", handle, event);
587 bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 4);
591 /*******************************************************************************
593 ** Function bta_av_stream5_cback
595 ** Description This is the AVDTP callback function for stream events.
599 *******************************************************************************/
600 #if BTA_AV_NUM_STRS > 5
601 static void bta_av_stream5_cback(UINT8 handle, BD_ADDR bd_addr, UINT8 event, tAVDT_CTRL *p_data)
603 APPL_TRACE_EVENT2("bta_av_stream5_cback avdt_handle: %d event=0x%x", handle, event);
604 bta_av_proc_stream_evt(handle, bd_addr, event, p_data, 5);
608 /*******************************************************************************
610 ** Function bta_av_a2d_sdp_cback
612 ** Description A2DP service discovery callback.
616 *******************************************************************************/
617 static void bta_av_a2d_sdp_cback(BOOLEAN found, tA2D_Service *p_service)
619 tBTA_AV_SDP_RES *p_msg;
622 if ((p_msg = (tBTA_AV_SDP_RES *) GKI_getbuf(sizeof(tBTA_AV_SDP_RES))) != NULL)
624 p_msg->hdr.event = (found) ? BTA_AV_SDP_DISC_OK_EVT : BTA_AV_SDP_DISC_FAIL_EVT;
626 p_scb = bta_av_hndl_to_scb(bta_av_cb.handle);
629 if (found && (p_service != NULL))
630 p_scb->avdt_version = p_service->avdt_version;
632 p_scb->avdt_version = 0x00;
634 p_msg->hdr.layer_specific = bta_av_cb.handle;
635 bta_sys_sendmsg(p_msg);
639 APPL_TRACE_ERROR1 ("bta_av_a2d_sdp_cback, no scb found for handle(0x%x)", bta_av_cb.handle);
644 /*******************************************************************************
646 ** Function bta_av_adjust_seps_idx
648 ** Description adjust the sep_idx
652 *******************************************************************************/
653 static void bta_av_adjust_seps_idx(tBTA_AV_SCB *p_scb)
657 APPL_TRACE_DEBUG1("bta_av_adjust_seps_idx codec_type: %d", p_scb->codec_type);
658 for(xx=0; xx<BTA_AV_MAX_SEPS; xx++)
660 APPL_TRACE_DEBUG2("av_handle: %d codec_type: %d",
661 p_scb->seps[xx].av_handle, p_scb->seps[xx].codec_type);
662 if(p_scb->seps[xx].av_handle && p_scb->codec_type == p_scb->seps[xx].codec_type)
665 p_scb->avdt_handle = p_scb->seps[xx].av_handle;
671 /*******************************************************************************
673 ** Function bta_av_switch_role
675 ** Description Switch role was not started and a timer was started.
676 ** another attempt to switch role now - still opening.
680 *******************************************************************************/
681 void bta_av_switch_role (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
683 tBTA_AV_RS_RES switch_res = BTA_AV_RS_NONE;
684 tBTA_AV_API_OPEN *p_buf = &p_scb->q_info.open;
687 APPL_TRACE_DEBUG1("bta_av_switch_role wait:x%x", p_scb->wait);
688 if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_RES_START)
689 p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RETRY;
691 /* clear the masks set when the timer is started */
692 p_scb->wait &= ~(BTA_AV_WAIT_ROLE_SW_RES_OPEN|BTA_AV_WAIT_ROLE_SW_RES_START);
694 if (p_scb->q_tag == BTA_AV_Q_TAG_OPEN)
696 if (bta_av_switch_if_needed(p_scb) || !bta_av_link_role_ok(p_scb, A2D_SET_MULTL_BIT))
698 p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RES_OPEN;
702 /* this should not happen in theory. Just in case...
703 * continue to do_disc_a2d */
704 switch_res = BTA_AV_RS_DONE;
709 /* report failure on OPEN */
710 switch_res = BTA_AV_RS_FAIL;
713 if (switch_res != BTA_AV_RS_NONE)
715 if (bta_av_cb.rs_idx == (p_scb->hdi + 1))
717 bta_av_cb.rs_idx = 0;
719 p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_RETRY;
721 p_buf->switch_res = switch_res;
722 bta_av_do_disc_a2d(p_scb, (tBTA_AV_DATA *)p_buf);
726 /*******************************************************************************
728 ** Function bta_av_role_res
730 ** Description Handle the role changed event
735 *******************************************************************************/
736 void bta_av_role_res (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
738 BOOLEAN initiator = FALSE;
740 tBTA_AV_OPEN av_open;
742 APPL_TRACE_DEBUG3("bta_av_role_res q_tag:%d, wait:x%x, role:x%x", p_scb->q_tag, p_scb->wait, p_scb->role);
743 if (p_scb->role & BTA_AV_ROLE_START_INT)
746 if (p_scb->q_tag == BTA_AV_Q_TAG_START)
748 if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_STARTED)
750 p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
751 if (p_data->role_res.hci_status != HCI_SUCCESS)
753 p_scb->role &= ~BTA_AV_ROLE_START_INT;
754 bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
755 /* start failed because of role switch. */
756 start.chnl = p_scb->chnl;
757 start.status = BTA_AV_FAIL_ROLE;
758 start.hndl = p_scb->hndl;
759 start.initiator = initiator;
760 (*bta_av_cb.p_cback)(BTA_AV_START_EVT, (tBTA_AV *) &start);
764 bta_av_start_ok(p_scb, p_data);
767 else if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_RES_START)
768 p_scb->wait |= BTA_AV_WAIT_ROLE_SW_FAILED;
770 else if (p_scb->q_tag == BTA_AV_Q_TAG_OPEN)
772 if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_RES_OPEN)
774 p_scb->role &= ~BTA_AV_ROLE_START_INT;
775 p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
777 if (p_data->role_res.hci_status != HCI_SUCCESS)
779 /* Open failed because of role switch. */
780 bdcpy(av_open.bd_addr, p_scb->peer_addr);
781 av_open.chnl = p_scb->chnl;
782 av_open.hndl = p_scb->hndl;
783 start.status = BTA_AV_FAIL_ROLE;
784 (*bta_av_cb.p_cback)(BTA_AV_OPEN_EVT, (tBTA_AV *)&av_open);
788 /* Continue av open process */
789 p_scb->q_info.open.switch_res = BTA_AV_RS_DONE;
790 bta_av_do_disc_a2d (p_scb, (tBTA_AV_DATA *)&(p_scb->q_info.open));
795 APPL_TRACE_WARNING2 ("Unexpected role switch event: q_tag = %d wait = %d", p_scb->q_tag, p_scb->wait);
799 APPL_TRACE_DEBUG2("wait:x%x, role:x%x", p_scb->wait, p_scb->role);
802 /*******************************************************************************
804 ** Function bta_av_delay_co
806 ** Description Call the delay call-out function to report the delay report
811 *******************************************************************************/
812 void bta_av_delay_co (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
814 p_scb->p_cos->delay(p_scb->hndl, p_data->str_msg.msg.delay_rpt_cmd.delay);
817 /*******************************************************************************
819 ** Function bta_av_do_disc_a2d
821 ** Description Do service discovery for A2DP.
825 *******************************************************************************/
826 void bta_av_do_disc_a2d (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
828 BOOLEAN ok_continue = FALSE;
829 tA2D_SDP_DB_PARAMS db_params;
830 UINT16 attr_list[] = {ATTR_ID_SERVICE_CLASS_ID_LIST,
831 ATTR_ID_PROTOCOL_DESC_LIST,
832 ATTR_ID_BT_PROFILE_DESC_LIST};
834 APPL_TRACE_DEBUG3("bta_av_do_disc_a2d use_rc: %d rs:%d, oc:%d",
835 p_data->api_open.use_rc, p_data->api_open.switch_res, bta_av_cb.audio_open_cnt);
837 memcpy (&(p_scb->open_api), &(p_data->api_open), sizeof(tBTA_AV_API_OPEN));
839 switch(p_data->api_open.switch_res)
842 if (bta_av_switch_if_needed(p_scb) || !bta_av_link_role_ok(p_scb, A2D_SET_MULTL_BIT))
844 /* waiting for role switch result. save the api to control block */
845 memcpy(&p_scb->q_info.open, &p_data->api_open, sizeof(tBTA_AV_API_OPEN));
846 p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RES_OPEN;
847 p_scb->q_tag = BTA_AV_Q_TAG_OPEN;
856 /* report a new failure event */
857 p_scb->open_status = BTA_AV_FAIL_ROLE;
858 bta_av_ssm_execute(p_scb, BTA_AV_SDP_DISC_FAIL_EVT, NULL);
862 p_data = (tBTA_AV_DATA *)&p_scb->q_info.open;
863 /* continue to open if link role is ok */
864 if (bta_av_link_role_ok(p_scb, A2D_SET_MULTL_BIT))
870 p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RES_OPEN;
879 APPL_TRACE_DEBUG3("ok_continue: %d wait:x%x, q_tag: %d", ok_continue, p_scb->wait, p_scb->q_tag);
883 /* clear the role switch bits */
884 p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
886 if (p_scb->wait & BTA_AV_WAIT_CHECK_RC)
888 p_scb->wait &= ~BTA_AV_WAIT_CHECK_RC;
889 bta_sys_start_timer(&p_scb->timer, BTA_AV_AVRC_TIMER_EVT, BTA_AV_RC_DISC_TIME_VAL);
892 if (bta_av_cb.features & BTA_AV_FEAT_MASTER)
894 L2CA_SetDesireRole(L2CAP_ROLE_DISALLOW_SWITCH);
896 if (bta_av_cb.audio_open_cnt == 1)
898 /* there's already an A2DP connection. do not allow switch */
899 bta_sys_clear_default_policy(BTA_ID_AV, HCI_ENABLE_MASTER_SLAVE_SWITCH);
902 /* store peer addr other parameters */
903 bta_av_save_addr(p_scb, p_data->api_open.bd_addr);
904 p_scb->sec_mask = p_data->api_open.sec_mask;
905 p_scb->use_rc = p_data->api_open.use_rc;
907 bta_sys_app_open(BTA_ID_AV, p_scb->app_id, p_scb->peer_addr);
909 /* allocate discovery database */
910 if (p_scb->p_disc_db == NULL)
912 p_scb->p_disc_db = (tSDP_DISCOVERY_DB *) GKI_getbuf(BTA_AV_DISC_BUF_SIZE);
915 /* only one A2D find service is active at a time */
916 bta_av_cb.handle = p_scb->hndl;
920 /* set up parameters */
921 db_params.db_len = BTA_AV_DISC_BUF_SIZE;
922 db_params.num_attr = 3;
923 db_params.p_db = p_scb->p_disc_db;
924 db_params.p_attrs = attr_list;
926 if(A2D_FindService(UUID_SERVCLASS_AUDIO_SINK, p_scb->peer_addr, &db_params,
927 bta_av_a2d_sdp_cback) == A2D_SUCCESS)
933 /* when the code reaches here, either the DB is NULL
934 * or A2D_FindService is not successful */
935 bta_av_a2d_sdp_cback(FALSE, NULL);
938 /*******************************************************************************
940 ** Function bta_av_cleanup
942 ** Description cleanup AV stream control block.
946 *******************************************************************************/
947 void bta_av_cleanup(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
949 tBTA_AV_CONN_CHG msg;
951 UINT8 role = BTA_AV_ROLE_AD_INT;
954 APPL_TRACE_DEBUG0("bta_av_cleanup");
956 /* free any buffers */
957 utl_freebuf((void **) &p_scb->p_cap);
958 utl_freebuf((void **) &p_scb->p_disc_db);
959 p_scb->avdt_version = 0;
961 /* initialize some control block variables */
962 p_scb->open_status = BTA_AV_SUCCESS;
964 /* if de-registering shut everything down */
965 msg.hdr.layer_specific = p_scb->hndl;
966 p_scb->started = FALSE;
969 p_scb->cur_psc_mask = 0;
971 p_scb->num_disc_snks = 0;
972 bta_sys_stop_timer(&p_scb->timer);
973 if (p_scb->deregistring)
976 for(xx=0; xx<BTA_AV_MAX_SEPS; xx++)
978 if(p_scb->seps[xx].av_handle)
979 AVDT_RemoveStream(p_scb->seps[xx].av_handle);
980 p_scb->seps[xx].av_handle = 0;
983 bta_av_dereg_comp((tBTA_AV_DATA *) &msg);
987 /* report stream closed to main SM */
989 bdcpy(msg.peer_addr, p_scb->peer_addr);
990 bta_av_conn_chg((tBTA_AV_DATA *) &msg);
994 /*******************************************************************************
996 ** Function bta_av_free_sdb
998 ** Description Free service discovery db buffer.
1002 *******************************************************************************/
1003 void bta_av_free_sdb(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1006 utl_freebuf((void **) &p_scb->p_disc_db);
1009 /*******************************************************************************
1011 ** Function bta_av_config_ind
1013 ** Description Handle a stream configuration indication from the peer.
1017 *******************************************************************************/
1018 void bta_av_config_ind (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1020 tBTA_AV_CI_SETCONFIG setconfig;
1021 tAVDT_SEP_INFO *p_info;
1022 tAVDT_CFG *p_evt_cfg = &p_data->str_msg.cfg;
1023 UINT8 psc_mask = (p_evt_cfg->psc_mask | p_scb->cfg.psc_mask);
1026 p_scb->avdt_label = p_data->str_msg.msg.hdr.label;
1027 memcpy(p_scb->cfg.codec_info, p_evt_cfg->codec_info, AVDT_CODEC_SIZE);
1028 p_scb->codec_type = p_evt_cfg->codec_info[BTA_AV_CODEC_TYPE_IDX];
1029 bta_av_save_addr(p_scb, p_data->str_msg.bd_addr);
1031 /* Clear collision mask */
1032 p_scb->coll_mask = 0;
1033 bta_sys_stop_timer(&bta_av_cb.acp_sig_tmr);
1035 /* if no codec parameters in configuration, fail */
1036 if ((p_evt_cfg->num_codec == 0) ||
1037 /* or the peer requests for a service we do not support */
1038 ((psc_mask != p_scb->cfg.psc_mask) &&
1039 (psc_mask != (p_scb->cfg.psc_mask&~AVDT_PSC_DELAY_RPT))) )
1041 setconfig.hndl = p_scb->hndl; /* we may not need this */
1042 setconfig.err_code = AVDT_ERR_UNSUP_CFG;
1043 bta_av_ssm_execute(p_scb, BTA_AV_CI_SETCONFIG_FAIL_EVT, (tBTA_AV_DATA *) &setconfig);
1047 p_info = &p_scb->sep_info[0];
1049 p_info->media_type = p_scb->media_type;
1050 p_info->seid = p_data->str_msg.msg.config_ind.int_seid;
1051 p_info->tsep = AVDT_TSEP_SNK;
1052 p_scb->role |= BTA_AV_ROLE_AD_ACP;
1053 p_scb->cur_psc_mask = p_evt_cfg->psc_mask;
1054 if (bta_av_cb.features & BTA_AV_FEAT_RCTG)
1055 p_scb->use_rc = TRUE;
1057 p_scb->use_rc = FALSE;
1059 p_scb->num_seps = 1;
1060 p_scb->sep_info_idx = 0;
1061 APPL_TRACE_DEBUG3("bta_av_config_ind: SEID: %d use_rc: %d cur_psc_mask:0x%x", p_info->seid, p_scb->use_rc, p_scb->cur_psc_mask);
1063 p_scb->p_cos->setcfg(p_scb->hndl, p_scb->codec_type,
1064 p_evt_cfg->codec_info,
1067 p_evt_cfg->num_protect,
1068 p_evt_cfg->protect_info);
1072 /*******************************************************************************
1074 ** Function bta_av_disconnect_req
1076 ** Description Disconnect AVDTP connection.
1080 *******************************************************************************/
1081 void bta_av_disconnect_req (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1086 APPL_TRACE_DEBUG1("bta_av_disconnect_req conn_lcb: 0x%x", bta_av_cb.conn_lcb);
1088 bta_sys_stop_timer(&bta_av_cb.sig_tmr);
1089 bta_sys_stop_timer(&p_scb->timer);
1090 if(bta_av_cb.conn_lcb)
1092 p_rcb = bta_av_get_rcb_by_shdl((UINT8)(p_scb->hdi + 1));
1094 bta_av_del_rc(p_rcb);
1095 AVDT_DisconnectReq(p_scb->peer_addr, bta_av_dt_cback[p_scb->hdi]);
1099 bta_av_ssm_execute(p_scb, BTA_AV_AVDT_DISCONNECT_EVT, NULL);
1103 /*******************************************************************************
1105 ** Function bta_av_security_req
1107 ** Description Send an AVDTP security request.
1111 *******************************************************************************/
1112 void bta_av_security_req (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1114 if (bta_av_cb.features & BTA_AV_FEAT_PROTECT)
1116 AVDT_SecurityReq(p_scb->avdt_handle, p_data->api_protect_req.p_data,
1117 p_data->api_protect_req.len);
1121 /*******************************************************************************
1123 ** Function bta_av_security_rsp
1125 ** Description Send an AVDTP security response.
1129 *******************************************************************************/
1130 void bta_av_security_rsp (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1132 if (bta_av_cb.features & BTA_AV_FEAT_PROTECT)
1134 AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label, p_data->api_protect_rsp.error_code,
1135 p_data->api_protect_rsp.p_data, p_data->api_protect_rsp.len);
1139 AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label, AVDT_ERR_NSC,
1144 /*******************************************************************************
1146 ** Function bta_av_setconfig_rsp
1148 ** Description setconfig is OK
1152 *******************************************************************************/
1153 void bta_av_setconfig_rsp (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1155 UINT8 num = p_data->ci_setconfig.num_seid + 1;
1156 UINT8 *p_seid = p_data->ci_setconfig.p_seid;
1159 /* we like this codec_type. find the sep_idx */
1160 bta_av_adjust_seps_idx(p_scb);
1161 APPL_TRACE_DEBUG2("bta_av_setconfig_rsp: sep_idx: %d cur_psc_mask:0x%x", p_scb->sep_idx, p_scb->cur_psc_mask);
1162 AVDT_ConfigRsp(p_scb->avdt_handle, p_scb->avdt_label, p_data->ci_setconfig.err_code,
1163 p_data->ci_setconfig.category);
1165 bta_sys_stop_timer(&bta_av_cb.sig_tmr);
1167 if(p_data->ci_setconfig.err_code == AVDT_SUCCESS)
1169 p_scb->wait = BTA_AV_WAIT_ACP_CAPS_ON;
1170 if(p_data->ci_setconfig.recfg_needed)
1171 p_scb->role |= BTA_AV_ROLE_SUSPEND_OPT;
1172 APPL_TRACE_ERROR3("bta_av_setconfig_rsp recfg_needed:%d role:x%x num:%d",
1173 p_data->ci_setconfig.recfg_needed, p_scb->role, num);
1174 /* callout module tells BTA the number of "good" SEPs and their SEIDs.
1175 * getcap on these SEID */
1176 p_scb->num_seps = num;
1178 if (p_scb->cur_psc_mask & AVDT_PSC_DELAY_RPT)
1179 p_scb->avdt_version = AVDT_VERSION_SYNC;
1182 if (p_scb->codec_type == BTA_AV_CODEC_SBC || num > 1)
1184 /* if SBC is used by the SNK as INT, discover req is not sent in bta_av_config_ind.
1185 * call disc_res now */
1186 p_scb->p_cos->disc_res(p_scb->hndl, num, num, p_scb->peer_addr);
1190 /* we do not know the peer device and it is using non-SBC codec
1191 * we need to know all the SEPs on SNK */
1192 bta_av_discover_req(p_scb, NULL);
1196 for (i = 1; i < num; i++)
1198 APPL_TRACE_DEBUG2("sep_info[%d] SEID: %d", i, p_seid[i-1]);
1199 /* initialize the sep_info[] to get capabilities */
1200 p_scb->sep_info[i].in_use = FALSE;
1201 p_scb->sep_info[i].tsep = AVDT_TSEP_SNK;
1202 p_scb->sep_info[i].media_type = p_scb->media_type;
1203 p_scb->sep_info[i].seid = p_seid[i-1];
1205 bta_av_next_getcap(p_scb, p_data);
1209 /*******************************************************************************
1211 ** Function bta_av_str_opened
1213 ** Description Stream opened OK (incoming/outgoing).
1217 *******************************************************************************/
1218 void bta_av_str_opened (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1220 tBTA_AV_CONN_CHG msg;
1225 msg.hdr.layer_specific = p_scb->hndl;
1227 bdcpy(msg.peer_addr, p_scb->peer_addr);
1228 p_scb->l2c_cid = AVDT_GetL2CapChannel(p_scb->avdt_handle);
1229 bta_av_conn_chg((tBTA_AV_DATA *) &msg);
1230 /* set the congestion flag, so AV would not send media packets by accident */
1234 p_scb->stream_mtu = p_data->str_msg.msg.open_ind.peer_mtu - AVDT_MEDIA_HDR_SIZE;
1235 mtu = bta_av_chk_mtu(p_scb, p_scb->stream_mtu);
1236 APPL_TRACE_DEBUG3("bta_av_str_opened l2c_cid: 0x%x stream_mtu: %d mtu: %d",
1237 p_scb->l2c_cid, p_scb->stream_mtu, mtu);
1238 if(mtu == 0 || mtu > p_scb->stream_mtu)
1239 mtu = p_scb->stream_mtu;
1241 /* Set the media channel as medium priority */
1242 L2CA_SetTxPriority(p_scb->l2c_cid, L2CAP_CHNL_PRIORITY_MEDIUM);
1243 L2CA_SetChnlFlushability (p_scb->l2c_cid, TRUE);
1245 bta_sys_conn_open(BTA_ID_AV, p_scb->app_id, p_scb->peer_addr);
1246 memset(&p_scb->q_info, 0, sizeof(tBTA_AV_Q_INFO));
1248 p_scb->l2c_bufs = 0;
1249 p_scb->p_cos->open(p_scb->hndl,
1250 p_scb->codec_type, p_scb->cfg.codec_info, mtu);
1253 /* TODO check if other audio channel is open.
1254 * If yes, check if reconfig is needed
1255 * Rigt now we do not do this kind of checking.
1256 * BTA-AV is INT for 2nd audio connection.
1257 * The application needs to make sure the current codec_info is proper.
1258 * If one audio connection is open and another SNK attempts to connect to AV,
1259 * the connection will be rejected.
1261 /* check if other audio channel is started. If yes, start */
1262 bdcpy(open.bd_addr, p_scb->peer_addr);
1263 open.chnl = p_scb->chnl;
1264 open.hndl = p_scb->hndl;
1265 open.status = BTA_AV_SUCCESS;
1266 open.starting = bta_av_chk_start(p_scb);
1268 if( NULL != (p = BTM_ReadRemoteFeatures(p_scb->peer_addr)))
1270 if(HCI_EDR_ACL_2MPS_SUPPORTED(p))
1271 open.edr |= BTA_AV_EDR_2MBPS;
1272 if(HCI_EDR_ACL_3MPS_SUPPORTED(p))
1273 open.edr |= BTA_AV_EDR_3MBPS;
1275 #if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
1276 bta_ar_avdt_conn(BTA_ID_AV, open.bd_addr);
1278 (*bta_av_cb.p_cback)(BTA_AV_OPEN_EVT, (tBTA_AV *) &open);
1281 bta_av_ssm_execute(p_scb, BTA_AV_AP_START_EVT, NULL);
1286 /*******************************************************************************
1288 ** Function bta_av_security_ind
1290 ** Description Handle an AVDTP security indication.
1294 *******************************************************************************/
1295 void bta_av_security_ind (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1297 tBTA_AV_PROTECT_REQ protect_req;
1299 p_scb->avdt_label = p_data->str_msg.msg.hdr.label;
1301 if (bta_av_cb.features & BTA_AV_FEAT_PROTECT)
1303 protect_req.chnl = p_scb->chnl;
1304 protect_req.hndl = p_scb->hndl;
1306 APPL_TRACE_EVENT1("sec ind handle: x%x", protect_req.hndl);
1308 protect_req.p_data = p_data->str_msg.msg.security_ind.p_data;
1309 protect_req.len = p_data->str_msg.msg.security_ind.len;
1311 (*bta_av_cb.p_cback)(BTA_AV_PROTECT_REQ_EVT, (tBTA_AV *) &protect_req);
1313 /* app doesn't support security indication; respond with failure */
1316 AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label, AVDT_ERR_NSC, NULL, 0);
1320 /*******************************************************************************
1322 ** Function bta_av_security_cfm
1324 ** Description Handle an AVDTP security confirm.
1328 *******************************************************************************/
1329 void bta_av_security_cfm (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1331 tBTA_AV_PROTECT_RSP protect_rsp;
1333 if (bta_av_cb.features & BTA_AV_FEAT_PROTECT)
1335 protect_rsp.chnl = p_scb->chnl;
1336 protect_rsp.hndl = p_scb->hndl;
1337 protect_rsp.p_data = p_data->str_msg.msg.security_cfm.p_data;
1338 protect_rsp.len = p_data->str_msg.msg.security_cfm.len;
1339 protect_rsp.err_code= p_data->str_msg.msg.hdr.err_code;
1341 (*bta_av_cb.p_cback)(BTA_AV_PROTECT_RSP_EVT, (tBTA_AV *) &protect_rsp);
1345 /*******************************************************************************
1347 ** Function bta_av_do_close
1349 ** Description Close stream.
1353 *******************************************************************************/
1354 void bta_av_do_close (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1358 /* stop stream if started */
1359 if (p_scb->co_started)
1361 bta_av_str_stopped(p_scb, NULL);
1363 bta_sys_stop_timer(&bta_av_cb.sig_tmr);
1366 p_scb->started = FALSE;
1368 /* drop the buffers queued in L2CAP */
1369 L2CA_FlushChannel (p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
1371 AVDT_CloseReq(p_scb->avdt_handle);
1372 /* just in case that the link is congested, link is flow controled by peer or
1373 * for whatever reason the the close request can not be sent in time.
1374 * when this timer expires, AVDT_DisconnectReq will be called to disconnect the link
1376 bta_sys_start_timer(&p_scb->timer,
1377 (UINT16)BTA_AV_API_CLOSE_EVT,
1378 BTA_AV_CLOSE_REQ_TIME_VAL);
1382 /*******************************************************************************
1384 ** Function bta_av_connect_req
1386 ** Description Connect AVDTP connection.
1390 *******************************************************************************/
1391 void bta_av_connect_req (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1395 utl_freebuf((void **) &p_scb->p_disc_db);
1397 if (p_scb->coll_mask & BTA_AV_COLL_INC_TMR)
1399 /* SNK initiated L2C connection while SRC was doing SDP. */
1400 /* Wait until timeout to check if SNK starts signalling. */
1401 APPL_TRACE_EVENT1("bta_av_connect_req: coll_mask = 0x%2X", p_scb->coll_mask);
1405 AVDT_ConnectReq(p_scb->peer_addr, p_scb->sec_mask, bta_av_dt_cback[p_scb->hdi]);
1408 /*******************************************************************************
1410 ** Function bta_av_sdp_failed
1412 ** Description Service discovery failed.
1416 *******************************************************************************/
1417 void bta_av_sdp_failed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1419 if (!p_scb->open_status)
1420 p_scb->open_status = BTA_AV_FAIL_SDP;
1422 utl_freebuf((void **) &p_scb->p_disc_db);
1423 bta_av_str_closed(p_scb, p_data);
1426 /*******************************************************************************
1428 ** Function bta_av_disc_results
1430 ** Description Handle the AVDTP discover results. Search through the
1431 ** results and find the first available stream, and get
1432 ** its capabilities.
1436 *******************************************************************************/
1437 void bta_av_disc_results (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1439 UINT8 num_snks = 0, i;
1441 /* store number of stream endpoints returned */
1442 p_scb->num_seps = p_data->str_msg.msg.discover_cfm.num_seps;
1444 for (i = 0; i < p_scb->num_seps; i++)
1446 /* steam not in use, is a sink, and is audio */
1447 if ((p_scb->sep_info[i].in_use == FALSE) &&
1448 (p_scb->sep_info[i].tsep == AVDT_TSEP_SNK) &&
1449 (p_scb->sep_info[i].media_type == p_scb->media_type))
1455 p_scb->p_cos->disc_res(p_scb->hndl, p_scb->num_seps, num_snks, p_scb->peer_addr);
1456 p_scb->num_disc_snks = num_snks;
1459 if (p_scb->num_seps > 0)
1461 /* initialize index into discovery results */
1462 p_scb->sep_info_idx = 0;
1464 /* get the capabilities of the first available stream */
1465 bta_av_next_getcap(p_scb, p_data);
1467 /* else we got discover response but with no streams; we're done */
1470 bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, p_data);
1474 /*******************************************************************************
1476 ** Function bta_av_disc_res_as_acp
1478 ** Description Handle the AVDTP discover results. Search through the
1479 ** results and find the first available stream, and get
1480 ** its capabilities.
1484 *******************************************************************************/
1485 void bta_av_disc_res_as_acp (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1487 UINT8 num_snks = 0, i;
1489 /* store number of stream endpoints returned */
1490 p_scb->num_seps = p_data->str_msg.msg.discover_cfm.num_seps;
1494 for (i = 0; i < p_scb->num_seps; i++)
1496 /* steam is a sink, and is audio */
1497 if ((p_scb->sep_info[i].tsep == AVDT_TSEP_SNK) &&
1498 (p_scb->sep_info[i].media_type == p_scb->media_type))
1500 p_scb->sep_info[i].in_use = FALSE;
1505 p_scb->p_cos->disc_res(p_scb->hndl, p_scb->num_seps, num_snks, p_scb->peer_addr);
1506 p_scb->num_disc_snks = num_snks;
1509 if (p_scb->num_seps > 0)
1511 /* initialize index into discovery results */
1512 p_scb->sep_info_idx = 0;
1514 /* get the capabilities of the first available stream */
1515 bta_av_next_getcap(p_scb, p_data);
1517 /* else we got discover response but with no streams; we're done */
1520 bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, p_data);
1524 /*******************************************************************************
1526 ** Function bta_av_save_caps
1528 ** Description report the SNK SEP capabilities to application
1532 *******************************************************************************/
1533 void bta_av_save_caps(tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1536 tAVDT_SEP_INFO *p_info = &p_scb->sep_info[p_scb->sep_info_idx];
1537 UINT8 old_wait = p_scb->wait;
1538 BOOLEAN getcap_done = FALSE;
1540 APPL_TRACE_DEBUG3("bta_av_save_caps num_seps:%d sep_info_idx:%d wait:x%x",
1541 p_scb->num_seps, p_scb->sep_info_idx, p_scb->wait);
1542 memcpy(&cfg, p_scb->p_cap, sizeof(tAVDT_CFG));
1543 /* let application know the capability of the SNK */
1544 p_scb->p_cos->getcfg(p_scb->hndl, cfg.codec_info[BTA_AV_CODEC_TYPE_IDX],
1545 cfg.codec_info, &p_scb->sep_info_idx, p_info->seid,
1546 &cfg.num_protect, cfg.protect_info);
1548 p_scb->sep_info_idx++;
1549 if(p_scb->num_seps > p_scb->sep_info_idx)
1551 /* Some devices have seps at the end of the discover list, which is not */
1552 /* matching media type(video not audio). */
1553 /* In this case, we are done with getcap without sending another */
1554 /* request to AVDT. */
1555 if (!bta_av_next_getcap(p_scb, p_data))
1563 /* we are done getting capabilities. restore the p_cb->sep_info_idx */
1564 p_scb->sep_info_idx = 0;
1565 p_scb->wait &= ~(BTA_AV_WAIT_ACP_CAPS_ON|BTA_AV_WAIT_ACP_CAPS_STARTED);
1566 if (old_wait & BTA_AV_WAIT_ACP_CAPS_STARTED)
1568 bta_av_start_ok (p_scb, NULL);
1573 /*******************************************************************************
1575 ** Function bta_av_set_use_rc
1577 ** Description set to use AVRC for this stream control block.
1581 *******************************************************************************/
1582 void bta_av_set_use_rc (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1586 p_scb->use_rc = TRUE;
1589 /*******************************************************************************
1591 ** Function bta_av_cco_close
1593 ** Description call close call-out function.
1597 *******************************************************************************/
1598 void bta_av_cco_close (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1603 mtu = bta_av_chk_mtu(p_scb, BTA_AV_MAX_A2DP_MTU);
1605 p_scb->p_cos->close(p_scb->hndl, p_scb->codec_type, mtu);
1608 /*******************************************************************************
1610 ** Function bta_av_open_failed
1612 ** Description Failed to open an AVDT stream
1616 *******************************************************************************/
1617 void bta_av_open_failed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1620 BOOLEAN is_av_opened = FALSE;
1621 tBTA_AV_SCB * p_opened_scb = NULL;
1625 APPL_TRACE_DEBUG0("bta_av_open_failed");
1626 p_scb->open_status = BTA_AV_FAIL_STREAM;
1627 bta_av_cco_close(p_scb, p_data);
1629 /* check whether there is already an opened audio or video connection with the same device */
1630 for (idx = 0; (idx < BTA_AV_NUM_STRS) && (is_av_opened == FALSE); idx++ )
1632 p_opened_scb = bta_av_cb.p_scb[idx];
1633 if (p_opened_scb && (p_opened_scb->state == BTA_AV_OPEN_SST) && (!bdcmp(p_opened_scb->peer_addr,p_scb->peer_addr )) )
1634 is_av_opened = TRUE;
1638 /* if there is already an active AV connnection with the same bd_addr,
1639 don't send disconnect req, just report the open event with BTA_AV_FAIL_GET_CAP status */
1640 if (is_av_opened == TRUE)
1642 bdcpy(open.bd_addr, p_scb->peer_addr);
1643 open.chnl = p_scb->chnl;
1644 open.hndl = p_scb->hndl;
1645 open.status = BTA_AV_FAIL_GET_CAP;
1646 open.starting = bta_av_chk_start(p_scb);
1648 /* set the state back to initial state */
1649 bta_av_set_scb_sst_init(p_scb);
1651 (*bta_av_cb.p_cback)(BTA_AV_OPEN_EVT, (tBTA_AV *) &open);
1656 AVDT_DisconnectReq(p_scb->peer_addr, bta_av_dt_cback[p_scb->hdi]);
1661 /*******************************************************************************
1663 ** Function bta_av_getcap_results
1665 ** Description Handle the AVDTP get capabilities results. Check the codec
1666 ** type and see if it matches ours. If it does not, get the
1667 ** capabilities of the next stream, if any.
1671 *******************************************************************************/
1672 void bta_av_getcap_results (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1676 tAVDT_SEP_INFO *p_info = &p_scb->sep_info[p_scb->sep_info_idx];
1678 memcpy(&cfg, &p_scb->cfg, sizeof(tAVDT_CFG));
1680 cfg.num_protect = p_scb->p_cap->num_protect;
1681 memcpy(cfg.codec_info, p_scb->p_cap->codec_info, AVDT_CODEC_SIZE);
1682 memcpy(cfg.protect_info, p_scb->p_cap->protect_info, AVDT_PROTECT_SIZE);
1683 media_type = p_scb->p_cap->codec_info[BTA_AV_MEDIA_TYPE_IDX] >> 4;
1685 APPL_TRACE_DEBUG1("num_codec %d", p_scb->p_cap->num_codec);
1686 APPL_TRACE_DEBUG2("media type x%x, x%x", media_type, p_scb->media_type);
1687 #if AVDT_MULTIPLEXING == TRUE
1688 APPL_TRACE_DEBUG2("mux x%x, x%x", cfg.mux_mask, p_scb->p_cap->mux_mask);
1691 /* if codec present and we get a codec configuration */
1692 if ((p_scb->p_cap->num_codec != 0) &&
1693 (media_type == p_scb->media_type) &&
1694 (p_scb->p_cos->getcfg(p_scb->hndl, p_scb->p_cap->codec_info[BTA_AV_CODEC_TYPE_IDX],
1695 cfg.codec_info, &p_scb->sep_info_idx, p_info->seid,
1696 &cfg.num_protect, cfg.protect_info) == 0))
1698 #if AVDT_MULTIPLEXING == TRUE
1699 cfg.mux_mask &= p_scb->p_cap->mux_mask;
1700 APPL_TRACE_DEBUG1("mux_mask used x%x", cfg.mux_mask);
1702 /* save copy of codec type and configuration */
1703 p_scb->codec_type = cfg.codec_info[BTA_AV_CODEC_TYPE_IDX];
1704 memcpy(&p_scb->cfg, &cfg, sizeof(tAVDT_CFG));
1705 bta_av_adjust_seps_idx(p_scb);
1706 /* use only the services peer supports */
1707 cfg.psc_mask &= p_scb->p_cap->psc_mask;
1708 p_scb->cur_psc_mask = cfg.psc_mask;
1710 /* open the stream */
1711 AVDT_OpenReq(p_scb->seps[p_scb->sep_idx].av_handle, p_scb->peer_addr,
1712 p_scb->sep_info[p_scb->sep_info_idx].seid, &cfg);
1714 if (!bta_av_is_rcfg_sst(p_scb))
1716 /* free capabilities buffer */
1717 utl_freebuf((void **) &p_scb->p_cap);
1722 /* try the next stream, if any */
1723 p_scb->sep_info_idx++;
1724 bta_av_next_getcap(p_scb, p_data);
1729 /*******************************************************************************
1731 ** Function bta_av_setconfig_rej
1733 ** Description Send AVDTP set config reject.
1737 *******************************************************************************/
1738 void bta_av_setconfig_rej (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1740 tBTA_AV_REJECT reject;
1742 APPL_TRACE_DEBUG0("bta_av_setconfig_rej");
1743 AVDT_ConfigRsp(p_data->str_msg.handle, p_data->str_msg.msg.hdr.label, AVDT_ERR_BAD_STATE, 0);
1744 bdcpy(reject.bd_addr, p_data->str_msg.bd_addr);
1745 reject.hndl = p_scb->hndl;
1746 (*bta_av_cb.p_cback)(BTA_AV_REJECT_EVT, (tBTA_AV *) &reject);
1749 /*******************************************************************************
1751 ** Function bta_av_discover_req
1753 ** Description Send an AVDTP discover request to the peer.
1757 *******************************************************************************/
1758 void bta_av_discover_req (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1762 /* send avdtp discover request */
1764 AVDT_DiscoverReq(p_scb->peer_addr, p_scb->sep_info, BTA_AV_NUM_SEPS, bta_av_dt_cback[p_scb->hdi]);
1767 /*******************************************************************************
1769 ** Function bta_av_conn_failed
1771 ** Description AVDTP connection failed.
1775 *******************************************************************************/
1776 void bta_av_conn_failed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1778 p_scb->open_status = BTA_AV_FAIL_STREAM;
1779 bta_av_str_closed(p_scb, p_data);
1782 /*******************************************************************************
1784 ** Function bta_av_do_start
1786 ** Description Start stream.
1790 *******************************************************************************/
1791 void bta_av_do_start (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1793 UINT8 policy = HCI_ENABLE_SNIFF_MODE;
1796 APPL_TRACE_DEBUG3("bta_av_do_start sco_occupied:%d, role:x%x, started:%d", bta_av_cb.sco_occupied, p_scb->role, p_scb->started);
1797 if (bta_av_cb.sco_occupied)
1799 bta_av_start_failed(p_scb, p_data);
1803 /* disallow role switch during streaming, only if we are the master role
1804 * i.e. allow role switch, if we are slave.
1805 * It would not hurt us, if the peer device wants us to be master */
1806 if ((BTM_GetRole (p_scb->peer_addr, &cur_role) == BTM_SUCCESS) &&
1807 (cur_role == BTM_ROLE_MASTER) )
1809 policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
1812 bta_sys_clear_policy(BTA_ID_AV, policy, p_scb->peer_addr);
1814 if ((p_scb->started == FALSE) && ((p_scb->role & BTA_AV_ROLE_START_INT) == 0))
1816 p_scb->role |= BTA_AV_ROLE_START_INT;
1817 bta_sys_busy(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
1819 AVDT_StartReq(&p_scb->avdt_handle, 1);
1821 else if (p_scb->started)
1823 p_scb->role |= BTA_AV_ROLE_START_INT;
1824 if ( p_scb->wait == 0 ) {
1825 if (p_scb->role & BTA_AV_ROLE_SUSPEND) {
1826 notify_start_failed(p_scb);
1828 bta_av_start_ok(p_scb, NULL);
1832 APPL_TRACE_DEBUG2("started %d role:x%x", p_scb->started, p_scb->role);
1835 /*******************************************************************************
1837 ** Function bta_av_str_stopped
1839 ** Description Stream stopped.
1843 *******************************************************************************/
1844 void bta_av_str_stopped (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1846 tBTA_AV_SUSPEND suspend_rsp;
1847 UINT8 start = p_scb->started;
1848 BOOLEAN sus_evt = TRUE;
1850 UINT8 policy = HCI_ENABLE_SNIFF_MODE;
1852 APPL_TRACE_ERROR2("bta_av_str_stopped:audio_open_cnt=%d, p_data %x",
1853 bta_av_cb.audio_open_cnt, p_data);
1855 bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
1856 if ((bta_av_cb.features & BTA_AV_FEAT_MASTER) == 0 || bta_av_cb.audio_open_cnt == 1)
1857 policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
1858 bta_sys_set_policy(BTA_ID_AV, policy, p_scb->peer_addr);
1860 if(p_scb->co_started)
1862 bta_av_stream_chg(p_scb, FALSE);
1863 p_scb->co_started = FALSE;
1865 p_scb->p_cos->stop(p_scb->hndl, p_scb->codec_type);
1866 L2CA_SetFlushTimeout(p_scb->peer_addr, L2CAP_DEFAULT_FLUSH_TO);
1869 /* if q_info.a2d is not empty, drop it now */
1870 if(BTA_AV_CHNL_AUDIO == p_scb->chnl)
1872 while((p_buf = (BT_HDR*)GKI_dequeue (&p_scb->q_info.a2d)) != NULL)
1875 /* drop the audio buffers queued in L2CAP */
1876 if(p_data && p_data->api_stop.flush)
1877 L2CA_FlushChannel (p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
1880 suspend_rsp.chnl = p_scb->chnl;
1881 suspend_rsp.hndl = p_scb->hndl;
1883 if (p_data && p_data->api_stop.suspend)
1885 APPL_TRACE_DEBUG2("suspending: %d, sup:%d", start, p_scb->suspend_sup);
1886 if ((start) && (p_scb->suspend_sup))
1889 p_scb->l2c_bufs = 0;
1890 AVDT_SuspendReq(&p_scb->avdt_handle, 1);
1893 /* send SUSPEND_EVT event only if not in reconfiguring state and sus_evt is TRUE*/
1894 if ((sus_evt)&&(p_scb->state != BTA_AV_RCFG_SST))
1896 suspend_rsp.status = BTA_AV_SUCCESS;
1897 suspend_rsp.initiator = TRUE;
1898 (*bta_av_cb.p_cback)(BTA_AV_SUSPEND_EVT, (tBTA_AV *) &suspend_rsp);
1903 suspend_rsp.status = BTA_AV_SUCCESS;
1904 suspend_rsp.initiator = TRUE;
1905 APPL_TRACE_EVENT1("bta_av_str_stopped status %d", suspend_rsp.status);
1907 /* send STOP_EVT event only if not in reconfiguring state */
1908 if (p_scb->state != BTA_AV_RCFG_SST)
1910 (*bta_av_cb.p_cback)(BTA_AV_STOP_EVT, (tBTA_AV *) &suspend_rsp);
1915 /*******************************************************************************
1917 ** Function bta_av_reconfig
1919 ** Description process the reconfigure request.
1920 ** save the parameter in control block and
1921 ** suspend, reconfigure or close the stream
1925 *******************************************************************************/
1926 void bta_av_reconfig (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
1929 tBTA_AV_API_STOP stop;
1930 tBTA_AV_RECONFIG evt;
1931 tBTA_AV_API_RCFG *p_rcfg = &p_data->api_reconfig;
1933 APPL_TRACE_DEBUG4("bta_av_reconfig r:%d, s:%d idx: %d (o:%d)",
1934 p_scb->recfg_sup, p_scb->suspend_sup,
1935 p_scb->rcfg_idx, p_scb->sep_info_idx);
1937 p_scb->num_recfg = 0;
1938 /* store the new configuration in control block */
1939 if (p_scb->p_cap == NULL)
1941 p_scb->p_cap = (tAVDT_CFG *) GKI_getbuf(sizeof(tAVDT_CFG));
1943 if((p_cfg = p_scb->p_cap) == NULL)
1945 /* report failure */
1946 evt.status = BTA_AV_FAIL_RESOURCES;
1947 evt.chnl = p_scb->chnl;
1948 evt.hndl = p_scb->hndl;
1949 (*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, (tBTA_AV *)&evt);
1951 /* this event is not possible in this state.
1952 * use it to bring the SSM back to open state */
1953 bta_av_ssm_execute(p_scb, BTA_AV_SDP_DISC_OK_EVT, NULL);
1957 /*if(bta_av_cb.features & BTA_AV_FEAT_RCCT)*/
1958 bta_sys_stop_timer(&p_scb->timer);
1960 memcpy(p_cfg, &p_scb->cfg, sizeof(tAVDT_CFG));
1961 p_cfg->num_protect = p_rcfg->num_protect;
1962 memcpy(p_cfg->codec_info, p_rcfg->codec_info, AVDT_CODEC_SIZE);
1963 memcpy(p_cfg->protect_info, p_rcfg->p_protect_info, p_rcfg->num_protect);
1964 p_scb->rcfg_idx = p_rcfg->sep_info_idx;
1965 p_scb->p_cap->psc_mask = p_scb->cur_psc_mask;
1967 /* if the requested index differs from the current one, we can only close/open */
1968 if ((p_scb->rcfg_idx == p_scb->sep_info_idx) &&
1969 (p_rcfg->suspend)&& (p_scb->recfg_sup) && (p_scb->suspend_sup))
1974 stop.suspend = TRUE;
1975 bta_av_str_stopped(p_scb, (tBTA_AV_DATA *)&stop);
1979 APPL_TRACE_DEBUG0("Reconfig");
1980 AVDT_ReconfigReq(p_scb->avdt_handle, p_scb->p_cap);
1981 p_scb->p_cap->psc_mask = p_scb->cur_psc_mask;
1986 /* close the stream */
1987 APPL_TRACE_DEBUG1("close/open num_protect: %d", p_cfg->num_protect);
1989 bta_av_str_stopped(p_scb, NULL);
1990 p_scb->started = FALSE;
1992 /* drop the buffers queued in L2CAP */
1993 L2CA_FlushChannel (p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
1995 AVDT_CloseReq(p_scb->avdt_handle);
2000 /*******************************************************************************
2002 ** Function bta_av_data_path
2004 ** Description Handle stream data path.
2008 *******************************************************************************/
2009 void bta_av_data_path (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2014 BOOLEAN new_buf = FALSE;
2015 UINT8 m_pt = 0x60 | p_scb->codec_type;
2016 tAVDT_DATA_OPT_MASK opt;
2022 APPL_TRACE_ERROR1("q: %d", p_scb->l2c_bufs);
2024 //Always get the current number of bufs que'd up
2025 p_scb->l2c_bufs = (UINT8)L2CA_FlushChannel (p_scb->l2c_cid, L2CAP_FLUSH_CHANS_GET);
2027 p_buf = (BT_HDR *)GKI_dequeue (&p_scb->q_info.a2d);
2030 /* use q_info.a2d data, read the timestamp */
2031 timestamp = *(UINT32 *)(p_buf + 1);
2036 /* q_info.a2d empty, call co_data, dup data to other channels */
2037 p_buf = (BT_HDR *)p_scb->p_cos->data(p_scb->codec_type, &data_len,
2042 /* use the offset area for the time stamp */
2043 *(UINT32 *)(p_buf + 1) = timestamp;
2045 /* dup the data to other channels */
2046 bta_av_dup_audio_buf(p_scb, p_buf);
2052 if(p_scb->l2c_bufs < (BTA_AV_QUEUE_DATA_CHK_NUM))
2054 /* there's a buffer, just queue it to L2CAP */
2055 /* There's no need to increment it here, it is always read from L2CAP see above */
2056 /* p_scb->l2c_bufs++; */
2058 APPL_TRACE_ERROR1("qw: %d", p_scb->l2c_bufs);
2061 /* opt is a bit mask, it could have several options set */
2062 opt = AVDT_DATA_OPT_NONE;
2063 if (p_scb->no_rtp_hdr)
2065 opt |= AVDT_DATA_OPT_NO_RTP;
2068 AVDT_WriteReqOpt(p_scb->avdt_handle, p_buf, timestamp, m_pt, opt);
2073 /* there's a buffer, but L2CAP does not seem to be moving data */
2076 /* just got this buffer from co_data,
2077 * put it in queue */
2078 GKI_enqueue(&p_scb->q_info.a2d, p_buf);
2082 /* just dequeue it from the q_info.a2d */
2083 if(p_scb->q_info.a2d.count < 3)
2085 /* put it back to the queue */
2086 GKI_enqueue_head (&p_scb->q_info.a2d, p_buf);
2090 /* too many buffers in q_info.a2d, drop it. */
2091 bta_av_co_audio_drop(p_scb->hndl);
2100 /*******************************************************************************
2102 ** Function bta_av_start_ok
2104 ** Description Stream started.
2108 *******************************************************************************/
2109 void bta_av_start_ok (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2111 tBTA_AV_START start;
2112 tBTA_AV_API_STOP stop;
2113 BOOLEAN initiator = FALSE;
2114 BOOLEAN suspend = FALSE;
2116 UINT8 new_role = p_scb->role;
2118 UINT8 policy = HCI_ENABLE_SNIFF_MODE;
2121 APPL_TRACE_DEBUG2("bta_av_start_ok wait:x%x, role:x%x", p_scb->wait, p_scb->role);
2123 p_scb->started = TRUE;
2124 if (p_scb->sco_suspend)
2126 p_scb->sco_suspend = FALSE;
2129 if (new_role & BTA_AV_ROLE_START_INT)
2132 if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_FAILED)
2134 /* role switch has failed */
2135 p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_FAILED;
2136 p_data = (tBTA_AV_DATA *)&hdr;
2137 hdr.offset = BTA_AV_RS_FAIL;
2139 APPL_TRACE_DEBUG1("wait:x%x", p_scb->wait);
2141 if (p_data && (p_data->hdr.offset != BTA_AV_RS_NONE))
2143 p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
2144 if (p_data->hdr.offset == BTA_AV_RS_FAIL)
2146 bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
2147 start.chnl = p_scb->chnl;
2148 start.status = BTA_AV_FAIL_ROLE;
2149 start.hndl = p_scb->hndl;
2150 start.initiator = initiator;
2151 (*bta_av_cb.p_cback)(BTA_AV_START_EVT, (tBTA_AV *) &start);
2156 if (!bta_av_link_role_ok(p_scb, A2D_SET_ONE_BIT))
2157 p_scb->q_tag = BTA_AV_Q_TAG_START;
2160 /* The wait flag may be set here while we are already master on the link */
2161 /* this could happen if a role switch complete event occurred during reconfig */
2162 /* if we are now master on the link, there is no need to wait for the role switch, */
2163 /* complete anymore so we can clear the wait for role switch flag */
2164 p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
2167 if (p_scb->wait & (BTA_AV_WAIT_ROLE_SW_RES_OPEN|BTA_AV_WAIT_ROLE_SW_RES_START))
2169 p_scb->wait |= BTA_AV_WAIT_ROLE_SW_STARTED;
2170 p_scb->q_tag = BTA_AV_Q_TAG_START;
2173 if (p_scb->wait & BTA_AV_WAIT_ACP_CAPS_ON)
2175 p_scb->wait |= BTA_AV_WAIT_ACP_CAPS_STARTED;
2180 APPL_TRACE_DEBUG2("wait:x%x q_tag:%d- not started", p_scb->wait, p_scb->q_tag);
2184 /* tell role manager to check M/S role */
2185 bta_sys_conn_open(BTA_ID_AV, p_scb->app_id, p_scb->peer_addr);
2187 bta_sys_busy(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
2189 if(p_scb->media_type == AVDT_MEDIA_AUDIO)
2191 /* in normal logic, conns should be bta_av_cb.audio_count - 1,
2192 * However, bta_av_stream_chg is not called to increase bta_av_cb.audio_count yet.
2193 * If the code were to be re-arranged for some reasons, this number may need to be changed
2195 p_scb->co_started = bta_av_cb.audio_open_cnt;
2196 flush_to = p_bta_av_cfg->p_audio_flush_to[p_scb->co_started - 1];
2200 flush_to = p_bta_av_cfg->video_flush_to;
2202 L2CA_SetFlushTimeout(p_scb->peer_addr, flush_to );
2204 /* clear the congestion flag */
2205 p_scb->cong = FALSE;
2207 if (new_role & BTA_AV_ROLE_START_INT)
2209 new_role &= ~BTA_AV_ROLE_START_INT;
2211 else if ((new_role & BTA_AV_ROLE_AD_ACP) && (new_role & BTA_AV_ROLE_SUSPEND_OPT))
2218 p_scb->q_tag = BTA_AV_Q_TAG_STREAM;
2219 bta_av_stream_chg(p_scb, TRUE);
2223 /* If sink starts stream, disable sniff mode here */
2226 /* If souce is the master role, disable role switch during streaming.
2227 * Otherwise allow role switch, if source is slave.
2228 * Because it would not hurt source, if the peer device wants source to be master */
2229 if ((BTM_GetRole (p_scb->peer_addr, &cur_role) == BTM_SUCCESS) &&
2230 (cur_role == BTM_ROLE_MASTER) )
2232 policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
2235 bta_sys_clear_policy(BTA_ID_AV, policy, p_scb->peer_addr);
2238 p_scb->role = new_role;
2239 p_scb->role &= ~BTA_AV_ROLE_AD_ACP;
2240 p_scb->role &= ~BTA_AV_ROLE_SUSPEND_OPT;
2242 p_scb->no_rtp_hdr = FALSE;
2243 p_scb->p_cos->start(p_scb->hndl, p_scb->codec_type, p_scb->cfg.codec_info, &p_scb->no_rtp_hdr);
2244 p_scb->co_started = TRUE;
2246 APPL_TRACE_DEBUG3("bta_av_start_ok suspending: %d, role:x%x, init %d",
2247 suspend, p_scb->role, initiator);
2249 start.suspending = suspend;
2250 start.initiator = initiator;
2251 start.chnl = p_scb->chnl;
2252 start.status = BTA_AV_SUCCESS;
2253 start.hndl = p_scb->hndl;
2254 (*bta_av_cb.p_cback)(BTA_AV_START_EVT, (tBTA_AV *) &start);
2258 p_scb->role |= BTA_AV_ROLE_SUSPEND;
2259 p_scb->cong = TRUE; /* do not allow the media data to go through */
2260 /* do not duplicate the media packets to this channel */
2261 p_scb->p_cos->stop(p_scb->hndl, p_scb->codec_type);
2262 p_scb->co_started = FALSE;
2264 stop.suspend = TRUE;
2265 bta_av_ssm_execute(p_scb, BTA_AV_AP_STOP_EVT, (tBTA_AV_DATA *)&stop);
2270 /*******************************************************************************
2272 ** Function bta_av_start_failed
2274 ** Description Stream start failed.
2278 *******************************************************************************/
2279 void bta_av_start_failed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2283 if(p_scb->started == FALSE && p_scb->co_started == FALSE)
2285 bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
2286 notify_start_failed(p_scb);
2289 bta_sys_set_policy(BTA_ID_AV, (HCI_ENABLE_SNIFF_MODE|HCI_ENABLE_MASTER_SLAVE_SWITCH), p_scb->peer_addr);
2290 p_scb->sco_suspend = FALSE;
2293 /*******************************************************************************
2295 ** Function bta_av_str_closed
2297 ** Description Stream closed.
2301 *******************************************************************************/
2302 void bta_av_str_closed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2307 UINT8 policy = HCI_ENABLE_SNIFF_MODE;
2309 if ((bta_av_cb.features & BTA_AV_FEAT_MASTER) == 0 || bta_av_cb.audio_open_cnt == 1)
2310 policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
2311 bta_sys_set_policy(BTA_ID_AV, policy, p_scb->peer_addr);
2312 if (bta_av_cb.audio_open_cnt <= 1)
2314 /* last connection - restore the allow switch flag */
2315 L2CA_SetDesireRole(L2CAP_ROLE_ALLOW_SWITCH);
2318 if (p_scb->open_status)
2320 /* must be failure when opening the stream */
2321 bdcpy(data.open.bd_addr, p_scb->peer_addr);
2322 data.open.status = p_scb->open_status;
2323 data.open.chnl = p_scb->chnl;
2324 data.open.hndl = p_scb->hndl;
2325 event = BTA_AV_OPEN_EVT;
2326 p_scb->open_status = BTA_AV_SUCCESS;
2328 bta_sys_conn_close(BTA_ID_AV, p_scb->app_id, p_scb->peer_addr);
2329 bta_av_cleanup(p_scb, p_data);
2330 (*bta_av_cb.p_cback)(event, &data);
2334 /* do stop if we were started */
2335 if (p_scb->co_started)
2337 bta_av_str_stopped(p_scb, NULL);
2340 /* Update common mtu shared by remaining connectons */
2341 mtu = bta_av_chk_mtu(p_scb, BTA_AV_MAX_A2DP_MTU);
2344 p_scb->p_cos->close(p_scb->hndl, p_scb->codec_type, mtu);
2345 data.close.chnl = p_scb->chnl;
2346 data.close.hndl = p_scb->hndl;
2347 event = BTA_AV_CLOSE_EVT;
2349 bta_sys_conn_close(BTA_ID_AV, p_scb->app_id, p_scb->peer_addr);
2350 bta_av_cleanup(p_scb, p_data);
2351 (*bta_av_cb.p_cback)(event, &data);
2356 /*******************************************************************************
2358 ** Function bta_av_clr_cong
2360 ** Description Clear stream congestion flag.
2364 *******************************************************************************/
2365 void bta_av_clr_cong (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2369 if(p_scb->co_started)
2370 p_scb->cong = FALSE;
2373 /*******************************************************************************
2375 ** Function bta_av_suspend_cfm
2377 ** Description process the suspend response
2381 *******************************************************************************/
2382 void bta_av_suspend_cfm (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2384 tBTA_AV_SUSPEND suspend_rsp;
2385 UINT8 err_code = p_data->str_msg.msg.hdr.err_code;
2386 UINT8 policy = HCI_ENABLE_SNIFF_MODE;
2388 APPL_TRACE_DEBUG2 ("bta_av_suspend_cfm:audio_open_cnt = %d, err_code = %d",
2389 bta_av_cb.audio_open_cnt, err_code);
2391 if (p_scb->started == FALSE)
2393 /* handle the condition where there is a collision of SUSPEND req from either side
2394 ** Second SUSPEND req could be rejected. Do not treat this as a failure
2396 APPL_TRACE_WARNING1("bta_av_suspend_cfm: already suspended, ignore, err_code %d",
2401 suspend_rsp.status = BTA_AV_SUCCESS;
2402 if (err_code && (err_code != AVDT_ERR_BAD_STATE))
2404 /* Disable suspend feature only with explicit rejection(not with timeout) */
2405 if (err_code != AVDT_ERR_TIMEOUT)
2407 p_scb->suspend_sup = FALSE;
2409 suspend_rsp.status = BTA_AV_FAIL;
2411 APPL_TRACE_ERROR0 ("bta_av_suspend_cfm: suspend failed, closing connection");
2413 /* SUSPEND failed. Close connection. */
2414 bta_av_ssm_execute(p_scb, BTA_AV_API_CLOSE_EVT, NULL);
2418 /* only set started to FALSE when suspend is successful */
2419 p_scb->started = FALSE;
2422 if (p_scb->role & BTA_AV_ROLE_SUSPEND)
2424 p_scb->role &= ~BTA_AV_ROLE_SUSPEND;
2425 p_scb->cong = FALSE;
2428 bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->peer_addr);
2429 if ((bta_av_cb.features & BTA_AV_FEAT_MASTER) == 0 || bta_av_cb.audio_open_cnt == 1)
2430 policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
2431 bta_sys_set_policy(BTA_ID_AV, policy, p_scb->peer_addr);
2433 /* in case that we received suspend_ind, we may need to call co_stop here */
2434 if(p_scb->co_started)
2436 bta_av_stream_chg(p_scb, FALSE);
2439 p_scb->co_started = FALSE;
2440 p_scb->p_cos->stop(p_scb->hndl, p_scb->codec_type);
2442 L2CA_SetFlushTimeout(p_scb->peer_addr, L2CAP_DEFAULT_FLUSH_TO);
2446 suspend_rsp.chnl = p_scb->chnl;
2447 suspend_rsp.hndl = p_scb->hndl;
2448 suspend_rsp.initiator = p_data->str_msg.initiator;
2449 (*bta_av_cb.p_cback)(BTA_AV_SUSPEND_EVT, (tBTA_AV *) &suspend_rsp);
2453 /*******************************************************************************
2455 ** Function bta_av_rcfg_str_ok
2457 ** Description report reconfigure successful
2461 *******************************************************************************/
2462 void bta_av_rcfg_str_ok (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2464 tBTA_AV_RECONFIG evt;
2467 p_scb->l2c_cid = AVDT_GetL2CapChannel(p_scb->avdt_handle);
2468 APPL_TRACE_DEBUG1("bta_av_rcfg_str_ok: l2c_cid: %d", p_scb->l2c_cid);
2471 bta_av_st_rc_timer(p_scb, NULL);
2472 utl_freebuf((void **)&p_scb->p_cap);
2474 /* No need to keep the role bits once reconfig is done. */
2475 p_scb->role &= ~BTA_AV_ROLE_AD_ACP;
2476 p_scb->role &= ~BTA_AV_ROLE_SUSPEND_OPT;
2477 p_scb->role &= ~BTA_AV_ROLE_START_INT;
2480 /* reconfigure success */
2481 evt.status = BTA_AV_SUCCESS;
2482 evt.chnl = p_scb->chnl;
2483 evt.hndl = p_scb->hndl;
2484 (*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, (tBTA_AV *)&evt);
2488 /*******************************************************************************
2490 ** Function bta_av_rcfg_failed
2492 ** Description process reconfigure failed
2496 *******************************************************************************/
2497 void bta_av_rcfg_failed (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2499 tBTA_AV_RECONFIG evt;
2501 APPL_TRACE_DEBUG2("bta_av_rcfg_failed num_recfg: %d, conn_lcb:0x%x",
2502 p_scb->num_recfg, bta_av_cb.conn_lcb);
2503 if(p_scb->num_recfg > BTA_AV_RECONFIG_RETRY)
2505 bta_av_cco_close(p_scb, p_data);
2506 /* report failure */
2507 evt.status = BTA_AV_FAIL_STREAM;
2508 evt.chnl = p_scb->chnl;
2509 evt.hndl = p_scb->hndl;
2510 (*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, (tBTA_AV *)&evt);
2511 /* go to closing state */
2512 bta_av_ssm_execute(p_scb, BTA_AV_API_CLOSE_EVT, NULL);
2516 /* open failed. try again */
2518 if(bta_av_cb.conn_lcb)
2520 AVDT_DisconnectReq(p_scb->peer_addr, bta_av_dt_cback[p_scb->hdi]);
2524 bta_av_connect_req(p_scb, NULL);
2529 /*******************************************************************************
2531 ** Function bta_av_rcfg_connect
2533 ** Description stream closed. reconnect the stream
2537 *******************************************************************************/
2538 void bta_av_rcfg_connect (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2542 p_scb->cong = FALSE;
2544 APPL_TRACE_DEBUG1("bta_av_rcfg_connect num_recfg: %d", p_scb->num_recfg);
2545 if(p_scb->num_recfg > BTA_AV_RECONFIG_RETRY)
2547 /* let bta_av_rcfg_failed report fail */
2548 bta_av_rcfg_failed(p_scb, NULL);
2551 AVDT_ConnectReq(p_scb->peer_addr, p_scb->sec_mask, bta_av_dt_cback[p_scb->hdi]);
2554 /*******************************************************************************
2556 ** Function bta_av_rcfg_discntd
2558 ** Description AVDT disconnected. reconnect the stream
2562 *******************************************************************************/
2563 void bta_av_rcfg_discntd (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2565 tBTA_AV_RECONFIG evt;
2568 APPL_TRACE_DEBUG1("bta_av_rcfg_discntd num_recfg: %d", p_scb->num_recfg);
2570 if(p_scb->num_recfg > BTA_AV_RECONFIG_RETRY)
2572 /* report failure */
2573 evt.status = BTA_AV_FAIL_STREAM;
2574 evt.chnl = p_scb->chnl;
2575 evt.hndl = p_scb->hndl;
2576 (*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, (tBTA_AV *)&evt);
2577 /* report close event & go to init state */
2578 bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, NULL);
2581 AVDT_ConnectReq(p_scb->peer_addr, p_scb->sec_mask, bta_av_dt_cback[p_scb->hdi]);
2584 /*******************************************************************************
2586 ** Function bta_av_suspend_cont
2588 ** Description received the suspend response.
2589 ** continue to reconfigure the stream
2593 *******************************************************************************/
2594 void bta_av_suspend_cont (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2596 UINT8 err_code = p_data->str_msg.msg.hdr.err_code;
2597 tBTA_AV_RECONFIG evt;
2599 p_scb->started = FALSE;
2600 p_scb->cong = FALSE;
2603 if (AVDT_ERR_CONNECT == err_code)
2605 /* report failure */
2606 evt.status = BTA_AV_FAIL;
2607 (*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, (tBTA_AV *)&evt);
2608 bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, NULL);
2612 APPL_TRACE_ERROR0("suspend rejected, try close");
2613 /* Disable suspend feature only with explicit rejection(not with timeout) */
2614 if (err_code != AVDT_ERR_TIMEOUT)
2616 p_scb->suspend_sup = FALSE;
2618 /* drop the buffers queued in L2CAP */
2619 L2CA_FlushChannel (p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
2621 AVDT_CloseReq(p_scb->avdt_handle);
2626 APPL_TRACE_DEBUG0("bta_av_suspend_cont calling AVDT_ReconfigReq");
2627 /* reconfig the stream */
2629 AVDT_ReconfigReq(p_scb->avdt_handle, p_scb->p_cap);
2630 p_scb->p_cap->psc_mask = p_scb->cur_psc_mask;
2634 /*******************************************************************************
2636 ** Function bta_av_rcfg_cfm
2638 ** Description if reconfigure is successful, report the event
2639 ** otherwise, close the stream.
2643 *******************************************************************************/
2644 void bta_av_rcfg_cfm (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2646 UINT8 err_code = p_data->str_msg.msg.hdr.err_code;
2649 APPL_TRACE_DEBUG0("bta_av_rcfg_cfm");
2653 APPL_TRACE_ERROR0("reconfig rejected, try close");
2654 /* Disable reconfiguration feature only with explicit rejection(not with timeout) */
2655 if (err_code != AVDT_ERR_TIMEOUT)
2657 p_scb->recfg_sup = FALSE;
2659 /* started flag is FALSE when reconfigure command is sent */
2660 /* drop the buffers queued in L2CAP */
2661 L2CA_FlushChannel (p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
2662 AVDT_CloseReq(p_scb->avdt_handle);
2666 /* update the codec info after rcfg cfm */
2667 memcpy(p_scb->cfg.codec_info,p_data->str_msg.msg.reconfig_cfm.p_cfg->codec_info,AVDT_CODEC_SIZE);
2668 /* take the SSM back to OPEN state */
2669 bta_av_ssm_execute(p_scb, BTA_AV_STR_OPEN_OK_EVT, NULL);
2673 /*******************************************************************************
2675 ** Function bta_av_rcfg_open
2677 ** Description AVDT is connected. open the stream with the new configuration
2681 *******************************************************************************/
2682 void bta_av_rcfg_open (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2686 APPL_TRACE_DEBUG1("bta_av_rcfg_open, num_disc_snks = %d", p_scb->num_disc_snks);
2688 if (p_scb->num_disc_snks == 0)
2690 /* Need to update call-out module so that it will be ready for discover */
2691 p_scb->p_cos->stop(p_scb->hndl, p_scb->codec_type);
2693 /* send avdtp discover request */
2694 AVDT_DiscoverReq(p_scb->peer_addr, p_scb->sep_info, BTA_AV_NUM_SEPS, bta_av_dt_cback[p_scb->hdi]);
2698 p_scb->codec_type = p_scb->p_cap->codec_info[BTA_AV_CODEC_TYPE_IDX];
2699 memcpy(p_scb->cfg.codec_info, p_scb->p_cap->codec_info, AVDT_CODEC_SIZE);
2700 /* we may choose to use a different SEP at reconfig.
2701 * adjust the sep_idx now */
2702 bta_av_adjust_seps_idx(p_scb);
2704 /* open the stream with the new config */
2705 p_scb->sep_info_idx = p_scb->rcfg_idx;
2706 AVDT_OpenReq(p_scb->avdt_handle, p_scb->peer_addr,
2707 p_scb->sep_info[p_scb->sep_info_idx].seid, p_scb->p_cap);
2712 /*******************************************************************************
2714 ** Function bta_av_security_rej
2716 ** Description Send an AVDTP security reject.
2720 *******************************************************************************/
2721 void bta_av_security_rej (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2725 AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label, AVDT_ERR_BAD_STATE,
2729 /*******************************************************************************
2731 ** Function bta_av_chk_2nd_start
2733 ** Description check if this is 2nd stream and if it needs to be started.
2734 ** This function needs to be kept very similar to bta_av_chk_start
2738 *******************************************************************************/
2739 void bta_av_chk_2nd_start (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2741 tBTA_AV_SCB *p_scbi;
2743 BOOLEAN new_started = FALSE;
2746 if ((p_scb->chnl == BTA_AV_CHNL_AUDIO) && (bta_av_cb.audio_open_cnt >= 2))
2748 /* more than one audio channel is connected */
2749 if (!(p_scb->role & BTA_AV_ROLE_SUSPEND_OPT))
2751 /* this channel does not need to be reconfigured.
2752 * if there is other channel streaming, start the stream now */
2753 for(i=0; i<BTA_AV_NUM_STRS; i++)
2755 p_scbi = bta_av_cb.p_scb[i];
2756 if(p_scbi && p_scbi->chnl == BTA_AV_CHNL_AUDIO && p_scbi->co_started)
2760 /* start the new stream */
2762 bta_av_ssm_execute(p_scb, BTA_AV_AP_START_EVT, NULL);
2764 /* may need to update the flush timeout of this already started stream */
2765 if (p_scbi->co_started != bta_av_cb.audio_open_cnt)
2767 p_scbi->co_started = bta_av_cb.audio_open_cnt;
2768 L2CA_SetFlushTimeout(p_scbi->peer_addr, p_bta_av_cfg->p_audio_flush_to[p_scbi->co_started - 1] );
2776 /*******************************************************************************
2778 ** Function bta_av_open_rc
2780 ** Description Send a message to main SM to open RC channel.
2784 *******************************************************************************/
2785 void bta_av_open_rc (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2787 tBTA_AV_START start;
2789 APPL_TRACE_DEBUG3("bta_av_open_rc use_rc: %d, wait: x%x role:x%x", p_scb->use_rc, p_scb->wait, p_scb->role);
2790 if ((p_scb->wait & BTA_AV_WAIT_ROLE_SW_BITS) && (p_scb->q_tag == BTA_AV_Q_TAG_START))
2792 /* waiting for role switch for some reason & the timer expires */
2793 if (!bta_av_link_role_ok(p_scb, A2D_SET_ONE_BIT))
2795 APPL_TRACE_ERROR0 ("failed to start streaming for role management reasons!!");
2796 bta_sys_stop_timer(&p_scb->timer);
2797 start.chnl = p_scb->chnl;
2798 start.status = BTA_AV_FAIL_ROLE;
2799 start.initiator = TRUE;
2800 start.hndl = p_scb->hndl;
2801 p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
2802 bta_av_cb.rs_idx = 0;
2803 (*bta_av_cb.p_cback)(BTA_AV_START_EVT, (tBTA_AV *) &start);
2807 /* role switch is done. continue to start streaming */
2808 bta_av_cb.rs_idx = 0;
2809 p_data->hdr.offset = BTA_AV_RS_OK;
2810 bta_av_start_ok (p_scb, p_data);
2815 if(p_scb->use_rc == TRUE || (p_scb->role & BTA_AV_ROLE_AD_ACP) )
2819 /* AVRC discover db is in use */
2820 if(p_scb->rc_handle == BTA_AV_RC_HANDLE_NONE)
2822 /* AVRC channel is not connected. delay a little bit */
2823 if ((p_scb->wait & BTA_AV_WAIT_ROLE_SW_BITS) == 0)
2824 bta_sys_start_timer(&p_scb->timer, BTA_AV_AVRC_TIMER_EVT, BTA_AV_RC_DISC_TIME_VAL);
2826 p_scb->wait |= BTA_AV_WAIT_CHECK_RC;
2831 /* use main SM for AVRC SDP activities */
2832 bta_av_rc_disc((UINT8)(p_scb->hdi + 1));
2837 if(BTA_AV_RC_HANDLE_NONE != p_scb->rc_handle)
2839 /* the open API said that this handle does not want a RC connection.
2840 * disconnect it now */
2841 AVRC_Close(p_scb->rc_handle);
2846 /*******************************************************************************
2848 ** Function bta_av_open_at_inc
2850 ** Description This function is called if API open is called by application
2851 ** while state-machine is at incoming state.
2855 *******************************************************************************/
2856 void bta_av_open_at_inc (tBTA_AV_SCB *p_scb, tBTA_AV_DATA *p_data)
2858 tBTA_AV_API_OPEN *p_buf;
2860 memcpy (&(p_scb->open_api), &(p_data->api_open), sizeof(tBTA_AV_API_OPEN));
2862 if (p_scb->coll_mask & BTA_AV_COLL_INC_TMR)
2864 p_scb->coll_mask |= BTA_AV_COLL_API_CALLED;
2866 /* API open will be handled at timeout if SNK did not start signalling. */
2867 /* API open will be ignored if SNK starts signalling. */
2871 /* SNK did not start signalling, API was called N seconds timeout. */
2872 /* We need to switch to INIT state and start opening connection. */
2873 p_scb->coll_mask = 0;
2874 bta_av_set_scb_sst_init (p_scb);
2876 if ((p_buf = (tBTA_AV_API_OPEN *) GKI_getbuf(sizeof(tBTA_AV_API_OPEN))) != NULL)
2878 memcpy(p_buf, &(p_scb->open_api), sizeof(tBTA_AV_API_OPEN));
2879 bta_sys_sendmsg(p_buf);
2884 #endif /* BTA_AV_INCLUDED */