1 /*
2 * Copyright (c) 2022-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 #include "version_info.h"
17
18 #include <string>
19
20 #include "cJSON.h"
21
22 #include "anonymous_string.h"
23 #include "constants.h"
24 #include "distributed_hardware_errno.h"
25 #include "distributed_hardware_log.h"
26 #include "dh_utils_tool.h"
27
28 namespace OHOS {
29 namespace DistributedHardware {
30 #undef DH_LOG_TAG
31 #define DH_LOG_TAG "VersionInfo"
32
FromJsonString(const std::string & jsonStr)33 int32_t VersionInfo::FromJsonString(const std::string &jsonStr)
34 {
35 if (!IsJsonLengthValid(jsonStr)) {
36 return ERR_DH_FWK_PARA_INVALID;
37 }
38 cJSON *jsonObj = cJSON_Parse(jsonStr.c_str());
39 if (jsonObj == NULL) {
40 DHLOGE("json string parse failed");
41 return ERR_DH_FWK_JSON_PARSE_FAILED;
42 }
43 FromJson(jsonObj, *this);
44 cJSON_Delete(jsonObj);
45 return DH_FWK_SUCCESS;
46 }
47
ToJsonString() const48 std::string VersionInfo::ToJsonString() const
49 {
50 cJSON *jsonObj = cJSON_CreateObject();
51 if (jsonObj == NULL) {
52 DHLOGE("Failed to create cJSON object.");
53 return "";
54 }
55 ToJson(jsonObj, *this);
56 char *cjson = cJSON_PrintUnformatted(jsonObj);
57 if (cjson == nullptr) {
58 cJSON_Delete(jsonObj);
59 return "";
60 }
61 std::string result(cjson);
62 cJSON_free(cjson);
63 cJSON_Delete(jsonObj);
64 return result;
65 }
66
ToJson(cJSON * jsonObject,const CompVersion & compVer)67 void ToJson(cJSON *jsonObject, const CompVersion &compVer)
68 {
69 if (jsonObject == nullptr) {
70 DHLOGE("Json pointer is nullptr!");
71 return;
72 }
73 cJSON_AddStringToObject(jsonObject, NAME.c_str(), compVer.name.c_str());
74 cJSON_AddNumberToObject(jsonObject, TYPE.c_str(), (double)compVer.dhType);
75 cJSON_AddStringToObject(jsonObject, HANDLER.c_str(), compVer.handlerVersion.c_str());
76 cJSON_AddStringToObject(jsonObject, SOURCE_VER.c_str(), compVer.sourceVersion.c_str());
77 cJSON_AddStringToObject(jsonObject, SINK_VER.c_str(), compVer.sinkVersion.c_str());
78 if (compVer.haveFeature) {
79 cJSON *sourceFeatureFilters = cJSON_CreateArray();
80 if (sourceFeatureFilters == NULL) {
81 DHLOGE("Failed to create cJSON object.");
82 return;
83 }
84 cJSON *sinkSupportedFeatures = cJSON_CreateArray();
85 if (sinkSupportedFeatures == NULL) {
86 cJSON_Delete(sourceFeatureFilters);
87 DHLOGE("Failed to create cJSON object.");
88 return;
89 }
90 for (const auto &filter : compVer.sourceFeatureFilters) {
91 cJSON_AddItemToArray(sourceFeatureFilters, cJSON_CreateString(filter.c_str()));
92 }
93 cJSON_AddItemToObject(jsonObject, SOURCE_FEATURE_FILTER.c_str(), sourceFeatureFilters);
94 for (const auto &feature : compVer.sinkSupportedFeatures) {
95 cJSON_AddItemToArray(sinkSupportedFeatures, cJSON_CreateString(feature.c_str()));
96 }
97 cJSON_AddItemToObject(jsonObject, SINK_SUPPORTED_FEATURE.c_str(), sinkSupportedFeatures);
98 }
99 }
100
ToJson(cJSON * jsonObject,const VersionInfo & versionInfo)101 void ToJson(cJSON *jsonObject, const VersionInfo &versionInfo)
102 {
103 if (jsonObject == nullptr) {
104 DHLOGE("Json pointer is nullptr!");
105 return;
106 }
107 cJSON_AddStringToObject(jsonObject, DEV_ID.c_str(), versionInfo.deviceId.c_str());
108 cJSON_AddStringToObject(jsonObject, DH_VER.c_str(), versionInfo.dhVersion.c_str());
109
110 cJSON *compVers = cJSON_CreateArray();
111 if (compVers == NULL) {
112 DHLOGE("Failed to create cJSON array.");
113 return;
114 }
115 for (const auto &compVersion : versionInfo.compVersions) {
116 cJSON *compVer = cJSON_CreateObject();
117 if (compVer == NULL) {
118 cJSON_Delete(compVers);
119 DHLOGE("Failed to create cJSON object.");
120 return;
121 }
122 ToJson(compVer, compVersion.second);
123 cJSON_AddItemToArray(compVers, compVer);
124 }
125 cJSON_AddItemToObject(jsonObject, COMP_VER.c_str(), compVers);
126 }
127
FromJson(const cJSON * jsonObject,CompVersion & compVer)128 void FromJson(const cJSON *jsonObject, CompVersion &compVer)
129 {
130 if (jsonObject == nullptr) {
131 DHLOGE("Json pointer is nullptr!");
132 return;
133 }
134 cJSON *nameJson = cJSON_GetObjectItem(jsonObject, NAME.c_str());
135 if (IsString(nameJson)) {
136 compVer.name = nameJson->valuestring;
137 }
138 cJSON *typeJson = cJSON_GetObjectItem(jsonObject, TYPE.c_str());
139 if (IsUInt32(typeJson) && (DHType)typeJson->valueint <= DHType::MAX_DH) {
140 compVer.dhType = (DHType)typeJson->valueint;
141 }
142 cJSON *handlerJson = cJSON_GetObjectItem(jsonObject, HANDLER.c_str());
143 if (IsString(handlerJson)) {
144 compVer.handlerVersion = handlerJson->valuestring;
145 }
146 cJSON *sourceVerJson = cJSON_GetObjectItem(jsonObject, SOURCE_VER.c_str());
147 if (IsString(sourceVerJson)) {
148 compVer.sourceVersion = sourceVerJson->valuestring;
149 }
150 cJSON *sinkVerJson = cJSON_GetObjectItem(jsonObject, SINK_VER.c_str());
151 if (IsString(sinkVerJson)) {
152 compVer.sinkVersion = sinkVerJson->valuestring;
153 }
154 cJSON *sourceFeatureFilters = cJSON_GetObjectItem(jsonObject, SOURCE_FEATURE_FILTER.c_str());
155 cJSON *sinkSupportedFeatures = cJSON_GetObjectItem(jsonObject, SINK_SUPPORTED_FEATURE.c_str());
156 if (sourceFeatureFilters || sinkSupportedFeatures) {
157 compVer.haveFeature = true;
158 if (sourceFeatureFilters) {
159 cJSON *filterObj = nullptr;
160 compVer.sourceFeatureFilters.clear();
161 cJSON_ArrayForEach(filterObj, sourceFeatureFilters) {
162 compVer.sourceFeatureFilters.push_back(filterObj->valuestring);
163 }
164 }
165 if (sinkSupportedFeatures) {
166 cJSON *featureObj = nullptr;
167 compVer.sinkSupportedFeatures.clear();
168 cJSON_ArrayForEach(featureObj, sinkSupportedFeatures) {
169 compVer.sinkSupportedFeatures.push_back(featureObj->valuestring);
170 }
171 }
172 } else {
173 compVer.haveFeature = false;
174 }
175 }
176
FromJson(const cJSON * jsonObject,VersionInfo & versionInfo)177 void FromJson(const cJSON *jsonObject, VersionInfo &versionInfo)
178 {
179 if (jsonObject == nullptr) {
180 DHLOGE("Json pointer is nullptr!");
181 return;
182 }
183 cJSON *devIdJson = cJSON_GetObjectItem(jsonObject, DEV_ID.c_str());
184 if (IsString(devIdJson)) {
185 versionInfo.deviceId = devIdJson->valuestring;
186 }
187
188 cJSON *dhVerJson = cJSON_GetObjectItem(jsonObject, DH_VER.c_str());
189 if (IsString(dhVerJson)) {
190 versionInfo.dhVersion = dhVerJson->valuestring;
191 }
192
193 const cJSON *compVer = cJSON_GetObjectItem(jsonObject, COMP_VER.c_str());
194 if (compVer != NULL) {
195 cJSON *compVerObj = nullptr;
196 cJSON_ArrayForEach(compVerObj, compVer) {
197 CompVersion compVerValue;
198 FromJson(compVerObj, compVerValue);
199 versionInfo.compVersions.insert(std::pair<DHType, CompVersion>(compVerValue.dhType, compVerValue));
200 }
201 }
202 }
203 } // namespace DistributedHardware
204 } // namespace OHOS
205