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