Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fe458a08 authored by Jack He's avatar Jack He
Browse files

Fix net_test_bluetooth

* net_test_bluetooth should try to load Bluetooth library from
  system/lib/hw or system/lib64/hw if it cannot find it from default
  path
* Also replaced legacy logging macro with libbase logging statements

Bug: 67059247
Test: net_test_bluetooth
Change-Id: I6c45f66aedf675397f377ca56203bec9708324e1
parent 72a9441d
Loading
Loading
Loading
Loading
+29 −16
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

#define LOG_TAG "hal_util"

#include <base/logging.h>
#include <base/strings/stringprintf.h>
#include <hardware/bluetooth.h>
#include <hardware/hardware.h>

@@ -26,15 +28,14 @@
#include "btcore/include/hal_util.h"
#include "osi/include/log.h"

// TODO(armansito): All logging macros should include __func__ by default (see
// Bug: 22671731)
#define HULOGERR(fmt, args...)                                          \
  LOG_ERROR(LOG_TAG, "[%s] failed to load the Bluetooth library: " fmt, \
            __func__, ##args)
using base::StringPrintf;

// TODO(armansito): It might be better to pass the library name in a more
// generic manner as opposed to hard-coding it here.
static const char kBluetoothLibraryName[] = "bluetooth.default.so";
#define BLUETOOTH_LIBRARY_NAME "bluetooth.default.so"
#if defined(__LP64__)
#define BACKUP_PATH "/system/lib64/hw/" BLUETOOTH_LIBRARY_NAME
#else
#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;
@@ -42,31 +43,43 @@ int hal_util_load_bt_library(const struct hw_module_t** module) {
  struct hw_module_t* hmi = nullptr;

  // Always try to load the default Bluetooth stack on GN builds.
  void* handle = dlopen(kBluetoothLibraryName, RTLD_NOW);
  const char* path = BLUETOOTH_LIBRARY_NAME;
  void* handle = dlopen(path, RTLD_NOW);
  if (!handle) {
    char const* err_str = dlerror();
    HULOGERR("%s", err_str ? err_str : "error unknown");
    const char* err_str = dlerror();
    LOG(WARNING) << __func__ << ": failed to load Bluetooth library " << path
                 << ", error=" << (err_str ? err_str : "error unknown");
    path = BACKUP_PATH;
    LOG(WARNING) << __func__ << ": loading backup path " << path;
    handle = dlopen(path, RTLD_NOW);
    if (!handle) {
      err_str = dlerror();
      LOG(ERROR) << __func__ << ": failed to load Bluetooth library " << path
                 << ", error=" << (err_str ? err_str : "error unknown");
      goto error;
    }
  }

  // Get the address of the struct hal_module_info.
  hmi = (struct hw_module_t*)dlsym(handle, sym);
  if (!hmi) {
    HULOGERR("%s", sym);
    LOG(ERROR) << __func__ << ": failed to load symbol from Bluetooth library "
               << sym;
    goto error;
  }

  // Check that the id matches.
  if (strcmp(id, hmi->id) != 0) {
    HULOGERR("id=%s does not match HAL module ID: %s", id, hmi->id);
    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(LOG_TAG, "[%s] loaded HAL id=%s path=%s hmi=%p handle=%p", __func__,
           id, kBluetoothLibraryName, hmi, handle);
  LOG(INFO) << StringPrintf("%s: loaded HAL id=%s path=%s hmi=%p handle=%p",
                            __func__, id, path, hmi, handle);

  *module = hmi;
  return 0;