1f8af9c48Sopenharmony_ci/*
2f8af9c48Sopenharmony_ci * Copyright (c) 2023-2024 Huawei Device Co., Ltd.
3f8af9c48Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f8af9c48Sopenharmony_ci * you may not use this file except in compliance with the License.
5f8af9c48Sopenharmony_ci * You may obtain a copy of the License at
6f8af9c48Sopenharmony_ci *
7f8af9c48Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f8af9c48Sopenharmony_ci *
9f8af9c48Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f8af9c48Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f8af9c48Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f8af9c48Sopenharmony_ci * See the License for the specific language governing permissions and
13f8af9c48Sopenharmony_ci * limitations under the License.
14f8af9c48Sopenharmony_ci */
15f8af9c48Sopenharmony_ci
16f8af9c48Sopenharmony_ci#include "device_profile.h"
17f8af9c48Sopenharmony_ci#include "cJSON.h"
18f8af9c48Sopenharmony_ci#include "distributed_device_profile_constants.h"
19f8af9c48Sopenharmony_ci#include "macro_utils.h"
20f8af9c48Sopenharmony_ci#include "profile_utils.h"
21f8af9c48Sopenharmony_ci
22f8af9c48Sopenharmony_cinamespace OHOS {
23f8af9c48Sopenharmony_cinamespace DistributedDeviceProfile {
24f8af9c48Sopenharmony_cinamespace {
25f8af9c48Sopenharmony_ci    const std::string TAG = "DeviceProfile";
26f8af9c48Sopenharmony_ci}
27f8af9c48Sopenharmony_cistd::string DeviceProfile::GetDeviceId() const
28f8af9c48Sopenharmony_ci{
29f8af9c48Sopenharmony_ci    return deviceId_;
30f8af9c48Sopenharmony_ci}
31f8af9c48Sopenharmony_ci
32f8af9c48Sopenharmony_civoid DeviceProfile::SetDeviceId(const std::string& deviceId)
33f8af9c48Sopenharmony_ci{
34f8af9c48Sopenharmony_ci    deviceId_ = deviceId;
35f8af9c48Sopenharmony_ci}
36f8af9c48Sopenharmony_ci
37f8af9c48Sopenharmony_cistd::string DeviceProfile::GetDeviceTypeName() const
38f8af9c48Sopenharmony_ci{
39f8af9c48Sopenharmony_ci    return deviceTypeName_;
40f8af9c48Sopenharmony_ci}
41f8af9c48Sopenharmony_ci
42f8af9c48Sopenharmony_civoid DeviceProfile::SetDeviceTypeName(const std::string &deviceTypeName)
43f8af9c48Sopenharmony_ci{
44f8af9c48Sopenharmony_ci    deviceTypeName_ = deviceTypeName;
45f8af9c48Sopenharmony_ci}
46f8af9c48Sopenharmony_ci
47f8af9c48Sopenharmony_ciint32_t DeviceProfile::GetDeviceTypeId() const
48f8af9c48Sopenharmony_ci{
49f8af9c48Sopenharmony_ci    return deviceTypeId_;
50f8af9c48Sopenharmony_ci}
51f8af9c48Sopenharmony_ci
52f8af9c48Sopenharmony_civoid DeviceProfile::SetDeviceTypeId(int32_t deviceTypeId)
53f8af9c48Sopenharmony_ci{
54f8af9c48Sopenharmony_ci    deviceTypeId_ = deviceTypeId;
55f8af9c48Sopenharmony_ci}
56f8af9c48Sopenharmony_ci
57f8af9c48Sopenharmony_cistd::string DeviceProfile::GetDeviceName() const
58f8af9c48Sopenharmony_ci{
59f8af9c48Sopenharmony_ci    return deviceName_;
60f8af9c48Sopenharmony_ci}
61f8af9c48Sopenharmony_ci
62f8af9c48Sopenharmony_civoid DeviceProfile::SetDeviceName(const std::string& deviceName)
63f8af9c48Sopenharmony_ci{
64f8af9c48Sopenharmony_ci    deviceName_ = deviceName;
65f8af9c48Sopenharmony_ci}
66f8af9c48Sopenharmony_ci
67f8af9c48Sopenharmony_cistd::string DeviceProfile::GetManufactureName() const
68f8af9c48Sopenharmony_ci{
69f8af9c48Sopenharmony_ci    return manufactureName_;
70f8af9c48Sopenharmony_ci}
71f8af9c48Sopenharmony_ci
72f8af9c48Sopenharmony_civoid DeviceProfile::SetManufactureName(std::string manufactureName)
73f8af9c48Sopenharmony_ci{
74f8af9c48Sopenharmony_ci    manufactureName_ = manufactureName;
75f8af9c48Sopenharmony_ci}
76f8af9c48Sopenharmony_ci
77f8af9c48Sopenharmony_cistd::string DeviceProfile::GetDeviceModel() const
78f8af9c48Sopenharmony_ci{
79f8af9c48Sopenharmony_ci    return deviceModel_;
80f8af9c48Sopenharmony_ci}
81f8af9c48Sopenharmony_ci
82f8af9c48Sopenharmony_civoid DeviceProfile::SetDeviceModel(const std::string &deviceModel)
83f8af9c48Sopenharmony_ci{
84f8af9c48Sopenharmony_ci    deviceModel_ = deviceModel;
85f8af9c48Sopenharmony_ci}
86f8af9c48Sopenharmony_ci
87f8af9c48Sopenharmony_ciint64_t DeviceProfile::GetStorageCapability() const
88f8af9c48Sopenharmony_ci{
89f8af9c48Sopenharmony_ci    return storageCapability_;
90f8af9c48Sopenharmony_ci}
91f8af9c48Sopenharmony_ci
92f8af9c48Sopenharmony_civoid DeviceProfile::SetStorageCapability(int64_t storageCapability)
93f8af9c48Sopenharmony_ci{
94f8af9c48Sopenharmony_ci    storageCapability_ = storageCapability;
95f8af9c48Sopenharmony_ci}
96f8af9c48Sopenharmony_ci
97f8af9c48Sopenharmony_ciint32_t DeviceProfile::GetOsApiLevel() const
98f8af9c48Sopenharmony_ci{
99f8af9c48Sopenharmony_ci    return osApiLevel_;
100f8af9c48Sopenharmony_ci}
101f8af9c48Sopenharmony_ci
102f8af9c48Sopenharmony_civoid DeviceProfile::SetOsApiLevel(int32_t osApiLevel)
103f8af9c48Sopenharmony_ci{
104f8af9c48Sopenharmony_ci    osApiLevel_ = osApiLevel;
105f8af9c48Sopenharmony_ci}
106f8af9c48Sopenharmony_ci
107f8af9c48Sopenharmony_cistd::string DeviceProfile::GetOsVersion() const
108f8af9c48Sopenharmony_ci{
109f8af9c48Sopenharmony_ci    return osVersion_;
110f8af9c48Sopenharmony_ci}
111f8af9c48Sopenharmony_ci
112f8af9c48Sopenharmony_civoid DeviceProfile::SetOsVersion(const std::string& osVersion)
113f8af9c48Sopenharmony_ci{
114f8af9c48Sopenharmony_ci    osVersion_ = osVersion;
115f8af9c48Sopenharmony_ci}
116f8af9c48Sopenharmony_ci
117f8af9c48Sopenharmony_ciint32_t DeviceProfile::GetOsType() const
118f8af9c48Sopenharmony_ci{
119f8af9c48Sopenharmony_ci    return osType_;
120f8af9c48Sopenharmony_ci}
121f8af9c48Sopenharmony_ci
122f8af9c48Sopenharmony_civoid DeviceProfile::SetOsType(int32_t osType)
123f8af9c48Sopenharmony_ci{
124f8af9c48Sopenharmony_ci    osType_ = osType;
125f8af9c48Sopenharmony_ci}
126f8af9c48Sopenharmony_ci
127f8af9c48Sopenharmony_cistd::string DeviceProfile::GetOsSysCap() const
128f8af9c48Sopenharmony_ci{
129f8af9c48Sopenharmony_ci    return osSysCap_;
130f8af9c48Sopenharmony_ci}
131f8af9c48Sopenharmony_ci
132f8af9c48Sopenharmony_civoid DeviceProfile::SetOsSysCap(const std::string& osSysCap)
133f8af9c48Sopenharmony_ci{
134f8af9c48Sopenharmony_ci    osSysCap_ = osSysCap;
135f8af9c48Sopenharmony_ci}
136f8af9c48Sopenharmony_ci
137f8af9c48Sopenharmony_cibool DeviceProfile::Marshalling(MessageParcel& parcel) const
138f8af9c48Sopenharmony_ci{
139f8af9c48Sopenharmony_ci    WRITE_HELPER_RET(parcel, String, deviceId_, false);
140f8af9c48Sopenharmony_ci    WRITE_HELPER_RET(parcel, String, deviceTypeName_, false);
141f8af9c48Sopenharmony_ci    WRITE_HELPER_RET(parcel, Int32, deviceTypeId_, false);
142f8af9c48Sopenharmony_ci    WRITE_HELPER_RET(parcel, String, deviceName_, false);
143f8af9c48Sopenharmony_ci    WRITE_HELPER_RET(parcel, String, manufactureName_, false);
144f8af9c48Sopenharmony_ci    WRITE_HELPER_RET(parcel, String, deviceModel_, false);
145f8af9c48Sopenharmony_ci    WRITE_HELPER_RET(parcel, Int64, storageCapability_, false);
146f8af9c48Sopenharmony_ci    WRITE_HELPER_RET(parcel, String, osSysCap_, false);
147f8af9c48Sopenharmony_ci    WRITE_HELPER_RET(parcel, Int32, osApiLevel_, false);
148f8af9c48Sopenharmony_ci    WRITE_HELPER_RET(parcel, String, osVersion_, false);
149f8af9c48Sopenharmony_ci    WRITE_HELPER_RET(parcel, Int32, osType_, false);
150f8af9c48Sopenharmony_ci    return true;
151f8af9c48Sopenharmony_ci}
152f8af9c48Sopenharmony_ci
153f8af9c48Sopenharmony_cibool DeviceProfile::UnMarshalling(MessageParcel& parcel)
154f8af9c48Sopenharmony_ci{
155f8af9c48Sopenharmony_ci    READ_HELPER_RET(parcel, String, deviceId_, false);
156f8af9c48Sopenharmony_ci    READ_HELPER_RET(parcel, String, deviceTypeName_, false);
157f8af9c48Sopenharmony_ci    READ_HELPER_RET(parcel, Int32, deviceTypeId_, false);
158f8af9c48Sopenharmony_ci    READ_HELPER_RET(parcel, String, deviceName_, false);
159f8af9c48Sopenharmony_ci    READ_HELPER_RET(parcel, String, manufactureName_, false);
160f8af9c48Sopenharmony_ci    READ_HELPER_RET(parcel, String, deviceModel_, false);
161f8af9c48Sopenharmony_ci    READ_HELPER_RET(parcel, Int64, storageCapability_, false);
162f8af9c48Sopenharmony_ci    READ_HELPER_RET(parcel, String, osSysCap_, false);
163f8af9c48Sopenharmony_ci    READ_HELPER_RET(parcel, Int32, osApiLevel_, false);
164f8af9c48Sopenharmony_ci    READ_HELPER_RET(parcel, String, osVersion_, false);
165f8af9c48Sopenharmony_ci    READ_HELPER_RET(parcel, Int32, osType_, false);
166f8af9c48Sopenharmony_ci    return true;
167f8af9c48Sopenharmony_ci}
168f8af9c48Sopenharmony_ci
169f8af9c48Sopenharmony_cibool DeviceProfile::operator!=(const DeviceProfile& deviceProfile) const
170f8af9c48Sopenharmony_ci{
171f8af9c48Sopenharmony_ci    bool isNotEqual = (deviceId_ != deviceProfile.GetDeviceId() || deviceTypeName_ != deviceProfile.GetDeviceTypeName()
172f8af9c48Sopenharmony_ci        || deviceTypeId_ != deviceProfile.GetDeviceTypeId() || deviceName_ != deviceProfile.GetDeviceName() ||
173f8af9c48Sopenharmony_ci        manufactureName_ != deviceProfile.GetManufactureName() || deviceModel_ != deviceProfile.GetDeviceModel() ||
174f8af9c48Sopenharmony_ci        storageCapability_ != deviceProfile.GetStorageCapability() || osSysCap_ != deviceProfile.GetOsSysCap() ||
175f8af9c48Sopenharmony_ci        osApiLevel_ != deviceProfile.GetOsApiLevel() || osVersion_ != deviceProfile.GetOsVersion() || osType_ !=
176f8af9c48Sopenharmony_ci        deviceProfile.GetOsType());
177f8af9c48Sopenharmony_ci    if (isNotEqual) {
178f8af9c48Sopenharmony_ci        return true;
179f8af9c48Sopenharmony_ci    } else {
180f8af9c48Sopenharmony_ci        return false;
181f8af9c48Sopenharmony_ci    }
182f8af9c48Sopenharmony_ci}
183f8af9c48Sopenharmony_ci
184f8af9c48Sopenharmony_cistd::string DeviceProfile::dump() const
185f8af9c48Sopenharmony_ci{
186f8af9c48Sopenharmony_ci    cJSON* json = cJSON_CreateObject();
187f8af9c48Sopenharmony_ci    if (!cJSON_IsObject(json)) {
188f8af9c48Sopenharmony_ci        cJSON_Delete(json);
189f8af9c48Sopenharmony_ci        return EMPTY_STRING;
190f8af9c48Sopenharmony_ci    }
191f8af9c48Sopenharmony_ci    cJSON_AddStringToObject(json, DEVICE_ID.c_str(), ProfileUtils::GetAnonyString(deviceId_).c_str());
192f8af9c48Sopenharmony_ci    cJSON_AddStringToObject(json, DEVICE_TYPE_NAME.c_str(), deviceTypeName_.c_str());
193f8af9c48Sopenharmony_ci    cJSON_AddNumberToObject(json, DEVICE_TYPE_ID.c_str(), deviceTypeId_);
194f8af9c48Sopenharmony_ci    cJSON_AddStringToObject(json, DEVICE_NAME.c_str(), deviceName_.c_str());
195f8af9c48Sopenharmony_ci    cJSON_AddStringToObject(json, MANUFACTURE_NAME.c_str(), manufactureName_.c_str());
196f8af9c48Sopenharmony_ci    cJSON_AddStringToObject(json, DEVICE_MODEL.c_str(), deviceModel_.c_str());
197f8af9c48Sopenharmony_ci    cJSON_AddNumberToObject(json, STORAGE_CAPACITY.c_str(), storageCapability_);
198f8af9c48Sopenharmony_ci    cJSON_AddStringToObject(json, OS_SYS_CAPACITY.c_str(), osSysCap_.c_str());
199f8af9c48Sopenharmony_ci    cJSON_AddNumberToObject(json, OS_API_LEVEL.c_str(), osApiLevel_);
200f8af9c48Sopenharmony_ci    cJSON_AddStringToObject(json, OS_VERSION.c_str(), osVersion_.c_str());
201f8af9c48Sopenharmony_ci    cJSON_AddNumberToObject(json, OS_TYPE.c_str(), osType_);
202f8af9c48Sopenharmony_ci    char* jsonChars = cJSON_PrintUnformatted(json);
203f8af9c48Sopenharmony_ci    if (jsonChars == NULL) {
204f8af9c48Sopenharmony_ci        cJSON_Delete(json);
205f8af9c48Sopenharmony_ci        HILOGE("cJSON formatted to string failed!");
206f8af9c48Sopenharmony_ci        return EMPTY_STRING;
207f8af9c48Sopenharmony_ci    }
208f8af9c48Sopenharmony_ci    std::string jsonStr = jsonChars;
209f8af9c48Sopenharmony_ci    cJSON_Delete(json);
210f8af9c48Sopenharmony_ci    cJSON_free(jsonChars);
211f8af9c48Sopenharmony_ci    return jsonStr;
212f8af9c48Sopenharmony_ci}
213f8af9c48Sopenharmony_ci
214f8af9c48Sopenharmony_cistd::string DeviceProfile::AnnoymizeDump() const
215f8af9c48Sopenharmony_ci{
216f8af9c48Sopenharmony_ci    cJSON* json = cJSON_CreateObject();
217f8af9c48Sopenharmony_ci    if (!cJSON_IsObject(json)) {
218f8af9c48Sopenharmony_ci        cJSON_Delete(json);
219f8af9c48Sopenharmony_ci        return EMPTY_STRING;
220f8af9c48Sopenharmony_ci    }
221f8af9c48Sopenharmony_ci    cJSON_AddStringToObject(json, DEVICE_ID.c_str(), ProfileUtils::GetAnonyString(deviceId_).c_str());
222f8af9c48Sopenharmony_ci    cJSON_AddStringToObject(json, DEVICE_TYPE_NAME.c_str(), deviceTypeName_.c_str());
223f8af9c48Sopenharmony_ci    cJSON_AddNumberToObject(json, DEVICE_TYPE_ID.c_str(), deviceTypeId_);
224f8af9c48Sopenharmony_ci    cJSON_AddStringToObject(json, DEVICE_NAME.c_str(), deviceName_.c_str());
225f8af9c48Sopenharmony_ci    cJSON_AddStringToObject(json, MANUFACTURE_NAME.c_str(), manufactureName_.c_str());
226f8af9c48Sopenharmony_ci    cJSON_AddStringToObject(json, DEVICE_MODEL.c_str(), deviceModel_.c_str());
227f8af9c48Sopenharmony_ci    cJSON_AddStringToObject(json, STORAGE_CAPACITY.c_str(),
228f8af9c48Sopenharmony_ci        ProfileUtils::GetAnonyString(std::to_string(storageCapability_)).c_str());
229f8af9c48Sopenharmony_ci    cJSON_AddStringToObject(json, OS_SYS_CAPACITY.c_str(),
230f8af9c48Sopenharmony_ci        ProfileUtils::GetAnonyString(osSysCap_).c_str());
231f8af9c48Sopenharmony_ci    cJSON_AddStringToObject(json, OS_API_LEVEL.c_str(),
232f8af9c48Sopenharmony_ci        ProfileUtils::GetAnonyString(std::to_string(osApiLevel_)).c_str());
233f8af9c48Sopenharmony_ci    cJSON_AddStringToObject(json, OS_VERSION.c_str(),
234f8af9c48Sopenharmony_ci        ProfileUtils::GetAnonyString(osVersion_).c_str());
235f8af9c48Sopenharmony_ci    cJSON_AddStringToObject(json, OS_TYPE.c_str(),
236f8af9c48Sopenharmony_ci        ProfileUtils::GetAnonyString(std::to_string(osType_)).c_str());
237f8af9c48Sopenharmony_ci    char* jsonChars = cJSON_PrintUnformatted(json);
238f8af9c48Sopenharmony_ci    if (jsonChars == NULL) {
239f8af9c48Sopenharmony_ci        cJSON_Delete(json);
240f8af9c48Sopenharmony_ci        HILOGE("cJSON formatted to string failed!");
241f8af9c48Sopenharmony_ci        return EMPTY_STRING;
242f8af9c48Sopenharmony_ci    }
243f8af9c48Sopenharmony_ci    std::string jsonStr = jsonChars;
244f8af9c48Sopenharmony_ci    cJSON_Delete(json);
245f8af9c48Sopenharmony_ci    cJSON_free(jsonChars);
246f8af9c48Sopenharmony_ci    return jsonStr;
247f8af9c48Sopenharmony_ci}
248f8af9c48Sopenharmony_ci} // namespace DistributedDeviceProfile
249f8af9c48Sopenharmony_ci} // namespace OHOS