FingerPrintFSM.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #ifndef FINGERPRINT_FSM_H
  2. #define FINGERPRINT_FSM_H
  3. #include "DevFSMCommBase.hpp"
  4. #include "FingerPrintClass.h"
  5. #include "CardSwiper_client_g.h"
  6. #include <ctime>
  7. using namespace CardSwiper;
  8. #define FINGERPRINT_SCAN_TIMEOUT 15000 * 1000 //us
  9. #define FINGERPRINT_SCAN_INTERNAL 100 //ms
  10. #define FINGERPRINT_MATCH_TIMEOUT 15000 * 1000 //us
  11. #define MAX_FEATURE_LEN 1024
  12. struct ScanParam
  13. {
  14. CSimpleStringA m_DepPath;
  15. CSimpleStringA m_FullFilePath;
  16. CSimpleStringA m_BmpFileName;
  17. clock_t m_TimeStart;
  18. clock_t m_TimeEnd;
  19. DWORD m_TimeLeft;
  20. LPBYTE m_Feature;
  21. LPBYTE m_Template;
  22. int m_FeatureLen;
  23. bool m_ScanSuc;
  24. bool m_GetTemplateSuc;
  25. bool m_NotFindImage;
  26. bool m_FeatureIsNull;
  27. bool m_Quit;
  28. };
  29. enum EvtType
  30. {
  31. USER_EVT_TEST = EVT_USER + 1,
  32. USER_EVT_GET_DEVINFO,
  33. USER_EVT_SCAN,
  34. USER_EVT_SCAN_FINISHED,
  35. USER_EVT_MATCH,
  36. USER_EVT_MATCH_FINISHED,
  37. USER_EVT_CANCEL_SCAN,
  38. USER_EVT_CANCEL_MATCH,
  39. USER_EVT_CANCEL_SCAN_FINISHED,
  40. USER_EVT_QUIT,
  41. USER_EVT_ERROR,
  42. USER_EVT_EXIT
  43. };
  44. enum BmpType
  45. {
  46. BmpImage = 1,
  47. TestImage
  48. };
  49. #include "FingerPrint_server_g.h"
  50. #include "FingerPrintClass.h"
  51. using namespace FingerPrint;
  52. typedef ErrorCodeEnum (*lpCreateDevCom)(DeviceBaseClass *&baseObj);
  53. typedef ErrorCodeEnum (*lpReleaseDevCom)(DeviceBaseClass *&pBaseObj);
  54. class ScanEvent : public FSMEvent
  55. {
  56. public:
  57. ScanEvent() : FSMEvent(USER_EVT_SCAN){}
  58. virtual ~ScanEvent(){}
  59. SpReqAnsContext<FingerPrintService_GetImageAndFeature_Req, FingerPrintService_GetImageAndFeature_Ans>::Pointer ctx;
  60. virtual void OnUnhandled()
  61. {
  62. if(ctx != NULL)
  63. ctx->Answer(Error_InvalidState);
  64. }
  65. };
  66. class CancelRegisterEvent : public FSMEvent
  67. {
  68. public:
  69. CancelRegisterEvent() : FSMEvent(USER_EVT_CANCEL_SCAN){}
  70. virtual ~CancelRegisterEvent(){}
  71. virtual void OnUnhandled()
  72. {
  73. LOG_TRACE("Cancel scan not handled");
  74. }
  75. };
  76. class MatchEvent : public FSMEvent
  77. {
  78. public:
  79. MatchEvent() : FSMEvent(USER_EVT_MATCH){}
  80. virtual ~MatchEvent(){}
  81. SpReqAnsContext<FingerPrintService_Match_Req, FingerPrintService_Match_Ans>::Pointer ctx;
  82. virtual void OnUnhandled()
  83. {
  84. if (ctx != NULL)
  85. ctx->Answer(Error_InvalidState);
  86. }
  87. };
  88. class MatchFinishedEvent : public FSMEvent
  89. {
  90. public:
  91. MatchFinishedEvent() : FSMEvent(USER_EVT_MATCH_FINISHED){}
  92. ~MatchFinishedEvent(){}
  93. SpReqAnsContext<FingerPrintService_Match_Req, FingerPrintService_Match_Ans>::Pointer ctx;
  94. virtual void OnUnhandled()
  95. {
  96. Dbg("Match finished unhandled.");
  97. }
  98. };
  99. class CancelMatchEvent : public FSMEvent
  100. {
  101. public:
  102. CancelMatchEvent() : FSMEvent(USER_EVT_CANCEL_MATCH){}
  103. virtual ~CancelMatchEvent(){}
  104. virtual void OnUnhandled()
  105. {
  106. LOG_TRACE("Cancel match not handled");
  107. }
  108. };
  109. class GetDevInfoEvent : public FSMEvent
  110. {
  111. public:
  112. GetDevInfoEvent() : FSMEvent(USER_EVT_GET_DEVINFO){}
  113. virtual ~GetDevInfoEvent(){}
  114. SpReqAnsContext<FingerPrintService_GetDevInfo_Req, FingerPrintService_GetDevInfo_Ans>::Pointer ctx;
  115. virtual void OnUnhandled()
  116. {
  117. if(ctx != NULL)
  118. ctx->Answer(Error_InvalidState);
  119. }
  120. };
  121. class CFingerPrintFSM : public CCommDevFSM<CFingerPrintFSM, FingerPrintClass>
  122. {
  123. public:
  124. enum {s0, s1, s2, s3, s4};
  125. BEGIN_FSM_STATE(CFingerPrintFSM)
  126. FSM_STATE_ENTRY(s0, "Normal", s0_on_entry,s0_on_exit,s0_on_event)
  127. FSM_STATE_ENTRY(s1, "Scan", s1_on_entry,s1_on_exit,s1_on_event)
  128. FSM_STATE_ENTRY(s2, "Fail", s2_on_entry,s2_on_exit,s2_on_event)
  129. FSM_STATE_ENTRY(s3, "Match", s3_on_entry,s3_on_exit,s3_on_event)
  130. END_FSM_STATE()
  131. BEGIN_FSM_RULE(CFingerPrintFSM, s0)
  132. FSM_RULE_ENTRY(s0, FSM_STATE_EXIT, USER_EVT_QUIT, 0)
  133. FSM_RULE_ENTRY(s0, s1, USER_EVT_SCAN, 0)
  134. FSM_RULE_ENTRY(s0, s2, USER_EVT_ERROR, 0)
  135. FSM_RULE_ENTRY(s0, s3, USER_EVT_MATCH, 0)
  136. FSM_RULE_ENTRY(s1, FSM_STATE_EXIT, USER_EVT_QUIT, 0)
  137. FSM_RULE_ENTRY(s1, s0, USER_EVT_SCAN_FINISHED, 0)
  138. FSM_RULE_ENTRY(s1, s2, USER_EVT_SCAN_FINISHED, 2)
  139. FSM_RULE_ENTRY(s3, FSM_STATE_EXIT, USER_EVT_QUIT, 0)
  140. FSM_RULE_ENTRY(s3, s0, USER_EVT_MATCH_FINISHED, 0)
  141. FSM_RULE_ENTRY(s3, s2, USER_EVT_MATCH_FINISHED, 2)
  142. END_FSM_RULE()
  143. CFingerPrintFSM():m_devInit(false), m_bCancelRegister(false), m_bCancelMatch(false),
  144. m_bExit(false), m_testResult(Error_Succeed), m_DevAdptLibHelper(NULL){};
  145. virtual ErrorCodeEnum OnInit();
  146. virtual ErrorCodeEnum OnExit();
  147. void s0_on_entry();
  148. void s0_on_exit();
  149. unsigned int s0_on_event(FSMEvent* e);
  150. void s1_on_entry();
  151. void s1_on_exit();
  152. unsigned int s1_on_event(FSMEvent* e);
  153. void s2_on_entry();
  154. void s2_on_exit();
  155. unsigned int s2_on_event(FSMEvent* e);
  156. void s3_on_entry();
  157. void s3_on_exit();
  158. unsigned int s3_on_event(FSMEvent* e);
  159. int GetImageAndFeature(SpReqAnsContext<FingerPrintService_GetImageAndFeature_Req, FingerPrintService_GetImageAndFeature_Ans>::Pointer ctx);
  160. ErrorCodeEnum Match(SpReqAnsContext<FingerPrintService_Match_Req, FingerPrintService_Match_Ans>::Pointer ctx);
  161. void DeleteBmp(int type);
  162. ErrorCodeEnum DeleteFileIfExisted(LPCTSTR fileName);
  163. bool IsFileExist(CSimpleString fileName);
  164. ErrorCodeEnum GetDevCatInfo(DevCategoryInfo& devInfo);
  165. ErrorCodeEnum GetDevState(int& state);
  166. ErrorCodeEnum CheckCardSwiperStatus();
  167. CSimpleString GenerateAlarmJson(CSimpleString entityName, int cost);
  168. void GetVendorLibName(CSmartPointer<IConfigInfo> spConfig, CSimpleString& dllName);
  169. ErrorCodeEnum GetMachineInfo(CSimpleString& machineVersion, CSimpleString& machineType);
  170. ErrorCodeEnum DoDevOpen(CSmartPointer<IConfigInfo> spConfig);
  171. ErrorCodeEnum DoGetDevInfo();
  172. ErrorCodeEnum QueryRootConfigObj(CSmartPointer<IConfigInfo> &spConfig);
  173. bool IsFWBDevice();
  174. ErrorCodeEnum InitParamBeforeScan(ScanParam *initParam, int scanTime);
  175. void ScanProcess(ScanParam* pScanParam, SpReqAnsContext<FingerPrintService_GetImageAndFeature_Req, FingerPrintService_GetImageAndFeature_Ans>::Pointer &ctx);
  176. void ProcessAfterScan(ScanParam* pScanParam, SpReqAnsContext<FingerPrintService_GetImageAndFeature_Req, FingerPrintService_GetImageAndFeature_Ans>::Pointer& ctx);
  177. void SelfTest(EntityTestEnum eTestType,CSmartPointer<ITransactionContext> pTransactionContext);
  178. private:
  179. DevAdptLibHelper<FingerPrintClass> *m_DevAdptLibHelper;
  180. ErrorCodeEnum m_testResult;
  181. DevCategoryInfo m_devCatInfo;
  182. DevStateEnum m_devState;
  183. CSimpleStringA m_BmpFileFullPath1;
  184. CSimpleStringA m_BmpFileFullPath2;
  185. CSimpleStringA m_BmpFileFullPath3;
  186. bool m_bCancelRegister;
  187. bool m_bCancelMatch;
  188. bool m_devInit;
  189. bool m_bExit;
  190. FingerPrintClass* m_pFingerPrint;
  191. };
  192. struct ScanTask : public ITaskSp
  193. {
  194. CFingerPrintFSM* fsm;
  195. SpReqAnsContext<FingerPrintService_GetImageAndFeature_Req, FingerPrintService_GetImageAndFeature_Ans>::Pointer ctx;
  196. ScanTask(CFingerPrintFSM* f) : fsm(f){}
  197. void Process()
  198. {
  199. LOG_FUNCTION();
  200. FSMEvent* e = new FSMEvent(USER_EVT_SCAN_FINISHED);
  201. e->param1 = fsm->GetImageAndFeature(ctx);
  202. fsm->PostEventFIFO(e);
  203. }
  204. };
  205. struct MatchTask : public ITaskSp
  206. {
  207. CFingerPrintFSM* fsm;
  208. SpReqAnsContext<FingerPrintService_Match_Req, FingerPrintService_Match_Ans>::Pointer ctx;
  209. MatchTask(CFingerPrintFSM* f) : fsm(f){}
  210. void Process()
  211. {
  212. LOG_FUNCTION();
  213. MatchFinishedEvent* e = new MatchFinishedEvent();
  214. e->param1 = fsm->Match(ctx);
  215. e->ctx = ctx;
  216. fsm->PostEventFIFO(e);
  217. }
  218. };
  219. #endif