1 /*
2  * Copyright (c) 2022-2024 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 VersionInfo & versionInfo)67 void ToJson(cJSON *jsonObject, const VersionInfo &versionInfo)
68 {
69     if (jsonObject == nullptr) {
70         DHLOGE("Json pointer is nullptr!");
71         return;
72     }
73     cJSON_AddStringToObject(jsonObject, DEV_ID.c_str(), versionInfo.deviceId.c_str());
74     cJSON_AddStringToObject(jsonObject, DH_VER.c_str(), versionInfo.dhVersion.c_str());
75 
76     cJSON *compVers = cJSON_CreateArray();
77     if (compVers == NULL) {
78         DHLOGE("Failed to create cJSON array.");
79         return;
80     }
81     for (const auto &compVersion : versionInfo.compVersions) {
82         cJSON *compVer = cJSON_CreateObject();
83         if (compVer == NULL) {
84             cJSON_Delete(compVers);
85             DHLOGE("Failed to create cJSON object.");
86             return;
87         }
88         cJSON_AddStringToObject(compVer, NAME.c_str(), compVersion.second.name.c_str());
89         cJSON_AddNumberToObject(compVer, TYPE.c_str(), (double)compVersion.second.dhType);
90         cJSON_AddStringToObject(compVer, HANDLER.c_str(), compVersion.second.handlerVersion.c_str());
91         cJSON_AddStringToObject(compVer, SOURCE_VER.c_str(), compVersion.second.sourceVersion.c_str());
92         cJSON_AddStringToObject(compVer, SINK_VER.c_str(), compVersion.second.sinkVersion.c_str());
93         cJSON_AddItemToArray(compVers, compVer);
94     }
95     cJSON_AddItemToObject(jsonObject, COMP_VER.c_str(), compVers);
96 }
97 
FromJson(const cJSON * jsonObject,CompVersion & compVer)98 void FromJson(const cJSON *jsonObject, CompVersion &compVer)
99 {
100     if (jsonObject == nullptr) {
101         DHLOGE("Json pointer is nullptr!");
102         return;
103     }
104     if (IsString(jsonObject, NAME)) {
105         compVer.name = cJSON_GetObjectItem(jsonObject, NAME.c_str())->valuestring;
106     }
107     if (IsUInt32(jsonObject, TYPE) &&
108         (DHType)cJSON_GetObjectItem(jsonObject, TYPE.c_str())->valuedouble <= DHType::MAX_DH) {
109         compVer.dhType = (DHType)(cJSON_GetObjectItem(jsonObject, TYPE.c_str())->valuedouble);
110     }
111     if (IsString(jsonObject, HANDLER)) {
112         compVer.handlerVersion = cJSON_GetObjectItem(jsonObject, HANDLER.c_str())->valuestring;
113     }
114     if (IsString(jsonObject, SOURCE_VER)) {
115         compVer.sourceVersion = cJSON_GetObjectItem(jsonObject, SOURCE_VER.c_str())->valuestring;
116     }
117     if (IsString(jsonObject, SINK_VER)) {
118         compVer.sinkVersion = cJSON_GetObjectItem(jsonObject, SINK_VER.c_str())->valuestring;
119     }
120 }
121 
FromJson(const cJSON * jsonObject,VersionInfo & versionInfo)122 void FromJson(const cJSON *jsonObject, VersionInfo &versionInfo)
123 {
124     if (jsonObject == nullptr) {
125         DHLOGE("Json pointer is nullptr!");
126         return;
127     }
128     if (IsString(jsonObject, DEV_ID)) {
129         versionInfo.deviceId = cJSON_GetObjectItem(jsonObject, DEV_ID.c_str())->valuestring;
130     }
131 
132     if (IsString(jsonObject, DH_VER)) {
133         versionInfo.dhVersion = cJSON_GetObjectItem(jsonObject, DH_VER.c_str())->valuestring;
134     }
135 
136     const cJSON *compVer = cJSON_GetObjectItem(jsonObject, COMP_VER.c_str());
137     if (compVer != NULL) {
138         cJSON *compVerObj = nullptr;
139         cJSON_ArrayForEach(compVerObj, compVer) {
140             CompVersion compVerValue;
141             FromJson(compVerObj, compVerValue);
142             versionInfo.compVersions.insert(std::pair<DHType, CompVersion>(compVerValue.dhType, compVerValue));
143         }
144     }
145 }
146 } // namespace DistributedHardware
147 } // namespace OHOS
148