#ifndef _RVC_DEVICE_ADAPTER_ENTITY_BASE_H__ #define _RVC_DEVICE_ADAPTER_ENTITY_BASE_H__ #pragma once #include "SpBase.h" #include "SpHelper.h" #include "DeviceBaseClass.h" #include "SimpleString.h" #include "ModuleMix.h" #include "path.h" #include "toolkit.h" #include struct VendorLibInfo { CSimpleStringA strDevice; CSimpleStringA strVendor; CSimpleStringA strVersion; CSimpleStringA strBatch; bool fileServering; CSimpleStringA strFileValue; VendorLibInfo() :strDevice(true), strVendor(true), strVersion(true), strBatch(true), fileServering(false), strFileValue(true){} VendorLibInfo(const CSimpleStringA& strDeviceName) :strDevice(strDeviceName), strVendor(true), strVersion(true), strBatch(true), fileServering(false), strFileValue(true) {} VendorLibInfo(const VendorLibInfo& rhs) :strDevice(rhs.strDevice), strVendor(rhs.strVendor), strVersion(rhs.strVersion), strBatch(rhs.strBatch), fileServering(rhs.fileServering), strFileValue(rhs.strFileValue) {} VendorLibInfo& operator = (const VendorLibInfo& rhs) { strDevice = rhs.strDevice; strVendor = rhs.strVendor; strVersion = rhs.strVersion; strBatch = rhs.strBatch; fileServering = rhs.fileServering; strFileValue = rhs.strFileValue; return *this; } bool IsValid() const { if (!strFileValue.IsNullOrEmpty()) return true; return (!strDevice.IsNullOrEmpty() && !strVendor.IsNullOrEmpty() && !strVersion.IsNullOrEmpty() && !strBatch.IsNullOrEmpty()); } bool IsNotConfig() const { return(strFileValue.IsNullOrEmpty() && strVendor.IsNullOrEmpty() && strVersion.IsNullOrEmpty() && strBatch.IsNullOrEmpty()); } int GetVersionInt() const { int result = 0; if (!strVersion.IsNullOrEmpty()) { result = atoi(strVersion.GetData()); } return result; } int GetBatchInt() const { int result = 0; if (!strBatch.IsNullOrEmpty()) { result = atoi(strBatch.GetData()); } return result; } CSimpleStringA toLibNameString() const { if (!strFileValue.IsNullOrEmpty()) { return strFileValue; } CSimpleStringA strFullLibName(true); if (!strDevice.IsNullOrEmpty()) { strFullLibName = strDevice; if (!strVendor.IsNullOrEmpty()) { strFullLibName += "."; strFullLibName += strVendor; } if (!strVersion.IsNullOrEmpty()) { strFullLibName += "."; strFullLibName += strVersion; } if (!strBatch.IsNullOrEmpty()) { strFullLibName += "."; strFullLibName += strBatch; } #ifdef RVC_OS_WIN strFullLibName += ".dll"; #else CSimpleStringA strPrefix("lib"); strPrefix += strFullLibName; strPrefix += ".so"; strFullLibName = strPrefix; #endif //RVC_OS_WIN } return strFullLibName; } }; class CDevAdptEntityBase : public CEntityBase { public: CDevAdptEntityBase() { } ErrorCodeEnum LoadVendorLibName() { if (!vendorLibInfo.IsValid()) { return ExtractVendorLibName(); } return IsNotConfigure() ? Error_NotConfig : Error_Succeed; } virtual CSimpleStringA GetVendorLibName() { if (!vendorLibInfo.IsValid()) { ExtractVendorLibName(); } return vendorLibInfo.toLibNameString(); } virtual ErrorCodeEnum ExtractVendorLibFullPath(CSimpleStringA& csLibFullPath); virtual void InitializeVendorLogSwitch(); virtual ~CDevAdptEntityBase() {} public: VendorLibInfo vendorLibInfo; protected: virtual ErrorCodeEnum CustomVendorLibInfo() { return Error_Succeed; } private: ErrorCodeEnum ExtractVendorLibName(); bool IsNotConfigure() const { return vendorLibInfo.IsNotConfig(); } }; inline ErrorCodeEnum CDevAdptEntityBase::ExtractVendorLibName() { CSmartPointer spRootConfig; ErrorCodeEnum erroCode = GetFunction()->OpenConfig(Config_Root, spRootConfig); if (IS_SUCCEED(erroCode)) { CSimpleStringA strDeviceEntityName = GetEntityName(); #ifndef DEVOPS_ON_PRD //非生产情况下,才支持测试模式 CSystemRunInfo runInfo = { 0 }; ErrorCodeEnum ec = GetFunction()->GetSystemRunInfo(runInfo); if (ec == Error_Succeed && runInfo.autoTest) //识别成测试模式的条件 { vendorLibInfo.strDevice = strDeviceEntityName; vendorLibInfo.strVendor = "simulator"; vendorLibInfo.strVersion = "1"; vendorLibInfo.strBatch = "1"; } else #endif { if (strDeviceEntityName.Compare("CardIssuerStore", true) == 0 || strDeviceEntityName.Compare("CardIssuerStand", true) == 0) { strDeviceEntityName = "CardIssuer"; } CSimpleStringA strSection = CSimpleStringA("Device.") + strDeviceEntityName; vendorLibInfo.strDevice = strDeviceEntityName; CSimpleStringA strValue(true); spRootConfig->ReadConfigValue(strSection, "File", strValue); if (!strValue.IsNullOrEmpty()) { vendorLibInfo.strFileValue = strValue; vendorLibInfo.fileServering = true; } else { //TODO: 需要处理部分硬件实体依赖Vendor字段实现某些功能的风险 [Gifur@20241021] spRootConfig->ReadConfigValue(strSection, "Vendor", vendorLibInfo.strVendor); spRootConfig->ReadConfigValue(strSection, "Version", vendorLibInfo.strVersion); spRootConfig->ReadConfigValue(strSection, "Batch", vendorLibInfo.strBatch); } } } if (IS_SUCCEED(erroCode)) { erroCode = CustomVendorLibInfo(); } return erroCode; } inline ErrorCodeEnum CDevAdptEntityBase::ExtractVendorLibFullPath(CSimpleStringA& csLibFullPath) { CSimpleStringA strLibName = GetVendorLibName(); CSimpleStringA strDepPath; ErrorCodeEnum rc = GetFunction()->GetPath("Dep", strDepPath); if (rc == Error_Succeed) { csLibFullPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", (LPCTSTR)strDepPath, (LPCTSTR)strLibName); rc = IsNotConfigure() ? Error_NotConfig : Error_Succeed; } else { DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("Get Dep path failed: %s", SpStrError(rc)); } return rc; } inline void CDevAdptEntityBase::InitializeVendorLogSwitch() { if (!vendorLibInfo.IsValid()) { ExtractVendorLibName(); } struct VendorLogConfig { VendorLogConfig() :strLevel("OFF"), strType("FILE"), strDllName(""), strLogPath(""), strModuleName(""){} CSimpleStringA strLevel; CSimpleStringA strType; CSimpleStringA strDllName; CSimpleStringA strLogPath; CSimpleStringA strModuleName; void Settle() { toolkit_setenv("VENDOR_RECODE_LEVEL", strLevel); toolkit_setenv("VENDOR_RECODE_TYPE", strType); toolkit_setenv("VENDOR_DLL_NAME", strDllName); toolkit_setenv("VENDOR_LOG_PATH", strLogPath); toolkit_setenv("VENDOR_PARENT_NAME", strModuleName); } } stLogConfig; stLogConfig.strDllName = vendorLibInfo.toLibNameString().GetData(); stLogConfig.strModuleName = GetEntityName(); CSmartPointer centerConfig; GetFunction()->OpenConfig(Config_CenterSetting, centerConfig); CSimpleStringA str; DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("To get config [All] key..."); centerConfig->ReadConfigValue(GetEntityName(), "All", str); DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("All: %s", str.GetData()); if (!str.IsNullOrEmpty()) { stLogConfig.strLevel = str; } if (stLogConfig.strLevel.Compare("OFF", true) != 0) { stLogConfig.strType = "UPLOAD"; int nSaveFileOrNot(0); centerConfig->ReadConfigValueInt("Common", "SaveFile", nSaveFileOrNot); if ((nSaveFileOrNot & 1) == 1) { if (!stLogConfig.strType.IsNullOrEmpty()) stLogConfig.strType += "|"; stLogConfig.strType += "FILE"; } GetFunction()->GetPath("Dbg", stLogConfig.strLogPath); DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("DbgPath: %s", stLogConfig.strLogPath.GetData()); stLogConfig.Settle(); DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("{\"RecordLevel\":\"%s\", \"RecordType\":\"%s\"}", stLogConfig.strLevel.GetData(),stLogConfig.strType.GetData()); } } #define GET_DEV_ENTITY_BASE_POINTER() \ (dynamic_cast(GetEntityBase())) #endif /*_RVC_DEVICE_ADAPTER_ENTITY_BASE_H__*/