CardSwiperFSM.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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 "EventCode.h"
  7. #include "CardSwiper_UserErrorCode.h"
  8. #include "CardSwiper_server_g.h"
  9. #include "CardSwiper_msg_g.h"
  10. #include "CardSwiperClass.h"
  11. #include "DevFSMCommBase.hpp"
  12. #include "CardAssist.h"
  13. #include "ICDataElement.h"
  14. #include "json/json.h"
  15. #include "AccessAuthorization_client_g.h"
  16. #include "PinPad_client_g.h"
  17. #include "HealthManager_client_g.h"
  18. #include "HealthManager_def_g.h"
  19. #include "SelfChecker_client_g.h"
  20. #include "SelfChecker_def_g.h"
  21. using namespace SelfChecker;
  22. using namespace HealthManager;
  23. using namespace PinPad;
  24. using namespace CardSwiper;
  25. using namespace AccessAuthorization;
  26. const int ONE_K = 1024;
  27. enum EvtType
  28. {
  29. USER_EVT_TEST = EVT_USER+1,
  30. USER_EVT_QUIT,
  31. USER_EVT_INIT,
  32. USER_EVT_INITFINISHED,
  33. USER_EVT_ACCEPT,
  34. USER_EVT_ACCEPT_CANCEL,
  35. USER_EVT_ACCEPTFINISHED,
  36. USER_EVT_ISSUE,
  37. USER_EVT_ISSUE_FINISHED,
  38. USER_EVT_READ,
  39. USER_EVT_EJECT,
  40. USER_EVT_CAPTURE,
  41. USER_EVT_READFINISHED,
  42. USER_EVT_EJECTFINISHED,
  43. USER_EVT_WAITFINISHED,
  44. USER_EVT_CAPTUREFINISHED,
  45. USER_EVT_WAITFETCHINGTIMEOUT,
  46. USER_EVT_PREONLINE,
  47. USER_EVT_POSTONLINE,
  48. USER_EVT_PREONLINEFINISHED,
  49. USER_EVT_POSTONLINEFINISHED,
  50. USER_EVT_EXIT,
  51. USER_EVT_CANCELINSERT,
  52. USER_EVT_QUERY_CARD_INFO,
  53. USER_EVT_QUERY_CARD_INFO_FINISHED,
  54. USER_EVT_MAGDATA_TRANSFER_INIT_V2,
  55. USER_EVT_MAGDATA_TRANSFER_INIT_V2_FINISHED,
  56. USER_EVT_DEV_DISCONNECTED,
  57. USER_EVT_DEV_CONNECTED,
  58. USER_EVT_ENCRYPTED_CHANNEL,
  59. USER_EVT_QUERY_FWB_LIST,
  60. USER_EVT_QUERY_FWB_LIST_FINISHED,
  61. USER_EVT_BIND_FWB,
  62. USER_EVT_BIND_FWB_FINISHED,
  63. USER_EVT_NO_BIND_FWB,
  64. };
  65. struct FWBRecord
  66. {
  67. CSimpleStringA name;
  68. CSimpleStringA remoteMac;
  69. };
  70. class SelfChekerClient:public SelfCheckerService_ClientBase
  71. {
  72. public:
  73. SelfChekerClient(CEntityBase* pEntity);
  74. };
  75. class HealthMngClient : public HealthManagerService_ClientBase
  76. {
  77. public:
  78. HealthMngClient(CEntityBase* pEntity);
  79. };
  80. typedef int(*lpCMBdecodeMag2)(char* Mag2Data, char * data);
  81. typedef int(*lpCMBdecodeEx)( char* MagData, char * type, char * data);
  82. typedef ErrorCodeEnum (*lpCreateDevCom)(DeviceBaseClass *&baseObj);
  83. typedef ErrorCodeEnum (*lpReleaseDevCom)(DeviceBaseClass *&pBaseObj);
  84. class CCardSwiperEntity;
  85. class CCardSwiperFSM;
  86. struct Track2Data
  87. {
  88. int status;
  89. CSimpleStringA t2Account;
  90. CSimpleStringA t2Region;
  91. CSimpleStringA t2CardSerial;
  92. CSimpleStringA t2CVC;
  93. CSimpleStringA t2ExpireDate;
  94. };
  95. class CardReadEvent : public FSMEvent
  96. {
  97. public:
  98. CardReadEvent() : FSMEvent(USER_EVT_READ){}
  99. ~CardReadEvent(){}
  100. SpReqAnsContext<CardSwiperService_Read_Req,CardSwiperService_Read_Ans>::Pointer ctx;
  101. virtual void OnUnhandled()
  102. {
  103. if (ctx != NULL)
  104. {
  105. DbgWarn("card read unhandled");
  106. ctx->Answer(Error_InvalidState);
  107. }
  108. }
  109. protected:
  110. private:
  111. };
  112. class CardReadFinishedEvent : public FSMEvent
  113. {
  114. public:
  115. CardReadFinishedEvent() : FSMEvent(USER_EVT_READFINISHED){}
  116. ~CardReadFinishedEvent(){}
  117. SpReqAnsContext<CardSwiperService_Read_Req,CardSwiperService_Read_Ans>::Pointer ctx;
  118. virtual void OnUnhandled()
  119. {
  120. if (ctx != NULL)
  121. {
  122. DbgWarn("card read unhandled");
  123. //ctx->Answer(Error_InvalidState);
  124. }
  125. }
  126. protected:
  127. private:
  128. };
  129. class PreOnlineEvent : public FSMEvent
  130. {
  131. public:
  132. PreOnlineEvent() : FSMEvent(USER_EVT_PREONLINE){}
  133. ~PreOnlineEvent(){}
  134. SpReqAnsContext<CardSwiperService_PreOnline_Req,CardSwiperService_PreOnline_Ans>::Pointer ctx;
  135. virtual void OnUnhandled()
  136. {
  137. if (ctx != NULL)
  138. ctx->Answer(Error_InvalidState);
  139. }
  140. protected:
  141. private:
  142. };
  143. class PostOnlineEvent : public FSMEvent
  144. {
  145. public:
  146. PostOnlineEvent() : FSMEvent(USER_EVT_POSTONLINE){}
  147. ~PostOnlineEvent(){}
  148. SpReqAnsContext<CardSwiperService_PostOnline_Req,CardSwiperService_PostOnline_Ans>::Pointer ctx;
  149. virtual void OnUnhandled()
  150. {
  151. if (ctx != NULL)
  152. ctx->Answer(Error_InvalidState);
  153. }
  154. protected:
  155. private:
  156. };
  157. class QueryCardInfoEvent : public FSMEvent
  158. {
  159. public:
  160. QueryCardInfoEvent() : FSMEvent(USER_EVT_QUERY_CARD_INFO){}
  161. ~QueryCardInfoEvent(){}
  162. SpReqAnsContext<CardSwiperService_QueryCardInfo_Req,CardSwiperService_QueryCardInfo_Ans>::Pointer ctx;
  163. virtual void OnUnhandled()
  164. {
  165. if (ctx != NULL)
  166. ctx->Answer(Error_InvalidState);
  167. }
  168. protected:
  169. private:
  170. };
  171. class QueryFWBListEvent : public FSMEvent
  172. {
  173. public:
  174. QueryFWBListEvent() : FSMEvent(USER_EVT_QUERY_FWB_LIST) {}
  175. ~QueryFWBListEvent() {}
  176. SpReqAnsContext<CardSwiperService_QueryFWBList_Req, CardSwiperService_QueryFWBList_Ans>::Pointer ctx;
  177. virtual void OnUnhandled()
  178. {
  179. if (ctx != NULL)
  180. {
  181. DbgWarn("query fwb list unhandled");
  182. ctx->Answer(Error_InvalidState);
  183. }
  184. }
  185. };
  186. class BindFWBEvent : public FSMEvent
  187. {
  188. public:
  189. BindFWBEvent() : FSMEvent(USER_EVT_BIND_FWB) {}
  190. ~BindFWBEvent() {}
  191. SpReqAnsContext<CardSwiperService_BindFWB_Req, CardSwiperService_BindFWB_Ans>::Pointer ctx;
  192. virtual void OnUnhandled()
  193. {
  194. if (ctx != NULL)
  195. {
  196. DbgWarn("bind fwb unhandled");
  197. ctx->Answer(Error_InvalidState);
  198. }
  199. }
  200. };
  201. class CCardSwiperFSM : public CCommDevFSM<CCardSwiperFSM, CardSwiperClass>, public IFSMStateHooker
  202. {
  203. public:
  204. enum {s0,s1,s2,s3,s4,s5,s6,s7,s8};
  205. BEGIN_FSM_STATE(CCardSwiperFSM)
  206. FSM_STATE_ENTRY(s0,"Init",s0_on_entry,s0_on_exit,s0_on_event)
  207. FSM_STATE_ENTRY(s1,"Initializing",s1_on_entry,s1_on_exit,s1_on_event)
  208. FSM_STATE_ENTRY(s2,"Idle",s2_on_entry,s2_on_exit,s2_on_event)
  209. FSM_STATE_ENTRY(s3,"Reading",s3_on_entry,s3_on_exit,s3_on_event)
  210. FSM_STATE_ENTRY(s4,"Hold",s4_on_entry,s4_on_exit,s4_on_event)
  211. FSM_STATE_ENTRY(s5,"Failed",s5_on_entry,s5_on_exit,s5_on_event)
  212. FSM_STATE_ENTRY(s6,"WaitingFetch",s6_on_entry,s6_on_exit,s6_on_event)
  213. FSM_STATE_ENTRY(s7,"DeviceOff",s7_on_entry,s7_on_exit,s7_on_event)
  214. FSM_STATE_ENTRY(s8, "FWB", s8_on_entry, s8_on_exit, s8_on_event)
  215. END_FSM_STATE()
  216. BEGIN_FSM_RULE(CCardSwiperFSM,s0)
  217. FSM_RULE_ENTRY(s0,s1,USER_EVT_INIT,0)
  218. FSM_RULE_ENTRY(s0, FSM_STATE_EXIT, USER_EVT_QUIT, 0)
  219. FSM_RULE_ENTRY(s1,s2,USER_EVT_INITFINISHED,0)
  220. FSM_RULE_ENTRY(s1, s5, USER_EVT_INITFINISHED, 5)
  221. // --Josephus at 11:25:31 2016126
  222. FSM_RULE_ENTRY(s1,s7,USER_EVT_INITFINISHED,1)
  223. FSM_RULE_ENTRY(s1,s1, USER_EVT_INITFINISHED , 2)
  224. FSM_RULE_ENTRY(s1, FSM_STATE_EXIT, USER_EVT_QUIT, 0)
  225. FSM_RULE_ENTRY(s4, FSM_STATE_EXIT, USER_EVT_QUIT, 0)
  226. FSM_RULE_ENTRY(s2,s3,USER_EVT_READ,0)
  227. FSM_RULE_ENTRY(s2,s6,USER_EVT_EJECT,0)
  228. FSM_RULE_ENTRY(s2, s8, USER_EVT_QUERY_FWB_LIST, 0)
  229. FSM_RULE_ENTRY(s2, s8, USER_EVT_BIND_FWB, 0)
  230. // --Josephus at 11:27:29 2016126
  231. FSM_RULE_ENTRY(s2, s7, USER_EVT_DEV_DISCONNECTED, 0)
  232. FSM_RULE_ENTRY(s3,s2,USER_EVT_READFINISHED,0)
  233. FSM_RULE_ENTRY(s3,s5,USER_EVT_READFINISHED,1)
  234. FSM_RULE_ENTRY(s3, FSM_STATE_EXIT, USER_EVT_QUIT, 0)
  235. FSM_RULE_ENTRY(s3,s2,USER_EVT_EXIT,0)
  236. FSM_RULE_ENTRY(s5, FSM_STATE_EXIT, USER_EVT_QUIT, 0)
  237. FSM_RULE_ENTRY(s5, s0, USER_EVT_INIT, 0)
  238. FSM_RULE_ENTRY(s5, s7, USER_EVT_DEV_DISCONNECTED, 0)
  239. FSM_RULE_ENTRY(s6,s2,USER_EVT_EJECTFINISHED,0)
  240. FSM_RULE_ENTRY(s6,s2,USER_EVT_EJECTFINISHED,1)
  241. FSM_RULE_ENTRY(s6,s2,USER_EVT_EJECTFINISHED,2)
  242. FSM_RULE_ENTRY(s7, s5, USER_EVT_DEV_CONNECTED, 0)
  243. FSM_RULE_ENTRY(s7, s0, USER_EVT_BIND_FWB_FINISHED, 0)
  244. FSM_RULE_ENTRY(s7, s2, USER_EVT_BIND_FWB_FINISHED, 2)
  245. FSM_RULE_ENTRY(s7, FSM_STATE_EXIT, USER_EVT_QUIT, 0)
  246. FSM_RULE_ENTRY(s7, s0, USER_EVT_INIT, 0)
  247. FSM_RULE_ENTRY(s8, s2, USER_EVT_QUERY_FWB_LIST_FINISHED, 2)
  248. FSM_RULE_ENTRY(s8, s0, USER_EVT_BIND_FWB_FINISHED, 0)
  249. FSM_RULE_ENTRY(s8, s2, USER_EVT_BIND_FWB_FINISHED, 2)
  250. END_FSM_RULE()
  251. CCardSwiperFSM() : m_bCancelAccept(false),m_bWaitingAccept(false),
  252. m_bWaitAccepteMore(false),m_bExit(false),m_resetTimes(0),m_testResult(Error_Succeed), m_pSelfcheckClient(NULL)
  253. ,m_bCDA(false),m_pDataToARQC(NULL),m_bCancelRead(false)
  254. , m_bReading(false), m_bNeedInitCh(true), m_connStatus(0), m_connChecking(false), m_periodResetCount(0)
  255. ,m_dwPeriodResetTime(0), m_bCancelQueryConn(true), m_nLastSentMsg(-1), m_nReadFailCnt(0), m_nAAStatus(-1)
  256. , m_bResetInsertMore(false), m_csDevNo(""), m_bDoingBindFWB(false), m_bCallingDevOpenEx(false),m_bBTScan(false)
  257. , m_bInMainPage(false), m_batteryLeft(999), m_bFWBOpenSucEver(false), m_isFWB(false), m_timesToTryOpen(3)
  258. {
  259. HARDWARE_ENTITY_RESET_ENTITYID(m_entCode, 0x202);
  260. m_aidList.Init(3);
  261. m_aidList[0] = "A000000333";
  262. m_aidList[1] = "A0000000108888";
  263. m_aidList[2] = "A000000790";
  264. }
  265. ~CCardSwiperFSM(){}
  266. virtual ErrorCodeEnum OnInit();
  267. virtual ErrorCodeEnum OnExit();
  268. void s0_on_entry();
  269. void s0_on_exit();
  270. unsigned int s0_on_event(FSMEvent* event);
  271. void s1_on_entry();
  272. void s1_on_exit();
  273. unsigned int s1_on_event(FSMEvent* event);
  274. void s2_on_entry();
  275. void s2_on_exit();
  276. unsigned int s2_on_event(FSMEvent* event);
  277. void s3_on_entry();
  278. void s3_on_exit();
  279. unsigned int s3_on_event(FSMEvent* event);
  280. void s4_on_entry();
  281. void s4_on_exit();
  282. unsigned int s4_on_event(FSMEvent* event);
  283. void s5_on_entry();
  284. void s5_on_exit();
  285. unsigned int s5_on_event(FSMEvent* event);
  286. void s6_on_entry();
  287. void s6_on_exit();
  288. unsigned int s6_on_event(FSMEvent* event);
  289. void s7_on_entry();
  290. void s7_on_exit();
  291. unsigned int s7_on_event(FSMEvent* event);
  292. void s8_on_entry();
  293. void s8_on_exit();
  294. unsigned int s8_on_event(FSMEvent* event);
  295. ErrorCodeEnum Load(CSimpleStringA csDevSN="");
  296. int Initial(CSimpleStringA csDevSN="",bool bInitChannel = true);
  297. bool GetDevStatus();
  298. int UnAcceptCard();
  299. int WaitFetchingCard();
  300. /*
  301. -1: !Error_succeed
  302. 3: CancelRead
  303. 1: device failed
  304. 4: timeout
  305. 5: unknown or ID card. returned directly
  306. 6: rebuild channel.
  307. \*/
  308. int ReadCard(SpReqAnsContext<CardSwiperService_Read_Req,CardSwiperService_Read_Ans>::Pointer ctx);
  309. int PreOnline(SpReqAnsContext<CardSwiperService_PreOnline_Req,CardSwiperService_PreOnline_Ans>::Pointer ctx);
  310. int PostOnline(SpReqAnsContext<CardSwiperService_PostOnline_Req,CardSwiperService_PostOnline_Ans>::Pointer ctx);
  311. int QueryCardInfo(SpReqAnsContext<CardSwiperService_QueryCardInfo_Req,CardSwiperService_QueryCardInfo_Ans>::Pointer ctx);
  312. int Eject();
  313. int InternalAcceptCard();
  314. //oiltmp 测试南天新加密通道
  315. int TransInitEx(bool bPassvie = false);
  316. int QueryFWBList(SpReqAnsContext<CardSwiperService_QueryFWBList_Req, CardSwiperService_QueryFWBList_Ans>::Pointer ctx,int fsmState);
  317. int BindFWB(SpReqAnsContext<CardSwiperService_BindFWB_Req, CardSwiperService_BindFWB_Ans>::Pointer ctx, int fsmState);
  318. void SetExitFlag(bool bFlag=true){m_bExit = bFlag;}
  319. //LPIDCCARDDATA* GetIDCData(){return lppCardData;}
  320. void SelfTest(EntityTestEnum eTestType,CSmartPointer<ITransactionContext> pTransactionContext);
  321. int QueryConnInfo(int nFlag = 0);
  322. int QueryConnStatus(){ return m_connStatus; }
  323. DevStateEnum GetDevState(){ return m_eDevState;}
  324. bool GetDevInitFlag() { return m_bOpened; }
  325. void SetMainPageFlag(bool bValue)
  326. {
  327. DbgInfo(CSimpleStringA::Format("SetMainPageFlag:%d", bValue));
  328. m_bInMainPage = bValue;
  329. }
  330. int DoAbortRead();
  331. int SplitDevModelInfo();
  332. char *m_pubKey;//oiltest
  333. bool IsReading()
  334. {
  335. return m_bReading;
  336. }
  337. bool IsNeedInitChannel()
  338. {
  339. return m_bNeedInitCh;
  340. }
  341. void ShowFatalrForConnect(LPCTSTR szMessage, BOOL bFatal = TRUE)
  342. {
  343. static int nDisConnectTimes = 0;
  344. if(bFatal)
  345. {
  346. nDisConnectTimes++;
  347. //GetEntityBase()->GetFunction()->ShowFatalError(szMessage);
  348. }
  349. else if(nDisConnectTimes != 0)
  350. {
  351. nDisConnectTimes = 0;
  352. //GetEntityBase()->GetFunction()->ShowFatalError(szMessage);
  353. }
  354. return;
  355. }
  356. void ShowFatalrForTransferChannel(int nChannelType, BOOL bFatal = TRUE)
  357. {
  358. static int nFailBuiltTimes = 0;
  359. CSimpleStringA csMessage;
  360. if(bFatal)
  361. {
  362. csMessage = CSimpleStringA::Format("CardSwiper加密传输通道V%d建立失败!", nChannelType);
  363. if(nFailBuiltTimes == 0)
  364. {
  365. //GetEntityBase()->GetFunction()->ShowFatalError((LPCTSTR)csMessage);
  366. //LogWarn(Severity_Middle, Error_NotInit,
  367. // nChannelType == 1 ? LOG_ERR_CARDSWIPER_BUILT_CHANNELV1_FAILED: LOG_ERR_CARDSWIPER_BUILT_CHANNELV2_FAILED,
  368. // (LPCTSTR)csMessage);
  369. }
  370. else
  371. {
  372. //LogWarn(Severity_Middle, Error_NotInit,
  373. // nChannelType == 1 ? LOG_ERR_CARDSWIPER_BUILT_CHANNELV1_FAILED: LOG_ERR_CARDSWIPER_BUILT_CHANNELV2_FAILED,
  374. // (LPCTSTR)csMessage);
  375. }
  376. //oilyang@20201015 no need to LogWarn repeatablility,as other LogWarn have contain the error.
  377. //only log the message
  378. LogWarn(Severity_Middle, Error_Unexpect, CardSwiper_UserErrorCode_Build_Channel_Failed, csMessage.GetData());
  379. nFailBuiltTimes++;
  380. }
  381. else if(nFailBuiltTimes != 0)
  382. {
  383. csMessage = CSimpleStringA::Format("CardSwiper加密传输通道V%d建立成功!", nChannelType);
  384. nFailBuiltTimes = 0;
  385. //GetEntityBase()->GetFunction()->ShowFatalError((LPCTSTR)csMessage);
  386. }
  387. return;
  388. }
  389. //The list of value of nInforType:
  390. //0: device is disconnected.
  391. //1: device is connected and available.
  392. //2: device is recovering.
  393. //3: device is unavailable and tell AM to remove the device off pad and connect it again.
  394. //4: device is not ready, need to wait a moment.
  395. //5: the ReadCardType of Swiping Magnetic stripe card is unavailable (Built Transfer encrypted channel failed).
  396. //6: The PinPad is unavailable.
  397. //7: The entity is going to die, the business have better do nothing.
  398. //8: device is still unavailalble after removing and connecting action.
  399. //9: device is busing reading card.
  400. //10:clear all the notify components.
  401. void SendDevStatusMsg(int nInforType, bool bForce = false)
  402. {
  403. if(m_nLastSentMsg == nInforType && !bForce)
  404. {
  405. DbgInfo(CSimpleStringA::Format("Stop from sending msg %d.", nInforType));
  406. return;
  407. }
  408. if(nInforType == 10 && (m_nLastSentMsg == 2 || m_nLastSentMsg == 0))
  409. {
  410. DbgInfo(CSimpleStringA::Format("Needless to clear Lst: %d.", m_nLastSentMsg));
  411. return;
  412. }
  413. switch (nInforType)
  414. {
  415. case 0:
  416. DbgInfo(" device is disconnected.");
  417. break;
  418. case 1:
  419. DbgInfo(" device is connected and available.");
  420. break;
  421. case 2:
  422. DbgInfo(" device is recovering.");
  423. break;
  424. case 3:
  425. DbgInfo(" device is unavailable and tell AM to remove the device off pad and connect it.");
  426. break;
  427. case 4:
  428. DbgInfo(" device is not ready, need to wait a moment.");
  429. break;
  430. case 5:
  431. DbgInfo(" the ReadCardType of Swiping Magnetic stripe card is unavailable (Built Transfer encrypted channel failed).");
  432. break;
  433. case 6:
  434. DbgInfo(" The PinPad is unavailable.");
  435. break;
  436. case 7:
  437. DbgInfo("The entity is going to die, the business have better do nothing.");
  438. break;
  439. case 8:
  440. DbgInfo(" device is still unavailalble after removing and connecting action.");
  441. break;
  442. case 9:
  443. DbgInfo("device is busing reading card.");
  444. nInforType = 1;
  445. DbgInfo("change Msg from 9 to 1 !!!");
  446. break;
  447. case 10:
  448. DbgInfo("clear all the notify components.");
  449. nInforType = 1;
  450. DbgInfo("change Msg from 10 to 1 !!!");
  451. break;
  452. default:
  453. DbgInfo(CSimpleStringA::Format(" Unknown information type %d.", nInforType));
  454. break;
  455. }
  456. m_nLastSentMsg = nInforType;
  457. //if(nInforType == 0 || nInforType == 1)
  458. {
  459. DbgInfo(CSimpleStringA::Format("Really send msg %d.", nInforType));
  460. ConnectStatus evt;
  461. evt.status = nInforType;
  462. SpSendBroadcast(m_pEntity->GetFunction(), SP_MSG_OF(ConnectStatus),
  463. SP_MSG_SIG_OF(ConnectStatus), evt);
  464. }
  465. }
  466. void OnStateTrans(int iSrcState, int iDstState)
  467. {
  468. DbgInfo(CSimpleStringA::Format("m_nLstState = %d, StateName = %s", iSrcState, GetStateName(iSrcState)));
  469. }
  470. int GetAccessAuthState();
  471. //-1:初始状态 0: 准入成功, 1: 准入失败, 2: 未准入
  472. void SetAAState(int val);
  473. //-1:初始状态 0: 准入成功, 1: 准入失败, 2: 未准入
  474. int GetAAState()
  475. {
  476. //oilyang@20220916 加密通道独立的,不要和准入形成依赖 默认返回0,后续再梳理清理本实体(南天、其他不需要的逻辑)
  477. return 0;
  478. if(m_nAAStatus == -1)
  479. {
  480. SetAAState(GetAccessAuthState());
  481. }
  482. return m_nAAStatus;
  483. }
  484. bool IsCryptMachinaType()
  485. {
  486. //oilyang@20200615 "Desk2S 1.0" should contain the encrypted Channel
  487. if (_strnicmp(m_rvcsysInfo.strMachineType.GetData(), "RVC.PAD", strlen("RVC.PAD")) == 0
  488. || (_strnicmp(m_rvcsysInfo.strMachineType.GetData(), "RVC.Desk2S", strlen("RVC.Desk2S")) == 0
  489. && m_rvcsysInfo.MachineVersion.GetMajor() == 1 && m_rvcsysInfo.MachineVersion.GetMinor() == 0))
  490. return true;
  491. else
  492. return false;
  493. }
  494. void SetAcceptWaitMore(bool bValue);
  495. bool GetAcceptWaitMore() { return m_bWaitAccepteMore; };
  496. void InsertWaitMore();
  497. int FWBBluetoothScanTask();
  498. int nameToBthAddr(bool isSearchNearBy);//oiltest
  499. void StartEntity(const char* pEntityName, bool bFWBOpenSucEver=false);
  500. void TodoReConn();
  501. protected:
  502. //LPIDCCARDDATA* lppCardData;
  503. int m_iInsertTries;
  504. int m_ejectTries;
  505. private:
  506. //m_periodResetCount:一定时间内重置计数
  507. int m_resetTimes,m_testThreadCount,m_connStatus,m_periodResetCount;
  508. //m_dwPeriodResetTime:一定时间内第一次重启时间
  509. DWORD m_dwPeriodResetTime,m_batteryLeft;
  510. HMODULE m_hVerdorDll;
  511. // GUIConsoleService_ClientBase *m_pGUIConsoleClient;
  512. lpCreateDevCom CreateDevComponent;
  513. lpReleaseDevCom ReleaseDevComponent;
  514. lpCMBdecodeMag2 cmdDecodeMag2;
  515. lpCMBdecodeEx cmdDecodeEx;
  516. bool m_devInit,m_bCancelAccept,m_bWaitingAccept,m_bWaitAccepteMore,m_bExit,m_bCancelRead
  517. , m_bCDA, m_bReading,m_bNeedInitCh,m_connChecking,m_bResetInsertMore
  518. , m_bDoingBindFWB, m_bCallingDevOpenEx, m_bBTScan, m_bInMainPage,m_bFWBOpenSucEver;
  519. ErrorCodeEnum m_testResult;
  520. CAutoArray<CSimpleStringA> m_aidList;
  521. SelfChekerClient*m_pSelfcheckClient;
  522. bool m_bCancelQueryConn;
  523. int m_nLastSentMsg, m_timesToTryOpen;
  524. int m_nReadFailCnt;
  525. int m_nAAStatus;
  526. CardSwiperStatus devStatus;
  527. DevStateEnum m_eDevState;
  528. CCardProcess *m_pCardProcess;
  529. CardReadType m_eReadType;
  530. char *m_pDataToARQC;
  531. long xxTest;
  532. CSimpleStringA m_csCM,m_csPM,m_csDevNo;
  533. bool m_isFWB;
  534. int m_version,m_batch;
  535. int m_nSplitRes;
  536. char m_AuthCode[2];
  537. 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];
  538. vector<FWBRecord> m_vFWBRecords;
  539. int SplitTrack2(CSimpleStringA pTrack2,Track2Data &decodeData);
  540. int ProcessEnChannel(bool bIgnore = false);
  541. int IsValidFWBName(const char* pName, int len);
  542. ErrorCodeEnum AfterBindUpdateFWBNameToRunCfg(const char* name, bool bBind);
  543. void AddToFWBRecords(const char* name, const char* remoteMac, bool bFirst);
  544. void ClearFWBRecords();
  545. int ScanNearbyBTDevice();
  546. void ClearHandleOjbects();
  547. CSimpleStringA TransferAboutVendorShortName(CSimpleStringA name);
  548. DevCategoryInfo m_devCat;
  549. CSimpleStringA m_csAlarmMsg;
  550. CSystemStaticInfo m_rvcsysInfo;
  551. };
  552. struct InitTask : public ITaskSp
  553. {
  554. CCardSwiperFSM* fsm;
  555. InitTask(CCardSwiperFSM* f) : fsm(f) {}
  556. void Process()
  557. {
  558. FSMEvent *e = new FSMEvent(USER_EVT_INITFINISHED);
  559. e->param1 = fsm->Initial();
  560. fsm->PostEventFIFO(e);
  561. }
  562. };
  563. struct ReadTask : public ITaskSp
  564. {
  565. SpReqAnsContext<CardSwiperService_Read_Req,CardSwiperService_Read_Ans>::Pointer ctx;
  566. CCardSwiperFSM* fsm;
  567. ReadTask(CCardSwiperFSM* f) : fsm(f) {}
  568. void Process()
  569. {
  570. fsm->SetExitFlag(false);
  571. //int tmpChannelTest = fsm->IsNeedInitChannel()? 1 : 0;
  572. CardReadFinishedEvent *e = new CardReadFinishedEvent();
  573. e->ctx = ctx;
  574. e->param1 = fsm->ReadCard(ctx);
  575. //e->param2 = tmpChannelTest;
  576. fsm->PostEventFIFO(e);
  577. }
  578. };
  579. struct CancelReadTask : public ITaskSp
  580. {
  581. CCardSwiperFSM* fsm;
  582. CancelReadTask(CCardSwiperFSM* f) : fsm(f) {}
  583. void Process()
  584. {
  585. int nRes = fsm->DoAbortRead();
  586. if(nRes != -1)
  587. {
  588. int checkTimes = 0;
  589. int checkMaxTimes = 5;
  590. for(; checkTimes < checkMaxTimes; ++checkTimes)
  591. {
  592. if(!fsm->IsReading()) break;
  593. Sleep(1000);
  594. }
  595. if(checkTimes >= checkMaxTimes)
  596. {
  597. LogWarn(Severity_Middle, Error_Unexpect,
  598. CardSwiper_UserErrorCode_Wait_Too_Much_Time_After_AbortRead,"Wait too much time after AbortRead op.");
  599. }
  600. }
  601. }
  602. };
  603. struct ExitTask : public ITaskSp
  604. {
  605. CCardSwiperFSM* fsm;
  606. ExitTask(CCardSwiperFSM* f) : fsm(f) {}
  607. void Process()
  608. {
  609. fsm->DoAbortRead();
  610. }
  611. };
  612. struct PreOnlineTask : public ITaskSp
  613. {
  614. SpReqAnsContext<CardSwiperService_PreOnline_Req,CardSwiperService_PreOnline_Ans>::Pointer ctx;
  615. CCardSwiperFSM* fsm;
  616. PreOnlineTask(CCardSwiperFSM* f) : fsm(f) {}
  617. void Process()
  618. {
  619. FSMEvent *e = new FSMEvent(USER_EVT_PREONLINEFINISHED);
  620. e->param1 = fsm->PreOnline(ctx);
  621. fsm->PostEventFIFO(e);
  622. }
  623. };
  624. struct PostOnlineTask : public ITaskSp
  625. {
  626. SpReqAnsContext<CardSwiperService_PostOnline_Req,CardSwiperService_PostOnline_Ans>::Pointer ctx;
  627. CCardSwiperFSM* fsm;
  628. PostOnlineTask(CCardSwiperFSM* f) : fsm(f) {}
  629. void Process()
  630. {
  631. FSMEvent *e = new FSMEvent(USER_EVT_POSTONLINEFINISHED);
  632. e->param1 = fsm->PostOnline(ctx);
  633. fsm->PostEventFIFO(e);
  634. }
  635. };
  636. struct EjectTask : public ITaskSp
  637. {
  638. CCardSwiperFSM* fsm;
  639. EjectTask(CCardSwiperFSM* f) : fsm(f) {}
  640. void Process()
  641. {
  642. FSMEvent *pEvt = new FSMEvent(USER_EVT_EJECTFINISHED);
  643. pEvt->param1 = fsm->Eject();
  644. fsm->PostEventFIFO(pEvt);
  645. }
  646. };
  647. struct QueryCardInfoTask : public ITaskSp
  648. {
  649. SpReqAnsContext<CardSwiperService_QueryCardInfo_Req,CardSwiperService_QueryCardInfo_Ans>::Pointer ctx;
  650. CCardSwiperFSM* fsm;
  651. QueryCardInfoTask(CCardSwiperFSM* f) : fsm(f) {}
  652. void Process()
  653. {
  654. FSMEvent *e = new FSMEvent(USER_EVT_QUERY_CARD_INFO_FINISHED);
  655. e->param1 = fsm->QueryCardInfo(ctx);
  656. fsm->PostEventFIFO(e);
  657. }
  658. };
  659. struct MagTransInitV2Task : public ITaskSp
  660. {
  661. CCardSwiperFSM* fsm;
  662. MagTransInitV2Task(CCardSwiperFSM* f) : fsm(f) {}
  663. void Process()
  664. {
  665. FSMEvent *e = new FSMEvent(USER_EVT_MAGDATA_TRANSFER_INIT_V2_FINISHED);
  666. e->param1 = fsm->TransInitEx(true);
  667. fsm->PostEventFIFO(e);
  668. }
  669. };
  670. struct QueryConnectTask : public ITaskSp
  671. {
  672. CCardSwiperFSM* fsm;
  673. int nReserved;
  674. QueryConnectTask(CCardSwiperFSM* f) : fsm(f), nReserved(0) {}
  675. void Process()
  676. {
  677. DbgInfo("Start listerning devcic connect status.");
  678. fsm->QueryConnInfo(nReserved);
  679. }
  680. };
  681. struct InsertWaitMoreTask : public ITaskSp
  682. {
  683. CCardSwiperFSM* fsm;
  684. InsertWaitMoreTask(CCardSwiperFSM* f) : fsm(f){}
  685. void Process()
  686. {
  687. fsm->InsertWaitMore();
  688. }
  689. };
  690. struct QueryFWBListTask : public ITaskSp
  691. {
  692. CCardSwiperFSM* fsm;
  693. SpReqAnsContext<CardSwiperService_QueryFWBList_Req, CardSwiperService_QueryFWBList_Ans>::Pointer ctx;
  694. int fsmState;
  695. QueryFWBListTask(CCardSwiperFSM* f) : fsm(f) {}
  696. void Process()
  697. {
  698. FSMEvent* e = new FSMEvent(USER_EVT_QUERY_FWB_LIST_FINISHED);
  699. e->param1 = fsm->QueryFWBList(ctx, fsmState);
  700. fsm->PostEventFIFO(e);
  701. }
  702. };
  703. struct BindFWBTask : public ITaskSp
  704. {
  705. CCardSwiperFSM* fsm;
  706. SpReqAnsContext<CardSwiperService_BindFWB_Req, CardSwiperService_BindFWB_Ans>::Pointer ctx;
  707. int fsmState;
  708. BindFWBTask(CCardSwiperFSM* f) : fsm(f) {}
  709. void Process()
  710. {
  711. FSMEvent* e = new FSMEvent(USER_EVT_BIND_FWB_FINISHED);
  712. e->param1 = fsm->BindFWB(ctx, fsmState);
  713. fsm->PostEventFIFO(e);
  714. }
  715. };
  716. struct FWBBluetoothScanTask : public ITaskSp
  717. {
  718. CCardSwiperFSM* fsm;
  719. FWBBluetoothScanTask(CCardSwiperFSM* f) : fsm(f) {}
  720. void Process()
  721. {
  722. fsm->FWBBluetoothScanTask();
  723. }
  724. };
  725. struct ReConnTask : public ITaskSp
  726. {
  727. CCardSwiperFSM* fsm;
  728. ReConnTask(CCardSwiperFSM* f) : fsm(f) {}
  729. void Process()
  730. {
  731. fsm->TodoReConn();
  732. }
  733. };
  734. #endif //__CARD_SWIPER_FSM_H