1 /*
2 * Copyright (c) 2023 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 "firmware_flow_manager.h"
17
18 #include "firmware_apply_executor.h"
19 #include "firmware_check_executor.h"
20 #include "firmware_common.h"
21 #include "firmware_download_executor.h"
22 #include "firmware_iexecute_mode.h"
23 #include "firmware_install_executor.h"
24 #include "firmware_log.h"
25
26 namespace OHOS {
27 namespace UpdateEngine {
FirmwareFlowManager()28 FirmwareFlowManager::FirmwareFlowManager()
29 {
30 FIRMWARE_LOGD("FirmwareFlowManager::FirmwareFlowManager()");
31 }
32
~FirmwareFlowManager()33 FirmwareFlowManager::~FirmwareFlowManager()
34 {
35 FIRMWARE_LOGD("FirmwareFlowManager::~FirmwareFlowManager()");
36 }
37
SetExecuteMode(std::shared_ptr<FirmwareIExecuteMode> executeMode)38 void FirmwareFlowManager::SetExecuteMode(std::shared_ptr<FirmwareIExecuteMode> executeMode)
39 {
40 executeMode_ = executeMode;
41 }
42
Start()43 void FirmwareFlowManager::Start()
44 {
45 if (executeMode_ == nullptr) {
46 FIRMWARE_LOGE("FirmwareFlowManager executeMode is null");
47 return;
48 }
49 Execute();
50 }
51
Execute()52 void FirmwareFlowManager::Execute()
53 {
54 if (executeMode_ == nullptr) {
55 FIRMWARE_LOGE("FirmwareFlowManager executeMode is null");
56 return;
57 }
58 nextStep_ = executeMode_->GetNextStep(nextStep_);
59 FIRMWARE_LOGI("FirmwareFlowManager::Execute %{public}d", static_cast<int>(nextStep_));
60 if (nextStep_ == FirmwareStep::COMPLETE) {
61 Complete();
62 } else {
63 executor_ = CreateInstance(nextStep_);
64 if (executor_ != nullptr) {
65 executor_->Execute();
66 } else {
67 FIRMWARE_LOGI("FirmwareFlowManager::Execute null");
68 }
69 }
70 }
71
Complete()72 void FirmwareFlowManager::Complete()
73 {
74 FIRMWARE_LOGI("FirmwareFlowManager::complete");
75 if (executeMode_ == nullptr) {
76 FIRMWARE_LOGE("FirmwareFlowManager executeMode is null");
77 return;
78 }
79 executeMode_->HandleComplete();
80 }
81
OnCheckCallback(CheckStatus status,const Duration & duration,const std::vector<FirmwareComponent> & componentList,const CheckAndAuthInfo & checkAndAuthInfo)82 void FirmwareFlowManager::OnCheckCallback(CheckStatus status, const Duration &duration,
83 const std::vector<FirmwareComponent> &componentList, const CheckAndAuthInfo &checkAndAuthInfo)
84 {
85 FIRMWARE_LOGI("FirmwareFlowManager::OnCheckCallback");
86 if (executeMode_ == nullptr) {
87 FIRMWARE_LOGE("FirmwareFlowManager executeMode is null");
88 return;
89 }
90 executeMode_->SetCheckResult(status, duration, componentList, checkAndAuthInfo);
91 Execute();
92 }
93
OnDownloadCallback(const Progress & progress)94 void FirmwareFlowManager::OnDownloadCallback(const Progress &progress)
95 {
96 FIRMWARE_LOGI("FirmwareFlowManager::OnDownloadCallback");
97 if (executeMode_ == nullptr) {
98 FIRMWARE_LOGE("FirmwareFlowManager executeMode is null");
99 return;
100 }
101 executeMode_->SetDownloadProgress(progress);
102 if (progress.status != UpgradeStatus::DOWNLOADING) {
103 Execute();
104 }
105 }
106
OnInstallCallback(const InstallCallbackInfo & installCallbackInfo)107 void FirmwareFlowManager::OnInstallCallback(const InstallCallbackInfo &installCallbackInfo)
108 {
109 FIRMWARE_LOGI("FirmwareFlowManager::OnInstallCallback");
110 if (executeMode_ == nullptr) {
111 FIRMWARE_LOGE("FirmwareFlowManager executeMode is null");
112 return;
113 }
114 executeMode_->SetInstallResult(installCallbackInfo);
115 if (installCallbackInfo.progress.status != UpgradeStatus::INSTALLING) {
116 Execute();
117 }
118 }
119
OnApplyCallback(bool isSuccess)120 void FirmwareFlowManager::OnApplyCallback(bool isSuccess)
121 {
122 FIRMWARE_LOGI("ParamFlowManager::apply %{public}s", isSuccess ? "success" : "fail");
123 executeMode_->SetApplyResult(isSuccess);
124 Execute();
125 }
126
CreateInstance(FirmwareStep step)127 std::shared_ptr<FirmwareIExecutor> FirmwareFlowManager::CreateInstance(FirmwareStep step)
128 {
129 std::shared_ptr<FirmwareIExecutor> executor = nullptr;
130 switch (step) {
131 case FirmwareStep::CHECK_STEP: {
132 FirmwareCheckComponentCallback checkCallback {[=](CheckStatus status, const Duration &duration,
133 const std::vector<FirmwareComponent> &componentList, const CheckAndAuthInfo &checkAndAuthInfo) {
134 OnCheckCallback(status, duration, componentList, checkAndAuthInfo);
135 }};
136 executor = std::make_shared<FirmwareCheckExecutor>(checkCallback);
137 break;
138 }
139 case FirmwareStep::DOWNLOAD_STEP: {
140 FirmwareProgressCallback downloadCallback{[=](const Progress &progress) { OnDownloadCallback(progress); }};
141 executor = std::make_shared<FirmwareDownloadExecutor>(executeMode_->GetDownloadOptions(), downloadCallback);
142 break;
143 }
144 case FirmwareStep::INSTALL_STEP: {
145 FirmwareInstallExecutorCallback installCallback {[=](const InstallCallbackInfo &installCallbackInfo) {
146 OnInstallCallback(installCallbackInfo);
147 }};
148 executor = std::make_shared<FirmwareInstallExecutor>(executeMode_->GetInstallType(), installCallback);
149 break;
150 }
151 case FirmwareStep::APPLY_STEP: {
152 FirmwareApplyCallback firmwareApplyCallback{[=](bool isSuccess) { OnApplyCallback(isSuccess); }};
153 executor = std::make_shared<FirmwareApplyExecutor>(firmwareApplyCallback);
154 break;
155 }
156 default:
157 FIRMWARE_LOGI("FirmwareFlowManager::CreateInstance NULL");
158 break;
159 }
160 return executor;
161 }
162 } // namespace UpdateEngine
163 } // namespace OHOS