diff --git a/middleware/McuManager/src/McuDevice.cpp b/middleware/McuManager/src/McuDevice.cpp index bd50a57..664d387 100644 --- a/middleware/McuManager/src/McuDevice.cpp +++ b/middleware/McuManager/src/McuDevice.cpp @@ -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"); diff --git a/test/middleware/McuManager/src_mock/McuManagerMockTest.cpp b/test/middleware/McuManager/src_mock/McuManagerMockTest.cpp new file mode 100644 index 0000000..c116866 --- /dev/null +++ b/test/middleware/McuManager/src_mock/McuManagerMockTest.cpp @@ -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 test = mLinuxTest; + LinuxApiMock::GetInstance(&test); + LinuxApiMock::GetInstance()->Init(); + McuManagerTestTool::Init(mLinuxTest); + CreateMcuManager(); +} +void McuManagerMockTest::TearDown() +{ + LinuxApiMock::GetInstance()->UnInit(); + mLinuxTest = std::make_shared(); + std::shared_ptr test = std::make_shared(); + LinuxApiMock::GetInstance(&test); + McuManagerTestTool::UnInit(); + DestroyMcuManager(); +} \ No newline at end of file diff --git a/test/middleware/McuManager/src_mock/McuManagerMockTest.h b/test/middleware/McuManager/src_mock/McuManagerMockTest.h new file mode 100644 index 0000000..620cdf3 --- /dev/null +++ b/test/middleware/McuManager/src_mock/McuManagerMockTest.h @@ -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 +#include +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 mLinuxTest; +}; +#endif \ No newline at end of file diff --git a/test/middleware/McuManager/src_mock/McuManager_AbnormalData_Test.cpp b/test/middleware/McuManager/src_mock/McuManager_AbnormalData_Test.cpp new file mode 100644 index 0000000..6a1d47e --- /dev/null +++ b/test/middleware/McuManager/src_mock/McuManager_AbnormalData_Test.cpp @@ -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 +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 \ No newline at end of file diff --git a/test/middleware/McuManager/src_mock/McuManager_Mock_Test.cpp b/test/middleware/McuManager/src_mock/McuManager_Mock_Test.cpp index 9fe310d..0dc3693 100644 --- a/test/middleware/McuManager/src_mock/McuManager_Mock_Test.cpp +++ b/test/middleware/McuManager/src_mock/McuManager_Mock_Test.cpp @@ -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 -#include +#include "McuManagerMockTest.h" #include -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 test = mLinuxTest; - LinuxApiMock::GetInstance(&test); - LinuxApiMock::GetInstance()->Init(); - McuManagerTestTool::Init(mLinuxTest); - CreateMcuManager(); - } - virtual void TearDown() - { - LinuxApiMock::GetInstance()->UnInit(); - mLinuxTest = std::make_shared(); - std::shared_ptr test = std::make_shared(); - LinuxApiMock::GetInstance(&test); - McuManagerTestTool::UnInit(); - DestroyMcuManager(); - } - -public: - std::shared_ptr 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 \ No newline at end of file +} // namespace McuManager_Mock_Test \ No newline at end of file diff --git a/test/middleware/McuManager/tool/include/McuManagerTestTool.h b/test/middleware/McuManager/tool/include/McuManagerTestTool.h index 8ea0692..ac2e9b5 100644 --- a/test/middleware/McuManager/tool/include/McuManagerTestTool.h +++ b/test/middleware/McuManager/tool/include/McuManagerTestTool.h @@ -31,14 +31,17 @@ class McuManagerTestTool : virtual public McuProtocolTestTool public: McuManagerTestTool() = default; virtual ~McuManagerTestTool() = default; + bool CheckAskExist(const std::shared_ptr &ask); + +protected: void Init(std::shared_ptr &mock); void UnInit(void); - bool CheckAskExist(const std::shared_ptr &ask); void MockOtherSideAskIpcMission(std::shared_ptr &mock, const unsigned int &serialNumber); void MockOtherSideAskHeartBeat(std::shared_ptr &mock, const unsigned int &serialNumber); void MockOtherSideGetIntervalStart(std::shared_ptr &mock, const unsigned int &serialNumber); void MockOtherSideGetDateTime(std::shared_ptr &mock, const unsigned int &serialNumber); void MockOtherSideGetPriSensitivity(std::shared_ptr &mock, const unsigned int &serialNumber); + void MockOtherSideSendData(std::shared_ptr &mock, const void *data, const size_t &size); void MockMcuDeviceOpenFailed(std::shared_ptr &mock); void MockMcuDeviceOpenSuccessButReadNothing(std::shared_ptr &mock); diff --git a/test/middleware/McuManager/tool/src/McuManagerTestTool.cpp b/test/middleware/McuManager/tool/src/McuManagerTestTool.cpp index 9976685..d23cd21 100644 --- a/test/middleware/McuManager/tool/src/McuManagerTestTool.cpp +++ b/test/middleware/McuManager/tool/src/McuManagerTestTool.cpp @@ -66,6 +66,10 @@ void McuManagerTestTool::MockOtherSideGetPriSensitivity(std::shared_ptr &mock, const void *data, const size_t &size) +{ + McuProtocolTestTool::MockOtherSideAskSendAnyData(mock, data, size); +} void McuManagerTestTool::MockMcuDeviceOpenFailed(std::shared_ptr &mock) { UartDeviceTestTool::SetUartDeviceOpenFailed(mock, gUartDevice); diff --git a/test/utils/McuProtocol/tool/include/McuProtocolTestTool.h b/test/utils/McuProtocol/tool/include/McuProtocolTestTool.h index 2981df9..36c04cc 100644 --- a/test/utils/McuProtocol/tool/include/McuProtocolTestTool.h +++ b/test/utils/McuProtocol/tool/include/McuProtocolTestTool.h @@ -36,7 +36,9 @@ public: void MockOtherSideAskGetIntervalStart(std::shared_ptr &mock, const unsigned int &serialNumber); void MockOtherSideAskGetDateTime(std::shared_ptr &mock, const unsigned int &serialNumber); void MockOtherSideAskGetPirSensitivity(std::shared_ptr &mock, const unsigned int &serialNumber); + void MockOtherSideAskSendAnyData(std::shared_ptr &mock, const void *data, const size_t &size); void ReadNothingAnyTime(std::shared_ptr &mock); + void ReadOnceSelectSucceed(std::shared_ptr &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 &mock, const int &uartFd, const unsigned int &serialNumber); + void OtherSideAskSendSomeDataHandle(std::shared_ptr &mock, const int &uartFd, const void *data, + const size_t &size); + void OtherSideAskSendSomeDataInit(std::shared_ptr &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); diff --git a/test/utils/McuProtocol/tool/src/McuProtocolTestTool.cpp b/test/utils/McuProtocol/tool/src/McuProtocolTestTool.cpp index d997d65..786d486 100644 --- a/test/utils/McuProtocol/tool/src/McuProtocolTestTool.cpp +++ b/test/utils/McuProtocol/tool/src/McuProtocolTestTool.cpp @@ -173,6 +173,11 @@ void McuProtocolTestTool::MockOtherSideAskGetPirSensitivity(std::shared_ptr &mock, const void *data, + const size_t &size) +{ + OtherSideAskSendSomeDataHandle(mock, mUartFd, data, size); +} void McuProtocolTestTool::ReadNothingAnyTime(std::shared_ptr &mock) { static size_t WRITE_COUNT = -1; @@ -287,6 +292,49 @@ void McuProtocolTestTool::ReplySelectSucceed(std::shared_ptr &mock, c .WillRepeatedly(DoAll(WithArgs<0, 1, 2, 3, 4>(Invoke(selectTimeOut)), Return(MOCK_SELECT_TIME_OUT))); PipeSelectTimeoutForProtocolHandleImmediately(); } +void McuProtocolTestTool::ReadOnceSelectSucceed(std::shared_ptr &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 &mock, const int &uartFd) { if (mUart) { @@ -964,6 +1012,53 @@ void McuProtocolTestTool::OtherSideAskGetPirSensitivityInit(std::shared_ptr &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 &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); diff --git a/utils/McuProtocol/include/McuProtocol.h b/utils/McuProtocol/include/McuProtocol.h index 284daf9..d986796 100644 --- a/utils/McuProtocol/include/McuProtocol.h +++ b/utils/McuProtocol/include/McuProtocol.h @@ -64,30 +64,15 @@ public: virtual ~VProtocolBase() = default; protected: - virtual std::shared_ptr SharedFromThis(void) - { - return std::make_shared(); - } + virtual std::shared_ptr SharedFromThis(void); virtual ssize_t WriteData(const void *buff, const size_t buffLength, std::shared_ptr &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 &context, diff --git a/utils/McuProtocol/src/LittleEndianHandle.cpp b/utils/McuProtocol/src/LittleEndianHandle.cpp index 63f9f42..91a11b8 100644 --- a/utils/McuProtocol/src/LittleEndianHandle.cpp +++ b/utils/McuProtocol/src/LittleEndianHandle.cpp @@ -19,11 +19,9 @@ #include LittleEndianHandle::LittleEndianHandle(const std::shared_ptr ¶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) { diff --git a/utils/McuProtocol/src/McuProtocol.cpp b/utils/McuProtocol/src/McuProtocol.cpp index ce7ceb4..55d6539 100644 --- a/utils/McuProtocol/src/McuProtocol.cpp +++ b/utils/McuProtocol/src/McuProtocol.cpp @@ -16,6 +16,30 @@ #include "ILog.h" #include "ProtocolHandle.h" #include +std::shared_ptr VProtocolBase::SharedFromThis(void) +{ + return std::make_shared(); +} +ssize_t VProtocolBase::WriteData(const void *buff, const size_t buffLength, std::shared_ptr &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 &context, const unsigned int &serialNumber) { diff --git a/utils/McuProtocol/src/ProtocolHandle.cpp b/utils/McuProtocol/src/ProtocolHandle.cpp index 78b5780..9f3b912 100644 --- a/utils/McuProtocol/src/ProtocolHandle.cpp +++ b/utils/McuProtocol/src/ProtocolHandle.cpp @@ -152,6 +152,8 @@ void ProtocolHandle::MakeNoUserDataPacket(const std::shared_ptr // 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 ¶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) { diff --git a/utils/McuProtocol/src/ProtocolHandle.h b/utils/McuProtocol/src/ProtocolHandle.h index 61e0db1..d8fe6cb 100644 --- a/utils/McuProtocol/src/ProtocolHandle.h +++ b/utils/McuProtocol/src/ProtocolHandle.h @@ -236,6 +236,7 @@ public: static std::shared_ptr CreateProtocolData(const std::shared_ptr ¶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); diff --git a/utils/ModBusCRC16/src/ModBusCRC16.cpp b/utils/ModBusCRC16/src/ModBusCRC16.cpp index ba8069a..b560aff 100644 --- a/utils/ModBusCRC16/src/ModBusCRC16.cpp +++ b/utils/ModBusCRC16/src/ModBusCRC16.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ #include "ModBusCRC16.h" +#include /* 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); } \ No newline at end of file