diff --git a/CMakeLists.txt b/CMakeLists.txt index e0914214..3c9e8b31 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,12 @@ set(CMAKE_CXX_STANDARD_INCLUDE_DIRECTORIES ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTOR message("platform = ${TARGET_PLATFORM}") message("platform PATH = ${PLATFORM_PATH}") +add_custom_target( + sdk_clean + COMMAND echo "sdk clean finished." + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) + # Gdb debug include(build/sdk_config.cmake) @@ -166,22 +172,6 @@ set(TEST_LINUX_MOCK "" CACHE STRING INTERNAL) # if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX}) set(TEST_LINK_LIB "testUtils" CACHE STRING INTERNAL FORCE) # endif() -if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX}) - # set(TEST_LINUX_MOCK "-Wl,--wrap=fopen,--wrap=fprintf_gpio,--wrap=fprintf_dir" CACHE STRING INTERNAL FORCE) - # set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=tcgetattr" CACHE STRING INTERNAL FORCE) - # set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=tcsetattr" CACHE STRING INTERNAL FORCE) - # set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=gethostbyname" CACHE STRING INTERNAL FORCE) - # set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=connect" CACHE STRING INTERNAL FORCE) - # set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=socket" CACHE STRING INTERNAL FORCE) - # set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=select" CACHE STRING INTERNAL FORCE) - # set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=linux_open" CACHE STRING INTERNAL FORCE) - # set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=linux_read" CACHE STRING INTERNAL FORCE) - # set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=linux_write" CACHE STRING INTERNAL FORCE) - # set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=linux_close" CACHE STRING INTERNAL FORCE) - # set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=linux_fclose" CACHE STRING INTERNAL FORCE) - # set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=linux_fread" CACHE STRING INTERNAL FORCE) - # set(TEST_LINUX_MOCK "${TEST_LINUX_MOCK},--wrap=linux_fcntl" CACHE STRING INTERNAL FORCE) -endif() # 添加编译目录 add_subdirectory(external) diff --git a/README.md b/README.md index 8f33687a..79f7de70 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,8 @@ -<<<<<<< HEAD # 1. 一个嵌入式软件架构的示例 ## 1.1. 概述   从嵌入式软件角度来描述软件架构开发方法。 -======= -# 1. 一个嵌入式软件架构的示例。 - -## 1.1. 概述 - -   从嵌入式软件角度来描述软件架构开发方法。 ->>>>>>> embedded-framework/master 欢迎在仓库里面提issue交流学习。 @@ -30,7 +22,6 @@ title 嵌入式软件开发 "工具使用" : 5 ``` -<<<<<<< HEAD   本仓库从上述全方面来部署一个嵌入式软件开发项目。各部分环环相扣不分彼此,组成一个完整的健康的敏捷的可持续的嵌入式开发体系。 ### 1.2.1. 源码构建 @@ -44,21 +35,6 @@ title 嵌入式软件开发   通俗来讲,CMakeList.txt脚本就是告诉编译器,从哪里搜索头文件 / 把哪些源代码编译 / 从哪里找到哪些链接库 /输出什么文件到哪个目录。 ```code -======= -    本仓库从上述全方面来部署一个嵌入式软件开发项目。各部分环环相扣不分彼此,组成一个完整的健康的敏捷的可持续的嵌入式开发体系。 - - ### 1.2.1. 源码构建 - -    源码构建是一个嵌入式软件项目的开始,属于嵌入式软件开发的最基础要素,他决定了开发者使用何种方式何种工具来部署 / 编译项目源码。源码构建方案设计会直接影响到开发效率。 - -    源码构建的工具很多,嵌入式开发中常见的有Makefile / CMake / Scons / GN等,例如:国内比较热门的开源项目鸿蒙系统,使用GN作为构建工具。由于历史原因,Makefile是相对古老的源码构建工具,大部分开源项目使用的都是Makefile,随着发展,部分开源项目开始增加使用CMake构建脚本,此时开源项目同时支持Makefile和CMake两种构建方案。构建工具的选择没有好坏之分,大部分后开发的工具都会针对性解决前者的一些缺陷,往往后来者会有更多的优点。本仓库自研部分使用的是CMake工具进行源码构建。 - - **一个CMakeList.txt脚本示例** - -   通俗来讲,CMakeList.txt脚本就是告诉编译器,从哪里搜索头文件 / 把哪些源代码编译 / 从哪里找到哪些链接库 /输出什么文件到哪个目录。 - -``` ->>>>>>> embedded-framework/master include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake) // 一个配置文件 set(EXECUTABLE_OUTPUT_PATH ${EXEC_OUTPUT_PATH}) // 设置可执行文件的输出目录 set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH}) // 设置库的输出目录 @@ -111,12 +87,8 @@ endif()   在编译的时候,使用clang-tidy工具针对性选择对自研部分的代码进行静态检测。 示例: -<<<<<<< HEAD ```code -======= -``` ->>>>>>> embedded-framework/master add_custom_target( SharedData_code_check COMMAND ${CLANG_TIDY_EXE} @@ -134,12 +106,8 @@ add_custom_target(   在编译的时候,使用clang-format工具针对性选择对自研部分的代码进行自动格式化,实现编码格式的客观绝对统一。 示例: -<<<<<<< HEAD ```code -======= -``` ->>>>>>> embedded-framework/master add_custom_target( SharedData_code_format COMMAND ${CLANG_FORMAT_EXE} @@ -151,8 +119,4 @@ add_custom_target( ### 1.2.2. 架构设计 -<<<<<<< HEAD   抛开产品业务不进行描述,代码部署主要使用分层模块化的代码结构,结合面向对象的多态设计模式进行代码开发,使得业务代码在部署时具备灵活多变的特性,进一步提高代码的复用性,最终实现产品业务软件层面的敏捷迭代。 -======= -   抛开产品业务不进行描述,代码部署主要使用分层模块化的代码结构,结合面向对象的多态设计模式进行代码开发,使得业务代码在部署时具备灵活多变的特性,进一步提高代码的复用性,最终实现产品业务软件层面的敏捷迭代。 ->>>>>>> embedded-framework/master diff --git a/application/CMakeLists.txt b/application/CMakeLists.txt new file mode 100644 index 00000000..b4a994c7 --- /dev/null +++ b/application/CMakeLists.txt @@ -0,0 +1,3 @@ +add_subdirectory(main) +add_subdirectory(HuntingCamera) +add_subdirectory(MissionManager) \ No newline at end of file diff --git a/application/HuntingCamera/CMakeLists.txt b/application/HuntingCamera/CMakeLists.txt new file mode 100644 index 00000000..5e922eda --- /dev/null +++ b/application/HuntingCamera/CMakeLists.txt @@ -0,0 +1,73 @@ +include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake) +include(${APPLICATION_SOURCE_PATH}/HuntingCamera/build/hunting_camera.cmake) +set(EXECUTABLE_OUTPUT_PATH ${EXEC_OUTPUT_PATH}) +set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH}) + +include_directories( + ${HUNTTING_MAIN_INCLUDE_PATH} +) + +link_directories( + ${LIBS_OUTPUT_PATH} + ${EXTERNAL_LIBS_OUTPUT_PATH} +) + +aux_source_directory(. SRC_FILES) +aux_source_directory(./src MAIN_SRC_FILE_THIS) + +# Mark src files for test. +# file(GLOB_RECURSE MAIN_SRC_FILE_THIS src/*.cpp src/*.c) +# set(MAIN_SRC_FILE "${MAIN_SRC_FILE_THIS}" CACHE STRING INTERNAL FORCE) + +set(TARGET_LIB HuntingMainLib) +add_library(${TARGET_LIB} STATIC ${MAIN_SRC_FILE_THIS}) +set(TARGET_NAME HuntingCamera_x86) +add_executable(${TARGET_NAME} ${SRC_FILES}) + +target_link_libraries(${TARGET_LIB} ${HUNTTING_LINK_LIB}) +target_link_libraries(${TARGET_NAME} ${TARGET_LIB}) +if(${TEST_COVERAGE} MATCHES "true") + target_link_libraries(${TARGET_NAME} gcov) +endif() + +if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true") +add_custom_target( + HuntingCamera_code_check + COMMAND ${CLANG_TIDY_EXE} + -checks='${CLANG_TIDY_CHECKS}' + --header-filter=.* + --system-headers=false + ${SRC_FILES} + ${MAIN_SRC_FILE_THIS} + ${CLANG_TIDY_CONFIG} + -p ${PLATFORM_PATH}/cmake-shell + WORKING_DIRECTORY ${APPLICATION_SOURCE_PATH}/HuntingCamera +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make HuntingCamera_code_check + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) +file(GLOB_RECURSE HEADER_FILES *.h) +add_custom_target( + HuntingCamera_code_format + COMMAND ${CLANG_FORMAT_EXE} + -style=file + -i ${SRC_FILES} ${MAIN_SRC_FILE_THIS} ${HEADER_FILES} + WORKING_DIRECTORY ${APPLICATION_SOURCE_PATH}/HuntingCamera +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make HuntingCamera_code_check + COMMAND make HuntingCamera_code_format + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) +endif() + +define_file_name(${TARGET_LIB}) +define_file_name(${TARGET_NAME}) + +file(GLOB_RECURSE INSTALL_HEADER_FILES *.h) +install(FILES ${INSTALL_HEADER_FILES} DESTINATION include) \ No newline at end of file diff --git a/application/HuntingCamera/build/hunting_camera.cmake b/application/HuntingCamera/build/hunting_camera.cmake new file mode 100644 index 00000000..3af19096 --- /dev/null +++ b/application/HuntingCamera/build/hunting_camera.cmake @@ -0,0 +1,16 @@ +set(HUNTTING_MAIN_INCLUDE_PATH "${APPLICATION_SOURCE_PATH}/HuntingCamera/src") +set(HUNTTING_MAIN_INCLUDE_PATH "${HUNTTING_MAIN_INCLUDE_PATH};${APPLICATION_SOURCE_PATH}/MissionManager/include") +set(HUNTTING_MAIN_INCLUDE_PATH "${HUNTTING_MAIN_INCLUDE_PATH};${MIDDLEWARE_SOURCE_PATH}/StateMachine/include") +set(HUNTTING_MAIN_INCLUDE_PATH "${HUNTTING_MAIN_INCLUDE_PATH};${MIDDLEWARE_SOURCE_PATH}/McuManager/include") +set(HUNTTING_MAIN_INCLUDE_PATH "${HUNTTING_MAIN_INCLUDE_PATH};${MIDDLEWARE_SOURCE_PATH}/AppManager/include") +set(HUNTTING_MAIN_INCLUDE_PATH "${HUNTTING_MAIN_INCLUDE_PATH};${MIDDLEWARE_SOURCE_PATH}/MediaManager/include") +set(HUNTTING_MAIN_INCLUDE_PATH "${HUNTTING_MAIN_INCLUDE_PATH};${MIDDLEWARE_SOURCE_PATH}/FilesManager/include") +set(HUNTTING_MAIN_INCLUDE_PATH "${HUNTTING_MAIN_INCLUDE_PATH};${MIDDLEWARE_SOURCE_PATH}/StorageManager/include") +set(HUNTTING_MAIN_INCLUDE_PATH "${HUNTTING_MAIN_INCLUDE_PATH};${MIDDLEWARE_SOURCE_PATH}/HuntingUpgrade/include") +set(HUNTTING_MAIN_INCLUDE_PATH "${HUNTTING_MAIN_INCLUDE_PATH};${MIDDLEWARE_SOURCE_PATH}/DeviceManager/include") +set(HUNTTING_MAIN_INCLUDE_PATH "${HUNTTING_MAIN_INCLUDE_PATH};${MIDDLEWARE_SOURCE_PATH}/IpcConfig/include") +set(HUNTTING_MAIN_INCLUDE_PATH "${HUNTTING_MAIN_INCLUDE_PATH};${UTILS_SOURCE_PATH}/StatusCode/include") +set(HUNTTING_MAIN_INCLUDE_PATH "${HUNTTING_MAIN_INCLUDE_PATH};${UTILS_SOURCE_PATH}/Log/include") +set(HUNTTING_MAIN_INCLUDE_PATH "${HUNTTING_MAIN_INCLUDE_PATH};${HAL_SOURCE_PATH}/include") + +set(HUNTTING_LINK_LIB McuManager MissionManager StateMachine AppManager FilesManager StorageManager HuntingUpgrade IpcConfig DeviceManager StatusCode Log Hal pthread dl) \ No newline at end of file diff --git a/application/HuntingCamera/main.cpp b/application/HuntingCamera/main.cpp new file mode 100644 index 00000000..8c1b801c --- /dev/null +++ b/application/HuntingCamera/main.cpp @@ -0,0 +1,24 @@ +/* + * 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 "ILog.h" +#include "MainThread.h" +#include +int main(int argc, char *argv[]) +{ + MainThread::GetInstance()->Init(); + MainThread::GetInstance()->Runing(); + MainThread::GetInstance()->UnInit(); + return 0; +} \ No newline at end of file diff --git a/application/HuntingCamera/src/MainThread.cpp b/application/HuntingCamera/src/MainThread.cpp new file mode 100644 index 00000000..b1456515 --- /dev/null +++ b/application/HuntingCamera/src/MainThread.cpp @@ -0,0 +1,132 @@ +/* + * 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 "MainThread.h" +#include "IAppManager.h" +#include "IDeviceManager.h" +#include "IFilesManager.h" +#include "IHalCpp.h" +#include "IHuntingUpgrade.h" +#include "IIpcConfig.h" +#include "ILog.h" +#include "IMcuManager.h" +#include "IMediaManager.h" +#include "IMissionManager.h" +#include "IStateMachine.h" +#include "IStorageManager.h" +#include +#include +static void sigHandler(int signo) +{ + LogInfo("Stop main application.\n"); + MainThread::GetInstance()->Exit(); +} +void InitSignalHandle(void) +{ + signal(SIGINT, sigHandler); + signal(SIGTERM, sigHandler); + signal(SIGKILL, sigHandler); + signal(SIGPIPE, SIG_IGN); +} +MainThread::MainThread() +{ + mMainThreadRuning = false; +} +std::shared_ptr &MainThread::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; +} +StatusCode MainThread::Init(void) +{ + InitSignalHandle(); + CreateLogModule(); + ILogInit(LOG_EASYLOGGING); + CustomizationInit(); + mMainThreadRuning = true; + CreateAllModules(); + IHalCpp::GetInstance()->Init(); + IDeviceManager::GetInstance()->Init(); + IMcuManager::GetInstance()->Init(); + IStorageManager::GetInstance()->Init(); + IIpcConfig::GetInstance()->Init(); + IMediaManager::GetInstance()->Init(); + IMissionManager::GetInstance()->Init(); + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode MainThread::UnInit(void) +{ + IMissionManager::GetInstance()->UnInit(); + IMediaManager::GetInstance()->UnInit(); + IIpcConfig::GetInstance()->UnInit(); + IStorageManager::GetInstance()->UnInit(); + IMcuManager::GetInstance()->UnInit(); + IDeviceManager::GetInstance()->UnInit(); + IHalCpp::GetInstance()->UnInit(); + DestoryAllModules(); + ILogUnInit(); + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode MainThread::CreateAllModules(void) +{ + CreateHalCppModule(); + CreateMcuManager(); + CreateStorageManagerModule(); + CreateFilesManagerModule(); + CreateMissionManagerModule(); + CreateStateMachine(); + CreateAppManagerModule(); + CreateMediaManagerModule(); + CreateHuntingUpgradeModule(); + CreateIpcConfigModule(); + CreateDeviceManagerModule(); + return CreateStatusCode(STATUS_CODE_OK); +} +void MainThread::DestoryAllModules(void) +{ + DestroyHuntingUpgradeModule(); + DestroyMediaManagerModule(); + DestroyAppManagerModule(); + DestroyStateMachine(); + DestroyMissionManagerModule(); + DestroyFilesManagerModule(); + DestroyStorageManagerModule(); + DestroyMcuManager(); + DestroyHalCppModule(); + DestroyIpcConfigModule(); + DestroyDeviceManagerModule(); +} +void MainThread::ResetAllPtrMaker(void) +{ +} +void MainThread::Runing(void) +{ + while (mMainThreadRuning) { + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + } +} +extern bool CreateProtocolHandleImpl(void); +void MainThread::CustomizationInit(void) +{ + CreateProtocolHandleImpl(); +} \ No newline at end of file diff --git a/application/HuntingCamera/src/MainThread.h b/application/HuntingCamera/src/MainThread.h new file mode 100644 index 00000000..a3361e09 --- /dev/null +++ b/application/HuntingCamera/src/MainThread.h @@ -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. + */ +#ifndef MAIN_THREAD_H +#define MAIN_THREAD_H +#include "StatusCode.h" +#include +class MainThread +{ +public: + MainThread(); + virtual ~MainThread() = default; + static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); + virtual StatusCode Init(void); + virtual StatusCode UnInit(void); + void Runing(void); + void Exit(void) + { + mMainThreadRuning = false; + } + virtual void CustomizationInit(void); + +private: + StatusCode CreateAllModules(void); + void DestoryAllModules(void); + void ResetAllPtrMaker(void); + +private: + bool mMainThreadRuning; +}; +#endif // !MAIN_THREAD_H \ No newline at end of file diff --git a/application/MissionManager/CMakeLists.txt b/application/MissionManager/CMakeLists.txt new file mode 100644 index 00000000..10d17bb1 --- /dev/null +++ b/application/MissionManager/CMakeLists.txt @@ -0,0 +1,73 @@ +include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake) +# include(${UTILS_SOURCE_PATH}/WebServer/build/webserver.cmake) +include(${MIDDLEWARE_SOURCE_PATH}/AppManager/build/app_manager.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}/KeyControl/include + ${UTILS_SOURCE_PATH}/LedControl/include + ${MIDDLEWARE_SOURCE_PATH}/StateMachine/include + ${MIDDLEWARE_SOURCE_PATH}/AppManager/include + ${MIDDLEWARE_SOURCE_PATH}/MediaManager/include + ${MIDDLEWARE_SOURCE_PATH}/FilesManager/include + ${MIDDLEWARE_SOURCE_PATH}/StorageManager/include + ${MIDDLEWARE_SOURCE_PATH}/McuManager/include + ${MIDDLEWARE_SOURCE_PATH}/McuAskBase/include + ${MIDDLEWARE_SOURCE_PATH}/HuntingUpgrade/include + ${MIDDLEWARE_SOURCE_PATH}/DeviceManager/include +) +#do not rely on any other library +#link_directories( +#) + +aux_source_directory(./src SRC_FILES) + +set(TARGET_NAME MissionManager) +add_library(${TARGET_NAME} STATIC ${SRC_FILES}) + +target_link_libraries(${TARGET_NAME} McuAskBase StateMachine MediaManager StorageManager DeviceManager HuntingUpgrade KeyControl LedControl StatusCode Log) + +if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true") +add_custom_target( + MissionManager_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 ${APPLICATION_SOURCE_PATH}/MissionManager +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make MissionManager_code_check + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) +file(GLOB_RECURSE HEADER_FILES *.h) +add_custom_target( + MissionManager_code_format + COMMAND ${CLANG_FORMAT_EXE} + -style=file + -i ${SRC_FILES} ${HEADER_FILES} + WORKING_DIRECTORY ${APPLICATION_SOURCE_PATH}/MissionManager +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make MissionManager_code_check + COMMAND make MissionManager_code_format + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) +endif() + +define_file_name(${TARGET_NAME}) + +file(GLOB_RECURSE INSTALL_HEADER_FILES include/*.h) +install(FILES ${INSTALL_HEADER_FILES} DESTINATION include) \ No newline at end of file diff --git a/application/MissionManager/README.md b/application/MissionManager/README.md new file mode 100644 index 00000000..a68c1a95 --- /dev/null +++ b/application/MissionManager/README.md @@ -0,0 +1,82 @@ + +# 1. 相机任务管理 + +  相机主业务逻辑使用状态机机制进行管理。 + +## 1.1. 任务状态 + +  任务状态是指相机启动需要执行的任务,可能是拍照 / 视频,可能是其它任务。 + +**例如:** +1. 移动物体侦测启动; +2. 定时启动拍照 / 录像; +3. 测试启动; + +## 1.2. 状态机设计 + +### 1.2.1. 状态树设计图 + +```mermaid +stateDiagram-v2 +[*] --> TopState +TopState --> PowerOff +TopState --> MSDCState +TopState --> DeviceAbnormal +TopState --> MissionState +MissionState --> 空闲 +MissionState --> 存储管理 +存储管理 --> EMMC +存储管理 --> SD卡 +MissionState --> 媒体管理 +SD卡 --> 插卡 +SD卡 --> 拔卡 +SD卡 --> 卡异常 +MissionState --> 网络管理 +网络管理 --> 联网 +联网 --> 上传文件 +网络管理 --> 未联网 +MissionState --> 直播 +MissionState --> 4G管理 +4G管理 --> Sim卡初始化 +4G管理 --> 注网状态 +MissionState --> Upgrade +``` + +1. 任意状态处理命令时,不能阻塞,否则整个应用将会瘫痪无法响应任意的命令; +2. MissionState: 任务状态,在此状态下,由任务状态处理各种逻辑命令,根据逻辑命令定义,切换到相应的状态再处理数据,处理完数据会停留在当前状态,等待新的命令; + +## 1.3. 任务状态获取启动 + +  应用程序运行后,首先需要知道主控是由于何种任务被唤醒,然后根据任务来执行相应的功能代码; + +**时序图** + +```mermaid +sequenceDiagram +participant MCU +participant 大核 +MCU ->> MCU:待机 +opt MCU上电 + MCU ->> 大核:上电 + activate 大核 + 大核 ->> 大核:系统初始化 + 大核 ->> 大核:启动脚本拉起APP + 大核 ->> +MCU:读取启动任务 + MCU -->> -大核:return + alt PIR触发 + 大核 ->> 大核:APP初始化 + 大核 ->> 大核:抓拍并保存 + else 定时 + 大核 ->> 大核:APP初始化 + 大核 ->> 大核:抓拍并保存 + else TEST + 大核 ->> 大核:APP初始化 + 大核 ->> 大核:待机 + end + deactivate 大核 +end +``` + +## 1.4. MCU监视器 + +  MCU监视器必须由其中一个状态继承,只有状态机运行之后才能处理串口命令。 \ No newline at end of file diff --git a/application/MissionManager/include/IMissionManager.h b/application/MissionManager/include/IMissionManager.h new file mode 100644 index 00000000..1cbcb5d4 --- /dev/null +++ b/application/MissionManager/include/IMissionManager.h @@ -0,0 +1,63 @@ +/* + * 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_MISSION_MANAGER +#define I_MISSION_MANAGER +#include "StatusCode.h" +#include +#include +bool CreateMissionManagerModule(void); +bool DestroyMissionManagerModule(void); +enum class MissionEvent +{ + TEST = 0, + END +}; +class VMissionData +{ +public: + VMissionData(const MissionEvent &event) : mEvent(event) + { + mRetryTimes = 0; + mDelayTimeMs = 0; + } + virtual ~VMissionData() = default; + const MissionEvent mEvent; + int mRetryTimes; + unsigned int mDelayTimeMs; + std::function)> mMissionFinshed; +}; +template +class VMissionDataV2 : public VMissionData +{ + +public: + VMissionDataV2(const MissionEvent &event, T value) : VMissionData(event), mData(value) + { + } + virtual ~VMissionDataV2() = default; + +public: + T mData; +}; +class IMissionManager +{ +public: + IMissionManager() = default; + virtual ~IMissionManager() = default; + static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); + virtual const StatusCode Init(void); + virtual const StatusCode UnInit(void); +}; +#endif \ No newline at end of file diff --git a/application/MissionManager/src/AppMonitor.cpp b/application/MissionManager/src/AppMonitor.cpp new file mode 100644 index 00000000..1ec20014 --- /dev/null +++ b/application/MissionManager/src/AppMonitor.cpp @@ -0,0 +1,159 @@ +/* + * 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 "AppMonitor.h" +#include "ILog.h" +#include +StatusCode AppMonitor::GetProductInfo(AppGetProductInfo ¶m) +{ + LogInfo("AppMonitor::GetProductInfo.\n"); + param.mModel = "test"; + param.mCompany = "test"; + param.mSoc = "test"; + param.mSp = "test"; + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode AppMonitor::GetDeviceAttr(AppGetDeviceAttr ¶m) +{ + LogInfo("AppMonitor::GetDeviceAttr.\n"); + param.mUUID = "test"; + param.mSoftVersion = "test"; + param.mOtaVersion = "test"; + param.mHardwareVersion = "test"; + param.mSSID = "test"; + param.mBSSID = "test"; + param.mCameraNumber = "test"; + param.mCurrentCameraID = "test"; + param.mWifiReboot = "test"; + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode AppMonitor::GetMediaInfo(AppGetMediaInfo ¶m) +{ + LogInfo("AppMonitor::GetMediaInfo.\n"); + param.mRtspUrl = "rtsp://192.168.169.1/live/0"; + param.mTransport = "tcp"; + param.mPort = APP_MANAGER_TCP_SERVER_PORT; + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode AppMonitor::GetSdCardInfo(AppGetSdCardInfo ¶m) +{ + LogInfo("AppMonitor::GetSdCardInfo.\n"); + SdCardInfo info; + IStorageManager::GetInstance()->GetSdCardInfo(info); + param.mStatus = SdCardStatusConvert(info.mEvent); + param.mFree = info.mFreeSizeMB; + param.mTotal = info.mTotalSizeMB; + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode AppMonitor::GetBatteryInfo(AppGetBatteryInfo ¶m) +{ + LogInfo("AppMonitor::GetBatteryInfo.\n"); + param.mCapacity = 0; + param.mChargeStatus = ChargeStatus::CHARGING; + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode AppMonitor::GetParamValue(AppParamValue ¶m) +{ + param.mMicStatus = SwitchStatus::ON; + param.mRec = SwitchStatus::OFF; + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode AppMonitor::GetCapability(AppGetCapability ¶m) +{ + param.mPlaybackType = PlaybackType::DOWNLOAD_PLAYBACK; + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode AppMonitor::GetLockVideoStatus(LockVideoStatus ¶m) +{ + param = LockVideoStatus::LOCK; + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode AppMonitor::GetStorageInfo(std::vector ¶m) +{ + AppGetStorageInfo info; + SdCardInfo sdInfo; + IStorageManager::GetInstance()->GetSdCardInfo(sdInfo); + info.mIndex = 0; + info.mName = "TF card 1"; + info.mType = StorageType::SD_CARD_1; + info.mFree = sdInfo.mFreeSizeMB; + info.mTotal = sdInfo.mTotalSizeMB; + param.push_back(info); + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode AppMonitor::GetStorageFileList(const AppGetFileInfo &fileInfo, std::vector ¶m) +{ + if (StorageFileEvent::LOOP == fileInfo.mEvent) { + AppGetFileList file; + file.mCreateTime_s = 123456789; + file.mDuration = 182; + file.mName = "/DCIM/2024/01/15/20240115140207-20240115140509.mp4"; + file.mSize = 1024 * 182; + file.mType = StorageFileType::VIDEO; + param.push_back(file); + AppGetFileList file2; + file2.mCreateTime_s = 123456789; + file2.mDuration = 0; + file2.mName = "/34a396526922a33e97906920dbfef2a5.jpg"; + file2.mSize = 1024; + file2.mType = StorageFileType::PICTURE; + param.push_back(file2); + } + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode AppMonitor::SetDateTime(const AppSetDateTime ¶m) +{ + // + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode AppMonitor::SetTimeZone(const unsigned int &zone) +{ + // + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode AppMonitor::SetParamValue(const AppSetParamValue ¶m) +{ + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode AppMonitor::EnterRecorder(void) +{ + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode AppMonitor::AppPlayback(const PlayBackEvent &event) +{ + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode AppMonitor::UploadFile(AppUploadFile ¶m) +{ + // + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode AppMonitor::GetThumbnail(AppGetThumbnail ¶m) +{ + param.mThumbnail = "./34a396526922a33e97906920dbfef2a5.jpg"; + return CreateStatusCode(STATUS_CODE_OK); +} +SdCardStatus AppMonitor::SdCardStatusConvert(const StorageEvent &event) +{ + switch (event) { + case StorageEvent::SD_CARD_INSERT: + return SdCardStatus::NORMAL; + case StorageEvent::SD_CARD_REMOVE: + return SdCardStatus::NOT_INSERTED; + case StorageEvent::SD_ABNORMAL: + return SdCardStatus::NOT_INSERTED; + default: + return SdCardStatus::END; + } +} \ No newline at end of file diff --git a/application/MissionManager/src/AppMonitor.h b/application/MissionManager/src/AppMonitor.h new file mode 100644 index 00000000..adb16723 --- /dev/null +++ b/application/MissionManager/src/AppMonitor.h @@ -0,0 +1,45 @@ +/* + * 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 APP_MONITOR_H +#define APP_MONITOR_H +#include "IAppManager.h" +#include "IStorageManager.h" +class AppMonitor : public VAppMonitor +{ +public: + AppMonitor() = default; + virtual ~AppMonitor() = default; + StatusCode GetProductInfo(AppGetProductInfo ¶m) override; + StatusCode GetDeviceAttr(AppGetDeviceAttr ¶m) override; + StatusCode GetMediaInfo(AppGetMediaInfo ¶m) override; + StatusCode GetSdCardInfo(AppGetSdCardInfo ¶m) override; + StatusCode GetBatteryInfo(AppGetBatteryInfo ¶m) override; + StatusCode GetParamValue(AppParamValue ¶m) override; + StatusCode GetCapability(AppGetCapability ¶m) override; + StatusCode GetLockVideoStatus(LockVideoStatus ¶m) override; + StatusCode GetStorageInfo(std::vector ¶m) override; + StatusCode GetStorageFileList(const AppGetFileInfo &fileInfo, std::vector ¶m) override; + StatusCode SetDateTime(const AppSetDateTime ¶m) override; + StatusCode SetTimeZone(const unsigned int &zone) override; + StatusCode SetParamValue(const AppSetParamValue ¶m) override; + StatusCode EnterRecorder(void) override; + StatusCode AppPlayback(const PlayBackEvent &event) override; + StatusCode UploadFile(AppUploadFile ¶m) override; + StatusCode GetThumbnail(AppGetThumbnail ¶m) override; + +private: + SdCardStatus SdCardStatusConvert(const StorageEvent &event); +}; +#endif \ No newline at end of file diff --git a/application/MissionManager/src/DataProcessing.cpp b/application/MissionManager/src/DataProcessing.cpp new file mode 100644 index 00000000..138d3457 --- /dev/null +++ b/application/MissionManager/src/DataProcessing.cpp @@ -0,0 +1,75 @@ +/* + * 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 "DataProcessing.h" +#include "ILog.h" +const bool NOT_EXECUTED = false; +const bool EXECUTED = true; +key_event_data::key_event_data(const std::string &keyName, const KeyEvent &keyEvent, const unsigned int &holdTime) + : mKeyName(keyName), mKeyEvent(keyEvent), mHoldTime(holdTime) +{ +} +MissionData::MissionData(const std::shared_ptr &data) : mMissionData(data) +{ +} +MissionMessage::MissionMessage(const std::shared_ptr &message) : mMissionData(message) +{ +} +DataProcessing::DataProcessing() +{ + mEventHandle[InternalStateEvent::KEY_EVENT_HANDLE] = std::bind(&DataProcessing::KeyEventHandle, this, _1); +} +bool DataProcessing::EventHandle(VStateMachineData *msg) +{ + if (nullptr == msg) { + LogError("nullptr pointer.\n"); + return NOT_EXECUTED; + } + std::map::iterator iter; + std::shared_ptr message = std::dynamic_pointer_cast(msg->GetMessageObj()); + InternalStateEvent event = static_cast(message->mMissionData->mEvent); + iter = mEventHandle.find(event); + if (iter != mEventHandle.end()) { + return mEventHandle[event](msg); + } + return NOT_EXECUTED; +} +bool DataProcessing::KeyEventHandle(VStateMachineData *msg) +{ + if (nullptr == msg) { + LogError("nullptr pointer.\n"); + return NOT_EXECUTED; + } + std::map::iterator iter; + std::shared_ptr message = std::dynamic_pointer_cast(msg->GetMessageObj()); + std::shared_ptr> data = + std::dynamic_pointer_cast>(message->mMissionData); + if (!data) { + LogError("nullptr pointer.\n"); + return NOT_EXECUTED; + } + iter = mKeyClickHandle.find(data->mData.mKeyName); + if (iter != mKeyClickHandle.end() && KeyEvent::SHORT_CLICK == data->mData.mKeyEvent) { + return mKeyClickHandle[data->mData.mKeyName](data->mData); + } + iter = mKeyHoldDownHandle.find(data->mData.mKeyName); + if (iter != mKeyHoldDownHandle.end() && KeyEvent::HOLD_DOWN == data->mData.mKeyEvent) { + return mKeyHoldDownHandle[data->mData.mKeyName](data->mData); + } + iter = mKeyHoldUpHandle.find(data->mData.mKeyName); + if (iter != mKeyHoldUpHandle.end() && KeyEvent::HOLD_UP == data->mData.mKeyEvent) { + return mKeyHoldUpHandle[data->mData.mKeyName](data->mData); + } + return NOT_EXECUTED; +} \ No newline at end of file diff --git a/application/MissionManager/src/DataProcessing.h b/application/MissionManager/src/DataProcessing.h new file mode 100644 index 00000000..29c08048 --- /dev/null +++ b/application/MissionManager/src/DataProcessing.h @@ -0,0 +1,78 @@ +/* + * 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 DATA_PROCESSING_H +#define DATA_PROCESSING_H +#include "IMissionManager.h" +#include "IStateMachine.h" +#include "KeyControl.h" +#include +#include +using std::placeholders::_1; +using DataProcessingFunc = std::function; +enum class InternalStateEvent +{ + STORAGE_HANDLE_STATE_INIT = static_cast(MissionEvent::END), + SD_CARD_HANDLE_STATE_SD_STATUS_REPORTED, + ANY_STATE_SD_STATUS_PERORIED, + CHECK_UPGRADE_FILE, + MEDIA_REPORT_EVENT, + KEY_EVENT_HANDLE, + RESET_KEY_MEDIA_TASK, + MEDIA_HANDLE_STATE_TASK_TIME_OUT, + END +}; +typedef struct key_event_data +{ + key_event_data(const std::string &keyName, const KeyEvent &keyEvent, const unsigned int &holdTime); + const std::string mKeyName; + const KeyEvent mKeyEvent; + const unsigned int mHoldTime; +} KeyEventData; +using KeyHandleFunc = std::function; +class MissionData : public VStateMachineData +{ +public: + MissionData(const std::shared_ptr &data); + virtual ~MissionData() = default; + const std::shared_ptr mMissionData; +}; +/** + * @brief + * MissionMessage abstracts user data into state machine data, enabling the transfer of arbitrary data within the state + * machine. + */ +class MissionMessage : public VStateMessage +{ +public: + MissionMessage(const std::shared_ptr &message); + virtual ~MissionMessage() = default; + const std::shared_ptr mMissionData; // The message from users of state manager module. +}; +class DataProcessing +{ +public: + DataProcessing(); + virtual ~DataProcessing() = default; + bool EventHandle(VStateMachineData *msg); + virtual bool KeyEventHandle(VStateMachineData *msg); + +protected: + std::map mEventHandle; + std::map mKeyEventHandle; + std::map mKeyClickHandle; + std::map mKeyHoldDownHandle; + std::map mKeyHoldUpHandle; +}; +#endif \ No newline at end of file diff --git a/application/MissionManager/src/IMissionManager.cpp b/application/MissionManager/src/IMissionManager.cpp new file mode 100644 index 00000000..51af08f9 --- /dev/null +++ b/application/MissionManager/src/IMissionManager.cpp @@ -0,0 +1,38 @@ +/* + * 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 "IMissionManager.h" +#include "ILog.h" +std::shared_ptr &IMissionManager::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; +} +const StatusCode IMissionManager::Init(void) +{ + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} +const StatusCode IMissionManager::UnInit(void) +{ + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} \ No newline at end of file diff --git a/application/MissionManager/src/IdleState.cpp b/application/MissionManager/src/IdleState.cpp new file mode 100644 index 00000000..f0a2d6ee --- /dev/null +++ b/application/MissionManager/src/IdleState.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 "IdleState.h" +#include "IFilesManager.h" +#include "ILog.h" +#include "IMediaManager.h" +#include "MissionStateMachine.h" +IdleState::IdleState() : State("IdleState") +{ + // mEventHandle[InternalStateEvent::MEDIA_REPORT_EVENT] = std::bind(&IdleState::MediaReportHandle, this, _1); + // mEventHandle[InternalStateEvent::SD_CARD_HANDLE_STATE_SD_STATUS_REPORTED] = + // std::bind(&IdleState::SdCardEventHandle, this, _1); +} +void IdleState::GoInState() +{ + LogInfo(" ========== IdleState::GoInState.\n"); +} +void IdleState::GoOutState() +{ + LogInfo(" ========== IdleState::GoOutState.\n"); +} +bool IdleState::ExecuteStateMsg(VStateMachineData *msg) +{ + return DataProcessing::EventHandle(msg); +} \ No newline at end of file diff --git a/application/MissionManager/src/IdleState.h b/application/MissionManager/src/IdleState.h new file mode 100644 index 00000000..f680ba03 --- /dev/null +++ b/application/MissionManager/src/IdleState.h @@ -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. + */ +#ifndef IDLE_STATE_H +#define IDLE_STATE_H +#include "DataProcessing.h" +#include "IStateMachine.h" +class IdleState : public State, public DataProcessing, public std::enable_shared_from_this +{ +public: + IdleState(); + virtual ~IdleState() = default; + void GoInState() override; + void GoOutState() override; + bool ExecuteStateMsg(VStateMachineData *msg) override; + +private: +}; +#endif \ No newline at end of file diff --git a/application/MissionManager/src/LedsHandle.cpp b/application/MissionManager/src/LedsHandle.cpp new file mode 100644 index 00000000..57fab914 --- /dev/null +++ b/application/MissionManager/src/LedsHandle.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 "LedsHandle.h" +#include "ILog.h" +void LedsHandle::ControlDeviceStatusLed(const DeviceStatus &status, const long int &keepAliveTime, + const unsigned int &blinkPeriod) +{ + switch (status) { + case DeviceStatus::NORMAL: + mDeviceStatus = SetLedState::ControlLed("device_status", LedState::GREEN, keepAliveTime, blinkPeriod); + break; + + default: + LogWarning("unknow device status.\n"); + break; + } +} +void inline LedsHandle::DeleteDeviceStatusLed(void) +{ + mDeviceStatus->DeleteState(); +} +void LedsHandle::DeleteAllLeds(void) +{ + DeleteDeviceStatusLed(); +} \ No newline at end of file diff --git a/application/MissionManager/src/LedsHandle.h b/application/MissionManager/src/LedsHandle.h new file mode 100644 index 00000000..e394d5be --- /dev/null +++ b/application/MissionManager/src/LedsHandle.h @@ -0,0 +1,46 @@ +/* + * 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 LEDS_HANDLE_H +#define LEDS_HANDLE_H +#include "SetLedState.h" +enum class DeviceStatus +{ + NORMAL = 0, + TAKING_PICTURE_OR_VIDEO, + END +}; +class LedsHandle +{ +public: + LedsHandle() = default; + virtual ~LedsHandle() = default; + +protected: + /** + * @brief This function is designed as a virtual function so that when the board uses different LED circuits, this + * function can be overloaded to achieve polymorphic control of the light. + * @param status + * @param keepAliveTime + * @param blinkPeriod + */ + virtual void ControlDeviceStatusLed(const DeviceStatus &status, const long int &keepAliveTime = KEEP_ALIVE_FOREVER, + const unsigned int &blinkPeriod = LED_NOT_BLINK); + void DeleteDeviceStatusLed(void); + void DeleteAllLeds(void); + +private: + std::shared_ptr mDeviceStatus; +}; +#endif \ No newline at end of file diff --git a/application/MissionManager/src/McuMonitor.cpp b/application/MissionManager/src/McuMonitor.cpp new file mode 100644 index 00000000..49d7ec10 --- /dev/null +++ b/application/MissionManager/src/McuMonitor.cpp @@ -0,0 +1,69 @@ +/* + * 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 "McuMonitor.h" +#include "ILog.h" +#include "IMcuManager.h" +void McuMonitor::Init(std::shared_ptr &monitor) +{ + IMcuManager::GetInstance()->SetMcuMonitor(monitor); +} +void McuMonitor::UnInit(void) +{ +} +void McuMonitor::RecvIpcMissionEvent(std::shared_ptr &recv, const IpcMission &mission) +{ +} +void McuMonitor::RecvMcuHeartBeatEvent(std::shared_ptr &recv) +{ +} +void McuMonitor::RecvGetIntervalStartEvent(std::shared_ptr &recv) +{ + LogInfo("RecvGetIntervalStartEvent\n"); + std::shared_ptr> recvData = + std::dynamic_pointer_cast>(recv); + if (recvData) { + recvData->mDataRecvReply.mHour = 10; + recvData->mDataRecvReply.mMin = 10; + recvData->mDataRecvReply.mSecond = 10; + recv->ReplyFinished(true); + } + recv->ReplyFinished(false); +} +void McuMonitor::RecvGetDateTime(std::shared_ptr &recv) +{ + LogInfo("RecvGetDateTime\n"); + std::shared_ptr> recvData = std::dynamic_pointer_cast>(recv); + if (recvData) { + recvData->mDataRecvReply.mYear = 2024; + recvData->mDataRecvReply.mMon = 10; + recvData->mDataRecvReply.mDay = 10; + recvData->mDataRecvReply.mHour = 10; + recvData->mDataRecvReply.mMin = 10; + recvData->mDataRecvReply.mSecond = 10; + recv->ReplyFinished(true); + } + recv->ReplyFinished(false); +} +void McuMonitor::RecvGetPirSensitivity(std::shared_ptr &recv) +{ + LogInfo("RecvGetPirSensitivity\n"); + std::shared_ptr> recvData = + std::dynamic_pointer_cast>(recv); + if (recvData) { + recvData->mDataRecvReply.mSensitivity = 9; + recv->ReplyFinished(true); + } + recv->ReplyFinished(false); +} \ No newline at end of file diff --git a/application/MissionManager/src/McuMonitor.h b/application/MissionManager/src/McuMonitor.h new file mode 100644 index 00000000..d7abad62 --- /dev/null +++ b/application/MissionManager/src/McuMonitor.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_MONITOR_H +#define MCU_MONITOR_H +#include "IMcuManager.h" +class McuMonitor : public VMcuMonitor +{ +public: + McuMonitor() = default; + virtual ~McuMonitor() = default; + void Init(std::shared_ptr &monitor); + void UnInit(void); + void RecvIpcMissionEvent(std::shared_ptr &recv, const IpcMission &mission) override; + void RecvMcuHeartBeatEvent(std::shared_ptr &recv) override; + void RecvGetIntervalStartEvent(std::shared_ptr &recv) override; + void RecvGetDateTime(std::shared_ptr &recv) override; + void RecvGetPirSensitivity(std::shared_ptr &recv) override; +}; +#endif \ No newline at end of file diff --git a/application/MissionManager/src/MediaHandleState.cpp b/application/MissionManager/src/MediaHandleState.cpp new file mode 100644 index 00000000..f6b29f66 --- /dev/null +++ b/application/MissionManager/src/MediaHandleState.cpp @@ -0,0 +1,47 @@ +/* + * 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 "MediaHandleState.h" +#include "IFilesManager.h" +#include "ILog.h" +#include "IMediaManager.h" +#include "MissionStateMachine.h" +MediaHandleState::MediaHandleState() : State("MediaHandleState") +{ + mEventHandle[InternalStateEvent::RESET_KEY_MEDIA_TASK] = + std::bind(&MediaHandleState::ResetKeyMediaTaskHandle, this, _1); +} +void MediaHandleState::GoInState() +{ + LogInfo(" ========== MediaHandleState::GoInState.\n"); + if (!mMediaHandle) { + MediaTaskHandle::Init(); + } +} +void MediaHandleState::GoOutState() +{ + LogInfo(" ========== MediaHandleState::GoOutState.\n"); +} +bool MediaHandleState::ExecuteStateMsg(VStateMachineData *msg) +{ + return DataProcessing::EventHandle(msg); +} +void MediaHandleState::TaskResponse(const std::vector &response) +{ +} +bool MediaHandleState::ResetKeyMediaTaskHandle(VStateMachineData *msg) +{ + MakeSingleTask(InternalStateEvent::RESET_KEY_MEDIA_TASK, shared_from_this()); + return EXECUTED; +} \ No newline at end of file diff --git a/application/MissionManager/src/MediaHandleState.h b/application/MissionManager/src/MediaHandleState.h new file mode 100644 index 00000000..4a1e594c --- /dev/null +++ b/application/MissionManager/src/MediaHandleState.h @@ -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. + */ +#ifndef MEDIA_HANDLE_STATE_H +#define MEDIA_HANDLE_STATE_H +#include "DataProcessing.h" +#include "IStateMachine.h" +#include "MediaTaskHandle.h" +class MediaHandleState : public State, + public DataProcessing, + public MediaTaskHandle, + public VMediaTaskIniator, + public std::enable_shared_from_this +{ +public: + MediaHandleState(); + virtual ~MediaHandleState() = default; + void GoInState() override; + void GoOutState() override; + bool ExecuteStateMsg(VStateMachineData *msg) override; + +private: + void TaskResponse(const std::vector &response) override; + bool ResetKeyMediaTaskHandle(VStateMachineData *msg); +}; +#endif \ No newline at end of file diff --git a/application/MissionManager/src/MediaTask.cpp b/application/MissionManager/src/MediaTask.cpp new file mode 100644 index 00000000..20d0c6c2 --- /dev/null +++ b/application/MissionManager/src/MediaTask.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 "MediaTask.h" +MediaTask::MediaTask(const MediaTaskType &type, const InternalStateEvent &bindEvent, + const std::weak_ptr &iniator) + : mType(type), mBindEvent(bindEvent), mIniator(iniator) +{ + mResponseData.reset(); +} +unsigned int MediaTask::GetTaskTimeOutMs(void) +{ + return MEDIA_TASK_TIMEOUT_MS; +} \ No newline at end of file diff --git a/application/MissionManager/src/MediaTask.h b/application/MissionManager/src/MediaTask.h new file mode 100644 index 00000000..5eb77ae7 --- /dev/null +++ b/application/MissionManager/src/MediaTask.h @@ -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. + */ +#ifndef MEDIA_TASK_H +#define MEDIA_TASK_H +#include "DataProcessing.h" +#include "IMediaManager.h" +constexpr unsigned int MEDIA_TASK_TIMEOUT_MS = 1000 * 60; +class VMediaTaskIniator +{ +public: + VMediaTaskIniator() = default; + virtual ~VMediaTaskIniator() = default; + virtual void TaskResponse(const std::vector &response) = 0; +}; +class MediaTask : public VMediaTask +{ +public: + MediaTask(const MediaTaskType &type, const InternalStateEvent &bindEvent, + const std::weak_ptr &iniator); + virtual ~MediaTask() = default; + virtual unsigned int GetTaskTimeOutMs(void); + +private: + const MediaTaskType mType; + const InternalStateEvent mBindEvent; + const std::weak_ptr mIniator; + bool mFinished = false; + std::shared_ptr mResponseData; +}; +#endif \ No newline at end of file diff --git a/application/MissionManager/src/MediaTaskHandle.cpp b/application/MissionManager/src/MediaTaskHandle.cpp new file mode 100644 index 00000000..37661004 --- /dev/null +++ b/application/MissionManager/src/MediaTaskHandle.cpp @@ -0,0 +1,51 @@ +/* + * 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 "MediaTaskHandle.h" +#include "ILog.h" +#include "MissionStateMachine.h" +MediaTaskHandle::MediaTaskHandle() +{ + mMediaHandle.reset(); + // mMediaHandle = std::make_shared(); +} +void MediaTaskHandle::Init(void) +{ + IMediaManager::GetInstance()->GetMediaChannel(MediaChannel::MEDIA_1, mMediaHandle); +} +void MediaTaskHandle::UnInit(void) +{ + mMediaHandle->ClearTask(); + mMediaHandle.reset(); +} +void MediaTaskHandle::MakeSingleTask(const InternalStateEvent &bindEvent, + const std::shared_ptr &iniator) +{ + std::shared_ptr task = std::make_shared(MediaTaskType::END, bindEvent, iniator); + if (!mMediaHandle) { + LogError("MediaHandle is null"); + return; + } + auto code = mMediaHandle->ExecuteTask(task); + if (IsCodeOK(code)) { + mRuningTask = task; + long long timeOut = std::dynamic_pointer_cast(task)->GetTaskTimeOutMs(); + std::shared_ptr message = std::make_shared( + static_cast(InternalStateEvent::MEDIA_HANDLE_STATE_TASK_TIME_OUT)); + MissionStateMachine::GetInstance()->MessageExecutedLater(message, timeOut); + } + // else if () { + // mMediaHandle->StopTask(); + // } +} \ No newline at end of file diff --git a/application/MissionManager/src/MediaTaskHandle.h b/application/MissionManager/src/MediaTaskHandle.h new file mode 100644 index 00000000..40170499 --- /dev/null +++ b/application/MissionManager/src/MediaTaskHandle.h @@ -0,0 +1,35 @@ +/* + * 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 MEDIA_TASK_HANDLE_H +#define MEDIA_TASK_HANDLE_H +#include "DataProcessing.h" +#include "IMediaManager.h" +#include "MediaTask.h" +class MediaTaskHandle +{ +public: + MediaTaskHandle(); + virtual ~MediaTaskHandle() = default; + void Init(void); + void UnInit(void); + void MakeSingleTask(const InternalStateEvent &bindEvent, const std::shared_ptr &iniator); + +protected: + std::shared_ptr mMediaHandle; + +private: + std::shared_ptr mRuningTask; +}; +#endif \ No newline at end of file diff --git a/application/MissionManager/src/MissionManager.cpp b/application/MissionManager/src/MissionManager.cpp new file mode 100644 index 00000000..f4a786a3 --- /dev/null +++ b/application/MissionManager/src/MissionManager.cpp @@ -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. + */ +#include "MissionManager.h" +#include "IAppManager.h" +#include "MissionStateMachine.h" +const StatusCode MissionManager::Init(void) +{ + MissionStateMachine::GetInstance()->Init(); + return CreateStatusCode(STATUS_CODE_OK); +} +const StatusCode MissionManager::UnInit(void) +{ + MissionStateMachine::GetInstance()->UnInit(); + IAppManager::GetInstance()->UnInit(); + return CreateStatusCode(STATUS_CODE_OK); +} \ No newline at end of file diff --git a/application/MissionManager/src/MissionManager.h b/application/MissionManager/src/MissionManager.h new file mode 100644 index 00000000..a1e6af4a --- /dev/null +++ b/application/MissionManager/src/MissionManager.h @@ -0,0 +1,26 @@ +/* + * 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 MISSION_MANAGER_H +#define MISSION_MANAGER_H +#include "IMissionManager.h" +class MissionManager : public IMissionManager +{ +public: + MissionManager() = default; + virtual ~MissionManager() = default; + const StatusCode Init(void) override; + const StatusCode UnInit(void) override; +}; +#endif \ No newline at end of file diff --git a/application/MissionManager/src/MissionManagerMakePtr.cpp b/application/MissionManager/src/MissionManagerMakePtr.cpp new file mode 100644 index 00000000..f478ecd5 --- /dev/null +++ b/application/MissionManager/src/MissionManagerMakePtr.cpp @@ -0,0 +1,118 @@ +/* + * 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 "MissionManagerMakePtr.h" +#include "ILog.h" +#include "IdleState.h" +#include "MediaHandleState.h" +#include "MissionManager.h" +#include "OnMissionState.h" +#include "PirTriggeredMissionState.h" +#include "SdCardHandleState.h" +#include "StorageHandleState.h" +#include "TestMissionState.h" +#include "TopState.h" +#include "UpgradeState.h" +bool CreateMissionManagerModule(void) +{ + auto instance = std::make_shared(); + StatusCode code = MissionManagerMakePtr::GetInstance()->CreateMissionManagerInstance(instance); + if (IsCodeOK(code)) { + LogInfo("CreateMcuManager is ok.\n"); + IMissionManager::GetInstance(&instance); + return true; + } + return false; +} +bool DestroyMissionManagerModule(void) +{ + auto instance = std::make_shared(); + IMissionManager::GetInstance(&instance); + return true; +} +std::shared_ptr &MissionManagerMakePtr::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; +} +const StatusCode MissionManagerMakePtr::CreateMissionManagerInstance(std::shared_ptr &instance) +{ + instance = std::make_shared(); + return CreateStatusCode(STATUS_CODE_OK); +} +std::shared_ptr MissionManagerMakePtr::CreateTopState(void) +{ + std::shared_ptr state = std::make_shared(); + return state; +} +std::shared_ptr MissionManagerMakePtr::CreateMissionState(const IpcMission &mission) +{ + LogInfo("MissionManagerMakePtr::CreateMissionState\n"); + std::shared_ptr state; + switch (mission) { + case IpcMission::PIR_TRIGGERED: + LogInfo("Create PirTriggeredMissionState.\n"); + state = std::make_shared(); + break; + case IpcMission::TEST: + LogInfo("Create TestMissionState.\n"); + state = std::make_shared(); + break; + case IpcMission::ON: + LogInfo("Create OnMissionState.\n"); + state = std::make_shared(); + break; + + default: + LogWarning("Unknown mission.\n"); + state = std::make_shared(); + break; + } + return state; +} +std::shared_ptr MissionManagerMakePtr::CreateStorageHandleState(void) +{ + std::shared_ptr state = std::make_shared(); + // std::dynamic_pointer_cast(state)->Init(); + return state; +} +std::shared_ptr MissionManagerMakePtr::CreateSdCardHandleState(void) +{ + std::shared_ptr state = std::make_shared(); + return state; +} +std::shared_ptr MissionManagerMakePtr::CreateUpgradeState(void) +{ + std::shared_ptr state = std::make_shared(); + return state; +} +std::shared_ptr MissionManagerMakePtr::CreateMediaHandleState(void) +{ + std::shared_ptr state = std::make_shared(); + return state; +} +std::shared_ptr MissionManagerMakePtr::CreateIdleState(void) +{ + std::shared_ptr state = std::make_shared(); + return state; +} \ No newline at end of file diff --git a/application/MissionManager/src/MissionManagerMakePtr.h b/application/MissionManager/src/MissionManagerMakePtr.h new file mode 100644 index 00000000..6bc2ded6 --- /dev/null +++ b/application/MissionManager/src/MissionManagerMakePtr.h @@ -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. + */ +#ifndef MISSION_MANAGER_MAKE_PTR_H +#define MISSION_MANAGER_MAKE_PTR_H +#include "IMcuManager.h" +#include "IMissionManager.h" +#include "IStateMachine.h" +#include "StatusCode.h" +#include +class MissionManagerMakePtr +{ +public: + MissionManagerMakePtr() = default; + virtual ~MissionManagerMakePtr() = default; + static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); + virtual const StatusCode CreateMissionManagerInstance(std::shared_ptr &instance); + virtual std::shared_ptr CreateTopState(void); + virtual std::shared_ptr CreateMissionState(const IpcMission &mission); + virtual std::shared_ptr CreateStorageHandleState(void); + virtual std::shared_ptr CreateSdCardHandleState(void); + virtual std::shared_ptr CreateUpgradeState(void); + virtual std::shared_ptr CreateMediaHandleState(void); + virtual std::shared_ptr CreateIdleState(void); +}; +#endif \ No newline at end of file diff --git a/application/MissionManager/src/MissionState.cpp b/application/MissionManager/src/MissionState.cpp new file mode 100644 index 00000000..04b3608e --- /dev/null +++ b/application/MissionManager/src/MissionState.cpp @@ -0,0 +1,56 @@ +/* + * 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 "MissionState.h" +#include "IAppManager.h" +#include "ILog.h" +#include "MissionStateMachine.h" +MissionState::MissionState(const std::string &name) : State(name) +{ + mEventHandle[InternalStateEvent::MEDIA_REPORT_EVENT] = std::bind(&MissionState::MediaReportHandle, this, _1); + mEventHandle[InternalStateEvent::SD_CARD_HANDLE_STATE_SD_STATUS_REPORTED] = + std::bind(&MissionState::SdCardEventReportHandle, this, _1); + mEventHandle[InternalStateEvent::CHECK_UPGRADE_FILE] = std::bind(&MissionState::CheckUpgradeFileHandle, this, _1); +} +void MissionState::GoInState() +{ + LogInfo(" ========== MissionState::GoInState.\n"); +} +void MissionState::GoOutState() +{ + LogInfo(" ========== MissionState::GoOutState.\n"); + LedsHandle::DeleteAllLeds(); +} +bool MissionState::ExecuteStateMsg(VStateMachineData *msg) +{ + return DataProcessing::EventHandle(msg); +} +bool MissionState::MediaReportHandle(VStateMachineData *msg) +{ + MissionStateMachine::GetInstance()->DelayMessage(msg); + MissionStateMachine::GetInstance()->SwitchState(SystemState::STORAGE_HANDLE_STATE); + return EXECUTED; +} +bool MissionState::SdCardEventReportHandle(VStateMachineData *msg) +{ + MissionStateMachine::GetInstance()->DelayMessage(msg); + MissionStateMachine::GetInstance()->SwitchState(SystemState::SD_CARD_HANDLE_STATE); + return EXECUTED; +} +bool MissionState::CheckUpgradeFileHandle(VStateMachineData *msg) +{ + MissionStateMachine::GetInstance()->DelayMessage(msg); + MissionStateMachine::GetInstance()->SwitchState(SystemState::UPGRADE_STATE); + return EXECUTED; +} \ No newline at end of file diff --git a/application/MissionManager/src/MissionState.h b/application/MissionManager/src/MissionState.h new file mode 100644 index 00000000..2f5fcca2 --- /dev/null +++ b/application/MissionManager/src/MissionState.h @@ -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. + */ +#ifndef MISSION_STATE_H +#define MISSION_STATE_H +#include "DataProcessing.h" +#include "IStateMachine.h" +#include "LedsHandle.h" +class MissionState : public State, + public DataProcessing, + public LedsHandle, + public std::enable_shared_from_this +{ +public: + MissionState(const std::string &name); + virtual ~MissionState() = default; + void GoInState() override; + void GoOutState() override; + bool ExecuteStateMsg(VStateMachineData *msg) override; + +protected: + bool SdCardEventReportHandle(VStateMachineData *msg); + +private: + bool MediaReportHandle(VStateMachineData *msg); + bool CheckUpgradeFileHandle(VStateMachineData *msg); +}; +#endif \ No newline at end of file diff --git a/application/MissionManager/src/MissionStateMachine.cpp b/application/MissionManager/src/MissionStateMachine.cpp new file mode 100644 index 00000000..d62fc283 --- /dev/null +++ b/application/MissionManager/src/MissionStateMachine.cpp @@ -0,0 +1,124 @@ +/* + * 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 "MissionStateMachine.h" +#include "DataProcessing.h" +#include "ILog.h" +#include "McuAskBase.h" +#include "MissionManagerMakePtr.h" +// #include "TopState.h" +std::shared_ptr &MissionStateMachine::GetInstance(std::shared_ptr *impl) +{ + static auto instance = std::make_shared(); + if (impl) { + instance = *impl; + } + return instance; +} +MissionStateMachine::MissionStateMachine() +{ + mStartMission = IpcMission::END; +} +void MissionStateMachine::Init(void) +{ + mStateMachine = std::make_shared(); + auto code = IStateMachine::GetInstance()->CreateStateMachine(mStateMachine); + if (!IsCodeOK(code)) { + LogError("Create state machine failed[%s].\n", code.mPrintStringCode(code)); + return; + } + if (!mStateMachine->InitialStateMachine()) { + LogError("State machine init failed.\n"); + return; + } + mStartMission = GetStartMission(); + if (mStartMission == IpcMission::END) { + LogError("ipcmission error, will start test mode.\n"); + mStartMission = IpcMission::TEST; + } + RunStateMachine(mStartMission); +} +void MissionStateMachine::UnInit(void) +{ + mStateMachine->StopHandlerThread(); + mStateTree.clear(); +} +StatusCode MissionStateMachine::SendStateMessage(const std::shared_ptr &message) +{ + std::shared_ptr msg = std::make_shared(message); + mStateMachine->SendMessage(static_cast(message->mEvent), msg); + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode MissionStateMachine::MessageExecutedLater(const std::shared_ptr &message, + long long &delayTimeMs) +{ + std::shared_ptr msg = std::make_shared(message); + mStateMachine->MessageExecutedLater(static_cast(message->mEvent), msg, delayTimeMs); + return CreateStatusCode(STATUS_CODE_OK); +} +void MissionStateMachine::DelayMessage(VStateMachineData *msg) +{ + mStateMachine->DelayMessage(msg); +} +void MissionStateMachine::SwitchState(const SystemState &state) +{ + mStateMachine->SwitchState(mStateTree[state].get()); +} +IpcMission MissionStateMachine::GetStartMission(void) +{ + class McuAskIpcMission : public McuAsk, public McuAskBase + { + public: + McuAskIpcMission() : McuAskBase(McuAskBlock::BLOCK, McuAskReply::NEED_REPLY) + { + mDataReply = IpcMission::END; + } + virtual ~McuAskIpcMission() = default; + }; + std::shared_ptr ask = std::make_shared(); + IMcuManager::GetInstance()->GetIpcMission(ask); + return std::dynamic_pointer_cast(ask)->mDataReply; +} +void MissionStateMachine::RunStateMachine(const IpcMission &mission) +{ + LogInfo("Make all states and start the state machine, ipc mission = %s.\n", + IMcuManager::GetInstance()->PrintIpcMissionString(mission)); + mStateTree[SystemState::TOP_STATE] = MissionManagerMakePtr::GetInstance()->CreateTopState(); + mStateTree[SystemState::MISSION_STATE] = MissionManagerMakePtr::GetInstance()->CreateMissionState(mission); + mStateTree[SystemState::STORAGE_HANDLE_STATE] = MissionManagerMakePtr::GetInstance()->CreateStorageHandleState(); + mStateTree[SystemState::SD_CARD_HANDLE_STATE] = MissionManagerMakePtr::GetInstance()->CreateSdCardHandleState(); + mStateTree[SystemState::UPGRADE_STATE] = MissionManagerMakePtr::GetInstance()->CreateUpgradeState(); + mStateTree[SystemState::MEDIA_HANDLE_STATE] = MissionManagerMakePtr::GetInstance()->CreateMediaHandleState(); + mStateTree[SystemState::IDLE_STATE] = MissionManagerMakePtr::GetInstance()->CreateIdleState(); + mStateMachine->StatePlus(mStateTree[SystemState::TOP_STATE].get(), nullptr); + mStateMachine->StatePlus(mStateTree[SystemState::MISSION_STATE].get(), mStateTree[SystemState::TOP_STATE].get()); + mStateMachine->StatePlus(mStateTree[SystemState::STORAGE_HANDLE_STATE].get(), + mStateTree[SystemState::MISSION_STATE].get()); + mStateMachine->StatePlus(mStateTree[SystemState::SD_CARD_HANDLE_STATE].get(), + mStateTree[SystemState::STORAGE_HANDLE_STATE].get()); + mStateMachine->StatePlus(mStateTree[SystemState::UPGRADE_STATE].get(), + mStateTree[SystemState::MISSION_STATE].get()); + mStateMachine->StatePlus(mStateTree[SystemState::MEDIA_HANDLE_STATE].get(), + mStateTree[SystemState::MISSION_STATE].get()); + mStateMachine->StatePlus(mStateTree[SystemState::IDLE_STATE].get(), mStateTree[SystemState::MISSION_STATE].get()); + mStateMachine->SetCurrentState(mStateTree[SystemState::TOP_STATE].get()); + mStateMachine->StartStateMachine(); + /** + * @brief The business can only be processed after the state machine is started. + * + */ + std::shared_ptr message = + std::make_shared(static_cast(InternalStateEvent::STORAGE_HANDLE_STATE_INIT)); + SendStateMessage(message); +} diff --git a/application/MissionManager/src/MissionStateMachine.h b/application/MissionManager/src/MissionStateMachine.h new file mode 100644 index 00000000..dc039bd1 --- /dev/null +++ b/application/MissionManager/src/MissionStateMachine.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 MISSION_STATE_MACHINE_H +#define MISSION_STATE_MACHINE_H +// #include "IDeviceManager.h" +#include "IMcuManager.h" +#include "IMissionManager.h" +#include "IStateMachine.h" +#include "StatusCode.h" +#include +const bool NOT_EXECUTED = false; +const bool EXECUTED = true; +enum class SystemState +{ + TOP_STATE = 0, + MISSION_STATE, + STORAGE_HANDLE_STATE, + SD_CARD_HANDLE_STATE, + UPGRADE_STATE, + MEDIA_HANDLE_STATE, + IDLE_STATE, + END +}; +class MissionStateMachine +{ +public: + MissionStateMachine(); + virtual ~MissionStateMachine() = default; + static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); + void Init(void); + void UnInit(void); + StatusCode SendStateMessage(const std::shared_ptr &message); + StatusCode MessageExecutedLater(const std::shared_ptr &message, long long &delayTimeMs); + void DelayMessage(VStateMachineData *msg); + void SwitchState(const SystemState &state); + +private: + IpcMission GetStartMission(void); + void RunStateMachine(const IpcMission &mission); + +private: + std::shared_ptr mStateMachine; + std::map> mStateTree; + IpcMission mStartMission; +}; +#endif \ No newline at end of file diff --git a/application/MissionManager/src/OnMissionState.cpp b/application/MissionManager/src/OnMissionState.cpp new file mode 100644 index 00000000..f84c44ee --- /dev/null +++ b/application/MissionManager/src/OnMissionState.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 "OnMissionState.h" +#include "ILog.h" +#include "IStorageManager.h" +#include "MissionStateMachine.h" +OnMissionState::OnMissionState() : MissionState("OnMissionState") +{ + // mEventHandle[InternalStateEvent::ANY_STATE_SD_STATUS_PERORIED] = + // std::bind(&OnMissionState::SdCardEventReportSendToApp, this, _1); +} +void OnMissionState::GoInState() +{ + MissionState::GoInState(); + LogInfo(" ========== OnMissionState::GoInState.\n"); +} +void OnMissionState::GoOutState() +{ + MissionState::GoOutState(); + LogInfo(" ========== OnMissionState::GoOutState.\n"); +} +bool OnMissionState::SdCardEventReportSendToApp(VStateMachineData *msg) +{ + return EXECUTED; +} \ No newline at end of file diff --git a/application/MissionManager/src/OnMissionState.h b/application/MissionManager/src/OnMissionState.h new file mode 100644 index 00000000..9b8e617e --- /dev/null +++ b/application/MissionManager/src/OnMissionState.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 ON_MISSION_STATE_H +#define ON_MISSION_STATE_H +#include "IStateMachine.h" +#include "IStorageManager.h" +#include "MissionState.h" +class OnMissionState : public MissionState +{ +public: + OnMissionState(); + virtual ~OnMissionState() = default; + void GoInState() override; + void GoOutState() override; + +private: + bool SdCardEventReportSendToApp(VStateMachineData *msg); +}; +#endif \ No newline at end of file diff --git a/application/MissionManager/src/PirTrggeredMissionState.cpp b/application/MissionManager/src/PirTrggeredMissionState.cpp new file mode 100644 index 00000000..7f2c287a --- /dev/null +++ b/application/MissionManager/src/PirTrggeredMissionState.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 "PirTriggeredMissionState.h" +#include "ILog.h" +#include "IStorageManager.h" +#include "MissionStateMachine.h" +PirTriggeredMissionState::PirTriggeredMissionState() : MissionState("PirTriggeredMissionState") +{ + // mEventHandle[InternalStateEvent::ANY_STATE_SD_STATUS_PERORIED] = + // std::bind(&PirTriggeredMissionState::SdCardEventReportSendToApp, this, _1); +} +void PirTriggeredMissionState::GoInState() +{ + MissionState::GoInState(); + LogInfo(" ========== PirTriggeredMissionState::GoInState.\n"); +} +void PirTriggeredMissionState::GoOutState() +{ + MissionState::GoOutState(); + LogInfo(" ========== PirTriggeredMissionState::GoOutState.\n"); +} +bool PirTriggeredMissionState::SdCardEventReportSendToApp(VStateMachineData *msg) +{ + return EXECUTED; +} \ No newline at end of file diff --git a/application/MissionManager/src/PirTriggeredMissionState.h b/application/MissionManager/src/PirTriggeredMissionState.h new file mode 100644 index 00000000..6734768c --- /dev/null +++ b/application/MissionManager/src/PirTriggeredMissionState.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 PIR_TRIGGERED_MISSION_STATE_H +#define PIR_TRIGGERED_MISSION_STATE_H +#include "IStateMachine.h" +#include "IStorageManager.h" +#include "MissionState.h" +class PirTriggeredMissionState : public MissionState +{ +public: + PirTriggeredMissionState(); + virtual ~PirTriggeredMissionState() = default; + void GoInState() override; + void GoOutState() override; + +private: + bool SdCardEventReportSendToApp(VStateMachineData *msg); +}; +#endif \ No newline at end of file diff --git a/application/MissionManager/src/SdCardHandleState.cpp b/application/MissionManager/src/SdCardHandleState.cpp new file mode 100644 index 00000000..fef26ffb --- /dev/null +++ b/application/MissionManager/src/SdCardHandleState.cpp @@ -0,0 +1,86 @@ +/* + * 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 "SdCardHandleState.h" +#include "IFilesManager.h" +#include "ILog.h" +#include "IMediaManager.h" +#include "MissionStateMachine.h" +SdCardHandleState::SdCardHandleState() : State("SdCardHandleState"), mSdCardStatus(StorageEvent::END) +{ + mEventHandle[InternalStateEvent::MEDIA_REPORT_EVENT] = std::bind(&SdCardHandleState::MediaReportHandle, this, _1); + mEventHandle[InternalStateEvent::SD_CARD_HANDLE_STATE_SD_STATUS_REPORTED] = + std::bind(&SdCardHandleState::SdCardEventHandle, this, _1); +} +void SdCardHandleState::GoInState() +{ + LogInfo(" ========== SdCardHandleState::GoInState.\n"); +} +void SdCardHandleState::GoOutState() +{ + LogInfo(" ========== SdCardHandleState::GoOutState.\n"); +} +bool SdCardHandleState::ExecuteStateMsg(VStateMachineData *msg) +{ + return DataProcessing::EventHandle(msg); +} +bool SdCardHandleState::MediaReportHandle(VStateMachineData *msg) +{ + LogInfo(" MediaReportHandle.\n"); + std::shared_ptr message = std::dynamic_pointer_cast(msg->GetMessageObj()); + std::shared_ptr> data = + std::dynamic_pointer_cast>(message->mMissionData); + if (!data) { + LogError("nullptr pointer.\n"); + return NOT_EXECUTED; + } + if (StorageEvent::SD_CARD_INSERT != mSdCardStatus) { + /** + * @brief The SD card has not been mounted yet, cache the data for the quick start first. + * + */ + LogWarning("Sd card is not inserted, cache data.\n"); + mFileNeedToSave = std::make_shared(data->mData.mFileName); + return EXECUTED; + } + LogInfo("file = %s.\n", data->mData.mFileName.c_str()); + SaveFileInfo saveFileInfo(data->mData.mFileName); + StatusCode code = IFilesManager::GetInstance()->SaveFile(saveFileInfo); + if (IsCodeOK(code) == false) { + LogError("SaveFile failed.\n"); + } + return EXECUTED; +} +bool SdCardHandleState::SdCardEventHandle(VStateMachineData *msg) +{ + std::shared_ptr message = std::dynamic_pointer_cast(msg->GetMessageObj()); + std::shared_ptr> data = + std::dynamic_pointer_cast>(message->mMissionData); + LogInfo(" SdCardEventHandle event:%s.\n", IStorageManager::GetInstance()->PrintStringStorageEvent(data->mData)); + if (mFileNeedToSave && StorageEvent::SD_CARD_INSERT == data->mData) { + LogInfo("Sd card is inserted for the first time.\n"); + StatusCode code = IFilesManager::GetInstance()->SaveFile(*(mFileNeedToSave.get())); + if (IsCodeOK(code) == false) { + LogError("SaveFile failed.\n"); + } + mFileNeedToSave.reset(); + } + mSdCardStatus = data->mData; + if (StorageEvent::SD_CARD_INSERT == mSdCardStatus) { + std::shared_ptr message = + std::make_shared(static_cast(InternalStateEvent::CHECK_UPGRADE_FILE)); + MissionStateMachine::GetInstance()->SendStateMessage(message); + } + return EXECUTED; +} \ No newline at end of file diff --git a/application/MissionManager/src/SdCardHandleState.h b/application/MissionManager/src/SdCardHandleState.h new file mode 100644 index 00000000..c8eb7efb --- /dev/null +++ b/application/MissionManager/src/SdCardHandleState.h @@ -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. + */ +#ifndef SD_CARD_HANDLE_STATE_H +#define SD_CARD_HANDLE_STATE_H +#include "DataProcessing.h" +#include "IFilesManager.h" +#include "IMediaManager.h" +#include "IStateMachine.h" +#include "IStorageManager.h" +class SdCardHandleState : public State, public DataProcessing, public std::enable_shared_from_this +{ +public: + SdCardHandleState(); + virtual ~SdCardHandleState() = default; + void GoInState() override; + void GoOutState() override; + bool ExecuteStateMsg(VStateMachineData *msg) override; + +protected: + bool MediaReportHandle(VStateMachineData *msg); + bool SdCardEventHandle(VStateMachineData *msg); + +private: + StorageEvent mSdCardStatus; + std::shared_ptr mFileNeedToSave; +}; +#endif \ No newline at end of file diff --git a/application/MissionManager/src/SetLedState.cpp b/application/MissionManager/src/SetLedState.cpp new file mode 100644 index 00000000..d0f9b81a --- /dev/null +++ b/application/MissionManager/src/SetLedState.cpp @@ -0,0 +1,44 @@ +/* + * 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 "SetLedState.h" +SetLedState::SetLedState(const LedState &state, const unsigned int &keepAliveTime, const unsigned int &blinkPeriod) + : mState(state), mKeepAliveTime(keepAliveTime), mBlinkPeriod(blinkPeriod) +{ +} +StatusCode SetLedState::GetLedState(LedState &state) +{ + state = mState; + return CreateStatusCode(STATUS_CODE_OK); +} +unsigned int SetLedState::GetKeepAliveTimeMs(void) +{ + return mKeepAliveTime; +} +unsigned int SetLedState::GetBlinkTimeMs(void) +{ + return mBlinkPeriod; +} +void SetLedState::DeleteState(void) +{ + mKeepAliveTime = DELETED_LED_STATE; +} +std::shared_ptr SetLedState::ControlLed(const std::string &ledName, const LedState &state, + const long int &keepAliveTime, const unsigned int &blinkPeriod) +{ + std::shared_ptr ledState = std::make_shared(state, keepAliveTime, blinkPeriod); + std::shared_ptr ledState2 = ledState; + IDeviceManager::GetInstance()->ControlLed(ledName, ledState2); + return ledState; +} \ No newline at end of file diff --git a/application/MissionManager/src/SetLedState.h b/application/MissionManager/src/SetLedState.h new file mode 100644 index 00000000..e18ba582 --- /dev/null +++ b/application/MissionManager/src/SetLedState.h @@ -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. + */ +#ifndef SET_LED_STATE_H +#define SET_LED_STATE_H +#include "IDeviceManager.h" +#include "LedControl.h" +constexpr int LED_BLINKING_FAST_PERIOD = 500; +constexpr int LED_BLINKING_SLOW_PERIOD = 1000; +class SetLedState : public LedControlContext, public VSingleControl +{ +public: + SetLedState(const LedState &state, const unsigned int &keepAliveTime = KEEP_ALIVE_FOREVER, + const unsigned int &blinkPeriod = LED_NOT_BLINK); + virtual ~SetLedState() = default; + StatusCode GetLedState(LedState &state) override; + unsigned int GetKeepAliveTimeMs(void) override; + unsigned int GetBlinkTimeMs(void) override; + void DeleteState(void); + +public: + static std::shared_ptr ControlLed(const std::string &ledName, const LedState &state, + const long int &keepAliveTime = KEEP_ALIVE_FOREVER, + const unsigned int &blinkPeriod = LED_NOT_BLINK); + +private: + const LedState mState; + unsigned int mKeepAliveTime; + const unsigned int mBlinkPeriod; +}; +#endif \ No newline at end of file diff --git a/application/MissionManager/src/StorageHandleState.cpp b/application/MissionManager/src/StorageHandleState.cpp new file mode 100644 index 00000000..86b80a9a --- /dev/null +++ b/application/MissionManager/src/StorageHandleState.cpp @@ -0,0 +1,59 @@ +/* + * 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 "StorageHandleState.h" +#include "ILog.h" +#include "IMediaManager.h" +#include "MissionStateMachine.h" +StorageHandleState::StorageHandleState() : State("StorageHandleState") +{ + mEventHandle[InternalStateEvent::STORAGE_HANDLE_STATE_INIT] = + std::bind(&StorageHandleState::StorageStartInitHandle, this, _1); +} +void StorageHandleState::GoInState() +{ + LogInfo(" ========== StorageHandleState::GoInState.\n"); + MissionStateMachine::GetInstance()->SwitchState(SystemState::SD_CARD_HANDLE_STATE); +} +void StorageHandleState::GoOutState() +{ + LogInfo(" ========== StorageHandleState::GoOutState.\n"); +} +bool StorageHandleState::ExecuteStateMsg(VStateMachineData *msg) +{ + return DataProcessing::EventHandle(msg); +} +void StorageHandleState::Init(void) +{ + std::shared_ptr monitor = shared_from_this(); + IStorageManager::GetInstance()->SetMonitor(monitor); +} +void StorageHandleState::UnInit(void) +{ +} +void StorageHandleState::ReportEvent(const StorageEvent &event) +{ + LogInfo("StorageHandleState::ReportEvent.\n"); + std::shared_ptr message = std::make_shared>( + static_cast(InternalStateEvent::SD_CARD_HANDLE_STATE_SD_STATUS_REPORTED), event); + MissionStateMachine::GetInstance()->SendStateMessage(message); + std::shared_ptr message2 = std::make_shared>( + static_cast(InternalStateEvent::ANY_STATE_SD_STATUS_PERORIED), event); + MissionStateMachine::GetInstance()->SendStateMessage(message2); +} +bool StorageHandleState::StorageStartInitHandle(VStateMachineData *msg) +{ + Init(); + return EXECUTED; +} \ No newline at end of file diff --git a/application/MissionManager/src/StorageHandleState.h b/application/MissionManager/src/StorageHandleState.h new file mode 100644 index 00000000..0c98175e --- /dev/null +++ b/application/MissionManager/src/StorageHandleState.h @@ -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. + */ +#ifndef STORAGE_HANDLE_STATE_H +#define STORAGE_HANDLE_STATE_H +#include "DataProcessing.h" +#include "IMediaManager.h" +#include "IStateMachine.h" +#include "IStorageManager.h" +class StorageHandleState : public State, + public DataProcessing, + public VStorageMoniter, + public std::enable_shared_from_this +{ +public: + StorageHandleState(); + virtual ~StorageHandleState() = default; + void GoInState() override; + void GoOutState() override; + bool ExecuteStateMsg(VStateMachineData *msg) override; + void Init(void); + void UnInit(void); + +private: // About VStorageMoniter + void ReportEvent(const StorageEvent &event) override; + bool StorageStartInitHandle(VStateMachineData *msg); +}; +#endif \ No newline at end of file diff --git a/application/MissionManager/src/TestMissionState.cpp b/application/MissionManager/src/TestMissionState.cpp new file mode 100644 index 00000000..7af850d7 --- /dev/null +++ b/application/MissionManager/src/TestMissionState.cpp @@ -0,0 +1,84 @@ +/* + * 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 "TestMissionState.h" +#include "IAppManager.h" +#include "ILog.h" +#include "IStorageManager.h" +#include "MissionStateMachine.h" +TestMissionState::TestMissionState() : MissionState("TestMissionState") +{ + mEventHandle[InternalStateEvent::ANY_STATE_SD_STATUS_PERORIED] = + std::bind(&TestMissionState::SdCardEventReportSendToApp, this, _1); + mEventHandle[InternalStateEvent::RESET_KEY_MEDIA_TASK] = + std::bind(&TestMissionState::ResetKeyMediaTaskHandle, this, _1); + mKeyClickHandle["reset"] = std::bind(&TestMissionState::ClickResetKey, this, _1); +} +void TestMissionState::GoInState() +{ + MissionState::GoInState(); + LogInfo(" ========== TestMissionState::GoInState.\n"); + AppParam mAppParam(APP_MANAGER_DEVICE_IP, APP_MANAGER_HTTP_SERVER_PORT, APP_MANAGER_TCP_SERVER_PORT); // TODO: + IAppManager::GetInstance()->Init(mAppParam); + std::shared_ptr monitor = + std::dynamic_pointer_cast(MissionState::shared_from_this()); + IAppManager::GetInstance()->SetAppMonitor(monitor); + ControlDeviceStatusLed(DeviceStatus::NORMAL); +} +void TestMissionState::GoOutState() +{ + MissionState::GoOutState(); + LogInfo(" ========== TestMissionState::GoOutState.\n"); +} +bool TestMissionState::SdCardEventReportSendToApp(VStateMachineData *msg) +{ + std::shared_ptr message = std::dynamic_pointer_cast(msg->GetMessageObj()); + std::shared_ptr> data = + std::dynamic_pointer_cast>(message->mMissionData); + LogInfo(" SdCardEventHandle event:%s.\n", IStorageManager::GetInstance()->PrintStringStorageEvent(data->mData)); + SdCardStatus status = SdCardStatusConvert(data->mData); + IAppManager::GetInstance()->SetSdCardStatus(status); + return EXECUTED; +} +bool TestMissionState::ResetKeyMediaTaskHandle(VStateMachineData *msg) +{ + MissionStateMachine::GetInstance()->DelayMessage(msg); + MissionStateMachine::GetInstance()->SwitchState(SystemState::MEDIA_HANDLE_STATE); + return EXECUTED; +} +bool TestMissionState::ClickResetKey(const KeyEventData &data) +{ + LogInfo("reset key click:make a media task.\n"); + // std::shared_ptr message = std::make_shared>( + // static_cast(InternalStateEvent::MEDIA_REPORT_EVENT), event); + // MissionStateMachine::GetInstance()->SendStateMessage(message); + std::shared_ptr message = + std::make_shared(static_cast(InternalStateEvent::RESET_KEY_MEDIA_TASK)); + MissionStateMachine::GetInstance()->SendStateMessage(message); + return EXECUTED; +} +SdCardStatus TestMissionState::SdCardStatusConvert(const StorageEvent &event) +{ + switch (event) { + case StorageEvent::SD_CARD_INSERT: + return SdCardStatus::NORMAL; + case StorageEvent::SD_CARD_REMOVE: + return SdCardStatus::NOT_INSERTED; + case StorageEvent::SD_ABNORMAL: + return SdCardStatus::NOT_INSERTED; + default: + LogError("SdCardStatusConvert failed.\n"); + return SdCardStatus::END; + } +} \ No newline at end of file diff --git a/application/MissionManager/src/TestMissionState.h b/application/MissionManager/src/TestMissionState.h new file mode 100644 index 00000000..52aa089e --- /dev/null +++ b/application/MissionManager/src/TestMissionState.h @@ -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. + */ +#ifndef TEST_MISSION_STATE_H +#define TEST_MISSION_STATE_H +#include "AppMonitor.h" +#include "IStateMachine.h" +#include "IStorageManager.h" +#include "MissionState.h" +class TestMissionState : public MissionState, public AppMonitor +{ +public: + TestMissionState(); + virtual ~TestMissionState() = default; + void GoInState() override; + void GoOutState() override; + +private: + bool SdCardEventReportSendToApp(VStateMachineData *msg); + bool ResetKeyMediaTaskHandle(VStateMachineData *msg); + bool ClickResetKey(const KeyEventData &data); + +private: + SdCardStatus SdCardStatusConvert(const StorageEvent &event); +}; +#endif \ No newline at end of file diff --git a/application/MissionManager/src/TopState.cpp b/application/MissionManager/src/TopState.cpp new file mode 100644 index 00000000..83a6fa5e --- /dev/null +++ b/application/MissionManager/src/TopState.cpp @@ -0,0 +1,68 @@ +/* + * 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 "TopState.h" +#include "ILog.h" +#include "IMediaManager.h" +#include "KeyControl.h" +#include "MissionStateMachine.h" +TopState::TopState() : State("TopState") +{ + mEventHandle[InternalStateEvent::STORAGE_HANDLE_STATE_INIT] = + std::bind(&TopState::StorageStartInitHandle, this, _1); +} +void TopState::GoInState() +{ + LogInfo(" ========== TopState::GoInState.\n"); + std::shared_ptr mcuMonitor = std::dynamic_pointer_cast(shared_from_this()); + McuMonitor::Init(mcuMonitor); + std::shared_ptr mediaMonitor = std::dynamic_pointer_cast(shared_from_this()); + IMediaManager::GetInstance()->SetMediaMonitor(mediaMonitor); + MissionStateMachine::GetInstance()->SwitchState(SystemState::MISSION_STATE); + std::shared_ptr keysMonitor = std::dynamic_pointer_cast(shared_from_this()); + IDeviceManager::GetInstance()->SetAllKeysMonitor(keysMonitor); +} +void TopState::GoOutState() +{ + LogInfo(" ========== TopState::GoOutState.\n"); +} +bool TopState::ExecuteStateMsg(VStateMachineData *msg) +{ + return DataProcessing::EventHandle(msg); +} +StatusCode TopState::ReportEvent(const MediaReportEvent &event) +{ + LogInfo(" ReportEvent:\n"); + std::shared_ptr message = std::make_shared>( + static_cast(InternalStateEvent::MEDIA_REPORT_EVENT), event); + MissionStateMachine::GetInstance()->SendStateMessage(message); + return CreateStatusCode(STATUS_CODE_OK); +} +void TopState::KeyEventReport(const std::string &keyName, const VirtualKeyEvent &event, const unsigned int &timeMs) +{ + LogInfo(" KeyEventReport:key name = %s, key event = %s, time = %d\n", + keyName.c_str(), + PrintKeyEvent(static_cast(event)), + timeMs); + KeyEventData data(keyName, static_cast(event), timeMs); + std::shared_ptr message = std::make_shared>( + static_cast(InternalStateEvent::KEY_EVENT_HANDLE), data); + MissionStateMachine::GetInstance()->SendStateMessage(message); +} +bool TopState::StorageStartInitHandle(VStateMachineData *msg) +{ + MissionStateMachine::GetInstance()->DelayMessage(msg); + MissionStateMachine::GetInstance()->SwitchState(SystemState::STORAGE_HANDLE_STATE); + return EXECUTED; +} \ No newline at end of file diff --git a/application/MissionManager/src/TopState.h b/application/MissionManager/src/TopState.h new file mode 100644 index 00000000..e55e6e74 --- /dev/null +++ b/application/MissionManager/src/TopState.h @@ -0,0 +1,45 @@ +/* + * 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 TOP_STATE_H +#define TOP_STATE_H +#include "DataProcessing.h" +#include "IDeviceManager.h" +#include "IMediaManager.h" +#include "IStateMachine.h" +#include "McuMonitor.h" +class TopState : public State, + public DataProcessing, + public VMediaMonitor, + public McuMonitor, + public VKeyMonitor, + public std::enable_shared_from_this +{ +public: + TopState(); + virtual ~TopState() = default; + void GoInState() override; + void GoOutState() override; + bool ExecuteStateMsg(VStateMachineData *msg) override; + +private: // About VMediaMonitor + StatusCode ReportEvent(const MediaReportEvent &event) override; + +private: // About KeyMonitor + void KeyEventReport(const std::string &keyName, const VirtualKeyEvent &event, const unsigned int &timeMs) override; + +private: + bool StorageStartInitHandle(VStateMachineData *msg); +}; +#endif \ No newline at end of file diff --git a/application/MissionManager/src/UpgradeState.cpp b/application/MissionManager/src/UpgradeState.cpp new file mode 100644 index 00000000..e9c70044 --- /dev/null +++ b/application/MissionManager/src/UpgradeState.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 "UpgradeState.h" +#include "IHuntingUpgrade.h" +#include "ILog.h" +#include "IMediaManager.h" +#include "MissionStateMachine.h" +UpgradeState::UpgradeState() : State("UpgradeState") +{ + mEventHandle[InternalStateEvent::CHECK_UPGRADE_FILE] = std::bind(&UpgradeState::CheckUpgradeFileHandle, this, _1); +} +void UpgradeState::GoInState() +{ + LogInfo(" ========== UpgradeState::GoInState.\n"); +} +void UpgradeState::GoOutState() +{ + LogInfo(" ========== UpgradeState::GoOutState.\n"); +} +bool UpgradeState::ExecuteStateMsg(VStateMachineData *msg) +{ + return DataProcessing::EventHandle(msg); +} +bool UpgradeState::CheckUpgradeFileHandle(VStateMachineData *msg) +{ + // TODO: need to improve:It cannot be blocked. + IHuntingUpgrade::GetInstance()->CheckUpgradeFile(); + MissionStateMachine::GetInstance()->SwitchState(SystemState::IDLE_STATE); + return EXECUTED; +} \ No newline at end of file diff --git a/application/MissionManager/src/UpgradeState.h b/application/MissionManager/src/UpgradeState.h new file mode 100644 index 00000000..99b23ebd --- /dev/null +++ b/application/MissionManager/src/UpgradeState.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 UPGRADE_STATE_H +#define UPGRADE_STATE_H +#include "DataProcessing.h" +#include "IMediaManager.h" +#include "IStateMachine.h" +class UpgradeState : public State, public DataProcessing, public std::enable_shared_from_this +{ +public: + UpgradeState(); + virtual ~UpgradeState() = default; + void GoInState() override; + void GoOutState() override; + bool ExecuteStateMsg(VStateMachineData *msg) override; + +private: + bool CheckUpgradeFileHandle(VStateMachineData *msg); +}; +#endif \ No newline at end of file diff --git a/application/main/CMakeLists.txt b/application/main/CMakeLists.txt new file mode 100644 index 00000000..b0d5e124 --- /dev/null +++ b/application/main/CMakeLists.txt @@ -0,0 +1,65 @@ +include(${CMAKE_SOURCE_DIR_IPCSDK}/build/global_config.cmake) +set(EXECUTABLE_OUTPUT_PATH ${EXEC_OUTPUT_PATH}) +set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH}) + + +set(MAIN_INCLUDE_PATH "${APPLICATION_SOURCE_PATH}/main/src" CACHE STRING INTERNAL FORCE) +set(MAIN_INCLUDE_PATH "${MAIN_INCLUDE_PATH};${UTILS_SOURCE_PATH}/StatusCode/include" CACHE STRING INTERNAL FORCE) +set(MAIN_INCLUDE_PATH "${MAIN_INCLUDE_PATH};${UTILS_SOURCE_PATH}/Log/include" CACHE STRING INTERNAL FORCE) +set(MAIN_INCLUDE_PATH "${MAIN_INCLUDE_PATH};${HAL_SOURCE_PATH}/include" CACHE STRING INTERNAL FORCE) +include_directories(${MAIN_INCLUDE_PATH}) + +link_directories( + ${LIBS_OUTPUT_PATH} + ${HAL_SOURCE_PATH}/include +) + +aux_source_directory(./ SRC_FILES) +# aux_source_directory(./src SRC_FILES) + +# Mark src files for test. +file(GLOB_RECURSE MAIN_SRC_FILE_THIS src/*.cpp src/*.c) +set(MAIN_SRC_FILE "${MAIN_SRC_FILE_THIS}" CACHE STRING INTERNAL FORCE) + + +set(TARGET_LIB MainLib) +add_library(${TARGET_LIB} STATIC ${MAIN_SRC_FILE_THIS}) +set(TARGET_NAME ipc_x86) +add_executable(${TARGET_NAME} ${SRC_FILES}) + +set(LINK_LIB StatusCode Log Hal pthread dl) +set(MAIN_LINK_LIB "${LINK_LIB}" CACHE STRING INTERNAL FORCE) +target_link_libraries(${TARGET_LIB} ${MAIN_LINK_LIB}) +target_link_libraries(${TARGET_NAME} ${TARGET_LIB}) +if(${TEST_COVERAGE} MATCHES "true") + target_link_libraries(${TARGET_NAME} gcov) +endif() + +if ("${COMPILE_IMPROVE_SUPPORT}" MATCHES "true") +add_custom_target( + ipc_x86_code_check + COMMAND ${CLANG_TIDY_EXE} + -checks='${CLANG_TIDY_CHECKS}' + --header-filter=.* + --system-headers=false + ${MAIN_SRC_FILE_THIS} + ${CLANG_TIDY_CONFIG} + -p ${PLATFORM_PATH}/cmake-shell + WORKING_DIRECTORY ${APPLICATION_SOURCE_PATH}/main +) +file(GLOB_RECURSE HEADER_FILES *.h) +add_custom_target( + ipc_x86_code_format + COMMAND ${CLANG_FORMAT_EXE} + -style=file + -i ${SRC_FILES} ${HEADER_FILES} ${MAIN_SRC_FILE_THIS} + WORKING_DIRECTORY ${APPLICATION_SOURCE_PATH}/main +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make ipc_x86_code_check + COMMAND make ipc_x86_code_format + WORKING_DIRECTORY ${PLATFORM_PATH}/cmake-shell/ +) +endif() diff --git a/application/main/main.cpp b/application/main/main.cpp new file mode 100644 index 00000000..8c1b801c --- /dev/null +++ b/application/main/main.cpp @@ -0,0 +1,24 @@ +/* + * 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 "ILog.h" +#include "MainThread.h" +#include +int main(int argc, char *argv[]) +{ + MainThread::GetInstance()->Init(); + MainThread::GetInstance()->Runing(); + MainThread::GetInstance()->UnInit(); + return 0; +} \ No newline at end of file diff --git a/application/main/src/MainThread.cpp b/application/main/src/MainThread.cpp new file mode 100644 index 00000000..49353c9f --- /dev/null +++ b/application/main/src/MainThread.cpp @@ -0,0 +1,73 @@ +/* + * 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 "MainThread.h" +#include "IHalCpp.h" +#include "ILog.h" +#include +MainThread::MainThread() +{ + mMainThreadRuning = false; +} +std::shared_ptr &MainThread::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; +} +StatusCode MainThread::Init(void) +{ + CreateLogModule(); + ILogInit(LOG_EASYLOGGING); + CustomizationInit(); + mMainThreadRuning = true; + CreateAllModules(); + // IHalInit(); + IHalCpp::GetInstance()->Init(); + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode MainThread::UnInit(void) +{ + IHalCpp::GetInstance()->UnInit(); + DestoryAllModules(); + ILogUnInit(); + return CreateStatusCode(STATUS_CODE_OK); +} +StatusCode MainThread::CreateAllModules(void) +{ + // CreateLogModule(); + // CreateHalModule(); + CreateHalCppModule(); + return CreateStatusCode(STATUS_CODE_OK); +} +void MainThread::DestoryAllModules(void) +{ +} +void MainThread::ResetAllPtrMaker(void) +{ +} +void MainThread::Runing(void) +{ + while (mMainThreadRuning) { + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + } +} \ No newline at end of file diff --git a/application/main/src/MainThread.h b/application/main/src/MainThread.h new file mode 100644 index 00000000..6d3fd9e0 --- /dev/null +++ b/application/main/src/MainThread.h @@ -0,0 +1,44 @@ +/* + * 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 MAIN_THREAD_H +#define MAIN_THREAD_H +#include "StatusCode.h" +#include +class MainThread +{ +public: + MainThread(); + virtual ~MainThread() = default; + static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); + virtual StatusCode Init(void); + virtual StatusCode UnInit(void); + void Runing(void); + void Exit(void) + { + mMainThreadRuning = false; + } + virtual void CustomizationInit(void) + { + } + +private: + StatusCode CreateAllModules(void); + void DestoryAllModules(void); + void ResetAllPtrMaker(void); + +private: + bool mMainThreadRuning; +}; +#endif // !MAIN_THREAD_H \ No newline at end of file diff --git a/external/.gitignore b/external/.gitignore index 8b47a85e..9c683404 100644 --- a/external/.gitignore +++ b/external/.gitignore @@ -1,2 +1,3 @@ -goahead-5.2.0/GoAhead \ No newline at end of file +goahead-5.2.0/GoAhead +ffmpeg/ffmpeg-6.1.1 \ No newline at end of file diff --git a/external/CMakeLists.txt b/external/CMakeLists.txt index ce28bb86..1b27a22c 100644 --- a/external/CMakeLists.txt +++ b/external/CMakeLists.txt @@ -19,4 +19,5 @@ add_subdirectory(httpserver.h-master/src) # ================= httpserver end ================= # add_subdirectory(cJSON-1.7.17) -add_subdirectory(libhv/libhv-1.3.2) \ No newline at end of file +add_subdirectory(libhv/libhv-1.3.2) +add_subdirectory(ffmpeg) \ No newline at end of file diff --git a/external/ffmpeg/CMakeLists.txt b/external/ffmpeg/CMakeLists.txt new file mode 100644 index 00000000..908b6802 --- /dev/null +++ b/external/ffmpeg/CMakeLists.txt @@ -0,0 +1,40 @@ + + +add_custom_target( + ffmpeg + # DEPENDS ${EXTERNAL_LIBS_OUTPUT_PATH}/libgo.a + # COMMAND mkdir ${GOAHEAD_UPLOAD_TMP_PATH} + # COMMAND cp ${EXTERNAL_SOURCE_PATH}/goahead-5.2.0/modify/http.c ${EXTERNAL_SOURCE_PATH}/goahead-5.2.0/GoAhead/src + # COMMAND touch ${EXTERNAL_SOURCE_PATH}/goahead-5.2.0/GoAhead/src/http.c + COMMAND test -f ${EXTERNAL_SOURCE_PATH}/ffmpeg/Makefile || tar -xf ffmpeg_6.1.1.orig.tar.xz + COMMAND cd ffmpeg-6.1.1 && ./configure --enable-cross-compile --target-os=linux --arch=arm64 + --cc=${CMAKE_C_COMPILER} + --cxx=${CMAKE_CXX_COMPILER} + --prefix=${EXTERNAL_LIBS_OUTPUT_PATH}/ffmpeg + --disable-asm --enable-parsers --disable-decoders --enable-decoder=h264 + --disable-debug --enable-ffmpeg --enable-shared --enable-static --disable-stripping --disable-doc + --enable-gpl --enable-nonfree --enable-version3 --enable-small + --disable-mipsdsp --disable-mipsdspr2 + --disable-encoders + --disable-muxers --enable-muxer=mov --enable-muxer=mp4 + --disable-decoders --enable-decoder=aac + --disable-filters + --disable-demuxers --enable-demuxer=mov + --disable-parsers + --disable-protocols --enable-protocol=file + --disable-bsfs --enable-bsf=aac_adtstoasc --enable-bsf=h264_mp4toannexb --enable-bsf=hevc_mp4toannexb + --disable-indevs + --disable-outdevs --disable-ffprobe --disable-ffmpeg --disable-ffplay --disable-debug + COMMAND cd ffmpeg-6.1.1 && make + COMMAND cd ffmpeg-6.1.1 && make install + WORKING_DIRECTORY ${EXTERNAL_SOURCE_PATH}/ffmpeg/ +) + +add_custom_target( + remove_ffmpeg_source_files + COMMAND rm -rf ffmpeg-6.1.1 + WORKING_DIRECTORY ${EXTERNAL_SOURCE_PATH}/ffmpeg/ +) + +# 将clean目标依赖于我们自定义的clean_script目标 +add_dependencies(sdk_clean remove_ffmpeg_source_files) \ No newline at end of file diff --git a/external/ffmpeg/build_ffmpeg.sh b/external/ffmpeg/build_ffmpeg.sh new file mode 100755 index 00000000..92ee6896 --- /dev/null +++ b/external/ffmpeg/build_ffmpeg.sh @@ -0,0 +1,10 @@ +#!/bin/bash +# 编译ffmpeg +echo "Compile ffmpeg." +cd ffmpeg-6.1.1 +./configure --enable-cross-compile --target-os=linux --arch=arm64 \ + --cc=gcc \ + --cxx=g++ \ + --prefix=/home/xiaojiazhu/project/tmp \ + --disable-asm --enable-parsers --disable-decoders --enable-decoder=h264 --enable-decoder=aac \ + --disable-debug --enable-ffmpeg --enable-shared --enable-static --disable-stripping --disable-doc diff --git a/external/ffmpeg/ffmpeg_6.1.1.orig.tar.xz b/external/ffmpeg/ffmpeg_6.1.1.orig.tar.xz new file mode 100755 index 00000000..3c4a2a24 Binary files /dev/null and b/external/ffmpeg/ffmpeg_6.1.1.orig.tar.xz differ diff --git a/middleware/DeviceManager/include/IDeviceManager.h b/middleware/DeviceManager/include/IDeviceManager.h index a479a4a1..a6a53897 100644 --- a/middleware/DeviceManager/include/IDeviceManager.h +++ b/middleware/DeviceManager/include/IDeviceManager.h @@ -38,11 +38,11 @@ public: virtual ~VKeyMonitor() = default; virtual void KeyEventReport(const std::string &keyName, const VirtualKeyEvent &event, const unsigned int &timeMs); }; -class VirtualLedControl +class LedControlContext { public: - VirtualLedControl() = default; - virtual ~VirtualLedControl() = default; + LedControlContext() = default; + virtual ~LedControlContext() = default; }; class IDeviceManager { @@ -52,7 +52,7 @@ public: static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); virtual const StatusCode Init(void); virtual const StatusCode UnInit(void); - virtual const StatusCode ControlLed(const std::string &ledName, std::shared_ptr &control); + virtual const StatusCode ControlLed(const std::string &ledName, std::shared_ptr &control); virtual const StatusCode SetAllKeysMonitor(std::shared_ptr &monitor); }; #endif \ No newline at end of file diff --git a/middleware/DeviceManager/src/DeviceManager.cpp b/middleware/DeviceManager/src/DeviceManager.cpp index c97eea37..a23b1913 100644 --- a/middleware/DeviceManager/src/DeviceManager.cpp +++ b/middleware/DeviceManager/src/DeviceManager.cpp @@ -36,7 +36,7 @@ const StatusCode DeviceManager::SetAllKeysMonitor(std::shared_ptr & LogInfo("DeviceManager::SetAllKeysMonitor\n"); return KeyManager::GetInstance()->SetKeyMonitor(monitor); } -const StatusCode DeviceManager::ControlLed(const std::string &ledName, std::shared_ptr &control) +const StatusCode DeviceManager::ControlLed(const std::string &ledName, std::shared_ptr &control) { LedManager::GetInstance()->ControlLed(ledName, control); return CreateStatusCode(STATUS_CODE_OK); diff --git a/middleware/DeviceManager/src/DeviceManager.h b/middleware/DeviceManager/src/DeviceManager.h index faf003c7..682ce656 100644 --- a/middleware/DeviceManager/src/DeviceManager.h +++ b/middleware/DeviceManager/src/DeviceManager.h @@ -26,7 +26,7 @@ public: const StatusCode Init(void) override; const StatusCode UnInit(void) override; const StatusCode SetAllKeysMonitor(std::shared_ptr &monitor) override; - const StatusCode ControlLed(const std::string &ledName, std::shared_ptr &control) override; + const StatusCode ControlLed(const std::string &ledName, std::shared_ptr &control) override; private: // std::vector> mLedManagers; diff --git a/middleware/DeviceManager/src/IDeviceManager.cpp b/middleware/DeviceManager/src/IDeviceManager.cpp index 3c3ff155..4ad0e7d1 100644 --- a/middleware/DeviceManager/src/IDeviceManager.cpp +++ b/middleware/DeviceManager/src/IDeviceManager.cpp @@ -39,7 +39,7 @@ const StatusCode IDeviceManager::UnInit(void) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } -const StatusCode IDeviceManager::ControlLed(const std::string &ledName, std::shared_ptr &control) +const StatusCode IDeviceManager::ControlLed(const std::string &ledName, std::shared_ptr &control) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } diff --git a/middleware/DeviceManager/src/LedManager.cpp b/middleware/DeviceManager/src/LedManager.cpp index a1477220..5f3a33ae 100644 --- a/middleware/DeviceManager/src/LedManager.cpp +++ b/middleware/DeviceManager/src/LedManager.cpp @@ -37,6 +37,11 @@ void LedManager::Init(void) void LedManager::UnInit(void) { StopTimer(); + mMutex.lock(); + for (auto &iter : mAllLedHal) { + iter.second->DeleteAllState(); + } + mMutex.unlock(); mAllLedHal.clear(); } void LedManager::Timer(void) @@ -53,7 +58,7 @@ void LedManager::Timer(void) std::this_thread::sleep_for(std::chrono::milliseconds(LED_STATE_CHECK_PERIOD_MS)); } } -void LedManager::ControlLed(const std::string &ledName, std::shared_ptr &control) +void LedManager::ControlLed(const std::string &ledName, std::shared_ptr &control) { std::lock_guard locker(mMutex); std::shared_ptr singleControl = std::dynamic_pointer_cast(control); @@ -61,6 +66,11 @@ void LedManager::ControlLed(const std::string &ledName, std::shared_ptrAddLedState(singleControl); } void LedManager::StartTimer(void) diff --git a/middleware/DeviceManager/src/LedManager.h b/middleware/DeviceManager/src/LedManager.h index 07d1ca8b..2ebc6eab 100644 --- a/middleware/DeviceManager/src/LedManager.h +++ b/middleware/DeviceManager/src/LedManager.h @@ -31,7 +31,7 @@ public: void UnInit(void); void StartTimer(void); void StopTimer(void); - void ControlLed(const std::string &ledName, std::shared_ptr &control); + void ControlLed(const std::string &ledName, std::shared_ptr &control); private: void Timer(void); diff --git a/output_files/libs/test_tools/libDeviceManagerTestTool.a b/output_files/libs/test_tools/libDeviceManagerTestTool.a index e52b40a9..1486e761 100644 Binary files a/output_files/libs/test_tools/libDeviceManagerTestTool.a and b/output_files/libs/test_tools/libDeviceManagerTestTool.a differ diff --git a/output_files/libs/test_tools/libHalTestTool.a b/output_files/libs/test_tools/libHalTestTool.a new file mode 100644 index 00000000..bcbc9c0d Binary files /dev/null and b/output_files/libs/test_tools/libHalTestTool.a differ diff --git a/test.txt b/test.txt new file mode 100644 index 00000000..e69de29b diff --git a/test/application/HuntingCamera/CMakeLists.txt b/test/application/HuntingCamera/CMakeLists.txt index 11ea37b7..8131a8c3 100644 --- a/test/application/HuntingCamera/CMakeLists.txt +++ b/test/application/HuntingCamera/CMakeLists.txt @@ -42,8 +42,10 @@ endif() set(TARGET_NAME HuntingCameraTest) add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES}) +target_link_libraries(${TARGET_NAME} McuProtocolTestTool UartDeviceTestTool) target_link_libraries(${TARGET_NAME} HuntingMainLib MissionManagerTestTool McuManagerTestTool McuAskBaseTestTool - AppManagerTestTool HalTestTool DeviceManagerTestTool TestManager gtest gmock pthread) + AppManagerTestTool HalTestTool DeviceManagerTestTool TestManager UartDevice Hal LinuxApiMock gtest gmock pthread) +target_link_libraries(${TARGET_NAME} UpgradeTool Servers) if(${TEST_COVERAGE} MATCHES "true") target_link_libraries(${TARGET_NAME} gcov) endif() diff --git a/test/application/HuntingCamera/src_mock/DeviceManager_Mock_Test.cpp b/test/application/HuntingCamera/src_mock/DeviceManager_Mock_Test.cpp index e38c8437..1a0dc5dd 100644 --- a/test/application/HuntingCamera/src_mock/DeviceManager_Mock_Test.cpp +++ b/test/application/HuntingCamera/src_mock/DeviceManager_Mock_Test.cpp @@ -65,4 +65,18 @@ TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_KeyControlClick) std::this_thread::sleep_for(std::chrono::milliseconds(100)); MainThread::GetInstance()->Runing(); } +/** + * @brief Construct a new test f object + * ../output_files/test/bin/HuntingCameraTest + * --gtest_filter=HuntingCameraTest.HS_INTEGRATION_HunttingCamera_EXAMPLE_LedControl + */ +TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_LedControl) +{ + SetAllLedsResult(mAllLedsMock); + MockOtherSideIpcMissionReply(IpcMission::TEST); + MainThread::GetInstance()->Init(); + TestManager::ResetTimeOut(1000 * 3); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + MainThread::GetInstance()->Runing(); +} } // namespace DeviceManager_Mock_Test \ No newline at end of file diff --git a/test/application/HuntingCamera/src_mock/HuntingCameraTest.cpp b/test/application/HuntingCamera/src_mock/HuntingCameraTest.cpp index 9759b887..579e0adb 100644 --- a/test/application/HuntingCamera/src_mock/HuntingCameraTest.cpp +++ b/test/application/HuntingCamera/src_mock/HuntingCameraTest.cpp @@ -18,6 +18,7 @@ #include "MainThread.h" #include const char *KEY_RESET = "reset"; +const char *LED_DEVICE_STATUS = "device_status"; void MainThreadTest::CustomizationInit(void) { // Do nothing here to make sure test tool work. @@ -44,6 +45,7 @@ void HuntingCameraTest::SetUp() MainThread::GetInstance(&mainThread); HalTestTool::Init(); CreateAllKeysMcok(); + CreateAllLedsMcok(); AppManagerTestTool::Init(); MissionManagerTestTool::Init(); mLinuxTest = LinuxTest::CreateLinuxTest(); @@ -72,6 +74,7 @@ void HuntingCameraTest::TearDown() DeviceManagerTestTool::UnInit(); DestroyAllCamerasMock(); DestroyAllKeysMock(); + DestroyAllLedsMock(); } void HuntingCameraTest::CreateAllCamerasMcok(void) { @@ -86,13 +89,19 @@ void HuntingCameraTest::CreateAllKeysMcok(void) { std::shared_ptr key = HalTestTool::MakeKeyHalTest(KEY_RESET); mAllKeysMock[KEY_RESET] = key; - // std::shared_ptr led = HalTestTool::MakeLedHalTest(LED_TEST); - // mAllLedsMock[LED_TEST] = led; HalTestTool::SetAllKeysResult(mAllKeysMock); } void HuntingCameraTest::DestroyAllKeysMock(void) { mAllKeysMock.clear(); - // mAllLedsMock.clear(); +} +void HuntingCameraTest::CreateAllLedsMcok(void) +{ + std::shared_ptr led = HalTestTool::MakeLedHalTest(LED_DEVICE_STATUS); + mAllLedsMock[LED_DEVICE_STATUS] = led; +} +void HuntingCameraTest::DestroyAllLedsMock(void) +{ + mAllLedsMock.clear(); } \ No newline at end of file diff --git a/test/application/HuntingCamera/src_mock/HuntingCameraTest.h b/test/application/HuntingCamera/src_mock/HuntingCameraTest.h index 3cae61d3..29215075 100644 --- a/test/application/HuntingCamera/src_mock/HuntingCameraTest.h +++ b/test/application/HuntingCamera/src_mock/HuntingCameraTest.h @@ -52,11 +52,14 @@ private: void DestroyAllCamerasMock(void); void CreateAllKeysMcok(void); void DestroyAllKeysMock(void); + void CreateAllLedsMcok(void); + void DestroyAllLedsMock(void); protected: std::shared_ptr mLinuxTest; std::map> mAllCamerasMock; std::map> mAllKeysMock; + std::map> mAllLedsMock; }; #endif \ No newline at end of file diff --git a/test/hal/CMakeLists.txt b/test/hal/CMakeLists.txt index c5ccbcc6..044fbda9 100644 --- a/test/hal/CMakeLists.txt +++ b/test/hal/CMakeLists.txt @@ -12,18 +12,16 @@ include_directories( link_directories( ${LIBS_OUTPUT_PATH} ${EXTERNAL_LIBS_OUTPUT_PATH} + ${TEST_TOOLS_OUTPUT_PATH} ) - - - - aux_source_directory(. SRC_FILES) aux_source_directory(./src SRC_FILES) set(TARGET_NAME HalTest) add_executable(${TARGET_NAME} ${SRC_FILES}) target_link_libraries(${TARGET_NAME} HalTestTool gtest gmock pthread) +target_link_libraries(${TARGET_NAME} Hal Log LinuxApiMock) if(${TEST_COVERAGE} MATCHES "true") target_link_libraries(${TARGET_NAME} gcov) endif() diff --git a/test/middleware/AppManager/CMakeLists.txt b/test/middleware/AppManager/CMakeLists.txt index 2fbe857e..ea265eb8 100644 --- a/test/middleware/AppManager/CMakeLists.txt +++ b/test/middleware/AppManager/CMakeLists.txt @@ -37,6 +37,7 @@ endif() set(TARGET_NAME AppManagerTest) add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES}) target_link_libraries(${TARGET_NAME} AppManagerTestTool HalTestTool gtest gmock pthread) +target_link_libraries(${TARGET_NAME} AppManager Servers KeyControl LedControl Hal LinuxApiMock) if(${TEST_COVERAGE} MATCHES "true") target_link_libraries(${TARGET_NAME} gcov) endif() diff --git a/test/middleware/DeviceManager/CMakeLists.txt b/test/middleware/DeviceManager/CMakeLists.txt index ac68481b..c159c9e9 100644 --- a/test/middleware/DeviceManager/CMakeLists.txt +++ b/test/middleware/DeviceManager/CMakeLists.txt @@ -35,6 +35,7 @@ endif() set(TARGET_NAME DeviceManagerTest) add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES}) target_link_libraries(${TARGET_NAME} DeviceManager DeviceManagerTestTool HalTestTool gtest gmock pthread) +target_link_libraries(${TARGET_NAME} Hal LinuxApiMock) if(${TEST_COVERAGE} MATCHES "true") target_link_libraries(${TARGET_NAME} gcov) endif() diff --git a/test/middleware/DeviceManager/src/DeviceManager_Test.cpp b/test/middleware/DeviceManager/src/DeviceManager_Test.cpp index cd56856b..ebe37f02 100644 --- a/test/middleware/DeviceManager/src/DeviceManager_Test.cpp +++ b/test/middleware/DeviceManager/src/DeviceManager_Test.cpp @@ -196,4 +196,20 @@ TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed4) std::this_thread::sleep_for(std::chrono::milliseconds(2000)); IDeviceManager::GetInstance()->UnInit(); } +// ../output_files/test/bin/DeviceManagerTest +// --gtest_filter=DeviceManagerTest.INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed5 +TEST_F(DeviceManagerTest, INTEGRATION_DeviceManager_EXAMPLE_AUTO_ControlLed5) +{ + SetAllLedsResult(mAllLedsMock); + IDeviceManager::GetInstance()->Init(); + std::shared_ptr monitor = std::make_shared(); + IDeviceManager::GetInstance()->SetAllKeysMonitor(monitor); + std::shared_ptr ledControl = + DeviceManagerTestTool::ControlLed(LED_TEST, LedState::ON, KEEP_ALIVE_FOREVER, LED_NOT_BLINK); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + DeviceManagerTestTool::DestoryLedControl(ledControl); + DeviceManagerTestTool::ControlLed(LED_TEST, LedState::ON, KEEP_ALIVE_FOREVER, LED_NOT_BLINK); + std::this_thread::sleep_for(std::chrono::milliseconds(1000)); + IDeviceManager::GetInstance()->UnInit(); +} } // namespace DeviceManagerTest \ No newline at end of file diff --git a/test/middleware/DeviceManager/tool/include/DeviceManagerTestTool.h b/test/middleware/DeviceManager/tool/include/DeviceManagerTestTool.h index 9ee60272..56f0a526 100644 --- a/test/middleware/DeviceManager/tool/include/DeviceManagerTestTool.h +++ b/test/middleware/DeviceManager/tool/include/DeviceManagerTestTool.h @@ -54,8 +54,9 @@ public: void UnInit(void); public: - std::shared_ptr ControlLed(const std::string &ledName, const LedState &state, + std::shared_ptr ControlLed(const std::string &ledName, const LedState &state, const unsigned int &aliveTimeMs, const unsigned int &blinkTimeMs); + void DestoryLedControl(std::shared_ptr &ledControl); protected: void DeviceManagerNotice(const std::string &keyName, const unsigned int &pressingTimeMs) override; diff --git a/test/middleware/HuntingUpgrade/CMakeLists.txt b/test/middleware/HuntingUpgrade/CMakeLists.txt index fd0abb7f..ae94240e 100644 --- a/test/middleware/HuntingUpgrade/CMakeLists.txt +++ b/test/middleware/HuntingUpgrade/CMakeLists.txt @@ -34,6 +34,7 @@ endif() set(TARGET_NAME HuntingUpgradeTest) add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES}) target_link_libraries(${TARGET_NAME} HuntingUpgradeTestTool gtest gmock pthread) +target_link_libraries(${TARGET_NAME} HuntingUpgrade UpgradeTool Log LinuxApiMock) if(${TEST_COVERAGE} MATCHES "true") target_link_libraries(${TARGET_NAME} gcov) endif() diff --git a/test/middleware/IpcConfig/CMakeLists.txt b/test/middleware/IpcConfig/CMakeLists.txt index 6ac99b43..d30bac57 100644 --- a/test/middleware/IpcConfig/CMakeLists.txt +++ b/test/middleware/IpcConfig/CMakeLists.txt @@ -35,6 +35,7 @@ endif() set(TARGET_NAME IpcConfigTest) add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES}) target_link_libraries(${TARGET_NAME} IpcConfigTestTool LinuxApi gtest gmock pthread) +target_link_libraries(${TARGET_NAME} IpcConfig LinuxApiMock) if(${TEST_COVERAGE} MATCHES "true") target_link_libraries(${TARGET_NAME} gcov) endif() diff --git a/test/middleware/McuManager/CMakeLists.txt b/test/middleware/McuManager/CMakeLists.txt index 06a68ecd..c1ef7051 100644 --- a/test/middleware/McuManager/CMakeLists.txt +++ b/test/middleware/McuManager/CMakeLists.txt @@ -43,6 +43,7 @@ endif() set(TARGET_NAME McuManagerTest) add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES}) target_link_libraries(${TARGET_NAME} McuAskBase McuManager McuManagerTestTool McuAskBaseTestTool gtest gmock pthread) +target_link_libraries(${TARGET_NAME} McuProtocolTestTool UartDeviceTestTool UartDevice LinuxApiMock) if(${TEST_COVERAGE} MATCHES "true") target_link_libraries(${TARGET_NAME} gcov) endif() diff --git a/test/middleware/MediaManager/CMakeLists.txt b/test/middleware/MediaManager/CMakeLists.txt index 539561a8..75ce039e 100644 --- a/test/middleware/MediaManager/CMakeLists.txt +++ b/test/middleware/MediaManager/CMakeLists.txt @@ -36,6 +36,7 @@ endif() set(TARGET_NAME MediaManagerTest) add_executable(${TARGET_NAME} ${SRC_FILES_MAIN} ${SRC_FILES}) target_link_libraries(${TARGET_NAME} MediaManager MediaManagerTestTool HalTestTool gtest gmock pthread) +target_link_libraries(${TARGET_NAME} Hal LedControl LinuxApiMock) if(${TEST_COVERAGE} MATCHES "true") target_link_libraries(${TARGET_NAME} gcov) endif() diff --git a/tools/README.md b/tools/README.md index 5b034c55..550aa8cb 100644 --- a/tools/README.md +++ b/tools/README.md @@ -1,4 +1,3 @@ -<<<<<<< HEAD # 1. 工具 ## 1.1. 概述 @@ -22,10 +21,4 @@ $ ./delete_test_code.sh ../../test/ ``` $ cd //tools/shell $ ./delete_sdk_code.sh -``` -======= -# 1. 工具 - -## 1.1. 概述 -  该目录存放使用到的工具。 ->>>>>>> embedded-framework/master +``` \ No newline at end of file diff --git a/tools/clang-tidy/README.md b/tools/clang-tidy/README.md index 045c12e7..9af88708 100644 --- a/tools/clang-tidy/README.md +++ b/tools/clang-tidy/README.md @@ -4,10 +4,6 @@ ## 1.1. 使用说明 -<<<<<<< HEAD 1. 在本目录执行make;会下载源码,编译,并把llvm源码包(含生成的工具文件)拷贝到**用户根目录**,以方便不同目录的源码工程可以复用llvm工具; -**如果安装时使用普通用户执行make compile_llvm,编译时使用跟用户权限(或者加sudo),可能会提示命令不存在。** -======= -1. 在工程根目录执行make compile_llvm;会下载源码,编译,并把llvm源码包(含生成的工具文件)拷贝到用户根目录,以方便不同目录的源码工程可以复用llvm工具; ->>>>>>> embedded-framework/master +**如果安装时使用普通用户执行make compile_llvm,编译时使用跟用户权限(或者加sudo),可能会提示命令不存在。** \ No newline at end of file diff --git a/tools/shell/delete_test_code.sh b/tools/shell/delete_test_code.sh index 9fba630f..2206019b 100755 --- a/tools/shell/delete_test_code.sh +++ b/tools/shell/delete_test_code.sh @@ -24,7 +24,6 @@ find "$1" -type d -name "tool" | while read -r tool_dir; do else echo "No 'src' directory found in '$tool_dir'" fi -<<<<<<< HEAD # 检查CMakeLists.txt文件是否存在,然后删除 cmake_file="$tool_dir/CMakeLists.txt" @@ -36,8 +35,6 @@ find "$1" -type d -name "tool" | while read -r tool_dir; do echo "Error deleting 'CMakeLists.txt' in '$tool_dir'" fi fi -======= ->>>>>>> embedded-framework/master done echo "Operation completed." \ No newline at end of file diff --git a/utils/LedControl/include/LedControl.h b/utils/LedControl/include/LedControl.h index c0c1a1a1..af957861 100644 --- a/utils/LedControl/include/LedControl.h +++ b/utils/LedControl/include/LedControl.h @@ -30,7 +30,12 @@ enum class LedState ON, GREEN, RED, + BLUE, YELLOW, + GREEN_RED_MEANS_YELLOW, + GREEN_BLUE_MEANS_CYAN, + RED_BLUE_MEANS_PURPLE, + GREEN_RED_BLUE_MEANS_WHITE, LEVEL_0, LEVEL_1, LEVEL_2, @@ -44,39 +49,20 @@ class VSingleControl public: VSingleControl() = default; virtual ~VSingleControl() = default; - virtual StatusCode GetLedState(LedState &state) - { - return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); - } - virtual unsigned int GetKeepAliveTimeMs(void) - { - return KEEP_ALIVE_FOREVER; - } - virtual unsigned int GetBlinkTimeMs(void) - { - return LED_NOT_BLINK; - } + virtual StatusCode GetLedState(LedState &state); + virtual unsigned int GetKeepAliveTimeMs(void); + virtual unsigned int GetBlinkTimeMs(void); }; class VLedControl { public: VLedControl() = default; virtual ~VLedControl() = default; - virtual StatusCode SetLedState(const LedState &state) - { - return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); - } - virtual void AddLedState(std::shared_ptr &control) - { - } - virtual void CheckState(const unsigned int &period) - { - } - // virtual void SetHalLedState(const VirtualLedState &state) {} - virtual std::string GetLedName(void) - { - return "undefine"; - } + virtual StatusCode SetLedState(const LedState &state); + virtual void AddLedState(std::shared_ptr &control); + virtual void CheckState(const unsigned int &period); + virtual std::string GetLedName(void); + virtual void DeleteAllState(void); }; class LedControl : virtual public VLedControl { @@ -87,6 +73,12 @@ public: void AddLedState(std::shared_ptr &state) override; private: + void DeleteAllState(void) override; + /** + * @brief Each time you control a light, check for invalid data to avoid wasting memory resources. + * + */ + void DeleteUselessState(void); void NewLedStateStart(void); void DeleteTopLedState(void); void BlinkOff(std::shared_ptr &state); diff --git a/utils/LedControl/src/LedControl.cpp b/utils/LedControl/src/LedControl.cpp index 62fea645..3191ae7c 100644 --- a/utils/LedControl/src/LedControl.cpp +++ b/utils/LedControl/src/LedControl.cpp @@ -14,9 +14,45 @@ */ #include "LedControl.h" #include "ILog.h" +#include +StatusCode VSingleControl::GetLedState(LedState &state) +{ + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} +unsigned int VSingleControl::GetKeepAliveTimeMs(void) +{ + return KEEP_ALIVE_FOREVER; +} +unsigned int VSingleControl::GetBlinkTimeMs(void) +{ + return LED_NOT_BLINK; +} +StatusCode VLedControl::SetLedState(const LedState &state) +{ + LogWarning("STATUS_CODE_VIRTUAL_FUNCTION.\n"); + return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); +} +void VLedControl::AddLedState(std::shared_ptr &control) +{ + LogWarning("STATUS_CODE_VIRTUAL_FUNCTION.\n"); +} +void VLedControl::CheckState(const unsigned int &period) +{ + LogWarning("STATUS_CODE_VIRTUAL_FUNCTION.\n"); +} +std::string VLedControl::GetLedName(void) +{ + LogWarning("STATUS_CODE_VIRTUAL_FUNCTION.\n"); + return "undefine"; +} +void VLedControl::DeleteAllState(void) +{ + LogWarning("STATUS_CODE_VIRTUAL_FUNCTION.\n"); +} void LedControl::AddLedState(std::shared_ptr &state) { LogInfo("Add led state.\n"); + DeleteUselessState(); NewLedStateStart(); LedState ledState = LedState::END; state->GetLedState(ledState); @@ -27,6 +63,24 @@ void LedControl::AddLedState(std::shared_ptr &state) } mStates.push_back(state); } +void LedControl::DeleteAllState(void) +{ + mStates.clear(); + SetLedState(LedState::OFF); +} +void LedControl::DeleteUselessState(void) +{ + constexpr bool DELETE_STATE = true; + constexpr bool KEEP_STATE = false; + auto is_to_remove = [](const std::shared_ptr &state) { + if (DELETED_LED_STATE == state->GetKeepAliveTimeMs()) { + LogInfo(" Delete useless led state.\n"); + return DELETE_STATE; + } + return KEEP_STATE; + }; + mStates.erase(std::remove_if(mStates.begin(), mStates.end(), is_to_remove), mStates.end()); +} void LedControl::CheckState(const unsigned int &period) { const int TOP_STATE_SHOW = mStates.size() - 1; @@ -71,6 +125,7 @@ void LedControl::DeleteTopLedState(void) mStates.erase(mStates.begin() + TOP_STATE_SHOW); const int NEXT_LED_STATE = mStates.size() - 1; if (NEXT_LED_STATE < 0) { + SetLedState(LedState::OFF); return; } LogInfo("Top next led state.\n");