• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "nfc_vendor_adaptions.h"
16 #include <dlfcn.h>
17 #include <fstream>
18 #include <hdf_base.h>
19 #include <hdf_log.h>
20 #include <mutex>
21 #include <iostream>
22 #include <pthread.h>
23 #include <string>
24 #include "securec.h"
25 
26 #define HDF_LOG_TAG hdf_nfc_dal
27 
28 #ifdef LOG_DOMAIN
29 #undef LOG_DOMAIN
30 #endif
31 
32 #define LOG_DOMAIN 0xD000306
33 std::mutex g_openMutex;
34 using namespace std;
35 namespace OHOS {
36 namespace HDI {
37 namespace Nfc {
GetNfcHalSoName(const std::string & chipType)38 static string GetNfcHalSoName(const std::string &chipType)
39 {
40     string nfcHalSoName = NFC_HAL_SO_PREFIX + chipType + NFC_HAL_SO_SUFFIX;
41     return nfcHalSoName;
42 }
43 
GetNfcStatus(void)44 int NfcVendorAdaptions::GetNfcStatus(void)
45 {
46     int nfcStatus = NFC_STATUS_OPEN;
47     nfcExtHandle = dlopen(VENDOR_NFC_EXT_SERVICE_LIB.c_str(), RTLD_LAZY | RTLD_GLOBAL);
48     if (nfcExtHandle == nullptr) {
49         HDF_LOGE("%{public}s: fail to get nfc ext service handle.", __func__);
50         return NFC_STATUS_CLOSE;
51     }
52     nfcExtInf.getNfcStatus = reinterpret_cast<int (*)()>
53         (dlsym(nfcExtHandle, EXT_GET_NFC_STATUS_FUNC_NAME.c_str()));
54 
55     if (nfcExtInf.getNfcStatus == nullptr) {
56         HDF_LOGE("%{public}s: fail to init func ptr.", __func__);
57         dlclose(nfcExtHandle);
58         nfcExtHandle = nullptr;
59         return NFC_STATUS_CLOSE;
60     }
61     nfcStatus = nfcExtInf.getNfcStatus();
62     dlclose(nfcExtHandle);
63     nfcExtHandle = nullptr;
64     HDF_LOGE("%{public}s: status %{public}d.", __func__, nfcStatus);
65     return nfcStatus;
66 }
67 
GetChipType(void)68 string NfcVendorAdaptions::GetChipType(void)
69 {
70     string nfcChipType = "";
71     nfcExtHandle = dlopen(VENDOR_NFC_EXT_SERVICE_LIB.c_str(), RTLD_LAZY | RTLD_GLOBAL);
72     if (nfcExtHandle == nullptr) {
73         HDF_LOGE("%{public}s: fail to get nfc ext service handle.", __func__);
74         return nfcChipType;
75     }
76     nfcExtInf.getNfcChipType = reinterpret_cast<const char* (*)()>
77         (dlsym(nfcExtHandle, EXT_GET_CHIP_TYPE_FUNC_NAME.c_str()));
78     nfcExtInf.getNfcHalFuncNameSuffix = reinterpret_cast<const char* (*)(const char*)>
79         (dlsym(nfcExtHandle, EXT_GET_SUFFIX_FUNC_NAME.c_str()));
80 
81     if (nfcExtInf.getNfcChipType == nullptr || nfcExtInf.getNfcHalFuncNameSuffix == nullptr) {
82         HDF_LOGE("%{public}s: fail to init func ptr.", __func__);
83         dlclose(nfcExtHandle);
84         nfcExtHandle = nullptr;
85         return nfcChipType;
86     }
87     nfcChipType = string(nfcExtInf.getNfcChipType());
88     dlclose(nfcExtHandle);
89     nfcExtHandle = nullptr;
90     return nfcChipType;
91 }
92 
CheckFirmwareUpdate(void)93 void NfcVendorAdaptions::CheckFirmwareUpdate(void)
94 {
95     nfcExtHandle = dlopen(VENDOR_NFC_EXT_SERVICE_LIB.c_str(), RTLD_LAZY | RTLD_GLOBAL);
96     if (nfcExtHandle == nullptr) {
97         HDF_LOGE("%{public}s: fail to get nfc ext service handle.", __func__);
98         return;
99     }
100     nfcExtInf.checkFirmwareUpdate = reinterpret_cast<void (*)()>
101         (dlsym(nfcExtHandle, EXT_SET_FW_UPDATE_CONFIG_FUNC_NAME.c_str()));
102     if (nfcExtInf.checkFirmwareUpdate == nullptr) {
103         HDF_LOGE("%{public}s: fail to init func ptr.", __func__);
104         dlclose(nfcExtHandle);
105         nfcExtHandle = nullptr;
106         return;
107     }
108     nfcExtInf.checkFirmwareUpdate();
109     dlclose(nfcExtHandle);
110     nfcExtHandle = nullptr;
111 }
112 
GetNfcHalFuncNameSuffix(const std::string & chipType)113 string NfcVendorAdaptions::GetNfcHalFuncNameSuffix(const std::string &chipType)
114 {
115     string suffix = DEFAULT_FUNC_NAME_SUFFIX;
116     if (nfcExtInf.getNfcHalFuncNameSuffix != nullptr) {
117         suffix = string(nfcExtInf.getNfcHalFuncNameSuffix(chipType.c_str()));
118     }
119     return suffix;
120 }
121 
ResetNfcInterface(void)122 void NfcVendorAdaptions::ResetNfcInterface(void)
123 {
124     nfcHalHandle = nullptr;
125     nfcHalInf.nfcHalOpen = nullptr;
126     nfcHalInf.nfcHalWrite = nullptr;
127     nfcHalInf.nfcHalCoreInitialized = nullptr;
128     nfcHalInf.nfcHalPrediscover = nullptr;
129     nfcHalInf.nfcHalClose = nullptr;
130     nfcHalInf.nfcHalControlGranted = nullptr;
131     nfcHalInf.nfcHalPowerCycle = nullptr;
132     nfcHalInf.nfcHalIoctl = nullptr;
133     nfcHalInf.nfcHalGetConfig = nullptr;
134     nfcHalInf.nfcHalFactoryReset = nullptr;
135     nfcHalInf.nfcHalShutdownCase = nullptr;
136     nfcHalInf.nfcHalMinOpen = nullptr;
137     nfcHalInf.nfcHalMinClose = nullptr;
138     nfcExtHandle = nullptr;
139     nfcExtInf.getNfcChipType = nullptr;
140     nfcExtInf.getNfcStatus = nullptr;
141     nfcExtInf.getNfcHalFuncNameSuffix = nullptr;
142 }
143 
DoHalPreOpen(void * arg)144 void* NfcVendorAdaptions::DoHalPreOpen(void* arg)
145 {
146     NFCSTATUS status = HDF_SUCCESS;
147     if (arg == nullptr) {
148         return nullptr;
149     }
150     NfcVendorAdaptions *mVendorAdapter = static_cast<NfcVendorAdaptions*>(arg);
151     HDF_LOGI("%{public}s: enter.", __func__);
152     mVendorAdapter->isNfcPreDone = true;
153     if (mVendorAdapter->nfcHalInf.nfcHalMinOpen == nullptr ||
154         mVendorAdapter->nfcHalInf.nfcHalMinClose == nullptr) {
155         HDF_LOGE("%{public}s: function is null", __func__);
156         return nullptr;
157     }
158     std::lock_guard<std::mutex> lock(g_openMutex);
159     status = mVendorAdapter->nfcHalInf.nfcHalMinOpen(true);
160     if (status != HDF_SUCCESS) {
161         HDF_LOGE("%{public}s: nfcHalMinOpen is fail", __func__);
162         return nullptr;
163     }
164     status = mVendorAdapter->nfcHalInf.nfcHalMinClose();
165     if (status != HDF_SUCCESS) {
166         HDF_LOGE("%{public}s: nfcHalMinClose is fail", __func__);
167         return nullptr;
168     }
169     HDF_LOGI("%{public}s: exit.", __func__);
170     return nullptr;
171 }
172 
HalPreOpen(void)173 void NfcVendorAdaptions::HalPreOpen(void)
174 {
175     int ret = HDF_SUCCESS;
176     int nfcStatus = NFC_STATUS_OPEN;
177     pthread_t pthread;
178     HDF_LOGI("%{public}s: enter.", __func__);
179     nfcStatus = GetNfcStatus();
180     if (!isNfcPreDone && (nfcStatus != NFC_STATUS_OPEN)) {
181         ret = pthread_create(&pthread, nullptr, NfcVendorAdaptions::DoHalPreOpen, this);
182         if (ret != HDF_SUCCESS) {
183             HDF_LOGE("%{public}s: pthread_create is fail", __func__);
184         }
185     }
186     HDF_LOGI("%{public}s: exit.", __func__);
187 }
188 
PreInitNfcHalInterfaces(string nfcHalSoName,string suffix)189 int8_t NfcVendorAdaptions::PreInitNfcHalInterfaces(string nfcHalSoName, string suffix)
190 {
191     if (nfcHalHandle == nullptr) {
192         nfcHalHandle = dlopen(nfcHalSoName.c_str(), RTLD_LAZY | RTLD_GLOBAL);
193     }
194     if (nfcHalHandle == nullptr) {
195         HDF_LOGE("%{public}s: invalid input path, opening default hal lib", __func__);
196         nfcHalSoName = NFC_HAL_SO_DEFAULT_NAME;
197         suffix = DEFAULT_FUNC_NAME_SUFFIX;
198         nfcHalHandle = dlopen(nfcHalSoName.c_str(), RTLD_LAZY | RTLD_GLOBAL);
199     }
200     if (nfcHalHandle == nullptr) {
201         HDF_LOGE("%{public}s: fail to open hal path.", __func__);
202         return HDF_FAILURE;
203     }
204     return HDF_SUCCESS;
205 }
206 
InitNfcHalInterfaces(string nfcHalSoName,string suffix)207 int8_t NfcVendorAdaptions::InitNfcHalInterfaces(string nfcHalSoName, string suffix)
208 {
209     int8_t ret = PreInitNfcHalInterfaces(nfcHalSoName, suffix);
210     if (ret != HDF_SUCCESS) {
211         return ret;
212     }
213     nfcHalInf.nfcHalOpen = reinterpret_cast<int (*)(NfcStackCallbackT *, NfcStackDataCallbackT *)>
214         (dlsym(nfcHalHandle, (HAL_OPEN_FUNC_NAME + suffix).c_str()));
215 
216     nfcHalInf.nfcHalWrite = reinterpret_cast<int (*)(uint16_t, const uint8_t *)>
217         (dlsym(nfcHalHandle, (HAL_WRITE_FUNC_NAME + suffix).c_str()));
218 
219     nfcHalInf.nfcHalCoreInitialized = reinterpret_cast<int (*)(uint16_t, uint8_t *)>
220         (dlsym(nfcHalHandle, (HAL_CORE_INIT_FUNC_NAME + suffix).c_str()));
221 
222     nfcHalInf.nfcHalPrediscover = reinterpret_cast<int (*)()>
223         (dlsym(nfcHalHandle, (HAL_PRE_DISC_FUNC_NAME + suffix).c_str()));
224 
225     nfcHalInf.nfcHalClose = reinterpret_cast<int (*)(bool)>
226         (dlsym(nfcHalHandle, (HAL_CLOSE_FUNC_NAME + suffix).c_str()));
227 
228     nfcHalInf.nfcHalControlGranted = reinterpret_cast<int (*)()>
229         (dlsym(nfcHalHandle, (HAL_CTRL_GRANTED_FUNC_NAME + suffix).c_str()));
230 
231     nfcHalInf.nfcHalPowerCycle = reinterpret_cast<int (*)()>
232         (dlsym(nfcHalHandle, (HAL_POWER_CYCLE_FUNC_NAME + suffix).c_str()));
233 
234     nfcHalInf.nfcHalIoctl = reinterpret_cast<int (*)(long, void *)>
235         (dlsym(nfcHalHandle, (HAL_IOCTL_FUNC_NAME + suffix).c_str()));
236 
237     nfcHalInf.nfcHalGetConfig = reinterpret_cast<void (*)(V1_1::NfcVendorConfig &)>
238         (dlsym(nfcHalHandle, (HAL_GET_CONFIG_FUNC_NAME + suffix).c_str()));
239 
240     nfcHalInf.nfcHalFactoryReset = reinterpret_cast<void (*)()>
241         (dlsym(nfcHalHandle, (HAL_FACTORY_RESET_FUNC_NAME + suffix).c_str()));
242 
243     nfcHalInf.nfcHalShutdownCase = reinterpret_cast<int (*)()>
244         (dlsym(nfcHalHandle, (HAL_SHUTDOWN_CASE_FUNC_NAME + suffix).c_str()));
245 
246     nfcHalInf.nfcHalMinOpen = reinterpret_cast<NFCSTATUS (*)(bool)>
247         (dlsym(nfcHalHandle, (HAL_MIN_OPEN_FUNC_NAME + suffix).c_str()));
248 
249     nfcHalInf.nfcHalMinClose = reinterpret_cast<NFCSTATUS (*)()>
250         (dlsym(nfcHalHandle, (HAL_MIN_CLOSE_FUNC_NAME + suffix).c_str()));
251 
252     if (nfcHalInf.nfcHalOpen == nullptr || nfcHalInf.nfcHalWrite == nullptr ||
253         nfcHalInf.nfcHalCoreInitialized == nullptr || nfcHalInf.nfcHalPrediscover == nullptr ||
254         nfcHalInf.nfcHalClose == nullptr || nfcHalInf.nfcHalControlGranted == nullptr ||
255         nfcHalInf.nfcHalPowerCycle == nullptr || nfcHalInf.nfcHalIoctl == nullptr ||
256         nfcHalInf.nfcHalGetConfig == nullptr || nfcHalInf.nfcHalFactoryReset == nullptr ||
257         nfcHalInf.nfcHalShutdownCase == nullptr) {
258         HDF_LOGE("%{public}s: fail to init func ptr.", __func__);
259         return HDF_FAILURE;
260     }
261     HDF_LOGI("%{public}s: init nfc hal inf successfully.", __func__);
262     return HDF_SUCCESS;
263 }
264 
NfcVendorAdaptions()265 NfcVendorAdaptions::NfcVendorAdaptions()
266 {
267     ResetNfcInterface();
268     if (nfcHalHandle == nullptr) {
269         CheckFirmwareUpdate();
270         string chipType = GetChipType();
271         string nfcHalSoName = GetNfcHalSoName(chipType);
272         string nfcHalFuncNameSuffix = GetNfcHalFuncNameSuffix(chipType);
273         if (InitNfcHalInterfaces(nfcHalSoName, nfcHalFuncNameSuffix) != HDF_SUCCESS) {
274             HDF_LOGE("%{public}s: fail to init hal inf.", __func__);
275         }
276         HalPreOpen();
277     }
278 }
279 
~NfcVendorAdaptions()280 NfcVendorAdaptions::~NfcVendorAdaptions() {}
281 
VendorOpen(NfcStackCallbackT * pCback,NfcStackDataCallbackT * pDataCback)282 int NfcVendorAdaptions::VendorOpen(NfcStackCallbackT *pCback, NfcStackDataCallbackT *pDataCback)
283 {
284     if (nfcHalInf.nfcHalOpen == nullptr) {
285         HDF_LOGE("%{public}s: Function null.", __func__);
286         return HDF_FAILURE;
287     }
288     if (pCback == nullptr || pDataCback == nullptr) {
289         HDF_LOGE("%{public}s: input param null.", __func__);
290         return HDF_FAILURE;
291     }
292     std::lock_guard<std::mutex> lock(g_openMutex);
293     int ret = nfcHalInf.nfcHalOpen(pCback, pDataCback);
294     return ret;
295 }
296 
VendorCoreInitialized(uint16_t coreInitRspLen,uint8_t * pCoreInitRspParams)297 int NfcVendorAdaptions::VendorCoreInitialized(uint16_t coreInitRspLen, uint8_t *pCoreInitRspParams)
298 {
299     if (nfcHalInf.nfcHalCoreInitialized == nullptr) {
300         HDF_LOGE("%{public}s: Function null.", __func__);
301         return HDF_FAILURE;
302     }
303     if (pCoreInitRspParams == nullptr) {
304         HDF_LOGE("%{public}s: input param null.", __func__);
305         return HDF_FAILURE;
306     }
307     int ret = nfcHalInf.nfcHalCoreInitialized(coreInitRspLen, pCoreInitRspParams);
308     return ret;
309 }
310 
VendorWrite(uint16_t dataLen,const uint8_t * pData)311 int NfcVendorAdaptions::VendorWrite(uint16_t dataLen, const uint8_t *pData)
312 {
313     if (nfcHalInf.nfcHalWrite == nullptr) {
314         HDF_LOGE("%{public}s: Function null.", __func__);
315         return HDF_FAILURE;
316     }
317     if (pData == nullptr) {
318         HDF_LOGE("%{public}s: input param null.", __func__);
319         return HDF_FAILURE;
320     }
321     int ret = nfcHalInf.nfcHalWrite(dataLen, pData);
322     return ret;
323 }
324 
VendorPrediscover(void)325 int NfcVendorAdaptions::VendorPrediscover(void)
326 {
327     if (nfcHalInf.nfcHalPrediscover == nullptr) {
328         HDF_LOGE("%{public}s: Function null.", __func__);
329         return HDF_FAILURE;
330     }
331     int ret = nfcHalInf.nfcHalPrediscover();
332     return ret;
333 }
334 
VendorClose(bool bShutdown)335 int NfcVendorAdaptions::VendorClose(bool bShutdown)
336 {
337     if (nfcHalInf.nfcHalClose == nullptr) {
338         HDF_LOGE("%{public}s: Function null.", __func__);
339         return HDF_FAILURE;
340     }
341     int ret = nfcHalInf.nfcHalClose(bShutdown);
342     return ret;
343 }
344 
VendorControlGranted(void)345 int NfcVendorAdaptions::VendorControlGranted(void)
346 {
347     if (nfcHalInf.nfcHalControlGranted == nullptr) {
348         HDF_LOGE("%{public}s: Function null.", __func__);
349         return HDF_FAILURE;
350     }
351     int ret = nfcHalInf.nfcHalControlGranted();
352     return ret;
353 }
354 
VendorPowerCycle(void)355 int NfcVendorAdaptions::VendorPowerCycle(void)
356 {
357     if (nfcHalInf.nfcHalPowerCycle == nullptr) {
358         HDF_LOGE("%{public}s: Function null.", __func__);
359         return HDF_FAILURE;
360     }
361     int ret = nfcHalInf.nfcHalPowerCycle();
362     return ret;
363 }
364 
VendorIoctl(long arg,void * pData)365 int NfcVendorAdaptions::VendorIoctl(long arg, void *pData)
366 {
367     if (nfcHalInf.nfcHalIoctl == nullptr) {
368         HDF_LOGE("%{public}s: Function null.", __func__);
369         return HDF_FAILURE;
370     }
371     if (pData == nullptr) {
372         HDF_LOGE("%{public}s: input param null.", __func__);
373         return HDF_FAILURE;
374     }
375     int ret = nfcHalInf.nfcHalIoctl(arg, pData);
376     return ret;
377 }
378 
VendorIoctlWithResponse(long arg,void * pData,uint16_t dataLen,std::vector<uint8_t> & pRetVal)379 int NfcVendorAdaptions::VendorIoctlWithResponse(long arg, void *pData, uint16_t dataLen, std::vector<uint8_t> &pRetVal)
380 {
381     if (nfcHalInf.nfcHalIoctl == nullptr) {
382         HDF_LOGE("%{public}s: Function null.", __func__);
383         return HDF_FAILURE;
384     }
385     if (pData == nullptr) {
386         HDF_LOGE("%{public}s: input param null.", __func__);
387         return HDF_FAILURE;
388     }
389     if (arg == VENDOR_GET_HISTORY_NCI_CMD) {
390         HDF_LOGI("%{public}s: getting history nci from vendor!", __func__);
391         return VendorGetHistoryNci(pData, dataLen, pRetVal);
392     }
393     if (dataLen < VENDOR_IOCTL_INPUT_MIN_LEN || dataLen > VENDOR_IOCTL_TOTAL_LEN) {
394         HDF_LOGE("%{public}s: dataLen is invalid!", __func__);
395         return HDF_ERR_INVALID_PARAM;
396     }
397     uint8_t inOutData[VENDOR_IOCTL_TOTAL_LEN] = { 0 };
398     if (memcpy_s(inOutData, VENDOR_IOCTL_TOTAL_LEN, pData, VENDOR_IOCTL_INOUT_DATA_LEN) != EOK) {
399         HDF_LOGE("%{public}s: memcpy_s pData failed.", __func__);
400         return HDF_FAILURE;
401     }
402     int ret = nfcHalInf.nfcHalIoctl(arg, inOutData);
403     if (ret == HDF_SUCCESS) {
404         uint8_t* pTmp = inOutData;
405         int i;
406         for (i = 0; i <= pTmp[VENDOR_IOCTL_OUTPUT_LEN_INDEX]; i++) {
407             pRetVal.push_back(pTmp[VENDOR_IOCTL_OUTPUT_LEN_INDEX + i]);
408         }
409     }
410     return ret;
411 }
412 
VendorGetHistoryNci(void * pData,uint16_t dataLen,std::vector<uint8_t> & pRetVal)413 int NfcVendorAdaptions::VendorGetHistoryNci(void *pData, uint16_t dataLen, std::vector<uint8_t> &pRetVal)
414 {
415     if (dataLen != VENDOR_IOCTL_INPUT_DATA_LEN) {
416         HDF_LOGE("%{public}s: input param data len err.", __func__);
417         return HDF_FAILURE;
418     }
419     std::vector<uint8_t> inOutData(VENDOR_IOCTL_TOTAL_LENGTH, 0);
420     if (memcpy_s(&inOutData[0], inOutData.size(), pData, dataLen) != EOK) {
421         HDF_LOGE("%{public}s: memcpy_s pData failed.", __func__);
422         return HDF_FAILURE;
423     }
424     int ret = nfcHalInf.nfcHalIoctl(VENDOR_GET_HISTORY_NCI_CMD, &inOutData[0]);
425     if (ret == HDF_SUCCESS) {
426         for (uint16_t i = 0; i < VENDOR_IOCTL_OUTPUT_DATA_LEN; i++) {
427             pRetVal.push_back(inOutData[VENDOR_IOCTL_OUTPUT_DATA_START_INDEX + i]);
428         }
429     }
430     return ret;
431 }
432 
VendorGetConfig(V1_1::NfcVendorConfig & config)433 int NfcVendorAdaptions::VendorGetConfig(V1_1::NfcVendorConfig &config)
434 {
435     HDF_LOGD("%{public}s: start.", __func__);
436     if (nfcHalInf.nfcHalGetConfig == nullptr) {
437         HDF_LOGE("%{public}s: Function null.", __func__);
438         return HDF_FAILURE;
439     }
440     nfcHalInf.nfcHalGetConfig(config);
441     return HDF_SUCCESS;
442 }
443 
VendorFactoryReset(void)444 int NfcVendorAdaptions::VendorFactoryReset(void)
445 {
446     HDF_LOGD("%{public}s: start.", __func__);
447     if (nfcHalInf.nfcHalFactoryReset == nullptr) {
448         HDF_LOGE("%{public}s: Function null.", __func__);
449         return HDF_FAILURE;
450     }
451     nfcHalInf.nfcHalFactoryReset();
452     return HDF_SUCCESS;
453 }
454 
VendorShutdownCase(void)455 int NfcVendorAdaptions::VendorShutdownCase(void)
456 {
457     HDF_LOGD("%{public}s: start.", __func__);
458     if (nfcHalInf.nfcHalShutdownCase == nullptr) {
459         HDF_LOGE("%{public}s: Function null.", __func__);
460         return HDF_FAILURE;
461     }
462     int ret = nfcHalInf.nfcHalShutdownCase();
463     return ret;
464 }
465 } // Nfc
466 } // HDI
467 } // OHOS