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