UpgradeMonitor.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * UpgradeMonitor.h
  3. *
  4. * Created on: Aug 4, 2017
  5. * Author: guoxs
  6. */
  7. #ifndef _OS_UPGRADE_MONITOR_H_
  8. #define _OS_UPGRADE_MONITOR_H_
  9. #include <stdint.h>
  10. #include <string>
  11. #include <vector>
  12. #include "MountMonitor.h"
  13. class UpgradeMonitor : Thread {
  14. public:
  15. virtual ~UpgradeMonitor();
  16. void startMonitoring();
  17. void stopMonitoring();
  18. bool startUpgrade();
  19. void stopUpgrade();
  20. bool isUpgrading() const;
  21. bool needUpgrade() const;
  22. bool checkUpgrade();
  23. bool checkUpgradeFile(const char *pPath);
  24. static UpgradeMonitor* getInstance();
  25. public:
  26. typedef enum {
  27. E_UPGRADE_STATUS_START,
  28. E_UPGRADE_STATUS_SUCCESS,
  29. E_UPGRADE_STATUS_ERROR
  30. } EUpgradeStatus;
  31. typedef enum {
  32. E_UPGRADE_TYPE_UBOOT,
  33. E_UPGRADE_TYPE_BOOT,
  34. E_UPGRADE_TYPE_SYSTEM,
  35. E_UPGRADE_TYPE_RES,
  36. E_UPGRADE_TYPE_DATA,
  37. E_UPGRADE_TYPE_LOGO,
  38. E_UPGRADE_TYPE_ENV,
  39. E_UPGRADE_TYPE_PRIVATE,
  40. E_UPGRADE_TYPE_FULL
  41. } ESystemUpgradeType;
  42. /**
  43. * 分区信息
  44. */
  45. typedef struct {
  46. uint8_t partn; // 分区号
  47. uint8_t reserve[3]; // 保留
  48. uint8_t offset[4]; // 基于head的偏移
  49. uint8_t imageSize[4]; // 分区镜像大小
  50. uint8_t imageHead[16]; // image头数据
  51. } SPartInfo;
  52. /**
  53. * 扩展信息
  54. */
  55. typedef struct {
  56. uint8_t size[4]; // 额外信息长度
  57. uint8_t perm; // 权限
  58. uint8_t type[4]; // 机型
  59. uint8_t flag; // 标记
  60. uint8_t attr; // 加密信息相关
  61. uint8_t reserve[509]; // append
  62. uint8_t crc32[4]; // 校验
  63. } SExtendInfo;
  64. typedef struct {
  65. ESystemUpgradeType upgradeType;
  66. std::string upgradeFilePath;
  67. std::string partName;
  68. SPartInfo partInfo;
  69. bool needUpgrade;
  70. } SSystemUpgradeInfo;
  71. /**
  72. * TS升级文件信息
  73. */
  74. typedef struct {
  75. uint8_t hsize; // 等于tsinfo大小
  76. uint8_t type; // 0=未知型号、1=TS_GT911
  77. uint8_t flags; // BIT(0)=强制升级位(1表示强制升级使能)、BIT(1)-BIT(7)保留
  78. uint8_t reserve; // 保留
  79. uint8_t version[4]; // ts固件版本号
  80. uint8_t pix[4]; // ts分辨率,H16=x L16=y,高16表示x,低16表示y
  81. uint8_t date[4]; // ts固件生成日期,0x20171104 = 2017年11月4号
  82. uint8_t dataLen[4]; // 数据长度
  83. uint8_t hmd5[16]; // tsdata的MD5校验,不包含tsinfo,#define MD5_DIGEST_LENGTH 16
  84. uint8_t *pData; // 数据
  85. bool needUpgrade;
  86. } STSUpgradeInfo;
  87. /**
  88. * ts类型
  89. */
  90. enum ts_type {
  91. TS_GT911 = 1,
  92. };
  93. class IUpgradeStatusListener {
  94. public:
  95. virtual ~IUpgradeStatusListener() { };
  96. virtual void notify(int what, int status, const char *msg) = 0;
  97. };
  98. void setUpgradeStatusListener(IUpgradeStatusListener *pListener) {
  99. mUpgradeStatusListenerPtr = pListener;
  100. }
  101. std::vector<SSystemUpgradeInfo*>& getUpgradeInfoList() {
  102. return mSystemUpgradeInfoList;
  103. }
  104. const STSUpgradeInfo& getTSUpgradeInfo() const { return mTSUpgradeInfo; }
  105. protected:
  106. virtual bool threadLoop();
  107. private:
  108. UpgradeMonitor();
  109. bool checkSystemUpgradeFile();
  110. bool checkTSUpgradeFile();
  111. bool checkTouchCalib(const char *pPath);
  112. bool checkRestart(const char *pPath);
  113. bool checkImgMD5(FILE *pf, const SPartInfo &partInfo);
  114. bool checkMtdDevMD5(const char *pDev, FILE *pf, const SPartInfo &partInfo);
  115. void resetSystemUpgradeInfo();
  116. void resetTSUpgradeInfo();
  117. void initMtdInfoList();
  118. const char* getMtdDevByName(const char *pName) const;
  119. bool writeMtdDev(const char *pDev, FILE *pf, const SSystemUpgradeInfo &upgradeInfo);
  120. private:
  121. class UpgradeMountListener : public MountMonitor::IMountListener {
  122. public:
  123. UpgradeMountListener(UpgradeMonitor *pUM) : mUMPtr(pUM) {
  124. }
  125. virtual void notify(int what, int status, const char *msg) {
  126. switch (status) {
  127. case MountMonitor::E_MOUNT_STATUS_MOUNTED:
  128. if (mUMPtr->checkRestart(msg) ||
  129. mUMPtr->checkTouchCalib(msg) ||
  130. mUMPtr->checkUpgradeFile(msg)) {
  131. // do nothing
  132. }
  133. break;
  134. case MountMonitor::E_MOUNT_STATUS_REMOVE:
  135. break;
  136. }
  137. }
  138. private:
  139. UpgradeMonitor *mUMPtr;
  140. };
  141. private:
  142. UpgradeMountListener mUpgradeMountListener;
  143. IUpgradeStatusListener *mUpgradeStatusListenerPtr;
  144. bool mHasStartMonitor;
  145. std::vector<SSystemUpgradeInfo*> mSystemUpgradeInfoList;
  146. std::string mUpgradeDir;
  147. bool mIsUpgrading;
  148. typedef struct {
  149. std::string dev;
  150. std::string name;
  151. } SMtdInfo;
  152. std::vector<SMtdInfo> mMtdInfoList;
  153. SExtendInfo mExtendInfo;
  154. STSUpgradeInfo mTSUpgradeInfo;
  155. const char *mErrorCode;
  156. };
  157. #define UPGRADEMONITOR UpgradeMonitor::getInstance()
  158. #endif /* _OS_UPGRADE_MONITOR_H_ */