From 67993b88ca14cb516717a01f7a87f7e43b58cd48 Mon Sep 17 00:00:00 2001 From: Fancy code <258828110.@qq.com> Date: Mon, 12 Feb 2024 18:42:46 -0800 Subject: [PATCH] Improve:test code. --- middleware/McuManager/src/McuManagerImpl.cpp | 6 +- middleware/McuManager/src/McuManagerImpl.h | 2 +- test/utils/McuProtocol/tool/CMakeLists.txt | 1 + .../tool/src/McuProtocolTestTool.cpp | 152 ++++++++++-------- .../McuProtocol/tool/src/ProtocolMonitor.cpp | 19 +++ .../McuProtocol/tool/src/ProtocolMonitor.h | 35 ++++ utils/McuProtocol/include/McuProtocol.h | 2 +- utils/McuProtocol/src/McuProtocol.cpp | 3 +- utils/McuProtocol/src/ProtocolHandle.cpp | 13 +- utils/McuProtocol/src/ProtocolHandle.h | 8 +- 10 files changed, 163 insertions(+), 78 deletions(-) diff --git a/middleware/McuManager/src/McuManagerImpl.cpp b/middleware/McuManager/src/McuManagerImpl.cpp index 604f3de..f9af197 100644 --- a/middleware/McuManager/src/McuManagerImpl.cpp +++ b/middleware/McuManager/src/McuManagerImpl.cpp @@ -92,7 +92,7 @@ void McuManagerImpl::OtherSideSendIpcMission(const unsigned int &serialNumber, c void ReplyFinished(const bool result) override { if (result) { - mMcuManager->ReplyOtherSideSendIpcMission(mDataReply); + mMcuManager->ReplyOtherSideSendIpcMission(mDataReply, mSerialNumber); } } std::shared_ptr mMcuManager; @@ -104,8 +104,8 @@ void McuManagerImpl::OtherSideSendIpcMission(const unsigned int &serialNumber, c monitor->RecvIpcMissionEvent(ask, static_cast(mission)); } } -void McuManagerImpl::ReplyOtherSideSendIpcMission(const ASK_RESULT &result) +void McuManagerImpl::ReplyOtherSideSendIpcMission(const ASK_RESULT &result, const unsigned int &serialNumber) { LogInfo("ReplyOtherSideSendIpcMission\n"); - return McuProtocol::ReplyOtherSideSendIpcMission(static_cast(result)); + return McuProtocol::ReplyOtherSideSendIpcMission(static_cast(result), serialNumber); } \ No newline at end of file diff --git a/middleware/McuManager/src/McuManagerImpl.h b/middleware/McuManager/src/McuManagerImpl.h index 0c60c6b..3f77b0b 100644 --- a/middleware/McuManager/src/McuManagerImpl.h +++ b/middleware/McuManager/src/McuManagerImpl.h @@ -39,7 +39,7 @@ private: private: void OtherSideSendIpcMission(const unsigned int &serialNumber, const unsigned char &mission) override; - void ReplyOtherSideSendIpcMission(const ASK_RESULT &result); + void ReplyOtherSideSendIpcMission(const ASK_RESULT &result, const unsigned int &serialNumber); private: std::weak_ptr mMonitor; diff --git a/test/utils/McuProtocol/tool/CMakeLists.txt b/test/utils/McuProtocol/tool/CMakeLists.txt index dadac4b..06461f9 100644 --- a/test/utils/McuProtocol/tool/CMakeLists.txt +++ b/test/utils/McuProtocol/tool/CMakeLists.txt @@ -9,6 +9,7 @@ include_directories( ${UTILS_SOURCE_PATH}/Log/include ${UTILS_SOURCE_PATH}/UartDevice/include ${UTILS_SOURCE_PATH}/ModBusCRC16/include + ${UTILS_SOURCE_PATH}/McuProtocol/src ${TEST_SOURCE_PATH}/utils/LinuxApiMock/include ${TEST_SOURCE_PATH}/utils/UartDevice/tool/include ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include diff --git a/test/utils/McuProtocol/tool/src/McuProtocolTestTool.cpp b/test/utils/McuProtocol/tool/src/McuProtocolTestTool.cpp index 5f5f8a2..ae7875d 100644 --- a/test/utils/McuProtocol/tool/src/McuProtocolTestTool.cpp +++ b/test/utils/McuProtocol/tool/src/McuProtocolTestTool.cpp @@ -15,6 +15,7 @@ #include "McuProtocolTestTool.h" #include "ILog.h" #include "ModBusCRC16.h" +#include "ProtocolHandle.h" #include "ProtocolMonitor.h" #include #include @@ -38,24 +39,24 @@ constexpr size_t PROTOCOL_DATA_LENGTH_OFFSET = PROTOCOL_COMMAND_OFFSET + PROTOCO constexpr size_t PROTOCOL_CHECK_CODE_LENGTH = sizeof(short); constexpr size_t PROTOCOL_DATA_KEY_HEAD_LENGTH = PROTOCOL_HEAD_LENGTH + PROTOCOL_SERIAL_NUMBER_LENGTH + PROTOCOL_COMMAND_LENGTH + PROTOCOL_DATA_LENGTH_LENGTH; -unsigned char ASK_IPC_MISSION[] = {0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x81, 0x01, 0x00, 0x0C, 0xFF, 0xFF}; -unsigned char REPLY_IPC_MISSION[] = {0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x0D, 0x01, 0xFF, 0xFF}; -unsigned char ASK_CUT_OFF_POWER_SUPPLY[] = {0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x81, 0x02, 0x00, 0x0C, 0xFF, 0xFF}; -unsigned char ASK_FEED_WATCH_DOG[] = {0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x81, 0x03, 0x00, 0x0C, 0xFF, 0xFF}; -unsigned char ASK_SET_FEEDING_CYCLE[] = { +unsigned char ASK_IPC_MISSION_X[] = {0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x81, 0x01, 0x00, 0x0C, 0xFF, 0xFF}; +unsigned char REPLY_IPC_MISSION_X[] = {0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x0D, 0x01, 0xFF, 0xFF}; +unsigned char ASK_CUT_OFF_POWER_SUPPLY_X[] = {0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x81, 0x02, 0x00, 0x0C, 0xFF, 0xFF}; +unsigned char ASK_FEED_WATCH_DOG_X[] = {0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x81, 0x03, 0x00, 0x0C, 0xFF, 0xFF}; +unsigned char ASK_SET_FEEDING_CYCLE_X[] = { 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x81, 0x04, 0x00, 0x0F, 0x01, 0x01, 0x01, 0xFF, 0xFF}; -unsigned char REPLY_SET_FEEDING_CYCLE[] = { +unsigned char REPLY_SET_FEEDING_CYCLE_X[] = { 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x0D, 0x01, 0xFF, 0xFF}; -unsigned char ASK_SET_DATE_TIME[] = { +unsigned char ASK_SET_DATE_TIME_X[] = { 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x01, 0x81, 0x07, 0x00, 0x13, 0xDE, 0x07, 0x01, 0x0F, 0x00, 0x00, 0x00, 0xFF, 0xFF}; -unsigned char REPLY_SET_DATE_TIME[] = {0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x00, 0x0D, 0x01, 0xFF, 0xFF}; -unsigned char ASK_SET_PIR_SENSITIVITY[] = { +unsigned char REPLY_SET_DATE_TIME_X[] = {0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x01, 0x07, 0x00, 0x0D, 0x01, 0xFF, 0xFF}; +unsigned char ASK_SET_PIR_SENSITIVITY_X[] = { 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x01, 0x81, 0x08, 0x00, 0x0D, 0x09, 0xFF, 0xFF}; -unsigned char REPLY_SET_PIR_SENSITIVITY[] = { +unsigned char REPLY_SET_PIR_SENSITIVITY_X[] = { 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x00, 0x0D, 0x01, 0xFF, 0xFF}; -unsigned char REPLY_OTHER_SIDE_ASK_SEND_IPC_MISSION[] = { +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[] = { +unsigned char OTHER_SIDE_ASK_SEND_IPC_MISSION_X[] = { 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x41, 0x01, 0x00, 0x0D, 0x01, 0xFF, 0xFF}; McuProtocolTestTool::McuProtocolTestTool() { @@ -75,6 +76,8 @@ McuProtocolTestTool::McuProtocolTestTool() void McuProtocolTestTool::Init(std::shared_ptr &mock, const UartInfo &uart) { std::shared_ptr test = std::make_shared(); + std::shared_ptr tmp = std::dynamic_pointer_cast(test); + ProtocolMonitorTest::Init(tmp); ProtocolMonitor::GetInstance(&test); if (pipe(mPipeFdMockSelect) == 0) { mPipeFdMockSelectInit = true; @@ -199,16 +202,20 @@ bool McuProtocolTestTool::MonitorProtocolPacket(std::shared_ptr &mock memcpy(&command, (unsigned char *)buf + PROTOCOL_COMMAND_OFFSET, PROTOCOL_COMMAND_LENGTH); memcpy(&packetLength, (unsigned char *)buf + PROTOCOL_DATA_LENGTH_OFFSET, PROTOCOL_DATA_LENGTH_LENGTH); data = (unsigned char *)buf + PROTOCOL_DATA_KEY_HEAD_LENGTH; - // ProtocolMonitor::GetInstance()->MonitorWriteProtocolData(head, serialNumber, command, data, packetLength); + head = ntohs(head); + serialNumber = ntohl(serialNumber); + command = ntohs(command); + packetLength = ntohs(packetLength); + ProtocolMonitor::GetInstance()->MonitorWriteProtocolData(head, serialNumber, command, data, packetLength); return PROTOCOL_NOT_HANDLED; } bool McuProtocolTestTool::IpcMissionProtocolHandle(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count) { - if (sizeof(ASK_IPC_MISSION) == count && memcmp(ASK_IPC_MISSION + PROTOCOL_COMMAND_OFFSET, - (unsigned char *)buf + PROTOCOL_COMMAND_OFFSET, - PROTOCOL_COMMAND_LENGTH) == 0) { - LogInfo("Set REPLY_IPC_MISSION\n"); + if (sizeof(ASK_IPC_MISSION_X) == count && memcmp(ASK_IPC_MISSION_X + PROTOCOL_COMMAND_OFFSET, + (unsigned char *)buf + PROTOCOL_COMMAND_OFFSET, + PROTOCOL_COMMAND_LENGTH) == 0) { + LogInfo("Set REPLY_IPC_MISSION_X\n"); short askCheckCode = calculate_check_sum((unsigned char *)buf, count - PROTOCOL_CHECK_CODE_LENGTH); // askCheckCode = htons(askCheckCode); int result = memcmp( @@ -229,18 +236,18 @@ void McuProtocolTestTool::IpcMissionProtocolInit(std::shared_ptr &moc size_t count) { LockProtocolHandle(); - memcpy(REPLY_IPC_MISSION + PROTOCOL_SERIAL_NUMBER_OFFSET, + memcpy(REPLY_IPC_MISSION_X + PROTOCOL_SERIAL_NUMBER_OFFSET, (unsigned char *)buf + PROTOCOL_SERIAL_NUMBER_OFFSET, PROTOCOL_SERIAL_NUMBER_LENGTH); - ResetCheckCode(REPLY_IPC_MISSION, sizeof(REPLY_IPC_MISSION)); + ResetCheckCode(REPLY_IPC_MISSION_X, sizeof(REPLY_IPC_MISSION_X)); ReplySelectSucceed(mock, uartFd); - constexpr int LEFT_DATA_LENGTH = sizeof(REPLY_IPC_MISSION) - PROTOCOL_DATA_KEY_HEAD_LENGTH; + constexpr int LEFT_DATA_LENGTH = sizeof(REPLY_IPC_MISSION_X) - PROTOCOL_DATA_KEY_HEAD_LENGTH; auto apiReadKeyHead = [=](int fd, void *buf, size_t count) { - memcpy(buf, REPLY_IPC_MISSION, PROTOCOL_DATA_KEY_HEAD_LENGTH); + memcpy(buf, REPLY_IPC_MISSION_X, PROTOCOL_DATA_KEY_HEAD_LENGTH); McuProtocolTestTool::PrintHexadecimalData(buf, PROTOCOL_DATA_KEY_HEAD_LENGTH, READ_PRINT); }; auto apiReadLeftData = [=](int fd, void *buf, size_t count) { - memcpy(buf, REPLY_IPC_MISSION + PROTOCOL_DATA_KEY_HEAD_LENGTH, LEFT_DATA_LENGTH); + memcpy(buf, REPLY_IPC_MISSION_X + PROTOCOL_DATA_KEY_HEAD_LENGTH, LEFT_DATA_LENGTH); McuProtocolTestTool::PrintHexadecimalData(buf, LEFT_DATA_LENGTH, READ_PRINT); UnlockProtocolHandle(); }; @@ -252,10 +259,10 @@ void McuProtocolTestTool::IpcMissionProtocolInit(std::shared_ptr &moc bool McuProtocolTestTool::CutOffPowerSupplyProtocolHandle(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count) { - if (sizeof(ASK_CUT_OFF_POWER_SUPPLY) == count && memcmp(ASK_CUT_OFF_POWER_SUPPLY + PROTOCOL_COMMAND_OFFSET, - (unsigned char *)buf + PROTOCOL_COMMAND_OFFSET, - PROTOCOL_COMMAND_LENGTH) == 0) { - LogInfo("Set ASK_CUT_OFF_POWER_SUPPLY\n"); + if (sizeof(ASK_CUT_OFF_POWER_SUPPLY_X) == count && memcmp(ASK_CUT_OFF_POWER_SUPPLY_X + PROTOCOL_COMMAND_OFFSET, + (unsigned char *)buf + PROTOCOL_COMMAND_OFFSET, + PROTOCOL_COMMAND_LENGTH) == 0) { + LogInfo("Set ASK_CUT_OFF_POWER_SUPPLY_X\n"); short askCheckCode = calculate_check_sum((unsigned char *)buf, count - PROTOCOL_CHECK_CODE_LENGTH); // askCheckCode = htons(askCheckCode); int result = memcmp( @@ -269,10 +276,10 @@ bool McuProtocolTestTool::CutOffPowerSupplyProtocolHandle(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count) { - if (sizeof(ASK_FEED_WATCH_DOG) == count && memcmp(ASK_FEED_WATCH_DOG + PROTOCOL_COMMAND_OFFSET, - (unsigned char *)buf + PROTOCOL_COMMAND_OFFSET, - PROTOCOL_COMMAND_LENGTH) == 0) { - LogInfo("Set ASK_FEED_WATCH_DOG\n"); + if (sizeof(ASK_FEED_WATCH_DOG_X) == count && memcmp(ASK_FEED_WATCH_DOG_X + PROTOCOL_COMMAND_OFFSET, + (unsigned char *)buf + PROTOCOL_COMMAND_OFFSET, + PROTOCOL_COMMAND_LENGTH) == 0) { + LogInfo("Set ASK_FEED_WATCH_DOG_X\n"); short askCheckCode = calculate_check_sum((unsigned char *)buf, count - PROTOCOL_CHECK_CODE_LENGTH); // askCheckCode = htons(askCheckCode); int result = memcmp( @@ -286,13 +293,13 @@ bool McuProtocolTestTool::FeedWatchDogProtocolHandle(std::shared_ptr bool McuProtocolTestTool::FeedingCycleProtocolHandle(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count) { - if (sizeof(ASK_SET_FEEDING_CYCLE) == count && memcmp(ASK_SET_FEEDING_CYCLE + PROTOCOL_COMMAND_OFFSET, - (unsigned char *)buf + PROTOCOL_COMMAND_OFFSET, - PROTOCOL_COMMAND_LENGTH) == 0) { - short replyCheckCode = - calculate_check_sum(REPLY_SET_FEEDING_CYCLE, sizeof(REPLY_SET_FEEDING_CYCLE) - PROTOCOL_CHECK_CODE_LENGTH); + if (sizeof(ASK_SET_FEEDING_CYCLE_X) == count && memcmp(ASK_SET_FEEDING_CYCLE_X + PROTOCOL_COMMAND_OFFSET, + (unsigned char *)buf + PROTOCOL_COMMAND_OFFSET, + PROTOCOL_COMMAND_LENGTH) == 0) { + short replyCheckCode = calculate_check_sum(REPLY_SET_FEEDING_CYCLE_X, + sizeof(REPLY_SET_FEEDING_CYCLE_X) - PROTOCOL_CHECK_CODE_LENGTH); replyCheckCode = htons(replyCheckCode); - LogInfo("Set ASK_SET_FEEDING_CYCLE, reply data check code = 0x%x\n", replyCheckCode); + LogInfo("Set ASK_SET_FEEDING_CYCLE_X, reply data check code = 0x%x\n", replyCheckCode); short askCheckCode = calculate_check_sum((unsigned char *)buf, count - PROTOCOL_CHECK_CODE_LENGTH); // askCheckCode = htons(askCheckCode); int result = memcmp( @@ -314,18 +321,18 @@ void McuProtocolTestTool::FeedingCycleProtocolInit(std::shared_ptr &m size_t count) { LockProtocolHandle(); - memcpy(REPLY_SET_FEEDING_CYCLE + PROTOCOL_SERIAL_NUMBER_OFFSET, + memcpy(REPLY_SET_FEEDING_CYCLE_X + PROTOCOL_SERIAL_NUMBER_OFFSET, (unsigned char *)buf + PROTOCOL_SERIAL_NUMBER_OFFSET, PROTOCOL_SERIAL_NUMBER_LENGTH); - ResetCheckCode(REPLY_SET_FEEDING_CYCLE, sizeof(REPLY_SET_FEEDING_CYCLE)); + ResetCheckCode(REPLY_SET_FEEDING_CYCLE_X, sizeof(REPLY_SET_FEEDING_CYCLE_X)); ReplySelectSucceed(mock, uartFd); - constexpr int LEFT_DATA_LENGTH = sizeof(REPLY_SET_FEEDING_CYCLE) - PROTOCOL_DATA_KEY_HEAD_LENGTH; + constexpr int LEFT_DATA_LENGTH = sizeof(REPLY_SET_FEEDING_CYCLE_X) - PROTOCOL_DATA_KEY_HEAD_LENGTH; auto apiReadKeyHead = [=](int fd, void *buf, size_t count) { - memcpy(buf, REPLY_SET_FEEDING_CYCLE, PROTOCOL_DATA_KEY_HEAD_LENGTH); + memcpy(buf, REPLY_SET_FEEDING_CYCLE_X, PROTOCOL_DATA_KEY_HEAD_LENGTH); McuProtocolTestTool::PrintHexadecimalData(buf, PROTOCOL_DATA_KEY_HEAD_LENGTH, READ_PRINT); }; auto apiReadLeftData = [=](int fd, void *buf, size_t count) { - memcpy(buf, REPLY_SET_FEEDING_CYCLE + PROTOCOL_DATA_KEY_HEAD_LENGTH, LEFT_DATA_LENGTH); + memcpy(buf, REPLY_SET_FEEDING_CYCLE_X + PROTOCOL_DATA_KEY_HEAD_LENGTH, LEFT_DATA_LENGTH); McuProtocolTestTool::PrintHexadecimalData(buf, LEFT_DATA_LENGTH, READ_PRINT); UnlockProtocolHandle(); }; @@ -337,13 +344,13 @@ void McuProtocolTestTool::FeedingCycleProtocolInit(std::shared_ptr &m bool McuProtocolTestTool::SetDataTimeProtocolHandle(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count) { - if (sizeof(ASK_SET_DATE_TIME) == count && memcmp(ASK_SET_DATE_TIME + PROTOCOL_COMMAND_OFFSET, - (unsigned char *)buf + PROTOCOL_COMMAND_OFFSET, - PROTOCOL_COMMAND_LENGTH) == 0) { + if (sizeof(ASK_SET_DATE_TIME_X) == count && memcmp(ASK_SET_DATE_TIME_X + PROTOCOL_COMMAND_OFFSET, + (unsigned char *)buf + PROTOCOL_COMMAND_OFFSET, + PROTOCOL_COMMAND_LENGTH) == 0) { short replyCheckCode = - calculate_check_sum(REPLY_SET_DATE_TIME, sizeof(REPLY_SET_DATE_TIME) - PROTOCOL_CHECK_CODE_LENGTH); + calculate_check_sum(REPLY_SET_DATE_TIME_X, sizeof(REPLY_SET_DATE_TIME_X) - PROTOCOL_CHECK_CODE_LENGTH); replyCheckCode = htons(replyCheckCode); - LogInfo("Set ASK_SET_DATE_TIME, reply data check code = 0x%x\n", replyCheckCode); + LogInfo("Set ASK_SET_DATE_TIME_X, reply data check code = 0x%x\n", replyCheckCode); short askCheckCode = calculate_check_sum((unsigned char *)buf, count - PROTOCOL_CHECK_CODE_LENGTH); // askCheckCode = htons(askCheckCode); int result = memcmp( @@ -364,18 +371,18 @@ void McuProtocolTestTool::SetDataTimeProtocolInit(std::shared_ptr &mo size_t count) { LockProtocolHandle(); - memcpy(REPLY_SET_DATE_TIME + PROTOCOL_SERIAL_NUMBER_OFFSET, + memcpy(REPLY_SET_DATE_TIME_X + PROTOCOL_SERIAL_NUMBER_OFFSET, (unsigned char *)buf + PROTOCOL_SERIAL_NUMBER_OFFSET, PROTOCOL_SERIAL_NUMBER_LENGTH); - ResetCheckCode(REPLY_SET_DATE_TIME, sizeof(REPLY_SET_DATE_TIME)); + ResetCheckCode(REPLY_SET_DATE_TIME_X, sizeof(REPLY_SET_DATE_TIME_X)); ReplySelectSucceed(mock, uartFd); - constexpr int LEFT_DATA_LENGTH = sizeof(REPLY_SET_DATE_TIME) - PROTOCOL_DATA_KEY_HEAD_LENGTH; + constexpr int LEFT_DATA_LENGTH = sizeof(REPLY_SET_DATE_TIME_X) - PROTOCOL_DATA_KEY_HEAD_LENGTH; auto apiReadKeyHead = [=](int fd, void *buf, size_t count) { - memcpy(buf, REPLY_SET_DATE_TIME, PROTOCOL_DATA_KEY_HEAD_LENGTH); + memcpy(buf, REPLY_SET_DATE_TIME_X, PROTOCOL_DATA_KEY_HEAD_LENGTH); McuProtocolTestTool::PrintHexadecimalData(buf, PROTOCOL_DATA_KEY_HEAD_LENGTH, READ_PRINT); }; auto apiReadLeftData = [=](int fd, void *buf, size_t count) { - memcpy(buf, REPLY_SET_DATE_TIME + PROTOCOL_DATA_KEY_HEAD_LENGTH, LEFT_DATA_LENGTH); + memcpy(buf, REPLY_SET_DATE_TIME_X + PROTOCOL_DATA_KEY_HEAD_LENGTH, LEFT_DATA_LENGTH); McuProtocolTestTool::PrintHexadecimalData(buf, LEFT_DATA_LENGTH, READ_PRINT); UnlockProtocolHandle(); }; @@ -387,13 +394,13 @@ void McuProtocolTestTool::SetDataTimeProtocolInit(std::shared_ptr &mo bool McuProtocolTestTool::SetPirSensitivityProtocolHandle(std::shared_ptr &mock, const int &uartFd, const void *buf, size_t count) { - if (sizeof(ASK_SET_PIR_SENSITIVITY) == count && memcmp(ASK_SET_PIR_SENSITIVITY + PROTOCOL_COMMAND_OFFSET, - (unsigned char *)buf + PROTOCOL_COMMAND_OFFSET, - PROTOCOL_COMMAND_LENGTH) == 0) { - short replyCheckCode = calculate_check_sum(REPLY_SET_PIR_SENSITIVITY, - sizeof(REPLY_SET_PIR_SENSITIVITY) - PROTOCOL_CHECK_CODE_LENGTH); + if (sizeof(ASK_SET_PIR_SENSITIVITY_X) == count && memcmp(ASK_SET_PIR_SENSITIVITY_X + PROTOCOL_COMMAND_OFFSET, + (unsigned char *)buf + PROTOCOL_COMMAND_OFFSET, + PROTOCOL_COMMAND_LENGTH) == 0) { + short replyCheckCode = calculate_check_sum(REPLY_SET_PIR_SENSITIVITY_X, + sizeof(REPLY_SET_PIR_SENSITIVITY_X) - PROTOCOL_CHECK_CODE_LENGTH); replyCheckCode = htons(replyCheckCode); - LogInfo("Set ASK_SET_PIR_SENSITIVITY, reply data check code = 0x%x\n", replyCheckCode); + LogInfo("Set ASK_SET_PIR_SENSITIVITY_X, reply data check code = 0x%x\n", replyCheckCode); short askCheckCode = calculate_check_sum((unsigned char *)buf, count - PROTOCOL_CHECK_CODE_LENGTH); // askCheckCode = htons(askCheckCode); int result = memcmp( @@ -414,18 +421,18 @@ void McuProtocolTestTool::SetPirSensitivityProtocolInit(std::shared_ptr LockProtocolHandle(); unsigned int serialNum = serialNumber; serialNum = htonl(serialNum); - memcpy(OTHER_SIDE_ASK_SEND_IPC_MISSION + PROTOCOL_SERIAL_NUMBER_OFFSET, &serialNum, PROTOCOL_SERIAL_NUMBER_LENGTH); - ResetCheckCode(OTHER_SIDE_ASK_SEND_IPC_MISSION, sizeof(OTHER_SIDE_ASK_SEND_IPC_MISSION)); + memcpy( + OTHER_SIDE_ASK_SEND_IPC_MISSION_X + PROTOCOL_SERIAL_NUMBER_OFFSET, &serialNum, PROTOCOL_SERIAL_NUMBER_LENGTH); + ResetCheckCode(OTHER_SIDE_ASK_SEND_IPC_MISSION_X, sizeof(OTHER_SIDE_ASK_SEND_IPC_MISSION_X)); ReplySelectSucceed(mock, uartFd); - constexpr int LEFT_DATA_LENGTH = sizeof(OTHER_SIDE_ASK_SEND_IPC_MISSION) - PROTOCOL_DATA_KEY_HEAD_LENGTH; + constexpr int LEFT_DATA_LENGTH = sizeof(OTHER_SIDE_ASK_SEND_IPC_MISSION_X) - PROTOCOL_DATA_KEY_HEAD_LENGTH; auto apiReadKeyHead = [=](int fd, void *buf, size_t count) { - memcpy(buf, OTHER_SIDE_ASK_SEND_IPC_MISSION, PROTOCOL_DATA_KEY_HEAD_LENGTH); + memcpy(buf, OTHER_SIDE_ASK_SEND_IPC_MISSION_X, PROTOCOL_DATA_KEY_HEAD_LENGTH); McuProtocolTestTool::PrintHexadecimalData(buf, PROTOCOL_DATA_KEY_HEAD_LENGTH, READ_PRINT); }; auto apiReadLeftData = [=](int fd, void *buf, size_t count) { - memcpy(buf, OTHER_SIDE_ASK_SEND_IPC_MISSION + PROTOCOL_DATA_KEY_HEAD_LENGTH, LEFT_DATA_LENGTH); + memcpy(buf, OTHER_SIDE_ASK_SEND_IPC_MISSION_X + PROTOCOL_DATA_KEY_HEAD_LENGTH, LEFT_DATA_LENGTH); McuProtocolTestTool::PrintHexadecimalData(buf, LEFT_DATA_LENGTH, READ_PRINT); UnlockProtocolHandle(); }; @@ -501,6 +509,16 @@ void McuProtocolTestTool::OtherSideAskIpcMissionInit(std::shared_ptr .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_IPC_MISSION, + nullptr, + sizeof(OTHER_SIDE_ASK_SEND_IPC_MISSION_X)); + } } void McuProtocolTestTool::PrintHexadecimalData(const void *buf, const size_t &bufLength, const int event) { diff --git a/test/utils/McuProtocol/tool/src/ProtocolMonitor.cpp b/test/utils/McuProtocol/tool/src/ProtocolMonitor.cpp index d9fa1f4..b6bd886 100644 --- a/test/utils/McuProtocol/tool/src/ProtocolMonitor.cpp +++ b/test/utils/McuProtocol/tool/src/ProtocolMonitor.cpp @@ -27,4 +27,23 @@ std::shared_ptr &ProtocolMonitor::GetInstance(std::shared_ptr

&test) +{ + EXPECT_CALL(*test.get(), MonitorWriteProtocolData(_, _, _, _, _)).WillRepeatedly(DoAll(Return())); +} +void ProtocolMonitorTest::WriteDataOnce(std::shared_ptr &test, const short &head, + const unsigned int &serialNumber, const short &command, const void *data, + const short &packetLength) +{ + auto printfParam = [=](const short &head, + const unsigned int &serialNumber, + const short &command, + const void *data, + const short &packetLength) { + // + }; + EXPECT_CALL(*test.get(), MonitorWriteProtocolData(head, serialNumber, command, _, packetLength)) + .Times(1) + .WillOnce(DoAll(WithArgs<0, 1, 2, 3, 4>(Invoke(printfParam)), Return())); } \ No newline at end of file diff --git a/test/utils/McuProtocol/tool/src/ProtocolMonitor.h b/test/utils/McuProtocol/tool/src/ProtocolMonitor.h index 02d71dd..73ce399 100644 --- a/test/utils/McuProtocol/tool/src/ProtocolMonitor.h +++ b/test/utils/McuProtocol/tool/src/ProtocolMonitor.h @@ -17,6 +17,35 @@ #include #include #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: @@ -35,5 +64,11 @@ public: virtual ~ProtocolMonitorTest() = default; MOCK_METHOD5(MonitorWriteProtocolData, void(const short &, const unsigned int &, const short &, const void *, const short &)); + +public: + static void Init(std::shared_ptr &test); + static void WriteDataOnce(std::shared_ptr &test, const short &head, + const unsigned int &serialNumber, const short &command, const void *data, + const short &packetLength); }; #endif \ No newline at end of file diff --git a/utils/McuProtocol/include/McuProtocol.h b/utils/McuProtocol/include/McuProtocol.h index acd5f2d..51e4d27 100644 --- a/utils/McuProtocol/include/McuProtocol.h +++ b/utils/McuProtocol/include/McuProtocol.h @@ -125,7 +125,7 @@ protected: void DataHandleThread(void); protected: - void ReplyOtherSideSendIpcMission(const ReplyResult &result); + void ReplyOtherSideSendIpcMission(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 8330550..41b89ee 100644 --- a/utils/McuProtocol/src/McuProtocol.cpp +++ b/utils/McuProtocol/src/McuProtocol.cpp @@ -126,11 +126,12 @@ void McuProtocol::DataHandleThread(void) free((void *)packet.mBuf); } } -void McuProtocol::ReplyOtherSideSendIpcMission(const ReplyResult &result) +void McuProtocol::ReplyOtherSideSendIpcMission(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_IPC_MISSION, static_cast(result)); + param->mSerialNumber = serialNumber; std::shared_ptr handle = ProtocolHandle::CreateProtocolData(param); WriteProtocolData( handle->GetProtocolDataBuff(), handle->GetProtocolDataLength(), NULL_CONTEXT, handle->GetSerialNumber()); diff --git a/utils/McuProtocol/src/ProtocolHandle.cpp b/utils/McuProtocol/src/ProtocolHandle.cpp index b0c438c..ce455f4 100644 --- a/utils/McuProtocol/src/ProtocolHandle.cpp +++ b/utils/McuProtocol/src/ProtocolHandle.cpp @@ -25,7 +25,6 @@ using std::placeholders::_1; // using std::placeholders::_4; unsigned int ProtocolHandle::mSerialNumber = 1; std::mutex ProtocolHandle::mMutex; -constexpr unsigned short PROTOCOL_HEAD = 0xFAC1; ProtocolHandle::ProtocolHandle(const std::shared_ptr ¶m) : mParam(param) { mProtocolData = nullptr; @@ -65,9 +64,14 @@ ProtocolPacket ProtocolHandle::CreatePocketWithSerialNumber(void) std::lock_guard locker(mMutex); ProtocolPacket packet; memset(&packet, 0, sizeof(ProtocolPacket)); - packet.mSerialNumber = mSerialNumber; - mProtocolSerialNumber = packet.mSerialNumber; - mSerialNumber++; + if (SERIAL_NUMBER_NEED_TO_MAKE_A_LOCAL_ONE == mProtocolSerialNumber) { + packet.mSerialNumber = mSerialNumber; + mProtocolSerialNumber = packet.mSerialNumber; + mSerialNumber++; + } + else { + packet.mSerialNumber = mProtocolSerialNumber; + } return packet; } void ProtocolHandle::MallocPacketDataBuff(const void *data, const size_t dataLength, const short &command) @@ -96,6 +100,7 @@ void ProtocolHandle::MakeProtocolPacket(const std::shared_ptr &p LogError("Something wrong happened, make packet failed.\n"); return; } + mProtocolSerialNumber = param->mSerialNumber; std::map::iterator iter; iter = mMakePacketFunc.find(param->mCommand); if (iter != mMakePacketFunc.end()) { diff --git a/utils/McuProtocol/src/ProtocolHandle.h b/utils/McuProtocol/src/ProtocolHandle.h index bef97da..5cb1fac 100644 --- a/utils/McuProtocol/src/ProtocolHandle.h +++ b/utils/McuProtocol/src/ProtocolHandle.h @@ -24,6 +24,7 @@ constexpr char ORDER_BIG_ENDIAN = 0x01; constexpr char ORDER_LITTLE_ENDIAN = 0x02; constexpr size_t KEY_HEAD_LENGTH = sizeof(short) + sizeof(unsigned int) + sizeof(short) + sizeof(short); constexpr size_t CHECK_CODE_LENGTH = sizeof(short); +constexpr unsigned int SERIAL_NUMBER_NEED_TO_MAKE_A_LOCAL_ONE = 0; #pragma pack(1) typedef struct protocol_packet { @@ -35,6 +36,7 @@ typedef struct protocol_packet short mCheckCode; } ProtocolPacket; #pragma pack() +constexpr unsigned short PROTOCOL_HEAD = 0xFAC1; enum PROTOCOL_COMMAND { ASK_IPC_MISSION = 0x8101, @@ -85,9 +87,13 @@ typedef struct set_date_time class VProtocolParam { public: - VProtocolParam(const PROTOCOL_COMMAND &command) : mCommand(command) {} + VProtocolParam(const PROTOCOL_COMMAND &command) : mCommand(command) + { + mSerialNumber = SERIAL_NUMBER_NEED_TO_MAKE_A_LOCAL_ONE; + } virtual ~VProtocolParam() = default; const PROTOCOL_COMMAND mCommand; + unsigned int mSerialNumber; }; template class ProtocolParam : public VProtocolParam