Improve:McuProtocol test code.

This commit is contained in:
Fancy code 2024-02-10 09:23:51 -08:00
parent 4627d47f50
commit cb2c073c3d
2 changed files with 33 additions and 12 deletions

View File

@ -21,6 +21,9 @@
#include <thread> #include <thread>
constexpr int READ_PRINT = 0; constexpr int READ_PRINT = 0;
constexpr int WRITE_PRINT = 1; constexpr int WRITE_PRINT = 1;
constexpr bool PROTOCOL_HANDLED = true;
constexpr bool PROTOCOL_NOT_HANDLED = false;
using ProtocolHandleFunc = std::function<bool(std::shared_ptr<LinuxTest> &, const int &, const void *, size_t)>;
class McuProtocolTestTool : virtual public UartDeviceTestTool class McuProtocolTestTool : virtual public UartDeviceTestTool
{ {
public: public:
@ -34,12 +37,12 @@ private:
void ChecCRC16Code(const void *buf, const size_t &count); void ChecCRC16Code(const void *buf, const size_t &count);
void ResetCheckCode(const void *buf, const size_t &count); void ResetCheckCode(const void *buf, const size_t &count);
void ReplySelectSucceed(std::shared_ptr<LinuxTest> &mock, const int &uartFd); void ReplySelectSucceed(std::shared_ptr<LinuxTest> &mock, const int &uartFd);
void IpcMissionProtocolHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf, size_t count); bool IpcMissionProtocolHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf, size_t count);
void IpcMissionProtocolInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf, size_t count); void IpcMissionProtocolInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf, size_t count);
void CutOffPowerSupplyProtocolHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf, bool CutOffPowerSupplyProtocolHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf,
size_t count); size_t count);
void FeedWatchDogProtocolHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf, size_t count); bool FeedWatchDogProtocolHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf, size_t count);
void FeedingCycleProtocolHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf, size_t count); bool FeedingCycleProtocolHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf, size_t count);
void FeedingCycleProtocolInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf, size_t count); void FeedingCycleProtocolInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf, size_t count);
void LockProtocolHandle(void); void LockProtocolHandle(void);
void UnlockProtocolHandle(void); void UnlockProtocolHandle(void);
@ -57,5 +60,6 @@ private:
std::list<unsigned int> mSerialNumberList; std::list<unsigned int> mSerialNumberList;
bool mPipeFdMockSelectInit; bool mPipeFdMockSelectInit;
int mPipeFdMockSelect[2]; int mPipeFdMockSelect[2];
std::list<ProtocolHandleFunc> mProtocolHandle;
}; };
#endif #endif

View File

@ -19,6 +19,10 @@
#include <string.h> #include <string.h>
#include <thread> #include <thread>
#include <unistd.h> #include <unistd.h>
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_READ_FD_INDEX = 0;
constexpr int PIPE_WRITE_FD_INDEX = 1; constexpr int PIPE_WRITE_FD_INDEX = 1;
const char *gPipeBuf = "nothing"; const char *gPipeBuf = "nothing";
@ -40,6 +44,10 @@ McuProtocolTestTool::McuProtocolTestTool()
mPipeFdMockSelectInit = false; mPipeFdMockSelectInit = false;
mPipeFdMockSelect[PIPE_READ_FD_INDEX] = -1; mPipeFdMockSelect[PIPE_READ_FD_INDEX] = -1;
mPipeFdMockSelect[PIPE_WRITE_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<LinuxTest> &mock, const UartInfo &uart) void McuProtocolTestTool::Init(std::shared_ptr<LinuxTest> &mock, const UartInfo &uart)
{ {
@ -55,10 +63,11 @@ void McuProtocolTestTool::Init(std::shared_ptr<LinuxTest> &mock, const UartInfo
McuProtocolTestTool::PrintHexadecimalData(buf, count, WRITE_PRINT); McuProtocolTestTool::PrintHexadecimalData(buf, count, WRITE_PRINT);
CheckSerialNumber(buf, count); CheckSerialNumber(buf, count);
ChecCRC16Code(buf, count); ChecCRC16Code(buf, count);
IpcMissionProtocolHandle(mock, uartFd, buf, count); for (auto iter = mProtocolHandle.begin(); iter != mProtocolHandle.end(); ++iter) {
CutOffPowerSupplyProtocolHandle(mock, uartFd, buf, count); if ((*iter)(mock, uartFd, buf, count) == PROTOCOL_HANDLED) {
FeedWatchDogProtocolHandle(mock, uartFd, buf, count); return;
FeedingCycleProtocolHandle(mock, uartFd, buf, count); }
}
}; };
EXPECT_CALL(*mock.get(), fx_write(uartFd, _, _)) EXPECT_CALL(*mock.get(), fx_write(uartFd, _, _))
.WillRepeatedly( .WillRepeatedly(
@ -144,7 +153,7 @@ void McuProtocolTestTool::ReplySelectSucceed(std::shared_ptr<LinuxTest> &mock, c
.WillRepeatedly(DoAll(WithArgs<0, 1, 2, 3, 4>(Invoke(selectTimeOut)), Return(MOCK_SELECT_TIME_OUT))); .WillRepeatedly(DoAll(WithArgs<0, 1, 2, 3, 4>(Invoke(selectTimeOut)), Return(MOCK_SELECT_TIME_OUT)));
PipeSelectTimeoutForProtocolHandleImmediately(); PipeSelectTimeoutForProtocolHandleImmediately();
} }
void McuProtocolTestTool::IpcMissionProtocolHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf, bool McuProtocolTestTool::IpcMissionProtocolHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf,
size_t count) size_t count)
{ {
if (sizeof(ASK_IPC_MISSION) == count && if (sizeof(ASK_IPC_MISSION) == count &&
@ -160,7 +169,9 @@ void McuProtocolTestTool::IpcMissionProtocolHandle(std::shared_ptr<LinuxTest> &m
mLockThread.join(); mLockThread.join();
} }
mLockThread = std::thread(handle, this); mLockThread = std::thread(handle, this);
return PROTOCOL_HANDLED;
} }
return PROTOCOL_NOT_HANDLED;
} }
void McuProtocolTestTool::IpcMissionProtocolInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf, void McuProtocolTestTool::IpcMissionProtocolInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf,
size_t count) size_t count)
@ -184,7 +195,7 @@ void McuProtocolTestTool::IpcMissionProtocolInit(std::shared_ptr<LinuxTest> &moc
.WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(apiReadLeftData)), Return(LEFT_DATA_LENGTH))) .WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(apiReadLeftData)), Return(LEFT_DATA_LENGTH)))
.WillRepeatedly(DoAll(Return(UART_DEVICE_READ_NOTHING))); .WillRepeatedly(DoAll(Return(UART_DEVICE_READ_NOTHING)));
} }
void McuProtocolTestTool::CutOffPowerSupplyProtocolHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd, bool McuProtocolTestTool::CutOffPowerSupplyProtocolHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const void *buf, size_t count) const void *buf, size_t count)
{ {
if (sizeof(ASK_CUT_OFF_POWER_SUPPLY) == count && if (sizeof(ASK_CUT_OFF_POWER_SUPPLY) == count &&
@ -198,9 +209,11 @@ void McuProtocolTestTool::CutOffPowerSupplyProtocolHandle(std::shared_ptr<LinuxT
memcmp((unsigned char *)ASK_CUT_OFF_POWER_SUPPLY + count - PROTOCOL_CHECK_CODE_LENGTH, &askCheckCode, 2), memcmp((unsigned char *)ASK_CUT_OFF_POWER_SUPPLY + count - PROTOCOL_CHECK_CODE_LENGTH, &askCheckCode, 2),
0); 0);
// IpcMissionProtocolInit(mock, uartFd); // IpcMissionProtocolInit(mock, uartFd);
return PROTOCOL_HANDLED;
} }
return PROTOCOL_NOT_HANDLED;
} }
void McuProtocolTestTool::FeedWatchDogProtocolHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd, bool McuProtocolTestTool::FeedWatchDogProtocolHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const void *buf, size_t count) const void *buf, size_t count)
{ {
if (sizeof(ASK_FEED_WATCH_DOG) == count && if (sizeof(ASK_FEED_WATCH_DOG) == count &&
@ -212,9 +225,11 @@ void McuProtocolTestTool::FeedWatchDogProtocolHandle(std::shared_ptr<LinuxTest>
EXPECT_EQ(memcmp((unsigned char *)ASK_FEED_WATCH_DOG + count - PROTOCOL_CHECK_CODE_LENGTH, &askCheckCode, 2), EXPECT_EQ(memcmp((unsigned char *)ASK_FEED_WATCH_DOG + count - PROTOCOL_CHECK_CODE_LENGTH, &askCheckCode, 2),
0); 0);
// IpcMissionProtocolInit(mock, uartFd); // IpcMissionProtocolInit(mock, uartFd);
return PROTOCOL_HANDLED;
} }
return PROTOCOL_NOT_HANDLED;
} }
void McuProtocolTestTool::FeedingCycleProtocolHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd, bool McuProtocolTestTool::FeedingCycleProtocolHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const void *buf, size_t count) const void *buf, size_t count)
{ {
if (sizeof(ASK_SET_FEEDING_CYCLE) == count && if (sizeof(ASK_SET_FEEDING_CYCLE) == count &&
@ -237,7 +252,9 @@ void McuProtocolTestTool::FeedingCycleProtocolHandle(std::shared_ptr<LinuxTest>
} }
mLockThread = std::thread(handle, this); mLockThread = std::thread(handle, this);
// FeedingCycleProtocolInit(mock, uartFd); // FeedingCycleProtocolInit(mock, uartFd);
return PROTOCOL_HANDLED;
} }
return PROTOCOL_NOT_HANDLED;
} }
void McuProtocolTestTool::FeedingCycleProtocolInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf, void McuProtocolTestTool::FeedingCycleProtocolInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf,
size_t count) size_t count)