btu_task.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 1999-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. #include <string.h>
  19. #include "osi/alarm.h"
  20. #include "osi/thread.h"
  21. #include "common/bt_target.h"
  22. #include "common/bt_trace.h"
  23. #include "stack/bt_types.h"
  24. #include "osi/allocator.h"
  25. #include "osi/mutex.h"
  26. #include "stack/btm_api.h"
  27. #include "btm_int.h"
  28. #include "stack/btu.h"
  29. #include "osi/hash_map.h"
  30. #include "stack/hcimsgs.h"
  31. #include "l2c_int.h"
  32. #include "osi/osi.h"
  33. #if (defined(SDP_INCLUDED) && SDP_INCLUDED == TRUE)
  34. #include "sdpint.h"
  35. #endif
  36. #if (defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE)
  37. #include "stack/port_api.h"
  38. #include "stack/port_ext.h"
  39. #endif
  40. #if (defined(GAP_INCLUDED) && GAP_INCLUDED == TRUE)
  41. #include "gap_int.h"
  42. #endif
  43. #if (defined(BNEP_INCLUDED) && BNEP_INCLUDED == TRUE)
  44. #include "bnep_int.h"
  45. #endif
  46. #if (defined(PAN_INCLUDED) && PAN_INCLUDED == TRUE)
  47. #include "pan_int.h"
  48. #endif
  49. #if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE )
  50. #include "hidh_int.h"
  51. #endif
  52. #if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE)
  53. #include "avdt_int.h"
  54. #else
  55. extern void avdt_rcv_sync_info (BT_HDR *p_buf);
  56. #endif
  57. #if (defined(MCA_INCLUDED) && MCA_INCLUDED == TRUE)
  58. #include "mca_api.h"
  59. #include "mca_defs.h"
  60. #include "mca_int.h"
  61. #endif
  62. #if (defined(BTA_INCLUDED) && BTA_INCLUDED == TRUE)
  63. #include "bta/bta_sys.h"
  64. #endif
  65. #if (BLE_INCLUDED == TRUE)
  66. #include "gatt_int.h"
  67. #if (SMP_INCLUDED == TRUE)
  68. #include "smp_int.h"
  69. #endif
  70. #include "btm_ble_int.h"
  71. #endif
  72. typedef struct {
  73. uint32_t sig;
  74. void *param;
  75. } btu_thread_evt_t;
  76. //#if (defined(BT_APP_DEMO) && BT_APP_DEMO == TRUE)
  77. //#include "bt_app_common.h"
  78. //#endif
  79. extern bt_status_t BTE_InitStack(void);
  80. extern void BTE_DeinitStack(void);
  81. /* Define BTU storage area
  82. */
  83. #if BTU_DYNAMIC_MEMORY == FALSE
  84. tBTU_CB btu_cb;
  85. #else
  86. tBTU_CB *btu_cb_ptr;
  87. #endif
  88. extern hash_map_t *btu_general_alarm_hash_map;
  89. extern osi_mutex_t btu_general_alarm_lock;
  90. // Oneshot timer queue.
  91. extern hash_map_t *btu_oneshot_alarm_hash_map;
  92. extern osi_mutex_t btu_oneshot_alarm_lock;
  93. // l2cap timer queue.
  94. extern hash_map_t *btu_l2cap_alarm_hash_map;
  95. extern osi_mutex_t btu_l2cap_alarm_lock;
  96. extern void *btu_thread;
  97. extern bluedroid_init_done_cb_t bluedroid_init_done_cb;
  98. /* Define a function prototype to allow a generic timeout handler */
  99. typedef void (tUSER_TIMEOUT_FUNC) (TIMER_LIST_ENT *p_tle);
  100. static void btu_l2cap_alarm_process(void *param);
  101. static void btu_general_alarm_process(void *param);
  102. static void btu_hci_msg_process(void *param);
  103. #if (defined(BTA_INCLUDED) && BTA_INCLUDED == TRUE)
  104. static void btu_bta_alarm_process(void *param);
  105. #endif
  106. static void btu_hci_msg_process(void *param)
  107. {
  108. /* Determine the input message type. */
  109. BT_HDR *p_msg = (BT_HDR *)param;
  110. switch (p_msg->event & BT_EVT_MASK) {
  111. case BTU_POST_TO_TASK_NO_GOOD_HORRIBLE_HACK: // TODO(zachoverflow): remove this
  112. {
  113. post_to_task_hack_t *ph = (post_to_task_hack_t *) &p_msg->data[0];
  114. ph->callback(p_msg);
  115. break;
  116. }
  117. case BT_EVT_TO_BTU_HCI_ACL:
  118. /* All Acl Data goes to L2CAP */
  119. l2c_rcv_acl_data (p_msg);
  120. break;
  121. case BT_EVT_TO_BTU_L2C_SEG_XMIT:
  122. /* L2CAP segment transmit complete */
  123. l2c_link_segments_xmitted (p_msg);
  124. break;
  125. case BT_EVT_TO_BTU_HCI_SCO:
  126. #if BTM_SCO_INCLUDED == TRUE
  127. btm_route_sco_data (p_msg);
  128. break;
  129. #endif
  130. case BT_EVT_TO_BTU_HCI_EVT:
  131. btu_hcif_process_event ((UINT8)(p_msg->event & BT_SUB_EVT_MASK), p_msg);
  132. osi_free(p_msg);
  133. #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
  134. /* If host receives events which it doesn't response to, */
  135. /* host should start idle timer to enter sleep mode. */
  136. btu_check_bt_sleep ();
  137. #endif
  138. break;
  139. case BT_EVT_TO_BTU_HCI_CMD:
  140. btu_hcif_send_cmd ((UINT8)(p_msg->event & BT_SUB_EVT_MASK), p_msg);
  141. break;
  142. default:;
  143. int i = 0;
  144. uint16_t mask = (UINT16) (p_msg->event & BT_EVT_MASK);
  145. BOOLEAN handled = FALSE;
  146. for (; !handled && i < BTU_MAX_REG_EVENT; i++) {
  147. if (btu_cb.event_reg[i].event_cb == NULL) {
  148. continue;
  149. }
  150. if (mask == btu_cb.event_reg[i].event_range) {
  151. if (btu_cb.event_reg[i].event_cb) {
  152. btu_cb.event_reg[i].event_cb(p_msg);
  153. handled = TRUE;
  154. }
  155. }
  156. }
  157. if (handled == FALSE) {
  158. osi_free (p_msg);
  159. }
  160. break;
  161. }
  162. }
  163. #if (defined(BTA_INCLUDED) && BTA_INCLUDED == TRUE)
  164. static void btu_bta_alarm_process(void *param)
  165. {
  166. TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)param;
  167. // call timer callback
  168. if (p_tle->p_cback) {
  169. (*p_tle->p_cback)(p_tle);
  170. } else if (p_tle->event) {
  171. BT_HDR *p_msg;
  172. if ((p_msg = (BT_HDR *) osi_malloc(sizeof(BT_HDR))) != NULL) {
  173. p_msg->event = p_tle->event;
  174. p_msg->layer_specific = 0;
  175. //osi_free(p_msg);
  176. bta_sys_sendmsg(p_msg);
  177. }
  178. }
  179. }
  180. #endif
  181. bool btu_task_post(uint32_t sig, void *param, uint32_t timeout)
  182. {
  183. bool status = false;
  184. switch (sig) {
  185. case SIG_BTU_START_UP:
  186. status = osi_thread_post(btu_thread, btu_task_start_up, param, 0, timeout);
  187. break;
  188. case SIG_BTU_HCI_MSG:
  189. status = osi_thread_post(btu_thread, btu_hci_msg_process, param, 0, timeout);
  190. break;
  191. #if (defined(BTA_INCLUDED) && BTA_INCLUDED == TRUE)
  192. case SIG_BTU_BTA_MSG:
  193. status = osi_thread_post(btu_thread, bta_sys_event, param, 0, timeout);
  194. break;
  195. case SIG_BTU_BTA_ALARM:
  196. status = osi_thread_post(btu_thread, btu_bta_alarm_process, param, 0, timeout);
  197. break;
  198. #endif
  199. case SIG_BTU_GENERAL_ALARM:
  200. case SIG_BTU_ONESHOT_ALARM:
  201. status = osi_thread_post(btu_thread, btu_general_alarm_process, param, 0, timeout);
  202. break;
  203. case SIG_BTU_L2CAP_ALARM:
  204. status = osi_thread_post(btu_thread, btu_l2cap_alarm_process, param, 0, timeout);
  205. break;
  206. default:
  207. break;
  208. }
  209. return status;
  210. }
  211. void btu_task_start_up(void *param)
  212. {
  213. UNUSED(param);
  214. /* Initialize the mandatory core stack control blocks
  215. (BTU, BTM, L2CAP, and SDP)
  216. */
  217. btu_init_core();
  218. /* Initialize any optional stack components */
  219. BTE_InitStack();
  220. #if (defined(BTA_INCLUDED) && BTA_INCLUDED == TRUE)
  221. bta_sys_init();
  222. #endif
  223. // Inform the bt jni thread initialization is ok.
  224. // btif_transfer_context(btif_init_ok, 0, NULL, 0, NULL);
  225. #if(defined(BT_APP_DEMO) && BT_APP_DEMO == TRUE)
  226. if (bluedroid_init_done_cb) {
  227. bluedroid_init_done_cb();
  228. }
  229. #endif
  230. }
  231. void btu_task_shut_down(void)
  232. {
  233. #if (defined(BTA_INCLUDED) && BTA_INCLUDED == TRUE)
  234. bta_sys_free();
  235. #endif
  236. BTE_DeinitStack();
  237. btu_free_core();
  238. }
  239. /*******************************************************************************
  240. **
  241. ** Function btu_start_timer
  242. **
  243. ** Description Start a timer for the specified amount of time.
  244. ** NOTE: The timeout resolution is in SECONDS! (Even
  245. ** though the timer structure field is ticks)
  246. **
  247. ** Returns void
  248. **
  249. *******************************************************************************/
  250. static void btu_general_alarm_process(void *param)
  251. {
  252. TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)param;
  253. assert(p_tle != NULL);
  254. switch (p_tle->event) {
  255. case BTU_TTYPE_BTM_DEV_CTL:
  256. btm_dev_timeout(p_tle);
  257. break;
  258. case BTU_TTYPE_L2CAP_LINK:
  259. case BTU_TTYPE_L2CAP_CHNL:
  260. case BTU_TTYPE_L2CAP_HOLD:
  261. case BTU_TTYPE_L2CAP_INFO:
  262. case BTU_TTYPE_L2CAP_FCR_ACK:
  263. case BTU_TTYPE_L2CAP_UPDA_CONN_PARAMS:
  264. l2c_process_timeout (p_tle);
  265. break;
  266. #if (defined(SDP_INCLUDED) && SDP_INCLUDED == TRUE)
  267. case BTU_TTYPE_SDP:
  268. sdp_conn_timeout ((tCONN_CB *)p_tle->param);
  269. break;
  270. #endif
  271. case BTU_TTYPE_BTM_RMT_NAME:
  272. btm_inq_rmt_name_failed();
  273. break;
  274. #if (defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE)
  275. case BTU_TTYPE_RFCOMM_MFC:
  276. case BTU_TTYPE_RFCOMM_PORT:
  277. rfcomm_process_timeout (p_tle);
  278. break;
  279. #endif
  280. #if ((defined(BNEP_INCLUDED) && BNEP_INCLUDED == TRUE))
  281. case BTU_TTYPE_BNEP:
  282. bnep_process_timeout(p_tle);
  283. break;
  284. #endif
  285. #if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE)
  286. case BTU_TTYPE_AVDT_CCB_RET:
  287. case BTU_TTYPE_AVDT_CCB_RSP:
  288. case BTU_TTYPE_AVDT_CCB_IDLE:
  289. case BTU_TTYPE_AVDT_SCB_TC:
  290. avdt_process_timeout(p_tle);
  291. break;
  292. #endif
  293. #if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE)
  294. case BTU_TTYPE_HID_HOST_REPAGE_TO :
  295. hidh_proc_repage_timeout(p_tle);
  296. break;
  297. #endif
  298. #if (defined(BLE_INCLUDED) && BLE_INCLUDED == TRUE)
  299. case BTU_TTYPE_BLE_INQUIRY:
  300. case BTU_TTYPE_BLE_GAP_LIM_DISC:
  301. case BTU_TTYPE_BLE_RANDOM_ADDR:
  302. case BTU_TTYPE_BLE_GAP_FAST_ADV:
  303. case BTU_TTYPE_BLE_SCAN:
  304. case BTU_TTYPE_BLE_OBSERVE:
  305. btm_ble_timeout(p_tle);
  306. break;
  307. case BTU_TTYPE_ATT_WAIT_FOR_RSP:
  308. gatt_rsp_timeout(p_tle);
  309. break;
  310. case BTU_TTYPE_ATT_WAIT_FOR_IND_ACK:
  311. gatt_ind_ack_timeout(p_tle);
  312. break;
  313. #if (defined(SMP_INCLUDED) && SMP_INCLUDED == TRUE)
  314. case BTU_TTYPE_SMP_PAIRING_CMD:
  315. smp_rsp_timeout(p_tle);
  316. break;
  317. #endif
  318. #endif
  319. #if (MCA_INCLUDED == TRUE)
  320. case BTU_TTYPE_MCA_CCB_RSP:
  321. mca_process_timeout(p_tle);
  322. break;
  323. #endif
  324. case BTU_TTYPE_USER_FUNC: {
  325. tUSER_TIMEOUT_FUNC *p_uf = (tUSER_TIMEOUT_FUNC *)p_tle->param;
  326. (*p_uf)(p_tle);
  327. }
  328. break;
  329. case BTU_TTYPE_BTM_QOS:
  330. btm_qos_setup_timeout(p_tle);
  331. break;
  332. default:
  333. for (int i = 0; i < BTU_MAX_REG_TIMER; i++) {
  334. if (btu_cb.timer_reg[i].timer_cb == NULL) {
  335. continue;
  336. }
  337. if (btu_cb.timer_reg[i].p_tle == p_tle) {
  338. btu_cb.timer_reg[i].timer_cb(p_tle);
  339. break;
  340. }
  341. }
  342. break;
  343. }
  344. }
  345. void btu_general_alarm_cb(void *data)
  346. {
  347. assert(data != NULL);
  348. TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;
  349. btu_task_post(SIG_BTU_GENERAL_ALARM, p_tle, OSI_THREAD_MAX_TIMEOUT);
  350. }
  351. void btu_start_timer(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_sec)
  352. {
  353. osi_alarm_t *alarm = NULL;
  354. assert(p_tle != NULL);
  355. // Get the alarm for the timer list entry.
  356. osi_mutex_lock(&btu_general_alarm_lock, OSI_MUTEX_MAX_TIMEOUT);
  357. if (!hash_map_has_key(btu_general_alarm_hash_map, p_tle)) {
  358. alarm = osi_alarm_new("btu_gen", btu_general_alarm_cb, (void *)p_tle, 0);
  359. hash_map_set(btu_general_alarm_hash_map, p_tle, alarm);
  360. }
  361. osi_mutex_unlock(&btu_general_alarm_lock);
  362. alarm = hash_map_get(btu_general_alarm_hash_map, p_tle);
  363. if (alarm == NULL) {
  364. HCI_TRACE_ERROR("%s Unable to create alarm", __func__);
  365. return;
  366. }
  367. osi_alarm_cancel(alarm);
  368. p_tle->event = type;
  369. // NOTE: This value is in seconds but stored in a ticks field.
  370. p_tle->ticks = timeout_sec;
  371. p_tle->in_use = TRUE;
  372. osi_alarm_set(alarm, (period_ms_t)(timeout_sec * 1000));
  373. }
  374. /*******************************************************************************
  375. **
  376. ** Function btu_stop_timer
  377. **
  378. ** Description Stop a timer.
  379. **
  380. ** Returns void
  381. **
  382. *******************************************************************************/
  383. void btu_stop_timer(TIMER_LIST_ENT *p_tle)
  384. {
  385. assert(p_tle != NULL);
  386. if (p_tle->in_use == FALSE) {
  387. return;
  388. }
  389. p_tle->in_use = FALSE;
  390. // Get the alarm for the timer list entry.
  391. osi_alarm_t *alarm = hash_map_get(btu_general_alarm_hash_map, p_tle);
  392. if (alarm == NULL) {
  393. HCI_TRACE_WARNING("%s Unable to find expected alarm in hashmap", __func__);
  394. return;
  395. }
  396. osi_alarm_cancel(alarm);
  397. }
  398. /*******************************************************************************
  399. **
  400. ** Function btu_free_timer
  401. **
  402. ** Description Stop and free a timer.
  403. **
  404. ** Returns void
  405. **
  406. *******************************************************************************/
  407. void btu_free_timer(TIMER_LIST_ENT *p_tle)
  408. {
  409. assert(p_tle != NULL);
  410. p_tle->in_use = FALSE;
  411. // Get the alarm for the timer list entry.
  412. osi_alarm_t *alarm = hash_map_get(btu_general_alarm_hash_map, p_tle);
  413. if (alarm == NULL) {
  414. HCI_TRACE_DEBUG("%s Unable to find expected alarm in hashmap", __func__);
  415. return;
  416. }
  417. osi_alarm_cancel(alarm);
  418. hash_map_erase(btu_general_alarm_hash_map, p_tle);
  419. }
  420. #if defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0)
  421. /*******************************************************************************
  422. **
  423. ** Function btu_start_quick_timer
  424. **
  425. ** Description Start a timer for the specified amount of time in ticks.
  426. **
  427. ** Returns void
  428. **
  429. *******************************************************************************/
  430. static void btu_l2cap_alarm_process(void *param)
  431. {
  432. TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)param;
  433. assert(p_tle != NULL);
  434. switch (p_tle->event) {
  435. case BTU_TTYPE_L2CAP_CHNL: /* monitor or retransmission timer */
  436. case BTU_TTYPE_L2CAP_FCR_ACK: /* ack timer */
  437. l2c_process_timeout (p_tle);
  438. break;
  439. default:
  440. break;
  441. }
  442. }
  443. static void btu_l2cap_alarm_cb(void *data)
  444. {
  445. assert(data != NULL);
  446. TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;
  447. btu_task_post(SIG_BTU_L2CAP_ALARM, p_tle, OSI_THREAD_MAX_TIMEOUT);
  448. }
  449. void btu_start_quick_timer(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_ticks)
  450. {
  451. osi_alarm_t *alarm = NULL;
  452. assert(p_tle != NULL);
  453. // Get the alarm for the timer list entry.
  454. osi_mutex_lock(&btu_l2cap_alarm_lock, OSI_MUTEX_MAX_TIMEOUT);
  455. if (!hash_map_has_key(btu_l2cap_alarm_hash_map, p_tle)) {
  456. alarm = osi_alarm_new("btu_l2cap", btu_l2cap_alarm_cb, (void *)p_tle, 0);
  457. hash_map_set(btu_l2cap_alarm_hash_map, p_tle, (void *)alarm);
  458. }
  459. osi_mutex_unlock(&btu_l2cap_alarm_lock);
  460. alarm = hash_map_get(btu_l2cap_alarm_hash_map, p_tle);
  461. if (alarm == NULL) {
  462. HCI_TRACE_ERROR("%s Unable to create alarm", __func__);
  463. return;
  464. }
  465. osi_alarm_cancel(alarm);
  466. p_tle->event = type;
  467. p_tle->ticks = timeout_ticks;
  468. p_tle->in_use = TRUE;
  469. // The quick timer ticks are 100ms long.
  470. osi_alarm_set(alarm, (period_ms_t)(timeout_ticks * 100));
  471. }
  472. /*******************************************************************************
  473. **
  474. ** Function btu_stop_quick_timer
  475. **
  476. ** Description Stop a timer.
  477. **
  478. ** Returns void
  479. **
  480. *******************************************************************************/
  481. void btu_stop_quick_timer(TIMER_LIST_ENT *p_tle)
  482. {
  483. assert(p_tle != NULL);
  484. if (p_tle->in_use == FALSE) {
  485. return;
  486. }
  487. p_tle->in_use = FALSE;
  488. // Get the alarm for the timer list entry.
  489. osi_alarm_t *alarm = hash_map_get(btu_l2cap_alarm_hash_map, p_tle);
  490. if (alarm == NULL) {
  491. HCI_TRACE_WARNING("%s Unable to find expected alarm in hashmap", __func__);
  492. return;
  493. }
  494. osi_alarm_cancel(alarm);
  495. }
  496. void btu_free_quick_timer(TIMER_LIST_ENT *p_tle)
  497. {
  498. assert(p_tle != NULL);
  499. p_tle->in_use = FALSE;
  500. // Get the alarm for the timer list entry.
  501. osi_alarm_t *alarm = hash_map_get(btu_l2cap_alarm_hash_map, p_tle);
  502. if (alarm == NULL) {
  503. HCI_TRACE_DEBUG("%s Unable to find expected alarm in hashmap", __func__);
  504. return;
  505. }
  506. osi_alarm_cancel(alarm);
  507. hash_map_erase(btu_l2cap_alarm_hash_map, p_tle);
  508. }
  509. #endif /* defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0) */
  510. void btu_oneshot_alarm_cb(void *data)
  511. {
  512. assert(data != NULL);
  513. TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;
  514. btu_stop_timer_oneshot(p_tle);
  515. btu_task_post(SIG_BTU_ONESHOT_ALARM, p_tle, OSI_THREAD_MAX_TIMEOUT);
  516. }
  517. /*
  518. * Starts a oneshot timer with a timeout in seconds.
  519. */
  520. void btu_start_timer_oneshot(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_sec)
  521. {
  522. osi_alarm_t *alarm = NULL;
  523. assert(p_tle != NULL);
  524. // Get the alarm for the timer list entry.
  525. osi_mutex_lock(&btu_oneshot_alarm_lock, OSI_MUTEX_MAX_TIMEOUT);
  526. if (!hash_map_has_key(btu_oneshot_alarm_hash_map, p_tle)) {
  527. alarm = osi_alarm_new("btu_oneshot", btu_oneshot_alarm_cb, (void *)p_tle, 0);
  528. hash_map_set(btu_oneshot_alarm_hash_map, p_tle, alarm);
  529. }
  530. osi_mutex_unlock(&btu_oneshot_alarm_lock);
  531. alarm = hash_map_get(btu_oneshot_alarm_hash_map, p_tle);
  532. if (alarm == NULL) {
  533. HCI_TRACE_ERROR("%s Unable to create alarm", __func__);
  534. return;
  535. }
  536. osi_alarm_cancel(alarm);
  537. p_tle->event = type;
  538. p_tle->in_use = TRUE;
  539. // NOTE: This value is in seconds but stored in a ticks field.
  540. p_tle->ticks = timeout_sec;
  541. osi_alarm_set(alarm, (period_ms_t)(timeout_sec * 1000));
  542. }
  543. void btu_stop_timer_oneshot(TIMER_LIST_ENT *p_tle)
  544. {
  545. assert(p_tle != NULL);
  546. if (p_tle->in_use == FALSE) {
  547. return;
  548. }
  549. p_tle->in_use = FALSE;
  550. // Get the alarm for the timer list entry.
  551. osi_alarm_t *alarm = hash_map_get(btu_oneshot_alarm_hash_map, p_tle);
  552. if (alarm == NULL) {
  553. HCI_TRACE_WARNING("%s Unable to find expected alarm in hashmap", __func__);
  554. return;
  555. }
  556. osi_alarm_cancel(alarm);
  557. }
  558. #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
  559. /*******************************************************************************
  560. **
  561. ** Function btu_check_bt_sleep
  562. **
  563. ** Description This function is called to check if controller can go to sleep.
  564. **
  565. ** Returns void
  566. **
  567. *******************************************************************************/
  568. void btu_check_bt_sleep (void)
  569. {
  570. // TODO(zachoverflow) take pending commands into account?
  571. if (l2cb.controller_xmit_window == l2cb.num_lm_acl_bufs) {
  572. bte_main_lpm_allow_bt_device_sleep();
  573. }
  574. }
  575. #endif