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 "cJSON.h"
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 if (!IsJsonLengthValid(jsonStr)) {
124 return ERR_DH_FWK_PARA_INVALID;
125 }
126 cJSON *jsonObj = cJSON_Parse(jsonStr.c_str());
127 if (jsonObj == NULL) {
128 DHLOGE("jsonStr parse failed");
129 return ERR_DH_FWK_JSON_PARSE_FAILED;
130 }
131
132 FromJson(jsonObj, *this);
133 cJSON_Delete(jsonObj);
134 return DH_FWK_SUCCESS;
135 }
136
ToJsonString()137 std::string CapabilityInfo::ToJsonString()
138 {
139 cJSON *jsonObj = cJSON_CreateObject();
140 if (jsonObj == NULL) {
141 DHLOGE("Failed to create cJSON object.");
142 return "";
143 }
144 ToJson(jsonObj, *this);
145 char *cjson = cJSON_PrintUnformatted(jsonObj);
146 if (cjson == nullptr) {
147 cJSON_Delete(jsonObj);
148 return "";
149 }
150 std::string jsonString(cjson);
151 cJSON_free(cjson);
152 cJSON_Delete(jsonObj);
153 return jsonString;
154 }
155
Compare(const CapabilityInfo & capInfo)156 bool CapabilityInfo::Compare(const CapabilityInfo& capInfo)
157 {
158 if (strcmp(this->deviceId_.c_str(), capInfo.deviceId_.c_str()) != 0) {
159 DHLOGE("deviceId is not equal");
160 return false;
161 }
162 if (strcmp(this->dhId_.c_str(), capInfo.dhId_.c_str()) != 0) {
163 DHLOGE("dhId is not equal");
164 return false;
165 }
166 if (strcmp(this->deviceName_.c_str(), capInfo.deviceName_.c_str()) != 0) {
167 DHLOGE("deviceName is not equal");
168 return false;
169 }
170 if (this->deviceType_ != capInfo.deviceType_) {
171 DHLOGE("deviceType is not equal");
172 return false;
173 }
174 if (this->dhType_ != capInfo.dhType_) {
175 DHLOGE("dhType is not equal");
176 return false;
177 }
178 if (strcmp(this->dhAttrs_.c_str(), capInfo.dhAttrs_.c_str()) != 0) {
179 DHLOGE("dhAttrs is not equal");
180 return false;
181 }
182 if (strcmp(this->dhSubtype_.c_str(), capInfo.dhSubtype_.c_str()) != 0) {
183 DHLOGE("dhSubtype is not equal");
184 return false;
185 }
186 return true;
187 }
188
ToJson(cJSON * jsonObject,const CapabilityInfo & capability)189 void ToJson(cJSON *jsonObject, const CapabilityInfo &capability)
190 {
191 if (jsonObject == nullptr) {
192 DHLOGE("Json pointer is nullptr!");
193 return;
194 }
195 cJSON_AddStringToObject(jsonObject, DH_ID.c_str(), capability.GetDHId().c_str());
196 cJSON_AddStringToObject(jsonObject, DEV_ID.c_str(), capability.GetDeviceId().c_str());
197 cJSON_AddStringToObject(jsonObject, DEV_NAME.c_str(), capability.GetDeviceName().c_str());
198 cJSON_AddNumberToObject(jsonObject, DEV_TYPE.c_str(), (double)capability.GetDeviceType());
199 cJSON_AddNumberToObject(jsonObject, DH_TYPE.c_str(), (double)capability.GetDHType());
200 cJSON_AddStringToObject(jsonObject, DH_ATTRS.c_str(), capability.GetDHAttrs().c_str());
201 cJSON_AddStringToObject(jsonObject, DH_SUBTYPE.c_str(), capability.GetDHSubtype().c_str());
202 }
203
FromJson(const cJSON * jsonObject,CapabilityInfo & capability)204 void FromJson(const cJSON *jsonObject, CapabilityInfo &capability)
205 {
206 if (jsonObject == nullptr) {
207 DHLOGE("Json pointer is nullptr!");
208 return;
209 }
210 if (!IsString(jsonObject, DH_ID)) {
211 DHLOGE("DH_ID is invalid!");
212 return;
213 }
214 capability.SetDHId(cJSON_GetObjectItem(jsonObject, DH_ID.c_str())->valuestring);
215
216 if (!IsString(jsonObject, DEV_ID)) {
217 DHLOGE("DEV_ID is invalid!");
218 return;
219 }
220 capability.SetDeviceId(cJSON_GetObjectItem(jsonObject, DEV_ID.c_str())->valuestring);
221
222 if (!IsString(jsonObject, DEV_NAME)) {
223 DHLOGE("DEV_NAME is invalid!");
224 return;
225 }
226 capability.SetDeviceName(cJSON_GetObjectItem(jsonObject, DEV_NAME.c_str())->valuestring);
227
228 if (!IsUInt16(jsonObject, DEV_TYPE)) {
229 DHLOGE("DEV_TYPE is invalid!");
230 return;
231 }
232 capability.SetDeviceType((uint16_t)cJSON_GetObjectItem(jsonObject, DEV_TYPE.c_str())->valuedouble);
233
234 if (!IsUInt32(jsonObject, DH_TYPE)) {
235 DHLOGE("DH_TYPE is invalid!");
236 return;
237 }
238 capability.SetDHType((DHType)cJSON_GetObjectItem(jsonObject, DH_TYPE.c_str())->valuedouble);
239
240 if (!IsString(jsonObject, DH_ATTRS)) {
241 DHLOGE("DH_ATTRS is invalid!");
242 return;
243 }
244 capability.SetDHAttrs(cJSON_GetObjectItem(jsonObject, DH_ATTRS.c_str())->valuestring);
245
246 if (!IsString(jsonObject, DH_SUBTYPE)) {
247 DHLOGE("DH_SUBTYPE is invalid!");
248 return;
249 }
250 capability.SetDHSubtype(cJSON_GetObjectItem(jsonObject, DH_SUBTYPE.c_str())->valuestring);
251 }
252 } // namespace DistributedHardware
253 } // namespace OHOS
254