btu_task.c 19 KB

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