From 2d262d5e402a35a61f9351d324320f502732efc9 Mon Sep 17 00:00:00 2001 From: Jakub Pawlowski Date: Mon, 16 Oct 2017 15:57:16 -0700 Subject: [PATCH] Remove hw_module_t dependency in Bluetooth (1/3) Bug: 67853426 Test: run Bluetooth Merged-In: Ia3808552137d1f770f2c0305aaa01181f383d064 Change-Id: Ia3808552137d1f770f2c0305aaa01181f383d064 --- btcore/include/hal_util.h | 4 ++-- btcore/src/hal_util.cc | 30 +++++++++---------------- btif/src/bluetooth.cc | 39 +-------------------------------- build/BUILD.gn | 5 ----- service/hal/bluetooth_interface.cc | 26 ++++------------------ service/hal/bluetooth_interface.h | 5 ----- service/hal/fake_bluetooth_interface.cc | 5 ----- service/hal/fake_bluetooth_interface.h | 1 - service/test/fake_hal_util.cc | 2 +- tools/mcap_tool/mcap_tool.cc | 27 +++++++---------------- 10 files changed, 26 insertions(+), 118 deletions(-) diff --git a/btcore/include/hal_util.h b/btcore/include/hal_util.h index e14fc4d30..615c96095 100644 --- a/btcore/include/hal_util.h +++ b/btcore/include/hal_util.h @@ -16,8 +16,8 @@ #pragma once -struct hw_module_t; +#include // Loads the Bluetooth library. This function looks explicitly for // libbluetooth.default.so and loads it. -int hal_util_load_bt_library(const struct hw_module_t** module); +int hal_util_load_bt_library(const bt_interface_t** interface); diff --git a/btcore/src/hal_util.cc b/btcore/src/hal_util.cc index 58c97ec24..d48e08271 100644 --- a/btcore/src/hal_util.cc +++ b/btcore/src/hal_util.cc @@ -37,10 +37,9 @@ using base::StringPrintf; #define BACKUP_PATH "/system/lib/hw/" BLUETOOTH_LIBRARY_NAME #endif -int hal_util_load_bt_library(const struct hw_module_t** module) { - const char* id = BT_STACK_MODULE_ID; - const char* sym = HAL_MODULE_INFO_SYM_AS_STR; - struct hw_module_t* hmi = nullptr; +int hal_util_load_bt_library(const bt_interface_t** interface) { + const char* sym = BLUETOOTH_INTERFACE_STRING; + bt_interface_t* itf = nullptr; // Always try to load the default Bluetooth stack on GN builds. const char* path = BLUETOOTH_LIBRARY_NAME; @@ -60,32 +59,23 @@ int hal_util_load_bt_library(const struct hw_module_t** module) { } } - // Get the address of the struct hal_module_info. - hmi = (struct hw_module_t*)dlsym(handle, sym); - if (!hmi) { + // Get the address of the bt_interface_t. + itf = (bt_interface_t*)dlsym(handle, sym); + if (!itf) { LOG(ERROR) << __func__ << ": failed to load symbol from Bluetooth library " << sym; goto error; } - // Check that the id matches. - if (strcmp(id, hmi->id) != 0) { - LOG(ERROR) << StringPrintf("%s: id=%s does not match HAL module ID: %s", - __func__, id, hmi->id); - goto error; - } - - hmi->dso = handle; - // Success. - LOG(INFO) << StringPrintf("%s: loaded HAL id=%s path=%s hmi=%p handle=%p", - __func__, id, path, hmi, handle); + LOG(INFO) << __func__ << " loaded HAL path=" << BLUETOOTH_LIBRARY_NAME + << " btinterface=" << itf << " handle=" << handle; - *module = hmi; + *interface = itf; return 0; error: - *module = NULL; + *interface = NULL; if (handle) dlclose(handle); return -EINVAL; diff --git a/btif/src/bluetooth.cc b/btif/src/bluetooth.cc index 8c9b7aecb..9b80af135 100644 --- a/btif/src/bluetooth.cc +++ b/btif/src/bluetooth.cc @@ -414,7 +414,7 @@ static int config_clear(void) { return btif_config_clear() ? BT_STATUS_SUCCESS : BT_STATUS_FAIL; } -static const bt_interface_t bluetoothInterface = { +EXPORT_SYMBOL bt_interface_t bluetoothInterface = { sizeof(bluetoothInterface), init, enable, @@ -448,40 +448,3 @@ static const bt_interface_t bluetoothInterface = { interop_database_clear, interop_database_add, }; - -const bt_interface_t* bluetooth__get_bluetooth_interface() { - /* fixme -- add property to disable bt interface ? */ - - return &bluetoothInterface; -} - -static int close_bluetooth_stack(UNUSED_ATTR struct hw_device_t* device) { - cleanup(); - return 0; -} - -static int open_bluetooth_stack(const struct hw_module_t* module, - UNUSED_ATTR char const* name, - struct hw_device_t** abstraction) { - static bluetooth_device_t device; - device.common.tag = HARDWARE_DEVICE_TAG; - device.common.version = 0; - device.common.close = close_bluetooth_stack; - device.get_bluetooth_interface = bluetooth__get_bluetooth_interface; - device.common.module = (struct hw_module_t*)module; - *abstraction = (struct hw_device_t*)&device; - return 0; -} - -static struct hw_module_methods_t bt_stack_module_methods = { - .open = open_bluetooth_stack, -}; - -EXPORT_SYMBOL struct hw_module_t HAL_MODULE_INFO_SYM = { - .tag = HARDWARE_MODULE_TAG, - .version_major = 1, - .version_minor = 0, - .id = BT_HARDWARE_MODULE_ID, - .name = "Bluetooth Stack", - .author = "The Android Open Source Project", - .methods = &bt_stack_module_methods}; diff --git a/build/BUILD.gn b/build/BUILD.gn index 8a36d7473..ebf63728a 100644 --- a/build/BUILD.gn +++ b/build/BUILD.gn @@ -55,11 +55,6 @@ config("linux") { "EXPORT_SYMBOL=__attribute__((visibility(\"default\")))", "KERNEL_MISSING_CLOCK_BOOTTIME_ALARM=TRUE", - # This is a macro to that can be used by android hardware/libhardware - # to not include dependencies on core project. This is a temporary - # workaround until we get rid of dependency on hardware. - "_HW_DONT_INCLUDE_CORE_=1", - # This is a macro to that can be used by source code to detect if the # current build is done by GN or via Android.mk. This is a temporary # workaround until we can remove all Android-specific dependencies. diff --git a/service/hal/bluetooth_interface.cc b/service/hal/bluetooth_interface.cc index 39816ef78..dd642b23c 100644 --- a/service/hal/bluetooth_interface.cc +++ b/service/hal/bluetooth_interface.cc @@ -209,7 +209,7 @@ bt_os_callouts_t bt_os_callouts = {sizeof(bt_os_callouts_t), // BluetoothInterface implementation for production. class BluetoothInterfaceImpl : public BluetoothInterface { public: - BluetoothInterfaceImpl() : hal_iface_(nullptr), hal_adapter_(nullptr) {} + BluetoothInterfaceImpl() : hal_iface_(nullptr) {} ~BluetoothInterfaceImpl() override { if (hal_iface_) hal_iface_->cleanup(); @@ -230,31 +230,18 @@ class BluetoothInterfaceImpl : public BluetoothInterface { bt_callbacks_t* GetHALCallbacks() const override { return &bt_callbacks; } - const bluetooth_device_t* GetHALAdapter() const override { - return hal_adapter_; - } - // Initialize the interface. This loads the shared Bluetooth library and sets // up the callbacks. bool Initialize() { // Load the Bluetooth shared library module. - const hw_module_t* module; - int status = hal_util_load_bt_library(&module); - if (status) { - LOG(ERROR) << "Failed to load Bluetooth library: " << status; - return false; - } - - // Open the Bluetooth adapter. - hw_device_t* device; - status = module->methods->open(module, BT_HARDWARE_MODULE_ID, &device); + const bt_interface_t* interface; + int status = hal_util_load_bt_library(&interface); if (status) { LOG(ERROR) << "Failed to open the Bluetooth module"; return false; } - hal_adapter_ = reinterpret_cast(device); - hal_iface_ = hal_adapter_->get_bluetooth_interface(); + hal_iface_ = interface; // Initialize the Bluetooth interface. Set up the adapter (Bluetooth DM) API // callbacks. @@ -286,11 +273,6 @@ class BluetoothInterfaceImpl : public BluetoothInterface { // to this since the actual data resides in the shared Bluetooth library. const bt_interface_t* hal_iface_; - // The HAL handle that represents the underlying Bluetooth adapter. We hold a - // weak reference to this since the actual data resides in the shared - // Bluetooth library. - const bluetooth_device_t* hal_adapter_; - DISALLOW_COPY_AND_ASSIGN(BluetoothInterfaceImpl); }; diff --git a/service/hal/bluetooth_interface.h b/service/hal/bluetooth_interface.h index 49d621c9a..f24f6f660 100644 --- a/service/hal/bluetooth_interface.h +++ b/service/hal/bluetooth_interface.h @@ -111,11 +111,6 @@ class BluetoothInterface { // Returns the HAL callbacks that have been initialized previously. virtual bt_callbacks_t* GetHALCallbacks() const = 0; - // The HAL module pointer that represents the underlying Bluetooth adapter. - // This is implemented in and provided by the shared Bluetooth library, so - // this isn't owned by us. - virtual const bluetooth_device_t* GetHALAdapter() const = 0; - protected: BluetoothInterface() = default; virtual ~BluetoothInterface() = default; diff --git a/service/hal/fake_bluetooth_interface.cc b/service/hal/fake_bluetooth_interface.cc index 45fdca080..55dea79f1 100644 --- a/service/hal/fake_bluetooth_interface.cc +++ b/service/hal/fake_bluetooth_interface.cc @@ -154,10 +154,5 @@ bt_callbacks_t* FakeBluetoothInterface::GetHALCallbacks() const { return nullptr; } -const bluetooth_device_t* FakeBluetoothInterface::GetHALAdapter() const { - // TODO(armansito): Do something meaningful here to simulate test behavior. - return nullptr; -} - } // namespace hal } // namespace bluetooth diff --git a/service/hal/fake_bluetooth_interface.h b/service/hal/fake_bluetooth_interface.h index b0fd657af..53cc7eb77 100644 --- a/service/hal/fake_bluetooth_interface.h +++ b/service/hal/fake_bluetooth_interface.h @@ -63,7 +63,6 @@ class FakeBluetoothInterface : public BluetoothInterface { void RemoveObserver(Observer* observer) override; const bt_interface_t* GetHALInterface() const override; bt_callbacks_t* GetHALCallbacks() const override; - const bluetooth_device_t* GetHALAdapter() const override; private: base::ObserverList observers_; diff --git a/service/test/fake_hal_util.cc b/service/test/fake_hal_util.cc index 0fdc19ba5..b123c1d58 100644 --- a/service/test/fake_hal_util.cc +++ b/service/test/fake_hal_util.cc @@ -22,4 +22,4 @@ // tests. Instead of doing it this way, however, we should instead provide a C++ // class abstraction for this (and all other btif interfaces) that we can mock // for testing. -int hal_util_load_bt_library(const struct hw_module_t** module) { return -1; } +int hal_util_load_bt_library(const bt_interface_t** interface) { return -1; } diff --git a/tools/mcap_tool/mcap_tool.cc b/tools/mcap_tool/mcap_tool.cc index fad3378d2..2ec8e6dd1 100644 --- a/tools/mcap_tool/mcap_tool.cc +++ b/tools/mcap_tool/mcap_tool.cc @@ -53,9 +53,7 @@ #include "mca_api.h" #include "mca_defs.h" #include "osi/include/compat.h" -#if defined(OS_GENERIC) #include "hal_util.h" -#endif #include "mcap_test_app.h" #include "mcap_test_mcl.h" #include "mcap_test_mdep.h" @@ -98,7 +96,6 @@ static bt_status_t global_status; static bool global_strict_mode = false; /* Device and Profile Interfaces */ -static bluetooth_device_t* sBtDevice = nullptr; const bt_interface_t* sBtInterface = nullptr; static btmcap_test_interface_t* sMcapTestInterface = nullptr; static McapTestApp* sMcapTestApp = nullptr; @@ -353,23 +350,15 @@ static int create_cmdjob(char* cmd) { *******************************************************************************/ int HAL_load(void) { - int err = 0; - hw_module_t* module; - hw_device_t* device; LOG(INFO) << "Loading HAL library and extensions"; -#if defined(OS_GENERIC) - err = hal_util_load_bt_library((hw_module_t const**)&module); -#else - err = hw_get_module(BT_HARDWARE_MODULE_ID, (hw_module_t const**)&module); -#endif - if (!err) { - err = module->methods->open(module, BT_HARDWARE_MODULE_ID, &device); - if (!err) { - sBtDevice = (bluetooth_device_t*)device; - sBtInterface = sBtDevice->get_bluetooth_interface(); - } - } - LOG(INFO) << "HAL library loaded, status: " << strerror(err); + bt_interface_t* interface; + int err = hal_util_load_bt_library((const bt_interface_t**)&interface); + if (err) { + LOG(ERROR) << "Error loading HAL library: " << strerror(err); + return err; + } + sBtInterface = interface; + LOG(INFO) << "HAL library loaded"; return err; } -- 2.11.0