blob: 6322ff303a0bea3bd7f3ec92d6688917ab3dae9e [file] [log] [blame]
Shraddha Basantwani6545b4e2022-09-21 16:26:19 +05301/*
2 * Copyright (C) 2022 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#define LOG_TAG "android.hardware.cas-SharedLibrary"
18
19#include "SharedLibrary.h"
20#include <dlfcn.h>
21#include <utils/Log.h>
Jooyung Han5a3d5882023-09-14 13:31:43 +000022#include <vndksupport/linker.h>
Shraddha Basantwani6545b4e2022-09-21 16:26:19 +053023
24namespace aidl {
25namespace android {
26namespace hardware {
27namespace cas {
28
29SharedLibrary::SharedLibrary(const String8& path) {
Jooyung Han5a3d5882023-09-14 13:31:43 +000030 mLibHandle = android_load_sphal_library(path.c_str(), RTLD_NOW);
Shraddha Basantwani6545b4e2022-09-21 16:26:19 +053031}
32
33SharedLibrary::~SharedLibrary() {
34 if (mLibHandle != NULL) {
Jooyung Han5a3d5882023-09-14 13:31:43 +000035 android_unload_sphal_library(mLibHandle);
Shraddha Basantwani6545b4e2022-09-21 16:26:19 +053036 mLibHandle = NULL;
37 }
38}
39
40bool SharedLibrary::operator!() const {
41 return mLibHandle == NULL;
42}
43
44void* SharedLibrary::lookup(const char* symbol) const {
45 if (!mLibHandle) {
46 return NULL;
47 }
48 // Clear last error before we load the symbol again,
49 // in case the caller didn't retrieve it.
50 (void)dlerror();
51 return dlsym(mLibHandle, symbol);
52}
53
54const char* SharedLibrary::lastError() const {
55 const char* error = dlerror();
56 return error ? error : "No errors or unknown error";
57}
58
59} // namespace cas
60} // namespace hardware
61} // namespace android
62} // namespace aidl