IDCertFSM.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. #ifndef IDCERTFSM_H
  2. #define IDCERTFSM_H
  3. #pragma once
  4. #include "SpFSM.h"
  5. #include "SpTest.h"
  6. enum EvtType
  7. {
  8. USER_EVT_TEST = EVT_USER+1,
  9. USER_EVT_QUIT,
  10. USER_EVT_READ,
  11. USER_EVT_CANCEL_READ,
  12. USER_EVT_READ_FINISHED,
  13. USER_EVT_EXIT,
  14. USER_EVT_GET_DEVINFO,
  15. USER_EVT_ERROR,
  16. USER_EVT_READ_EX,
  17. USER_EVT_READ_EX_FINISHED,
  18. USER_EVT_EJECT,
  19. USER_EVT_WAIT_FETCH_IDCARD_FINISHED,
  20. USER_EVT_READ_AND_SCAN,
  21. USER_EVT_READ_AND_SCAN_FINISHED,
  22. USER_EVT_INIT_FINISHED,
  23. };
  24. enum BmpType
  25. {
  26. Bmp_ZP = 1,
  27. Bmp_SCAN = 2,
  28. };
  29. #include "IDCerClass.h"
  30. #include "IDCertificate_def_g.h"
  31. #pragma region forsonar
  32. typedef IDCertificate::IDCertService_Read_Req IDCert_Read_Req;
  33. typedef IDCertificate::IDCertService_Read_Ans IDCert_Read_Ans;
  34. typedef IDCertificate::IDCertService_CancelRead_Info IDCert_CancelRead_Info;
  35. typedef IDCertificate::IDCertService_ReadWaitMore_Info IDCert_ReadWaitMore_Info;
  36. typedef IDCertificate::IDCertService_Exit_Info IDCert_Exit_Info;
  37. typedef IDCertificate::IDCertService_ReadEx_Req IDCert_ReadEx_Req;
  38. typedef IDCertificate::IDCertService_ReadEx_Ans IDCert_ReadEx_Ans;
  39. typedef IDCertificate::IDCertService_Eject_Req IDCert_Eject_Req;
  40. typedef IDCertificate::IDCertService_Eject_Ans IDCert_Eject_Ans;
  41. typedef IDCertificate::IDCertService_ReadAndScan_Req IDCert_ReadAndScan_Req;
  42. typedef IDCertificate::IDCertService_ReadAndScan_Ans IDCert_ReadAndScan_Ans;
  43. typedef IDCertificate::IDCertService_CrossTermCall_Req IDCert_CrossTermCall_Req;
  44. typedef IDCertificate::IDCertService_CrossTermCall_Ans IDCert_CrossTermCall_Ans;
  45. typedef IDCertificate::IDCertService_CrossTermInvokeInfo_Info IDCert_CrossTermInvokeInfo_Info;
  46. typedef IDCertificate::IDCertService_GetDevInfo_Req IDCert_GetDevInfo_Req;
  47. typedef IDCertificate::IDCertService_GetDevInfo_Ans IDCert_GetDevInfo_Ans;
  48. #pragma endregion forsonar
  49. #include "mod_heartbeat/HeartBeat_client_g.h"
  50. using namespace HeartBeat;
  51. #include "DeviceCrossHelper.h"
  52. #include <map>
  53. using namespace std;
  54. typedef ErrorCodeEnum ( *lpCreateDevCom)(DeviceBaseClass *&baseObj);
  55. typedef ErrorCodeEnum ( *lpReleaseDevCom)(DeviceBaseClass *&pBaseObj);
  56. #include "DevErrorCode.h"
  57. #include "DevFSMCommBase.hpp"
  58. struct CtxInfo
  59. {
  60. int dataSize;
  61. LPVOID pCtx;
  62. };
  63. class ReadEvent : public FSMEvent
  64. {
  65. public:
  66. ReadEvent() : FSMEvent(USER_EVT_READ){}
  67. virtual ~ReadEvent(){}
  68. SpReqAnsContext<IDCert_Read_Req, IDCert_Read_Ans>::Pointer ctx;
  69. virtual void OnUnhandled()
  70. {
  71. if (ctx != NULL)
  72. ctx->Answer(Error_InvalidState);
  73. }
  74. };
  75. class CancelReadEvent : public FSMEvent
  76. {
  77. public:
  78. CancelReadEvent() : FSMEvent(USER_EVT_CANCEL_READ){}
  79. virtual ~CancelReadEvent(){}
  80. virtual void OnUnhandled()
  81. {
  82. LOG_TRACE("idcer cancel read not handled");
  83. }
  84. };
  85. class GetDevInfoEvent : public FSMEvent
  86. {
  87. public:
  88. GetDevInfoEvent() : FSMEvent(USER_EVT_GET_DEVINFO){}
  89. virtual ~GetDevInfoEvent(){}
  90. SpReqAnsContext<IDCert_GetDevInfo_Req, IDCert_GetDevInfo_Ans>::Pointer ctx;
  91. virtual void OnUnhandled()
  92. {
  93. if (ctx != NULL)
  94. ctx->Answer(Error_InvalidState);
  95. }
  96. };
  97. class ReadExEvent : public FSMEvent
  98. {
  99. public:
  100. ReadExEvent() : FSMEvent(USER_EVT_READ_EX){}
  101. virtual ~ReadExEvent(){}
  102. SpReqAnsContext<IDCert_ReadEx_Req, IDCert_ReadEx_Ans>::Pointer ctx;
  103. virtual void OnUnhandled()
  104. {
  105. if (ctx != NULL)
  106. ctx->Answer(Error_InvalidState);
  107. }
  108. };
  109. class EjectEvent : public FSMEvent
  110. {
  111. public:
  112. EjectEvent() : FSMEvent(USER_EVT_EJECT){}
  113. virtual ~EjectEvent(){}
  114. SpReqAnsContext<IDCert_Eject_Req, IDCert_Eject_Ans>::Pointer ctx;
  115. virtual void OnUnhandled()
  116. {
  117. if (ctx != NULL)
  118. ctx->Answer(Error_InvalidState);
  119. }
  120. };
  121. class ReadAndScanEvent : public FSMEvent
  122. {
  123. public:
  124. ReadAndScanEvent() : FSMEvent(USER_EVT_READ_AND_SCAN) {}
  125. virtual ~ReadAndScanEvent() {}
  126. SpReqAnsContext<IDCert_ReadAndScan_Req, IDCert_ReadAndScan_Ans>::Pointer ctx;
  127. virtual void OnUnhandled()
  128. {
  129. if (ctx != NULL)
  130. ctx->Answer(Error_InvalidState);
  131. }
  132. };
  133. class CIDCertFSM : public CCommDevFSM<CIDCertFSM, IDCerClass>
  134. {
  135. public:
  136. enum {s0,s1,s2,s3,s4,s5};
  137. //ON_ENTITYT_TEST();
  138. BEGIN_FSM_STATE(CIDCertFSM)
  139. FSM_STATE_ENTRY(s0,"Normal",s0_on_entry,s0_on_exit,s0_on_event)
  140. FSM_STATE_ENTRY(s1,"Reading",s1_on_entry,s1_on_exit,s1_on_event)
  141. FSM_STATE_ENTRY(s2,"Fail",s2_on_entry,s2_on_exit,s2_on_event)
  142. FSM_STATE_ENTRY(s3, "Eject", s3_on_entry, s3_on_exit, s3_on_event)
  143. FSM_STATE_ENTRY(s4, "WaitingFetch", s4_on_entry, s4_on_exit, s4_on_event)
  144. FSM_STATE_ENTRY(s5, "Init", s5_on_entry, s5_on_exit, s5_on_event)
  145. END_FSM_STATE()
  146. BEGIN_FSM_RULE(CIDCertFSM, s5)
  147. FSM_RULE_ENTRY(s0, s2, USER_EVT_ERROR, 0)
  148. FSM_RULE_ENTRY(s0, FSM_STATE_EXIT, USER_EVT_QUIT, 0)
  149. FSM_RULE_ENTRY(s0, s1, USER_EVT_READ, 0)
  150. FSM_RULE_ENTRY(s0, s1, USER_EVT_READ_EX, 0)
  151. FSM_RULE_ENTRY(s0, s1, USER_EVT_READ_AND_SCAN, 0)
  152. FSM_RULE_ENTRY(s1, s0, USER_EVT_READ_FINISHED, 0)
  153. FSM_RULE_ENTRY(s1, s2, USER_EVT_READ_FINISHED, 1)
  154. FSM_RULE_ENTRY(s1, s0, USER_EVT_READ_EX_FINISHED, 0)
  155. FSM_RULE_ENTRY(s1, s2, USER_EVT_READ_EX_FINISHED, 1)
  156. FSM_RULE_ENTRY(s1, s4, USER_EVT_READ_EX_FINISHED, 4)
  157. FSM_RULE_ENTRY(s1, s0, USER_EVT_READ_AND_SCAN_FINISHED, 0)
  158. FSM_RULE_ENTRY(s1, s2, USER_EVT_READ_AND_SCAN_FINISHED, 1)
  159. FSM_RULE_ENTRY(s1, s4, USER_EVT_READ_AND_SCAN_FINISHED, 4)
  160. FSM_RULE_ENTRY(s1, FSM_STATE_EXIT, USER_EVT_QUIT, 0)
  161. FSM_RULE_ENTRY(s4, s0, USER_EVT_WAIT_FETCH_IDCARD_FINISHED, 0)
  162. FSM_RULE_ENTRY(s5, s0, USER_EVT_INIT_FINISHED, 0)
  163. FSM_RULE_ENTRY(s5, s2, USER_EVT_INIT_FINISHED, 2)
  164. END_FSM_RULE()
  165. CIDCertFSM():m_devInit(false),m_bCancelRead(false),m_bReading(false),m_bWaitReadMore(false),
  166. m_bExit(false),m_testResult(Error_Succeed), m_csMachineType(""), m_csSite(""), m_terminalNo("")
  167. ,m_devVendor(""), m_devVer(""), m_devBatch("") , m_bRVCIL(false), m_pHBClient(nullptr){
  168. HARDWARE_ENTITY_RESET_ENTITYID(m_entCode, 0x201);
  169. };
  170. virtual ~CIDCertFSM() {};
  171. virtual ErrorCodeEnum OnInit();
  172. virtual ErrorCodeEnum OnExit();
  173. virtual void s0_on_entry();
  174. virtual void s0_on_exit();
  175. virtual unsigned int s0_on_event(FSMEvent* e);
  176. virtual void s1_on_entry();
  177. virtual void s1_on_exit();
  178. virtual unsigned int s1_on_event(FSMEvent* e);
  179. virtual void s2_on_entry();
  180. virtual void s2_on_exit();
  181. virtual unsigned int s2_on_event(FSMEvent* e);
  182. virtual void s3_on_entry();
  183. virtual void s3_on_exit();
  184. virtual unsigned int s3_on_event(FSMEvent* e);
  185. virtual void s4_on_entry();
  186. virtual void s4_on_exit();
  187. virtual unsigned int s4_on_event(FSMEvent* e);
  188. virtual void s5_on_entry();
  189. virtual void s5_on_exit();
  190. virtual unsigned int s5_on_event(FSMEvent* e);
  191. int ReadInfo(SpReqAnsContext<IDCert_Read_Req, IDCert_Read_Ans>::Pointer ctx);
  192. int ReadInfoEx(SpReqAnsContext<IDCert_ReadEx_Req, IDCert_ReadEx_Ans>::Pointer ctx);
  193. int ReadAndScan(SpReqAnsContext<IDCert_ReadAndScan_Req, IDCert_ReadAndScan_Ans>::Pointer ctx);
  194. int WaitFetchIDCard();
  195. void SetReadMore(){m_bWaitReadMore = true;}
  196. void SetExitFlag(){m_bExit = true;}
  197. bool GetReadFlag(){return m_bReading;}
  198. ErrorCodeEnum GetDevCatInfo(DevCategoryInfo &devInfo, CSimpleStringA& devType);
  199. void GetDevState(DevStateEnum &devState){ devState = m_devState; }
  200. int ReadHZCard(SpReqAnsContext<IDCert_ReadEx_Req, IDCert_ReadEx_Ans>::Pointer ctx);
  201. void DoExit(CSmartPointer<ITransactionContext> pTransactionContext)
  202. {
  203. pTransactionContext->SendAnswer(Error_Succeed);
  204. }
  205. int LocalCallHeartBeat(int method, CBlob &bbSend, bool bTwoWay = false);
  206. bool GetDevInitFlag() { return m_devInit; }
  207. bool IsRVCIL() { /*return m_bRVCIL;*/return false; }//oilyang@20190123 方案改了,这里暂时不需要了
  208. void CrossTermCall(SpReqAnsContext<IDCert_CrossTermCall_Req, IDCert_CrossTermCall_Ans>::Pointer ctx);
  209. template <class TReq,class TAns>
  210. void SaveCtx(int methodID, CSmartPointer<SpReqAnsContext<TReq, TAns> > ctx)
  211. {
  212. m_mapCtx[methodID].pCtx = static_cast<void*>(ctx.GetRawPointer());
  213. }
  214. void SaveCtxAA(int methodID, SpReqAnsContext<IDCert_ReadAndScan_Req, IDCert_ReadAndScan_Ans>::Pointer ctx)
  215. {
  216. m_readAndScanCtx = ctx;
  217. }
  218. LPVOID GetCtx(int methodID)
  219. {
  220. if (m_mapCtx.find(methodID) != m_mapCtx.end())
  221. return m_mapCtx[methodID].pCtx;
  222. else
  223. return NULL;
  224. }
  225. int Initial();
  226. void CrossTermInvokeInfo(SpOnewayCallContext<IDCert_CrossTermInvokeInfo_Info>::Pointer ctx);
  227. void SelfTest(EntityTestEnum eTestType,CSmartPointer<ITransactionContext> pTransactionContext);
  228. protected:
  229. ErrorCodeEnum GetVendorDllPath(CSimpleStringA &strPath);
  230. ErrorCodeEnum GetPngBlob(CBlob &data,bool bClear=false);
  231. ErrorCodeEnum GetPngBlobEx(CBlob &data, CSimpleStringA fileNamePrefix,bool bClear=false);
  232. //type:1,delete img about zp; type:2,delete img about scan ID;type:3,delete both zp and scan ID
  233. void DeleteZP(int type);
  234. //ErrorCodeEnum ConvertBmpToPng(CSimpleStringA &strBmpPath, CSimpleStringA &strPngPath);
  235. //Delete bmp file in dep directory, you should just convey fileName only without paths -Joseph
  236. ErrorCodeEnum DeleteFileIfExisted(LPCTSTR fileName);
  237. bool GetAttachedTerminal(CSimpleStringA &csTerm);
  238. DWORD UpdateDEC(DWORD dwVal = 0)
  239. {
  240. int reserved = 0;
  241. reserved = (dwVal & 0xFC000) >> 14;
  242. HARDWARE_ENTITY_SET_DEVCODE(m_entCode, dwVal, reserved);
  243. return GetDEC();
  244. }
  245. DWORD GetDEC() const
  246. {
  247. return HARDWARE_ENTITY_MAKE_ERRORCODE(m_entCode);
  248. }
  249. DWORD AlarmDEC() const {
  250. return GetDEC();
  251. }
  252. DWORD AlarmDECToBusiness(DWORD dwVal = 0) {
  253. if (dwVal > 0)
  254. UpdateDEC(dwVal);
  255. //oilyang@20200528 according the meeting result,throw a LogError while answering ctx
  256. //oilyang@20200525 if have been set ErrorCode of entity defined (not device),use it directly
  257. DWORD dwCode = 0;
  258. if ((m_entCode.dwVendorErroCode >> 20) == m_entCode.dwEntityId)
  259. dwCode = m_entCode.dwVendorErroCode;
  260. else
  261. dwCode = HARDWARE_ENTITY_MAKE_ERRORCODE_TO_BUSINESS(m_entCode);
  262. LogError(Severity_Middle, Error_Unexpect, dwCode, "");
  263. return dwCode;
  264. }
  265. void ClearRelatedDEC()
  266. {
  267. UpdateDEC();
  268. m_csAlarmMsg = "NO more information";
  269. }
  270. void LogErrMsg(const char *pMsgHead, ErrorCodeEnum eErrCode, DWORD defaultDevCode = 0, BOOL bAlarm = FALSE);
  271. protected:
  272. bool m_devInit,m_bCancelRead,m_bReading,m_bWaitReadMore,m_bExit,m_bRVCIL;
  273. ErrorCodeEnum m_testResult;
  274. DevCategoryInfo m_devCatInfo;
  275. DevStateEnum m_devState;
  276. CSimpleStringA m_csMachineType,m_csSite,m_terminalNo;
  277. CSimpleStringA m_devVendor, m_devVer, m_devBatch;
  278. map<int, CtxInfo> m_mapCtx;
  279. HeartBeatService_ClientBase* m_pHBClient;
  280. SpReqAnsContext<IDCert_ReadAndScan_Req, IDCert_ReadAndScan_Ans>::Pointer m_readAndScanCtx;
  281. DevEntityErrorCode m_entCode;
  282. CSimpleStringA m_csAlarmMsg;
  283. };
  284. struct ReadTask : public ITaskSp
  285. {
  286. CIDCertFSM* fsm;
  287. SpReqAnsContext<IDCert_Read_Req, IDCert_Read_Ans>::Pointer ctx;
  288. ReadTask(CIDCertFSM* f) : fsm(f) {}
  289. void Process()
  290. {
  291. FSMEvent *e = new FSMEvent(USER_EVT_READ_FINISHED);
  292. e->param1 = fsm->ReadInfo(ctx);
  293. fsm->PostEventFIFO(e);
  294. }
  295. };
  296. struct ReadExTask : public ITaskSp
  297. {
  298. CIDCertFSM* fsm;
  299. SpReqAnsContext<IDCert_ReadEx_Req, IDCert_ReadEx_Ans>::Pointer ctx;
  300. ReadExTask(CIDCertFSM* f) : fsm(f) {}
  301. void Process()
  302. {
  303. FSMEvent *e = new FSMEvent(USER_EVT_READ_EX_FINISHED);
  304. e->param1 = fsm->ReadInfoEx(ctx);
  305. fsm->PostEventFIFO(e);
  306. }
  307. };
  308. struct ReadAndScanTask : public ITaskSp
  309. {
  310. CIDCertFSM* fsm;
  311. SpReqAnsContext<IDCert_ReadAndScan_Req, IDCert_ReadAndScan_Ans>::Pointer ctx;
  312. ReadAndScanTask(CIDCertFSM* f) : fsm(f) {}
  313. void Process()
  314. {
  315. FSMEvent *e = new FSMEvent(USER_EVT_READ_AND_SCAN_FINISHED);
  316. e->param1 = fsm->ReadAndScan(ctx);
  317. fsm->PostEventFIFO(e);
  318. }
  319. };
  320. struct WaitFetchIDCardTask : public ITaskSp
  321. {
  322. CIDCertFSM* fsm;
  323. WaitFetchIDCardTask(CIDCertFSM* f) : fsm(f) {}
  324. void Process()
  325. {
  326. FSMEvent *e = new FSMEvent(USER_EVT_WAIT_FETCH_IDCARD_FINISHED);
  327. e->param1 = fsm->WaitFetchIDCard();
  328. fsm->PostEventFIFO(e);
  329. }
  330. };
  331. struct InitTask : public ITaskSp
  332. {
  333. CIDCertFSM* fsm;
  334. InitTask(CIDCertFSM* f) : fsm(f) {}
  335. void Process()
  336. {
  337. FSMEvent* e = new FSMEvent(USER_EVT_INIT_FINISHED);
  338. e->param1 = fsm->Initial();
  339. fsm->PostEventFIFO(e);
  340. }
  341. };
  342. #endif //IDCERTFSM_H