Backup McuManager module.
This commit is contained in:
		
							parent
							
								
									9cfaaf4c48
								
							
						
					
					
						commit
						7706998056
					
				| 
						 | 
				
			
			@ -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;
 | 
			
		||||
        }
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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(
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -26,10 +26,11 @@ static UartInfo gUartDevice = {
 | 
			
		|||
void McuManagerTestTool::Init(std::shared_ptr<LinuxTest> &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);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -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(
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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.
 | 
			
		||||
 */
 | 
			
		||||
 */
 | 
			
		||||
#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<LinuxTest> &mock, const UartInfo &uart);
 | 
			
		||||
    void UnInit(void);
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    static void PrintHexadecimalData(const void *buf, const size_t &bufLength);
 | 
			
		||||
};
 | 
			
		||||
#endif
 | 
			
		||||
| 
						 | 
				
			
			@ -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.
 | 
			
		||||
 */
 | 
			
		||||
 */
 | 
			
		||||
#include "McuProtocolTestTool.h"
 | 
			
		||||
#include "ILog.h"
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <thread>
 | 
			
		||||
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<LinuxTest> &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");
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -17,6 +17,7 @@
 | 
			
		|||
#include "LinuxApiMock.h"
 | 
			
		||||
#include "UartDevice.h"
 | 
			
		||||
#include <map>
 | 
			
		||||
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<LinuxTest> &mock, const UartInfo &uart);
 | 
			
		||||
    int RegisterUartDevice(std::shared_ptr<LinuxTest> &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<LinuxTest> &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<LinuxTest> &mock, const UartInfo &uart, void *recvBuff, const ssize_t &length);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -16,11 +16,12 @@
 | 
			
		|||
#include "ILog.h"
 | 
			
		||||
#include <thread>
 | 
			
		||||
static size_t WRITE_COUNT = -1;
 | 
			
		||||
void UartDeviceTestTool::RegisterUartDevice(std::shared_ptr<LinuxTest> &mock, const UartInfo &uart)
 | 
			
		||||
constexpr int INVALID_FD = -1;
 | 
			
		||||
int UartDeviceTestTool::RegisterUartDevice(std::shared_ptr<LinuxTest> &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<LinuxTest> &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<const char *, int>::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<LinuxTest> &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)));
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										37
									
								
								utils/McuProtocol/src/LittleEndianHandle.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								utils/McuProtocol/src/LittleEndianHandle.cpp
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -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 <netinet/in.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
LittleEndianHandle::LittleEndianHandle(const std::shared_ptr<VProtocolParam> ¶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));
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										27
									
								
								utils/McuProtocol/src/LittleEndianHandle.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								utils/McuProtocol/src/LittleEndianHandle.h
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -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<VProtocolParam> ¶m);
 | 
			
		||||
    virtual ~LittleEndianHandle() = default;
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    void BigEndianConversion(ProtocolPacket &packet) override;
 | 
			
		||||
};
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										41
									
								
								utils/McuProtocol/src/McuProtocolMakePtr.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								utils/McuProtocol/src/McuProtocolMakePtr.cpp
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -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 <netinet/in.h>
 | 
			
		||||
std::shared_ptr<McuProtocolMakePtr> &McuProtocolMakePtr::GetInstance(std::shared_ptr<McuProtocolMakePtr> *impl)
 | 
			
		||||
{
 | 
			
		||||
    static auto instance = std::make_shared<McuProtocolMakePtr>();
 | 
			
		||||
    if (impl) {
 | 
			
		||||
        instance = *impl;
 | 
			
		||||
    }
 | 
			
		||||
    return instance;
 | 
			
		||||
}
 | 
			
		||||
McuProtocolMakePtr::McuProtocolMakePtr() { mByteOrder = 0x00; }
 | 
			
		||||
const StatusCode McuProtocolMakePtr::CreateProtocolHandle(std::shared_ptr<ProtocolHandle> &impl,
 | 
			
		||||
                                                          const std::shared_ptr<VProtocolParam> ¶m)
 | 
			
		||||
{
 | 
			
		||||
    std::shared_ptr<ProtocolHandle> tmp;
 | 
			
		||||
    char byteOrder = ProtocolHandle::GetByteOrder();
 | 
			
		||||
    if (ORDER_BIG_ENDIAN == byteOrder) {
 | 
			
		||||
        tmp = std::make_shared<ProtocolHandle>(param);
 | 
			
		||||
    }
 | 
			
		||||
    else {
 | 
			
		||||
        tmp = std::make_shared<LittleEndianHandle>(param);
 | 
			
		||||
    }
 | 
			
		||||
    impl = tmp;
 | 
			
		||||
    return CreateStatusCode(STATUS_CODE_OK);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										32
									
								
								utils/McuProtocol/src/McuProtocolMakePtr.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								utils/McuProtocol/src/McuProtocolMakePtr.h
									
									
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -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 <memory>
 | 
			
		||||
class McuProtocolMakePtr
 | 
			
		||||
{
 | 
			
		||||
public:
 | 
			
		||||
    McuProtocolMakePtr();
 | 
			
		||||
    virtual ~McuProtocolMakePtr() = default;
 | 
			
		||||
    static std::shared_ptr<McuProtocolMakePtr> &GetInstance(std::shared_ptr<McuProtocolMakePtr> *impl = nullptr);
 | 
			
		||||
    virtual const StatusCode CreateProtocolHandle(std::shared_ptr<ProtocolHandle> &impl,
 | 
			
		||||
                                                  const std::shared_ptr<VProtocolParam> ¶m);
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    char mByteOrder;
 | 
			
		||||
};
 | 
			
		||||
#endif // !MCU_PROTOCOL_MAKE_PTR_H
 | 
			
		||||
| 
						 | 
				
			
			@ -14,22 +14,13 @@
 | 
			
		|||
 */
 | 
			
		||||
#include "ProtocolHandle.h"
 | 
			
		||||
#include "ILog.h"
 | 
			
		||||
#include "McuProtocolMakePtr.h"
 | 
			
		||||
#include "ModBusCRC16.h"
 | 
			
		||||
#include <netinet/in.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
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<VProtocolParam> ¶m) : mParam(param)
 | 
			
		||||
{
 | 
			
		||||
    mProtocolData = nullptr;
 | 
			
		||||
| 
						 | 
				
			
			@ -71,13 +62,15 @@ void ProtocolHandle::MakeAskIpcMissionPacket(const std::shared_ptr<VProtocolPara
 | 
			
		|||
    packet.mCheckCode = Calculate_Check_Sum(mProtocolData, dataLength - sizeof(short));
 | 
			
		||||
    packet.mSerialNumber = mSerialNumber;
 | 
			
		||||
    mSerialNumber++;
 | 
			
		||||
    BigEndianConversion(packet);
 | 
			
		||||
    memcpy(mProtocolData, &packet, KEY_HEAD_LENGTH);
 | 
			
		||||
    memcpy(mProtocolData + KEY_HEAD_LENGTH, &packet.mCheckCode, sizeof(short));
 | 
			
		||||
    mProtocolDataLength = dataLength;
 | 
			
		||||
}
 | 
			
		||||
std::shared_ptr<ProtocolHandle> ProtocolHandle::CreateProtocolData(const std::shared_ptr<VProtocolParam> ¶m)
 | 
			
		||||
{
 | 
			
		||||
    std::shared_ptr<ProtocolHandle> handle = std::make_shared<ProtocolHandle>(param);
 | 
			
		||||
    std::shared_ptr<ProtocolHandle> 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;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -18,6 +18,19 @@
 | 
			
		|||
#include <functional>
 | 
			
		||||
#include <map>
 | 
			
		||||
#include <memory>
 | 
			
		||||
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<VProtocolParam> ¶m);
 | 
			
		||||
    void MakeAskIpcMissionPacket(const std::shared_ptr<VProtocolParam> ¶m);
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    virtual void BigEndianConversion(ProtocolPacket &packet) {}
 | 
			
		||||
 | 
			
		||||
private:
 | 
			
		||||
    std::shared_ptr<VProtocolParam> mParam;
 | 
			
		||||
    std::map<PROTOCOL_COMMAND, MakePacketFunc> 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
 | 
			
		||||
| 
						 | 
				
			
			@ -16,6 +16,7 @@
 | 
			
		|||
#define UARTD_EVICE_H
 | 
			
		||||
#include "StatusCode.h"
 | 
			
		||||
#include <cstddef>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
#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.
 | 
			
		||||
 *
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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");
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user