1 /******************************************************************************
2  *
3  *  Copyright 2019 NXP
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 #include "eSEClientExtns.h"
19 #include <dlfcn.h>
20 #include <hidl/LegacySupport.h>
21 #include <unistd.h>
22 
23 void* HalLibnfc_handle = NULL;
24 
25 /*******************************************************************************
26 **
27 ** Function         initializeEseClient()
28 **
29 ** Description      Loads the module dynamically to access the functions.
30 **
31 ** Parameters       none
32 **
33 ** Returns          void
34 *******************************************************************************/
initializeEseClient()35 void initializeEseClient() {
36   // Getting pointer to Ese Client  module
37   HalLibnfc_handle = dlopen("/system/vendor/lib64/hal_libnfc.so", RTLD_NOW);
38 
39   if (HalLibnfc_handle == NULL) {
40     /*fail to load the library*/
41     ALOGE("Error : opening (/system/vendor/lib64/hal_libnfc.so) !!");
42     return;
43   }
44 }
45 /*******************************************************************************
46 **
47 ** Function         checkEseClientUpdate()
48 **
49 ** Description      Interface API to dynamically invoke  checkEseClientUpdate
50 **                  method.
51 **
52 ** Parameters       none
53 **
54 ** Returns          void
55 *******************************************************************************/
checkEseClientUpdate()56 void checkEseClientUpdate() {
57   if (HalLibnfc_handle == NULL) return;
58 
59   fpCheckEseClientUpdate_t fpCheckEseClientUpdate = NULL;
60 
61   /*find the address of function and data objects*/
62   fpCheckEseClientUpdate =
63       (fpCheckEseClientUpdate_t)dlsym(HalLibnfc_handle, "checkEseClientUpdate");
64 
65   if (!fpCheckEseClientUpdate) {
66     ALOGE("Error while linking (checkEseClientUpdate) %s!!", dlerror());
67   } else {
68     ALOGD("Success: while linking (checkEseClientUpdate) !!");
69     /* invoke function*/
70     fpCheckEseClientUpdate();
71   }
72 }
73 /*******************************************************************************
74 **
75 ** Function         perform_eSEClientUpdate()
76 **
77 ** Description      Interface API to dynamically invoke  perform_eSEClientUpdate
78 **                  method.
79 **
80 ** Parameters       none
81 **
82 ** Returns          void
83 *******************************************************************************/
perform_eSEClientUpdate()84 void perform_eSEClientUpdate() {
85   if (HalLibnfc_handle == NULL) return;
86 
87   fpPerformEseClientUpdate_t fpPerformEseClientUpdate = NULL;
88 
89   /*find the address of function and data objects*/
90   fpPerformEseClientUpdate = (fpPerformEseClientUpdate_t)dlsym(
91       HalLibnfc_handle, "perform_eSEClientUpdate");
92 
93   if (!fpPerformEseClientUpdate) {
94     ALOGE("Error while linking (perform_eSEClientUpdate) !!");
95   } else {
96     ALOGD("Success while linking (perform_eSEClientUpdate) !!");
97     fpPerformEseClientUpdate();
98   }
99 }
100 /*******************************************************************************
101 **
102 ** Function         eSEClientUpdate_NFC_Thread
103 **
104 ** Description      Interface API to dynamically invoke
105 *eSEClientUpdate_NFC_Thread
106 **                  method.
107 **
108 ** Parameters       none
109 **
110 ** Returns          void
111 *******************************************************************************/
eSEClientUpdate_NFC_Thread()112 void eSEClientUpdate_NFC_Thread() {
113   if (HalLibnfc_handle == NULL) return;
114 
115   fpEseClientUpdate_Nfc_Thread_t fpEseClientUpdate_Nfc_Thread = NULL;
116 
117   /*find the address of function and data objects*/
118   fpEseClientUpdate_Nfc_Thread = (fpEseClientUpdate_Nfc_Thread_t)dlsym(
119       HalLibnfc_handle, "eSEClientUpdate_NFC_Thread");
120 
121   if (!fpEseClientUpdate_Nfc_Thread) {
122     ALOGE("Error while linking (eSEClientUpdate_NFC_Thread) !!");
123   } else {
124     ALOGD("Success while linking (eSEClientUpdate_NFC_Thread) !!");
125     /* invoke function*/
126     fpEseClientUpdate_Nfc_Thread();
127   }
128 }
129 /*******************************************************************************
130 **
131 ** Function         seteSEClientState
132 **
133 ** Description      Interface API to dynamically invoke  seteSEClientState
134 **                  method.
135 **
136 ** Parameters       int
137 **
138 ** Returns          none
139 *******************************************************************************/
seteSEClientState(uint8_t state)140 void seteSEClientState(uint8_t state) {
141   if (HalLibnfc_handle == NULL) return;
142 
143   fpSeteSEClientState_t fpSeteSEClientState = NULL;
144 
145   ALOGD("seteSEClientState state %d", state);
146 
147   /*find the address of function and data objects*/
148   fpSeteSEClientState =
149       (fpSeteSEClientState_t)dlsym(HalLibnfc_handle, "seteSEClientState");
150 
151   if (!fpSeteSEClientState) {
152     ALOGE("Error while linking (seteSEClientState) !!");
153   } else {
154     ALOGD("Success while linking (seteSEClientState) !!");
155     /* invoke function, passing value of integer as a parameter */
156     fpSeteSEClientState(state);
157   }
158 }
159 /*******************************************************************************
160 **
161 ** Function         deinitializeEseClient(void)
162 **
163 ** Description      Resets the module handle & all the function pointers
164 **
165 ** Parameters       none
166 **
167 ** Returns          void
168 *******************************************************************************/
deinitializeEseClient()169 void deinitializeEseClient() {
170   if (HalLibnfc_handle != NULL) {
171     ALOGD("closing hal_libnfc.so");
172     dlclose(HalLibnfc_handle);
173     HalLibnfc_handle = NULL;
174   }
175 }
176