FingerPrintFSM.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. #ifndef FINGERPRINT_FSM_H
  2. #define FINGERPRINT_FSM_H
  3. #include "CommEntityUtil.hpp"
  4. #include "DevFSMCommBase.hpp"
  5. #include "DeviceBaseHelper.h"
  6. #include "FingerPrintClass.h"
  7. #include "CardSwiper_client_g.h"
  8. #include "fileutil.h"
  9. #include <ctime>
  10. using namespace CardSwiper;
  11. struct ScanParam
  12. {
  13. CSimpleStringA m_DepPath;
  14. CSimpleStringA m_FullFilePath;
  15. CSimpleStringA m_BmpFileName;
  16. ULLINT m_TimeStart;
  17. ULLINT m_TimeEnd;
  18. ULLINT m_TimeLeft;
  19. LPBYTE m_Feature;
  20. LPBYTE m_Template;
  21. int m_FeatureLen;
  22. bool m_ScanSuc;
  23. bool m_GetTemplateSuc;
  24. bool m_NotFindImage;
  25. bool m_FeatureIsNull;
  26. bool m_bLenIsNotRight;
  27. bool m_bGetFingerPrintSuc;
  28. bool m_Quit;
  29. };
  30. enum OperateType
  31. {
  32. RegisterType = 1,
  33. MatchType
  34. };
  35. enum EvtType
  36. {
  37. USER_EVT_TEST = EVT_USER + 1,
  38. USER_EVT_GET_DEVINFO,
  39. USER_EVT_SCAN,
  40. USER_EVT_SCAN_FINISHED,
  41. USER_EVT_SCANEX,
  42. USER_EVT_SCANEX_FINISHED,
  43. USER_EVT_MATCH,
  44. USER_EVT_MATCH_FINISHED,
  45. USER_EVT_GETFINGER,
  46. USER_EVT_GETFINGER_FINISHED,
  47. USER_EVT_GENERATE_TEMPLATE,
  48. USER_EVT_GENERATE_TEMPLATE_FINISHED,
  49. USER_EVT_CANCEL_SCAN,
  50. USER_EVT_CANCEL_MATCH,
  51. USER_EVT_CANCEL_SCAN_FINISHED,
  52. USER_EVT_QUIT,
  53. USER_EVT_ERROR,
  54. USER_EVT_EXIT
  55. };
  56. enum BmpType
  57. {
  58. BmpImage = 1,
  59. TestImage
  60. };
  61. #include "FingerPrint_server_g.h"
  62. #include "FingerPrintClass.h"
  63. using namespace FingerPrint;
  64. class ScanEvent : public FSMEvent
  65. {
  66. public:
  67. ScanEvent() : FSMEvent(USER_EVT_SCAN){}
  68. virtual ~ScanEvent(){}
  69. SpReqAnsContext<FingerPrintService_GetImageAndFeature_Req, FingerPrintService_GetImageAndFeature_Ans>::Pointer ctx;
  70. virtual void OnUnhandled()
  71. {
  72. if (ctx != NULL) {
  73. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("ScanEvent unhandled.");
  74. ctx->Answer(Error_InvalidState);
  75. }
  76. }
  77. };
  78. class ScanExEvent : public FSMEvent
  79. {
  80. public:
  81. ScanExEvent() : FSMEvent(USER_EVT_SCANEX) {}
  82. virtual ~ScanExEvent() {}
  83. SpReqAnsContext<FingerPrintService_GetImageAndFeatureEx_Req, FingerPrintService_GetImageAndFeatureEx_Ans>::Pointer ctx;
  84. virtual void OnUnhandled()
  85. {
  86. if (ctx != NULL)
  87. {
  88. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("ScanEx unhandled");
  89. ctx->Answer(Error_InvalidState);
  90. }
  91. }
  92. };
  93. class GetFingerEvent : public FSMEvent
  94. {
  95. public:
  96. GetFingerEvent() : FSMEvent(USER_EVT_GETFINGER) {}
  97. virtual ~GetFingerEvent() {}
  98. SpReqAnsContext<FingerPrintService_GetFingerPrint_Req, FingerPrintService_GetFingerPrint_Ans>::Pointer ctx;
  99. virtual void OnUnhandled()
  100. {
  101. if (ctx != NULL)
  102. {
  103. ctx->Answer(Error_InvalidState);
  104. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("GetFinger unhandled");
  105. }
  106. }
  107. };
  108. class GenerateTemplateEvent : public FSMEvent
  109. {
  110. public:
  111. GenerateTemplateEvent() : FSMEvent(USER_EVT_GENERATE_TEMPLATE) {}
  112. virtual ~GenerateTemplateEvent() {}
  113. SpReqAnsContext<FingerPrintService_GenerateTemplate_Req, FingerPrintService_GenerateTemplate_Ans>::Pointer ctx;
  114. virtual void OnUnhandled()
  115. {
  116. if (ctx != NULL)
  117. {
  118. ctx->Answer(Error_InvalidState);
  119. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("GenerateTemplate unhandled.");
  120. }
  121. }
  122. };
  123. class CancelRegisterEvent : public FSMEvent
  124. {
  125. public:
  126. CancelRegisterEvent() : FSMEvent(USER_EVT_CANCEL_SCAN){}
  127. virtual ~CancelRegisterEvent(){}
  128. virtual void OnUnhandled()
  129. {
  130. LOG_TRACE("Cancel scan not handled");
  131. }
  132. };
  133. class MatchEvent : public FSMEvent
  134. {
  135. public:
  136. MatchEvent() : FSMEvent(USER_EVT_MATCH){}
  137. virtual ~MatchEvent(){}
  138. SpReqAnsContext<FingerPrintService_Match_Req, FingerPrintService_Match_Ans>::Pointer ctx;
  139. virtual void OnUnhandled()
  140. {
  141. if (ctx != NULL)
  142. {
  143. ctx->Answer(Error_InvalidState);
  144. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("match unhandled.");
  145. }
  146. }
  147. };
  148. class MatchFinishedEvent : public FSMEvent
  149. {
  150. public:
  151. MatchFinishedEvent() : FSMEvent(USER_EVT_MATCH_FINISHED){}
  152. ~MatchFinishedEvent(){}
  153. SpReqAnsContext<FingerPrintService_Match_Req, FingerPrintService_Match_Ans>::Pointer ctx;
  154. virtual void OnUnhandled()
  155. {
  156. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Match finished unhandled.");
  157. }
  158. };
  159. class GetFingerFinishedEvent : public FSMEvent
  160. {
  161. public:
  162. GetFingerFinishedEvent() : FSMEvent(USER_EVT_GETFINGER_FINISHED) {}
  163. ~GetFingerFinishedEvent() {}
  164. SpReqAnsContext<FingerPrintService_GetFingerPrint_Req, FingerPrintService_GetFingerPrint_Ans>::Pointer ctx;
  165. virtual void OnUnhandled()
  166. {
  167. if (ctx != NULL)
  168. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("GetFingerFinished unhandled.");
  169. }
  170. };
  171. class CancelMatchEvent : public FSMEvent
  172. {
  173. public:
  174. CancelMatchEvent() : FSMEvent(USER_EVT_CANCEL_MATCH){}
  175. virtual ~CancelMatchEvent(){}
  176. virtual void OnUnhandled()
  177. {
  178. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM)("Cancel match not handled");
  179. }
  180. };
  181. class GetDevInfoEvent : public FSMEvent
  182. {
  183. public:
  184. GetDevInfoEvent() : FSMEvent(USER_EVT_GET_DEVINFO){}
  185. virtual ~GetDevInfoEvent(){}
  186. SpReqAnsContext<FingerPrintService_GetDevInfo_Req, FingerPrintService_GetDevInfo_Ans>::Pointer ctx;
  187. virtual void OnUnhandled()
  188. {
  189. if(ctx != NULL)
  190. ctx->Answer(Error_InvalidState);
  191. }
  192. };
  193. class CFingerPrintFSM : public CCommDevFSM<CFingerPrintFSM, FingerPrintClass>
  194. {
  195. public:
  196. enum {s0, s1, s2, s3, s4};
  197. BEGIN_FSM_STATE(CFingerPrintFSM)
  198. FSM_STATE_ENTRY(s0, "Normal", s0_on_entry,s0_on_exit,s0_on_event)
  199. FSM_STATE_ENTRY(s1, "Scan", s1_on_entry,s1_on_exit,s1_on_event)
  200. FSM_STATE_ENTRY(s2, "Fail", s2_on_entry,s2_on_exit,s2_on_event)
  201. FSM_STATE_ENTRY(s3, "Match", s3_on_entry,s3_on_exit,s3_on_event)
  202. END_FSM_STATE()
  203. BEGIN_FSM_RULE(CFingerPrintFSM, s0)
  204. FSM_RULE_ENTRY(s0, FSM_STATE_EXIT, USER_EVT_QUIT, 0)
  205. FSM_RULE_ENTRY(s0, s1, USER_EVT_SCAN, 0)
  206. FSM_RULE_ENTRY(s0, s1, USER_EVT_SCANEX, 0)
  207. FSM_RULE_ENTRY(s0, s1, USER_EVT_GETFINGER, 0)
  208. FSM_RULE_ENTRY(s0, s1, USER_EVT_GENERATE_TEMPLATE, 0)
  209. FSM_RULE_ENTRY(s0, s2, USER_EVT_ERROR, 0)
  210. FSM_RULE_ENTRY(s0, s3, USER_EVT_MATCH, 0)
  211. FSM_RULE_ENTRY(s1, FSM_STATE_EXIT, USER_EVT_QUIT, 0)
  212. FSM_RULE_ENTRY(s1, s0, USER_EVT_SCAN_FINISHED, 0)
  213. FSM_RULE_ENTRY(s1, s2, USER_EVT_SCAN_FINISHED, 2)
  214. FSM_RULE_ENTRY(s1, s0, USER_EVT_SCANEX_FINISHED, 0)
  215. FSM_RULE_ENTRY(s1, s2, USER_EVT_SCANEX_FINISHED, 2)
  216. FSM_RULE_ENTRY(s1, s0, USER_EVT_GETFINGER_FINISHED, 0)
  217. FSM_RULE_ENTRY(s1, s2, USER_EVT_GETFINGER_FINISHED, 2)
  218. FSM_RULE_ENTRY(s1, s0, USER_EVT_GENERATE_TEMPLATE_FINISHED, 0)
  219. FSM_RULE_ENTRY(s1, s2, USER_EVT_GENERATE_TEMPLATE_FINISHED, 2)
  220. FSM_RULE_ENTRY(s3, FSM_STATE_EXIT, USER_EVT_QUIT, 0)
  221. FSM_RULE_ENTRY(s3, s0, USER_EVT_MATCH_FINISHED, 0)
  222. FSM_RULE_ENTRY(s3, s2, USER_EVT_MATCH_FINISHED, 2)
  223. END_FSM_RULE()
  224. CFingerPrintFSM():m_devInit(false), m_bCancelRegister(false), m_bCancelMatch(false),
  225. m_bExit(false), m_testResult(Error_Succeed), m_csDevSN("")
  226. {
  227. HARDWARE_ENTITY_RESET_ENTITYID(m_entCode, 0x204);
  228. m_FirstStart = TRUE;
  229. ZeroMemory(&m_adapterInfo, sizeof(m_adapterInfo));
  230. };
  231. virtual ErrorCodeEnum OnInit();
  232. virtual ErrorCodeEnum OnExit();
  233. void s0_on_entry();
  234. void s0_on_exit();
  235. unsigned int s0_on_event(FSMEvent* e);
  236. void s1_on_entry();
  237. void s1_on_exit();
  238. unsigned int s1_on_event(FSMEvent* e);
  239. void s2_on_entry();
  240. void s2_on_exit();
  241. unsigned int s2_on_event(FSMEvent* e);
  242. void s3_on_entry();
  243. void s3_on_exit();
  244. unsigned int s3_on_event(FSMEvent* e);
  245. int GetImageAndFeature(SpReqAnsContext<FingerPrintService_GetImageAndFeature_Req, FingerPrintService_GetImageAndFeature_Ans>::Pointer ctx);
  246. int GetImageAndFeatureEx(SpReqAnsContext<FingerPrintService_GetImageAndFeatureEx_Req, FingerPrintService_GetImageAndFeatureEx_Ans>::Pointer ctx);
  247. int GetFingerPrint(SpReqAnsContext<FingerPrintService_GetFingerPrint_Req, FingerPrintService_GetFingerPrint_Ans>::Pointer ctx);
  248. int GenerateTemplate(SpReqAnsContext<FingerPrintService_GenerateTemplate_Req, FingerPrintService_GenerateTemplate_Ans>::Pointer ctx);
  249. ErrorCodeEnum Match(SpReqAnsContext<FingerPrintService_Match_Req, FingerPrintService_Match_Ans>::Pointer ctx);
  250. void DeleteBmp(int type);
  251. ErrorCodeEnum DeleteFileIfExisted(const char* fileName);
  252. ErrorCodeEnum GetDevCatInfo(DevCategoryInfo& devInfo);
  253. ErrorCodeEnum GetDevState(int& state);
  254. ErrorCodeEnum CheckCardSwiperStatus();
  255. CSimpleString GenerateAlarmJson(CSimpleString entityName, int cost);
  256. ErrorCodeEnum DoDevOpen(CSmartPointer<IConfigInfo> spConfig);
  257. ErrorCodeEnum DoGetDevInfo();
  258. ErrorCodeEnum QueryRootConfigObj(CSmartPointer<IConfigInfo> &spConfig);
  259. bool IsFWBDevice(CSimpleString& vendorName);
  260. ErrorCodeEnum InitCommParam(ScanParam* initParam, int type, int scanTime, int templateNum = -1);
  261. ErrorCodeEnum InitParamBeforeScan(ScanParam *initParam, int scanTime);
  262. void ScanProcess(ScanParam* pScanParam, SpReqAnsContext<FingerPrintService_GetImageAndFeature_Req, FingerPrintService_GetImageAndFeature_Ans>::Pointer &ctx);
  263. void ScanProcessEx(ScanParam* pScanParam, SpReqAnsContext<FingerPrintService_GetImageAndFeatureEx_Req, FingerPrintService_GetImageAndFeatureEx_Ans>::Pointer& ctx);
  264. void ProcessAfterScan(ScanParam* pScanParam, SpReqAnsContext<FingerPrintService_GetImageAndFeature_Req, FingerPrintService_GetImageAndFeature_Ans>::Pointer& ctx);
  265. void ProcessAfterScanEx(ScanParam* pScanParam, SpReqAnsContext<FingerPrintService_GetImageAndFeatureEx_Req, FingerPrintService_GetImageAndFeatureEx_Ans>::Pointer& ctx);
  266. void CollectProcess(ScanParam* pScanParam, SpReqAnsContext<FingerPrintService_GetFingerPrint_Req, FingerPrintService_GetFingerPrint_Ans>::Pointer& ctx);
  267. void ProcessAfterCollect(ScanParam* pScanParam, SpReqAnsContext<FingerPrintService_GetFingerPrint_Req, FingerPrintService_GetFingerPrint_Ans>::Pointer& ctx);
  268. ErrorCodeEnum InitParamBeforeMatch(ScanParam* initParam, int templateNum);
  269. void ScanBeforeMatch(ScanParam* initParam);
  270. ErrorCodeEnum MatchProcess(ScanParam* initParam, SpReqAnsContext<FingerPrintService_Match_Req, FingerPrintService_Match_Ans>::Pointer& ctx);
  271. ErrorCodeEnum ExtractVendorLib(CSimpleStringA& strLibFullPath);
  272. void SelfTest(EntityTestEnum eTestType,CSmartPointer<ITransactionContext> pTransactionContext);
  273. private:
  274. ErrorCodeEnum m_testResult;
  275. DevCategoryInfo m_devCatInfo;
  276. DevStateEnum m_devState;
  277. CSimpleStringA m_BmpFileFullPath1;
  278. CSimpleStringA m_BmpFileFullPath2;
  279. CSimpleStringA m_BmpFileFullPath3;
  280. CSimpleStringA m_csDevSN;
  281. bool m_bCancelRegister;
  282. bool m_bCancelMatch;
  283. bool m_devInit;
  284. bool m_bExit;
  285. ErrorPackage m_errPkg;
  286. AdapterInfo m_adapterInfo;
  287. };
  288. struct ScanTask : public ITaskSp
  289. {
  290. CFingerPrintFSM* fsm;
  291. SpReqAnsContext<FingerPrintService_GetImageAndFeature_Req, FingerPrintService_GetImageAndFeature_Ans>::Pointer ctx;
  292. ScanTask(CFingerPrintFSM* f) : fsm(f){}
  293. void Process()
  294. {
  295. LOG_FUNCTION();
  296. FSMEvent* e = new FSMEvent(USER_EVT_SCAN_FINISHED);
  297. e->param1 = fsm->GetImageAndFeature(ctx);
  298. fsm->PostEventFIFO(e);
  299. }
  300. };
  301. struct ScanExTask : public ITaskSp
  302. {
  303. CFingerPrintFSM* fsm;
  304. SpReqAnsContext<FingerPrintService_GetImageAndFeatureEx_Req, FingerPrintService_GetImageAndFeatureEx_Ans>::Pointer ctx;
  305. ScanExTask(CFingerPrintFSM* f) : fsm(f) {}
  306. void Process()
  307. {
  308. LOG_FUNCTION();
  309. FSMEvent* e = new FSMEvent(USER_EVT_SCANEX_FINISHED);
  310. e->param1 = fsm->GetImageAndFeatureEx(ctx);
  311. fsm->PostEventFIFO(e);
  312. }
  313. };
  314. struct GetFingerTask : public ITaskSp
  315. {
  316. CFingerPrintFSM* fsm;
  317. SpReqAnsContext<FingerPrintService_GetFingerPrint_Req, FingerPrintService_GetFingerPrint_Ans>::Pointer ctx;
  318. GetFingerTask(CFingerPrintFSM* f) : fsm(f) {}
  319. void Process()
  320. {
  321. LOG_FUNCTION();
  322. FSMEvent* e = new FSMEvent(USER_EVT_GETFINGER_FINISHED);
  323. e->param1 = fsm->GetFingerPrint(ctx);
  324. fsm->PostEventFIFO(e);
  325. }
  326. };
  327. struct GenerateTemplateTask : public ITaskSp
  328. {
  329. CFingerPrintFSM* fsm;
  330. SpReqAnsContext<FingerPrintService_GenerateTemplate_Req, FingerPrintService_GenerateTemplate_Ans>::Pointer ctx;
  331. GenerateTemplateTask(CFingerPrintFSM* f) : fsm(f) {}
  332. void Process()
  333. {
  334. LOG_FUNCTION();
  335. FSMEvent* e = new FSMEvent(USER_EVT_GENERATE_TEMPLATE_FINISHED);
  336. e->param1 = fsm->GenerateTemplate(ctx);
  337. fsm->PostEventFIFO(e);
  338. }
  339. };
  340. struct MatchTask : public ITaskSp
  341. {
  342. CFingerPrintFSM* fsm;
  343. SpReqAnsContext<FingerPrintService_Match_Req, FingerPrintService_Match_Ans>::Pointer ctx;
  344. MatchTask(CFingerPrintFSM* f) : fsm(f){}
  345. void Process()
  346. {
  347. LOG_FUNCTION();
  348. MatchFinishedEvent* e = new MatchFinishedEvent();
  349. e->param1 = fsm->Match(ctx);
  350. e->ctx = ctx;
  351. fsm->PostEventFIFO(e);
  352. }
  353. };
  354. #endif