Improve:test code.

This commit is contained in:
Fancy code 2024-02-12 18:42:46 -08:00
parent 9ca156dcb3
commit 67993b88ca
10 changed files with 163 additions and 78 deletions

View File

@ -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<McuManagerImpl> mMcuManager;
@ -104,8 +104,8 @@ void McuManagerImpl::OtherSideSendIpcMission(const unsigned int &serialNumber, c
monitor->RecvIpcMissionEvent(ask, static_cast<IpcMission>(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<ReplyResult>(result));
return McuProtocol::ReplyOtherSideSendIpcMission(static_cast<ReplyResult>(result), serialNumber);
}

View File

@ -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<VMcuMonitor> mMonitor;

View File

@ -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

View File

@ -15,6 +15,7 @@
#include "McuProtocolTestTool.h"
#include "ILog.h"
#include "ModBusCRC16.h"
#include "ProtocolHandle.h"
#include "ProtocolMonitor.h"
#include <netinet/in.h>
#include <string.h>
@ -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<LinuxTest> &mock, const UartInfo &uart)
{
std::shared_ptr<ProtocolMonitor> test = std::make_shared<ProtocolMonitorTest>();
std::shared_ptr<ProtocolMonitorTest> tmp = std::dynamic_pointer_cast<ProtocolMonitorTest>(test);
ProtocolMonitorTest::Init(tmp);
ProtocolMonitor::GetInstance(&test);
if (pipe(mPipeFdMockSelect) == 0) {
mPipeFdMockSelectInit = true;
@ -199,16 +202,20 @@ bool McuProtocolTestTool::MonitorProtocolPacket(std::shared_ptr<LinuxTest> &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<LinuxTest> &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<LinuxTest> &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<LinuxTest> &moc
bool McuProtocolTestTool::CutOffPowerSupplyProtocolHandle(std::shared_ptr<LinuxTest> &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<LinuxT
bool McuProtocolTestTool::FeedWatchDogProtocolHandle(std::shared_ptr<LinuxTest> &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<LinuxTest>
bool McuProtocolTestTool::FeedingCycleProtocolHandle(std::shared_ptr<LinuxTest> &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<LinuxTest> &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<LinuxTest> &m
bool McuProtocolTestTool::SetDataTimeProtocolHandle(std::shared_ptr<LinuxTest> &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<LinuxTest> &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<LinuxTest> &mo
bool McuProtocolTestTool::SetPirSensitivityProtocolHandle(std::shared_ptr<LinuxTest> &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<LinuxTes
const void *buf, size_t count)
{
LockProtocolHandle();
memcpy(REPLY_SET_PIR_SENSITIVITY + PROTOCOL_SERIAL_NUMBER_OFFSET,
memcpy(REPLY_SET_PIR_SENSITIVITY_X + PROTOCOL_SERIAL_NUMBER_OFFSET,
(unsigned char *)buf + PROTOCOL_SERIAL_NUMBER_OFFSET,
PROTOCOL_SERIAL_NUMBER_LENGTH);
ResetCheckCode(REPLY_SET_PIR_SENSITIVITY, sizeof(REPLY_SET_PIR_SENSITIVITY));
ResetCheckCode(REPLY_SET_PIR_SENSITIVITY_X, sizeof(REPLY_SET_PIR_SENSITIVITY_X));
ReplySelectSucceed(mock, uartFd);
constexpr int LEFT_DATA_LENGTH = sizeof(REPLY_SET_PIR_SENSITIVITY) - PROTOCOL_DATA_KEY_HEAD_LENGTH;
constexpr int LEFT_DATA_LENGTH = sizeof(REPLY_SET_PIR_SENSITIVITY_X) - PROTOCOL_DATA_KEY_HEAD_LENGTH;
auto apiReadKeyHead = [=](int fd, void *buf, size_t count) {
memcpy(buf, REPLY_SET_PIR_SENSITIVITY, PROTOCOL_DATA_KEY_HEAD_LENGTH);
memcpy(buf, REPLY_SET_PIR_SENSITIVITY_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_PIR_SENSITIVITY + PROTOCOL_DATA_KEY_HEAD_LENGTH, LEFT_DATA_LENGTH);
memcpy(buf, REPLY_SET_PIR_SENSITIVITY_X + PROTOCOL_DATA_KEY_HEAD_LENGTH, LEFT_DATA_LENGTH);
McuProtocolTestTool::PrintHexadecimalData(buf, LEFT_DATA_LENGTH, READ_PRINT);
UnlockProtocolHandle();
};
@ -484,16 +491,17 @@ void McuProtocolTestTool::OtherSideAskIpcMissionInit(std::shared_ptr<LinuxTest>
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<LinuxTest>
.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<ProtocolMonitorTest> test =
std::dynamic_pointer_cast<ProtocolMonitorTest>(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)
{

View File

@ -27,4 +27,23 @@ std::shared_ptr<ProtocolMonitor> &ProtocolMonitor::GetInstance(std::shared_ptr<P
}
}
return instance;
}
void ProtocolMonitorTest::Init(std::shared_ptr<ProtocolMonitorTest> &test)
{
EXPECT_CALL(*test.get(), MonitorWriteProtocolData(_, _, _, _, _)).WillRepeatedly(DoAll(Return()));
}
void ProtocolMonitorTest::WriteDataOnce(std::shared_ptr<ProtocolMonitorTest> &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()));
}

View File

@ -17,6 +17,35 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <memory>
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<ProtocolMonitorTest> &test);
static void WriteDataOnce(std::shared_ptr<ProtocolMonitorTest> &test, const short &head,
const unsigned int &serialNumber, const short &command, const void *data,
const short &packetLength);
};
#endif

View File

@ -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;

View File

@ -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<VProtocolContext> NULL_CONTEXT;
std::shared_ptr<VProtocolParam> param = std::make_shared<ProtocolParam<const unsigned char>>(
PROTOCOL_COMMAND::REPLY_OTHER_SIDE_ASK_SEND_IPC_MISSION, static_cast<const unsigned char>(result));
param->mSerialNumber = serialNumber;
std::shared_ptr<ProtocolHandle> handle = ProtocolHandle::CreateProtocolData(param);
WriteProtocolData(
handle->GetProtocolDataBuff(), handle->GetProtocolDataLength(), NULL_CONTEXT, handle->GetSerialNumber());

View File

@ -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<VProtocolParam> &param) : mParam(param)
{
mProtocolData = nullptr;
@ -65,9 +64,14 @@ ProtocolPacket ProtocolHandle::CreatePocketWithSerialNumber(void)
std::lock_guard<std::mutex> 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<VProtocolParam> &p
LogError("Something wrong happened, make packet failed.\n");
return;
}
mProtocolSerialNumber = param->mSerialNumber;
std::map<PROTOCOL_COMMAND, MakePacketFunc>::iterator iter;
iter = mMakePacketFunc.find(param->mCommand);
if (iter != mMakePacketFunc.end()) {

View File

@ -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 <typename T>
class ProtocolParam : public VProtocolParam