• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //  Copyright 2015 Google, Inc.
3 //
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 
19 #include <base/logging.h>
20 #include <base/strings/stringprintf.h>
21 #include <hardware/bluetooth.h>
22 
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 
30 using base::StringPrintf;
31 
32 #define BLUETOOTH_LIBRARY_NAME "libbluetooth.so"
33 
hal_util_load_bt_library(const bt_interface_t ** interface)34 int hal_util_load_bt_library(const bt_interface_t** interface) {
35   const char* sym = BLUETOOTH_INTERFACE_STRING;
36   bt_interface_t* itf = nullptr;
37 
38   // Always try to load the default Bluetooth stack on GN builds.
39   void* handle = dlopen(BLUETOOTH_LIBRARY_NAME, RTLD_NOW);
40   if (!handle) {
41     const char* err_str = dlerror();
42     LOG(ERROR) << __func__ << ": failed to load bluetooth library, error="
43                << (err_str ? err_str : "error unknown");
44     goto error;
45   }
46 
47   // Get the address of the bt_interface_t.
48   itf = (bt_interface_t*)dlsym(handle, sym);
49   if (!itf) {
50     LOG(ERROR) << __func__ << ": failed to load symbol from Bluetooth library "
51                << sym;
52     goto error;
53   }
54 
55   // Success.
56   LOG(INFO) << __func__ << " loaded HAL path=" << BLUETOOTH_LIBRARY_NAME
57             << " btinterface=" << itf << " handle=" << handle;
58 
59   *interface = itf;
60   return 0;
61 
62 error:
63   *interface = NULL;
64   if (handle) dlclose(handle);
65 
66   return -EINVAL;
67 }
68