DevFSMCommBase.hpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. #ifndef _COMM_DEVICE_FSM_BASE_HPP_
  2. #define _COMM_DEVICE_FSM_BASE_HPP_
  3. #include "DevEntityCommBase.hpp"
  4. #include "CommEntityUtil.hpp"
  5. #include "SpFSM.h"
  6. #include "RVCComm.h"
  7. #include "DevErrorCode.h"
  8. #include "DeviceBaseClass.h"
  9. #include "publicFunExport.h"
  10. #include <winpr/file.h>
  11. #include <winpr/sysinfo.h>
  12. #include <map>
  13. #include <string>
  14. #include "fileutil.h"
  15. #if defined(_MSC_VER)
  16. #pragma comment(lib, "Version.lib")
  17. typedef ErrorCodeEnum(*DevAdaptObjCreateFunc)(DeviceBaseClass*& baseObj);
  18. typedef ErrorCodeEnum(*DevAdaptObjReleaseFunc)(DeviceBaseClass*& pBaseObj);
  19. typedef void(*GetPrefixErrMsgFunc)(char* szErrMsg, DWORD* pMsgLen);
  20. #else //C/C++ Compiler
  21. using DevAdaptObjCreateFunc = ErrorCodeEnum(*)(DeviceBaseClass*& pOutDevAptObj);
  22. using DevAdaptObjReleaseFunc = ErrorCodeEnum(*)(DeviceBaseClass*& pInDevAptObj);
  23. using GetPrefixErrMsgFunc = void(*)(char* szErrMsg, DWORD* pMsgLen);
  24. #endif //_MSC_VER
  25. typedef ErrorCodeEnum(*GetDevAdapterVersionFunc)(DevSoftVersion& retVersion);
  26. #define HARDWARE_ENTITY_RESET_ENTITYID(ent, entityID) \
  27. do{\
  28. memset(&ent, 0, sizeof(ent)); \
  29. ent.dwEntityId = entityID; \
  30. } while (false)
  31. #define HARDWARE_ENTITY_SET_DEVCODE(ent, ec, reserved) \
  32. do {\
  33. ent.dwVendorErroCode = ec; \
  34. ent.dwReserved = reserved; \
  35. } while (false)
  36. #define HARDWARE_ENTITY_MAKE_ERRORCODE(ec) \
  37. MAKE_SLV_ERRORCODE(ec.dwEntityId, (ec.dwVendorId), ec.dwVendorErroCode)
  38. #define HARDWARE_ENTITY_MAKE_ERRORCODE_TO_BUSINESS(ec) \
  39. MAKE_SLV_ERRORCODE_TO_BUSINESS(ec.dwEntityId, ec.dwVendorErroCode, ec.dwReserved)
  40. enum PageType
  41. {
  42. PageType_Init,
  43. PageType_MainPage,
  44. PageType_UserDesktop,
  45. PageType_Other,
  46. };
  47. template <typename TSubAdpt>
  48. struct DevAdptLibHelper
  49. {
  50. private:
  51. bool CheckAndGetMapAdatperName(const CSimpleStringA& strPath, CSimpleStringA& replaceAdapterName)
  52. {
  53. bool result = false;
  54. CSimpleStringA strTest = m_strAdapterName.IsNullOrEmpty() ? strPath.GetData() : m_strAdapterName.GetData();
  55. for (int i = 0; i < strTest.GetLength(); ++i) {
  56. if (strTest[i] >= 'A' && strTest[i] <= 'Z') strTest[i] = strTest[i] + ('a' - 'A');
  57. }
  58. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("strTest:%s", strPath.GetData());
  59. if (strTest.IndexOf("pinpad") != -1) {
  60. replaceAdapterName = "PinPad.self.1.1.dll";
  61. result = true;
  62. } else if (strTest.IndexOf("cardissuer") != -1) {
  63. replaceAdapterName = "CardIssuer.self.1.1.dll";
  64. result = true;
  65. } else if (strTest.IndexOf("contactlesscard") != -1) {
  66. replaceAdapterName = "ContactlessCard.self.1.1.dll";
  67. result = true;
  68. } else if (strTest.IndexOf("fingerprint") != -1) {
  69. replaceAdapterName = "FingerPrint.self.1.1.dll";
  70. result = true;
  71. } else if (strTest.IndexOf("gpio") != -1) {
  72. replaceAdapterName = "GPIO.self.1.1.dll";
  73. result = true;
  74. } else if (strTest.IndexOf("idcertificate") != -1) {
  75. replaceAdapterName = "IDCertificate.self.1.1.dll";
  76. result = true;
  77. } else if (strTest.IndexOf("hspscanner") != -1) {
  78. replaceAdapterName = "HSPSCanner.self.1.1.dll";
  79. result = true;
  80. }
  81. return result;
  82. }
  83. bool PreLoadLib(CSimpleStringA& strFullLibPath)
  84. {
  85. bool result(false);
  86. ///*TODO: Test (80374374@1/23/2024)*/
  87. DWORD d1;
  88. DWORD d2;
  89. DWORD d3;
  90. DWORD d4;
  91. if (!ExistsFileA(strFullLibPath)) {
  92. return result;
  93. }
  94. #ifndef DEVOPS_ON_PRD
  95. GetDllVersionInfo(strFullLibPath, d1, d2, d3, d4);
  96. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("dll %s version: %d.%d.%d.%d", strFullLibPath.GetData(), d1, d2, d3, d4);
  97. //VC16.0 [1920,1929] VS2019 142
  98. if (d4 >= 1920 && d4 <= 1929 && d3 == 142) {
  99. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("detect it built with VS2019, skip latter procedure, version: %d.%d.%d.%d", d1, d2, d3, d4);
  100. return result;
  101. }
  102. #endif
  103. CSimpleStringA strTest = strFullLibPath.GetData();
  104. CSimpleStringA strSelf(true);
  105. if (CheckAndGetMapAdatperName(strTest, strSelf)) {
  106. int index = -1;
  107. for (int i = 0; i < strTest.GetLength(); ++i)
  108. if (strTest[i] == '\\' || strTest[i] == '/') index = i;
  109. if (index != -1) {
  110. toolkit_setenv("INVOKE_VENDOR_ADAPTER_NAME", strFullLibPath.GetData());
  111. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("env:%s", strFullLibPath.GetData());
  112. CSimpleStringA strAdapterName = strTest.SubString(index + 1);
  113. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("adapter name:%s", strAdapterName.GetData());
  114. strTest[index + 1] = '\0'; //!!!!
  115. strFullLibPath = strTest.SubString(0, index + 1);
  116. strFullLibPath += strSelf;
  117. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("new adapter path:%s", strFullLibPath.GetData());
  118. result = true;
  119. }
  120. }
  121. return result;
  122. }
  123. public:
  124. void SetAdapterName(const CSimpleStringA& strEntityName) { m_strAdapterName = strEntityName; }
  125. ErrorCodeEnum LoadLibAddress(const CSimpleStringA& strFullLibPath)
  126. {
  127. ErrorCodeEnum erroCode = Error_Succeed;
  128. CSimpleStringA strTemp = strFullLibPath.GetData();
  129. #if defined(_MSC_VER)
  130. m_VS2010BridgeMode = PreLoadLib(strTemp);
  131. #endif //_MSC_VER
  132. do {
  133. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to load lib: %s", strTemp.GetData());
  134. if (!ExistsFileA(strTemp)) {
  135. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("the lib file %s is not existed!", strTemp.GetData());
  136. erroCode = Error_NotExist;
  137. break;
  138. }
  139. int res = toolkit_dlopen(strTemp, &m_lib);
  140. if (res != 0) {
  141. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("toolkit_dlopen[%s] failed with error %s.", strTemp.GetData(), toolkit_dlerror(&m_lib));
  142. erroCode = Error_DevLoadFileFailed;
  143. break;
  144. }
  145. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to get CreateDevComponent pointer address...");
  146. if ((res = toolkit_dlsym(&m_lib, "CreateDevComponent", (void**)&pFuncCreateAdapt)) != 0)
  147. {
  148. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("Get 'CreateDevComponent' Func address failed with error: %s", toolkit_dlerror(&m_lib));
  149. erroCode = Error_DevLoadFileFailed;
  150. break;
  151. }
  152. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to get ReleaseDevComponent pointer address...");
  153. if ((res = toolkit_dlsym(&m_lib, "ReleaseDevComponent", (void**)&pFuncReleaseAdapt)) != 0)
  154. {
  155. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("Get 'ReleaseDevComponent' Func address failed with error: %s", toolkit_dlerror(&m_lib));
  156. erroCode = Error_DevLoadFileFailed;
  157. break;
  158. }
  159. #if defined(RVC_OS_WIN)
  160. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to get GetMoreErrorDetail pointer address...");
  161. if ((res = toolkit_dlsym(&m_lib, "GetMoreErrorDetail", (void**)&pFuncGetPrefixErr)) != 0)
  162. {
  163. pFuncGetPrefixErr = nullptr;
  164. }
  165. else {
  166. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Get 'GetMoreErrorDetail' Func address succ");
  167. }
  168. #endif //RVC_OS_WIN
  169. } while (false);
  170. if (IS_FAILURED(erroCode))
  171. {
  172. TearDown();
  173. }
  174. return erroCode;
  175. }
  176. ErrorCodeEnum CreateDevAdptObject()
  177. {
  178. if (m_AdptObjPtr != nullptr) {
  179. return Error_AlreadyExist;
  180. }
  181. if (pFuncCreateAdapt == nullptr || pFuncReleaseAdapt == nullptr) {
  182. return Error_NotInit;
  183. }
  184. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to get Adapter object...");
  185. ErrorCodeEnum erroCode = pFuncCreateAdapt((DeviceBaseClass*&)m_AdptObjPtr);
  186. if (IS_FAILURED(erroCode)) {
  187. if (pFuncGetPrefixErr != nullptr) {
  188. char szErrMsg[MAX_DEV_ERROR_MSG_LEN];
  189. memset(szErrMsg, '\0', sizeof(szErrMsg));
  190. DWORD dwLen = MAX_DEV_ERROR_MSG_LEN;
  191. pFuncGetPrefixErr(szErrMsg, &dwLen);
  192. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("Create device adapter object failed! EC=%s, Msg:[%s]"
  193. , SpStrError(erroCode), szErrMsg);
  194. }
  195. else {
  196. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("Create device adapter object failed! EC=%s", SpStrError(erroCode));
  197. }
  198. return erroCode;
  199. }
  200. LOG_ASSERT(m_AdptObjPtr != nullptr);
  201. return Error_Succeed;
  202. }
  203. /** Integrate function LoadLibAddress and CreateDevAdptObject, please use LoadUpAdapterLibrary replace with it*/
  204. ErrorCodeEnum LoadUp(const CSimpleStringA& strFullLibPath)
  205. {
  206. ErrorCodeEnum erroCode = LoadLibAddress(strFullLibPath);
  207. if (IS_SUCCEED(erroCode)) {
  208. erroCode = CreateDevAdptObject();
  209. if (!IS_SUCCEED(erroCode)) {
  210. TearDown();
  211. }
  212. }
  213. return erroCode;
  214. }
  215. void TearDown()
  216. {
  217. LOG_FUNCTION();
  218. if (m_AdptObjPtr != nullptr)
  219. {
  220. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("To DevClose...");
  221. ErrorCodeEnum result = m_AdptObjPtr->DevClose();
  222. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("DevClose returned %s", SpStrError(result));
  223. if (nullptr != pFuncReleaseAdapt)
  224. {
  225. DeviceBaseClass* devBasePtr = static_cast<DeviceBaseClass*>(m_AdptObjPtr);
  226. if (devBasePtr != nullptr) {
  227. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("To Release DevAdapter Object...");
  228. pFuncReleaseAdapt(devBasePtr);
  229. m_AdptObjPtr = nullptr;
  230. } else {
  231. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("static cast base dev class failed!");
  232. }
  233. }
  234. }
  235. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to dlclose DevAdapter libs...");
  236. toolkit_dlclose(&m_lib);
  237. pFuncReleaseAdapt = pFuncCreateAdapt = nullptr;
  238. pFuncGetPrefixErr = nullptr;
  239. }
  240. #if defined(RVC_OS_WIN)
  241. ErrorCodeEnum GetDllVersionInfo(LPCTSTR lpcszFilePath, DWORD& dwMajor, DWORD& dwMinor, DWORD& dwBuild, DWORD& dwRevision)
  242. {
  243. char* buf = NULL;
  244. dwMajor = dwMinor = dwBuild = dwRevision = (DWORD)(-1);
  245. DWORD dwHnd;
  246. DWORD dwVerInfoSize;
  247. if (0 >= (dwVerInfoSize = GetFileVersionInfoSizeA(lpcszFilePath, &dwHnd))) {
  248. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("GetFileVersionInfoSizeA failed: %u", GetLastError());
  249. return Error_Unexpect;
  250. }
  251. buf = new char[dwVerInfoSize];
  252. if (NULL == buf) {
  253. return Error_Resource;
  254. }
  255. // get file version info
  256. if (!GetFileVersionInfo(lpcszFilePath, dwHnd, dwVerInfoSize, buf)) {
  257. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("GetFileVersionInfo failed: %u", GetLastError());
  258. delete[] buf;
  259. return Error_Unexpect;
  260. }
  261. // get FileVersion string from resource
  262. VS_FIXEDFILEINFO* ptrVersion;
  263. unsigned int versionLen = 0;
  264. if (!VerQueryValue(buf, "\\", (void**)&ptrVersion, &versionLen)) {
  265. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("VS_FIXEDFILEINFO failed: %u", GetLastError());
  266. delete[] buf;
  267. return Error_Unexpect;
  268. }
  269. dwMajor = (ptrVersion->dwFileVersionMS >> 16) & 0x0000FFFF;
  270. dwMinor = ptrVersion->dwFileVersionMS & 0x0000FFFF;
  271. dwBuild = (ptrVersion->dwFileVersionLS >> 16) & 0x0000FFFF;
  272. dwRevision = ptrVersion->dwFileVersionLS & 0x0000FFFF;
  273. delete[] buf;
  274. return Error_Succeed;
  275. }
  276. #else
  277. ErrorCodeEnum GetDllVersionInfo(LPCTSTR lpcszFilePath, DWORD& dwMajor, DWORD& dwMinor, DWORD& dwBuild, DWORD& dwRevision)
  278. {
  279. dwMajor = dwMinor = dwBuild = dwRevision = 0;
  280. return Error_NotSupport;
  281. }
  282. #endif //RVC_OS_WIN
  283. #if defined(_MSC_VER)
  284. operator bool() const { return (m_AdptObjPtr != nullptr); }
  285. #else
  286. /** Linux 下不加此会影响 编译 Gifur@2023329]*/
  287. explicit operator bool() const { return (m_AdptObjPtr != nullptr); }
  288. #endif //_MSC_VER
  289. operator DeviceBaseClass* () { return m_AdptObjPtr; }
  290. TSubAdpt* GetDevPointer() { return m_AdptObjPtr; }
  291. TSubAdpt* operator->()
  292. {
  293. if (m_AdptObjPtr == nullptr) {
  294. throw std::runtime_error("the dev class pointer is nullptr!!");
  295. }
  296. return m_AdptObjPtr;
  297. }
  298. bool operator !=(TSubAdpt* rhs)
  299. {
  300. return !(m_AdptObjPtr == rhs);
  301. }
  302. bool operator !=(const DevAdptLibHelper& rhs)
  303. {
  304. return !(m_AdptObjPtr == rhs.operator->());
  305. }
  306. DevAdptLibHelper()
  307. : pFuncCreateAdapt(nullptr), pFuncReleaseAdapt(nullptr), pFuncGetPrefixErr(nullptr)
  308. , m_AdptObjPtr(nullptr), m_VS2010BridgeMode(false), m_strAdapterName(true)
  309. {
  310. memset(&m_lib, 0, sizeof(toolkit_lib_t));
  311. }
  312. ~DevAdptLibHelper()
  313. {
  314. TearDown();
  315. }
  316. #if defined(RVC_OS_LINUX)
  317. DevAdptLibHelper(const DevAdptLibHelper&) = delete;
  318. DevAdptLibHelper& operator=(const DevAdptLibHelper&) = delete;
  319. #endif //RVC_OS_LINUX
  320. private:
  321. toolkit_lib_t m_lib;
  322. DevAdaptObjCreateFunc pFuncCreateAdapt;
  323. DevAdaptObjReleaseFunc pFuncReleaseAdapt;
  324. GetDevAdapterVersionFunc pFuncGetDevAdapterVer;
  325. GetPrefixErrMsgFunc pFuncGetPrefixErr;
  326. TSubAdpt* m_AdptObjPtr;
  327. bool m_VS2010BridgeMode;
  328. CSimpleStringA m_strAdapterName;
  329. };
  330. struct AdaptorInfo
  331. {
  332. public:
  333. CSimpleStringA strVendor;
  334. CSimpleStringA strVersion;
  335. CSimpleStringA strBatch;
  336. CSimpleStringA strPort;
  337. CSimpleStringA strBaudrate;
  338. CSimpleStringA strPortNum;
  339. CSimpleStringA strCheckdata;
  340. CSimpleStringA strKeysn;
  341. CSimpleStringA strPureAdapterName;
  342. AdaptorInfo() :strVendor(true), strVersion(true), strBatch(true)
  343. , strPort(true), strBaudrate(true)
  344. , strPortNum(true)
  345. , strCheckdata(true), strKeysn(true), strPureAdapterName(true)
  346. {
  347. }
  348. int GetVersionInt() const
  349. {
  350. int result = 0;
  351. if (!strVersion.IsNullOrEmpty()) {
  352. result = atoi(strVersion.GetData());
  353. }
  354. return result;
  355. }
  356. int GetBatchInt() const
  357. {
  358. int result = 0;
  359. if (!strBatch.IsNullOrEmpty()) {
  360. result = atoi(strBatch.GetData());
  361. }
  362. return result;
  363. }
  364. int GetPortInt() const
  365. {
  366. int result = 0;
  367. if (!strPort.IsNullOrEmpty()) {
  368. result = atoi(strPort.GetData());
  369. }
  370. return result;
  371. }
  372. int GetBaudrateInt() const
  373. {
  374. int result = 0;
  375. if (!strBaudrate.IsNullOrEmpty()) {
  376. result = atoi(strBaudrate.GetData());
  377. }
  378. return result;
  379. }
  380. int GetPortNumInt() const
  381. {
  382. int result = 0;
  383. if (!strPortNum.IsNullOrEmpty()) {
  384. result = atoi(strPortNum.GetData());
  385. }
  386. return result;
  387. }
  388. };
  389. struct AdapterInfo : public AdaptorInfo
  390. {
  391. CSimpleStringA adapterFilePath;
  392. CSimpleStringA adapterFileName;
  393. DevCategoryInfo devCatInfo;
  394. AdapterInfo() :adapterFilePath(true), adapterFileName(true)
  395. {
  396. ZeroMemory(devCatInfo.szModel, sizeof(devCatInfo.szModel));
  397. ZeroMemory(devCatInfo.szType, sizeof(devCatInfo.szType));
  398. ZeroMemory(devCatInfo.szVendor, sizeof(devCatInfo.szVendor));
  399. }
  400. void FulfillCategoryInfo(const DevCategoryInfo& rhs)
  401. {
  402. devCatInfo.eState = rhs.eState;
  403. devCatInfo.version.wMajor = rhs.version.wMajor;
  404. devCatInfo.version.wMinor = rhs.version.wMinor;
  405. devCatInfo.version.wRevision = rhs.version.wRevision;
  406. devCatInfo.version.wBuild = rhs.version.wBuild;
  407. strcpy(devCatInfo.szType, rhs.szType);
  408. strcpy(devCatInfo.szModel, rhs.szModel);
  409. strcpy(devCatInfo.szVendor, rhs.szVendor);
  410. CSimpleStringA strCatVersion = CSimpleStringA::Format("%d.%d.%d.%d", devCatInfo.version.wMajor, devCatInfo.version.wMinor, devCatInfo.version.wRevision, devCatInfo.version.wBuild);
  411. if (strlen(devCatInfo.szType) >= MAX_DEV_TYPE_LEN) {
  412. devCatInfo.szType[MAX_DEV_TYPE_LEN - 1] = '\0';
  413. }
  414. if (strlen(devCatInfo.szModel) >= MAX_DEV_MODEL_LEN) {
  415. devCatInfo.szModel[MAX_DEV_MODEL_LEN - 1] = '\0';
  416. }
  417. if (strlen(devCatInfo.szVendor) >= MAX_DEV_VENDOR_LEN) {
  418. devCatInfo.szVendor[MAX_DEV_VENDOR_LEN - 1] = '\0';
  419. }
  420. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("GetDevCategory")("{\"szVendor\":\"%s\",\"szType\":\"%s\",\"szModel\":\"%s\",\"szVersion\":\"%s\"}", devCatInfo.szVendor, devCatInfo.szType, devCatInfo.szModel, strCatVersion.GetData());
  421. //oilyang@20250220 as device adapter have been decoupled from us, no need to check anymore
  422. //CheckCategoryInfoFormat();
  423. }
  424. void CheckCategoryInfoFormat()
  425. {
  426. const int DEV_CATINFO_SUM = 3;
  427. char* devInfo[DEV_CATINFO_SUM] = { "szModel", "szVendor", "szType" };
  428. for (int devSn = 0; devSn < DEV_CATINFO_SUM; devSn++)
  429. {
  430. CSimpleStringA strCatInfo;
  431. if (devSn == 0)
  432. strCatInfo = devCatInfo.szModel;
  433. else if (devSn == 1)
  434. strCatInfo = devCatInfo.szVendor;
  435. else if (devSn == 2)
  436. strCatInfo = devCatInfo.szType;
  437. if (!strCatInfo.IsNullOrEmpty())
  438. {
  439. auto arr = strCatInfo.Split('#');
  440. if (arr.GetCount() > 0)
  441. {
  442. for (int sec = 0; sec < arr.GetCount(); sec++)
  443. {
  444. auto arr2 = arr[sec].Split('=');
  445. if (arr2.GetCount() != 2)
  446. {
  447. if (devSn != 1)//szVendor 没有要求符合"key=value#"的格式
  448. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("CheckDevInfoFormat, Wrong section of %s:%s", devInfo[devSn], arr[sec].GetData());
  449. continue;
  450. }
  451. if ((strnicmp((LPCTSTR)arr2[0], "MID", strlen("MID")) == 0 && arr2[1].Compare("1.0") != 0)
  452. || (strnicmp((LPCTSTR)arr2[0], "CODE", strlen("CODE")) == 0 && arr2[1].Compare("UCS2") != 0))
  453. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("CheckDevInfoFormat, Wrong format of %s:%s", arr2[0].GetData(), arr2[1].GetData());
  454. else if (strnicmp((LPCTSTR)arr2[0], "PID", strlen("PID")) == 0 || strnicmp((LPCTSTR)arr2[0], "FWID", strlen("FWID")) == 0)
  455. {
  456. string strData = arr2[1].GetData();
  457. regex pattern(".{8,16}");
  458. smatch sm;
  459. if (!regex_match(strData, sm, pattern))
  460. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("CheckDevInfoFormat, Wrong format of %s:%s", arr2[0].GetData(), arr2[1].GetData());
  461. }
  462. else if (strnicmp((LPCTSTR)arr2[0], "STYLE", strlen("STYLE")) == 0)
  463. {
  464. string strData = arr2[1].GetData();
  465. regex pattern("CL|IG");
  466. smatch sm;
  467. if (!regex_match(strData, sm, pattern))
  468. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("CheckDevInfoFormat, Wrong format of %s:%s", arr2[0].GetData(), arr2[1].GetData());
  469. }
  470. else if (strnicmp((LPCTSTR)arr2[0], "RF", strlen("RF")) == 0)
  471. {
  472. string strData = arr2[1].GetData();
  473. regex pattern("Y|N");
  474. smatch sm;
  475. if (!regex_match(strData, sm, pattern))
  476. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("CheckDevInfoFormat, Wrong format of %s:%s", arr2[0].GetData(), arr2[1].GetData());
  477. }
  478. else if (strnicmp((LPCTSTR)arr2[0], "FUNCTION", strlen("FUNCTION")) == 0)
  479. {
  480. string strData = arr2[1].GetData();
  481. //regex pattern("^(I|T|F|Y|IT|TI|IF|FI|TF|FT|IY|TY|FY|ITF|IFT|FIT|FTI|FIT|FTI)$");//stupid,how to write the right... composite of 'I'/'T'/'F'/'Y'
  482. regex pattern("^[ITFY]{1,4}$");
  483. smatch sm;
  484. if (!regex_match(strData, sm, pattern))
  485. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("CheckDevInfoFormat, Wrong format of %s:%s", arr2[0].GetData(), arr2[1].GetData());
  486. }
  487. }
  488. }
  489. else
  490. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("CheckDevInfoFormat, can't split from %s:%s", devInfo[devSn], strCatInfo.GetData());
  491. }
  492. else
  493. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("CheckDevInfoFormat, %s is empty.", devInfo[devSn]);
  494. }
  495. }
  496. };
  497. struct ErrorPackageEx {
  498. ErrorCodeEnum errCode;
  499. DWORD apiUserCode;
  500. CSimpleStringA devApi;
  501. bool bInBusiness;
  502. int costTime;
  503. CSimpleStringA funPath;
  504. ErrorPackageEx() :errCode(Error_Succeed), apiUserCode(0), devApi(true), bInBusiness(false), costTime(0), funPath(true) {}
  505. };
  506. struct DevEntityErrorCode {
  507. DWORD dwEntityId;
  508. DWORD dwVendorId;
  509. DWORD dwVendorErroCode;
  510. DWORD dwReserved;
  511. DevEntityErrorCode() :dwEntityId(0), dwVendorId(0), dwVendorErroCode(0), dwReserved(0) {}
  512. void ResetWithEntityID(WORD wEntityID)
  513. {
  514. dwEntityId = dwVendorId = dwVendorErroCode = dwReserved = 0;
  515. dwEntityId = wEntityID;
  516. }
  517. void SetDevCode(DWORD devErrorCode, DWORD dwReservedCode)
  518. {
  519. dwVendorErroCode = devErrorCode;
  520. dwReserved = dwReservedCode;
  521. }
  522. DWORD GetCompleteErrorCode() const
  523. {
  524. return MAKE_SLV_ERRORCODE(dwEntityId, dwVendorId, dwVendorErroCode);
  525. }
  526. };
  527. template<class TFSM, class TDevClass>
  528. class CCommDevFSM : public FSMImpl<TFSM>
  529. {
  530. public:
  531. CCommDevFSM() : m_iInWhatPage(PageType_Init),m_bOpened(false), m_bOpening(true), m_eDevState(DEVICE_STATUS_NOT_READY),m_contiErrTimes(0), m_preUIState('X')
  532. {
  533. }
  534. ErrorCodeEnum GetAndSplitDevErrInfo(
  535. CSimpleStringA& csErrMsg, WORD& wdDevErrCode, LPCTSTR lpszFuncNameForDisplay = "");
  536. DWORD SetErrorAndLog(ErrorCodeEnum errCode, DWORD userCode, CSimpleStringA devApi, CSimpleStringA funPath, bool bInBusiness = false, int costTime = 0, CSimpleStringA logCode = "", CSimpleStringA context = "")
  537. {
  538. //set error info
  539. m_errPkgEx.errCode = errCode;
  540. m_errPkgEx.apiUserCode = userCode;
  541. m_errPkgEx.devApi = devApi;
  542. m_errPkgEx.bInBusiness = bInBusiness;
  543. m_errPkgEx.costTime = costTime;
  544. m_errPkgEx.funPath = funPath;
  545. //get DevAdapter vendor errmsg and log info
  546. WORD wdErrCode = 0;
  547. CSimpleStringA csErrMsg(true);
  548. ErrorCodeEnum ec = GetAndSplitDevErrInfo(csErrMsg, wdErrCode, "");
  549. if (ec == Error_Succeed && wdErrCode != 0) {
  550. //oilyang@20200525 if have been set ErrorCode of entity defined (not device),use it directly
  551. if ((m_errPkgEx.apiUserCode >> 20) == m_entCode.dwEntityId)
  552. UpdateDEC(m_errPkgEx.apiUserCode);
  553. else
  554. UpdateDEC(wdErrCode);
  555. }
  556. else if (m_errPkgEx.apiUserCode != 0) {
  557. UpdateDEC(m_errPkgEx.apiUserCode);
  558. }
  559. //oilyang@20200525 if have been set ErrorCode of entity defined (not device),use it directly
  560. const DWORD dwCode = GetAlarmDEC();
  561. const CSimpleStringA alarmMsg = CSimpleStringA::Format("{\"Function\":\"%s\", \"DevApi\":\"%s\", \"ReturnCode\":\"%s\", \"Msg\":\"%s\", \"Context\":\"%s\"}"
  562. , funPath.GetData(), devApi.GetData(), SpStrError(m_errPkgEx.errCode), csErrMsg.GetData(), context.GetData());
  563. std::map<std::string, std::string> msgInfo;
  564. msgInfo["ReturnCode"] = SpStrError(errCode);
  565. msgInfo["ErrMsg"] = csErrMsg.GetData();
  566. msgInfo["Context"] = context.GetData();
  567. std::pair<bool, std::string> strResult;
  568. strResult = generateJsonStr(msgInfo);
  569. CSimpleStringA csErrMsgWithReturnCode = strResult.second.c_str();
  570. CSimpleStringA tmpRTA(true), tmpDesc(true);
  571. this->GetEntityBase()->GetFunction()->GetVTMErrMsg(dwCode, tmpDesc, tmpRTA);
  572. if (bInBusiness) {
  573. LogError(Severity_High, m_errPkgEx.errCode, dwCode, alarmMsg.GetData());
  574. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_USER).setLogCode(logCode).setAPI(devApi).setResultCode(tmpRTA.GetData()).setCostTime(costTime)(csErrMsgWithReturnCode.GetData());
  575. }
  576. else {
  577. LogWarn(Severity_High, m_errPkgEx.errCode, dwCode, alarmMsg.GetData());
  578. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER).setLogCode(logCode).setAPI(devApi).setResultCode(tmpRTA.GetData()).setCostTime(costTime)(csErrMsgWithReturnCode.GetData());
  579. }
  580. return dwCode;
  581. }
  582. //在旧dep路径下找到文件时告警
  583. void UpdateAndWarnFileFindInDepBak(CSimpleStringA& newDepPath, const char* filename, DWORD userCode)
  584. {
  585. CSimpleStringA strOldPath;
  586. if (!ExistsFileA(newDepPath.GetData()))
  587. {
  588. ErrorCodeEnum eErrOld = this->GetEntityBase()->GetFunction()->GetPath("DepBak", strOldPath);
  589. if (eErrOld != Error_Succeed)
  590. {
  591. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Get [DepBak] Path failed! errcode:%s.", SpStrError(eErrOld));
  592. }
  593. strOldPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", strOldPath.GetData(), filename);
  594. if (ExistsFileA(strOldPath.GetData()))
  595. {
  596. newDepPath = strOldPath;
  597. CSimpleStringA warnMsg = CSimpleStringA::Format("Find %s in OLD dep path!", strOldPath.GetData());
  598. LogWarn(Severity_Middle, Error_Unexpect, userCode, warnMsg.GetData());
  599. }
  600. }
  601. }
  602. ErrorCodeEnum LoadUpAdapterLibrary(const CSimpleStringA& customDllPath = "")
  603. {
  604. m_hDevHelper.SetAdapterName(this->GetEntityBase()->GetEntityName());
  605. ErrorCodeEnum rc = m_hDevHelper.LoadUp(customDllPath.IsNullOrEmpty() ? m_adapterInfo.adapterFilePath : customDllPath);
  606. return rc;
  607. }
  608. /*fulfill adapter elem, open param and adapter file name&absolute path */
  609. void FulfillAdapterInfoFrom(const VendorLibInfo& vendorLib)
  610. {
  611. m_adapterInfo.strVendor = vendorLib.strVendor;
  612. m_adapterInfo.strVersion = vendorLib.strVersion;
  613. m_adapterInfo.strBatch = vendorLib.strBatch;
  614. m_adapterInfo.strPureAdapterName = vendorLib.strFileValue;
  615. CSmartPointer<IConfigInfo> spConfigRoot;
  616. this->GetEntityBase()->GetFunction()->OpenConfig(Config_Root, spConfigRoot);
  617. CSimpleStringA entityName(this->GetEntityBase()->GetEntityName());
  618. if (entityName.Compare("CardIssuerStore", true) == 0 || entityName.Compare("CardIssuerStand", true) == 0) {
  619. entityName = "CardIssuer";
  620. }
  621. CSimpleStringA sectionName = CSimpleStringA::Format("Device.%s", entityName.GetData());
  622. CSimpleStringA csPort(true), csBaudrate(true);
  623. spConfigRoot->ReadConfigValue(sectionName.GetData(), "Port", csPort);
  624. spConfigRoot->ReadConfigValue(sectionName.GetData(), "Baudrate", csBaudrate);
  625. m_adapterInfo.strPort = csPort;
  626. m_adapterInfo.strBaudrate = csBaudrate;
  627. CSimpleStringA csPortNum(true), csCheckData(true), csKeySN(true), csInOutDir(true);
  628. if (!entityName.Compare("Gpio")) {
  629. spConfigRoot->ReadConfigValue(sectionName.GetData(), "PortNum", csPortNum);
  630. m_adapterInfo.strPortNum = csPortNum;
  631. } else if (!entityName.Compare("PinPad")) {
  632. spConfigRoot->ReadConfigValue(sectionName.GetData(), "Checkdata", csCheckData);
  633. spConfigRoot->ReadConfigValue(sectionName.GetData(), "Keysn", csKeySN);
  634. m_adapterInfo.strCheckdata = csCheckData;
  635. m_adapterInfo.strKeysn = csKeySN;
  636. }
  637. m_adapterInfo.adapterFileName = vendorLib.toLibNameString();
  638. CSimpleStringA strDepPath(true);
  639. this->GetEntityBase()->GetFunction()->GetPath("Dep", strDepPath);
  640. m_adapterInfo.adapterFilePath = CSimpleStringA::Format(
  641. "%s" SPLIT_SLASH_STR "%s", (LPCTSTR)strDepPath, (LPCTSTR)m_adapterInfo.adapterFileName);
  642. }
  643. ErrorCodeEnum FulfillAdapterDevCategory()
  644. {
  645. if (!!m_hDevHelper) {
  646. ErrorCodeEnum result(Error_Succeed);
  647. DevCategoryInfo devCat;
  648. ZeroMemory(devCat.szModel, sizeof(devCat.szModel));
  649. ZeroMemory(devCat.szType, sizeof(devCat.szType));
  650. ZeroMemory(devCat.szVendor, sizeof(devCat.szVendor));
  651. result = m_hDevHelper->GetDevCategory(devCat);
  652. if (IS_SUCCEED(result)) {
  653. m_adapterInfo.FulfillCategoryInfo(devCat);
  654. }
  655. return result;
  656. }
  657. return Error_InvalidState;
  658. }
  659. #if defined(_MSC_VER)
  660. const char* LPCSTRSmartPrintString(const CSimpleStringA& str)
  661. {
  662. return str.IsNullOrEmpty() ? "" : str.GetData();
  663. }
  664. CSimpleStringA GenerateJson(AdaptorInfo adaptorInfo, const CSimpleStringA& devDeviceName)
  665. {
  666. CSimpleStringA result(true);
  667. if (0 == devDeviceName.Compare("Gpio", true)) {
  668. result = CSimpleStringA::Format("{\"Vendor\":\"%s\", \"Version\":\"%s\", \"Batch\":\"%s\", \"Port\":\"%s\", \"Baudrate\":\"%s\", \"PortNum\":\"%s\"}",
  669. LPCSTRSmartPrintString(adaptorInfo.strVendor), LPCSTRSmartPrintString(adaptorInfo.strVersion),
  670. LPCSTRSmartPrintString(adaptorInfo.strBatch), LPCSTRSmartPrintString(adaptorInfo.strPort), LPCSTRSmartPrintString(adaptorInfo.strBaudrate),
  671. LPCSTRSmartPrintString(adaptorInfo.strPortNum));
  672. } else if (0 == devDeviceName.Compare("PinPad", true)) {
  673. result = CSimpleStringA::Format("{\"Vendor\":\"%s\", \"Version\":\"%s\", \"Batch\":\"%s\", \"Port\":\"%s\", \"Baudrate\":\"%s\", \"Checkdata\":\"%s\", \"Keysn\":\"%s\"}",
  674. LPCSTRSmartPrintString(adaptorInfo.strVendor), LPCSTRSmartPrintString(adaptorInfo.strVersion),
  675. LPCSTRSmartPrintString(adaptorInfo.strBatch), LPCSTRSmartPrintString(adaptorInfo.strPort), LPCSTRSmartPrintString(adaptorInfo.strBaudrate),
  676. LPCSTRSmartPrintString(adaptorInfo.strCheckdata), LPCSTRSmartPrintString(adaptorInfo.strKeysn));
  677. } else {
  678. result = CSimpleStringA::Format("{\"Vendor\":\"%s\", \"Version\":\"%s\", \"Batch\":\"%s\", \"Port\":\"%s\", \"Baudrate\":\"%s\"}",
  679. LPCSTRSmartPrintString(adaptorInfo.strVendor), LPCSTRSmartPrintString(adaptorInfo.strVersion),
  680. LPCSTRSmartPrintString(adaptorInfo.strBatch), LPCSTRSmartPrintString(adaptorInfo.strPort), LPCSTRSmartPrintString(adaptorInfo.strBaudrate));
  681. }
  682. return result;
  683. }
  684. #endif //_MSC_VER
  685. void ResetRepeatErrTimes()
  686. {
  687. m_contiErrTimes = 0;
  688. }
  689. /** TODO等待废弃,不建议再继续使用 [Gifur@2025114]*/
  690. ErrorCodeEnum TransECWithRepeat(const ErrorCodeEnum& ec)
  691. {
  692. ErrorCodeEnum result(ec);
  693. if (ec != Error_Succeed) {
  694. if (!m_bOpened) {
  695. result = Error_DevNotAvailable;
  696. } else if (0 < CountDevError(this->GetEntityBase())) {
  697. result = Error_DevFailAddUp;
  698. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER)("exceed max allow repeat error times! 0x%x -> 0x%x", ec, result);
  699. }
  700. }
  701. return result;
  702. }
  703. virtual ErrorCodeEnum PreOpenDevice() { return Error_Succeed; }
  704. virtual ErrorCodeEnum ToOpenDevice() { return Error_NotImpl; }
  705. virtual ErrorCodeEnum PostOpenDevice() { return Error_Succeed; }
  706. //暂时未使用
  707. ErrorCodeEnum OpenDevice()
  708. {
  709. if (m_bOpening) {
  710. return Error_Pending;
  711. }
  712. if (m_bOpened) {
  713. return Error_DevAlreadyConnected;
  714. }
  715. m_bOpening = true;
  716. ErrorCodeEnum result = PreOpenDevice();
  717. if (IS_SUCCEED(result)) {
  718. result = ToOpenDevice();
  719. if (IS_SUCCEED(result)) {
  720. m_bOpened = true;
  721. result = PostOpenDevice();
  722. }
  723. }
  724. m_bOpening = false;
  725. return result;
  726. }
  727. virtual void SetDevInitFlag(bool val = true) {
  728. m_bOpened = val;
  729. }
  730. bool GetDevInitFlag() const {
  731. return m_bOpened;
  732. }
  733. virtual void SetDevInitingFlag(bool val = true) {
  734. m_bOpening = val;
  735. }
  736. bool GetDevInitingFlag() const {
  737. return m_bOpening;
  738. }
  739. virtual void SetInWhatPage(int iPageType) {
  740. m_iInWhatPage = iPageType;
  741. }
  742. void OnUIState4SetWhatPage(const char* pcszValue) {
  743. if (pcszValue != NULL && strlen(pcszValue) > 0) {
  744. bool trigger(false);
  745. //增加页面位置判断
  746. if (_strnicmp(pcszValue, "M", strlen("M")) == 0) {
  747. SetInWhatPage(PageType_MainPage);
  748. }
  749. else if (_strnicmp(pcszValue, "U", strlen("U")) == 0) {
  750. SetInWhatPage(PageType_UserDesktop);
  751. }
  752. else {
  753. SetInWhatPage(PageType_Other);
  754. }
  755. if (m_preUIState != pcszValue[0]) {
  756. if (pcszValue[0] == 'M') {
  757. trigger = true;
  758. }
  759. m_preUIState = pcszValue[0];
  760. }
  761. if (trigger) {
  762. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("just test EVT_MAINPAGE_DISPLAY");
  763. this->PostEventFIFO(new FSMEvent(EVT_MAINPAGE_DISPLAY));
  764. }
  765. }
  766. }
  767. virtual DevStateEnum GetDevState() const {
  768. return m_eDevState;
  769. }
  770. void SetDevState(DevStateEnum val = DEVICE_STATUS_NORMAL) {
  771. m_eDevState = val;
  772. }
  773. public:
  774. bool m_bOpened;
  775. bool m_bOpening;
  776. protected:
  777. DevAdptLibHelper<TDevClass> m_hDevHelper;
  778. DevEntityErrorCode m_entCode;
  779. int m_iInWhatPage/*在哪个页面,区分首页,用户桌面,其他页*/;
  780. ErrorPackageEx m_errPkgEx;
  781. unsigned int m_contiErrTimes;
  782. AdapterInfo m_adapterInfo;
  783. DevStateEnum m_eDevState;
  784. void UpdateEntityIDIfZero()
  785. {
  786. if (m_entCode.dwEntityId == 0) {
  787. UpdateEntityID();
  788. }
  789. }
  790. void UpdateEntityID()
  791. {
  792. CEntityStaticInfo esi = { 0 };
  793. ErrorCodeEnum ec = this->GetEntityBase()->GetFunction()->GetEntityStaticInfo(this->GetEntityBase()->GetEntityName(), esi);
  794. if (ec == Error_Succeed) {
  795. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("wEntityDevelopID: 0x%X", esi.wEntityDevelopID);
  796. m_entCode.ResetWithEntityID(esi.wEntityDevelopID);
  797. }
  798. }
  799. DWORD GetEntityID()
  800. {
  801. UpdateEntityIDIfZero();
  802. return m_entCode.dwEntityId;
  803. }
  804. DWORD UpdateDEC(DWORD dwVal = 0)
  805. {
  806. UpdateEntityIDIfZero();
  807. /** 14 is for entity define, FC(6bit) is reserved*/
  808. int reserved = (dwVal & 0xFC000) >> 14;
  809. m_entCode.SetDevCode(dwVal, reserved);
  810. return GetDEC();
  811. }
  812. DWORD GetDEC() const
  813. {
  814. return m_entCode.GetCompleteErrorCode();
  815. }
  816. DWORD GetAlarmDEC(DWORD dwValue = 0)
  817. {
  818. if (dwValue > 0)
  819. UpdateDEC(dwValue);
  820. //if have been set ErrorCode of entity defined (not device),use it directly
  821. DWORD dwCode = 0;
  822. //if ((m_entCode.dwVendorErroCode >> 20) == m_entCode.dwEntityId)
  823. if(IsEntityIDSettle(m_entCode.dwVendorErroCode))
  824. dwCode = m_entCode.dwVendorErroCode;
  825. else
  826. dwCode = HARDWARE_ENTITY_MAKE_ERRORCODE_TO_BUSINESS(m_entCode);
  827. return dwCode;
  828. }
  829. void ClearRelatedDEC()
  830. {
  831. UpdateDEC();
  832. }
  833. bool IsEntityIDSettle(const DWORD& dwVal) const
  834. {
  835. return ((dwVal >> 20) == m_entCode.dwEntityId);
  836. }
  837. static void GetFileSizeAndCalcHashValue(const char* fileName, int& fileSize, BYTE fileHash[32])
  838. {
  839. HANDLE hFile;
  840. hFile = CreateFile(fileName, 0, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  841. if (hFile == INVALID_HANDLE_VALUE) {
  842. fileSize = 0;
  843. return;
  844. }
  845. fileSize = GetFileSize(hFile, NULL);
  846. CloseHandle(hFile);
  847. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to calc vendor dll hash");
  848. SM3File(const_cast<char*>(fileName), fileHash);
  849. return;
  850. }
  851. int CountDevError(CEntityBase *pEntityBase);
  852. virtual bool IsInBusiness() const {
  853. return (m_iInWhatPage == PageType_Other);
  854. }
  855. virtual void OnHardwareShakeHand(CSmartPointer<ITransactionContext> pTransactionContext);
  856. private:
  857. char m_preUIState;
  858. };
  859. template<class TFSM, class TDevClass>
  860. inline ErrorCodeEnum CCommDevFSM<TFSM, TDevClass>::GetAndSplitDevErrInfo(
  861. CSimpleStringA& csErrMsg, WORD& wdDevErrCode, LPCTSTR lpszFuncNameForDisplay)
  862. {
  863. csErrMsg = "";
  864. wdDevErrCode = 0;
  865. BOOL bDisplayFunName = TRUE;
  866. if (lpszFuncNameForDisplay == NULL || strlen(lpszFuncNameForDisplay) == 0) {
  867. bDisplayFunName = FALSE;
  868. }
  869. if (!m_hDevHelper) {
  870. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("inst occurs nullptr !!!");
  871. return Error_Param;
  872. }
  873. DevErrorInfo devErrInfo;
  874. ZeroMemory(&devErrInfo, sizeof(DevErrorInfo));
  875. ErrorCodeEnum erroCode = m_hDevHelper->GetLastErr(devErrInfo);
  876. if (erroCode == Error_Succeed) {
  877. if (strlen(devErrInfo.szErrMsg) > 0) {
  878. csErrMsg = devErrInfo.szErrMsg;
  879. }
  880. if (devErrInfo.dwErrMsgLen > MAX_DEV_ERROR_MSG_LEN) {
  881. wdDevErrCode = (WORD)((devErrInfo.dwErrMsgLen >> 16) & 0x0000FFFF);
  882. }
  883. } else {
  884. if (bDisplayFunName) {
  885. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI("DevAdapter::GetLastErr")("Invoke <%s> failed, and Dev_GLE failed returned EC: %s",
  886. lpszFuncNameForDisplay, SpStrError(erroCode));
  887. csErrMsg = CSimpleStringA::Format("Invoke <%s> failed", lpszFuncNameForDisplay);
  888. } else {
  889. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI("DevAdapter::GetLastErr")("Dev_GLE failed returned EC: %s", SpStrError(erroCode));
  890. }
  891. }
  892. /** reset vendor device error code to stop reporting to alarm [12/11/2020 Gifur] */
  893. wdDevErrCode = 0;
  894. return erroCode;
  895. }
  896. //1:报错累计到上限,需要返回错误码给业务
  897. //0:报错累计未到上限,或者不需要计数
  898. //-1:出现错误
  899. template<class TFSM, class TDevClass>
  900. int CCommDevFSM<TFSM, TDevClass>::CountDevError(CEntityBase* pEntityBase)
  901. {
  902. static int iErrNumLimit = -1;
  903. if (iErrNumLimit == -1) {
  904. CSmartPointer<IConfigInfo> spCenterConfig;
  905. ErrorCodeEnum eErrDev = pEntityBase->GetFunction()->OpenConfig(Config_CenterSetting, spCenterConfig);
  906. if (eErrDev != Error_Succeed) {
  907. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("open centersetting file failed!");
  908. return -1;
  909. } else {
  910. int value(0);
  911. spCenterConfig->ReadConfigValueInt("Common", "ErrUpperLimit", value);
  912. if (0 == value) {
  913. iErrNumLimit = 5;
  914. } else if (0 > value) {
  915. iErrNumLimit = 0;
  916. } else {
  917. iErrNumLimit = value;
  918. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("iErrNumLimit=%d", iErrNumLimit);
  919. }
  920. }
  921. }
  922. m_contiErrTimes++;
  923. if (iErrNumLimit > 0 && m_contiErrTimes >= iErrNumLimit) {
  924. return 1;
  925. } else {
  926. return 0;
  927. }
  928. }
  929. template<class TFSM, class TDevClass>
  930. void CCommDevFSM<TFSM, TDevClass>::OnHardwareShakeHand(CSmartPointer<ITransactionContext> pTransactionContext)
  931. {
  932. if (m_eDevState == DEVICE_STATUS_NORMAL)
  933. pTransactionContext->SendAnswer(Error_Succeed);
  934. else
  935. {
  936. if (m_bOpening)
  937. pTransactionContext->SendAnswer(Error_Succeed);
  938. else if (!m_bOpened)
  939. pTransactionContext->SendAnswer(Error_DevNotAvailable);
  940. else
  941. pTransactionContext->SendAnswer(Error_InvalidState);
  942. }
  943. }
  944. #endif /*_COMM_DEVICE_FSM_BASE_HPP_*/