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