• 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 #ifndef UPDATER_SERVICE_H
16 #define UPDATER_SERVICE_H
17 
18 #include <iostream>
19 #include <thread>
20 #include "cJSON.h"
21 #include "if_system_ability_manager.h"
22 #include "ipc_skeleton.h"
23 #include "iremote_stub.h"
24 #include "iupdate_service.h"
25 #include "openssl/err.h"
26 #include "openssl/ssl.h"
27 #include "progress_thread.h"
28 #include "system_ability.h"
29 #include "update_service_stub.h"
30 
31 namespace OHOS {
32 namespace update_engine {
33 class UpdateService : public SystemAbility, public UpdateServiceStub {
34 public:
35     DECLARE_SYSTEM_ABILITY(UpdateService);
36     DISALLOW_COPY_AND_MOVE(UpdateService);
37     explicit UpdateService(int32_t systemAbilityId, bool runOnCreate = true);
38     ~UpdateService() override;
39 
40     int32_t RegisterUpdateCallback(const UpdateContext &ctx, const sptr<IUpdateCallback> &updateCallback) override;
41 
42     int32_t UnregisterUpdateCallback() override;
43 
44     int32_t CheckNewVersion() override;
45 
46     int32_t DownloadVersion() override;
47 
48     int32_t DoUpdate() override;
49 
50     int32_t GetNewVersion(VersionInfo &versionInfo) override;
51 
52     int32_t GetUpgradeStatus (UpgradeInfo &info) override;
53 
54     int32_t SetUpdatePolicy(const UpdatePolicy &policy) override;
55 
56     int32_t GetUpdatePolicy(UpdatePolicy &policy) override;
57 
58     int32_t Cancel(int32_t service) override;
59 
60     int32_t RebootAndClean(const std::string &miscFile, const std::string &cmd) override;
61 
62     int32_t RebootAndInstall(const std::string &miscFile, const std::string &packageName) override;
63     static int32_t ParseJsonFile(const std::vector<char> &buffer, VersionInfo &info, std::string &url);
64     static int32_t ReadCheckVersionResult(const cJSON* results, VersionInfo &info, std::string &url);
65     static int32_t ReadCheckVersiondescriptInfo(const cJSON *descriptInfo, VersionInfo &info);
66 #ifndef UPDATER_UT
67 protected:
68 #else
69 public:
70 #endif
71     void OnStart() override;
72     void OnStop() override;
73 private:
74     void DownloadCallback(const std::string &fileName, const Progress &progress);
75     void UpgradeCallback(const Progress &progress);
76     std::string GetDownloadServerUrl() const;
77     void InitVersionInfo(VersionInfo &versionInfo) const;
78 #ifndef UPDATER_UT
79 private:
80 #else
81 public:
82 #endif
83     void SearchCallback(const std::string &msg, SearchStatus status);
84     bool VerifyDownloadPkg(const std::string &pkgName, Progress &progress);
85     void ReadDataFromSSL(int32_t engineSocket);
86 private:
87     UpdatePolicy policy_ = {
88         1, 1, INSTALLMODE_AUTO, AUTOUPGRADECONDITION_IDLE, { 10, 20 }
89     };
90     UpgradeStatus upgradeStatus_ = UPDATE_STATE_INIT;
91     VersionInfo versionInfo_ {};
92 
93     sptr<IUpdateCallback> updateCallback_ { nullptr };
94     DownloadThread *downloadThread_  { nullptr };
95     UpdateContext updateContext_ {};
96     std::string downloadUrl_;
97 };
98 }
99 } // namespace OHOS
100 #endif // UPDATER_SERVICE_H