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 "trust_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 = "TrustDeviceProfile";
26 }
GetDeviceId() const27 std::string TrustDeviceProfile::GetDeviceId() const
28 {
29 return deviceId_;
30 }
31
SetDeviceId(const std::string & deviceId)32 void TrustDeviceProfile::SetDeviceId(const std::string& deviceId)
33 {
34 deviceId_ = deviceId;
35 }
36
GetDeviceIdType() const37 int32_t TrustDeviceProfile::GetDeviceIdType() const
38 {
39 return deviceIdType_;
40 }
41
SetDeviceIdType(int32_t deviceType)42 void TrustDeviceProfile::SetDeviceIdType(int32_t deviceType)
43 {
44 deviceIdType_ = deviceType;
45 }
46
GetDeviceIdHash() const47 std::string TrustDeviceProfile::GetDeviceIdHash() const
48 {
49 return deviceIdHash_;
50 }
51
SetDeviceIdHash(const std::string & deviceIdHash)52 void TrustDeviceProfile::SetDeviceIdHash(const std::string& deviceIdHash)
53 {
54 deviceIdHash_ = deviceIdHash;
55 }
56
GetStatus() const57 int32_t TrustDeviceProfile::GetStatus() const
58 {
59 return status_;
60 }
61
SetStatus(int32_t status)62 void TrustDeviceProfile::SetStatus(int32_t status)
63 {
64 status_ = status;
65 }
66
GetBindType() const67 uint32_t TrustDeviceProfile::GetBindType() const
68 {
69 return bindType_;
70 }
71
SetBindType(uint32_t bindType)72 void TrustDeviceProfile::SetBindType(uint32_t bindType)
73 {
74 bindType_ = bindType;
75 }
76
GetPeerUserId() const77 int32_t TrustDeviceProfile::GetPeerUserId() const
78 {
79 return peerUserId_;
80 }
81
SetPeerUserId(int32_t peerUserId)82 void TrustDeviceProfile::SetPeerUserId(int32_t peerUserId)
83 {
84 peerUserId_ = peerUserId;
85 }
86
GetLocalUserId() const87 int32_t TrustDeviceProfile::GetLocalUserId() const
88 {
89 return localUserId_;
90 }
91
SetLocalUserId(int32_t localUserId)92 void TrustDeviceProfile::SetLocalUserId(int32_t localUserId)
93 {
94 localUserId_ = localUserId;
95 }
96
Marshalling(MessageParcel & parcel) const97 bool TrustDeviceProfile::Marshalling(MessageParcel& parcel) const
98 {
99 WRITE_HELPER_RET(parcel, String, deviceId_, false);
100 WRITE_HELPER_RET(parcel, Uint32, deviceIdType_, false);
101 WRITE_HELPER_RET(parcel, String, deviceIdHash_, false);
102 WRITE_HELPER_RET(parcel, Int32, status_, false);
103 WRITE_HELPER_RET(parcel, Uint32, bindType_, false);
104 WRITE_HELPER_RET(parcel, Int32, peerUserId_, false);
105 WRITE_HELPER_RET(parcel, Int32, localUserId_, false);
106 return true;
107 }
108
UnMarshalling(MessageParcel & parcel)109 bool TrustDeviceProfile::UnMarshalling(MessageParcel& parcel)
110 {
111 READ_HELPER_RET(parcel, String, deviceId_, false);
112 READ_HELPER_RET(parcel, Uint32, deviceIdType_, false);
113 READ_HELPER_RET(parcel, String, deviceIdHash_, false);
114 READ_HELPER_RET(parcel, Int32, status_, false);
115 READ_HELPER_RET(parcel, Uint32, bindType_, false);
116 READ_HELPER_RET(parcel, Int32, peerUserId_, false);
117 READ_HELPER_RET(parcel, Int32, localUserId_, false);
118 return true;
119 }
120
dump() const121 std::string TrustDeviceProfile::dump() const
122 {
123 cJSON* json = cJSON_CreateObject();
124 if (!cJSON_IsObject(json)) {
125 cJSON_Delete(json);
126 return EMPTY_STRING;
127 }
128 cJSON_AddStringToObject(json, DEVICE_ID.c_str(), ProfileUtils::GetAnonyString(deviceId_).c_str());
129 cJSON_AddNumberToObject(json, DEVICE_ID_TYPE.c_str(), deviceIdType_);
130 cJSON_AddStringToObject(json, DEVICE_ID_HASH.c_str(), deviceIdHash_.c_str());
131 cJSON_AddNumberToObject(json, STATUS.c_str(), status_);
132 cJSON_AddNumberToObject(json, BIND_TYPE.c_str(), bindType_);
133 cJSON_AddStringToObject(json, PEER_USER_ID.c_str(),
134 ProfileUtils::GetAnonyString(std::to_string(peerUserId_)).c_str());
135 cJSON_AddStringToObject(json, LOCAL_USER_ID.c_str(),
136 ProfileUtils::GetAnonyString(std::to_string(localUserId_)).c_str());
137 char* jsonChars = cJSON_PrintUnformatted(json);
138 if (jsonChars == NULL) {
139 cJSON_Delete(json);
140 HILOGE("cJSON formatted to string failed!");
141 return EMPTY_STRING;
142 }
143 std::string jsonStr = jsonChars;
144 cJSON_Delete(json);
145 cJSON_free(jsonChars);
146 return jsonStr;
147 }
148 } // namespace DistributedDeviceProfile
149 } // namespace OHOS