• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     nlohmann::json compVers;
55     for (const auto &compVersion : versionInfo.compVersions) {
56         nlohmann::json compVer;
57         compVer[NAME] = compVersion.second.name;
58         compVer[TYPE] = compVersion.second.dhType;
59         compVer[HANDLER] = compVersion.second.handlerVersion;
60         compVer[SOURCE_VER] = compVersion.second.sourceVersion;
61         compVer[SINK_VER] = compVersion.second.sinkVersion;
62         compVers.push_back(compVer);
63     }
64     jsonObject[COMP_VER] = compVers;
65 }
66 
FromJson(const nlohmann::json & jsonObject,CompVersion & compVer)67 void FromJson(const nlohmann::json &jsonObject, CompVersion &compVer)
68 {
69     if (jsonObject.find(NAME) != jsonObject.end() && jsonObject[NAME].is_string()) {
70         compVer.name = jsonObject.at(NAME).get<std::string>();
71     }
72     if (jsonObject.find(TYPE) != jsonObject.end() && jsonObject[TYPE].is_number_unsigned() &&
73         jsonObject[TYPE] <= DHType::MAX_DH) {
74         compVer.dhType = jsonObject.at(TYPE).get<DHType>();
75     }
76     if (jsonObject.find(HANDLER) != jsonObject.end() && jsonObject[HANDLER].is_string()) {
77         compVer.handlerVersion = jsonObject.at(HANDLER).get<std::string>();
78     }
79     if (jsonObject.find(SOURCE_VER) != jsonObject.end() && jsonObject[SOURCE_VER].is_string()) {
80         compVer.sourceVersion = jsonObject.at(SOURCE_VER).get<std::string>();
81     }
82     if (jsonObject.find(SINK_VER) != jsonObject.end() && jsonObject[SINK_VER].is_string()) {
83         compVer.sinkVersion = jsonObject.at(SINK_VER).get<std::string>();
84     }
85 }
86 
FromJson(const nlohmann::json & jsonObject,VersionInfo & versionInfo)87 void FromJson(const nlohmann::json &jsonObject, VersionInfo &versionInfo)
88 {
89     if (jsonObject.find(DEV_ID) != jsonObject.end() && jsonObject[DEV_ID].is_string()) {
90         versionInfo.deviceId = jsonObject.at(DEV_ID).get<std::string>();
91     }
92 
93     if (jsonObject.find(DH_VER) != jsonObject.end() && jsonObject[DH_VER].is_string()) {
94         versionInfo.dhVersion = jsonObject.at(DH_VER).get<std::string>();
95     }
96 
97     if (jsonObject.find(COMP_VER) != jsonObject.end()) {
98         for (const auto &compVerObj : jsonObject.at(COMP_VER)) {
99             CompVersion compVer;
100             FromJson(compVerObj, compVer);
101             versionInfo.compVersions.insert(std::pair<DHType, CompVersion>(compVer.dhType, compVer));
102         }
103     }
104 }
105 } // namespace DistributedHardware
106 } // namespace OHOS
107