From cdccef76a716707aae72f83eb0df609e420318da Mon Sep 17 00:00:00 2001 From: fancy <258828110.@qq.com> Date: Tue, 19 Sep 2023 07:54:26 -0700 Subject: [PATCH] Add state machine code. --- CMakeLists.txt | 2 +- application/CMakeLists.txt | 1 + application/MissionManager/CMakeLists.txt | 46 ++++++++++++++ .../MissionManager/include/IMissionManager.h | 62 +++++++++++++++++++ .../MissionManager/src/DataProcessing.cpp | 35 +++++++++++ .../MissionManager/src/DataProcessing.h | 47 ++++++++++++++ .../MissionManager/src/IMissionManager.cpp | 33 ++++++++++ .../MissionManager/src/MissionManager.cpp | 14 +++++ .../MissionManager/src/MissionManager.h | 24 +++++++ .../src/MissionManagerMakeImpl.cpp | 34 ++++++++++ .../src/MissionManagerMakeImpl.h | 30 +++++++++ .../src/MissionStateMachine.cpp | 52 ++++++++++++++++ .../MissionManager/src/MissionStateMachine.h | 43 +++++++++++++ application/MissionManager/src/TopState.cpp | 28 +++++++++ application/MissionManager/src/TopState.h | 30 +++++++++ doc/design.md | 6 +- middleware/CMakeLists.txt | 3 +- middleware/DeviceManager/CMakeLists.txt | 45 ++++++++++++++ .../DeviceManager/include/IDeviceManager.h | 35 +++++++++++ .../DeviceManager/src/IDeviceManager.cpp | 33 ++++++++++ middleware/IpcConfig/include/IIpcConfig.h | 2 +- middleware/IpcConfig/src/IIpcConfig.cpp | 2 +- middleware/IpcConfig/src/IpcConfig.cpp | 2 +- middleware/IpcConfig/src/IpcConfig.h | 2 +- middleware/IpcConfig/src/IpcConfigMakePtr.cpp | 2 +- middleware/IpcConfig/src/IpcConfigMakePtr.h | 2 +- .../StateMachine/include/IStateMachine.h | 22 +++---- middleware/StateMachine/src/IStateMachine.cpp | 2 +- .../src/OpenHarmony/internal_message.h | 2 +- .../src/OpenHarmony/state_machine.cpp | 10 +-- .../src/OpenHarmony/state_machine.h | 8 +-- .../StateMachine/src/StateMachineImpl.cpp | 4 +- .../StateMachine/src/StateMachineImpl.h | 4 +- .../StateMachine/src/StateMachineMakePtr.cpp | 4 +- .../StateMachine/src/StateMachineMakePtr.h | 4 +- utils/Config/include/Config.h | 2 +- utils/Config/src/Config.c | 2 +- utils/Config/src/ConfigCode.c | 2 +- utils/Config/src/ConfigCode.h | 2 +- utils/Config/src/ConfigImpl.c | 2 +- utils/Config/src/ConfigImpl.h | 2 +- utils/StatusCode/include/StatusCode.h | 2 +- utils/StatusCode/src/StatusCode.c | 2 +- 43 files changed, 642 insertions(+), 49 deletions(-) create mode 100644 application/CMakeLists.txt create mode 100644 application/MissionManager/CMakeLists.txt create mode 100644 application/MissionManager/include/IMissionManager.h create mode 100644 application/MissionManager/src/DataProcessing.cpp create mode 100644 application/MissionManager/src/DataProcessing.h create mode 100644 application/MissionManager/src/IMissionManager.cpp create mode 100644 application/MissionManager/src/MissionManager.cpp create mode 100644 application/MissionManager/src/MissionManager.h create mode 100644 application/MissionManager/src/MissionManagerMakeImpl.cpp create mode 100644 application/MissionManager/src/MissionManagerMakeImpl.h create mode 100644 application/MissionManager/src/MissionStateMachine.cpp create mode 100644 application/MissionManager/src/MissionStateMachine.h create mode 100644 application/MissionManager/src/TopState.cpp create mode 100644 application/MissionManager/src/TopState.h create mode 100644 middleware/DeviceManager/CMakeLists.txt create mode 100644 middleware/DeviceManager/include/IDeviceManager.h create mode 100644 middleware/DeviceManager/src/IDeviceManager.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 856ea38..0c1e8a4 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -76,7 +76,7 @@ if(${TARGET_PLATFORM} MATCHES ${DEFINE_LINUX}) endif() # 添加编译目录 -# add_subdirectory(application) +add_subdirectory(application) add_subdirectory(middleware) add_subdirectory(utils) add_subdirectory(hal) diff --git a/application/CMakeLists.txt b/application/CMakeLists.txt new file mode 100644 index 0000000..a18ab47 --- /dev/null +++ b/application/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(MissionManager) \ No newline at end of file diff --git a/application/MissionManager/CMakeLists.txt b/application/MissionManager/CMakeLists.txt new file mode 100644 index 0000000..67c231f --- /dev/null +++ b/application/MissionManager/CMakeLists.txt @@ -0,0 +1,46 @@ + +include(${CMAKE_SOURCE_DIR}/build/global_config.cmake) +set(EXECUTABLE_OUTPUT_PATH ${EXEC_OUTPUT_PATH}) +set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH}) + +include_directories( + ./src + ./include + ${UTILS_SOURCE_PATH}/StatusCode/include + ${UTILS_SOURCE_PATH}/Log/include + ${MIDDLEWARE_SOURCE_PATH}/StateMachine/include + ${MIDDLEWARE_SOURCE_PATH}/DeviceManager/include +) +#do not rely on any other library +#link_directories( +#) + +set(CMAKE_AUTOMOC ON) +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +aux_source_directory(./src SRC_FILES) + +set(TARGET_NAME MissionManager) +add_library(${TARGET_NAME} STATIC ${SRC_FILES}) + +target_link_libraries(${TARGET_NAME} StatusCode Log) + +if ("${CLANG_TIDY_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 ${CMAKE_SOURCE_DIR_IPCSDK}/cmake-shell + WORKING_DIRECTORY ${APPLICATION_SOURCE_PATH}/MissionManager +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make MissionManager_code_check + WORKING_DIRECTORY ${PROJECT_ROOT_PATH}/cmake-shell/ +) +endif() \ 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 0000000..1b28260 --- /dev/null +++ b/application/MissionManager/include/IMissionManager.h @@ -0,0 +1,62 @@ +/* + * 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 +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) { return CreateStatusCode(STATUS_CODE_OK); } + virtual const StatusCode UnInit(void) { return CreateStatusCode(STATUS_CODE_OK); } +}; +bool CreateMissionManagerModule(void); +#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 0000000..30ed4b9 --- /dev/null +++ b/application/MissionManager/src/DataProcessing.cpp @@ -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. + */ +#include "DataProcessing.h" +#include "ILog.h" +const bool NOT_EXECUTED = false; +const bool EXECUTED = true; +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; +} \ 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 0000000..9e1b1c4 --- /dev/null +++ b/application/MissionManager/src/DataProcessing.h @@ -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. + */ +#ifndef DATAPROCESSING_H +#define DATAPROCESSING_H +#include "IStateMachine.h" +#include "IMissionManager.h" +#include +#include +using DataProcessingFunc = std::function; +enum class InternalStateEvent +{ + TEST = static_cast(MissionEvent::END), + END +}; +class MissionData : public VStateMachineData +{ +public: + MissionData(const std::shared_ptr &data) + : mMissionData(data) + { + } + virtual ~MissionData() = default; + const std::shared_ptr mMissionData; +}; +class DataProcessing +{ +public: + DataProcessing() = default; + virtual ~DataProcessing() = default; + bool EventHandle(VStateMachineData *msg); + +protected: + std::map mEventHandle; +}; +#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 0000000..f17c6da --- /dev/null +++ b/application/MissionManager/src/IMissionManager.cpp @@ -0,0 +1,33 @@ +/* + * 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; +} \ 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 0000000..854b372 --- /dev/null +++ b/application/MissionManager/src/MissionManager.cpp @@ -0,0 +1,14 @@ +/* + * 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. + */ \ 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 0000000..d6548b8 --- /dev/null +++ b/application/MissionManager/src/MissionManager.h @@ -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. + */ +#ifndef MISSIONMANAGER_H +#define MISSIONMANAGER_H +#include "IMissionManager.h" +class MissionManager : public IMissionManager +{ +public: + MissionManager() = default; + virtual ~MissionManager() = default; +}; +#endif \ No newline at end of file diff --git a/application/MissionManager/src/MissionManagerMakeImpl.cpp b/application/MissionManager/src/MissionManagerMakeImpl.cpp new file mode 100644 index 0000000..cc54e35 --- /dev/null +++ b/application/MissionManager/src/MissionManagerMakeImpl.cpp @@ -0,0 +1,34 @@ +/* + * 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 "MissionManagerMakeImpl.h" +#include "TopState.h" +std::shared_ptr &MissionManagerMakeImpl::GetInstance(std::shared_ptr *impl) +{ + static auto instance = std::make_shared(); + if (impl) + { + instance = *impl; + } + return instance; +} +const StatusCode MissionManagerMakeImpl::CreateMissionManagerInstance(std::shared_ptr &instance) +{ + return CreateStatusCode(STATUS_CODE_OK); +} +std::shared_ptr MissionManagerMakeImpl::CreateTopState(void) +{ + std::shared_ptr state = std::make_shared(); + return state; +} \ No newline at end of file diff --git a/application/MissionManager/src/MissionManagerMakeImpl.h b/application/MissionManager/src/MissionManagerMakeImpl.h new file mode 100644 index 0000000..a6cb99b --- /dev/null +++ b/application/MissionManager/src/MissionManagerMakeImpl.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 MISSIONMANAGERMAKEIMPL_H +#define MISSIONMANAGERMAKEIMPL_H +#include "IMissionManager.h" +#include "IStateMachine.h" +#include "StatusCode.h" +#include +class MissionManagerMakeImpl +{ +public: + MissionManagerMakeImpl() = default; + virtual ~MissionManagerMakeImpl() = 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); +}; +#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 0000000..fd29263 --- /dev/null +++ b/application/MissionManager/src/MissionStateMachine.cpp @@ -0,0 +1,52 @@ +/* + * 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 "ILog.h" +#include "TopState.h" +#include "MissionManagerMakeImpl.h" +std::shared_ptr &MissionStateMachine::GetInstance(std::shared_ptr *impl) +{ + static auto instance = std::make_shared(); + if (impl) + { + instance = *impl; + } + return instance; +} +void MissionStateMachine::Init(void) +{ + auto code = IStateMachine::GetInstance()->CreateStateMachine(mStateMachine); + if (IsCodeOK(code)) + { + LogError("Create state machine failed.\n"); + return; + } + if (!mStateMachine->InitialStateMachine()) + { + LogError("State machine init failed.\n"); + return; + } + mStartMission = IDeviceManager::GetInstance()->GetIpcMissiony(); + RunStateMachine(mStartMission); +} +void MissionStateMachine::UnInit(void) +{ +} +void MissionStateMachine::RunStateMachine(const IpcMission &mission) +{ + LogInfo("Make all states and start the state machine.\n"); + mStateTree[MissionState::TOP_STATE] = MissionManagerMakeImpl::GetInstance()->CreateTopState(); + mStateMachine->SetTopState(mStateTree[MissionState::TOP_STATE].get()); +} diff --git a/application/MissionManager/src/MissionStateMachine.h b/application/MissionManager/src/MissionStateMachine.h new file mode 100644 index 0000000..df1f603 --- /dev/null +++ b/application/MissionManager/src/MissionStateMachine.h @@ -0,0 +1,43 @@ +/* + * 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 MISSIONSTATEMACHINE_H +#define MISSIONSTATEMACHINE_H +#include "IStateMachine.h" +#include "IMissionManager.h" +#include "IDeviceManager.h" +#include +enum class MissionState +{ + TOP_STATE = 0, + END +}; +class MissionStateMachine +{ +public: + MissionStateMachine() = default; + virtual ~MissionStateMachine() = default; + static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); + void Init(void); + void UnInit(void); + +private: + 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/TopState.cpp b/application/MissionManager/src/TopState.cpp new file mode 100644 index 0000000..c733043 --- /dev/null +++ b/application/MissionManager/src/TopState.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 "TopState.h" +TopState::TopState() : State("TopState") +{ +} +void TopState::GoInState() +{ +} +void TopState::GoOutState() +{ +} +bool TopState::ExecuteStateMsg(VStateMachineData *msg) +{ + return DataProcessing::EventHandle(msg); +} \ 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 0000000..cbc36c5 --- /dev/null +++ b/application/MissionManager/src/TopState.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 TOPSTATE_H +#define TOPSTATE_H +#include "IStateMachine.h" +#include "DataProcessing.h" +class TopState + : public State, + public DataProcessing +{ +public: + TopState(); + virtual ~TopState() = default; + void GoInState() override; + void GoOutState() override; + bool ExecuteStateMsg(VStateMachineData *msg) override; +}; +#endif \ No newline at end of file diff --git a/doc/design.md b/doc/design.md index 789503e..4337ec0 100644 --- a/doc/design.md +++ b/doc/design.md @@ -187,7 +187,7 @@ participant app    通过构建配置文件选择需要实例化的网络服务模块代码。 -##### 1.4.3.1.3. 相机状态管理 +##### 1.4.3.1.3. 相机任务管理    相机主业务逻辑使用状态机机制进行管理。 @@ -206,9 +206,9 @@ participant app    一些相对中性的业务功能库,这些库可以提供给不同的产品需求使用,在应用层不同的调用方式可实现不同的产品功能。中间件只能被应用层调用或者向下调用适配层或者调用工具库,中间件各模块之间不能互相调用。中间件库接口可以使用C或者C++接口。 -##### 1.4.3.2.2. 外设管理模块 +##### 1.4.3.2.2. 设备管理模块 -   应用层唯一的硬件外设接口库。包含灯 / 按键 / GPIO / SD卡等。 +   设备,统指Linux的设备节点,应用层唯一的硬件设备接口库。包含灯 / 按键 / GPIO / SD卡 / 串口 / USB等。 ##### 1.4.3.2.3. 相机管理模块 diff --git a/middleware/CMakeLists.txt b/middleware/CMakeLists.txt index 96cf763..4eb09b7 100644 --- a/middleware/CMakeLists.txt +++ b/middleware/CMakeLists.txt @@ -1,2 +1,3 @@ add_subdirectory(StateMachine) -add_subdirectory(IpcConfig) \ No newline at end of file +add_subdirectory(IpcConfig) +add_subdirectory(DeviceManager) \ No newline at end of file diff --git a/middleware/DeviceManager/CMakeLists.txt b/middleware/DeviceManager/CMakeLists.txt new file mode 100644 index 0000000..ba8dbb0 --- /dev/null +++ b/middleware/DeviceManager/CMakeLists.txt @@ -0,0 +1,45 @@ + +include(${CMAKE_SOURCE_DIR}/build/global_config.cmake) +set(EXECUTABLE_OUTPUT_PATH ${EXEC_OUTPUT_PATH}) +set(LIBRARY_OUTPUT_PATH ${LIBS_OUTPUT_PATH}) + +include_directories( + ./src + ./include + ${UTILS_SOURCE_PATH}/StatusCode/include + ${UTILS_SOURCE_PATH}/Log/include + ${UTILS_SOURCE_PATH}/Config/include +) +#do not rely on any other library +#link_directories( +#) + +set(CMAKE_AUTOMOC ON) +set(CMAKE_INCLUDE_CURRENT_DIR ON) + +aux_source_directory(./src SRC_FILES) + +set(TARGET_NAME DeviceManager) +add_library(${TARGET_NAME} STATIC ${SRC_FILES}) + +target_link_libraries(${TARGET_NAME} StatusCode Log) + +if ("${CLANG_TIDY_SUPPORT}" MATCHES "true") +add_custom_target( + DeviceManager_code_check + COMMAND ${CLANG_TIDY_EXE} + -checks='${CLANG_TIDY_CHECKS}' + --header-filter=.* + --system-headers=false + ${SRC_FILES} + ${CLANG_TIDY_CONFIG} + -p ${CMAKE_SOURCE_DIR_IPCSDK}/cmake-shell + WORKING_DIRECTORY ${MIDDLEWARE_SOURCE_PATH}/DeviceManager +) +add_custom_command( + TARGET ${TARGET_NAME} + PRE_BUILD + COMMAND make DeviceManager_code_check + WORKING_DIRECTORY ${PROJECT_ROOT_PATH}/cmake-shell/ +) +endif() \ No newline at end of file diff --git a/middleware/DeviceManager/include/IDeviceManager.h b/middleware/DeviceManager/include/IDeviceManager.h new file mode 100644 index 0000000..b2b930e --- /dev/null +++ b/middleware/DeviceManager/include/IDeviceManager.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 IDEVICEMANAGER_H +#define IDEVICEMANAGER_H +#include "StatusCode.h" +#include +enum class IpcMission +{ + TEST = 0, + END +}; +class IDeviceManager +{ +public: + IDeviceManager() = default; + virtual ~IDeviceManager() = default; + static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); + virtual const StatusCode Init(void) { return CreateStatusCode(STATUS_CODE_OK); } + virtual const StatusCode UnInit(void) { return CreateStatusCode(STATUS_CODE_OK); } + virtual const IpcMission GetIpcMissiony(void) { return IpcMission::END; } +}; +bool CreateDeviceManagerModule(void); +#endif \ No newline at end of file diff --git a/middleware/DeviceManager/src/IDeviceManager.cpp b/middleware/DeviceManager/src/IDeviceManager.cpp new file mode 100644 index 0000000..416b30b --- /dev/null +++ b/middleware/DeviceManager/src/IDeviceManager.cpp @@ -0,0 +1,33 @@ +/* + * 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 "IDeviceManager.h" +#include "ILog.h" +std::shared_ptr &IDeviceManager::GetInstance(std::shared_ptr *impl) +{ + static auto instance = std::make_shared(); + if (impl) + { + if (instance.use_count() == 1) + { + LogInfo("Instance changed succeed.\n"); + instance = *impl; + } + else + { + LogError("Can't changing the instance becase of using by some one.\n"); + } + } + return instance; +} \ No newline at end of file diff --git a/middleware/IpcConfig/include/IIpcConfig.h b/middleware/IpcConfig/include/IIpcConfig.h index 4d88f56..6904321 100644 --- a/middleware/IpcConfig/include/IIpcConfig.h +++ b/middleware/IpcConfig/include/IIpcConfig.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 JIUYILIAN Co., Ltd. + * 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 diff --git a/middleware/IpcConfig/src/IIpcConfig.cpp b/middleware/IpcConfig/src/IIpcConfig.cpp index 2e63d28..41ee2de 100644 --- a/middleware/IpcConfig/src/IIpcConfig.cpp +++ b/middleware/IpcConfig/src/IIpcConfig.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 JIUYILIAN Co., Ltd. + * 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 diff --git a/middleware/IpcConfig/src/IpcConfig.cpp b/middleware/IpcConfig/src/IpcConfig.cpp index 543665d..bfed7d6 100644 --- a/middleware/IpcConfig/src/IpcConfig.cpp +++ b/middleware/IpcConfig/src/IpcConfig.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 JIUYILIAN Co., Ltd. + * 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 diff --git a/middleware/IpcConfig/src/IpcConfig.h b/middleware/IpcConfig/src/IpcConfig.h index 6c6c373..ee37af0 100644 --- a/middleware/IpcConfig/src/IpcConfig.h +++ b/middleware/IpcConfig/src/IpcConfig.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 JIUYILIAN Co., Ltd. + * 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 diff --git a/middleware/IpcConfig/src/IpcConfigMakePtr.cpp b/middleware/IpcConfig/src/IpcConfigMakePtr.cpp index d1e5d7a..de87cfb 100644 --- a/middleware/IpcConfig/src/IpcConfigMakePtr.cpp +++ b/middleware/IpcConfig/src/IpcConfigMakePtr.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 JIUYILIAN Co., Ltd. + * 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 diff --git a/middleware/IpcConfig/src/IpcConfigMakePtr.h b/middleware/IpcConfig/src/IpcConfigMakePtr.h index 28b750f..36c737a 100644 --- a/middleware/IpcConfig/src/IpcConfigMakePtr.h +++ b/middleware/IpcConfig/src/IpcConfigMakePtr.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 JIUYILIAN Co., Ltd. + * 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 diff --git a/middleware/StateMachine/include/IStateMachine.h b/middleware/StateMachine/include/IStateMachine.h index 120fc0b..df42cf7 100644 --- a/middleware/StateMachine/include/IStateMachine.h +++ b/middleware/StateMachine/include/IStateMachine.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 JIUYILIAN Co., Ltd. + * 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 @@ -22,11 +22,11 @@ public: VStateMessage() = default; virtual ~VStateMessage() = default; }; -class VStateMachineParam +class VStateMachineData { public: - VStateMachineParam() = default; - virtual ~VStateMachineParam() = default; + VStateMachineData() = default; + virtual ~VStateMachineData() = default; virtual int GetMessageName() const = 0; virtual const std::shared_ptr &GetMessageObj(void) const = 0; }; @@ -39,20 +39,20 @@ public: public: virtual void GoInState() = 0; virtual void GoOutState() = 0; - virtual bool ExecuteStateMsg(VStateMachineParam *msg) = 0; + virtual bool ExecuteStateMsg(VStateMachineData *msg) = 0; std::string GetStateName() { return mStateName; } private: std::string mStateName; }; -class VStateMachine +class VStateMachineHandle { public: - VStateMachine() = default; - virtual ~VStateMachine() = default; + VStateMachineHandle() = default; + virtual ~VStateMachineHandle() = default; virtual bool InitialStateMachine() { return false; } virtual void StatePlus(State *state, State *upper) {} - virtual void SetFirstState(State *firstState) {} + virtual void SetTopState(State *firstState) {} virtual void StartStateMachine() {} virtual void SendMessage(int msgName) {} virtual void StopHandlerThread() {} @@ -60,7 +60,7 @@ public: virtual void MessageExecutedLater(int msgName, const std::shared_ptr &messageObj, int64_t delayTimeMs) {} virtual void SwitchState(State *targetState) {} virtual void StopTimer(int timerName) {} - virtual void DelayMessage(VStateMachineParam *msg) {} + virtual void DelayMessage(VStateMachineData *msg) {} }; class IStateMachine { @@ -68,7 +68,7 @@ public: IStateMachine() = default; virtual ~IStateMachine() = default; static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); - virtual const StatusCode CreateStateMachine(std::shared_ptr &stateMachine) + virtual const StatusCode CreateStateMachine(std::shared_ptr &stateMachine) { return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION); } diff --git a/middleware/StateMachine/src/IStateMachine.cpp b/middleware/StateMachine/src/IStateMachine.cpp index 951aa91..dbfa643 100644 --- a/middleware/StateMachine/src/IStateMachine.cpp +++ b/middleware/StateMachine/src/IStateMachine.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 JIUYILIAN Co., Ltd. + * 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 diff --git a/middleware/StateMachine/src/OpenHarmony/internal_message.h b/middleware/StateMachine/src/OpenHarmony/internal_message.h index 1972290..feb740f 100644 --- a/middleware/StateMachine/src/OpenHarmony/internal_message.h +++ b/middleware/StateMachine/src/OpenHarmony/internal_message.h @@ -76,7 +76,7 @@ private: std::list stringArray_; }; -class InternalMessage : public VStateMachineParam { +class InternalMessage : public VStateMachineData { public: /** * @Description : Construct a new Internal Message object. diff --git a/middleware/StateMachine/src/OpenHarmony/state_machine.cpp b/middleware/StateMachine/src/OpenHarmony/state_machine.cpp index 0bce11f..e06e236 100644 --- a/middleware/StateMachine/src/OpenHarmony/state_machine.cpp +++ b/middleware/StateMachine/src/OpenHarmony/state_machine.cpp @@ -95,14 +95,14 @@ void StateMachine::StateDelete(State *state) pStateMachineHandler->StateDelete(state); } -void StateMachine::SetFirstState(State *firstState) +void StateMachine::SetTopState(State *firstState) { if (pStateMachineHandler == nullptr) { LogError("Start StateMachine failed, pStateMachineHandler is nullptr!\n"); return; } - pStateMachineHandler->SetFirstState(firstState); + pStateMachineHandler->SetTopState(firstState); } void StateMachine::SwitchState(State *targetState) @@ -124,7 +124,7 @@ void StateMachine::DelayMessage(const InternalMessage *msg) pStateMachineHandler->DelayMessage(msg); } -void StateMachine::DelayMessage(VStateMachineParam *msg) +void StateMachine::DelayMessage(VStateMachineData *msg) { if (pStateMachineHandler == nullptr) { LogError("Start StateMachine failed, pStateMachineHandler is nullptr!\n"); @@ -382,7 +382,7 @@ void StateMachineHandler::StateDelete(State *state) } } -void StateMachineHandler::SetFirstState(State *firstState) +void StateMachineHandler::SetTopState(State *firstState) { pFirstState = firstState; } @@ -638,7 +638,7 @@ State *StateMachineHandler::ExecuteTreeStateMsg(InternalMessage *msg) curStateInfo->state->GetStateName().c_str(), msg->GetMessageName()); } // TODO: - while (curStateInfo->state && (!curStateInfo->state->ExecuteStateMsg((VStateMachineParam *)(msg)))) { + while (curStateInfo->state && (!curStateInfo->state->ExecuteStateMsg((VStateMachineData *)(msg)))) { curStateInfo = curStateInfo->upperStateInfo; if (curStateInfo == nullptr) { diff --git a/middleware/StateMachine/src/OpenHarmony/state_machine.h b/middleware/StateMachine/src/OpenHarmony/state_machine.h index df0afc0..c73aae6 100644 --- a/middleware/StateMachine/src/OpenHarmony/state_machine.h +++ b/middleware/StateMachine/src/OpenHarmony/state_machine.h @@ -28,7 +28,7 @@ #define CMD_SET_OPERATIONAL_MODE 1 class StateMachineHandler; -class StateMachine : public VStateMachine +class StateMachine : public VStateMachineHandle { public: /** @@ -295,7 +295,7 @@ protected: * * @param firstState - First state.[in] */ - void SetFirstState(State *firstState) override; + void SetTopState(State *firstState) override; /** * @Description : Transition to orther state. @@ -310,7 +310,7 @@ protected: * @param msg - Message object.[in] */ void DelayMessage(const InternalMessage *msg); - void DelayMessage(VStateMachineParam *msg) override; + void DelayMessage(VStateMachineData *msg) override; private: StateMachineHandler *pStateMachineHandler; @@ -372,7 +372,7 @@ public: * * @param firstState - Initialization State.[in] */ - void SetFirstState(State *firstState); + void SetTopState(State *firstState); /** * @Description : State transition function. diff --git a/middleware/StateMachine/src/StateMachineImpl.cpp b/middleware/StateMachine/src/StateMachineImpl.cpp index 3443938..3b6d14b 100644 --- a/middleware/StateMachine/src/StateMachineImpl.cpp +++ b/middleware/StateMachine/src/StateMachineImpl.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 JIUYILIAN Co., Ltd. + * 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 @@ -14,7 +14,7 @@ */ #include "StateMachineImpl.h" #include "StateMachineMakePtr.h" -const StatusCode StateMachineImpl::CreateStateMachine(std::shared_ptr &stateMachine) +const StatusCode StateMachineImpl::CreateStateMachine(std::shared_ptr &stateMachine) { return StateMachineMakePtr::GetInstance()->CreateStateMachine(stateMachine); } \ No newline at end of file diff --git a/middleware/StateMachine/src/StateMachineImpl.h b/middleware/StateMachine/src/StateMachineImpl.h index 46fd52e..12d48af 100644 --- a/middleware/StateMachine/src/StateMachineImpl.h +++ b/middleware/StateMachine/src/StateMachineImpl.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 JIUYILIAN Co., Ltd. + * 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 @@ -20,6 +20,6 @@ class StateMachineImpl : public IStateMachine public: StateMachineImpl() = default; ~StateMachineImpl() = default; - const StatusCode CreateStateMachine(std::shared_ptr &stateMachine) override; + const StatusCode CreateStateMachine(std::shared_ptr &stateMachine) override; }; #endif \ No newline at end of file diff --git a/middleware/StateMachine/src/StateMachineMakePtr.cpp b/middleware/StateMachine/src/StateMachineMakePtr.cpp index f15ca56..98e4227 100644 --- a/middleware/StateMachine/src/StateMachineMakePtr.cpp +++ b/middleware/StateMachine/src/StateMachineMakePtr.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 JIUYILIAN Co., Ltd. + * 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 @@ -39,7 +39,7 @@ std::shared_ptr &StateMachineMakePtr::GetInstance(std::shar } return instance; } -StatusCode StateMachineMakePtr::CreateStateMachine(std::shared_ptr &stateMachine) +StatusCode StateMachineMakePtr::CreateStateMachine(std::shared_ptr &stateMachine) { // TODO: need a name ? auto tmp = std::make_shared("TODO"); diff --git a/middleware/StateMachine/src/StateMachineMakePtr.h b/middleware/StateMachine/src/StateMachineMakePtr.h index 978ca1a..7a8d420 100644 --- a/middleware/StateMachine/src/StateMachineMakePtr.h +++ b/middleware/StateMachine/src/StateMachineMakePtr.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 JIUYILIAN Co., Ltd. + * 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 @@ -23,7 +23,7 @@ public: StateMachineMakePtr() = default; virtual ~StateMachineMakePtr() = default; static std::shared_ptr &GetInstance(std::shared_ptr *impl = nullptr); - virtual StatusCode CreateStateMachine(std::shared_ptr &stateMachine); + virtual StatusCode CreateStateMachine(std::shared_ptr &stateMachine); virtual StatusCode CreateStateMachine(std::shared_ptr &instance); }; #endif \ No newline at end of file diff --git a/utils/Config/include/Config.h b/utils/Config/include/Config.h index aa0984f..e67fe6e 100644 --- a/utils/Config/include/Config.h +++ b/utils/Config/include/Config.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 JIUYILIAN Co., Ltd. + * 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 diff --git a/utils/Config/src/Config.c b/utils/Config/src/Config.c index f1dd6d9..a35660d 100644 --- a/utils/Config/src/Config.c +++ b/utils/Config/src/Config.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 JIUYILIAN Co., Ltd. + * 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 diff --git a/utils/Config/src/ConfigCode.c b/utils/Config/src/ConfigCode.c index faa2b64..5f7ce00 100644 --- a/utils/Config/src/ConfigCode.c +++ b/utils/Config/src/ConfigCode.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 JIUYILIAN Co., Ltd. + * 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 diff --git a/utils/Config/src/ConfigCode.h b/utils/Config/src/ConfigCode.h index e014d14..4ce6510 100644 --- a/utils/Config/src/ConfigCode.h +++ b/utils/Config/src/ConfigCode.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 JIUYILIAN Co., Ltd. + * 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 diff --git a/utils/Config/src/ConfigImpl.c b/utils/Config/src/ConfigImpl.c index 1d9a143..28f1223 100644 --- a/utils/Config/src/ConfigImpl.c +++ b/utils/Config/src/ConfigImpl.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 JIUYILIAN Co., Ltd. + * 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 diff --git a/utils/Config/src/ConfigImpl.h b/utils/Config/src/ConfigImpl.h index d1f50e2..871aa22 100644 --- a/utils/Config/src/ConfigImpl.h +++ b/utils/Config/src/ConfigImpl.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 JIUYILIAN Co., Ltd. + * 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 diff --git a/utils/StatusCode/include/StatusCode.h b/utils/StatusCode/include/StatusCode.h index 69f3d9f..50a1e90 100644 --- a/utils/StatusCode/include/StatusCode.h +++ b/utils/StatusCode/include/StatusCode.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 JIUYILIAN Co., Ltd. + * 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 diff --git a/utils/StatusCode/src/StatusCode.c b/utils/StatusCode/src/StatusCode.c index f07b806..2ab2fa2 100644 --- a/utils/StatusCode/src/StatusCode.c +++ b/utils/StatusCode/src/StatusCode.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 JIUYILIAN Co., Ltd. + * 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