1 /*
2 * Copyright (C) 2017 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
18 #include <dlfcn.h>
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <unistd.h>
23
24 #define LOG_TAG "ese-sh"
25 #include <ese/ese.h>
26 #include <ese/log.h>
27
28 #include "hw.h"
29
print_supported_hardware(const struct SupportedHardware * supported)30 void print_supported_hardware(const struct SupportedHardware *supported) {
31 printf("Supported hardware:\n");
32 for (size_t i = 0; i < supported->len; ++i) {
33 printf("\t%s\t(%s / %s)\n", supported->hw[i].name, supported->hw[i].sym,
34 supported->hw[i].lib);
35 }
36 }
37
find_supported_hardware(const struct SupportedHardware * supported,const char * name)38 int find_supported_hardware(const struct SupportedHardware *supported,
39 const char *name) {
40 for (size_t i = 0; i < supported->len; ++i) {
41 if (!strcmp(name, supported->hw[i].name)) {
42 return (int)i;
43 }
44 }
45 return -1;
46 }
47
48 /* This should be C++. */
release_hardware(const struct Hardware * hw)49 void release_hardware(const struct Hardware *hw) {
50 void *hw_handle = dlopen(hw->lib, RTLD_NOW);
51 /* Close for the above open and the original. */
52 dlclose(hw_handle);
53 dlclose(hw_handle);
54 }
55
initialize_hardware(struct EseInterface * ese,const struct Hardware * hw)56 bool initialize_hardware(struct EseInterface *ese, const struct Hardware *hw) {
57 void *hw_handle = dlopen(hw->lib, RTLD_NOW);
58 if (!hw_handle) {
59 fprintf(stderr, "Failed to open hardware implementation: %s\n", dlerror());
60 return false;
61 }
62 const struct EseOperations **hw_ops = dlsym(hw_handle, hw->sym);
63 if (!hw_ops) {
64 fprintf(stderr, "Failed to find hardware implementation: %s\n", dlerror());
65 return false;
66 }
67 /* N.b., ese_init appends _ops to the second argument. */
68 ese_init(ese, *hw);
69 return true;
70 }
71