mod_PinPad.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #ifndef __MOD_PINPAD_H
  2. #define __MOD_PINPAD_H
  3. #include "SpBase.h"
  4. #include "PinPadFSM.h"
  5. #include "DevEntityCommBase.hpp"
  6. class PinPadServerSession : public PinPad::PinPadService_ServerSessionBase
  7. {
  8. public:
  9. PinPadServerSession(CPinPadEntity* pEntity):m_pEntity(pEntity){}
  10. virtual ~PinPadServerSession(){}
  11. void Handle_InputWaitMore(SpOnewayCallContext<PinPadService_InputWaitMore_Info>::Pointer ctx);
  12. void Handle_InputCancel(SpOnewayCallContext<PinPadService_InputCancel_Info>::Pointer ctx);
  13. void Handle_Exit(SpOnewayCallContext<PinPadService_Exit_Info>::Pointer ctx);
  14. void Handle_GetInputJS(SpReqAnsContext<PinPadService_GetInputJS_Req, PinPadService_GetInputJS_Ans>::Pointer ctx);
  15. void Handle_InputCancelJS(SpReqAnsContext<PinPadService_InputCancelJS_Req, PinPadService_InputCancelJS_Ans>::Pointer ctx);
  16. void Handle_GetDevInfo(SpReqAnsContext<PinPadService_GetDevInfo_Req, PinPadService_GetDevInfo_Ans>::Pointer ctx);
  17. void Handle_GetInputSM(SpReqAnsContext<PinPadService_GetInputSM_Req, PinPadService_GetInputSM_Ans>::Pointer ctx);
  18. void Handle_LoadKeysSM(SpReqAnsContext<PinPadService_LoadKeysSM_Req, PinPadService_LoadKeysSM_Ans>::Pointer ctx);
  19. void Handle_EncryptDataSM(SpReqAnsContext<PinPadService_EncryptDataSM_Req, PinPadService_EncryptDataSM_Ans>::Pointer ctx);
  20. void Handle_QueryFunc(SpReqAnsContext<PinPadService_QueryFunc_Req, PinPadService_QueryFunc_Ans>::Pointer ctx);
  21. void Handle_GetCheckCode(SpReqAnsContext<PinPadService_GetCheckCode_Req, PinPadService_GetCheckCode_Ans>::Pointer ctx);
  22. private:
  23. CPinPadEntity* m_pEntity;
  24. };
  25. class CPinPadEntity : public CDevAdptEntityBase, public ISysVarListener
  26. {
  27. public:
  28. CPinPadEntity():m_state(-1){}
  29. virtual ~CPinPadEntity(){}
  30. virtual const char *GetEntityName() const { return "PinPad"; }
  31. virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
  32. {
  33. ErrorCodeEnum Error = __OnStart(Error_Succeed);
  34. GetFunction()->RegistSysVarEvent("UIState", this);
  35. pTransactionContext->SendAnswer(Error);
  36. }
  37. virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
  38. {
  39. //ErrorCodeEnum Error = __OnClose(Error_Succeed);
  40. //MessageBoxA(0,0,0,0);
  41. //m_fsm.OnExit();
  42. m_fsm.PostExitEvent();
  43. pTransactionContext->SendAnswer(Error_Succeed);
  44. }
  45. virtual void OnPrePause(CSmartPointer<ITransactionContext> pTransactionContext)
  46. {
  47. ErrorCodeEnum Error = __OnPause(Error_Succeed);
  48. pTransactionContext->SendAnswer(Error);
  49. }
  50. virtual ErrorCodeEnum __OnStart(ErrorCodeEnum preOperationError)
  51. {
  52. LOG_FUNCTION();
  53. //MessageBoxA(0,0,0,0);
  54. ErrorCodeEnum Error = m_fsm.Init(this);
  55. return Error;
  56. }
  57. virtual ErrorCodeEnum __OnPause(ErrorCodeEnum preOperationError)
  58. {
  59. LOG_FUNCTION();
  60. return Error_Succeed;
  61. }
  62. virtual CServerSessionBase* OnNewSession(const char*,const char*)
  63. {
  64. return new PinPadServerSession(this);
  65. }
  66. void InputWaitMore(SpOnewayCallContext<PinPadService_InputWaitMore_Info>::Pointer ctx)
  67. {
  68. LOG_FUNCTION();
  69. InputWaitingMoreEvent *e = new InputWaitingMoreEvent();
  70. m_fsm.PostEventFIFO(e);
  71. }
  72. void InputCancel(SpOnewayCallContext<PinPadService_InputCancel_Info>::Pointer ctx)
  73. {
  74. LOG_FUNCTION();
  75. InputCancelEvent *e = new InputCancelEvent();
  76. m_fsm.PostEventFIFO(e);
  77. }
  78. void Exit(SpOnewayCallContext<PinPadService_Exit_Info>::Pointer ctx)
  79. {
  80. //LOG_FUNCTION();
  81. FSMEvent *evt = new FSMEvent(USER_EVT_EXIT);
  82. m_fsm.PostEventFIFO(evt);
  83. }
  84. void GetInputJS(SpReqAnsContext<PinPadService_GetInputJS_Req, PinPadService_GetInputJS_Ans>::Pointer ctx)
  85. {
  86. if (!m_fsm.GetDevInitFlag())
  87. ctx->Answer(Error_DevNotAvailable, PinPad_UserErrorCode_PinPad_DevOpenFailed);
  88. else {
  89. m_fsm.SetJSCtx(ctx);
  90. GetInputJSEvent* e = new GetInputJSEvent();
  91. e->ctx = ctx;
  92. m_fsm.PostEventFIFO(e);
  93. }
  94. }
  95. void InputCancelJS(SpReqAnsContext<PinPadService_InputCancelJS_Req, PinPadService_InputCancelJS_Ans>::Pointer ctx)
  96. {
  97. if (!m_fsm.GetDevInitFlag())
  98. ctx->Answer(Error_DevNotAvailable, PinPad_UserErrorCode_PinPad_DevOpenFailed);
  99. else if (_stricmp(m_fsm.GetCurrStateName(), "Input") != 0)
  100. ctx->Answer(Error_Unexpect, PinPad_UserErrorCode_NotInGetInput);
  101. else
  102. {
  103. InputCancelEvent* e = new InputCancelEvent();
  104. m_fsm.PostEventFIFO(e);
  105. }
  106. }
  107. void GetInputSM(SpReqAnsContext<PinPadService_GetInputSM_Req, PinPadService_GetInputSM_Ans>::Pointer ctx)
  108. {
  109. if (!m_fsm.GetDevInitFlag())
  110. {
  111. ctx->Answer(Error_DevNotAvailable, PinPad_UserErrorCode_PinPad_DevOpenFailed);
  112. LogWarn(Severity_Middle, Error_DevNotAvailable, PinPad_UserErrorCode_PinPad_DevOpenFailed, "Open PinPad failed(GetInputSM)");
  113. }else{
  114. m_fsm.SetCtx(ctx);
  115. GetInputSMEvent* e = new GetInputSMEvent();
  116. e->ctx = ctx;
  117. m_fsm.PostEventFIFO(e);
  118. }
  119. }
  120. void LoadKeysSM(SpReqAnsContext<PinPadService_LoadKeysSM_Req, PinPadService_LoadKeysSM_Ans>::Pointer ctx)
  121. {
  122. LOG_FUNCTION();
  123. if(!m_fsm.GetDevInitFlag())
  124. ctx->Answer(Error_DevNotAvailable, PinPad_UserErrorCode_PinPad_DevOpenFailed);
  125. else{
  126. LoadKeySMEvent *evt = new LoadKeySMEvent();
  127. evt->ctx = ctx;
  128. m_fsm.PostEventFIFO(evt);
  129. }
  130. }
  131. void EncryptDataSM(SpReqAnsContext<PinPadService_EncryptDataSM_Req, PinPadService_EncryptDataSM_Ans>::Pointer ctx)
  132. {
  133. LOG_FUNCTION();
  134. if(!m_fsm.GetDevInitFlag())
  135. ctx->Answer(Error_DevNotAvailable, PinPad_UserErrorCode_PinPad_DevOpenFailed);
  136. else{
  137. EncryptSMEvent *evt = new EncryptSMEvent();
  138. evt->ctx = ctx;
  139. m_fsm.PostEventFIFO(evt);
  140. }
  141. }
  142. void QueryFunc(SpReqAnsContext<PinPadService_QueryFunc_Req, PinPadService_QueryFunc_Ans>::Pointer ctx)
  143. {
  144. if (m_fsm.GetDevInitFlag())
  145. {
  146. m_fsm.GetEncryptKey(ctx->Ans.encryptkey, ctx->Ans.reserved1);
  147. ctx->Answer(Error_Succeed);
  148. }
  149. else{
  150. ctx->Answer(Error_DevNotAvailable, PinPad_UserErrorCode_PinPad_DevOpenFailed);
  151. }
  152. }
  153. void GetCheckCode(SpReqAnsContext<PinPadService_GetCheckCode_Req, PinPadService_GetCheckCode_Ans>::Pointer ctx)
  154. {
  155. if(!m_fsm.GetDevInitFlag())
  156. ctx->Answer(Error_DevNotAvailable, PinPad_UserErrorCode_PinPad_DevOpenFailed);
  157. else{
  158. GetCheckCodeEvent *evt = new GetCheckCodeEvent();
  159. evt->ctx = ctx;
  160. m_fsm.PostEventFIFO(evt);
  161. }
  162. }
  163. void GetDevInfo(SpReqAnsContext<PinPadService_GetDevInfo_Req, PinPadService_GetDevInfo_Ans>::Pointer ctx)
  164. {
  165. if (m_fsm.GetDeviceOpeningFlag())
  166. ctx->Answer(Error_NotInit, PinPad_UserErrorCode_PinPad_DevOpenFailed);
  167. else if (m_fsm.GetDevInitFlag())
  168. {
  169. DevCategoryInfo devInfo;
  170. memset(&devInfo, 0, sizeof(devInfo));
  171. ErrorCodeEnum eGetDevInfo = m_fsm.GetDevInfo(devInfo);
  172. if (eGetDevInfo == Error_Succeed)
  173. {
  174. ctx->Ans.model = devInfo.szModel;
  175. ctx->Ans.type = devInfo.szType;
  176. }
  177. ctx->Ans.state = eGetDevInfo;
  178. ctx->Answer(Error_Succeed);
  179. }
  180. else
  181. ctx->Answer(Error_DevNotAvailable, PinPad_UserErrorCode_PinPad_DevOpenFailed);
  182. }
  183. void DoInputCancelJS(SpReqAnsContext<PinPadService_InputCancelJS_Req, PinPadService_InputCancelJS_Ans>::Pointer ctx);
  184. bool GetDevInitInfo() { return m_fsm.GetDevInitFlag(); }
  185. virtual void OnSysVarEvent(const char *pszKey,
  186. const char *pszValue, const char *pszOldValue, const char *pszEntityName);
  187. virtual bool IsService()const{return true;}
  188. virtual bool IsMultiThread()const{return true;}
  189. virtual void OnSelfTest(EntityTestEnum eTestType,CSmartPointer<ITransactionContext> pTransactionContext)
  190. {
  191. m_fsm.SelfTest(eTestType,pTransactionContext);
  192. }
  193. protected:
  194. private:
  195. CPinPadFSM m_fsm;
  196. int m_state;
  197. };
  198. struct InputCancelJSTask : public ITaskSp
  199. {
  200. CPinPadEntity* pEntity;
  201. SpReqAnsContext<PinPadService_InputCancelJS_Req, PinPadService_InputCancelJS_Ans>::Pointer ctx;
  202. InputCancelJSTask(CPinPadEntity* entity) : pEntity(entity) {}
  203. void Process()
  204. {
  205. pEntity->DoInputCancelJS(ctx);
  206. }
  207. };
  208. #endif //__MOD_PINPAD_H