blob: a13745ed4965f6cc28c2d60da99bc200adb39957 [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 Yim34067de2016-05-17 09:46:37 -070065struct hw_device_t* DllLoader::GetHWDevice(const char* module_name) {
Keun Soo Yimb2811ee2016-03-03 13:27:33 -080066 if (!handle_) {
67 cerr << __FUNCTION__ << ": handle_ is NULL" << endl;
68 return NULL;
69 }
Keun Soo Yim34067de2016-05-17 09:46:37 -070070 cout << __FUNCTION__ << ":" << __LINE__ << endl;
Keun Soo Yimb2811ee2016-03-03 13:27:33 -080071 struct hw_module_t *hmi = (struct hw_module_t *) dlsym(
72 handle_, HAL_MODULE_INFO_SYM_AS_STR);
73 if (!hmi) {
74 cerr << __FUNCTION__ << ": " << HAL_MODULE_INFO_SYM_AS_STR
75 << " not found" << endl;
76 return NULL;
77 }
Keun Soo Yim34067de2016-05-17 09:46:37 -070078 cout << __FUNCTION__ << ":" << __LINE__ << endl;
Keun Soo Yimb2811ee2016-03-03 13:27:33 -080079 hmi->dso = handle_;
80 device_ = NULL;
Keun Soo Yim34067de2016-05-17 09:46:37 -070081 int ret;
82 if (!module_name || strlen(module_name) == 0) {
83 cout << __FUNCTION__ << ":" << __LINE__ << ": " << hmi->name << endl;
84 ret = hmi->methods->open(hmi, hmi->name,
85 (struct hw_device_t**) &device_);
86 } else {
87 cout << __FUNCTION__ << ":" << __LINE__ << ": module_name |" << module_name << "|" << endl;
88 ret = hmi->methods->open(hmi, module_name,
89 (struct hw_device_t**) &device_);
90 }
91 if (ret != 0) {
92 cout << "returns " << ret << " " << strerror(errno) << endl;
93 }
94 cout << __FUNCTION__ << ":" << __LINE__ << endl;
Keun Soo Yimb2811ee2016-03-03 13:27:33 -080095 return device_;
96}
97
98
99loader_function DllLoader::GetLoaderFunction(const char* function_name) {
100 const char* error;
101 loader_function func;
102
103 func = (loader_function) dlsym(handle_, function_name);
104 if ((error = dlerror()) != NULL) {
105 fputs(error, stderr);
106 cerr << __FUNCTION__ << ": Can't find " << function_name << endl;
107 return NULL;
108 }
109 return func;
110}
111
112} // namespace vts
113} // namespace android