• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-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 "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 IS_NEW_EVENT_KEY = "isNewEvent";
27 const char* const COMM_MSG_CODE_KEY = "code";
28 const char* const COMM_MSG_MSG_KEY = "msg";
29 const char* const DSOFTBUS_NOTIFY_USERIDS_UDIDKEY = "remoteUdid";
30 const char* const DSOFTBUS_NOTIFY_USERIDS_USERIDKEY = "foregroundUserIds";
31 const char* const DSOFTBUS_NOTIFY_ACCOUNTID_KEY = "accountId";
32 const char* const DSOFTBUS_NOTIFY_USERID_KEY = "userId";
33 const char* const DSOFTBUS_NOTIFY_TOKENID_KEY = "tokenId";
34 const char* const DSOFTBUS_NOTIFY_EXTRA_KEY = "extra";
35 const char* const DSOFTBUS_NOTIFY_UDID_KEY = "udid";
36 }
ToJson(cJSON * jsonObject,const UserIdsMsg & userIdsMsg)37 void ToJson(cJSON *jsonObject, const UserIdsMsg &userIdsMsg)
38 {
39     if (jsonObject == nullptr) {
40         LOGE("Json pointer is nullptr!");
41         return;
42     }
43     cJSON *foregroundUserIdArr = cJSON_CreateArray();
44     if (foregroundUserIdArr == nullptr) {
45         return;
46     }
47     cJSON *numberObj = nullptr;
48     for (auto const &userId : userIdsMsg.foregroundUserIds) {
49         numberObj = cJSON_CreateNumber(userId);
50         if (numberObj == nullptr || !cJSON_AddItemToArray(foregroundUserIdArr, numberObj)) {
51             cJSON_Delete(numberObj);
52             cJSON_Delete(foregroundUserIdArr);
53             return;
54         }
55     }
56     if (!cJSON_AddItemToObject(jsonObject, FOREGROUND_USERIDS_MSG_USERIDS_KEY, foregroundUserIdArr)) {
57         cJSON_Delete(foregroundUserIdArr);
58         return;
59     }
60 
61     cJSON *backgroundUserIdArr = cJSON_CreateArray();
62     if (backgroundUserIdArr == nullptr) {
63         return;
64     }
65     cJSON *backgroundNumberObj = nullptr;
66     for (auto const &userId : userIdsMsg.backgroundUserIds) {
67         backgroundNumberObj = cJSON_CreateNumber(userId);
68         if (backgroundNumberObj == nullptr || !cJSON_AddItemToArray(backgroundUserIdArr, backgroundNumberObj)) {
69             cJSON_Delete(backgroundNumberObj);
70             cJSON_Delete(backgroundUserIdArr);
71             return;
72         }
73     }
74     if (!cJSON_AddItemToObject(jsonObject, BACKGROUND_USERIDS_MSG_USERIDS_KEY, backgroundUserIdArr)) {
75         cJSON_Delete(backgroundUserIdArr);
76         return;
77     }
78     cJSON_AddBoolToObject(jsonObject, IS_NEW_EVENT_KEY, userIdsMsg.isNewEvent);
79 }
80 
FromJson(const cJSON * jsonObject,UserIdsMsg & userIdsMsg)81 void FromJson(const cJSON *jsonObject, UserIdsMsg &userIdsMsg)
82 {
83     if (jsonObject == nullptr) {
84         LOGE("Json pointer is nullptr!");
85         return;
86     }
87     cJSON *foregroundUserIdsArr = cJSON_GetObjectItem(jsonObject, FOREGROUND_USERIDS_MSG_USERIDS_KEY);
88     if (cJSON_IsArray(foregroundUserIdsArr)) {
89         int32_t arrSize = cJSON_GetArraySize(foregroundUserIdsArr);
90         if (arrSize > MAX_USER_ID_NUM) {
91             LOGE("Receive too many foreground userids, %{public}d", arrSize);
92             return;
93         }
94         for (int32_t i = 0; i < arrSize; i++) {
95             cJSON *userIdItem = cJSON_GetArrayItem(foregroundUserIdsArr, i);
96             if (cJSON_IsNumber(userIdItem)) {
97                 uint32_t userId = static_cast<uint32_t>(userIdItem->valueint);
98                 userIdsMsg.foregroundUserIds.push_back(userId);
99             }
100         }
101     }
102 
103     cJSON *backgroundUserIdsArr = cJSON_GetObjectItem(jsonObject, BACKGROUND_USERIDS_MSG_USERIDS_KEY);
104     if (cJSON_IsArray(backgroundUserIdsArr)) {
105         int32_t arrSize = cJSON_GetArraySize(backgroundUserIdsArr);
106         if (arrSize > MAX_BACKGROUND_USER_ID_NUM) {
107             LOGE("Receive too many background userids, %{public}d", arrSize);
108             return;
109         }
110         for (int32_t i = 0; i < arrSize; i++) {
111             cJSON *userIdItem = cJSON_GetArrayItem(backgroundUserIdsArr, i);
112             if (cJSON_IsNumber(userIdItem)) {
113                 uint32_t userId = static_cast<uint32_t>(userIdItem->valueint);
114                 userIdsMsg.backgroundUserIds.push_back(userId);
115             }
116         }
117     }
118     cJSON *isNewEventJson = cJSON_GetObjectItem(jsonObject, IS_NEW_EVENT_KEY);
119     if (cJSON_IsBool(isNewEventJson)) {
120         userIdsMsg.isNewEvent = (isNewEventJson->valueint != 0);
121     }
122 }
123 
ToJson(cJSON * jsonObject,const CommMsg & commMsg)124 void ToJson(cJSON *jsonObject, const CommMsg &commMsg)
125 {
126     if (jsonObject == nullptr) {
127         LOGE("Json pointer is nullptr!");
128         return;
129     }
130     cJSON_AddNumberToObject(jsonObject, COMM_MSG_CODE_KEY, commMsg.code);
131     const char *msg = commMsg.msg.c_str();
132     cJSON_AddStringToObject(jsonObject, COMM_MSG_MSG_KEY, msg);
133 }
134 
FromJson(const cJSON * jsonObject,CommMsg & commMsg)135 void FromJson(const cJSON *jsonObject, CommMsg &commMsg)
136 {
137     if (jsonObject == nullptr) {
138         LOGE("Json pointer is nullptr!");
139         return;
140     }
141     cJSON *codeObj = cJSON_GetObjectItem(jsonObject, COMM_MSG_CODE_KEY);
142     if (cJSON_IsNumber(codeObj)) {
143         commMsg.code = codeObj->valueint;
144     }
145 
146     cJSON *msgObj = cJSON_GetObjectItem(jsonObject, COMM_MSG_MSG_KEY);
147     if (cJSON_IsString(msgObj)) {
148         commMsg.msg = msgObj->valuestring;
149     }
150 }
151 
GetCommMsgString(const CommMsg & commMsg)152 std::string GetCommMsgString(const CommMsg &commMsg)
153 {
154     cJSON *rootMsg = cJSON_CreateObject();
155     if (rootMsg == nullptr) {
156         LOGE("Create cJSON object failed.");
157         return "";
158     }
159     ToJson(rootMsg, commMsg);
160     char *msg = cJSON_PrintUnformatted(rootMsg);
161     if (msg == nullptr) {
162         cJSON_Delete(rootMsg);
163         return "";
164     }
165     std::string msgStr = std::string(msg);
166     cJSON_free(msg);
167     cJSON_Delete(rootMsg);
168 
169     return msgStr;
170 }
171 
ToJson(cJSON * jsonObject,const NotifyUserIds & notifyUserIds)172 void ToJson(cJSON *jsonObject, const NotifyUserIds &notifyUserIds)
173 {
174     if (jsonObject == nullptr) {
175         LOGE("Json pointer is nullptr!");
176         return;
177     }
178 
179     cJSON_AddStringToObject(jsonObject, DSOFTBUS_NOTIFY_USERIDS_UDIDKEY, notifyUserIds.remoteUdid.c_str());
180 
181     cJSON *userIdArr = cJSON_CreateArray();
182     if (userIdArr == nullptr) {
183         return;
184     }
185     cJSON *userIdNumberObj = nullptr;
186     for (auto const &userId : notifyUserIds.userIds) {
187         userIdNumberObj = cJSON_CreateNumber(userId);
188         if (userIdNumberObj == nullptr || !cJSON_AddItemToArray(userIdArr, userIdNumberObj)) {
189             cJSON_Delete(userIdNumberObj);
190             cJSON_Delete(userIdArr);
191             return;
192         }
193     }
194     if (!cJSON_AddItemToObject(jsonObject, DSOFTBUS_NOTIFY_USERIDS_USERIDKEY, userIdArr)) {
195         cJSON_Delete(userIdArr);
196         return;
197     }
198 }
199 
FromJson(const cJSON * jsonObject,NotifyUserIds & notifyUserIds)200 void FromJson(const cJSON *jsonObject, NotifyUserIds &notifyUserIds)
201 {
202     if (jsonObject == nullptr) {
203         LOGE("Json pointer is nullptr!");
204         return;
205     }
206 
207     cJSON *msgObj = cJSON_GetObjectItem(jsonObject, DSOFTBUS_NOTIFY_USERIDS_UDIDKEY);
208     if (cJSON_IsString(msgObj)) {
209         notifyUserIds.remoteUdid = msgObj->valuestring;
210     }
211 
212     cJSON *userIdsArr = cJSON_GetObjectItem(jsonObject, DSOFTBUS_NOTIFY_USERIDS_USERIDKEY);
213     if (cJSON_IsArray(userIdsArr)) {
214         int32_t arrSize = cJSON_GetArraySize(userIdsArr);
215         if (arrSize > MAX_USER_ID_NUM) {
216             LOGE("Receive too many userids, %{public}d", arrSize);
217             return;
218         }
219         for (int32_t i = 0; i < arrSize; i++) {
220             cJSON *userIdItem = cJSON_GetArrayItem(userIdsArr, i);
221             if (cJSON_IsNumber(userIdItem)) {
222                 uint32_t userId = static_cast<uint32_t>(userIdItem->valueint);
223                 notifyUserIds.userIds.push_back(userId);
224             }
225         }
226     }
227 }
228 
ToString()229 std::string NotifyUserIds::ToString()
230 {
231     cJSON *msg = cJSON_CreateObject();
232     if (msg == NULL) {
233         LOGE("failed to create cjson object");
234         return "";
235     }
236 
237     ToJson(msg, *this);
238     char *retStr = cJSON_PrintUnformatted(msg);
239     if (retStr == nullptr) {
240         LOGE("to json is nullptr.");
241         cJSON_Delete(msg);
242         return "";
243     }
244     std::string ret = std::string(retStr);
245     cJSON_Delete(msg);
246     cJSON_free(retStr);
247     return ret;
248 }
249 
ToJson(cJSON * jsonObject,const LogoutAccountMsg & accountInfo)250 void ToJson(cJSON *jsonObject, const LogoutAccountMsg &accountInfo)
251 {
252     if (jsonObject == nullptr) {
253         LOGE("Json pointer is nullptr!");
254         return;
255     }
256 
257     cJSON_AddStringToObject(jsonObject, DSOFTBUS_NOTIFY_ACCOUNTID_KEY, accountInfo.accountId.c_str());
258     cJSON_AddNumberToObject(jsonObject, DSOFTBUS_NOTIFY_USERID_KEY, accountInfo.userId);
259 }
260 
FromJson(const cJSON * jsonObject,LogoutAccountMsg & accountInfo)261 void FromJson(const cJSON *jsonObject, LogoutAccountMsg &accountInfo)
262 {
263     if (jsonObject == nullptr) {
264         LOGE("Json pointer is nullptr!");
265         return;
266     }
267     cJSON *accountIdObj = cJSON_GetObjectItem(jsonObject, DSOFTBUS_NOTIFY_ACCOUNTID_KEY);
268     if (cJSON_IsString(accountIdObj)) {
269         accountInfo.accountId = accountIdObj->valuestring;
270     }
271 
272     cJSON *userIdObj = cJSON_GetObjectItem(jsonObject, DSOFTBUS_NOTIFY_USERID_KEY);
273     if (cJSON_IsNumber(userIdObj)) {
274         accountInfo.userId = userIdObj->valueint;
275     }
276 }
277 
ToJson(cJSON * jsonObject,const UninstAppMsg & uninstAppMsg)278 void ToJson(cJSON *jsonObject, const UninstAppMsg &uninstAppMsg)
279 {
280     if (jsonObject == nullptr) {
281         LOGE("Json pointer is nullptr!");
282         return;
283     }
284 
285     cJSON_AddNumberToObject(jsonObject, DSOFTBUS_NOTIFY_USERID_KEY, uninstAppMsg.userId_);
286     cJSON_AddNumberToObject(jsonObject, DSOFTBUS_NOTIFY_TOKENID_KEY, uninstAppMsg.tokenId_);
287 }
288 
FromJson(const cJSON * jsonObject,UninstAppMsg & uninstAppMsg)289 void FromJson(const cJSON *jsonObject, UninstAppMsg &uninstAppMsg)
290 {
291     if (jsonObject == nullptr) {
292         LOGE("Json pointer is nullptr!");
293         return;
294     }
295 
296     cJSON *userIdObj = cJSON_GetObjectItem(jsonObject, DSOFTBUS_NOTIFY_USERID_KEY);
297     cJSON *tokenIdObj = cJSON_GetObjectItem(jsonObject, DSOFTBUS_NOTIFY_TOKENID_KEY);
298     if (userIdObj == nullptr || tokenIdObj == nullptr) {
299         LOGE("userIdObj or tokenIdObj is nullptr");
300         return;
301     }
302     if (cJSON_IsNumber(userIdObj)) {
303         uninstAppMsg.userId_ = userIdObj->valueint;
304     }
305     if (cJSON_IsNumber(tokenIdObj)) {
306         uninstAppMsg.tokenId_ = tokenIdObj->valueint;
307     }
308 }
309 
ToJson(cJSON * jsonObject,const UnBindAppMsg & unBindAppMsg)310 void ToJson(cJSON *jsonObject, const UnBindAppMsg &unBindAppMsg)
311 {
312     if (jsonObject == nullptr) {
313         LOGE("Json pointer is nullptr!");
314         return;
315     }
316 
317     cJSON_AddNumberToObject(jsonObject, DSOFTBUS_NOTIFY_USERID_KEY, unBindAppMsg.userId_);
318     cJSON_AddNumberToObject(jsonObject, DSOFTBUS_NOTIFY_TOKENID_KEY, unBindAppMsg.tokenId_);
319     cJSON_AddStringToObject(jsonObject, DSOFTBUS_NOTIFY_EXTRA_KEY, unBindAppMsg.extra_.c_str());
320     cJSON_AddStringToObject(jsonObject, DSOFTBUS_NOTIFY_UDID_KEY, unBindAppMsg.udid_.c_str());
321 }
322 
FromJson(const cJSON * jsonObject,UnBindAppMsg & unBindAppMsg)323 void FromJson(const cJSON *jsonObject, UnBindAppMsg &unBindAppMsg)
324 {
325     if (jsonObject == nullptr) {
326         LOGE("Json pointer is nullptr!");
327         return;
328     }
329 
330     cJSON *userIdObj = cJSON_GetObjectItem(jsonObject, DSOFTBUS_NOTIFY_USERID_KEY);
331     cJSON *tokenIdObj = cJSON_GetObjectItem(jsonObject, DSOFTBUS_NOTIFY_TOKENID_KEY);
332     cJSON *extraObj = cJSON_GetObjectItem(jsonObject, DSOFTBUS_NOTIFY_EXTRA_KEY);
333     cJSON *udidObj = cJSON_GetObjectItem(jsonObject, DSOFTBUS_NOTIFY_UDID_KEY);
334     if (userIdObj == nullptr || tokenIdObj == nullptr || extraObj == nullptr || udidObj == nullptr) {
335         LOGE("userIdObj or tokenIdObj or extraObj or udidObj is nullptr");
336         return;
337     }
338     if (cJSON_IsNumber(userIdObj)) {
339         unBindAppMsg.userId_ = userIdObj->valueint;
340     }
341     if (cJSON_IsNumber(tokenIdObj)) {
342         unBindAppMsg.tokenId_ = tokenIdObj->valueint;
343     }
344     if (cJSON_IsString(extraObj)) {
345         unBindAppMsg.extra_ = extraObj->valuestring;
346     }
347     if (cJSON_IsString(udidObj)) {
348         unBindAppMsg.udid_ = udidObj->valuestring;
349     }
350 }
351 } // DistributedHardware
352 } // OHOS