• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_install_apply_mode.h"
17 
18 #include <utility>
19 
20 #include "dupdate_errno.h"
21 #include "dupdate_net_manager.h"
22 #include "dupdate_upgrade_helper.h"
23 #include "firmware_callback_utils.h"
24 #include "firmware_changelog_utils.h"
25 #include "firmware_common.h"
26 #include "firmware_component_operator.h"
27 #include "firmware_constant.h"
28 #include "firmware_log.h"
29 #include "firmware_preferences_utils.h"
30 #include "firmware_status_cache.h"
31 #include "firmware_task_operator.h"
32 #include "firmware_update_helper.h"
33 #include "update_helper.h"
34 
35 namespace OHOS {
36 namespace UpdateEngine {
GetNextStep(FirmwareStep step)37 FirmwareStep FirmwareInstallApplyMode::GetNextStep(FirmwareStep step)
38 {
39     FIRMWARE_LOGI("GetNextStep %{public}d", static_cast<uint32_t>(step));
40     FirmwareStep nextStep = FirmwareStep::COMPLETE;
41     switch (step) {
42         case FirmwareStep::INIT:
43             nextStep = GetStepAfterInit();
44             break;
45         case FirmwareStep::INSTALL_STEP:
46             nextStep = GetStepAfterInstall();
47             break;
48         case FirmwareStep::APPLY_STEP:
49             nextStep = GetStepAfterApply();
50             break;
51         default:
52             break;
53     }
54     return nextStep;
55 }
56 
GetStepAfterInit()57 FirmwareStep FirmwareInstallApplyMode::GetStepAfterInit()
58 {
59     GetTask();
60     if (!tasks_.isExistTask) {
61         FIRMWARE_LOGI("GetStepAfterInit no task");
62         businessError_.Build(CallResult::FAIL, "no task!");
63         return FirmwareStep::COMPLETE;
64     }
65 
66     FIRMWARE_LOGI("GetStepAfterInit status %{public}d taskId = %{public}s, order = %{public}d",
67         static_cast<int32_t>(tasks_.status), tasks_.taskId.c_str(), CAST_INT(upgradeOptions_.order));
68 
69     if (upgradeOptions_.order == Order::INSTALL || upgradeOptions_.order == Order::INSTALL_AND_APPLY) {
70         return GetStepForInstallAndApplyOrder();
71     }
72 
73     if (upgradeOptions_.order == Order::APPLY) {
74         return FirmwareStep::APPLY_STEP;
75     }
76 
77     businessError_.Build(CallResult::FAIL, "upgradeOptions error");
78     return FirmwareStep::COMPLETE;
79 }
80 
GetStepForInstallAndApplyOrder()81 FirmwareStep FirmwareInstallApplyMode::GetStepForInstallAndApplyOrder()
82 {
83     if (tasks_.status != UpgradeStatus::DOWNLOAD_SUCCESS) {
84         businessError_.Build(CallResult::FAIL, "status error!");
85         return FirmwareStep::COMPLETE;
86     }
87 
88     if (!IsAllowInstall()) {
89         return FirmwareStep::COMPLETE;
90     }
91 
92     if (!IsUpgradeFilesReady()) {
93         return FirmwareStep::COMPLETE;
94     }
95 
96     return FirmwareStep::INSTALL_STEP;
97 }
98 
IsAllowInstall()99 bool FirmwareInstallApplyMode::IsAllowInstall()
100 {
101     int64_t requireTotalSize = 0;
102     if (!installStepDataProcessor_.IsSpaceEnough(requireTotalSize)) {
103         FIRMWARE_LOGE("GetStepAfterInit isSpaceEnough false");
104         businessError_.Build(CallResult::FAIL, "install condition fail!");
105         businessError_.AddErrorMessage(CAST_INT(DUPDATE_ERR_NO_ENOUGH_MEMORY), std::to_string(requireTotalSize));
106         return false;
107     }
108 
109     if (!FirmwareUpdateHelper::IsBatteryEnough(MANUAL_UPGRADE_BATTERY_LEVEL)) {
110         FIRMWARE_LOGE("IsAllowInstall isBatteryEnough is false");
111         businessError_.Build(CallResult::FAIL, "install condition fail!");
112         businessError_.AddErrorMessage(CAST_INT(DUPDATE_ERR_LOW_BATTERY_LEVEL), "battery is low");
113         return false;
114     }
115     return true;
116 }
117 
IsUpgradeFilesReady()118 bool FirmwareInstallApplyMode::IsUpgradeFilesReady()
119 {
120     if (installStepDataProcessor_.IsUpgradeFileCheckSuccess()) {
121         return true;
122     }
123 
124     FIRMWARE_LOGE("check component failed");
125     // 手动触发安装场景,DUE通过接口回调返回错误信息给OUC
126     businessError_.Build(CallResult::FAIL, "check component failed");
127     businessError_.AddErrorMessage(CAST_INT(DUPDATE_ERR_UPDATE_PRECHECK_FAIL), "check component failed");
128 
129     FirmwareUpdateHelper::ClearFirmwareInfo();
130     return false;
131 }
132 
GetStepAfterInstall()133 FirmwareStep FirmwareInstallApplyMode::GetStepAfterInstall()
134 {
135     FIRMWARE_LOGI("FirmwareInstallApplyMode installOptions %{public}d", CAST_INT(upgradeOptions_.order));
136     if (!installStepDataProcessor_.HasInstallSuccess()) {
137         FirmwareUpdateHelper::ClearFirmwareInfo();
138     }
139 
140     return FirmwareStep::COMPLETE;
141 }
142 
GetStepAfterApply()143 FirmwareStep FirmwareInstallApplyMode::GetStepAfterApply()
144 {
145     return FirmwareStep::COMPLETE;
146 }
147 
GetTask()148 void FirmwareInstallApplyMode::GetTask()
149 {
150     if (!tasks_.isExistTask) {
151         FirmwareTaskOperator().QueryTask(tasks_);
152         FIRMWARE_LOGI("FirmwareInstallApplyMode::GetTask status: %{public}d ", CAST_INT(tasks_.status));
153     }
154 }
155 
HandleComplete()156 void FirmwareInstallApplyMode::HandleComplete()
157 {
158     onExecuteFinishCallback_();
159 }
160 
GetUpgradeOptions()161 UpgradeOptions FirmwareInstallApplyMode::GetUpgradeOptions()
162 {
163     return upgradeOptions_;
164 }
165 
GetInstallType()166 InstallType FirmwareInstallApplyMode::GetInstallType()
167 {
168     return installType_;
169 }
170 
SetInstallResult(const InstallCallbackInfo & installCallbackInfo)171 void FirmwareInstallApplyMode::SetInstallResult(const InstallCallbackInfo &installCallbackInfo)
172 {
173     installStepDataProcessor_.SetInstallResult(installCallbackInfo);
174 }
175 
SetApplyResult(bool isSuccess)176 void FirmwareInstallApplyMode::SetApplyResult(bool isSuccess)
177 {
178     FIRMWARE_LOGI("SetApplyResult isSuccess: %{public}s", isSuccess ? "success" : "fail");
179 }
180 } // namespace UpdateEngine
181 } // namespace OHOS