Improve:watch dog thread.
This commit is contained in:
parent
844ecd7e8b
commit
8f89a12e5e
|
@ -14,6 +14,7 @@
|
||||||
*/
|
*/
|
||||||
#include "McuManagerImpl.h"
|
#include "McuManagerImpl.h"
|
||||||
#include "ILog.h"
|
#include "ILog.h"
|
||||||
|
#include "McuAskBase.h"
|
||||||
class McuRecvImpl
|
class McuRecvImpl
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -30,7 +31,7 @@ protected:
|
||||||
public:
|
public:
|
||||||
const OtherSideSendType mSendType;
|
const OtherSideSendType mSendType;
|
||||||
};
|
};
|
||||||
McuManagerImpl::McuManagerImpl()
|
McuManagerImpl::McuManagerImpl() : mWatchDogRuning(false)
|
||||||
{
|
{
|
||||||
mMcuAskHandle[OtherSideSendType::SEND_IPC_MISSION] =
|
mMcuAskHandle[OtherSideSendType::SEND_IPC_MISSION] =
|
||||||
std::bind(&McuManagerImpl::McuAskSendIpcMissionHandle, this, _1);
|
std::bind(&McuManagerImpl::McuAskSendIpcMissionHandle, this, _1);
|
||||||
|
@ -49,6 +50,11 @@ const StatusCode McuManagerImpl::Init(void)
|
||||||
{
|
{
|
||||||
McuDevice::Init();
|
McuDevice::Init();
|
||||||
McuProtocol::Init();
|
McuProtocol::Init();
|
||||||
|
auto watchThread = [](std::shared_ptr<McuManagerImpl> mcuManager) {
|
||||||
|
LogInfo("mWatchDogThread started.\n");
|
||||||
|
mcuManager->WatchDogThread();
|
||||||
|
};
|
||||||
|
mWatchDogThread = std::thread(watchThread, shared_from_this());
|
||||||
return CreateStatusCode(STATUS_CODE_OK);
|
return CreateStatusCode(STATUS_CODE_OK);
|
||||||
}
|
}
|
||||||
const StatusCode McuManagerImpl::UnInit(void)
|
const StatusCode McuManagerImpl::UnInit(void)
|
||||||
|
@ -57,6 +63,13 @@ const StatusCode McuManagerImpl::UnInit(void)
|
||||||
McuProtocol::UnInit();
|
McuProtocol::UnInit();
|
||||||
mMcuAskList.clear();
|
mMcuAskList.clear();
|
||||||
mMonitor.reset();
|
mMonitor.reset();
|
||||||
|
mMutex.lock();
|
||||||
|
mWatchDogRuning = false;
|
||||||
|
mCv.notify_one();
|
||||||
|
mMutex.unlock();
|
||||||
|
if (mWatchDogThread.joinable()) {
|
||||||
|
mWatchDogThread.join();
|
||||||
|
}
|
||||||
return CreateStatusCode(STATUS_CODE_OK);
|
return CreateStatusCode(STATUS_CODE_OK);
|
||||||
}
|
}
|
||||||
const StatusCode McuManagerImpl::SetMcuMonitor(std::shared_ptr<VMcuMonitor> &monitor)
|
const StatusCode McuManagerImpl::SetMcuMonitor(std::shared_ptr<VMcuMonitor> &monitor)
|
||||||
|
@ -139,6 +152,28 @@ const char *McuManagerImpl::PrintIpcMissionString(const IpcMission &mission)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
void McuManagerImpl::WatchDogThread(void)
|
||||||
|
{
|
||||||
|
class WatchDogAsk : public McuAskBase
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
WatchDogAsk() : McuAskBase(McuAskBlock::UNRELATED, McuAskReply::NEED_NOT_REPLY)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
virtual ~WatchDogAsk() = default;
|
||||||
|
};
|
||||||
|
mWatchDogRuning = true;
|
||||||
|
constexpr int WATCH_DOG_CYCLE_MS = 1000 * 4;
|
||||||
|
std::shared_ptr<VMcuAsk> ask = std::make_shared<WatchDogAsk>();
|
||||||
|
std::shared_ptr<VProtocolContext> context = std::make_shared<ProtocolContext<std::shared_ptr<VMcuAsk>>>(ask);
|
||||||
|
while (mWatchDogRuning) {
|
||||||
|
std::unique_lock<std::mutex> lock(mMutex);
|
||||||
|
McuProtocol::FeedWatchDog(context);
|
||||||
|
mCv.wait_for(lock, std::chrono::milliseconds(WATCH_DOG_CYCLE_MS), [&] {
|
||||||
|
return !mWatchDogRuning;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
std::shared_ptr<VMcuMonitor> McuManagerImpl::GetMcuMonitor(void)
|
std::shared_ptr<VMcuMonitor> McuManagerImpl::GetMcuMonitor(void)
|
||||||
{
|
{
|
||||||
auto monitor = mMonitor.lock();
|
auto monitor = mMonitor.lock();
|
||||||
|
|
|
@ -17,10 +17,12 @@
|
||||||
#include "IMcuManager.h"
|
#include "IMcuManager.h"
|
||||||
#include "McuDevice.h"
|
#include "McuDevice.h"
|
||||||
#include "McuProtocol.h"
|
#include "McuProtocol.h"
|
||||||
|
#include <condition_variable>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
#include <thread>
|
||||||
using std::placeholders::_1;
|
using std::placeholders::_1;
|
||||||
using McuAskHandleFunc = std::function<void(std::shared_ptr<VMcuRecv> &)>;
|
using McuAskHandleFunc = std::function<void(std::shared_ptr<VMcuRecv> &)>;
|
||||||
enum class OtherSideSendType
|
enum class OtherSideSendType
|
||||||
|
@ -51,6 +53,7 @@ public:
|
||||||
const StatusCode ContorlInfraredLight(std::shared_ptr<VMcuAsk> &ask, const ControlLight &control) override;
|
const StatusCode ContorlInfraredLight(std::shared_ptr<VMcuAsk> &ask, const ControlLight &control) override;
|
||||||
const StatusCode GetPhotosensitivityValue(std::shared_ptr<VMcuAsk> &ask) override;
|
const StatusCode GetPhotosensitivityValue(std::shared_ptr<VMcuAsk> &ask) override;
|
||||||
const char *PrintIpcMissionString(const IpcMission &mission) override;
|
const char *PrintIpcMissionString(const IpcMission &mission) override;
|
||||||
|
void WatchDogThread(void);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<VMcuMonitor> GetMcuMonitor(void);
|
std::shared_ptr<VMcuMonitor> GetMcuMonitor(void);
|
||||||
|
@ -90,5 +93,8 @@ private:
|
||||||
*/
|
*/
|
||||||
std::list<std::shared_ptr<VMcuRecv>> mMcuAskList;
|
std::list<std::shared_ptr<VMcuRecv>> mMcuAskList;
|
||||||
std::map<OtherSideSendType, McuAskHandleFunc> mMcuAskHandle;
|
std::map<OtherSideSendType, McuAskHandleFunc> mMcuAskHandle;
|
||||||
|
bool mWatchDogRuning = false;
|
||||||
|
std::thread mWatchDogThread;
|
||||||
|
std::condition_variable mCv;
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
|
@ -562,6 +562,14 @@ TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_FeedWatchDog)
|
||||||
IMcuManager::GetInstance()->UnInit();
|
IMcuManager::GetInstance()->UnInit();
|
||||||
}
|
}
|
||||||
// ../output_files/test/bin/McuManagerTest
|
// ../output_files/test/bin/McuManagerTest
|
||||||
|
// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_FeedWatchDogThread
|
||||||
|
TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_FeedWatchDogThread)
|
||||||
|
{
|
||||||
|
IMcuManager::GetInstance()->Init();
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||||
|
IMcuManager::GetInstance()->UnInit();
|
||||||
|
}
|
||||||
|
// ../output_files/test/bin/McuManagerTest
|
||||||
// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_SetFeedingCycleForWatchDog
|
// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_SetFeedingCycleForWatchDog
|
||||||
TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_SetFeedingCycleForWatchDog)
|
TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_SetFeedingCycleForWatchDog)
|
||||||
{
|
{
|
||||||
|
@ -946,6 +954,7 @@ TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_OtherSideGetIntervalSt
|
||||||
recvData->mDataRecvReply.mMin = 10;
|
recvData->mDataRecvReply.mMin = 10;
|
||||||
recvData->mDataRecvReply.mSecond = 10;
|
recvData->mDataRecvReply.mSecond = 10;
|
||||||
recv->ReplyFinished(true);
|
recv->ReplyFinished(true);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
recv->ReplyFinished(false);
|
recv->ReplyFinished(false);
|
||||||
}
|
}
|
||||||
|
@ -984,6 +993,7 @@ TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_OtherSideGetIntervalSt
|
||||||
recvData->mDataRecvReply.mMin = 10;
|
recvData->mDataRecvReply.mMin = 10;
|
||||||
recvData->mDataRecvReply.mSecond = 10;
|
recvData->mDataRecvReply.mSecond = 10;
|
||||||
recv->ReplyFinished(true);
|
recv->ReplyFinished(true);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
recv->ReplyFinished(false);
|
recv->ReplyFinished(false);
|
||||||
}
|
}
|
||||||
|
@ -1020,6 +1030,7 @@ TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_OtherSideGetDateTime)
|
||||||
recvData->mDataRecvReply.mMin = 10;
|
recvData->mDataRecvReply.mMin = 10;
|
||||||
recvData->mDataRecvReply.mSecond = 10;
|
recvData->mDataRecvReply.mSecond = 10;
|
||||||
recv->ReplyFinished(true);
|
recv->ReplyFinished(true);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
recv->ReplyFinished(false);
|
recv->ReplyFinished(false);
|
||||||
}
|
}
|
||||||
|
@ -1061,6 +1072,7 @@ TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_OtherSideGetDateTime2)
|
||||||
recvData->mDataRecvReply.mMin = 10;
|
recvData->mDataRecvReply.mMin = 10;
|
||||||
recvData->mDataRecvReply.mSecond = 10;
|
recvData->mDataRecvReply.mSecond = 10;
|
||||||
recv->ReplyFinished(true);
|
recv->ReplyFinished(true);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
recv->ReplyFinished(false);
|
recv->ReplyFinished(false);
|
||||||
}
|
}
|
||||||
|
@ -1092,6 +1104,7 @@ TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_OtherSideGetPirSensiti
|
||||||
if (recvData) {
|
if (recvData) {
|
||||||
recvData->mDataRecvReply.mSensitivity = 9;
|
recvData->mDataRecvReply.mSensitivity = 9;
|
||||||
recv->ReplyFinished(true);
|
recv->ReplyFinished(true);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
recv->ReplyFinished(false);
|
recv->ReplyFinished(false);
|
||||||
}
|
}
|
||||||
|
@ -1128,6 +1141,7 @@ TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_OtherSideGetPirSensiti
|
||||||
if (recvData) {
|
if (recvData) {
|
||||||
recvData->mDataRecvReply.mSensitivity = 9;
|
recvData->mDataRecvReply.mSensitivity = 9;
|
||||||
recv->ReplyFinished(true);
|
recv->ReplyFinished(true);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
recv->ReplyFinished(false);
|
recv->ReplyFinished(false);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,8 @@
|
||||||
|
|
||||||
  负责对MCU协议进行封包/解包,负责协议的多态替换。协议数据统一使用网络字节序(大端字节序)。
|
  负责对MCU协议进行封包/解包,负责协议的多态替换。协议数据统一使用网络字节序(大端字节序)。
|
||||||
|
|
||||||
|
**注:协议数据统一使用网络字节序(大端字节序)**
|
||||||
|
|
||||||
| 版本 | 时间 | 说明 |
|
| 版本 | 时间 | 说明 |
|
||||||
| ---- | ---- | ---- |
|
| ---- | ---- | ---- |
|
||||||
| V1.0 | 2024-5-14 | 首次评审。 |
|
| V1.0 | 2024-5-14 | 首次评审。 |
|
||||||
|
|
Loading…
Reference in New Issue
Block a user