FingerPrintFSM.h 12 KB

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