1 /******************************************************************************
2  *
3  *  Copyright 2018-2020 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 #define LOG_TAG "NxpEseHal-NfcAdaptation"
19 #include "NfcAdaptation.h"
20 #include <android/hardware/nfc/1.0/types.h>
21 #include <hwbinder/ProcessState.h>
22 #include <log/log.h>
23 #include <pthread.h>
24 
25 #undef LOG_TAG
26 #define LOG_TAG "SpiAdaptation"
27 
28 using android::OK;
29 using android::sp;
30 using android::status_t;
31 
32 using android::hardware::hidl_vec;
33 using android::hardware::ProcessState;
34 using android::hardware::Return;
35 using android::hardware::Void;
36 using vendor::nxp::nxpnfc::V2_0::INxpNfc;
37 
38 sp<INxpNfc> NfcAdaptation::mHalNxpNfc = nullptr;
39 ThreadMutex NfcAdaptation::sIoctlLock;
40 NfcAdaptation* NfcAdaptation::mpInstance = NULL;
41 ThreadMutex NfcAdaptation::sLock;
42 
43 int omapi_status;
44 
Initialize()45 void NfcAdaptation::Initialize() {
46   const char* func = "NfcAdaptation::Initialize";
47   ALOGD_IF(ese_debug_enabled, "%s", func);
48   if (mHalNxpNfc != nullptr) return;
49   mHalNxpNfc = INxpNfc::tryGetService();
50   if (mHalNxpNfc != nullptr) {
51     ALOGE("%s: INxpNfc::getService() returned %p (%s)", func, mHalNxpNfc.get(),
52           (mHalNxpNfc->isRemote() ? "remote" : "local"));
53   }
54   ALOGD_IF(ese_debug_enabled, "%s: exit", func);
55 }
56 /*******************************************************************************
57 **
58 ** Function:    NfcAdaptation::GetInstance()
59 **
60 ** Description: access class singleton
61 **
62 ** Returns:     pointer to the singleton object
63 **
64 *******************************************************************************/
GetInstance()65 NfcAdaptation& NfcAdaptation::GetInstance() {
66   AutoThreadMutex a(sLock);
67 
68   if (!mpInstance) mpInstance = new NfcAdaptation;
69   return *mpInstance;
70 }
71 /*******************************************************************************
72 **
73 ** Function:    ThreadMutex::ThreadMutex()
74 **
75 ** Description: class constructor
76 **
77 ** Returns:     none
78 **
79 *******************************************************************************/
ThreadMutex()80 ThreadMutex::ThreadMutex() {
81   pthread_mutexattr_t mutexAttr;
82 
83   pthread_mutexattr_init(&mutexAttr);
84   pthread_mutex_init(&mMutex, &mutexAttr);
85   pthread_mutexattr_destroy(&mutexAttr);
86 }
87 /*******************************************************************************
88 **
89 ** Function:    ThreadMutex::~ThreadMutex()
90 **
91 ** Description: class destructor
92 **
93 ** Returns:     none
94 **
95 *******************************************************************************/
~ThreadMutex()96 ThreadMutex::~ThreadMutex() { pthread_mutex_destroy(&mMutex); }
97 
98 /*******************************************************************************
99 **
100 ** Function:    AutoThreadMutex::AutoThreadMutex()
101 **
102 ** Description: class constructor, automatically lock the mutex
103 **
104 ** Returns:     none
105 **
106 *******************************************************************************/
AutoThreadMutex(ThreadMutex & m)107 AutoThreadMutex::AutoThreadMutex(ThreadMutex& m) : mm(m) { mm.lock(); }
108 
109 /*******************************************************************************
110 **
111 ** Function:    AutoThreadMutex::~AutoThreadMutex()
112 **
113 ** Description: class destructor, automatically unlock the mutex
114 **
115 ** Returns:     none
116 **
117 *******************************************************************************/
~AutoThreadMutex()118 AutoThreadMutex::~AutoThreadMutex() { mm.unlock(); }
119 
120 /*******************************************************************************
121 **
122 ** Function:    ThreadMutex::lock()
123 **
124 ** Description: lock kthe mutex
125 **
126 ** Returns:     none
127 **
128 *******************************************************************************/
lock()129 void ThreadMutex::lock() { pthread_mutex_lock(&mMutex); }
130 
131 /*******************************************************************************
132 **
133 ** Function:    ThreadMutex::unblock()
134 **
135 ** Description: unlock the mutex
136 **
137 ** Returns:     none
138 **
139 *******************************************************************************/
unlock()140 void ThreadMutex::unlock() { pthread_mutex_unlock(&mMutex); }
141 
142 /*******************************************************************************
143 **
144 ** Function:    NfcAdaptation::NfcAdaptation()
145 **
146 ** Description: class constructor
147 **
148 ** Returns:     none
149 **
150 *******************************************************************************/
NfcAdaptation()151 NfcAdaptation::NfcAdaptation() { mCurrentIoctlData = NULL; }
152 
153 /*******************************************************************************
154 **
155 ** Function:    NfcAdaptation::~NfcAdaptation()
156 **
157 ** Description: class destructor
158 **
159 ** Returns:     none
160 **
161 *******************************************************************************/
~NfcAdaptation()162 NfcAdaptation::~NfcAdaptation() { mpInstance = NULL; }
163 
164 /*******************************************************************************
165 **
166 ** Function:    NfcAdaptation::resetEse
167 **
168 ** Description:  This function a wrapper function which triggers Ese reset
169 **
170 **
171 **
172 ** Returns:     -1 or 0.
173 **
174 *******************************************************************************/
resetEse(uint64_t level)175 ESESTATUS NfcAdaptation::resetEse(uint64_t level) {
176   const char* func = "NfcAdaptation::resetEse";
177   ESESTATUS result = ESESTATUS_FAILED;
178   bool ret = 0;
179 
180   ALOGD_IF(ese_debug_enabled, "%s : Enter", func);
181 
182   if (mHalNxpNfc != nullptr) {
183     ret = mHalNxpNfc->resetEse(level);
184     if (ret) {
185       ALOGE("NfcAdaptation::resetEse mHalNxpNfc completed");
186       result = ESESTATUS_SUCCESS;
187     } else {
188       ALOGE("NfcAdaptation::resetEse mHalNxpNfc failed");
189     }
190   }
191 
192   return result;
193 }
194 
195 /*******************************************************************************
196 **
197 ** Function:    NfcAdaptation::setEseUpdateState
198 **
199 ** Description:  This is a wrapper functions notifies upper layer about
200 ** the jcob download comple
201 ** tion.
202 ** Returns:     -1 or 0.
203 **
204 *******************************************************************************/
setEseUpdateState(void * p_data)205 ESESTATUS NfcAdaptation::setEseUpdateState(void* p_data) {
206   const char* func = "NfcAdaptation::setEseUpdateState";
207   ::android::hardware::nfc::V1_0::NfcData data;
208   ESESTATUS result = ESESTATUS_FAILED;
209   bool ret = 0;
210 
211   ALOGD_IF(ese_debug_enabled, "%s : Enter", func);
212 
213   ese_nxp_IoctlInOutData_t* pInpOutData = (ese_nxp_IoctlInOutData_t*)p_data;
214   data.setToExternal((uint8_t*)pInpOutData, sizeof(ese_nxp_IoctlInOutData_t));
215 
216   if (mHalNxpNfc != nullptr) {
217     ret = mHalNxpNfc->setEseUpdateState(
218         (::vendor::nxp::nxpnfc::V2_0::NxpNfcHalEseState)
219             pInpOutData->inp.data.nxpCmd.p_cmd[0]);
220     if (ret) {
221       ALOGE("NfcAdaptation::setEseUpdateState completed");
222       result = ESESTATUS_SUCCESS;
223     } else {
224       ALOGE("NfcAdaptation::setEseUpdateState failed");
225     }
226   }
227 
228   return result;
229 }
230 
231 /*******************************************************************************
232 **
233 ** Function:    ThreadCondVar::ThreadCondVar()
234 **
235 ** Description: class constructor
236 **
237 ** Returns:     none
238 **
239 *******************************************************************************/
ThreadCondVar()240 ThreadCondVar::ThreadCondVar() {
241   pthread_condattr_t CondAttr;
242 
243   pthread_condattr_init(&CondAttr);
244   pthread_cond_init(&mCondVar, &CondAttr);
245 
246   pthread_condattr_destroy(&CondAttr);
247 }
248 
249 /*******************************************************************************
250 **
251 ** Function:    ThreadCondVar::~ThreadCondVar()
252 **
253 ** Description: class destructor
254 **
255 ** Returns:     none
256 **
257 *******************************************************************************/
~ThreadCondVar()258 ThreadCondVar::~ThreadCondVar() { pthread_cond_destroy(&mCondVar); }
259