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