btu_task.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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 "hid_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. case SIG_BTU_HCI_ADV_RPT_MSG:
  192. #if BLE_INCLUDED == TRUE
  193. if (param != NULL) {
  194. btm_ble_adv_pkt_post(param);
  195. }
  196. btm_ble_adv_pkt_ready();
  197. status = true;
  198. #else
  199. osi_free(param);
  200. status = false;
  201. #endif
  202. break;
  203. #if (defined(BTA_INCLUDED) && BTA_INCLUDED == TRUE)
  204. case SIG_BTU_BTA_MSG:
  205. status = osi_thread_post(btu_thread, bta_sys_event, param, 0, timeout);
  206. break;
  207. case SIG_BTU_BTA_ALARM:
  208. status = osi_thread_post(btu_thread, btu_bta_alarm_process, param, 0, timeout);
  209. break;
  210. #endif
  211. case SIG_BTU_GENERAL_ALARM:
  212. case SIG_BTU_ONESHOT_ALARM:
  213. status = osi_thread_post(btu_thread, btu_general_alarm_process, param, 0, timeout);
  214. break;
  215. case SIG_BTU_L2CAP_ALARM:
  216. status = osi_thread_post(btu_thread, btu_l2cap_alarm_process, param, 0, timeout);
  217. break;
  218. default:
  219. break;
  220. }
  221. return status;
  222. }
  223. void btu_task_start_up(void *param)
  224. {
  225. UNUSED(param);
  226. /* Initialize the mandatory core stack control blocks
  227. (BTU, BTM, L2CAP, and SDP)
  228. */
  229. btu_init_core();
  230. /* Initialize any optional stack components */
  231. BTE_InitStack();
  232. #if (defined(BTA_INCLUDED) && BTA_INCLUDED == TRUE)
  233. bta_sys_init();
  234. #endif
  235. // Inform the bt jni thread initialization is ok.
  236. // btif_transfer_context(btif_init_ok, 0, NULL, 0, NULL);
  237. #if(defined(BT_APP_DEMO) && BT_APP_DEMO == TRUE)
  238. if (bluedroid_init_done_cb) {
  239. bluedroid_init_done_cb();
  240. }
  241. #endif
  242. }
  243. void btu_task_shut_down(void)
  244. {
  245. #if (defined(BTA_INCLUDED) && BTA_INCLUDED == TRUE)
  246. bta_sys_free();
  247. #endif
  248. BTE_DeinitStack();
  249. btu_free_core();
  250. }
  251. /*******************************************************************************
  252. **
  253. ** Function btu_start_timer
  254. **
  255. ** Description Start a timer for the specified amount of time.
  256. ** NOTE: The timeout resolution is in SECONDS! (Even
  257. ** though the timer structure field is ticks)
  258. **
  259. ** Returns void
  260. **
  261. *******************************************************************************/
  262. static void btu_general_alarm_process(void *param)
  263. {
  264. TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)param;
  265. assert(p_tle != NULL);
  266. switch (p_tle->event) {
  267. case BTU_TTYPE_BTM_DEV_CTL:
  268. btm_dev_timeout(p_tle);
  269. break;
  270. case BTU_TTYPE_L2CAP_LINK:
  271. case BTU_TTYPE_L2CAP_CHNL:
  272. case BTU_TTYPE_L2CAP_HOLD:
  273. case BTU_TTYPE_L2CAP_INFO:
  274. case BTU_TTYPE_L2CAP_FCR_ACK:
  275. case BTU_TTYPE_L2CAP_UPDA_CONN_PARAMS:
  276. l2c_process_timeout (p_tle);
  277. break;
  278. #if (defined(SDP_INCLUDED) && SDP_INCLUDED == TRUE)
  279. case BTU_TTYPE_SDP:
  280. sdp_conn_timeout ((tCONN_CB *)p_tle->param);
  281. break;
  282. #endif
  283. case BTU_TTYPE_BTM_RMT_NAME:
  284. btm_inq_rmt_name_failed();
  285. break;
  286. #if (defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE)
  287. case BTU_TTYPE_RFCOMM_MFC:
  288. case BTU_TTYPE_RFCOMM_PORT:
  289. rfcomm_process_timeout (p_tle);
  290. break;
  291. #endif
  292. #if ((defined(BNEP_INCLUDED) && BNEP_INCLUDED == TRUE))
  293. case BTU_TTYPE_BNEP:
  294. bnep_process_timeout(p_tle);
  295. break;
  296. #endif
  297. #if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE)
  298. case BTU_TTYPE_AVDT_SCB_DELAY_RPT:
  299. case BTU_TTYPE_AVDT_CCB_RET:
  300. case BTU_TTYPE_AVDT_CCB_RSP:
  301. case BTU_TTYPE_AVDT_CCB_IDLE:
  302. case BTU_TTYPE_AVDT_SCB_TC:
  303. avdt_process_timeout(p_tle);
  304. break;
  305. #endif
  306. #if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE)
  307. case BTU_TTYPE_HID_HOST_REPAGE_TO :
  308. hidh_proc_repage_timeout(p_tle);
  309. break;
  310. #endif
  311. #if (defined(BLE_INCLUDED) && BLE_INCLUDED == TRUE)
  312. case BTU_TTYPE_BLE_INQUIRY:
  313. case BTU_TTYPE_BLE_GAP_LIM_DISC:
  314. case BTU_TTYPE_BLE_RANDOM_ADDR:
  315. case BTU_TTYPE_BLE_GAP_FAST_ADV:
  316. case BTU_TTYPE_BLE_SCAN:
  317. case BTU_TTYPE_BLE_OBSERVE:
  318. btm_ble_timeout(p_tle);
  319. break;
  320. case BTU_TTYPE_ATT_WAIT_FOR_RSP:
  321. gatt_rsp_timeout(p_tle);
  322. break;
  323. case BTU_TTYPE_ATT_WAIT_FOR_IND_ACK:
  324. gatt_ind_ack_timeout(p_tle);
  325. break;
  326. #if (defined(SMP_INCLUDED) && SMP_INCLUDED == TRUE)
  327. case BTU_TTYPE_SMP_PAIRING_CMD:
  328. smp_rsp_timeout(p_tle);
  329. break;
  330. #endif
  331. #endif
  332. #if (MCA_INCLUDED == TRUE)
  333. case BTU_TTYPE_MCA_CCB_RSP:
  334. mca_process_timeout(p_tle);
  335. break;
  336. #endif
  337. case BTU_TTYPE_USER_FUNC: {
  338. tUSER_TIMEOUT_FUNC *p_uf = (tUSER_TIMEOUT_FUNC *)p_tle->param;
  339. (*p_uf)(p_tle);
  340. }
  341. break;
  342. case BTU_TTYPE_BTM_QOS:
  343. btm_qos_setup_timeout(p_tle);
  344. break;
  345. case BTU_TTYPE_BTM_SET_PAGE_TO:
  346. btm_page_to_setup_timeout(p_tle);
  347. break;
  348. default:
  349. for (int i = 0; i < BTU_MAX_REG_TIMER; i++) {
  350. if (btu_cb.timer_reg[i].timer_cb == NULL) {
  351. continue;
  352. }
  353. if (btu_cb.timer_reg[i].p_tle == p_tle) {
  354. btu_cb.timer_reg[i].timer_cb(p_tle);
  355. break;
  356. }
  357. }
  358. break;
  359. }
  360. }
  361. void btu_general_alarm_cb(void *data)
  362. {
  363. assert(data != NULL);
  364. TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;
  365. btu_task_post(SIG_BTU_GENERAL_ALARM, p_tle, OSI_THREAD_MAX_TIMEOUT);
  366. }
  367. void btu_start_timer(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_sec)
  368. {
  369. osi_alarm_t *alarm = NULL;
  370. assert(p_tle != NULL);
  371. // Get the alarm for the timer list entry.
  372. osi_mutex_lock(&btu_general_alarm_lock, OSI_MUTEX_MAX_TIMEOUT);
  373. if (!hash_map_has_key(btu_general_alarm_hash_map, p_tle)) {
  374. alarm = osi_alarm_new("btu_gen", btu_general_alarm_cb, (void *)p_tle, 0);
  375. hash_map_set(btu_general_alarm_hash_map, p_tle, alarm);
  376. }
  377. osi_mutex_unlock(&btu_general_alarm_lock);
  378. alarm = hash_map_get(btu_general_alarm_hash_map, p_tle);
  379. if (alarm == NULL) {
  380. HCI_TRACE_ERROR("%s Unable to create alarm", __func__);
  381. return;
  382. }
  383. osi_alarm_cancel(alarm);
  384. p_tle->event = type;
  385. // NOTE: This value is in seconds but stored in a ticks field.
  386. p_tle->ticks = timeout_sec;
  387. p_tle->in_use = TRUE;
  388. osi_alarm_set(alarm, (period_ms_t)(timeout_sec * 1000));
  389. }
  390. /*******************************************************************************
  391. **
  392. ** Function btu_stop_timer
  393. **
  394. ** Description Stop a timer.
  395. **
  396. ** Returns void
  397. **
  398. *******************************************************************************/
  399. void btu_stop_timer(TIMER_LIST_ENT *p_tle)
  400. {
  401. assert(p_tle != NULL);
  402. if (p_tle->in_use == FALSE) {
  403. return;
  404. }
  405. p_tle->in_use = FALSE;
  406. // Get the alarm for the timer list entry.
  407. osi_alarm_t *alarm = hash_map_get(btu_general_alarm_hash_map, p_tle);
  408. if (alarm == NULL) {
  409. HCI_TRACE_WARNING("%s Unable to find expected alarm in hashmap", __func__);
  410. return;
  411. }
  412. osi_alarm_cancel(alarm);
  413. }
  414. /*******************************************************************************
  415. **
  416. ** Function btu_free_timer
  417. **
  418. ** Description Stop and free a timer.
  419. **
  420. ** Returns void
  421. **
  422. *******************************************************************************/
  423. void btu_free_timer(TIMER_LIST_ENT *p_tle)
  424. {
  425. assert(p_tle != NULL);
  426. p_tle->in_use = FALSE;
  427. // Get the alarm for the timer list entry.
  428. osi_alarm_t *alarm = hash_map_get(btu_general_alarm_hash_map, p_tle);
  429. if (alarm == NULL) {
  430. HCI_TRACE_DEBUG("%s Unable to find expected alarm in hashmap", __func__);
  431. return;
  432. }
  433. osi_alarm_cancel(alarm);
  434. hash_map_erase(btu_general_alarm_hash_map, p_tle);
  435. }
  436. #if defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0)
  437. /*******************************************************************************
  438. **
  439. ** Function btu_start_quick_timer
  440. **
  441. ** Description Start a timer for the specified amount of time in ticks.
  442. **
  443. ** Returns void
  444. **
  445. *******************************************************************************/
  446. static void btu_l2cap_alarm_process(void *param)
  447. {
  448. TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)param;
  449. assert(p_tle != NULL);
  450. switch (p_tle->event) {
  451. case BTU_TTYPE_L2CAP_CHNL: /* monitor or retransmission timer */
  452. case BTU_TTYPE_L2CAP_FCR_ACK: /* ack timer */
  453. l2c_process_timeout (p_tle);
  454. break;
  455. default:
  456. break;
  457. }
  458. }
  459. static void btu_l2cap_alarm_cb(void *data)
  460. {
  461. assert(data != NULL);
  462. TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;
  463. btu_task_post(SIG_BTU_L2CAP_ALARM, p_tle, OSI_THREAD_MAX_TIMEOUT);
  464. }
  465. void btu_start_quick_timer(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_ticks)
  466. {
  467. osi_alarm_t *alarm = NULL;
  468. assert(p_tle != NULL);
  469. // Get the alarm for the timer list entry.
  470. osi_mutex_lock(&btu_l2cap_alarm_lock, OSI_MUTEX_MAX_TIMEOUT);
  471. if (!hash_map_has_key(btu_l2cap_alarm_hash_map, p_tle)) {
  472. alarm = osi_alarm_new("btu_l2cap", btu_l2cap_alarm_cb, (void *)p_tle, 0);
  473. hash_map_set(btu_l2cap_alarm_hash_map, p_tle, (void *)alarm);
  474. }
  475. osi_mutex_unlock(&btu_l2cap_alarm_lock);
  476. alarm = hash_map_get(btu_l2cap_alarm_hash_map, p_tle);
  477. if (alarm == NULL) {
  478. HCI_TRACE_ERROR("%s Unable to create alarm", __func__);
  479. return;
  480. }
  481. osi_alarm_cancel(alarm);
  482. p_tle->event = type;
  483. p_tle->ticks = timeout_ticks;
  484. p_tle->in_use = TRUE;
  485. // The quick timer ticks are 100ms long.
  486. osi_alarm_set(alarm, (period_ms_t)(timeout_ticks * 100));
  487. }
  488. /*******************************************************************************
  489. **
  490. ** Function btu_stop_quick_timer
  491. **
  492. ** Description Stop a timer.
  493. **
  494. ** Returns void
  495. **
  496. *******************************************************************************/
  497. void btu_stop_quick_timer(TIMER_LIST_ENT *p_tle)
  498. {
  499. assert(p_tle != NULL);
  500. if (p_tle->in_use == FALSE) {
  501. return;
  502. }
  503. p_tle->in_use = FALSE;
  504. // Get the alarm for the timer list entry.
  505. osi_alarm_t *alarm = hash_map_get(btu_l2cap_alarm_hash_map, p_tle);
  506. if (alarm == NULL) {
  507. HCI_TRACE_WARNING("%s Unable to find expected alarm in hashmap", __func__);
  508. return;
  509. }
  510. osi_alarm_cancel(alarm);
  511. }
  512. void btu_free_quick_timer(TIMER_LIST_ENT *p_tle)
  513. {
  514. assert(p_tle != NULL);
  515. p_tle->in_use = FALSE;
  516. // Get the alarm for the timer list entry.
  517. osi_alarm_t *alarm = hash_map_get(btu_l2cap_alarm_hash_map, p_tle);
  518. if (alarm == NULL) {
  519. HCI_TRACE_DEBUG("%s Unable to find expected alarm in hashmap", __func__);
  520. return;
  521. }
  522. osi_alarm_cancel(alarm);
  523. hash_map_erase(btu_l2cap_alarm_hash_map, p_tle);
  524. }
  525. #endif /* defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0) */
  526. void btu_oneshot_alarm_cb(void *data)
  527. {
  528. assert(data != NULL);
  529. TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;
  530. btu_stop_timer_oneshot(p_tle);
  531. btu_task_post(SIG_BTU_ONESHOT_ALARM, p_tle, OSI_THREAD_MAX_TIMEOUT);
  532. }
  533. /*
  534. * Starts a oneshot timer with a timeout in seconds.
  535. */
  536. void btu_start_timer_oneshot(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_sec)
  537. {
  538. osi_alarm_t *alarm = NULL;
  539. assert(p_tle != NULL);
  540. // Get the alarm for the timer list entry.
  541. osi_mutex_lock(&btu_oneshot_alarm_lock, OSI_MUTEX_MAX_TIMEOUT);
  542. if (!hash_map_has_key(btu_oneshot_alarm_hash_map, p_tle)) {
  543. alarm = osi_alarm_new("btu_oneshot", btu_oneshot_alarm_cb, (void *)p_tle, 0);
  544. hash_map_set(btu_oneshot_alarm_hash_map, p_tle, alarm);
  545. }
  546. osi_mutex_unlock(&btu_oneshot_alarm_lock);
  547. alarm = hash_map_get(btu_oneshot_alarm_hash_map, p_tle);
  548. if (alarm == NULL) {
  549. HCI_TRACE_ERROR("%s Unable to create alarm", __func__);
  550. return;
  551. }
  552. osi_alarm_cancel(alarm);
  553. p_tle->event = type;
  554. p_tle->in_use = TRUE;
  555. // NOTE: This value is in seconds but stored in a ticks field.
  556. p_tle->ticks = timeout_sec;
  557. osi_alarm_set(alarm, (period_ms_t)(timeout_sec * 1000));
  558. }
  559. void btu_stop_timer_oneshot(TIMER_LIST_ENT *p_tle)
  560. {
  561. assert(p_tle != NULL);
  562. if (p_tle->in_use == FALSE) {
  563. return;
  564. }
  565. p_tle->in_use = FALSE;
  566. // Get the alarm for the timer list entry.
  567. osi_alarm_t *alarm = hash_map_get(btu_oneshot_alarm_hash_map, p_tle);
  568. if (alarm == NULL) {
  569. HCI_TRACE_WARNING("%s Unable to find expected alarm in hashmap", __func__);
  570. return;
  571. }
  572. osi_alarm_cancel(alarm);
  573. }
  574. #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
  575. /*******************************************************************************
  576. **
  577. ** Function btu_check_bt_sleep
  578. **
  579. ** Description This function is called to check if controller can go to sleep.
  580. **
  581. ** Returns void
  582. **
  583. *******************************************************************************/
  584. void btu_check_bt_sleep (void)
  585. {
  586. // TODO(zachoverflow) take pending commands into account?
  587. if (l2cb.controller_xmit_window == l2cb.num_lm_acl_bufs) {
  588. bte_main_lpm_allow_bt_device_sleep();
  589. }
  590. }
  591. #endif