mod_cardswiper.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. #include "CardSwiper_server_g.h"
  2. #include "CardSwiper_def_g.h"
  3. #include "CardSwiperClass.h"
  4. #include "CardSwiperFSM.h"
  5. using namespace CardSwiper;
  6. //#pragma comment(lib,"CardSwiper.lib")
  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_MagTransferInit(SpReqAnsContext<CardSwiperService_MagTransferInit_Req, CardSwiperService_MagTransferInit_Ans>::Pointer ctx);
  22. virtual void Handle_QueryConnInfo(SpReqAnsContext<CardSwiperService_QueryConnInfo_Req, CardSwiperService_QueryConnInfo_Ans>::Pointer ctx);
  23. virtual void Handle_InsertWaitMore(SpOnewayCallContext<CardSwiperService_InsertWaitMore_Info>::Pointer ctx);
  24. virtual void Handle_QueryFWBList(SpReqAnsContext<CardSwiperService_QueryFWBList_Req, CardSwiperService_QueryFWBList_Ans>::Pointer ctx);
  25. virtual void Handle_BindFWB(SpReqAnsContext<CardSwiperService_BindFWB_Req, CardSwiperService_BindFWB_Ans>::Pointer ctx);
  26. private:
  27. CCardSwiperEntity* m_pEntity;
  28. };
  29. class CCardSwiperEntity : public CEntityBase,public ILogListener, public ISysVarListener
  30. {
  31. public:
  32. CCardSwiperEntity()
  33. {
  34. //MessageBoxA(NULL, "", "", MB_OK);
  35. bInitialized = false;
  36. }
  37. virtual ~CCardSwiperEntity(){}
  38. virtual const char *GetEntityName() const { return "CardSwiper"; }
  39. virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
  40. {
  41. LOG_FUNCTION();
  42. //MessageBoxA(0,0,0,0);
  43. ErrorCodeEnum eStart = m_fsm.Init(this);
  44. GetFunction()->SubscribeLog(m_uuidAccessAuth, this,
  45. Log_Error, Severity_Middle, Error_IgnoreAll, -1, "AccessAuthorization");
  46. GetFunction()->SubscribeLog(m_uuidAccessAuth, this,
  47. Log_Event, Severity_Middle, Error_IgnoreAll, -1, "AccessAuthorization");
  48. GetFunction()->RegistSysVarEvent("UIState", this);
  49. if (eStart == Error_Succeed)
  50. {
  51. //oiltest no gpio for current
  52. //m_pGpioClient = new GpioService_ClientBase(this);
  53. //ErrorCodeEnum ErrorConn = m_pGpioClient->Connect();
  54. //if (ErrorConn != Error_Succeed) {
  55. // m_pGpioClient->SafeDelete();
  56. // m_pGpioClient = NULL;
  57. //}
  58. //else
  59. // Dbg("gpio connected.");
  60. }
  61. pTransactionContext->SendAnswer(eStart);
  62. //ErrorCodeEnum Error = __OnStart(Error_Succeed);
  63. //pTransactionContext->SendAnswer(Error);
  64. }
  65. virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
  66. {
  67. ErrorCodeEnum Error = __OnStart(Error_Succeed);
  68. pTransactionContext->SendAnswer(Error);
  69. }
  70. virtual void OnPrePause(CSmartPointer<ITransactionContext> pTransactionContext)
  71. {
  72. ErrorCodeEnum Error = __OnPause(Error_Succeed);
  73. pTransactionContext->SendAnswer(Error);
  74. }
  75. virtual ErrorCodeEnum __OnStart(ErrorCodeEnum preOperationError)
  76. {
  77. //LOG_FUNCTION();
  78. ////MessageBoxA(0,0,0,0);
  79. //Init();
  80. //if (!bInitialized)
  81. //{
  82. // Dbg("test.notinit");
  83. // return Error_Unexpect;
  84. //}
  85. //HRESULT hr = m_pCardIss->SetCardInType(CI_CARD_IN_TYPE_ALL);
  86. //Dbg("setcardintype[%d]",hr);
  87. //if (hr != Error_Succeed)
  88. // return ErrorCodeEnum(hr);
  89. return Error_Succeed;
  90. }
  91. virtual ErrorCodeEnum __OnPause(ErrorCodeEnum preOperationError)
  92. {
  93. //MessageBoxA(0,0,0,0);
  94. //Test();
  95. //TestMagCard();
  96. //TestRFCard();
  97. m_fsm.QueryFWBList(NULL, 2);//oiltest
  98. return Error_Succeed;
  99. }
  100. virtual ErrorCodeEnum __OnClose(ErrorCodeEnum preOperationError)
  101. {
  102. return Error_Succeed;
  103. }
  104. virtual void OnSelfTest(EntityTestEnum eTestType,CSmartPointer<ITransactionContext> pTransactionContext)
  105. {
  106. m_fsm.SelfTest(eTestType,pTransactionContext);
  107. }
  108. virtual CServerSessionBase* OnNewSession(const char*,const char*)
  109. {
  110. return new CardSwiperServerSession(this);
  111. }
  112. void Init()
  113. {
  114. LOG_FUNCTION();
  115. if (CreateDevComponent((DeviceBaseClass *&)m_pCardIss) != Error_Succeed)
  116. {
  117. Dbg("创建cardiss设备模块失败");
  118. return;
  119. }
  120. //CardSwiperInitParam cardIssInitParam;
  121. //cardIssInitParam.dwBaudRate = 115200;
  122. //cardIssInitParam.btPort = 2;
  123. //HRESULT hr = m_pCardIss->DevOpen(cardIssInitParam);
  124. //if (hr != Error_Succeed)
  125. //{
  126. // Dbg("devopen failed[%d]",hr);
  127. // return;
  128. //}
  129. bInitialized = true;
  130. }
  131. //void TestRFCard()
  132. //{
  133. // LOG_FUNCTION();
  134. // HRESULT hr;
  135. // MessageBoxA(0,0,0,0);
  136. // hr = m_pCardIss->MoveCard(CI_MOVECARD_RF_POSITION);
  137. // Dbg("move card 2 rf [%d]",hr);
  138. // hr = m_pCardIss->ActiveCard();
  139. // Dbg("activecard[%d]",hr);
  140. // if (hr != Error_Succeed)
  141. // return;
  142. // CmdInfo cmdSend,cmdRecv;
  143. // BYTE zz = 0x00;
  144. // BYTE cls = 0x00;
  145. // BYTE ins = 0xa4;
  146. // BYTE p1 = 0x04;
  147. // BYTE p2 = 0x00;
  148. // BYTE le = NULL;
  149. // char *pse = "1PAY.SYS.DDF01";
  150. // BYTE tmpPse = strlen(pse);
  151. // LPBYTE pData = new BYTE[tmpPse];
  152. // if (pData == NULL)
  153. // Dbg("NEW");
  154. // memcpy(pData,pse,tmpPse);
  155. // ConstructAPDU(cls,ins,p1,p2,tmpPse,pData,&le);//oiltest &le -> NULL for test
  156. // cmdSend.dwSize = m_lenAPDU;
  157. // memcpy(cmdSend.data,m_APDUsendBuf,m_lenAPDU);
  158. // Dbg("send cmd");
  159. // hr = m_pCardIss->RFTypeABCommand(cmdSend,cmdRecv);
  160. // Dbg("cmdrecv hr[%d],len[%d],data[%s]",hr,cmdRecv.dwSize,cmdRecv.data);
  161. // //Dbg("[%d][%d][%d][%d][%d][%d]",pCmdRecv->err[2],pCmdRecv->err[3],pCmdRecv->err[4],pCmdRecv->err[5],pCmdRecv->err[6],pCmdRecv->err[7]);
  162. // //hr = m_pCardIss->OnCommnd(pCmdSend,pCmdRecv);
  163. // //Dbg("cmdrecv2 hr[%d],len[%d],data[%s]",hr,pCmdRecv->length,pCmdRecv->data);
  164. // //Dbg("2[%d][%d][%d][%d][%d][%d]",pCmdRecv->err[2],pCmdRecv->err[3],pCmdRecv->err[4],pCmdRecv->err[5],pCmdRecv->err[6],pCmdRecv->err[7]);
  165. // DevCategoryInfo devInfo;
  166. // memset(devInfo.szVendor,0,sizeof(devInfo.szVendor));
  167. // hr = m_pCardIss->GetDevCategory(devInfo);
  168. // Dbg("dev info[%d][%s]",hr,devInfo.szVendor);
  169. // hr = m_pCardIss->DeactivateCard();
  170. // Dbg("deactivated[%d]",hr);
  171. // hr = m_pCardIss->MoveCard(CI_MOVECARD_FRONT_GATE);
  172. // Dbg("eject[%d]",hr);
  173. // if (hr != Error_Succeed)
  174. // return;
  175. //}
  176. void TestMagCard()
  177. {
  178. //MessageBoxA(0,0,0,0);
  179. Dbg("read...");
  180. //hr = m_pCardSwiper->GetCardAccount(CI_TRACK_1|CI_TRACK_2|CI_TRACK_3,&ppCardData);
  181. TrackInfo* pTracks = new TrackInfo;
  182. MagTracks magTracks;
  183. magTracks.eRange = CI_TRACK_RANGE_1_2_3;
  184. SpReqAnsContext<CardSwiperService_Read_Req,CardSwiperService_Read_Ans>::Pointer ctx;
  185. m_fsm.ReadCard(ctx);
  186. return;
  187. ErrorCodeEnum hr = Error_Succeed;
  188. //ErrorCodeEnum hr = m_pCardIss->MagRead(magTracks);
  189. Dbg("read(%d)",hr);
  190. if (Error_Succeed == hr){
  191. Dbg("read answer:[%d][%d][%d]",(LPCSTR)pTracks[0].dwSize,
  192. (LPCSTR)pTracks[1].dwSize,(LPCSTR)pTracks[2].dwSize);
  193. Dbg("read answer:[%s][%s][%s]",(LPCSTR)pTracks[0].data,
  194. (LPCSTR)pTracks[1].data,(LPCSTR)pTracks[2].data);
  195. }
  196. else
  197. {
  198. Dbg("idc_read(%d)",hr);
  199. }
  200. Dbg("eject(%d)",hr);
  201. }
  202. //void Test()
  203. //{
  204. // LOG_FUNCTION();
  205. // HRESULT hr;
  206. // //hr = m_pCardIss->DectectCardType();
  207. // //Dbg("DectectCardType[%d]",hr);
  208. // //if (hr != Error_Succeed)
  209. // // return;
  210. // hr = m_pCardIss->ActiveCard();
  211. // Dbg("activecard[%d]",hr);
  212. // if (hr != Error_Succeed)
  213. // return;
  214. // CmdInfo cmdSend,cmdRecv;
  215. // BYTE zz = 0x00;
  216. // BYTE cls = 0x00;
  217. // BYTE ins = 0xa4;
  218. // BYTE p1 = 0x04;
  219. // BYTE p2 = 0x00;
  220. // BYTE le = NULL;
  221. // char *pse = "1PAY.SYS.DDF01";
  222. // BYTE tmpPse = strlen(pse);
  223. // LPBYTE pData = new BYTE[tmpPse];
  224. // if (pData == NULL)
  225. // Dbg("NEW");
  226. // memcpy(pData,pse,tmpPse);
  227. // ConstructAPDU(cls,ins,p1,p2,tmpPse,pData,&le);//oiltest &le -> NULL for test
  228. // //MessageBoxA(0,0,0,0);
  229. // cmdSend.dwSize = m_lenAPDU;
  230. // memcpy(cmdSend.data,m_APDUsendBuf,m_lenAPDU);
  231. // hr = m_pCardIss->ICCommand(cmdSend,cmdRecv);
  232. // Dbg("cmdrecv hr[%d],len[%d],data[%s]",hr,cmdRecv.dwSize,cmdRecv.data);
  233. // //Dbg("[%d][%d][%d][%d][%d][%d]",pCmdRecv->err[2],pCmdRecv->err[3],pCmdRecv->err[4],pCmdRecv->err[5],pCmdRecv->err[6],pCmdRecv->err[7]);
  234. // //hr = m_pCardIss->OnCommnd(pCmdSend,pCmdRecv);
  235. // //Dbg("cmdrecv2 hr[%d],len[%d],data[%s]",hr,pCmdRecv->length,pCmdRecv->data);
  236. // //Dbg("2[%d][%d][%d][%d][%d][%d]",pCmdRecv->err[2],pCmdRecv->err[3],pCmdRecv->err[4],pCmdRecv->err[5],pCmdRecv->err[6],pCmdRecv->err[7]);
  237. // DevCategoryInfo devInfo;
  238. // memset(devInfo.szVendor,0,sizeof(devInfo.szVendor));
  239. // hr = m_pCardIss->GetDevCategory(devInfo);
  240. // Dbg("dev info[%d][%s]",hr,devInfo.szVendor);
  241. // hr = m_pCardIss->DeactivateCard();
  242. // Dbg("deactivated[%d]",hr);
  243. // hr = m_pCardIss->MoveCard(CI_MOVECARD_FRONT_GATE);
  244. // Dbg("eject[%d]",hr);
  245. // if (hr != Error_Succeed)
  246. // return;
  247. //}
  248. void Read(SpReqAnsContext<CardSwiperService_Read_Req, CardSwiperService_Read_Ans>::Pointer ctx)
  249. {
  250. LOG_FUNCTION();
  251. m_fsm.SetAcceptWaitMore(false);
  252. CardReadEvent* e = new CardReadEvent();
  253. e->ctx = ctx;
  254. m_fsm.PostEventFIFO(e);
  255. }
  256. void PreOnline(SpReqAnsContext<CardSwiperService_PreOnline_Req, CardSwiperService_PreOnline_Ans>::Pointer ctx)
  257. {
  258. LOG_FUNCTION();
  259. PreOnlineEvent* e = new PreOnlineEvent();
  260. e->ctx = ctx;
  261. m_fsm.PostEventFIFO(e);
  262. }
  263. void PostOnline(SpReqAnsContext<CardSwiperService_PostOnline_Req, CardSwiperService_PostOnline_Ans>::Pointer ctx)
  264. {
  265. LOG_FUNCTION();
  266. PostOnlineEvent* e = new PostOnlineEvent();
  267. e->ctx = ctx;
  268. m_fsm.PostEventFIFO(e);
  269. }
  270. void Exit(SpOnewayCallContext<CardSwiperService_Exit_Info>::Pointer ctx)
  271. {
  272. //LOG_FUNCTION();
  273. FSMEvent *evt = new FSMEvent(USER_EVT_EXIT);
  274. m_fsm.PostEventFIFO(evt);
  275. }
  276. void CancelInsert(SpOnewayCallContext<CardSwiperService_CancelInsert_Info>::Pointer ctx)
  277. {
  278. m_fsm.SetAcceptWaitMore(false);
  279. FSMEvent *evt = new FSMEvent(USER_EVT_CANCELINSERT);
  280. m_fsm.PostEventFIFO(evt);
  281. }
  282. void Eject(SpReqAnsContext<CardSwiperService_Eject_Req, CardSwiperService_Eject_Ans>::Pointer ctx)
  283. {
  284. ctx->Answer(Error_Succeed);
  285. FSMEvent *pEvt = new FSMEvent(USER_EVT_EJECT);
  286. m_fsm.PostEventFIFO(pEvt);
  287. }
  288. void QueryCardInfo(SpReqAnsContext<CardSwiperService_QueryCardInfo_Req, CardSwiperService_QueryCardInfo_Ans>::Pointer ctx)
  289. {
  290. LOG_FUNCTION();
  291. QueryCardInfoEvent* e = new QueryCardInfoEvent();
  292. e->ctx = ctx;
  293. m_fsm.PostEventFIFO(e);
  294. }
  295. void MagTransferInit(SpReqAnsContext<CardSwiperService_MagTransferInit_Req, CardSwiperService_MagTransferInit_Ans>::Pointer ctx)
  296. {
  297. LOG_FUNCTION();
  298. MagDataTransferInitEvent * e = new MagDataTransferInitEvent();
  299. e->ctx = ctx;
  300. m_fsm.PostEventFIFO(e);
  301. }
  302. void QueryConnInfo(SpReqAnsContext<CardSwiperService_QueryConnInfo_Req, CardSwiperService_QueryConnInfo_Ans>::Pointer ctx)
  303. {
  304. ctx->Ans.connect = m_fsm.QueryConnStatus();
  305. ctx->Answer(Error_Succeed);
  306. }
  307. void InsertWaitMore(SpOnewayCallContext<CardSwiperService_InsertWaitMore_Info>::Pointer ctx)
  308. {
  309. m_fsm.SetAcceptWaitMore(true);
  310. }
  311. void QueryFWBList(SpReqAnsContext<CardSwiperService_QueryFWBList_Req, CardSwiperService_QueryFWBList_Ans>::Pointer ctx)
  312. {
  313. QueryFWBListEvent* pEvt = new QueryFWBListEvent();
  314. pEvt->ctx = ctx;
  315. m_fsm.PostEventFIFO(pEvt);
  316. }
  317. void BindFWB(SpReqAnsContext<CardSwiperService_BindFWB_Req, CardSwiperService_BindFWB_Ans>::Pointer ctx)
  318. {
  319. BindFWBEvent* pEvt = new BindFWBEvent();
  320. pEvt->ctx = ctx;
  321. m_fsm.PostEventFIFO(pEvt);
  322. }
  323. void GetDevInfo(SpReqAnsContext<CardSwiperService_GetDevInfo_Req, CardSwiperService_GetDevInfo_Ans>::Pointer ctx)
  324. {
  325. ctx->Ans.state = m_fsm.GetDevState();
  326. ctx->Answer(Error_Succeed);
  327. }
  328. virtual bool IsService()const{return true;}
  329. virtual bool IsMultiThread()const{return true;}
  330. virtual void OnLog(const CAutoArray<CUUID> &SubIDs, const CUUID nLogID, const LogTypeEnum eLogType, const SeverityLevelEnum eLevel,
  331. const DWORD dwSysError, const DWORD dwUserCode, const DWORD dwEntityInstanceID, const WORD wEntityDevelID,
  332. const CAutoArray<DWORD> &Param, const char *pszEntityName, const char *pszModuleName, const char *pszMessage);
  333. virtual void OnSysVarEvent(const char* pszKey,const char* pszValue, const char* pszOldValue, const char* pszEntityName);
  334. protected:
  335. private:
  336. CardSwiperClass* m_pCardIss;
  337. CCardSwiperFSM m_fsm;
  338. BOOL bInitialized;
  339. LONG m_lenAPDU;
  340. BYTE m_APDUsendBuf[512];
  341. CUUID m_uuidAccessAuth;
  342. private:
  343. void ConstructAPDU(BYTE cls,BYTE ins,BYTE p1,BYTE p2,BYTE lc,LPBYTE data,LPBYTE le)
  344. {
  345. LOG_FUNCTION();
  346. m_lenAPDU = 0;
  347. m_APDUsendBuf[0] = cls;
  348. m_APDUsendBuf[1] = ins;
  349. m_APDUsendBuf[2] = p1;
  350. m_APDUsendBuf[3] = p2;
  351. m_lenAPDU = 4;
  352. if (lc != NULL && data != NULL)
  353. {
  354. m_APDUsendBuf[4] = lc;
  355. m_lenAPDU++;
  356. if (lc != 0)
  357. {
  358. memcpy(m_APDUsendBuf+m_lenAPDU,data,lc);
  359. m_lenAPDU += lc;
  360. }
  361. }
  362. if (le != NULL)
  363. m_APDUsendBuf[m_lenAPDU++] = (*le);
  364. if (!data)
  365. {
  366. delete[] data;
  367. data = NULL;
  368. }
  369. Dbg("cmd len[%d]",m_lenAPDU);
  370. }
  371. };