PinPadFSM.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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_devSN(""), 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 PushInputChar(char *buf, int c);
  200. void pop_char(char *buf, int *len,bool bClear=true);
  201. void clear_char(char *buf, int *len);
  202. bool Get12Account(char *szAcc);
  203. int GetInput(SpReqAnsContext<PinPadService_GetInputSM_Req, PinPadService_GetInputSM_Ans>::Pointer ctxSM);
  204. ErrorCodeEnum GetEncryptText(SpReqAnsContext<PinPadService_GetInputSM_Req, PinPadService_GetInputSM_Ans>::Pointer ctxSM);
  205. bool GetDevInitFlag(){ return m_bOpened;}
  206. bool GetDeviceOpeningFlag() { return m_bOpening; }
  207. void SetExitFlag(){m_bExit = true;}
  208. bool GetEntityExit() { return m_bEntityExit;}
  209. void SetCtx(SpReqAnsContext<PinPadService_GetInputSM_Req, PinPadService_GetInputSM_Ans>::Pointer ctxSM)
  210. {
  211. m_inputSMCtx = ctxSM;
  212. m_bPinInput = true;
  213. }
  214. bool GetCtx(SpReqAnsContext<PinPadService_GetInputSM_Req, PinPadService_GetInputSM_Ans>::Pointer& ctxSM)
  215. {
  216. if (m_inputSMCtx == NULL)
  217. {
  218. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("ctxSM is null");
  219. return false;
  220. }
  221. ctxSM = m_inputSMCtx;
  222. return true;
  223. }
  224. void SetJSCtx(SpReqAnsContext<PinPadService_GetInputJS_Req, PinPadService_GetInputJS_Ans>::Pointer ctxJS)
  225. {
  226. m_inputJSCtx = ctxJS;
  227. m_bPinInputJS = true;
  228. }
  229. bool GetJSCtx(SpReqAnsContext<PinPadService_GetInputJS_Req, PinPadService_GetInputJS_Ans>::Pointer& ctxJS)
  230. {
  231. if (m_inputJSCtx == NULL)
  232. {
  233. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("ctxJS is null");
  234. return false;
  235. }
  236. ctxJS = m_inputJSCtx;
  237. return true;
  238. }
  239. int GetInputJS(SpReqAnsContext<PinPadService_GetInputJS_Req, PinPadService_GetInputJS_Ans>::Pointer ctxJS);
  240. ErrorCodeEnum GetEncryptTextJS(SpReqAnsContext<PinPadService_GetInputJS_Req, PinPadService_GetInputJS_Ans>::Pointer ctxJS);
  241. ErrorCodeEnum GetDevInfo(DevCategoryInfo &devInfo);
  242. int LoadKeySM(SpReqAnsContext<PinPadService_LoadKeysSM_Req, PinPadService_LoadKeysSM_Ans>::Pointer ctx);
  243. int EncryptSM(SpReqAnsContext<PinPadService_EncryptDataSM_Req, PinPadService_EncryptDataSM_Ans>::Pointer ctx);
  244. ErrorCodeEnum GetCheckCode(SpReqAnsContext<PinPadService_GetCheckCode_Req, PinPadService_GetCheckCode_Ans>::Pointer ctx);
  245. int GetEncryptKey(int &devFunc,int &loadFunc)
  246. {
  247. devFunc = loadFunc = 0;
  248. devFunc = m_encryptkey;
  249. int tmp = 0;
  250. if (m_bSMLoaded)
  251. tmp += 2;
  252. if (m_bSMLoaded)
  253. loadFunc = 2;
  254. else
  255. loadFunc = tmp;
  256. return 0;
  257. }
  258. void SetInWhatPage(int iPageType);
  259. void SelfTest(EntityTestEnum eTestType,CSmartPointer<ITransactionContext> pTransactionContext);
  260. void ToLogWarnInfoAboutTermCustom();
  261. bool IsInGetInputJS() { return m_bPinInputJS; }
  262. public:
  263. SP::Toolkit::CConditionVarPlus m_hInputConVar;
  264. SP::Toolkit::CConditionVarPlus m_hInitConVar;
  265. private:
  266. SpReqAnsContext<PinPadService_GetInputSM_Req, PinPadService_GetInputSM_Ans>::Pointer m_inputSMCtx;
  267. SpReqAnsContext<PinPadService_GetInputJS_Req, PinPadService_GetInputJS_Ans>::Pointer m_inputJSCtx;
  268. char m_szAccount[MAX_ACCOUNT_LEN];
  269. bool m_bFrontCancel,m_bWaitingMore,m_bExit,m_bPlainPin,m_bEntityExit
  270. ,m_bPinInput,m_bLoadKey,m_bEncrypt,m_bSM,m_bSMLoaded, m_bPinInputJS, m_bUseBackupSN/*if use backup sn*/;
  271. ULLINT m_ullBeginTime, m_ullEndTime,m_ullConnectCost;
  272. CSimpleStringA m_deviceNo, m_port, m_Baudrate, m_devCheckData,m_keySNSM,m_szModel,m_szType,m_szVendor,m_devSN/*fwb SN*/;
  273. char m_buf[4];
  274. ErrorCodeEnum m_testResult;
  275. DWORD m_dwDevCommFailCount,m_dwPinPadRunCount;
  276. DevStateEnum m_eDevState;
  277. int m_encryptkey;
  278. int m_iInWhatPage/*在哪个页面,区分首页,用户桌面,其他页*/;
  279. CSimpleStringA m_csMachineType, m_csSite, m_terminalNo,m_lastCheckCode;
  280. CSystemStaticInfo m_sysStaticInfo;
  281. CSimpleStringA m_csAlarmMsg;
  282. bool IsInBusiness() { return (m_iInWhatPage == PageType_Other); }
  283. };
  284. struct InitTask : public ITaskSp
  285. {
  286. CPinPadFSM *fsm;
  287. InitTask(CPinPadFSM* f) : fsm(f) {}
  288. void Process()
  289. {
  290. FSMEvent *e = new FSMEvent(USER_EVT_INITFINISHED);
  291. e->param1 = fsm->Initial();
  292. fsm->PostEventFIFO(e);
  293. }
  294. };
  295. struct LoadKeySMTask : public ITaskSp
  296. {
  297. CPinPadFSM* fsm;
  298. SpReqAnsContext<PinPadService_LoadKeysSM_Req, PinPadService_LoadKeysSM_Ans>::Pointer ctx;
  299. LoadKeySMTask(CPinPadFSM* f) : fsm(f) {}
  300. void Process()
  301. {
  302. LOG_FUNCTION();
  303. FSMEvent *e = new FSMEvent(USER_EVT_LOADKEY_SM_FINISHED);
  304. e->param1 = fsm->LoadKeySM(ctx);
  305. fsm->PostEventFIFO(e);
  306. }
  307. };
  308. struct EncryptSMTask : public ITaskSp
  309. {
  310. CPinPadFSM* fsm;
  311. SpReqAnsContext<PinPadService_EncryptDataSM_Req, PinPadService_EncryptDataSM_Ans>::Pointer ctx;
  312. EncryptSMTask(CPinPadFSM* f) : fsm(f) {}
  313. void Process()
  314. {
  315. LOG_FUNCTION();
  316. FSMEvent *e = new FSMEvent(USER_EVT_ENCRYPT_SM_FINISHED);
  317. e->param1 = fsm->EncryptSM(ctx);
  318. fsm->PostEventFIFO(e);
  319. }
  320. };
  321. struct GetCheckCodeTask : public ITaskSp
  322. {
  323. CPinPadFSM* fsm;
  324. SpReqAnsContext<PinPadService_GetCheckCode_Req, PinPadService_GetCheckCode_Ans>::Pointer ctx;
  325. GetCheckCodeTask(CPinPadFSM* f) : fsm(f) {}
  326. void Process()
  327. {
  328. FSMEvent *e = new FSMEvent(USER_EVT_GET_CHECKCODE_FINISHED);
  329. e->param1 = fsm->GetCheckCode(ctx);
  330. fsm->PostEventFIFO(e);
  331. }
  332. };
  333. template<class T>
  334. class TimerOutHelper : public ITimerListener
  335. {
  336. public:
  337. typedef void (T::*FuncTimer)(void *pUserdata);
  338. TimerOutHelper(T *p, FuncTimer pTimerFunc, void *pUserData, bool bDeleteSelf = false)
  339. : m_pObject(p), m_pUserData(pUserData), m_pTimer(pTimerFunc), m_bDeleteSelf(bDeleteSelf) {}
  340. virtual void OnTimeout(DWORD dwTimerID)
  341. {
  342. (m_pObject->*m_pTimer)(m_pUserData);
  343. if (m_bDeleteSelf)
  344. delete this;
  345. }
  346. private:
  347. void *m_pUserData;
  348. T *m_pObject;
  349. FuncTimer m_pTimer;
  350. bool m_bDeleteSelf;
  351. };
  352. #endif //__PINPAD_FSM_H