CustMngrAuthFSM.cpp 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048
  1. #include "CustMngrAuthFSM.h"
  2. #include "CustMngrAuth_UserErrorCode.h"
  3. #include "SpHelper.h"
  4. #include "fileutil.h"
  5. #include <fstream>
  6. #include <map>
  7. #include <memory>
  8. #include <thread>
  9. #include <chrono>
  10. #include <string>
  11. #include <ctime>
  12. #include <cstring>
  13. using std::string;
  14. const int UPDATE_INTERNAL = 10 * 60 * 1000; //query data from branch server internal, 10min
  15. const int CONNECT_INTERNAL = 10 * 60 * 1000; //connect branch server internal, 10min
  16. const int FINGER_NUM = 8; //suuport max register finger num
  17. const int COLLECT_TIMES = 3; //press finger times
  18. #define IMAGE_NUM 3
  19. #define RUNINFOFILE "CustMngrAuth.ini"
  20. #define RUNINFOFILE_BAK "CustMngrAuth_bak.ini"
  21. class CCustMngrAuthEntity;
  22. #pragma region event response
  23. void CCustMngrAuthFSM::s0_on_entry()
  24. {
  25. LOG_FUNCTION();
  26. FSMEvent* pEvt = new FSMEvent(USER_EVT_INIT);
  27. PostEventFIFO(pEvt);
  28. }
  29. void CCustMngrAuthFSM::s0_on_exit()
  30. {
  31. LOG_FUNCTION();
  32. }
  33. unsigned int CCustMngrAuthFSM::s0_on_event(FSMEvent* pEvt)
  34. {
  35. LOG_FUNCTION();
  36. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("s0 evt %d", pEvt->iEvt);
  37. switch (pEvt->iEvt)
  38. {
  39. case USER_EVT_INIT:
  40. {
  41. InitTask* task = new InitTask(this);
  42. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);
  43. pEvt->SetHandled();
  44. return 0;
  45. }
  46. break;
  47. default:
  48. break;
  49. }
  50. return 0;
  51. }
  52. void CCustMngrAuthFSM::s1_on_entry()
  53. {
  54. LOG_FUNCTION();
  55. }
  56. void CCustMngrAuthFSM::s1_on_exit()
  57. {
  58. LOG_FUNCTION();
  59. }
  60. unsigned int CCustMngrAuthFSM::s1_on_event(FSMEvent* pEvt)
  61. {
  62. LOG_FUNCTION();
  63. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("s1 evt %d", pEvt->iEvt);
  64. switch (pEvt->iEvt)
  65. {
  66. case USER_EVT_INIT_FINISHED:
  67. pEvt->SetHandled();
  68. return pEvt->param1;
  69. default:
  70. break;
  71. }
  72. return 0;
  73. }
  74. void CCustMngrAuthFSM::s2_on_entry()
  75. {
  76. LOG_FUNCTION();
  77. }
  78. void CCustMngrAuthFSM::s2_on_exit()
  79. {
  80. LOG_FUNCTION();
  81. }
  82. unsigned int CCustMngrAuthFSM::s2_on_event(FSMEvent* pEvt)
  83. {
  84. LOG_FUNCTION();
  85. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("s2 evt(%d)", pEvt->iEvt);
  86. switch (pEvt->iEvt)
  87. {
  88. case USER_EVT_AUTHORIZE_START:
  89. {
  90. pEvt->SetHandled();
  91. AuthorizeStartEvent* authorEvt = dynamic_cast<AuthorizeStartEvent*>(pEvt);
  92. if (authorEvt->ctx->Req.TimeLimit <= 0)
  93. {
  94. AuthorizeFinishedEvent* e = new AuthorizeFinishedEvent();
  95. e->ctx = authorEvt->ctx;
  96. e->param1 = Error_Param;
  97. this->PostEventFIFO(e);
  98. break;
  99. }
  100. //time here should be less than the web
  101. m_TimeLimit = (authorEvt->ctx->Req.TimeLimit - 3) * 1000;
  102. m_bCancelAuthorize = false;
  103. MatchFingerPrintTask* matchTask = new MatchFingerPrintTask(this);
  104. matchTask->ctx = authorEvt->ctx;
  105. GetEntityBase()->GetFunction()->PostThreadPoolTask(matchTask);
  106. }
  107. break;
  108. case USER_EVT_QUERYAUTHORINFO:
  109. {
  110. pEvt->SetHandled();
  111. QueryAuthorInfoEvent* queryEvt = dynamic_cast<QueryAuthorInfoEvent*>(pEvt);
  112. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER)("time = %d", queryEvt->ctx->Req.TimeLimit);
  113. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER)("wayOfKey = %d", queryEvt->ctx->Req.WayofKey);
  114. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER)("wayOfFinger = %d", queryEvt->ctx->Req.WayofFinger);
  115. if (queryEvt->ctx->Req.TimeLimit <= 0)
  116. {
  117. LOG_TRACE("ERROR: receive timelimit lessequal than zero. ctx->Answer(error_Param)");
  118. QueryAuthorInfoFinishedEvent* e = new QueryAuthorInfoFinishedEvent();
  119. e->ctx = queryEvt->ctx;
  120. e->param1 = Error_Param;
  121. this->PostEventFIFO(e);
  122. break;
  123. }
  124. m_TimeLimit = (queryEvt->ctx->Req.TimeLimit - 2) * 1000;
  125. ErrorCodeEnum eErr;
  126. m_bCancelAuthorize = false;
  127. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER)("WayOfKey = %d, WayOfFinger = %d", queryEvt->ctx->Req.WayofKey, queryEvt->ctx->Req.WayofFinger);
  128. if (queryEvt->ctx->Req.WayofKey == 1)
  129. {
  130. //打开USB
  131. }
  132. if (queryEvt->ctx->Req.WayofFinger == 1)
  133. {
  134. //起指纹匹配线程
  135. MatchFingerPrintTaskEx* mTask = new MatchFingerPrintTaskEx(this);
  136. mTask->ctx = queryEvt->ctx;
  137. GetEntityBase()->GetFunction()->PostThreadPoolTask(mTask);
  138. }
  139. }
  140. break;
  141. case USER_EVT_COLLECTFINGERPRINT_START:
  142. {
  143. pEvt->SetHandled();
  144. CollectFingerPrintStartEvent* collectEvt = dynamic_cast<CollectFingerPrintStartEvent*>(pEvt);
  145. CollectFingerPrintTask* collectTask = new CollectFingerPrintTask(this);
  146. collectTask->ctx = collectEvt->ctx;
  147. GetEntityBase()->GetFunction()->PostThreadPoolTask(collectTask);
  148. }
  149. break;
  150. case USER_EVT_COLLECTFINGERPRINTEX_START:
  151. {
  152. pEvt->SetHandled();
  153. CollectFingerPrintExStartEvent* collectEvt = dynamic_cast<CollectFingerPrintExStartEvent*>(pEvt);
  154. CollectFingerPrintExTask* collectExTask = new CollectFingerPrintExTask(this);
  155. collectExTask->ctx = collectEvt->ctx;
  156. GetEntityBase()->GetFunction()->PostThreadPoolTask(collectExTask);
  157. }
  158. break;
  159. case USER_EVT_COLLECTFINGERPRINTINFO:
  160. {
  161. pEvt->SetHandled();
  162. CollectFingerPrintInfoEvent* collectEvt = dynamic_cast<CollectFingerPrintInfoEvent*>(pEvt);
  163. CollectFingerPrintInfoTask* collectExTask = new CollectFingerPrintInfoTask(this);
  164. collectExTask->ctx = collectEvt->ctx;
  165. GetEntityBase()->GetFunction()->PostThreadPoolTask(collectExTask);
  166. }
  167. break;
  168. case USER_EVT_GENERATE_TEMPLATE:
  169. {
  170. pEvt->SetHandled();
  171. GenerateTemplateEvent* gtEvt = dynamic_cast<GenerateTemplateEvent*>(pEvt);
  172. GenerateTemplateTask* gtTask = new GenerateTemplateTask(this);
  173. gtTask->ctx = gtEvt->ctx;
  174. GetEntityBase()->GetFunction()->PostThreadPoolTask(gtTask);
  175. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Generate Template task posted");
  176. }
  177. break;
  178. case USER_EVT_SAVEFINGERPRINT_START:
  179. {
  180. pEvt->SetHandled();
  181. SaveFingerPrintStartEvent* saveEvtfpe = dynamic_cast<SaveFingerPrintStartEvent*>(pEvt);
  182. SaveFingerPrintTask* saveTask = new SaveFingerPrintTask(this);
  183. saveTask->ctx = saveEvtfpe->ctx;
  184. GetEntityBase()->GetFunction()->PostThreadPoolTask(saveTask);
  185. }
  186. break;
  187. case USER_EVT_CHECKUKEY:
  188. {
  189. pEvt->SetHandled();
  190. //SwitchUSB(true);
  191. }
  192. break;
  193. default:
  194. break;
  195. }
  196. return 0;
  197. }
  198. void CCustMngrAuthFSM::s3_on_entry()
  199. {
  200. LOG_FUNCTION();
  201. ScheduleTimer(1, m_TimeLimit);
  202. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("set timer when match, %dms", m_TimeLimit);
  203. }
  204. void CCustMngrAuthFSM::s3_on_exit()
  205. {
  206. LOG_FUNCTION();
  207. CancelTimer(1);
  208. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("exit match event, timer canceled");
  209. }
  210. unsigned int CCustMngrAuthFSM::s3_on_event(FSMEvent* pEvt)
  211. {
  212. LOG_FUNCTION();
  213. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("s3 evt %d, %d", pEvt->iEvt, pEvt->param1);
  214. switch (pEvt->iEvt)
  215. {
  216. case USER_EVT_HOLDON:
  217. {
  218. pEvt->SetHandled();
  219. CancelTimer(1);
  220. HoldOnEvent* holdEvt = dynamic_cast<HoldOnEvent*>(pEvt);
  221. if (holdEvt->ctx == NULL)
  222. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("HoldOnEvent->ctx is NULL");
  223. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER)("HoldOn with MoreTime: %d second", holdEvt->ctx->Req.MoreTime);
  224. int moreTime = holdEvt->ctx->Req.MoreTime * 1000;
  225. ScheduleTimer(1, (moreTime > 0) ? moreTime : m_TimeLimit);
  226. }
  227. break;
  228. case EVT_TIMER:
  229. {
  230. pEvt->SetHandled();
  231. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Hit Timer");
  232. m_bAuthorizeTimeout = true;
  233. m_bCancelAuthorize = true;
  234. CancelAuthorize();
  235. }
  236. break;
  237. case USER_EVT_AUTHORIZE_FINISHED:
  238. {
  239. pEvt->SetHandled();
  240. AuthorizeFinishedEvent* authorEvt = dynamic_cast<AuthorizeFinishedEvent*>(pEvt);
  241. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Checking m_authCtx and answer ctx");
  242. if (authorEvt->param1 == 0)
  243. {
  244. if (m_authCtx.eAuthByWhich == AuthByUkey)
  245. {
  246. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("m_authCtx.eAuthByWhich == AuthByUkey");
  247. if (m_pFingerPrint != NULL && !m_pFingerPrint->QuerySessionClosed())
  248. {
  249. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Invoke cancel match.");
  250. m_pFingerPrint->CancelMatch();
  251. }
  252. authorEvt->ctx->Ans.WayofAuth = AuthByUkey;
  253. authorEvt->ctx->Ans.UkeyID = m_authCtx.UkeyID;
  254. authorEvt->ctx->Answer(Error_Succeed);
  255. }
  256. else if (m_authCtx.eAuthByWhich == AuthByFngPrnt)
  257. {
  258. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("m_authCtx.eAuthByWhich == AuthByFngPrnt");
  259. authorEvt->ctx->Ans.WayofAuth = AuthByFngPrnt;
  260. authorEvt->ctx->Ans.CustomerID = m_authCtx.CustomerID;
  261. authorEvt->ctx->Answer(Error_Succeed);
  262. }
  263. }
  264. else
  265. {
  266. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("authorize finished with param1 as %d", authorEvt->param1);
  267. authorEvt->ctx->Answer((ErrorCodeEnum)authorEvt->param1);
  268. }
  269. if (m_pFingerPrint != NULL && !m_pFingerPrint->QuerySessionClosed())
  270. {
  271. //m_bCancelAuthorize = true; //已结束授权,无用?
  272. m_pFingerPrint->GetFunction()->CloseSession();
  273. m_pFingerPrint = NULL;
  274. }
  275. m_authCtx.eAuthByWhich = AuthByNone;
  276. m_authCtx.CustomerID = "";
  277. m_authCtx.UkeyID = "";
  278. m_ctx = NULL;
  279. }
  280. break;
  281. case USER_EVT_QUERYAUTHORINFO_FINISHED:
  282. {
  283. pEvt->SetHandled();
  284. QueryAuthorInfoFinishedEvent* qafEvt = dynamic_cast<QueryAuthorInfoFinishedEvent*>(pEvt);
  285. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Checking m_authCtx and answer qaInfoCtx");
  286. if (qafEvt->param1 == 0)
  287. {
  288. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER)("authorize finished with param1 as 0"); //Error_Succeed
  289. if (m_authCtx.eAuthByWhich == AuthByUkey)
  290. {
  291. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("m_authCtx.eAuthByWhich == AuthByUkey");
  292. if (m_pFingerPrint != NULL && !m_pFingerPrint->QuerySessionClosed())
  293. {
  294. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Invoke cancel collect.");
  295. m_pFingerPrint->CancelRegister();
  296. }
  297. qafEvt->ctx->Ans.WayofAuth = AuthByUkey;
  298. qafEvt->ctx->Ans.UkeyID = m_authCtx.UkeyID;
  299. qafEvt->ctx->Answer(Error_Succeed);
  300. }
  301. else if (m_authCtx.eAuthByWhich == AuthByFngPrnt)
  302. {
  303. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("m_authCtx.eAuthByWhich == AuthByFngPrnt");
  304. qafEvt->ctx->Ans.WayofAuth = AuthByFngPrnt;
  305. qafEvt->ctx->Ans.Feature = m_authCtx.Feature;
  306. qafEvt->ctx->Ans.FingerImg = m_authCtx.FingerImg;
  307. qafEvt->ctx->Answer(Error_Succeed);
  308. }
  309. }
  310. else
  311. {
  312. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("authorize finished with param1 as %d", qafEvt->param1);
  313. qafEvt->ctx->Answer((ErrorCodeEnum)qafEvt->param1, qafEvt->param2);
  314. }
  315. if (m_pFingerPrint != NULL && !m_pFingerPrint->QuerySessionClosed())
  316. {
  317. m_bCancelAuthorize = true;
  318. m_pFingerPrint->GetFunction()->CloseSession();
  319. m_pFingerPrint = NULL;
  320. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("M_pFingerPrint disconnected.");
  321. }
  322. m_authCtx.eAuthByWhich = AuthByNone;
  323. m_authCtx.CustomerID = "";
  324. m_authCtx.UkeyID = "";
  325. m_authCtx.Feature = "";
  326. memset(&m_authCtx.FingerImg, 0, sizeof(m_authCtx.FingerImg));
  327. qaInfoCtx = NULL;
  328. }
  329. break;
  330. case USER_EVT_AUTHORIZE_CANCEL:
  331. {
  332. pEvt->SetHandled();
  333. m_bCancelAuthorize = true;
  334. CancelAuthorize();
  335. }
  336. break;
  337. case USER_EVT_QUERYAUTHORINFO_CANCEL:
  338. {
  339. pEvt->SetHandled();
  340. m_bCancelAuthorize = true;
  341. CancelCollect();
  342. }
  343. break;
  344. default:
  345. break;
  346. }
  347. return 0;
  348. }
  349. void CCustMngrAuthFSM::s4_on_entry()
  350. {
  351. LOG_FUNCTION();
  352. }
  353. void CCustMngrAuthFSM::s4_on_exit()
  354. {
  355. LOG_FUNCTION();
  356. }
  357. unsigned int CCustMngrAuthFSM::s4_on_event(FSMEvent* pEvt)
  358. {
  359. LOG_FUNCTION();
  360. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("s4 evt %d, %d", pEvt->iEvt, pEvt->param1);
  361. switch (pEvt->iEvt)
  362. {
  363. case USER_EVT_COLLECTFINGERPRINT_FINISHED:
  364. pEvt->SetHandled();
  365. break;
  366. case USER_EVT_COLLECTFINGERPRINTEX_FINISHED:
  367. pEvt->SetHandled();
  368. break;
  369. case USER_EVT_COLLECTFINGERPRINTINFO_FINISHED:
  370. pEvt->SetHandled();
  371. break;
  372. case USER_EVT_GENERATE_TEMPLATE_FINISHED:
  373. pEvt->SetHandled();
  374. break;
  375. case USER_EVT_COLLECTFINGERPRINT_CANCEL:
  376. {
  377. pEvt->SetHandled();
  378. if (m_pFingerPrint != NULL && !m_pFingerPrint->QuerySessionClosed())
  379. m_pFingerPrint->CancelRegister();
  380. }
  381. break;
  382. case USER_EVT_SAVEFINGERPRINT_FINISHED:
  383. pEvt->SetHandled();
  384. break;
  385. case USER_EVT_CHECKUKEY_FINISHED:
  386. {
  387. pEvt->SetHandled();
  388. }
  389. break;
  390. default:
  391. break;
  392. }
  393. return 0;
  394. }
  395. void CCustMngrAuthFSM::s5_on_entry()
  396. {
  397. LOG_FUNCTION();
  398. }
  399. void CCustMngrAuthFSM::s5_on_exit()
  400. {
  401. LOG_FUNCTION();
  402. }
  403. unsigned int CCustMngrAuthFSM::s5_on_event(FSMEvent* pEvt)
  404. {
  405. LOG_FUNCTION();
  406. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("s5 evt %d, %d", pEvt->iEvt, pEvt->param1);
  407. return 0;
  408. }
  409. #pragma endregion
  410. #pragma region invoked function
  411. ErrorCodeEnum CCustMngrAuthFSM::WaitForUkey(ErrorCodeEnum eErr)
  412. {
  413. int status = (eErr == Error_NoTarget) ? 4 : 3;//4: no data in local, 3:enitty exception
  414. BroadcastGetFinger(status);
  415. while (1)
  416. {
  417. SLEEP(300);
  418. if (m_bCancelAuthorize)
  419. {
  420. m_bCancelAuthorize = false;
  421. if (m_bAuthorizeTimeout)
  422. {
  423. m_bAuthorizeTimeout = false;
  424. return Error_TimeOut;
  425. }
  426. else
  427. return Error_Cancel;
  428. }
  429. }
  430. }
  431. void CCustMngrAuthFSM::CancelAuthorize()
  432. {
  433. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Invoke m_pFingerPrint->CancelMatch()");
  434. if (m_pFingerPrint != NULL && !m_pFingerPrint->QuerySessionClosed())
  435. m_pFingerPrint->CancelMatch();
  436. }
  437. void CCustMngrAuthFSM::CancelCollect()
  438. {
  439. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Invoke m_pFingerPrint->CancelRegister()");
  440. if (m_pFingerPrint != NULL && !m_pFingerPrint->QuerySessionClosed())
  441. m_pFingerPrint->CancelRegister();
  442. }
  443. ErrorCodeEnum CCustMngrAuthFSM::SwitchUSB(bool bOpen)
  444. {
  445. LOG_FUNCTION();
  446. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("connecting DeviceControl");
  447. ErrorCodeEnum errCode;
  448. m_pDeviceControl = new DeviceControlService_ClientBase(GetEntityBase());
  449. if (m_pDeviceControl != NULL)
  450. {
  451. errCode = m_pDeviceControl->Connect();
  452. if (errCode != Error_Succeed)
  453. {
  454. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("m_pDeviceControl connect failed with errCode(%s)", SpStrError(errCode));
  455. m_pDeviceControl->SafeDelete();
  456. }else {
  457. if (bOpen)
  458. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Open USB");
  459. else
  460. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Close USB");
  461. DeviceControlService_USB_Req usbReq;
  462. DeviceControlService_USB_Ans usbAns;
  463. usbReq.open = bOpen;//open or close usb
  464. errCode = m_pDeviceControl->USB(usbReq, usbAns, 2000);
  465. if (errCode != Error_Succeed)
  466. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Open/Close usb failed.");
  467. else
  468. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Open/Close usb success.");
  469. m_pDeviceControl->GetFunction()->CloseSession();
  470. }
  471. m_pDeviceControl = NULL;
  472. return errCode;
  473. }
  474. else {
  475. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("DeviceControl is null.");
  476. return Error_Unexpect;
  477. }
  478. }
  479. #pragma endregion
  480. #pragma region entity init
  481. ErrorCodeEnum CCustMngrAuthFSM::OnInit()
  482. {
  483. LOG_FUNCTION();
  484. CSystemStaticInfo staticInfo;
  485. m_pEntity->GetFunction()->GetSystemStaticInfo(staticInfo);
  486. m_TerminalID = staticInfo.strTerminalID;
  487. m_csMachineType = staticInfo.strMachineType;
  488. m_authCtx.eAuthByWhich = AuthByNone;
  489. m_FingerSection = "FingerInfo";
  490. return Error_Succeed;
  491. }
  492. int CCustMngrAuthFSM::Initial()
  493. {
  494. ErrorCodeEnum errCode = OpenRunInfoFile();
  495. if (errCode != Error_Succeed)
  496. return 1;
  497. return 0;
  498. }
  499. ErrorCodeEnum CCustMngrAuthFSM::OpenRunInfoFile()
  500. {
  501. ErrorCodeEnum errCode;
  502. errCode = GetEntityBase()->GetFunction()->GetPath("RunInfo", m_RunInfoPath);
  503. if (errCode != Error_Succeed)
  504. {
  505. LogError(Severity_High, Error_DevLoadFileFailed
  506. , LOG_ERR_CUSTMNGRAUTH_GET_RUNINFO_PATH_FAILED_Init
  507. , "get runinfo path failed while init");
  508. errCode = Error_Unexpect;
  509. }
  510. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("runinfo path: %s", m_RunInfoPath.GetData());
  511. return errCode;
  512. }
  513. #pragma endregion
  514. #pragma region entity exit
  515. ErrorCodeEnum CCustMngrAuthFSM::OnExit()
  516. {
  517. LOG_FUNCTION();
  518. FSMImpl<CCustMngrAuthFSM>::OnExit();
  519. return Error_Succeed;
  520. }
  521. #pragma endregion
  522. #pragma region update feature process
  523. void CCustMngrAuthFSM::FeatureUpdate()
  524. {
  525. LOG_FUNCTION();
  526. InitBeforeUpdateData();
  527. int waitInternal = UPDATE_INTERNAL;
  528. while (true)
  529. {
  530. do
  531. {
  532. int connectFailedTimes = 0;
  533. m_pConnection = new FeatureUpdateConn(m_pEntity, this);
  534. if (m_pConnection->ConnectFromCentralSetting() && m_pConnection->IsConnectionOK())
  535. {
  536. connectFailedTimes = 0;
  537. waitInternal = UPDATE_INTERNAL;
  538. ErrorCodeEnum errCode;
  539. CSmartPointer<IConfigInfo> spConfig;
  540. CAutoArray<CSimpleStringA> transArray;
  541. RunInfoParams runInfoParam;
  542. memset(&runInfoParam, 0, sizeof(runInfoParam));
  543. errCode = InitBeforeQueryData(runInfoParam, spConfig);
  544. if (errCode != Error_Succeed)
  545. break;
  546. errCode = ReceiveDataFromServer(transArray, runInfoParam);
  547. if (errCode != Error_Succeed)
  548. break;
  549. errCode = BackupBeforeWriteData(runInfoParam, spConfig);
  550. if (errCode != Error_Succeed)
  551. break;
  552. errCode = WriteData(runInfoParam, transArray, spConfig);
  553. if (errCode != Error_Succeed)
  554. break;
  555. }else {
  556. connectFailedTimes++;
  557. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("connect branchserver failed for %d times, try again in %d ms."
  558. , connectFailedTimes, CONNECT_INTERNAL);
  559. if (connectFailedTimes >= 60)
  560. {
  561. //30min give a warn
  562. LogWarn(Severity_Middle, Error_Unexpect
  563. , LOG_ERR_CUSTMNGRAUTH_FEATUPDATE_CONNECT_FAILED
  564. , "Connect branch server failed.");
  565. connectFailedTimes = 0;
  566. }
  567. waitInternal = CONNECT_INTERNAL;
  568. }
  569. } while (false);
  570. if (m_pConnection)
  571. {
  572. m_pConnection->Close();
  573. m_pConnection->DecRefCount();
  574. m_pConnection = NULL;
  575. }
  576. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Feature-update processed, wait until next");
  577. //wait for next query
  578. if (WaitForSingleObject(hStopUpdate, waitInternal) == WAIT_OBJECT_0)
  579. break;
  580. }
  581. }
  582. /// <summary>
  583. /// Init memory data
  584. /// </summary>
  585. void CCustMngrAuthFSM::InitBeforeUpdateData()
  586. {
  587. bool bHasData = false;
  588. bool bReadResult = ReadDataIntoMemory(bHasData);
  589. if (bReadResult && bHasData)
  590. {
  591. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Read feature data into memory success.");
  592. }
  593. else if (bReadResult && !bHasData) {
  594. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Has no data in local file.");
  595. }
  596. else {
  597. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Read feature data into memory failed, wait next read.");
  598. }
  599. }
  600. /// <summary>
  601. /// read fingerprint feature data into memory from local file
  602. /// </summary>
  603. /// <param name="bHasData">output param</param>
  604. /// <returns></returns>
  605. bool CCustMngrAuthFSM::ReadDataIntoMemory(bool& bHasData)
  606. {
  607. LOG_FUNCTION();
  608. CSimpleStringA runInfoFile(true);
  609. runInfoFile = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR RUNINFOFILE
  610. , m_RunInfoPath.GetData());
  611. std::ifstream inFile(runInfoFile.GetData());
  612. string line;
  613. int customerNum = 0;
  614. ULLINT startReadFile = RVCGetTickCount();
  615. ULLINT endReadFile = RVCGetTickCount();
  616. while (getline(inFile, line))
  617. {
  618. if (line.length() <= 0)
  619. continue;
  620. string tempLine = ClearStringSpaceHeadTail(line);
  621. if (!tempLine.compare("[UpdateTime]") || string::npos != tempLine.find("UpdateTime")
  622. || !tempLine.compare("[LatestTime]") || string::npos != tempLine.find("LatestTime")
  623. || !tempLine.compare("[FingerInfo]") || !tempLine.compare("[FaceInfo]")
  624. || string::npos != tempLine.find("FaceInfo1") || string::npos != tempLine.find("FaceInfo2"))
  625. {
  626. continue;
  627. }
  628. string::size_type pos = tempLine.find("=");
  629. if (pos != 16)
  630. continue;
  631. string keys = tempLine.substr(0, pos);
  632. string values = tempLine.substr(pos + 1);
  633. Json::Reader reader;
  634. Json::Value root;
  635. if (reader.parse((const char*)values.c_str(), root))
  636. {
  637. customerNum++;
  638. FeatureData* fd = new FeatureData();
  639. fd->FingerIDArray.Init(FINGER_NUM);
  640. fd->FingerIDLenArray.Init(FINGER_NUM);
  641. char index[20];
  642. for (int i = 0; i < FINGER_NUM; ++i)
  643. {
  644. memset(index, 0, sizeof(index));
  645. #ifdef RVC_OS_WIN
  646. _snprintf_s(index, 10, "FingerID%d", i + 1);
  647. #else
  648. snprintf(index, 10, "FingerID%d", i + 1);
  649. #endif // RVC_OS_WIN
  650. fd->FingerIDArray[i] = root.isMember(index) ? CSimpleStringA(root[index].asCString()) : "";
  651. fd->FingerIDLenArray[i] = root.isMember(index) ? fd->FingerIDArray[i].GetLength() : 0;
  652. }
  653. m_featureData[CSimpleStringA(keys.c_str())] = fd;
  654. }
  655. else {
  656. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("Error: parse jsonFingerInfo failed.");
  657. LogWarn(Severity_Middle, Error_Unexpect
  658. , LOG_ERR_CUSTMNGRAUTH_AUTHORIZATION_READFEAT_FAILED
  659. , "Read fingerprint feature json failed.");
  660. return false;
  661. }
  662. }
  663. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Total CustomerNum:%d in local file.", customerNum);
  664. endReadFile = RVCGetTickCount();
  665. ULLINT duration = endReadFile - startReadFile;
  666. LogWarn(Severity_Middle, Error_Debug
  667. , LOG_ERR_CUSTMNGRAUTH_READ_INTO_MEMORY_TIME
  668. , GenerateAlarmJson("CustMngrAuth", duration).GetData());
  669. bHasData = true;
  670. return true;
  671. }
  672. CSimpleString CCustMngrAuthFSM::GenerateAlarmJson(CSimpleString entityName, int cost)
  673. {
  674. return CSimpleString::Format("[{\"name\":\"%s\",\"cost\":%d}]"
  675. , entityName.GetData(), cost);
  676. }
  677. string CCustMngrAuthFSM::ClearStringSpaceHeadTail(string& line)
  678. {
  679. if (line.empty())
  680. return line;
  681. line.erase(0, line.find_first_not_of(" "));
  682. line.erase(line.find_last_not_of(" ") + 1);
  683. return line;
  684. }
  685. /// <summary>
  686. /// init runinfo pointer and some time parameters
  687. /// </summary>
  688. /// <param name="runInfoParam">out param</param>
  689. /// <param name="spConfig">out param</param>
  690. /// <returns></returns>
  691. ErrorCodeEnum CCustMngrAuthFSM::InitBeforeQueryData(RunInfoParams& runInfoParam, CSmartPointer<IConfigInfo>& spConfig)
  692. {
  693. ErrorCodeEnum errCode = Error_Succeed;
  694. EnterCriticalSection(&m_cs);//临时锁一下运行时,防止在写入
  695. errCode = LoadRunConfig(spConfig);
  696. if (Error_Succeed != errCode)
  697. {
  698. LeaveCriticalSection(&m_cs);
  699. return errCode;
  700. }
  701. InitTimeParams(runInfoParam, spConfig);
  702. LeaveCriticalSection(&m_cs);
  703. return errCode;
  704. }
  705. ErrorCodeEnum CCustMngrAuthFSM::LoadRunConfig(CSmartPointer<IConfigInfo>& spConfig)
  706. {
  707. ErrorCodeEnum errCode = Error_Succeed;
  708. errCode = GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfig);
  709. if (errCode != Error_Succeed)
  710. {
  711. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Open runcfg file failed before query data.");
  712. LogError(Severity_High, Error_Unexpect
  713. , LOG_ERR_CUSTMNGRAUTH_OPEN_RUNINFO_FAILED_UPDATE
  714. , "open runcfg failed before query data.");
  715. }
  716. return errCode;
  717. }
  718. void CCustMngrAuthFSM::InitTimeParams(RunInfoParams& runInfoParam, CSmartPointer<IConfigInfo>& spConfig)
  719. {
  720. ErrorCodeEnum errCode = Error_Succeed;
  721. runInfoParam.LatestTime = "";
  722. runInfoParam.UpdateTime = "";
  723. CSimpleStringA latestTime(""), updateTime("");
  724. spConfig->ReadConfigValue("LatestTime", "LatestTime", latestTime);
  725. spConfig->ReadConfigValue("UpdateTime", "UpdateTime", updateTime);
  726. //query current time
  727. CSimpleStringA newTime = GetCurrentDate();
  728. runInfoParam.UpdateTime = newTime;
  729. runInfoParam.LatestTime = latestTime;
  730. //当前日期大于文件中日期时,需要做全量更新
  731. if (updateTime.GetLength() <= 0
  732. || (updateTime.GetLength() > 0 && CompareTime(newTime, updateTime) > 0)
  733. || latestTime.GetLength() <= 0)
  734. {
  735. runInfoParam.IsFirstTimeQueryData = true;
  736. }
  737. }
  738. CSimpleStringA CCustMngrAuthFSM::GetCurrentDate()
  739. {
  740. time_t curTime = time(NULL);
  741. tm* p = localtime(&curTime);
  742. char cTime[100] = { 0 };
  743. #ifdef RVC_OS_WIN
  744. _snprintf_s(cTime, 100, "%d%02d%02d", p->tm_year + 1900, p->tm_mon + 1, p->tm_mday);
  745. #else
  746. snprintf(cTime, 100, "%d%02d%02d", p->tm_year + 1900, p->tm_mon + 1, p->tm_mday);
  747. #endif // RVC_OS_WIN
  748. CSimpleStringA curDate(cTime);
  749. return curDate;
  750. }
  751. ///**TODO(Gifur@9/23/2022): param */
  752. int CCustMngrAuthFSM::CompareTime(CSimpleStringA time1, CSimpleStringA time2)
  753. {
  754. if (time1.GetLength() > 0 && time2.GetLength() > 0)
  755. {
  756. int year1 = atoi((const char*)time1.SubString(0, 4));
  757. int month1 = atoi((const char*)time1.SubString(4, 2));
  758. int day1 = atoi((const char*)time1.SubString(6, 2));
  759. int year2 = atoi((const char*)time2.SubString(0, 4));
  760. int month2 = atoi((const char*)time2.SubString(4, 2));
  761. int day2 = atoi((const char*)time2.SubString(6, 2));
  762. int temp1 = year1 * 10000 + month1 * 100 + day1;
  763. int temp2 = year2 * 10000 + month2 * 100 + day2;
  764. if (temp1 > temp2)
  765. return 1;
  766. else if (temp1 < temp2)
  767. return -1;
  768. else
  769. return 0;
  770. }
  771. return 0;
  772. }
  773. /// <summary>
  774. /// receive fingerprint feature from branch server
  775. /// </summary>
  776. /// <param name="dataArray">out param</param>
  777. /// <param name="runInfoParam">in param</param>
  778. /// <returns></returns>
  779. ErrorCodeEnum CCustMngrAuthFSM::ReceiveDataFromServer(CAutoArray<CSimpleStringA>& dataArray, RunInfoParams runInfoParam)
  780. {
  781. LOG_FUNCTION();
  782. char currAgent[16];
  783. char branchID[16];
  784. memset(currAgent, 0, sizeof(currAgent));
  785. memset(branchID, 0, sizeof(currAgent));
  786. bool bResumeTrans = true;
  787. ErrorCodeEnum errCode = Error_Unexpect;
  788. while (bResumeTrans)
  789. {
  790. if (runInfoParam.IsFirstTimeQueryData)
  791. m_pConnection->SendFeatReq(currAgent, branchID);
  792. else
  793. m_pConnection->SendFeatReq(currAgent, branchID, runInfoParam.LatestTime.GetData());
  794. ResetEvent(m_pConnection->m_hPkgAnswer);
  795. DWORD dw = WaitForSingleObject(m_pConnection->m_hPkgAnswer, 20000); //10->20 20200430@liuwentao
  796. switch (dw)
  797. {
  798. case WAIT_FAILED:
  799. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("WAIT_FAILED!");
  800. break;
  801. case WAIT_TIMEOUT:
  802. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("WAIT_TIMEOUT");
  803. case WAIT_OBJECT_0:
  804. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("WAIT_OBJECT_0");
  805. break;
  806. }
  807. ResetEvent(m_pConnection->m_hPkgAnswer);
  808. if (m_pConnection->m_reply == NULL)
  809. {
  810. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("m_reply still null after m_hPkgAnswer handled");
  811. break;
  812. }
  813. if (m_pConnection->m_GetErrMsg)
  814. {
  815. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("get error message, check dbg log");
  816. break;
  817. }
  818. if (m_pConnection->m_reply->ResultCode == 2)
  819. {
  820. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("remote server uninitialized yet, unable to excute query");
  821. break;
  822. }
  823. else {
  824. if (m_pConnection->m_reply->ResultCode == 0)
  825. {
  826. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("All package downloaded from branch server.");
  827. bResumeTrans = false;
  828. errCode = Error_Succeed;
  829. }
  830. memcpy(currAgent, m_pConnection->m_reply->CurrentAgent, 16);
  831. memcpy(branchID, m_pConnection->m_reply->BranchID, 16);
  832. CSimpleStringA jbuf(m_pConnection->m_reply->Data, m_pConnection->m_jsonLen);
  833. dataArray.Append(&jbuf, 0, 1);
  834. }
  835. }
  836. if (bResumeTrans)
  837. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("some errors happened, check the related log");
  838. if (dataArray.GetCount() <= 0)
  839. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("query no data from branchServer.");
  840. return errCode;
  841. }
  842. /// <summary>
  843. /// backup runinfo file, in case some error occurs when process data
  844. /// </summary>
  845. /// <param name="runInfoParam">input/output param</param>
  846. /// <param name="spConfig">input param</param>
  847. /// <returns></returns>
  848. ErrorCodeEnum CCustMngrAuthFSM::BackupBeforeWriteData(RunInfoParams& runInfoParam, CSmartPointer<IConfigInfo>& spConfig)
  849. {
  850. LOG_FUNCTION();
  851. ErrorCodeEnum errCode = Error_Succeed;
  852. runInfoParam.SrcFile = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR RUNINFOFILE
  853. , m_RunInfoPath.GetData());
  854. runInfoParam.BackupFile = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR RUNINFOFILE_BAK
  855. , m_RunInfoPath.GetData());
  856. EnterCriticalSection(&m_cs);
  857. if (!BackupFile(runInfoParam.SrcFile, runInfoParam.BackupFile))
  858. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Backup runinfo file failed.");
  859. if (runInfoParam.IsFirstTimeQueryData)
  860. {
  861. //首次更新,需清除数据,全量写入,并更新时间
  862. ofstream fileOut((const char*)runInfoParam.SrcFile, ios::trunc);
  863. fileOut.close();
  864. }
  865. errCode = GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfig);
  866. if (errCode != Error_Succeed)
  867. {
  868. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("error: open runcfg failed with errCode(%s)", SpStrError(errCode));
  869. BackupFile(runInfoParam.BackupFile, runInfoParam.SrcFile);// if backup fail, recover
  870. LeaveCriticalSection(&m_cs);
  871. }
  872. spConfig->WriteConfigValue("UpdateTime", "UpdateTime", runInfoParam.UpdateTime.GetData());
  873. if (runInfoParam.IsFirstTimeQueryData)
  874. {
  875. spConfig->WriteConfigValue("LatestTime", "LatestTime", "");
  876. }
  877. return errCode;
  878. }
  879. bool CCustMngrAuthFSM::BackupFile(CSimpleStringA srcFile, CSimpleStringA dstFile)
  880. {
  881. if (!ExistsFile(srcFile.GetData()))
  882. return true;
  883. int result = fileutil_copy_file(dstFile.GetData(), srcFile.GetData());
  884. return (result == 0) ? true : false;
  885. }
  886. /// <summary>
  887. /// write data into local file and memory
  888. /// </summary>
  889. /// <param name="runInfoParam">input/output param</param>
  890. /// <param name="dataArray">input param</param>
  891. /// <param name="spConfig">input param</param>
  892. /// <returns></returns>
  893. ErrorCodeEnum CCustMngrAuthFSM::WriteData(RunInfoParams& runInfoParam
  894. , CAutoArray<CSimpleStringA> dataArray
  895. , CSmartPointer<IConfigInfo>& spConfig)
  896. {
  897. ErrorCodeEnum errCode = Error_Unexpect;
  898. TempFeatureData tmpFeatureData;
  899. tmpFeatureData.MaxUpdateTime = runInfoParam.LatestTime;
  900. bool bExitLoop = false;
  901. for (int transTime = 0; transTime < dataArray.GetCount(); ++transTime)
  902. {
  903. Json::Value root;
  904. Json::Reader reader;
  905. //only 3-4 data, cause the limit of transfer amount
  906. CSimpleStringA transBuffer = dataArray[transTime];
  907. if (reader.parse(transBuffer.GetData(), root))
  908. {
  909. for (int i = 0; i < (int)root.size(); ++i)
  910. {
  911. JsonParams jsonParam;
  912. jsonParam.Root = root;
  913. jsonParam.Index = i;
  914. errCode = ProcessFeatureData(jsonParam, tmpFeatureData, runInfoParam, spConfig);
  915. if (Error_Succeed != errCode)
  916. {
  917. bExitLoop = true;
  918. break;
  919. }
  920. }
  921. if (bExitLoop)
  922. break;
  923. }else {
  924. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("fail to parse transArray[%d]!", transTime);
  925. LeaveCriticalSection(&m_cs);
  926. bExitLoop = true;
  927. break;
  928. }
  929. }
  930. if (bExitLoop)
  931. return Error_Unexpect;
  932. if (tmpFeatureData.MaxUpdateTime.GetLength() > 0)
  933. {
  934. spConfig->WriteConfigValue("LatestTime", "LatestTime"
  935. , tmpFeatureData.MaxUpdateTime.GetData());
  936. }
  937. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("updateNum=%d", tmpFeatureData.tmpFeatureMap.size());
  938. UpdateDataIntoMemory(tmpFeatureData.tmpFeatureMap, runInfoParam.IsFirstTimeQueryData);
  939. LeaveCriticalSection(&m_cs);
  940. return Error_Succeed;
  941. }
  942. /// <summary>
  943. /// process feature data
  944. /// </summary>
  945. /// <param name="jsonParam"></param>
  946. /// <param name="tmpFeatureData"></param>
  947. /// <param name="runinfoParam"></param>
  948. /// <param name="spConfig"></param>
  949. /// <returns></returns>
  950. ErrorCodeEnum CCustMngrAuthFSM::ProcessFeatureData(JsonParams& jsonParam, TempFeatureData& tmpFeatureData
  951. , RunInfoParams runinfoParam, CSmartPointer<IConfigInfo>& spConfig)
  952. {
  953. ErrorCodeEnum errCode = Error_Succeed;
  954. FeatureData* fd = new FeatureData();
  955. fd->FingerIDArray.Init(FINGER_NUM);
  956. fd->FingerIDLenArray.Init(FINGER_NUM);
  957. char FingerID[20];//runinfo file is named "FingerID"
  958. char fingerId[20];//branchserver is named "fingerId" , history bug
  959. for (int fingerIndex = 0; fingerIndex < FINGER_NUM; ++fingerIndex)
  960. {
  961. ZeroMemory(FingerID, sizeof(FingerID));
  962. ZeroMemory(fingerId, sizeof(fingerId));
  963. #ifdef RVC_OS_WIN
  964. _snprintf_s(FingerID, 10, "FingerID%d", fingerIndex + 1);
  965. _snprintf_s(fingerId, 10, "fingerId%d", fingerIndex + 1);
  966. #else
  967. snprintf(FingerID, 10, "FingerID%d", fingerIndex + 1);
  968. snprintf(fingerId, 10, "fingerId%d", fingerIndex + 1);
  969. #endif // RVC_OS_WIN
  970. jsonParam.FingerInfo[FingerID] = jsonParam.Root[jsonParam.Index][fingerId].asCString();
  971. fd->FingerIDArray[fingerIndex] = CSimpleStringA(jsonParam.Root[jsonParam.Index][fingerId].asCString());
  972. fd->FingerIDLenArray[fingerIndex] = fd->FingerIDArray[fingerIndex].GetLength();
  973. }
  974. CSimpleStringA customerID = CSimpleStringA(jsonParam.Root[jsonParam.Index]["customerID"].asCString());
  975. if (tmpFeatureData.tmpFeatureMap.find(customerID) == tmpFeatureData.tmpFeatureMap.end())
  976. {
  977. // if not exist , insert directly
  978. tmpFeatureData.tmpFeatureMap[customerID] = fd;
  979. }else {
  980. auto tempFD = tmpFeatureData.tmpFeatureMap[customerID];
  981. tmpFeatureData.tmpFeatureMap[customerID] = fd;
  982. if (tempFD)
  983. {
  984. delete tempFD;
  985. tempFD = NULL;
  986. }
  987. }
  988. CSimpleStringA tempMaxUpdateTime = CSimpleStringA(jsonParam.Root[jsonParam.Index]["updateTime"].asCString());
  989. tmpFeatureData.MaxUpdateTime = GetMaxTime(tmpFeatureData.MaxUpdateTime, tempMaxUpdateTime);
  990. int fingerDataState = jsonParam.Root[jsonParam.Index]["state"].asCString()[0] - '0';
  991. if (fingerDataState == 0)
  992. {
  993. Json::FastWriter writer;
  994. CSimpleStringA jsonFingerStr(writer.write(jsonParam.FingerInfo).c_str());
  995. int jlen = jsonFingerStr.GetLength() - 1;
  996. char* jstr = new char[jlen + 1];
  997. memcpy(jstr, jsonFingerStr.GetData(), jlen);
  998. jstr[jlen] = '\0'; //in case no \n in the end
  999. errCode = spConfig->WriteConfigValue(m_FingerSection.GetData(), customerID.GetData(), jstr);
  1000. delete[] jstr;
  1001. if (errCode != Error_Succeed)
  1002. {
  1003. BackupFile(runinfoParam.BackupFile, runinfoParam.SrcFile);
  1004. LeaveCriticalSection(&m_cs);
  1005. errCode = Error_Unexpect;
  1006. }
  1007. }else if (fingerDataState == 2)
  1008. {
  1009. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("state(2): customer %s is currently unavailable.", customerID.GetData());
  1010. spConfig->WriteConfigValue((const char*)m_FingerSection, customerID.GetData(), "");
  1011. }else {
  1012. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("unexpected customer(%s)'s state is either 0 or 2", customerID.GetData());
  1013. }
  1014. return errCode;
  1015. }
  1016. CSimpleStringA CCustMngrAuthFSM::GetMaxTime(CSimpleStringA maxTime, CSimpleStringA tempTime)
  1017. {
  1018. if (tempTime.GetLength() <= 0)
  1019. return maxTime;
  1020. if (maxTime.GetLength() <= 0)
  1021. {
  1022. maxTime = tempTime;
  1023. }
  1024. else {
  1025. int compareResult = CompareUpdateTime((const char*)maxTime, (const char*)tempTime);
  1026. if (compareResult == 0)
  1027. {
  1028. maxTime = tempTime;
  1029. }
  1030. }
  1031. return maxTime;
  1032. }
  1033. int CCustMngrAuthFSM::CompareUpdateTime(const char* time1, const char* time2)
  1034. {
  1035. int year1, month1, day1, hour1, minute1, second1;
  1036. int year2, month2, day2, hour2, minute2, second2;
  1037. sscanf(time1, "%d-%d-%d %d:%d:%d", &year1, &month1, &day1, &hour1, &minute1, &second1);
  1038. sscanf(time2, "%d-%d-%d %d:%d:%d", &year2, &month2, &day2, &hour2, &minute2, &second2);
  1039. int tm1 = year1 * 10000 + month1 * 100 + day1;
  1040. int tm2 = year2 * 10000 + month2 * 100 + day2;
  1041. if (tm1 != tm2)
  1042. return (tm1 > tm2) ? 1 : 0;
  1043. tm1 = hour1 * 3600 + minute1 * 60 + second1;
  1044. tm2 = hour2 * 3600 + minute2 * 60 + second2;
  1045. if (tm1 != tm2)
  1046. return (tm1 > tm2) ? 1 : 0;
  1047. return 2;
  1048. }
  1049. /// <summary>
  1050. /// write data into memory when update fingerprint feature
  1051. /// </summary>
  1052. /// <param name="tempFeature"></param>
  1053. /// <param name="bIsFirstTimeQueryData"></param>
  1054. void CCustMngrAuthFSM::UpdateDataIntoMemory(map<CSimpleStringA, FeatureData*> tempFeature, bool bIsFirstTimeQueryData)
  1055. {
  1056. if (!bIsFirstTimeQueryData)
  1057. {
  1058. for (auto it = tempFeature.begin(); it != tempFeature.end(); ++it) {
  1059. if (m_featureData.find(it->first) == m_featureData.end()) {//if not exist,insert immediately
  1060. m_featureData[it->first] = it->second;
  1061. }
  1062. else {//if exist already, release memory first, then insert
  1063. auto tempFD = m_featureData[it->first];
  1064. m_featureData[it->first] = it->second;
  1065. if (tempFD != NULL) { delete tempFD; tempFD = NULL; }
  1066. }
  1067. }
  1068. }else {
  1069. for (auto iter = m_featureData.begin(); iter != m_featureData.end();) {
  1070. auto fd = iter->second;
  1071. if (fd) { delete fd; fd = NULL; m_featureData.erase(iter++); }
  1072. }
  1073. m_featureData.insert(tempFeature.begin(), tempFeature.end());
  1074. }
  1075. }
  1076. #pragma endregion
  1077. #pragma region Registe process
  1078. ErrorCodeEnum CCustMngrAuthFSM::CollectFingerPrint(SpReqAnsContext<CustMngrAuthService_CollectFingerPrint_Req, CustMngrAuthService_CollectFingerPrint_Ans>::Pointer ctx, DWORD& dwUserErrCode)
  1079. {
  1080. LOG_FUNCTION();
  1081. LogWarn(Severity_Low, Error_Unexpect, LOG_ERR_CUSTMNGRAUTH_INVOKE_GETIMAGE, "web invoke CollectFingerPrint");
  1082. ErrorCodeEnum errCode = ConnectFingerPrintEntity();
  1083. if (errCode != Error_Succeed)
  1084. return errCode;
  1085. errCode = CollectProcess(ctx, dwUserErrCode);
  1086. if (errCode == Error_Succeed)
  1087. {
  1088. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Register FingerPrint successfully.");
  1089. return Error_Succeed;
  1090. }
  1091. else {
  1092. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Register FingerPrint failed! %s", SpStrError(errCode));
  1093. return errCode;
  1094. }
  1095. }
  1096. ErrorCodeEnum CCustMngrAuthFSM::CollectFingerPrintInfo(SpReqAnsContext<CustMngrAuthService_CollectFingerPrintInfo_Req, CustMngrAuthService_CollectFingerPrintInfo_Ans>::Pointer ctx, DWORD& dwUserErrCode)
  1097. {
  1098. LOG_FUNCTION();
  1099. if (ctx == NULL || !(ctx->Req.FingerId > 0 && ctx->Req.FingerId < 10))
  1100. {
  1101. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("CollectFingerPrintInfo, Invalid inParam, FingerId:%d", ctx->Req.FingerId);
  1102. return Error_Param;
  1103. }
  1104. CSimpleStringA tmpParam = CSimpleStringA::Format("web invoke CollectFingerPrintInfo with inParam FingerId:%d", ctx->Req.FingerId);
  1105. LogWarn(Severity_Low, Error_Unexpect, LOG_ERR_CUSTMNGRAUTH_INVOKE_CollectFingerPrintInfo, "web invoke CollectFingerPrintInfo");
  1106. ErrorCodeEnum errCode = ConnectFingerPrintEntity();
  1107. if (errCode != Error_Succeed)
  1108. return errCode;
  1109. CSimpleStringA depPath(true);
  1110. m_pEntity->GetFunction()->GetPath("Dep", depPath);
  1111. FingerPrintService_GetFingerPrint_Req GetFingerReq;
  1112. FingerPrintService_GetFingerPrint_Ans GetFingerAns;
  1113. GetFingerReq.times = ctx->Req.FingerId;
  1114. if (m_pFingerPrint == NULL || m_pFingerPrint->QuerySessionClosed())
  1115. {
  1116. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("m_pFingerPrint is NULL or connection closed.");
  1117. return Error_Hardware;
  1118. }
  1119. errCode = m_pFingerPrint->GetFingerPrint(GetFingerReq, GetFingerAns, 30000, dwUserErrCode);
  1120. if (errCode == Error_NotInit)
  1121. {
  1122. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("FingerPrintEntity not ready or failed.");
  1123. return Error_DevNotAvailable;
  1124. }
  1125. if (errCode == Error_Succeed)
  1126. {
  1127. switch (GetFingerAns.reserved1[0])
  1128. {
  1129. case 1:
  1130. errCode = Error_Succeed;
  1131. break;
  1132. case 2:
  1133. errCode = Error_TimeOut;
  1134. break;
  1135. case 3:
  1136. errCode = Error_Cancel;
  1137. break;
  1138. default:
  1139. errCode = Error_Unexpect;
  1140. break;
  1141. }
  1142. if (errCode == Error_Succeed)
  1143. {
  1144. ctx->Ans.feature = GetFingerAns.feature;
  1145. CBlob data;
  1146. CSimpleStringA imgFullPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", depPath.GetData(), GetFingerAns.imageName.GetData());
  1147. ErrorCodeEnum imgErrCode = GetImgBlob(data, imgFullPath);
  1148. if (imgErrCode != Error_Succeed)
  1149. {
  1150. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Failed to load image(%s)", imgFullPath.GetData());
  1151. return Error_Unexpect;
  1152. }
  1153. ctx->Ans.fingerImg = data;
  1154. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("CollectFingerPrintInfo return success.");
  1155. }
  1156. }
  1157. else
  1158. errCode = Error_Unexpect;
  1159. return errCode;
  1160. }
  1161. ErrorCodeEnum CCustMngrAuthFSM::GenerateTemplate(SpReqAnsContext<CustMngrAuthService_GenerateTemplate_Req, CustMngrAuthService_GenerateTemplate_Ans>::Pointer ctx, DWORD& dwUserErrCode)
  1162. {
  1163. LOG_FUNCTION();
  1164. LogWarn(Severity_Low, Error_Unexpect, LOG_ERR_CUSTMNGRAUTH_INVOKE_GenerateTemplate, "web invoke GenerateTemplate");
  1165. int fingerNum = ctx->Req.FingerIDList.GetCount();
  1166. if (fingerNum != IMAGE_NUM)
  1167. {
  1168. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("GenerateTemplate, Invalid inParam, fingerNum:%d", fingerNum);
  1169. return Error_Param;
  1170. }
  1171. ErrorCodeEnum errCode;
  1172. m_pFingerPrint = new FingerPrintService_ClientBase(m_pEntity);
  1173. errCode = m_pFingerPrint->Connect();
  1174. if (errCode != Error_Succeed)
  1175. {
  1176. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("ERROR: connect to fingerprint entity failed while GenerateTemplate: %s", SpStrError(errCode));
  1177. m_pFingerPrint->SafeDelete();
  1178. m_pFingerPrint = NULL;
  1179. return Error_DevNotAvailable; //TODO::give one other errCode
  1180. }
  1181. CSimpleStringA depPath(true);
  1182. m_pEntity->GetFunction()->GetPath("Dep", depPath);
  1183. FingerPrintService_GenerateTemplate_Req generateReq;
  1184. FingerPrintService_GenerateTemplate_Ans generateAns;
  1185. generateReq.FingerIDList.Init(IMAGE_NUM);
  1186. for (int i = 0; i < generateReq.FingerIDList.GetCount(); ++i)
  1187. {
  1188. generateReq.FingerIDList[i] = ctx->Req.FingerIDList[i];
  1189. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("fingerIDList = %d", generateReq.FingerIDList[i]);
  1190. }
  1191. if (m_pFingerPrint == NULL || m_pFingerPrint->QuerySessionClosed())
  1192. {
  1193. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("m_pFingerPrint is NULL or connection closed.");
  1194. return Error_DevNotAvailable;
  1195. }
  1196. errCode = m_pFingerPrint->GenerateTemplate(generateReq, generateAns, 20000, dwUserErrCode);
  1197. if (errCode == Error_NotInit)
  1198. {
  1199. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER)("FingerPrintEntity not ready or failed.");
  1200. return Error_DevNotAvailable;
  1201. }
  1202. if (errCode == Error_Succeed)
  1203. {
  1204. ctx->Ans.templateFeature = generateAns.templateFeature;
  1205. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("GenerateTemplate return success.");
  1206. }
  1207. else {
  1208. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER)("Invoke GenerateTemplate returned %d", errCode);
  1209. errCode = Error_Unexpect;
  1210. }
  1211. return errCode;
  1212. }
  1213. ErrorCodeEnum CCustMngrAuthFSM::CollectFingerPrintEx(SpReqAnsContext<CustMngrAuthService_CollectFingerPrintEx_Req, CustMngrAuthService_CollectFingerPrintEx_Ans>::Pointer ctx, DWORD& dwUserErrCode)
  1214. {
  1215. LOG_FUNCTION();
  1216. LogWarn(Severity_Low, Error_Unexpect, LOG_ERR_CUSTMNGRAUTH_INVOKE_GETIMAGEEx, "web invoke CollectFingerPrintEx");
  1217. ErrorCodeEnum errCode = ConnectFingerPrintEntity();
  1218. if (errCode != Error_Succeed)
  1219. return errCode;
  1220. errCode = CollectProcessEx(ctx, dwUserErrCode);
  1221. if (errCode == Error_Succeed)
  1222. {
  1223. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Register FingerPrint successfully.");
  1224. return Error_Succeed;
  1225. }
  1226. else {
  1227. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Register FingerPrint failed: %s !", SpStrError(errCode));
  1228. return errCode;
  1229. }
  1230. }
  1231. ErrorCodeEnum CCustMngrAuthFSM::CollectProcess(SpReqAnsContext<CustMngrAuthService_CollectFingerPrint_Req, CustMngrAuthService_CollectFingerPrint_Ans>::Pointer& ctx, DWORD& dwUserErrCode)
  1232. {
  1233. ErrorCodeEnum errCode = Error_Succeed;
  1234. vector<CSimpleStringA> imgPaths;
  1235. CSimpleStringA depPath(true);
  1236. m_pEntity->GetFunction()->GetPath("Dep", depPath);
  1237. ctx->Ans.FingerImgs.Init(COLLECT_TIMES);
  1238. for (int i = 0; i < COLLECT_TIMES; ++i)
  1239. {
  1240. if (i) SLEEP(2000);
  1241. if (m_pFingerPrint == NULL || m_pFingerPrint->QuerySessionClosed())
  1242. {
  1243. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("m_pFingerPrint is NULL or connection closed.");
  1244. return Error_NoTarget;
  1245. }
  1246. BroadcastPressFinger(i + 1, true);//press finger
  1247. FingerPrintService_GetImageAndFeature_Req collecetReq;
  1248. FingerPrintService_GetImageAndFeature_Ans collecetAns;
  1249. collecetReq.times = i + 1;//the num from 1 start , 1/2/3
  1250. errCode = m_pFingerPrint->GetImageAndFeature(collecetReq, collecetAns, 16000, dwUserErrCode);//fingerprint entity loop duration is 15s
  1251. if (errCode == Error_Succeed)
  1252. {
  1253. BroadcastPressFinger(i + 1, false);//lift finger
  1254. CBlob data;
  1255. CSimpleStringA imgFullPath(true);
  1256. imgFullPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s"
  1257. , depPath.GetData(), collecetAns.imageName.GetData());
  1258. errCode = GetImgBlob(data, imgFullPath);
  1259. imgPaths.push_back(imgFullPath);
  1260. if (errCode != Error_Succeed)
  1261. {
  1262. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_USER)("Failed to load finger image, %s", imgFullPath.GetData());
  1263. errCode = Error_Unexpect;
  1264. break;
  1265. }
  1266. switch (i)
  1267. {
  1268. case 0:
  1269. ctx->Ans.FingerImg1 = data;
  1270. break;
  1271. case 1:
  1272. ctx->Ans.FingerImg2 = data;
  1273. break;
  1274. case 2:
  1275. ctx->Ans.FingerImg3 = data;
  1276. break;
  1277. }
  1278. ctx->Ans.FingerImgs[i] = data;
  1279. if (i == (COLLECT_TIMES - 1)) {
  1280. ctx->Ans.feature = collecetAns.feature;
  1281. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("feature = %s", ctx->Ans.feature.GetData());
  1282. }
  1283. }
  1284. else {
  1285. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER)("invoke GetImageAndFeature failed in %dth time, errCode(%s)", i + 1, SpStrError(errCode));
  1286. break;
  1287. }
  1288. }
  1289. for (size_t i = 0; i < imgPaths.size(); ++i)
  1290. {
  1291. if (ExistsFileA(imgPaths[i]))
  1292. RemoveFileA(imgPaths[i]);
  1293. }
  1294. return errCode;
  1295. }
  1296. ErrorCodeEnum CCustMngrAuthFSM::CollectProcessEx(SpReqAnsContext<CustMngrAuthService_CollectFingerPrintEx_Req, CustMngrAuthService_CollectFingerPrintEx_Ans>::Pointer& ctx, DWORD& dwUserErrCode)
  1297. {
  1298. ErrorCodeEnum errCode = Error_Succeed;
  1299. CSimpleStringA depPath(true);
  1300. m_pEntity->GetFunction()->GetPath("Dep", depPath);
  1301. ctx->Ans.FingerImgs.Init(COLLECT_TIMES);
  1302. FingerPrintService_GetImageAndFeatureEx_Req collectExReq;
  1303. FingerPrintService_GetImageAndFeatureEx_Ans collectExAns;
  1304. collectExReq.times = ctx->Req.FingerId;
  1305. if (m_pFingerPrint == NULL || m_pFingerPrint->QuerySessionClosed())
  1306. {
  1307. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("m_pFingerPrint is NULL or connection closed.");
  1308. return Error_NoTarget;
  1309. }
  1310. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Start %dth scan fingerprint", collectExReq.times);
  1311. errCode = m_pFingerPrint->GetImageAndFeatureEx(collectExReq, collectExAns, 30000, dwUserErrCode);//fingerprint entity loop duration is 15s
  1312. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("reserverd1[0] = %d", collectExAns.reserved1[0]);
  1313. if (errCode == Error_NotExist) {
  1314. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("cannot find finger img in dep!");
  1315. return Error_NotExist;
  1316. }
  1317. if (errCode == Error_Succeed && collectExAns.reserved1[0] == 3) {
  1318. return Error_Cancel;
  1319. }
  1320. if (errCode == Error_Succeed && collectExAns.reserved1[0] == 2) {
  1321. return Error_TimeOut;
  1322. }
  1323. CBlob data;
  1324. CSimpleStringA imgFullPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s"
  1325. , depPath.GetData(), collectExAns.imageName.GetData());
  1326. ErrorCodeEnum imgErrCode = GetImgBlob(data, imgFullPath);
  1327. if (imgErrCode != Error_Succeed)
  1328. {
  1329. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER)("Failed to load image(%s)", imgFullPath.GetData());
  1330. return Error_NotExist;
  1331. }
  1332. CSimpleStringA imageName(true);
  1333. CSimpleStringA imgPrefix = "finger";
  1334. CSimpleStringA imgSuffix = ".bmp";
  1335. switch (ctx->Req.FingerId)
  1336. {
  1337. case 1:
  1338. ctx->Ans.FingerImg1 = data;
  1339. break;
  1340. case 2:
  1341. ctx->Ans.FingerImg2 = data;
  1342. break;
  1343. case 3:
  1344. ctx->Ans.FingerImg3 = data;
  1345. for (size_t i = 0; i < 10; ++i)
  1346. {
  1347. imageName = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s%d%s", depPath.GetData(), imgPrefix.GetData(), i, imgSuffix.GetData());
  1348. if (ExistsFileA(imageName))
  1349. RemoveFileA(imageName);
  1350. }
  1351. imageName = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s%s", depPath.GetData(), imgPrefix.GetData(), imgSuffix.GetData());
  1352. if (ExistsFileA(imageName))
  1353. RemoveFileA(imageName);
  1354. break;
  1355. default:
  1356. break;
  1357. }
  1358. if (errCode == Error_Succeed && collectExAns.reserved1[0] == 1) {
  1359. if (ctx->Req.FingerId == COLLECT_TIMES)
  1360. ctx->Ans.feature = collectExAns.feature;
  1361. }
  1362. else if(errCode == Error_NotInit){
  1363. errCode = Error_DevNotAvailable;
  1364. }else {
  1365. errCode = Error_Unexpect;
  1366. }
  1367. if (errCode == Error_Succeed) {
  1368. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Register FingerPrint successfully.");
  1369. }
  1370. else {
  1371. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER)("Register FingerPrint failed! %s", SpStrError(errCode));
  1372. }
  1373. return errCode;
  1374. }
  1375. void CCustMngrAuthFSM::BroadcastPressFinger(int times, bool bPressFinger)
  1376. {
  1377. if (bPressFinger)
  1378. {
  1379. PressFinger pfEvt;//
  1380. pfEvt.FingerNo = 1;//maybe no use,control by @zhuyi
  1381. pfEvt.Times = times;
  1382. SpSendBroadcast(m_pEntity->GetFunction(), SP_MSG_OF(PressFinger)
  1383. , SP_MSG_SIG_OF(PressFinger), pfEvt);
  1384. }
  1385. else {
  1386. LiftFinger lfEvt;
  1387. lfEvt.FingerNo = 1;
  1388. lfEvt.Times = times;
  1389. SpSendBroadcast(m_pEntity->GetFunction(), SP_MSG_OF(LiftFinger)
  1390. , SP_MSG_SIG_OF(LiftFinger), lfEvt);
  1391. }
  1392. }
  1393. ErrorCodeEnum CCustMngrAuthFSM::GetImgBlob(CBlob& data, CSimpleStringA imgPath)
  1394. {
  1395. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("image full path: %s", imgPath.GetData());
  1396. FILE* fp = fopen(imgPath, "rb");
  1397. if (fp)
  1398. {
  1399. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("fopen succeed.");
  1400. fseek(fp, 0, SEEK_END);
  1401. long flen = ftell(fp);
  1402. fseek(fp, 0, SEEK_SET);
  1403. data.Alloc(flen);
  1404. fread(data.m_pData, 1, flen, fp);
  1405. fclose(fp);
  1406. return Error_Succeed;
  1407. }
  1408. else {
  1409. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("fopen %s failed!", imgPath.GetData());
  1410. return Error_IO;
  1411. }
  1412. }
  1413. #pragma endregion
  1414. #pragma region Save fingerprint process
  1415. ErrorCodeEnum CCustMngrAuthFSM::SaveFingerPrint(SpReqAnsContext<CustMngrAuthService_SaveFingerPrint_Req, CustMngrAuthService_SaveFingerPrint_Ans>::Pointer ctx)
  1416. {
  1417. LOG_FUNCTION();
  1418. EnterCriticalSection(&m_cs);
  1419. CSmartPointer<IConfigInfo> spConfig;
  1420. Json::FastWriter writer;
  1421. ErrorCodeEnum errCode;
  1422. errCode = GetEntityBase()->GetFunction()->OpenConfig(Config_Run, spConfig);
  1423. if (errCode != Error_Succeed) {
  1424. LogError(Severity_High, Error_DevLoadFileFailed
  1425. , LOG_ERR_CUSTMNGRAUTH_OPEN_RUNINFO_FAILED_SAVEFINGERPRINT
  1426. , "open runinfo file failed while save data to local.");
  1427. LeaveCriticalSection(&m_cs);
  1428. return Error_Unexpect;
  1429. }
  1430. if (ctx->Req.FPFeatureList == nullptr)
  1431. return Error_Param;
  1432. int fingerIDNum = ctx->Req.FingerIdList.GetCount();
  1433. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("FingerIDNum=%d", fingerIDNum);
  1434. FeatureData* fd = new FeatureData();
  1435. fd->FingerIDArray.Init(fingerIDNum);
  1436. fd->FingerIDLenArray.Init(fingerIDNum);
  1437. char fingerID[20];
  1438. Json::Value fingerInfo;
  1439. for (int i = 0; i < fingerIDNum; ++i)
  1440. {
  1441. memset(fingerID, 0, sizeof(fingerID));
  1442. #ifdef RVC_OS_WIN
  1443. _snprintf_s(fingerID, 10, "FingerID%d", ctx->Req.FingerIdList[i]);
  1444. #else
  1445. snprintf(fingerID, 10, "FingerID%d", ctx->Req.FingerIdList[i]);
  1446. #endif // RVC_OS_WIN
  1447. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("writing %s", fingerID);
  1448. fingerInfo[fingerID] = ctx->Req.FPFeatureList[i].GetData();
  1449. fd->FingerIDArray[i] = ctx->Req.FPFeatureList[i];
  1450. fd->FingerIDLenArray[i] = fd->FingerIDArray[i].GetLength();
  1451. }
  1452. if (m_featureData.find(ctx->Req.CustomerID) == m_featureData.end())
  1453. {
  1454. //not exist, insert directly
  1455. m_featureData[ctx->Req.CustomerID] = fd;
  1456. }
  1457. else {
  1458. auto tempFD = m_featureData[ctx->Req.CustomerID];
  1459. m_featureData[ctx->Req.CustomerID] = fd;
  1460. if (tempFD) { delete tempFD; tempFD = NULL; }
  1461. }
  1462. errCode = spConfig->WriteConfigValue(m_FingerSection.GetData()
  1463. , ctx->Req.CustomerID.GetData()
  1464. , writer.write(fingerInfo).c_str());
  1465. if (errCode != Error_Succeed)
  1466. {
  1467. LogError(Severity_High, Error_DevLoadFileFailed
  1468. , LOG_ERR_CUSTMNGRAUTH_REGISTER_WRITE_RUNINFO_FAILED
  1469. , "write data into runinfo failed when commit.");
  1470. LeaveCriticalSection(&m_cs);
  1471. return Error_Unexpect;
  1472. }
  1473. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("write data into runinfo success when commit.");
  1474. LeaveCriticalSection(&m_cs);
  1475. return errCode;
  1476. }
  1477. #pragma endregion
  1478. #pragma region authorize process
  1479. ErrorCodeEnum CCustMngrAuthFSM::MatchFingerPrint(SpReqAnsContext<CustMngrAuthService_StartAuthorize_Req, CustMngrAuthService_StartAuthorize_Ans>::Pointer ctx, bool& bStopAuthorize)
  1480. {
  1481. LOG_FUNCTION();
  1482. LogWarn(Severity_Low, Error_Unexpect, LOG_ERR_CUSTMNGRAUTH_INVOKE_MATCH, "web invoke MatchFingerPrint");
  1483. ErrorCodeEnum errCode = ConnectFingerPrintEntity();
  1484. if (errCode != Error_Succeed)
  1485. return errCode;
  1486. MatchParams* matchParam = new MatchParams();
  1487. matchParam->sTotalNumOfTemplate = 0;
  1488. errCode = PrepareDataBeforeMatch(matchParam);
  1489. if (errCode != Error_Succeed)
  1490. return errCode;
  1491. errCode = MatchProcess(matchParam, bStopAuthorize);
  1492. return errCode;
  1493. }
  1494. ErrorCodeEnum CCustMngrAuthFSM::ConnectFingerPrintEntity()
  1495. {
  1496. ErrorCodeEnum errCode = Error_Succeed;
  1497. m_pFingerPrint = new FingerPrintService_ClientBase(m_pEntity);
  1498. errCode = m_pFingerPrint->Connect();
  1499. if (errCode != Error_Succeed)
  1500. {
  1501. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("ERROR: connect to fingerprint entity failed! %s", SpStrError(errCode));
  1502. m_pFingerPrint->SafeDelete();
  1503. errCode = Error_NoTarget; //TODO::give one other errCode
  1504. }
  1505. return errCode;
  1506. }
  1507. ErrorCodeEnum CCustMngrAuthFSM::PrepareDataBeforeMatch(MatchParams* matchParam)
  1508. {
  1509. EnterCriticalSection(&m_cs);
  1510. CAutoArray<CSimpleStringA> tempFeatureArray;
  1511. CAutoArray<int> tempFeatureLenArray;
  1512. int totalCustomerID = m_featureData.size();
  1513. int totalTemplate = totalCustomerID * FINGER_NUM;
  1514. tempFeatureArray.Init(totalTemplate);
  1515. tempFeatureLenArray.Init(totalTemplate);
  1516. if (totalCustomerID <= 0)
  1517. {
  1518. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("No FingerPrint data in local file or read local file failed.");
  1519. LeaveCriticalSection(&m_cs);
  1520. if (matchParam != NULL)
  1521. delete matchParam;
  1522. return Error_NoTarget;
  1523. }
  1524. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("begin copy feature to reqParams");
  1525. for (auto it = m_featureData.begin(); it != m_featureData.end(); ++it)
  1526. {
  1527. TemplateInfo ti;
  1528. memset(&ti, 0, sizeof(ti));
  1529. ti.CustomerID = it->first;
  1530. ti.TemplateNum = 0;
  1531. for (int index = 0; index < FINGER_NUM; ++index)
  1532. {
  1533. if (index >= it->second->FingerIDArray.GetCount()) //旧版本可能只有2个
  1534. break;
  1535. if (it->second->FingerIDArray[index].GetLength() <= 0)
  1536. continue;
  1537. tempFeatureArray[matchParam->sTotalNumOfTemplate] = it->second->FingerIDArray[index];
  1538. tempFeatureLenArray[matchParam->sTotalNumOfTemplate] = it->second->FingerIDLenArray[index];
  1539. ti.TemplateNum = ti.TemplateNum + 1;
  1540. ++matchParam->sTotalNumOfTemplate;
  1541. }
  1542. if (ti.TemplateNum > 0)
  1543. matchParam->sFingerCount.push_back(ti);
  1544. }
  1545. LeaveCriticalSection(&m_cs);
  1546. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("num of template not empty:%d", matchParam->sTotalNumOfTemplate);
  1547. matchParam->sMatchReq.templateNum = matchParam->sTotalNumOfTemplate;
  1548. matchParam->sMatchReq.templates.Init(matchParam->sTotalNumOfTemplate);
  1549. matchParam->sMatchReq.templateLen.Init(matchParam->sTotalNumOfTemplate);
  1550. for (int i = 0; i < matchParam->sTotalNumOfTemplate; ++i)
  1551. {
  1552. matchParam->sMatchReq.templates[i] = tempFeatureArray[i];
  1553. matchParam->sMatchReq.templateLen[i] = tempFeatureLenArray[i];
  1554. }
  1555. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("end copy feature to reqParams");
  1556. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("templateNum: %d", matchParam->sMatchReq.templateNum);
  1557. return Error_Succeed;
  1558. }
  1559. ErrorCodeEnum CCustMngrAuthFSM::MatchProcess(MatchParams* matchParam, bool& bStopAuthorize)
  1560. {
  1561. ErrorCodeEnum errCode = Error_Succeed;
  1562. while (true)
  1563. {
  1564. if (m_pFingerPrint == NULL || m_pFingerPrint->QuerySessionClosed())
  1565. {
  1566. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("m_pFingerPrint is NULL or connection closed.");
  1567. return Error_Unexpect;
  1568. }
  1569. //fingerprint entity may not return immediately
  1570. SLEEP(100);
  1571. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("begin next invoke match.");
  1572. errCode = m_pFingerPrint->Match(matchParam->sMatchReq, matchParam->sMatchAns, 20000);
  1573. if (m_bCancelAuthorize || errCode == Error_Cancel)
  1574. {
  1575. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("cancel authorize task.");
  1576. bStopAuthorize = true;
  1577. m_bCancelAuthorize = false;
  1578. if (m_bAuthorizeTimeout)
  1579. {
  1580. m_bAuthorizeTimeout = false;
  1581. return Error_TimeOut;
  1582. }
  1583. return Error_Cancel;
  1584. }
  1585. else if (errCode == Error_Unexpect || errCode == Error_TimeOut)
  1586. {
  1587. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("invoke match error with errCode(%s)", SpStrError(errCode));
  1588. BroadcastGetFinger(2);
  1589. }
  1590. else if (errCode == Error_Succeed)
  1591. {
  1592. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("invoke match success, start analyze result.");
  1593. errCode = AnalyzeMatchResult(matchParam, bStopAuthorize);
  1594. if (errCode == Error_Succeed)
  1595. return Error_Succeed; //continue if other errCode
  1596. }
  1597. else {
  1598. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("invole match error(%s), stop authorize", SpStrError(errCode));
  1599. return Error_Unexpect;
  1600. }
  1601. }
  1602. }
  1603. void CCustMngrAuthFSM::BroadcastGetFinger(int status)
  1604. {
  1605. GetFinger evt;
  1606. evt.Status = status;
  1607. SpSendBroadcast(GetEntityBase()->GetFunction()
  1608. , SP_MSG_OF(GetFinger), SP_MSG_SIG_OF(GetFinger)
  1609. , evt);
  1610. }
  1611. ErrorCodeEnum CCustMngrAuthFSM::AnalyzeMatchResult(MatchParams* matchParam, bool& bStopAuthorize)
  1612. {
  1613. int resTemplateNum = matchParam->sMatchAns.result.GetCount();
  1614. if (resTemplateNum != matchParam->sTotalNumOfTemplate)
  1615. {
  1616. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("result templateNum(%d) is not equale to the Req's(%d)."
  1617. , resTemplateNum, matchParam->sTotalNumOfTemplate);
  1618. return Error_Unexpect;
  1619. }
  1620. std::vector<TemplateInfo>tmpFingerCountInfo(matchParam->sFingerCount);
  1621. int matchIndex = -1; //matched CustomerID's index in array
  1622. int matchCount = 0; //num of matched
  1623. int fingerCountIndex = 0; //index of fingerCountInfo
  1624. int FINGER_NUM = tmpFingerCountInfo[fingerCountIndex].TemplateNum;
  1625. for (int i = 0; i < resTemplateNum; i += FINGER_NUM)
  1626. {
  1627. int oneCustMatchResult = 0;
  1628. FINGER_NUM = tmpFingerCountInfo[fingerCountIndex].TemplateNum;
  1629. for (int j = 0; j < FINGER_NUM; ++j)
  1630. {
  1631. oneCustMatchResult += matchParam->sMatchAns.result[i + j];
  1632. }
  1633. if (oneCustMatchResult > 0) //one customer has more than one match, just as one
  1634. {
  1635. matchCount++;
  1636. matchIndex = fingerCountIndex;
  1637. }
  1638. if (matchCount > 1) //more than one customer's finger matched
  1639. break;
  1640. fingerCountIndex++;
  1641. }
  1642. int matchResult = matchCount;//0:no match; 1:just one match; 2: muti match
  1643. switch (matchResult)
  1644. {
  1645. case 0:
  1646. {
  1647. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("MatchResult: 0| no match");
  1648. BroadcastGetFinger(0);
  1649. }
  1650. break;
  1651. case 1:
  1652. {
  1653. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("MatchResult: 1| one and only one match, authorize done.");
  1654. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("Match Finger(CustomerID=%s)", (const char*)tmpFingerCountInfo[matchIndex].CustomerID);
  1655. m_authCtx.eAuthByWhich = AuthByFngPrnt;
  1656. m_authCtx.CustomerID = tmpFingerCountInfo[matchIndex].CustomerID;
  1657. bStopAuthorize = true;
  1658. return Error_Succeed;
  1659. }
  1660. break;
  1661. case 2:
  1662. {
  1663. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("MatchResult: 2 | two and more matches. be alerted");
  1664. BroadcastGetFinger(1);
  1665. }
  1666. break;
  1667. default:break;
  1668. }
  1669. return Error_Unexpect;
  1670. }
  1671. ErrorCodeEnum CCustMngrAuthFSM::MatchFingerPrintEx(SpReqAnsContext<CustMngrAuthService_QueryAuthorInfo_Req, CustMngrAuthService_QueryAuthorInfo_Ans>::Pointer ctx, bool& bStopAuthorize, DWORD& dwUserErrCode)
  1672. {
  1673. LOG_FUNCTION();
  1674. LogWarn(Severity_Low, Error_Unexpect, LOG_ERR_CUSTMNGRAUTH_INVOKE_MATCHEx, "web invoke MatchFingerPrintEx");
  1675. ErrorCodeEnum errCode;
  1676. m_pFingerPrint = new FingerPrintService_ClientBase(m_pEntity);
  1677. errCode = m_pFingerPrint->Connect();
  1678. if (errCode != Error_Succeed)
  1679. {
  1680. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("ERROR: connect to fingerprint entity failed while match %s", SpStrError(errCode));
  1681. m_pFingerPrint->SafeDelete();
  1682. m_pFingerPrint = NULL;
  1683. return Error_Hardware; //TODO::give one other errCode
  1684. }
  1685. CSimpleStringA depPath(true);
  1686. m_pEntity->GetFunction()->GetPath("Dep", depPath);
  1687. FingerPrintService_GetFingerPrint_Req GetFingerReq;
  1688. FingerPrintService_GetFingerPrint_Ans GetFingerAns;
  1689. GetFingerReq.times = 10;
  1690. if (m_pFingerPrint == NULL || m_pFingerPrint->QuerySessionClosed())
  1691. {
  1692. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("m_pFingerPrint is NULL or connection closed.");
  1693. return Error_Hardware;
  1694. }
  1695. errCode = m_pFingerPrint->GetFingerPrint(GetFingerReq, GetFingerAns, 30000, dwUserErrCode);
  1696. if (errCode == Error_NotInit)
  1697. {
  1698. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER)("FingerPrintEntity not ready or failed.");
  1699. return Error_Hardware;
  1700. }
  1701. bStopAuthorize = true;
  1702. if (m_bCancelAuthorize || GetFingerAns.reserved1[0] == 3) {
  1703. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("GetFingerPrint is canceled while match.");
  1704. m_bCancelAuthorize = false;
  1705. if (m_bAuthorizeTimeout) {
  1706. m_bAuthorizeTimeout = false;
  1707. return Error_TimeOut;
  1708. }
  1709. return Error_Cancel;
  1710. }
  1711. if (errCode == Error_Succeed)
  1712. {
  1713. switch (GetFingerAns.reserved1[0])
  1714. {
  1715. case 1:
  1716. errCode = Error_Succeed;
  1717. break;
  1718. case 2:
  1719. errCode = Error_TimeOut;
  1720. break;
  1721. case 3:
  1722. errCode = Error_Cancel;
  1723. break;
  1724. default:
  1725. errCode = Error_Unexpect;
  1726. break;
  1727. }
  1728. if (errCode == Error_Succeed)
  1729. {
  1730. m_authCtx.eAuthByWhich = AuthByFngPrnt;
  1731. ctx->Ans.Feature = GetFingerAns.feature;
  1732. CBlob data;
  1733. CSimpleStringA imgFullPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", depPath.GetData(), GetFingerAns.imageName.GetData());
  1734. ErrorCodeEnum imgErrCode = GetImgBlob(data, imgFullPath);
  1735. if (imgErrCode != Error_Succeed)
  1736. {
  1737. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Failed to load image(%s)", imgFullPath.GetData());
  1738. return Error_Unexpect;
  1739. }
  1740. ctx->Ans.FingerImg = data;
  1741. m_authCtx.Feature = ctx->Ans.Feature;
  1742. m_authCtx.FingerImg = ctx->Ans.FingerImg;
  1743. }
  1744. }
  1745. else
  1746. errCode = Error_Unexpect;
  1747. DeleteBmp(BmpImage);
  1748. return errCode;
  1749. }
  1750. void CCustMngrAuthFSM::DeleteBmp(int type)
  1751. {
  1752. if ((type & BmpImage) == BmpImage)
  1753. {
  1754. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("to delete fingerprint images...");
  1755. CSimpleStringA imageName;
  1756. DeleteFileIfExisted("finger.bmp");
  1757. for (int i = 1; i <= 10; ++i) {
  1758. imageName = CSimpleString::Format("finger%d.bmp", i);
  1759. DeleteFileIfExisted(imageName.GetData());
  1760. }
  1761. }
  1762. if ((type & TestImage) == TestImage)
  1763. {
  1764. DeleteFileIfExisted("testFinger.bmp");
  1765. }
  1766. }
  1767. ErrorCodeEnum CCustMngrAuthFSM::DeleteFileIfExisted(const char* fileName)
  1768. {
  1769. if (strlen(fileName) == 0 || strchr(fileName, (int)'*') != NULL)
  1770. {
  1771. DbgWithLink(LOG_LEVEL_ERROR, LOG_TYPE_SYSTEM)("Invalid or empty filename(%s)", fileName);
  1772. return Error_Param;
  1773. }
  1774. CSimpleStringA strPath, strObjPath;
  1775. ErrorCodeEnum errCode = m_pEntity->GetFunction()->GetPath("Dep", strPath);
  1776. strObjPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s"
  1777. , strPath.GetData(), fileName);
  1778. if (ExistsFileA(strObjPath))
  1779. {
  1780. if (RemoveFileA(strObjPath))
  1781. {
  1782. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("DeleteFile(%s) success.", strObjPath.GetData());
  1783. return Error_Succeed;
  1784. }
  1785. else {
  1786. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("DeleteFile(%s) failed LastError(%s).", strObjPath.GetData(), GetLastError());
  1787. return Error_Unexpect;
  1788. }
  1789. }
  1790. else {
  1791. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("file %s not exist.", fileName);
  1792. return Error_Succeed;
  1793. }
  1794. }
  1795. #pragma endregion