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