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 <map> 21 #include <mutex> 22 #include "cJSON.h" 23 #include "dm_single_instance.h" 24 #include "dm_timer.h" 25 namespace OHOS { 26 namespace DistributedHardware { 27 enum class RelationShipChangeType : uint32_t { 28 ACCOUNT_LOGOUT = 0, 29 DEVICE_UNBIND = 1, 30 APP_UNBIND = 2, 31 SERVICE_UNBIND = 3, 32 DEL_USER = 4, 33 APP_UNINSTALL = 5, 34 SYNC_USERID = 6, 35 STOP_USER = 7, 36 SHARE_UNBIND = 8, 37 TYPE_MAX = 9 38 }; 39 40 struct UserIdInfo { 41 /** 42 * @brief true for foreground userid, false for background 43 */ 44 bool isForeground; 45 std::uint16_t userId; UserIdInfoUserIdInfo46 UserIdInfo(bool isForeground, std::uint16_t userId) : isForeground(isForeground), userId(userId) {} 47 const std::string ToString() const; 48 }; 49 50 struct RelationShipChangeMsg { 51 RelationShipChangeType type; 52 uint32_t userId; 53 std::string accountId; 54 uint64_t tokenId; 55 uint64_t peerTokenId = 0; 56 // The broadcast need send to these devices with the udids 57 std::vector<std::string> peerUdids; 58 // The broadcast from which device with the udid. 59 std::string peerUdid; 60 std::string accountName; 61 // true: request, false: response 62 bool syncUserIdFlag; 63 // The foreground and background user id infos 64 std::vector<UserIdInfo> userIdInfos; 65 std::string credId; 66 bool isNewEvent; 67 uint8_t broadCastId; 68 69 explicit RelationShipChangeMsg(); 70 bool ToBroadcastPayLoad(uint8_t *&msg, uint32_t &len) const; 71 bool FromBroadcastPayLoad(const cJSON *payloadJson, RelationShipChangeType type); 72 bool IsValid() const; 73 bool IsChangeTypeValid(); 74 bool IsChangeTypeValid(uint32_t type); 75 void ToAccountLogoutPayLoad(uint8_t *&msg, uint32_t &len) const; 76 void ToDeviceUnbindPayLoad(uint8_t *&msg, uint32_t &len) const; 77 void ToAppUnbindPayLoad(uint8_t *&msg, uint32_t &len) const; 78 void ToAppUninstallPayLoad(uint8_t *&msg, uint32_t &len) const; 79 void ToServiceUnbindPayLoad(uint8_t *&msg, uint32_t &len) const; 80 bool ToSyncFrontOrBackUserIdPayLoad(uint8_t *&msg, uint32_t &len) const; 81 void ToDelUserPayLoad(uint8_t *&msg, uint32_t &len) const; 82 void ToStopUserPayLoad(uint8_t *&msg, uint32_t &len) const; 83 void ToShareUnbindPayLoad(uint8_t *&msg, uint32_t &len) const; 84 cJSON *ToPayLoadJson() const; 85 86 bool FromAccountLogoutPayLoad(const cJSON *payloadJson); 87 bool FromDeviceUnbindPayLoad(const cJSON *payloadJson); 88 bool FromAppUnbindPayLoad(const cJSON *payloadJson); 89 bool FromServiceUnbindPayLoad(const cJSON *payloadJson); 90 bool FromSyncFrontOrBackUserIdPayLoad(const cJSON *payloadJson); 91 bool FromDelUserPayLoad(const cJSON *payloadJson); 92 bool FromStopUserPayLoad(const cJSON *payloadJson); 93 bool FromShareUnbindPayLoad(const cJSON *payloadJson); 94 bool FromAppUninstallPayLoad(const cJSON *payloadJson); 95 bool GetBroadCastId(const cJSON *payloadJson, uint32_t userIdNum); 96 97 std::string ToJson() const; 98 bool FromJson(const std::string &msgJson); 99 100 const std::string ToString() const; 101 const std::string ToMapKey() const; 102 }; 103 104 class ReleationShipSyncMgr { 105 DM_DECLARE_SINGLE_INSTANCE(ReleationShipSyncMgr); 106 public: 107 std::string SyncTrustRelationShip(RelationShipChangeMsg &msg); 108 RelationShipChangeMsg ParseTrustRelationShipChange(const std::string &msgJson); 109 bool IsNewBroadCastId(const RelationShipChangeMsg &msg); 110 private: 111 void HandleRecvBroadCastTimeout(const std::string &key); 112 bool GetCurrentTimeSec(int32_t &sec); 113 std::map<std::string, int32_t> recvBroadCastIdMap_; 114 std::shared_ptr<DmTimer> timer_; 115 std::mutex lock_; 116 }; 117 118 const std::string GetUserIdInfoList(const std::vector<UserIdInfo> &list); 119 void GetFrontAndBackUserIdInfos(const std::vector<UserIdInfo> &remoteUserIdInfos, 120 std::vector<UserIdInfo> &foregroundUserIdInfos, std::vector<UserIdInfo> &backgroundUserIdInfos); 121 } // DistributedHardware 122 } // OHOS 123 #endif // OHOS_RELATIONSHIP_SYNC_MGR_H