DevFSMCommBase.hpp 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370
  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. /*class object must have 'LogErrMsg' function.*/
  41. #define LOG_ERROR_MSG_MACRO(errCode, MethodSig, DeviceType)\
  42. do {\
  43. if(errCode != Error_Succeed) {\
  44. LogErrMsg(CSimpleStringA::Format("%s::%s at line:%d", __FUNCTION__, #MethodSig, __LINE__), \
  45. errCode, MEC_DEVAPI_##DeviceType##_(MethodSig), TRUE);\
  46. }\
  47. } while (false)
  48. #define LOG_CARDISSUER_ERROR_MSG_MACRO(errCode, MethodSig)\
  49. LOG_ERROR_MSG_MACRO(errCode, MethodSig, CARDISSUER)
  50. #define LOG_THERMALPRINT_ERROR_MSG_MACRO(errCode, MethodSig)\
  51. LOG_ERROR_MSG_MACRO(errCode, MethodSig, THERMALPRINT)
  52. #define LOG_DEVCONTROL_ERROR_MSG_MACRO(errCode, MethodSig)\
  53. LOG_ERROR_MSG_MACRO(errCode, MethodSig, DEVCONTROL)
  54. #define LOG_PRINTSEAL_ERROR_MSG_MACRO(errCode, MethodSig)\
  55. LOG_ERROR_MSG_MACRO(errCode, MethodSig, PRINTSEAL)
  56. #define LOG_FINGERPRINT_ERROR_MSG_MACRO(errCode, MethodSig)\
  57. LOG_ERROR_MSG_MACRO(errCode, MethodSig, FINGERPRINT)
  58. #define LOG_HSPSCANNER_ERROR_MSG_MACRO(errCode, MethodSig)\
  59. LOG_ERROR_MSG_MACRO(errCode, MethodSig, HSPSCANNER)
  60. #define LOG_UPS_ERROR_MSG_MACRO(errCode, MethodSig)\
  61. LOG_ERROR_MSG_MACRO(errCode, MethodSig, UPS)
  62. #define LOG_GPIO_ERROR_MSG_MACRO(errCode, MethodSig)\
  63. LOG_ERROR_MSG_MACRO(errCode, MethodSig, GPIO)
  64. #define LOG_IDCER_ERROR_MSG_MACRO(errCode, MethodSig)\
  65. LOG_ERROR_MSG_MACRO(errCode, MethodSig, IDCER)
  66. #define LOG_RF_ERROR_MSG_MACRO(errCode, MethodSig)\
  67. LOG_ERROR_MSG_MACRO(errCode, MethodSig, RF)
  68. #define LOG_PINPAD_ERROR_MSG_MACRO(errCode, MethodSig)\
  69. LOG_ERROR_MSG_MACRO(errCode, MethodSig, EPP)
  70. #define LOG_WATCHDOG_ERROR_MSG_MACRO(errCode, MethodSig)\
  71. LOG_ERROR_MSG_MACRO(errCode, MethodSig, WATCHDOG)
  72. enum PageType
  73. {
  74. PageType_Init,
  75. PageType_MainPage,
  76. PageType_UserDesktop,
  77. PageType_Other,
  78. };
  79. template <typename TSubAdpt>
  80. struct DevAdptLibHelper
  81. {
  82. private:
  83. bool CheckAndGetMapAdatperName(const CSimpleStringA& strPath, CSimpleStringA& replaceAdapterName)
  84. {
  85. bool result = false;
  86. CSimpleStringA strTest = m_strAdapterName.IsNullOrEmpty() ? strPath.GetData() : m_strAdapterName.GetData();
  87. for (int i = 0; i < strTest.GetLength(); ++i) {
  88. if (strTest[i] >= 'A' && strTest[i] <= 'Z') strTest[i] = strTest[i] + ('a' - 'A');
  89. }
  90. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("strTest:%s", strPath.GetData());
  91. if (strTest.IndexOf("pinpad") != -1) {
  92. replaceAdapterName = "PinPad.self.1.1.dll";
  93. result = true;
  94. } else if (strTest.IndexOf("cardissuer") != -1) {
  95. replaceAdapterName = "CardIssuer.self.1.1.dll";
  96. result = true;
  97. } else if (strTest.IndexOf("contactlesscard") != -1) {
  98. replaceAdapterName = "ContactlessCard.self.1.1.dll";
  99. result = true;
  100. } else if (strTest.IndexOf("fingerprint") != -1) {
  101. replaceAdapterName = "FingerPrint.self.1.1.dll";
  102. result = true;
  103. } else if (strTest.IndexOf("gpio") != -1) {
  104. replaceAdapterName = "GPIO.self.1.1.dll";
  105. result = true;
  106. } else if (strTest.IndexOf("idcertificate") != -1) {
  107. replaceAdapterName = "IDCertificate.self.1.1.dll";
  108. result = true;
  109. } else if (strTest.IndexOf("hspscanner") != -1) {
  110. replaceAdapterName = "HSPSCanner.self.1.1.dll";
  111. result = true;
  112. }
  113. return result;
  114. }
  115. bool PreLoadLib(CSimpleStringA& strFullLibPath)
  116. {
  117. bool result(false);
  118. ///*TODO: Test (80374374@1/23/2024)*/
  119. DWORD d1;
  120. DWORD d2;
  121. DWORD d3;
  122. DWORD d4;
  123. if (!ExistsFileA(strFullLibPath)) {
  124. return result;
  125. }
  126. #ifndef DEVOPS_ON_PRD
  127. GetDllVersionInfo(strFullLibPath, d1, d2, d3, d4);
  128. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("dll %s version: %d.%d.%d.%d", strFullLibPath.GetData(), d1, d2, d3, d4);
  129. //VC16.0 [1920,1929] VS2019 142
  130. if (d4 >= 1920 && d4 <= 1929 && d3 == 142) {
  131. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("detect it built with VS2019, skip latter procedure, version: %d.%d.%d.%d", d1, d2, d3, d4);
  132. return result;
  133. }
  134. #endif
  135. CSimpleStringA strTest = strFullLibPath.GetData();
  136. CSimpleStringA strSelf(true);
  137. if (CheckAndGetMapAdatperName(strTest, strSelf)) {
  138. int index = -1;
  139. for (int i = 0; i < strTest.GetLength(); ++i)
  140. if (strTest[i] == '\\' || strTest[i] == '/') index = i;
  141. if (index != -1) {
  142. toolkit_setenv("INVOKE_VENDOR_ADAPTER_NAME", strFullLibPath.GetData());
  143. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("env:%s", strFullLibPath.GetData());
  144. CSimpleStringA strAdapterName = strTest.SubString(index + 1);
  145. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("adapter name:%s", strAdapterName.GetData());
  146. strTest[index + 1] = '\0'; //!!!!
  147. strFullLibPath = strTest.SubString(0, index + 1);
  148. strFullLibPath += strSelf;
  149. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("new adapter path:%s", strFullLibPath.GetData());
  150. result = true;
  151. }
  152. }
  153. return result;
  154. }
  155. public:
  156. void SetAdapterName(const CSimpleStringA& strEntityName) { m_strAdapterName = strEntityName; }
  157. ErrorCodeEnum LoadLibAddress(const CSimpleStringA& strFullLibPath)
  158. {
  159. ErrorCodeEnum erroCode = Error_Succeed;
  160. CSimpleStringA strTemp = strFullLibPath.GetData();
  161. #if defined(_MSC_VER)
  162. m_VS2010BridgeMode = PreLoadLib(strTemp);
  163. #endif //_MSC_VER
  164. do {
  165. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to load lib: %s", strTemp.GetData());
  166. if (!ExistsFileA(strTemp)) {
  167. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("the lib file %s is not existed!", strTemp.GetData());
  168. erroCode = Error_NotExist;
  169. break;
  170. }
  171. int res = toolkit_dlopen(strTemp, &m_lib);
  172. if (res != 0) {
  173. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("toolkit_dlopen[%s] failed with error %s.", strTemp.GetData(), toolkit_dlerror(&m_lib));
  174. erroCode = Error_DevLoadFileFailed;
  175. break;
  176. }
  177. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to get CreateDevComponent pointer address...");
  178. if ((res = toolkit_dlsym(&m_lib, "CreateDevComponent", (void**)&pFuncCreateAdapt)) != 0)
  179. {
  180. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("Get 'CreateDevComponent' Func address failed with error: %s", toolkit_dlerror(&m_lib));
  181. erroCode = Error_DevLoadFileFailed;
  182. break;
  183. }
  184. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to get ReleaseDevComponent pointer address...");
  185. if ((res = toolkit_dlsym(&m_lib, "ReleaseDevComponent", (void**)&pFuncReleaseAdapt)) != 0)
  186. {
  187. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("Get 'ReleaseDevComponent' Func address failed with error: %s", toolkit_dlerror(&m_lib));
  188. erroCode = Error_DevLoadFileFailed;
  189. break;
  190. }
  191. #if defined(RVC_OS_WIN)
  192. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to get GetMoreErrorDetail pointer address...");
  193. if ((res = toolkit_dlsym(&m_lib, "GetMoreErrorDetail", (void**)&pFuncGetPrefixErr)) != 0)
  194. {
  195. pFuncGetPrefixErr = nullptr;
  196. }
  197. else {
  198. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Get 'GetMoreErrorDetail' Func address succ");
  199. }
  200. #endif //RVC_OS_WIN
  201. } while (false);
  202. if (IS_FAILURED(erroCode))
  203. {
  204. TearDown();
  205. }
  206. return erroCode;
  207. }
  208. ErrorCodeEnum CreateDevAdptObject()
  209. {
  210. if (m_AdptObjPtr != nullptr) {
  211. return Error_AlreadyExist;
  212. }
  213. if (pFuncCreateAdapt == nullptr || pFuncReleaseAdapt == nullptr) {
  214. return Error_NotInit;
  215. }
  216. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to get Adapter object...");
  217. ErrorCodeEnum erroCode = pFuncCreateAdapt((DeviceBaseClass*&)m_AdptObjPtr);
  218. if (IS_FAILURED(erroCode)) {
  219. if (pFuncGetPrefixErr != nullptr) {
  220. char szErrMsg[MAX_DEV_ERROR_MSG_LEN];
  221. memset(szErrMsg, '\0', sizeof(szErrMsg));
  222. DWORD dwLen = MAX_DEV_ERROR_MSG_LEN;
  223. pFuncGetPrefixErr(szErrMsg, &dwLen);
  224. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("Create device adapter object failed! EC=%s, Msg:[%s]"
  225. , SpStrError(erroCode), szErrMsg);
  226. }
  227. else {
  228. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("Create device adapter object failed! EC=%s", SpStrError(erroCode));
  229. }
  230. return erroCode;
  231. }
  232. LOG_ASSERT(m_AdptObjPtr != nullptr);
  233. return Error_Succeed;
  234. }
  235. /** Integrate function LoadLibAddress and CreateDevAdptObject, please use LoadUpAdapterLibrary replace with it*/
  236. ErrorCodeEnum LoadUp(const CSimpleStringA& strFullLibPath)
  237. {
  238. ErrorCodeEnum erroCode = LoadLibAddress(strFullLibPath);
  239. if (IS_SUCCEED(erroCode)) {
  240. erroCode = CreateDevAdptObject();
  241. if (!IS_SUCCEED(erroCode)) {
  242. TearDown();
  243. }
  244. }
  245. return erroCode;
  246. }
  247. void TearDown()
  248. {
  249. LOG_FUNCTION();
  250. if (m_AdptObjPtr != nullptr)
  251. {
  252. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("To DevClose...");
  253. ErrorCodeEnum result = m_AdptObjPtr->DevClose();
  254. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("DevClose returned %s", SpStrError(result));
  255. if (nullptr != pFuncReleaseAdapt)
  256. {
  257. DeviceBaseClass* devBasePtr = static_cast<DeviceBaseClass*>(m_AdptObjPtr);
  258. if (devBasePtr != nullptr) {
  259. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("To Release DevAdapter Object...");
  260. pFuncReleaseAdapt(devBasePtr);
  261. m_AdptObjPtr = nullptr;
  262. } else {
  263. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("static cast base dev class failed!");
  264. }
  265. }
  266. }
  267. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to dlclose DevAdapter libs...");
  268. toolkit_dlclose(&m_lib);
  269. pFuncReleaseAdapt = pFuncCreateAdapt = nullptr;
  270. pFuncGetPrefixErr = nullptr;
  271. }
  272. #if defined(RVC_OS_WIN)
  273. ErrorCodeEnum GetDllVersionInfo(LPCTSTR lpcszFilePath, DWORD& dwMajor, DWORD& dwMinor, DWORD& dwBuild, DWORD& dwRevision)
  274. {
  275. char* buf = NULL;
  276. dwMajor = dwMinor = dwBuild = dwRevision = (DWORD)(-1);
  277. DWORD dwHnd;
  278. DWORD dwVerInfoSize;
  279. if (0 >= (dwVerInfoSize = GetFileVersionInfoSizeA(lpcszFilePath, &dwHnd))) {
  280. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("GetFileVersionInfoSizeA failed: %u", GetLastError());
  281. return Error_Unexpect;
  282. }
  283. buf = new char[dwVerInfoSize];
  284. if (NULL == buf) {
  285. return Error_Resource;
  286. }
  287. // get file version info
  288. if (!GetFileVersionInfo(lpcszFilePath, dwHnd, dwVerInfoSize, buf)) {
  289. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("GetFileVersionInfo failed: %u", GetLastError());
  290. delete[] buf;
  291. return Error_Unexpect;
  292. }
  293. // get FileVersion string from resource
  294. VS_FIXEDFILEINFO* ptrVersion;
  295. unsigned int versionLen = 0;
  296. if (!VerQueryValue(buf, "\\", (void**)&ptrVersion, &versionLen)) {
  297. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("VS_FIXEDFILEINFO failed: %u", GetLastError());
  298. delete[] buf;
  299. return Error_Unexpect;
  300. }
  301. dwMajor = (ptrVersion->dwFileVersionMS >> 16) & 0x0000FFFF;
  302. dwMinor = ptrVersion->dwFileVersionMS & 0x0000FFFF;
  303. dwBuild = (ptrVersion->dwFileVersionLS >> 16) & 0x0000FFFF;
  304. dwRevision = ptrVersion->dwFileVersionLS & 0x0000FFFF;
  305. delete[] buf;
  306. return Error_Succeed;
  307. }
  308. #else
  309. ErrorCodeEnum GetDllVersionInfo(LPCTSTR lpcszFilePath, DWORD& dwMajor, DWORD& dwMinor, DWORD& dwBuild, DWORD& dwRevision)
  310. {
  311. dwMajor = dwMinor = dwBuild = dwRevision = 0;
  312. return Error_NotSupport;
  313. }
  314. #endif //RVC_OS_WIN
  315. #if defined(_MSC_VER)
  316. operator bool() const { return (m_AdptObjPtr != nullptr); }
  317. #else
  318. /** Linux 下不加此会影响 编译 Gifur@2023329]*/
  319. explicit operator bool() const { return (m_AdptObjPtr != nullptr); }
  320. #endif //_MSC_VER
  321. operator DeviceBaseClass* () { return m_AdptObjPtr; }
  322. TSubAdpt* GetDevPointer() { return m_AdptObjPtr; }
  323. TSubAdpt* operator->()
  324. {
  325. if (m_AdptObjPtr == nullptr) {
  326. throw std::runtime_error("the dev class pointer is nullptr!!");
  327. }
  328. return m_AdptObjPtr;
  329. }
  330. bool operator !=(TSubAdpt* rhs)
  331. {
  332. return !(m_AdptObjPtr == rhs);
  333. }
  334. bool operator !=(const DevAdptLibHelper& rhs)
  335. {
  336. return !(m_AdptObjPtr == rhs.operator->());
  337. }
  338. DevAdptLibHelper()
  339. : pFuncCreateAdapt(nullptr), pFuncReleaseAdapt(nullptr), pFuncGetPrefixErr(nullptr)
  340. , m_AdptObjPtr(nullptr), m_VS2010BridgeMode(false), m_strAdapterName(true)
  341. {
  342. memset(&m_lib, 0, sizeof(toolkit_lib_t));
  343. }
  344. ~DevAdptLibHelper()
  345. {
  346. TearDown();
  347. }
  348. #if defined(RVC_OS_LINUX)
  349. DevAdptLibHelper(const DevAdptLibHelper&) = delete;
  350. DevAdptLibHelper& operator=(const DevAdptLibHelper&) = delete;
  351. #endif //RVC_OS_LINUX
  352. private:
  353. toolkit_lib_t m_lib;
  354. DevAdaptObjCreateFunc pFuncCreateAdapt;
  355. DevAdaptObjReleaseFunc pFuncReleaseAdapt;
  356. GetDevAdapterVersionFunc pFuncGetDevAdapterVer;
  357. GetPrefixErrMsgFunc pFuncGetPrefixErr;
  358. TSubAdpt* m_AdptObjPtr;
  359. bool m_VS2010BridgeMode;
  360. CSimpleStringA m_strAdapterName;
  361. };
  362. struct AdaptorInfo
  363. {
  364. public:
  365. CSimpleStringA strVendor;
  366. CSimpleStringA strVersion;
  367. CSimpleStringA strBatch;
  368. CSimpleStringA strPort;
  369. CSimpleStringA strBaudrate;
  370. CSimpleStringA strPortNum;
  371. CSimpleStringA strInOutDir;
  372. CSimpleStringA strCheckdata;
  373. CSimpleStringA strKeysn;
  374. CSimpleStringA strPureAdapterName;
  375. AdaptorInfo() :strVendor(true), strVersion(true), strBatch(true)
  376. , strPort(true), strBaudrate(true)
  377. , strPortNum(true), strInOutDir(true)
  378. , strCheckdata(true), strKeysn(true), strPureAdapterName(true)
  379. {
  380. }
  381. int GetVersionInt() const
  382. {
  383. int result = 0;
  384. if (!strVersion.IsNullOrEmpty()) {
  385. result = atoi(strVersion.GetData());
  386. }
  387. return result;
  388. }
  389. int GetBatchInt() const
  390. {
  391. int result = 0;
  392. if (!strBatch.IsNullOrEmpty()) {
  393. result = atoi(strBatch.GetData());
  394. }
  395. return result;
  396. }
  397. int GetPortInt() const
  398. {
  399. int result = 0;
  400. if (!strPort.IsNullOrEmpty()) {
  401. result = atoi(strPort.GetData());
  402. }
  403. return result;
  404. }
  405. int GetBaudrateInt() const
  406. {
  407. int result = 0;
  408. if (!strBaudrate.IsNullOrEmpty()) {
  409. result = atoi(strBaudrate.GetData());
  410. }
  411. return result;
  412. }
  413. int GetPortNumInt() const
  414. {
  415. int result = 0;
  416. if (!strPortNum.IsNullOrEmpty()) {
  417. result = atoi(strPortNum.GetData());
  418. }
  419. return result;
  420. }
  421. int GetInOutDirInt() const
  422. {
  423. int result = 0;
  424. if (!strInOutDir.IsNullOrEmpty()) {
  425. result = atoi(strInOutDir.GetData());
  426. }
  427. return result;
  428. }
  429. };
  430. struct AdapterInfo : public AdaptorInfo
  431. {
  432. CSimpleStringA adapterFilePath;
  433. CSimpleStringA adapterFileName;
  434. DevCategoryInfo devCatInfo;
  435. AdapterInfo() :adapterFilePath(true), adapterFileName(true)
  436. {
  437. ZeroMemory(devCatInfo.szModel, sizeof(devCatInfo.szModel));
  438. ZeroMemory(devCatInfo.szType, sizeof(devCatInfo.szType));
  439. ZeroMemory(devCatInfo.szVendor, sizeof(devCatInfo.szVendor));
  440. }
  441. void FulfillCategoryInfo(const DevCategoryInfo& rhs)
  442. {
  443. devCatInfo.eState = rhs.eState;
  444. devCatInfo.version.wMajor = rhs.version.wMajor;
  445. devCatInfo.version.wMinor = rhs.version.wMinor;
  446. devCatInfo.version.wRevision = rhs.version.wRevision;
  447. devCatInfo.version.wBuild = rhs.version.wBuild;
  448. strcpy(devCatInfo.szType, rhs.szType);
  449. strcpy(devCatInfo.szModel, rhs.szModel);
  450. strcpy(devCatInfo.szVendor, rhs.szVendor);
  451. CSimpleStringA strCatVersion = CSimpleStringA::Format("%d.%d.%d.%d", devCatInfo.version.wMajor, devCatInfo.version.wMinor, devCatInfo.version.wRevision, devCatInfo.version.wBuild);
  452. if (strlen(devCatInfo.szType) >= MAX_DEV_TYPE_LEN) {
  453. devCatInfo.szType[MAX_DEV_TYPE_LEN - 1] = '\0';
  454. }
  455. if (strlen(devCatInfo.szModel) >= MAX_DEV_MODEL_LEN) {
  456. devCatInfo.szModel[MAX_DEV_MODEL_LEN - 1] = '\0';
  457. }
  458. if (strlen(devCatInfo.szVendor) >= MAX_DEV_VENDOR_LEN) {
  459. devCatInfo.szVendor[MAX_DEV_VENDOR_LEN - 1] = '\0';
  460. }
  461. 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());
  462. CheckCategoryInfoFormat();
  463. }
  464. void CheckCategoryInfoFormat()
  465. {
  466. const int DEV_CATINFO_SUM = 3;
  467. char* devInfo[DEV_CATINFO_SUM] = { "szModel", "szVendor", "szType" };
  468. for (int devSn = 0; devSn < DEV_CATINFO_SUM; devSn++)
  469. {
  470. CSimpleStringA strCatInfo;
  471. if (devSn == 0)
  472. strCatInfo = devCatInfo.szModel;
  473. else if (devSn == 1)
  474. strCatInfo = devCatInfo.szVendor;
  475. else if (devSn == 2)
  476. strCatInfo = devCatInfo.szType;
  477. if (!strCatInfo.IsNullOrEmpty())
  478. {
  479. auto arr = strCatInfo.Split('#');
  480. if (arr.GetCount() > 0)
  481. {
  482. for (int sec = 0; sec < arr.GetCount(); sec++)
  483. {
  484. auto arr2 = arr[sec].Split('=');
  485. if (arr2.GetCount() != 2)
  486. {
  487. if (devSn != 1)//szVendor 没有要求符合"key=value#"的格式
  488. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("CheckDevInfoFormat, Wrong section of %s:%s", devInfo[devSn], arr[sec].GetData());
  489. continue;
  490. }
  491. if ((strnicmp((LPCTSTR)arr2[0], "MID", strlen("MID")) == 0 && arr2[1].Compare("1.0") != 0)
  492. || (strnicmp((LPCTSTR)arr2[0], "CODE", strlen("CODE")) == 0 && arr2[1].Compare("UCS2") != 0))
  493. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("CheckDevInfoFormat, Wrong format of %s:%s", arr2[0].GetData(), arr2[1].GetData());
  494. else if (strnicmp((LPCTSTR)arr2[0], "PID", strlen("PID")) == 0 || strnicmp((LPCTSTR)arr2[0], "FWID", strlen("FWID")) == 0)
  495. {
  496. string strData = arr2[1].GetData();
  497. regex pattern(".{8,16}");
  498. smatch sm;
  499. if (!regex_match(strData, sm, pattern))
  500. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("CheckDevInfoFormat, Wrong format of %s:%s", arr2[0].GetData(), arr2[1].GetData());
  501. }
  502. else if (strnicmp((LPCTSTR)arr2[0], "STYLE", strlen("STYLE")) == 0)
  503. {
  504. string strData = arr2[1].GetData();
  505. regex pattern("CL|IG");
  506. smatch sm;
  507. if (!regex_match(strData, sm, pattern))
  508. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("CheckDevInfoFormat, Wrong format of %s:%s", arr2[0].GetData(), arr2[1].GetData());
  509. }
  510. else if (strnicmp((LPCTSTR)arr2[0], "RF", strlen("RF")) == 0)
  511. {
  512. string strData = arr2[1].GetData();
  513. regex pattern("Y|N");
  514. smatch sm;
  515. if (!regex_match(strData, sm, pattern))
  516. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("CheckDevInfoFormat, Wrong format of %s:%s", arr2[0].GetData(), arr2[1].GetData());
  517. }
  518. else if (strnicmp((LPCTSTR)arr2[0], "FUNCTION", strlen("FUNCTION")) == 0)
  519. {
  520. string strData = arr2[1].GetData();
  521. //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'
  522. regex pattern("^[ITFY]{1,4}$");
  523. smatch sm;
  524. if (!regex_match(strData, sm, pattern))
  525. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("CheckDevInfoFormat, Wrong format of %s:%s", arr2[0].GetData(), arr2[1].GetData());
  526. }
  527. }
  528. }
  529. else
  530. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("CheckDevInfoFormat, can't split from %s:%s", devInfo[devSn], strCatInfo.GetData());
  531. }
  532. else
  533. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("CheckDevInfoFormat, %s is empty.", devInfo[devSn]);
  534. }
  535. }
  536. };
  537. struct ErrorPackage {
  538. CSimpleStringA msgHead;
  539. ///*TODO(80374374@3/31/2023): to apply, replace the one defined at entity scope with it */
  540. CSimpleStringA devSN;
  541. ErrorCodeEnum devErrCode;
  542. DWORD apiErrCode;
  543. ErrorPackage() :msgHead(true), devSN(true), devErrCode(Error_Succeed), apiErrCode(0) {}
  544. };
  545. struct ErrorPackageEx {//or another name? or replace ErrorPackage ?
  546. ErrorCodeEnum errCode;
  547. DWORD apiUserCode;
  548. CSimpleStringA devApi;
  549. bool bInBusiness;
  550. int costTime;
  551. CSimpleStringA funPath;
  552. ErrorPackageEx() :errCode(Error_Succeed), apiUserCode(0), devApi(true), bInBusiness(false), costTime(0), funPath(true) {}
  553. };
  554. struct DevEntityErrorCode {
  555. DWORD dwEntityId;
  556. DWORD dwVendorId;
  557. DWORD dwVendorErroCode;
  558. DWORD dwReserved;
  559. DevEntityErrorCode() :dwEntityId(0), dwVendorId(0), dwVendorErroCode(0), dwReserved(0) {}
  560. void ResetWithEntityID(WORD wEntityID)
  561. {
  562. dwEntityId = dwVendorId = dwVendorErroCode = dwReserved = 0;
  563. dwEntityId = wEntityID;
  564. }
  565. void SetDevCode(DWORD devErrorCode, DWORD dwReservedCode)
  566. {
  567. dwVendorErroCode = devErrorCode;
  568. dwReserved = dwReservedCode;
  569. }
  570. /*no used now!!*/
  571. void SetVendorID(DWORD dwVendorID)
  572. {
  573. dwVendorId = dwVendorID;
  574. }
  575. DWORD GetCompleteErrorCode() const
  576. {
  577. return MAKE_SLV_ERRORCODE(dwEntityId, dwVendorId, dwVendorErroCode);
  578. }
  579. DWORD GetCompleteErrorCode2Business() const
  580. {
  581. return MAKE_SLV_ERRORCODE_TO_BUSINESS(dwEntityId, dwVendorErroCode, dwReserved);
  582. }
  583. };
  584. template<class TFSM, class TDevClass>
  585. class CCommDevFSM : public FSMImpl<TFSM>
  586. {
  587. public:
  588. CCommDevFSM() :m_FirstStart(TRUE), m_iInWhatPage(PageType_Init),m_bOpened(false), m_bOpening(true), m_csAlarmMsg("No more information"), m_contiErrTimes(0)
  589. {
  590. m_WarnCodeMap.insert(std::make_pair<std::string, DWORD>("CardIssuerStand", 0x2030021f));
  591. m_WarnCodeMap.insert(std::make_pair<std::string, DWORD>("CardIssuerStore", 0x2200021f));
  592. m_WarnCodeMap.insert(std::make_pair<std::string, DWORD>("ContactlessCard", 0x21300203));
  593. m_WarnCodeMap.insert(std::make_pair<std::string, DWORD>("FingerPrint", 0x20400211));
  594. m_WarnCodeMap.insert(std::make_pair<std::string, DWORD>("IDCertificate", 0x20100202));
  595. m_WarnCodeMap.insert(std::make_pair<std::string, DWORD>("PinPad", 0x20600207));
  596. m_WarnCodeMap.insert(std::make_pair<std::string, DWORD>("Gpio", 0x20900201));
  597. m_WarnCodeMap.insert(std::make_pair<std::string, DWORD>("HSPScanner", 0x21700207));
  598. m_RootIniCodeMap.insert(std::make_pair<std::string, DWORD>("CardIssuerStand", 0x20300299));
  599. m_RootIniCodeMap.insert(std::make_pair<std::string, DWORD>("CardIssuerStore", 0x22000299));
  600. m_RootIniCodeMap.insert(std::make_pair<std::string, DWORD>("ContactlessCard", 0x21300224));
  601. m_RootIniCodeMap.insert(std::make_pair<std::string, DWORD>("FingerPrint", 0x20400229));
  602. m_RootIniCodeMap.insert(std::make_pair<std::string, DWORD>("IDCertificate", 0x2010021f));
  603. m_RootIniCodeMap.insert(std::make_pair<std::string, DWORD>("PinPad", 0x2060021f));
  604. m_RootIniCodeMap.insert(std::make_pair<std::string, DWORD>("Gpio", 0x20900203));
  605. m_RootIniCodeMap.insert(std::make_pair<std::string, DWORD>("HSPScanner", 0x2170020f));
  606. }
  607. ErrorCodeEnum GetAndSplitDevErrInfo(
  608. CSimpleStringA& csErrMsg, WORD& wdDevErrCode, LPCTSTR lpszFuncNameForDisplay = "");
  609. ErrorCodeEnum LogDevErrInfo(LPCTSTR lpszFuncName = "")
  610. {
  611. CSimpleStringA csMsg;
  612. WORD wdErrorCode = 0;
  613. return GetAndSplitDevErrInfo(csMsg, wdErrorCode, lpszFuncName);
  614. }
  615. bool GetDevErrorCode(WORD& wdErrorCode)
  616. {
  617. CSimpleStringA csMsg;
  618. ErrorCodeEnum eRet = GetAndSplitDevErrInfo(csMsg, wdErrorCode);
  619. return (eRet == Error_Succeed);
  620. }
  621. void SetErrPackage(CSimpleStringA msgHead, CSimpleStringA devSN, ErrorCodeEnum errCode, DWORD apiCode)
  622. {
  623. m_errPkg.msgHead = msgHead;
  624. m_errPkg.devSN = devSN;
  625. m_errPkg.devErrCode = errCode;
  626. m_errPkg.apiErrCode = apiCode;
  627. }
  628. DWORD AlarmDEC(bool bToBusiness = false, bool alarmOrNot = true) {
  629. WORD wdErrCode = 0;
  630. CSimpleStringA csErrMsg(true);
  631. ErrorCodeEnum ec = GetAndSplitDevErrInfo(csErrMsg, wdErrCode, (LPCTSTR)m_errPkg.msgHead);
  632. if (ec == Error_Succeed && wdErrCode != 0) {
  633. //oilyang@20200525 if have been set ErrorCode of entity defined (not device),use it directly
  634. if ((m_errPkg.apiErrCode >> 20) == m_entCode.dwEntityId)
  635. UpdateDEC(m_errPkg.apiErrCode);
  636. else
  637. UpdateDEC(wdErrCode);
  638. } else if (m_errPkg.apiErrCode != 0) {
  639. UpdateDEC(m_errPkg.apiErrCode);
  640. }
  641. //oilyang@20200525 if have been set ErrorCode of entity defined (not device),use it directly
  642. DWORD dwCode = GetAlarmDEC();
  643. if (alarmOrNot) {
  644. LogErrMsgEx(m_errPkg, csErrMsg, dwCode, bToBusiness);
  645. } else {
  646. DbgWithLink(bToBusiness ? LOG_LEVEL_ERROR : LOG_LEVEL_WARN, bToBusiness ? LOG_TYPE_USER : LOG_TYPE_SYSTEM).setAPI(m_errPkg.msgHead)("{\"DevSN\":\"%s\", \"Code\":\"%s\", \"Msg\":\"%s\"}"
  647. , m_errPkg.devSN.GetData(), SpStrError(m_errPkg.devErrCode), csErrMsg.GetData());
  648. }
  649. return dwCode;
  650. }
  651. void SetErrorAndLog(ErrorCodeEnum errCode, DWORD userCode, CSimpleStringA devApi, CSimpleStringA funPath, bool bInBusiness = false, int costTime = 0, CSimpleStringA logCode = "", CSimpleStringA context = "")
  652. {
  653. //set error info
  654. m_errPkgEx.errCode = errCode;
  655. m_errPkgEx.apiUserCode = userCode;
  656. m_errPkgEx.devApi = devApi;
  657. m_errPkgEx.bInBusiness = bInBusiness;
  658. m_errPkgEx.costTime = costTime;
  659. m_errPkgEx.funPath = funPath;
  660. //get DevAdapter vendor errmsg and log info
  661. WORD wdErrCode = 0;
  662. CSimpleStringA csErrMsg(true);
  663. ErrorCodeEnum ec = GetAndSplitDevErrInfo(csErrMsg, wdErrCode, "");
  664. if (ec == Error_Succeed && wdErrCode != 0) {
  665. //oilyang@20200525 if have been set ErrorCode of entity defined (not device),use it directly
  666. if ((m_errPkgEx.apiUserCode >> 20) == m_entCode.dwEntityId)
  667. UpdateDEC(m_errPkgEx.apiUserCode);
  668. else
  669. UpdateDEC(wdErrCode);
  670. }
  671. else if (m_errPkgEx.apiUserCode != 0) {
  672. UpdateDEC(m_errPkgEx.apiUserCode);
  673. }
  674. //oilyang@20200525 if have been set ErrorCode of entity defined (not device),use it directly
  675. const DWORD dwCode = GetAlarmDEC();
  676. const CSimpleStringA alarmMsg = CSimpleStringA::Format("{\"Function\":\"%s\", \"DevApi\":\"%s\", \"ReturnCode\":\"%s\", \"Msg\":%s, \"Context\":%s}"
  677. , funPath.GetData(), devApi.GetData(), SpStrError(m_errPkgEx.errCode), csErrMsg.GetData(), context.GetData());
  678. std::map<std::string, std::string> msgInfo;
  679. msgInfo["ReturnCode"] = SpStrError(errCode);
  680. msgInfo["ErrMsg"] = csErrMsg.GetData();
  681. msgInfo["Context"] = context.GetData();
  682. std::pair<bool, std::string> strResult;
  683. strResult = generateJsonStr(msgInfo);
  684. CSimpleStringA csErrMsgWithReturnCode = strResult.second.c_str();
  685. CSimpleStringA tmpRTA(true), tmpDesc(true);
  686. if (this->GetEntityBase()->GetFunction()->GetVTMErrMsg(dwCode, tmpDesc, tmpRTA) != Error_Succeed)
  687. tmpRTA = CSimpleStringA::Format("0x%X", dwCode);//if map failed, use dwCode instead
  688. if (bInBusiness) {
  689. LogError(Severity_High, m_errPkgEx.errCode, dwCode, alarmMsg.GetData());
  690. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_USER).setLogCode(logCode).setAPI(devApi).setResultCode(tmpRTA.GetData()).setCostTime(costTime)(csErrMsgWithReturnCode.GetData());
  691. }
  692. else {
  693. LogWarn(Severity_High, m_errPkgEx.errCode, dwCode, alarmMsg.GetData());
  694. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER).setLogCode(logCode).setAPI(devApi).setResultCode(tmpRTA.GetData()).setCostTime(costTime)(csErrMsgWithReturnCode.GetData());
  695. }
  696. return;
  697. }
  698. //在旧dep路径下找到文件时告警
  699. void UpdateAndWarnFileFindInDepBak(CSimpleStringA& newDepPath, const char* filename, DWORD userCode)
  700. {
  701. CSimpleStringA strOldPath;
  702. if (!ExistsFileA(newDepPath.GetData()))
  703. {
  704. ErrorCodeEnum eErrOld = this->GetEntityBase()->GetFunction()->GetPath("DepBak", strOldPath);
  705. if (eErrOld != Error_Succeed)
  706. {
  707. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Get [DepBak] Path failed! errcode:%s.", SpStrError(eErrOld));
  708. }
  709. strOldPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", strOldPath.GetData(), filename);
  710. if (ExistsFileA(strOldPath.GetData()))
  711. {
  712. newDepPath = strOldPath;
  713. CSimpleStringA warnMsg = CSimpleStringA::Format("Find %s in OLD dep path!", strOldPath.GetData());
  714. LogWarn(Severity_Middle, Error_Unexpect, userCode, warnMsg.GetData());
  715. }
  716. }
  717. }
  718. ErrorCodeEnum LoadUpAdapterLibrary(const CSimpleStringA& customDllPath = "")
  719. {
  720. m_hDevHelper.SetAdapterName(this->GetEntityBase()->GetEntityName());
  721. ErrorCodeEnum rc = m_hDevHelper.LoadUp(customDllPath.IsNullOrEmpty() ? m_adapterInfo.adapterFilePath : customDllPath);
  722. return rc;
  723. }
  724. /*fulfill adapter elem, open param and adapter file name&absolute path */
  725. void FulfillAdapterInfoFrom(const VendorLibInfo& vendorLib)
  726. {
  727. m_adapterInfo.strVendor = vendorLib.strVendor;
  728. m_adapterInfo.strVersion = vendorLib.strVersion;
  729. m_adapterInfo.strBatch = vendorLib.strBatch;
  730. m_adapterInfo.strPureAdapterName = vendorLib.strFileValue;
  731. CSmartPointer<IConfigInfo> spConfigRoot;
  732. this->GetEntityBase()->GetFunction()->OpenConfig(Config_Root, spConfigRoot);
  733. CSimpleStringA entityName(this->GetEntityBase()->GetEntityName());
  734. if (entityName.Compare("CardIssuerStore", true) == 0 || entityName.Compare("CardIssuerStand", true) == 0) {
  735. entityName = "CardIssuer";
  736. }
  737. CSimpleStringA sectionName = CSimpleStringA::Format("Device.%s", entityName.GetData());
  738. CSimpleStringA csPort(true), csBaudrate(true);
  739. spConfigRoot->ReadConfigValue(sectionName.GetData(), "Port", csPort);
  740. spConfigRoot->ReadConfigValue(sectionName.GetData(), "Baudrate", csBaudrate);
  741. m_adapterInfo.strPort = csPort;
  742. m_adapterInfo.strBaudrate = csBaudrate;
  743. CSimpleStringA csPortNum(true), csCheckData(true), csKeySN(true), csInOutDir(true);
  744. if (!entityName.Compare("Gpio")) {
  745. spConfigRoot->ReadConfigValue(sectionName.GetData(), "PortNum", csPortNum);
  746. spConfigRoot->ReadConfigValue(sectionName.GetData(), "InOutDir", csInOutDir);
  747. m_adapterInfo.strPortNum = csPortNum;
  748. m_adapterInfo.strInOutDir = csInOutDir;
  749. } else if (!entityName.Compare("PinPad")) {
  750. spConfigRoot->ReadConfigValue(sectionName.GetData(), "Checkdata", csCheckData);
  751. spConfigRoot->ReadConfigValue(sectionName.GetData(), "Keysn", csKeySN);
  752. m_adapterInfo.strCheckdata = csCheckData;
  753. m_adapterInfo.strKeysn = csKeySN;
  754. }
  755. m_adapterInfo.adapterFileName = vendorLib.toLibNameString();
  756. CSimpleStringA strDepPath(true);
  757. this->GetEntityBase()->GetFunction()->GetPath("Dep", strDepPath);
  758. m_adapterInfo.adapterFilePath = CSimpleStringA::Format(
  759. "%s" SPLIT_SLASH_STR "%s", (LPCTSTR)strDepPath, (LPCTSTR)m_adapterInfo.adapterFileName);
  760. }
  761. ErrorCodeEnum FulfillAdapterDevCategory()
  762. {
  763. if (!!m_hDevHelper) {
  764. ErrorCodeEnum result(Error_Succeed);
  765. DevCategoryInfo devCat;
  766. ZeroMemory(devCat.szModel, sizeof(devCat.szModel));
  767. ZeroMemory(devCat.szType, sizeof(devCat.szType));
  768. ZeroMemory(devCat.szVendor, sizeof(devCat.szVendor));
  769. result = m_hDevHelper->GetDevCategory(devCat);
  770. if (IS_SUCCEED(result)) {
  771. m_adapterInfo.FulfillCategoryInfo(devCat);
  772. }
  773. return result;
  774. }
  775. return Error_InvalidState;
  776. }
  777. #if defined(_MSC_VER)
  778. const char* LPCSTRSmartPrintString(const CSimpleStringA& str)
  779. {
  780. return str.IsNullOrEmpty() ? "" : str.GetData();
  781. }
  782. CSimpleStringA GenerateJson(AdaptorInfo adaptorInfo, const CSimpleStringA& devDeviceName)
  783. {
  784. CSimpleStringA result(true);
  785. if (0 == devDeviceName.Compare("Gpio", true)) {
  786. result = CSimpleStringA::Format("{\"Vendor\":\"%s\", \"Version\":\"%s\", \"Batch\":\"%s\", \"Port\":\"%s\", \"Baudrate\":\"%s\", \"PortNum\":\"%s\", \"InOutDir\":\"%s\"}",
  787. LPCSTRSmartPrintString(adaptorInfo.strVendor), LPCSTRSmartPrintString(adaptorInfo.strVersion),
  788. LPCSTRSmartPrintString(adaptorInfo.strBatch), LPCSTRSmartPrintString(adaptorInfo.strPort), LPCSTRSmartPrintString(adaptorInfo.strBaudrate),
  789. LPCSTRSmartPrintString(adaptorInfo.strPortNum), LPCSTRSmartPrintString(adaptorInfo.strInOutDir));
  790. } else if (0 == devDeviceName.Compare("PinPad", true)) {
  791. result = CSimpleStringA::Format("{\"Vendor\":\"%s\", \"Version\":\"%s\", \"Batch\":\"%s\", \"Port\":\"%s\", \"Baudrate\":\"%s\", \"Checkdata\":\"%s\", \"Keysn\":\"%s\"}",
  792. LPCSTRSmartPrintString(adaptorInfo.strVendor), LPCSTRSmartPrintString(adaptorInfo.strVersion),
  793. LPCSTRSmartPrintString(adaptorInfo.strBatch), LPCSTRSmartPrintString(adaptorInfo.strPort), LPCSTRSmartPrintString(adaptorInfo.strBaudrate),
  794. LPCSTRSmartPrintString(adaptorInfo.strCheckdata), LPCSTRSmartPrintString(adaptorInfo.strKeysn));
  795. } else {
  796. result = CSimpleStringA::Format("{\"Vendor\":\"%s\", \"Version\":\"%s\", \"Batch\":\"%s\", \"Port\":\"%s\", \"Baudrate\":\"%s\"}",
  797. LPCSTRSmartPrintString(adaptorInfo.strVendor), LPCSTRSmartPrintString(adaptorInfo.strVersion),
  798. LPCSTRSmartPrintString(adaptorInfo.strBatch), LPCSTRSmartPrintString(adaptorInfo.strPort), LPCSTRSmartPrintString(adaptorInfo.strBaudrate));
  799. }
  800. return result;
  801. }
  802. #endif //_MSC_VER
  803. void ToLogRootINIInfoOnce()
  804. {
  805. CSmartPointer<IConfigInfo> spConfig;
  806. this->GetEntityBase()->GetFunction()->OpenConfig(Config_Cache, spConfig);
  807. CSimpleStringA entityName(this->GetEntityBase()->GetEntityName());
  808. int nUploadTime(0);
  809. spConfig->ReadConfigValueInt("RootUploadFlag", entityName, nUploadTime);
  810. SYSTEMTIME stNow = CSmallDateTime::GetNow().ToSystemTime();
  811. SYSTEMTIME lastUploadTime = CSmallDateTime(nUploadTime).ToSystemTime();
  812. if (lastUploadTime.wYear != stNow.wYear || lastUploadTime.wMonth != stNow.wMonth || lastUploadTime.wDay != stNow.wDay) {
  813. ToLogRootINIInfo();
  814. spConfig->WriteConfigValue("RootUploadFlag", entityName, CSimpleStringA::Format("0x%08X", (DWORD)CSmallDateTime::GetNow()));
  815. }
  816. }
  817. /** GPIO不调用此接口 [Gifur@20221025]*/
  818. void ToLogRootINIInfo()
  819. {
  820. CSimpleStringA entityName(this->GetEntityBase()->GetEntityName());
  821. std::map<std::string, std::string> rootInfo;
  822. rootInfo["Vendor"] = m_adapterInfo.strVendor.GetData();
  823. rootInfo["Version"] = m_adapterInfo.strVersion.GetData();
  824. rootInfo["Batch"] = m_adapterInfo.strBatch.GetData();
  825. rootInfo["Port"] = m_adapterInfo.strPort.GetData();
  826. rootInfo["Baudrate"] = m_adapterInfo.strBaudrate.GetData();
  827. if (!entityName.Compare("Gpio")) {
  828. rootInfo["PortNum"] = m_adapterInfo.strPortNum.GetData();
  829. rootInfo["InOutDir"] = m_adapterInfo.strInOutDir.GetData();
  830. } else if (!entityName.Compare("PinPad")) {
  831. rootInfo["Checkdata"] = m_adapterInfo.strCheckdata.GetData();
  832. rootInfo["Keysn"] = m_adapterInfo.strKeysn.GetData();
  833. }
  834. std::pair<bool, std::string> strResult;
  835. strResult = generateJsonStr(rootInfo);
  836. if (!entityName.IsNullOrEmpty()) {
  837. LogWarn(Severity_Low, Error_Debug, m_RootIniCodeMap[entityName.GetData()], strResult.second.c_str());
  838. } else {
  839. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Entity Name is empty, please check!!!");
  840. }
  841. return;
  842. }
  843. /** upload adapter file's hash value and open param as well as dev category information, return if information has uploaded this time*/
  844. bool ToLogWarnInfoAboutTerm()
  845. {
  846. std::map<std::string, std::string> emptys;
  847. return ToLogWarnInfoAboutTermExtend(emptys);
  848. }
  849. /** upload adapter file's hash value and open param as well as dev category information, return if information has uploaded this time
  850. extend from ToLogWarnInfoAboutTerm
  851. */
  852. bool ToLogWarnInfoAboutTermExtend(std::map<std::string, std::string> additionalParams)
  853. {
  854. bool bUpload(false);
  855. int fileSize = 0;
  856. //calculate file hash value
  857. std::map<std::string, std::string> termInfo;
  858. char* strFileHash = new char[128];
  859. if (ExistsFileA(m_adapterInfo.adapterFilePath)) {
  860. BYTE fileHash[32];
  861. SM3File(const_cast<char*>(m_adapterInfo.adapterFilePath.GetData()), fileHash);
  862. ZeroMemory(strFileHash, 128);
  863. SP::Module::Util::HexBuf2StrBuf(fileHash, &strFileHash, 32);
  864. #if defined(_MSC_VER)
  865. HANDLE hFile;
  866. hFile = CreateFile(m_adapterInfo.adapterFilePath.GetData(), 0, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  867. if (hFile == INVALID_HANDLE_VALUE) {
  868. fileSize = 0;
  869. } else {
  870. fileSize = GetFileSize(hFile, NULL);
  871. CloseHandle(hFile);
  872. }
  873. #else
  874. //calculate file size
  875. struct stat statbuf;
  876. ZeroMemory(&statbuf, sizeof(statbuf));
  877. stat(m_adapterInfo.adapterFilePath.GetData(), &statbuf);
  878. fileSize = statbuf.st_size;
  879. #endif //_MSC_VER
  880. } else {
  881. memset(strFileHash, '\0', sizeof(char) * 128);
  882. }
  883. //necessary elements about adapter
  884. CSimpleStringA csPortNum(true), csCheckData(true), csKeySN(true), csInOutDir(true);
  885. termInfo["VendorDllName"] = m_adapterInfo.adapterFilePath.GetData();
  886. termInfo["VendorDllFileHash"] = strFileHash;
  887. ZeroMemory(strFileHash, 128);
  888. termInfo["VendorDllFileSize"] = _itoa(fileSize, strFileHash, 10);
  889. termInfo["szModel"] = m_adapterInfo.devCatInfo.szModel;
  890. termInfo["szType"] = m_adapterInfo.devCatInfo.szType;
  891. termInfo["Port"] = m_adapterInfo.strPort.GetData();
  892. termInfo["Baudrate"] = m_adapterInfo.strBaudrate.GetData();
  893. if (strFileHash != nullptr) {
  894. delete[] strFileHash;
  895. strFileHash = nullptr;
  896. }
  897. CSimpleStringA entityName(this->GetEntityBase()->GetEntityName());
  898. if (!entityName.Compare("Gpio")) {
  899. termInfo["PortNum"] = m_adapterInfo.strPortNum.GetData();
  900. termInfo["InOutDir"] = m_adapterInfo.strInOutDir.GetData();
  901. }
  902. else if (!entityName.Compare("PinPad")) {
  903. termInfo["CheckData"] = m_adapterInfo.strCheckdata.GetData();
  904. termInfo["Keysn"] = m_adapterInfo.strKeysn.GetData();
  905. }
  906. if (!additionalParams.empty()) {
  907. for (auto it = additionalParams.cbegin(); it != additionalParams.cend(); ++it) {
  908. termInfo[it->first.c_str()] = it->second;
  909. }
  910. }
  911. std::pair<bool, std::string> strResult;
  912. strResult = generateJsonStr(termInfo);
  913. CSmartPointer<IConfigInfo> spConfigRun;
  914. CSimpleStringA csWarnMsg;
  915. this->GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  916. spConfigRun->ReadConfigValue("Run", "WarnMsg", csWarnMsg);
  917. SYSTEMTIME localTime;
  918. GetLocalTime(&localTime);
  919. if (csWarnMsg.IsNullOrEmpty() || csWarnMsg.Compare(strResult.second.c_str()) != 0) {
  920. spConfigRun->WriteConfigValueInt("Run", "WarnDay", localTime.wDay);
  921. spConfigRun->WriteConfigValue("Run", "WarnMsg", strResult.second.c_str());
  922. if (!entityName.IsNullOrEmpty()) {
  923. LogWarn(Severity_Low, Error_Unexpect, m_WarnCodeMap[entityName.GetData()], strResult.second.c_str());
  924. bUpload = true;
  925. } else {
  926. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Entity Name is empty, please check!!!");
  927. }
  928. } else {
  929. int wDay = 99;
  930. spConfigRun->ReadConfigValueInt("Run", "WarnDay", wDay);
  931. if (wDay != localTime.wDay) {
  932. spConfigRun->WriteConfigValueInt("Run", "WarnDay", localTime.wDay);
  933. if (!entityName.IsNullOrEmpty()) {
  934. LogWarn(Severity_Low, Error_Unexpect, m_WarnCodeMap[entityName.GetData()], strResult.second.c_str());
  935. bUpload = true;
  936. } else {
  937. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Entity Name is empty, please check!!!");
  938. }
  939. }
  940. }
  941. return bUpload;
  942. }
  943. void ResetRepeatErrTimes()
  944. {
  945. m_contiErrTimes = 0;
  946. }
  947. ErrorCodeEnum TransECWithRepeat(const ErrorCodeEnum& ec)
  948. {
  949. ErrorCodeEnum result(ec);
  950. if (ec != Error_Succeed) {
  951. if (!m_bOpened) {
  952. result = Error_DevNotAvailable;
  953. } else if (0 < CountDevError(this->GetEntityBase())) {
  954. result = Error_DevFailAddUp;
  955. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER)("exceed max allow repeat error times! 0x%x -> 0x%x", ec, result);
  956. }
  957. }
  958. return result;
  959. }
  960. virtual ErrorCodeEnum PreOpenDevice() { return Error_Succeed; }
  961. virtual ErrorCodeEnum ToOpenDevice() { return Error_NotImpl; }
  962. virtual ErrorCodeEnum PostOpenDevice() { return Error_Succeed; }
  963. //暂时未使用
  964. ErrorCodeEnum OpenDevice()
  965. {
  966. if (m_bOpening) {
  967. return Error_Pending;
  968. }
  969. if (m_bOpened) {
  970. return Error_DevAlreadyConnected;
  971. }
  972. m_bOpening = true;
  973. ErrorCodeEnum result = PreOpenDevice();
  974. if (IS_SUCCEED(result)) {
  975. result = ToOpenDevice();
  976. if (IS_SUCCEED(result)) {
  977. m_bOpened = true;
  978. result = PostOpenDevice();
  979. }
  980. }
  981. m_bOpening = false;
  982. return result;
  983. }
  984. virtual void SetInWhatPage(int iPageType) { m_iInWhatPage = iPageType; }
  985. public:
  986. bool m_bOpened;
  987. bool m_bOpening;
  988. protected:
  989. DevAdptLibHelper<TDevClass> m_hDevHelper;
  990. DevEntityErrorCode m_entCode;
  991. BOOL m_FirstStart;
  992. int m_iInWhatPage/*在哪个页面,区分首页,用户桌面,其他页*/;
  993. ErrorPackage m_errPkg;
  994. ErrorPackageEx m_errPkgEx;
  995. CSimpleStringA m_csAlarmMsg;
  996. unsigned int m_contiErrTimes;
  997. AdapterInfo m_adapterInfo;
  998. void UpdateEntityIDIfZero()
  999. {
  1000. if (m_entCode.dwEntityId == 0) {
  1001. UpdateEntityID();
  1002. }
  1003. }
  1004. void UpdateEntityID()
  1005. {
  1006. CEntityStaticInfo esi = { 0 };
  1007. ErrorCodeEnum ec = this->GetEntityBase()->GetFunction()->GetEntityStaticInfo(this->GetEntityBase()->GetEntityName(), esi);
  1008. if (ec == Error_Succeed) {
  1009. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("wEntityDevelopID: 0x%X", esi.wEntityDevelopID);
  1010. m_entCode.ResetWithEntityID(esi.wEntityDevelopID);
  1011. }
  1012. }
  1013. DWORD GetEntityID()
  1014. {
  1015. UpdateEntityIDIfZero();
  1016. return m_entCode.dwEntityId;
  1017. }
  1018. DWORD UpdateDEC(DWORD dwVal = 0)
  1019. {
  1020. UpdateEntityIDIfZero();
  1021. /** 14 is for entity define, FC(6bit) is reserved*/
  1022. int reserved = (dwVal & 0xFC000) >> 14;
  1023. m_entCode.SetDevCode(dwVal, reserved);
  1024. return GetDEC();
  1025. }
  1026. DWORD GetDEC() const
  1027. {
  1028. return m_entCode.GetCompleteErrorCode();
  1029. }
  1030. DWORD GetAlarmDEC(DWORD dwValue = 0)
  1031. {
  1032. if (dwValue > 0)
  1033. UpdateDEC(dwValue);
  1034. //if have been set ErrorCode of entity defined (not device),use it directly
  1035. DWORD dwCode = 0;
  1036. //if ((m_entCode.dwVendorErroCode >> 20) == m_entCode.dwEntityId)
  1037. if(IsEntityIDSettle(m_entCode.dwVendorErroCode))
  1038. dwCode = m_entCode.dwVendorErroCode;
  1039. else
  1040. dwCode = HARDWARE_ENTITY_MAKE_ERRORCODE_TO_BUSINESS(m_entCode);
  1041. return dwCode;
  1042. }
  1043. DWORD AlarmDECToBusiness(DWORD dwVal = 0)
  1044. {
  1045. if (dwVal > 0) {
  1046. UpdateDEC(dwVal);
  1047. }
  1048. //oilyang@20200528 according the meeting result,throw a LogError while answering ctx
  1049. //oilyang@20200525 if have been set ErrorCode of entity defined (not device),use it directly
  1050. const DWORD dwCode = IsEntityIDSettle(m_entCode.dwVendorErroCode)
  1051. ? m_entCode.dwVendorErroCode : m_entCode.GetCompleteErrorCode2Business();
  1052. ///**TODO(Gifur@9/10/2021): */
  1053. //LogWarn(Severity_Middle, Error_NotImpl, dwCode, CSimpleStringA::Format("%s TODO:", __FUNCTION__));
  1054. return dwCode;
  1055. }
  1056. void ClearRelatedDEC()
  1057. {
  1058. UpdateDEC();
  1059. m_csAlarmMsg = "No more information";
  1060. }
  1061. bool IsEntityIDSettle(const DWORD& dwVal) const
  1062. {
  1063. return ((dwVal >> 20) == m_entCode.dwEntityId);
  1064. }
  1065. void LogErrMsg(const char* pMsgHead, ErrorCodeEnum eErrCode, DWORD defaultDevCode = 0, BOOL bAlarm = FALSE);
  1066. void LogErrMsgEx(const ErrorPackage& errPkg, const char* pMsgBody, DWORD finalErrCode /*= 0*/, BOOL bToBusiness = FALSE);
  1067. static void GetFileSizeAndCalcHashValue(const char* fileName, int& fileSize, BYTE fileHash[32])
  1068. {
  1069. HANDLE hFile;
  1070. hFile = CreateFile(fileName, 0, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  1071. if (hFile == INVALID_HANDLE_VALUE) {
  1072. fileSize = 0;
  1073. return;
  1074. }
  1075. fileSize = GetFileSize(hFile, NULL);
  1076. CloseHandle(hFile);
  1077. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to calc vendor dll hash");
  1078. SM3File(const_cast<char*>(fileName), fileHash);
  1079. return;
  1080. }
  1081. int CountDevError(CEntityBase *pEntityBase);
  1082. bool IsInBusiness() const { return (m_iInWhatPage == PageType_Other); }
  1083. private:
  1084. std::map<std::string, DWORD> m_WarnCodeMap;
  1085. std::map<std::string, DWORD> m_RootIniCodeMap;
  1086. };
  1087. template<class TFSM, class TDevClass>
  1088. inline ErrorCodeEnum CCommDevFSM<TFSM, TDevClass>::GetAndSplitDevErrInfo(
  1089. CSimpleStringA& csErrMsg, WORD& wdDevErrCode, LPCTSTR lpszFuncNameForDisplay)
  1090. {
  1091. csErrMsg = "";
  1092. wdDevErrCode = 0;
  1093. BOOL bDisplayFunName = TRUE;
  1094. if (lpszFuncNameForDisplay == NULL || strlen(lpszFuncNameForDisplay) == 0) {
  1095. bDisplayFunName = FALSE;
  1096. }
  1097. if (!m_hDevHelper) {
  1098. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("inst occurs nullptr !!!");
  1099. return Error_Param;
  1100. }
  1101. DevErrorInfo devErrInfo;
  1102. ZeroMemory(&devErrInfo, sizeof(DevErrorInfo));
  1103. ErrorCodeEnum erroCode = m_hDevHelper->GetLastErr(devErrInfo);
  1104. if (erroCode == Error_Succeed) {
  1105. if (strlen(devErrInfo.szErrMsg) > 0) {
  1106. csErrMsg = devErrInfo.szErrMsg;
  1107. }
  1108. if (devErrInfo.dwErrMsgLen > MAX_DEV_ERROR_MSG_LEN) {
  1109. wdDevErrCode = (WORD)((devErrInfo.dwErrMsgLen >> 16) & 0x0000FFFF);
  1110. }
  1111. if (bDisplayFunName) {
  1112. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(lpszFuncNameForDisplay)("Invoke <%s> failed, Dev_GLE: DevErrCode[%d], ErrMsg[%s]",
  1113. lpszFuncNameForDisplay, wdDevErrCode, (LPCTSTR)csErrMsg);
  1114. } else {
  1115. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Dev_GLE: DevErrCode[%d], ErrMsg[%s]", wdDevErrCode, (LPCTSTR)csErrMsg);
  1116. }
  1117. } else {
  1118. if (bDisplayFunName) {
  1119. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI("DevAdapter::GetLastErr")("Invoke <%s> failed, and unfortunately Dev_GLE failed returned EC: %s",
  1120. lpszFuncNameForDisplay, SpStrError(erroCode));
  1121. csErrMsg = CSimpleStringA::Format("Invoke <%s> failed", lpszFuncNameForDisplay);
  1122. } else {
  1123. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI("DevAdapter::GetLastErr")("Dev_GLE failed returned EC: %s", SpStrError(erroCode));
  1124. }
  1125. }
  1126. /** reset vendor device error code to stop reporting to alarm [12/11/2020 Gifur] */
  1127. wdDevErrCode = 0;
  1128. return erroCode;
  1129. }
  1130. template<class TFSM, class TDevClass>
  1131. inline void CCommDevFSM<TFSM, TDevClass>::LogErrMsg(
  1132. const char* pMsgHead, ErrorCodeEnum eErrCode, DWORD defaultDevCode, BOOL bAlarm)
  1133. {
  1134. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("%s failed, EC = %s", pMsgHead, SpStrError(eErrCode));
  1135. WORD wdErrCode = 0;
  1136. CSimpleStringA csErrMsg(true);
  1137. ErrorCodeEnum ec = GetAndSplitDevErrInfo(csErrMsg, wdErrCode);
  1138. if (IsEntityIDSettle(defaultDevCode)) {
  1139. UpdateDEC(defaultDevCode);
  1140. } else if (ec == Error_Succeed && wdErrCode != 0) {
  1141. UpdateDEC(wdErrCode);
  1142. } else if (defaultDevCode != 0) {
  1143. UpdateDEC(defaultDevCode);
  1144. }
  1145. m_csAlarmMsg = CSimpleStringA::Format("%s failed EC= %s : %s",
  1146. pMsgHead, SpStrError(eErrCode), (LPCTSTR)csErrMsg);
  1147. if (bAlarm) {
  1148. LogWarn(Severity_Middle, eErrCode, AlarmDECToBusiness(), (LPCTSTR)m_csAlarmMsg);
  1149. }
  1150. return;
  1151. }
  1152. template<class TFSM, class TDevClass>
  1153. inline void CCommDevFSM<TFSM, TDevClass>::LogErrMsgEx(const ErrorPackage& errPkg, const char* pMsgBody, DWORD finalErrCode /*= 0*/, BOOL bToBusiness)
  1154. {
  1155. const CSimpleStringA alarmMsg = CSimpleStringA::Format("{\"DevSN\":\"%s\", \"Prefix\":\"%s\", \"Code\":\"%s\", \"Msg\":\"%s\"}"
  1156. , errPkg.devSN.GetData(), errPkg.msgHead.GetData(), SpStrError(errPkg.devErrCode), pMsgBody);
  1157. if (bToBusiness) {
  1158. LogError(Severity_High, errPkg.devErrCode, finalErrCode, alarmMsg.GetData());
  1159. } else {
  1160. LogWarn(Severity_High, errPkg.devErrCode, finalErrCode, alarmMsg.GetData());
  1161. }
  1162. return;
  1163. }
  1164. //1:报错累计到上限,需要返回错误码给业务
  1165. //0:报错累计未到上限,或者不需要计数
  1166. //-1:出现错误
  1167. template<class TFSM, class TDevClass>
  1168. int CCommDevFSM<TFSM, TDevClass>::CountDevError(CEntityBase* pEntityBase)
  1169. {
  1170. static int iErrNumLimit = -1;
  1171. if (iErrNumLimit == -1) {
  1172. CSmartPointer<IConfigInfo> spCenterConfig;
  1173. ErrorCodeEnum eErrDev = pEntityBase->GetFunction()->OpenConfig(Config_CenterSetting, spCenterConfig);
  1174. if (eErrDev != Error_Succeed) {
  1175. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("open centersetting file failed!");
  1176. return -1;
  1177. } else {
  1178. int value(0);
  1179. spCenterConfig->ReadConfigValueInt("Common", "ErrUpperLimit", value);
  1180. if (0 == value) {
  1181. iErrNumLimit = 5;
  1182. } else if (0 > value) {
  1183. iErrNumLimit = 0;
  1184. } else {
  1185. iErrNumLimit = value;
  1186. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("iErrNumLimit=%d", iErrNumLimit);
  1187. }
  1188. }
  1189. }
  1190. m_contiErrTimes++;
  1191. if (iErrNumLimit > 0 && m_contiErrTimes >= iErrNumLimit) {
  1192. return 1;
  1193. } else {
  1194. return 0;
  1195. }
  1196. }
  1197. #endif /*_COMM_DEVICE_FSM_BASE_HPP_*/