mod_ResourceWatcher.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. #pragma once
  2. #include "modVer.h"
  3. #include "SpTest.h"
  4. #include "ResourceWatcherFSM.h"
  5. #include "UOSTools.hpp"
  6. class ResourceWatcherEntity;
  7. #define ENT_TIMERID_CHECK_SOGOU_INPUT_INSTALLED_STATE 1
  8. #define ENT_TIMERINTERVAL_CHECK_SOGOU_INPUT_INSTALLED_STATE 60 * 1000
  9. #define ENT_TIMERID_CHECK_SOGOU_INPUT_PROCESS_STATUS_CHANGE 3
  10. #define ENT_TIMERINTERVAL_CHECK_SOGOU_INPUT_PROCESS_STATUS_CHANGE 30 * 1000
  11. class ResourceWatcherServiceSession : public ResourceWatcherService_ServerSessionBase
  12. {
  13. public:
  14. ResourceWatcherServiceSession(ResourceWatcherEntity* pEntity) : m_pEntity(pEntity) {}
  15. virtual ~ResourceWatcherServiceSession() {}
  16. virtual void Handle_Fetch(SpReqAnsContext<ResourceWatcherService_Fetch_Req,
  17. ResourceWatcherService_Fetch_Ans>::Pointer ctx);
  18. virtual void Handle_GetDevInfo(SpReqAnsContext<ResourceWatcherService_GetDevInfo_Req,
  19. ResourceWatcherService_GetDevInfo_Ans>::Pointer ctx);
  20. virtual void Handle_GetCardSwiper(SpReqAnsContext<ResourceWatcherService_GetCardSwiper_Req, ResourceWatcherService_GetCardSwiper_Ans>::Pointer ctx);
  21. virtual void Handle_GetCpuType(SpReqAnsContext<ResourceWatcherService_GetCpuType_Req, ResourceWatcherService_GetCpuType_Ans>::Pointer ctx);
  22. virtual void Handle_OperateFile(SpReqAnsContext<ResourceWatcherService_OperateFile_Req, ResourceWatcherService_OperateFile_Ans>::Pointer ctx);
  23. virtual void Handle_ExtractEventLog(SpReqAnsContext<ResourceWatcherService_ExtractEventLog_Req, ResourceWatcherService_ExtractEventLog_Ans>::Pointer ctx);
  24. virtual void Handle_UpdateDNS(SpReqAnsContext<ResourceWatcherService_UpdateDNS_Req, ResourceWatcherService_UpdateDNS_Ans>::Pointer ctx);
  25. virtual void Handle_GetNetworkInfo(SpReqAnsContext<ResourceWatcherService_GetNetworkInfo_Req, ResourceWatcherService_GetNetworkInfo_Ans>::Pointer ctx);
  26. virtual void Handle_GetThirdPartyInstallState(SpReqAnsContext<ResourceWatcherService_GetThirdPartyInstallState_Req, ResourceWatcherService_GetThirdPartyInstallState_Ans>::Pointer ctx);
  27. virtual void Handle_InstallThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req, ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx);
  28. virtual void Handle_BizLinkDetect(SpReqAnsContext<ResourceWatcherService_BizLinkDetect_Req, ResourceWatcherService_BizLinkDetect_Ans>::Pointer ctx);
  29. virtual void Handle_CheckNetType(SpReqAnsContext<ResourceWatcherService_CheckNetType_Req, ResourceWatcherService_CheckNetType_Ans>::Pointer ctx);
  30. virtual void Handle_GetBizLinks(SpReqAnsContext<ResourceWatcherService_GetBizLinks_Req, ResourceWatcherService_GetBizLinks_Ans>::Pointer ctx);
  31. virtual void Handle_GetTerminalVersionList(SpReqAnsContext<ResourceWatcherService_GetTerminalVersionList_Req, ResourceWatcherService_GetTerminalVersionList_Ans>::Pointer ctx);
  32. virtual void Handle_ManipulateVersion(SpReqAnsContext<ResourceWatcherService_ManipulateVersion_Req, ResourceWatcherService_ManipulateVersion_Ans>::Pointer ctx);
  33. virtual void Handle_UninstallThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_UninstallThirdPartyProgram_Req, ResourceWatcherService_UninstallThirdPartyProgram_Ans>::Pointer ctx);
  34. virtual void Handle_RestartThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_RestartThirdPartyProgram_Req, ResourceWatcherService_RestartThirdPartyProgram_Ans>::Pointer ctx);
  35. virtual void Handle_ProcessDetectThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_ProcessDetectThirdPartyProgram_Req, ResourceWatcherService_ProcessDetectThirdPartyProgram_Ans>::Pointer ctx);
  36. private:
  37. ResourceWatcherEntity* m_pEntity;
  38. };
  39. struct CPUTestTask : public ITaskSp
  40. {
  41. void Process()
  42. {
  43. while (true) {
  44. int cal = 123232323 + 23434324;
  45. double t = 334243.334 / 34343.2;
  46. }
  47. }
  48. };
  49. class ResourceWatcherEntity : public CEntityBase, public IBroadcastListener, public ITimerListener, public ISysVarListener
  50. {
  51. public:
  52. ResourceWatcherEntity():m_bInitMode(FALSE) {}
  53. virtual ~ResourceWatcherEntity() {}
  54. virtual const char* GetEntityName() const { return "ResourceWatcher"; }
  55. const char* GetEntityVersion() const { return MODULE_VERSION_FULL; }
  56. ON_ENTITYT_TEST()
  57. virtual CServerSessionBase* OnNewSession(const char* , const char * )
  58. {
  59. LOG_FUNCTION();
  60. return new ResourceWatcherServiceSession(this);
  61. }
  62. virtual void OnPreStart(CAutoArray<CSimpleStringA> strArgs,
  63. CSmartPointer<ITransactionContext> pTransactionContext)
  64. {
  65. LOG_FUNCTION();
  66. ErrorCodeEnum errorCode = Error_Succeed;
  67. CSimpleStringA strtermState;
  68. GetFunction()->GetSysVar("TerminalStage", strtermState);
  69. if (strtermState.IsStartWith("Z=")) {
  70. Dbg("aaa in config mode");
  71. m_bInitMode = TRUE;
  72. }
  73. errorCode = m_fsm.Init(this);
  74. if (errorCode == Error_Succeed) {
  75. GetFunction()->RegistSysVarEvent("UIState", this);
  76. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  77. CSystemStaticInfo staticInfo;
  78. spEntityFunction->GetSystemStaticInfo(staticInfo);
  79. if (!staticInfo.strMachineType.IsNullOrEmpty() && !staticInfo.strMachineType.Compare("RVC.Pad", true)) {
  80. errorCode = spEntityFunction->SubscribeBroadcast("CardSwiper", NULL, this, m_uiCardSwiperStatusListener);
  81. if (errorCode != Error_Succeed) {
  82. LOG_TRACE("Subscribe CardSwiper evt failed 0x%x", errorCode);
  83. pTransactionContext->SendAnswer(errorCode);
  84. return;
  85. }
  86. Dbg("SubScribe CardSwiper status broadcast suc.");
  87. }
  88. }
  89. pTransactionContext->SendAnswer(errorCode);
  90. }
  91. void OnStarted()
  92. {
  93. m_fsm.AfterInit();
  94. if (!m_bInitMode) {
  95. CSmartPointer<IEntityFunction> spEntityFunction = GetFunction();
  96. spEntityFunction->SetTimer(ENT_TIMERID_CHECK_SOGOU_INPUT_INSTALLED_STATE,
  97. this, ENT_TIMERINTERVAL_CHECK_SOGOU_INPUT_INSTALLED_STATE);
  98. }
  99. }
  100. /*ignore*/
  101. virtual void OnPrePause(CSmartPointer<ITransactionContext> pTransactionContext)
  102. {
  103. GetFunction()->PostThreadPoolTask(new CPUTestTask());
  104. //UOS::MonitorInfo info;
  105. //info.name = "VGA-0";
  106. //info.nResolutionX = 1280;
  107. //info.nResolutionY = 1024;
  108. //info.isPrimary = true;
  109. //ConfigMonitorSetting(info);
  110. pTransactionContext->SendAnswer(Error_Succeed);
  111. }
  112. /*ignore*/
  113. virtual void OnPreContinue(CSmartPointer<ITransactionContext> pTransactionContext)
  114. {
  115. //UOS::MonitorInfo info;
  116. //info.name = "HDMI-A-0";
  117. //info.nResolutionX = 1024;
  118. //info.nResolutionY = 768;
  119. //info.isPrimary = false;
  120. //info.other = "VGA-0";
  121. //info.posDirecttion = 2;
  122. //ConfigMonitorSetting(info);
  123. pTransactionContext->SendAnswer(Error_Succeed);
  124. }
  125. /*invoke fsm.onExit(), invoked when entity will be closed*/
  126. virtual void OnPreClose(EntityCloseCauseEnum eCloseCause,
  127. CSmartPointer<ITransactionContext>pTransactionContext)
  128. {
  129. ErrorCodeEnum errorCode = m_fsm.OnExit();
  130. pTransactionContext->SendAnswer(errorCode);
  131. }
  132. virtual void OnSelfTest(EntityTestEnum eTestType,
  133. CSmartPointer<ITransactionContext>pTransactionContext)
  134. {
  135. m_fsm.SelfTest(eTestType, pTransactionContext);
  136. }
  137. virtual bool IsService() const
  138. {
  139. return true;
  140. }
  141. virtual bool IsMultiThread() const
  142. {
  143. return false;
  144. }
  145. void Fetch(SpReqAnsContext<ResourceWatcherService_Fetch_Req,
  146. ResourceWatcherService_Fetch_Ans>::Pointer ctx)
  147. {
  148. FetchEvent* evt = new FetchEvent();
  149. evt->ctx = ctx;
  150. m_fsm.PostEventFIFO(evt);
  151. }
  152. void GetDevInfo(SpReqAnsContext<ResourceWatcherService_GetDevInfo_Req,
  153. ResourceWatcherService_GetDevInfo_Ans>::Pointer ctx)
  154. {
  155. ctx->Answer(Error_NotImpl);
  156. }
  157. void GetCSwiperStatus(SpReqAnsContext<ResourceWatcherService_GetCardSwiper_Req,
  158. ResourceWatcherService_GetCardSwiper_Ans>::Pointer ctx)
  159. {
  160. ctx->Ans.status = m_fsm.GetCardSwiperVal();
  161. ctx->Answer(Error_Succeed);
  162. }
  163. void GetCPUType(SpReqAnsContext<ResourceWatcherService_GetCpuType_Req,
  164. ResourceWatcherService_GetCpuType_Ans>::Pointer ctx)
  165. {
  166. m_fsm.GetCpuType(ctx);
  167. }
  168. void BizLinkDetect(SpReqAnsContext<ResourceWatcherService_BizLinkDetect_Req,
  169. ResourceWatcherService_BizLinkDetect_Ans>::Pointer ctx)
  170. {
  171. m_fsm.BizLinkDetect(ctx);
  172. }
  173. void CheckNetType(SpReqAnsContext<ResourceWatcherService_CheckNetType_Req,
  174. ResourceWatcherService_CheckNetType_Ans>::Pointer ctx)
  175. {
  176. m_fsm.CheckNetType(ctx);
  177. }
  178. void GetBizLinks(SpReqAnsContext<ResourceWatcherService_GetBizLinks_Req,
  179. ResourceWatcherService_GetBizLinks_Ans>::Pointer ctx)
  180. {
  181. m_fsm.GetBizLinks(ctx);
  182. }
  183. void GetTerminalVersionList(SpReqAnsContext<ResourceWatcherService_GetTerminalVersionList_Req, ResourceWatcherService_GetTerminalVersionList_Ans>::Pointer ctx);
  184. void ManipulateVersion(SpReqAnsContext<ResourceWatcherService_ManipulateVersion_Req, ResourceWatcherService_ManipulateVersion_Ans>::Pointer ctx);
  185. void UninstallThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_UninstallThirdPartyProgram_Req, ResourceWatcherService_UninstallThirdPartyProgram_Ans>::Pointer ctx);
  186. void OperateFile(
  187. SpReqAnsContext<ResourceWatcherService_OperateFile_Req,
  188. ResourceWatcherService_OperateFile_Ans>::Pointer ctx)
  189. {
  190. OperateFileEvent* evt = new OperateFileEvent();
  191. evt->m_ctx = ctx;
  192. m_fsm.PostEventFIFO(evt);
  193. }
  194. void RetrieveEventLog(SpReqAnsContext<ResourceWatcherService_ExtractEventLog_Req,
  195. ResourceWatcherService_ExtractEventLog_Ans>::Pointer ctx)
  196. {
  197. LOG_FUNCTION();
  198. #if defined(RVC_OS_WIN)
  199. m_fsm.PostEventFIFO(new GetEventLogEvent(ctx));
  200. #else
  201. ctx->Answer(Error_NotSupport);
  202. #endif //RVC_OS_WIN
  203. }
  204. void UpdateDNS(SpReqAnsContext<ResourceWatcherService_UpdateDNS_Req, ResourceWatcherService_UpdateDNS_Ans>::Pointer ctx);
  205. void GetNetworkInfo(SpReqAnsContext<ResourceWatcherService_GetNetworkInfo_Req, ResourceWatcherService_GetNetworkInfo_Ans>::Pointer ctx);
  206. void GetThirdPartyInstallState(SpReqAnsContext<ResourceWatcherService_GetThirdPartyInstallState_Req, ResourceWatcherService_GetThirdPartyInstallState_Ans>::Pointer ctx);
  207. void InstallThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_InstallThirdPartyProgram_Req, ResourceWatcherService_InstallThirdPartyProgram_Ans>::Pointer ctx);
  208. void RestartThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_RestartThirdPartyProgram_Req,
  209. ResourceWatcherService_RestartThirdPartyProgram_Ans>::Pointer ctx);
  210. void ProcessDetectThirdPartyProgram(SpReqAnsContext<ResourceWatcherService_ProcessDetectThirdPartyProgram_Req,
  211. ResourceWatcherService_ProcessDetectThirdPartyProgram_Ans>::Pointer ctx);
  212. virtual void OnSysVarEvent(const char* pszKey, const char* pszValue, const char* pszOldValue, const char* pszEntityName);
  213. #if defined(_MSC_VER)
  214. SP_BEGIN_MSG_DISPATCH_MAP(ResourceWatcherEntity)
  215. SP_BEGIN_ENTITY_MSG("CardSwiper")
  216. SP_MSG_HANDLE_NS(CardSwiper, ConnectStatus, OnCardSwiperConnectStatus)
  217. SP_END_ENTITY_MSG()
  218. SP_END_MSG_DISPATCH_MAP()
  219. #else
  220. virtual void OnBroadcastEvent(CUUID SubID, const char* pszEntityName, DWORD dwMessageId, DWORD dwMessageSignature, CAutoBuffer Buffer)
  221. {
  222. }
  223. #endif //_MSC_VER
  224. private:
  225. void OnCardSwiperConnectStatus(const char *pszEntityName, DWORD dwMessageId, DWORD dwMessageSignature, CardSwiper::ConnectStatus &evt)
  226. {
  227. Dbg("OnCardSwiperConnectStatus %d", evt.status);
  228. QueryCardSwiper cardswiperStatus;
  229. cardswiperStatus.status = evt.status;
  230. m_fsm.m_cardswiperStatus = evt.status;
  231. SpSendBroadcast(GetFunction(), SP_MSG_OF(QueryCardSwiper), SP_MSG_SIG_OF(QueryCardSwiper),
  232. cardswiperStatus);
  233. }
  234. ErrorCodeEnum DoCheckInstallStateJob();
  235. void DoCheckSogouProcessStatus();
  236. void OnTimeout(DWORD dwTimerID)
  237. {
  238. if(dwTimerID == ENT_TIMERID_CHECK_SOGOU_INPUT_INSTALLED_STATE) {
  239. CSimpleStringA strState;
  240. auto err = GetFunction()->GetSysVar("UIState",strState);
  241. /** 让进入首页之后才获取输入法信息 [Gifur@20211212]*/
  242. if(/*TRUE || */err == Error_Succeed && strState == "M") {
  243. Dbg("to check Sogou input install state...");
  244. if(Error_Succeed == DoCheckInstallStateJob()) {
  245. GetFunction()->KillTimer(ENT_TIMERID_CHECK_SOGOU_INPUT_INSTALLED_STATE);
  246. GetFunction()->SetTimer(ENT_TIMERID_CHECK_SOGOU_INPUT_PROCESS_STATUS_CHANGE,
  247. this, ENT_TIMERINTERVAL_CHECK_SOGOU_INPUT_PROCESS_STATUS_CHANGE);
  248. }
  249. } else {
  250. Dbg("Get UIState result: %s, %s", SpStrError(err), strState.GetData());
  251. }
  252. } else if (dwTimerID == ENT_TIMERID_CHECK_SOGOU_INPUT_PROCESS_STATUS_CHANGE) {
  253. DoCheckSogouProcessStatus();
  254. }
  255. else {
  256. Dbg("Unkonwn timer id: %u", dwTimerID);
  257. }
  258. }
  259. ErrorCodeEnum ConfigMonitorSetting(const UOS::MonitorInfo& config);
  260. std::vector<std::string> GetUserNameList(bool bExcludeRoot = false);
  261. ErrorCodeEnum GetSogouPkgDirPath(CSimpleStringA& strPkgPath);
  262. ErrorCodeEnum RunShellScript(LPCTSTR cmdline);
  263. std::string DoCheckCertainProcessStatus(const CAutoArray<CSimpleStringA>& pName);
  264. private:
  265. ResourceWatcherFSM m_fsm;
  266. CUUID m_uiCardSwiperStatusListener;
  267. BOOL m_bInitMode;
  268. };