| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510 |
- #include "stdafx.h"
- #include "IDCertFSM.h"
- #include "GetDevInfoHelper.h"
- #include "EventCode.h"
- //#pragma comment(lib,"IDCerReader.lib")
- #include "libimageproc.h"
- #include "ModuleMix.h"
- #include "IDCertificate_UserErrorCode.h"
- #include "CommDevEntityErrorCode.h"
- //#include <ximabmp.h>
- //#include <ximapng.h>
- #include <iostream>
- #include <fstream>
- #include <stdio.h>
- #include <cv.h>
- #include <cxcore.h>
- #include <highgui.h>
- //#include <Windows.h>
- //#include "DeviceBaseHelper.h"
- #include "DevFSMCommBase.hpp"
- #pragma comment(lib,"user32.lib")
- #define IDCER_INIT_COUNT 3
- #define IDCER_READ_TIMEOUT 60000
- #define IDCER_AUTH_INTERVAL 300
- //oiltmp need to move together?
- int StrBuf2HexBuf(LPCTSTR strBuf, PBYTE* hexBuf)
- {
- int len = strlen(strBuf);
- if (len == 0 || len % 2 != 0)
- return 0;
- BYTE* buf = new BYTE[len / 2];
- if (buf == NULL)
- return 0;
- int j = 0;
- for (int i = 0; i < len;)
- {
- int tmpVal;
- sscanf(strBuf + i, "%2X", &tmpVal);
- buf[j] = tmpVal;
- //buf[j] = char2int(strBuf[i])*16 + char2int(strBuf[i+1]);
- i += 2;
- j++;
- }
- //memcpy(buf,strBuf,len);
- *hexBuf = buf;
- return j;
- }
- int HexBuf2StrBuf(PBYTE hexBuf, char** strBuf, DWORD len)
- {
- char* tmpStr = *strBuf;
- int count = 0;
- for (int i = 0; i < len; ++i)
- {
- sprintf(tmpStr + count, "%0.2X", hexBuf[i]);
- count += 2;
- }
- return 0;
- }
- //Normal/Idle
- void CIDCertFSM::s0_on_entry()
- {
- LOG_FUNCTION();
- m_devState = DEVICE_STATUS_NORMAL;
- GetEntityBase()->GetFunction()->SetUserDefineState(USER_IDCERTIFICATE_IDLE);
- }
- void CIDCertFSM::s0_on_exit()
- {
- LOG_FUNCTION();
- //if (m_pIDCert != NULL) {
- // if (m_pIDCert->DevClose() == Error_Succeed)
- // LOG_TRACE("关闭二代证成功!");
- // else
- // LOG_TRACE("关闭二代证失败!");
- //}
- }
- unsigned int CIDCertFSM::s0_on_event(FSMEvent* pEvt)
- {
- LOG_FUNCTION();
- Dbg("s0 evt %d",pEvt->iEvt);
- switch (pEvt->iEvt)
- {
- case USER_EVT_ERROR:
- pEvt->SetHandled();
- break;
- case USER_EVT_QUIT:
- pEvt->SetHandled();
- Dbg("s0 on event quit");
- break;
- case USER_EVT_READ:
- {
- pEvt->SetHandled();
- ReadEvent* ide = dynamic_cast<ReadEvent *>(pEvt);
- ReadTask* task = new ReadTask(this);
- task->ctx = ide->ctx;
- GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
- }
- break;
- case USER_EVT_READ_EX:
- {
- pEvt->SetHandled();
- ReadExEvent* ide = dynamic_cast<ReadExEvent *>(pEvt);
- ReadExTask* task = new ReadExTask(this);
- task->ctx = ide->ctx;
- GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
- }
- break;
- case USER_EVT_READ_AND_SCAN:
- {
- pEvt->SetHandled();
- ReadAndScanEvent* ide = dynamic_cast<ReadAndScanEvent *>(pEvt);
- ReadAndScanTask* task = new ReadAndScanTask(this);
- task->ctx = ide->ctx;
- GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
- }
- break;
- default:
- break;
- }
- return 0;
- }
- //Reading
- void CIDCertFSM::s1_on_entry()
- {
- LOG_FUNCTION();
- GetEntityBase()->GetFunction()->SetUserDefineState(USER_IDCERTIFICATE_READING);
- }
- void CIDCertFSM::s1_on_exit()
- {
- LOG_FUNCTION();
- }
- unsigned int CIDCertFSM::s1_on_event(FSMEvent *pEvt)
- {
- LOG_FUNCTION();
- Dbg("s1 evt (%d).", pEvt->iEvt);
- int ret = 0;
- switch(pEvt->iEvt)
- {
- case USER_EVT_READ_FINISHED:
- pEvt->SetHandled();
- ret = pEvt->param1;
- break;
- case USER_EVT_CANCEL_READ:
- pEvt->SetHandled();
- Dbg("Set cancel read flag");
- m_bCancelRead = true;
- break;
- case USER_EVT_EXIT:
- pEvt->SetHandled();
- SetExitFlag();
- break;
- case USER_EVT_QUIT:
- pEvt->SetHandled();
- break;
- case USER_EVT_READ_EX_FINISHED:
- pEvt->SetHandled();
- ret = pEvt->param1;
- break;
- case USER_EVT_READ_AND_SCAN_FINISHED:
- pEvt->SetHandled();
- ret = pEvt->param1;
- break;
- default:
- break;
- }
-
- return ret;
- }
- //failed
- void CIDCertFSM::s2_on_entry()
- {
- LOG_FUNCTION();
- m_devState = DEVICE_STATUS_FAULT;
- GetEntityBase()->GetFunction()->SetUserDefineState(USER_IDCERTIFICATE_FAILED);
- m_testResult = Error_InvalidState;
- }
- void CIDCertFSM::s2_on_exit()
- {
- LOG_FUNCTION();
- }
- unsigned int CIDCertFSM::s2_on_event(FSMEvent* e)
- {
- LOG_FUNCTION();
- if (e->iEvt == USER_EVT_QUIT)
- {
- e->SetHandled();
- return 0;
- }
-
- return 0;
- }
- //Eject
- void CIDCertFSM::s3_on_entry()
- {
- }
- void CIDCertFSM::s3_on_exit()
- {
- }
- unsigned int CIDCertFSM::s3_on_event(FSMEvent* e)
- {
- return 0;
- }
- //WaitingFetch
- void CIDCertFSM::s4_on_entry()
- {
- LOG_FUNCTION();
- WaitFetchIDCardTask* task = new WaitFetchIDCardTask(this);
- GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
- }
- void CIDCertFSM::s4_on_exit()
- {
- }
- unsigned int CIDCertFSM::s4_on_event(FSMEvent* pEvt)
- {
- Dbg("wait fetch id card.event %d,%d",pEvt->iEvt,pEvt->param1);
- switch (pEvt->iEvt)
- {
- case USER_EVT_WAIT_FETCH_IDCARD_FINISHED:
- pEvt->SetHandled();
- break;
- default:
- break;
- }
- return 0;
- }
- //Init
- void CIDCertFSM::s5_on_entry()
- {
- LOG_FUNCTION();
- m_devState = DEVICE_STATUS_NOT_READY;
- InitTask* task = new InitTask(this);
- GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
- }
- void CIDCertFSM::s5_on_exit()
- {
- LOG_FUNCTION();
- }
- unsigned int CIDCertFSM::s5_on_event(FSMEvent* pEvt)
- {
- LOG_FUNCTION();
- Dbg("s5 evt %d", pEvt->iEvt);
- int ret = 0;
- switch (pEvt->iEvt)
- {
- case USER_EVT_ERROR:
- pEvt->SetHandled();
- break;
- case USER_EVT_QUIT:
- pEvt->SetHandled();
- Dbg("s5 on event quit");
- break;
- case USER_EVT_INIT_FINISHED:
- {
- pEvt->SetHandled();
- ret = pEvt->param1;
- }
- break;
- default:
- break;
- }
- return ret;
- }
- ErrorCodeEnum CIDCertFSM::GetVendorDllPath(CSimpleStringA &strPath)
- {
- strPath = "termb.dll";
- return Error_Succeed;//oiltest
- CSimpleStringA strDepPath;
- ErrorCodeEnum Error = m_pEntity->GetFunction()->GetPath("Dep", strDepPath);
- if (Error == Error_Succeed) {
- strPath = "termb.dll";//CSimpleStringA::Format("%s\\mod_IDCertificate\\termb.dll", (LPCSTR)strDepPath);
- }
- return Error;
- }
- bool isnostr(const char *str)
- {
- int len = strlen(str);
- if (len == 0)
- return true;
- for (int i = 0; i < len; ++i)
- {
- if (*(str+i) != ' ')
- return false;
- }
- return true;
- }
- ErrorCodeEnum CIDCertFSM::OnInit()
- {
- ZeroMemory(m_devCatInfo.szModel, MAX_DEV_MODEL_LEN);
- ZeroMemory(m_devCatInfo.szType, MAX_DEV_TYPE_LEN);
- ZeroMemory(m_devCatInfo.szVendor, MAX_DEV_VENDOR_LEN);
- return Error_Succeed;
- }
- ErrorCodeEnum CIDCertFSM::OnExit()
- {
- if (m_pIDCert != NULL)
- {
- ErrorCodeEnum err;
- err = m_pIDCert->DevClose();
- LOG_TRACE("OnExit hr(%d)",err);
- }
- if (m_pIDCert)
- {
- ReleaseDevComponent((DeviceBaseClass *&)m_pIDCert);
- m_pIDCert = NULL;
- }
- return Error_Succeed;
- }
- int CIDCertFSM::ReadInfo(SpReqAnsContext<IDCert_Read_Req, IDCert_Read_Ans>::Pointer ctx)
- {
- LOG_FUNCTION();
- DWORD elapsed = 0;
- DWORD dwStart = cvGetTickCount();
- DWORD dwEnd = cvGetTickCount();
- ErrorCodeEnum hr;
- LogEvent(Severity_Middle,LOG_EVT_IDCERTIFICATE_GREEN_ON,"IDCer warning on");
- Dbg("Before readingInfo, do clear job.");
- DeleteZP(Bmp_ZP);
- Dbg("Before readingInfo, finish doing clear job.");
- bool bOpenRF = false,bGetIDCert = false;
- IDCerInfo idInfo;
- memset(&idInfo, 0, sizeof(idInfo));
- m_bCancelRead = false;
- m_bWaitReadMore = false;
- m_bReading = true;
- m_bExit = false;
- while(elapsed < IDCER_READ_TIMEOUT && !bGetIDCert && !m_bCancelRead)
- {
- if (m_bExit)
- {
- Dbg("exit");
- break;
- }
- if (m_bWaitReadMore)
- {
- dwStart = cvGetTickCount();
- m_bWaitReadMore = false;
- }
- if (!bOpenRF)
- {
- hr = m_pIDCert->IDCerRFControl(true);
- if (Error_Succeed == hr)
- {
- Dbg("open rf succeed.");
- }
- else
- {
- Dbg("open rf failed. %d",hr);
- dwEnd = cvGetTickCount();
- elapsed = (dwEnd - dwStart) / cvGetTickFrequency() * 1000;
- //if cannot open rf in 10s ,quit
- // Adapt to Desk2S [4/12/2018 15:05 @Gifur]
- /*if (elapsed > 10000)
- break;
- else*/{
- Sleep(IDCER_AUTH_INTERVAL);
- continue;
- }
- }
- }
- bOpenRF = true;
- Sleep(IDCER_AUTH_INTERVAL);
- hr = m_pIDCert->IDCerAuthenticate();
- if (Error_Succeed != hr)
- {
- DevErrorInfo devErrInfo;
- m_pIDCert->GetLastErr(devErrInfo);
- Dbg("IDCerAuthenticate failed %d,%s.", hr, devErrInfo.szErrMsg);
- dwEnd = cvGetTickCount();
- elapsed = (dwEnd - dwStart) / cvGetTickFrequency() * 1000;
- continue;
- }
- else
- {
- Dbg("auth succeed.");
- hr = m_pIDCert->IDCerGetData(idInfo);
- if (hr != Error_Succeed)
- {
- LogErrMsg("ReadInfo::IDCerGetData", hr, MEC_DEVAPI_IDCER_IDCerGetData);
- }
- else
- {
- LOG_TRACE("read succeed.");
- LogEvent(Severity_Middle,LOG_EVT_IDCERTIFICATE_OP,"IDCertifacate op.");
- //oiltest
- ctx->Ans.name = idInfo.name.data;
- //ctx->Ans.name = "我.们我.们我·们我·们爱邱总";
- //ctx->Ans.name = "张云虎";
- ctx->Ans.sex = idInfo.sex.data;
- ctx->Ans.nation = idInfo.nation.data;
- ctx->Ans.birthday = idInfo.birthday.data;
- ctx->Ans.address = idInfo.address.data;
- //oiltest
- ctx->Ans.idcode = idInfo.idno.data;
- //oilyang@20180309 根据俞根伟,生产上存在身份证异常为'x'的问题,统一进行转换
- if (ctx->Ans.idcode[ctx->Ans.idcode.GetLength() - 1] == 'x')
- {
- Dbg("x to X");
- ctx->Ans.idcode[ctx->Ans.idcode.GetLength() - 1] = 'X';
- }
- //ctx->Ans.idcode = "370285198804025014";
- ctx->Ans.department = idInfo.department.data;
- ctx->Ans.startdate = idInfo.startDate.data;
- ctx->Ans.enddate = idInfo.endDate.data;
- //ctx->Ans.startdate = "2015.08.06";
- //ctx->Ans.enddate = "2035.08.06";
- //oiltest
- /*Dbg("Name:%s,Sex:%s,Nation:%s,Birthday:%s,StartDate:%s,EndDate:%s,IDNo:%s****%s,Address:%s,"
- ,idInfo.name.data
- ,idInfo.sex.data
- ,idInfo.nation.data
- ,idInfo.birthday.data
- ,idInfo.startDate.data
- ,idInfo.endDate.data
- ,(const char*)ctx->Ans.idcode.SubString(0, 4), (const char*)ctx->Ans.idcode.SubString(ctx->Ans.idcode.GetLength() - 1, 1)
- ,idInfo.address.data);*/
- Dbg("Sex:%s,Nation:%s,EndDate:%s,IDNo:%s****%s,"
- ,idInfo.sex.data
- ,idInfo.nation.data
- ,idInfo.endDate.data
- ,(const char*)ctx->Ans.idcode.SubString(0, 4), (const char*)ctx->Ans.idcode.SubString(ctx->Ans.idcode.GetLength() - 1, 1));
- Dbg("to get photo");
- hr = GetPngBlob(ctx->Ans.photodata,true);
- if (hr != Error_Succeed)
- {
- bGetIDCert = false;
- LOG_TRACE("get photo failed.");
- break;
- }
- bGetIDCert = true;
- break;
- }
- }
- dwEnd = cvGetTickCount();
- elapsed = (dwEnd - dwStart) / cvGetTickFrequency() * 1000;
- }
- m_bExit = false;
- m_bReading = false;
- LogEvent(Severity_Middle,LOG_EVT_IDCERTIFICATE_GREEN_OFF,"IDCer warning off");
- Dbg("after close light");
- if (bGetIDCert)
- ctx->Answer(Error_Succeed);
- else if (m_bCancelRead)
- ctx->Answer(Error_Cancel);
- else if (elapsed >= IDCER_READ_TIMEOUT)
- ctx->Answer(Error_TimeOut);
- else
- ctx->Answer(Error_Unexpect, AlarmDECToBusiness());
- //if (!bGetIDCert)
- //LogWarn(Severity_Low,Error_DevCommFailed,LOG_WARN_IDCERTIFICATE_ACTION,"IDCertificate read failed.");
- Dbg("after send ack");
- //if (!bOpenRF)
- //{
- // LOG_TRACE("rf failed.");
- // return 1;
- //}
- if(bOpenRF) {
- hr = m_pIDCert->IDCerRFControl(false);
- if (hr != Error_Succeed)
- {
- Dbg("rf close failed(%d).",hr);
- return 1;
- }
- }
- return 0;
- }
- int CIDCertFSM::ReadInfoEx(SpReqAnsContext<IDCert_ReadEx_Req, IDCert_ReadEx_Ans>::Pointer ctx)
- {
- LOG_FUNCTION();
- DWORD elapsed = 0;
- DWORD dwStart = cvGetTickCount();
- DWORD dwEnd = cvGetTickCount();
- ErrorCodeEnum eErr;
- LogEvent(Severity_Middle,LOG_EVT_IDCERTIFICATE_GREEN_ON,"IDCer warning on");
- Dbg("Before readingInfo, do clear job.");
- DeleteZP(Bmp_ZP);
- Dbg("Before readingInfo, finish doing clear job.");
- bool bOpenRF = false, bGetIDCert = false;
- IDCerInfoEx idInfo;
- ZeroMemory(&idInfo, sizeof(IDCerInfoEx));
- m_bCancelRead = false;
- m_bWaitReadMore = false;
- m_bReading = true;
- m_bExit = false;
- while (elapsed < IDCER_READ_TIMEOUT && !bGetIDCert && !m_bCancelRead)
- {
- if (m_bExit)
- {
- Dbg("exit");
- break;
- }
- if (m_bWaitReadMore)
- {
- dwStart = cvGetTickCount();
- m_bWaitReadMore = false;
- }
- if (!bOpenRF)
- {
- eErr = m_pIDCert->IDCerRFControl(true);
- if (Error_Succeed == eErr)
- {
- Dbg("open rf succeed.");
- }
- else
- {
- Dbg("open rf failed.");
- dwEnd = cvGetTickCount();
- elapsed = (dwEnd - dwStart) / cvGetTickFrequency() * 1000;
- //if cannot open rf in 10s ,quit
- /* if (elapsed > 10000)
- break;
- else*/{
- Sleep(IDCER_AUTH_INTERVAL);
- continue;
- }
- }
- }
- bOpenRF = true;
- Sleep(IDCER_AUTH_INTERVAL);
- eErr = m_pIDCert->IDCerAuthenticate();//卡认证
- if (Error_Succeed != eErr)
- {
- //LOG_TRACE("auth failed.");
- DevErrorInfo devErrInfo;
- m_pIDCert->GetLastErr(devErrInfo);
- Dbg("IDCerAuthenticate failed %d,%s.", eErr, devErrInfo.szErrMsg);
- dwEnd = cvGetTickCount();
- elapsed = (dwEnd - dwStart) / cvGetTickFrequency() * 1000;
- continue;
- }
- else
- {
- Dbg("auth succeed.");
- eErr = m_pIDCert->IDCerGetDataEx(idInfo);
- if (eErr != Error_Succeed)
- {
- LogErrMsg("ReadInfoEx::IDCerGetDataEx", eErr, MEC_DEVAPI_IDCER_IDCerGetDataEx,TRUE);
- }
- else
- {
- LOG_TRACE("read succeed.");
- LogEvent(Severity_Middle, LOG_EVT_IDCERTIFICATE_OP, "IDCertifacate op.");
- ctx->Ans.name = idInfo.name.data;
- ctx->Ans.sex = idInfo.sex.data;
- ctx->Ans.nation = idInfo.nation.data;
- ctx->Ans.birthday = idInfo.birthday.data;
- ctx->Ans.address = idInfo.address.data;
- ctx->Ans.idcode = idInfo.idno.data;
- //oilyang@20180309 根据俞根伟,生产上存在身份证异常为'x'的问题,统一进行转换
- if (ctx->Ans.idcode[ctx->Ans.idcode.GetLength() - 1] == 'x')
- {
- Dbg("x to X");
- ctx->Ans.idcode[ctx->Ans.idcode.GetLength() - 1] = 'X';
- }
- ctx->Ans.department = idInfo.department.data;
- ctx->Ans.startdate = idInfo.startDate.data;
- ctx->Ans.enddate = idInfo.endDate.data;
- ctx->Ans.englishname = idInfo.englishName.data;
- ctx->Ans.nationality = idInfo.nationality.data;
- ctx->Ans.idversion = idInfo.idVersion.data;
- ctx->Ans.idtype = idInfo.idType.data;
- ctx->Ans.reserved = idInfo.reserved.data;
- ctx->Ans.startdate = idInfo.startDate.data;
- /*Dbg("Name:%s,Sex:%s,Nation:%s,Birthday:%s,StartDate:%s,EndDate:%s,IDNo:%s****%s,Address:%s,IDType:%s,Department:%s,EnglishName:%s,Nationality:%s"
- , (const char*)ctx->Ans.name, (const char*)ctx->Ans.sex, (const char*)ctx->Ans.nation
- , (const char*)ctx->Ans.birthday
- , (const char*)ctx->Ans.startdate, (const char*)ctx->Ans.enddate
- , (const char*)ctx->Ans.idcode.SubString(0, 4), (const char*)ctx->Ans.idcode.SubString(ctx->Ans.idcode.GetLength() - 1, 1)
- , (const char*)ctx->Ans.address
- , (const char*)ctx->Ans.idtype
- , (const char*)ctx->Ans.department
- , (const char*)ctx->Ans.englishname , (const char*)ctx->Ans.nationality);*/
- Dbg("Sex:%s,Nation:%s,EndDate:%s,IDNo:%s****%s,IDType:%s"
- ,(const char*)ctx->Ans.sex, (const char*)ctx->Ans.nation
- ,(const char*)ctx->Ans.enddate
- , (const char*)ctx->Ans.idcode.SubString(0, 4), (const char*)ctx->Ans.idcode.SubString(ctx->Ans.idcode.GetLength() - 1, 1)
- , (const char*)ctx->Ans.idtype);
- Dbg("to get photo");
- eErr = GetPngBlob(ctx->Ans.photodata,true);
- if (eErr != Error_Succeed)
- {
- bGetIDCert = false;
- LOG_TRACE("get photo failed.");
- break;
- }
- bGetIDCert = true;
- break;
- }
- }
- dwEnd = cvGetTickCount();
- elapsed = (dwEnd - dwStart) / cvGetTickFrequency() * 1000;
- }
- m_bExit = false;
- m_bReading = false;
- LogEvent(Severity_Middle, LOG_EVT_IDCERTIFICATE_GREEN_OFF, "IDCer warning off");
- Dbg("after close light");
- //if (!bGetIDCert)
- //LogWarn(Severity_Low, Error_DevCommFailed, LOG_WARN_IDCERTIFICATE_ACTION, "IDCertificate read failed.");
-
- int pos = 99;
- ErrorCodeEnum eErr1 =m_pIDCert->QueryCardPos(pos);
- if (eErr1 == Error_NotImpl)
- {
- Dbg("old version.no need position.");
- }
- else
- {
- switch (pos)
- {
- case 1:
- eErr1 = m_pIDCert->IDCerRFControl(false);
- if (eErr1 != Error_Succeed)
- m_pIDCert->ForceIDEject();
- break;
- case 2:
- break;
- case 0:
- default:
- break;
- }
- //查询卡片位置以决定跳转
- eErr1 = m_pIDCert->QueryCardPos(pos);
- if (eErr1 == Error_Succeed)
- {
- Dbg("pos:%d",pos);
- /*if (pos == 2)
- return 4;*/
- }
- }
- if (!(eErr1 == Error_Succeed && pos == 2))
- {
- //oilyang@20180531 add for close enter card
- m_pIDCert->IDCerRFControl(false); //这里基本不耗时,凯欣达大机耗时较长
- }
- //zjw@20191219 处理完相关操作再返回给前端,防止状态还未跳转又发起流程
- if (bGetIDCert)
- ctx->Answer(Error_Succeed);
- else if (m_bCancelRead)
- ctx->Answer(Error_Cancel);
- else if (elapsed >= IDCER_READ_TIMEOUT)
- ctx->Answer(Error_TimeOut);
- else
- ctx->Answer(Error_Unexpect);
- if (eErr1 == Error_Succeed && pos == 2)
- {
- return 4;
- }
- return 0;
- }
- int CIDCertFSM::ReadAndScan(SpReqAnsContext<IDCert_ReadAndScan_Req, IDCert_ReadAndScan_Ans>::Pointer ctx)
- {
- LOG_FUNCTION();
- DWORD elapsed = 0;
- DWORD dwStart = cvGetTickCount();
- DWORD dwEnd = cvGetTickCount();
- ErrorCodeEnum eErr;
- LogEvent(Severity_Middle, LOG_EVT_IDCERTIFICATE_GREEN_ON, "IDCer warning on");
- Dbg("Before readingInfo, do clear job.");
- DeleteZP(Bmp_ZP|Bmp_SCAN);
- Dbg("Before readingInfo, finish doing clear job.");
- bool bOpenRF = false, bGetIDCert = false;
- IDCerInfoEx idInfo;
- IDCerInfo idInfoOld;
- memset(&idInfo, 0, sizeof(idInfo));
- memset(&idInfoOld, 0, sizeof(idInfoOld));
- m_bCancelRead = false;
- m_bWaitReadMore = false;
- m_bReading = true;
- m_bExit = false;
- while (elapsed < IDCER_READ_TIMEOUT && !bGetIDCert && !m_bCancelRead)
- {
- if (m_bExit)
- {
- Dbg("exit");
- break;
- }
- if (m_bWaitReadMore)
- {
- dwStart = cvGetTickCount();
- m_bWaitReadMore = false;
- }
- if (!bOpenRF)
- {
- eErr = m_pIDCert->IDCerRFControl(true);
- if (Error_Succeed == eErr)
- {
- Dbg("open rf succeed.");
- }
- else
- {
- Dbg("open rf failed.");
- dwEnd = cvGetTickCount();
- elapsed = (dwEnd - dwStart) / cvGetTickFrequency() * 1000;
- //if cannot open rf in 10s ,quit
- /* if (elapsed > 10000)
- break;
- else*/ {
- Sleep(IDCER_AUTH_INTERVAL);
- continue;
- }
- }
- }
- bOpenRF = true;
- Sleep(IDCER_AUTH_INTERVAL);
- eErr = m_pIDCert->IDCerAuthenticate();
- if (Error_Succeed != eErr)
- {
- DevErrorInfo devErrInfo;
- m_pIDCert->GetLastErr(devErrInfo);
- Dbg("IDCerAuthenticate failed %d,%s.", eErr, devErrInfo.szErrMsg);
- dwEnd = cvGetTickCount();
- elapsed = (dwEnd - dwStart) / cvGetTickFrequency() * 1000;
- continue;
- }
- else
- {
- Dbg("auth succeed.");
- eErr = m_pIDCert->IDCerGetDataEx(idInfo);
- if (eErr == Error_NotImpl)
- {
- Dbg("to call old interface");
- eErr = m_pIDCert->IDCerGetData(idInfoOld);
- if (eErr != Error_Succeed)
- {
- LogErrMsg("ReadAndScan::IDCerGetData", eErr, MEC_DEVAPI_IDCER_IDCerGetData);
- }
- else
- {
- Dbg("read succeed.");
- LogEvent(Severity_Middle, LOG_EVT_IDCERTIFICATE_OP, "IDCertifacate op.");
- //oiltest
- ctx->Ans.name = idInfoOld.name.data;
- //ctx->Ans.name = "我.们我.们我·们我·们爱邱总";
- //ctx->Ans.name = "张云虎";
- ctx->Ans.sex = idInfoOld.sex.data;
- ctx->Ans.nation = idInfoOld.nation.data;
- ctx->Ans.birthday = idInfoOld.birthday.data;
- ctx->Ans.address = idInfoOld.address.data;
- //oiltest
- ctx->Ans.idcode = idInfoOld.idno.data;
- //oilyang@20180309 根据俞根伟,生产上存在身份证异常为'x'的问题,统一进行转换
- if (ctx->Ans.idcode[ctx->Ans.idcode.GetLength() - 1] == 'x')
- {
- Dbg("x to X");
- ctx->Ans.idcode[ctx->Ans.idcode.GetLength() - 1] = 'X';
- }
- //ctx->Ans.idcode = "370285198804025014";
- ctx->Ans.department = idInfoOld.department.data;
- ctx->Ans.startdate = idInfoOld.startDate.data;
- ctx->Ans.enddate = idInfoOld.endDate.data;
- //Dbg("nation: %s",(const char*)ctx->Ans.name);
- /*Dbg("Name:%s,Sex:%s,Nation:%s,Birthday:%s,StartDate:%s,EndDate:%s,IDNo:%s****%s,Address:%s,"
- ,idInfoOld.name.data
- ,idInfoOld.sex.data
- ,idInfoOld.nation.data
- ,idInfoOld.birthday.data
- ,idInfoOld.startDate.data
- ,idInfoOld.endDate.data
- ,(const char*)ctx->Ans.idcode.SubString(0, 4), (const char*)ctx->Ans.idcode.SubString(ctx->Ans.idcode.GetLength() - 1, 1)
- ,idInfoOld.address.data);*/
- Dbg("Sex:%s,Nation:%s,EndDate:%s,IDNo:%s****%s,"
- ,idInfoOld.sex.data
- ,idInfoOld.nation.data
- ,idInfoOld.endDate.data
- ,(const char*)ctx->Ans.idcode.SubString(0, 4), (const char*)ctx->Ans.idcode.SubString(ctx->Ans.idcode.GetLength() - 1, 1));
- Dbg("to get photo");
- GetPngBlobEx(ctx->Ans.headphoto, "zp");
- eErr = GetPngBlob(ctx->Ans.photodata, true);
- if (eErr != Error_Succeed)
- {
- bGetIDCert = false;
- LOG_TRACE("get photo failed.");
- break;
- }
- bGetIDCert = true;
- break;
- }
- }
- else
- {
- if (eErr != Error_Succeed)
- {
- LogErrMsg("ReadAndScan::IDCerGetDataEx", eErr, MEC_DEVAPI_IDCER_IDCerGetDataEx,TRUE);
- }
- else
- {
- LOG_TRACE("read succeed.");
- LogEvent(Severity_Middle, LOG_EVT_IDCERTIFICATE_OP, "IDCertifacate op.");
- ctx->Ans.name = idInfo.name.data;
- ctx->Ans.sex = idInfo.sex.data;
- ctx->Ans.nation = idInfo.nation.data;
- ctx->Ans.birthday = idInfo.birthday.data;
- ctx->Ans.address = idInfo.address.data;
- ctx->Ans.idcode = idInfo.idno.data;
- //oilyang@20180309 根据俞根伟,生产上存在身份证异常为'x'的问题,统一进行转换
- if (ctx->Ans.idcode[ctx->Ans.idcode.GetLength() - 1] == 'x')
- {
- Dbg("x to X");
- ctx->Ans.idcode[ctx->Ans.idcode.GetLength() - 1] = 'X';
- }
- ctx->Ans.department = idInfo.department.data;
- ctx->Ans.startdate = idInfo.startDate.data;
- ctx->Ans.enddate = idInfo.endDate.data;
- ctx->Ans.englishname = idInfo.englishName.data;
- ctx->Ans.nationality = idInfo.nationality.data;
- ctx->Ans.idversion = idInfo.idVersion.data;
- ctx->Ans.idtype = idInfo.idType.data;
- ctx->Ans.reserved = idInfo.reserved.data;
- ctx->Ans.startdate = idInfo.startDate.data;
- eErr = m_pIDCert->ScanIDAndSaveImage();
- ctx->Ans.hasscan = 0;
- if (eErr == Error_Succeed)
- {
- //idfront.bmp" and "idback.bmp
- ErrorCodeEnum eErrFront, eErrBack;
- eErrFront = GetPngBlobEx(ctx->Ans.frontphoto, "idfront");
- eErrBack = GetPngBlobEx(ctx->Ans.backphoto, "idback", true);
- if (eErrFront == Error_Succeed && eErrBack == Error_Succeed)
- ctx->Ans.hasscan = 1;
- }
- GetPngBlobEx(ctx->Ans.headphoto, "zp");
- /*Dbg("Name:%s,Sex:%s,Nation:%s,Birthday:%s,StartDate:%s,EndDate:%s,IDNo:%s****%s,Address:%s,IDType:%s,Department:%s,EnglishName:%s,Nationality:%s"
- , (const char*)ctx->Ans.name, (const char*)ctx->Ans.sex, (const char*)ctx->Ans.nation
- , (const char*)ctx->Ans.birthday
- , (const char*)ctx->Ans.startdate, (const char*)ctx->Ans.enddate
- , (const char*)ctx->Ans.idcode.SubString(0, 4), (const char*)ctx->Ans.idcode.SubString(ctx->Ans.idcode.GetLength() - 1, 1)
- , (const char*)ctx->Ans.address
- , (const char*)ctx->Ans.idtype
- , (const char*)ctx->Ans.department
- , (const char*)ctx->Ans.englishname , (const char*)ctx->Ans.nationality);*/
- Dbg("Sex:%s,Nation:%s,EndDate:%s,IDNo:%s****%s,IDType:%s"
- , (const char*)ctx->Ans.sex, (const char*)ctx->Ans.nation
- , (const char*)ctx->Ans.enddate
- , (const char*)ctx->Ans.idcode.SubString(0, 4), (const char*)ctx->Ans.idcode.SubString(ctx->Ans.idcode.GetLength() - 1, 1)
- , (const char*)ctx->Ans.idtype);
- Dbg("to get photo");
- eErr = GetPngBlob(ctx->Ans.photodata, true);
- if (eErr != Error_Succeed)
- {
- bGetIDCert = false;
- LOG_TRACE("get photo failed.");
- break;
- }
- bGetIDCert = true;
- break;
- }
- }
- }
- dwEnd = cvGetTickCount();
- elapsed = (dwEnd - dwStart) / cvGetTickFrequency() * 1000;
- }
-
- m_bExit = false;
- m_bReading = false;
- LogEvent(Severity_Middle, LOG_EVT_IDCERTIFICATE_GREEN_OFF, "IDCer warning off");
- Dbg("after close light");
- Dbg("ans size:%d",sizeof(ctx->Ans));
- Dbg("%d",FIELD_OFFSET(IDCertificate::IDCertService_ReadAndScan_Ans, reserved));
-
- //if (!bGetIDCert)
- //LogWarn(Severity_Low, Error_DevCommFailed, LOG_WARN_IDCERTIFICATE_ACTION, "IDCertificate read failed.");
- int pos = 99;
- ErrorCodeEnum eErr1 = m_pIDCert->QueryCardPos(pos);
- if (eErr1 == Error_NotImpl)
- {
- Dbg("old version.no need position.");
- }
- else
- {
- switch (pos)
- {
- case 1:
- eErr1 = m_pIDCert->IDCerRFControl(false);
- if (eErr1 != Error_Succeed)
- m_pIDCert->ForceIDEject();
- break;
- case 2:
- break;
- case 0:
- default:
- break;
- }
- //查询卡片位置以决定跳转
- eErr1 = m_pIDCert->QueryCardPos(pos);
- if (eErr1 == Error_Succeed)
- {
- Dbg("pos:%d", pos);
- /*if (pos == 2)
- return 4;*/
- }
- }
- if (!(eErr1 == Error_Succeed && pos == 2))
- {
- //oilyang@20180531 add for close enter card
- m_pIDCert->IDCerRFControl(false); //这里基本不耗时,凯欣达新大机耗时较长(先改我们流程解决,后续需要厂商优化这里)
- }
- //zjw@20191219 处理完相关操作再返回给前端,防止状态还未跳转又发起流程
- if (bGetIDCert)
- ctx->Answer(Error_Succeed);
- else if (m_bCancelRead)
- ctx->Answer(Error_Cancel);
- else if (elapsed >= IDCER_READ_TIMEOUT)
- ctx->Answer(Error_TimeOut);
- else
- ctx->Answer(Error_Unexpect);
- if (eErr1 == Error_Succeed && pos == 2)
- {
- return 4;
- }
- return 0;
- }
- #if 0
- ErrorCodeEnum CIDCertFSM::ConvertBmpToPng(CSimpleStringA &strBmpPath, CSimpleStringA &strPngPath)
- {
- CxImage image;
- if (image.Load((LPCSTR)strBmpPath, CXIMAGE_FORMAT_BMP)) {
- if (image.Save((LPCSTR)strPngPath, CXIMAGE_FORMAT_PNG)) {
- return Error_Succeed;
- } else {
- LOG_TRACE("save %s failed!", (LPCSTR)strPngPath);
- }
- } else {
- LOG_TRACE("load %s failed!", (LPCSTR)strBmpPath);
- }
- return Error_IO;
- }
- #endif
- ErrorCodeEnum CIDCertFSM::GetPngBlob(CBlob &data, bool bClear)
- {
- CSimpleStringA strPath,strBkPath,strBmpPath,strPngPath,strTxtPath,strWltPath;
- ErrorCodeEnum err;
- err = m_pEntity->GetFunction()->GetPath("Dep", strPath);
- if (err != Error_Succeed)
- {
- Dbg("Get path failed.");
- return Error_Param;
- }
- strBkPath = strPath + "\\bk.PNG";
- strBmpPath = strPath + "\\zp.bmp";
- strPngPath = strPath + "\\zp.JPEG";
- strTxtPath = strPath + "\\wz.txt";
- strWltPath = strPath + "\\xp.wlt";
- //ErrorCodeEnum Error = ConvertBmpToPng(strBmpPath, strPngPath);
- Dbg("photo change: %s,%s,%s",(LPCSTR)strBkPath,(LPCSTR)strBmpPath,(LPCSTR)strPngPath);
- bool bResult = imageprocess((char*)strBmpPath.GetData(),(char*)strPngPath.GetData());
- Dbg("photo changed");
- if (!bResult)
- {
- Dbg("photo changed failed.");
- return Error_Unexpect;
- }
- //if (Error == Error_Succeed) {
- FILE *fp = fopen(strPngPath, "rb");
- if (fp) {
- fseek(fp, 0, SEEK_END);
- long flen = ftell(fp);
- fseek(fp, 0, SEEK_SET);
- data.Alloc(flen);
- fread(data.m_pData, 1, flen, fp);
- fclose(fp);
- //DeleteFileA(strTxtPath);
- //DeleteFileA(strWltPath);
- } else {
- LOG_TRACE("fopen %s failed!", (LPCSTR)strPngPath);
- err = Error_IO;
- }
- //}
- Dbg("After ReadInfo, do clear job.");
- if (bClear)
- DeleteZP(Bmp_ZP);
- Dbg("After ReadInfo, finish doing clear job.");
- return err;
- }
- ErrorCodeEnum CIDCertFSM::GetPngBlobEx(CBlob &data, CSimpleStringA fileNamePrefix, bool bClear)
- {
- CSimpleStringA strPath;
- ErrorCodeEnum eErr;
- eErr = m_pEntity->GetFunction()->GetPath("Dep", strPath);
- if (eErr != Error_Succeed)
- {
- Dbg("Get path failed.");
- if (bClear)
- DeleteZP(Bmp_SCAN);
- return Error_Param;
- }
- strPath = strPath + "\\" + fileNamePrefix;
- Dbg("to load [%s.bmp",(const char*)strPath);
- IplImage *src = cvLoadImage(strPath + ".bmp");
- if (!src)
- {
- Dbg("read file %s.bmp failed.err:%d", (const char*)strPath, GetLastError());
- if (bClear)
- DeleteZP(Bmp_SCAN);
- return Error_Unexpect;
- }
- cvSaveImage(strPath + ".jpg", src);
- FILE *fp = fopen(strPath + ".jpg", "rb");
- if (fp) {
- fseek(fp, 0, SEEK_END);
- long flen = ftell(fp);
- fseek(fp, 0, SEEK_SET);
- data.Alloc(flen);
- Dbg("alloc data:%d",flen);
- fread(data.m_pData, 1, flen, fp);
- fclose(fp);
- if (bClear)
- DeleteZP(Bmp_SCAN);
- return Error_Succeed;
- }
- else {
- Dbg("fopen %s failed!", (const char*)strPath);
- if (bClear)
- DeleteZP(Bmp_SCAN);
- return Error_IO;
- }
- }
- void CIDCertFSM::SelfTest(EntityTestEnum eTestType,CSmartPointer<ITransactionContext> pTransactionContext)
- {
- //for simple
- pTransactionContext->SendAnswer(m_testResult);
- }
- ErrorCodeEnum CIDCertFSM::DeleteFileIfExisted(LPCTSTR fileName)
- {
- if(strlen(fileName) == 0 || strchr(fileName, (int)'*') != NULL)
- {
- Dbg("Invalid or empty fileName(%s)", fileName);
- return Error_Param;
- }
- CSimpleStringA strPath, strAimPath;
- ErrorCodeEnum erroCode = m_pEntity->GetFunction()->GetPath("Dep", strPath);
- strAimPath = strPath + "\\" + fileName;
- //WIN32_FIND_DATA findData;
- ifstream file(strAimPath);
- if(file)
- {
- if(remove((LPCTSTR)strAimPath) != 0)
- {
- Dbg("DeleteFile(%s) suc.", (LPCTSTR)strAimPath);
- return Error_Succeed;
- }
- else
- {
- Dbg("DeleteFile(%s) failed LastError(%d).", (LPCTSTR)strAimPath, GetLastError());
- return Error_Unexpect;
- }
- }
- if(GetLastError() == ERROR_FILE_NOT_FOUND)
- {
- return Error_Succeed;
- }
- file.close();
- Dbg("DeleteFile(%s) Unexpect GetLastError(%d).", (LPCTSTR)strAimPath, GetLastError());
- return Error_Unexpect;
- }
- ErrorCodeEnum CIDCertFSM::GetDevCatInfo(DevCategoryInfo &devInfo, CSimpleStringA& devType)
- {
- LOG_FUNCTION();
- if (m_bRVCIL)
- {
- ZeroMemory(m_devCatInfo.szModel, MAX_DEV_MODEL_LEN);
- ZeroMemory(m_devCatInfo.szType, MAX_DEV_TYPE_LEN);
- ZeroMemory(m_devCatInfo.szVendor, MAX_DEV_VENDOR_LEN);
- return Error_Succeed;
- }
- Dbg("DevCategoryInfo len:%d,%d,%d", strlen(m_devCatInfo.szModel), strlen(m_devCatInfo.szType), strlen(m_devCatInfo.szVendor));
- strncpy(devInfo.szModel, m_devCatInfo.szModel, (MAX_DEV_MODEL_LEN >strlen(m_devCatInfo.szModel)) ? strlen(m_devCatInfo.szModel) : MAX_DEV_MODEL_LEN);
- strncpy(devInfo.szType, m_devCatInfo.szType, (MAX_DEV_TYPE_LEN >strlen(m_devCatInfo.szType)) ? strlen(m_devCatInfo.szType) : MAX_DEV_TYPE_LEN);
- strncpy(devInfo.szVendor, m_devCatInfo.szVendor, (MAX_DEV_VENDOR_LEN >strlen(m_devCatInfo.szVendor)) ? strlen(m_devCatInfo.szVendor) : MAX_DEV_VENDOR_LEN);
- devType = m_devVer;//适配器版本号
- return Error_Succeed;
- }
- int CIDCertFSM::WaitFetchIDCard()
- {
- LOG_FUNCTION();
- ErrorCodeEnum eErr = Error_Unexpect;
- int pos = 99;
- //DWORD64 dwStart = GetTickCount64();
- DWORD64 dwStart = cvGetTickCount();
- DWORD64 dwEnd = 0;
- while (((dwEnd - dwStart) / cvGetTickFrequency()) * 1000< 120 * 1000)//wait for 120 seconds
- {
- eErr = m_pIDCert->QueryCardPos(pos);
- if (eErr == Error_Succeed && pos == 0)
- {
- Dbg("身份证被取回");
- return 0;
- }
- Sleep(1000);
- }
- // BugFix [4/16/2020 14:27 @Gifur]
- return -1;
- }
- void CIDCertFSM::DeleteZP(int type)
- {
- if ((type&Bmp_ZP) == Bmp_ZP)
- {
- Dbg("to delete zp...");
- DeleteFileIfExisted("zp.JPEG");
- DeleteFileIfExisted("zp.jpg");
- DeleteFileIfExisted("zp.bmp"); //All
- DeleteFileIfExisted("wz.txt"); //Sankyo
- DeleteFileIfExisted("xp.wlt");
- DeleteFileIfExisted("ImgTemp.bmp"); //Nantian
- DeleteFileIfExisted("BP8913_ID.bmp"); //Nantian
- }
- if ((type&Bmp_SCAN) == Bmp_SCAN)
- {
- Dbg("to delete id...");
- DeleteFileIfExisted("idfront.bmp");
- DeleteFileIfExisted("idback.bmp");
- DeleteFileIfExisted("idfront.jpg");
- DeleteFileIfExisted("idback.jpg");
- }
- }
- int CIDCertFSM::ReadHZCard(SpReqAnsContext<IDCert_ReadEx_Req, IDCert_ReadEx_Ans>::Pointer ctx)
- {
- //oiltest@20180613 for songrui hz
- //ErrorCodeEnum eErr = m_pIDCert->DevClose();
- //Dbg("before read card,DevClose result:%d", eErr);
- //eErr = ReleaseDevComponent((DeviceBaseClass *&)m_pIDCert);
- //Dbg("before read card,ReleaseDevComponent result:%d", eErr);
- //HANDLE itc = device_open("USB", 0, 9600);
- //if (itc > 0)
- //{
- // BYTE type;
- // PBYTE pID = new BYTE[10];
- // ZeroMemory(pID, 10);
- // int ret = rf_card(itc, 0xfffe, &type, pID);
- // if (ret == 0)
- // {
- // memcpy(ctx->Ans.photodata.m_pData, pID, 4);
- // ctx->Answer(Error_Succeed);
- // Dbg("read card ok.");
- // }
- // else
- // {
- // Dbg("read card failed %d",ret);
- // ctx->Answer(Error_Unexpect);
- // }
- // device_close(itc);
- //}
- //else
- // Dbg("device_open failed result:%x", itc);
- //eErr = CreateDevComponent((DeviceBaseClass *&)m_pIDCert);
- //Dbg("after read card,CreateDevComponent result:%d", eErr);
- //eErr = m_pIDCert->DevOpen(0);
- //Dbg("after read card,devopen result:%d",eErr);
- return 0;
- }
- void CIDCertFSM::CrossTermCall(SpReqAnsContext<IDCert_CrossTermCall_Req, IDCert_CrossTermCall_Ans>::Pointer cctx)
- {
- ErrorCodeEnum eErr = Error_Unexpect;
- Dbg("CrossTermCall,type:%x", cctx->Req.type);
- switch (cctx->Req.type)
- {
- case IDCertService_Method_Read:
- {
- DEFINE_PUPPET_CONTEXT_WITH_TYPE(IDCert_Read_Req, IDCert_Read_Ans);
- if ((eErr = SpBlob2Object(cctx->Req.paramX, ctx->Req)) == Error_Succeed) {
- if (this->ReadInfo(ctx) != 0) {
- eErr = Error_Unexpect;
- }
- else {
- eErr = SpObject2Blob(ctx->Ans, cctx->Ans.retParamX);
- Dbg("after ReadInfo,eErr:%d,paramX.len:%d",eErr, cctx->Ans.retParamX.m_iLength);
- }
- }
- cctx->Answer(eErr);
- }
- break;
- case IDCertService_Method_ReadAndScan:
- {
- DEFINE_PUPPET_CONTEXT_WITH_TYPE(IDCert_ReadAndScan_Req, IDCert_ReadAndScan_Ans);
- if ((eErr = SpBlob2Object(cctx->Req.paramX, ctx->Req)) == Error_Succeed) {
- if (this->ReadAndScan(ctx) != 0) {
- eErr = Error_Unexpect;
- }
- else {
- eErr = SpObject2Blob(ctx->Ans, cctx->Ans.retParamX);
- Dbg("after ReadAndScan,eErr:%d,paramX.len:%d", eErr, cctx->Ans.retParamX.m_iLength);
- }
- }
- cctx->Answer(eErr);
- }
- break;
- case IDCertService_Method_CancelRead:
- m_bCancelRead = true;
- break;
- case IDCertService_Method_ReadWaitMore:
- if (GetReadFlag())
- SetReadMore();
- break;
- case IDCertService_Method_Exit:
- SetExitFlag();
- break;
- default:
- break;
- }
- }
- void CIDCertFSM::CrossTermInvokeInfo(SpOnewayCallContext<IDCert_CrossTermInvokeInfo_Info>::Pointer ctx)
- {
- LOG_FUNCTION();
- Dbg("entity sn:%x,type:%d,errcode:%x", ctx->Info.nEntityID, ctx->Info.type,ctx->Info.result);
- //Dbg("mxx 001[%d]", m_readAndScanCtx->Req.type);
- if (ctx->Info.paramX.m_iLength <= 1)
- {
- }
- ErrorCodeEnum eErr = Error_Unexpect;
- CSmartPointer<ITransactionContext> pTransactionContext;
- switch (ctx->Info.type)
- {
- case IDCertService_Method_Read:
- {
- auto pt = static_cast<SpReqAnsContext<IDCert_Read_Req,
- IDCert_Read_Ans>*>(GetCtx(ctx->Info.type));
- SpReqAnsContext<IDCert_Read_Req,
- IDCert_Read_Ans>::Pointer xctx;
- xctx.Attach(pt);
- eErr = SpBlob2Object(ctx->Info.paramX, xctx->Ans);
- Dbg("eErr:%d,Y len:%d", eErr, ctx->Info.paramX.m_iLength);
- xctx->Answer((ErrorCodeEnum)ctx->Info.result);
- }
- break;
- case IDCertService_Method_ReadAndScan:
- {
- auto pt = static_cast<SpReqAnsContext<IDCert_ReadAndScan_Req,IDCert_ReadAndScan_Ans>*>(m_mapCtx[ctx->Info.type].pCtx);
- SpReqAnsContext<IDCert_ReadAndScan_Req, IDCert_ReadAndScan_Ans>::Pointer tmpctx;
- tmpctx.Attach(pt);
- eErr = SpBlob2Object(ctx->Info.paramX, tmpctx->Ans);
- Dbg("eErr:%d,Y len:%d", eErr, ctx->Info.paramX.m_iLength);
- Dbg("name:%s",(const char*)tmpctx->Ans.name);
- tmpctx->Answer(eErr);
- tmpctx.SubRef();
- m_mapCtx.erase(ctx->Info.type);
- }
- break;
- default:
- break;
- }
- }
- int CIDCertFSM::LocalCallHeartBeat(int method, CBlob &bbSend, bool bTwoWay)
- {
- ErrorCodeEnum eErr = Error_Unexpect;
- if (m_pHBClient == NULL)
- {
- m_pHBClient = new HeartBeatService_ClientBase(GetEntityBase());
- if (m_pHBClient == NULL)
- {
- Dbg("create HeartBeat client failed.GetLastError:%d", GetLastError());
- return -1;
- }
- eErr = m_pHBClient->Connect();
- if (eErr != Error_Succeed)
- {
- Dbg("client connect to HeartBeat failed:%x", eErr);
- m_pHBClient->SafeDelete();
- m_pHBClient = NULL;
- return -1;
- }
- }
- HeartBeatService_CrossTermCall_Req req;
- HeartBeatService_CrossTermCall_Ans ans;
- req.nEntityID = 0x201;
- req.isTwoWay = bTwoWay;
- req.fromTerminalNo = m_terminalNo;
- GetAttachedTerminal(req.toTerminalNo);
- //req.toTerminalNo = "7717715004";
- req.param1 = "";
- req.param2 = "";
- req.paramX = bbSend;
- req.type = method;
- Dbg("call to heartbeat,from %s to %s,entity id:%x,type:%d",(const char*)req.fromTerminalNo,(const char*)req.toTerminalNo,req.nEntityID,req.type);
- eErr = m_pHBClient->CrossTermCall(req, ans, 20000);
- Dbg("CrossTermCall to HeartBeat result:%x", eErr);
- m_pHBClient->SafeDelete();
- m_pHBClient = NULL;//resource cost?
- return 0;
- }
- bool CIDCertFSM::GetAttachedTerminal(CSimpleStringA &csTerm)
- {
- return (Error_Succeed == GetEntityBase()->GetFunction()->GetSysVar("AttachedTerminal", csTerm));
- }
- void CIDCertFSM::LogErrMsg(const char *pMsgHead, ErrorCodeEnum eErrCode,
- DWORD defaultDevCode /*= 0*/, BOOL bAlarm /*= FALSE*/)
- {
- WORD wdErrCode = 0;
- CSimpleStringA csErrMsg(true);
- ErrorCodeEnum ec = CCommDevFSM::GetAndSplitDevErrInfo(csErrMsg, wdErrCode, "");
- if(ec == Error_Succeed && wdErrCode != 0) {
- //oilyang@20200525 if have been set ErrorCode of entity defined (not device),use it directly
- if ((defaultDevCode >> 20) == m_entCode.dwEntityId)
- UpdateDEC(defaultDevCode);
- else
- UpdateDEC(wdErrCode);
- } else if(defaultDevCode != 0) {
- UpdateDEC(defaultDevCode);
- }
- m_csAlarmMsg = CSimpleStringA::Format("%s failed EC= 0x%x : %s", pMsgHead, eErrCode, (LPCTSTR)csErrMsg);
- if(bAlarm) {
- LogWarn(Severity_High, eErrCode, AlarmDECToBusiness(), (LPCTSTR)m_csAlarmMsg);
- }
- return;
- }
- int CIDCertFSM::Initial()
- {
- CSystemStaticInfo sysInfo;
- GetEntityBase()->GetFunction()->GetSystemStaticInfo(sysInfo);
- m_csMachineType = sysInfo.strMachineType;
- m_terminalNo = sysInfo.strTerminalID;
- Dbg("machineType:%s,terminalNo:%s", (const char*)m_csMachineType, (const char*)m_terminalNo);
- if (_strnicmp((const char*)m_csMachineType, "RVC.IL", strlen("RVC.IL")) == 0)
- {
- m_bRVCIL = true;
- return 0;
- }
- m_devState = DEVICE_STATUS_NOT_READY;
- //to do device init
- int initCount = 0;
- ErrorCodeEnum err;
- m_pIDCert = NULL;
- bool bCreateDevCom = false;
- ErrorCodeEnum eErrDev;
- CSimpleStringA dllName, tmpVendor(""), tmpDevSN(""),tmpDLLVersion, csDepPath(""), csBackslash("\\");
- CSmartPointer<IEntityFunction> spEntityFunction = GetEntityBase()->GetFunction();
- CSmartPointer<IConfigInfo> spConfig;
- eErrDev = spEntityFunction->OpenConfig(Config_Root, spConfig);
- if (eErrDev != Error_Succeed) {
- Dbg("open cfg file failed!");
- LogErrMsg("Initial:OpenConfig", eErrDev, AlarmDECToBusiness(MEC_CFG_RUN_POINTER_FAILED),TRUE);
- return 2;
- }
- GetEntityBase()->GetFunction()->GetSysVar("FWBVendor", tmpVendor);
- GetEntityBase()->GetFunction()->GetSysVar("FWBDevSN", tmpDevSN);
- GetEntityBase()->GetFunction()->GetSysVar("FWBVersion", tmpDLLVersion);
- if (tmpDLLVersion.GetLength() < 2)//如果忘记配置,则直接使用默认值8.1
- tmpDLLVersion = "8.1";
- if (tmpDevSN.GetLength() > 12 && tmpDevSN.IndexOf("FWB") > 2)
- {
- Dbg("This is fwb device.");
- GetEntityBase()->GetFunction()->GetPath("Dep", csDepPath);
- dllName = csDepPath + csBackslash + "IDCertificate." + tmpVendor + "." + tmpDLLVersion + ".dll";//oiltmp
- Dbg("%s", (const char*)dllName);
- HARDWARE_ENTITY_SET_VENDOR_NAME(m_entCode, tmpVendor);
- }
- else
- {
- eErrDev = SpGetDevAdaptorPath(m_pEntity, GetEntityBase()->GetEntityName(), dllName);
- if (eErrDev != Error_Succeed){
- Dbg("get vendor dll(%s)name failed.", (LPCTSTR)dllName);
- return 2;
- }
- CSmartPointer<IConfigInfo> pConfig;
- ErrorCodeEnum rc = GetEntityBase()->GetFunction()->OpenConfig(Config_Root, pConfig);
- CSimpleStringA strSection = CSimpleStringA("Device.") + GetEntityBase()->GetEntityName();
- CSimpleStringA str;
- pConfig->ReadConfigValue(strSection, "Vendor", str);
- HARDWARE_ENTITY_SET_VENDOR_NAME(m_entCode, str);
- }
- Dbg("%s", (LPCTSTR)dllName);
-
- spConfig->ReadConfigValue("Device.IDCertificate", "Vendor", m_devVendor);
- spConfig->ReadConfigValue("Device.IDCertificate", "Version", m_devVer);
- spConfig->ReadConfigValue("Device.IDCertificate", "Batch", m_devBatch);
- Dbg("Vendor=%s#Version=%s#Batch=%s", (const char*)m_devVendor, (const char*)m_devVer, (const char*)m_devBatch);
- /*------2020-02-13-------*/
- //modify by LZM
- eErrDev = VendorLogControler(this, "IDCertificate");
- if (eErrDev != Error_Succeed)
- {
- Dbg("VendorLogControler Failed...");
- }
- /*-----------------------*/
- m_hVerdorDll = LoadLibraryA(dllName);
- if (m_hVerdorDll == NULL)
- {
- Dbg("load verdor dll failed(%d).", GetLastError());
- return 2;
- }
- if ((CreateDevComponent = (lpCreateDevCom)GetProcAddress(m_hVerdorDll, "CreateDevComponent")) == NULL)
- {
- Dbg("Get CreateDevComponent failed(%d).", GetLastError());
- return 2;
- }
- if ((ReleaseDevComponent = (lpReleaseDevCom)GetProcAddress(m_hVerdorDll, "ReleaseDevComponent")) == NULL)
- {
- Dbg("Get ReleaseDevComponent failed(%d).", GetLastError());
- return 2;
- }
- Dbg("Get function suc.");
- bool bOpenFlag = false;
- do {
- if (!bCreateDevCom)
- {
- if (CreateDevComponent((DeviceBaseClass * &)m_pIDCert) != Error_Succeed)
- {
- LOG_TRACE("Create IDCertificateReader module failed.");
- initCount++;
- m_pIDCert = NULL;
- if (initCount >= IDCER_INIT_COUNT)
- break;
- else
- continue;
- }
- bCreateDevCom = true;
- LOG_TRACE("Create IDCertificateReader module succeeded.");
- }
- // IDCerInitParam initParam;
- // initParam.ePortType = IDCER_PORTCOM;
- int tmpPort = 0;
- spConfig->ReadConfigValueInt("Device.IDCertificate", "Port", tmpPort);
- Dbg("IDCer open with port %d", tmpPort);
- err = m_pIDCert->DevOpen(tmpPort);
- if (err != Error_Succeed)
- {
- LogErrMsg("Initial::DevOpen", err, MEC_DEVAPI_IDCER_DevOpen);
- LogWarn(Severity_Middle, err, AlarmDEC(), "IDCertificate init failed.");
- return 2;
- //initCount++;
- //m_pIDCert = NULL;
- //if (initCount >= IDCER_INIT_COUNT)
- // break;
- //else
- // continue;
- }
- } while (m_pIDCert == NULL && bOpenFlag == false);
- if (m_pIDCert != NULL)
- {
- err = m_pIDCert->IDCerRFControl(false);
- if (err != Error_Succeed)
- LOG_TRACE("close rf failed.");
- else
- LOG_TRACE("close rf ok.");
- }
- ZeroMemory(m_devCatInfo.szModel, MAX_DEV_MODEL_LEN);
- ZeroMemory(m_devCatInfo.szType, MAX_DEV_TYPE_LEN);
- ZeroMemory(m_devCatInfo.szVendor, MAX_DEV_VENDOR_LEN);
- eErrDev = m_pIDCert->GetDevCategory(m_devCatInfo);
- if (eErrDev == Error_Succeed)
- {
- Dbg("%d,%d,%d", strlen(m_devCatInfo.szModel), strlen(m_devCatInfo.szType), strlen(m_devCatInfo.szVendor));
- if (strlen(m_devCatInfo.szModel) < 256)
- Dbg("%s", m_devCatInfo.szModel);
- }
- if (m_pIDCert != NULL)
- {
- m_devInit = true;
- return 0;
- }
- else
- return 2;
- return 0;
- }
|