Improve:HuntingUpgrade.

This commit is contained in:
Fancy code 2024-05-18 11:33:51 +08:00
parent 53662789e1
commit 844ecd7e8b
4 changed files with 36 additions and 17 deletions

View File

@ -19,21 +19,10 @@
#include <string.h> #include <string.h>
StatusCode HuntingUpgradeImpl::CheckFileHeader(const UpgradeFileHeader &head) StatusCode HuntingUpgradeImpl::CheckFileHeader(const UpgradeFileHeader &head)
{ {
constexpr int VERSION_BUFF_LENGTH = 36; if (CheckVersion(head) == false) {
char versionStr[VERSION_BUFF_LENGTH] = {0}; LogError("Check version failed.\n");
memcpy(versionStr, HUNTING_CAMERA_VERSION, strlen(HUNTING_CAMERA_VERSION)); return CreateStatusCode(STATUS_CODE_NOT_OK);
unsigned char versionChar[VERSION_LENGTH] = {0};
char *token;
int i = 0;
token = strtok(versionStr, ".");
while (token != NULL && i < VERSION_LENGTH) {
versionChar[i] = atoi(token);
token = strtok(NULL, ".");
i++;
} }
// for (int j = 0; j < VERSION_LENGTH; j++) {
// printf("0x%X\n", versionChar[j]);
// }
return CreateStatusCode(STATUS_CODE_OK); return CreateStatusCode(STATUS_CODE_OK);
} }
StatusCode HuntingUpgradeImpl::CheckUpgradeFile(void) StatusCode HuntingUpgradeImpl::CheckUpgradeFile(void)
@ -47,3 +36,30 @@ StatusCode HuntingUpgradeImpl::CheckUpgradeFile(void)
UpgradeBase::MoveUpgradeFile(SD_CARD_MOUNT_PATH APPLICATION_CHECK_PATH, APPLICATION_UPGRADE_PATH); UpgradeBase::MoveUpgradeFile(SD_CARD_MOUNT_PATH APPLICATION_CHECK_PATH, APPLICATION_UPGRADE_PATH);
return code; return code;
} }
bool HuntingUpgradeImpl::CheckVersion(const UpgradeFileHeader &head)
{
constexpr int VERSION_BUFF_LENGTH = 36;
char versionStr[VERSION_BUFF_LENGTH] = {0};
memcpy(versionStr, HUNTING_CAMERA_VERSION, strlen(HUNTING_CAMERA_VERSION));
unsigned char versionOld[VERSION_LENGTH] = {0};
char *token;
int i = 0;
token = strtok(versionStr, ".");
while (token != NULL && i < VERSION_LENGTH) {
versionOld[i] = atoi(token);
token = strtok(NULL, ".");
i++;
}
unsigned long long oldVersionNum = 0;
unsigned long long newVersionNum = 0;
for (int j = 0; j < VERSION_LENGTH; j++) {
LogInfo("0x%X\n", versionOld[j]);
oldVersionNum += versionOld[j] * 10 * (i + 1);
newVersionNum += head.version[j] * 10 * (i + 1);
}
if (oldVersionNum >= newVersionNum) {
LogError("Version is too low.\n");
return false;
}
return true;
}

View File

@ -14,9 +14,9 @@
*/ */
#ifndef HUNTING_UPGRADE_H #ifndef HUNTING_UPGRADE_H
#define HUNTING_UPGRADE_H #define HUNTING_UPGRADE_H
#include "IHuntingUpgrade.h"
#include "StatusCode.h" #include "StatusCode.h"
#include "UpgradeBase.h" #include "UpgradeBase.h"
#include "IHuntingUpgrade.h"
#include <memory> #include <memory>
class HuntingUpgradeImpl : public UpgradeBase, public IHuntingUpgrade class HuntingUpgradeImpl : public UpgradeBase, public IHuntingUpgrade
{ {
@ -25,5 +25,8 @@ public:
virtual ~HuntingUpgradeImpl() = default; virtual ~HuntingUpgradeImpl() = default;
StatusCode CheckFileHeader(const UpgradeFileHeader &head) override; StatusCode CheckFileHeader(const UpgradeFileHeader &head) override;
StatusCode CheckUpgradeFile(void) override; StatusCode CheckUpgradeFile(void) override;
private:
bool CheckVersion(const UpgradeFileHeader &head);
}; };
#endif #endif

View File

@ -36,7 +36,7 @@ void HuntingUpgradeTestTool::CreateUpgradeFile(void)
fx_system("touch " SD_CARD_MOUNT_PATH APPLICATION_CHECK_PATH "-test"); fx_system("touch " SD_CARD_MOUNT_PATH APPLICATION_CHECK_PATH "-test");
UpgradeTool::GetInstance()->PackFile(SD_CARD_MOUNT_PATH APPLICATION_CHECK_PATH "-test", UpgradeTool::GetInstance()->PackFile(SD_CARD_MOUNT_PATH APPLICATION_CHECK_PATH "-test",
SD_CARD_MOUNT_PATH APPLICATION_CHECK_PATH, SD_CARD_MOUNT_PATH APPLICATION_CHECK_PATH,
"1.0.0.0", "6.4.5.6",
"hunting", "hunting",
"dgiot", "dgiot",
"app"); "app");

View File

@ -56,7 +56,7 @@ bool UpgradeTool::StringToVersionBytes(const std::string &versionString, unsigne
} }
result[index++] = static_cast<unsigned char>(value); result[index++] = static_cast<unsigned char>(value);
printf("================ version[%d]: %02x\n", index, result[index - 1]); printf("version[%d]: %02x\n", index, result[index - 1]);
} }
if (index != 4) { if (index != 4) {