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     accesser_({}),
44     accessee_({})
45 {
46 }
47 
GetAccessControlId() const48 int64_t AccessControlProfile::GetAccessControlId() const
49 {
50     return accessControlId_;
51 }
52 
SetAccessControlId(int64_t accessControlId)53 void AccessControlProfile::SetAccessControlId(int64_t accessControlId)
54 {
55     accessControlId_ = accessControlId;
56 }
57 
GetAccesserId() const58 int64_t AccessControlProfile::GetAccesserId() const
59 {
60     return accesserId_;
61 }
62 
SetAccesserId(int64_t accesserId)63 void AccessControlProfile::SetAccesserId(int64_t accesserId)
64 {
65     accesserId_ = accesserId;
66 }
67 
GetAccesseeId() const68 int64_t AccessControlProfile::GetAccesseeId() const
69 {
70     return accesseeId_;
71 }
72 
SetAccesseeId(int64_t accesseeId)73 void AccessControlProfile::SetAccesseeId(int64_t accesseeId)
74 {
75     accesseeId_ = accesseeId;
76 }
77 
GetSessionKey() const78 std::string AccessControlProfile::GetSessionKey() const
79 {
80     return sessionKey_;
81 }
82 
SetSessionKey(const std::string & sessionKey)83 void AccessControlProfile::SetSessionKey(const std::string &sessionKey)
84 {
85     sessionKey_ = sessionKey;
86 }
87 
GetTrustDeviceId() const88 std::string AccessControlProfile::GetTrustDeviceId() const
89 {
90     return trustDeviceId_;
91 }
92 
SetTrustDeviceId(const std::string & trustDeviceId)93 void AccessControlProfile::SetTrustDeviceId(const std::string &trustDeviceId)
94 {
95     trustDeviceId_ = trustDeviceId;
96 }
97 
GetBindType() const98 uint32_t AccessControlProfile::GetBindType() const
99 {
100     return bindType_;
101 }
102 
SetBindType(uint32_t bindType)103 void AccessControlProfile::SetBindType(uint32_t bindType)
104 {
105     bindType_ = bindType;
106 }
107 
GetAuthenticationType() const108 uint32_t AccessControlProfile::GetAuthenticationType() const
109 {
110     return authenticationType_;
111 }
112 
SetAuthenticationType(uint32_t authenticationType)113 void AccessControlProfile::SetAuthenticationType(uint32_t authenticationType)
114 {
115     authenticationType_ = authenticationType;
116 }
117 
GetStatus() const118 int32_t AccessControlProfile::GetStatus() const
119 {
120     return status_;
121 }
122 
SetStatus(int32_t status)123 void AccessControlProfile::SetStatus(int32_t status)
124 {
125     status_ = status;
126 }
127 
GetValidPeriod() const128 int32_t AccessControlProfile::GetValidPeriod() const
129 {
130     return validPeriod_;
131 }
132 
SetValidPeriod(int32_t validPeriod)133 void AccessControlProfile::SetValidPeriod(int32_t validPeriod)
134 {
135     validPeriod_ = validPeriod;
136 }
137 
GetLastAuthTime() const138 int32_t AccessControlProfile::GetLastAuthTime() const
139 {
140     return lastAuthTime_;
141 }
142 
SetLastAuthTime(int32_t lastAuthTime)143 void AccessControlProfile::SetLastAuthTime(int32_t lastAuthTime)
144 {
145     lastAuthTime_ = lastAuthTime;
146 }
147 
GetAccesser() const148 Accesser AccessControlProfile::GetAccesser() const
149 {
150     return accesser_;
151 }
152 
SetAccesser(const Accesser & accesser)153 void AccessControlProfile::SetAccesser(const Accesser &accesser)
154 {
155     // impl deep clone
156     accesser_ = accesser;
157 }
158 
GetAccessee() const159 Accessee AccessControlProfile::GetAccessee() const
160 {
161     return accessee_;
162 }
163 
SetAccessee(const Accessee & accessee)164 void AccessControlProfile::SetAccessee(const Accessee &accessee)
165 {
166     // impl deep clone
167     accessee_ = accessee;
168 }
169 
GetBindLevel() const170 uint32_t AccessControlProfile::GetBindLevel() const
171 {
172     return bindLevel_;
173 }
174 
SetBindLevel(uint32_t bindLevel)175 void AccessControlProfile::SetBindLevel(uint32_t bindLevel)
176 {
177     bindLevel_ = bindLevel;
178 }
179 
GetDeviceIdType() const180 uint32_t AccessControlProfile::GetDeviceIdType() const
181 {
182     return deviceIdType_;
183 }
184 
SetDeviceIdType(uint32_t deviceIdType)185 void AccessControlProfile::SetDeviceIdType(uint32_t deviceIdType)
186 {
187     deviceIdType_ = deviceIdType;
188 }
189 
GetDeviceIdHash() const190 std::string AccessControlProfile::GetDeviceIdHash() const
191 {
192     return deviceIdHash_;
193 }
194 
SetDeviceIdHash(const std::string & deviceIdHash)195 void AccessControlProfile::SetDeviceIdHash(const std::string& deviceIdHash)
196 {
197     deviceIdHash_ = deviceIdHash;
198 }
199 
Marshalling(MessageParcel & parcel) const200 bool AccessControlProfile::Marshalling(MessageParcel& parcel) const
201 {
202     WRITE_HELPER_RET(parcel, Int64, accessControlId_, false);
203     WRITE_HELPER_RET(parcel, Int64, accesserId_, false);
204     WRITE_HELPER_RET(parcel, Int64, accesseeId_, false);
205     WRITE_HELPER_RET(parcel, String, sessionKey_, false);
206     WRITE_HELPER_RET(parcel, Uint32, bindType_, false);
207     WRITE_HELPER_RET(parcel, Uint32, authenticationType_, false);
208     WRITE_HELPER_RET(parcel, Uint32, bindLevel_, false);
209     WRITE_HELPER_RET(parcel, Int32, status_, false);
210     WRITE_HELPER_RET(parcel, Int32, validPeriod_, false);
211     WRITE_HELPER_RET(parcel, Int32, lastAuthTime_, false);
212     WRITE_HELPER_RET(parcel, String, trustDeviceId_, false);
213     WRITE_HELPER_RET(parcel, Uint32, deviceIdType_, false);
214     WRITE_HELPER_RET(parcel, String, deviceIdHash_, false);
215     accesser_.Marshalling(parcel);
216     accessee_.Marshalling(parcel);
217     return true;
218 }
219 
UnMarshalling(MessageParcel & parcel)220 bool AccessControlProfile::UnMarshalling(MessageParcel& parcel)
221 {
222     READ_HELPER_RET(parcel, Int64, accessControlId_, false);
223     READ_HELPER_RET(parcel, Int64, accesserId_, false);
224     READ_HELPER_RET(parcel, Int64, accesseeId_, false);
225     READ_HELPER_RET(parcel, String, sessionKey_, false);
226     READ_HELPER_RET(parcel, Uint32, bindType_, false);
227     READ_HELPER_RET(parcel, Uint32, authenticationType_, false);
228     READ_HELPER_RET(parcel, Uint32, bindLevel_, false);
229     READ_HELPER_RET(parcel, Int32, status_, false);
230     READ_HELPER_RET(parcel, Int32, validPeriod_, false);
231     READ_HELPER_RET(parcel, Int32, lastAuthTime_, false);
232     READ_HELPER_RET(parcel, String, trustDeviceId_, false);
233     READ_HELPER_RET(parcel, Uint32, deviceIdType_, false);
234     READ_HELPER_RET(parcel, String, deviceIdHash_, false);
235     accesser_.UnMarshalling(parcel);
236     accessee_.UnMarshalling(parcel);
237     return true;
238 }
239 
dump() const240 std::string AccessControlProfile::dump() const
241 {
242     cJSON* json = cJSON_CreateObject();
243     if (!cJSON_IsObject(json)) {
244         cJSON_Delete(json);
245         return EMPTY_STRING;
246     }
247     cJSON_AddNumberToObject(json, ACCESS_CONTROL_ID.c_str(), accessControlId_);
248     cJSON_AddNumberToObject(json, ACCESSER_ID.c_str(), accesserId_);
249     cJSON_AddNumberToObject(json, ACCESSEE_ID.c_str(), accesseeId_);
250     cJSON_AddStringToObject(json, SESSION_KEY.c_str(), sessionKey_.c_str());
251     cJSON_AddNumberToObject(json, BIND_TYPE.c_str(), bindType_);
252     cJSON_AddNumberToObject(json, AUTHENTICATION_TYPE.c_str(), authenticationType_);
253     cJSON_AddNumberToObject(json, BIND_LEVEL.c_str(), bindLevel_);
254     cJSON_AddNumberToObject(json, STATUS.c_str(), status_);
255     cJSON_AddNumberToObject(json, VALID_PERIOD.c_str(), validPeriod_);
256     cJSON_AddNumberToObject(json, LAST_AUTH_TIME.c_str(), lastAuthTime_);
257     cJSON_AddStringToObject(json, TRUST_DEVICE_ID.c_str(), ProfileUtils::GetAnonyString(trustDeviceId_).c_str());
258     cJSON_AddNumberToObject(json, DEVICE_ID_TYPE.c_str(), deviceIdType_);
259     cJSON_AddStringToObject(json, DEVICE_ID_HASH.c_str(), deviceIdHash_.c_str());
260     char* jsonChars = cJSON_PrintUnformatted(json);
261     if (jsonChars == NULL) {
262         cJSON_Delete(json);
263         HILOGE("cJSON formatted to string failed!");
264         return EMPTY_STRING;
265     }
266     std::string jsonStr = jsonChars;
267     cJSON_Delete(json);
268     cJSON_free(jsonChars);
269     return jsonStr;
270 }
271 } // namespace DistributedDeviceProfile
272 } // namespace OHOS