1190978c3Sopenharmony_ci/*
2190978c3Sopenharmony_ci * Copyright (c) 2021 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.h"
17190978c3Sopenharmony_ci
18190978c3Sopenharmony_ci#include <dlfcn.h>
19190978c3Sopenharmony_ci#include <unistd.h>
20190978c3Sopenharmony_ci#include <updater_sa_ipc_interface_code.h>
21190978c3Sopenharmony_ci
22190978c3Sopenharmony_ci#include "iservice_registry.h"
23190978c3Sopenharmony_ci#include "system_ability_definition.h"
24190978c3Sopenharmony_ci
25190978c3Sopenharmony_ci#include "access_manager.h"
26190978c3Sopenharmony_ci#include "dupdate_net_manager.h"
27190978c3Sopenharmony_ci#include "config_parse.h"
28190978c3Sopenharmony_ci#include "firmware_common.h"
29190978c3Sopenharmony_ci#include "firmware_manager.h"
30190978c3Sopenharmony_ci#include "startup_manager.h"
31190978c3Sopenharmony_ci#include "update_log.h"
32190978c3Sopenharmony_ci#include "update_service_cache.h"
33190978c3Sopenharmony_ci#include "update_service_local_updater.h"
34190978c3Sopenharmony_ci#include "update_service_restorer.h"
35190978c3Sopenharmony_ci#include "update_service_util.h"
36190978c3Sopenharmony_ci
37190978c3Sopenharmony_ci#include "update_service_module.h"
38190978c3Sopenharmony_ci#include "module_manager.h"
39190978c3Sopenharmony_ci
40190978c3Sopenharmony_cinamespace OHOS {
41190978c3Sopenharmony_cinamespace UpdateEngine {
42190978c3Sopenharmony_ciREGISTER_SYSTEM_ABILITY_BY_ID(UpdateService, UPDATE_DISTRIBUTED_SERVICE_ID, true)
43190978c3Sopenharmony_ci
44190978c3Sopenharmony_ciOHOS::sptr<UpdateService> UpdateService::updateService_ { nullptr };
45190978c3Sopenharmony_ci
46190978c3Sopenharmony_civoid UpdateService::ClientDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
47190978c3Sopenharmony_ci{
48190978c3Sopenharmony_ci    ENGINE_LOGI("client DeathRecipient OnRemoteDied: %{public}s", upgradeInfo_.ToString().c_str());
49190978c3Sopenharmony_ci    sptr<UpdateService> service = UpdateService::GetInstance();
50190978c3Sopenharmony_ci    if (service != nullptr) {
51190978c3Sopenharmony_ci        service->UnregisterUpdateCallback(upgradeInfo_);
52190978c3Sopenharmony_ci    }
53190978c3Sopenharmony_ci}
54190978c3Sopenharmony_ci
55190978c3Sopenharmony_ciUpdateService::ClientProxy::ClientProxy(const UpgradeInfo &info, const sptr<IUpdateCallback> &callback)
56190978c3Sopenharmony_ci    : proxy_(callback)
57190978c3Sopenharmony_ci{
58190978c3Sopenharmony_ci    ENGINE_LOGI("UpdateService::ClientProxy constructor");
59190978c3Sopenharmony_ci    auto clientDeathRecipient = new (std::nothrow) ClientDeathRecipient(info);
60190978c3Sopenharmony_ci    if (clientDeathRecipient != nullptr) {
61190978c3Sopenharmony_ci        deathRecipient_ = sptr<ClientDeathRecipient>(clientDeathRecipient);
62190978c3Sopenharmony_ci    } else {
63190978c3Sopenharmony_ci        ENGINE_LOGE("UpdateService::ClientProxy, new fail");
64190978c3Sopenharmony_ci    }
65190978c3Sopenharmony_ci}
66190978c3Sopenharmony_ci
67190978c3Sopenharmony_civoid UpdateService::ClientProxy::AddDeathRecipient()
68190978c3Sopenharmony_ci{
69190978c3Sopenharmony_ci    ENGINE_LOGI("UpdateService::ClientProxy AddDeathRecipient in");
70190978c3Sopenharmony_ci    if (proxy_ != nullptr) {
71190978c3Sopenharmony_ci        auto remoteObject = proxy_->AsObject();
72190978c3Sopenharmony_ci        if ((remoteObject != nullptr) && (deathRecipient_ != nullptr)) {
73190978c3Sopenharmony_ci            remoteObject->AddDeathRecipient(deathRecipient_);
74190978c3Sopenharmony_ci            ENGINE_LOGI("UpdateService::ClientProxy AddDeathRecipient success");
75190978c3Sopenharmony_ci        }
76190978c3Sopenharmony_ci    }
77190978c3Sopenharmony_ci}
78190978c3Sopenharmony_ci
79190978c3Sopenharmony_civoid UpdateService::ClientProxy::RemoveDeathRecipient()
80190978c3Sopenharmony_ci{
81190978c3Sopenharmony_ci    ENGINE_LOGI("UpdateService::ClientProxy RemoveDeathRecipient in");
82190978c3Sopenharmony_ci    if (proxy_ != nullptr) {
83190978c3Sopenharmony_ci        auto remoteObject = proxy_->AsObject();
84190978c3Sopenharmony_ci        if ((remoteObject != nullptr) && (deathRecipient_ != nullptr)) {
85190978c3Sopenharmony_ci            remoteObject->RemoveDeathRecipient(deathRecipient_);
86190978c3Sopenharmony_ci            ENGINE_LOGI("UpdateService::ClientProxy RemoveDeathRecipient success");
87190978c3Sopenharmony_ci        }
88190978c3Sopenharmony_ci    }
89190978c3Sopenharmony_ci}
90190978c3Sopenharmony_ci
91190978c3Sopenharmony_cisptr<IUpdateCallback> UpdateService::ClientProxy::Get()
92190978c3Sopenharmony_ci{
93190978c3Sopenharmony_ci    return proxy_;
94190978c3Sopenharmony_ci}
95190978c3Sopenharmony_ci
96190978c3Sopenharmony_ciUpdateService::UpdateService(int32_t systemAbilityId, bool runOnCreate)
97190978c3Sopenharmony_ci    : SystemAbility(systemAbilityId, runOnCreate)
98190978c3Sopenharmony_ci{
99190978c3Sopenharmony_ci    updateImplMgr_ = std::make_shared<UpdateServiceImplManager>();
100190978c3Sopenharmony_ci}
101190978c3Sopenharmony_ci
102190978c3Sopenharmony_ciUpdateService::~UpdateService()
103190978c3Sopenharmony_ci{
104190978c3Sopenharmony_ci    ENGINE_LOGI("UpdateServerTest free now");
105190978c3Sopenharmony_ci    for (auto &iter : clientProxyMap_) {
106190978c3Sopenharmony_ci        iter.second.RemoveDeathRecipient();
107190978c3Sopenharmony_ci    }
108190978c3Sopenharmony_ci}
109190978c3Sopenharmony_ci
110190978c3Sopenharmony_cisptr<UpdateService> UpdateService::GetInstance()
111190978c3Sopenharmony_ci{
112190978c3Sopenharmony_ci    return updateService_;
113190978c3Sopenharmony_ci}
114190978c3Sopenharmony_ci
115190978c3Sopenharmony_ciint32_t UpdateService::RegisterUpdateCallback(const UpgradeInfo &info, const sptr<IUpdateCallback> &updateCallback)
116190978c3Sopenharmony_ci{
117190978c3Sopenharmony_ci    ENGINE_LOGI("RegisterUpdateCallback");
118190978c3Sopenharmony_ci    UnregisterUpdateCallback(info);
119190978c3Sopenharmony_ci    {
120190978c3Sopenharmony_ci        std::lock_guard<std::mutex> lock(clientProxyMapLock_);
121190978c3Sopenharmony_ci        ClientProxy clientProxy(info, updateCallback);
122190978c3Sopenharmony_ci        clientProxy.AddDeathRecipient();
123190978c3Sopenharmony_ci        clientProxyMap_.insert({info, clientProxy});
124190978c3Sopenharmony_ci    }
125190978c3Sopenharmony_ci    if (!info.IsLocal()) {
126190978c3Sopenharmony_ci        UpdateServiceCache::SetUpgradeInfo(info);
127190978c3Sopenharmony_ci    }
128190978c3Sopenharmony_ci    DelayedSingleton<AccessManager>::GetInstance()->SetRemoteIdle(clientProxyMap_.empty());
129190978c3Sopenharmony_ci    return INT_CALL_SUCCESS;
130190978c3Sopenharmony_ci}
131190978c3Sopenharmony_ci
132190978c3Sopenharmony_ciint32_t UpdateService::UnregisterUpdateCallback(const UpgradeInfo &info)
133190978c3Sopenharmony_ci{
134190978c3Sopenharmony_ci    ENGINE_LOGI("UnregisterUpdateCallback");
135190978c3Sopenharmony_ci    std::lock_guard<std::mutex> lock(clientProxyMapLock_);
136190978c3Sopenharmony_ci    auto iter = clientProxyMap_.find(info);
137190978c3Sopenharmony_ci    if (iter == clientProxyMap_.end()) {
138190978c3Sopenharmony_ci        return INT_CALL_SUCCESS;
139190978c3Sopenharmony_ci    }
140190978c3Sopenharmony_ci    iter->second.RemoveDeathRecipient();
141190978c3Sopenharmony_ci    clientProxyMap_.erase(info);
142190978c3Sopenharmony_ci    DelayedSingleton<AccessManager>::GetInstance()->SetRemoteIdle(clientProxyMap_.empty());
143190978c3Sopenharmony_ci    return INT_CALL_SUCCESS;
144190978c3Sopenharmony_ci}
145190978c3Sopenharmony_ci
146190978c3Sopenharmony_cisptr<IUpdateCallback> UpdateService::GetUpgradeCallback(const UpgradeInfo &info)
147190978c3Sopenharmony_ci{
148190978c3Sopenharmony_ci    std::lock_guard<std::mutex> lock(clientProxyMapLock_);
149190978c3Sopenharmony_ci    auto iter = clientProxyMap_.find(info);
150190978c3Sopenharmony_ci    if (iter == clientProxyMap_.end()) {
151190978c3Sopenharmony_ci        return nullptr;
152190978c3Sopenharmony_ci    }
153190978c3Sopenharmony_ci    return iter->second.Get();
154190978c3Sopenharmony_ci}
155190978c3Sopenharmony_ci
156190978c3Sopenharmony_ciint32_t UpdateService::GetNewVersionInfo(const UpgradeInfo &info, NewVersionInfo &newVersionInfo,
157190978c3Sopenharmony_ci    BusinessError &businessError)
158190978c3Sopenharmony_ci{
159190978c3Sopenharmony_ci    sptr<IServiceOnlineUpdater> onlineUpdater = updateImplMgr_->GetOnlineUpdater(info);
160190978c3Sopenharmony_ci    if (onlineUpdater == nullptr) {
161190978c3Sopenharmony_ci        ENGINE_LOGI("GetNewVersionInfo onlineUpdater null");
162190978c3Sopenharmony_ci        return INT_CALL_FAIL;
163190978c3Sopenharmony_ci    }
164190978c3Sopenharmony_ci    return onlineUpdater->GetNewVersionInfo(info, newVersionInfo, businessError);
165190978c3Sopenharmony_ci}
166190978c3Sopenharmony_ci
167190978c3Sopenharmony_ciint32_t UpdateService::GetNewVersionDescription(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
168190978c3Sopenharmony_ci    const DescriptionOptions &descriptionOptions, VersionDescriptionInfo &newVersionDescriptionInfo,
169190978c3Sopenharmony_ci    BusinessError &businessError)
170190978c3Sopenharmony_ci{
171190978c3Sopenharmony_ci    sptr<IServiceOnlineUpdater> onlineUpdater = updateImplMgr_->GetOnlineUpdater(info);
172190978c3Sopenharmony_ci    if (onlineUpdater == nullptr) {
173190978c3Sopenharmony_ci        ENGINE_LOGI("GetNewVersionDescription onlineUpdater null");
174190978c3Sopenharmony_ci        return INT_CALL_FAIL;
175190978c3Sopenharmony_ci    }
176190978c3Sopenharmony_ci    return onlineUpdater->GetNewVersionDescription(info, versionDigestInfo, descriptionOptions,
177190978c3Sopenharmony_ci        newVersionDescriptionInfo, businessError);
178190978c3Sopenharmony_ci}
179190978c3Sopenharmony_ci
180190978c3Sopenharmony_ciint32_t UpdateService::GetCurrentVersionInfo(const UpgradeInfo &info, CurrentVersionInfo &currentVersionInfo,
181190978c3Sopenharmony_ci    BusinessError &businessError)
182190978c3Sopenharmony_ci{
183190978c3Sopenharmony_ci    sptr<IServiceOnlineUpdater> onlineUpdater = updateImplMgr_->GetOnlineUpdater(info);
184190978c3Sopenharmony_ci    if (onlineUpdater == nullptr) {
185190978c3Sopenharmony_ci        ENGINE_LOGI("GetCurrentVersionInfo onlineUpdater null");
186190978c3Sopenharmony_ci        return INT_CALL_FAIL;
187190978c3Sopenharmony_ci    }
188190978c3Sopenharmony_ci    return onlineUpdater->GetCurrentVersionInfo(info, currentVersionInfo, businessError);
189190978c3Sopenharmony_ci}
190190978c3Sopenharmony_ci
191190978c3Sopenharmony_ciint32_t UpdateService::GetCurrentVersionDescription(const UpgradeInfo &info,
192190978c3Sopenharmony_ci    const DescriptionOptions &descriptionOptions, VersionDescriptionInfo &currentVersionDescriptionInfo,
193190978c3Sopenharmony_ci    BusinessError &businessError)
194190978c3Sopenharmony_ci{
195190978c3Sopenharmony_ci    sptr<IServiceOnlineUpdater> onlineUpdater = updateImplMgr_->GetOnlineUpdater(info);
196190978c3Sopenharmony_ci    if (onlineUpdater == nullptr) {
197190978c3Sopenharmony_ci        ENGINE_LOGI("GetCurrentVersionDescription onlineUpdater null");
198190978c3Sopenharmony_ci        return INT_CALL_FAIL;
199190978c3Sopenharmony_ci    }
200190978c3Sopenharmony_ci    return onlineUpdater->GetCurrentVersionDescription(info, descriptionOptions, currentVersionDescriptionInfo,
201190978c3Sopenharmony_ci        businessError);
202190978c3Sopenharmony_ci}
203190978c3Sopenharmony_ci
204190978c3Sopenharmony_ciint32_t UpdateService::GetTaskInfo(const UpgradeInfo &info, TaskInfo &taskInfo, BusinessError &businessError)
205190978c3Sopenharmony_ci{
206190978c3Sopenharmony_ci    sptr<IServiceOnlineUpdater> onlineUpdater = updateImplMgr_->GetOnlineUpdater(info);
207190978c3Sopenharmony_ci    if (onlineUpdater == nullptr) {
208190978c3Sopenharmony_ci        ENGINE_LOGI("GetTaskInfo onlineUpdater null");
209190978c3Sopenharmony_ci        return INT_CALL_FAIL;
210190978c3Sopenharmony_ci    }
211190978c3Sopenharmony_ci    return onlineUpdater->GetTaskInfo(info, taskInfo, businessError);
212190978c3Sopenharmony_ci}
213190978c3Sopenharmony_ci
214190978c3Sopenharmony_ciint32_t UpdateService::SetUpgradePolicy(const UpgradeInfo &info, const UpgradePolicy &policy,
215190978c3Sopenharmony_ci    BusinessError &businessError)
216190978c3Sopenharmony_ci{
217190978c3Sopenharmony_ci    sptr<IServiceOnlineUpdater> onlineUpdater = updateImplMgr_->GetOnlineUpdater(info);
218190978c3Sopenharmony_ci    if (onlineUpdater == nullptr) {
219190978c3Sopenharmony_ci        ENGINE_LOGI("SetUpgradePolicy onlineUpdater null");
220190978c3Sopenharmony_ci        return INT_CALL_FAIL;
221190978c3Sopenharmony_ci    }
222190978c3Sopenharmony_ci    return onlineUpdater->SetUpgradePolicy(info, policy, businessError);
223190978c3Sopenharmony_ci}
224190978c3Sopenharmony_ci
225190978c3Sopenharmony_ciint32_t UpdateService::GetUpgradePolicy(const UpgradeInfo &info, UpgradePolicy &policy, BusinessError &businessError)
226190978c3Sopenharmony_ci{
227190978c3Sopenharmony_ci    sptr<IServiceOnlineUpdater> onlineUpdater = updateImplMgr_->GetOnlineUpdater(info);
228190978c3Sopenharmony_ci    if (onlineUpdater == nullptr) {
229190978c3Sopenharmony_ci        ENGINE_LOGI("GetUpgradePolicy onlineUpdater null");
230190978c3Sopenharmony_ci        return INT_CALL_FAIL;
231190978c3Sopenharmony_ci    }
232190978c3Sopenharmony_ci    return onlineUpdater->GetUpgradePolicy(info, policy, businessError);
233190978c3Sopenharmony_ci}
234190978c3Sopenharmony_ci
235190978c3Sopenharmony_ciint32_t UpdateService::CheckNewVersion(const UpgradeInfo &info, BusinessError &businessError, CheckResult &checkResult)
236190978c3Sopenharmony_ci{
237190978c3Sopenharmony_ci    sptr<IServiceOnlineUpdater> onlineUpdater = updateImplMgr_->GetOnlineUpdater(info);
238190978c3Sopenharmony_ci    if (onlineUpdater == nullptr) {
239190978c3Sopenharmony_ci        ENGINE_LOGI("CheckNewVersion onlineUpdater null");
240190978c3Sopenharmony_ci        return INT_CALL_FAIL;
241190978c3Sopenharmony_ci    }
242190978c3Sopenharmony_ci    return onlineUpdater->CheckNewVersion(info, businessError, checkResult);
243190978c3Sopenharmony_ci}
244190978c3Sopenharmony_ci
245190978c3Sopenharmony_ciint32_t UpdateService::Download(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
246190978c3Sopenharmony_ci    const DownloadOptions &downloadOptions, BusinessError &businessError)
247190978c3Sopenharmony_ci{
248190978c3Sopenharmony_ci    sptr<IServiceOnlineUpdater> onlineUpdater = updateImplMgr_->GetOnlineUpdater(info);
249190978c3Sopenharmony_ci    if (onlineUpdater == nullptr) {
250190978c3Sopenharmony_ci        ENGINE_LOGI("Download onlineUpdater null");
251190978c3Sopenharmony_ci        return INT_CALL_FAIL;
252190978c3Sopenharmony_ci    }
253190978c3Sopenharmony_ci    return onlineUpdater->Download(info, versionDigestInfo, downloadOptions, businessError);
254190978c3Sopenharmony_ci}
255190978c3Sopenharmony_ci
256190978c3Sopenharmony_ciint32_t UpdateService::PauseDownload(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
257190978c3Sopenharmony_ci    const PauseDownloadOptions &pauseDownloadOptions, BusinessError &businessError)
258190978c3Sopenharmony_ci{
259190978c3Sopenharmony_ci    ENGINE_LOGI("PauseDownload");
260190978c3Sopenharmony_ci    businessError.errorNum = CallResult::SUCCESS;
261190978c3Sopenharmony_ci    businessError.Build(CallResult::UN_SUPPORT, "PauseDownload unsupport");
262190978c3Sopenharmony_ci    return INT_CALL_SUCCESS;
263190978c3Sopenharmony_ci}
264190978c3Sopenharmony_ci
265190978c3Sopenharmony_ciint32_t UpdateService::ResumeDownload(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
266190978c3Sopenharmony_ci    const ResumeDownloadOptions &resumeDownloadOptions, BusinessError &businessError)
267190978c3Sopenharmony_ci{
268190978c3Sopenharmony_ci    ENGINE_LOGI("ResumeDownload allowNetwork:%{public}d", CAST_INT(resumeDownloadOptions.allowNetwork));
269190978c3Sopenharmony_ci    businessError.Build(CallResult::UN_SUPPORT, "ResumeDownload unsupport");
270190978c3Sopenharmony_ci    return INT_CALL_SUCCESS;
271190978c3Sopenharmony_ci}
272190978c3Sopenharmony_ci
273190978c3Sopenharmony_ciint32_t UpdateService::Upgrade(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
274190978c3Sopenharmony_ci    const UpgradeOptions &upgradeOptions, BusinessError &businessError)
275190978c3Sopenharmony_ci{
276190978c3Sopenharmony_ci    sptr<IServiceOnlineUpdater> onlineUpdater = updateImplMgr_->GetOnlineUpdater(info);
277190978c3Sopenharmony_ci    if (onlineUpdater == nullptr) {
278190978c3Sopenharmony_ci        ENGINE_LOGI("Upgrade onlineUpdater null");
279190978c3Sopenharmony_ci        return INT_CALL_FAIL;
280190978c3Sopenharmony_ci    }
281190978c3Sopenharmony_ci    return onlineUpdater->Upgrade(info, versionDigestInfo, upgradeOptions, businessError);
282190978c3Sopenharmony_ci}
283190978c3Sopenharmony_ci
284190978c3Sopenharmony_ciint32_t UpdateService::ClearError(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
285190978c3Sopenharmony_ci    const ClearOptions &clearOptions, BusinessError &businessError)
286190978c3Sopenharmony_ci{
287190978c3Sopenharmony_ci    sptr<IServiceOnlineUpdater> onlineUpdater = updateImplMgr_->GetOnlineUpdater(info);
288190978c3Sopenharmony_ci    if (onlineUpdater == nullptr) {
289190978c3Sopenharmony_ci        ENGINE_LOGI("ClearError onlineUpdater null");
290190978c3Sopenharmony_ci        return INT_CALL_FAIL;
291190978c3Sopenharmony_ci    }
292190978c3Sopenharmony_ci    return onlineUpdater->ClearError(info, versionDigestInfo, clearOptions, businessError);
293190978c3Sopenharmony_ci}
294190978c3Sopenharmony_ci
295190978c3Sopenharmony_ciint32_t UpdateService::TerminateUpgrade(const UpgradeInfo &info, BusinessError &businessError)
296190978c3Sopenharmony_ci{
297190978c3Sopenharmony_ci    sptr<IServiceOnlineUpdater> onlineUpdater = updateImplMgr_->GetOnlineUpdater(info);
298190978c3Sopenharmony_ci    if (onlineUpdater == nullptr) {
299190978c3Sopenharmony_ci        ENGINE_LOGI("TerminateUpgrade onlineUpdater null");
300190978c3Sopenharmony_ci        return INT_CALL_FAIL;
301190978c3Sopenharmony_ci    }
302190978c3Sopenharmony_ci    return onlineUpdater->TerminateUpgrade(info, businessError);
303190978c3Sopenharmony_ci}
304190978c3Sopenharmony_ci
305190978c3Sopenharmony_ciint32_t UpdateService::Cancel(const UpgradeInfo &info, int32_t service, BusinessError &businessError)
306190978c3Sopenharmony_ci{
307190978c3Sopenharmony_ci    sptr<IServiceOnlineUpdater> onlineUpdater = updateImplMgr_->GetOnlineUpdater(info);
308190978c3Sopenharmony_ci    if (onlineUpdater == nullptr) {
309190978c3Sopenharmony_ci        ENGINE_LOGI("Cancel onlineUpdater null");
310190978c3Sopenharmony_ci        return INT_CALL_FAIL;
311190978c3Sopenharmony_ci    }
312190978c3Sopenharmony_ci    return onlineUpdater->Cancel(info, service, businessError);
313190978c3Sopenharmony_ci}
314190978c3Sopenharmony_ci
315190978c3Sopenharmony_ciint32_t UpdateService::FactoryReset(BusinessError &businessError)
316190978c3Sopenharmony_ci{
317190978c3Sopenharmony_ci    sptr<UpdateServiceRestorer> restorer = new UpdateServiceRestorer();
318190978c3Sopenharmony_ci    if (restorer == nullptr) {
319190978c3Sopenharmony_ci        ENGINE_LOGI("FactoryReset restorer null");
320190978c3Sopenharmony_ci        return INT_CALL_FAIL;
321190978c3Sopenharmony_ci    }
322190978c3Sopenharmony_ci    return restorer->FactoryReset(businessError);
323190978c3Sopenharmony_ci}
324190978c3Sopenharmony_ci
325190978c3Sopenharmony_ciint32_t UpdateService::ApplyNewVersion(const UpgradeInfo &info, const std::string &miscFile,
326190978c3Sopenharmony_ci    const std::vector<std::string> &packageNames, BusinessError &businessError)
327190978c3Sopenharmony_ci{
328190978c3Sopenharmony_ci    sptr<UpdateServiceLocalUpdater> localUpdater = new UpdateServiceLocalUpdater();
329190978c3Sopenharmony_ci    if (localUpdater == nullptr) {
330190978c3Sopenharmony_ci        ENGINE_LOGI("FactoryReset localUpdater null");
331190978c3Sopenharmony_ci        return INT_CALL_FAIL;
332190978c3Sopenharmony_ci    }
333190978c3Sopenharmony_ci    return localUpdater->ApplyNewVersion(info, miscFile, packageNames, businessError);
334190978c3Sopenharmony_ci}
335190978c3Sopenharmony_ci
336190978c3Sopenharmony_ciint32_t UpdateService::VerifyUpgradePackage(const std::string &packagePath, const std::string &keyPath,
337190978c3Sopenharmony_ci    BusinessError &businessError)
338190978c3Sopenharmony_ci{
339190978c3Sopenharmony_ci    sptr<UpdateServiceLocalUpdater> localUpdater = new UpdateServiceLocalUpdater();
340190978c3Sopenharmony_ci    if (localUpdater == nullptr) {
341190978c3Sopenharmony_ci        ENGINE_LOGI("FactoryReset localUpdater null");
342190978c3Sopenharmony_ci        return INT_CALL_FAIL;
343190978c3Sopenharmony_ci    }
344190978c3Sopenharmony_ci    return localUpdater->VerifyUpgradePackage(packagePath, keyPath, businessError);
345190978c3Sopenharmony_ci}
346190978c3Sopenharmony_ci
347190978c3Sopenharmony_civoid BuildUpgradeInfoDump(const int fd, UpgradeInfo &info)
348190978c3Sopenharmony_ci{
349190978c3Sopenharmony_ci    dprintf(fd, "---------------------upgrade info info--------------------\n");
350190978c3Sopenharmony_ci    dprintf(fd, "UpgradeApp: %s\n", info.upgradeApp.c_str());
351190978c3Sopenharmony_ci    dprintf(fd, "vendor: %s\n", info.businessType.vendor.c_str());
352190978c3Sopenharmony_ci    dprintf(fd, "subType: %d\n", static_cast<int>(info.businessType.subType));
353190978c3Sopenharmony_ci}
354190978c3Sopenharmony_ci
355190978c3Sopenharmony_civoid BuildVersionInfoDump(const int fd, const CheckResult &checkResult)
356190978c3Sopenharmony_ci{
357190978c3Sopenharmony_ci    dprintf(fd, "---------------------version info--------------------\n");
358190978c3Sopenharmony_ci    dprintf(fd, "isExistNewVersion: %d\n", checkResult.isExistNewVersion);
359190978c3Sopenharmony_ci    if (checkResult.newVersionInfo.versionComponents.empty()) {
360190978c3Sopenharmony_ci        return;
361190978c3Sopenharmony_ci    }
362190978c3Sopenharmony_ci    dprintf(fd, "PackageSize: %zu\n", static_cast<size_t>(checkResult.newVersionInfo.versionComponents[0].size));
363190978c3Sopenharmony_ci    dprintf(fd, "ComponentType: %d\n", checkResult.newVersionInfo.versionComponents[0].componentType);
364190978c3Sopenharmony_ci    dprintf(fd, "UpgradeAction: %s\n", checkResult.newVersionInfo.versionComponents[0].upgradeAction.c_str());
365190978c3Sopenharmony_ci    dprintf(fd, "DisplayVersion: %s\n", checkResult.newVersionInfo.versionComponents[0].displayVersion.c_str());
366190978c3Sopenharmony_ci    dprintf(fd, "InnerVersion: %s\n", checkResult.newVersionInfo.versionComponents[0].innerVersion.c_str());
367190978c3Sopenharmony_ci    dprintf(fd, "Content: %s\n", checkResult.newVersionInfo.versionComponents[0].descriptionInfo.content.c_str());
368190978c3Sopenharmony_ci}
369190978c3Sopenharmony_ci
370190978c3Sopenharmony_civoid BuildTaskInfoDump(const int fd)
371190978c3Sopenharmony_ci{
372190978c3Sopenharmony_ci    sptr<UpdateService> service = UpdateService::GetInstance();
373190978c3Sopenharmony_ci    if (service == nullptr) {
374190978c3Sopenharmony_ci        ENGINE_LOGI("BuildTaskInfoDump no instance");
375190978c3Sopenharmony_ci        return;
376190978c3Sopenharmony_ci    }
377190978c3Sopenharmony_ci
378190978c3Sopenharmony_ci    TaskInfo taskInfo;
379190978c3Sopenharmony_ci    BusinessError businessError;
380190978c3Sopenharmony_ci    UpgradeInfo upgradeInfo;
381190978c3Sopenharmony_ci    service->GetTaskInfo(upgradeInfo, taskInfo, businessError);
382190978c3Sopenharmony_ci    if (!taskInfo.existTask) {
383190978c3Sopenharmony_ci        dprintf(fd, "TaskInfo is empty\n");
384190978c3Sopenharmony_ci        return;
385190978c3Sopenharmony_ci    }
386190978c3Sopenharmony_ci
387190978c3Sopenharmony_ci    dprintf(fd, "---------------------OTA status info--------------------\n");
388190978c3Sopenharmony_ci    dprintf(fd, "Progress: %d\n", taskInfo.taskBody.progress);
389190978c3Sopenharmony_ci    dprintf(fd, "UpgradeStatus: %d\n", taskInfo.taskBody.status);
390190978c3Sopenharmony_ci    dprintf(fd, "SubStatus: %d\n", taskInfo.taskBody.subStatus);
391190978c3Sopenharmony_ci    for (auto &iter : taskInfo.taskBody.errorMessages) {
392190978c3Sopenharmony_ci        dprintf(fd, "ErrorCode: %d\n", iter.errorCode);
393190978c3Sopenharmony_ci        dprintf(fd, "ErrorMsg: %s\n", iter.errorMessage.c_str());
394190978c3Sopenharmony_ci    }
395190978c3Sopenharmony_ci}
396190978c3Sopenharmony_ci
397190978c3Sopenharmony_civoid UpdateService::DumpUpgradeCallback(const int fd)
398190978c3Sopenharmony_ci{
399190978c3Sopenharmony_ci    dprintf(fd, "---------------------callback info--------------------\n");
400190978c3Sopenharmony_ci    for (const auto &iter : clientProxyMap_) {
401190978c3Sopenharmony_ci        const UpgradeInfo& info = iter.first;
402190978c3Sopenharmony_ci        dprintf(fd, "%s\n", info.ToString().c_str());
403190978c3Sopenharmony_ci    }
404190978c3Sopenharmony_ci}
405190978c3Sopenharmony_ci
406190978c3Sopenharmony_ciint UpdateService::Dump(int fd, const std::vector<std::u16string> &args)
407190978c3Sopenharmony_ci{
408190978c3Sopenharmony_ci    if (!ModuleManager::GetInstance().IsModuleLoaded()) {
409190978c3Sopenharmony_ci        if (fd < 0) {
410190978c3Sopenharmony_ci            ENGINE_LOGI("HiDumper handle invalid");
411190978c3Sopenharmony_ci            return -1;
412190978c3Sopenharmony_ci        }
413190978c3Sopenharmony_ci
414190978c3Sopenharmony_ci        if (args.size() == 0) {
415190978c3Sopenharmony_ci            UpgradeInfo upgradeInfo = UpdateServiceCache::GetUpgradeInfo(BusinessSubType::FIRMWARE);
416190978c3Sopenharmony_ci            BuildUpgradeInfoDump(fd, upgradeInfo);
417190978c3Sopenharmony_ci            BuildTaskInfoDump(fd);
418190978c3Sopenharmony_ci            DumpUpgradeCallback(fd);
419190978c3Sopenharmony_ci        } else {
420190978c3Sopenharmony_ci            dprintf(fd, "input error, no parameters required\n");
421190978c3Sopenharmony_ci        }
422190978c3Sopenharmony_ci        return 0;
423190978c3Sopenharmony_ci    } else {
424190978c3Sopenharmony_ci        return ModuleManager::GetInstance().HandleDumpFunc("Dump", fd, args);
425190978c3Sopenharmony_ci    }
426190978c3Sopenharmony_ci}
427190978c3Sopenharmony_ci
428190978c3Sopenharmony_civoid UpdateService::OnStart(const SystemAbilityOnDemandReason &startReason)
429190978c3Sopenharmony_ci{
430190978c3Sopenharmony_ci    ENGINE_LOGI("UpdaterService oh OnStart, startReason name %{public}s, id %{public}d, value %{public}s",
431190978c3Sopenharmony_ci        startReason.GetName().c_str(), CAST_INT(startReason.GetId()), startReason.GetValue().c_str());
432190978c3Sopenharmony_ci    updateService_ = this;
433190978c3Sopenharmony_ci    if (updateService_ == nullptr) {
434190978c3Sopenharmony_ci        ENGINE_LOGE("updateService_ null");
435190978c3Sopenharmony_ci    }
436190978c3Sopenharmony_ci
437190978c3Sopenharmony_ci    DelayedSingleton<ConfigParse>::GetInstance()->LoadConfigInfo(); // 启动读取配置信息
438190978c3Sopenharmony_ci    std::string libPath = DelayedSingleton<ConfigParse>::GetInstance()->GetModuleLibPath();
439190978c3Sopenharmony_ci    ENGINE_LOGI("GetModuleLibPath %{public}s ", libPath.c_str());
440190978c3Sopenharmony_ci    ModuleManager::GetInstance().LoadModule(libPath);
441190978c3Sopenharmony_ci
442190978c3Sopenharmony_ci    ENGINE_LOGI("RegisterOhFunc HandleOhRemoteRequest");
443190978c3Sopenharmony_ci    RegisterOhFunc();
444190978c3Sopenharmony_ci
445190978c3Sopenharmony_ci    if (!ModuleManager::GetInstance().IsModuleLoaded()) {
446190978c3Sopenharmony_ci        ENGINE_LOGI("IsModuleLoaded false, init updateservice_sa");
447190978c3Sopenharmony_ci        DelayedSingleton<NetManager>::GetInstance()->Init();
448190978c3Sopenharmony_ci
449190978c3Sopenharmony_ci        // 动态启停流程启动
450190978c3Sopenharmony_ci        DelayedSingleton<StartupManager>::GetInstance()->Start();
451190978c3Sopenharmony_ci    }
452190978c3Sopenharmony_ci
453190978c3Sopenharmony_ci    if (Publish(this)) {
454190978c3Sopenharmony_ci        ENGINE_LOGI("UpdaterService OnStart publish success");
455190978c3Sopenharmony_ci    } else {
456190978c3Sopenharmony_ci        ENGINE_LOGI("UpdaterService OnStart publish fail");
457190978c3Sopenharmony_ci    }
458190978c3Sopenharmony_ci
459190978c3Sopenharmony_ci    if (ModuleManager::GetInstance().IsModuleLoaded()) {
460190978c3Sopenharmony_ci        ENGINE_LOGI("IsModuleLoaded true");
461190978c3Sopenharmony_ci        ModuleManager::GetInstance().HandleOnStartOnStopFunc("OnStart", startReason);
462190978c3Sopenharmony_ci    }
463190978c3Sopenharmony_ci}
464190978c3Sopenharmony_ci
465190978c3Sopenharmony_ciint32_t UpdateService::OnIdle(const SystemAbilityOnDemandReason &idleReason)
466190978c3Sopenharmony_ci{
467190978c3Sopenharmony_ci    ENGINE_LOGI("UpdaterService OnIdle");
468190978c3Sopenharmony_ci    return ModuleManager::GetInstance().HandleOnIdleFunc("OnIdle", idleReason);
469190978c3Sopenharmony_ci}
470190978c3Sopenharmony_ci
471190978c3Sopenharmony_civoid UpdateService::OnStop(const SystemAbilityOnDemandReason &stopReason)
472190978c3Sopenharmony_ci{
473190978c3Sopenharmony_ci    ENGINE_LOGI("UpdaterService OnStop");
474190978c3Sopenharmony_ci    ModuleManager::GetInstance().HandleOnStartOnStopFunc("OnStop", stopReason);
475190978c3Sopenharmony_ci}
476190978c3Sopenharmony_ci
477190978c3Sopenharmony_ciint32_t HandleOhRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
478190978c3Sopenharmony_ci{
479190978c3Sopenharmony_ci    return UpdateService::GetInstance()-> HandleRemoteRequest(code, data, reply, option);
480190978c3Sopenharmony_ci}
481190978c3Sopenharmony_ci
482190978c3Sopenharmony_civoid UpdateService::RegisterOhFunc()
483190978c3Sopenharmony_ci{
484190978c3Sopenharmony_ci    std::vector<uint32_t> codes = {
485190978c3Sopenharmony_ci        CAST_UINT(UpdaterSaInterfaceCode::CHECK_VERSION),
486190978c3Sopenharmony_ci        CAST_UINT(UpdaterSaInterfaceCode::DOWNLOAD),
487190978c3Sopenharmony_ci        CAST_UINT(UpdaterSaInterfaceCode::PAUSE_DOWNLOAD),
488190978c3Sopenharmony_ci        CAST_UINT(UpdaterSaInterfaceCode::RESUME_DOWNLOAD),
489190978c3Sopenharmony_ci        CAST_UINT(UpdaterSaInterfaceCode::UPGRADE),
490190978c3Sopenharmony_ci        CAST_UINT(UpdaterSaInterfaceCode::CLEAR_ERROR),
491190978c3Sopenharmony_ci        CAST_UINT(UpdaterSaInterfaceCode::TERMINATE_UPGRADE),
492190978c3Sopenharmony_ci        CAST_UINT(UpdaterSaInterfaceCode::SET_POLICY),
493190978c3Sopenharmony_ci        CAST_UINT(UpdaterSaInterfaceCode::GET_POLICY),
494190978c3Sopenharmony_ci        CAST_UINT(UpdaterSaInterfaceCode::GET_NEW_VERSION),
495190978c3Sopenharmony_ci        CAST_UINT(UpdaterSaInterfaceCode::GET_NEW_VERSION_DESCRIPTION),
496190978c3Sopenharmony_ci        CAST_UINT(UpdaterSaInterfaceCode::GET_CURRENT_VERSION),
497190978c3Sopenharmony_ci        CAST_UINT(UpdaterSaInterfaceCode::GET_CURRENT_VERSION_DESCRIPTION),
498190978c3Sopenharmony_ci        CAST_UINT(UpdaterSaInterfaceCode::GET_TASK_INFO),
499190978c3Sopenharmony_ci        CAST_UINT(UpdaterSaInterfaceCode::REGISTER_CALLBACK),
500190978c3Sopenharmony_ci        CAST_UINT(UpdaterSaInterfaceCode::UNREGISTER_CALLBACK),
501190978c3Sopenharmony_ci        CAST_UINT(UpdaterSaInterfaceCode::CANCEL),
502190978c3Sopenharmony_ci        CAST_UINT(UpdaterSaInterfaceCode::FACTORY_RESET),
503190978c3Sopenharmony_ci        CAST_UINT(UpdaterSaInterfaceCode::APPLY_NEW_VERSION),
504190978c3Sopenharmony_ci        CAST_UINT(UpdaterSaInterfaceCode::VERIFY_UPGRADE_PACKAGE)
505190978c3Sopenharmony_ci    };
506190978c3Sopenharmony_ci    RegisterFunc(codes, HandleOhRemoteRequest);
507190978c3Sopenharmony_ci}
508190978c3Sopenharmony_ci} // namespace UpdateEngine
509190978c3Sopenharmony_ci} // namespace OHOS
510