DevEntityCommBase.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #ifndef _RVC_DEVICE_ADAPTER_ENTITY_BASE_H__
  2. #define _RVC_DEVICE_ADAPTER_ENTITY_BASE_H__
  3. #pragma once
  4. #include "SpBase.h"
  5. #include "SpHelper.h"
  6. #include "DeviceBaseClass.h"
  7. #include "SimpleString.h"
  8. #include "ModuleMix.h"
  9. #include "path.h"
  10. #include "toolkit.h"
  11. #include <regex>
  12. struct VendorLibInfo
  13. {
  14. CSimpleStringA strDevice;
  15. CSimpleStringA strVendor;
  16. CSimpleStringA strVersion;
  17. CSimpleStringA strBatch;
  18. bool fileServering;
  19. CSimpleStringA strFileValue;
  20. VendorLibInfo() :strDevice(true), strVendor(true), strVersion(true), strBatch(true), fileServering(false), strFileValue(true){}
  21. VendorLibInfo(const CSimpleStringA& strDeviceName) :strDevice(strDeviceName), strVendor(true), strVersion(true), strBatch(true), fileServering(false), strFileValue(true) {}
  22. VendorLibInfo(const VendorLibInfo& rhs) :strDevice(rhs.strDevice), strVendor(rhs.strVendor), strVersion(rhs.strVersion), strBatch(rhs.strBatch), fileServering(rhs.fileServering), strFileValue(rhs.strFileValue) {}
  23. VendorLibInfo& operator = (const VendorLibInfo& rhs) {
  24. strDevice = rhs.strDevice;
  25. strVendor = rhs.strVendor;
  26. strVersion = rhs.strVersion;
  27. strBatch = rhs.strBatch;
  28. fileServering = rhs.fileServering;
  29. strFileValue = rhs.strFileValue;
  30. return *this;
  31. }
  32. bool IsValid() const
  33. {
  34. if (!strFileValue.IsNullOrEmpty()) return true;
  35. return (!strDevice.IsNullOrEmpty()
  36. && !strVendor.IsNullOrEmpty()
  37. && !strVersion.IsNullOrEmpty()
  38. && !strBatch.IsNullOrEmpty());
  39. }
  40. bool IsNotConfig() const
  41. {
  42. return(strFileValue.IsNullOrEmpty() && strVendor.IsNullOrEmpty() && strVersion.IsNullOrEmpty() && strBatch.IsNullOrEmpty());
  43. }
  44. int GetVersionInt() const
  45. {
  46. int result = 0;
  47. if (!strVersion.IsNullOrEmpty())
  48. {
  49. result = atoi(strVersion.GetData());
  50. }
  51. return result;
  52. }
  53. int GetBatchInt() const
  54. {
  55. int result = 0;
  56. if (!strBatch.IsNullOrEmpty())
  57. {
  58. result = atoi(strBatch.GetData());
  59. }
  60. return result;
  61. }
  62. CSimpleStringA toLibNameString() const
  63. {
  64. if (!strFileValue.IsNullOrEmpty()) {
  65. return strFileValue;
  66. }
  67. CSimpleStringA strFullLibName(true);
  68. if (!strDevice.IsNullOrEmpty())
  69. {
  70. strFullLibName = strDevice;
  71. if (!strVendor.IsNullOrEmpty())
  72. {
  73. strFullLibName += ".";
  74. strFullLibName += strVendor;
  75. }
  76. if (!strVersion.IsNullOrEmpty())
  77. {
  78. strFullLibName += ".";
  79. strFullLibName += strVersion;
  80. }
  81. if (!strBatch.IsNullOrEmpty())
  82. {
  83. strFullLibName += ".";
  84. strFullLibName += strBatch;
  85. }
  86. #ifdef RVC_OS_WIN
  87. strFullLibName += ".dll";
  88. #else
  89. CSimpleStringA strPrefix("lib");
  90. strPrefix += strFullLibName;
  91. strPrefix += ".so";
  92. strFullLibName = strPrefix;
  93. #endif //RVC_OS_WIN
  94. }
  95. return strFullLibName;
  96. }
  97. };
  98. class CDevAdptEntityBase : public CEntityBase
  99. {
  100. public:
  101. CDevAdptEntityBase() { }
  102. ErrorCodeEnum LoadVendorLibName()
  103. {
  104. if (!vendorLibInfo.IsValid()) {
  105. return ExtractVendorLibName();
  106. }
  107. return IsNotConfigure() ? Error_NotConfig : Error_Succeed;
  108. }
  109. virtual CSimpleStringA GetVendorLibName()
  110. {
  111. if (!vendorLibInfo.IsValid()) {
  112. ExtractVendorLibName();
  113. }
  114. return vendorLibInfo.toLibNameString();
  115. }
  116. virtual ErrorCodeEnum ExtractVendorLibFullPath(CSimpleStringA& csLibFullPath);
  117. virtual void InitializeVendorLogSwitch();
  118. virtual ~CDevAdptEntityBase() {}
  119. public:
  120. VendorLibInfo vendorLibInfo;
  121. protected:
  122. virtual ErrorCodeEnum CustomVendorLibInfo() { return Error_Succeed; }
  123. private:
  124. ErrorCodeEnum ExtractVendorLibName();
  125. bool IsNotConfigure() const
  126. {
  127. return vendorLibInfo.IsNotConfig();
  128. }
  129. };
  130. inline ErrorCodeEnum CDevAdptEntityBase::ExtractVendorLibName()
  131. {
  132. CSmartPointer<IConfigInfo> spRootConfig;
  133. ErrorCodeEnum erroCode = GetFunction()->OpenConfig(Config_Root, spRootConfig);
  134. if (IS_SUCCEED(erroCode))
  135. {
  136. CSimpleStringA strDeviceEntityName = GetEntityName();
  137. #ifndef DEVOPS_ON_PRD
  138. //非生产情况下,才支持测试模式
  139. CSystemRunInfo runInfo = { 0 };
  140. ErrorCodeEnum ec = GetFunction()->GetSystemRunInfo(runInfo);
  141. if (ec == Error_Succeed && runInfo.autoTest) //识别成测试模式的条件
  142. {
  143. vendorLibInfo.strDevice = strDeviceEntityName;
  144. vendorLibInfo.strVendor = "simulator";
  145. vendorLibInfo.strVersion = "1";
  146. vendorLibInfo.strBatch = "1";
  147. }
  148. else
  149. #endif
  150. {
  151. if (strDeviceEntityName.Compare("CardIssuerStore", true) == 0 || strDeviceEntityName.Compare("CardIssuerStand", true) == 0) {
  152. strDeviceEntityName = "CardIssuer";
  153. }
  154. CSimpleStringA strSection = CSimpleStringA("Device.") + strDeviceEntityName;
  155. vendorLibInfo.strDevice = strDeviceEntityName;
  156. CSimpleStringA strValue(true);
  157. spRootConfig->ReadConfigValue(strSection, "File", strValue);
  158. if (!strValue.IsNullOrEmpty()) { vendorLibInfo.strFileValue = strValue; vendorLibInfo.fileServering = true; }
  159. else {
  160. //TODO: 需要处理部分硬件实体依赖Vendor字段实现某些功能的风险 [Gifur@20241021]
  161. spRootConfig->ReadConfigValue(strSection, "Vendor", vendorLibInfo.strVendor);
  162. spRootConfig->ReadConfigValue(strSection, "Version", vendorLibInfo.strVersion);
  163. spRootConfig->ReadConfigValue(strSection, "Batch", vendorLibInfo.strBatch);
  164. }
  165. }
  166. }
  167. if (IS_SUCCEED(erroCode)) {
  168. erroCode = CustomVendorLibInfo();
  169. }
  170. return erroCode;
  171. }
  172. inline ErrorCodeEnum CDevAdptEntityBase::ExtractVendorLibFullPath(CSimpleStringA& csLibFullPath)
  173. {
  174. CSimpleStringA strLibName = GetVendorLibName();
  175. CSimpleStringA strDepPath;
  176. ErrorCodeEnum rc = GetFunction()->GetPath("Dep", strDepPath);
  177. if (rc == Error_Succeed) {
  178. csLibFullPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", (LPCTSTR)strDepPath, (LPCTSTR)strLibName);
  179. rc = IsNotConfigure() ? Error_NotConfig : Error_Succeed;
  180. }
  181. else {
  182. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("Get Dep path failed: %s", SpStrError(rc));
  183. }
  184. return rc;
  185. }
  186. inline void CDevAdptEntityBase::InitializeVendorLogSwitch()
  187. {
  188. if (!vendorLibInfo.IsValid()) {
  189. ExtractVendorLibName();
  190. }
  191. struct VendorLogConfig {
  192. VendorLogConfig() :strLevel("OFF"), strType("FILE"), strDllName(""), strLogPath(""), strModuleName(""){}
  193. CSimpleStringA strLevel;
  194. CSimpleStringA strType;
  195. CSimpleStringA strDllName;
  196. CSimpleStringA strLogPath;
  197. CSimpleStringA strModuleName;
  198. void Settle() {
  199. toolkit_setenv("VENDOR_RECODE_LEVEL", strLevel);
  200. toolkit_setenv("VENDOR_RECODE_TYPE", strType);
  201. toolkit_setenv("VENDOR_DLL_NAME", strDllName);
  202. toolkit_setenv("VENDOR_LOG_PATH", strLogPath);
  203. toolkit_setenv("VENDOR_PARENT_NAME", strModuleName);
  204. }
  205. } stLogConfig;
  206. stLogConfig.strDllName = vendorLibInfo.toLibNameString().GetData();
  207. stLogConfig.strModuleName = GetEntityName();
  208. CSmartPointer<IConfigInfo> centerConfig;
  209. GetFunction()->OpenConfig(Config_CenterSetting, centerConfig);
  210. CSimpleStringA str;
  211. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("To get config [All] key...");
  212. centerConfig->ReadConfigValue(GetEntityName(), "All", str);
  213. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("All: %s", str.GetData());
  214. if (!str.IsNullOrEmpty()) {
  215. stLogConfig.strLevel = str;
  216. }
  217. if (stLogConfig.strLevel.Compare("OFF", true) != 0) {
  218. stLogConfig.strType = "UPLOAD";
  219. int nSaveFileOrNot(0);
  220. centerConfig->ReadConfigValueInt("Common", "SaveFile", nSaveFileOrNot);
  221. if ((nSaveFileOrNot & 1) == 1) {
  222. if (!stLogConfig.strType.IsNullOrEmpty()) stLogConfig.strType += "|";
  223. stLogConfig.strType += "FILE";
  224. }
  225. GetFunction()->GetPath("Dbg", stLogConfig.strLogPath);
  226. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("DbgPath: %s", stLogConfig.strLogPath.GetData());
  227. stLogConfig.Settle();
  228. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("{\"RecordLevel\":\"%s\", \"RecordType\":\"%s\"}",
  229. stLogConfig.strLevel.GetData(),stLogConfig.strType.GetData());
  230. }
  231. }
  232. #define GET_DEV_ENTITY_BASE_POINTER() \
  233. (dynamic_cast<CDevAdptEntityBase*>(GetEntityBase()))
  234. #endif /*_RVC_DEVICE_ADAPTER_ENTITY_BASE_H__*/