UpgradeMgrFSM.h 13 KB

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