1 /*
2  * Copyright (c) 2024 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 "meta_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 "MetaCapabilityInfo"
32 
GetUdidHash() const33 std::string MetaCapabilityInfo::GetUdidHash() const
34 {
35     return udidHash_;
36 }
37 
SetUdidHash(const std::string & udidHash)38 void MetaCapabilityInfo::SetUdidHash(const std::string &udidHash)
39 {
40     this->udidHash_ = udidHash;
41 }
42 
GetSinkVersion() const43 std::string MetaCapabilityInfo::GetSinkVersion() const
44 {
45     return sinkVersion_;
46 }
47 
SetSinkVersion(const std::string & sinkVersion)48 void MetaCapabilityInfo::SetSinkVersion(const std::string &sinkVersion)
49 {
50     this->sinkVersion_ = sinkVersion;
51 }
52 
FromJsonString(const std::string & jsonStr)53 int32_t MetaCapabilityInfo::FromJsonString(const std::string &jsonStr)
54 {
55     if (!IsJsonLengthValid(jsonStr)) {
56         return ERR_DH_FWK_PARA_INVALID;
57     }
58     cJSON *jsonObj = cJSON_Parse(jsonStr.c_str());
59     if (jsonObj == NULL) {
60         DHLOGE("jsonStr parse failed");
61         return ERR_DH_FWK_JSON_PARSE_FAILED;
62     }
63 
64     FromJson(jsonObj, *this);
65     cJSON_Delete(jsonObj);
66     return DH_FWK_SUCCESS;
67 }
68 
ToJsonString()69 std::string MetaCapabilityInfo::ToJsonString()
70 {
71     cJSON *jsonObj = cJSON_CreateObject();
72     if (jsonObj == NULL) {
73         DHLOGE("Failed to create cJSON object.");
74         return "";
75     }
76     ToJson(jsonObj, *this);
77     char *cjson = cJSON_PrintUnformatted(jsonObj);
78     if (cjson == nullptr) {
79         cJSON_Delete(jsonObj);
80         return "";
81     }
82     std::string jsonString(cjson);
83     cJSON_free(cjson);
84     cJSON_Delete(jsonObj);
85     return jsonString;
86 }
87 
Compare(const MetaCapabilityInfo & metaCapInfo)88 bool MetaCapabilityInfo::Compare(const MetaCapabilityInfo& metaCapInfo)
89 {
90     if (strcmp(this->GetDeviceId().c_str(), metaCapInfo.GetDeviceId().c_str()) != 0) {
91         DHLOGE("deviceId is not equal");
92         return false;
93     }
94     if (strcmp(this->GetDHId().c_str(), metaCapInfo.GetDHId().c_str()) != 0) {
95         DHLOGE("dhId is not equal");
96         return false;
97     }
98     if (strcmp(this->GetDeviceName().c_str(), metaCapInfo.GetDeviceName().c_str()) != 0) {
99         DHLOGE("deviceName is not equal");
100         return false;
101     }
102     if (this->GetDeviceType() != metaCapInfo.GetDeviceType()) {
103         DHLOGE("deviceType is not equal");
104         return false;
105     }
106     if (this->GetDHType() != metaCapInfo.GetDHType()) {
107         DHLOGE("dhType is not equal");
108         return false;
109     }
110     if (strcmp(this->GetDHAttrs().c_str(), metaCapInfo.GetDHAttrs().c_str()) != 0) {
111         DHLOGE("dhAttrs is not equal");
112         return false;
113     }
114     if (strcmp(this->GetDHSubtype().c_str(), metaCapInfo.GetDHSubtype().c_str()) != 0) {
115         DHLOGE("dhSubtype is not equal");
116         return false;
117     }
118     if (strcmp(this->GetUdidHash().c_str(), metaCapInfo.GetUdidHash().c_str()) != 0) {
119         DHLOGE("udidHash is not equal");
120         return false;
121     }
122     if (strcmp(this->GetSinkVersion().c_str(), metaCapInfo.GetSinkVersion().c_str()) != 0) {
123         DHLOGE("sinkVersion is not equal");
124         return false;
125     }
126     return true;
127 }
128 
GetKey() const129 std::string MetaCapabilityInfo::GetKey() const
130 {
131     std::string kvStoreKey;
132     kvStoreKey.append(udidHash_);
133     kvStoreKey.append(RESOURCE_SEPARATOR);
134     kvStoreKey.append(this->GetDHId());
135     return kvStoreKey;
136 }
137 
GetAnonymousKey() const138 std::string MetaCapabilityInfo::GetAnonymousKey() const
139 {
140     std::string kvStoreKey;
141     kvStoreKey.append(GetAnonyString(udidHash_));
142     kvStoreKey.append(RESOURCE_SEPARATOR);
143     kvStoreKey.append(GetAnonyString(this->GetDHId()));
144     return kvStoreKey;
145 }
146 
ToJson(cJSON * jsonObject,const MetaCapabilityInfo & metaCapInfo)147 void ToJson(cJSON *jsonObject, const MetaCapabilityInfo &metaCapInfo)
148 {
149     if (jsonObject == nullptr) {
150         DHLOGE("Json pointer is nullptr!");
151         return;
152     }
153     cJSON_AddStringToObject(jsonObject, DH_ID.c_str(), metaCapInfo.GetDHId().c_str());
154     cJSON_AddStringToObject(jsonObject, DEV_ID.c_str(), metaCapInfo.GetDeviceId().c_str());
155     cJSON_AddStringToObject(jsonObject, DEV_NAME.c_str(), metaCapInfo.GetDeviceName().c_str());
156     cJSON_AddNumberToObject(jsonObject, DEV_TYPE.c_str(), (double)metaCapInfo.GetDeviceType());
157     cJSON_AddNumberToObject(jsonObject, DH_TYPE.c_str(), (double)metaCapInfo.GetDHType());
158     cJSON_AddStringToObject(jsonObject, DH_ATTRS.c_str(), metaCapInfo.GetDHAttrs().c_str());
159     cJSON_AddStringToObject(jsonObject, DH_SUBTYPE.c_str(), metaCapInfo.GetDHSubtype().c_str());
160     cJSON_AddStringToObject(jsonObject, DEV_UDID_HASH.c_str(), metaCapInfo.GetUdidHash().c_str());
161     cJSON_AddStringToObject(jsonObject, SINK_VER.c_str(), metaCapInfo.GetSinkVersion().c_str());
162 }
163 
FromJson(const cJSON * jsonObject,MetaCapabilityInfo & metaCapInfo)164 void FromJson(const cJSON *jsonObject, MetaCapabilityInfo &metaCapInfo)
165 {
166     if (!IsString(jsonObject, DH_ID)) {
167         DHLOGE("DH_ID is invalid!");
168         return;
169     }
170     metaCapInfo.SetDHId(cJSON_GetObjectItem(jsonObject, DH_ID.c_str())->valuestring);
171 
172     if (!IsString(jsonObject, DEV_ID)) {
173         DHLOGE("DEV_ID is invalid!");
174         return;
175     }
176     metaCapInfo.SetDeviceId(cJSON_GetObjectItem(jsonObject, DEV_ID.c_str())->valuestring);
177 
178     if (!IsString(jsonObject, DEV_NAME)) {
179         DHLOGE("DEV_NAME is invalid!");
180         return;
181     }
182     metaCapInfo.SetDeviceName(cJSON_GetObjectItem(jsonObject, DEV_NAME.c_str())->valuestring);
183 
184     if (!IsUInt16(jsonObject, DEV_TYPE)) {
185         DHLOGE("DEV_TYPE is invalid!");
186         return;
187     }
188     metaCapInfo.SetDeviceType((uint16_t)cJSON_GetObjectItem(jsonObject, DEV_TYPE.c_str())->valuedouble);
189 
190     if (!IsUInt32(jsonObject, DH_TYPE)) {
191         DHLOGE("DH_TYPE is invalid!");
192         return;
193     }
194     metaCapInfo.SetDHType((DHType)cJSON_GetObjectItem(jsonObject, DH_TYPE.c_str())->valuedouble);
195 
196     if (!IsString(jsonObject, DH_ATTRS)) {
197         DHLOGE("DH_ATTRS is invalid!");
198         return;
199     }
200     metaCapInfo.SetDHAttrs(cJSON_GetObjectItem(jsonObject, DH_ATTRS.c_str())->valuestring);
201 
202     if (!IsString(jsonObject, DH_SUBTYPE)) {
203         DHLOGE("DH_SUBTYPE is invalid!");
204         return;
205     }
206     metaCapInfo.SetDHSubtype(cJSON_GetObjectItem(jsonObject, DH_SUBTYPE.c_str())->valuestring);
207 
208     if (!IsString(jsonObject, DEV_UDID_HASH)) {
209         DHLOGE("DEV_UDID_HASH is invalid!");
210         return;
211     }
212     metaCapInfo.SetUdidHash(cJSON_GetObjectItem(jsonObject, DEV_UDID_HASH.c_str())->valuestring);
213 
214     if (!IsString(jsonObject, SINK_VER)) {
215         DHLOGE("SINK_VER is invalid!");
216         return;
217     }
218     metaCapInfo.SetSinkVersion(cJSON_GetObjectItem(jsonObject, SINK_VER.c_str())->valuestring);
219 }
220 } // namespace DistributedHardware
221 } // namespace OHOS
222