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 
28 namespace OHOS {
29 namespace UpdateEngine {
Execute()30 void FirmwareInstallExecutor::Execute()
31 {
32     FIRMWARE_LOGI("FirmwareInstallExecutor::Execute");
33     std::thread installThread([this] { this->DoInstall(); });
34     installThread.detach();
35 }
36 
DoInstall()37 void FirmwareInstallExecutor::DoInstall()
38 {
39     FirmwareComponentOperator().QueryAll(components_);
40     FIRMWARE_LOGI("FirmwareInstallExecutor DoInstall installType: %{public}d, component num: %{public}d",
41         CAST_INT(installType_), CAST_INT(components_.size()));
42     if (components_.size() == 0) {
43         Progress progress;
44         progress.status = UpgradeStatus::UPDATE_FAIL;
45         progress.endReason = "no task";
46         installCallbackInfo_.progress = progress;
47         if (installCallback_.installCallback == nullptr) {
48             FIRMWARE_LOGE("FirmwareInstallExecutor DoInstall installCallback is null");
49             return;
50         }
51         installCallback_.installCallback(installCallbackInfo_);
52         return;
53     }
54 
55     GetTask();
56     Progress progress;
57     if (installType_ == InstallType::SYS_INSTALLER) {
58         progress.status = UpgradeStatus::INSTALLING;
59     } else if (installType_ == InstallType::UPDATER) {
60         progress.status = UpgradeStatus::UPDATING;
61     } else {
62         FIRMWARE_LOGI("installType:%{public}d is illegal", CAST_INT(installType_));
63     }
64 
65     FirmwareTaskOperator().UpdateProgressByTaskId(tasks_.taskId, progress.status, progress.percent);
66     for (FirmwareComponent &component : components_) {
67         FirmwareComponentOperator().UpdateProgressByUrl(component.url, progress.status, progress.percent);
68     }
69 
70     FirmwareInstallCallback cb {[=](const FirmwareComponent &component) {
71                                     Progress progress;
72                                     progress.status = component.status;
73                                     progress.percent = component.progress;
74                                     HandleInstallProgress(component, progress);
75                                 },
76         [=](const bool result, const ErrorMessage &errMsg) {
77             HandleInstallResult(result, errMsg);
78         },
79         [=](const UpgradeStatus &status) {
80             FIRMWARE_LOGI("update start status :%{public}d", CAST_INT(status));
81             DelayedSingleton<FirmwareCallbackUtils>::GetInstance()->NotifyEvent(tasks_.taskId,
82                 EventId::EVENT_UPGRADE_START, status);
83         }};
84 
85     std::shared_ptr<FirmwareInstall> executor = InstallFactory::GetInstance(installType_);
86     if (executor == nullptr) {
87         FIRMWARE_LOGE("get install pointer fail");
88         return;
89     }
90     executor->StartInstall(components_, cb);
91 }
92 
GetTask()93 void FirmwareInstallExecutor::GetTask()
94 {
95     if (!tasks_.isExistTask) {
96         FirmwareTaskOperator().QueryTask(tasks_);
97     }
98 }
99 
HandleInstallProgress(const FirmwareComponent & component,const Progress & progress)100 void FirmwareInstallExecutor::HandleInstallProgress(const FirmwareComponent &component, const Progress &progress)
101 {
102     FIRMWARE_LOGI("UpdateCallback versionId %{public}s status %{public}d progress %{public}d",
103         component.versionId.c_str(), progress.status, progress.percent);
104     FirmwareComponentOperator().UpdateProgressByUrl(component.url, progress.status, progress.percent);
105 
106     // 避免安装失败重复提交事件, 进度回调状态置为安装中
107     taskProgress_.status = UpgradeStatus::INSTALLING;
108     taskProgress_.percent = progress.percent;
109 
110     // 整体进度插入到 task 表
111     FirmwareTaskOperator().UpdateProgressByTaskId(tasks_.taskId, taskProgress_.status, taskProgress_.percent);
112     installCallbackInfo_.progress = taskProgress_;
113     if (installCallback_.installCallback == nullptr) {
114         FIRMWARE_LOGE("FirmwareInstallExecutor HandleInstallProgress installCallback is null");
115         return;
116     }
117     installCallback_.installCallback(installCallbackInfo_);
118 }
119 
HandleInstallResult(const bool result,const ErrorMessage & errMsg)120 void FirmwareInstallExecutor::HandleInstallResult(const bool result, const ErrorMessage &errMsg)
121 {
122     FIRMWARE_LOGI("FirmwareInstallExecutor::HandleInstallResult, result =%{public}d", result);
123     if (result) {
124         taskProgress_.status = UpgradeStatus::INSTALL_SUCCESS;
125         taskProgress_.percent = Firmware::ONE_HUNDRED;
126     } else {
127         if (installType_ == InstallType::SYS_INSTALLER) {
128             taskProgress_.status = UpgradeStatus::INSTALL_FAIL;
129         } else if (installType_ == InstallType::UPDATER) {
130             taskProgress_.status = UpgradeStatus::UPDATE_FAIL;
131         }
132     }
133     // 整体进度插入到 task 表
134     FirmwareTaskOperator().UpdateProgressByTaskId(tasks_.taskId, taskProgress_.status, taskProgress_.percent);
135     installCallbackInfo_.progress = taskProgress_;
136     installCallbackInfo_.errorMessage.errorCode = errMsg.errorCode;
137     installCallbackInfo_.errorMessage.errorMessage = errMsg.errorMessage;
138     if (installCallback_.installCallback == nullptr) {
139         FIRMWARE_LOGE("FirmwareInstallExecutor HandleInstallResult installCallback is null");
140         return;
141     }
142     installCallback_.installCallback(installCallbackInfo_);
143 }
144 } // namespace UpdateEngine
145 } // namespace OHOS
146