embedded-framework/utils/UpgradeTool/src/UpgradeTool.cpp
2024-05-18 11:33:51 +08:00

134 lines
4.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "UpgradeTool.h"
#include "ILog.h"
#include "LinuxApi.h"
#include "UpgradeBase.h"
#include <cstring>
#include <iostream>
#include <sstream>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
std::shared_ptr<UpgradeTool> &UpgradeTool::GetInstance(std::shared_ptr<UpgradeTool> *impl)
{
static auto instance = std::make_shared<UpgradeTool>();
if (impl) {
if (instance.use_count() == 1) {
LogInfo("Instance changed succeed.\n");
instance = *impl;
}
else {
LogError("Can't changing the instance becase of using by some one.\n");
}
}
return instance;
}
bool UpgradeTool::StringToVersionBytes(const std::string &versionString, unsigned char result[4])
{
std::stringstream ss(versionString);
std::string segment;
int index = 0;
while (std::getline(ss, segment, '.') && index < 4) {
unsigned int value = 0;
std::stringstream segmentStream(segment);
segmentStream >> std::dec >> value;
if (value > 255) {
return false;
}
result[index++] = static_cast<unsigned char>(value);
printf("version[%d]: %02x\n", index, result[index - 1]);
}
if (index != 4) {
return false;
}
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,
const std::string &product, const std::string &project, const std::string &upgradeType)
{
struct stat fileStat;
UpgradeFileHeader header;
memset(&header, 0, sizeof(UpgradeFileHeader));
StringToVersionBytes(version, header.version);
FillInTime(header.packTime);
FILE *input_file = fopen(fileName.c_str(), "rb");
if (!input_file) {
perror("Error opening input file");
return;
}
if (stat(fileName.c_str(), &fileStat) == -1) {
LogError("Error getting file size\n");
fclose(input_file);
return;
}
LogInfo("File size:%ld\n", fileStat.st_size);
FILE *output_file = fopen(outputFile.c_str(), "wb");
if (!output_file) {
perror("Error opening output file");
fclose(input_file);
return;
}
long writeSize = 0;
fwrite(&header, sizeof(header), 1, output_file);
char buffer[1024];
size_t bytes_read = 0;
size_t bytes_write = 0;
while ((bytes_read = fread(buffer, 1, sizeof(buffer), input_file)) > 0) {
bytes_write = fwrite(buffer, 1, bytes_read, output_file);
if (bytes_write != bytes_read) {
LogError("fwrite failed, break;\n");
break;
}
writeSize += bytes_write;
}
LogInfo("write size:%ld\n", writeSize);
if (writeSize != fileStat.st_size) {
LogError("write file failed, remove output file.\n");
char cmd[1024];
snprintf(cmd, sizeof(cmd), "rm -f %s", outputFile.c_str());
fx_system(cmd);
}
else {
LogInfo("write file success.\n");
}
fclose(input_file);
fclose(output_file);
fx_system("sync");
}