From dd0f1b0895877eef47ce086bf4e490147e15e47c Mon Sep 17 00:00:00 2001 From: Fancy code <258828110.@qq.com> Date: Tue, 16 Jul 2024 16:25:31 +0800 Subject: [PATCH] Add:FormattingState. --- application/MissionManager/README.md | 3 +- .../MissionManager/src/FormattingState.cpp | 70 +++++++++++++++++++ .../MissionManager/src/FormattingState.h | 46 ++++++++++++ .../src/MissionManagerMakePtr.cpp | 6 ++ .../src/MissionManagerMakePtr.h | 1 + .../src/MissionStateMachine.cpp | 9 +-- .../MissionManager/src/MissionStateMachine.h | 1 + .../MissionManager/src/SdCardHandleState.cpp | 11 ++- .../MissionManager/src/TestMissionState.cpp | 1 + middleware/AppManager/src/AppManager.cpp | 2 + .../src_mock/AppMonitor_Mock_Test.cpp | 16 +++++ .../src_mock/DeviceManager_Mock_Test.cpp | 2 +- .../tool/src/AppManagerTestTool.cpp | 5 +- 13 files changed, 161 insertions(+), 12 deletions(-) create mode 100644 application/MissionManager/src/FormattingState.cpp create mode 100644 application/MissionManager/src/FormattingState.h diff --git a/application/MissionManager/README.md b/application/MissionManager/README.md index a68c1a9..d08c414 100644 --- a/application/MissionManager/README.md +++ b/application/MissionManager/README.md @@ -21,8 +21,9 @@ stateDiagram-v2 [*] --> TopState TopState --> PowerOff TopState --> MSDCState -TopState --> DeviceAbnormal TopState --> MissionState +TopState --> DeviceAbnormal +TopState --> FormattingSDCard MissionState --> 空闲 MissionState --> 存储管理 存储管理 --> EMMC diff --git a/application/MissionManager/src/FormattingState.cpp b/application/MissionManager/src/FormattingState.cpp new file mode 100644 index 0000000..7d0dc74 --- /dev/null +++ b/application/MissionManager/src/FormattingState.cpp @@ -0,0 +1,70 @@ +/* + * 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 "FormattingState.h" +#include "DataProcessing.h" +#include "IFilesManager.h" +#include "ILog.h" +#include "IStateMachine.h" +#include "IStorageManager.h" +#include "MissionStateMachine.h" +#include +#include +using std::placeholders::_1; +FormattingState::FormattingState() : State("FormattingState") +{ + mEventHandle[InternalStateEvent::FORMAT_KEY_FORMAT_SD_CARD] = + std::bind(&FormattingState::FormatKeyFormattingSDCardHandle, this, _1); +} +void FormattingState::GoInState() +{ + LogInfo(" ========== FormattingState::GoInState.\n"); +} +void FormattingState::GoOutState() +{ + LogInfo(" ========== FormattingState::GoOutState.\n"); + if (mFormattingThread.joinable()) { + mFormattingThread.join(); + } +} +bool FormattingState::ExecuteStateMsg(VStateMachineData *msg) +{ + return DataProcessing::EventHandle(msg); +} +void FormattingState::StateInit(void) +{ +} +void FormattingState::StateUnInit(void) +{ + if (mFormattingThread.joinable()) { + mFormattingThread.join(); + } +} +bool FormattingState::FormatKeyFormattingSDCardHandle(VStateMachineData *msg) +{ + LogInfo("Now formatting SD card.\n"); + auto formatting = [](std::shared_ptr impl) { + impl->FormattingThread(); + }; + mFormattingThread = std::thread(formatting, shared_from_this()); + return EXECUTED; +} +void FormattingState::FormattingThread(void) +{ + IFilesManager::GetInstance()->UnInit(); + IStorageManager::GetInstance()->FormatSDCardNow(); + // IFilesManager::GetInstance()->Init(); + MissionStateMachine::GetInstance()->SwitchState(SystemState::STORAGE_HANDLE_STATE); + LogInfo("Formatting SD card done.\n"); +} \ No newline at end of file diff --git a/application/MissionManager/src/FormattingState.h b/application/MissionManager/src/FormattingState.h new file mode 100644 index 0000000..cf5da93 --- /dev/null +++ b/application/MissionManager/src/FormattingState.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 FORMATTING_STATE_H +#define FORMATTING_STATE_H +#include "DataProcessing.h" +#include "IFilesManager.h" +#include "IMediaManager.h" +#include "IStateMachine.h" +#include "IStorageManager.h" +#include "VStateBase.h" +#include +class FormattingState : public State, + public DataProcessing, + public VStateBase, + public std::enable_shared_from_this +{ +public: + FormattingState(); + virtual ~FormattingState() = default; + void GoInState() override; + void GoOutState() override; + bool ExecuteStateMsg(VStateMachineData *msg) override; + +protected: + void StateInit(void) override; + void StateUnInit(void) override; + bool FormatKeyFormattingSDCardHandle(VStateMachineData *msg); + void FormattingThread(void); + +private: + std::thread mFormattingThread; +}; + +#endif \ No newline at end of file diff --git a/application/MissionManager/src/MissionManagerMakePtr.cpp b/application/MissionManager/src/MissionManagerMakePtr.cpp index 13a53e0..dff2fa5 100644 --- a/application/MissionManager/src/MissionManagerMakePtr.cpp +++ b/application/MissionManager/src/MissionManagerMakePtr.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ #include "MissionManagerMakePtr.h" +#include "FormattingState.h" #include "ILog.h" #include "IMcuManager.h" #include "IMissionManager.h" @@ -120,4 +121,9 @@ std::shared_ptr MissionManagerMakePtr::CreateIdleState(void) { std::shared_ptr state = std::make_shared(); return state; +} +std::shared_ptr MissionManagerMakePtr::CreateFormattingState(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 index 6bc2ded..534a923 100644 --- a/application/MissionManager/src/MissionManagerMakePtr.h +++ b/application/MissionManager/src/MissionManagerMakePtr.h @@ -33,5 +33,6 @@ public: virtual std::shared_ptr CreateUpgradeState(void); virtual std::shared_ptr CreateMediaHandleState(void); virtual std::shared_ptr CreateIdleState(void); + virtual std::shared_ptr CreateFormattingState(void); }; #endif \ No newline at end of file diff --git a/application/MissionManager/src/MissionStateMachine.cpp b/application/MissionManager/src/MissionStateMachine.cpp index e79ad38..4aebffb 100644 --- a/application/MissionManager/src/MissionStateMachine.cpp +++ b/application/MissionManager/src/MissionStateMachine.cpp @@ -108,8 +108,10 @@ void MissionStateMachine::RunStateMachine(const IpcMission &mission) mStateTree[SystemState::UPGRADE_STATE] = MissionManagerMakePtr::GetInstance()->CreateUpgradeState(); mStateTree[SystemState::MEDIA_HANDLE_STATE] = MissionManagerMakePtr::GetInstance()->CreateMediaHandleState(); mStateTree[SystemState::IDLE_STATE] = MissionManagerMakePtr::GetInstance()->CreateIdleState(); + mStateTree[SystemState::FORMATTING_STATE] = MissionManagerMakePtr::GetInstance()->CreateFormattingState(); mStateMachine->StatePlus(mStateTree[SystemState::TOP_STATE].get(), nullptr); mStateMachine->StatePlus(mStateTree[SystemState::MISSION_STATE].get(), mStateTree[SystemState::TOP_STATE].get()); + mStateMachine->StatePlus(mStateTree[SystemState::FORMATTING_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(), @@ -121,13 +123,6 @@ void MissionStateMachine::RunStateMachine(const IpcMission &mission) 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); InitAllState(); } void MissionStateMachine::InitAllState(void) diff --git a/application/MissionManager/src/MissionStateMachine.h b/application/MissionManager/src/MissionStateMachine.h index 12fd0b2..5eb1233 100644 --- a/application/MissionManager/src/MissionStateMachine.h +++ b/application/MissionManager/src/MissionStateMachine.h @@ -33,6 +33,7 @@ enum class SystemState UPGRADE_STATE, MEDIA_HANDLE_STATE, IDLE_STATE, + FORMATTING_STATE, END }; class MissionStateMachine diff --git a/application/MissionManager/src/SdCardHandleState.cpp b/application/MissionManager/src/SdCardHandleState.cpp index 035cb22..0546b45 100644 --- a/application/MissionManager/src/SdCardHandleState.cpp +++ b/application/MissionManager/src/SdCardHandleState.cpp @@ -112,11 +112,20 @@ bool SdCardHandleState::ResetKeyMediaTaskHandle(VStateMachineData *msg) LogWarning("Sd card is not inserted, ignore reset key media task.\n"); return HANDLE_HELPESS; } +/** + * @brief Why do we need to switch status and go through the formatting process here? + * This is because the guarantee business requires that the SD card be formatted safely under the condition that the SD + * card is not occupied. + * @param msg + * @return true + * @return false + */ bool SdCardHandleState::FormatKeyFormattingSDCardHandle(VStateMachineData *msg) { if (StorageEvent::SD_CARD_INSERT == mSdCardStatus) { LogInfo("FormatKeyFormattingSDCardHandle.\n"); - IStorageManager::GetInstance()->FormatSDCardNow(); + MissionStateMachine::GetInstance()->DelayMessage(msg); + MissionStateMachine::GetInstance()->SwitchState(SystemState::FORMATTING_STATE); return EXECUTED; } LogWarning("Sd card is not inserted, ignore format key.\n"); diff --git a/application/MissionManager/src/TestMissionState.cpp b/application/MissionManager/src/TestMissionState.cpp index d498b6a..eacf40c 100644 --- a/application/MissionManager/src/TestMissionState.cpp +++ b/application/MissionManager/src/TestMissionState.cpp @@ -47,6 +47,7 @@ void TestMissionState::GoInState() } void TestMissionState::GoOutState() { + IAppManager::GetInstance()->UnInit(); MissionState::GoOutState(); LogInfo(" ========== TestMissionState::GoOutState.\n"); } diff --git a/middleware/AppManager/src/AppManager.cpp b/middleware/AppManager/src/AppManager.cpp index d16a1f8..3e81aa8 100644 --- a/middleware/AppManager/src/AppManager.cpp +++ b/middleware/AppManager/src/AppManager.cpp @@ -60,6 +60,7 @@ GOAHEAD: std::shared_ptr protocolHandle = std::make_shared(); IAppProtocolHandle::GetInstance(&protocolHandle); mAppMonitor.reset(); + LogInfo("AppManager uninit success.\n"); return CreateStatusCode(STATUS_CODE_OK); } const StatusCode AppManager::SetAppMonitor(std::shared_ptr &monitor) @@ -198,6 +199,7 @@ void AppManager::TcpServerStop(void) { if (nullptr != mTcpServer) { FreeTcpServer(mTcpServer); + mTcpServer = nullptr; } } void AppManager::WifiApModeInit(const AppParam ¶m) diff --git a/test/application/HuntingCamera/src_mock/AppMonitor_Mock_Test.cpp b/test/application/HuntingCamera/src_mock/AppMonitor_Mock_Test.cpp index 71d0a56..cc3d894 100644 --- a/test/application/HuntingCamera/src_mock/AppMonitor_Mock_Test.cpp +++ b/test/application/HuntingCamera/src_mock/AppMonitor_Mock_Test.cpp @@ -249,4 +249,20 @@ TEST_F(HuntingCameraTest, INTEGRATION_HunttingCamera_AUTO_AppPlayback) MockAppPlayback(); MainThread::GetInstance()->Runing(); } +/** + * @brief Construct a new test f object + * ../output_files/test/bin/HuntingCameraTest + * --gtest_filter=HuntingCameraTest.HS_INTEGRATION_HunttingCamera_EXAMPLE_ReInitAppManager + */ +TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_ReInitAppManager) +{ + SetAllCamerasResult(mAllCamerasMock); + MockOtherSideIpcMissionReply(IpcMission::TEST); + MainThread::GetInstance()->Init(); + TestManager::ResetTimeOut(1000 * 20); + HalTestTool::MockKeyClick("format", 1000 * 18); // Simulate pressing a button. + std::this_thread::sleep_for(std::chrono::milliseconds(1000 * 17)); + MockGetSdCardInfo(); + MainThread::GetInstance()->Runing(); +} } // namespace AppMonitor_Mock_Test \ No newline at end of file diff --git a/test/application/HuntingCamera/src_mock/DeviceManager_Mock_Test.cpp b/test/application/HuntingCamera/src_mock/DeviceManager_Mock_Test.cpp index dcb1b9f..e6f8d1d 100644 --- a/test/application/HuntingCamera/src_mock/DeviceManager_Mock_Test.cpp +++ b/test/application/HuntingCamera/src_mock/DeviceManager_Mock_Test.cpp @@ -76,7 +76,7 @@ TEST_F(HuntingCameraTest, HS_INTEGRATION_HunttingCamera_EXAMPLE_FormatSDCard) MockOtherSideIpcMissionReply(IpcMission::TEST); MainThread::GetInstance()->Init(); TestManager::ResetTimeOut(1000 * 17); - HalTestTool::MockKeyClick("format", 1000 * 16); // Simulate pressing a button. + HalTestTool::MockKeyClick("format", 1000 * 18); // Simulate pressing a button. std::this_thread::sleep_for(std::chrono::milliseconds(100)); MainThread::GetInstance()->Runing(); } diff --git a/test/middleware/AppManager/tool/src/AppManagerTestTool.cpp b/test/middleware/AppManager/tool/src/AppManagerTestTool.cpp index 8610704..542dc09 100644 --- a/test/middleware/AppManager/tool/src/AppManagerTestTool.cpp +++ b/test/middleware/AppManager/tool/src/AppManagerTestTool.cpp @@ -322,8 +322,9 @@ void AppManagerTestTool::AppManagerMockInit(std::shared_ptr &vMock) AppManagerTestTool::AppMonitorInit(mAppMonitorMock); }; EXPECT_CALL(*mock.get(), SetAppMonitorTrace(_)) - .Times(Between(0, 1)) - .WillOnce(DoAll(WithArgs<0>(Invoke(getAppMonitor)), Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); + .Times(AtLeast(1)) + .WillRepeatedly( + DoAll(WithArgs<0>(Invoke(getAppMonitor)), Return(CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION)))); } std::shared_ptr AppManagerTestTool::MakeMonitorMock(void) {