mod_cardswiper.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include "CardSwiper_server_g.h"
  2. #include "CardSwiper_def_g.h"
  3. #include "CardSwiperClass.h"
  4. #include "CardSwiperFSM.h"
  5. #include "DevEntityCommBase.hpp"
  6. using namespace CardSwiper;
  7. class CCardSwiperEntity;
  8. class CardSwiperServerSession : public CardSwiperService_ServerSessionBase
  9. {
  10. public:
  11. CardSwiperServerSession(CCardSwiperEntity* pEntity):m_pEntity(pEntity){}
  12. virtual ~CardSwiperServerSession(){}
  13. virtual void Handle_Read(SpReqAnsContext<CardSwiperService_Read_Req, CardSwiperService_Read_Ans>::Pointer ctx);
  14. virtual void Handle_PreOnline(SpReqAnsContext<CardSwiperService_PreOnline_Req, CardSwiperService_PreOnline_Ans>::Pointer ctx);
  15. virtual void Handle_PostOnline(SpReqAnsContext<CardSwiperService_PostOnline_Req, CardSwiperService_PostOnline_Ans>::Pointer ctx);
  16. virtual void Handle_Exit(SpOnewayCallContext<CardSwiperService_Exit_Info>::Pointer ctx);
  17. virtual void Handle_GetDevInfo(SpReqAnsContext<CardSwiperService_GetDevInfo_Req, CardSwiperService_GetDevInfo_Ans>::Pointer ctx);
  18. virtual void Handle_CancelInsert(SpOnewayCallContext<CardSwiperService_CancelInsert_Info>::Pointer ctx);
  19. virtual void Handle_Eject(SpReqAnsContext<CardSwiperService_Eject_Req, CardSwiperService_Eject_Ans>::Pointer ctx);
  20. virtual void Handle_QueryCardInfo(SpReqAnsContext<CardSwiperService_QueryCardInfo_Req, CardSwiperService_QueryCardInfo_Ans>::Pointer ctx);
  21. virtual void Handle_QueryConnInfo(SpReqAnsContext<CardSwiperService_QueryConnInfo_Req, CardSwiperService_QueryConnInfo_Ans>::Pointer ctx);
  22. virtual void Handle_InsertWaitMore(SpOnewayCallContext<CardSwiperService_InsertWaitMore_Info>::Pointer ctx);
  23. virtual void Handle_QueryFWBList(SpReqAnsContext<CardSwiperService_QueryFWBList_Req, CardSwiperService_QueryFWBList_Ans>::Pointer ctx);
  24. virtual void Handle_BindFWB(SpReqAnsContext<CardSwiperService_BindFWB_Req, CardSwiperService_BindFWB_Ans>::Pointer ctx);
  25. private:
  26. CCardSwiperEntity* m_pEntity;
  27. };
  28. class CCardSwiperEntity : public CDevAdptEntityBase,public ILogListener, public ISysVarListener
  29. {
  30. public:
  31. CCardSwiperEntity()
  32. {
  33. bInitialized = false;
  34. }
  35. virtual ~CCardSwiperEntity(){}
  36. virtual const char *GetEntityName() const { return "CardSwiper"; }
  37. virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
  38. {
  39. LOG_FUNCTION();
  40. //MessageBoxA(0,0,0,0);
  41. ErrorCodeEnum eStart = m_fsm.Init(this);
  42. GetFunction()->SubscribeLog(m_uuidAccessAuth, this,
  43. Log_Error, Severity_Middle, Error_IgnoreAll, -1, "AccessAuthorization");
  44. GetFunction()->SubscribeLog(m_uuidAccessAuth, this,
  45. Log_Event, Severity_Middle, Error_IgnoreAll, -1, "AccessAuthorization");
  46. GetFunction()->RegistSysVarEvent("UIState", this);
  47. pTransactionContext->SendAnswer(eStart);
  48. }
  49. virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
  50. {
  51. ErrorCodeEnum Error = __OnStart(Error_Succeed);
  52. pTransactionContext->SendAnswer(Error);
  53. }
  54. virtual void OnPrePause(CSmartPointer<ITransactionContext> pTransactionContext)
  55. {
  56. ErrorCodeEnum Error = __OnPause(Error_Succeed);
  57. pTransactionContext->SendAnswer(Error);
  58. }
  59. virtual ErrorCodeEnum __OnStart(ErrorCodeEnum preOperationError)
  60. {
  61. return Error_Succeed;
  62. }
  63. virtual ErrorCodeEnum __OnPause(ErrorCodeEnum preOperationError)
  64. {
  65. m_fsm.QueryFWBList(NULL, 2);//oiltest
  66. return Error_Succeed;
  67. }
  68. virtual ErrorCodeEnum __OnClose(ErrorCodeEnum preOperationError)
  69. {
  70. return Error_Succeed;
  71. }
  72. virtual void OnSelfTest(EntityTestEnum eTestType,CSmartPointer<ITransactionContext> pTransactionContext)
  73. {
  74. m_fsm.SelfTest(eTestType,pTransactionContext);
  75. }
  76. virtual CServerSessionBase* OnNewSession(const char*,const char*)
  77. {
  78. return new CardSwiperServerSession(this);
  79. }
  80. void Init()
  81. {
  82. LOG_FUNCTION();
  83. if (CreateDevComponent((DeviceBaseClass *&)m_pCardIss) != Error_Succeed)
  84. {
  85. DbgWarn("创建cardiss设备模块失败");
  86. return;
  87. }
  88. bInitialized = true;
  89. }
  90. void Read(SpReqAnsContext<CardSwiperService_Read_Req, CardSwiperService_Read_Ans>::Pointer ctx)
  91. {
  92. LOG_FUNCTION();
  93. if (!m_fsm.GetDevInitFlag())
  94. {
  95. ctx->Answer(Error_DevNotAvailable, CardSwiper_UserErrorCode_Read_OpenFailed);
  96. LogWarn(Severity_Middle, Error_DevNotAvailable, CardSwiper_UserErrorCode_Read_OpenFailed, "ReadCard but DevOpen failed.");
  97. }
  98. else if (m_fsm.GetCurrState()->id == 7) {
  99. ctx->Answer(Error_DevNotAvailable, CardSwiper_UserErrorCode_Read_DeviceOff);
  100. LogWarn(Severity_Middle, Error_DevNotAvailable, CardSwiper_UserErrorCode_Read_DeviceOff, "ReadCard but device is offline.");
  101. }
  102. else{
  103. m_fsm.SetAcceptWaitMore(false);
  104. CardReadEvent* e = new CardReadEvent();
  105. e->ctx = ctx;
  106. m_fsm.PostEventFIFO(e);
  107. }
  108. }
  109. void PreOnline(SpReqAnsContext<CardSwiperService_PreOnline_Req, CardSwiperService_PreOnline_Ans>::Pointer ctx)
  110. {
  111. LOG_FUNCTION();
  112. if (!m_fsm.GetDevInitFlag())
  113. {
  114. ctx->Answer(Error_DevNotAvailable, CardSwiper_UserErrorCode_PreOnline_OpenFailed);
  115. LogWarn(Severity_Middle, Error_DevNotAvailable, CardSwiper_UserErrorCode_PreOnline_OpenFailed, "PreOnline but DevOpen failed.");
  116. }
  117. else if (m_fsm.GetCurrState()->id == 7) {
  118. ctx->Answer(Error_DevNotAvailable, CardSwiper_UserErrorCode_PreOnline_DeviceOff);
  119. LogWarn(Severity_Middle, Error_DevNotAvailable, CardSwiper_UserErrorCode_PreOnline_DeviceOff, "PreOnline but device is offline.");
  120. }
  121. else{
  122. PreOnlineEvent* e = new PreOnlineEvent();
  123. e->ctx = ctx;
  124. m_fsm.PostEventFIFO(e);
  125. }
  126. }
  127. void PostOnline(SpReqAnsContext<CardSwiperService_PostOnline_Req, CardSwiperService_PostOnline_Ans>::Pointer ctx)
  128. {
  129. LOG_FUNCTION();
  130. PostOnlineEvent* e = new PostOnlineEvent();
  131. e->ctx = ctx;
  132. m_fsm.PostEventFIFO(e);
  133. }
  134. void Exit(SpOnewayCallContext<CardSwiperService_Exit_Info>::Pointer ctx)
  135. {
  136. //LOG_FUNCTION();
  137. FSMEvent *evt = new FSMEvent(USER_EVT_EXIT);
  138. m_fsm.PostEventFIFO(evt);
  139. }
  140. void CancelInsert(SpOnewayCallContext<CardSwiperService_CancelInsert_Info>::Pointer ctx)
  141. {
  142. m_fsm.SetAcceptWaitMore(false);
  143. FSMEvent *evt = new FSMEvent(USER_EVT_CANCELINSERT);
  144. m_fsm.PostEventFIFO(evt);
  145. }
  146. void Eject(SpReqAnsContext<CardSwiperService_Eject_Req, CardSwiperService_Eject_Ans>::Pointer ctx)
  147. {
  148. ctx->Answer(Error_Succeed);
  149. FSMEvent *pEvt = new FSMEvent(USER_EVT_EJECT);
  150. m_fsm.PostEventFIFO(pEvt);
  151. }
  152. void QueryCardInfo(SpReqAnsContext<CardSwiperService_QueryCardInfo_Req, CardSwiperService_QueryCardInfo_Ans>::Pointer ctx)
  153. {
  154. LOG_FUNCTION();
  155. QueryCardInfoEvent* e = new QueryCardInfoEvent();
  156. e->ctx = ctx;
  157. m_fsm.PostEventFIFO(e);
  158. }
  159. void QueryConnInfo(SpReqAnsContext<CardSwiperService_QueryConnInfo_Req, CardSwiperService_QueryConnInfo_Ans>::Pointer ctx)
  160. {
  161. ctx->Ans.connect = m_fsm.QueryConnStatus();
  162. ctx->Answer(Error_Succeed);
  163. }
  164. void InsertWaitMore(SpOnewayCallContext<CardSwiperService_InsertWaitMore_Info>::Pointer ctx)
  165. {
  166. m_fsm.SetAcceptWaitMore(true);
  167. }
  168. void QueryFWBList(SpReqAnsContext<CardSwiperService_QueryFWBList_Req, CardSwiperService_QueryFWBList_Ans>::Pointer ctx)
  169. {
  170. QueryFWBListEvent* pEvt = new QueryFWBListEvent();
  171. pEvt->ctx = ctx;
  172. m_fsm.PostEventFIFO(pEvt);
  173. }
  174. void BindFWB(SpReqAnsContext<CardSwiperService_BindFWB_Req, CardSwiperService_BindFWB_Ans>::Pointer ctx)
  175. {
  176. BindFWBEvent* pEvt = new BindFWBEvent();
  177. pEvt->ctx = ctx;
  178. m_fsm.PostEventFIFO(pEvt);
  179. }
  180. void GetDevInfo(SpReqAnsContext<CardSwiperService_GetDevInfo_Req, CardSwiperService_GetDevInfo_Ans>::Pointer ctx)
  181. {
  182. ctx->Ans.state = m_fsm.GetDevState();
  183. ctx->Answer(Error_Succeed);
  184. }
  185. virtual bool IsService()const{return true;}
  186. virtual bool IsMultiThread()const{return true;}
  187. virtual void OnLog(const CAutoArray<CUUID> &SubIDs, const CUUID nLogID, const LogTypeEnum eLogType, const SeverityLevelEnum eLevel,
  188. const DWORD dwSysError, const DWORD dwUserCode, const DWORD dwEntityInstanceID, const WORD wEntityDevelID,
  189. const CAutoArray<DWORD> &Param, const char *pszEntityName, const char *pszModuleName,const char *pszMessage, const linkContext &pLinkInfo);
  190. virtual void OnSysVarEvent(const char* pszKey,const char* pszValue, const char* pszOldValue, const char* pszEntityName);
  191. protected:
  192. private:
  193. CardSwiperClass* m_pCardIss;
  194. CCardSwiperFSM m_fsm;
  195. BOOL bInitialized;
  196. LONG m_lenAPDU;
  197. BYTE m_APDUsendBuf[512];
  198. CUUID m_uuidAccessAuth;
  199. private:
  200. void ConstructAPDU(BYTE cls,BYTE ins,BYTE p1,BYTE p2,BYTE lc,LPBYTE data,LPBYTE le)
  201. {
  202. LOG_FUNCTION();
  203. m_lenAPDU = 0;
  204. m_APDUsendBuf[0] = cls;
  205. m_APDUsendBuf[1] = ins;
  206. m_APDUsendBuf[2] = p1;
  207. m_APDUsendBuf[3] = p2;
  208. m_lenAPDU = 4;
  209. if (lc != NULL && data != NULL)
  210. {
  211. m_APDUsendBuf[4] = lc;
  212. m_lenAPDU++;
  213. if (lc != 0)
  214. {
  215. memcpy(m_APDUsendBuf+m_lenAPDU,data,lc);
  216. m_lenAPDU += lc;
  217. }
  218. }
  219. if (le != NULL)
  220. m_APDUsendBuf[m_lenAPDU++] = (*le);
  221. if (!data)
  222. {
  223. delete[] data;
  224. data = NULL;
  225. }
  226. DbgInfo(CSimpleStringA::Format("ConstructAPDU, lenAPDU:%d",m_lenAPDU));
  227. }
  228. };