• 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 #ifndef NFC_VENDOR_ADAPTIONS_H
16 #define NFC_VENDOR_ADAPTIONS_H
17 
18 #include <cstdint>
19 #include <string>
20 #include "infc_vendor.h"
21 
22 typedef uint16_t NFCSTATUS;
23 namespace OHOS {
24 namespace HDI {
25 namespace Nfc {
26 const std::string NFC_HAL_SO_DEFAULT_NAME = "libnfc_hdiimpl.z.so";
27 const std::string VENDOR_NFC_EXT_SERVICE_LIB = "libvendor_ext_nfc_service.z.so";
28 
29 const std::string EXT_GET_CHIP_TYPE_FUNC_NAME = "GetChipType";
30 const std::string EXT_GET_SUFFIX_FUNC_NAME = "GetNfcHalFuncNameSuffix";
31 const std::string EXT_SET_FW_UPDATE_CONFIG_FUNC_NAME = "CheckFirmwareUpdate";
32 const std::string EXT_GET_NFC_STATUS_FUNC_NAME = "GetNfcStatus";
33 
34 const std::string HAL_OPEN_FUNC_NAME = "phNxpNciHal_open";
35 const std::string HAL_WRITE_FUNC_NAME = "phNxpNciHal_write";
36 const std::string HAL_CORE_INIT_FUNC_NAME = "phNxpNciHal_core_initialized";
37 const std::string HAL_PRE_DISC_FUNC_NAME = "phNxpNciHal_pre_discover";
38 const std::string HAL_CLOSE_FUNC_NAME = "phNxpNciHal_close";
39 const std::string HAL_CTRL_GRANTED_FUNC_NAME = "phNxpNciHal_control_granted";
40 const std::string HAL_POWER_CYCLE_FUNC_NAME = "phNxpNciHal_power_cycle";
41 const std::string HAL_IOCTL_FUNC_NAME = "phNxpNciHal_ioctl";
42 const std::string HAL_GET_CONFIG_FUNC_NAME = "phNxpNciHal_getVendorConfig_1_2";
43 const std::string HAL_FACTORY_RESET_FUNC_NAME = "phNxpNciHal_do_factory_reset";
44 const std::string HAL_SHUTDOWN_CASE_FUNC_NAME = "phNxpNciHal_configDiscShutdown";
45 const std::string HAL_MIN_OPEN_FUNC_NAME = "phNxpNciHal_MinOpen";
46 const std::string HAL_MIN_CLOSE_FUNC_NAME = "phNxpNciHal_MinClose";
47 const std::string DEFAULT_FUNC_NAME_SUFFIX = "";
48 
49 const std::string NFC_HAL_SO_PREFIX = "libnfc_hal_impl_";
50 const std::string NFC_HAL_SO_SUFFIX = ".z.so";
51 const unsigned int VENDOR_IOCTL_TOTAL_LEN = 256;
52 const unsigned int VENDOR_IOCTL_INOUT_DATA_LEN = 128;
53 const unsigned int VENDOR_IOCTL_OUTPUT_LEN_INDEX = 128;
54 const unsigned int VENDOR_IOCTL_OUTPUT_START_INDEX = 129;
55 const unsigned int VENDOR_IOCTL_INPUT_LEN_INDEX = 0;
56 const unsigned int VENDOR_IOCTL_INPUT_START_INDEX = 1;
57 const unsigned int VENDOR_IOCTL_INPUT_MIN_LEN = 128;
58 
59 const unsigned int VENDOR_IOCTL_INPUT_DATA_LEN = 288;
60 const unsigned int VENDOR_IOCTL_OUTPUT_DATA_START_INDEX = 288;
61 const unsigned int VENDOR_IOCTL_OUTPUT_DATA_LEN = 4128;
62 const unsigned int VENDOR_IOCTL_TOTAL_LENGTH = VENDOR_IOCTL_INPUT_DATA_LEN + VENDOR_IOCTL_OUTPUT_DATA_LEN;
63 const long VENDOR_GET_HISTORY_NCI_CMD = 112;
64 const int NFC_STATUS_OPEN = 0;
65 const int NFC_STATUS_CLOSE = 1;
66 struct NfcHalInterface {
67     int (*nfcHalOpen)(NfcStackCallbackT *pCback, NfcStackDataCallbackT *pDataCback);
68     int (*nfcHalWrite)(uint16_t dataLen, const uint8_t *pData);
69     int (*nfcHalCoreInitialized)(uint16_t coreInitRspLen, uint8_t *pCoreInitRspParams);
70     int (*nfcHalPrediscover)(void);
71     int (*nfcHalClose)(bool bShutdown);
72     int (*nfcHalControlGranted)(void);
73     int (*nfcHalPowerCycle)(void);
74     int (*nfcHalIoctl)(long arg, void *pData);
75     void (*nfcHalGetConfig)(V1_1::NfcVendorConfig &config);
76     void (*nfcHalFactoryReset)(void);
77     int (*nfcHalShutdownCase)(void);
78     NFCSTATUS (*nfcHalMinOpen)(bool isHalPreOpen);
79     NFCSTATUS (*nfcHalMinClose)(void);
80 };
81 
82 struct NfcExtInterface {
83     const char* (*getNfcChipType)(void);
84     const char* (*getNfcHalFuncNameSuffix)(const char* chipType);
85     void (*checkFirmwareUpdate)(void);
86     int (*getNfcStatus)(void);
87 };
88 
89 class NfcVendorAdaptions : public INfcVendor {
90 public:
91     NfcVendorAdaptions();
92     ~NfcVendorAdaptions() override;
93 
94     int VendorOpen(NfcStackCallbackT *pCback, NfcStackDataCallbackT *pDataCback) override;
95     int VendorWrite(uint16_t dataLen, const uint8_t *pData) override;
96     int VendorCoreInitialized(uint16_t coreInitRspLen, uint8_t *pCoreInitRspParams) override;
97     int VendorPrediscover(void) override;
98     int VendorClose(bool bShutdown) override;
99     int VendorControlGranted(void) override;
100     int VendorPowerCycle(void) override;
101     int VendorIoctl(long arg, void *pData) override;
102     int VendorIoctlWithResponse(long arg, void *pData, uint16_t dataLen, std::vector<uint8_t> &pRetVal) override;
103     int VendorGetConfig(V1_1::NfcVendorConfig &config) override;
104     int VendorFactoryReset(void) override;
105     int VendorShutdownCase(void) override;
106     static void *DoHalPreOpen(void *arg);
107     void HalPreOpen(void);
108 
109 private:
110     std::string GetChipType(void);
111     std::string GetNfcHalFuncNameSuffix(const std::string &chipType);
112     void ResetNfcInterface(void);
113     int8_t PreInitNfcHalInterfaces(std::string nfcHalSoName, std::string suffix);
114     int8_t InitNfcHalInterfaces(std::string nfcHalSoName, std::string suffix);
115     void CheckFirmwareUpdate(void);
116     int GetNfcStatus(void);
117     int VendorGetHistoryNci(void *pData, uint16_t dataLen, std::vector<uint8_t> &pRetVal);
118 
119     void *nfcHalHandle; // handle of nfc hal so
120     NfcHalInterface nfcHalInf;
121     bool isNfcPreDone = false;
122     void *nfcExtHandle; // handle of nfc ext service
123     NfcExtInterface nfcExtInf;
124 };
125 } // Nfc
126 } // HDI
127 } // OHOS
128 
129 #endif // NFC_VENDOR_ADAPTIONS_H
130