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 "access_control_profile.h"
17 #include <cstdint>
18 #include <string>
19 #include "cJSON.h"
20 #include "distributed_device_profile_constants.h"
21 #include "macro_utils.h"
22 #include "profile_utils.h"
23
24 namespace OHOS {
25 namespace DistributedDeviceProfile {
26 namespace {
27 const std::string TAG = "AccessControlProfile";
28 }
AccessControlProfile()29 AccessControlProfile::AccessControlProfile()
30 : accessControlId_(0),
31 accesserId_(0),
32 accesseeId_(0),
33 sessionKey_(""),
34 bindType_(static_cast<uint32_t>(BindType::MIN)),
35 authenticationType_(static_cast<uint32_t>(AuthenticationType::MIN)),
36 bindLevel_(static_cast<uint32_t>(BindLevel::MIN)),
37 status_(static_cast<int32_t>(Status::MIN)),
38 validPeriod_(-1),
39 lastAuthTime_(-1),
40 trustDeviceId_(""),
41 deviceIdType_((uint32_t)DeviceIdType::MIN),
42 deviceIdHash_(""),
43 extraData_(""),
44 accesser_({}),
45 accessee_({})
46 {
47 }
48
GetAccessControlId() const49 int64_t AccessControlProfile::GetAccessControlId() const
50 {
51 return accessControlId_;
52 }
53
SetAccessControlId(int64_t accessControlId)54 void AccessControlProfile::SetAccessControlId(int64_t accessControlId)
55 {
56 accessControlId_ = accessControlId;
57 }
58
GetAccesserId() const59 int64_t AccessControlProfile::GetAccesserId() const
60 {
61 return accesserId_;
62 }
63
SetAccesserId(int64_t accesserId)64 void AccessControlProfile::SetAccesserId(int64_t accesserId)
65 {
66 accesserId_ = accesserId;
67 }
68
GetAccesseeId() const69 int64_t AccessControlProfile::GetAccesseeId() const
70 {
71 return accesseeId_;
72 }
73
SetAccesseeId(int64_t accesseeId)74 void AccessControlProfile::SetAccesseeId(int64_t accesseeId)
75 {
76 accesseeId_ = accesseeId;
77 }
78
GetSessionKey() const79 std::string AccessControlProfile::GetSessionKey() const
80 {
81 return sessionKey_;
82 }
83
SetSessionKey(const std::string & sessionKey)84 void AccessControlProfile::SetSessionKey(const std::string &sessionKey)
85 {
86 sessionKey_ = sessionKey;
87 }
88
GetTrustDeviceId() const89 std::string AccessControlProfile::GetTrustDeviceId() const
90 {
91 return trustDeviceId_;
92 }
93
SetTrustDeviceId(const std::string & trustDeviceId)94 void AccessControlProfile::SetTrustDeviceId(const std::string &trustDeviceId)
95 {
96 trustDeviceId_ = trustDeviceId;
97 }
98
GetBindType() const99 uint32_t AccessControlProfile::GetBindType() const
100 {
101 return bindType_;
102 }
103
SetBindType(uint32_t bindType)104 void AccessControlProfile::SetBindType(uint32_t bindType)
105 {
106 bindType_ = bindType;
107 }
108
GetAuthenticationType() const109 uint32_t AccessControlProfile::GetAuthenticationType() const
110 {
111 return authenticationType_;
112 }
113
SetAuthenticationType(uint32_t authenticationType)114 void AccessControlProfile::SetAuthenticationType(uint32_t authenticationType)
115 {
116 authenticationType_ = authenticationType;
117 }
118
GetStatus() const119 int32_t AccessControlProfile::GetStatus() const
120 {
121 return status_;
122 }
123
SetStatus(int32_t status)124 void AccessControlProfile::SetStatus(int32_t status)
125 {
126 status_ = status;
127 }
128
GetValidPeriod() const129 int64_t AccessControlProfile::GetValidPeriod() const
130 {
131 return validPeriod_;
132 }
133
SetValidPeriod(int64_t validPeriod)134 void AccessControlProfile::SetValidPeriod(int64_t validPeriod)
135 {
136 validPeriod_ = validPeriod;
137 }
138
GetLastAuthTime() const139 int64_t AccessControlProfile::GetLastAuthTime() const
140 {
141 return lastAuthTime_;
142 }
143
SetLastAuthTime(int64_t lastAuthTime)144 void AccessControlProfile::SetLastAuthTime(int64_t lastAuthTime)
145 {
146 lastAuthTime_ = lastAuthTime;
147 }
148
GetAccesser() const149 Accesser AccessControlProfile::GetAccesser() const
150 {
151 return accesser_;
152 }
153
SetAccesser(const Accesser & accesser)154 void AccessControlProfile::SetAccesser(const Accesser &accesser)
155 {
156 // impl deep clone
157 accesser_ = accesser;
158 }
159
GetAccessee() const160 Accessee AccessControlProfile::GetAccessee() const
161 {
162 return accessee_;
163 }
164
SetAccessee(const Accessee & accessee)165 void AccessControlProfile::SetAccessee(const Accessee &accessee)
166 {
167 // impl deep clone
168 accessee_ = accessee;
169 }
170
GetBindLevel() const171 uint32_t AccessControlProfile::GetBindLevel() const
172 {
173 return bindLevel_;
174 }
175
SetBindLevel(uint32_t bindLevel)176 void AccessControlProfile::SetBindLevel(uint32_t bindLevel)
177 {
178 bindLevel_ = bindLevel;
179 }
180
GetDeviceIdType() const181 uint32_t AccessControlProfile::GetDeviceIdType() const
182 {
183 return deviceIdType_;
184 }
185
SetDeviceIdType(uint32_t deviceIdType)186 void AccessControlProfile::SetDeviceIdType(uint32_t deviceIdType)
187 {
188 deviceIdType_ = deviceIdType;
189 }
190
GetDeviceIdHash() const191 std::string AccessControlProfile::GetDeviceIdHash() const
192 {
193 return deviceIdHash_;
194 }
195
SetDeviceIdHash(const std::string & deviceIdHash)196 void AccessControlProfile::SetDeviceIdHash(const std::string& deviceIdHash)
197 {
198 deviceIdHash_ = deviceIdHash;
199 }
200
GetExtraData() const201 std::string AccessControlProfile::GetExtraData() const
202 {
203 return extraData_;
204 }
205
SetExtraData(const std::string & extraData)206 void AccessControlProfile::SetExtraData(const std::string& extraData)
207 {
208 extraData_ = extraData;
209 }
210
Marshalling(MessageParcel & parcel) const211 bool AccessControlProfile::Marshalling(MessageParcel& parcel) const
212 {
213 WRITE_HELPER_RET(parcel, Int64, accessControlId_, false);
214 WRITE_HELPER_RET(parcel, Int64, accesserId_, false);
215 WRITE_HELPER_RET(parcel, Int64, accesseeId_, false);
216 WRITE_HELPER_RET(parcel, String, sessionKey_, false);
217 WRITE_HELPER_RET(parcel, Uint32, bindType_, false);
218 WRITE_HELPER_RET(parcel, Uint32, authenticationType_, false);
219 WRITE_HELPER_RET(parcel, Uint32, bindLevel_, false);
220 WRITE_HELPER_RET(parcel, Int32, status_, false);
221 WRITE_HELPER_RET(parcel, Int64, validPeriod_, false);
222 WRITE_HELPER_RET(parcel, Int64, lastAuthTime_, false);
223 WRITE_HELPER_RET(parcel, String, trustDeviceId_, false);
224 WRITE_HELPER_RET(parcel, Uint32, deviceIdType_, false);
225 WRITE_HELPER_RET(parcel, String, deviceIdHash_, false);
226 WRITE_HELPER_RET(parcel, String, extraData_, false);
227 accesser_.Marshalling(parcel);
228 accessee_.Marshalling(parcel);
229 return true;
230 }
231
UnMarshalling(MessageParcel & parcel)232 bool AccessControlProfile::UnMarshalling(MessageParcel& parcel)
233 {
234 READ_HELPER_RET(parcel, Int64, accessControlId_, false);
235 READ_HELPER_RET(parcel, Int64, accesserId_, false);
236 READ_HELPER_RET(parcel, Int64, accesseeId_, false);
237 READ_HELPER_RET(parcel, String, sessionKey_, false);
238 READ_HELPER_RET(parcel, Uint32, bindType_, false);
239 READ_HELPER_RET(parcel, Uint32, authenticationType_, false);
240 READ_HELPER_RET(parcel, Uint32, bindLevel_, false);
241 READ_HELPER_RET(parcel, Int32, status_, false);
242 READ_HELPER_RET(parcel, Int64, validPeriod_, false);
243 READ_HELPER_RET(parcel, Int64, lastAuthTime_, false);
244 READ_HELPER_RET(parcel, String, trustDeviceId_, false);
245 READ_HELPER_RET(parcel, Uint32, deviceIdType_, false);
246 READ_HELPER_RET(parcel, String, deviceIdHash_, false);
247 READ_HELPER_RET(parcel, String, extraData_, false);
248 accesser_.UnMarshalling(parcel);
249 accessee_.UnMarshalling(parcel);
250 return true;
251 }
252
dump() const253 std::string AccessControlProfile::dump() const
254 {
255 cJSON* json = cJSON_CreateObject();
256 if (!cJSON_IsObject(json)) {
257 cJSON_Delete(json);
258 return EMPTY_STRING;
259 }
260 cJSON_AddNumberToObject(json, ACCESS_CONTROL_ID.c_str(), accessControlId_);
261 cJSON_AddNumberToObject(json, ACCESSER_ID.c_str(), accesserId_);
262 cJSON_AddNumberToObject(json, ACCESSEE_ID.c_str(), accesseeId_);
263 cJSON_AddNumberToObject(json, BIND_TYPE.c_str(), bindType_);
264 cJSON_AddNumberToObject(json, AUTHENTICATION_TYPE.c_str(), authenticationType_);
265 cJSON_AddNumberToObject(json, BIND_LEVEL.c_str(), bindLevel_);
266 cJSON_AddNumberToObject(json, STATUS.c_str(), status_);
267 cJSON_AddStringToObject(json, TRUST_DEVICE_ID.c_str(), ProfileUtils::GetAnonyString(trustDeviceId_).c_str());
268 cJSON_AddNumberToObject(json, DEVICE_ID_TYPE.c_str(), deviceIdType_);
269 cJSON_AddStringToObject(json, DEVICE_ID_HASH.c_str(), deviceIdHash_.c_str());
270 cJSON_AddStringToObject(json, EXTRA_DATA.c_str(), extraData_.c_str());
271 char* jsonChars = cJSON_PrintUnformatted(json);
272 if (jsonChars == NULL) {
273 cJSON_Delete(json);
274 HILOGE("cJSON formatted to string failed!");
275 return EMPTY_STRING;
276 }
277 std::string jsonStr = jsonChars;
278 cJSON_Delete(json);
279 cJSON_free(jsonChars);
280 return jsonStr;
281 }
282 } // namespace DistributedDeviceProfile
283 } // namespace OHOS