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
GetDHSubtype() const93 std::string CapabilityInfo::GetDHSubtype() const
94 {
95 return dhSubtype_;
96 }
97
SetDHSubtype(const std::string & dhSubtype)98 void CapabilityInfo::SetDHSubtype(const std::string &dhSubtype)
99 {
100 this->dhSubtype_ = dhSubtype;
101 }
102
GetKey() const103 std::string CapabilityInfo::GetKey() const
104 {
105 std::string kvStoreKey;
106 kvStoreKey.append(deviceId_);
107 kvStoreKey.append(RESOURCE_SEPARATOR);
108 kvStoreKey.append(dhId_);
109 return kvStoreKey;
110 }
111
GetAnonymousKey() const112 std::string CapabilityInfo::GetAnonymousKey() const
113 {
114 std::string kvStoreKey;
115 kvStoreKey.append(GetAnonyString(deviceId_));
116 kvStoreKey.append(RESOURCE_SEPARATOR);
117 kvStoreKey.append(GetAnonyString(dhId_));
118 return kvStoreKey;
119 }
120
FromJsonString(const std::string & jsonStr)121 int32_t CapabilityInfo::FromJsonString(const std::string &jsonStr)
122 {
123 nlohmann::json jsonObj = nlohmann::json::parse(jsonStr, nullptr, false);
124 if (jsonObj.is_discarded()) {
125 DHLOGE("jsonStr parse failed");
126 return ERR_DH_FWK_JSON_PARSE_FAILED;
127 }
128
129 FromJson(jsonObj, *this);
130 return DH_FWK_SUCCESS;
131 }
132
ToJsonString()133 std::string CapabilityInfo::ToJsonString()
134 {
135 nlohmann::json jsonObj;
136 ToJson(jsonObj, *this);
137 return jsonObj.dump();
138 }
139
Compare(const CapabilityInfo & capInfo)140 bool CapabilityInfo::Compare(const CapabilityInfo& capInfo)
141 {
142 if (strcmp(this->deviceId_.c_str(), capInfo.deviceId_.c_str()) != 0) {
143 DHLOGE("deviceId is not equal");
144 return false;
145 }
146 if (strcmp(this->dhId_.c_str(), capInfo.dhId_.c_str()) != 0) {
147 DHLOGE("dhId is not equal");
148 return false;
149 }
150 if (strcmp(this->deviceName_.c_str(), capInfo.deviceName_.c_str()) != 0) {
151 DHLOGE("deviceName is not equal");
152 return false;
153 }
154 if (this->deviceType_ != capInfo.deviceType_) {
155 DHLOGE("deviceType is not equal");
156 return false;
157 }
158 if (this->dhType_ != capInfo.dhType_) {
159 DHLOGE("dhType is not equal");
160 return false;
161 }
162 if (strcmp(this->dhAttrs_.c_str(), capInfo.dhAttrs_.c_str()) != 0) {
163 DHLOGE("dhAttrs is not equal");
164 return false;
165 }
166 if (strcmp(this->dhSubtype_.c_str(), capInfo.dhSubtype_.c_str()) != 0) {
167 DHLOGE("dhSubtype is not equal");
168 return false;
169 }
170 return true;
171 }
172
ToJson(nlohmann::json & jsonObject,const CapabilityInfo & capability)173 void ToJson(nlohmann::json &jsonObject, const CapabilityInfo &capability)
174 {
175 jsonObject[DH_ID] = capability.GetDHId();
176 jsonObject[DEV_ID] = capability.GetDeviceId();
177 jsonObject[DEV_NAME] = capability.GetDeviceName();
178 jsonObject[DEV_TYPE] = capability.GetDeviceType();
179 jsonObject[DH_TYPE] = capability.GetDHType();
180 jsonObject[DH_ATTRS] = capability.GetDHAttrs();
181 jsonObject[DH_SUBTYPE] = capability.GetDHSubtype();
182 }
183
FromJson(const nlohmann::json & jsonObject,CapabilityInfo & capability)184 void FromJson(const nlohmann::json &jsonObject, CapabilityInfo &capability)
185 {
186 if (!IsString(jsonObject, DH_ID)) {
187 DHLOGE("DH_ID is invalid!");
188 return;
189 }
190 capability.SetDHId(jsonObject.at(DH_ID).get<std::string>());
191 if (!IsString(jsonObject, DEV_ID)) {
192 DHLOGE("DEV_ID is invalid!");
193 return;
194 }
195 capability.SetDeviceId(jsonObject.at(DEV_ID).get<std::string>());
196 if (!IsString(jsonObject, DEV_NAME)) {
197 DHLOGE("DEV_NAME is invalid!");
198 return;
199 }
200 capability.SetDeviceName(jsonObject.at(DEV_NAME).get<std::string>());
201 if (!IsUInt16(jsonObject, DEV_TYPE)) {
202 DHLOGE("DEV_TYPE is invalid!");
203 return;
204 }
205 capability.SetDeviceType(jsonObject.at(DEV_TYPE).get<uint16_t>());
206 if (!IsUInt32(jsonObject, DH_TYPE)) {
207 DHLOGE("DH_TYPE is invalid!");
208 return;
209 }
210 capability.SetDHType(jsonObject.at(DH_TYPE).get<DHType>());
211 if (!IsString(jsonObject, DH_ATTRS)) {
212 DHLOGE("DH_ATTRS is invalid!");
213 return;
214 }
215 capability.SetDHAttrs(jsonObject.at(DH_ATTRS).get<std::string>());
216 if (!IsString(jsonObject, DH_SUBTYPE)) {
217 DHLOGE("DH_SUBTYPE is invalid!");
218 return;
219 }
220 capability.SetDHSubtype(jsonObject.at(DH_SUBTYPE).get<std::string>());
221 }
222 } // namespace DistributedHardware
223 } // namespace OHOS
224