This commit is contained in:
Fancy code 2024-05-07 18:58:36 +08:00
parent 275c1aa59d
commit e82f4f43f8
5 changed files with 60 additions and 18 deletions

View File

@ -8,9 +8,9 @@
**32字节头定义** **32字节头定义**
| 打包时间6 | 版本号4 | 型号代码2 | 项目代码2 | 升级类型1 | 预留17 | | 打包时间6 | 版本号4 | 型号代码2 | 项目代码2 | 升级类型1 | 预留15 | 校验码尾2 |
|----|----|----|----|----|----| |----|----|----|----|----|----|----|
|----|----|----|----|----|----| |----|----|----|----|----|----|----|
**升级类型说明** **升级类型说明**

View File

@ -22,7 +22,8 @@ typedef struct __attribute__((packed)) upgrade_file_header
unsigned char product[2]; unsigned char product[2];
unsigned char project[2]; unsigned char project[2];
unsigned char upgradeType[1]; unsigned char upgradeType[1];
unsigned char reserved[17]; unsigned char reserved[15];
unsigned char checkCode[2];
} UpgradeFileHeader; } UpgradeFileHeader;
enum class UpgradeType enum class UpgradeType
{ {
@ -45,5 +46,6 @@ public:
StatusCode MoveUpgradeFile(const char *sourceFile, const char *targetFile); StatusCode MoveUpgradeFile(const char *sourceFile, const char *targetFile);
private: private:
void PrintfHeader(const UpgradeFileHeader &header);
}; };
#endif #endif

View File

@ -34,6 +34,7 @@ StatusCode UpgradeBase::CheckUpgradeFile(const char *fileName, UpgradeFileHeader
fclose(file); fclose(file);
return CreateStatusCode(STATUS_CODE_NOT_OK); return CreateStatusCode(STATUS_CODE_NOT_OK);
} }
PrintfHeader(header);
return CheckFileHeader(header); return CheckFileHeader(header);
} }
StatusCode UpgradeBase::MoveUpgradeFile(const char *sourceFile, const char *targetFile) StatusCode UpgradeBase::MoveUpgradeFile(const char *sourceFile, const char *targetFile)
@ -83,4 +84,29 @@ StatusCode UpgradeBase::MoveUpgradeFile(const char *sourceFile, const char *targ
LogInfo("File processed successfully.\n"); LogInfo("File processed successfully.\n");
return CreateStatusCode(STATUS_CODE_OK); return CreateStatusCode(STATUS_CODE_OK);
}
void UpgradeBase::PrintfHeader(const UpgradeFileHeader &header)
{
printf("=====================================\n");
printf("packTime:");
for (long unsigned int i = 0; i < sizeof(header.packTime); ++i) {
printf("0x%02x ", header.packTime[i]);
}
printf("\n");
printf("version:");
for (long unsigned int i = 0; i < sizeof(header.version); ++i) {
printf("0x%02x ", header.version[i]);
}
printf("\n");
printf("product:");
for (long unsigned int i = 0; i < sizeof(header.product); ++i) {
printf("0x%02x ", header.product[i]);
}
printf("\n");
printf("upgradeType:");
for (long unsigned int i = 0; i < sizeof(header.upgradeType); ++i) {
printf("0x%02x ", header.upgradeType[i]);
}
printf("\n");
printf("=====================================\n");
} }

View File

@ -13,9 +13,9 @@
* limitations under the License. * limitations under the License.
*/ */
#ifndef UPGRADE_TOOL_H #ifndef UPGRADE_TOOL_H
#define UPGRADE_TOOL_H #define UPGRADE_TOOL_H
#include "StatusCode.h" #include "StatusCode.h"
#include <memory> #include <memory>
class UpgradeTool class UpgradeTool
{ {
public: public:
@ -24,12 +24,9 @@ public:
static std::shared_ptr<UpgradeTool> &GetInstance(std::shared_ptr<UpgradeTool> *impl = nullptr); static std::shared_ptr<UpgradeTool> &GetInstance(std::shared_ptr<UpgradeTool> *impl = nullptr);
void PackFile(const std::string &fileName, const std::string &outputFile, const std::string &version, void PackFile(const std::string &fileName, const std::string &outputFile, const std::string &version,
const std::string &product, const std::string &project, const std::string &upgradeType); const std::string &product, const std::string &project, const std::string &upgradeType);
};
#endif
// unsigned char packTime[6]; private:
// unsigned char version[4]; void FillInTime(unsigned char packTime[6]);
// unsigned char product[2]; bool StringToVersionBytes(const std::string &versionString, unsigned char result[4]);
// unsigned char project[2]; };
// unsigned char upgradeType[1]; #endif
// unsigned char reserved[17];

View File

@ -14,15 +14,17 @@
*/ */
#include "UpgradeTool.h" #include "UpgradeTool.h"
#include "ILog.h" #include "ILog.h"
#include "UpgradeBase.h"
#include "LinuxApi.h" #include "LinuxApi.h"
#include "UpgradeBase.h"
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <string> #include <string>
#include <time.h>
#include <vector> #include <vector>
std::shared_ptr<UpgradeTool> &UpgradeTool::GetInstance(std::shared_ptr<UpgradeTool> *impl) std::shared_ptr<UpgradeTool> &UpgradeTool::GetInstance(std::shared_ptr<UpgradeTool> *impl)
{ {
@ -38,7 +40,7 @@ std::shared_ptr<UpgradeTool> &UpgradeTool::GetInstance(std::shared_ptr<UpgradeTo
} }
return instance; return instance;
} }
bool stringToVersionBytes(const std::string &versionString, unsigned char result[4]) bool UpgradeTool::StringToVersionBytes(const std::string &versionString, unsigned char result[4])
{ {
std::stringstream ss(versionString); std::stringstream ss(versionString);
std::string segment; std::string segment;
@ -63,12 +65,27 @@ bool stringToVersionBytes(const std::string &versionString, unsigned char result
return true; return true;
} }
void UpgradeTool::FillInTime(unsigned char packTime[6])
{
time_t rawtime;
struct tm *timeinfo;
time(&rawtime);
timeinfo = localtime(&rawtime);
packTime[0] = timeinfo->tm_year;
packTime[1] = timeinfo->tm_mon + 1; // 月份1-12
packTime[2] = timeinfo->tm_mday; // 日期1-31
packTime[3] = timeinfo->tm_hour; // 小时0-23
packTime[4] = timeinfo->tm_min; // 分钟0-59
packTime[5] = timeinfo->tm_sec; // 秒0-59
}
void UpgradeTool::PackFile(const std::string &fileName, const std::string &outputFile, const std::string &version, void UpgradeTool::PackFile(const std::string &fileName, const std::string &outputFile, const std::string &version,
const std::string &product, const std::string &project, const std::string &upgradeType) const std::string &product, const std::string &project, const std::string &upgradeType)
{ {
UpgradeFileHeader header; UpgradeFileHeader header;
memset(&header, 0, sizeof(UpgradeFileHeader)); memset(&header, 0, sizeof(UpgradeFileHeader));
stringToVersionBytes(version, header.version); StringToVersionBytes(version, header.version);
FillInTime(header.packTime);
FILE *input_file = fopen(fileName.c_str(), "rb"); FILE *input_file = fopen(fileName.c_str(), "rb");
if (!input_file) { if (!input_file) {
perror("Error opening input file"); perror("Error opening input file");