Home | History | Annotate | Download | only in btu
      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
    109 extern 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
    132 extern "C"
    133 {
    134 #endif
    135 
    136 BT_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
    145 tBTU_CB  btu_cb;
    146 #endif
    147 
    148 
    149 /* Define a function prototype to allow a generic timeout handler */
    150 typedef 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 *******************************************************************************/
    163 BTU_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     return(0);
    635 }
    636 
    637 /*******************************************************************************
    638 **
    639 ** Function         btu_start_timer
    640 **
    641 ** Description      Start a timer for the specified amount of time.
    642 **                  NOTE: The timeout resolution is in SECONDS! (Even
    643 **                          though the timer structure field is ticks)
    644 **
    645 ** Returns          void
    646 **
    647 *******************************************************************************/
    648 void btu_start_timer (TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout)
    649 {
    650     BT_HDR *p_msg;
    651     GKI_disable();
    652     /* if timer list is currently empty, start periodic GKI timer */
    653     if (btu_cb.timer_queue.p_first == NULL)
    654     {
    655         /* if timer starts on other than BTU task */
    656         if (GKI_get_taskid() != BTU_TASK)
    657         {
    658             /* post event to start timer in BTU task */
    659             if ((p_msg = (BT_HDR *)GKI_getbuf(BT_HDR_SIZE)) != NULL)
    660             {
    661                 p_msg->event = BT_EVT_TO_START_TIMER;
    662                 GKI_send_msg (BTU_TASK, TASK_MBOX_0, p_msg);
    663             }
    664         }
    665         else
    666         {
    667             /* Start free running 1 second timer for list management */
    668             GKI_start_timer (TIMER_0, GKI_SECS_TO_TICKS (1), TRUE);
    669         }
    670     }
    671 
    672     GKI_remove_from_timer_list (&btu_cb.timer_queue, p_tle);
    673 
    674     p_tle->event = type;
    675     p_tle->ticks = timeout;
    676     p_tle->ticks_initial = timeout;
    677 
    678     GKI_add_to_timer_list (&btu_cb.timer_queue, p_tle);
    679     GKI_enable();
    680 }
    681 
    682 /*******************************************************************************
    683 **
    684 ** Function         btu_remaining_time
    685 **
    686 ** Description      Return amount of time to expire
    687 **
    688 ** Returns          time in second
    689 **
    690 *******************************************************************************/
    691 UINT32 btu_remaining_time (TIMER_LIST_ENT *p_tle)
    692 {
    693     return(GKI_get_remaining_ticks (&btu_cb.timer_queue, p_tle));
    694 }
    695 
    696 /*******************************************************************************
    697 **
    698 ** Function         btu_stop_timer
    699 **
    700 ** Description      Stop a timer.
    701 **
    702 ** Returns          void
    703 **
    704 *******************************************************************************/
    705 void btu_stop_timer (TIMER_LIST_ENT *p_tle)
    706 {
    707     BT_HDR *p_msg;
    708     GKI_disable();
    709     GKI_remove_from_timer_list (&btu_cb.timer_queue, p_tle);
    710 
    711     /* if timer is stopped on other than BTU task */
    712     if (GKI_get_taskid() != BTU_TASK)
    713     {
    714         /* post event to stop timer in BTU task */
    715         if ((p_msg = (BT_HDR *)GKI_getbuf(BT_HDR_SIZE)) != NULL)
    716         {
    717             p_msg->event = BT_EVT_TO_STOP_TIMER;
    718             GKI_send_msg (BTU_TASK, TASK_MBOX_0, p_msg);
    719         }
    720     }
    721     else
    722     {
    723         /* if timer list is empty stop periodic GKI timer */
    724         if (btu_cb.timer_queue.p_first == NULL)
    725         {
    726             GKI_stop_timer(TIMER_0);
    727         }
    728     }
    729     GKI_enable();
    730 }
    731 
    732 #if defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0)
    733 /*******************************************************************************
    734 **
    735 ** Function         btu_start_quick_timer
    736 **
    737 ** Description      Start a timer for the specified amount of time.
    738 **                  NOTE: The timeout resolution depends on including modules.
    739 **                  QUICK_TIMER_TICKS_PER_SEC should be used to convert from
    740 **                  time to ticks.
    741 **
    742 **
    743 ** Returns          void
    744 **
    745 *******************************************************************************/
    746 void btu_start_quick_timer (TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout)
    747 {
    748     BT_HDR *p_msg;
    749 
    750     GKI_disable();
    751     /* if timer list is currently empty, start periodic GKI timer */
    752     if (btu_cb.quick_timer_queue.p_first == NULL)
    753     {
    754         /* script test calls stack API without posting event */
    755         if (GKI_get_taskid() != BTU_TASK)
    756         {
    757             /* post event to start timer in BTU task */
    758             if ((p_msg = (BT_HDR *)GKI_getbuf(BT_HDR_SIZE)) != NULL)
    759             {
    760                 p_msg->event = BT_EVT_TO_START_QUICK_TIMER;
    761                 GKI_send_msg (BTU_TASK, TASK_MBOX_0, p_msg);
    762             }
    763         }
    764         else
    765             GKI_start_timer(TIMER_2, QUICK_TIMER_TICKS, TRUE);
    766     }
    767 
    768     GKI_remove_from_timer_list (&btu_cb.quick_timer_queue, p_tle);
    769 
    770     p_tle->event = type;
    771     p_tle->ticks = timeout;
    772     p_tle->ticks_initial = timeout;
    773 
    774     GKI_add_to_timer_list (&btu_cb.quick_timer_queue, p_tle);
    775     GKI_enable();
    776 }
    777 
    778 
    779 /*******************************************************************************
    780 **
    781 ** Function         btu_stop_quick_timer
    782 **
    783 ** Description      Stop a timer.
    784 **
    785 ** Returns          void
    786 **
    787 *******************************************************************************/
    788 void btu_stop_quick_timer (TIMER_LIST_ENT *p_tle)
    789 {
    790     GKI_disable();
    791     GKI_remove_from_timer_list (&btu_cb.quick_timer_queue, p_tle);
    792 
    793     /* if timer list is empty stop periodic GKI timer */
    794     if (btu_cb.quick_timer_queue.p_first == NULL)
    795     {
    796         GKI_stop_timer(TIMER_2);
    797     }
    798     GKI_enable();
    799 }
    800 
    801 /*******************************************************************************
    802 **
    803 ** Function         btu_process_quick_timer_evt
    804 **
    805 ** Description      Process quick timer event
    806 **
    807 ** Returns          void
    808 **
    809 *******************************************************************************/
    810 void btu_process_quick_timer_evt(void)
    811 {
    812     process_quick_timer_evt(&btu_cb.quick_timer_queue);
    813 
    814     /* if timer list is empty stop periodic GKI timer */
    815     if (btu_cb.quick_timer_queue.p_first == NULL)
    816     {
    817         GKI_stop_timer(TIMER_2);
    818     }
    819 }
    820 
    821 /*******************************************************************************
    822 **
    823 ** Function         process_quick_timer_evt
    824 **
    825 ** Description      Process quick timer event
    826 **
    827 ** Returns          void
    828 **
    829 *******************************************************************************/
    830 void process_quick_timer_evt(TIMER_LIST_Q *p_tlq)
    831 {
    832     TIMER_LIST_ENT  *p_tle;
    833 
    834     GKI_update_timer_list (p_tlq, 1);
    835 
    836     while ((p_tlq->p_first) && (!p_tlq->p_first->ticks))
    837     {
    838         p_tle = p_tlq->p_first;
    839         GKI_remove_from_timer_list (p_tlq, p_tle);
    840 
    841         switch (p_tle->event)
    842         {
    843             case BTU_TTYPE_L2CAP_CHNL:      /* monitor or retransmission timer */
    844             case BTU_TTYPE_L2CAP_FCR_ACK:   /* ack timer */
    845                 l2c_process_timeout (p_tle);
    846                 break;
    847 
    848             default:
    849                 break;
    850         }
    851     }
    852 }
    853 #endif /* defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0) */
    854 
    855 /*
    856  * Starts a oneshot timer with a timeout in seconds.
    857  */
    858 void btu_start_timer_oneshot(TIMER_LIST_ENT *p_tle, UINT16 type, UINT32 timeout_in_secs) {
    859     INT32 timeout_in_ticks = GKI_SECS_TO_TICKS(timeout_in_secs);
    860     BTM_TRACE_DEBUG("Starting oneshot timer type:%d timeout:%ds", type, timeout_in_secs);
    861     GKI_disable();
    862     if (GKI_timer_queue_is_empty(&btu_cb.timer_queue_oneshot)) {
    863     }
    864 
    865     GKI_remove_from_timer_list(&btu_cb.timer_queue_oneshot, p_tle);
    866 
    867     p_tle->event = type;
    868     p_tle->ticks = timeout_in_ticks;
    869     p_tle->ticks_initial = timeout_in_ticks;
    870 
    871     GKI_add_to_timer_list(&btu_cb.timer_queue_oneshot, p_tle);
    872     /* RPC to BTU thread if timer start request from non-BTU task */
    873     if (GKI_get_taskid() != BTU_TASK) {
    874         /* post event to start timer in BTU task */
    875         BTM_TRACE_WARNING("Posting oneshot timer event to btu_task");
    876         BT_HDR *p_msg = (BT_HDR *)GKI_getbuf(BT_HDR_SIZE);
    877         if (p_msg != NULL) {
    878             p_msg->event = BT_EVT_TO_START_TIMER_ONESHOT;
    879             GKI_send_msg (BTU_TASK, TASK_MBOX_0, p_msg);
    880         }
    881     } else {
    882         TIMER_LIST_ENT *tle = GKI_timer_getfirst(&btu_cb.timer_queue_oneshot);
    883         GKI_start_timer(TIMER_3, tle->ticks, FALSE);
    884     }
    885     GKI_enable();
    886 }
    887 
    888 void btu_stop_timer_oneshot(TIMER_LIST_ENT *p_tle) {
    889     GKI_disable();
    890     GKI_remove_from_timer_list(&btu_cb.timer_queue_oneshot, p_tle);
    891 
    892     if (GKI_get_taskid() != BTU_TASK) {
    893         /* post event to stop timer in BTU task */
    894         BT_HDR *p_msg = (BT_HDR *)GKI_getbuf(BT_HDR_SIZE);
    895         if (p_msg != NULL) {
    896             p_msg->event = BT_EVT_TO_STOP_TIMER_ONESHOT;
    897             GKI_send_msg (BTU_TASK, TASK_MBOX_0, p_msg);
    898         }
    899     } else {
    900         if (GKI_timer_queue_is_empty(&btu_cb.timer_queue_oneshot)) {
    901             BTM_TRACE_WARNING("Stopping oneshot timer");
    902             GKI_stop_timer(TIMER_3);
    903         } else {
    904             BTM_TRACE_WARNING("Request to stop oneshot timer with non empty queue");
    905         }
    906     }
    907     GKI_enable();
    908 }
    909 
    910 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
    911 /*******************************************************************************
    912 **
    913 ** Function         btu_check_bt_sleep
    914 **
    915 ** Description      This function is called to check if controller can go to sleep.
    916 **
    917 ** Returns          void
    918 **
    919 *******************************************************************************/
    920 void btu_check_bt_sleep (void)
    921 {
    922     if ((btu_cb.hci_cmd_cb[LOCAL_BR_EDR_CONTROLLER_ID].cmd_cmpl_q.count == 0)
    923         &&(btu_cb.hci_cmd_cb[LOCAL_BR_EDR_CONTROLLER_ID].cmd_xmit_q.count == 0))
    924     {
    925         if (l2cb.controller_xmit_window == l2cb.num_lm_acl_bufs)
    926         {
    927             /* enable dev to sleep  in the cmd cplt and cmd status only and num cplt packet */
    928             HCI_LP_ALLOW_BT_DEVICE_SLEEP();
    929         }
    930     }
    931 }
    932 #endif
    933