Add:HuntingUpgrade test code.

This commit is contained in:
Fancy code 2024-05-18 10:36:52 +08:00
parent 467e998479
commit 53662789e1
16 changed files with 451 additions and 10 deletions

5
doc/sdk_build_guide.md Normal file
View File

@ -0,0 +1,5 @@
# 1. SDK构建设计文档
## 1.1. 概述
  SDK使用cmake构建把分层解耦合的独立模块编译成静态库应用程序根据依赖关系进行自动关联链接。

View File

@ -16,9 +16,6 @@ include_directories(
#link_directories(
#)
add_definitions(-DAPPLICATION_CHECK_PATH=\"${APPLICATION_CHECK_PATH}\")
add_definitions(-DAPPLICATION_UPGRADE_PATH=\"${APPLICATION_UPGRADE_PATH}\")
aux_source_directory(./src SRC_FILES)
set(TARGET_NAME HuntingUpgrade)

View File

@ -1,2 +1,4 @@
add_definitions(-DHUNTING_CAMERA_VERSION="1.0.0.0")
add_definitions(-DHUNTING_CAMERA_VERSION="1.0.0.0")
add_definitions(-DAPPLICATION_CHECK_PATH="${APPLICATION_CHECK_PATH}")
add_definitions(-DAPPLICATION_UPGRADE_PATH="${APPLICATION_UPGRADE_PATH}")

View File

@ -14,9 +14,26 @@
*/
#include "HuntingUpgradeImpl.h"
#include "ILog.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
StatusCode HuntingUpgradeImpl::CheckFileHeader(const UpgradeFileHeader &head)
{
const char *version = HUNTING_CAMERA_VERSION;
constexpr int VERSION_BUFF_LENGTH = 36;
char versionStr[VERSION_BUFF_LENGTH] = {0};
memcpy(versionStr, HUNTING_CAMERA_VERSION, strlen(HUNTING_CAMERA_VERSION));
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);
}
StatusCode HuntingUpgradeImpl::CheckUpgradeFile(void)

View File

@ -1,4 +1,5 @@
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
include(${MIDDLEWARE_SOURCE_PATH}/HuntingUpgrade/build/hunting_upgrade.cmake)
include(${HAL_SOURCE_PATH}/build/hal.cmake)
set(EXECUTABLE_OUTPUT_PATH ${EXEC_OUTPUT_PATH})
set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH})
@ -31,9 +32,6 @@ include_directories(
# ${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs
# )
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})

View File

@ -0,0 +1,51 @@
/*
* 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 "VersionBase.h"
#include <fstream>
#include <iostream>
#include <regex>
#include <sstream>
#include <string>
void VersionBase::GetVersion(const std::string &fileName, const std::regex &pattern, std::string &version)
{
std::ifstream file(fileName);
if (!file.is_open()) {
std::cerr << "无法打开文件: " << fileName << std::endl;
return;
}
std::string line;
std::string versionGet;
// std::regex pattern(R"(add_definitions\(-DHUNTING_CAMERA_VERSION=\"([^"]+)\"\))");
while (std::getline(file, line)) {
std::smatch match;
if (std::regex_search(line, match, pattern)) {
// 如果找到匹配项match[1]将包含捕获的版本号
versionGet = match[1].str();
break; // 找到后退出循环
}
}
file.close();
if (versionGet.empty()) {
std::cerr << "在文件中未找到指定的行或版本号!" << std::endl;
return;
}
std::cout << "提取的版本号: " << versionGet << std::endl;
return;
}

View File

@ -0,0 +1,26 @@
/*
* 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 VERSION_BASE_H
#define VERSION_BASE_H
#include <string>
#include <regex>
class VersionBase
{
public:
VersionBase() = default;
virtual ~VersionBase() = default;
void GetVersion(const std::string &fileName, const std::regex &pattern, std::string &version);
};
#endif

View File

@ -5,4 +5,5 @@ add_subdirectory(McuManager)
add_subdirectory(McuAskBase)
add_subdirectory(DeviceManager)
add_subdirectory(AppManager)
add_subdirectory(MediaManager)
add_subdirectory(MediaManager)
add_subdirectory(HuntingUpgrade)

View File

@ -0,0 +1,82 @@
# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake)
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
include(${MIDDLEWARE_SOURCE_PATH}/HuntingUpgrade/build/hunting_upgrade.cmake)
set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin)
include_directories(
./src
./include
./tool/include
${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/StatusCode/include
# ${UTILS_SOURCE_PATH}/LinuxApi/include
# ${HAL_SOURCE_PATH}/include
${MIDDLEWARE_SOURCE_PATH}/HuntingUpgrade/include
${TEST_SOURCE_PATH}
# ${TEST_SOURCE_PATH}/hal/tool/include
# ${TEST_SOURCE_PATH}/utils/LinuxApiMock/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
)
link_directories(
${LIBS_OUTPUT_PATH}
${EXTERNAL_LIBS_OUTPUT_PATH}
)
aux_source_directory(. SRC_FILES_MAIN)
aux_source_directory(./src SRC_FILES)
if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX})
aux_source_directory(./src_mock SRC_FILES)
endif()
set(TARGET_NAME HuntingUpgradeTest)
add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES})
target_link_libraries(${TARGET_NAME} HuntingUpgradeTestTool gtest gmock pthread)
if(${TEST_COVERAGE} MATCHES "true")
target_link_libraries(${TARGET_NAME} gcov)
endif()
if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true")
add_custom_target(
HuntingUpgradeTest_code_check
COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}'
--header-filter=.*
--system-headers=false
${SRC_FILES}
${CLANG_TIDY_CONFIG}
# --line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/gtest.h\"}]'
--line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/*.h\"}]'
-p ${PLATFORM_PATH}/cmake-shell
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/HuntingUpgrade
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make HuntingUpgradeTest_code_check
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
file(GLOB_RECURSE HEADER_FILES *.h)
add_custom_target(
HuntingUpgradeTest_code_format
COMMAND ${CLANG_FORMAT_EXE}
-style=file
-i ${SRC_FILES} ${SRC_FILES_MAIN} ${HEADER_FILES}
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/HuntingUpgrade
)
add_custom_command(
TARGET ${TARGET_NAME}
PRE_BUILD
COMMAND make HuntingUpgradeTest_code_check
COMMAND make HuntingUpgradeTest_code_format
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
endif()
define_file_name(${TARGET_NAME})
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/tool/CMakeLists.txt")
add_subdirectory(tool)
endif()

View File

@ -0,0 +1,23 @@
/*
* 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 <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
#include <unistd.h>
int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -0,0 +1,68 @@
/*
* 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 "HuntingUpgradeTestTool.h"
#include "IHuntingUpgrade.h"
#include "ILog.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <thread>
namespace HuntingUpgradeTest
{
class HuntingUpgradeTest : public testing::Test, public HuntingUpgradeTestTool
{
public:
HuntingUpgradeTest()
{
}
virtual ~HuntingUpgradeTest()
{
}
static void SetUpTestCase()
{
CreateLogModule();
ILogInit(LOG_INSTANCE_TYPE_END);
}
static void TearDownTestCase()
{
ILogUnInit();
DestroyLogModule();
}
virtual void SetUp()
{
// HalTestTool::Init();
HuntingUpgradeTestTool::Init();
// CreateHalCppModule();
// CreateHuntingUpgradeModule();
CreateHuntingUpgradeModule();
}
virtual void TearDown()
{
HuntingUpgradeTestTool::UnInit();
// HalTestTool::UnInit();
// DestroyHuntingUpgradeModule();
// DestroyHalCppModule();
DestroyHuntingUpgradeModule();
}
};
/**
* @brief
* ../output_files/test/bin/HuntingUpgradeTest --gtest_filter=HuntingUpgradeTest.HS_RH_UNIT_HuntingUpgrade_EXAMPLE_Demo0
*/
TEST_F(HuntingUpgradeTest, HS_RH_UNIT_HuntingUpgrade_EXAMPLE_Demo0)
{
HuntingUpgradeTestTool::CreateUpgradeFile();
IHuntingUpgrade::GetInstance()->CheckUpgradeFile();
}
} // namespace HuntingUpgradeTest

View File

@ -0,0 +1,56 @@
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
include(${HAL_SOURCE_PATH}/build/hal.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}/LinuxApi/include
${UTILS_SOURCE_PATH}/UpgradeTool/include
${MIDDLEWARE_SOURCE_PATH}/HuntingUpgrade/src
${TEST_SOURCE_PATH}
${TEST_SOURCE_PATH}/utils/LinuxApiMock/include
${TEST_SOURCE_PATH}/utils/McuProtocol/tool/include
)
# link_directories(
# ${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs
# )
aux_source_directory(./src TEST_TOOL_SRC_FILES)
set(TEST_TOOL_TARGET HuntingUpgradeTestTool)
add_library(${TEST_TOOL_TARGET} STATIC ${TEST_TOOL_SRC_FILES})
target_link_libraries(${TEST_TOOL_TARGET} UpgradeTool LinuxApi HuntingUpgrade Log)
if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true")
add_custom_target(
HuntingUpgradeTestTool_code_check
COMMAND ${CLANG_TIDY_EXE}
-checks='${CLANG_TIDY_CHECKS}'
--header-filter=.*
--system-headers=false
${TEST_TOOL_SRC_FILES}
${CLANG_TIDY_CONFIG}
-p ${PLATFORM_PATH}/cmake-shell
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/HuntingUpgrade/tool
)
file(GLOB_RECURSE HEADER_FILES *.h)
add_custom_target(
HuntingUpgradeTestTool_code_format
COMMAND ${CLANG_FORMAT_EXE}
-style=file
-i ${TEST_TOOL_SRC_FILES} ${HEADER_FILES}
WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/HuntingUpgrade/tool
)
add_custom_command(
TARGET ${TEST_TOOL_TARGET}
PRE_BUILD
COMMAND make HuntingUpgradeTestTool_code_check
COMMAND make HuntingUpgradeTestTool_code_format
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
endif()
define_file_name(${TEST_TOOL_TARGET})

View File

@ -0,0 +1,32 @@
/*
* 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 HUNTING_UPGRADE_TEST_TOOL_H
#define HUNTING_UPGRADE_TEST_TOOL_H
#include "GtestUsing.h"
class HuntingUpgradeTestTool
{
public:
HuntingUpgradeTestTool() = default;
virtual ~HuntingUpgradeTestTool() = default;
protected:
void Init(void);
void UnInit(void);
void CreateUpgradeFile(void);
private:
bool CheckDirectory(const char *filepath);
};
#endif

View File

@ -0,0 +1,82 @@
/*
* 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 "HuntingUpgradeTestTool.h"
#include "ILog.h"
#include "LinuxApi.h"
#include "UpgradeTool.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
void HuntingUpgradeTestTool::Init(void)
{
}
void HuntingUpgradeTestTool::UnInit(void)
{
if (access(SD_CARD_MOUNT_PATH APPLICATION_CHECK_PATH "-test", F_OK) == 0) {
fx_system("rm -rf " SD_CARD_MOUNT_PATH);
}
}
void HuntingUpgradeTestTool::CreateUpgradeFile(void)
{
CheckDirectory(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",
SD_CARD_MOUNT_PATH APPLICATION_CHECK_PATH,
"1.0.0.0",
"hunting",
"dgiot",
"app");
}
bool HuntingUpgradeTestTool::CheckDirectory(const char *filepath)
{
LogInfo("CheckDirectory:%s\n", filepath);
char *path = nullptr;
char *sep = nullptr;
struct stat st = {0};
path = strdup(filepath);
if (!path) {
LogError("strdup\n");
return false;
}
for (sep = strchr(path, '/'); sep != NULL; sep = strchr(sep + 1, '/')) {
*sep = '\0';
if (strlen(path) == 0) {
*sep = '/';
continue;
}
if (stat(path, &st) == -1) {
if (errno == ENOENT) {
if (mkdir(path, 0755) == -1) {
LogError("mkdir path failed:%s\n", path);
free(path);
return false;
}
}
else {
LogError("stat\n");
free(path);
return false;
}
}
*sep = '/';
}
free(path);
return true;
}

View File

@ -15,10 +15,11 @@
#ifndef UPGRADE_BASE_H
#define UPGRADE_BASE_H
#include "StatusCode.h"
constexpr int VERSION_LENGTH = 4;
typedef struct __attribute__((packed)) upgrade_file_header
{
unsigned char packTime[6];
unsigned char version[4];
unsigned char version[VERSION_LENGTH];
unsigned char product[2];
unsigned char project[2];
unsigned char upgradeType[1];