OSDN Git Service

3daa167b0466cfd4106b4776986699b66cca22c8
[android-x86/system-bt.git] / stack / btu / btu_task.c
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
19 /******************************************************************************
20  *
21  *  This file contains the main Bluetooth Upper Layer processing loop.
22  *  The Broadcom implementations of L2CAP RFCOMM, SDP and the BTIf run as one
23  *  GKI task. This btu_task switches between them.
24  *
25  *  Note that there will always be an L2CAP, but there may or may not be an
26  *  RFCOMM or SDP. Whether these layers are present or not is determined by
27  *  compile switches.
28  *
29  ******************************************************************************/
30
31 #include <assert.h>
32
33 #define LOG_TAG "btu_task"
34 #include <cutils/log.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <stdio.h>
38
39 #include "alarm.h"
40 #include "bt_target.h"
41 #include "bt_trace.h"
42 #include "bt_types.h"
43 #include "bt_utils.h"
44 #include "btif_common.h"
45 #include "btm_api.h"
46 #include "btm_int.h"
47 #include "btu.h"
48 #include "fixed_queue.h"
49 #include "gki.h"
50 #include "hash_map.h"
51 #include "hcimsgs.h"
52 #include "l2c_int.h"
53 #include "btu.h"
54 #include "bt_utils.h"
55 #include <sys/prctl.h>
56
57 #include "sdpint.h"
58
59 #if ( defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE )
60 #include "port_api.h"
61 #include "port_ext.h"
62 #endif
63
64 #if (defined(EVAL) && EVAL == TRUE)
65 #include "btu_eval.h"
66 #endif
67
68 #if GAP_INCLUDED == TRUE
69 #include "gap_int.h"
70 #endif
71
72 #if (defined(OBX_INCLUDED) && OBX_INCLUDED == TRUE)
73 #include "obx_int.h"
74
75 #if (defined(BIP_INCLUDED) && BIP_INCLUDED == TRUE)
76 #include "bip_int.h"
77 #endif /* BIP */
78
79 #if (BPP_SND_INCLUDED == TRUE ||  BPP_INCLUDED == TRUE)
80 #include "bpp_int.h"
81 #endif /* BPP */
82
83 #endif /* OBX */
84
85 /* BTE application task */
86 #if APPL_INCLUDED == TRUE
87 #include "bte_appl.h"
88 #endif
89
90 #if (defined(BNEP_INCLUDED) && BNEP_INCLUDED == TRUE)
91 #include "bnep_int.h"
92 #endif
93
94 #if (defined(PAN_INCLUDED) && PAN_INCLUDED == TRUE)
95 #include "pan_int.h"
96 #endif
97
98 #if (defined(SAP_SERVER_INCLUDED) && SAP_SERVER_INCLUDED == TRUE)
99 #include "sap_int.h"
100 #endif
101
102 #if (defined(HID_DEV_INCLUDED) && HID_DEV_INCLUDED == TRUE )
103 #include "hidd_int.h"
104 #endif
105
106 #if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE )
107 #include "hidh_int.h"
108 #endif
109
110 #if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE)
111 #include "avdt_int.h"
112 #else
113 extern void avdt_rcv_sync_info (BT_HDR *p_buf); /* this is for hci_test */
114 #endif
115
116 #if (defined(MCA_INCLUDED) && MCA_INCLUDED == TRUE)
117 #include "mca_api.h"
118 #include "mca_defs.h"
119 #include "mca_int.h"
120 #endif
121
122 #include "bta_sys.h"
123
124 #if (BLE_INCLUDED == TRUE)
125 #include "gatt_int.h"
126 #if (SMP_INCLUDED == TRUE)
127 #include "smp_int.h"
128 #endif
129 #include "btm_ble_int.h"
130 #endif
131
132 #ifdef __cplusplus
133 extern "C"
134 {
135 #endif
136
137 BT_API extern void BTE_InitStack(void);
138
139 #ifdef __cplusplus
140 }
141 #endif
142
143 /* Define BTU storage area
144 */
145 #if BTU_DYNAMIC_MEMORY == FALSE
146 tBTU_CB  btu_cb;
147 #endif
148
149 // Communication queue between btu_task and bta.
150 extern fixed_queue_t *btu_bta_msg_queue;
151
152 // Communication queue between btu_task and hci.
153 extern fixed_queue_t *btu_hci_msg_queue;
154
155 // Timer queue between btu_task and bta.
156 extern fixed_queue_t *btu_bta_alarm_queue;
157
158 // General timer queue.
159 extern fixed_queue_t *btu_general_alarm_queue;
160 extern hash_map_t *btu_general_alarm_hash_map;
161 extern pthread_mutex_t btu_general_alarm_lock;
162
163 // Oneshot timer queue.
164 extern fixed_queue_t *btu_oneshot_alarm_queue;
165 extern hash_map_t *btu_oneshot_alarm_hash_map;
166 extern pthread_mutex_t btu_oneshot_alarm_lock;
167
168 // l2cap timer queue.
169 extern fixed_queue_t *btu_l2cap_alarm_queue;
170 extern hash_map_t *btu_l2cap_alarm_hash_map;
171 extern pthread_mutex_t btu_l2cap_alarm_lock;
172
173 /* Define a function prototype to allow a generic timeout handler */
174 typedef void (tUSER_TIMEOUT_FUNC) (TIMER_LIST_ENT *p_tle);
175
176 static void btu_l2cap_alarm_process(TIMER_LIST_ENT *p_tle);
177 static void btu_general_alarm_process(TIMER_LIST_ENT *p_tle);
178
179 static void btu_hci_msg_process(BT_HDR *p_msg) {
180     /* Determine the input message type. */
181     switch (p_msg->event & BT_EVT_MASK)
182     {
183         case BTU_POST_TO_TASK_NO_GOOD_HORRIBLE_HACK: // TODO(zachoverflow): remove this
184             ((post_to_task_hack_t *)(&p_msg->data[0]))->callback(p_msg);
185             break;
186         case BT_EVT_TO_BTU_HCI_ACL:
187             /* All Acl Data goes to L2CAP */
188             l2c_rcv_acl_data (p_msg);
189             break;
190
191         case BT_EVT_TO_BTU_L2C_SEG_XMIT:
192             /* L2CAP segment transmit complete */
193             l2c_link_segments_xmitted (p_msg);
194             break;
195
196         case BT_EVT_TO_BTU_HCI_SCO:
197 #if BTM_SCO_INCLUDED == TRUE
198             btm_route_sco_data (p_msg);
199             break;
200 #endif
201
202         case BT_EVT_TO_BTU_HCI_EVT:
203             btu_hcif_process_event ((UINT8)(p_msg->event & BT_SUB_EVT_MASK), p_msg);
204             GKI_freebuf(p_msg);
205
206 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
207             /* If host receives events which it doesn't response to, */
208             /* host should start idle timer to enter sleep mode.     */
209             btu_check_bt_sleep ();
210 #endif
211             break;
212
213         case BT_EVT_TO_BTU_HCI_CMD:
214             btu_hcif_send_cmd ((UINT8)(p_msg->event & BT_SUB_EVT_MASK), p_msg);
215             break;
216
217 #if (defined(OBX_INCLUDED) && OBX_INCLUDED == TRUE)
218 #if (defined(OBX_SERVER_INCLUDED) && OBX_SERVER_INCLUDED == TRUE)
219         case BT_EVT_TO_OBX_SR_MSG:
220             obx_sr_proc_evt((tOBX_PORT_EVT *)(p_msg + 1));
221             GKI_freebuf (p_msg);
222             break;
223
224         case BT_EVT_TO_OBX_SR_L2C_MSG:
225             obx_sr_proc_l2c_evt((tOBX_L2C_EVT_MSG *)(p_msg + 1));
226             GKI_freebuf (p_msg);
227             break;
228 #endif
229
230 #if (defined(OBX_CLIENT_INCLUDED) && OBX_CLIENT_INCLUDED == TRUE)
231         case BT_EVT_TO_OBX_CL_MSG:
232             obx_cl_proc_evt((tOBX_PORT_EVT *)(p_msg + 1));
233             GKI_freebuf (p_msg);
234             break;
235
236         case BT_EVT_TO_OBX_CL_L2C_MSG:
237             obx_cl_proc_l2c_evt((tOBX_L2C_EVT_MSG *)(p_msg + 1));
238             GKI_freebuf (p_msg);
239             break;
240 #endif
241
242 #if (defined(BIP_INCLUDED) && BIP_INCLUDED == TRUE)
243         case BT_EVT_TO_BIP_CMDS :
244             bip_proc_btu_event(p_msg);
245             GKI_freebuf (p_msg);
246             break;
247 #endif /* BIP */
248 #if (BPP_SND_INCLUDED == TRUE || BPP_INCLUDED == TRUE)
249         case BT_EVT_TO_BPP_PR_CMDS:
250             bpp_pr_proc_event(p_msg);
251             GKI_freebuf (p_msg);
252             break;
253         case BT_EVT_TO_BPP_SND_CMDS:
254             bpp_snd_proc_event(p_msg);
255             GKI_freebuf (p_msg);
256             break;
257
258 #endif /* BPP */
259
260 #endif /* OBX */
261
262 #if (defined(SAP_SERVER_INCLUDED) && SAP_SERVER_INCLUDED == TRUE)
263         case BT_EVT_TO_BTU_SAP :
264             sap_proc_btu_event(p_msg);
265             GKI_freebuf (p_msg);
266             break;
267 #endif /* SAP */
268 #if (defined(GAP_CONN_INCLUDED) && GAP_CONN_INCLUDED == TRUE && GAP_CONN_POST_EVT_INCLUDED == TRUE)
269         case BT_EVT_TO_GAP_MSG :
270             gap_proc_btu_event(p_msg);
271             GKI_freebuf (p_msg);
272             break;
273 #endif
274             // NOTE: The timer calls below may not be sent by HCI.
275         case BT_EVT_TO_START_TIMER :
276             /* Start free running 1 second timer for list management */
277             GKI_start_timer (TIMER_0, GKI_SECS_TO_TICKS (1), TRUE);
278             GKI_freebuf (p_msg);
279             break;
280
281         case BT_EVT_TO_STOP_TIMER:
282             if (GKI_timer_queue_is_empty(&btu_cb.timer_queue)) {
283                 GKI_stop_timer(TIMER_0);
284             }
285             GKI_freebuf (p_msg);
286             break;
287
288                     case BT_EVT_TO_START_TIMER_ONESHOT:
289                         if (!GKI_timer_queue_is_empty(&btu_cb.timer_queue_oneshot)) {
290                             TIMER_LIST_ENT *tle = GKI_timer_getfirst(&btu_cb.timer_queue_oneshot);
291                             // Start non-repeating timer.
292                             GKI_start_timer(TIMER_3, tle->ticks, FALSE);
293                         } else {
294                             BTM_TRACE_WARNING("Oneshot timer queue empty when received start request");
295                         }
296                         GKI_freebuf(p_msg);
297                         break;
298
299                     case BT_EVT_TO_STOP_TIMER_ONESHOT:
300                         if (GKI_timer_queue_is_empty(&btu_cb.timer_queue_oneshot)) {
301                             GKI_stop_timer(TIMER_3);
302                         } else {
303                             BTM_TRACE_WARNING("Oneshot timer queue not empty when received stop request");
304                         }
305                         GKI_freebuf (p_msg);
306                         break;
307
308 #if defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0)
309         case BT_EVT_TO_START_QUICK_TIMER :
310             GKI_start_timer (TIMER_2, QUICK_TIMER_TICKS, TRUE);
311             GKI_freebuf (p_msg);
312             break;
313 #endif
314
315         default:;
316             int i = 0;
317             uint16_t mask = (UINT16) (p_msg->event & BT_EVT_MASK);
318             BOOLEAN handled = FALSE;
319
320             for (; !handled && i < BTU_MAX_REG_EVENT; i++)
321             {
322                 if (btu_cb.event_reg[i].event_cb == NULL)
323                     continue;
324
325                 if (mask == btu_cb.event_reg[i].event_range)
326                 {
327                     if (btu_cb.event_reg[i].event_cb)
328                     {
329                         btu_cb.event_reg[i].event_cb(p_msg);
330                         handled = TRUE;
331                     }
332                 }
333             }
334
335             if (handled == FALSE)
336                 GKI_freebuf (p_msg);
337
338             break;
339     }
340
341 }
342
343 static void btu_bta_alarm_process(TIMER_LIST_ENT *p_tle) {
344     /* call timer callback */
345     if (p_tle->p_cback) {
346         (*p_tle->p_cback)(p_tle);
347     } else if (p_tle->event) {
348         BT_HDR *p_msg;
349         if ((p_msg = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL) {
350             p_msg->event = p_tle->event;
351             p_msg->layer_specific = 0;
352             bta_sys_sendmsg(p_msg);
353         }
354     }
355 }
356
357 /*******************************************************************************
358 **
359 ** Function         btu_task
360 **
361 ** Description      This is the main task of the Bluetooth Upper Layers unit.
362 **                  It sits in a loop waiting for messages, and dispatches them
363 **                  to the appropiate handlers.
364 **
365 ** Returns          should never return
366 **
367 *******************************************************************************/
368 BTU_API void btu_task (void)
369 {
370     UINT16           event;
371     BT_HDR          *p_msg;
372     TIMER_LIST_ENT  *p_tle;
373     UINT8            i;
374     UINT16           mask;
375     BOOLEAN          handled;
376
377     /* wait an event that HCISU is ready */
378     BT_TRACE(TRACE_LAYER_BTU, TRACE_TYPE_API,
379                 "btu_task pending for preload complete event");
380
381     for (;;)
382     {
383         event = GKI_wait (0xFFFF, 0);
384         if (event & EVENT_MASK(GKI_SHUTDOWN_EVT))
385         {
386             /* indicates BT ENABLE abort */
387             BT_TRACE(TRACE_LAYER_BTU, TRACE_TYPE_WARNING,
388                         "btu_task start abort!");
389             return;
390         }
391         else if (event & BT_EVT_PRELOAD_CMPL)
392         {
393             break;
394         }
395         else
396         {
397             BT_TRACE(TRACE_LAYER_BTU, TRACE_TYPE_WARNING,
398                 "btu_task ignore evt %04x while pending for preload complete",
399                 event);
400         }
401     }
402
403     BT_TRACE(TRACE_LAYER_BTU, TRACE_TYPE_API,
404                 "btu_task received preload complete event");
405
406     /* Initialize the mandatory core stack control blocks
407        (BTU, BTM, L2CAP, and SDP)
408      */
409     btu_init_core();
410
411     /* Initialize any optional stack components */
412     BTE_InitStack();
413
414     bta_sys_init();
415
416     /* Initialise platform trace levels at this point as BTE_InitStack() and bta_sys_init()
417      * reset the control blocks and preset the trace level with XXX_INITIAL_TRACE_LEVEL
418      */
419 #if ( BT_USE_TRACES==TRUE )
420     BTE_InitTraceLevels();
421 #endif
422
423     // Inform the bt jni thread initialization is ok.
424     btif_transfer_context(btif_init_ok, 0, NULL, 0, NULL);
425
426     raise_priority_a2dp(TASK_HIGH_BTU);
427
428     /* Wait for, and process, events */
429     for (;;) {
430         event = GKI_wait (0xFFFF, 0);
431
432         // HCI message queue.
433         while (!fixed_queue_is_empty(btu_hci_msg_queue)) {
434             p_msg = (BT_HDR *)fixed_queue_dequeue(btu_hci_msg_queue);
435             btu_hci_msg_process(p_msg);
436         }
437
438         // General alarm queue.
439         while (!fixed_queue_is_empty(btu_general_alarm_queue)) {
440             p_tle = (TIMER_LIST_ENT *)fixed_queue_dequeue(btu_general_alarm_queue);
441             btu_general_alarm_process(p_tle);
442         }
443
444 #if defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0)
445         // L2CAP alarm queue.
446         while (!fixed_queue_is_empty(btu_l2cap_alarm_queue)) {
447             p_tle = (TIMER_LIST_ENT *)fixed_queue_dequeue(btu_l2cap_alarm_queue);
448             btu_l2cap_alarm_process(p_tle);
449         }
450 #endif  // QUICK_TIMER
451
452         // BTA message queue.
453         while (!fixed_queue_is_empty(btu_bta_msg_queue)) {
454             p_msg = (BT_HDR *)fixed_queue_dequeue(btu_bta_msg_queue);
455             bta_sys_event(p_msg);
456         }
457
458         // BTA timer queue.
459         while (!fixed_queue_is_empty(btu_bta_alarm_queue)) {
460             p_tle = (TIMER_LIST_ENT *)fixed_queue_dequeue(btu_bta_alarm_queue);
461             btu_bta_alarm_process(p_tle);
462         }
463
464         while (!fixed_queue_is_empty(btu_oneshot_alarm_queue)) {
465             p_tle = (TIMER_LIST_ENT *)fixed_queue_dequeue(btu_oneshot_alarm_queue);
466             switch (p_tle->event) {
467 #if (defined(BLE_INCLUDED) && BLE_INCLUDED == TRUE)
468                 case BTU_TTYPE_BLE_RANDOM_ADDR:
469                     btm_ble_timeout(p_tle);
470                     break;
471 #endif
472
473                 case BTU_TTYPE_USER_FUNC:
474                     {
475                         tUSER_TIMEOUT_FUNC  *p_uf = (tUSER_TIMEOUT_FUNC *)p_tle->param;
476                         (*p_uf)(p_tle);
477                     }
478                     break;
479
480                 default:
481                     // FAIL
482                     BTM_TRACE_WARNING("Received unexpected oneshot timer event:0x%x\n",
483                         p_tle->event);
484                     break;
485             }
486         }
487
488         if (event & EVENT_MASK(APPL_EVT_7))
489             break;
490     }
491
492     bta_sys_free();
493
494     btu_free_core();
495
496     return;
497 }
498
499 /*******************************************************************************
500 **
501 ** Function         btu_start_timer
502 **
503 ** Description      Start a timer for the specified amount of time.
504 **                  NOTE: The timeout resolution is in SECONDS! (Even
505 **                          though the timer structure field is ticks)
506 **
507 ** Returns          void
508 **
509 *******************************************************************************/
510 static void btu_general_alarm_process(TIMER_LIST_ENT *p_tle) {
511     assert(p_tle != NULL);
512
513     switch (p_tle->event) {
514         case BTU_TTYPE_BTM_DEV_CTL:
515             btm_dev_timeout(p_tle);
516             break;
517
518         case BTU_TTYPE_BTM_ACL:
519             btm_acl_timeout(p_tle);
520             break;
521
522         case BTU_TTYPE_L2CAP_LINK:
523         case BTU_TTYPE_L2CAP_CHNL:
524         case BTU_TTYPE_L2CAP_HOLD:
525         case BTU_TTYPE_L2CAP_INFO:
526         case BTU_TTYPE_L2CAP_FCR_ACK:
527             l2c_process_timeout (p_tle);
528             break;
529
530         case BTU_TTYPE_SDP:
531             sdp_conn_timeout ((tCONN_CB *)p_tle->param);
532             break;
533
534         case BTU_TTYPE_BTM_RMT_NAME:
535             btm_inq_rmt_name_failed();
536             break;
537
538 #if (defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE)
539         case BTU_TTYPE_RFCOMM_MFC:
540         case BTU_TTYPE_RFCOMM_PORT:
541             rfcomm_process_timeout (p_tle);
542             break;
543
544 #endif /* If defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE */
545
546 #if ((defined(BNEP_INCLUDED) && BNEP_INCLUDED == TRUE))
547         case BTU_TTYPE_BNEP:
548             bnep_process_timeout(p_tle);
549             break;
550 #endif
551
552
553 #if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE)
554         case BTU_TTYPE_AVDT_CCB_RET:
555         case BTU_TTYPE_AVDT_CCB_RSP:
556         case BTU_TTYPE_AVDT_CCB_IDLE:
557         case BTU_TTYPE_AVDT_SCB_TC:
558             avdt_process_timeout(p_tle);
559             break;
560 #endif
561
562 #if (defined(OBX_INCLUDED) && OBX_INCLUDED == TRUE)
563 #if (defined(OBX_CLIENT_INCLUDED) && OBX_CLIENT_INCLUDED == TRUE)
564         case BTU_TTYPE_OBX_CLIENT_TO:
565             obx_cl_timeout(p_tle);
566             break;
567 #endif
568 #if (defined(OBX_SERVER_INCLUDED) && OBX_SERVER_INCLUDED == TRUE)
569         case BTU_TTYPE_OBX_SERVER_TO:
570             obx_sr_timeout(p_tle);
571             break;
572
573         case BTU_TTYPE_OBX_SVR_SESS_TO:
574             obx_sr_sess_timeout(p_tle);
575             break;
576 #endif
577 #endif
578
579 #if (defined(SAP_SERVER_INCLUDED) && SAP_SERVER_INCLUDED == TRUE)
580         case BTU_TTYPE_SAP_TO:
581             sap_process_timeout(p_tle);
582             break;
583 #endif
584
585 #if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE)
586         case BTU_TTYPE_HID_HOST_REPAGE_TO :
587             hidh_proc_repage_timeout(p_tle);
588             break;
589 #endif
590
591 #if (defined(BLE_INCLUDED) && BLE_INCLUDED == TRUE)
592         case BTU_TTYPE_BLE_INQUIRY:
593         case BTU_TTYPE_BLE_GAP_LIM_DISC:
594         case BTU_TTYPE_BLE_RANDOM_ADDR:
595         case BTU_TTYPE_BLE_GAP_FAST_ADV:
596         case BTU_TTYPE_BLE_OBSERVE:
597             btm_ble_timeout(p_tle);
598             break;
599
600         case BTU_TTYPE_ATT_WAIT_FOR_RSP:
601             gatt_rsp_timeout(p_tle);
602             break;
603
604         case BTU_TTYPE_ATT_WAIT_FOR_IND_ACK:
605             gatt_ind_ack_timeout(p_tle);
606             break;
607 #if (defined(SMP_INCLUDED) && SMP_INCLUDED == TRUE)
608         case BTU_TTYPE_SMP_PAIRING_CMD:
609             smp_rsp_timeout(p_tle);
610             break;
611 #endif
612
613 #endif
614
615 #if (MCA_INCLUDED == TRUE)
616         case BTU_TTYPE_MCA_CCB_RSP:
617             mca_process_timeout(p_tle);
618             break;
619 #endif
620         case BTU_TTYPE_USER_FUNC:
621             {
622                 tUSER_TIMEOUT_FUNC  *p_uf = (tUSER_TIMEOUT_FUNC *)p_tle->param;
623                 (*p_uf)(p_tle);
624             }
625             break;
626
627         default:;
628                 int i = 0;
629                 BOOLEAN handled = FALSE;
630
631                 for (; !handled && i < BTU_MAX_REG_TIMER; i++)
632                 {
633                     if (btu_cb.timer_reg[i].timer_cb == NULL)
634                         continue;
635                     if (btu_cb.timer_reg[i].p_tle == p_tle)
636                     {
637                         btu_cb.timer_reg[i].timer_cb(p_tle);
638                         handled = TRUE;
639                     }
640                 }
641                 break;
642     }
643 }
644
645 void btu_general_alarm_cb(void *data) {
646   assert(data != NULL);
647   TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;
648
649   fixed_queue_enqueue(btu_general_alarm_queue, p_tle);
650   GKI_send_event(BTU_TASK, TIMER_0_EVT_MASK);
651 }
652
653 void btu_start_timer(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_sec) {
654   assert(p_tle != NULL);
655
656   // Get the alarm for the timer list entry.
657   pthread_mutex_lock(&btu_general_alarm_lock);
658   if (!hash_map_has_key(btu_general_alarm_hash_map, p_tle)) {
659     hash_map_set(btu_general_alarm_hash_map, p_tle, alarm_new());
660   }
661   pthread_mutex_unlock(&btu_general_alarm_lock);
662
663   alarm_t *alarm = hash_map_get(btu_general_alarm_hash_map, p_tle);
664   if (alarm == NULL) {
665     ALOGE("%s Unable to create alarm\n", __func__);
666     return;
667   }
668   alarm_cancel(alarm);
669
670   p_tle->event = type;
671   // NOTE: This value is in seconds but stored in a ticks field.
672   p_tle->ticks = timeout_sec;
673   if (p_tle->in_use == TRUE)
674     ALOGW("%s Starting alarm already in use\n", __func__);
675   p_tle->in_use = TRUE;
676   alarm_set(alarm, (period_ms_t)(timeout_sec * 1000), btu_general_alarm_cb, (void *)p_tle);
677 }
678
679 /*******************************************************************************
680 **
681 ** Function         btu_remaining_time
682 **
683 ** Description      Return amount of time to expire
684 **
685 ** Returns          time in second
686 **
687 *******************************************************************************/
688 UINT32 btu_remaining_time (TIMER_LIST_ENT *p_tle)
689 {
690     return(GKI_get_remaining_ticks (&btu_cb.timer_queue, p_tle));
691 }
692
693 /*******************************************************************************
694 **
695 ** Function         btu_stop_timer
696 **
697 ** Description      Stop a timer.
698 **
699 ** Returns          void
700 **
701 *******************************************************************************/
702 void btu_stop_timer(TIMER_LIST_ENT *p_tle) {
703   assert(p_tle != NULL);
704
705   if (p_tle->in_use == FALSE)
706     return;
707   p_tle->in_use = FALSE;
708
709   // Get the alarm for the timer list entry.
710   alarm_t *alarm = hash_map_get(btu_general_alarm_hash_map, p_tle);
711   if (alarm == NULL) {
712     ALOGW("%s Unable to find expected alarm in hashmap\n", __func__);
713     return;
714   }
715   alarm_cancel(alarm);
716 }
717
718 #if defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0)
719 /*******************************************************************************
720 **
721 ** Function         btu_start_quick_timer
722 **
723 ** Description      Start a timer for the specified amount of time in ticks.
724 **
725 ** Returns          void
726 **
727 *******************************************************************************/
728 static void btu_l2cap_alarm_process(TIMER_LIST_ENT *p_tle) {
729   assert(p_tle != NULL);
730
731   switch (p_tle->event) {
732     case BTU_TTYPE_L2CAP_CHNL:      /* monitor or retransmission timer */
733     case BTU_TTYPE_L2CAP_FCR_ACK:   /* ack timer */
734       l2c_process_timeout (p_tle);
735       break;
736
737     default:
738       break;
739   }
740 }
741
742 static void btu_l2cap_alarm_cb(void *data) {
743   assert(data != NULL);
744   TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;
745
746   fixed_queue_enqueue(btu_l2cap_alarm_queue, p_tle);
747   GKI_send_event(BTU_TASK, TIMER_2_EVT_MASK);
748 }
749
750 void btu_start_quick_timer(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_ticks) {
751   assert(p_tle != NULL);
752
753   // Get the alarm for the timer list entry.
754   pthread_mutex_lock(&btu_l2cap_alarm_lock);
755   if (!hash_map_has_key(btu_l2cap_alarm_hash_map, p_tle)) {
756     hash_map_set(btu_l2cap_alarm_hash_map, p_tle, alarm_new());
757   }
758   pthread_mutex_unlock(&btu_l2cap_alarm_lock);
759
760   alarm_t *alarm = hash_map_get(btu_l2cap_alarm_hash_map, p_tle);
761   if (alarm == NULL) {
762     ALOGE("%s Unable to create alarm\n", __func__);
763     return;
764   }
765   alarm_cancel(alarm);
766
767   p_tle->event = type;
768   p_tle->ticks = timeout_ticks;
769   if (p_tle->in_use == TRUE)
770     ALOGW("%s Starting alarm already in use\n", __func__);
771   p_tle->in_use = TRUE;
772   // The quick timer ticks are 100ms long.
773   alarm_set(alarm, (period_ms_t)(timeout_ticks * 100), btu_l2cap_alarm_cb, (void *)p_tle);
774 }
775
776 /*******************************************************************************
777 **
778 ** Function         btu_stop_quick_timer
779 **
780 ** Description      Stop a timer.
781 **
782 ** Returns          void
783 **
784 *******************************************************************************/
785 void btu_stop_quick_timer(TIMER_LIST_ENT *p_tle) {
786   assert(p_tle != NULL);
787
788   if (p_tle->in_use == FALSE)
789     return;
790   p_tle->in_use = FALSE;
791
792   // Get the alarm for the timer list entry.
793   alarm_t *alarm = hash_map_get(btu_l2cap_alarm_hash_map, p_tle);
794   if (alarm == NULL) {
795     ALOGW("%s Unable to find expected alarm in hashmap\n", __func__);
796     return;
797   }
798   alarm_cancel(alarm);
799 }
800 #endif /* defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0) */
801
802 void btu_oneshot_alarm_cb(void *data) {
803   assert(data != NULL);
804   TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;
805
806   btu_stop_timer_oneshot(p_tle);
807
808   fixed_queue_enqueue(btu_oneshot_alarm_queue, p_tle);
809   GKI_send_event(BTU_TASK, TIMER_3_EVT_MASK);
810 }
811
812 /*
813  * Starts a oneshot timer with a timeout in seconds.
814  */
815 void btu_start_timer_oneshot(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_sec) {
816   assert(p_tle != NULL);
817
818   // Get the alarm for the timer list entry.
819   pthread_mutex_lock(&btu_oneshot_alarm_lock);
820   if (!hash_map_has_key(btu_oneshot_alarm_hash_map, p_tle)) {
821     hash_map_set(btu_oneshot_alarm_hash_map, p_tle, alarm_new());
822   }
823   pthread_mutex_unlock(&btu_oneshot_alarm_lock);
824
825   alarm_t *alarm = hash_map_get(btu_oneshot_alarm_hash_map, p_tle);
826   if (alarm == NULL) {
827     ALOGE("%s Unable to create alarm\n", __func__);
828     return;
829   }
830   alarm_cancel(alarm);
831
832   p_tle->event = type;
833   if (p_tle->in_use == TRUE)
834     ALOGW("%s Starting alarm already in use\n", __func__);
835   p_tle->in_use = TRUE;
836   // NOTE: This value is in seconds but stored in a ticks field.
837   p_tle->ticks = timeout_sec;
838   alarm_set(alarm, (period_ms_t)(timeout_sec * 1000), btu_oneshot_alarm_cb, (void *)p_tle);
839 }
840
841 void btu_stop_timer_oneshot(TIMER_LIST_ENT *p_tle) {
842   assert(p_tle != NULL);
843
844   if (p_tle->in_use == FALSE)
845     return;
846   p_tle->in_use = FALSE;
847
848   // Get the alarm for the timer list entry.
849   alarm_t *alarm = hash_map_get(btu_oneshot_alarm_hash_map, p_tle);
850   if (alarm == NULL) {
851     ALOGW("%s Unable to find expected alarm in hashmap\n", __func__);
852     return;
853   }
854   alarm_cancel(alarm);
855 }
856
857 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
858 /*******************************************************************************
859 **
860 ** Function         btu_check_bt_sleep
861 **
862 ** Description      This function is called to check if controller can go to sleep.
863 **
864 ** Returns          void
865 **
866 *******************************************************************************/
867 void btu_check_bt_sleep (void)
868 {
869     if ((GKI_queue_is_empty(&btu_cb.hci_cmd_cb[LOCAL_BR_EDR_CONTROLLER_ID].cmd_cmpl_q)
870         && GKI_queue_is_empty(&btu_cb.hci_cmd_cb[LOCAL_BR_EDR_CONTROLLER_ID].cmd_xmit_q)))
871     {
872         if (l2cb.controller_xmit_window == l2cb.num_lm_acl_bufs)
873         {
874             /* enable dev to sleep  in the cmd cplt and cmd status only and num cplt packet */
875             HCI_LP_ALLOW_BT_DEVICE_SLEEP();
876         }
877     }
878 }
879 #endif