1 /******************************************************************************
2 *
3 * Copyright 2018-2020,2022-2023 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
21 #include <aidl/vendor/nxp/nxpnfc_aidl/INxpNfc.h>
22 #include <android/binder_auto_utils.h>
23 #include <android/binder_enums.h>
24 #include <android/binder_ibinder.h>
25 #include <android/binder_interface_utils.h>
26 #include <android/binder_manager.h>
27 #include <android/binder_process.h>
28 #include <android/hardware/nfc/1.0/types.h>
29 #include <binder/IServiceManager.h>
30 #include <ese_logs.h>
31 #include <hwbinder/ProcessState.h>
32 #include <log/log.h>
33 #include <pthread.h>
34
35 #undef LOG_TAG
36 #define LOG_TAG "SpiAdaptation"
37
38 using android::OK;
39 using android::sp;
40 using android::status_t;
41
42 using android::hardware::hidl_vec;
43 using android::hardware::ProcessState;
44 using android::hardware::Return;
45 using android::hardware::Void;
46 using INxpNfc = vendor::nxp::nxpnfc::V2_0::INxpNfc;
47 using INxpNfcAidl = ::aidl::vendor::nxp::nxpnfc_aidl::INxpNfc;
48
49 #define MAX_NFC_GET_RETRY 30
50 #define NFC_GET_SERVICE_DELAY_MS 100
51 std::string NXPNFC_AIDL_HAL_SERVICE_NAME =
52 "vendor.nxp.nxpnfc_aidl.INxpNfc/default";
53
54 sp<INxpNfc> NfcAdaptation::mHalNxpNfc = nullptr;
55 std::shared_ptr<INxpNfcAidl> NfcAdaptation::mAidlHalNxpNfc = nullptr;
56 ThreadMutex NfcAdaptation::sIoctlLock;
57 NfcAdaptation* NfcAdaptation::mpInstance = NULL;
58 ThreadMutex NfcAdaptation::sLock;
59
60 int omapi_status;
61
Initialize()62 void NfcAdaptation::Initialize() {
63 int retry = 0;
64 const char* func = "NfcAdaptation::Initialize";
65 NXP_LOG_ESE_D("%s", func);
66 // Try get AIDL
67 do {
68 ::ndk::SpAIBinder binder(
69 AServiceManager_checkService(NXPNFC_AIDL_HAL_SERVICE_NAME.c_str()));
70 mAidlHalNxpNfc = INxpNfcAidl::fromBinder(binder);
71 if (mAidlHalNxpNfc != nullptr) {
72 NXP_LOG_ESE_E("%s: INxpNfcAidl::fromBinder returned", func);
73 break;
74 }
75 usleep(NFC_GET_SERVICE_DELAY_MS * 1000);
76 } while (retry++ < MAX_NFC_GET_RETRY);
77 if (mAidlHalNxpNfc == nullptr) {
78 ALOGE("Failed to get NXP NFC AIDLHAL .. Try for HIDL HAL");
79 mHalNxpNfc = INxpNfc::tryGetService();
80 if (mHalNxpNfc != nullptr) {
81 ALOGI("NXP NFC HAL service is available");
82 } else {
83 ALOGE("Failed to get INxpNfc::tryGetService");
84 }
85 }
86 NXP_LOG_ESE_D("%s: exit", func);
87 }
88 /*******************************************************************************
89 **
90 ** Function: NfcAdaptation::GetInstance()
91 **
92 ** Description: access class singleton
93 **
94 ** Returns: pointer to the singleton object
95 **
96 *******************************************************************************/
GetInstance()97 NfcAdaptation& NfcAdaptation::GetInstance() {
98 AutoThreadMutex a(sLock);
99
100 if (!mpInstance) mpInstance = new NfcAdaptation;
101 return *mpInstance;
102 }
103 /*******************************************************************************
104 **
105 ** Function: ThreadMutex::ThreadMutex()
106 **
107 ** Description: class constructor
108 **
109 ** Returns: none
110 **
111 *******************************************************************************/
ThreadMutex()112 ThreadMutex::ThreadMutex() {
113 pthread_mutexattr_t mutexAttr;
114
115 pthread_mutexattr_init(&mutexAttr);
116 pthread_mutex_init(&mMutex, &mutexAttr);
117 pthread_mutexattr_destroy(&mutexAttr);
118 }
119 /*******************************************************************************
120 **
121 ** Function: ThreadMutex::~ThreadMutex()
122 **
123 ** Description: class destructor
124 **
125 ** Returns: none
126 **
127 *******************************************************************************/
~ThreadMutex()128 ThreadMutex::~ThreadMutex() { pthread_mutex_destroy(&mMutex); }
129
130 /*******************************************************************************
131 **
132 ** Function: AutoThreadMutex::AutoThreadMutex()
133 **
134 ** Description: class constructor, automatically lock the mutex
135 **
136 ** Returns: none
137 **
138 *******************************************************************************/
AutoThreadMutex(ThreadMutex & m)139 AutoThreadMutex::AutoThreadMutex(ThreadMutex& m) : mm(m) { mm.lock(); }
140
141 /*******************************************************************************
142 **
143 ** Function: AutoThreadMutex::~AutoThreadMutex()
144 **
145 ** Description: class destructor, automatically unlock the mutex
146 **
147 ** Returns: none
148 **
149 *******************************************************************************/
~AutoThreadMutex()150 AutoThreadMutex::~AutoThreadMutex() { mm.unlock(); }
151
152 /*******************************************************************************
153 **
154 ** Function: ThreadMutex::lock()
155 **
156 ** Description: lock the mutex
157 **
158 ** Returns: none
159 **
160 *******************************************************************************/
lock()161 void ThreadMutex::lock() { pthread_mutex_lock(&mMutex); }
162
163 /*******************************************************************************
164 **
165 ** Function: ThreadMutex::unblock()
166 **
167 ** Description: unlock the mutex
168 **
169 ** Returns: none
170 **
171 *******************************************************************************/
unlock()172 void ThreadMutex::unlock() { pthread_mutex_unlock(&mMutex); }
173
174 /*******************************************************************************
175 **
176 ** Function: NfcAdaptation::NfcAdaptation()
177 **
178 ** Description: class constructor
179 **
180 ** Returns: none
181 **
182 *******************************************************************************/
NfcAdaptation()183 NfcAdaptation::NfcAdaptation() { mCurrentIoctlData = NULL; }
184
185 /*******************************************************************************
186 **
187 ** Function: NfcAdaptation::~NfcAdaptation()
188 **
189 ** Description: class destructor
190 **
191 ** Returns: none
192 **
193 *******************************************************************************/
~NfcAdaptation()194 NfcAdaptation::~NfcAdaptation() { mpInstance = NULL; }
195
196 /*******************************************************************************
197 **
198 ** Function: NfcAdaptation::resetEse
199 **
200 ** Description: This function a wrapper function which triggers Ese reset
201 **
202 **
203 **
204 ** Returns: -1 or 0.
205 **
206 *******************************************************************************/
resetEse(uint64_t level)207 ESESTATUS NfcAdaptation::resetEse(uint64_t level) {
208 const char* func = "NfcAdaptation::resetEse";
209 ESESTATUS result = ESESTATUS_FAILED;
210 bool ret = 0;
211
212 NXP_LOG_ESE_D("%s : Enter", func);
213
214 if (mAidlHalNxpNfc != nullptr) {
215 mAidlHalNxpNfc->resetEse(level, &ret);
216 if (ret) {
217 NXP_LOG_ESE_E("NfcAdaptation::resetEse mAidlHalNxpNfc completed");
218 result = ESESTATUS_SUCCESS;
219 } else {
220 NXP_LOG_ESE_E("NfcAdaptation::resetEse mAidlHalNxpNfc failed");
221 }
222 } else if (mHalNxpNfc != nullptr) {
223 ret = mHalNxpNfc->resetEse(level);
224 if (ret) {
225 NXP_LOG_ESE_E("NfcAdaptation::resetEse mHalNxpNfc completed");
226 result = ESESTATUS_SUCCESS;
227 } else {
228 NXP_LOG_ESE_E("NfcAdaptation::resetEse mHalNxpNfc failed");
229 }
230 }
231
232 return result;
233 }
234
235 #ifdef NXP_BOOTTIME_UPDATE
236 /*******************************************************************************
237 **
238 ** Function: NfcAdaptation::setEseUpdateState
239 **
240 ** Description: This is a wrapper functions notifies upper layer about
241 ** the jcob download completion.
242 **
243 ** Returns: -1 or 0.
244 **
245 *******************************************************************************/
setEseUpdateState(void * p_data)246 ESESTATUS NfcAdaptation::setEseUpdateState(void* p_data) {
247 const char* func = "NfcAdaptation::setEseUpdateState";
248 ::android::hardware::nfc::V1_0::NfcData data;
249 ESESTATUS result = ESESTATUS_FAILED;
250 bool ret = 0;
251
252 NXP_LOG_ESE_D("%s : Enter", func);
253
254 ese_nxp_IoctlInOutData_t* pInpOutData = (ese_nxp_IoctlInOutData_t*)p_data;
255 data.setToExternal((uint8_t*)pInpOutData, sizeof(ese_nxp_IoctlInOutData_t));
256
257 if (mAidlHalNxpNfc != nullptr) {
258 NXP_LOG_ESE_D(
259 "NfcAdaptation::setEseUpdateState not supported for mAidlHalNxpNfc");
260 } else if (mHalNxpNfc != nullptr) {
261 ret = mHalNxpNfc->setEseUpdateState(
262 (::vendor::nxp::nxpnfc::V2_0::NxpNfcHalEseState)
263 pInpOutData->inp.data.nxpCmd.p_cmd[0]);
264 if (ret) {
265 NXP_LOG_ESE_E("NfcAdaptation::setEseUpdateState mHalNxpNfc completed");
266 result = ESESTATUS_SUCCESS;
267 } else {
268 NXP_LOG_ESE_E("NfcAdaptation::setEseUpdateState mHalNxpNfc failed");
269 }
270 }
271
272 return result;
273 }
274 #endif
275 /*******************************************************************************
276 **
277 ** Function: ThreadCondVar::ThreadCondVar()
278 **
279 ** Description: class constructor
280 **
281 ** Returns: none
282 **
283 *******************************************************************************/
ThreadCondVar()284 ThreadCondVar::ThreadCondVar() {
285 pthread_condattr_t CondAttr;
286
287 pthread_condattr_init(&CondAttr);
288 pthread_cond_init(&mCondVar, &CondAttr);
289
290 pthread_condattr_destroy(&CondAttr);
291 }
292
293 /*******************************************************************************
294 **
295 ** Function: ThreadCondVar::~ThreadCondVar()
296 **
297 ** Description: class destructor
298 **
299 ** Returns: none
300 **
301 *******************************************************************************/
~ThreadCondVar()302 ThreadCondVar::~ThreadCondVar() { pthread_cond_destroy(&mCondVar); }
303