Add close watch dog function.

This commit is contained in:
Fancy code 2024-07-22 20:51:50 +08:00
parent cb5c52bae8
commit 2bfc255a57
16 changed files with 184 additions and 10 deletions

View File

@ -81,3 +81,9 @@ end
## 1.4. MCU监视器
  MCU监视器必须由其中一个状态继承只有状态机运行之后才能处理串口命令。
## 1.5. 测试启动模式TestMissionState
  面向用户开放的测试模式。
1. 此状态下先按下format按键不放再按下reset按键不放可以关闭喂狗功能

View File

@ -36,6 +36,9 @@ void LedsHandle::ControlDeviceStatusLed(const DeviceStatus &status, const long i
case DeviceStatus::TAKING_PICTURE_OR_VIDEO:
mDeviceStatus = SetLedState::ControlLed("device_status", LedState::GREEN, keepAliveTime, blinkPeriod);
break;
case DeviceStatus::CLOSE_WATCHDOG:
mDeviceStatus = SetLedState::ControlLed("device_status", LedState::RED, keepAliveTime, blinkPeriod);
break;
default:
LogWarning("unknow device status.\n");

View File

@ -23,6 +23,7 @@ enum class DeviceStatus
SD_CARD_REMOVE,
SD_CARD_INSERT,
SD_CARD_ABNORMAL,
CLOSE_WATCHDOG,
END
};
/**

View File

@ -19,6 +19,8 @@
#include "IMediaManager.h"
#include "IMissionManager.h"
#include "IStateMachine.h"
#include "LedsHandle.h"
#include "LedControl.h"
#include "MediaTask.h"
#include "MediaTaskHandle.h"
#include "MissionStateMachine.h"

View File

@ -16,27 +16,35 @@
#include "DataProcessing.h"
#include "IAppManager.h"
#include "ILog.h"
#include "IMcuManager.h"
#include "IMissionManager.h"
#include "IStateMachine.h"
#include "IStorageManager.h"
#include "LedControl.h"
#include "LedsHandle.h"
#include "McuAskBase.h"
#include "MissionState.h"
#include "MissionStateMachine.h"
#include <functional>
#include <memory>
TestMissionState::TestMissionState() : MissionState("TestMissionState"), mFormattingSDCard(false)
TestMissionState::TestMissionState()
: MissionState("TestMissionState"), mFormattingSDCard(SD_CARD_IS_NOT_FORMATTING), mResetKeyHoldTime(0),
mFormatKeyHoldTime(0)
{
mEventHandle[InternalStateEvent::ANY_STATE_SD_STATUS_PERORIED] =
std::bind(&TestMissionState::SdCardEventReportSendToApp, this, _1);
mEventHandle[InternalStateEvent::RESET_KEY_MEDIA_TASK] =
std::bind(&TestMissionState::ResetKeyMediaTaskHandle, this, _1);
mKeyClickHandle["reset"] = std::bind(&TestMissionState::ClickResetKey, this, _1);
mKeyHoldDownHandle["reset"] = std::bind(&TestMissionState::HoldDownResetKey, this, _1);
mKeyHoldDownHandle["format"] = std::bind(&TestMissionState::HoldDownFormatKey, this, _1);
mKeyHoldUpHandle["format"] = std::bind(&TestMissionState::HoldUpFormatKey, this, _1);
}
void TestMissionState::GoInState()
{
mFormattingSDCard = false;
mFormattingSDCard = SD_CARD_IS_NOT_FORMATTING;
mFormatKeyHoldTime = 0;
mResetKeyHoldTime = 0;
MissionState::GoInState();
LogInfo(" ========== TestMissionState::GoInState.\n");
AppParam mAppParam(APP_MANAGER_DEVICE_IP, APP_MANAGER_HTTP_SERVER_PORT, APP_MANAGER_TCP_SERVER_PORT); // TODO:
@ -71,32 +79,73 @@ bool TestMissionState::ResetKeyMediaTaskHandle(VStateMachineData *msg)
bool TestMissionState::ClickResetKey(const KeyEventData &data)
{
LogInfo("reset key click:make a media task.\n");
// std::shared_ptr<VMissionData> message = std::make_shared<VMissionDataV2<MediaReportEvent>>(
// static_cast<MissionEvent>(InternalStateEvent::MEDIA_REPORT_EVENT), event);
// MissionStateMachine::GetInstance()->SendStateMessage(message);
std::shared_ptr<VMissionData> message =
std::make_shared<VMissionData>(static_cast<MissionEvent>(InternalStateEvent::RESET_KEY_MEDIA_TASK));
MissionStateMachine::GetInstance()->SendStateMessage(message);
return EXECUTED;
}
bool TestMissionState::HoldDownResetKey(const KeyEventData &data)
{
constexpr int CLOSE_WATCH_DOG_PRESSING_TIME_MS = 1000 * 3;
if (mFormatKeyHoldTime > data.mHoldTime && data.mHoldTime >= CLOSE_WATCH_DOG_PRESSING_TIME_MS) {
LogInfo("Close watch dog.\n");
constexpr int KEEP_BLINKING_FAST_MS = 1000 * 3;
LedsHandle::ControlDeviceStatusLed(DeviceStatus::CLOSE_WATCHDOG, KEEP_BLINKING_FAST_MS, BLINKING_FAST_MS);
CloseWatchDog();
}
mFormattingSDCard = DO_NOT_FORMAT_SDCARD_ANYMORE;
// mResetKeyHoldTime = data.mHoldTime;
return EXECUTED;
}
bool TestMissionState::HoldUpResetKey(const KeyEventData &data)
{
mFormattingSDCard = SD_CARD_IS_NOT_FORMATTING;
return EXECUTED;
}
bool TestMissionState::HoldDownFormatKey(const KeyEventData &data)
{
constexpr int FORMAT_SD_CARD_PRESSING_TIME_MS = 1000 * 15;
if (FORMAT_SD_CARD_PRESSING_TIME_MS <= data.mHoldTime && false == mFormattingSDCard) {
if (FORMAT_SD_CARD_PRESSING_TIME_MS <= data.mHoldTime && SD_CARD_IS_NOT_FORMATTING == mFormattingSDCard) {
LogInfo("format key down.\n");
mFormattingSDCard = true;
mFormattingSDCard = DO_NOT_FORMAT_SDCARD_ANYMORE;
std::shared_ptr<VMissionData> message =
std::make_shared<VMissionData>(static_cast<MissionEvent>(InternalStateEvent::FORMAT_KEY_FORMAT_SD_CARD));
MissionStateMachine::GetInstance()->SendStateMessage(message);
return EXECUTED;
}
mFormatKeyHoldTime = data.mHoldTime;
return NOT_EXECUTED;
}
bool TestMissionState::HoldUpFormatKey(const KeyEventData &data)
{
// mFormattingSDCard = false;
mFormatKeyHoldTime = 0;
return EXECUTED;
}
void inline TestMissionState::CloseWatchDog(void)
{
class McuAskCloseWatchDog : public McuAsk<ASK_RESULT>, public McuAskBase
{
public:
McuAskCloseWatchDog() : McuAskBase(McuAskBlock::NOT_BLOCK, McuAskReply::NEED_REPLY)
{
} // using McuAskBlock::NOT_BLOCK
virtual ~McuAskCloseWatchDog() = default;
void ReplyFinished(const bool result) override
{
McuAskBase::ReplyFinished(result);
if (result) {
LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast<int>(mDataReply));
// Do something here.
}
else {
LogError("Ask data falied.\n");
}
}
};
std::shared_ptr<VMcuAsk> ask = std::make_shared<McuAskCloseWatchDog>();
IMcuManager::GetInstance()->SetFeedingCycleForWatchDog(ask, CLOSE_WATCH_DOG);
}
SdCardStatus TestMissionState::SdCardStatusConvert(const StorageEvent &event)
{
switch (event) {

View File

@ -18,6 +18,8 @@
#include "IStateMachine.h"
#include "IStorageManager.h"
#include "MissionState.h"
constexpr bool DO_NOT_FORMAT_SDCARD_ANYMORE = true;
constexpr bool SD_CARD_IS_NOT_FORMATTING = false;
class TestMissionState : public MissionState, public AppMonitor
{
public:
@ -30,13 +32,20 @@ private:
bool SdCardEventReportSendToApp(VStateMachineData *msg);
bool ResetKeyMediaTaskHandle(VStateMachineData *msg);
bool ClickResetKey(const KeyEventData &data);
bool HoldDownResetKey(const KeyEventData &data);
bool HoldUpResetKey(const KeyEventData &data);
bool HoldDownFormatKey(const KeyEventData &data);
bool HoldUpFormatKey(const KeyEventData &data);
private:
void CloseWatchDog(void);
private:
SdCardStatus SdCardStatusConvert(const StorageEvent &event);
private:
bool mFormattingSDCard;
unsigned int mResetKeyHoldTime;
unsigned int mFormatKeyHoldTime;
};
#endif

View File

@ -16,6 +16,7 @@
#define I_MCU_MANAGER_H
#include "StatusCode.h"
#include <memory>
constexpr unsigned short CLOSE_WATCH_DOG = 0;
bool CreateMcuManager(void);
bool DestroyMcuManager(void);
enum class IpcMission
@ -153,6 +154,15 @@ public:
virtual const StatusCode FeedWatchDog(std::shared_ptr<VMcuAsk> &ask);
virtual const StatusCode SetFeedingCycleForWatchDog(std::shared_ptr<VMcuAsk> &ask, const unsigned char &hour,
const unsigned char &min, const unsigned char &second);
/**
* @brief Set the dog feeding cycle. When the dog feeding cycle is set to 0, it means that the dog feeding is turned
* off.
* @param ask
* @param feedCycle_s The dog feeding cycle is in seconds. Using CLOSE_WATCH_DOG(0) means close watch dog.
* @return const StatusCode
*/
virtual const StatusCode SetFeedingCycleForWatchDog(std::shared_ptr<VMcuAsk> &ask,
const unsigned short &feedCycle_s);
virtual const StatusCode SetDateTime(std::shared_ptr<VMcuAsk> &ask, const McuAskDateTime &value);
virtual const StatusCode SetPirSensitivity(std::shared_ptr<VMcuAsk> &ask, const unsigned char &sensitivity);
virtual const StatusCode ContorlInfraredLight(std::shared_ptr<VMcuAsk> &ask, const ControlLight &control);

View File

@ -83,50 +83,68 @@ std::shared_ptr<IMcuManager> &IMcuManager::GetInstance(std::shared_ptr<IMcuManag
}
const StatusCode IMcuManager::Init(void)
{
LogWarning("STATUS_CODE_VIRTUAL_FUNCTION.\n");
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
const StatusCode IMcuManager::UnInit(void)
{
LogWarning("STATUS_CODE_VIRTUAL_FUNCTION.\n");
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
const StatusCode IMcuManager::SetMcuMonitor(std::shared_ptr<VMcuMonitor> &monitor)
{
LogWarning("STATUS_CODE_VIRTUAL_FUNCTION.\n");
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
const StatusCode IMcuManager::GetIpcMission(std::shared_ptr<VMcuAsk> &ask)
{
LogWarning("STATUS_CODE_VIRTUAL_FUNCTION.\n");
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
const StatusCode IMcuManager::CutOffPowerSupply(std::shared_ptr<VMcuAsk> &ask)
{
LogWarning("STATUS_CODE_VIRTUAL_FUNCTION.\n");
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
const StatusCode IMcuManager::FeedWatchDog(std::shared_ptr<VMcuAsk> &ask)
{
LogWarning("STATUS_CODE_VIRTUAL_FUNCTION.\n");
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
const StatusCode IMcuManager::SetFeedingCycleForWatchDog(std::shared_ptr<VMcuAsk> &ask, const unsigned char &hour,
const unsigned char &min, const unsigned char &second)
{
LogWarning("STATUS_CODE_VIRTUAL_FUNCTION.\n");
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
const StatusCode IMcuManager::SetFeedingCycleForWatchDog(std::shared_ptr<VMcuAsk> &ask,
const unsigned short &feedCycle_s)
{
LogWarning("STATUS_CODE_VIRTUAL_FUNCTION.\n");
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
const StatusCode IMcuManager::SetDateTime(std::shared_ptr<VMcuAsk> &ask, const McuAskDateTime &value)
{
LogWarning("STATUS_CODE_VIRTUAL_FUNCTION.\n");
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
const StatusCode IMcuManager::SetPirSensitivity(std::shared_ptr<VMcuAsk> &ask, const unsigned char &sensitivity)
{
LogWarning("STATUS_CODE_VIRTUAL_FUNCTION.\n");
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
const StatusCode IMcuManager::ContorlInfraredLight(std::shared_ptr<VMcuAsk> &ask, const ControlLight &control)
{
LogWarning("STATUS_CODE_VIRTUAL_FUNCTION.\n");
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
const StatusCode IMcuManager::GetPhotosensitivityValue(std::shared_ptr<VMcuAsk> &ask)
{
LogWarning("STATUS_CODE_VIRTUAL_FUNCTION.\n");
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
const char *IMcuManager::PrintIpcMissionString(const IpcMission &mission)
{
LogWarning("STATUS_CODE_VIRTUAL_FUNCTION.\n");
return "VIRTUAL FUNCTION";
}

View File

@ -121,6 +121,12 @@ const StatusCode McuManagerImpl::SetFeedingCycleForWatchDog(std::shared_ptr<VMcu
std::shared_ptr<VProtocolContext> context = std::make_shared<ProtocolContext<std::shared_ptr<VMcuAsk>>>(ask);
return McuProtocol::SetFeedingCycleForWatchDog(hour, min, second, context);
}
const StatusCode McuManagerImpl::SetFeedingCycleForWatchDog(std::shared_ptr<VMcuAsk> &ask,
const unsigned short &feedCycle_s)
{
std::shared_ptr<VProtocolContext> context = std::make_shared<ProtocolContext<std::shared_ptr<VMcuAsk>>>(ask);
return McuProtocol::SetFeedingCycleForWatchDog(feedCycle_s, context);
}
const StatusCode McuManagerImpl::SetDateTime(std::shared_ptr<VMcuAsk> &ask, const McuAskDateTime &value)
{
std::shared_ptr<VProtocolContext> context = std::make_shared<ProtocolContext<std::shared_ptr<VMcuAsk>>>(ask);

View File

@ -48,6 +48,8 @@ public:
const StatusCode FeedWatchDog(std::shared_ptr<VMcuAsk> &ask) override;
const StatusCode SetFeedingCycleForWatchDog(std::shared_ptr<VMcuAsk> &ask, const unsigned char &hour,
const unsigned char &min, const unsigned char &second) override;
const StatusCode SetFeedingCycleForWatchDog(std::shared_ptr<VMcuAsk> &ask,
const unsigned short &feedCycle_s) override;
const StatusCode SetDateTime(std::shared_ptr<VMcuAsk> &ask, const McuAskDateTime &value) override;
const StatusCode SetPirSensitivity(std::shared_ptr<VMcuAsk> &ask, const unsigned char &sensitivity) override;
const StatusCode ContorlInfraredLight(std::shared_ptr<VMcuAsk> &ask, const ControlLight &control) override;

View File

@ -64,4 +64,20 @@ TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_PirTriggeredMiss
std::this_thread::sleep_for(std::chrono::milliseconds(100));
MainThread::GetInstance()->Runing();
}
/**
* @brief Construct a new test f object
* ../output_files/test/bin/HuntingCameraTest
* --gtest_filter=HuntingCameraTest.HS_INTEGRATION_HunttingCamera_EXAMPLE_CloseWatchDog
*/
TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_CloseWatchDog)
{
McuManagerTestTool::MockOtherSideIpcMissionReply(IpcMission::TEST);
MainThread::GetInstance()->Init();
TestManager::ResetTimeOut(1000 * 10);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
HalTestTool::MockKeyClick("format", 1000 * 5); // Simulate pressing a button.
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
HalTestTool::MockKeyClick("reset", 1000 * 5); // Simulate pressing a button.
MainThread::GetInstance()->Runing();
}
} // namespace McuManager_Mock_Test

View File

@ -593,6 +593,41 @@ TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_SetFeedingCycleForWatc
IMcuManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/McuManagerTest
// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_SetFeedingCycleToCloseWatchDog
TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_SetFeedingCycleToCloseWatchDog)
{
class McuAskTest : public McuAsk<ASK_RESULT>, public McuAskBaseTestTool
{
public:
McuAskTest()
: McuAskBaseTestTool(McuAskBlock::NOT_BLOCK,
McuAskReply::NEED_REPLY) // using McuAskBlock::NOT_BLOCK
{
}
virtual ~McuAskTest() = default;
void ReplyFinished(const bool result) override
{
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");
}
}
};
IMcuManager::GetInstance()->Init();
std::shared_ptr<VMcuAsk> ask = std::make_shared<McuAskTest>();
std::shared_ptr<McuAskBaseTestTool> testTool = std::dynamic_pointer_cast<McuAskBaseTestTool>(ask);
testTool->McuAskDefaultFeatures(testTool);
StatusCode code = IMcuManager::GetInstance()->SetFeedingCycleForWatchDog(ask, CLOSE_WATCH_DOG);
EXPECT_EQ(code.mStatusCode, STATUS_CODE_OK); // STATUS_CODE_OK means write data to mcu succeed.
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
EXPECT_EQ(CheckAskExist(ask), false); // Ensure that the request has been processed and deleted.
IMcuManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/McuManagerTest
// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_STRESS_MultiThreadWrite
TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_STRESS_MultiThreadWrite)
{

View File

@ -63,7 +63,7 @@ unsigned char REPLY_IPC_MISSION_X[] = {0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x01,
unsigned char ASK_CUT_OFF_POWER_SUPPLY_X[] = {0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x81, 0x02, 0x00, 0x0C, 0xFF, 0xFF};
unsigned char ASK_FEED_WATCH_DOG_X[] = {0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x81, 0x03, 0x00, 0x0C, 0xFF, 0xFF};
unsigned char ASK_SET_FEEDING_CYCLE_X[] = {
0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x81, 0x04, 0x00, 0x0F, 0x01, 0x01, 0x01, 0xFF, 0xFF};
0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x81, 0x04, 0x00, 0x0F, 0x01, 0x01, 0xFF, 0xFF};
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[] = {

View File

@ -121,6 +121,8 @@ protected:
const StatusCode SetFeedingCycleForWatchDog(const unsigned char &hour, const unsigned char &min,
const unsigned char &second,
std::shared_ptr<VProtocolContext> &context);
const StatusCode SetFeedingCycleForWatchDog(const unsigned short &feedCycle_s,
std::shared_ptr<VProtocolContext> &context);
const StatusCode McuSetDateTime(const unsigned short &year, const unsigned char &mon, const unsigned char &day,
const unsigned char &hour, const unsigned char &min, const unsigned char &second,
std::shared_ptr<VProtocolContext> &context);

View File

@ -19,6 +19,7 @@
#include <cstddef>
#include <memory>
#include <mutex>
#include <netinet/in.h>
#include <semaphore.h>
#include <stdlib.h>
#include <string.h>
@ -142,6 +143,20 @@ const StatusCode McuProtocol::SetFeedingCycleForWatchDog(const unsigned char &ho
return WriteProtocolData(
handle->GetProtocolDataBuff(), handle->GetProtocolDataLength(), context, handle->GetSerialNumber());
}
const StatusCode McuProtocol::SetFeedingCycleForWatchDog(const unsigned short &feedCycle_s,
std::shared_ptr<VProtocolContext> &context)
{
unsigned short feedCycle = 0;
char byteOrder = ProtocolHandle::GetByteOrder();
if (ORDER_LITTLE_ENDIAN == byteOrder) {
feedCycle = htons(feedCycle_s);
}
std::shared_ptr<VProtocolParam> param =
std::make_shared<ProtocolParam<unsigned short>>(PROTOCOL_COMMAND::ASK_SET_FEEDING_CYCLE, feedCycle);
std::shared_ptr<ProtocolHandle> handle = ProtocolHandle::CreateProtocolData(param);
return WriteProtocolData(
handle->GetProtocolDataBuff(), handle->GetProtocolDataLength(), context, handle->GetSerialNumber());
}
const StatusCode McuProtocol::McuSetDateTime(const unsigned short &year, const unsigned char &mon,
const unsigned char &day, const unsigned char &hour,
const unsigned char &min, const unsigned char &second,

View File

@ -178,7 +178,7 @@ void ProtocolHandle::MakeAskFeedWatchDogPacket(const std::shared_ptr<VProtocolPa
}
void ProtocolHandle::MakeAskSetFeedingCyclePacket(const std::shared_ptr<VProtocolParam> &param)
{
MakeProtocolData<SetTime>(param);
MakeProtocolData<unsigned short>(param);
}
void ProtocolHandle::MakeAskSetDateTimePacket(const std::shared_ptr<VProtocolParam> &param)
{