diff --git a/middleware/McuManager/include/IMcuManager.h b/middleware/McuManager/include/IMcuManager.h index 51b1327..097e49d 100644 --- a/middleware/McuManager/include/IMcuManager.h +++ b/middleware/McuManager/include/IMcuManager.h @@ -102,6 +102,7 @@ public: VMcuMonitor() = default; virtual ~VMcuMonitor() = default; virtual void RecvIpcMissionEvent(std::shared_ptr &recv, const IpcMission &mission); + virtual void RecvMcuHeartBeat(std::shared_ptr &recv); }; class IMcuManager { diff --git a/middleware/McuManager/src/IMcuManager.cpp b/middleware/McuManager/src/IMcuManager.cpp index 9fc2c29..1248b4b 100644 --- a/middleware/McuManager/src/IMcuManager.cpp +++ b/middleware/McuManager/src/IMcuManager.cpp @@ -17,6 +17,9 @@ void VMcuMonitor::RecvIpcMissionEvent(std::shared_ptr &recv, const IpcMission &mission) { } +void VMcuMonitor::RecvMcuHeartBeat(std::shared_ptr &recv) +{ +} std::shared_ptr &IMcuManager::GetInstance(std::shared_ptr *impl) { static auto instance = std::make_shared(); diff --git a/middleware/McuManager/src/McuManagerImpl.cpp b/middleware/McuManager/src/McuManagerImpl.cpp index e907f28..d9ef88c 100644 --- a/middleware/McuManager/src/McuManagerImpl.cpp +++ b/middleware/McuManager/src/McuManagerImpl.cpp @@ -15,6 +15,43 @@ #include "McuManagerImpl.h" #include "ILog.h" #include "UartRecvAsk.h" +class OtherSideSend : public UartRecvAsk, public McuAsk +{ +public: + OtherSideSend(std::shared_ptr &mcuManager, const unsigned int &serialNumber, + const OtherSideSendType &sendType) + : mMcuManager(mcuManager), mSendType(sendType) + { + McuAsk::mSerialNumber = serialNumber; + } + virtual ~OtherSideSend() = default; + +protected: + std::shared_ptr mMcuManager; + +public: + const OtherSideSendType mSendType; +}; +template +class OtherSideSendWithData : public OtherSideSend +{ +public: + OtherSideSendWithData(std::shared_ptr &mcuManager, const unsigned int &serialNumber, + const OtherSideSendType &sendType, const T &otherSideData) + : OtherSideSend(mcuManager, serialNumber, sendType), mOtherSideData(otherSideData) + { + } + virtual ~OtherSideSendWithData() = default; + +public: + const T mOtherSideData; +}; +McuManagerImpl::McuManagerImpl() +{ + mMcuAskHandle[OtherSideSendType::SEND_IPC_MISSION] = + std::bind(&McuManagerImpl::McuAskSendIpcMissionHandle, this, _1); + mMcuAskHandle[OtherSideSendType::SEND_HEART_BEAT] = std::bind(&McuManagerImpl::McuAskSendHeartBeatHandle, this, _1); +} std::shared_ptr McuManagerImpl::SharedFromThis(void) { return shared_from_this(); @@ -29,11 +66,21 @@ const StatusCode McuManagerImpl::UnInit(void) { McuDevice::UnInit(); McuProtocol::UnInit(); + mMcuAskList.clear(); return CreateStatusCode(STATUS_CODE_OK); } const StatusCode McuManagerImpl::SetMcuMonitor(std::shared_ptr &monitor) { + std::lock_guard locker(mMutex); mMonitor = monitor; + for (auto ask : mMcuAskList) { + std::shared_ptr data = std::dynamic_pointer_cast(ask); + auto handle = mMcuAskHandle.find(data->mSendType); + if (handle != mMcuAskHandle.end()) { + handle->second(ask); + } + } + mMcuAskList.clear(); return CreateStatusCode(STATUS_CODE_OK); } const StatusCode McuManagerImpl::GetIpcMission(std::shared_ptr &ask) @@ -116,32 +163,90 @@ std::shared_ptr McuManagerImpl::GetMcuMonitor(void) } void McuManagerImpl::OtherSideSendIpcMission(const unsigned int &serialNumber, const unsigned char &mission) { - class OtherSideSend : public UartRecvAsk, public McuAsk + class OtherSideSendV2 : public OtherSideSendWithData { public: - OtherSideSend(std::shared_ptr &mcuManager, const unsigned int &serialNumber) - : mMcuManager(mcuManager) + OtherSideSendV2(std::shared_ptr &mcuManager, const unsigned int &serialNumber, + const OtherSideSendType &sendType, const unsigned char &mission) + : OtherSideSendWithData(mcuManager, serialNumber, sendType, mission) { - mSerialNumber = serialNumber; } - ~OtherSideSend() = default; + ~OtherSideSendV2() = default; void ReplyFinished(const bool result) override { - if (result) { - mMcuManager->ReplyOtherSideSendIpcMission(mDataReply, mSerialNumber); - } + mMcuManager->ReplyOtherSideSendIpcMission(McuAsk::mDataReply, VMcuAsk::mSerialNumber); } - std::shared_ptr mMcuManager; }; std::shared_ptr monitor = GetMcuMonitor(); + std::shared_ptr manager = std::dynamic_pointer_cast(SharedFromThis()); + std::shared_ptr ask = + std::make_shared(manager, serialNumber, OtherSideSendType::SEND_IPC_MISSION, mission); if (monitor) { - std::shared_ptr manager = std::dynamic_pointer_cast(SharedFromThis()); - std::shared_ptr ask = std::make_shared(manager, serialNumber); monitor->RecvIpcMissionEvent(ask, static_cast(mission)); } + else { + LogWarning("mMonitor is nullptr, AddMcuAsk.\n"); + AddMcuAsk(ask); + } +} +void McuManagerImpl::OtherSideSendHearBeat(const unsigned int &serialNumber) +{ + class OtherSideSendV2 : public OtherSideSend + { + public: + OtherSideSendV2(std::shared_ptr &mcuManager, const unsigned int &serialNumber, + const OtherSideSendType &sendType) + : OtherSideSend(mcuManager, serialNumber, sendType) + { + } + ~OtherSideSendV2() = default; + void ReplyFinished(const bool result) override + { + LogInfo("Mcu monitor reply heart beat.\n"); + mMcuManager->ReplyOtherSideSendHeartBeat(mSerialNumber); + } + }; + std::shared_ptr monitor = GetMcuMonitor(); + std::shared_ptr manager = std::dynamic_pointer_cast(SharedFromThis()); + std::shared_ptr ask = + std::make_shared(manager, serialNumber, OtherSideSendType::SEND_HEART_BEAT); + if (monitor) { + LogInfo("Mcu manager report heart beat to mcu monitor.\n"); + monitor->RecvMcuHeartBeat(ask); + } + else { + LogWarning("mMonitor is nullptr, AddMcuAsk.\n"); + AddMcuAsk(ask); + } } void McuManagerImpl::ReplyOtherSideSendIpcMission(const ASK_RESULT &result, const unsigned int &serialNumber) { LogInfo("ReplyOtherSideSendIpcMission\n"); - return McuProtocol::ReplyOtherSideSendIpcMission(static_cast(result), serialNumber); + McuProtocol::ReplyOtherSideSendIpcMission(static_cast(result), serialNumber); +} +void McuManagerImpl::ReplyOtherSideSendHeartBeat(const unsigned int &serialNumber) +{ + LogInfo("ReplyOtherSideSendHeartBeat\n"); + McuProtocol::ReplyOtherSideSendHearBeat(static_cast(ASK_RESULT::SUCCEED), serialNumber); +} +void McuManagerImpl::AddMcuAsk(std::shared_ptr &ask) +{ + std::lock_guard locker(mMutex); + mMcuAskList.push_back(ask); +} +void McuManagerImpl::McuAskSendIpcMissionHandle(std::shared_ptr &ask) +{ + std::shared_ptr monitor = GetMcuMonitor(); + std::shared_ptr> data = + std::dynamic_pointer_cast>(ask); + if (monitor) { + monitor->RecvIpcMissionEvent(ask, static_cast(data->mOtherSideData)); + } +} +void McuManagerImpl::McuAskSendHeartBeatHandle(std::shared_ptr &ask) +{ + std::shared_ptr monitor = GetMcuMonitor(); + if (monitor) { + monitor->RecvMcuHeartBeat(ask); + } } \ No newline at end of file diff --git a/middleware/McuManager/src/McuManagerImpl.h b/middleware/McuManager/src/McuManagerImpl.h index d524c17..448c6bb 100644 --- a/middleware/McuManager/src/McuManagerImpl.h +++ b/middleware/McuManager/src/McuManagerImpl.h @@ -17,10 +17,22 @@ #include "IMcuManager.h" #include "McuDevice.h" #include "McuProtocol.h" +#include +#include +#include +#include +using std::placeholders::_1; +using McuAskHandleFunc = std::function &)>; +enum class OtherSideSendType +{ + SEND_IPC_MISSION, + SEND_HEART_BEAT, + END +}; class McuManagerImpl : public McuDevice, public McuProtocol, public std::enable_shared_from_this { public: - McuManagerImpl() = default; + McuManagerImpl(); virtual ~McuManagerImpl() = default; std::shared_ptr SharedFromThis(void) override; const StatusCode Init(void) override; @@ -42,9 +54,28 @@ private: private: void OtherSideSendIpcMission(const unsigned int &serialNumber, const unsigned char &mission) override; + /** + * @brief The heartbeat packet must be processed by the state machine in the main thread. When the state machine + * blocks/freezes, it can be restored by the external microcontroller after powering off and restarting. + * @param serialNumber + */ + void OtherSideSendHearBeat(const unsigned int &serialNumber) override; void ReplyOtherSideSendIpcMission(const ASK_RESULT &result, const unsigned int &serialNumber); + void ReplyOtherSideSendHeartBeat(const unsigned int &serialNumber); + +private: // About mMcuAskList + void AddMcuAsk(std::shared_ptr &ask); + void McuAskSendIpcMissionHandle(std::shared_ptr &ask); + void McuAskSendHeartBeatHandle(std::shared_ptr &ask); private: + std::mutex mMutex; std::weak_ptr mMonitor; + /** + * @brief If the monitor has not been registered yet, it is necessary to cache the reported messages and report them + * again when the monitor is registered. + */ + std::list> mMcuAskList; + std::map mMcuAskHandle; }; #endif \ No newline at end of file diff --git a/middleware/McuManager/src/UartRecvAsk.h b/middleware/McuManager/src/UartRecvAsk.h index 2bc22ab..ff4ceab 100644 --- a/middleware/McuManager/src/UartRecvAsk.h +++ b/middleware/McuManager/src/UartRecvAsk.h @@ -12,8 +12,8 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#ifndef OTHER_SIDE_ASK_H -#define OTHER_SIDE_ASK_H +#ifndef UART_RECV_ASK_H +#define UART_RECV_ASK_H #include "McuAskBase.h" class UartRecvAsk : public McuAskBase { diff --git a/test/middleware/McuManager/src_mock/McuManager_Mock_Test.cpp b/test/middleware/McuManager/src_mock/McuManager_Mock_Test.cpp index 797707e..3f911c3 100644 --- a/test/middleware/McuManager/src_mock/McuManager_Mock_Test.cpp +++ b/test/middleware/McuManager/src_mock/McuManager_Mock_Test.cpp @@ -64,13 +64,13 @@ public: std::shared_ptr mLinuxTest; }; // ../output_files/test/bin/McuManagerTest -// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_EXAMPLE_GetIpcMission +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_EXAMPLE_GetIpcMission /** * @brief Construct a new test f object * This test is an example that demonstrates how to obtain IpcMission data and add the business code after obtaining the * data in the abstract interface. */ -TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_GetIpcMission) +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_EXAMPLE_GetIpcMission) { /** * @brief The user needs to derive a subclass of McuAskBase and add the business code after obtaining data in the @@ -104,13 +104,13 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_GetIpcMission) IMcuManager::GetInstance()->UnInit(); } // ../output_files/test/bin/McuManagerTest -// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_EXAMPLE_GetIpcMission2 +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_EXAMPLE_GetIpcMission2 /** * @brief Construct a new test f object * This test is an example that demonstrates how to obtain IpcMission data and add the business code after obtaining the * data in the abstract interface. */ -TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_GetIpcMission2) +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_EXAMPLE_GetIpcMission2) { /** * @brief The user needs to derive a subclass of McuAskBase and add the business code after obtaining data in the @@ -144,14 +144,14 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_GetIpcMission2) IMcuManager::GetInstance()->UnInit(); } // ../output_files/test/bin/McuManagerTest -// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_EXAMPLE_OtherSideSendIpcMission +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_EXAMPLE_OtherSideSendIpcMission /** * @brief Construct a new test f object * This example demonstrates how to use a monitor to capture event information sent by an MCU. You must assign a value - * to the processing result (mDataReply member) and call the ReplyCompleted function to complete the entire process. + * to the processing result (mDataReply member) and call the ReplyFinished function to complete the entire process. */ constexpr unsigned int TEST_SERIAL_NUMBER = 2; -TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_OtherSideSendIpcMission) +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_EXAMPLE_OtherSideSendIpcMission) { class MonitorTest : public VMcuMonitor { @@ -168,15 +168,45 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_OtherSideSendIpcMissio } }; IMcuManager::GetInstance()->Init(); - OtherSideAskIpcMission(mLinuxTest, TEST_SERIAL_NUMBER); + MockOtherSideAskIpcMission(mLinuxTest, TEST_SERIAL_NUMBER); std::shared_ptr monitor = std::make_shared(); IMcuManager::GetInstance()->SetMcuMonitor(monitor); std::this_thread::sleep_for(std::chrono::milliseconds(2000)); IMcuManager::GetInstance()->UnInit(); } // ../output_files/test/bin/McuManagerTest -// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_EXAMPLE_CutOffPowerSupply -TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_CutOffPowerSupply) +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_EXAMPLE_OtherSideSendHeartBeat +/** + * @brief Construct a new test f object + * This example demonstrates how to use a monitor to capture event information sent by an MCU. You must assign a value + * to the processing result (mDataReply member) and call the ReplyFinished function to complete the entire process. + */ +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_EXAMPLE_OtherSideSendHeartBeat) +{ + class MonitorTest : public VMcuMonitor + { + public: + MonitorTest() = default; + virtual ~MonitorTest() = default; + void RecvMcuHeartBeat(std::shared_ptr &recv) override + { + LogInfo("RecvMcuHeartBeat\n"); + std::shared_ptr> ask = std::dynamic_pointer_cast>(recv); + ask->mDataReply = ASK_RESULT::SUCCEED; + recv->ReplyFinished(true); + EXPECT_EQ(TEST_SERIAL_NUMBER, recv->mSerialNumber); + } + }; + IMcuManager::GetInstance()->Init(); + MockOtherSideAskHeartBeat(mLinuxTest, TEST_SERIAL_NUMBER); + std::shared_ptr monitor = std::make_shared(); + IMcuManager::GetInstance()->SetMcuMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(3000)); + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_EXAMPLE_CutOffPowerSupply +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_EXAMPLE_CutOffPowerSupply) { class McuAskTest : public McuAskBase { @@ -196,8 +226,8 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_CutOffPowerSupply) IMcuManager::GetInstance()->UnInit(); } // ../output_files/test/bin/McuManagerTest -// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_EXAMPLE_FeedWatchDog -TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_FeedWatchDog) +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_EXAMPLE_FeedWatchDog +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_EXAMPLE_FeedWatchDog) { class McuAskTest : public McuAskBase { @@ -217,8 +247,8 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_FeedWatchDog) IMcuManager::GetInstance()->UnInit(); } // ../output_files/test/bin/McuManagerTest -// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_EXAMPLE_SetFeedingCycleForWatchDog -TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_SetFeedingCycleForWatchDog) +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_EXAMPLE_SetFeedingCycleForWatchDog +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_EXAMPLE_SetFeedingCycleForWatchDog) { class McuAskTest : public McuAsk, public McuAskBase { @@ -249,8 +279,8 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_SetFeedingCycleForWatc IMcuManager::GetInstance()->UnInit(); } // ../output_files/test/bin/McuManagerTest -// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_EXAMPLE_SetDateTime -TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_SetDateTime) +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_EXAMPLE_SetDateTime +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_EXAMPLE_SetDateTime) { /** * @brief The user needs to derive a subclass of McuAskBase and add the business code after obtaining data in the @@ -283,8 +313,8 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_SetDateTime) IMcuManager::GetInstance()->UnInit(); } // ../output_files/test/bin/McuManagerTest -// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_EXAMPLE_SetPirSensitivity -TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_SetPirSensitivity) +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_EXAMPLE_SetPirSensitivity +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_EXAMPLE_SetPirSensitivity) { /** * @brief The user needs to derive a subclass of McuAskBase and add the business code after obtaining data in the @@ -316,8 +346,8 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_SetPirSensitivity) IMcuManager::GetInstance()->UnInit(); } // ../output_files/test/bin/McuManagerTest -// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_EXAMPLE_ContorlInfraredLight -TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_ContorlInfraredLight) +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_EXAMPLE_ContorlInfraredLight +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_EXAMPLE_ContorlInfraredLight) { /** * @brief The user needs to derive a subclass of McuAskBase and add the business code after obtaining data in the @@ -349,8 +379,8 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_ContorlInfraredLight) IMcuManager::GetInstance()->UnInit(); } // ../output_files/test/bin/McuManagerTest -// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_EXAMPLE_GetPhotosensitivityValue -TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_GetPhotosensitivityValue) +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_EXAMPLE_GetPhotosensitivityValue +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_EXAMPLE_GetPhotosensitivityValue) { /** * @brief The user needs to derive a subclass of McuAskBase and add the business code after obtaining data in the @@ -382,8 +412,8 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_GetPhotosensitivityVal IMcuManager::GetInstance()->UnInit(); } // ../output_files/test/bin/McuManagerTest -// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_AUTO_GetIpcMission -TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_GetIpcMission) +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_GetIpcMission +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_GetIpcMission) { class McuAskTest : public McuAsk, public McuAskBaseTestTool { @@ -417,9 +447,10 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_GetIpcMission) * @brief This use case demonstrates that only the serial port was opened and written successfully, and no data was * read. * Run Test: - * ../output_files/test/bin/McuManagerTest --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_AUTO_GetIpcMission_1 + * ../output_files/test/bin/McuManagerTest + * --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_GetIpcMission_1 */ -TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_GetIpcMission_1) +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_GetIpcMission_1) { class McuAskTest : public McuAsk, public McuAskBaseTestTool { @@ -451,8 +482,8 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_GetIpcMission_1) IMcuManager::GetInstance()->UnInit(); } // ../output_files/test/bin/McuManagerTest -// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_AUTO_GetIpcMission -TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_GetIpcMission2) +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_GetIpcMission +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_GetIpcMission2) { class McuAskTest : public McuAsk, public McuAskBaseTestTool { @@ -483,33 +514,8 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_GetIpcMission2) IMcuManager::GetInstance()->UnInit(); } // ../output_files/test/bin/McuManagerTest -// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_AUTO_OtherSideSendIpcMission -TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_OtherSideSendIpcMission) -{ - constexpr unsigned int TEST_SERIAL_NUMBER = 99; - class MonitorTest : public VMcuMonitor - { - public: - MonitorTest() = default; - virtual ~MonitorTest() = default; - void RecvIpcMissionEvent(std::shared_ptr &recv, const IpcMission &mission) override - { - LogInfo("RecvIpcMissionEvent\n"); - std::shared_ptr> ask = std::dynamic_pointer_cast>(recv); - ask->mDataReply = ASK_RESULT::SUCCEED; - recv->ReplyFinished(true); - } - }; - IMcuManager::GetInstance()->Init(); - OtherSideAskIpcMission(mLinuxTest, TEST_SERIAL_NUMBER); - std::shared_ptr monitor = std::make_shared(); - IMcuManager::GetInstance()->SetMcuMonitor(monitor); - std::this_thread::sleep_for(std::chrono::milliseconds(2000)); - IMcuManager::GetInstance()->UnInit(); -} -// ../output_files/test/bin/McuManagerTest -// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_AUTO_CutOffPowerSupply -TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_CutOffPowerSupply) +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_CutOffPowerSupply +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_CutOffPowerSupply) { class McuAskTest : public McuAskBaseTestTool { @@ -532,8 +538,8 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_CutOffPowerSupply) IMcuManager::GetInstance()->UnInit(); } // ../output_files/test/bin/McuManagerTest -// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_AUTO_FeedWatchDog -TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_FeedWatchDog) +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_FeedWatchDog +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_FeedWatchDog) { class McuAskTest : public McuAskBaseTestTool { @@ -556,8 +562,8 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_FeedWatchDog) IMcuManager::GetInstance()->UnInit(); } // ../output_files/test/bin/McuManagerTest -// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_AUTO_SetFeedingCycleForWatchDog -TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_SetFeedingCycleForWatchDog) +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_SetFeedingCycleForWatchDog +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_SetFeedingCycleForWatchDog) { class McuAskTest : public McuAsk, public McuAskBaseTestTool { @@ -590,8 +596,8 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_SetFeedingCycleForWatchDo IMcuManager::GetInstance()->UnInit(); } // ../output_files/test/bin/McuManagerTest -// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_AUTO_SetFeedingCycleForWatchDog2 -TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_SetFeedingCycleForWatchDog2) +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_SetFeedingCycleForWatchDog2 +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_SetFeedingCycleForWatchDog2) { class McuAskTest : public McuAsk, public McuAskBaseTestTool { @@ -625,8 +631,8 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_SetFeedingCycleForWatchDo IMcuManager::GetInstance()->UnInit(); } // ../output_files/test/bin/McuManagerTest -// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_AUTO_STRESS_MultiThreadWrite -TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_STRESS_MultiThreadWrite) +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_STRESS_MultiThreadWrite +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_STRESS_MultiThreadWrite) { class McuAskTest : public McuAsk, public McuAskBaseTestTool { @@ -696,8 +702,8 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_STRESS_MultiThreadWrite) IMcuManager::GetInstance()->UnInit(); } // ../output_files/test/bin/McuManagerTest -// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_AUTO_SetDataTime -TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_SetDataTime) +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_SetDataTime +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_SetDataTime) { class McuAskTest : public McuAsk, public McuAskBaseTestTool { @@ -731,8 +737,8 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_SetDataTime) IMcuManager::GetInstance()->UnInit(); } // ../output_files/test/bin/McuManagerTest -// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_AUTO_SetDataTime2 -TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_SetDataTime2) +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_SetDataTime2 +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_SetDataTime2) { class McuAskTest : public McuAsk, public McuAskBaseTestTool { @@ -767,8 +773,8 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_SetDataTime2) IMcuManager::GetInstance()->UnInit(); } // ../output_files/test/bin/McuManagerTest -// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_AUTO_SetPirSensitivity -TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_SetPirSensitivity) +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_SetPirSensitivity +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_SetPirSensitivity) { class McuAskTest : public McuAsk, public McuAskBaseTestTool { @@ -801,8 +807,8 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_SetPirSensitivity) IMcuManager::GetInstance()->UnInit(); } // ../output_files/test/bin/McuManagerTest -// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_AUTO_SetPirSensitivity2 -TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_SetPirSensitivity2) +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_SetPirSensitivity2 +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_SetPirSensitivity2) { class McuAskTest : public McuAsk, public McuAskBaseTestTool { @@ -836,8 +842,8 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_SetPirSensitivity2) IMcuManager::GetInstance()->UnInit(); } // ../output_files/test/bin/McuManagerTest -// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_AUTO_GetIpcMissionFailed -TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_GetIpcMissionFailed) +// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_GetIpcMissionFailed +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_GetIpcMissionFailed) { class McuAskTest : public McuAsk, public McuAskBaseTestTool { @@ -869,4 +875,29 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_GetIpcMissionFailed) 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_OtherSideSendIpcMission +TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_OtherSideSendIpcMission) +{ + constexpr unsigned int TEST_SERIAL_NUMBER = 99; + class MonitorTest : public VMcuMonitor + { + public: + MonitorTest() = default; + virtual ~MonitorTest() = default; + void RecvIpcMissionEvent(std::shared_ptr &recv, const IpcMission &mission) override + { + LogInfo("RecvIpcMissionEvent\n"); + std::shared_ptr> ask = std::dynamic_pointer_cast>(recv); + ask->mDataReply = ASK_RESULT::SUCCEED; + recv->ReplyFinished(true); + } + }; + IMcuManager::GetInstance()->Init(); + MockOtherSideAskIpcMission(mLinuxTest, TEST_SERIAL_NUMBER); + std::shared_ptr monitor = std::make_shared(); + IMcuManager::GetInstance()->SetMcuMonitor(monitor); + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IMcuManager::GetInstance()->UnInit(); +} } // namespace McuManagerMockTest \ No newline at end of file diff --git a/test/middleware/McuManager/tool/CMakeLists.txt b/test/middleware/McuManager/tool/CMakeLists.txt index 0e7c650..cfc6d46 100644 --- a/test/middleware/McuManager/tool/CMakeLists.txt +++ b/test/middleware/McuManager/tool/CMakeLists.txt @@ -20,7 +20,7 @@ include_directories( aux_source_directory(./src TEST_TOOL_SRC_FILES) set(TEST_TOOL_TARGET McuManagerTestTool) add_library(${TEST_TOOL_TARGET} STATIC ${TEST_TOOL_SRC_FILES}) -target_link_libraries(${TEST_TOOL_TARGET} McuProtocolTestTool UartDeviceTestTool LinuxApiMock Log) +target_link_libraries(${TEST_TOOL_TARGET} McuManager McuProtocolTestTool UartDeviceTestTool LinuxApiMock Log) if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true") add_custom_target( diff --git a/test/middleware/McuManager/tool/include/McuManagerTestTool.h b/test/middleware/McuManager/tool/include/McuManagerTestTool.h index 6556a7e..3c34e3e 100644 --- a/test/middleware/McuManager/tool/include/McuManagerTestTool.h +++ b/test/middleware/McuManager/tool/include/McuManagerTestTool.h @@ -34,7 +34,8 @@ public: void Init(std::shared_ptr &mock); void UnInit(void); bool CheckAskExist(const std::shared_ptr &ask); - void OtherSideAskIpcMission(std::shared_ptr &mock, const unsigned int &serialNumber); + void MockOtherSideAskIpcMission(std::shared_ptr &mock, const unsigned int &serialNumber); + void MockOtherSideAskHeartBeat(std::shared_ptr &mock, const unsigned int &serialNumber); void MockMcuDeviceOpenFailed(std::shared_ptr &mock); void MockMcuDeviceOpenSuccessButReadNothing(std::shared_ptr &mock); diff --git a/test/middleware/McuManager/tool/src/McuManagerTestTool.cpp b/test/middleware/McuManager/tool/src/McuManagerTestTool.cpp index 2f81030..93bcbff 100644 --- a/test/middleware/McuManager/tool/src/McuManagerTestTool.cpp +++ b/test/middleware/McuManager/tool/src/McuManagerTestTool.cpp @@ -44,9 +44,13 @@ bool McuManagerTestTool::CheckAskExist(const std::shared_ptr &ask) { return mMcuManagerMock->CheckAskExist(ask); } -void McuManagerTestTool::OtherSideAskIpcMission(std::shared_ptr &mock, const unsigned int &serialNumber) +void McuManagerTestTool::MockOtherSideAskIpcMission(std::shared_ptr &mock, const unsigned int &serialNumber) { - McuProtocolTestTool::OtherSideAskIpcMission(mock, serialNumber); + McuProtocolTestTool::MockOtherSideAskIpcMission(mock, serialNumber); +} +void McuManagerTestTool::MockOtherSideAskHeartBeat(std::shared_ptr &mock, const unsigned int &serialNumber) +{ + McuProtocolTestTool::MockOtherSideAskHeartBeat(mock, serialNumber); } void McuManagerTestTool::MockMcuDeviceOpenFailed(std::shared_ptr &mock) { diff --git a/test/utils/McuProtocol/tool/include/McuProtocolTestTool.h b/test/utils/McuProtocol/tool/include/McuProtocolTestTool.h index f53d1a8..34541b2 100644 --- a/test/utils/McuProtocol/tool/include/McuProtocolTestTool.h +++ b/test/utils/McuProtocol/tool/include/McuProtocolTestTool.h @@ -31,7 +31,8 @@ public: virtual ~McuProtocolTestTool() = default; void Init(std::shared_ptr &mock, const UartInfo &uart); void UnInit(void); - void OtherSideAskIpcMission(std::shared_ptr &mock, const unsigned int &serialNumber); + void MockOtherSideAskIpcMission(std::shared_ptr &mock, const unsigned int &serialNumber); + void MockOtherSideAskHeartBeat(std::shared_ptr &mock, const unsigned int &serialNumber); void ReadNothingAnyTime(std::shared_ptr &mock); private: @@ -70,6 +71,10 @@ private: const unsigned int &serialNumber); void OtherSideAskIpcMissionInit(std::shared_ptr &mock, const int &uartFd, const unsigned int &serialNumber); + void OtherSideAskHeartBeatHandle(std::shared_ptr &mock, const int &uartFd, + const unsigned int &serialNumber); + void OtherSideAskHeartBeatInit(std::shared_ptr &mock, const int &uartFd, + const unsigned int &serialNumber); private: static void PrintHexadecimalData(const void *buf, const size_t &bufLength, const char *log); diff --git a/test/utils/McuProtocol/tool/src/McuProtocolTestTool.cpp b/test/utils/McuProtocol/tool/src/McuProtocolTestTool.cpp index a0f8325..1782738 100644 --- a/test/utils/McuProtocol/tool/src/McuProtocolTestTool.cpp +++ b/test/utils/McuProtocol/tool/src/McuProtocolTestTool.cpp @@ -65,6 +65,10 @@ unsigned char REPLY_OTHER_SIDE_ASK_SEND_IPC_MISSION_X[] = { 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0xC1, 0x01, 0x00, 0x0D, 0x0A, 0xFF, 0xFF}; unsigned char OTHER_SIDE_ASK_SEND_IPC_MISSION_X[] = { 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x41, 0x01, 0x00, 0x0D, 0x01, 0xFF, 0xFF}; +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}; McuProtocolTestTool::McuProtocolTestTool() { mThreadRuning = false; @@ -133,10 +137,14 @@ void McuProtocolTestTool::UnInit(void) std::shared_ptr monitor = std::make_shared(); ProtocolMonitor::GetInstance(&monitor); } -void McuProtocolTestTool::OtherSideAskIpcMission(std::shared_ptr &mock, const unsigned int &serialNumber) +void McuProtocolTestTool::MockOtherSideAskIpcMission(std::shared_ptr &mock, const unsigned int &serialNumber) { OtherSideAskIpcMissionHandle(mock, mUartFd, serialNumber); } +void McuProtocolTestTool::MockOtherSideAskHeartBeat(std::shared_ptr &mock, const unsigned int &serialNumber) +{ + OtherSideAskHeartBeatHandle(mock, mUartFd, serialNumber); +} void McuProtocolTestTool::ReadNothingAnyTime(std::shared_ptr &mock) { static size_t WRITE_COUNT = -1; @@ -228,6 +236,7 @@ void McuProtocolTestTool::ReplySelectTimeOut(std::shared_ptr &mock, c bool McuProtocolTestTool::MonitorProtocolPacket(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count) { + LogInfo("MonitorProtocolPacket\n"); short head; unsigned int serialNumber; short command; @@ -633,7 +642,6 @@ void McuProtocolTestTool::UnlockProtocolHandle(void) } void McuProtocolTestTool::LockProtocolHandle(void) { - // mMutex.lock(); } void McuProtocolTestTool::UnlockThread(void) @@ -703,6 +711,53 @@ void McuProtocolTestTool::OtherSideAskIpcMissionInit(std::shared_ptr sizeof(OTHER_SIDE_ASK_SEND_IPC_MISSION_X)); } } +void McuProtocolTestTool::OtherSideAskHeartBeatHandle(std::shared_ptr &mock, const int &uartFd, + const unsigned int &serialNumber) +{ + LogInfo("OtherSideAskHeartBeatHandle\n"); + auto handle = [=, &mock](McuProtocolTestTool *testTool) { + testTool->OtherSideAskHeartBeatInit(mock, uartFd, serialNumber); + }; + if (mLockThread.joinable()) { + mLockThread.join(); + } + mLockThread = std::thread(handle, this); +} +void McuProtocolTestTool::OtherSideAskHeartBeatInit(std::shared_ptr &mock, const int &uartFd, + const unsigned int &serialNumber) +{ + LockProtocolHandle(); + unsigned int serialNum = serialNumber; + 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); + McuProtocolTestTool::PrintHexadecimalData( + buf, PROTOCOL_DATA_KEY_HEAD_LENGTH, "OtherSideAskHeartBeatInit read:"); + }; + auto apiReadLeftData = [=](int fd, void *buf, size_t count) { + memcpy(buf, OTHER_SIDE_ASK_SEND_HEART_BEAT_X + PROTOCOL_DATA_KEY_HEAD_LENGTH, LEFT_DATA_LENGTH); + McuProtocolTestTool::PrintHexadecimalData(buf, LEFT_DATA_LENGTH, "OtherSideAskHeartBeatInit 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))); + std::shared_ptr test = + std::dynamic_pointer_cast(ProtocolMonitorTest::GetInstance()); + if (test) { + ProtocolMonitorTest::WriteDataOnce(test, + PROTOCOL_HEAD, + serialNumber, + REPLY_OTHER_SIDE_ASK_SEND_HEART_BEAT, + nullptr, + sizeof(OTHER_SIDE_ASK_SEND_HEART_BEAT_X)); + } +} void McuProtocolTestTool::PrintHexadecimalData(const void *buf, const size_t &bufLength, const char *log) { printf("%s { 0x%02X", log, *(unsigned char *)buf); diff --git a/test/utils/McuProtocol/tool/src/ProtocolMonitor.cpp b/test/utils/McuProtocol/tool/src/ProtocolMonitor.cpp index b6bd886..ec40cc3 100644 --- a/test/utils/McuProtocol/tool/src/ProtocolMonitor.cpp +++ b/test/utils/McuProtocol/tool/src/ProtocolMonitor.cpp @@ -28,6 +28,10 @@ std::shared_ptr &ProtocolMonitor::GetInstance(std::shared_ptr

&test) { EXPECT_CALL(*test.get(), MonitorWriteProtocolData(_, _, _, _, _)).WillRepeatedly(DoAll(Return())); @@ -41,7 +45,6 @@ void ProtocolMonitorTest::WriteDataOnce(std::shared_ptr &te const short &command, const void *data, const short &packetLength) { - // }; EXPECT_CALL(*test.get(), MonitorWriteProtocolData(head, serialNumber, command, _, packetLength)) .Times(1) diff --git a/test/utils/McuProtocol/tool/src/ProtocolMonitor.h b/test/utils/McuProtocol/tool/src/ProtocolMonitor.h index 73ce399..3d49168 100644 --- a/test/utils/McuProtocol/tool/src/ProtocolMonitor.h +++ b/test/utils/McuProtocol/tool/src/ProtocolMonitor.h @@ -14,48 +14,25 @@ */ #ifndef PROTOCOL_MONITOR_H #define PROTOCOL_MONITOR_H -#include -#include +#include "GtestUsing.h" #include -using ::testing::_; -using ::testing::Action; -using ::testing::ActionInterface; -using ::testing::AnyNumber; -using ::testing::Assign; -using ::testing::AtLeast; -using ::testing::ByMove; -using ::testing::ByRef; -using ::testing::DefaultValue; -using ::testing::DoAll; -using ::testing::DoDefault; -using ::testing::IgnoreResult; -using ::testing::Invoke; -using ::testing::InvokeWithoutArgs; -using ::testing::MakePolymorphicAction; -using ::testing::PolymorphicAction; -using ::testing::Return; -using ::testing::ReturnNew; -using ::testing::ReturnNull; -using ::testing::ReturnPointee; -using ::testing::ReturnRef; -using ::testing::ReturnRefOfCopy; -using ::testing::ReturnRoundRobin; -using ::testing::SaveArg; -using ::testing::SetArgPointee; -using ::testing::SetArgumentPointee; -using ::testing::Unused; -using ::testing::WithArgs; -using ::testing::internal::BuiltInDefaultValue; class ProtocolMonitor { public: ProtocolMonitor() = default; virtual ~ProtocolMonitor() = default; static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); + /** + * @brief This function is used to monitor whether the data written to the serial port matches the protocol and + * belongs to a piling function. + * @param head Whether to match the protocol header. + * @param serialNumber Whether to match the serial number. + * @param command Whether to match command words. + * @param data + * @param packetLength Whether to match the packet length. + */ virtual void MonitorWriteProtocolData(const short &head, const unsigned int &serialNumber, const short &command, - const void *data, const short &packetLength) - { - } + const void *data, const short &packetLength); }; class ProtocolMonitorTest : public ProtocolMonitor { diff --git a/utils/McuProtocol/include/McuProtocol.h b/utils/McuProtocol/include/McuProtocol.h index 08b7ea8..d4f6c21 100644 --- a/utils/McuProtocol/include/McuProtocol.h +++ b/utils/McuProtocol/include/McuProtocol.h @@ -112,9 +112,8 @@ class OtherSideAsk public: OtherSideAsk() = default; virtual ~OtherSideAsk() = default; - virtual void OtherSideSendIpcMission(const unsigned int &serialNumber, const unsigned char &mission) - { - } + virtual void OtherSideSendIpcMission(const unsigned int &serialNumber, const unsigned char &mission); + virtual void OtherSideSendHearBeat(const unsigned int &serialNumber); }; class VProtocolRecv : public OtherSideReply, public OtherSideAsk { @@ -148,6 +147,7 @@ protected: protected: void ReplyOtherSideSendIpcMission(const ReplyResult &result, const unsigned int &serialNumber); + void ReplyOtherSideSendHearBeat(const ReplyResult &result, const unsigned int &serialNumber); protected: size_t GetKeyHeadLength(void) override; diff --git a/utils/McuProtocol/src/McuProtocol.cpp b/utils/McuProtocol/src/McuProtocol.cpp index 5e811d4..c8ed234 100644 --- a/utils/McuProtocol/src/McuProtocol.cpp +++ b/utils/McuProtocol/src/McuProtocol.cpp @@ -16,6 +16,12 @@ #include "ILog.h" #include "ProtocolHandle.h" #include +void OtherSideAsk::OtherSideSendIpcMission(const unsigned int &serialNumber, const unsigned char &mission) +{ +} +void OtherSideAsk::OtherSideSendHearBeat(const unsigned int &serialNumber) +{ +} std::shared_ptr &VProtocolRecv::GetInstance(std::shared_ptr *impl) { static auto instance = std::make_shared(); @@ -155,6 +161,18 @@ void McuProtocol::ReplyOtherSideSendIpcMission(const ReplyResult &result, const WriteProtocolData( handle->GetProtocolDataBuff(), handle->GetProtocolDataLength(), NULL_CONTEXT, handle->GetSerialNumber()); } +void McuProtocol::ReplyOtherSideSendHearBeat(const ReplyResult &result, const unsigned int &serialNumber) +{ + std::shared_ptr NULL_CONTEXT; + // std::shared_ptr param = std::make_shared>( + // PROTOCOL_COMMAND::REPLY_OTHER_SIDE_ASK_SEND_HEART_BEAT, static_cast(result)); + std::shared_ptr param = + std::make_shared(PROTOCOL_COMMAND::REPLY_OTHER_SIDE_ASK_SEND_HEART_BEAT); + param->mSerialNumber = serialNumber; + std::shared_ptr handle = ProtocolHandle::CreateProtocolData(param); + WriteProtocolData( + handle->GetProtocolDataBuff(), handle->GetProtocolDataLength(), NULL_CONTEXT, handle->GetSerialNumber()); +} size_t McuProtocol::GetKeyHeadLength(void) { return ProtocolHandle::GetKeyHeadLength(); diff --git a/utils/McuProtocol/src/ProtocolHandle.cpp b/utils/McuProtocol/src/ProtocolHandle.cpp index 0cbeb7c..4c27ba6 100644 --- a/utils/McuProtocol/src/ProtocolHandle.cpp +++ b/utils/McuProtocol/src/ProtocolHandle.cpp @@ -41,6 +41,8 @@ ProtocolHandle::ProtocolHandle(const std::shared_ptr ¶m) : m /**************************************************************************************************************************/ mMakePacketFunc[REPLY_OTHER_SIDE_ASK_SEND_IPC_MISSION] = std::bind(&ProtocolHandle::MakeReplyOtherSideSendIpcMissionPacket, this, _1); + mMakePacketFunc[REPLY_OTHER_SIDE_ASK_SEND_HEART_BEAT] = + std::bind(&ProtocolHandle::MakeReplyOtherSideSendHeartBeatPacket, this, _1); } ProtocolHandle::ProtocolHandle(const void *data, const size_t &length) { @@ -166,6 +168,10 @@ void ProtocolHandle::MakeReplyOtherSideSendIpcMissionPacket(const std::shared_pt { MakeProtocolData(param); } +void ProtocolHandle::MakeReplyOtherSideSendHeartBeatPacket(const std::shared_ptr ¶m) +{ + MakeNoUserDataPacket(param); +} void ProtocolHandle::MakeAskControlInfraredLightPacket(const std::shared_ptr ¶m) { MakeProtocolData(param); @@ -232,13 +238,11 @@ void ProtocolHandle::AnalyzeOtherSideSendIpcMissionPacket(const ProtocolPacket & void ProtocolHandle::AnalyzeOtherSideSendHeartBeatPacket(const ProtocolPacket &packet) { LogInfo("AnalyzeOtherSideSendHeartBeatPacket\n"); - unsigned char ipcMission = ReplyOneBytePacketResult(packet); - VProtocolRecv::GetInstance()->OtherSideSendIpcMission(mProtocolSerialNumber, ipcMission); + VProtocolRecv::GetInstance()->OtherSideSendHearBeat(mProtocolSerialNumber); } bool ProtocolHandle::CheckoutTheCheckCode(const ProtocolPacket &packet) { short code = calculate_check_sum(mProtocolData, mProtocolDataLength - CHECK_CODE_LENGTH); - // short packetCode = BigEndianConversion(code); if (code == packet.mCheckCode) { return true; } @@ -247,12 +251,10 @@ bool ProtocolHandle::CheckoutTheCheckCode(const ProtocolPacket &packet) void ProtocolHandle::Init(void) { - // mSerialNumber = 1; } void ProtocolHandle::UnInit(void) { - // mSerialNumber = 1; } std::shared_ptr ProtocolHandle::CreateProtocolData(const std::shared_ptr ¶m) diff --git a/utils/McuProtocol/src/ProtocolHandle.h b/utils/McuProtocol/src/ProtocolHandle.h index 1411ab1..2079ce2 100644 --- a/utils/McuProtocol/src/ProtocolHandle.h +++ b/utils/McuProtocol/src/ProtocolHandle.h @@ -161,13 +161,14 @@ private: void MakeAskControlInfraredLightPacket(const std::shared_ptr ¶m); void MakeAskGetPhotosensitivityPacket(const std::shared_ptr ¶m); void MakeReplyOtherSideSendIpcMissionPacket(const std::shared_ptr ¶m); + void MakeReplyOtherSideSendHeartBeatPacket(const std::shared_ptr ¶m); template void MakeProtocolData(const std::shared_ptr ¶m) { constexpr int PARAM_DATA_LENGTH = sizeof(T); std::shared_ptr> SetParam = std::dynamic_pointer_cast>(param); if (!SetParam) { - LogError("Invalid param.\n"); + LogError("MakeProtocolData::Invalid param.\n"); return; } MallocPacketDataBuff(&(SetParam->mData), PARAM_DATA_LENGTH, param->mCommand);