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 #ifndef OHOS_RELATIONSHIP_SYNC_MGR_H 17 #define OHOS_RELATIONSHIP_SYNC_MGR_H 18 19 #include <string> 20 #include "cJSON.h" 21 #include "dm_single_instance.h" 22 namespace OHOS { 23 namespace DistributedHardware { 24 enum class RelationShipChangeType : uint32_t { 25 ACCOUNT_LOGOUT = 0, 26 DEVICE_UNBIND = 1, 27 APP_UNBIND = 2, 28 SERVICE_UNBIND = 3, 29 DEL_USER = 4, 30 APP_UNINSTALL = 5, 31 SYNC_USERID = 6, 32 STOP_USER = 7, 33 TYPE_MAX = 8 34 }; 35 36 struct UserIdInfo { 37 /** 38 * @brief true for foreground userid, false for background 39 */ 40 bool isForeground; 41 std::uint16_t userId; UserIdInfoUserIdInfo42 UserIdInfo(bool isForeground, std::uint16_t userId) : isForeground(isForeground), userId(userId) {} 43 const std::string ToString() const; 44 }; 45 46 struct RelationShipChangeMsg { 47 RelationShipChangeType type; 48 uint32_t userId; 49 std::string accountId; 50 uint64_t tokenId; 51 uint64_t peerTokenId = 0; 52 // The broadcast need send to these devices with the udids 53 std::vector<std::string> peerUdids; 54 // The broadcast from which device with the udid. 55 std::string peerUdid; 56 std::string accountName; 57 // true: request, false: response 58 bool syncUserIdFlag; 59 // The foreground and background user id infos 60 std::vector<UserIdInfo> userIdInfos; 61 62 explicit RelationShipChangeMsg(); 63 bool ToBroadcastPayLoad(uint8_t *&msg, uint32_t &len) const; 64 bool FromBroadcastPayLoad(const cJSON *payloadJson, RelationShipChangeType type); 65 bool IsValid() const; 66 bool IsChangeTypeValid(); 67 bool IsChangeTypeValid(uint32_t type); 68 void ToAccountLogoutPayLoad(uint8_t *&msg, uint32_t &len) const; 69 void ToDeviceUnbindPayLoad(uint8_t *&msg, uint32_t &len) const; 70 void ToAppUnbindPayLoad(uint8_t *&msg, uint32_t &len) const; 71 bool ToSyncFrontOrBackUserIdPayLoad(uint8_t *&msg, uint32_t &len) const; 72 void ToDelUserPayLoad(uint8_t *&msg, uint32_t &len) const; 73 void ToStopUserPayLoad(uint8_t *&msg, uint32_t &len) const; 74 cJSON *ToPayLoadJson() const; 75 76 bool FromAccountLogoutPayLoad(const cJSON *payloadJson); 77 bool FromDeviceUnbindPayLoad(const cJSON *payloadJson); 78 bool FromAppUnbindPayLoad(const cJSON *payloadJson); 79 bool FromSyncFrontOrBackUserIdPayLoad(const cJSON *payloadJson); 80 bool FromDelUserPayLoad(const cJSON *payloadJson); 81 bool FromStopUserPayLoad(const cJSON *payloadJson); 82 83 std::string ToJson() const; 84 bool FromJson(const std::string &msgJson); 85 86 const std::string ToString() const; 87 }; 88 89 class ReleationShipSyncMgr { 90 DM_DECLARE_SINGLE_INSTANCE(ReleationShipSyncMgr); 91 public: 92 std::string SyncTrustRelationShip(const RelationShipChangeMsg &msg); 93 RelationShipChangeMsg ParseTrustRelationShipChange(const std::string &msgJson); 94 }; 95 96 const std::string GetUserIdInfoList(const std::vector<UserIdInfo> &list); 97 void GetFrontAndBackUserIdInfos(const std::vector<UserIdInfo> &remoteUserIdInfos, 98 std::vector<UserIdInfo> &foregroundUserIdInfos, std::vector<UserIdInfo> &backgroundUserIdInfos); 99 } // DistributedHardware 100 } // OHOS 101 #endif // OHOS_RELATIONSHIP_SYNC_MGR_H