mod_PinPad.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. ErrorCodeEnum Error = m_fsm.Init(this);
  53. return Error;
  54. }
  55. virtual ErrorCodeEnum __OnPause(ErrorCodeEnum preOperationError)
  56. {
  57. return Error_Succeed;
  58. }
  59. virtual CServerSessionBase* OnNewSession(const char*,const char*)
  60. {
  61. return new PinPadServerSession(this);
  62. }
  63. void InputWaitMore(SpOnewayCallContext<PinPadService_InputWaitMore_Info>::Pointer ctx)
  64. {
  65. LOG_FUNCTION();
  66. InputWaitingMoreEvent *e = new InputWaitingMoreEvent();
  67. m_fsm.PostEventFIFO(e);
  68. }
  69. void InputCancel(SpOnewayCallContext<PinPadService_InputCancel_Info>::Pointer ctx)
  70. {
  71. LOG_FUNCTION();
  72. InputCancelEvent *e = new InputCancelEvent();
  73. m_fsm.PostEventFIFO(e);
  74. }
  75. void Exit(SpOnewayCallContext<PinPadService_Exit_Info>::Pointer ctx)
  76. {
  77. FSMEvent *evt = new FSMEvent(USER_EVT_EXIT);
  78. m_fsm.PostEventFIFO(evt);
  79. }
  80. void GetInputJS(SpReqAnsContext<PinPadService_GetInputJS_Req, PinPadService_GetInputJS_Ans>::Pointer ctx)
  81. {
  82. if (!m_fsm.GetDevInitFlag())
  83. ctx->Answer(Error_DevNotAvailable, PinPad_UserErrorCode_PinPad_DevOpenFailed);
  84. else {
  85. m_fsm.SetJSCtx(ctx);
  86. GetInputJSEvent* e = new GetInputJSEvent();
  87. e->ctx = ctx;
  88. m_fsm.PostEventFIFO(e);
  89. }
  90. }
  91. void InputCancelJS(SpReqAnsContext<PinPadService_InputCancelJS_Req, PinPadService_InputCancelJS_Ans>::Pointer ctx)
  92. {
  93. if (!m_fsm.GetDevInitFlag())
  94. ctx->Answer(Error_DevNotAvailable, PinPad_UserErrorCode_PinPad_DevOpenFailed);
  95. else if (_stricmp(m_fsm.GetCurrStateName(), "Input") != 0)
  96. ctx->Answer(Error_Unexpect, PinPad_UserErrorCode_NotInGetInput);
  97. else
  98. {
  99. InputCancelEvent* e = new InputCancelEvent();
  100. m_fsm.PostEventFIFO(e);
  101. }
  102. }
  103. void GetInputSM(SpReqAnsContext<PinPadService_GetInputSM_Req, PinPadService_GetInputSM_Ans>::Pointer ctx)
  104. {
  105. if (!m_fsm.GetDevInitFlag())
  106. {
  107. ctx->Answer(Error_DevNotAvailable, PinPad_UserErrorCode_PinPad_DevOpenFailed);
  108. LogWarn(Severity_Middle, Error_DevNotAvailable, PinPad_UserErrorCode_PinPad_DevOpenFailed, "Open PinPad failed(GetInputSM)");
  109. }else{
  110. m_fsm.SetCtx(ctx);
  111. GetInputSMEvent* e = new GetInputSMEvent();
  112. e->ctx = ctx;
  113. m_fsm.PostEventFIFO(e);
  114. }
  115. }
  116. void LoadKeysSM(SpReqAnsContext<PinPadService_LoadKeysSM_Req, PinPadService_LoadKeysSM_Ans>::Pointer ctx)
  117. {
  118. LOG_FUNCTION();
  119. if(!m_fsm.GetDevInitFlag())
  120. ctx->Answer(Error_DevNotAvailable, PinPad_UserErrorCode_PinPad_DevOpenFailed);
  121. else{
  122. LoadKeySMEvent *evt = new LoadKeySMEvent();
  123. evt->ctx = ctx;
  124. m_fsm.PostEventFIFO(evt);
  125. }
  126. }
  127. void EncryptDataSM(SpReqAnsContext<PinPadService_EncryptDataSM_Req, PinPadService_EncryptDataSM_Ans>::Pointer ctx)
  128. {
  129. LOG_FUNCTION();
  130. if(!m_fsm.GetDevInitFlag())
  131. ctx->Answer(Error_DevNotAvailable, PinPad_UserErrorCode_PinPad_DevOpenFailed);
  132. else{
  133. EncryptSMEvent *evt = new EncryptSMEvent();
  134. evt->ctx = ctx;
  135. m_fsm.PostEventFIFO(evt);
  136. }
  137. }
  138. void QueryFunc(SpReqAnsContext<PinPadService_QueryFunc_Req, PinPadService_QueryFunc_Ans>::Pointer ctx)
  139. {
  140. if (m_fsm.GetDevInitFlag())
  141. {
  142. m_fsm.GetEncryptKey(ctx->Ans.encryptkey, ctx->Ans.reserved1);
  143. ctx->Answer(Error_Succeed);
  144. }
  145. else{
  146. ctx->Answer(Error_DevNotAvailable, PinPad_UserErrorCode_PinPad_DevOpenFailed);
  147. }
  148. }
  149. void GetCheckCode(SpReqAnsContext<PinPadService_GetCheckCode_Req, PinPadService_GetCheckCode_Ans>::Pointer ctx)
  150. {
  151. if(!m_fsm.GetDevInitFlag())
  152. ctx->Answer(Error_DevNotAvailable, PinPad_UserErrorCode_PinPad_DevOpenFailed);
  153. else{
  154. GetCheckCodeEvent *evt = new GetCheckCodeEvent();
  155. evt->ctx = ctx;
  156. m_fsm.PostEventFIFO(evt);
  157. }
  158. }
  159. void GetDevInfo(SpReqAnsContext<PinPadService_GetDevInfo_Req, PinPadService_GetDevInfo_Ans>::Pointer ctx)
  160. {
  161. if (m_fsm.GetDevInitingFlag())
  162. ctx->Answer(Error_NotInit, PinPad_UserErrorCode_PinPad_DevOpenFailed);
  163. else if (m_fsm.GetDevInitFlag())
  164. {
  165. DevCategoryInfo devInfo;
  166. memset(&devInfo, 0, sizeof(devInfo));
  167. ErrorCodeEnum eGetDevInfo = m_fsm.GetDevInfo(devInfo);
  168. if (eGetDevInfo == Error_Succeed)
  169. {
  170. //oiltmp@20240823 纪林的与业务组的初始化优化完成之后,是不是不要再拼接了?
  171. CSimpleStringA tmpAddStr("");
  172. tmpAddStr = tmpAddStr + "Vendor=" + devInfo.szVendor;
  173. if (devInfo.szModel[strlen(devInfo.szModel) - 1] == '#')
  174. ctx->Ans.model = CSimpleStringA(devInfo.szModel) + tmpAddStr;
  175. else
  176. ctx->Ans.model = CSimpleStringA(devInfo.szModel) + "#" + tmpAddStr;
  177. ctx->Ans.type = devInfo.szType;
  178. }
  179. ctx->Ans.state = eGetDevInfo;
  180. ctx->Answer(Error_Succeed);
  181. }
  182. else
  183. ctx->Answer(Error_DevNotAvailable, PinPad_UserErrorCode_PinPad_DevOpenFailed);
  184. }
  185. void DoInputCancelJS(SpReqAnsContext<PinPadService_InputCancelJS_Req, PinPadService_InputCancelJS_Ans>::Pointer ctx);
  186. virtual void OnSysVarEvent(const char *pszKey,
  187. const char *pszValue, const char *pszOldValue, const char *pszEntityName);
  188. virtual bool IsService()const{return true;}
  189. virtual bool IsMultiThread()const{return true;}
  190. virtual void OnSelfTest(EntityTestEnum eTestType,CSmartPointer<ITransactionContext> pTransactionContext)
  191. {
  192. m_fsm.SelfTest(eTestType,pTransactionContext);
  193. }
  194. protected:
  195. private:
  196. CPinPadFSM m_fsm;
  197. int m_state;
  198. };
  199. struct InputCancelJSTask : public ITaskSp
  200. {
  201. CPinPadEntity* pEntity;
  202. SpReqAnsContext<PinPadService_InputCancelJS_Req, PinPadService_InputCancelJS_Ans>::Pointer ctx;
  203. InputCancelJSTask(CPinPadEntity* entity) : pEntity(entity) {}
  204. void Process()
  205. {
  206. pEntity->DoInputCancelJS(ctx);
  207. }
  208. };
  209. #endif //__MOD_PINPAD_H