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