123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- #include "CardSwiper_server_g.h"
- #include "CardSwiper_def_g.h"
- #include "CardSwiperClass.h"
- #include "CardSwiperFSM.h"
- using namespace CardSwiper;
- //#pragma comment(lib,"CardSwiper.lib")
- class CCardSwiperEntity;
- class CardSwiperServerSession : public CardSwiperService_ServerSessionBase
- {
- public:
- CardSwiperServerSession(CCardSwiperEntity* pEntity):m_pEntity(pEntity){}
- virtual ~CardSwiperServerSession(){}
- virtual void Handle_Read(SpReqAnsContext<CardSwiperService_Read_Req, CardSwiperService_Read_Ans>::Pointer ctx);
- virtual void Handle_PreOnline(SpReqAnsContext<CardSwiperService_PreOnline_Req, CardSwiperService_PreOnline_Ans>::Pointer ctx);
- virtual void Handle_PostOnline(SpReqAnsContext<CardSwiperService_PostOnline_Req, CardSwiperService_PostOnline_Ans>::Pointer ctx);
- virtual void Handle_Exit(SpOnewayCallContext<CardSwiperService_Exit_Info>::Pointer ctx);
- virtual void Handle_GetDevInfo(SpReqAnsContext<CardSwiperService_GetDevInfo_Req, CardSwiperService_GetDevInfo_Ans>::Pointer ctx);
- virtual void Handle_CancelInsert(SpOnewayCallContext<CardSwiperService_CancelInsert_Info>::Pointer ctx);
- virtual void Handle_Eject(SpReqAnsContext<CardSwiperService_Eject_Req, CardSwiperService_Eject_Ans>::Pointer ctx);
- virtual void Handle_QueryCardInfo(SpReqAnsContext<CardSwiperService_QueryCardInfo_Req, CardSwiperService_QueryCardInfo_Ans>::Pointer ctx);
- virtual void Handle_MagTransferInit(SpReqAnsContext<CardSwiperService_MagTransferInit_Req, CardSwiperService_MagTransferInit_Ans>::Pointer ctx);
- virtual void Handle_QueryConnInfo(SpReqAnsContext<CardSwiperService_QueryConnInfo_Req, CardSwiperService_QueryConnInfo_Ans>::Pointer ctx);
- virtual void Handle_InsertWaitMore(SpOnewayCallContext<CardSwiperService_InsertWaitMore_Info>::Pointer ctx);
- virtual void Handle_QueryFWBList(SpReqAnsContext<CardSwiperService_QueryFWBList_Req, CardSwiperService_QueryFWBList_Ans>::Pointer ctx);
- virtual void Handle_BindFWB(SpReqAnsContext<CardSwiperService_BindFWB_Req, CardSwiperService_BindFWB_Ans>::Pointer ctx);
- private:
- CCardSwiperEntity* m_pEntity;
- };
- class CCardSwiperEntity : public CEntityBase,public ILogListener, public ISysVarListener
- {
- public:
- CCardSwiperEntity()
- {
- //MessageBoxA(NULL, "", "", MB_OK);
- bInitialized = false;
- }
- virtual ~CCardSwiperEntity(){}
- virtual const char *GetEntityName() const { return "CardSwiper"; }
- virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,CSmartPointer<ITransactionContext> pTransactionContext)
- {
- LOG_FUNCTION();
- //MessageBoxA(0,0,0,0);
- ErrorCodeEnum eStart = m_fsm.Init(this);
- GetFunction()->SubscribeLog(m_uuidAccessAuth, this,
- Log_Error, Severity_Middle, Error_IgnoreAll, -1, "AccessAuthorization");
- GetFunction()->SubscribeLog(m_uuidAccessAuth, this,
- Log_Event, Severity_Middle, Error_IgnoreAll, -1, "AccessAuthorization");
- GetFunction()->RegistSysVarEvent("UIState", this);
- if (eStart == Error_Succeed)
- {
- //oiltest no gpio for current
- //m_pGpioClient = new GpioService_ClientBase(this);
- //ErrorCodeEnum ErrorConn = m_pGpioClient->Connect();
- //if (ErrorConn != Error_Succeed) {
- // m_pGpioClient->SafeDelete();
- // m_pGpioClient = NULL;
- //}
- //else
- // Dbg("gpio connected.");
- }
- pTransactionContext->SendAnswer(eStart);
- //ErrorCodeEnum Error = __OnStart(Error_Succeed);
- //pTransactionContext->SendAnswer(Error);
- }
- virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,CSmartPointer<ITransactionContext> pTransactionContext)
- {
- ErrorCodeEnum Error = __OnStart(Error_Succeed);
- pTransactionContext->SendAnswer(Error);
- }
- virtual void OnPrePause(CSmartPointer<ITransactionContext> pTransactionContext)
- {
- ErrorCodeEnum Error = __OnPause(Error_Succeed);
- pTransactionContext->SendAnswer(Error);
- }
- virtual ErrorCodeEnum __OnStart(ErrorCodeEnum preOperationError)
- {
- //LOG_FUNCTION();
- ////MessageBoxA(0,0,0,0);
- //Init();
- //if (!bInitialized)
- //{
- // Dbg("test.notinit");
- // return Error_Unexpect;
- //}
- //HRESULT hr = m_pCardIss->SetCardInType(CI_CARD_IN_TYPE_ALL);
- //Dbg("setcardintype[%d]",hr);
- //if (hr != Error_Succeed)
- // return ErrorCodeEnum(hr);
- return Error_Succeed;
- }
- virtual ErrorCodeEnum __OnPause(ErrorCodeEnum preOperationError)
- {
- //MessageBoxA(0,0,0,0);
- //Test();
- //TestMagCard();
- //TestRFCard();
- m_fsm.QueryFWBList(NULL, 2);//oiltest
- return Error_Succeed;
- }
- virtual ErrorCodeEnum __OnClose(ErrorCodeEnum preOperationError)
- {
- return Error_Succeed;
- }
- virtual void OnSelfTest(EntityTestEnum eTestType,CSmartPointer<ITransactionContext> pTransactionContext)
- {
- m_fsm.SelfTest(eTestType,pTransactionContext);
- }
- virtual CServerSessionBase* OnNewSession(const char*,const char*)
- {
- return new CardSwiperServerSession(this);
- }
- void Init()
- {
- LOG_FUNCTION();
- if (CreateDevComponent((DeviceBaseClass *&)m_pCardIss) != Error_Succeed)
- {
- Dbg("创建cardiss设备模块失败");
- return;
- }
- //CardSwiperInitParam cardIssInitParam;
- //cardIssInitParam.dwBaudRate = 115200;
- //cardIssInitParam.btPort = 2;
- //HRESULT hr = m_pCardIss->DevOpen(cardIssInitParam);
- //if (hr != Error_Succeed)
- //{
- // Dbg("devopen failed[%d]",hr);
- // return;
- //}
- bInitialized = true;
- }
- //void TestRFCard()
- //{
- // LOG_FUNCTION();
- // HRESULT hr;
- // MessageBoxA(0,0,0,0);
- // hr = m_pCardIss->MoveCard(CI_MOVECARD_RF_POSITION);
- // Dbg("move card 2 rf [%d]",hr);
- // hr = m_pCardIss->ActiveCard();
- // Dbg("activecard[%d]",hr);
- // if (hr != Error_Succeed)
- // return;
- // CmdInfo cmdSend,cmdRecv;
- // BYTE zz = 0x00;
- // BYTE cls = 0x00;
- // BYTE ins = 0xa4;
- // BYTE p1 = 0x04;
- // BYTE p2 = 0x00;
- // BYTE le = NULL;
- // char *pse = "1PAY.SYS.DDF01";
- // BYTE tmpPse = strlen(pse);
- // LPBYTE pData = new BYTE[tmpPse];
- // if (pData == NULL)
- // Dbg("NEW");
- // memcpy(pData,pse,tmpPse);
- // ConstructAPDU(cls,ins,p1,p2,tmpPse,pData,&le);//oiltest &le -> NULL for test
- // cmdSend.dwSize = m_lenAPDU;
- // memcpy(cmdSend.data,m_APDUsendBuf,m_lenAPDU);
- // Dbg("send cmd");
- // hr = m_pCardIss->RFTypeABCommand(cmdSend,cmdRecv);
- // Dbg("cmdrecv hr[%d],len[%d],data[%s]",hr,cmdRecv.dwSize,cmdRecv.data);
- // //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]);
- // //hr = m_pCardIss->OnCommnd(pCmdSend,pCmdRecv);
- // //Dbg("cmdrecv2 hr[%d],len[%d],data[%s]",hr,pCmdRecv->length,pCmdRecv->data);
- // //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]);
- // DevCategoryInfo devInfo;
- // memset(devInfo.szVendor,0,sizeof(devInfo.szVendor));
- // hr = m_pCardIss->GetDevCategory(devInfo);
- // Dbg("dev info[%d][%s]",hr,devInfo.szVendor);
- // hr = m_pCardIss->DeactivateCard();
- // Dbg("deactivated[%d]",hr);
- // hr = m_pCardIss->MoveCard(CI_MOVECARD_FRONT_GATE);
- // Dbg("eject[%d]",hr);
- // if (hr != Error_Succeed)
- // return;
- //}
- void TestMagCard()
- {
- //MessageBoxA(0,0,0,0);
- Dbg("read...");
- //hr = m_pCardSwiper->GetCardAccount(CI_TRACK_1|CI_TRACK_2|CI_TRACK_3,&ppCardData);
- TrackInfo* pTracks = new TrackInfo;
- MagTracks magTracks;
- magTracks.eRange = CI_TRACK_RANGE_1_2_3;
- SpReqAnsContext<CardSwiperService_Read_Req,CardSwiperService_Read_Ans>::Pointer ctx;
- m_fsm.ReadCard(ctx);
- return;
- ErrorCodeEnum hr = Error_Succeed;
- //ErrorCodeEnum hr = m_pCardIss->MagRead(magTracks);
-
- Dbg("read(%d)",hr);
- if (Error_Succeed == hr){
- Dbg("read answer:[%d][%d][%d]",(LPCSTR)pTracks[0].dwSize,
- (LPCSTR)pTracks[1].dwSize,(LPCSTR)pTracks[2].dwSize);
- Dbg("read answer:[%s][%s][%s]",(LPCSTR)pTracks[0].data,
- (LPCSTR)pTracks[1].data,(LPCSTR)pTracks[2].data);
- }
- else
- {
- Dbg("idc_read(%d)",hr);
- }
- Dbg("eject(%d)",hr);
- }
- //void Test()
- //{
- // LOG_FUNCTION();
- // HRESULT hr;
- // //hr = m_pCardIss->DectectCardType();
- // //Dbg("DectectCardType[%d]",hr);
- // //if (hr != Error_Succeed)
- // // return;
- // hr = m_pCardIss->ActiveCard();
- // Dbg("activecard[%d]",hr);
- // if (hr != Error_Succeed)
- // return;
- // CmdInfo cmdSend,cmdRecv;
- // BYTE zz = 0x00;
- // BYTE cls = 0x00;
- // BYTE ins = 0xa4;
- // BYTE p1 = 0x04;
- // BYTE p2 = 0x00;
- // BYTE le = NULL;
- // char *pse = "1PAY.SYS.DDF01";
- // BYTE tmpPse = strlen(pse);
- // LPBYTE pData = new BYTE[tmpPse];
- // if (pData == NULL)
- // Dbg("NEW");
- // memcpy(pData,pse,tmpPse);
- // ConstructAPDU(cls,ins,p1,p2,tmpPse,pData,&le);//oiltest &le -> NULL for test
- // //MessageBoxA(0,0,0,0);
- // cmdSend.dwSize = m_lenAPDU;
- // memcpy(cmdSend.data,m_APDUsendBuf,m_lenAPDU);
- // hr = m_pCardIss->ICCommand(cmdSend,cmdRecv);
- // Dbg("cmdrecv hr[%d],len[%d],data[%s]",hr,cmdRecv.dwSize,cmdRecv.data);
- // //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]);
- // //hr = m_pCardIss->OnCommnd(pCmdSend,pCmdRecv);
- // //Dbg("cmdrecv2 hr[%d],len[%d],data[%s]",hr,pCmdRecv->length,pCmdRecv->data);
- // //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]);
- // DevCategoryInfo devInfo;
- // memset(devInfo.szVendor,0,sizeof(devInfo.szVendor));
- // hr = m_pCardIss->GetDevCategory(devInfo);
- // Dbg("dev info[%d][%s]",hr,devInfo.szVendor);
- // hr = m_pCardIss->DeactivateCard();
- // Dbg("deactivated[%d]",hr);
- // hr = m_pCardIss->MoveCard(CI_MOVECARD_FRONT_GATE);
- // Dbg("eject[%d]",hr);
- // if (hr != Error_Succeed)
- // return;
- //}
- void Read(SpReqAnsContext<CardSwiperService_Read_Req, CardSwiperService_Read_Ans>::Pointer ctx)
- {
- LOG_FUNCTION();
- m_fsm.SetAcceptWaitMore(false);
- CardReadEvent* e = new CardReadEvent();
- e->ctx = ctx;
- m_fsm.PostEventFIFO(e);
- }
- void PreOnline(SpReqAnsContext<CardSwiperService_PreOnline_Req, CardSwiperService_PreOnline_Ans>::Pointer ctx)
- {
- LOG_FUNCTION();
- PreOnlineEvent* e = new PreOnlineEvent();
- e->ctx = ctx;
- m_fsm.PostEventFIFO(e);
- }
- void PostOnline(SpReqAnsContext<CardSwiperService_PostOnline_Req, CardSwiperService_PostOnline_Ans>::Pointer ctx)
- {
- LOG_FUNCTION();
- PostOnlineEvent* e = new PostOnlineEvent();
- e->ctx = ctx;
- m_fsm.PostEventFIFO(e);
- }
- void Exit(SpOnewayCallContext<CardSwiperService_Exit_Info>::Pointer ctx)
- {
- //LOG_FUNCTION();
- FSMEvent *evt = new FSMEvent(USER_EVT_EXIT);
- m_fsm.PostEventFIFO(evt);
- }
- void CancelInsert(SpOnewayCallContext<CardSwiperService_CancelInsert_Info>::Pointer ctx)
- {
- m_fsm.SetAcceptWaitMore(false);
- FSMEvent *evt = new FSMEvent(USER_EVT_CANCELINSERT);
- m_fsm.PostEventFIFO(evt);
- }
- void Eject(SpReqAnsContext<CardSwiperService_Eject_Req, CardSwiperService_Eject_Ans>::Pointer ctx)
- {
- ctx->Answer(Error_Succeed);
- FSMEvent *pEvt = new FSMEvent(USER_EVT_EJECT);
- m_fsm.PostEventFIFO(pEvt);
- }
- void QueryCardInfo(SpReqAnsContext<CardSwiperService_QueryCardInfo_Req, CardSwiperService_QueryCardInfo_Ans>::Pointer ctx)
- {
- LOG_FUNCTION();
- QueryCardInfoEvent* e = new QueryCardInfoEvent();
- e->ctx = ctx;
- m_fsm.PostEventFIFO(e);
- }
- void MagTransferInit(SpReqAnsContext<CardSwiperService_MagTransferInit_Req, CardSwiperService_MagTransferInit_Ans>::Pointer ctx)
- {
- LOG_FUNCTION();
- MagDataTransferInitEvent * e = new MagDataTransferInitEvent();
- e->ctx = ctx;
- m_fsm.PostEventFIFO(e);
- }
- void QueryConnInfo(SpReqAnsContext<CardSwiperService_QueryConnInfo_Req, CardSwiperService_QueryConnInfo_Ans>::Pointer ctx)
- {
- ctx->Ans.connect = m_fsm.QueryConnStatus();
- ctx->Answer(Error_Succeed);
- }
- void InsertWaitMore(SpOnewayCallContext<CardSwiperService_InsertWaitMore_Info>::Pointer ctx)
- {
- m_fsm.SetAcceptWaitMore(true);
- }
- void QueryFWBList(SpReqAnsContext<CardSwiperService_QueryFWBList_Req, CardSwiperService_QueryFWBList_Ans>::Pointer ctx)
- {
- QueryFWBListEvent* pEvt = new QueryFWBListEvent();
- pEvt->ctx = ctx;
- m_fsm.PostEventFIFO(pEvt);
- }
- void BindFWB(SpReqAnsContext<CardSwiperService_BindFWB_Req, CardSwiperService_BindFWB_Ans>::Pointer ctx)
- {
- BindFWBEvent* pEvt = new BindFWBEvent();
- pEvt->ctx = ctx;
- m_fsm.PostEventFIFO(pEvt);
- }
- void GetDevInfo(SpReqAnsContext<CardSwiperService_GetDevInfo_Req, CardSwiperService_GetDevInfo_Ans>::Pointer ctx)
- {
- ctx->Ans.state = m_fsm.GetDevState();
- ctx->Answer(Error_Succeed);
- }
- virtual bool IsService()const{return true;}
- virtual bool IsMultiThread()const{return true;}
- virtual void OnLog(const CAutoArray<CUUID> &SubIDs, const CUUID nLogID, const LogTypeEnum eLogType, const SeverityLevelEnum eLevel,
- const DWORD dwSysError, const DWORD dwUserCode, const DWORD dwEntityInstanceID, const WORD wEntityDevelID,
- const CAutoArray<DWORD> &Param, const char *pszEntityName, const char *pszModuleName, const char *pszMessage);
- virtual void OnSysVarEvent(const char* pszKey,const char* pszValue, const char* pszOldValue, const char* pszEntityName);
- protected:
- private:
- CardSwiperClass* m_pCardIss;
- CCardSwiperFSM m_fsm;
- BOOL bInitialized;
- LONG m_lenAPDU;
- BYTE m_APDUsendBuf[512];
- CUUID m_uuidAccessAuth;
- private:
- void ConstructAPDU(BYTE cls,BYTE ins,BYTE p1,BYTE p2,BYTE lc,LPBYTE data,LPBYTE le)
- {
- LOG_FUNCTION();
- m_lenAPDU = 0;
- m_APDUsendBuf[0] = cls;
- m_APDUsendBuf[1] = ins;
- m_APDUsendBuf[2] = p1;
- m_APDUsendBuf[3] = p2;
- m_lenAPDU = 4;
- if (lc != NULL && data != NULL)
- {
- m_APDUsendBuf[4] = lc;
- m_lenAPDU++;
- if (lc != 0)
- {
- memcpy(m_APDUsendBuf+m_lenAPDU,data,lc);
- m_lenAPDU += lc;
- }
- }
- if (le != NULL)
- m_APDUsendBuf[m_lenAPDU++] = (*le);
- if (!data)
- {
- delete[] data;
- data = NULL;
- }
- Dbg("cmd len[%d]",m_lenAPDU);
- }
- };
|