Swap BTA and HCI queues with message loops
Swapping out the queues with base::MessageLoop allows for an easier refactor
of alarms as we can just replace them with an instance of base::Timer. Also
refactored out the data_dispatcher as most of the code wasn't being used.
Bug: 37245162
Test: Bluetooth sanity test and listening to music
added net_test_btu_message_loop
Change-Id: I73c359f07a225733dc89f51422a6a24ce807c593
diff --git a/main/bte_main.cc b/main/bte_main.cc
index 58e44fd..527d93e 100644
--- a/main/bte_main.cc
+++ b/main/bte_main.cc
@@ -27,6 +27,7 @@
#define LOG_TAG "bt_main"
#include <base/logging.h>
+#include <base/threading/thread.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
@@ -79,13 +80,35 @@
static const hci_t* hci;
/*******************************************************************************
+ * Externs
+ ******************************************************************************/
+extern void btu_hci_msg_process(BT_HDR* p_msg);
+
+/*******************************************************************************
* Static functions
******************************************************************************/
-/*******************************************************************************
- * Externs
- ******************************************************************************/
-fixed_queue_t* btu_hci_msg_queue;
+/******************************************************************************
+ *
+ * Function post_to_hci_message_loop
+ *
+ * Description Post an HCI event to the hci message queue
+ *
+ * Returns None
+ *
+ *****************************************************************************/
+void post_to_hci_message_loop(const tracked_objects::Location& from_here,
+ BT_HDR* p_msg) {
+ base::MessageLoop* hci_message_loop = get_message_loop();
+ if (!hci_message_loop || !hci_message_loop->task_runner().get()) {
+ LOG_ERROR(LOG_TAG, "%s: HCI message loop not running, accessed from %s",
+ __func__, from_here.ToString().c_str());
+ return;
+ }
+
+ hci_message_loop->task_runner()->PostTask(
+ from_here, base::Bind(&btu_hci_msg_process, p_msg));
+}
/******************************************************************************
*
@@ -105,14 +128,7 @@
return;
}
- btu_hci_msg_queue = fixed_queue_new(SIZE_MAX);
- if (btu_hci_msg_queue == NULL) {
- LOG_ERROR(LOG_TAG, "%s unable to allocate hci message queue.", __func__);
- return;
- }
-
- data_dispatcher_register_default(hci->event_dispatcher, btu_hci_msg_queue);
- hci->set_data_queue(btu_hci_msg_queue);
+ hci->set_data_cb(base::Bind(&post_to_hci_message_loop));
module_init(get_module(STACK_CONFIG_MODULE));
}
@@ -127,13 +143,6 @@
*
*****************************************************************************/
void bte_main_cleanup() {
- data_dispatcher_register_default(hci_layer_get_interface()->event_dispatcher,
- NULL);
- hci->set_data_queue(NULL);
- fixed_queue_free(btu_hci_msg_queue, NULL);
-
- btu_hci_msg_queue = NULL;
-
module_clean_up(get_module(STACK_CONFIG_MODULE));
module_clean_up(get_module(INTEROP_MODULE));