mirror of
https://gitee.com/jiuyilian/embedded-framework.git
synced 2025-01-06 10:16:51 -05:00
110 lines
3.4 KiB
C++
110 lines
3.4 KiB
C++
/*
|
||
* 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)
|
||
{
|
||
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;
|
||
}
|
||
|
||
FILE *output_file = fopen(outputFile.c_str(), "wb");
|
||
if (!output_file) {
|
||
perror("Error opening output file");
|
||
fclose(input_file);
|
||
return;
|
||
}
|
||
fwrite(&header, sizeof(header), 1, output_file);
|
||
char buffer[1024];
|
||
size_t bytes_read;
|
||
while ((bytes_read = fread(buffer, 1, sizeof(buffer), input_file)) > 0) {
|
||
fwrite(buffer, 1, bytes_read, output_file);
|
||
}
|
||
fclose(input_file);
|
||
fclose(output_file);
|
||
fx_system("sync");
|
||
} |