IDCertFSM.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510
  1. #include "stdafx.h"
  2. #include "IDCertFSM.h"
  3. #include "GetDevInfoHelper.h"
  4. #include "EventCode.h"
  5. //#pragma comment(lib,"IDCerReader.lib")
  6. #include "libimageproc.h"
  7. #include "ModuleMix.h"
  8. #include "IDCertificate_UserErrorCode.h"
  9. #include "CommDevEntityErrorCode.h"
  10. //#include <ximabmp.h>
  11. //#include <ximapng.h>
  12. #include <iostream>
  13. #include <fstream>
  14. #include <stdio.h>
  15. #include <cv.h>
  16. #include <cxcore.h>
  17. #include <highgui.h>
  18. //#include <Windows.h>
  19. //#include "DeviceBaseHelper.h"
  20. #include "DevFSMCommBase.hpp"
  21. #pragma comment(lib,"user32.lib")
  22. #define IDCER_INIT_COUNT 3
  23. #define IDCER_READ_TIMEOUT 60000
  24. #define IDCER_AUTH_INTERVAL 300
  25. //oiltmp need to move together?
  26. int StrBuf2HexBuf(LPCTSTR strBuf, PBYTE* hexBuf)
  27. {
  28. int len = strlen(strBuf);
  29. if (len == 0 || len % 2 != 0)
  30. return 0;
  31. BYTE* buf = new BYTE[len / 2];
  32. if (buf == NULL)
  33. return 0;
  34. int j = 0;
  35. for (int i = 0; i < len;)
  36. {
  37. int tmpVal;
  38. sscanf(strBuf + i, "%2X", &tmpVal);
  39. buf[j] = tmpVal;
  40. //buf[j] = char2int(strBuf[i])*16 + char2int(strBuf[i+1]);
  41. i += 2;
  42. j++;
  43. }
  44. //memcpy(buf,strBuf,len);
  45. *hexBuf = buf;
  46. return j;
  47. }
  48. int HexBuf2StrBuf(PBYTE hexBuf, char** strBuf, DWORD len)
  49. {
  50. char* tmpStr = *strBuf;
  51. int count = 0;
  52. for (int i = 0; i < len; ++i)
  53. {
  54. sprintf(tmpStr + count, "%0.2X", hexBuf[i]);
  55. count += 2;
  56. }
  57. return 0;
  58. }
  59. //Normal/Idle
  60. void CIDCertFSM::s0_on_entry()
  61. {
  62. LOG_FUNCTION();
  63. m_devState = DEVICE_STATUS_NORMAL;
  64. GetEntityBase()->GetFunction()->SetUserDefineState(USER_IDCERTIFICATE_IDLE);
  65. }
  66. void CIDCertFSM::s0_on_exit()
  67. {
  68. LOG_FUNCTION();
  69. //if (m_pIDCert != NULL) {
  70. // if (m_pIDCert->DevClose() == Error_Succeed)
  71. // LOG_TRACE("关闭二代证成功!");
  72. // else
  73. // LOG_TRACE("关闭二代证失败!");
  74. //}
  75. }
  76. unsigned int CIDCertFSM::s0_on_event(FSMEvent* pEvt)
  77. {
  78. LOG_FUNCTION();
  79. Dbg("s0 evt %d",pEvt->iEvt);
  80. switch (pEvt->iEvt)
  81. {
  82. case USER_EVT_ERROR:
  83. pEvt->SetHandled();
  84. break;
  85. case USER_EVT_QUIT:
  86. pEvt->SetHandled();
  87. Dbg("s0 on event quit");
  88. break;
  89. case USER_EVT_READ:
  90. {
  91. pEvt->SetHandled();
  92. ReadEvent* ide = dynamic_cast<ReadEvent *>(pEvt);
  93. ReadTask* task = new ReadTask(this);
  94. task->ctx = ide->ctx;
  95. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  96. }
  97. break;
  98. case USER_EVT_READ_EX:
  99. {
  100. pEvt->SetHandled();
  101. ReadExEvent* ide = dynamic_cast<ReadExEvent *>(pEvt);
  102. ReadExTask* task = new ReadExTask(this);
  103. task->ctx = ide->ctx;
  104. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  105. }
  106. break;
  107. case USER_EVT_READ_AND_SCAN:
  108. {
  109. pEvt->SetHandled();
  110. ReadAndScanEvent* ide = dynamic_cast<ReadAndScanEvent *>(pEvt);
  111. ReadAndScanTask* task = new ReadAndScanTask(this);
  112. task->ctx = ide->ctx;
  113. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  114. }
  115. break;
  116. default:
  117. break;
  118. }
  119. return 0;
  120. }
  121. //Reading
  122. void CIDCertFSM::s1_on_entry()
  123. {
  124. LOG_FUNCTION();
  125. GetEntityBase()->GetFunction()->SetUserDefineState(USER_IDCERTIFICATE_READING);
  126. }
  127. void CIDCertFSM::s1_on_exit()
  128. {
  129. LOG_FUNCTION();
  130. }
  131. unsigned int CIDCertFSM::s1_on_event(FSMEvent *pEvt)
  132. {
  133. LOG_FUNCTION();
  134. Dbg("s1 evt (%d).", pEvt->iEvt);
  135. int ret = 0;
  136. switch(pEvt->iEvt)
  137. {
  138. case USER_EVT_READ_FINISHED:
  139. pEvt->SetHandled();
  140. ret = pEvt->param1;
  141. break;
  142. case USER_EVT_CANCEL_READ:
  143. pEvt->SetHandled();
  144. Dbg("Set cancel read flag");
  145. m_bCancelRead = true;
  146. break;
  147. case USER_EVT_EXIT:
  148. pEvt->SetHandled();
  149. SetExitFlag();
  150. break;
  151. case USER_EVT_QUIT:
  152. pEvt->SetHandled();
  153. break;
  154. case USER_EVT_READ_EX_FINISHED:
  155. pEvt->SetHandled();
  156. ret = pEvt->param1;
  157. break;
  158. case USER_EVT_READ_AND_SCAN_FINISHED:
  159. pEvt->SetHandled();
  160. ret = pEvt->param1;
  161. break;
  162. default:
  163. break;
  164. }
  165. return ret;
  166. }
  167. //failed
  168. void CIDCertFSM::s2_on_entry()
  169. {
  170. LOG_FUNCTION();
  171. m_devState = DEVICE_STATUS_FAULT;
  172. GetEntityBase()->GetFunction()->SetUserDefineState(USER_IDCERTIFICATE_FAILED);
  173. m_testResult = Error_InvalidState;
  174. }
  175. void CIDCertFSM::s2_on_exit()
  176. {
  177. LOG_FUNCTION();
  178. }
  179. unsigned int CIDCertFSM::s2_on_event(FSMEvent* e)
  180. {
  181. LOG_FUNCTION();
  182. if (e->iEvt == USER_EVT_QUIT)
  183. {
  184. e->SetHandled();
  185. return 0;
  186. }
  187. return 0;
  188. }
  189. //Eject
  190. void CIDCertFSM::s3_on_entry()
  191. {
  192. }
  193. void CIDCertFSM::s3_on_exit()
  194. {
  195. }
  196. unsigned int CIDCertFSM::s3_on_event(FSMEvent* e)
  197. {
  198. return 0;
  199. }
  200. //WaitingFetch
  201. void CIDCertFSM::s4_on_entry()
  202. {
  203. LOG_FUNCTION();
  204. WaitFetchIDCardTask* task = new WaitFetchIDCardTask(this);
  205. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  206. }
  207. void CIDCertFSM::s4_on_exit()
  208. {
  209. }
  210. unsigned int CIDCertFSM::s4_on_event(FSMEvent* pEvt)
  211. {
  212. Dbg("wait fetch id card.event %d,%d",pEvt->iEvt,pEvt->param1);
  213. switch (pEvt->iEvt)
  214. {
  215. case USER_EVT_WAIT_FETCH_IDCARD_FINISHED:
  216. pEvt->SetHandled();
  217. break;
  218. default:
  219. break;
  220. }
  221. return 0;
  222. }
  223. //Init
  224. void CIDCertFSM::s5_on_entry()
  225. {
  226. LOG_FUNCTION();
  227. m_devState = DEVICE_STATUS_NOT_READY;
  228. InitTask* task = new InitTask(this);
  229. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  230. }
  231. void CIDCertFSM::s5_on_exit()
  232. {
  233. LOG_FUNCTION();
  234. }
  235. unsigned int CIDCertFSM::s5_on_event(FSMEvent* pEvt)
  236. {
  237. LOG_FUNCTION();
  238. Dbg("s5 evt %d", pEvt->iEvt);
  239. int ret = 0;
  240. switch (pEvt->iEvt)
  241. {
  242. case USER_EVT_ERROR:
  243. pEvt->SetHandled();
  244. break;
  245. case USER_EVT_QUIT:
  246. pEvt->SetHandled();
  247. Dbg("s5 on event quit");
  248. break;
  249. case USER_EVT_INIT_FINISHED:
  250. {
  251. pEvt->SetHandled();
  252. ret = pEvt->param1;
  253. }
  254. break;
  255. default:
  256. break;
  257. }
  258. return ret;
  259. }
  260. ErrorCodeEnum CIDCertFSM::GetVendorDllPath(CSimpleStringA &strPath)
  261. {
  262. strPath = "termb.dll";
  263. return Error_Succeed;//oiltest
  264. CSimpleStringA strDepPath;
  265. ErrorCodeEnum Error = m_pEntity->GetFunction()->GetPath("Dep", strDepPath);
  266. if (Error == Error_Succeed) {
  267. strPath = "termb.dll";//CSimpleStringA::Format("%s\\mod_IDCertificate\\termb.dll", (LPCSTR)strDepPath);
  268. }
  269. return Error;
  270. }
  271. bool isnostr(const char *str)
  272. {
  273. int len = strlen(str);
  274. if (len == 0)
  275. return true;
  276. for (int i = 0; i < len; ++i)
  277. {
  278. if (*(str+i) != ' ')
  279. return false;
  280. }
  281. return true;
  282. }
  283. ErrorCodeEnum CIDCertFSM::OnInit()
  284. {
  285. ZeroMemory(m_devCatInfo.szModel, MAX_DEV_MODEL_LEN);
  286. ZeroMemory(m_devCatInfo.szType, MAX_DEV_TYPE_LEN);
  287. ZeroMemory(m_devCatInfo.szVendor, MAX_DEV_VENDOR_LEN);
  288. return Error_Succeed;
  289. }
  290. ErrorCodeEnum CIDCertFSM::OnExit()
  291. {
  292. if (m_pIDCert != NULL)
  293. {
  294. ErrorCodeEnum err;
  295. err = m_pIDCert->DevClose();
  296. LOG_TRACE("OnExit hr(%d)",err);
  297. }
  298. if (m_pIDCert)
  299. {
  300. ReleaseDevComponent((DeviceBaseClass *&)m_pIDCert);
  301. m_pIDCert = NULL;
  302. }
  303. return Error_Succeed;
  304. }
  305. int CIDCertFSM::ReadInfo(SpReqAnsContext<IDCert_Read_Req, IDCert_Read_Ans>::Pointer ctx)
  306. {
  307. LOG_FUNCTION();
  308. DWORD elapsed = 0;
  309. DWORD dwStart = cvGetTickCount();
  310. DWORD dwEnd = cvGetTickCount();
  311. ErrorCodeEnum hr;
  312. LogEvent(Severity_Middle,LOG_EVT_IDCERTIFICATE_GREEN_ON,"IDCer warning on");
  313. Dbg("Before readingInfo, do clear job.");
  314. DeleteZP(Bmp_ZP);
  315. Dbg("Before readingInfo, finish doing clear job.");
  316. bool bOpenRF = false,bGetIDCert = false;
  317. IDCerInfo idInfo;
  318. memset(&idInfo, 0, sizeof(idInfo));
  319. m_bCancelRead = false;
  320. m_bWaitReadMore = false;
  321. m_bReading = true;
  322. m_bExit = false;
  323. while(elapsed < IDCER_READ_TIMEOUT && !bGetIDCert && !m_bCancelRead)
  324. {
  325. if (m_bExit)
  326. {
  327. Dbg("exit");
  328. break;
  329. }
  330. if (m_bWaitReadMore)
  331. {
  332. dwStart = cvGetTickCount();
  333. m_bWaitReadMore = false;
  334. }
  335. if (!bOpenRF)
  336. {
  337. hr = m_pIDCert->IDCerRFControl(true);
  338. if (Error_Succeed == hr)
  339. {
  340. Dbg("open rf succeed.");
  341. }
  342. else
  343. {
  344. Dbg("open rf failed. %d",hr);
  345. dwEnd = cvGetTickCount();
  346. elapsed = (dwEnd - dwStart) / cvGetTickFrequency() * 1000;
  347. //if cannot open rf in 10s ,quit
  348. // Adapt to Desk2S [4/12/2018 15:05 @Gifur]
  349. /*if (elapsed > 10000)
  350. break;
  351. else*/{
  352. Sleep(IDCER_AUTH_INTERVAL);
  353. continue;
  354. }
  355. }
  356. }
  357. bOpenRF = true;
  358. Sleep(IDCER_AUTH_INTERVAL);
  359. hr = m_pIDCert->IDCerAuthenticate();
  360. if (Error_Succeed != hr)
  361. {
  362. DevErrorInfo devErrInfo;
  363. m_pIDCert->GetLastErr(devErrInfo);
  364. Dbg("IDCerAuthenticate failed %d,%s.", hr, devErrInfo.szErrMsg);
  365. dwEnd = cvGetTickCount();
  366. elapsed = (dwEnd - dwStart) / cvGetTickFrequency() * 1000;
  367. continue;
  368. }
  369. else
  370. {
  371. Dbg("auth succeed.");
  372. hr = m_pIDCert->IDCerGetData(idInfo);
  373. if (hr != Error_Succeed)
  374. {
  375. LogErrMsg("ReadInfo::IDCerGetData", hr, MEC_DEVAPI_IDCER_IDCerGetData);
  376. }
  377. else
  378. {
  379. LOG_TRACE("read succeed.");
  380. LogEvent(Severity_Middle,LOG_EVT_IDCERTIFICATE_OP,"IDCertifacate op.");
  381. //oiltest
  382. ctx->Ans.name = idInfo.name.data;
  383. //ctx->Ans.name = "我.们我.们我·们我·们爱邱总";
  384. //ctx->Ans.name = "张云虎";
  385. ctx->Ans.sex = idInfo.sex.data;
  386. ctx->Ans.nation = idInfo.nation.data;
  387. ctx->Ans.birthday = idInfo.birthday.data;
  388. ctx->Ans.address = idInfo.address.data;
  389. //oiltest
  390. ctx->Ans.idcode = idInfo.idno.data;
  391. //oilyang@20180309 根据俞根伟,生产上存在身份证异常为'x'的问题,统一进行转换
  392. if (ctx->Ans.idcode[ctx->Ans.idcode.GetLength() - 1] == 'x')
  393. {
  394. Dbg("x to X");
  395. ctx->Ans.idcode[ctx->Ans.idcode.GetLength() - 1] = 'X';
  396. }
  397. //ctx->Ans.idcode = "370285198804025014";
  398. ctx->Ans.department = idInfo.department.data;
  399. ctx->Ans.startdate = idInfo.startDate.data;
  400. ctx->Ans.enddate = idInfo.endDate.data;
  401. //ctx->Ans.startdate = "2015.08.06";
  402. //ctx->Ans.enddate = "2035.08.06";
  403. //oiltest
  404. /*Dbg("Name:%s,Sex:%s,Nation:%s,Birthday:%s,StartDate:%s,EndDate:%s,IDNo:%s****%s,Address:%s,"
  405. ,idInfo.name.data
  406. ,idInfo.sex.data
  407. ,idInfo.nation.data
  408. ,idInfo.birthday.data
  409. ,idInfo.startDate.data
  410. ,idInfo.endDate.data
  411. ,(const char*)ctx->Ans.idcode.SubString(0, 4), (const char*)ctx->Ans.idcode.SubString(ctx->Ans.idcode.GetLength() - 1, 1)
  412. ,idInfo.address.data);*/
  413. Dbg("Sex:%s,Nation:%s,EndDate:%s,IDNo:%s****%s,"
  414. ,idInfo.sex.data
  415. ,idInfo.nation.data
  416. ,idInfo.endDate.data
  417. ,(const char*)ctx->Ans.idcode.SubString(0, 4), (const char*)ctx->Ans.idcode.SubString(ctx->Ans.idcode.GetLength() - 1, 1));
  418. Dbg("to get photo");
  419. hr = GetPngBlob(ctx->Ans.photodata,true);
  420. if (hr != Error_Succeed)
  421. {
  422. bGetIDCert = false;
  423. LOG_TRACE("get photo failed.");
  424. break;
  425. }
  426. bGetIDCert = true;
  427. break;
  428. }
  429. }
  430. dwEnd = cvGetTickCount();
  431. elapsed = (dwEnd - dwStart) / cvGetTickFrequency() * 1000;
  432. }
  433. m_bExit = false;
  434. m_bReading = false;
  435. LogEvent(Severity_Middle,LOG_EVT_IDCERTIFICATE_GREEN_OFF,"IDCer warning off");
  436. Dbg("after close light");
  437. if (bGetIDCert)
  438. ctx->Answer(Error_Succeed);
  439. else if (m_bCancelRead)
  440. ctx->Answer(Error_Cancel);
  441. else if (elapsed >= IDCER_READ_TIMEOUT)
  442. ctx->Answer(Error_TimeOut);
  443. else
  444. ctx->Answer(Error_Unexpect, AlarmDECToBusiness());
  445. //if (!bGetIDCert)
  446. //LogWarn(Severity_Low,Error_DevCommFailed,LOG_WARN_IDCERTIFICATE_ACTION,"IDCertificate read failed.");
  447. Dbg("after send ack");
  448. //if (!bOpenRF)
  449. //{
  450. // LOG_TRACE("rf failed.");
  451. // return 1;
  452. //}
  453. if(bOpenRF) {
  454. hr = m_pIDCert->IDCerRFControl(false);
  455. if (hr != Error_Succeed)
  456. {
  457. Dbg("rf close failed(%d).",hr);
  458. return 1;
  459. }
  460. }
  461. return 0;
  462. }
  463. int CIDCertFSM::ReadInfoEx(SpReqAnsContext<IDCert_ReadEx_Req, IDCert_ReadEx_Ans>::Pointer ctx)
  464. {
  465. LOG_FUNCTION();
  466. DWORD elapsed = 0;
  467. DWORD dwStart = cvGetTickCount();
  468. DWORD dwEnd = cvGetTickCount();
  469. ErrorCodeEnum eErr;
  470. LogEvent(Severity_Middle,LOG_EVT_IDCERTIFICATE_GREEN_ON,"IDCer warning on");
  471. Dbg("Before readingInfo, do clear job.");
  472. DeleteZP(Bmp_ZP);
  473. Dbg("Before readingInfo, finish doing clear job.");
  474. bool bOpenRF = false, bGetIDCert = false;
  475. IDCerInfoEx idInfo;
  476. ZeroMemory(&idInfo, sizeof(IDCerInfoEx));
  477. m_bCancelRead = false;
  478. m_bWaitReadMore = false;
  479. m_bReading = true;
  480. m_bExit = false;
  481. while (elapsed < IDCER_READ_TIMEOUT && !bGetIDCert && !m_bCancelRead)
  482. {
  483. if (m_bExit)
  484. {
  485. Dbg("exit");
  486. break;
  487. }
  488. if (m_bWaitReadMore)
  489. {
  490. dwStart = cvGetTickCount();
  491. m_bWaitReadMore = false;
  492. }
  493. if (!bOpenRF)
  494. {
  495. eErr = m_pIDCert->IDCerRFControl(true);
  496. if (Error_Succeed == eErr)
  497. {
  498. Dbg("open rf succeed.");
  499. }
  500. else
  501. {
  502. Dbg("open rf failed.");
  503. dwEnd = cvGetTickCount();
  504. elapsed = (dwEnd - dwStart) / cvGetTickFrequency() * 1000;
  505. //if cannot open rf in 10s ,quit
  506. /* if (elapsed > 10000)
  507. break;
  508. else*/{
  509. Sleep(IDCER_AUTH_INTERVAL);
  510. continue;
  511. }
  512. }
  513. }
  514. bOpenRF = true;
  515. Sleep(IDCER_AUTH_INTERVAL);
  516. eErr = m_pIDCert->IDCerAuthenticate();//卡认证
  517. if (Error_Succeed != eErr)
  518. {
  519. //LOG_TRACE("auth failed.");
  520. DevErrorInfo devErrInfo;
  521. m_pIDCert->GetLastErr(devErrInfo);
  522. Dbg("IDCerAuthenticate failed %d,%s.", eErr, devErrInfo.szErrMsg);
  523. dwEnd = cvGetTickCount();
  524. elapsed = (dwEnd - dwStart) / cvGetTickFrequency() * 1000;
  525. continue;
  526. }
  527. else
  528. {
  529. Dbg("auth succeed.");
  530. eErr = m_pIDCert->IDCerGetDataEx(idInfo);
  531. if (eErr != Error_Succeed)
  532. {
  533. LogErrMsg("ReadInfoEx::IDCerGetDataEx", eErr, MEC_DEVAPI_IDCER_IDCerGetDataEx,TRUE);
  534. }
  535. else
  536. {
  537. LOG_TRACE("read succeed.");
  538. LogEvent(Severity_Middle, LOG_EVT_IDCERTIFICATE_OP, "IDCertifacate op.");
  539. ctx->Ans.name = idInfo.name.data;
  540. ctx->Ans.sex = idInfo.sex.data;
  541. ctx->Ans.nation = idInfo.nation.data;
  542. ctx->Ans.birthday = idInfo.birthday.data;
  543. ctx->Ans.address = idInfo.address.data;
  544. ctx->Ans.idcode = idInfo.idno.data;
  545. //oilyang@20180309 根据俞根伟,生产上存在身份证异常为'x'的问题,统一进行转换
  546. if (ctx->Ans.idcode[ctx->Ans.idcode.GetLength() - 1] == 'x')
  547. {
  548. Dbg("x to X");
  549. ctx->Ans.idcode[ctx->Ans.idcode.GetLength() - 1] = 'X';
  550. }
  551. ctx->Ans.department = idInfo.department.data;
  552. ctx->Ans.startdate = idInfo.startDate.data;
  553. ctx->Ans.enddate = idInfo.endDate.data;
  554. ctx->Ans.englishname = idInfo.englishName.data;
  555. ctx->Ans.nationality = idInfo.nationality.data;
  556. ctx->Ans.idversion = idInfo.idVersion.data;
  557. ctx->Ans.idtype = idInfo.idType.data;
  558. ctx->Ans.reserved = idInfo.reserved.data;
  559. ctx->Ans.startdate = idInfo.startDate.data;
  560. /*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"
  561. , (const char*)ctx->Ans.name, (const char*)ctx->Ans.sex, (const char*)ctx->Ans.nation
  562. , (const char*)ctx->Ans.birthday
  563. , (const char*)ctx->Ans.startdate, (const char*)ctx->Ans.enddate
  564. , (const char*)ctx->Ans.idcode.SubString(0, 4), (const char*)ctx->Ans.idcode.SubString(ctx->Ans.idcode.GetLength() - 1, 1)
  565. , (const char*)ctx->Ans.address
  566. , (const char*)ctx->Ans.idtype
  567. , (const char*)ctx->Ans.department
  568. , (const char*)ctx->Ans.englishname , (const char*)ctx->Ans.nationality);*/
  569. Dbg("Sex:%s,Nation:%s,EndDate:%s,IDNo:%s****%s,IDType:%s"
  570. ,(const char*)ctx->Ans.sex, (const char*)ctx->Ans.nation
  571. ,(const char*)ctx->Ans.enddate
  572. , (const char*)ctx->Ans.idcode.SubString(0, 4), (const char*)ctx->Ans.idcode.SubString(ctx->Ans.idcode.GetLength() - 1, 1)
  573. , (const char*)ctx->Ans.idtype);
  574. Dbg("to get photo");
  575. eErr = GetPngBlob(ctx->Ans.photodata,true);
  576. if (eErr != Error_Succeed)
  577. {
  578. bGetIDCert = false;
  579. LOG_TRACE("get photo failed.");
  580. break;
  581. }
  582. bGetIDCert = true;
  583. break;
  584. }
  585. }
  586. dwEnd = cvGetTickCount();
  587. elapsed = (dwEnd - dwStart) / cvGetTickFrequency() * 1000;
  588. }
  589. m_bExit = false;
  590. m_bReading = false;
  591. LogEvent(Severity_Middle, LOG_EVT_IDCERTIFICATE_GREEN_OFF, "IDCer warning off");
  592. Dbg("after close light");
  593. //if (!bGetIDCert)
  594. //LogWarn(Severity_Low, Error_DevCommFailed, LOG_WARN_IDCERTIFICATE_ACTION, "IDCertificate read failed.");
  595. int pos = 99;
  596. ErrorCodeEnum eErr1 =m_pIDCert->QueryCardPos(pos);
  597. if (eErr1 == Error_NotImpl)
  598. {
  599. Dbg("old version.no need position.");
  600. }
  601. else
  602. {
  603. switch (pos)
  604. {
  605. case 1:
  606. eErr1 = m_pIDCert->IDCerRFControl(false);
  607. if (eErr1 != Error_Succeed)
  608. m_pIDCert->ForceIDEject();
  609. break;
  610. case 2:
  611. break;
  612. case 0:
  613. default:
  614. break;
  615. }
  616. //查询卡片位置以决定跳转
  617. eErr1 = m_pIDCert->QueryCardPos(pos);
  618. if (eErr1 == Error_Succeed)
  619. {
  620. Dbg("pos:%d",pos);
  621. /*if (pos == 2)
  622. return 4;*/
  623. }
  624. }
  625. if (!(eErr1 == Error_Succeed && pos == 2))
  626. {
  627. //oilyang@20180531 add for close enter card
  628. m_pIDCert->IDCerRFControl(false); //这里基本不耗时,凯欣达大机耗时较长
  629. }
  630. //zjw@20191219 处理完相关操作再返回给前端,防止状态还未跳转又发起流程
  631. if (bGetIDCert)
  632. ctx->Answer(Error_Succeed);
  633. else if (m_bCancelRead)
  634. ctx->Answer(Error_Cancel);
  635. else if (elapsed >= IDCER_READ_TIMEOUT)
  636. ctx->Answer(Error_TimeOut);
  637. else
  638. ctx->Answer(Error_Unexpect);
  639. if (eErr1 == Error_Succeed && pos == 2)
  640. {
  641. return 4;
  642. }
  643. return 0;
  644. }
  645. int CIDCertFSM::ReadAndScan(SpReqAnsContext<IDCert_ReadAndScan_Req, IDCert_ReadAndScan_Ans>::Pointer ctx)
  646. {
  647. LOG_FUNCTION();
  648. DWORD elapsed = 0;
  649. DWORD dwStart = cvGetTickCount();
  650. DWORD dwEnd = cvGetTickCount();
  651. ErrorCodeEnum eErr;
  652. LogEvent(Severity_Middle, LOG_EVT_IDCERTIFICATE_GREEN_ON, "IDCer warning on");
  653. Dbg("Before readingInfo, do clear job.");
  654. DeleteZP(Bmp_ZP|Bmp_SCAN);
  655. Dbg("Before readingInfo, finish doing clear job.");
  656. bool bOpenRF = false, bGetIDCert = false;
  657. IDCerInfoEx idInfo;
  658. IDCerInfo idInfoOld;
  659. memset(&idInfo, 0, sizeof(idInfo));
  660. memset(&idInfoOld, 0, sizeof(idInfoOld));
  661. m_bCancelRead = false;
  662. m_bWaitReadMore = false;
  663. m_bReading = true;
  664. m_bExit = false;
  665. while (elapsed < IDCER_READ_TIMEOUT && !bGetIDCert && !m_bCancelRead)
  666. {
  667. if (m_bExit)
  668. {
  669. Dbg("exit");
  670. break;
  671. }
  672. if (m_bWaitReadMore)
  673. {
  674. dwStart = cvGetTickCount();
  675. m_bWaitReadMore = false;
  676. }
  677. if (!bOpenRF)
  678. {
  679. eErr = m_pIDCert->IDCerRFControl(true);
  680. if (Error_Succeed == eErr)
  681. {
  682. Dbg("open rf succeed.");
  683. }
  684. else
  685. {
  686. Dbg("open rf failed.");
  687. dwEnd = cvGetTickCount();
  688. elapsed = (dwEnd - dwStart) / cvGetTickFrequency() * 1000;
  689. //if cannot open rf in 10s ,quit
  690. /* if (elapsed > 10000)
  691. break;
  692. else*/ {
  693. Sleep(IDCER_AUTH_INTERVAL);
  694. continue;
  695. }
  696. }
  697. }
  698. bOpenRF = true;
  699. Sleep(IDCER_AUTH_INTERVAL);
  700. eErr = m_pIDCert->IDCerAuthenticate();
  701. if (Error_Succeed != eErr)
  702. {
  703. DevErrorInfo devErrInfo;
  704. m_pIDCert->GetLastErr(devErrInfo);
  705. Dbg("IDCerAuthenticate failed %d,%s.", eErr, devErrInfo.szErrMsg);
  706. dwEnd = cvGetTickCount();
  707. elapsed = (dwEnd - dwStart) / cvGetTickFrequency() * 1000;
  708. continue;
  709. }
  710. else
  711. {
  712. Dbg("auth succeed.");
  713. eErr = m_pIDCert->IDCerGetDataEx(idInfo);
  714. if (eErr == Error_NotImpl)
  715. {
  716. Dbg("to call old interface");
  717. eErr = m_pIDCert->IDCerGetData(idInfoOld);
  718. if (eErr != Error_Succeed)
  719. {
  720. LogErrMsg("ReadAndScan::IDCerGetData", eErr, MEC_DEVAPI_IDCER_IDCerGetData);
  721. }
  722. else
  723. {
  724. Dbg("read succeed.");
  725. LogEvent(Severity_Middle, LOG_EVT_IDCERTIFICATE_OP, "IDCertifacate op.");
  726. //oiltest
  727. ctx->Ans.name = idInfoOld.name.data;
  728. //ctx->Ans.name = "我.们我.们我·们我·们爱邱总";
  729. //ctx->Ans.name = "张云虎";
  730. ctx->Ans.sex = idInfoOld.sex.data;
  731. ctx->Ans.nation = idInfoOld.nation.data;
  732. ctx->Ans.birthday = idInfoOld.birthday.data;
  733. ctx->Ans.address = idInfoOld.address.data;
  734. //oiltest
  735. ctx->Ans.idcode = idInfoOld.idno.data;
  736. //oilyang@20180309 根据俞根伟,生产上存在身份证异常为'x'的问题,统一进行转换
  737. if (ctx->Ans.idcode[ctx->Ans.idcode.GetLength() - 1] == 'x')
  738. {
  739. Dbg("x to X");
  740. ctx->Ans.idcode[ctx->Ans.idcode.GetLength() - 1] = 'X';
  741. }
  742. //ctx->Ans.idcode = "370285198804025014";
  743. ctx->Ans.department = idInfoOld.department.data;
  744. ctx->Ans.startdate = idInfoOld.startDate.data;
  745. ctx->Ans.enddate = idInfoOld.endDate.data;
  746. //Dbg("nation: %s",(const char*)ctx->Ans.name);
  747. /*Dbg("Name:%s,Sex:%s,Nation:%s,Birthday:%s,StartDate:%s,EndDate:%s,IDNo:%s****%s,Address:%s,"
  748. ,idInfoOld.name.data
  749. ,idInfoOld.sex.data
  750. ,idInfoOld.nation.data
  751. ,idInfoOld.birthday.data
  752. ,idInfoOld.startDate.data
  753. ,idInfoOld.endDate.data
  754. ,(const char*)ctx->Ans.idcode.SubString(0, 4), (const char*)ctx->Ans.idcode.SubString(ctx->Ans.idcode.GetLength() - 1, 1)
  755. ,idInfoOld.address.data);*/
  756. Dbg("Sex:%s,Nation:%s,EndDate:%s,IDNo:%s****%s,"
  757. ,idInfoOld.sex.data
  758. ,idInfoOld.nation.data
  759. ,idInfoOld.endDate.data
  760. ,(const char*)ctx->Ans.idcode.SubString(0, 4), (const char*)ctx->Ans.idcode.SubString(ctx->Ans.idcode.GetLength() - 1, 1));
  761. Dbg("to get photo");
  762. GetPngBlobEx(ctx->Ans.headphoto, "zp");
  763. eErr = GetPngBlob(ctx->Ans.photodata, true);
  764. if (eErr != Error_Succeed)
  765. {
  766. bGetIDCert = false;
  767. LOG_TRACE("get photo failed.");
  768. break;
  769. }
  770. bGetIDCert = true;
  771. break;
  772. }
  773. }
  774. else
  775. {
  776. if (eErr != Error_Succeed)
  777. {
  778. LogErrMsg("ReadAndScan::IDCerGetDataEx", eErr, MEC_DEVAPI_IDCER_IDCerGetDataEx,TRUE);
  779. }
  780. else
  781. {
  782. LOG_TRACE("read succeed.");
  783. LogEvent(Severity_Middle, LOG_EVT_IDCERTIFICATE_OP, "IDCertifacate op.");
  784. ctx->Ans.name = idInfo.name.data;
  785. ctx->Ans.sex = idInfo.sex.data;
  786. ctx->Ans.nation = idInfo.nation.data;
  787. ctx->Ans.birthday = idInfo.birthday.data;
  788. ctx->Ans.address = idInfo.address.data;
  789. ctx->Ans.idcode = idInfo.idno.data;
  790. //oilyang@20180309 根据俞根伟,生产上存在身份证异常为'x'的问题,统一进行转换
  791. if (ctx->Ans.idcode[ctx->Ans.idcode.GetLength() - 1] == 'x')
  792. {
  793. Dbg("x to X");
  794. ctx->Ans.idcode[ctx->Ans.idcode.GetLength() - 1] = 'X';
  795. }
  796. ctx->Ans.department = idInfo.department.data;
  797. ctx->Ans.startdate = idInfo.startDate.data;
  798. ctx->Ans.enddate = idInfo.endDate.data;
  799. ctx->Ans.englishname = idInfo.englishName.data;
  800. ctx->Ans.nationality = idInfo.nationality.data;
  801. ctx->Ans.idversion = idInfo.idVersion.data;
  802. ctx->Ans.idtype = idInfo.idType.data;
  803. ctx->Ans.reserved = idInfo.reserved.data;
  804. ctx->Ans.startdate = idInfo.startDate.data;
  805. eErr = m_pIDCert->ScanIDAndSaveImage();
  806. ctx->Ans.hasscan = 0;
  807. if (eErr == Error_Succeed)
  808. {
  809. //idfront.bmp" and "idback.bmp
  810. ErrorCodeEnum eErrFront, eErrBack;
  811. eErrFront = GetPngBlobEx(ctx->Ans.frontphoto, "idfront");
  812. eErrBack = GetPngBlobEx(ctx->Ans.backphoto, "idback", true);
  813. if (eErrFront == Error_Succeed && eErrBack == Error_Succeed)
  814. ctx->Ans.hasscan = 1;
  815. }
  816. GetPngBlobEx(ctx->Ans.headphoto, "zp");
  817. /*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"
  818. , (const char*)ctx->Ans.name, (const char*)ctx->Ans.sex, (const char*)ctx->Ans.nation
  819. , (const char*)ctx->Ans.birthday
  820. , (const char*)ctx->Ans.startdate, (const char*)ctx->Ans.enddate
  821. , (const char*)ctx->Ans.idcode.SubString(0, 4), (const char*)ctx->Ans.idcode.SubString(ctx->Ans.idcode.GetLength() - 1, 1)
  822. , (const char*)ctx->Ans.address
  823. , (const char*)ctx->Ans.idtype
  824. , (const char*)ctx->Ans.department
  825. , (const char*)ctx->Ans.englishname , (const char*)ctx->Ans.nationality);*/
  826. Dbg("Sex:%s,Nation:%s,EndDate:%s,IDNo:%s****%s,IDType:%s"
  827. , (const char*)ctx->Ans.sex, (const char*)ctx->Ans.nation
  828. , (const char*)ctx->Ans.enddate
  829. , (const char*)ctx->Ans.idcode.SubString(0, 4), (const char*)ctx->Ans.idcode.SubString(ctx->Ans.idcode.GetLength() - 1, 1)
  830. , (const char*)ctx->Ans.idtype);
  831. Dbg("to get photo");
  832. eErr = GetPngBlob(ctx->Ans.photodata, true);
  833. if (eErr != Error_Succeed)
  834. {
  835. bGetIDCert = false;
  836. LOG_TRACE("get photo failed.");
  837. break;
  838. }
  839. bGetIDCert = true;
  840. break;
  841. }
  842. }
  843. }
  844. dwEnd = cvGetTickCount();
  845. elapsed = (dwEnd - dwStart) / cvGetTickFrequency() * 1000;
  846. }
  847. m_bExit = false;
  848. m_bReading = false;
  849. LogEvent(Severity_Middle, LOG_EVT_IDCERTIFICATE_GREEN_OFF, "IDCer warning off");
  850. Dbg("after close light");
  851. Dbg("ans size:%d",sizeof(ctx->Ans));
  852. Dbg("%d",FIELD_OFFSET(IDCertificate::IDCertService_ReadAndScan_Ans, reserved));
  853. //if (!bGetIDCert)
  854. //LogWarn(Severity_Low, Error_DevCommFailed, LOG_WARN_IDCERTIFICATE_ACTION, "IDCertificate read failed.");
  855. int pos = 99;
  856. ErrorCodeEnum eErr1 = m_pIDCert->QueryCardPos(pos);
  857. if (eErr1 == Error_NotImpl)
  858. {
  859. Dbg("old version.no need position.");
  860. }
  861. else
  862. {
  863. switch (pos)
  864. {
  865. case 1:
  866. eErr1 = m_pIDCert->IDCerRFControl(false);
  867. if (eErr1 != Error_Succeed)
  868. m_pIDCert->ForceIDEject();
  869. break;
  870. case 2:
  871. break;
  872. case 0:
  873. default:
  874. break;
  875. }
  876. //查询卡片位置以决定跳转
  877. eErr1 = m_pIDCert->QueryCardPos(pos);
  878. if (eErr1 == Error_Succeed)
  879. {
  880. Dbg("pos:%d", pos);
  881. /*if (pos == 2)
  882. return 4;*/
  883. }
  884. }
  885. if (!(eErr1 == Error_Succeed && pos == 2))
  886. {
  887. //oilyang@20180531 add for close enter card
  888. m_pIDCert->IDCerRFControl(false); //这里基本不耗时,凯欣达新大机耗时较长(先改我们流程解决,后续需要厂商优化这里)
  889. }
  890. //zjw@20191219 处理完相关操作再返回给前端,防止状态还未跳转又发起流程
  891. if (bGetIDCert)
  892. ctx->Answer(Error_Succeed);
  893. else if (m_bCancelRead)
  894. ctx->Answer(Error_Cancel);
  895. else if (elapsed >= IDCER_READ_TIMEOUT)
  896. ctx->Answer(Error_TimeOut);
  897. else
  898. ctx->Answer(Error_Unexpect);
  899. if (eErr1 == Error_Succeed && pos == 2)
  900. {
  901. return 4;
  902. }
  903. return 0;
  904. }
  905. #if 0
  906. ErrorCodeEnum CIDCertFSM::ConvertBmpToPng(CSimpleStringA &strBmpPath, CSimpleStringA &strPngPath)
  907. {
  908. CxImage image;
  909. if (image.Load((LPCSTR)strBmpPath, CXIMAGE_FORMAT_BMP)) {
  910. if (image.Save((LPCSTR)strPngPath, CXIMAGE_FORMAT_PNG)) {
  911. return Error_Succeed;
  912. } else {
  913. LOG_TRACE("save %s failed!", (LPCSTR)strPngPath);
  914. }
  915. } else {
  916. LOG_TRACE("load %s failed!", (LPCSTR)strBmpPath);
  917. }
  918. return Error_IO;
  919. }
  920. #endif
  921. ErrorCodeEnum CIDCertFSM::GetPngBlob(CBlob &data, bool bClear)
  922. {
  923. CSimpleStringA strPath,strBkPath,strBmpPath,strPngPath,strTxtPath,strWltPath;
  924. ErrorCodeEnum err;
  925. err = m_pEntity->GetFunction()->GetPath("Dep", strPath);
  926. if (err != Error_Succeed)
  927. {
  928. Dbg("Get path failed.");
  929. return Error_Param;
  930. }
  931. strBkPath = strPath + "\\bk.PNG";
  932. strBmpPath = strPath + "\\zp.bmp";
  933. strPngPath = strPath + "\\zp.JPEG";
  934. strTxtPath = strPath + "\\wz.txt";
  935. strWltPath = strPath + "\\xp.wlt";
  936. //ErrorCodeEnum Error = ConvertBmpToPng(strBmpPath, strPngPath);
  937. Dbg("photo change: %s,%s,%s",(LPCSTR)strBkPath,(LPCSTR)strBmpPath,(LPCSTR)strPngPath);
  938. bool bResult = imageprocess((char*)strBmpPath.GetData(),(char*)strPngPath.GetData());
  939. Dbg("photo changed");
  940. if (!bResult)
  941. {
  942. Dbg("photo changed failed.");
  943. return Error_Unexpect;
  944. }
  945. //if (Error == Error_Succeed) {
  946. FILE *fp = fopen(strPngPath, "rb");
  947. if (fp) {
  948. fseek(fp, 0, SEEK_END);
  949. long flen = ftell(fp);
  950. fseek(fp, 0, SEEK_SET);
  951. data.Alloc(flen);
  952. fread(data.m_pData, 1, flen, fp);
  953. fclose(fp);
  954. //DeleteFileA(strTxtPath);
  955. //DeleteFileA(strWltPath);
  956. } else {
  957. LOG_TRACE("fopen %s failed!", (LPCSTR)strPngPath);
  958. err = Error_IO;
  959. }
  960. //}
  961. Dbg("After ReadInfo, do clear job.");
  962. if (bClear)
  963. DeleteZP(Bmp_ZP);
  964. Dbg("After ReadInfo, finish doing clear job.");
  965. return err;
  966. }
  967. ErrorCodeEnum CIDCertFSM::GetPngBlobEx(CBlob &data, CSimpleStringA fileNamePrefix, bool bClear)
  968. {
  969. CSimpleStringA strPath;
  970. ErrorCodeEnum eErr;
  971. eErr = m_pEntity->GetFunction()->GetPath("Dep", strPath);
  972. if (eErr != Error_Succeed)
  973. {
  974. Dbg("Get path failed.");
  975. if (bClear)
  976. DeleteZP(Bmp_SCAN);
  977. return Error_Param;
  978. }
  979. strPath = strPath + "\\" + fileNamePrefix;
  980. Dbg("to load [%s.bmp",(const char*)strPath);
  981. IplImage *src = cvLoadImage(strPath + ".bmp");
  982. if (!src)
  983. {
  984. Dbg("read file %s.bmp failed.err:%d", (const char*)strPath, GetLastError());
  985. if (bClear)
  986. DeleteZP(Bmp_SCAN);
  987. return Error_Unexpect;
  988. }
  989. cvSaveImage(strPath + ".jpg", src);
  990. FILE *fp = fopen(strPath + ".jpg", "rb");
  991. if (fp) {
  992. fseek(fp, 0, SEEK_END);
  993. long flen = ftell(fp);
  994. fseek(fp, 0, SEEK_SET);
  995. data.Alloc(flen);
  996. Dbg("alloc data:%d",flen);
  997. fread(data.m_pData, 1, flen, fp);
  998. fclose(fp);
  999. if (bClear)
  1000. DeleteZP(Bmp_SCAN);
  1001. return Error_Succeed;
  1002. }
  1003. else {
  1004. Dbg("fopen %s failed!", (const char*)strPath);
  1005. if (bClear)
  1006. DeleteZP(Bmp_SCAN);
  1007. return Error_IO;
  1008. }
  1009. }
  1010. void CIDCertFSM::SelfTest(EntityTestEnum eTestType,CSmartPointer<ITransactionContext> pTransactionContext)
  1011. {
  1012. //for simple
  1013. pTransactionContext->SendAnswer(m_testResult);
  1014. }
  1015. ErrorCodeEnum CIDCertFSM::DeleteFileIfExisted(LPCTSTR fileName)
  1016. {
  1017. if(strlen(fileName) == 0 || strchr(fileName, (int)'*') != NULL)
  1018. {
  1019. Dbg("Invalid or empty fileName(%s)", fileName);
  1020. return Error_Param;
  1021. }
  1022. CSimpleStringA strPath, strAimPath;
  1023. ErrorCodeEnum erroCode = m_pEntity->GetFunction()->GetPath("Dep", strPath);
  1024. strAimPath = strPath + "\\" + fileName;
  1025. //WIN32_FIND_DATA findData;
  1026. ifstream file(strAimPath);
  1027. if(file)
  1028. {
  1029. if(remove((LPCTSTR)strAimPath) != 0)
  1030. {
  1031. Dbg("DeleteFile(%s) suc.", (LPCTSTR)strAimPath);
  1032. return Error_Succeed;
  1033. }
  1034. else
  1035. {
  1036. Dbg("DeleteFile(%s) failed LastError(%d).", (LPCTSTR)strAimPath, GetLastError());
  1037. return Error_Unexpect;
  1038. }
  1039. }
  1040. if(GetLastError() == ERROR_FILE_NOT_FOUND)
  1041. {
  1042. return Error_Succeed;
  1043. }
  1044. file.close();
  1045. Dbg("DeleteFile(%s) Unexpect GetLastError(%d).", (LPCTSTR)strAimPath, GetLastError());
  1046. return Error_Unexpect;
  1047. }
  1048. ErrorCodeEnum CIDCertFSM::GetDevCatInfo(DevCategoryInfo &devInfo, CSimpleStringA& devType)
  1049. {
  1050. LOG_FUNCTION();
  1051. if (m_bRVCIL)
  1052. {
  1053. ZeroMemory(m_devCatInfo.szModel, MAX_DEV_MODEL_LEN);
  1054. ZeroMemory(m_devCatInfo.szType, MAX_DEV_TYPE_LEN);
  1055. ZeroMemory(m_devCatInfo.szVendor, MAX_DEV_VENDOR_LEN);
  1056. return Error_Succeed;
  1057. }
  1058. Dbg("DevCategoryInfo len:%d,%d,%d", strlen(m_devCatInfo.szModel), strlen(m_devCatInfo.szType), strlen(m_devCatInfo.szVendor));
  1059. strncpy(devInfo.szModel, m_devCatInfo.szModel, (MAX_DEV_MODEL_LEN >strlen(m_devCatInfo.szModel)) ? strlen(m_devCatInfo.szModel) : MAX_DEV_MODEL_LEN);
  1060. strncpy(devInfo.szType, m_devCatInfo.szType, (MAX_DEV_TYPE_LEN >strlen(m_devCatInfo.szType)) ? strlen(m_devCatInfo.szType) : MAX_DEV_TYPE_LEN);
  1061. strncpy(devInfo.szVendor, m_devCatInfo.szVendor, (MAX_DEV_VENDOR_LEN >strlen(m_devCatInfo.szVendor)) ? strlen(m_devCatInfo.szVendor) : MAX_DEV_VENDOR_LEN);
  1062. devType = m_devVer;//适配器版本号
  1063. return Error_Succeed;
  1064. }
  1065. int CIDCertFSM::WaitFetchIDCard()
  1066. {
  1067. LOG_FUNCTION();
  1068. ErrorCodeEnum eErr = Error_Unexpect;
  1069. int pos = 99;
  1070. //DWORD64 dwStart = GetTickCount64();
  1071. DWORD64 dwStart = cvGetTickCount();
  1072. DWORD64 dwEnd = 0;
  1073. while (((dwEnd - dwStart) / cvGetTickFrequency()) * 1000< 120 * 1000)//wait for 120 seconds
  1074. {
  1075. eErr = m_pIDCert->QueryCardPos(pos);
  1076. if (eErr == Error_Succeed && pos == 0)
  1077. {
  1078. Dbg("身份证被取回");
  1079. return 0;
  1080. }
  1081. Sleep(1000);
  1082. }
  1083. // BugFix [4/16/2020 14:27 @Gifur]
  1084. return -1;
  1085. }
  1086. void CIDCertFSM::DeleteZP(int type)
  1087. {
  1088. if ((type&Bmp_ZP) == Bmp_ZP)
  1089. {
  1090. Dbg("to delete zp...");
  1091. DeleteFileIfExisted("zp.JPEG");
  1092. DeleteFileIfExisted("zp.jpg");
  1093. DeleteFileIfExisted("zp.bmp"); //All
  1094. DeleteFileIfExisted("wz.txt"); //Sankyo
  1095. DeleteFileIfExisted("xp.wlt");
  1096. DeleteFileIfExisted("ImgTemp.bmp"); //Nantian
  1097. DeleteFileIfExisted("BP8913_ID.bmp"); //Nantian
  1098. }
  1099. if ((type&Bmp_SCAN) == Bmp_SCAN)
  1100. {
  1101. Dbg("to delete id...");
  1102. DeleteFileIfExisted("idfront.bmp");
  1103. DeleteFileIfExisted("idback.bmp");
  1104. DeleteFileIfExisted("idfront.jpg");
  1105. DeleteFileIfExisted("idback.jpg");
  1106. }
  1107. }
  1108. int CIDCertFSM::ReadHZCard(SpReqAnsContext<IDCert_ReadEx_Req, IDCert_ReadEx_Ans>::Pointer ctx)
  1109. {
  1110. //oiltest@20180613 for songrui hz
  1111. //ErrorCodeEnum eErr = m_pIDCert->DevClose();
  1112. //Dbg("before read card,DevClose result:%d", eErr);
  1113. //eErr = ReleaseDevComponent((DeviceBaseClass *&)m_pIDCert);
  1114. //Dbg("before read card,ReleaseDevComponent result:%d", eErr);
  1115. //HANDLE itc = device_open("USB", 0, 9600);
  1116. //if (itc > 0)
  1117. //{
  1118. // BYTE type;
  1119. // PBYTE pID = new BYTE[10];
  1120. // ZeroMemory(pID, 10);
  1121. // int ret = rf_card(itc, 0xfffe, &type, pID);
  1122. // if (ret == 0)
  1123. // {
  1124. // memcpy(ctx->Ans.photodata.m_pData, pID, 4);
  1125. // ctx->Answer(Error_Succeed);
  1126. // Dbg("read card ok.");
  1127. // }
  1128. // else
  1129. // {
  1130. // Dbg("read card failed %d",ret);
  1131. // ctx->Answer(Error_Unexpect);
  1132. // }
  1133. // device_close(itc);
  1134. //}
  1135. //else
  1136. // Dbg("device_open failed result:%x", itc);
  1137. //eErr = CreateDevComponent((DeviceBaseClass *&)m_pIDCert);
  1138. //Dbg("after read card,CreateDevComponent result:%d", eErr);
  1139. //eErr = m_pIDCert->DevOpen(0);
  1140. //Dbg("after read card,devopen result:%d",eErr);
  1141. return 0;
  1142. }
  1143. void CIDCertFSM::CrossTermCall(SpReqAnsContext<IDCert_CrossTermCall_Req, IDCert_CrossTermCall_Ans>::Pointer cctx)
  1144. {
  1145. ErrorCodeEnum eErr = Error_Unexpect;
  1146. Dbg("CrossTermCall,type:%x", cctx->Req.type);
  1147. switch (cctx->Req.type)
  1148. {
  1149. case IDCertService_Method_Read:
  1150. {
  1151. DEFINE_PUPPET_CONTEXT_WITH_TYPE(IDCert_Read_Req, IDCert_Read_Ans);
  1152. if ((eErr = SpBlob2Object(cctx->Req.paramX, ctx->Req)) == Error_Succeed) {
  1153. if (this->ReadInfo(ctx) != 0) {
  1154. eErr = Error_Unexpect;
  1155. }
  1156. else {
  1157. eErr = SpObject2Blob(ctx->Ans, cctx->Ans.retParamX);
  1158. Dbg("after ReadInfo,eErr:%d,paramX.len:%d",eErr, cctx->Ans.retParamX.m_iLength);
  1159. }
  1160. }
  1161. cctx->Answer(eErr);
  1162. }
  1163. break;
  1164. case IDCertService_Method_ReadAndScan:
  1165. {
  1166. DEFINE_PUPPET_CONTEXT_WITH_TYPE(IDCert_ReadAndScan_Req, IDCert_ReadAndScan_Ans);
  1167. if ((eErr = SpBlob2Object(cctx->Req.paramX, ctx->Req)) == Error_Succeed) {
  1168. if (this->ReadAndScan(ctx) != 0) {
  1169. eErr = Error_Unexpect;
  1170. }
  1171. else {
  1172. eErr = SpObject2Blob(ctx->Ans, cctx->Ans.retParamX);
  1173. Dbg("after ReadAndScan,eErr:%d,paramX.len:%d", eErr, cctx->Ans.retParamX.m_iLength);
  1174. }
  1175. }
  1176. cctx->Answer(eErr);
  1177. }
  1178. break;
  1179. case IDCertService_Method_CancelRead:
  1180. m_bCancelRead = true;
  1181. break;
  1182. case IDCertService_Method_ReadWaitMore:
  1183. if (GetReadFlag())
  1184. SetReadMore();
  1185. break;
  1186. case IDCertService_Method_Exit:
  1187. SetExitFlag();
  1188. break;
  1189. default:
  1190. break;
  1191. }
  1192. }
  1193. void CIDCertFSM::CrossTermInvokeInfo(SpOnewayCallContext<IDCert_CrossTermInvokeInfo_Info>::Pointer ctx)
  1194. {
  1195. LOG_FUNCTION();
  1196. Dbg("entity sn:%x,type:%d,errcode:%x", ctx->Info.nEntityID, ctx->Info.type,ctx->Info.result);
  1197. //Dbg("mxx 001[%d]", m_readAndScanCtx->Req.type);
  1198. if (ctx->Info.paramX.m_iLength <= 1)
  1199. {
  1200. }
  1201. ErrorCodeEnum eErr = Error_Unexpect;
  1202. CSmartPointer<ITransactionContext> pTransactionContext;
  1203. switch (ctx->Info.type)
  1204. {
  1205. case IDCertService_Method_Read:
  1206. {
  1207. auto pt = static_cast<SpReqAnsContext<IDCert_Read_Req,
  1208. IDCert_Read_Ans>*>(GetCtx(ctx->Info.type));
  1209. SpReqAnsContext<IDCert_Read_Req,
  1210. IDCert_Read_Ans>::Pointer xctx;
  1211. xctx.Attach(pt);
  1212. eErr = SpBlob2Object(ctx->Info.paramX, xctx->Ans);
  1213. Dbg("eErr:%d,Y len:%d", eErr, ctx->Info.paramX.m_iLength);
  1214. xctx->Answer((ErrorCodeEnum)ctx->Info.result);
  1215. }
  1216. break;
  1217. case IDCertService_Method_ReadAndScan:
  1218. {
  1219. auto pt = static_cast<SpReqAnsContext<IDCert_ReadAndScan_Req,IDCert_ReadAndScan_Ans>*>(m_mapCtx[ctx->Info.type].pCtx);
  1220. SpReqAnsContext<IDCert_ReadAndScan_Req, IDCert_ReadAndScan_Ans>::Pointer tmpctx;
  1221. tmpctx.Attach(pt);
  1222. eErr = SpBlob2Object(ctx->Info.paramX, tmpctx->Ans);
  1223. Dbg("eErr:%d,Y len:%d", eErr, ctx->Info.paramX.m_iLength);
  1224. Dbg("name:%s",(const char*)tmpctx->Ans.name);
  1225. tmpctx->Answer(eErr);
  1226. tmpctx.SubRef();
  1227. m_mapCtx.erase(ctx->Info.type);
  1228. }
  1229. break;
  1230. default:
  1231. break;
  1232. }
  1233. }
  1234. int CIDCertFSM::LocalCallHeartBeat(int method, CBlob &bbSend, bool bTwoWay)
  1235. {
  1236. ErrorCodeEnum eErr = Error_Unexpect;
  1237. if (m_pHBClient == NULL)
  1238. {
  1239. m_pHBClient = new HeartBeatService_ClientBase(GetEntityBase());
  1240. if (m_pHBClient == NULL)
  1241. {
  1242. Dbg("create HeartBeat client failed.GetLastError:%d", GetLastError());
  1243. return -1;
  1244. }
  1245. eErr = m_pHBClient->Connect();
  1246. if (eErr != Error_Succeed)
  1247. {
  1248. Dbg("client connect to HeartBeat failed:%x", eErr);
  1249. m_pHBClient->SafeDelete();
  1250. m_pHBClient = NULL;
  1251. return -1;
  1252. }
  1253. }
  1254. HeartBeatService_CrossTermCall_Req req;
  1255. HeartBeatService_CrossTermCall_Ans ans;
  1256. req.nEntityID = 0x201;
  1257. req.isTwoWay = bTwoWay;
  1258. req.fromTerminalNo = m_terminalNo;
  1259. GetAttachedTerminal(req.toTerminalNo);
  1260. //req.toTerminalNo = "7717715004";
  1261. req.param1 = "";
  1262. req.param2 = "";
  1263. req.paramX = bbSend;
  1264. req.type = method;
  1265. 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);
  1266. eErr = m_pHBClient->CrossTermCall(req, ans, 20000);
  1267. Dbg("CrossTermCall to HeartBeat result:%x", eErr);
  1268. m_pHBClient->SafeDelete();
  1269. m_pHBClient = NULL;//resource cost?
  1270. return 0;
  1271. }
  1272. bool CIDCertFSM::GetAttachedTerminal(CSimpleStringA &csTerm)
  1273. {
  1274. return (Error_Succeed == GetEntityBase()->GetFunction()->GetSysVar("AttachedTerminal", csTerm));
  1275. }
  1276. void CIDCertFSM::LogErrMsg(const char *pMsgHead, ErrorCodeEnum eErrCode,
  1277. DWORD defaultDevCode /*= 0*/, BOOL bAlarm /*= FALSE*/)
  1278. {
  1279. WORD wdErrCode = 0;
  1280. CSimpleStringA csErrMsg(true);
  1281. ErrorCodeEnum ec = CCommDevFSM::GetAndSplitDevErrInfo(csErrMsg, wdErrCode, "");
  1282. if(ec == Error_Succeed && wdErrCode != 0) {
  1283. //oilyang@20200525 if have been set ErrorCode of entity defined (not device),use it directly
  1284. if ((defaultDevCode >> 20) == m_entCode.dwEntityId)
  1285. UpdateDEC(defaultDevCode);
  1286. else
  1287. UpdateDEC(wdErrCode);
  1288. } else if(defaultDevCode != 0) {
  1289. UpdateDEC(defaultDevCode);
  1290. }
  1291. m_csAlarmMsg = CSimpleStringA::Format("%s failed EC= 0x%x : %s", pMsgHead, eErrCode, (LPCTSTR)csErrMsg);
  1292. if(bAlarm) {
  1293. LogWarn(Severity_High, eErrCode, AlarmDECToBusiness(), (LPCTSTR)m_csAlarmMsg);
  1294. }
  1295. return;
  1296. }
  1297. int CIDCertFSM::Initial()
  1298. {
  1299. CSystemStaticInfo sysInfo;
  1300. GetEntityBase()->GetFunction()->GetSystemStaticInfo(sysInfo);
  1301. m_csMachineType = sysInfo.strMachineType;
  1302. m_terminalNo = sysInfo.strTerminalID;
  1303. Dbg("machineType:%s,terminalNo:%s", (const char*)m_csMachineType, (const char*)m_terminalNo);
  1304. if (_strnicmp((const char*)m_csMachineType, "RVC.IL", strlen("RVC.IL")) == 0)
  1305. {
  1306. m_bRVCIL = true;
  1307. return 0;
  1308. }
  1309. m_devState = DEVICE_STATUS_NOT_READY;
  1310. //to do device init
  1311. int initCount = 0;
  1312. ErrorCodeEnum err;
  1313. m_pIDCert = NULL;
  1314. bool bCreateDevCom = false;
  1315. ErrorCodeEnum eErrDev;
  1316. CSimpleStringA dllName, tmpVendor(""), tmpDevSN(""),tmpDLLVersion, csDepPath(""), csBackslash("\\");
  1317. CSmartPointer<IEntityFunction> spEntityFunction = GetEntityBase()->GetFunction();
  1318. CSmartPointer<IConfigInfo> spConfig;
  1319. eErrDev = spEntityFunction->OpenConfig(Config_Root, spConfig);
  1320. if (eErrDev != Error_Succeed) {
  1321. Dbg("open cfg file failed!");
  1322. LogErrMsg("Initial:OpenConfig", eErrDev, AlarmDECToBusiness(MEC_CFG_RUN_POINTER_FAILED),TRUE);
  1323. return 2;
  1324. }
  1325. GetEntityBase()->GetFunction()->GetSysVar("FWBVendor", tmpVendor);
  1326. GetEntityBase()->GetFunction()->GetSysVar("FWBDevSN", tmpDevSN);
  1327. GetEntityBase()->GetFunction()->GetSysVar("FWBVersion", tmpDLLVersion);
  1328. if (tmpDLLVersion.GetLength() < 2)//如果忘记配置,则直接使用默认值8.1
  1329. tmpDLLVersion = "8.1";
  1330. if (tmpDevSN.GetLength() > 12 && tmpDevSN.IndexOf("FWB") > 2)
  1331. {
  1332. Dbg("This is fwb device.");
  1333. GetEntityBase()->GetFunction()->GetPath("Dep", csDepPath);
  1334. dllName = csDepPath + csBackslash + "IDCertificate." + tmpVendor + "." + tmpDLLVersion + ".dll";//oiltmp
  1335. Dbg("%s", (const char*)dllName);
  1336. HARDWARE_ENTITY_SET_VENDOR_NAME(m_entCode, tmpVendor);
  1337. }
  1338. else
  1339. {
  1340. eErrDev = SpGetDevAdaptorPath(m_pEntity, GetEntityBase()->GetEntityName(), dllName);
  1341. if (eErrDev != Error_Succeed){
  1342. Dbg("get vendor dll(%s)name failed.", (LPCTSTR)dllName);
  1343. return 2;
  1344. }
  1345. CSmartPointer<IConfigInfo> pConfig;
  1346. ErrorCodeEnum rc = GetEntityBase()->GetFunction()->OpenConfig(Config_Root, pConfig);
  1347. CSimpleStringA strSection = CSimpleStringA("Device.") + GetEntityBase()->GetEntityName();
  1348. CSimpleStringA str;
  1349. pConfig->ReadConfigValue(strSection, "Vendor", str);
  1350. HARDWARE_ENTITY_SET_VENDOR_NAME(m_entCode, str);
  1351. }
  1352. Dbg("%s", (LPCTSTR)dllName);
  1353. spConfig->ReadConfigValue("Device.IDCertificate", "Vendor", m_devVendor);
  1354. spConfig->ReadConfigValue("Device.IDCertificate", "Version", m_devVer);
  1355. spConfig->ReadConfigValue("Device.IDCertificate", "Batch", m_devBatch);
  1356. Dbg("Vendor=%s#Version=%s#Batch=%s", (const char*)m_devVendor, (const char*)m_devVer, (const char*)m_devBatch);
  1357. /*------2020-02-13-------*/
  1358. //modify by LZM
  1359. eErrDev = VendorLogControler(this, "IDCertificate");
  1360. if (eErrDev != Error_Succeed)
  1361. {
  1362. Dbg("VendorLogControler Failed...");
  1363. }
  1364. /*-----------------------*/
  1365. m_hVerdorDll = LoadLibraryA(dllName);
  1366. if (m_hVerdorDll == NULL)
  1367. {
  1368. Dbg("load verdor dll failed(%d).", GetLastError());
  1369. return 2;
  1370. }
  1371. if ((CreateDevComponent = (lpCreateDevCom)GetProcAddress(m_hVerdorDll, "CreateDevComponent")) == NULL)
  1372. {
  1373. Dbg("Get CreateDevComponent failed(%d).", GetLastError());
  1374. return 2;
  1375. }
  1376. if ((ReleaseDevComponent = (lpReleaseDevCom)GetProcAddress(m_hVerdorDll, "ReleaseDevComponent")) == NULL)
  1377. {
  1378. Dbg("Get ReleaseDevComponent failed(%d).", GetLastError());
  1379. return 2;
  1380. }
  1381. Dbg("Get function suc.");
  1382. bool bOpenFlag = false;
  1383. do {
  1384. if (!bCreateDevCom)
  1385. {
  1386. if (CreateDevComponent((DeviceBaseClass * &)m_pIDCert) != Error_Succeed)
  1387. {
  1388. LOG_TRACE("Create IDCertificateReader module failed.");
  1389. initCount++;
  1390. m_pIDCert = NULL;
  1391. if (initCount >= IDCER_INIT_COUNT)
  1392. break;
  1393. else
  1394. continue;
  1395. }
  1396. bCreateDevCom = true;
  1397. LOG_TRACE("Create IDCertificateReader module succeeded.");
  1398. }
  1399. // IDCerInitParam initParam;
  1400. // initParam.ePortType = IDCER_PORTCOM;
  1401. int tmpPort = 0;
  1402. spConfig->ReadConfigValueInt("Device.IDCertificate", "Port", tmpPort);
  1403. Dbg("IDCer open with port %d", tmpPort);
  1404. err = m_pIDCert->DevOpen(tmpPort);
  1405. if (err != Error_Succeed)
  1406. {
  1407. LogErrMsg("Initial::DevOpen", err, MEC_DEVAPI_IDCER_DevOpen);
  1408. LogWarn(Severity_Middle, err, AlarmDEC(), "IDCertificate init failed.");
  1409. return 2;
  1410. //initCount++;
  1411. //m_pIDCert = NULL;
  1412. //if (initCount >= IDCER_INIT_COUNT)
  1413. // break;
  1414. //else
  1415. // continue;
  1416. }
  1417. } while (m_pIDCert == NULL && bOpenFlag == false);
  1418. if (m_pIDCert != NULL)
  1419. {
  1420. err = m_pIDCert->IDCerRFControl(false);
  1421. if (err != Error_Succeed)
  1422. LOG_TRACE("close rf failed.");
  1423. else
  1424. LOG_TRACE("close rf ok.");
  1425. }
  1426. ZeroMemory(m_devCatInfo.szModel, MAX_DEV_MODEL_LEN);
  1427. ZeroMemory(m_devCatInfo.szType, MAX_DEV_TYPE_LEN);
  1428. ZeroMemory(m_devCatInfo.szVendor, MAX_DEV_VENDOR_LEN);
  1429. eErrDev = m_pIDCert->GetDevCategory(m_devCatInfo);
  1430. if (eErrDev == Error_Succeed)
  1431. {
  1432. Dbg("%d,%d,%d", strlen(m_devCatInfo.szModel), strlen(m_devCatInfo.szType), strlen(m_devCatInfo.szVendor));
  1433. if (strlen(m_devCatInfo.szModel) < 256)
  1434. Dbg("%s", m_devCatInfo.szModel);
  1435. }
  1436. if (m_pIDCert != NULL)
  1437. {
  1438. m_devInit = true;
  1439. return 0;
  1440. }
  1441. else
  1442. return 2;
  1443. return 0;
  1444. }