From cb2c073c3de035e5e95bfadc5b0e63b5c451e5c0 Mon Sep 17 00:00:00 2001 From: Fancy code <258828110.@qq.com> Date: Sat, 10 Feb 2024 09:23:51 -0800 Subject: [PATCH] Improve:McuProtocol test code. --- .../tool/include/McuProtocolTestTool.h | 12 ++++--- .../tool/src/McuProtocolTestTool.cpp | 33 ++++++++++++++----- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/test/utils/McuProtocol/tool/include/McuProtocolTestTool.h b/test/utils/McuProtocol/tool/include/McuProtocolTestTool.h index 750ba5c..551c32c 100644 --- a/test/utils/McuProtocol/tool/include/McuProtocolTestTool.h +++ b/test/utils/McuProtocol/tool/include/McuProtocolTestTool.h @@ -21,6 +21,9 @@ #include constexpr int READ_PRINT = 0; constexpr int WRITE_PRINT = 1; +constexpr bool PROTOCOL_HANDLED = true; +constexpr bool PROTOCOL_NOT_HANDLED = false; +using ProtocolHandleFunc = std::function &, const int &, const void *, size_t)>; class McuProtocolTestTool : virtual public UartDeviceTestTool { public: @@ -34,12 +37,12 @@ private: void ChecCRC16Code(const void *buf, const size_t &count); void ResetCheckCode(const void *buf, const size_t &count); void ReplySelectSucceed(std::shared_ptr &mock, const int &uartFd); - void IpcMissionProtocolHandle(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count); + bool IpcMissionProtocolHandle(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count); void IpcMissionProtocolInit(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count); - void CutOffPowerSupplyProtocolHandle(std::shared_ptr &mock, const int &uartFd, const void *buf, + bool CutOffPowerSupplyProtocolHandle(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count); - void FeedWatchDogProtocolHandle(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count); - void FeedingCycleProtocolHandle(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count); + bool FeedWatchDogProtocolHandle(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count); + bool FeedingCycleProtocolHandle(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count); void FeedingCycleProtocolInit(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count); void LockProtocolHandle(void); void UnlockProtocolHandle(void); @@ -57,5 +60,6 @@ private: std::list mSerialNumberList; bool mPipeFdMockSelectInit; int mPipeFdMockSelect[2]; + std::list mProtocolHandle; }; #endif \ 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 76dd1fe..790a06a 100644 --- a/test/utils/McuProtocol/tool/src/McuProtocolTestTool.cpp +++ b/test/utils/McuProtocol/tool/src/McuProtocolTestTool.cpp @@ -19,6 +19,10 @@ #include #include #include +using std::placeholders::_1; +using std::placeholders::_2; +using std::placeholders::_3; +using std::placeholders::_4; constexpr int PIPE_READ_FD_INDEX = 0; constexpr int PIPE_WRITE_FD_INDEX = 1; const char *gPipeBuf = "nothing"; @@ -40,6 +44,10 @@ McuProtocolTestTool::McuProtocolTestTool() mPipeFdMockSelectInit = false; mPipeFdMockSelect[PIPE_READ_FD_INDEX] = -1; mPipeFdMockSelect[PIPE_WRITE_FD_INDEX] = -1; + mProtocolHandle.push_back(std::bind(&McuProtocolTestTool::IpcMissionProtocolHandle, this, _1, _2, _3, _4)); + mProtocolHandle.push_back(std::bind(&McuProtocolTestTool::CutOffPowerSupplyProtocolHandle, this, _1, _2, _3, _4)); + mProtocolHandle.push_back(std::bind(&McuProtocolTestTool::FeedWatchDogProtocolHandle, this, _1, _2, _3, _4)); + mProtocolHandle.push_back(std::bind(&McuProtocolTestTool::FeedingCycleProtocolHandle, this, _1, _2, _3, _4)); } void McuProtocolTestTool::Init(std::shared_ptr &mock, const UartInfo &uart) { @@ -55,10 +63,11 @@ void McuProtocolTestTool::Init(std::shared_ptr &mock, const UartInfo McuProtocolTestTool::PrintHexadecimalData(buf, count, WRITE_PRINT); CheckSerialNumber(buf, count); ChecCRC16Code(buf, count); - IpcMissionProtocolHandle(mock, uartFd, buf, count); - CutOffPowerSupplyProtocolHandle(mock, uartFd, buf, count); - FeedWatchDogProtocolHandle(mock, uartFd, buf, count); - FeedingCycleProtocolHandle(mock, uartFd, buf, count); + for (auto iter = mProtocolHandle.begin(); iter != mProtocolHandle.end(); ++iter) { + if ((*iter)(mock, uartFd, buf, count) == PROTOCOL_HANDLED) { + return; + } + } }; EXPECT_CALL(*mock.get(), fx_write(uartFd, _, _)) .WillRepeatedly( @@ -144,7 +153,7 @@ void McuProtocolTestTool::ReplySelectSucceed(std::shared_ptr &mock, c .WillRepeatedly(DoAll(WithArgs<0, 1, 2, 3, 4>(Invoke(selectTimeOut)), Return(MOCK_SELECT_TIME_OUT))); PipeSelectTimeoutForProtocolHandleImmediately(); } -void McuProtocolTestTool::IpcMissionProtocolHandle(std::shared_ptr &mock, const int &uartFd, const void *buf, +bool McuProtocolTestTool::IpcMissionProtocolHandle(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count) { if (sizeof(ASK_IPC_MISSION) == count && @@ -160,7 +169,9 @@ void McuProtocolTestTool::IpcMissionProtocolHandle(std::shared_ptr &m mLockThread.join(); } mLockThread = std::thread(handle, this); + return PROTOCOL_HANDLED; } + return PROTOCOL_NOT_HANDLED; } void McuProtocolTestTool::IpcMissionProtocolInit(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count) @@ -184,7 +195,7 @@ void McuProtocolTestTool::IpcMissionProtocolInit(std::shared_ptr &moc .WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(apiReadLeftData)), Return(LEFT_DATA_LENGTH))) .WillRepeatedly(DoAll(Return(UART_DEVICE_READ_NOTHING))); } -void McuProtocolTestTool::CutOffPowerSupplyProtocolHandle(std::shared_ptr &mock, const int &uartFd, +bool McuProtocolTestTool::CutOffPowerSupplyProtocolHandle(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count) { if (sizeof(ASK_CUT_OFF_POWER_SUPPLY) == count && @@ -198,9 +209,11 @@ void McuProtocolTestTool::CutOffPowerSupplyProtocolHandle(std::shared_ptr &mock, const int &uartFd, +bool McuProtocolTestTool::FeedWatchDogProtocolHandle(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count) { if (sizeof(ASK_FEED_WATCH_DOG) == count && @@ -212,9 +225,11 @@ void McuProtocolTestTool::FeedWatchDogProtocolHandle(std::shared_ptr EXPECT_EQ(memcmp((unsigned char *)ASK_FEED_WATCH_DOG + count - PROTOCOL_CHECK_CODE_LENGTH, &askCheckCode, 2), 0); // IpcMissionProtocolInit(mock, uartFd); + return PROTOCOL_HANDLED; } + return PROTOCOL_NOT_HANDLED; } -void McuProtocolTestTool::FeedingCycleProtocolHandle(std::shared_ptr &mock, const int &uartFd, +bool McuProtocolTestTool::FeedingCycleProtocolHandle(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count) { if (sizeof(ASK_SET_FEEDING_CYCLE) == count && @@ -237,7 +252,9 @@ void McuProtocolTestTool::FeedingCycleProtocolHandle(std::shared_ptr } mLockThread = std::thread(handle, this); // FeedingCycleProtocolInit(mock, uartFd); + return PROTOCOL_HANDLED; } + return PROTOCOL_NOT_HANDLED; } void McuProtocolTestTool::FeedingCycleProtocolInit(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count)