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 #include "update_helper.h"
17
18 #include <cstring>
19 #include <string>
20
21 #include "encrypt_utils.h"
22 #include "update_log.h"
23
24 namespace OHOS {
25 namespace UpdateEngine {
ToString() const26 std::string UpgradeInfo::ToString() const
27 {
28 std::string output = "upgradeApp:" + upgradeApp;
29 output += ",businessType(vender:" + businessType.vendor;
30 output += ",subType:" + std::to_string(CAST_INT(businessType.subType));
31 output += "),upgradeDevId:" + EncryptUtils::EncryptString(upgradeDevId);
32 output += ",controlDevId:" + EncryptUtils::EncryptString(controlDevId);
33 return output;
34 }
35
GetJsonBuilder()36 JsonBuilder BusinessType::GetJsonBuilder()
37 {
38 return JsonBuilder()
39 .Append("{")
40 .Append("vendor", vendor)
41 .Append("subType", CAST_INT(subType))
42 .Append("}");
43 }
44
GetJsonBuilder()45 JsonBuilder VersionDigestInfo::GetJsonBuilder()
46 {
47 return JsonBuilder().Append("{").Append("versionDigest", versionDigest).Append("}");
48 }
49
GetJsonBuilder()50 JsonBuilder ErrorMessage::GetJsonBuilder()
51 {
52 return JsonBuilder()
53 .Append("{")
54 .Append("errorCode", errorCode)
55 .Append("errorMessage", errorMessage)
56 .Append("}");
57 }
58
GetJsonBuilder()59 JsonBuilder SubscribeInfo::GetJsonBuilder()
60 {
61 return JsonBuilder()
62 .Append("{")
63 .Append("upgradeApp", upgradeApp)
64 .Append("businessType", businessType.GetJsonBuilder())
65 .Append("abilityName", abilityName)
66 .Append("subscriberDevId", subscriberDevId)
67 .Append("devUpgradeId", devUpgradeId)
68 .Append("}");
69 }
70
GetJsonBuilder()71 JsonBuilder EventInfo::GetJsonBuilder()
72 {
73 return JsonBuilder()
74 .Append("{")
75 .Append("eventId", CAST_INT(eventId))
76 .Append("taskBody", taskBody.GetJsonBuilder(eventId))
77 .Append("}");
78 }
79
GetJsonBuilder()80 JsonBuilder DescriptionInfo::GetJsonBuilder()
81 {
82 return JsonBuilder()
83 .Append("{")
84 .Append("descriptionType", CAST_INT(descriptionType))
85 .Append("content", content)
86 .Append("}");
87 }
88
GetJsonBuilder()89 JsonBuilder VersionComponent::GetJsonBuilder()
90 {
91 return JsonBuilder()
92 .Append("{")
93 .Append("componentId", componentId)
94 .Append("componentType", componentType)
95 .Append("upgradeAction", upgradeAction)
96 .Append("displayVersion", displayVersion)
97 .Append("innerVersion", innerVersion)
98 .Append("size", static_cast<int64_t>(size))
99 .Append("effectiveMode", static_cast<int32_t>(effectiveMode))
100 .Append("descriptionInfo", descriptionInfo.GetJsonBuilder())
101 .Append("componentExtra", componentExtra)
102 .Append("}");
103 }
104
GetJsonBuilder(VersionComponent & versionComponent)105 JsonBuilder GetJsonBuilder(VersionComponent &versionComponent)
106 {
107 return versionComponent.GetJsonBuilder();
108 }
109
GetJsonBuilder(ErrorMessage & errorMessage)110 JsonBuilder GetJsonBuilder(ErrorMessage &errorMessage)
111 {
112 return errorMessage.GetJsonBuilder();
113 }
114
115 template <typename T>
GetArrayJsonBuidlerList(const std::vector<T> & valueList)116 std::vector<JsonBuilder> GetArrayJsonBuidlerList(const std::vector<T> &valueList)
117 {
118 std::vector<JsonBuilder> jsonBuilderList;
119 for (T value : valueList) {
120 jsonBuilderList.push_back(GetJsonBuilder(value));
121 }
122 return jsonBuilderList;
123 }
124
GetJsonBuilder(EventId eventId)125 JsonBuilder TaskBody::GetJsonBuilder(EventId eventId)
126 {
127 JsonBuilder jsonBuilder = JsonBuilder();
128 auto iter = g_taskBodyTemplateMap.find(eventId);
129 if (iter == g_taskBodyTemplateMap.end()) {
130 ENGINE_LOGE("GetJsonBuilder eventId not find");
131 return jsonBuilder;
132 }
133 jsonBuilder.Append("{");
134 uint32_t taskBodyTemplate = iter->second;
135 if (taskBodyTemplate & VERSION_DIGEST_INFO) {
136 jsonBuilder.Append("versionDigestInfo", versionDigestInfo.GetJsonBuilder());
137 }
138 if (taskBodyTemplate & UPGRADE_STATUS) {
139 jsonBuilder.Append("status", CAST_INT(status));
140 }
141 if (taskBodyTemplate & SUB_STATUS) {
142 jsonBuilder.Append("subStatus", subStatus);
143 }
144 if (taskBodyTemplate & PROGRESS) {
145 jsonBuilder.Append("progress", progress);
146 }
147 if (taskBodyTemplate & INSTALL_MODE) {
148 jsonBuilder.Append("installMode", installMode);
149 }
150 if (taskBodyTemplate & ERROR_MESSAGE) {
151 jsonBuilder.Append("errorMessages", GetArrayJsonBuidlerList(errorMessages));
152 }
153 if (taskBodyTemplate & VERSION_COMPONENT) {
154 jsonBuilder.Append("versionComponents", GetArrayJsonBuidlerList(versionComponents));
155 }
156 return jsonBuilder.Append("}");
157 }
158
BuildEventDevId(const UpgradeInfo & info)159 std::string UpdateHelper::BuildEventDevId(const UpgradeInfo &info)
160 {
161 return std::string("{") + std::string("upgradeDevId: ") + info.upgradeDevId
162 + std::string(", controlDevId: ") + info.controlDevId + std::string(" }");
163 }
164
IsUpgradeFailed(UpgradeStatus status)165 bool UpdateHelper::IsUpgradeFailed(UpgradeStatus status)
166 {
167 return status == UpgradeStatus::DOWNLOAD_FAIL ||
168 status == UpgradeStatus::PACKAGE_TRANS_FAIL ||
169 status == UpgradeStatus::INSTALL_FAIL ||
170 status == UpgradeStatus::UPDATE_FAIL ||
171 status == UpgradeStatus::VERIFY_FAIL;
172 }
173 } // namespace UpdateEngine
174 } // namespace OHOS
175