• 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 <iostream>
21 #include <string>
22 #include "securec.h"
23 
24 #define HDF_LOG_TAG hdf_nfc_dal
25 
26 #ifdef LOG_DOMAIN
27 #undef LOG_DOMAIN
28 #endif
29 
30 #define LOG_DOMAIN 0xD000306
31 
32 using namespace std;
33 
34 namespace OHOS {
35 namespace HDI {
36 namespace Nfc {
GetNfcHalSoName(const std::string & chipType)37 static string GetNfcHalSoName(const std::string &chipType)
38 {
39     string nfcHalSoName = NFC_HAL_SO_PREFIX + chipType + NFC_HAL_SO_SUFFIX;
40     return nfcHalSoName;
41 }
42 
GetChipType(void)43 string NfcVendorAdaptions::GetChipType(void)
44 {
45     string nfcChipType = "";
46     nfcExtHandle = dlopen(VENDOR_NFC_EXT_SERVICE_LIB.c_str(), RTLD_LAZY | RTLD_GLOBAL);
47     if (nfcExtHandle == nullptr) {
48         HDF_LOGE("%{public}s: fail to get nfc ext service handle.", __func__);
49         return nfcChipType;
50     }
51     nfcExtInf.getNfcChipType = reinterpret_cast<const char* (*)()>
52         (dlsym(nfcExtHandle, EXT_GET_CHIP_TYPE_FUNC_NAME.c_str()));
53     nfcExtInf.getNfcHalFuncNameSuffix = reinterpret_cast<const char* (*)(const char*)>
54         (dlsym(nfcExtHandle, EXT_GET_SUFFIX_FUNC_NAME.c_str()));
55 
56     if (nfcExtInf.getNfcChipType == nullptr || nfcExtInf.getNfcHalFuncNameSuffix == nullptr) {
57         HDF_LOGE("%{public}s: fail to init func ptr.", __func__);
58         return nfcChipType;
59     }
60     nfcChipType = string(nfcExtInf.getNfcChipType());
61     return nfcChipType;
62 }
63 
GetNfcHalFuncNameSuffix(const std::string & chipType)64 string NfcVendorAdaptions::GetNfcHalFuncNameSuffix(const std::string &chipType)
65 {
66     string suffix = DEFAULT_FUNC_NAME_SUFFIX;
67     if (nfcExtInf.getNfcHalFuncNameSuffix != nullptr) {
68         suffix = string(nfcExtInf.getNfcHalFuncNameSuffix(chipType.c_str()));
69     }
70     return suffix;
71 }
72 
ResetNfcInterface(void)73 void NfcVendorAdaptions::ResetNfcInterface(void)
74 {
75     nfcHalHandle = nullptr;
76     nfcHalInf.nfcHalOpen = nullptr;
77     nfcHalInf.nfcHalWrite = nullptr;
78     nfcHalInf.nfcHalCoreInitialized = nullptr;
79     nfcHalInf.nfcHalPrediscover = nullptr;
80     nfcHalInf.nfcHalClose = nullptr;
81     nfcHalInf.nfcHalControlGranted = nullptr;
82     nfcHalInf.nfcHalPowerCycle = nullptr;
83     nfcHalInf.nfcHalIoctl = nullptr;
84     nfcHalInf.nfcHalGetConfig = nullptr;
85     nfcHalInf.nfcHalFactoryReset = nullptr;
86     nfcHalInf.nfcHalShutdownCase = nullptr;
87     nfcExtHandle = nullptr;
88     nfcExtInf.getNfcChipType = nullptr;
89     nfcExtInf.getNfcHalFuncNameSuffix = nullptr;
90 }
91 
InitNfcHalInterfaces(string nfcHalSoName,string suffix)92 int8_t NfcVendorAdaptions::InitNfcHalInterfaces(string nfcHalSoName, string suffix)
93 {
94     if (nfcHalHandle == nullptr) {
95         nfcHalHandle = dlopen(nfcHalSoName.c_str(), RTLD_LAZY | RTLD_GLOBAL);
96     }
97     if (nfcHalHandle == nullptr) {
98         HDF_LOGE("%{public}s: invalid input path, opening default hal lib", __func__);
99         nfcHalSoName = NFC_HAL_SO_DEFAULT_NAME;
100         suffix = DEFAULT_FUNC_NAME_SUFFIX;
101         nfcHalHandle = dlopen(nfcHalSoName.c_str(), RTLD_LAZY | RTLD_GLOBAL);
102     }
103     if (nfcHalHandle == nullptr) {
104         HDF_LOGE("%{public}s: fail to open hal path.", __func__);
105         return HDF_FAILURE;
106     }
107 
108     nfcHalInf.nfcHalOpen = reinterpret_cast<int (*)(NfcStackCallbackT *, NfcStackDataCallbackT *)>
109         (dlsym(nfcHalHandle, (HAL_OPEN_FUNC_NAME + suffix).c_str()));
110 
111     nfcHalInf.nfcHalWrite = reinterpret_cast<int (*)(uint16_t, const uint8_t *)>
112         (dlsym(nfcHalHandle, (HAL_WRITE_FUNC_NAME + suffix).c_str()));
113 
114     nfcHalInf.nfcHalCoreInitialized = reinterpret_cast<int (*)(uint16_t, uint8_t *)>
115         (dlsym(nfcHalHandle, (HAL_CORE_INIT_FUNC_NAME + suffix).c_str()));
116 
117     nfcHalInf.nfcHalPrediscover = reinterpret_cast<int (*)()>
118         (dlsym(nfcHalHandle, (HAL_PRE_DISC_FUNC_NAME + suffix).c_str()));
119 
120     nfcHalInf.nfcHalClose = reinterpret_cast<int (*)(bool)>
121         (dlsym(nfcHalHandle, (HAL_CLOSE_FUNC_NAME + suffix).c_str()));
122 
123     nfcHalInf.nfcHalControlGranted = reinterpret_cast<int (*)()>
124         (dlsym(nfcHalHandle, (HAL_CTRL_GRANTED_FUNC_NAME + suffix).c_str()));
125 
126     nfcHalInf.nfcHalPowerCycle = reinterpret_cast<int (*)()>
127         (dlsym(nfcHalHandle, (HAL_POWER_CYCLE_FUNC_NAME + suffix).c_str()));
128 
129     nfcHalInf.nfcHalIoctl = reinterpret_cast<int (*)(long, void *)>
130         (dlsym(nfcHalHandle, (HAL_IOCTL_FUNC_NAME + suffix).c_str()));
131 
132     nfcHalInf.nfcHalGetConfig = reinterpret_cast<void (*)(V1_1::NfcVendorConfig &)>
133         (dlsym(nfcHalHandle, (HAL_GET_CONFIG_FUNC_NAME + suffix).c_str()));
134 
135     nfcHalInf.nfcHalFactoryReset = reinterpret_cast<void (*)()>
136         (dlsym(nfcHalHandle, (HAL_FACTORY_RESET_FUNC_NAME + suffix).c_str()));
137 
138     nfcHalInf.nfcHalShutdownCase = reinterpret_cast<int (*)()>
139         (dlsym(nfcHalHandle, (HAL_SHUTDOWN_CASE_FUNC_NAME + suffix).c_str()));
140 
141     if (nfcHalInf.nfcHalOpen == nullptr || nfcHalInf.nfcHalWrite == nullptr ||
142         nfcHalInf.nfcHalCoreInitialized == nullptr || nfcHalInf.nfcHalPrediscover == nullptr ||
143         nfcHalInf.nfcHalClose == nullptr || nfcHalInf.nfcHalControlGranted == nullptr ||
144         nfcHalInf.nfcHalPowerCycle == nullptr || nfcHalInf.nfcHalIoctl == nullptr ||
145         nfcHalInf.nfcHalGetConfig == nullptr || nfcHalInf.nfcHalFactoryReset == nullptr ||
146         nfcHalInf.nfcHalShutdownCase == nullptr) {
147         HDF_LOGE("%{public}s: fail to init func ptr.", __func__);
148         return HDF_FAILURE;
149     }
150     HDF_LOGI("%{public}s: init nfc hal inf successfully.", __func__);
151     return HDF_SUCCESS;
152 }
153 
NfcVendorAdaptions()154 NfcVendorAdaptions::NfcVendorAdaptions()
155 {
156     ResetNfcInterface();
157     if (nfcHalHandle == nullptr) {
158         string chipType = GetChipType();
159         string nfcHalSoName = GetNfcHalSoName(chipType);
160         string nfcHalFuncNameSuffix = GetNfcHalFuncNameSuffix(chipType);
161         if (InitNfcHalInterfaces(nfcHalSoName, nfcHalFuncNameSuffix) != HDF_SUCCESS) {
162             HDF_LOGE("%{public}s: fail to init hal inf.", __func__);
163         }
164     }
165 }
166 
~NfcVendorAdaptions()167 NfcVendorAdaptions::~NfcVendorAdaptions() {}
168 
VendorOpen(NfcStackCallbackT * pCback,NfcStackDataCallbackT * pDataCback)169 int NfcVendorAdaptions::VendorOpen(NfcStackCallbackT *pCback, NfcStackDataCallbackT *pDataCback)
170 {
171     if (nfcHalInf.nfcHalOpen == nullptr) {
172         HDF_LOGE("%{public}s: Function null.", __func__);
173         return HDF_FAILURE;
174     }
175     if (pCback == nullptr || pDataCback == nullptr) {
176         HDF_LOGE("%{public}s: input param null.", __func__);
177         return HDF_FAILURE;
178     }
179     int ret = nfcHalInf.nfcHalOpen(pCback, pDataCback);
180     return ret;
181 }
182 
VendorCoreInitialized(uint16_t coreInitRspLen,uint8_t * pCoreInitRspParams)183 int NfcVendorAdaptions::VendorCoreInitialized(uint16_t coreInitRspLen, uint8_t *pCoreInitRspParams)
184 {
185     if (nfcHalInf.nfcHalCoreInitialized == nullptr) {
186         HDF_LOGE("%{public}s: Function null.", __func__);
187         return HDF_FAILURE;
188     }
189     if (pCoreInitRspParams == nullptr) {
190         HDF_LOGE("%{public}s: input param null.", __func__);
191         return HDF_FAILURE;
192     }
193     int ret = nfcHalInf.nfcHalCoreInitialized(coreInitRspLen, pCoreInitRspParams);
194     return ret;
195 }
196 
VendorWrite(uint16_t dataLen,const uint8_t * pData)197 int NfcVendorAdaptions::VendorWrite(uint16_t dataLen, const uint8_t *pData)
198 {
199     if (nfcHalInf.nfcHalWrite == nullptr) {
200         HDF_LOGE("%{public}s: Function null.", __func__);
201         return HDF_FAILURE;
202     }
203     if (pData == nullptr) {
204         HDF_LOGE("%{public}s: input param null.", __func__);
205         return HDF_FAILURE;
206     }
207     int ret = nfcHalInf.nfcHalWrite(dataLen, pData);
208     return ret;
209 }
210 
VendorPrediscover(void)211 int NfcVendorAdaptions::VendorPrediscover(void)
212 {
213     if (nfcHalInf.nfcHalPrediscover == nullptr) {
214         HDF_LOGE("%{public}s: Function null.", __func__);
215         return HDF_FAILURE;
216     }
217     int ret = nfcHalInf.nfcHalPrediscover();
218     return ret;
219 }
220 
VendorClose(bool bShutdown)221 int NfcVendorAdaptions::VendorClose(bool bShutdown)
222 {
223     if (nfcHalInf.nfcHalClose == nullptr) {
224         HDF_LOGE("%{public}s: Function null.", __func__);
225         return HDF_FAILURE;
226     }
227     int ret = nfcHalInf.nfcHalClose(bShutdown);
228     return ret;
229 }
230 
VendorControlGranted(void)231 int NfcVendorAdaptions::VendorControlGranted(void)
232 {
233     if (nfcHalInf.nfcHalControlGranted == nullptr) {
234         HDF_LOGE("%{public}s: Function null.", __func__);
235         return HDF_FAILURE;
236     }
237     int ret = nfcHalInf.nfcHalControlGranted();
238     return ret;
239 }
240 
VendorPowerCycle(void)241 int NfcVendorAdaptions::VendorPowerCycle(void)
242 {
243     if (nfcHalInf.nfcHalPowerCycle == nullptr) {
244         HDF_LOGE("%{public}s: Function null.", __func__);
245         return HDF_FAILURE;
246     }
247     int ret = nfcHalInf.nfcHalPowerCycle();
248     return ret;
249 }
250 
VendorIoctl(long arg,void * pData)251 int NfcVendorAdaptions::VendorIoctl(long arg, void *pData)
252 {
253     if (nfcHalInf.nfcHalIoctl == nullptr) {
254         HDF_LOGE("%{public}s: Function null.", __func__);
255         return HDF_FAILURE;
256     }
257     if (pData == nullptr) {
258         HDF_LOGE("%{public}s: input param null.", __func__);
259         return HDF_FAILURE;
260     }
261     int ret = nfcHalInf.nfcHalIoctl(arg, pData);
262     return ret;
263 }
264 
VendorIoctlWithResponse(long arg,void * pData,std::vector<uint8_t> & pRetVal)265 int NfcVendorAdaptions::VendorIoctlWithResponse(long arg, void *pData, std::vector<uint8_t> &pRetVal)
266 {
267     if (nfcHalInf.nfcHalIoctl == nullptr) {
268         HDF_LOGE("%{public}s: Function null.", __func__);
269         return HDF_FAILURE;
270     }
271     if (pData == nullptr) {
272         HDF_LOGE("%{public}s: input param null.", __func__);
273         return HDF_FAILURE;
274     }
275     uint8_t inOutData[VENDOR_IOCTL_TOTAL_LEN] = { 0 };
276     if (memcpy_s(inOutData, VENDOR_IOCTL_TOTAL_LEN, pData, VENDOR_IOCTL_INOUT_DATA_LEN) != EOK) {
277         HDF_LOGE("%{public}s: memcpy_s pData failed.", __func__);
278         return HDF_FAILURE;
279     }
280     int ret = nfcHalInf.nfcHalIoctl(arg, inOutData);
281     if (ret == HDF_SUCCESS) {
282         uint8_t* pTmp = inOutData;
283         int i;
284         for (i = 0; i <= pTmp[VENDOR_IOCTL_OUTPUT_LEN_INDEX]; i++) {
285             pRetVal.push_back(pTmp[VENDOR_IOCTL_OUTPUT_LEN_INDEX + i]);
286         }
287     }
288     return ret;
289 }
290 
VendorGetConfig(V1_1::NfcVendorConfig & config)291 int NfcVendorAdaptions::VendorGetConfig(V1_1::NfcVendorConfig &config)
292 {
293     HDF_LOGD("%{public}s: start.", __func__);
294     if (nfcHalInf.nfcHalGetConfig == nullptr) {
295         HDF_LOGE("%{public}s: Function null.", __func__);
296         return HDF_FAILURE;
297     }
298     nfcHalInf.nfcHalGetConfig(config);
299     return HDF_SUCCESS;
300 }
301 
VendorFactoryReset(void)302 int NfcVendorAdaptions::VendorFactoryReset(void)
303 {
304     HDF_LOGD("%{public}s: start.", __func__);
305     if (nfcHalInf.nfcHalFactoryReset == nullptr) {
306         HDF_LOGE("%{public}s: Function null.", __func__);
307         return HDF_FAILURE;
308     }
309     nfcHalInf.nfcHalFactoryReset();
310     return HDF_SUCCESS;
311 }
312 
VendorShutdownCase(void)313 int NfcVendorAdaptions::VendorShutdownCase(void)
314 {
315     HDF_LOGD("%{public}s: start.", __func__);
316     if (nfcHalInf.nfcHalShutdownCase == nullptr) {
317         HDF_LOGE("%{public}s: Function null.", __func__);
318         return HDF_FAILURE;
319     }
320     int ret = nfcHalInf.nfcHalShutdownCase();
321     return ret;
322 }
323 } // Nfc
324 } // HDI
325 } // OHOS