bta_ag_act.cc 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861
  1. /******************************************************************************
  2. *
  3. * Copyright 2003-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 action functions for the audio gateway.
  21. *
  22. ******************************************************************************/
  23. #include <cstring>
  24. #include "bta_ag_api.h"
  25. #include "bta_ag_int.h"
  26. #include "bta_api.h"
  27. #include "bta_dm_api.h"
  28. #include "bta_sys.h"
  29. #include "btif_config.h"
  30. #include "l2c_api.h"
  31. #include "osi/include/osi.h"
  32. #include "port_api.h"
  33. #include "utl.h"
  34. /*****************************************************************************
  35. * Constants
  36. ****************************************************************************/
  37. /* maximum length of data to read from RFCOMM */
  38. #define BTA_AG_RFC_READ_MAX 512
  39. /* maximum AT command length */
  40. #define BTA_AG_CMD_MAX 512
  41. const uint16_t bta_ag_uuid[BTA_AG_NUM_IDX] = {
  42. UUID_SERVCLASS_HEADSET_AUDIO_GATEWAY, UUID_SERVCLASS_AG_HANDSFREE};
  43. const uint8_t bta_ag_sec_id[BTA_AG_NUM_IDX] = {BTM_SEC_SERVICE_HEADSET_AG,
  44. BTM_SEC_SERVICE_AG_HANDSFREE};
  45. const tBTA_SERVICE_ID bta_ag_svc_id[BTA_AG_NUM_IDX] = {BTA_HSP_SERVICE_ID,
  46. BTA_HFP_SERVICE_ID};
  47. const tBTA_SERVICE_MASK bta_ag_svc_mask[BTA_AG_NUM_IDX] = {
  48. BTA_HSP_SERVICE_MASK, BTA_HFP_SERVICE_MASK};
  49. typedef void (*tBTA_AG_ATCMD_CBACK)(tBTA_AG_SCB* p_scb, uint16_t cmd,
  50. uint8_t arg_type, char* p_arg, char* p_end,
  51. int16_t int_arg);
  52. const tBTA_AG_ATCMD_CBACK bta_ag_at_cback_tbl[BTA_AG_NUM_IDX] = {
  53. bta_ag_at_hsp_cback, bta_ag_at_hfp_cback};
  54. /*******************************************************************************
  55. *
  56. * Function bta_ag_cback_open
  57. *
  58. * Description Send open callback event to application.
  59. *
  60. *
  61. * Returns void
  62. *
  63. ******************************************************************************/
  64. static void bta_ag_cback_open(tBTA_AG_SCB* p_scb, const RawAddress& bd_addr,
  65. tBTA_AG_STATUS status) {
  66. tBTA_AG_OPEN open = {};
  67. /* call app callback with open event */
  68. open.hdr.handle = bta_ag_scb_to_idx(p_scb);
  69. open.hdr.app_id = p_scb->app_id;
  70. open.status = status;
  71. open.service_id = bta_ag_svc_id[p_scb->conn_service];
  72. open.bd_addr = bd_addr;
  73. (*bta_ag_cb.p_cback)(BTA_AG_OPEN_EVT, (tBTA_AG*)&open);
  74. }
  75. /*******************************************************************************
  76. *
  77. * Function bta_ag_register
  78. *
  79. * Description This function initializes values of the AG cb and sets up
  80. * the SDP record for the services.
  81. *
  82. *
  83. * Returns void
  84. *
  85. ******************************************************************************/
  86. void bta_ag_register(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data) {
  87. /* initialize control block */
  88. p_scb->reg_services = data.api_register.services;
  89. p_scb->serv_sec_mask = data.api_register.sec_mask;
  90. p_scb->features = data.api_register.features;
  91. p_scb->app_id = data.api_register.app_id;
  92. /* create SDP records */
  93. bta_ag_create_records(p_scb, data);
  94. /* start RFCOMM servers */
  95. bta_ag_start_servers(p_scb, p_scb->reg_services);
  96. /* call app callback with register event */
  97. tBTA_AG_REGISTER reg = {};
  98. reg.hdr.handle = bta_ag_scb_to_idx(p_scb);
  99. reg.hdr.app_id = p_scb->app_id;
  100. reg.status = BTA_AG_SUCCESS;
  101. (*bta_ag_cb.p_cback)(BTA_AG_REGISTER_EVT, (tBTA_AG*)&reg);
  102. }
  103. /*******************************************************************************
  104. *
  105. * Function bta_ag_deregister
  106. *
  107. * Description This function removes the sdp records, closes the RFCOMM
  108. * servers, and deallocates the service control block.
  109. *
  110. *
  111. * Returns void
  112. *
  113. ******************************************************************************/
  114. void bta_ag_deregister(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data) {
  115. /* set dealloc */
  116. p_scb->dealloc = true;
  117. /* remove sdp records */
  118. bta_ag_del_records(p_scb);
  119. /* remove rfcomm servers */
  120. bta_ag_close_servers(p_scb, p_scb->reg_services);
  121. /* dealloc */
  122. bta_ag_scb_dealloc(p_scb);
  123. }
  124. /*******************************************************************************
  125. *
  126. * Function bta_ag_start_dereg
  127. *
  128. * Description Start a deregister event.
  129. *
  130. *
  131. * Returns void
  132. *
  133. ******************************************************************************/
  134. void bta_ag_start_dereg(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data) {
  135. /* set dealloc */
  136. p_scb->dealloc = true;
  137. /* remove sdp records */
  138. bta_ag_del_records(p_scb);
  139. }
  140. /*******************************************************************************
  141. *
  142. * Function bta_ag_start_open
  143. *
  144. * Description This starts an AG open.
  145. *
  146. *
  147. * Returns void
  148. *
  149. ******************************************************************************/
  150. void bta_ag_start_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data) {
  151. p_scb->peer_addr = data.api_open.bd_addr;
  152. p_scb->cli_sec_mask = data.api_open.sec_mask;
  153. p_scb->open_services = p_scb->reg_services;
  154. /* Check if RFCOMM has any incoming connection to avoid collision. */
  155. RawAddress pending_bd_addr = RawAddress::kEmpty;
  156. if (PORT_IsOpening(&pending_bd_addr)) {
  157. /* Let the incoming connection goes through. */
  158. /* Issue collision for this scb for now. */
  159. /* We will decide what to do when we find incoming connetion later. */
  160. bta_ag_collision_cback(0, BTA_ID_AG, 0, p_scb->peer_addr);
  161. return;
  162. }
  163. /* close servers */
  164. bta_ag_close_servers(p_scb, p_scb->reg_services);
  165. /* set role */
  166. p_scb->role = BTA_AG_INT;
  167. /* do service search */
  168. bta_ag_do_disc(p_scb, p_scb->open_services);
  169. }
  170. /*******************************************************************************
  171. *
  172. * Function bta_ag_disc_int_res
  173. *
  174. * Description This function handles a discovery result when initiator.
  175. *
  176. *
  177. * Returns void
  178. *
  179. ******************************************************************************/
  180. void bta_ag_disc_int_res(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data) {
  181. uint16_t event = BTA_AG_DISC_FAIL_EVT;
  182. APPL_TRACE_DEBUG("bta_ag_disc_int_res: Status: %d", data.disc_result.status);
  183. /* if found service */
  184. if (data.disc_result.status == SDP_SUCCESS ||
  185. data.disc_result.status == SDP_DB_FULL) {
  186. /* get attributes */
  187. if (bta_ag_sdp_find_attr(p_scb, p_scb->open_services)) {
  188. /* set connected service */
  189. p_scb->conn_service = bta_ag_service_to_idx(p_scb->open_services);
  190. /* send ourselves sdp ok event */
  191. event = BTA_AG_DISC_OK_EVT;
  192. }
  193. }
  194. /* free discovery db */
  195. bta_ag_free_db(p_scb, data);
  196. /* if service not found check if we should search for other service */
  197. if ((event == BTA_AG_DISC_FAIL_EVT) &&
  198. (data.disc_result.status == SDP_SUCCESS ||
  199. data.disc_result.status == SDP_DB_FULL ||
  200. data.disc_result.status == SDP_NO_RECS_MATCH)) {
  201. if ((p_scb->open_services & BTA_HFP_SERVICE_MASK) &&
  202. (p_scb->open_services & BTA_HSP_SERVICE_MASK)) {
  203. /* search for HSP */
  204. p_scb->open_services &= ~BTA_HFP_SERVICE_MASK;
  205. bta_ag_do_disc(p_scb, p_scb->open_services);
  206. } else if ((p_scb->open_services & BTA_HSP_SERVICE_MASK) &&
  207. (p_scb->hsp_version == HSP_VERSION_1_2)) {
  208. /* search for UUID_SERVCLASS_HEADSET instead */
  209. p_scb->hsp_version = HSP_VERSION_1_0;
  210. bta_ag_do_disc(p_scb, p_scb->open_services);
  211. } else {
  212. /* send ourselves sdp ok/fail event */
  213. bta_ag_sm_execute(p_scb, event, data);
  214. }
  215. } else {
  216. /* send ourselves sdp ok/fail event */
  217. bta_ag_sm_execute(p_scb, event, data);
  218. }
  219. }
  220. /*******************************************************************************
  221. *
  222. * Function bta_ag_disc_acp_res
  223. *
  224. * Description This function handles a discovery result when acceptor.
  225. *
  226. *
  227. * Returns void
  228. *
  229. ******************************************************************************/
  230. void bta_ag_disc_acp_res(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data) {
  231. /* if found service */
  232. if (data.disc_result.status == SDP_SUCCESS ||
  233. data.disc_result.status == SDP_DB_FULL) {
  234. /* get attributes */
  235. bta_ag_sdp_find_attr(p_scb, bta_ag_svc_mask[p_scb->conn_service]);
  236. }
  237. /* free discovery db */
  238. bta_ag_free_db(p_scb, data);
  239. }
  240. /*******************************************************************************
  241. *
  242. * Function bta_ag_disc_fail
  243. *
  244. * Description This function handles a discovery failure.
  245. *
  246. *
  247. * Returns void
  248. *
  249. ******************************************************************************/
  250. void bta_ag_disc_fail(tBTA_AG_SCB* p_scb,
  251. UNUSED_ATTR const tBTA_AG_DATA& data) {
  252. /* reopen registered servers */
  253. bta_ag_start_servers(p_scb, p_scb->reg_services);
  254. /* reinitialize stuff */
  255. /* clear the remote BD address */
  256. RawAddress peer_addr = p_scb->peer_addr;
  257. p_scb->peer_addr = RawAddress::kEmpty;
  258. /* call open cback w. failure */
  259. bta_ag_cback_open(p_scb, peer_addr, BTA_AG_FAIL_SDP);
  260. }
  261. /*******************************************************************************
  262. *
  263. * Function bta_ag_open_fail
  264. *
  265. * Description open connection failed.
  266. *
  267. *
  268. * Returns void
  269. *
  270. ******************************************************************************/
  271. void bta_ag_open_fail(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data) {
  272. /* call open cback w. failure */
  273. bta_ag_cback_open(p_scb, data.api_open.bd_addr, BTA_AG_FAIL_RESOURCES);
  274. }
  275. /*******************************************************************************
  276. *
  277. * Function bta_ag_rfc_fail
  278. *
  279. * Description RFCOMM connection failed.
  280. *
  281. *
  282. * Returns void
  283. *
  284. ******************************************************************************/
  285. void bta_ag_rfc_fail(tBTA_AG_SCB* p_scb, UNUSED_ATTR const tBTA_AG_DATA& data) {
  286. RawAddress peer_addr = p_scb->peer_addr;
  287. /* reinitialize stuff */
  288. p_scb->conn_handle = 0;
  289. p_scb->conn_service = 0;
  290. p_scb->peer_features = 0;
  291. p_scb->peer_codecs = BTA_AG_CODEC_CVSD;
  292. p_scb->sco_codec = BTA_AG_CODEC_CVSD;
  293. p_scb->role = 0;
  294. p_scb->svc_conn = false;
  295. p_scb->hsp_version = HSP_VERSION_1_2;
  296. /*Clear the BD address*/
  297. p_scb->peer_addr = RawAddress::kEmpty;
  298. /* reopen registered servers */
  299. bta_ag_start_servers(p_scb, p_scb->reg_services);
  300. /* call open cback w. failure */
  301. bta_ag_cback_open(p_scb, peer_addr, BTA_AG_FAIL_RFCOMM);
  302. }
  303. /*******************************************************************************
  304. *
  305. * Function bta_ag_rfc_close
  306. *
  307. * Description RFCOMM connection closed.
  308. *
  309. *
  310. * Returns void
  311. *
  312. ******************************************************************************/
  313. void bta_ag_rfc_close(tBTA_AG_SCB* p_scb,
  314. UNUSED_ATTR const tBTA_AG_DATA& data) {
  315. tBTA_AG_CLOSE close = {};
  316. tBTA_SERVICE_MASK services;
  317. int i, num_active_conn = 0;
  318. /* reinitialize stuff */
  319. p_scb->conn_service = 0;
  320. p_scb->peer_features = 0;
  321. p_scb->peer_codecs = BTA_AG_CODEC_CVSD;
  322. p_scb->sco_codec = BTA_AG_CODEC_CVSD;
  323. /* Clear these flags upon SLC teardown */
  324. p_scb->codec_updated = false;
  325. p_scb->codec_fallback = false;
  326. p_scb->codec_msbc_settings = BTA_AG_SCO_MSBC_SETTINGS_T2;
  327. p_scb->role = 0;
  328. p_scb->post_sco = BTA_AG_POST_SCO_NONE;
  329. p_scb->svc_conn = false;
  330. p_scb->hsp_version = HSP_VERSION_1_2;
  331. bta_ag_at_reinit(&p_scb->at_cb);
  332. for (auto& peer_hf_indicator : p_scb->peer_hf_indicators) {
  333. peer_hf_indicator = {};
  334. }
  335. for (auto& local_hf_indicator : p_scb->local_hf_indicators) {
  336. local_hf_indicator = {};
  337. }
  338. /* stop timers */
  339. alarm_cancel(p_scb->ring_timer);
  340. alarm_cancel(p_scb->codec_negotiation_timer);
  341. close.hdr.handle = bta_ag_scb_to_idx(p_scb);
  342. close.hdr.app_id = p_scb->app_id;
  343. close.bd_addr = p_scb->peer_addr;
  344. bta_sys_conn_close(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
  345. if (bta_ag_get_active_device() == p_scb->peer_addr) {
  346. bta_clear_active_device();
  347. }
  348. /* call close cback */
  349. (*bta_ag_cb.p_cback)(BTA_AG_CLOSE_EVT, (tBTA_AG*)&close);
  350. /* if not deregistering (deallocating) reopen registered servers */
  351. if (!p_scb->dealloc) {
  352. /* Clear peer bd_addr so instance can be reused */
  353. p_scb->peer_addr = RawAddress::kEmpty;
  354. /* start only unopened server */
  355. services = p_scb->reg_services;
  356. for (i = 0; i < BTA_AG_NUM_IDX && services != 0; i++) {
  357. if (p_scb->serv_handle[i])
  358. services &= ~((tBTA_SERVICE_MASK)1 << (BTA_HSP_SERVICE_ID + i));
  359. }
  360. bta_ag_start_servers(p_scb, services);
  361. p_scb->conn_handle = 0;
  362. /* Make sure SCO state is BTA_AG_SCO_SHUTDOWN_ST */
  363. bta_ag_sco_shutdown(p_scb, tBTA_AG_DATA::kEmpty);
  364. /* Check if all the SLCs are down */
  365. for (i = 0; i < BTA_AG_MAX_NUM_CLIENTS; i++) {
  366. if (bta_ag_cb.scb[i].in_use && bta_ag_cb.scb[i].svc_conn)
  367. num_active_conn++;
  368. }
  369. if (!num_active_conn) {
  370. bta_sys_sco_unuse(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
  371. }
  372. }
  373. /* else close port and deallocate scb */
  374. else {
  375. RFCOMM_RemoveServer(p_scb->conn_handle);
  376. bta_ag_scb_dealloc(p_scb);
  377. }
  378. }
  379. /*******************************************************************************
  380. *
  381. * Function bta_ag_rfc_open
  382. *
  383. * Description Handle RFCOMM channel open.
  384. *
  385. *
  386. * Returns void
  387. *
  388. ******************************************************************************/
  389. void bta_ag_rfc_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data) {
  390. /* initialize AT feature variables */
  391. p_scb->clip_enabled = false;
  392. p_scb->ccwa_enabled = false;
  393. p_scb->cmer_enabled = false;
  394. p_scb->cmee_enabled = false;
  395. p_scb->inband_enabled =
  396. ((p_scb->features & BTA_AG_FEAT_INBAND) == BTA_AG_FEAT_INBAND);
  397. if (p_scb->conn_service == BTA_AG_HFP) {
  398. size_t version_value_size = sizeof(p_scb->peer_version);
  399. if (!btif_config_get_bin(
  400. p_scb->peer_addr.ToString(), HFP_VERSION_CONFIG_KEY,
  401. (uint8_t*)&p_scb->peer_version, &version_value_size)) {
  402. APPL_TRACE_WARNING("%s: Failed read cached peer HFP version for %s",
  403. __func__, p_scb->peer_addr.ToString().c_str());
  404. p_scb->peer_version = HFP_HSP_VERSION_UNKNOWN;
  405. }
  406. size_t sdp_features_size = sizeof(p_scb->peer_sdp_features);
  407. if (btif_config_get_bin(
  408. p_scb->peer_addr.ToString(), HFP_SDP_FEATURES_CONFIG_KEY,
  409. (uint8_t*)&p_scb->peer_sdp_features, &sdp_features_size)) {
  410. bool sdp_wbs_support = p_scb->peer_sdp_features & BTA_AG_FEAT_WBS_SUPPORT;
  411. if (!p_scb->received_at_bac && sdp_wbs_support) {
  412. p_scb->codec_updated = true;
  413. p_scb->peer_codecs = BTA_AG_CODEC_CVSD & BTA_AG_CODEC_MSBC;
  414. p_scb->sco_codec = UUID_CODEC_MSBC;
  415. }
  416. } else {
  417. APPL_TRACE_WARNING("%s: Failed read cached peer HFP SDP features for %s",
  418. __func__, p_scb->peer_addr.ToString().c_str());
  419. }
  420. }
  421. /* set up AT command interpreter */
  422. p_scb->at_cb.p_at_tbl = bta_ag_at_tbl[p_scb->conn_service];
  423. p_scb->at_cb.p_cmd_cback = bta_ag_at_cback_tbl[p_scb->conn_service];
  424. p_scb->at_cb.p_err_cback = bta_ag_at_err_cback;
  425. p_scb->at_cb.p_user = p_scb;
  426. p_scb->at_cb.cmd_max_len = BTA_AG_CMD_MAX;
  427. bta_ag_at_init(&p_scb->at_cb);
  428. bta_sys_conn_open(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
  429. bta_ag_cback_open(p_scb, p_scb->peer_addr, BTA_AG_SUCCESS);
  430. if (p_scb->conn_service == BTA_AG_HFP) {
  431. /* if hfp start timer for service level conn */
  432. bta_sys_start_timer(p_scb->ring_timer, p_bta_ag_cfg->conn_tout,
  433. BTA_AG_SVC_TIMEOUT_EVT, bta_ag_scb_to_idx(p_scb));
  434. } else {
  435. /* else service level conn is open */
  436. bta_ag_svc_conn_open(p_scb, data);
  437. }
  438. }
  439. /*******************************************************************************
  440. *
  441. * Function bta_ag_rfc_acp_open
  442. *
  443. * Description Handle RFCOMM channel open when accepting connection.
  444. *
  445. *
  446. * Returns void
  447. *
  448. ******************************************************************************/
  449. void bta_ag_rfc_acp_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data) {
  450. APPL_TRACE_DEBUG("%s: serv_handle0 = %d serv_handle = %d", __func__,
  451. p_scb->serv_handle[0], p_scb->serv_handle[1]);
  452. /* set role */
  453. p_scb->role = BTA_AG_ACP;
  454. /* get bd addr of peer */
  455. uint16_t lcid = 0;
  456. RawAddress dev_addr = RawAddress::kEmpty;
  457. int status = PORT_CheckConnection(data.rfc.port_handle, &dev_addr, &lcid);
  458. if (status != PORT_SUCCESS) {
  459. LOG(ERROR) << __func__ << ", PORT_CheckConnection returned " << status;
  460. return;
  461. }
  462. /* Collision Handling */
  463. for (tBTA_AG_SCB& ag_scb : bta_ag_cb.scb) {
  464. // Cancel any pending collision timers
  465. if (ag_scb.in_use && alarm_is_scheduled(ag_scb.collision_timer)) {
  466. VLOG(1) << __func__ << ": cancel collision alarm for "
  467. << ag_scb.peer_addr;
  468. alarm_cancel(ag_scb.collision_timer);
  469. if (dev_addr != ag_scb.peer_addr && p_scb != &ag_scb) {
  470. // Resume outgoing connection if incoming is not on the same device
  471. bta_ag_resume_open(&ag_scb);
  472. }
  473. }
  474. if (dev_addr == ag_scb.peer_addr && p_scb != &ag_scb) {
  475. VLOG(1) << __func__ << ": fail outgoing connection before accepting "
  476. << ag_scb.peer_addr;
  477. // Fail the outgoing connection to clean up any upper layer states
  478. bta_ag_rfc_fail(&ag_scb, tBTA_AG_DATA::kEmpty);
  479. // If client port is opened, close it
  480. if (ag_scb.conn_handle > 0) {
  481. status = RFCOMM_RemoveConnection(ag_scb.conn_handle);
  482. if (status != PORT_SUCCESS) {
  483. LOG(WARNING) << __func__ << ": RFCOMM_RemoveConnection failed for "
  484. << dev_addr << ", handle "
  485. << std::to_string(ag_scb.conn_handle) << ", error "
  486. << status;
  487. }
  488. }
  489. }
  490. VLOG(1) << __func__ << ": dev_addr=" << dev_addr
  491. << ", peer_addr=" << ag_scb.peer_addr
  492. << ", in_use=" << ag_scb.in_use
  493. << ", index=" << bta_ag_scb_to_idx(p_scb);
  494. }
  495. p_scb->peer_addr = dev_addr;
  496. /* determine connected service from port handle */
  497. for (uint8_t i = 0; i < BTA_AG_NUM_IDX; i++) {
  498. APPL_TRACE_DEBUG(
  499. "bta_ag_rfc_acp_open: i = %d serv_handle = %d port_handle = %d", i,
  500. p_scb->serv_handle[i], data.rfc.port_handle);
  501. if (p_scb->serv_handle[i] == data.rfc.port_handle) {
  502. p_scb->conn_service = i;
  503. p_scb->conn_handle = data.rfc.port_handle;
  504. break;
  505. }
  506. }
  507. APPL_TRACE_DEBUG("bta_ag_rfc_acp_open: conn_service = %d conn_handle = %d",
  508. p_scb->conn_service, p_scb->conn_handle);
  509. /* close any unopened server */
  510. bta_ag_close_servers(
  511. p_scb, (p_scb->reg_services & ~bta_ag_svc_mask[p_scb->conn_service]));
  512. /* do service discovery to get features */
  513. bta_ag_do_disc(p_scb, bta_ag_svc_mask[p_scb->conn_service]);
  514. /* continue with common open processing */
  515. bta_ag_rfc_open(p_scb, data);
  516. }
  517. /*******************************************************************************
  518. *
  519. * Function bta_ag_rfc_data
  520. *
  521. * Description Read and process data from RFCOMM.
  522. *
  523. *
  524. * Returns void
  525. *
  526. ******************************************************************************/
  527. void bta_ag_rfc_data(tBTA_AG_SCB* p_scb, UNUSED_ATTR const tBTA_AG_DATA& data) {
  528. uint16_t len;
  529. char buf[BTA_AG_RFC_READ_MAX] = "";
  530. APPL_TRACE_DEBUG("%s", __func__);
  531. /* do the following */
  532. for (;;) {
  533. /* read data from rfcomm; if bad status, we're done */
  534. if (PORT_ReadData(p_scb->conn_handle, buf, BTA_AG_RFC_READ_MAX, &len) !=
  535. PORT_SUCCESS) {
  536. LOG(ERROR) << __func__ << ": failed to read data " << p_scb->peer_addr;
  537. break;
  538. }
  539. /* if no data, we're done */
  540. if (len == 0) {
  541. LOG(WARNING) << __func__ << ": no data for " << p_scb->peer_addr;
  542. break;
  543. }
  544. /* run AT command interpreter on data */
  545. bta_sys_busy(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
  546. bta_ag_at_parse(&p_scb->at_cb, buf, len);
  547. if ((p_scb->sco_idx != BTM_INVALID_SCO_INDEX) &&
  548. bta_ag_sco_is_open(p_scb)) {
  549. APPL_TRACE_DEBUG("%s change link policy for SCO", __func__);
  550. bta_sys_sco_open(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
  551. } else {
  552. bta_sys_idle(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
  553. }
  554. /* no more data to read, we're done */
  555. if (len < BTA_AG_RFC_READ_MAX) {
  556. break;
  557. }
  558. }
  559. }
  560. /*******************************************************************************
  561. *
  562. * Function bta_ag_start_close
  563. *
  564. * Description Start the process of closing SCO and RFCOMM connection.
  565. *
  566. *
  567. * Returns void
  568. *
  569. ******************************************************************************/
  570. void bta_ag_start_close(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data) {
  571. /* Take the link out of sniff and set L2C idle time to 0 */
  572. bta_dm_pm_active(p_scb->peer_addr);
  573. L2CA_SetIdleTimeoutByBdAddr(p_scb->peer_addr, 0, BT_TRANSPORT_BR_EDR);
  574. /* if SCO is open close SCO and wait on RFCOMM close */
  575. if (bta_ag_sco_is_open(p_scb)) {
  576. p_scb->post_sco = BTA_AG_POST_SCO_CLOSE_RFC;
  577. } else {
  578. p_scb->post_sco = BTA_AG_POST_SCO_NONE;
  579. bta_ag_rfc_do_close(p_scb, data);
  580. }
  581. /* always do SCO shutdown to handle all SCO corner cases */
  582. bta_ag_sco_shutdown(p_scb, data);
  583. }
  584. /*******************************************************************************
  585. *
  586. * Function bta_ag_post_sco_open
  587. *
  588. * Description Perform post-SCO open action, if any
  589. *
  590. *
  591. * Returns void
  592. *
  593. ******************************************************************************/
  594. void bta_ag_post_sco_open(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data) {
  595. switch (p_scb->post_sco) {
  596. case BTA_AG_POST_SCO_RING:
  597. bta_ag_send_ring(p_scb, data);
  598. p_scb->post_sco = BTA_AG_POST_SCO_NONE;
  599. break;
  600. case BTA_AG_POST_SCO_CALL_CONN:
  601. bta_ag_send_call_inds(p_scb, BTA_AG_IN_CALL_CONN_RES);
  602. p_scb->post_sco = BTA_AG_POST_SCO_NONE;
  603. break;
  604. default:
  605. break;
  606. }
  607. }
  608. /*******************************************************************************
  609. *
  610. * Function bta_ag_post_sco_close
  611. *
  612. * Description Perform post-SCO close action, if any
  613. *
  614. *
  615. * Returns void
  616. *
  617. ******************************************************************************/
  618. void bta_ag_post_sco_close(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data) {
  619. switch (p_scb->post_sco) {
  620. case BTA_AG_POST_SCO_CLOSE_RFC:
  621. bta_ag_rfc_do_close(p_scb, data);
  622. p_scb->post_sco = BTA_AG_POST_SCO_NONE;
  623. break;
  624. case BTA_AG_POST_SCO_CALL_CONN:
  625. bta_ag_send_call_inds(p_scb, BTA_AG_IN_CALL_CONN_RES);
  626. p_scb->post_sco = BTA_AG_POST_SCO_NONE;
  627. break;
  628. case BTA_AG_POST_SCO_CALL_ORIG:
  629. bta_ag_send_call_inds(p_scb, BTA_AG_OUT_CALL_ORIG_RES);
  630. p_scb->post_sco = BTA_AG_POST_SCO_NONE;
  631. break;
  632. case BTA_AG_POST_SCO_CALL_END:
  633. bta_ag_send_call_inds(p_scb, BTA_AG_END_CALL_RES);
  634. p_scb->post_sco = BTA_AG_POST_SCO_NONE;
  635. break;
  636. case BTA_AG_POST_SCO_CALL_END_INCALL:
  637. bta_ag_send_call_inds(p_scb, BTA_AG_END_CALL_RES);
  638. /* Sending callsetup IND and Ring were defered to after SCO close. */
  639. bta_ag_send_call_inds(p_scb, BTA_AG_IN_CALL_RES);
  640. if (bta_ag_inband_enabled(p_scb) &&
  641. !(p_scb->features & BTA_AG_FEAT_NOSCO)) {
  642. p_scb->post_sco = BTA_AG_POST_SCO_RING;
  643. bta_ag_sco_open(p_scb, data);
  644. } else {
  645. p_scb->post_sco = BTA_AG_POST_SCO_NONE;
  646. bta_ag_send_ring(p_scb, data);
  647. }
  648. break;
  649. default:
  650. break;
  651. }
  652. }
  653. /*******************************************************************************
  654. *
  655. * Function bta_ag_svc_conn_open
  656. *
  657. * Description Service level connection opened
  658. *
  659. *
  660. * Returns void
  661. *
  662. ******************************************************************************/
  663. void bta_ag_svc_conn_open(tBTA_AG_SCB* p_scb,
  664. UNUSED_ATTR const tBTA_AG_DATA& data) {
  665. tBTA_AG_CONN evt = {};
  666. if (!p_scb->svc_conn) {
  667. /* set state variable */
  668. p_scb->svc_conn = true;
  669. /* Clear AT+BIA mask from previous SLC if any. */
  670. p_scb->bia_masked_out = 0;
  671. alarm_cancel(p_scb->ring_timer);
  672. /* call callback */
  673. evt.hdr.handle = bta_ag_scb_to_idx(p_scb);
  674. evt.hdr.app_id = p_scb->app_id;
  675. evt.peer_feat = p_scb->peer_features;
  676. evt.bd_addr = p_scb->peer_addr;
  677. evt.peer_codec = p_scb->peer_codecs;
  678. if ((p_scb->call_ind != BTA_AG_CALL_INACTIVE) ||
  679. (p_scb->callsetup_ind != BTA_AG_CALLSETUP_NONE)) {
  680. bta_sys_sco_use(BTA_ID_AG, p_scb->app_id, p_scb->peer_addr);
  681. }
  682. if (bta_ag_get_active_device().IsEmpty()) {
  683. bta_ag_api_set_active_device(p_scb->peer_addr);
  684. }
  685. (*bta_ag_cb.p_cback)(BTA_AG_CONN_EVT, (tBTA_AG*)&evt);
  686. }
  687. }
  688. /*******************************************************************************
  689. *
  690. * Function bta_ag_setcodec
  691. *
  692. * Description Handle API SetCodec
  693. *
  694. *
  695. * Returns void
  696. *
  697. ******************************************************************************/
  698. void bta_ag_setcodec(tBTA_AG_SCB* p_scb, const tBTA_AG_DATA& data) {
  699. tBTA_AG_PEER_CODEC codec_type = data.api_setcodec.codec;
  700. tBTA_AG_VAL val = {};
  701. val.hdr.handle = bta_ag_scb_to_idx(p_scb);
  702. /* Check if the requested codec type is valid */
  703. if ((codec_type != BTA_AG_CODEC_NONE) && (codec_type != BTA_AG_CODEC_CVSD) &&
  704. (codec_type != BTA_AG_CODEC_MSBC)) {
  705. val.num = codec_type;
  706. val.hdr.status = BTA_AG_FAIL_RESOURCES;
  707. APPL_TRACE_ERROR("bta_ag_setcodec error: unsupported codec type %d",
  708. codec_type);
  709. (*bta_ag_cb.p_cback)(BTA_AG_WBS_EVT, (tBTA_AG*)&val);
  710. return;
  711. }
  712. if ((p_scb->peer_codecs & codec_type) || (codec_type == BTA_AG_CODEC_NONE) ||
  713. (codec_type == BTA_AG_CODEC_CVSD)) {
  714. p_scb->sco_codec = codec_type;
  715. p_scb->codec_updated = true;
  716. val.num = codec_type;
  717. val.hdr.status = BTA_AG_SUCCESS;
  718. APPL_TRACE_DEBUG("bta_ag_setcodec: Updated codec type %d", codec_type);
  719. } else {
  720. val.num = codec_type;
  721. val.hdr.status = BTA_AG_FAIL_RESOURCES;
  722. APPL_TRACE_ERROR("bta_ag_setcodec error: unsupported codec type %d",
  723. codec_type);
  724. }
  725. (*bta_ag_cb.p_cback)(BTA_AG_WBS_EVT, (tBTA_AG*)&val);
  726. }
  727. static void bta_ag_collision_timer_cback(void* data) {
  728. if (data == nullptr) {
  729. LOG(ERROR) << __func__ << ": data should never be null in a timer callback";
  730. return;
  731. }
  732. /* If the peer haven't opened AG connection */
  733. /* we will restart opening process. */
  734. bta_ag_resume_open(static_cast<tBTA_AG_SCB*>(data));
  735. }
  736. void bta_ag_handle_collision(tBTA_AG_SCB* p_scb,
  737. UNUSED_ATTR const tBTA_AG_DATA& data) {
  738. /* Cancel SDP if it had been started. */
  739. if (p_scb->p_disc_db) {
  740. SDP_CancelServiceSearch(p_scb->p_disc_db);
  741. bta_ag_free_db(p_scb, tBTA_AG_DATA::kEmpty);
  742. }
  743. /* reopen registered servers */
  744. /* Collision may be detected before or after we close servers. */
  745. if (bta_ag_is_server_closed(p_scb)) {
  746. bta_ag_start_servers(p_scb, p_scb->reg_services);
  747. }
  748. /* Start timer to han */
  749. alarm_set_on_mloop(p_scb->collision_timer, BTA_AG_COLLISION_TIMEOUT_MS,
  750. bta_ag_collision_timer_cback, p_scb);
  751. }