Improve code.

This commit is contained in:
Fancy code 2024-02-12 15:15:47 -08:00
parent 9933fb45b0
commit 9ca156dcb3
8 changed files with 207 additions and 42 deletions

View File

@ -83,7 +83,11 @@ void McuManagerImpl::OtherSideSendIpcMission(const unsigned int &serialNumber, c
class OtherSideSend : public UartRecvAsk, public McuAsk<ASK_RESULT>
{
public:
OtherSideSend(std::shared_ptr<McuManagerImpl> &mcuManager) : mMcuManager(mcuManager) {}
OtherSideSend(std::shared_ptr<McuManagerImpl> &mcuManager, const unsigned int &serialNumber)
: mMcuManager(mcuManager)
{
mSerialNumber = serialNumber;
}
~OtherSideSend() = default;
void ReplyFinished(const bool result) override
{
@ -96,7 +100,7 @@ void McuManagerImpl::OtherSideSendIpcMission(const unsigned int &serialNumber, c
std::shared_ptr<VMcuMonitor> monitor = GetMcuMonitor();
if (monitor) {
std::shared_ptr<McuManagerImpl> manager = std::dynamic_pointer_cast<McuManagerImpl>(SharedFromThis());
std::shared_ptr<VMcuAsk> ask = std::make_shared<OtherSideSend>(manager);
std::shared_ptr<VMcuAsk> ask = std::make_shared<OtherSideSend>(manager, serialNumber);
monitor->RecvIpcMissionEvent(ask, static_cast<IpcMission>(mission));
}
}

View File

@ -81,12 +81,51 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_GetIpcMission)
IMcuManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/McuManagerTest
// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_EXAMPLE_GetIpcMission2
/**
* @brief Construct a new test f object
* This test is an example that demonstrates how to obtain IpcMission data and add the business code after obtaining the
* data in the abstract interface.
*/
TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_GetIpcMission2)
{
/**
* @brief The user needs to derive a subclass of McuAskBase and add the business code after obtaining data in the
* abstract interface.
*/
class McuAskTest : public McuAsk<IpcMission>, public McuAskBase
{
public:
McuAskTest() : McuAskBase(McuAskBlock::NOT_BLOCK, McuAskReply::NEED_REPLY) {} // using McuAskBlock::NOT_BLOCK
virtual ~McuAskTest() = default;
void ReplyFinished(const bool result) override
{
McuAskBase::ReplyFinished(result);
if (result) {
LogInfo("Ask data succeed, mDataReply = %d.\n", static_cast<int>(mDataReply));
// Do something here.
}
else {
LogError("Ask data falied.\n");
}
}
};
IMcuManager::GetInstance()->Init();
std::shared_ptr<VMcuAsk> ask = std::make_shared<McuAskTest>();
LogInfo("GetIpcMission will not block here.\n");
IMcuManager::GetInstance()->GetIpcMission(ask);
LogInfo("GetIpcMission finished.\n");
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IMcuManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/McuManagerTest
// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_EXAMPLE_OtherSideSendIpcMission
/**
* @brief Construct a new test f object
* This example demonstrates how to use a monitor to capture event information sent by an MCU. You must assign a value
* to the processing result (mDataReply member) and call the ReplyCompleted function to complete the entire process.
*/
constexpr unsigned int TEST_SERIAL_NUMBER = 2;
TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_OtherSideSendIpcMission)
{
class MonitorTest : public VMcuMonitor
@ -100,10 +139,11 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_OtherSideSendIpcMissio
std::shared_ptr<McuAsk<ASK_RESULT>> ask = std::dynamic_pointer_cast<McuAsk<ASK_RESULT>>(recv);
ask->mDataReply = ASK_RESULT::SUCCEED;
recv->ReplyFinished(true);
EXPECT_EQ(TEST_SERIAL_NUMBER, recv->mSerialNumber);
}
};
IMcuManager::GetInstance()->Init();
OtherSideAskIpcMission(mLinuxTest);
OtherSideAskIpcMission(mLinuxTest, TEST_SERIAL_NUMBER);
std::shared_ptr<VMcuMonitor> monitor = std::make_shared<MonitorTest>();
IMcuManager::GetInstance()->SetMcuMonitor(monitor);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));

View File

@ -64,7 +64,7 @@ public:
void Init(std::shared_ptr<LinuxTest> &mock);
void UnInit(void);
bool CheckAskExist(const std::shared_ptr<VMcuAsk> &ask);
void OtherSideAskIpcMission(std::shared_ptr<LinuxTest> &mock);
void OtherSideAskIpcMission(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
private:
std::shared_ptr<McuManagerImplTest> mMcuManagerMock;

View File

@ -55,7 +55,7 @@ bool McuManagerTestTool::CheckAskExist(const std::shared_ptr<VMcuAsk> &ask)
{
return mMcuManagerMock->CheckAskExist(ask);
}
void McuManagerTestTool::OtherSideAskIpcMission(std::shared_ptr<LinuxTest> &mock)
void McuManagerTestTool::OtherSideAskIpcMission(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber)
{
McuProtocolTestTool::OtherSideAskIpcMission(mock);
McuProtocolTestTool::OtherSideAskIpcMission(mock, serialNumber);
}

View File

@ -31,13 +31,14 @@ public:
virtual ~McuProtocolTestTool() = default;
void Init(std::shared_ptr<LinuxTest> &mock, const UartInfo &uart);
void UnInit(void);
void OtherSideAskIpcMission(std::shared_ptr<LinuxTest> &mock);
void OtherSideAskIpcMission(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
private:
void CheckSerialNumber(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 ReplySelectSucceed(std::shared_ptr<LinuxTest> &mock, const int &uartFd);
bool MonitorProtocolPacket(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);
bool CutOffPowerSupplyProtocolHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf,
@ -57,12 +58,20 @@ private:
void PipeSelectTimeoutForProtocolHandleImmediately(void);
private:
void OtherSideAskIpcMissionHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd);
void OtherSideAskIpcMissionInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd);
void OtherSideAskIpcMissionHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const unsigned int &serialNumber);
void OtherSideAskIpcMissionInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const unsigned int &serialNumber);
private:
static void PrintHexadecimalData(const void *buf, const size_t &bufLength, const int event);
private:
// virtual void MonitorWriteProtocolData(const short &head, const unsigned int &serialNumber, const short &command,
// const void *data, const short &packetLength)
// {
// }
private:
std::mutex mMutex;
bool mThreadRuning;

View File

@ -15,6 +15,7 @@
#include "McuProtocolTestTool.h"
#include "ILog.h"
#include "ModBusCRC16.h"
#include "ProtocolMonitor.h"
#include <netinet/in.h>
#include <string.h>
#include <thread>
@ -27,10 +28,16 @@ constexpr int PIPE_READ_FD_INDEX = 0;
constexpr int PIPE_WRITE_FD_INDEX = 1;
constexpr int INVALID_FD = -1;
const char *gPipeBuf = "nothing";
constexpr size_t PROTOCOL_DATA_KEY_HEAD_LENGTH = 10;
constexpr size_t PROTOCOL_COMMAND_LENGTH = 6;
constexpr size_t PROTOCOL_CHECK_CODE_LENGTH = sizeof(short);
constexpr size_t PROTOCOL_HEAD_LENGTH = 2;
constexpr size_t PROTOCOL_SERIAL_NUMBER_LENGTH = sizeof(unsigned int);
constexpr size_t PROTOCOL_SERIAL_NUMBER_OFFSET = PROTOCOL_HEAD_LENGTH;
constexpr size_t PROTOCOL_COMMAND_LENGTH = sizeof(short);
constexpr size_t PROTOCOL_COMMAND_OFFSET = PROTOCOL_SERIAL_NUMBER_OFFSET + PROTOCOL_SERIAL_NUMBER_LENGTH;
constexpr size_t PROTOCOL_DATA_LENGTH_LENGTH = sizeof(short);
constexpr size_t PROTOCOL_DATA_LENGTH_OFFSET = PROTOCOL_COMMAND_OFFSET + PROTOCOL_DATA_LENGTH_LENGTH;
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};
@ -57,6 +64,7 @@ McuProtocolTestTool::McuProtocolTestTool()
mPipeFdMockSelect[PIPE_READ_FD_INDEX] = -1;
mPipeFdMockSelect[PIPE_WRITE_FD_INDEX] = -1;
mUartFd = INVALID_FD;
mProtocolHandle.push_back(std::bind(&McuProtocolTestTool::MonitorProtocolPacket, this, _1, _2, _3, _4));
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));
@ -66,6 +74,8 @@ McuProtocolTestTool::McuProtocolTestTool()
}
void McuProtocolTestTool::Init(std::shared_ptr<LinuxTest> &mock, const UartInfo &uart)
{
std::shared_ptr<ProtocolMonitor> test = std::make_shared<ProtocolMonitorTest>();
ProtocolMonitor::GetInstance(&test);
if (pipe(mPipeFdMockSelect) == 0) {
mPipeFdMockSelectInit = true;
}
@ -106,16 +116,18 @@ void McuProtocolTestTool::UnInit(void)
}
mPipeFdMockSelectInit = false;
mUartFd = INVALID_FD;
std::shared_ptr<ProtocolMonitor> monitor = std::make_shared<ProtocolMonitor>();
ProtocolMonitor::GetInstance(&monitor);
}
void McuProtocolTestTool::OtherSideAskIpcMission(std::shared_ptr<LinuxTest> &mock)
void McuProtocolTestTool::OtherSideAskIpcMission(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber)
{
OtherSideAskIpcMissionHandle(mock, mUartFd);
OtherSideAskIpcMissionHandle(mock, mUartFd, serialNumber);
}
void McuProtocolTestTool::CheckSerialNumber(const void *buf, const size_t &count)
{
unsigned int serialNumber = 0;
if (count > PROTOCOL_COMMAND_LENGTH + PROTOCOL_CHECK_CODE_LENGTH) {
memcpy(&serialNumber, (unsigned char *)buf + 2, 4);
if (count > PROTOCOL_COMMAND_OFFSET + PROTOCOL_CHECK_CODE_LENGTH) {
memcpy(&serialNumber, (unsigned char *)buf + PROTOCOL_SERIAL_NUMBER_OFFSET, PROTOCOL_SERIAL_NUMBER_LENGTH);
for (auto iter = mSerialNumberList.begin(); iter != mSerialNumberList.end(); ++iter) {
EXPECT_NE(*iter, serialNumber);
}
@ -125,7 +137,7 @@ void McuProtocolTestTool::CheckSerialNumber(const void *buf, const size_t &count
void McuProtocolTestTool::ChecCRC16Code(const void *buf, const size_t &count)
{
short code = calculate_check_sum((unsigned char *)buf, count - PROTOCOL_CHECK_CODE_LENGTH);
EXPECT_EQ(memcmp((unsigned char *)buf + count - PROTOCOL_CHECK_CODE_LENGTH, &code, 2), 0);
EXPECT_EQ(memcmp((unsigned char *)buf + count - PROTOCOL_CHECK_CODE_LENGTH, &code, PROTOCOL_CHECK_CODE_LENGTH), 0);
}
void McuProtocolTestTool::ResetCheckCode(const void *buf, const size_t &count)
{
@ -174,11 +186,28 @@ void McuProtocolTestTool::ReplySelectSucceed(std::shared_ptr<LinuxTest> &mock, c
.WillRepeatedly(DoAll(WithArgs<0, 1, 2, 3, 4>(Invoke(selectTimeOut)), Return(MOCK_SELECT_TIME_OUT)));
PipeSelectTimeoutForProtocolHandleImmediately();
}
bool McuProtocolTestTool::MonitorProtocolPacket(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf,
size_t count)
{
short head;
unsigned int serialNumber;
short command;
void *data;
short packetLength;
memcpy(&head, buf, PROTOCOL_HEAD_LENGTH);
memcpy(&serialNumber, (unsigned char *)buf + PROTOCOL_SERIAL_NUMBER_OFFSET, PROTOCOL_SERIAL_NUMBER_LENGTH);
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);
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_LENGTH, (unsigned char *)buf + PROTOCOL_COMMAND_LENGTH, 2) == 0) {
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");
short askCheckCode = calculate_check_sum((unsigned char *)buf, count - PROTOCOL_CHECK_CODE_LENGTH);
// askCheckCode = htons(askCheckCode);
@ -200,7 +229,9 @@ void McuProtocolTestTool::IpcMissionProtocolInit(std::shared_ptr<LinuxTest> &moc
size_t count)
{
LockProtocolHandle();
memcpy(REPLY_IPC_MISSION + 2, (unsigned char *)buf + 2, PROTOCOL_SERIAL_NUMBER_LENGTH);
memcpy(REPLY_IPC_MISSION + PROTOCOL_SERIAL_NUMBER_OFFSET,
(unsigned char *)buf + PROTOCOL_SERIAL_NUMBER_OFFSET,
PROTOCOL_SERIAL_NUMBER_LENGTH);
ResetCheckCode(REPLY_IPC_MISSION, sizeof(REPLY_IPC_MISSION));
ReplySelectSucceed(mock, uartFd);
constexpr int LEFT_DATA_LENGTH = sizeof(REPLY_IPC_MISSION) - PROTOCOL_DATA_KEY_HEAD_LENGTH;
@ -221,9 +252,9 @@ 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_LENGTH, (unsigned char *)buf + PROTOCOL_COMMAND_LENGTH, 2) ==
0) {
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");
short askCheckCode = calculate_check_sum((unsigned char *)buf, count - PROTOCOL_CHECK_CODE_LENGTH);
// askCheckCode = htons(askCheckCode);
@ -238,8 +269,9 @@ 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_LENGTH, (unsigned char *)buf + PROTOCOL_COMMAND_LENGTH, 2) == 0) {
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");
short askCheckCode = calculate_check_sum((unsigned char *)buf, count - PROTOCOL_CHECK_CODE_LENGTH);
// askCheckCode = htons(askCheckCode);
@ -254,9 +286,9 @@ 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_LENGTH, (unsigned char *)buf + PROTOCOL_COMMAND_LENGTH, 2) ==
0) {
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);
replyCheckCode = htons(replyCheckCode);
@ -282,7 +314,9 @@ void McuProtocolTestTool::FeedingCycleProtocolInit(std::shared_ptr<LinuxTest> &m
size_t count)
{
LockProtocolHandle();
memcpy(REPLY_SET_FEEDING_CYCLE + 2, (unsigned char *)buf + 2, PROTOCOL_SERIAL_NUMBER_LENGTH);
memcpy(REPLY_SET_FEEDING_CYCLE + 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));
ReplySelectSucceed(mock, uartFd);
constexpr int LEFT_DATA_LENGTH = sizeof(REPLY_SET_FEEDING_CYCLE) - PROTOCOL_DATA_KEY_HEAD_LENGTH;
@ -303,8 +337,9 @@ 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_LENGTH, (unsigned char *)buf + PROTOCOL_COMMAND_LENGTH, 2) == 0) {
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) {
short replyCheckCode =
calculate_check_sum(REPLY_SET_DATE_TIME, sizeof(REPLY_SET_DATE_TIME) - PROTOCOL_CHECK_CODE_LENGTH);
replyCheckCode = htons(replyCheckCode);
@ -329,7 +364,9 @@ void McuProtocolTestTool::SetDataTimeProtocolInit(std::shared_ptr<LinuxTest> &mo
size_t count)
{
LockProtocolHandle();
memcpy(REPLY_SET_DATE_TIME + 2, (unsigned char *)buf + 2, PROTOCOL_SERIAL_NUMBER_LENGTH);
memcpy(REPLY_SET_DATE_TIME + 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));
ReplySelectSucceed(mock, uartFd);
constexpr int LEFT_DATA_LENGTH = sizeof(REPLY_SET_DATE_TIME) - PROTOCOL_DATA_KEY_HEAD_LENGTH;
@ -350,9 +387,9 @@ 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_LENGTH, (unsigned char *)buf + PROTOCOL_COMMAND_LENGTH, 2) ==
0) {
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);
replyCheckCode = htons(replyCheckCode);
@ -377,7 +414,9 @@ void McuProtocolTestTool::SetPirSensitivityProtocolInit(std::shared_ptr<LinuxTes
const void *buf, size_t count)
{
LockProtocolHandle();
memcpy(REPLY_SET_PIR_SENSITIVITY + 2, (unsigned char *)buf + 2, PROTOCOL_SERIAL_NUMBER_LENGTH);
memcpy(REPLY_SET_PIR_SENSITIVITY + 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));
ReplySelectSucceed(mock, uartFd);
constexpr int LEFT_DATA_LENGTH = sizeof(REPLY_SET_PIR_SENSITIVITY) - PROTOCOL_DATA_KEY_HEAD_LENGTH;
@ -427,21 +466,25 @@ void McuProtocolTestTool::PipeSelectTimeoutForProtocolHandleImmediately(void)
}
}
}
void McuProtocolTestTool::OtherSideAskIpcMissionHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd)
void McuProtocolTestTool::OtherSideAskIpcMissionHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const unsigned int &serialNumber)
{
LogInfo("OtherSideAskIpcMissionHandle\n");
auto handle = [=, &mock](McuProtocolTestTool *testTool) { testTool->OtherSideAskIpcMissionInit(mock, uartFd); };
auto handle = [=, &mock](McuProtocolTestTool *testTool) {
testTool->OtherSideAskIpcMissionInit(mock, uartFd, serialNumber);
};
if (mLockThread.joinable()) {
mLockThread.join();
}
mLockThread = std::thread(handle, this);
}
void McuProtocolTestTool::OtherSideAskIpcMissionInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd)
void McuProtocolTestTool::OtherSideAskIpcMissionInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
const unsigned int &serialNumber)
{
LockProtocolHandle();
unsigned int serialNumber = 99;
serialNumber = htonl(serialNumber);
memcpy(OTHER_SIDE_ASK_SEND_IPC_MISSION + 2, &serialNumber, PROTOCOL_SERIAL_NUMBER_LENGTH);
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));
ReplySelectSucceed(mock, uartFd);
constexpr int LEFT_DATA_LENGTH = sizeof(OTHER_SIDE_ASK_SEND_IPC_MISSION) - PROTOCOL_DATA_KEY_HEAD_LENGTH;

View File

@ -0,0 +1,30 @@
/*
* 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 "ProtocolMonitor.h"
#include "ILog.h"
std::shared_ptr<ProtocolMonitor> &ProtocolMonitor::GetInstance(std::shared_ptr<ProtocolMonitor> *impl)
{
static auto instance = std::make_shared<ProtocolMonitor>();
if (impl) {
if (instance.use_count() == 1) {
LogInfo("Instance changed succeed.\n");
instance = *impl;
}
else {
LogError("Can't changing the instance becase of using by some one.\n");
}
}
return instance;
}

View File

@ -0,0 +1,39 @@
/*
* 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.
*/
#ifndef PROTOCOL_MONITOR_H
#define PROTOCOL_MONITOR_H
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <memory>
class ProtocolMonitor
{
public:
ProtocolMonitor() = default;
virtual ~ProtocolMonitor() = default;
static std::shared_ptr<ProtocolMonitor> &GetInstance(std::shared_ptr<ProtocolMonitor> *impl = nullptr);
virtual void MonitorWriteProtocolData(const short &head, const unsigned int &serialNumber, const short &command,
const void *data, const short &packetLength)
{
}
};
class ProtocolMonitorTest : public ProtocolMonitor
{
public:
ProtocolMonitorTest() = default;
virtual ~ProtocolMonitorTest() = default;
MOCK_METHOD5(MonitorWriteProtocolData,
void(const short &, const unsigned int &, const short &, const void *, const short &));
};
#endif