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_data_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 = "DSchedContinueDataState";
26 }
DSchedContinueDataState(std::shared_ptr<DSchedContinueStateMachine> stateMachine)27 DSchedContinueDataState::DSchedContinueDataState(std::shared_ptr<DSchedContinueStateMachine> stateMachine)
28     : stateMachine_(stateMachine)
29 {
30     memberFuncMap_[DSCHED_CONTINUE_DATA_EVENT] = &DSchedContinueDataState::DoContinueDataTask;
31     memberFuncMap_[DSCHED_CONTINUE_COMPLETE_EVENT] = &DSchedContinueDataState::DoContinueEndTask;
32     memberFuncMap_[DSCHED_CONTINUE_END_EVENT] = &DSchedContinueDataState::DoContinueErrorTask;
33 }
34 
~DSchedContinueDataState()35 DSchedContinueDataState::~DSchedContinueDataState()
36 {
37 }
38 
Execute(std::shared_ptr<DSchedContinue> dContinue,const AppExecFwk::InnerEvent::Pointer & event)39 int32_t DSchedContinueDataState::Execute(std::shared_ptr<DSchedContinue> dContinue,
40     const AppExecFwk::InnerEvent::Pointer &event)
41 {
42     if (event == nullptr) {
43         HILOGE("event is null");
44         return INVALID_PARAMETERS_ERR;
45     }
46     auto iterFunc = memberFuncMap_.find(event->GetInnerEventId());
47     if (iterFunc == memberFuncMap_.end()) {
48         HILOGE("DSchedContinueDataState execute %{public}d in wrong state", event->GetInnerEventId());
49         return CONTINUE_STATE_MACHINE_INVALID_STATE;
50     }
51 
52     auto memberFunc = iterFunc->second;
53     int32_t ret = (this->*memberFunc)(dContinue, event);
54     if (ret != ERR_OK) {
55         HILOGI("DSchedContinueDataState execute %{public}d failed, ret: %{public}d", event->GetInnerEventId(), ret);
56     }
57     return ret;
58 }
59 
GetStateType()60 DSchedContinueStateType DSchedContinueDataState::GetStateType()
61 {
62     return DSCHED_CONTINUE_DATA_STATE;
63 }
64 
DoContinueDataTask(std::shared_ptr<DSchedContinue> dContinue,const AppExecFwk::InnerEvent::Pointer & event)65 int32_t DSchedContinueDataState::DoContinueDataTask(std::shared_ptr<DSchedContinue> dContinue,
66     const AppExecFwk::InnerEvent::Pointer &event)
67 {
68     if (dContinue == nullptr || event == nullptr) {
69         HILOGE("dContinue or event is null");
70         return INVALID_PARAMETERS_ERR;
71     }
72     auto syncContinueData = event->GetSharedObject<DSchedContinueDataCmd>();
73     int32_t ret = dContinue->ExecuteContinueData(syncContinueData);
74     if (ret != ERR_OK) {
75         HILOGE("DSchedContinueAbilityState ExecuteContinueSend failed, ret: %{public}d", ret);
76     }
77     return ret;
78 }
79 
DoContinueErrorTask(std::shared_ptr<DSchedContinue> dContinue,const AppExecFwk::InnerEvent::Pointer & event)80 int32_t DSchedContinueDataState::DoContinueErrorTask(std::shared_ptr<DSchedContinue> dContinue,
81     const AppExecFwk::InnerEvent::Pointer &event)
82 {
83     if (dContinue == nullptr || event == nullptr) {
84         HILOGE("dContinue or event is null");
85         return INVALID_PARAMETERS_ERR;
86     }
87     auto syncContinueData = event->GetSharedObject<int32_t>();
88     int32_t ret = dContinue->ExecuteContinueError(*syncContinueData);
89     if (ret != ERR_OK) {
90         HILOGE("DSchedContinueDataState ExecuteContinueSend failed, ret: %{public}d", ret);
91     }
92     return ret;
93 }
94 
DoContinueEndTask(std::shared_ptr<DSchedContinue> dContinue,const AppExecFwk::InnerEvent::Pointer & event)95 int32_t DSchedContinueDataState::DoContinueEndTask(std::shared_ptr<DSchedContinue> dContinue,
96     const AppExecFwk::InnerEvent::Pointer &event)
97 {
98     if (dContinue == nullptr || event == nullptr) {
99         HILOGE("dContinue or event is null");
100         return INVALID_PARAMETERS_ERR;
101     }
102     auto syncContinueData = event->GetSharedObject<int32_t>();
103     int32_t ret = dContinue->ExecuteContinueEnd(*syncContinueData);
104     if (ret != ERR_OK) {
105         HILOGE("DSchedContinueDataState ExecuteContinueSend failed, ret: %{public}d", ret);
106     }
107     return ret;
108 }
109 }  // namespace DistributedSchedule
110 }  // namespace OHOS
111