From 275c1aa59d58f8584079311e5153b04127dcd835 Mon Sep 17 00:00:00 2001 From: Fancy code <258828110.@qq.com> Date: Tue, 7 May 2024 17:29:14 +0800 Subject: [PATCH] Backup:Upgrade. --- .../MissionManager/src/UpgradeState.cpp | 2 +- build/cmake/toolchain/linux.toolchain.cmake | 1 + middleware/HuntingUpgrade/CMakeLists.txt | 1 + .../HuntingUpgrade/src/HuntingUpgradeImpl.cpp | 3 +- .../src_mock/MediaManager_Mock_Test.cpp | 3 +- .../MissionManager/tool/CMakeLists.txt | 4 +- .../tool/include/MissionManagerTestTool.h | 1 + .../tool/src/MissionManagerTestTool.cpp | 14 ++- utils/CMakeLists.txt | 3 +- utils/UpgradeTool/CMakeLists.txt | 55 +++++++++++ utils/UpgradeTool/README.md | 5 + utils/UpgradeTool/include/UpgradeTool.h | 35 +++++++ utils/UpgradeTool/src/UpgradeTool.cpp | 93 +++++++++++++++++++ 13 files changed, 214 insertions(+), 6 deletions(-) create mode 100644 utils/UpgradeTool/CMakeLists.txt create mode 100644 utils/UpgradeTool/README.md create mode 100644 utils/UpgradeTool/include/UpgradeTool.h create mode 100644 utils/UpgradeTool/src/UpgradeTool.cpp diff --git a/application/MissionManager/src/UpgradeState.cpp b/application/MissionManager/src/UpgradeState.cpp index c2f0846..49abc61 100644 --- a/application/MissionManager/src/UpgradeState.cpp +++ b/application/MissionManager/src/UpgradeState.cpp @@ -13,10 +13,10 @@ * limitations under the License. */ #include "UpgradeState.h" +#include "IHuntingUpgrade.h" #include "ILog.h" #include "IMediaManager.h" #include "MissionStateMachine.h" -#include "IHuntingUpgrade.h" UpgradeState::UpgradeState() : State("UpgradeState") { mEventHandle[InternalStateEvent::CHECK_UPGRADE_FILE] = std::bind(&UpgradeState::CheckUpgradeFileHandle, this, _1); diff --git a/build/cmake/toolchain/linux.toolchain.cmake b/build/cmake/toolchain/linux.toolchain.cmake index 5cef9ea..6562fba 100755 --- a/build/cmake/toolchain/linux.toolchain.cmake +++ b/build/cmake/toolchain/linux.toolchain.cmake @@ -81,6 +81,7 @@ set(SD_CARD_MOUNT_PATH "./sdcard") # ------------ build upgrade ------------ # set(APPLICATION_CHECK_PATH "/application.bin") +set(APPLICATION_UPGRADE_PATH "./fastboot_server") set(APPLICATION_VERSION_1 1) set(APPLICATION_VERSION_2 0) set(APPLICATION_VERSION_3 0) diff --git a/middleware/HuntingUpgrade/CMakeLists.txt b/middleware/HuntingUpgrade/CMakeLists.txt index cbfcd31..8a80d50 100644 --- a/middleware/HuntingUpgrade/CMakeLists.txt +++ b/middleware/HuntingUpgrade/CMakeLists.txt @@ -16,6 +16,7 @@ include_directories( #) add_definitions(-DAPPLICATION_CHECK_PATH=\"${APPLICATION_CHECK_PATH}\") +add_definitions(-DAPPLICATION_UPGRADE_PATH=\"${APPLICATION_UPGRADE_PATH}\") aux_source_directory(./src SRC_FILES) diff --git a/middleware/HuntingUpgrade/src/HuntingUpgradeImpl.cpp b/middleware/HuntingUpgrade/src/HuntingUpgradeImpl.cpp index 02c0402..495dbab 100644 --- a/middleware/HuntingUpgrade/src/HuntingUpgradeImpl.cpp +++ b/middleware/HuntingUpgrade/src/HuntingUpgradeImpl.cpp @@ -23,8 +23,9 @@ StatusCode HuntingUpgradeImpl::CheckUpgradeFile(void) UpgradeFileHeader header; StatusCode code = UpgradeBase::CheckUpgradeFile(SD_CARD_MOUNT_PATH APPLICATION_CHECK_PATH, header); if (!IsCodeOK(code)) { - LogInfo("Check upgrade file:not found.\n"); + LogWarning("Check upgrade file:not found.\n"); return CreateStatusCode(STATUS_CODE_NOT_OK); } + UpgradeBase::MoveUpgradeFile(SD_CARD_MOUNT_PATH APPLICATION_CHECK_PATH, APPLICATION_UPGRADE_PATH); return code; } \ No newline at end of file diff --git a/test/application/HuntingCamera/src_mock/MediaManager_Mock_Test.cpp b/test/application/HuntingCamera/src_mock/MediaManager_Mock_Test.cpp index ea2466f..dc5844e 100644 --- a/test/application/HuntingCamera/src_mock/MediaManager_Mock_Test.cpp +++ b/test/application/HuntingCamera/src_mock/MediaManager_Mock_Test.cpp @@ -34,9 +34,10 @@ TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_EXAMPLE_MediaReprot) std::this_thread::sleep_for(std::chrono::milliseconds(100)); // McuManagerTestTool::MockMcuDeviceOpenFailed(mLinuxTest); MainThread::GetInstance()->Init(); - TestManager::ResetTimeOut(1000 * 6); + TestManager::ResetTimeOut(1000 * 4); std::this_thread::sleep_for(std::chrono::milliseconds(100)); // MockAppPlayback(); MainThread::GetInstance()->Runing(); + RemoveUpgradeFile(); } } // namespace MediaManager_Mock_Test \ No newline at end of file diff --git a/test/application/MissionManager/tool/CMakeLists.txt b/test/application/MissionManager/tool/CMakeLists.txt index 2bf9e9f..2cae5dc 100644 --- a/test/application/MissionManager/tool/CMakeLists.txt +++ b/test/application/MissionManager/tool/CMakeLists.txt @@ -18,6 +18,7 @@ include_directories( ${UTILS_SOURCE_PATH}/StatusCode/include ${UTILS_SOURCE_PATH}/Log/include ${UTILS_SOURCE_PATH}/LinuxApi/include + ${UTILS_SOURCE_PATH}/UpgradeTool/include ${TEST_SOURCE_PATH} ${TEST_SOURCE_PATH}/middleware/AppManager/tool/include ${TEST_SOURCE_PATH}/middleware/AppManager/tool/src @@ -31,11 +32,12 @@ include_directories( # ) add_definitions(-DAPPLICATION_CHECK_PATH=\"${APPLICATION_CHECK_PATH}\") +add_definitions(-DAPPLICATION_UPGRADE_PATH=\"${APPLICATION_UPGRADE_PATH}\") aux_source_directory(./src TEST_TOOL_SRC_FILES) set(TEST_TOOL_TARGET MissionManagerTestTool) add_library(${TEST_TOOL_TARGET} STATIC ${TEST_TOOL_SRC_FILES}) -target_link_libraries(${TEST_TOOL_TARGET} MissionManager AppManagerTestTool MediaManagerTestTool Log) +target_link_libraries(${TEST_TOOL_TARGET} MissionManager AppManagerTestTool MediaManagerTestTool UpgradeTool StatusCode Log) if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true") add_custom_target( diff --git a/test/application/MissionManager/tool/include/MissionManagerTestTool.h b/test/application/MissionManager/tool/include/MissionManagerTestTool.h index 0cdbc9a..5cd774b 100644 --- a/test/application/MissionManager/tool/include/MissionManagerTestTool.h +++ b/test/application/MissionManager/tool/include/MissionManagerTestTool.h @@ -27,6 +27,7 @@ public: protected: void CreateUpgradeFile(void); + void RemoveUpgradeFile(void); private: std::shared_ptr mMissionManagerMock; diff --git a/test/application/MissionManager/tool/src/MissionManagerTestTool.cpp b/test/application/MissionManager/tool/src/MissionManagerTestTool.cpp index 047ef9e..5c1e174 100644 --- a/test/application/MissionManager/tool/src/MissionManagerTestTool.cpp +++ b/test/application/MissionManager/tool/src/MissionManagerTestTool.cpp @@ -13,9 +13,11 @@ * limitations under the License. */ #include "MissionManagerTestTool.h" +#include "ILog.h" #include "LinuxApi.h" #include "MissionManagerMakePtrTest.h" #include "MissionManagerMock.h" +#include "UpgradeTool.h" void MissionManagerTestTool::Init(void) { mMissionManagerMock = std::make_shared(); @@ -29,5 +31,15 @@ void MissionManagerTestTool::UnInit(void) } void MissionManagerTestTool::CreateUpgradeFile(void) { - fx_system("touch " SD_CARD_MOUNT_PATH APPLICATION_CHECK_PATH); + fx_system("touch " 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, + "1.0.0.0", + "hunting", + "dgiot", + "app"); +} +void MissionManagerTestTool::RemoveUpgradeFile(void) +{ + fx_system("rm " SD_CARD_MOUNT_PATH APPLICATION_CHECK_PATH); } \ No newline at end of file diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt index 5240e45..b24fc13 100644 --- a/utils/CMakeLists.txt +++ b/utils/CMakeLists.txt @@ -16,4 +16,5 @@ add_subdirectory(MediaAdapter) add_subdirectory(FxHttpServer) add_subdirectory(Servers) add_subdirectory(TcpModule) -add_subdirectory(UpgradeBase) \ No newline at end of file +add_subdirectory(UpgradeBase) +add_subdirectory(UpgradeTool) \ No newline at end of file diff --git a/utils/UpgradeTool/CMakeLists.txt b/utils/UpgradeTool/CMakeLists.txt new file mode 100644 index 0000000..9665d8b --- /dev/null +++ b/utils/UpgradeTool/CMakeLists.txt @@ -0,0 +1,55 @@ +include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake) +set(EXECUTABLE_OUTPUT_PATH ${EXEC_OUTPUT_PATH}) +set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH}) + +include_directories( + ./src + ./include + ${UTILS_SOURCE_PATH}/StatusCode/include + ${UTILS_SOURCE_PATH}/Log/include + ${UTILS_SOURCE_PATH}/UpgradeBase/include + ${UTILS_SOURCE_PATH}/LinuxApi/include +) +# link_directories( +# ${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs +# ) + +aux_source_directory(./src SRC_FILES) + +set(TARGET_NAME UpgradeTool) +add_library(${TARGET_NAME} STATIC ${SRC_FILES}) +target_link_libraries(${TARGET_NAME} LinuxApi StatusCode Log) + +if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true") +add_custom_target( + UpgradeTool_code_check + COMMAND ${CLANG_TIDY_EXE} + -checks='${CLANG_TIDY_CHECKS}' + --header-filter=.* + --system-headers=false + ${SRC_FILES} + ${CLANG_TIDY_CONFIG} + -p ${PLATFORM_PATH}/cmake-shell + WORKING_DIRECTORY ${UTILS_SOURCE_PATH}/UpgradeTool +) +file(GLOB_RECURSE HEADER_FILES *.h) +add_custom_target( + UpgradeTool_code_format + COMMAND ${CLANG_FORMAT_EXE} + -style=file + -i ${SRC_FILES} ${HEADER_FILES} + WORKING_DIRECTORY ${UTILS_SOURCE_PATH}/UpgradeTool +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make UpgradeTool_code_check + COMMAND make UpgradeTool_code_format + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) +endif() + +define_file_name(${TARGET_NAME}) + +file(GLOB_RECURSE INSTALL_HEADER_FILES include/*.h) +install(FILES ${INSTALL_HEADER_FILES} DESTINATION include) \ No newline at end of file diff --git a/utils/UpgradeTool/README.md b/utils/UpgradeTool/README.md new file mode 100644 index 0000000..20c4101 --- /dev/null +++ b/utils/UpgradeTool/README.md @@ -0,0 +1,5 @@ +# 升级打包工具库 + +## 概述 + +  升级打包工具库,用于把升级文件添加校验字节头进行打包。 \ No newline at end of file diff --git a/utils/UpgradeTool/include/UpgradeTool.h b/utils/UpgradeTool/include/UpgradeTool.h new file mode 100644 index 0000000..c0ee3a9 --- /dev/null +++ b/utils/UpgradeTool/include/UpgradeTool.h @@ -0,0 +1,35 @@ +/* + * 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. + */ +#ifndef UPGRADE_TOOL_H + #define UPGRADE_TOOL_H + #include "StatusCode.h" + #include +class UpgradeTool +{ +public: + UpgradeTool() = default; + virtual ~UpgradeTool() = default; + static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); + 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); +}; +#endif + +// unsigned char packTime[6]; +// unsigned char version[4]; +// unsigned char product[2]; +// unsigned char project[2]; +// unsigned char upgradeType[1]; +// unsigned char reserved[17]; \ No newline at end of file diff --git a/utils/UpgradeTool/src/UpgradeTool.cpp b/utils/UpgradeTool/src/UpgradeTool.cpp new file mode 100644 index 0000000..d481457 --- /dev/null +++ b/utils/UpgradeTool/src/UpgradeTool.cpp @@ -0,0 +1,93 @@ +/* + * 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 "UpgradeBase.h" +#include "LinuxApi.h" +#include +#include +#include +#include +#include +#include +#include +#include +std::shared_ptr &UpgradeTool::GetInstance(std::shared_ptr *impl) +{ + static auto instance = std::make_shared(); + 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 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(value); + printf("================ version[%d]: %02x\n", index, result[index - 1]); + } + + if (index != 4) { + return false; + } + + return true; +} +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); + 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"); +} \ No newline at end of file