btu_task.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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. //#if (defined(BT_APP_DEMO) && BT_APP_DEMO == TRUE)
  73. //#include "bt_app_common.h"
  74. //#endif
  75. extern void BTE_InitStack(void);
  76. extern void BTE_DeinitStack(void);
  77. /* Define BTU storage area
  78. */
  79. #if BTU_DYNAMIC_MEMORY == FALSE
  80. tBTU_CB btu_cb;
  81. #else
  82. tBTU_CB *btu_cb_ptr;
  83. #endif
  84. extern hash_map_t *btu_general_alarm_hash_map;
  85. extern osi_mutex_t btu_general_alarm_lock;
  86. // Oneshot timer queue.
  87. extern hash_map_t *btu_oneshot_alarm_hash_map;
  88. extern osi_mutex_t btu_oneshot_alarm_lock;
  89. // l2cap timer queue.
  90. extern hash_map_t *btu_l2cap_alarm_hash_map;
  91. extern osi_mutex_t btu_l2cap_alarm_lock;
  92. extern xTaskHandle xBtuTaskHandle;
  93. extern xQueueHandle xBtuQueue;
  94. extern bluedroid_init_done_cb_t bluedroid_init_done_cb;
  95. /* Define a function prototype to allow a generic timeout handler */
  96. typedef void (tUSER_TIMEOUT_FUNC) (TIMER_LIST_ENT *p_tle);
  97. static void btu_l2cap_alarm_process(TIMER_LIST_ENT *p_tle);
  98. static void btu_general_alarm_process(TIMER_LIST_ENT *p_tle);
  99. static void btu_hci_msg_process(BT_HDR *p_msg);
  100. #if (defined(BTA_INCLUDED) && BTA_INCLUDED == TRUE)
  101. static void btu_bta_alarm_process(TIMER_LIST_ENT *p_tle);
  102. #endif
  103. static void btu_hci_msg_process(BT_HDR *p_msg)
  104. {
  105. /* Determine the input message type. */
  106. switch (p_msg->event & BT_EVT_MASK) {
  107. case BTU_POST_TO_TASK_NO_GOOD_HORRIBLE_HACK: // TODO(zachoverflow): remove this
  108. {
  109. post_to_task_hack_t *ph = (post_to_task_hack_t *) &p_msg->data[0];
  110. ph->callback(p_msg);
  111. break;
  112. }
  113. case BT_EVT_TO_BTU_HCI_ACL:
  114. /* All Acl Data goes to L2CAP */
  115. l2c_rcv_acl_data (p_msg);
  116. break;
  117. case BT_EVT_TO_BTU_L2C_SEG_XMIT:
  118. /* L2CAP segment transmit complete */
  119. l2c_link_segments_xmitted (p_msg);
  120. break;
  121. case BT_EVT_TO_BTU_HCI_SCO:
  122. #if BTM_SCO_INCLUDED == TRUE
  123. btm_route_sco_data (p_msg);
  124. break;
  125. #endif
  126. case BT_EVT_TO_BTU_HCI_EVT:
  127. btu_hcif_process_event ((UINT8)(p_msg->event & BT_SUB_EVT_MASK), p_msg);
  128. osi_free(p_msg);
  129. #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
  130. /* If host receives events which it doesn't response to, */
  131. /* host should start idle timer to enter sleep mode. */
  132. btu_check_bt_sleep ();
  133. #endif
  134. break;
  135. case BT_EVT_TO_BTU_HCI_CMD:
  136. btu_hcif_send_cmd ((UINT8)(p_msg->event & BT_SUB_EVT_MASK), p_msg);
  137. break;
  138. default:;
  139. int i = 0;
  140. uint16_t mask = (UINT16) (p_msg->event & BT_EVT_MASK);
  141. BOOLEAN handled = FALSE;
  142. for (; !handled && i < BTU_MAX_REG_EVENT; i++) {
  143. if (btu_cb.event_reg[i].event_cb == NULL) {
  144. continue;
  145. }
  146. if (mask == btu_cb.event_reg[i].event_range) {
  147. if (btu_cb.event_reg[i].event_cb) {
  148. btu_cb.event_reg[i].event_cb(p_msg);
  149. handled = TRUE;
  150. }
  151. }
  152. }
  153. if (handled == FALSE) {
  154. osi_free (p_msg);
  155. }
  156. break;
  157. }
  158. }
  159. #if (defined(BTA_INCLUDED) && BTA_INCLUDED == TRUE)
  160. static void btu_bta_alarm_process(TIMER_LIST_ENT *p_tle)
  161. {
  162. // call timer callback
  163. if (p_tle->p_cback) {
  164. (*p_tle->p_cback)(p_tle);
  165. } else if (p_tle->event) {
  166. BT_HDR *p_msg;
  167. if ((p_msg = (BT_HDR *) osi_malloc(sizeof(BT_HDR))) != NULL) {
  168. p_msg->event = p_tle->event;
  169. p_msg->layer_specific = 0;
  170. //osi_free(p_msg);
  171. bta_sys_sendmsg(p_msg);
  172. }
  173. }
  174. }
  175. #endif
  176. /*****************************************************************************
  177. **
  178. ** Function btu_task_thread_handler
  179. **
  180. ** Description Process BTU Task Thread.
  181. ******************************************************************************/
  182. void btu_task_thread_handler(void *arg)
  183. {
  184. BtTaskEvt_t e;
  185. for (;;) {
  186. if (pdTRUE == xQueueReceive(xBtuQueue, &e, (portTickType)portMAX_DELAY)) {
  187. switch (e.sig) {
  188. case SIG_BTU_START_UP:
  189. btu_task_start_up();
  190. break;
  191. case SIG_BTU_HCI_MSG:
  192. btu_hci_msg_process((BT_HDR *)e.par);
  193. break;
  194. #if (defined(BTA_INCLUDED) && BTA_INCLUDED == TRUE)
  195. case SIG_BTU_BTA_MSG:
  196. bta_sys_event((BT_HDR *)e.par);
  197. break;
  198. case SIG_BTU_BTA_ALARM:
  199. btu_bta_alarm_process((TIMER_LIST_ENT *)e.par);
  200. break;
  201. #endif
  202. case SIG_BTU_GENERAL_ALARM:
  203. btu_general_alarm_process((TIMER_LIST_ENT *)e.par);
  204. break;
  205. case SIG_BTU_ONESHOT_ALARM: {
  206. TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)e.par;
  207. btu_general_alarm_process(p_tle);
  208. break;
  209. }
  210. case SIG_BTU_L2CAP_ALARM:
  211. btu_l2cap_alarm_process((TIMER_LIST_ENT *)e.par);
  212. break;
  213. default:
  214. break;
  215. }
  216. }
  217. }
  218. }
  219. task_post_status_t btu_task_post(uint32_t sig, void *param, task_post_t timeout)
  220. {
  221. BtTaskEvt_t evt;
  222. evt.sig = sig;
  223. evt.par = param;
  224. if (xQueueSend(xBtuQueue, &evt, timeout) != pdTRUE) {
  225. HCI_TRACE_ERROR("xBtuQueue failed\n");
  226. return TASK_POST_FAIL;
  227. }
  228. return TASK_POST_SUCCESS;
  229. }
  230. void btu_task_start_up(void)
  231. {
  232. /* Initialize the mandatory core stack control blocks
  233. (BTU, BTM, L2CAP, and SDP)
  234. */
  235. btu_init_core();
  236. /* Initialize any optional stack components */
  237. BTE_InitStack();
  238. #if (defined(BTA_INCLUDED) && BTA_INCLUDED == TRUE)
  239. bta_sys_init();
  240. #endif
  241. // Inform the bt jni thread initialization is ok.
  242. // btif_transfer_context(btif_init_ok, 0, NULL, 0, NULL);
  243. #if(defined(BT_APP_DEMO) && BT_APP_DEMO == TRUE)
  244. if (bluedroid_init_done_cb) {
  245. bluedroid_init_done_cb();
  246. }
  247. #endif
  248. }
  249. void btu_task_shut_down(void)
  250. {
  251. #if (defined(BTA_INCLUDED) && BTA_INCLUDED == TRUE)
  252. bta_sys_free();
  253. #endif
  254. BTE_DeinitStack();
  255. btu_free_core();
  256. }
  257. /*******************************************************************************
  258. **
  259. ** Function btu_start_timer
  260. **
  261. ** Description Start a timer for the specified amount of time.
  262. ** NOTE: The timeout resolution is in SECONDS! (Even
  263. ** though the timer structure field is ticks)
  264. **
  265. ** Returns void
  266. **
  267. *******************************************************************************/
  268. static void btu_general_alarm_process(TIMER_LIST_ENT *p_tle)
  269. {
  270. assert(p_tle != NULL);
  271. switch (p_tle->event) {
  272. case BTU_TTYPE_BTM_DEV_CTL:
  273. btm_dev_timeout(p_tle);
  274. break;
  275. case BTU_TTYPE_L2CAP_LINK:
  276. case BTU_TTYPE_L2CAP_CHNL:
  277. case BTU_TTYPE_L2CAP_HOLD:
  278. case BTU_TTYPE_L2CAP_INFO:
  279. case BTU_TTYPE_L2CAP_FCR_ACK:
  280. case BTU_TTYPE_L2CAP_UPDA_CONN_PARAMS:
  281. l2c_process_timeout (p_tle);
  282. break;
  283. #if (defined(SDP_INCLUDED) && SDP_INCLUDED == TRUE)
  284. case BTU_TTYPE_SDP:
  285. sdp_conn_timeout ((tCONN_CB *)p_tle->param);
  286. break;
  287. #endif
  288. case BTU_TTYPE_BTM_RMT_NAME:
  289. btm_inq_rmt_name_failed();
  290. break;
  291. #if (defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE)
  292. case BTU_TTYPE_RFCOMM_MFC:
  293. case BTU_TTYPE_RFCOMM_PORT:
  294. rfcomm_process_timeout (p_tle);
  295. break;
  296. #endif
  297. #if ((defined(BNEP_INCLUDED) && BNEP_INCLUDED == TRUE))
  298. case BTU_TTYPE_BNEP:
  299. bnep_process_timeout(p_tle);
  300. break;
  301. #endif
  302. #if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE)
  303. case BTU_TTYPE_AVDT_CCB_RET:
  304. case BTU_TTYPE_AVDT_CCB_RSP:
  305. case BTU_TTYPE_AVDT_CCB_IDLE:
  306. case BTU_TTYPE_AVDT_SCB_TC:
  307. avdt_process_timeout(p_tle);
  308. break;
  309. #endif
  310. #if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE)
  311. case BTU_TTYPE_HID_HOST_REPAGE_TO :
  312. hidh_proc_repage_timeout(p_tle);
  313. break;
  314. #endif
  315. #if (defined(BLE_INCLUDED) && BLE_INCLUDED == TRUE)
  316. case BTU_TTYPE_BLE_INQUIRY:
  317. case BTU_TTYPE_BLE_GAP_LIM_DISC:
  318. case BTU_TTYPE_BLE_RANDOM_ADDR:
  319. case BTU_TTYPE_BLE_GAP_FAST_ADV:
  320. case BTU_TTYPE_BLE_SCAN:
  321. case BTU_TTYPE_BLE_OBSERVE:
  322. btm_ble_timeout(p_tle);
  323. break;
  324. case BTU_TTYPE_ATT_WAIT_FOR_RSP:
  325. gatt_rsp_timeout(p_tle);
  326. break;
  327. case BTU_TTYPE_ATT_WAIT_FOR_IND_ACK:
  328. gatt_ind_ack_timeout(p_tle);
  329. break;
  330. #if (defined(SMP_INCLUDED) && SMP_INCLUDED == TRUE)
  331. case BTU_TTYPE_SMP_PAIRING_CMD:
  332. smp_rsp_timeout(p_tle);
  333. break;
  334. #endif
  335. #endif
  336. #if (MCA_INCLUDED == TRUE)
  337. case BTU_TTYPE_MCA_CCB_RSP:
  338. mca_process_timeout(p_tle);
  339. break;
  340. #endif
  341. case BTU_TTYPE_USER_FUNC: {
  342. tUSER_TIMEOUT_FUNC *p_uf = (tUSER_TIMEOUT_FUNC *)p_tle->param;
  343. (*p_uf)(p_tle);
  344. }
  345. break;
  346. default:;
  347. int i = 0;
  348. BOOLEAN handled = FALSE;
  349. for (; !handled && 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. handled = TRUE;
  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, TASK_POST_BLOCKING);
  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(TIMER_LIST_ENT *p_tle)
  447. {
  448. assert(p_tle != NULL);
  449. switch (p_tle->event) {
  450. case BTU_TTYPE_L2CAP_CHNL: /* monitor or retransmission timer */
  451. case BTU_TTYPE_L2CAP_FCR_ACK: /* ack timer */
  452. l2c_process_timeout (p_tle);
  453. break;
  454. default:
  455. break;
  456. }
  457. }
  458. static void btu_l2cap_alarm_cb(void *data)
  459. {
  460. assert(data != NULL);
  461. TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;
  462. btu_task_post(SIG_BTU_L2CAP_ALARM, p_tle, TASK_POST_BLOCKING);
  463. }
  464. void btu_start_quick_timer(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_ticks)
  465. {
  466. osi_alarm_t *alarm = NULL;
  467. assert(p_tle != NULL);
  468. // Get the alarm for the timer list entry.
  469. osi_mutex_lock(&btu_l2cap_alarm_lock, OSI_MUTEX_MAX_TIMEOUT);
  470. if (!hash_map_has_key(btu_l2cap_alarm_hash_map, p_tle)) {
  471. alarm = osi_alarm_new("btu_l2cap", btu_l2cap_alarm_cb, (void *)p_tle, 0);
  472. hash_map_set(btu_l2cap_alarm_hash_map, p_tle, (void *)alarm);
  473. }
  474. osi_mutex_unlock(&btu_l2cap_alarm_lock);
  475. alarm = hash_map_get(btu_l2cap_alarm_hash_map, p_tle);
  476. if (alarm == NULL) {
  477. HCI_TRACE_ERROR("%s Unable to create alarm", __func__);
  478. return;
  479. }
  480. osi_alarm_cancel(alarm);
  481. p_tle->event = type;
  482. p_tle->ticks = timeout_ticks;
  483. p_tle->in_use = TRUE;
  484. // The quick timer ticks are 100ms long.
  485. osi_alarm_set(alarm, (period_ms_t)(timeout_ticks * 100));
  486. }
  487. /*******************************************************************************
  488. **
  489. ** Function btu_stop_quick_timer
  490. **
  491. ** Description Stop a timer.
  492. **
  493. ** Returns void
  494. **
  495. *******************************************************************************/
  496. void btu_stop_quick_timer(TIMER_LIST_ENT *p_tle)
  497. {
  498. assert(p_tle != NULL);
  499. if (p_tle->in_use == FALSE) {
  500. return;
  501. }
  502. p_tle->in_use = FALSE;
  503. // Get the alarm for the timer list entry.
  504. osi_alarm_t *alarm = hash_map_get(btu_l2cap_alarm_hash_map, p_tle);
  505. if (alarm == NULL) {
  506. HCI_TRACE_WARNING("%s Unable to find expected alarm in hashmap", __func__);
  507. return;
  508. }
  509. osi_alarm_cancel(alarm);
  510. }
  511. void btu_free_quick_timer(TIMER_LIST_ENT *p_tle)
  512. {
  513. assert(p_tle != NULL);
  514. p_tle->in_use = FALSE;
  515. // Get the alarm for the timer list entry.
  516. osi_alarm_t *alarm = hash_map_get(btu_l2cap_alarm_hash_map, p_tle);
  517. if (alarm == NULL) {
  518. HCI_TRACE_DEBUG("%s Unable to find expected alarm in hashmap", __func__);
  519. return;
  520. }
  521. osi_alarm_cancel(alarm);
  522. hash_map_erase(btu_l2cap_alarm_hash_map, p_tle);
  523. }
  524. #endif /* defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0) */
  525. void btu_oneshot_alarm_cb(void *data)
  526. {
  527. assert(data != NULL);
  528. TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;
  529. btu_stop_timer_oneshot(p_tle);
  530. btu_task_post(SIG_BTU_ONESHOT_ALARM, p_tle, TASK_POST_BLOCKING);
  531. }
  532. /*
  533. * Starts a oneshot timer with a timeout in seconds.
  534. */
  535. void btu_start_timer_oneshot(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_sec)
  536. {
  537. osi_alarm_t *alarm = NULL;
  538. assert(p_tle != NULL);
  539. // Get the alarm for the timer list entry.
  540. osi_mutex_lock(&btu_oneshot_alarm_lock, OSI_MUTEX_MAX_TIMEOUT);
  541. if (!hash_map_has_key(btu_oneshot_alarm_hash_map, p_tle)) {
  542. alarm = osi_alarm_new("btu_oneshot", btu_oneshot_alarm_cb, (void *)p_tle, 0);
  543. hash_map_set(btu_oneshot_alarm_hash_map, p_tle, alarm);
  544. }
  545. osi_mutex_unlock(&btu_oneshot_alarm_lock);
  546. alarm = hash_map_get(btu_oneshot_alarm_hash_map, p_tle);
  547. if (alarm == NULL) {
  548. HCI_TRACE_ERROR("%s Unable to create alarm", __func__);
  549. return;
  550. }
  551. osi_alarm_cancel(alarm);
  552. p_tle->event = type;
  553. p_tle->in_use = TRUE;
  554. // NOTE: This value is in seconds but stored in a ticks field.
  555. p_tle->ticks = timeout_sec;
  556. osi_alarm_set(alarm, (period_ms_t)(timeout_sec * 1000));
  557. }
  558. void btu_stop_timer_oneshot(TIMER_LIST_ENT *p_tle)
  559. {
  560. assert(p_tle != NULL);
  561. if (p_tle->in_use == FALSE) {
  562. return;
  563. }
  564. p_tle->in_use = FALSE;
  565. // Get the alarm for the timer list entry.
  566. osi_alarm_t *alarm = hash_map_get(btu_oneshot_alarm_hash_map, p_tle);
  567. if (alarm == NULL) {
  568. HCI_TRACE_WARNING("%s Unable to find expected alarm in hashmap", __func__);
  569. return;
  570. }
  571. osi_alarm_cancel(alarm);
  572. }
  573. #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
  574. /*******************************************************************************
  575. **
  576. ** Function btu_check_bt_sleep
  577. **
  578. ** Description This function is called to check if controller can go to sleep.
  579. **
  580. ** Returns void
  581. **
  582. *******************************************************************************/
  583. void btu_check_bt_sleep (void)
  584. {
  585. // TODO(zachoverflow) take pending commands into account?
  586. if (l2cb.controller_xmit_window == l2cb.num_lm_acl_bufs) {
  587. bte_main_lpm_allow_bt_device_sleep();
  588. }
  589. }
  590. #endif