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