1 /******************************************************************************
2 *
3 * Copyright 2004-2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 /******************************************************************************
20 *
21 * This file contains functions for managing the SCO connection used in AG.
22 *
23 ******************************************************************************/
24
25 #include <base/functional/bind.h>
26 #include <bluetooth/log.h>
27 #include <com_android_bluetooth_flags.h>
28
29 #include <cstdint>
30
31 #include "audio_hal_interface/hfp_client_interface.h"
32 #include "bta/ag/bta_ag_int.h"
33 #include "bta_ag_swb_aptx.h"
34 #include "btm_status.h"
35 #include "common/init_flags.h"
36 #include "hci/controller_interface.h"
37 #include "internal_include/bt_target.h"
38 #include "internal_include/bt_trace.h"
39 #include "main/shim/entry.h"
40 #include "os/logging/log_adapter.h"
41 #include "osi/include/properties.h"
42 #include "stack/btm/btm_int_types.h"
43 #include "stack/btm/btm_sco.h"
44 #include "stack/btm/btm_sco_hfp_hal.h"
45 #include "stack/include/btm_api.h"
46 #include "stack/include/btm_client_interface.h"
47 #include "stack/include/main_thread.h"
48 #include "types/raw_address.h"
49
50 extern tBTM_CB btm_cb;
51
52 using HfpInterface = bluetooth::audio::hfp::HfpClientInterface;
53 using namespace bluetooth;
54
55 /* Codec negotiation timeout */
56 #ifndef BTA_AG_CODEC_NEGOTIATION_TIMEOUT_MS
57 #define BTA_AG_CODEC_NEGOTIATION_TIMEOUT_MS (3 * 1000) /* 3 seconds */
58 #endif
59
60 #define BTM_VOICE_SETTING_CVSD \
61 ((uint16_t)(HCI_INP_CODING_LINEAR | HCI_INP_DATA_FMT_2S_COMPLEMENT | \
62 HCI_INP_SAMPLE_SIZE_16BIT | HCI_AIR_CODING_FORMAT_CVSD))
63
64 #define BTM_VOICE_SETTING_TRANS \
65 ((uint16_t)(HCI_INP_CODING_LINEAR | HCI_INP_DATA_FMT_2S_COMPLEMENT | \
66 HCI_INP_SAMPLE_SIZE_16BIT | HCI_AIR_CODING_FORMAT_TRANSPNT))
67
68 static bool sco_allowed = true;
69 static RawAddress active_device_addr = {};
70 static std::unique_ptr<HfpInterface> hfp_client_interface;
71 static std::unique_ptr<HfpInterface::Offload> hfp_offload_interface;
72 static std::unordered_map<int, ::hfp::sco_config> sco_config_map;
73 static std::unordered_map<tBTA_AG_PEER_CODEC, esco_coding_format_t>
74 codec_coding_format_map{
75 {UUID_CODEC_LC3, ESCO_CODING_FORMAT_LC3},
76 {UUID_CODEC_MSBC, ESCO_CODING_FORMAT_MSBC},
77 {UUID_CODEC_CVSD, ESCO_CODING_FORMAT_CVSD},
78 };
79
80 /* sco events */
81 enum {
82 BTA_AG_SCO_LISTEN_E, /* listen request */
83 BTA_AG_SCO_OPEN_E, /* open request */
84 BTA_AG_SCO_XFER_E, /* transfer request */
85 BTA_AG_SCO_CN_DONE_E, /* codec negotiation done */
86 BTA_AG_SCO_REOPEN_E, /* Retry with other codec when failed */
87 BTA_AG_SCO_CLOSE_E, /* close request */
88 BTA_AG_SCO_SHUTDOWN_E, /* shutdown request */
89 BTA_AG_SCO_CONN_OPEN_E, /* sco open */
90 BTA_AG_SCO_CONN_CLOSE_E, /* sco closed */
91 };
92
93 #define CASE_RETURN_STR(const) \
94 case const: \
95 return #const;
96
bta_ag_sco_evt_str(uint8_t event)97 static const char* bta_ag_sco_evt_str(uint8_t event) {
98 switch (event) {
99 CASE_RETURN_STR(BTA_AG_SCO_LISTEN_E)
100 CASE_RETURN_STR(BTA_AG_SCO_OPEN_E)
101 CASE_RETURN_STR(BTA_AG_SCO_XFER_E)
102 CASE_RETURN_STR(BTA_AG_SCO_CN_DONE_E)
103 CASE_RETURN_STR(BTA_AG_SCO_REOPEN_E)
104 CASE_RETURN_STR(BTA_AG_SCO_CLOSE_E)
105 CASE_RETURN_STR(BTA_AG_SCO_SHUTDOWN_E)
106 CASE_RETURN_STR(BTA_AG_SCO_CONN_OPEN_E)
107 CASE_RETURN_STR(BTA_AG_SCO_CONN_CLOSE_E)
108 default:
109 return "Unknown SCO Event";
110 }
111 }
112
bta_ag_sco_state_str(uint8_t state)113 static const char* bta_ag_sco_state_str(uint8_t state) {
114 switch (state) {
115 CASE_RETURN_STR(BTA_AG_SCO_SHUTDOWN_ST)
116 CASE_RETURN_STR(BTA_AG_SCO_LISTEN_ST)
117 CASE_RETURN_STR(BTA_AG_SCO_CODEC_ST)
118 CASE_RETURN_STR(BTA_AG_SCO_OPENING_ST)
119 CASE_RETURN_STR(BTA_AG_SCO_OPEN_CL_ST)
120 CASE_RETURN_STR(BTA_AG_SCO_OPEN_XFER_ST)
121 CASE_RETURN_STR(BTA_AG_SCO_OPEN_ST)
122 CASE_RETURN_STR(BTA_AG_SCO_CLOSING_ST)
123 CASE_RETURN_STR(BTA_AG_SCO_CLOSE_OP_ST)
124 CASE_RETURN_STR(BTA_AG_SCO_CLOSE_XFER_ST)
125 CASE_RETURN_STR(BTA_AG_SCO_SHUTTING_ST)
126 default:
127 return "Unknown SCO State";
128 }
129 }
130
131 /**
132 * Check if bd_addr is the current active device.
133 *
134 * @param bd_addr target device address
135 * @return True if bd_addr is the current active device, False otherwise or if
136 * no active device is set (i.e. active_device_addr is empty)
137 */
bta_ag_sco_is_active_device(const RawAddress & bd_addr)138 bool bta_ag_sco_is_active_device(const RawAddress& bd_addr) {
139 return !active_device_addr.IsEmpty() && active_device_addr == bd_addr;
140 }
141
142 void updateCodecParametersFromProviderInfo(tBTA_AG_PEER_CODEC esco_codec,
143 enh_esco_params_t& params);
144
145 /*******************************************************************************
146 *
147 * Function bta_ag_sco_conn_cback
148 *
149 * Description BTM SCO connection callback.
150 *
151 *
152 * Returns void
153 *
154 ******************************************************************************/
bta_ag_sco_conn_cback(uint16_t sco_idx)155 static void bta_ag_sco_conn_cback(uint16_t sco_idx) {
156 uint16_t handle;
157 tBTA_AG_SCB* p_scb;
158
159 /* match callback to scb; first check current sco scb */
160 if (bta_ag_cb.sco.p_curr_scb != nullptr && bta_ag_cb.sco.p_curr_scb->in_use) {
161 handle = bta_ag_scb_to_idx(bta_ag_cb.sco.p_curr_scb);
162 }
163 /* then check for scb connected to this peer */
164 else {
165 /* Check if SLC is up */
166 handle = bta_ag_idx_by_bdaddr(BTM_ReadScoBdAddr(sco_idx));
167 p_scb = bta_ag_scb_by_idx(handle);
168 if (p_scb && !p_scb->svc_conn) handle = 0;
169 }
170
171 if (handle != 0) {
172 do_in_main_thread(
173 FROM_HERE, base::BindOnce(&bta_ag_sm_execute_by_handle, handle,
174 BTA_AG_SCO_OPEN_EVT, tBTA_AG_DATA::kEmpty));
175 } else {
176 /* no match found; disconnect sco, init sco variables */
177 bta_ag_cb.sco.p_curr_scb = nullptr;
178 bta_ag_cb.sco.state = BTA_AG_SCO_SHUTDOWN_ST;
179 if (get_btm_client_interface().sco.BTM_RemoveSco(sco_idx) != BTM_SUCCESS) {
180 log::warn("Unable to remove SCO idx:{}", sco_idx);
181 }
182 }
183 }
184
185 /*******************************************************************************
186 *
187 * Function bta_ag_sco_disc_cback
188 *
189 * Description BTM SCO disconnection callback.
190 *
191 *
192 * Returns void
193 *
194 ******************************************************************************/
bta_ag_sco_disc_cback(uint16_t sco_idx)195 static void bta_ag_sco_disc_cback(uint16_t sco_idx) {
196 uint16_t handle = 0;
197
198 log::debug("sco_idx: 0x{:x} sco.state:{}", sco_idx,
199 sco_state_text(static_cast<tSCO_STATE>(bta_ag_cb.sco.state)));
200 log::debug("scb[0] in_use:{} sco_idx: 0x{:x} ag state:{}",
201 bta_ag_cb.scb[0].in_use, bta_ag_cb.scb[0].sco_idx,
202 bta_ag_state_str(bta_ag_cb.scb[0].state));
203 log::debug("scb[1] in_use:{} sco_idx:0x{:x} ag state:{}",
204 bta_ag_cb.scb[1].in_use, bta_ag_cb.scb[1].sco_idx,
205 bta_ag_state_str(bta_ag_cb.scb[1].state));
206
207 /* match callback to scb */
208 if (bta_ag_cb.sco.p_curr_scb != nullptr && bta_ag_cb.sco.p_curr_scb->in_use) {
209 /* We only care about callbacks for the active SCO */
210 if (bta_ag_cb.sco.p_curr_scb->sco_idx != sco_idx) {
211 if (bta_ag_cb.sco.p_curr_scb->sco_idx != 0xFFFF) return;
212 }
213 handle = bta_ag_scb_to_idx(bta_ag_cb.sco.p_curr_scb);
214 }
215
216 if (handle != 0) {
217 const bool aptx_voice =
218 is_hfp_aptx_voice_enabled() &&
219 (bta_ag_cb.sco.p_curr_scb->is_aptx_swb_codec == true) &&
220 (bta_ag_cb.sco.p_curr_scb->inuse_codec ==
221 BTA_AG_SCO_APTX_SWB_SETTINGS_Q0);
222 log::verbose("aptx_voice={}, inuse_codec={:#x}", aptx_voice,
223 bta_ag_cb.sco.p_curr_scb->inuse_codec);
224
225 /* Restore settings */
226 if (bta_ag_cb.sco.p_curr_scb->inuse_codec == UUID_CODEC_MSBC ||
227 bta_ag_cb.sco.p_curr_scb->inuse_codec == UUID_CODEC_LC3 || aptx_voice ||
228 (com::android::bluetooth::flags::fix_hfp_qual_1_9() &&
229 bta_ag_cb.sco.p_curr_scb->inuse_codec == UUID_CODEC_CVSD &&
230 bta_ag_cb.sco.p_curr_scb->codec_cvsd_settings !=
231 BTA_AG_SCO_CVSD_SETTINGS_S1)) {
232 /* Bypass vendor specific and voice settings if enhanced eSCO supported */
233 if (!(bluetooth::shim::GetController()->IsSupported(
234 bluetooth::hci::OpCode::ENHANCED_SETUP_SYNCHRONOUS_CONNECTION))) {
235 BTM_WriteVoiceSettings(BTM_VOICE_SETTING_CVSD);
236 }
237
238 /* If SCO open was initiated by AG and failed for mSBC T2, try mSBC T1
239 * 'Safe setting' first. If T1 also fails, try CVSD
240 * same operations for LC3 settings */
241 if (bta_ag_sco_is_opening(bta_ag_cb.sco.p_curr_scb) &&
242 (!com::android::bluetooth::flags::fix_hfp_qual_1_9() ||
243 bta_ag_cb.sco.is_local)) {
244 /* Don't bother to edit |p_curr_scb->state| because it is in
245 * |BTA_AG_OPEN_ST|, which has the same value as |BTA_AG_SCO_CODEC_ST|
246 */
247 if (!com::android::bluetooth::flags::fix_hfp_qual_1_9()) {
248 bta_ag_cb.sco.p_curr_scb->state = (tBTA_AG_STATE)BTA_AG_SCO_CODEC_ST;
249 }
250 if (bta_ag_cb.sco.p_curr_scb->inuse_codec == UUID_CODEC_LC3) {
251 if (bta_ag_cb.sco.p_curr_scb->codec_lc3_settings ==
252 BTA_AG_SCO_LC3_SETTINGS_T2) {
253 log::warn(
254 "eSCO/SCO failed to open, falling back to LC3 T1 settings");
255 bta_ag_cb.sco.p_curr_scb->codec_lc3_settings =
256 BTA_AG_SCO_LC3_SETTINGS_T1;
257 } else {
258 log::warn("eSCO/SCO failed to open, falling back to CVSD settings");
259 bta_ag_cb.sco.p_curr_scb->inuse_codec = UUID_CODEC_CVSD;
260 bta_ag_cb.sco.p_curr_scb->codec_fallback = true;
261 }
262 } else if (bta_ag_cb.sco.p_curr_scb->inuse_codec == UUID_CODEC_MSBC ||
263 aptx_voice) {
264 if (bta_ag_cb.sco.p_curr_scb->codec_msbc_settings ==
265 BTA_AG_SCO_MSBC_SETTINGS_T2) {
266 log::warn(
267 "eSCO/SCO failed to open, falling back to mSBC T1 settings");
268 bta_ag_cb.sco.p_curr_scb->codec_msbc_settings =
269 BTA_AG_SCO_MSBC_SETTINGS_T1;
270
271 } else {
272 log::warn("eSCO/SCO failed to open, falling back to CVSD");
273 bta_ag_cb.sco.p_curr_scb->inuse_codec = UUID_CODEC_CVSD;
274 bta_ag_cb.sco.p_curr_scb->codec_fallback = true;
275 }
276 } else {
277 // Entering this block implies
278 // - |fix_hfp_qual_1_9| is enabled, AND
279 // - we just failed CVSD S2+.
280 log::warn(
281 "eSCO/SCO failed to open, falling back to CVSD S1 settings");
282 bta_ag_cb.sco.p_curr_scb->codec_cvsd_settings =
283 BTA_AG_SCO_CVSD_SETTINGS_S1;
284 bta_ag_cb.sco.p_curr_scb->trying_cvsd_safe_settings = true;
285 }
286 }
287 } else if (bta_ag_sco_is_opening(bta_ag_cb.sco.p_curr_scb) &&
288 (!com::android::bluetooth::flags::fix_hfp_qual_1_9() ||
289 bta_ag_cb.sco.is_local)) {
290 if (com::android::bluetooth::flags::
291 retry_esco_with_zero_retransmission_effort() &&
292 bta_ag_cb.sco.p_curr_scb->retransmission_effort_retries == 0) {
293 bta_ag_cb.sco.p_curr_scb->retransmission_effort_retries++;
294 if (!com::android::bluetooth::flags::fix_hfp_qual_1_9()) {
295 bta_ag_cb.sco.p_curr_scb->state = (tBTA_AG_STATE)BTA_AG_SCO_CODEC_ST;
296 }
297 log::warn("eSCO/SCO failed to open, retry with retransmission_effort");
298 } else {
299 log::error("eSCO/SCO failed to open, no more fall back");
300 if (bta_ag_is_sco_managed_by_audio()) {
301 hfp_offload_interface->CancelStreamingRequest();
302 }
303 }
304 }
305
306 bta_ag_cb.sco.p_curr_scb->inuse_codec = BTM_SCO_CODEC_NONE;
307
308 do_in_main_thread(
309 FROM_HERE, base::BindOnce(&bta_ag_sm_execute_by_handle, handle,
310 BTA_AG_SCO_CLOSE_EVT, tBTA_AG_DATA::kEmpty));
311 } else {
312 /* no match found */
313 log::verbose("no scb for ag_sco_disc_cback");
314
315 /* sco could be closed after scb dealloc'ed */
316 if (bta_ag_cb.sco.p_curr_scb != nullptr) {
317 bta_ag_cb.sco.p_curr_scb->sco_idx = BTM_INVALID_SCO_INDEX;
318 bta_ag_cb.sco.p_curr_scb = nullptr;
319 bta_ag_cb.sco.state = BTA_AG_SCO_SHUTDOWN_ST;
320 }
321 }
322 }
323
324 /*******************************************************************************
325 *
326 * Function bta_ag_remove_sco
327 *
328 * Description Removes the specified SCO from the system.
329 * If only_active is true, then SCO is only removed if
330 * connected
331 *
332 * Returns bool - true if SCO removal was started
333 *
334 ******************************************************************************/
bta_ag_remove_sco(tBTA_AG_SCB * p_scb,bool only_active)335 static bool bta_ag_remove_sco(tBTA_AG_SCB* p_scb, bool only_active) {
336 if (p_scb->sco_idx != BTM_INVALID_SCO_INDEX) {
337 if (!only_active || p_scb->sco_idx == bta_ag_cb.sco.cur_idx) {
338 tBTM_STATUS status = BTM_RemoveSco(p_scb->sco_idx);
339 log::debug("Removed SCO index:0x{:04x} status:{}", p_scb->sco_idx,
340 btm_status_text(status));
341 if (status == BTM_CMD_STARTED) {
342 /* SCO is connected; set current control block */
343 bta_ag_cb.sco.p_curr_scb = p_scb;
344 return true;
345 } else if ((status == BTM_SUCCESS) || (status == BTM_UNKNOWN_ADDR)) {
346 /* If no connection reset the SCO handle */
347 p_scb->sco_idx = BTM_INVALID_SCO_INDEX;
348 }
349 }
350 }
351 return false;
352 }
353
354 /*******************************************************************************
355 *
356 * Function bta_ag_esco_connreq_cback
357 *
358 * Description BTM eSCO connection requests and eSCO change requests
359 * Only the connection requests are processed by BTA.
360 *
361 * Returns void
362 *
363 ******************************************************************************/
bta_ag_esco_connreq_cback(tBTM_ESCO_EVT event,tBTM_ESCO_EVT_DATA * p_data)364 static void bta_ag_esco_connreq_cback(tBTM_ESCO_EVT event,
365 tBTM_ESCO_EVT_DATA* p_data) {
366 /* Only process connection requests */
367 if (event == BTM_ESCO_CONN_REQ_EVT) {
368 uint16_t sco_inx = p_data->conn_evt.sco_inx;
369 const RawAddress* remote_bda = BTM_ReadScoBdAddr(sco_inx);
370 tBTA_AG_SCB* p_scb = bta_ag_scb_by_idx(bta_ag_idx_by_bdaddr(remote_bda));
371 if (remote_bda && bta_ag_sco_is_active_device(*remote_bda) && p_scb &&
372 p_scb->svc_conn) {
373 p_scb->sco_idx = sco_inx;
374
375 /* If no other SCO active, allow this one */
376 if (!bta_ag_cb.sco.p_curr_scb) {
377 log::verbose("Accept Conn Request (sco_inx 0x{:04x})", sco_inx);
378 bta_ag_sco_conn_rsp(p_scb, &p_data->conn_evt);
379
380 bta_ag_cb.sco.state = BTA_AG_SCO_OPENING_ST;
381 bta_ag_cb.sco.p_curr_scb = p_scb;
382 bta_ag_cb.sco.cur_idx = p_scb->sco_idx;
383 } else {
384 /* Begin a transfer: Close current SCO before responding */
385 log::verbose("bta_ag_esco_connreq_cback: Begin XFER");
386 bta_ag_cb.sco.p_xfer_scb = p_scb;
387 bta_ag_cb.sco.conn_data = p_data->conn_evt;
388 bta_ag_cb.sco.state = BTA_AG_SCO_OPEN_XFER_ST;
389
390 if (!bta_ag_remove_sco(bta_ag_cb.sco.p_curr_scb, true)) {
391 log::error(
392 "Nothing to remove,so accept Conn Request(sco_inx 0x{:04x})",
393 sco_inx);
394 bta_ag_cb.sco.p_xfer_scb = nullptr;
395 bta_ag_cb.sco.state = BTA_AG_SCO_LISTEN_ST;
396
397 bta_ag_sco_conn_rsp(p_scb, &p_data->conn_evt);
398 }
399 }
400 } else {
401 log::warn(
402 "reject incoming SCO connection, remote_bda={}, active_bda={}, "
403 "current_bda={}",
404 remote_bda ? *remote_bda : RawAddress::kEmpty, active_device_addr,
405 p_scb ? p_scb->peer_addr : RawAddress::kEmpty);
406 BTM_EScoConnRsp(p_data->conn_evt.sco_inx, HCI_ERR_HOST_REJECT_RESOURCES,
407 (enh_esco_params_t*)nullptr);
408 }
409 }
410 }
411
412 /*******************************************************************************
413 *
414 * Function bta_ag_cback_sco
415 *
416 * Description Call application callback function with SCO event.
417 *
418 *
419 * Returns void
420 *
421 ******************************************************************************/
bta_ag_cback_sco(tBTA_AG_SCB * p_scb,tBTA_AG_EVT event)422 static void bta_ag_cback_sco(tBTA_AG_SCB* p_scb, tBTA_AG_EVT event) {
423 tBTA_AG_HDR sco = {};
424 sco.handle = bta_ag_scb_to_idx(p_scb);
425 sco.app_id = p_scb->app_id;
426 /* call close cback */
427 (*bta_ag_cb.p_cback)(static_cast<tBTA_AG_EVT>(event), (tBTA_AG*)&sco);
428 }
429
430 /*******************************************************************************
431 *
432 * Function bta_ag_create_sco
433 *
434 * Description Create a SCO connection for a given control block
435 * p_scb : Pointer to the target AG control block
436 * is_orig : Whether to initiate or listen for SCO connection
437 *
438 * Returns void
439 *
440 ******************************************************************************/
bta_ag_create_sco(tBTA_AG_SCB * p_scb,bool is_orig)441 void bta_ag_create_sco(tBTA_AG_SCB* p_scb, bool is_orig) {
442 log::debug("BEFORE {}", p_scb->ToString());
443 tBTA_AG_PEER_CODEC esco_codec = UUID_CODEC_CVSD;
444
445 if (!bta_ag_sco_is_active_device(p_scb->peer_addr)) {
446 log::warn("device {} is not active, active_device={}", p_scb->peer_addr,
447 active_device_addr);
448 if (bta_ag_cb.sco.p_curr_scb != nullptr &&
449 bta_ag_cb.sco.p_curr_scb->in_use && p_scb == bta_ag_cb.sco.p_curr_scb) {
450 do_in_main_thread(FROM_HERE, base::BindOnce(&bta_ag_sm_execute, p_scb,
451 BTA_AG_SCO_CLOSE_EVT,
452 tBTA_AG_DATA::kEmpty));
453 }
454 return;
455 }
456 /* Make sure this SCO handle is not already in use */
457 if (p_scb->sco_idx != BTM_INVALID_SCO_INDEX) {
458 log::error("device {}, index 0x{:04x} already in use!", p_scb->peer_addr,
459 p_scb->sco_idx);
460 return;
461 }
462
463 if ((p_scb->sco_codec == BTM_SCO_CODEC_MSBC) && !p_scb->codec_fallback &&
464 hfp_hal_interface::get_wbs_supported()) {
465 esco_codec = UUID_CODEC_MSBC;
466 }
467
468 if (is_hfp_aptx_voice_enabled()) {
469 if ((p_scb->sco_codec == BTA_AG_SCO_APTX_SWB_SETTINGS_Q0) &&
470 !p_scb->codec_fallback) {
471 esco_codec = BTA_AG_SCO_APTX_SWB_SETTINGS_Q0;
472 }
473 }
474
475 if ((p_scb->sco_codec == BTM_SCO_CODEC_LC3) && !p_scb->codec_fallback &&
476 hfp_hal_interface::get_swb_supported()) {
477 esco_codec = UUID_CODEC_LC3;
478 }
479
480 p_scb->trying_cvsd_safe_settings = false;
481
482 if (p_scb->codec_fallback) {
483 p_scb->codec_fallback = false;
484 /* Force AG to send +BCS for the next audio connection. */
485 p_scb->codec_updated = true;
486 /* reset to CVSD S4 settings as the preferred */
487 p_scb->codec_cvsd_settings = BTA_AG_SCO_CVSD_SETTINGS_S4;
488 /* Reset mSBC settings to T2 for the next audio connection */
489 p_scb->codec_msbc_settings = BTA_AG_SCO_MSBC_SETTINGS_T2;
490 /* Reset LC3 settings to T2 for the next audio connection */
491 p_scb->codec_lc3_settings = BTA_AG_SCO_LC3_SETTINGS_T2;
492 /* Reset SWB settings to Q3 for the next audio connection */
493 p_scb->codec_aptx_settings = BTA_AG_SCO_APTX_SWB_SETTINGS_Q0;
494 }
495
496 bool offload = hfp_hal_interface::get_offload_enabled();
497 /* Initialize eSCO parameters */
498 enh_esco_params_t params = {};
499 /* If SWB/WBS are excluded, use CVSD by default,
500 * index is 0 for CVSD by initialization.
501 * If eSCO codec is mSBC, index is T2 or T1.
502 * If eSCO coedc is LC3, index is T2 or T1. */
503 log::warn("esco_codec: {}", (int)esco_codec);
504 if (esco_codec == UUID_CODEC_LC3) {
505 if (p_scb->codec_lc3_settings == BTA_AG_SCO_LC3_SETTINGS_T2) {
506 params = esco_parameters_for_codec(ESCO_CODEC_LC3_T2, offload);
507 } else {
508 params = esco_parameters_for_codec(ESCO_CODEC_LC3_T1, offload);
509 }
510 } else if (is_hfp_aptx_voice_enabled() &&
511 (p_scb->is_aptx_swb_codec == true && !p_scb->codec_updated)) {
512 if (p_scb->codec_aptx_settings == BTA_AG_SCO_APTX_SWB_SETTINGS_Q3) {
513 params = esco_parameters_for_codec(ESCO_CODEC_SWB_Q3, true);
514 } else if (p_scb->codec_aptx_settings == BTA_AG_SCO_APTX_SWB_SETTINGS_Q2) {
515 params = esco_parameters_for_codec(ESCO_CODEC_SWB_Q2, true);
516 } else if (p_scb->codec_aptx_settings == BTA_AG_SCO_APTX_SWB_SETTINGS_Q1) {
517 params = esco_parameters_for_codec(ESCO_CODEC_SWB_Q1, true);
518 } else if (p_scb->codec_aptx_settings == BTA_AG_SCO_APTX_SWB_SETTINGS_Q0) {
519 params = esco_parameters_for_codec(ESCO_CODEC_SWB_Q0, true);
520 }
521 } else if (esco_codec == UUID_CODEC_MSBC) {
522 if (p_scb->codec_msbc_settings == BTA_AG_SCO_MSBC_SETTINGS_T2) {
523 params = esco_parameters_for_codec(ESCO_CODEC_MSBC_T2, offload);
524 } else {
525 params = esco_parameters_for_codec(ESCO_CODEC_MSBC_T1, offload);
526 }
527 } else {
528 if (com::android::bluetooth::flags::fix_hfp_qual_1_9() &&
529 p_scb->codec_cvsd_settings == BTA_AG_SCO_CVSD_SETTINGS_S1) {
530 params = esco_parameters_for_codec(ESCO_CODEC_CVSD_S1, offload);
531 } else {
532 if ((p_scb->features & BTA_AG_FEAT_ESCO_S4) &&
533 (p_scb->peer_features & BTA_AG_PEER_FEAT_ESCO_S4)) {
534 // HFP >=1.7 eSCO
535 params = esco_parameters_for_codec(ESCO_CODEC_CVSD_S4, offload);
536 } else {
537 // HFP <=1.6 eSCO
538 params = esco_parameters_for_codec(ESCO_CODEC_CVSD_S3, offload);
539 }
540 }
541 }
542
543 updateCodecParametersFromProviderInfo(esco_codec, params);
544
545 if (com::android::bluetooth::flags::
546 retry_esco_with_zero_retransmission_effort() &&
547 p_scb->retransmission_effort_retries == 1) {
548 log::info("change retransmission_effort to 0, retry");
549 p_scb->retransmission_effort_retries++;
550 params.retransmission_effort = ESCO_RETRANSMISSION_OFF;
551 }
552
553 /* Configure input/output data path based on HAL settings. */
554 hfp_hal_interface::set_codec_datapath(esco_codec);
555 hfp_hal_interface::update_esco_parameters(¶ms);
556
557 /* If initiating, setup parameters to start SCO/eSCO connection */
558 if (is_orig) {
559 bta_ag_cb.sco.is_local = true;
560 /* Set eSCO Mode */
561 if (get_btm_client_interface().sco.BTM_SetEScoMode(¶ms) !=
562 BTM_SUCCESS) {
563 log::warn("Unable to set ESCO mode");
564 }
565 bta_ag_cb.sco.p_curr_scb = p_scb;
566 /* save the current codec as sco_codec can be updated while SCO is open. */
567 p_scb->inuse_codec = esco_codec;
568
569 /* tell sys to stop av if any */
570 bta_sys_sco_use(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
571
572 bta_ag_cb.sco.cur_idx = p_scb->sco_idx;
573
574 /* Bypass voice settings if enhanced SCO setup command is supported */
575 if (!(bluetooth::shim::GetController()->IsSupported(
576 bluetooth::hci::OpCode::ENHANCED_SETUP_SYNCHRONOUS_CONNECTION))) {
577 if (esco_codec == UUID_CODEC_MSBC || esco_codec == UUID_CODEC_LC3) {
578 BTM_WriteVoiceSettings(BTM_VOICE_SETTING_TRANS);
579 } else {
580 BTM_WriteVoiceSettings(BTM_VOICE_SETTING_CVSD);
581 }
582 }
583
584 if (BTM_CreateSco(&p_scb->peer_addr, true, params.packet_types,
585 &p_scb->sco_idx, bta_ag_sco_conn_cback,
586 bta_ag_sco_disc_cback) == BTM_CMD_STARTED) {
587 /* Initiating the connection, set the current sco handle */
588 bta_ag_cb.sco.cur_idx = p_scb->sco_idx;
589 /* Configure input/output data. */
590 hfp_hal_interface::set_codec_datapath(esco_codec);
591 log::verbose("initiated SCO connection");
592 }
593
594 log::debug("Initiating AG SCO inx 0x{:04x}, pkt types 0x{:04x}",
595 p_scb->sco_idx, params.packet_types);
596 } else {
597 /* Not initiating, go to listen mode */
598 tBTM_STATUS btm_status = BTM_CreateSco(
599 &p_scb->peer_addr, false, params.packet_types, &p_scb->sco_idx,
600 bta_ag_sco_conn_cback, bta_ag_sco_disc_cback);
601 if (btm_status == BTM_CMD_STARTED) {
602 if (get_btm_client_interface().sco.BTM_RegForEScoEvts(
603 p_scb->sco_idx, bta_ag_esco_connreq_cback) != BTM_SUCCESS) {
604 log::warn("Unable to register for ESCO events");
605 }
606 }
607 log::debug("Listening AG SCO inx 0x{:04x} status:{} pkt types 0x{:04x}",
608 p_scb->sco_idx, btm_status_text(btm_status),
609 params.packet_types);
610 }
611 log::debug("AFTER {}", p_scb->ToString());
612 }
613
updateCodecParametersFromProviderInfo(tBTA_AG_PEER_CODEC esco_codec,enh_esco_params_t & params)614 void updateCodecParametersFromProviderInfo(tBTA_AG_PEER_CODEC esco_codec,
615 enh_esco_params_t& params) {
616 if (bta_ag_is_sco_managed_by_audio() && !sco_config_map.empty()) {
617 auto sco_config_it = sco_config_map.find(esco_codec);
618 if (sco_config_it == sco_config_map.end()) {
619 log::error("cannot find sco config for esco_codec index={}", esco_codec);
620 return;
621 }
622 log::debug("use ProviderInfo to update (e)sco parameters");
623 params.input_data_path = sco_config_it->second.inputDataPath;
624 params.output_data_path = sco_config_it->second.outputDataPath;
625 if (!sco_config_it->second.useControllerCodec) {
626 log::debug("use DSP Codec instead of controller codec");
627
628 esco_coding_format_t codingFormat = codec_coding_format_map[esco_codec];
629 params.input_coding_format.coding_format = codingFormat;
630 params.output_coding_format.coding_format = codingFormat;
631 params.input_bandwidth = TXRX_64KBITS_RATE;
632 params.output_bandwidth = TXRX_64KBITS_RATE;
633 }
634 }
635 }
636
637 /*******************************************************************************
638 *
639 * Function bta_ag_codec_negotiation_timer_cback
640 *
641 * Description
642 *
643 *
644 * Returns void
645 *
646 ******************************************************************************/
bta_ag_codec_negotiation_timer_cback(void * data)647 static void bta_ag_codec_negotiation_timer_cback(void* data) {
648 log::warn("Codec negotiation timeout");
649 tBTA_AG_SCB* p_scb = (tBTA_AG_SCB*)data;
650
651 /* Announce that codec negotiation failed. */
652 bta_ag_sco_codec_nego(p_scb, false);
653
654 /* call app callback */
655 bta_ag_cback_sco(p_scb, BTA_AG_AUDIO_CLOSE_EVT);
656 }
657
658 /*******************************************************************************
659 *
660 * Function bta_ag_codec_negotiate
661 *
662 * Description Initiate codec negotiation by sending AT command.
663 * If not necessary, skip negotiation.
664 *
665 * Returns void
666 *
667 ******************************************************************************/
bta_ag_codec_negotiate(tBTA_AG_SCB * p_scb)668 void bta_ag_codec_negotiate(tBTA_AG_SCB* p_scb) {
669 bta_ag_cb.sco.p_curr_scb = p_scb;
670 uint8_t* p_rem_feat = BTM_ReadRemoteFeatures(p_scb->peer_addr);
671 bool sdp_wbs_support = p_scb->peer_sdp_features & BTA_AG_FEAT_WBS_SUPPORT;
672
673 if (p_rem_feat == nullptr) {
674 log::warn("Skip codec negotiation, failed to read remote features");
675 bta_ag_sco_codec_nego(p_scb, false);
676 return;
677 }
678
679 // Workaround for misbehaving HFs, which indicate which one is not support on
680 // Transparent Synchronous Data in Remote Supported Features, WBS in SDP and
681 // and Codec Negotiation in BRSF. Fluoride will assume CVSD codec by default.
682 // In Sony XAV AX100 car kit and Sony MW600 Headset case, which indicate
683 // Transparent Synchronous Data and WBS support, but no codec negotiation
684 // support, using mSBC codec can result background noise or no audio.
685 // In Skullcandy JIB case, which indicate WBS and codec negotiation support,
686 // but no Transparent Synchronous Data support, using mSBC codec can result
687 // SCO setup fail by Firmware reject.
688 if (!HCI_LMP_TRANSPNT_SUPPORTED(p_rem_feat) || !sdp_wbs_support ||
689 !(p_scb->peer_features & BTA_AG_PEER_FEAT_CODEC)) {
690 log::info("Assume CVSD by default due to mask mismatch");
691 p_scb->sco_codec = UUID_CODEC_CVSD;
692 }
693 const bool aptx_voice =
694 is_hfp_aptx_voice_enabled() &&
695 (get_swb_codec_status(bluetooth::headset::BTHF_SWB_CODEC_VENDOR_APTX,
696 &p_scb->peer_addr) ||
697 p_scb->is_aptx_swb_codec);
698 log::verbose(
699 "aptx_voice={}, is_aptx_swb_codec={}, Q0 codec supported={}", aptx_voice,
700 p_scb->is_aptx_swb_codec,
701 (p_scb->peer_codecs & BTA_AG_SCO_APTX_SWB_SETTINGS_Q0_MASK) != 0);
702
703 if (((p_scb->codec_updated || p_scb->codec_fallback) &&
704 (p_scb->features & BTA_AG_FEAT_CODEC) &&
705 (p_scb->peer_features & BTA_AG_PEER_FEAT_CODEC)) ||
706 (aptx_voice)) {
707 log::info("Starting codec negotiation");
708 /* Change the power mode to Active until SCO open is completed. */
709 bta_sys_busy(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
710
711 if (get_swb_codec_status(bluetooth::headset::BTHF_SWB_CODEC_VENDOR_APTX,
712 &p_scb->peer_addr) &&
713 (p_scb->peer_codecs & BTA_AG_SCO_APTX_SWB_SETTINGS_Q0_MASK)) {
714 if (p_scb->is_aptx_swb_codec == false) {
715 p_scb->sco_codec = BTA_AG_SCO_APTX_SWB_SETTINGS_Q0;
716 p_scb->is_aptx_swb_codec = true;
717 }
718 log::verbose("Sending +QCS, sco_codec={}, is_aptx_swb_codec={}",
719 p_scb->sco_codec, p_scb->is_aptx_swb_codec);
720 /* Send +QCS to the peer */
721 bta_ag_send_qcs(p_scb, NULL);
722 } else {
723 if (aptx_voice) {
724 p_scb->sco_codec = BTM_SCO_CODEC_MSBC;
725 p_scb->is_aptx_swb_codec = false;
726 }
727 log::verbose("Sending +BCS, sco_codec={}, is_aptx_swb_codec={}",
728 p_scb->sco_codec, p_scb->is_aptx_swb_codec);
729 /* Send +BCS to the peer */
730 bta_ag_send_bcs(p_scb);
731 }
732
733 /* Start timer to handle timeout */
734 alarm_set_on_mloop(p_scb->codec_negotiation_timer,
735 BTA_AG_CODEC_NEGOTIATION_TIMEOUT_MS,
736 bta_ag_codec_negotiation_timer_cback, p_scb);
737 } else {
738 /* use same codec type as previous SCO connection, skip codec negotiation */
739 log::info("Skip codec negotiation, using the same codec");
740 bta_ag_sco_codec_nego(p_scb, true);
741 }
742 }
743
bta_ag_sco_event(tBTA_AG_SCB * p_scb,uint8_t event)744 static void bta_ag_sco_event(tBTA_AG_SCB* p_scb, uint8_t event) {
745 tBTA_AG_SCO_CB* p_sco = &bta_ag_cb.sco;
746 uint8_t previous_state = p_sco->state;
747 log::info("device:{} index:0x{:04x} state:{}[{}] event:{}[{}]",
748 p_scb->peer_addr, p_scb->sco_idx,
749 bta_ag_sco_state_str(p_sco->state), p_sco->state,
750 bta_ag_sco_evt_str(event), event);
751
752 switch (p_sco->state) {
753 case BTA_AG_SCO_SHUTDOWN_ST:
754 switch (event) {
755 case BTA_AG_SCO_LISTEN_E:
756 /* create sco listen connection */
757 bta_ag_create_sco(p_scb, false);
758 p_sco->state = BTA_AG_SCO_LISTEN_ST;
759 break;
760
761 default:
762 log::warn("BTA_AG_SCO_SHUTDOWN_ST: Ignoring event {}[{}]",
763 bta_ag_sco_evt_str(event), event);
764 break;
765 }
766 break;
767
768 case BTA_AG_SCO_LISTEN_ST:
769 switch (event) {
770 case BTA_AG_SCO_LISTEN_E:
771 /* create sco listen connection (Additional channel) */
772 bta_ag_create_sco(p_scb, false);
773 break;
774
775 case BTA_AG_SCO_OPEN_E:
776 /* remove listening connection */
777 bta_ag_remove_sco(p_scb, false);
778
779 /* start codec negotiation */
780 p_sco->state = BTA_AG_SCO_CODEC_ST;
781 bta_ag_codec_negotiate(p_scb);
782 break;
783
784 case BTA_AG_SCO_SHUTDOWN_E:
785 /* remove listening connection */
786 bta_ag_remove_sco(p_scb, false);
787
788 if (p_scb == p_sco->p_curr_scb) p_sco->p_curr_scb = nullptr;
789
790 /* If last SCO instance then finish shutting down */
791 if (!bta_ag_other_scb_open(p_scb)) {
792 p_sco->state = BTA_AG_SCO_SHUTDOWN_ST;
793 }
794 break;
795
796 case BTA_AG_SCO_CLOSE_E:
797 /* remove listening connection */
798 /* Ignore the event. Keep listening SCO for the active SLC */
799 log::warn("BTA_AG_SCO_LISTEN_ST: Ignoring event {}[{}]",
800 bta_ag_sco_evt_str(event), event);
801 break;
802
803 case BTA_AG_SCO_CONN_CLOSE_E:
804 /* sco failed; create sco listen connection */
805 bta_ag_create_sco(p_scb, false);
806 p_sco->state = BTA_AG_SCO_LISTEN_ST;
807 break;
808
809 default:
810 log::warn("BTA_AG_SCO_LISTEN_ST: Ignoring event {}[{}]",
811 bta_ag_sco_evt_str(event), event);
812 break;
813 }
814 break;
815
816 case BTA_AG_SCO_CODEC_ST:
817 switch (event) {
818 case BTA_AG_SCO_LISTEN_E:
819 /* create sco listen connection (Additional channel) */
820 bta_ag_create_sco(p_scb, false);
821 break;
822
823 case BTA_AG_SCO_CN_DONE_E:
824 /* create sco connection to peer */
825 bta_ag_create_sco(p_scb, true);
826 p_sco->state = BTA_AG_SCO_OPENING_ST;
827 break;
828
829 case BTA_AG_SCO_XFER_E:
830 /* save xfer scb */
831 p_sco->p_xfer_scb = p_scb;
832 p_sco->state = BTA_AG_SCO_CLOSE_XFER_ST;
833 break;
834
835 case BTA_AG_SCO_SHUTDOWN_E:
836 /* remove listening connection */
837 bta_ag_remove_sco(p_scb, false);
838
839 if (p_scb == p_sco->p_curr_scb) p_sco->p_curr_scb = nullptr;
840
841 /* If last SCO instance then finish shutting down */
842 if (!bta_ag_other_scb_open(p_scb)) {
843 p_sco->state = BTA_AG_SCO_SHUTDOWN_ST;
844 }
845 break;
846
847 case BTA_AG_SCO_CLOSE_E:
848 if (bluetooth::common::init_flags::
849 sco_codec_timeout_clear_is_enabled()) {
850 /* remove listening connection */
851 bta_ag_remove_sco(p_scb, false);
852
853 if (p_scb == p_sco->p_curr_scb) p_sco->p_curr_scb = nullptr;
854
855 bta_ag_create_sco(p_scb, false);
856 }
857 /* sco open is not started yet. just go back to listening */
858 p_sco->state = BTA_AG_SCO_LISTEN_ST;
859 break;
860
861 case BTA_AG_SCO_CONN_CLOSE_E:
862 /* sco failed; create sco listen connection */
863 bta_ag_create_sco(p_scb, false);
864 p_sco->state = BTA_AG_SCO_LISTEN_ST;
865 break;
866
867 default:
868 log::warn("BTA_AG_SCO_CODEC_ST: Ignoring event {}[{}]",
869 bta_ag_sco_evt_str(event), event);
870 break;
871 }
872 break;
873
874 case BTA_AG_SCO_OPENING_ST:
875 switch (event) {
876 case BTA_AG_SCO_LISTEN_E:
877 /* second headset has now joined */
878 /* create sco listen connection (Additional channel) */
879 if (p_scb != p_sco->p_curr_scb) {
880 bta_ag_create_sco(p_scb, false);
881 }
882 break;
883
884 case BTA_AG_SCO_REOPEN_E:
885 /* start codec negotiation */
886 p_sco->state = BTA_AG_SCO_CODEC_ST;
887 bta_ag_codec_negotiate(p_scb);
888 break;
889
890 case BTA_AG_SCO_XFER_E:
891 /* save xfer scb */
892 p_sco->p_xfer_scb = p_scb;
893 p_sco->state = BTA_AG_SCO_CLOSE_XFER_ST;
894 break;
895
896 case BTA_AG_SCO_CLOSE_E:
897 p_sco->state = BTA_AG_SCO_OPEN_CL_ST;
898 break;
899
900 case BTA_AG_SCO_SHUTDOWN_E:
901 /* If not opening scb, just close it */
902 if (p_scb != p_sco->p_curr_scb) {
903 /* remove listening connection */
904 bta_ag_remove_sco(p_scb, false);
905 } else
906 p_sco->state = BTA_AG_SCO_SHUTTING_ST;
907
908 break;
909
910 case BTA_AG_SCO_CONN_OPEN_E:
911 p_sco->state = BTA_AG_SCO_OPEN_ST;
912 break;
913
914 case BTA_AG_SCO_CONN_CLOSE_E:
915 /* sco failed; create sco listen connection */
916 bta_ag_create_sco(p_scb, false);
917 p_sco->state = BTA_AG_SCO_LISTEN_ST;
918 break;
919
920 default:
921 log::warn("BTA_AG_SCO_OPENING_ST: Ignoring event {}[{}]",
922 bta_ag_sco_evt_str(event), event);
923 break;
924 }
925 break;
926
927 case BTA_AG_SCO_OPEN_CL_ST:
928 switch (event) {
929 case BTA_AG_SCO_XFER_E:
930 /* save xfer scb */
931 p_sco->p_xfer_scb = p_scb;
932
933 p_sco->state = BTA_AG_SCO_CLOSE_XFER_ST;
934 break;
935
936 case BTA_AG_SCO_OPEN_E:
937 p_sco->state = BTA_AG_SCO_OPENING_ST;
938 break;
939
940 case BTA_AG_SCO_SHUTDOWN_E:
941 /* If not opening scb, just close it */
942 if (p_scb != p_sco->p_curr_scb) {
943 /* remove listening connection */
944 bta_ag_remove_sco(p_scb, false);
945 } else
946 p_sco->state = BTA_AG_SCO_SHUTTING_ST;
947
948 break;
949
950 case BTA_AG_SCO_CONN_OPEN_E:
951 /* close sco connection */
952 bta_ag_remove_sco(p_scb, true);
953
954 p_sco->state = BTA_AG_SCO_CLOSING_ST;
955 break;
956
957 case BTA_AG_SCO_CONN_CLOSE_E:
958 /* sco failed; create sco listen connection */
959
960 p_sco->state = BTA_AG_SCO_LISTEN_ST;
961 break;
962
963 default:
964 log::warn("BTA_AG_SCO_OPEN_CL_ST: Ignoring event {}[{}]",
965 bta_ag_sco_evt_str(event), event);
966 break;
967 }
968 break;
969
970 case BTA_AG_SCO_OPEN_XFER_ST:
971 switch (event) {
972 case BTA_AG_SCO_CLOSE_E:
973 /* close sco connection */
974 bta_ag_remove_sco(p_scb, true);
975
976 p_sco->state = BTA_AG_SCO_CLOSING_ST;
977 break;
978
979 case BTA_AG_SCO_SHUTDOWN_E:
980 /* remove all connection */
981 bta_ag_remove_sco(p_scb, false);
982 p_sco->state = BTA_AG_SCO_SHUTTING_ST;
983
984 break;
985
986 case BTA_AG_SCO_CONN_CLOSE_E:
987 /* closed sco; place in listen mode and
988 accept the transferred connection */
989 bta_ag_create_sco(p_scb, false); /* Back into listen mode */
990
991 /* Accept sco connection with xfer scb */
992 bta_ag_sco_conn_rsp(p_sco->p_xfer_scb, &p_sco->conn_data);
993 p_sco->state = BTA_AG_SCO_OPENING_ST;
994 p_sco->p_curr_scb = p_sco->p_xfer_scb;
995 p_sco->cur_idx = p_sco->p_xfer_scb->sco_idx;
996 p_sco->p_xfer_scb = nullptr;
997 break;
998
999 default:
1000 log::warn("BTA_AG_SCO_OPEN_XFER_ST: Ignoring event {}[{}]",
1001 bta_ag_sco_evt_str(event), event);
1002 break;
1003 }
1004 break;
1005
1006 case BTA_AG_SCO_OPEN_ST:
1007 switch (event) {
1008 case BTA_AG_SCO_LISTEN_E:
1009 /* second headset has now joined */
1010 /* create sco listen connection (Additional channel) */
1011 if (p_scb != p_sco->p_curr_scb) {
1012 bta_ag_create_sco(p_scb, false);
1013 }
1014 break;
1015
1016 case BTA_AG_SCO_XFER_E:
1017 /* close current sco connection */
1018 bta_ag_remove_sco(p_sco->p_curr_scb, true);
1019
1020 /* save xfer scb */
1021 p_sco->p_xfer_scb = p_scb;
1022
1023 p_sco->state = BTA_AG_SCO_CLOSE_XFER_ST;
1024 break;
1025
1026 case BTA_AG_SCO_CLOSE_E:
1027 /* close sco connection if active */
1028 if (bta_ag_remove_sco(p_scb, true)) {
1029 p_sco->state = BTA_AG_SCO_CLOSING_ST;
1030 }
1031 break;
1032
1033 case BTA_AG_SCO_SHUTDOWN_E:
1034 /* remove all listening connections */
1035 bta_ag_remove_sco(p_scb, false);
1036
1037 /* If SCO was active on this scb, close it */
1038 if (p_scb == p_sco->p_curr_scb) {
1039 p_sco->state = BTA_AG_SCO_SHUTTING_ST;
1040 }
1041 break;
1042
1043 case BTA_AG_SCO_CONN_CLOSE_E:
1044 /* peer closed sco; create sco listen connection */
1045 bta_ag_create_sco(p_scb, false);
1046 p_sco->state = BTA_AG_SCO_LISTEN_ST;
1047 break;
1048
1049 default:
1050 log::warn("BTA_AG_SCO_OPEN_ST: Ignoring event {}[{}]",
1051 bta_ag_sco_evt_str(event), event);
1052 break;
1053 }
1054 break;
1055
1056 case BTA_AG_SCO_CLOSING_ST:
1057 switch (event) {
1058 case BTA_AG_SCO_LISTEN_E:
1059 /* create sco listen connection (Additional channel) */
1060 if (p_scb != p_sco->p_curr_scb) {
1061 bta_ag_create_sco(p_scb, false);
1062 }
1063 break;
1064
1065 case BTA_AG_SCO_OPEN_E:
1066 p_sco->state = BTA_AG_SCO_CLOSE_OP_ST;
1067 break;
1068
1069 case BTA_AG_SCO_XFER_E:
1070 /* save xfer scb */
1071 p_sco->p_xfer_scb = p_scb;
1072
1073 p_sco->state = BTA_AG_SCO_CLOSE_XFER_ST;
1074 break;
1075
1076 case BTA_AG_SCO_SHUTDOWN_E:
1077 /* If not closing scb, just close it */
1078 if (p_scb != p_sco->p_curr_scb) {
1079 /* remove listening connection */
1080 bta_ag_remove_sco(p_scb, false);
1081 } else
1082 p_sco->state = BTA_AG_SCO_SHUTTING_ST;
1083
1084 break;
1085
1086 case BTA_AG_SCO_CONN_CLOSE_E:
1087 /* peer closed sco; create sco listen connection */
1088 bta_ag_create_sco(p_scb, false);
1089
1090 p_sco->state = BTA_AG_SCO_LISTEN_ST;
1091 break;
1092
1093 default:
1094 log::warn("BTA_AG_SCO_CLOSING_ST: Ignoring event {}[{}]",
1095 bta_ag_sco_evt_str(event), event);
1096 break;
1097 }
1098 break;
1099
1100 case BTA_AG_SCO_CLOSE_OP_ST:
1101 switch (event) {
1102 case BTA_AG_SCO_CLOSE_E:
1103 p_sco->state = BTA_AG_SCO_CLOSING_ST;
1104 break;
1105
1106 case BTA_AG_SCO_SHUTDOWN_E:
1107 p_sco->state = BTA_AG_SCO_SHUTTING_ST;
1108 break;
1109
1110 case BTA_AG_SCO_CONN_CLOSE_E:
1111 /* start codec negotiation */
1112 p_sco->state = BTA_AG_SCO_CODEC_ST;
1113 bta_ag_codec_negotiate(p_scb);
1114 break;
1115
1116 case BTA_AG_SCO_LISTEN_E:
1117 /* create sco listen connection (Additional channel) */
1118 if (p_scb != p_sco->p_curr_scb) {
1119 bta_ag_create_sco(p_scb, false);
1120 }
1121 break;
1122
1123 default:
1124 log::warn("BTA_AG_SCO_CLOSE_OP_ST: Ignoring event {}[{}]",
1125 bta_ag_sco_evt_str(event), event);
1126 break;
1127 }
1128 break;
1129
1130 case BTA_AG_SCO_CLOSE_XFER_ST:
1131 switch (event) {
1132 case BTA_AG_SCO_CONN_OPEN_E:
1133 /* close sco connection so headset can be transferred
1134 Probably entered this state from "opening state" */
1135 bta_ag_remove_sco(p_scb, true);
1136 break;
1137
1138 case BTA_AG_SCO_CLOSE_E:
1139 /* clear xfer scb */
1140 p_sco->p_xfer_scb = nullptr;
1141
1142 p_sco->state = BTA_AG_SCO_CLOSING_ST;
1143 break;
1144
1145 case BTA_AG_SCO_SHUTDOWN_E:
1146 /* clear xfer scb */
1147 p_sco->p_xfer_scb = nullptr;
1148
1149 p_sco->state = BTA_AG_SCO_SHUTTING_ST;
1150 break;
1151
1152 case BTA_AG_SCO_CN_DONE_E:
1153 case BTA_AG_SCO_CONN_CLOSE_E: {
1154 /* closed sco; place old sco in listen mode,
1155 take current sco out of listen, and
1156 create originating sco for current */
1157 bta_ag_create_sco(p_scb, false);
1158 bta_ag_remove_sco(p_sco->p_xfer_scb, false);
1159
1160 /* start codec negotiation */
1161 p_sco->state = BTA_AG_SCO_CODEC_ST;
1162 tBTA_AG_SCB* p_cn_scb = p_sco->p_xfer_scb;
1163 p_sco->p_xfer_scb = nullptr;
1164 bta_ag_codec_negotiate(p_cn_scb);
1165 break;
1166 }
1167
1168 default:
1169 log::warn("BTA_AG_SCO_CLOSE_XFER_ST: Ignoring event {}[{}]",
1170 bta_ag_sco_evt_str(event), event);
1171 break;
1172 }
1173 break;
1174
1175 case BTA_AG_SCO_SHUTTING_ST:
1176 switch (event) {
1177 case BTA_AG_SCO_CONN_OPEN_E:
1178 /* close sco connection; wait for conn close event */
1179 bta_ag_remove_sco(p_scb, true);
1180 break;
1181
1182 case BTA_AG_SCO_CONN_CLOSE_E:
1183 /* If last SCO instance then finish shutting down */
1184 if (!bta_ag_other_scb_open(p_scb)) {
1185 p_sco->state = BTA_AG_SCO_SHUTDOWN_ST;
1186 bta_sys_sco_unuse(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
1187 } else /* Other instance is still listening */
1188 {
1189 p_sco->state = BTA_AG_SCO_LISTEN_ST;
1190 }
1191
1192 /* If SCO closed for other HS which is not being disconnected,
1193 then create listen sco connection for it as scb still open */
1194 if (bta_ag_scb_open(p_scb)) {
1195 bta_ag_create_sco(p_scb, false);
1196 p_sco->state = BTA_AG_SCO_LISTEN_ST;
1197 }
1198
1199 if (p_scb == p_sco->p_curr_scb) {
1200 p_sco->p_curr_scb->sco_idx = BTM_INVALID_SCO_INDEX;
1201 p_sco->p_curr_scb = nullptr;
1202 }
1203 break;
1204
1205 case BTA_AG_SCO_LISTEN_E:
1206 /* create sco listen connection (Additional channel) */
1207 if (p_scb != p_sco->p_curr_scb) {
1208 bta_ag_create_sco(p_scb, false);
1209 }
1210 break;
1211
1212 case BTA_AG_SCO_SHUTDOWN_E:
1213 if (!bta_ag_other_scb_open(p_scb)) {
1214 p_sco->state = BTA_AG_SCO_SHUTDOWN_ST;
1215 } else /* Other instance is still listening */
1216 {
1217 p_sco->state = BTA_AG_SCO_LISTEN_ST;
1218 }
1219
1220 if (p_scb == p_sco->p_curr_scb) {
1221 p_sco->p_curr_scb->sco_idx = BTM_INVALID_SCO_INDEX;
1222 p_sco->p_curr_scb = nullptr;
1223 }
1224 break;
1225
1226 default:
1227 log::warn("BTA_AG_SCO_SHUTTING_ST: Ignoring event {}[{}]",
1228 bta_ag_sco_evt_str(event), event);
1229 break;
1230 }
1231 break;
1232
1233 default:
1234 break;
1235 }
1236 if (p_sco->state != previous_state) {
1237 log::warn(
1238 "SCO_state_change: [{}(0x{:02x})]->[{}(0x{:02x})] after event "
1239 "[{}(0x{:02x})]",
1240 bta_ag_sco_state_str(previous_state), previous_state,
1241 bta_ag_sco_state_str(p_sco->state), p_sco->state,
1242 bta_ag_sco_evt_str(event), event);
1243 }
1244 }
1245
1246 /*******************************************************************************
1247 *
1248 * Function bta_ag_sco_is_open
1249 *
1250 * Description Check if sco is open for this scb.
1251 *
1252 *
1253 * Returns true if sco open for this scb, false otherwise.
1254 *
1255 ******************************************************************************/
bta_ag_sco_is_open(tBTA_AG_SCB * p_scb)1256 bool bta_ag_sco_is_open(tBTA_AG_SCB* p_scb) {
1257 return ((bta_ag_cb.sco.state == BTA_AG_SCO_OPEN_ST) &&
1258 (bta_ag_cb.sco.p_curr_scb == p_scb));
1259 }
1260
1261 /*******************************************************************************
1262 *
1263 * Function bta_ag_sco_is_opening
1264 *
1265 * Description Check if sco is in Opening state.
1266 *
1267 *
1268 * Returns true if sco is in Opening state for this scb, false
1269 * otherwise.
1270 *
1271 ******************************************************************************/
bta_ag_sco_is_opening(tBTA_AG_SCB * p_scb)1272 bool bta_ag_sco_is_opening(tBTA_AG_SCB* p_scb) {
1273 return ((bta_ag_cb.sco.state == BTA_AG_SCO_OPENING_ST) &&
1274 (bta_ag_cb.sco.p_curr_scb == p_scb));
1275 }
1276
1277 /*******************************************************************************
1278 *
1279 * Function bta_ag_sco_listen
1280 *
1281 * Description
1282 *
1283 *
1284 * Returns void
1285 *
1286 ******************************************************************************/
bta_ag_sco_listen(tBTA_AG_SCB * p_scb,const tBTA_AG_DATA &)1287 void bta_ag_sco_listen(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& /* data */) {
1288 log::info("{}", p_scb->peer_addr);
1289 bta_ag_sco_event(p_scb, BTA_AG_SCO_LISTEN_E);
1290 }
1291
1292 /*******************************************************************************
1293 *
1294 * Function bta_ag_sco_open
1295 *
1296 * Description
1297 *
1298 *
1299 * Returns void
1300 *
1301 ******************************************************************************/
bta_ag_sco_open(tBTA_AG_SCB * p_scb,const tBTA_AG_DATA & data)1302 void bta_ag_sco_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data) {
1303 if (!sco_allowed) {
1304 log::info("not opening sco, by policy");
1305 return;
1306 }
1307
1308 p_scb->disabled_codecs = data.api_audio_open.disabled_codecs;
1309 log::info("disabled_codecs = {}, sco_codec = {}", p_scb->disabled_codecs,
1310 p_scb->sco_codec);
1311
1312 if (p_scb->disabled_codecs & p_scb->sco_codec) {
1313 tBTA_AG_PEER_CODEC updated_codec = BTM_SCO_CODEC_NONE;
1314
1315 if (hfp_hal_interface::get_swb_supported() &&
1316 (p_scb->peer_codecs & BTM_SCO_CODEC_LC3) &&
1317 !(p_scb->disabled_codecs & BTM_SCO_CODEC_LC3)) {
1318 updated_codec = BTM_SCO_CODEC_LC3;
1319 } else if ((p_scb->peer_codecs & BTM_SCO_CODEC_MSBC) &&
1320 !(p_scb->disabled_codecs & BTM_SCO_CODEC_MSBC)) {
1321 updated_codec = BTM_SCO_CODEC_MSBC;
1322 } else {
1323 updated_codec = BTM_SCO_CODEC_CVSD;
1324 }
1325
1326 p_scb->sco_codec = updated_codec;
1327 p_scb->codec_updated = true;
1328 }
1329
1330 /* if another scb using sco, this is a transfer */
1331 if (bta_ag_cb.sco.p_curr_scb && bta_ag_cb.sco.p_curr_scb != p_scb) {
1332 log::info("transfer {} -> {}", bta_ag_cb.sco.p_curr_scb->peer_addr,
1333 p_scb->peer_addr);
1334 bta_ag_sco_event(p_scb, BTA_AG_SCO_XFER_E);
1335 } else {
1336 /* else it is an open */
1337 log::info("open {}", p_scb->peer_addr);
1338 bta_ag_sco_event(p_scb, BTA_AG_SCO_OPEN_E);
1339 }
1340 }
1341
1342 /*******************************************************************************
1343 *
1344 * Function bta_ag_sco_close
1345 *
1346 * Description
1347 *
1348 *
1349 * Returns void
1350 *
1351 ******************************************************************************/
bta_ag_sco_close(tBTA_AG_SCB * p_scb,const tBTA_AG_DATA &)1352 void bta_ag_sco_close(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& /* data */) {
1353 /* if scb is in use */
1354 /* sco_idx is not allocated in SCO_CODEC_ST, still need to move to listen
1355 * state. */
1356 if ((p_scb->sco_idx != BTM_INVALID_SCO_INDEX) ||
1357 (bta_ag_cb.sco.state == BTA_AG_SCO_CODEC_ST)) {
1358 log::verbose("bta_ag_sco_close: sco_inx = {}", p_scb->sco_idx);
1359 bta_ag_sco_event(p_scb, BTA_AG_SCO_CLOSE_E);
1360 }
1361 }
1362
1363 /*******************************************************************************
1364 *
1365 * Function bta_ag_sco_codec_nego
1366 *
1367 * Description Handles result of eSCO codec negotiation
1368 *
1369 *
1370 * Returns void
1371 *
1372 ******************************************************************************/
bta_ag_sco_codec_nego(tBTA_AG_SCB * p_scb,bool result)1373 void bta_ag_sco_codec_nego(tBTA_AG_SCB* p_scb, bool result) {
1374 if (result) {
1375 /* Subsequent SCO connection will skip codec negotiation */
1376 log::info("Succeeded for index 0x{:04x}, device {}", p_scb->sco_idx,
1377 p_scb->peer_addr);
1378 p_scb->codec_updated = false;
1379 bta_ag_sco_event(p_scb, BTA_AG_SCO_CN_DONE_E);
1380 } else {
1381 /* codec negotiation failed */
1382 log::info("Failed for index 0x{:04x}, device {}", p_scb->sco_idx,
1383 p_scb->peer_addr);
1384 bta_ag_sco_event(p_scb, BTA_AG_SCO_CLOSE_E);
1385 }
1386 }
1387
1388 /*******************************************************************************
1389 *
1390 * Function bta_ag_sco_shutdown
1391 *
1392 * Description
1393 *
1394 *
1395 * Returns void
1396 *
1397 ******************************************************************************/
bta_ag_sco_shutdown(tBTA_AG_SCB * p_scb,const tBTA_AG_DATA &)1398 void bta_ag_sco_shutdown(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& /* data */) {
1399 bta_ag_sco_event(p_scb, BTA_AG_SCO_SHUTDOWN_E);
1400 }
1401
1402 /*******************************************************************************
1403 *
1404 * Function bta_ag_sco_conn_open
1405 *
1406 * Description
1407 *
1408 *
1409 * Returns void
1410 *
1411 ******************************************************************************/
bta_ag_sco_conn_open(tBTA_AG_SCB * p_scb,const tBTA_AG_DATA &)1412 void bta_ag_sco_conn_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& /* data */) {
1413 bta_ag_sco_event(p_scb, BTA_AG_SCO_CONN_OPEN_E);
1414 bta_sys_sco_open(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
1415
1416 if (bta_ag_is_sco_managed_by_audio()) {
1417 // ConfirmStreamingRequest before sends callback to java layer
1418 hfp_offload_interface->ConfirmStreamingRequest();
1419
1420 bool is_controller_codec = false;
1421 if (sco_config_map.find(p_scb->inuse_codec) == sco_config_map.end()) {
1422 log::error("sco_config_map does not have inuse_codec={}",
1423 p_scb->inuse_codec);
1424 } else {
1425 is_controller_codec =
1426 sco_config_map[p_scb->inuse_codec].useControllerCodec;
1427 }
1428
1429 hfp::offload_config config{
1430 .sco_codec = p_scb->inuse_codec,
1431 .connection_handle = p_scb->conn_handle,
1432 .is_controller_codec = is_controller_codec,
1433 .is_nrec = p_scb->nrec_enabled,
1434 };
1435 hfp_offload_interface->UpdateAudioConfigToHal(config);
1436 }
1437
1438 /* call app callback */
1439 bta_ag_cback_sco(p_scb, BTA_AG_AUDIO_OPEN_EVT);
1440
1441 /* reset retransmission_effort_retries*/
1442 p_scb->retransmission_effort_retries = 0;
1443 /* reset to mSBC T2 settings as the preferred */
1444 p_scb->codec_msbc_settings = BTA_AG_SCO_MSBC_SETTINGS_T2;
1445 /* reset to LC3 T2 settings as the preferred */
1446 p_scb->codec_lc3_settings = BTA_AG_SCO_LC3_SETTINGS_T2;
1447 /* reset to SWB Q0 settings as the preferred */
1448 p_scb->codec_aptx_settings = BTA_AG_SCO_APTX_SWB_SETTINGS_Q0;
1449 }
1450
1451 /*******************************************************************************
1452 *
1453 * Function bta_ag_sco_conn_close
1454 *
1455 * Description
1456 *
1457 *
1458 * Returns void
1459 *
1460 ******************************************************************************/
bta_ag_sco_conn_close(tBTA_AG_SCB * p_scb,const tBTA_AG_DATA &)1461 void bta_ag_sco_conn_close(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& /* data */) {
1462 /* clear current scb */
1463 bta_ag_cb.sco.p_curr_scb = nullptr;
1464 p_scb->sco_idx = BTM_INVALID_SCO_INDEX;
1465 const bool aptx_voice = is_hfp_aptx_voice_enabled() &&
1466 p_scb->codec_fallback &&
1467 (p_scb->sco_codec == BTA_AG_SCO_APTX_SWB_SETTINGS_Q0);
1468 log::verbose("aptx_voice={}, codec_fallback={:#x}, sco_codec={:#x}",
1469 aptx_voice, p_scb->codec_fallback, p_scb->sco_codec);
1470
1471 /* codec_fallback is set when AG is initiator and connection failed for mSBC.
1472 * OR if codec is msbc and T2 settings failed, then retry Safe T1 settings
1473 * same operations for LC3 settings */
1474 if (p_scb->svc_conn &&
1475 (p_scb->codec_fallback ||
1476 (p_scb->sco_codec == BTM_SCO_CODEC_MSBC &&
1477 p_scb->codec_msbc_settings == BTA_AG_SCO_MSBC_SETTINGS_T1) ||
1478 (p_scb->sco_codec == BTM_SCO_CODEC_LC3 &&
1479 p_scb->codec_lc3_settings == BTA_AG_SCO_LC3_SETTINGS_T1) ||
1480 (com::android::bluetooth::flags::
1481 retry_esco_with_zero_retransmission_effort() &&
1482 p_scb->retransmission_effort_retries == 1) ||
1483 aptx_voice ||
1484 (com::android::bluetooth::flags::fix_hfp_qual_1_9() &&
1485 p_scb->sco_codec == BTM_SCO_CODEC_CVSD &&
1486 p_scb->codec_cvsd_settings == BTA_AG_SCO_CVSD_SETTINGS_S1 &&
1487 p_scb->trying_cvsd_safe_settings))) {
1488 bta_ag_sco_event(p_scb, BTA_AG_SCO_REOPEN_E);
1489 } else {
1490 /* Indicate if the closing of audio is because of transfer */
1491 bta_ag_sco_event(p_scb, BTA_AG_SCO_CONN_CLOSE_E);
1492
1493 bta_sys_sco_close(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
1494
1495 /* if av got suspended by this call, let it resume. */
1496 /* In case call stays alive regardless of sco, av should not be affected. */
1497 if (((p_scb->call_ind == BTA_AG_CALL_INACTIVE) &&
1498 (p_scb->callsetup_ind == BTA_AG_CALLSETUP_NONE)) ||
1499 (p_scb->post_sco == BTA_AG_POST_SCO_CALL_END)) {
1500 bta_sys_sco_unuse(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
1501 }
1502
1503 /* call app callback */
1504 bta_ag_cback_sco(p_scb, BTA_AG_AUDIO_CLOSE_EVT);
1505 p_scb->codec_cvsd_settings = BTA_AG_SCO_CVSD_SETTINGS_S4;
1506 p_scb->codec_msbc_settings = BTA_AG_SCO_MSBC_SETTINGS_T2;
1507 p_scb->codec_lc3_settings = BTA_AG_SCO_LC3_SETTINGS_T2;
1508 p_scb->codec_aptx_settings = BTA_AG_SCO_APTX_SWB_SETTINGS_Q0;
1509 }
1510 }
1511
1512 /*******************************************************************************
1513 *
1514 * Function bta_ag_sco_conn_rsp
1515 *
1516 * Description Process the SCO connection request
1517 *
1518 *
1519 * Returns void
1520 *
1521 ******************************************************************************/
bta_ag_sco_conn_rsp(tBTA_AG_SCB * p_scb,tBTM_ESCO_CONN_REQ_EVT_DATA * p_data)1522 void bta_ag_sco_conn_rsp(tBTA_AG_SCB* p_scb,
1523 tBTM_ESCO_CONN_REQ_EVT_DATA* p_data) {
1524 bta_ag_cb.sco.is_local = false;
1525
1526 log::verbose(
1527 "eSCO {}, state {}",
1528 bluetooth::shim::GetController()->IsSupported(
1529 bluetooth::hci::OpCode::ENHANCED_SETUP_SYNCHRONOUS_CONNECTION),
1530 bta_ag_cb.sco.state);
1531
1532 if (bta_ag_cb.sco.state == BTA_AG_SCO_LISTEN_ST ||
1533 bta_ag_cb.sco.state == BTA_AG_SCO_CLOSE_XFER_ST ||
1534 bta_ag_cb.sco.state == BTA_AG_SCO_OPEN_XFER_ST) {
1535 /* tell sys to stop av if any */
1536 bta_sys_sco_use(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
1537 /* When HS initiated SCO, it cannot be WBS. */
1538 }
1539
1540 /* If SCO open was initiated from HS, it must be CVSD */
1541 p_scb->inuse_codec = BTM_SCO_CODEC_NONE;
1542 /* Send pending commands to create SCO connection to peer */
1543 enh_esco_params_t params = {};
1544 bool offload = hfp_hal_interface::get_offload_enabled();
1545 bta_ag_cb.sco.p_curr_scb = p_scb;
1546 bta_ag_cb.sco.cur_idx = p_scb->sco_idx;
1547
1548 // Local device accepted SCO connection from peer(HF)
1549 // Because HF devices usually do not send AT+BAC and +BCS command,
1550 // and there is no plan to implement corresponding command handlers,
1551 // so we only accept CVSD connection from HF no matter what's
1552 // requested.
1553 if ((p_scb->features & BTA_AG_FEAT_ESCO_S4) &&
1554 (p_scb->peer_features & BTA_AG_PEER_FEAT_ESCO_S4)) {
1555 // HFP >=1.7 eSCO
1556 params = esco_parameters_for_codec(ESCO_CODEC_CVSD_S4, offload);
1557 } else {
1558 // HFP <=1.6 eSCO
1559 params = esco_parameters_for_codec(ESCO_CODEC_CVSD_S3, offload);
1560 }
1561
1562 // HFP v1.8 5.7.3 CVSD coding
1563 tSCO_CONN* p_sco = NULL;
1564 if (p_scb->sco_idx < BTM_MAX_SCO_LINKS)
1565 p_sco = &btm_cb.sco_cb.sco_db[p_scb->sco_idx];
1566 if (p_sco && (p_sco->esco.data.link_type == BTM_LINK_TYPE_SCO ||
1567 !btm_peer_supports_esco_ev3(p_sco->esco.data.bd_addr))) {
1568 params = esco_parameters_for_codec(SCO_CODEC_CVSD_D1, offload);
1569 }
1570
1571 BTM_EScoConnRsp(p_scb->sco_idx, HCI_SUCCESS, ¶ms);
1572 log::verbose("listening for SCO connection");
1573 }
1574
bta_ag_get_sco_offload_enabled()1575 bool bta_ag_get_sco_offload_enabled() {
1576 return hfp_hal_interface::get_offload_enabled();
1577 }
1578
bta_ag_set_sco_offload_enabled(bool value)1579 void bta_ag_set_sco_offload_enabled(bool value) {
1580 hfp_hal_interface::enable_offload(value);
1581 }
1582
bta_ag_set_sco_allowed(bool value)1583 void bta_ag_set_sco_allowed(bool value) {
1584 sco_allowed = value;
1585 log::verbose("{}", sco_allowed ? "sco now allowed" : "sco now not allowed");
1586 }
1587
bta_ag_is_sco_managed_by_audio()1588 bool bta_ag_is_sco_managed_by_audio() {
1589 bool value = false;
1590 if (com::android::bluetooth::flags::is_sco_managed_by_audio()) {
1591 value = osi_property_get_bool("bluetooth.sco.managed_by_audio", false);
1592 log::verbose("is_sco_managed_by_audio enabled={}", value);
1593 }
1594 return value;
1595 }
1596
bta_ag_get_active_device()1597 const RawAddress& bta_ag_get_active_device() { return active_device_addr; }
1598
bta_clear_active_device()1599 void bta_clear_active_device() {
1600 log::debug("Set bta active device to null");
1601 if (bta_ag_is_sco_managed_by_audio()) {
1602 if (hfp_offload_interface && !active_device_addr.IsEmpty()) {
1603 hfp_offload_interface->StopSession();
1604 }
1605 }
1606 active_device_addr = RawAddress::kEmpty;
1607 }
1608
bta_ag_api_set_active_device(const RawAddress & new_active_device)1609 void bta_ag_api_set_active_device(const RawAddress& new_active_device) {
1610 if (new_active_device.IsEmpty()) {
1611 log::error("empty device");
1612 return;
1613 }
1614
1615 if (bta_ag_is_sco_managed_by_audio()) {
1616 if (!hfp_client_interface) {
1617 hfp_client_interface = std::unique_ptr<HfpInterface>(HfpInterface::Get());
1618 if (!hfp_client_interface) {
1619 log::error("could not acquire audio source interface");
1620 return;
1621 }
1622 }
1623
1624 if (!hfp_offload_interface) {
1625 hfp_offload_interface = std::unique_ptr<HfpInterface::Offload>(
1626 hfp_client_interface->GetOffload(get_main_thread()));
1627 sco_config_map = hfp_offload_interface->GetHfpScoConfig();
1628 if (!hfp_offload_interface) {
1629 log::warn("could not get offload interface");
1630 } else {
1631 // start audio session if there was no previous active device
1632 // if there was an active device, java layer would call disconnectAudio
1633 if (active_device_addr.IsEmpty()) {
1634 hfp_offload_interface->StartSession();
1635 }
1636 }
1637 }
1638 }
1639 active_device_addr = new_active_device;
1640 }
1641