2 // Copyright (C) 2015 Google, Inc.
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at:
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
17 #define LOG_TAG "hal_util"
19 #include <hardware/bluetooth.h>
20 #include <hardware/hardware.h>
26 #include "btcore/include/hal_util.h"
27 #include "osi/include/log.h"
29 #if defined(OS_GENERIC)
31 // TODO(armansito): All logging macros should include __func__ by default (see
33 #define HULOGERR(fmt, args...) \
34 LOG_ERROR(LOG_TAG, "[%s] failed to load the Bluetooth library: " fmt, \
37 // TODO(armansito): It might be better to pass the library name in a more
38 // generic manner as opposed to hard-coding it here.
39 static const char kBluetoothLibraryName[] = "libbluetooth.default.so";
41 static int load_bt_library(const struct hw_module_t **module) {
42 const char *id = BT_STACK_MODULE_ID;
44 // Always try to load the default Bluetooth stack on GN builds.
45 void *handle = dlopen(kBluetoothLibraryName, RTLD_NOW);
47 char const *err_str = dlerror();
48 HULOGERR("%s", err_str ? err_str : "error unknown");
52 // Get the address of the struct hal_module_info.
53 const char *sym = HAL_MODULE_INFO_SYM_AS_STR;
54 struct hw_module_t *hmi = (struct hw_module_t *)dlsym(handle, sym);
60 // Check that the id matches.
61 if (strcmp(id, hmi->id) != 0) {
62 HULOGERR("id=%s does not match HAL module ID: %s", id, hmi->id);
70 LOG_TAG, "[%s] loaded HAL id=%s path=%s hmi=%p handle=%p",
71 __func__, id, kBluetoothLibraryName, hmi, handle);
84 #endif // defined(OS_GENERIC)
86 int hal_util_load_bt_library(const struct hw_module_t **module) {
87 #if defined(OS_GENERIC)
88 return load_bt_library(module);
89 #else // !defined(OS_GENERIC)
90 return hw_get_module(BT_STACK_MODULE_ID, module);
91 #endif // defined(OS_GENERIC)