1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "dsched_continue_state_machine.h"
17 
18 #include "dsched_continue.h"
19 #include "dtbschedmgr_log.h"
20 #include "sink_state/dsched_continue_data_state.h"
21 #include "sink_state/dsched_continue_sink_end_state.h"
22 #include "sink_state/dsched_continue_sink_start_state.h"
23 #include "sink_state/dsched_continue_sink_wait_end_state.h"
24 #include "source_state/dsched_continue_ability_state.h"
25 #include "source_state/dsched_continue_source_end_state.h"
26 #include "source_state/dsched_continue_source_start_state.h"
27 #include "source_state/dsched_continue_source_wait_end_state.h"
28 
29 namespace OHOS {
30 namespace DistributedSchedule {
31 namespace {
32 const std::string TAG = "DSchedContinueStateMachine";
33 }
DSchedContinueStateMachine(std::shared_ptr<DSchedContinue> dContinue)34 DSchedContinueStateMachine::DSchedContinueStateMachine(std::shared_ptr<DSchedContinue> dContinue)
35     : dContinue_(dContinue)
36 {
37 }
38 
~DSchedContinueStateMachine()39 DSchedContinueStateMachine::~DSchedContinueStateMachine()
40 {
41 }
42 
Execute(const AppExecFwk::InnerEvent::Pointer & event)43 int32_t DSchedContinueStateMachine::Execute(const AppExecFwk::InnerEvent::Pointer &event)
44 {
45     std::shared_ptr<DSchedContinue> dContinue = dContinue_.lock();
46     if (dContinue == nullptr || currentState_ == nullptr) {
47         HILOGE("DSchedContinueStateMachine excute failed, continue or currentState is null");
48         return INVALID_PARAMETERS_ERR;
49     }
50 
51     auto state = currentState_;
52     int32_t ret = state->Execute(dContinue, event);
53     if (ret != ERR_OK) {
54         HILOGE("DSchedContinueStateMachine currentState: %{public}d excute event %{public}d failed, ret %{public}d",
55             state->GetStateType(), event->GetInnerEventId(), ret);
56     }
57     return ret;
58 }
59 
UpdateState(DSchedContinueStateType stateType)60 void DSchedContinueStateMachine::UpdateState(DSchedContinueStateType stateType)
61 {
62     if (stateType != DSCHED_CONTINUE_SOURCE_START_STATE && stateType != DSCHED_CONTINUE_SINK_START_STATE) {
63         HILOGI("DSchedContinueStateMachine update state from %{public}d to %{public}d",
64             currentState_->GetStateType(), stateType);
65     } else {
66         HILOGI("DSchedContinueStateMachine update state %{public}d", stateType);
67     }
68     currentState_ = CreateState(stateType);
69     return;
70 }
71 
CreateState(DSchedContinueStateType stateType)72 std::shared_ptr<DSchedContinueState> DSchedContinueStateMachine::CreateState(DSchedContinueStateType stateType)
73 {
74     std::shared_ptr<DSchedContinueState> state = nullptr;
75     auto stateMachine = std::shared_ptr<DSchedContinueStateMachine>(shared_from_this());
76     switch (stateType) {
77         case DSCHED_CONTINUE_SOURCE_START_STATE: {
78             state = std::make_shared<DSchedContinueSourceStartState>(stateMachine);
79             break;
80         }
81         case DSCHED_CONTINUE_ABILITY_STATE: {
82             state = std::make_shared<DSchedContinueAbilityState>(stateMachine);
83             break;
84         }
85         case DSCHED_CONTINUE_SOURCE_WAIT_END_STATE: {
86             state = std::make_shared<DSchedContinueWaitEndState>(stateMachine);
87             break;
88         }
89         case DSCHED_CONTINUE_SOURCE_END_STATE: {
90             state = std::make_shared<DSchedContinueEndState>(stateMachine);
91             break;
92         }
93         case DSCHED_CONTINUE_SINK_START_STATE: {
94             state = std::make_shared<DSchedContinueSinkStartState>(stateMachine);
95             break;
96         }
97         case DSCHED_CONTINUE_DATA_STATE: {
98             state = std::make_shared<DSchedContinueDataState>(stateMachine);
99             break;
100         }
101         case DSCHED_CONTINUE_SINK_WAIT_END_STATE: {
102             state = std::make_shared<DSchedContinueSinkWaitEndState>(stateMachine);
103             break;
104         }
105         case DSCHED_CONTINUE_SINK_END_STATE: {
106             state = std::make_shared<DSchedContinueSinkEndState>(stateMachine);
107             break;
108         }
109         default:
110             HILOGE("DSchedContinueStateMachine create state failed, stateType: %{public}d", stateType);
111             break;
112     }
113     return state;
114 }
115 
GetStateType()116 DSchedContinueStateType DSchedContinueStateMachine::GetStateType()
117 {
118     if (currentState_ == nullptr) {
119         return DSCHED_CONTINUE_SOURCE_START_STATE;
120     }
121     return currentState_->GetStateType();
122 }
123 }  // namespace DistributedSchedule
124 }  // namespace OHOS
125