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_source_start_state.h"
17 
18 #include "dsched_continue.h"
19 #include "dsched_continue_event.h"
20 #include "dtbschedmgr_log.h"
21 
22 namespace OHOS {
23 namespace DistributedSchedule {
24 namespace {
25 const std::string TAG = "DSchedContinueSourceStartState";
26 }
DSchedContinueSourceStartState(std::shared_ptr<DSchedContinueStateMachine> stateMachine)27 DSchedContinueSourceStartState::DSchedContinueSourceStartState(std::shared_ptr<DSchedContinueStateMachine> stateMachine)
28     : stateMachine_(stateMachine)
29 {
30     memberFuncMap_[DSHCED_CONTINUE_REQ_PUSH_EVENT] = &DSchedContinueSourceStartState::DoContinuePushReqTask;
31     memberFuncMap_[DSHCED_CONTINUE_ABILITY_EVENT] = &DSchedContinueSourceStartState::DoContinueAbilityTask;
32     memberFuncMap_[DSCHED_CONTINUE_COMPLETE_EVENT] = &DSchedContinueSourceStartState::DoContinueEndTask;
33     memberFuncMap_[DSCHED_CONTINUE_END_EVENT] = &DSchedContinueSourceStartState::DoContinueErrorTask;
34 }
35 
~DSchedContinueSourceStartState()36 DSchedContinueSourceStartState::~DSchedContinueSourceStartState()
37 {
38 }
39 
Execute(std::shared_ptr<DSchedContinue> dContinue,const AppExecFwk::InnerEvent::Pointer & event)40 int32_t DSchedContinueSourceStartState::Execute(std::shared_ptr<DSchedContinue> dContinue,
41     const AppExecFwk::InnerEvent::Pointer &event)
42 {
43     if (event == nullptr) {
44         HILOGE("event is null");
45         return INVALID_PARAMETERS_ERR;
46     }
47     auto iterFunc = memberFuncMap_.find(event->GetInnerEventId());
48     if (iterFunc == memberFuncMap_.end()) {
49         HILOGI("DSchedContinueSourceStartState execute %{public}d in wrong state", event->GetInnerEventId());
50         return CONTINUE_STATE_MACHINE_INVALID_STATE;
51     }
52 
53     auto memberFunc = iterFunc->second;
54     int32_t ret = (this->*memberFunc)(dContinue, event);
55     if (ret != ERR_OK) {
56         HILOGI("DSchedContinueSourceStartState execute %{public}d failed, ret: %{public}d",
57             event->GetInnerEventId(), ret);
58     }
59     return ret;
60 }
61 
GetStateType()62 DSchedContinueStateType DSchedContinueSourceStartState::GetStateType()
63 {
64     return DSCHED_CONTINUE_SOURCE_START_STATE;
65 }
66 
DoContinuePushReqTask(std::shared_ptr<DSchedContinue> dContinue,const AppExecFwk::InnerEvent::Pointer & event)67 int32_t DSchedContinueSourceStartState::DoContinuePushReqTask(std::shared_ptr<DSchedContinue> dContinue,
68     const AppExecFwk::InnerEvent::Pointer &event)
69 {
70     if (dContinue == nullptr || event == nullptr) {
71         HILOGE("dContinue or event is null");
72         return INVALID_PARAMETERS_ERR;
73     }
74     auto syncContinueData = event->GetSharedObject<DistributedWantParams>();
75     int32_t ret = dContinue->ExecuteContinueReq(syncContinueData);
76     if (ret != ERR_OK) {
77         HILOGE("DSchedContinueSourceStartState ExecuteContinueAbility failed, ret: %{public}d", ret);
78     }
79     return ret;
80 }
81 
DoContinueAbilityTask(std::shared_ptr<DSchedContinue> dContinue,const AppExecFwk::InnerEvent::Pointer & event)82 int32_t DSchedContinueSourceStartState::DoContinueAbilityTask(std::shared_ptr<DSchedContinue> dContinue,
83     const AppExecFwk::InnerEvent::Pointer &event)
84 {
85     if (dContinue == nullptr || event == nullptr) {
86         HILOGE("dContinue or event is null");
87         return INVALID_PARAMETERS_ERR;
88     }
89     auto syncContinueData = event->GetSharedObject<int32_t>();
90     int32_t ret = dContinue->ExecuteContinueAbility(*syncContinueData);
91     if (ret != ERR_OK) {
92         HILOGE("DSchedContinueSourceStartState ExecuteContinueAbility failed, ret: %{public}d", ret);
93     }
94     return ret;
95 }
96 
DoContinueErrorTask(std::shared_ptr<DSchedContinue> dContinue,const AppExecFwk::InnerEvent::Pointer & event)97 int32_t DSchedContinueSourceStartState::DoContinueErrorTask(std::shared_ptr<DSchedContinue> dContinue,
98     const AppExecFwk::InnerEvent::Pointer &event)
99 {
100     if (dContinue == nullptr || event == nullptr) {
101         HILOGE("dContinue or event is null");
102         return INVALID_PARAMETERS_ERR;
103     }
104     auto syncContinueData = event->GetSharedObject<int32_t>();
105     int32_t ret = dContinue->ExecuteContinueError(*syncContinueData);
106     if (ret != ERR_OK) {
107         HILOGE("DSchedContinueSourceStartState ExecuteContinueError failed, ret: %{public}d", ret);
108     }
109     return ret;
110 }
111 
DoContinueEndTask(std::shared_ptr<DSchedContinue> dContinue,const AppExecFwk::InnerEvent::Pointer & event)112 int32_t DSchedContinueSourceStartState::DoContinueEndTask(std::shared_ptr<DSchedContinue> dContinue,
113     const AppExecFwk::InnerEvent::Pointer &event)
114 {
115     if (dContinue == nullptr || event == nullptr) {
116         HILOGE("dContinue or event is null");
117         return INVALID_PARAMETERS_ERR;
118     }
119     auto syncContinueData = event->GetSharedObject<int32_t>();
120     int32_t ret = dContinue->ExecuteContinueEnd(*syncContinueData);
121     if (ret != ERR_OK) {
122         HILOGE("DSchedContinueSourceStartState ExecuteContinueError failed, ret: %{public}d", ret);
123     }
124     return ret;
125 }
126 }  // namespace DistributedSchedule
127 }  // namespace OHOS
128