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 "trusted_device_info.h"
17
18 #include "cJSON.h"
19
20 #include "macro_utils.h"
21 #include "profile_utils.h"
22
23 namespace OHOS {
24 namespace DistributedDeviceProfile {
25 namespace {
26 const std::string TAG = "TrustedDeviceInfo";
27 const std::string NETWORK_ID = "networkId";
28 const std::string AUTH_FORM = "authForm";
29 const std::string UDID = "udid";
30 const std::string UUID = "uuid";
31 }
32
GetNetworkId() const33 std::string TrustedDeviceInfo::GetNetworkId() const
34 {
35 return networkId_;
36 }
37
SetNetworkId(const std::string & networkId)38 void TrustedDeviceInfo::SetNetworkId(const std::string& networkId)
39 {
40 networkId_ = networkId;
41 }
42
GetAuthForm() const43 int32_t TrustedDeviceInfo::GetAuthForm() const
44 {
45 return authForm_;
46 }
47
SetAuthForm(int32_t authForm)48 void TrustedDeviceInfo::SetAuthForm(int32_t authForm)
49 {
50 authForm_ = authForm;
51 }
52
GetDeviceTypeId() const53 uint16_t TrustedDeviceInfo::GetDeviceTypeId() const
54 {
55 return deviceTypeId_;
56 }
57
SetDeviceTypeId(uint16_t deviceTypeId)58 void TrustedDeviceInfo::SetDeviceTypeId(uint16_t deviceTypeId)
59 {
60 deviceTypeId_ = deviceTypeId;
61 }
62
GetOsVersion() const63 std::string TrustedDeviceInfo::GetOsVersion() const
64 {
65 return osVersion_;
66 }
67
SetOsVersion(const std::string & osVersion)68 void TrustedDeviceInfo::SetOsVersion(const std::string& osVersion)
69 {
70 osVersion_ = osVersion;
71 }
72
GetOsType() const73 int32_t TrustedDeviceInfo::GetOsType() const
74 {
75 return osType_;
76 }
77
SetOsType(int32_t osType)78 void TrustedDeviceInfo::SetOsType(int32_t osType)
79 {
80 osType_ = osType;
81 }
82
GetUdid() const83 std::string TrustedDeviceInfo::GetUdid() const
84 {
85 return udid_;
86 }
87
SetUdid(const std::string & udid)88 void TrustedDeviceInfo::SetUdid(const std::string& udid)
89 {
90 udid_ = udid;
91 }
92
GetUuid() const93 std::string TrustedDeviceInfo::GetUuid() const
94 {
95 return uuid_;
96 }
97
SetUuid(const std::string & uuid)98 void TrustedDeviceInfo::SetUuid(const std::string& uuid)
99 {
100 uuid_ = uuid;
101 }
102
operator !=(const TrustedDeviceInfo & other) const103 bool TrustedDeviceInfo::operator!=(const TrustedDeviceInfo& other) const
104 {
105 return (networkId_ != other.GetNetworkId() || authForm_ != other.GetAuthForm() ||
106 deviceTypeId_ != other.GetDeviceTypeId() || osVersion_ != other.GetOsVersion() ||
107 osType_ != other.GetOsType() || udid_ != other.GetUdid() ||
108 uuid_ != other.GetUuid());
109 }
110
operator <(const TrustedDeviceInfo & other) const111 bool TrustedDeviceInfo::operator<(const TrustedDeviceInfo& other) const
112 {
113 return ((networkId_ < other.GetNetworkId()) ||
114 (networkId_ == other.GetNetworkId() && authForm_ < other.GetAuthForm()) ||
115 (networkId_ == other.GetNetworkId() && authForm_ == other.GetAuthForm() &&
116 deviceTypeId_ < other.GetDeviceTypeId()) ||
117 (networkId_ == other.GetNetworkId() && authForm_ == other.GetAuthForm() &&
118 deviceTypeId_ == other.GetDeviceTypeId() && osVersion_ < other.GetOsVersion()) ||
119 (networkId_ == other.GetNetworkId() && authForm_ == other.GetAuthForm() &&
120 deviceTypeId_ == other.GetDeviceTypeId() && osVersion_ == other.GetOsVersion() &&
121 osType_ < other.GetOsType()) ||
122 (networkId_ == other.GetNetworkId() && authForm_ == other.GetAuthForm() &&
123 deviceTypeId_ == other.GetDeviceTypeId() && osVersion_ == other.GetOsVersion() &&
124 osType_ == other.GetOsType() && udid_ < other.GetUdid()) ||
125 (networkId_ == other.GetNetworkId() && authForm_ == other.GetAuthForm() &&
126 deviceTypeId_ == other.GetDeviceTypeId() && osVersion_ == other.GetOsVersion() &&
127 osType_ == other.GetOsType() && udid_ == other.GetUdid() && uuid_ < other.GetUuid()));
128 }
129
Marshalling(MessageParcel & parcel) const130 bool TrustedDeviceInfo::Marshalling(MessageParcel& parcel) const
131 {
132 WRITE_HELPER_RET(parcel, String, networkId_, false);
133 WRITE_HELPER_RET(parcel, Int32, authForm_, false);
134 WRITE_HELPER_RET(parcel, Uint16, deviceTypeId_, false);
135 WRITE_HELPER_RET(parcel, String, osVersion_, false);
136 WRITE_HELPER_RET(parcel, Int32, osType_, false);
137 WRITE_HELPER_RET(parcel, String, udid_, false);
138 WRITE_HELPER_RET(parcel, String, uuid_, false);
139 return true;
140 }
141
UnMarshalling(MessageParcel & parcel)142 bool TrustedDeviceInfo::UnMarshalling(MessageParcel& parcel)
143 {
144 READ_HELPER_RET(parcel, String, networkId_, false);
145 READ_HELPER_RET(parcel, Int32, authForm_, false);
146 READ_HELPER_RET(parcel, Uint16, deviceTypeId_, false);
147 READ_HELPER_RET(parcel, String, osVersion_, false);
148 READ_HELPER_RET(parcel, Int32, osType_, false);
149 READ_HELPER_RET(parcel, String, udid_, false);
150 READ_HELPER_RET(parcel, String, uuid_, false);
151 return true;
152 }
153
dump() const154 std::string TrustedDeviceInfo::dump() const
155 {
156 cJSON* json = cJSON_CreateObject();
157 if (!cJSON_IsObject(json)) {
158 cJSON_Delete(json);
159 return EMPTY_STRING;
160 }
161 cJSON_AddStringToObject(json, NETWORK_ID.c_str(), ProfileUtils::GetAnonyString(networkId_).c_str());
162 cJSON_AddNumberToObject(json, AUTH_FORM.c_str(), authForm_);
163 cJSON_AddNumberToObject(json, DEVICE_TYPE_ID.c_str(), deviceTypeId_);
164 cJSON_AddStringToObject(json, OS_VERSION.c_str(), ProfileUtils::GetAnonyString(osVersion_).c_str());
165 cJSON_AddNumberToObject(json, OS_TYPE.c_str(), osType_);
166 cJSON_AddStringToObject(json, UDID.c_str(), ProfileUtils::GetAnonyString(udid_).c_str());
167 cJSON_AddStringToObject(json, UUID.c_str(), ProfileUtils::GetAnonyString(uuid_).c_str());
168 char* jsonChars = cJSON_PrintUnformatted(json);
169 cJSON_Delete(json);
170 if (jsonChars == NULL) {
171 HILOGE("cJSON formatted to string failed!");
172 return EMPTY_STRING;
173 }
174 std::string jsonStr = jsonChars;
175 cJSON_free(jsonChars);
176 return jsonStr;
177 }
178 } // namespace DistributedDeviceProfile
179 } // namespace OHOS
180