1 /* 2 * Copyright (c) 2023 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 <algorithm> 20 #include <cstdlib> 21 #include <cstdio> 22 #include <cstring> 23 #include <iostream> 24 #include <list> 25 #include <map> 26 #include <string> 27 #include <string_ex.h> 28 #include <utility> 29 30 #include "nlohmann/json.hpp" 31 32 #include "common_error_define.h" 33 #include "json_builder.h" 34 #include "net_manager_model.h" 35 #include "update_define.h" 36 37 namespace OHOS::UpdateEngine { 38 const std::string OUC_PACKAGE_NAME = "com.ohos.updateapp"; 39 const std::string OUC_SERVICE_EXT_ABILITY_NAME = "ServiceExtAbility"; 40 41 // 搜索状态 42 enum class SearchStatus { 43 NET_ERROR = -2, 44 SYSTEM_ERROR, 45 HAS_NEW_VERSION, 46 NO_NEW_VERSION, 47 SERVER_BUSY, 48 CHECK_EXECUTE_ERR 49 }; 50 51 enum class UpgradeStatus { 52 INIT = 0, 53 CHECKING_VERSION = 10, 54 CHECK_VERSION_FAIL, 55 CHECK_VERSION_SUCCESS, 56 DOWNLOADING = 20, 57 DOWNLOAD_PAUSE, 58 DOWNLOAD_CANCEL, 59 DOWNLOAD_FAIL, 60 DOWNLOAD_SUCCESS, 61 VERIFYING = 30, 62 VERIFY_FAIL, 63 VERIFY_SUCCESS, 64 AUTHING, 65 AUTH_FAIL, 66 AUTH_SUCCESS, 67 PACKAGE_TRANSING = 70, 68 PACKAGE_TRANS_FAIL, 69 PACKAGE_TRANS_SUCCESS, 70 INSTALLING = 80, 71 INSTALL_FAIL, 72 INSTALL_SUCCESS, 73 UPDATING = 90, 74 UPDATE_FAIL, 75 UPDATE_SUCCESS 76 }; 77 78 enum class PackageType { 79 DYNAMIC = 0, 80 NORMAL = 1, 81 BASE = 2, 82 CUST = 3, 83 PRELOAD = 4, 84 COTA = 5, 85 VERSION = 6, 86 PATCH = 8, 87 SA = 9 88 }; 89 90 enum class ComponentType { 91 INVALID = 0, 92 OTA = 1, 93 PATCH = 2, 94 COTA = 4, 95 PARAM = 8, 96 SA = 16 97 }; 98 99 enum class EffectiveMode { 100 COLD = 1, 101 LIVE = 2, 102 LIVE_AND_COLD = 3 103 }; 104 105 enum class Order { 106 DOWNLOAD = 1, 107 INSTALL = 2, 108 DOWNLOAD_AND_INSTALL = DOWNLOAD | INSTALL, 109 APPLY = 4, 110 INSTALL_AND_APPLY = INSTALL | APPLY 111 }; 112 113 enum class OptionsAfterUpgrade { 114 NORMAL = 0, 115 REBOOT = 1, 116 SHUTDOWN = 2 117 }; 118 119 enum class BusinessSubType { 120 START_UP = 0, 121 FIRMWARE = 1, 122 PARAM = 2, 123 ROLLBACK = 3, 124 ACCESSORY = 4 125 }; 126 127 enum class DescriptionType { 128 CONTENT = 0, 129 URI = 1, 130 ID = 2 131 }; 132 133 enum class DescriptionFormat { 134 STANDARD = 0, 135 SIMPLIFIED = 1 136 }; 137 138 enum ForceUpgradeSwitch { 139 OPEN = 0x01000000, 140 CLOSE = 0x00000000, 141 }; 142 143 enum ForceUpgradeDataNetworkOption { 144 CELLULAR_NOT_SUPPORT = 0x00000000, 145 CELLULAR = 0x00000001, 146 ROAMING = 0x00000002, 147 CELLULAR_AND_ROAMING = CELLULAR | ROAMING, 148 }; 149 150 enum class ForceUpgradeType { 151 NOT_SUPPORT = ForceUpgradeSwitch::CLOSE, 152 CELLULAR_NOT_SUPPORT = ForceUpgradeSwitch::OPEN | ForceUpgradeDataNetworkOption::CELLULAR_NOT_SUPPORT, 153 CELLULAR = ForceUpgradeSwitch::OPEN | ForceUpgradeDataNetworkOption::CELLULAR, 154 CELLULAR_AND_ROAMING = ForceUpgradeSwitch::OPEN | ForceUpgradeDataNetworkOption::CELLULAR_AND_ROAMING, 155 }; 156 157 enum class EventClassify { 158 TASK = 0x01000000, 159 SYSTEM = 0x02000000, 160 }; 161 162 const std::list g_eventClassifyList = { EventClassify::TASK, EventClassify::SYSTEM }; 163 164 enum class EventId { 165 EVENT_TASK_BASE = CAST_UINT(EventClassify::TASK), 166 EVENT_TASK_RECEIVE, 167 EVENT_TASK_CANCEL, 168 EVENT_DOWNLOAD_WAIT, 169 EVENT_DOWNLOAD_START, 170 EVENT_DOWNLOAD_UPDATE, 171 EVENT_DOWNLOAD_PAUSE, 172 EVENT_DOWNLOAD_RESUME, 173 EVENT_DOWNLOAD_SUCCESS, 174 EVENT_DOWNLOAD_FAIL, 175 EVENT_UPGRADE_WAIT, 176 EVENT_UPGRADE_START, 177 EVENT_UPGRADE_UPDATE, 178 EVENT_APPLY_WAIT, 179 EVENT_APPLY_START, 180 EVENT_UPGRADE_SUCCESS, 181 EVENT_UPGRADE_FAIL, 182 EVENT_AUTH_START, 183 EVENT_AUTH_SUCCESS, 184 EVENT_DOWNLOAD_CANCEL, 185 EVENT_INITIALIZE, 186 EVENT_TASK_CHANGE, 187 EVENT_VERSION_INFO_CHANGE, 188 SYSTEM_BASE = CAST_UINT(EventClassify::SYSTEM), 189 SYSTEM_BOOT_COMPLETE, 190 }; 191 192 enum TaskBodyMemeberMask { 193 VERSION_DIGEST_INFO = 0x00000001, 194 UPGRADE_STATUS = 0x00000010, 195 SUB_STATUS = 0x00000100, 196 PROGRESS = 0x00001000, 197 INSTALL_MODE = 0x00010000, 198 ERROR_MESSAGE = 0x00100000, 199 VERSION_COMPONENT = 0x01000000 200 }; 201 202 const std::map<EventId, uint32_t> g_taskBodyTemplateMap = { 203 { EventId::EVENT_TASK_RECEIVE, VERSION_DIGEST_INFO }, 204 { EventId::EVENT_TASK_CHANGE, VERSION_DIGEST_INFO | UPGRADE_STATUS | PROGRESS }, 205 { EventId::EVENT_TASK_CANCEL, VERSION_DIGEST_INFO }, 206 { EventId::EVENT_VERSION_INFO_CHANGE, VERSION_DIGEST_INFO | UPGRADE_STATUS }, 207 { EventId::EVENT_DOWNLOAD_WAIT, VERSION_DIGEST_INFO | UPGRADE_STATUS | INSTALL_MODE }, 208 { EventId::EVENT_DOWNLOAD_START, VERSION_DIGEST_INFO | INSTALL_MODE }, 209 { EventId::EVENT_DOWNLOAD_UPDATE, VERSION_DIGEST_INFO | UPGRADE_STATUS | PROGRESS | INSTALL_MODE }, 210 { EventId::EVENT_DOWNLOAD_PAUSE, VERSION_DIGEST_INFO | UPGRADE_STATUS | PROGRESS | INSTALL_MODE | ERROR_MESSAGE }, 211 { EventId::EVENT_DOWNLOAD_RESUME, VERSION_DIGEST_INFO | UPGRADE_STATUS | PROGRESS | INSTALL_MODE }, 212 { EventId::EVENT_DOWNLOAD_SUCCESS, VERSION_DIGEST_INFO | INSTALL_MODE }, 213 { EventId::EVENT_DOWNLOAD_CANCEL, VERSION_DIGEST_INFO | UPGRADE_STATUS }, 214 { EventId::EVENT_DOWNLOAD_FAIL, VERSION_DIGEST_INFO | INSTALL_MODE | ERROR_MESSAGE }, 215 { EventId::EVENT_UPGRADE_WAIT, VERSION_DIGEST_INFO | UPGRADE_STATUS | INSTALL_MODE | ERROR_MESSAGE }, 216 { EventId::EVENT_UPGRADE_START, VERSION_DIGEST_INFO | UPGRADE_STATUS | INSTALL_MODE }, 217 { EventId::EVENT_UPGRADE_UPDATE, VERSION_DIGEST_INFO | UPGRADE_STATUS | PROGRESS | INSTALL_MODE }, 218 { EventId::EVENT_APPLY_WAIT, VERSION_DIGEST_INFO | UPGRADE_STATUS | ERROR_MESSAGE }, 219 { EventId::EVENT_APPLY_START, VERSION_DIGEST_INFO }, 220 { EventId::EVENT_UPGRADE_SUCCESS, VERSION_DIGEST_INFO | VERSION_COMPONENT }, 221 { EventId::EVENT_UPGRADE_FAIL, VERSION_DIGEST_INFO | VERSION_COMPONENT | ERROR_MESSAGE }, 222 { EventId::EVENT_AUTH_START, VERSION_DIGEST_INFO | VERSION_COMPONENT | UPGRADE_STATUS }, 223 { EventId::EVENT_AUTH_SUCCESS, VERSION_DIGEST_INFO | VERSION_COMPONENT | UPGRADE_STATUS }, 224 { EventId::EVENT_INITIALIZE, UPGRADE_STATUS }, 225 { EventId::SYSTEM_BOOT_COMPLETE, UPGRADE_STATUS } 226 }; 227 228 class UpgradeAction { 229 public: 230 static constexpr const char *UPGRADE = "upgrade"; 231 static constexpr const char *RECOVERY = "recovery"; 232 static constexpr const char *ROLLBACK = "rollback"; 233 }; 234 235 class BusinessVendor { 236 public: 237 static constexpr const char *PUBLIC = "public"; 238 }; 239 240 struct BaseJsonStruct { ~BaseJsonStructBaseJsonStruct241 virtual ~BaseJsonStruct() {} 242 virtual JsonBuilder GetJsonBuilder() = 0; 243 ToJsonBaseJsonStruct244 virtual std::string ToJson() 245 { 246 return GetJsonBuilder().ToJson(); 247 }; 248 }; 249 250 enum class DeviceType { 251 UNKNOWN = 0, 252 SMART_PHONE = 1, // 手机 253 SMART_PAD = 2, // 平板 254 SMART_TV = 4, // 智能电视 255 TWS = 6, // 真无线耳机 256 KEYBOARD = 7, // 键盘 257 PEN = 8 // 手写笔 258 }; 259 260 struct BusinessType : public BaseJsonStruct { 261 std::string vendor; // BusinessVendor 262 BusinessSubType subType = BusinessSubType::FIRMWARE; 263 264 bool operator<(const BusinessType &businessType) const 265 { 266 if (vendor < businessType.vendor) return true; 267 if (vendor > businessType.vendor) return false; 268 269 if (CAST_INT(subType) < CAST_INT(businessType.subType)) return true; 270 if (CAST_INT(subType) > CAST_INT(businessType.subType)) return false; 271 272 return false; 273 } 274 275 JsonBuilder GetJsonBuilder() final; 276 }; 277 278 const std::string LOCAL_UPGRADE_INFO = "LocalUpgradeInfo"; 279 struct UpgradeInfo { 280 std::string upgradeApp; 281 BusinessType businessType; 282 std::string upgradeDevId; 283 std::string controlDevId; 284 int32_t processId; 285 DeviceType deviceType; 286 287 bool operator<(const UpgradeInfo &r) const 288 { 289 if (upgradeApp < r.upgradeApp) return true; 290 if (upgradeApp > r.upgradeApp) return false; 291 292 if (businessType < r.businessType) return true; 293 if (r.businessType < businessType) return false; 294 295 if (upgradeDevId < r.upgradeDevId) return true; 296 if (upgradeDevId > r.upgradeDevId) return false; 297 298 if (controlDevId < r.controlDevId) return true; 299 if (controlDevId > r.controlDevId) return false; 300 301 if (processId < r.processId) return true; 302 if (processId > r.processId) return false; 303 304 if (deviceType < r.deviceType) return true; 305 if (deviceType > r.deviceType) return false; 306 307 return false; 308 } 309 310 std::string ToString() const; 311 IsLocalUpgradeInfo312 bool IsLocal() const 313 { 314 return upgradeApp == LOCAL_UPGRADE_INFO; 315 } 316 }; 317 318 struct SubscribeInfo : public BaseJsonStruct { 319 std::string upgradeApp = OUC_PACKAGE_NAME; 320 BusinessType businessType; 321 std::string abilityName; 322 std::string subscriberDevId; 323 std::string upgradeDevId; 324 DeviceType deviceType = DeviceType::UNKNOWN; 325 std::string deviceName; 326 SubscribeInfoSubscribeInfo327 explicit SubscribeInfo(BusinessSubType subType) 328 { 329 businessType.subType = subType; 330 } 331 SubscribeInfo() = default; 332 333 JsonBuilder GetJsonBuilder() final; 334 }; 335 336 struct VersionDigestInfo : public BaseJsonStruct { 337 std::string versionDigest; 338 339 JsonBuilder GetJsonBuilder() final; 340 }; 341 342 struct DescriptionOptions { 343 DescriptionFormat format = DescriptionFormat::STANDARD; 344 std::string language; 345 }; 346 347 struct DownloadOptions { 348 NetType allowNetwork = NetType::WIFI; 349 Order order = Order::DOWNLOAD; 350 }; 351 352 struct ResumeDownloadOptions { 353 NetType allowNetwork = NetType::WIFI; 354 }; 355 356 struct PauseDownloadOptions { 357 bool isAllowAutoResume = false; 358 }; 359 360 struct UpgradeOptions { 361 Order order = Order::INSTALL; 362 OptionsAfterUpgrade optionsAfterUpgrade = OptionsAfterUpgrade::NORMAL; 363 }; 364 365 struct ClearOptions { 366 UpgradeStatus status = UpgradeStatus::INIT; 367 }; 368 369 enum class InstallMode { 370 NORMAL = 0, 371 NIGHT, 372 AUTO 373 }; 374 375 enum class AutoUpgradeCondition { 376 IDLE = 0, 377 }; 378 379 struct DescriptionInfo : public BaseJsonStruct { 380 DescriptionType descriptionType = DescriptionType::CONTENT; 381 std::string content; 382 383 JsonBuilder GetJsonBuilder() final; 384 }; 385 386 struct ComponentDescription { 387 std::string componentId; 388 DescriptionInfo descriptionInfo; 389 DescriptionInfo notifyDescriptionInfo; 390 }; 391 392 struct VersionComponent : public BaseJsonStruct { 393 std::string componentId; 394 int32_t componentType = CAST_INT(ComponentType::INVALID); 395 std::string upgradeAction; 396 std::string displayVersion; 397 std::string innerVersion; 398 size_t size = 0; 399 size_t effectiveMode = static_cast<size_t>(EffectiveMode::COLD); 400 DescriptionInfo descriptionInfo; 401 std::string componentExtra; 402 403 JsonBuilder GetJsonBuilder() final; 404 }; 405 406 struct CurrentVersionInfo { 407 std::string osVersion; 408 std::string deviceName; 409 std::vector<VersionComponent> versionComponents; 410 }; 411 412 struct NewVersionInfo { 413 VersionDigestInfo versionDigestInfo; 414 std::vector<VersionComponent> versionComponents; 415 }; 416 417 struct VersionDescriptionInfo { 418 std::vector<ComponentDescription> componentDescriptions; 419 }; 420 421 struct CheckResult { 422 bool isExistNewVersion = false; 423 NewVersionInfo newVersionInfo; 424 }; 425 426 struct TaskBody { 427 VersionDigestInfo versionDigestInfo; 428 UpgradeStatus status = UpgradeStatus::INIT; 429 int32_t subStatus = CAST_INT(UpgradeStatus::INIT); 430 int32_t progress = 0; 431 int32_t installMode = CAST_INT(InstallMode::NORMAL); 432 std::vector<ErrorMessage> errorMessages; 433 std::vector<VersionComponent> versionComponents; 434 435 JsonBuilder GetJsonBuilder(EventId eventId); 436 }; 437 438 struct TaskInfo { 439 bool existTask; 440 TaskBody taskBody; 441 }; 442 443 struct Progress { 444 uint32_t percent = 0; 445 UpgradeStatus status = UpgradeStatus::INIT; 446 std::string endReason; 447 }; 448 449 struct UpgradeInterval { 450 uint64_t timeStart = 0; 451 uint64_t timeEnd = 0; 452 }; 453 454 struct UpgradePeriod { 455 uint32_t start = 0; 456 uint32_t end = 0; 457 }; 458 459 struct UpdateTime { 460 int64_t lastUpdateTime = 0; 461 int64_t installWindowStart = 0; 462 int64_t installWindowEnd = 0; 463 to_jsonUpdateTime464 friend void to_json(nlohmann::json &jsonObj, const UpdateTime &updateTime) 465 { 466 jsonObj["lastUpdateTime"] = updateTime.lastUpdateTime; 467 jsonObj["installWindowStart"] = updateTime.installWindowStart; 468 jsonObj["installWindowEnd"] = updateTime.installWindowEnd; 469 } 470 from_jsonUpdateTime471 friend void from_json(const nlohmann::json &jsonObj, UpdateTime &updateTime) 472 { 473 JsonUtils::GetValueAndSetTo(jsonObj, "lastUpdateTime", updateTime.lastUpdateTime); 474 JsonUtils::GetValueAndSetTo(jsonObj, "installWindowStart", updateTime.installWindowStart); 475 JsonUtils::GetValueAndSetTo(jsonObj, "installWindowEnd", updateTime.installWindowEnd); 476 } 477 }; 478 479 struct UpgradePolicy { 480 bool downloadStrategy = false; 481 bool autoUpgradeStrategy = false; 482 UpgradePeriod autoUpgradePeriods[2]; 483 }; 484 485 struct UpgradeFile { 486 ComponentType fileType = ComponentType::INVALID; 487 std::string filePath; 488 }; 489 490 struct EventClassifyInfo { 491 EventClassify eventClassify = EventClassify::TASK; 492 std::string extraInfo; 493 EventClassifyInfoEventClassifyInfo494 EventClassifyInfo() : eventClassify(EventClassify::TASK) {} EventClassifyInfoEventClassifyInfo495 explicit EventClassifyInfo(EventClassify classify) : eventClassify(classify) {} EventClassifyInfoEventClassifyInfo496 EventClassifyInfo(EventClassify classify, const std::string &info) : eventClassify(classify), extraInfo(info) {} 497 }; 498 499 struct EventInfo : public BaseJsonStruct { 500 EventId eventId = EventId::EVENT_TASK_BASE; 501 TaskBody taskBody; 502 503 EventInfo() = default; EventInfoEventInfo504 EventInfo(EventId id, TaskBody body) : eventId(id), taskBody(std::move(body)) {} 505 506 JsonBuilder GetJsonBuilder() final; 507 }; 508 509 struct ConfigInfo { 510 std::string businessDomain; 511 uint32_t abInstallTimeout = 1800; // 1800s 512 std::string moduleLibPath; 513 }; 514 515 using OnEvent = std::function<void(const EventInfo &eventInfo)>; 516 517 // 回调函数 518 struct UpdateCallbackInfo { 519 OnEvent onEvent; 520 }; 521 } // namespace OHOS::UpdateEngine 522 #endif // UPDATE_HELPER_H 523