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_executor.h"
17
18 #include <thread>
19
20 #include "event_id.h"
21 #include "firmware_callback_utils.h"
22 #include "firmware_component_operator.h"
23 #include "firmware_constant.h"
24 #include "firmware_install_factory.h"
25 #include "firmware_log.h"
26 #include "firmware_task_operator.h"
27 #include "firmware_update_helper.h"
28
29 namespace OHOS {
30 namespace UpdateService {
Execute()31 void FirmwareInstallExecutor::Execute()
32 {
33 FIRMWARE_LOGI("FirmwareInstallExecutor::Execute");
34 std::thread installThread([this] { this->DoInstall(); });
35 installThread.detach();
36 }
37
DoInstall()38 void FirmwareInstallExecutor::DoInstall()
39 {
40 FirmwareComponentOperator().QueryAll(components_);
41 FIRMWARE_LOGI("FirmwareInstallExecutor DoInstall installType: %{public}d, component num: %{public}d",
42 CAST_INT(installType_), CAST_INT(components_.size()));
43 if (components_.size() == 0) {
44 Progress progress;
45 if (installType_ == InstallType::SYS_INSTALLER || installType_ == InstallType::STREAM_INSTALLLER) {
46 progress.status = UpgradeStatus::INSTALL_FAIL;
47 } else {
48 progress.status = UpgradeStatus::UPDATE_FAIL;
49 }
50
51 progress.endReason = "no task";
52 installCallbackInfo_.progress = progress;
53 if (installCallback_.installCallback == nullptr) {
54 FIRMWARE_LOGE("FirmwareInstallExecutor DoInstall installCallback is null");
55 return;
56 }
57 installCallback_.installCallback(installCallbackInfo_);
58 return;
59 }
60
61 GetTask();
62 Progress progress;
63 if (installType_ == InstallType::SYS_INSTALLER || installType_ == InstallType::STREAM_INSTALLLER) {
64 progress.status = UpgradeStatus::INSTALLING;
65 } else if (installType_ == InstallType::UPDATER) {
66 progress.status = UpgradeStatus::UPDATING;
67 } else {
68 FIRMWARE_LOGI("installType:%{public}d is illegal", CAST_INT(installType_));
69 }
70
71 FirmwareTaskOperator().UpdateProgressByTaskId(tasks_.taskId, progress.status, progress.percent);
72 for (FirmwareComponent &component : components_) {
73 FirmwareComponentOperator().UpdateProgressByUrl(component.url, progress.status, progress.percent);
74 }
75
76 StartInstall();
77 }
78
StartInstall()79 void FirmwareInstallExecutor::StartInstall()
80 {
81 FirmwareInstallCallback cb{
82 [=](const FirmwareComponent &component) {
83 HandleInstallProgress(component);
84 },
85 [=](bool result, const ErrorMessage &errMsg, UpgradeStatus status) {
86 HandleInstallResult(result, errMsg, status);
87 },
88 [=](UpgradeStatus status) {
89 FIRMWARE_LOGI("update start status :%{public}d", CAST_INT(status));
90 DelayedSingleton<FirmwareCallbackUtils>::GetInstance()->NotifyEvent(tasks_.taskId,
91 EventId::EVENT_UPGRADE_START, status);
92 }
93 };
94
95 std::shared_ptr<FirmwareInstall> executor = InstallFactory::GetInstance(installType_);
96 if (executor == nullptr) {
97 FIRMWARE_LOGE("get install pointer fail");
98 return;
99 }
100 executor->StartInstall(components_, cb);
101 }
102
GetTask()103 void FirmwareInstallExecutor::GetTask()
104 {
105 if (!tasks_.isExistTask) {
106 FirmwareTaskOperator().QueryTask(tasks_);
107 }
108 }
109
HandleInstallProgress(const FirmwareComponent & component)110 void FirmwareInstallExecutor::HandleInstallProgress(const FirmwareComponent &component)
111 {
112 FIRMWARE_LOGI("FirmwareInstallExecutor::HandleInstallProgress status:%{public}d,progress:%{public}d, "
113 "recordPoint:%{public}" PRId64, component.status, component.progress, component.recordPoint);
114 FirmwareComponentOperator().UpdateProgressByUrl(component.url, component.status, component.progress);
115 // 避免安装失败重复提交事件, 进度回调状态置为安装中
116 taskProgress_.status = UpgradeStatus::INSTALLING;
117 taskProgress_.percent = component.progress;
118 installCallbackInfo_.progress = taskProgress_;
119 if (installCallback_.installCallback == nullptr) {
120 FIRMWARE_LOGE("FirmwareInstallExecutor HandleInstallProgress installCallback is null");
121 return;
122 }
123 //流式升级只更新recordPoint还原点不实时更新task的status,避免异步安装过程覆盖结果
124 if (installType_ == InstallType::STREAM_INSTALLLER) {
125 FirmwareComponentOperator().UpdateRecordPointByUrl(component.url, component.recordPoint);
126 } else {
127 FirmwareTaskOperator().UpdateProgressByTaskId(tasks_.taskId, taskProgress_.status, taskProgress_.percent);
128 }
129
130 installCallback_.installCallback(installCallbackInfo_);
131 }
132
HandleInstallResult(const bool result,const ErrorMessage & errMsg,const UpgradeStatus & status)133 void FirmwareInstallExecutor::HandleInstallResult(const bool result, const ErrorMessage &errMsg,
134 const UpgradeStatus &status)
135 {
136 FIRMWARE_LOGI("FirmwareInstallExecutor::HandleInstallResult, result =%{public}d", result);
137 if (result) {
138 taskProgress_.status = UpgradeStatus::INSTALL_SUCCESS;
139 taskProgress_.percent = Firmware::ONE_HUNDRED;
140 } else {
141 if (installType_ == InstallType::SYS_INSTALLER) {
142 taskProgress_.status = UpgradeStatus::INSTALL_FAIL;
143 } else if (installType_ == InstallType::STREAM_INSTALLLER) {
144 taskProgress_.status = status == UpgradeStatus::DOWNLOAD_CANCEL ?
145 UpgradeStatus::INSTALL_PAUSE : UpgradeStatus::INSTALL_FAIL;
146 } else {
147 taskProgress_.status = UpgradeStatus::UPDATE_FAIL;
148 }
149 }
150 FIRMWARE_LOGI("HandleInstallResult status: %{public}d progress: %{public}d",
151 taskProgress_.status, taskProgress_.percent);
152 // 整体进度插入到 task 表
153 FirmwareTaskOperator().UpdateProgressByTaskId(tasks_.taskId, taskProgress_.status, taskProgress_.percent);
154 installCallbackInfo_.progress = taskProgress_;
155 installCallbackInfo_.errorMessage.errorCode = errMsg.errorCode;
156 installCallbackInfo_.errorMessage.errorMessage = errMsg.errorMessage;
157 if (installCallback_.installCallback == nullptr) {
158 FIRMWARE_LOGE("FirmwareInstallExecutor HandleInstallResult installCallback is null");
159 return;
160 }
161 installCallback_.installCallback(installCallbackInfo_);
162 }
163
164 } // namespace UpdateService
165 } // namespace OHOS
166