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 "relationship_sync_mgr.h"
17
18 #include <ctime>
19 #include <sstream>
20
21 #include "dm_anonymous.h"
22 #include "dm_constants.h"
23 #include "dm_log.h"
24
25 namespace OHOS {
26 namespace DistributedHardware {
27 DM_IMPLEMENT_SINGLE_INSTANCE(ReleationShipSyncMgr);
28 namespace {
29 /**
30 * @brief account logout payload length 9 bytes
31 * | 2 bytes | 6 bytes | 1 bytes |
32 * | userid lower 2 bytes | account id first 6 bytes | broadcastId |
33 */
34 const int32_t ACCOUNT_LOGOUT_PAYLOAD_LEN = 9;
35 /**
36 * @brief device unbind payload length 2 bytes
37 * | 2 bytes | 1 bytes |
38 * | userid lower 2 bytes | broadcastId |
39 */
40 const int32_t DEVICE_UNBIND_PAYLOAD_LEN = 3;
41 /**
42 * @brief app unbind payload length 6 bytes
43 * | 2 bytes | 4 bytes | 4 bytes | 1 bytes |
44 * | userid lower 2 bytes | token id lower 4 bytes | peertoken id lower 4 bytes | broadcastId |
45 */
46 const int32_t APP_UNBIND_PAYLOAD_LEN = 11;
47 /**
48 * @brief delete user payload length 2 bytes
49 * | 2 bytes | 1 bytes |
50 * | userid lower 2 bytes | broadcastId |
51 */
52 const int32_t DEL_USER_PAYLOAD_LEN = 3;
53 const int32_t STOP_USER_PAYLOAD_LEN = 3;
54 const int32_t SHARE_UNBIND_PAYLOAD_LEN = 9;
55 const int32_t SHARE_UNBIND_BROADCAST_LEN = 8;
56 const int32_t APP_UNINSTALL_PAYLOAD_LEN = 7;
57 /**
58 * @brief the userid payload cost 2 bytes.
59 *
60 */
61 const int32_t USERID_PAYLOAD_LEN = 2;
62 const int32_t TOKENID_PAYLOAD_LEN = 6;
63 const int32_t ACCOUNTID_PAYLOAD_LEN = 6;
64 const int32_t SYNC_FRONT_OR_BACK_USERID_PAYLOAD_MAX_LEN = 11;
65 const int32_t SYNC_FRONT_OR_BACK_USERID_PAYLOAD_MIN_LEN = 3;
66 const int32_t USERID_BYTES = 2;
67 const int32_t BITS_PER_BYTE = 8;
68 const int32_t INVALIED_PAYLOAD_SIZE = 12;
69 const int32_t CREDID_PAYLOAD_LEN = 8;
70 const int32_t ACCOUNT_LOGOUT_BROADCAST_LEN = 8;
71 const int32_t BROADCAST_PAYLOAD_LEN = 10;
72 const int32_t BROADCAST_TIMEOUT_S = 5;
73 const int32_t CURRENT_TIME_SEC_FLAG = 10;
74 const int32_t GET_CURRENT_TIME_MAX_NUM = 3;
75
76 const char * const MSG_TYPE = "TYPE";
77 const char * const MSG_VALUE = "VALUE";
78 const char * const MSG_PEER_UDID = "PEER_UDID";
79 const char * const MSG_ACCOUNTID = "ACCOUNTID";
80 const char * const MSG_PEER_TOKENID = "PEER_TOKENID";
81
82 // The need response mask offset, the 8th bit.
83 const int32_t NEED_RSP_MASK_OFFSET = 7;
84 const int32_t IS_NEW_USER_SYNC_MASK_OFFSET = 6;
85 /**
86 * @brief The userid cost 2 byte, the heigher part set on the 2th byte.
87 * The Max user id is just above 10000+, cost 15bits. We use the first bit
88 * to mark the user id foreground or background.
89 * 1 means foreground user, 0 means background user.
90 * @example foreground userid: 100 -> 0000 0000 0110 0100
91 * we save it in payload:
92 * |----first byte----|----second byte----|
93 * ----------------------------------------
94 * | 0110 0100 | 1000 0000 |
95 */
96 const int32_t FRONT_OR_BACK_USER_FLAG_OFFSET = 7;
97 const uint8_t FRONT_OR_BACK_USER_FLAG_MASK = 0b01111111;
98 const uint8_t FRONT_OR_BACK_FLAG_MASK = 0b10000000;
99 // The total number of foreground and background userids offset, the 3th ~ 5th bits.
100 const uint16_t ALL_USERID_NUM_MASK_OFFSET = 3;
101 const uint16_t FOREGROUND_USERID_LEN_MASK = 0b00000111;
102 const uint16_t ALL_USERID_NUM_MASK = 0b00111000;
103 const uint32_t MAX_MEM_MALLOC_SIZE = 4 * 1024;
104 const uint32_t MAX_USER_ID_NUM = 4;
105 }
106
RelationShipChangeMsg()107 RelationShipChangeMsg::RelationShipChangeMsg() : type(RelationShipChangeType::TYPE_MAX),
108 userId(UINT32_MAX), accountId(""), tokenId(UINT64_MAX), peerUdids({}), peerUdid(""), accountName(""),
109 syncUserIdFlag(false), userIdInfos({}), isNewEvent(false), broadCastId(UINT8_MAX)
110 {
111 }
112
ToBroadcastPayLoad(uint8_t * & msg,uint32_t & len) const113 bool RelationShipChangeMsg::ToBroadcastPayLoad(uint8_t *&msg, uint32_t &len) const
114 {
115 if (!IsValid()) {
116 LOGE("invalid");
117 return false;
118 }
119
120 bool ret = false;
121 switch (type) {
122 case RelationShipChangeType::ACCOUNT_LOGOUT:
123 ToAccountLogoutPayLoad(msg, len);
124 ret = true;
125 break;
126 case RelationShipChangeType::DEVICE_UNBIND:
127 ToDeviceUnbindPayLoad(msg, len);
128 ret = true;
129 break;
130 case RelationShipChangeType::APP_UNBIND:
131 ToAppUnbindPayLoad(msg, len);
132 ret = true;
133 break;
134 case RelationShipChangeType::SERVICE_UNBIND:
135 ToServiceUnbindPayLoad(msg, len);
136 ret = true;
137 break;
138 case RelationShipChangeType::SYNC_USERID:
139 ret = ToSyncFrontOrBackUserIdPayLoad(msg, len);
140 break;
141 case RelationShipChangeType::DEL_USER:
142 ToDelUserPayLoad(msg, len);
143 ret = true;
144 break;
145 case RelationShipChangeType::STOP_USER:
146 ToStopUserPayLoad(msg, len);
147 ret = true;
148 break;
149 case RelationShipChangeType::SHARE_UNBIND:
150 ToShareUnbindPayLoad(msg, len);
151 ret = true;
152 break;
153 case RelationShipChangeType::APP_UNINSTALL:
154 ToAppUninstallPayLoad(msg, len);
155 ret = true;
156 break;
157 default:
158 LOGE("RelationShipChange type invalid");
159 break;
160 }
161 return ret;
162 }
163
ToShareUnbindPayLoad(uint8_t * & msg,uint32_t & len) const164 void RelationShipChangeMsg::ToShareUnbindPayLoad(uint8_t *&msg, uint32_t &len) const
165 {
166 if (credId.length() <= (CREDID_PAYLOAD_LEN - USERID_PAYLOAD_LEN)) {
167 LOGE("ToShareUnbindPayLoad credId length is invalid.");
168 len = 0;
169 return;
170 }
171 msg = new uint8_t[SHARE_UNBIND_PAYLOAD_LEN]();
172 for (int i = 0; i < USERID_PAYLOAD_LEN; i++) {
173 msg[i] |= (userId >> (i * BITS_PER_BYTE)) & 0xFF;
174 }
175
176 for (int i = USERID_PAYLOAD_LEN; i < CREDID_PAYLOAD_LEN; i++) {
177 msg[i] = credId[i - USERID_PAYLOAD_LEN];
178 }
179 for (int i = CREDID_PAYLOAD_LEN; i < SHARE_UNBIND_PAYLOAD_LEN; i++) {
180 msg[i] |= (broadCastId >> ((i - CREDID_PAYLOAD_LEN) * BITS_PER_BYTE)) & 0xFF;
181 }
182 len = SHARE_UNBIND_PAYLOAD_LEN;
183 }
184
FromBroadcastPayLoad(const cJSON * payloadJson,RelationShipChangeType type)185 bool RelationShipChangeMsg::FromBroadcastPayLoad(const cJSON *payloadJson, RelationShipChangeType type)
186 {
187 LOGI("FromBroadcastPayLoad type %{public}d.", type);
188 if (type == RelationShipChangeType::TYPE_MAX) {
189 LOGE("ChangeType invalid, type: %{public}d", type);
190 return false;
191 }
192 bool ret = false;
193 switch (type) {
194 case RelationShipChangeType::ACCOUNT_LOGOUT:
195 ret = FromAccountLogoutPayLoad(payloadJson);
196 break;
197 case RelationShipChangeType::DEVICE_UNBIND:
198 ret = FromDeviceUnbindPayLoad(payloadJson);
199 break;
200 case RelationShipChangeType::APP_UNBIND:
201 ret = FromAppUnbindPayLoad(payloadJson);
202 break;
203 case RelationShipChangeType::SERVICE_UNBIND:
204 ret = FromServiceUnbindPayLoad(payloadJson);
205 break;
206 case RelationShipChangeType::SYNC_USERID:
207 ret = FromSyncFrontOrBackUserIdPayLoad(payloadJson);
208 break;
209 case RelationShipChangeType::DEL_USER:
210 ret = FromDelUserPayLoad(payloadJson);
211 break;
212 case RelationShipChangeType::STOP_USER:
213 ret = FromStopUserPayLoad(payloadJson);
214 break;
215 case RelationShipChangeType::SHARE_UNBIND:
216 ret = FromShareUnbindPayLoad(payloadJson);
217 break;
218 case RelationShipChangeType::APP_UNINSTALL:
219 ret = FromAppUninstallPayLoad(payloadJson);
220 break;
221 default:
222 LOGE("RelationShipChange type invalid");
223 break;
224 }
225 return ret;
226 }
227
FromShareUnbindPayLoad(const cJSON * payloadJson)228 bool RelationShipChangeMsg::FromShareUnbindPayLoad(const cJSON *payloadJson)
229 {
230 if (payloadJson == NULL) {
231 LOGE("Share unbind payloadJson is null.");
232 return false;
233 }
234 int32_t arraySize = cJSON_GetArraySize(payloadJson);
235 if (arraySize < SHARE_UNBIND_PAYLOAD_LEN || arraySize >= INVALIED_PAYLOAD_SIZE) {
236 LOGE("Payload invalied,the size is %{public}d.", arraySize);
237 return false;
238 }
239 userId = 0;
240 for (uint32_t i = 0; i < USERID_PAYLOAD_LEN; i++) {
241 cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, i);
242 CHECK_NULL_RETURN(payloadItem, false);
243 if (cJSON_IsNumber(payloadItem)) {
244 userId |= (static_cast<uint8_t>(payloadItem->valueint)) << (i * BITS_PER_BYTE);
245 }
246 }
247 credId = "";
248 for (uint32_t j = USERID_PAYLOAD_LEN; j < CREDID_PAYLOAD_LEN; j++) {
249 cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, j);
250 CHECK_NULL_RETURN(payloadItem, false);
251 if (cJSON_IsNumber(payloadItem)) {
252 credId += static_cast<char>(payloadItem->valueint);
253 }
254 }
255 this->broadCastId = 0;
256 for (uint32_t j = CREDID_PAYLOAD_LEN; j < SHARE_UNBIND_PAYLOAD_LEN; j++) {
257 cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, j);
258 CHECK_NULL_RETURN(payloadItem, false);
259 if (cJSON_IsNumber(payloadItem)) {
260 this->broadCastId |= (static_cast<uint8_t>(payloadItem->valueint)) <<
261 ((j - CREDID_PAYLOAD_LEN) * BITS_PER_BYTE);
262 }
263 }
264 return true;
265 }
266
IsValid() const267 bool RelationShipChangeMsg::IsValid() const
268 {
269 bool ret = false;
270 switch (type) {
271 case RelationShipChangeType::ACCOUNT_LOGOUT:
272 ret = (userId != UINT32_MAX && accountId.length() >= ACCOUNTID_PAYLOAD_LEN);
273 break;
274 case RelationShipChangeType::DEVICE_UNBIND:
275 ret = (userId != UINT32_MAX);
276 break;
277 case RelationShipChangeType::APP_UNBIND:
278 ret = (userId != UINT32_MAX && tokenId != UINT64_MAX);
279 break;
280 case RelationShipChangeType::DEL_USER:
281 ret = (userId != UINT32_MAX);
282 break;
283 case RelationShipChangeType::STOP_USER:
284 ret = (userId != UINT32_MAX);
285 break;
286 case RelationShipChangeType::SHARE_UNBIND:
287 ret = (userId != UINT32_MAX);
288 break;
289 case RelationShipChangeType::SERVICE_UNBIND:
290 ret = (userId != UINT32_MAX);
291 break;
292 case RelationShipChangeType::APP_UNINSTALL:
293 ret = (userId != UINT32_MAX && tokenId != UINT64_MAX);
294 break;
295 case RelationShipChangeType::SYNC_USERID:
296 ret = (!userIdInfos.empty() &&
297 (static_cast<uint32_t>(userIdInfos.size()) <= MAX_USER_ID_NUM));
298 break;
299 case RelationShipChangeType::TYPE_MAX:
300 ret = false;
301 break;
302 default:
303 ret = false;
304 break;
305 }
306 return ret;
307 }
308
IsChangeTypeValid()309 bool RelationShipChangeMsg::IsChangeTypeValid()
310 {
311 return (type == RelationShipChangeType::ACCOUNT_LOGOUT) || (type == RelationShipChangeType::DEVICE_UNBIND) ||
312 (type == RelationShipChangeType::APP_UNBIND) || (type == RelationShipChangeType::SYNC_USERID) ||
313 (type == RelationShipChangeType::DEL_USER) || (type == RelationShipChangeType::STOP_USER) ||
314 (type == RelationShipChangeType::SERVICE_UNBIND) || (type == RelationShipChangeType::APP_UNINSTALL);
315 }
316
IsChangeTypeValid(uint32_t type)317 bool RelationShipChangeMsg::IsChangeTypeValid(uint32_t type)
318 {
319 return (type == (uint32_t)RelationShipChangeType::ACCOUNT_LOGOUT) ||
320 (type == (uint32_t)RelationShipChangeType::DEVICE_UNBIND) ||
321 (type == (uint32_t)RelationShipChangeType::APP_UNBIND) ||
322 (type == (uint32_t)RelationShipChangeType::SYNC_USERID) ||
323 (type == (uint32_t)RelationShipChangeType::DEL_USER) ||
324 (type == (uint32_t)RelationShipChangeType::STOP_USER) ||
325 (type == (uint32_t)RelationShipChangeType::SHARE_UNBIND) ||
326 (type == (uint32_t)RelationShipChangeType::SERVICE_UNBIND) ||
327 (type == (uint32_t)RelationShipChangeType::APP_UNINSTALL);
328 }
329
ToAccountLogoutPayLoad(uint8_t * & msg,uint32_t & len) const330 void RelationShipChangeMsg::ToAccountLogoutPayLoad(uint8_t *&msg, uint32_t &len) const
331 {
332 msg = new uint8_t[ACCOUNT_LOGOUT_PAYLOAD_LEN]();
333 for (int i = 0; i < USERID_PAYLOAD_LEN; i++) {
334 msg[i] |= (userId >> (i * BITS_PER_BYTE)) & 0xFF;
335 }
336
337 for (int j = USERID_PAYLOAD_LEN; j < ACCOUNT_LOGOUT_BROADCAST_LEN; j++) {
338 msg[j] = accountId[j - USERID_PAYLOAD_LEN];
339 }
340
341 for (int j = ACCOUNT_LOGOUT_BROADCAST_LEN; j < ACCOUNT_LOGOUT_PAYLOAD_LEN; j++) {
342 msg[j] |= (broadCastId >> ((j - ACCOUNT_LOGOUT_BROADCAST_LEN) * BITS_PER_BYTE)) & 0xFF;
343 }
344 len = ACCOUNT_LOGOUT_PAYLOAD_LEN;
345 }
346
ToDeviceUnbindPayLoad(uint8_t * & msg,uint32_t & len) const347 void RelationShipChangeMsg::ToDeviceUnbindPayLoad(uint8_t *&msg, uint32_t &len) const
348 {
349 msg = new uint8_t[DEVICE_UNBIND_PAYLOAD_LEN]();
350 for (int i = 0; i < USERID_PAYLOAD_LEN; i++) {
351 msg[i] |= (userId >> (i * BITS_PER_BYTE)) & 0xFF;
352 }
353 for (int i = USERID_PAYLOAD_LEN; i < DEVICE_UNBIND_PAYLOAD_LEN; i++) {
354 msg[i] |= (broadCastId >> ((i - USERID_PAYLOAD_LEN) * BITS_PER_BYTE)) & 0xFF;
355 }
356 len = DEVICE_UNBIND_PAYLOAD_LEN;
357 }
358
ToAppUnbindPayLoad(uint8_t * & msg,uint32_t & len) const359 void RelationShipChangeMsg::ToAppUnbindPayLoad(uint8_t *&msg, uint32_t &len) const
360 {
361 msg = new uint8_t[APP_UNBIND_PAYLOAD_LEN]();
362 for (int i = 0; i < USERID_PAYLOAD_LEN; i++) {
363 msg[i] |= (userId >> (i * BITS_PER_BYTE)) & 0xFF;
364 }
365
366 for (int i = USERID_PAYLOAD_LEN; i < TOKENID_PAYLOAD_LEN; i++) {
367 msg[i] |= (tokenId >> ((i - USERID_PAYLOAD_LEN) * BITS_PER_BYTE)) & 0xFF;
368 }
369
370 for (int i = TOKENID_PAYLOAD_LEN; i < BROADCAST_PAYLOAD_LEN; i++) {
371 msg[i] |= (peerTokenId >> ((i - TOKENID_PAYLOAD_LEN) * BITS_PER_BYTE)) & 0xFF;
372 }
373
374 for (int i = BROADCAST_PAYLOAD_LEN; i < APP_UNBIND_PAYLOAD_LEN; i++) {
375 msg[i] |= (broadCastId >> ((i - BROADCAST_PAYLOAD_LEN) * BITS_PER_BYTE)) & 0xFF;
376 }
377
378 len = APP_UNBIND_PAYLOAD_LEN;
379 }
380
ToAppUninstallPayLoad(uint8_t * & msg,uint32_t & len) const381 void RelationShipChangeMsg::ToAppUninstallPayLoad(uint8_t *&msg, uint32_t &len) const
382 {
383 msg = new uint8_t[APP_UNINSTALL_PAYLOAD_LEN]();
384 for (int i = 0; i < USERID_PAYLOAD_LEN; i++) {
385 msg[i] |= (userId >> (i * BITS_PER_BYTE)) & 0xFF;
386 }
387
388 for (int i = USERID_PAYLOAD_LEN; i < TOKENID_PAYLOAD_LEN; i++) {
389 msg[i] |= (tokenId >> ((i - USERID_PAYLOAD_LEN) * BITS_PER_BYTE)) & 0xFF;
390 }
391
392 for (int i = TOKENID_PAYLOAD_LEN; i < APP_UNINSTALL_PAYLOAD_LEN; i++) {
393 msg[i] |= (broadCastId >> ((i - TOKENID_PAYLOAD_LEN) * BITS_PER_BYTE)) & 0xFF;
394 }
395 len = APP_UNINSTALL_PAYLOAD_LEN;
396 }
397
ToServiceUnbindPayLoad(uint8_t * & msg,uint32_t & len) const398 void RelationShipChangeMsg::ToServiceUnbindPayLoad(uint8_t *&msg, uint32_t &len) const
399 {
400 ToAppUnbindPayLoad(msg, len);
401 }
402
ToSyncFrontOrBackUserIdPayLoad(uint8_t * & msg,uint32_t & len) const403 bool RelationShipChangeMsg::ToSyncFrontOrBackUserIdPayLoad(uint8_t *&msg, uint32_t &len) const
404 {
405 uint32_t userIdNum = static_cast<uint32_t>(userIdInfos.size());
406 if (userIdNum > MAX_USER_ID_NUM) {
407 LOGE("userIdNum too many, %{public}u", userIdNum);
408 return false;
409 }
410
411 len = (userIdNum + 1) * USERID_BYTES;
412
413 if (len > INVALIED_PAYLOAD_SIZE) {
414 LOGE("len too long");
415 return false;
416 }
417 msg = new uint8_t[len]();
418 if (syncUserIdFlag) {
419 msg[0] |= 0x1 << NEED_RSP_MASK_OFFSET;
420 } else {
421 msg[0] |= 0x0 << NEED_RSP_MASK_OFFSET;
422 }
423 if (isNewEvent) {
424 msg[0] |= 0x1 << IS_NEW_USER_SYNC_MASK_OFFSET;
425 }
426
427 msg[0] |= userIdNum;
428 int32_t userIdIdx = 0;
429 for (uint32_t idx = 1; idx <= (len - USERID_BYTES);) {
430 msg[idx] |= userIdInfos[userIdIdx].userId & 0xFF;
431 msg[idx + 1] |= (userIdInfos[userIdIdx].userId >> BITS_PER_BYTE) & 0xFF;
432 if (userIdInfos[userIdIdx].isForeground) {
433 msg[idx + 1] |= (0x1 << FRONT_OR_BACK_USER_FLAG_OFFSET);
434 }
435 idx += USERID_BYTES;
436 userIdIdx++;
437 }
438 msg[userIdNum * USERID_BYTES + 1] |= broadCastId & 0xFF;
439 return true;
440 }
441
ToDelUserPayLoad(uint8_t * & msg,uint32_t & len) const442 void RelationShipChangeMsg::ToDelUserPayLoad(uint8_t *&msg, uint32_t &len) const
443 {
444 len = DEL_USER_PAYLOAD_LEN;
445 msg = new uint8_t[DEL_USER_PAYLOAD_LEN]();
446 for (int i = 0; i < USERID_PAYLOAD_LEN; i++) {
447 msg[i] |= (userId >> (i * BITS_PER_BYTE)) & 0xFF;
448 }
449 for (int i = USERID_PAYLOAD_LEN; i < DEL_USER_PAYLOAD_LEN; i++) {
450 msg[i] |= (broadCastId >> ((i - USERID_PAYLOAD_LEN) * BITS_PER_BYTE)) & 0xFF;
451 }
452 }
453
ToStopUserPayLoad(uint8_t * & msg,uint32_t & len) const454 void RelationShipChangeMsg::ToStopUserPayLoad(uint8_t *&msg, uint32_t &len) const
455 {
456 len = STOP_USER_PAYLOAD_LEN;
457 msg = new uint8_t[STOP_USER_PAYLOAD_LEN]();
458 for (int i = 0; i < USERID_PAYLOAD_LEN; i++) {
459 msg[i] |= (userId >> (i * BITS_PER_BYTE)) & 0xFF;
460 }
461 for (int i = USERID_PAYLOAD_LEN; i < STOP_USER_PAYLOAD_LEN; i++) {
462 msg[i] |= (broadCastId >> ((i - USERID_PAYLOAD_LEN) * BITS_PER_BYTE)) & 0xFF;
463 }
464 }
465
FromAccountLogoutPayLoad(const cJSON * payloadJson)466 bool RelationShipChangeMsg::FromAccountLogoutPayLoad(const cJSON *payloadJson)
467 {
468 if (payloadJson == NULL) {
469 LOGE("Account logout payloadJson is null.");
470 return false;
471 }
472 int32_t arraySize = cJSON_GetArraySize(payloadJson);
473 if (arraySize < ACCOUNT_LOGOUT_PAYLOAD_LEN || arraySize >= INVALIED_PAYLOAD_SIZE) {
474 LOGE("Payload invalied,the size is %{public}d.", arraySize);
475 return false;
476 }
477 userId = 0;
478 for (uint32_t i = 0; i < USERID_PAYLOAD_LEN; i++) {
479 cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, i);
480 CHECK_NULL_RETURN(payloadItem, false);
481 if (cJSON_IsNumber(payloadItem)) {
482 userId |= (static_cast<uint8_t>(payloadItem->valueint)) << (i * BITS_PER_BYTE);
483 }
484 }
485 accountId = "";
486 for (uint32_t j = USERID_PAYLOAD_LEN; j < ACCOUNT_LOGOUT_BROADCAST_LEN; j++) {
487 cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, j);
488 CHECK_NULL_RETURN(payloadItem, false);
489 if (cJSON_IsNumber(payloadItem)) {
490 accountId += static_cast<char>(payloadItem->valueint);
491 }
492 }
493 broadCastId = 0;
494 for (uint32_t j = ACCOUNT_LOGOUT_BROADCAST_LEN; j < ACCOUNT_LOGOUT_PAYLOAD_LEN; j++) {
495 cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, j);
496 CHECK_NULL_RETURN(payloadItem, false);
497 if (cJSON_IsNumber(payloadItem)) {
498 broadCastId |= (static_cast<uint8_t>(payloadItem->valueint)) <<
499 ((j - ACCOUNT_LOGOUT_BROADCAST_LEN) * BITS_PER_BYTE);
500 }
501 }
502 return true;
503 }
504
FromDeviceUnbindPayLoad(const cJSON * payloadJson)505 bool RelationShipChangeMsg::FromDeviceUnbindPayLoad(const cJSON *payloadJson)
506 {
507 if (payloadJson == NULL) {
508 LOGE("Device unbind payloadJson is null.");
509 return false;
510 }
511 int32_t arraySize = cJSON_GetArraySize(payloadJson);
512 if (arraySize < DEVICE_UNBIND_PAYLOAD_LEN || arraySize >= INVALIED_PAYLOAD_SIZE) {
513 LOGE("Payload invalied,the size is %{public}d.", arraySize);
514 return false;
515 }
516 userId = 0;
517 for (uint32_t i = 0; i < USERID_PAYLOAD_LEN; i++) {
518 cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, i);
519 CHECK_NULL_RETURN(payloadItem, false);
520 if (cJSON_IsNumber(payloadItem)) {
521 userId |= (static_cast<uint8_t>(payloadItem->valueint)) << (i * BITS_PER_BYTE);
522 }
523 }
524 broadCastId = 0;
525 for (uint32_t j = USERID_PAYLOAD_LEN; j < DEVICE_UNBIND_PAYLOAD_LEN; j++) {
526 cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, j);
527 CHECK_NULL_RETURN(payloadItem, false);
528 if (cJSON_IsNumber(payloadItem)) {
529 broadCastId |= (static_cast<uint8_t>(payloadItem->valueint)) <<
530 ((j - USERID_PAYLOAD_LEN) * BITS_PER_BYTE);
531 }
532 }
533 return true;
534 }
535
FromAppUnbindPayLoad(const cJSON * payloadJson)536 bool RelationShipChangeMsg::FromAppUnbindPayLoad(const cJSON *payloadJson)
537 {
538 if (payloadJson == NULL) {
539 LOGE("App unbind payloadJson is null.");
540 return false;
541 }
542 int32_t arraySize = cJSON_GetArraySize(payloadJson);
543 if (arraySize < ACCOUNT_LOGOUT_PAYLOAD_LEN || arraySize >= INVALIED_PAYLOAD_SIZE) {
544 LOGE("Payload invalied,the size is %{public}d.", arraySize);
545 return false;
546 }
547 userId = 0;
548 for (uint32_t i = 0; i < USERID_PAYLOAD_LEN; i++) {
549 cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, i);
550 CHECK_NULL_RETURN(payloadItem, false);
551 if (cJSON_IsNumber(payloadItem)) {
552 userId |= (static_cast<uint8_t>(payloadItem->valueint)) << (i * BITS_PER_BYTE);
553 }
554 }
555 tokenId = 0;
556 for (uint32_t j = USERID_PAYLOAD_LEN; j < TOKENID_PAYLOAD_LEN; j++) {
557 cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, j);
558 CHECK_NULL_RETURN(payloadItem, false);
559 if (cJSON_IsNumber(payloadItem)) {
560 tokenId |= (static_cast<uint8_t>(payloadItem->valueint)) << ((j - USERID_PAYLOAD_LEN) * BITS_PER_BYTE);
561 }
562 }
563 peerTokenId = 0;
564 for (uint32_t j = TOKENID_PAYLOAD_LEN; j < BROADCAST_PAYLOAD_LEN; j++) {
565 cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, j);
566 CHECK_NULL_RETURN(payloadItem, false);
567 if (cJSON_IsNumber(payloadItem)) {
568 peerTokenId |= (static_cast<uint8_t>(payloadItem->valueint)) <<
569 ((j - TOKENID_PAYLOAD_LEN) * BITS_PER_BYTE);
570 }
571 }
572 broadCastId = 0;
573 for (uint32_t j = BROADCAST_PAYLOAD_LEN; j < APP_UNBIND_PAYLOAD_LEN; j++) {
574 cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, j);
575 CHECK_NULL_RETURN(payloadItem, false);
576 if (cJSON_IsNumber(payloadItem)) {
577 broadCastId |= (static_cast<uint8_t>(payloadItem->valueint)) <<
578 ((j - BROADCAST_PAYLOAD_LEN) * BITS_PER_BYTE);
579 }
580 }
581 return true;
582 }
583
FromAppUninstallPayLoad(const cJSON * payloadJson)584 bool RelationShipChangeMsg::FromAppUninstallPayLoad(const cJSON *payloadJson)
585 {
586 if (payloadJson == NULL) {
587 LOGE("App unbind payloadJson is null.");
588 return false;
589 }
590 int32_t arraySize = cJSON_GetArraySize(payloadJson);
591 if (arraySize < APP_UNINSTALL_PAYLOAD_LEN || arraySize >= INVALIED_PAYLOAD_SIZE) {
592 LOGE("Payload invalied,the size is %{public}d.", arraySize);
593 return false;
594 }
595 userId = 0;
596 for (uint32_t i = 0; i < USERID_PAYLOAD_LEN; i++) {
597 cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, i);
598 CHECK_NULL_RETURN(payloadItem, false);
599 if (cJSON_IsNumber(payloadItem)) {
600 userId |= (static_cast<uint8_t>(payloadItem->valueint)) << (i * BITS_PER_BYTE);
601 }
602 }
603 tokenId = 0;
604 for (uint32_t j = USERID_PAYLOAD_LEN; j < TOKENID_PAYLOAD_LEN; j++) {
605 cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, j);
606 CHECK_NULL_RETURN(payloadItem, false);
607 if (cJSON_IsNumber(payloadItem)) {
608 tokenId |= (static_cast<uint8_t>(payloadItem->valueint)) << ((j - USERID_PAYLOAD_LEN) * BITS_PER_BYTE);
609 }
610 }
611 this->broadCastId = 0;
612 for (uint32_t j = TOKENID_PAYLOAD_LEN; j < APP_UNINSTALL_PAYLOAD_LEN; j++) {
613 cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, j);
614 CHECK_NULL_RETURN(payloadItem, false);
615 if (cJSON_IsNumber(payloadItem)) {
616 this->broadCastId |= (static_cast<uint8_t>(payloadItem->valueint)) <<
617 ((j - TOKENID_PAYLOAD_LEN) * BITS_PER_BYTE);
618 }
619 }
620 return true;
621 }
622
FromServiceUnbindPayLoad(const cJSON * payloadJson)623 bool RelationShipChangeMsg::FromServiceUnbindPayLoad(const cJSON *payloadJson)
624 {
625 return FromAppUnbindPayLoad(payloadJson);
626 }
627
GetBroadCastId(const cJSON * payloadJson,uint32_t userIdNum)628 bool RelationShipChangeMsg::GetBroadCastId(const cJSON *payloadJson, uint32_t userIdNum)
629 {
630 broadCastId = 0;
631 cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, userIdNum * USERID_BYTES + 1);
632 CHECK_NULL_RETURN(payloadItem, false);
633 if (cJSON_IsNumber(payloadItem)) {
634 broadCastId |= static_cast<uint8_t>(payloadItem->valueint);
635 }
636 return true;
637 }
638
FromSyncFrontOrBackUserIdPayLoad(const cJSON * payloadJson)639 bool RelationShipChangeMsg::FromSyncFrontOrBackUserIdPayLoad(const cJSON *payloadJson)
640 {
641 if (payloadJson == NULL) {
642 LOGE("payloadJson is null.");
643 return false;
644 }
645
646 int32_t arraySize = cJSON_GetArraySize(payloadJson);
647 if (arraySize < SYNC_FRONT_OR_BACK_USERID_PAYLOAD_MIN_LEN ||
648 arraySize > SYNC_FRONT_OR_BACK_USERID_PAYLOAD_MAX_LEN) {
649 LOGE("Payload invalid, the size is %{public}d.", arraySize);
650 return false;
651 }
652
653 cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, 0);
654 CHECK_NULL_RETURN(payloadItem, false);
655 uint32_t userIdNum = 0;
656 if (cJSON_IsNumber(payloadItem)) {
657 uint8_t val = static_cast<uint8_t>(payloadItem->valueint);
658 this->syncUserIdFlag = (((val >> NEED_RSP_MASK_OFFSET) & 0x1) == 0x1);
659 this->isNewEvent = (((val >> IS_NEW_USER_SYNC_MASK_OFFSET) & 0x1) == 0x1);
660 userIdNum = ((static_cast<uint8_t>(payloadItem->valueint)) & FOREGROUND_USERID_LEN_MASK);
661 }
662
663 int32_t effectiveLen = static_cast<int32_t>((userIdNum + 1) * USERID_BYTES);
664 if (effectiveLen > arraySize) {
665 LOGE("payload userIdNum invalid, userIdNum: %{public}u, arraySize: %{public}d", userIdNum, arraySize);
666 return false;
667 }
668
669 uint16_t tempUserId = 0;
670 bool isForegroundUser = false;
671 for (int32_t idx = 1; idx < effectiveLen; idx++) {
672 cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, idx);
673 CHECK_NULL_RETURN(payloadItem, false);
674 if (!cJSON_IsNumber(payloadItem)) {
675 LOGE("Payload invalid, user id not integer");
676 return false;
677 }
678 if ((idx - 1) % USERID_BYTES == 0) {
679 tempUserId |= (static_cast<uint8_t>(payloadItem->valueint));
680 }
681 if ((idx - 1) % USERID_BYTES == 1) {
682 tempUserId |= (static_cast<uint8_t>(payloadItem->valueint) << BITS_PER_BYTE);
683 tempUserId &= FRONT_OR_BACK_USER_FLAG_MASK;
684 isForegroundUser = (((static_cast<uint8_t>(payloadItem->valueint) & FRONT_OR_BACK_FLAG_MASK) >>
685 FRONT_OR_BACK_USER_FLAG_OFFSET) == 0b1);
686 UserIdInfo userIdInfo(isForegroundUser, tempUserId);
687 this->userIdInfos.push_back(userIdInfo);
688 tempUserId = 0;
689 isForegroundUser = false;
690 }
691 }
692 return GetBroadCastId(payloadJson, userIdNum);
693 }
694
FromDelUserPayLoad(const cJSON * payloadJson)695 bool RelationShipChangeMsg::FromDelUserPayLoad(const cJSON *payloadJson)
696 {
697 if (payloadJson == NULL) {
698 LOGE("FromDelUserPayLoad payloadJson is null.");
699 return false;
700 }
701
702 int32_t arraySize = cJSON_GetArraySize(payloadJson);
703 if (arraySize < DEL_USER_PAYLOAD_LEN) {
704 LOGE("Payload invalid, the size is %{public}d.", arraySize);
705 return false;
706 }
707 this->userId = 0;
708 for (uint32_t i = 0; i < USERID_PAYLOAD_LEN; i++) {
709 cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, i);
710 CHECK_NULL_RETURN(payloadItem, false);
711 if (cJSON_IsNumber(payloadItem)) {
712 this->userId |= (static_cast<uint8_t>(payloadItem->valueint)) << (i * BITS_PER_BYTE);
713 }
714 }
715 this->broadCastId = 0;
716 for (uint32_t j = USERID_PAYLOAD_LEN; j < DEL_USER_PAYLOAD_LEN; j++) {
717 cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, j);
718 CHECK_NULL_RETURN(payloadItem, false);
719 if (cJSON_IsNumber(payloadItem)) {
720 this->broadCastId |= (static_cast<uint8_t>(payloadItem->valueint)) <<
721 ((j - USERID_PAYLOAD_LEN) * BITS_PER_BYTE);
722 }
723 }
724 return true;
725 }
726
FromStopUserPayLoad(const cJSON * payloadJson)727 bool RelationShipChangeMsg::FromStopUserPayLoad(const cJSON *payloadJson)
728 {
729 if (payloadJson == NULL) {
730 LOGE("FromStopUserPayLoad payloadJson is null.");
731 return false;
732 }
733
734 int32_t arraySize = cJSON_GetArraySize(payloadJson);
735 if (arraySize < STOP_USER_PAYLOAD_LEN) {
736 LOGE("Payload invalid, the size is %{public}d.", arraySize);
737 return false;
738 }
739 this->userId = 0;
740 for (uint32_t i = 0; i < USERID_PAYLOAD_LEN; i++) {
741 cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, i);
742 CHECK_NULL_RETURN(payloadItem, false);
743 if (cJSON_IsNumber(payloadItem)) {
744 this->userId |= (static_cast<uint8_t>(payloadItem->valueint)) << (i * BITS_PER_BYTE);
745 }
746 }
747 this->broadCastId = 0;
748 for (uint32_t j = USERID_PAYLOAD_LEN; j < STOP_USER_PAYLOAD_LEN; j++) {
749 cJSON *payloadItem = cJSON_GetArrayItem(payloadJson, j);
750 CHECK_NULL_RETURN(payloadItem, false);
751 if (cJSON_IsNumber(payloadItem)) {
752 this->broadCastId |= (static_cast<uint8_t>(payloadItem->valueint)) <<
753 ((j - USERID_PAYLOAD_LEN) * BITS_PER_BYTE);
754 }
755 }
756 return true;
757 }
758
ToPayLoadJson() const759 cJSON *RelationShipChangeMsg::ToPayLoadJson() const
760 {
761 uint8_t *payload = nullptr;
762 uint32_t len = 0;
763 if (!this->ToBroadcastPayLoad(payload, len)) {
764 LOGE("Get broadcast payload failed");
765 return nullptr;
766 }
767 if (payload == nullptr || len == 0) {
768 LOGE("payload is null or len is 0.");
769 return nullptr;
770 }
771 cJSON *arrayObj = cJSON_CreateArray();
772 if (arrayObj == nullptr) {
773 LOGE("cJSON_CreateArray failed");
774 if (payload != nullptr) {
775 delete[] payload;
776 }
777 return nullptr;
778 }
779 cJSON *numberObj = nullptr;
780 for (uint32_t index = 0; index < len; index++) {
781 numberObj = cJSON_CreateNumber(payload[index]);
782 if (numberObj == nullptr || !cJSON_AddItemToArray(arrayObj, numberObj)) {
783 cJSON_Delete(numberObj);
784 cJSON_Delete(arrayObj);
785 if (payload != nullptr) {
786 delete[] payload;
787 }
788 return nullptr;
789 }
790 }
791 if (payload != nullptr) {
792 delete[] payload;
793 }
794 return arrayObj;
795 }
796
ToJson() const797 std::string RelationShipChangeMsg::ToJson() const
798 {
799 cJSON *msg = cJSON_CreateObject();
800 if (msg == NULL) {
801 LOGE("failed to create cjson object");
802 return "";
803 }
804 cJSON_AddNumberToObject(msg, MSG_TYPE, (uint32_t)type);
805 cJSON *arrayObj = ToPayLoadJson();
806 if (arrayObj == nullptr || !cJSON_AddItemToObject(msg, MSG_VALUE, arrayObj)) {
807 LOGE("ArrayObj is nullptr.");
808 cJSON_Delete(arrayObj);
809 cJSON_Delete(msg);
810 return "";
811 }
812
813 cJSON *udidArrayObj = cJSON_CreateArray();
814 if (udidArrayObj == nullptr || !cJSON_AddItemToObject(msg, MSG_PEER_UDID, udidArrayObj)) {
815 LOGE("cJSON_CreateArray failed");
816 cJSON_Delete(udidArrayObj);
817 cJSON_Delete(msg);
818 return "";
819 }
820 cJSON *udidStringObj = nullptr;
821 for (uint32_t index = 0; index < peerUdids.size(); index++) {
822 udidStringObj = cJSON_CreateString(peerUdids[index].c_str());
823 if (udidStringObj == nullptr) {
824 cJSON_Delete(msg);
825 return "";
826 }
827 if (!cJSON_AddItemToArray(udidArrayObj, udidStringObj)) {
828 cJSON_Delete(udidStringObj);
829 cJSON_Delete(msg);
830 return "";
831 }
832 }
833 cJSON_AddStringToObject(msg, MSG_ACCOUNTID, accountName.c_str());
834 char *retStr = cJSON_PrintUnformatted(msg);
835 if (retStr == nullptr) {
836 LOGE("to json is nullptr.");
837 cJSON_Delete(msg);
838 return "";
839 }
840 std::string ret = std::string(retStr);
841 cJSON_Delete(msg);
842 cJSON_free(retStr);
843 return ret;
844 }
845
FromJson(const std::string & msgJson)846 bool RelationShipChangeMsg::FromJson(const std::string &msgJson)
847 {
848 cJSON *msgObj = cJSON_Parse(msgJson.c_str());
849 if (msgObj == NULL) {
850 LOGE("parse msg failed");
851 return false;
852 }
853
854 cJSON *typeJson = cJSON_GetObjectItem(msgObj, MSG_TYPE);
855 if (typeJson == NULL || !cJSON_IsNumber(typeJson) || !IsChangeTypeValid(typeJson->valueint)) {
856 LOGE("parse type failed.");
857 cJSON_Delete(msgObj);
858 return false;
859 }
860 this->type = (RelationShipChangeType)typeJson->valueint;
861
862 cJSON *payloadJson = cJSON_GetObjectItem(msgObj, MSG_VALUE);
863 if (payloadJson == NULL || !cJSON_IsArray(payloadJson)) {
864 LOGE("parse payload failed.");
865 cJSON_Delete(msgObj);
866 return false;
867 }
868 if (!this->FromBroadcastPayLoad(payloadJson, type)) {
869 LOGE("parse payload error.");
870 cJSON_Delete(msgObj);
871 return false;
872 }
873
874 cJSON *peerUdidJson = cJSON_GetObjectItem(msgObj, MSG_PEER_UDID);
875 if (peerUdidJson == NULL || !cJSON_IsString(peerUdidJson)) {
876 LOGE("parse peer udid failed.");
877 cJSON_Delete(msgObj);
878 return false;
879 }
880 this->peerUdid = std::string(peerUdidJson->valuestring);
881 cJSON_Delete(msgObj);
882 return true;
883 }
884
ToMapKey() const885 const std::string RelationShipChangeMsg::ToMapKey() const
886 {
887 std::ostringstream ret;
888 std::string isNewEventStr = isNewEvent ? "true" : "false";
889 ret << "_" << std::to_string(static_cast<uint32_t>(type));
890 ret << "_" << isNewEventStr;
891 ret << "_" << std::to_string(userId);
892 ret << "_" << GetAnonyString(accountId);
893 ret << "_" << GetAnonyString(peerUdid);
894 ret << "_" << std::to_string(tokenId);
895 ret << "_" << std::to_string(peerTokenId);
896 ret << "_" << GetUserIdInfoList(userIdInfos);
897 ret << "_" << std::to_string(syncUserIdFlag);
898 ret << "_" << credId;
899 return ret.str();
900 }
901
HandleRecvBroadCastTimeout(const std::string & key)902 void ReleationShipSyncMgr::HandleRecvBroadCastTimeout(const std::string &key)
903 {
904 std::lock_guard<std::mutex> autoLock(lock_);
905 auto iter = recvBroadCastIdMap_.find(key);
906 if (iter != recvBroadCastIdMap_.end()) {
907 recvBroadCastIdMap_.erase(iter);
908 }
909 }
910
GetCurrentTimeSec(int32_t & sec)911 bool ReleationShipSyncMgr::GetCurrentTimeSec(int32_t &sec)
912 {
913 int32_t retryNum = 0;
914 time_t now;
915 struct tm ltm;
916 while (retryNum < GET_CURRENT_TIME_MAX_NUM) {
917 time(&now);
918 struct tm *res = localtime_r(&now, <m);
919 if (res == nullptr) {
920 retryNum++;
921 LOGE("get current time failed. retryNum: %{public}d", retryNum);
922 continue;
923 }
924 sec = ltm.tm_sec % CURRENT_TIME_SEC_FLAG;
925 if (sec == 0) {
926 sec = CURRENT_TIME_SEC_FLAG;
927 }
928 return true;
929 }
930 return false;
931 }
932
SyncTrustRelationShip(RelationShipChangeMsg & msg)933 std::string ReleationShipSyncMgr::SyncTrustRelationShip(RelationShipChangeMsg &msg)
934 {
935 int32_t currentTimeSec = 0;
936 msg.broadCastId = 0;
937 if (!GetCurrentTimeSec(currentTimeSec)) {
938 LOGE("get current time failed, use default value");
939 return msg.ToJson();
940 }
941 msg.broadCastId = static_cast<uint8_t>(currentTimeSec);
942 LOGI("send trust change msg: %{public}s", msg.ToString().c_str());
943 return msg.ToJson();
944 }
945
ParseTrustRelationShipChange(const std::string & msgJson)946 RelationShipChangeMsg ReleationShipSyncMgr::ParseTrustRelationShipChange(const std::string &msgJson)
947 {
948 RelationShipChangeMsg msgObj;
949 if (!msgObj.FromJson(msgJson)) {
950 LOGE("Parse json failed");
951 }
952 return msgObj;
953 }
954
IsNewBroadCastId(const RelationShipChangeMsg & msg)955 bool ReleationShipSyncMgr::IsNewBroadCastId(const RelationShipChangeMsg &msg)
956 {
957 if (msg.broadCastId == 0) {
958 return true;
959 }
960 std::string key = msg.ToMapKey();
961 std::lock_guard<std::mutex> autoLock(lock_);
962 if (timer_ == nullptr) {
963 timer_ = std::make_shared<DmTimer>();
964 }
965 auto iter = recvBroadCastIdMap_.find(key);
966 if (iter == recvBroadCastIdMap_.end()) {
967 CHECK_SIZE_RETURN(recvBroadCastIdMap_, true);
968 recvBroadCastIdMap_[key] = msg.broadCastId;
969 timer_->StartTimer(key, BROADCAST_TIMEOUT_S, [this](std::string key) {
970 ReleationShipSyncMgr::HandleRecvBroadCastTimeout(key);
971 });
972 return true;
973 } else if (iter != recvBroadCastIdMap_.end() && recvBroadCastIdMap_[key] != msg.broadCastId) {
974 timer_->DeleteTimer(key);
975 recvBroadCastIdMap_[key] = msg.broadCastId;
976 timer_->StartTimer(key, BROADCAST_TIMEOUT_S, [this](std::string key) {
977 ReleationShipSyncMgr::HandleRecvBroadCastTimeout(key);
978 });
979 return true;
980 } else {
981 return false;
982 }
983 return false;
984 }
985
ToString() const986 const std::string RelationShipChangeMsg::ToString() const
987 {
988 std::ostringstream ret;
989 std::string isNewEventStr = isNewEvent ? "true" : "false";
990 ret << "{ MsgType: " << std::to_string(static_cast<uint32_t>(type));
991 ret << "{ isNewEvent: " << isNewEventStr;
992 ret << ", userId: " << std::to_string(userId);
993 ret << ", accountId: " << GetAnonyString(accountId);
994 ret << ", tokenId: " << std::to_string(tokenId);
995 ret << ", peerUdids: " << GetAnonyStringList(peerUdids);
996 ret << ", peerUdid: " << GetAnonyString(peerUdid);
997 ret << ", accountName: " << GetAnonyString(accountName);
998 ret << ", syncUserIdFlag: " << std::to_string(syncUserIdFlag);
999 ret << ", userIds: " << GetUserIdInfoList(userIdInfos);
1000 ret << ", broadCastId: " << std::to_string(broadCastId) << " }";
1001 return ret.str();
1002 }
1003
ToString() const1004 const std::string UserIdInfo::ToString() const
1005 {
1006 std::ostringstream ret;
1007 ret << "{ " << std::to_string(this->isForeground);
1008 ret << ", userId: " << std::to_string(this->userId) << " }";
1009 return ret.str();
1010 }
1011
GetUserIdInfoList(const std::vector<UserIdInfo> & list)1012 const std::string GetUserIdInfoList(const std::vector<UserIdInfo> &list)
1013 {
1014 std::string temp = "[ ";
1015 bool flag = false;
1016 for (auto const &userId : list) {
1017 temp += userId.ToString() + PRINT_LIST_SPLIT;
1018 flag = true;
1019 }
1020 if (flag) {
1021 temp.erase(temp.length() - LIST_SPLIT_LEN);
1022 }
1023 temp += " ]";
1024 return temp;
1025 }
1026
GetFrontAndBackUserIdInfos(const std::vector<UserIdInfo> & remoteUserIdInfos,std::vector<UserIdInfo> & foregroundUserIdInfos,std::vector<UserIdInfo> & backgroundUserIdInfos)1027 void GetFrontAndBackUserIdInfos(const std::vector<UserIdInfo> &remoteUserIdInfos,
1028 std::vector<UserIdInfo> &foregroundUserIdInfos, std::vector<UserIdInfo> &backgroundUserIdInfos)
1029 {
1030 foregroundUserIdInfos.clear();
1031 backgroundUserIdInfos.clear();
1032 for (auto const &u : remoteUserIdInfos) {
1033 if (u.isForeground) {
1034 foregroundUserIdInfos.push_back(u);
1035 } else {
1036 backgroundUserIdInfos.push_back(u);
1037 }
1038 }
1039 }
1040 } // DistributedHardware
1041 } // OHOS