Improve:mcu protocol code.
This commit is contained in:
parent
4b88568d15
commit
5711ce11eb
|
@ -173,6 +173,11 @@ void McuDevice::DeviceRecvThread(void)
|
|||
recvTotalLength += recvLength;
|
||||
if (keyHeadLength == recvTotalLength) {
|
||||
PrintHexadecimalData(keyHeadBuf, keyHeadLength, "Recv head:");
|
||||
StatusCode code = CheckHeader(keyHeadBuf, recvTotalLength);
|
||||
if (IsCodeOK(code) == false) {
|
||||
LogWarning("Check header failed, header data wrong.\n");
|
||||
continue;
|
||||
}
|
||||
DeviceRecvData(keyHeadBuf, keyHeadLength);
|
||||
memset(keyHeadBuf, 0, keyHeadLength);
|
||||
recvTotalLength = 0;
|
||||
|
@ -192,12 +197,15 @@ void McuDevice::DeviceRecvData(const char *keyHead, const size_t headLength)
|
|||
constexpr int RECV_TIMEOUT_MS = 1000;
|
||||
size_t recvTotalLength = 0;
|
||||
size_t dataLength = 0;
|
||||
// const size_t keyHeadLength = GetKeyHeadLength();
|
||||
const StatusCode code = GetDataLength(keyHead, headLength, dataLength);
|
||||
if (IsCodeOK(code) == false || 0 == dataLength || dataLength <= headLength) {
|
||||
LogError("Recv data error.\n");
|
||||
return;
|
||||
}
|
||||
if (dataLength > 0xFF) {
|
||||
LogWarning("Recv data length is too large, discard it.\n");
|
||||
return;
|
||||
}
|
||||
char *dataBuf = (char *)malloc(dataLength);
|
||||
if (nullptr == dataBuf) {
|
||||
LogError("malloc failed, DeviceRecvData return.\n");
|
||||
|
|
48
test/middleware/McuManager/src_mock/McuManagerMockTest.cpp
Normal file
48
test/middleware/McuManager/src_mock/McuManagerMockTest.cpp
Normal file
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
* 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 "McuManagerMockTest.h"
|
||||
McuManagerMockTest::McuManagerMockTest()
|
||||
{
|
||||
}
|
||||
McuManagerMockTest::~McuManagerMockTest()
|
||||
{
|
||||
}
|
||||
void McuManagerMockTest::SetUpTestCase()
|
||||
{
|
||||
CreateLogModule();
|
||||
ILogInit(LOG_INSTANCE_TYPE_END);
|
||||
}
|
||||
void McuManagerMockTest::TearDownTestCase()
|
||||
{
|
||||
ILogUnInit();
|
||||
}
|
||||
void McuManagerMockTest::SetUp()
|
||||
{
|
||||
mLinuxTest = LinuxTest::CreateLinuxTest();
|
||||
std::shared_ptr<LinuxApiMock> test = mLinuxTest;
|
||||
LinuxApiMock::GetInstance(&test);
|
||||
LinuxApiMock::GetInstance()->Init();
|
||||
McuManagerTestTool::Init(mLinuxTest);
|
||||
CreateMcuManager();
|
||||
}
|
||||
void McuManagerMockTest::TearDown()
|
||||
{
|
||||
LinuxApiMock::GetInstance()->UnInit();
|
||||
mLinuxTest = std::make_shared<LinuxTest>();
|
||||
std::shared_ptr<LinuxApiMock> test = std::make_shared<LinuxApiMock>();
|
||||
LinuxApiMock::GetInstance(&test);
|
||||
McuManagerTestTool::UnInit();
|
||||
DestroyMcuManager();
|
||||
}
|
36
test/middleware/McuManager/src_mock/McuManagerMockTest.h
Normal file
36
test/middleware/McuManager/src_mock/McuManagerMockTest.h
Normal file
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* 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 MCU_MANAGER_MOCK_TEST_H
|
||||
#define MCU_MANAGER_MOCK_TEST_H
|
||||
#include "ILog.h"
|
||||
#include "IMcuManager.h"
|
||||
#include "LinuxApiMock.h"
|
||||
#include "McuManagerTestTool.h"
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
class McuManagerMockTest : public testing::Test, public McuManagerTestTool
|
||||
{
|
||||
public:
|
||||
McuManagerMockTest();
|
||||
virtual ~McuManagerMockTest();
|
||||
static void SetUpTestCase();
|
||||
static void TearDownTestCase();
|
||||
virtual void SetUp();
|
||||
virtual void TearDown();
|
||||
|
||||
public:
|
||||
std::shared_ptr<LinuxTest> mLinuxTest;
|
||||
};
|
||||
#endif
|
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* 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 "McuAskBase.h"
|
||||
#include "McuAskBaseTestTool.h"
|
||||
#include "McuManagerMockTest.h"
|
||||
#include <thread>
|
||||
const unsigned char McuRecvData[] = {
|
||||
0xFA, 0xC1, 0x00, 0x00, 0x00, 0x08, 0x41, 0x01, 0x00, 0x0A, 0x03, 0x08, 0xEC, 0xFA, 0xC1, 0x00, 0x00, 0x00,
|
||||
0x0A, 0x41, 0x03, 0x08, 0xEC, 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x0A, 0x41, 0x07, 0x00, 0x0C, 0x35, 0x88, 0xFA,
|
||||
0xC1, 0x00, 0x03, 0x08, 0xEC, 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x0A, 0x41, 0x07, 0x00, 0x0C, 0x35, 0x88, 0xFA,
|
||||
0xC1, 0x00, 0x00, 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x0A, 0x03, 0x08, 0xEC, 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x0A,
|
||||
0x41, 0x07, 0x00, 0x0C, 0x35, 0x88, 0xFA, 0xC1, 0x00, 0x00, 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x0A};
|
||||
namespace McuManager_AbnormalData_Test
|
||||
{
|
||||
/**
|
||||
* @brief Construct a new test f object
|
||||
* ../output_files/test/bin/McuManagerTest
|
||||
* --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_AUTO_ProtocolLengthIsWrong
|
||||
*/
|
||||
TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_ProtocolLengthIsWrong)
|
||||
{
|
||||
IMcuManager::GetInstance()->Init();
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
McuManagerTestTool::MockOtherSideSendData(mLinuxTest, McuRecvData, sizeof(McuRecvData));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
||||
IMcuManager::GetInstance()->UnInit();
|
||||
}
|
||||
} // namespace McuManager_AbnormalData_Test
|
|
@ -12,57 +12,12 @@
|
|||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#include "ILog.h"
|
||||
#include "IMcuManager.h"
|
||||
#include "LinuxApiMock.h"
|
||||
#include "McuAskBase.h"
|
||||
#include "McuAskBaseTestTool.h"
|
||||
#include "McuManagerTestTool.h"
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include "McuManagerMockTest.h"
|
||||
#include <thread>
|
||||
namespace McuManagerMockTest
|
||||
namespace McuManager_Mock_Test
|
||||
{
|
||||
class McuManagerMockTest : public testing::Test, public McuManagerTestTool
|
||||
{
|
||||
public:
|
||||
McuManagerMockTest()
|
||||
{
|
||||
}
|
||||
virtual ~McuManagerMockTest()
|
||||
{
|
||||
}
|
||||
static void SetUpTestCase()
|
||||
{
|
||||
CreateLogModule();
|
||||
ILogInit(LOG_INSTANCE_TYPE_END);
|
||||
}
|
||||
static void TearDownTestCase()
|
||||
{
|
||||
ILogUnInit();
|
||||
}
|
||||
virtual void SetUp()
|
||||
{
|
||||
mLinuxTest = LinuxTest::CreateLinuxTest();
|
||||
std::shared_ptr<LinuxApiMock> test = mLinuxTest;
|
||||
LinuxApiMock::GetInstance(&test);
|
||||
LinuxApiMock::GetInstance()->Init();
|
||||
McuManagerTestTool::Init(mLinuxTest);
|
||||
CreateMcuManager();
|
||||
}
|
||||
virtual void TearDown()
|
||||
{
|
||||
LinuxApiMock::GetInstance()->UnInit();
|
||||
mLinuxTest = std::make_shared<LinuxTest>();
|
||||
std::shared_ptr<LinuxApiMock> test = std::make_shared<LinuxApiMock>();
|
||||
LinuxApiMock::GetInstance(&test);
|
||||
McuManagerTestTool::UnInit();
|
||||
DestroyMcuManager();
|
||||
}
|
||||
|
||||
public:
|
||||
std::shared_ptr<LinuxTest> mLinuxTest;
|
||||
};
|
||||
// ../output_files/test/bin/McuManagerTest
|
||||
// --gtest_filter=McuManagerMockTest.HS_INTEGRATION_McuManager_EXAMPLE_GetIpcMission
|
||||
/**
|
||||
|
@ -1154,4 +1109,4 @@ TEST_F(McuManagerMockTest, HS_INTEGRATION_McuManager_AUTO_OtherSideGetPirSensiti
|
|||
std::this_thread::sleep_for(std::chrono::milliseconds(2000));
|
||||
IMcuManager::GetInstance()->UnInit();
|
||||
}
|
||||
} // namespace McuManagerMockTest
|
||||
} // namespace McuManager_Mock_Test
|
|
@ -31,14 +31,17 @@ class McuManagerTestTool : virtual public McuProtocolTestTool
|
|||
public:
|
||||
McuManagerTestTool() = default;
|
||||
virtual ~McuManagerTestTool() = default;
|
||||
bool CheckAskExist(const std::shared_ptr<VMcuAsk> &ask);
|
||||
|
||||
protected:
|
||||
void Init(std::shared_ptr<LinuxTest> &mock);
|
||||
void UnInit(void);
|
||||
bool CheckAskExist(const std::shared_ptr<VMcuAsk> &ask);
|
||||
void MockOtherSideAskIpcMission(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
|
||||
void MockOtherSideAskHeartBeat(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
|
||||
void MockOtherSideGetIntervalStart(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
|
||||
void MockOtherSideGetDateTime(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
|
||||
void MockOtherSideGetPriSensitivity(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
|
||||
void MockOtherSideSendData(std::shared_ptr<LinuxTest> &mock, const void *data, const size_t &size);
|
||||
void MockMcuDeviceOpenFailed(std::shared_ptr<LinuxTest> &mock);
|
||||
void MockMcuDeviceOpenSuccessButReadNothing(std::shared_ptr<LinuxTest> &mock);
|
||||
|
||||
|
|
|
@ -66,6 +66,10 @@ void McuManagerTestTool::MockOtherSideGetPriSensitivity(std::shared_ptr<LinuxTes
|
|||
{
|
||||
McuProtocolTestTool::MockOtherSideAskGetPirSensitivity(mock, serialNumber);
|
||||
}
|
||||
void McuManagerTestTool::MockOtherSideSendData(std::shared_ptr<LinuxTest> &mock, const void *data, const size_t &size)
|
||||
{
|
||||
McuProtocolTestTool::MockOtherSideAskSendAnyData(mock, data, size);
|
||||
}
|
||||
void McuManagerTestTool::MockMcuDeviceOpenFailed(std::shared_ptr<LinuxTest> &mock)
|
||||
{
|
||||
UartDeviceTestTool::SetUartDeviceOpenFailed(mock, gUartDevice);
|
||||
|
|
|
@ -36,7 +36,9 @@ public:
|
|||
void MockOtherSideAskGetIntervalStart(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
|
||||
void MockOtherSideAskGetDateTime(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
|
||||
void MockOtherSideAskGetPirSensitivity(std::shared_ptr<LinuxTest> &mock, const unsigned int &serialNumber);
|
||||
void MockOtherSideAskSendAnyData(std::shared_ptr<LinuxTest> &mock, const void *data, const size_t &size);
|
||||
void ReadNothingAnyTime(std::shared_ptr<LinuxTest> &mock);
|
||||
void ReadOnceSelectSucceed(std::shared_ptr<LinuxTest> &mock, const int &uartFd);
|
||||
|
||||
private:
|
||||
void CheckSerialNumber(const void *buf, const size_t &count);
|
||||
|
@ -97,6 +99,10 @@ private:
|
|||
const unsigned int &serialNumber);
|
||||
void OtherSideAskGetPirSensitivityInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
|
||||
const unsigned int &serialNumber);
|
||||
void OtherSideAskSendSomeDataHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *data,
|
||||
const size_t &size);
|
||||
void OtherSideAskSendSomeDataInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd, const void *data,
|
||||
const size_t &size);
|
||||
|
||||
private:
|
||||
static void PrintHexadecimalData(const void *buf, const size_t &bufLength, const char *log);
|
||||
|
|
|
@ -173,6 +173,11 @@ void McuProtocolTestTool::MockOtherSideAskGetPirSensitivity(std::shared_ptr<Linu
|
|||
{
|
||||
OtherSideAskGetPirSensitivityHandle(mock, mUartFd, serialNumber);
|
||||
}
|
||||
void McuProtocolTestTool::MockOtherSideAskSendAnyData(std::shared_ptr<LinuxTest> &mock, const void *data,
|
||||
const size_t &size)
|
||||
{
|
||||
OtherSideAskSendSomeDataHandle(mock, mUartFd, data, size);
|
||||
}
|
||||
void McuProtocolTestTool::ReadNothingAnyTime(std::shared_ptr<LinuxTest> &mock)
|
||||
{
|
||||
static size_t WRITE_COUNT = -1;
|
||||
|
@ -287,6 +292,49 @@ 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();
|
||||
}
|
||||
void McuProtocolTestTool::ReadOnceSelectSucceed(std::shared_ptr<LinuxTest> &mock, const int &uartFd)
|
||||
{
|
||||
auto selectReadable =
|
||||
[=, &mock](int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) {
|
||||
FD_ZERO(readfds);
|
||||
FD_SET(uartFd, readfds);
|
||||
};
|
||||
auto selectTimeOut =
|
||||
[=, &mock](int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) {
|
||||
if (false == mPipeFdMockSelectInit) {
|
||||
long long timeMs = timeout->tv_sec * 1000 + timeout->tv_usec / 1000;
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(timeMs));
|
||||
}
|
||||
else {
|
||||
constexpr int READ_BUF_LENGTH = 256;
|
||||
constexpr int READ_FAILD = -1;
|
||||
char buf[READ_BUF_LENGTH] = {0};
|
||||
int selectResult = -1;
|
||||
fd_set fdsRead;
|
||||
FD_ZERO(&fdsRead);
|
||||
FD_SET(mPipeFdMockSelect[PIPE_READ_FD_INDEX], &fdsRead);
|
||||
selectResult = select(mPipeFdMockSelect[PIPE_READ_FD_INDEX] + 1, &fdsRead, NULL, NULL, timeout);
|
||||
if (selectResult) {
|
||||
if (false == mPipeFdMockSelectInit) {
|
||||
LogWarning("mPipeFdMockSelectInit = false.\n");
|
||||
return;
|
||||
}
|
||||
ssize_t length = read(mPipeFdMockSelect[PIPE_READ_FD_INDEX], buf, READ_BUF_LENGTH);
|
||||
if (READ_FAILD == length) {
|
||||
LogError("mPipeFdMockSelect failed.\n");
|
||||
return;
|
||||
}
|
||||
if ((size_t)length != strlen(gPipeBuf)) {
|
||||
LogWarning("Something wrong happened.\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
EXPECT_CALL(*mock.get(), fx_select(uartFd + 1, _, _, _, _))
|
||||
.WillOnce(DoAll(WithArgs<0, 1, 2, 3, 4>(Invoke(selectReadable)), Return(1)))
|
||||
.WillRepeatedly(DoAll(WithArgs<0, 1, 2, 3, 4>(Invoke(selectTimeOut)), Return(MOCK_SELECT_TIME_OUT)));
|
||||
PipeSelectTimeoutForProtocolHandleImmediately();
|
||||
}
|
||||
void McuProtocolTestTool::ReplySelectTimeOut(std::shared_ptr<LinuxTest> &mock, const int &uartFd)
|
||||
{
|
||||
if (mUart) {
|
||||
|
@ -964,6 +1012,53 @@ void McuProtocolTestTool::OtherSideAskGetPirSensitivityInit(std::shared_ptr<Linu
|
|||
sizeof(REPLY_OTHER_SIDE_ASK_GET_SENSITIVITY_X));
|
||||
}
|
||||
}
|
||||
void McuProtocolTestTool::OtherSideAskSendSomeDataHandle(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
|
||||
const void *data, const size_t &size)
|
||||
{
|
||||
LogInfo("OtherSideAskSendSomeDataHandle\n");
|
||||
auto handle = [=, &mock](McuProtocolTestTool *testTool) {
|
||||
testTool->OtherSideAskSendSomeDataInit(mock, uartFd, data, size);
|
||||
};
|
||||
if (mLockThread.joinable()) {
|
||||
mLockThread.join();
|
||||
}
|
||||
mLockThread = std::thread(handle, this);
|
||||
}
|
||||
void McuProtocolTestTool::OtherSideAskSendSomeDataInit(std::shared_ptr<LinuxTest> &mock, const int &uartFd,
|
||||
const void *data, const size_t &size)
|
||||
{
|
||||
LockProtocolHandle();
|
||||
static size_t totalLength = 0;
|
||||
static size_t leftDataLength = 0;
|
||||
static size_t readResult = 0;
|
||||
totalLength = size;
|
||||
leftDataLength = totalLength;
|
||||
readResult = 0;
|
||||
auto apiReadLeftData = [this, data, &mock, uartFd](int fd, void *buf, size_t count) {
|
||||
if (leftDataLength >= count) {
|
||||
memcpy(buf, (unsigned char *)data + totalLength - leftDataLength, count);
|
||||
readResult = count;
|
||||
leftDataLength = leftDataLength - count;
|
||||
McuProtocolTestTool::PrintHexadecimalData(buf, count, "OtherSideAskSendSomeDataInit read:");
|
||||
}
|
||||
else {
|
||||
memcpy(buf, (unsigned char *)data + totalLength - leftDataLength, leftDataLength);
|
||||
readResult = leftDataLength;
|
||||
leftDataLength = 0;
|
||||
McuProtocolTestTool::PrintHexadecimalData(buf, readResult, "OtherSideAskSendSomeDataInit2 read:");
|
||||
}
|
||||
if (leftDataLength > 0) {
|
||||
this->ReadOnceSelectSucceed(mock, uartFd);
|
||||
}
|
||||
else {
|
||||
EXPECT_CALL(*mock.get(), fx_read(uartFd, _, _)).WillRepeatedly(DoAll(Return(UART_DEVICE_READ_NOTHING)));
|
||||
UnlockProtocolHandle();
|
||||
}
|
||||
};
|
||||
EXPECT_CALL(*mock.get(), fx_read(uartFd, _, _))
|
||||
.WillRepeatedly(DoAll(WithArgs<0, 1, 2>(Invoke(apiReadLeftData)), ReturnPointee(&readResult)));
|
||||
ReadOnceSelectSucceed(mock, uartFd);
|
||||
}
|
||||
void McuProtocolTestTool::PrintHexadecimalData(const void *buf, const size_t &bufLength, const char *log)
|
||||
{
|
||||
printf("%s { 0x%02X", log, *(unsigned char *)buf);
|
||||
|
|
|
@ -64,30 +64,15 @@ public:
|
|||
virtual ~VProtocolBase() = default;
|
||||
|
||||
protected:
|
||||
virtual std::shared_ptr<VProtocolBase> SharedFromThis(void)
|
||||
{
|
||||
return std::make_shared<VProtocolBase>();
|
||||
}
|
||||
virtual std::shared_ptr<VProtocolBase> SharedFromThis(void);
|
||||
virtual ssize_t WriteData(const void *buff, const size_t buffLength, std::shared_ptr<VProtocolContext> &context,
|
||||
const unsigned int &serialNumber)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
const unsigned int &serialNumber);
|
||||
|
||||
protected:
|
||||
virtual size_t GetKeyHeadLength(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
virtual StatusCode GetDataLength(const void *keyHead, const size_t &headLength, size_t &dataLength)
|
||||
{
|
||||
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
|
||||
}
|
||||
virtual void PushMcuData(const void *buf, const size_t &length)
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
virtual size_t GetKeyHeadLength(void);
|
||||
virtual StatusCode GetDataLength(const void *keyHead, const size_t &headLength, size_t &dataLength);
|
||||
virtual void PushMcuData(const void *buf, const size_t &length);
|
||||
virtual StatusCode CheckHeader(void *header, size_t &headLength);
|
||||
};
|
||||
enum class ReplyResult
|
||||
{
|
||||
|
@ -161,6 +146,7 @@ protected:
|
|||
size_t GetKeyHeadLength(void) override;
|
||||
StatusCode GetDataLength(const void *keyHead, const size_t &headLength, size_t &dataLength) override;
|
||||
void PushMcuData(const void *buf, const size_t &length) override;
|
||||
StatusCode CheckHeader(void *header, size_t &headLength) override;
|
||||
|
||||
private:
|
||||
StatusCode WriteProtocolData(const void *buff, const size_t buffLength, std::shared_ptr<VProtocolContext> &context,
|
||||
|
|
|
@ -19,11 +19,9 @@
|
|||
#include <string.h>
|
||||
LittleEndianHandle::LittleEndianHandle(const std::shared_ptr<VProtocolParam> ¶m) : ProtocolHandle(param)
|
||||
{
|
||||
//
|
||||
}
|
||||
LittleEndianHandle::LittleEndianHandle(const void *data, const size_t &length) : ProtocolHandle(data, length)
|
||||
{
|
||||
//
|
||||
}
|
||||
void LittleEndianHandle::BigEndianConversion(ProtocolPacket &packet)
|
||||
{
|
||||
|
@ -47,6 +45,8 @@ void LittleEndianHandle::HostByteOrderConversion(ProtocolPacket &packet)
|
|||
}
|
||||
bool LittleEndianHandle::CheckoutTheCheckCode(const ProtocolPacket &packet)
|
||||
{
|
||||
ProtocolHandle::PrintHexadecimalData(
|
||||
mProtocolData, mProtocolDataLength - CHECK_CODE_LENGTH, "CheckoutTheCheckCode little endian:");
|
||||
short code = calculate_check_sum(mProtocolData, mProtocolDataLength - sizeof(short));
|
||||
code = ntohs(code); // TODO:
|
||||
if (code == packet.mCheckCode) {
|
||||
|
|
|
@ -16,6 +16,30 @@
|
|||
#include "ILog.h"
|
||||
#include "ProtocolHandle.h"
|
||||
#include <string.h>
|
||||
std::shared_ptr<VProtocolBase> VProtocolBase::SharedFromThis(void)
|
||||
{
|
||||
return std::make_shared<VProtocolBase>();
|
||||
}
|
||||
ssize_t VProtocolBase::WriteData(const void *buff, const size_t buffLength, std::shared_ptr<VProtocolContext> &context,
|
||||
const unsigned int &serialNumber)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
size_t VProtocolBase::GetKeyHeadLength(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
StatusCode VProtocolBase::GetDataLength(const void *keyHead, const size_t &headLength, size_t &dataLength)
|
||||
{
|
||||
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
|
||||
}
|
||||
void VProtocolBase::PushMcuData(const void *buf, const size_t &length)
|
||||
{
|
||||
}
|
||||
StatusCode VProtocolBase::CheckHeader(void *header, size_t &headLength)
|
||||
{
|
||||
return CreateStatusCode(STATUS_CODE_INVALID_PARAMENTER);
|
||||
}
|
||||
void OtherSideReply::GetIpcMissionReply(const unsigned int &serialNumber, const unsigned char &mission)
|
||||
{
|
||||
}
|
||||
|
@ -247,6 +271,10 @@ void McuProtocol::PushMcuData(const void *buf, const size_t &length)
|
|||
mMcuDataList.push_back(packet);
|
||||
sem_post(&mSem);
|
||||
}
|
||||
StatusCode McuProtocol::CheckHeader(void *header, size_t &headLength)
|
||||
{
|
||||
return ProtocolHandle::CheckHeader(header, headLength);
|
||||
}
|
||||
StatusCode McuProtocol::WriteProtocolData(const void *buff, const size_t buffLength,
|
||||
std::shared_ptr<VProtocolContext> &context, const unsigned int &serialNumber)
|
||||
{
|
||||
|
|
|
@ -152,6 +152,8 @@ void ProtocolHandle::MakeNoUserDataPacket(const std::shared_ptr<VProtocolParam>
|
|||
// packet.mCheckCode = BigEndianConversion(packet.mCheckCode);
|
||||
memcpy(mProtocolData + KEY_HEAD_LENGTH, &packet.mCheckCode, CHECK_CODE_LENGTH);
|
||||
mProtocolDataLength = packetLength;
|
||||
ProtocolHandle::PrintHexadecimalData(
|
||||
mProtocolData, mProtocolDataLength - CHECK_CODE_LENGTH, "MakeNoUserDataPacket:");
|
||||
}
|
||||
void ProtocolHandle::MakeAskIpcMissionPacket(const std::shared_ptr<VProtocolParam> ¶m)
|
||||
{
|
||||
|
@ -282,6 +284,8 @@ void ProtocolHandle::AnalyzeOtherSideSendGetPirSensitivity(const ProtocolPacket
|
|||
}
|
||||
bool ProtocolHandle::CheckoutTheCheckCode(const ProtocolPacket &packet)
|
||||
{
|
||||
ProtocolHandle::PrintHexadecimalData(
|
||||
mProtocolData, mProtocolDataLength - CHECK_CODE_LENGTH, "CheckoutTheCheckCode:");
|
||||
short code = calculate_check_sum(mProtocolData, mProtocolDataLength - CHECK_CODE_LENGTH);
|
||||
if (code == packet.mCheckCode) {
|
||||
return true;
|
||||
|
@ -319,6 +323,29 @@ size_t ProtocolHandle::GetKeyHeadLength(void)
|
|||
{
|
||||
return KEY_HEAD_LENGTH;
|
||||
}
|
||||
StatusCode ProtocolHandle::CheckHeader(void *header, size_t &headLength)
|
||||
{
|
||||
unsigned char *data = (unsigned char *)header;
|
||||
unsigned short protocolHead = 0;
|
||||
protocolHead = PROTOCOL_HEAD;
|
||||
// LogInfo("head = 0x%02X, 0x%02X\n", protocolHead >> 8, protocolHead & 0x00FF);
|
||||
for (size_t i = 0; i < headLength; i++) {
|
||||
if ((protocolHead >> 8 == data[i]) && ((protocolHead & 0x00FF) == data[i + 1])) {
|
||||
headLength -= i;
|
||||
if (0 == i) {
|
||||
return CreateStatusCode(STATUS_CODE_OK);
|
||||
}
|
||||
unsigned char fixedHeader[KEY_HEAD_LENGTH] = {0};
|
||||
memcpy(fixedHeader, data + i, headLength);
|
||||
memcpy(header, fixedHeader, headLength);
|
||||
LogWarning("Protocol head unmatch, unknow data before head. headLength =%d\n", headLength);
|
||||
ProtocolHandle::PrintHexadecimalData(header, headLength, "Fixed header:");
|
||||
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||
}
|
||||
}
|
||||
LogWarning("Protocol head unmatch, head not found.\n");
|
||||
return CreateStatusCode(STATUS_CODE_NOT_OK);
|
||||
}
|
||||
StatusCode ProtocolHandle::GetDataLength(const void *keyHead, const size_t &headLength, size_t &dataLength)
|
||||
{
|
||||
if (KEY_HEAD_LENGTH != headLength) {
|
||||
|
|
|
@ -236,6 +236,7 @@ public:
|
|||
static std::shared_ptr<ProtocolHandle> CreateProtocolData(const std::shared_ptr<VProtocolParam> ¶m);
|
||||
static void ProtocolAnalysis(const void *data, const size_t &length);
|
||||
static size_t GetKeyHeadLength(void);
|
||||
static StatusCode CheckHeader(void *header, size_t &headLength);
|
||||
static StatusCode GetDataLength(const void *keyHead, const size_t &headLength, size_t &dataLength);
|
||||
static char GetByteOrder(void);
|
||||
static void PrintHexadecimalData(const void *buf, const size_t &bufLength, const char *log = nullptr);
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
#include "ModBusCRC16.h"
|
||||
#include <stdio.h>
|
||||
/* Table of CRC values for high–order byte */
|
||||
unsigned char chCRCHi[] = {
|
||||
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80,
|
||||
|
@ -63,5 +64,6 @@ unsigned short calculate_check_sum(const unsigned char *pData, unsigned short le
|
|||
CRCHi = CRCLo ^ chCRCHi[uIndex];
|
||||
CRCLo = chCRCLo[uIndex];
|
||||
}
|
||||
printf("CRCHi = 0x%x, CRCLo = 0x%x\n", CRCHi, CRCLo);
|
||||
return (CRCHi << 8 | CRCLo);
|
||||
}
|
Loading…
Reference in New Issue
Block a user