1190978c3Sopenharmony_ci/*
2190978c3Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3190978c3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4190978c3Sopenharmony_ci * you may not use this file except in compliance with the License.
5190978c3Sopenharmony_ci * You may obtain a copy of the License at
6190978c3Sopenharmony_ci *
7190978c3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8190978c3Sopenharmony_ci *
9190978c3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10190978c3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11190978c3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12190978c3Sopenharmony_ci * See the License for the specific language governing permissions and
13190978c3Sopenharmony_ci * limitations under the License.
14190978c3Sopenharmony_ci */
15190978c3Sopenharmony_ci
16190978c3Sopenharmony_ci#include "update_service_impl_firmware.h"
17190978c3Sopenharmony_ci
18190978c3Sopenharmony_ci#include <string>
19190978c3Sopenharmony_ci
20190978c3Sopenharmony_ci#include "firmware_check_data_processor.h"
21190978c3Sopenharmony_ci#include "firmware_constant.h"
22190978c3Sopenharmony_ci#include "firmware_component_operator.h"
23190978c3Sopenharmony_ci#include "firmware_log.h"
24190978c3Sopenharmony_ci#include "firmware_manager.h"
25190978c3Sopenharmony_ci#include "firmware_status_cache.h"
26190978c3Sopenharmony_ci#include "firmware_task_operator.h"
27190978c3Sopenharmony_ci#include "device_adapter.h"
28190978c3Sopenharmony_ci#include "firmware_update_helper.h"
29190978c3Sopenharmony_ci#include "string_utils.h"
30190978c3Sopenharmony_ci#include "update_service_cache.h"
31190978c3Sopenharmony_ci#include "update_service_util.h"
32190978c3Sopenharmony_ci
33190978c3Sopenharmony_cinamespace OHOS {
34190978c3Sopenharmony_cinamespace UpdateEngine {
35190978c3Sopenharmony_ciconst std::string LANGUAGE_CHINESE = "<language name=\"zh-cn\" code=\"2052\">";
36190978c3Sopenharmony_ciconst std::string LANGUAGE_ENGLISH = "<language name=\"en-us\" code=\"1033\">";
37190978c3Sopenharmony_ciconst std::string LANGUAGE_END = "</language>";
38190978c3Sopenharmony_ci
39190978c3Sopenharmony_ciint32_t UpdateServiceImplFirmware::CheckNewVersion(const UpgradeInfo &info, BusinessError &businessError,
40190978c3Sopenharmony_ci    CheckResult &checkResult)
41190978c3Sopenharmony_ci{
42190978c3Sopenharmony_ci    wptr<UpdateServiceImplFirmware> weakPtr(this);
43190978c3Sopenharmony_ci    FirmwareManager::GetInstance()->DoCheck(
44190978c3Sopenharmony_ci        [&, weakPtr](const BusinessError &error, const CheckResult &result) {
45190978c3Sopenharmony_ci            sptr<UpdateServiceImplFirmware> firmwareSptr = weakPtr.promote();
46190978c3Sopenharmony_ci            if (firmwareSptr == nullptr) {
47190978c3Sopenharmony_ci                FIRMWARE_LOGE("UpdateServiceImplFirmware has been destructed, CheckNewVersion is TimeOut");
48190978c3Sopenharmony_ci                return;
49190978c3Sopenharmony_ci            }
50190978c3Sopenharmony_ci            businessError = error;
51190978c3Sopenharmony_ci            checkResult = result;
52190978c3Sopenharmony_ci            firmwareSptr->checkComplete_ = true;
53190978c3Sopenharmony_ci            firmwareSptr->conditionVariable_.notify_all();
54190978c3Sopenharmony_ci        });
55190978c3Sopenharmony_ci    std::unique_lock<std::mutex> lock(checkNewVersionMutex_);
56190978c3Sopenharmony_ci    constexpr int32_t waitTime = 40;
57190978c3Sopenharmony_ci    conditionVariable_.wait_for(lock, std::chrono::seconds(waitTime), [&] { return checkComplete_; });
58190978c3Sopenharmony_ci    if (!checkComplete_) {
59190978c3Sopenharmony_ci        FIRMWARE_LOGE("CheckNewVersion is time out");
60190978c3Sopenharmony_ci        businessError.errorNum = CallResult::TIME_OUT;
61190978c3Sopenharmony_ci        businessError.message = "CheckNewVersion TimeOut";
62190978c3Sopenharmony_ci    }
63190978c3Sopenharmony_ci    weakPtr->checkComplete_ = false;
64190978c3Sopenharmony_ci    return INT_CALL_SUCCESS;
65190978c3Sopenharmony_ci}
66190978c3Sopenharmony_ci
67190978c3Sopenharmony_ciint32_t UpdateServiceImplFirmware::Download(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
68190978c3Sopenharmony_ci    const DownloadOptions &downloadOptions, BusinessError &businessError)
69190978c3Sopenharmony_ci{
70190978c3Sopenharmony_ci    FIRMWARE_LOGI("Download allowNetwork:%{public}d order:%{public}d", CAST_INT(downloadOptions.allowNetwork),
71190978c3Sopenharmony_ci        CAST_INT(downloadOptions.order));
72190978c3Sopenharmony_ci    //控制1秒内重复点击下载
73190978c3Sopenharmony_ci    if (DelayedSingleton<FirmwareStatusCache>::GetInstance()->IsDownloadTriggered()) {
74190978c3Sopenharmony_ci        FIRMWARE_LOGI("on downloading, not perrmit repeat submmit");
75190978c3Sopenharmony_ci        businessError.Build(CallResult::FAIL, "repeat download error");
76190978c3Sopenharmony_ci        return INT_CALL_SUCCESS;
77190978c3Sopenharmony_ci    }
78190978c3Sopenharmony_ci
79190978c3Sopenharmony_ci    FirmwareTask task;
80190978c3Sopenharmony_ci    FirmwareTaskOperator firmwareTaskOperator;
81190978c3Sopenharmony_ci    firmwareTaskOperator.QueryTask(task);
82190978c3Sopenharmony_ci    if (task.status != UpgradeStatus::CHECK_VERSION_SUCCESS) {
83190978c3Sopenharmony_ci        FIRMWARE_LOGI("download fail current status: %{public}d", CAST_INT(task.status));
84190978c3Sopenharmony_ci        businessError.Build(CallResult::FAIL, "download error");
85190978c3Sopenharmony_ci        return INT_CALL_SUCCESS;
86190978c3Sopenharmony_ci    }
87190978c3Sopenharmony_ci
88190978c3Sopenharmony_ci    firmwareTaskOperator.UpdateDownloadOptionByTaskId(task.taskId,
89190978c3Sopenharmony_ci        DownloadMode::MANUAL, downloadOptions.allowNetwork, downloadOptions.order);
90190978c3Sopenharmony_ci    DelayedSingleton<FirmwareManager>::GetInstance()->DoDownload(downloadOptions, businessError);
91190978c3Sopenharmony_ci    return INT_CALL_SUCCESS;
92190978c3Sopenharmony_ci}
93190978c3Sopenharmony_ci
94190978c3Sopenharmony_ciint32_t UpdateServiceImplFirmware::PauseDownload(const UpgradeInfo &info,
95190978c3Sopenharmony_ci    const VersionDigestInfo &versionDigestInfo, const PauseDownloadOptions &pauseDownloadOptions,
96190978c3Sopenharmony_ci    BusinessError &businessError)
97190978c3Sopenharmony_ci{
98190978c3Sopenharmony_ci    FIRMWARE_LOGI("PauseDownload isAllowAutoResume:%{public}s",
99190978c3Sopenharmony_ci        StringUtils::GetBoolStr(pauseDownloadOptions.isAllowAutoResume).c_str());
100190978c3Sopenharmony_ci    businessError.Build(CallResult::UN_SUPPORT, "pause download not support");
101190978c3Sopenharmony_ci    return INT_CALL_SUCCESS;
102190978c3Sopenharmony_ci}
103190978c3Sopenharmony_ci
104190978c3Sopenharmony_ciint32_t UpdateServiceImplFirmware::ResumeDownload(const UpgradeInfo &info,
105190978c3Sopenharmony_ci    const VersionDigestInfo &versionDigestInfo, const ResumeDownloadOptions &resumeDownloadOptions,
106190978c3Sopenharmony_ci    BusinessError &businessError)
107190978c3Sopenharmony_ci{
108190978c3Sopenharmony_ci    FIRMWARE_LOGI("ResumeDownload allowNetwork:%{public}d", CAST_INT(resumeDownloadOptions.allowNetwork));
109190978c3Sopenharmony_ci    businessError.Build(CallResult::UN_SUPPORT, "resume download not support");
110190978c3Sopenharmony_ci    return INT_CALL_SUCCESS;
111190978c3Sopenharmony_ci}
112190978c3Sopenharmony_ci
113190978c3Sopenharmony_ci
114190978c3Sopenharmony_ciint32_t UpdateServiceImplFirmware::Upgrade(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
115190978c3Sopenharmony_ci    const UpgradeOptions &upgradeOptions, BusinessError &businessError)
116190978c3Sopenharmony_ci{
117190978c3Sopenharmony_ci    FIRMWARE_LOGI("Upgrade order = %{public}d", CAST_INT(upgradeOptions.order));
118190978c3Sopenharmony_ci    FirmwareTask task;
119190978c3Sopenharmony_ci    FirmwareTaskOperator firmwareTaskOperator;
120190978c3Sopenharmony_ci    firmwareTaskOperator.QueryTask(task);
121190978c3Sopenharmony_ci    firmwareTaskOperator.UpdateUpgradeModeByTaskId(task.taskId, UpgradeMode::MANUAL);
122190978c3Sopenharmony_ci    DelayedSingleton<FirmwareManager>::GetInstance()->DoInstall(upgradeOptions, businessError,
123190978c3Sopenharmony_ci        FirmwareUpdateHelper::GetInstallType());
124190978c3Sopenharmony_ci    return INT_CALL_SUCCESS;
125190978c3Sopenharmony_ci}
126190978c3Sopenharmony_ci
127190978c3Sopenharmony_ciint32_t UpdateServiceImplFirmware::ClearError(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
128190978c3Sopenharmony_ci    const ClearOptions &clearOptions, BusinessError &businessError)
129190978c3Sopenharmony_ci{
130190978c3Sopenharmony_ci    FIRMWARE_LOGI("ClearError, versionDigestInfo %{public}s ClearOptions %{public}d",
131190978c3Sopenharmony_ci        versionDigestInfo.versionDigest.c_str(),
132190978c3Sopenharmony_ci        CAST_INT(clearOptions.status));
133190978c3Sopenharmony_ci    DelayedSingleton<FirmwareManager>::GetInstance()->DoClearError(businessError);
134190978c3Sopenharmony_ci    return INT_CALL_SUCCESS;
135190978c3Sopenharmony_ci}
136190978c3Sopenharmony_ci
137190978c3Sopenharmony_ciint32_t UpdateServiceImplFirmware::TerminateUpgrade(const UpgradeInfo &info, BusinessError &businessError)
138190978c3Sopenharmony_ci{
139190978c3Sopenharmony_ci    FIRMWARE_LOGI("TerminateUpgrade");
140190978c3Sopenharmony_ci    DelayedSingleton<FirmwareManager>::GetInstance()->DoTerminateUpgrade(businessError);
141190978c3Sopenharmony_ci    return INT_CALL_SUCCESS;
142190978c3Sopenharmony_ci}
143190978c3Sopenharmony_ci
144190978c3Sopenharmony_ciint32_t UpdateServiceImplFirmware::GetNewVersionInfo(
145190978c3Sopenharmony_ci    const UpgradeInfo &info, NewVersionInfo &newVersionInfo, BusinessError &businessError)
146190978c3Sopenharmony_ci{
147190978c3Sopenharmony_ci    FIRMWARE_LOGI("GetNewVersionInfo");
148190978c3Sopenharmony_ci    FirmwareTask task;
149190978c3Sopenharmony_ci    FirmwareTaskOperator().QueryTask(task);
150190978c3Sopenharmony_ci    std::vector<FirmwareComponent> components;
151190978c3Sopenharmony_ci    FirmwareComponentOperator().QueryAll(components);
152190978c3Sopenharmony_ci    FirmwareUpdateHelper::BuildNewVersionInfo(components, newVersionInfo.versionComponents);
153190978c3Sopenharmony_ci    newVersionInfo.versionDigestInfo.versionDigest = task.taskId;
154190978c3Sopenharmony_ci    return INT_CALL_SUCCESS;
155190978c3Sopenharmony_ci}
156190978c3Sopenharmony_ci
157190978c3Sopenharmony_ciint32_t UpdateServiceImplFirmware::GetNewVersionDescription(const UpgradeInfo &info,
158190978c3Sopenharmony_ci    const VersionDigestInfo &versionDigestInfo, const DescriptionOptions &descriptionOptions,
159190978c3Sopenharmony_ci    VersionDescriptionInfo &newVersionDescriptionInfo, BusinessError &businessError)
160190978c3Sopenharmony_ci{
161190978c3Sopenharmony_ci    FIRMWARE_LOGI("GetNewVersionDescription versionDigestInfo %{public}s format %{public}d language %{public}s",
162190978c3Sopenharmony_ci        versionDigestInfo.versionDigest.c_str(),
163190978c3Sopenharmony_ci        CAST_INT(descriptionOptions.format),
164190978c3Sopenharmony_ci        descriptionOptions.language.c_str());
165190978c3Sopenharmony_ci
166190978c3Sopenharmony_ci    businessError.Build(CallResult::SUCCESS, "start GetNewVersionDescription");
167190978c3Sopenharmony_ci    std::vector<FirmwareComponent> components;
168190978c3Sopenharmony_ci    FirmwareComponentOperator().QueryAll(components);
169190978c3Sopenharmony_ci    if (components.size() == 0) {
170190978c3Sopenharmony_ci        FIRMWARE_LOGI("GetNewVersionDescription: no data");
171190978c3Sopenharmony_ci        businessError.Build(CallResult::FAIL, "GetNewVersionDescription failed");
172190978c3Sopenharmony_ci        return INT_CALL_SUCCESS;
173190978c3Sopenharmony_ci    }
174190978c3Sopenharmony_ci
175190978c3Sopenharmony_ci    for (auto const &component : components) {
176190978c3Sopenharmony_ci        ComponentDescription componentDescription;
177190978c3Sopenharmony_ci        componentDescription.componentId = component.componentId;
178190978c3Sopenharmony_ci        std::string changelogFilePath = Firmware::CHANGELOG_PATH + "/" + component.componentId + ".xml";
179190978c3Sopenharmony_ci        if (!FileUtils::IsFileExist(changelogFilePath)) {
180190978c3Sopenharmony_ci            FIRMWARE_LOGE("changelog file [%{public}s] is not exist!", changelogFilePath.c_str());
181190978c3Sopenharmony_ci            businessError.Build(CallResult::FAIL, "GetNewVersionDescription failed");
182190978c3Sopenharmony_ci            return INT_CALL_SUCCESS;
183190978c3Sopenharmony_ci        }
184190978c3Sopenharmony_ci        std::string dataXml = FileUtils::ReadDataFromFile(changelogFilePath);
185190978c3Sopenharmony_ci        size_t startIndex = dataXml.find_first_of("|");
186190978c3Sopenharmony_ci        if (startIndex == std::string::npos) {
187190978c3Sopenharmony_ci            FIRMWARE_LOGE("dataXml not found |");
188190978c3Sopenharmony_ci            businessError.Build(CallResult::FAIL, "GetNewVersionDescription failed");
189190978c3Sopenharmony_ci            return INT_CALL_SUCCESS;
190190978c3Sopenharmony_ci        }
191190978c3Sopenharmony_ci        std::string dataXmlFinal = dataXml.substr(startIndex + 1, dataXml.size());
192190978c3Sopenharmony_ci        GetChangelogContent(dataXmlFinal, descriptionOptions.language);
193190978c3Sopenharmony_ci        componentDescription.descriptionInfo.content = dataXmlFinal;
194190978c3Sopenharmony_ci        componentDescription.descriptionInfo.descriptionType =
195190978c3Sopenharmony_ci            static_cast<DescriptionType>(atoi(dataXml.substr(0, dataXml.find_first_of("|")).c_str()));
196190978c3Sopenharmony_ci        newVersionDescriptionInfo.componentDescriptions.push_back(componentDescription);
197190978c3Sopenharmony_ci    }
198190978c3Sopenharmony_ci    return INT_CALL_SUCCESS;
199190978c3Sopenharmony_ci}
200190978c3Sopenharmony_ci
201190978c3Sopenharmony_ciint32_t UpdateServiceImplFirmware::GetCurrentVersionInfo(const UpgradeInfo &info,
202190978c3Sopenharmony_ci    CurrentVersionInfo &currentVersionInfo, BusinessError &businessError)
203190978c3Sopenharmony_ci{
204190978c3Sopenharmony_ci    FIRMWARE_LOGI("UpdateServiceImplFirmware::GetCurrentVersionInfo");
205190978c3Sopenharmony_ci    businessError.errorNum = CallResult::SUCCESS;
206190978c3Sopenharmony_ci    FirmwareUpdateHelper::BuildCurrentVersionInfo(currentVersionInfo);
207190978c3Sopenharmony_ci    return INT_CALL_SUCCESS;
208190978c3Sopenharmony_ci}
209190978c3Sopenharmony_ci
210190978c3Sopenharmony_ciint32_t UpdateServiceImplFirmware::GetCurrentVersionDescription(const UpgradeInfo &info,
211190978c3Sopenharmony_ci    const DescriptionOptions &descriptionOptions, VersionDescriptionInfo &currentVersionDescriptionInfo,
212190978c3Sopenharmony_ci    BusinessError &businessError)
213190978c3Sopenharmony_ci{
214190978c3Sopenharmony_ci    FIRMWARE_LOGI("GetCurrentVersionDescription format %{public}d language %{public}s",
215190978c3Sopenharmony_ci        CAST_INT(descriptionOptions.format),
216190978c3Sopenharmony_ci        descriptionOptions.language.c_str());
217190978c3Sopenharmony_ci
218190978c3Sopenharmony_ci    ComponentDescription descriptionContent;
219190978c3Sopenharmony_ci    descriptionContent.componentId =
220190978c3Sopenharmony_ci        DelayedSingleton<FirmwarePreferencesUtil>::GetInstance()->ObtainString(Firmware::HOTA_CURRENT_COMPONENT_ID, "");
221190978c3Sopenharmony_ci    if (descriptionContent.componentId.empty()) {
222190978c3Sopenharmony_ci        businessError.Build(CallResult::FAIL, "GetCurrentVersionDescription failed");
223190978c3Sopenharmony_ci        FIRMWARE_LOGE("componentId is null");
224190978c3Sopenharmony_ci        return INT_CALL_SUCCESS;
225190978c3Sopenharmony_ci    }
226190978c3Sopenharmony_ci
227190978c3Sopenharmony_ci    std::string changelogFilePath = Firmware::CHANGELOG_PATH + "/" + descriptionContent.componentId + ".xml";
228190978c3Sopenharmony_ci    if (!FileUtils::IsFileExist(changelogFilePath)) {
229190978c3Sopenharmony_ci        FIRMWARE_LOGE("current changelog file [%{public}s] is not exist!", changelogFilePath.c_str());
230190978c3Sopenharmony_ci        businessError.Build(CallResult::FAIL, "GetCurrentVersionDescription failed");
231190978c3Sopenharmony_ci        return INT_CALL_SUCCESS;
232190978c3Sopenharmony_ci    }
233190978c3Sopenharmony_ci    std::string dataXml = FileUtils::ReadDataFromFile(changelogFilePath);
234190978c3Sopenharmony_ci    size_t startIndex = dataXml.find_first_of("|");
235190978c3Sopenharmony_ci    if (startIndex == std::string::npos) {
236190978c3Sopenharmony_ci        FIRMWARE_LOGE("dataXml not found |");
237190978c3Sopenharmony_ci        businessError.Build(CallResult::FAIL, "GetCurrentVersionDescription failed");
238190978c3Sopenharmony_ci        return INT_CALL_SUCCESS;
239190978c3Sopenharmony_ci    }
240190978c3Sopenharmony_ci    std::string dataXmlFinal = dataXml.substr(startIndex + 1, dataXml.size());
241190978c3Sopenharmony_ci    GetChangelogContent(dataXmlFinal, descriptionOptions.language);
242190978c3Sopenharmony_ci    descriptionContent.descriptionInfo.content = dataXmlFinal;
243190978c3Sopenharmony_ci    descriptionContent.descriptionInfo.descriptionType =
244190978c3Sopenharmony_ci        static_cast<DescriptionType>(atoi(dataXml.substr(0, dataXml.find_first_of("|")).c_str()));
245190978c3Sopenharmony_ci    currentVersionDescriptionInfo.componentDescriptions.push_back(descriptionContent);
246190978c3Sopenharmony_ci    businessError.Build(CallResult::SUCCESS, "GetCurrentVersionDescription ok");
247190978c3Sopenharmony_ci    return INT_CALL_SUCCESS;
248190978c3Sopenharmony_ci}
249190978c3Sopenharmony_ci
250190978c3Sopenharmony_ciint32_t UpdateServiceImplFirmware::GetTaskInfo(const UpgradeInfo &info, TaskInfo &taskInfo,
251190978c3Sopenharmony_ci    BusinessError &businessError)
252190978c3Sopenharmony_ci{
253190978c3Sopenharmony_ci    FIRMWARE_LOGI("GetTaskInfo");
254190978c3Sopenharmony_ci    businessError.errorNum = CallResult::SUCCESS;
255190978c3Sopenharmony_ci    FirmwareTask task;
256190978c3Sopenharmony_ci    FirmwareTaskOperator().QueryTask(task);
257190978c3Sopenharmony_ci    if (task.isExistTask) {
258190978c3Sopenharmony_ci        taskInfo.existTask = true;
259190978c3Sopenharmony_ci        taskInfo.taskBody.status = static_cast<UpgradeStatus>(task.status);
260190978c3Sopenharmony_ci        taskInfo.taskBody.progress = task.progress;
261190978c3Sopenharmony_ci        taskInfo.taskBody.versionDigestInfo.versionDigest = task.taskId;
262190978c3Sopenharmony_ci    }
263190978c3Sopenharmony_ci    FIRMWARE_LOGI("GetTaskInfo existTask %{public}s status %{public}d , progress %{public}d",
264190978c3Sopenharmony_ci        StringUtils::GetBoolStr(taskInfo.existTask).c_str(), CAST_INT(taskInfo.taskBody.status),
265190978c3Sopenharmony_ci        taskInfo.taskBody.progress);
266190978c3Sopenharmony_ci    return INT_CALL_SUCCESS;
267190978c3Sopenharmony_ci}
268190978c3Sopenharmony_ci
269190978c3Sopenharmony_ciint32_t UpdateServiceImplFirmware::SetUpgradePolicy(const UpgradeInfo &info, const UpgradePolicy &policy,
270190978c3Sopenharmony_ci    BusinessError &businessError)
271190978c3Sopenharmony_ci{
272190978c3Sopenharmony_ci    FIRMWARE_LOGI(
273190978c3Sopenharmony_ci        "SetUpgradePolicy autoDownload %{public}d installmode %{public}d startTime %{public}d endTime %{public}d",
274190978c3Sopenharmony_ci        policy.downloadStrategy, policy.autoUpgradeStrategy, policy.autoUpgradePeriods[0].start,
275190978c3Sopenharmony_ci        policy.autoUpgradePeriods[1].end);
276190978c3Sopenharmony_ci    businessError.errorNum = CallResult::SUCCESS;
277190978c3Sopenharmony_ci    bool isAutoDownloadSwitchOn = preferencesUtil_->ObtainBool(Firmware::AUTO_DOWNLOAD_SWITCH, false);
278190978c3Sopenharmony_ci    FIRMWARE_LOGI("SetUpgradePolicy isAutoDownloadSwitchOn %{public}s",
279190978c3Sopenharmony_ci        StringUtils::GetBoolStr(isAutoDownloadSwitchOn).c_str());
280190978c3Sopenharmony_ci    if (isAutoDownloadSwitchOn != policy.downloadStrategy) {
281190978c3Sopenharmony_ci        DelayedSingleton<FirmwareManager>::GetInstance()->DoAutoDownloadSwitchChanged(policy.downloadStrategy);
282190978c3Sopenharmony_ci    }
283190978c3Sopenharmony_ci    return INT_CALL_SUCCESS;
284190978c3Sopenharmony_ci}
285190978c3Sopenharmony_ci
286190978c3Sopenharmony_ciint32_t UpdateServiceImplFirmware::GetUpgradePolicy(const UpgradeInfo &info, UpgradePolicy &policy,
287190978c3Sopenharmony_ci    BusinessError &businessError)
288190978c3Sopenharmony_ci{
289190978c3Sopenharmony_ci    FIRMWARE_LOGI("GetUpgradePolicy");
290190978c3Sopenharmony_ci    bool isAutoDownloadSwitchOn = preferencesUtil_->ObtainBool(Firmware::AUTO_DOWNLOAD_SWITCH, false);
291190978c3Sopenharmony_ci    FIRMWARE_LOGI("GetUpgradePolicy isAutoDownloadSwitchOn %{public}s",
292190978c3Sopenharmony_ci        StringUtils::GetBoolStr(isAutoDownloadSwitchOn).c_str());
293190978c3Sopenharmony_ci    policy.downloadStrategy = isAutoDownloadSwitchOn;
294190978c3Sopenharmony_ci    policy.autoUpgradePeriods[0].start =
295190978c3Sopenharmony_ci        static_cast<uint32_t>(Constant::ONE_HOUR_MINUTES * Firmware::NIGHT_UPGRADE_START_HOUR);
296190978c3Sopenharmony_ci    policy.autoUpgradePeriods[0].end =
297190978c3Sopenharmony_ci        static_cast<uint32_t>(Constant::ONE_HOUR_MINUTES * Firmware::NIGHT_UPGRADE_END_HOUR);
298190978c3Sopenharmony_ci    return INT_CALL_SUCCESS;
299190978c3Sopenharmony_ci}
300190978c3Sopenharmony_ci
301190978c3Sopenharmony_ciint32_t UpdateServiceImplFirmware::Cancel(const UpgradeInfo &info, int32_t service, BusinessError &businessError)
302190978c3Sopenharmony_ci{
303190978c3Sopenharmony_ci    FIRMWARE_LOGI("Cancel %{public}d", service);
304190978c3Sopenharmony_ci    businessError.errorNum = CallResult::SUCCESS;
305190978c3Sopenharmony_ci    FirmwareTask task;
306190978c3Sopenharmony_ci    FirmwareTaskOperator().QueryTask(task);
307190978c3Sopenharmony_ci    if (task.status != UpgradeStatus::DOWNLOADING && task.status != UpgradeStatus::DOWNLOAD_PAUSE) {
308190978c3Sopenharmony_ci        FIRMWARE_LOGI("Cancel fail current status: %{public}d", CAST_INT(task.status));
309190978c3Sopenharmony_ci        businessError.Build(CallResult::FAIL, "Cancel download error");
310190978c3Sopenharmony_ci    } else {
311190978c3Sopenharmony_ci        DelayedSingleton<FirmwareManager>::GetInstance()->DoCancelDownload(businessError);
312190978c3Sopenharmony_ci    }
313190978c3Sopenharmony_ci    return INT_CALL_SUCCESS;
314190978c3Sopenharmony_ci}
315190978c3Sopenharmony_ci
316190978c3Sopenharmony_civoid UpdateServiceImplFirmware::GetChangelogContent(std::string &dataXml, const std::string &language)
317190978c3Sopenharmony_ci{
318190978c3Sopenharmony_ci    std::string languageStart = LANGUAGE_ENGLISH;
319190978c3Sopenharmony_ci    if (language.compare("zh-cn") != 0) {
320190978c3Sopenharmony_ci        languageStart = LANGUAGE_CHINESE;
321190978c3Sopenharmony_ci    }
322190978c3Sopenharmony_ci    StringUtils::StringRemove(dataXml, languageStart, LANGUAGE_END);
323190978c3Sopenharmony_ci}
324190978c3Sopenharmony_ci} // namespace UpdateEngine
325190978c3Sopenharmony_ci} // namespace OHOS
326