CardSwiperFSM.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905
  1. #ifndef __CARD_SWIPER_FSM_H
  2. #define __CARD_SWIPER_FSM_H
  3. #pragma once
  4. #include "stdafx.h"
  5. #include "SpFSM.h"
  6. #include "sm2.h"
  7. #include "sm3.h"
  8. #include "public.h"
  9. #include "EventCode.h"
  10. #include "CardSwiper_UserErrorCode.h"
  11. #include "HealthManager_client_g.h"
  12. #include "HealthManager_def_g.h"
  13. using namespace HealthManager;
  14. #include "PinPad_client_g.h"
  15. using namespace PinPad;
  16. #include "DevFSMCommBase.hpp"
  17. class HealthMngClient : public HealthManagerService_ClientBase
  18. {
  19. public:
  20. HealthMngClient(CEntityBase* pEntity);
  21. };
  22. const int ONE_K = 1024;
  23. const int FAILED_MAXTIMES_TO_RESET = 2;
  24. enum EvtType
  25. {
  26. USER_EVT_TEST = EVT_USER+1,
  27. USER_EVT_QUIT,
  28. USER_EVT_INIT,
  29. USER_EVT_INITFINISHED,
  30. USER_EVT_ACCEPT,
  31. USER_EVT_ACCEPT_CANCEL,
  32. USER_EVT_ACCEPTFINISHED,
  33. USER_EVT_ISSUE,
  34. USER_EVT_ISSUE_FINISHED,
  35. USER_EVT_READ,
  36. USER_EVT_EJECT,
  37. USER_EVT_CAPTURE,
  38. USER_EVT_READFINISHED,
  39. USER_EVT_EJECTFINISHED,
  40. USER_EVT_WAITFINISHED,
  41. USER_EVT_CAPTUREFINISHED,
  42. USER_EVT_WAITFETCHINGTIMEOUT,
  43. USER_EVT_PREONLINE,
  44. USER_EVT_POSTONLINE,
  45. USER_EVT_PREONLINEFINISHED,
  46. USER_EVT_POSTONLINEFINISHED,
  47. USER_EVT_RESET,
  48. USER_EVT_RESETFINISHED,
  49. USER_EVT_EXIT,
  50. USER_EVT_CANCELINSERT,
  51. USER_EVT_GET_MATERIAL,
  52. USER_EVT_SET_MATERIAL,
  53. USER_EVT_QUERY_CARD_INFO,
  54. USER_EVT_QUERY_CARD_INFO_FINISHED,
  55. USER_EVT_MAGDATA_TRANSFER_INIT,
  56. USER_EVT_MAGDATA_TRANSFER_INIT_FINISHED,
  57. USER_EVT_MAGDATA_TRANSFER_INIT_V2,
  58. USER_EVT_MAGDATA_TRANSFER_INIT_V2_FINISHED,
  59. USER_EVT_DEV_DISCONNECTED,
  60. USER_EVT_DEV_CONNECTED,
  61. USER_EVT_WAITREMOVE,
  62. USER_EVT_WAITTODIE,
  63. USER_EVT_WAITTODIE_TIMEOUT,
  64. USER_EVT_ENCRYPTED_CHANNEL,
  65. USER_EVT_QUERY_FWB_LIST,
  66. USER_EVT_QUERY_FWB_LIST_FINISHED,
  67. USER_EVT_BIND_FWB,
  68. USER_EVT_BIND_FWB_FINISHED,
  69. };
  70. struct FWBRecord
  71. {
  72. CSimpleStringA name;
  73. CSimpleStringA remoteMac;
  74. };
  75. #include "CardSwiper_server_g.h"
  76. #include "CardSwiper_msg_g.h"
  77. #include "CardSwiperClass.h"
  78. #include "CardAssist.h"
  79. #include "ICDataElement.h"
  80. using namespace CardSwiper;
  81. #include "AccessAuthorization_client_g.h"
  82. using namespace AccessAuthorization;
  83. #include "DevErrorCode.h"
  84. typedef int(*lpCMBdecodeMag2)(char* Mag2Data, char * data);
  85. typedef int(*lpCMBdecodeEx)( char* MagData, char * type, char * data);
  86. typedef ErrorCodeEnum (*lpCreateDevCom)(DeviceBaseClass *&baseObj);
  87. typedef ErrorCodeEnum (*lpReleaseDevCom)(DeviceBaseClass *&pBaseObj);
  88. class CCardSwiperEntity;
  89. class CCardSwiperFSM;
  90. struct Track2Data
  91. {
  92. int status;
  93. CSimpleStringA t2Account;
  94. CSimpleStringA t2Region;
  95. CSimpleStringA t2CardSerial;
  96. CSimpleStringA t2CVC;
  97. CSimpleStringA t2ExpireDate;
  98. };
  99. struct CMBBin
  100. {
  101. CSimpleStringA bin;//6 byte long
  102. bool bIC;//IC:true or mag:false
  103. bool bDC;//debit:true,credit:false
  104. };
  105. class CardReadEvent : public FSMEvent
  106. {
  107. public:
  108. CardReadEvent() : FSMEvent(USER_EVT_READ){}
  109. ~CardReadEvent(){}
  110. SpReqAnsContext<CardSwiperService_Read_Req,CardSwiperService_Read_Ans>::Pointer ctx;
  111. virtual void OnUnhandled()
  112. {
  113. if (ctx != NULL)
  114. {
  115. Dbg("card read unhandled");
  116. ctx->Answer(Error_InvalidState);
  117. }
  118. }
  119. protected:
  120. private:
  121. };
  122. class CardReadFinishedEvent : public FSMEvent
  123. {
  124. public:
  125. CardReadFinishedEvent() : FSMEvent(USER_EVT_READFINISHED){}
  126. ~CardReadFinishedEvent(){}
  127. SpReqAnsContext<CardSwiperService_Read_Req,CardSwiperService_Read_Ans>::Pointer ctx;
  128. virtual void OnUnhandled()
  129. {
  130. if (ctx != NULL)
  131. {
  132. Dbg("card read unhandled");
  133. ctx->Answer(Error_InvalidState);
  134. }
  135. }
  136. protected:
  137. private:
  138. };
  139. class ResetDeviceEvent : public FSMEvent
  140. {
  141. public:
  142. ResetDeviceEvent() : FSMEvent(USER_EVT_RESET){}
  143. ~ResetDeviceEvent(){}
  144. protected:
  145. private:
  146. };
  147. class ResetFinishedEvent : public FSMEvent
  148. {
  149. public:
  150. ResetFinishedEvent() : FSMEvent(USER_EVT_RESETFINISHED){}
  151. ~ResetFinishedEvent(){}
  152. protected:
  153. private:
  154. };
  155. class PreOnlineEvent : public FSMEvent
  156. {
  157. public:
  158. PreOnlineEvent() : FSMEvent(USER_EVT_PREONLINE){}
  159. ~PreOnlineEvent(){}
  160. SpReqAnsContext<CardSwiperService_PreOnline_Req,CardSwiperService_PreOnline_Ans>::Pointer ctx;
  161. virtual void OnUnhandled()
  162. {
  163. if (ctx != NULL)
  164. ctx->Answer(Error_InvalidState);
  165. }
  166. protected:
  167. private:
  168. };
  169. class PostOnlineEvent : public FSMEvent
  170. {
  171. public:
  172. PostOnlineEvent() : FSMEvent(USER_EVT_POSTONLINE){}
  173. ~PostOnlineEvent(){}
  174. SpReqAnsContext<CardSwiperService_PostOnline_Req,CardSwiperService_PostOnline_Ans>::Pointer ctx;
  175. virtual void OnUnhandled()
  176. {
  177. if (ctx != NULL)
  178. ctx->Answer(Error_InvalidState);
  179. }
  180. protected:
  181. private:
  182. };
  183. class QueryCardInfoEvent : public FSMEvent
  184. {
  185. public:
  186. QueryCardInfoEvent() : FSMEvent(USER_EVT_QUERY_CARD_INFO){}
  187. ~QueryCardInfoEvent(){}
  188. SpReqAnsContext<CardSwiperService_QueryCardInfo_Req,CardSwiperService_QueryCardInfo_Ans>::Pointer ctx;
  189. virtual void OnUnhandled()
  190. {
  191. if (ctx != NULL)
  192. ctx->Answer(Error_InvalidState);
  193. }
  194. protected:
  195. private:
  196. };
  197. class MagDataTransferInitEvent : public FSMEvent
  198. {
  199. public:
  200. MagDataTransferInitEvent() : FSMEvent(USER_EVT_MAGDATA_TRANSFER_INIT){}
  201. ~MagDataTransferInitEvent(){}
  202. SpReqAnsContext<CardSwiperService_MagTransferInit_Req,CardSwiperService_MagTransferInit_Ans>::Pointer ctx;
  203. virtual void OnUnhandled()
  204. {
  205. if (ctx != NULL)
  206. {
  207. Dbg("mag data transfer init unhandled");
  208. ctx->Answer(Error_InvalidState);
  209. }
  210. }
  211. protected:
  212. private:
  213. };
  214. class MagDataTransferInitFinishedEvent : public FSMEvent
  215. {
  216. public:
  217. MagDataTransferInitFinishedEvent() : FSMEvent(USER_EVT_MAGDATA_TRANSFER_INIT_FINISHED){}
  218. ~MagDataTransferInitFinishedEvent(){}
  219. SpReqAnsContext<CardSwiperService_MagTransferInit_Req,CardSwiperService_MagTransferInit_Ans>::Pointer ctx;
  220. virtual void OnUnhandled()
  221. {
  222. if (ctx != NULL)
  223. {
  224. Dbg("card read unhandled");
  225. ctx->Answer(Error_InvalidState);
  226. }
  227. }
  228. protected:
  229. private:
  230. };
  231. class QueryFWBListEvent : public FSMEvent
  232. {
  233. public:
  234. QueryFWBListEvent() : FSMEvent(USER_EVT_QUERY_FWB_LIST) {}
  235. ~QueryFWBListEvent() {}
  236. SpReqAnsContext<CardSwiperService_QueryFWBList_Req, CardSwiperService_QueryFWBList_Ans>::Pointer ctx;
  237. virtual void OnUnhandled()
  238. {
  239. if (ctx != NULL)
  240. {
  241. Dbg("query fwb list unhandled");
  242. ctx->Answer(Error_InvalidState);
  243. }
  244. }
  245. };
  246. class BindFWBEvent : public FSMEvent
  247. {
  248. public:
  249. BindFWBEvent() : FSMEvent(USER_EVT_BIND_FWB) {}
  250. ~BindFWBEvent() {}
  251. SpReqAnsContext<CardSwiperService_BindFWB_Req, CardSwiperService_BindFWB_Ans>::Pointer ctx;
  252. virtual void OnUnhandled()
  253. {
  254. if (ctx != NULL)
  255. {
  256. Dbg("bind fwb unhandled");
  257. ctx->Answer(Error_InvalidState);
  258. }
  259. }
  260. };
  261. class CCardSwiperFSM : public CCommDevFSM<CCardSwiperFSM, CardSwiperClass>, public IFSMStateHooker
  262. {
  263. public:
  264. enum {s0,s1,s2,s3,s4,s5,s6,s7,s8,s9,s10,s11};
  265. BEGIN_FSM_STATE(CCardSwiperFSM)
  266. FSM_STATE_ENTRY(s0,"Init",s0_on_entry,s0_on_exit,s0_on_event)
  267. FSM_STATE_ENTRY(s1,"Initializing",s1_on_entry,s1_on_exit,s1_on_event)
  268. FSM_STATE_ENTRY(s2,"Idle",s2_on_entry,s2_on_exit,s2_on_event)
  269. FSM_STATE_ENTRY(s3,"Reading",s3_on_entry,s3_on_exit,s3_on_event)
  270. FSM_STATE_ENTRY(s4,"Hold",s4_on_entry,s4_on_exit,s4_on_event)
  271. FSM_STATE_ENTRY(s5,"Failed",s5_on_entry,s5_on_exit,s5_on_event)
  272. FSM_STATE_ENTRY(s6,"WaitingFetch",s6_on_entry,s6_on_exit,s6_on_event)
  273. FSM_STATE_ENTRY(s7,"MagTransInit",s7_on_entry,s7_on_exit,s7_on_event)
  274. FSM_STATE_ENTRY(s8,"DeviceOff",s8_on_entry,s8_on_exit,s8_on_event)
  275. FSM_STATE_ENTRY(s9,"WaitingRemove",s9_on_entry,s9_on_exit,s9_on_event)
  276. FSM_STATE_ENTRY(s10,"WaitingDie",s10_on_entry,s10_on_exit,s10_on_event)
  277. FSM_STATE_ENTRY(s11, "FWB", s11_on_entry, s11_on_exit, s11_on_event)
  278. END_FSM_STATE()
  279. BEGIN_FSM_RULE(CCardSwiperFSM,s0)
  280. FSM_RULE_ENTRY(s0,s1,USER_EVT_INIT,0)
  281. FSM_RULE_ENTRY(s0, FSM_STATE_EXIT, USER_EVT_QUIT, 0)
  282. FSM_RULE_ENTRY(s1,s2,USER_EVT_INITFINISHED,0)
  283. FSM_RULE_ENTRY(s1, s5, USER_EVT_INITFINISHED, 5)
  284. // --Josephus at 11:25:31 2016126
  285. FSM_RULE_ENTRY(s1,s8,USER_EVT_INITFINISHED,1)
  286. FSM_RULE_ENTRY(s1,s1, USER_EVT_INITFINISHED , 2)
  287. FSM_RULE_ENTRY(s1, FSM_STATE_EXIT, USER_EVT_QUIT, 0)
  288. FSM_RULE_ENTRY(s4, FSM_STATE_EXIT, USER_EVT_QUIT, 0)
  289. FSM_RULE_ENTRY(s2,s3,USER_EVT_READ,0)
  290. FSM_RULE_ENTRY(s2,s6,USER_EVT_EJECT,0)
  291. FSM_RULE_ENTRY(s2,s7,USER_EVT_MAGDATA_TRANSFER_INIT,0)
  292. FSM_RULE_ENTRY(s2, s11, USER_EVT_QUERY_FWB_LIST, 0)
  293. FSM_RULE_ENTRY(s2, s11, USER_EVT_BIND_FWB, 0)
  294. // --Josephus at 11:27:29 2016126
  295. FSM_RULE_ENTRY(s2, s8, USER_EVT_DEV_DISCONNECTED, 0)
  296. FSM_RULE_ENTRY(s3,s2,USER_EVT_READFINISHED,0)
  297. FSM_RULE_ENTRY(s3,s5,USER_EVT_READFINISHED,1)
  298. //FSM_RULE_ENTRY(s3,s4,USER_EVT_READFINISHED,2)
  299. FSM_RULE_ENTRY(s3, FSM_STATE_EXIT, USER_EVT_QUIT, 0)
  300. FSM_RULE_ENTRY(s3,s2,USER_EVT_EXIT,0)
  301. FSM_RULE_ENTRY(s5, s0, USER_EVT_RESETFINISHED, 0)
  302. FSM_RULE_ENTRY(s5, s5, USER_EVT_RESETFINISHED, 1)
  303. FSM_RULE_ENTRY(s5, FSM_STATE_EXIT, USER_EVT_QUIT, 0)
  304. FSM_RULE_ENTRY(s6,s2,USER_EVT_EJECTFINISHED,0)
  305. FSM_RULE_ENTRY(s6,s2,USER_EVT_EJECTFINISHED,1)
  306. FSM_RULE_ENTRY(s6,s2,USER_EVT_EJECTFINISHED,2)
  307. FSM_RULE_ENTRY(s7,s2,USER_EVT_MAGDATA_TRANSFER_INIT_FINISHED,0)
  308. FSM_RULE_ENTRY(s5, s8, USER_EVT_DEV_DISCONNECTED, 0)
  309. FSM_RULE_ENTRY(s8, s5, USER_EVT_DEV_CONNECTED, 0)
  310. FSM_RULE_ENTRY(s8, s10, USER_EVT_DEV_CONNECTED, 1)
  311. //oilyang@20200407 s8 no need to change FSM state
  312. //FSM_RULE_ENTRY(s8, s11, USER_EVT_QUERY_FWB_LIST, 0)
  313. //FSM_RULE_ENTRY(s8, s11, USER_EVT_BIND_FWB, 0)
  314. FSM_RULE_ENTRY(s8, s0, USER_EVT_BIND_FWB_FINISHED, 0)
  315. FSM_RULE_ENTRY(s8, s2, USER_EVT_BIND_FWB_FINISHED, 2)
  316. FSM_RULE_ENTRY(s8, FSM_STATE_EXIT, USER_EVT_QUIT, 0)
  317. FSM_RULE_ENTRY(s5, s9, USER_EVT_WAITREMOVE, 0)
  318. FSM_RULE_ENTRY(s9, s8, USER_EVT_DEV_DISCONNECTED, 0)
  319. FSM_RULE_ENTRY(s9, s3, USER_EVT_READ, 0)
  320. FSM_RULE_ENTRY(s9, s6, USER_EVT_EJECT,0)
  321. FSM_RULE_ENTRY(s9, s7, USER_EVT_MAGDATA_TRANSFER_INIT,0)
  322. FSM_RULE_ENTRY(s10, s5, USER_EVT_WAITTODIE_TIMEOUT, 0)
  323. FSM_RULE_ENTRY(s5, s10, USER_EVT_WAITTODIE, 0)
  324. FSM_RULE_ENTRY(s11, s2, USER_EVT_QUERY_FWB_LIST_FINISHED, 2)
  325. FSM_RULE_ENTRY(s11, s0, USER_EVT_BIND_FWB_FINISHED, 0)
  326. FSM_RULE_ENTRY(s11, s2, USER_EVT_BIND_FWB_FINISHED, 2)
  327. END_FSM_RULE()
  328. CCardSwiperFSM() : m_bCancelAccept(false),m_bWaitingAccept(false),
  329. m_bWaitAccepteMore(false),m_bExit(false),m_resetTimes(0),m_testResult(Error_Succeed)
  330. //,m_pTACReject(NULL),m_pIACOnline(NULL),m_pTACOnline(NULL),m_pIACDefault(NULL),m_pTACDefault(NULL)
  331. ,m_bCDA(false),m_pDataToARQC(NULL),m_bCancelRead(false),m_csVendor(""),m_csMachineType(""),m_csSite("")
  332. , m_bReading(false), m_bNeedInitCh(true), m_connStatus(0), m_connChecking(false), m_periodResetCount(0)
  333. ,m_dwPeriodResetTime(0), m_bCancelQueryConn(true), m_nLastSentMsg(-1), m_nReadFailCnt(0), m_nAAStatus(-1)
  334. , m_bResetInsertMore(false), m_csDevNo(""), m_bDoingBindFWB(false), m_bDevOpenEx(false),m_bBTScan(false)
  335. , m_bInMainPage(false),m_pubKey(nullptr)
  336. {
  337. HARDWARE_ENTITY_RESET_ENTITYID(m_entCode, 0x202);
  338. }
  339. ~CCardSwiperFSM(){}
  340. virtual ErrorCodeEnum OnInit();
  341. virtual ErrorCodeEnum OnExit();
  342. void s0_on_entry();
  343. void s0_on_exit();
  344. unsigned int s0_on_event(FSMEvent* event);
  345. void s1_on_entry();
  346. void s1_on_exit();
  347. unsigned int s1_on_event(FSMEvent* event);
  348. void s2_on_entry();
  349. void s2_on_exit();
  350. unsigned int s2_on_event(FSMEvent* event);
  351. void s3_on_entry();
  352. void s3_on_exit();
  353. unsigned int s3_on_event(FSMEvent* event);
  354. void s4_on_entry();
  355. void s4_on_exit();
  356. unsigned int s4_on_event(FSMEvent* event);
  357. void s5_on_entry();
  358. void s5_on_exit();
  359. unsigned int s5_on_event(FSMEvent* event);
  360. void s6_on_entry();
  361. void s6_on_exit();
  362. unsigned int s6_on_event(FSMEvent* event);
  363. void s7_on_entry();
  364. void s7_on_exit();
  365. unsigned int s7_on_event(FSMEvent* event);
  366. void s8_on_entry();
  367. void s8_on_exit();
  368. unsigned int s8_on_event(FSMEvent* event);
  369. void s9_on_entry();
  370. void s9_on_exit();
  371. unsigned int s9_on_event(FSMEvent* event);
  372. void s10_on_entry();
  373. void s10_on_exit();
  374. unsigned int s10_on_event(FSMEvent* event);
  375. void s11_on_entry();
  376. void s11_on_exit();
  377. unsigned int s11_on_event(FSMEvent* event);
  378. ErrorCodeEnum Load(CSimpleStringA csDevSN="");
  379. ErrorCodeEnum DataTransferInit();
  380. int Initial(CSimpleStringA csDevSN="",bool bInitChannel = true);
  381. bool GetDevStatus();
  382. /*
  383. -1: !Error_succeed
  384. 3: CancelRead
  385. 1: device failed
  386. 4: timeout
  387. 5: unknown or ID card. returned directly
  388. 6: rebuild channel.
  389. \*/
  390. int ReadCard(SpReqAnsContext<CardSwiperService_Read_Req,CardSwiperService_Read_Ans>::Pointer ctx);
  391. int PreOnline(SpReqAnsContext<CardSwiperService_PreOnline_Req,CardSwiperService_PreOnline_Ans>::Pointer ctx);
  392. int PostOnline(SpReqAnsContext<CardSwiperService_PostOnline_Req,CardSwiperService_PostOnline_Ans>::Pointer ctx);
  393. int QueryCardInfo(SpReqAnsContext<CardSwiperService_QueryCardInfo_Req,CardSwiperService_QueryCardInfo_Ans>::Pointer ctx);
  394. int Reset();
  395. int Eject();
  396. int MagTransInit(SpReqAnsContext<CardSwiperService_MagTransferInit_Req,CardSwiperService_MagTransferInit_Ans>::Pointer ctx);
  397. //oiltmp 测试南天新加密通道
  398. int TransInitEx(bool bPassvie = false);
  399. int QueryFWBList(SpReqAnsContext<CardSwiperService_QueryFWBList_Req, CardSwiperService_QueryFWBList_Ans>::Pointer ctx,int fsmState);
  400. int BindFWB(SpReqAnsContext<CardSwiperService_BindFWB_Req, CardSwiperService_BindFWB_Ans>::Pointer ctx, int fsmState);
  401. int DetectCardTypeInfoByAccount(char *pAccount, int len, char *pT3=NULL, int t3Len=0);
  402. void LoadCMBBin();
  403. void SetExitFlag(bool bFlag=true){m_bExit = bFlag;}
  404. //LPIDCCARDDATA* GetIDCData(){return lppCardData;}
  405. void SelfTest(EntityTestEnum eTestType,CSmartPointer<ITransactionContext> pTransactionContext);
  406. int QueryConnInfo(int nFlag = 0);
  407. int QueryConnStatus(){ return m_connStatus; }
  408. DevStateEnum GetDevState(){ return m_eDevState;}
  409. void SetMainPageFlag(bool bValue) { m_bInMainPage = bValue; }
  410. int DoAbortRead();
  411. int SplitDevModelInfo();
  412. char *m_pubKey;//oiltest
  413. bool IsReading()
  414. {
  415. return m_bReading;
  416. }
  417. bool IsNeedInitChannel()
  418. {
  419. return m_bNeedInitCh;
  420. }
  421. ErrorCodeEnum CommitSuicide();
  422. ErrorCodeEnum CheckPinPadStatus();
  423. ErrorCodeEnum CheckDevCtrlStatus();
  424. ErrorCodeEnum CheckIDCerStatus();
  425. ErrorCodeEnum ReStartRelateEntity(LPCTSTR szEntityName = "");
  426. void ShowFatalrForConnect(LPCTSTR szMessage, BOOL bFatal = TRUE)
  427. {
  428. static int nDisConnectTimes = 0;
  429. if(bFatal)
  430. {
  431. nDisConnectTimes++;
  432. GetEntityBase()->GetFunction()->ShowFatalError(szMessage);
  433. }
  434. else if(nDisConnectTimes != 0)
  435. {
  436. nDisConnectTimes = 0;
  437. GetEntityBase()->GetFunction()->ShowFatalError(szMessage);
  438. }
  439. return;
  440. }
  441. void ShowFatalrForTransferChannel(int nChannelType, BOOL bFatal = TRUE)
  442. {
  443. static int nFailBuiltTimes = 0;
  444. CSimpleStringA csMessage;
  445. if(bFatal)
  446. {
  447. csMessage = CSimpleStringA::Format("CardSwiper加密传输通道V%d建立失败!", nChannelType);
  448. if(nFailBuiltTimes == 0)
  449. {
  450. GetEntityBase()->GetFunction()->ShowFatalError((LPCTSTR)csMessage);
  451. LogWarn(Severity_Middle, Error_NotInit,
  452. nChannelType == 1 ? LOG_ERR_CARDSWIPER_BUILT_CHANNELV1_FAILED: LOG_ERR_CARDSWIPER_BUILT_CHANNELV2_FAILED,
  453. (LPCTSTR)csMessage);
  454. }
  455. else
  456. {
  457. LogWarn(Severity_Middle, Error_NotInit,
  458. nChannelType == 1 ? LOG_ERR_CARDSWIPER_BUILT_CHANNELV1_FAILED: LOG_ERR_CARDSWIPER_BUILT_CHANNELV2_FAILED,
  459. (LPCTSTR)csMessage);
  460. }
  461. nFailBuiltTimes++;
  462. }
  463. else if(nFailBuiltTimes != 0)
  464. {
  465. csMessage = CSimpleStringA::Format("CardSwiper加密传输通道V%d建立成功!", nChannelType);
  466. nFailBuiltTimes = 0;
  467. GetEntityBase()->GetFunction()->ShowFatalError((LPCTSTR)csMessage);
  468. }
  469. return;
  470. }
  471. //The list of value of nInforType:
  472. //0: device is disconnected.
  473. //1: device is connected and available.
  474. //2: device is recovering.
  475. //3: device is unavailable and tell AM to remove the device off pad and connect it again.
  476. //4: device is not ready, need to wait a moment.
  477. //5: the ReadCardType of Swiping Magnetic stripe card is unavailable (Built Transfer encrypted channel failed).
  478. //6: The PinPad is unavailable.
  479. //7: The entity is going to die, the business have better do nothing.
  480. //8: device is still unavailalble after removing and connecting action.
  481. //9: device is busing reading card.
  482. //10:clear all the notify components.
  483. void SendDevStatusMsg(int nInforType, bool bForce = false)
  484. {
  485. if(m_nLastSentMsg == nInforType && !bForce)
  486. {
  487. Dbg("Stop from sending msg %d.", nInforType);
  488. return;
  489. }
  490. if(nInforType == 10 && (m_nLastSentMsg == 2 || m_nLastSentMsg == 0))
  491. {
  492. Dbg("Needless to clear Lst: %d.", m_nLastSentMsg);
  493. return;
  494. }
  495. Dbg("================");
  496. switch (nInforType)
  497. {
  498. case 0:
  499. Dbg(" device is disconnected.");
  500. break;
  501. case 1:
  502. Dbg(" device is connected and available.");
  503. break;
  504. case 2:
  505. Dbg(" device is recovering.");
  506. break;
  507. case 3:
  508. Dbg(" device is unavailable and tell AM to remove the device off pad and connect it.");
  509. break;
  510. case 4:
  511. Dbg(" device is not ready, need to wait a moment.");
  512. break;
  513. case 5:
  514. Dbg(" the ReadCardType of Swiping Magnetic stripe card is unavailable (Built Transfer encrypted channel failed).");
  515. break;
  516. case 6:
  517. Dbg(" The PinPad is unavailable.");
  518. break;
  519. case 7:
  520. Dbg("The entity is going to die, the business have better do nothing.");
  521. break;
  522. case 8:
  523. Dbg(" device is still unavailalble after removing and connecting action.");
  524. break;
  525. case 9:
  526. Dbg("device is busing reading card.");
  527. nInforType = 1;
  528. Dbg("change Msg from 9 to 1 !!!");
  529. break;
  530. case 10:
  531. Dbg("clear all the notify components.");
  532. nInforType = 1;
  533. Dbg("change Msg from 10 to 1 !!!");
  534. break;
  535. default:
  536. Dbg(" Unknown information type %d.", nInforType);
  537. break;
  538. }
  539. m_nLastSentMsg = nInforType;
  540. Dbg("================");
  541. //if(nInforType == 0 || nInforType == 1)
  542. {
  543. Dbg("Really send msg %d.", nInforType);
  544. ConnectStatus evt;
  545. evt.status = nInforType;
  546. SpSendBroadcast(m_pEntity->GetFunction(), SP_MSG_OF(ConnectStatus),
  547. SP_MSG_SIG_OF(ConnectStatus), evt);
  548. }
  549. }
  550. void RecordRunCfg(CSimpleStringA csState);
  551. void OnStateTrans(int iSrcState, int iDstState)
  552. {
  553. Dbg("State changes from %s[%d] to %s[%d].",
  554. GetState(iSrcState)->pszName, iSrcState, GetState(iDstState)->pszName, iDstState);
  555. m_nLstSate = iSrcState;
  556. }
  557. ErrorCodeEnum GetAllInOneState(CSimpleStringA& csState)
  558. {
  559. csState = m_csAllInOneState;
  560. return Error_Succeed;
  561. }
  562. int GetAccessAuthState();
  563. //-1:初始状态 0: 准入成功, 1: 准入失败, 2: 未准入
  564. void SetAAState(int val);
  565. //-1:初始状态 0: 准入成功, 1: 准入失败, 2: 未准入
  566. int GetAAState()
  567. {
  568. if(m_nAAStatus == -1)
  569. {
  570. SetAAState(GetAccessAuthState());
  571. }
  572. return m_nAAStatus;
  573. }
  574. bool IsCryptMachinaType()
  575. {
  576. //oilyang@20200426 delete RVC.Desk2S
  577. if (_strnicmp(m_csMachineType, "RVC.PAD", strlen("RVC.PAD")) == 0 )
  578. //_strnicmp(m_csMachineType, "RVC.Desk2S", strlen("RVC.Desk2S")) == 0)
  579. return true;
  580. else
  581. return false;
  582. }
  583. void SetAcceptWaitMore(bool bValue);
  584. bool GetAcceptWaitMore() { return m_bWaitAccepteMore; };
  585. void InsertWaitMore();
  586. int FWBBluetoothScanTask();
  587. int nameToBthAddr(bool isSearchNearBy);//oiltest
  588. protected:
  589. //LPIDCCARDDATA* lppCardData;
  590. int m_iInsertTries;
  591. int m_resetTries;
  592. int m_ejectTries;
  593. private:
  594. //m_periodResetCount:一定时间内重置计数
  595. int m_resetTimes,m_testThreadCount,m_connStatus,m_periodResetCount;
  596. //m_dwPeriodResetTime:一定时间内第一次重启时间
  597. DWORD m_dwPeriodResetTime;
  598. lpCMBdecodeMag2 cmdDecodeMag2 = nullptr;
  599. lpCMBdecodeEx cmdDecodeEx = nullptr;
  600. bool m_devInit,m_bCancelAccept,m_bWaitingAccept,m_bWaitAccepteMore,m_bExit,m_bCancelRead
  601. , m_bSM, m_bOnlineOnly, m_bCDA, m_bReading,m_bNeedInitCh,m_connChecking,m_bResetInsertMore
  602. , m_bDoingBindFWB, m_bDevOpenEx, m_bBTScan, m_bInMainPage;
  603. ErrorCodeEnum m_testResult;
  604. bool m_bCancelQueryConn;
  605. int m_nLastSentMsg;
  606. CSimpleStringA m_csAllInOneState;
  607. int m_nLstSate;
  608. int m_nReadFailCnt;
  609. int m_nAAStatus;
  610. //int m_CardInit,m_CardRemains,m_CardIssued,m_CardCaptured,m_CardMixed,m_CardPercent;
  611. CardSwiperStatus devStatus;
  612. DevStateEnum m_eDevState;
  613. CCardProcess *m_pCardProcess;
  614. CardReadType m_eReadType;
  615. // CardTypeEnum m_cardType;
  616. //about IC
  617. //BYTE m_CPUType,m_AIP[2],m_appVersion[2],m_TSI[2],m_TVR[5],m_IACReject[5],m_IACOnline[5],m_IACDefault[5]
  618. //,m_P1,m_randData[4],m_CVR[4];
  619. //LPBYTE m_pTACReject,m_pIACOnline,m_pTACOnline,m_pIACDefault,m_pTACDefault;
  620. char *m_pDataToARQC;
  621. long xxTest;
  622. CSimpleStringA m_csVendor,m_csMachineType,m_csSite,m_csCM,m_csPM,m_csDevNo;
  623. WORD m_majorVerion, m_minorVerion;
  624. int m_version,m_batch;
  625. int m_nSplitRes;
  626. //BYTE m_APDUsendBuf[1024];
  627. //int m_lenAPDU;
  628. char m_AuthCode[2];
  629. //bool m_bOnlineOnly,m_bCDA;
  630. //ICData gpoData;
  631. //vector<ICData> m_vICData,m_vBusData;
  632. //vector<ADFRecord> m_vADFRec;
  633. vector<CMBBin> m_vBin;
  634. BYTE m_r1[ONE_K / 8], m_r2[ONE_K / 8], m_r3[ONE_K / 8], m_priKey[ONE_K / 2], m_devPubKey[ONE_K / 2];
  635. vector<FWBRecord> m_vFWBRecords;
  636. int SplitTrack2(CSimpleStringA pTrack2,Track2Data &decodeData);
  637. //deprecated: please replace it with 'LogErrMsg' [4/1/2020 10:51 @Gifur]
  638. //void LogErrInfo(const char*);
  639. void SwitchDataForSave(bool bSet,char *&pData);
  640. //deprecated: please replace it with 'LogErrMsg' [4/1/2020 11:03 @Gifur]
  641. //void LogDevErrorInfo();
  642. int ProcessEnChannel(bool bIgnore = false);
  643. int IsValidFWBName(const char* pName, int len);
  644. ErrorCodeEnum AfterBindUpdateFWBNameToRunCfg(const char* name, bool bBind);
  645. void AddToFWBRecords(const char* name, const char* remoteMac, bool bFirst);
  646. void ClearFWBRecords();
  647. int ScanNearbyBTDevice();
  648. void ClearHandleOjbects();
  649. CSimpleStringA TransferAboutVendorShortName(CSimpleStringA name);
  650. DevCategoryInfo m_devCat;
  651. };
  652. struct InitTask : public ITaskSp
  653. {
  654. CCardSwiperFSM* fsm;
  655. InitTask(CCardSwiperFSM* f) : fsm(f) {}
  656. void Process()
  657. {
  658. FSMEvent *e = new FSMEvent(USER_EVT_INITFINISHED);
  659. e->param1 = fsm->Initial();
  660. fsm->PostEventFIFO(e);
  661. }
  662. };
  663. struct ReadTask : public ITaskSp
  664. {
  665. SpReqAnsContext<CardSwiperService_Read_Req,CardSwiperService_Read_Ans>::Pointer ctx;
  666. CCardSwiperFSM* fsm;
  667. ReadTask(CCardSwiperFSM* f) : fsm(f) {}
  668. void Process()
  669. {
  670. fsm->SetExitFlag(false);
  671. //fsm->SendDevStatusMsg(9);
  672. int tmpChannelTest = fsm->IsNeedInitChannel()? 1 : 0;
  673. if(tmpChannelTest)
  674. {
  675. CSimpleStringA csState = "";
  676. fsm->GetAllInOneState(csState);
  677. if(csState.IsStartWith("R", true))
  678. {
  679. fsm->SendDevStatusMsg(5);
  680. }
  681. }
  682. CardReadFinishedEvent *e = new CardReadFinishedEvent();
  683. e->ctx = ctx;
  684. e->param1 = fsm->ReadCard(ctx);
  685. e->param2 = tmpChannelTest;
  686. fsm->PostEventFIFO(e);
  687. }
  688. };
  689. struct CancelReadTask : public ITaskSp
  690. {
  691. CCardSwiperFSM* fsm;
  692. CancelReadTask(CCardSwiperFSM* f) : fsm(f) {}
  693. void Process()
  694. {
  695. int nRes = fsm->DoAbortRead();
  696. if(nRes != -1)
  697. {
  698. int checkTimes = 0;
  699. int checkMaxTimes = 5;
  700. for(; checkTimes < checkMaxTimes; ++checkTimes)
  701. {
  702. if(!fsm->IsReading()) break;
  703. Sleep(1000);
  704. }
  705. if(checkTimes >= checkMaxTimes)
  706. {
  707. LogWarn(Severity_Middle, Error_Unexpect,
  708. CardSwiper_UserErrorCode_Wait_Too_Much_Time_After_AbortRead,"Wait too much time after AbortRead op.");
  709. }
  710. }
  711. }
  712. };
  713. struct ExitTask : public ITaskSp
  714. {
  715. CCardSwiperFSM* fsm;
  716. ExitTask(CCardSwiperFSM* f) : fsm(f) {}
  717. void Process()
  718. {
  719. fsm->DoAbortRead();
  720. }
  721. };
  722. struct PreOnlineTask : public ITaskSp
  723. {
  724. SpReqAnsContext<CardSwiperService_PreOnline_Req,CardSwiperService_PreOnline_Ans>::Pointer ctx;
  725. CCardSwiperFSM* fsm;
  726. PreOnlineTask(CCardSwiperFSM* f) : fsm(f) {}
  727. void Process()
  728. {
  729. FSMEvent *e = new FSMEvent(USER_EVT_PREONLINEFINISHED);
  730. e->param1 = fsm->PreOnline(ctx);
  731. fsm->PostEventFIFO(e);
  732. }
  733. };
  734. struct PostOnlineTask : public ITaskSp
  735. {
  736. SpReqAnsContext<CardSwiperService_PostOnline_Req,CardSwiperService_PostOnline_Ans>::Pointer ctx;
  737. CCardSwiperFSM* fsm;
  738. PostOnlineTask(CCardSwiperFSM* f) : fsm(f) {}
  739. void Process()
  740. {
  741. FSMEvent *e = new FSMEvent(USER_EVT_POSTONLINEFINISHED);
  742. e->param1 = fsm->PostOnline(ctx);
  743. fsm->PostEventFIFO(e);
  744. }
  745. };
  746. struct ResetTask : public ITaskSp
  747. {
  748. CCardSwiperFSM* fsm;
  749. ResetTask(CCardSwiperFSM* f) : fsm(f) {}
  750. void Process()
  751. {
  752. ResetFinishedEvent *e = new ResetFinishedEvent();
  753. e->param1 = fsm->Reset();
  754. fsm->PostEventFIFO(e);
  755. }
  756. };
  757. struct EjectTask : public ITaskSp
  758. {
  759. CCardSwiperFSM* fsm;
  760. EjectTask(CCardSwiperFSM* f) : fsm(f) {}
  761. void Process()
  762. {
  763. FSMEvent *pEvt = new FSMEvent(USER_EVT_EJECTFINISHED);
  764. pEvt->param1 = fsm->Eject();
  765. fsm->PostEventFIFO(pEvt);
  766. }
  767. };
  768. struct QueryCardInfoTask : public ITaskSp
  769. {
  770. SpReqAnsContext<CardSwiperService_QueryCardInfo_Req,CardSwiperService_QueryCardInfo_Ans>::Pointer ctx;
  771. CCardSwiperFSM* fsm;
  772. QueryCardInfoTask(CCardSwiperFSM* f) : fsm(f) {}
  773. void Process()
  774. {
  775. FSMEvent *e = new FSMEvent(USER_EVT_QUERY_CARD_INFO_FINISHED);
  776. e->param1 = fsm->QueryCardInfo(ctx);
  777. fsm->PostEventFIFO(e);
  778. }
  779. };
  780. struct MagTransInitTask : public ITaskSp
  781. {
  782. SpReqAnsContext<CardSwiperService_MagTransferInit_Req,CardSwiperService_MagTransferInit_Ans>::Pointer ctx;
  783. CCardSwiperFSM* fsm;
  784. MagTransInitTask(CCardSwiperFSM* f) : fsm(f) {}
  785. void Process()
  786. {
  787. MagDataTransferInitFinishedEvent *e = new MagDataTransferInitFinishedEvent();
  788. e->ctx = ctx;
  789. e->param1 = fsm->MagTransInit(ctx);
  790. fsm->PostEventFIFO(e);
  791. }
  792. };
  793. struct MagTransInitV2Task : public ITaskSp
  794. {
  795. CCardSwiperFSM* fsm;
  796. MagTransInitV2Task(CCardSwiperFSM* f) : fsm(f) {}
  797. void Process()
  798. {
  799. FSMEvent *e = new FSMEvent(USER_EVT_MAGDATA_TRANSFER_INIT_V2_FINISHED);
  800. e->param1 = fsm->TransInitEx(true);
  801. fsm->PostEventFIFO(e);
  802. }
  803. };
  804. struct QueryConnectTask : public ITaskSp
  805. {
  806. CCardSwiperFSM* fsm;
  807. int nReserved;
  808. QueryConnectTask(CCardSwiperFSM* f) : fsm(f), nReserved(0) {}
  809. void Process()
  810. {
  811. Dbg("Start listerning devcic connect status.");
  812. fsm->QueryConnInfo(nReserved);
  813. }
  814. };
  815. struct InsertWaitMoreTask : public ITaskSp
  816. {
  817. CCardSwiperFSM* fsm;
  818. InsertWaitMoreTask(CCardSwiperFSM* f) : fsm(f){}
  819. void Process()
  820. {
  821. fsm->InsertWaitMore();
  822. }
  823. };
  824. struct QueryFWBListTask : public ITaskSp
  825. {
  826. CCardSwiperFSM* fsm;
  827. SpReqAnsContext<CardSwiperService_QueryFWBList_Req, CardSwiperService_QueryFWBList_Ans>::Pointer ctx;
  828. int fsmState;
  829. QueryFWBListTask(CCardSwiperFSM* f) : fsm(f) {}
  830. void Process()
  831. {
  832. FSMEvent* e = new FSMEvent(USER_EVT_QUERY_FWB_LIST_FINISHED);
  833. e->param1 = fsm->QueryFWBList(ctx, fsmState);
  834. fsm->PostEventFIFO(e);
  835. }
  836. };
  837. struct BindFWBTask : public ITaskSp
  838. {
  839. CCardSwiperFSM* fsm;
  840. SpReqAnsContext<CardSwiperService_BindFWB_Req, CardSwiperService_BindFWB_Ans>::Pointer ctx;
  841. int fsmState;
  842. BindFWBTask(CCardSwiperFSM* f) : fsm(f) {}
  843. void Process()
  844. {
  845. FSMEvent* e = new FSMEvent(USER_EVT_BIND_FWB_FINISHED);
  846. e->param1 = fsm->BindFWB(ctx, fsmState);
  847. fsm->PostEventFIFO(e);
  848. }
  849. };
  850. struct FWBBluetoothScanTask : public ITaskSp
  851. {
  852. CCardSwiperFSM* fsm;
  853. FWBBluetoothScanTask(CCardSwiperFSM* f) : fsm(f) {}
  854. void Process()
  855. {
  856. fsm->FWBBluetoothScanTask();
  857. }
  858. };
  859. #endif //__CARD_SWIPER_FSM_H