AccessAuthFSM.cpp 16 KB

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