UpgradeMgrFSM.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. #ifndef RVC_MOD_UPGRADEMGR_FSM_H_
  2. #define RVC_MOD_UPGRADEMGR_FSM_H_
  3. #include "SimpleString.h"
  4. #include "SpBase.h"
  5. #include "SpFSM.h"
  6. #include "UpgradeMgrConn.h"
  7. #include <map>
  8. #include <list>
  9. #include <WinBase.h>
  10. using namespace std;
  11. #define NOT_MANUALPACK_ERR 0xF0000001 // 不是手动升级包
  12. #define IS_UPDATEING_ERR 0xF0000002 // 正在升级
  13. #define NULL_PARAM_ERR 0xF0000003 // 参数为空
  14. #define NO_EXIST_MANUAL_PACK_ERR 0xF0000004 // 不存在手动升级包
  15. // 上报状态机(并负责轮询升级)
  16. class CReportMgrFSM : public FSMImpl<CReportMgrFSM>, public IFSMStateHooker
  17. {
  18. public:
  19. enum
  20. {
  21. Event_StartConnect = EVT_USER+1,
  22. Event_Disconnected,
  23. Event_Connected,
  24. Event_ReportState,
  25. Event_StartPoll,
  26. Event_SwitchConfirm,
  27. Event_QueryExecInfo,
  28. Event_ReportSysCustomVer,
  29. Event_SendMD5list,
  30. };
  31. struct ReportStateEvent : public FSMEvent
  32. {
  33. ReportStateEvent(const char *pszPack, const CVersion &version, DWORD dwError, char cState, const char *pszComment = "")
  34. :FSMEvent(Event_ReportState), strPackName(pszPack), InstallVersion(version), dwErrorCode(dwError), cInstallState(cState), strComment(pszComment)
  35. {}
  36. CSimpleStringA strPackName;
  37. CVersion InstallVersion;
  38. DWORD dwErrorCode;
  39. char cInstallState;
  40. CSimpleStringA strComment;
  41. };
  42. struct ReportSysCustomVerEvent : public FSMEvent
  43. {
  44. ReportSysCustomVerEvent(const CSimpleStringA &strPack, const CSimpleStringA &version, const CSimpleStringA &FWID, const CSimpleStringA &SysPatchName)
  45. :FSMEvent(Event_ReportSysCustomVer), strPackName(strPack), strSysCustomVer(version), strFWID(FWID), strSysPatchName(SysPatchName)
  46. {}
  47. CSimpleStringA strPackName;
  48. CSimpleStringA strSysCustomVer;
  49. CSimpleStringA strFWID;
  50. CSimpleStringA strSysPatchName;
  51. };
  52. struct SendMD5ListEvent : public FSMEvent
  53. {
  54. SendMD5ListEvent(const CSimpleStringA &strBlockId)
  55. :FSMEvent(Event_SendMD5list), BlockId(strBlockId)
  56. {}
  57. CSimpleStringA BlockId;
  58. };
  59. struct SwitchConfirmEvent : public FSMEvent
  60. {
  61. CSimpleStringA strPackExecID;
  62. SwitchConfirmEvent(const CSimpleStringA &strExecID)
  63. : FSMEvent(Event_SwitchConfirm), strPackExecID(strExecID){}
  64. };
  65. struct QueryExecInfoEvent : public FSMEvent
  66. {
  67. CSimpleStringA strPackExecID;
  68. QueryExecInfoEvent(const CSimpleStringA &strExecID)
  69. : FSMEvent(Event_QueryExecInfo), strPackExecID(strExecID) {}
  70. };
  71. CReportMgrFSM():m_pConnection(NULL),m_pOuterFSM(NULL),m_LastPollTime(0), m_LastReqTime(0), m_bNeedPoll(false){}
  72. ~CReportMgrFSM(){}
  73. void SetOuterFSM(FSMBase *pFSM){m_pOuterFSM = pFSM;}
  74. private:
  75. virtual ErrorCodeEnum OnInit()
  76. {
  77. AddStateHooker(this);
  78. return Error_Succeed;
  79. }
  80. virtual ErrorCodeEnum OnExit()
  81. {
  82. RemoveStateHooker(this);
  83. return Error_Succeed;
  84. }
  85. enum{s1, s2, s3};
  86. BEGIN_FSM_STATE(CReportMgrFSM)
  87. FSM_STATE_ENTRY(s1, "Disable",s1_on_entry,s1_on_exit,s1_on_event)
  88. FSM_STATE_ENTRY(s2, "Connecting",s2_on_entry,s2_on_exit,s2_on_event)
  89. FSM_STATE_ENTRY(s3, "Connected",s3_on_entry,s3_on_exit,s3_on_event)
  90. END_FSM_STATE()
  91. BEGIN_FSM_RULE(CReportMgrFSM,s1)
  92. FSM_RULE_ENTRY_ANY(s1, s2, Event_StartConnect)
  93. FSM_RULE_ENTRY_ANY(s2, s3, Event_Connected)
  94. FSM_RULE_ENTRY_ANY(s3, s1, Event_Disconnected)
  95. END_FSM_RULE()
  96. virtual void OnStateTrans(int iSrcState, int iDstState);
  97. void s1_on_entry();
  98. void s1_on_exit();
  99. unsigned int s1_on_event(FSMEvent* event);
  100. void s2_on_entry();
  101. void s2_on_exit();
  102. unsigned int s2_on_event(FSMEvent* event);
  103. void s3_on_entry();
  104. void s3_on_exit();
  105. unsigned int s3_on_event(FSMEvent* event);
  106. ErrorCodeEnum SecureClientConnect();
  107. ErrorCodeEnum SecureClientRelease();
  108. ErrorCodeEnum StartPoll();//发起策略查询
  109. public:
  110. UpgradeConnect::CUpgradeMgrConn *m_pConnection;
  111. private:
  112. FSMBase *m_pOuterFSM;
  113. //CUpgradeMgrConn *m_pConnection;
  114. CSmallDateTime m_LastPollTime;
  115. CSmallDateTime m_LastReqTime;
  116. // 等待上报事件
  117. struct CReportInfo
  118. {
  119. CReportInfo(const CSimpleStringA &pack, const CVersion &ver, DWORD error, char state, const CSimpleStringA &comment)
  120. :strPackName(pack), InstallVersion(ver), dwFailCode(error), cInstallState(state), dwStateTime(CSmallDateTime::GetNow()),strComment(comment)
  121. {}
  122. CSimpleStringA strPackName;
  123. CVersion InstallVersion;
  124. DWORD dwStateTime;
  125. DWORD dwFailCode;
  126. char cInstallState;
  127. CSimpleStringA strComment;
  128. };
  129. list<CReportInfo> m_PendingReports; // 等待上报信息
  130. //list<CSimpleStringA> m_ConfirmPacks; // 等待确认安装包
  131. CSimpleStringA m_strToConfirmExecID; // 待确认策略ID
  132. CSimpleStringA m_strToQueryExecID; // 待查询策略ID
  133. bool m_bNeedPoll; // 是否轮询触发
  134. CSimpleStringA m_strSysCustomVer;
  135. CSimpleStringA m_strFWID;
  136. CSimpleStringA m_strSysPatchName;
  137. CSimpleStringA m_strSysCustomPack;
  138. CSimpleStringA m_strBlockId;
  139. };
  140. // 升级管理状态机
  141. class CUpgradeMgrFSM : public FSMImpl<CUpgradeMgrFSM>, public IFSMStateHooker
  142. {
  143. public:
  144. enum
  145. {
  146. Event_EntryPermit = EVT_USER+1,
  147. Event_StartPoll,
  148. Event_EndPoll,
  149. Event_StartDownload,
  150. Event_EndDownload,
  151. Event_StartInstall,
  152. Event_InstallCheck,
  153. Event_EndInstall,
  154. Event_WaitConfirm,
  155. Event_EndConfirm,
  156. Event_PreRestart,
  157. Event_CancelUpgrade,
  158. Event_StartSwitch,
  159. Event_EndQueryExec,
  160. Event_SwitchNow,
  161. /*Event_StartMD5,
  162. Event_ContinueMD5,
  163. Event_RetryMD5,
  164. Event_EndMD5,*/
  165. };
  166. struct RvcCommRetEvent : public FSMEvent
  167. {
  168. RvcCommRetEvent(int nEventID, BYTE *pBuf, int nArrayNum) : FSMEvent(nEventID)
  169. {
  170. param1 = (int)pBuf;
  171. param2 = nArrayNum;
  172. }
  173. virtual ~RvcCommRetEvent()
  174. {
  175. if (param1 != 0 && param2>0)
  176. delete[] (BYTE*)param1;
  177. }
  178. int GetArrayNum()
  179. {
  180. return param2;
  181. }
  182. BYTE* GetRetData()
  183. {
  184. return (BYTE*)param1;
  185. }
  186. };
  187. struct DownloadedEvent: public FSMEvent
  188. {
  189. CSimpleStringA strFileName;
  190. ErrorCodeEnum ErrorCode;
  191. CSimpleStringA strErrorMsg;
  192. DownloadedEvent(const char *pFile, unsigned int error, const char *pszErrMsg)
  193. : FSMEvent(Event_EndDownload),strFileName(pFile), ErrorCode((ErrorCodeEnum)error), strErrorMsg(pszErrMsg) {}
  194. };
  195. struct UpgradeRunCheckEvent :public FSMEvent
  196. {
  197. CSimpleStringA strPackName;
  198. int nErrorCode;
  199. CAutoArray<CSimpleStringA> CoverList;
  200. CSimpleStringA strComment;
  201. UpgradeRunCheckEvent(const CSimpleStringA& pack, int error, const CAutoArray<CSimpleStringA> arr,const CSimpleStringA &comment)
  202. : FSMEvent(Event_InstallCheck), strPackName(pack), nErrorCode(error), CoverList(arr), strComment(comment)
  203. {}
  204. };
  205. struct UpgradeRunDoneEvent:public FSMEvent
  206. {
  207. CSimpleStringA strPackName;
  208. int nErrorCode;
  209. bool bSysInstall;
  210. bool bLightPack;
  211. CSimpleStringA strNewVersion;
  212. CSimpleStringA strFWID;
  213. CSimpleStringA strSysPatchName;
  214. CSimpleStringA strComment;
  215. UpgradeRunDoneEvent(const CSimpleStringA &pack, int error, bool sysInstall, bool lightInstall, const CSimpleStringA &newVer, const CSimpleStringA &newFWID, const CSimpleStringA &newSysPatchName,const CSimpleStringA &comment)
  216. :FSMEvent(Event_EndInstall), strPackName(pack), nErrorCode(error), bSysInstall(sysInstall), bLightPack(lightInstall), strNewVersion(newVer), strFWID(newFWID), strSysPatchName(newSysPatchName),strComment(comment)
  217. {}
  218. };
  219. // 待安装包信息
  220. struct CPendingPackInfo
  221. {
  222. CSimpleStringA strPackName;
  223. BYTE nLevel; // 3级优先级
  224. char cTriggerType; // 更新触发方式 I(立即)|T(定时)|C(控制)
  225. DWORD dwTriggerTimer; // 触发时间,2000后的秒数
  226. char cPendingState; // 初始状态N、下载中U、已下载D、安装中I、等待确认C、等待切换S
  227. bool bSysInstall; // 是否系统升级
  228. CSimpleStringA strPackExecID; // 执行策略
  229. };
  230. // 已安装或取消安装包信息
  231. struct CInstalledPackInfo
  232. {
  233. CSimpleStringA strPackName;
  234. CSmallDateTime InstallTime;
  235. char cInstallState; // 安装失败F、已经安装D、取消安装C
  236. };
  237. public:
  238. CUpgradeMgrFSM();
  239. ~CUpgradeMgrFSM(){}
  240. void PushCancelUpgradePack(const CSimpleStringA &strPackName);
  241. bool HasDownloadingPack();
  242. bool HasRunningUpdate();
  243. ErrorCodeEnum RegistLocalPack(const CSimpleStringA &strPackName);
  244. DWORD RegistManualPack(const CSimpleStringA &strPackName);
  245. DWORD GetManualPacks(CSimpleStringA &strManualPacks);
  246. ErrorCodeEnum GetUpgradeState(bool &bInstalling, CSimpleStringA &strPackFile, CSimpleStringA &strExecID,
  247. char &nInstallState, bool &bSysInstall, bool &bLightPack, CSimpleStringA &strNewVersion);
  248. char GetInstallState(int nState);
  249. bool UpdateManualTaskState();
  250. bool IsManualInstallOk(CSimpleStringA strPackName);
  251. bool GetServerPackUpgradeResult(const char *pszPackageName, CSimpleStringA &strVerison, CSimpleStringA &strErrMsg);
  252. bool GetFirmwarePackUpgradeResult(const char *pszPackageName, CSimpleStringA &strVerison, CSimpleStringA &strErrMsg);
  253. CSimpleStringA GetSysPatchNameFromPackName(const char *pszPackName);
  254. ErrorCodeEnum GetPackInstallFailedCnts(UINT64 &nCnts);
  255. ErrorCodeEnum SetPackInstallFailedCnts(UINT64 nCnts);
  256. ErrorCodeEnum GetInstallFailedPackName(CSimpleStringA &strPackName);
  257. ErrorCodeEnum SetInstallFailedPackName(CSimpleStringA strPackName);
  258. ErrorCodeEnum SetInstallFailedPackInfo(CSimpleStringA strPackName);
  259. ErrorCodeEnum GetInstallFailedPackInfo(CSimpleStringA &strPackName);
  260. ErrorCodeEnum UpdateInstallFailedPackInfo(CSimpleStringA strPackName);
  261. void SendSM3ListEvent();
  262. private:
  263. virtual ErrorCodeEnum OnInit();
  264. virtual ErrorCodeEnum OnExit();
  265. virtual void OnStateTrans(int iSrcState, int iDstState);
  266. enum{s1, s2, s3, s4, s5, s6};
  267. BEGIN_FSM_STATE(CUpgradeMgrFSM)
  268. FSM_STATE_ENTRY(s1, "Disable",s1_on_entry,s1_on_exit,s1_on_event)
  269. FSM_STATE_ENTRY(s2, "Poll",s2_on_entry,s2_on_exit,s2_on_event)
  270. FSM_STATE_ENTRY(s3, "Download",s3_on_entry,s3_on_exit,s3_on_event)
  271. FSM_STATE_ENTRY(s4, "Install", s4_on_entry, s4_on_exit, s4_on_event)
  272. FSM_STATE_ENTRY(s5, "Confirm", s5_on_entry, s5_on_exit, s5_on_event)
  273. FSM_STATE_ENTRY(s6, "Switch", s6_on_entry, s6_on_exit, s6_on_event)
  274. END_FSM_STATE()
  275. BEGIN_FSM_RULE(CUpgradeMgrFSM,s1)
  276. FSM_RULE_ENTRY(s1, s2, Event_EntryPermit, 0)
  277. FSM_RULE_ENTRY(s1, s3, Event_EntryPermit, 3)
  278. FSM_RULE_ENTRY(s1, s4, Event_EntryPermit, 4)
  279. FSM_RULE_ENTRY(s1, s5, Event_EntryPermit, 5)
  280. FSM_RULE_ENTRY(s1, s6, Event_EntryPermit, 6)
  281. FSM_RULE_ENTRY_ANY(s2, s3, Event_StartDownload)
  282. FSM_RULE_ENTRY_ANY(s2, s4, Event_StartInstall)
  283. FSM_RULE_ENTRY_ANY(s2, s5, Event_WaitConfirm)
  284. FSM_RULE_ENTRY_ANY(s3, s4, Event_StartInstall)
  285. FSM_RULE_ENTRY_ANY(s3, s2, Event_StartPoll)
  286. FSM_RULE_ENTRY_ANY(s4, s5, Event_WaitConfirm)
  287. FSM_RULE_ENTRY_ANY(s4, s2, Event_StartPoll)
  288. FSM_RULE_ENTRY_ANY(s4, s1, Event_PreRestart)
  289. FSM_RULE_ENTRY_ANY(s5, s6, Event_StartSwitch)
  290. FSM_RULE_ENTRY_ANY(s5, s2, Event_StartPoll)
  291. FSM_RULE_ENTRY_ANY(s6, s1, Event_PreRestart)
  292. FSM_RULE_ENTRY_ANY(s6, s2, Event_StartPoll)
  293. END_FSM_RULE()
  294. private:
  295. void s1_on_entry();
  296. void s1_on_exit();
  297. unsigned int s1_on_event(FSMEvent* event);
  298. void s2_on_entry();
  299. void s2_on_exit();
  300. unsigned int s2_on_event(FSMEvent* event);
  301. void s3_on_entry();
  302. void s3_on_exit();
  303. unsigned int s3_on_event(FSMEvent* event);
  304. void s4_on_entry();
  305. void s4_on_exit();
  306. unsigned int s4_on_event(FSMEvent* event);
  307. void s5_on_entry();
  308. void s5_on_exit();
  309. unsigned int s5_on_event(FSMEvent* event);
  310. void s6_on_entry();
  311. void s6_on_exit();
  312. unsigned int s6_on_event(FSMEvent* event);
  313. bool IsPackDownloaded(const char *pPackName);
  314. bool IsPackCancelled(const char *pPackName);
  315. bool IsPackInstalled(const char *pPackName);
  316. bool IsPackFialOver3Times(const char *pPackName);
  317. ErrorCodeEnum LoadPersistInfo();
  318. CPendingPackInfo* GetPriorInstallPack();
  319. bool CheckIfPackConfirmed();
  320. bool CheckIfInstallCancelled();
  321. void CancelUpgradePacks(RvcCommRetEvent *pEvent);
  322. ErrorCodeEnum SwitchUpgradeNow();
  323. bool CheckIfCanSwitchNow();
  324. bool WriteInstallLog(const char *pszPackName, const char *pszLogText);
  325. bool RemoveInstallLog(const char *pszPackName);
  326. ErrorCodeEnum SetRunConfigStrValue(const char *pszSection, const char *pszKey, const char *pszValue);
  327. ErrorCodeEnum GetRunConfigStrValue(const char *pszSection, const char *pszKey, CSimpleStringA &strValue);
  328. ErrorCodeEnum SetRunConfigHexIntValue(const char *pszSection, const char *pszKey, UINT64 nValue);
  329. ErrorCodeEnum GetRunConfigHexIntValue(const char *pszSection, const char *pszKey, UINT64 &nValue);
  330. private:
  331. CReportMgrFSM m_ReportFSM;
  332. CSimpleStringA m_strIntallingPack; // 当前正在安装的项
  333. int m_nIntallingPackType; // 当前正在安装的项的包类型,1,自动安装;2,手动安装
  334. CSimpleStringA m_strNewVersion; // 新版本对应目录
  335. map<CSimpleStringA, CPendingPackInfo> m_PendingPacks; // 等待安装包
  336. list<CSimpleStringA> m_CancelledPacks; // 被主动取消安装包
  337. list<CSimpleStringA> m_InstallFailPacks; // 安装或检查失败的包,防止重复安装
  338. list<CSimpleStringA> m_ManualPendingPacks; // 等待手动安装包
  339. // map<CSimpleStringA, CInstalledPackInfo> m_InstalledPacks; // 当前版本已安装包
  340. //list<CSimpleStringA> m_ConfirmedPacks; // 提前确认安装包
  341. DWORD m_dwDownloadTime; // 当前状态开始时间
  342. unsigned int m_nPackInstallFailCnts;
  343. public:
  344. list<CSimpleStringA> m_DepWhitelist; //dep文件夹白名单
  345. list<CSimpleStringA> m_DirBlacklist; //整体文件夹黑名单
  346. list<CSimpleStringA> m_FileBlacklist; //整体文件黑名单
  347. };
  348. #endif //RVC_MOD_UPGRADEMGR_FSM_H_