Add:McuManager set data and time.

This commit is contained in:
Fancy code 2024-02-10 18:08:01 -08:00
parent cb2c073c3d
commit 74a9705d97
13 changed files with 283 additions and 61 deletions

View File

@ -30,13 +30,26 @@ enum class ASK_RESULT
TIMEOUT,
END
};
typedef struct mcu_ask_date_time
{
const unsigned short mYear;
const unsigned char mMon;
const unsigned char mDay;
const unsigned char mHour;
const unsigned char mMin;
const unsigned char mSecond;
mcu_ask_date_time(const unsigned short year, const unsigned char mon, const unsigned char day,
const unsigned char hour, const unsigned char min, const unsigned char second)
: mYear(year), mMon(mon), mDay(day), mHour(hour), mMin(min), mSecond(second)
{
}
} McuAskDateTime;
class VMcuAsk
{
public:
VMcuAsk() { mSerialNumber = 0; }
virtual ~VMcuAsk() = default;
virtual ASK_RESULT Blocking(void) { return ASK_RESULT::END; }
// virtual void StopBlocking(void) {}
virtual bool NeedReply(void) { return false; }
virtual void ReplyFinished(const bool result) {}
virtual bool IfTimeout(const unsigned int &integrationTimeMs) { return false; }
@ -53,7 +66,7 @@ class McuAsk : virtual public VMcuAsk
{
public:
McuAsk() {}
McuAsk() = default;
virtual ~McuAsk() = default;
public:
@ -71,28 +84,14 @@ public:
IMcuManager() = default;
virtual ~IMcuManager() = default;
static std::shared_ptr<IMcuManager> &GetInstance(std::shared_ptr<IMcuManager> *impl = nullptr);
virtual const StatusCode Init(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); }
virtual const StatusCode UnInit(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); }
virtual const StatusCode SetMcuMonitor(std::shared_ptr<VMcuMonitor> &monitor)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
virtual const StatusCode GetIpcMission(std::shared_ptr<VMcuAsk> &ask)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
virtual const StatusCode CutOffPowerSupply(std::shared_ptr<VMcuAsk> &ask)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
virtual const StatusCode FeedWatchDog(std::shared_ptr<VMcuAsk> &ask)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
virtual const StatusCode Init(void);
virtual const StatusCode UnInit(void);
virtual const StatusCode SetMcuMonitor(std::shared_ptr<VMcuMonitor> &monitor);
virtual const StatusCode GetIpcMission(std::shared_ptr<VMcuAsk> &ask);
virtual const StatusCode CutOffPowerSupply(std::shared_ptr<VMcuAsk> &ask);
virtual const StatusCode FeedWatchDog(std::shared_ptr<VMcuAsk> &ask);
virtual const StatusCode SetFeedingCycleForWatchDog(std::shared_ptr<VMcuAsk> &ask, const unsigned char &hour,
const unsigned char &min, const unsigned char &second)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
const unsigned char &min, const unsigned char &second);
virtual const StatusCode SetDateTime(std::shared_ptr<VMcuAsk> &ask, const McuAskDateTime &value);
};
#endif

View File

@ -27,4 +27,31 @@ std::shared_ptr<IMcuManager> &IMcuManager::GetInstance(std::shared_ptr<IMcuManag
}
}
return instance;
}
const StatusCode IMcuManager::Init(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); }
const StatusCode IMcuManager::UnInit(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); }
const StatusCode IMcuManager::SetMcuMonitor(std::shared_ptr<VMcuMonitor> &monitor)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
const StatusCode IMcuManager::GetIpcMission(std::shared_ptr<VMcuAsk> &ask)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
const StatusCode IMcuManager::CutOffPowerSupply(std::shared_ptr<VMcuAsk> &ask)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
const StatusCode IMcuManager::FeedWatchDog(std::shared_ptr<VMcuAsk> &ask)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
const StatusCode IMcuManager::SetFeedingCycleForWatchDog(std::shared_ptr<VMcuAsk> &ask, const unsigned char &hour,
const unsigned char &min, const unsigned char &second)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
const StatusCode IMcuManager::SetDateTime(std::shared_ptr<VMcuAsk> &ask, const McuAskDateTime &value)
{
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}

View File

@ -109,6 +109,22 @@ void McuDevice::GetIpcMissionReply(const unsigned int &serialNumber, const unsig
DeleteMcuAsk(ask);
}
}
void McuDevice::OnlyResultReply(const unsigned int &serialNumber, const ReplyResult &result)
{
std::shared_ptr<VMcuAsk> ask;
SearchMcuAsk(serialNumber, ask);
if (ask) {
std::shared_ptr<McuAsk<ASK_RESULT>> realAsk = std::dynamic_pointer_cast<McuAsk<ASK_RESULT>>(ask);
if (realAsk) {
realAsk->mDataReply = static_cast<ASK_RESULT>(result);
ask->ReplyFinished(true);
}
else {
ask->ReplyFinished(false);
}
DeleteMcuAsk(ask);
}
}
void McuDevice::SetFeedingCycleForWatchDogReply(const unsigned int &serialNumber, const ReplyResult &result)
{
std::shared_ptr<VMcuAsk> ask;

View File

@ -34,6 +34,7 @@ public:
public:
void GetIpcMissionReply(const unsigned int &serialNumber, const unsigned char &mission) override;
void OnlyResultReply(const unsigned int &serialNumber, const ReplyResult &result) override;
void SetFeedingCycleForWatchDogReply(const unsigned int &serialNumber, const ReplyResult &result) override;
public:

View File

@ -46,4 +46,10 @@ const StatusCode McuManagerImpl::SetFeedingCycleForWatchDog(std::shared_ptr<VMcu
{
std::shared_ptr<VProtocolContext> context = std::make_shared<ProtocolContext<std::shared_ptr<VMcuAsk>>>(ask);
return McuProtocol::SetFeedingCycleForWatchDog(hour, min, second, context);
}
const StatusCode McuManagerImpl::SetDateTime(std::shared_ptr<VMcuAsk> &ask, const McuAskDateTime &value)
{
std::shared_ptr<VProtocolContext> context = std::make_shared<ProtocolContext<std::shared_ptr<VMcuAsk>>>(ask);
return McuProtocol::McuSetDateTime(
value.mYear, value.mMon, value.mDay, value.mHour, value.mMin, value.mSecond, context);
}

View File

@ -30,5 +30,6 @@ public:
const StatusCode FeedWatchDog(std::shared_ptr<VMcuAsk> &ask) override;
const StatusCode SetFeedingCycleForWatchDog(std::shared_ptr<VMcuAsk> &ask, const unsigned char &hour,
const unsigned char &min, const unsigned char &second) override;
const StatusCode SetDateTime(std::shared_ptr<VMcuAsk> &ask, const McuAskDateTime &value) override;
};
#endif

View File

@ -162,11 +162,22 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_FeedWatchDog)
// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_EXAMPLE_SetFeedingCycleForWatchDog
TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_SetFeedingCycleForWatchDog)
{
class McuAskTest : public McuAskBase
class McuAskTest : public McuAsk<ASK_RESULT>, public McuAskBase
{
public:
McuAskTest() : McuAskBase(McuAskBlock::BLOCK, McuAskReply::NEED_REPLY) {} // using McuAskBlock::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>();
@ -178,6 +189,38 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_SetFeedingCycleForWatc
IMcuManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/McuManagerTest
// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_EXAMPLE_SetDateTime
TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_SetDateTime)
{
/**
* @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<ASK_RESULT>, 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>();
McuAskDateTime value(2014, 1, 15, 0, 0, 0);
IMcuManager::GetInstance()->SetDateTime(ask, value);
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
IMcuManager::GetInstance()->UnInit();
}
// ../output_files/test/bin/McuManagerTest
// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_AUTO_GetIpcMission
TEST_F(McuManagerMockTest, INTEGRATION_McuManager_AUTO_GetIpcMission)
{

View File

@ -44,6 +44,8 @@ private:
bool FeedWatchDogProtocolHandle(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);
bool SetDataTimeProtocolHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf, size_t count);
void SetDataTimeProtocolInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf, size_t count);
void LockProtocolHandle(void);
void UnlockProtocolHandle(void);
void UnlockThread(void);

View File

@ -38,6 +38,9 @@ unsigned char ASK_SET_FEEDING_CYCLE[] = {
0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x81, 0x04, 0x00, 0x0F, 0x01, 0x01, 0x01, 0xA7, 0x9A};
unsigned char REPLY_SET_FEEDING_CYCLE[] = {
0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00, 0x0D, 0x01, 0x52, 0x26};
unsigned char ASK_SET_DATE_TIME[] = {
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};
McuProtocolTestTool::McuProtocolTestTool()
{
mThreadRuning = false;
@ -48,6 +51,7 @@ McuProtocolTestTool::McuProtocolTestTool()
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));
mProtocolHandle.push_back(std::bind(&McuProtocolTestTool::SetDataTimeProtocolHandle, this, _1, _2, _3, _4));
}
void McuProtocolTestTool::Init(std::shared_ptr<LinuxTest> &mock, const UartInfo &uart)
{
@ -278,6 +282,53 @@ void McuProtocolTestTool::FeedingCycleProtocolInit(std::shared_ptr<LinuxTest> &m
.WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(apiReadLeftData)), Return(LEFT_DATA_LENGTH)))
.WillRepeatedly(DoAll(Return(UART_DEVICE_READ_NOTHING)));
}
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) {
short replyCheckCode =
calculate_check_sum(REPLY_SET_DATE_TIME, sizeof(REPLY_SET_DATE_TIME) - PROTOCOL_CHECK_CODE_LENGTH);
replyCheckCode = htons(replyCheckCode);
LogInfo("Set ASK_SET_DATE_TIME, 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(
(unsigned char *)buf + count - PROTOCOL_CHECK_CODE_LENGTH, &askCheckCode, PROTOCOL_CHECK_CODE_LENGTH);
EXPECT_EQ(result, 0) << "ask protocol data errer, check code isn't right.";
auto handle = [=, &mock](McuProtocolTestTool *testTool) {
testTool->SetDataTimeProtocolInit(mock, uartFd, buf, count);
};
if (mLockThread.joinable()) {
mLockThread.join();
}
mLockThread = std::thread(handle, this);
return PROTOCOL_HANDLED;
}
return PROTOCOL_NOT_HANDLED;
}
void McuProtocolTestTool::SetDataTimeProtocolInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *buf,
size_t count)
{
LockProtocolHandle();
memcpy(REPLY_SET_DATE_TIME + 2, (unsigned char *)buf + 2, 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;
auto apiReadKeyHead = [=](int fd, void *buf, size_t count) {
memcpy(buf, REPLY_SET_DATE_TIME, 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);
McuProtocolTestTool::PrintHexadecimalData(buf, LEFT_DATA_LENGTH, READ_PRINT);
UnlockProtocolHandle();
};
EXPECT_CALL(*mock.get(), fx_read(uartFd, _, _))
.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)));
}
void McuProtocolTestTool::UnlockProtocolHandle(void)
{
auto unlockThread = [=](McuProtocolTestTool *testTool) { testTool->UnlockThread(); };

View File

@ -88,6 +88,7 @@ public:
virtual ~VProtocolRecv() = default;
static std::shared_ptr<VProtocolRecv> &GetInstance(std::shared_ptr<VProtocolRecv> *impl = nullptr);
virtual void GetIpcMissionReply(const unsigned int &serialNumber, const unsigned char &mission) {}
virtual void OnlyResultReply(const unsigned int &serialNumber, const ReplyResult &result) {}
virtual void SetFeedingCycleForWatchDogReply(const unsigned int &serialNumber, const ReplyResult &result) {}
};
class McuProtocol : virtual public VProtocolBase
@ -103,6 +104,9 @@ public:
const StatusCode SetFeedingCycleForWatchDog(const unsigned char &hour, const unsigned char &min,
const unsigned char &second,
std::shared_ptr<VProtocolContext> &context);
const StatusCode McuSetDateTime(const unsigned short &year, const unsigned char &mon, const unsigned char &day,
const unsigned char &hour, const unsigned char &min, const unsigned char &second,
std::shared_ptr<VProtocolContext> &context);
void DataHandleThread(void);
protected:

View File

@ -88,6 +88,18 @@ const StatusCode McuProtocol::SetFeedingCycleForWatchDog(const unsigned char &ho
return WriteProtocolData(
handle->GetProtocolDataBuff(), handle->GetProtocolDataLength(), context, handle->GetSerialNumber());
}
const StatusCode McuProtocol::McuSetDateTime(const unsigned short &year, const unsigned char &mon,
const unsigned char &day, const unsigned char &hour,
const unsigned char &min, const unsigned char &second,
std::shared_ptr<VProtocolContext> &context)
{
SetDateTime dateTime(year, mon, day, hour, min, second);
std::shared_ptr<VProtocolParam> param =
std::make_shared<ProtocolParam<SetDateTime>>(PROTOCOL_COMMAND::ASK_SET_DATE_TIME, dateTime);
std::shared_ptr<ProtocolHandle> handle = ProtocolHandle::CreateProtocolData(param);
return WriteProtocolData(
handle->GetProtocolDataBuff(), handle->GetProtocolDataLength(), context, handle->GetSerialNumber());
}
void McuProtocol::DataHandleThread(void)
{
mThreadRuning = true;

View File

@ -19,21 +19,22 @@
#include "ModBusCRC16.h"
#include <netinet/in.h>
#include <string.h>
using std::placeholders::_1;
// using std::placeholders::_2;
// using std::placeholders::_3;
// using std::placeholders::_4;
unsigned int ProtocolHandle::mSerialNumber = 1;
std::mutex ProtocolHandle::mMutex;
constexpr unsigned short PROTOCOL_HEAD = 0xFAC1;
constexpr size_t KEY_HEAD_LENGTH = sizeof(short) + sizeof(unsigned int) + sizeof(short) + sizeof(short);
ProtocolHandle::ProtocolHandle(const std::shared_ptr<VProtocolParam> &param) : mParam(param)
{
mProtocolData = nullptr;
mProtocolDataLength = 0;
mMakePacketFunc[ASK_IPC_MISSION] = std::bind(&ProtocolHandle::MakeAskIpcMissionPacket, this, std::placeholders::_1);
mMakePacketFunc[ASK_CUT_OFF_PWOER_SUPPLY] =
std::bind(&ProtocolHandle::MakeAskCutOffPowerSupplyPacket, this, std::placeholders::_1);
mMakePacketFunc[ASK_FEED_WATCH_DOG] =
std::bind(&ProtocolHandle::MakeAskFeedWatchDogPacket, this, std::placeholders::_1);
mMakePacketFunc[ASK_SET_FEEDING_CYCLE] =
std::bind(&ProtocolHandle::MakeAskSetFeedingCyclePacket, this, std::placeholders::_1);
mMakePacketFunc[ASK_IPC_MISSION] = std::bind(&ProtocolHandle::MakeAskIpcMissionPacket, this, _1);
mMakePacketFunc[ASK_CUT_OFF_PWOER_SUPPLY] = std::bind(&ProtocolHandle::MakeAskCutOffPowerSupplyPacket, this, _1);
mMakePacketFunc[ASK_FEED_WATCH_DOG] = std::bind(&ProtocolHandle::MakeAskFeedWatchDogPacket, this, _1);
mMakePacketFunc[ASK_SET_FEEDING_CYCLE] = std::bind(&ProtocolHandle::MakeAskSetFeedingCyclePacket, this, _1);
mMakePacketFunc[ASK_SET_DATE_TIME] = std::bind(&ProtocolHandle::MakeAskSetDateTimePacket, this, _1);
}
ProtocolHandle::ProtocolHandle(const void *data, const size_t &length)
{
@ -42,10 +43,10 @@ ProtocolHandle::ProtocolHandle(const void *data, const size_t &length)
memcpy(mProtocolData, data, length);
}
mProtocolDataLength = length;
mAnalyzePacketFunc[REPLY_IPC_MISSION] =
std::bind(&ProtocolHandle::AnalyzeReplyIpcMissionPacket, this, std::placeholders::_1);
mAnalyzePacketFunc[REPLY_IPC_MISSION] = std::bind(&ProtocolHandle::AnalyzeReplyIpcMissionPacket, this, _1);
mAnalyzePacketFunc[REPLY_SET_FEEDING_CYCLE] =
std::bind(&ProtocolHandle::AnalyzeReplySetFeedingCyclePacket, this, std::placeholders::_1);
std::bind(&ProtocolHandle::AnalyzeReplySetFeedingCyclePacket, this, _1);
mAnalyzePacketFunc[REPLY_SET_DATE_TIME] = std::bind(&ProtocolHandle::AnalyzeReplyResultPacket, this, _1);
}
ProtocolHandle::~ProtocolHandle()
{
@ -64,6 +65,26 @@ ProtocolPacket ProtocolHandle::CreatePocketWithSerialNumber(void)
mSerialNumber++;
return packet;
}
void ProtocolHandle::MallocPacketDataBuff(const void *data, const size_t dataLength, const short &command)
{
size_t packetLength = KEY_HEAD_LENGTH + dataLength + CHECK_CODE_LENGTH;
mProtocolData = (unsigned char *)malloc(packetLength);
if (nullptr == mProtocolData) {
LogError("malloc failed, MakeAskIpcMissionPacket return.\n");
return;
}
ProtocolPacket packet = CreatePocketWithSerialNumber();
packet.mHead = PROTOCOL_HEAD;
packet.mCommand = command;
packet.mLength = packetLength;
BigEndianConversion(packet);
memcpy(mProtocolData, &packet, KEY_HEAD_LENGTH);
memcpy(mProtocolData + KEY_HEAD_LENGTH, data, dataLength);
packet.mCheckCode = calculate_check_sum(mProtocolData, packetLength - CHECK_CODE_LENGTH);
// packet.mCheckCode = BigEndianConversion(packet.mCheckCode);
memcpy(mProtocolData + packetLength - CHECK_CODE_LENGTH, &packet.mCheckCode, CHECK_CODE_LENGTH);
mProtocolDataLength = packetLength;
}
void ProtocolHandle::MakeProtocolPacket(const std::shared_ptr<VProtocolParam> &param)
{
if (mProtocolData != nullptr) {
@ -81,8 +102,8 @@ void ProtocolHandle::MakeProtocolPacket(const std::shared_ptr<VProtocolParam> &p
}
void ProtocolHandle::MakeNoUserDataPacket(const std::shared_ptr<VProtocolParam> &param)
{
size_t dataLength = KEY_HEAD_LENGTH + sizeof(short);
mProtocolData = (unsigned char *)malloc(dataLength);
size_t packetLength = KEY_HEAD_LENGTH + CHECK_CODE_LENGTH;
mProtocolData = (unsigned char *)malloc(packetLength);
if (nullptr == mProtocolData) {
LogError("malloc failed, MakeAskIpcMissionPacket return.\n");
return;
@ -90,13 +111,13 @@ void ProtocolHandle::MakeNoUserDataPacket(const std::shared_ptr<VProtocolParam>
ProtocolPacket packet = CreatePocketWithSerialNumber();
packet.mHead = PROTOCOL_HEAD;
packet.mCommand = param->mCommand;
packet.mLength = dataLength;
packet.mLength = packetLength;
BigEndianConversion(packet);
memcpy(mProtocolData, &packet, KEY_HEAD_LENGTH);
packet.mCheckCode = calculate_check_sum(mProtocolData, dataLength - sizeof(short));
packet.mCheckCode = calculate_check_sum(mProtocolData, packetLength - CHECK_CODE_LENGTH);
// packet.mCheckCode = BigEndianConversion(packet.mCheckCode);
memcpy(mProtocolData + KEY_HEAD_LENGTH, &packet.mCheckCode, sizeof(short));
mProtocolDataLength = dataLength;
memcpy(mProtocolData + KEY_HEAD_LENGTH, &packet.mCheckCode, CHECK_CODE_LENGTH);
mProtocolDataLength = packetLength;
}
void ProtocolHandle::MakeAskIpcMissionPacket(const std::shared_ptr<VProtocolParam> &param)
{
@ -112,9 +133,9 @@ void ProtocolHandle::MakeAskFeedWatchDogPacket(const std::shared_ptr<VProtocolPa
}
void ProtocolHandle::MakeAskSetFeedingCyclePacket(const std::shared_ptr<VProtocolParam> &param)
{
constexpr int PARAM_DATA_LENGTH = 3;
size_t dataLength = KEY_HEAD_LENGTH + PARAM_DATA_LENGTH + sizeof(short);
mProtocolData = (unsigned char *)malloc(dataLength);
constexpr int PARAM_DATA_LENGTH = sizeof(WatchDogParam);
size_t packetLength = KEY_HEAD_LENGTH + PARAM_DATA_LENGTH + CHECK_CODE_LENGTH;
mProtocolData = (unsigned char *)malloc(packetLength);
if (nullptr == mProtocolData) {
LogError("malloc failed, MakeAskIpcMissionPacket return.\n");
return;
@ -125,21 +146,21 @@ void ProtocolHandle::MakeAskSetFeedingCyclePacket(const std::shared_ptr<VProtoco
LogError("Invalid param.\n");
return;
}
char feedingCycle[PARAM_DATA_LENGTH] = {0};
feedingCycle[0] = SetParam->mData.mHour;
feedingCycle[1] = SetParam->mData.mMin;
feedingCycle[2] = SetParam->mData.mSecond;
ProtocolPacket packet = CreatePocketWithSerialNumber();
packet.mHead = PROTOCOL_HEAD;
packet.mCommand = param->mCommand;
packet.mLength = dataLength;
packet.mLength = packetLength;
BigEndianConversion(packet);
memcpy(mProtocolData, &packet, KEY_HEAD_LENGTH);
memcpy(mProtocolData + KEY_HEAD_LENGTH, feedingCycle, PARAM_DATA_LENGTH);
packet.mCheckCode = calculate_check_sum(mProtocolData, dataLength - sizeof(short));
memcpy(mProtocolData + KEY_HEAD_LENGTH, &(SetParam->mData), PARAM_DATA_LENGTH);
packet.mCheckCode = calculate_check_sum(mProtocolData, packetLength - CHECK_CODE_LENGTH);
// packet.mCheckCode = BigEndianConversion(packet.mCheckCode);
memcpy(mProtocolData + dataLength - sizeof(short), &packet.mCheckCode, sizeof(short));
mProtocolDataLength = dataLength;
memcpy(mProtocolData + packetLength - CHECK_CODE_LENGTH, &packet.mCheckCode, CHECK_CODE_LENGTH);
mProtocolDataLength = packetLength;
}
void ProtocolHandle::MakeAskSetDateTimePacket(const std::shared_ptr<VProtocolParam> &param)
{
MakeProtocolData<SetDateTime>(param);
}
void ProtocolHandle::AnalyzeProtocolPacket(void)
{
@ -179,6 +200,12 @@ unsigned char ProtocolHandle::ReplyOneBytePacketResult(const ProtocolPacket &pac
LogInfo("reply result = 0x%02X\n", replyResult);
return replyResult;
}
void ProtocolHandle::AnalyzeReplyResultPacket(const ProtocolPacket &packet)
{
LogInfo("AnalyzeReplyResultPacket\n");
unsigned char replyResult = ReplyOneBytePacketResult(packet);
VProtocolRecv::GetInstance()->OnlyResultReply(mProtocolSerialNumber, static_cast<ReplyResult>(replyResult));
}
void ProtocolHandle::AnalyzeReplyIpcMissionPacket(const ProtocolPacket &packet)
{
LogInfo("AnalyzeReplyIpcMissionPacket\n");
@ -194,7 +221,7 @@ void ProtocolHandle::AnalyzeReplySetFeedingCyclePacket(const ProtocolPacket &pac
}
bool ProtocolHandle::CheckoutTheCheckCode(const ProtocolPacket &packet)
{
short code = calculate_check_sum(mProtocolData, mProtocolDataLength - sizeof(short));
short code = calculate_check_sum(mProtocolData, mProtocolDataLength - CHECK_CODE_LENGTH);
// short packetCode = BigEndianConversion(code);
if (code == packet.mCheckCode) {
return true;

View File

@ -14,6 +14,7 @@
*/
#ifndef PROTOCOL_HANDLE_H
#define PROTOCOL_HANDLE_H
#include "ILog.h"
#include "StatusCode.h"
#include <functional>
#include <map>
@ -21,6 +22,8 @@
#include <mutex>
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);
#pragma pack(1)
typedef struct protocol_packet
{
@ -40,21 +43,37 @@ enum PROTOCOL_COMMAND
ASK_FEED_WATCH_DOG = 0x8103,
ASK_SET_FEEDING_CYCLE = 0x8104,
REPLY_SET_FEEDING_CYCLE = 0x0104,
ASK_SET_DATE_TIME = 0x8107,
REPLY_SET_DATE_TIME = 0x0107,
PROTOCOL_COMMAND_END
};
constexpr unsigned char ZERO_MEANS_SHUTDOWN_WATCH_DOG = 0x00;
class WatchDogParam
#pragma pack(1)
typedef struct watch_dog_param
{
public:
WatchDogParam(const unsigned char &hour, const unsigned char &min, const unsigned char &second)
watch_dog_param(const unsigned char &hour, const unsigned char &min, const unsigned char &second)
: mHour(hour), mMin(min), mSecond(second)
{
}
~WatchDogParam() = default;
const unsigned char mHour;
const unsigned char mMin;
const unsigned char mSecond;
};
} WatchDogParam;
typedef struct set_date_time
{
set_date_time(const unsigned short &year, const unsigned char &mon, const unsigned char &day,
const unsigned char &hour, const unsigned char &min, const unsigned char &second)
: mYear(year), mMon(mon), mDay(day), mHour(hour), mMin(min), mSecond(second)
{
}
const unsigned short mYear;
const unsigned char mMon;
const unsigned char mDay;
const unsigned char mHour;
const unsigned char mMin;
const unsigned char mSecond;
} SetDateTime;
#pragma pack()
class VProtocolParam
{
public:
@ -99,20 +118,34 @@ public:
*/
private:
ProtocolPacket CreatePocketWithSerialNumber(void);
void MallocPacketDataBuff(const void *data, const size_t dataLength, const short &command);
void MakeProtocolPacket(const std::shared_ptr<VProtocolParam> &param);
void MakeNoUserDataPacket(const std::shared_ptr<VProtocolParam> &param);
void MakeAskIpcMissionPacket(const std::shared_ptr<VProtocolParam> &param);
void MakeAskCutOffPowerSupplyPacket(const std::shared_ptr<VProtocolParam> &param);
void MakeAskFeedWatchDogPacket(const std::shared_ptr<VProtocolParam> &param);
void MakeAskSetFeedingCyclePacket(const std::shared_ptr<VProtocolParam> &param);
void MakeAskSetDateTimePacket(const std::shared_ptr<VProtocolParam> &param);
template <typename T>
void MakeProtocolData(const std::shared_ptr<VProtocolParam> &param)
{
constexpr int PARAM_DATA_LENGTH = sizeof(T);
std::shared_ptr<ProtocolParam<T>> SetParam = std::dynamic_pointer_cast<ProtocolParam<T>>(param);
if (!SetParam) {
LogError("Invalid param.\n");
return;
}
MallocPacketDataBuff(&(SetParam->mData), PARAM_DATA_LENGTH, param->mCommand);
}
private:
/**
* @brief These function implementations parse the received frame by frame continuous data into the data required by
* the application, completing the protocol unpacking.
*/
private:
void AnalyzeProtocolPacket(void);
unsigned char ReplyOneBytePacketResult(const ProtocolPacket &packet);
void AnalyzeReplyResultPacket(const ProtocolPacket &packet);
void AnalyzeReplyIpcMissionPacket(const ProtocolPacket &packet);
void AnalyzeReplySetFeedingCyclePacket(const ProtocolPacket &packet);