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
CheckFirmwareUpdate(void)64 void NfcVendorAdaptions::CheckFirmwareUpdate(void)
65 {
66 nfcExtHandle = dlopen(VENDOR_NFC_EXT_SERVICE_LIB.c_str(), RTLD_LAZY | RTLD_GLOBAL);
67 if (nfcExtHandle == nullptr) {
68 HDF_LOGE("%{public}s: fail to get nfc ext service handle.", __func__);
69 return;
70 }
71 nfcExtInf.checkFirmwareUpdate = reinterpret_cast<void (*)()>
72 (dlsym(nfcExtHandle, EXT_SET_FW_UPDATE_CONFIG_FUNC_NAME.c_str()));
73 if (nfcExtInf.checkFirmwareUpdate == nullptr) {
74 HDF_LOGE("%{public}s: fail to init func ptr.", __func__);
75 dlclose(nfcExtHandle);
76 nfcExtHandle = nullptr;
77 return;
78 }
79 nfcExtInf.checkFirmwareUpdate();
80 dlclose(nfcExtHandle);
81 nfcExtHandle = nullptr;
82 }
83
GetNfcHalFuncNameSuffix(const std::string & chipType)84 string NfcVendorAdaptions::GetNfcHalFuncNameSuffix(const std::string &chipType)
85 {
86 string suffix = DEFAULT_FUNC_NAME_SUFFIX;
87 if (nfcExtInf.getNfcHalFuncNameSuffix != nullptr) {
88 suffix = string(nfcExtInf.getNfcHalFuncNameSuffix(chipType.c_str()));
89 }
90 return suffix;
91 }
92
ResetNfcInterface(void)93 void NfcVendorAdaptions::ResetNfcInterface(void)
94 {
95 nfcHalHandle = nullptr;
96 nfcHalInf.nfcHalOpen = nullptr;
97 nfcHalInf.nfcHalWrite = nullptr;
98 nfcHalInf.nfcHalCoreInitialized = nullptr;
99 nfcHalInf.nfcHalPrediscover = nullptr;
100 nfcHalInf.nfcHalClose = nullptr;
101 nfcHalInf.nfcHalControlGranted = nullptr;
102 nfcHalInf.nfcHalPowerCycle = nullptr;
103 nfcHalInf.nfcHalIoctl = nullptr;
104 nfcHalInf.nfcHalGetConfig = nullptr;
105 nfcHalInf.nfcHalFactoryReset = nullptr;
106 nfcHalInf.nfcHalShutdownCase = nullptr;
107 nfcExtHandle = nullptr;
108 nfcExtInf.getNfcChipType = nullptr;
109 nfcExtInf.getNfcHalFuncNameSuffix = nullptr;
110 }
111
InitNfcHalInterfaces(string nfcHalSoName,string suffix)112 int8_t NfcVendorAdaptions::InitNfcHalInterfaces(string nfcHalSoName, string suffix)
113 {
114 if (nfcHalHandle == nullptr) {
115 nfcHalHandle = dlopen(nfcHalSoName.c_str(), RTLD_LAZY | RTLD_GLOBAL);
116 }
117 if (nfcHalHandle == nullptr) {
118 HDF_LOGE("%{public}s: invalid input path, opening default hal lib", __func__);
119 nfcHalSoName = NFC_HAL_SO_DEFAULT_NAME;
120 suffix = DEFAULT_FUNC_NAME_SUFFIX;
121 nfcHalHandle = dlopen(nfcHalSoName.c_str(), RTLD_LAZY | RTLD_GLOBAL);
122 }
123 if (nfcHalHandle == nullptr) {
124 HDF_LOGE("%{public}s: fail to open hal path.", __func__);
125 return HDF_FAILURE;
126 }
127
128 nfcHalInf.nfcHalOpen = reinterpret_cast<int (*)(NfcStackCallbackT *, NfcStackDataCallbackT *)>
129 (dlsym(nfcHalHandle, (HAL_OPEN_FUNC_NAME + suffix).c_str()));
130
131 nfcHalInf.nfcHalWrite = reinterpret_cast<int (*)(uint16_t, const uint8_t *)>
132 (dlsym(nfcHalHandle, (HAL_WRITE_FUNC_NAME + suffix).c_str()));
133
134 nfcHalInf.nfcHalCoreInitialized = reinterpret_cast<int (*)(uint16_t, uint8_t *)>
135 (dlsym(nfcHalHandle, (HAL_CORE_INIT_FUNC_NAME + suffix).c_str()));
136
137 nfcHalInf.nfcHalPrediscover = reinterpret_cast<int (*)()>
138 (dlsym(nfcHalHandle, (HAL_PRE_DISC_FUNC_NAME + suffix).c_str()));
139
140 nfcHalInf.nfcHalClose = reinterpret_cast<int (*)(bool)>
141 (dlsym(nfcHalHandle, (HAL_CLOSE_FUNC_NAME + suffix).c_str()));
142
143 nfcHalInf.nfcHalControlGranted = reinterpret_cast<int (*)()>
144 (dlsym(nfcHalHandle, (HAL_CTRL_GRANTED_FUNC_NAME + suffix).c_str()));
145
146 nfcHalInf.nfcHalPowerCycle = reinterpret_cast<int (*)()>
147 (dlsym(nfcHalHandle, (HAL_POWER_CYCLE_FUNC_NAME + suffix).c_str()));
148
149 nfcHalInf.nfcHalIoctl = reinterpret_cast<int (*)(long, void *)>
150 (dlsym(nfcHalHandle, (HAL_IOCTL_FUNC_NAME + suffix).c_str()));
151
152 nfcHalInf.nfcHalGetConfig = reinterpret_cast<void (*)(V1_1::NfcVendorConfig &)>
153 (dlsym(nfcHalHandle, (HAL_GET_CONFIG_FUNC_NAME + suffix).c_str()));
154
155 nfcHalInf.nfcHalFactoryReset = reinterpret_cast<void (*)()>
156 (dlsym(nfcHalHandle, (HAL_FACTORY_RESET_FUNC_NAME + suffix).c_str()));
157
158 nfcHalInf.nfcHalShutdownCase = reinterpret_cast<int (*)()>
159 (dlsym(nfcHalHandle, (HAL_SHUTDOWN_CASE_FUNC_NAME + suffix).c_str()));
160
161 if (nfcHalInf.nfcHalOpen == nullptr || nfcHalInf.nfcHalWrite == nullptr ||
162 nfcHalInf.nfcHalCoreInitialized == nullptr || nfcHalInf.nfcHalPrediscover == nullptr ||
163 nfcHalInf.nfcHalClose == nullptr || nfcHalInf.nfcHalControlGranted == nullptr ||
164 nfcHalInf.nfcHalPowerCycle == nullptr || nfcHalInf.nfcHalIoctl == nullptr ||
165 nfcHalInf.nfcHalGetConfig == nullptr || nfcHalInf.nfcHalFactoryReset == nullptr ||
166 nfcHalInf.nfcHalShutdownCase == nullptr) {
167 HDF_LOGE("%{public}s: fail to init func ptr.", __func__);
168 return HDF_FAILURE;
169 }
170 HDF_LOGI("%{public}s: init nfc hal inf successfully.", __func__);
171 return HDF_SUCCESS;
172 }
173
NfcVendorAdaptions()174 NfcVendorAdaptions::NfcVendorAdaptions()
175 {
176 ResetNfcInterface();
177 if (nfcHalHandle == nullptr) {
178 CheckFirmwareUpdate();
179 string chipType = GetChipType();
180 string nfcHalSoName = GetNfcHalSoName(chipType);
181 string nfcHalFuncNameSuffix = GetNfcHalFuncNameSuffix(chipType);
182 if (InitNfcHalInterfaces(nfcHalSoName, nfcHalFuncNameSuffix) != HDF_SUCCESS) {
183 HDF_LOGE("%{public}s: fail to init hal inf.", __func__);
184 }
185 }
186 }
187
~NfcVendorAdaptions()188 NfcVendorAdaptions::~NfcVendorAdaptions() {}
189
VendorOpen(NfcStackCallbackT * pCback,NfcStackDataCallbackT * pDataCback)190 int NfcVendorAdaptions::VendorOpen(NfcStackCallbackT *pCback, NfcStackDataCallbackT *pDataCback)
191 {
192 if (nfcHalInf.nfcHalOpen == nullptr) {
193 HDF_LOGE("%{public}s: Function null.", __func__);
194 return HDF_FAILURE;
195 }
196 if (pCback == nullptr || pDataCback == nullptr) {
197 HDF_LOGE("%{public}s: input param null.", __func__);
198 return HDF_FAILURE;
199 }
200 int ret = nfcHalInf.nfcHalOpen(pCback, pDataCback);
201 return ret;
202 }
203
VendorCoreInitialized(uint16_t coreInitRspLen,uint8_t * pCoreInitRspParams)204 int NfcVendorAdaptions::VendorCoreInitialized(uint16_t coreInitRspLen, uint8_t *pCoreInitRspParams)
205 {
206 if (nfcHalInf.nfcHalCoreInitialized == nullptr) {
207 HDF_LOGE("%{public}s: Function null.", __func__);
208 return HDF_FAILURE;
209 }
210 if (pCoreInitRspParams == nullptr) {
211 HDF_LOGE("%{public}s: input param null.", __func__);
212 return HDF_FAILURE;
213 }
214 int ret = nfcHalInf.nfcHalCoreInitialized(coreInitRspLen, pCoreInitRspParams);
215 return ret;
216 }
217
VendorWrite(uint16_t dataLen,const uint8_t * pData)218 int NfcVendorAdaptions::VendorWrite(uint16_t dataLen, const uint8_t *pData)
219 {
220 if (nfcHalInf.nfcHalWrite == nullptr) {
221 HDF_LOGE("%{public}s: Function null.", __func__);
222 return HDF_FAILURE;
223 }
224 if (pData == nullptr) {
225 HDF_LOGE("%{public}s: input param null.", __func__);
226 return HDF_FAILURE;
227 }
228 int ret = nfcHalInf.nfcHalWrite(dataLen, pData);
229 return ret;
230 }
231
VendorPrediscover(void)232 int NfcVendorAdaptions::VendorPrediscover(void)
233 {
234 if (nfcHalInf.nfcHalPrediscover == nullptr) {
235 HDF_LOGE("%{public}s: Function null.", __func__);
236 return HDF_FAILURE;
237 }
238 int ret = nfcHalInf.nfcHalPrediscover();
239 return ret;
240 }
241
VendorClose(bool bShutdown)242 int NfcVendorAdaptions::VendorClose(bool bShutdown)
243 {
244 if (nfcHalInf.nfcHalClose == nullptr) {
245 HDF_LOGE("%{public}s: Function null.", __func__);
246 return HDF_FAILURE;
247 }
248 int ret = nfcHalInf.nfcHalClose(bShutdown);
249 return ret;
250 }
251
VendorControlGranted(void)252 int NfcVendorAdaptions::VendorControlGranted(void)
253 {
254 if (nfcHalInf.nfcHalControlGranted == nullptr) {
255 HDF_LOGE("%{public}s: Function null.", __func__);
256 return HDF_FAILURE;
257 }
258 int ret = nfcHalInf.nfcHalControlGranted();
259 return ret;
260 }
261
VendorPowerCycle(void)262 int NfcVendorAdaptions::VendorPowerCycle(void)
263 {
264 if (nfcHalInf.nfcHalPowerCycle == nullptr) {
265 HDF_LOGE("%{public}s: Function null.", __func__);
266 return HDF_FAILURE;
267 }
268 int ret = nfcHalInf.nfcHalPowerCycle();
269 return ret;
270 }
271
VendorIoctl(long arg,void * pData)272 int NfcVendorAdaptions::VendorIoctl(long arg, void *pData)
273 {
274 if (nfcHalInf.nfcHalIoctl == nullptr) {
275 HDF_LOGE("%{public}s: Function null.", __func__);
276 return HDF_FAILURE;
277 }
278 if (pData == nullptr) {
279 HDF_LOGE("%{public}s: input param null.", __func__);
280 return HDF_FAILURE;
281 }
282 int ret = nfcHalInf.nfcHalIoctl(arg, pData);
283 return ret;
284 }
285
VendorIoctlWithResponse(long arg,void * pData,uint16_t dataLen,std::vector<uint8_t> & pRetVal)286 int NfcVendorAdaptions::VendorIoctlWithResponse(long arg, void *pData, uint16_t dataLen, std::vector<uint8_t> &pRetVal)
287 {
288 if (nfcHalInf.nfcHalIoctl == nullptr) {
289 HDF_LOGE("%{public}s: Function null.", __func__);
290 return HDF_FAILURE;
291 }
292 if (pData == nullptr) {
293 HDF_LOGE("%{public}s: input param null.", __func__);
294 return HDF_FAILURE;
295 }
296 if (arg == VENDOR_GET_HISTORY_NCI_CMD) {
297 HDF_LOGI("%{public}s: getting history nci from vendor!", __func__);
298 return VendorGetHistoryNci(pData, dataLen, pRetVal);
299 }
300 if (dataLen < VENDOR_IOCTL_INPUT_MIN_LEN || dataLen > VENDOR_IOCTL_TOTAL_LEN) {
301 HDF_LOGE("%{public}s: dataLen is invalid!", __func__);
302 return HDF_ERR_INVALID_PARAM;
303 }
304 uint8_t inOutData[VENDOR_IOCTL_TOTAL_LEN] = { 0 };
305 if (memcpy_s(inOutData, VENDOR_IOCTL_TOTAL_LEN, pData, VENDOR_IOCTL_INOUT_DATA_LEN) != EOK) {
306 HDF_LOGE("%{public}s: memcpy_s pData failed.", __func__);
307 return HDF_FAILURE;
308 }
309 int ret = nfcHalInf.nfcHalIoctl(arg, inOutData);
310 if (ret == HDF_SUCCESS) {
311 uint8_t* pTmp = inOutData;
312 int i;
313 for (i = 0; i <= pTmp[VENDOR_IOCTL_OUTPUT_LEN_INDEX]; i++) {
314 pRetVal.push_back(pTmp[VENDOR_IOCTL_OUTPUT_LEN_INDEX + i]);
315 }
316 }
317 return ret;
318 }
319
VendorGetHistoryNci(void * pData,uint16_t dataLen,std::vector<uint8_t> & pRetVal)320 int NfcVendorAdaptions::VendorGetHistoryNci(void *pData, uint16_t dataLen, std::vector<uint8_t> &pRetVal)
321 {
322 if (dataLen != VENDOR_IOCTL_INPUT_DATA_LEN) {
323 HDF_LOGE("%{public}s: input param data len err.", __func__);
324 return HDF_FAILURE;
325 }
326 std::vector<uint8_t> inOutData(VENDOR_IOCTL_TOTAL_LENGTH, 0);
327 if (memcpy_s(&inOutData[0], inOutData.size(), pData, dataLen) != EOK) {
328 HDF_LOGE("%{public}s: memcpy_s pData failed.", __func__);
329 return HDF_FAILURE;
330 }
331 int ret = nfcHalInf.nfcHalIoctl(VENDOR_GET_HISTORY_NCI_CMD, &inOutData[0]);
332 if (ret == HDF_SUCCESS) {
333 for (uint16_t i = 0; i < VENDOR_IOCTL_OUTPUT_DATA_LEN; i++) {
334 pRetVal.push_back(inOutData[VENDOR_IOCTL_OUTPUT_DATA_START_INDEX + i]);
335 }
336 }
337 return ret;
338 }
339
VendorGetConfig(V1_1::NfcVendorConfig & config)340 int NfcVendorAdaptions::VendorGetConfig(V1_1::NfcVendorConfig &config)
341 {
342 HDF_LOGD("%{public}s: start.", __func__);
343 if (nfcHalInf.nfcHalGetConfig == nullptr) {
344 HDF_LOGE("%{public}s: Function null.", __func__);
345 return HDF_FAILURE;
346 }
347 nfcHalInf.nfcHalGetConfig(config);
348 return HDF_SUCCESS;
349 }
350
VendorFactoryReset(void)351 int NfcVendorAdaptions::VendorFactoryReset(void)
352 {
353 HDF_LOGD("%{public}s: start.", __func__);
354 if (nfcHalInf.nfcHalFactoryReset == nullptr) {
355 HDF_LOGE("%{public}s: Function null.", __func__);
356 return HDF_FAILURE;
357 }
358 nfcHalInf.nfcHalFactoryReset();
359 return HDF_SUCCESS;
360 }
361
VendorShutdownCase(void)362 int NfcVendorAdaptions::VendorShutdownCase(void)
363 {
364 HDF_LOGD("%{public}s: start.", __func__);
365 if (nfcHalInf.nfcHalShutdownCase == nullptr) {
366 HDF_LOGE("%{public}s: Function null.", __func__);
367 return HDF_FAILURE;
368 }
369 int ret = nfcHalInf.nfcHalShutdownCase();
370 return ret;
371 }
372 } // Nfc
373 } // HDI
374 } // OHOS