Add:McuManager handle protocol data send by other side.
This commit is contained in:
parent
f1747df345
commit
9933fb45b0
|
@ -524,7 +524,7 @@ end
|
|||
|
||||
##### 1.4.3.2.5. MCU协议模块
|
||||
|
||||
   负责对MCU协议进行封包/解包,负责协议的多态替换。
|
||||
   负责对MCU协议进行封包/解包,负责协议的多态替换。协议数据统一使用网络字节序(大端字节序)。
|
||||
|
||||
###### 1.4.3.2.5.1. 协议格式
|
||||
|
||||
|
@ -541,6 +541,8 @@ unsigned char ASK_IPC_MISSION[] = {0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x81, 0x0
|
|||
unsigned char REPLY_IPC_MISSION[] = {0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x0D, 0x01, 0xAA, 0x89};
|
||||
```
|
||||
|
||||
   流水号管理发送方和接收方互相独立,各自往外发的协议中流水号存在重复的可能,接收方直接复制回传即可。
|
||||
|
||||
**校验码算法**
|
||||
|
||||
   校验码算法使用ModBus CRC16方法计算。
|
||||
|
|
|
@ -38,7 +38,6 @@ public:
|
|||
const unsigned int timeoutMs = DEFAULT_ASK_TIMEOUT);
|
||||
virtual ~McuAskBase() = default;
|
||||
ASK_RESULT Blocking(void) override;
|
||||
// void StopBlocking(void) override;
|
||||
bool NeedReply(void) override;
|
||||
void ReplyFinished(const bool result) override;
|
||||
bool IfTimeout(const unsigned int &integrationTimeMs) override;
|
||||
|
|
|
@ -6,6 +6,7 @@ set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH})
|
|||
include_directories(
|
||||
./src
|
||||
./include
|
||||
${MIDDLEWARE_SOURCE_PATH}/McuAskBase/include
|
||||
${UTILS_SOURCE_PATH}/StatusCode/include
|
||||
${UTILS_SOURCE_PATH}/Log/include
|
||||
${UTILS_SOURCE_PATH}/McuProtocol/include
|
||||
|
@ -29,7 +30,7 @@ aux_source_directory(./src SRC_FILES)
|
|||
set(TARGET_NAME McuManager)
|
||||
add_library(${TARGET_NAME} STATIC ${SRC_FILES})
|
||||
|
||||
target_link_libraries(${TARGET_NAME} UartDevice McuProtocol StatusCode Log)
|
||||
target_link_libraries(${TARGET_NAME} UartDevice McuAskBase McuProtocol StatusCode Log)
|
||||
|
||||
if ("${CLANG_TIDY_SUPPORT}" MATCHES "true")
|
||||
add_custom_target(
|
||||
|
|
|
@ -81,6 +81,7 @@ class VMcuMonitor
|
|||
public:
|
||||
VMcuMonitor() = default;
|
||||
virtual ~VMcuMonitor() = default;
|
||||
virtual void RecvIpcMissionEvent(std::shared_ptr<VMcuAsk> &recv, const IpcMission &mission) {}
|
||||
};
|
||||
class IMcuManager
|
||||
{
|
||||
|
|
|
@ -77,6 +77,17 @@ size_t McuDevice::WriteData(const void *buff, const size_t buffLength, std::shar
|
|||
const unsigned int &serialNumber)
|
||||
{
|
||||
constexpr int WRITE_ERROR = -1;
|
||||
size_t length = WRITE_ERROR;
|
||||
if (context.get() == nullptr) {
|
||||
/**
|
||||
* @brief A null context pointer indicates that the data is the reply data.
|
||||
*
|
||||
*/
|
||||
mMutex.lock();
|
||||
length = IUartSend(mUartDevice, buff, buffLength);
|
||||
mMutex.unlock();
|
||||
return length;
|
||||
}
|
||||
std::shared_ptr<ProtocolContext<std::shared_ptr<VMcuAsk>>> ctx =
|
||||
std::dynamic_pointer_cast<ProtocolContext<std::shared_ptr<VMcuAsk>>>(context);
|
||||
if (!ctx) {
|
||||
|
@ -86,7 +97,7 @@ size_t McuDevice::WriteData(const void *buff, const size_t buffLength, std::shar
|
|||
ask->mSerialNumber = serialNumber;
|
||||
mMutex.lock();
|
||||
AddMcuAsk(ask);
|
||||
size_t length = IUartSend(mUartDevice, buff, buffLength);
|
||||
length = IUartSend(mUartDevice, buff, buffLength);
|
||||
mMutex.unlock();
|
||||
if (ask->NeedReply() == true) {
|
||||
ask->Blocking();
|
||||
|
|
|
@ -13,6 +13,8 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
#include "McuManagerImpl.h"
|
||||
#include "ILog.h"
|
||||
#include "UartRecvAsk.h"
|
||||
std::shared_ptr<VProtocolBase> McuManagerImpl::SharedFromThis(void) { return shared_from_this(); }
|
||||
const StatusCode McuManagerImpl::Init(void)
|
||||
{
|
||||
|
@ -26,6 +28,11 @@ const StatusCode McuManagerImpl::UnInit(void)
|
|||
McuProtocol::UnInit();
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
||||
const StatusCode McuManagerImpl::SetMcuMonitor(std::shared_ptr<VMcuMonitor> &monitor)
|
||||
{
|
||||
mMonitor = monitor;
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
||||
const StatusCode McuManagerImpl::GetIpcMission(std::shared_ptr<VMcuAsk> &ask)
|
||||
{
|
||||
std::shared_ptr<VProtocolContext> context = std::make_shared<ProtocolContext<std::shared_ptr<VMcuAsk>>>(ask);
|
||||
|
@ -57,4 +64,44 @@ const StatusCode McuManagerImpl::SetPirSensitivity(std::shared_ptr<VMcuAsk> &ask
|
|||
{
|
||||
std::shared_ptr<VProtocolContext> context = std::make_shared<ProtocolContext<std::shared_ptr<VMcuAsk>>>(ask);
|
||||
return McuProtocol::SetPirSensitivity(sensitivity, context);
|
||||
}
|
||||
std::shared_ptr<VMcuMonitor> McuManagerImpl::GetMcuMonitor(void)
|
||||
{
|
||||
auto monitor = mMonitor.lock();
|
||||
if (!monitor) {
|
||||
LogWarning("mMonitor is nullptr.\n");
|
||||
return nullptr;
|
||||
}
|
||||
if (mMonitor.expired()) {
|
||||
LogWarning("mMonitor shared ptr expired failed.\n");
|
||||
return nullptr;
|
||||
}
|
||||
return monitor;
|
||||
}
|
||||
void McuManagerImpl::OtherSideSendIpcMission(const unsigned int &serialNumber, const unsigned char &mission)
|
||||
{
|
||||
class OtherSideSend : public UartRecvAsk, public McuAsk<ASK_RESULT>
|
||||
{
|
||||
public:
|
||||
OtherSideSend(std::shared_ptr<McuManagerImpl> &mcuManager) : mMcuManager(mcuManager) {}
|
||||
~OtherSideSend() = default;
|
||||
void ReplyFinished(const bool result) override
|
||||
{
|
||||
if (result) {
|
||||
mMcuManager->ReplyOtherSideSendIpcMission(mDataReply);
|
||||
}
|
||||
}
|
||||
std::shared_ptr<McuManagerImpl> mMcuManager;
|
||||
};
|
||||
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);
|
||||
monitor->RecvIpcMissionEvent(ask, static_cast<IpcMission>(mission));
|
||||
}
|
||||
}
|
||||
void McuManagerImpl::ReplyOtherSideSendIpcMission(const ASK_RESULT &result)
|
||||
{
|
||||
LogInfo("ReplyOtherSideSendIpcMission\n");
|
||||
return McuProtocol::ReplyOtherSideSendIpcMission(static_cast<ReplyResult>(result));
|
||||
}
|
|
@ -25,6 +25,7 @@ public:
|
|||
std::shared_ptr<VProtocolBase> SharedFromThis(void) override;
|
||||
const StatusCode Init(void) override;
|
||||
const StatusCode UnInit(void) override;
|
||||
const StatusCode SetMcuMonitor(std::shared_ptr<VMcuMonitor> &monitor) override;
|
||||
const StatusCode GetIpcMission(std::shared_ptr<VMcuAsk> &ask) override;
|
||||
const StatusCode CutOffPowerSupply(std::shared_ptr<VMcuAsk> &ask) override;
|
||||
const StatusCode FeedWatchDog(std::shared_ptr<VMcuAsk> &ask) override;
|
||||
|
@ -32,5 +33,15 @@ public:
|
|||
const unsigned char &min, const unsigned char &second) override;
|
||||
const StatusCode SetDateTime(std::shared_ptr<VMcuAsk> &ask, const McuAskDateTime &value) override;
|
||||
const StatusCode SetPirSensitivity(std::shared_ptr<VMcuAsk> &ask, const unsigned char &sensitivity) override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<VMcuMonitor> GetMcuMonitor(void);
|
||||
|
||||
private:
|
||||
void OtherSideSendIpcMission(const unsigned int &serialNumber, const unsigned char &mission) override;
|
||||
void ReplyOtherSideSendIpcMission(const ASK_RESULT &result);
|
||||
|
||||
private:
|
||||
std::weak_ptr<VMcuMonitor> mMonitor;
|
||||
};
|
||||
#endif
|
19
middleware/McuManager/src/UartRecvAsk.cpp
Normal file
19
middleware/McuManager/src/UartRecvAsk.cpp
Normal file
|
@ -0,0 +1,19 @@
|
|||
/*
|
||||
* 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 "UartRecvAsk.h"
|
||||
UartRecvAsk::UartRecvAsk() : McuAskBase(McuAskBlock::NOT_BLOCK, McuAskReply::NEED_REPLY)
|
||||
{
|
||||
//
|
||||
}
|
24
middleware/McuManager/src/UartRecvAsk.h
Normal file
24
middleware/McuManager/src/UartRecvAsk.h
Normal file
|
@ -0,0 +1,24 @@
|
|||
/*
|
||||
* 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 OTHER_SIDE_ASK_H
|
||||
#define OTHER_SIDE_ASK_H
|
||||
#include "McuAskBase.h"
|
||||
class UartRecvAsk : public McuAskBase
|
||||
{
|
||||
public:
|
||||
UartRecvAsk();
|
||||
virtual ~UartRecvAsk() = default;
|
||||
};
|
||||
#endif
|
|
@ -81,40 +81,31 @@ TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_GetIpcMission)
|
|||
IMcuManager::GetInstance()->UnInit();
|
||||
}
|
||||
// ../output_files/test/bin/McuManagerTest
|
||||
// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_EXAMPLE_GetIpcMission2
|
||||
// --gtest_filter=McuManagerMockTest.INTEGRATION_McuManager_EXAMPLE_OtherSideSendIpcMission
|
||||
/**
|
||||
* @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.
|
||||
* 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.
|
||||
*/
|
||||
TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_GetIpcMission2)
|
||||
TEST_F(McuManagerMockTest, INTEGRATION_McuManager_EXAMPLE_OtherSideSendIpcMission)
|
||||
{
|
||||
/**
|
||||
* @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
|
||||
class MonitorTest : public VMcuMonitor
|
||||
{
|
||||
public:
|
||||
McuAskTest() : McuAskBase(McuAskBlock::NOT_BLOCK, McuAskReply::NEED_REPLY) {} // using McuAskBlock::NOT_BLOCK
|
||||
virtual ~McuAskTest() = default;
|
||||
void ReplyFinished(const bool result) override
|
||||
MonitorTest() = default;
|
||||
virtual ~MonitorTest() = default;
|
||||
void RecvIpcMissionEvent(std::shared_ptr<VMcuAsk> &recv, const IpcMission &mission) 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");
|
||||
}
|
||||
LogInfo("RecvIpcMissionEvent\n");
|
||||
std::shared_ptr<McuAsk<ASK_RESULT>> ask = std::dynamic_pointer_cast<McuAsk<ASK_RESULT>>(recv);
|
||||
ask->mDataReply = ASK_RESULT::SUCCEED;
|
||||
recv->ReplyFinished(true);
|
||||
}
|
||||
};
|
||||
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");
|
||||
OtherSideAskIpcMission(mLinuxTest);
|
||||
std::shared_ptr<VMcuMonitor> monitor = std::make_shared<MonitorTest>();
|
||||
IMcuManager::GetInstance()->SetMcuMonitor(monitor);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
|
||||
IMcuManager::GetInstance()->UnInit();
|
||||
}
|
||||
|
|
|
@ -64,6 +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);
|
||||
|
||||
private:
|
||||
std::shared_ptr<McuManagerImplTest> mMcuManagerMock;
|
||||
|
|
|
@ -54,4 +54,8 @@ void McuManagerTestTool::UnInit(void)
|
|||
bool McuManagerTestTool::CheckAskExist(const std::shared_ptr<VMcuAsk> &ask)
|
||||
{
|
||||
return mMcuManagerMock->CheckAskExist(ask);
|
||||
}
|
||||
void McuManagerTestTool::OtherSideAskIpcMission(std::shared_ptr<LinuxTest> &mock)
|
||||
{
|
||||
McuProtocolTestTool::OtherSideAskIpcMission(mock);
|
||||
}
|
|
@ -31,6 +31,7 @@ public:
|
|||
virtual ~McuProtocolTestTool() = default;
|
||||
void Init(std::shared_ptr<LinuxTest> &mock, const UartInfo &uart);
|
||||
void UnInit(void);
|
||||
void OtherSideAskIpcMission(std::shared_ptr<LinuxTest> &mock);
|
||||
|
||||
private:
|
||||
void CheckSerialNumber(const void *buf, const size_t &count);
|
||||
|
@ -55,6 +56,10 @@ private:
|
|||
void UnlockThread(void);
|
||||
void PipeSelectTimeoutForProtocolHandleImmediately(void);
|
||||
|
||||
private:
|
||||
void OtherSideAskIpcMissionHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd);
|
||||
void OtherSideAskIpcMissionInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd);
|
||||
|
||||
private:
|
||||
static void PrintHexadecimalData(const void *buf, const size_t &bufLength, const int event);
|
||||
|
||||
|
@ -67,5 +72,6 @@ private:
|
|||
bool mPipeFdMockSelectInit;
|
||||
int mPipeFdMockSelect[2];
|
||||
std::list<ProtocolHandleFunc> mProtocolHandle;
|
||||
int mUartFd;
|
||||
};
|
||||
#endif
|
|
@ -25,6 +25,7 @@ using std::placeholders::_3;
|
|||
using std::placeholders::_4;
|
||||
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;
|
||||
|
@ -45,12 +46,17 @@ unsigned char ASK_SET_PIR_SENSITIVITY[] = {
|
|||
0xFA, 0xC1, 0x00, 0x00, 0x00, 0x01, 0x81, 0x08, 0x00, 0x0D, 0x09, 0xFF, 0xFF};
|
||||
unsigned char REPLY_SET_PIR_SENSITIVITY[] = {
|
||||
0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x01, 0x08, 0x00, 0x0D, 0x01, 0xFF, 0xFF};
|
||||
unsigned char REPLY_OTHER_SIDE_ASK_SEND_IPC_MISSION[] = {
|
||||
0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0xC1, 0x01, 0x00, 0x0D, 0x0A, 0xFF, 0xFF};
|
||||
unsigned char OTHER_SIDE_ASK_SEND_IPC_MISSION[] = {
|
||||
0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x41, 0x01, 0x00, 0x0D, 0x01, 0xFF, 0xFF};
|
||||
McuProtocolTestTool::McuProtocolTestTool()
|
||||
{
|
||||
mThreadRuning = false;
|
||||
mPipeFdMockSelectInit = false;
|
||||
mPipeFdMockSelect[PIPE_READ_FD_INDEX] = -1;
|
||||
mPipeFdMockSelect[PIPE_WRITE_FD_INDEX] = -1;
|
||||
mUartFd = INVALID_FD;
|
||||
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));
|
||||
|
@ -67,6 +73,7 @@ void McuProtocolTestTool::Init(std::shared_ptr<LinuxTest> &mock, const UartInfo
|
|||
LogWarning("pipe failed, mPipeFdMockSelect willn,t work.\n");
|
||||
}
|
||||
int uartFd = GetDeviceMockFd(uart);
|
||||
mUartFd = uartFd;
|
||||
static size_t WRITE_COUNT = -1;
|
||||
auto api_write = [=, &mock](int fd, const void *buf, size_t count) {
|
||||
McuProtocolTestTool::PrintHexadecimalData(buf, count, WRITE_PRINT);
|
||||
|
@ -98,6 +105,11 @@ void McuProtocolTestTool::UnInit(void)
|
|||
mPipeFdMockSelect[PIPE_WRITE_FD_INDEX] = -1;
|
||||
}
|
||||
mPipeFdMockSelectInit = false;
|
||||
mUartFd = INVALID_FD;
|
||||
}
|
||||
void McuProtocolTestTool::OtherSideAskIpcMission(std::shared_ptr<LinuxTest> &mock)
|
||||
{
|
||||
OtherSideAskIpcMissionHandle(mock, mUartFd);
|
||||
}
|
||||
void McuProtocolTestTool::CheckSerialNumber(const void *buf, const size_t &count)
|
||||
{
|
||||
|
@ -415,6 +427,38 @@ void McuProtocolTestTool::PipeSelectTimeoutForProtocolHandleImmediately(void)
|
|||
}
|
||||
}
|
||||
}
|
||||
void McuProtocolTestTool::OtherSideAskIpcMissionHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd)
|
||||
{
|
||||
LogInfo("OtherSideAskIpcMissionHandle\n");
|
||||
auto handle = [=, &mock](McuProtocolTestTool *testTool) { testTool->OtherSideAskIpcMissionInit(mock, uartFd); };
|
||||
if (mLockThread.joinable()) {
|
||||
mLockThread.join();
|
||||
}
|
||||
mLockThread = std::thread(handle, this);
|
||||
}
|
||||
void McuProtocolTestTool::OtherSideAskIpcMissionInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd)
|
||||
{
|
||||
LockProtocolHandle();
|
||||
unsigned int serialNumber = 99;
|
||||
serialNumber = htonl(serialNumber);
|
||||
memcpy(OTHER_SIDE_ASK_SEND_IPC_MISSION + 2, &serialNumber, 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;
|
||||
auto apiReadKeyHead = [=](int fd, void *buf, size_t count) {
|
||||
memcpy(buf, OTHER_SIDE_ASK_SEND_IPC_MISSION, 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);
|
||||
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::PrintHexadecimalData(const void *buf, const size_t &bufLength, const int event)
|
||||
{
|
||||
if (WRITE_PRINT == event) {
|
||||
|
|
|
@ -75,26 +75,41 @@ protected:
|
|||
|
||||
public:
|
||||
};
|
||||
enum ReplyResult
|
||||
enum class ReplyResult
|
||||
{
|
||||
SUCCEED = 1,
|
||||
FAILED,
|
||||
END
|
||||
};
|
||||
class VProtocolRecv
|
||||
class OtherSideReply
|
||||
{
|
||||
public:
|
||||
OtherSideReply() = default;
|
||||
virtual ~OtherSideReply() = default;
|
||||
virtual void GetIpcMissionReply(const unsigned int &serialNumber, const unsigned char &mission) {}
|
||||
virtual void OnlyResultReply(const unsigned int &serialNumber, const ReplyResult &result) {}
|
||||
};
|
||||
class OtherSideAsk
|
||||
{
|
||||
public:
|
||||
OtherSideAsk() = default;
|
||||
virtual ~OtherSideAsk() = default;
|
||||
virtual void OtherSideSendIpcMission(const unsigned int &serialNumber, const unsigned char &mission) {}
|
||||
};
|
||||
class VProtocolRecv : public OtherSideReply, public OtherSideAsk
|
||||
{
|
||||
public:
|
||||
VProtocolRecv() = default;
|
||||
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) {}
|
||||
};
|
||||
class McuProtocol : virtual public VProtocolBase
|
||||
{
|
||||
public:
|
||||
McuProtocol() = default;
|
||||
virtual ~McuProtocol() = default;
|
||||
|
||||
protected:
|
||||
const StatusCode Init(void);
|
||||
const StatusCode UnInit(void);
|
||||
const StatusCode GetIpcMission(std::shared_ptr<VProtocolContext> &context);
|
||||
|
@ -109,6 +124,9 @@ public:
|
|||
const StatusCode SetPirSensitivity(const unsigned char &sensitivity, std::shared_ptr<VProtocolContext> &context);
|
||||
void DataHandleThread(void);
|
||||
|
||||
protected:
|
||||
void ReplyOtherSideSendIpcMission(const ReplyResult &result);
|
||||
|
||||
protected:
|
||||
size_t GetKeyHeadLength(void) override;
|
||||
StatusCode GetDataLength(const void *keyHead, const size_t &headLength, size_t &dataLength) override;
|
||||
|
|
|
@ -126,6 +126,15 @@ void McuProtocol::DataHandleThread(void)
|
|||
free((void *)packet.mBuf);
|
||||
}
|
||||
}
|
||||
void McuProtocol::ReplyOtherSideSendIpcMission(const ReplyResult &result)
|
||||
{
|
||||
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));
|
||||
std::shared_ptr<ProtocolHandle> handle = ProtocolHandle::CreateProtocolData(param);
|
||||
WriteProtocolData(
|
||||
handle->GetProtocolDataBuff(), handle->GetProtocolDataLength(), NULL_CONTEXT, handle->GetSerialNumber());
|
||||
}
|
||||
size_t McuProtocol::GetKeyHeadLength(void) { return ProtocolHandle::GetKeyHeadLength(); }
|
||||
StatusCode McuProtocol::GetDataLength(const void *keyHead, const size_t &headLength, size_t &dataLength)
|
||||
{
|
||||
|
|
|
@ -36,6 +36,8 @@ ProtocolHandle::ProtocolHandle(const std::shared_ptr<VProtocolParam> ¶m) : m
|
|||
mMakePacketFunc[ASK_SET_FEEDING_CYCLE] = std::bind(&ProtocolHandle::MakeAskSetFeedingCyclePacket, this, _1);
|
||||
mMakePacketFunc[ASK_SET_DATE_TIME] = std::bind(&ProtocolHandle::MakeAskSetDateTimePacket, this, _1);
|
||||
mMakePacketFunc[ASK_SET_PIR_SENSITIVITY] = std::bind(&ProtocolHandle::MakeAskSetPirSensitivityPacket, this, _1);
|
||||
mMakePacketFunc[REPLY_OTHER_SIDE_ASK_SEND_IPC_MISSION] =
|
||||
std::bind(&ProtocolHandle::MakeReplyOtherSideSendIpcMissionPacket, this, _1);
|
||||
}
|
||||
ProtocolHandle::ProtocolHandle(const void *data, const size_t &length)
|
||||
{
|
||||
|
@ -48,6 +50,8 @@ ProtocolHandle::ProtocolHandle(const void *data, const size_t &length)
|
|||
mAnalyzePacketFunc[REPLY_SET_FEEDING_CYCLE] = std::bind(&ProtocolHandle::AnalyzeReplyResultPacket, this, _1);
|
||||
mAnalyzePacketFunc[REPLY_SET_DATE_TIME] = std::bind(&ProtocolHandle::AnalyzeReplyResultPacket, this, _1);
|
||||
mAnalyzePacketFunc[REPLY_SET_PIR_SENSITIVITY] = std::bind(&ProtocolHandle::AnalyzeReplyResultPacket, this, _1);
|
||||
mAnalyzePacketFunc[OTHER_SIDE_ASK_SEND_IPC_MISSION] =
|
||||
std::bind(&ProtocolHandle::AnalyzeOtherSideSendIpcMissionPacket, this, _1);
|
||||
}
|
||||
ProtocolHandle::~ProtocolHandle()
|
||||
{
|
||||
|
@ -144,6 +148,10 @@ void ProtocolHandle::MakeAskSetPirSensitivityPacket(const std::shared_ptr<VProto
|
|||
{
|
||||
MakeProtocolData<const unsigned char>(param);
|
||||
}
|
||||
void ProtocolHandle::MakeReplyOtherSideSendIpcMissionPacket(const std::shared_ptr<VProtocolParam> ¶m)
|
||||
{
|
||||
MakeProtocolData<const unsigned char>(param);
|
||||
}
|
||||
void ProtocolHandle::AnalyzeProtocolPacket(void)
|
||||
{
|
||||
ProtocolPacket packet = {0};
|
||||
|
@ -179,7 +187,7 @@ unsigned char ProtocolHandle::ReplyOneBytePacketResult(const ProtocolPacket &pac
|
|||
constexpr unsigned int PROTOCOL_DATA_START_ADDRESS = KEY_HEAD_LENGTH;
|
||||
unsigned char replyResult = UNKNOWN_RESULT;
|
||||
replyResult = mProtocolData[PROTOCOL_DATA_START_ADDRESS];
|
||||
LogInfo("reply result = 0x%02X\n", replyResult);
|
||||
LogInfo("Other side send: result = 0x%02X\n", replyResult);
|
||||
return replyResult;
|
||||
}
|
||||
void ProtocolHandle::AnalyzeReplyResultPacket(const ProtocolPacket &packet)
|
||||
|
@ -194,6 +202,12 @@ void ProtocolHandle::AnalyzeReplyIpcMissionPacket(const ProtocolPacket &packet)
|
|||
unsigned char ipcMission = ReplyOneBytePacketResult(packet);
|
||||
VProtocolRecv::GetInstance()->GetIpcMissionReply(mProtocolSerialNumber, ipcMission);
|
||||
}
|
||||
void ProtocolHandle::AnalyzeOtherSideSendIpcMissionPacket(const ProtocolPacket &packet)
|
||||
{
|
||||
LogInfo("AnalyzeOtherSideSendIpcMissionPacket\n");
|
||||
unsigned char ipcMission = ReplyOneBytePacketResult(packet);
|
||||
VProtocolRecv::GetInstance()->OtherSideSendIpcMission(mProtocolSerialNumber, ipcMission);
|
||||
}
|
||||
bool ProtocolHandle::CheckoutTheCheckCode(const ProtocolPacket &packet)
|
||||
{
|
||||
short code = calculate_check_sum(mProtocolData, mProtocolDataLength - CHECK_CODE_LENGTH);
|
||||
|
|
|
@ -47,6 +47,12 @@ enum PROTOCOL_COMMAND
|
|||
REPLY_SET_DATE_TIME = 0x0107,
|
||||
ASK_SET_PIR_SENSITIVITY = 0x8108,
|
||||
REPLY_SET_PIR_SENSITIVITY = 0x0108,
|
||||
/**
|
||||
* @brief The protocol starting from here is a request sent from the other end.
|
||||
*
|
||||
*/
|
||||
REPLY_OTHER_SIDE_ASK_SEND_IPC_MISSION = 0xC101,
|
||||
OTHER_SIDE_ASK_SEND_IPC_MISSION = 0x4101,
|
||||
PROTOCOL_COMMAND_END
|
||||
};
|
||||
constexpr unsigned char ZERO_MEANS_SHUTDOWN_WATCH_DOG = 0x00;
|
||||
|
@ -129,6 +135,7 @@ private:
|
|||
void MakeAskSetFeedingCyclePacket(const std::shared_ptr<VProtocolParam> ¶m);
|
||||
void MakeAskSetDateTimePacket(const std::shared_ptr<VProtocolParam> ¶m);
|
||||
void MakeAskSetPirSensitivityPacket(const std::shared_ptr<VProtocolParam> ¶m);
|
||||
void MakeReplyOtherSideSendIpcMissionPacket(const std::shared_ptr<VProtocolParam> ¶m);
|
||||
template <typename T>
|
||||
void MakeProtocolData(const std::shared_ptr<VProtocolParam> ¶m)
|
||||
{
|
||||
|
@ -150,6 +157,7 @@ private:
|
|||
unsigned char ReplyOneBytePacketResult(const ProtocolPacket &packet);
|
||||
void AnalyzeReplyResultPacket(const ProtocolPacket &packet);
|
||||
void AnalyzeReplyIpcMissionPacket(const ProtocolPacket &packet);
|
||||
void AnalyzeOtherSideSendIpcMissionPacket(const ProtocolPacket &packet);
|
||||
|
||||
private:
|
||||
virtual void BigEndianConversion(ProtocolPacket &packet) {}
|
||||
|
|
Loading…
Reference in New Issue
Block a user