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_end_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 = "DSchedContinueSinkEndState";
26 }
DSchedContinueSinkEndState(std::shared_ptr<DSchedContinueStateMachine> stateMachine)27 DSchedContinueSinkEndState::DSchedContinueSinkEndState(std::shared_ptr<DSchedContinueStateMachine> stateMachine)
28     : stateMachine_(stateMachine)
29 {
30     memberFuncMap_[DSCHED_CONTINUE_END_EVENT] = &DSchedContinueSinkEndState::DoContinueEndTask;
31 }
32 
~DSchedContinueSinkEndState()33 DSchedContinueSinkEndState::~DSchedContinueSinkEndState()
34 {
35 }
36 
Execute(std::shared_ptr<DSchedContinue> dContinue,const AppExecFwk::InnerEvent::Pointer & event)37 int32_t DSchedContinueSinkEndState::Execute(std::shared_ptr<DSchedContinue> dContinue,
38     const AppExecFwk::InnerEvent::Pointer &event)
39 {
40     if (event == nullptr) {
41         HILOGE("event is null");
42         return INVALID_PARAMETERS_ERR;
43     }
44     auto iterFunc = memberFuncMap_.find(event->GetInnerEventId());
45     if (iterFunc == memberFuncMap_.end()) {
46         HILOGI("DSchedContinueSinkEndState execute %{public}d in wrong state", event->GetInnerEventId());
47         return CONTINUE_STATE_MACHINE_INVALID_STATE;
48     }
49 
50     auto memberFunc = iterFunc->second;
51     int32_t ret = (this->*memberFunc)(dContinue, event);
52     if (ret != ERR_OK) {
53         HILOGI("DSchedContinueSinkEndState execute %{public}d failed, ret: %{public}d",
54             event->GetInnerEventId(), ret);
55     }
56     return ret;
57 }
58 
GetStateType()59 DSchedContinueStateType DSchedContinueSinkEndState::GetStateType()
60 {
61     return DSCHED_CONTINUE_SINK_END_STATE;
62 }
63 
DoContinueEndTask(std::shared_ptr<DSchedContinue> dContinue,const AppExecFwk::InnerEvent::Pointer & event)64 int32_t DSchedContinueSinkEndState::DoContinueEndTask(std::shared_ptr<DSchedContinue> dContinue,
65     const AppExecFwk::InnerEvent::Pointer &event)
66 {
67     if (dContinue == nullptr || event == nullptr) {
68         HILOGE("dContinue or event is null");
69         return INVALID_PARAMETERS_ERR;
70     }
71     auto syncContinueData = event->GetSharedObject<int32_t>();
72     int32_t ret = dContinue->ExecuteContinueEnd(*syncContinueData);
73     if (ret != ERR_OK) {
74         HILOGE("DSchedContinueSinkEndState ExecuteContinueEnd failed, ret: %{public}d", ret);
75     }
76     return ret;
77 }
78 }  // namespace DistributedSchedule
79 }  // namespace OHOS
80