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 && currentState_ != nullptr) {
64 HILOGI("DSchedContinueStateMachine update state from %{public}d to %{public}d",
65 currentState_->GetStateType(), stateType);
66 } else {
67 HILOGI("DSchedContinueStateMachine update state %{public}d", stateType);
68 }
69 currentState_ = CreateState(stateType);
70 return;
71 }
72
CreateState(DSchedContinueStateType stateType)73 std::shared_ptr<DSchedContinueState> DSchedContinueStateMachine::CreateState(DSchedContinueStateType stateType)
74 {
75 std::shared_ptr<DSchedContinueState> state = nullptr;
76 auto stateMachine = std::shared_ptr<DSchedContinueStateMachine>(shared_from_this());
77 switch (stateType) {
78 case DSCHED_CONTINUE_SOURCE_START_STATE: {
79 state = std::make_shared<DSchedContinueSourceStartState>(stateMachine);
80 break;
81 }
82 case DSCHED_CONTINUE_ABILITY_STATE: {
83 state = std::make_shared<DSchedContinueAbilityState>(stateMachine);
84 break;
85 }
86 case DSCHED_CONTINUE_SOURCE_WAIT_END_STATE: {
87 state = std::make_shared<DSchedContinueWaitEndState>(stateMachine);
88 break;
89 }
90 case DSCHED_CONTINUE_SOURCE_END_STATE: {
91 state = std::make_shared<DSchedContinueEndState>(stateMachine);
92 break;
93 }
94 case DSCHED_CONTINUE_SINK_START_STATE: {
95 state = std::make_shared<DSchedContinueSinkStartState>(stateMachine);
96 break;
97 }
98 case DSCHED_CONTINUE_DATA_STATE: {
99 state = std::make_shared<DSchedContinueDataState>(stateMachine);
100 break;
101 }
102 case DSCHED_CONTINUE_SINK_WAIT_END_STATE: {
103 state = std::make_shared<DSchedContinueSinkWaitEndState>(stateMachine);
104 break;
105 }
106 case DSCHED_CONTINUE_SINK_END_STATE: {
107 state = std::make_shared<DSchedContinueSinkEndState>(stateMachine);
108 break;
109 }
110 default:
111 HILOGE("DSchedContinueStateMachine create state failed, stateType: %{public}d", stateType);
112 break;
113 }
114 return state;
115 }
116
GetStateType()117 DSchedContinueStateType DSchedContinueStateMachine::GetStateType()
118 {
119 if (currentState_ == nullptr) {
120 return DSCHED_CONTINUE_SOURCE_START_STATE;
121 }
122 return currentState_->GetStateType();
123 }
124 } // namespace DistributedSchedule
125 } // namespace OHOS
126