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_HELPER_H
17 #define UPDATE_HELPER_H
18
19 #include <cstdlib>
20 #include <cstdio>
21 #include <cstring>
22 #include <iostream>
23 #include <ipc_types.h>
24 #include <list>
25 #include <map>
26 #include <string>
27 #include <string_ex.h>
28
29 #include "parcel.h"
30 #include "message_parcel.h"
31 #include "hilog/log.h"
32 #include "system_ability_definition.h"
33
34 #include "json_builder.h"
35 #include "update_define.h"
36
37 namespace OHOS {
38 namespace UpdateEngine {
39 static const std::string OUC_PACKAGE_NAME = "com.ohos.updateapp";
40 static const std::string OUC_SERVICE_EXT_ABILITY_NAME = "ServiceExtAbility";
41 constexpr int CALL_RESULT_OFFSET = 2000;
42
43 enum class CallResult {
44 // 通用错误码
45 APP_NOT_GRANTED = 201,
46 NOT_SYSTEM_APP = 202,
47 PARAM_ERR = 401,
48 UN_SUPPORT = 801,
49
50 // 模块内错误码
51 SUCCESS = 0,
52 FAIL = 100,
53 DEV_UPG_INFO_ERR = 102,
54 FORBIDDEN = 103,
55 IPC_ERR = 104,
56 TIME_OUT = 402,
57 DB_ERROR = 501,
58 IO_ERROR = 502,
59 NET_ERROR = 503
60 };
61
62 constexpr int32_t INT_CALL_SUCCESS = CAST_INT(CallResult::SUCCESS);
63 constexpr int32_t INT_CALL_FAIL = CAST_INT(CallResult::FAIL);
64 constexpr int32_t INT_UN_SUPPORT = CAST_INT(CallResult::UN_SUPPORT);
65 constexpr int32_t INT_FORBIDDEN = CAST_INT(CallResult::FORBIDDEN);
66 constexpr int32_t INT_CALL_IPC_ERR = CAST_INT(CallResult::IPC_ERR);
67 constexpr int32_t INT_APP_NOT_GRANTED = CAST_INT(CallResult::APP_NOT_GRANTED);
68 constexpr int32_t INT_NOT_SYSTEM_APP = CAST_INT(CallResult::NOT_SYSTEM_APP);
69 constexpr int32_t INT_PARAM_ERR = CAST_INT(CallResult::PARAM_ERR);
70 constexpr int32_t INT_DEV_UPG_INFO_ERR = CAST_INT(CallResult::DEV_UPG_INFO_ERR);
71 constexpr int32_t INT_TIME_OUT = CAST_INT(CallResult::TIME_OUT);
72 constexpr int32_t INT_DB_ERROR = CAST_INT(CallResult::DB_ERROR);
73 constexpr int32_t INT_IO_ERROR = CAST_INT(CallResult::IO_ERROR);
74 constexpr int32_t INT_NET_ERROR = CAST_INT(CallResult::NET_ERROR);
75
76 // 搜索状态
77 enum class SearchStatus {
78 NET_ERROR = -2,
79 SYSTEM_ERROR,
80 HAS_NEW_VERSION,
81 NO_NEW_VERSION,
82 SERVER_BUSY,
83 CHECK_EXECUTE_ERR,
84 };
85
86 enum class UpgradeStatus {
87 INIT = 0,
88 CHECKING_VERSION = 10,
89 CHECK_VERSION_FAIL,
90 CHECK_VERSION_SUCCESS,
91 DOWNLOADING = 20,
92 DOWNLOAD_PAUSE,
93 DOWNLOAD_CANCEL,
94 DOWNLOAD_FAIL,
95 DOWNLOAD_SUCCESS,
96 VERIFYING = 30,
97 VERIFY_FAIL,
98 VERIFY_SUCCESS,
99 PACKAGE_TRANSING = 70,
100 PACKAGE_TRANS_FAIL,
101 PACKAGE_TRANS_SUCCESS,
102 INSTALLING = 80,
103 INSTALL_FAIL,
104 INSTALL_SUCCESS,
105 UPDATING = 90,
106 UPDATE_FAIL,
107 UPDATE_SUCCESS
108 };
109
110 enum class PackageType {
111 DYNAMIC = 0,
112 NORMAL = 1
113 };
114
115 enum class ComponentType {
116 INVALID = 0,
117 OTA = 1
118 };
119
120 enum class EffectiveMode {
121 COLD = 1,
122 LIVE = 2,
123 LIVE_AND_COLD = 3
124 };
125
126 enum class NetType {
127 NO_NET = 0,
128 CELLULAR = 1,
129 METERED_WIFI = 2,
130 NOT_METERED_WIFI = 4,
131 CELLULAR_AND_METERED_WIFI = CELLULAR | METERED_WIFI,
132 CELLULAR_AND_NOT_METERED_WIFI = CELLULAR | NOT_METERED_WIFI,
133 WIFI = METERED_WIFI | NOT_METERED_WIFI,
134 CELLULAR_AND_WIFI = CELLULAR | WIFI
135 };
136
137 enum class Order {
138 DOWNLOAD = 1,
139 INSTALL = 2,
140 DOWNLOAD_AND_INSTALL = DOWNLOAD | INSTALL,
141 APPLY = 4,
142 INSTALL_AND_APPLY = INSTALL | APPLY
143 };
144
145 enum class EventClassify {
146 TASK = 0x01000000,
147 };
148
149 enum class BusinessSubType {
150 FIRMWARE = 1,
151 PARAM = 2
152 };
153
154 enum class DescriptionType {
155 CONTENT = 0,
156 URI = 1,
157 ID = 2
158 };
159
160 enum class DescriptionFormat {
161 STANDARD = 0,
162 SIMPLIFIED = 1
163 };
164
165 enum class EventId {
166 EVENT_TASK_BASE = 0x01000000,
167 EVENT_TASK_RECEIVE,
168 EVENT_TASK_CANCEL,
169 EVENT_DOWNLOAD_WAIT,
170 EVENT_DOWNLOAD_START,
171 EVENT_DOWNLOAD_UPDATE,
172 EVENT_DOWNLOAD_PAUSE,
173 EVENT_DOWNLOAD_RESUME,
174 EVENT_DOWNLOAD_SUCCESS,
175 EVENT_DOWNLOAD_FAIL,
176 EVENT_UPGRADE_WAIT,
177 EVENT_UPGRADE_START,
178 EVENT_UPGRADE_UPDATE,
179 EVENT_APPLY_WAIT,
180 EVENT_APPLY_START,
181 EVENT_UPGRADE_SUCCESS,
182 EVENT_UPGRADE_FAIL,
183 EVENT_AUTH_START,
184 EVENT_AUTH_SUCCESS,
185 EVENT_DOWNLOAD_CANCEL,
186 EVENT_INITIALIZE
187 };
188
189 enum TaskBodyMemeberMask {
190 VERSION_DIGEST_INFO = 0x00000001,
191 UPGRADE_STATUS = 0x00000010,
192 SUB_STATUS = 0x00000100,
193 PROGRESS = 0x00001000,
194 INSTALL_MODE = 0x00010000,
195 ERROR_MESSAGE = 0x00100000,
196 VERSION_COMPONENT = 0x01000000
197 };
198
199 static std::map<EventId, uint32_t> g_taskBodyTemplateMap = {
200 { EventId::EVENT_TASK_RECEIVE, VERSION_DIGEST_INFO },
201 { EventId::EVENT_TASK_CANCEL, VERSION_DIGEST_INFO },
202 { EventId::EVENT_DOWNLOAD_WAIT, VERSION_DIGEST_INFO | UPGRADE_STATUS | INSTALL_MODE },
203 { EventId::EVENT_DOWNLOAD_START, VERSION_DIGEST_INFO | INSTALL_MODE },
204 { EventId::EVENT_DOWNLOAD_UPDATE, VERSION_DIGEST_INFO | UPGRADE_STATUS | PROGRESS | INSTALL_MODE },
205 { EventId::EVENT_DOWNLOAD_PAUSE, VERSION_DIGEST_INFO | UPGRADE_STATUS | PROGRESS | INSTALL_MODE | ERROR_MESSAGE },
206 { EventId::EVENT_DOWNLOAD_RESUME, VERSION_DIGEST_INFO | UPGRADE_STATUS | PROGRESS | INSTALL_MODE },
207 { EventId::EVENT_DOWNLOAD_SUCCESS, VERSION_DIGEST_INFO | INSTALL_MODE },
208 { EventId::EVENT_DOWNLOAD_CANCEL, VERSION_DIGEST_INFO | UPGRADE_STATUS },
209 { EventId::EVENT_DOWNLOAD_FAIL, VERSION_DIGEST_INFO | INSTALL_MODE | ERROR_MESSAGE },
210 { EventId::EVENT_UPGRADE_WAIT, VERSION_DIGEST_INFO | UPGRADE_STATUS | INSTALL_MODE | ERROR_MESSAGE },
211 { EventId::EVENT_UPGRADE_START, VERSION_DIGEST_INFO | UPGRADE_STATUS | INSTALL_MODE },
212 { EventId::EVENT_UPGRADE_UPDATE, VERSION_DIGEST_INFO | UPGRADE_STATUS | PROGRESS | INSTALL_MODE },
213 { EventId::EVENT_APPLY_WAIT, VERSION_DIGEST_INFO | UPGRADE_STATUS | ERROR_MESSAGE },
214 { EventId::EVENT_APPLY_START, VERSION_DIGEST_INFO },
215 { EventId::EVENT_UPGRADE_SUCCESS, VERSION_DIGEST_INFO | VERSION_COMPONENT },
216 { EventId::EVENT_UPGRADE_FAIL, VERSION_DIGEST_INFO | VERSION_COMPONENT | ERROR_MESSAGE },
217 { EventId::EVENT_AUTH_START, VERSION_DIGEST_INFO | VERSION_COMPONENT | UPGRADE_STATUS},
218 { EventId::EVENT_AUTH_SUCCESS, VERSION_DIGEST_INFO | VERSION_COMPONENT | UPGRADE_STATUS },
219 { EventId::EVENT_INITIALIZE, UPGRADE_STATUS }
220 };
221
222 class UpgradeAction {
223 public:
224 static constexpr const char *UPGRADE = "upgrade";
225 static constexpr const char *RECOVERY = "recovery";
226 };
227
228 class BusinessVendor {
229 public:
230 static constexpr const char *PUBLIC = "public";
231 };
232
233 struct BaseJsonStruct {
~BaseJsonStructBaseJsonStruct234 virtual ~BaseJsonStruct() {}
235 virtual JsonBuilder GetJsonBuilder() = 0;
236
ToJsonBaseJsonStruct237 virtual std::string ToJson()
238 {
239 return GetJsonBuilder().ToJson();
240 };
241 };
242
243 struct BusinessType : public BaseJsonStruct {
244 std::string vendor; // BusinessVendor
245 BusinessSubType subType = BusinessSubType::FIRMWARE;
246
247 bool operator<(const BusinessType &businessType) const
248 {
249 if (vendor < businessType.vendor) return true;
250 if (vendor > businessType.vendor) return false;
251
252 if (CAST_INT(subType) < CAST_INT(businessType.subType)) return true;
253 if (CAST_INT(subType) > CAST_INT(businessType.subType)) return false;
254
255 return false;
256 }
257
258 JsonBuilder GetJsonBuilder() final;
259 };
260
261 const std::string LOCAL_UPGRADE_INFO = "LocalUpgradeInfo";
262 struct UpgradeInfo {
263 std::string upgradeApp;
264 BusinessType businessType;
265 std::string upgradeDevId;
266 std::string controlDevId;
267
268 bool operator<(const UpgradeInfo &r) const
269 {
270 if (upgradeApp < r.upgradeApp) return true;
271 if (upgradeApp > r.upgradeApp) return false;
272
273 if (businessType < r.businessType) return true;
274 if (r.businessType < businessType) return false;
275
276 if (upgradeDevId < r.upgradeDevId) return true;
277 if (upgradeDevId > r.upgradeDevId) return false;
278
279 if (controlDevId < r.controlDevId) return true;
280 if (controlDevId > r.controlDevId) return false;
281
282 return false;
283 }
284
285 std::string ToString() const;
286
IsLocalUpgradeInfo287 bool IsLocal() const
288 {
289 return upgradeApp == LOCAL_UPGRADE_INFO;
290 }
291 };
292
293 struct SubscribeInfo : public BaseJsonStruct {
294 std::string upgradeApp = OUC_PACKAGE_NAME;
295 BusinessType businessType;
296 std::string abilityName;
297 std::string subscriberDevId;
298 std::string devUpgradeId;
299
SubscribeInfoSubscribeInfo300 SubscribeInfo(BusinessSubType subType)
301 {
302 businessType.subType = subType;
303 }
304
305 JsonBuilder GetJsonBuilder() final;
306 };
307
308 struct VersionDigestInfo : public BaseJsonStruct {
309 std::string versionDigest;
310
311 JsonBuilder GetJsonBuilder() final;
312 };
313
314 struct ErrorMessage : public BaseJsonStruct {
315 int32_t errorCode = 0;
316 std::string errorMessage;
317
318 JsonBuilder GetJsonBuilder() final;
319 };
320
321 struct DescriptionOptions {
322 DescriptionFormat format = DescriptionFormat::STANDARD;
323 std::string language;
324 };
325
326 struct DownloadOptions {
327 NetType allowNetwork = NetType::WIFI;
328 Order order = Order::DOWNLOAD;
329 };
330
331 struct ResumeDownloadOptions {
332 NetType allowNetwork = NetType::WIFI;
333 };
334
335 struct PauseDownloadOptions {
336 bool isAllowAutoResume = false;
337 };
338
339 struct UpgradeOptions {
340 Order order = Order::INSTALL;
341 };
342
343 struct ClearOptions {
344 UpgradeStatus status = UpgradeStatus::INIT;
345 };
346
347 enum class InstallMode {
348 NORMAL = 0,
349 NIGHT,
350 AUTO
351 };
352
353 struct DescriptionInfo : public BaseJsonStruct {
354 DescriptionType descriptionType = DescriptionType::CONTENT;
355 std::string content;
356
357 JsonBuilder GetJsonBuilder() final;
358 };
359
360 struct ComponentDescription {
361 std::string componentId;
362 DescriptionInfo descriptionInfo;
363 };
364
365 struct VersionComponent : public BaseJsonStruct {
366 std::string componentId;
367 int32_t componentType = CAST_INT(ComponentType::INVALID);
368 std::string upgradeAction;
369 std::string displayVersion;
370 std::string innerVersion;
371 size_t size = 0;
372 size_t effectiveMode = static_cast<size_t>(EffectiveMode::COLD);
373 DescriptionInfo descriptionInfo;
374 std::string componentExtra;
375
376 JsonBuilder GetJsonBuilder() final;
377 };
378
379 struct CurrentVersionInfo {
380 std::string osVersion;
381 std::string deviceName;
382 std::vector<VersionComponent> versionComponents;
383 };
384
385 struct NewVersionInfo {
386 VersionDigestInfo versionDigestInfo;
387 std::vector<VersionComponent> versionComponents;
388 };
389
390 struct VersionDescriptionInfo {
391 std::vector<ComponentDescription> componentDescriptions;
392 };
393
394 struct CheckResult {
395 bool isExistNewVersion = false;
396 NewVersionInfo newVersionInfo;
397 };
398
399 struct TaskBody {
400 VersionDigestInfo versionDigestInfo;
401 UpgradeStatus status = UpgradeStatus::INIT;
402 int32_t subStatus = CAST_INT(UpgradeStatus::INIT);
403 int32_t progress = 0;
404 int32_t installMode = CAST_INT(InstallMode::NORMAL);
405 std::vector<ErrorMessage> errorMessages;
406 std::vector<VersionComponent> versionComponents;
407
408 JsonBuilder GetJsonBuilder(EventId eventId);
409 };
410
411 struct TaskInfo {
412 bool existTask;
413 TaskBody taskBody;
414 };
415
416 struct Progress {
417 uint32_t percent = 0;
418 UpgradeStatus status = UpgradeStatus::INIT;
419 std::string endReason;
420 };
421
422 struct ErrMsg {
423 int32_t errorCode = 0;
424 std::string errorMsg;
425 };
426
427 struct UpgradeInterval {
428 uint64_t timeStart = 0;
429 uint64_t timeEnd = 0;
430 };
431
432 struct BusinessError {
433 std::string message;
434 CallResult errorNum = CallResult::SUCCESS;
435 std::vector<ErrorMessage> data;
436
BuildBusinessError437 BusinessError &Build(CallResult callResult, const std::string &msg)
438 {
439 errorNum = callResult;
440 message = msg;
441 return *this;
442 }
443
AddErrorMessageBusinessError444 BusinessError &AddErrorMessage(int32_t errorCode, const std::string &errorMessage)
445 {
446 ErrorMessage errMsg;
447 errMsg.errorCode = errorCode;
448 errMsg.errorMessage = errorMessage;
449 data.push_back(errMsg);
450 return *this;
451 }
452 };
453
454 struct UpgradePeriod {
455 uint32_t start = 0;
456 uint32_t end = 0;
457 };
458
459 struct UpgradePolicy {
460 bool downloadStrategy = false;
461 bool autoUpgradeStrategy = false;
462 UpgradePeriod autoUpgradePeriods[2];
463 };
464
465 struct UpgradeFile {
466 ComponentType fileType = ComponentType::INVALID;
467 std::string filePath;
468 };
469
470 struct EventClassifyInfo {
471 EventClassify eventClassify = EventClassify::TASK;
472 std::string extraInfo;
473
EventClassifyInfoEventClassifyInfo474 EventClassifyInfo() : eventClassify(EventClassify::TASK) {}
EventClassifyInfoEventClassifyInfo475 EventClassifyInfo(EventClassify classify) : eventClassify(classify) {}
EventClassifyInfoEventClassifyInfo476 EventClassifyInfo(EventClassify classify, const std::string &info) : eventClassify(classify), extraInfo(info) {}
477 };
478
479 struct EventInfo : public BaseJsonStruct {
480 EventId eventId = EventId::EVENT_TASK_BASE;
481 TaskBody taskBody;
482
EventInfoEventInfo483 EventInfo() {}
EventInfoEventInfo484 EventInfo(EventId id, TaskBody body) : eventId(id), taskBody(body) {}
485
486 JsonBuilder GetJsonBuilder() final;
487 };
488
489 struct ConfigInfo {
490 std::string businessDomain;
491 uint32_t abInstallTimeout = 1800; // 1800s
492 };
493
494 enum class AuthType {
495 PKI = 1,
496 WD = 2
497 };
498
499 using CheckNewVersionDone = std::function<void(const BusinessError &businessError, const CheckResult &checkResult)>;
500 using OnEvent = std::function<void(const EventInfo &eventInfo)>;
501
502 // 回调函数
503 struct UpdateCallbackInfo {
504 CheckNewVersionDone checkNewVersionDone;
505 OnEvent onEvent;
506 };
507
508 template<typename T>
IsValidEnum(const std::list<T> & enumList,int32_t number)509 bool IsValidEnum(const std::list<T> &enumList, int32_t number)
510 {
511 for (auto i : enumList) {
512 if (number == static_cast<int32_t>(i)) {
513 return true;
514 }
515 }
516 return false;
517 }
518
519 class UpdateHelper {
520 public:
521 static std::string BuildEventDevId(const UpgradeInfo &info);
522 static bool IsUpgradeFailed(UpgradeStatus status);
523 };
524
525 struct DirInfo {
526 std::string dirName;
527 int32_t dirPermissions;
528 bool isAllowDestroyContents = false; // 是否允许删除当前目录下的所有文件(包括子目录)
529 };
530 } // namespace UpdateEngine
531 } // namespace OHOS
532 #endif // UPDATE_HELPER_H
533