From a2c4fa60ec2153c7535cc6cdd24b985ab2840a40 Mon Sep 17 00:00:00 2001 From: Fancy code <258828110.@qq.com> Date: Fri, 2 Feb 2024 06:25:00 -0800 Subject: [PATCH] Add MucManager module. --- .clang-format | 2 +- build/cmake/toolchain/linux.toolchain.cmake | 5 +- middleware/CMakeLists.txt | 3 +- middleware/McuManager/CMakeLists.txt | 67 ++++++++++++++++ middleware/McuManager/include/IMcuManager.h | 58 ++++++++++++++ middleware/McuManager/src/IMcuManager.cpp | 30 +++++++ middleware/McuManager/src/McuDevice.cpp | 39 ++++++++++ middleware/McuManager/src/McuDevice.h | 31 ++++++++ middleware/McuManager/src/McuManagerImpl.cpp | 22 ++++++ middleware/McuManager/src/McuManagerImpl.h | 27 +++++++ .../McuManager/src/McuManagerMakePtr.cpp | 42 ++++++++++ middleware/McuManager/src/McuManagerMakePtr.h | 28 +++++++ test/middleware/CMakeLists.txt | 3 +- test/middleware/IpcConfig/mainTest.cpp | 14 ++++ test/middleware/McuManager/CMakeLists.txt | 78 +++++++++++++++++++ test/middleware/McuManager/mainTest.cpp | 23 ++++++ .../src_mock/McuManager_Mock_Test.cpp | 42 ++++++++++ test/utils/LinuxApiMock/CMakeLists.txt | 2 +- test/utils/UartDevice/tool/CMakeLists.txt | 2 +- utils/CMakeLists.txt | 1 + utils/McuProtocol/CMakeLists.txt | 54 +++++++++++++ utils/McuProtocol/include/McuProtocol.h | 60 ++++++++++++++ utils/McuProtocol/src/McuProtocol.cpp | 25 ++++++ utils/McuProtocol/src/ProtocolHandle.cpp | 31 ++++++++ utils/McuProtocol/src/ProtocolHandle.h | 57 ++++++++++++++ utils/UartDevice/include/UartDevice.h | 4 +- utils/UartDevice/src/UartDevice.cpp | 4 +- utils/UartDevice/src/UartDeviceImpl.cpp | 8 +- utils/UartDevice/src/UartDeviceImpl.h | 8 +- 29 files changed, 752 insertions(+), 18 deletions(-) create mode 100644 middleware/McuManager/CMakeLists.txt create mode 100644 middleware/McuManager/include/IMcuManager.h create mode 100644 middleware/McuManager/src/IMcuManager.cpp create mode 100644 middleware/McuManager/src/McuDevice.cpp create mode 100644 middleware/McuManager/src/McuDevice.h create mode 100644 middleware/McuManager/src/McuManagerImpl.cpp create mode 100644 middleware/McuManager/src/McuManagerImpl.h create mode 100644 middleware/McuManager/src/McuManagerMakePtr.cpp create mode 100644 middleware/McuManager/src/McuManagerMakePtr.h create mode 100644 test/middleware/McuManager/CMakeLists.txt create mode 100644 test/middleware/McuManager/mainTest.cpp create mode 100644 test/middleware/McuManager/src_mock/McuManager_Mock_Test.cpp create mode 100644 utils/McuProtocol/CMakeLists.txt create mode 100644 utils/McuProtocol/include/McuProtocol.h create mode 100644 utils/McuProtocol/src/McuProtocol.cpp create mode 100644 utils/McuProtocol/src/ProtocolHandle.cpp create mode 100644 utils/McuProtocol/src/ProtocolHandle.h diff --git a/.clang-format b/.clang-format index 291c72e..c7bb547 100755 --- a/.clang-format +++ b/.clang-format @@ -21,7 +21,7 @@ AllowShortLoopsOnASingleLine: false AlwaysBreakAfterDefinitionReturnType: None AlwaysBreakAfterReturnType: None AlwaysBreakBeforeMultilineStrings: false -AlwaysBreakTemplateDeclarations: MultiLine +AlwaysBreakTemplateDeclarations: true BinPackArguments: false BinPackParameters: true BraceWrapping: diff --git a/build/cmake/toolchain/linux.toolchain.cmake b/build/cmake/toolchain/linux.toolchain.cmake index 558b576..e32701d 100755 --- a/build/cmake/toolchain/linux.toolchain.cmake +++ b/build/cmake/toolchain/linux.toolchain.cmake @@ -45,4 +45,7 @@ set(LOG_SUPPORT "true") set(GOAHEAD_DOCUMENTS_PATH "web") # GOAHEAD_CONFIG_FILE_PATH should be set when cross compile # set(GOAHEAD_CONFIG_FILE_PATH "./") -# ------------ build GoAhead end ------------ # \ No newline at end of file +# ------------ build GoAhead end ------------ # +# ------------ build McuManager ------------ # +set(MCU_UART_DEVICE "dev/s1") +# ------------ build McuManager end ------------ # \ No newline at end of file diff --git a/middleware/CMakeLists.txt b/middleware/CMakeLists.txt index 4eb09b7..c63c6ac 100644 --- a/middleware/CMakeLists.txt +++ b/middleware/CMakeLists.txt @@ -1,3 +1,4 @@ add_subdirectory(StateMachine) add_subdirectory(IpcConfig) -add_subdirectory(DeviceManager) \ No newline at end of file +add_subdirectory(DeviceManager) +add_subdirectory(McuManager) \ No newline at end of file diff --git a/middleware/McuManager/CMakeLists.txt b/middleware/McuManager/CMakeLists.txt new file mode 100644 index 0000000..4b0d859 --- /dev/null +++ b/middleware/McuManager/CMakeLists.txt @@ -0,0 +1,67 @@ + +include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake) +set(EXECUTABLE_OUTPUT_PATH ${EXEC_OUTPUT_PATH}) +set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH}) + +include_directories( + ./src + ./include + ${UTILS_SOURCE_PATH}/StatusCode/include + ${UTILS_SOURCE_PATH}/Log/include + ${UTILS_SOURCE_PATH}/McuProtocol/include + ${UTILS_SOURCE_PATH}/UartDevice/include +) +#do not rely on any other library +#link_directories( +#) + +set(CMAKE_AUTOMOC ON) +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +if (DEFINED MCU_UART_DEVICE) + add_definitions(-DMCU_UART_DEVICE=\"${MCU_UART_DEVICE}\") +else() + message(FATAL_ERROR "You set define MCU_UART_DEVICE in toolchan .cmake file to tell what uart device to contrl.") +endif() + +aux_source_directory(./src SRC_FILES) + +set(TARGET_NAME McuManager) +add_library(${TARGET_NAME} STATIC ${SRC_FILES}) + +target_link_libraries(${TARGET_NAME} UartDevice McuProtocol StatusCode Log) + +if ("${CLANG_TIDY_SUPPORT}" MATCHES "true") +add_custom_target( + McuManager_code_check + COMMAND ${CLANG_TIDY_EXE} + -checks='${CLANG_TIDY_CHECKS}' + --header-filter=.* + --system-headers=false + ${SRC_FILES} + ${CLANG_TIDY_CONFIG} + -p ${PLATFORM_PATH}/cmake-shell + WORKING_DIRECTORY ${MIDDLEWARE_SOURCE_PATH}/McuManager +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make McuManager_code_check + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) +file(GLOB_RECURSE HEADER_FILES *.h) +add_custom_target( + McuManager_code_format + COMMAND ${CLANG_FORMAT_EXE} + -style=file + -i ${SRC_FILES} ${HEADER_FILES} + WORKING_DIRECTORY ${MIDDLEWARE_SOURCE_PATH}/McuManager +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make McuManager_code_check + COMMAND make McuManager_code_format + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) +endif() \ No newline at end of file diff --git a/middleware/McuManager/include/IMcuManager.h b/middleware/McuManager/include/IMcuManager.h new file mode 100644 index 0000000..1cb501b --- /dev/null +++ b/middleware/McuManager/include/IMcuManager.h @@ -0,0 +1,58 @@ +/* + * 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 I_MCU_MANAGER_H +#define I_MCU_MANAGER_H +#include "StatusCode.h" +#include +bool CreateMcuManager(void); +enum class IpcMission +{ + TEST = 0, + END +}; +constexpr int NEVER_TIMEOUT_UNTIL_REPLY = 0; +class VMcuAsk +{ +public: + VMcuAsk() = default; + virtual ~VMcuAsk() = default; + bool IsBlock(void) { return false; } + unsigned int GetTimeOutMs(void) { return NEVER_TIMEOUT_UNTIL_REPLY; } +}; +template +class McuAsk : public VMcuAsk +{ + +public: + McuAsk() {} + virtual ~McuAsk() = default; + +public: + T mDataReply; +}; +class IMcuManager +{ +public: + IMcuManager() = default; + virtual ~IMcuManager() = default; + static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); + virtual const StatusCode Init(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } + virtual const StatusCode UnInit(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } + virtual const StatusCode GetIpcMissiony(std::shared_ptr ask) + { + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); + } +}; +#endif \ No newline at end of file diff --git a/middleware/McuManager/src/IMcuManager.cpp b/middleware/McuManager/src/IMcuManager.cpp new file mode 100644 index 0000000..4a423cb --- /dev/null +++ b/middleware/McuManager/src/IMcuManager.cpp @@ -0,0 +1,30 @@ +/* + * 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 "IMcuManager.h" +#include "ILog.h" +std::shared_ptr &IMcuManager::GetInstance(std::shared_ptr *impl) +{ + static auto instance = std::make_shared(); + if (impl) { + if (instance.use_count() == 1) { + LogInfo("Instance changed succeed.\n"); + instance = *impl; + } + else { + LogError("Can't changing the instance becase of using by some one.\n"); + } + } + return instance; +} \ No newline at end of file diff --git a/middleware/McuManager/src/McuDevice.cpp b/middleware/McuManager/src/McuDevice.cpp new file mode 100644 index 0000000..f5298dc --- /dev/null +++ b/middleware/McuManager/src/McuDevice.cpp @@ -0,0 +1,39 @@ +/* + * 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 "McuDevice.h" +McuDevice::McuDevice() { mUartDevice = nullptr; } +McuDevice::~McuDevice() {} +const StatusCode McuDevice::Init(void) +{ + UartInfo uartDevice = { + MCU_UART_DEVICE, + 1152000, + 'N', + 8, + 1, + 'N', + }; + mUartDevice = CreateUartDevice(uartDevice); + return CreateStatusCode(STATUS_CODE_OK); +} +const StatusCode McuDevice::UnInit(void) +{ + if (nullptr != mUartDevice) { + + IUartDeviceFree(mUartDevice); + } + mUartDevice = nullptr; + return CreateStatusCode(STATUS_CODE_OK); +} \ No newline at end of file diff --git a/middleware/McuManager/src/McuDevice.h b/middleware/McuManager/src/McuDevice.h new file mode 100644 index 0000000..e5b0221 --- /dev/null +++ b/middleware/McuManager/src/McuDevice.h @@ -0,0 +1,31 @@ +/* + * 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_DEVICE_H +#define MCU_DEVICE_H +#include "IMcuManager.h" +#include "McuProtocol.h" +#include "UartDevice.h" +class McuDevice : public IMcuManager, virtual public VProtocolBase +{ +public: + McuDevice(); + virtual ~McuDevice(); + const StatusCode Init(void) override; + const StatusCode UnInit(void) override; + +private: + void *mUartDevice; +}; +#endif \ No newline at end of file diff --git a/middleware/McuManager/src/McuManagerImpl.cpp b/middleware/McuManager/src/McuManagerImpl.cpp new file mode 100644 index 0000000..49a37a2 --- /dev/null +++ b/middleware/McuManager/src/McuManagerImpl.cpp @@ -0,0 +1,22 @@ +/* + * 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 "McuManagerImpl.h" +const StatusCode McuManagerImpl::GetIpcMissiony(std::shared_ptr ask) +{ + std::shared_ptr context = std::make_shared>>(ask); + McuProtocol::GetIpcMissiony(context); + // IpcMission mission = static_cast(data); + return CreateStatusCode(STATUS_CODE_OK); +} \ No newline at end of file diff --git a/middleware/McuManager/src/McuManagerImpl.h b/middleware/McuManager/src/McuManagerImpl.h new file mode 100644 index 0000000..16a3182 --- /dev/null +++ b/middleware/McuManager/src/McuManagerImpl.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 MCU_MANAGER_IMPL_H +#define MCU_MANAGER_IMPL_H +#include "IMcuManager.h" +#include "McuDevice.h" +#include "McuProtocol.h" +class McuManagerImpl : public McuDevice, public McuProtocol +{ +public: + McuManagerImpl() = default; + virtual ~McuManagerImpl() = default; + const StatusCode GetIpcMissiony(std::shared_ptr ask) override; +}; +#endif \ No newline at end of file diff --git a/middleware/McuManager/src/McuManagerMakePtr.cpp b/middleware/McuManager/src/McuManagerMakePtr.cpp new file mode 100644 index 0000000..7959bdb --- /dev/null +++ b/middleware/McuManager/src/McuManagerMakePtr.cpp @@ -0,0 +1,42 @@ +/* + * 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 "McuManagerMakePtr.h" +#include "ILog.h" +#include "McuManagerImpl.h" +bool CreateMcuManager(void) +{ + auto instance = std::make_shared(); + StatusCode code = McuManagerMakePtr::GetInstance()->CreateMcuManager(instance); + if (IsCodeOK(code)) { + LogInfo("CreateMcuManager is ok.\n"); + IMcuManager::GetInstance(&instance); + return true; + } + return false; +} +std::shared_ptr &McuManagerMakePtr::GetInstance(std::shared_ptr *impl) +{ + static auto instance = std::make_shared(); + if (impl) { + instance = *impl; + } + return instance; +} +const StatusCode McuManagerMakePtr::CreateMcuManager(std::shared_ptr &impl) +{ + auto tmp = std::make_shared(); + impl = tmp; + return CreateStatusCode(STATUS_CODE_OK); +} \ No newline at end of file diff --git a/middleware/McuManager/src/McuManagerMakePtr.h b/middleware/McuManager/src/McuManagerMakePtr.h new file mode 100644 index 0000000..a194822 --- /dev/null +++ b/middleware/McuManager/src/McuManagerMakePtr.h @@ -0,0 +1,28 @@ +/* + * 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 MCUMANAGER_MAKE_PTR_H +#define MCUMANAGER_MAKE_PTR_H +#include "IMcuManager.h" +#include "StatusCode.h" +#include +class McuManagerMakePtr +{ +public: + McuManagerMakePtr() = default; + virtual ~McuManagerMakePtr() = default; + static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); + virtual const StatusCode CreateMcuManager(std::shared_ptr &impl); +}; +#endif // !IPC_CONFIG_MAKE_PTR_H \ No newline at end of file diff --git a/test/middleware/CMakeLists.txt b/test/middleware/CMakeLists.txt index 92339dc..66b159a 100644 --- a/test/middleware/CMakeLists.txt +++ b/test/middleware/CMakeLists.txt @@ -1,3 +1,4 @@ # cmake_minimum_required(VERSION 2.8.0) -add_subdirectory(IpcConfig) \ No newline at end of file +add_subdirectory(IpcConfig) +add_subdirectory(McuManager) \ No newline at end of file diff --git a/test/middleware/IpcConfig/mainTest.cpp b/test/middleware/IpcConfig/mainTest.cpp index fb1d91b..475ceee 100644 --- a/test/middleware/IpcConfig/mainTest.cpp +++ b/test/middleware/IpcConfig/mainTest.cpp @@ -1,3 +1,17 @@ +/* + * 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 #include #include diff --git a/test/middleware/McuManager/CMakeLists.txt b/test/middleware/McuManager/CMakeLists.txt new file mode 100644 index 0000000..d3491c1 --- /dev/null +++ b/test/middleware/McuManager/CMakeLists.txt @@ -0,0 +1,78 @@ +# include(${CMAKE_SOURCE_DIR}/build/independent_source.cmake) +include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake) +set(EXECUTABLE_OUTPUT_PATH ${TEST_OUTPUT_PATH}/bin) + +include_directories( + . + ./src + ./include + ${UTILS_SOURCE_PATH}/Log/include + ${UTILS_SOURCE_PATH}/StatusCode/include + ${MIDDLEWARE_SOURCE_PATH}/McuManager/include + ${TEST_SOURCE_PATH}/utils/LinuxApiMock/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( + ${LIBS_OUTPUT_PATH} + ${EXTERNAL_LIBS_OUTPUT_PATH} +) + +set(CMAKE_AUTOMOC ON) +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +set(CMAKE_AUTOMOC ON) +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +aux_source_directory(. SRC_FILES_MAIN) +aux_source_directory(./src SRC_FILES) +if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX}) + aux_source_directory(./src_mock SRC_FILES) +endif() + +set(TARGET_NAME McuManagerTest) +add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES}) +target_link_libraries(${TARGET_NAME} McuManager UartDeviceTestTool gtest gmock pthread) +if(${TEST_COVERAGE} MATCHES "true") + target_link_libraries(${TARGET_NAME} gcov) +endif() + +if ("${CLANG_TIDY_SUPPORT}" MATCHES "true") +add_custom_target( + McuManagerTest_code_check + COMMAND ${CLANG_TIDY_EXE} + -checks='${CLANG_TIDY_CHECKS}' + --header-filter=.* + --system-headers=false + ${SRC_FILES} + ${CLANG_TIDY_CONFIG} + # --line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/gtest.h\"}]' + --line-filter='[{\"name\":\"${EXTERNAL_SOURCE_PATH}/gtest/googletest-release-1.11.0/googletest/include/getest/*.h\"}]' + -p ${PLATFORM_PATH}/cmake-shell + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/McuManager +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make McuManagerTest_code_check + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) + +file(GLOB_RECURSE HEADER_FILES *.h) +add_custom_target( + McuManagerTest_code_format + COMMAND ${CLANG_FORMAT_EXE} + -style=file + -i ${SRC_FILES} ${SRC_FILES_MAIN} ${HEADER_FILES} + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/McuManager +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make McuManagerTest_code_check + COMMAND make McuManagerTest_code_format + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) +endif() +define_file_name(${TARGET_NAME}) \ No newline at end of file diff --git a/test/middleware/McuManager/mainTest.cpp b/test/middleware/McuManager/mainTest.cpp new file mode 100644 index 0000000..475ceee --- /dev/null +++ b/test/middleware/McuManager/mainTest.cpp @@ -0,0 +1,23 @@ +/* + * 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 +#include +#include +#include +int main(int argc, char *argv[]) +{ + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ 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 new file mode 100644 index 0000000..48c095a --- /dev/null +++ b/test/middleware/McuManager/src_mock/McuManager_Mock_Test.cpp @@ -0,0 +1,42 @@ +#include "ILog.h" +#include "IMcuManager.h" +#include "LinuxApiMock.h" +#include +#include +namespace McuManagerMockTest +{ +class McuManagerMockTest : public testing::Test +{ +public: + McuManagerMockTest() {} + virtual ~McuManagerMockTest() {} + static void SetUpTestCase() + { + CreateLogModule(); + CreateMcuManager(); + 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(); + // RegisterUartDevice(mLinuxTest, gUartDevice); + } + virtual void TearDown() + { + LinuxApiMock::GetInstance()->UnInit(); + mLinuxTest = std::make_shared(); + std::shared_ptr test = std::make_shared(); + LinuxApiMock::GetInstance(&test); + // UnregisterUartDevice(gUartDevice); + } + +public: + std::shared_ptr mLinuxTest; +}; +// ../output_files/test/bin/UartDeviceTest --gtest_filter=McuManagerTest.UNIT_UartDevice_EXAMPLE_AUTO_Demo +TEST_F(McuManagerMockTest, UNIT_UartDevice_EXAMPLE_AUTO_Demo) {} +} // namespace McuManagerMockTest \ No newline at end of file diff --git a/test/utils/LinuxApiMock/CMakeLists.txt b/test/utils/LinuxApiMock/CMakeLists.txt index 85ae0aa..8426e18 100644 --- a/test/utils/LinuxApiMock/CMakeLists.txt +++ b/test/utils/LinuxApiMock/CMakeLists.txt @@ -20,7 +20,7 @@ aux_source_directory(./src SRC_FILES) set(TARGET_NAME LinuxApiMock) add_library(${TARGET_NAME} STATIC ${SRC_FILES}) -target_link_libraries(${TARGET_NAME} gtest gmock Log) +target_link_libraries(${TARGET_NAME} LinuxApi gtest gmock Log) if ("${CLANG_TIDY_SUPPORT}" MATCHES "true") add_custom_target( diff --git a/test/utils/UartDevice/tool/CMakeLists.txt b/test/utils/UartDevice/tool/CMakeLists.txt index cf971e8..a79a69d 100644 --- a/test/utils/UartDevice/tool/CMakeLists.txt +++ b/test/utils/UartDevice/tool/CMakeLists.txt @@ -19,7 +19,7 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON) aux_source_directory(./src TEST_TOOL_SRC_FILES) set(TEST_TOOL_TARGET UartDeviceTestTool) add_library(${TEST_TOOL_TARGET} STATIC ${TEST_TOOL_SRC_FILES}) -target_link_libraries(${TEST_TOOL_TARGET} Log) +target_link_libraries(${TEST_TOOL_TARGET} UartDevice LinuxApiMock Log) if ("${CLANG_TIDY_SUPPORT}" MATCHES "true") add_custom_target( diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt index 878dcc9..7fa1047 100644 --- a/utils/CMakeLists.txt +++ b/utils/CMakeLists.txt @@ -8,3 +8,4 @@ add_subdirectory(UartDevice) add_subdirectory(LinuxApi) # add_subdirectory(MultiProcess) add_subdirectory(WebServer) +add_subdirectory(McuProtocol) diff --git a/utils/McuProtocol/CMakeLists.txt b/utils/McuProtocol/CMakeLists.txt new file mode 100644 index 0000000..1232919 --- /dev/null +++ b/utils/McuProtocol/CMakeLists.txt @@ -0,0 +1,54 @@ +include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake) +set(EXECUTABLE_OUTPUT_PATH ${EXEC_OUTPUT_PATH}) +set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH}) + +include_directories( + ./src + ./include + # ${UTILS_SOURCE_PATH}/LinuxApi/include + ${UTILS_SOURCE_PATH}/StatusCode/include + ${UTILS_SOURCE_PATH}/Log/include +) +# link_directories( +# ${EXTERNAL_SOURCE_PATH}/libconfig/libconfig-1.7.3/lib/.libs +# ) + +set(CMAKE_AUTOMOC ON) +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +aux_source_directory(./src SRC_FILES) + +set(TARGET_NAME McuProtocol) +add_library(${TARGET_NAME} STATIC ${SRC_FILES}) +target_link_libraries(${TARGET_NAME} StatusCode Log) + +if ("${CLANG_TIDY_SUPPORT}" MATCHES "true") +add_custom_target( + McuProtocol_code_check + COMMAND ${CLANG_TIDY_EXE} + -checks='${CLANG_TIDY_CHECKS}' + --header-filter=.* + --system-headers=false + ${SRC_FILES} + ${CLANG_TIDY_CONFIG} + -p ${PLATFORM_PATH}/cmake-shell + WORKING_DIRECTORY ${UTILS_SOURCE_PATH}/McuProtocol +) +file(GLOB_RECURSE HEADER_FILES *.h) +add_custom_target( + McuProtocol_code_format + COMMAND ${CLANG_FORMAT_EXE} + -style=file + -i ${SRC_FILES} ${HEADER_FILES} + WORKING_DIRECTORY ${UTILS_SOURCE_PATH}/McuProtocol +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make McuProtocol_code_check + COMMAND make McuProtocol_code_format + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) +endif() + +define_file_name(${TARGET_NAME}) \ No newline at end of file diff --git a/utils/McuProtocol/include/McuProtocol.h b/utils/McuProtocol/include/McuProtocol.h new file mode 100644 index 0000000..a5782af --- /dev/null +++ b/utils/McuProtocol/include/McuProtocol.h @@ -0,0 +1,60 @@ +/* + * 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_H +#define MCU_PROTOCOL_H +#include "StatusCode.h" +#include +#include +/** + * @brief The context of protocol processing is used for the association binding of protocol data transmission and + * reception, ensuring the correct logic of protocol question and answer. + * + */ +class VProtocolContext +{ +public: + VProtocolContext() = default; + virtual ~VProtocolContext() = default; +}; +template +class ProtocolContext : public VProtocolContext +{ + +public: + ProtocolContext(T &value) : mData(value) {} + virtual ~ProtocolContext() = default; + +public: + T mData; +}; +class VProtocolBase +{ +public: + VProtocolBase() = default; + virtual ~VProtocolBase() = default; + +public: + virtual size_t WriteData(const void *buff, const size_t buffLength) { return 0; } + +public: +}; +class McuProtocol : virtual public VProtocolBase +{ +public: + McuProtocol() = default; + virtual ~McuProtocol() = default; + const StatusCode GetIpcMissiony(std::shared_ptr &context); +}; +#endif \ No newline at end of file diff --git a/utils/McuProtocol/src/McuProtocol.cpp b/utils/McuProtocol/src/McuProtocol.cpp new file mode 100644 index 0000000..6392b04 --- /dev/null +++ b/utils/McuProtocol/src/McuProtocol.cpp @@ -0,0 +1,25 @@ +/* + * 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 "McuProtocol.h" +#include "ProtocolHandle.h" +const StatusCode McuProtocol::GetIpcMissiony(std::shared_ptr &context) +{ + char data = 0; + std::shared_ptr param = + std::make_shared>(PROTOCOL_COMMAND::ASK_IPC_MISSION, data); + std::shared_ptr handle = ProtocolHandle::CreateProtocolData(param); + WriteData(handle->GetProtocolDataBuff(), handle->GetProtocolDataLength()); + return CreateStatusCode(STATUS_CODE_OK); +} \ No newline at end of file diff --git a/utils/McuProtocol/src/ProtocolHandle.cpp b/utils/McuProtocol/src/ProtocolHandle.cpp new file mode 100644 index 0000000..2162b86 --- /dev/null +++ b/utils/McuProtocol/src/ProtocolHandle.cpp @@ -0,0 +1,31 @@ +/* + * 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 "ProtocolHandle.h" +constexpr unsigned short PROTOCOL_HEAD = 0xFAC1; +#pragma pack(1) +typedef struct protocol_packet +{ + short mHead; + short mCommand; + short mLength; + char *mData; + short mCheckCode; +} ProtocolPacket; +#pragma pack() +std::shared_ptr ProtocolHandle::CreateProtocolData(const std::shared_ptr ¶m) +{ + std::shared_ptr handle = std::make_shared(); + return handle; +} \ No newline at end of file diff --git a/utils/McuProtocol/src/ProtocolHandle.h b/utils/McuProtocol/src/ProtocolHandle.h new file mode 100644 index 0000000..9cb5fed --- /dev/null +++ b/utils/McuProtocol/src/ProtocolHandle.h @@ -0,0 +1,57 @@ +/* + * 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 PROTOCOL_HANDLE_H +#define PROTOCOL_HANDLE_H +#include +enum PROTOCOL_COMMAND +{ + ASK_IPC_MISSION = 0x8101, + REPLY_IPC_MISSION = 0x0101, + PROTOCOL_COMMAND_END +}; +class VProtocolParam +{ +public: + VProtocolParam(const PROTOCOL_COMMAND &command) : mCommand(command) {} + virtual ~VProtocolParam() = default; + const PROTOCOL_COMMAND mCommand; +}; +template +class ProtocolParam : public VProtocolParam +{ + +public: + ProtocolParam(const PROTOCOL_COMMAND &command, T &value) : VProtocolParam(command), mData(value) {} + virtual ~ProtocolParam() = default; + +public: + /** + * @brief + * Protocol data. + */ + T mData; +}; +class ProtocolHandle +{ +public: + ProtocolHandle() = default; + virtual ~ProtocolHandle() = default; + virtual void *GetProtocolDataBuff(void) { return nullptr; } + virtual size_t GetProtocolDataLength(void) { return 0; } + +public: + static std::shared_ptr CreateProtocolData(const std::shared_ptr ¶m); +}; +#endif \ No newline at end of file diff --git a/utils/UartDevice/include/UartDevice.h b/utils/UartDevice/include/UartDevice.h index 05a8488..09a4a28 100644 --- a/utils/UartDevice/include/UartDevice.h +++ b/utils/UartDevice/include/UartDevice.h @@ -50,7 +50,7 @@ const StatusCode IUartOpen(void *object); * @param buffLength The length of data to be sent. * @return const size_t */ -const size_t IUartSend(void *object, const char *buff, const size_t buffLength); +const size_t IUartSend(void *object, const void *buff, const size_t buffLength); /** * @brief Use a serial port to receive data. * @@ -60,7 +60,7 @@ const size_t IUartSend(void *object, const char *buff, const size_t buffLength); * @param timeoutMs The timeout time in milliseconds. * @return const size_t */ -const size_t IUartRecv(void *object, char *buff, const size_t buffLength, const unsigned int timeoutMs); +const size_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 e4f10f8..ba56909 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 char *buff, const size_t buffLength) +const size_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 char *buff, const size_t buffLength) } return ((UartDevice *)object)->mSend((UartDevice *)object, buff, buffLength); } -const size_t IUartRecv(void *object, char *buff, const size_t buffLength, const unsigned int timeoutMs) +const size_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 6bac887..21af015 100644 --- a/utils/UartDevice/src/UartDeviceImpl.cpp +++ b/utils/UartDevice/src/UartDeviceImpl.cpp @@ -38,7 +38,7 @@ const StatusCode UartDeviceImpl::UartOpen(void) return SetConfig(); } -const size_t UartDeviceImpl::UartSend(const char *buff, const size_t &buffLength) +const size_t UartDeviceImpl::UartSend(const void *buff, const size_t &buffLength) { constexpr int SEND_FAILED = -1; size_t writeLength = 0; @@ -61,7 +61,7 @@ const size_t UartDeviceImpl::UartSend(const char *buff, const size_t &buffLength tcflush(mFd, TCOFLUSH); return writeTotal; } -const size_t UartDeviceImpl::UartRecv(char *buff, const size_t &buffLength, const unsigned int &timeOutMs, +const size_t UartDeviceImpl::UartRecv(void *buff, const size_t &buffLength, const unsigned int &timeOutMs, const unsigned int delayReadMs) { constexpr int RECV_FAILED = -1; @@ -229,12 +229,12 @@ static const StatusCode UartOpen(UartDevice *object) UartDeviceImpl_S *impl = UartDeviceImplConvert(object); return impl->mUartImpl->UartOpen(); } -static const size_t UartSend(UartDevice *object, const char *buff, const size_t buffLength) +static const size_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, char *buff, const size_t buffLength, const unsigned int timeoutMs) +static const size_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 8218d9f..ac80114 100644 --- a/utils/UartDevice/src/UartDeviceImpl.h +++ b/utils/UartDevice/src/UartDeviceImpl.h @@ -29,8 +29,8 @@ public: UartDeviceImpl(const UartInfo &uatrInfo); virtual ~UartDeviceImpl() = default; const StatusCode UartOpen(void); - const size_t UartSend(const char *buff, const size_t &buffLength); - const size_t UartRecv(char *buff, const size_t &buffLength, const unsigned int &timeOutMs = 0, + 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); void UartTcflush(void); @@ -45,8 +45,8 @@ typedef struct uart_device UartDevice; typedef struct uart_device { const StatusCode (*mOpen)(UartDevice *); - const size_t (*mSend)(UartDevice *, const char *, const size_t); - const size_t (*mRecv)(UartDevice *, char *, const size_t, const unsigned int); + const size_t (*mSend)(UartDevice *, const void *, const size_t); + const size_t (*mRecv)(UartDevice *, void *, const size_t, const unsigned int); void (*mTcflush)(UartDevice *); void (*mFree)(void *); } UartDevice;