PinPadFSM.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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_encryptkey(1)
  164. , m_bSM(false), m_bSMLoaded(false), m_szModel(""), m_szType("")
  165. , m_szVendor(""),m_csMachineType(true), m_terminalNo(true)
  166. , m_bPinInputJS(false), m_bUseBackupSN(true), m_bSetParamOnce(false){
  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. void SetExitFlag(){m_bExit = true;}
  205. bool GetEntityExit() { return m_bEntityExit;}
  206. void SetCtx(SpReqAnsContext<PinPadService_GetInputSM_Req, PinPadService_GetInputSM_Ans>::Pointer ctxSM)
  207. {
  208. m_inputSMCtx = ctxSM;
  209. m_bPinInput = true;
  210. m_bPinInputJS = false;
  211. }
  212. bool GetCtx(SpReqAnsContext<PinPadService_GetInputSM_Req, PinPadService_GetInputSM_Ans>::Pointer& ctxSM)
  213. {
  214. if (m_inputSMCtx == NULL)
  215. {
  216. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("ctxSM is null");
  217. return false;
  218. }
  219. ctxSM = m_inputSMCtx;
  220. return true;
  221. }
  222. void SetJSCtx(SpReqAnsContext<PinPadService_GetInputJS_Req, PinPadService_GetInputJS_Ans>::Pointer ctxJS)
  223. {
  224. m_inputJSCtx = ctxJS;
  225. m_bPinInputJS = true;
  226. m_bPinInput = false;
  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 SelfTest(EntityTestEnum eTestType,CSmartPointer<ITransactionContext> pTransactionContext);
  258. bool IsInGetInputJS() { return m_bPinInputJS; }
  259. public:
  260. SP::Toolkit::CConditionVarPlus m_hInputConVar;
  261. SP::Toolkit::CConditionVarPlus m_hInitConVar;
  262. private:
  263. SpReqAnsContext<PinPadService_GetInputSM_Req, PinPadService_GetInputSM_Ans>::Pointer m_inputSMCtx;
  264. SpReqAnsContext<PinPadService_GetInputJS_Req, PinPadService_GetInputJS_Ans>::Pointer m_inputJSCtx;
  265. char m_szAccount[MAX_ACCOUNT_LEN];
  266. bool m_bFrontCancel,m_bWaitingMore,m_bExit,m_bPlainPin,m_bEntityExit
  267. ,m_bPinInput,m_bLoadKey,m_bEncrypt,m_bSM,m_bSMLoaded, m_bPinInputJS, m_bUseBackupSN/*if use backup sn*/, m_bSetParamOnce/*call SetParam Only once*/;
  268. ULLINT m_ullBeginTime, m_ullEndTime, m_ullAdpFileLoadCost, m_ullEntityStart;
  269. CSimpleStringA m_deviceNo, m_devCheckData,m_keySNSM,m_szModel,m_szType,m_szVendor;
  270. char m_buf[4];
  271. DWORD m_dwDevCommFailCount,m_dwPinPadRunCount;
  272. int m_encryptkey;
  273. CSimpleStringA m_csMachineType, m_terminalNo,m_lastCheckCode;
  274. CSystemStaticInfo m_sysStaticInfo;
  275. };
  276. struct InitTask : public ITaskSp
  277. {
  278. CPinPadFSM *fsm;
  279. InitTask(CPinPadFSM* f) : fsm(f) {}
  280. void Process()
  281. {
  282. FSMEvent *e = new FSMEvent(USER_EVT_INITFINISHED);
  283. e->param1 = fsm->Initial();
  284. fsm->PostEventFIFO(e);
  285. }
  286. };
  287. struct LoadKeySMTask : public ITaskSp
  288. {
  289. CPinPadFSM* fsm;
  290. SpReqAnsContext<PinPadService_LoadKeysSM_Req, PinPadService_LoadKeysSM_Ans>::Pointer ctx;
  291. LoadKeySMTask(CPinPadFSM* f) : fsm(f) {}
  292. void Process()
  293. {
  294. LOG_FUNCTION();
  295. FSMEvent *e = new FSMEvent(USER_EVT_LOADKEY_SM_FINISHED);
  296. e->param1 = fsm->LoadKeySM(ctx);
  297. fsm->PostEventFIFO(e);
  298. }
  299. };
  300. struct EncryptSMTask : public ITaskSp
  301. {
  302. CPinPadFSM* fsm;
  303. SpReqAnsContext<PinPadService_EncryptDataSM_Req, PinPadService_EncryptDataSM_Ans>::Pointer ctx;
  304. EncryptSMTask(CPinPadFSM* f) : fsm(f) {}
  305. void Process()
  306. {
  307. LOG_FUNCTION();
  308. FSMEvent *e = new FSMEvent(USER_EVT_ENCRYPT_SM_FINISHED);
  309. e->param1 = fsm->EncryptSM(ctx);
  310. fsm->PostEventFIFO(e);
  311. }
  312. };
  313. struct GetCheckCodeTask : public ITaskSp
  314. {
  315. CPinPadFSM* fsm;
  316. SpReqAnsContext<PinPadService_GetCheckCode_Req, PinPadService_GetCheckCode_Ans>::Pointer ctx;
  317. GetCheckCodeTask(CPinPadFSM* f) : fsm(f) {}
  318. void Process()
  319. {
  320. FSMEvent *e = new FSMEvent(USER_EVT_GET_CHECKCODE_FINISHED);
  321. e->param1 = fsm->GetCheckCode(ctx);
  322. fsm->PostEventFIFO(e);
  323. }
  324. };
  325. template<class T>
  326. class TimerOutHelper : public ITimerListener
  327. {
  328. public:
  329. typedef void (T::*FuncTimer)(void *pUserdata);
  330. TimerOutHelper(T *p, FuncTimer pTimerFunc, void *pUserData, bool bDeleteSelf = false)
  331. : m_pObject(p), m_pUserData(pUserData), m_pTimer(pTimerFunc), m_bDeleteSelf(bDeleteSelf) {}
  332. virtual void OnTimeout(DWORD dwTimerID)
  333. {
  334. (m_pObject->*m_pTimer)(m_pUserData);
  335. if (m_bDeleteSelf)
  336. delete this;
  337. }
  338. private:
  339. void *m_pUserData;
  340. T *m_pObject;
  341. FuncTimer m_pTimer;
  342. bool m_bDeleteSelf;
  343. };
  344. #endif //__PINPAD_FSM_H