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_download_mode.h"
17
18 #include "dupdate_errno.h"
19 #include "dupdate_inet_observer.h"
20 #include "dupdate_net_manager.h"
21 #include "firmware_callback_utils.h"
22 #include "firmware_common.h"
23 #include "firmware_constant.h"
24 #include "firmware_log.h"
25 #include "firmware_preferences_utils.h"
26 #include "firmware_status_cache.h"
27 #include "firmware_task_operator.h"
28 #include "firmware_update_helper.h"
29 #include "update_helper.h"
30
31 namespace OHOS {
32 namespace UpdateEngine {
GetNextStep(FirmwareStep step)33 FirmwareStep FirmwareDownloadMode::GetNextStep(FirmwareStep step)
34 {
35 FIRMWARE_LOGI("GetNextStep %{public}d", static_cast<uint32_t>(step));
36 FirmwareStep nextStep = FirmwareStep::COMPLETE;
37 switch (step) {
38 case FirmwareStep::INIT:
39 nextStep = GetStepAfterInit();
40 break;
41 case FirmwareStep::DOWNLOAD_STEP:
42 nextStep = GetStepAfterDownload();
43 break;
44 default:
45 break;
46 }
47 return nextStep;
48 }
49
GetStepAfterInit()50 FirmwareStep FirmwareDownloadMode::GetStepAfterInit()
51 {
52 GetTask();
53 if (!tasks_.isExistTask) {
54 FIRMWARE_LOGI("GetStepAfterInit no task");
55 businessError_.Build(CallResult::FAIL, "no task!");
56 return FirmwareStep::COMPLETE;
57 }
58
59 FIRMWARE_LOGI("GetStepAfterInit status %{public}d", static_cast<int32_t>(tasks_.status));
60 UpgradeStatus taskStatus = tasks_.status;
61 if (taskStatus != UpgradeStatus::CHECK_VERSION_SUCCESS &&
62 taskStatus != UpgradeStatus::DOWNLOAD_FAIL &&
63 taskStatus != UpgradeStatus::DOWNLOAD_PAUSE) {
64 businessError_.Build(CallResult::FAIL, "status error!");
65 return FirmwareStep::COMPLETE;
66 }
67
68 if (taskStatus == UpgradeStatus::DOWNLOADING) {
69 FIRMWARE_LOGI("GetStepAfterInit system busy");
70 businessError_.Build(CallResult::FAIL, "system busy!");
71 businessError_.AddErrorMessage(CAST_INT(DUPDATE_ERR_SYSTEM_BUSY_ON_DOWNLOAD), "busy on Downloading!");
72 return FirmwareStep::COMPLETE;
73 }
74
75 if (!DelayedSingleton<NetManager>::GetInstance()->IsNetAvailable()) {
76 FIRMWARE_LOGI("GetStepAfterInit IsNeedBlockCheck no network, nettype is %{public}d",
77 DelayedSingleton<NetManager>::GetInstance()->GetNetType());
78 businessError_.Build(CallResult::FAIL, "download no network!");
79 businessError_.AddErrorMessage(CAST_INT(DUPDATE_ERR_DLOAD_REQUEST_FAIL), "download no network!");
80 return FirmwareStep::COMPLETE;
81 }
82
83 int64_t requireTotalSize = 0;
84 bool isSpaceEnough = downloadDataProcessor_.IsSpaceEnough(requireTotalSize);
85 if (!isSpaceEnough) {
86 FIRMWARE_LOGE("GetStepAfterInit isSpaceEnough false");
87 businessError_.Build(CallResult::FAIL, "download condition fail!");
88 businessError_.AddErrorMessage(CAST_INT(DUPDATE_ERR_NO_ENOUGH_MEMORY), std::to_string(requireTotalSize));
89 return FirmwareStep::COMPLETE;
90 }
91 return FirmwareStep::DOWNLOAD_STEP;
92 }
93
GetStepAfterDownload()94 FirmwareStep FirmwareDownloadMode::GetStepAfterDownload()
95 {
96 FIRMWARE_LOGI("GetStepAfterDownload downloadOptions %{public}d", CAST_INT(downloadOptions_.order));
97 FirmwareTask task;
98 FirmwareTaskOperator().QueryTask(task);
99 FIRMWARE_LOGI("GetStepAfterDownload task status %{public}d", task.status);
100 if (!task.isExistTask) {
101 FIRMWARE_LOGE("GetStepAfterDownload task not exist");
102 return FirmwareStep::COMPLETE;
103 }
104 ErrorMessage errorMessage;
105 if (!downloadDataProcessor_.GetDownloadProgress().endReason.empty()) {
106 errorMessage.errorCode = stoi(downloadDataProcessor_.GetDownloadProgress().endReason);
107 }
108 FirmwareStep nextStep = FirmwareStep::COMPLETE;
109 switch (task.status) {
110 case UpgradeStatus::DOWNLOAD_PAUSE:
111 DownloadPauseProcess(task, errorMessage);
112 nextStep = FirmwareStep::COMPLETE;
113 break;
114 case UpgradeStatus::DOWNLOAD_FAIL:
115 DownloadFailProcess(task, errorMessage);
116 nextStep = FirmwareStep::COMPLETE;
117 break;
118 case UpgradeStatus::DOWNLOAD_CANCEL:
119 DownloadCancelProcess(task, errorMessage);
120 nextStep = FirmwareStep::COMPLETE;
121 break;
122 case UpgradeStatus::DOWNLOAD_SUCCESS:
123 DownloadSucessProcess(task, errorMessage);
124 nextStep = FirmwareStep::COMPLETE;
125 break;
126 default:
127 break;
128 }
129 return nextStep;
130 }
131
DownloadPauseProcess(const FirmwareTask & task,const ErrorMessage & errorMessage)132 void FirmwareDownloadMode::DownloadPauseProcess(const FirmwareTask &task, const ErrorMessage &errorMessage)
133 {
134 FIRMWARE_LOGI("GetStepAfterDownload download pause");
135 DelayedSingleton<FirmwareCallbackUtils>::GetInstance()->NotifyEvent(
136 task.taskId, EventId::EVENT_DOWNLOAD_PAUSE, UpgradeStatus::DOWNLOAD_PAUSE, errorMessage);
137 }
138
DownloadFailProcess(const FirmwareTask & task,const ErrorMessage & errorMessage)139 void FirmwareDownloadMode::DownloadFailProcess(const FirmwareTask &task, const ErrorMessage &errorMessage)
140 {
141 FIRMWARE_LOGI("GetStepAfterDownload download fail");
142 DelayedSingleton<FirmwareCallbackUtils>::GetInstance()->NotifyEvent(
143 task.taskId, EventId::EVENT_DOWNLOAD_FAIL, UpgradeStatus::DOWNLOAD_FAIL, errorMessage);
144 FirmwareUpdateHelper::ClearFirmwareInfo();
145 }
146
DownloadCancelProcess(const FirmwareTask & task,const ErrorMessage & errorMessage)147 void FirmwareDownloadMode::DownloadCancelProcess(const FirmwareTask &task, const ErrorMessage &errorMessage)
148 {
149 FIRMWARE_LOGI("GetStepAfterDownload download cancel");
150 DelayedSingleton<FirmwareCallbackUtils>::GetInstance()->NotifyEvent(
151 task.taskId, EventId::EVENT_DOWNLOAD_CANCEL, UpgradeStatus::DOWNLOAD_CANCEL, errorMessage);
152 FirmwareTaskOperator().UpdateProgressByTaskId(task.taskId, UpgradeStatus::CHECK_VERSION_SUCCESS, 0);
153 std::vector<FirmwareComponent> firmwareComponentList;
154 FirmwareComponentOperator firmwareComponentOperator;
155 firmwareComponentOperator.QueryAll(firmwareComponentList);
156 for (const FirmwareComponent &component : firmwareComponentList) {
157 firmwareComponentOperator.UpdateProgressByUrl(
158 component.url, UpgradeStatus::CHECK_VERSION_SUCCESS, 0);
159 }
160 FirmwareTaskOperator().UpdateDownloadTaskIdByTaskId(task.taskId, "");
161 FirmwareFileUtils::DeleteDownloadFiles();
162 }
163
DownloadSucessProcess(const FirmwareTask & task,const ErrorMessage & errorMessage)164 void FirmwareDownloadMode::DownloadSucessProcess(const FirmwareTask &task, const ErrorMessage &errorMessage)
165 {
166 FIRMWARE_LOGI("GetStepAfterDownload download success");
167 DelayedSingleton<FirmwareCallbackUtils>::GetInstance()->NotifyEvent(
168 task.taskId, EventId::EVENT_UPGRADE_WAIT, UpgradeStatus::DOWNLOAD_SUCCESS);
169 }
170
GetTask()171 void FirmwareDownloadMode::GetTask()
172 {
173 if (!tasks_.isExistTask) {
174 FirmwareTaskOperator().QueryTask(tasks_);
175 }
176 }
177
HandleComplete()178 void FirmwareDownloadMode::HandleComplete()
179 {
180 FIRMWARE_LOGI("FirmwareDownloadMode::HandleComplete");
181 onExecuteFinishCallback_();
182 }
183
GetDownloadOptions()184 DownloadOptions FirmwareDownloadMode::GetDownloadOptions()
185 {
186 return downloadOptions_;
187 }
188
SetDownloadProgress(const Progress & progress)189 void FirmwareDownloadMode::SetDownloadProgress(const Progress &progress)
190 {
191 downloadDataProcessor_.SetDownloadProgress(progress);
192 }
193 } // namespace UpdateEngine
194 } // namespace OHOS