blob: 9cdbfe0dc0961cb59df04908072f4905ed92e1fa [file] [log] [blame]
Keun Soo Yimb2811ee2016-03-03 13:27:33 -08001/*
2 * Copyright (C) 2016 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 "component_loader/DllLoader.h"
18
19#include <stdlib.h>
20#include <stdio.h>
21#include <dlfcn.h>
22
23#include <iostream>
24
25#include "hardware/hardware.h"
26
27using namespace std;
28
29
30namespace android {
31namespace vts {
32
33
34DllLoader::DllLoader()
35 : handle_(NULL) {
36}
37
38
39DllLoader::~DllLoader() {
40 if (!handle_) {
41 dlclose(handle_);
42 handle_ = NULL;
43 }
44}
45
46
47void* DllLoader::Load(const char* file_path) {
48 if (!file_path) {
49 cerr << __FUNCTION__ << ": file_path is NULL" << endl;
50 return NULL;
51 }
52
53 // consider using the load mechanism in hardware/libhardware/hardware.c
54 handle_ = dlopen(file_path, RTLD_LAZY);
55 if (!handle_) {
56 fputs(dlerror(), stderr);
57 cerr << "Can't load a dll " << file_path << endl;
58 return NULL;
59 }
60 cout << "DLL loaded " << file_path << endl;
61 return handle_;
62}
63
64
Keun Soo Yim6d944952016-05-31 16:30:56 -070065struct hw_module_t* DllLoader::InitConventionalHal() {
Keun Soo Yimb2811ee2016-03-03 13:27:33 -080066 if (!handle_) {
67 cerr << __FUNCTION__ << ": handle_ is NULL" << endl;
68 return NULL;
69 }
Keun Soo Yim6d944952016-05-31 16:30:56 -070070 hmi_ = (struct hw_module_t*) dlsym(handle_, HAL_MODULE_INFO_SYM_AS_STR);
71 if (!hmi_) {
Keun Soo Yimb2811ee2016-03-03 13:27:33 -080072 cerr << __FUNCTION__ << ": " << HAL_MODULE_INFO_SYM_AS_STR
73 << " not found" << endl;
74 return NULL;
75 }
Keun Soo Yim34067de2016-05-17 09:46:37 -070076 cout << __FUNCTION__ << ":" << __LINE__ << endl;
Keun Soo Yim6d944952016-05-31 16:30:56 -070077 hmi_->dso = handle_;
Keun Soo Yimb2811ee2016-03-03 13:27:33 -080078 device_ = NULL;
Keun Soo Yim6d944952016-05-31 16:30:56 -070079 cout << __FUNCTION__ << ": version " << hmi_->module_api_version << endl;
80 return hmi_;
81}
Keun Soo Yim8e07a092016-05-04 16:30:35 -070082
Keun Soo Yim6d944952016-05-31 16:30:56 -070083
84struct hw_device_t* DllLoader::OpenConventionalHal(const char* module_name) {
85 cout << __func__ << " module_name " << module_name << endl;
86 if (!handle_) {
87 cerr << __FUNCTION__ << ": handle_ is NULL" << endl;
88 return NULL;
89 }
90 if (!hmi_) {
91 cerr << __FUNCTION__ << ": hmi_ is NULL" << endl;
92 return NULL;
93 }
94
95 device_ = NULL;
Keun Soo Yim34067de2016-05-17 09:46:37 -070096 int ret;
Keun Soo Yim6d944952016-05-31 16:30:56 -070097 if (module_name && strlen(module_name) > 0) {
Keun Soo Yim8e07a092016-05-04 16:30:35 -070098 cout << __FUNCTION__ << ":" << __LINE__ << ": module_name |"
99 << module_name << "|" << endl;
Keun Soo Yim6d944952016-05-31 16:30:56 -0700100 ret = hmi_->methods->open(
101 hmi_, module_name, (struct hw_device_t**) &device_);
102 } else {
103 cout << __FUNCTION__ << ":" << __LINE__ << ": (default) "
104 << hmi_->name << endl;
105 ret = hmi_->methods->open(hmi_, hmi_->name, (struct hw_device_t**) &device_);
Keun Soo Yim34067de2016-05-17 09:46:37 -0700106 }
107 if (ret != 0) {
108 cout << "returns " << ret << " " << strerror(errno) << endl;
109 }
Keun Soo Yim6d944952016-05-31 16:30:56 -0700110 cout << __FUNCTION__ << ":" << __LINE__ << " device_ " << device_ << endl;
Keun Soo Yimb2811ee2016-03-03 13:27:33 -0800111 return device_;
112}
113
114
115loader_function DllLoader::GetLoaderFunction(const char* function_name) {
116 const char* error;
117 loader_function func;
118
119 func = (loader_function) dlsym(handle_, function_name);
120 if ((error = dlerror()) != NULL) {
121 fputs(error, stderr);
122 cerr << __FUNCTION__ << ": Can't find " << function_name << endl;
123 return NULL;
124 }
125 return func;
126}
127
Keun Soo Yim8e07a092016-05-04 16:30:35 -0700128
Keun Soo Yim04431a92016-05-24 16:26:42 -0700129bool DllLoader::SancovResetCoverage() {
130 const char* error;
131 void (*func)();
132
133 func = (void (*)()) dlsym(handle_, "__sanitizer_reset_coverage");
134 if ((error = dlerror()) != NULL) {
135 fputs(error, stderr);
136 cerr << __FUNCTION__ << ": Can't find __sanitizer_reset_coverage" << endl;
137 return false;
138 }
139 func();
140 return true;
141}
142
143
144bool DllLoader::GcovInit(writeout_fn wfn, flush_fn ffn) {
145 const char* error;
146 void (*func)(writeout_fn, flush_fn);
147
148 func = (void (*)(writeout_fn, flush_fn)) dlsym(handle_, "llvm_gcov_init");
149 if ((error = dlerror()) != NULL) {
150 fputs(error, stderr);
151 cerr << __FUNCTION__ << ": Can't find llvm_gcov_init" << endl;
152 return false;
153 }
154 func(wfn, ffn);
155 return true;
156}
157
158
159bool DllLoader::GcovFlush() {
160 const char* error;
161 void (*func)();
162
163 func = (void (*)()) dlsym(handle_, "__gcov_flush");
164 if ((error = dlerror()) != NULL) {
165 fputs(error, stderr);
166 cerr << __FUNCTION__ << ": Can't find __gcov_flush" << endl;
167 return false;
168 }
169 func();
170
171 return true;
172}
173
Keun Soo Yimb2811ee2016-03-03 13:27:33 -0800174} // namespace vts
175} // namespace android