Replace mailboxes with fixed queues.

Also refactored btu_task a bit.
diff --git a/stack/btu/btu_task.c b/stack/btu/btu_task.c
index 2880223..92e9872 100644
--- a/stack/btu/btu_task.c
+++ b/stack/btu/btu_task.c
@@ -28,13 +28,20 @@
  *
  ******************************************************************************/
 
+#include <cutils/log.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
 
 #include "bt_target.h"
-#include "gki.h"
+#include "bt_trace.h"
 #include "bt_types.h"
+#include "bt_utils.h"
+#include "btm_api.h"
+#include "btm_int.h"
+#include "btu.h"
+#include "fixed_queue.h"
+#include "gki.h"
 #include "hcimsgs.h"
 #include "l2c_int.h"
 #include "btu.h"
@@ -48,9 +55,6 @@
 #include "port_ext.h"
 #endif
 
-#include "btm_api.h"
-#include "btm_int.h"
-
 #if (defined(EVAL) && EVAL == TRUE)
 #include "btu_eval.h"
 #endif
@@ -72,8 +76,6 @@
 
 #endif /* OBX */
 
-#include "bt_trace.h"
-
 /* BTE application task */
 #if APPL_INCLUDED == TRUE
 #include "bte_appl.h"
@@ -111,7 +113,6 @@
 #include "mca_int.h"
 #endif
 
-
 #if (defined(BTU_BTA_INCLUDED) && BTU_BTA_INCLUDED == TRUE)
 #include "bta_sys.h"
 #endif
@@ -141,10 +142,332 @@
 tBTU_CB  btu_cb;
 #endif
 
+// Communication queue between btu_task and bta.
+extern fixed_queue_t *btu_bta_msg_queue;
+
+// Communication queue between btu_task and hci.
+extern fixed_queue_t *btu_hci_msg_queue;
+
 
 /* Define a function prototype to allow a generic timeout handler */
 typedef void (tUSER_TIMEOUT_FUNC) (TIMER_LIST_ENT *p_tle);
 
+static void btu_hci_msg_process(BT_HDR *p_msg) {
+    /* Determine the input message type. */
+    switch (p_msg->event & BT_EVT_MASK)
+    {
+        case BT_EVT_TO_BTU_HCI_ACL:
+            /* All Acl Data goes to L2CAP */
+            l2c_rcv_acl_data (p_msg);
+            break;
+
+        case BT_EVT_TO_BTU_L2C_SEG_XMIT:
+            /* L2CAP segment transmit complete */
+            l2c_link_segments_xmitted (p_msg);
+            break;
+
+        case BT_EVT_TO_BTU_HCI_SCO:
+#if BTM_SCO_INCLUDED == TRUE
+            btm_route_sco_data (p_msg);
+            break;
+#endif
+
+        case BT_EVT_TO_BTU_HCI_EVT:
+            btu_hcif_process_event ((UINT8)(p_msg->event & BT_SUB_EVT_MASK), p_msg);
+            GKI_freebuf(p_msg);
+
+#if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
+            /* If host receives events which it doesn't response to, */
+            /* host should start idle timer to enter sleep mode.     */
+            btu_check_bt_sleep ();
+#endif
+            break;
+
+        case BT_EVT_TO_BTU_HCI_CMD:
+            btu_hcif_send_cmd ((UINT8)(p_msg->event & BT_SUB_EVT_MASK), p_msg);
+            break;
+
+#if (defined(OBX_INCLUDED) && OBX_INCLUDED == TRUE)
+#if (defined(OBX_SERVER_INCLUDED) && OBX_SERVER_INCLUDED == TRUE)
+        case BT_EVT_TO_OBX_SR_MSG:
+            obx_sr_proc_evt((tOBX_PORT_EVT *)(p_msg + 1));
+            GKI_freebuf (p_msg);
+            break;
+
+        case BT_EVT_TO_OBX_SR_L2C_MSG:
+            obx_sr_proc_l2c_evt((tOBX_L2C_EVT_MSG *)(p_msg + 1));
+            GKI_freebuf (p_msg);
+            break;
+#endif
+
+#if (defined(OBX_CLIENT_INCLUDED) && OBX_CLIENT_INCLUDED == TRUE)
+        case BT_EVT_TO_OBX_CL_MSG:
+            obx_cl_proc_evt((tOBX_PORT_EVT *)(p_msg + 1));
+            GKI_freebuf (p_msg);
+            break;
+
+        case BT_EVT_TO_OBX_CL_L2C_MSG:
+            obx_cl_proc_l2c_evt((tOBX_L2C_EVT_MSG *)(p_msg + 1));
+            GKI_freebuf (p_msg);
+            break;
+#endif
+
+#if (defined(BIP_INCLUDED) && BIP_INCLUDED == TRUE)
+        case BT_EVT_TO_BIP_CMDS :
+            bip_proc_btu_event(p_msg);
+            GKI_freebuf (p_msg);
+            break;
+#endif /* BIP */
+#if (BPP_SND_INCLUDED == TRUE || BPP_INCLUDED == TRUE)
+        case BT_EVT_TO_BPP_PR_CMDS:
+            bpp_pr_proc_event(p_msg);
+            GKI_freebuf (p_msg);
+            break;
+        case BT_EVT_TO_BPP_SND_CMDS:
+            bpp_snd_proc_event(p_msg);
+            GKI_freebuf (p_msg);
+            break;
+
+#endif /* BPP */
+
+#endif /* OBX */
+
+#if (defined(SAP_SERVER_INCLUDED) && SAP_SERVER_INCLUDED == TRUE)
+        case BT_EVT_TO_BTU_SAP :
+            sap_proc_btu_event(p_msg);
+            GKI_freebuf (p_msg);
+            break;
+#endif /* SAP */
+#if (defined(GAP_CONN_INCLUDED) && GAP_CONN_INCLUDED == TRUE && GAP_CONN_POST_EVT_INCLUDED == TRUE)
+        case BT_EVT_TO_GAP_MSG :
+            gap_proc_btu_event(p_msg);
+            GKI_freebuf (p_msg);
+            break;
+#endif
+            // NOTE: The timer calls below may not be sent by HCI.
+        case BT_EVT_TO_START_TIMER :
+            /* Start free running 1 second timer for list management */
+            GKI_start_timer (TIMER_0, GKI_SECS_TO_TICKS (1), TRUE);
+            GKI_freebuf (p_msg);
+            break;
+
+        case BT_EVT_TO_STOP_TIMER:
+            if (GKI_timer_queue_is_empty(&btu_cb.timer_queue)) {
+                GKI_stop_timer(TIMER_0);
+            }
+            GKI_freebuf (p_msg);
+            break;
+
+                    case BT_EVT_TO_START_TIMER_ONESHOT:
+                        if (!GKI_timer_queue_is_empty(&btu_cb.timer_queue_oneshot)) {
+                            TIMER_LIST_ENT *tle = GKI_timer_getfirst(&btu_cb.timer_queue_oneshot);
+                            // Start non-repeating timer.
+                            GKI_start_timer(TIMER_3, tle->ticks, FALSE);
+                        } else {
+                            BTM_TRACE_WARNING("Oneshot timer queue empty when received start request");
+                        }
+                        GKI_freebuf(p_msg);
+                        break;
+
+                    case BT_EVT_TO_STOP_TIMER_ONESHOT:
+                        if (GKI_timer_queue_is_empty(&btu_cb.timer_queue_oneshot)) {
+                            GKI_stop_timer(TIMER_3);
+                        } else {
+                            BTM_TRACE_WARNING("Oneshot timer queue not empty when received stop request");
+                        }
+                        GKI_freebuf (p_msg);
+                        break;
+
+#if defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0)
+        case BT_EVT_TO_START_QUICK_TIMER :
+            GKI_start_timer (TIMER_2, QUICK_TIMER_TICKS, TRUE);
+            GKI_freebuf (p_msg);
+            break;
+#endif
+
+        default:;
+            int i = 0;
+            uint16_t mask = (UINT16) (p_msg->event & BT_EVT_MASK);
+            BOOLEAN handled = FALSE;
+
+            for (; !handled && i < BTU_MAX_REG_EVENT; i++)
+            {
+                if (btu_cb.event_reg[i].event_cb == NULL)
+                    continue;
+
+                if (mask == btu_cb.event_reg[i].event_range)
+                {
+                    if (btu_cb.event_reg[i].event_cb)
+                    {
+                        btu_cb.event_reg[i].event_cb(p_msg);
+                        handled = TRUE;
+                    }
+                }
+            }
+
+            if (handled == FALSE)
+                GKI_freebuf (p_msg);
+
+            break;
+    }
+
+}
+
+static void btu_timer_process() {
+    GKI_update_timer_list (&btu_cb.timer_queue, 1);
+
+    while (!GKI_timer_queue_is_empty(&btu_cb.timer_queue)) {
+        TIMER_LIST_ENT *p_tle = GKI_timer_getfirst(&btu_cb.timer_queue);
+        if (p_tle->ticks != 0)
+            break;
+
+        GKI_remove_from_timer_list (&btu_cb.timer_queue, p_tle);
+
+        switch (p_tle->event)
+        {
+            case BTU_TTYPE_BTM_DEV_CTL:
+                btm_dev_timeout(p_tle);
+                break;
+
+            case BTU_TTYPE_BTM_ACL:
+                btm_acl_timeout(p_tle);
+                break;
+
+            case BTU_TTYPE_L2CAP_LINK:
+            case BTU_TTYPE_L2CAP_CHNL:
+            case BTU_TTYPE_L2CAP_HOLD:
+            case BTU_TTYPE_L2CAP_INFO:
+            case BTU_TTYPE_L2CAP_FCR_ACK:
+
+                l2c_process_timeout (p_tle);
+                break;
+
+            case BTU_TTYPE_SDP:
+                sdp_conn_timeout ((tCONN_CB *)p_tle->param);
+                break;
+
+            case BTU_TTYPE_BTM_RMT_NAME:
+                btm_inq_rmt_name_failed();
+                break;
+
+#if (defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE)
+            case BTU_TTYPE_RFCOMM_MFC:
+            case BTU_TTYPE_RFCOMM_PORT:
+                rfcomm_process_timeout (p_tle);
+                break;
+
+#endif /* If defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE */
+
+#if ((defined(BNEP_INCLUDED) && BNEP_INCLUDED == TRUE))
+            case BTU_TTYPE_BNEP:
+                bnep_process_timeout(p_tle);
+                break;
+#endif
+
+
+#if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE)
+            case BTU_TTYPE_AVDT_CCB_RET:
+            case BTU_TTYPE_AVDT_CCB_RSP:
+            case BTU_TTYPE_AVDT_CCB_IDLE:
+            case BTU_TTYPE_AVDT_SCB_TC:
+                avdt_process_timeout(p_tle);
+                break;
+#endif
+
+#if (defined(OBX_INCLUDED) && OBX_INCLUDED == TRUE)
+#if (defined(OBX_CLIENT_INCLUDED) && OBX_CLIENT_INCLUDED == TRUE)
+            case BTU_TTYPE_OBX_CLIENT_TO:
+                obx_cl_timeout(p_tle);
+                break;
+#endif
+#if (defined(OBX_SERVER_INCLUDED) && OBX_SERVER_INCLUDED == TRUE)
+            case BTU_TTYPE_OBX_SERVER_TO:
+                obx_sr_timeout(p_tle);
+                break;
+
+            case BTU_TTYPE_OBX_SVR_SESS_TO:
+                obx_sr_sess_timeout(p_tle);
+                break;
+#endif
+#endif
+
+#if (defined(SAP_SERVER_INCLUDED) && SAP_SERVER_INCLUDED == TRUE)
+            case BTU_TTYPE_SAP_TO:
+                sap_process_timeout(p_tle);
+                break;
+#endif
+
+            case BTU_TTYPE_BTU_CMD_CMPL:
+                btu_hcif_cmd_timeout((UINT8)(p_tle->event - BTU_TTYPE_BTU_CMD_CMPL));
+                break;
+
+#if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE)
+            case BTU_TTYPE_HID_HOST_REPAGE_TO :
+                hidh_proc_repage_timeout(p_tle);
+                break;
+#endif
+
+#if (defined(BLE_INCLUDED) && BLE_INCLUDED == TRUE)
+            case BTU_TTYPE_BLE_INQUIRY:
+            case BTU_TTYPE_BLE_GAP_LIM_DISC:
+            case BTU_TTYPE_BLE_RANDOM_ADDR:
+            case BTU_TTYPE_BLE_GAP_FAST_ADV:
+            case BTU_TTYPE_BLE_OBSERVE:
+                btm_ble_timeout(p_tle);
+                break;
+
+            case BTU_TTYPE_ATT_WAIT_FOR_RSP:
+                gatt_rsp_timeout(p_tle);
+                break;
+
+            case BTU_TTYPE_ATT_WAIT_FOR_IND_ACK:
+                gatt_ind_ack_timeout(p_tle);
+                break;
+#if (defined(SMP_INCLUDED) && SMP_INCLUDED == TRUE)
+            case BTU_TTYPE_SMP_PAIRING_CMD:
+                smp_rsp_timeout(p_tle);
+                break;
+#endif
+
+#endif
+
+#if (MCA_INCLUDED == TRUE)
+            case BTU_TTYPE_MCA_CCB_RSP:
+                mca_process_timeout(p_tle);
+                break;
+#endif
+            case BTU_TTYPE_USER_FUNC:
+                {
+                    tUSER_TIMEOUT_FUNC  *p_uf = (tUSER_TIMEOUT_FUNC *)p_tle->param;
+                    (*p_uf)(p_tle);
+                }
+                break;
+
+            default:;
+                int i = 0;
+                BOOLEAN handled = FALSE;
+
+                for (; !handled && i < BTU_MAX_REG_TIMER; i++)
+                {
+                    if (btu_cb.timer_reg[i].timer_cb == NULL)
+                        continue;
+                    if (btu_cb.timer_reg[i].p_tle == p_tle)
+                    {
+                        btu_cb.timer_reg[i].timer_cb(p_tle);
+                        handled = TRUE;
+                    }
+                }
+                break;
+        }
+    }
+
+    /* if timer list is empty stop periodic GKI timer */
+    if (btu_cb.timer_queue.p_first == NULL)
+    {
+        GKI_stop_timer(TIMER_0);
+    }
+}
+
 /*******************************************************************************
 **
 ** Function         btu_task
@@ -216,352 +539,29 @@
     /* Send a startup evt message to BTIF_TASK to kickstart the init procedure */
     GKI_send_event(BTIF_TASK, BT_EVT_TRIGGER_STACK_INIT);
 
-    prctl(PR_SET_NAME, (unsigned long)"BTU TASK", 0, 0, 0);
-
     raise_priority_a2dp(TASK_HIGH_BTU);
 
     /* Wait for, and process, events */
-    for (;;)
-    {
+    for (;;) {
         event = GKI_wait (0xFFFF, 0);
 
-        if (event & TASK_MBOX_0_EVT_MASK)
-        {
-            /* Process all messages in the queue */
-            while ((p_msg = (BT_HDR *) GKI_read_mbox (BTU_HCI_RCV_MBOX)) != NULL)
-            {
-                /* Determine the input message type. */
-                switch (p_msg->event & BT_EVT_MASK)
-                {
-                    case BT_EVT_TO_BTU_HCI_ACL:
-                        /* All Acl Data goes to L2CAP */
-                        l2c_rcv_acl_data (p_msg);
-                        break;
+        while ((p_msg = (BT_HDR *)fixed_queue_try_dequeue(btu_hci_msg_queue)) != NULL)
+            btu_hci_msg_process(p_msg);
 
-                    case BT_EVT_TO_BTU_L2C_SEG_XMIT:
-                        /* L2CAP segment transmit complete */
-                        l2c_link_segments_xmitted (p_msg);
-                        break;
-
-                    case BT_EVT_TO_BTU_HCI_SCO:
-#if BTM_SCO_INCLUDED == TRUE
-                        btm_route_sco_data (p_msg);
-                        break;
-#endif
-
-                    case BT_EVT_TO_BTU_HCI_EVT:
-                        btu_hcif_process_event ((UINT8)(p_msg->event & BT_SUB_EVT_MASK), p_msg);
-                        GKI_freebuf(p_msg);
-
-#if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
-                        /* If host receives events which it doesn't response to, */
-                        /* host should start idle timer to enter sleep mode.     */
-                        btu_check_bt_sleep ();
-#endif
-                        break;
-
-                    case BT_EVT_TO_BTU_HCI_CMD:
-                        btu_hcif_send_cmd ((UINT8)(p_msg->event & BT_SUB_EVT_MASK), p_msg);
-                        break;
-
-#if (defined(OBX_INCLUDED) && OBX_INCLUDED == TRUE)
-#if (defined(OBX_SERVER_INCLUDED) && OBX_SERVER_INCLUDED == TRUE)
-                    case BT_EVT_TO_OBX_SR_MSG:
-                        obx_sr_proc_evt((tOBX_PORT_EVT *)(p_msg + 1));
-                        GKI_freebuf (p_msg);
-                        break;
-
-                    case BT_EVT_TO_OBX_SR_L2C_MSG:
-                        obx_sr_proc_l2c_evt((tOBX_L2C_EVT_MSG *)(p_msg + 1));
-                        GKI_freebuf (p_msg);
-                        break;
-#endif
-
-#if (defined(OBX_CLIENT_INCLUDED) && OBX_CLIENT_INCLUDED == TRUE)
-                    case BT_EVT_TO_OBX_CL_MSG:
-                        obx_cl_proc_evt((tOBX_PORT_EVT *)(p_msg + 1));
-                        GKI_freebuf (p_msg);
-                        break;
-
-                    case BT_EVT_TO_OBX_CL_L2C_MSG:
-                        obx_cl_proc_l2c_evt((tOBX_L2C_EVT_MSG *)(p_msg + 1));
-                        GKI_freebuf (p_msg);
-                        break;
-#endif
-
-#if (defined(BIP_INCLUDED) && BIP_INCLUDED == TRUE)
-                    case BT_EVT_TO_BIP_CMDS :
-                        bip_proc_btu_event(p_msg);
-                        GKI_freebuf (p_msg);
-                        break;
-#endif /* BIP */
-#if (BPP_SND_INCLUDED == TRUE || BPP_INCLUDED == TRUE)
-                    case BT_EVT_TO_BPP_PR_CMDS:
-                        bpp_pr_proc_event(p_msg);
-                        GKI_freebuf (p_msg);
-                        break;
-                    case BT_EVT_TO_BPP_SND_CMDS:
-                        bpp_snd_proc_event(p_msg);
-                        GKI_freebuf (p_msg);
-                        break;
-
-#endif /* BPP */
-
-#endif /* OBX */
-
-#if (defined(SAP_SERVER_INCLUDED) && SAP_SERVER_INCLUDED == TRUE)
-                    case BT_EVT_TO_BTU_SAP :
-                        sap_proc_btu_event(p_msg);
-                        GKI_freebuf (p_msg);
-                        break;
-#endif /* SAP */
-#if (defined(GAP_CONN_INCLUDED) && GAP_CONN_INCLUDED == TRUE && GAP_CONN_POST_EVT_INCLUDED == TRUE)
-                    case BT_EVT_TO_GAP_MSG :
-                        gap_proc_btu_event(p_msg);
-                        GKI_freebuf (p_msg);
-                        break;
-#endif
-                    case BT_EVT_TO_START_TIMER :
-                        /* Start free running 1 second timer for list management */
-                        GKI_start_timer (TIMER_0, GKI_SECS_TO_TICKS (1), TRUE);
-                        GKI_freebuf (p_msg);
-                        break;
-
-                    case BT_EVT_TO_STOP_TIMER:
-                        if (GKI_timer_queue_is_empty(&btu_cb.timer_queue)) {
-                            GKI_stop_timer(TIMER_0);
-                        }
-                        GKI_freebuf (p_msg);
-                        break;
-
-                    case BT_EVT_TO_START_TIMER_ONESHOT:
-                        if (!GKI_timer_queue_is_empty(&btu_cb.timer_queue_oneshot)) {
-                            TIMER_LIST_ENT *tle = GKI_timer_getfirst(&btu_cb.timer_queue_oneshot);
-                            // Start non-repeating timer.
-                            GKI_start_timer(TIMER_3, tle->ticks, FALSE);
-                        } else {
-                            BTM_TRACE_WARNING("Oneshot timer queue empty when received start request");
-                        }
-                        GKI_freebuf(p_msg);
-                        break;
-
-                    case BT_EVT_TO_STOP_TIMER_ONESHOT:
-                        if (GKI_timer_queue_is_empty(&btu_cb.timer_queue_oneshot)) {
-                            GKI_stop_timer(TIMER_3);
-                        } else {
-                            BTM_TRACE_WARNING("Oneshot timer queue not empty when received stop request");
-                        }
-                        GKI_freebuf (p_msg);
-                        break;
-
-#if defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0)
-                    case BT_EVT_TO_START_QUICK_TIMER :
-                        GKI_start_timer (TIMER_2, QUICK_TIMER_TICKS, TRUE);
-                        GKI_freebuf (p_msg);
-                        break;
-#endif
-
-                    default:
-                        i = 0;
-                        mask = (UINT16) (p_msg->event & BT_EVT_MASK);
-                        handled = FALSE;
-
-                        for (; !handled && i < BTU_MAX_REG_EVENT; i++)
-                        {
-                            if (btu_cb.event_reg[i].event_cb == NULL)
-                                continue;
-
-                            if (mask == btu_cb.event_reg[i].event_range)
-                            {
-                                if (btu_cb.event_reg[i].event_cb)
-                                {
-                                    btu_cb.event_reg[i].event_cb(p_msg);
-                                    handled = TRUE;
-                                }
-                            }
-                        }
-
-                        if (handled == FALSE)
-                            GKI_freebuf (p_msg);
-
-                        break;
-                }
-            }
-        }
-
-
-        if (event & TIMER_0_EVT_MASK) {
-            GKI_update_timer_list (&btu_cb.timer_queue, 1);
-
-            while (!GKI_timer_queue_is_empty(&btu_cb.timer_queue)) {
-                TIMER_LIST_ENT *p_tle = GKI_timer_getfirst(&btu_cb.timer_queue);
-                if (p_tle->ticks != 0)
-                    break;
-
-                GKI_remove_from_timer_list(&btu_cb.timer_queue, p_tle);
-
-                switch (p_tle->event) {
-                    case BTU_TTYPE_BTM_DEV_CTL:
-                        btm_dev_timeout(p_tle);
-                        break;
-
-                    case BTU_TTYPE_BTM_ACL:
-                        btm_acl_timeout(p_tle);
-                        break;
-
-                    case BTU_TTYPE_L2CAP_LINK:
-                    case BTU_TTYPE_L2CAP_CHNL:
-                    case BTU_TTYPE_L2CAP_HOLD:
-                    case BTU_TTYPE_L2CAP_INFO:
-                    case BTU_TTYPE_L2CAP_FCR_ACK:
-                        l2c_process_timeout (p_tle);
-                        break;
-
-                    case BTU_TTYPE_SDP:
-                        sdp_conn_timeout ((tCONN_CB *)p_tle->param);
-                        break;
-
-                    case BTU_TTYPE_BTM_RMT_NAME:
-                        btm_inq_rmt_name_failed();
-                        break;
-
-#if (defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE)
-                    case BTU_TTYPE_RFCOMM_MFC:
-                    case BTU_TTYPE_RFCOMM_PORT:
-                        rfcomm_process_timeout (p_tle);
-                        break;
-
-#endif /* If defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE */
-
-#if ((defined(BNEP_INCLUDED) && BNEP_INCLUDED == TRUE))
-                    case BTU_TTYPE_BNEP:
-                        bnep_process_timeout(p_tle);
-                        break;
-#endif
-
-
-#if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE)
-                    case BTU_TTYPE_AVDT_CCB_RET:
-                    case BTU_TTYPE_AVDT_CCB_RSP:
-                    case BTU_TTYPE_AVDT_CCB_IDLE:
-                    case BTU_TTYPE_AVDT_SCB_TC:
-                        avdt_process_timeout(p_tle);
-                        break;
-#endif
-
-#if (defined(OBX_INCLUDED) && OBX_INCLUDED == TRUE)
-#if (defined(OBX_CLIENT_INCLUDED) && OBX_CLIENT_INCLUDED == TRUE)
-                    case BTU_TTYPE_OBX_CLIENT_TO:
-                        obx_cl_timeout(p_tle);
-                        break;
-#endif
-#if (defined(OBX_SERVER_INCLUDED) && OBX_SERVER_INCLUDED == TRUE)
-                    case BTU_TTYPE_OBX_SERVER_TO:
-                        obx_sr_timeout(p_tle);
-                        break;
-
-                    case BTU_TTYPE_OBX_SVR_SESS_TO:
-                        obx_sr_sess_timeout(p_tle);
-                        break;
-#endif
-#endif
-
-#if (defined(SAP_SERVER_INCLUDED) && SAP_SERVER_INCLUDED == TRUE)
-                    case BTU_TTYPE_SAP_TO:
-                        sap_process_timeout(p_tle);
-                        break;
-#endif
-
-                    case BTU_TTYPE_BTU_CMD_CMPL:
-                        btu_hcif_cmd_timeout((UINT8)(p_tle->event - BTU_TTYPE_BTU_CMD_CMPL));
-                        break;
-
-#if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE)
-                    case BTU_TTYPE_HID_HOST_REPAGE_TO :
-                        hidh_proc_repage_timeout(p_tle);
-                        break;
-#endif
-
-#if (defined(BLE_INCLUDED) && BLE_INCLUDED == TRUE)
-                    case BTU_TTYPE_BLE_INQUIRY:
-                    case BTU_TTYPE_BLE_GAP_LIM_DISC:
-                    case BTU_TTYPE_BLE_GAP_FAST_ADV:
-                    case BTU_TTYPE_BLE_OBSERVE:
-                        btm_ble_timeout(p_tle);
-                        break;
-
-                    case BTU_TTYPE_ATT_WAIT_FOR_RSP:
-                        gatt_rsp_timeout(p_tle);
-                        break;
-
-                    case BTU_TTYPE_ATT_WAIT_FOR_IND_ACK:
-                        gatt_ind_ack_timeout(p_tle);
-                        break;
-#if (defined(SMP_INCLUDED) && SMP_INCLUDED == TRUE)
-                    case BTU_TTYPE_SMP_PAIRING_CMD:
-                        smp_rsp_timeout(p_tle);
-                        break;
-#endif
-
-#endif
-
-#if (MCA_INCLUDED == TRUE)
-                    case BTU_TTYPE_MCA_CCB_RSP:
-                        mca_process_timeout(p_tle);
-                        break;
-#endif
-                    case BTU_TTYPE_USER_FUNC:
-                        {
-                            tUSER_TIMEOUT_FUNC  *p_uf = (tUSER_TIMEOUT_FUNC *)p_tle->param;
-                            (*p_uf)(p_tle);
-                        }
-                        break;
-
-                    default:
-                        i = 0;
-                        handled = FALSE;
-
-                        for (; !handled && i < BTU_MAX_REG_TIMER; i++)
-                        {
-                            if (btu_cb.timer_reg[i].timer_cb == NULL)
-                                continue;
-                            if (btu_cb.timer_reg[i].p_tle == p_tle)
-                            {
-                                btu_cb.timer_reg[i].timer_cb(p_tle);
-                                handled = TRUE;
-                            }
-                        }
-                        break;
-                }
-            }
-
-            /* if timer list is empty stop periodic GKI timer */
-            if (btu_cb.timer_queue.p_first == NULL)
-            {
-                GKI_stop_timer(TIMER_0);
-            }
-        }
+        if (event & TIMER_0_EVT_MASK)
+            btu_timer_process();
 
 #if defined(QUICK_TIMER_TICKS_PER_SEC) && (QUICK_TIMER_TICKS_PER_SEC > 0)
         if (event & TIMER_2_EVT_MASK)
-        {
             btu_process_quick_timer_evt();
-        }
 #endif
 
 #if (defined(BTU_BTA_INCLUDED) && BTU_BTA_INCLUDED == TRUE)
-        if (event & TASK_MBOX_2_EVT_MASK)
-        {
-            while ((p_msg = (BT_HDR *) GKI_read_mbox(TASK_MBOX_2)) != NULL)
-            {
-                bta_sys_event(p_msg);
-            }
-        }
+        while ((p_msg = (BT_HDR *)fixed_queue_try_dequeue(btu_bta_msg_queue)) != NULL)
+            bta_sys_event(p_msg);
 
         if (event & TIMER_1_EVT_MASK)
-        {
             bta_sys_timer_update();
-        }
 #endif
 
         if (event & TIMER_3_EVT_MASK) {
@@ -645,7 +645,9 @@
             if ((p_msg = (BT_HDR *)GKI_getbuf(BT_HDR_SIZE)) != NULL)
             {
                 p_msg->event = BT_EVT_TO_START_TIMER;
-                GKI_send_msg (BTU_TASK, TASK_MBOX_0, p_msg);
+                fixed_queue_enqueue(btu_hci_msg_queue, p_msg);
+                // Signal the target thread work is ready.
+                GKI_send_event(BTU_TASK, (UINT16)EVENT_MASK(BTU_HCI_RCV_MBOX));
             }
         }
         else
@@ -701,7 +703,9 @@
         if ((p_msg = (BT_HDR *)GKI_getbuf(BT_HDR_SIZE)) != NULL)
         {
             p_msg->event = BT_EVT_TO_STOP_TIMER;
-            GKI_send_msg (BTU_TASK, TASK_MBOX_0, p_msg);
+            fixed_queue_enqueue(btu_hci_msg_queue, p_msg);
+            // Signal the target thread work is ready.
+            GKI_send_event(BTU_TASK, (UINT16)EVENT_MASK(BTU_HCI_RCV_MBOX));
         }
     }
     else
@@ -744,7 +748,9 @@
             if ((p_msg = (BT_HDR *)GKI_getbuf(BT_HDR_SIZE)) != NULL)
             {
                 p_msg->event = BT_EVT_TO_START_QUICK_TIMER;
-                GKI_send_msg (BTU_TASK, TASK_MBOX_0, p_msg);
+                fixed_queue_enqueue(btu_hci_msg_queue, p_msg);
+                // Signal the target thread work is ready.
+                GKI_send_event(BTU_TASK, (UINT16)EVENT_MASK(BTU_HCI_RCV_MBOX));
             }
         }
         else
@@ -862,7 +868,9 @@
         BT_HDR *p_msg = (BT_HDR *)GKI_getbuf(BT_HDR_SIZE);
         if (p_msg != NULL) {
             p_msg->event = BT_EVT_TO_START_TIMER_ONESHOT;
-            GKI_send_msg (BTU_TASK, TASK_MBOX_0, p_msg);
+            fixed_queue_enqueue(btu_hci_msg_queue, p_msg);
+            // Signal the target thread work is ready.
+            GKI_send_event(BTU_TASK, (UINT16)EVENT_MASK(BTU_HCI_RCV_MBOX));
         }
     } else {
         TIMER_LIST_ENT *tle = GKI_timer_getfirst(&btu_cb.timer_queue_oneshot);
@@ -880,7 +888,9 @@
         BT_HDR *p_msg = (BT_HDR *)GKI_getbuf(BT_HDR_SIZE);
         if (p_msg != NULL) {
             p_msg->event = BT_EVT_TO_STOP_TIMER_ONESHOT;
-            GKI_send_msg (BTU_TASK, TASK_MBOX_0, p_msg);
+            fixed_queue_enqueue(btu_hci_msg_queue, p_msg);
+            // Signal the target thread work is ready.
+            GKI_send_event(BTU_TASK, (UINT16)EVENT_MASK(BTU_HCI_RCV_MBOX));
         }
     } else {
         if (GKI_timer_queue_is_empty(&btu_cb.timer_queue_oneshot)) {