bta_sys_main.cc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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 is the main implementation file for the BTA system manager.
  21. *
  22. ******************************************************************************/
  23. #define LOG_TAG "bt_bta_sys_main"
  24. #include <base/bind.h>
  25. #include <base/logging.h>
  26. #include <string.h>
  27. #include "bt_common.h"
  28. #include "bta_api.h"
  29. #include "bta_sys.h"
  30. #include "bta_sys_int.h"
  31. #include "btm_api.h"
  32. #include "btu.h"
  33. #include "osi/include/alarm.h"
  34. #include "osi/include/fixed_queue.h"
  35. #include "osi/include/log.h"
  36. #include "osi/include/osi.h"
  37. #include "utl.h"
  38. #if (defined BTA_AR_INCLUDED) && (BTA_AR_INCLUDED == TRUE)
  39. #include "bta_ar_api.h"
  40. #endif
  41. /* system manager control block definition */
  42. tBTA_SYS_CB bta_sys_cb;
  43. /* trace level */
  44. /* TODO Hard-coded trace levels - Needs to be configurable */
  45. uint8_t appl_trace_level = BT_TRACE_LEVEL_WARNING; // APPL_INITIAL_TRACE_LEVEL;
  46. uint8_t btif_trace_level = BT_TRACE_LEVEL_WARNING;
  47. static const tBTA_SYS_REG bta_sys_hw_reg = {bta_sys_sm_execute, NULL};
  48. /* type for action functions */
  49. typedef void (*tBTA_SYS_ACTION)(tBTA_SYS_HW_MSG* p_data);
  50. /* action function list */
  51. const tBTA_SYS_ACTION bta_sys_action[] = {
  52. /* device manager local device API events - cf bta_sys.h for events */
  53. bta_sys_hw_api_enable, /* 0 BTA_SYS_HW_API_ENABLE_EVT */
  54. bta_sys_hw_evt_enabled, /* 1 BTA_SYS_HW_EVT_ENABLED_EVT */
  55. bta_sys_hw_evt_stack_enabled, /* 2 BTA_SYS_HW_EVT_STACK_ENABLED_EVT */
  56. bta_sys_hw_api_disable, /* 3 BTA_SYS_HW_API_DISABLE_EVT */
  57. bta_sys_hw_evt_disabled, /* 4 BTA_SYS_HW_EVT_DISABLED_EVT */
  58. bta_sys_hw_error /* 5 BTA_SYS_HW_ERROR_EVT */
  59. };
  60. /* state machine action enumeration list */
  61. enum {
  62. /* device manager local device API events */
  63. BTA_SYS_HW_API_ENABLE,
  64. BTA_SYS_HW_EVT_ENABLED,
  65. BTA_SYS_HW_EVT_STACK_ENABLED,
  66. BTA_SYS_HW_API_DISABLE,
  67. BTA_SYS_HW_EVT_DISABLED,
  68. BTA_SYS_HW_ERROR
  69. };
  70. #define BTA_SYS_NUM_ACTIONS (BTA_SYS_MAX_EVT & 0x00ff)
  71. #define BTA_SYS_IGNORE BTA_SYS_NUM_ACTIONS
  72. /* state table information */
  73. #define BTA_SYS_ACTIONS 2 /* number of actions */
  74. #define BTA_SYS_NEXT_STATE 2 /* position of next state */
  75. #define BTA_SYS_NUM_COLS 3 /* number of columns in state tables */
  76. /* state table for OFF state */
  77. const uint8_t bta_sys_hw_off[][BTA_SYS_NUM_COLS] = {
  78. /* Event Action 1 Action 2
  79. Next State */
  80. /* API_ENABLE */ {BTA_SYS_HW_API_ENABLE, BTA_SYS_IGNORE,
  81. BTA_SYS_HW_STARTING},
  82. /* EVT_ENABLED */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_STARTING},
  83. /* STACK_ENABLED */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
  84. /* API_DISABLE */ {BTA_SYS_HW_EVT_DISABLED, BTA_SYS_IGNORE,
  85. BTA_SYS_HW_OFF},
  86. /* EVT_DISABLED */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_OFF},
  87. /* EVT_ERROR */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_OFF}};
  88. const uint8_t bta_sys_hw_starting[][BTA_SYS_NUM_COLS] = {
  89. /* Event Action 1 Action 2
  90. Next State */
  91. /* API_ENABLE */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE,
  92. BTA_SYS_HW_STARTING}, /* wait for completion event */
  93. /* EVT_ENABLED */ {BTA_SYS_HW_EVT_ENABLED, BTA_SYS_IGNORE,
  94. BTA_SYS_HW_STARTING},
  95. /* STACK_ENABLED */ {BTA_SYS_HW_EVT_STACK_ENABLED, BTA_SYS_IGNORE,
  96. BTA_SYS_HW_ON},
  97. /* API_DISABLE */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE,
  98. BTA_SYS_HW_STOPPING}, /* successive disable/enable:
  99. change state wait for
  100. completion to disable */
  101. /* EVT_DISABLED */ {BTA_SYS_HW_EVT_DISABLED, BTA_SYS_HW_API_ENABLE,
  102. BTA_SYS_HW_STARTING}, /* successive enable/disable:
  103. notify, then restart HW */
  104. /* EVT_ERROR */ {BTA_SYS_HW_ERROR, BTA_SYS_IGNORE, BTA_SYS_HW_ON}};
  105. const uint8_t bta_sys_hw_on[][BTA_SYS_NUM_COLS] = {
  106. /* Event Action 1 Action 2
  107. Next State */
  108. /* API_ENABLE */ {BTA_SYS_HW_API_ENABLE, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
  109. /* EVT_ENABLED */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
  110. /* STACK_ENABLED */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
  111. /* API_DISABLE */
  112. {BTA_SYS_HW_API_DISABLE, BTA_SYS_IGNORE,
  113. BTA_SYS_HW_ON}, /* don't change the state here, as some
  114. other modules might be active */
  115. /* EVT_DISABLED */ {BTA_SYS_HW_ERROR, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
  116. /* EVT_ERROR */ {BTA_SYS_HW_ERROR, BTA_SYS_IGNORE, BTA_SYS_HW_ON}};
  117. const uint8_t bta_sys_hw_stopping[][BTA_SYS_NUM_COLS] = {
  118. /* Event Action 1 Action 2
  119. Next State */
  120. /* API_ENABLE */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE,
  121. BTA_SYS_HW_STARTING}, /* change state, and wait for
  122. completion event to enable */
  123. /* EVT_ENABLED */ {BTA_SYS_HW_EVT_ENABLED, BTA_SYS_IGNORE,
  124. BTA_SYS_HW_STOPPING}, /* successive enable/disable:
  125. finish the enable before
  126. disabling */
  127. /* STACK_ENABLED */ {BTA_SYS_HW_EVT_STACK_ENABLED, BTA_SYS_HW_API_DISABLE,
  128. BTA_SYS_HW_STOPPING}, /* successive enable/disable:
  129. notify, then stop */
  130. /* API_DISABLE */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE,
  131. BTA_SYS_HW_STOPPING}, /* wait for completion event */
  132. /* EVT_DISABLED */ {BTA_SYS_HW_EVT_DISABLED, BTA_SYS_IGNORE,
  133. BTA_SYS_HW_OFF},
  134. /* EVT_ERROR */ {BTA_SYS_HW_API_DISABLE, BTA_SYS_IGNORE,
  135. BTA_SYS_HW_STOPPING}};
  136. typedef const uint8_t (*tBTA_SYS_ST_TBL)[BTA_SYS_NUM_COLS];
  137. /* state table */
  138. const tBTA_SYS_ST_TBL bta_sys_st_tbl[] = {
  139. bta_sys_hw_off, /* BTA_SYS_HW_OFF */
  140. bta_sys_hw_starting, /* BTA_SYS_HW_STARTING */
  141. bta_sys_hw_on, /* BTA_SYS_HW_ON */
  142. bta_sys_hw_stopping /* BTA_SYS_HW_STOPPING */
  143. };
  144. /*******************************************************************************
  145. *
  146. * Function bta_sys_init
  147. *
  148. * Description BTA initialization; called from task initialization.
  149. *
  150. *
  151. * Returns void
  152. *
  153. ******************************************************************************/
  154. void bta_sys_init(void) {
  155. memset(&bta_sys_cb, 0, sizeof(tBTA_SYS_CB));
  156. appl_trace_level = APPL_INITIAL_TRACE_LEVEL;
  157. /* register BTA SYS message handler */
  158. bta_sys_register(BTA_ID_SYS, &bta_sys_hw_reg);
  159. /* register for BTM notifications */
  160. BTM_RegisterForDeviceStatusNotif(&bta_sys_hw_btm_cback);
  161. #if (defined BTA_AR_INCLUDED) && (BTA_AR_INCLUDED == TRUE)
  162. bta_ar_init();
  163. #endif
  164. }
  165. void bta_sys_free(void) {
  166. }
  167. /*******************************************************************************
  168. *
  169. * Function bta_dm_sm_execute
  170. *
  171. * Description State machine event handling function for DM
  172. *
  173. *
  174. * Returns void
  175. *
  176. ******************************************************************************/
  177. bool bta_sys_sm_execute(BT_HDR* p_msg) {
  178. bool freebuf = true;
  179. tBTA_SYS_ST_TBL state_table;
  180. uint8_t action;
  181. int i;
  182. APPL_TRACE_EVENT("bta_sys_sm_execute state:%d, event:0x%x", bta_sys_cb.state,
  183. p_msg->event);
  184. /* look up the state table for the current state */
  185. state_table = bta_sys_st_tbl[bta_sys_cb.state];
  186. /* update state */
  187. bta_sys_cb.state = state_table[p_msg->event & 0x00ff][BTA_SYS_NEXT_STATE];
  188. /* execute action functions */
  189. for (i = 0; i < BTA_SYS_ACTIONS; i++) {
  190. action = state_table[p_msg->event & 0x00ff][i];
  191. if (action != BTA_SYS_IGNORE) {
  192. (*bta_sys_action[action])((tBTA_SYS_HW_MSG*)p_msg);
  193. } else {
  194. break;
  195. }
  196. }
  197. return freebuf;
  198. }
  199. void bta_sys_hw_register(tBTA_SYS_HW_MODULE module, tBTA_SYS_HW_CBACK* cback) {
  200. bta_sys_cb.sys_hw_cback[module] = cback;
  201. }
  202. void bta_sys_hw_unregister(tBTA_SYS_HW_MODULE module) {
  203. bta_sys_cb.sys_hw_cback[module] = NULL;
  204. }
  205. /*******************************************************************************
  206. *
  207. * Function bta_sys_hw_btm_cback
  208. *
  209. * Description This function is registered by BTA SYS to BTM in order to get
  210. * status notifications
  211. *
  212. *
  213. * Returns
  214. *
  215. ******************************************************************************/
  216. void bta_sys_hw_btm_cback(tBTM_DEV_STATUS status) {
  217. tBTA_SYS_HW_MSG* sys_event =
  218. (tBTA_SYS_HW_MSG*)osi_malloc(sizeof(tBTA_SYS_HW_MSG));
  219. APPL_TRACE_DEBUG("%s was called with parameter: %i", __func__, status);
  220. /* send a message to BTA SYS */
  221. if (status == BTM_DEV_STATUS_UP) {
  222. sys_event->hdr.event = BTA_SYS_EVT_STACK_ENABLED_EVT;
  223. } else if (status == BTM_DEV_STATUS_DOWN) {
  224. sys_event->hdr.event = BTA_SYS_ERROR_EVT;
  225. } else {
  226. /* BTM_DEV_STATUS_CMD_TOUT is ignored for now. */
  227. osi_free_and_reset((void**)&sys_event);
  228. }
  229. if (sys_event) bta_sys_sendmsg(sys_event);
  230. }
  231. /*******************************************************************************
  232. *
  233. * Function bta_sys_hw_error
  234. *
  235. * Description In case the HW device stops answering... Try to turn it off,
  236. * then re-enable all
  237. * previously active SW modules.
  238. *
  239. * Returns success or failure
  240. *
  241. ******************************************************************************/
  242. void bta_sys_hw_error(UNUSED_ATTR tBTA_SYS_HW_MSG* p_sys_hw_msg) {
  243. uint8_t module_index;
  244. APPL_TRACE_DEBUG("%s", __func__);
  245. for (module_index = 0; module_index < BTA_SYS_MAX_HW_MODULES;
  246. module_index++) {
  247. if (bta_sys_cb.sys_hw_module_active & ((uint32_t)1 << module_index)) {
  248. switch (module_index) {
  249. case BTA_SYS_HW_BLUETOOTH:
  250. /* Send BTA_SYS_HW_ERROR_EVT to DM */
  251. if (bta_sys_cb.sys_hw_cback[module_index] != NULL)
  252. bta_sys_cb.sys_hw_cback[module_index](BTA_SYS_HW_ERROR_EVT);
  253. break;
  254. default:
  255. /* not yet supported */
  256. break;
  257. }
  258. }
  259. }
  260. }
  261. /*******************************************************************************
  262. *
  263. * Function bta_sys_hw_enable
  264. *
  265. * Description this function is called after API enable and HW has been
  266. * turned on
  267. *
  268. *
  269. * Returns success or failure
  270. *
  271. ******************************************************************************/
  272. void bta_sys_hw_api_enable(tBTA_SYS_HW_MSG* p_sys_hw_msg) {
  273. if ((!bta_sys_cb.sys_hw_module_active) &&
  274. (bta_sys_cb.state != BTA_SYS_HW_ON)) {
  275. /* register which HW module was turned on */
  276. bta_sys_cb.sys_hw_module_active |= ((uint32_t)1 << p_sys_hw_msg->hw_module);
  277. tBTA_SYS_HW_MSG* p_msg =
  278. (tBTA_SYS_HW_MSG*)osi_malloc(sizeof(tBTA_SYS_HW_MSG));
  279. p_msg->hdr.event = BTA_SYS_EVT_ENABLED_EVT;
  280. p_msg->hw_module = p_sys_hw_msg->hw_module;
  281. bta_sys_sendmsg(p_msg);
  282. } else {
  283. /* register which HW module was turned on */
  284. bta_sys_cb.sys_hw_module_active |= ((uint32_t)1 << p_sys_hw_msg->hw_module);
  285. /* HW already in use, so directly notify the caller */
  286. if (bta_sys_cb.sys_hw_cback[p_sys_hw_msg->hw_module] != NULL)
  287. bta_sys_cb.sys_hw_cback[p_sys_hw_msg->hw_module](BTA_SYS_HW_ON_EVT);
  288. }
  289. APPL_TRACE_EVENT("bta_sys_hw_api_enable for %d, active modules 0x%04X",
  290. p_sys_hw_msg->hw_module, bta_sys_cb.sys_hw_module_active);
  291. }
  292. /*******************************************************************************
  293. *
  294. * Function bta_sys_hw_disable
  295. *
  296. * Description if no other module is using the HW, this function will call
  297. * (if defined) a user-macro to turn off the HW
  298. *
  299. *
  300. * Returns success or failure
  301. *
  302. ******************************************************************************/
  303. void bta_sys_hw_api_disable(tBTA_SYS_HW_MSG* p_sys_hw_msg) {
  304. APPL_TRACE_DEBUG("bta_sys_hw_api_disable for %d, active modules: 0x%04X",
  305. p_sys_hw_msg->hw_module, bta_sys_cb.sys_hw_module_active);
  306. /* make sure the related SW blocks were stopped */
  307. bta_sys_disable(p_sys_hw_msg->hw_module);
  308. /* register which module we turn off */
  309. bta_sys_cb.sys_hw_module_active &= ~((uint32_t)1 << p_sys_hw_msg->hw_module);
  310. /* if there are still some SW modules using the HW, just provide an answer to
  311. * the calling */
  312. if (bta_sys_cb.sys_hw_module_active != 0) {
  313. /* if there are still some SW modules using the HW, directly notify the
  314. * caller */
  315. if (bta_sys_cb.sys_hw_cback[p_sys_hw_msg->hw_module] != NULL)
  316. bta_sys_cb.sys_hw_cback[p_sys_hw_msg->hw_module](BTA_SYS_HW_OFF_EVT);
  317. } else {
  318. /* manually update the state of our system */
  319. bta_sys_cb.state = BTA_SYS_HW_STOPPING;
  320. tBTA_SYS_HW_MSG* p_msg =
  321. (tBTA_SYS_HW_MSG*)osi_malloc(sizeof(tBTA_SYS_HW_MSG));
  322. p_msg->hdr.event = BTA_SYS_EVT_DISABLED_EVT;
  323. p_msg->hw_module = p_sys_hw_msg->hw_module;
  324. bta_sys_sendmsg(p_msg);
  325. }
  326. }
  327. /*******************************************************************************
  328. *
  329. * Function bta_sys_hw_event_enabled
  330. *
  331. * Description
  332. *
  333. *
  334. * Returns success or failure
  335. *
  336. ******************************************************************************/
  337. void bta_sys_hw_evt_enabled(tBTA_SYS_HW_MSG* p_sys_hw_msg) {
  338. APPL_TRACE_EVENT("bta_sys_hw_evt_enabled for %i", p_sys_hw_msg->hw_module);
  339. BTM_DeviceReset(NULL);
  340. }
  341. /*******************************************************************************
  342. *
  343. * Function bta_sys_hw_event_disabled
  344. *
  345. * Description
  346. *
  347. *
  348. * Returns success or failure
  349. *
  350. ******************************************************************************/
  351. void bta_sys_hw_evt_disabled(tBTA_SYS_HW_MSG* p_sys_hw_msg) {
  352. uint8_t hw_module_index;
  353. APPL_TRACE_DEBUG("bta_sys_hw_evt_disabled - module 0x%X",
  354. p_sys_hw_msg->hw_module);
  355. for (hw_module_index = 0; hw_module_index < BTA_SYS_MAX_HW_MODULES;
  356. hw_module_index++) {
  357. if (bta_sys_cb.sys_hw_cback[hw_module_index] != NULL)
  358. bta_sys_cb.sys_hw_cback[hw_module_index](BTA_SYS_HW_OFF_EVT);
  359. }
  360. }
  361. /*******************************************************************************
  362. *
  363. * Function bta_sys_hw_event_stack_enabled
  364. *
  365. * Description we receive this event once the SW side is ready (stack, FW
  366. * download,... ), i.e. we can really start using the device. So
  367. * notify the app.
  368. *
  369. * Returns success or failure
  370. *
  371. ******************************************************************************/
  372. void bta_sys_hw_evt_stack_enabled(UNUSED_ATTR tBTA_SYS_HW_MSG* p_sys_hw_msg) {
  373. uint8_t hw_module_index;
  374. APPL_TRACE_DEBUG(" bta_sys_hw_evt_stack_enabled!notify the callers");
  375. for (hw_module_index = 0; hw_module_index < BTA_SYS_MAX_HW_MODULES;
  376. hw_module_index++) {
  377. if (bta_sys_cb.sys_hw_cback[hw_module_index] != NULL)
  378. bta_sys_cb.sys_hw_cback[hw_module_index](BTA_SYS_HW_ON_EVT);
  379. }
  380. }
  381. /*******************************************************************************
  382. *
  383. * Function bta_sys_event
  384. *
  385. * Description BTA event handler; called from task event handler.
  386. *
  387. *
  388. * Returns void
  389. *
  390. ******************************************************************************/
  391. void bta_sys_event(BT_HDR* p_msg) {
  392. uint8_t id;
  393. bool freebuf = true;
  394. APPL_TRACE_EVENT("%s: Event 0x%x", __func__, p_msg->event);
  395. /* get subsystem id from event */
  396. id = (uint8_t)(p_msg->event >> 8);
  397. /* verify id and call subsystem event handler */
  398. if ((id < BTA_ID_MAX) && (bta_sys_cb.reg[id] != NULL)) {
  399. freebuf = (*bta_sys_cb.reg[id]->evt_hdlr)(p_msg);
  400. } else {
  401. APPL_TRACE_WARNING("%s: Received unregistered event id %d", __func__, id);
  402. }
  403. if (freebuf) {
  404. osi_free(p_msg);
  405. }
  406. }
  407. /*******************************************************************************
  408. *
  409. * Function bta_sys_register
  410. *
  411. * Description Called by other BTA subsystems to register their event
  412. * handler.
  413. *
  414. *
  415. * Returns void
  416. *
  417. ******************************************************************************/
  418. void bta_sys_register(uint8_t id, const tBTA_SYS_REG* p_reg) {
  419. bta_sys_cb.reg[id] = (tBTA_SYS_REG*)p_reg;
  420. bta_sys_cb.is_reg[id] = true;
  421. }
  422. /*******************************************************************************
  423. *
  424. * Function bta_sys_deregister
  425. *
  426. * Description Called by other BTA subsystems to de-register
  427. * handler.
  428. *
  429. *
  430. * Returns void
  431. *
  432. ******************************************************************************/
  433. void bta_sys_deregister(uint8_t id) { bta_sys_cb.is_reg[id] = false; }
  434. /*******************************************************************************
  435. *
  436. * Function bta_sys_is_register
  437. *
  438. * Description Called by other BTA subsystems to get registeration
  439. * status.
  440. *
  441. *
  442. * Returns void
  443. *
  444. ******************************************************************************/
  445. bool bta_sys_is_register(uint8_t id) { return bta_sys_cb.is_reg[id]; }
  446. /*******************************************************************************
  447. *
  448. * Function bta_sys_sendmsg
  449. *
  450. * Description Send a GKI message to BTA. This function is designed to
  451. * optimize sending of messages to BTA. It is called by BTA
  452. * API functions and call-in functions.
  453. *
  454. * TODO (apanicke): Add location object as parameter for easier
  455. * future debugging when doing alarm refactor
  456. *
  457. *
  458. * Returns void
  459. *
  460. ******************************************************************************/
  461. void bta_sys_sendmsg(void* p_msg) {
  462. if (do_in_main_thread(
  463. FROM_HERE, base::Bind(&bta_sys_event, static_cast<BT_HDR*>(p_msg))) !=
  464. BT_STATUS_SUCCESS) {
  465. LOG(ERROR) << __func__ << ": do_in_main_thread failed";
  466. }
  467. }
  468. /*******************************************************************************
  469. *
  470. * Function bta_sys_start_timer
  471. *
  472. * Description Start a protocol timer for the specified amount
  473. * of time in milliseconds.
  474. *
  475. * Returns void
  476. *
  477. ******************************************************************************/
  478. void bta_sys_start_timer(alarm_t* alarm, uint64_t interval_ms, uint16_t event,
  479. uint16_t layer_specific) {
  480. BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
  481. p_buf->event = event;
  482. p_buf->layer_specific = layer_specific;
  483. alarm_set_on_mloop(alarm, interval_ms, bta_sys_sendmsg, p_buf);
  484. }
  485. /*******************************************************************************
  486. *
  487. * Function bta_sys_disable
  488. *
  489. * Description For each registered subsystem execute its disable function.
  490. *
  491. * Returns void
  492. *
  493. ******************************************************************************/
  494. void bta_sys_disable(tBTA_SYS_HW_MODULE module) {
  495. int bta_id = 0;
  496. int bta_id_max = 0;
  497. APPL_TRACE_DEBUG("bta_sys_disable: module %i", module);
  498. switch (module) {
  499. case BTA_SYS_HW_BLUETOOTH:
  500. bta_id = BTA_ID_DM_SEARCH;
  501. bta_id_max = BTA_ID_BLUETOOTH_MAX;
  502. break;
  503. default:
  504. APPL_TRACE_WARNING("bta_sys_disable: unkown module");
  505. return;
  506. }
  507. for (; bta_id <= bta_id_max; bta_id++) {
  508. if (bta_sys_cb.reg[bta_id] != NULL) {
  509. if (bta_sys_cb.is_reg[bta_id] &&
  510. bta_sys_cb.reg[bta_id]->disable != NULL) {
  511. (*bta_sys_cb.reg[bta_id]->disable)();
  512. }
  513. }
  514. }
  515. }
  516. /*******************************************************************************
  517. *
  518. * Function bta_sys_set_trace_level
  519. *
  520. * Description Set trace level for BTA
  521. *
  522. * Returns void
  523. *
  524. ******************************************************************************/
  525. void bta_sys_set_trace_level(uint8_t level) { appl_trace_level = level; }
  526. /*******************************************************************************
  527. *
  528. * Function bta_sys_get_sys_features
  529. *
  530. * Description Returns sys_features to other BTA modules.
  531. *
  532. * Returns sys_features
  533. *
  534. ******************************************************************************/
  535. uint16_t bta_sys_get_sys_features(void) { return bta_sys_cb.sys_features; }