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 is the main implementation file for the BTA advanced audio/video.
22 *
23 ******************************************************************************/
24
25 #define LOG_TAG "bluetooth-a2dp"
26
27 #include <bluetooth/log.h>
28 #include <com_android_bluetooth_flags.h>
29 #include <stdio.h>
30
31 #include <chrono>
32 #include <cstddef>
33 #include <cstdint>
34 #include <cstring>
35
36 #include "a2dp_api.h"
37 #include "a2dp_codec_api.h"
38 #include "a2dp_constants.h"
39 #include "avct_api.h"
40 #include "avdt_api.h"
41 #include "avrc_api.h"
42 #include "avrc_defs.h"
43 #include "bt_dev_class.h"
44 #include "bta/av/bta_av_int.h"
45 #include "bta/include/bta_ar_api.h"
46 #include "bta/include/bta_av_co.h"
47 #include "bta/include/utl.h"
48 #include "bta/sys/bta_sys.h"
49 #include "bta_av_api.h"
50 #include "btif/avrcp/avrcp_service.h"
51 #include "btif/include/btif_av.h"
52 #include "btif/include/btif_av_co.h"
53 #include "btif/include/btif_config.h"
54 #include "hardware/bt_av.h"
55 #include "internal_include/bt_target.h"
56 #include "osi/include/alarm.h"
57 #include "osi/include/allocator.h"
58 #include "osi/include/list.h"
59 #include "stack/include/bt_hdr.h"
60 #include "stack/include/bt_uuid16.h"
61 #include "stack/include/btm_client_interface.h"
62 #include "stack/include/btm_status.h"
63 #include "stack/include/hci_error_code.h"
64 #include "stack/include/sdp_api.h"
65 #include "storage/config_keys.h"
66 #include "types/hci_role.h"
67 #include "types/raw_address.h"
68
69 using namespace bluetooth::legacy::stack::sdp;
70 using namespace bluetooth;
71
72 /*****************************************************************************
73 * Constants and types
74 ****************************************************************************/
75
76 #ifndef BTA_AV_RET_TOUT
77 #define BTA_AV_RET_TOUT 4
78 #endif
79
80 #ifndef BTA_AV_SIG_TOUT
81 #define BTA_AV_SIG_TOUT 4
82 #endif
83
84 #ifndef BTA_AV_IDLE_TOUT
85 #define BTA_AV_IDLE_TOUT 10
86 #endif
87
88 /* the delay time in milliseconds to retry role switch */
89 #ifndef BTA_AV_RS_TIME_VAL
90 #define BTA_AV_RS_TIME_VAL 1000
91 #endif
92
93 /* state machine states */
94 enum { BTA_AV_INIT_ST, BTA_AV_OPEN_ST };
95
96 typedef void (*tBTA_AV_NSM_ACT)(tBTA_AV_DATA* p_data);
97 static void bta_av_api_enable(tBTA_AV_DATA* p_data);
98 static void bta_av_api_register(tBTA_AV_DATA* p_data);
99 static void bta_av_ci_data(tBTA_AV_DATA* p_data);
100 static void bta_av_rpc_conn(tBTA_AV_DATA* p_data);
101
102 static void bta_av_sco_chg_cback(tBTA_SYS_CONN_STATUS status, uint8_t num_sco_links, uint8_t app_id,
103 const RawAddress& peer_addr);
104 static void bta_av_sys_rs_cback(tBTA_SYS_CONN_STATUS status, tHCI_ROLE new_role,
105 tHCI_STATUS hci_status, const RawAddress& peer_addr);
106
107 /*****************************************************************************
108 * Global data
109 ****************************************************************************/
110
111 /* AV control block */
112 tBTA_AV_CB bta_av_cb = {};
113
114 static const char* bta_av_st_code(uint8_t state);
115
116 /*******************************************************************************
117 *
118 * Function bta_av_api_enable
119 *
120 * Description Handle an API enable event.
121 *
122 *
123 * Returns void
124 *
125 ******************************************************************************/
bta_av_api_enable(tBTA_AV_DATA * p_data)126 static void bta_av_api_enable(tBTA_AV_DATA* p_data) {
127 if (btif_av_src_sink_coexist_enabled() && bta_av_cb.features != 0) {
128 tBTA_AV_ENABLE enable;
129 tBTA_AV bta_av_data;
130 bta_av_cb.sink_features = p_data->api_enable.features;
131
132 enable.features = p_data->api_enable.features;
133 bta_av_data.enable = enable;
134 (*bta_av_cb.p_cback)(BTA_AV_ENABLE_EVT, &bta_av_data);
135
136 /* if this is source feature, then exchange them */
137 if (p_data->api_enable.features & BTA_AV_FEAT_SRC) {
138 tBTA_AV_FEAT tmp_feature = bta_av_cb.features;
139 bta_av_cb.features = bta_av_cb.sink_features;
140 bta_av_cb.sink_features = tmp_feature;
141 }
142 return;
143 }
144
145 if (bta_av_cb.disabling) {
146 log::warn("previous (reg_audio={:#x}) is still disabling (attempts={})", bta_av_cb.reg_audio,
147 bta_av_cb.enabling_attempts);
148 if (++bta_av_cb.enabling_attempts <= kEnablingAttemptsCountMaximum) {
149 tBTA_AV_API_ENABLE* p_buf = (tBTA_AV_API_ENABLE*)osi_malloc(sizeof(tBTA_AV_API_ENABLE));
150 memcpy(p_buf, &p_data->api_enable, sizeof(tBTA_AV_API_ENABLE));
151 bta_sys_sendmsg_delayed(p_buf, std::chrono::milliseconds(kEnablingAttemptsIntervalMs));
152 return;
153 }
154 if (bta_av_cb.sdp_a2dp_handle) {
155 if (!get_legacy_stack_sdp_api()->handle.SDP_DeleteRecord(bta_av_cb.sdp_a2dp_handle)) {
156 log::warn("Unable to delete SDP record handle:{}", bta_av_cb.sdp_a2dp_handle);
157 }
158 bta_sys_remove_uuid(UUID_SERVCLASS_AUDIO_SOURCE);
159 }
160 if (bta_av_cb.sdp_a2dp_snk_handle) {
161 if (!get_legacy_stack_sdp_api()->handle.SDP_DeleteRecord(bta_av_cb.sdp_a2dp_snk_handle)) {
162 log::warn("Unable to delete SDP record handle:{}", bta_av_cb.sdp_a2dp_snk_handle);
163 }
164 bta_sys_remove_uuid(UUID_SERVCLASS_AUDIO_SINK);
165 }
166 // deregister from AVDT
167 bta_ar_dereg_avdt();
168
169 // deregister from AVRC
170 bta_ar_dereg_avrc(UUID_SERVCLASS_AV_REMOTE_CONTROL);
171 bta_ar_dereg_avrc(UUID_SERVCLASS_AV_REM_CTRL_TARGET);
172 // deregister from AVCT
173 bta_ar_dereg_avct();
174 }
175
176 /* initialize control block */
177 memset(&bta_av_cb, 0, sizeof(tBTA_AV_CB));
178
179 for (int i = 0; i < BTA_AV_NUM_RCB; i++) {
180 bta_av_cb.rcb[i].handle = BTA_AV_RC_HANDLE_NONE;
181 }
182
183 bta_av_cb.rc_acp_handle = BTA_AV_RC_HANDLE_NONE;
184
185 /* store parameters */
186 bta_av_cb.p_cback = p_data->api_enable.p_cback;
187 bta_av_cb.features = p_data->api_enable.features;
188 bta_av_cb.offload_start_pending_hndl = BTA_AV_INVALID_HANDLE;
189 bta_av_cb.offload_started_hndl = BTA_AV_INVALID_HANDLE;
190
191 tBTA_AV_ENABLE enable;
192 enable.features = bta_av_cb.features;
193
194 /* Register for SCO change event */
195 bta_sys_sco_register(bta_av_sco_chg_cback);
196
197 /* call callback with enable event */
198 tBTA_AV bta_av_data;
199 bta_av_data.enable = enable;
200 (*bta_av_cb.p_cback)(BTA_AV_ENABLE_EVT, &bta_av_data);
201 }
202
203 /*******************************************************************************
204 *
205 * Function bta_av_addr_to_scb
206 *
207 * Description find the stream control block by the peer addr
208 *
209 * Returns void
210 *
211 ******************************************************************************/
bta_av_addr_to_scb(const RawAddress & bd_addr)212 tBTA_AV_SCB* bta_av_addr_to_scb(const RawAddress& bd_addr) {
213 tBTA_AV_SCB* p_scb = NULL;
214 int xx;
215
216 for (xx = 0; xx < BTA_AV_NUM_STRS; xx++) {
217 if (bta_av_cb.p_scb[xx]) {
218 if (bd_addr == bta_av_cb.p_scb[xx]->PeerAddress()) {
219 p_scb = bta_av_cb.p_scb[xx];
220 break;
221 }
222 }
223 }
224 return p_scb;
225 }
226
BTA_AvObtainPeerChannelIndex(const RawAddress & peer_address)227 int BTA_AvObtainPeerChannelIndex(const RawAddress& peer_address) {
228 // Find the entry for the peer (if exists)
229 tBTA_AV_SCB* p_scb = bta_av_addr_to_scb(peer_address);
230 if (p_scb != nullptr) {
231 return p_scb->hdi;
232 }
233
234 // Find the index for an entry that is not used
235 for (int index = 0; index < BTA_AV_NUM_STRS; index++) {
236 tBTA_AV_SCB* p_scb = bta_av_cb.p_scb[index];
237 if (p_scb == nullptr) {
238 continue;
239 }
240 if (p_scb->PeerAddress().IsEmpty()) {
241 const RawAddress& btif_addr = btif_av_find_by_handle(p_scb->hndl);
242 if (!btif_addr.IsEmpty() && btif_addr != peer_address) {
243 log::verbose("btif_addr = {}, index={}!", btif_addr.ToString(), index);
244 continue;
245 }
246 return p_scb->hdi;
247 }
248 }
249
250 return -1;
251 }
252
253 /*******************************************************************************
254 *
255 * Function bta_av_hndl_to_scb
256 *
257 * Description find the stream control block by the handle
258 *
259 * Returns void
260 *
261 ******************************************************************************/
bta_av_hndl_to_scb(uint16_t handle)262 tBTA_AV_SCB* bta_av_hndl_to_scb(uint16_t handle) {
263 tBTA_AV_HNDL hndl = (tBTA_AV_HNDL)handle;
264 tBTA_AV_SCB* p_scb = NULL;
265 uint8_t idx = (hndl & BTA_AV_HNDL_MSK);
266
267 if (idx && (idx <= BTA_AV_NUM_STRS)) {
268 p_scb = bta_av_cb.p_scb[idx - 1];
269 }
270 return p_scb;
271 }
272
273 /*******************************************************************************
274 *
275 * Function bta_av_alloc_scb
276 *
277 * Description allocate stream control block,
278 * register the service to stack
279 * create SDP record
280 *
281 * Returns void
282 *
283 ******************************************************************************/
bta_av_alloc_scb(tBTA_AV_CHNL chnl)284 static tBTA_AV_SCB* bta_av_alloc_scb(tBTA_AV_CHNL chnl) {
285 if (chnl != BTA_AV_CHNL_AUDIO) {
286 log::error("bad channel: {}", chnl);
287 return nullptr;
288 }
289
290 for (int xx = 0; xx < BTA_AV_NUM_STRS; xx++) {
291 if (bta_av_cb.p_scb[xx] != nullptr) {
292 continue;
293 }
294 // Found an empty spot
295 // TODO: After tBTA_AV_SCB is changed to a proper class, the entry
296 // here should be allocated by C++ 'new' statement.
297 tBTA_AV_SCB* p_ret = (tBTA_AV_SCB*)osi_calloc(sizeof(tBTA_AV_SCB));
298 p_ret->rc_handle = BTA_AV_RC_HANDLE_NONE;
299 p_ret->chnl = chnl;
300 p_ret->hndl = (tBTA_AV_HNDL)((xx + 1) | chnl);
301 p_ret->hdi = xx;
302 p_ret->a2dp_list = list_new(nullptr);
303 p_ret->avrc_ct_timer = alarm_new("bta_av.avrc_ct_timer");
304 bta_av_cb.p_scb[xx] = p_ret;
305 return p_ret;
306 }
307
308 return nullptr;
309 }
310
bta_av_find_scb(tBTA_AV_CHNL chnl,uint8_t app_id)311 static tBTA_AV_SCB* bta_av_find_scb(tBTA_AV_CHNL chnl, uint8_t app_id) {
312 if (chnl != BTA_AV_CHNL_AUDIO) {
313 log::error("bad channel: {}", chnl);
314 return nullptr;
315 }
316
317 for (int xx = 0; xx < BTA_AV_NUM_STRS; xx++) {
318 if ((bta_av_cb.p_scb[xx] != nullptr) && (bta_av_cb.p_scb[xx]->chnl == chnl) &&
319 (bta_av_cb.p_scb[xx]->app_id == app_id)) {
320 log::verbose("found at: {}", xx);
321 return bta_av_cb.p_scb[xx];
322 }
323 }
324
325 return nullptr;
326 }
327
bta_av_free_scb(tBTA_AV_SCB * p_scb)328 void bta_av_free_scb(tBTA_AV_SCB* p_scb) {
329 if (p_scb == nullptr) {
330 return;
331 }
332 uint8_t scb_index = p_scb->hdi;
333 log::assert_that(scb_index < BTA_AV_NUM_STRS, "assert failed: scb_index < BTA_AV_NUM_STRS");
334
335 log::assert_that(p_scb == bta_av_cb.p_scb[scb_index],
336 "assert failed: p_scb == bta_av_cb.p_scb[scb_index]");
337 bta_av_cb.p_scb[scb_index] = nullptr;
338 alarm_free(p_scb->avrc_ct_timer);
339 list_free(p_scb->a2dp_list);
340 p_scb->a2dp_list = NULL;
341 // TODO: After tBTA_AV_SCB is changed to a proper class, the entry
342 // here should be de-allocated by C++ 'delete' statement.
343 osi_free(p_scb);
344 }
345
OnConnected(const RawAddress & peer_address)346 void tBTA_AV_SCB::OnConnected(const RawAddress& peer_address) {
347 peer_address_ = peer_address;
348
349 if (peer_address.IsEmpty()) {
350 log::error("Invalid peer address: {}", peer_address);
351 return;
352 }
353
354 // Read and restore the AVDTP version from local storage
355 uint16_t avdtp_version = 0;
356 size_t version_value_size = sizeof(avdtp_version);
357 if (!btif_config_get_bin(peer_address_.ToString(), BTIF_STORAGE_KEY_AVDTP_VERSION,
358 (uint8_t*)&avdtp_version, &version_value_size)) {
359 log::warn("Failed to read cached peer AVDTP version for {}", peer_address_);
360 } else {
361 SetAvdtpVersion(avdtp_version);
362 }
363 }
364
OnDisconnected()365 void tBTA_AV_SCB::OnDisconnected() {
366 peer_address_ = RawAddress::kEmpty;
367 SetAvdtpVersion(0);
368 }
369
SetAvdtpVersion(uint16_t avdtp_version)370 void tBTA_AV_SCB::SetAvdtpVersion(uint16_t avdtp_version) {
371 avdtp_version_ = avdtp_version;
372 log::info("AVDTP version for {} set to 0x{:x}", peer_address_, avdtp_version_);
373 }
374
375 /*******************************************************************************
376 ******************************************************************************/
bta_av_conn_cback(uint8_t,const RawAddress & bd_addr,uint8_t event,tAVDT_CTRL * p_data,uint8_t scb_index)377 void bta_av_conn_cback(uint8_t /* handle */, const RawAddress& bd_addr, uint8_t event,
378 tAVDT_CTRL* p_data, uint8_t scb_index) {
379 uint16_t evt = 0;
380 tBTA_AV_SCB* p_scb = NULL;
381
382 if (event == BTA_AR_AVDT_CONN_EVT || event == AVDT_CONNECT_IND_EVT ||
383 event == AVDT_DISCONNECT_IND_EVT) {
384 evt = BTA_AV_SIG_CHG_EVT;
385 if (event == AVDT_DISCONNECT_IND_EVT) {
386 p_scb = bta_av_addr_to_scb(bd_addr);
387 } else if (event == AVDT_CONNECT_IND_EVT) {
388 log::verbose("CONN_IND is ACP:{}", p_data->hdr.err_param);
389 }
390
391 tBTA_AV_STR_MSG* p_msg = (tBTA_AV_STR_MSG*)osi_malloc(sizeof(tBTA_AV_STR_MSG));
392 p_msg->hdr.event = evt;
393 p_msg->hdr.layer_specific = event;
394 p_msg->hdr.offset = p_data->hdr.err_param;
395 p_msg->bd_addr = bd_addr;
396 p_msg->scb_index = scb_index;
397 if (p_scb) {
398 log::verbose("bta_handle x{:x}, role x{:x}", p_scb->hndl, p_scb->role);
399 }
400 log::info("conn_cback bd_addr: {}, scb_index: {}", bd_addr, scb_index);
401 bta_sys_sendmsg(p_msg);
402 }
403 }
404
405 /*******************************************************************************
406 *
407 * Function bta_av_a2dp_report_cback
408 *
409 * Description A2DP report callback.
410 *
411 * Returns void
412 *
413 ******************************************************************************/
bta_av_a2dp_report_cback(uint8_t,AVDT_REPORT_TYPE,tAVDT_REPORT_DATA *)414 static void bta_av_a2dp_report_cback(uint8_t /* handle */, AVDT_REPORT_TYPE /* type */,
415 tAVDT_REPORT_DATA* /* p_data */) {
416 /* Do not need to handle report data for now.
417 * This empty function is here for conformance reasons. */
418 }
419
420 /*******************************************************************************
421 *
422 * Function bta_av_api_register
423 *
424 * Description allocate stream control block,
425 * register the service to stack
426 * create SDP record
427 *
428 * Returns void
429 *
430 ******************************************************************************/
bta_av_api_register(tBTA_AV_DATA * p_data)431 static void bta_av_api_register(tBTA_AV_DATA* p_data) {
432 tBTA_AV_REGISTER reg_data;
433 tBTA_AV_SCB* p_scb; /* stream control block */
434 AvdtpRcb reg;
435 AvdtpStreamConfig avdtp_stream_config;
436 char* p_service_name;
437 tBTA_UTL_COD cod;
438 uint8_t local_role = 0;
439
440 if (bta_av_cb.disabling || (bta_av_cb.features == 0)) {
441 log::warn("AV instance (features={:#x}, reg_audio={:#x}) is not ready for app_id {}",
442 bta_av_cb.features, bta_av_cb.reg_audio, p_data->api_reg.app_id);
443 tBTA_AV_API_REG* p_buf = (tBTA_AV_API_REG*)osi_malloc(sizeof(tBTA_AV_API_REG));
444 memcpy(p_buf, &p_data->api_reg, sizeof(tBTA_AV_API_REG));
445 bta_sys_sendmsg_delayed(p_buf, std::chrono::milliseconds(kEnablingAttemptsIntervalMs));
446 return;
447 }
448
449 avdtp_stream_config.Reset();
450 if (btif_av_src_sink_coexist_enabled()) {
451 local_role = (p_data->api_reg.service_uuid == UUID_SERVCLASS_AUDIO_SINK) ? AVDT_TSEP_SNK
452 : AVDT_TSEP_SRC;
453 }
454
455 reg_data.status = BTA_AV_FAIL_RESOURCES;
456 reg_data.app_id = p_data->api_reg.app_id;
457 reg_data.chnl = (tBTA_AV_CHNL)p_data->hdr.layer_specific;
458
459 const uint16_t avrcp_version = AVRC_GetProfileVersion();
460 log::info("AVRCP version used for sdp: 0x{:x}", avrcp_version);
461 uint16_t profile_initialized = p_data->api_reg.service_uuid;
462 if (profile_initialized == UUID_SERVCLASS_AUDIO_SINK) {
463 p_bta_av_cfg = get_bta_avk_cfg();
464 } else if (profile_initialized == UUID_SERVCLASS_AUDIO_SOURCE) {
465 p_bta_av_cfg = &bta_av_cfg;
466
467 if (avrcp_version == AVRC_REV_1_3) {
468 log::info("AVRCP 1.3 capabilites used");
469 p_bta_av_cfg = &bta_av_cfg_compatibility;
470 }
471 }
472
473 log::verbose("profile: 0x{:x}", profile_initialized);
474 if (p_bta_av_cfg == NULL) {
475 log::error("AV configuration is null!");
476 return;
477 }
478
479 do {
480 p_scb = nullptr;
481 if (btif_av_src_sink_coexist_enabled()) {
482 p_scb = bta_av_find_scb(reg_data.chnl, reg_data.app_id);
483 }
484 if (p_scb == nullptr) {
485 p_scb = bta_av_alloc_scb(reg_data.chnl);
486 }
487 if (p_scb == NULL) {
488 log::error("failed to alloc SCB");
489 break;
490 }
491
492 reg_data.hndl = p_scb->hndl;
493 p_scb->app_id = reg_data.app_id;
494
495 /* initialize the stream control block */
496 reg_data.status = BTA_AV_SUCCESS;
497
498 if ((btif_av_src_sink_coexist_enabled() && !(bta_av_cb.reg_role & (1 << local_role))) ||
499 (!btif_av_src_sink_coexist_enabled() && bta_av_cb.reg_audio == 0)) {
500 /* the first channel registered. register to AVDTP */
501 reg.ctrl_mtu = 672;
502 reg.ret_tout = BTA_AV_RET_TOUT;
503 reg.sig_tout = BTA_AV_SIG_TOUT;
504 reg.idle_tout = BTA_AV_IDLE_TOUT;
505 reg.scb_index = p_scb->hdi;
506 bta_ar_reg_avdt(®, bta_av_conn_cback);
507 bta_sys_role_chg_register(&bta_av_sys_rs_cback);
508
509 /* create remote control TG service if required */
510 if (bta_av_cb.features & (BTA_AV_FEAT_RCTG)) {
511 /* register with no authorization; let AVDTP use authorization instead
512 */
513 bta_ar_reg_avct();
514
515 // Add target record for
516 // a) A2DP sink profile. or
517 // b) A2DP source profile only if new avrcp service is disabled.
518 if (profile_initialized == UUID_SERVCLASS_AUDIO_SINK ||
519 (profile_initialized == UUID_SERVCLASS_AUDIO_SOURCE && !is_new_avrcp_enabled())) {
520 bta_ar_reg_avrc(UUID_SERVCLASS_AV_REM_CTRL_TARGET, "AV Remote Control Target", "",
521 p_bta_av_cfg->avrc_tg_cat, bta_av_cb.features & BTA_AV_FEAT_BROWSE,
522 avrcp_version);
523 }
524 }
525
526 /* Set the Capturing service class bit */
527 if (profile_initialized == UUID_SERVCLASS_AUDIO_SOURCE) {
528 cod.service = BTM_COD_SERVICE_CAPTURING;
529 } else if (profile_initialized == UUID_SERVCLASS_AUDIO_SINK) {
530 cod.service = BTM_COD_SERVICE_RENDERING;
531 }
532 utl_set_device_class(&cod, BTA_UTL_SET_COD_SERVICE_CLASS);
533 } /* if 1st channel */
534
535 /* get stream configuration and create stream */
536 avdtp_stream_config.cfg.num_codec = 1;
537 avdtp_stream_config.nsc_mask = AvdtpStreamConfig::AVDT_NSC_RECONFIG;
538 if (!(bta_av_cb.features & BTA_AV_FEAT_PROTECT)) {
539 avdtp_stream_config.nsc_mask |= AvdtpStreamConfig::AVDT_NSC_SECURITY;
540 }
541 log::verbose("nsc_mask: 0x{:x}", avdtp_stream_config.nsc_mask);
542
543 if (p_data->api_reg.p_service_name[0] == 0) {
544 p_service_name = NULL;
545 } else {
546 p_service_name = p_data->api_reg.p_service_name;
547 }
548
549 p_scb->suspend_sup = true;
550 p_scb->recfg_sup = true;
551
552 avdtp_stream_config.scb_index = p_scb->hdi;
553 avdtp_stream_config.p_avdt_ctrl_cback = &bta_av_proc_stream_evt;
554
555 /* set up the audio stream control block */
556 p_scb->p_cos = &bta_av_a2dp_cos;
557 p_scb->media_type = AVDT_MEDIA_TYPE_AUDIO;
558 avdtp_stream_config.cfg.psc_mask = AVDT_PSC_TRANS;
559 avdtp_stream_config.media_type = AVDT_MEDIA_TYPE_AUDIO;
560 avdtp_stream_config.mtu = MAX_3MBPS_AVDTP_MTU;
561 btav_a2dp_codec_index_t codec_index_min = BTAV_A2DP_CODEC_INDEX_SOURCE_MIN;
562 btav_a2dp_codec_index_t codec_index_max = BTAV_A2DP_CODEC_INDEX_SOURCE_MAX;
563
564 if (bta_av_cb.features & BTA_AV_FEAT_REPORT) {
565 avdtp_stream_config.cfg.psc_mask |= AVDT_PSC_REPORT;
566 avdtp_stream_config.p_report_cback = bta_av_a2dp_report_cback;
567 }
568 if (bta_av_cb.features & BTA_AV_FEAT_DELAY_RPT) {
569 avdtp_stream_config.cfg.psc_mask |= AVDT_PSC_DELAY_RPT;
570 }
571
572 if (profile_initialized == UUID_SERVCLASS_AUDIO_SOURCE) {
573 avdtp_stream_config.tsep = AVDT_TSEP_SRC;
574 codec_index_min = BTAV_A2DP_CODEC_INDEX_SOURCE_MIN;
575 codec_index_max = BTAV_A2DP_CODEC_INDEX_SOURCE_MAX;
576 } else if (profile_initialized == UUID_SERVCLASS_AUDIO_SINK) {
577 avdtp_stream_config.tsep = AVDT_TSEP_SNK;
578 avdtp_stream_config.p_sink_data_cback = bta_av_sink_data_cback;
579 codec_index_min = BTAV_A2DP_CODEC_INDEX_SINK_MIN;
580 codec_index_max = BTAV_A2DP_CODEC_INDEX_SINK_MAX;
581 }
582
583 if (btif_av_src_sink_coexist_enabled()) {
584 for (int xx = codec_index_min; xx < codec_index_max; xx++) {
585 p_scb->seps[xx].av_handle = 0;
586 }
587 } else {
588 for (int xx = 0; xx < BTAV_A2DP_CODEC_INDEX_MAX; xx++) {
589 p_scb->seps[xx].av_handle = 0;
590 }
591 }
592
593 /* keep the configuration in the stream control block */
594 p_scb->cfg = avdtp_stream_config.cfg;
595 for (int i = codec_index_min; i < codec_index_max; i++) {
596 btav_a2dp_codec_index_t codec_index = static_cast<btav_a2dp_codec_index_t>(i);
597 if (!bta_av_co_is_supported_codec(codec_index)) {
598 continue;
599 }
600 if (!(*bta_av_a2dp_cos.init)(codec_index, &avdtp_stream_config.cfg)) {
601 continue;
602 }
603 if (AVDT_CreateStream(p_scb->app_id, &p_scb->seps[codec_index].av_handle,
604 avdtp_stream_config) != AVDT_SUCCESS) {
605 log::warn("bta_handle=0x{:x} (app_id {}) failed to alloc an SEP index:{}", p_scb->hndl,
606 p_scb->app_id, codec_index);
607 continue;
608 }
609 /* Save a copy of the codec */
610 memcpy(p_scb->seps[codec_index].codec_info, avdtp_stream_config.cfg.codec_info,
611 AVDT_CODEC_SIZE);
612 p_scb->seps[codec_index].tsep = avdtp_stream_config.tsep;
613 if (avdtp_stream_config.tsep == AVDT_TSEP_SNK) {
614 p_scb->seps[codec_index].p_app_sink_data_cback = p_data->api_reg.p_app_sink_data_cback;
615 } else {
616 /* In case of A2DP SOURCE we don't need a callback to
617 * handle media packets.
618 */
619 p_scb->seps[codec_index].p_app_sink_data_cback = NULL;
620 }
621 }
622 if ((btif_av_src_sink_coexist_enabled() && !(bta_av_cb.reg_role & (1 << local_role))) ||
623 (!btif_av_src_sink_coexist_enabled() && !bta_av_cb.reg_audio)) {
624 bta_av_cb.sdp_a2dp_handle = 0;
625 bta_av_cb.sdp_a2dp_snk_handle = 0;
626 if (profile_initialized == UUID_SERVCLASS_AUDIO_SOURCE) {
627 /* create the SDP records on the 1st audio channel */
628 bta_av_cb.sdp_a2dp_handle = get_legacy_stack_sdp_api()->handle.SDP_CreateRecord();
629 A2DP_AddRecord(UUID_SERVCLASS_AUDIO_SOURCE, p_service_name, NULL, A2DP_SUPF_PLAYER,
630 bta_av_cb.sdp_a2dp_handle);
631 bta_sys_add_uuid(UUID_SERVCLASS_AUDIO_SOURCE);
632 } else if (profile_initialized == UUID_SERVCLASS_AUDIO_SINK) {
633 bta_av_cb.sdp_a2dp_snk_handle = get_legacy_stack_sdp_api()->handle.SDP_CreateRecord();
634 A2DP_AddRecord(UUID_SERVCLASS_AUDIO_SINK, p_service_name, NULL, A2DP_SUPF_PLAYER,
635 bta_av_cb.sdp_a2dp_snk_handle);
636 bta_sys_add_uuid(UUID_SERVCLASS_AUDIO_SINK);
637 }
638 /* start listening when A2DP is registered */
639 if (bta_av_cb.features & BTA_AV_FEAT_RCTG) {
640 bta_av_rc_create(&bta_av_cb, AVCT_ROLE_ACCEPTOR, 0, BTA_AV_NUM_LINKS + 1);
641 }
642
643 /* if the AV and AVK are both supported, it cannot support the CT role
644 */
645 if (bta_av_cb.features & (BTA_AV_FEAT_RCCT)) {
646 /* if TG is not supported, we need to register to AVCT now */
647 if ((bta_av_cb.features & (BTA_AV_FEAT_RCTG)) == 0) {
648 bta_ar_reg_avct();
649 bta_av_rc_create(&bta_av_cb, AVCT_ROLE_ACCEPTOR, 0, BTA_AV_NUM_LINKS + 1);
650 }
651 // Add control record for sink profile.
652 // Also adds control record for source profile when new avrcp service is not enabled.
653 if (profile_initialized == UUID_SERVCLASS_AUDIO_SINK ||
654 (profile_initialized == UUID_SERVCLASS_AUDIO_SOURCE && !is_new_avrcp_enabled())) {
655 uint16_t control_version = AVRC_GetControlProfileVersion();
656 /* Create an SDP record as AVRC CT. We create 1.3 for SOURCE
657 * because we rely on feature bits being scanned by external
658 * devices more than the profile version itself.
659 */
660 if (profile_initialized == UUID_SERVCLASS_AUDIO_SOURCE && !is_new_avrcp_enabled()) {
661 control_version = AVRC_REV_1_3;
662 }
663 if (!btif_av_src_sink_coexist_enabled() &&
664 profile_initialized == UUID_SERVCLASS_AUDIO_SINK) {
665 control_version = AVRC_REV_1_6;
666 }
667 bta_ar_reg_avrc(UUID_SERVCLASS_AV_REMOTE_CONTROL, "AV Remote Control", "",
668 p_bta_av_cfg->avrc_ct_cat, bta_av_cb.features & BTA_AV_FEAT_BROWSE,
669 control_version);
670 }
671 }
672 }
673 bta_av_cb.reg_audio |= BTA_AV_HNDL_TO_MSK(p_scb->hdi);
674 log::verbose("reg_audio: 0x{:x}", bta_av_cb.reg_audio);
675 } while (0);
676
677 if (btif_av_src_sink_coexist_enabled()) {
678 bta_av_cb.reg_role |= (1 << local_role);
679 reg_data.peer_sep =
680 (profile_initialized == UUID_SERVCLASS_AUDIO_SOURCE) ? AVDT_TSEP_SNK : AVDT_TSEP_SRC;
681
682 /* there are too much check depend on it's only source */
683 if ((profile_initialized == UUID_SERVCLASS_AUDIO_SINK) &&
684 (bta_av_cb.reg_role & (1 << AVDT_TSEP_SRC))) {
685 p_bta_av_cfg = &bta_av_cfg;
686
687 if (avrcp_version == AVRC_REV_1_3) { // ver if need
688 log::verbose("AVRCP 1.3 capabilites used");
689 p_bta_av_cfg = &bta_av_cfg_compatibility;
690 }
691 }
692 }
693
694 /* call callback with register event */
695 tBTA_AV bta_av_data;
696 bta_av_data.reg = reg_data;
697 (*bta_av_cb.p_cback)(BTA_AV_REGISTER_EVT, &bta_av_data);
698 }
699
700 /*******************************************************************************
701 *
702 * Function bta_av_api_deregister
703 *
704 * Description de-register a channel
705 *
706 *
707 * Returns void
708 *
709 ******************************************************************************/
bta_av_api_deregister(tBTA_AV_DATA * p_data)710 void bta_av_api_deregister(tBTA_AV_DATA* p_data) {
711 tBTA_AV_SCB* p_scb = bta_av_hndl_to_scb(p_data->hdr.layer_specific);
712
713 if (p_scb) {
714 p_scb->deregistering = true;
715 bta_av_ssm_execute(p_scb, BTA_AV_API_CLOSE_EVT, p_data);
716 } else {
717 bta_av_dereg_comp(p_data);
718 }
719 }
720
721 /*******************************************************************************
722 *
723 * Function bta_av_ci_data
724 *
725 * Description Forward the BTA_AV_CI_SRC_DATA_READY_EVT to stream state
726 * machine.
727 *
728 *
729 * Returns void
730 *
731 ******************************************************************************/
bta_av_ci_data(tBTA_AV_DATA * p_data)732 static void bta_av_ci_data(tBTA_AV_DATA* p_data) {
733 tBTA_AV_SCB* p_scb;
734 int i;
735 uint8_t chnl = (uint8_t)p_data->hdr.layer_specific;
736
737 for (i = 0; i < BTA_AV_NUM_STRS; i++) {
738 p_scb = bta_av_cb.p_scb[i];
739
740 if (p_scb && p_scb->chnl == chnl) {
741 bta_av_ssm_execute(p_scb, BTA_AV_SRC_DATA_READY_EVT, p_data);
742 }
743 }
744 }
745
746 /*******************************************************************************
747 *
748 * Function bta_av_rpc_conn
749 *
750 * Description report report channel open
751 *
752 * Returns void
753 *
754 ******************************************************************************/
bta_av_rpc_conn(tBTA_AV_DATA *)755 static void bta_av_rpc_conn(tBTA_AV_DATA* /* p_data */) {}
756
757 /*******************************************************************************
758 *
759 * Function bta_av_chk_start
760 *
761 * Description if this is audio channel, check if more than one audio
762 * channel is connected & already started.
763 *
764 * Returns true, if need api_start
765 *
766 ******************************************************************************/
bta_av_chk_start(tBTA_AV_SCB * p_scb)767 bool bta_av_chk_start(tBTA_AV_SCB* p_scb) {
768 bool start = false;
769
770 if ((p_scb->chnl == BTA_AV_CHNL_AUDIO) && (bta_av_cb.audio_open_cnt >= 2) &&
771 (((p_scb->role & BTA_AV_ROLE_AD_ACP) == 0) || // Outgoing connection or
772 (bta_av_cb.features & BTA_AV_FEAT_ACP_START))) { // Auto-starting option
773 // More than one audio channel is connected.
774 // If this is the 2nd stream as ACP, give INT a chance to issue the START
775 // command.
776 for (int i = 0; i < BTA_AV_NUM_STRS; i++) {
777 tBTA_AV_SCB* p_scbi = bta_av_cb.p_scb[i];
778 if (p_scbi && p_scbi->chnl == BTA_AV_CHNL_AUDIO && p_scbi->co_started) {
779 start = true;
780 // May need to update the flush timeout of this already started stream
781 if (p_scbi->co_started != bta_av_cb.audio_open_cnt) {
782 p_scbi->co_started = bta_av_cb.audio_open_cnt;
783 }
784 }
785 }
786 }
787
788 log::info("peer {} channel:{} bta_av_cb.audio_open_cnt:{} role:0x{:x} features:0x{:x} start:{}",
789 p_scb->PeerAddress(), p_scb->chnl, bta_av_cb.audio_open_cnt, p_scb->role,
790 bta_av_cb.features, start);
791 return start;
792 }
793
794 /*******************************************************************************
795 *
796 * Function bta_av_restore_switch
797 *
798 * Description assume that the caller of this function already makes
799 * sure that there's only one ACL connection left
800 *
801 * Returns void
802 *
803 ******************************************************************************/
bta_av_restore_switch(void)804 void bta_av_restore_switch(void) {
805 tBTA_AV_CB* p_cb = &bta_av_cb;
806 int i;
807 uint8_t mask;
808
809 log::verbose("reg_audio: 0x{:x}", bta_av_cb.reg_audio);
810 for (i = 0; i < BTA_AV_NUM_STRS; i++) {
811 mask = BTA_AV_HNDL_TO_MSK(i);
812 if (p_cb->conn_audio == mask) {
813 if (p_cb->p_scb[i]) {
814 get_btm_client_interface().link_policy.BTM_unblock_role_switch_for(
815 p_cb->p_scb[i]->PeerAddress());
816 }
817 break;
818 }
819 }
820 }
821
822 /*******************************************************************************
823 *
824 * Function bta_av_sys_rs_cback
825 *
826 * Description Receives the role change event from dm
827 *
828 * Returns (BTA_SYS_ROLE_CHANGE, new_role, hci_status, p_bda)
829 *
830 ******************************************************************************/
bta_av_sys_rs_cback(tBTA_SYS_CONN_STATUS,tHCI_ROLE new_role,tHCI_STATUS hci_status,const RawAddress & peer_addr)831 static void bta_av_sys_rs_cback(tBTA_SYS_CONN_STATUS /* status */, tHCI_ROLE new_role,
832 tHCI_STATUS hci_status, const RawAddress& peer_addr) {
833 int i;
834 tBTA_AV_SCB* p_scb = NULL;
835 tHCI_ROLE cur_role;
836 uint8_t peer_idx = 0;
837
838 log::verbose("peer {} new_role:{} hci_status:0x{:x} bta_av_cb.rs_idx:{}", peer_addr, new_role,
839 hci_status, bta_av_cb.rs_idx);
840
841 for (i = 0; i < BTA_AV_NUM_STRS; i++) {
842 /* loop through all the SCBs to find matching peer addresses and report the
843 * role change event */
844 /* note that more than one SCB (a2dp & vdp) maybe waiting for this event */
845 p_scb = bta_av_cb.p_scb[i];
846 if (p_scb && p_scb->PeerAddress() == peer_addr) {
847 tBTA_AV_ROLE_RES* p_buf = (tBTA_AV_ROLE_RES*)osi_malloc(sizeof(tBTA_AV_ROLE_RES));
848 log::verbose("peer {} found: new_role:{}, hci_status:0x{:x} bta_handle:0x{:x}", peer_addr,
849 new_role, hci_status, p_scb->hndl);
850 p_buf->hdr.event = BTA_AV_ROLE_CHANGE_EVT;
851 p_buf->hdr.layer_specific = p_scb->hndl;
852 p_buf->new_role = new_role;
853 p_buf->hci_status = hci_status;
854 bta_sys_sendmsg(p_buf);
855
856 peer_idx = p_scb->hdi + 1; /* Handle index for the peer_addr */
857 }
858 }
859
860 /* restore role switch policy, if role switch failed */
861 if ((HCI_SUCCESS != hci_status) &&
862 (get_btm_client_interface().link_policy.BTM_GetRole(peer_addr, &cur_role) ==
863 tBTM_STATUS::BTM_SUCCESS) &&
864 (cur_role == HCI_ROLE_PERIPHERAL)) {
865 get_btm_client_interface().link_policy.BTM_unblock_role_switch_for(peer_addr);
866 }
867
868 /* if BTA_AvOpen() was called for other device, which caused the role switch
869 * of the peer_addr, */
870 /* we need to continue opening process for the BTA_AvOpen(). */
871 if ((bta_av_cb.rs_idx != 0) && (bta_av_cb.rs_idx != peer_idx)) {
872 if ((bta_av_cb.rs_idx - 1) < BTA_AV_NUM_STRS) {
873 p_scb = bta_av_cb.p_scb[bta_av_cb.rs_idx - 1];
874 }
875 if (p_scb && p_scb->q_tag == BTA_AV_Q_TAG_OPEN) {
876 log::verbose("peer {} rs_idx:{}, bta_handle:0x{:x} q_tag:{}", p_scb->PeerAddress(),
877 bta_av_cb.rs_idx, p_scb->hndl, p_scb->q_tag);
878
879 if (HCI_SUCCESS == hci_status || HCI_ERR_NO_CONNECTION == hci_status) {
880 p_scb->q_info.open.switch_res = BTA_AV_RS_OK;
881 } else {
882 log::error("peer {} (p_scb peer {}) role switch failed: new_role:{} hci_status:0x{:x}",
883 peer_addr, p_scb->PeerAddress(), new_role, hci_status);
884 p_scb->q_info.open.switch_res = BTA_AV_RS_FAIL;
885 }
886
887 /* Continue av open process */
888 bta_av_do_disc_a2dp(p_scb, (tBTA_AV_DATA*)&(p_scb->q_info.open));
889 }
890
891 bta_av_cb.rs_idx = 0;
892 }
893 }
894
895 /*******************************************************************************
896 *
897 * Function bta_av_sco_chg_cback
898 *
899 * Description receive & process the SCO connection up/down event from sys.
900 * call setup also triggers this callback, to suspend av before
901 * SCO activity happens, or to resume av once call ends.
902 *
903 * Returns void
904 *
905 ******************************************************************************/
bta_av_sco_chg_cback(tBTA_SYS_CONN_STATUS status,uint8_t num_sco_links,uint8_t,const RawAddress & peer_addr)906 static void bta_av_sco_chg_cback(tBTA_SYS_CONN_STATUS status, uint8_t num_sco_links,
907 uint8_t /* app_id */, const RawAddress& peer_addr) {
908 tBTA_AV_SCB* p_scb;
909 int i;
910 tBTA_AV_API_STOP stop;
911
912 log::info("status={}, num_links={}", bta_sys_conn_status_text(status), num_sco_links);
913 if (num_sco_links) {
914 bta_av_cb.sco_occupied = true;
915 log::debug("SCO occupied peer:{} status:{}", peer_addr, bta_sys_conn_status_text(status));
916
917 if (bta_av_cb.features & BTA_AV_FEAT_NO_SCO_SSPD) {
918 return;
919 }
920
921 /* either BTA_SYS_SCO_OPEN or BTA_SYS_SCO_CLOSE with remaining active SCO */
922 for (i = 0; i < BTA_AV_NUM_STRS; i++) {
923 p_scb = bta_av_cb.p_scb[i];
924
925 if (p_scb && p_scb->co_started && (!p_scb->sco_suspend)) {
926 log::verbose("suspending scb:{}", i);
927 /* scb is used and started, not suspended automatically */
928 p_scb->sco_suspend = true;
929 stop.flush = false;
930 stop.suspend = true;
931 stop.reconfig_stop = false;
932 bta_av_ssm_execute(p_scb, BTA_AV_AP_STOP_EVT, (tBTA_AV_DATA*)&stop);
933 }
934 }
935 } else {
936 bta_av_cb.sco_occupied = false;
937 log::debug("SCO unoccupied peer:{} status:{}", peer_addr, bta_sys_conn_status_text(status));
938
939 if (bta_av_cb.features & BTA_AV_FEAT_NO_SCO_SSPD) {
940 return;
941 }
942
943 for (i = 0; i < BTA_AV_NUM_STRS; i++) {
944 p_scb = bta_av_cb.p_scb[i];
945
946 if (p_scb && p_scb->sco_suspend) /* scb is used and suspended for SCO */
947 {
948 log::verbose("starting scb:{}", i);
949 bta_av_ssm_execute(p_scb, BTA_AV_AP_START_EVT, NULL);
950 }
951 }
952 }
953 }
954
955 /*******************************************************************************
956 *
957 * Function bta_av_switch_if_needed
958 *
959 * Description This function checks if there is another existing AV
960 * channel that is local as peripheral role.
961 * If so, role switch and remove it from link policy.
962 *
963 * Returns true, if role switch is done
964 *
965 ******************************************************************************/
bta_av_switch_if_needed(tBTA_AV_SCB *)966 bool bta_av_switch_if_needed(tBTA_AV_SCB* /*p_scb*/) {
967 // TODO: A workaround for devices that are connected first, become
968 // Central, and block follow-up role changes - b/72122792 .
969 return false;
970 #if 0
971 uint8_t role;
972 bool needed = false;
973 tBTA_AV_SCB* p_scbi;
974 int i;
975 uint8_t mask;
976
977 for (i = 0; i < BTA_AV_NUM_STRS; i++) {
978 mask = BTA_AV_HNDL_TO_MSK(i);
979 p_scbi = bta_av_cb.p_scb[i];
980 if (p_scbi && (p_scb->hdi != i) && /* not the original channel */
981 ((bta_av_cb.conn_audio & mask))) /* connected audio */
982 {
983 get_btm_client_interface().link_policy.BTM_GetRole(p_scbi->PeerAddress(), &role);
984 /* this channel is open - clear the role switch link policy for this link
985 */
986 if (HCI_ROLE_CENTRAL != role) {
987 if (bta_av_cb.features & BTA_AV_FEAT_CENTRAL)
988 get_btm_client_interface().link_policy.BTM_block_role_switch_for(p_scbi->PeerAddress());
989 if (BTM_CMD_STARTED !=
990 BTM_SwitchRole(p_scbi->PeerAddress(), HCI_ROLE_CENTRAL)) {
991 /* can not switch role on SCBI
992 * start the timer on SCB - because this function is ONLY called when
993 * SCB gets API_OPEN */
994 bta_sys_start_timer(p_scb->avrc_ct_timer, BTA_AV_RS_TIME_VAL,
995 BTA_AV_AVRC_TIMER_EVT, p_scb->hndl);
996 }
997 needed = true;
998 /* mark the original channel as waiting for RS result */
999 bta_av_cb.rs_idx = p_scb->hdi + 1;
1000 break;
1001 }
1002 }
1003 }
1004 return needed;
1005 #endif
1006 }
1007
1008 /*******************************************************************************
1009 *
1010 * Function bta_av_link_role_ok
1011 *
1012 * Description This function checks if the SCB has existing ACL connection
1013 * If so, check if the link role fits the requirements.
1014 *
1015 * Returns true, if role is ok
1016 *
1017 ******************************************************************************/
bta_av_link_role_ok(tBTA_AV_SCB * p_scb,uint8_t bits)1018 bool bta_av_link_role_ok(tBTA_AV_SCB* p_scb, uint8_t bits) {
1019 tHCI_ROLE role;
1020 if (get_btm_client_interface().link_policy.BTM_GetRole(p_scb->PeerAddress(), &role) !=
1021 tBTM_STATUS::BTM_SUCCESS) {
1022 log::warn("Unable to find link role for device:{}", p_scb->PeerAddress());
1023 return true;
1024 }
1025
1026 if (role != HCI_ROLE_CENTRAL && (A2DP_BitsSet(bta_av_cb.conn_audio) > bits)) {
1027 log::info(
1028 "Switch link role to central peer:{} bta_handle:0x{:x} current_role:{} "
1029 "conn_audio:0x{:x} bits:{} features:0x{:x}",
1030 p_scb->PeerAddress(), p_scb->hndl, RoleText(role), bta_av_cb.conn_audio, bits,
1031 bta_av_cb.features);
1032 const tBTM_STATUS status =
1033 get_btm_client_interface().link_policy.BTM_SwitchRoleToCentral(p_scb->PeerAddress());
1034 switch (status) {
1035 case tBTM_STATUS::BTM_CMD_STARTED:
1036 break;
1037 case tBTM_STATUS::BTM_MODE_UNSUPPORTED:
1038 case tBTM_STATUS::BTM_DEV_RESTRICT_LISTED:
1039 // Role switch can never happen, but indicate to caller
1040 // a result such that a timer will not start to repeatedly
1041 // try something not possible.
1042 log::error("Link can never role switch to central device:{}", p_scb->PeerAddress());
1043 break;
1044 default:
1045 /* can not switch role on SCB - start the timer on SCB */
1046 p_scb->wait |= BTA_AV_WAIT_ROLE_SW_RES_START;
1047 log::error("Unable to switch role to central device:{} error:{}", p_scb->PeerAddress(),
1048 btm_status_text(status));
1049 return false;
1050 }
1051 }
1052 return true;
1053 }
1054
1055 /*******************************************************************************
1056 *
1057 * Function bta_av_dup_audio_buf
1058 *
1059 * Description dup the audio data to the q_info.a2dp of other audio
1060 * channels
1061 *
1062 * Returns void
1063 *
1064 ******************************************************************************/
bta_av_dup_audio_buf(tBTA_AV_SCB * p_scb,BT_HDR * p_buf)1065 void bta_av_dup_audio_buf(tBTA_AV_SCB* p_scb, BT_HDR* p_buf) {
1066 /* Test whether there is more than one audio channel connected */
1067 if ((p_buf == NULL) || (bta_av_cb.audio_open_cnt < 2)) {
1068 return;
1069 }
1070
1071 uint16_t copy_size = BT_HDR_SIZE + p_buf->len + p_buf->offset;
1072 for (int i = 0; i < BTA_AV_NUM_STRS; i++) {
1073 tBTA_AV_SCB* p_scbi = bta_av_cb.p_scb[i];
1074
1075 if (i == p_scb->hdi) {
1076 continue; /* Ignore the original channel */
1077 }
1078 if ((p_scbi == NULL) || !p_scbi->co_started) {
1079 continue; /* Ignore if SCB is not used or started */
1080 }
1081 if (!(bta_av_cb.conn_audio & BTA_AV_HNDL_TO_MSK(i))) {
1082 continue; /* Audio is not connected */
1083 }
1084
1085 /* Enqueue the data */
1086 BT_HDR* p_new = (BT_HDR*)osi_malloc(copy_size);
1087 memcpy(p_new, p_buf, copy_size);
1088 list_append(p_scbi->a2dp_list, p_new);
1089
1090 if (list_length(p_scbi->a2dp_list) > p_bta_av_cfg->audio_mqs) {
1091 // Drop the oldest packet
1092 bta_av_co_audio_drop(p_scbi->hndl, p_scbi->PeerAddress());
1093 BT_HDR* p_buf_drop = static_cast<BT_HDR*>(list_front(p_scbi->a2dp_list));
1094 list_remove(p_scbi->a2dp_list, p_buf_drop);
1095 osi_free(p_buf_drop);
1096 }
1097 }
1098 }
1099
bta_av_non_state_machine_event(uint16_t event,tBTA_AV_DATA * p_data)1100 static void bta_av_non_state_machine_event(uint16_t event, tBTA_AV_DATA* p_data) {
1101 switch (event) {
1102 case BTA_AV_API_ENABLE_EVT:
1103 bta_av_api_enable(p_data);
1104 break;
1105 case BTA_AV_API_REGISTER_EVT:
1106 bta_av_api_register(p_data);
1107 break;
1108 case BTA_AV_API_DEREGISTER_EVT:
1109 bta_av_api_deregister(p_data);
1110 break;
1111 case BTA_AV_API_DISCONNECT_EVT:
1112 bta_av_api_disconnect(p_data);
1113 break;
1114 case BTA_AV_API_SET_LATENCY_EVT:
1115 bta_av_api_set_latency(p_data);
1116 break;
1117 case BTA_AV_CI_SRC_DATA_READY_EVT:
1118 bta_av_ci_data(p_data);
1119 break;
1120 case BTA_AV_SIG_CHG_EVT:
1121 bta_av_sig_chg(p_data);
1122 break;
1123 case BTA_AV_SIGNALLING_TIMER_EVT:
1124 bta_av_signalling_timer(p_data);
1125 break;
1126 case BTA_AV_SDP_AVRC_DISC_EVT:
1127 bta_av_rc_disc_done(p_data);
1128 break;
1129 case BTA_AV_AVRC_CLOSE_EVT:
1130 bta_av_rc_closed(p_data);
1131 break;
1132 case BTA_AV_AVRC_BROWSE_OPEN_EVT:
1133 bta_av_rc_browse_opened(p_data);
1134 break;
1135 case BTA_AV_AVRC_BROWSE_CLOSE_EVT:
1136 bta_av_rc_browse_closed(p_data);
1137 break;
1138 case BTA_AV_CONN_CHG_EVT:
1139 bta_av_conn_chg(p_data);
1140 break;
1141 case BTA_AV_DEREG_COMP_EVT:
1142 bta_av_dereg_comp(p_data);
1143 break;
1144 case BTA_AV_AVDT_RPT_CONN_EVT:
1145 bta_av_rpc_conn(p_data);
1146 break;
1147 case BTA_AV_API_PEER_SEP_EVT:
1148 bta_av_api_set_peer_sep(p_data);
1149 break;
1150 }
1151 }
1152
bta_av_sm_execute(tBTA_AV_CB * p_cb,uint16_t event,tBTA_AV_DATA * p_data)1153 void bta_av_sm_execute(tBTA_AV_CB* p_cb, uint16_t event, tBTA_AV_DATA* p_data) {
1154 log::verbose("AV event=0x{:x}({}) state={}({})", event, bta_av_evt_code(event), p_cb->state,
1155 bta_av_st_code(p_cb->state));
1156 switch (p_cb->state) {
1157 case BTA_AV_INIT_ST:
1158 switch (event) {
1159 case BTA_AV_API_DISABLE_EVT:
1160 bta_av_disable(p_cb, p_data);
1161 break;
1162 case BTA_AV_API_META_RSP_EVT:
1163 bta_av_rc_free_rsp(p_cb, p_data);
1164 break;
1165 case BTA_AV_AVRC_OPEN_EVT:
1166 p_cb->state = BTA_AV_OPEN_ST;
1167 bta_av_rc_opened(p_cb, p_data);
1168 break;
1169 case BTA_AV_AVRC_MSG_EVT:
1170 bta_av_rc_free_browse_msg(p_cb, p_data);
1171 break;
1172 }
1173 break;
1174 case BTA_AV_OPEN_ST:
1175 switch (event) {
1176 case BTA_AV_API_DISABLE_EVT:
1177 p_cb->state = BTA_AV_INIT_ST;
1178 bta_av_disable(p_cb, p_data);
1179 break;
1180 case BTA_AV_API_REMOTE_CMD_EVT:
1181 bta_av_rc_remote_cmd(p_cb, p_data);
1182 break;
1183 case BTA_AV_API_VENDOR_CMD_EVT:
1184 bta_av_rc_vendor_cmd(p_cb, p_data);
1185 break;
1186 case BTA_AV_API_VENDOR_RSP_EVT:
1187 bta_av_rc_vendor_rsp(p_cb, p_data);
1188 break;
1189 case BTA_AV_API_META_RSP_EVT:
1190 bta_av_rc_meta_rsp(p_cb, p_data);
1191 break;
1192 case BTA_AV_API_RC_CLOSE_EVT:
1193 bta_av_rc_close(p_cb, p_data);
1194 break;
1195 case BTA_AV_AVRC_OPEN_EVT:
1196 bta_av_rc_opened(p_cb, p_data);
1197 break;
1198 case BTA_AV_AVRC_MSG_EVT:
1199 bta_av_rc_msg(p_cb, p_data);
1200 break;
1201 case BTA_AV_AVRC_NONE_EVT:
1202 p_cb->state = BTA_AV_INIT_ST;
1203 break;
1204 }
1205 break;
1206 }
1207 }
1208
1209 /*******************************************************************************
1210 *
1211 * Function bta_av_hdl_event
1212 *
1213 * Description Advanced audio/video main event handling function.
1214 *
1215 *
1216 * Returns bool
1217 *
1218 ******************************************************************************/
bta_av_hdl_event(const BT_HDR_RIGID * p_msg)1219 bool bta_av_hdl_event(const BT_HDR_RIGID* p_msg) {
1220 if (p_msg->event > BTA_AV_LAST_EVT) {
1221 return true; /* to free p_msg */
1222 }
1223 if (p_msg->event >= BTA_AV_FIRST_NSM_EVT) {
1224 log::verbose("AV nsm event=0x{:x}({})", p_msg->event, bta_av_evt_code(p_msg->event));
1225 bta_av_non_state_machine_event(p_msg->event, (tBTA_AV_DATA*)p_msg);
1226 } else if (p_msg->event >= BTA_AV_FIRST_SM_EVT && p_msg->event <= BTA_AV_LAST_SM_EVT) {
1227 log::verbose("AV sm event=0x{:x}({})", p_msg->event, bta_av_evt_code(p_msg->event));
1228 /* state machine events */
1229 bta_av_sm_execute(&bta_av_cb, p_msg->event, (tBTA_AV_DATA*)p_msg);
1230 } else {
1231 log::verbose("bta_handle=0x{:x}", p_msg->layer_specific);
1232 /* stream state machine events */
1233 bta_av_ssm_execute(bta_av_hndl_to_scb(p_msg->layer_specific), p_msg->event,
1234 (tBTA_AV_DATA*)p_msg);
1235 }
1236 return true;
1237 }
1238
1239 /*****************************************************************************
1240 * Debug Functions
1241 ****************************************************************************/
1242 /*******************************************************************************
1243 *
1244 * Function bta_av_st_code
1245 *
1246 * Description
1247 *
1248 * Returns char *
1249 *
1250 ******************************************************************************/
bta_av_st_code(uint8_t state)1251 static const char* bta_av_st_code(uint8_t state) {
1252 switch (state) {
1253 case BTA_AV_INIT_ST:
1254 return "INIT";
1255 case BTA_AV_OPEN_ST:
1256 return "OPEN";
1257 default:
1258 return "unknown";
1259 }
1260 }
1261 /*******************************************************************************
1262 *
1263 * Function bta_av_evt_code
1264 *
1265 * Description
1266 *
1267 * Returns char *
1268 *
1269 ******************************************************************************/
bta_av_evt_code(uint16_t evt_code)1270 const char* bta_av_evt_code(uint16_t evt_code) {
1271 switch (evt_code) {
1272 case BTA_AV_API_DISABLE_EVT:
1273 return "API_DISABLE";
1274 case BTA_AV_API_REMOTE_CMD_EVT:
1275 return "API_REMOTE_CMD";
1276 case BTA_AV_API_VENDOR_CMD_EVT:
1277 return "API_VENDOR_CMD";
1278 case BTA_AV_API_VENDOR_RSP_EVT:
1279 return "API_VENDOR_RSP";
1280 case BTA_AV_API_META_RSP_EVT:
1281 return "API_META_RSP_EVT";
1282 case BTA_AV_API_RC_CLOSE_EVT:
1283 return "API_RC_CLOSE";
1284 case BTA_AV_AVRC_OPEN_EVT:
1285 return "AVRC_OPEN";
1286 case BTA_AV_AVRC_MSG_EVT:
1287 return "AVRC_MSG";
1288 case BTA_AV_AVRC_NONE_EVT:
1289 return "AVRC_NONE";
1290
1291 case BTA_AV_API_OPEN_EVT:
1292 return "API_OPEN";
1293 case BTA_AV_API_CLOSE_EVT:
1294 return "API_CLOSE";
1295 case BTA_AV_AP_START_EVT:
1296 return "AP_START";
1297 case BTA_AV_AP_STOP_EVT:
1298 return "AP_STOP";
1299 case BTA_AV_API_RECONFIG_EVT:
1300 return "API_RECONFIG";
1301 case BTA_AV_API_PROTECT_REQ_EVT:
1302 return "API_PROTECT_REQ";
1303 case BTA_AV_API_PROTECT_RSP_EVT:
1304 return "API_PROTECT_RSP";
1305 case BTA_AV_API_RC_OPEN_EVT:
1306 return "API_RC_OPEN";
1307 case BTA_AV_SRC_DATA_READY_EVT:
1308 return "SRC_DATA_READY";
1309 case BTA_AV_CI_SETCONFIG_OK_EVT:
1310 return "CI_SETCONFIG_OK";
1311 case BTA_AV_CI_SETCONFIG_FAIL_EVT:
1312 return "CI_SETCONFIG_FAIL";
1313 case BTA_AV_SDP_DISC_OK_EVT:
1314 return "SDP_DISC_OK";
1315 case BTA_AV_SDP_DISC_FAIL_EVT:
1316 return "SDP_DISC_FAIL";
1317 case BTA_AV_STR_DISC_OK_EVT:
1318 return "STR_DISC_OK";
1319 case BTA_AV_STR_DISC_FAIL_EVT:
1320 return "STR_DISC_FAIL";
1321 case BTA_AV_STR_GETCAP_OK_EVT:
1322 return "STR_GETCAP_OK";
1323 case BTA_AV_STR_GETCAP_FAIL_EVT:
1324 return "STR_GETCAP_FAIL";
1325 case BTA_AV_STR_OPEN_OK_EVT:
1326 return "STR_OPEN_OK";
1327 case BTA_AV_STR_OPEN_FAIL_EVT:
1328 return "STR_OPEN_FAIL";
1329 case BTA_AV_STR_START_OK_EVT:
1330 return "STR_START_OK";
1331 case BTA_AV_STR_START_FAIL_EVT:
1332 return "STR_START_FAIL";
1333 case BTA_AV_STR_CLOSE_EVT:
1334 return "STR_CLOSE";
1335 case BTA_AV_STR_CONFIG_IND_EVT:
1336 return "STR_CONFIG_IND";
1337 case BTA_AV_STR_SECURITY_IND_EVT:
1338 return "STR_SECURITY_IND";
1339 case BTA_AV_STR_SECURITY_CFM_EVT:
1340 return "STR_SECURITY_CFM";
1341 case BTA_AV_STR_WRITE_CFM_EVT:
1342 return "STR_WRITE_CFM";
1343 case BTA_AV_STR_SUSPEND_CFM_EVT:
1344 return "STR_SUSPEND_CFM";
1345 case BTA_AV_STR_RECONFIG_CFM_EVT:
1346 return "STR_RECONFIG_CFM";
1347 case BTA_AV_AVRC_TIMER_EVT:
1348 return "AVRC_TIMER";
1349 case BTA_AV_AVDT_CONNECT_EVT:
1350 return "AVDT_CONNECT";
1351 case BTA_AV_AVDT_DISCONNECT_EVT:
1352 return "AVDT_DISCONNECT";
1353 case BTA_AV_ROLE_CHANGE_EVT:
1354 return "ROLE_CHANGE";
1355 case BTA_AV_AVDT_DELAY_RPT_EVT:
1356 return "AVDT_DELAY_RPT";
1357 case BTA_AV_ACP_CONNECT_EVT:
1358 return "ACP_CONNECT";
1359 case BTA_AV_API_OFFLOAD_START_EVT:
1360 return "OFFLOAD_START";
1361 case BTA_AV_API_OFFLOAD_START_RSP_EVT:
1362 return "OFFLOAD_START_RSP";
1363
1364 case BTA_AV_API_ENABLE_EVT:
1365 return "API_ENABLE";
1366 case BTA_AV_API_REGISTER_EVT:
1367 return "API_REG";
1368 case BTA_AV_API_DEREGISTER_EVT:
1369 return "API_DEREG";
1370 case BTA_AV_API_DISCONNECT_EVT:
1371 return "API_DISCNT";
1372 case BTA_AV_CI_SRC_DATA_READY_EVT:
1373 return "CI_DATA_READY";
1374 case BTA_AV_SIG_CHG_EVT:
1375 return "SIG_CHG";
1376 case BTA_AV_SIGNALLING_TIMER_EVT:
1377 return "SIGNALLING_TIMER";
1378 case BTA_AV_SDP_AVRC_DISC_EVT:
1379 return "SDP_AVRC_DISC";
1380 case BTA_AV_AVRC_CLOSE_EVT:
1381 return "AVRC_CLOSE";
1382 case BTA_AV_AVRC_BROWSE_OPEN_EVT:
1383 return "AVRC_BROWSE_OPEN";
1384 case BTA_AV_AVRC_BROWSE_CLOSE_EVT:
1385 return "AVRC_BROWSE_CLOSE";
1386 case BTA_AV_CONN_CHG_EVT:
1387 return "CONN_CHG";
1388 case BTA_AV_DEREG_COMP_EVT:
1389 return "DEREG_COMP";
1390 case BTA_AV_AVDT_RPT_CONN_EVT:
1391 return "RPT_CONN";
1392 default:
1393 return "unknown";
1394 }
1395 }
1396
bta_debug_av_dump(int fd)1397 void bta_debug_av_dump(int fd) {
1398 dprintf(fd, "\nBTA AV State:\n");
1399 dprintf(fd, " State Machine State: %s\n", bta_av_st_code(bta_av_cb.state));
1400 dprintf(fd, " SDP A2DP source handle: %d\n", bta_av_cb.sdp_a2dp_handle);
1401 dprintf(fd, " SDP A2DP sink handle: %d\n", bta_av_cb.sdp_a2dp_snk_handle);
1402 dprintf(fd, " Features: 0x%x\n", bta_av_cb.features);
1403 dprintf(fd, " SDP handle: %d\n", bta_av_cb.handle);
1404 dprintf(fd, " Disabling: %s\n", bta_av_cb.disabling ? "true" : "false");
1405 dprintf(fd, " SCO occupied: %s\n", bta_av_cb.sco_occupied ? "true" : "false");
1406 dprintf(fd, " Connected audio channels: %d\n", bta_av_cb.audio_open_cnt);
1407 dprintf(fd, " Connected audio channels mask: 0x%x\n", bta_av_cb.conn_audio);
1408 dprintf(fd, " Registered audio channels mask: 0x%x\n", bta_av_cb.reg_audio);
1409 dprintf(fd, " Connected LCBs mask: 0x%x\n", bta_av_cb.conn_lcb);
1410 dprintf(fd, " Offload start pending handle: %d\n", bta_av_cb.offload_start_pending_hndl);
1411 dprintf(fd, " Offload started handle: %d\n", bta_av_cb.offload_started_hndl);
1412
1413 for (size_t i = 0; i < sizeof(bta_av_cb.lcb) / sizeof(bta_av_cb.lcb[0]); i++) {
1414 const tBTA_AV_LCB& lcb = bta_av_cb.lcb[i];
1415 if (lcb.addr.IsEmpty()) {
1416 continue;
1417 }
1418 dprintf(fd, "\n Link control block: %zu peer: %s\n", i,
1419 lcb.addr.ToRedactedStringForLogging().c_str());
1420 dprintf(fd, " Connected stream handle mask: 0x%x\n", lcb.conn_msk);
1421 dprintf(fd, " Index(+1) to LCB: %d\n", lcb.lidx);
1422 }
1423 for (size_t i = 0; i < BTA_AV_NUM_STRS; i++) {
1424 const tBTA_AV_SCB* p_scb = bta_av_cb.p_scb[i];
1425 if (p_scb == nullptr) {
1426 continue;
1427 }
1428 if (p_scb->PeerAddress().IsEmpty()) {
1429 continue;
1430 }
1431 dprintf(fd, "\n BTA ID: %zu peer: %s\n", i,
1432 p_scb->PeerAddress().ToRedactedStringForLogging().c_str());
1433 dprintf(fd, " SDP discovery started: %s\n", p_scb->sdp_discovery_started ? "true" : "false");
1434 for (size_t j = 0; j < BTAV_A2DP_CODEC_INDEX_MAX; j++) {
1435 const tBTA_AV_SEP& sep = p_scb->seps[j];
1436 if (sep.av_handle == 0) {
1437 continue;
1438 }
1439 dprintf(fd, " SEP ID: %zu\n", j);
1440 dprintf(fd, " SEP AVDTP handle: %d\n", sep.av_handle);
1441 dprintf(fd, " Local SEP type: %d\n", sep.tsep);
1442 dprintf(fd, " Codec: %s\n", A2DP_CodecName(sep.codec_info));
1443 }
1444 dprintf(fd, " BTA info tag: %d\n", p_scb->q_tag);
1445 dprintf(fd, " API Open peer: %s\n",
1446 p_scb->q_info.open.bd_addr.ToRedactedStringForLogging().c_str());
1447 dprintf(fd, " Use AVRCP: %s\n", p_scb->q_info.open.use_rc ? "true" : "false");
1448 dprintf(fd, " Switch result: %d\n", p_scb->q_info.open.switch_res);
1449 dprintf(fd, " Initiator UUID: 0x%x\n", p_scb->q_info.open.uuid);
1450 dprintf(fd, " Saved API Open peer: %s\n",
1451 p_scb->open_api.bd_addr.ToRedactedStringForLogging().c_str());
1452 dprintf(fd, " Use AVRCP: %s\n", p_scb->open_api.use_rc ? "true" : "false");
1453 dprintf(fd, " Switch result: %d\n", p_scb->open_api.switch_res);
1454 dprintf(fd, " Initiator UUID: 0x%x\n", p_scb->open_api.uuid);
1455 dprintf(fd, " Link signalling timer: %s\n",
1456 alarm_is_scheduled(p_scb->link_signalling_timer) ? "Scheduled" : "Not scheduled");
1457 dprintf(fd, " Accept signalling timer: %s\n",
1458 alarm_is_scheduled(p_scb->accept_signalling_timer) ? "Scheduled" : "Not scheduled");
1459 dprintf(fd, " Accept open timer: %s\n",
1460 alarm_is_scheduled(p_scb->accept_open_timer) ? "Scheduled" : "Not scheduled");
1461 // TODO: Print p_scb->sep_info[], cfg, avrc_ct_timer, current_codec ?
1462 dprintf(fd, " L2CAP Channel ID: %d\n", p_scb->l2c_cid);
1463 dprintf(fd, " Stream MTU: %d\n", p_scb->stream_mtu);
1464 dprintf(fd, " AVDTP version: 0x%x\n", p_scb->AvdtpVersion());
1465 dprintf(fd, " Media type: %d\n", p_scb->media_type);
1466 dprintf(fd, " Congested: %s\n", p_scb->cong ? "true" : "false");
1467 dprintf(fd, " Open status: %d\n", p_scb->open_status);
1468 dprintf(fd, " Channel: %d\n", p_scb->chnl);
1469 dprintf(fd, " BTA handle: 0x%x\n", p_scb->hndl);
1470 dprintf(fd, " Protocol service capabilities mask: 0x%x\n", p_scb->cur_psc_mask);
1471 dprintf(fd, " AVDTP handle: %d\n", p_scb->avdt_handle);
1472 dprintf(fd, " Stream control block index: %d\n", p_scb->hdi);
1473 dprintf(fd, " State machine state: %s(%d)\n", bta_av_sst_code(p_scb->state), p_scb->state);
1474 dprintf(fd, " AVDTP label: 0x%x\n", p_scb->avdt_label);
1475 dprintf(fd, " Application ID: %d\n", p_scb->app_id);
1476 dprintf(fd, " Role: 0x%x\n", p_scb->role);
1477 dprintf(fd, " Queued L2CAP buffers: %d\n", p_scb->l2c_bufs);
1478 dprintf(fd, " AVRCP allowed: %s\n", p_scb->use_rc ? "true" : "false");
1479 dprintf(fd, " Stream started: %s\n", p_scb->started ? "true" : "false");
1480 dprintf(fd, " Stream call-out started: %d\n", p_scb->co_started);
1481 dprintf(fd, " AVDTP Reconfig supported: %s\n", p_scb->recfg_sup ? "true" : "false");
1482 dprintf(fd, " AVDTP Suspend supported: %s\n", p_scb->suspend_sup ? "true" : "false");
1483 dprintf(fd, " Deregistering: %s\n", p_scb->deregistering ? "true" : "false");
1484 dprintf(fd, " SCO automatic Suspend: %s\n", p_scb->sco_suspend ? "true" : "false");
1485 dprintf(fd, " Incoming/outgoing connection collusion mask: 0x%x\n", p_scb->coll_mask);
1486 dprintf(fd, " Wait mask: 0x%x\n", p_scb->wait);
1487 dprintf(fd, " Don't use RTP header: %s\n", p_scb->no_rtp_header ? "true" : "false");
1488 dprintf(fd, " Intended UUID of Initiator to connect to: 0x%x\n", p_scb->uuid_int);
1489 }
1490 }
1491