blob: d0433486fac9a0ffebb38fceac25a20730a2c905 [file] [log] [blame]
Arman Ugurayf2d64342015-07-08 15:47:39 -07001//
Jakub Pawlowski5b790fe2017-09-18 09:00:20 -07002// Copyright 2015 Google, Inc.
Arman Ugurayf2d64342015-07-08 15:47:39 -07003//
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:
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
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.
15//
16
17#define LOG_TAG "hal_util"
18
Jack He1611b052017-10-13 18:23:05 -070019#include <base/logging.h>
20#include <base/strings/stringprintf.h>
Arman Ugurayf2d64342015-07-08 15:47:39 -070021#include <hardware/bluetooth.h>
Arman Ugurayf2d64342015-07-08 15:47:39 -070022
23#include <dlfcn.h>
24#include <errno.h>
25#include <string.h>
26
27#include "btcore/include/hal_util.h"
28#include "osi/include/log.h"
29
Jack He1611b052017-10-13 18:23:05 -070030using base::StringPrintf;
Arman Ugurayf2d64342015-07-08 15:47:39 -070031
Jakub Pawlowskie56fabc2017-10-19 00:26:09 -070032#define BLUETOOTH_LIBRARY_NAME "libbluetooth.so"
Arman Ugurayf2d64342015-07-08 15:47:39 -070033
Jakub Pawlowski2d262d52017-10-16 15:57:16 -070034int hal_util_load_bt_library(const bt_interface_t** interface) {
35 const char* sym = BLUETOOTH_INTERFACE_STRING;
36 bt_interface_t* itf = nullptr;
Arman Ugurayf2d64342015-07-08 15:47:39 -070037
38 // Always try to load the default Bluetooth stack on GN builds.
Jakub Pawlowskie56fabc2017-10-19 00:26:09 -070039 void* handle = dlopen(BLUETOOTH_LIBRARY_NAME, RTLD_NOW);
Arman Ugurayf2d64342015-07-08 15:47:39 -070040 if (!handle) {
Jack He1611b052017-10-13 18:23:05 -070041 const char* err_str = dlerror();
Jakub Pawlowskie56fabc2017-10-19 00:26:09 -070042 LOG(ERROR) << __func__ << ": failed to load bluetooth library, error="
43 << (err_str ? err_str : "error unknown");
44 goto error;
Arman Ugurayf2d64342015-07-08 15:47:39 -070045 }
46
Jakub Pawlowski2d262d52017-10-16 15:57:16 -070047 // Get the address of the bt_interface_t.
48 itf = (bt_interface_t*)dlsym(handle, sym);
49 if (!itf) {
Jack He1611b052017-10-13 18:23:05 -070050 LOG(ERROR) << __func__ << ": failed to load symbol from Bluetooth library "
51 << sym;
Arman Ugurayf2d64342015-07-08 15:47:39 -070052 goto error;
53 }
54
Arman Ugurayf2d64342015-07-08 15:47:39 -070055 // Success.
Jakub Pawlowski2d262d52017-10-16 15:57:16 -070056 LOG(INFO) << __func__ << " loaded HAL path=" << BLUETOOTH_LIBRARY_NAME
57 << " btinterface=" << itf << " handle=" << handle;
Arman Ugurayf2d64342015-07-08 15:47:39 -070058
Jakub Pawlowski2d262d52017-10-16 15:57:16 -070059 *interface = itf;
Arman Ugurayf2d64342015-07-08 15:47:39 -070060 return 0;
61
62error:
Jakub Pawlowski2d262d52017-10-16 15:57:16 -070063 *interface = NULL;
Myles Watson911d1ae2016-11-28 16:44:40 -080064 if (handle) dlclose(handle);
Arman Ugurayf2d64342015-07-08 15:47:39 -070065
66 return -EINVAL;
67}