bta_sys_main.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 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 <assert.h>
  25. #include <string.h>
  26. #include "osi/alarm.h"
  27. #include "osi/thread.h"
  28. #include "stack/btm_api.h"
  29. #include "stack/btu.h"
  30. #include "bta/bta_api.h"
  31. #include "bta/bta_sys.h"
  32. #include "bta_sys_int.h"
  33. #include "osi/fixed_queue.h"
  34. #include "osi/hash_map.h"
  35. #include "osi/osi.h"
  36. #include "osi/hash_functions.h"
  37. #if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
  38. #include "bta/bta_ar_api.h"
  39. #endif
  40. #include "bta/utl.h"
  41. #include "osi/allocator.h"
  42. #include "osi/mutex.h"
  43. /* system manager control block definition */
  44. #if BTA_DYNAMIC_MEMORY == FALSE
  45. tBTA_SYS_CB bta_sys_cb;
  46. #else
  47. tBTA_SYS_CB *bta_sys_cb_ptr;
  48. #endif
  49. static hash_map_t *bta_alarm_hash_map;
  50. static const size_t BTA_ALARM_HASH_MAP_SIZE = 17;
  51. static osi_mutex_t bta_alarm_lock;
  52. // extern thread_t *bt_workqueue_thread;
  53. /* trace level */
  54. /* TODO Bluedroid - Hard-coded trace levels - Needs to be configurable */
  55. UINT8 appl_trace_level = APPL_INITIAL_TRACE_LEVEL;
  56. UINT8 btif_trace_level = BTIF_INITIAL_TRACE_LEVEL;
  57. void btu_bta_alarm_ready(fixed_queue_t *queue);
  58. static const tBTA_SYS_REG bta_sys_hw_reg = {
  59. bta_sys_sm_execute,
  60. NULL
  61. };
  62. /* type for action functions */
  63. typedef void (*tBTA_SYS_ACTION)(tBTA_SYS_HW_MSG *p_data);
  64. /* action function list */
  65. const tBTA_SYS_ACTION bta_sys_action[] = {
  66. /* device manager local device API events - cf bta/bta_sys.h for events */
  67. bta_sys_hw_api_enable, /* 0 BTA_SYS_HW_API_ENABLE_EVT */
  68. bta_sys_hw_evt_enabled, /* 1 BTA_SYS_HW_EVT_ENABLED_EVT */
  69. bta_sys_hw_evt_stack_enabled, /* 2 BTA_SYS_HW_EVT_STACK_ENABLED_EVT */
  70. bta_sys_hw_api_disable, /* 3 BTA_SYS_HW_API_DISABLE_EVT */
  71. bta_sys_hw_evt_disabled, /* 4 BTA_SYS_HW_EVT_DISABLED_EVT */
  72. bta_sys_hw_error /* 5 BTA_SYS_HW_ERROR_EVT */
  73. };
  74. /* state machine action enumeration list */
  75. enum {
  76. /* device manager local device API events */
  77. BTA_SYS_HW_API_ENABLE,
  78. BTA_SYS_HW_EVT_ENABLED,
  79. BTA_SYS_HW_EVT_STACK_ENABLED,
  80. BTA_SYS_HW_API_DISABLE,
  81. BTA_SYS_HW_EVT_DISABLED,
  82. BTA_SYS_HW_ERROR
  83. };
  84. #define BTA_SYS_NUM_ACTIONS (BTA_SYS_MAX_EVT & 0x00ff)
  85. #define BTA_SYS_IGNORE BTA_SYS_NUM_ACTIONS
  86. /* state table information */
  87. #define BTA_SYS_ACTIONS 2 /* number of actions */
  88. #define BTA_SYS_NEXT_STATE 2 /* position of next state */
  89. #define BTA_SYS_NUM_COLS 3 /* number of columns in state tables */
  90. /* state table for OFF state */
  91. const UINT8 bta_sys_hw_off[][BTA_SYS_NUM_COLS] = {
  92. /* Event Action 1 Action 2 Next State */
  93. /* API_ENABLE */ {BTA_SYS_HW_API_ENABLE, BTA_SYS_IGNORE, BTA_SYS_HW_STARTING},
  94. /* EVT_ENABLED */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_STARTING},
  95. /* STACK_ENABLED */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
  96. /* API_DISABLE */ {BTA_SYS_HW_EVT_DISABLED, BTA_SYS_IGNORE, BTA_SYS_HW_OFF},
  97. /* EVT_DISABLED */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_OFF},
  98. /* EVT_ERROR */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_OFF}
  99. };
  100. const UINT8 bta_sys_hw_starting[][BTA_SYS_NUM_COLS] = {
  101. /* Event Action 1 Action 2 Next State */
  102. /* API_ENABLE */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_STARTING}, /* wait for completion event */
  103. /* EVT_ENABLED */ {BTA_SYS_HW_EVT_ENABLED, BTA_SYS_IGNORE, BTA_SYS_HW_STARTING},
  104. /* STACK_ENABLED */ {BTA_SYS_HW_EVT_STACK_ENABLED, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
  105. /* API_DISABLE */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_STOPPING}, /* successive disable/enable: change state wait for completion to disable */
  106. /* EVT_DISABLED */ {BTA_SYS_HW_EVT_DISABLED, BTA_SYS_HW_API_ENABLE, BTA_SYS_HW_STARTING}, /* successive enable/disable: notify, then restart HW */
  107. /* EVT_ERROR */ {BTA_SYS_HW_ERROR, BTA_SYS_IGNORE, BTA_SYS_HW_ON}
  108. };
  109. const UINT8 bta_sys_hw_on[][BTA_SYS_NUM_COLS] = {
  110. /* Event Action 1 Action 2 Next State */
  111. /* API_ENABLE */ {BTA_SYS_HW_API_ENABLE, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
  112. /* EVT_ENABLED */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
  113. /* STACK_ENABLED */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
  114. /* API_DISABLE */ {BTA_SYS_HW_API_DISABLE, BTA_SYS_IGNORE, BTA_SYS_HW_ON}, /* don't change the state here, as some 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. };
  118. const UINT8 bta_sys_hw_stopping[][BTA_SYS_NUM_COLS] = {
  119. /* Event Action 1 Action 2 Next State */
  120. /* API_ENABLE */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_STARTING}, /* change state, and wait for completion event to enable */
  121. /* EVT_ENABLED */ {BTA_SYS_HW_EVT_ENABLED, BTA_SYS_IGNORE, BTA_SYS_HW_STOPPING}, /* successive enable/disable: finish the enable before disabling */
  122. /* STACK_ENABLED */ {BTA_SYS_HW_EVT_STACK_ENABLED, BTA_SYS_HW_API_DISABLE, BTA_SYS_HW_STOPPING}, /* successive enable/disable: notify, then stop */
  123. /* API_DISABLE */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_STOPPING}, /* wait for completion event */
  124. /* EVT_DISABLED */ {BTA_SYS_HW_EVT_DISABLED, BTA_SYS_IGNORE, BTA_SYS_HW_OFF},
  125. /* EVT_ERROR */ {BTA_SYS_HW_API_DISABLE, BTA_SYS_IGNORE, BTA_SYS_HW_STOPPING}
  126. };
  127. typedef const UINT8 (*tBTA_SYS_ST_TBL)[BTA_SYS_NUM_COLS];
  128. /* state table */
  129. const tBTA_SYS_ST_TBL bta_sys_st_tbl[] = {
  130. bta_sys_hw_off,
  131. bta_sys_hw_starting,
  132. bta_sys_hw_on,
  133. bta_sys_hw_stopping
  134. };
  135. /*******************************************************************************
  136. **
  137. ** Function bta_sys_init
  138. **
  139. ** Description BTA initialization; called from task initialization.
  140. **
  141. **
  142. ** Returns void
  143. **
  144. *******************************************************************************/
  145. void bta_sys_init(void)
  146. {
  147. memset(&bta_sys_cb, 0, sizeof(tBTA_SYS_CB));
  148. osi_mutex_new(&bta_alarm_lock);
  149. bta_alarm_hash_map = hash_map_new(BTA_ALARM_HASH_MAP_SIZE,
  150. hash_function_pointer, NULL, (data_free_fn)osi_alarm_free, NULL);
  151. appl_trace_level = APPL_INITIAL_TRACE_LEVEL;
  152. /* register BTA SYS message handler */
  153. bta_sys_register( BTA_ID_SYS, &bta_sys_hw_reg);
  154. /* register for BTM notifications */
  155. BTM_RegisterForDeviceStatusNotif ((tBTM_DEV_STATUS_CB *)&bta_sys_hw_btm_cback );
  156. #if( defined BTA_AR_INCLUDED ) && (BTA_AR_INCLUDED == TRUE)
  157. bta_ar_init();
  158. #endif
  159. }
  160. void bta_sys_free(void)
  161. {
  162. hash_map_free(bta_alarm_hash_map);
  163. osi_mutex_free(&bta_alarm_lock);
  164. #if BTA_DYNAMIC_MEMORY
  165. FREE_AND_RESET(bta_sys_cb_ptr);
  166. #endif
  167. }
  168. /*******************************************************************************
  169. **
  170. ** Function bta_sys_sm_execute
  171. **
  172. ** Description State machine event handling function for DM
  173. **
  174. **
  175. ** Returns void
  176. **
  177. *******************************************************************************/
  178. BOOLEAN bta_sys_sm_execute(BT_HDR *p_msg)
  179. {
  180. BOOLEAN freebuf = TRUE;
  181. tBTA_SYS_ST_TBL state_table;
  182. UINT8 action;
  183. int i;
  184. APPL_TRACE_EVENT("bta_sys_sm_execute state:%d, event:0x%x\n", bta_sys_cb.state, p_msg->event);
  185. /* look up the state table for the current state */
  186. state_table = bta_sys_st_tbl[bta_sys_cb.state];
  187. /* update state */
  188. bta_sys_cb.state = state_table[p_msg->event & 0x00ff][BTA_SYS_NEXT_STATE];
  189. /* execute action functions */
  190. for (i = 0; i < BTA_SYS_ACTIONS; i++) {
  191. if ((action = state_table[p_msg->event & 0x00ff][i]) != 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. {
  201. bta_sys_cb.sys_hw_cback[module] = cback;
  202. }
  203. void bta_sys_hw_unregister( tBTA_SYS_HW_MODULE module )
  204. {
  205. bta_sys_cb.sys_hw_cback[module] = NULL;
  206. }
  207. /*******************************************************************************
  208. **
  209. ** Function bta_sys_hw_btm_cback
  210. **
  211. ** Description This function is registered by BTA SYS to BTM in order to get status notifications
  212. **
  213. **
  214. ** Returns
  215. **
  216. *******************************************************************************/
  217. void bta_sys_hw_btm_cback( tBTM_DEV_STATUS status )
  218. {
  219. tBTA_SYS_HW_MSG *sys_event;
  220. APPL_TRACE_DEBUG(" bta_sys_hw_btm_cback was called with parameter: %i" , status );
  221. /* send a message to BTA SYS */
  222. if ((sys_event = (tBTA_SYS_HW_MSG *) osi_malloc(sizeof(tBTA_SYS_HW_MSG))) != NULL) {
  223. if (status == BTM_DEV_STATUS_UP) {
  224. sys_event->hdr.event = BTA_SYS_EVT_STACK_ENABLED_EVT;
  225. } else if (status == BTM_DEV_STATUS_DOWN) {
  226. sys_event->hdr.event = BTA_SYS_ERROR_EVT;
  227. } else {
  228. /* BTM_DEV_STATUS_CMD_TOUT is ignored for now. */
  229. osi_free (sys_event);
  230. sys_event = NULL;
  231. }
  232. if (sys_event) {
  233. bta_sys_sendmsg(sys_event);
  234. }
  235. } else {
  236. APPL_TRACE_DEBUG("ERROR bta_sys_hw_btm_cback couldn't send msg" );
  237. }
  238. }
  239. /*******************************************************************************
  240. **
  241. ** Function bta_sys_hw_error
  242. **
  243. ** Description In case the HW device stops answering... Try to turn it off, then re-enable all
  244. ** previously active SW modules.
  245. **
  246. ** Returns success or failure
  247. **
  248. *******************************************************************************/
  249. void bta_sys_hw_error(tBTA_SYS_HW_MSG *p_sys_hw_msg)
  250. {
  251. UINT8 module_index;
  252. UNUSED(p_sys_hw_msg);
  253. APPL_TRACE_DEBUG("%s\n", __FUNCTION__);
  254. for (module_index = 0; module_index < BTA_SYS_MAX_HW_MODULES; module_index++) {
  255. if ( bta_sys_cb.sys_hw_module_active & ((UINT32)1 << module_index )) {
  256. switch ( module_index) {
  257. case BTA_SYS_HW_BLUETOOTH:
  258. /* Send BTA_SYS_HW_ERROR_EVT to DM */
  259. if (bta_sys_cb.sys_hw_cback[module_index] != NULL) {
  260. bta_sys_cb.sys_hw_cback[module_index] (BTA_SYS_HW_ERROR_EVT);
  261. }
  262. break;
  263. default:
  264. /* not yet supported */
  265. break;
  266. }
  267. }
  268. }
  269. }
  270. /*******************************************************************************
  271. **
  272. ** Function bta_sys_hw_enable
  273. **
  274. ** Description this function is called after API enable and HW has been turned on
  275. **
  276. **
  277. ** Returns success or failure
  278. **
  279. *******************************************************************************/
  280. void bta_sys_hw_api_enable( tBTA_SYS_HW_MSG *p_sys_hw_msg )
  281. {
  282. if ((!bta_sys_cb.sys_hw_module_active) && (bta_sys_cb.state != BTA_SYS_HW_ON)) {
  283. /* register which HW module was turned on */
  284. bta_sys_cb.sys_hw_module_active |= ((UINT32)1 << p_sys_hw_msg->hw_module );
  285. tBTA_SYS_HW_MSG *p_msg;
  286. if ((p_msg = (tBTA_SYS_HW_MSG *) osi_malloc(sizeof(tBTA_SYS_HW_MSG))) != NULL) {
  287. p_msg->hdr.event = BTA_SYS_EVT_ENABLED_EVT;
  288. p_msg->hw_module = p_sys_hw_msg->hw_module;
  289. bta_sys_sendmsg(p_msg);
  290. }
  291. } else {
  292. /* register which HW module was turned on */
  293. bta_sys_cb.sys_hw_module_active |= ((UINT32)1 << p_sys_hw_msg->hw_module );
  294. /* HW already in use, so directly notify the caller */
  295. if (bta_sys_cb.sys_hw_cback[p_sys_hw_msg->hw_module ] != NULL ) {
  296. bta_sys_cb.sys_hw_cback[p_sys_hw_msg->hw_module ]( BTA_SYS_HW_ON_EVT );
  297. }
  298. }
  299. APPL_TRACE_EVENT ("bta_sys_hw_api_enable for %d, active modules 0x%04X\n",
  300. p_sys_hw_msg->hw_module, bta_sys_cb.sys_hw_module_active);
  301. }
  302. /*******************************************************************************
  303. **
  304. ** Function bta_sys_hw_disable
  305. **
  306. ** Description if no other module is using the HW, this function will call ( if defined ) a user-macro to turn off the HW
  307. **
  308. **
  309. ** Returns success or failure
  310. **
  311. *******************************************************************************/
  312. void bta_sys_hw_api_disable(tBTA_SYS_HW_MSG *p_sys_hw_msg)
  313. {
  314. APPL_TRACE_DEBUG("bta_sys_hw_api_disable for %d, active modules: 0x%04X\n",
  315. p_sys_hw_msg->hw_module, bta_sys_cb.sys_hw_module_active );
  316. /* make sure the related SW blocks were stopped */
  317. bta_sys_disable( p_sys_hw_msg->hw_module );
  318. /* register which module we turn off */
  319. bta_sys_cb.sys_hw_module_active &= ~((UINT32)1 << p_sys_hw_msg->hw_module );
  320. /* if there are still some SW modules using the HW, just provide an answer to the calling */
  321. if ( bta_sys_cb.sys_hw_module_active != 0 ) {
  322. /* if there are still some SW modules using the HW, directly notify the caller */
  323. if ( bta_sys_cb.sys_hw_cback[p_sys_hw_msg->hw_module ] != NULL ) {
  324. bta_sys_cb.sys_hw_cback[p_sys_hw_msg->hw_module ]( BTA_SYS_HW_OFF_EVT );
  325. }
  326. } else {
  327. /* manually update the state of our system */
  328. bta_sys_cb.state = BTA_SYS_HW_STOPPING;
  329. tBTA_SYS_HW_MSG *p_msg;
  330. if ((p_msg = (tBTA_SYS_HW_MSG *) osi_malloc(sizeof(tBTA_SYS_HW_MSG))) != NULL) {
  331. p_msg->hdr.event = BTA_SYS_EVT_DISABLED_EVT;
  332. p_msg->hw_module = p_sys_hw_msg->hw_module;
  333. bta_sys_sendmsg(p_msg);
  334. }
  335. }
  336. }
  337. /*******************************************************************************
  338. **
  339. ** Function bta_sys_hw_event_enabled
  340. **
  341. ** Description
  342. **
  343. **
  344. ** Returns success or failure
  345. **
  346. *******************************************************************************/
  347. void bta_sys_hw_evt_enabled(tBTA_SYS_HW_MSG *p_sys_hw_msg)
  348. {
  349. APPL_TRACE_EVENT("bta_sys_hw_evt_enabled for %i\n", p_sys_hw_msg->hw_module);
  350. BTM_DeviceReset( NULL );
  351. }
  352. /*******************************************************************************
  353. **
  354. ** Function bta_sys_hw_event_disabled
  355. **
  356. ** Description
  357. **
  358. **
  359. ** Returns success or failure
  360. **
  361. *******************************************************************************/
  362. void bta_sys_hw_evt_disabled(tBTA_SYS_HW_MSG *p_sys_hw_msg)
  363. {
  364. UINT8 hw_module_index;
  365. APPL_TRACE_DEBUG("bta_sys_hw_evt_disabled - module 0x%X\n", p_sys_hw_msg->hw_module);
  366. for (hw_module_index = 0; hw_module_index < BTA_SYS_MAX_HW_MODULES; hw_module_index++) {
  367. if (bta_sys_cb.sys_hw_cback[hw_module_index] != NULL) {
  368. bta_sys_cb.sys_hw_cback[hw_module_index] (BTA_SYS_HW_OFF_EVT);
  369. }
  370. }
  371. }
  372. /*******************************************************************************
  373. **
  374. ** Function bta_sys_hw_event_stack_enabled
  375. **
  376. ** Description we receive this event once the SW side is ready ( stack, FW download,... ),
  377. ** i.e. we can really start using the device. So notify the app.
  378. **
  379. ** Returns success or failure
  380. **
  381. *******************************************************************************/
  382. void bta_sys_hw_evt_stack_enabled(tBTA_SYS_HW_MSG *p_sys_hw_msg)
  383. {
  384. UINT8 hw_module_index;
  385. UNUSED(p_sys_hw_msg);
  386. APPL_TRACE_DEBUG(" bta_sys_hw_evt_stack_enabled!notify the callers\n");
  387. for (hw_module_index = 0; hw_module_index < BTA_SYS_MAX_HW_MODULES; hw_module_index++ ) {
  388. if (bta_sys_cb.sys_hw_cback[hw_module_index] != NULL) {
  389. bta_sys_cb.sys_hw_cback[hw_module_index] (BTA_SYS_HW_ON_EVT);
  390. }
  391. }
  392. }
  393. /*******************************************************************************
  394. **
  395. ** Function bta_sys_event
  396. **
  397. ** Description BTA event handler; called from task event handler.
  398. **
  399. **
  400. ** Returns void
  401. **
  402. *******************************************************************************/
  403. void bta_sys_event(void * param)
  404. {
  405. BT_HDR *p_msg = (BT_HDR *)param;
  406. UINT8 id;
  407. BOOLEAN freebuf = TRUE;
  408. APPL_TRACE_EVENT("BTA got event 0x%x\n", p_msg->event);
  409. /* get subsystem id from event */
  410. id = (UINT8) (p_msg->event >> 8);
  411. /* verify id and call subsystem event handler */
  412. if ((id < BTA_ID_MAX) && (bta_sys_cb.reg[id] != NULL)) {
  413. freebuf = (*bta_sys_cb.reg[id]->evt_hdlr)(p_msg);
  414. } else {
  415. APPL_TRACE_WARNING("BTA got unregistered event id %d\n", id);
  416. }
  417. if (freebuf) {
  418. osi_free(p_msg);
  419. }
  420. }
  421. /*******************************************************************************
  422. **
  423. ** Function bta_sys_register
  424. **
  425. ** Description Called by other BTA subsystems to register their event
  426. ** handler.
  427. **
  428. **
  429. ** Returns void
  430. **
  431. *******************************************************************************/
  432. void bta_sys_register(UINT8 id, const tBTA_SYS_REG *p_reg)
  433. {
  434. bta_sys_cb.reg[id] = (tBTA_SYS_REG *) p_reg;
  435. bta_sys_cb.is_reg[id] = TRUE;
  436. }
  437. /*******************************************************************************
  438. **
  439. ** Function bta_sys_deregister
  440. **
  441. ** Description Called by other BTA subsystems to de-register
  442. ** handler.
  443. **
  444. **
  445. ** Returns void
  446. **
  447. *******************************************************************************/
  448. void bta_sys_deregister(UINT8 id)
  449. {
  450. bta_sys_cb.is_reg[id] = FALSE;
  451. }
  452. /*******************************************************************************
  453. **
  454. ** Function bta_sys_is_register
  455. **
  456. ** Description Called by other BTA subsystems to get registeration
  457. ** status.
  458. **
  459. **
  460. ** Returns void
  461. **
  462. *******************************************************************************/
  463. BOOLEAN bta_sys_is_register(UINT8 id)
  464. {
  465. return bta_sys_cb.is_reg[id];
  466. }
  467. /*******************************************************************************
  468. **
  469. ** Function bta_sys_sendmsg
  470. **
  471. ** Description Send a message to BTA. This function is designed to
  472. ** optimize sending of messages to BTA. It is called by BTA
  473. ** API functions and call-in functions.
  474. **
  475. **
  476. ** Returns void
  477. **
  478. *******************************************************************************/
  479. void bta_sys_sendmsg(void *p_msg)
  480. {
  481. // There is a race condition that occurs if the stack is shut down while
  482. // there is a procedure in progress that can schedule a task via this
  483. // message queue. This causes |btu_bta_msg_queue| to get cleaned up before
  484. // it gets used here; hence we check for NULL before using it.
  485. if (btu_task_post(SIG_BTU_BTA_MSG, p_msg, OSI_THREAD_MAX_TIMEOUT) == false) {
  486. osi_free(p_msg);
  487. }
  488. }
  489. /*******************************************************************************
  490. **
  491. ** Function bta_sys_start_timer
  492. **
  493. ** Description Start a protocol timer for the specified amount
  494. ** of time in milliseconds.
  495. **
  496. ** Returns void
  497. **
  498. *******************************************************************************/
  499. void bta_alarm_cb(void *data)
  500. {
  501. assert(data != NULL);
  502. TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;
  503. btu_task_post(SIG_BTU_BTA_ALARM, p_tle, OSI_THREAD_MAX_TIMEOUT);
  504. }
  505. void bta_sys_start_timer(TIMER_LIST_ENT *p_tle, UINT16 type, INT32 timeout_ms)
  506. {
  507. assert(p_tle != NULL);
  508. // Get the alarm for this p_tle.
  509. osi_mutex_lock(&bta_alarm_lock, OSI_MUTEX_MAX_TIMEOUT);
  510. if (!hash_map_has_key(bta_alarm_hash_map, p_tle)) {
  511. hash_map_set(bta_alarm_hash_map, p_tle, osi_alarm_new("bta_sys", bta_alarm_cb, p_tle, 0));
  512. }
  513. osi_mutex_unlock(&bta_alarm_lock);
  514. osi_alarm_t *alarm = hash_map_get(bta_alarm_hash_map, p_tle);
  515. if (alarm == NULL) {
  516. APPL_TRACE_ERROR("%s unable to create alarm.", __func__);
  517. return;
  518. }
  519. p_tle->event = type;
  520. p_tle->ticks = timeout_ms;
  521. //osi_alarm_set(alarm, (period_ms_t)timeout_ms, bta_alarm_cb, p_tle);
  522. osi_alarm_set(alarm, (period_ms_t)timeout_ms);
  523. }
  524. bool hash_iter_ro_cb(hash_map_entry_t *hash_map_entry, void *context)
  525. {
  526. osi_alarm_t *alarm = (osi_alarm_t *)hash_map_entry->data;
  527. period_ms_t *p_remaining_ms = (period_ms_t *)context;
  528. *p_remaining_ms += osi_alarm_get_remaining_ms(alarm);
  529. return true;
  530. }
  531. UINT32 bta_sys_get_remaining_ticks(TIMER_LIST_ENT *p_target_tle)
  532. {
  533. period_ms_t remaining_ms = 0;
  534. osi_mutex_lock(&bta_alarm_lock, OSI_MUTEX_MAX_TIMEOUT);
  535. // Get the alarm for this p_tle
  536. hash_map_foreach(bta_alarm_hash_map, hash_iter_ro_cb, &remaining_ms);
  537. osi_mutex_unlock(&bta_alarm_lock);
  538. return remaining_ms;
  539. }
  540. /*******************************************************************************
  541. **
  542. ** Function bta_sys_stop_timer
  543. **
  544. ** Description Stop a BTA timer.
  545. **
  546. ** Returns void
  547. **
  548. *******************************************************************************/
  549. void bta_sys_stop_timer(TIMER_LIST_ENT *p_tle)
  550. {
  551. assert(p_tle != NULL);
  552. osi_alarm_t *alarm = hash_map_get(bta_alarm_hash_map, p_tle);
  553. if (alarm == NULL) {
  554. APPL_TRACE_DEBUG("%s expected alarm was not in bta alarm hash map.", __func__);
  555. return;
  556. }
  557. osi_alarm_cancel(alarm);
  558. }
  559. /*******************************************************************************
  560. **
  561. ** Function bta_sys_free_timer
  562. **
  563. ** Description Stop and free a BTA timer.
  564. **
  565. ** Returns void
  566. **
  567. *******************************************************************************/
  568. void bta_sys_free_timer(TIMER_LIST_ENT *p_tle)
  569. {
  570. assert(p_tle != NULL);
  571. osi_alarm_t *alarm = hash_map_get(bta_alarm_hash_map, p_tle);
  572. if (alarm == NULL) {
  573. APPL_TRACE_DEBUG("%s expected alarm was not in bta alarm hash map.", __func__);
  574. return;
  575. }
  576. osi_alarm_cancel(alarm);
  577. hash_map_erase(bta_alarm_hash_map, p_tle);
  578. }
  579. /*******************************************************************************
  580. **
  581. ** Function bta_sys_disable
  582. **
  583. ** Description For each registered subsystem execute its disable function.
  584. **
  585. ** Returns void
  586. **
  587. *******************************************************************************/
  588. void bta_sys_disable(tBTA_SYS_HW_MODULE module)
  589. {
  590. int bta_id = 0;
  591. int bta_id_max = 0;
  592. APPL_TRACE_DEBUG("bta_sys_disable: module %i", module);
  593. switch ( module ) {
  594. case BTA_SYS_HW_BLUETOOTH:
  595. bta_id = BTA_ID_DM;
  596. bta_id_max = BTA_ID_BLUETOOTH_MAX;
  597. break;
  598. default:
  599. APPL_TRACE_WARNING("bta_sys_disable: unkown module");
  600. return;
  601. }
  602. for ( ; bta_id <= bta_id_max; bta_id++) {
  603. if (bta_sys_cb.reg[bta_id] != NULL) {
  604. if (bta_sys_cb.is_reg[bta_id] == TRUE && bta_sys_cb.reg[bta_id]->disable != NULL) {
  605. (*bta_sys_cb.reg[bta_id]->disable)();
  606. }
  607. }
  608. }
  609. }
  610. /*******************************************************************************
  611. **
  612. ** Function bta_sys_set_trace_level
  613. **
  614. ** Description Set trace level for BTA
  615. **
  616. ** Returns void
  617. **
  618. *******************************************************************************/
  619. void bta_sys_set_trace_level(UINT8 level)
  620. {
  621. appl_trace_level = level;
  622. }
  623. /*******************************************************************************
  624. **
  625. ** Function bta_sys_get_sys_features
  626. **
  627. ** Description Returns sys_features to other BTA modules.
  628. **
  629. ** Returns sys_features
  630. **
  631. *******************************************************************************/
  632. UINT16 bta_sys_get_sys_features (void)
  633. {
  634. return bta_sys_cb.sys_features;
  635. }