blob: dd8a8068b41fc20ba22db14c9412715d3d2e1806 [file] [log] [blame]
Orion Hodson9b16e342019-10-09 13:29:16 +01001/*
2 * Copyright (C) 2019 The Android Open Source Project
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#include "nativebridge/native_bridge.h"
18#define LOG_TAG "nativebridge"
19
20#include <dlfcn.h>
21#include <errno.h>
22#include <string.h>
23
24#include <log/log.h>
25
26namespace android {
27
28namespace {
29
30void* GetLibHandle() {
31 static void* handle = dlopen("libnativebridge.so", RTLD_NOW);
32 LOG_FATAL_IF(handle == nullptr, "Failed to load libnativebridge.so: %s", dlerror());
33 return handle;
34}
35
36template <typename FuncPtr>
37FuncPtr GetFuncPtr(const char* function_name) {
38 auto f = reinterpret_cast<FuncPtr>(dlsym(GetLibHandle(), function_name));
39 LOG_FATAL_IF(f == nullptr, "Failed to get address of %s: %s", function_name, dlerror());
40 return f;
41}
42
Chih-Hung Hsieh310432e2020-03-04 10:58:29 -080043#define GET_FUNC_PTR(name) GetFuncPtr<decltype(&(name))>(#name)
Orion Hodson9b16e342019-10-09 13:29:16 +010044
45} // namespace
46
Orion Hodson9b16e342019-10-09 13:29:16 +010047bool NeedsNativeBridge(const char* instruction_set) {
48 static auto f = GET_FUNC_PTR(NeedsNativeBridge);
49 return f(instruction_set);
50}
51
52bool PreInitializeNativeBridge(const char* app_data_dir, const char* instruction_set) {
53 static auto f = GET_FUNC_PTR(PreInitializeNativeBridge);
54 return f(app_data_dir, instruction_set);
55}
56
Orion Hodson9b16e342019-10-09 13:29:16 +010057bool NativeBridgeAvailable() {
58 static auto f = GET_FUNC_PTR(NativeBridgeAvailable);
59 return f();
60}
61
62bool NativeBridgeInitialized() {
63 static auto f = GET_FUNC_PTR(NativeBridgeInitialized);
64 return f();
65}
66
Orion Hodson9b16e342019-10-09 13:29:16 +010067void* NativeBridgeGetTrampoline(void* handle, const char* name, const char* shorty, uint32_t len) {
68 static auto f = GET_FUNC_PTR(NativeBridgeGetTrampoline);
69 return f(handle, name, shorty, len);
70}
71
Orion Hodson9b16e342019-10-09 13:29:16 +010072const char* NativeBridgeGetError() {
73 static auto f = GET_FUNC_PTR(NativeBridgeGetError);
74 return f();
75}
76
Orion Hodson9b16e342019-10-09 13:29:16 +010077#undef GET_FUNC_PTR
78
79} // namespace android