mod_ContactlessCard.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #ifndef _MOD_CONTACTLESS_H
  2. #define _MOD_CONTACTLESS_H
  3. #pragma once
  4. #include "stdafx.h"
  5. #include "ContactlessFSM.h"
  6. #include "DevEntityCommBase.hpp"
  7. #include "modVer.h"
  8. class CContactlessCardEntity;
  9. class ContactlessCardServerSession : public ContactlessCardService_ServerSessionBase
  10. {
  11. public:
  12. ContactlessCardServerSession(CContactlessCardEntity* pEntity):m_pEntity(pEntity){}
  13. virtual ~ContactlessCardServerSession(){}
  14. virtual void Handle_Insert(SpReqAnsContext<ContactlessCardService_Insert_Req, ContactlessCardService_Insert_Ans>::Pointer ctx);
  15. virtual void Handle_CancelInsert(SpOnewayCallContext<ContactlessCardService_CancelInsert_Info>::Pointer ctx);
  16. virtual void Handle_InsertWaitMore(SpOnewayCallContext<ContactlessCardService_InsertWaitMore_Info>::Pointer ctx);
  17. virtual void Handle_PreOnline(SpReqAnsContext<ContactlessCardService_PreOnline_Req, ContactlessCardService_PreOnline_Ans>::Pointer ctx);
  18. virtual void Handle_PostOnline(SpReqAnsContext<ContactlessCardService_PostOnline_Req, ContactlessCardService_PostOnline_Ans>::Pointer ctx);
  19. virtual void Handle_Eject(SpReqAnsContext<ContactlessCardService_Eject_Req, ContactlessCardService_Eject_Ans>::Pointer ctx);
  20. virtual void Handle_Exit(SpOnewayCallContext<ContactlessCardService_Exit_Info>::Pointer ctx);
  21. virtual void Handle_QueryCardInfo(SpReqAnsContext<ContactlessCardService_QueryCardInfo_Req, ContactlessCardService_QueryCardInfo_Ans>::Pointer ctx);
  22. virtual void Handle_GetDevInfo(SpReqAnsContext<ContactlessCardService_GetDevInfo_Req, ContactlessCardService_GetDevInfo_Ans>::Pointer ctx);
  23. private:
  24. CContactlessCardEntity* m_pEntity;
  25. };
  26. class CContactlessCardEntity : public CDevAdptEntityBase
  27. {
  28. public:
  29. CContactlessCardEntity()
  30. {
  31. }
  32. virtual ~CContactlessCardEntity(){}
  33. virtual const char *GetEntityName() const { return "ContactlessCard"; }
  34. virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
  35. {
  36. LOG_FUNCTION();
  37. ErrorCodeEnum eStart = m_fsm.Init(this);
  38. pTransactionContext->SendAnswer(Error_Succeed);
  39. }
  40. virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
  41. {
  42. ErrorCodeEnum Error = __OnStart(Error_Succeed);
  43. pTransactionContext->SendAnswer(Error);
  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. return Error_Succeed;
  53. }
  54. virtual ErrorCodeEnum __OnPause(ErrorCodeEnum preOperationError)
  55. {
  56. return Error_Succeed;
  57. }
  58. virtual ErrorCodeEnum __OnClose(ErrorCodeEnum preOperationError)
  59. {
  60. return Error_Succeed;
  61. }
  62. void Insert(SpReqAnsContext<ContactlessCardService_Insert_Req, ContactlessCardService_Insert_Ans>::Pointer ctx)
  63. {
  64. LOG_FUNCTION();
  65. if(!m_fsm.GetDevInitFlag()){
  66. ctx->Answer(Error_DevNotAvailable, ContactlessCard_UserErrorCode_Read_OpenFailed);
  67. LogWarn(Severity_Middle, Error_DevNotAvailable, ContactlessCard_UserErrorCode_Read_OpenFailed, "ReadCard but DevOpen failed.");
  68. }
  69. else{
  70. m_fsm.SetExitFlag(false);
  71. CardAcceptEvent* e = new CardAcceptEvent();
  72. e->ctx = ctx;
  73. m_fsm.PostEventFIFO(e);
  74. }
  75. }
  76. void CancelInsert(SpOnewayCallContext<ContactlessCardService_CancelInsert_Info>::Pointer ctx)
  77. {
  78. LOG_FUNCTION();
  79. if(!m_fsm.GetDevInitFlag()){
  80. LogWarn(Severity_Middle, Error_DevNotAvailable, ContactlessCard_UserErrorCode_Cancel_OpenFailed, "CancelInsert but DevOpen failed.");
  81. }else{
  82. FSMEvent *evt = new FSMEvent(USER_EVT_ACCEPT_CANCEL);
  83. m_fsm.PostEventFIFO(evt);
  84. }
  85. }
  86. void InsertWaitMore(SpOnewayCallContext<ContactlessCardService_InsertWaitMore_Info>::Pointer ctx)
  87. {
  88. if (m_fsm.GetWaitFlag())
  89. m_fsm.SetWaitMore();
  90. }
  91. void PreOnline(SpReqAnsContext<ContactlessCardService_PreOnline_Req, ContactlessCardService_PreOnline_Ans>::Pointer ctx)
  92. {
  93. LOG_FUNCTION();
  94. PreOnlineEvent* e = new PreOnlineEvent();
  95. e->ctx = ctx;
  96. m_fsm.PostEventFIFO(e);
  97. }
  98. void PostOnline(SpReqAnsContext<ContactlessCardService_PostOnline_Req, ContactlessCardService_PostOnline_Ans>::Pointer ctx)
  99. {
  100. LOG_FUNCTION();
  101. PostOnlineEvent* e = new PostOnlineEvent();
  102. e->ctx = ctx;
  103. m_fsm.PostEventFIFO(e);
  104. }
  105. void Eject(SpReqAnsContext<ContactlessCardService_Eject_Req, ContactlessCardService_Eject_Ans>::Pointer ctx)
  106. {
  107. LOG_FUNCTION();
  108. CardEjectEvent* e = new CardEjectEvent();
  109. e->ctx = ctx;
  110. m_fsm.PostEventFIFO(e);
  111. }
  112. void Exit(SpOnewayCallContext<ContactlessCardService_Exit_Info>::Pointer ctx)
  113. {
  114. LOG_FUNCTION();
  115. if(!m_fsm.GetDevInitFlag()){
  116. LogWarn(Severity_Middle, Error_DevNotAvailable, ContactlessCard_UserErrorCode_Exit_OpenFailed, "Exit but DevOpen failed.");
  117. }else{
  118. FSMEvent *evt = new FSMEvent(USER_EVT_EXIT);
  119. m_fsm.PostEventFIFO(evt);
  120. }
  121. }
  122. void QueryCardInfo(SpReqAnsContext<ContactlessCardService_QueryCardInfo_Req, ContactlessCardService_QueryCardInfo_Ans>::Pointer ctx)
  123. {
  124. LOG_FUNCTION();
  125. QueryCardInfoEvent* e = new QueryCardInfoEvent();
  126. e->ctx = ctx;
  127. m_fsm.PostEventFIFO(e);
  128. }
  129. void GetDevInfo(SpReqAnsContext<ContactlessCardService_GetDevInfo_Req, ContactlessCardService_GetDevInfo_Ans>::Pointer ctx)
  130. {
  131. LOG_FUNCTION();
  132. ctx->Ans.state = m_fsm.GetDevState();
  133. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER)("GetDevInfo %d", ctx->Ans.state);
  134. ctx->Answer(Error_Succeed);
  135. }
  136. virtual void OnSelfTest(EntityTestEnum eTestType,CSmartPointer<ITransactionContext> pTransactionContext)
  137. {
  138. m_fsm.SelfTest(eTestType,pTransactionContext);
  139. }
  140. virtual CServerSessionBase* OnNewSession(const char*,const char*)
  141. {
  142. return new ContactlessCardServerSession(this);
  143. }
  144. virtual bool IsService()const{return true;}
  145. virtual bool IsMultiThread()const{return true;}
  146. protected:
  147. private:
  148. CContactlessCardFSM m_fsm;
  149. private:
  150. };
  151. #endif //_MOD_CONTACTLESS_H