• 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 #include "dupdate_errno.h"
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     //控制1秒内重复点击下载
119     if (DelayedSingleton<FirmwareStatusCache>::GetInstance()->IsUpgradeTriggered()) {
120         FIRMWARE_LOGI("on upgrading, not perrmit repeat submmit");
121         businessError.Build(CallResult::FAIL, "repeat upgrade error");
122         return INT_CALL_SUCCESS;
123     }
124 
125     FirmwareTask task;
126     FirmwareTaskOperator firmwareTaskOperator;
127     firmwareTaskOperator.QueryTask(task);
128     firmwareTaskOperator.UpdateUpgradeModeByTaskId(task.taskId, UpgradeMode::MANUAL);
129     DelayedSingleton<FirmwareManager>::GetInstance()->DoInstall(upgradeOptions, businessError,
130         FirmwareUpdateHelper::GetInstallType());
131     return INT_CALL_SUCCESS;
132 }
133 
ClearError(const UpgradeInfo & info,const VersionDigestInfo & versionDigestInfo,const ClearOptions & clearOptions,BusinessError & businessError)134 int32_t UpdateServiceImplFirmware::ClearError(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
135     const ClearOptions &clearOptions, BusinessError &businessError)
136 {
137     FIRMWARE_LOGI("ClearError, versionDigestInfo %{public}s ClearOptions %{public}d",
138         versionDigestInfo.versionDigest.c_str(),
139         CAST_INT(clearOptions.status));
140     DelayedSingleton<FirmwareManager>::GetInstance()->DoClearError(businessError);
141     return INT_CALL_SUCCESS;
142 }
143 
TerminateUpgrade(const UpgradeInfo & info,BusinessError & businessError)144 int32_t UpdateServiceImplFirmware::TerminateUpgrade(const UpgradeInfo &info, BusinessError &businessError)
145 {
146     FIRMWARE_LOGI("TerminateUpgrade");
147     DelayedSingleton<FirmwareManager>::GetInstance()->DoTerminateUpgrade(businessError);
148     return INT_CALL_SUCCESS;
149 }
150 
GetNewVersionInfo(const UpgradeInfo & info,NewVersionInfo & newVersionInfo,BusinessError & businessError)151 int32_t UpdateServiceImplFirmware::GetNewVersionInfo(
152     const UpgradeInfo &info, NewVersionInfo &newVersionInfo, BusinessError &businessError)
153 {
154     FIRMWARE_LOGI("GetNewVersionInfo");
155     FirmwareTask task;
156     FirmwareTaskOperator().QueryTask(task);
157     std::vector<FirmwareComponent> components;
158     FirmwareComponentOperator().QueryAll(components);
159     FirmwareUpdateHelper::BuildNewVersionInfo(components, newVersionInfo.versionComponents);
160     newVersionInfo.versionDigestInfo.versionDigest = task.taskId;
161     return INT_CALL_SUCCESS;
162 }
163 
GetNewVersionDescription(const UpgradeInfo & info,const VersionDigestInfo & versionDigestInfo,const DescriptionOptions & descriptionOptions,VersionDescriptionInfo & newVersionDescriptionInfo,BusinessError & businessError)164 int32_t UpdateServiceImplFirmware::GetNewVersionDescription(const UpgradeInfo &info,
165     const VersionDigestInfo &versionDigestInfo, const DescriptionOptions &descriptionOptions,
166     VersionDescriptionInfo &newVersionDescriptionInfo, BusinessError &businessError)
167 {
168     FIRMWARE_LOGI("GetNewVersionDescription versionDigestInfo %{public}s format %{public}d language %{public}s",
169         versionDigestInfo.versionDigest.c_str(),
170         CAST_INT(descriptionOptions.format),
171         descriptionOptions.language.c_str());
172 
173     businessError.Build(CallResult::SUCCESS, "start GetNewVersionDescription");
174     std::vector<FirmwareComponent> components;
175     FirmwareComponentOperator().QueryAll(components);
176     if (components.size() == 0) {
177         FIRMWARE_LOGI("GetNewVersionDescription: no data");
178         businessError.Build(CallResult::FAIL, "GetNewVersionDescription failed");
179         return INT_CALL_SUCCESS;
180     }
181 
182     for (auto const &component : components) {
183         ComponentDescription componentDescription;
184         componentDescription.componentId = component.componentId;
185         std::string changelogFilePath = Firmware::CHANGELOG_PATH + "/" + component.componentId + ".xml";
186         if (!FileUtils::IsFileExist(changelogFilePath)) {
187             FIRMWARE_LOGE("changelog file [%{public}s] is not exist!", changelogFilePath.c_str());
188             businessError.Build(CallResult::FAIL, "GetNewVersionDescription failed");
189             return INT_CALL_SUCCESS;
190         }
191         std::string dataXml = FileUtils::ReadDataFromFile(changelogFilePath);
192         size_t startIndex = dataXml.find_first_of("|");
193         if (startIndex == std::string::npos) {
194             FIRMWARE_LOGE("dataXml not found |");
195             businessError.Build(CallResult::FAIL, "GetNewVersionDescription failed");
196             return INT_CALL_SUCCESS;
197         }
198         std::string dataXmlFinal = dataXml.substr(startIndex + 1, dataXml.size());
199         GetChangelogContent(dataXmlFinal, descriptionOptions.language);
200         componentDescription.descriptionInfo.content = dataXmlFinal;
201         componentDescription.descriptionInfo.descriptionType =
202             static_cast<DescriptionType>(atoi(dataXml.substr(0, dataXml.find_first_of("|")).c_str()));
203         newVersionDescriptionInfo.componentDescriptions.push_back(componentDescription);
204     }
205     return INT_CALL_SUCCESS;
206 }
207 
GetCurrentVersionInfo(const UpgradeInfo & info,CurrentVersionInfo & currentVersionInfo,BusinessError & businessError)208 int32_t UpdateServiceImplFirmware::GetCurrentVersionInfo(const UpgradeInfo &info,
209     CurrentVersionInfo &currentVersionInfo, BusinessError &businessError)
210 {
211     FIRMWARE_LOGI("UpdateServiceImplFirmware::GetCurrentVersionInfo");
212     businessError.errorNum = CallResult::SUCCESS;
213     FirmwareUpdateHelper::BuildCurrentVersionInfo(currentVersionInfo);
214     return INT_CALL_SUCCESS;
215 }
216 
GetCurrentVersionDescription(const UpgradeInfo & info,const DescriptionOptions & descriptionOptions,VersionDescriptionInfo & currentVersionDescriptionInfo,BusinessError & businessError)217 int32_t UpdateServiceImplFirmware::GetCurrentVersionDescription(const UpgradeInfo &info,
218     const DescriptionOptions &descriptionOptions, VersionDescriptionInfo &currentVersionDescriptionInfo,
219     BusinessError &businessError)
220 {
221     FIRMWARE_LOGI("GetCurrentVersionDescription format %{public}d language %{public}s",
222         CAST_INT(descriptionOptions.format),
223         descriptionOptions.language.c_str());
224 
225     ComponentDescription descriptionContent;
226     descriptionContent.componentId =
227         DelayedSingleton<FirmwarePreferencesUtil>::GetInstance()->ObtainString(Firmware::HOTA_CURRENT_COMPONENT_ID, "");
228     if (descriptionContent.componentId.empty()) {
229         businessError.Build(CallResult::FAIL, "GetCurrentVersionDescription failed");
230         FIRMWARE_LOGE("componentId is null");
231         return INT_CALL_SUCCESS;
232     }
233 
234     std::string changelogFilePath = Firmware::CHANGELOG_PATH + "/" + descriptionContent.componentId + ".xml";
235     if (!FileUtils::IsFileExist(changelogFilePath)) {
236         FIRMWARE_LOGE("current changelog file [%{public}s] is not exist!", changelogFilePath.c_str());
237         businessError.Build(CallResult::FAIL, "GetCurrentVersionDescription failed");
238         return INT_CALL_SUCCESS;
239     }
240     std::string dataXml = FileUtils::ReadDataFromFile(changelogFilePath);
241     size_t startIndex = dataXml.find_first_of("|");
242     if (startIndex == std::string::npos) {
243         FIRMWARE_LOGE("dataXml not found |");
244         businessError.Build(CallResult::FAIL, "GetCurrentVersionDescription failed");
245         return INT_CALL_SUCCESS;
246     }
247     std::string dataXmlFinal = dataXml.substr(startIndex + 1, dataXml.size());
248     GetChangelogContent(dataXmlFinal, descriptionOptions.language);
249     descriptionContent.descriptionInfo.content = dataXmlFinal;
250     descriptionContent.descriptionInfo.descriptionType =
251         static_cast<DescriptionType>(atoi(dataXml.substr(0, dataXml.find_first_of("|")).c_str()));
252     currentVersionDescriptionInfo.componentDescriptions.push_back(descriptionContent);
253     businessError.Build(CallResult::SUCCESS, "GetCurrentVersionDescription ok");
254     return INT_CALL_SUCCESS;
255 }
256 
GetTaskInfo(const UpgradeInfo & info,TaskInfo & taskInfo,BusinessError & businessError)257 int32_t UpdateServiceImplFirmware::GetTaskInfo(const UpgradeInfo &info, TaskInfo &taskInfo,
258     BusinessError &businessError)
259 {
260     FIRMWARE_LOGI("GetTaskInfo");
261     businessError.errorNum = CallResult::SUCCESS;
262     FirmwareTask task;
263     FirmwareTaskOperator().QueryTask(task);
264     if (task.isExistTask) {
265         taskInfo.existTask = true;
266         taskInfo.taskBody.status = static_cast<UpgradeStatus>(task.status);
267         taskInfo.taskBody.progress = task.progress;
268         taskInfo.taskBody.versionDigestInfo.versionDigest = task.taskId;
269     }
270     FIRMWARE_LOGI("GetTaskInfo existTask %{public}s status %{public}d , progress %{public}d",
271         StringUtils::GetBoolStr(taskInfo.existTask).c_str(), CAST_INT(taskInfo.taskBody.status),
272         taskInfo.taskBody.progress);
273     return INT_CALL_SUCCESS;
274 }
275 
SetUpgradePolicy(const UpgradeInfo & info,const UpgradePolicy & policy,BusinessError & businessError)276 int32_t UpdateServiceImplFirmware::SetUpgradePolicy(const UpgradeInfo &info, const UpgradePolicy &policy,
277     BusinessError &businessError)
278 {
279     FIRMWARE_LOGI(
280         "SetUpgradePolicy autoDownload %{public}d installmode %{public}d startTime %{public}d endTime %{public}d",
281         policy.downloadStrategy, policy.autoUpgradeStrategy, policy.autoUpgradePeriods[0].start,
282         policy.autoUpgradePeriods[1].end);
283     businessError.errorNum = CallResult::SUCCESS;
284     bool isAutoDownloadSwitchOn = preferencesUtil_->ObtainBool(Firmware::AUTO_DOWNLOAD_SWITCH, false);
285     FIRMWARE_LOGI("SetUpgradePolicy isAutoDownloadSwitchOn %{public}s",
286         StringUtils::GetBoolStr(isAutoDownloadSwitchOn).c_str());
287     if (isAutoDownloadSwitchOn != policy.downloadStrategy) {
288         DelayedSingleton<FirmwareManager>::GetInstance()->DoAutoDownloadSwitchChanged(policy.downloadStrategy);
289     }
290     return INT_CALL_SUCCESS;
291 }
292 
GetUpgradePolicy(const UpgradeInfo & info,UpgradePolicy & policy,BusinessError & businessError)293 int32_t UpdateServiceImplFirmware::GetUpgradePolicy(const UpgradeInfo &info, UpgradePolicy &policy,
294     BusinessError &businessError)
295 {
296     FIRMWARE_LOGI("GetUpgradePolicy");
297     bool isAutoDownloadSwitchOn = preferencesUtil_->ObtainBool(Firmware::AUTO_DOWNLOAD_SWITCH, false);
298     FIRMWARE_LOGI("GetUpgradePolicy isAutoDownloadSwitchOn %{public}s",
299         StringUtils::GetBoolStr(isAutoDownloadSwitchOn).c_str());
300     policy.downloadStrategy = isAutoDownloadSwitchOn;
301     policy.autoUpgradePeriods[0].start =
302         static_cast<uint32_t>(Constant::ONE_HOUR_MINUTES * Firmware::NIGHT_UPGRADE_START_HOUR);
303     policy.autoUpgradePeriods[0].end =
304         static_cast<uint32_t>(Constant::ONE_HOUR_MINUTES * Firmware::NIGHT_UPGRADE_END_HOUR);
305     return INT_CALL_SUCCESS;
306 }
307 
Cancel(const UpgradeInfo & info,int32_t service,BusinessError & businessError)308 int32_t UpdateServiceImplFirmware::Cancel(const UpgradeInfo &info, int32_t service, BusinessError &businessError)
309 {
310     FIRMWARE_LOGI("Cancel %{public}d", service);
311     businessError.errorNum = CallResult::SUCCESS;
312     FirmwareTask task;
313     FirmwareTaskOperator().QueryTask(task);
314     if (FirmwareUpdateHelper::IsStreamUpgrade()) {
315         if (task.status != UpgradeStatus::INSTALLING) {
316             FIRMWARE_LOGI("Cancel fail current status: %{public}d", CAST_INT(task.status));
317             businessError.Build(CallResult::FAIL, "Cancel install error");
318             businessError.AddErrorMessage(CAST_INT(DUPDATE_ERR_CANCEL_TASK_ERROR), "no install task to cancel!");
319         } else {
320             DelayedSingleton<FirmwareManager>::GetInstance()->DoCancel(businessError);
321         }
322     } else {
323         if (task.status != UpgradeStatus::DOWNLOADING && task.status != UpgradeStatus::DOWNLOAD_PAUSE) {
324             FIRMWARE_LOGI("Cancel fail current status: %{public}d", CAST_INT(task.status));
325             businessError.Build(CallResult::FAIL, "Cancel download error");
326             businessError.AddErrorMessage(CAST_INT(DUPDATE_ERR_CANCEL_TASK_ERROR), "no download task to cancel!");
327         } else {
328             DelayedSingleton<FirmwareManager>::GetInstance()->DoCancel(businessError);
329         }
330     }
331 
332     return INT_CALL_SUCCESS;
333 }
334 
GetChangelogContent(std::string & dataXml,const std::string & language)335 void UpdateServiceImplFirmware::GetChangelogContent(std::string &dataXml, const std::string &language)
336 {
337     std::string languageStart = LANGUAGE_ENGLISH;
338     if (language.compare("zh-cn") != 0) {
339         languageStart = LANGUAGE_CHINESE;
340     }
341     StringUtils::StringRemove(dataXml, languageStart, LANGUAGE_END);
342 }
343 } // namespace UpdateEngine
344 } // namespace OHOS
345