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_data_processor.h"
17 
18 #include "firmware_callback_utils.h"
19 #include "firmware_component_operator.h"
20 #include "firmware_constant.h"
21 #include "firmware_file_utils.h"
22 #include "firmware_iexecutor.h"
23 #include "firmware_log.h"
24 #include "firmware_task_operator.h"
25 #include "firmware_update_helper.h"
26 #include "string_utils.h"
27 
28 namespace OHOS {
29 namespace UpdateEngine {
30 constexpr int64_t SPACE_OFFSET = 100 *1024 *1024;
31 
IsSpaceEnough(int64_t & requireTotalSize)32 bool FirmwareInstallDataProcessor::IsSpaceEnough(int64_t &requireTotalSize)
33 {
34     std::vector<FirmwareComponent> components;
35     FirmwareComponentOperator().QueryAll(components);
36     for (const auto &component : components) {
37         requireTotalSize = requireTotalSize + component.size;
38     }
39     bool isSpaceEnough = FirmwareFileUtils::IsSpaceEnough(requireTotalSize * Firmware::COMPRESSION_RATIO
40         + SPACE_OFFSET);
41     FIRMWARE_LOGI("IsSpaceEnough %{public}s", StringUtils::GetBoolStr(isSpaceEnough).c_str());
42     return isSpaceEnough;
43 }
44 
HasInstallSuccess()45 bool FirmwareInstallDataProcessor::HasInstallSuccess()
46 {
47     // 实时取当前状态
48     FirmwareTaskOperator().QueryTask(tasks_);
49     FIRMWARE_LOGI("GetTaskStatus status: %{public}d", tasks_.status);
50     return tasks_.status == UpgradeStatus::INSTALL_SUCCESS;
51 }
52 
HasUpdateSuccess()53 bool FirmwareInstallDataProcessor::HasUpdateSuccess()
54 {
55     // 实时取当前状态
56     FirmwareTaskOperator().QueryTask(tasks_);
57     FIRMWARE_LOGI("GetTaskStatus status: %{public}d", tasks_.status);
58     return tasks_.status == UpgradeStatus::UPDATE_SUCCESS;
59 }
60 
GetTask()61 void FirmwareInstallDataProcessor::GetTask()
62 {
63     if (!tasks_.isExistTask) {
64         FirmwareTaskOperator().QueryTask(tasks_);
65         FIRMWARE_LOGI("GetTask has task %{public}s", StringUtils::GetBoolStr(tasks_.isExistTask).c_str());
66     }
67 }
68 
SetInstallResult(const InstallCallbackInfo & installCallbackInfo)69 void FirmwareInstallDataProcessor::SetInstallResult(const InstallCallbackInfo &installCallbackInfo)
70 {
71     Progress progress = installCallbackInfo.progress;
72     FIRMWARE_LOGI("SetInstallResult status: %{public}d progress: %{public}d", progress.status, progress.percent);
73     GetTask();
74     if (progress.status == UpgradeStatus::UPDATE_FAIL || progress.status == UpgradeStatus::INSTALL_FAIL) {
75         FIRMWARE_LOGI("SetInstallResult errorCode: %{public}d errorMessage: %{public}s",
76             installCallbackInfo.errorMessage.errorCode, installCallbackInfo.errorMessage.errorMessage.c_str());
77         DelayedSingleton<FirmwareCallbackUtils>::GetInstance()->NotifyEvent(tasks_.taskId, EventId::EVENT_UPGRADE_FAIL,
78             progress.status, installCallbackInfo.errorMessage);
79         return;
80     }
81 
82     DelayedSingleton<FirmwareCallbackUtils>::GetInstance()->ProgressCallback(tasks_.taskId, progress);
83 }
84 
IsUpgradeFileCheckSuccess()85 bool FirmwareInstallDataProcessor::IsUpgradeFileCheckSuccess()
86 {
87     std::vector<FirmwareComponent> components;
88     FirmwareComponentOperator().QueryAll(components);
89     return FirmwareUpdateHelper::IsUpgradePackagesReady(components);
90 }
91 } // namespace UpdateEngine
92 } // namespace OHOS
93