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("%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("%s: peer %s role:0x%x bta_channel:%d bta_handle:0x%x", __func__,
297 p_scb->PeerAddress().ToString().c_str(), p_scb->role, p_scb->chnl,
298 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("%s peer %s", __func__, p_scb->PeerAddress().ToString().c_str());
906
907 /* free any buffers */
908 p_scb->sdp_discovery_started = false;
909 p_scb->SetAvdtpVersion(0);
910
911 /* initialize some control block variables */
912 p_scb->open_status = BTA_AV_SUCCESS;
913
914 /* if de-registering shut everything down */
915 msg.hdr.layer_specific = p_scb->hndl;
916 p_scb->started = false;
917 p_scb->offload_started = false;
918 p_scb->use_rtp_header_marker_bit = false;
919 p_scb->cong = false;
920 p_scb->role = role;
921 p_scb->cur_psc_mask = 0;
922 p_scb->wait = 0;
923 p_scb->num_disc_snks = 0;
924 p_scb->coll_mask = 0;
925 alarm_cancel(p_scb->avrc_ct_timer);
926
927 /* TODO(eisenbach): RE-IMPLEMENT USING VSC OR HAL EXTENSION
928 vendor_get_interface()->send_command(
929 (vendor_opcode_t)BT_VND_OP_A2DP_OFFLOAD_STOP, (void*)&p_scb->l2c_cid);
930 if (p_scb->offload_start_pending) {
931 tBTA_AV_STATUS status = BTA_AV_FAIL_STREAM;
932 tBTA_AV bta_av_data;
933 bta_av_data.status = status;
934 (*bta_av_cb.p_cback)(BTA_AV_OFFLOAD_START_RSP_EVT, &bta_av_data);
935 }
936 */
937
938 p_scb->offload_start_pending = false;
939
940 if (p_scb->deregistering) {
941 /* remove stream */
942 for (int i = 0; i < BTAV_A2DP_CODEC_INDEX_MAX; i++) {
943 if (p_scb->seps[i].av_handle) AVDT_RemoveStream(p_scb->seps[i].av_handle);
944 p_scb->seps[i].av_handle = 0;
945 }
946
947 bta_av_dereg_comp((tBTA_AV_DATA*)&msg);
948 } else {
949 /* report stream closed to main SM */
950 msg.is_up = false;
951 msg.peer_addr = p_scb->PeerAddress();
952 bta_av_conn_chg((tBTA_AV_DATA*)&msg);
953 }
954 }
955
956 /*******************************************************************************
957 *
958 * Function bta_av_free_sdb
959 *
960 * Description Free service discovery db buffer.
961 *
962 * Returns void
963 *
964 ******************************************************************************/
bta_av_free_sdb(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)965 void bta_av_free_sdb(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
966 p_scb->sdp_discovery_started = false;
967 }
968
969 /*******************************************************************************
970 *
971 * Function bta_av_config_ind
972 *
973 * Description Handle a stream configuration indication from the peer.
974 *
975 * Returns void
976 *
977 ******************************************************************************/
bta_av_config_ind(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)978 void bta_av_config_ind(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
979 tBTA_AV_CI_SETCONFIG setconfig{};
980 tAVDT_SEP_INFO* p_info;
981 const AvdtpSepConfig* p_evt_cfg = &p_data->str_msg.cfg;
982 uint8_t psc_mask = (p_evt_cfg->psc_mask | p_scb->cfg.psc_mask);
983 uint8_t
984 local_sep; /* sep type of local handle on which connection was received */
985 tBTA_AV_STR_MSG* p_msg = (tBTA_AV_STR_MSG*)p_data;
986
987 local_sep = bta_av_get_scb_sep_type(p_scb, p_msg->handle);
988 p_scb->avdt_label = p_data->str_msg.msg.hdr.label;
989
990 APPL_TRACE_DEBUG("%s: peer %s bta_handle:0x%x local_sep:%d", __func__,
991 p_scb->PeerAddress().ToString().c_str(), p_scb->hndl,
992 local_sep);
993 APPL_TRACE_DEBUG("%s: codec: %s", __func__,
994 A2DP_CodecInfoString(p_evt_cfg->codec_info).c_str());
995
996 memcpy(p_scb->cfg.codec_info, p_evt_cfg->codec_info, AVDT_CODEC_SIZE);
997 bta_av_save_addr(p_scb, p_data->str_msg.bd_addr);
998
999 /* Clear collision mask */
1000 p_scb->coll_mask = 0;
1001 alarm_cancel(bta_av_cb.accept_signalling_timer);
1002
1003 /* if no codec parameters in configuration, fail */
1004 if ((p_evt_cfg->num_codec == 0) ||
1005 /* or the peer requests for a service we do not support */
1006 ((psc_mask != p_scb->cfg.psc_mask) &&
1007 (psc_mask != (p_scb->cfg.psc_mask & ~AVDT_PSC_DELAY_RPT)))) {
1008 setconfig.hndl = p_scb->hndl; /* we may not need this */
1009 setconfig.err_code = AVDT_ERR_UNSUP_CFG;
1010 bta_av_ssm_execute(p_scb, BTA_AV_CI_SETCONFIG_FAIL_EVT,
1011 (tBTA_AV_DATA*)&setconfig);
1012 } else {
1013 p_info = &p_scb->sep_info[0];
1014 p_info->in_use = 0;
1015 p_info->media_type = p_scb->media_type;
1016 p_info->seid = p_data->str_msg.msg.config_ind.int_seid;
1017
1018 /* Sep type of Peer will be oppsite role to our local sep */
1019 if (local_sep == AVDT_TSEP_SRC)
1020 p_info->tsep = AVDT_TSEP_SNK;
1021 else if (local_sep == AVDT_TSEP_SNK)
1022 p_info->tsep = AVDT_TSEP_SRC;
1023
1024 p_scb->role |= BTA_AV_ROLE_AD_ACP;
1025 p_scb->cur_psc_mask = p_evt_cfg->psc_mask;
1026 if (bta_av_cb.features & BTA_AV_FEAT_RCTG)
1027 p_scb->use_rc = true;
1028 else
1029 p_scb->use_rc = false;
1030
1031 p_scb->num_seps = 1;
1032 p_scb->sep_info_idx = 0;
1033 APPL_TRACE_DEBUG("%s: SEID: %d use_rc: %d cur_psc_mask:0x%x", __func__,
1034 p_info->seid, p_scb->use_rc, p_scb->cur_psc_mask);
1035 /* in case of A2DP SINK this is the first time peer data is being sent to
1036 * co functions */
1037 if (local_sep == AVDT_TSEP_SNK) {
1038 p_scb->p_cos->setcfg(p_scb->hndl, p_scb->PeerAddress(),
1039 p_evt_cfg->codec_info, p_info->seid,
1040 p_evt_cfg->num_protect, p_evt_cfg->protect_info,
1041 AVDT_TSEP_SNK, p_msg->handle);
1042 } else {
1043 p_scb->p_cos->setcfg(p_scb->hndl, p_scb->PeerAddress(),
1044 p_evt_cfg->codec_info, p_info->seid,
1045 p_evt_cfg->num_protect, p_evt_cfg->protect_info,
1046 AVDT_TSEP_SRC, p_msg->handle);
1047 }
1048 }
1049 }
1050
1051 /*******************************************************************************
1052 *
1053 * Function bta_av_disconnect_req
1054 *
1055 * Description Disconnect AVDTP connection.
1056 *
1057 * Returns void
1058 *
1059 ******************************************************************************/
bta_av_disconnect_req(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)1060 void bta_av_disconnect_req(tBTA_AV_SCB* p_scb,
1061 UNUSED_ATTR tBTA_AV_DATA* p_data) {
1062 tBTA_AV_RCB* p_rcb;
1063
1064 APPL_TRACE_API("%s: conn_lcb: 0x%x peer_addr: %s", __func__,
1065 bta_av_cb.conn_lcb, p_scb->PeerAddress().ToString().c_str());
1066
1067 alarm_cancel(bta_av_cb.link_signalling_timer);
1068 alarm_cancel(p_scb->avrc_ct_timer);
1069
1070 // conn_lcb is the index bitmask of all used LCBs, and since LCB and SCB use
1071 // the same index, it should be safe to use SCB index here.
1072 if ((bta_av_cb.conn_lcb & (1 << p_scb->hdi)) != 0) {
1073 p_rcb = bta_av_get_rcb_by_shdl((uint8_t)(p_scb->hdi + 1));
1074 if (p_rcb) bta_av_del_rc(p_rcb);
1075 AVDT_DisconnectReq(p_scb->PeerAddress(), &bta_av_proc_stream_evt);
1076 } else {
1077 APPL_TRACE_WARNING("%s: conn_lcb=0x%x bta_handle=0x%x (hdi=%u) no link",
1078 __func__, bta_av_cb.conn_lcb, p_scb->hndl, p_scb->hdi);
1079 bta_av_ssm_execute(p_scb, BTA_AV_AVDT_DISCONNECT_EVT, NULL);
1080 }
1081 }
1082
1083 /*******************************************************************************
1084 *
1085 * Function bta_av_security_req
1086 *
1087 * Description Send an AVDTP security request.
1088 *
1089 * Returns void
1090 *
1091 ******************************************************************************/
bta_av_security_req(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1092 void bta_av_security_req(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1093 if (bta_av_cb.features & BTA_AV_FEAT_PROTECT) {
1094 AVDT_SecurityReq(p_scb->avdt_handle, p_data->api_protect_req.p_data,
1095 p_data->api_protect_req.len);
1096 }
1097 }
1098
1099 /*******************************************************************************
1100 *
1101 * Function bta_av_security_rsp
1102 *
1103 * Description Send an AVDTP security response.
1104 *
1105 * Returns void
1106 *
1107 ******************************************************************************/
bta_av_security_rsp(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1108 void bta_av_security_rsp(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1109 if (bta_av_cb.features & BTA_AV_FEAT_PROTECT) {
1110 AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label,
1111 p_data->api_protect_rsp.error_code,
1112 p_data->api_protect_rsp.p_data,
1113 p_data->api_protect_rsp.len);
1114 } else {
1115 AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label, AVDT_ERR_NSC, NULL,
1116 0);
1117 }
1118 }
1119
1120 /*******************************************************************************
1121 *
1122 * Function bta_av_setconfig_rsp
1123 *
1124 * Description setconfig is OK
1125 *
1126 * Returns void
1127 *
1128 ******************************************************************************/
bta_av_setconfig_rsp(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1129 void bta_av_setconfig_rsp(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1130 uint8_t num = p_data->ci_setconfig.num_seid + 1;
1131 uint8_t avdt_handle = p_data->ci_setconfig.avdt_handle;
1132 uint8_t* p_seid = p_data->ci_setconfig.p_seid;
1133 int i;
1134 uint8_t local_sep;
1135
1136 /* we like this codec_type. find the sep_idx */
1137 local_sep = bta_av_get_scb_sep_type(p_scb, avdt_handle);
1138 bta_av_adjust_seps_idx(p_scb, avdt_handle);
1139 LOG_DEBUG(
1140 "%s: peer %s bta_handle=0x%x avdt_handle=%d sep_idx=%d cur_psc_mask:0x%x",
1141 __func__, p_scb->PeerAddress().ToString().c_str(), p_scb->hndl,
1142 p_scb->avdt_handle, p_scb->sep_idx, p_scb->cur_psc_mask);
1143
1144 if ((AVDT_TSEP_SNK == local_sep) &&
1145 (p_data->ci_setconfig.err_code == AVDT_SUCCESS) &&
1146 (p_scb->seps[p_scb->sep_idx].p_app_sink_data_cback != NULL)) {
1147 tBTA_AV_MEDIA av_sink_codec_info;
1148 av_sink_codec_info.avk_config.bd_addr = p_scb->PeerAddress();
1149 av_sink_codec_info.avk_config.codec_info = p_scb->cfg.codec_info;
1150 p_scb->seps[p_scb->sep_idx].p_app_sink_data_cback(
1151 p_scb->PeerAddress(), BTA_AV_SINK_MEDIA_CFG_EVT, &av_sink_codec_info);
1152 }
1153
1154 AVDT_ConfigRsp(p_scb->avdt_handle, p_scb->avdt_label,
1155 p_data->ci_setconfig.err_code, p_data->ci_setconfig.category);
1156
1157 alarm_cancel(bta_av_cb.link_signalling_timer);
1158
1159 if (p_data->ci_setconfig.err_code == AVDT_SUCCESS) {
1160 p_scb->wait = BTA_AV_WAIT_ACP_CAPS_ON;
1161 if (p_data->ci_setconfig.recfg_needed)
1162 p_scb->role |= BTA_AV_ROLE_SUSPEND_OPT;
1163 APPL_TRACE_DEBUG("%s: recfg_needed:%d role:0x%x num:%d", __func__,
1164 p_data->ci_setconfig.recfg_needed, p_scb->role, num);
1165 /* callout module tells BTA the number of "good" SEPs and their SEIDs.
1166 * getcap on these SEID */
1167 p_scb->num_seps = num;
1168
1169 if (p_scb->cur_psc_mask & AVDT_PSC_DELAY_RPT)
1170 p_scb->SetAvdtpVersion(AVDT_VERSION_1_3);
1171
1172 if (A2DP_GetCodecType(p_scb->cfg.codec_info) == A2DP_MEDIA_CT_SBC ||
1173 num > 1) {
1174 /* if SBC is used by the SNK as INT, discover req is not sent in
1175 * bta_av_config_ind.
1176 * call disc_res now */
1177 /* this is called in A2DP SRC path only, In case of SINK we don't need it
1178 */
1179 if (local_sep == AVDT_TSEP_SRC)
1180 p_scb->p_cos->disc_res(p_scb->hndl, p_scb->PeerAddress(), num, num, 0,
1181 UUID_SERVCLASS_AUDIO_SOURCE);
1182 } else {
1183 /* we do not know the peer device and it is using non-SBC codec
1184 * we need to know all the SEPs on SNK */
1185 if (p_scb->uuid_int == 0) p_scb->uuid_int = p_scb->open_api.uuid;
1186 bta_av_discover_req(p_scb, NULL);
1187 return;
1188 }
1189
1190 for (i = 1; i < num; i++) {
1191 APPL_TRACE_DEBUG("%s: sep_info[%d] SEID: %d", __func__, i, p_seid[i - 1]);
1192 /* initialize the sep_info[] to get capabilities */
1193 p_scb->sep_info[i].in_use = false;
1194 p_scb->sep_info[i].tsep = AVDT_TSEP_SNK;
1195 p_scb->sep_info[i].media_type = p_scb->media_type;
1196 p_scb->sep_info[i].seid = p_seid[i - 1];
1197 }
1198
1199 /* only in case of local sep as SRC we need to look for other SEPs, In case
1200 * of SINK we don't */
1201 if (local_sep == AVDT_TSEP_SRC) {
1202 /* Make sure UUID has been initialized... */
1203 if (p_scb->uuid_int == 0) p_scb->uuid_int = p_scb->open_api.uuid;
1204 bta_av_next_getcap(p_scb, p_data);
1205 }
1206 }
1207 }
1208
1209 /*******************************************************************************
1210 *
1211 * Function bta_av_str_opened
1212 *
1213 * Description Stream opened OK (incoming/outgoing).
1214 *
1215 * Returns void
1216 *
1217 ******************************************************************************/
bta_av_str_opened(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1218 void bta_av_str_opened(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1219 tBTA_AV_CONN_CHG msg;
1220 uint8_t* p;
1221
1222 APPL_TRACE_DEBUG("%s: peer %s bta_handle: 0x%x", __func__,
1223 p_scb->PeerAddress().ToString().c_str(), p_scb->hndl);
1224
1225 msg.hdr.layer_specific = p_scb->hndl;
1226 msg.is_up = true;
1227 msg.peer_addr = p_scb->PeerAddress();
1228 p_scb->l2c_cid = AVDT_GetL2CapChannel(p_scb->avdt_handle);
1229 bta_av_conn_chg((tBTA_AV_DATA*)&msg);
1230 /* set the congestion flag, so AV would not send media packets by accident */
1231 p_scb->cong = true;
1232 p_scb->offload_start_pending = false;
1233
1234 p_scb->stream_mtu =
1235 p_data->str_msg.msg.open_ind.peer_mtu - AVDT_MEDIA_HDR_SIZE;
1236 APPL_TRACE_DEBUG("%s: l2c_cid: 0x%x stream_mtu: %d", __func__, p_scb->l2c_cid,
1237 p_scb->stream_mtu);
1238
1239 /* Set the media channel as high priority */
1240 L2CA_SetTxPriority(p_scb->l2c_cid, L2CAP_CHNL_PRIORITY_HIGH);
1241 L2CA_SetChnlFlushability(p_scb->l2c_cid, true);
1242
1243 bta_sys_conn_open(BTA_ID_AV, p_scb->app_id, p_scb->PeerAddress());
1244 memset(&p_scb->q_info, 0, sizeof(tBTA_AV_Q_INFO));
1245
1246 p_scb->l2c_bufs = 0;
1247 p_scb->p_cos->open(p_scb->hndl, p_scb->PeerAddress(), p_scb->stream_mtu);
1248
1249 {
1250 /* TODO check if other audio channel is open.
1251 * If yes, check if reconfig is needed
1252 * Rigt now we do not do this kind of checking.
1253 * BTA-AV is INT for 2nd audio connection.
1254 * The application needs to make sure the current codec_info is proper.
1255 * If one audio connection is open and another SNK attempts to connect to
1256 * AV,
1257 * the connection will be rejected.
1258 */
1259 /* check if other audio channel is started. If yes, start */
1260 tBTA_AV_OPEN open;
1261 open.bd_addr = p_scb->PeerAddress();
1262 open.chnl = p_scb->chnl;
1263 open.hndl = p_scb->hndl;
1264 open.status = BTA_AV_SUCCESS;
1265 open.edr = 0;
1266 p = BTM_ReadRemoteFeatures(p_scb->PeerAddress());
1267 if (p != NULL) {
1268 if (HCI_EDR_ACL_2MPS_SUPPORTED(p)) open.edr |= BTA_AV_EDR_2MBPS;
1269 if (HCI_EDR_ACL_3MPS_SUPPORTED(p)) {
1270 if (!interop_match_addr(INTEROP_2MBPS_LINK_ONLY,
1271 &p_scb->PeerAddress())) {
1272 open.edr |= BTA_AV_EDR_3MBPS;
1273 }
1274 }
1275 }
1276 #if (BTA_AR_INCLUDED == TRUE)
1277 bta_ar_avdt_conn(BTA_ID_AV, open.bd_addr, p_scb->hdi);
1278 #endif
1279 if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SRC) {
1280 open.starting = false;
1281 open.sep = AVDT_TSEP_SNK;
1282 } else if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SNK) {
1283 open.starting = bta_av_chk_start(p_scb);
1284 open.sep = AVDT_TSEP_SRC;
1285 }
1286
1287 tBTA_AV bta_av_data;
1288 bta_av_data.open = open;
1289 (*bta_av_cb.p_cback)(BTA_AV_OPEN_EVT, &bta_av_data);
1290 if (open.starting) {
1291 bta_av_ssm_execute(p_scb, BTA_AV_AP_START_EVT, NULL);
1292 }
1293 }
1294
1295 // This code is used to pass PTS TC for AVDTP ABORT
1296 char value[PROPERTY_VALUE_MAX] = {0};
1297 if ((osi_property_get("bluetooth.pts.force_a2dp_abort", value, "false")) &&
1298 (!strcmp(value, "true"))) {
1299 APPL_TRACE_ERROR("%s: Calling AVDT_AbortReq", __func__);
1300 AVDT_AbortReq(p_scb->avdt_handle);
1301 }
1302 }
1303
1304 /*******************************************************************************
1305 *
1306 * Function bta_av_security_ind
1307 *
1308 * Description Handle an AVDTP security indication.
1309 *
1310 * Returns void
1311 *
1312 ******************************************************************************/
bta_av_security_ind(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1313 void bta_av_security_ind(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1314 p_scb->avdt_label = p_data->str_msg.msg.hdr.label;
1315
1316 if (bta_av_cb.features & BTA_AV_FEAT_PROTECT) {
1317 tBTA_AV_PROTECT_REQ protect_req;
1318 protect_req.chnl = p_scb->chnl;
1319 protect_req.hndl = p_scb->hndl;
1320 protect_req.p_data = p_data->str_msg.msg.security_ind.p_data;
1321 protect_req.len = p_data->str_msg.msg.security_ind.len;
1322
1323 tBTA_AV bta_av_data;
1324 bta_av_data.protect_req = protect_req;
1325 (*bta_av_cb.p_cback)(BTA_AV_PROTECT_REQ_EVT, &bta_av_data);
1326 }
1327 /* app doesn't support security indication; respond with failure */
1328 else {
1329 AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label, AVDT_ERR_NSC, NULL,
1330 0);
1331 }
1332 }
1333
1334 /*******************************************************************************
1335 *
1336 * Function bta_av_security_cfm
1337 *
1338 * Description Handle an AVDTP security confirm.
1339 *
1340 * Returns void
1341 *
1342 ******************************************************************************/
bta_av_security_cfm(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1343 void bta_av_security_cfm(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1344 if (bta_av_cb.features & BTA_AV_FEAT_PROTECT) {
1345 tBTA_AV_PROTECT_RSP protect_rsp;
1346 protect_rsp.chnl = p_scb->chnl;
1347 protect_rsp.hndl = p_scb->hndl;
1348 protect_rsp.p_data = p_data->str_msg.msg.security_cfm.p_data;
1349 protect_rsp.len = p_data->str_msg.msg.security_cfm.len;
1350 protect_rsp.err_code = p_data->str_msg.msg.hdr.err_code;
1351
1352 tBTA_AV bta_av_data;
1353 bta_av_data.protect_rsp = protect_rsp;
1354 (*bta_av_cb.p_cback)(BTA_AV_PROTECT_RSP_EVT, &bta_av_data);
1355 }
1356 }
1357
1358 /*******************************************************************************
1359 *
1360 * Function bta_av_do_close
1361 *
1362 * Description Close stream.
1363 *
1364 * Returns void
1365 *
1366 ******************************************************************************/
bta_av_do_close(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)1367 void bta_av_do_close(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
1368 APPL_TRACE_DEBUG("%s: p_scb->co_started=%d", __func__, p_scb->co_started);
1369
1370 /* stop stream if started */
1371 if (p_scb->co_started) {
1372 bta_av_str_stopped(p_scb, NULL);
1373 }
1374 alarm_cancel(bta_av_cb.link_signalling_timer);
1375
1376 /* close stream */
1377 p_scb->started = false;
1378 p_scb->use_rtp_header_marker_bit = false;
1379
1380 /* drop the buffers queued in L2CAP */
1381 L2CA_FlushChannel(p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
1382
1383 AVDT_CloseReq(p_scb->avdt_handle);
1384 /* just in case that the link is congested, link is flow controled by peer or
1385 * for whatever reason the the close request can not be sent in time.
1386 * when this timer expires, AVDT_DisconnectReq will be called to disconnect
1387 * the link
1388 */
1389 bta_sys_start_timer(p_scb->avrc_ct_timer, BTA_AV_CLOSE_REQ_TIME_VAL,
1390 BTA_AV_API_CLOSE_EVT, p_scb->hndl);
1391 }
1392
1393 /*******************************************************************************
1394 *
1395 * Function bta_av_connect_req
1396 *
1397 * Description Connect AVDTP connection.
1398 *
1399 * Returns void
1400 *
1401 ******************************************************************************/
bta_av_connect_req(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)1402 void bta_av_connect_req(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
1403 APPL_TRACE_DEBUG("%s: peer %s coll_mask=0x%02x", __func__,
1404 p_scb->PeerAddress().ToString().c_str(), p_scb->coll_mask);
1405 p_scb->sdp_discovery_started = false;
1406 if (p_scb->coll_mask & BTA_AV_COLL_INC_TMR) {
1407 /* SNK initiated L2C connection while SRC was doing SDP. */
1408 /* Wait until timeout to check if SNK starts signalling. */
1409 APPL_TRACE_WARNING("%s: coll_mask=0x%02x incoming timer is up", __func__,
1410 p_scb->coll_mask);
1411 p_scb->coll_mask |= BTA_AV_COLL_API_CALLED;
1412 APPL_TRACE_EVENT("%s: updated coll_mask=0x%02x", __func__,
1413 p_scb->coll_mask);
1414 return;
1415 }
1416
1417 AVDT_ConnectReq(p_scb->PeerAddress(), p_scb->hdi, p_scb->sec_mask,
1418 &bta_av_proc_stream_evt);
1419 }
1420
1421 /*******************************************************************************
1422 *
1423 * Function bta_av_sdp_failed
1424 *
1425 * Description Service discovery failed.
1426 *
1427 * Returns void
1428 *
1429 ******************************************************************************/
bta_av_sdp_failed(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1430 void bta_av_sdp_failed(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1431 APPL_TRACE_ERROR("%s: peer_addr=%s open_status=%d", __func__,
1432 p_scb->PeerAddress().ToString().c_str(), p_scb->open_status);
1433
1434 if (p_scb->open_status == BTA_AV_SUCCESS) {
1435 p_scb->open_status = BTA_AV_FAIL_SDP;
1436 }
1437
1438 p_scb->sdp_discovery_started = false;
1439 bta_av_str_closed(p_scb, p_data);
1440 }
1441
1442 /*******************************************************************************
1443 *
1444 * Function bta_av_disc_results
1445 *
1446 * Description Handle the AVDTP discover results. Search through the
1447 * results and find the first available stream, and get
1448 * its capabilities.
1449 *
1450 * Returns void
1451 *
1452 ******************************************************************************/
bta_av_disc_results(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1453 void bta_av_disc_results(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1454 uint8_t num_snks = 0, num_srcs = 0, i;
1455 /* our uuid in case we initiate connection */
1456 uint16_t uuid_int = p_scb->uuid_int;
1457
1458 APPL_TRACE_DEBUG("%s: peer %s bta_handle: 0x%x initiator UUID 0x%x", __func__,
1459 p_scb->PeerAddress().ToString().c_str(), p_scb->hndl,
1460 uuid_int);
1461
1462 /* store number of stream endpoints returned */
1463 p_scb->num_seps = p_data->str_msg.msg.discover_cfm.num_seps;
1464
1465 for (i = 0; i < p_scb->num_seps; i++) {
1466 /* steam not in use, is a sink, and is audio */
1467 if ((!p_scb->sep_info[i].in_use) &&
1468 (p_scb->sep_info[i].media_type == p_scb->media_type)) {
1469 if ((p_scb->sep_info[i].tsep == AVDT_TSEP_SNK) &&
1470 (uuid_int == UUID_SERVCLASS_AUDIO_SOURCE))
1471 num_snks++;
1472
1473 if ((p_scb->sep_info[i].tsep == AVDT_TSEP_SRC) &&
1474 (uuid_int == UUID_SERVCLASS_AUDIO_SINK))
1475 num_srcs++;
1476 }
1477 }
1478
1479 p_scb->p_cos->disc_res(p_scb->hndl, p_scb->PeerAddress(), p_scb->num_seps,
1480 num_snks, num_srcs, uuid_int);
1481 p_scb->num_disc_snks = num_snks;
1482 p_scb->num_disc_srcs = num_srcs;
1483
1484 /* if we got any */
1485 if (p_scb->num_seps > 0) {
1486 /* initialize index into discovery results */
1487 p_scb->sep_info_idx = 0;
1488
1489 /* get the capabilities of the first available stream */
1490 bta_av_next_getcap(p_scb, p_data);
1491 }
1492 /* else we got discover response but with no streams; we're done */
1493 else {
1494 APPL_TRACE_ERROR("%s: BTA_AV_STR_DISC_FAIL_EVT: peer_addr=%s", __func__,
1495 p_scb->PeerAddress().ToString().c_str());
1496 bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, p_data);
1497 }
1498 }
1499
1500 /*******************************************************************************
1501 *
1502 * Function bta_av_disc_res_as_acp
1503 *
1504 * Description Handle the AVDTP discover results. Search through the
1505 * results and find the first available stream, and get
1506 * its capabilities.
1507 *
1508 * Returns void
1509 *
1510 ******************************************************************************/
bta_av_disc_res_as_acp(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1511 void bta_av_disc_res_as_acp(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1512 uint8_t num_snks = 0, i;
1513
1514 APPL_TRACE_DEBUG("%s: peer %s bta_handle: 0x%x", __func__,
1515 p_scb->PeerAddress().ToString().c_str(), p_scb->hndl);
1516
1517 /* store number of stream endpoints returned */
1518 p_scb->num_seps = p_data->str_msg.msg.discover_cfm.num_seps;
1519
1520 for (i = 0; i < p_scb->num_seps; i++) {
1521 /* steam is a sink, and is audio */
1522 if ((p_scb->sep_info[i].tsep == AVDT_TSEP_SNK) &&
1523 (p_scb->sep_info[i].media_type == p_scb->media_type)) {
1524 p_scb->sep_info[i].in_use = false;
1525 num_snks++;
1526 }
1527 }
1528 p_scb->p_cos->disc_res(p_scb->hndl, p_scb->PeerAddress(), p_scb->num_seps,
1529 num_snks, 0, UUID_SERVCLASS_AUDIO_SOURCE);
1530 p_scb->num_disc_snks = num_snks;
1531 p_scb->num_disc_srcs = 0;
1532
1533 /* if we got any */
1534 if (p_scb->num_seps > 0) {
1535 /* initialize index into discovery results */
1536 p_scb->sep_info_idx = 0;
1537
1538 /* get the capabilities of the first available stream */
1539 bta_av_next_getcap(p_scb, p_data);
1540 }
1541 /* else we got discover response but with no streams; we're done */
1542 else {
1543 APPL_TRACE_ERROR("%s: BTA_AV_STR_DISC_FAIL_EVT: peer_addr=%s", __func__,
1544 p_scb->PeerAddress().ToString().c_str());
1545 bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, p_data);
1546 }
1547 }
1548
1549 /*******************************************************************************
1550 *
1551 * Function bta_av_save_caps
1552 *
1553 * Description report the SNK SEP capabilities to application
1554 *
1555 * Returns void
1556 *
1557 ******************************************************************************/
bta_av_save_caps(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1558 void bta_av_save_caps(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1559 AvdtpSepConfig cfg;
1560 tAVDT_SEP_INFO* p_info = &p_scb->sep_info[p_scb->sep_info_idx];
1561 uint8_t old_wait = p_scb->wait;
1562 bool getcap_done = false;
1563
1564 APPL_TRACE_DEBUG(
1565 "%s: peer %s bta_handle:0x%x num_seps:%d sep_info_idx:%d wait:0x%x",
1566 __func__, p_scb->PeerAddress().ToString().c_str(), p_scb->hndl,
1567 p_scb->num_seps, p_scb->sep_info_idx, p_scb->wait);
1568 APPL_TRACE_DEBUG("%s: codec: %s", __func__,
1569 A2DP_CodecInfoString(p_scb->peer_cap.codec_info).c_str());
1570
1571 cfg = p_scb->peer_cap;
1572 /* let application know the capability of the SNK */
1573 if (p_scb->p_cos->getcfg(p_scb->hndl, p_scb->PeerAddress(), cfg.codec_info,
1574 &p_scb->sep_info_idx, p_info->seid, &cfg.num_protect,
1575 cfg.protect_info) != A2DP_SUCCESS) {
1576 p_scb->sep_info_idx++;
1577 APPL_TRACE_DEBUG("%s: result: next sep_info_idx:%d", __func__,
1578 p_scb->sep_info_idx);
1579 } else {
1580 // All capabilities found
1581 getcap_done = true;
1582 APPL_TRACE_DEBUG("%s: result: done sep_info_idx:%d", __func__,
1583 p_scb->sep_info_idx);
1584 }
1585 APPL_TRACE_DEBUG("%s: codec: %s", __func__,
1586 A2DP_CodecInfoString(cfg.codec_info).c_str());
1587
1588 if (p_scb->num_seps > p_scb->sep_info_idx && !getcap_done) {
1589 /* Some devices have seps at the end of the discover list, which is not */
1590 /* matching media type(video not audio). */
1591 /* In this case, we are done with getcap without sending another */
1592 /* request to AVDT. */
1593 if (!bta_av_next_getcap(p_scb, p_data)) getcap_done = true;
1594 } else {
1595 getcap_done = true;
1596 }
1597
1598 if (getcap_done) {
1599 APPL_TRACE_DEBUG("%s: getcap_done: num_seps:%d sep_info_idx:%d wait:0x%x",
1600 __func__, p_scb->num_seps, p_scb->sep_info_idx,
1601 p_scb->wait);
1602 p_scb->wait &= ~(BTA_AV_WAIT_ACP_CAPS_ON | BTA_AV_WAIT_ACP_CAPS_STARTED);
1603 if (old_wait & BTA_AV_WAIT_ACP_CAPS_STARTED) {
1604 bta_av_start_ok(p_scb, NULL);
1605 }
1606 }
1607 }
1608
1609 /*******************************************************************************
1610 *
1611 * Function bta_av_set_use_rc
1612 *
1613 * Description set to use AVRC for this stream control block.
1614 *
1615 * Returns void
1616 *
1617 ******************************************************************************/
bta_av_set_use_rc(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)1618 void bta_av_set_use_rc(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
1619 p_scb->use_rc = true;
1620 }
1621
1622 /*******************************************************************************
1623 *
1624 * Function bta_av_cco_close
1625 *
1626 * Description call close call-out function.
1627 *
1628 * Returns void
1629 *
1630 ******************************************************************************/
bta_av_cco_close(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)1631 void bta_av_cco_close(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
1632 APPL_TRACE_DEBUG("%s: peer %s bta_handle:0x%x", __func__,
1633 p_scb->PeerAddress().ToString().c_str(), p_scb->hndl);
1634 p_scb->p_cos->close(p_scb->hndl, p_scb->PeerAddress());
1635 }
1636
1637 /*******************************************************************************
1638 *
1639 * Function bta_av_open_failed
1640 *
1641 * Description Failed to open an AVDT stream
1642 *
1643 * Returns void
1644 *
1645 ******************************************************************************/
bta_av_open_failed(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1646 void bta_av_open_failed(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1647 bool is_av_opened = false;
1648 tBTA_AV_SCB* p_opened_scb = NULL;
1649 uint8_t idx;
1650
1651 APPL_TRACE_ERROR("%s: peer_addr=%s", __func__,
1652 p_scb->PeerAddress().ToString().c_str());
1653 p_scb->open_status = BTA_AV_FAIL_STREAM;
1654 bta_av_cco_close(p_scb, p_data);
1655
1656 /* check whether there is already an opened audio or video connection with the
1657 * same device */
1658 for (idx = 0; (idx < BTA_AV_NUM_STRS) && (!is_av_opened); idx++) {
1659 p_opened_scb = bta_av_cb.p_scb[idx];
1660 if (p_opened_scb && (p_opened_scb->state == BTA_AV_OPEN_SST) &&
1661 (p_opened_scb->PeerAddress() == p_scb->PeerAddress()))
1662 is_av_opened = true;
1663 }
1664
1665 /* if there is already an active AV connnection with the same bd_addr,
1666 don't send disconnect req, just report the open event with
1667 BTA_AV_FAIL_GET_CAP status */
1668 if (is_av_opened) {
1669 tBTA_AV_OPEN open;
1670 open.bd_addr = p_scb->PeerAddress();
1671 open.chnl = p_scb->chnl;
1672 open.hndl = p_scb->hndl;
1673 open.status = BTA_AV_FAIL_GET_CAP;
1674 open.starting = bta_av_chk_start(p_scb);
1675 open.edr = 0;
1676 /* set the state back to initial state */
1677 bta_av_set_scb_sst_init(p_scb);
1678
1679 if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SRC) {
1680 open.sep = AVDT_TSEP_SNK;
1681 } else if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SNK) {
1682 open.sep = AVDT_TSEP_SRC;
1683 }
1684
1685 APPL_TRACE_ERROR(
1686 "%s: there is already an active connection: peer_addr=%s chnl=%d "
1687 "hndl=0x%x status=%d starting=%d edr=%d",
1688 __func__, open.bd_addr.ToString().c_str(), open.chnl, open.hndl,
1689 open.status, open.starting, open.edr);
1690
1691 tBTA_AV bta_av_data;
1692 bta_av_data.open = open;
1693 (*bta_av_cb.p_cback)(BTA_AV_OPEN_EVT, &bta_av_data);
1694 } else {
1695 AVDT_DisconnectReq(p_scb->PeerAddress(), &bta_av_proc_stream_evt);
1696 }
1697 }
1698
1699 /*******************************************************************************
1700 *
1701 * Function bta_av_getcap_results
1702 *
1703 * Description Handle the AVDTP get capabilities results. Check the codec
1704 * type and see if it matches ours. If it does not, get the
1705 * capabilities of the next stream, if any.
1706 *
1707 * Returns void
1708 *
1709 ******************************************************************************/
bta_av_getcap_results(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1710 void bta_av_getcap_results(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1711 AvdtpSepConfig cfg = p_scb->cfg;
1712 uint8_t media_type = A2DP_GetMediaType(p_scb->peer_cap.codec_info);
1713 tAVDT_SEP_INFO* p_info = &p_scb->sep_info[p_scb->sep_info_idx];
1714
1715 cfg.num_codec = 1;
1716 cfg.num_protect = p_scb->peer_cap.num_protect;
1717 memcpy(cfg.codec_info, p_scb->peer_cap.codec_info, AVDT_CODEC_SIZE);
1718 memcpy(cfg.protect_info, p_scb->peer_cap.protect_info, AVDT_PROTECT_SIZE);
1719
1720 APPL_TRACE_DEBUG("%s: peer %s bta_handle:0x%x num_codec:%d psc_mask=0x%x",
1721 __func__, p_scb->PeerAddress().ToString().c_str(),
1722 p_scb->hndl, p_scb->peer_cap.num_codec, p_scb->cfg.psc_mask);
1723 APPL_TRACE_DEBUG("%s: media type 0x%x, 0x%x", __func__, media_type,
1724 p_scb->media_type);
1725 APPL_TRACE_DEBUG("%s: codec: %s", __func__,
1726 A2DP_CodecInfoString(p_scb->cfg.codec_info).c_str());
1727
1728 /* if codec present and we get a codec configuration */
1729 if ((p_scb->peer_cap.num_codec != 0) && (media_type == p_scb->media_type) &&
1730 (p_scb->p_cos->getcfg(p_scb->hndl, p_scb->PeerAddress(), cfg.codec_info,
1731 &p_scb->sep_info_idx, p_info->seid,
1732 &cfg.num_protect,
1733 cfg.protect_info) == A2DP_SUCCESS)) {
1734 /* UUID for which connection was initiatied */
1735 uint16_t uuid_int = p_scb->uuid_int;
1736
1737 /* save copy of codec configuration */
1738 p_scb->cfg = cfg;
1739
1740 APPL_TRACE_DEBUG("%s: result: sep_info_idx=%d", __func__,
1741 p_scb->sep_info_idx);
1742 APPL_TRACE_DEBUG("%s: codec: %s", __func__,
1743 A2DP_CodecInfoString(p_scb->cfg.codec_info).c_str());
1744
1745 APPL_TRACE_DEBUG("%s: initiator UUID = 0x%x", __func__, uuid_int);
1746 if (uuid_int == UUID_SERVCLASS_AUDIO_SOURCE)
1747 bta_av_adjust_seps_idx(p_scb,
1748 bta_av_get_scb_handle(p_scb, AVDT_TSEP_SRC));
1749 else if (uuid_int == UUID_SERVCLASS_AUDIO_SINK)
1750 bta_av_adjust_seps_idx(p_scb,
1751 bta_av_get_scb_handle(p_scb, AVDT_TSEP_SNK));
1752 LOG_DEBUG("%s: sep_idx=%d avdt_handle=%d bta_handle=0x%x", __func__,
1753 p_scb->sep_idx, p_scb->avdt_handle, p_scb->hndl);
1754
1755 /* use only the services peer supports */
1756 cfg.psc_mask &= p_scb->peer_cap.psc_mask;
1757 p_scb->cur_psc_mask = cfg.psc_mask;
1758 APPL_TRACE_DEBUG(
1759 "%s: peer %s bta_handle:0x%x sep_idx:%d sep_info_idx:%d "
1760 "cur_psc_mask:0x%x",
1761 __func__, p_scb->PeerAddress().ToString().c_str(), p_scb->hndl,
1762 p_scb->sep_idx, p_scb->sep_info_idx, p_scb->cur_psc_mask);
1763
1764 if ((uuid_int == UUID_SERVCLASS_AUDIO_SINK) &&
1765 (p_scb->seps[p_scb->sep_idx].p_app_sink_data_cback != NULL)) {
1766 APPL_TRACE_DEBUG("%s: configure decoder for Sink connection", __func__);
1767 tBTA_AV_MEDIA av_sink_codec_info;
1768 av_sink_codec_info.avk_config.bd_addr = p_scb->PeerAddress();
1769 av_sink_codec_info.avk_config.codec_info = p_scb->cfg.codec_info;
1770 p_scb->seps[p_scb->sep_idx].p_app_sink_data_cback(
1771 p_scb->PeerAddress(), BTA_AV_SINK_MEDIA_CFG_EVT, &av_sink_codec_info);
1772 }
1773
1774 if (uuid_int == UUID_SERVCLASS_AUDIO_SOURCE) {
1775 A2DP_AdjustCodec(cfg.codec_info);
1776 }
1777
1778 /* open the stream */
1779 AVDT_OpenReq(p_scb->seps[p_scb->sep_idx].av_handle, p_scb->PeerAddress(),
1780 p_scb->hdi, p_scb->sep_info[p_scb->sep_info_idx].seid, &cfg);
1781 } else {
1782 /* try the next stream, if any */
1783 p_scb->sep_info_idx++;
1784 bta_av_next_getcap(p_scb, p_data);
1785 }
1786 }
1787
1788 /*******************************************************************************
1789 *
1790 * Function bta_av_setconfig_rej
1791 *
1792 * Description Send AVDTP set config reject.
1793 *
1794 * Returns void
1795 *
1796 ******************************************************************************/
bta_av_setconfig_rej(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1797 void bta_av_setconfig_rej(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1798 tBTA_AV_REJECT reject;
1799 uint8_t avdt_handle = p_data->ci_setconfig.avdt_handle;
1800
1801 bta_av_adjust_seps_idx(p_scb, avdt_handle);
1802 LOG_DEBUG("%s: sep_idx=%d avdt_handle=%d bta_handle=0x%x", __func__,
1803 p_scb->sep_idx, p_scb->avdt_handle, p_scb->hndl);
1804 AVDT_ConfigRsp(p_scb->avdt_handle, p_scb->avdt_label, AVDT_ERR_UNSUP_CFG, 0);
1805
1806 reject.bd_addr = p_data->str_msg.bd_addr;
1807 reject.hndl = p_scb->hndl;
1808
1809 tBTA_AV bta_av_data;
1810 bta_av_data.reject = reject;
1811 (*bta_av_cb.p_cback)(BTA_AV_REJECT_EVT, &bta_av_data);
1812 }
1813
1814 /*******************************************************************************
1815 *
1816 * Function bta_av_discover_req
1817 *
1818 * Description Send an AVDTP discover request to the peer.
1819 *
1820 * Returns void
1821 *
1822 ******************************************************************************/
bta_av_discover_req(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)1823 void bta_av_discover_req(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
1824 /* send avdtp discover request */
1825
1826 AVDT_DiscoverReq(p_scb->PeerAddress(), p_scb->hdi, p_scb->sep_info,
1827 BTA_AV_NUM_SEPS, &bta_av_proc_stream_evt);
1828 }
1829
1830 /*******************************************************************************
1831 *
1832 * Function bta_av_conn_failed
1833 *
1834 * Description AVDTP connection failed.
1835 *
1836 * Returns void
1837 *
1838 ******************************************************************************/
bta_av_conn_failed(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1839 void bta_av_conn_failed(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1840 APPL_TRACE_ERROR("%s: peer_addr=%s open_status=%d", __func__,
1841 p_scb->PeerAddress().ToString().c_str(), p_scb->open_status);
1842
1843 p_scb->open_status = BTA_AV_FAIL_STREAM;
1844 bta_av_str_closed(p_scb, p_data);
1845 }
1846
1847 /*******************************************************************************
1848 *
1849 * Function bta_av_do_start
1850 *
1851 * Description Start stream.
1852 *
1853 * Returns void
1854 *
1855 ******************************************************************************/
bta_av_do_start(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1856 void bta_av_do_start(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1857 uint8_t clear_policy = 0;
1858 uint8_t cur_role;
1859
1860 LOG_INFO("%s: peer %s sco_occupied:%s role:0x%x started:%s wait:0x%x",
1861 __func__, p_scb->PeerAddress().ToString().c_str(),
1862 logbool(bta_av_cb.sco_occupied).c_str(), p_scb->role,
1863 logbool(p_scb->started).c_str(), p_scb->wait);
1864 if (bta_av_cb.sco_occupied) {
1865 bta_av_start_failed(p_scb, p_data);
1866 return;
1867 }
1868
1869 /* disallow role switch during streaming, only if we are the master role
1870 * i.e. allow role switch, if we are slave.
1871 * It would not hurt us, if the peer device wants us to be master */
1872 if ((BTM_GetRole(p_scb->PeerAddress(), &cur_role) == BTM_SUCCESS) &&
1873 (cur_role == BTM_ROLE_MASTER)) {
1874 clear_policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
1875 }
1876
1877 bta_sys_clear_policy(BTA_ID_AV, clear_policy, p_scb->PeerAddress());
1878
1879 if (p_scb->started) {
1880 p_scb->role |= BTA_AV_ROLE_START_INT;
1881 if (p_scb->wait != 0) {
1882 LOG_WARN(
1883 "%s: peer %s start stream request ignored: "
1884 "already waiting: sco_occupied:%s role:0x%x started:%s wait:0x%x",
1885 __func__, p_scb->PeerAddress().ToString().c_str(),
1886 logbool(bta_av_cb.sco_occupied).c_str(), p_scb->role,
1887 logbool(p_scb->started).c_str(), p_scb->wait);
1888 return;
1889 }
1890 if (p_scb->role & BTA_AV_ROLE_SUSPEND) {
1891 notify_start_failed(p_scb);
1892 } else {
1893 bta_av_start_ok(p_scb, NULL);
1894 }
1895 return;
1896 }
1897
1898 if ((p_scb->role & BTA_AV_ROLE_START_INT) != 0) {
1899 LOG_WARN(
1900 "%s: peer %s start stream request ignored: "
1901 "already initiated: sco_occupied:%s role:0x%x started:%s wait:0x%x",
1902 __func__, p_scb->PeerAddress().ToString().c_str(),
1903 logbool(bta_av_cb.sco_occupied).c_str(), p_scb->role,
1904 logbool(p_scb->started).c_str(), p_scb->wait);
1905 return;
1906 }
1907
1908 p_scb->role |= BTA_AV_ROLE_START_INT;
1909 bta_sys_busy(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->PeerAddress());
1910 uint16_t result = AVDT_StartReq(&p_scb->avdt_handle, 1);
1911 if (result != AVDT_SUCCESS) {
1912 LOG_ERROR("%s: AVDT_StartReq failed for peer %s result:%d", __func__,
1913 p_scb->PeerAddress().ToString().c_str(), result);
1914 bta_av_start_failed(p_scb, p_data);
1915 }
1916 LOG_INFO(
1917 "%s: peer %s start requested: sco_occupied:%s role:0x%x "
1918 "started:%s wait:0x%x",
1919 __func__, p_scb->PeerAddress().ToString().c_str(),
1920 logbool(bta_av_cb.sco_occupied).c_str(), p_scb->role,
1921 logbool(p_scb->started).c_str(), p_scb->wait);
1922 }
1923
1924 /*******************************************************************************
1925 *
1926 * Function bta_av_str_stopped
1927 *
1928 * Description Stream stopped.
1929 *
1930 * Returns void
1931 *
1932 ******************************************************************************/
bta_av_str_stopped(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)1933 void bta_av_str_stopped(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
1934 tBTA_AV_SUSPEND suspend_rsp;
1935 uint8_t start = p_scb->started;
1936 bool sus_evt = true;
1937 BT_HDR* p_buf;
1938 uint8_t set_policy = HCI_ENABLE_SNIFF_MODE;
1939
1940 APPL_TRACE_ERROR(
1941 "%s: peer %s bta_handle:0x%x audio_open_cnt:%d, p_data %p start:%d",
1942 __func__, p_scb->PeerAddress().ToString().c_str(), p_scb->hndl,
1943 bta_av_cb.audio_open_cnt, p_data, start);
1944
1945 bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->PeerAddress());
1946 if ((bta_av_cb.features & BTA_AV_FEAT_MASTER) == 0 ||
1947 bta_av_cb.audio_open_cnt == 1) {
1948 set_policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
1949 }
1950 bta_sys_set_policy(BTA_ID_AV, set_policy, p_scb->PeerAddress());
1951
1952 if (p_scb->co_started) {
1953 if (p_scb->offload_started) {
1954 bta_av_vendor_offload_stop();
1955 p_scb->offload_started = false;
1956 }
1957
1958 bta_av_stream_chg(p_scb, false);
1959 p_scb->co_started = false;
1960
1961 p_scb->p_cos->stop(p_scb->hndl, p_scb->PeerAddress());
1962 L2CA_SetFlushTimeout(p_scb->PeerAddress(), L2CAP_DEFAULT_FLUSH_TO);
1963 }
1964
1965 /* if q_info.a2dp_list is not empty, drop it now */
1966 if (BTA_AV_CHNL_AUDIO == p_scb->chnl) {
1967 while (!list_is_empty(p_scb->a2dp_list)) {
1968 p_buf = (BT_HDR*)list_front(p_scb->a2dp_list);
1969 list_remove(p_scb->a2dp_list, p_buf);
1970 osi_free(p_buf);
1971 }
1972
1973 /* drop the audio buffers queued in L2CAP */
1974 if (p_data && p_data->api_stop.flush)
1975 L2CA_FlushChannel(p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
1976 }
1977
1978 suspend_rsp.chnl = p_scb->chnl;
1979 suspend_rsp.hndl = p_scb->hndl;
1980
1981 if (p_data && p_data->api_stop.suspend) {
1982 APPL_TRACE_DEBUG("%s: peer %s suspending: %d, sup:%d", __func__,
1983 p_scb->PeerAddress().ToString().c_str(), start,
1984 p_scb->suspend_sup);
1985 if ((start) && (p_scb->suspend_sup)) {
1986 sus_evt = false;
1987 p_scb->l2c_bufs = 0;
1988 AVDT_SuspendReq(&p_scb->avdt_handle, 1);
1989 }
1990
1991 /* send SUSPEND_EVT event only if not in reconfiguring state and sus_evt is
1992 * true*/
1993 if ((sus_evt) && (p_scb->state != BTA_AV_RCFG_SST)) {
1994 suspend_rsp.status = BTA_AV_SUCCESS;
1995 suspend_rsp.initiator = true;
1996 tBTA_AV bta_av_data;
1997 bta_av_data.suspend = suspend_rsp;
1998 (*bta_av_cb.p_cback)(BTA_AV_SUSPEND_EVT, &bta_av_data);
1999 }
2000 } else {
2001 suspend_rsp.status = BTA_AV_SUCCESS;
2002 suspend_rsp.initiator = true;
2003 APPL_TRACE_EVENT("%s: status %d", __func__, suspend_rsp.status);
2004
2005 // Send STOP_EVT event only if not in reconfiguring state.
2006 // However, we should send STOP_EVT if we are reconfiguring when taking
2007 // the Close->Configure->Open->Start path.
2008 if (p_scb->state != BTA_AV_RCFG_SST ||
2009 (p_data && p_data->api_stop.reconfig_stop)) {
2010 tBTA_AV bta_av_data;
2011 bta_av_data.suspend = suspend_rsp;
2012 (*bta_av_cb.p_cback)(BTA_AV_STOP_EVT, &bta_av_data);
2013 }
2014 }
2015 }
2016
2017 /*******************************************************************************
2018 *
2019 * Function bta_av_reconfig
2020 *
2021 * Description process the reconfigure request.
2022 * save the parameter in control block and
2023 * suspend, reconfigure or close the stream
2024 *
2025 * Returns void
2026 *
2027 ******************************************************************************/
bta_av_reconfig(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2028 void bta_av_reconfig(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
2029 AvdtpSepConfig* p_cfg;
2030 tBTA_AV_API_STOP stop;
2031 tBTA_AV_API_RCFG* p_rcfg = &p_data->api_reconfig;
2032
2033 APPL_TRACE_DEBUG("%s: r:%d, s:%d idx: %d (o:%d)", __func__, p_scb->recfg_sup,
2034 p_scb->suspend_sup, p_scb->rcfg_idx, p_scb->sep_info_idx);
2035
2036 p_scb->num_recfg = 0;
2037 /* store the new configuration in control block */
2038 p_cfg = &p_scb->cfg;
2039
2040 alarm_cancel(p_scb->avrc_ct_timer);
2041
2042 APPL_TRACE_DEBUG(
2043 "%s: p_scb->sep_info_idx=%d p_scb->rcfg_idx=%d p_rcfg->sep_info_idx=%d",
2044 __func__, p_scb->sep_info_idx, p_scb->rcfg_idx, p_rcfg->sep_info_idx);
2045 APPL_TRACE_DEBUG("%s: codec: %s", __func__,
2046 A2DP_CodecInfoString(p_scb->peer_cap.codec_info).c_str());
2047 APPL_TRACE_DEBUG("%s: codec: %s", __func__,
2048 A2DP_CodecInfoString(p_scb->cfg.codec_info).c_str());
2049 APPL_TRACE_DEBUG("%s: codec: %s", __func__,
2050 A2DP_CodecInfoString(p_rcfg->codec_info).c_str());
2051
2052 p_cfg->num_protect = p_rcfg->num_protect;
2053 memcpy(p_cfg->codec_info, p_rcfg->codec_info, AVDT_CODEC_SIZE);
2054 memcpy(p_cfg->protect_info, p_rcfg->p_protect_info, p_rcfg->num_protect);
2055 p_scb->rcfg_idx = p_rcfg->sep_info_idx;
2056 p_cfg->psc_mask = p_scb->cur_psc_mask;
2057
2058 // If the requested SEP index is same as the current one, then we
2059 // can Suspend->Reconfigure->Start.
2060 // Otherwise, we have to Close->Configure->Open->Start or
2061 // Close->Configure->Open for streams that are / are not started.
2062 if ((p_scb->rcfg_idx == p_scb->sep_info_idx) && p_rcfg->suspend &&
2063 p_scb->recfg_sup && p_scb->suspend_sup) {
2064 if (p_scb->started) {
2065 // Suspend->Reconfigure->Start
2066 stop.flush = false;
2067 stop.suspend = true;
2068 stop.reconfig_stop = false;
2069 bta_av_str_stopped(p_scb, (tBTA_AV_DATA*)&stop);
2070 } else {
2071 // Reconfigure
2072 APPL_TRACE_DEBUG("%s: reconfig", __func__);
2073 APPL_TRACE_DEBUG("%s: codec: %s", __func__,
2074 A2DP_CodecInfoString(p_scb->cfg.codec_info).c_str());
2075 AVDT_ReconfigReq(p_scb->avdt_handle, &p_scb->cfg);
2076 p_scb->cfg.psc_mask = p_scb->cur_psc_mask;
2077 }
2078 } else {
2079 // Close the stream first, and then Configure it
2080 APPL_TRACE_DEBUG("%s: Close/Open started: %d state: %d num_protect: %d",
2081 __func__, p_scb->started, p_scb->state,
2082 p_cfg->num_protect);
2083 if (p_scb->started) {
2084 // Close->Configure->Open->Start
2085 if ((p_scb->rcfg_idx != p_scb->sep_info_idx) && p_scb->recfg_sup) {
2086 // Make sure we trigger STOP_EVT when taking the longer road to
2087 // reconfiguration, otherwise we don't call Start.
2088 stop.flush = false;
2089 stop.suspend = false;
2090 stop.reconfig_stop = true;
2091 bta_av_str_stopped(p_scb, (tBTA_AV_DATA*)&stop);
2092 } else {
2093 bta_av_str_stopped(p_scb, NULL);
2094 }
2095 p_scb->started = false;
2096 } else {
2097 // Close->Configure->Open
2098 bta_av_str_stopped(p_scb, NULL);
2099 }
2100 // Drop the buffers queued in L2CAP
2101 L2CA_FlushChannel(p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
2102 AVDT_CloseReq(p_scb->avdt_handle);
2103 }
2104 }
2105
2106 /*******************************************************************************
2107 *
2108 * Function bta_av_data_path
2109 *
2110 * Description Handle stream data path.
2111 *
2112 * Returns void
2113 *
2114 ******************************************************************************/
bta_av_data_path(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)2115 void bta_av_data_path(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
2116 BT_HDR* p_buf = NULL;
2117 uint32_t timestamp;
2118 bool new_buf = false;
2119 uint8_t m_pt = 0x60;
2120 tAVDT_DATA_OPT_MASK opt;
2121
2122 if (!p_scb->started) return;
2123
2124 if (p_scb->cong) return;
2125
2126 if (p_scb->use_rtp_header_marker_bit) {
2127 m_pt |= AVDT_MARKER_SET;
2128 }
2129
2130 // Always get the current number of bufs que'd up
2131 p_scb->l2c_bufs =
2132 (uint8_t)L2CA_FlushChannel(p_scb->l2c_cid, L2CAP_FLUSH_CHANS_GET);
2133
2134 if (!list_is_empty(p_scb->a2dp_list)) {
2135 p_buf = (BT_HDR*)list_front(p_scb->a2dp_list);
2136 list_remove(p_scb->a2dp_list, p_buf);
2137 /* use q_info.a2dp data, read the timestamp */
2138 timestamp = *(uint32_t*)(p_buf + 1);
2139 } else {
2140 new_buf = true;
2141 /* A2DP_list empty, call co_data, dup data to other channels */
2142 p_buf = p_scb->p_cos->data(p_scb->cfg.codec_info, ×tamp);
2143
2144 if (p_buf) {
2145 /* use the offset area for the time stamp */
2146 *(uint32_t*)(p_buf + 1) = timestamp;
2147
2148 /* dup the data to other channels */
2149 bta_av_dup_audio_buf(p_scb, p_buf);
2150 }
2151 }
2152
2153 if (p_buf) {
2154 if (p_scb->l2c_bufs < (BTA_AV_QUEUE_DATA_CHK_NUM)) {
2155 /* There's a buffer, just queue it to L2CAP.
2156 * There's no need to increment it here, it is always read from
2157 * L2CAP (see above).
2158 */
2159
2160 /* opt is a bit mask, it could have several options set */
2161 opt = AVDT_DATA_OPT_NONE;
2162 if (p_scb->no_rtp_header) {
2163 opt |= AVDT_DATA_OPT_NO_RTP;
2164 }
2165
2166 //
2167 // Fragment the payload if larger than the MTU.
2168 // NOTE: The fragmentation is RTP-compatibie.
2169 //
2170 size_t extra_fragments_n = 0;
2171 if (p_buf->len > 0) {
2172 extra_fragments_n = (p_buf->len / p_scb->stream_mtu) +
2173 ((p_buf->len % p_scb->stream_mtu) ? 1 : 0) - 1;
2174 }
2175 std::vector<BT_HDR*> extra_fragments;
2176 extra_fragments.reserve(extra_fragments_n);
2177
2178 uint8_t* data_begin = (uint8_t*)(p_buf + 1) + p_buf->offset;
2179 uint8_t* data_end = (uint8_t*)(p_buf + 1) + p_buf->offset + p_buf->len;
2180 while (extra_fragments_n-- > 0) {
2181 data_begin += p_scb->stream_mtu;
2182 size_t fragment_len = data_end - data_begin;
2183 if (fragment_len > p_scb->stream_mtu) fragment_len = p_scb->stream_mtu;
2184
2185 BT_HDR* p_buf2 = (BT_HDR*)osi_malloc(BT_DEFAULT_BUFFER_SIZE);
2186 p_buf2->offset = p_buf->offset;
2187 p_buf2->len = 0;
2188 p_buf2->layer_specific = 0;
2189 uint8_t* packet2 =
2190 (uint8_t*)(p_buf2 + 1) + p_buf2->offset + p_buf2->len;
2191 memcpy(packet2, data_begin, fragment_len);
2192 p_buf2->len += fragment_len;
2193 extra_fragments.push_back(p_buf2);
2194 p_buf->len -= fragment_len;
2195 }
2196
2197 if (!extra_fragments.empty()) {
2198 // Reset the RTP Marker bit for all fragments except the last one
2199 m_pt &= ~AVDT_MARKER_SET;
2200 }
2201 AVDT_WriteReqOpt(p_scb->avdt_handle, p_buf, timestamp, m_pt, opt);
2202 for (size_t i = 0; i < extra_fragments.size(); i++) {
2203 if (i + 1 == extra_fragments.size()) {
2204 // Set the RTP Marker bit for the last fragment
2205 m_pt |= AVDT_MARKER_SET;
2206 }
2207 BT_HDR* p_buf2 = extra_fragments[i];
2208 AVDT_WriteReqOpt(p_scb->avdt_handle, p_buf2, timestamp, m_pt, opt);
2209 }
2210 p_scb->cong = true;
2211 } else {
2212 /* there's a buffer, but L2CAP does not seem to be moving data */
2213 if (new_buf) {
2214 /* just got this buffer from co_data,
2215 * put it in queue */
2216 list_append(p_scb->a2dp_list, p_buf);
2217 } else {
2218 /* just dequeue it from the a2dp_list */
2219 if (list_length(p_scb->a2dp_list) < 3) {
2220 /* put it back to the queue */
2221 list_prepend(p_scb->a2dp_list, p_buf);
2222 } else {
2223 /* too many buffers in a2dp_list, drop it. */
2224 bta_av_co_audio_drop(p_scb->hndl, p_scb->PeerAddress());
2225 osi_free(p_buf);
2226 }
2227 }
2228 }
2229 }
2230 }
2231
2232 /*******************************************************************************
2233 *
2234 * Function bta_av_start_ok
2235 *
2236 * Description Stream started.
2237 *
2238 * Returns void
2239 *
2240 ******************************************************************************/
bta_av_start_ok(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2241 void bta_av_start_ok(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
2242 bool initiator = false;
2243 bool suspend = false;
2244 uint16_t flush_to;
2245 uint8_t new_role = p_scb->role;
2246 BT_HDR hdr;
2247 uint8_t clear_policy = 0;
2248 uint8_t cur_role;
2249 uint8_t local_tsep = p_scb->seps[p_scb->sep_idx].tsep;
2250
2251 LOG_INFO("%s: peer %s bta_handle:0x%x wait:0x%x role:0x%x local_tsep:%d",
2252 __func__, p_scb->PeerAddress().ToString().c_str(), p_scb->hndl,
2253 p_scb->wait, p_scb->role, local_tsep);
2254
2255 p_scb->started = true;
2256
2257 if (local_tsep == AVDT_TSEP_SRC) {
2258 // The RTP Header marker bit for the A2DP Source encoder
2259 A2dpCodecConfig* codec_config =
2260 bta_av_get_a2dp_peer_current_codec(p_scb->PeerAddress());
2261 CHECK(codec_config != nullptr);
2262 p_scb->use_rtp_header_marker_bit = codec_config->useRtpHeaderMarkerBit();
2263 }
2264
2265 if (p_scb->sco_suspend) {
2266 p_scb->sco_suspend = false;
2267 }
2268
2269 if (new_role & BTA_AV_ROLE_START_INT) initiator = true;
2270
2271 /* for A2DP SINK we do not send get_caps */
2272 if ((p_scb->avdt_handle == p_scb->seps[p_scb->sep_idx].av_handle) &&
2273 (local_tsep == AVDT_TSEP_SNK)) {
2274 p_scb->wait &= ~(BTA_AV_WAIT_ACP_CAPS_ON);
2275 APPL_TRACE_DEBUG("%s: local SEP type is SNK new wait is 0x%x", __func__,
2276 p_scb->wait);
2277 }
2278 if (p_scb->wait & BTA_AV_WAIT_ROLE_SW_FAILED) {
2279 /* role switch has failed */
2280 APPL_TRACE_ERROR(
2281 "%s: peer %s role switch failed: bta_handle:0x%x wait:0x%x, role:0x%x",
2282 __func__, p_scb->PeerAddress().ToString().c_str(), p_scb->hndl,
2283 p_scb->wait, p_scb->role);
2284 p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_FAILED;
2285 p_data = (tBTA_AV_DATA*)&hdr;
2286 hdr.offset = BTA_AV_RS_FAIL;
2287 }
2288 APPL_TRACE_DEBUG("%s: peer %s wait:0x%x use_rtp_header_marker_bit:%s",
2289 __func__, p_scb->PeerAddress().ToString().c_str(),
2290 p_scb->wait,
2291 (p_scb->use_rtp_header_marker_bit) ? "true" : "false");
2292
2293 if (p_data && (p_data->hdr.offset != BTA_AV_RS_NONE)) {
2294 p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
2295 if (p_data->hdr.offset == BTA_AV_RS_FAIL) {
2296 bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->PeerAddress());
2297 tBTA_AV_START start;
2298 start.chnl = p_scb->chnl;
2299 start.status = BTA_AV_FAIL_ROLE;
2300 start.hndl = p_scb->hndl;
2301 start.initiator = initiator;
2302 tBTA_AV bta_av_data;
2303 bta_av_data.start = start;
2304 (*bta_av_cb.p_cback)(BTA_AV_START_EVT, &bta_av_data);
2305 return;
2306 }
2307 }
2308
2309 if (!bta_av_link_role_ok(p_scb, A2DP_SET_ONE_BIT))
2310 p_scb->q_tag = BTA_AV_Q_TAG_START;
2311 else {
2312 /* The wait flag may be set here while we are already master on the link */
2313 /* this could happen if a role switch complete event occurred during
2314 * reconfig */
2315 /* if we are now master on the link, there is no need to wait for the role
2316 * switch, */
2317 /* complete anymore so we can clear the wait for role switch flag */
2318 p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
2319 }
2320
2321 if (p_scb->wait &
2322 (BTA_AV_WAIT_ROLE_SW_RES_OPEN | BTA_AV_WAIT_ROLE_SW_RES_START)) {
2323 p_scb->wait |= BTA_AV_WAIT_ROLE_SW_STARTED;
2324 p_scb->q_tag = BTA_AV_Q_TAG_START;
2325 }
2326
2327 if (p_scb->wait) {
2328 APPL_TRACE_ERROR("%s: peer %s wait:0x%x q_tag:%d not started", __func__,
2329 p_scb->PeerAddress().ToString().c_str(), p_scb->wait,
2330 p_scb->q_tag);
2331 /* Clear first bit of p_scb->wait and not to return from this point else
2332 * HAL layer gets blocked. And if there is delay in Get Capability response
2333 * as
2334 * first bit of p_scb->wait is cleared hence it ensures bt_av_start_ok is
2335 * not called
2336 * again from bta_av_save_caps.
2337 */
2338 p_scb->wait &= ~BTA_AV_WAIT_ACP_CAPS_ON;
2339 }
2340
2341 /* tell role manager to check M/S role */
2342 bta_sys_conn_open(BTA_ID_AV, p_scb->app_id, p_scb->PeerAddress());
2343
2344 bta_sys_busy(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->PeerAddress());
2345
2346 if (p_scb->media_type == AVDT_MEDIA_TYPE_AUDIO) {
2347 /* in normal logic, conns should be bta_av_cb.audio_count - 1,
2348 * However, bta_av_stream_chg is not called to increase
2349 * bta_av_cb.audio_count yet.
2350 * If the code were to be re-arranged for some reasons, this number may need
2351 * to be changed
2352 */
2353 p_scb->co_started = bta_av_cb.audio_open_cnt;
2354 flush_to = p_bta_av_cfg->p_audio_flush_to[p_scb->co_started - 1];
2355 } else {
2356 flush_to = 0;
2357 }
2358 L2CA_SetFlushTimeout(p_scb->PeerAddress(), flush_to);
2359
2360 /* clear the congestion flag */
2361 p_scb->cong = false;
2362
2363 if (new_role & BTA_AV_ROLE_START_INT) {
2364 new_role &= ~BTA_AV_ROLE_START_INT;
2365 } else if ((new_role & BTA_AV_ROLE_AD_ACP) &&
2366 (new_role & BTA_AV_ROLE_SUSPEND_OPT)) {
2367 suspend = true;
2368 }
2369
2370 if (!suspend) {
2371 p_scb->q_tag = BTA_AV_Q_TAG_STREAM;
2372 bta_av_stream_chg(p_scb, true);
2373 }
2374
2375 {
2376 /* If sink starts stream, disable sniff mode here */
2377 if (!initiator) {
2378 /* If souce is the master role, disable role switch during streaming.
2379 * Otherwise allow role switch, if source is slave.
2380 * Because it would not hurt source, if the peer device wants source to be
2381 * master */
2382 if ((BTM_GetRole(p_scb->PeerAddress(), &cur_role) == BTM_SUCCESS) &&
2383 (cur_role == BTM_ROLE_MASTER)) {
2384 clear_policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
2385 }
2386
2387 bta_sys_clear_policy(BTA_ID_AV, clear_policy, p_scb->PeerAddress());
2388 }
2389
2390 p_scb->role = new_role;
2391 p_scb->role &= ~BTA_AV_ROLE_AD_ACP;
2392 p_scb->role &= ~BTA_AV_ROLE_SUSPEND_OPT;
2393
2394 p_scb->no_rtp_header = false;
2395 p_scb->p_cos->start(p_scb->hndl, p_scb->PeerAddress(),
2396 p_scb->cfg.codec_info, &p_scb->no_rtp_header);
2397 p_scb->co_started = true;
2398
2399 APPL_TRACE_DEBUG("%s: peer %s suspending: %d, role:0x%x, init %d", __func__,
2400 p_scb->PeerAddress().ToString().c_str(), suspend,
2401 p_scb->role, initiator);
2402
2403 tBTA_AV_START start;
2404 start.suspending = suspend;
2405 start.initiator = initiator;
2406 start.chnl = p_scb->chnl;
2407 start.status = BTA_AV_SUCCESS;
2408 start.hndl = p_scb->hndl;
2409 tBTA_AV bta_av_data;
2410 bta_av_data.start = start;
2411 (*bta_av_cb.p_cback)(BTA_AV_START_EVT, &bta_av_data);
2412
2413 if (suspend) {
2414 tBTA_AV_API_STOP stop;
2415 p_scb->role |= BTA_AV_ROLE_SUSPEND;
2416 p_scb->cong = true; /* do not allow the media data to go through */
2417 /* do not duplicate the media packets to this channel */
2418 p_scb->p_cos->stop(p_scb->hndl, p_scb->PeerAddress());
2419 p_scb->co_started = false;
2420 stop.flush = false;
2421 stop.suspend = true;
2422 stop.reconfig_stop = false;
2423 bta_av_ssm_execute(p_scb, BTA_AV_AP_STOP_EVT, (tBTA_AV_DATA*)&stop);
2424 }
2425 }
2426 }
2427
2428 /*******************************************************************************
2429 *
2430 * Function bta_av_start_failed
2431 *
2432 * Description Stream start failed.
2433 *
2434 * Returns void
2435 *
2436 ******************************************************************************/
bta_av_start_failed(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)2437 void bta_av_start_failed(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
2438 uint8_t set_policy = (HCI_ENABLE_SNIFF_MODE | HCI_ENABLE_MASTER_SLAVE_SWITCH);
2439
2440 APPL_TRACE_ERROR(
2441 "%s: peer %s bta_handle:0x%x audio_open_cnt:%d started:%s co_started:%d",
2442 __func__, p_scb->PeerAddress().ToString().c_str(), p_scb->hndl,
2443 bta_av_cb.audio_open_cnt, logbool(p_scb->started).c_str(),
2444 p_scb->co_started);
2445
2446 if (!p_scb->started && !p_scb->co_started) {
2447 bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->PeerAddress());
2448 notify_start_failed(p_scb);
2449 }
2450
2451 bta_sys_set_policy(BTA_ID_AV, set_policy, p_scb->PeerAddress());
2452 p_scb->sco_suspend = false;
2453 }
2454
2455 /*******************************************************************************
2456 *
2457 * Function bta_av_str_closed
2458 *
2459 * Description Stream closed.
2460 *
2461 * Returns void
2462 *
2463 ******************************************************************************/
bta_av_str_closed(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2464 void bta_av_str_closed(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
2465 tBTA_AV data;
2466 tBTA_AV_EVT event;
2467 uint8_t set_policy = HCI_ENABLE_SNIFF_MODE;
2468
2469 APPL_TRACE_WARNING(
2470 "%s: peer %s bta_handle:0x%x open_status:%d chnl:%d co_started:%d",
2471 __func__, p_scb->PeerAddress().ToString().c_str(), p_scb->hndl,
2472 p_scb->open_status, p_scb->chnl, p_scb->co_started);
2473
2474 if ((bta_av_cb.features & BTA_AV_FEAT_MASTER) == 0 ||
2475 bta_av_cb.audio_open_cnt == 1) {
2476 set_policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
2477 }
2478 bta_sys_set_policy(BTA_ID_AV, set_policy, p_scb->PeerAddress());
2479 if (bta_av_cb.audio_open_cnt <= 1) {
2480 /* last connection - restore the allow switch flag */
2481 L2CA_SetDesireRole(L2CAP_ROLE_ALLOW_SWITCH);
2482 }
2483
2484 if (p_scb->open_status != BTA_AV_SUCCESS) {
2485 /* must be failure when opening the stream */
2486 data.open.bd_addr = p_scb->PeerAddress();
2487 data.open.status = p_scb->open_status;
2488 data.open.chnl = p_scb->chnl;
2489 data.open.hndl = p_scb->hndl;
2490
2491 if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SRC)
2492 data.open.sep = AVDT_TSEP_SNK;
2493 else if (p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SNK)
2494 data.open.sep = AVDT_TSEP_SRC;
2495
2496 event = BTA_AV_OPEN_EVT;
2497 p_scb->open_status = BTA_AV_SUCCESS;
2498
2499 bta_sys_conn_close(BTA_ID_AV, p_scb->app_id, p_scb->PeerAddress());
2500 bta_av_cleanup(p_scb, p_data);
2501 (*bta_av_cb.p_cback)(event, &data);
2502 } else {
2503 /* do stop if we were started */
2504 if (p_scb->co_started) {
2505 bta_av_str_stopped(p_scb, NULL);
2506 }
2507
2508 {
2509 p_scb->p_cos->close(p_scb->hndl, p_scb->PeerAddress());
2510 data.close.chnl = p_scb->chnl;
2511 data.close.hndl = p_scb->hndl;
2512 event = BTA_AV_CLOSE_EVT;
2513
2514 bta_sys_conn_close(BTA_ID_AV, p_scb->app_id, p_scb->PeerAddress());
2515 bta_av_cleanup(p_scb, p_data);
2516 (*bta_av_cb.p_cback)(event, &data);
2517 }
2518 }
2519 }
2520
2521 /*******************************************************************************
2522 *
2523 * Function bta_av_clr_cong
2524 *
2525 * Description Clear stream congestion flag.
2526 *
2527 * Returns void
2528 *
2529 ******************************************************************************/
bta_av_clr_cong(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)2530 void bta_av_clr_cong(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
2531 APPL_TRACE_DEBUG("%s", __func__);
2532 if (p_scb->co_started) {
2533 p_scb->cong = false;
2534 }
2535 }
2536
2537 /*******************************************************************************
2538 *
2539 * Function bta_av_suspend_cfm
2540 *
2541 * Description process the suspend response
2542 *
2543 * Returns void
2544 *
2545 ******************************************************************************/
bta_av_suspend_cfm(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2546 void bta_av_suspend_cfm(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
2547 tBTA_AV_SUSPEND suspend_rsp;
2548 uint8_t err_code = p_data->str_msg.msg.hdr.err_code;
2549 uint8_t set_policy = HCI_ENABLE_SNIFF_MODE;
2550
2551 APPL_TRACE_DEBUG("%s: peer %s bta_handle:0x%x audio_open_cnt:%d err_code:%d",
2552 __func__, p_scb->PeerAddress().ToString().c_str(),
2553 p_scb->hndl, bta_av_cb.audio_open_cnt, err_code);
2554
2555 if (!p_scb->started) {
2556 /* handle the condition where there is a collision of SUSPEND req from
2557 *either side
2558 ** Second SUSPEND req could be rejected. Do not treat this as a failure
2559 */
2560 APPL_TRACE_WARNING("%s: already suspended, ignore, err_code %d", __func__,
2561 err_code);
2562 return;
2563 }
2564
2565 suspend_rsp.status = BTA_AV_SUCCESS;
2566 if (err_code && (err_code != AVDT_ERR_BAD_STATE)) {
2567 /* Disable suspend feature only with explicit rejection(not with timeout) */
2568 if (err_code != AVDT_ERR_TIMEOUT) {
2569 p_scb->suspend_sup = false;
2570 }
2571 suspend_rsp.status = BTA_AV_FAIL;
2572
2573 APPL_TRACE_ERROR("%s: suspend failed, closing connection", __func__);
2574
2575 /* SUSPEND failed. Close connection. */
2576 bta_av_ssm_execute(p_scb, BTA_AV_API_CLOSE_EVT, NULL);
2577 } else {
2578 /* only set started to false when suspend is successful */
2579 p_scb->started = false;
2580 }
2581
2582 if (p_scb->role & BTA_AV_ROLE_SUSPEND) {
2583 p_scb->role &= ~BTA_AV_ROLE_SUSPEND;
2584 p_scb->cong = false;
2585 }
2586
2587 bta_sys_idle(BTA_ID_AV, bta_av_cb.audio_open_cnt, p_scb->PeerAddress());
2588 if ((bta_av_cb.features & BTA_AV_FEAT_MASTER) == 0 ||
2589 bta_av_cb.audio_open_cnt == 1) {
2590 set_policy |= HCI_ENABLE_MASTER_SLAVE_SWITCH;
2591 }
2592 bta_sys_set_policy(BTA_ID_AV, set_policy, p_scb->PeerAddress());
2593
2594 /* in case that we received suspend_ind, we may need to call co_stop here */
2595 if (p_scb->co_started) {
2596 if (p_scb->offload_started) {
2597 bta_av_vendor_offload_stop();
2598 p_scb->offload_started = false;
2599 }
2600 bta_av_stream_chg(p_scb, false);
2601
2602 {
2603 p_scb->co_started = false;
2604 p_scb->p_cos->stop(p_scb->hndl, p_scb->PeerAddress());
2605 }
2606 L2CA_SetFlushTimeout(p_scb->PeerAddress(), L2CAP_DEFAULT_FLUSH_TO);
2607 }
2608
2609 {
2610 suspend_rsp.chnl = p_scb->chnl;
2611 suspend_rsp.hndl = p_scb->hndl;
2612 suspend_rsp.initiator = p_data->str_msg.initiator;
2613 tBTA_AV bta_av_data;
2614 bta_av_data.suspend = suspend_rsp;
2615 (*bta_av_cb.p_cback)(BTA_AV_SUSPEND_EVT, &bta_av_data);
2616 }
2617 }
2618
2619 /*******************************************************************************
2620 *
2621 * Function bta_av_rcfg_str_ok
2622 *
2623 * Description report reconfigure successful
2624 *
2625 * Returns void
2626 *
2627 ******************************************************************************/
bta_av_rcfg_str_ok(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2628 void bta_av_rcfg_str_ok(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
2629 p_scb->l2c_cid = AVDT_GetL2CapChannel(p_scb->avdt_handle);
2630 APPL_TRACE_DEBUG("%s: peer %s bta_handle:0x%x l2c_cid:%d", __func__,
2631 p_scb->PeerAddress().ToString().c_str(), p_scb->hndl,
2632 p_scb->l2c_cid);
2633
2634 if (p_data != NULL) {
2635 // p_data could be NULL if the reconfig was triggered by the local device
2636 p_scb->stream_mtu =
2637 p_data->str_msg.msg.open_ind.peer_mtu - AVDT_MEDIA_HDR_SIZE;
2638 APPL_TRACE_DEBUG("%s: l2c_cid: 0x%x stream_mtu: %d", __func__,
2639 p_scb->l2c_cid, p_scb->stream_mtu);
2640 p_scb->p_cos->update_mtu(p_scb->hndl, p_scb->PeerAddress(),
2641 p_scb->stream_mtu);
2642 }
2643
2644 /* rc listen */
2645 bta_av_st_rc_timer(p_scb, NULL);
2646
2647 /* No need to keep the role bits once reconfig is done. */
2648 p_scb->role &= ~BTA_AV_ROLE_AD_ACP;
2649 p_scb->role &= ~BTA_AV_ROLE_SUSPEND_OPT;
2650 p_scb->role &= ~BTA_AV_ROLE_START_INT;
2651
2652 {
2653 /* reconfigure success */
2654 tBTA_AV_RECONFIG reconfig;
2655 reconfig.status = BTA_AV_SUCCESS;
2656 reconfig.chnl = p_scb->chnl;
2657 reconfig.hndl = p_scb->hndl;
2658 tBTA_AV bta_av_data;
2659 bta_av_data.reconfig = reconfig;
2660 (*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, &bta_av_data);
2661 }
2662 }
2663
2664 /*******************************************************************************
2665 *
2666 * Function bta_av_rcfg_failed
2667 *
2668 * Description process reconfigure failed
2669 *
2670 * Returns void
2671 *
2672 ******************************************************************************/
bta_av_rcfg_failed(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2673 void bta_av_rcfg_failed(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
2674 APPL_TRACE_ERROR("%s: num_recfg=%d conn_lcb=0x%x peer_addr=%s", __func__,
2675 p_scb->num_recfg, bta_av_cb.conn_lcb,
2676 p_scb->PeerAddress().ToString().c_str());
2677
2678 if (p_scb->num_recfg > BTA_AV_RECONFIG_RETRY) {
2679 bta_av_cco_close(p_scb, p_data);
2680 /* report failure */
2681 tBTA_AV_RECONFIG reconfig;
2682 reconfig.status = BTA_AV_FAIL_STREAM;
2683 reconfig.chnl = p_scb->chnl;
2684 reconfig.hndl = p_scb->hndl;
2685 tBTA_AV bta_av_data;
2686 bta_av_data.reconfig = reconfig;
2687 (*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, &bta_av_data);
2688 /* go to closing state */
2689 bta_av_ssm_execute(p_scb, BTA_AV_API_CLOSE_EVT, NULL);
2690 } else {
2691 /* open failed. try again */
2692 p_scb->num_recfg++;
2693 // conn_lcb is the index bitmask of all used LCBs, and since LCB and SCB use
2694 // the same index, it should be safe to use SCB index here.
2695 if ((bta_av_cb.conn_lcb & (1 << p_scb->hdi)) != 0) {
2696 AVDT_DisconnectReq(p_scb->PeerAddress(), &bta_av_proc_stream_evt);
2697 } else {
2698 APPL_TRACE_WARNING("%s: conn_lcb=0x%x bta_handle=0x%x (hdi=%u) no link",
2699 __func__, bta_av_cb.conn_lcb, p_scb->hndl, p_scb->hdi);
2700 bta_av_connect_req(p_scb, NULL);
2701 }
2702 }
2703 }
2704
2705 /*******************************************************************************
2706 *
2707 * Function bta_av_rcfg_connect
2708 *
2709 * Description stream closed. reconnect the stream
2710 *
2711 * Returns void
2712 *
2713 ******************************************************************************/
bta_av_rcfg_connect(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)2714 void bta_av_rcfg_connect(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
2715 APPL_TRACE_DEBUG("%s", __func__);
2716
2717 p_scb->cong = false;
2718 p_scb->num_recfg++;
2719 APPL_TRACE_DEBUG("%s: num_recfg: %d", __func__, p_scb->num_recfg);
2720 if (p_scb->num_recfg > BTA_AV_RECONFIG_RETRY) {
2721 /* let bta_av_rcfg_failed report fail */
2722 bta_av_rcfg_failed(p_scb, NULL);
2723 } else {
2724 AVDT_ConnectReq(p_scb->PeerAddress(), p_scb->hdi, p_scb->sec_mask,
2725 &bta_av_proc_stream_evt);
2726 }
2727 }
2728
2729 /*******************************************************************************
2730 *
2731 * Function bta_av_rcfg_discntd
2732 *
2733 * Description AVDT disconnected. reconnect the stream
2734 *
2735 * Returns void
2736 *
2737 ******************************************************************************/
bta_av_rcfg_discntd(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)2738 void bta_av_rcfg_discntd(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
2739 APPL_TRACE_ERROR("%s: num_recfg=%d conn_lcb=0x%x peer_addr=%s", __func__,
2740 p_scb->num_recfg, bta_av_cb.conn_lcb,
2741 p_scb->PeerAddress().ToString().c_str());
2742
2743 p_scb->num_recfg++;
2744 if (p_scb->num_recfg > BTA_AV_RECONFIG_RETRY) {
2745 /* report failure */
2746 tBTA_AV_RECONFIG reconfig;
2747 reconfig.status = BTA_AV_FAIL_STREAM;
2748 reconfig.chnl = p_scb->chnl;
2749 reconfig.hndl = p_scb->hndl;
2750 tBTA_AV bta_av_data;
2751 bta_av_data.reconfig = reconfig;
2752 (*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, &bta_av_data);
2753 /* report close event & go to init state */
2754 bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, NULL);
2755 } else {
2756 AVDT_ConnectReq(p_scb->PeerAddress(), p_scb->hdi, p_scb->sec_mask,
2757 &bta_av_proc_stream_evt);
2758 }
2759 }
2760
2761 /*******************************************************************************
2762 *
2763 * Function bta_av_suspend_cont
2764 *
2765 * Description received the suspend response.
2766 * continue to reconfigure the stream
2767 *
2768 * Returns void
2769 *
2770 ******************************************************************************/
bta_av_suspend_cont(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2771 void bta_av_suspend_cont(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
2772 uint8_t err_code = p_data->str_msg.msg.hdr.err_code;
2773
2774 APPL_TRACE_DEBUG("%s: err_code=%d", __func__, err_code);
2775
2776 p_scb->started = false;
2777 p_scb->cong = false;
2778 if (err_code) {
2779 if (AVDT_ERR_CONNECT == err_code) {
2780 /* report failure */
2781 tBTA_AV_RECONFIG reconfig;
2782 reconfig.status = BTA_AV_FAIL;
2783 tBTA_AV bta_av_data;
2784 bta_av_data.reconfig = reconfig;
2785 (*bta_av_cb.p_cback)(BTA_AV_RECONFIG_EVT, &bta_av_data);
2786 APPL_TRACE_ERROR("%s: BTA_AV_STR_DISC_FAIL_EVT: peer_addr=%s", __func__,
2787 p_scb->PeerAddress().ToString().c_str());
2788 bta_av_ssm_execute(p_scb, BTA_AV_STR_DISC_FAIL_EVT, NULL);
2789 } else {
2790 APPL_TRACE_ERROR("%s: suspend rejected, try close", __func__);
2791 /* Disable suspend feature only with explicit rejection(not with timeout)
2792 */
2793 if (err_code != AVDT_ERR_TIMEOUT) {
2794 p_scb->suspend_sup = false;
2795 }
2796 /* drop the buffers queued in L2CAP */
2797 L2CA_FlushChannel(p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
2798
2799 AVDT_CloseReq(p_scb->avdt_handle);
2800 }
2801 } else {
2802 APPL_TRACE_DEBUG("%s: calling AVDT_ReconfigReq", __func__);
2803 /* reconfig the stream */
2804
2805 APPL_TRACE_DEBUG("%s: codec: %s", __func__,
2806 A2DP_CodecInfoString(p_scb->cfg.codec_info).c_str());
2807 AVDT_ReconfigReq(p_scb->avdt_handle, &p_scb->cfg);
2808 p_scb->cfg.psc_mask = p_scb->cur_psc_mask;
2809 }
2810 }
2811
2812 /*******************************************************************************
2813 *
2814 * Function bta_av_rcfg_cfm
2815 *
2816 * Description if reconfigure is successful, report the event
2817 * otherwise, close the stream.
2818 *
2819 * Returns void
2820 *
2821 ******************************************************************************/
bta_av_rcfg_cfm(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2822 void bta_av_rcfg_cfm(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
2823 uint8_t err_code = p_data->str_msg.msg.hdr.err_code;
2824
2825 APPL_TRACE_DEBUG("%s: err_code = %d", __func__, err_code);
2826
2827 // Disable AVDTP RECONFIGURE for blacklisted devices
2828 bool disable_avdtp_reconfigure = false;
2829 {
2830 char remote_name[BTM_MAX_REM_BD_NAME_LEN] = "";
2831 if (btif_storage_get_stored_remote_name(p_scb->PeerAddress(),
2832 remote_name)) {
2833 if (interop_match_name(INTEROP_DISABLE_AVDTP_RECONFIGURE, remote_name) ||
2834 interop_match_addr(INTEROP_DISABLE_AVDTP_RECONFIGURE,
2835 (const RawAddress*)&p_scb->PeerAddress())) {
2836 LOG_INFO(
2837 "%s: disable AVDTP RECONFIGURE: interop matched "
2838 "name %s address %s",
2839 __func__, remote_name, p_scb->PeerAddress().ToString().c_str());
2840 disable_avdtp_reconfigure = true;
2841 }
2842 }
2843 }
2844
2845 if ((err_code != 0) || disable_avdtp_reconfigure) {
2846 APPL_TRACE_ERROR("%s: reconfig rejected, try close", __func__);
2847 /* Disable reconfiguration feature only with explicit rejection(not with
2848 * timeout) */
2849 if ((err_code != AVDT_ERR_TIMEOUT) || disable_avdtp_reconfigure) {
2850 p_scb->recfg_sup = false;
2851 }
2852 /* started flag is false when reconfigure command is sent */
2853 /* drop the buffers queued in L2CAP */
2854 L2CA_FlushChannel(p_scb->l2c_cid, L2CAP_FLUSH_CHANS_ALL);
2855 AVDT_CloseReq(p_scb->avdt_handle);
2856 } else {
2857 /* update the codec info after rcfg cfm */
2858 APPL_TRACE_DEBUG(
2859 "%s: updating from codec %s to codec %s", __func__,
2860 A2DP_CodecName(p_scb->cfg.codec_info),
2861 A2DP_CodecName(p_data->str_msg.msg.reconfig_cfm.p_cfg->codec_info));
2862 memcpy(p_scb->cfg.codec_info,
2863 p_data->str_msg.msg.reconfig_cfm.p_cfg->codec_info, AVDT_CODEC_SIZE);
2864 /* take the SSM back to OPEN state */
2865 bta_av_ssm_execute(p_scb, BTA_AV_STR_OPEN_OK_EVT, NULL);
2866 }
2867 }
2868
2869 /*******************************************************************************
2870 *
2871 * Function bta_av_rcfg_open
2872 *
2873 * Description AVDT is connected. open the stream with the new
2874 * configuration
2875 *
2876 * Returns void
2877 *
2878 ******************************************************************************/
bta_av_rcfg_open(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)2879 void bta_av_rcfg_open(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
2880 APPL_TRACE_DEBUG("%s: peer %s bta_handle:0x%x num_disc_snks:%d", __func__,
2881 p_scb->PeerAddress().ToString().c_str(), p_scb->hndl,
2882 p_scb->num_disc_snks);
2883
2884 if (p_scb->num_disc_snks == 0) {
2885 /* Need to update call-out module so that it will be ready for discover */
2886 p_scb->p_cos->stop(p_scb->hndl, p_scb->PeerAddress());
2887
2888 /* send avdtp discover request */
2889 AVDT_DiscoverReq(p_scb->PeerAddress(), p_scb->hdi, p_scb->sep_info,
2890 BTA_AV_NUM_SEPS, &bta_av_proc_stream_evt);
2891 } else {
2892 APPL_TRACE_DEBUG("%s: calling AVDT_OpenReq()", __func__);
2893 APPL_TRACE_DEBUG("%s: codec: %s", __func__,
2894 A2DP_CodecInfoString(p_scb->cfg.codec_info).c_str());
2895
2896 /* we may choose to use a different SEP at reconfig.
2897 * adjust the sep_idx now */
2898 bta_av_adjust_seps_idx(p_scb, bta_av_get_scb_handle(p_scb, AVDT_TSEP_SRC));
2899 LOG_DEBUG("%s: sep_idx=%d avdt_handle=%d bta_handle=0x%x", __func__,
2900 p_scb->sep_idx, p_scb->avdt_handle, p_scb->hndl);
2901
2902 /* open the stream with the new config */
2903 p_scb->sep_info_idx = p_scb->rcfg_idx;
2904 AVDT_OpenReq(p_scb->avdt_handle, p_scb->PeerAddress(), p_scb->hdi,
2905 p_scb->sep_info[p_scb->sep_info_idx].seid, &p_scb->cfg);
2906 }
2907 }
2908
2909 /*******************************************************************************
2910 *
2911 * Function bta_av_security_rej
2912 *
2913 * Description Send an AVDTP security reject.
2914 *
2915 * Returns void
2916 *
2917 ******************************************************************************/
bta_av_security_rej(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)2918 void bta_av_security_rej(tBTA_AV_SCB* p_scb, UNUSED_ATTR tBTA_AV_DATA* p_data) {
2919 AVDT_SecurityRsp(p_scb->avdt_handle, p_scb->avdt_label, AVDT_ERR_BAD_STATE,
2920 NULL, 0);
2921 }
2922
2923 /*******************************************************************************
2924 *
2925 * Function bta_av_chk_2nd_start
2926 *
2927 * Description check if this is 2nd stream and if it needs to be started.
2928 * This function needs to be kept very similar to
2929 * bta_av_chk_start
2930 *
2931 * Returns void
2932 *
2933 ******************************************************************************/
bta_av_chk_2nd_start(tBTA_AV_SCB * p_scb,UNUSED_ATTR tBTA_AV_DATA * p_data)2934 void bta_av_chk_2nd_start(tBTA_AV_SCB* p_scb,
2935 UNUSED_ATTR tBTA_AV_DATA* p_data) {
2936 LOG_INFO(
2937 "%s: peer %s channel:%d bta_av_cb.audio_open_cnt:%d role:0x%x "
2938 "features:0x%x",
2939 __func__, p_scb->PeerAddress().ToString().c_str(), p_scb->chnl,
2940 bta_av_cb.audio_open_cnt, p_scb->role, bta_av_cb.features);
2941
2942 if ((p_scb->chnl == BTA_AV_CHNL_AUDIO) && (bta_av_cb.audio_open_cnt >= 2) &&
2943 (((p_scb->role & BTA_AV_ROLE_AD_ACP) == 0) || // Outgoing connection or
2944 (bta_av_cb.features & BTA_AV_FEAT_ACP_START))) { // Auto-starting option
2945 // More than one audio channel is connected.
2946 if (!(p_scb->role & BTA_AV_ROLE_SUSPEND_OPT)) {
2947 // This channel does not need to be reconfigured.
2948 // If there is other channel streaming, start the stream now.
2949 bool new_started = false;
2950 for (int i = 0; i < BTA_AV_NUM_STRS; i++) {
2951 tBTA_AV_SCB* p_scbi = bta_av_cb.p_scb[i];
2952 if (p_scb == p_scbi) {
2953 continue;
2954 }
2955 if (p_scbi && p_scbi->chnl == BTA_AV_CHNL_AUDIO && p_scbi->co_started) {
2956 if (!new_started) {
2957 // Start the new stream
2958 new_started = true;
2959 LOG_INFO(
2960 "%s: starting new stream for peer %s because peer %s "
2961 "already started",
2962 __func__, p_scb->PeerAddress().ToString().c_str(),
2963 p_scbi->PeerAddress().ToString().c_str());
2964 bta_av_ssm_execute(p_scb, BTA_AV_AP_START_EVT, NULL);
2965 }
2966 // May need to update the flush timeout of this already started stream
2967 if (p_scbi->co_started != bta_av_cb.audio_open_cnt) {
2968 p_scbi->co_started = bta_av_cb.audio_open_cnt;
2969 L2CA_SetFlushTimeout(
2970 p_scbi->PeerAddress(),
2971 p_bta_av_cfg->p_audio_flush_to[p_scbi->co_started - 1]);
2972 }
2973 }
2974 }
2975 }
2976 }
2977 }
2978
2979 /*******************************************************************************
2980 *
2981 * Function bta_av_open_rc
2982 *
2983 * Description Send a message to main SM to open RC channel.
2984 *
2985 * Returns void
2986 *
2987 ******************************************************************************/
bta_av_open_rc(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)2988 void bta_av_open_rc(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
2989 APPL_TRACE_DEBUG("%s: use_rc: %d, wait: 0x%x role: 0x%x", __func__,
2990 p_scb->use_rc, p_scb->wait, p_scb->role);
2991 if ((p_scb->wait & BTA_AV_WAIT_ROLE_SW_BITS) &&
2992 (p_scb->q_tag == BTA_AV_Q_TAG_START)) {
2993 /* waiting for role switch for some reason & the timer expires */
2994 if (!bta_av_link_role_ok(p_scb, A2DP_SET_ONE_BIT)) {
2995 APPL_TRACE_ERROR(
2996 "%s: failed to start streaming for role management reasons!!",
2997 __func__);
2998 alarm_cancel(p_scb->avrc_ct_timer);
2999
3000 tBTA_AV_START start;
3001 start.chnl = p_scb->chnl;
3002 start.status = BTA_AV_FAIL_ROLE;
3003 start.initiator = true;
3004 start.hndl = p_scb->hndl;
3005 p_scb->wait &= ~BTA_AV_WAIT_ROLE_SW_BITS;
3006 bta_av_cb.rs_idx = 0;
3007 tBTA_AV bta_av_data;
3008 bta_av_data.start = start;
3009 (*bta_av_cb.p_cback)(BTA_AV_START_EVT, &bta_av_data);
3010 } else {
3011 /* role switch is done. continue to start streaming */
3012 bta_av_cb.rs_idx = 0;
3013 p_data->hdr.offset = BTA_AV_RS_OK;
3014 bta_av_start_ok(p_scb, p_data);
3015 }
3016 return;
3017 }
3018
3019 if (p_scb->use_rc || (p_scb->role & BTA_AV_ROLE_AD_ACP)) {
3020 if (bta_av_cb.disc) {
3021 /* AVRC discover db is in use */
3022 if (p_scb->rc_handle == BTA_AV_RC_HANDLE_NONE) {
3023 /* AVRC channel is not connected. delay a little bit */
3024 if ((p_scb->wait & BTA_AV_WAIT_ROLE_SW_BITS) == 0) {
3025 bta_sys_start_timer(p_scb->avrc_ct_timer, BTA_AV_RC_DISC_TIME_VAL,
3026 BTA_AV_AVRC_TIMER_EVT, p_scb->hndl);
3027 } else {
3028 p_scb->wait |= BTA_AV_WAIT_CHECK_RC;
3029 }
3030 }
3031 } else {
3032 /* use main SM for AVRC SDP activities */
3033 if (is_new_avrcp_enabled()) {
3034 APPL_TRACE_WARNING("%s: Using the new AVRCP Profile", __func__);
3035 bluetooth::avrcp::AvrcpService::Get()->ConnectDevice(
3036 p_scb->PeerAddress());
3037 } else {
3038 bta_av_rc_disc((uint8_t)(p_scb->hdi + 1));
3039 }
3040 }
3041 } else {
3042 if (BTA_AV_RC_HANDLE_NONE != p_scb->rc_handle) {
3043 /* the open API said that this handle does not want a RC connection.
3044 * disconnect it now */
3045 AVRC_Close(p_scb->rc_handle);
3046 }
3047 }
3048 }
3049
3050 /*******************************************************************************
3051 *
3052 * Function bta_av_open_at_inc
3053 *
3054 * Description This function is called if API open is called by application
3055 * while state-machine is at incoming state.
3056 *
3057 * Returns void
3058 *
3059 ******************************************************************************/
bta_av_open_at_inc(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)3060 void bta_av_open_at_inc(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
3061 memcpy(&(p_scb->open_api), &(p_data->api_open), sizeof(tBTA_AV_API_OPEN));
3062
3063 APPL_TRACE_DEBUG("%s: peer %s coll_mask=0x%02x", __func__,
3064 p_scb->PeerAddress().ToString().c_str(), p_scb->coll_mask);
3065
3066 if (p_scb->coll_mask & BTA_AV_COLL_INC_TMR) {
3067 p_scb->coll_mask |= BTA_AV_COLL_API_CALLED;
3068
3069 /* API open will be handled at timeout if SNK did not start signalling. */
3070 /* API open will be ignored if SNK starts signalling. */
3071 } else {
3072 /* SNK did not start signalling, API was called N seconds timeout. */
3073 /* We need to switch to INIT state and start opening connection. */
3074 p_scb->coll_mask = 0;
3075 bta_av_set_scb_sst_init(p_scb);
3076
3077 tBTA_AV_API_OPEN* p_buf =
3078 (tBTA_AV_API_OPEN*)osi_malloc(sizeof(tBTA_AV_API_OPEN));
3079 memcpy(p_buf, &(p_scb->open_api), sizeof(tBTA_AV_API_OPEN));
3080 bta_sys_sendmsg(p_buf);
3081 }
3082 }
3083
offload_vendor_callback(tBTM_VSC_CMPL * param)3084 void offload_vendor_callback(tBTM_VSC_CMPL* param) {
3085 tBTA_AV value{0};
3086 uint8_t sub_opcode = 0;
3087 if (param->param_len) {
3088 APPL_TRACE_DEBUG("%s: param_len = %d status = %d", __func__,
3089 param->param_len, param->p_param_buf[0]);
3090 value.status = param->p_param_buf[0];
3091 }
3092 if (value.status == 0) {
3093 sub_opcode = param->p_param_buf[1];
3094 APPL_TRACE_DEBUG("%s: subopcode = %d", __func__, sub_opcode);
3095 switch (sub_opcode) {
3096 case VS_HCI_A2DP_OFFLOAD_STOP:
3097 APPL_TRACE_DEBUG("%s: VS_HCI_STOP_A2DP_MEDIA successful", __func__);
3098 break;
3099 case VS_HCI_A2DP_OFFLOAD_START:
3100 (*bta_av_cb.p_cback)(BTA_AV_OFFLOAD_START_RSP_EVT, &value);
3101 break;
3102 default:
3103 break;
3104 }
3105 } else {
3106 APPL_TRACE_DEBUG("%s: Offload failed for subopcode= %d", __func__,
3107 sub_opcode);
3108 if (param->opcode != VS_HCI_A2DP_OFFLOAD_STOP)
3109 (*bta_av_cb.p_cback)(BTA_AV_OFFLOAD_START_RSP_EVT, &value);
3110 }
3111 }
3112
bta_av_vendor_offload_start(tBTA_AV_SCB * p_scb,tBT_A2DP_OFFLOAD * offload_start)3113 void bta_av_vendor_offload_start(tBTA_AV_SCB* p_scb,
3114 tBT_A2DP_OFFLOAD* offload_start) {
3115 uint8_t param[sizeof(tBT_A2DP_OFFLOAD)];
3116 APPL_TRACE_DEBUG("%s", __func__);
3117
3118 uint8_t* p_param = param;
3119 *p_param++ = VS_HCI_A2DP_OFFLOAD_START;
3120
3121 UINT32_TO_STREAM(p_param, offload_start->codec_type);
3122 UINT16_TO_STREAM(p_param, offload_start->max_latency);
3123 UINT16_TO_STREAM(p_param, offload_start->scms_t_enable);
3124 UINT32_TO_STREAM(p_param, offload_start->sample_rate);
3125 UINT8_TO_STREAM(p_param, offload_start->bits_per_sample);
3126 UINT8_TO_STREAM(p_param, offload_start->ch_mode);
3127 UINT32_TO_STREAM(p_param, offload_start->encoded_audio_bitrate);
3128 UINT16_TO_STREAM(p_param, offload_start->acl_hdl);
3129 UINT16_TO_STREAM(p_param, offload_start->l2c_rcid);
3130 UINT16_TO_STREAM(p_param, offload_start->mtu);
3131 ARRAY_TO_STREAM(p_param, offload_start->codec_info,
3132 (int8_t)sizeof(offload_start->codec_info));
3133 p_scb->offload_started = true;
3134 BTM_VendorSpecificCommand(HCI_CONTROLLER_A2DP, p_param - param,
3135 param, offload_vendor_callback);
3136 }
3137
bta_av_vendor_offload_stop()3138 void bta_av_vendor_offload_stop() {
3139 uint8_t param[sizeof(tBT_A2DP_OFFLOAD)];
3140 APPL_TRACE_DEBUG("%s", __func__);
3141 param[0] = VS_HCI_A2DP_OFFLOAD_STOP;
3142 BTM_VendorSpecificCommand(HCI_CONTROLLER_A2DP, 1, param,
3143 offload_vendor_callback);
3144 }
3145 /*******************************************************************************
3146 *
3147 * Function bta_av_offload_req
3148 *
3149 * Description This function is called if application requests offload of
3150 * a2dp audio.
3151 *
3152 * Returns void
3153 *
3154 ******************************************************************************/
bta_av_offload_req(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)3155 void bta_av_offload_req(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
3156 tBTA_AV_STATUS status = BTA_AV_FAIL_RESOURCES;
3157
3158 tBT_A2DP_OFFLOAD offload_start;
3159 APPL_TRACE_DEBUG("%s: stream %s, audio channels open %d", __func__,
3160 p_scb->started ? "STARTED" : "STOPPED",
3161 bta_av_cb.audio_open_cnt);
3162 /* Check if stream has already been started. */
3163 /* Support offload if only one audio source stream is open. */
3164 if (p_scb->started != true) {
3165 status = BTA_AV_FAIL_STREAM;
3166 } else {
3167 bta_av_offload_codec_builder(p_scb, &offload_start);
3168 bta_av_vendor_offload_start(p_scb, &offload_start);
3169 return;
3170 }
3171 if (status != BTA_AV_SUCCESS) {
3172 tBTA_AV bta_av_data;
3173 bta_av_data.status = status;
3174 (*bta_av_cb.p_cback)(BTA_AV_OFFLOAD_START_RSP_EVT, &bta_av_data);
3175 }
3176 /* TODO(eisenbach): RE-IMPLEMENT USING VSC OR HAL EXTENSION
3177 else if (bta_av_cb.audio_open_cnt == 1 &&
3178 p_scb->seps[p_scb->sep_idx].tsep == AVDT_TSEP_SRC &&
3179 p_scb->chnl == BTA_AV_CHNL_AUDIO) {
3180 bt_vendor_op_a2dp_offload_t a2dp_offload_start;
3181
3182 if (L2CA_GetConnectionConfig(
3183 p_scb->l2c_cid, &a2dp_offload_start.acl_data_size,
3184 &a2dp_offload_start.remote_cid, &a2dp_offload_start.lm_handle)) {
3185 APPL_TRACE_DEBUG("%s: l2cmtu %d lcid 0x%02X rcid 0x%02X lm_handle
3186 0x%02X",
3187 __func__, a2dp_offload_start.acl_data_size,
3188 p_scb->l2c_cid, a2dp_offload_start.remote_cid,
3189 a2dp_offload_start.lm_handle);
3190
3191 a2dp_offload_start.bta_av_handle = p_scb->hndl;
3192 a2dp_offload_start.xmit_quota = BTA_AV_A2DP_OFFLOAD_XMIT_QUOTA;
3193 a2dp_offload_start.stream_mtu = p_scb->stream_mtu;
3194 a2dp_offload_start.local_cid = p_scb->l2c_cid;
3195 a2dp_offload_start.is_flushable = true;
3196 a2dp_offload_start.stream_source =
3197 ((uint32_t)(p_scb->cfg.codec_info[1] | p_scb->cfg.codec_info[2]));
3198
3199 memcpy(a2dp_offload_start.codec_info, p_scb->cfg.codec_info,
3200 sizeof(a2dp_offload_start.codec_info));
3201
3202 if (!vendor_get_interface()->send_command(
3203 (vendor_opcode_t)BT_VND_OP_A2DP_OFFLOAD_START,
3204 &a2dp_offload_start)) {
3205 status = BTA_AV_SUCCESS;
3206 p_scb->offload_start_pending = true;
3207 }
3208 }
3209 }
3210 */
3211 }
3212
3213 /*******************************************************************************
3214 *
3215 * Function bta_av_offload_rsp
3216 *
3217 * Description This function is called when the vendor lib responds to
3218 * BT_VND_OP_A2DP_OFFLOAD_START.
3219 *
3220 * Returns void
3221 *
3222 ******************************************************************************/
bta_av_offload_rsp(tBTA_AV_SCB * p_scb,tBTA_AV_DATA * p_data)3223 void bta_av_offload_rsp(tBTA_AV_SCB* p_scb, tBTA_AV_DATA* p_data) {
3224 tBTA_AV_STATUS status = p_data->api_status_rsp.status;
3225
3226 APPL_TRACE_DEBUG("%s: stream %s status %s", __func__,
3227 p_scb->started ? "STARTED" : "STOPPED",
3228 status ? "FAIL" : "SUCCESS");
3229
3230 /* Check if stream has already been started. */
3231 if (status == BTA_AV_SUCCESS && p_scb->started != true) {
3232 status = BTA_AV_FAIL_STREAM;
3233 }
3234
3235 p_scb->offload_start_pending = false;
3236 tBTA_AV bta_av_data;
3237 bta_av_data.status = status;
3238 (*bta_av_cb.p_cback)(BTA_AV_OFFLOAD_START_RSP_EVT, &bta_av_data);
3239 }
3240
bta_av_offload_codec_builder(tBTA_AV_SCB * p_scb,tBT_A2DP_OFFLOAD * p_a2dp_offload)3241 static void bta_av_offload_codec_builder(tBTA_AV_SCB* p_scb,
3242 tBT_A2DP_OFFLOAD* p_a2dp_offload) {
3243 A2dpCodecConfig* CodecConfig = bta_av_get_a2dp_current_codec();
3244 btav_a2dp_codec_index_t codec_index =
3245 A2DP_SourceCodecIndex(p_scb->cfg.codec_info);
3246 uint32_t codec_type = 0;
3247 uint16_t mtu = p_scb->stream_mtu;
3248 APPL_TRACE_DEBUG("%s:codec_index = %d", __func__, codec_index);
3249 switch (codec_index) {
3250 case BTAV_A2DP_CODEC_INDEX_SOURCE_SBC:
3251 codec_type = BTA_AV_CODEC_TYPE_SBC;
3252 if (A2DP_GetMaxBitpoolSbc(p_scb->cfg.codec_info) <=
3253 BTIF_A2DP_MAX_BITPOOL_MQ) {
3254 APPL_TRACE_WARNING("%s: Restricting streaming MTU size for MQ Bitpool",
3255 __func__);
3256 mtu = MAX_2MBPS_AVDTP_MTU;
3257 }
3258 break;
3259 case BTAV_A2DP_CODEC_INDEX_SOURCE_AAC:
3260 codec_type = BTA_AV_CODEC_TYPE_AAC;
3261 break;
3262 case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX:
3263 codec_type = BTA_AV_CODEC_TYPE_APTX;
3264 break;
3265 case BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD:
3266 codec_type = BTA_AV_CODEC_TYPE_APTXHD;
3267 break;
3268 case BTAV_A2DP_CODEC_INDEX_SOURCE_LDAC:
3269 codec_type = BTA_AV_CODEC_TYPE_LDAC;
3270 break;
3271 default:
3272 APPL_TRACE_ERROR("%s: Unknown Codec type ", __func__);
3273 return;
3274 }
3275 if (mtu > MAX_3MBPS_AVDTP_MTU) mtu = MAX_3MBPS_AVDTP_MTU;
3276 p_a2dp_offload->codec_type = codec_type;
3277 p_a2dp_offload->max_latency = 0;
3278 p_a2dp_offload->mtu = mtu;
3279 p_a2dp_offload->acl_hdl =
3280 BTM_GetHCIConnHandle(p_scb->PeerAddress(), BT_TRANSPORT_BR_EDR);
3281 p_a2dp_offload->scms_t_enable =
3282 p_scb->p_cos->cp_is_active(p_scb->PeerAddress());
3283 APPL_TRACE_DEBUG("%s: scms_t_enable =%d", __func__,
3284 p_a2dp_offload->scms_t_enable);
3285
3286 switch (A2DP_GetTrackSampleRate(p_scb->cfg.codec_info)) {
3287 case 44100:
3288 p_a2dp_offload->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
3289 break;
3290 case 48000:
3291 p_a2dp_offload->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
3292 break;
3293 case 88200:
3294 p_a2dp_offload->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
3295 break;
3296 case 96000:
3297 p_a2dp_offload->sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
3298 break;
3299 }
3300 if (L2CA_GetRemoteCid(p_scb->l2c_cid, &p_a2dp_offload->l2c_rcid) == false) {
3301 APPL_TRACE_ERROR("%s: Failed to fetch l2c rcid", __func__);
3302 return;
3303 }
3304 switch (CodecConfig->getAudioBitsPerSample()) {
3305 case 16:
3306 p_a2dp_offload->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
3307 break;
3308 case 24:
3309 p_a2dp_offload->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
3310 break;
3311 case 32:
3312 p_a2dp_offload->bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32;
3313 break;
3314 }
3315 p_a2dp_offload->ch_mode = A2DP_GetTrackChannelCount(p_scb->cfg.codec_info);
3316 p_a2dp_offload->encoded_audio_bitrate = CodecConfig->getTrackBitRate();
3317 if (!CodecConfig->getCodecSpecificConfig(p_a2dp_offload)) {
3318 APPL_TRACE_ERROR("%s: not a valid codec info", __func__);
3319 }
3320 }
3321