diff --git a/middleware/McuManager/src/McuDevice.cpp b/middleware/McuManager/src/McuDevice.cpp index 2159919..68f84bd 100644 --- a/middleware/McuManager/src/McuDevice.cpp +++ b/middleware/McuManager/src/McuDevice.cpp @@ -185,7 +185,7 @@ void McuDevice::AddMcuAsk(std::shared_ptr &ask) mAllAsk.push_back(ask); } } -void McuDevice::SearchMcuAsk(const unsigned int &serialNumber, std::shared_ptr &ask) +bool McuDevice::SearchMcuAsk(const unsigned int &serialNumber, std::shared_ptr &ask) { std::lock_guard locker(mMutex); for (auto iter = mAllAsk.begin(); iter != mAllAsk.end(); ++iter) { @@ -196,10 +196,11 @@ void McuDevice::SearchMcuAsk(const unsigned int &serialNumber, std::shared_ptrmSerialNumber) { ask = listData; - return; + return true; } } LogWarning("Can't find mcu ask recork.\n"); + return false; } void McuDevice::DeleteMcuAsk(std::shared_ptr &ask) { diff --git a/middleware/McuManager/src/McuDevice.h b/middleware/McuManager/src/McuDevice.h index 20c57a8..1bff0bf 100644 --- a/middleware/McuManager/src/McuDevice.h +++ b/middleware/McuManager/src/McuDevice.h @@ -39,15 +39,15 @@ public: void DeviceRecvThread(void); void McuAskHandleThread(void); -private: +protected: void DeviceRecvData(const char *keyHead, const size_t headLength); void AddMcuAsk(std::shared_ptr &ask); - void SearchMcuAsk(const unsigned int &serialNumber, std::shared_ptr &ask); + bool SearchMcuAsk(const unsigned int &serialNumber, std::shared_ptr &ask); void DeleteMcuAsk(std::shared_ptr &ask); void DeleteAllAsk(void); void TraverseCheckAllAsk(void); -private: +protected: std::mutex mMutex; void *mUartDevice; std::thread mUartRecvThread; diff --git a/test/middleware/McuManager/src_mock/McuManager_Mock_Test.cpp b/test/middleware/McuManager/src_mock/McuManager_Mock_Test.cpp index c406e95..9fc04fd 100644 --- a/test/middleware/McuManager/src_mock/McuManager_Mock_Test.cpp +++ b/test/middleware/McuManager/src_mock/McuManager_Mock_Test.cpp @@ -139,6 +139,26 @@ 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) +{ + class McuAskTest : public McuAskBase + { + public: + McuAskTest() + : McuAskBase(McuAskBlock::UNRELATED, McuAskReply::NEED_NOT_REPLY) {} // using McuAskReply::NEED_NOT_REPLY + virtual ~McuAskTest() = default; + }; + IMcuManager::GetInstance()->Init(); + std::shared_ptr ask = std::make_shared(); + StatusCode code = IMcuManager::GetInstance()->FeedWatchDog(ask); + if (IsCodeOK(code) == true) { + LogInfo("Write data to mcu succeed.\n"); + } + std::this_thread::sleep_for(std::chrono::milliseconds(2000)); + IMcuManager::GetInstance()->UnInit(); +} +// ../output_files/test/bin/McuManagerTest // --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_AUTO_GetIpcMission TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_GetIpcMission) { @@ -164,8 +184,7 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_GetIpcMission) std::shared_ptr testTool = std::dynamic_pointer_cast(ask); testTool->McuAskDefaultFeatures(testTool); IMcuManager::GetInstance()->GetIpcMission(ask); - constexpr int NO_USER_USING_AFTER_ASK = 2; // ask and testTool - EXPECT_EQ(ask.use_count(), NO_USER_USING_AFTER_ASK); // TODO: neet to improve. + EXPECT_EQ(CheckAskExist(ask), false); // Ensure that the request has been processed and deleted. IMcuManager::GetInstance()->UnInit(); } // ../output_files/test/bin/McuManagerTest @@ -195,8 +214,7 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_GetIpcMission2) testTool->McuAskDefaultFeatures(testTool); IMcuManager::GetInstance()->GetIpcMission(ask); std::this_thread::sleep_for(std::chrono::milliseconds(2000)); - constexpr int NO_USER_USING_AFTER_ASK = 2; // ask and testTool - EXPECT_EQ(ask.use_count(), NO_USER_USING_AFTER_ASK); + EXPECT_EQ(CheckAskExist(ask), false); // Ensure that the request has been processed and deleted. IMcuManager::GetInstance()->UnInit(); } // ../output_files/test/bin/McuManagerTest @@ -218,8 +236,7 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_CutOffPowerSupply) std::shared_ptr testTool = std::dynamic_pointer_cast(ask); testTool->McuAskDefaultFeatures(testTool); StatusCode code = IMcuManager::GetInstance()->CutOffPowerSupply(ask); - constexpr int NO_USER_USING_AFTER_ASK = 2; // ask and testTool - EXPECT_EQ(ask.use_count(), NO_USER_USING_AFTER_ASK); + EXPECT_EQ(CheckAskExist(ask), false); // Ensure that the request has been processed and deleted. 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)); IMcuManager::GetInstance()->UnInit(); diff --git a/test/middleware/McuManager/tool/include/McuManagerTestTool.h b/test/middleware/McuManager/tool/include/McuManagerTestTool.h index d8c0e05..dcbe596 100644 --- a/test/middleware/McuManager/tool/include/McuManagerTestTool.h +++ b/test/middleware/McuManager/tool/include/McuManagerTestTool.h @@ -54,6 +54,7 @@ public: McuManagerImplTest() = default; virtual ~McuManagerImplTest() = default; MOCK_METHOD1(DeleteMcuAsk, void(std::shared_ptr &)); + bool CheckAskExist(const std::shared_ptr &ask); }; class McuManagerTestTool : virtual public McuProtocolTestTool { @@ -62,6 +63,7 @@ public: virtual ~McuManagerTestTool() = default; void Init(std::shared_ptr &mock); void UnInit(void); + bool CheckAskExist(const std::shared_ptr &ask); private: std::shared_ptr mMcuManagerMock; diff --git a/test/middleware/McuManager/tool/src/McuManagerImplTest.cpp b/test/middleware/McuManager/tool/src/McuManagerImplTest.cpp new file mode 100644 index 0000000..1145815 --- /dev/null +++ b/test/middleware/McuManager/tool/src/McuManagerImplTest.cpp @@ -0,0 +1,20 @@ +/* + * 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 "McuManagerTestTool.h" +bool McuManagerImplTest::CheckAskExist(const std::shared_ptr &ask) +{ + std::shared_ptr result; + return SearchMcuAsk(ask->mSerialNumber, result); +} \ No newline at end of file diff --git a/test/middleware/McuManager/tool/src/McuManagerTestTool.cpp b/test/middleware/McuManager/tool/src/McuManagerTestTool.cpp index 48df1b9..96b2019 100644 --- a/test/middleware/McuManager/tool/src/McuManagerTestTool.cpp +++ b/test/middleware/McuManager/tool/src/McuManagerTestTool.cpp @@ -28,7 +28,7 @@ void McuManagerTestTool::Init(std::shared_ptr &mock) { LogInfo("McuManagerTestTool::Init\n"); mMcuManagerMock = std::make_shared(); - EXPECT_CALL(*mMcuManagerMock.get(), DeleteMcuAsk(_)).Times(500); + // EXPECT_CALL(*mMcuManagerMock.get(), DeleteMcuAsk(_)).Times(500); OverrideMcuManagerMakePtrObject(); std::shared_ptr tmp = McuManagerMakePtr::GetInstance(); std::shared_ptr test = std::dynamic_pointer_cast(tmp); @@ -49,4 +49,8 @@ void McuManagerTestTool::UnInit(void) std::shared_ptr impl = std::make_shared(); McuManagerMakePtr::GetInstance(&impl); UartDeviceTestTool::UnregisterUartDevice(gUartDevice); +} +bool McuManagerTestTool::CheckAskExist(const std::shared_ptr &ask) +{ + return mMcuManagerMock->CheckAskExist(ask); } \ No newline at end of file diff --git a/test/utils/McuProtocol/tool/src/McuProtocolTestTool.cpp b/test/utils/McuProtocol/tool/src/McuProtocolTestTool.cpp index 2861a35..95d6ce6 100644 --- a/test/utils/McuProtocol/tool/src/McuProtocolTestTool.cpp +++ b/test/utils/McuProtocol/tool/src/McuProtocolTestTool.cpp @@ -24,6 +24,7 @@ const unsigned char REPLY_IPC_MISSION[] = { 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x0D, 0x01, 0xAA, 0x89}; const unsigned char ASK_CUT_OFF_POWER_SUPPLY[] = { 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x81, 0x02, 0x00, 0x0C, 0x81, 0x88}; +const unsigned char ASK_FEED_WATCH_DOG[] = {0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x81, 0x03, 0x00, 0x0C, 0xD0, 0x48}; void McuProtocolTestTool::Init(std::shared_ptr &mock, const UartInfo &uart) { int uartFd = GetDeviceMockFd(uart); @@ -39,6 +40,10 @@ void McuProtocolTestTool::Init(std::shared_ptr &mock, const UartInfo LogInfo("Set ASK_CUT_OFF_POWER_SUPPLY\n"); // IpcMissionProtocolInit(mock, uartFd); } + if (sizeof(ASK_FEED_WATCH_DOG) == count && memcmp(ASK_FEED_WATCH_DOG, buf, count) == 0) { + LogInfo("Set ASK_FEED_WATCH_DOG\n"); + // IpcMissionProtocolInit(mock, uartFd); + } }; EXPECT_CALL(*mock.get(), fx_write(uartFd, _, _)) .WillRepeatedly( diff --git a/utils/McuProtocol/src/ProtocolHandle.cpp b/utils/McuProtocol/src/ProtocolHandle.cpp index 252a433..11f70b1 100644 --- a/utils/McuProtocol/src/ProtocolHandle.cpp +++ b/utils/McuProtocol/src/ProtocolHandle.cpp @@ -30,6 +30,8 @@ ProtocolHandle::ProtocolHandle(const std::shared_ptr ¶m) : m mMakePacketFunc[ASK_IPC_MISSION] = std::bind(&ProtocolHandle::MakeAskIpcMissionPacket, this, std::placeholders::_1); mMakePacketFunc[ASK_CUT_OFF_PWOER_SUPPLY] = std::bind(&ProtocolHandle::MakeAskCutOffPowerSupplyPacket, this, std::placeholders::_1); + mMakePacketFunc[ASK_FEED_WATCH_DOG] = + std::bind(&ProtocolHandle::MakeAskFeedWatchDogPacket, this, std::placeholders::_1); } ProtocolHandle::ProtocolHandle(const void *data, const size_t &length) {