AccessAuthFSM.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. #include "stdafx.h"
  2. #include "AccessAuthFSM.h"
  3. #include "mod_AccessAuth.h"
  4. #include "Event.h"
  5. #include <io.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include "MD5file.h"
  9. CAccessAuthFSM::CAccessAuthFSM()
  10. :m_pConnection(NULL)
  11. {
  12. }
  13. CAccessAuthFSM::~CAccessAuthFSM()
  14. {
  15. m_iState = FSM_STATE_EXIT; // 屏蔽退出ASSERT错误
  16. }
  17. void CAccessAuthFSM::OnStateTrans(int iSrcState, int iDstState)
  18. {
  19. Dbg("trans from %s to %s", GetStateName(iSrcState), GetStateName(iDstState));
  20. }
  21. // 初始化PinPad及KMC
  22. ErrorCodeEnum CAccessAuthFSM::OnInit()
  23. {
  24. LOG_FUNCTION();
  25. AddStateHooker(this);
  26. m_finishAccess = 0;
  27. //设置初始锁定状态,0
  28. CSmartPointer<IEntityFunction> spFunction = m_pEntity->GetFunction();
  29. spFunction->SetSysVar("LockState", "0", true);
  30. ErrorCodeEnum Error = LoadCenterConfig();
  31. if (Error != Error_Succeed)
  32. {
  33. LOG_TRACE("load CenterSetting.ini failed!");
  34. }
  35. return Error_Succeed;
  36. }
  37. ErrorCodeEnum CAccessAuthFSM::OnExit()
  38. {
  39. RemoveStateHooker(this);
  40. return Error_Succeed;
  41. }
  42. void CAccessAuthFSM::s1_on_entry()
  43. {
  44. SetSysVar("I");
  45. }
  46. void CAccessAuthFSM::s1_on_exit()
  47. {
  48. }
  49. unsigned int CAccessAuthFSM::s1_on_event(FSMEvent* pEvent)
  50. {
  51. LOG_FUNCTION();
  52. Dbg("s1_on_event: %d", pEvent->iEvt);
  53. if (pEvent->iEvt == Event_ReportStage)
  54. {
  55. // 上报状态
  56. if (SecureClientConnect() == Error_Succeed)
  57. {
  58. ReportStateEvent *pReportEvent = (ReportStateEvent*)pEvent;
  59. m_pConnection->SendTerminalStagePackage(pReportEvent->cNewStage, pReportEvent->dwNewStageTime,
  60. pReportEvent->cOldStage, pReportEvent->dwOldStageTime);
  61. }
  62. }
  63. return 0;
  64. }
  65. ErrorCodeEnum CAccessAuthFSM::SecureClientConnect()
  66. {
  67. if (m_pConnection != NULL && m_pConnection->IsConnectionOK())
  68. return Error_Succeed;
  69. SecureClientRelease();
  70. m_pConnection = new CAccessAuthConn(m_pEntity, this);
  71. if (m_pConnection->ConnectFromCentralSetting() && m_pConnection->IsConnectionOK())
  72. return Error_Succeed;
  73. SecureClientRelease();
  74. return Error_PeerReject;
  75. }
  76. ErrorCodeEnum CAccessAuthFSM::SecureClientRelease()
  77. {
  78. if (m_pConnection != NULL)
  79. {
  80. m_pConnection->Close();
  81. m_pConnection->DecRefCount();
  82. m_pConnection = NULL;
  83. }
  84. return Error_Succeed;
  85. }
  86. std::_Mutex mut;
  87. struct TimeOutTask : public ITaskSp {
  88. CAccessAuthFSM* m_fsm;
  89. long m_timeOut;//毫秒级
  90. TimeOutTask(CAccessAuthFSM* fsm,long timeOut) :m_fsm(fsm),m_timeOut(timeOut) {}
  91. void Process()
  92. {
  93. Sleep(m_timeOut);
  94. MyMutex myMut(&mut);
  95. if (!m_fsm->m_finishAccess) {
  96. Dbg("准入超时[%d]",m_fsm->m_finishAccess);
  97. m_fsm->PostEventFIFO(new FSMEvent(m_fsm->Event_ReqTokenCancel));
  98. m_fsm->m_finishAccess = 1;
  99. }
  100. }
  101. };
  102. void CAccessAuthFSM::s2_on_entry()
  103. {
  104. LOG_FUNCTION();
  105. // [6/16/2020 9:51 @Gifur]
  106. //ScheduleTimer(2, 120 * 1000); // 设定30秒准入超时
  107. m_finishAccess = 0;
  108. TimeOutTask* timeOutTask = new TimeOutTask(this,120 * 1000);// 设定30秒准入超时
  109. GetEntityBase()->GetFunction()->PostThreadPoolTask(timeOutTask);
  110. Dbg("启动了准入超时定时器2分钟[%d]",m_finishAccess);
  111. auto pEntity = (CAccessAuthEntity*)m_pEntity;
  112. auto rc = pEntity->InitKMC();
  113. if (rc != Error_Succeed)
  114. {
  115. FSMEvent *pEvent = new FSMEvent(Event_UpdateWKFail);
  116. PostEventFIFO(pEvent);
  117. LogWarn(Severity_Middle, Error_Unexpect, ERR_ACCESSAUTH_INIT_KMC,
  118. GetOutPutStr("%s%08X", "InitKMC", rc).c_str());
  119. return;
  120. }
  121. SetSysVar("C");
  122. if ((rc = SecureClientConnect()) != Error_Succeed)
  123. {
  124. // 启动定时器尝试重试
  125. LogWarn(Severity_Middle, Error_Unexpect, ERR_ACCESSAUTH_CONNECT_SERVER,
  126. GetOutPutStr("%s%08X", "SecureClientConnect", rc).c_str());
  127. ScheduleTimer(1, 8000);
  128. return;
  129. }
  130. PostEventFIFO(new FSMEvent(Event_ConnectionOK));
  131. }
  132. void CAccessAuthFSM::s2_on_exit()
  133. {
  134. // 关闭连接
  135. auto pEntity = (CAccessAuthEntity*)m_pEntity;
  136. pEntity->ReleaseKMC();
  137. SecureClientRelease();
  138. CancelTimer(1);
  139. //CancelTimer(2);
  140. }
  141. // 会收到Event_UpdateWKResult和Event_ReqTokenResult和EVT_TIMER
  142. unsigned int CAccessAuthFSM::s2_on_event(FSMEvent* pEvent)
  143. {
  144. if (pEvent->iEvt == EVT_TIMER)
  145. {
  146. if (pEvent->param1 == 2) // access timeout
  147. {
  148. // 重试超时
  149. Dbg("access authorize timeout");
  150. PostEventFIFO(new FSMEvent(Event_ReqTokenCancel));
  151. }
  152. else if (pEvent->param1 == 1 || pEvent->param1 == 3) // reconnect
  153. {
  154. auto rc = SecureClientConnect();
  155. if (rc != Error_Succeed)
  156. {
  157. // 启动定时器尝试重试
  158. LogWarn(Severity_Middle, Error_Unexpect, ERR_ACCESSAUTH_CONNECT_SERVER,
  159. GetOutPutStr("%s%08X","SecureClientConnect",rc).c_str());
  160. ScheduleTimer(1, 8000);
  161. return 1;
  162. }
  163. PostEventFIFO(new FSMEvent(Event_ConnectionOK));
  164. return 0;
  165. }
  166. }
  167. else if (pEvent->iEvt == Event_ConnectionOK)
  168. {
  169. auto pEntity = ((CAccessAuthEntity*)m_pEntity);
  170. int isFirstAccessAfterSM = pEntity->GetOrSetIsFirstSM(0);
  171. if (isFirstAccessAfterSM != 1) {
  172. FSMEvent* pEvent = new FSMEvent(Event_CheckMD5Succ);
  173. PostEventFIFO(pEvent);
  174. return 0;
  175. }
  176. CSimpleStringA strInitState;
  177. pEntity->GetFunction()->GetSysVar("InitState", strInitState);
  178. if (strInitState == "1") {
  179. //2020/5/29 删除了各个文件MD5检验的代码
  180. FSMEvent* pEvent = new FSMEvent(Event_CheckMD5Succ);
  181. PostEventFIFO(pEvent);
  182. }
  183. else {
  184. ScheduleTimer(3, 1500);
  185. }
  186. }
  187. else if (pEvent->iEvt == Event_CheckMD5Fail)
  188. {
  189. // 上报状态
  190. m_pConnection->SendReportStatePackage("CheckMD5", Error_Unexpect, ((CAccessAuthEntity*)m_pEntity)->GetAuthErrMsg());
  191. return 0;
  192. }
  193. else if (pEvent->iEvt == Event_CheckMD5Succ)
  194. {
  195. // 同步服务器时间
  196. DWORD rc = m_pConnection->SendSyncTimePackage();
  197. if (rc != Error_Succeed)
  198. {
  199. FSMEvent *pEvent = new FSMEvent(Event_EndSyncTime);
  200. PostEventFIFO(pEvent);
  201. LogWarn(Severity_Middle, Error_Unexpect, ERR_ACCESSAUTH_SYNC_TIME,
  202. GetOutPutStr("%s%08X", "SendSyncTimePackage", rc).c_str());
  203. }
  204. //获取终端锁定状态
  205. rc = m_pConnection->SendLockStatePackage();
  206. if (rc != Error_Succeed)
  207. {
  208. LogWarn(Severity_Middle, Error_Unexpect, rc,
  209. GetOutPutStr("%s%08X", "SendLockStatePackage", rc).c_str());
  210. }
  211. }
  212. else if (pEvent->iEvt == Event_EndSyncTime)
  213. {
  214. auto pEntity = ((CAccessAuthEntity*)m_pEntity);
  215. if (!pEntity->HasPinPad())
  216. {
  217. // 没有密码键盘,直接准入
  218. Dbg("has no pinpad, ignore update wk");
  219. PostEventFIFO(new FSMEvent(CAccessAuthFSM::Event_IgnoreUpdateWK));
  220. return 0;
  221. }
  222. // 检查上次密钥同步时间(一天只同步一次)
  223. //CSmartPointer<IConfigInfo> pConfig;
  224. //m_pEntity->GetFunction()->OpenConfig(Config_Software, pConfig);
  225. int nWKLastSyncTime(0);
  226. //pConfig->ReadConfigValueInt("Main", "WKSyncSuccTime", nWKLastSyncTime);
  227. int nWKSyncFailCount(0);
  228. //pConfig->ReadConfigValueInt("Main", "WKSyncFailCount", nWKSyncFailCount);
  229. CSimpleStringA strWKSyncSuccTime = "";
  230. CSimpleStringA strWKSyncFailCount = "";
  231. auto rc = pEntity->GetFunction()->GetSysVar("WKSyncSuccTime", strWKSyncSuccTime);
  232. assert(rc == Error_Succeed);
  233. nWKLastSyncTime = atoi(strWKSyncSuccTime);
  234. rc = pEntity->GetFunction()->GetSysVar("WKSyncFailCount", strWKSyncFailCount);
  235. assert(rc == Error_Succeed);
  236. nWKSyncFailCount = atoi(strWKSyncFailCount);
  237. SYSTEMTIME stSyncTime = CSmallDateTime(nWKLastSyncTime).ToSystemTime();
  238. Dbg("last WK sync time: %04d-%02d-%02d %02d:%02d:%02d",
  239. stSyncTime.wYear, stSyncTime.wMonth, stSyncTime.wDay,
  240. stSyncTime.wHour, stSyncTime.wMinute, stSyncTime.wSecond);
  241. SYSTEMTIME stNow = {};
  242. GetLocalTime(&stNow);
  243. if (nWKLastSyncTime > 0 && stSyncTime.wYear == stNow.wYear
  244. && stSyncTime.wMonth == stNow.wMonth && stSyncTime.wDay == stNow.wDay
  245. && nWKSyncFailCount == 0 ) // 最近一次同步成功,才能跳过
  246. {
  247. Dbg("WK has been updated today, last sync time: %s", (const char*)CSmallDateTime(nWKLastSyncTime).ToTimeString());
  248. FSMEvent *pEvent = new FSMEvent(Event_IgnoreUpdateWK);
  249. PostEventFIFO(pEvent);
  250. }
  251. else
  252. {
  253. Dbg("begin update WK now");
  254. // 请求WK
  255. DWORD rc = m_pConnection->SendWKUpdatePackage();
  256. if (rc != Error_Succeed)
  257. {
  258. LogWarn(Severity_Middle, Error_Unexpect, ERR_ACCESSAUTH_UPDATE_WK,
  259. GetOutPutStr("%s%08X", "SendWKUpdatePackage", rc).c_str());
  260. FSMEvent *pEvent = new FSMEvent(Event_UpdateWKFail);
  261. PostEventFIFO(pEvent);
  262. }
  263. }
  264. return 0;
  265. }
  266. else if (pEvent->iEvt == Event_UpdateWKSucc)
  267. {
  268. // 上报状态
  269. m_pConnection->SendReportStatePackage("UpdateWK", Error_Succeed, "更新工作密钥成功");
  270. // 保存WK同步时间
  271. //CSmartPointer<IConfigInfo> pConfig;
  272. //m_pEntity->GetFunction()->OpenConfig(Config_Software, pConfig);
  273. DWORD rc = m_pEntity->GetFunction()->SetSysVar("WKSyncSuccTime", (const char*) CSimpleStringA::Format("0x%08X", (DWORD)CSmallDateTime::GetNow()), true);
  274. assert(rc == Error_Succeed);
  275. /*pConfig->WriteConfigValue("Main", "WKSyncSuccTime",
  276. (const char*) CSimpleStringA::Format("0x%08X", (DWORD)CSmallDateTime::GetNow()));*/
  277. // 重置失败次数
  278. //pConfig->WriteConfigValueInt("Main", "WKSyncFailCount", 0);
  279. rc = m_pEntity->GetFunction()->SetSysVar("WKSyncFailCount", "0", true);
  280. assert(rc == Error_Succeed);
  281. // 请求Token
  282. Dbg("begin get token now");
  283. rc = SecureClientConnect();
  284. if (rc == Error_Succeed)
  285. rc = m_pConnection->SendGetTokenPackage();
  286. if (rc != Error_Succeed)
  287. {
  288. FSMEvent *pEvent = new FSMEvent(Event_ReqTokenFail);
  289. PostEventFIFO(pEvent);
  290. }
  291. return 0;
  292. }
  293. else if (pEvent->iEvt == Event_IgnoreUpdateWK)
  294. {
  295. // 忽略同步WK,直接准入
  296. Dbg("ignore update wk, get token now");
  297. DWORD rc = SecureClientConnect();
  298. if (rc == Error_Succeed)
  299. rc = m_pConnection->SendGetTokenPackage();
  300. if (rc != Error_Succeed)
  301. {
  302. FSMEvent *pEvent = new FSMEvent(Event_ReqTokenFail);
  303. PostEventFIFO(pEvent);
  304. }
  305. }
  306. else if (pEvent->iEvt == Event_UpdateWKFail)
  307. {
  308. Dbg("update pinpad WK fail");
  309. // 上报状态
  310. m_pConnection->SendReportStatePackage("UpdateWK", Error_Unexpect, ((CAccessAuthEntity*)m_pEntity)->GetAuthErrMsg());
  311. // WK失败,累加失败次数,一天内失败3次以上,不再同步WK,直接请求Token
  312. /*CSmartPointer<IConfigInfo> pConfig;
  313. m_pEntity->GetFunction()->OpenConfig(Config_Software, pConfig);
  314. int nWKSyncFailCount(0);
  315. pConfig->ReadConfigValueInt("Main", "WKSyncFailCount", nWKSyncFailCount);
  316. pConfig->WriteConfigValueInt("Main", "WKSyncFailCount", ++nWKSyncFailCount);*/
  317. // zl@20190624 WKSyncFailCount迁移到系统变量
  318. CSimpleStringA strWKSyncFailCount = "";
  319. auto rc = m_pEntity->GetFunction()->GetSysVar("WKSyncFailCount", strWKSyncFailCount);
  320. assert(rc == Error_Succeed);
  321. int nWKSyncFailCount = atoi(strWKSyncFailCount);
  322. nWKSyncFailCount++;
  323. rc = m_pEntity->GetFunction()->SetSysVar("WKSyncFailCount", CSimpleStringA::Format("%d", nWKSyncFailCount), true);
  324. assert(rc == Error_Succeed);
  325. // xkm@20150702 启用新准入方案,密钥同步失败不得准入
  326. // xkm@20151116 失败3次以上应直接跳过同步,避免KMC故障时影响可视柜台准入
  327. // xkm@20161220 更新密钥失败直接准入
  328. //if (nWKSyncFailCount >= 3)
  329. {
  330. //Dbg("update WK fail more than 3 times, ignore it");
  331. PostEventFIFO(new FSMEvent(CAccessAuthFSM::Event_IgnoreUpdateWK));
  332. }
  333. //else
  334. //{
  335. // PostEventFIFO(new FSMEvent(CAccessAuthFSM::Event_ReqTokenFail));
  336. //}
  337. }
  338. else if (pEvent->iEvt == Event_ReqTokenFail)
  339. {
  340. do {
  341. MyMutex myMut(&mut);
  342. if (m_finishAccess) return 0;
  343. m_finishAccess = 1;
  344. auto pEntity = (CAccessAuthEntity*)m_pEntity;
  345. CSimpleStringA strErrMsg = CSimpleStringA::Format("准入失败(%d)", m_finishAccess);
  346. pEntity->GetFunction()->ShowFatalError(strErrMsg);
  347. } while (0);
  348. // 上报状态
  349. m_pConnection->SendReportStatePackage("AccessAuth", Error_Unexpect, ((CAccessAuthEntity*)m_pEntity)->GetAuthErrMsg());
  350. //// 如果准入失败,表示前后台密钥可能不同步,需累加WKSyncFailCount
  351. //CSmartPointer<IConfigInfo> pConfig;
  352. //m_pEntity->GetFunction()->OpenConfig(Config_Software, pConfig);
  353. //int nWKSyncFailCount(0);
  354. //pConfig->ReadConfigValueInt("Main", "WKSyncFailCount", nWKSyncFailCount);
  355. //if (nWKSyncFailCount == 0)
  356. // pConfig->WriteConfigValueInt("Main", "WKSyncFailCount", ++nWKSyncFailCount);
  357. return 0;
  358. }
  359. else if (pEvent->iEvt == Event_ReqTokenCancel)
  360. {
  361. auto pEntity = (CAccessAuthEntity*)m_pEntity;
  362. CSimpleStringA strErrMsg = CSimpleStringA::Format("准入超时(%d)", m_finishAccess);
  363. pEntity->GetFunction()->ShowFatalError(strErrMsg);
  364. // 上报状态
  365. if (m_pConnection != NULL && m_pConnection->IsConnectionOK())
  366. {
  367. m_pConnection->SendReportStatePackage("AccessAuth", Error_TimeOut, "准入超时");
  368. }
  369. return 0;
  370. }
  371. else if (pEvent->iEvt == Event_ReqTokenSucc)
  372. {
  373. do {
  374. MyMutex myMut(&mut);
  375. if (m_finishAccess) return 0;
  376. m_finishAccess = 1;
  377. } while (0);
  378. // 上报状态
  379. m_pConnection->SendReportStatePackage("AccessAuth", Error_Succeed, "准入成功");
  380. return 0;
  381. }
  382. return 0;
  383. }
  384. void CAccessAuthFSM::s3_on_entry()
  385. {
  386. SetSysVar("F");
  387. auto pEntity = (CAccessAuthEntity*)m_pEntity;
  388. CSimpleStringA strErrMsg = CSimpleStringA::Format("准入失败(%s)", (const char*)pEntity->GetAuthErrMsg());
  389. // 发送准入失败事件
  390. //LogError(Severity_Middle, Error_Unexpect, EVENT_ACCESSAUTH_FAILED, strErrMsg);
  391. LogWarn(Severity_Middle, Error_Unexpect, ERR_ACCESSAUTH_FAILED,
  392. GetOutPutStr("%s%s","准入", (const char*)pEntity->GetAuthErrMsg()).c_str());
  393. pEntity->GetFunction()->ShowFatalError(strErrMsg);
  394. }
  395. void CAccessAuthFSM::s3_on_exit()
  396. {
  397. }
  398. unsigned int CAccessAuthFSM::s3_on_event(FSMEvent* event)
  399. {
  400. return 0;
  401. }
  402. void CAccessAuthFSM::s4_on_entry()
  403. {
  404. SetSysVar("A");
  405. // 发送准入超时事件
  406. LogWarn(Severity_Middle, Error_Unexpect, ERR_ACCESSAUTH_TIMEOUT,
  407. GetOutPutStr("%s%s", "准入", "超时").c_str());
  408. // 切换到s1
  409. PostEventFIFO(new FSMEvent(Event_StateTimeout));
  410. }
  411. void CAccessAuthFSM::s4_on_exit()
  412. {
  413. }
  414. unsigned int CAccessAuthFSM::s4_on_event(FSMEvent* event)
  415. {
  416. return 0;
  417. }
  418. void CAccessAuthFSM::s5_on_entry()
  419. {
  420. SetSysVar("L");
  421. LogEvent(Severity_Middle, EVENT_ACCESSAUTH_SUCCEED, "终端准入成功");
  422. m_pEntity->GetFunction()->ShowStartupInfo("准入成功");
  423. }
  424. void CAccessAuthFSM::s5_on_exit()
  425. {
  426. }
  427. unsigned int CAccessAuthFSM::s5_on_event(FSMEvent* pEvent)
  428. {
  429. if (pEvent->iEvt == Event_StartUnregist)
  430. {
  431. // 取出参数先保存
  432. m_nExitReason = pEvent->param1;
  433. m_nExitWay = pEvent->param2;
  434. }
  435. else if (pEvent->iEvt == Event_ReportStage)
  436. {
  437. // 上报状态
  438. if (SecureClientConnect() == Error_Succeed)
  439. {
  440. ReportStateEvent *pReportEvent = (ReportStateEvent*)pEvent;
  441. m_pConnection->SendTerminalStagePackage(pReportEvent->cNewStage, pReportEvent->dwNewStageTime,
  442. pReportEvent->cOldStage, pReportEvent->dwOldStageTime);
  443. }
  444. }
  445. return 0;
  446. }
  447. void CAccessAuthFSM::s6_on_entry()
  448. {
  449. SetSysVar("E");
  450. if (SecureClientConnect() != Error_Succeed)
  451. {
  452. // 启动定时器尝试重试
  453. Dbg("connect to AccessAuthorization service fail, start timer(30s) to retry");
  454. ScheduleTimer(2, 30000);
  455. return;
  456. }
  457. PostEventFIFO(new FSMEvent(Event_ConnectionOK));
  458. }
  459. void CAccessAuthFSM::s6_on_exit()
  460. {
  461. CancelTimer(2);
  462. }
  463. unsigned int CAccessAuthFSM::s6_on_event(FSMEvent* pEvent)
  464. {
  465. if (pEvent->iEvt == EVT_TIMER)
  466. {
  467. if (SecureClientConnect() != Error_Succeed)
  468. {
  469. // 启动定时器尝试重试
  470. Dbg("connect to AccessAuthorization service fail, start timer to retry");
  471. ScheduleTimer(2, 30000);
  472. return 1;
  473. }
  474. PostEventFIFO(new FSMEvent(Event_ConnectionOK));
  475. }
  476. else if (pEvent->iEvt == Event_ConnectionOK)
  477. {
  478. // 请求退出
  479. DWORD rc = m_pConnection->SendExitNoticePackage(m_nExitReason, m_nExitWay);
  480. // 切换到s1
  481. PostEventFIFO(new FSMEvent(Event_StateTimeout));
  482. }
  483. return 0;
  484. }
  485. void CAccessAuthFSM::s7_on_entry()
  486. {
  487. SetSysVar("T");
  488. // 过渡状态,立刻转入孤立状态
  489. PostEventFIFO(new FSMEvent(Event_StateTimeout));
  490. }
  491. void CAccessAuthFSM::s7_on_exit()
  492. {
  493. }
  494. unsigned int CAccessAuthFSM::s7_on_event(FSMEvent* event)
  495. {
  496. return 0;
  497. }
  498. ErrorCodeEnum CAccessAuthFSM::SetSysVar(const CSimpleStringA &newVal)
  499. {
  500. CSmartPointer<IEntityFunction> spFunction = m_pEntity->GetFunction();
  501. return spFunction->SetSysVar("EntryPermit", (const char*)newVal);
  502. }
  503. DWORD CAccessAuthFSM::InitDevice(SpReqAnsContext<AccessAuthService_InitDev_Req, AccessAuthService_InitDev_Ans>::Pointer &ctx)
  504. {
  505. DWORD rc = SecureClientConnect();
  506. if (rc == Error_Succeed)
  507. rc = m_pConnection->SendInitDevicePackage(ctx);
  508. else
  509. Dbg("secure connect fail");
  510. if (rc != Error_Succeed)
  511. {
  512. ctx->Answer(rc? Error_Unexpect: Error_Succeed);
  513. return rc;
  514. }
  515. return Error_Succeed;
  516. }
  517. DWORD CAccessAuthFSM::SyncTime()
  518. {
  519. auto rc = SecureClientConnect();
  520. if (rc == Error_Succeed)
  521. {
  522. return m_pConnection->SendSyncTimePackageNew();
  523. }
  524. else
  525. {
  526. Dbg("secure connect fail");
  527. return rc;
  528. }
  529. }
  530. ErrorCodeEnum CAccessAuthFSM::LoadCenterConfig()
  531. {
  532. CSmartPointer<IEntityFunction> spFunction = m_pEntity->GetFunction();
  533. CSmartPointer<IConfigInfo> spConfig;
  534. ErrorCodeEnum Error = spFunction->OpenConfig(Config_CenterSetting, spConfig);
  535. if (Error_Succeed == Error)
  536. {
  537. Error = spConfig->ReadConfigValueInt("AccessAuthorization", "CheckMD5", m_nCheckMD5);
  538. if (Error_Succeed == Error)
  539. {
  540. Dbg("get CheckMD5=%d from CenterSetting.ini", m_nCheckMD5);
  541. }
  542. else
  543. {
  544. Dbg("get CheckMD5 from CenterSetting.ini failed");
  545. }
  546. }
  547. return Error;
  548. }