diff --git a/middleware/McuManager/src/McuDevice.cpp b/middleware/McuManager/src/McuDevice.cpp index 02bd3e3a..25e9c10a 100644 --- a/middleware/McuManager/src/McuDevice.cpp +++ b/middleware/McuManager/src/McuDevice.cpp @@ -99,9 +99,9 @@ void McuDevice::DeviceRecvThread(void) } mThreadRuning = true; while (mThreadRuning) { - size_t recvLength = + ssize_t recvLength = IUartRecv(mUartDevice, keyHeadBuf + recvTotalLength, keyHeadLength - recvTotalLength, RECV_TIMEOUT_MS); - LogInfo("mcu recv length = %d\n", recvLength); + LogInfo("mcu recv length = %ld\n", recvLength); if (recvLength <= 0) { // recv failed or recv nothing continue; } diff --git a/test/middleware/McuManager/CMakeLists.txt b/test/middleware/McuManager/CMakeLists.txt index fb311cb5..431e6e46 100644 --- a/test/middleware/McuManager/CMakeLists.txt +++ b/test/middleware/McuManager/CMakeLists.txt @@ -11,7 +11,8 @@ include_directories( ${UTILS_SOURCE_PATH}/UartDevice/include ${MIDDLEWARE_SOURCE_PATH}/McuManager/include ${TEST_SOURCE_PATH}/utils/LinuxApiMock/include - ${TEST_SOURCE_PATH}/utils/UartDevice/tool/include/ + ${TEST_SOURCE_PATH}/utils/UartDevice/tool/include + ${TEST_SOURCE_PATH}/utils/McuProtocol/tool/include ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include ) diff --git a/test/middleware/McuManager/tool/CMakeLists.txt b/test/middleware/McuManager/tool/CMakeLists.txt index 2c5ed3bc..13bb586a 100644 --- a/test/middleware/McuManager/tool/CMakeLists.txt +++ b/test/middleware/McuManager/tool/CMakeLists.txt @@ -8,6 +8,7 @@ include_directories( ${UTILS_SOURCE_PATH}/StatusCode/include ${UTILS_SOURCE_PATH}/Log/include ${TEST_SOURCE_PATH}/utils/LinuxApiMock/include + ${TEST_SOURCE_PATH}/utils/McuProtocol/tool/include ) # link_directories( # ${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs @@ -19,7 +20,7 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON) aux_source_directory(./src TEST_TOOL_SRC_FILES) set(TEST_TOOL_TARGET McuManagerTestTool) add_library(${TEST_TOOL_TARGET} STATIC ${TEST_TOOL_SRC_FILES}) -target_link_libraries(${TEST_TOOL_TARGET} UartDeviceTestTool LinuxApiMock Log) +target_link_libraries(${TEST_TOOL_TARGET} McuProtocolTestTool UartDeviceTestTool LinuxApiMock Log) if ("${CLANG_TIDY_SUPPORT}" MATCHES "true") add_custom_target( diff --git a/test/middleware/McuManager/tool/include/McuManagerTestTool.h b/test/middleware/McuManager/tool/include/McuManagerTestTool.h index ce2eaebd..13718685 100644 --- a/test/middleware/McuManager/tool/include/McuManagerTestTool.h +++ b/test/middleware/McuManager/tool/include/McuManagerTestTool.h @@ -15,8 +15,8 @@ #ifndef MCU_MANAGER_TEST_TOOL_H #define MCU_MANAGER_TEST_TOOL_H #include "LinuxApiMock.h" -#include "UartDeviceTestTool.h" -class McuManagerTestTool : public UartDeviceTestTool +#include "McuProtocolTestTool.h" +class McuManagerTestTool : virtual public McuProtocolTestTool { public: McuManagerTestTool() = default; diff --git a/test/middleware/McuManager/tool/src/McuManagerTestTool.cpp b/test/middleware/McuManager/tool/src/McuManagerTestTool.cpp index bd5f565b..a97fb43d 100644 --- a/test/middleware/McuManager/tool/src/McuManagerTestTool.cpp +++ b/test/middleware/McuManager/tool/src/McuManagerTestTool.cpp @@ -26,10 +26,11 @@ static UartInfo gUartDevice = { void McuManagerTestTool::Init(std::shared_ptr &mock) { LogInfo("McuManagerTestTool::Init\n"); - RegisterUartDevice(mock, gUartDevice); + UartDeviceTestTool::RegisterUartDevice(mock, gUartDevice); + McuProtocolTestTool::Init(mock, gUartDevice); } void McuManagerTestTool::UnInit(void) { // - UnregisterUartDevice(gUartDevice); + UartDeviceTestTool::UnregisterUartDevice(gUartDevice); } \ No newline at end of file diff --git a/test/utils/McuProtocol/tool/CMakeLists.txt b/test/utils/McuProtocol/tool/CMakeLists.txt index a1382016..92c93cd2 100644 --- a/test/utils/McuProtocol/tool/CMakeLists.txt +++ b/test/utils/McuProtocol/tool/CMakeLists.txt @@ -7,7 +7,11 @@ include_directories( ./include ${UTILS_SOURCE_PATH}/StatusCode/include ${UTILS_SOURCE_PATH}/Log/include + ${UTILS_SOURCE_PATH}/UartDevice/include ${TEST_SOURCE_PATH}/utils/LinuxApiMock/include + ${TEST_SOURCE_PATH}/utils/UartDevice/tool/include + ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include + ${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googlemock/include ) # link_directories( # ${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs @@ -19,7 +23,7 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON) aux_source_directory(./src TEST_TOOL_SRC_FILES) set(TEST_TOOL_TARGET McuProtocolTestTool) add_library(${TEST_TOOL_TARGET} STATIC ${TEST_TOOL_SRC_FILES}) -target_link_libraries(${TEST_TOOL_TARGET} LinuxApiMock Log) +target_link_libraries(${TEST_TOOL_TARGET} UartDeviceTestTool Log) if ("${CLANG_TIDY_SUPPORT}" MATCHES "true") add_custom_target( diff --git a/test/utils/McuProtocol/tool/include/McuProtocolTestTool.h b/test/utils/McuProtocol/tool/include/McuProtocolTestTool.h index f297c002..a2505411 100644 --- a/test/utils/McuProtocol/tool/include/McuProtocolTestTool.h +++ b/test/utils/McuProtocol/tool/include/McuProtocolTestTool.h @@ -11,4 +11,20 @@ * 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. - */ \ No newline at end of file + */ +#ifndef MCU_PROTOCOL_TEST_TOOL_H +#define MCU_PROTOCOL_TEST_TOOL_H +#include "LinuxApiMock.h" +#include "UartDeviceTestTool.h" +class McuProtocolTestTool : virtual public UartDeviceTestTool +{ +public: + McuProtocolTestTool() = default; + virtual ~McuProtocolTestTool() = default; + void Init(std::shared_ptr &mock, const UartInfo &uart); + void UnInit(void); + +private: + static void PrintHexadecimalData(const void *buf, const size_t &bufLength); +}; +#endif \ No newline at end of file diff --git a/test/utils/McuProtocol/tool/src/McuProtocolTestTool.cpp b/test/utils/McuProtocol/tool/src/McuProtocolTestTool.cpp index f297c002..8e951b5f 100644 --- a/test/utils/McuProtocol/tool/src/McuProtocolTestTool.cpp +++ b/test/utils/McuProtocol/tool/src/McuProtocolTestTool.cpp @@ -11,4 +11,64 @@ * 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. - */ \ No newline at end of file + */ +#include "McuProtocolTestTool.h" +#include "ILog.h" +#include +#include +constexpr size_t PROTOCOL_DATA_KEY_HEAD_LENGTH = 10; +const unsigned char ASK_IPC_MISSION[] = {0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x81, 0x01, 0x00, 0x0C, 0x3D, 0x68}; +const unsigned char REPLY_IPC_MISSION[] = { + 0xFA, 0xC1, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x0D, 0x00, 0x3D, 0x68}; +void McuProtocolTestTool::Init(std::shared_ptr &mock, const UartInfo &uart) +{ + int uartFd = GetDeviceMockFd(uart); + static size_t WRITE_COUNT = -1; + auto api_write = [=, &mock](int fd, const void *buf, size_t count) { + McuProtocolTestTool::PrintHexadecimalData(buf, count); + if (sizeof(ASK_IPC_MISSION) == count && memcmp(ASK_IPC_MISSION, buf, count) == 0) { + LogInfo("Set REPLY_IPC_MISSION\n"); + 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) { + long long timeMs = timeout->tv_sec * 1000 + timeout->tv_usec / 1000; + std::this_thread::sleep_for(std::chrono::milliseconds(timeMs)); + }; + EXPECT_CALL(*mock.get(), fx_select(uartFd + 1, _, _, _, _)) + .WillOnce(DoAll(WithArgs<0, 1, 2, 3, 4>(Invoke(selectReadable)), Return(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))); + constexpr int LEFT_DATA_LENGTH = sizeof(REPLY_IPC_MISSION) - PROTOCOL_DATA_KEY_HEAD_LENGTH; + auto apiReadKeyHead = [=](int fd, void *buf, size_t count) { + LogInfo("apiReadKeyHead count = %d\n", count); + memcpy(buf, REPLY_IPC_MISSION, PROTOCOL_DATA_KEY_HEAD_LENGTH); + }; + auto apiReadLeftData = [=](int fd, void *buf, size_t count) { + memcpy(buf, REPLY_IPC_MISSION + PROTOCOL_DATA_KEY_HEAD_LENGTH, LEFT_DATA_LENGTH); + }; + 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))); + } + }; + EXPECT_CALL(*mock.get(), fx_write(uartFd, _, _)) + .WillRepeatedly( + DoAll(SaveArg<2>(&WRITE_COUNT), WithArgs<0, 1, 2>(Invoke(api_write)), ReturnPointee(&WRITE_COUNT))); +} +void McuProtocolTestTool::UnInit(void) +{ + // +} +void McuProtocolTestTool::PrintHexadecimalData(const void *buf, const size_t &bufLength) +{ + printf("write buf = { 0x%02X", *(unsigned char *)buf); + for (size_t i = 1; i < bufLength; i++) { + printf(", 0x%02X", *((unsigned char *)buf + i)); + } + printf(" }\n"); +} \ No newline at end of file diff --git a/test/utils/UartDevice/tool/include/UartDeviceTestTool.h b/test/utils/UartDevice/tool/include/UartDeviceTestTool.h index 1a04c827..6a8523d0 100644 --- a/test/utils/UartDevice/tool/include/UartDeviceTestTool.h +++ b/test/utils/UartDevice/tool/include/UartDeviceTestTool.h @@ -17,6 +17,7 @@ #include "LinuxApiMock.h" #include "UartDevice.h" #include +constexpr int UART_DEVICE_READ_NOTHING = 0; class UartDeviceTestTool { public: @@ -27,8 +28,16 @@ public: * * @param mock * @param uart + * @return int Return a virtual handle, and the simulated test code uses this handle to bind the device. */ - void RegisterUartDevice(std::shared_ptr &mock, const UartInfo &uart); + int RegisterUartDevice(std::shared_ptr &mock, const UartInfo &uart); + /** + * @brief Get the Device Mock Fd object + * + * @param uart + * @return int fd of uart device. + */ + int GetDeviceMockFd(const UartInfo &uart); /** * @brief * @@ -37,19 +46,19 @@ public: void UnregisterUartDevice(const UartInfo &uart); /** * @brief Set the Send Api object - * + * Set the length of a successful send. * @param mock * @param uart - * @param length + * @param length The length of a successful send. */ void SetSendApiOnce(std::shared_ptr &mock, const UartInfo &uart, const ssize_t &length); /** * @brief Set the Recv Api Once object - * + * Set the data received at once. * @param mock * @param uart - * @param recvBuff - * @param length + * @param recvBuff The data that will be received. + * @param length The length of data that will be received. */ void SetRecvApiOnce(std::shared_ptr &mock, const UartInfo &uart, void *recvBuff, const ssize_t &length); diff --git a/test/utils/UartDevice/tool/src/UartDeviceTestTool.cpp b/test/utils/UartDevice/tool/src/UartDeviceTestTool.cpp index b827d318..d22e3b5f 100644 --- a/test/utils/UartDevice/tool/src/UartDeviceTestTool.cpp +++ b/test/utils/UartDevice/tool/src/UartDeviceTestTool.cpp @@ -16,11 +16,12 @@ #include "ILog.h" #include static size_t WRITE_COUNT = -1; -void UartDeviceTestTool::RegisterUartDevice(std::shared_ptr &mock, const UartInfo &uart) +constexpr int INVALID_FD = -1; +int UartDeviceTestTool::RegisterUartDevice(std::shared_ptr &mock, const UartInfo &uart) { if (nullptr == uart.mDevice) { LogError("Parament error, nullptr == uartInfo.mDevice\n"); - return; + return INVALID_FD; } constexpr int TCGETATTR_SUCCEED = 0; constexpr int TCSETATTR_SUCCEED = 0; @@ -38,9 +39,19 @@ void UartDeviceTestTool::RegisterUartDevice(std::shared_ptr &mock, co }; EXPECT_CALL(*mock.get(), fx_select(uartFd + 1, _, _, _, _)) .WillRepeatedly(DoAll(WithArgs<0, 1, 2, 3, 4>(Invoke(selectTimeOut)), Return(MOCK_SELECT_TIME_OUT))); - constexpr int READ_NOTHING = 0; - EXPECT_CALL(*mock.get(), fx_read(uartFd, _, _)).WillRepeatedly(DoAll(Return(READ_NOTHING))); + EXPECT_CALL(*mock.get(), fx_read(uartFd, _, _)).WillRepeatedly(DoAll(Return(UART_DEVICE_READ_NOTHING))); mDeviceMap[uart.mDevice] = uartFd; + return uartFd; +} +int UartDeviceTestTool::GetDeviceMockFd(const UartInfo &uart) +{ + std::map::iterator iter; + iter = mDeviceMap.find(uart.mDevice); + if (iter == mDeviceMap.end()) { + LogError("Can't found the uart device[%s].\n", uart.mDevice); + return INVALID_FD; + } + return mDeviceMap[uart.mDevice]; } void UartDeviceTestTool::UnregisterUartDevice(const UartInfo &uart) { @@ -89,8 +100,7 @@ void UartDeviceTestTool::SetRecvApiOnce(std::shared_ptr &mock, const .WillOnce(DoAll(WithArgs<0, 1, 2, 3, 4>(Invoke(selectSucceed)), Return(SELECT_READABLE))) .WillRepeatedly(DoAll(WithArgs<0, 1, 2, 3, 4>(Invoke(selectTimeOut)), Return(MOCK_SELECT_TIME_OUT))); auto readBuf = [=, &mock](int fd, void *buf, size_t count) { memcpy(buf, recvBuff, length); }; - constexpr int READ_NOTHING = 0; EXPECT_CALL(*mock.get(), fx_read(uartFd, _, _)) .WillOnce(DoAll(WithArgs<0, 1, 2>(Invoke(readBuf)), Return(length))) - .WillRepeatedly(DoAll(Return(READ_NOTHING))); + .WillRepeatedly(DoAll(Return(UART_DEVICE_READ_NOTHING))); } \ No newline at end of file diff --git a/utils/McuProtocol/src/LittleEndianHandle.cpp b/utils/McuProtocol/src/LittleEndianHandle.cpp new file mode 100644 index 00000000..8da8e5de --- /dev/null +++ b/utils/McuProtocol/src/LittleEndianHandle.cpp @@ -0,0 +1,37 @@ +/* + * 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 "LittleEndianHandle.h" +#include "ILog.h" +#include +#include +LittleEndianHandle::LittleEndianHandle(const std::shared_ptr ¶m) : ProtocolHandle(param) +{ + // +} +void LittleEndianHandle::BigEndianConversion(ProtocolPacket &packet) +{ + // LogInfo("packet.mHead = 0x%02x\n", packet.mHead); + // LogInfo("packet.mCommand = 0x%02x\n", packet.mCommand); + // LogInfo("packet.mLength = %d\n", packet.mLength); + // LogInfo("packet.mCheckCode = 0x%02x\n", packet.mCheckCode); + // LogInfo("packet.mSerialNumber = %d\n", packet.mSerialNumber); + // ProtocolPacket packetBigEndian; + packet.mHead = htons(packet.mHead); + packet.mCommand = htons(packet.mCommand); + packet.mLength = htons(packet.mLength); + packet.mCheckCode = htons(packet.mCheckCode); + packet.mSerialNumber = htonl(packet.mSerialNumber); + // memcpy(&packet, &packetBigEndian, sizeof(ProtocolPacket)); +} \ No newline at end of file diff --git a/utils/McuProtocol/src/LittleEndianHandle.h b/utils/McuProtocol/src/LittleEndianHandle.h new file mode 100644 index 00000000..bf0c768d --- /dev/null +++ b/utils/McuProtocol/src/LittleEndianHandle.h @@ -0,0 +1,27 @@ +/* + * 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 LITTLE_ENDIAN_HANDLE_H +#define LITTLE_ENDIAN_HANDLE_H +#include "ProtocolHandle.h" +class LittleEndianHandle : public ProtocolHandle +{ +public: + LittleEndianHandle(const std::shared_ptr ¶m); + virtual ~LittleEndianHandle() = default; + +private: + void BigEndianConversion(ProtocolPacket &packet) override; +}; +#endif \ No newline at end of file diff --git a/utils/McuProtocol/src/McuProtocolMakePtr.cpp b/utils/McuProtocol/src/McuProtocolMakePtr.cpp new file mode 100644 index 00000000..59a3e13b --- /dev/null +++ b/utils/McuProtocol/src/McuProtocolMakePtr.cpp @@ -0,0 +1,41 @@ +/* + * 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 "McuProtocolMakePtr.h" +#include "ILog.h" +#include "LittleEndianHandle.h" +#include +std::shared_ptr &McuProtocolMakePtr::GetInstance(std::shared_ptr *impl) +{ + static auto instance = std::make_shared(); + if (impl) { + instance = *impl; + } + return instance; +} +McuProtocolMakePtr::McuProtocolMakePtr() { mByteOrder = 0x00; } +const StatusCode McuProtocolMakePtr::CreateProtocolHandle(std::shared_ptr &impl, + const std::shared_ptr ¶m) +{ + std::shared_ptr tmp; + char byteOrder = ProtocolHandle::GetByteOrder(); + if (ORDER_BIG_ENDIAN == byteOrder) { + tmp = std::make_shared(param); + } + else { + tmp = std::make_shared(param); + } + impl = tmp; + return CreateStatusCode(STATUS_CODE_OK); +} \ No newline at end of file diff --git a/utils/McuProtocol/src/McuProtocolMakePtr.h b/utils/McuProtocol/src/McuProtocolMakePtr.h new file mode 100644 index 00000000..d130f321 --- /dev/null +++ b/utils/McuProtocol/src/McuProtocolMakePtr.h @@ -0,0 +1,32 @@ +/* + * 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_PROTOCOL_MAKE_PTR_H +#define MCU_PROTOCOL_MAKE_PTR_H +#include "ProtocolHandle.h" +#include "StatusCode.h" +#include +class McuProtocolMakePtr +{ +public: + McuProtocolMakePtr(); + virtual ~McuProtocolMakePtr() = default; + static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); + virtual const StatusCode CreateProtocolHandle(std::shared_ptr &impl, + const std::shared_ptr ¶m); + +private: + char mByteOrder; +}; +#endif // !MCU_PROTOCOL_MAKE_PTR_H \ No newline at end of file diff --git a/utils/McuProtocol/src/ProtocolHandle.cpp b/utils/McuProtocol/src/ProtocolHandle.cpp index 876e772a..9a1604be 100644 --- a/utils/McuProtocol/src/ProtocolHandle.cpp +++ b/utils/McuProtocol/src/ProtocolHandle.cpp @@ -14,22 +14,13 @@ */ #include "ProtocolHandle.h" #include "ILog.h" +#include "McuProtocolMakePtr.h" #include "ModBusCRC16.h" +#include #include unsigned int ProtocolHandle::mSerialNumber = 0; constexpr unsigned short PROTOCOL_HEAD = 0xFAC1; constexpr size_t KEY_HEAD_LENGTH = sizeof(short) + sizeof(unsigned int) + sizeof(short) + sizeof(short); -#pragma pack(1) -typedef struct protocol_packet -{ - short mHead; - unsigned int mSerialNumber; - short mCommand; - short mLength; - char *mData; - short mCheckCode; -} ProtocolPacket; -#pragma pack() ProtocolHandle::ProtocolHandle(const std::shared_ptr ¶m) : mParam(param) { mProtocolData = nullptr; @@ -71,13 +62,15 @@ void ProtocolHandle::MakeAskIpcMissionPacket(const std::shared_ptr ProtocolHandle::CreateProtocolData(const std::shared_ptr ¶m) { - std::shared_ptr handle = std::make_shared(param); + std::shared_ptr handle; + McuProtocolMakePtr::GetInstance()->CreateProtocolHandle(handle, param); handle->MakeProtocolPacket(param); return handle; } @@ -100,4 +93,22 @@ StatusCode ProtocolHandle::GetDataLength(const void *keyHead, const size_t &head } dataLength = packet.mLength; return CreateStatusCode(STATUS_CODE_OK); +} +char ProtocolHandle::GetByteOrder(void) +{ + static char byteOrder = 0x00; + if (0x00 != byteOrder) { + return byteOrder; + } + unsigned int value = 0x12345678; + unsigned int networkOrder = htonl(value); + if (value == networkOrder) { + LogInfo("Big Endian.\n"); + byteOrder = ORDER_BIG_ENDIAN; + } + else { + LogInfo("Little Endian.\n"); + byteOrder = ORDER_LITTLE_ENDIAN; + } + return byteOrder; } \ No newline at end of file diff --git a/utils/McuProtocol/src/ProtocolHandle.h b/utils/McuProtocol/src/ProtocolHandle.h index 9f89db4d..b87c1a1f 100644 --- a/utils/McuProtocol/src/ProtocolHandle.h +++ b/utils/McuProtocol/src/ProtocolHandle.h @@ -18,6 +18,19 @@ #include #include #include +constexpr char ORDER_BIG_ENDIAN = 0x01; +constexpr char ORDER_LITTLE_ENDIAN = 0x02; +#pragma pack(1) +typedef struct protocol_packet +{ + short mHead; + unsigned int mSerialNumber; + short mCommand; + short mLength; + char *mData; + short mCheckCode; +} ProtocolPacket; +#pragma pack() enum PROTOCOL_COMMAND { ASK_IPC_MISSION = 0x8101, @@ -60,6 +73,9 @@ private: void MakeProtocolPacket(const std::shared_ptr ¶m); void MakeAskIpcMissionPacket(const std::shared_ptr ¶m); +private: + virtual void BigEndianConversion(ProtocolPacket &packet) {} + private: std::shared_ptr mParam; std::map mMakePacketFunc; @@ -82,5 +98,6 @@ public: static void ProtocolAnalysis(const void *data, const size_t &length); static size_t GetKeyHeadLength(void); static StatusCode GetDataLength(const void *keyHead, const size_t &headLength, size_t &dataLength); + static char GetByteOrder(void); }; #endif \ No newline at end of file diff --git a/utils/UartDevice/include/UartDevice.h b/utils/UartDevice/include/UartDevice.h index 09a4a28f..15e78173 100644 --- a/utils/UartDevice/include/UartDevice.h +++ b/utils/UartDevice/include/UartDevice.h @@ -16,6 +16,7 @@ #define UARTD_EVICE_H #include "StatusCode.h" #include +#include #ifdef __cplusplus extern "C" { #endif @@ -48,9 +49,9 @@ const StatusCode IUartOpen(void *object); * @param object Serial port object pointer. * @param buff The data to be sent. * @param buffLength The length of data to be sent. - * @return const size_t + * @return const ssize_t */ -const size_t IUartSend(void *object, const void *buff, const size_t buffLength); +const ssize_t IUartSend(void *object, const void *buff, const size_t buffLength); /** * @brief Use a serial port to receive data. * @@ -58,9 +59,9 @@ const size_t IUartSend(void *object, const void *buff, const size_t buffLength); * @param buff The cache area for receiving data. * @param buffLength The size of the cache area for receiving data. * @param timeoutMs The timeout time in milliseconds. - * @return const size_t + * @return const ssize_t */ -const size_t IUartRecv(void *object, void *buff, const size_t buffLength, const unsigned int timeoutMs); +const ssize_t IUartRecv(void *object, void *buff, const size_t buffLength, const unsigned int timeoutMs); /** * @brief Clear the serial port cache data. * diff --git a/utils/UartDevice/src/UartDevice.cpp b/utils/UartDevice/src/UartDevice.cpp index ba56909e..16236f3b 100644 --- a/utils/UartDevice/src/UartDevice.cpp +++ b/utils/UartDevice/src/UartDevice.cpp @@ -29,7 +29,7 @@ const StatusCode IUartOpen(void *object) } return ((UartDevice *)object)->mOpen((UartDevice *)object); } -const size_t IUartSend(void *object, const void *buff, const size_t buffLength) +const ssize_t IUartSend(void *object, const void *buff, const size_t buffLength) { if (nullptr == object) { LogError("nullptr object!\n"); @@ -41,7 +41,7 @@ const size_t IUartSend(void *object, const void *buff, const size_t buffLength) } return ((UartDevice *)object)->mSend((UartDevice *)object, buff, buffLength); } -const size_t IUartRecv(void *object, void *buff, const size_t buffLength, const unsigned int timeoutMs) +const ssize_t IUartRecv(void *object, void *buff, const size_t buffLength, const unsigned int timeoutMs) { if (nullptr == object) { LogError("nullptr object!\n"); diff --git a/utils/UartDevice/src/UartDeviceImpl.cpp b/utils/UartDevice/src/UartDeviceImpl.cpp index 79a06319..08940b6a 100644 --- a/utils/UartDevice/src/UartDeviceImpl.cpp +++ b/utils/UartDevice/src/UartDeviceImpl.cpp @@ -39,10 +39,10 @@ const StatusCode UartDeviceImpl::UartOpen(void) return SetConfig(); } -const size_t UartDeviceImpl::UartSend(const void *buff, const size_t &buffLength) +const ssize_t UartDeviceImpl::UartSend(const void *buff, const size_t &buffLength) { - constexpr int SEND_FAILED = -1; - size_t writeLength = 0; + constexpr ssize_t SEND_FAILED = -1; + ssize_t writeLength = 0; if (nullptr == buff) { LogError("data buff is nullptr.\n"); return SEND_FAILED; @@ -62,11 +62,11 @@ const size_t UartDeviceImpl::UartSend(const void *buff, const size_t &buffLength tcflush(mFd, TCOFLUSH); return writeTotal; } -const size_t UartDeviceImpl::UartRecv(void *buff, const size_t &buffLength, const unsigned int &timeOutMs, - const unsigned int delayReadMs) +const ssize_t UartDeviceImpl::UartRecv(void *buff, const size_t &buffLength, const unsigned int &timeOutMs, + const unsigned int delayReadMs) { - constexpr int RECV_FAILED = -1; - int readLength = 0; + constexpr size_t RECV_FAILED = -1; + ssize_t readLength = 0; int fs_sel = -1; fd_set fs_read; struct timeval time; @@ -230,12 +230,12 @@ static const StatusCode UartOpen(UartDevice *object) UartDeviceImpl_S *impl = UartDeviceImplConvert(object); return impl->mUartImpl->UartOpen(); } -static const size_t UartSend(UartDevice *object, const void *buff, const size_t buffLength) +static const ssize_t UartSend(UartDevice *object, const void *buff, const size_t buffLength) { UartDeviceImpl_S *impl = UartDeviceImplConvert(object); return impl->mUartImpl->UartSend(buff, buffLength); } -static const size_t UartRecv(UartDevice *object, void *buff, const size_t buffLength, const unsigned int timeoutMs) +static const ssize_t UartRecv(UartDevice *object, void *buff, const size_t buffLength, const unsigned int timeoutMs) { UartDeviceImpl_S *impl = UartDeviceImplConvert(object); constexpr int READ_NOT_DELAY = 0; diff --git a/utils/UartDevice/src/UartDeviceImpl.h b/utils/UartDevice/src/UartDeviceImpl.h index ac801144..e37e15f6 100644 --- a/utils/UartDevice/src/UartDeviceImpl.h +++ b/utils/UartDevice/src/UartDeviceImpl.h @@ -29,9 +29,9 @@ public: UartDeviceImpl(const UartInfo &uatrInfo); virtual ~UartDeviceImpl() = default; const StatusCode UartOpen(void); - const size_t UartSend(const void *buff, const size_t &buffLength); - const size_t UartRecv(void *buff, const size_t &buffLength, const unsigned int &timeOutMs = 0, - const unsigned int delayReadMs = 0); + const ssize_t UartSend(const void *buff, const size_t &buffLength); + const ssize_t UartRecv(void *buff, const size_t &buffLength, const unsigned int &timeOutMs = 0, + const unsigned int delayReadMs = 0); void UartTcflush(void); private: @@ -45,8 +45,8 @@ typedef struct uart_device UartDevice; typedef struct uart_device { const StatusCode (*mOpen)(UartDevice *); - const size_t (*mSend)(UartDevice *, const void *, const size_t); - const size_t (*mRecv)(UartDevice *, void *, const size_t, const unsigned int); + const ssize_t (*mSend)(UartDevice *, const void *, const size_t); + const ssize_t (*mRecv)(UartDevice *, void *, const size_t, const unsigned int); void (*mTcflush)(UartDevice *); void (*mFree)(void *); } UartDevice;