PinPadFSM.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. #ifndef __PINPAD_FSM_H
  2. #define __PINPAD_FSM_H
  3. #pragma once
  4. #include "SpFSM.h"
  5. #include "toolkit.h"
  6. #include "SpUtility.h"
  7. #include "DevFSMCommBase.hpp"
  8. #include "CommEntityUtil.hpp"
  9. #include "PinPad_UserErrorCode.h"
  10. #include "json/json.h"
  11. enum EvtType
  12. {
  13. USER_EVT_TEST = EVT_USER+1,
  14. USER_EVT_ANY_INPUT_TIMER,
  15. USER_EVT_QUIT,
  16. USER_EVT_INIT,
  17. USER_EVT_INITFINISHED,
  18. USER_EVT_SETACCOUNT,
  19. USER_EVT_SETACCOUNTFINISHED,
  20. USER_EVT_INPUTCANCEL,
  21. USER_EVT_INPUTWAITINGMORE,
  22. USER_EVT_EXIT,
  23. USER_EVT_GETINPUT_SM,
  24. USER_EVT_GETINPUT_SM_FINISHED,
  25. USER_EVT_LOADKEY_SM,
  26. USER_EVT_LOADKEY_SM_FINISHED,
  27. USER_EVT_ENCRYPT_SM,
  28. USER_EVT_ENCRYPT_SM_FINISHED,
  29. USER_EVT_GET_CHECKCODE,
  30. USER_EVT_GET_CHECKCODE_FINISHED,
  31. USER_EVT_TODO_INIT,
  32. USER_EVT_TODO_INIT_FINISHED,
  33. USER_EVT_GETINPUT_JS,
  34. USER_EVT_GETINPUT_JS_FINISHED,
  35. };
  36. #include "PinPad_server_g.h"
  37. #include "PinPad_msg_g.h"
  38. #include "PinPadClass.h"
  39. using namespace PinPad;
  40. #include <map>
  41. using namespace std;
  42. class CPinPadEntity;
  43. class CPinPadFSM;
  44. unsigned int __stdcall DoWork(void* pData);
  45. class PinPadInitFinishedEvent : public FSMEvent
  46. {
  47. public:
  48. PinPadInitFinishedEvent() : FSMEvent(USER_EVT_INITFINISHED){}
  49. ~PinPadInitFinishedEvent(){}
  50. };
  51. class InputCancelEvent : public FSMEvent
  52. {
  53. public:
  54. InputCancelEvent() : FSMEvent(USER_EVT_INPUTCANCEL){}
  55. ~InputCancelEvent(){}
  56. };
  57. class InputWaitingMoreEvent : public FSMEvent
  58. {
  59. public:
  60. InputWaitingMoreEvent() : FSMEvent(USER_EVT_INPUTWAITINGMORE){}
  61. ~InputWaitingMoreEvent(){}
  62. };
  63. class GetInputSMEvent : public FSMEvent
  64. {
  65. public:
  66. GetInputSMEvent() : FSMEvent(USER_EVT_GETINPUT_SM){}
  67. ~GetInputSMEvent(){}
  68. SpReqAnsContext<PinPadService_GetInputSM_Req, PinPadService_GetInputSM_Ans>::Pointer ctx;
  69. virtual void OnUnhandled()
  70. {
  71. if (ctx != NULL) {
  72. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI("GetInputSMEvent")("InvalidState");
  73. ctx->Answer(Error_InvalidState);
  74. }
  75. }
  76. };
  77. class GetInputJSEvent : public FSMEvent
  78. {
  79. public:
  80. GetInputJSEvent() : FSMEvent(USER_EVT_GETINPUT_JS) {}
  81. ~GetInputJSEvent() {}
  82. SpReqAnsContext<PinPadService_GetInputJS_Req, PinPadService_GetInputJS_Ans>::Pointer ctx;
  83. virtual void OnUnhandled()
  84. {
  85. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("InvalidState");
  86. }
  87. };
  88. class LoadKeySMEvent : public FSMEvent
  89. {
  90. public:
  91. LoadKeySMEvent() : FSMEvent(USER_EVT_LOADKEY_SM){}
  92. ~LoadKeySMEvent(){}
  93. SpReqAnsContext<PinPadService_LoadKeysSM_Req, PinPadService_LoadKeysSM_Ans>::Pointer ctx;
  94. virtual void OnUnhandled()
  95. {
  96. if (ctx != NULL) {
  97. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI("LoadKeySMEvent")("InvalidState");
  98. ctx->Answer(Error_InvalidState);
  99. }
  100. }
  101. };
  102. class EncryptSMEvent : public FSMEvent
  103. {
  104. public:
  105. EncryptSMEvent() : FSMEvent(USER_EVT_ENCRYPT_SM){}
  106. ~EncryptSMEvent(){}
  107. SpReqAnsContext<PinPadService_EncryptDataSM_Req, PinPadService_EncryptDataSM_Ans>::Pointer ctx;
  108. virtual void OnUnhandled()
  109. {
  110. if (ctx != NULL) {
  111. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI("EncryptSMEvent")("InvalidState");
  112. ctx->Answer(Error_InvalidState);
  113. }
  114. }
  115. };
  116. class GetCheckCodeEvent : public FSMEvent
  117. {
  118. public:
  119. GetCheckCodeEvent() : FSMEvent(USER_EVT_GET_CHECKCODE){}
  120. ~GetCheckCodeEvent(){}
  121. SpReqAnsContext<PinPadService_GetCheckCode_Req, PinPadService_GetCheckCode_Ans>::Pointer ctx;
  122. virtual void OnUnhandled()
  123. {
  124. if (ctx != NULL) {
  125. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI("GetCheckCodeEvent")("InvalidState");
  126. ctx->Answer(Error_InvalidState);
  127. }
  128. }
  129. };
  130. class CPinPadFSM : public CCommDevFSM<CPinPadFSM, PinPadClass>
  131. {
  132. public:
  133. enum {s0,s1,s2,s3,s4,s5,s6};
  134. BEGIN_FSM_STATE(CPinPadFSM)
  135. FSM_STATE_ENTRY(s0, "Init", s0_on_entry, s0_on_exit, s0_on_event)
  136. FSM_STATE_ENTRY(s1, "Initializing", s1_on_entry, s1_on_exit, s1_on_event)
  137. FSM_STATE_ENTRY(s2, "Idle", s2_on_entry, s2_on_exit, s2_on_event)
  138. FSM_STATE_ENTRY(s3, "Input", s3_on_entry, s3_on_exit, s3_on_event)
  139. FSM_STATE_ENTRY(s4, "AccessAuth", s4_on_entry, s4_on_exit, s4_on_event)
  140. FSM_STATE_ENTRY(s5, "Failed", s5_on_entry, s5_on_exit, s5_on_event)
  141. FSM_STATE_ENTRY(s6, "InitFailed", s6_on_entry, s6_on_exit, s6_on_event)
  142. END_FSM_STATE()
  143. BEGIN_FSM_RULE(CPinPadFSM, s0)
  144. FSM_RULE_ENTRY(s0, s1, USER_EVT_INIT, 0)
  145. FSM_RULE_ENTRY(s1, s2, USER_EVT_INITFINISHED, 0)
  146. FSM_RULE_ENTRY(s1, s6, USER_EVT_INITFINISHED, 1)
  147. FSM_RULE_ENTRY(s2, s3, USER_EVT_GETINPUT_SM, 0)
  148. FSM_RULE_ENTRY(s2, s3, USER_EVT_GETINPUT_JS, 0)
  149. FSM_RULE_ENTRY(s2, s4, USER_EVT_LOADKEY_SM, 2)
  150. FSM_RULE_ENTRY(s2, s4, USER_EVT_ENCRYPT_SM, 2)
  151. FSM_RULE_ENTRY(s2, s0, USER_EVT_TODO_INIT_FINISHED, 0)
  152. FSM_RULE_ENTRY(s3, s2, USER_EVT_GETINPUT_SM_FINISHED, 0)
  153. FSM_RULE_ENTRY(s3, s2, USER_EVT_GETINPUT_JS_FINISHED, 0)
  154. FSM_RULE_ENTRY(s3, s2, USER_EVT_EXIT, 0)
  155. FSM_RULE_ENTRY(s4, s2, USER_EVT_LOADKEY_SM_FINISHED, 0)
  156. FSM_RULE_ENTRY(s4, s2, USER_EVT_ENCRYPT_SM_FINISHED, 0)
  157. FSM_RULE_ENTRY(s5, s0, USER_EVT_TODO_INIT_FINISHED, 0)
  158. FSM_RULE_ENTRY(s6, s0, USER_EVT_TODO_INIT_FINISHED, 0)
  159. END_FSM_RULE()
  160. CPinPadFSM() : m_bFrontCancel(false),
  161. m_bWaitingMore(false), m_bExit(false), m_bPlainPin(true),
  162. m_bEntityExit(false), m_bPinInput(false), m_bLoadKey(false), m_bEncrypt(false)
  163. , m_dwDevCommFailCount(0), m_dwPinPadRunCount(0), m_eDevState(DEVICE_STATUS_NOT_READY), m_encryptkey(1)
  164. , m_bSM(false), m_bSMLoaded(false), m_iInWhatPage(PageType_Init), m_szModel(""), m_szType("")
  165. , m_szVendor(""),m_csMachineType(true), m_csSite(true), m_terminalNo(true), m_port(true), m_Baudrate(true)
  166. , m_bPinInputJS(false), m_bUseBackupSN(true){
  167. HARDWARE_ENTITY_RESET_ENTITYID(m_entCode, 0x206);
  168. ZeroMemory(&m_adapterInfo, sizeof(m_adapterInfo));
  169. }
  170. ~CPinPadFSM(){
  171. m_bEntityExit = true;
  172. }
  173. virtual ErrorCodeEnum OnInit();
  174. virtual ErrorCodeEnum OnExit();
  175. //void DoWork();
  176. void s0_on_entry();
  177. void s0_on_exit();
  178. unsigned int s0_on_event(FSMEvent* event);
  179. void s1_on_entry();
  180. void s1_on_exit();
  181. unsigned int s1_on_event(FSMEvent* event);
  182. void s2_on_entry();
  183. void s2_on_exit();
  184. unsigned int s2_on_event(FSMEvent* event);
  185. void s3_on_entry();
  186. void s3_on_exit();
  187. unsigned int s3_on_event(FSMEvent* event);
  188. void s4_on_entry();
  189. void s4_on_exit();
  190. unsigned int s4_on_event(FSMEvent* event);
  191. void s5_on_entry();
  192. void s5_on_exit();
  193. unsigned int s5_on_event(FSMEvent* event);
  194. void s6_on_entry();
  195. void s6_on_exit();
  196. unsigned int s6_on_event(FSMEvent* event);
  197. int Initial();
  198. void push_char(char *buf, int *len, int c);
  199. void pop_char(char *buf, int *len,bool bClear=true);
  200. void clear_char(char *buf, int *len);
  201. bool Get12Account(char *szAcc);
  202. int GetInput(SpReqAnsContext<PinPadService_GetInputSM_Req, PinPadService_GetInputSM_Ans>::Pointer ctxSM);
  203. ErrorCodeEnum GetEncryptText(SpReqAnsContext<PinPadService_GetInputSM_Req, PinPadService_GetInputSM_Ans>::Pointer ctxSM);
  204. bool GetDevInitFlag(){ return m_bOpened;}
  205. bool GetDeviceOpeningFlag() { return m_bOpening; }
  206. void SetExitFlag(){m_bExit = true;}
  207. bool GetEntityExit() { return m_bEntityExit;}
  208. void SetCtx(SpReqAnsContext<PinPadService_GetInputSM_Req, PinPadService_GetInputSM_Ans>::Pointer ctxSM)
  209. {
  210. m_inputSMCtx = ctxSM;
  211. m_bPinInput = true;
  212. }
  213. bool GetCtx(SpReqAnsContext<PinPadService_GetInputSM_Req, PinPadService_GetInputSM_Ans>::Pointer& ctxSM)
  214. {
  215. if (m_inputSMCtx == NULL)
  216. {
  217. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("ctxSM is null");
  218. return false;
  219. }
  220. ctxSM = m_inputSMCtx;
  221. return true;
  222. }
  223. void SetJSCtx(SpReqAnsContext<PinPadService_GetInputJS_Req, PinPadService_GetInputJS_Ans>::Pointer ctxJS)
  224. {
  225. m_inputJSCtx = ctxJS;
  226. m_bPinInputJS = true;
  227. }
  228. bool GetJSCtx(SpReqAnsContext<PinPadService_GetInputJS_Req, PinPadService_GetInputJS_Ans>::Pointer& ctxJS)
  229. {
  230. if (m_inputJSCtx == NULL)
  231. {
  232. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("ctxJS is null");
  233. return false;
  234. }
  235. ctxJS = m_inputJSCtx;
  236. return true;
  237. }
  238. int GetInputJS(SpReqAnsContext<PinPadService_GetInputJS_Req, PinPadService_GetInputJS_Ans>::Pointer ctxJS);
  239. ErrorCodeEnum GetEncryptTextJS(SpReqAnsContext<PinPadService_GetInputJS_Req, PinPadService_GetInputJS_Ans>::Pointer ctxJS);
  240. ErrorCodeEnum GetDevInfo(DevCategoryInfo &devInfo);
  241. int LoadKeySM(SpReqAnsContext<PinPadService_LoadKeysSM_Req, PinPadService_LoadKeysSM_Ans>::Pointer ctx);
  242. int EncryptSM(SpReqAnsContext<PinPadService_EncryptDataSM_Req, PinPadService_EncryptDataSM_Ans>::Pointer ctx);
  243. ErrorCodeEnum GetCheckCode(SpReqAnsContext<PinPadService_GetCheckCode_Req, PinPadService_GetCheckCode_Ans>::Pointer ctx);
  244. int GetEncryptKey(int &devFunc,int &loadFunc)
  245. {
  246. devFunc = loadFunc = 0;
  247. devFunc = m_encryptkey;
  248. int tmp = 0;
  249. if (m_bSMLoaded)
  250. tmp += 2;
  251. if (m_bSMLoaded)
  252. loadFunc = 2;
  253. else
  254. loadFunc = tmp;
  255. return 0;
  256. }
  257. void SetInWhatPage(int iPageType);
  258. void SelfTest(EntityTestEnum eTestType,CSmartPointer<ITransactionContext> pTransactionContext);
  259. void ToLogWarnInfoAboutTermCustom();
  260. bool IsInGetInputJS() { return m_bPinInputJS; }
  261. public:
  262. SP::Toolkit::CConditionVarPlus m_hInputConVar;
  263. SP::Toolkit::CConditionVarPlus m_hInitConVar;
  264. private:
  265. SpReqAnsContext<PinPadService_GetInputSM_Req, PinPadService_GetInputSM_Ans>::Pointer m_inputSMCtx;
  266. SpReqAnsContext<PinPadService_GetInputJS_Req, PinPadService_GetInputJS_Ans>::Pointer m_inputJSCtx;
  267. char m_szAccount[MAX_ACCOUNT_LEN];
  268. bool m_bFrontCancel,m_bWaitingMore,m_bExit,m_bPlainPin,m_bEntityExit
  269. ,m_bPinInput,m_bLoadKey,m_bEncrypt,m_bSM,m_bSMLoaded, m_bPinInputJS, m_bUseBackupSN/*if use backup sn*/;
  270. ULLINT m_ullBeginTime, m_ullEndTime,m_ullConnectCost;
  271. CSimpleStringA m_deviceNo, m_port, m_Baudrate, m_devCheckData,m_keySNSM,m_szModel,m_szType,m_szVendor;
  272. char m_buf[4];
  273. DWORD m_dwDevCommFailCount,m_dwPinPadRunCount;
  274. DevStateEnum m_eDevState;
  275. int m_encryptkey;
  276. int m_iInWhatPage/*在哪个页面,区分首页,用户桌面,其他页*/;
  277. CSimpleStringA m_csMachineType, m_csSite, m_terminalNo,m_lastCheckCode;
  278. CSystemStaticInfo m_sysStaticInfo;
  279. CSimpleStringA m_csAlarmMsg;
  280. bool IsInBusiness() { return (m_iInWhatPage == PageType_Other); }
  281. };
  282. struct InitTask : public ITaskSp
  283. {
  284. CPinPadFSM *fsm;
  285. InitTask(CPinPadFSM* f) : fsm(f) {}
  286. void Process()
  287. {
  288. FSMEvent *e = new FSMEvent(USER_EVT_INITFINISHED);
  289. e->param1 = fsm->Initial();
  290. fsm->PostEventFIFO(e);
  291. }
  292. };
  293. struct LoadKeySMTask : public ITaskSp
  294. {
  295. CPinPadFSM* fsm;
  296. SpReqAnsContext<PinPadService_LoadKeysSM_Req, PinPadService_LoadKeysSM_Ans>::Pointer ctx;
  297. LoadKeySMTask(CPinPadFSM* f) : fsm(f) {}
  298. void Process()
  299. {
  300. LOG_FUNCTION();
  301. FSMEvent *e = new FSMEvent(USER_EVT_LOADKEY_SM_FINISHED);
  302. e->param1 = fsm->LoadKeySM(ctx);
  303. fsm->PostEventFIFO(e);
  304. }
  305. };
  306. struct EncryptSMTask : public ITaskSp
  307. {
  308. CPinPadFSM* fsm;
  309. SpReqAnsContext<PinPadService_EncryptDataSM_Req, PinPadService_EncryptDataSM_Ans>::Pointer ctx;
  310. EncryptSMTask(CPinPadFSM* f) : fsm(f) {}
  311. void Process()
  312. {
  313. LOG_FUNCTION();
  314. FSMEvent *e = new FSMEvent(USER_EVT_ENCRYPT_SM_FINISHED);
  315. e->param1 = fsm->EncryptSM(ctx);
  316. fsm->PostEventFIFO(e);
  317. }
  318. };
  319. struct GetCheckCodeTask : public ITaskSp
  320. {
  321. CPinPadFSM* fsm;
  322. SpReqAnsContext<PinPadService_GetCheckCode_Req, PinPadService_GetCheckCode_Ans>::Pointer ctx;
  323. GetCheckCodeTask(CPinPadFSM* f) : fsm(f) {}
  324. void Process()
  325. {
  326. FSMEvent *e = new FSMEvent(USER_EVT_GET_CHECKCODE_FINISHED);
  327. e->param1 = fsm->GetCheckCode(ctx);
  328. fsm->PostEventFIFO(e);
  329. }
  330. };
  331. template<class T>
  332. class TimerOutHelper : public ITimerListener
  333. {
  334. public:
  335. typedef void (T::*FuncTimer)(void *pUserdata);
  336. TimerOutHelper(T *p, FuncTimer pTimerFunc, void *pUserData, bool bDeleteSelf = false)
  337. : m_pObject(p), m_pUserData(pUserData), m_pTimer(pTimerFunc), m_bDeleteSelf(bDeleteSelf) {}
  338. virtual void OnTimeout(DWORD dwTimerID)
  339. {
  340. (m_pObject->*m_pTimer)(m_pUserData);
  341. if (m_bDeleteSelf)
  342. delete this;
  343. }
  344. private:
  345. void *m_pUserData;
  346. T *m_pObject;
  347. FuncTimer m_pTimer;
  348. bool m_bDeleteSelf;
  349. };
  350. #endif //__PINPAD_FSM_H