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