Merge branch 'master-develop' of gitee.com:shenzhen-jiuyilian/ipc into master-develop

This commit is contained in:
xiaojiazhu 2024-05-18 09:46:54 -07:00
commit 1b6e073d5d
61 changed files with 2591 additions and 729 deletions

View File

@ -7,8 +7,9 @@ set(HUNTTING_MAIN_INCLUDE_PATH "${HUNTTING_MAIN_INCLUDE_PATH};${MIDDLEWARE_S
set(HUNTTING_MAIN_INCLUDE_PATH "${HUNTTING_MAIN_INCLUDE_PATH};${MIDDLEWARE_SOURCE_PATH}/FilesManager/include")
set(HUNTTING_MAIN_INCLUDE_PATH "${HUNTTING_MAIN_INCLUDE_PATH};${MIDDLEWARE_SOURCE_PATH}/StorageManager/include")
set(HUNTTING_MAIN_INCLUDE_PATH "${HUNTTING_MAIN_INCLUDE_PATH};${MIDDLEWARE_SOURCE_PATH}/HuntingUpgrade/include")
set(HUNTTING_MAIN_INCLUDE_PATH "${HUNTTING_MAIN_INCLUDE_PATH};${MIDDLEWARE_SOURCE_PATH}/IpcConfig/include")
set(HUNTTING_MAIN_INCLUDE_PATH "${HUNTTING_MAIN_INCLUDE_PATH};${UTILS_SOURCE_PATH}/StatusCode/include")
set(HUNTTING_MAIN_INCLUDE_PATH "${HUNTTING_MAIN_INCLUDE_PATH};${UTILS_SOURCE_PATH}/Log/include")
set(HUNTTING_MAIN_INCLUDE_PATH "${HUNTTING_MAIN_INCLUDE_PATH};${HAL_SOURCE_PATH}/include")
set(HUNTTING_LINK_LIB McuManager MissionManager StateMachine AppManager FilesManager StorageManager HuntingUpgrade StatusCode Log Hal pthread dl)
set(HUNTTING_LINK_LIB McuManager MissionManager StateMachine AppManager FilesManager StorageManager HuntingUpgrade IpcConfig StatusCode Log Hal pthread dl)

View File

@ -17,6 +17,7 @@
#include "IFilesManager.h"
#include "IHalCpp.h"
#include "IHuntingUpgrade.h"
#include "IIpcConfig.h"
#include "ILog.h"
#include "IMcuManager.h"
#include "IMediaManager.h"
@ -66,6 +67,7 @@ StatusCode MainThread::Init(void)
IHalCpp::GetInstance()->Init();
IMcuManager::GetInstance()->Init();
IStorageManager::GetInstance()->Init();
IIpcConfig::GetInstance()->Init();
IMediaManager::GetInstance()->Init();
IMissionManager::GetInstance()->Init();
return CreateStatusCode(STATUS_CODE_OK);
@ -74,6 +76,7 @@ StatusCode MainThread::UnInit(void)
{
IMissionManager::GetInstance()->UnInit();
IMediaManager::GetInstance()->UnInit();
IIpcConfig::GetInstance()->UnInit();
IStorageManager::GetInstance()->UnInit();
IMcuManager::GetInstance()->UnInit();
IHalCpp::GetInstance()->UnInit();
@ -92,6 +95,7 @@ StatusCode MainThread::CreateAllModules(void)
CreateAppManagerModule();
CreateMediaManagerModule();
CreateHuntingUpgradeModule();
CreateIpcConfigModule();
return CreateStatusCode(STATUS_CODE_OK);
}
void MainThread::DestoryAllModules(void)
@ -105,6 +109,7 @@ void MainThread::DestoryAllModules(void)
DestroyStorageManagerModule();
DestroyMcuManager();
DestroyHalCppModule();
DestroyIpcConfigModule();
}
void MainThread::ResetAllPtrMaker(void)
{
@ -118,6 +123,5 @@ void MainThread::Runing(void)
extern bool CreateProtocolHandleImpl(void);
void MainThread::CustomizationInit(void)
{
//
CreateProtocolHandleImpl();
}

View File

@ -74,3 +74,7 @@ opt MCU上电
deactivate 大核
end
```
## 1.4. MCU监视器
  MCU监视器必须由其中一个状态继承只有状态机运行之后才能处理串口命令。

View File

@ -0,0 +1,69 @@
/*
* 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 "McuMonitor.h"
#include "ILog.h"
#include "IMcuManager.h"
void McuMonitor::Init(std::shared_ptr<VMcuMonitor> &monitor)
{
IMcuManager::GetInstance()->SetMcuMonitor(monitor);
}
void McuMonitor::UnInit(void)
{
}
void McuMonitor::RecvIpcMissionEvent(std::shared_ptr<VMcuRecv> &recv, const IpcMission &mission)
{
}
void McuMonitor::RecvMcuHeartBeatEvent(std::shared_ptr<VMcuRecv> &recv)
{
}
void McuMonitor::RecvGetIntervalStartEvent(std::shared_ptr<VMcuRecv> &recv)
{
LogInfo("RecvGetIntervalStartEvent\n");
std::shared_ptr<McuRecv<McuGetIntervalStart>> recvData =
std::dynamic_pointer_cast<McuRecv<McuGetIntervalStart>>(recv);
if (recvData) {
recvData->mDataRecvReply.mHour = 10;
recvData->mDataRecvReply.mMin = 10;
recvData->mDataRecvReply.mSecond = 10;
recv->ReplyFinished(true);
}
recv->ReplyFinished(false);
}
void McuMonitor::RecvGetDateTime(std::shared_ptr<VMcuRecv> &recv)
{
LogInfo("RecvGetDateTime\n");
std::shared_ptr<McuRecv<McuReplyDateTime>> recvData = std::dynamic_pointer_cast<McuRecv<McuReplyDateTime>>(recv);
if (recvData) {
recvData->mDataRecvReply.mYear = 2024;
recvData->mDataRecvReply.mMon = 10;
recvData->mDataRecvReply.mDay = 10;
recvData->mDataRecvReply.mHour = 10;
recvData->mDataRecvReply.mMin = 10;
recvData->mDataRecvReply.mSecond = 10;
recv->ReplyFinished(true);
}
recv->ReplyFinished(false);
}
void McuMonitor::RecvGetPirSensitivity(std::shared_ptr<VMcuRecv> &recv)
{
LogInfo("RecvGetPirSensitivity\n");
std::shared_ptr<McuRecv<McuGetPirSensitivity>> recvData =
std::dynamic_pointer_cast<McuRecv<McuGetPirSensitivity>>(recv);
if (recvData) {
recvData->mDataRecvReply.mSensitivity = 9;
recv->ReplyFinished(true);
}
recv->ReplyFinished(false);
}

View File

@ -0,0 +1,31 @@
/*
* 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 MCU_MONITOR_H
#define MCU_MONITOR_H
#include "IMcuManager.h"
class McuMonitor : public VMcuMonitor
{
public:
McuMonitor() = default;
virtual ~McuMonitor() = default;
void Init(std::shared_ptr<VMcuMonitor> &monitor);
void UnInit(void);
void RecvIpcMissionEvent(std::shared_ptr<VMcuRecv> &recv, const IpcMission &mission) override;
void RecvMcuHeartBeatEvent(std::shared_ptr<VMcuRecv> &recv) override;
void RecvGetIntervalStartEvent(std::shared_ptr<VMcuRecv> &recv) override;
void RecvGetDateTime(std::shared_ptr<VMcuRecv> &recv) override;
void RecvGetPirSensitivity(std::shared_ptr<VMcuRecv> &recv) override;
};
#endif

View File

@ -18,13 +18,16 @@
#include "MissionStateMachine.h"
TopState::TopState() : State("TopState")
{
mEventHandle[InternalStateEvent::STORAGE_HANDLE_STATE_INIT] = std::bind(&TopState::StorageStartInitHandle, this, _1);
mEventHandle[InternalStateEvent::STORAGE_HANDLE_STATE_INIT] =
std::bind(&TopState::StorageStartInitHandle, this, _1);
}
void TopState::GoInState()
{
LogInfo(" ========== TopState::GoInState.\n");
std::shared_ptr<VMediaMonitor> monitor = std::dynamic_pointer_cast<TopState>(shared_from_this());
IMediaManager::GetInstance()->SetMediaMonitor(monitor);
std::shared_ptr<VMcuMonitor> mcuMonitor = std::dynamic_pointer_cast<VMcuMonitor>(shared_from_this());
McuMonitor::Init(mcuMonitor);
std::shared_ptr<VMediaMonitor> mediaMonitor = std::dynamic_pointer_cast<VMediaMonitor>(shared_from_this());
IMediaManager::GetInstance()->SetMediaMonitor(mediaMonitor);
MissionStateMachine::GetInstance()->SwitchState(SystemState::MISSION_STATE);
}
void TopState::GoOutState()

View File

@ -17,9 +17,11 @@
#include "DataProcessing.h"
#include "IMediaManager.h"
#include "IStateMachine.h"
#include "McuMonitor.h"
class TopState : public State,
public DataProcessing,
public VMediaMonitor,
public McuMonitor,
public std::enable_shared_from_this<TopState>
{
public:

74
doc/develop_standard.md Normal file
View File

@ -0,0 +1,74 @@
# 1. SDK开发规范
## 1.1. 编码规范
### 1.1.1. 指针/智能指针
* C++编码只能使用智能指针;
* 指针遵循谁使用谁进行“非空”判断,且无比使用前进行“非空”判断;
* 智能指针经过转换后务必进行“非空”判断;
理论上,**明显不可能为空的指针,可以不进行“非空”判断**,可以不进行“非空”判断的场景:
```
void McuManagerImpl::OtherSideSendIpcMission(const unsigned int &serialNumber, const unsigned char &mission)
{
class McuRecvIpcMission : public McuRecvImpl, public McuRecv<unsigned char>
{
public:
McuRecvIpcMission(std::shared_ptr<McuManagerImpl> &mcuManager, const unsigned int &serialNumber,
const OtherSideSendType &sendType, const unsigned char &mission)
: McuRecvImpl(serialNumber, sendType)
{
McuRecv::mDataRecvReply = mission;
McuRecvImpl::mMcuManager = mcuManager;
}
~McuRecvIpcMission() = default;
void ReplyFinished(const bool result) override
{
// 此处可以不进行“非空”判断该值在有限范围内OtherSideSendIpcMission函数内部就能看出是否为空
McuRecvImpl::mMcuManager->ReplyOtherSideSendIpcMission(ASK_RESULT::SUCCEED, McuRecvImpl::mSerialNumber);
}
};
std::shared_ptr<VMcuMonitor> monitor = GetMcuMonitor();
std::shared_ptr<McuManagerImpl> manager = std::dynamic_pointer_cast<McuManagerImpl>(SharedFromThis());
std::shared_ptr<VMcuRecv> recv =
std::make_shared<McuRecvIpcMission>(manager, serialNumber, OtherSideSendType::SEND_IPC_MISSION, mission);
if (monitor) {
monitor->RecvIpcMissionEvent(recv, static_cast<IpcMission>(mission));
}
else {
LogWarning("mMonitor is nullptr, AddMcuRecv.\n");
AddMcuRecv(recv);
}
}
```
**没有进行“非空”判断的代码,应该开发测试用例,保证“空指针”的报错。**
### 1.1.2. 注释
* 注释必须使用英文,且使用翻译器翻译;
&emsp;&emsp;避免编码问题导致的乱码,且需要保证阅读困难时可使用翻译器翻译成可读的中文;
**注:** 注释翻译工具使用[百度翻译](https://fanyi.baidu.com/)
### 1.1.3. C++继承
* 子类使用父类的函数时,函数前必须加父类名,降低阅读难度,没有父类名的一律为本类函数(有可能是虚函数);
### 1.1.4. 变量命名
#### 1.1.4.1. 结构体/类成员
* 结构体和类成员必须要使用驼峰命名法且首字母必须为m表示成员变量
```
typedef struct app_get_product_info
{
app_get_product_info();
std::string mModel;
std::string mCompany;
std::string mSoc;
std::string mSp;
} AppGetProductInfo;
```

View File

@ -112,7 +112,7 @@ For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ../output_files/test/bin/McuManagerTest...
warning: core file may not match specified executable file.
warning: core file may not match specified executable file. // 表示coredump文件与可执行文件不匹配
[New LWP 3390383]
Core was generated by `/usr/sbin/smbd --foreground --no-process-group'.
Program terminated with signal SIGABRT, Aborted.
@ -125,6 +125,8 @@ Program terminated with signal SIGABRT, Aborted.
由于gdb和asan同时启用会冲突导致无法识别coredump文件。解决办法如下
修改://build/sdk_config.cmake
```
# Gdb debug
if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX})

5
doc/sdk_build_guide.md Normal file
View File

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

View File

@ -1,6 +1,7 @@
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
include(${HAL_SOURCE_PATH}/build/hal.cmake)
include(./build/hunting_upgrade.cmake)
set(EXECUTABLE_OUTPUT_PATH ${EXEC_OUTPUT_PATH})
set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH})
@ -15,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

@ -0,0 +1,4 @@
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,8 +14,15 @@
*/
#include "HuntingUpgradeImpl.h"
#include "ILog.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
StatusCode HuntingUpgradeImpl::CheckFileHeader(const UpgradeFileHeader &head)
{
if (CheckVersion(head) == false) {
LogError("Check version failed.\n");
return CreateStatusCode(STATUS_CODE_NOT_OK);
}
return CreateStatusCode(STATUS_CODE_OK);
}
StatusCode HuntingUpgradeImpl::CheckUpgradeFile(void)
@ -29,3 +36,30 @@ StatusCode HuntingUpgradeImpl::CheckUpgradeFile(void)
UpgradeBase::MoveUpgradeFile(SD_CARD_MOUNT_PATH APPLICATION_CHECK_PATH, APPLICATION_UPGRADE_PATH);
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
#define HUNTING_UPGRADE_H
#include "IHuntingUpgrade.h"
#include "StatusCode.h"
#include "UpgradeBase.h"
#include "IHuntingUpgrade.h"
#include <memory>
class HuntingUpgradeImpl : public UpgradeBase, public IHuntingUpgrade
{
@ -25,5 +25,8 @@ public:
virtual ~HuntingUpgradeImpl() = default;
StatusCode CheckFileHeader(const UpgradeFileHeader &head) override;
StatusCode CheckUpgradeFile(void) override;
private:
bool CheckVersion(const UpgradeFileHeader &head);
};
#endif

View File

@ -1,5 +1,6 @@
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
include(./build/ipc_config.cmake)
set(EXECUTABLE_OUTPUT_PATH ${EXEC_OUTPUT_PATH})
set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH})
@ -14,13 +15,6 @@ include_directories(
#link_directories(
#)
if (DEFINED IPC_CONFIG_FILE_PATH)
add_definitions(-DIPC_CONFIG_FILE_PATH=\"${IPC_CONFIG_FILE_PATH}\")
else()
message(FATAL_ERROR "You set define IPC_CONFIG_FILE_PATH in toolchan .cmake file to tell code where to save config file.")
endif()
aux_source_directory(./src SRC_FILES)
set(TARGET_NAME IpcConfig)
@ -62,3 +56,8 @@ add_custom_command(
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)

View File

@ -0,0 +1,21 @@
# 1. IPC配置文件库
## 1.1. 概述
&emsp;&emsp;配置文件库用于读写IPC的配置文件。适用于打猎机产品。
## 1.2. 软件设计
1. 使用了第三方开源库libconfig
2. 本库使用结构体保存数据,可拓展不使用第三方开源库,直接保存结构体数据即可;在资源受限时,可动/静态取消第三方开源库;
3. 配置文件明文显示,可加密;
## 1.3. 数据丢失还原机制
&emsp;&emsp;针对可能发生的数据丢失/损坏,提供数据还原机制。
* 系统配置一份默认的只读配置文件,用于数据丢失/损坏时使用;
## 1.4. 数据备份还原机制
&emsp;&emsp;每次修改配置文件,需要备份一份,用于数据丢失/损坏时使用;

View File

@ -0,0 +1,6 @@
if (DEFINED IPC_CONFIG_FILE_PATH)
add_definitions(-DIPC_CONFIG_FILE_PATH=\"${IPC_CONFIG_FILE_PATH}\")
else()
message(FATAL_ERROR "You set define IPC_CONFIG_FILE_PATH in toolchan .cmake file to tell code where to save config file.")
endif()

View File

@ -18,22 +18,23 @@
#include <iostream>
#include <memory>
#include <string_view>
enum class IpcConfigKey
{
KEY_WORK_MODE = 0,
KEY_CONTINUOUS_SHOT,
KEY_BURST_PHOTO_INTERVAL,
KEY_IMGAE_SIZE,
KEY_VIDEO_SIZE,
KEY_INFRARED_LAMP_POWER,
KEY_DELAYED,
KEY_PIR_SENSITIVITY,
KEY_STORAGE_LOOP_SWITCH,
KEY_FACTORY_RESET_FLAG,
KEY_FORMATTING_SD_CARD,
KEY_DARK_MODE,
KEY_WORK_INTERVAL,
WIFI_SSID = 0,
WIFI_PASSWORD,
WORK_MODE,
CONTINUOUS_SHOT,
BURST_PHOTO_INTERVAL,
IMGAE_SIZE,
VIDEO_LENGTH,
INFRARED_POWER,
PIR_DELAYED,
PIR_SENSITIVITY,
STORAGE_LOOP_SWITCH,
FACTORY_RESET_FLAG,
FORMATTING_SD_CARD,
DARK_MODE,
WORK_INTERVAL,
TEST_SHORT,
TEST_LONG,
TEST_LLONG,
@ -42,11 +43,24 @@ enum class IpcConfigKey
TEST_FLOAT,
END
};
enum WorkMode
enum class WorkMode
{
WORK_MODE_PIC = 0,
WORK_MODE_PIC_VIDEO,
WORK_MODE_END,
MODE_PIC = 0,
MODE_PIC_VIDEO,
END,
};
enum class ConfigSwitch
{
OFF = 0,
ON,
END
};
enum class ConfigLevel
{
HIGHT = 0,
MIDDLE,
LOW,
END
};
enum ContinuousShot
{
@ -68,7 +82,6 @@ enum VideoSize
VIDEO_SIZE_15 = 15,
VIDEO_SIZE_END,
};
enum InfraredIampPower
{
INFRARED_IAMP_POWER_LOW = 0,
@ -90,7 +103,16 @@ enum PirSensitivity
PIR_SENSITIVITY_MAX = 9,
PIR_SENSITIVITY_END,
};
typedef struct working_time
{
working_time();
unsigned char mHourFrom;
unsigned char mHourTo;
unsigned char mMinuteFrom;
unsigned char mMinuteTo;
} WorkingTime; // TODO:
bool CreateIpcConfigModule(void);
bool DestroyIpcConfigModule(void);
class IIpcConfig
{
public:
@ -119,7 +141,12 @@ public:
virtual const bool GetBool(const IpcConfigKey &key);
virtual void SetBool(const IpcConfigKey &key, const bool &value);
virtual const std::string GetString(const IpcConfigKey &key);
virtual void SetString(const IpcConfigKey &key, const std::string string);
virtual void SetString(const IpcConfigKey &key, const std::string &string);
virtual WorkMode GetWorkMode(void);
virtual void SetWorkMode(const WorkMode &mode);
virtual ConfigSwitch GetSwitch(const IpcConfigKey &key);
virtual void SetSwitch(const IpcConfigKey &key, const ConfigSwitch &value);
virtual ConfigLevel GetLevel(const IpcConfigKey &key);
virtual void SetLevel(const IpcConfigKey &key, const ConfigLevel &value);
};
bool CreateIpcConfig(void);
#endif

View File

@ -14,6 +14,9 @@
*/
#include "IIpcConfig.h"
#include "ILog.h"
working_time::working_time() : mHourFrom(0), mHourTo(0), mMinuteFrom(0), mMinuteTo(0)
{
}
std::shared_ptr<IIpcConfig> &IIpcConfig::GetInstance(std::shared_ptr<IIpcConfig> *impl)
{
static auto instance = std::make_shared<IIpcConfig>();
@ -107,6 +110,27 @@ const std::string IIpcConfig::GetString(const IpcConfigKey &key)
{
return "undefine";
}
void IIpcConfig::SetString(const IpcConfigKey &key, const std::string string)
void IIpcConfig::SetString(const IpcConfigKey &key, const std::string &string)
{
}
WorkMode IIpcConfig::GetWorkMode(void)
{
return WorkMode::END;
}
void IIpcConfig::SetWorkMode(const WorkMode &mode)
{
}
ConfigSwitch IIpcConfig::GetSwitch(const IpcConfigKey &key)
{
return ConfigSwitch::END;
}
void IIpcConfig::SetSwitch(const IpcConfigKey &key, const ConfigSwitch &value)
{
}
ConfigLevel IIpcConfig::GetLevel(const IpcConfigKey &key)
{
return ConfigLevel::END;
}
void IIpcConfig::SetLevel(const IpcConfigKey &key, const ConfigLevel &value)
{
}

View File

@ -1,568 +0,0 @@
/*
* 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 "IpcConfig.h"
#include "ILog.h"
#include <string.h>
#define CHECK_MAP(map) (map.size() == 1 ? true : false)
IpcConfig::IpcConfig()
{
mCfgChanged = CONFIG_HAS_NOT_CHANGED;
std::map<std::string, std::reference_wrapper<int>> innerMapWorkMode;
innerMapWorkMode.insert(std::make_pair("work_mode", std::reference_wrapper<int>(mAllData.workMode)));
mCfgMapInt.insert(std::make_pair(IpcConfigKey::KEY_WORK_MODE, innerMapWorkMode));
std::map<std::string, std::reference_wrapper<int>> innerMapContinuousShot;
innerMapContinuousShot.insert(
std::make_pair("continuous_shot", std::reference_wrapper<int>(mAllData.continuousShot)));
mCfgMapInt.insert(std::make_pair(IpcConfigKey::KEY_CONTINUOUS_SHOT, innerMapContinuousShot));
std::map<std::string, std::reference_wrapper<int>> innerMapBurstPhotoInterval;
innerMapBurstPhotoInterval.insert(
std::make_pair("burst_photo_interval", std::reference_wrapper<int>(mAllData.burstPhotoInterval)));
mCfgMapInt.insert(std::make_pair(IpcConfigKey::KEY_BURST_PHOTO_INTERVAL, innerMapBurstPhotoInterval));
std::map<std::string, std::reference_wrapper<CHAR_STRING>> innerMapImageSize;
innerMapImageSize.insert(std::make_pair("image_size", std::reference_wrapper<CHAR_STRING>(mAllData.imageSize)));
mCfgMapString.insert(std::make_pair(IpcConfigKey::KEY_IMGAE_SIZE, innerMapImageSize));
std::map<std::string, std::reference_wrapper<int>> innerMapVideoSize;
innerMapVideoSize.insert(std::make_pair("video_size", std::reference_wrapper<int>(mAllData.videoSize)));
mCfgMapInt.insert(std::make_pair(IpcConfigKey::KEY_VIDEO_SIZE, innerMapVideoSize));
std::map<std::string, std::reference_wrapper<int>> innerMapInfraredLampPower;
innerMapInfraredLampPower.insert(
std::make_pair("infrared_lamp_power", std::reference_wrapper<int>(mAllData.infraredIampPower)));
mCfgMapInt.insert(std::make_pair(IpcConfigKey::KEY_INFRARED_LAMP_POWER, innerMapInfraredLampPower));
std::map<std::string, std::reference_wrapper<int>> innerMapDelayed;
innerMapDelayed.insert(std::make_pair("delayed", std::reference_wrapper<int>(mAllData.delayed)));
mCfgMapInt.insert(std::make_pair(IpcConfigKey::KEY_DELAYED, innerMapDelayed));
std::map<std::string, std::reference_wrapper<int>> innerMapPirSensitivity;
innerMapPirSensitivity.insert(
std::make_pair("pir_sensitivity", std::reference_wrapper<int>(mAllData.pirSensitivity)));
mCfgMapInt.insert(std::make_pair(IpcConfigKey::KEY_PIR_SENSITIVITY, innerMapPirSensitivity));
std::map<std::string, std::reference_wrapper<bool>> innerMapStorageLoopSwitch;
innerMapStorageLoopSwitch.insert(
std::make_pair("storage_loop_switch", std::reference_wrapper<bool>(mAllData.storageLoopSwitch)));
mCfgMapBool.insert(std::make_pair(IpcConfigKey::KEY_STORAGE_LOOP_SWITCH, innerMapStorageLoopSwitch));
std::map<std::string, std::reference_wrapper<bool>> innerMapFactoryReset;
innerMapFactoryReset.insert(std::make_pair("factory_reset", std::reference_wrapper<bool>(mAllData.factoryReset)));
mCfgMapBool.insert(std::make_pair(IpcConfigKey::KEY_FACTORY_RESET_FLAG, innerMapFactoryReset));
std::map<std::string, std::reference_wrapper<bool>> innerMapFormattingSDCard;
innerMapFormattingSDCard.insert(
std::make_pair("formatting_SD_card", std::reference_wrapper<bool>(mAllData.formattingSDCard)));
mCfgMapBool.insert(std::make_pair(IpcConfigKey::KEY_FORMATTING_SD_CARD, innerMapFormattingSDCard));
std::map<std::string, std::reference_wrapper<CHAR_STRING>> innerMapDrakMode;
innerMapDrakMode.insert(std::make_pair("dark_mode", std::reference_wrapper<CHAR_STRING>(mAllData.darkMode)));
mCfgMapString.insert(std::make_pair(IpcConfigKey::KEY_DARK_MODE, innerMapDrakMode));
std::map<std::string, std::reference_wrapper<CHAR_STRING>> innerMapWorkInterval;
innerMapWorkInterval.insert(
std::make_pair("work_interval", std::reference_wrapper<CHAR_STRING>(mAllData.workingInterval)));
mCfgMapString.insert(std::make_pair(IpcConfigKey::KEY_WORK_INTERVAL, innerMapWorkInterval));
std::map<std::string, std::reference_wrapper<short>> innerMapShort;
innerMapShort.insert(std::make_pair("test_short", std::reference_wrapper<short>(mAllData.testShort)));
mCfgMapShort.insert(std::make_pair(IpcConfigKey::TEST_SHORT, innerMapShort));
std::map<std::string, std::reference_wrapper<long>> innerMapLong;
innerMapLong.insert(std::make_pair("test_long", std::reference_wrapper<long>(mAllData.testLong)));
mCfgMapLong.insert(std::make_pair(IpcConfigKey::TEST_LONG, innerMapLong));
std::map<std::string, std::reference_wrapper<long long>> innerMapLLong;
innerMapLLong.insert(std::make_pair("test_llong", std::reference_wrapper<long long>(mAllData.testLLong)));
mCfgMapLLong.insert(std::make_pair(IpcConfigKey::TEST_LLONG, innerMapLLong));
std::map<std::string, std::reference_wrapper<char>> innerMapChar;
innerMapChar.insert(std::make_pair("test_char", std::reference_wrapper<char>(mAllData.testChar)));
mCfgMapChar.insert(std::make_pair(IpcConfigKey::TEST_CHAR, innerMapChar));
std::map<std::string, std::reference_wrapper<float>> innerMapFloat;
innerMapFloat.insert(std::make_pair("test_float", std::reference_wrapper<float>(mAllData.testFloat)));
mCfgMapFloat.insert(std::make_pair(IpcConfigKey::TEST_FLOAT, innerMapFloat));
}
const StatusCode IpcConfig::Init(void)
{
memset(&mAllData, 0, sizeof(Config_s));
mCfg = OpenConfigFile(IPC_CONFIG_FILE_PATH);
if (nullptr == mCfg) {
LogError("Open config file failed.\n");
return CreateStatusCode(STATUS_CODE_NOT_OK);
}
ReadAllConfigParameters();
return CreateStatusCode(STATUS_CODE_OK);
}
const StatusCode IpcConfig::UnInit(void)
{
if (CONFIG_HAS_CHANGED == mCfgChanged) {
LogInfo("Save config files.\n");
ConfigSaveFile(mCfg);
}
CloseConfigFile(mCfg);
return CreateStatusCode(STATUS_CODE_OK);
}
const StatusCode IpcConfig::ConfigFileSave(void)
{
return ConfigSaveFile(mCfg);
}
const int IpcConfig::GetInt(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<int>>>::iterator iter;
iter = mCfgMapInt.find(key);
if (iter != mCfgMapInt.end() && CHECK_MAP(iter->second)) {
return iter->second.begin()->second;
}
LogError("Can't find the key.\n");
constexpr int UNKNOWN_CONFIG = -1;
return UNKNOWN_CONFIG;
}
void IpcConfig::SetInt(const IpcConfigKey &key, const int &value)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<int>>>::iterator iter;
iter = mCfgMapInt.find(key);
if (iter != mCfgMapInt.end() && CHECK_MAP(iter->second)) {
iter->second.begin()->second.get() = value;
const char *name = iter->second.begin()->first.c_str();
ConfigSetInt(mCfg, name, iter->second.begin()->second);
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
const short IpcConfig::GetShort(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<short>>>::iterator iter;
iter = mCfgMapShort.find(key);
if (iter != mCfgMapShort.end() && CHECK_MAP(iter->second)) {
return iter->second.begin()->second;
}
LogError("Can't find the key.\n");
constexpr short UNKNOWN_CONFIG = -1;
return UNKNOWN_CONFIG;
}
void IpcConfig::SetShort(const IpcConfigKey &key, const short &value)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<short>>>::iterator iter;
iter = mCfgMapShort.find(key);
if (iter != mCfgMapShort.end() && CHECK_MAP(iter->second)) {
iter->second.begin()->second.get() = value;
const char *name = iter->second.begin()->first.c_str();
ConfigSetShort(mCfg, name, iter->second.begin()->second);
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
const long IpcConfig::GetLong(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<long>>>::iterator iter;
iter = mCfgMapLong.find(key);
if (iter != mCfgMapLong.end() && CHECK_MAP(iter->second)) {
return iter->second.begin()->second;
}
LogError("Can't find the key.\n");
constexpr long UNKNOWN_CONFIG = -1;
return UNKNOWN_CONFIG;
}
void IpcConfig::SetLong(const IpcConfigKey &key, const long &value)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<long>>>::iterator iter;
iter = mCfgMapLong.find(key);
if (iter != mCfgMapLong.end() && CHECK_MAP(iter->second)) {
iter->second.begin()->second.get() = value;
const char *name = iter->second.begin()->first.c_str();
ConfigSetLong(mCfg, name, iter->second.begin()->second);
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
const long long IpcConfig::GetLLong(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<long long>>>::iterator iter;
iter = mCfgMapLLong.find(key);
if (iter != mCfgMapLLong.end() && CHECK_MAP(iter->second)) {
return iter->second.begin()->second;
}
LogError("Can't find the key.\n");
constexpr long long UNKNOWN_CONFIG = -1;
return UNKNOWN_CONFIG;
}
void IpcConfig::SetLLong(const IpcConfigKey &key, const long long &value)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<long long>>>::iterator iter;
iter = mCfgMapLLong.find(key);
if (iter != mCfgMapLLong.end() && CHECK_MAP(iter->second)) {
iter->second.begin()->second.get() = value;
const char *name = iter->second.begin()->first.c_str();
ConfigSetLLong(mCfg, name, iter->second.begin()->second);
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
const char IpcConfig::GetChar(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<char>>>::iterator iter;
iter = mCfgMapChar.find(key);
if (iter != mCfgMapChar.end() && CHECK_MAP(iter->second)) {
return iter->second.begin()->second;
}
LogError("Can't find the key.\n");
constexpr char UNKNOWN_CONFIG = '\0';
return UNKNOWN_CONFIG;
}
void IpcConfig::SetChar(const IpcConfigKey &key, const char &character)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<char>>>::iterator iter;
iter = mCfgMapChar.find(key);
if (iter != mCfgMapChar.end() && CHECK_MAP(iter->second)) {
iter->second.begin()->second.get() = character;
const char *name = iter->second.begin()->first.c_str();
ConfigSetChar(mCfg, name, iter->second.begin()->second);
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
const float IpcConfig::GetFloat(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<float>>>::iterator iter;
iter = mCfgMapFloat.find(key);
if (iter != mCfgMapFloat.end() && CHECK_MAP(iter->second)) {
return iter->second.begin()->second;
}
LogError("Can't find the key.\n");
constexpr float UNKNOWN_CONFIG = -1.00;
return UNKNOWN_CONFIG;
}
void IpcConfig::SetFloat(const IpcConfigKey &key, const float &value)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<float>>>::iterator iter;
iter = mCfgMapFloat.find(key);
if (iter != mCfgMapFloat.end() && CHECK_MAP(iter->second)) {
iter->second.begin()->second.get() = value;
const char *name = iter->second.begin()->first.c_str();
ConfigSetFloat(mCfg, name, iter->second.begin()->second);
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
const double IpcConfig::GetDouble(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<double>>>::iterator iter;
iter = mCfgMapDouble.find(key);
if (iter != mCfgMapDouble.end() && CHECK_MAP(iter->second)) {
return iter->second.begin()->second;
}
LogError("Can't find the key.\n");
constexpr double UNKNOWN_CONFIG = -1.00;
return UNKNOWN_CONFIG;
}
void IpcConfig::SetDouble(const IpcConfigKey &key, const double &value)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<double>>>::iterator iter;
iter = mCfgMapDouble.find(key);
if (iter != mCfgMapDouble.end() && CHECK_MAP(iter->second)) {
iter->second.begin()->second.get() = value;
const char *name = iter->second.begin()->first.c_str();
ConfigSetDouble(mCfg, name, iter->second.begin()->second);
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
const long double IpcConfig::GetLongDouble(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::reference_wrapper<long double>>::iterator iter;
iter = mCfgMapLongDouble.find(key);
if (iter != mCfgMapLongDouble.end()) {
return iter->second;
}
LogError("Can't find the key.\n");
constexpr long double UNKNOWN_CONFIG = -1.0;
return UNKNOWN_CONFIG;
}
void IpcConfig::SetLongDouble(const IpcConfigKey &key, const long double &value)
{
std::map<IpcConfigKey, std::reference_wrapper<long double>>::iterator iter;
iter = mCfgMapLongDouble.find(key);
if (iter != mCfgMapLongDouble.end()) {
iter->second.get() = value;
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
const bool IpcConfig::GetBool(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<bool>>>::iterator iter;
iter = mCfgMapBool.find(key);
if (iter != mCfgMapBool.end() && CHECK_MAP(iter->second)) {
return iter->second.begin()->second;
}
LogError("Can't find the key.\n");
constexpr bool UNKNOWN_CONFIG = false;
return UNKNOWN_CONFIG;
}
void IpcConfig::SetBool(const IpcConfigKey &key, const bool &value)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<bool>>>::iterator iter;
iter = mCfgMapBool.find(key);
if (iter != mCfgMapBool.end() && CHECK_MAP(iter->second)) {
iter->second.begin()->second.get() = value;
const char *name = iter->second.begin()->first.c_str();
ConfigSetBool(mCfg, name, iter->second.begin()->second);
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
const std::string IpcConfig::GetString(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<CHAR_STRING>>>::iterator iter;
iter = mCfgMapString.find(key);
if (iter != mCfgMapString.end() && CHECK_MAP(iter->second)) {
const std::string sv(iter->second.begin()->second); // char[] --> const std::strinbg
return sv;
}
LogError("Can't find the key.\n");
const std::string UNKNOWN_CONFIG = "undefine";
return UNKNOWN_CONFIG;
}
void IpcConfig::SetString(const IpcConfigKey &key, const std::string string)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<CHAR_STRING>>>::iterator iter;
iter = mCfgMapString.find(key);
if (iter != mCfgMapString.end() && CHECK_MAP(iter->second)) {
strncpy(iter->second.begin()->second, string.c_str(), sizeof(CHAR_STRING)); // const std::strinbg --> char[]
const char *name = iter->second.begin()->first.c_str();
ConfigSetString(mCfg, name, iter->second.begin()->second);
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
void IpcConfig::ReadAllConfigParameters(void)
{
StatusCode workModeCode = ConfigGetInt(mCfg, "work_mode", &(mAllData.workMode));
if (StatusCodeEqual(workModeCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("work_mode doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.workMode = WORK_MODE_PIC;
ConfigSetInt(mCfg, "work_mode", mAllData.workMode);
}
StatusCode continuousShotCode = ConfigGetInt(mCfg, "continuous_shot", &(mAllData.continuousShot));
if (StatusCodeEqual(continuousShotCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("continuous_shot doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.continuousShot = CONTINUOUS_SHOT_ONE_PIC;
ConfigSetInt(mCfg, "continuous_shot", mAllData.continuousShot);
}
StatusCode burstPhotoIntervalCode = ConfigGetInt(mCfg, "burst_photo_interval", &(mAllData.burstPhotoInterval));
if (StatusCodeEqual(burstPhotoIntervalCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("burst_photo_interval doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.burstPhotoInterval = BURST_PHOTO_INTERVAL_DEFAULT;
ConfigSetInt(mCfg, "burst_photo_interval", mAllData.burstPhotoInterval);
}
const char *imageSizeString = nullptr;
StatusCode imageSizeCode = ConfigGetString(mCfg, "image_size", &(imageSizeString));
if (StatusCodeEqual(imageSizeCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("image_size doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
char defaultImageSize[] = "320*320";
strncpy(mAllData.imageSize, defaultImageSize, sizeof(mAllData.imageSize));
ConfigSetString(mCfg, "image_size", mAllData.imageSize);
}
else {
if (nullptr != imageSizeString) {
strncpy(mAllData.imageSize, imageSizeString, sizeof(mAllData.imageSize));
}
else {
LogError("image_size get failed.\n");
}
}
StatusCode videoSizeCode = ConfigGetInt(mCfg, "video_size", &(mAllData.videoSize));
if (StatusCodeEqual(videoSizeCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("video_size doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.videoSize = VIDEO_SIZE_15;
ConfigSetInt(mCfg, "video_size", mAllData.videoSize);
}
StatusCode infraredIampPowerCode = ConfigGetInt(mCfg, "infrared_lamp_power", &(mAllData.infraredIampPower));
if (StatusCodeEqual(infraredIampPowerCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("infrared_lamp_power doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.infraredIampPower = INFRARED_IAMP_POWER_MID;
ConfigSetInt(mCfg, "infrared_lamp_power", mAllData.infraredIampPower);
}
StatusCode delayedCode = ConfigGetInt(mCfg, "delayed", &(mAllData.delayed));
if (StatusCodeEqual(delayedCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("delayed doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.delayed = DELAYED_DEFAULT;
ConfigSetInt(mCfg, "delayed", mAllData.delayed);
}
StatusCode pirSensitivityCode = ConfigGetInt(mCfg, "pir_sensitivity", &(mAllData.pirSensitivity));
if (StatusCodeEqual(pirSensitivityCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("pir_sensitivity doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.pirSensitivity = PIR_SENSITIVITY_DEFAULT;
ConfigSetInt(mCfg, "pir_sensitivity", mAllData.pirSensitivity);
}
StatusCode storageLoopSwitchCode = ConfigGetBool(mCfg, "storage_loop_switch", &(mAllData.storageLoopSwitch));
if (StatusCodeEqual(storageLoopSwitchCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("storage_loop_switch doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
constexpr bool storageLoopSwitch = true;
mAllData.storageLoopSwitch = storageLoopSwitch;
ConfigSetBool(mCfg, "storage_loop_switch", mAllData.storageLoopSwitch);
}
StatusCode factoryResetCode = ConfigGetBool(mCfg, "factory_reset", &(mAllData.factoryReset));
if (StatusCodeEqual(factoryResetCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("factory_resetCode doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
constexpr bool factoryReset = true;
mAllData.factoryReset = factoryReset;
ConfigSetBool(mCfg, "factory_reset", mAllData.factoryReset);
}
StatusCode formattingSDCardCode = ConfigGetBool(mCfg, "formatting_SD_card", &(mAllData.formattingSDCard));
if (StatusCodeEqual(formattingSDCardCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("formatting_SD_card doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
constexpr bool formattingSDCard = true;
mAllData.formattingSDCard = formattingSDCard;
ConfigSetBool(mCfg, "formatting_SD_card", mAllData.formattingSDCard);
}
const char *darkModeString = nullptr;
StatusCode darkModeCode = ConfigGetString(mCfg, "dark_mode", &(darkModeString));
if (StatusCodeEqual(darkModeCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("dark_mode doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
char defaultDarkMode[] = "19:00:00-07:00:00";
strncpy(mAllData.darkMode, defaultDarkMode, sizeof(mAllData.darkMode));
ConfigSetString(mCfg, "dark_mode", mAllData.darkMode);
}
else {
if (nullptr != darkModeString) {
strncpy(mAllData.darkMode, darkModeString, sizeof(mAllData.darkMode));
}
else {
LogError("dark_mode get failed.\n");
}
}
const char *workIntervalString = nullptr;
StatusCode workIntervalCode = ConfigGetString(mCfg, "work_interval", &(workIntervalString));
if (StatusCodeEqual(workIntervalCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("work_interval doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
char defaultWorkInterval[] = "07:00:01-07:00:00";
strncpy(mAllData.workingInterval, defaultWorkInterval, sizeof(mAllData.workingInterval));
ConfigSetString(mCfg, "work_interval", mAllData.workingInterval);
}
else {
if (nullptr != workIntervalString) {
strncpy(mAllData.workingInterval, workIntervalString, sizeof(mAllData.workingInterval));
}
else {
LogError("work_interval get failed.\n");
}
}
StatusCode shortCode = ConfigGetShort(mCfg, "test_short", &(mAllData.testShort));
if (StatusCodeEqual(shortCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("test_short doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
constexpr short DEFAULT_TEST_SHORT_NUM = 11;
mAllData.testShort = DEFAULT_TEST_SHORT_NUM;
ConfigSetShort(mCfg, "test_short", mAllData.testShort);
}
StatusCode longCode = ConfigGetLong(mCfg, "test_long", &(mAllData.testLong));
if (StatusCodeEqual(longCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("test_long doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
constexpr long DEFAULT_TEST_LONG_NUM = 12;
mAllData.testLong = DEFAULT_TEST_LONG_NUM;
ConfigSetLong(mCfg, "test_long", mAllData.testLong);
}
StatusCode llongCode = ConfigGetLLong(mCfg, "test_llong", &(mAllData.testLLong));
if (StatusCodeEqual(llongCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("test_llong doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
constexpr long long DEFAULT_TEST_LLONG_NUM = 13;
mAllData.testLLong = DEFAULT_TEST_LLONG_NUM;
ConfigSetLLong(mCfg, "test_llong", mAllData.testLLong);
}
StatusCode charCode = ConfigGetChar(mCfg, "test_char", &(mAllData.testChar));
if (StatusCodeEqual(charCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("test_char doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
constexpr char DEFAULT_TEST_CHAR_NUM = 'B';
mAllData.testChar = DEFAULT_TEST_CHAR_NUM;
ConfigSetChar(mCfg, "test_char", mAllData.testChar);
}
StatusCode floatCode = ConfigGetFloat(mCfg, "test_float", &(mAllData.testFloat));
if (StatusCodeEqual(floatCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("test_float doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
constexpr float DEFAULT_TEST_FLOAT_NUM = 1.123456;
mAllData.testFloat = DEFAULT_TEST_FLOAT_NUM;
ConfigSetFloat(mCfg, "test_float", mAllData.testFloat);
}
if (CONFIG_HAS_CHANGED == mCfgChanged) {
LogInfo("Save the config file.\n");
mCfgChanged = CONFIG_HAS_NOT_CHANGED;
ConfigSaveFile(mCfg);
}
}

View File

@ -0,0 +1,813 @@
/*
* 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 "IpcConfigImpl.h"
#include "ILog.h"
#include <string.h>
#define CHECK_MAP(map) (map.size() == 1 ? true : false)
const char *CONFIG_WIFI_SSID = "wifi_ssid";
const char *CONFIG_WIFI_SSID_DEFAULT = "Hunting 2024";
const char *CONFIG_WIFI_PASSWORD = "wifi_password";
const char *CONFIG_WIFI_PASSWORD_DEFAULT = "123456";
const char *CONFIG_WORK_MODE = "work_mode";
const char *CONFIG_CONTINUE_SHOT = "continuous_shot";
const char *CONFIG_BURST_PHOTO_INTERVAL = "continuous_interval";
const char *CONFIG_IMAGE_SIZE = "image_size";
const char *CONFIG_VIDEO_SIZE = "video_size";
const char *CONFIG_PIR_DELAYED = "pir_delayed";
const char *CONFIG_STORAGE_LOOP = "storage_loop";
const char *CONFIG_INFRARED_POWER = "infrared_power";
const char *CONFIG_PIR_SENSITIVITY = "pir_sensitivity";
IpcConfigImpl::IpcConfigImpl()
{
mCfgChanged = CONFIG_HAS_NOT_CHANGED;
memset(&mAllData, 0, sizeof(Config_s));
{
std::map<std::string, StringConfigPack> config;
StringConfigPack pack = {WIFI_SSID_BUFF_SIZE, mAllData.mWifiSsid};
config.insert(std::make_pair(CONFIG_WIFI_SSID, pack));
mCfgMapStringV2.insert(std::make_pair(IpcConfigKey::WIFI_SSID, config));
}
{
std::map<std::string, StringConfigPack> config;
StringConfigPack pack = {WIFI_PASSWORD_BUFF_SIZE, mAllData.mWifiPassword};
config.insert(std::make_pair(CONFIG_WIFI_PASSWORD, pack));
mCfgMapStringV2.insert(std::make_pair(IpcConfigKey::WIFI_PASSWORD, config));
}
{
std::map<std::string, char *> config;
config.insert(std::make_pair(CONFIG_WORK_MODE, &mAllData.mWorkMode));
mCfgMapCharV2.insert(std::make_pair(IpcConfigKey::WORK_MODE, config));
}
{
std::map<std::string, char *> config;
config.insert(std::make_pair(CONFIG_INFRARED_POWER, &mAllData.mInfraredPower));
mCfgMapCharV2.insert(std::make_pair(IpcConfigKey::INFRARED_POWER, config));
}
{
std::map<std::string, int *> config;
config.insert(std::make_pair(CONFIG_CONTINUE_SHOT, &mAllData.mContinuousShot));
mCfgMapIntV2.insert(std::make_pair(IpcConfigKey::CONTINUOUS_SHOT, config));
}
{
std::map<std::string, int *> config;
config.insert(std::make_pair(CONFIG_BURST_PHOTO_INTERVAL, &mAllData.mBurstPhotoInterval));
mCfgMapIntV2.insert(std::make_pair(IpcConfigKey::BURST_PHOTO_INTERVAL, config));
}
{
std::map<std::string, int *> config;
config.insert(std::make_pair(CONFIG_IMAGE_SIZE, &mAllData.mImageSize));
mCfgMapIntV2.insert(std::make_pair(IpcConfigKey::IMGAE_SIZE, config));
}
{
std::map<std::string, int *> config;
config.insert(std::make_pair(CONFIG_VIDEO_SIZE, &mAllData.mVideoLength));
mCfgMapIntV2.insert(std::make_pair(IpcConfigKey::VIDEO_LENGTH, config));
}
{
std::map<std::string, int *> config;
config.insert(std::make_pair(CONFIG_PIR_DELAYED, &mAllData.mPirDelayed));
mCfgMapIntV2.insert(std::make_pair(IpcConfigKey::PIR_DELAYED, config));
}
{
std::map<std::string, int *> config;
config.insert(std::make_pair(CONFIG_PIR_SENSITIVITY, &mAllData.mPirSensitivity));
mCfgMapIntV2.insert(std::make_pair(IpcConfigKey::PIR_SENSITIVITY, config));
}
{
std::map<std::string, bool *> config;
config.insert(std::make_pair(CONFIG_STORAGE_LOOP, &mAllData.mStorageLoopSwitch));
mCfgMapBoolV2.insert(std::make_pair(IpcConfigKey::STORAGE_LOOP_SWITCH, config));
}
// std::map<std::string, std::reference_wrapper<int>> innerMapWorkMode;
// innerMapWorkMode.insert(std::make_pair("work_mode", std::reference_wrapper<int>(mAllData.mWorkMode)));
// mCfgMapInt.insert(std::make_pair(IpcConfigKey::WORK_MODE, innerMapWorkMode));
// std::map<std::string, std::reference_wrapper<int>> innerMapContinuousShot;
// innerMapContinuousShot.insert(
// std::make_pair("continuous_shot", std::reference_wrapper<int>(mAllData.mContinuousShot)));
// mCfgMapInt.insert(std::make_pair(IpcConfigKey::CONTINUOUS_SHOT, innerMapContinuousShot));
// std::map<std::string, std::reference_wrapper<int>> innerMapBurstPhotoInterval;
// innerMapBurstPhotoInterval.insert(
// std::make_pair("burst_photo_interval", std::reference_wrapper<int>(mAllData.mBurstPhotoInterval)));
// mCfgMapInt.insert(std::make_pair(IpcConfigKey::BURST_PHOTO_INTERVAL, innerMapBurstPhotoInterval));
// std::map<std::string, std::reference_wrapper<CHAR_STRING>> innerMapImageSize;
// innerMapImageSize.insert(std::make_pair("image_size", std::reference_wrapper<CHAR_STRING>(mAllData.imageSize)));
// mCfgMapString.insert(std::make_pair(IpcConfigKey::IMGAE_SIZE, innerMapImageSize));
// std::map<std::string, std::reference_wrapper<int>> innerMapVideoSize;
// innerMapVideoSize.insert(std::make_pair("video_size", std::reference_wrapper<int>(mAllData.videoSize)));
// mCfgMapInt.insert(std::make_pair(IpcConfigKey::VIDEO_LENGTH, innerMapVideoSize));
// std::map<std::string, std::reference_wrapper<int>> innerMapInfraredLampPower;
// innerMapInfraredLampPower.insert(
// std::make_pair("infrared_lamp_power", std::reference_wrapper<int>(mAllData.mInfraredPower)));
// mCfgMapInt.insert(std::make_pair(IpcConfigKey::INFRARED_POWER, innerMapInfraredLampPower));
// std::map<std::string, std::reference_wrapper<int>> innerMapDelayed;
// innerMapDelayed.insert(std::make_pair("delayed", std::reference_wrapper<int>(mAllData.delayed)));
// mCfgMapInt.insert(std::make_pair(IpcConfigKey::PIR_DELAYED, innerMapDelayed));
// std::map<std::string, std::reference_wrapper<int>> innerMapPirSensitivity;
// innerMapPirSensitivity.insert(
// std::make_pair("pir_sensitivity", std::reference_wrapper<int>(mAllData.mPirSensitivity)));
// mCfgMapInt.insert(std::make_pair(IpcConfigKey::PIR_SENSITIVITY, innerMapPirSensitivity));
// std::map<std::string, std::reference_wrapper<bool>> innerMapStorageLoopSwitch;
// innerMapStorageLoopSwitch.insert(
// std::make_pair("storage_loop_switch", std::reference_wrapper<bool>(mAllData.mStorageLoopSwitch)));
// mCfgMapBool.insert(std::make_pair(IpcConfigKey::STORAGE_LOOP_SWITCH, innerMapStorageLoopSwitch));
// std::map<std::string, std::reference_wrapper<bool>> innerMapFactoryReset;
// innerMapFactoryReset.insert(std::make_pair("factory_reset",
// std::reference_wrapper<bool>(mAllData.factoryReset)));
// mCfgMapBool.insert(std::make_pair(IpcConfigKey::FACTORY_RESET_FLAG, innerMapFactoryReset));
// std::map<std::string, std::reference_wrapper<bool>> innerMapFormattingSDCard;
// innerMapFormattingSDCard.insert(
// std::make_pair("formatting_SD_card", std::reference_wrapper<bool>(mAllData.formattingSDCard)));
// mCfgMapBool.insert(std::make_pair(IpcConfigKey::FORMATTING_SD_CARD, innerMapFormattingSDCard));
// std::map<std::string, std::reference_wrapper<CHAR_STRING>> innerMapDrakMode;
// innerMapDrakMode.insert(std::make_pair("dark_mode", std::reference_wrapper<CHAR_STRING>(mAllData.darkMode)));
// mCfgMapString.insert(std::make_pair(IpcConfigKey::DARK_MODE, innerMapDrakMode));
// std::map<std::string, std::reference_wrapper<CHAR_STRING>> innerMapWorkInterval;
// innerMapWorkInterval.insert(
// std::make_pair("work_interval", std::reference_wrapper<CHAR_STRING>(mAllData.workingInterval)));
// mCfgMapString.insert(std::make_pair(IpcConfigKey::WORK_INTERVAL, innerMapWorkInterval));
// std::map<std::string, std::reference_wrapper<short>> innerMapShort;
// innerMapShort.insert(std::make_pair("test_short", std::reference_wrapper<short>(mAllData.testShort)));
// mCfgMapShort.insert(std::make_pair(IpcConfigKey::TEST_SHORT, innerMapShort));
// std::map<std::string, std::reference_wrapper<long>> innerMapLong;
// innerMapLong.insert(std::make_pair("test_long", std::reference_wrapper<long>(mAllData.testLong)));
// mCfgMapLong.insert(std::make_pair(IpcConfigKey::TEST_LONG, innerMapLong));
// std::map<std::string, std::reference_wrapper<long long>> innerMapLLong;
// innerMapLLong.insert(std::make_pair("test_llong", std::reference_wrapper<long long>(mAllData.testLLong)));
// mCfgMapLLong.insert(std::make_pair(IpcConfigKey::TEST_LLONG, innerMapLLong));
// std::map<std::string, std::reference_wrapper<char>> innerMapChar;
// innerMapChar.insert(std::make_pair("test_char", std::reference_wrapper<char>(mAllData.testChar)));
// mCfgMapChar.insert(std::make_pair(IpcConfigKey::TEST_CHAR, innerMapChar));
// std::map<std::string, std::reference_wrapper<float>> innerMapFloat;
// innerMapFloat.insert(std::make_pair("test_float", std::reference_wrapper<float>(mAllData.testFloat)));
// mCfgMapFloat.insert(std::make_pair(IpcConfigKey::TEST_FLOAT, innerMapFloat));
}
const StatusCode IpcConfigImpl::Init(void)
{
memset(&mAllData, 0, sizeof(Config_s));
mCfg = OpenConfigFile(IPC_CONFIG_FILE_PATH);
if (nullptr == mCfg) {
LogError("Open config file failed.\n");
return CreateStatusCode(STATUS_CODE_NOT_OK);
}
ReadAllConfigParameters();
return CreateStatusCode(STATUS_CODE_OK);
}
const StatusCode IpcConfigImpl::UnInit(void)
{
if (CONFIG_HAS_CHANGED == mCfgChanged) {
LogInfo("Save config files.\n");
ConfigSaveFile(mCfg);
}
CloseConfigFile(mCfg);
return CreateStatusCode(STATUS_CODE_OK);
}
const StatusCode IpcConfigImpl::ConfigFileSave(void)
{
return ConfigSaveFile(mCfg);
}
const int IpcConfigImpl::GetInt(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::map<std::string, int *>>::iterator iter;
iter = mCfgMapIntV2.find(key);
if (iter != mCfgMapIntV2.end() && CHECK_MAP(iter->second)) {
return *(iter->second.begin()->second);
}
LogError("Can't find the key.\n");
constexpr int UNKNOWN_CONFIG = -1;
return UNKNOWN_CONFIG;
}
void IpcConfigImpl::SetInt(const IpcConfigKey &key, const int &value)
{
std::map<IpcConfigKey, std::map<std::string, int *>>::iterator iter;
iter = mCfgMapIntV2.find(key);
if (iter != mCfgMapIntV2.end() && CHECK_MAP(iter->second)) {
*(iter->second.begin()->second) = value;
ConfigSetInt(mCfg, iter->second.begin()->first.c_str(), *(iter->second.begin()->second));
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
const short IpcConfigImpl::GetShort(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<short>>>::iterator iter;
iter = mCfgMapShort.find(key);
if (iter != mCfgMapShort.end() && CHECK_MAP(iter->second)) {
return iter->second.begin()->second;
}
LogError("Can't find the key.\n");
constexpr short UNKNOWN_CONFIG = -1;
return UNKNOWN_CONFIG;
}
void IpcConfigImpl::SetShort(const IpcConfigKey &key, const short &value)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<short>>>::iterator iter;
iter = mCfgMapShort.find(key);
if (iter != mCfgMapShort.end() && CHECK_MAP(iter->second)) {
iter->second.begin()->second.get() = value;
const char *name = iter->second.begin()->first.c_str();
ConfigSetShort(mCfg, name, iter->second.begin()->second);
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
const long IpcConfigImpl::GetLong(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<long>>>::iterator iter;
iter = mCfgMapLong.find(key);
if (iter != mCfgMapLong.end() && CHECK_MAP(iter->second)) {
return iter->second.begin()->second;
}
LogError("Can't find the key.\n");
constexpr long UNKNOWN_CONFIG = -1;
return UNKNOWN_CONFIG;
}
void IpcConfigImpl::SetLong(const IpcConfigKey &key, const long &value)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<long>>>::iterator iter;
iter = mCfgMapLong.find(key);
if (iter != mCfgMapLong.end() && CHECK_MAP(iter->second)) {
iter->second.begin()->second.get() = value;
const char *name = iter->second.begin()->first.c_str();
ConfigSetLong(mCfg, name, iter->second.begin()->second);
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
const long long IpcConfigImpl::GetLLong(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<long long>>>::iterator iter;
iter = mCfgMapLLong.find(key);
if (iter != mCfgMapLLong.end() && CHECK_MAP(iter->second)) {
return iter->second.begin()->second;
}
LogError("Can't find the key.\n");
constexpr long long UNKNOWN_CONFIG = -1;
return UNKNOWN_CONFIG;
}
void IpcConfigImpl::SetLLong(const IpcConfigKey &key, const long long &value)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<long long>>>::iterator iter;
iter = mCfgMapLLong.find(key);
if (iter != mCfgMapLLong.end() && CHECK_MAP(iter->second)) {
iter->second.begin()->second.get() = value;
const char *name = iter->second.begin()->first.c_str();
ConfigSetLLong(mCfg, name, iter->second.begin()->second);
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
const char IpcConfigImpl::GetChar(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::map<std::string, char *>>::iterator iter;
iter = mCfgMapCharV2.find(key);
if (iter != mCfgMapCharV2.end() && CHECK_MAP(iter->second)) {
return *(iter->second.begin()->second);
}
LogError("Can't find the key.\n");
constexpr char UNKNOWN_CONFIG = '\0';
return UNKNOWN_CONFIG;
}
void IpcConfigImpl::SetChar(const IpcConfigKey &key, const char &character)
{
std::map<IpcConfigKey, std::map<std::string, char *>>::iterator iter;
iter = mCfgMapCharV2.find(key);
if (iter != mCfgMapCharV2.end() && CHECK_MAP(iter->second)) {
*(iter->second.begin()->second) = character;
const char *name = iter->second.begin()->first.c_str();
ConfigSetChar(mCfg, name, *(iter->second.begin()->second));
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
const float IpcConfigImpl::GetFloat(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<float>>>::iterator iter;
iter = mCfgMapFloat.find(key);
if (iter != mCfgMapFloat.end() && CHECK_MAP(iter->second)) {
return iter->second.begin()->second;
}
LogError("Can't find the key.\n");
constexpr float UNKNOWN_CONFIG = -1.00;
return UNKNOWN_CONFIG;
}
void IpcConfigImpl::SetFloat(const IpcConfigKey &key, const float &value)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<float>>>::iterator iter;
iter = mCfgMapFloat.find(key);
if (iter != mCfgMapFloat.end() && CHECK_MAP(iter->second)) {
iter->second.begin()->second.get() = value;
const char *name = iter->second.begin()->first.c_str();
ConfigSetFloat(mCfg, name, iter->second.begin()->second);
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
const double IpcConfigImpl::GetDouble(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<double>>>::iterator iter;
iter = mCfgMapDouble.find(key);
if (iter != mCfgMapDouble.end() && CHECK_MAP(iter->second)) {
return iter->second.begin()->second;
}
LogError("Can't find the key.\n");
constexpr double UNKNOWN_CONFIG = -1.00;
return UNKNOWN_CONFIG;
}
void IpcConfigImpl::SetDouble(const IpcConfigKey &key, const double &value)
{
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<double>>>::iterator iter;
iter = mCfgMapDouble.find(key);
if (iter != mCfgMapDouble.end() && CHECK_MAP(iter->second)) {
iter->second.begin()->second.get() = value;
const char *name = iter->second.begin()->first.c_str();
ConfigSetDouble(mCfg, name, iter->second.begin()->second);
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
const long double IpcConfigImpl::GetLongDouble(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::reference_wrapper<long double>>::iterator iter;
iter = mCfgMapLongDouble.find(key);
if (iter != mCfgMapLongDouble.end()) {
return iter->second;
}
LogError("Can't find the key.\n");
constexpr long double UNKNOWN_CONFIG = -1.0;
return UNKNOWN_CONFIG;
}
void IpcConfigImpl::SetLongDouble(const IpcConfigKey &key, const long double &value)
{
std::map<IpcConfigKey, std::reference_wrapper<long double>>::iterator iter;
iter = mCfgMapLongDouble.find(key);
if (iter != mCfgMapLongDouble.end()) {
iter->second.get() = value;
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
const bool IpcConfigImpl::GetBool(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::map<std::string, bool *>>::iterator iter;
iter = mCfgMapBoolV2.find(key);
if (iter != mCfgMapBoolV2.end() && CHECK_MAP(iter->second)) {
return *(iter->second.begin()->second);
}
LogError("Can't find the key.\n");
constexpr bool UNKNOWN_CONFIG = false;
return UNKNOWN_CONFIG;
}
void IpcConfigImpl::SetBool(const IpcConfigKey &key, const bool &value)
{
std::map<IpcConfigKey, std::map<std::string, bool *>>::iterator iter;
iter = mCfgMapBoolV2.find(key);
if (iter != mCfgMapBoolV2.end() && CHECK_MAP(iter->second)) {
*(iter->second.begin()->second) = value;
ConfigSetBool(mCfg, iter->second.begin()->first.c_str(), *(iter->second.begin()->second));
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
const std::string IpcConfigImpl::GetString(const IpcConfigKey &key)
{
std::map<IpcConfigKey, std::map<std::string, StringConfigPack>>::iterator iter;
iter = mCfgMapStringV2.find(key);
if (iter != mCfgMapStringV2.end() && CHECK_MAP(iter->second)) {
const std::string sv(iter->second.begin()->second.mValue); // char[] --> const std::strinbg
return sv;
}
LogError("Can't find the key.\n");
const std::string UNKNOWN_CONFIG = "undefine";
return UNKNOWN_CONFIG;
}
void IpcConfigImpl::SetString(const IpcConfigKey &key, const std::string &string)
{
std::map<IpcConfigKey, std::map<std::string, StringConfigPack>>::iterator iter;
iter = mCfgMapStringV2.find(key);
if (iter != mCfgMapStringV2.end() && CHECK_MAP(iter->second)) {
if (string.length() > iter->second.begin()->second.mLength) {
LogError("The string is too long. Your string is %s, the max length is %d\n",
string.c_str(),
iter->second.begin()->second.mLength);
return;
}
strncpy(iter->second.begin()->second.mValue, string.c_str(), iter->second.begin()->second.mLength);
ConfigSetString(mCfg, iter->second.begin()->first.c_str(), iter->second.begin()->second.mValue);
mCfgChanged = CONFIG_HAS_CHANGED;
}
else {
LogError("Can't find the key.\n");
}
}
WorkMode IpcConfigImpl::GetWorkMode(void)
{
return static_cast<WorkMode>(GetChar(IpcConfigKey::WORK_MODE));
}
void IpcConfigImpl::SetWorkMode(const WorkMode &mode)
{
SetChar(IpcConfigKey::WORK_MODE, static_cast<char>(mode));
}
ConfigSwitch IpcConfigImpl::GetSwitch(const IpcConfigKey &key)
{
bool value = GetBool(key);
return value == true ? ConfigSwitch::ON : ConfigSwitch::OFF;
}
void IpcConfigImpl::SetSwitch(const IpcConfigKey &key, const ConfigSwitch &value)
{
bool config = value == ConfigSwitch::ON ? true : false;
SetBool(key, config);
}
ConfigLevel IpcConfigImpl::GetLevel(const IpcConfigKey &key)
{
char value = GetChar(key);
return static_cast<ConfigLevel>(value);
}
void IpcConfigImpl::SetLevel(const IpcConfigKey &key, const ConfigLevel &value)
{
char config = static_cast<char>(value);
SetChar(key, config);
}
void IpcConfigImpl::ReadAllConfigParameters(void)
{
{
const char *config = nullptr;
StatusCode code = ConfigGetString(mCfg, CONFIG_WIFI_SSID, &config);
if (StatusCodeEqual(code, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("CONFIG_WIFI_SSID doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
strncpy(mAllData.mWifiSsid, CONFIG_WIFI_SSID_DEFAULT, sizeof(mAllData.mWifiSsid));
ConfigSetString(mCfg, CONFIG_WIFI_SSID, mAllData.mWifiSsid);
}
else {
if (nullptr != config) {
strncpy(mAllData.mWifiSsid, config, sizeof(mAllData.mWifiSsid));
}
else {
LogError("CONFIG_WIFI_SSID get failed.\n");
}
}
}
{
const char *config = nullptr;
StatusCode code = ConfigGetString(mCfg, CONFIG_WIFI_PASSWORD, &config);
if (StatusCodeEqual(code, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("CONFIG_WIFI_PASSWORD doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
strncpy(mAllData.mWifiPassword, CONFIG_WIFI_PASSWORD_DEFAULT, sizeof(mAllData.mWifiPassword));
ConfigSetString(mCfg, CONFIG_WIFI_PASSWORD, mAllData.mWifiPassword);
}
else {
if (nullptr != config) {
strncpy(mAllData.mWifiPassword, config, sizeof(mAllData.mWifiPassword));
}
else {
LogError("CONFIG_WIFI_PASSWORD get failed.\n");
}
}
}
{
StatusCode code = ConfigGetChar(mCfg, CONFIG_WORK_MODE, &mAllData.mWorkMode);
if (StatusCodeEqual(code, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("CONFIG_WORK_MODE doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.mWorkMode = CONFIG_WORK_MODE_DEFAULT;
ConfigSetChar(mCfg, CONFIG_WORK_MODE, mAllData.mWorkMode);
}
}
{
StatusCode code = ConfigGetChar(mCfg, CONFIG_INFRARED_POWER, &mAllData.mInfraredPower);
if (StatusCodeEqual(code, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("CONFIG_INFRARED_POWER doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.mInfraredPower = CONFIG_INFRARED_POWER_DEFAULT;
ConfigSetChar(mCfg, CONFIG_INFRARED_POWER, mAllData.mInfraredPower);
}
}
{
int config = -1;
StatusCode code = ConfigGetInt(mCfg, CONFIG_CONTINUE_SHOT, &config);
if (StatusCodeEqual(code, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("CONFIG_CONTINUE_SHOT doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.mContinuousShot = CONFIG_CONTINUE_SHOT_DEFAULT;
ConfigSetInt(mCfg, CONFIG_CONTINUE_SHOT, mAllData.mContinuousShot);
}
else {
mAllData.mContinuousShot = config;
}
}
{
int config = -1;
StatusCode code = ConfigGetInt(mCfg, CONFIG_BURST_PHOTO_INTERVAL, &config);
if (StatusCodeEqual(code, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("CONFIG_BURST_PHOTO_INTERVAL doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.mBurstPhotoInterval = CONFIG_BURST_PHOTO_INTERVAL_DEFAULT;
ConfigSetInt(mCfg, CONFIG_BURST_PHOTO_INTERVAL, mAllData.mBurstPhotoInterval);
}
else {
mAllData.mBurstPhotoInterval = config;
}
}
{
int config = -1;
StatusCode code = ConfigGetInt(mCfg, CONFIG_IMAGE_SIZE, &config);
if (StatusCodeEqual(code, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("CONFIG_IMAGE_SIZE doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.mImageSize = CONFIG_IMAGE_SIZE_DEFAULT;
ConfigSetInt(mCfg, CONFIG_IMAGE_SIZE, mAllData.mImageSize);
}
else {
mAllData.mImageSize = config;
}
}
{
int config = -1;
StatusCode code = ConfigGetInt(mCfg, CONFIG_VIDEO_SIZE, &config);
if (StatusCodeEqual(code, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("CONFIG_VIDEO_SIZE doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.mVideoLength = CONFIG_VIDEO_SIZE_DEFAULT;
ConfigSetInt(mCfg, CONFIG_VIDEO_SIZE, mAllData.mVideoLength);
}
else {
mAllData.mVideoLength = config;
}
}
{
int config = -1;
StatusCode code = ConfigGetInt(mCfg, CONFIG_PIR_DELAYED, &config);
if (StatusCodeEqual(code, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("CONFIG_PIR_DELAYED doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.mPirDelayed = CONFIG_PIR_DELAYED_DEFAULT;
ConfigSetInt(mCfg, CONFIG_PIR_DELAYED, mAllData.mPirDelayed);
}
else {
mAllData.mPirDelayed = config;
}
}
{
int config = -1;
StatusCode code = ConfigGetInt(mCfg, CONFIG_PIR_SENSITIVITY, &config);
if (StatusCodeEqual(code, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("CONFIG_PIR_SENSITIVITY doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.mPirSensitivity = CONFIG_PIR_SENSITIVITY_DEFAULT;
ConfigSetInt(mCfg, CONFIG_PIR_SENSITIVITY, mAllData.mPirSensitivity);
}
else {
mAllData.mPirSensitivity = config;
}
}
{
StatusCode code = ConfigGetBool(mCfg, CONFIG_STORAGE_LOOP, &(mAllData.mStorageLoopSwitch));
if (StatusCodeEqual(code, "CONFIG_CODE_PARAM_NOT_EXIST")) {
LogWarning("CONFIG_STORAGE_LOOP doesn't exist, will make it as default.\n");
mCfgChanged = CONFIG_HAS_CHANGED;
mAllData.mStorageLoopSwitch = CONFIG_STORAGE_LOOP_DEFAULT;
ConfigSetBool(mCfg, CONFIG_STORAGE_LOOP, mAllData.mStorageLoopSwitch);
}
}
// StatusCode workModeCode = ConfigGetInt(mCfg, "work_mode", &(mAllData.mWorkMode));
// if (StatusCodeEqual(workModeCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
// LogWarning("work_mode doesn't exist, will make it as default.\n");
// mCfgChanged = CONFIG_HAS_CHANGED;
// mAllData.mWorkMode = MODE_PIC;
// ConfigSetInt(mCfg, "work_mode", mAllData.mWorkMode);
// }
// StatusCode continuousShotCode = ConfigGetInt(mCfg, "continuous_shot", &(mAllData.mContinuousShot));
// if (StatusCodeEqual(continuousShotCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
// LogWarning("continuous_shot doesn't exist, will make it as default.\n");
// mCfgChanged = CONFIG_HAS_CHANGED;
// mAllData.mContinuousShot = CONTINUOUS_SHOT_ONE_PIC;
// ConfigSetInt(mCfg, "continuous_shot", mAllData.mContinuousShot);
// }
// StatusCode burstPhotoIntervalCode = ConfigGetInt(mCfg, "burst_photo_interval",
// &(mAllData.mBurstPhotoInterval)); if (StatusCodeEqual(burstPhotoIntervalCode, "CONFIG_CODE_PARAM_NOT_EXIST"))
// {
// LogWarning("burst_photo_interval doesn't exist, will make it as default.\n");
// mCfgChanged = CONFIG_HAS_CHANGED;
// mAllData.mBurstPhotoInterval = BURST_PHOTO_INTERVAL_DEFAULT;
// ConfigSetInt(mCfg, "burst_photo_interval", mAllData.mBurstPhotoInterval);
// }
// const char *imageSizeString = nullptr;
// StatusCode imageSizeCode = ConfigGetString(mCfg, "image_size", &(imageSizeString));
// if (StatusCodeEqual(imageSizeCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
// LogWarning("image_size doesn't exist, will make it as default.\n");
// mCfgChanged = CONFIG_HAS_CHANGED;
// char defaultImageSize[] = "320*320";
// strncpy(mAllData.imageSize, defaultImageSize, sizeof(mAllData.imageSize));
// ConfigSetString(mCfg, "image_size", mAllData.imageSize);
// }
// else {
// if (nullptr != imageSizeString) {
// strncpy(mAllData.imageSize, imageSizeString, sizeof(mAllData.imageSize));
// }
// else {
// LogError("image_size get failed.\n");
// }
// }
// StatusCode videoSizeCode = ConfigGetInt(mCfg, "video_size", &(mAllData.videoSize));
// if (StatusCodeEqual(videoSizeCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
// LogWarning("video_size doesn't exist, will make it as default.\n");
// mCfgChanged = CONFIG_HAS_CHANGED;
// mAllData.videoSize = VIDEO_SIZE_15;
// ConfigSetInt(mCfg, "video_size", mAllData.videoSize);
// }
// StatusCode infraredIampPowerCode = ConfigGetInt(mCfg, "infrared_lamp_power", &(mAllData.mInfraredPower));
// if (StatusCodeEqual(infraredIampPowerCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
// LogWarning("infrared_lamp_power doesn't exist, will make it as default.\n");
// mCfgChanged = CONFIG_HAS_CHANGED;
// mAllData.mInfraredPower = INFRARED_IAMP_POWER_MID;
// ConfigSetInt(mCfg, "infrared_lamp_power", mAllData.mInfraredPower);
// }
// StatusCode delayedCode = ConfigGetInt(mCfg, "delayed", &(mAllData.delayed));
// if (StatusCodeEqual(delayedCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
// LogWarning("delayed doesn't exist, will make it as default.\n");
// mCfgChanged = CONFIG_HAS_CHANGED;
// mAllData.delayed = DELAYED_DEFAULT;
// ConfigSetInt(mCfg, "delayed", mAllData.delayed);
// }
// StatusCode pirSensitivityCode = ConfigGetInt(mCfg, "pir_sensitivity", &(mAllData.mPirSensitivity));
// if (StatusCodeEqual(pirSensitivityCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
// LogWarning("pir_sensitivity doesn't exist, will make it as default.\n");
// mCfgChanged = CONFIG_HAS_CHANGED;
// mAllData.mPirSensitivity = PIR_SENSITIVITY_DEFAULT;
// ConfigSetInt(mCfg, "pir_sensitivity", mAllData.mPirSensitivity);
// }
// StatusCode storageLoopSwitchCode = ConfigGetBool(mCfg, "storage_loop_switch",
// &(mAllData.mStorageLoopSwitch)); if (StatusCodeEqual(storageLoopSwitchCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
// LogWarning("storage_loop_switch doesn't exist, will make it as default.\n");
// mCfgChanged = CONFIG_HAS_CHANGED;
// constexpr bool storageLoopSwitch = true;
// mAllData.mStorageLoopSwitch = storageLoopSwitch;
// ConfigSetBool(mCfg, "storage_loop_switch", mAllData.mStorageLoopSwitch);
// }
// StatusCode factoryResetCode = ConfigGetBool(mCfg, "factory_reset", &(mAllData.factoryReset));
// if (StatusCodeEqual(factoryResetCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
// LogWarning("factory_resetCode doesn't exist, will make it as default.\n");
// mCfgChanged = CONFIG_HAS_CHANGED;
// constexpr bool factoryReset = true;
// mAllData.factoryReset = factoryReset;
// ConfigSetBool(mCfg, "factory_reset", mAllData.factoryReset);
// }
// StatusCode formattingSDCardCode = ConfigGetBool(mCfg, "formatting_SD_card", &(mAllData.formattingSDCard));
// if (StatusCodeEqual(formattingSDCardCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
// LogWarning("formatting_SD_card doesn't exist, will make it as default.\n");
// mCfgChanged = CONFIG_HAS_CHANGED;
// constexpr bool formattingSDCard = true;
// mAllData.formattingSDCard = formattingSDCard;
// ConfigSetBool(mCfg, "formatting_SD_card", mAllData.formattingSDCard);
// }
// const char *darkModeString = nullptr;
// StatusCode darkModeCode = ConfigGetString(mCfg, "dark_mode", &(darkModeString));
// if (StatusCodeEqual(darkModeCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
// LogWarning("dark_mode doesn't exist, will make it as default.\n");
// mCfgChanged = CONFIG_HAS_CHANGED;
// char defaultDarkMode[] = "19:00:00-07:00:00";
// strncpy(mAllData.darkMode, defaultDarkMode, sizeof(mAllData.darkMode));
// ConfigSetString(mCfg, "dark_mode", mAllData.darkMode);
// }
// else {
// if (nullptr != darkModeString) {
// strncpy(mAllData.darkMode, darkModeString, sizeof(mAllData.darkMode));
// }
// else {
// LogError("dark_mode get failed.\n");
// }
// }
// const char *workIntervalString = nullptr;
// StatusCode workIntervalCode = ConfigGetString(mCfg, "work_interval", &(workIntervalString));
// if (StatusCodeEqual(workIntervalCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
// LogWarning("work_interval doesn't exist, will make it as default.\n");
// mCfgChanged = CONFIG_HAS_CHANGED;
// char defaultWorkInterval[] = "07:00:01-07:00:00";
// strncpy(mAllData.workingInterval, defaultWorkInterval, sizeof(mAllData.workingInterval));
// ConfigSetString(mCfg, "work_interval", mAllData.workingInterval);
// }
// else {
// if (nullptr != workIntervalString) {
// strncpy(mAllData.workingInterval, workIntervalString, sizeof(mAllData.workingInterval));
// }
// else {
// LogError("work_interval get failed.\n");
// }
// }
// StatusCode shortCode = ConfigGetShort(mCfg, "test_short", &(mAllData.testShort));
// if (StatusCodeEqual(shortCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
// LogWarning("test_short doesn't exist, will make it as default.\n");
// mCfgChanged = CONFIG_HAS_CHANGED;
// constexpr short DEFAULT_TEST_SHORT_NUM = 11;
// mAllData.testShort = DEFAULT_TEST_SHORT_NUM;
// ConfigSetShort(mCfg, "test_short", mAllData.testShort);
// }
// StatusCode longCode = ConfigGetLong(mCfg, "test_long", &(mAllData.testLong));
// if (StatusCodeEqual(longCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
// LogWarning("test_long doesn't exist, will make it as default.\n");
// mCfgChanged = CONFIG_HAS_CHANGED;
// constexpr long DEFAULT_TEST_LONG_NUM = 12;
// mAllData.testLong = DEFAULT_TEST_LONG_NUM;
// ConfigSetLong(mCfg, "test_long", mAllData.testLong);
// }
// StatusCode llongCode = ConfigGetLLong(mCfg, "test_llong", &(mAllData.testLLong));
// if (StatusCodeEqual(llongCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
// LogWarning("test_llong doesn't exist, will make it as default.\n");
// mCfgChanged = CONFIG_HAS_CHANGED;
// constexpr long long DEFAULT_TEST_LLONG_NUM = 13;
// mAllData.testLLong = DEFAULT_TEST_LLONG_NUM;
// ConfigSetLLong(mCfg, "test_llong", mAllData.testLLong);
// }
// StatusCode charCode = ConfigGetChar(mCfg, "test_char", &(mAllData.testChar));
// if (StatusCodeEqual(charCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
// LogWarning("test_char doesn't exist, will make it as default.\n");
// mCfgChanged = CONFIG_HAS_CHANGED;
// constexpr char DEFAULT_TEST_CHAR_NUM = 'B';
// mAllData.testChar = DEFAULT_TEST_CHAR_NUM;
// ConfigSetChar(mCfg, "test_char", mAllData.testChar);
// }
// StatusCode floatCode = ConfigGetFloat(mCfg, "test_float", &(mAllData.testFloat));
// if (StatusCodeEqual(floatCode, "CONFIG_CODE_PARAM_NOT_EXIST")) {
// LogWarning("test_float doesn't exist, will make it as default.\n");
// mCfgChanged = CONFIG_HAS_CHANGED;
// constexpr float DEFAULT_TEST_FLOAT_NUM = 1.123456;
// mAllData.testFloat = DEFAULT_TEST_FLOAT_NUM;
// ConfigSetFloat(mCfg, "test_float", mAllData.testFloat);
// }
// if (CONFIG_HAS_CHANGED == mCfgChanged) {
// LogInfo("Save the config file.\n");
// mCfgChanged = CONFIG_HAS_NOT_CHANGED;
// ConfigSaveFile(mCfg);
// }
}

View File

@ -12,8 +12,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef IPCCONFIG_H
#define IPCCONFIG_H
#ifndef IPC_CONFIG_IMPL_H
#define IPC_CONFIG_IMPL_H
#include "ConfigBase.h"
#include "IIpcConfig.h"
#include "StatusCode.h"
@ -21,20 +21,37 @@
#include <memory>
constexpr bool CONFIG_HAS_CHANGED = true;
constexpr bool CONFIG_HAS_NOT_CHANGED = false;
constexpr unsigned int WIFI_SSID_BUFF_SIZE = 64;
constexpr unsigned int WIFI_PASSWORD_BUFF_SIZE = 8;
constexpr unsigned int WORKING_TIME_MAX_ALLOW = 32;
constexpr char CONFIG_WORK_MODE_DEFAULT = static_cast<char>(WorkMode::MODE_PIC);
constexpr char CONFIG_INFRARED_POWER_DEFAULT = static_cast<char>(ConfigLevel::LOW);
constexpr int CONFIG_CONTINUE_SHOT_DEFAULT = 1;
constexpr int CONFIG_BURST_PHOTO_INTERVAL_DEFAULT = 0;
constexpr int CONFIG_IMAGE_SIZE_DEFAULT = 8;
constexpr int CONFIG_VIDEO_SIZE_DEFAULT = 10;
constexpr int CONFIG_PIR_DELAYED_DEFAULT = 0;
constexpr int CONFIG_PIR_SENSITIVITY_DEFAULT = 9;
constexpr bool CONFIG_STORAGE_LOOP_DEFAULT = true;
typedef char CHAR_STRING[64];
typedef struct Config_s
typedef struct string_config_pack
{
bool storageLoopSwitch;
unsigned int mLength;
char *mValue;
} StringConfigPack;
typedef struct __attribute__((packed)) Config_s
{
bool mStorageLoopSwitch;
bool factoryReset;
bool formattingSDCard;
int workMode;
int continuousShot;
int burstPhotoInterval;
int videoSize;
int infraredIampPower;
int delayed;
int pirSensitivity;
char mWorkMode;
int mContinuousShot;
int mBurstPhotoInterval;
int mImageSize;
int mVideoLength;
char mInfraredPower;
int mPirDelayed;
int mPirSensitivity;
short testShort;
char testChar;
long testLong;
@ -43,6 +60,10 @@ typedef struct Config_s
CHAR_STRING imageSize;
CHAR_STRING darkMode;
CHAR_STRING workingInterval;
char mWifiSsid[WIFI_SSID_BUFF_SIZE + 1];
char mWifiPassword[WIFI_PASSWORD_BUFF_SIZE + 1];
unsigned char mWorkingTimeFrom[WORKING_TIME_MAX_ALLOW];
unsigned char mWorkingTimeTo[WORKING_TIME_MAX_ALLOW];
} Config_s;
class MapInt
{
@ -50,11 +71,11 @@ public:
MapInt() = default;
~MapInt() = default;
};
class IpcConfig : public IIpcConfig
class IpcConfigImpl : public IIpcConfig
{
public:
IpcConfig();
virtual ~IpcConfig() = default;
IpcConfigImpl();
virtual ~IpcConfigImpl() = default;
const StatusCode Init(void) override;
const StatusCode UnInit(void) override;
const StatusCode ConfigFileSave(void) override;
@ -77,7 +98,13 @@ public:
const bool GetBool(const IpcConfigKey &key) override;
void SetBool(const IpcConfigKey &key, const bool &value) override;
const std::string GetString(const IpcConfigKey &key) override;
void SetString(const IpcConfigKey &key, const std::string string) override;
void SetString(const IpcConfigKey &key, const std::string &string) override;
WorkMode GetWorkMode(void) override;
void SetWorkMode(const WorkMode &mode) override;
ConfigSwitch GetSwitch(const IpcConfigKey &key) override;
void SetSwitch(const IpcConfigKey &key, const ConfigSwitch &value) override;
ConfigLevel GetLevel(const IpcConfigKey &key) override;
void SetLevel(const IpcConfigKey &key, const ConfigLevel &value) override;
private:
void ReadAllConfigParameters(void);
@ -87,14 +114,18 @@ private:
void *mCfg;
Config_s mAllData;
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<int>>> mCfgMapInt;
std::map<IpcConfigKey, std::map<std::string, int *>> mCfgMapIntV2;
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<short>>> mCfgMapShort;
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<long>>> mCfgMapLong;
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<long long>>> mCfgMapLLong;
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<char>>> mCfgMapChar;
std::map<IpcConfigKey, std::map<std::string, char *>> mCfgMapCharV2;
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<float>>> mCfgMapFloat;
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<double>>> mCfgMapDouble;
std::map<IpcConfigKey, std::reference_wrapper<long double>> mCfgMapLongDouble;
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<bool>>> mCfgMapBool;
std::map<IpcConfigKey, std::map<std::string, bool *>> mCfgMapBoolV2;
std::map<IpcConfigKey, std::map<std::string, std::reference_wrapper<CHAR_STRING>>> mCfgMapString;
std::map<IpcConfigKey, std::map<std::string, StringConfigPack>> mCfgMapStringV2;
};
#endif

View File

@ -14,29 +14,41 @@
*/
#include "IpcConfigMakePtr.h"
#include "ILog.h"
#include "IpcConfig.h"
bool CreateIpcConfig(void)
#include "IpcConfigImpl.h"
bool CreateIpcConfigModule(void)
{
auto instance = std::make_shared<IIpcConfig>();
StatusCode code = IpcConfigMakePtr::GetInstance()->CreateIpcConfig(instance);
if (IsCodeOK(code)) {
LogInfo("CreateIpcConfig is ok.\n");
LogInfo("CreateIpcConfigModule is ok.\n");
IIpcConfig::GetInstance(&instance);
return true;
}
return false;
}
bool DestroyIpcConfigModule(void)
{
auto instance = std::make_shared<IIpcConfig>();
IIpcConfig::GetInstance(&instance);
return true;
}
std::shared_ptr<IpcConfigMakePtr> &IpcConfigMakePtr::GetInstance(std::shared_ptr<IpcConfigMakePtr> *impl)
{
static auto instance = std::make_shared<IpcConfigMakePtr>();
if (impl) {
instance = *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;
}
const StatusCode IpcConfigMakePtr::CreateIpcConfig(std::shared_ptr<IIpcConfig> &impl)
{
auto tmp = std::make_shared<IpcConfig>();
auto tmp = std::make_shared<IpcConfigImpl>();
impl = tmp;
return CreateStatusCode(STATUS_CODE_OK);
}

View File

@ -5,7 +5,7 @@
| 版本 | 时间 | 说明 |
| ---- | ---- | ---- |
| V1.0 | 2024-5-14 | 首次评审。 |
| V1.1 | 2024-5-15 | 完善时序图。 |
| V1.1 | 2024-5-16 | 完善时序图。 |
## 1.1. 基本概念

View File

@ -133,8 +133,8 @@ public:
VMcuMonitor() = default;
virtual ~VMcuMonitor() = default;
virtual void RecvIpcMissionEvent(std::shared_ptr<VMcuRecv> &recv, const IpcMission &mission);
virtual void RecvMcuHeartBeat(std::shared_ptr<VMcuRecv> &recv);
virtual void RecvGetIntervalStart(std::shared_ptr<VMcuRecv> &recv);
virtual void RecvMcuHeartBeatEvent(std::shared_ptr<VMcuRecv> &recv);
virtual void RecvGetIntervalStartEvent(std::shared_ptr<VMcuRecv> &recv);
virtual void RecvGetDateTime(std::shared_ptr<VMcuRecv> &recv);
virtual void RecvGetPirSensitivity(std::shared_ptr<VMcuRecv> &recv);
};

View File

@ -52,10 +52,10 @@ void VMcuRecv::ReplyFinished(const bool result)
void VMcuMonitor::RecvIpcMissionEvent(std::shared_ptr<VMcuRecv> &recv, const IpcMission &mission)
{
}
void VMcuMonitor::RecvMcuHeartBeat(std::shared_ptr<VMcuRecv> &recv)
void VMcuMonitor::RecvMcuHeartBeatEvent(std::shared_ptr<VMcuRecv> &recv)
{
}
void VMcuMonitor::RecvGetIntervalStart(std::shared_ptr<VMcuRecv> &recv)
void VMcuMonitor::RecvGetIntervalStartEvent(std::shared_ptr<VMcuRecv> &recv)
{
}
void VMcuMonitor::RecvGetDateTime(std::shared_ptr<VMcuRecv> &recv)

View File

@ -35,6 +35,11 @@ McuManagerImpl::McuManagerImpl()
mMcuAskHandle[OtherSideSendType::SEND_IPC_MISSION] =
std::bind(&McuManagerImpl::McuAskSendIpcMissionHandle, this, _1);
mMcuAskHandle[OtherSideSendType::SEND_HEART_BEAT] = std::bind(&McuManagerImpl::McuAskSendHeartBeatHandle, this, _1);
mMcuAskHandle[OtherSideSendType::GET_INTERVAL_START] =
std::bind(&McuManagerImpl::McuAskGetIntervalStartHandle, this, _1);
mMcuAskHandle[OtherSideSendType::GET_DATE_TIME] = std::bind(&McuManagerImpl::McuAskGetDateTimeHandle, this, _1);
mMcuAskHandle[OtherSideSendType::GET_PIR_SENSITIVITY] =
std::bind(&McuManagerImpl::McuAskGetPirSensitivityHandle, this, _1);
}
std::shared_ptr<VProtocolBase> McuManagerImpl::SharedFromThis(void)
{
@ -57,6 +62,7 @@ const StatusCode McuManagerImpl::UnInit(void)
const StatusCode McuManagerImpl::SetMcuMonitor(std::shared_ptr<VMcuMonitor> &monitor)
{
std::lock_guard<std::mutex> locker(mMutex);
LogInfo("SetMcuMonitor.\n");
mMonitor = monitor;
for (auto ask : mMcuAskList) {
std::shared_ptr<McuRecvImpl> data = std::dynamic_pointer_cast<McuRecvImpl>(ask);
@ -161,6 +167,7 @@ void McuManagerImpl::OtherSideSendIpcMission(const unsigned int &serialNumber, c
~McuRecvIpcMission() = default;
void ReplyFinished(const bool result) override
{
LogInfo("OtherSideSendIpcMission finised.\n");
McuRecvImpl::mMcuManager->ReplyOtherSideSendIpcMission(ASK_RESULT::SUCCEED, McuRecvImpl::mSerialNumber);
}
};
@ -169,6 +176,7 @@ void McuManagerImpl::OtherSideSendIpcMission(const unsigned int &serialNumber, c
std::shared_ptr<VMcuRecv> recv =
std::make_shared<McuRecvIpcMission>(manager, serialNumber, OtherSideSendType::SEND_IPC_MISSION, mission);
if (monitor) {
LogInfo("Mcu manager report recv ipc mission to mcu monitor.\n");
monitor->RecvIpcMissionEvent(recv, static_cast<IpcMission>(mission));
}
else {
@ -190,6 +198,7 @@ void McuManagerImpl::OtherSideSendHearBeat(const unsigned int &serialNumber)
~McuRecvHeartBeat() = default;
void ReplyFinished(const bool result) override
{
LogInfo("OtherSideSendHearBeat finised.\n");
McuRecvImpl::mMcuManager->ReplyOtherSideSendHeartBeat(McuRecvImpl::mSerialNumber);
}
};
@ -199,7 +208,7 @@ void McuManagerImpl::OtherSideSendHearBeat(const unsigned int &serialNumber)
std::make_shared<McuRecvHeartBeat>(manager, serialNumber, OtherSideSendType::SEND_HEART_BEAT);
if (monitor) {
LogInfo("Mcu manager report heart beat to mcu monitor.\n");
monitor->RecvMcuHeartBeat(recv);
monitor->RecvMcuHeartBeatEvent(recv);
}
else {
LogWarning("mMonitor is nullptr, AddMcuRecv.\n");
@ -220,6 +229,7 @@ void McuManagerImpl::OtherSideSendGetIntervalStart(const unsigned int &serialNum
~McuRecvGetIntervalStart() = default;
void ReplyFinished(const bool result) override
{
LogInfo("OtherSideSendGetIntervalStart finised.\n");
McuRecvImpl::mMcuManager->ReplyOtherSideSendGetIntervalStart(mDataRecvReply, McuRecvImpl::mSerialNumber);
}
};
@ -228,8 +238,8 @@ void McuManagerImpl::OtherSideSendGetIntervalStart(const unsigned int &serialNum
std::shared_ptr<VMcuRecv> recv =
std::make_shared<McuRecvGetIntervalStart>(manager, serialNumber, OtherSideSendType::GET_INTERVAL_START);
if (monitor) {
LogInfo("Mcu manager report heart beat to mcu monitor.\n");
monitor->RecvMcuHeartBeat(recv);
LogInfo("Mcu manager report get interval start to mcu monitor.\n");
monitor->RecvGetIntervalStartEvent(recv);
}
else {
LogWarning("mMonitor is nullptr, AddMcuRecv.\n");
@ -250,6 +260,7 @@ void McuManagerImpl::OtherSideSendGetDateTime(const unsigned int &serialNumber)
~McuRecvGetDateTime() = default;
void ReplyFinished(const bool result) override
{
LogInfo("OtherSideSendGetDateTime finised.\n");
McuRecvImpl::mMcuManager->ReplyOtherSideSendGetDateTime(mDataRecvReply, McuRecvImpl::mSerialNumber);
}
};
@ -258,8 +269,8 @@ void McuManagerImpl::OtherSideSendGetDateTime(const unsigned int &serialNumber)
std::shared_ptr<VMcuRecv> recv =
std::make_shared<McuRecvGetDateTime>(manager, serialNumber, OtherSideSendType::GET_DATE_TIME);
if (monitor) {
LogInfo("Mcu manager report heart beat to mcu monitor.\n");
monitor->RecvMcuHeartBeat(recv);
LogInfo("Mcu manager report get date and time to mcu monitor.\n");
monitor->RecvGetDateTime(recv);
}
else {
LogWarning("mMonitor is nullptr, AddMcuRecv.\n");
@ -280,6 +291,7 @@ void McuManagerImpl::OtherSideSendGetPirSensitivity(const unsigned int &serialNu
~McuRecvGetDateTime() = default;
void ReplyFinished(const bool result) override
{
LogInfo("OtherSideSendGetPirSensitivity finised.\n");
McuRecvImpl::mMcuManager->ReplyOtherSideSendGetPirSensitivity(mDataRecvReply.mSensitivity,
McuRecvImpl::mSerialNumber);
}
@ -287,10 +299,10 @@ void McuManagerImpl::OtherSideSendGetPirSensitivity(const unsigned int &serialNu
std::shared_ptr<VMcuMonitor> monitor = GetMcuMonitor();
std::shared_ptr<McuManagerImpl> manager = std::dynamic_pointer_cast<McuManagerImpl>(SharedFromThis());
std::shared_ptr<VMcuRecv> recv =
std::make_shared<McuRecvGetDateTime>(manager, serialNumber, OtherSideSendType::GET_DATE_TIME);
std::make_shared<McuRecvGetDateTime>(manager, serialNumber, OtherSideSendType::GET_PIR_SENSITIVITY);
if (monitor) {
LogInfo("Mcu manager report heart beat to mcu monitor.\n");
monitor->RecvMcuHeartBeat(recv);
LogInfo("Mcu manager report get pir sensitivity to mcu monitor.\n");
monitor->RecvGetPirSensitivity(recv);
}
else {
LogWarning("mMonitor is nullptr, AddMcuRecv.\n");
@ -350,6 +362,27 @@ void McuManagerImpl::McuAskSendHeartBeatHandle(std::shared_ptr<VMcuRecv> &recv)
{
std::shared_ptr<VMcuMonitor> monitor = GetMcuMonitor();
if (monitor) {
monitor->RecvMcuHeartBeat(recv);
monitor->RecvMcuHeartBeatEvent(recv);
}
}
void McuManagerImpl::McuAskGetIntervalStartHandle(std::shared_ptr<VMcuRecv> &recv)
{
std::shared_ptr<VMcuMonitor> monitor = GetMcuMonitor();
if (monitor) {
monitor->RecvGetIntervalStartEvent(recv);
}
}
void McuManagerImpl::McuAskGetDateTimeHandle(std::shared_ptr<VMcuRecv> &recv)
{
std::shared_ptr<VMcuMonitor> monitor = GetMcuMonitor();
if (monitor) {
monitor->RecvGetDateTime(recv);
}
}
void McuManagerImpl::McuAskGetPirSensitivityHandle(std::shared_ptr<VMcuRecv> &recv)
{
std::shared_ptr<VMcuMonitor> monitor = GetMcuMonitor();
if (monitor) {
monitor->RecvGetPirSensitivity(recv);
}
}

View File

@ -29,6 +29,7 @@ enum class OtherSideSendType
SEND_HEART_BEAT,
GET_INTERVAL_START,
GET_DATE_TIME,
GET_PIR_SENSITIVITY,
END
};
class McuManagerImpl : public McuDevice, public McuProtocol, public std::enable_shared_from_this<McuManagerImpl>
@ -76,6 +77,9 @@ private: // About mMcuAskList
void AddMcuRecv(std::shared_ptr<VMcuRecv> &recv);
void McuAskSendIpcMissionHandle(std::shared_ptr<VMcuRecv> &recv);
void McuAskSendHeartBeatHandle(std::shared_ptr<VMcuRecv> &recv);
void McuAskGetIntervalStartHandle(std::shared_ptr<VMcuRecv> &recv);
void McuAskGetDateTimeHandle(std::shared_ptr<VMcuRecv> &recv);
void McuAskGetPirSensitivityHandle(std::shared_ptr<VMcuRecv> &recv);
private:
std::mutex mMutex;

View File

@ -19,7 +19,7 @@ link_directories(
)
aux_source_directory(. SRC_FILES)
aux_source_directory(${TEST_SOURCE_PATH}/middleware/IpcConfig/src SRC_FILES)
# aux_source_directory(${TEST_SOURCE_PATH}/middleware/IpcConfig/src SRC_FILES)
set(TARGET_NAME AllTest)
add_executable(${TARGET_NAME} ${SRC_FILES})
target_link_libraries(${TARGET_NAME} IpcConfig gtest gmock pthread)

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

@ -1,7 +1,7 @@
# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake)
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
# include(${APPLICATION_SOURCE_PATH}/VersionRelease/build/hunting_camera.cmake)
set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR_IPCSDK}/tools/version_release)
include_directories(
./src
@ -35,6 +35,8 @@ if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX})
aux_source_directory(./src_mock SRC_FILES)
endif()
add_definitions(-DHUNTING_UPGRADE_BUILD_FILE="../ipc-sdk/middleware/HuntingUpgrade/build/hunting_upgrade.cmake")
set(TARGET_NAME VersionReleaseTool)
add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES})
target_link_libraries(${TARGET_NAME} UpgradeTool StatusCode Log gtest gmock pthread)

View File

@ -24,8 +24,8 @@ int main(int argc, char *argv[])
ILogInit(LOG_INSTANCE_TYPE_END);
ArgvAnalysis::GetInstance()->Analyze(argc, argv);
testing::InitGoogleTest(&argc, argv);
RUN_ALL_TESTS();
int result = RUN_ALL_TESTS();
ILogUnInit();
DestroyLogModule();
return 0;
return result;
}

View File

@ -14,6 +14,8 @@
*/
#include "ArgvAnalysis.h"
#include "ILog.h"
const char *V_SOURCE_FILE = "v_source_file";
const char *V_OUTPUT_FILE = "v_output_file";
std::shared_ptr<ArgvAnalysis> &ArgvAnalysis::GetInstance(std::shared_ptr<ArgvAnalysis> *impl)
{
static auto instance = std::make_shared<ArgvAnalysis>();
@ -56,3 +58,21 @@ void ArgvAnalysis::Analyze(int argc, char *argv[])
LogInfo("Key: %s, Value: %s\n", pair.first.c_str(), pair.second.c_str());
}
}
std::string ArgvAnalysis::GetSourceFile(void)
{
auto it = mOptions.find(V_SOURCE_FILE);
if (it != mOptions.end()) {
return it->second;
}
LogWarning("Can't find the source file.\n");
return "";
}
std::string ArgvAnalysis::GetOutputFile(void)
{
auto it = mOptions.find(V_OUTPUT_FILE);
if (it != mOptions.end()) {
return it->second;
}
LogWarning("Can't find the source file.\n");
return "";
}

View File

@ -24,6 +24,8 @@ public:
virtual ~ArgvAnalysis() = default;
static std::shared_ptr<ArgvAnalysis> &GetInstance(std::shared_ptr<ArgvAnalysis> *impl = nullptr);
void Analyze(int argc, char *argv[]);
std::string GetSourceFile(void);
std::string GetOutputFile(void);
private:
std::map<std::string, std::string> mOptions;

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

@ -12,15 +12,64 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "VersionReleaseTool.h"
#include "ArgvAnalysis.h"
#include "GtestUsing.h"
#include "ILog.h"
#include "UpgradeTool.h"
#include "VersionReleaseTool.h"
#include <thread>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <regex>
namespace VersionReleaseTool
{
/**
* @brief Construct a new TEST object
* ../output_files/test/bin/VersionReleaseTool --v_source_file=./test.bin --v_output_file=./test_output.bin
*/
static void GetVersion(std::string &versiond)
{
const std::string filename = HUNTING_UPGRADE_BUILD_FILE;
std::ifstream file(filename);
if (!file.is_open()) {
std::cerr << "无法打开文件: " << filename << std::endl;
return ;
}
std::string line;
std::string version;
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]将包含捕获的版本号
version = match[1].str();
break; // 找到后退出循环
}
}
file.close();
if (version.empty()) {
std::cerr << "在文件中未找到指定的行或版本号!" << std::endl;
return ;
}
std::cout << "提取的版本号: " << version << std::endl;
return ;
}
TEST(VersionReleaseTool, Version)
{
UpgradeTool::GetInstance()->PackFile("src", "output", "1.0.0", "product", "project", "upgradeType");
std::string sourceFile = ArgvAnalysis::GetInstance()->GetSourceFile();
std::string outputFile = ArgvAnalysis::GetInstance()->GetOutputFile();
std::string version;
GetVersion(version);
UpgradeTool::GetInstance()->PackFile(sourceFile, outputFile, "1.0.0", "product", "project", "upgradeType");
}
} // namespace VersionReleaseTool

View File

@ -38,6 +38,7 @@ public:
static void TearDownTestCase()
{
ILogUnInit();
DestroyLogModule();
}
virtual void SetUp()
{

View File

@ -6,3 +6,4 @@ add_subdirectory(McuAskBase)
add_subdirectory(DeviceManager)
add_subdirectory(AppManager)
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,
"6.4.5.6",
"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

@ -1,5 +1,6 @@
# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake)
include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake)
include(${MIDDLEWARE_SOURCE_PATH}/IpcConfig/build/ipc_config.cmake)
set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin)
include_directories(
@ -8,7 +9,10 @@ include_directories(
./include
${UTILS_SOURCE_PATH}/Log/include
${UTILS_SOURCE_PATH}/StatusCode/include
${UTILS_SOURCE_PATH}/LinuxApi/include
${UTILS_SOURCE_PATH}/ConfigBase/include
${MIDDLEWARE_SOURCE_PATH}/IpcConfig/include
${MIDDLEWARE_SOURCE_PATH}/IpcConfig/src
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include
${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include
)
@ -23,7 +27,7 @@ aux_source_directory(./src SRC_FILES)
set(TARGET_NAME IpcConfigTest)
add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES})
target_link_libraries(${TARGET_NAME} IpcConfig gtest gmock pthread)
target_link_libraries(${TARGET_NAME} IpcConfig LinuxApi gtest gmock pthread)
if(${TEST_COVERAGE} MATCHES "true")
target_link_libraries(${TARGET_NAME} gcov)
endif()
@ -64,4 +68,5 @@ add_custom_command(
WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/
)
endif()
define_file_name(${TARGET_NAME})

View File

@ -1,81 +1,85 @@
#include "IIpcConfig.h"
#include "ILog.h"
#include "IpcConfigImpl.h"
#include "LinuxApi.h"
// #include <gmock/gmock.h>
#include <gtest/gtest.h>
extern const char *CONFIG_WIFI_SSID_DEFAULT;
extern const char *CONFIG_WIFI_PASSWORD_DEFAULT;
namespace IpcConfigTest
{
// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.Demo
TEST(IpcConfigTest, Demo)
// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigDemo.Demo
TEST(IpcConfigDemo, Demo)
{
CreateLogModule();
CreateIpcConfig();
CreateIpcConfigModule();
ILogInit(LOG_INSTANCE_TYPE_END);
IIpcConfig::GetInstance()->Init();
const int workMode = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::KEY_WORK_MODE);
const int workMode = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::WORK_MODE);
LogInfo("Get workMode = %d\n", workMode);
const int wworkMode = WORK_MODE_PIC_VIDEO;
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::KEY_WORK_MODE, wworkMode);
const int wworkMode = static_cast<int>(WorkMode::MODE_PIC_VIDEO);
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::WORK_MODE, wworkMode);
const int continuousShot = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::KEY_CONTINUOUS_SHOT);
const int continuousShot = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::CONTINUOUS_SHOT);
LogInfo("Get continuousShot = %d\n", continuousShot);
const int ccontinuousShot = CONTINUOUS_SHOT_TWO_PIC;
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::KEY_CONTINUOUS_SHOT, ccontinuousShot);
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::CONTINUOUS_SHOT, ccontinuousShot);
const int burstPhotoInterval = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::KEY_BURST_PHOTO_INTERVAL);
const int burstPhotoInterval = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::BURST_PHOTO_INTERVAL);
LogInfo("Get burstPhotoInterval = %d\n", burstPhotoInterval);
const int bburstPhotoInterval = BURST_PHOTO_INTERVAL_DEFAULT;
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::KEY_BURST_PHOTO_INTERVAL, bburstPhotoInterval);
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::BURST_PHOTO_INTERVAL, bburstPhotoInterval);
const std::string imageSize = IIpcConfig::GetInstance()->GetString(IpcConfigKey::KEY_IMGAE_SIZE);
const std::string imageSize = IIpcConfig::GetInstance()->GetString(IpcConfigKey::IMGAE_SIZE);
LogInfo("Get image_size = %s\n", imageSize.c_str());
const std::string iimageSize = "410*410";
IIpcConfig::GetInstance()->SetString(IpcConfigKey::KEY_IMGAE_SIZE, iimageSize);
IIpcConfig::GetInstance()->SetString(IpcConfigKey::IMGAE_SIZE, iimageSize);
const int videoSize = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::KEY_VIDEO_SIZE);
const int videoSize = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::VIDEO_LENGTH);
LogInfo("Get video_size = %d\n", videoSize);
const int vvideoSize = VIDEO_SIZE_10;
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::KEY_VIDEO_SIZE, vvideoSize);
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::VIDEO_LENGTH, vvideoSize);
const int infraredIampPower = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::KEY_INFRARED_LAMP_POWER);
const int infraredIampPower = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::INFRARED_POWER);
LogInfo("Get infrared_lamp_power = %d\n", infraredIampPower);
const int iinfraredIampPower = INFRARED_IAMP_POWER_LOW;
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::KEY_INFRARED_LAMP_POWER, iinfraredIampPower);
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::INFRARED_POWER, iinfraredIampPower);
const int delayed = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::KEY_DELAYED);
const int delayed = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::PIR_DELAYED);
LogInfo("Get delayed = %d\n", delayed);
const int ddelayed = DELAYED_MAX;
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::KEY_DELAYED, ddelayed);
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::PIR_DELAYED, ddelayed);
const int pirSensitivity = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::KEY_PIR_SENSITIVITY);
const int pirSensitivity = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::PIR_SENSITIVITY);
LogInfo("Get pir_sensitivity = %d\n", pirSensitivity);
const int ppirSensitivity = PIR_SENSITIVITY_MAX;
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::KEY_PIR_SENSITIVITY, ppirSensitivity);
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::PIR_SENSITIVITY, ppirSensitivity);
const bool storageLoopSwitch = IIpcConfig::GetInstance()->GetBool(IpcConfigKey::KEY_STORAGE_LOOP_SWITCH);
const bool storageLoopSwitch = IIpcConfig::GetInstance()->GetBool(IpcConfigKey::STORAGE_LOOP_SWITCH);
LogInfo("Get storage_loop_switch = %d\n", storageLoopSwitch);
const bool sstorageLoopSwitch = false;
IIpcConfig::GetInstance()->SetBool(IpcConfigKey::KEY_STORAGE_LOOP_SWITCH, sstorageLoopSwitch);
IIpcConfig::GetInstance()->SetBool(IpcConfigKey::STORAGE_LOOP_SWITCH, sstorageLoopSwitch);
const bool factoryReset = IIpcConfig::GetInstance()->GetBool(IpcConfigKey::KEY_FACTORY_RESET_FLAG);
const bool factoryReset = IIpcConfig::GetInstance()->GetBool(IpcConfigKey::FACTORY_RESET_FLAG);
LogInfo("Get factory_reset = %d\n", factoryReset);
const bool ffactoryReset = false;
IIpcConfig::GetInstance()->SetBool(IpcConfigKey::KEY_FACTORY_RESET_FLAG, ffactoryReset);
IIpcConfig::GetInstance()->SetBool(IpcConfigKey::FACTORY_RESET_FLAG, ffactoryReset);
const bool formattingSDCard = IIpcConfig::GetInstance()->GetBool(IpcConfigKey::KEY_FORMATTING_SD_CARD);
const bool formattingSDCard = IIpcConfig::GetInstance()->GetBool(IpcConfigKey::FORMATTING_SD_CARD);
LogInfo("Get formatting_SD_card = %d\n", formattingSDCard);
const bool fformattingSDCard = false;
IIpcConfig::GetInstance()->SetBool(IpcConfigKey::KEY_FORMATTING_SD_CARD, fformattingSDCard);
IIpcConfig::GetInstance()->SetBool(IpcConfigKey::FORMATTING_SD_CARD, fformattingSDCard);
const std::string workInterval = IIpcConfig::GetInstance()->GetString(IpcConfigKey::KEY_WORK_INTERVAL);
const std::string workInterval = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WORK_INTERVAL);
LogInfo("Get work_Interval = %s\n", workInterval.c_str());
const std::string wworkInterval = "06:00:01-06:00:00";
IIpcConfig::GetInstance()->SetString(IpcConfigKey::KEY_WORK_INTERVAL, wworkInterval);
IIpcConfig::GetInstance()->SetString(IpcConfigKey::WORK_INTERVAL, wworkInterval);
const std::string drakMode = IIpcConfig::GetInstance()->GetString(IpcConfigKey::KEY_DARK_MODE);
const std::string drakMode = IIpcConfig::GetInstance()->GetString(IpcConfigKey::DARK_MODE);
LogInfo("Get dark_mode = %s\n", drakMode.c_str());
const std::string ddrakMode = "18:00:00-06:00:00";
IIpcConfig::GetInstance()->SetString(IpcConfigKey::KEY_DARK_MODE, ddrakMode);
IIpcConfig::GetInstance()->SetString(IpcConfigKey::DARK_MODE, ddrakMode);
short testShort = IIpcConfig::GetInstance()->GetShort(IpcConfigKey::TEST_SHORT);
LogInfo("Get test_short = %d\n", testShort);
@ -107,4 +111,185 @@ TEST(IpcConfigTest, Demo)
ILogUnInit();
DestroyLogModule();
}
class IpcConfigTest : public testing::Test
{
public:
IpcConfigTest()
{
}
virtual ~IpcConfigTest()
{
}
static void SetUpTestCase()
{
ILogInit(LOG_INSTANCE_TYPE_END);
CreateLogModule();
}
static void TearDownTestCase()
{
ILogUnInit();
DestroyLogModule();
}
virtual void SetUp()
{
CreateIpcConfigModule();
IIpcConfig::GetInstance()->Init();
}
virtual void TearDown()
{
IIpcConfig::GetInstance()->UnInit();
DestroyIpcConfigModule();
RemoveConfigFile();
}
private:
void RemoveConfigFile(void)
{
constexpr int FIEL_EXIST = 0;
if (FIEL_EXIST == access(IPC_CONFIG_FILE_PATH, F_OK)) {
constexpr int BUFF_SIZE = 128;
char cmd[BUFF_SIZE] = {0};
snprintf(cmd, BUFF_SIZE, "rm -f %s", IPC_CONFIG_FILE_PATH);
fx_system(cmd);
}
}
};
// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_WifiSsid
TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_WifiSsid)
{
const char *MODIFIED_STRING = "modified_string";
const char *MODIFIED_STRING_SHORTER = "modified";
const char *LONG_STRING =
"LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL"
"LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL";
std::string config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_SSID);
LogInfo("Get wifi ssid = %s\n", config.c_str());
EXPECT_STREQ(config.c_str(), CONFIG_WIFI_SSID_DEFAULT);
IIpcConfig::GetInstance()->SetString(IpcConfigKey::WIFI_SSID, MODIFIED_STRING);
config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_SSID);
LogInfo("Get wifiSsid = %s\n", config.c_str());
EXPECT_STREQ(config.c_str(), MODIFIED_STRING);
IIpcConfig::GetInstance()->SetString(IpcConfigKey::WIFI_SSID, LONG_STRING);
config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_SSID);
EXPECT_STREQ(config.c_str(), MODIFIED_STRING);
IIpcConfig::GetInstance()->SetString(IpcConfigKey::WIFI_SSID, "");
config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_SSID);
EXPECT_STREQ(config.c_str(), "");
IIpcConfig::GetInstance()->SetString(IpcConfigKey::WIFI_SSID, MODIFIED_STRING_SHORTER);
config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_SSID);
EXPECT_STREQ(config.c_str(), MODIFIED_STRING_SHORTER);
IIpcConfig::GetInstance()->SetString(IpcConfigKey::WIFI_SSID, MODIFIED_STRING);
config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_SSID);
EXPECT_STREQ(config.c_str(), MODIFIED_STRING);
}
// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_WifiPassword
TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_WifiPassword)
{
const char *MODIFIED_STRING = "99999999";
const char *MODIFIED_STRING_SHORTER = "999";
const char *LONG_STRING =
"LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL"
"LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL";
std::string config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_PASSWORD);
LogInfo("Get wifi password = %s\n", config.c_str());
EXPECT_STREQ(config.c_str(), CONFIG_WIFI_PASSWORD_DEFAULT);
IIpcConfig::GetInstance()->SetString(IpcConfigKey::WIFI_PASSWORD, MODIFIED_STRING);
config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_PASSWORD);
LogInfo("Get wifi password = %s\n", config.c_str());
EXPECT_STREQ(config.c_str(), MODIFIED_STRING);
IIpcConfig::GetInstance()->SetString(IpcConfigKey::WIFI_PASSWORD, LONG_STRING);
config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_PASSWORD);
EXPECT_STREQ(config.c_str(), MODIFIED_STRING);
IIpcConfig::GetInstance()->SetString(IpcConfigKey::WIFI_PASSWORD, "");
config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_PASSWORD);
EXPECT_STREQ(config.c_str(), "");
IIpcConfig::GetInstance()->SetString(IpcConfigKey::WIFI_PASSWORD, MODIFIED_STRING_SHORTER);
config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_PASSWORD);
EXPECT_STREQ(config.c_str(), MODIFIED_STRING_SHORTER);
IIpcConfig::GetInstance()->SetString(IpcConfigKey::WIFI_PASSWORD, MODIFIED_STRING);
config = IIpcConfig::GetInstance()->GetString(IpcConfigKey::WIFI_PASSWORD);
EXPECT_STREQ(config.c_str(), MODIFIED_STRING);
}
// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_WorkMode
TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_WorkMode)
{
WorkMode config = IIpcConfig::GetInstance()->GetWorkMode();
EXPECT_EQ(static_cast<int>(config), static_cast<int>(CONFIG_WORK_MODE_DEFAULT));
IIpcConfig::GetInstance()->SetWorkMode(WorkMode::MODE_PIC_VIDEO);
config = IIpcConfig::GetInstance()->GetWorkMode();
EXPECT_EQ(static_cast<int>(config), static_cast<int>(WorkMode::MODE_PIC_VIDEO));
}
// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_ContinueShot
TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_ContinueShot)
{
int config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::CONTINUOUS_SHOT);
EXPECT_EQ(config, CONFIG_CONTINUE_SHOT_DEFAULT);
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::CONTINUOUS_SHOT, 2);
config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::CONTINUOUS_SHOT);
EXPECT_EQ(config, 2);
}
// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_BurstPhotoInterval
TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_BurstPhotoInterval)
{
int config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::BURST_PHOTO_INTERVAL);
EXPECT_EQ(config, CONFIG_BURST_PHOTO_INTERVAL_DEFAULT);
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::BURST_PHOTO_INTERVAL, 10);
config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::BURST_PHOTO_INTERVAL);
EXPECT_EQ(config, 10);
}
// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_ImageSize
TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_ImageSize)
{
int config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::IMGAE_SIZE);
EXPECT_EQ(config, CONFIG_IMAGE_SIZE_DEFAULT);
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::IMGAE_SIZE, 16);
config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::IMGAE_SIZE);
EXPECT_EQ(config, 16);
}
// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_VideoLength
TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_VideoLength)
{
int config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::VIDEO_LENGTH);
EXPECT_EQ(config, CONFIG_VIDEO_SIZE_DEFAULT);
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::VIDEO_LENGTH, 15);
config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::VIDEO_LENGTH);
EXPECT_EQ(config, 15);
}
// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_PirDelayed
TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_PirDelayed)
{
int config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::PIR_DELAYED);
EXPECT_EQ(config, CONFIG_PIR_DELAYED_DEFAULT);
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::PIR_DELAYED, 15);
config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::PIR_DELAYED);
EXPECT_EQ(config, 15);
}
// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_StorageLoop
TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_StorageLoop)
{
ConfigSwitch config = IIpcConfig::GetInstance()->GetSwitch(IpcConfigKey::STORAGE_LOOP_SWITCH);
EXPECT_EQ(static_cast<int>(config),
static_cast<int>(CONFIG_STORAGE_LOOP_DEFAULT == true ? ConfigSwitch::ON : ConfigSwitch::OFF));
IIpcConfig::GetInstance()->SetSwitch(IpcConfigKey::STORAGE_LOOP_SWITCH, ConfigSwitch::OFF);
config = IIpcConfig::GetInstance()->GetSwitch(IpcConfigKey::STORAGE_LOOP_SWITCH);
EXPECT_EQ(static_cast<int>(config), static_cast<int>(ConfigSwitch::OFF));
}
// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_InfraredPower
TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_InfraredPower)
{
ConfigLevel config = IIpcConfig::GetInstance()->GetLevel(IpcConfigKey::INFRARED_POWER);
EXPECT_EQ(static_cast<int>(config), static_cast<int>(CONFIG_INFRARED_POWER_DEFAULT));
IIpcConfig::GetInstance()->SetLevel(IpcConfigKey::INFRARED_POWER, ConfigLevel::HIGHT);
config = IIpcConfig::GetInstance()->GetLevel(IpcConfigKey::INFRARED_POWER);
EXPECT_EQ(static_cast<int>(config), static_cast<int>(ConfigSwitch::OFF));
}
// ../output_files/test/bin/IpcConfigTest --gtest_filter=IpcConfigTest.HS_RH_UNIT_IpcConfig_AUTO_PirSensitivity
TEST_F(IpcConfigTest, HS_RH_UNIT_IpcConfig_AUTO_PirSensitivity)
{
int config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::PIR_SENSITIVITY);
EXPECT_EQ(config, CONFIG_PIR_SENSITIVITY_DEFAULT);
IIpcConfig::GetInstance()->SetInt(IpcConfigKey::PIR_SENSITIVITY, 0);
config = IIpcConfig::GetInstance()->GetInt(IpcConfigKey::PIR_SENSITIVITY);
EXPECT_EQ(config, 0);
}
} // namespace IpcConfigTest

View File

@ -188,9 +188,9 @@ TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_EXAMPLE_OtherSideSendHeartB
public:
MonitorTest() = default;
virtual ~MonitorTest() = default;
void RecvMcuHeartBeat(std::shared_ptr<VMcuRecv> &recv) override
void RecvMcuHeartBeatEvent(std::shared_ptr<VMcuRecv> &recv) override
{
LogInfo("RecvMcuHeartBeat\n");
LogInfo("RecvMcuHeartBeatEvent\n");
// std::shared_ptr<McuAsk<ASK_RESULT>> ask = std::dynamic_pointer_cast<McuAsk<ASK_RESULT>>(recv);
// ask->mDataReply = ASK_RESULT::SUCCEED;
recv->ReplyFinished(true);
@ -578,7 +578,6 @@ TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_SetFeedingCycleForWatc
McuAskBaseTestTool::ReplyFinished(result);
if (result) {
LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast<int>(mDataReply));
// Do something here.
}
else {
LogError("Ask data falied.\n");
@ -647,7 +646,6 @@ TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_STRESS_MultiThreadWrit
McuAskBaseTestTool::ReplyFinished(result);
if (result) {
LogInfo("Ask ASK_RESULT succeed, mDataReply = %d.\n", static_cast<int>(mDataReply));
// Do something here.
}
else {
LogError("Ask data falied.\n");
@ -666,7 +664,6 @@ TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_STRESS_MultiThreadWrit
McuAskBaseTestTool::ReplyFinished(result);
if (result) {
LogInfo("Ask IpcMission succeed, mDataReply = %d.\n", static_cast<int>(mDataReply));
// Do something here.
}
else {
LogError("Ask data falied.\n");
@ -679,7 +676,7 @@ TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_STRESS_MultiThreadWrit
testTool->McuAskDefaultFeatures(testTool);
StatusCode code = IMcuManager::GetInstance()->SetFeedingCycleForWatchDog(ask, 1, 1, 1);
EXPECT_EQ(code.mStatusCode, STATUS_CODE_OK); // STATUS_CODE_OK means write data to mcu succeed.
std::this_thread::sleep_for(std::chrono::milliseconds(3000));
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
EXPECT_EQ(test->CheckAskExist(ask), false); // Ensure that the request has been processed and deleted.
};
auto threadTest2 = [](McuManagerMockTest *test) {
@ -687,7 +684,7 @@ TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_STRESS_MultiThreadWrit
std::shared_ptr<McuAskBaseTestTool> testTool = std::dynamic_pointer_cast<McuAskBaseTestTool>(ask);
testTool->McuAskDefaultFeatures(testTool);
IMcuManager::GetInstance()->GetIpcMission(ask);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
EXPECT_EQ(test->CheckAskExist(ask), false); // Ensure that the request has been processed and deleted.
};
IMcuManager::GetInstance()->Init();
@ -754,7 +751,6 @@ TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_SetDataTime2)
McuAskBaseTestTool::ReplyFinished(result);
if (result) {
LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast<int>(mDataReply));
// Do something here.
}
else {
LogError("Ask data falied.\n");
@ -857,7 +853,6 @@ TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_GetIpcMissionFailed)
McuAskBaseTestTool::ReplyFinished(result);
if (result) {
LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast<int>(mDataReply));
// Do something here.
}
else {
LogError("Ask data falied.\n");
@ -899,4 +894,250 @@ TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_OtherSideSendIpcMissio
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IMcuManager::GetInstance()->UnInit();
}
/**
* @brief Construct a new test f object
* This test case simulates the state machine receiving serial port data before it is fully started. At this point, the
* serial port data should be cached until the state machine starts and registers the serial port monitor, and reports
* the serial port data when registering the serial port monitor.
* Run: ../output_files/test/bin/McuManagerTest
* --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_OtherSideSendIpcMission2
*/
TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_OtherSideSendIpcMission2)
{
constexpr unsigned int TEST_SERIAL_NUMBER = 99;
class MonitorTest : public VMcuMonitor
{
public:
MonitorTest() = default;
virtual ~MonitorTest() = default;
void RecvIpcMissionEvent(std::shared_ptr<VMcuRecv> &recv, const IpcMission &mission) override
{
LogInfo("RecvIpcMissionEvent\n");
std::shared_ptr<McuRecv<unsigned char>> recvData = std::dynamic_pointer_cast<McuRecv<unsigned char>>(recv);
recv->ReplyFinished(true);
}
};
IMcuManager::GetInstance()->Init();
MockOtherSideAskIpcMission(mLinuxTest, TEST_SERIAL_NUMBER);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
std::shared_ptr<VMcuMonitor> monitor = std::make_shared<MonitorTest>();
IMcuManager::GetInstance()->SetMcuMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IMcuManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/McuManagerTest
// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_OtherSideGetIntervalStart
TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_OtherSideGetIntervalStart)
{
constexpr unsigned int TEST_SERIAL_NUMBER = 99;
class MonitorTest : public VMcuMonitor
{
public:
MonitorTest() = default;
virtual ~MonitorTest() = default;
void RecvGetIntervalStartEvent(std::shared_ptr<VMcuRecv> &recv) override
{
LogInfo("RecvGetIntervalStartEvent\n");
std::shared_ptr<McuRecv<McuGetIntervalStart>> recvData =
std::dynamic_pointer_cast<McuRecv<McuGetIntervalStart>>(recv);
EXPECT_NE(recvData, nullptr) << "recvData is not McuGetIntervalStart.";
if (recvData) {
recvData->mDataRecvReply.mHour = 10;
recvData->mDataRecvReply.mMin = 10;
recvData->mDataRecvReply.mSecond = 10;
recv->ReplyFinished(true);
}
recv->ReplyFinished(false);
}
};
IMcuManager::GetInstance()->Init();
MockOtherSideGetIntervalStart(mLinuxTest, TEST_SERIAL_NUMBER);
std::shared_ptr<VMcuMonitor> monitor = std::make_shared<MonitorTest>();
IMcuManager::GetInstance()->SetMcuMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IMcuManager::GetInstance()->UnInit();
}
/**
* @brief Construct a new test f object
* This test case simulates the state machine receiving serial port data before it is fully started. At this point, the
* serial port data should be cached until the state machine starts and registers the serial port monitor, and reports
* the serial port data when registering the serial port monitor.
* Run: ../output_files/test/bin/McuManagerTest
* --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_OtherSideGetIntervalStart2
*/
TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_OtherSideGetIntervalStart2)
{
constexpr unsigned int TEST_SERIAL_NUMBER = 99;
class MonitorTest : public VMcuMonitor
{
public:
MonitorTest() = default;
virtual ~MonitorTest() = default;
void RecvGetIntervalStartEvent(std::shared_ptr<VMcuRecv> &recv) override
{
LogInfo("RecvGetIntervalStartEvent\n");
std::shared_ptr<McuRecv<McuGetIntervalStart>> recvData =
std::dynamic_pointer_cast<McuRecv<McuGetIntervalStart>>(recv);
EXPECT_NE(recvData, nullptr) << "recvData is not McuGetIntervalStart.";
if (recvData) {
recvData->mDataRecvReply.mHour = 10;
recvData->mDataRecvReply.mMin = 10;
recvData->mDataRecvReply.mSecond = 10;
recv->ReplyFinished(true);
}
recv->ReplyFinished(false);
}
};
IMcuManager::GetInstance()->Init();
MockOtherSideGetIntervalStart(mLinuxTest, TEST_SERIAL_NUMBER);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
std::shared_ptr<VMcuMonitor> monitor = std::make_shared<MonitorTest>();
IMcuManager::GetInstance()->SetMcuMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IMcuManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/McuManagerTest
// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_OtherSideGetDateTime
TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_OtherSideGetDateTime)
{
constexpr unsigned int TEST_SERIAL_NUMBER = 99;
class MonitorTest : public VMcuMonitor
{
public:
MonitorTest() = default;
virtual ~MonitorTest() = default;
void RecvGetDateTime(std::shared_ptr<VMcuRecv> &recv) override
{
LogInfo("RecvGetDateTime\n");
std::shared_ptr<McuRecv<McuReplyDateTime>> recvData =
std::dynamic_pointer_cast<McuRecv<McuReplyDateTime>>(recv);
EXPECT_NE(recvData, nullptr) << "recvData is not McuReplyDateTime.";
if (recvData) {
recvData->mDataRecvReply.mYear = 2024;
recvData->mDataRecvReply.mMon = 10;
recvData->mDataRecvReply.mDay = 10;
recvData->mDataRecvReply.mHour = 10;
recvData->mDataRecvReply.mMin = 10;
recvData->mDataRecvReply.mSecond = 10;
recv->ReplyFinished(true);
}
recv->ReplyFinished(false);
}
};
IMcuManager::GetInstance()->Init();
MockOtherSideGetDateTime(mLinuxTest, TEST_SERIAL_NUMBER);
std::shared_ptr<VMcuMonitor> monitor = std::make_shared<MonitorTest>();
IMcuManager::GetInstance()->SetMcuMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IMcuManager::GetInstance()->UnInit();
}
/**
* @brief Construct a new test f object
* This test case simulates the state machine receiving serial port data before it is fully started. At this point, the
* serial port data should be cached until the state machine starts and registers the serial port monitor, and reports
* the serial port data when registering the serial port monitor.
* Run: ../output_files/test/bin/McuManagerTest
* --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_OtherSideGetDateTime2
*/
TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_OtherSideGetDateTime2)
{
constexpr unsigned int TEST_SERIAL_NUMBER = 99;
class MonitorTest : public VMcuMonitor
{
public:
MonitorTest() = default;
virtual ~MonitorTest() = default;
void RecvGetDateTime(std::shared_ptr<VMcuRecv> &recv) override
{
LogInfo("RecvGetDateTime\n");
std::shared_ptr<McuRecv<McuReplyDateTime>> recvData =
std::dynamic_pointer_cast<McuRecv<McuReplyDateTime>>(recv);
EXPECT_NE(recvData, nullptr) << "recvData is not McuReplyDateTime.";
if (recvData) {
recvData->mDataRecvReply.mYear = 2024;
recvData->mDataRecvReply.mMon = 10;
recvData->mDataRecvReply.mDay = 10;
recvData->mDataRecvReply.mHour = 10;
recvData->mDataRecvReply.mMin = 10;
recvData->mDataRecvReply.mSecond = 10;
recv->ReplyFinished(true);
}
recv->ReplyFinished(false);
}
};
IMcuManager::GetInstance()->Init();
MockOtherSideGetDateTime(mLinuxTest, TEST_SERIAL_NUMBER);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
std::shared_ptr<VMcuMonitor> monitor = std::make_shared<MonitorTest>();
IMcuManager::GetInstance()->SetMcuMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IMcuManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/McuManagerTest
// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_OtherSideGetPirSensitivity
TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_OtherSideGetPirSensitivity)
{
constexpr unsigned int TEST_SERIAL_NUMBER = 99;
class MonitorTest : public VMcuMonitor
{
public:
MonitorTest() = default;
virtual ~MonitorTest() = default;
void RecvGetPirSensitivity(std::shared_ptr<VMcuRecv> &recv) override
{
LogInfo("RecvGetPirSensitivity\n");
std::shared_ptr<McuRecv<McuGetPirSensitivity>> recvData =
std::dynamic_pointer_cast<McuRecv<McuGetPirSensitivity>>(recv);
EXPECT_NE(recvData, nullptr) << "recvData is not McuGetPirSensitivity.";
if (recvData) {
recvData->mDataRecvReply.mSensitivity = 9;
recv->ReplyFinished(true);
}
recv->ReplyFinished(false);
}
};
IMcuManager::GetInstance()->Init();
MockOtherSideGetPriSensitivity(mLinuxTest, TEST_SERIAL_NUMBER);
std::shared_ptr<VMcuMonitor> monitor = std::make_shared<MonitorTest>();
IMcuManager::GetInstance()->SetMcuMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IMcuManager::GetInstance()->UnInit();
}
/**
* @brief Construct a new test f object
* This test case simulates the state machine receiving serial port data before it is fully started. At this point, the
* serial port data should be cached until the state machine starts and registers the serial port monitor, and reports
* the serial port data when registering the serial port monitor.
* Run: ../output_files/test/bin/McuManagerTest
* --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_OtherSideGetPirSensitivity2
*/
TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_OtherSideGetPirSensitivity2)
{
constexpr unsigned int TEST_SERIAL_NUMBER = 99;
class MonitorTest : public VMcuMonitor
{
public:
MonitorTest() = default;
virtual ~MonitorTest() = default;
void RecvGetPirSensitivity(std::shared_ptr<VMcuRecv> &recv) override
{
LogInfo("RecvGetPirSensitivity\n");
std::shared_ptr<McuRecv<McuGetPirSensitivity>> recvData =
std::dynamic_pointer_cast<McuRecv<McuGetPirSensitivity>>(recv);
EXPECT_NE(recvData, nullptr) << "recvData is not McuGetPirSensitivity.";
if (recvData) {
recvData->mDataRecvReply.mSensitivity = 9;
recv->ReplyFinished(true);
}
recv->ReplyFinished(false);
}
};
IMcuManager::GetInstance()->Init();
MockOtherSideGetPriSensitivity(mLinuxTest, TEST_SERIAL_NUMBER);
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
std::shared_ptr<VMcuMonitor> monitor = std::make_shared<MonitorTest>();
IMcuManager::GetInstance()->SetMcuMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IMcuManager::GetInstance()->UnInit();
}
} // namespace McuManagerMockTest

View File

@ -36,6 +36,9 @@ public:
bool CheckAskExist(const std::shared_ptr<VMcuAsk> &ask);
void MockOtherSideAskIpcMission(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
void MockOtherSideAskHeartBeat(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
void MockOtherSideGetIntervalStart(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
void MockOtherSideGetDateTime(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
void MockOtherSideGetPriSensitivity(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
void MockMcuDeviceOpenFailed(std::shared_ptr<LinuxTest> &mock);
void MockMcuDeviceOpenSuccessButReadNothing(std::shared_ptr<LinuxTest> &mock);

View File

@ -52,6 +52,20 @@ void McuManagerTestTool::MockOtherSideAskHeartBeat(std::shared_ptr<LinuxTest> &m
{
McuProtocolTestTool::MockOtherSideAskHeartBeat(mock, serialNumber);
}
void McuManagerTestTool::MockOtherSideGetIntervalStart(std::shared_ptr<LinuxTest> &mock,
const unsigned int &serialNumber)
{
McuProtocolTestTool::MockOtherSideAskGetIntervalStart(mock, serialNumber);
}
void McuManagerTestTool::MockOtherSideGetDateTime(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber)
{
McuProtocolTestTool::MockOtherSideAskGetDateTime(mock, serialNumber);
}
void McuManagerTestTool::MockOtherSideGetPriSensitivity(std::shared_ptr<LinuxTest> &mock,
const unsigned int &serialNumber)
{
McuProtocolTestTool::MockOtherSideAskGetPirSensitivity(mock, serialNumber);
}
void McuManagerTestTool::MockMcuDeviceOpenFailed(std::shared_ptr<LinuxTest> &mock)
{
UartDeviceTestTool::SetUartDeviceOpenFailed(mock, gUartDevice);

View File

@ -24,7 +24,7 @@
### 1.1.2. 测试用例命名:
1. 仿真硬件仿真使用hardware simulation的缩写**HS**硬件仿真只能运行在Ubuntu系统进行仿真测试真实硬件接口使用real hardware的缩写**RH**,真实硬件接口测试用例只能运行在开发板进行真机测试;不涉及使用NOT INVOLVED的缩写**NI**即可执行在Ubuntu也可以运行在开发板
1. 仿真硬件仿真使用hardware simulation的缩写**HS**硬件仿真只能运行在Ubuntu系统进行仿真测试真实硬件接口使用real hardware的缩写**RH**,真实硬件接口测试用例只能运行在开发板进行真机测试;
2. 测试用例类型含单元测试UNIT和集成测试INTEGRATION
3. 用例所属模块:大小驼峰;
4. 测试用例属性EXAMPLE/AUTO/STRESS

View File

@ -33,12 +33,22 @@ public:
void UnInit(void);
void MockOtherSideAskIpcMission(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
void MockOtherSideAskHeartBeat(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
void MockOtherSideAskGetIntervalStart(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
void MockOtherSideAskGetDateTime(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
void MockOtherSideAskGetPirSensitivity(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
void ReadNothingAnyTime(std::shared_ptr<LinuxTest> &mock);
private:
void CheckSerialNumber(const void *buf, const size_t &count);
void ChecCRC16Code(const void *buf, const size_t &count);
void ResetCheckCode(const void *buf, const size_t &count);
/**
* @brief The function of initializing the fx_select function can quickly return when selecting without waiting for
* the timeout to end.
* @param mock
* @param uartFd
*/
void SelectInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd);
void ReplySelectSucceed(std::shared_ptr<LinuxTest> &mock, const int &uartFd);
void ReplySelectTimeOut(std::shared_ptr<LinuxTest> &mock, const int &uartFd);
bool MonitorProtocolPacket(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf, size_t count);
@ -75,6 +85,18 @@ private:
const unsigned int &serialNumber);
void OtherSideAskHeartBeatInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const unsigned int &serialNumber);
void OtherSideAskGetIntervalStartHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const unsigned int &serialNumber);
void OtherSideAskGetIntervalStartInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const unsigned int &serialNumber);
void OtherSideAskGetDateTimeHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const unsigned int &serialNumber);
void OtherSideAskGetDateTimeInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const unsigned int &serialNumber);
void OtherSideAskGetPirSensitivityHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const unsigned int &serialNumber);
void OtherSideAskGetPirSensitivityInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const unsigned int &serialNumber);
private:
static void PrintHexadecimalData(const void *buf, const size_t &bufLength, const char *log);

View File

@ -48,7 +48,7 @@ unsigned char ASK_SET_FEEDING_CYCLE_X[] = {
unsigned char REPLY_SET_FEEDING_CYCLE_X[] = {
0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x0D, 0x01, 0xFF, 0xFF};
unsigned char ASK_SET_DATE_TIME_X[] = {
0xFA, 0xC1, 0x00, 0x00, 0x00, 0x01, 0x81, 0x07, 0x00, 0x13, 0xDE, 0x07, 0x01, 0x0F, 0x00, 0x00, 0x00, 0xFF, 0xFF};
0xFA, 0xC1, 0x00, 0x00, 0x00, 0x01, 0x81, 0x07, 0x00, 0x12, 0x20, 0x01, 0x0F, 0x00, 0x00, 0x00, 0xFF, 0xFF};
unsigned char REPLY_SET_DATE_TIME_X[] = {0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x00, 0x0D, 0x01, 0xFF, 0xFF};
unsigned char ASK_SET_PIR_SENSITIVITY_X[] = {
0xFA, 0xC1, 0x00, 0x00, 0x00, 0x01, 0x81, 0x08, 0x00, 0x0D, 0x09, 0xFF, 0xFF};
@ -69,6 +69,18 @@ unsigned char REPLY_OTHER_SIDE_ASK_SEND_HEART_BEAT_X[] = {
0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0xC1, 0x02, 0x00, 0x0C, 0xFF, 0xFF};
unsigned char OTHER_SIDE_ASK_SEND_HEART_BEAT_X[] = {
0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x41, 0x02, 0x00, 0x0C, 0xFF, 0xFF};
unsigned char REPLY_OTHER_SIDE_ASK_GET_INTERVAL_START_X[] = {
0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0xC1, 0x06, 0x00, 0x0F, 0x00, 0x00, 0x00, 0xFF, 0xFF};
unsigned char OTHER_SIDE_ASK_GET_INTERVAL_START_X[] = {
0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x41, 0x06, 0x00, 0x0C, 0xFF, 0xFF};
unsigned char REPLY_OTHER_SIDE_ASK_GET_DATE_TIME_X[] = {
0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0xC1, 0x07, 0x00, 0x13, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF};
unsigned char OTHER_SIDE_ASK_GET_DATE_TIME_X[] = {
0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x41, 0x07, 0x00, 0x0C, 0xFF, 0xFF};
unsigned char REPLY_OTHER_SIDE_ASK_GET_SENSITIVITY_X[] = {
0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0xC1, 0x08, 0x00, 0x0C, 0x00, 0xFF, 0xFF};
unsigned char OTHER_SIDE_ASK_GET_SENSITIVITY_X[] = {
0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x41, 0x08, 0x00, 0x0C, 0xFF, 0xFF};
McuProtocolTestTool::McuProtocolTestTool()
{
mThreadRuning = false;
@ -116,6 +128,7 @@ void McuProtocolTestTool::Init(std::shared_ptr<LinuxTest> &mock, const UartInfo
EXPECT_CALL(*mock.get(), fx_write(uartFd, _, _))
.WillRepeatedly(
DoAll(SaveArg<2>(&WRITE_COUNT), WithArgs<0, 1, 2>(Invoke(api_write)), ReturnPointee(&WRITE_COUNT)));
SelectInit(mock, mUartFd);
}
void McuProtocolTestTool::UnInit(void)
{
@ -145,6 +158,21 @@ void McuProtocolTestTool::MockOtherSideAskHeartBeat(std::shared_ptr<LinuxTest> &
{
OtherSideAskHeartBeatHandle(mock, mUartFd, serialNumber);
}
void McuProtocolTestTool::MockOtherSideAskGetIntervalStart(std::shared_ptr<LinuxTest> &mock,
const unsigned int &serialNumber)
{
OtherSideAskGetIntervalStartHandle(mock, mUartFd, serialNumber);
}
void McuProtocolTestTool::MockOtherSideAskGetDateTime(std::shared_ptr<LinuxTest> &mock,
const unsigned int &serialNumber)
{
OtherSideAskGetDateTimeHandle(mock, mUartFd, serialNumber);
}
void McuProtocolTestTool::MockOtherSideAskGetPirSensitivity(std::shared_ptr<LinuxTest> &mock,
const unsigned int &serialNumber)
{
OtherSideAskGetPirSensitivityHandle(mock, mUartFd, serialNumber);
}
void McuProtocolTestTool::ReadNothingAnyTime(std::shared_ptr<LinuxTest> &mock)
{
static size_t WRITE_COUNT = -1;
@ -179,6 +207,42 @@ void McuProtocolTestTool::ResetCheckCode(const void *buf, const size_t &count)
// checkCode = htons(checkCode);
memcpy((unsigned char *)buf + count - PROTOCOL_CHECK_CODE_LENGTH, &checkCode, PROTOCOL_CHECK_CODE_LENGTH);
}
void McuProtocolTestTool::SelectInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd)
{
auto selectTimeOut =
[=, &mock](int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) {
if (false == mPipeFdMockSelectInit) {
long long timeMs = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
std::this_thread::sleep_for(std::chrono::milliseconds(timeMs));
}
else {
constexpr int READ_BUF_LENGTH = 256;
constexpr int READ_FAILD = -1;
char buf[READ_BUF_LENGTH] = {0};
int selectResult = -1;
fd_set fdsRead;
FD_ZERO(&fdsRead);
FD_SET(mPipeFdMockSelect[PIPE_READ_FD_INDEX], &fdsRead);
selectResult = select(mPipeFdMockSelect[PIPE_READ_FD_INDEX] + 1, &fdsRead, NULL, NULL, timeout);
if (selectResult) {
if (false == mPipeFdMockSelectInit) {
LogWarning("mPipeFdMockSelectInit = false.\n");
return;
}
ssize_t length = read(mPipeFdMockSelect[PIPE_READ_FD_INDEX], buf, READ_BUF_LENGTH);
if (READ_FAILD == length) {
LogError("mPipeFdMockSelect failed.\n");
return;
}
if ((size_t)length != strlen(gPipeBuf)) {
LogWarning("Something wrong happened.\n");
}
}
}
};
EXPECT_CALL(*mock.get(), fx_select(uartFd + 1, _, _, _, _))
.WillRepeatedly(DoAll(WithArgs<0, 1, 2, 3, 4>(Invoke(selectTimeOut)), Return(MOCK_SELECT_TIME_OUT)));
}
void McuProtocolTestTool::ReplySelectSucceed(std::shared_ptr<LinuxTest> &mock, const int &uartFd)
{
auto selectReadable =
@ -202,7 +266,6 @@ void McuProtocolTestTool::ReplySelectSucceed(std::shared_ptr<LinuxTest> &mock, c
FD_SET(mPipeFdMockSelect[PIPE_READ_FD_INDEX], &fdsRead);
selectResult = select(mPipeFdMockSelect[PIPE_READ_FD_INDEX] + 1, &fdsRead, NULL, NULL, timeout);
if (selectResult) {
// Do nothing here.
if (false == mPipeFdMockSelectInit) {
LogWarning("mPipeFdMockSelectInit = false.\n");
return;
@ -287,12 +350,12 @@ void McuProtocolTestTool::IpcMissionProtocolInit(std::shared_ptr<LinuxTest> &moc
size_t count)
{
LockProtocolHandle();
LogInfo("IpcMissionProtocolInit start.\n");
memcpy(REPLY_IPC_MISSION_X + PROTOCOL_SERIAL_NUMBER_OFFSET,
(unsigned char *)buf + PROTOCOL_SERIAL_NUMBER_OFFSET,
PROTOCOL_SERIAL_NUMBER_LENGTH);
free((void *)buf);
ResetCheckCode(REPLY_IPC_MISSION_X, sizeof(REPLY_IPC_MISSION_X));
ReplySelectSucceed(mock, uartFd);
constexpr int LEFT_DATA_LENGTH = sizeof(REPLY_IPC_MISSION_X) - PROTOCOL_DATA_KEY_HEAD_LENGTH;
auto apiReadKeyHead = [=](int fd, void *buf, size_t count) {
memcpy(buf, REPLY_IPC_MISSION_X, PROTOCOL_DATA_KEY_HEAD_LENGTH);
@ -302,11 +365,13 @@ void McuProtocolTestTool::IpcMissionProtocolInit(std::shared_ptr<LinuxTest> &moc
memcpy(buf, REPLY_IPC_MISSION_X + PROTOCOL_DATA_KEY_HEAD_LENGTH, LEFT_DATA_LENGTH);
McuProtocolTestTool::PrintHexadecimalData(buf, LEFT_DATA_LENGTH, "IpcMissionProtocolInit read:");
UnlockProtocolHandle();
LogInfo("IpcMissionProtocolInit finished.\n");
};
EXPECT_CALL(*mock.get(), fx_read(uartFd, _, _))
.WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(apiReadKeyHead)), Return(PROTOCOL_DATA_KEY_HEAD_LENGTH)))
.WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(apiReadLeftData)), Return(LEFT_DATA_LENGTH)))
.WillRepeatedly(DoAll(Return(UART_DEVICE_READ_NOTHING)));
ReplySelectSucceed(mock, uartFd);
}
bool McuProtocolTestTool::CutOffPowerSupplyProtocolHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const void *buf, size_t count)
@ -316,11 +381,9 @@ bool McuProtocolTestTool::CutOffPowerSupplyProtocolHandle(std::shared_ptr<LinuxT
PROTOCOL_COMMAND_LENGTH) == 0) {
LogInfo("Set ASK_CUT_OFF_POWER_SUPPLY_X\n");
short askCheckCode = calculate_check_sum((unsigned char *)buf, count - PROTOCOL_CHECK_CODE_LENGTH);
// askCheckCode = htons(askCheckCode);
int result = memcmp(
(unsigned char *)buf + count - PROTOCOL_CHECK_CODE_LENGTH, &askCheckCode, PROTOCOL_CHECK_CODE_LENGTH);
EXPECT_EQ(result, 0) << "ask protocol data errer, check code isn't right.";
// IpcMissionProtocolInit(mock, uartFd);
return PROTOCOL_HANDLED;
}
return PROTOCOL_NOT_HANDLED;
@ -337,7 +400,6 @@ bool McuProtocolTestTool::FeedWatchDogProtocolHandle(std::shared_ptr<LinuxTest>
int result = memcmp(
(unsigned char *)buf + count - PROTOCOL_CHECK_CODE_LENGTH, &askCheckCode, PROTOCOL_CHECK_CODE_LENGTH);
EXPECT_EQ(result, 0) << "ask protocol data errer, check code isn't right.";
// IpcMissionProtocolInit(mock, uartFd);
return PROTOCOL_HANDLED;
}
return PROTOCOL_NOT_HANDLED;
@ -353,7 +415,6 @@ bool McuProtocolTestTool::FeedingCycleProtocolHandle(std::shared_ptr<LinuxTest>
replyCheckCode = htons(replyCheckCode);
LogInfo("Set ASK_SET_FEEDING_CYCLE_X, reply data check code = 0x%x\n", replyCheckCode);
short askCheckCode = calculate_check_sum((unsigned char *)buf, count - PROTOCOL_CHECK_CODE_LENGTH);
// askCheckCode = htons(askCheckCode);
int result = memcmp(
(unsigned char *)buf + count - PROTOCOL_CHECK_CODE_LENGTH, &askCheckCode, PROTOCOL_CHECK_CODE_LENGTH);
EXPECT_EQ(result, 0) << "ask protocol data errer, check code isn't right.";
@ -370,7 +431,6 @@ bool McuProtocolTestTool::FeedingCycleProtocolHandle(std::shared_ptr<LinuxTest>
mLockThread.join();
}
mLockThread = std::thread(handle, this);
// FeedingCycleProtocolInit(mock, uartFd);
return PROTOCOL_HANDLED;
}
return PROTOCOL_NOT_HANDLED;
@ -379,12 +439,12 @@ void McuProtocolTestTool::FeedingCycleProtocolInit(std::shared_ptr<LinuxTest> &m
size_t count)
{
LockProtocolHandle();
LogInfo("FeedingCycleProtocolInit start.\n");
memcpy(REPLY_SET_FEEDING_CYCLE_X + PROTOCOL_SERIAL_NUMBER_OFFSET,
(unsigned char *)buf + PROTOCOL_SERIAL_NUMBER_OFFSET,
PROTOCOL_SERIAL_NUMBER_LENGTH);
free((void *)buf);
ResetCheckCode(REPLY_SET_FEEDING_CYCLE_X, sizeof(REPLY_SET_FEEDING_CYCLE_X));
ReplySelectSucceed(mock, uartFd);
constexpr int LEFT_DATA_LENGTH = sizeof(REPLY_SET_FEEDING_CYCLE_X) - PROTOCOL_DATA_KEY_HEAD_LENGTH;
auto apiReadKeyHead = [=](int fd, void *buf, size_t count) {
memcpy(buf, REPLY_SET_FEEDING_CYCLE_X, PROTOCOL_DATA_KEY_HEAD_LENGTH);
@ -394,11 +454,13 @@ void McuProtocolTestTool::FeedingCycleProtocolInit(std::shared_ptr<LinuxTest> &m
memcpy(buf, REPLY_SET_FEEDING_CYCLE_X + PROTOCOL_DATA_KEY_HEAD_LENGTH, LEFT_DATA_LENGTH);
McuProtocolTestTool::PrintHexadecimalData(buf, LEFT_DATA_LENGTH, "FeedingCycleProtocolInit read:");
UnlockProtocolHandle();
LogInfo("FeedingCycleProtocolInit finished.\n");
};
EXPECT_CALL(*mock.get(), fx_read(uartFd, _, _))
.WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(apiReadKeyHead)), Return(PROTOCOL_DATA_KEY_HEAD_LENGTH)))
.WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(apiReadLeftData)), Return(LEFT_DATA_LENGTH)))
.WillRepeatedly(DoAll(Return(UART_DEVICE_READ_NOTHING)));
ReplySelectSucceed(mock, uartFd);
}
bool McuProtocolTestTool::SetDataTimeProtocolHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const void *buf, size_t count)
@ -441,7 +503,6 @@ void McuProtocolTestTool::SetDataTimeProtocolInit(std::shared_ptr<LinuxTest> &mo
PROTOCOL_SERIAL_NUMBER_LENGTH);
free((void *)buf);
ResetCheckCode(REPLY_SET_DATE_TIME_X, sizeof(REPLY_SET_DATE_TIME_X));
ReplySelectSucceed(mock, uartFd);
constexpr int LEFT_DATA_LENGTH = sizeof(REPLY_SET_DATE_TIME_X) - PROTOCOL_DATA_KEY_HEAD_LENGTH;
auto apiReadKeyHead = [=](int fd, void *buf, size_t count) {
memcpy(buf, REPLY_SET_DATE_TIME_X, PROTOCOL_DATA_KEY_HEAD_LENGTH);
@ -456,6 +517,7 @@ void McuProtocolTestTool::SetDataTimeProtocolInit(std::shared_ptr<LinuxTest> &mo
.WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(apiReadKeyHead)), Return(PROTOCOL_DATA_KEY_HEAD_LENGTH)))
.WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(apiReadLeftData)), Return(LEFT_DATA_LENGTH)))
.WillRepeatedly(DoAll(Return(UART_DEVICE_READ_NOTHING)));
ReplySelectSucceed(mock, uartFd);
}
bool McuProtocolTestTool::SetPirSensitivityProtocolHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const void *buf, size_t count)
@ -497,7 +559,6 @@ void McuProtocolTestTool::SetPirSensitivityProtocolInit(std::shared_ptr<LinuxTes
(unsigned char *)buf + PROTOCOL_SERIAL_NUMBER_OFFSET,
PROTOCOL_SERIAL_NUMBER_LENGTH);
ResetCheckCode(REPLY_SET_PIR_SENSITIVITY_X, sizeof(REPLY_SET_PIR_SENSITIVITY_X));
ReplySelectSucceed(mock, uartFd);
constexpr int LEFT_DATA_LENGTH = sizeof(REPLY_SET_PIR_SENSITIVITY_X) - PROTOCOL_DATA_KEY_HEAD_LENGTH;
auto apiReadKeyHead = [=](int fd, void *buf, size_t count) {
memcpy(buf, REPLY_SET_PIR_SENSITIVITY_X, PROTOCOL_DATA_KEY_HEAD_LENGTH);
@ -513,6 +574,7 @@ void McuProtocolTestTool::SetPirSensitivityProtocolInit(std::shared_ptr<LinuxTes
.WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(apiReadKeyHead)), Return(PROTOCOL_DATA_KEY_HEAD_LENGTH)))
.WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(apiReadLeftData)), Return(LEFT_DATA_LENGTH)))
.WillRepeatedly(DoAll(Return(UART_DEVICE_READ_NOTHING)));
ReplySelectSucceed(mock, uartFd);
}
bool McuProtocolTestTool::ContorlInfraredLightHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const void *buf, size_t count)
@ -556,7 +618,6 @@ void McuProtocolTestTool::ContorlInfraredLightInit(std::shared_ptr<LinuxTest> &m
PROTOCOL_SERIAL_NUMBER_LENGTH);
free((void *)buf);
ResetCheckCode(REPLY_ASK_CONTORL_INFRARED_LIGHT_X, sizeof(REPLY_ASK_CONTORL_INFRARED_LIGHT_X));
ReplySelectSucceed(mock, uartFd);
constexpr int LEFT_DATA_LENGTH = sizeof(REPLY_ASK_CONTORL_INFRARED_LIGHT_X) - PROTOCOL_DATA_KEY_HEAD_LENGTH;
auto apiReadKeyHead = [=](int fd, void *buf, size_t count) {
memcpy(buf, REPLY_ASK_CONTORL_INFRARED_LIGHT_X, PROTOCOL_DATA_KEY_HEAD_LENGTH);
@ -571,6 +632,7 @@ void McuProtocolTestTool::ContorlInfraredLightInit(std::shared_ptr<LinuxTest> &m
.WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(apiReadKeyHead)), Return(PROTOCOL_DATA_KEY_HEAD_LENGTH)))
.WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(apiReadLeftData)), Return(LEFT_DATA_LENGTH)))
.WillRepeatedly(DoAll(Return(UART_DEVICE_READ_NOTHING)));
ReplySelectSucceed(mock, uartFd);
}
bool McuProtocolTestTool::GetPhotosensitivityValueHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const void *buf, size_t count)
@ -613,7 +675,6 @@ void McuProtocolTestTool::GetPhotosensitivityValueInit(std::shared_ptr<LinuxTest
PROTOCOL_SERIAL_NUMBER_LENGTH);
free((void *)buf);
ResetCheckCode(REPLY_ASK_GET_PHOTOSENSITIVITY_X, sizeof(REPLY_ASK_GET_PHOTOSENSITIVITY_X));
ReplySelectSucceed(mock, uartFd);
constexpr int LEFT_DATA_LENGTH = sizeof(REPLY_ASK_GET_PHOTOSENSITIVITY_X) - PROTOCOL_DATA_KEY_HEAD_LENGTH;
auto apiReadKeyHead = [=](int fd, void *buf, size_t count) {
memcpy(buf, REPLY_ASK_GET_PHOTOSENSITIVITY_X, PROTOCOL_DATA_KEY_HEAD_LENGTH);
@ -629,6 +690,7 @@ void McuProtocolTestTool::GetPhotosensitivityValueInit(std::shared_ptr<LinuxTest
.WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(apiReadKeyHead)), Return(PROTOCOL_DATA_KEY_HEAD_LENGTH)))
.WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(apiReadLeftData)), Return(LEFT_DATA_LENGTH)))
.WillRepeatedly(DoAll(Return(UART_DEVICE_READ_NOTHING)));
ReplySelectSucceed(mock, uartFd);
}
void McuProtocolTestTool::UnlockProtocolHandle(void)
{
@ -679,12 +741,12 @@ void McuProtocolTestTool::OtherSideAskIpcMissionInit(std::shared_ptr<LinuxTest>
const unsigned int &serialNumber)
{
LockProtocolHandle();
LogInfo("OtherSideAskIpcMissionInit start.\n");
unsigned int serialNum = serialNumber;
serialNum = htonl(serialNum);
memcpy(
OTHER_SIDE_ASK_SEND_IPC_MISSION_X + PROTOCOL_SERIAL_NUMBER_OFFSET, &serialNum, PROTOCOL_SERIAL_NUMBER_LENGTH);
ResetCheckCode(OTHER_SIDE_ASK_SEND_IPC_MISSION_X, sizeof(OTHER_SIDE_ASK_SEND_IPC_MISSION_X));
ReplySelectSucceed(mock, uartFd);
constexpr int LEFT_DATA_LENGTH = sizeof(OTHER_SIDE_ASK_SEND_IPC_MISSION_X) - PROTOCOL_DATA_KEY_HEAD_LENGTH;
auto apiReadKeyHead = [=](int fd, void *buf, size_t count) {
memcpy(buf, OTHER_SIDE_ASK_SEND_IPC_MISSION_X, PROTOCOL_DATA_KEY_HEAD_LENGTH);
@ -695,11 +757,13 @@ void McuProtocolTestTool::OtherSideAskIpcMissionInit(std::shared_ptr<LinuxTest>
memcpy(buf, OTHER_SIDE_ASK_SEND_IPC_MISSION_X + PROTOCOL_DATA_KEY_HEAD_LENGTH, LEFT_DATA_LENGTH);
McuProtocolTestTool::PrintHexadecimalData(buf, LEFT_DATA_LENGTH, "OtherSideAskIpcMissionInit read:");
UnlockProtocolHandle();
LogInfo("OtherSideAskIpcMissionInit finished.\n");
};
EXPECT_CALL(*mock.get(), fx_read(uartFd, _, _))
.WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(apiReadKeyHead)), Return(PROTOCOL_DATA_KEY_HEAD_LENGTH)))
.WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(apiReadLeftData)), Return(LEFT_DATA_LENGTH)))
.WillRepeatedly(DoAll(Return(UART_DEVICE_READ_NOTHING)));
ReplySelectSucceed(mock, uartFd);
std::shared_ptr<ProtocolMonitorTest> test =
std::dynamic_pointer_cast<ProtocolMonitorTest>(ProtocolMonitorTest::GetInstance());
if (test) {
@ -708,7 +772,7 @@ void McuProtocolTestTool::OtherSideAskIpcMissionInit(std::shared_ptr<LinuxTest>
serialNumber,
REPLY_OTHER_SIDE_ASK_SEND_IPC_MISSION,
nullptr,
sizeof(OTHER_SIDE_ASK_SEND_IPC_MISSION_X));
sizeof(REPLY_OTHER_SIDE_ASK_SEND_IPC_MISSION_X));
}
}
void McuProtocolTestTool::OtherSideAskHeartBeatHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
@ -731,7 +795,6 @@ void McuProtocolTestTool::OtherSideAskHeartBeatInit(std::shared_ptr<LinuxTest> &
serialNum = htonl(serialNum);
memcpy(OTHER_SIDE_ASK_SEND_HEART_BEAT_X + PROTOCOL_SERIAL_NUMBER_OFFSET, &serialNum, PROTOCOL_SERIAL_NUMBER_LENGTH);
ResetCheckCode(OTHER_SIDE_ASK_SEND_HEART_BEAT_X, sizeof(OTHER_SIDE_ASK_SEND_HEART_BEAT_X));
ReplySelectSucceed(mock, uartFd);
constexpr int LEFT_DATA_LENGTH = sizeof(OTHER_SIDE_ASK_SEND_HEART_BEAT_X) - PROTOCOL_DATA_KEY_HEAD_LENGTH;
auto apiReadKeyHead = [=](int fd, void *buf, size_t count) {
memcpy(buf, OTHER_SIDE_ASK_SEND_HEART_BEAT_X, PROTOCOL_DATA_KEY_HEAD_LENGTH);
@ -747,6 +810,7 @@ void McuProtocolTestTool::OtherSideAskHeartBeatInit(std::shared_ptr<LinuxTest> &
.WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(apiReadKeyHead)), Return(PROTOCOL_DATA_KEY_HEAD_LENGTH)))
.WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(apiReadLeftData)), Return(LEFT_DATA_LENGTH)))
.WillRepeatedly(DoAll(Return(UART_DEVICE_READ_NOTHING)));
ReplySelectSucceed(mock, uartFd);
std::shared_ptr<ProtocolMonitorTest> test =
std::dynamic_pointer_cast<ProtocolMonitorTest>(ProtocolMonitorTest::GetInstance());
if (test) {
@ -755,7 +819,149 @@ void McuProtocolTestTool::OtherSideAskHeartBeatInit(std::shared_ptr<LinuxTest> &
serialNumber,
REPLY_OTHER_SIDE_ASK_SEND_HEART_BEAT,
nullptr,
sizeof(OTHER_SIDE_ASK_SEND_HEART_BEAT_X));
sizeof(REPLY_OTHER_SIDE_ASK_SEND_HEART_BEAT_X));
}
}
void McuProtocolTestTool::OtherSideAskGetIntervalStartHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const unsigned int &serialNumber)
{
LogInfo("OtherSideAskGetIntervalStartHandle\n");
auto handle = [=, &mock](McuProtocolTestTool *testTool) {
testTool->OtherSideAskGetIntervalStartInit(mock, uartFd, serialNumber);
};
if (mLockThread.joinable()) {
mLockThread.join();
}
mLockThread = std::thread(handle, this);
}
void McuProtocolTestTool::OtherSideAskGetIntervalStartInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const unsigned int &serialNumber)
{
LockProtocolHandle();
unsigned int serialNum = serialNumber;
serialNum = htonl(serialNum);
memcpy(
OTHER_SIDE_ASK_GET_INTERVAL_START_X + PROTOCOL_SERIAL_NUMBER_OFFSET, &serialNum, PROTOCOL_SERIAL_NUMBER_LENGTH);
ResetCheckCode(OTHER_SIDE_ASK_GET_INTERVAL_START_X, sizeof(OTHER_SIDE_ASK_GET_INTERVAL_START_X));
constexpr int LEFT_DATA_LENGTH = sizeof(OTHER_SIDE_ASK_GET_INTERVAL_START_X) - PROTOCOL_DATA_KEY_HEAD_LENGTH;
auto apiReadKeyHead = [=](int fd, void *buf, size_t count) {
memcpy(buf, OTHER_SIDE_ASK_GET_INTERVAL_START_X, PROTOCOL_DATA_KEY_HEAD_LENGTH);
McuProtocolTestTool::PrintHexadecimalData(
buf, PROTOCOL_DATA_KEY_HEAD_LENGTH, "OtherSideAskGetIntervalStartInit read:");
};
auto apiReadLeftData = [=](int fd, void *buf, size_t count) {
memcpy(buf, OTHER_SIDE_ASK_GET_INTERVAL_START_X + PROTOCOL_DATA_KEY_HEAD_LENGTH, LEFT_DATA_LENGTH);
McuProtocolTestTool::PrintHexadecimalData(buf, LEFT_DATA_LENGTH, "OtherSideAskGetIntervalStartInit read:");
UnlockProtocolHandle();
};
EXPECT_CALL(*mock.get(), fx_read(uartFd, _, _))
.WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(apiReadKeyHead)), Return(PROTOCOL_DATA_KEY_HEAD_LENGTH)))
.WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(apiReadLeftData)), Return(LEFT_DATA_LENGTH)))
.WillRepeatedly(DoAll(Return(UART_DEVICE_READ_NOTHING)));
ReplySelectSucceed(mock, uartFd);
std::shared_ptr<ProtocolMonitorTest> test =
std::dynamic_pointer_cast<ProtocolMonitorTest>(ProtocolMonitorTest::GetInstance());
if (test) {
ProtocolMonitorTest::WriteDataOnce(test,
PROTOCOL_HEAD,
serialNumber,
REPLY_OTHER_SIDE_ASK_GET_INTERVAL_START,
nullptr,
sizeof(REPLY_OTHER_SIDE_ASK_GET_INTERVAL_START_X));
}
}
void McuProtocolTestTool::OtherSideAskGetDateTimeHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const unsigned int &serialNumber)
{
LogInfo("OtherSideAskGetDateTimeHandle\n");
auto handle = [=, &mock](McuProtocolTestTool *testTool) {
testTool->OtherSideAskGetDateTimeInit(mock, uartFd, serialNumber);
};
if (mLockThread.joinable()) {
mLockThread.join();
}
mLockThread = std::thread(handle, this);
}
void McuProtocolTestTool::OtherSideAskGetDateTimeInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const unsigned int &serialNumber)
{
LockProtocolHandle();
unsigned int serialNum = serialNumber;
serialNum = htonl(serialNum);
memcpy(OTHER_SIDE_ASK_GET_DATE_TIME_X + PROTOCOL_SERIAL_NUMBER_OFFSET, &serialNum, PROTOCOL_SERIAL_NUMBER_LENGTH);
ResetCheckCode(OTHER_SIDE_ASK_GET_DATE_TIME_X, sizeof(OTHER_SIDE_ASK_GET_DATE_TIME_X));
constexpr int LEFT_DATA_LENGTH = sizeof(OTHER_SIDE_ASK_GET_DATE_TIME_X) - PROTOCOL_DATA_KEY_HEAD_LENGTH;
auto apiReadKeyHead = [=](int fd, void *buf, size_t count) {
memcpy(buf, OTHER_SIDE_ASK_GET_DATE_TIME_X, PROTOCOL_DATA_KEY_HEAD_LENGTH);
McuProtocolTestTool::PrintHexadecimalData(
buf, PROTOCOL_DATA_KEY_HEAD_LENGTH, "OtherSideAskGetDateTimeInit read:");
};
auto apiReadLeftData = [=](int fd, void *buf, size_t count) {
memcpy(buf, OTHER_SIDE_ASK_GET_DATE_TIME_X + PROTOCOL_DATA_KEY_HEAD_LENGTH, LEFT_DATA_LENGTH);
McuProtocolTestTool::PrintHexadecimalData(buf, LEFT_DATA_LENGTH, "OtherSideAskGetDateTimeInit read:");
UnlockProtocolHandle();
};
EXPECT_CALL(*mock.get(), fx_read(uartFd, _, _))
.WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(apiReadKeyHead)), Return(PROTOCOL_DATA_KEY_HEAD_LENGTH)))
.WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(apiReadLeftData)), Return(LEFT_DATA_LENGTH)))
.WillRepeatedly(DoAll(Return(UART_DEVICE_READ_NOTHING)));
ReplySelectSucceed(mock, uartFd);
std::shared_ptr<ProtocolMonitorTest> test =
std::dynamic_pointer_cast<ProtocolMonitorTest>(ProtocolMonitorTest::GetInstance());
if (test) {
ProtocolMonitorTest::WriteDataOnce(test,
PROTOCOL_HEAD,
serialNumber,
REPLY_OTHER_SIDE_ASK_GET_DATE_TIME,
nullptr,
sizeof(REPLY_OTHER_SIDE_ASK_GET_DATE_TIME_X));
}
}
void McuProtocolTestTool::OtherSideAskGetPirSensitivityHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const unsigned int &serialNumber)
{
LogInfo("OtherSideAskGetPirSensitivityHandle\n");
auto handle = [=, &mock](McuProtocolTestTool *testTool) {
testTool->OtherSideAskGetPirSensitivityInit(mock, uartFd, serialNumber);
};
if (mLockThread.joinable()) {
mLockThread.join();
}
mLockThread = std::thread(handle, this);
}
void McuProtocolTestTool::OtherSideAskGetPirSensitivityInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const unsigned int &serialNumber)
{
LockProtocolHandle();
unsigned int serialNum = serialNumber;
serialNum = htonl(serialNum);
memcpy(OTHER_SIDE_ASK_GET_SENSITIVITY_X + PROTOCOL_SERIAL_NUMBER_OFFSET, &serialNum, PROTOCOL_SERIAL_NUMBER_LENGTH);
ResetCheckCode(OTHER_SIDE_ASK_GET_SENSITIVITY_X, sizeof(OTHER_SIDE_ASK_GET_SENSITIVITY_X));
constexpr int LEFT_DATA_LENGTH = sizeof(OTHER_SIDE_ASK_GET_SENSITIVITY_X) - PROTOCOL_DATA_KEY_HEAD_LENGTH;
auto apiReadKeyHead = [=](int fd, void *buf, size_t count) {
memcpy(buf, OTHER_SIDE_ASK_GET_SENSITIVITY_X, PROTOCOL_DATA_KEY_HEAD_LENGTH);
McuProtocolTestTool::PrintHexadecimalData(
buf, PROTOCOL_DATA_KEY_HEAD_LENGTH, "OtherSideAskGetPirSensitivityInit read:");
};
auto apiReadLeftData = [=](int fd, void *buf, size_t count) {
memcpy(buf, OTHER_SIDE_ASK_GET_SENSITIVITY_X + PROTOCOL_DATA_KEY_HEAD_LENGTH, LEFT_DATA_LENGTH);
McuProtocolTestTool::PrintHexadecimalData(buf, LEFT_DATA_LENGTH, "OtherSideAskGetPirSensitivityInit read:");
UnlockProtocolHandle();
};
EXPECT_CALL(*mock.get(), fx_read(uartFd, _, _))
.WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(apiReadKeyHead)), Return(PROTOCOL_DATA_KEY_HEAD_LENGTH)))
.WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(apiReadLeftData)), Return(LEFT_DATA_LENGTH)))
.WillRepeatedly(DoAll(Return(UART_DEVICE_READ_NOTHING)));
ReplySelectSucceed(mock, uartFd);
std::shared_ptr<ProtocolMonitorTest> test =
std::dynamic_pointer_cast<ProtocolMonitorTest>(ProtocolMonitorTest::GetInstance());
if (test) {
ProtocolMonitorTest::WriteDataOnce(test,
PROTOCOL_HEAD,
serialNumber,
REPLY_OTHER_SIDE_ASK_GET_PIR_SENSITIVITY,
nullptr,
sizeof(REPLY_OTHER_SIDE_ASK_GET_SENSITIVITY_X));
}
}
void McuProtocolTestTool::PrintHexadecimalData(const void *buf, const size_t &bufLength, const char *log)

View File

@ -14,6 +14,14 @@
*/
#include "ProtocolMonitor.h"
#include "ILog.h"
static void PrintHexadecimalData(const void *buf, const size_t &bufLength, const char *log)
{
printf("%s { 0x%02X", log, *(unsigned char *)buf);
for (size_t i = 1; i < bufLength; i++) {
printf(", 0x%02X", *((unsigned char *)buf + i));
}
printf(" }\n");
}
std::shared_ptr<ProtocolMonitor> &ProtocolMonitor::GetInstance(std::shared_ptr<ProtocolMonitor> *impl)
{
static auto instance = std::make_shared<ProtocolMonitor>();
@ -34,7 +42,19 @@ void ProtocolMonitor::MonitorWriteProtocolData(const short &head, const unsigned
}
void ProtocolMonitorTest::Init(std::shared_ptr<ProtocolMonitorTest> &test)
{
EXPECT_CALL(*test.get(), MonitorWriteProtocolData(_, _, _, _, _)).WillRepeatedly(DoAll(Return()));
auto printfParam = [=](const short &head,
const unsigned int &serialNumber,
const short &command,
const void *data,
const short &packetLength) {
LogInfo("MonitorWriteProtocolData called.\n");
PrintHexadecimalData(&head, sizeof(head), "MonitorWriteProtocolData(head):");
PrintHexadecimalData(&serialNumber, sizeof(serialNumber), "MonitorWriteProtocolData(serialNumber):");
PrintHexadecimalData(&command, sizeof(command), "MonitorWriteProtocolData(command):");
PrintHexadecimalData(&packetLength, sizeof(packetLength), "MonitorWriteProtocolData(packetLength):");
};
EXPECT_CALL(*test.get(), MonitorWriteProtocolData(_, _, _, _, _))
.WillRepeatedly(DoAll(WithArgs<0, 1, 2, 3, 4>(Invoke(printfParam)), Return()));
}
void ProtocolMonitorTest::WriteDataOnce(std::shared_ptr<ProtocolMonitorTest> &test, const short &head,
const unsigned int &serialNumber, const short &command, const void *data,
@ -46,6 +66,10 @@ void ProtocolMonitorTest::WriteDataOnce(std::shared_ptr<ProtocolMonitorTest> &te
const void *data,
const short &packetLength) {
};
PrintHexadecimalData(&head, sizeof(head), "WriteDataOnce(head):");
PrintHexadecimalData(&serialNumber, sizeof(serialNumber), "WriteDataOnce(serialNumber):");
PrintHexadecimalData(&command, sizeof(command), "WriteDataOnce(command):");
PrintHexadecimalData(&packetLength, sizeof(packetLength), "WriteDataOnce(packetLength):");
EXPECT_CALL(*test.get(), MonitorWriteProtocolData(head, serialNumber, command, _, packetLength))
.Times(1)
.WillOnce(DoAll(WithArgs<0, 1, 2, 3, 4>(Invoke(printfParam)), Return()));

Binary file not shown.

View File

@ -3,9 +3,10 @@
&emsp;&emsp;负责对MCU协议进行封包/解包,负责协议的多态替换。协议数据统一使用网络字节序(大端字节序)。
| 修改 | 说明 |
| ---- | ---- |
| 2024-5-14 | 首次评审。 |
| 版本 | 时间 | 说明 |
| ---- | ---- | ---- |
| V1.0 | 2024-5-14 | 首次评审。 |
| V1.1 | 2024-5-16 | 补充数据同步协议。 |
## 1.1. 协议格式
@ -137,7 +138,7 @@ unsigned short calculate_check_sum(const unsigned char* pData, unsigned short le
| 0x0105 | - | reply | Data[0]:结果<br>0x01:成功<br>0x02:失败 | 关闭狗回复 | 取消 |
| 0x8106 | ask | - | Data[0]:Hour<br>0-23<br>Data[1]:Min<br>0-59<br>Data[2]:Sec<br>0-59 | 设置间隔启动时间 | 定时启动 |
| 0x0106 | - | reply | Data[0]:结果<br>0x01:成功<br>0x02:失败 | 设置间隔启动时间回复 | - |
| 0x8107 | ask | - | Data[0]:Year<br>Data[1]:Mon<br>1-12<br>Data[2]:Day<br>0-31<br>Data[3]:Hour<br>0-23<br>Data[4]:Min<br>0-59<br>Data[5]:Sec<br>0-59 | 设置日期和时间 | - |
| 0x8107 | ask | - | Data[0]:Year<br>0-255<br>Data[1]:Mon<br>1-12<br>Data[2]:Day<br>0-31<br>Data[3]:Hour<br>0-23<br>Data[4]:Min<br>0-59<br>Data[5]:Sec<br>0-59 | 设置日期和时间 | 年份需要+1970修正 |
| 0x0107 | - | reply | Data[0]:结果<br>0x01:成功<br>0x02:失败 | 设置日期和时间回复 | - |
| 0x8108 | ask | - | Data[0]:灵敏度<br>0-9 | 设置PIR灵敏度 | - |
| 0x0108 | - | reply | Data[0]:结果<br>0x01:成功<br>0x02:失败 | 设置PIR灵敏度回复 | - |
@ -154,7 +155,7 @@ unsigned short calculate_check_sum(const unsigned char* pData, unsigned short le
| 0x4102 | - | ask | - | 发送心跳包 | 取消 |
| 0xC106 | reply | - | Data[0]:Hour<br>0-23<br>Data[1]:Min<br>0-59<br>Data[2]:Sec<br>0-59 | 回复获取间隔启动时间 | - |
| 0x4106 | - | ask | - | 获取间隔启动时间 | - |
| 0xC107 | reply | - | Data[0]:Year<br>Data[1]:Mon<br>1-12<br>Data[2]:Day<br>0-31<br>Data[3]:Hour<br>0-23<br>Data[4]:Min<br>0-59<br>Data[5]:Sec<br>0-59 | 回复获取日期和时间 | - |
| 0xC107 | reply | - | Data[0]:Year<br>0-255<br>Data[1]:Mon<br>1-12<br>Data[2]:Day<br>0-31<br>Data[3]:Hour<br>0-23<br>Data[4]:Min<br>0-59<br>Data[5]:Sec<br>0-59 | 回复获取日期和时间 | 年份需要+1970修正 |
| 0x4107 | - | ask | - | 获取日期和时间 | - |
| 0xC108 | reply | - | Data[0]:灵敏度<br>0-9 | 回复获取PIR灵敏度 | - |
| 0x4108 | - | ask | - | 获取PIR灵敏度 | - |

View File

@ -194,8 +194,8 @@ void McuProtocol::ReplyOtherSideSendGetIntervalStart(const ReplyResult &result,
SetTime interval(hour, min, second);
std::shared_ptr<VProtocolParam> param =
std::make_shared<ProtocolParam<SetTime>>(PROTOCOL_COMMAND::REPLY_OTHER_SIDE_ASK_GET_INTERVAL_START, interval);
std::shared_ptr<ProtocolHandle> handle = ProtocolHandle::CreateProtocolData(param);
param->mSerialNumber = serialNumber;
std::shared_ptr<ProtocolHandle> handle = ProtocolHandle::CreateProtocolData(param);
WriteProtocolData(
handle->GetProtocolDataBuff(), handle->GetProtocolDataLength(), NULL_CONTEXT, handle->GetSerialNumber());
}
@ -205,11 +205,11 @@ void McuProtocol::ReplyOtherSideSendGetDateTime(const ReplyResult &result, const
const unsigned char &min, const unsigned char &second)
{
std::shared_ptr<VProtocolContext> NULL_CONTEXT;
SetDateTime dateTime(year, mon, day, hour, min, second);
SetDateTime dateTime(year - 1970, mon, day, hour, min, second);
std::shared_ptr<VProtocolParam> param =
std::make_shared<ProtocolParam<SetDateTime>>(PROTOCOL_COMMAND::REPLY_OTHER_SIDE_ASK_GET_DATE_TIME, dateTime);
std::shared_ptr<ProtocolHandle> handle = ProtocolHandle::CreateProtocolData(param);
param->mSerialNumber = serialNumber;
std::shared_ptr<ProtocolHandle> handle = ProtocolHandle::CreateProtocolData(param);
WriteProtocolData(
handle->GetProtocolDataBuff(), handle->GetProtocolDataLength(), NULL_CONTEXT, handle->GetSerialNumber());
}

View File

@ -68,6 +68,12 @@ ProtocolHandle::ProtocolHandle(const void *data, const size_t &length)
std::bind(&ProtocolHandle::AnalyzeOtherSideSendIpcMissionPacket, this, _1);
mAnalyzePacketFunc[OTHER_SIDE_ASK_SEND_HEART_BEAT] =
std::bind(&ProtocolHandle::AnalyzeOtherSideSendHeartBeatPacket, this, _1);
mAnalyzePacketFunc[OTHER_SIDE_ASK_GET_INTERVAL_START] =
std::bind(&ProtocolHandle::AnalyzeOtherSideSendGetIntervalStart, this, _1);
mAnalyzePacketFunc[OTHER_SIDE_ASK_GET_DATE_TIME] =
std::bind(&ProtocolHandle::AnalyzeOtherSideSendGetDataTime, this, _1);
mAnalyzePacketFunc[OTHER_SIDE_ASK_GET_PIR_SENSITIVITY] =
std::bind(&ProtocolHandle::AnalyzeOtherSideSendGetPirSensitivity, this, _1);
}
ProtocolHandle::~ProtocolHandle()
{
@ -110,6 +116,7 @@ void ProtocolHandle::MallocPacketDataBuff(const void *data, const size_t dataLen
// packet.mCheckCode = BigEndianConversion(packet.mCheckCode);
memcpy(mProtocolData + packetLength - CHECK_CODE_LENGTH, &packet.mCheckCode, CHECK_CODE_LENGTH);
mProtocolDataLength = packetLength;
ProtocolHandle::PrintHexadecimalData(mProtocolData, mProtocolDataLength, "Make protocol packet:");
}
void ProtocolHandle::MakeProtocolPacket(const std::shared_ptr<VProtocolParam> &param)
{

View File

@ -83,12 +83,12 @@ typedef struct watch_dog_param
} SetTime;
typedef struct set_date_time
{
set_date_time(const unsigned short &year, const unsigned char &mon, const unsigned char &day,
set_date_time(const unsigned char &year, const unsigned char &mon, const unsigned char &day,
const unsigned char &hour, const unsigned char &min, const unsigned char &second)
: mYear(year), mMon(mon), mDay(day), mHour(hour), mMin(min), mSecond(second)
{
}
const unsigned short mYear;
const unsigned char mYear;
const unsigned char mMon;
const unsigned char mDay;
const unsigned char mHour;

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];

View File

@ -56,7 +56,7 @@ bool UpgradeTool::StringToVersionBytes(const std::string &versionString, unsigne
}
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) {
@ -82,6 +82,7 @@ void UpgradeTool::FillInTime(unsigned char packTime[6])
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);
@ -91,6 +92,12 @@ void UpgradeTool::PackFile(const std::string &fileName, const std::string &outpu
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) {
@ -98,11 +105,28 @@ void UpgradeTool::PackFile(const std::string &fileName, const std::string &outpu
fclose(input_file);
return;
}
long writeSize = 0;
fwrite(&header, sizeof(header), 1, output_file);
char buffer[1024];
size_t bytes_read;
size_t bytes_read = 0;
size_t bytes_write = 0;
while ((bytes_read = fread(buffer, 1, sizeof(buffer), input_file)) > 0) {
fwrite(buffer, 1, bytes_read, output_file);
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);