• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "update_service_impl_firmware.h"
17 
18 #include <string>
19 
20 #include "firmware_check_data_processor.h"
21 #include "firmware_constant.h"
22 #include "firmware_component_operator.h"
23 #include "firmware_log.h"
24 #include "firmware_manager.h"
25 #include "firmware_status_cache.h"
26 #include "firmware_task_operator.h"
27 #include "device_adapter.h"
28 #include "firmware_update_helper.h"
29 #include "string_utils.h"
30 #include "update_service_cache.h"
31 #include "update_service_util.h"
32 
33 namespace OHOS {
34 namespace UpdateEngine {
35 const std::string LANGUAGE_CHINESE = "<language name=\"zh-cn\" code=\"2052\">";
36 const std::string LANGUAGE_ENGLISH = "<language name=\"en-us\" code=\"1033\">";
37 const std::string LANGUAGE_END = "</language>";
38 
CheckNewVersion(const UpgradeInfo & info,BusinessError & businessError,CheckResult & checkResult)39 int32_t UpdateServiceImplFirmware::CheckNewVersion(const UpgradeInfo &info, BusinessError &businessError,
40     CheckResult &checkResult)
41 {
42     wptr<UpdateServiceImplFirmware> weakPtr(this);
43     FirmwareManager::GetInstance()->DoCheck(
44         [&, weakPtr](const BusinessError &error, const CheckResult &result) {
45             sptr<UpdateServiceImplFirmware> firmwareSptr = weakPtr.promote();
46             if (firmwareSptr == nullptr) {
47                 FIRMWARE_LOGE("UpdateServiceImplFirmware has been destructed, CheckNewVersion is TimeOut");
48                 return;
49             }
50             businessError = error;
51             checkResult = result;
52             firmwareSptr->checkComplete_ = true;
53             firmwareSptr->conditionVariable_.notify_all();
54         });
55     std::unique_lock<std::mutex> lock(checkNewVersionMutex_);
56     constexpr int32_t waitTime = 40;
57     conditionVariable_.wait_for(lock, std::chrono::seconds(waitTime), [&] { return checkComplete_; });
58     if (!checkComplete_) {
59         FIRMWARE_LOGE("CheckNewVersion is time out");
60         businessError.errorNum = CallResult::TIME_OUT;
61         businessError.message = "CheckNewVersion TimeOut";
62     }
63     weakPtr->checkComplete_ = false;
64     return INT_CALL_SUCCESS;
65 }
66 
Download(const UpgradeInfo & info,const VersionDigestInfo & versionDigestInfo,const DownloadOptions & downloadOptions,BusinessError & businessError)67 int32_t UpdateServiceImplFirmware::Download(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
68     const DownloadOptions &downloadOptions, BusinessError &businessError)
69 {
70     FIRMWARE_LOGI("Download allowNetwork:%{public}d order:%{public}d", CAST_INT(downloadOptions.allowNetwork),
71         CAST_INT(downloadOptions.order));
72     //控制1秒内重复点击下载
73     if (DelayedSingleton<FirmwareStatusCache>::GetInstance()->IsDownloadTriggered()) {
74         FIRMWARE_LOGI("on downloading, not perrmit repeat submmit");
75         businessError.Build(CallResult::FAIL, "repeat download error");
76         return INT_CALL_SUCCESS;
77     }
78 
79     FirmwareTask task;
80     FirmwareTaskOperator firmwareTaskOperator;
81     firmwareTaskOperator.QueryTask(task);
82     if (task.status != UpgradeStatus::CHECK_VERSION_SUCCESS) {
83         FIRMWARE_LOGI("download fail current status: %{public}d", CAST_INT(task.status));
84         businessError.Build(CallResult::FAIL, "download error");
85         return INT_CALL_SUCCESS;
86     }
87 
88     firmwareTaskOperator.UpdateDownloadOptionByTaskId(task.taskId,
89         DownloadMode::MANUAL, downloadOptions.allowNetwork, downloadOptions.order);
90     DelayedSingleton<FirmwareManager>::GetInstance()->DoDownload(downloadOptions, businessError);
91     return INT_CALL_SUCCESS;
92 }
93 
PauseDownload(const UpgradeInfo & info,const VersionDigestInfo & versionDigestInfo,const PauseDownloadOptions & pauseDownloadOptions,BusinessError & businessError)94 int32_t UpdateServiceImplFirmware::PauseDownload(const UpgradeInfo &info,
95     const VersionDigestInfo &versionDigestInfo, const PauseDownloadOptions &pauseDownloadOptions,
96     BusinessError &businessError)
97 {
98     FIRMWARE_LOGI("PauseDownload isAllowAutoResume:%{public}s",
99         StringUtils::GetBoolStr(pauseDownloadOptions.isAllowAutoResume).c_str());
100     businessError.Build(CallResult::UN_SUPPORT, "pause download not support");
101     return INT_CALL_SUCCESS;
102 }
103 
ResumeDownload(const UpgradeInfo & info,const VersionDigestInfo & versionDigestInfo,const ResumeDownloadOptions & resumeDownloadOptions,BusinessError & businessError)104 int32_t UpdateServiceImplFirmware::ResumeDownload(const UpgradeInfo &info,
105     const VersionDigestInfo &versionDigestInfo, const ResumeDownloadOptions &resumeDownloadOptions,
106     BusinessError &businessError)
107 {
108     FIRMWARE_LOGI("ResumeDownload allowNetwork:%{public}d", CAST_INT(resumeDownloadOptions.allowNetwork));
109     businessError.Build(CallResult::UN_SUPPORT, "resume download not support");
110     return INT_CALL_SUCCESS;
111 }
112 
113 
Upgrade(const UpgradeInfo & info,const VersionDigestInfo & versionDigestInfo,const UpgradeOptions & upgradeOptions,BusinessError & businessError)114 int32_t UpdateServiceImplFirmware::Upgrade(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
115     const UpgradeOptions &upgradeOptions, BusinessError &businessError)
116 {
117     FIRMWARE_LOGI("Upgrade order = %{public}d", CAST_INT(upgradeOptions.order));
118     FirmwareTask task;
119     FirmwareTaskOperator firmwareTaskOperator;
120     firmwareTaskOperator.QueryTask(task);
121     firmwareTaskOperator.UpdateUpgradeModeByTaskId(task.taskId, UpgradeMode::MANUAL);
122     DelayedSingleton<FirmwareManager>::GetInstance()->DoInstall(upgradeOptions, businessError,
123         FirmwareUpdateHelper::GetInstallType());
124     return INT_CALL_SUCCESS;
125 }
126 
ClearError(const UpgradeInfo & info,const VersionDigestInfo & versionDigestInfo,const ClearOptions & clearOptions,BusinessError & businessError)127 int32_t UpdateServiceImplFirmware::ClearError(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
128     const ClearOptions &clearOptions, BusinessError &businessError)
129 {
130     FIRMWARE_LOGI("ClearError, versionDigestInfo %{public}s ClearOptions %{public}d",
131         versionDigestInfo.versionDigest.c_str(),
132         CAST_INT(clearOptions.status));
133     DelayedSingleton<FirmwareManager>::GetInstance()->DoClearError(businessError);
134     return INT_CALL_SUCCESS;
135 }
136 
TerminateUpgrade(const UpgradeInfo & info,BusinessError & businessError)137 int32_t UpdateServiceImplFirmware::TerminateUpgrade(const UpgradeInfo &info, BusinessError &businessError)
138 {
139     FIRMWARE_LOGI("TerminateUpgrade");
140     DelayedSingleton<FirmwareManager>::GetInstance()->DoTerminateUpgrade(businessError);
141     return INT_CALL_SUCCESS;
142 }
143 
GetNewVersionInfo(const UpgradeInfo & info,NewVersionInfo & newVersionInfo,BusinessError & businessError)144 int32_t UpdateServiceImplFirmware::GetNewVersionInfo(
145     const UpgradeInfo &info, NewVersionInfo &newVersionInfo, BusinessError &businessError)
146 {
147     FIRMWARE_LOGI("GetNewVersionInfo");
148     FirmwareTask task;
149     FirmwareTaskOperator().QueryTask(task);
150     std::vector<FirmwareComponent> components;
151     FirmwareComponentOperator().QueryAll(components);
152     FirmwareUpdateHelper::BuildNewVersionInfo(components, newVersionInfo.versionComponents);
153     newVersionInfo.versionDigestInfo.versionDigest = task.taskId;
154     return INT_CALL_SUCCESS;
155 }
156 
GetNewVersionDescription(const UpgradeInfo & info,const VersionDigestInfo & versionDigestInfo,const DescriptionOptions & descriptionOptions,VersionDescriptionInfo & newVersionDescriptionInfo,BusinessError & businessError)157 int32_t UpdateServiceImplFirmware::GetNewVersionDescription(const UpgradeInfo &info,
158     const VersionDigestInfo &versionDigestInfo, const DescriptionOptions &descriptionOptions,
159     VersionDescriptionInfo &newVersionDescriptionInfo, BusinessError &businessError)
160 {
161     FIRMWARE_LOGI("GetNewVersionDescription versionDigestInfo %{public}s format %{public}d language %{public}s",
162         versionDigestInfo.versionDigest.c_str(),
163         CAST_INT(descriptionOptions.format),
164         descriptionOptions.language.c_str());
165 
166     businessError.Build(CallResult::SUCCESS, "start GetNewVersionDescription");
167     std::vector<FirmwareComponent> components;
168     FirmwareComponentOperator().QueryAll(components);
169     if (components.size() == 0) {
170         FIRMWARE_LOGI("GetNewVersionDescription: no data");
171         businessError.Build(CallResult::FAIL, "GetNewVersionDescription failed");
172         return INT_CALL_SUCCESS;
173     }
174 
175     for (auto const &component : components) {
176         ComponentDescription componentDescription;
177         componentDescription.componentId = component.componentId;
178         std::string changelogFilePath = Firmware::CHANGELOG_PATH + "/" + component.componentId + ".xml";
179         if (!FileUtils::IsFileExist(changelogFilePath)) {
180             FIRMWARE_LOGE("changelog file [%{public}s] is not exist!", changelogFilePath.c_str());
181             businessError.Build(CallResult::FAIL, "GetNewVersionDescription failed");
182             return INT_CALL_SUCCESS;
183         }
184         std::string dataXml = FileUtils::ReadDataFromFile(changelogFilePath);
185         std::string dataXmlFinal = dataXml.substr(dataXml.find_first_of("|") + 1, dataXml.size());
186         GetChangelogContent(dataXmlFinal, descriptionOptions.language);
187         componentDescription.descriptionInfo.content = dataXmlFinal;
188         componentDescription.descriptionInfo.descriptionType =
189             static_cast<DescriptionType>(atoi(dataXml.substr(0, dataXml.find_first_of("|")).c_str()));
190         newVersionDescriptionInfo.componentDescriptions.push_back(componentDescription);
191     }
192     return INT_CALL_SUCCESS;
193 }
194 
GetCurrentVersionInfo(const UpgradeInfo & info,CurrentVersionInfo & currentVersionInfo,BusinessError & businessError)195 int32_t UpdateServiceImplFirmware::GetCurrentVersionInfo(const UpgradeInfo &info,
196     CurrentVersionInfo &currentVersionInfo, BusinessError &businessError)
197 {
198     FIRMWARE_LOGI("UpdateServiceImplFirmware::GetCurrentVersionInfo");
199     businessError.errorNum = CallResult::SUCCESS;
200     FirmwareUpdateHelper::BuildCurrentVersionInfo(currentVersionInfo);
201     return INT_CALL_SUCCESS;
202 }
203 
GetCurrentVersionDescription(const UpgradeInfo & info,const DescriptionOptions & descriptionOptions,VersionDescriptionInfo & currentVersionDescriptionInfo,BusinessError & businessError)204 int32_t UpdateServiceImplFirmware::GetCurrentVersionDescription(const UpgradeInfo &info,
205     const DescriptionOptions &descriptionOptions, VersionDescriptionInfo &currentVersionDescriptionInfo,
206     BusinessError &businessError)
207 {
208     FIRMWARE_LOGI("GetCurrentVersionDescription format %{public}d language %{public}s",
209         CAST_INT(descriptionOptions.format),
210         descriptionOptions.language.c_str());
211 
212     ComponentDescription descriptionContent;
213     descriptionContent.componentId =
214         DelayedSingleton<FirmwarePreferencesUtil>::GetInstance()->ObtainString(Firmware::HOTA_CURRENT_COMPONENT_ID, "");
215     if (descriptionContent.componentId.empty()) {
216         businessError.Build(CallResult::FAIL, "GetCurrentVersionDescription failed");
217         FIRMWARE_LOGE("componentId is null");
218         return INT_CALL_SUCCESS;
219     }
220 
221     std::string changelogFilePath = Firmware::CHANGELOG_PATH + "/" + descriptionContent.componentId + ".xml";
222     if (!FileUtils::IsFileExist(changelogFilePath)) {
223         FIRMWARE_LOGE("current changelog file [%{public}s] is not exist!", changelogFilePath.c_str());
224         businessError.Build(CallResult::FAIL, "GetCurrentVersionDescription failed");
225         return INT_CALL_SUCCESS;
226     }
227     std::string dataXml = FileUtils::ReadDataFromFile(changelogFilePath);
228     std::string dataXmlFinal = dataXml.substr(dataXml.find_first_of("|") + 1, dataXml.size());
229     GetChangelogContent(dataXmlFinal, descriptionOptions.language);
230     descriptionContent.descriptionInfo.content = dataXmlFinal;
231     descriptionContent.descriptionInfo.descriptionType =
232         static_cast<DescriptionType>(atoi(dataXml.substr(0, dataXml.find_first_of("|")).c_str()));
233     currentVersionDescriptionInfo.componentDescriptions.push_back(descriptionContent);
234     businessError.Build(CallResult::SUCCESS, "GetCurrentVersionDescription ok");
235     return INT_CALL_SUCCESS;
236 }
237 
GetTaskInfo(const UpgradeInfo & info,TaskInfo & taskInfo,BusinessError & businessError)238 int32_t UpdateServiceImplFirmware::GetTaskInfo(const UpgradeInfo &info, TaskInfo &taskInfo,
239     BusinessError &businessError)
240 {
241     FIRMWARE_LOGI("GetTaskInfo");
242     businessError.errorNum = CallResult::SUCCESS;
243     FirmwareTask task;
244     FirmwareTaskOperator().QueryTask(task);
245     if (task.isExistTask) {
246         taskInfo.existTask = true;
247         taskInfo.taskBody.status = static_cast<UpgradeStatus>(task.status);
248         taskInfo.taskBody.progress = task.progress;
249         taskInfo.taskBody.versionDigestInfo.versionDigest = task.taskId;
250     }
251     FIRMWARE_LOGI("GetTaskInfo existTask %{public}s status %{public}d , progress %{public}d",
252         StringUtils::GetBoolStr(taskInfo.existTask).c_str(), CAST_INT(taskInfo.taskBody.status),
253         taskInfo.taskBody.progress);
254     return INT_CALL_SUCCESS;
255 }
256 
SetUpgradePolicy(const UpgradeInfo & info,const UpgradePolicy & policy,BusinessError & businessError)257 int32_t UpdateServiceImplFirmware::SetUpgradePolicy(const UpgradeInfo &info, const UpgradePolicy &policy,
258     BusinessError &businessError)
259 {
260     FIRMWARE_LOGI(
261         "SetUpgradePolicy autoDownload %{public}d installmode %{public}d startTime %{public}d endTime %{public}d",
262         policy.downloadStrategy, policy.autoUpgradeStrategy, policy.autoUpgradePeriods[0].start,
263         policy.autoUpgradePeriods[1].end);
264     businessError.errorNum = CallResult::SUCCESS;
265     bool isAutoDownloadSwitchOn = preferencesUtil_->ObtainBool(Firmware::AUTO_DOWNLOAD_SWITCH, false);
266     FIRMWARE_LOGI("SetUpgradePolicy isAutoDownloadSwitchOn %{public}s",
267         StringUtils::GetBoolStr(isAutoDownloadSwitchOn).c_str());
268     if (isAutoDownloadSwitchOn != policy.downloadStrategy) {
269         DelayedSingleton<FirmwareManager>::GetInstance()->DoAutoDownloadSwitchChanged(policy.downloadStrategy);
270     }
271     return INT_CALL_SUCCESS;
272 }
273 
GetUpgradePolicy(const UpgradeInfo & info,UpgradePolicy & policy,BusinessError & businessError)274 int32_t UpdateServiceImplFirmware::GetUpgradePolicy(const UpgradeInfo &info, UpgradePolicy &policy,
275     BusinessError &businessError)
276 {
277     FIRMWARE_LOGI("GetUpgradePolicy");
278     bool isAutoDownloadSwitchOn = preferencesUtil_->ObtainBool(Firmware::AUTO_DOWNLOAD_SWITCH, false);
279     FIRMWARE_LOGI("GetUpgradePolicy isAutoDownloadSwitchOn %{public}s",
280         StringUtils::GetBoolStr(isAutoDownloadSwitchOn).c_str());
281     policy.downloadStrategy = isAutoDownloadSwitchOn;
282     policy.autoUpgradePeriods[0].start =
283         static_cast<uint32_t>(Constant::ONE_HOUR_MINUTES * Firmware::NIGHT_UPGRADE_START_HOUR);
284     policy.autoUpgradePeriods[0].end =
285         static_cast<uint32_t>(Constant::ONE_HOUR_MINUTES * Firmware::NIGHT_UPGRADE_END_HOUR);
286     return INT_CALL_SUCCESS;
287 }
288 
Cancel(const UpgradeInfo & info,int32_t service,BusinessError & businessError)289 int32_t UpdateServiceImplFirmware::Cancel(const UpgradeInfo &info, int32_t service, BusinessError &businessError)
290 {
291     FIRMWARE_LOGI("Cancel %{public}d", service);
292     businessError.errorNum = CallResult::SUCCESS;
293     FirmwareTask task;
294     FirmwareTaskOperator().QueryTask(task);
295     if (task.status != UpgradeStatus::DOWNLOADING && task.status != UpgradeStatus::DOWNLOAD_PAUSE) {
296         FIRMWARE_LOGI("Cancel fail current status: %{public}d", CAST_INT(task.status));
297         businessError.Build(CallResult::FAIL, "Cancel download error");
298     } else {
299         DelayedSingleton<FirmwareManager>::GetInstance()->DoCancelDownload(businessError);
300     }
301     return INT_CALL_SUCCESS;
302 }
303 
GetChangelogContent(std::string & dataXml,const std::string & language)304 void UpdateServiceImplFirmware::GetChangelogContent(std::string &dataXml, const std::string &language)
305 {
306     std::string languageStart = LANGUAGE_ENGLISH;
307     if (language.compare("zh-cn") != 0) {
308         languageStart = LANGUAGE_CHINESE;
309     }
310     StringUtils::StringRemove(dataXml, languageStart, LANGUAGE_END);
311 }
312 } // namespace UpdateEngine
313 } // namespace OHOS
314