Arman Uguray | f2d6434 | 2015-07-08 15:47:39 -0700 | [diff] [blame] | 1 | // |
Jakub Pawlowski | 5b790fe | 2017-09-18 09:00:20 -0700 | [diff] [blame] | 2 | // Copyright 2015 Google, Inc. |
Arman Uguray | f2d6434 | 2015-07-08 15:47:39 -0700 | [diff] [blame] | 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 | |
Jack He | 1611b05 | 2017-10-13 18:23:05 -0700 | [diff] [blame] | 19 | #include <base/logging.h> |
| 20 | #include <base/strings/stringprintf.h> |
Arman Uguray | f2d6434 | 2015-07-08 15:47:39 -0700 | [diff] [blame] | 21 | #include <hardware/bluetooth.h> |
Arman Uguray | f2d6434 | 2015-07-08 15:47:39 -0700 | [diff] [blame] | 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 | |
Jack He | 1611b05 | 2017-10-13 18:23:05 -0700 | [diff] [blame] | 30 | using base::StringPrintf; |
Arman Uguray | f2d6434 | 2015-07-08 15:47:39 -0700 | [diff] [blame] | 31 | |
Jakub Pawlowski | e56fabc | 2017-10-19 00:26:09 -0700 | [diff] [blame] | 32 | #define BLUETOOTH_LIBRARY_NAME "libbluetooth.so" |
Arman Uguray | f2d6434 | 2015-07-08 15:47:39 -0700 | [diff] [blame] | 33 | |
Jakub Pawlowski | 2d262d5 | 2017-10-16 15:57:16 -0700 | [diff] [blame] | 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; |
Arman Uguray | f2d6434 | 2015-07-08 15:47:39 -0700 | [diff] [blame] | 37 | |
| 38 | // Always try to load the default Bluetooth stack on GN builds. |
Jakub Pawlowski | e56fabc | 2017-10-19 00:26:09 -0700 | [diff] [blame] | 39 | void* handle = dlopen(BLUETOOTH_LIBRARY_NAME, RTLD_NOW); |
Arman Uguray | f2d6434 | 2015-07-08 15:47:39 -0700 | [diff] [blame] | 40 | if (!handle) { |
Jack He | 1611b05 | 2017-10-13 18:23:05 -0700 | [diff] [blame] | 41 | const char* err_str = dlerror(); |
Jakub Pawlowski | e56fabc | 2017-10-19 00:26:09 -0700 | [diff] [blame] | 42 | LOG(ERROR) << __func__ << ": failed to load bluetooth library, error=" |
| 43 | << (err_str ? err_str : "error unknown"); |
| 44 | goto error; |
Arman Uguray | f2d6434 | 2015-07-08 15:47:39 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Jakub Pawlowski | 2d262d5 | 2017-10-16 15:57:16 -0700 | [diff] [blame] | 47 | // Get the address of the bt_interface_t. |
| 48 | itf = (bt_interface_t*)dlsym(handle, sym); |
| 49 | if (!itf) { |
Jack He | 1611b05 | 2017-10-13 18:23:05 -0700 | [diff] [blame] | 50 | LOG(ERROR) << __func__ << ": failed to load symbol from Bluetooth library " |
| 51 | << sym; |
Arman Uguray | f2d6434 | 2015-07-08 15:47:39 -0700 | [diff] [blame] | 52 | goto error; |
| 53 | } |
| 54 | |
Arman Uguray | f2d6434 | 2015-07-08 15:47:39 -0700 | [diff] [blame] | 55 | // Success. |
Jakub Pawlowski | 2d262d5 | 2017-10-16 15:57:16 -0700 | [diff] [blame] | 56 | LOG(INFO) << __func__ << " loaded HAL path=" << BLUETOOTH_LIBRARY_NAME |
| 57 | << " btinterface=" << itf << " handle=" << handle; |
Arman Uguray | f2d6434 | 2015-07-08 15:47:39 -0700 | [diff] [blame] | 58 | |
Jakub Pawlowski | 2d262d5 | 2017-10-16 15:57:16 -0700 | [diff] [blame] | 59 | *interface = itf; |
Arman Uguray | f2d6434 | 2015-07-08 15:47:39 -0700 | [diff] [blame] | 60 | return 0; |
| 61 | |
| 62 | error: |
Jakub Pawlowski | 2d262d5 | 2017-10-16 15:57:16 -0700 | [diff] [blame] | 63 | *interface = NULL; |
Myles Watson | 911d1ae | 2016-11-28 16:44:40 -0800 | [diff] [blame] | 64 | if (handle) dlclose(handle); |
Arman Uguray | f2d6434 | 2015-07-08 15:47:39 -0700 | [diff] [blame] | 65 | |
| 66 | return -EINVAL; |
| 67 | } |