• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #ifndef UPDATE_SERVICE_H
17 #define UPDATE_SERVICE_H
18 
19 #include <iostream>
20 #include <thread>
21 
22 #include "cJSON.h"
23 #include "if_system_ability_manager.h"
24 #include "ipc_skeleton.h"
25 #include "iremote_stub.h"
26 #include "iupdate_service.h"
27 #include "progress_thread.h"
28 #include "system_ability.h"
29 #include "update_helper.h"
30 #include "update_service_stub.h"
31 
32 namespace OHOS {
33 namespace UpdateEngine {
34 class UpdateService : public SystemAbility, public UpdateServiceStub {
35 public:
36     DECLARE_SYSTEM_ABILITY(UpdateService);
37     DISALLOW_COPY_AND_MOVE(UpdateService);
38     explicit UpdateService(int32_t systemAbilityId, bool runOnCreate = true);
39     ~UpdateService() override;
40 
41     int32_t RegisterUpdateCallback(const UpgradeInfo &info, const sptr<IUpdateCallback> &updateCallback) override;
42 
43     int32_t UnregisterUpdateCallback(const UpgradeInfo &info) override;
44 
45     int32_t CheckNewVersion(const UpgradeInfo &info) override;
46 
47     int32_t DownloadVersion(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
48         const DownloadOptions &downloadOptions, BusinessError &businessError) override;
49 
50     int32_t PauseDownload(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
51         const PauseDownloadOptions &pauseDownloadOptions, BusinessError &businessError) override;
52 
53     int32_t ResumeDownload(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
54         const ResumeDownloadOptions &resumeDownloadOptions, BusinessError &businessError) override;
55 
56     int32_t DoUpdate(const UpgradeInfo &info, const VersionDigestInfo &versionDigest,
57         const UpgradeOptions &upgradeOptions, BusinessError &businessError) override;
58 
59     int32_t ClearError(const UpgradeInfo &info, const VersionDigestInfo &versionDigest,
60         const ClearOptions &clearOptions, BusinessError &businessError) override;
61 
62     int32_t TerminateUpgrade(const UpgradeInfo &info, BusinessError &businessError) override;
63 
64     int32_t GetNewVersion(
65         const UpgradeInfo &info, NewVersionInfo &newVersionInfo, BusinessError &businessError) override;
66 
67     int32_t GetNewVersionDescription(const UpgradeInfo &info, const VersionDigestInfo &versionDigestInfo,
68         const DescriptionOptions &descriptionOptions, VersionDescriptionInfo &newVersionDescriptionInfo,
69         BusinessError &businessError) override;
70 
71     int32_t GetCurrentVersionInfo(const UpgradeInfo &info, CurrentVersionInfo &currentVersionInfo,
72         BusinessError &businessError) override;
73 
74     int32_t GetCurrentVersionDescription(const UpgradeInfo &info, const DescriptionOptions &descriptionOptions,
75         VersionDescriptionInfo &currentVersionDescriptionInfo, BusinessError &businessError) override;
76 
77     int32_t GetTaskInfo(const UpgradeInfo &info, TaskInfo &taskInfo, BusinessError &businessError) override;
78 
79     int32_t SetUpgradePolicy(const UpgradeInfo &info, const UpgradePolicy &policy,
80         BusinessError &businessError) override;
81 
82     int32_t GetUpgradePolicy(const UpgradeInfo &info, UpgradePolicy &policy, BusinessError &businessError) override;
83 
84     int32_t Cancel(const UpgradeInfo &info, int32_t service, BusinessError &businessError) override;
85 
86     int32_t FactoryReset(BusinessError &businessError) override;
87 
88     int32_t ApplyNewVersion(const UpgradeInfo &info, const std::string &miscFile, const std::string &packageName,
89         BusinessError &businessError) override;
90 
91     int32_t VerifyUpgradePackage(const std::string &packagePath, const std::string &keyPath,
92         BusinessError &businessError) override;
93 
94     void DownloadCallback(const Progress &progress);
95 
96     int Dump(int fd, const std::vector<std::u16string> &args) override;
97 
98     void SearchCallback(const std::string &msg, SearchStatus status);
99 
100     void SearchCallbackEx(BusinessError &businessError, CheckResultEx &checkResult);
101 
102     static sptr<UpdateService> GetInstance();
103     sptr<IUpdateCallback> GetUpgradeCallback(const UpgradeInfo &info);
104 
105     void SetCheckInterval(uint64_t interval);
106     void SetDownloadInterval(uint64_t interval);
107     uint64_t GetCheckInterval();
108     uint64_t GetDownloadInterval();
109     void GetUpgradeContext(std::string &devIdInfo);
110 
111     static int32_t ParseJsonFile(const std::vector<char> &buffer, VersionInfo &info, std::string &url);
112     static int32_t ReadCheckVersionResult(const cJSON* results, VersionInfo &info, std::string &url);
113     static int32_t ReadCheckVersiondescriptInfo(const cJSON *descriptInfo, VersionInfo &info);
114 
115 #ifndef UPDATER_UT
116 protected:
117 #endif
118     void OnStart() override;
119     void OnStop() override;
120 
121 private:
122     void DumpUpgradeCallback(const int fd);
123     void GetCheckResult(CheckResultEx &checkResult);
124     void UpgradeCallback(const Progress &progress);
125     std::string GetDownloadServerUrl() const;
126     void InitVersionInfo(VersionInfo &versionInfo) const;
127     void SendEvent(const UpgradeInfo &upgradeInfo, EventId eventId);
128 
129 #ifndef UPDATER_UT
130 private:
131 #else
132 public:
133 #endif
134     bool VerifyDownloadPkg(const std::string &pkgName, Progress &progress);
135     void ReadDataFromSSL(int32_t engineSocket);
136 
137     class ClientDeathRecipient final : public IRemoteObject::DeathRecipient {
138     public:
ClientDeathRecipient(const UpgradeInfo & upgradeInfo)139         ClientDeathRecipient(const UpgradeInfo &upgradeInfo) : upgradeInfo_(upgradeInfo) {}
~ClientDeathRecipient()140         ~ClientDeathRecipient() final {}
141         DISALLOW_COPY_AND_MOVE(ClientDeathRecipient);
142         void OnRemoteDied(const wptr<IRemoteObject> &remote) final;
143     private:
144         UpgradeInfo upgradeInfo_;
145     };
146 
147     class ClientProxy {
148     public:
149         ClientProxy(const UpgradeInfo &info, const sptr<IUpdateCallback> &callback);
150         ClientProxy &operator=(const ClientProxy &source);
151         void AddDeathRecipient();
152         void RemoveDeathRecipient();
153         sptr<IUpdateCallback> Get();
154     private:
155         sptr<IUpdateCallback> proxy_;
156         sptr<IRemoteObject::DeathRecipient> deathRecipient_;
157     };
158 
159 private:
160     UpgradePolicy policy_ = {
161         1, 1, {{10, 20}, {10, 20}}
162     };
163     UpgradeStatus upgradeStatus_ = UPDATE_STATE_INIT;
164     VersionInfo versionInfo_ {};
165     CheckResultEx checkResultEx_ {};
166     OtaStatus otaStatus_ {};
167     UpgradeInterval upgradeInterval_ {};
168     uint64_t checkInterval_ = 0;
169     uint64_t downloadInterval_ = 0;
170     std::mutex clientProxyMapLock_;
171     std::map<UpgradeInfo, ClientProxy> clientProxyMap_;
172     DownloadThread *downloadThread_  { nullptr };
173     UpgradeInfo upgradeInfo_ {};
174     std::string downloadUrl_;
175     static sptr<UpdateService> updateService_;
176 };
177 } // namespace UpdateEngine
178 } // namespace OHOS
179 #endif // UPDATE_SERVICE_H
180