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_ability_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 = "DSchedContinueAbilityState";
26 }
DSchedContinueAbilityState(std::shared_ptr<DSchedContinueStateMachine> stateMachine)27 DSchedContinueAbilityState::DSchedContinueAbilityState(std::shared_ptr<DSchedContinueStateMachine> stateMachine)
28     : stateMachine_(stateMachine)
29 {
30     memberFuncMap_[DSHCED_CONTINUE_SEND_DATA_EVENT] = &DSchedContinueAbilityState::DoContinueSendTask;
31     memberFuncMap_[DSCHED_CONTINUE_COMPLETE_EVENT] = &DSchedContinueAbilityState::DoContinueEndTask;
32     memberFuncMap_[DSCHED_CONTINUE_END_EVENT] = &DSchedContinueAbilityState::DoContinueErrorTask;
33 }
34 
~DSchedContinueAbilityState()35 DSchedContinueAbilityState::~DSchedContinueAbilityState()
36 {
37 }
38 
Execute(std::shared_ptr<DSchedContinue> dContinue,const AppExecFwk::InnerEvent::Pointer & event)39 int32_t DSchedContinueAbilityState::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         HILOGI("DSchedContinueAbilityState 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         HILOGE("DSchedContinueAbilityState execute %{public}d failed, ret: %{public}d",
56             event->GetInnerEventId(), ret);
57     }
58     return ret;
59 }
60 
GetStateType()61 DSchedContinueStateType DSchedContinueAbilityState::GetStateType()
62 {
63     return DSCHED_CONTINUE_ABILITY_STATE;
64 }
65 
DoContinueSendTask(std::shared_ptr<DSchedContinue> dContinue,const AppExecFwk::InnerEvent::Pointer & event)66 int32_t DSchedContinueAbilityState::DoContinueSendTask(std::shared_ptr<DSchedContinue> dContinue,
67     const AppExecFwk::InnerEvent::Pointer &event)
68 {
69     if (dContinue == nullptr || event == nullptr) {
70         HILOGE("dContinue or event is null");
71         return INVALID_PARAMETERS_ERR;
72     }
73     auto syncContinueData = event->GetSharedObject<ContinueAbilityData>();
74     int32_t ret = dContinue->ExecuteContinueSend(syncContinueData);
75     if (ret != ERR_OK) {
76         HILOGE("DSchedContinueAbilityState ExecuteContinueSend failed, ret: %{public}d", ret);
77     }
78     return ret;
79 }
80 
DoContinueErrorTask(std::shared_ptr<DSchedContinue> dContinue,const AppExecFwk::InnerEvent::Pointer & event)81 int32_t DSchedContinueAbilityState::DoContinueErrorTask(std::shared_ptr<DSchedContinue> dContinue,
82     const AppExecFwk::InnerEvent::Pointer &event)
83 {
84     if (dContinue == nullptr || event == nullptr) {
85         HILOGE("dContinue or event is null");
86         return INVALID_PARAMETERS_ERR;
87     }
88     auto syncContinueData = event->GetSharedObject<int32_t>();
89     int32_t ret = dContinue->ExecuteContinueError(*syncContinueData);
90     if (ret != ERR_OK) {
91         HILOGE("DSchedContinueAbilityState ExecuteContinueEnd failed, ret: %{public}d", ret);
92     }
93     return ret;
94 }
95 
DoContinueEndTask(std::shared_ptr<DSchedContinue> dContinue,const AppExecFwk::InnerEvent::Pointer & event)96 int32_t DSchedContinueAbilityState::DoContinueEndTask(std::shared_ptr<DSchedContinue> dContinue,
97     const AppExecFwk::InnerEvent::Pointer &event)
98 {
99     if (dContinue == nullptr || event == nullptr) {
100         HILOGE("dContinue or event is null");
101         return INVALID_PARAMETERS_ERR;
102     }
103     auto syncContinueData = event->GetSharedObject<int32_t>();
104     int32_t ret = dContinue->ExecuteContinueEnd(*syncContinueData);
105     if (ret != ERR_OK) {
106         HILOGE("DSchedContinueAbilityState ExecuteContinueEnd failed, ret: %{public}d", ret);
107     }
108     return ret;
109 }
110 }  // namespace DistributedSchedule
111 }  // namespace OHOS
112