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. default:;
  330. int i = 0;
  331. BOOLEAN handled = FALSE;
  332. for (; !handled && i < BTU_MAX_REG_TIMER; i++) {
  333. if (btu_cb.timer_reg[i].timer_cb == NULL) {
  334. continue;
  335. }
  336. if (btu_cb.timer_reg[i].p_tle == p_tle) {
  337. btu_cb.timer_reg[i].timer_cb(p_tle);
  338. handled = TRUE;
  339. }
  340. }
  341. break;
  342. }
  343. }
  344. void btu_general_alarm_cb(void *data)
  345. {
  346. assert(data != NULL);
  347. TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;
  348. btu_task_post(SIG_BTU_GENERAL_ALARM, p_tle, OSI_THREAD_MAX_TIMEOUT);
  349. }
  350. void btu_start_timer(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_sec)
  351. {
  352. osi_alarm_t *alarm = NULL;
  353. assert(p_tle != NULL);
  354. // Get the alarm for the timer list entry.
  355. osi_mutex_lock(&btu_general_alarm_lock, OSI_MUTEX_MAX_TIMEOUT);
  356. if (!hash_map_has_key(btu_general_alarm_hash_map, p_tle)) {
  357. alarm = osi_alarm_new("btu_gen", btu_general_alarm_cb, (void *)p_tle, 0);
  358. hash_map_set(btu_general_alarm_hash_map, p_tle, alarm);
  359. }
  360. osi_mutex_unlock(&btu_general_alarm_lock);
  361. alarm = hash_map_get(btu_general_alarm_hash_map, p_tle);
  362. if (alarm == NULL) {
  363. HCI_TRACE_ERROR("%s Unable to create alarm", __func__);
  364. return;
  365. }
  366. osi_alarm_cancel(alarm);
  367. p_tle->event = type;
  368. // NOTE: This value is in seconds but stored in a ticks field.
  369. p_tle->ticks = timeout_sec;
  370. p_tle->in_use = TRUE;
  371. osi_alarm_set(alarm, (period_ms_t)(timeout_sec * 1000));
  372. }
  373. /*******************************************************************************
  374. **
  375. ** Function btu_stop_timer
  376. **
  377. ** Description Stop a timer.
  378. **
  379. ** Returns void
  380. **
  381. *******************************************************************************/
  382. void btu_stop_timer(TIMER_LIST_ENT *p_tle)
  383. {
  384. assert(p_tle != NULL);
  385. if (p_tle->in_use == FALSE) {
  386. return;
  387. }
  388. p_tle->in_use = FALSE;
  389. // Get the alarm for the timer list entry.
  390. osi_alarm_t *alarm = hash_map_get(btu_general_alarm_hash_map, p_tle);
  391. if (alarm == NULL) {
  392. HCI_TRACE_WARNING("%s Unable to find expected alarm in hashmap", __func__);
  393. return;
  394. }
  395. osi_alarm_cancel(alarm);
  396. }
  397. /*******************************************************************************
  398. **
  399. ** Function btu_free_timer
  400. **
  401. ** Description Stop and free a timer.
  402. **
  403. ** Returns void
  404. **
  405. *******************************************************************************/
  406. void btu_free_timer(TIMER_LIST_ENT *p_tle)
  407. {
  408. assert(p_tle != NULL);
  409. p_tle->in_use = FALSE;
  410. // Get the alarm for the timer list entry.
  411. osi_alarm_t *alarm = hash_map_get(btu_general_alarm_hash_map, p_tle);
  412. if (alarm == NULL) {
  413. HCI_TRACE_DEBUG("%s Unable to find expected alarm in hashmap", __func__);
  414. return;
  415. }
  416. osi_alarm_cancel(alarm);
  417. hash_map_erase(btu_general_alarm_hash_map, p_tle);
  418. }
  419. #if defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0)
  420. /*******************************************************************************
  421. **
  422. ** Function btu_start_quick_timer
  423. **
  424. ** Description Start a timer for the specified amount of time in ticks.
  425. **
  426. ** Returns void
  427. **
  428. *******************************************************************************/
  429. static void btu_l2cap_alarm_process(void *param)
  430. {
  431. TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)param;
  432. assert(p_tle != NULL);
  433. switch (p_tle->event) {
  434. case BTU_TTYPE_L2CAP_CHNL: /* monitor or retransmission timer */
  435. case BTU_TTYPE_L2CAP_FCR_ACK: /* ack timer */
  436. l2c_process_timeout (p_tle);
  437. break;
  438. default:
  439. break;
  440. }
  441. }
  442. static void btu_l2cap_alarm_cb(void *data)
  443. {
  444. assert(data != NULL);
  445. TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;
  446. btu_task_post(SIG_BTU_L2CAP_ALARM, p_tle, OSI_THREAD_MAX_TIMEOUT);
  447. }
  448. void btu_start_quick_timer(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_ticks)
  449. {
  450. osi_alarm_t *alarm = NULL;
  451. assert(p_tle != NULL);
  452. // Get the alarm for the timer list entry.
  453. osi_mutex_lock(&btu_l2cap_alarm_lock, OSI_MUTEX_MAX_TIMEOUT);
  454. if (!hash_map_has_key(btu_l2cap_alarm_hash_map, p_tle)) {
  455. alarm = osi_alarm_new("btu_l2cap", btu_l2cap_alarm_cb, (void *)p_tle, 0);
  456. hash_map_set(btu_l2cap_alarm_hash_map, p_tle, (void *)alarm);
  457. }
  458. osi_mutex_unlock(&btu_l2cap_alarm_lock);
  459. alarm = hash_map_get(btu_l2cap_alarm_hash_map, p_tle);
  460. if (alarm == NULL) {
  461. HCI_TRACE_ERROR("%s Unable to create alarm", __func__);
  462. return;
  463. }
  464. osi_alarm_cancel(alarm);
  465. p_tle->event = type;
  466. p_tle->ticks = timeout_ticks;
  467. p_tle->in_use = TRUE;
  468. // The quick timer ticks are 100ms long.
  469. osi_alarm_set(alarm, (period_ms_t)(timeout_ticks * 100));
  470. }
  471. /*******************************************************************************
  472. **
  473. ** Function btu_stop_quick_timer
  474. **
  475. ** Description Stop a timer.
  476. **
  477. ** Returns void
  478. **
  479. *******************************************************************************/
  480. void btu_stop_quick_timer(TIMER_LIST_ENT *p_tle)
  481. {
  482. assert(p_tle != NULL);
  483. if (p_tle->in_use == FALSE) {
  484. return;
  485. }
  486. p_tle->in_use = FALSE;
  487. // Get the alarm for the timer list entry.
  488. osi_alarm_t *alarm = hash_map_get(btu_l2cap_alarm_hash_map, p_tle);
  489. if (alarm == NULL) {
  490. HCI_TRACE_WARNING("%s Unable to find expected alarm in hashmap", __func__);
  491. return;
  492. }
  493. osi_alarm_cancel(alarm);
  494. }
  495. void btu_free_quick_timer(TIMER_LIST_ENT *p_tle)
  496. {
  497. assert(p_tle != NULL);
  498. p_tle->in_use = FALSE;
  499. // Get the alarm for the timer list entry.
  500. osi_alarm_t *alarm = hash_map_get(btu_l2cap_alarm_hash_map, p_tle);
  501. if (alarm == NULL) {
  502. HCI_TRACE_DEBUG("%s Unable to find expected alarm in hashmap", __func__);
  503. return;
  504. }
  505. osi_alarm_cancel(alarm);
  506. hash_map_erase(btu_l2cap_alarm_hash_map, p_tle);
  507. }
  508. #endif /* defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0) */
  509. void btu_oneshot_alarm_cb(void *data)
  510. {
  511. assert(data != NULL);
  512. TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;
  513. btu_stop_timer_oneshot(p_tle);
  514. btu_task_post(SIG_BTU_ONESHOT_ALARM, p_tle, OSI_THREAD_MAX_TIMEOUT);
  515. }
  516. /*
  517. * Starts a oneshot timer with a timeout in seconds.
  518. */
  519. void btu_start_timer_oneshot(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_sec)
  520. {
  521. osi_alarm_t *alarm = NULL;
  522. assert(p_tle != NULL);
  523. // Get the alarm for the timer list entry.
  524. osi_mutex_lock(&btu_oneshot_alarm_lock, OSI_MUTEX_MAX_TIMEOUT);
  525. if (!hash_map_has_key(btu_oneshot_alarm_hash_map, p_tle)) {
  526. alarm = osi_alarm_new("btu_oneshot", btu_oneshot_alarm_cb, (void *)p_tle, 0);
  527. hash_map_set(btu_oneshot_alarm_hash_map, p_tle, alarm);
  528. }
  529. osi_mutex_unlock(&btu_oneshot_alarm_lock);
  530. alarm = hash_map_get(btu_oneshot_alarm_hash_map, p_tle);
  531. if (alarm == NULL) {
  532. HCI_TRACE_ERROR("%s Unable to create alarm", __func__);
  533. return;
  534. }
  535. osi_alarm_cancel(alarm);
  536. p_tle->event = type;
  537. p_tle->in_use = TRUE;
  538. // NOTE: This value is in seconds but stored in a ticks field.
  539. p_tle->ticks = timeout_sec;
  540. osi_alarm_set(alarm, (period_ms_t)(timeout_sec * 1000));
  541. }
  542. void btu_stop_timer_oneshot(TIMER_LIST_ENT *p_tle)
  543. {
  544. assert(p_tle != NULL);
  545. if (p_tle->in_use == FALSE) {
  546. return;
  547. }
  548. p_tle->in_use = FALSE;
  549. // Get the alarm for the timer list entry.
  550. osi_alarm_t *alarm = hash_map_get(btu_oneshot_alarm_hash_map, p_tle);
  551. if (alarm == NULL) {
  552. HCI_TRACE_WARNING("%s Unable to find expected alarm in hashmap", __func__);
  553. return;
  554. }
  555. osi_alarm_cancel(alarm);
  556. }
  557. #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
  558. /*******************************************************************************
  559. **
  560. ** Function btu_check_bt_sleep
  561. **
  562. ** Description This function is called to check if controller can go to sleep.
  563. **
  564. ** Returns void
  565. **
  566. *******************************************************************************/
  567. void btu_check_bt_sleep (void)
  568. {
  569. // TODO(zachoverflow) take pending commands into account?
  570. if (l2cb.controller_xmit_window == l2cb.num_lm_acl_bufs) {
  571. bte_main_lpm_allow_bt_device_sleep();
  572. }
  573. }
  574. #endif