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