1 /*
2 * Copyright (c) 2023-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 "device_profile.h"
17 #include "cJSON.h"
18 #include "distributed_device_profile_constants.h"
19 #include "macro_utils.h"
20 #include "profile_utils.h"
21
22 namespace OHOS {
23 namespace DistributedDeviceProfile {
24 namespace {
25 const std::string TAG = "DeviceProfile";
26 }
GetDeviceId() const27 std::string DeviceProfile::GetDeviceId() const
28 {
29 return deviceId_;
30 }
31
SetDeviceId(const std::string & deviceId)32 void DeviceProfile::SetDeviceId(const std::string& deviceId)
33 {
34 deviceId_ = deviceId;
35 }
36
GetDeviceTypeName() const37 std::string DeviceProfile::GetDeviceTypeName() const
38 {
39 return deviceTypeName_;
40 }
41
SetDeviceTypeName(const std::string & deviceTypeName)42 void DeviceProfile::SetDeviceTypeName(const std::string &deviceTypeName)
43 {
44 deviceTypeName_ = deviceTypeName;
45 }
46
GetDeviceTypeId() const47 int32_t DeviceProfile::GetDeviceTypeId() const
48 {
49 return deviceTypeId_;
50 }
51
SetDeviceTypeId(int32_t deviceTypeId)52 void DeviceProfile::SetDeviceTypeId(int32_t deviceTypeId)
53 {
54 deviceTypeId_ = deviceTypeId;
55 }
56
GetDeviceName() const57 std::string DeviceProfile::GetDeviceName() const
58 {
59 return deviceName_;
60 }
61
SetDeviceName(const std::string & deviceName)62 void DeviceProfile::SetDeviceName(const std::string& deviceName)
63 {
64 deviceName_ = deviceName;
65 }
66
GetManufactureName() const67 std::string DeviceProfile::GetManufactureName() const
68 {
69 return manufactureName_;
70 }
71
SetManufactureName(std::string manufactureName)72 void DeviceProfile::SetManufactureName(std::string manufactureName)
73 {
74 manufactureName_ = manufactureName;
75 }
76
GetDeviceModel() const77 std::string DeviceProfile::GetDeviceModel() const
78 {
79 return deviceModel_;
80 }
81
SetDeviceModel(const std::string & deviceModel)82 void DeviceProfile::SetDeviceModel(const std::string &deviceModel)
83 {
84 deviceModel_ = deviceModel;
85 }
86
GetStorageCapability() const87 int64_t DeviceProfile::GetStorageCapability() const
88 {
89 return storageCapability_;
90 }
91
SetStorageCapability(int64_t storageCapability)92 void DeviceProfile::SetStorageCapability(int64_t storageCapability)
93 {
94 storageCapability_ = storageCapability;
95 }
96
GetOsApiLevel() const97 int32_t DeviceProfile::GetOsApiLevel() const
98 {
99 return osApiLevel_;
100 }
101
SetOsApiLevel(int32_t osApiLevel)102 void DeviceProfile::SetOsApiLevel(int32_t osApiLevel)
103 {
104 osApiLevel_ = osApiLevel;
105 }
106
GetOsVersion() const107 std::string DeviceProfile::GetOsVersion() const
108 {
109 return osVersion_;
110 }
111
SetOsVersion(const std::string & osVersion)112 void DeviceProfile::SetOsVersion(const std::string& osVersion)
113 {
114 osVersion_ = osVersion;
115 }
116
GetOsType() const117 int32_t DeviceProfile::GetOsType() const
118 {
119 return osType_;
120 }
121
SetOsType(int32_t osType)122 void DeviceProfile::SetOsType(int32_t osType)
123 {
124 osType_ = osType;
125 }
126
GetOsSysCap() const127 std::string DeviceProfile::GetOsSysCap() const
128 {
129 return osSysCap_;
130 }
131
SetOsSysCap(const std::string & osSysCap)132 void DeviceProfile::SetOsSysCap(const std::string& osSysCap)
133 {
134 osSysCap_ = osSysCap;
135 }
136
Marshalling(MessageParcel & parcel) const137 bool DeviceProfile::Marshalling(MessageParcel& parcel) const
138 {
139 WRITE_HELPER_RET(parcel, String, deviceId_, false);
140 WRITE_HELPER_RET(parcel, String, deviceTypeName_, false);
141 WRITE_HELPER_RET(parcel, Int32, deviceTypeId_, false);
142 WRITE_HELPER_RET(parcel, String, deviceName_, false);
143 WRITE_HELPER_RET(parcel, String, manufactureName_, false);
144 WRITE_HELPER_RET(parcel, String, deviceModel_, false);
145 WRITE_HELPER_RET(parcel, Int64, storageCapability_, false);
146 WRITE_HELPER_RET(parcel, String, osSysCap_, false);
147 WRITE_HELPER_RET(parcel, Int32, osApiLevel_, false);
148 WRITE_HELPER_RET(parcel, String, osVersion_, false);
149 WRITE_HELPER_RET(parcel, Int32, osType_, false);
150 return true;
151 }
152
UnMarshalling(MessageParcel & parcel)153 bool DeviceProfile::UnMarshalling(MessageParcel& parcel)
154 {
155 READ_HELPER_RET(parcel, String, deviceId_, false);
156 READ_HELPER_RET(parcel, String, deviceTypeName_, false);
157 READ_HELPER_RET(parcel, Int32, deviceTypeId_, false);
158 READ_HELPER_RET(parcel, String, deviceName_, false);
159 READ_HELPER_RET(parcel, String, manufactureName_, false);
160 READ_HELPER_RET(parcel, String, deviceModel_, false);
161 READ_HELPER_RET(parcel, Int64, storageCapability_, false);
162 READ_HELPER_RET(parcel, String, osSysCap_, false);
163 READ_HELPER_RET(parcel, Int32, osApiLevel_, false);
164 READ_HELPER_RET(parcel, String, osVersion_, false);
165 READ_HELPER_RET(parcel, Int32, osType_, false);
166 return true;
167 }
168
operator !=(const DeviceProfile & deviceProfile) const169 bool DeviceProfile::operator!=(const DeviceProfile& deviceProfile) const
170 {
171 bool isNotEqual = (deviceId_ != deviceProfile.GetDeviceId() || deviceTypeName_ != deviceProfile.GetDeviceTypeName()
172 || deviceTypeId_ != deviceProfile.GetDeviceTypeId() || deviceName_ != deviceProfile.GetDeviceName() ||
173 manufactureName_ != deviceProfile.GetManufactureName() || deviceModel_ != deviceProfile.GetDeviceModel() ||
174 storageCapability_ != deviceProfile.GetStorageCapability() || osSysCap_ != deviceProfile.GetOsSysCap() ||
175 osApiLevel_ != deviceProfile.GetOsApiLevel() || osVersion_ != deviceProfile.GetOsVersion() || osType_ !=
176 deviceProfile.GetOsType());
177 if (isNotEqual) {
178 return true;
179 } else {
180 return false;
181 }
182 }
183
dump() const184 std::string DeviceProfile::dump() const
185 {
186 cJSON* json = cJSON_CreateObject();
187 if (!cJSON_IsObject(json)) {
188 cJSON_Delete(json);
189 return EMPTY_STRING;
190 }
191 cJSON_AddStringToObject(json, DEVICE_ID.c_str(), ProfileUtils::GetAnonyString(deviceId_).c_str());
192 cJSON_AddStringToObject(json, DEVICE_TYPE_NAME.c_str(), deviceTypeName_.c_str());
193 cJSON_AddNumberToObject(json, DEVICE_TYPE_ID.c_str(), deviceTypeId_);
194 cJSON_AddStringToObject(json, DEVICE_NAME.c_str(), deviceName_.c_str());
195 cJSON_AddStringToObject(json, MANUFACTURE_NAME.c_str(), manufactureName_.c_str());
196 cJSON_AddStringToObject(json, DEVICE_MODEL.c_str(), deviceModel_.c_str());
197 cJSON_AddNumberToObject(json, STORAGE_CAPACITY.c_str(), storageCapability_);
198 cJSON_AddStringToObject(json, OS_SYS_CAPACITY.c_str(), osSysCap_.c_str());
199 cJSON_AddNumberToObject(json, OS_API_LEVEL.c_str(), osApiLevel_);
200 cJSON_AddStringToObject(json, OS_VERSION.c_str(), osVersion_.c_str());
201 cJSON_AddNumberToObject(json, OS_TYPE.c_str(), osType_);
202 char* jsonChars = cJSON_PrintUnformatted(json);
203 if (jsonChars == NULL) {
204 cJSON_Delete(json);
205 HILOGE("cJSON formatted to string failed!");
206 return EMPTY_STRING;
207 }
208 std::string jsonStr = jsonChars;
209 cJSON_Delete(json);
210 cJSON_free(jsonChars);
211 return jsonStr;
212 }
213
AnnoymizeDump() const214 std::string DeviceProfile::AnnoymizeDump() const
215 {
216 cJSON* json = cJSON_CreateObject();
217 if (!cJSON_IsObject(json)) {
218 cJSON_Delete(json);
219 return EMPTY_STRING;
220 }
221 cJSON_AddStringToObject(json, DEVICE_ID.c_str(), ProfileUtils::GetAnonyString(deviceId_).c_str());
222 cJSON_AddStringToObject(json, DEVICE_TYPE_NAME.c_str(), deviceTypeName_.c_str());
223 cJSON_AddNumberToObject(json, DEVICE_TYPE_ID.c_str(), deviceTypeId_);
224 cJSON_AddStringToObject(json, DEVICE_NAME.c_str(), deviceName_.c_str());
225 cJSON_AddStringToObject(json, MANUFACTURE_NAME.c_str(), manufactureName_.c_str());
226 cJSON_AddStringToObject(json, DEVICE_MODEL.c_str(), deviceModel_.c_str());
227 cJSON_AddStringToObject(json, STORAGE_CAPACITY.c_str(),
228 ProfileUtils::GetAnonyString(std::to_string(storageCapability_)).c_str());
229 cJSON_AddStringToObject(json, OS_SYS_CAPACITY.c_str(),
230 ProfileUtils::GetAnonyString(osSysCap_).c_str());
231 cJSON_AddStringToObject(json, OS_API_LEVEL.c_str(),
232 ProfileUtils::GetAnonyString(std::to_string(osApiLevel_)).c_str());
233 cJSON_AddStringToObject(json, OS_VERSION.c_str(),
234 ProfileUtils::GetAnonyString(osVersion_).c_str());
235 cJSON_AddStringToObject(json, OS_TYPE.c_str(),
236 ProfileUtils::GetAnonyString(std::to_string(osType_)).c_str());
237 char* jsonChars = cJSON_PrintUnformatted(json);
238 if (jsonChars == NULL) {
239 cJSON_Delete(json);
240 HILOGE("cJSON formatted to string failed!");
241 return EMPTY_STRING;
242 }
243 std::string jsonStr = jsonChars;
244 cJSON_Delete(json);
245 cJSON_free(jsonChars);
246 return jsonStr;
247 }
248 } // namespace DistributedDeviceProfile
249 } // namespace OHOS