1*5c4dab75SAndroid Build Coastguard Worker /*
2*5c4dab75SAndroid Build Coastguard Worker * Copyright (C) 2017 The Android Open Source Project
3*5c4dab75SAndroid Build Coastguard Worker *
4*5c4dab75SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*5c4dab75SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*5c4dab75SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*5c4dab75SAndroid Build Coastguard Worker *
8*5c4dab75SAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*5c4dab75SAndroid Build Coastguard Worker *
10*5c4dab75SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*5c4dab75SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*5c4dab75SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*5c4dab75SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*5c4dab75SAndroid Build Coastguard Worker * limitations under the License.
15*5c4dab75SAndroid Build Coastguard Worker *
16*5c4dab75SAndroid Build Coastguard Worker */
17*5c4dab75SAndroid Build Coastguard Worker
18*5c4dab75SAndroid Build Coastguard Worker #include <dlfcn.h>
19*5c4dab75SAndroid Build Coastguard Worker #include <stdint.h>
20*5c4dab75SAndroid Build Coastguard Worker #include <stdio.h>
21*5c4dab75SAndroid Build Coastguard Worker #include <string.h>
22*5c4dab75SAndroid Build Coastguard Worker #include <unistd.h>
23*5c4dab75SAndroid Build Coastguard Worker
24*5c4dab75SAndroid Build Coastguard Worker #define LOG_TAG "ese-sh"
25*5c4dab75SAndroid Build Coastguard Worker #include <ese/ese.h>
26*5c4dab75SAndroid Build Coastguard Worker #include <ese/log.h>
27*5c4dab75SAndroid Build Coastguard Worker
28*5c4dab75SAndroid Build Coastguard Worker #include "hw.h"
29*5c4dab75SAndroid Build Coastguard Worker
print_supported_hardware(const struct SupportedHardware * supported)30*5c4dab75SAndroid Build Coastguard Worker void print_supported_hardware(const struct SupportedHardware *supported) {
31*5c4dab75SAndroid Build Coastguard Worker printf("Supported hardware:\n");
32*5c4dab75SAndroid Build Coastguard Worker for (size_t i = 0; i < supported->len; ++i) {
33*5c4dab75SAndroid Build Coastguard Worker printf("\t%s\t(%s / %s)\n", supported->hw[i].name, supported->hw[i].sym,
34*5c4dab75SAndroid Build Coastguard Worker supported->hw[i].lib);
35*5c4dab75SAndroid Build Coastguard Worker }
36*5c4dab75SAndroid Build Coastguard Worker }
37*5c4dab75SAndroid Build Coastguard Worker
find_supported_hardware(const struct SupportedHardware * supported,const char * name)38*5c4dab75SAndroid Build Coastguard Worker int find_supported_hardware(const struct SupportedHardware *supported,
39*5c4dab75SAndroid Build Coastguard Worker const char *name) {
40*5c4dab75SAndroid Build Coastguard Worker for (size_t i = 0; i < supported->len; ++i) {
41*5c4dab75SAndroid Build Coastguard Worker if (!strcmp(name, supported->hw[i].name)) {
42*5c4dab75SAndroid Build Coastguard Worker return (int)i;
43*5c4dab75SAndroid Build Coastguard Worker }
44*5c4dab75SAndroid Build Coastguard Worker }
45*5c4dab75SAndroid Build Coastguard Worker return -1;
46*5c4dab75SAndroid Build Coastguard Worker }
47*5c4dab75SAndroid Build Coastguard Worker
48*5c4dab75SAndroid Build Coastguard Worker /* This should be C++. */
release_hardware(const struct Hardware * hw)49*5c4dab75SAndroid Build Coastguard Worker void release_hardware(const struct Hardware *hw) {
50*5c4dab75SAndroid Build Coastguard Worker void *hw_handle = dlopen(hw->lib, RTLD_NOW);
51*5c4dab75SAndroid Build Coastguard Worker /* Close for the above open and the original. */
52*5c4dab75SAndroid Build Coastguard Worker dlclose(hw_handle);
53*5c4dab75SAndroid Build Coastguard Worker dlclose(hw_handle);
54*5c4dab75SAndroid Build Coastguard Worker }
55*5c4dab75SAndroid Build Coastguard Worker
initialize_hardware(struct EseInterface * ese,const struct Hardware * hw)56*5c4dab75SAndroid Build Coastguard Worker bool initialize_hardware(struct EseInterface *ese, const struct Hardware *hw) {
57*5c4dab75SAndroid Build Coastguard Worker void *hw_handle = dlopen(hw->lib, RTLD_NOW);
58*5c4dab75SAndroid Build Coastguard Worker if (!hw_handle) {
59*5c4dab75SAndroid Build Coastguard Worker fprintf(stderr, "Failed to open hardware implementation: %s\n", dlerror());
60*5c4dab75SAndroid Build Coastguard Worker return false;
61*5c4dab75SAndroid Build Coastguard Worker }
62*5c4dab75SAndroid Build Coastguard Worker const struct EseOperations **hw_ops = dlsym(hw_handle, hw->sym);
63*5c4dab75SAndroid Build Coastguard Worker if (!hw_ops) {
64*5c4dab75SAndroid Build Coastguard Worker fprintf(stderr, "Failed to find hardware implementation: %s\n", dlerror());
65*5c4dab75SAndroid Build Coastguard Worker return false;
66*5c4dab75SAndroid Build Coastguard Worker }
67*5c4dab75SAndroid Build Coastguard Worker /* N.b., ese_init appends _ops to the second argument. */
68*5c4dab75SAndroid Build Coastguard Worker ese_init(ese, *hw);
69*5c4dab75SAndroid Build Coastguard Worker return true;
70*5c4dab75SAndroid Build Coastguard Worker }
71