UpgradeMgrFSM.h 14 KB

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