diff --git a/middleware/DeviceManager/CMakeLists.txt b/middleware/DeviceManager/CMakeLists.txt index 5c8e96fc..dede2ebc 100644 --- a/middleware/DeviceManager/CMakeLists.txt +++ b/middleware/DeviceManager/CMakeLists.txt @@ -22,7 +22,7 @@ aux_source_directory(./src SRC_FILES) set(TARGET_NAME DeviceManager) add_library(${TARGET_NAME} STATIC ${SRC_FILES}) -target_link_libraries(${TARGET_NAME} StatusCode Log) +target_link_libraries(${TARGET_NAME} Hal StatusCode Log) if ("${CLANG_TIDY_SUPPORT}" MATCHES "true") add_custom_target( diff --git a/middleware/DeviceManager/include/IDeviceManager.h b/middleware/DeviceManager/include/IDeviceManager.h index 588947be..ce8969db 100644 --- a/middleware/DeviceManager/include/IDeviceManager.h +++ b/middleware/DeviceManager/include/IDeviceManager.h @@ -14,6 +14,7 @@ */ #ifndef IDEVICEMANAGER_H #define IDEVICEMANAGER_H +#include "ILog.h" #include "StatusCode.h" #include #include diff --git a/middleware/DeviceManager/src/DeviceManager.cpp b/middleware/DeviceManager/src/DeviceManager.cpp index 9d4149d0..72996633 100644 --- a/middleware/DeviceManager/src/DeviceManager.cpp +++ b/middleware/DeviceManager/src/DeviceManager.cpp @@ -14,6 +14,7 @@ */ #include "DeviceManager.h" +#include "LedTimer.h" #include const StatusCode DeviceManager::Init(void) @@ -26,7 +27,10 @@ const StatusCode DeviceManager::Init(void) std::shared_ptr ledOut = std::make_shared(ledHal, NEW_LED_STATE, DEFAULT_KEEP_ALIVE_TIME, LED_NOT_BLINK); mLedManagers.push_back(ledOut); + LedTimer::GetInstance()->AddTimerLedManager(ledOut); } + + LedTimer::GetInstance()->Init(); return CreateStatusCode(STATUS_CODE_OK); } @@ -35,7 +39,7 @@ const StatusCode DeviceManager::UnInit(void) if (!mLedManagers.empty()) { mLedManagers.clear(); } - + LedTimer::GetInstance()->UnInit(); return CreateStatusCode(STATUS_CODE_OK); } @@ -44,6 +48,10 @@ const IpcMission DeviceManager::GetIpcMission(void) { return IpcMission::TEST; } const StatusCode DeviceManager::ISetLedState(std::string ledName, LedState &CurrentState, const unsigned int &KeepAliveTime, const unsigned int &BlinkPeriod) { + if (!ledName.empty() || *ledName.c_str() == '\0') { + LogDebug("\n\n\n\n %s %d\n\n\n", __func__, __LINE__); + } + for (auto it = mLedManagers.begin(); it != mLedManagers.end(); ++it) { std::shared_ptr ledOut = *it; if (ledOut->GetLedHal()->GetLedName() == ledName) { @@ -51,5 +59,6 @@ const StatusCode DeviceManager::ISetLedState(std::string ledName, LedState &Curr return CreateStatusCode(STATUS_CODE_OK); } } + LogWarning("ledName(%s) not found", ledName.c_str()); return CreateStatusCode(STATUS_CODE_NOT_OK); } diff --git a/middleware/DeviceManager/src/DeviceManagerMakePtr.cpp b/middleware/DeviceManager/src/DeviceManagerMakePtr.cpp index 8f6d0c1b..758a9fac 100644 --- a/middleware/DeviceManager/src/DeviceManagerMakePtr.cpp +++ b/middleware/DeviceManager/src/DeviceManagerMakePtr.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ #include "DeviceManagerMakePtr.h" +#include "DeviceManager.h" #include "ILog.h" bool CreateDeviceManagerModule(void) { @@ -35,7 +36,7 @@ std::shared_ptr &DeviceManagerMakePtr::GetInstance(std::sh } const StatusCode DeviceManagerMakePtr::CreateDeviceManager(std::shared_ptr &impl) { - auto tmp = std::make_shared(); + auto tmp = std::make_shared(); impl = tmp; return CreateStatusCode(STATUS_CODE_OK); } \ No newline at end of file diff --git a/middleware/DeviceManager/src/LedManager.cpp b/middleware/DeviceManager/src/LedManager.cpp index eceb2ccc..bfc3c775 100644 --- a/middleware/DeviceManager/src/LedManager.cpp +++ b/middleware/DeviceManager/src/LedManager.cpp @@ -52,14 +52,14 @@ StatusCode LedManager::GetLedState(LedState &CurrentState) return CreateStatusCode(STATUS_CODE_OK); } -StatusCode LedManager::BlinkOn(LedState CurrentState, unsigned int KeepAliveTime) -{ - mKeepAliveTime = KeepAliveTime; - return CreateStatusCode(STATUS_CODE_OK); -} +// StatusCode LedManager::BlinkOn(LedState CurrentState, unsigned int KeepAliveTime) +// { +// mKeepAliveTime = KeepAliveTime; +// return CreateStatusCode(STATUS_CODE_OK); +// } -StatusCode LedManager::BlinkOff(void) -{ - mCurrentState = LedState::LED_STATE_OFF; - return CreateStatusCode(STATUS_CODE_OK); -} +// StatusCode LedManager::BlinkOff(void) +// { +// mCurrentState = LedState::LED_STATE_OFF; +// return CreateStatusCode(STATUS_CODE_OK); +// } diff --git a/middleware/DeviceManager/src/LedManager.h b/middleware/DeviceManager/src/LedManager.h index e9caf07a..133aecf2 100644 --- a/middleware/DeviceManager/src/LedManager.h +++ b/middleware/DeviceManager/src/LedManager.h @@ -44,8 +44,8 @@ public: StatusCode SetLedState(LedState &CurrentState, const unsigned int &KeepAliveTime = DEFAULT_KEEP_ALIVE_TIME, const unsigned int &BlinkPeriod = LED_NOT_BLINK); StatusCode GetLedState(LedState &CurrentState); - StatusCode BlinkOn(LedState CurrentState, unsigned int KeepAliveTime); - StatusCode BlinkOff(void); + // StatusCode BlinkOn(LedState CurrentState, unsigned int KeepAliveTime); + // StatusCode BlinkOff(void); private: std::shared_ptr mLedHal; diff --git a/middleware/DeviceManager/src/LedTimer.cpp b/middleware/DeviceManager/src/LedTimer.cpp new file mode 100644 index 00000000..22453547 --- /dev/null +++ b/middleware/DeviceManager/src/LedTimer.cpp @@ -0,0 +1,76 @@ +/* + * 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 "LedTimer.h" +// #include "Log.h" +LedTimer::LedTimer() { mTimerRuning = false; } +std::shared_ptr &LedTimer::GetInstance(std::shared_ptr *impl) +{ + static auto instance = std::make_shared(); + // if (impl) + // { + // instance = *impl; + // } + return instance; +} + +// void LedTimer::Init(std::vector> &LedManagers) +void LedTimer::Init(void) +{ + // TimerLedManagers = LedManagers; + StartTimer(); +} +void LedTimer::UnInit(void) +{ + StopTimer(); + TimerLedManagers.clear(); +} +void LedTimer::StartTimer(void) +{ + + auto timerThread = [](std::shared_ptr timer) { timer->Timer(); }; + mTimer = std::thread(timerThread, shared_from_this()); +} +void LedTimer::StopTimer(void) +{ + mTimerRuning = false; + if (mTimer.joinable()) { + mTimer.join(); + } +} +void LedTimer::Timer(void) +{ + mTimerRuning = true; + while (mTimerRuning) { + mMutex.lock(); + CheckState(); + mMutex.unlock(); + std::this_thread::sleep_for(std::chrono::milliseconds(LED_STATE_CHECK_PERIOD_MS)); + } +} +const StatusCode LedTimer::AddTimerLedManager(std::shared_ptr &LedManager) +{ + TimerLedManagers.push_back(LedManager); + return CreateStatusCode(STATUS_CODE_OK); +} +const StatusCode LedTimer::CheckState(void) +{ + int count = 0; + for (auto it = TimerLedManagers.begin(); it != TimerLedManagers.end(); ++it) { + std::shared_ptr LedManager = *it; + LogInfo("%s %d count = %d\n\n", __func__, __LINE__, count++); + } + return CreateStatusCode(STATUS_CODE_OK); +} diff --git a/middleware/DeviceManager/src/LedTimer.h b/middleware/DeviceManager/src/LedTimer.h new file mode 100644 index 00000000..68c9aef9 --- /dev/null +++ b/middleware/DeviceManager/src/LedTimer.h @@ -0,0 +1,50 @@ +/* + * 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 LEDTIMER_H +#define LEDTIMER_H + +#include "LedManager.h" +#include +#include +#include +#include + +constexpr int LED_STATE_CHECK_PERIOD_MS = 100; + +class LedTimer : public std::enable_shared_from_this +{ +public: + LedTimer(); + ~LedTimer() = default; + static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); + // void Init(std::vector> &LedManagers); + void Init(void); + void UnInit(void); + void Timer(void); + const StatusCode CheckState(void); + const StatusCode AddTimerLedManager(std::shared_ptr &LedManager); + +private: + void StartTimer(void); + void StopTimer(void); + +private: + std::vector> TimerLedManagers; + std::mutex mMutex; + bool mTimerRuning; + std::thread mTimer; +}; + +#endif \ No newline at end of file diff --git a/test/middleware/CMakeLists.txt b/test/middleware/CMakeLists.txt index 2f0de7b8..d6d93b9e 100644 --- a/test/middleware/CMakeLists.txt +++ b/test/middleware/CMakeLists.txt @@ -2,4 +2,5 @@ # cmake_minimum_required(VERSION 2.8.0) add_subdirectory(IpcConfig) add_subdirectory(McuManager) -add_subdirectory(McuAskBase) \ No newline at end of file +add_subdirectory(McuAskBase) +add_subdirectory(LedTest) \ No newline at end of file diff --git a/test/middleware/LedTest/CMakeLists.txt b/test/middleware/LedTest/CMakeLists.txt new file mode 100644 index 00000000..f77eea0d --- /dev/null +++ b/test/middleware/LedTest/CMakeLists.txt @@ -0,0 +1,84 @@ +# 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}/IpcConfig/include + ${MIDDLEWARE_SOURCE_PATH}/DeviceManager/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) + + +# 获取当前路径下的所有源文件, 添加到 SRC_FILES_MAIN 变量中 +aux_source_directory(. SRC_FILES_MAIN) +# 获取./src路径下的所有源文件, 添加到 SRC_FILES 变量中 +aux_source_directory(./src SRC_FILES) + +set(TARGET_NAME Led_Test) + +# 生成可执行文件 +add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES}) + +target_link_libraries(${TARGET_NAME} DeviceManager gtest gmock pthread) +if(${TEST_COVERAGE} MATCHES "true") + target_link_libraries(${TARGET_NAME} gcov) +endif() + + + + +if ("${CLANG_TIDY_SUPPORT}" MATCHES "true") +add_custom_target( + Led_Test_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/*.h\"}]' + -p ${PLATFORM_PATH}/cmake-shell + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/LedTest +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make Led_Test_code_check + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) + +file(GLOB_RECURSE HEADER_FILES *.h) +add_custom_target( + Led_Test_code_format + COMMAND ${CLANG_FORMAT_EXE} + -style=file + -i ${SRC_FILES} ${SRC_FILES_MAIN} ${HEADER_FILES} + WORKING_DIRECTORY ${TEST_SOURCE_PATH}/middleware/LedTest +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make Led_Test_code_check + COMMAND make Led_Test_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/LedTest/mainTest.cpp b/test/middleware/LedTest/mainTest.cpp new file mode 100644 index 00000000..fb1d91b4 --- /dev/null +++ b/test/middleware/LedTest/mainTest.cpp @@ -0,0 +1,9 @@ +#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/LedTest/src/Led_Test.cpp b/test/middleware/LedTest/src/Led_Test.cpp new file mode 100644 index 00000000..d048192f --- /dev/null +++ b/test/middleware/LedTest/src/Led_Test.cpp @@ -0,0 +1,21 @@ +#include "IDeviceManager.h" +#include "ILog.h" +#include + +namespace Led_Test +{ +// ../output_files/test/bin/Led_Test --gtest_filter=Led_Test.Demo + +TEST(Led_Test, Demo) +{ + // 初始化Log + CreateLogModule(); + CreateDeviceManagerModule(); + + IDeviceManager::GetInstance()->Init(); + LedState _Led_State = LED_STATE_RED; + IDeviceManager::GetInstance()->ISetLedState("Led_testing", _Led_State, 10, 0); + IDeviceManager::GetInstance()->UnInit(); +} + +} /*namespace Led_Test*/ diff --git a/test/utils/UartDevice/src/UartDevice_Test.cpp b/test/utils/UartDevice/src/UartDevice_Test.cpp index d1e8cd34..8d644971 100644 --- a/test/utils/UartDevice/src/UartDevice_Test.cpp +++ b/test/utils/UartDevice/src/UartDevice_Test.cpp @@ -26,11 +26,13 @@ TEST(UartDeviceTest, UNIT_UartDevice_AUTO_IllegalObject) { CreateLogModule(); ILogInit(LOG_INSTANCE_TYPE_END); + IUartOpen(nullptr); IUartSend(nullptr, nullptr, 0); IUartRecv(nullptr, nullptr, 0, 0); IUartTcflush(nullptr); IUartDeviceFree(nullptr); + char illegalObject[100] = {0}; void *object = &illegalObject[10]; IUartOpen(object);