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_data_processor.h"
17 
18 #include "constant.h"
19 #include "firmware_callback_utils.h"
20 #include "firmware_component_operator.h"
21 #include "firmware_constant.h"
22 #include "firmware_file_utils.h"
23 #include "firmware_log.h"
24 #include "firmware_status_cache.h"
25 #include "firmware_task_operator.h"
26 #include "string_utils.h"
27 
28 namespace OHOS {
29 namespace UpdateEngine {
FirmwareDownloadDataProcessor()30 FirmwareDownloadDataProcessor::FirmwareDownloadDataProcessor()
31 {
32     FIRMWARE_LOGD("FirmwareDownloadDataProcessor::FirmwareDownloadDataProcessor");
33 }
34 
~FirmwareDownloadDataProcessor()35 FirmwareDownloadDataProcessor::~FirmwareDownloadDataProcessor()
36 {
37     FIRMWARE_LOGD("FirmwareDownloadDataProcessor::~FirmwareDownloadDataProcessor");
38 }
39 
IsDownloadSuccess()40 bool FirmwareDownloadDataProcessor::IsDownloadSuccess()
41 {
42     FirmwareTask task;
43     FirmwareTaskOperator().QueryTask(task);
44     if (!task.isExistTask) {
45         FIRMWARE_LOGI("IsDownloadSuccess no task");
46         return false;
47     }
48     FIRMWARE_LOGI("IsDownloadSuccess task status %{public}d", task.status);
49     return task.status == UpgradeStatus::DOWNLOAD_SUCCESS;
50 }
51 
IsSpaceEnough(int64_t & requireTotalSize)52 bool FirmwareDownloadDataProcessor::IsSpaceEnough(int64_t &requireTotalSize)
53 {
54     std::vector<FirmwareComponent> components;
55     FirmwareComponentOperator().QueryAll(components);
56     for (const FirmwareComponent &component : components) {
57         requireTotalSize += (Firmware::ONE_HUNDRED - component.progress) * component.size / Firmware::ONE_HUNDRED;
58     }
59     bool isDownloadSpaceEnough = FirmwareFileUtils::IsSpaceEnough(requireTotalSize);
60     FIRMWARE_LOGI("IsSpaceEnough %{public}s", StringUtils::GetBoolStr(isDownloadSpaceEnough).c_str());
61     return isDownloadSpaceEnough;
62 }
63 
GetTask()64 void FirmwareDownloadDataProcessor::GetTask()
65 {
66     if (!tasks_.isExistTask) {
67         FirmwareTaskOperator().QueryTask(tasks_);
68     }
69 }
70 
SetDownloadProgress(const Progress & progress)71 void FirmwareDownloadDataProcessor::SetDownloadProgress(const Progress &progress)
72 {
73     FIRMWARE_LOGI("SetDownloadProgress status: %{public}d progress: %{public}d", progress.status, progress.percent);
74     downloadProgress_ = progress;
75     GetTask();
76     DelayedSingleton<FirmwareCallbackUtils>::GetInstance()->ProgressCallback(tasks_.taskId, progress);
77 }
78 
GetDownloadProgress()79 Progress FirmwareDownloadDataProcessor::GetDownloadProgress()
80 {
81     return downloadProgress_;
82 }
83 } // namespace UpdateEngine
84 } // namespace OHOS
85