• 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 UPDATER_HELPER_H
17 #define UPDATER_HELPER_H
18 
19 #include <string>
20 #include <cstdlib>
21 #include <cstdio>
22 #include <cstring>
23 #include <iostream>
24 #include <ipc_types.h>
25 #include <string_ex.h>
26 #include "parcel.h"
27 #include "message_parcel.h"
28 #include "hilog/log.h"
29 #include "system_ability_definition.h"
30 
31 namespace OHOS {
32 namespace update_engine {
33 // 搜索状态
34 enum SearchStatus {
35     SYSTEM_ERROR = -1,
36     HAS_NEW_VERSION,
37     NO_NEW_VERSION,
38     SERVER_BUSY,
39 };
40 
41 enum UpgradeStatus {
42     UPDATE_STATE_INIT = 0,
43     UPDATE_STATE_CHECK_VERSION_ON = 10,
44     UPDATE_STATE_CHECK_VERSION_FAIL,
45     UPDATE_STATE_CHECK_VERSION_SUCCESS,
46     UPDATE_STATE_DOWNLOAD_ON = 20,
47     UPDATE_STATE_DOWNLOAD_PAUSE,
48     UPDATE_STATE_DOWNLOAD_CANCEL,
49     UPDATE_STATE_DOWNLOAD_FAIL,
50     UPDATE_STATE_DOWNLOAD_SUCCESS,
51     UPDATE_STATE_VERIFY_ON = 30,
52     UPDATE_STATE_VERIFY_FAIL,
53     UPDATE_STATE_VERIFY_SUCCESS,
54     UPDATE_STATE_PACKAGE_TRANS_ON = 70,
55     UPDATE_STATE_PACKAGE_TRANS_FAIL,
56     UPDATE_STATE_PACKAGE_TRANS_SUCCESS,
57     UPDATE_STATE_INSTALL_ON = 80,
58     UPDATE_STATE_INSTALL_FAIL,
59     UPDATE_STATE_INSTALL_SUCCESS,
60     UPDATE_STATE_UPDATE_ON = 90,
61     UPDATE_STATE_UPDATE_FAIL,
62     UPDATE_STATE_UPDATE_SUCCESS
63 };
64 
65 enum PackageType {
66     PACKAGE_TYPE_NORMAL = 1,
67     PACKAGE_TYPE_BASE = 2,
68     PACKAGE_TYPE_CUST = 3,
69     PACKAGE_TYPE_PRELOAD = 4,
70     PACKAGE_TYPE_COTA = 5,
71     PACKAGE_TYPE_VERSION = 6,
72     PACKAGE_TYPE_PATCH = 7
73 };
74 
75 struct UpdateContext {
76     std::string upgradeDevId;
77     std::string controlDevId;
78     std::string upgradeApp;
79     std::string upgradeFile;
80     std::string type;
81 };
82 
83 struct DescriptInfo {
84     std::string descriptPackageId;
85     std::string content;
86 };
87 
88 struct CheckResult {
89     size_t size;
90     PackageType packageType;
91     std::string versionName;
92     std::string versionCode;
93     std::string verifyInfo;
94     std::string descriptPackageId;
95 };
96 
97 struct VersionInfo {
98     SearchStatus status;
99     std::string errMsg;
100     CheckResult result[2];
101     DescriptInfo descriptInfo[2];
102 };
103 
104 struct Progress {
105     uint32_t percent;
106     UpgradeStatus status;
107     std::string endReason;
108 };
109 
110 struct UpgradeInfo {
111     UpgradeStatus status;
112 };
113 
114 enum InstallMode {
115     INSTALLMODE_NORMAL = 0,
116     INSTALLMODE_NIGHT,
117     INSTALLMODE_AUTO
118 };
119 
120 enum AutoUpgradeCondition {
121     AUTOUPGRADECONDITION_IDLE = 0,
122 };
123 
124 struct UpdatePolicy {
125     bool autoDownload;
126     bool autoDownloadNet;
127     InstallMode mode;
128     AutoUpgradeCondition autoUpgradeCondition;
129     uint32_t autoUpgradeInterval[2];
130 };
131 
132 using CheckNewVersionDone = std::function<void(const VersionInfo &info)>;
133 using DownloadProgress = std::function<void(const Progress &progress)>;
134 using UpgradeProgress = std::function<void(const Progress &progress)>;
135 
136 // 回调函数
137 struct UpdateCallbackInfo {
138     CheckNewVersionDone checkNewVersionDone;
139     DownloadProgress downloadProgress;
140     UpgradeProgress upgradeProgress;
141 };
142 
143 #ifdef UPDATE_SERVICE
144 static constexpr OHOS::HiviewDFX::HiLogLabel UPDATE_LABEL = {LOG_CORE, 0, "UPDATE_SA"};
145 #else
146 static constexpr OHOS::HiviewDFX::HiLogLabel UPDATE_LABEL = {LOG_CORE, 0, "UPDATE_KITS"};
147 #endif
148 
149 enum class UpdateLogLevel {
150     UPDATE_DEBUG = 0,
151     UPDATE_INFO,
152     UPDATE_WARN,
153     UPDATE_ERROR,
154     UPDATE_FATAL
155 };
156 
157 class UpdateHelper {
158 public:
159     static int32_t WriteUpdateContext(MessageParcel &data, const UpdateContext &info);
160     static int32_t ReadUpdateContext(MessageParcel &reply, UpdateContext &info);
161 
162     static int32_t ReadVersionInfo(MessageParcel &reply, VersionInfo &info);
163     static int32_t WriteVersionInfo(MessageParcel &data, const VersionInfo &info);
164 
165     static int32_t WriteUpdatePolicy(MessageParcel &data, const UpdatePolicy &policy);
166     static int32_t ReadUpdatePolicy(MessageParcel &reply, UpdatePolicy &policy);
167 
168     static int32_t ReadUpgradeInfo(MessageParcel &reply, UpgradeInfo &info);
169     static int32_t WriteUpgradeInfo(MessageParcel &reply, const UpgradeInfo &info);
170 
171     static int32_t ReadUpdateProgress(MessageParcel &reply, Progress &info);
172     static int32_t WriteUpdateProgress(MessageParcel &data, const Progress &info);
173 
174     static int32_t CopyVersionInfo(const VersionInfo &srcInfo, VersionInfo &dstInfo);
175     static int32_t CopyUpdatePolicy(const UpdatePolicy &srcInfo, UpdatePolicy &dstInfo);
176 
177     static std::vector<uint8_t> HexToDegist(const std::string &str);
178     static int32_t CompareVersion(const std::string &version1, const std::string &version2);
179     static std::vector<std::string> SplitString(const std::string &str, const std::string &delimiter);
180 
181     static bool JudgeLevel(const UpdateLogLevel& level);
182 
SetLogLevel(const UpdateLogLevel & level)183     static void SetLogLevel(const UpdateLogLevel& level)
184     {
185         level_ = level;
186     }
187 
GetLogLevel()188     static const UpdateLogLevel& GetLogLevel()
189     {
190         return level_;
191     }
192 
193     static std::string GetBriefFileName(const std::string &file);
194 
195 private:
196     static UpdateLogLevel level_;
197 };
198 
199 // 暂时记录两边日志
200 #define PRINT_LOG(LEVEL, Level, fmt, ...) \
201     if (UpdateHelper::JudgeLevel(UpdateLogLevel::LEVEL)) \
202         OHOS::HiviewDFX::HiLog::Level(UPDATE_LABEL, "[%{public}s(%{public}d)] " fmt, \
203         UpdateHelper::GetBriefFileName(std::string(__FILE__)).c_str(), __LINE__, ##__VA_ARGS__)
204 
205 #define ENGINE_LOGI(fmt, ...) PRINT_LOG(UPDATE_INFO, Info, fmt, ##__VA_ARGS__)
206 #define ENGINE_LOGE(fmt, ...) PRINT_LOG(UPDATE_ERROR, Error, fmt, ##__VA_ARGS__)
207 
208 #define ENGINE_CHECK(retCode, exper, ...) \
209     do { \
210         if (!(retCode)) {                     \
211             ENGINE_LOGE(__VA_ARGS__);         \
212             exper;                            \
213         } \
214     } while (0)
215 }
216 } // namespace OHOS
217 #endif // UPDATER_HELPER_H