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