summaryrefslogtreecommitdiffstats
path: root/stack/btu/btu_task.c
blob: 745136ee74be19a8c74ed1a951566e7d54723da0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
/******************************************************************************
 *
 *  Copyright (C) 1999-2012 Broadcom Corporation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

#define LOG_TAG "btu_task"

#include <assert.h>
#include <cutils/log.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "alarm.h"
#include "bt_target.h"
#include "bt_trace.h"
#include "bt_types.h"
#include "bt_utils.h"
#include "btif_common.h"
#include "btm_api.h"
#include "btm_int.h"
#include "btu.h"
#include "fixed_queue.h"
#include "future.h"
#include "gki.h"
#include "hash_map.h"
#include "hcimsgs.h"
#include "l2c_int.h"
#include "module.h"
#include "osi.h"
#include "sdpint.h"
#include "thread.h"

#if ( defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE )
#include "port_api.h"
#include "port_ext.h"
#endif

#if (defined(EVAL) && EVAL == TRUE)
#include "btu_eval.h"
#endif

#if GAP_INCLUDED == TRUE
#include "gap_int.h"
#endif

#if (defined(OBX_INCLUDED) && OBX_INCLUDED == TRUE)
#include "obx_int.h"

#if (defined(BIP_INCLUDED) && BIP_INCLUDED == TRUE)
#include "bip_int.h"
#endif /* BIP */

#if (BPP_SND_INCLUDED == TRUE ||  BPP_INCLUDED == TRUE)
#include "bpp_int.h"
#endif /* BPP */

#endif /* OBX */

/* BTE application task */
#if APPL_INCLUDED == TRUE
#include "bte_appl.h"
#endif

#if (defined(BNEP_INCLUDED) && BNEP_INCLUDED == TRUE)
#include "bnep_int.h"
#endif

#if (defined(PAN_INCLUDED) && PAN_INCLUDED == TRUE)
#include "pan_int.h"
#endif

#if (defined(SAP_SERVER_INCLUDED) && SAP_SERVER_INCLUDED == TRUE)
#include "sap_int.h"
#endif

#if (defined(HID_DEV_INCLUDED) && HID_DEV_INCLUDED == TRUE )
#include "hidd_int.h"
#endif

#if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE )
#include "hidh_int.h"
#endif

#if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE)
#include "avdt_int.h"
#else
extern void avdt_rcv_sync_info (BT_HDR *p_buf); /* this is for hci_test */
#endif

#if (defined(MCA_INCLUDED) && MCA_INCLUDED == TRUE)
#include "mca_api.h"
#include "mca_defs.h"
#include "mca_int.h"
#endif

#include "bta_sys.h"

#if (BLE_INCLUDED == TRUE)
#include "gatt_int.h"
#if (SMP_INCLUDED == TRUE)
#include "smp_int.h"
#endif
#include "btm_ble_int.h"
#endif

#ifdef __cplusplus
extern "C"
{
#endif

extern void BTE_InitStack(void);

#ifdef __cplusplus
}
#endif

/* Define BTU storage area
*/
#if BTU_DYNAMIC_MEMORY == FALSE
tBTU_CB  btu_cb;
#endif

// Communication queue between btu_task and bta.
extern fixed_queue_t *btu_bta_msg_queue;

// Communication queue between btu_task and hci.
extern fixed_queue_t *btu_hci_msg_queue;

// Timer queue between btu_task and bta.
extern fixed_queue_t *btu_bta_alarm_queue;

// General timer queue.
extern fixed_queue_t *btu_general_alarm_queue;
extern hash_map_t *btu_general_alarm_hash_map;
extern pthread_mutex_t btu_general_alarm_lock;

// Oneshot timer queue.
extern fixed_queue_t *btu_oneshot_alarm_queue;
extern hash_map_t *btu_oneshot_alarm_hash_map;
extern pthread_mutex_t btu_oneshot_alarm_lock;

// l2cap timer queue.
extern fixed_queue_t *btu_l2cap_alarm_queue;
extern hash_map_t *btu_l2cap_alarm_hash_map;
extern pthread_mutex_t btu_l2cap_alarm_lock;

extern fixed_queue_t *event_queue;
extern fixed_queue_t *btif_msg_queue;

extern thread_t *bt_workqueue_thread;

/* Define a function prototype to allow a generic timeout handler */
typedef void (tUSER_TIMEOUT_FUNC) (TIMER_LIST_ENT *p_tle);

static void btu_l2cap_alarm_process(TIMER_LIST_ENT *p_tle);
static void btu_general_alarm_process(TIMER_LIST_ENT *p_tle);
static void btu_bta_alarm_process(TIMER_LIST_ENT *p_tle);
static void btu_hci_msg_process(BT_HDR *p_msg);

void btu_hci_msg_ready(fixed_queue_t *queue, UNUSED_ATTR void *context) {
    BT_HDR *p_msg = (BT_HDR *)fixed_queue_dequeue(queue);
    btu_hci_msg_process(p_msg);
}

void btu_general_alarm_ready(fixed_queue_t *queue, UNUSED_ATTR void *context) {
    TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)fixed_queue_dequeue(queue);
    btu_general_alarm_process(p_tle);
}

void btu_oneshot_alarm_ready(fixed_queue_t *queue, UNUSED_ATTR void *context) {
    TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)fixed_queue_dequeue(queue);
    btu_general_alarm_process(p_tle);

    switch (p_tle->event) {
#if (defined(BLE_INCLUDED) && BLE_INCLUDED == TRUE)
        case BTU_TTYPE_BLE_RANDOM_ADDR:
            btm_ble_timeout(p_tle);
            break;
#endif

        case BTU_TTYPE_USER_FUNC:
            {
                tUSER_TIMEOUT_FUNC  *p_uf = (tUSER_TIMEOUT_FUNC *)p_tle->param;
                (*p_uf)(p_tle);
            }
            break;

        default:
            // FAIL
            BTM_TRACE_WARNING("Received unexpected oneshot timer event:0x%x\n",
                p_tle->event);
            break;
    }
}

void btu_l2cap_alarm_ready(fixed_queue_t *queue, UNUSED_ATTR void *context) {
    TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)fixed_queue_dequeue(queue);
    btu_l2cap_alarm_process(p_tle);
}

void btu_bta_msg_ready(fixed_queue_t *queue, UNUSED_ATTR void *context) {
    BT_HDR *p_msg = (BT_HDR *)fixed_queue_dequeue(queue);
    bta_sys_event(p_msg);
}

void btu_bta_alarm_ready(fixed_queue_t *queue, UNUSED_ATTR void *context) {
    TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)fixed_queue_dequeue(queue);
    btu_bta_alarm_process(p_tle);
}

static void btu_hci_msg_process(BT_HDR *p_msg) {
    /* Determine the input message type. */
    switch (p_msg->event & BT_EVT_MASK)
    {
        case BTU_POST_TO_TASK_NO_GOOD_HORRIBLE_HACK: // TODO(zachoverflow): remove this
            ((post_to_task_hack_t *)(&p_msg->data[0]))->callback(p_msg);
            break;
        case BT_EVT_TO_BTU_HCI_ACL:
            /* All Acl Data goes to L2CAP */
            l2c_rcv_acl_data (p_msg);
            break;

        case BT_EVT_TO_BTU_L2C_SEG_XMIT:
            /* L2CAP segment transmit complete */
            l2c_link_segments_xmitted (p_msg);
            break;

        case BT_EVT_TO_BTU_HCI_SCO:
#if BTM_SCO_INCLUDED == TRUE
            btm_route_sco_data (p_msg);
            break;
#endif

        case BT_EVT_TO_BTU_HCI_EVT:
            btu_hcif_process_event ((UINT8)(p_msg->event & BT_SUB_EVT_MASK), p_msg);
            GKI_freebuf(p_msg);

#if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
            /* If host receives events which it doesn't response to, */
            /* host should start idle timer to enter sleep mode.     */
            btu_check_bt_sleep ();
#endif
            break;

        case BT_EVT_TO_BTU_HCI_CMD:
            btu_hcif_send_cmd ((UINT8)(p_msg->event & BT_SUB_EVT_MASK), p_msg);
            break;

#if (defined(OBX_INCLUDED) && OBX_INCLUDED == TRUE)
#if (defined(OBX_SERVER_INCLUDED) && OBX_SERVER_INCLUDED == TRUE)
        case BT_EVT_TO_OBX_SR_MSG:
            obx_sr_proc_evt((tOBX_PORT_EVT *)(p_msg + 1));
            GKI_freebuf (p_msg);
            break;

        case BT_EVT_TO_OBX_SR_L2C_MSG:
            obx_sr_proc_l2c_evt((tOBX_L2C_EVT_MSG *)(p_msg + 1));
            GKI_freebuf (p_msg);
            break;
#endif

#if (defined(OBX_CLIENT_INCLUDED) && OBX_CLIENT_INCLUDED == TRUE)
        case BT_EVT_TO_OBX_CL_MSG:
            obx_cl_proc_evt((tOBX_PORT_EVT *)(p_msg + 1));
            GKI_freebuf (p_msg);
            break;

        case BT_EVT_TO_OBX_CL_L2C_MSG:
            obx_cl_proc_l2c_evt((tOBX_L2C_EVT_MSG *)(p_msg + 1));
            GKI_freebuf (p_msg);
            break;
#endif

#if (defined(BIP_INCLUDED) && BIP_INCLUDED == TRUE)
        case BT_EVT_TO_BIP_CMDS :
            bip_proc_btu_event(p_msg);
            GKI_freebuf (p_msg);
            break;
#endif /* BIP */
#if (BPP_SND_INCLUDED == TRUE || BPP_INCLUDED == TRUE)
        case BT_EVT_TO_BPP_PR_CMDS:
            bpp_pr_proc_event(p_msg);
            GKI_freebuf (p_msg);
            break;
        case BT_EVT_TO_BPP_SND_CMDS:
            bpp_snd_proc_event(p_msg);
            GKI_freebuf (p_msg);
            break;

#endif /* BPP */

#endif /* OBX */

#if (defined(SAP_SERVER_INCLUDED) && SAP_SERVER_INCLUDED == TRUE)
        case BT_EVT_TO_BTU_SAP :
            sap_proc_btu_event(p_msg);
            GKI_freebuf (p_msg);
            break;
#endif /* SAP */
            // NOTE: The timer calls below may not be sent by HCI.
        case BT_EVT_TO_START_TIMER :
            /* Start free running 1 second timer for list management */
            GKI_start_timer (TIMER_0, GKI_SECS_TO_TICKS (1), TRUE);
            GKI_freebuf (p_msg);
            break;

        case BT_EVT_TO_STOP_TIMER:
            if (GKI_timer_queue_is_empty(&btu_cb.timer_queue)) {
                GKI_stop_timer(TIMER_0);
            }
            GKI_freebuf (p_msg);
            break;

                    case BT_EVT_TO_START_TIMER_ONESHOT:
                        if (!GKI_timer_queue_is_empty(&btu_cb.timer_queue_oneshot)) {
                            TIMER_LIST_ENT *tle = GKI_timer_getfirst(&btu_cb.timer_queue_oneshot);
                            // Start non-repeating timer.
                            GKI_start_timer(TIMER_3, tle->ticks, FALSE);
                        } else {
                            BTM_TRACE_WARNING("Oneshot timer queue empty when received start request");
                        }
                        GKI_freebuf(p_msg);
                        break;

                    case BT_EVT_TO_STOP_TIMER_ONESHOT:
                        if (GKI_timer_queue_is_empty(&btu_cb.timer_queue_oneshot)) {
                            GKI_stop_timer(TIMER_3);
                        } else {
                            BTM_TRACE_WARNING("Oneshot timer queue not empty when received stop request");
                        }
                        GKI_freebuf (p_msg);
                        break;

#if defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0)
        case BT_EVT_TO_START_QUICK_TIMER :
            GKI_start_timer (TIMER_2, QUICK_TIMER_TICKS, TRUE);
            GKI_freebuf (p_msg);
            break;
#endif

        default:;
            int i = 0;
            uint16_t mask = (UINT16) (p_msg->event & BT_EVT_MASK);
            BOOLEAN handled = FALSE;

            for (; !handled && i < BTU_MAX_REG_EVENT; i++)
            {
                if (btu_cb.event_reg[i].event_cb == NULL)
                    continue;

                if (mask == btu_cb.event_reg[i].event_range)
                {
                    if (btu_cb.event_reg[i].event_cb)
                    {
                        btu_cb.event_reg[i].event_cb(p_msg);
                        handled = TRUE;
                    }
                }
            }

            if (handled == FALSE)
                GKI_freebuf (p_msg);

            break;
    }

}

static void btu_bta_alarm_process(TIMER_LIST_ENT *p_tle) {
    /* call timer callback */
    if (p_tle->p_cback) {
        (*p_tle->p_cback)(p_tle);
    } else if (p_tle->event) {
        BT_HDR *p_msg;
        if ((p_msg = (BT_HDR *) GKI_getbuf(sizeof(BT_HDR))) != NULL) {
            p_msg->event = p_tle->event;
            p_msg->layer_specific = 0;
            bta_sys_sendmsg(p_msg);
        }
    }
}

void btu_task_start_up(UNUSED_ATTR void *context) {
  BT_TRACE(TRACE_LAYER_BTU, TRACE_TYPE_API,
      "btu_task pending for preload complete event");

  ALOGI("Bluetooth chip preload is complete");

  BT_TRACE(TRACE_LAYER_BTU, TRACE_TYPE_API,
      "btu_task received preload complete event");

  /* Initialize the mandatory core stack control blocks
     (BTU, BTM, L2CAP, and SDP)
   */
  btu_init_core();

  /* Initialize any optional stack components */
  BTE_InitStack();

  bta_sys_init();

  /* Initialise platform trace levels at this point as BTE_InitStack() and bta_sys_init()
   * reset the control blocks and preset the trace level with XXX_INITIAL_TRACE_LEVEL
   */
#if ( BT_USE_TRACES==TRUE )
  module_init(get_module(BTE_LOGMSG_MODULE));
#endif

  // Inform the bt jni thread initialization is ok.
  btif_transfer_context(btif_init_ok, 0, NULL, 0, NULL);

  fixed_queue_register_dequeue(btu_bta_msg_queue,
      thread_get_reactor(bt_workqueue_thread),
      btu_bta_msg_ready,
      NULL);

  fixed_queue_register_dequeue(btu_hci_msg_queue,
      thread_get_reactor(bt_workqueue_thread),
      btu_hci_msg_ready,
      NULL);

  fixed_queue_register_dequeue(btu_general_alarm_queue,
      thread_get_reactor(bt_workqueue_thread),
      btu_general_alarm_ready,
      NULL);

  fixed_queue_register_dequeue(btu_oneshot_alarm_queue,
      thread_get_reactor(bt_workqueue_thread),
      btu_oneshot_alarm_ready,
      NULL);

  fixed_queue_register_dequeue(btu_l2cap_alarm_queue,
      thread_get_reactor(bt_workqueue_thread),
      btu_l2cap_alarm_ready,
      NULL);
}

void btu_task_shut_down(UNUSED_ATTR void *context) {
  fixed_queue_unregister_dequeue(btu_bta_msg_queue);
  fixed_queue_unregister_dequeue(btu_hci_msg_queue);
  fixed_queue_unregister_dequeue(btu_general_alarm_queue);
  fixed_queue_unregister_dequeue(btu_oneshot_alarm_queue);
  fixed_queue_unregister_dequeue(btu_l2cap_alarm_queue);

#if ( BT_USE_TRACES==TRUE )
  module_clean_up(get_module(BTE_LOGMSG_MODULE));
#endif

  bta_sys_free();
  btu_free_core();
}

/*******************************************************************************
**
** Function         btu_start_timer
**
** Description      Start a timer for the specified amount of time.
**                  NOTE: The timeout resolution is in SECONDS! (Even
**                          though the timer structure field is ticks)
**
** Returns          void
**
*******************************************************************************/
static void btu_general_alarm_process(TIMER_LIST_ENT *p_tle) {
    assert(p_tle != NULL);

    switch (p_tle->event) {
        case BTU_TTYPE_BTM_DEV_CTL:
            btm_dev_timeout(p_tle);
            break;

        case BTU_TTYPE_BTM_ACL:
            btm_acl_timeout(p_tle);
            break;

        case BTU_TTYPE_L2CAP_LINK:
        case BTU_TTYPE_L2CAP_CHNL:
        case BTU_TTYPE_L2CAP_HOLD:
        case BTU_TTYPE_L2CAP_INFO:
        case BTU_TTYPE_L2CAP_FCR_ACK:
            l2c_process_timeout (p_tle);
            break;

        case BTU_TTYPE_SDP:
            sdp_conn_timeout ((tCONN_CB *)p_tle->param);
            break;

        case BTU_TTYPE_BTM_RMT_NAME:
            btm_inq_rmt_name_failed();
            break;

#if (defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE)
        case BTU_TTYPE_RFCOMM_MFC:
        case BTU_TTYPE_RFCOMM_PORT:
            rfcomm_process_timeout (p_tle);
            break;

#endif /* If defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE */

#if ((defined(BNEP_INCLUDED) && BNEP_INCLUDED == TRUE))
        case BTU_TTYPE_BNEP:
            bnep_process_timeout(p_tle);
            break;
#endif


#if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE)
        case BTU_TTYPE_AVDT_CCB_RET:
        case BTU_TTYPE_AVDT_CCB_RSP:
        case BTU_TTYPE_AVDT_CCB_IDLE:
        case BTU_TTYPE_AVDT_SCB_TC:
            avdt_process_timeout(p_tle);
            break;
#endif

#if (defined(OBX_INCLUDED) && OBX_INCLUDED == TRUE)
#if (defined(OBX_CLIENT_INCLUDED) && OBX_CLIENT_INCLUDED == TRUE)
        case BTU_TTYPE_OBX_CLIENT_TO:
            obx_cl_timeout(p_tle);
            break;
#endif
#if (defined(OBX_SERVER_INCLUDED) && OBX_SERVER_INCLUDED == TRUE)
        case BTU_TTYPE_OBX_SERVER_TO:
            obx_sr_timeout(p_tle);
            break;

        case BTU_TTYPE_OBX_SVR_SESS_TO:
            obx_sr_sess_timeout(p_tle);
            break;
#endif
#endif

#if (defined(SAP_SERVER_INCLUDED) && SAP_SERVER_INCLUDED == TRUE)
        case BTU_TTYPE_SAP_TO:
            sap_process_timeout(p_tle);
            break;
#endif

#if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE)
        case BTU_TTYPE_HID_HOST_REPAGE_TO :
            hidh_proc_repage_timeout(p_tle);
            break;
#endif

#if (defined(BLE_INCLUDED) && BLE_INCLUDED == TRUE)
        case BTU_TTYPE_BLE_INQUIRY:
        case BTU_TTYPE_BLE_GAP_LIM_DISC:
        case BTU_TTYPE_BLE_RANDOM_ADDR:
        case BTU_TTYPE_BLE_GAP_FAST_ADV:
        case BTU_TTYPE_BLE_OBSERVE:
            btm_ble_timeout(p_tle);
            break;

        case BTU_TTYPE_ATT_WAIT_FOR_RSP:
            gatt_rsp_timeout(p_tle);
            break;

        case BTU_TTYPE_ATT_WAIT_FOR_IND_ACK:
            gatt_ind_ack_timeout(p_tle);
            break;
#if (defined(SMP_INCLUDED) && SMP_INCLUDED == TRUE)
        case BTU_TTYPE_SMP_PAIRING_CMD:
            smp_rsp_timeout(p_tle);
            break;
#endif

#endif

#if (MCA_INCLUDED == TRUE)
        case BTU_TTYPE_MCA_CCB_RSP:
            mca_process_timeout(p_tle);
            break;
#endif
        case BTU_TTYPE_USER_FUNC:
            {
                tUSER_TIMEOUT_FUNC  *p_uf = (tUSER_TIMEOUT_FUNC *)p_tle->param;
                (*p_uf)(p_tle);
            }
            break;

        default:;
                int i = 0;
                BOOLEAN handled = FALSE;

                for (; !handled && i < BTU_MAX_REG_TIMER; i++)
                {
                    if (btu_cb.timer_reg[i].timer_cb == NULL)
                        continue;
                    if (btu_cb.timer_reg[i].p_tle == p_tle)
                    {
                        btu_cb.timer_reg[i].timer_cb(p_tle);
                        handled = TRUE;
                    }
                }
                break;
    }
}

void btu_general_alarm_cb(void *data) {
  assert(data != NULL);
  TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;

  fixed_queue_enqueue(btu_general_alarm_queue, p_tle);
  GKI_send_event(BTU_TASK, TIMER_0_EVT_MASK);
}

void btu_start_timer(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_sec) {
  assert(p_tle != NULL);

  // Get the alarm for the timer list entry.
  pthread_mutex_lock(&btu_general_alarm_lock);
  if (!hash_map_has_key(btu_general_alarm_hash_map, p_tle)) {
    hash_map_set(btu_general_alarm_hash_map, p_tle, alarm_new());
  }
  pthread_mutex_unlock(&btu_general_alarm_lock);

  alarm_t *alarm = hash_map_get(btu_general_alarm_hash_map, p_tle);
  if (alarm == NULL) {
    ALOGE("%s Unable to create alarm\n", __func__);
    return;
  }
  alarm_cancel(alarm);

  p_tle->event = type;
  // NOTE: This value is in seconds but stored in a ticks field.
  p_tle->ticks = timeout_sec;
  if (p_tle->in_use == TRUE)
    ALOGW("%s Starting alarm already in use\n", __func__);
  p_tle->in_use = TRUE;
  alarm_set(alarm, (period_ms_t)(timeout_sec * 1000), btu_general_alarm_cb, (void *)p_tle);
}

/*******************************************************************************
**
** Function         btu_remaining_time
**
** Description      Return amount of time to expire
**
** Returns          time in second
**
*******************************************************************************/
UINT32 btu_remaining_time (TIMER_LIST_ENT *p_tle)
{
    return(GKI_get_remaining_ticks (&btu_cb.timer_queue, p_tle));
}

/*******************************************************************************
**
** Function         btu_stop_timer
**
** Description      Stop a timer.
**
** Returns          void
**
*******************************************************************************/
void btu_stop_timer(TIMER_LIST_ENT *p_tle) {
  assert(p_tle != NULL);

  if (p_tle->in_use == FALSE)
    return;
  p_tle->in_use = FALSE;

  // Get the alarm for the timer list entry.
  alarm_t *alarm = hash_map_get(btu_general_alarm_hash_map, p_tle);
  if (alarm == NULL) {
    ALOGW("%s Unable to find expected alarm in hashmap\n", __func__);
    return;
  }
  alarm_cancel(alarm);
}

#if defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0)
/*******************************************************************************
**
** Function         btu_start_quick_timer
**
** Description      Start a timer for the specified amount of time in ticks.
**
** Returns          void
**
*******************************************************************************/
static void btu_l2cap_alarm_process(TIMER_LIST_ENT *p_tle) {
  assert(p_tle != NULL);

  switch (p_tle->event) {
    case BTU_TTYPE_L2CAP_CHNL:      /* monitor or retransmission timer */
    case BTU_TTYPE_L2CAP_FCR_ACK:   /* ack timer */
      l2c_process_timeout (p_tle);
      break;

    default:
      break;
  }
}

static void btu_l2cap_alarm_cb(void *data) {
  assert(data != NULL);
  TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;

  fixed_queue_enqueue(btu_l2cap_alarm_queue, p_tle);
  GKI_send_event(BTU_TASK, TIMER_2_EVT_MASK);
}

void btu_start_quick_timer(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_ticks) {
  assert(p_tle != NULL);

  // Get the alarm for the timer list entry.
  pthread_mutex_lock(&btu_l2cap_alarm_lock);
  if (!hash_map_has_key(btu_l2cap_alarm_hash_map, p_tle)) {
    hash_map_set(btu_l2cap_alarm_hash_map, p_tle, alarm_new());
  }
  pthread_mutex_unlock(&btu_l2cap_alarm_lock);

  alarm_t *alarm = hash_map_get(btu_l2cap_alarm_hash_map, p_tle);
  if (alarm == NULL) {
    ALOGE("%s Unable to create alarm\n", __func__);
    return;
  }
  alarm_cancel(alarm);

  p_tle->event = type;
  p_tle->ticks = timeout_ticks;
  if (p_tle->in_use == TRUE)
    ALOGW("%s Starting alarm already in use\n", __func__);
  p_tle->in_use = TRUE;
  // The quick timer ticks are 100ms long.
  alarm_set(alarm, (period_ms_t)(timeout_ticks * 100), btu_l2cap_alarm_cb, (void *)p_tle);
}

/*******************************************************************************
**
** Function         btu_stop_quick_timer
**
** Description      Stop a timer.
**
** Returns          void
**
*******************************************************************************/
void btu_stop_quick_timer(TIMER_LIST_ENT *p_tle) {
  assert(p_tle != NULL);

  if (p_tle->in_use == FALSE)
    return;
  p_tle->in_use = FALSE;

  // Get the alarm for the timer list entry.
  alarm_t *alarm = hash_map_get(btu_l2cap_alarm_hash_map, p_tle);
  if (alarm == NULL) {
    ALOGW("%s Unable to find expected alarm in hashmap\n", __func__);
    return;
  }
  alarm_cancel(alarm);
}
#endif /* defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0) */

void btu_oneshot_alarm_cb(void *data) {
  assert(data != NULL);
  TIMER_LIST_ENT *p_tle = (TIMER_LIST_ENT *)data;

  btu_stop_timer_oneshot(p_tle);

  fixed_queue_enqueue(btu_oneshot_alarm_queue, p_tle);
  GKI_send_event(BTU_TASK, TIMER_3_EVT_MASK);
}

/*
 * Starts a oneshot timer with a timeout in seconds.
 */
void btu_start_timer_oneshot(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_sec) {
  assert(p_tle != NULL);

  // Get the alarm for the timer list entry.
  pthread_mutex_lock(&btu_oneshot_alarm_lock);
  if (!hash_map_has_key(btu_oneshot_alarm_hash_map, p_tle)) {
    hash_map_set(btu_oneshot_alarm_hash_map, p_tle, alarm_new());
  }
  pthread_mutex_unlock(&btu_oneshot_alarm_lock);

  alarm_t *alarm = hash_map_get(btu_oneshot_alarm_hash_map, p_tle);
  if (alarm == NULL) {
    ALOGE("%s Unable to create alarm\n", __func__);
    return;
  }
  alarm_cancel(alarm);

  p_tle->event = type;
  if (p_tle->in_use == TRUE)
    ALOGW("%s Starting alarm already in use\n", __func__);
  p_tle->in_use = TRUE;
  // NOTE: This value is in seconds but stored in a ticks field.
  p_tle->ticks = timeout_sec;
  alarm_set(alarm, (period_ms_t)(timeout_sec * 1000), btu_oneshot_alarm_cb, (void *)p_tle);
}

void btu_stop_timer_oneshot(TIMER_LIST_ENT *p_tle) {
  assert(p_tle != NULL);

  if (p_tle->in_use == FALSE)
    return;
  p_tle->in_use = FALSE;

  // Get the alarm for the timer list entry.
  alarm_t *alarm = hash_map_get(btu_oneshot_alarm_hash_map, p_tle);
  if (alarm == NULL) {
    ALOGW("%s Unable to find expected alarm in hashmap\n", __func__);
    return;
  }
  alarm_cancel(alarm);
}

#if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
/*******************************************************************************
**
** Function         btu_check_bt_sleep
**
** Description      This function is called to check if controller can go to sleep.
**
** Returns          void
**
*******************************************************************************/
void btu_check_bt_sleep (void)
{
    // TODO(zachoverflow) take pending commands into account?
    if (l2cb.controller_xmit_window == l2cb.num_lm_acl_bufs)
    {
        bte_main_lpm_allow_bt_device_sleep();
    }
}
#endif