bta_ag_sco.cc 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  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. * This file contains functions for managing the SCO connection used in AG.
  21. *
  22. ******************************************************************************/
  23. #include <cstddef>
  24. #include "bt_common.h"
  25. #include "bta_ag_api.h"
  26. #include "bta_ag_int.h"
  27. #include "bta_api.h"
  28. #include "btm_api.h"
  29. #include "device/include/controller.h"
  30. #include "device/include/esco_parameters.h"
  31. #include "osi/include/osi.h"
  32. #include "stack/include/btu.h"
  33. #include "utl.h"
  34. /* Codec negotiation timeout */
  35. #ifndef BTA_AG_CODEC_NEGOTIATION_TIMEOUT_MS
  36. #define BTA_AG_CODEC_NEGOTIATION_TIMEOUT_MS (3 * 1000) /* 3 seconds */
  37. #endif
  38. static bool sco_allowed = true;
  39. static RawAddress active_device_addr = {};
  40. /* sco events */
  41. enum {
  42. BTA_AG_SCO_LISTEN_E, /* listen request */
  43. BTA_AG_SCO_OPEN_E, /* open request */
  44. BTA_AG_SCO_XFER_E, /* transfer request */
  45. BTA_AG_SCO_CN_DONE_E, /* codec negotiation done */
  46. BTA_AG_SCO_REOPEN_E, /* Retry with other codec when failed */
  47. BTA_AG_SCO_CLOSE_E, /* close request */
  48. BTA_AG_SCO_SHUTDOWN_E, /* shutdown request */
  49. BTA_AG_SCO_CONN_OPEN_E, /* sco open */
  50. BTA_AG_SCO_CONN_CLOSE_E, /* sco closed */
  51. };
  52. #define CASE_RETURN_STR(const) \
  53. case const: \
  54. return #const;
  55. static const char* bta_ag_sco_evt_str(uint8_t event) {
  56. switch (event) {
  57. CASE_RETURN_STR(BTA_AG_SCO_LISTEN_E)
  58. CASE_RETURN_STR(BTA_AG_SCO_OPEN_E)
  59. CASE_RETURN_STR(BTA_AG_SCO_XFER_E)
  60. CASE_RETURN_STR(BTA_AG_SCO_CN_DONE_E)
  61. CASE_RETURN_STR(BTA_AG_SCO_REOPEN_E)
  62. CASE_RETURN_STR(BTA_AG_SCO_CLOSE_E)
  63. CASE_RETURN_STR(BTA_AG_SCO_SHUTDOWN_E)
  64. CASE_RETURN_STR(BTA_AG_SCO_CONN_OPEN_E)
  65. CASE_RETURN_STR(BTA_AG_SCO_CONN_CLOSE_E)
  66. default:
  67. return "Unknown SCO Event";
  68. }
  69. }
  70. static const char* bta_ag_sco_state_str(uint8_t state) {
  71. switch (state) {
  72. CASE_RETURN_STR(BTA_AG_SCO_SHUTDOWN_ST)
  73. CASE_RETURN_STR(BTA_AG_SCO_LISTEN_ST)
  74. CASE_RETURN_STR(BTA_AG_SCO_CODEC_ST)
  75. CASE_RETURN_STR(BTA_AG_SCO_OPENING_ST)
  76. CASE_RETURN_STR(BTA_AG_SCO_OPEN_CL_ST)
  77. CASE_RETURN_STR(BTA_AG_SCO_OPEN_XFER_ST)
  78. CASE_RETURN_STR(BTA_AG_SCO_OPEN_ST)
  79. CASE_RETURN_STR(BTA_AG_SCO_CLOSING_ST)
  80. CASE_RETURN_STR(BTA_AG_SCO_CLOSE_OP_ST)
  81. CASE_RETURN_STR(BTA_AG_SCO_CLOSE_XFER_ST)
  82. CASE_RETURN_STR(BTA_AG_SCO_SHUTTING_ST)
  83. default:
  84. return "Unknown SCO State";
  85. }
  86. }
  87. /**
  88. * Check if bd_addr is the current active device.
  89. *
  90. * @param bd_addr target device address
  91. * @return True if bd_addr is the current active device, False otherwise or if
  92. * no active device is set (i.e. active_device_addr is empty)
  93. */
  94. bool bta_ag_sco_is_active_device(const RawAddress& bd_addr) {
  95. return !active_device_addr.IsEmpty() && active_device_addr == bd_addr;
  96. }
  97. static void bta_ag_create_pending_sco(tBTA_AG_SCB* p_scb, bool is_local);
  98. /*******************************************************************************
  99. *
  100. * Function bta_ag_sco_conn_cback
  101. *
  102. * Description BTM SCO connection callback.
  103. *
  104. *
  105. * Returns void
  106. *
  107. ******************************************************************************/
  108. static void bta_ag_sco_conn_cback(uint16_t sco_idx) {
  109. uint16_t handle;
  110. tBTA_AG_SCB* p_scb;
  111. /* match callback to scb; first check current sco scb */
  112. if (bta_ag_cb.sco.p_curr_scb != nullptr && bta_ag_cb.sco.p_curr_scb->in_use) {
  113. handle = bta_ag_scb_to_idx(bta_ag_cb.sco.p_curr_scb);
  114. }
  115. /* then check for scb connected to this peer */
  116. else {
  117. /* Check if SLC is up */
  118. handle = bta_ag_idx_by_bdaddr(BTM_ReadScoBdAddr(sco_idx));
  119. p_scb = bta_ag_scb_by_idx(handle);
  120. if (p_scb && !p_scb->svc_conn) handle = 0;
  121. }
  122. if (handle != 0) {
  123. do_in_main_thread(FROM_HERE,
  124. base::Bind(&bta_ag_sm_execute_by_handle, handle,
  125. BTA_AG_SCO_OPEN_EVT, tBTA_AG_DATA::kEmpty));
  126. } else {
  127. /* no match found; disconnect sco, init sco variables */
  128. bta_ag_cb.sco.p_curr_scb = nullptr;
  129. bta_ag_cb.sco.state = BTA_AG_SCO_SHUTDOWN_ST;
  130. BTM_RemoveSco(sco_idx);
  131. }
  132. }
  133. /*******************************************************************************
  134. *
  135. * Function bta_ag_sco_disc_cback
  136. *
  137. * Description BTM SCO disconnection callback.
  138. *
  139. *
  140. * Returns void
  141. *
  142. ******************************************************************************/
  143. static void bta_ag_sco_disc_cback(uint16_t sco_idx) {
  144. uint16_t handle = 0;
  145. APPL_TRACE_DEBUG(
  146. "bta_ag_sco_disc_cback(): sco_idx: 0x%x p_cur_scb: 0x%08x sco.state: "
  147. "%d",
  148. sco_idx, bta_ag_cb.sco.p_curr_scb, bta_ag_cb.sco.state);
  149. APPL_TRACE_DEBUG(
  150. "bta_ag_sco_disc_cback(): scb[0] addr: 0x%08x in_use: %u sco_idx: 0x%x "
  151. " sco state: %u",
  152. &bta_ag_cb.scb[0], bta_ag_cb.scb[0].in_use, bta_ag_cb.scb[0].sco_idx,
  153. bta_ag_cb.scb[0].state);
  154. APPL_TRACE_DEBUG(
  155. "bta_ag_sco_disc_cback(): scb[1] addr: 0x%08x in_use: %u sco_idx: 0x%x "
  156. " sco state: %u",
  157. &bta_ag_cb.scb[1], bta_ag_cb.scb[1].in_use, bta_ag_cb.scb[1].sco_idx,
  158. bta_ag_cb.scb[1].state);
  159. /* match callback to scb */
  160. if (bta_ag_cb.sco.p_curr_scb != nullptr && bta_ag_cb.sco.p_curr_scb->in_use) {
  161. /* We only care about callbacks for the active SCO */
  162. if (bta_ag_cb.sco.p_curr_scb->sco_idx != sco_idx) {
  163. if (bta_ag_cb.sco.p_curr_scb->sco_idx != 0xFFFF) return;
  164. }
  165. handle = bta_ag_scb_to_idx(bta_ag_cb.sco.p_curr_scb);
  166. }
  167. if (handle != 0) {
  168. /* Restore settings */
  169. if (bta_ag_cb.sco.p_curr_scb->inuse_codec == BTA_AG_CODEC_MSBC) {
  170. /* Bypass vendor specific and voice settings if enhanced eSCO supported */
  171. if (!(controller_get_interface()
  172. ->supports_enhanced_setup_synchronous_connection())) {
  173. BTM_WriteVoiceSettings(BTM_VOICE_SETTING_CVSD);
  174. }
  175. /* If SCO open was initiated by AG and failed for mSBC T2, try mSBC T1
  176. * 'Safe setting' first. If T1 also fails, try CVSD */
  177. if (bta_ag_sco_is_opening(bta_ag_cb.sco.p_curr_scb)) {
  178. bta_ag_cb.sco.p_curr_scb->state = BTA_AG_SCO_CODEC_ST;
  179. if (bta_ag_cb.sco.p_curr_scb->codec_msbc_settings ==
  180. BTA_AG_SCO_MSBC_SETTINGS_T2) {
  181. APPL_TRACE_WARNING(
  182. "%s: eSCO/SCO failed to open, falling back to mSBC T1 settings",
  183. __func__);
  184. bta_ag_cb.sco.p_curr_scb->codec_msbc_settings =
  185. BTA_AG_SCO_MSBC_SETTINGS_T1;
  186. } else {
  187. APPL_TRACE_WARNING(
  188. "%s: eSCO/SCO failed to open, falling back to CVSD", __func__);
  189. bta_ag_cb.sco.p_curr_scb->codec_fallback = true;
  190. }
  191. }
  192. } else if (bta_ag_sco_is_opening(bta_ag_cb.sco.p_curr_scb)) {
  193. APPL_TRACE_ERROR("%s: eSCO/SCO failed to open, no more fall back",
  194. __func__);
  195. }
  196. bta_ag_cb.sco.p_curr_scb->inuse_codec = BTA_AG_CODEC_NONE;
  197. do_in_main_thread(FROM_HERE,
  198. base::Bind(&bta_ag_sm_execute_by_handle, handle,
  199. BTA_AG_SCO_CLOSE_EVT, tBTA_AG_DATA::kEmpty));
  200. } else {
  201. /* no match found */
  202. APPL_TRACE_DEBUG("no scb for ag_sco_disc_cback");
  203. /* sco could be closed after scb dealloc'ed */
  204. if (bta_ag_cb.sco.p_curr_scb != nullptr) {
  205. bta_ag_cb.sco.p_curr_scb->sco_idx = BTM_INVALID_SCO_INDEX;
  206. bta_ag_cb.sco.p_curr_scb = nullptr;
  207. bta_ag_cb.sco.state = BTA_AG_SCO_SHUTDOWN_ST;
  208. }
  209. }
  210. }
  211. /*******************************************************************************
  212. *
  213. * Function bta_ag_remove_sco
  214. *
  215. * Description Removes the specified SCO from the system.
  216. * If only_active is true, then SCO is only removed if
  217. * connected
  218. *
  219. * Returns bool - true if SCO removal was started
  220. *
  221. ******************************************************************************/
  222. static bool bta_ag_remove_sco(tBTA_AG_SCB* p_scb, bool only_active) {
  223. if (p_scb->sco_idx != BTM_INVALID_SCO_INDEX) {
  224. if (!only_active || p_scb->sco_idx == bta_ag_cb.sco.cur_idx) {
  225. tBTM_STATUS status = BTM_RemoveSco(p_scb->sco_idx);
  226. APPL_TRACE_DEBUG("%s: SCO index 0x%04x, status %d", __func__,
  227. p_scb->sco_idx, status);
  228. if (status == BTM_CMD_STARTED) {
  229. /* SCO is connected; set current control block */
  230. bta_ag_cb.sco.p_curr_scb = p_scb;
  231. return true;
  232. } else if ((status == BTM_SUCCESS) || (status == BTM_UNKNOWN_ADDR)) {
  233. /* If no connection reset the SCO handle */
  234. p_scb->sco_idx = BTM_INVALID_SCO_INDEX;
  235. }
  236. }
  237. }
  238. return false;
  239. }
  240. /*******************************************************************************
  241. *
  242. * Function bta_ag_esco_connreq_cback
  243. *
  244. * Description BTM eSCO connection requests and eSCO change requests
  245. * Only the connection requests are processed by BTA.
  246. *
  247. * Returns void
  248. *
  249. ******************************************************************************/
  250. static void bta_ag_esco_connreq_cback(tBTM_ESCO_EVT event,
  251. tBTM_ESCO_EVT_DATA* p_data) {
  252. /* Only process connection requests */
  253. if (event == BTM_ESCO_CONN_REQ_EVT) {
  254. uint16_t sco_inx = p_data->conn_evt.sco_inx;
  255. const RawAddress* remote_bda = BTM_ReadScoBdAddr(sco_inx);
  256. tBTA_AG_SCB* p_scb = bta_ag_scb_by_idx(bta_ag_idx_by_bdaddr(remote_bda));
  257. if (remote_bda && bta_ag_sco_is_active_device(*remote_bda) && p_scb &&
  258. p_scb->svc_conn) {
  259. p_scb->sco_idx = sco_inx;
  260. /* If no other SCO active, allow this one */
  261. if (!bta_ag_cb.sco.p_curr_scb) {
  262. APPL_TRACE_EVENT("%s: Accept Conn Request (sco_inx 0x%04x)", __func__,
  263. sco_inx);
  264. bta_ag_sco_conn_rsp(p_scb, &p_data->conn_evt);
  265. bta_ag_cb.sco.state = BTA_AG_SCO_OPENING_ST;
  266. bta_ag_cb.sco.p_curr_scb = p_scb;
  267. bta_ag_cb.sco.cur_idx = p_scb->sco_idx;
  268. } else {
  269. /* Begin a transfer: Close current SCO before responding */
  270. APPL_TRACE_DEBUG("bta_ag_esco_connreq_cback: Begin XFER");
  271. bta_ag_cb.sco.p_xfer_scb = p_scb;
  272. bta_ag_cb.sco.conn_data = p_data->conn_evt;
  273. bta_ag_cb.sco.state = BTA_AG_SCO_OPEN_XFER_ST;
  274. if (!bta_ag_remove_sco(bta_ag_cb.sco.p_curr_scb, true)) {
  275. APPL_TRACE_ERROR(
  276. "%s: Nothing to remove,so accept Conn Request(sco_inx 0x%04x)",
  277. __func__, sco_inx);
  278. bta_ag_cb.sco.p_xfer_scb = nullptr;
  279. bta_ag_cb.sco.state = BTA_AG_SCO_LISTEN_ST;
  280. bta_ag_sco_conn_rsp(p_scb, &p_data->conn_evt);
  281. }
  282. }
  283. } else {
  284. LOG(WARNING) << __func__
  285. << ": reject incoming SCO connection, remote_bda="
  286. << (remote_bda ? *remote_bda : RawAddress::kEmpty)
  287. << ", active_bda=" << active_device_addr << ", current_bda="
  288. << (p_scb ? p_scb->peer_addr : RawAddress::kEmpty);
  289. BTM_EScoConnRsp(p_data->conn_evt.sco_inx, HCI_ERR_HOST_REJECT_RESOURCES,
  290. (enh_esco_params_t*)nullptr);
  291. }
  292. } else if (event == BTM_ESCO_CHG_EVT) {
  293. /* Received a change in the esco link */
  294. APPL_TRACE_EVENT(
  295. "%s: eSCO change event (inx %d): rtrans %d, "
  296. "rxlen %d, txlen %d, txint %d",
  297. __func__, p_data->chg_evt.sco_inx, p_data->chg_evt.retrans_window,
  298. p_data->chg_evt.rx_pkt_len, p_data->chg_evt.tx_pkt_len,
  299. p_data->chg_evt.tx_interval);
  300. }
  301. }
  302. /*******************************************************************************
  303. *
  304. * Function bta_ag_cback_sco
  305. *
  306. * Description Call application callback function with SCO event.
  307. *
  308. *
  309. * Returns void
  310. *
  311. ******************************************************************************/
  312. static void bta_ag_cback_sco(tBTA_AG_SCB* p_scb, uint8_t event) {
  313. tBTA_AG_HDR sco = {};
  314. sco.handle = bta_ag_scb_to_idx(p_scb);
  315. sco.app_id = p_scb->app_id;
  316. /* call close cback */
  317. (*bta_ag_cb.p_cback)(event, (tBTA_AG*)&sco);
  318. }
  319. /*******************************************************************************
  320. *
  321. * Function bta_ag_create_sco
  322. *
  323. * Description Create a SCO connection for a given control block
  324. * p_scb : Pointer to the target AG control block
  325. * is_orig : Whether to initiate or listen for SCO connection
  326. *
  327. * Returns void
  328. *
  329. ******************************************************************************/
  330. static void bta_ag_create_sco(tBTA_AG_SCB* p_scb, bool is_orig) {
  331. APPL_TRACE_DEBUG(
  332. "%s: BEFORE codec_updated=%d, codec_fallback=%d, "
  333. "sco_codec=%d, peer_codec=%d, msbc_settings=%d, device=%s",
  334. __func__, p_scb->codec_updated, p_scb->codec_fallback, p_scb->sco_codec,
  335. p_scb->peer_codecs, p_scb->codec_msbc_settings,
  336. p_scb->peer_addr.ToString().c_str());
  337. tBTA_AG_PEER_CODEC esco_codec = BTA_AG_CODEC_CVSD;
  338. if (!bta_ag_sco_is_active_device(p_scb->peer_addr)) {
  339. LOG(WARNING) << __func__ << ": device " << p_scb->peer_addr
  340. << " is not active, active_device=" << active_device_addr;
  341. if (bta_ag_cb.sco.p_curr_scb != nullptr &&
  342. bta_ag_cb.sco.p_curr_scb->in_use && p_scb == bta_ag_cb.sco.p_curr_scb) {
  343. do_in_main_thread(
  344. FROM_HERE, base::Bind(&bta_ag_sm_execute, p_scb, BTA_AG_SCO_CLOSE_EVT,
  345. tBTA_AG_DATA::kEmpty));
  346. }
  347. return;
  348. }
  349. /* Make sure this SCO handle is not already in use */
  350. if (p_scb->sco_idx != BTM_INVALID_SCO_INDEX) {
  351. APPL_TRACE_ERROR("%s: device %s, index 0x%04x already in use!", __func__,
  352. p_scb->peer_addr.ToString().c_str(), p_scb->sco_idx);
  353. return;
  354. }
  355. #if (DISABLE_WBS == FALSE)
  356. if ((p_scb->sco_codec == BTA_AG_CODEC_MSBC) && !p_scb->codec_fallback)
  357. esco_codec = BTA_AG_CODEC_MSBC;
  358. #endif
  359. if (p_scb->codec_fallback) {
  360. p_scb->codec_fallback = false;
  361. /* Force AG to send +BCS for the next audio connection. */
  362. p_scb->codec_updated = true;
  363. /* Reset mSBC settings to T2 for the next audio connection */
  364. p_scb->codec_msbc_settings = BTA_AG_SCO_MSBC_SETTINGS_T2;
  365. }
  366. esco_codec_t codec_index = ESCO_CODEC_CVSD;
  367. /* If WBS included, use CVSD by default, index is 0 for CVSD by
  368. * initialization. If eSCO codec is mSBC, index is T2 or T1 */
  369. if (esco_codec == BTA_AG_CODEC_MSBC) {
  370. if (p_scb->codec_msbc_settings == BTA_AG_SCO_MSBC_SETTINGS_T2) {
  371. codec_index = ESCO_CODEC_MSBC_T2;
  372. } else {
  373. codec_index = ESCO_CODEC_MSBC_T1;
  374. }
  375. }
  376. /* Initialize eSCO parameters */
  377. enh_esco_params_t params = esco_parameters_for_codec(codec_index);
  378. /* For CVSD */
  379. if (esco_codec == BTM_SCO_CODEC_CVSD) {
  380. /* Use the applicable packet types
  381. (3-EV3 not allowed due to errata 2363) */
  382. params.packet_types =
  383. p_bta_ag_cfg->sco_pkt_types | ESCO_PKT_TYPES_MASK_NO_3_EV3;
  384. if ((!(p_scb->features & BTA_AG_FEAT_ESCO)) ||
  385. (!(p_scb->peer_features & BTA_AG_PEER_FEAT_ESCO))) {
  386. params.max_latency_ms = 10;
  387. params.retransmission_effort = ESCO_RETRANSMISSION_POWER;
  388. }
  389. }
  390. /* If initiating, setup parameters to start SCO/eSCO connection */
  391. if (is_orig) {
  392. bta_ag_cb.sco.is_local = true;
  393. /* Set eSCO Mode */
  394. BTM_SetEScoMode(&params);
  395. bta_ag_cb.sco.p_curr_scb = p_scb;
  396. /* save the current codec as sco_codec can be updated while SCO is open. */
  397. p_scb->inuse_codec = esco_codec;
  398. /* tell sys to stop av if any */
  399. bta_sys_sco_use(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
  400. /* Send pending commands to create SCO connection to peer */
  401. bta_ag_create_pending_sco(p_scb, bta_ag_cb.sco.is_local);
  402. APPL_TRACE_API("%s: orig %d, inx 0x%04x, pkt types 0x%04x", __func__,
  403. is_orig, p_scb->sco_idx, params.packet_types);
  404. } else {
  405. /* Not initiating, go to listen mode */
  406. tBTM_STATUS status = BTM_CreateSco(
  407. &p_scb->peer_addr, false, params.packet_types, &p_scb->sco_idx,
  408. bta_ag_sco_conn_cback, bta_ag_sco_disc_cback);
  409. if (status == BTM_CMD_STARTED) {
  410. BTM_RegForEScoEvts(p_scb->sco_idx, bta_ag_esco_connreq_cback);
  411. }
  412. APPL_TRACE_API("%s: orig %d, inx 0x%04x, status 0x%x, pkt types 0x%04x",
  413. __func__, is_orig, p_scb->sco_idx, status,
  414. params.packet_types);
  415. }
  416. APPL_TRACE_DEBUG(
  417. "%s: AFTER codec_updated=%d, codec_fallback=%d, "
  418. "sco_codec=%d, peer_codec=%d, msbc_settings=%d, device=%s",
  419. __func__, p_scb->codec_updated, p_scb->codec_fallback, p_scb->sco_codec,
  420. p_scb->peer_codecs, p_scb->codec_msbc_settings,
  421. p_scb->peer_addr.ToString().c_str());
  422. }
  423. /*******************************************************************************
  424. *
  425. * Function bta_ag_create_pending_sco
  426. *
  427. * Description This Function is called after the pre-SCO vendor setup is
  428. * done for the BTA to continue and send the HCI Commands for
  429. * creating/accepting SCO connection with peer based on the
  430. * is_local parameter.
  431. *
  432. * Returns void
  433. *
  434. ******************************************************************************/
  435. static void bta_ag_create_pending_sco(tBTA_AG_SCB* p_scb, bool is_local) {
  436. tBTA_AG_PEER_CODEC esco_codec = p_scb->inuse_codec;
  437. enh_esco_params_t params = {};
  438. bta_ag_cb.sco.p_curr_scb = p_scb;
  439. bta_ag_cb.sco.cur_idx = p_scb->sco_idx;
  440. /* Local device requested SCO connection to peer */
  441. if (is_local) {
  442. if (esco_codec == BTA_AG_CODEC_MSBC) {
  443. if (p_scb->codec_msbc_settings == BTA_AG_SCO_MSBC_SETTINGS_T2) {
  444. params = esco_parameters_for_codec(ESCO_CODEC_MSBC_T2);
  445. } else
  446. params = esco_parameters_for_codec(ESCO_CODEC_MSBC_T1);
  447. } else {
  448. params = esco_parameters_for_codec(ESCO_CODEC_CVSD);
  449. if ((!(p_scb->features & BTA_AG_FEAT_ESCO)) ||
  450. (!(p_scb->peer_features & BTA_AG_PEER_FEAT_ESCO))) {
  451. params.max_latency_ms = 10;
  452. params.retransmission_effort = ESCO_RETRANSMISSION_POWER;
  453. }
  454. }
  455. /* Bypass voice settings if enhanced SCO setup command is supported */
  456. if (!(controller_get_interface()
  457. ->supports_enhanced_setup_synchronous_connection())) {
  458. if (esco_codec == BTA_AG_CODEC_MSBC) {
  459. BTM_WriteVoiceSettings(BTM_VOICE_SETTING_TRANS);
  460. } else {
  461. BTM_WriteVoiceSettings(BTM_VOICE_SETTING_CVSD);
  462. }
  463. }
  464. if (BTM_CreateSco(&p_scb->peer_addr, true, params.packet_types,
  465. &p_scb->sco_idx, bta_ag_sco_conn_cback,
  466. bta_ag_sco_disc_cback) == BTM_CMD_STARTED) {
  467. /* Initiating the connection, set the current sco handle */
  468. bta_ag_cb.sco.cur_idx = p_scb->sco_idx;
  469. }
  470. APPL_TRACE_DEBUG("%s: initiated SCO connection", __func__);
  471. } else {
  472. /* Local device accepted SCO connection from peer */
  473. params = esco_parameters_for_codec(ESCO_CODEC_CVSD);
  474. if ((!(p_scb->features & BTA_AG_FEAT_ESCO)) ||
  475. (!(p_scb->peer_features & BTA_AG_PEER_FEAT_ESCO))) {
  476. params.max_latency_ms = 10;
  477. params.retransmission_effort = ESCO_RETRANSMISSION_POWER;
  478. }
  479. BTM_EScoConnRsp(p_scb->sco_idx, HCI_SUCCESS, &params);
  480. APPL_TRACE_DEBUG("%s: listening for SCO connection", __func__);
  481. }
  482. }
  483. /*******************************************************************************
  484. *
  485. * Function bta_ag_codec_negotiation_timer_cback
  486. *
  487. * Description
  488. *
  489. *
  490. * Returns void
  491. *
  492. ******************************************************************************/
  493. static void bta_ag_codec_negotiation_timer_cback(void* data) {
  494. APPL_TRACE_DEBUG("%s", __func__);
  495. tBTA_AG_SCB* p_scb = (tBTA_AG_SCB*)data;
  496. /* Announce that codec negotiation failed. */
  497. bta_ag_sco_codec_nego(p_scb, false);
  498. /* call app callback */
  499. bta_ag_cback_sco(p_scb, BTA_AG_AUDIO_CLOSE_EVT);
  500. }
  501. /*******************************************************************************
  502. *
  503. * Function bta_ag_codec_negotiate
  504. *
  505. * Description Initiate codec negotiation by sending AT command.
  506. * If not necessary, skip negotiation.
  507. *
  508. * Returns void
  509. *
  510. ******************************************************************************/
  511. void bta_ag_codec_negotiate(tBTA_AG_SCB* p_scb) {
  512. APPL_TRACE_DEBUG("%s", __func__);
  513. bta_ag_cb.sco.p_curr_scb = p_scb;
  514. uint8_t* p_rem_feat = BTM_ReadRemoteFeatures(p_scb->peer_addr);
  515. bool sdp_wbs_support = p_scb->peer_sdp_features & BTA_AG_FEAT_WBS_SUPPORT;
  516. if (p_rem_feat == nullptr) {
  517. LOG(WARNING) << __func__
  518. << ": Fail to read remote feature, skip codec negotiation";
  519. bta_ag_sco_codec_nego(p_scb, false);
  520. return;
  521. }
  522. // Workaround for misbehaving HFs, which indicate which one is not support on
  523. // Transparent Synchronous Data in Remote Supported Features, WBS in SDP and
  524. // and Codec Negotiation in BRSF. Fluoride will assume CVSD codec by default.
  525. // In Sony XAV AX100 car kit and Sony MW600 Headset case, which indicate
  526. // Transparent Synchronous Data and WBS support, but no codec negotiation
  527. // support, using mSBC codec can result background noise or no audio.
  528. // In Skullcandy JIB case, which indicate WBS and codec negotiation support,
  529. // but no Transparent Synchronous Data support, using mSBC codec can result
  530. // SCO setup fail by Firmware reject.
  531. if (!HCI_LMP_TRANSPNT_SUPPORTED(p_rem_feat) || !sdp_wbs_support ||
  532. !(p_scb->peer_features & BTA_AG_PEER_FEAT_CODEC)) {
  533. p_scb->sco_codec = UUID_CODEC_CVSD;
  534. }
  535. if ((p_scb->codec_updated || p_scb->codec_fallback) &&
  536. (p_scb->peer_features & BTA_AG_PEER_FEAT_CODEC)) {
  537. /* Change the power mode to Active until SCO open is completed. */
  538. bta_sys_busy(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
  539. /* Send +BCS to the peer */
  540. bta_ag_send_bcs(p_scb);
  541. /* Start timer to handle timeout */
  542. alarm_set_on_mloop(p_scb->codec_negotiation_timer,
  543. BTA_AG_CODEC_NEGOTIATION_TIMEOUT_MS,
  544. bta_ag_codec_negotiation_timer_cback, p_scb);
  545. } else {
  546. /* use same codec type as previous SCO connection, skip codec negotiation */
  547. APPL_TRACE_DEBUG(
  548. "use same codec type as previous SCO connection,skip codec "
  549. "negotiation");
  550. bta_ag_sco_codec_nego(p_scb, true);
  551. }
  552. }
  553. /*******************************************************************************
  554. *
  555. * Function bta_ag_sco_event
  556. *
  557. * Description
  558. *
  559. *
  560. * Returns void
  561. *
  562. ******************************************************************************/
  563. static void bta_ag_sco_event(tBTA_AG_SCB* p_scb, uint8_t event) {
  564. tBTA_AG_SCO_CB* p_sco = &bta_ag_cb.sco;
  565. uint8_t previous_state = p_sco->state;
  566. APPL_TRACE_EVENT("%s: index=0x%04x, device=%s, state=%s[%d], event=%s[%d]",
  567. __func__, p_scb->sco_idx,
  568. p_scb->peer_addr.ToString().c_str(),
  569. bta_ag_sco_state_str(p_sco->state), p_sco->state,
  570. bta_ag_sco_evt_str(event), event);
  571. switch (p_sco->state) {
  572. case BTA_AG_SCO_SHUTDOWN_ST:
  573. switch (event) {
  574. case BTA_AG_SCO_LISTEN_E:
  575. /* create sco listen connection */
  576. bta_ag_create_sco(p_scb, false);
  577. p_sco->state = BTA_AG_SCO_LISTEN_ST;
  578. break;
  579. default:
  580. APPL_TRACE_WARNING(
  581. "%s: BTA_AG_SCO_SHUTDOWN_ST: Ignoring event %s[%d]", __func__,
  582. bta_ag_sco_evt_str(event), event);
  583. break;
  584. }
  585. break;
  586. case BTA_AG_SCO_LISTEN_ST:
  587. switch (event) {
  588. case BTA_AG_SCO_LISTEN_E:
  589. /* create sco listen connection (Additional channel) */
  590. bta_ag_create_sco(p_scb, false);
  591. break;
  592. case BTA_AG_SCO_OPEN_E:
  593. /* remove listening connection */
  594. bta_ag_remove_sco(p_scb, false);
  595. #if (DISABLE_WBS == FALSE)
  596. /* start codec negotiation */
  597. p_sco->state = BTA_AG_SCO_CODEC_ST;
  598. bta_ag_codec_negotiate(p_scb);
  599. #else
  600. bta_ag_create_sco(p_scb, true);
  601. p_sco->state = BTA_AG_SCO_OPENING_ST;
  602. #endif
  603. break;
  604. case BTA_AG_SCO_SHUTDOWN_E:
  605. /* remove listening connection */
  606. bta_ag_remove_sco(p_scb, false);
  607. if (p_scb == p_sco->p_curr_scb) p_sco->p_curr_scb = nullptr;
  608. /* If last SCO instance then finish shutting down */
  609. if (!bta_ag_other_scb_open(p_scb)) {
  610. p_sco->state = BTA_AG_SCO_SHUTDOWN_ST;
  611. }
  612. break;
  613. case BTA_AG_SCO_CLOSE_E:
  614. /* remove listening connection */
  615. /* Ignore the event. Keep listening SCO for the active SLC
  616. */
  617. APPL_TRACE_WARNING("%s: BTA_AG_SCO_LISTEN_ST: Ignoring event %s[%d]",
  618. __func__, bta_ag_sco_evt_str(event), event);
  619. break;
  620. case BTA_AG_SCO_CONN_CLOSE_E:
  621. /* sco failed; create sco listen connection */
  622. bta_ag_create_sco(p_scb, false);
  623. p_sco->state = BTA_AG_SCO_LISTEN_ST;
  624. break;
  625. default:
  626. APPL_TRACE_WARNING("%s: BTA_AG_SCO_LISTEN_ST: Ignoring event %s[%d]",
  627. __func__, bta_ag_sco_evt_str(event), event);
  628. break;
  629. }
  630. break;
  631. case BTA_AG_SCO_CODEC_ST:
  632. switch (event) {
  633. case BTA_AG_SCO_LISTEN_E:
  634. /* create sco listen connection (Additional channel) */
  635. bta_ag_create_sco(p_scb, false);
  636. break;
  637. case BTA_AG_SCO_CN_DONE_E:
  638. /* create sco connection to peer */
  639. bta_ag_create_sco(p_scb, true);
  640. p_sco->state = BTA_AG_SCO_OPENING_ST;
  641. break;
  642. case BTA_AG_SCO_XFER_E:
  643. /* save xfer scb */
  644. p_sco->p_xfer_scb = p_scb;
  645. p_sco->state = BTA_AG_SCO_CLOSE_XFER_ST;
  646. break;
  647. case BTA_AG_SCO_SHUTDOWN_E:
  648. /* remove listening connection */
  649. bta_ag_remove_sco(p_scb, false);
  650. if (p_scb == p_sco->p_curr_scb) p_sco->p_curr_scb = nullptr;
  651. /* If last SCO instance then finish shutting down */
  652. if (!bta_ag_other_scb_open(p_scb)) {
  653. p_sco->state = BTA_AG_SCO_SHUTDOWN_ST;
  654. }
  655. break;
  656. case BTA_AG_SCO_CLOSE_E:
  657. /* sco open is not started yet. just go back to listening */
  658. p_sco->state = BTA_AG_SCO_LISTEN_ST;
  659. break;
  660. case BTA_AG_SCO_CONN_CLOSE_E:
  661. /* sco failed; create sco listen connection */
  662. bta_ag_create_sco(p_scb, false);
  663. p_sco->state = BTA_AG_SCO_LISTEN_ST;
  664. break;
  665. default:
  666. APPL_TRACE_WARNING("%s: BTA_AG_SCO_CODEC_ST: Ignoring event %s[%d]",
  667. __func__, bta_ag_sco_evt_str(event), event);
  668. break;
  669. }
  670. break;
  671. case BTA_AG_SCO_OPENING_ST:
  672. switch (event) {
  673. case BTA_AG_SCO_LISTEN_E:
  674. /* second headset has now joined */
  675. /* create sco listen connection (Additional channel) */
  676. if (p_scb != p_sco->p_curr_scb) {
  677. bta_ag_create_sco(p_scb, false);
  678. }
  679. break;
  680. #if (DISABLE_WBS == FALSE)
  681. case BTA_AG_SCO_REOPEN_E:
  682. /* start codec negotiation */
  683. p_sco->state = BTA_AG_SCO_CODEC_ST;
  684. bta_ag_codec_negotiate(p_scb);
  685. break;
  686. #endif
  687. case BTA_AG_SCO_XFER_E:
  688. /* save xfer scb */
  689. p_sco->p_xfer_scb = p_scb;
  690. p_sco->state = BTA_AG_SCO_CLOSE_XFER_ST;
  691. break;
  692. case BTA_AG_SCO_CLOSE_E:
  693. p_sco->state = BTA_AG_SCO_OPEN_CL_ST;
  694. break;
  695. case BTA_AG_SCO_SHUTDOWN_E:
  696. /* If not opening scb, just close it */
  697. if (p_scb != p_sco->p_curr_scb) {
  698. /* remove listening connection */
  699. bta_ag_remove_sco(p_scb, false);
  700. } else
  701. p_sco->state = BTA_AG_SCO_SHUTTING_ST;
  702. break;
  703. case BTA_AG_SCO_CONN_OPEN_E:
  704. p_sco->state = BTA_AG_SCO_OPEN_ST;
  705. break;
  706. case BTA_AG_SCO_CONN_CLOSE_E:
  707. /* sco failed; create sco listen connection */
  708. bta_ag_create_sco(p_scb, false);
  709. p_sco->state = BTA_AG_SCO_LISTEN_ST;
  710. break;
  711. default:
  712. APPL_TRACE_WARNING("%s: BTA_AG_SCO_OPENING_ST: Ignoring event %s[%d]",
  713. __func__, bta_ag_sco_evt_str(event), event);
  714. break;
  715. }
  716. break;
  717. case BTA_AG_SCO_OPEN_CL_ST:
  718. switch (event) {
  719. case BTA_AG_SCO_XFER_E:
  720. /* save xfer scb */
  721. p_sco->p_xfer_scb = p_scb;
  722. p_sco->state = BTA_AG_SCO_CLOSE_XFER_ST;
  723. break;
  724. case BTA_AG_SCO_OPEN_E:
  725. p_sco->state = BTA_AG_SCO_OPENING_ST;
  726. break;
  727. case BTA_AG_SCO_SHUTDOWN_E:
  728. /* If not opening scb, just close it */
  729. if (p_scb != p_sco->p_curr_scb) {
  730. /* remove listening connection */
  731. bta_ag_remove_sco(p_scb, false);
  732. } else
  733. p_sco->state = BTA_AG_SCO_SHUTTING_ST;
  734. break;
  735. case BTA_AG_SCO_CONN_OPEN_E:
  736. /* close sco connection */
  737. bta_ag_remove_sco(p_scb, true);
  738. p_sco->state = BTA_AG_SCO_CLOSING_ST;
  739. break;
  740. case BTA_AG_SCO_CONN_CLOSE_E:
  741. /* sco failed; create sco listen connection */
  742. p_sco->state = BTA_AG_SCO_LISTEN_ST;
  743. break;
  744. default:
  745. APPL_TRACE_WARNING("%s: BTA_AG_SCO_OPEN_CL_ST: Ignoring event %s[%d]",
  746. __func__, bta_ag_sco_evt_str(event), event);
  747. break;
  748. }
  749. break;
  750. case BTA_AG_SCO_OPEN_XFER_ST:
  751. switch (event) {
  752. case BTA_AG_SCO_CLOSE_E:
  753. /* close sco connection */
  754. bta_ag_remove_sco(p_scb, true);
  755. p_sco->state = BTA_AG_SCO_CLOSING_ST;
  756. break;
  757. case BTA_AG_SCO_SHUTDOWN_E:
  758. /* remove all connection */
  759. bta_ag_remove_sco(p_scb, false);
  760. p_sco->state = BTA_AG_SCO_SHUTTING_ST;
  761. break;
  762. case BTA_AG_SCO_CONN_CLOSE_E:
  763. /* closed sco; place in listen mode and
  764. accept the transferred connection */
  765. bta_ag_create_sco(p_scb, false); /* Back into listen mode */
  766. /* Accept sco connection with xfer scb */
  767. bta_ag_sco_conn_rsp(p_sco->p_xfer_scb, &p_sco->conn_data);
  768. p_sco->state = BTA_AG_SCO_OPENING_ST;
  769. p_sco->p_curr_scb = p_sco->p_xfer_scb;
  770. p_sco->cur_idx = p_sco->p_xfer_scb->sco_idx;
  771. p_sco->p_xfer_scb = nullptr;
  772. break;
  773. default:
  774. APPL_TRACE_WARNING(
  775. "%s: BTA_AG_SCO_OPEN_XFER_ST: Ignoring event %s[%d]", __func__,
  776. bta_ag_sco_evt_str(event), event);
  777. break;
  778. }
  779. break;
  780. case BTA_AG_SCO_OPEN_ST:
  781. switch (event) {
  782. case BTA_AG_SCO_LISTEN_E:
  783. /* second headset has now joined */
  784. /* create sco listen connection (Additional channel) */
  785. if (p_scb != p_sco->p_curr_scb) {
  786. bta_ag_create_sco(p_scb, false);
  787. }
  788. break;
  789. case BTA_AG_SCO_XFER_E:
  790. /* close current sco connection */
  791. bta_ag_remove_sco(p_sco->p_curr_scb, true);
  792. /* save xfer scb */
  793. p_sco->p_xfer_scb = p_scb;
  794. p_sco->state = BTA_AG_SCO_CLOSE_XFER_ST;
  795. break;
  796. case BTA_AG_SCO_CLOSE_E:
  797. /* close sco connection if active */
  798. if (bta_ag_remove_sco(p_scb, true)) {
  799. p_sco->state = BTA_AG_SCO_CLOSING_ST;
  800. }
  801. break;
  802. case BTA_AG_SCO_SHUTDOWN_E:
  803. /* remove all listening connections */
  804. bta_ag_remove_sco(p_scb, false);
  805. /* If SCO was active on this scb, close it */
  806. if (p_scb == p_sco->p_curr_scb) {
  807. p_sco->state = BTA_AG_SCO_SHUTTING_ST;
  808. }
  809. break;
  810. case BTA_AG_SCO_CONN_CLOSE_E:
  811. /* peer closed sco; create sco listen connection */
  812. bta_ag_create_sco(p_scb, false);
  813. p_sco->state = BTA_AG_SCO_LISTEN_ST;
  814. break;
  815. default:
  816. APPL_TRACE_WARNING("%s: BTA_AG_SCO_OPEN_ST: Ignoring event %s[%d]",
  817. __func__, bta_ag_sco_evt_str(event), event);
  818. break;
  819. }
  820. break;
  821. case BTA_AG_SCO_CLOSING_ST:
  822. switch (event) {
  823. case BTA_AG_SCO_LISTEN_E:
  824. /* create sco listen connection (Additional channel) */
  825. if (p_scb != p_sco->p_curr_scb) {
  826. bta_ag_create_sco(p_scb, false);
  827. }
  828. break;
  829. case BTA_AG_SCO_OPEN_E:
  830. p_sco->state = BTA_AG_SCO_CLOSE_OP_ST;
  831. break;
  832. case BTA_AG_SCO_XFER_E:
  833. /* save xfer scb */
  834. p_sco->p_xfer_scb = p_scb;
  835. p_sco->state = BTA_AG_SCO_CLOSE_XFER_ST;
  836. break;
  837. case BTA_AG_SCO_SHUTDOWN_E:
  838. /* If not closing scb, just close it */
  839. if (p_scb != p_sco->p_curr_scb) {
  840. /* remove listening connection */
  841. bta_ag_remove_sco(p_scb, false);
  842. } else
  843. p_sco->state = BTA_AG_SCO_SHUTTING_ST;
  844. break;
  845. case BTA_AG_SCO_CONN_CLOSE_E:
  846. /* peer closed sco; create sco listen connection */
  847. bta_ag_create_sco(p_scb, false);
  848. p_sco->state = BTA_AG_SCO_LISTEN_ST;
  849. break;
  850. default:
  851. APPL_TRACE_WARNING("%s: BTA_AG_SCO_CLOSING_ST: Ignoring event %s[%d]",
  852. __func__, bta_ag_sco_evt_str(event), event);
  853. break;
  854. }
  855. break;
  856. case BTA_AG_SCO_CLOSE_OP_ST:
  857. switch (event) {
  858. case BTA_AG_SCO_CLOSE_E:
  859. p_sco->state = BTA_AG_SCO_CLOSING_ST;
  860. break;
  861. case BTA_AG_SCO_SHUTDOWN_E:
  862. p_sco->state = BTA_AG_SCO_SHUTTING_ST;
  863. break;
  864. case BTA_AG_SCO_CONN_CLOSE_E:
  865. /* start codec negotiation */
  866. p_sco->state = BTA_AG_SCO_CODEC_ST;
  867. bta_ag_codec_negotiate(p_scb);
  868. break;
  869. case BTA_AG_SCO_LISTEN_E:
  870. /* create sco listen connection (Additional channel) */
  871. if (p_scb != p_sco->p_curr_scb) {
  872. bta_ag_create_sco(p_scb, false);
  873. }
  874. break;
  875. default:
  876. APPL_TRACE_WARNING(
  877. "%s: BTA_AG_SCO_CLOSE_OP_ST: Ignoring event %s[%d]", __func__,
  878. bta_ag_sco_evt_str(event), event);
  879. break;
  880. }
  881. break;
  882. case BTA_AG_SCO_CLOSE_XFER_ST:
  883. switch (event) {
  884. case BTA_AG_SCO_CONN_OPEN_E:
  885. /* close sco connection so headset can be transferred
  886. Probably entered this state from "opening state" */
  887. bta_ag_remove_sco(p_scb, true);
  888. break;
  889. case BTA_AG_SCO_CLOSE_E:
  890. /* clear xfer scb */
  891. p_sco->p_xfer_scb = nullptr;
  892. p_sco->state = BTA_AG_SCO_CLOSING_ST;
  893. break;
  894. case BTA_AG_SCO_SHUTDOWN_E:
  895. /* clear xfer scb */
  896. p_sco->p_xfer_scb = nullptr;
  897. p_sco->state = BTA_AG_SCO_SHUTTING_ST;
  898. break;
  899. case BTA_AG_SCO_CONN_CLOSE_E: {
  900. /* closed sco; place old sco in listen mode,
  901. take current sco out of listen, and
  902. create originating sco for current */
  903. bta_ag_create_sco(p_scb, false);
  904. bta_ag_remove_sco(p_sco->p_xfer_scb, false);
  905. #if (DISABLE_WBS == FALSE)
  906. /* start codec negotiation */
  907. p_sco->state = BTA_AG_SCO_CODEC_ST;
  908. tBTA_AG_SCB* p_cn_scb = p_sco->p_xfer_scb;
  909. p_sco->p_xfer_scb = nullptr;
  910. bta_ag_codec_negotiate(p_cn_scb);
  911. #else
  912. /* create sco connection to peer */
  913. bta_ag_create_sco(p_sco->p_xfer_scb, true);
  914. p_sco->p_xfer_scb = nullptr;
  915. p_sco->state = BTA_AG_SCO_OPENING_ST;
  916. #endif
  917. break;
  918. }
  919. default:
  920. APPL_TRACE_WARNING(
  921. "%s: BTA_AG_SCO_CLOSE_XFER_ST: Ignoring event %s[%d]", __func__,
  922. bta_ag_sco_evt_str(event), event);
  923. break;
  924. }
  925. break;
  926. case BTA_AG_SCO_SHUTTING_ST:
  927. switch (event) {
  928. case BTA_AG_SCO_CONN_OPEN_E:
  929. /* close sco connection; wait for conn close event */
  930. bta_ag_remove_sco(p_scb, true);
  931. break;
  932. case BTA_AG_SCO_CONN_CLOSE_E:
  933. /* If last SCO instance then finish shutting down */
  934. if (!bta_ag_other_scb_open(p_scb)) {
  935. p_sco->state = BTA_AG_SCO_SHUTDOWN_ST;
  936. } else /* Other instance is still listening */
  937. {
  938. p_sco->state = BTA_AG_SCO_LISTEN_ST;
  939. }
  940. /* If SCO closed for other HS which is not being disconnected,
  941. then create listen sco connection for it as scb still open */
  942. if (bta_ag_scb_open(p_scb)) {
  943. bta_ag_create_sco(p_scb, false);
  944. p_sco->state = BTA_AG_SCO_LISTEN_ST;
  945. }
  946. if (p_scb == p_sco->p_curr_scb) {
  947. p_sco->p_curr_scb->sco_idx = BTM_INVALID_SCO_INDEX;
  948. p_sco->p_curr_scb = nullptr;
  949. }
  950. break;
  951. case BTA_AG_SCO_LISTEN_E:
  952. /* create sco listen connection (Additional channel) */
  953. if (p_scb != p_sco->p_curr_scb) {
  954. bta_ag_create_sco(p_scb, false);
  955. }
  956. break;
  957. case BTA_AG_SCO_SHUTDOWN_E:
  958. if (!bta_ag_other_scb_open(p_scb)) {
  959. p_sco->state = BTA_AG_SCO_SHUTDOWN_ST;
  960. } else /* Other instance is still listening */
  961. {
  962. p_sco->state = BTA_AG_SCO_LISTEN_ST;
  963. }
  964. if (p_scb == p_sco->p_curr_scb) {
  965. p_sco->p_curr_scb->sco_idx = BTM_INVALID_SCO_INDEX;
  966. p_sco->p_curr_scb = nullptr;
  967. }
  968. break;
  969. default:
  970. APPL_TRACE_WARNING(
  971. "%s: BTA_AG_SCO_SHUTTING_ST: Ignoring event %s[%d]", __func__,
  972. bta_ag_sco_evt_str(event), event);
  973. break;
  974. }
  975. break;
  976. default:
  977. break;
  978. }
  979. if (p_sco->state != previous_state) {
  980. APPL_TRACE_EVENT(
  981. "%s: SCO_state_change: [%s(0x%02x)]->[%s(0x%02x)] "
  982. "after event [%s(0x%02x)]",
  983. __func__, bta_ag_sco_state_str(previous_state), previous_state,
  984. bta_ag_sco_state_str(p_sco->state), p_sco->state,
  985. bta_ag_sco_evt_str(event), event);
  986. }
  987. }
  988. /*******************************************************************************
  989. *
  990. * Function bta_ag_sco_is_open
  991. *
  992. * Description Check if sco is open for this scb.
  993. *
  994. *
  995. * Returns true if sco open for this scb, false otherwise.
  996. *
  997. ******************************************************************************/
  998. bool bta_ag_sco_is_open(tBTA_AG_SCB* p_scb) {
  999. return ((bta_ag_cb.sco.state == BTA_AG_SCO_OPEN_ST) &&
  1000. (bta_ag_cb.sco.p_curr_scb == p_scb));
  1001. }
  1002. /*******************************************************************************
  1003. *
  1004. * Function bta_ag_sco_is_opening
  1005. *
  1006. * Description Check if sco is in Opening state.
  1007. *
  1008. *
  1009. * Returns true if sco is in Opening state for this scb, false
  1010. * otherwise.
  1011. *
  1012. ******************************************************************************/
  1013. bool bta_ag_sco_is_opening(tBTA_AG_SCB* p_scb) {
  1014. return ((bta_ag_cb.sco.state == BTA_AG_SCO_OPENING_ST) &&
  1015. (bta_ag_cb.sco.p_curr_scb == p_scb));
  1016. }
  1017. /*******************************************************************************
  1018. *
  1019. * Function bta_ag_sco_listen
  1020. *
  1021. * Description
  1022. *
  1023. *
  1024. * Returns void
  1025. *
  1026. ******************************************************************************/
  1027. void bta_ag_sco_listen(tBTA_AG_SCB* p_scb,
  1028. UNUSED_ATTR const tBTA_AG_DATA& data) {
  1029. LOG(INFO) << __func__ << ": " << p_scb->peer_addr;
  1030. bta_ag_sco_event(p_scb, BTA_AG_SCO_LISTEN_E);
  1031. }
  1032. /*******************************************************************************
  1033. *
  1034. * Function bta_ag_sco_open
  1035. *
  1036. * Description
  1037. *
  1038. *
  1039. * Returns void
  1040. *
  1041. ******************************************************************************/
  1042. void bta_ag_sco_open(tBTA_AG_SCB* p_scb, UNUSED_ATTR const tBTA_AG_DATA& data) {
  1043. if (!sco_allowed) {
  1044. LOG(INFO) << __func__ << ": not opening sco, by policy";
  1045. return;
  1046. }
  1047. /* if another scb using sco, this is a transfer */
  1048. if (bta_ag_cb.sco.p_curr_scb && bta_ag_cb.sco.p_curr_scb != p_scb) {
  1049. LOG(INFO) << __func__ << ": tranfer " << bta_ag_cb.sco.p_curr_scb->peer_addr
  1050. << " -> " << p_scb->peer_addr;
  1051. bta_ag_sco_event(p_scb, BTA_AG_SCO_XFER_E);
  1052. } else {
  1053. /* else it is an open */
  1054. LOG(INFO) << __func__ << ": open " << p_scb->peer_addr;
  1055. bta_ag_sco_event(p_scb, BTA_AG_SCO_OPEN_E);
  1056. }
  1057. }
  1058. /*******************************************************************************
  1059. *
  1060. * Function bta_ag_sco_close
  1061. *
  1062. * Description
  1063. *
  1064. *
  1065. * Returns void
  1066. *
  1067. ******************************************************************************/
  1068. void bta_ag_sco_close(tBTA_AG_SCB* p_scb,
  1069. UNUSED_ATTR const tBTA_AG_DATA& data) {
  1070. /* if scb is in use */
  1071. /* sco_idx is not allocated in SCO_CODEC_ST, still need to move to listen
  1072. * state. */
  1073. if ((p_scb->sco_idx != BTM_INVALID_SCO_INDEX) ||
  1074. (bta_ag_cb.sco.state == BTA_AG_SCO_CODEC_ST)) {
  1075. APPL_TRACE_DEBUG("bta_ag_sco_close: sco_inx = %d", p_scb->sco_idx);
  1076. bta_ag_sco_event(p_scb, BTA_AG_SCO_CLOSE_E);
  1077. }
  1078. }
  1079. /*******************************************************************************
  1080. *
  1081. * Function bta_ag_sco_codec_nego
  1082. *
  1083. * Description Handles result of eSCO codec negotiation
  1084. *
  1085. *
  1086. * Returns void
  1087. *
  1088. ******************************************************************************/
  1089. void bta_ag_sco_codec_nego(tBTA_AG_SCB* p_scb, bool result) {
  1090. if (result) {
  1091. /* Subsequent SCO connection will skip codec negotiation */
  1092. APPL_TRACE_DEBUG("%s: Succeeded for index 0x%04x, device %s", __func__,
  1093. p_scb->sco_idx, p_scb->peer_addr.ToString().c_str());
  1094. p_scb->codec_updated = false;
  1095. bta_ag_sco_event(p_scb, BTA_AG_SCO_CN_DONE_E);
  1096. } else {
  1097. /* codec negotiation failed */
  1098. APPL_TRACE_ERROR("%s: Failed for index 0x%04x, device %s", __func__,
  1099. p_scb->sco_idx, p_scb->peer_addr.ToString().c_str());
  1100. bta_ag_sco_event(p_scb, BTA_AG_SCO_CLOSE_E);
  1101. }
  1102. }
  1103. /*******************************************************************************
  1104. *
  1105. * Function bta_ag_sco_shutdown
  1106. *
  1107. * Description
  1108. *
  1109. *
  1110. * Returns void
  1111. *
  1112. ******************************************************************************/
  1113. void bta_ag_sco_shutdown(tBTA_AG_SCB* p_scb,
  1114. UNUSED_ATTR const tBTA_AG_DATA& data) {
  1115. bta_ag_sco_event(p_scb, BTA_AG_SCO_SHUTDOWN_E);
  1116. }
  1117. /*******************************************************************************
  1118. *
  1119. * Function bta_ag_sco_conn_open
  1120. *
  1121. * Description
  1122. *
  1123. *
  1124. * Returns void
  1125. *
  1126. ******************************************************************************/
  1127. void bta_ag_sco_conn_open(tBTA_AG_SCB* p_scb,
  1128. UNUSED_ATTR const tBTA_AG_DATA& data) {
  1129. bta_ag_sco_event(p_scb, BTA_AG_SCO_CONN_OPEN_E);
  1130. bta_sys_sco_open(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
  1131. /* call app callback */
  1132. bta_ag_cback_sco(p_scb, BTA_AG_AUDIO_OPEN_EVT);
  1133. /* reset to mSBC T2 settings as the preferred */
  1134. p_scb->codec_msbc_settings = BTA_AG_SCO_MSBC_SETTINGS_T2;
  1135. }
  1136. /*******************************************************************************
  1137. *
  1138. * Function bta_ag_sco_conn_close
  1139. *
  1140. * Description
  1141. *
  1142. *
  1143. * Returns void
  1144. *
  1145. ******************************************************************************/
  1146. void bta_ag_sco_conn_close(tBTA_AG_SCB* p_scb,
  1147. UNUSED_ATTR const tBTA_AG_DATA& data) {
  1148. /* clear current scb */
  1149. bta_ag_cb.sco.p_curr_scb = nullptr;
  1150. p_scb->sco_idx = BTM_INVALID_SCO_INDEX;
  1151. /* codec_fallback is set when AG is initiator and connection failed for mSBC.
  1152. * OR if codec is msbc and T2 settings failed, then retry Safe T1 settings */
  1153. if (p_scb->svc_conn &&
  1154. (p_scb->codec_fallback ||
  1155. (p_scb->sco_codec == BTM_SCO_CODEC_MSBC &&
  1156. p_scb->codec_msbc_settings == BTA_AG_SCO_MSBC_SETTINGS_T1))) {
  1157. bta_ag_sco_event(p_scb, BTA_AG_SCO_REOPEN_E);
  1158. } else {
  1159. /* Indicate if the closing of audio is because of transfer */
  1160. bta_ag_sco_event(p_scb, BTA_AG_SCO_CONN_CLOSE_E);
  1161. bta_sys_sco_close(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
  1162. /* if av got suspended by this call, let it resume. */
  1163. /* In case call stays alive regardless of sco, av should not be affected. */
  1164. if (((p_scb->call_ind == BTA_AG_CALL_INACTIVE) &&
  1165. (p_scb->callsetup_ind == BTA_AG_CALLSETUP_NONE)) ||
  1166. (p_scb->post_sco == BTA_AG_POST_SCO_CALL_END)) {
  1167. bta_sys_sco_unuse(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
  1168. }
  1169. /* call app callback */
  1170. bta_ag_cback_sco(p_scb, BTA_AG_AUDIO_CLOSE_EVT);
  1171. p_scb->codec_msbc_settings = BTA_AG_SCO_MSBC_SETTINGS_T2;
  1172. }
  1173. }
  1174. /*******************************************************************************
  1175. *
  1176. * Function bta_ag_sco_conn_rsp
  1177. *
  1178. * Description Process the SCO connection request
  1179. *
  1180. *
  1181. * Returns void
  1182. *
  1183. ******************************************************************************/
  1184. void bta_ag_sco_conn_rsp(tBTA_AG_SCB* p_scb,
  1185. tBTM_ESCO_CONN_REQ_EVT_DATA* p_data) {
  1186. bta_ag_cb.sco.is_local = false;
  1187. APPL_TRACE_DEBUG("%s: eSCO %d, state %d", __func__,
  1188. controller_get_interface()
  1189. ->supports_enhanced_setup_synchronous_connection(),
  1190. bta_ag_cb.sco.state);
  1191. if (bta_ag_cb.sco.state == BTA_AG_SCO_LISTEN_ST ||
  1192. bta_ag_cb.sco.state == BTA_AG_SCO_CLOSE_XFER_ST ||
  1193. bta_ag_cb.sco.state == BTA_AG_SCO_OPEN_XFER_ST) {
  1194. /* tell sys to stop av if any */
  1195. bta_sys_sco_use(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
  1196. /* When HS initiated SCO, it cannot be WBS. */
  1197. }
  1198. /* If SCO open was initiated from HS, it must be CVSD */
  1199. p_scb->inuse_codec = BTA_AG_CODEC_NONE;
  1200. /* Send pending commands to create SCO connection to peer */
  1201. bta_ag_create_pending_sco(p_scb, bta_ag_cb.sco.is_local);
  1202. }
  1203. void bta_ag_set_sco_allowed(bool value) {
  1204. sco_allowed = value;
  1205. APPL_TRACE_DEBUG(sco_allowed ? "sco now allowed" : "sco now not allowed");
  1206. }
  1207. const RawAddress& bta_ag_get_active_device() { return active_device_addr; }
  1208. void bta_clear_active_device() { active_device_addr = RawAddress::kEmpty; }
  1209. void bta_ag_api_set_active_device(const RawAddress& new_active_device) {
  1210. if (new_active_device.IsEmpty()) {
  1211. APPL_TRACE_ERROR("%s: empty device", __func__);
  1212. return;
  1213. }
  1214. active_device_addr = new_active_device;
  1215. }