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 "dm_transport_msg.h"
17 #include "dm_log.h"
18
19 namespace OHOS {
20 namespace DistributedHardware {
21 namespace {
22 const int32_t MAX_USER_ID_NUM = 5;
23 const int32_t MAX_BACKGROUND_USER_ID_NUM = 5;
24 const char* const FOREGROUND_USERIDS_MSG_USERIDS_KEY = "foregroundUserIds";
25 const char* const BACKGROUND_USERIDS_MSG_USERIDS_KEY = "backgroundUserIds";
26 const char* const COMM_MSG_CODE_KEY = "code";
27 const char* const COMM_MSG_MSG_KEY = "msg";
28 const char* const DSOFTBUS_NOTIFY_USERIDS_UDIDKEY = "remoteUdid";
29 const char* const DSOFTBUS_NOTIFY_USERIDS_USERIDKEY = "foregroundUserIds";
30 const char* const DSOFTBUS_NOTIFY_ACCOUNTID_KEY = "accountId";
31 const char* const DSOFTBUS_NOTIFY_USERID_KEY = "userId";
32 }
ToJson(cJSON * jsonObject,const UserIdsMsg & userIdsMsg)33 void ToJson(cJSON *jsonObject, const UserIdsMsg &userIdsMsg)
34 {
35 if (jsonObject == nullptr) {
36 LOGE("Json pointer is nullptr!");
37 return;
38 }
39 cJSON *foregroundUserIdArr = cJSON_CreateArray();
40 if (foregroundUserIdArr == nullptr) {
41 return;
42 }
43 cJSON *numberObj = nullptr;
44 for (auto const &userId : userIdsMsg.foregroundUserIds) {
45 numberObj = cJSON_CreateNumber(userId);
46 if (numberObj == nullptr || !cJSON_AddItemToArray(foregroundUserIdArr, numberObj)) {
47 cJSON_Delete(numberObj);
48 cJSON_Delete(foregroundUserIdArr);
49 return;
50 }
51 }
52 cJSON_AddItemToObject(jsonObject, FOREGROUND_USERIDS_MSG_USERIDS_KEY, foregroundUserIdArr);
53
54 cJSON *backgroundUserIdArr = cJSON_CreateArray();
55 if (backgroundUserIdArr == nullptr) {
56 return;
57 }
58 cJSON *backgroundNumberObj = nullptr;
59 for (auto const &userId : userIdsMsg.backgroundUserIds) {
60 backgroundNumberObj = cJSON_CreateNumber(userId);
61 if (backgroundNumberObj == nullptr || !cJSON_AddItemToArray(backgroundUserIdArr, backgroundNumberObj)) {
62 cJSON_Delete(backgroundNumberObj);
63 cJSON_Delete(backgroundUserIdArr);
64 return;
65 }
66 }
67 cJSON_AddItemToObject(jsonObject, BACKGROUND_USERIDS_MSG_USERIDS_KEY, backgroundUserIdArr);
68 }
69
FromJson(const cJSON * jsonObject,UserIdsMsg & userIdsMsg)70 void FromJson(const cJSON *jsonObject, UserIdsMsg &userIdsMsg)
71 {
72 if (jsonObject == nullptr) {
73 LOGE("Json pointer is nullptr!");
74 return;
75 }
76 cJSON *foregroundUserIdsArr = cJSON_GetObjectItem(jsonObject, FOREGROUND_USERIDS_MSG_USERIDS_KEY);
77 if (cJSON_IsArray(foregroundUserIdsArr)) {
78 int32_t arrSize = cJSON_GetArraySize(foregroundUserIdsArr);
79 if (arrSize > MAX_USER_ID_NUM) {
80 LOGE("Receive too many foreground userids, %{public}d", arrSize);
81 return;
82 }
83 for (int32_t i = 0; i < arrSize; i++) {
84 cJSON *userIdItem = cJSON_GetArrayItem(foregroundUserIdsArr, i);
85 if (cJSON_IsNumber(userIdItem)) {
86 uint32_t userId = static_cast<uint32_t>(userIdItem->valueint);
87 userIdsMsg.foregroundUserIds.push_back(userId);
88 }
89 }
90 }
91
92 cJSON *backgroundUserIdsArr = cJSON_GetObjectItem(jsonObject, BACKGROUND_USERIDS_MSG_USERIDS_KEY);
93 if (cJSON_IsArray(backgroundUserIdsArr)) {
94 int32_t arrSize = cJSON_GetArraySize(backgroundUserIdsArr);
95 if (arrSize > MAX_BACKGROUND_USER_ID_NUM) {
96 LOGE("Receive too many background userids, %{public}d", arrSize);
97 return;
98 }
99 for (int32_t i = 0; i < arrSize; i++) {
100 cJSON *userIdItem = cJSON_GetArrayItem(backgroundUserIdsArr, i);
101 if (cJSON_IsNumber(userIdItem)) {
102 uint32_t userId = static_cast<uint32_t>(userIdItem->valueint);
103 userIdsMsg.backgroundUserIds.push_back(userId);
104 }
105 }
106 }
107 }
108
ToJson(cJSON * jsonObject,const CommMsg & commMsg)109 void ToJson(cJSON *jsonObject, const CommMsg &commMsg)
110 {
111 if (jsonObject == nullptr) {
112 LOGE("Json pointer is nullptr!");
113 return;
114 }
115 cJSON_AddNumberToObject(jsonObject, COMM_MSG_CODE_KEY, commMsg.code);
116 const char *msg = commMsg.msg.c_str();
117 cJSON_AddStringToObject(jsonObject, COMM_MSG_MSG_KEY, msg);
118 }
119
FromJson(const cJSON * jsonObject,CommMsg & commMsg)120 void FromJson(const cJSON *jsonObject, CommMsg &commMsg)
121 {
122 if (jsonObject == nullptr) {
123 LOGE("Json pointer is nullptr!");
124 return;
125 }
126 cJSON *codeObj = cJSON_GetObjectItem(jsonObject, COMM_MSG_CODE_KEY);
127 if (cJSON_IsNumber(codeObj)) {
128 commMsg.code = codeObj->valueint;
129 }
130
131 cJSON *msgObj = cJSON_GetObjectItem(jsonObject, COMM_MSG_MSG_KEY);
132 if (cJSON_IsString(msgObj)) {
133 commMsg.msg = msgObj->valuestring;
134 }
135 }
136
GetCommMsgString(const CommMsg & commMsg)137 std::string GetCommMsgString(const CommMsg &commMsg)
138 {
139 cJSON *rootMsg = cJSON_CreateObject();
140 if (rootMsg == nullptr) {
141 LOGE("Create cJSON object failed.");
142 return "";
143 }
144 ToJson(rootMsg, commMsg);
145 char *msg = cJSON_PrintUnformatted(rootMsg);
146 if (msg == nullptr) {
147 cJSON_Delete(rootMsg);
148 return "";
149 }
150 std::string msgStr = std::string(msg);
151 cJSON_free(msg);
152 cJSON_Delete(rootMsg);
153
154 return msgStr;
155 }
156
ToJson(cJSON * jsonObject,const NotifyUserIds & notifyUserIds)157 void ToJson(cJSON *jsonObject, const NotifyUserIds ¬ifyUserIds)
158 {
159 if (jsonObject == nullptr) {
160 LOGE("Json pointer is nullptr!");
161 return;
162 }
163
164 cJSON_AddStringToObject(jsonObject, DSOFTBUS_NOTIFY_USERIDS_UDIDKEY, notifyUserIds.remoteUdid.c_str());
165
166 cJSON *userIdArr = cJSON_CreateArray();
167 if (userIdArr == nullptr) {
168 return;
169 }
170 cJSON *userIdNumberObj = nullptr;
171 for (auto const &userId : notifyUserIds.userIds) {
172 userIdNumberObj = cJSON_CreateNumber(userId);
173 if (userIdNumberObj == nullptr || !cJSON_AddItemToArray(userIdArr, userIdNumberObj)) {
174 cJSON_Delete(userIdNumberObj);
175 cJSON_Delete(userIdArr);
176 return;
177 }
178 }
179 cJSON_AddItemToObject(jsonObject, DSOFTBUS_NOTIFY_USERIDS_USERIDKEY, userIdArr);
180 }
181
FromJson(const cJSON * jsonObject,NotifyUserIds & notifyUserIds)182 void FromJson(const cJSON *jsonObject, NotifyUserIds ¬ifyUserIds)
183 {
184 if (jsonObject == nullptr) {
185 LOGE("Json pointer is nullptr!");
186 return;
187 }
188
189 cJSON *msgObj = cJSON_GetObjectItem(jsonObject, DSOFTBUS_NOTIFY_USERIDS_UDIDKEY);
190 if (cJSON_IsString(msgObj)) {
191 notifyUserIds.remoteUdid = msgObj->valuestring;
192 }
193
194 cJSON *userIdsArr = cJSON_GetObjectItem(jsonObject, DSOFTBUS_NOTIFY_USERIDS_USERIDKEY);
195 if (cJSON_IsArray(userIdsArr)) {
196 int32_t arrSize = cJSON_GetArraySize(userIdsArr);
197 if (arrSize > MAX_USER_ID_NUM) {
198 LOGE("Receive too many userids, %{public}d", arrSize);
199 return;
200 }
201 for (int32_t i = 0; i < arrSize; i++) {
202 cJSON *userIdItem = cJSON_GetArrayItem(userIdsArr, i);
203 if (cJSON_IsNumber(userIdItem)) {
204 uint32_t userId = static_cast<uint32_t>(userIdItem->valueint);
205 notifyUserIds.userIds.push_back(userId);
206 }
207 }
208 }
209 }
210
ToString()211 std::string NotifyUserIds::ToString()
212 {
213 cJSON *msg = cJSON_CreateObject();
214 if (msg == NULL) {
215 LOGE("failed to create cjson object");
216 return "";
217 }
218
219 ToJson(msg, *this);
220 char *retStr = cJSON_PrintUnformatted(msg);
221 if (retStr == nullptr) {
222 LOGE("to json is nullptr.");
223 cJSON_Delete(msg);
224 return "";
225 }
226 std::string ret = std::string(retStr);
227 cJSON_Delete(msg);
228 cJSON_free(retStr);
229 return ret;
230 }
231
ToJson(cJSON * jsonObject,const LogoutAccountMsg & accountInfo)232 void ToJson(cJSON *jsonObject, const LogoutAccountMsg &accountInfo)
233 {
234 if (jsonObject == nullptr) {
235 LOGE("Json pointer is nullptr!");
236 return;
237 }
238
239 cJSON_AddStringToObject(jsonObject, DSOFTBUS_NOTIFY_ACCOUNTID_KEY, accountInfo.accountId.c_str());
240 cJSON_AddNumberToObject(jsonObject, DSOFTBUS_NOTIFY_USERID_KEY, accountInfo.userId);
241 }
242
FromJson(const cJSON * jsonObject,LogoutAccountMsg & accountInfo)243 void FromJson(const cJSON *jsonObject, LogoutAccountMsg &accountInfo)
244 {
245 if (jsonObject == nullptr) {
246 LOGE("Json pointer is nullptr!");
247 return;
248 }
249 cJSON *accountIdObj = cJSON_GetObjectItem(jsonObject, DSOFTBUS_NOTIFY_ACCOUNTID_KEY);
250 if (cJSON_IsString(accountIdObj)) {
251 accountInfo.accountId = accountIdObj->valuestring;
252 }
253
254 cJSON *userIdObj = cJSON_GetObjectItem(jsonObject, DSOFTBUS_NOTIFY_USERID_KEY);
255 if (cJSON_IsNumber(userIdObj)) {
256 accountInfo.userId = userIdObj->valueint;
257 }
258 }
259 } // DistributedHardware
260 } // OHOS