1 /*
2 * Copyright (c) 2025 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 "query_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 = "QueryProfile";
26 }
27
QueryProfile()28 QueryProfile::QueryProfile()
29 : accesserDeviceId_(""),
30 accesserUserId_(-1),
31 accesserAccountId_(""),
32 accesserTokenId_(-1),
33 accesseeDeviceId_(""),
34 accesseeUserId_(-1),
35 accesseeAccountId_(""),
36 accesseeTokenId_(-1)
37 {}
38
GetAccesserDeviceId() const39 std::string QueryProfile::GetAccesserDeviceId() const
40 {
41 return accesserDeviceId_;
42 }
43
SetAccesserDeviceId(const std::string & accesserDeviceId)44 void QueryProfile::SetAccesserDeviceId(const std::string& accesserDeviceId)
45 {
46 accesserDeviceId_ = accesserDeviceId;
47 }
48
GetAccesserTokenId() const49 int64_t QueryProfile::GetAccesserTokenId() const
50 {
51 return accesserTokenId_;
52 }
53
SetAccesserTokenId(int64_t accesserTokenId)54 void QueryProfile::SetAccesserTokenId(int64_t accesserTokenId)
55 {
56 accesserTokenId_ = accesserTokenId;
57 }
58
GetAccesserUserId() const59 int32_t QueryProfile::GetAccesserUserId() const
60 {
61 return accesserUserId_;
62 }
63
SetAccesserUserId(int32_t accesserUserId)64 void QueryProfile::SetAccesserUserId(int32_t accesserUserId)
65 {
66 accesserUserId_ = accesserUserId;
67 }
68
GetAccesserAccountId() const69 std::string QueryProfile::GetAccesserAccountId() const
70 {
71 return accesserAccountId_;
72 }
73
SetAccesserAccountId(const std::string & accesserAccountId)74 void QueryProfile::SetAccesserAccountId(const std::string& accesserAccountId)
75 {
76 accesserAccountId_ = accesserAccountId;
77 }
78
GetAccesseeDeviceId() const79 std::string QueryProfile::GetAccesseeDeviceId() const
80 {
81 return accesseeDeviceId_;
82 }
83
SetAccesseeDeviceId(const std::string & accesseeDeviceId)84 void QueryProfile::SetAccesseeDeviceId(const std::string& accesseeDeviceId)
85 {
86 accesseeDeviceId_ = accesseeDeviceId;
87 }
88
GetAccesseeTokenId() const89 int64_t QueryProfile::GetAccesseeTokenId() const
90 {
91 return accesseeTokenId_;
92 }
93
SetAccesseeTokenId(int64_t accesseeTokenId)94 void QueryProfile::SetAccesseeTokenId(int64_t accesseeTokenId)
95 {
96 accesseeTokenId_ = accesseeTokenId;
97 }
98
GetAccesseeUserId() const99 int32_t QueryProfile::GetAccesseeUserId() const
100 {
101 return accesseeUserId_;
102 }
103
GetAccesseeAccountId() const104 std::string QueryProfile::GetAccesseeAccountId() const
105 {
106 return accesseeAccountId_;
107 }
108
SetAccesseeAccountId(const std::string & accesseeAccountId)109 void QueryProfile::SetAccesseeAccountId(const std::string& accesseeAccountId)
110 {
111 accesseeAccountId_ = accesseeAccountId;
112 }
113
SetAccesseeUserId(int32_t accesseeUserId)114 void QueryProfile::SetAccesseeUserId(int32_t accesseeUserId)
115 {
116 accesseeUserId_ = accesseeUserId;
117 }
118
Marshalling(MessageParcel & parcel) const119 bool QueryProfile::Marshalling(MessageParcel& parcel) const
120 {
121 WRITE_HELPER_RET(parcel, String, accesserDeviceId_, false);
122 WRITE_HELPER_RET(parcel, Int32, accesserUserId_, false);
123 WRITE_HELPER_RET(parcel, String, accesserAccountId_, false);
124 WRITE_HELPER_RET(parcel, Int64, accesserTokenId_, false);
125 WRITE_HELPER_RET(parcel, String, accesseeDeviceId_, false);
126 WRITE_HELPER_RET(parcel, Int32, accesseeUserId_, false);
127 WRITE_HELPER_RET(parcel, String, accesseeAccountId_, false);
128 WRITE_HELPER_RET(parcel, Int64, accesseeTokenId_, false);
129 return true;
130 }
131
UnMarshalling(MessageParcel & parcel)132 bool QueryProfile::UnMarshalling(MessageParcel& parcel)
133 {
134 READ_HELPER_RET(parcel, String, accesserDeviceId_, false);
135 READ_HELPER_RET(parcel, Int32, accesserUserId_, false);
136 READ_HELPER_RET(parcel, String, accesserAccountId_, false);
137 READ_HELPER_RET(parcel, Int64, accesserTokenId_, false);
138 READ_HELPER_RET(parcel, String, accesseeDeviceId_, false);
139 READ_HELPER_RET(parcel, Int32, accesseeUserId_, false);
140 READ_HELPER_RET(parcel, String, accesseeAccountId_, false);
141 READ_HELPER_RET(parcel, Int64, accesseeTokenId_, false);
142 return true;
143 }
144
dump() const145 std::string QueryProfile::dump() const
146 {
147 cJSON* json = cJSON_CreateObject();
148 if (!cJSON_IsObject(json)) {
149 cJSON_Delete(json);
150 return EMPTY_STRING;
151 }
152 cJSON_AddStringToObject(json, ACCESSER_DEVICE_ID.c_str(), ProfileUtils::GetAnonyString(accesserDeviceId_).c_str());
153 cJSON_AddStringToObject(json, ACCESSER_USER_ID.c_str(),
154 ProfileUtils::GetAnonyString(std::to_string(accesserUserId_)).c_str());
155 cJSON_AddStringToObject(json, ACCESSER_ACCOUNT_ID.c_str(),
156 ProfileUtils::GetAnonyString(accesserAccountId_).c_str());
157 cJSON_AddNumberToObject(json, ACCESSER_TOKEN_ID.c_str(), accesserTokenId_);
158 cJSON_AddStringToObject(json, ACCESSEE_DEVICE_ID.c_str(), ProfileUtils::GetAnonyString(accesseeDeviceId_).c_str());
159 cJSON_AddStringToObject(json, ACCESSEE_USER_ID.c_str(),
160 ProfileUtils::GetAnonyString(std::to_string(accesseeUserId_)).c_str());
161 cJSON_AddStringToObject(json, ACCESSEE_ACCOUNT_ID.c_str(),
162 ProfileUtils::GetAnonyString(accesseeAccountId_).c_str());
163 cJSON_AddNumberToObject(json, ACCESSEE_TOKEN_ID.c_str(), accesseeTokenId_);
164 char* jsonChars = cJSON_PrintUnformatted(json);
165 if (jsonChars == NULL) {
166 cJSON_Delete(json);
167 HILOGE("cJSON formatted to string failed!");
168 return EMPTY_STRING;
169 }
170 std::string jsonStr = jsonChars;
171 cJSON_Delete(json);
172 cJSON_free(jsonChars);
173 return jsonStr;
174 }
175 } // namespace DistributedDeviceProfile
176 } // namespace OHOS