mod_selfchecker.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. // mod_selfchecker.cpp : Defines the exported functions for the DLL application.
  2. //
  3. #include "stdafx.h"
  4. #include "mod_selfchecker.h"
  5. #include "EventCode.h"
  6. #include "ModuleMix.h"
  7. #include "SpHelper.h"
  8. //read from config file? oiltmp
  9. const int MAX_RESTART_COUNT = 5;
  10. void SelfCheckerSession::Handle_RealCheck(SpReqAnsContext<SelfCheckerService_RealCheck_Req, SelfCheckerService_RealCheck_Ans>::Pointer ctx)
  11. {
  12. LOG_FUNCTION();
  13. m_pEntity->RealCheck(ctx);
  14. }
  15. void SelfCheckerSession::Handle_GetEntityErrorList(SpReqAnsContext<SelfCheckerService_GetEntityErrorList_Req, SelfCheckerService_GetEntityErrorList_Ans>::Pointer ctx)
  16. {
  17. m_pEntity->GetEntityErrorList(ctx);
  18. }
  19. ErrorCodeEnum CSelfCheckerEntity::GetLiveEntityList(CAutoArray<CSimpleString> &LiveEntitys)
  20. {
  21. ErrorCodeEnum errCode;
  22. errCode = m_fsm.GetAllLiveEntity(LiveEntitys);
  23. return errCode;
  24. }
  25. ErrorCodeEnum CSelfCheckerEntity::GetEntityTestCode(const char *pszEntityName,ErrorCodeEnum &LastTestError, TestActionEnum &eLastAction)
  26. {
  27. return Error_NotImpl;
  28. }
  29. ErrorCodeEnum CSelfCheckerEntity::ExamineEntity(const char *pszEntityName)
  30. {
  31. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  32. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  33. CSmartPointer<IAsynWaitSp> spWait;
  34. //ErrorCodeEnum errCode;
  35. pFuncPrivilege->TestEntity(pszEntityName,Test_Examine,spWait);
  36. return Error_Succeed;
  37. }
  38. //ErrorCodeEnum CSelfCheckerEntity::AddEntity(const char *pszEntityName)
  39. //{
  40. // if (pszEntityName == NULL)
  41. // return Error_Null;
  42. // return m_fsm.AddEntity(pszEntityName);
  43. //}
  44. //ErrorCodeEnum CSelfCheckerEntity::RemoveEntity(vector<CSimpleStringA> &entityVec, const char *pszEntityName)
  45. //{
  46. // if (pszEntityName == NULL)
  47. // return Error_Null;
  48. // return m_fsm.RemoveEntity(pszEntityName);
  49. //}
  50. //ErrorCodeEnum CSelfCheckerEntity::CheckEntity()
  51. //{
  52. // return Error_Succeed;
  53. //}
  54. // if pszTriggerEntity=null mean created by shell
  55. void CSelfCheckerEntity::OnCreated(const char *pszEntityName,ErrorCodeEnum eOnStartErrorCode,const char *pszCallerEntity)
  56. {
  57. m_fsm.DoOnCreated(pszEntityName,eOnStartErrorCode,pszCallerEntity);
  58. }
  59. //Call after the entity has close or killed, eOnCloseErrorCode notic if the entity do OnClose normally and resource has beed release
  60. void CSelfCheckerEntity::OnClosed(const char *pszEntityName,EntityCloseCauseEnum eCloseCause,ErrorCodeEnum eOnCloseErrorCode,const char *pszCallerEntity)
  61. {
  62. m_fsm.DoOnClosed(pszEntityName,eCloseCause,eOnCloseErrorCode,pszCallerEntity);
  63. }
  64. //the following OnException & OnEntityStateHook doing the same thing repeatly???
  65. //1 case is entity raise exception and catch by framework,but not mean the entity will close,2 case is the entity is enter lost state
  66. void CSelfCheckerEntity::OnException(const char *pszEntityName,const char *pszFunctionName,EntityStateEnum eState,EntityStateEnum eLastState,ErrorCodeEnum eErrorCode)
  67. {
  68. m_fsm.DoOnException(pszEntityName,pszFunctionName,eState,eLastState,eErrorCode);
  69. }
  70. void CSelfCheckerEntity::OnEntityStateHook(const char *pszEntityName,const char *pszTriggerEntity,EntityStateEnum eState,EntityStateEnum eLastState)
  71. {
  72. Dbg("%s trigger by %s: from %s to %s",pszEntityName,pszTriggerEntity, SpStrEntityState(eLastState), SpStrEntityState(eState));
  73. m_fsm.AddEntityState(pszEntityName,eState);
  74. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  75. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  76. if (pFuncPrivilege == NULL)
  77. {
  78. Dbg("NoPrivilege");
  79. return;
  80. }
  81. CSmartPointer<IAsynWaitSp> spWait;
  82. ErrorCodeEnum errCode;
  83. int count = 0;
  84. //oiltest
  85. //if (!_strnicmp("MediaController",pszEntityName,strlen("MediaController")))
  86. {
  87. switch(eState)
  88. {
  89. case EntityState_Lost:
  90. //doing nothing except for loading stage. HandShake will take it.
  91. //and only for MediaController module.
  92. if (m_bEverInMainPage && _strnicmp("TokenKeeper",pszEntityName,strlen("TokenKeeper")) == 0)
  93. {
  94. Dbg("TokenKeeper lost");
  95. LogEvent(Severity_Middle,LOG_EVT_SELFCHECK_TOKEN_KEEPER_LOST,"TokenKeeper lost,restart and auth again.");
  96. break;
  97. }
  98. if (m_bEverInMainPage)
  99. break;
  100. if (_strnicmp("MediaController",pszEntityName,strlen("MediaController")) != 0)
  101. break;
  102. count = m_entityProcInfo[pszEntityName].restartTimes;
  103. if (count >= MAX_RESTART_COUNT)
  104. {
  105. Dbg("%s has restart %d times,give up to restart it again.",pszEntityName,count);
  106. char xxx[256] = {0};
  107. sprintf(xxx,"%s restart %d times,give up.",(LPCTSTR)pszEntityName,count);
  108. Dbg("logwarn %s",xxx);
  109. LogWarn(Severity_Middle,Error_Unexpect,LOG_WARN_SELFCHECK_ENTITY_LOST_TIMES,xxx);
  110. break;
  111. }
  112. Dbg("%s lost count %d",pszEntityName,count);
  113. errCode = pFuncPrivilege->TerminateEntity(pszEntityName,spWait);
  114. if (errCode == Error_Succeed)
  115. {
  116. errCode = spWait->WaitAnswer(10000);
  117. Dbg("onlost kill wait %d",errCode);
  118. }
  119. else
  120. Dbg("onlost kill %d",errCode);
  121. Sleep(5000);
  122. Dbg("to start(lost)");
  123. errCode = pFuncPrivilege->StartEntity(pszEntityName,NULL,spWait);
  124. if (errCode == Error_Succeed)
  125. {
  126. errCode = spWait->WaitAnswer(10000);
  127. Dbg("onlost start wait %d",errCode);
  128. }
  129. else
  130. Dbg("onlost start %d",errCode);
  131. m_entityProcInfo[pszEntityName].restartTimes = count + 1;
  132. break;
  133. case EntityState_Idle:
  134. if (!_strnicmp("AssistantChannel",pszEntityName,strlen("AssistantChannel")))
  135. {
  136. if (m_pAssChanClient != NULL)
  137. {
  138. m_pAssChanClient->SafeDelete();
  139. m_pAssChanClient = NULL;
  140. }
  141. Dbg("AssistantChannel change to idle.");
  142. LogEvent(Severity_Middle,LOG_EVT_SELFCHECK_ASSISTANTCHANNEL_IDLE,"AssistantChannel to idle.");
  143. ConnectToAssistChannel();
  144. }
  145. if (!_strnicmp("SIPPhone",pszEntityName,strlen("SIPPhone")))
  146. {
  147. Dbg("SIPPhone change to idle.");
  148. LogEvent(Severity_Middle,LOG_EVT_SELFCHECK_SIPPHONE_IDLE,"SIPPhone to idle.");
  149. }
  150. if (!_strnicmp("IEBrowser",pszEntityName,strlen("IEBrowser")))
  151. {
  152. //Dbg("selfchecker to connect healthmanager...");
  153. //ConnectToHealthManager();
  154. Dbg("IEBrowser change to idle.");
  155. LogEvent(Severity_Middle,LOG_EVT_SELFCHECK_IEBROWSER_IDLE,"IEBrowser to idle.");
  156. m_bIEIdle = true;
  157. }
  158. if (!_strnicmp("HealthManager", pszEntityName, strlen("HealthManager")))
  159. {
  160. //Dbg("selfchecker to connect healthmanager...after healthmanager have been restarted.");
  161. //ConnectToHealthManager();
  162. }
  163. if (!_strnicmp("LocalMediaPlay",pszEntityName,strlen("LocalMediaPlay")))
  164. {
  165. Dbg("LocalMediaPlay change to idle.");
  166. LogEvent(Severity_Middle,LOG_EVT_SELFCHECK_LOCALMEDIAPLAY_IDLE,"LocalMediaPlay to idle.");
  167. }
  168. break;
  169. default:
  170. break;
  171. }
  172. }
  173. }
  174. void CSelfCheckerEntity::OnUserStateHook(const char *pszEntityName,DWORD dwState,DWORD dwLastState)
  175. {
  176. }
  177. void CSelfCheckerEntity::OnCeateConnection(const char *pszCallerEntity,const char *pszServiceEntity)
  178. {
  179. }
  180. void CSelfCheckerEntity::OnCloseConnection(const char *pszCallerEntity,const char *pszServiceEntity)
  181. {
  182. }
  183. void CSelfCheckerEntity::RealCheck(SpReqAnsContext<SelfCheckerService_RealCheck_Req, SelfCheckerService_RealCheck_Ans>::Pointer ctx)
  184. {
  185. Dbg("Real check %s",(LPCTSTR)ctx->Req.name);
  186. m_fsm.CheckEntity(ctx->Req.name,Test_ShakeHand);
  187. ctx->Answer(Error_Succeed);
  188. }
  189. void CSelfCheckerEntity::GetEntityErrorList(SpReqAnsContext<SelfCheckerService_GetEntityErrorList_Req, SelfCheckerService_GetEntityErrorList_Ans>::Pointer ctx)
  190. {
  191. ErrorCodeEnum eErr = m_fsm.GetEntityErrorList(ctx->Ans.warmLevel,ctx->Ans.list);
  192. ctx->Answer(eErr);
  193. }
  194. void CSelfCheckerEntity::OnLog(const CAutoArray<CUUID> &SubIDs, const CUUID nLogID,const LogTypeEnum eLogType, const SeverityLevelEnum eLevel,
  195. const DWORD dwSysError,const DWORD dwUserCode,const DWORD dwEntityInstanceID, const WORD wEntityDevelID,
  196. const CAutoArray<DWORD> &Param, const char *pszEntityName, const char *pszModuleName,const char *pszMessage)
  197. {
  198. //oilyang@20200304 add process FWB open suc.
  199. if (dwUserCode == LOG_EVT_CARDREADER_OPEN_SUC_FWB)
  200. {
  201. //Dbg("fwb user code %d,%s", dwUserCode, (LPCTSTR)pszMessage);
  202. //m_fsm.CheckEntity("PinPad", Test_ShakeHand);
  203. //m_fsm.CheckEntity("FingerPrint", Test_ShakeHand);
  204. //m_fsm.CheckEntity("IDCertificate", Test_ShakeHand);
  205. //m_fsm.CheckEntity("Sensors", Test_ShakeHand);
  206. //m_fsm.CheckEntity("DeviceControl", Test_ShakeHand);
  207. }
  208. //oilyang@20200407 move the following RestartModule from Healthmanager to here
  209. switch (dwUserCode)
  210. {
  211. case LOG_EVT_CARDSWIPER_PINPAD_RESTART:
  212. {
  213. RestartModule("PinPad");
  214. }
  215. break;
  216. case LOG_EVT_CARDSWIPER_CARDSWIPER_RESTART:
  217. {
  218. RestartModule("CardSwiper");
  219. }
  220. break;
  221. case LOG_EVT_CARDSWIPER_DEVICECONTROL_RESTART:
  222. {
  223. RestartModule("DeviceControl");
  224. }
  225. break;
  226. case LOG_EVT_CARDSWIPER_IDCERTIFICATE_RESTART:
  227. {
  228. RestartModule("IDCertificate");
  229. }
  230. break;
  231. case LOG_EVT_CARDSWIPER_FINGERPRINT_RESTART:
  232. {
  233. RestartModule("FingerPrint");
  234. }
  235. break;
  236. case LOG_EVT_CARDSWIPER_SENSORS_RESTART:
  237. {
  238. RestartModule("Sensors");
  239. }
  240. break;
  241. }
  242. //oilyang@20170918 only process the Log_Error of Severity_High
  243. if (eLogType != Log_Error || eLevel != Severity_High)
  244. return;
  245. Dbg("user code %d,%s",dwUserCode,(LPCTSTR)pszMessage);
  246. switch(dwUserCode)
  247. {
  248. //case LOG_ERR_CARDISSUER_FAILED:
  249. //case LOG_ERR_CARDREADER_FAILED:
  250. //case LOG_ERR_IDCERTIFICATE_FAILED:
  251. case LOG_ERR_PINPAD_FAILED:
  252. case ERROR_MOD_MEDIACONTROLLER_ENVCAMERA_BUG:
  253. case ERROR_MOD_MEDIACONTROLLER_OPECAMERA_BUG:
  254. case ERROR_MOD_MEDIACONTROLLER_ENVCAM_OPEN:
  255. case ERROR_MOD_MEDIACONTROLLER_OPTCAM_OPEN:
  256. case ERROR_MOD_MEDIACONTROLLER_ENVCAM_INITFAIL:
  257. case ERROR_MOD_MEDIACONTROLLER_OPTCAM_INITFAIL:
  258. MsgPushedByAssChannel(ACM_TYPE_DEVICE,ACM_DEVICE_ERROR,pszEntityName,pszMessage);
  259. break;
  260. default:
  261. break;
  262. }
  263. m_fsm.Proc(pszEntityName, ProcType_Warn, dwUserCode, pszMessage);
  264. }
  265. ErrorCodeEnum CSelfCheckerEntity::MsgPushedByAssChannel(const int type,const int sub_type,const char *pszEntityName,const char *pszMessage)
  266. {
  267. if (m_pAssChanClient == NULL)
  268. {
  269. ErrorCodeEnum eConn = ConnectToAssistChannel();
  270. if (eConn != Error_Succeed)
  271. {
  272. Dbg("connect to AssistantChannel failed(%d).",eConn);
  273. return Error_Resource;
  274. }
  275. }
  276. ChannelService_Send_Info Info;
  277. Info.compress = false;
  278. Info.encrypt = false;
  279. Info.type = type;//;
  280. Info.id = 0;
  281. Info.sub_type = sub_type;//;
  282. Info.data.Alloc(sizeof(int));
  283. SpBuffer buf;
  284. buf.OpenWrite();
  285. CSimpleStringA csEntityName(pszEntityName),csBlack(" "),csMsg(pszMessage);
  286. buf & csEntityName & csBlack & csMsg;
  287. Info.data = buf.ToBlob();
  288. m_pAssChanClient->Send(Info);
  289. Dbg("send msg %d %d:%s %s",type,sub_type,pszEntityName,pszMessage);
  290. return Error_Succeed;
  291. }
  292. ErrorCodeEnum CSelfCheckerEntity::GetEntityStatusStr(CSimpleStringA &result)
  293. {
  294. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  295. CEntityRunInfo runInfo;
  296. ErrorCodeEnum eErr;
  297. vector<CSimpleStringA>::iterator it;
  298. for (it = m_vQueryStateEntity.begin(); it != m_vQueryStateEntity.end(); ++it)
  299. {
  300. eErr = pFunc->GetEntityRunInfo(*it,runInfo);
  301. if (eErr == Error_Succeed)
  302. {
  303. Dbg("%s,%d",(LPCTSTR)*it,runInfo.dwUserState);
  304. result += UserDefEntityMsg[runInfo.dwUserState][0];
  305. result += " ;";
  306. }
  307. }
  308. Dbg("state[%s]",(LPCTSTR)result);
  309. return Error_Succeed;
  310. }
  311. ErrorCodeEnum CSelfCheckerEntity::ConnectToAssistChannel()
  312. {
  313. LOG_FUNCTION();
  314. if (m_pAssChanClient == NULL)
  315. {
  316. m_pAssChanClient = new AssistChannelSelfCheckerClient(this);
  317. ErrorCodeEnum ErrorConn = m_pAssChanClient->Connect();
  318. if (ErrorConn != Error_Succeed) {
  319. m_pAssChanClient->SafeDelete();
  320. m_pAssChanClient = NULL;
  321. return Error_Unexpect;
  322. }
  323. else
  324. {
  325. Dbg("AssistantChannel connected.");
  326. {
  327. ChannelService_BeginState_Sub Sub;
  328. ErrorConn = m_pAssChanClient->BeginState(Sub);
  329. if (ErrorConn != Error_Succeed)
  330. {
  331. Dbg("BeginState biz channel failed!");
  332. m_pAssChanClient->GetFunction()->CloseSession();
  333. m_pAssChanClient = NULL;
  334. return Error_Unexpect;
  335. }
  336. else
  337. {
  338. Dbg("BeginState biz channel success!");
  339. }
  340. }
  341. {
  342. ChannelService_BeginRecv_Sub Sub;
  343. Sub.type = ACM_TYPE_DEVICE;
  344. ErrorConn = m_pAssChanClient->BeginRecv(Sub);
  345. if (ErrorConn != Error_Succeed)
  346. {
  347. Dbg("Begin BeginRecv ACM_TYPE_DEVICE failed!");
  348. m_pAssChanClient->GetFunction()->CloseSession();
  349. m_pAssChanClient = NULL;
  350. return Error_Unexpect;
  351. }
  352. else
  353. {
  354. Dbg("Begin BeginRecv ACM_TYPE_DEVICE success!");
  355. }
  356. }
  357. }
  358. }
  359. return Error_Succeed;
  360. }
  361. ErrorCodeEnum CSelfCheckerEntity::RestartModule(const char* pEntityName)
  362. {
  363. Dbg("to RestartModule:%s",pEntityName);
  364. m_fsm.SetEntityRestarting(pEntityName, true);
  365. CSmartPointer<IEntityFunction> pFunc = GetFunction();
  366. CSmartPointer<IEntityFunctionPrivilege> pFuncPrivilege = pFunc.ConvertCase<IEntityFunctionPrivilege>();
  367. if (pFuncPrivilege == NULL)
  368. {
  369. Dbg("restart mc NoPrivilege");
  370. m_fsm.SetEntityRestarting(pEntityName, false);
  371. return Error_NoPrivilege;
  372. }
  373. CSmartPointer<IAsynWaitSp> spWait;
  374. ErrorCodeEnum eErrCode = Error_Succeed;
  375. eErrCode = pFuncPrivilege->TerminateEntity(pEntityName, spWait);
  376. if (spWait != NULL)
  377. eErrCode = spWait->WaitAnswer(10000);
  378. if (eErrCode != Error_Succeed)
  379. {
  380. Dbg("kill %s %d", pEntityName, eErrCode);
  381. m_fsm.SetEntityRestarting(pEntityName, false);
  382. return eErrCode;
  383. }
  384. Sleep(1000);
  385. eErrCode = pFuncPrivilege->StartEntity(pEntityName, NULL, spWait);
  386. if (spWait != NULL)
  387. eErrCode = spWait->WaitAnswer(10000);
  388. if (eErrCode != Error_Succeed)
  389. {
  390. Dbg("start %s %d", pEntityName, eErrCode);
  391. m_fsm.SetEntityRestarting(pEntityName, false);
  392. return eErrCode;
  393. }
  394. m_fsm.SetEntityRestarting(pEntityName, false);
  395. return Error_Succeed;
  396. }
  397. void CSelfCheckerEntity::OnSysVarEvent(const char* pszKey, const char* pszValue, const char* pszOldValue, const char* pszEntityName)
  398. {
  399. if ((_strnicmp(pszKey, "UIState", strlen("UIState")) == 0))
  400. {
  401. if (_strnicmp(pszValue, "M", strlen("M")) == 0)
  402. {
  403. m_bEverInMainPage = true;
  404. m_fsm.SetEverEnterMainPageFlag();
  405. }
  406. }
  407. }
  408. void AssistChannelSelfCheckerClient::OnMessage(ErrorCodeEnum Error, ChannelService_State_Info &Msg, CSmartPointer<IReleasable> pData)
  409. {
  410. }
  411. void AssistChannelSelfCheckerClient::OnMessage(ErrorCodeEnum Error, ChannelService_Packet_Info &Msg, CSmartPointer<IReleasable> pData)
  412. {
  413. LOG_FUNCTION();
  414. if (Error == Error_Succeed)
  415. {
  416. CSelfCheckerEntity *pEntity = static_cast<CSelfCheckerEntity*>(m_pEntityBase);
  417. switch(Msg.sub_type)
  418. {
  419. case ACM_ENTITY_STATE_REQ:
  420. {
  421. CSimpleStringA result;
  422. pEntity->GetEntityStatusStr(result);
  423. pEntity->MsgPushedByAssChannel(ACM_TYPE_DEVICE,ACM_ENTITY_STATE_ANS,result);
  424. }
  425. break;
  426. default:
  427. break;
  428. }
  429. }
  430. }
  431. SP_BEGIN_ENTITY_MAP()
  432. SP_ENTITY(CSelfCheckerEntity)
  433. SP_END_ENTITY_MAP()