DevFSMCommBase.hpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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. typedef ErrorCodeEnum(*DevAdaptObjCreateFunc)(DeviceBaseClass*& baseObj);
  17. typedef ErrorCodeEnum(*DevAdaptObjReleaseFunc)(DeviceBaseClass*& pBaseObj);
  18. #else //C/C++ Compiler
  19. using DevAdaptObjCreateFunc = ErrorCodeEnum(*)(DeviceBaseClass*& pOutDevAptObj);
  20. using DevAdaptObjReleaseFunc = ErrorCodeEnum(*)(DeviceBaseClass*& pInDevAptObj);
  21. #endif //_MSC_VER
  22. #define HARDWARE_ENTITY_RESET_ENTITYID(ent, entityID) \
  23. do{\
  24. memset(&ent, 0, sizeof(ent)); \
  25. ent.dwEntityId = entityID; \
  26. } while (false)
  27. #define HARDWARE_ENTITY_SET_DEVCODE(ent, ec, reserved) \
  28. do {\
  29. ent.dwVendorErroCode = ec; \
  30. ent.dwReserved = reserved; \
  31. } while (false)
  32. // 先不上厂商标识 [5/14/2020 7:34 @Gifur]
  33. #define HARDWARE_ENTITY_SET_VENDOR_NAME(ent, str) \
  34. ent.dwVendorId = 0/*(DWORD)DeviceBaseHelper::GetCurVendorType(str)*/
  35. #define HARDWARE_ENTITY_MAKE_ERRORCODE(ec) \
  36. MAKE_SLV_ERRORCODE(ec.dwEntityId, (ec.dwVendorId), ec.dwVendorErroCode)
  37. #define HARDWARE_ENTITY_MAKE_ERRORCODE_TO_BUSINESS(ec) \
  38. MAKE_SLV_ERRORCODE_TO_BUSINESS(ec.dwEntityId, ec.dwVendorErroCode, ec.dwReserved)
  39. /*class object must have 'LogErrMsg' function.*/
  40. #define LOG_ERROR_MSG_MACRO(errCode, MethodSig, DeviceType)\
  41. do {\
  42. if(errCode != Error_Succeed) {\
  43. LogErrMsg(CSimpleStringA::Format("%s::%s at line:%d", __FUNCTION__, #MethodSig, __LINE__), \
  44. errCode, MEC_DEVAPI_##DeviceType##_(MethodSig), TRUE);\
  45. }\
  46. } while (false)
  47. #define LOG_CARDISSUER_ERROR_MSG_MACRO(errCode, MethodSig)\
  48. LOG_ERROR_MSG_MACRO(errCode, MethodSig, CARDISSUER)
  49. #define LOG_CARDSWIPER_ERROR_MSG_MACRO(errCode, MethodSig)\
  50. LOG_ERROR_MSG_MACRO(errCode, MethodSig, CARDSWIPER)
  51. #define LOG_THERMALPRINT_ERROR_MSG_MACRO(errCode, MethodSig)\
  52. LOG_ERROR_MSG_MACRO(errCode, MethodSig, THERMALPRINT)
  53. #define LOG_DEVCONTROL_ERROR_MSG_MACRO(errCode, MethodSig)\
  54. LOG_ERROR_MSG_MACRO(errCode, MethodSig, DEVCONTROL)
  55. #define LOG_PRINTSEAL_ERROR_MSG_MACRO(errCode, MethodSig)\
  56. LOG_ERROR_MSG_MACRO(errCode, MethodSig, PRINTSEAL)
  57. #define LOG_FINGERPRINT_ERROR_MSG_MACRO(errCode, MethodSig)\
  58. LOG_ERROR_MSG_MACRO(errCode, MethodSig, FINGERPRINT)
  59. #define LOG_HSPSCANNER_ERROR_MSG_MACRO(errCode, MethodSig)\
  60. LOG_ERROR_MSG_MACRO(errCode, MethodSig, HSPSCANNER)
  61. #define LOG_UPS_ERROR_MSG_MACRO(errCode, MethodSig)\
  62. LOG_ERROR_MSG_MACRO(errCode, MethodSig, UPS)
  63. #define LOG_GPIO_ERROR_MSG_MACRO(errCode, MethodSig)\
  64. LOG_ERROR_MSG_MACRO(errCode, MethodSig, GPIO)
  65. #define LOG_IDCER_ERROR_MSG_MACRO(errCode, MethodSig)\
  66. LOG_ERROR_MSG_MACRO(errCode, MethodSig, IDCER)
  67. #define LOG_RF_ERROR_MSG_MACRO(errCode, MethodSig)\
  68. LOG_ERROR_MSG_MACRO(errCode, MethodSig, RF)
  69. #define LOG_PINPAD_ERROR_MSG_MACRO(errCode, MethodSig)\
  70. LOG_ERROR_MSG_MACRO(errCode, MethodSig, EPP)
  71. #define LOG_WATCHDOG_ERROR_MSG_MACRO(errCode, MethodSig)\
  72. LOG_ERROR_MSG_MACRO(errCode, MethodSig, WATCHDOG)
  73. enum PageType
  74. {
  75. PageType_MainPage,
  76. PageType_UserDesktop,
  77. PageType_Other,
  78. };
  79. enum VendorNameType
  80. {
  81. Vendor_Invalide,
  82. Vendor_Sankyo,
  83. Vendor_ChinaVision,
  84. Vendor_Hyosung,
  85. Vendor_Kxd,
  86. Vendor_Keba,
  87. Vendor_Nantian,
  88. Vendor_yotap,
  89. Vendor_ZSS,
  90. Vendor_GreateWall,
  91. Vendor_YiHua,
  92. Vendor_GRG,
  93. Vendor_CW,
  94. Vendor_EastCom,
  95. Vendor_HuaXin,
  96. Vendor_MAX
  97. };
  98. static const char* VendorNameStr[] = {
  99. "Unknown",
  100. "Sankyo",
  101. "ChinaVision",
  102. "hyosung",
  103. "kxd",
  104. "keba",
  105. "nantian",
  106. "yotap",
  107. "zss",
  108. "gwi",
  109. "yh", // "yihua"
  110. "grg",
  111. "CW",
  112. "EastCom",
  113. "hx"
  114. };
  115. inline static VendorNameType GetCurVendorType(LPCTSTR vendorName)
  116. {
  117. if (vendorName == NULL || strlen(vendorName) == 0) {
  118. return Vendor_Invalide;
  119. }
  120. int idx = -1;
  121. for (int i = 0; i < sizeof(VendorNameStr) / sizeof(VendorNameStr[0]); ++i) {
  122. if (strnicmp(vendorName, VendorNameStr[i], strlen(VendorNameStr[i])) == 0) {
  123. idx = i;
  124. break;
  125. }
  126. }
  127. if (idx == -1) {
  128. /*历史遗留原因,怡化厂商有两个适配器名字*/
  129. if (strnicmp(vendorName, "yihua", strlen("yihua")) == 0) {
  130. idx = Vendor_YiHua;
  131. } else {
  132. idx = Vendor_Invalide;
  133. }
  134. }
  135. return (VendorNameType)idx;
  136. }
  137. template <typename TSubAdpt>
  138. struct DevAdptLibHelper
  139. {
  140. ErrorCodeEnum LoadLibAddress(const CSimpleStringA& strFullLibPath)
  141. {
  142. ErrorCodeEnum erroCode = Error_Succeed;
  143. do {
  144. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to load lib: %s", strFullLibPath.GetData());
  145. if (!ExistsFileA(strFullLibPath)) {
  146. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("the lib file %s is not existed!", strFullLibPath.GetData());
  147. erroCode = Error_NotExist;
  148. break;
  149. }
  150. int res = toolkit_dlopen(strFullLibPath, &m_lib);
  151. if (res != 0) {
  152. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("toolkit_dlopen[%s] failed with error %s.", strFullLibPath.GetData(), toolkit_dlerror(&m_lib));
  153. erroCode = Error_DevLoadFileFailed;
  154. break;
  155. }
  156. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to get CreateDevComponent pointer address...");
  157. if ((res = toolkit_dlsym(&m_lib, "CreateDevComponent", (void**)&pFuncCreateAdapt)) != 0)
  158. {
  159. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("Get 'CreateDevComponent' Func address failed with error: %s", toolkit_dlerror(&m_lib));
  160. erroCode = Error_DevLoadFileFailed;
  161. break;
  162. }
  163. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to get ReleaseDevComponent pointer address...");
  164. if ((res = toolkit_dlsym(&m_lib, "ReleaseDevComponent", (void**)&pFuncReleaseAdapt)) != 0)
  165. {
  166. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("Get 'ReleaseDevComponent' Func address failed with error: %s", toolkit_dlerror(&m_lib));
  167. erroCode = Error_DevLoadFileFailed;
  168. break;
  169. }
  170. } while (false);
  171. if (IS_FAILURED(erroCode))
  172. {
  173. TearDown();
  174. }
  175. return erroCode;
  176. }
  177. ErrorCodeEnum CreateDevAdptObject()
  178. {
  179. if (m_AdptObjPtr != nullptr) {
  180. return Error_AlreadyExist;
  181. }
  182. if (pFuncCreateAdapt == nullptr || pFuncReleaseAdapt == nullptr) {
  183. return Error_NotInit;
  184. }
  185. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to get Adapter object...");
  186. ErrorCodeEnum erroCode = pFuncCreateAdapt((DeviceBaseClass*&)m_AdptObjPtr);
  187. if (IS_FAILURED(erroCode)) {
  188. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("Create device adapter object failed! EC=%s", SpStrError(erroCode));
  189. return erroCode;
  190. }
  191. LOG_ASSERT(m_AdptObjPtr != nullptr);
  192. return Error_Succeed;
  193. }
  194. /** Integrate function LoadLibAddress and CreateDevAdptObject*/
  195. ///*TODO(80374374@3/31/2023): remove input param for fetching vendor library path */
  196. ErrorCodeEnum LoadUp(const CSimpleStringA& strFullLibPath)
  197. {
  198. ErrorCodeEnum erroCode = LoadLibAddress(strFullLibPath);
  199. if (IS_SUCCEED(erroCode)) {
  200. erroCode = CreateDevAdptObject();
  201. if (!IS_SUCCEED(erroCode)) {
  202. TearDown();
  203. }
  204. }
  205. return erroCode;
  206. }
  207. void TearDown()
  208. {
  209. LOG_FUNCTION();
  210. if (m_AdptObjPtr != nullptr)
  211. {
  212. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("To DevClose...");
  213. ErrorCodeEnum result = m_AdptObjPtr->DevClose();
  214. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("DevClose returned %s", SpStrError(result));
  215. if (nullptr != pFuncReleaseAdapt)
  216. {
  217. DeviceBaseClass* devBasePtr = static_cast<DeviceBaseClass*>(m_AdptObjPtr);
  218. if (devBasePtr != nullptr) {
  219. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("To Release DevAdapter Object...");
  220. pFuncReleaseAdapt(devBasePtr);
  221. m_AdptObjPtr = nullptr;
  222. } else {
  223. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("static cast base dev class failed!");
  224. }
  225. }
  226. }
  227. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to dlclose DevAdapter libs...");
  228. toolkit_dlclose(&m_lib);
  229. pFuncReleaseAdapt = pFuncCreateAdapt = nullptr;
  230. }
  231. #if defined(_MSC_VER)
  232. operator bool() const { return (m_AdptObjPtr != nullptr); }
  233. #else
  234. /** Linux 下不加此会影响 编译 Gifur@2023329]*/
  235. explicit operator bool() const { return (m_AdptObjPtr != nullptr); }
  236. #endif //_MSC_VER
  237. operator DeviceBaseClass* () { return m_AdptObjPtr; }
  238. TSubAdpt* GetDevPointer() { return m_AdptObjPtr; }
  239. TSubAdpt* operator->()
  240. {
  241. if (m_AdptObjPtr == nullptr) {
  242. throw std::runtime_error("the dev class pointer is nullptr!!");
  243. }
  244. return m_AdptObjPtr;
  245. }
  246. bool operator !=(TSubAdpt* rhs)
  247. {
  248. return !(m_AdptObjPtr == rhs);
  249. }
  250. bool operator !=(const DevAdptLibHelper& rhs)
  251. {
  252. return !(m_AdptObjPtr == rhs.operator->());
  253. }
  254. DevAdptLibHelper()
  255. : pFuncCreateAdapt(nullptr), pFuncReleaseAdapt(nullptr)
  256. , m_AdptObjPtr(nullptr)
  257. {
  258. memset(&m_lib, 0, sizeof(toolkit_lib_t));
  259. }
  260. ~DevAdptLibHelper()
  261. {
  262. TearDown();
  263. }
  264. #if defined(RVC_OS_LINUX)
  265. DevAdptLibHelper(const DevAdptLibHelper&) = delete;
  266. DevAdptLibHelper& operator=(const DevAdptLibHelper&) = delete;
  267. #endif //RVC_OS_LINUX
  268. private:
  269. toolkit_lib_t m_lib;
  270. DevAdaptObjCreateFunc pFuncCreateAdapt;
  271. DevAdaptObjReleaseFunc pFuncReleaseAdapt;
  272. TSubAdpt* m_AdptObjPtr;
  273. };
  274. struct AdaptorInfo
  275. {
  276. public:
  277. CSimpleStringA strVendor;
  278. CSimpleStringA strVersion;
  279. CSimpleStringA strBatch;
  280. CSimpleStringA strPort;
  281. CSimpleStringA strBaudrate;
  282. CSimpleStringA strPortNum;
  283. CSimpleStringA strInOutDir;
  284. CSimpleStringA strCheckdata;
  285. CSimpleStringA strKeysn;
  286. AdaptorInfo() :strVendor(true), strVersion(true), strBatch(true)
  287. , strPort(true), strBaudrate(true)
  288. , strPortNum(true), strInOutDir(true)
  289. , strCheckdata(true), strKeysn(true)
  290. {
  291. }
  292. int GetVersionInt() const
  293. {
  294. int result = 0;
  295. if (!strVersion.IsNullOrEmpty()) {
  296. result = atoi(strVersion.GetData());
  297. }
  298. return result;
  299. }
  300. int GetBatchInt() const
  301. {
  302. int result = 0;
  303. if (!strBatch.IsNullOrEmpty()) {
  304. result = atoi(strBatch.GetData());
  305. }
  306. return result;
  307. }
  308. int GetPortInt() const
  309. {
  310. int result = 0;
  311. if (!strPort.IsNullOrEmpty()) {
  312. result = atoi(strPort.GetData());
  313. }
  314. return result;
  315. }
  316. int GetBaudrateInt() const
  317. {
  318. int result = 0;
  319. if (!strBaudrate.IsNullOrEmpty()) {
  320. result = atoi(strBaudrate.GetData());
  321. }
  322. return result;
  323. }
  324. };
  325. struct AdapterInfo : public AdaptorInfo
  326. {
  327. CSimpleStringA adapterFilePath;
  328. CSimpleStringA adapterFileName;
  329. DevCategoryInfo devCatInfo;
  330. AdapterInfo() :adapterFilePath(true), adapterFileName(true)
  331. {
  332. ZeroMemory(devCatInfo.szModel, sizeof(devCatInfo.szModel));
  333. ZeroMemory(devCatInfo.szType, sizeof(devCatInfo.szType));
  334. ZeroMemory(devCatInfo.szVendor, sizeof(devCatInfo.szVendor));
  335. }
  336. void FulfillCategoryInfo(const DevCategoryInfo& rhs)
  337. {
  338. devCatInfo.eState = rhs.eState;
  339. devCatInfo.version.wMajor = rhs.version.wMajor;
  340. devCatInfo.version.wMinor = rhs.version.wMinor;
  341. devCatInfo.version.wRevision = rhs.version.wRevision;
  342. devCatInfo.version.wBuild = rhs.version.wBuild;
  343. strcpy(devCatInfo.szType, rhs.szType);
  344. strcpy(devCatInfo.szModel, rhs.szModel);
  345. strcpy(devCatInfo.szVendor, rhs.szVendor);
  346. CSimpleStringA strVersion = CSimpleStringA::Format("%d.%d.%d.%d", devCatInfo.version.wMajor, devCatInfo.version.wMinor, devCatInfo.version.wRevision, devCatInfo.version.wBuild);
  347. if (strlen(devCatInfo.szType) >= MAX_DEV_TYPE_LEN) {
  348. devCatInfo.szType[MAX_DEV_TYPE_LEN - 1] = '\0';
  349. }
  350. if (strlen(devCatInfo.szModel) >= MAX_DEV_MODEL_LEN) {
  351. devCatInfo.szModel[MAX_DEV_MODEL_LEN - 1] = '\0';
  352. }
  353. if (strlen(devCatInfo.szVendor) >= MAX_DEV_VENDOR_LEN) {
  354. devCatInfo.szVendor[MAX_DEV_VENDOR_LEN - 1] = '\0';
  355. }
  356. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI("GetDevCategory")("{\"szVendor\":\"%s\",\"szType\":\"%s\",\"szModel\":\"%s\",\"szVersion\":\"%s\"}", devCatInfo.szVendor, devCatInfo.szType, devCatInfo.szModel, strVersion.GetData());
  357. CheckCategoryInfoFormat();
  358. }
  359. void CheckCategoryInfoFormat()
  360. {
  361. const int DEV_CATINFO_SUM = 3;
  362. char* devInfo[DEV_CATINFO_SUM] = { "szModel", "szVendor", "szType" };
  363. for (int devSn = 0; devSn < DEV_CATINFO_SUM; devSn++)
  364. {
  365. CSimpleStringA strCatInfo;
  366. if (devSn == 0)
  367. strCatInfo = devCatInfo.szModel;
  368. else if (devSn == 1)
  369. strCatInfo = devCatInfo.szVendor;
  370. else if (devSn == 2)
  371. strCatInfo = devCatInfo.szType;
  372. if (!strCatInfo.IsNullOrEmpty())
  373. {
  374. auto arr = strCatInfo.Split('#');
  375. if (arr.GetCount() > 0)
  376. {
  377. for (int sec = 0; sec < arr.GetCount(); sec++)
  378. {
  379. auto arr2 = arr[sec].Split('=');
  380. if (arr2.GetCount() != 2)
  381. {
  382. if (devSn != 1)//szVendor 没有要求符合"key=value#"的格式
  383. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("CheckDevInfoFormat, Wrong section of %s:%s", devInfo[devSn], arr[sec].GetData());
  384. continue;
  385. }
  386. if (strnicmp((LPCTSTR)arr2[0], "CM", strlen("CM")) == 0 || strnicmp((LPCTSTR)arr2[0], "PM", strlen("PM")) == 0)
  387. {
  388. string strData = arr2[1].GetData();
  389. regex pattern("V1.0|V2.0");
  390. smatch sm;
  391. if (!regex_match(strData, sm, pattern))
  392. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("CheckDevInfoFormat, Wrong format of %s:%s", arr2[0].GetData(), arr2[1].GetData());
  393. }
  394. else if (strnicmp((LPCTSTR)arr2[0], "PID", strlen("PID")) == 0 || strnicmp((LPCTSTR)arr2[0], "MID", strlen("MID")) == 0
  395. || strnicmp((LPCTSTR)arr2[0], "FWID", strlen("FWID")) == 0 || strnicmp((LPCTSTR)arr2[0], "CID", strlen("CID")) == 0)
  396. {
  397. string strData = arr2[1].GetData();
  398. regex pattern(".{8,16}");
  399. smatch sm;
  400. if (!regex_match(strData, sm, pattern))
  401. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("CheckDevInfoFormat, Wrong format of %s:%s", arr2[0].GetData(), arr2[1].GetData());
  402. }
  403. else if (strnicmp((LPCTSTR)arr2[0], "STYLE", strlen("STYLE")) == 0)
  404. {
  405. string strData = arr2[1].GetData();
  406. regex pattern("CL|IG");
  407. smatch sm;
  408. if (!regex_match(strData, sm, pattern))
  409. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("CheckDevInfoFormat, Wrong format of %s:%s", arr2[0].GetData(), arr2[1].GetData());
  410. }
  411. else if (strnicmp((LPCTSTR)arr2[0], "CODE", strlen("CODE")) == 0)
  412. {
  413. string strData = arr2[1].GetData();
  414. regex pattern("UCS2");
  415. smatch sm;
  416. if (!regex_match(strData, sm, pattern))
  417. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("CheckDevInfoFormat, Wrong format of %s:%s", arr2[0].GetData(), arr2[1].GetData());
  418. }
  419. else if (strnicmp((LPCTSTR)arr2[0], "FUNCTION", strlen("FUNCTION")) == 0)
  420. {
  421. string strData = arr2[1].GetData();
  422. regex pattern("^(I|T|F|IT|TI|IF|FI|TF|FT|ITF|IFT|FIT|FTI|FIT|FTI)$");//stupid,how to write the right... composite of 'I'/'T'/'F'
  423. smatch sm;
  424. if (!regex_match(strData, sm, pattern))
  425. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("CheckDevInfoFormat, Wrong format of %s:%s", arr2[0].GetData(), arr2[1].GetData());
  426. }
  427. }
  428. }
  429. else
  430. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("CheckDevInfoFormat, can't split from %s:%s", devInfo[devSn], strCatInfo.GetData());
  431. }
  432. else
  433. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("CheckDevInfoFormat, %s is empty.", devInfo[devSn]);
  434. }
  435. }
  436. };
  437. struct ErrorPackage {
  438. CSimpleStringA msgHead;
  439. ///*TODO(80374374@3/31/2023): to apply, replace the one defined at entity scope with it */
  440. CSimpleStringA devSN;
  441. ErrorCodeEnum devErrCode;
  442. DWORD apiErrCode;
  443. ErrorPackage() :msgHead(true), devSN(true), devErrCode(Error_Succeed), apiErrCode(0) {}
  444. };
  445. ///*TODO(80374374@3/31/2023): */
  446. /*
  447. CEntityStaticInfo esi = {0};
  448. ErrorCodeEnum ec = GetEntityBase()->GetFunction()->GetEntityStaticInfo(
  449. GetEntityBase()->GetEntityName(), esi);
  450. if(ec == Error_Succeed)
  451. {
  452. DbgSysInfoWithLink("wEntityDevelopID: 0x%X", esi.wEntityDevelopID);
  453. HARDWARE_ENTITY_RESET_ENTITYID(m_entCode, esi.wEntityDevelopID);
  454. }
  455. */
  456. struct DevEntityErrorCode {
  457. DWORD dwEntityId;
  458. DWORD dwVendorId;
  459. DWORD dwVendorErroCode;
  460. DWORD dwReserved;
  461. DevEntityErrorCode() :dwEntityId(0), dwVendorId(0), dwVendorErroCode(0), dwReserved(0) {}
  462. void ResetWithEntityID(WORD wEntityID)
  463. {
  464. dwEntityId = dwVendorId = dwVendorErroCode = dwReserved = 0;
  465. dwEntityId = wEntityID;
  466. }
  467. void SetDevCode(DWORD devErrorCode, DWORD dwReservedCode)
  468. {
  469. dwVendorErroCode = devErrorCode;
  470. dwReserved = dwReservedCode;
  471. }
  472. /*no used now!!*/
  473. void SetVendorID(DWORD dwVendorID)
  474. {
  475. dwVendorId = dwVendorID;
  476. }
  477. DWORD GetCompleteErrorCode() const
  478. {
  479. return MAKE_SLV_ERRORCODE(dwEntityId, dwVendorId, dwVendorErroCode);
  480. }
  481. DWORD GetCompleteErrorCode2Business() const
  482. {
  483. return MAKE_SLV_ERRORCODE_TO_BUSINESS(dwEntityId, dwVendorErroCode, dwReserved);
  484. }
  485. };
  486. template<class TFSM, class TDevClass>
  487. class CCommDevFSM : public FSMImpl<TFSM>
  488. {
  489. public:
  490. CCommDevFSM() :m_FirstStart(TRUE), m_bOpened(false), m_bOpening(false), m_csAlarmMsg("No more information"), m_contiErrTimes(0)
  491. {
  492. m_WarnCodeMap.insert(std::make_pair<std::string, DWORD>("CardIssuer", 0x2030021f));
  493. m_WarnCodeMap.insert(std::make_pair<std::string, DWORD>("ContactlessCard", 0x21300203));
  494. m_WarnCodeMap.insert(std::make_pair<std::string, DWORD>("CardSwiper", 0x20200207));
  495. m_WarnCodeMap.insert(std::make_pair<std::string, DWORD>("FingerPrint", 0x20400211));
  496. m_WarnCodeMap.insert(std::make_pair<std::string, DWORD>("IDCertificate", 0x20100202));
  497. m_WarnCodeMap.insert(std::make_pair<std::string, DWORD>("PinPad", 0x20600207));
  498. m_WarnCodeMap.insert(std::make_pair<std::string, DWORD>("Gpio", 0x20900201));
  499. m_WarnCodeMap.insert(std::make_pair<std::string, DWORD>("HSPScanner", 0x21700207));
  500. m_WarnCodeMap.insert(std::make_pair<std::string, DWORD>("WatchDog", 0x20d00201));
  501. m_WarnCodeMap.insert(std::make_pair<std::string, DWORD>("Ups", 0x20e00201));
  502. m_RootIniCodeMap.insert(std::make_pair<std::string, DWORD>("CardIssuer", 0x20300299));
  503. m_RootIniCodeMap.insert(std::make_pair<std::string, DWORD>("ContactlessCard", 0x21300224));
  504. m_RootIniCodeMap.insert(std::make_pair<std::string, DWORD>("CardSwiper", 0x20200238));
  505. m_RootIniCodeMap.insert(std::make_pair<std::string, DWORD>("FingerPrint", 0x20400229));
  506. m_RootIniCodeMap.insert(std::make_pair<std::string, DWORD>("IDCertificate", 0x2010021f));
  507. m_RootIniCodeMap.insert(std::make_pair<std::string, DWORD>("PinPad", 0x2060021f));
  508. m_RootIniCodeMap.insert(std::make_pair<std::string, DWORD>("Gpio", 0x20900203));
  509. m_RootIniCodeMap.insert(std::make_pair<std::string, DWORD>("HSPScanner", 0x2170020f));
  510. m_RootIniCodeMap.insert(std::make_pair<std::string, DWORD>("WatchDog", 0x20d00204));
  511. m_RootIniCodeMap.insert(std::make_pair<std::string, DWORD>("Ups", 0x20e00204));
  512. }
  513. ErrorCodeEnum GetAndSplitDevErrInfo(
  514. CSimpleStringA& csErrMsg, WORD& wdDevErrCode, LPCTSTR lpszFuncNameForDisplay = "");
  515. ErrorCodeEnum LogDevErrInfo(LPCTSTR lpszFuncName = "")
  516. {
  517. CSimpleStringA csMsg;
  518. WORD wdErrorCode = 0;
  519. return GetAndSplitDevErrInfo(csMsg, wdErrorCode, lpszFuncName);
  520. }
  521. bool GetDevErrorCode(WORD& wdErrorCode)
  522. {
  523. CSimpleStringA csMsg;
  524. ErrorCodeEnum eRet = GetAndSplitDevErrInfo(csMsg, wdErrorCode);
  525. return (eRet == Error_Succeed);
  526. }
  527. void SetErrPackage(CSimpleStringA msgHead, CSimpleStringA devSN, ErrorCodeEnum errCode, DWORD apiCode)
  528. {
  529. m_errPkg.msgHead = msgHead;
  530. m_errPkg.devSN = devSN;
  531. m_errPkg.devErrCode = errCode;
  532. m_errPkg.apiErrCode = apiCode;
  533. }
  534. DWORD AlarmDEC(bool bToBusiness = false, bool alarmOrNot = true) {
  535. WORD wdErrCode = 0;
  536. CSimpleStringA csErrMsg(true);
  537. ErrorCodeEnum ec = GetAndSplitDevErrInfo(csErrMsg, wdErrCode, (LPCTSTR)m_errPkg.msgHead);
  538. if (ec == Error_Succeed && wdErrCode != 0) {
  539. //oilyang@20200525 if have been set ErrorCode of entity defined (not device),use it directly
  540. if ((m_errPkg.apiErrCode >> 20) == m_entCode.dwEntityId)
  541. UpdateDEC(m_errPkg.apiErrCode);
  542. else
  543. UpdateDEC(wdErrCode);
  544. }
  545. else if (m_errPkg.apiErrCode != 0) {
  546. UpdateDEC(m_errPkg.apiErrCode);
  547. }
  548. //oilyang@20200525 if have been set ErrorCode of entity defined (not device),use it directly
  549. DWORD dwCode = GetAlarmDEC();
  550. if (alarmOrNot) {
  551. LogErrMsgEx(m_errPkg, csErrMsg, dwCode, bToBusiness);
  552. } else {
  553. const CSimpleStringA alarmMsg = CSimpleStringA::Format("{\"DevSN\":\"%s\", \"Prefix\":\"%s\", \"Code\":\"%s\", \"Msg\":\"%s\"}"
  554. , m_errPkg.devSN.GetData(), m_errPkg.msgHead.GetData(), SpStrError(m_errPkg.devErrCode), csErrMsg);
  555. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)(alarmMsg.GetData());
  556. }
  557. return dwCode;
  558. }
  559. /*fulfill adapter elem, open param and adapter file name&absolute path */
  560. void FulfillAdapterInfoFrom(const VendorLibInfo& vendorLib)
  561. {
  562. m_adapterInfo.strVendor = vendorLib.strVendor;
  563. m_adapterInfo.strVersion = vendorLib.strVersion;
  564. m_adapterInfo.strBatch = vendorLib.strBatch;
  565. CSmartPointer<IConfigInfo> spConfigRoot;
  566. this->GetEntityBase()->GetFunction()->OpenConfig(Config_Root, spConfigRoot);
  567. CSimpleStringA entityName(this->GetEntityBase()->GetEntityName());
  568. CSimpleStringA sectionName = CSimpleStringA::Format("Device.%s", entityName.GetData());
  569. CSimpleStringA csVendor(true), csVersion(true), csBatch(true), csPort(true), csBaudrate(true);
  570. //spConfigRoot->ReadConfigValue(sectionName.GetData(), "Vendor", csVendor);
  571. //spConfigRoot->ReadConfigValue(sectionName.GetData(), "Version", csVersion);
  572. //spConfigRoot->ReadConfigValue(sectionName.GetData(), "Batch", csBatch);
  573. spConfigRoot->ReadConfigValue(sectionName.GetData(), "Port", csPort);
  574. spConfigRoot->ReadConfigValue(sectionName.GetData(), "Baudrate", csBaudrate);
  575. m_adapterInfo.strPort = csPort;
  576. m_adapterInfo.strBaudrate = csBaudrate;
  577. CSimpleStringA csPortNum(true), csCheckData(true), csKeySN(true), csInOutDir(true);
  578. if (!entityName.Compare("Gpio")) {
  579. spConfigRoot->ReadConfigValue(sectionName.GetData(), "PortNum", csPortNum);
  580. spConfigRoot->ReadConfigValue(sectionName.GetData(), "InOutDir", csInOutDir);
  581. m_adapterInfo.strPortNum = csPortNum;
  582. m_adapterInfo.strInOutDir = csInOutDir;
  583. } else if (!entityName.Compare("PinPad")) {
  584. spConfigRoot->ReadConfigValue(sectionName.GetData(), "Checkdata", csCheckData);
  585. spConfigRoot->ReadConfigValue(sectionName.GetData(), "Keysn", csKeySN);
  586. m_adapterInfo.strCheckdata = csCheckData;
  587. m_adapterInfo.strKeysn = csKeySN;
  588. }
  589. m_adapterInfo.adapterFileName = vendorLib.toLibNameString();
  590. CSimpleStringA strDepPath;
  591. this->GetEntityBase()->GetFunction()->GetPath("Dep", strDepPath);
  592. m_adapterInfo.adapterFilePath = CSimpleStringA::Format(
  593. "%s" SPLIT_SLASH_STR "%s", (LPCTSTR)strDepPath, (LPCTSTR)m_adapterInfo.adapterFileName);
  594. }
  595. ErrorCodeEnum FulfillAdapterDevCategory()
  596. {
  597. if (!!m_hDevHelper) {
  598. ErrorCodeEnum result(Error_Succeed);
  599. DevCategoryInfo devCat;
  600. ZeroMemory(devCat.szModel, sizeof(devCat.szModel));
  601. ZeroMemory(devCat.szType, sizeof(devCat.szType));
  602. ZeroMemory(devCat.szVendor, sizeof(devCat.szVendor));
  603. result = m_hDevHelper->GetDevCategory(devCat);
  604. if (IS_SUCCEED(result)) {
  605. m_adapterInfo.FulfillCategoryInfo(devCat);
  606. }
  607. return result;
  608. }
  609. return Error_InvalidState;
  610. }
  611. #if defined(_MSC_VER)
  612. const char* LPCSTRSmartPrintString(const CSimpleStringA& str)
  613. {
  614. return str.IsNullOrEmpty() ? "" : str.GetData();
  615. }
  616. CSimpleStringA GenerateJson(AdaptorInfo adaptorInfo, const CSimpleStringA& devDeviceName)
  617. {
  618. CSimpleStringA result(true);
  619. if (0 == devDeviceName.Compare("Gpio", true)) {
  620. result = CSimpleStringA::Format("{\"Vendor\":\"%s\", \"Version\":\"%s\", \"Batch\":\"%s\", \"Port\":\"%s\", \"Baudrate\":\"%s\", \"PortNum\":\"%s\", \"InOutDir\":\"%s\"}",
  621. LPCSTRSmartPrintString(adaptorInfo.strVendor), LPCSTRSmartPrintString(adaptorInfo.strVersion),
  622. LPCSTRSmartPrintString(adaptorInfo.strBatch), LPCSTRSmartPrintString(adaptorInfo.strPort), LPCSTRSmartPrintString(adaptorInfo.strBaudrate),
  623. LPCSTRSmartPrintString(adaptorInfo.strPortNum), LPCSTRSmartPrintString(adaptorInfo.strInOutDir));
  624. } else if (0 == devDeviceName.Compare("PinPad", true)) {
  625. result = CSimpleStringA::Format("{\"Vendor\":\"%s\", \"Version\":\"%s\", \"Batch\":\"%s\", \"Port\":\"%s\", \"Baudrate\":\"%s\", \"Checkdata\":\"%s\", \"Keysn\":\"%s\"}",
  626. LPCSTRSmartPrintString(adaptorInfo.strVendor), LPCSTRSmartPrintString(adaptorInfo.strVersion),
  627. LPCSTRSmartPrintString(adaptorInfo.strBatch), LPCSTRSmartPrintString(adaptorInfo.strPort), LPCSTRSmartPrintString(adaptorInfo.strBaudrate),
  628. LPCSTRSmartPrintString(adaptorInfo.strCheckdata), LPCSTRSmartPrintString(adaptorInfo.strKeysn));
  629. } else {
  630. result = CSimpleStringA::Format("{\"Vendor\":\"%s\", \"Version\":\"%s\", \"Batch\":\"%s\", \"Port\":\"%s\", \"Baudrate\":\"%s\"}",
  631. LPCSTRSmartPrintString(adaptorInfo.strVendor), LPCSTRSmartPrintString(adaptorInfo.strVersion),
  632. LPCSTRSmartPrintString(adaptorInfo.strBatch), LPCSTRSmartPrintString(adaptorInfo.strPort), LPCSTRSmartPrintString(adaptorInfo.strBaudrate));
  633. }
  634. return result;
  635. }
  636. #endif //_MSC_VER
  637. void ToLogRootINIInfoOnce()
  638. {
  639. CSmartPointer<IConfigInfo> spConfig;
  640. this->GetEntityBase()->GetFunction()->OpenConfig(Config_Cache, spConfig);
  641. CSimpleStringA entityName(this->GetEntityBase()->GetEntityName());
  642. int nUploadTime(0);
  643. spConfig->ReadConfigValueInt("RootUploadFlag", entityName, nUploadTime);
  644. SYSTEMTIME stNow = CSmallDateTime::GetNow().ToSystemTime();
  645. SYSTEMTIME lastUploadTime = CSmallDateTime(nUploadTime).ToSystemTime();
  646. if (lastUploadTime.wYear != stNow.wYear || lastUploadTime.wMonth != stNow.wMonth || lastUploadTime.wDay != stNow.wDay) {
  647. ToLogRootINIInfo();
  648. spConfig->WriteConfigValue("RootUploadFlag", entityName, CSimpleStringA::Format("0x%08X", (DWORD)CSmallDateTime::GetNow()));
  649. }
  650. }
  651. /** GPIO不调用此接口 [Gifur@20221025]*/
  652. void ToLogRootINIInfo()
  653. {
  654. CSimpleStringA entityName(this->GetEntityBase()->GetEntityName());
  655. std::map<std::string, std::string> rootInfo;
  656. #if 1
  657. rootInfo["Vendor"] = m_adapterInfo.strVendor.GetData();
  658. rootInfo["Version"] = m_adapterInfo.strVersion.GetData();
  659. rootInfo["Batch"] = m_adapterInfo.strBatch.GetData();
  660. rootInfo["Port"] = m_adapterInfo.strPort.GetData();
  661. rootInfo["Baudrate"] = m_adapterInfo.strBaudrate.GetData();
  662. if (!entityName.Compare("Gpio")) {
  663. rootInfo["PortNum"] = m_adapterInfo.strPortNum.GetData();
  664. rootInfo["InOutDir"] = m_adapterInfo.strInOutDir.GetData();
  665. } else if (!entityName.Compare("PinPad")) {
  666. rootInfo["Checkdata"] = m_adapterInfo.strCheckdata.GetData();
  667. rootInfo["Keysn"] = m_adapterInfo.strKeysn.GetData();
  668. }
  669. #else
  670. CSmartPointer<IConfigInfo> spConfigRoot;
  671. this->GetEntityBase()->GetFunction()->OpenConfig(Config_Root, spConfigRoot);
  672. CSimpleStringA sectionName = CSimpleStringA::Format("Device.%s", entityName.GetData());
  673. CSimpleStringA csVendor(true), csVersion(true), csBatch(true), csPort(true), csBaudrate(true);
  674. spConfigRoot->ReadConfigValue(sectionName.GetData(), "Vendor", csVendor);
  675. spConfigRoot->ReadConfigValue(sectionName.GetData(), "Version", csVersion);
  676. spConfigRoot->ReadConfigValue(sectionName.GetData(), "Batch", csBatch);
  677. spConfigRoot->ReadConfigValue(sectionName.GetData(), "Port", csPort);
  678. spConfigRoot->ReadConfigValue(sectionName.GetData(), "Baudrate", csBaudrate);
  679. CSimpleStringA csPortNum(true), csCheckData(true), csKeySN(true), csInOutDir(true);
  680. rootInfo["Vendor"] = csVendor;
  681. rootInfo["Version"] = csVersion;
  682. rootInfo["Batch"] = csBatch;
  683. rootInfo["Port"] = csPort;
  684. rootInfo["Baudrate"] = csBaudrate;
  685. if (!entityName.Compare("Gpio")) {
  686. spConfigRoot->ReadConfigValue(sectionName.GetData(), "PortNum", csPortNum);
  687. spConfigRoot->ReadConfigValue(sectionName.GetData(), "InOutDir", csInOutDir);
  688. rootInfo["PortNum"] = csPortNum;
  689. rootInfo["InOutDir"] = csInOutDir;
  690. } else if (!entityName.Compare("PinPad")) {
  691. spConfigRoot->ReadConfigValue(sectionName.GetData(), "Checkdata", csCheckData);
  692. spConfigRoot->ReadConfigValue(sectionName.GetData(), "Keysn", csKeySN);
  693. rootInfo["Checkdata"] = csCheckData;
  694. rootInfo["Keysn"] = csKeySN;
  695. }
  696. #endif
  697. std::pair<bool, std::string> strResult;
  698. strResult = generateJsonStr(rootInfo);
  699. strResult.second.c_str();
  700. if (!entityName.IsNullOrEmpty()) {
  701. LogWarn(Severity_Low, Error_Debug, m_RootIniCodeMap[entityName.GetData()], strResult.second.c_str());
  702. } else {
  703. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Entity Name is empty, please check!!!");
  704. }
  705. return;
  706. }
  707. /** upload adapter file's hash value and open param as well as dev category information, return if information has uploaded this time*/
  708. bool ToLogWarnInfoAboutTerm()
  709. {
  710. std::map<std::string, std::string> emptys;
  711. return ToLogWarnInfoAboutTermExtend(emptys);
  712. }
  713. /** upload adapter file's hash value and open param as well as dev category information, return if information has uploaded this time
  714. extend from ToLogWarnInfoAboutTerm
  715. */
  716. bool ToLogWarnInfoAboutTermExtend(std::map<std::string, std::string> additionalParams)
  717. {
  718. bool bUpload(false);
  719. CSimpleStringA entityName(this->GetEntityBase()->GetEntityName());
  720. int fileSize = 0;
  721. //calculate file hash value
  722. std::map<std::string, std::string> termInfo;
  723. char* strFileHash = new char[128];
  724. if (ExistsFileA(m_adapterInfo.adapterFilePath)) {
  725. BYTE fileHash[32];
  726. SM3File(const_cast<char*>(m_adapterInfo.adapterFilePath.GetData()), fileHash);
  727. ZeroMemory(strFileHash, 128);
  728. SP::Module::Util::HexBuf2StrBuf(fileHash, &strFileHash, 32);
  729. #if defined(_MSC_VER)
  730. HANDLE hFile;
  731. hFile = CreateFile(m_adapterInfo.adapterFilePath.GetData(), 0, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  732. if (hFile == INVALID_HANDLE_VALUE) {
  733. fileSize = 0;
  734. } else {
  735. fileSize = GetFileSize(hFile, NULL);
  736. CloseHandle(hFile);
  737. }
  738. #else
  739. //calculate file size
  740. struct stat statbuf;
  741. ZeroMemory(&statbuf, sizeof(statbuf));
  742. stat(m_adapterInfo.adapterFilePath.GetData(), &statbuf);
  743. fileSize = statbuf.st_size;
  744. #endif //_MSC_VER
  745. } else {
  746. memset(strFileHash, '\0', sizeof(char) * 128);
  747. }
  748. //necessary elements about adapter
  749. CSimpleStringA csPortNum(true), csCheckData(true), csKeySN(true), csInOutDir(true);
  750. termInfo["VendorDllName"] = m_adapterInfo.adapterFilePath.GetData();
  751. termInfo["VendorDllFileHash"] = strFileHash;
  752. ZeroMemory(strFileHash, 128);
  753. termInfo["VendorDllFileSize"] = _itoa(fileSize, strFileHash, 10);
  754. termInfo["szModel"] = m_adapterInfo.devCatInfo.szModel;
  755. termInfo["szType"] = m_adapterInfo.devCatInfo.szType;
  756. termInfo["Port"] = m_adapterInfo.strPort.GetData();
  757. termInfo["Baudrate"] = m_adapterInfo.strBaudrate.GetData();
  758. if (strFileHash != nullptr) {
  759. delete[] strFileHash;
  760. strFileHash = nullptr;
  761. }
  762. if (!entityName.Compare("Gpio")) {
  763. termInfo["PortNum"] = m_adapterInfo.strPortNum.GetData();
  764. termInfo["InOutDir"] = m_adapterInfo.strInOutDir.GetData();
  765. }
  766. else if (!entityName.Compare("PinPad")) {
  767. termInfo["CheckData"] = m_adapterInfo.strCheckdata.GetData();
  768. termInfo["Keysn"] = m_adapterInfo.strKeysn.GetData();
  769. }
  770. if (!additionalParams.empty()) {
  771. for (auto it = additionalParams.cbegin(); it != additionalParams.cend(); ++it) {
  772. termInfo[it->first.c_str()] = it->second;
  773. }
  774. }
  775. std::pair<bool, std::string> strResult;
  776. strResult = generateJsonStr(termInfo);
  777. strResult.second.c_str();
  778. CSmartPointer<IConfigInfo> spConfigRun;
  779. CSimpleStringA csWarnMsg;
  780. this->GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfigRun);
  781. spConfigRun->ReadConfigValue("Run", "WarnMsg", csWarnMsg);
  782. SYSTEMTIME localTime;
  783. GetLocalTime(&localTime);
  784. if (csWarnMsg.IsNullOrEmpty() || csWarnMsg.Compare(strResult.second.c_str()) != 0) {
  785. spConfigRun->WriteConfigValueInt("Run", "WarnDay", localTime.wDay);
  786. spConfigRun->WriteConfigValue("Run", "WarnMsg", strResult.second.c_str());
  787. if (!entityName.IsNullOrEmpty()) {
  788. LogWarn(Severity_Low, Error_Unexpect, m_WarnCodeMap[entityName.GetData()], strResult.second.c_str());
  789. bUpload = true;
  790. } else {
  791. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Entity Name is empty, please check!!!");
  792. }
  793. } else {
  794. int wDay = 99;
  795. spConfigRun->ReadConfigValueInt("Run", "WarnDay", wDay);
  796. if (wDay != localTime.wDay) {
  797. spConfigRun->WriteConfigValueInt("Run", "WarnDay", localTime.wDay);
  798. if (!entityName.IsNullOrEmpty()) {
  799. LogWarn(Severity_Low, Error_Unexpect, m_WarnCodeMap[entityName.GetData()], strResult.second.c_str());
  800. bUpload = true;
  801. } else {
  802. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Entity Name is empty, please check!!!");
  803. }
  804. }
  805. }
  806. return bUpload;
  807. }
  808. void ResetRepeatErrTimes()
  809. {
  810. m_contiErrTimes = 0;
  811. }
  812. ErrorCodeEnum TransECWithRepeat(const ErrorCodeEnum& ec)
  813. {
  814. ErrorCodeEnum result(ec);
  815. if (ec != Error_Succeed) {
  816. if (!m_bOpened) {
  817. result = Error_DevNotAvailable;
  818. } else if (0 < CountDevError(this->GetEntityBase())) {
  819. result = Error_DevFailAddUp;
  820. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER)("exceed max allow repeat error times! 0x%x -> 0x%x", ec, result);
  821. }
  822. }
  823. return result;
  824. }
  825. virtual ErrorCodeEnum PreOpenDevice() { return Error_Succeed; }
  826. virtual ErrorCodeEnum ToOpenDevice() { return Error_NotImpl; }
  827. virtual ErrorCodeEnum PostOpenDevice() { return Error_Succeed; }
  828. ErrorCodeEnum OpenDevice()
  829. {
  830. if (m_bOpening) {
  831. return Error_Pending;
  832. }
  833. if (m_bOpened) {
  834. return Error_DevAlreadyConnected;
  835. }
  836. m_bOpening = true;
  837. ErrorCodeEnum result = PreOpenDevice();
  838. if (IS_SUCCEED(result)) {
  839. result = ToOpenDevice();
  840. if (IS_SUCCEED(result)) {
  841. m_bOpened = true;
  842. result = PostOpenDevice();
  843. }
  844. }
  845. m_bOpening = false;
  846. return result;
  847. }
  848. public:
  849. bool m_bOpened;
  850. bool m_bOpening;
  851. protected:
  852. DevAdptLibHelper<TDevClass> m_hDevHelper;
  853. DevEntityErrorCode m_entCode;
  854. BOOL m_FirstStart;
  855. ErrorPackage m_errPkg;
  856. CSimpleStringA m_csAlarmMsg;
  857. unsigned int m_contiErrTimes;
  858. AdapterInfo m_adapterInfo;
  859. void UpdateEntityIDIfZero()
  860. {
  861. if (m_entCode.dwEntityId == 0) {
  862. UpdateEntityID();
  863. }
  864. }
  865. void UpdateEntityID()
  866. {
  867. CEntityStaticInfo esi = { 0 };
  868. ErrorCodeEnum ec = this->GetEntityBase()->GetFunction()->GetEntityStaticInfo(this->GetEntityBase()->GetEntityName(), esi);
  869. if (ec == Error_Succeed) {
  870. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("wEntityDevelopID: 0x%X", esi.wEntityDevelopID);
  871. m_entCode.ResetWithEntityID(esi.wEntityDevelopID);
  872. }
  873. }
  874. DWORD GetEntityID()
  875. {
  876. UpdateEntityIDIfZero();
  877. return m_entCode.dwEntityId;
  878. }
  879. DWORD UpdateDEC(DWORD dwVal = 0)
  880. {
  881. UpdateEntityIDIfZero();
  882. /** 14 is for entity define, FC(6bit) is reserved*/
  883. int reserved = (dwVal & 0xFC000) >> 14;
  884. m_entCode.SetDevCode(dwVal, reserved);
  885. return GetDEC();
  886. }
  887. DWORD GetDEC() const
  888. {
  889. return m_entCode.GetCompleteErrorCode();
  890. }
  891. DWORD GetAlarmDEC(DWORD dwValue = 0)
  892. {
  893. if (dwValue > 0)
  894. UpdateDEC(dwValue);
  895. //if have been set ErrorCode of entity defined (not device),use it directly
  896. DWORD dwCode = 0;
  897. //if ((m_entCode.dwVendorErroCode >> 20) == m_entCode.dwEntityId)
  898. if(IsEntityIDSettle(m_entCode.dwVendorErroCode))
  899. dwCode = m_entCode.dwVendorErroCode;
  900. else
  901. dwCode = HARDWARE_ENTITY_MAKE_ERRORCODE_TO_BUSINESS(m_entCode);
  902. return dwCode;
  903. }
  904. DWORD AlarmDECToBusiness(DWORD dwVal = 0)
  905. {
  906. if (dwVal > 0) {
  907. UpdateDEC(dwVal);
  908. }
  909. //oilyang@20200528 according the meeting result,throw a LogError while answering ctx
  910. //oilyang@20200525 if have been set ErrorCode of entity defined (not device),use it directly
  911. const DWORD dwCode = IsEntityIDSettle(m_entCode.dwVendorErroCode)
  912. ? m_entCode.dwVendorErroCode : m_entCode.GetCompleteErrorCode2Business();
  913. ///**TODO(Gifur@9/10/2021): */
  914. //LogWarn(Severity_Middle, Error_NotImpl, dwCode, CSimpleStringA::Format("%s TODO:", __FUNCTION__));
  915. return dwCode;
  916. }
  917. void ClearRelatedDEC()
  918. {
  919. UpdateDEC();
  920. m_csAlarmMsg = "No more information";
  921. }
  922. bool IsEntityIDSettle(const DWORD& dwVal) const
  923. {
  924. return ((dwVal >> 20) == m_entCode.dwEntityId);
  925. }
  926. void LogErrMsg(const char* pMsgHead, ErrorCodeEnum eErrCode, DWORD defaultDevCode = 0, BOOL bAlarm = FALSE);
  927. void LogErrMsgEx(const ErrorPackage& errPkg, const char* pMsgBody, DWORD finalErrCode /*= 0*/, BOOL bToBusiness = FALSE);
  928. static void GetFileSizeAndCalcHashValue(const char* fileName, int& fileSize, BYTE fileHash[32])
  929. {
  930. HANDLE hFile;
  931. hFile = CreateFile(fileName, 0, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  932. if (hFile == INVALID_HANDLE_VALUE) {
  933. fileSize = 0;
  934. return;
  935. }
  936. fileSize = GetFileSize(hFile, NULL);
  937. CloseHandle(hFile);
  938. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to calc vendor dll hash");
  939. SM3File(const_cast<char*>(fileName), fileHash);
  940. return;
  941. }
  942. int CountDevError(CEntityBase *pEntityBase);
  943. private:
  944. std::map<std::string, DWORD> m_WarnCodeMap;
  945. std::map<std::string, DWORD> m_RootIniCodeMap;
  946. };
  947. template<class TFSM, class TDevClass>
  948. inline ErrorCodeEnum CCommDevFSM<TFSM, TDevClass>::GetAndSplitDevErrInfo(
  949. CSimpleStringA& csErrMsg, WORD& wdDevErrCode, LPCTSTR lpszFuncNameForDisplay)
  950. {
  951. csErrMsg = "";
  952. wdDevErrCode = 0;
  953. BOOL bDisplayFunName = TRUE;
  954. if (lpszFuncNameForDisplay == NULL || strlen(lpszFuncNameForDisplay) == 0) {
  955. bDisplayFunName = FALSE;
  956. }
  957. if (!m_hDevHelper) {
  958. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("inst occurs nullptr !!!");
  959. return Error_Param;
  960. }
  961. DevErrorInfo devErrInfo;
  962. ZeroMemory(&devErrInfo, sizeof(DevErrorInfo));
  963. ErrorCodeEnum erroCode = m_hDevHelper->GetLastErr(devErrInfo);
  964. if (erroCode == Error_Succeed)
  965. {
  966. if (strlen(devErrInfo.szErrMsg) > 0) {
  967. csErrMsg = devErrInfo.szErrMsg;
  968. }
  969. if (devErrInfo.dwErrMsgLen > MAX_DEV_ERROR_MSG_LEN) {
  970. wdDevErrCode = (WORD)((devErrInfo.dwErrMsgLen >> 16) & 0x0000FFFF);
  971. }
  972. if (bDisplayFunName) {
  973. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Invoke <%s> failed, Dev_GLE: DevErrCode[%d], ErrMsg[%s]",
  974. lpszFuncNameForDisplay, wdDevErrCode, (LPCTSTR)csErrMsg);
  975. } else {
  976. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Dev_GLE: DevErrCode[%d], ErrMsg[%s]", wdDevErrCode, (LPCTSTR)csErrMsg);
  977. }
  978. } else {
  979. if (bDisplayFunName) {
  980. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Invoke <%s> failed, and unfortunately Dev_GLE failed returned EC: %s",
  981. lpszFuncNameForDisplay, SpStrError(erroCode));
  982. csErrMsg = CSimpleStringA::Format("Invoke <%s> failed", lpszFuncNameForDisplay);
  983. } else {
  984. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Dev_GLE failed returned EC: %s", SpStrError(erroCode));
  985. }
  986. }
  987. /** reset vendor device error code to stop reporting to alarm [12/11/2020 Gifur] */
  988. wdDevErrCode = 0;
  989. return erroCode;
  990. }
  991. template<class TFSM, class TDevClass>
  992. inline void CCommDevFSM<TFSM, TDevClass>::LogErrMsg(
  993. const char* pMsgHead, ErrorCodeEnum eErrCode, DWORD defaultDevCode, BOOL bAlarm)
  994. {
  995. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("%s failed, EC = %s", pMsgHead, SpStrError(eErrCode));
  996. WORD wdErrCode = 0;
  997. CSimpleStringA csErrMsg(true);
  998. ErrorCodeEnum ec = GetAndSplitDevErrInfo(csErrMsg, wdErrCode);
  999. if (IsEntityIDSettle(defaultDevCode)) {
  1000. UpdateDEC(defaultDevCode);
  1001. } else if (ec == Error_Succeed && wdErrCode != 0) {
  1002. UpdateDEC(wdErrCode);
  1003. } else if (defaultDevCode != 0) {
  1004. UpdateDEC(defaultDevCode);
  1005. }
  1006. m_csAlarmMsg = CSimpleStringA::Format("%s failed EC= %s : %s",
  1007. pMsgHead, SpStrError(eErrCode), (LPCTSTR)csErrMsg);
  1008. if (bAlarm) {
  1009. LogWarn(Severity_Middle, eErrCode, AlarmDECToBusiness(), (LPCTSTR)m_csAlarmMsg);
  1010. }
  1011. return;
  1012. }
  1013. template<class TFSM, class TDevClass>
  1014. inline void CCommDevFSM<TFSM, TDevClass>::LogErrMsgEx(const ErrorPackage& errPkg, const char* pMsgBody, DWORD finalErrCode /*= 0*/, BOOL bToBusiness)
  1015. {
  1016. const CSimpleStringA alarmMsg = CSimpleStringA::Format("{\"DevSN\":\"%s\", \"Prefix\":\"%s\", \"Code\":\"%s\", \"Msg\":\"%s\"}"
  1017. , errPkg.devSN.GetData(), errPkg.msgHead.GetData(), SpStrError(errPkg.devErrCode), pMsgBody);
  1018. if (bToBusiness) {
  1019. LogError(Severity_High, errPkg.devErrCode, finalErrCode, alarmMsg.GetData());
  1020. } else {
  1021. LogWarn(Severity_High, errPkg.devErrCode, finalErrCode, alarmMsg.GetData());
  1022. }
  1023. return;
  1024. }
  1025. //1:报错累计到上限,需要返回错误码给业务
  1026. //0:报错累计未到上限,或者不需要计数
  1027. //-1:出现错误
  1028. template<class TFSM, class TDevClass>
  1029. int CCommDevFSM<TFSM, TDevClass>::CountDevError(CEntityBase* pEntityBase)
  1030. {
  1031. static int iErrNumLimit = -1;
  1032. if (iErrNumLimit == -1) {
  1033. CSmartPointer<IConfigInfo> spCenterConfig;
  1034. ErrorCodeEnum eErrDev = pEntityBase->GetFunction()->OpenConfig(Config_CenterSetting, spCenterConfig);
  1035. if (eErrDev != Error_Succeed) {
  1036. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("open centersetting file failed!");
  1037. return -1;
  1038. } else {
  1039. int value(0);
  1040. spCenterConfig->ReadConfigValueInt("Common", "ErrUpperLimit", value);
  1041. if (0 == value) {
  1042. iErrNumLimit = 5;
  1043. } else if (0 > value) {
  1044. iErrNumLimit = 0;
  1045. } else {
  1046. iErrNumLimit = value;
  1047. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("iErrNumLimit=%d", iErrNumLimit);
  1048. }
  1049. }
  1050. }
  1051. m_contiErrTimes++;
  1052. if (iErrNumLimit > 0 && m_contiErrTimes >= iErrNumLimit) {
  1053. return 1;
  1054. } else {
  1055. return 0;
  1056. }
  1057. }
  1058. #endif /*_COMM_DEVICE_FSM_BASE_HPP_*/