/* * 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); }