1 /* 2 * Copyright (c) 2021-2023 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 DISTRIBUTEDSCHED_MISSION_MANAGER_H 17 #define DISTRIBUTEDSCHED_MISSION_MANAGER_H 18 19 #include <set> 20 #include <string> 21 #include <vector> 22 23 #include "distributed_data_storage.h" 24 #include "distributed_mission_change_listener.h" 25 #include "distributed_mission_info.h" 26 #include "distributed_sched_interface.h" 27 #include "event_handler.h" 28 #include "single_instance.h" 29 #include "snapshot.h" 30 #include "distributed_sched_interface.h" 31 32 namespace OHOS { 33 namespace DistributedSchedule { 34 using AccountInfo = IDistributedSched::AccountInfo; 35 struct ListenerInfo { 36 bool called = false; 37 std::set<sptr<IRemoteObject>> listenerSet; 38 EmplaceListenerInfo39 bool Emplace(sptr<IRemoteObject> listener) 40 { 41 auto pairRet = listenerSet.emplace(listener); 42 return pairRet.second; 43 } 44 FindListenerInfo45 bool Find(const sptr<IRemoteObject> listener) 46 { 47 auto iter = listenerSet.find(listener); 48 if (iter == listenerSet.end()) { 49 return false; 50 } 51 return true; 52 } 53 EraseListenerInfo54 void Erase(sptr<IRemoteObject> listener) 55 { 56 listenerSet.erase(listener); 57 } 58 SizeListenerInfo59 int32_t Size() const 60 { 61 return listenerSet.size(); 62 } 63 EmptyListenerInfo64 bool Empty() const 65 { 66 return listenerSet.empty(); 67 } 68 }; 69 class DistributedSchedMissionManager { 70 DECLARE_SINGLE_INSTANCE(DistributedSchedMissionManager); 71 72 public: 73 void Init(); 74 int32_t GetMissionInfos(const std::string& deviceId, int32_t numMissions, 75 std::vector<AAFwk::MissionInfo>& missionInfoSet); 76 int32_t InitDataStorage(); 77 int32_t StopDataStorage(); 78 int32_t StoreSnapshotInfo(const std::string& deviceId, int32_t missionId, 79 const uint8_t* byteStream, size_t len); 80 int32_t RemoveSnapshotInfo(const std::string& deviceId, int32_t missionId); 81 int32_t GetRemoteMissionSnapshotInfo(const std::string& networkId, int32_t missionId, 82 std::unique_ptr<AAFwk::MissionSnapshot>& missionSnapshot); 83 void DeviceOnlineNotify(const std::string& deviceId); 84 void DeviceOfflineNotify(const std::string& deviceId); 85 void DeleteDataStorage(const std::string& deviceId, bool isDelayed); 86 int32_t RegisterMissionListener(const std::u16string& devId, const sptr<IRemoteObject>& obj); 87 int32_t UnRegisterMissionListener(const std::u16string& devId, const sptr<IRemoteObject>& obj); 88 int32_t StartSyncRemoteMissions(const std::string& devId, bool fixConflict, int64_t tag, 89 uint32_t callingTokenId = 0); 90 int32_t StartSyncMissionsFromRemote(const CallerInfo& callerInfo, std::vector<DstbMissionInfo>& missionInfoSet); 91 bool CheckAccessControlForMissions(const CallerInfo& callerInfo); 92 int32_t StopSyncRemoteMissions(const std::string& dstDevId, bool offline, bool exit = false); 93 void StopSyncMissionsFromRemote(const std::string& deviceId); 94 bool NeedSyncDevice(const std::string& deviceId); 95 96 void NotifySnapshotChanged(const std::string& networkId, int32_t missionId); 97 void OnRemoteDied(const wptr<IRemoteObject>& remote); 98 99 void EnqueueCachedSnapshotInfo(const std::string& deviceId, int32_t missionId, std::unique_ptr<Snapshot> snapshot); 100 std::unique_ptr<Snapshot> DequeueCachedSnapshotInfo(const std::string& deviceId, int32_t missionId); 101 int32_t NotifyMissionsChangedToRemote(const std::vector<DstbMissionInfo>& missionInfoSet); 102 int32_t NotifyMissionsChangedFromRemote(const CallerInfo& callerInfo, 103 const std::vector<DstbMissionInfo>& missionInfoSet); 104 void OnRemoteDmsDied(const wptr<IRemoteObject>& remote); 105 void NotifyDmsProxyProcessDied(); 106 void OnDnetDied(); 107 void NotifyLocalMissionsChanged(); 108 void NotifyMissionSnapshotCreated(int32_t missionId); 109 void NotifyMissionSnapshotChanged(int32_t missionId); 110 void NotifyMissionSnapshotDestroyed(int32_t missionId); 111 void NotifyRemoteDied(const wptr<IRemoteObject>& remote); 112 void NotifyNetDisconnectOffline(); 113 bool GetOsAccountData(AccountInfo& dmsAccountInfo); 114 private: 115 std::map<std::string, std::shared_ptr<AppExecFwk::EventHandler>> deviceHandle_; 116 mutable std::mutex remoteMissionInfosLock_; 117 std::map<std::string, std::vector<DstbMissionInfo>> deviceMissionInfos_; 118 sptr<IDistributedSched> GetRemoteDms(const std::string& deviceId); 119 bool IsDeviceIdValidated(const std::string& deviceId); 120 std::shared_ptr<AppExecFwk::EventHandler> FetchDeviceHandler(const std::string& deviceId); 121 bool GenerateCallerInfo(CallerInfo& callerInfo); 122 void NotifyMissionsChangedToRemoteInner(const std::string& remoteUuid, 123 const std::vector<DstbMissionInfo>& missionInfoSet, const CallerInfo& callerInfo); GenerateKeyInfo(const std::string & devId,int32_t missionId)124 std::string GenerateKeyInfo(const std::string& devId, int32_t missionId) 125 { 126 return devId + "_" + std::to_string(missionId); 127 } 128 int32_t StartSyncRemoteMissions(const std::string& dstDevId, const std::string& localDevId, 129 uint32_t callingTokenId = 0); 130 int32_t StartSyncRemoteMissions(const std::string& dstDevId, const sptr<IDistributedSched>& remoteDms, 131 uint32_t callingTokenId = 0); 132 void CleanMissionResources(const std::string& dstDevId); 133 void RetryStartSyncRemoteMissions(const std::string& dstDeviceId, const std::string& localDevId, 134 int32_t retryTimes); 135 bool HasSyncListener(const std::string& networkId); 136 void DeleteCachedSnapshotInfo(const std::string& networkId); 137 int32_t FetchCachedRemoteMissions(const std::string& srcId, int32_t numMissions, 138 std::vector<DstbMissionInfo>& missionInfoSet); 139 void RebornMissionCache(const std::string& deviceId, const std::vector<DstbMissionInfo>& missionInfoSet); 140 void CleanMissionCache(const std::string& deviceId); 141 void OnMissionListenerDied(const sptr<IRemoteObject>& remote); 142 void OnRemoteDmsDied(const sptr<IRemoteObject>& remote); 143 void RetryRegisterMissionChange(int32_t retryTimes); 144 void InitAllSnapshots(const std::vector<DstbMissionInfo>& missionInfoSet); 145 int32_t MissionSnapshotChanged(int32_t missionId); 146 int32_t MissionSnapshotDestroyed(int32_t missionId); 147 int32_t MissionSnapshotSequence(const Snapshot& snapshot, MessageParcel& data); 148 149 class ListenerDeathRecipient : public IRemoteObject::DeathRecipient { 150 public: 151 void OnRemoteDied(const wptr<IRemoteObject>& remote) override; 152 }; 153 sptr<ListenerDeathRecipient> listenerDeath_; 154 155 std::set<std::string> remoteSyncDeviceSet_; 156 std::mutex remoteSyncDeviceLock_; 157 158 std::map<std::string, std::unique_ptr<Snapshot>> cachedSnapshotInfos_; 159 std::map<std::u16string, ListenerInfo> listenDeviceMap_; 160 std::mutex listenDeviceLock_; 161 std::shared_ptr<DistributedDataStorage> distributedDataStorage_; 162 163 std::set<int32_t> allowMissionUids_; 164 std::mutex allowMissionUidsLock_; 165 std::atomic<bool> isRegMissionChange_ = false; 166 sptr<DistributedMissionChangeListener> missonChangeListener_; 167 std::shared_ptr<AppExecFwk::EventHandler> missionChangeHandler_; 168 169 class RemoteDmsDeathRecipient : public IRemoteObject::DeathRecipient { 170 public: 171 void OnRemoteDied(const wptr<IRemoteObject>& remote) override; 172 }; 173 sptr<RemoteDmsDeathRecipient> remoteDmsRecipient_; 174 std::map<std::string, sptr<IDistributedSched>> remoteDmsMap_; 175 std::mutex remoteDmsLock_; 176 std::shared_ptr<AppExecFwk::EventHandler> missionHandler_; 177 std::shared_ptr<AppExecFwk::EventHandler> updateHandler_; 178 }; 179 } // namespace DistributedSchedule 180 } // namespace OHOS 181 #endif // DISTRIBUTEDSCHED_MISSION_MANAGER_H 182