• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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 #include "version_info.h"
28 
29 namespace OHOS {
30 namespace DistributedHardware {
31 #undef DH_LOG_TAG
32 #define DH_LOG_TAG "MetaCapabilityInfo"
33 
GetUdidHash() const34 std::string MetaCapabilityInfo::GetUdidHash() const
35 {
36     return udidHash_;
37 }
38 
SetUdidHash(const std::string & udidHash)39 void MetaCapabilityInfo::SetUdidHash(const std::string &udidHash)
40 {
41     this->udidHash_ = udidHash;
42 }
43 
GetSinkVersion() const44 std::string MetaCapabilityInfo::GetSinkVersion() const
45 {
46     return compVersion_.sinkVersion;
47 }
48 
SetSinkVersion(const std::string & sinkVersion)49 void MetaCapabilityInfo::SetSinkVersion(const std::string &sinkVersion)
50 {
51     this->compVersion_.sinkVersion = sinkVersion;
52 }
53 
GetCompVersion()54 CompVersion& MetaCapabilityInfo::GetCompVersion()
55 {
56     return compVersion_;
57 }
58 
GetCompVersion() const59 CompVersion MetaCapabilityInfo::GetCompVersion() const
60 {
61     return compVersion_;
62 }
63 
SetCompVersion(const CompVersion & compVersion)64 void MetaCapabilityInfo::SetCompVersion(const CompVersion &compVersion)
65 {
66     this->compVersion_ = compVersion;
67 }
68 
FromJsonString(const std::string & jsonStr)69 int32_t MetaCapabilityInfo::FromJsonString(const std::string &jsonStr)
70 {
71     if (!IsJsonLengthValid(jsonStr)) {
72         return ERR_DH_FWK_PARA_INVALID;
73     }
74     cJSON *jsonObj = cJSON_Parse(jsonStr.c_str());
75     if (jsonObj == NULL) {
76         DHLOGE("jsonStr parse failed");
77         return ERR_DH_FWK_JSON_PARSE_FAILED;
78     }
79 
80     FromJson(jsonObj, *this);
81     cJSON_Delete(jsonObj);
82     return DH_FWK_SUCCESS;
83 }
84 
ToJsonString()85 std::string MetaCapabilityInfo::ToJsonString()
86 {
87     cJSON *jsonObj = cJSON_CreateObject();
88     if (jsonObj == NULL) {
89         DHLOGE("Failed to create cJSON object.");
90         return "";
91     }
92     ToJson(jsonObj, *this);
93     char *cjson = cJSON_PrintUnformatted(jsonObj);
94     if (cjson == nullptr) {
95         cJSON_Delete(jsonObj);
96         return "";
97     }
98     std::string jsonString(cjson);
99     cJSON_free(cjson);
100     cJSON_Delete(jsonObj);
101     return jsonString;
102 }
103 
Compare(const MetaCapabilityInfo & metaCapInfo)104 bool MetaCapabilityInfo::Compare(const MetaCapabilityInfo& metaCapInfo)
105 {
106     if (strcmp(this->GetDeviceId().c_str(), metaCapInfo.GetDeviceId().c_str()) != 0) {
107         DHLOGE("deviceId is not equal");
108         return false;
109     }
110     if (strcmp(this->GetDHId().c_str(), metaCapInfo.GetDHId().c_str()) != 0) {
111         DHLOGE("dhId is not equal");
112         return false;
113     }
114     if (strcmp(this->GetDeviceName().c_str(), metaCapInfo.GetDeviceName().c_str()) != 0) {
115         DHLOGE("deviceName is not equal");
116         return false;
117     }
118     if (this->GetDeviceType() != metaCapInfo.GetDeviceType()) {
119         DHLOGE("deviceType is not equal");
120         return false;
121     }
122     if (this->GetDHType() != metaCapInfo.GetDHType()) {
123         DHLOGE("dhType is not equal");
124         return false;
125     }
126     if (strcmp(this->GetDHAttrs().c_str(), metaCapInfo.GetDHAttrs().c_str()) != 0) {
127         DHLOGE("dhAttrs is not equal");
128         return false;
129     }
130     if (strcmp(this->GetDHSubtype().c_str(), metaCapInfo.GetDHSubtype().c_str()) != 0) {
131         DHLOGE("dhSubtype is not equal");
132         return false;
133     }
134     if (strcmp(this->GetUdidHash().c_str(), metaCapInfo.GetUdidHash().c_str()) != 0) {
135         DHLOGE("udidHash is not equal");
136         return false;
137     }
138     if (strcmp(this->GetSinkVersion().c_str(), metaCapInfo.GetSinkVersion().c_str()) != 0) {
139         DHLOGE("sinkVersion is not equal");
140         return false;
141     }
142     return true;
143 }
144 
GetKey() const145 std::string MetaCapabilityInfo::GetKey() const
146 {
147     std::string kvStoreKey;
148     kvStoreKey.append(udidHash_);
149     kvStoreKey.append(RESOURCE_SEPARATOR);
150     kvStoreKey.append(this->GetDHId());
151     return kvStoreKey;
152 }
153 
GetAnonymousKey() const154 std::string MetaCapabilityInfo::GetAnonymousKey() const
155 {
156     std::string kvStoreKey;
157     kvStoreKey.append(GetAnonyString(udidHash_));
158     kvStoreKey.append(RESOURCE_SEPARATOR);
159     kvStoreKey.append(GetAnonyString(this->GetDHId()));
160     return kvStoreKey;
161 }
162 
ToJson(cJSON * jsonObject,const MetaCapabilityInfo & metaCapInfo)163 void ToJson(cJSON *jsonObject, const MetaCapabilityInfo &metaCapInfo)
164 {
165     if (jsonObject == nullptr) {
166         DHLOGE("Json pointer is nullptr!");
167         return;
168     }
169     cJSON_AddStringToObject(jsonObject, DH_ID.c_str(), metaCapInfo.GetDHId().c_str());
170     cJSON_AddStringToObject(jsonObject, DEV_ID.c_str(), metaCapInfo.GetDeviceId().c_str());
171     cJSON_AddStringToObject(jsonObject, DEV_NAME.c_str(), metaCapInfo.GetDeviceName().c_str());
172     cJSON_AddNumberToObject(jsonObject, DEV_TYPE.c_str(), (double)metaCapInfo.GetDeviceType());
173     cJSON_AddNumberToObject(jsonObject, DH_TYPE.c_str(), (double)metaCapInfo.GetDHType());
174     cJSON_AddStringToObject(jsonObject, DH_ATTRS.c_str(), metaCapInfo.GetDHAttrs().c_str());
175     cJSON_AddStringToObject(jsonObject, DH_SUBTYPE.c_str(), metaCapInfo.GetDHSubtype().c_str());
176     cJSON_AddStringToObject(jsonObject, DEV_UDID_HASH.c_str(), metaCapInfo.GetUdidHash().c_str());
177     cJSON_AddStringToObject(jsonObject, SINK_VER.c_str(), metaCapInfo.GetSinkVersion().c_str());
178     cJSON *jsonObjCompVersion = cJSON_CreateObject();
179     if (jsonObjCompVersion == NULL) {
180         DHLOGE("Failed to create cJSON object.");
181         return;
182     }
183     ToJson(jsonObjCompVersion, metaCapInfo.GetCompVersion());
184     cJSON_AddItemToObject(jsonObject, COMP_VER.c_str(), jsonObjCompVersion);
185 }
186 
FromJson(const cJSON * jsonObject,MetaCapabilityInfo & metaCapInfo)187 void FromJson(const cJSON *jsonObject, MetaCapabilityInfo &metaCapInfo)
188 {
189     cJSON *dhIdJson = cJSON_GetObjectItem(jsonObject, DH_ID.c_str());
190     if (!IsString(dhIdJson)) {
191         DHLOGE("DH_ID is invalid!");
192         return;
193     }
194     metaCapInfo.SetDHId(dhIdJson->valuestring);
195 
196     cJSON *devIdJson = cJSON_GetObjectItem(jsonObject, DEV_ID.c_str());
197     if (!IsString(devIdJson)) {
198         DHLOGE("DEV_ID is invalid!");
199         return;
200     }
201     metaCapInfo.SetDeviceId(devIdJson->valuestring);
202 
203     cJSON *devNameJson = cJSON_GetObjectItem(jsonObject, DEV_NAME.c_str());
204     if (!IsString(devNameJson)) {
205         DHLOGE("DEV_NAME is invalid!");
206         return;
207     }
208     metaCapInfo.SetDeviceName(devNameJson->valuestring);
209 
210     cJSON *devTypeJson = cJSON_GetObjectItem(jsonObject, DEV_TYPE.c_str());
211     if (!IsUInt16(devTypeJson)) {
212         DHLOGE("DEV_TYPE is invalid!");
213         return;
214     }
215     metaCapInfo.SetDeviceType(static_cast<uint16_t>(devTypeJson->valueint));
216 
217     cJSON *dhTypeJson = cJSON_GetObjectItem(jsonObject, DH_TYPE.c_str());
218     if (!IsUInt32(dhTypeJson)) {
219         DHLOGE("DH_TYPE is invalid!");
220         return;
221     }
222     metaCapInfo.SetDHType((DHType)dhTypeJson->valueint);
223 
224     cJSON *dhAttrsObj = cJSON_GetObjectItem(jsonObject, DH_ATTRS.c_str());
225     if (!IsString(dhAttrsObj)) {
226         DHLOGE("DH_ATTRS is invalid!");
227         return;
228     }
229     metaCapInfo.SetDHAttrs(dhAttrsObj->valuestring);
230 
231     FromJsonContinue(jsonObject, metaCapInfo);
232 }
233 
FromJsonContinue(const cJSON * jsonObject,MetaCapabilityInfo & metaCapInfo)234 void FromJsonContinue(const cJSON *jsonObject, MetaCapabilityInfo &metaCapInfo)
235 {
236     cJSON *dhSubtypeJson = cJSON_GetObjectItem(jsonObject, DH_SUBTYPE.c_str());
237     if (!IsString(dhSubtypeJson)) {
238         DHLOGE("DH_SUBTYPE is invalid!");
239         return;
240     }
241     metaCapInfo.SetDHSubtype(dhSubtypeJson->valuestring);
242 
243     cJSON *udidHashJson = cJSON_GetObjectItem(jsonObject, DEV_UDID_HASH.c_str());
244     if (!IsString(udidHashJson)) {
245         DHLOGE("DEV_UDID_HASH is invalid!");
246         return;
247     }
248     metaCapInfo.SetUdidHash(udidHashJson->valuestring);
249 
250     cJSON *sinkVerJson = cJSON_GetObjectItem(jsonObject, SINK_VER.c_str());
251     if (!IsString(sinkVerJson)) {
252         DHLOGE("SINK_VER is invalid!");
253         return;
254     }
255     metaCapInfo.SetSinkVersion(sinkVerJson->valuestring);
256 
257     cJSON *compVersionJson = cJSON_GetObjectItem(jsonObject, COMP_VER.c_str());
258     if (compVersionJson == nullptr) {
259         DHLOGE("CompVersion is invalid!");
260         return;
261     }
262     FromJson(compVersionJson, metaCapInfo.GetCompVersion());
263 }
264 } // namespace DistributedHardware
265 } // namespace OHOS
266