1 /*
2 * Copyright (c) 2022 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 "nlohmann/json.hpp"
21
22 #include "anonymous_string.h"
23 #include "constants.h"
24 #include "distributed_hardware_errno.h"
25 #include "distributed_hardware_log.h"
26
27 namespace OHOS {
28 namespace DistributedHardware {
29 #undef DH_LOG_TAG
30 #define DH_LOG_TAG "VersionInfo"
31
FromJsonString(const std::string & jsonStr)32 int32_t VersionInfo::FromJsonString(const std::string &jsonStr)
33 {
34 nlohmann::json jsonObj = nlohmann::json::parse(jsonStr, nullptr, false);
35 if (jsonObj.is_discarded()) {
36 DHLOGE("json string parse failed");
37 return ERR_DH_FWK_JSON_PARSE_FAILED;
38 }
39 FromJson(jsonObj, *this);
40 return DH_FWK_SUCCESS;
41 }
42
ToJsonString() const43 std::string VersionInfo::ToJsonString() const
44 {
45 nlohmann::json jsonObj;
46 ToJson(jsonObj, *this);
47 return jsonObj.dump();
48 }
49
ToJson(nlohmann::json & jsonObject,const VersionInfo & versionInfo)50 void ToJson(nlohmann::json &jsonObject, const VersionInfo &versionInfo)
51 {
52 jsonObject[DEV_ID] = versionInfo.deviceId;
53 jsonObject[DH_VER] = versionInfo.dhVersion;
54
55 nlohmann::json compVers;
56 for (const auto &compVersion : versionInfo.compVersions) {
57 nlohmann::json compVer;
58 compVer[NAME] = compVersion.second.name;
59 compVer[TYPE] = compVersion.second.dhType;
60 compVer[HANDLER] = compVersion.second.handlerVersion;
61 compVer[SOURCE_VER] = compVersion.second.sourceVersion;
62 compVer[SINK_VER] = compVersion.second.sinkVersion;
63 compVers.push_back(compVer);
64 }
65
66 jsonObject[COMP_VER] = compVers;
67 }
68
FromJson(const nlohmann::json & jsonObject,CompVersion & compVer)69 void FromJson(const nlohmann::json &jsonObject, CompVersion &compVer)
70 {
71 if (jsonObject.find(NAME) != jsonObject.end() && jsonObject[NAME].is_string()) {
72 compVer.name = jsonObject.at(NAME).get<std::string>();
73 }
74 if (jsonObject.find(TYPE) != jsonObject.end() && jsonObject[TYPE].is_number_unsigned() &&
75 jsonObject[TYPE] <= DHType::MAX_DH) {
76 compVer.dhType = jsonObject.at(TYPE).get<DHType>();
77 }
78 if (jsonObject.find(HANDLER) != jsonObject.end() && jsonObject[HANDLER].is_string()) {
79 compVer.handlerVersion = jsonObject.at(HANDLER).get<std::string>();
80 }
81 if (jsonObject.find(SOURCE_VER) != jsonObject.end() && jsonObject[SOURCE_VER].is_string()) {
82 compVer.sourceVersion = jsonObject.at(SOURCE_VER).get<std::string>();
83 }
84 if (jsonObject.find(SINK_VER) != jsonObject.end() && jsonObject[SINK_VER].is_string()) {
85 compVer.sinkVersion = jsonObject.at(SINK_VER).get<std::string>();
86 }
87 }
88
FromJson(const nlohmann::json & jsonObject,VersionInfo & versionInfo)89 void FromJson(const nlohmann::json &jsonObject, VersionInfo &versionInfo)
90 {
91 if (jsonObject.find(DEV_ID) != jsonObject.end() && jsonObject[DEV_ID].is_string()) {
92 versionInfo.deviceId = jsonObject.at(DEV_ID).get<std::string>();
93 }
94
95 if (jsonObject.find(DH_VER) != jsonObject.end() && jsonObject[DH_VER].is_string()) {
96 versionInfo.dhVersion = jsonObject.at(DH_VER).get<std::string>();
97 }
98
99 if (jsonObject.find(COMP_VER) != jsonObject.end()) {
100 for (const auto &compVerObj : jsonObject.at(COMP_VER)) {
101 CompVersion compVer;
102 FromJson(compVerObj, compVer);
103 versionInfo.compVersions.insert(std::pair<DHType, CompVersion>(compVer.dhType, compVer));
104 }
105 }
106 }
107 } // namespace DistributedHardware
108 } // namespace OHOS
109