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_sink_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 = "DSchedContinueSinkStartState";
26 }
DSchedContinueSinkStartState(std::shared_ptr<DSchedContinueStateMachine> stateMachine)27 DSchedContinueSinkStartState::DSchedContinueSinkStartState(std::shared_ptr<DSchedContinueStateMachine> stateMachine)
28     : stateMachine_(stateMachine)
29 {
30     memberFuncMap_[DSCHED_CONTINUE_REQ_PULL_EVENT] = &DSchedContinueSinkStartState::DoContinuePullReqTask;
31     memberFuncMap_[DSHCED_CONTINUE_ABILITY_EVENT] = &DSchedContinueSinkStartState::DoContinueAbilityTask;
32     memberFuncMap_[DSCHED_CONTINUE_COMPLETE_EVENT] = &DSchedContinueSinkStartState::DoContinueEndTask;
33     memberFuncMap_[DSCHED_CONTINUE_END_EVENT] = &DSchedContinueSinkStartState::DoContinueErrorTask;
34 }
35 
~DSchedContinueSinkStartState()36 DSchedContinueSinkStartState::~DSchedContinueSinkStartState()
37 {
38 }
39 
Execute(std::shared_ptr<DSchedContinue> dContinue,const AppExecFwk::InnerEvent::Pointer & event)40 int32_t DSchedContinueSinkStartState::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("DSchedContinueSinkStartState 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("DSchedContinueSinkStartState execute %{public}d failed, ret: %{public}d",
57             event->GetInnerEventId(), ret);
58     }
59     return ret;
60 }
61 
GetStateType()62 DSchedContinueStateType DSchedContinueSinkStartState::GetStateType()
63 {
64     return DSCHED_CONTINUE_SINK_START_STATE;
65 }
66 
DoContinuePullReqTask(std::shared_ptr<DSchedContinue> dContinue,const AppExecFwk::InnerEvent::Pointer & event)67 int32_t DSchedContinueSinkStartState::DoContinuePullReqTask(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("DSchedContinueSinkStartState ExecuteContinueSend 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 DSchedContinueSinkStartState::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     return dContinue->ExecuteContinueReply();
90 }
91 
DoContinueErrorTask(std::shared_ptr<DSchedContinue> dContinue,const AppExecFwk::InnerEvent::Pointer & event)92 int32_t DSchedContinueSinkStartState::DoContinueErrorTask(std::shared_ptr<DSchedContinue> dContinue,
93     const AppExecFwk::InnerEvent::Pointer &event)
94 {
95     if (dContinue == nullptr || event == nullptr) {
96         HILOGE("dContinue or event is null");
97         return INVALID_PARAMETERS_ERR;
98     }
99     auto syncContinueData = event->GetSharedObject<int32_t>();
100     int32_t ret = dContinue->ExecuteContinueError(*syncContinueData);
101     if (ret != ERR_OK) {
102         HILOGE("DSchedContinueSinkStartState ExecuteContinueSend failed, ret: %{public}d", ret);
103     }
104     return ret;
105 }
106 
DoContinueEndTask(std::shared_ptr<DSchedContinue> dContinue,const AppExecFwk::InnerEvent::Pointer & event)107 int32_t DSchedContinueSinkStartState::DoContinueEndTask(std::shared_ptr<DSchedContinue> dContinue,
108     const AppExecFwk::InnerEvent::Pointer &event)
109 {
110     if (dContinue == nullptr || event == nullptr) {
111         HILOGE("dContinue or event is null");
112         return INVALID_PARAMETERS_ERR;
113     }
114     auto syncContinueData = event->GetSharedObject<int32_t>();
115     int32_t ret = dContinue->ExecuteContinueEnd(*syncContinueData);
116     if (ret != ERR_OK) {
117         HILOGE("DSchedContinueSinkStartState ExecuteContinueSend failed, ret: %{public}d", ret);
118     }
119     return ret;
120 }
121 }  // namespace DistributedSchedule
122 }  // namespace OHOS
123