1 /* 2 * Copyright (c) 2025 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 OHOS_FILEMGMT_BACKUP_RADAR_CONST_H 17 #define OHOS_FILEMGMT_BACKUP_RADAR_CONST_H 18 19 #include <string> 20 #include "b_error/b_error.h" 21 #include "b_utils/b_time.h" 22 23 namespace OHOS::FileManagement::Backup { 24 25 enum class Mode : uint32_t { 26 FULL = 1, 27 INCREMENTAL = 2 28 }; 29 30 enum class BizScene : int32_t { 31 UNKNOWN = 0, 32 BACKUP = 1, 33 RESTORE = 2, 34 }; 35 36 struct Duration { GetSpanDuration37 uint32_t GetSpan() const 38 { 39 if (endMilli_ < startMilli_ || startMilli_ == 0) { 40 return 0; 41 } 42 return static_cast<uint32_t>(endMilli_ - startMilli_); 43 } 44 StartDuration45 void Start() 46 { 47 startMilli_ = static_cast<uint64_t>(TimeUtils::GetTimeMS()); 48 } 49 EndDuration50 void End() 51 { 52 endMilli_ = static_cast<uint64_t>(TimeUtils::GetTimeMS()); 53 } 54 55 uint64_t startMilli_ = 0; 56 uint64_t endMilli_ = 0; 57 }; 58 59 constexpr uint32_t MASK_SYS = 0xff; 60 constexpr uint32_t MASK_MODULE = 0x1f; 61 constexpr uint32_t MASK_ERROR = 0xffff; 62 constexpr uint8_t MOVE_BIT_MODULE = 16; 63 constexpr uint8_t MOVE_BIT_SYS = MOVE_BIT_MODULE + 5; 64 65 constexpr int32_t DIVIDE_BASE = 1000; 66 constexpr int32_t ADDITION_DIVIDE_BASE = 10; 67 constexpr int32_t MOD_BASE = 100; 68 constexpr int32_t TRANSFER_BOUND = 30000; 69 70 // ERROR CODE 71 constexpr uint32_t SUB_SYSTEM_ID = 231; 72 constexpr uint32_t MODULE_UNKNOWN = 0; 73 constexpr uint32_t MODULE_INIT = 1; 74 constexpr uint32_t MODULE_BACKUP = 2; 75 constexpr uint32_t MODULE_RESTORE = 3; 76 constexpr uint32_t MODULE_BMS = 4; 77 constexpr uint32_t MODULE_ABILITY_MGR_SVC = 5; 78 constexpr uint32_t MODULE_KERNEL = 6; 79 constexpr uint32_t MODULE_HAP = 7; 80 constexpr uint32_t MODULE_OTHER = 9; 81 82 constexpr uint16_t ERR_VERIFY_CALLER_FAIL = 1000; 83 constexpr uint16_t ERR_ACTIVE_SESSION_FAIL = 1001; 84 constexpr uint16_t ERR_EXT_CONNECT_FAIL = 5001; 85 86 struct RadarError { RadarErrorRadarError87 RadarError(BError errCode) { UpdateByBError(errCode); } RadarErrorRadarError88 RadarError(uint32_t moduleId, BError errCode) : moduleId_(moduleId) { UpdateByBError(errCode); } RadarErrorRadarError89 RadarError(uint32_t moduleId) : moduleId_(moduleId) {} RadarErrorRadarError90 RadarError(uint32_t moduleId, uint32_t error) : moduleId_(moduleId) 91 { 92 error_ = TransferErrCode(error); 93 } 94 GenCodeRadarError95 int32_t GenCode() 96 { 97 if (error_ == 0) { 98 return ERROR_OK; 99 } 100 return static_cast<int32_t>(((SUB_SYSTEM_ID & MASK_SYS) << MOVE_BIT_SYS) 101 | ((moduleId_ & MASK_MODULE) << MOVE_BIT_MODULE) | (error_ & MASK_ERROR)); 102 } UpdateByBErrorRadarError103 void UpdateByBError(BError errCode) 104 { 105 int32_t code = errCode.GetCode(); 106 errMsg_ = errCode.ToString(); 107 error_ = TransferErrCode(code); 108 } 109 TransferErrCodeRadarError110 uint16_t TransferErrCode(int32_t code) 111 { 112 if (code == 0) { 113 return 0; 114 } 115 if (code < 0) { 116 code = -code; 117 } 118 int32_t errBase = code / DIVIDE_BASE; 119 while (errBase > TRANSFER_BOUND) { 120 errBase /= ADDITION_DIVIDE_BASE; 121 } 122 return static_cast<uint16_t>(errBase + code % MOD_BASE); 123 } 124 125 uint32_t moduleId_ = MODULE_UNKNOWN; 126 uint16_t error_ = 0; 127 std::string errMsg_ = ""; 128 }; 129 130 } // namespace OHOS::FileManagement::Backup 131 #endif // OHOS_FILEMGMT_BACKUP_RADAR_CONST_H 132