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 "capability_info.h"
17
18 #include <string>
19
20 #include "nlohmann/json.hpp"
21
22 #include "anonymous_string.h"
23 #include "constants.h"
24 #include "dh_utils_tool.h"
25 #include "distributed_hardware_errno.h"
26 #include "distributed_hardware_log.h"
27
28 namespace OHOS {
29 namespace DistributedHardware {
30 #undef DH_LOG_TAG
31 #define DH_LOG_TAG "CapabilityInfo"
32
GetDHId() const33 std::string CapabilityInfo::GetDHId() const
34 {
35 return dhId_;
36 }
37
SetDHId(const std::string & dhId)38 void CapabilityInfo::SetDHId(const std::string &dhId)
39 {
40 this->dhId_ = dhId;
41 }
42
GetDeviceId() const43 std::string CapabilityInfo::GetDeviceId() const
44 {
45 return deviceId_;
46 }
47
SetDeviceId(const std::string & deviceId)48 void CapabilityInfo::SetDeviceId(const std::string &deviceId)
49 {
50 this->deviceId_ = deviceId;
51 }
52
GetDeviceName() const53 std::string CapabilityInfo::GetDeviceName() const
54 {
55 return deviceName_;
56 }
57
SetDeviceName(const std::string & deviceName)58 void CapabilityInfo::SetDeviceName(const std::string &deviceName)
59 {
60 this->deviceName_ = deviceName;
61 }
62
GetDeviceType() const63 uint16_t CapabilityInfo::GetDeviceType() const
64 {
65 return deviceType_;
66 }
67
SetDeviceType(uint16_t deviceType)68 void CapabilityInfo::SetDeviceType(uint16_t deviceType)
69 {
70 this->deviceType_ = deviceType;
71 }
72
GetDHType() const73 DHType CapabilityInfo::GetDHType() const
74 {
75 return dhType_;
76 }
77
SetDHType(const DHType dhType)78 void CapabilityInfo::SetDHType(const DHType dhType)
79 {
80 this->dhType_ = dhType;
81 }
82
GetDHAttrs() const83 std::string CapabilityInfo::GetDHAttrs() const
84 {
85 return dhAttrs_;
86 }
87
SetDHAttrs(const std::string & dhAttrs)88 void CapabilityInfo::SetDHAttrs(const std::string &dhAttrs)
89 {
90 this->dhAttrs_ = dhAttrs;
91 }
92
GetKey() const93 std::string CapabilityInfo::GetKey() const
94 {
95 std::string kvStoreKey;
96 kvStoreKey.append(deviceId_);
97 kvStoreKey.append(RESOURCE_SEPARATOR);
98 kvStoreKey.append(dhId_);
99 return kvStoreKey;
100 }
101
GetAnonymousKey() const102 std::string CapabilityInfo::GetAnonymousKey() const
103 {
104 std::string kvStoreKey;
105 kvStoreKey.append(GetAnonyString(deviceId_));
106 kvStoreKey.append(RESOURCE_SEPARATOR);
107 kvStoreKey.append(GetAnonyString(dhId_));
108 return kvStoreKey;
109 }
110
FromJsonString(const std::string & jsonStr)111 int32_t CapabilityInfo::FromJsonString(const std::string &jsonStr)
112 {
113 nlohmann::json jsonObj = nlohmann::json::parse(jsonStr, nullptr, false);
114 if (jsonObj.is_discarded()) {
115 DHLOGE("jsonStr parse failed");
116 return ERR_DH_FWK_JSON_PARSE_FAILED;
117 }
118
119 FromJson(jsonObj, *this);
120 return DH_FWK_SUCCESS;
121 }
122
ToJsonString()123 std::string CapabilityInfo::ToJsonString()
124 {
125 nlohmann::json jsonObj;
126 ToJson(jsonObj, *this);
127 return jsonObj.dump();
128 }
129
Compare(const CapabilityInfo & capInfo)130 bool CapabilityInfo::Compare(const CapabilityInfo& capInfo)
131 {
132 if (strcmp(this->deviceId_.c_str(), capInfo.deviceId_.c_str()) != 0) {
133 DHLOGE("deviceId is not equal");
134 return false;
135 }
136 if (strcmp(this->dhId_.c_str(), capInfo.dhId_.c_str()) != 0) {
137 DHLOGE("dhId is not equal");
138 return false;
139 }
140 if (strcmp(this->deviceName_.c_str(), capInfo.deviceName_.c_str()) != 0) {
141 DHLOGE("deviceName is not equal");
142 return false;
143 }
144 if (this->deviceType_ != capInfo.deviceType_) {
145 DHLOGE("deviceType is not equal");
146 return false;
147 }
148 if (this->dhType_ != capInfo.dhType_) {
149 DHLOGE("dhType is not equal");
150 return false;
151 }
152 if (strcmp(this->dhAttrs_.c_str(), capInfo.dhAttrs_.c_str()) != 0) {
153 DHLOGE("dhAttrs is not equal");
154 return false;
155 }
156 return true;
157 }
158
ToJson(nlohmann::json & jsonObject,const CapabilityInfo & capability)159 void ToJson(nlohmann::json &jsonObject, const CapabilityInfo &capability)
160 {
161 jsonObject[DH_ID] = capability.GetDHId();
162 jsonObject[DEV_ID] = capability.GetDeviceId();
163 jsonObject[DEV_NAME] = capability.GetDeviceName();
164 jsonObject[DEV_TYPE] = capability.GetDeviceType();
165 jsonObject[DH_TYPE] = capability.GetDHType();
166 jsonObject[DH_ATTRS] = capability.GetDHAttrs();
167 }
168
FromJson(const nlohmann::json & jsonObject,CapabilityInfo & capability)169 void FromJson(const nlohmann::json &jsonObject, CapabilityInfo &capability)
170 {
171 if (!IsString(jsonObject, DH_ID)) {
172 DHLOGE("DH_ID is invalid!");
173 return;
174 }
175 capability.SetDHId(jsonObject.at(DH_ID).get<std::string>());
176 if (!IsString(jsonObject, DEV_ID)) {
177 DHLOGE("DEV_ID is invalid!");
178 return;
179 }
180 capability.SetDeviceId(jsonObject.at(DEV_ID).get<std::string>());
181 if (!IsString(jsonObject, DEV_NAME)) {
182 DHLOGE("DEV_NAME is invalid!");
183 return;
184 }
185 capability.SetDeviceName(jsonObject.at(DEV_NAME).get<std::string>());
186 if (!IsUInt16(jsonObject, DEV_TYPE)) {
187 DHLOGE("DEV_TYPE is invalid!");
188 return;
189 }
190 capability.SetDeviceType(jsonObject.at(DEV_TYPE).get<uint16_t>());
191 if (!IsUInt32(jsonObject, DH_TYPE)) {
192 DHLOGE("DH_TYPE is invalid!");
193 return;
194 }
195 capability.SetDHType(jsonObject.at(DH_TYPE).get<DHType>());
196 if (!IsString(jsonObject, DH_ATTRS)) {
197 DHLOGE("DH_ATTRS is invalid!");
198 return;
199 }
200 capability.SetDHAttrs(jsonObject.at(DH_ATTRS).get<std::string>());
201 }
202 } // namespace DistributedHardware
203 } // namespace OHOS
204