1 /* 2 * Copyright (c) 2021-2022 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 "common_event_manager.h" 24 #include "distributed_data_storage.h" 25 #include "distributed_mission_change_listener.h" 26 #include "distributed_mission_info.h" 27 #include "distributed_sched_interface.h" 28 #include "event_handler.h" 29 #include "single_instance.h" 30 #include "snapshot.h" 31 32 namespace OHOS { 33 namespace DistributedSchedule { 34 struct ListenerInfo { 35 bool called = false; 36 std::set<sptr<IRemoteObject>> listenerSet; 37 EmplaceListenerInfo38 bool Emplace(sptr<IRemoteObject> listener) 39 { 40 auto pairRet = listenerSet.emplace(listener); 41 if (!pairRet.second) { 42 return false; 43 } 44 return true; 45 } 46 FindListenerInfo47 bool Find(sptr<IRemoteObject> listener) 48 { 49 auto iter = listenerSet.find(listener); 50 if (iter == listenerSet.end()) { 51 return false; 52 } 53 return true; 54 } 55 EraseListenerInfo56 void Erase(sptr<IRemoteObject> listener) 57 { 58 listenerSet.erase(listener); 59 } 60 SizeListenerInfo61 int32_t Size() const 62 { 63 return listenerSet.size(); 64 } 65 EmptyListenerInfo66 bool Empty() const 67 { 68 return listenerSet.empty(); 69 } 70 }; 71 class DistributedSchedMissionManager { 72 DECLARE_SINGLE_INSTANCE(DistributedSchedMissionManager); 73 74 public: 75 void Init(); 76 int32_t GetMissionInfos(const std::string& deviceId, int32_t numMissions, 77 std::vector<AAFwk::MissionInfo>& missionInfos); 78 int32_t InitDataStorage(); 79 int32_t StopDataStorage(); 80 int32_t StoreSnapshotInfo(const std::string& deviceId, int32_t missionId, 81 const uint8_t* byteStream, size_t len); 82 int32_t RemoveSnapshotInfo(const std::string& deviceId, int32_t missionId); 83 std::unique_ptr<Snapshot> GetRemoteSnapshotInfo(const std::string& deviceId, int32_t missionId); 84 int32_t GetRemoteMissionSnapshotInfo(const std::string& networkId, int32_t missionId, 85 std::unique_ptr<AAFwk::MissionSnapshot>& missionSnapshot); 86 void DeviceOnlineNotify(const std::string& deviceId); 87 void DeviceOfflineNotify(const std::string& deviceId); 88 void DeleteDataStorage(const std::string& deviceId, bool isDelayed); 89 int32_t RegisterMissionListener(const std::u16string& devId, const sptr<IRemoteObject>& obj); 90 int32_t UnRegisterMissionListener(const std::u16string& devId, const sptr<IRemoteObject>& obj); 91 int32_t StartSyncRemoteMissions(const std::string& devId, bool fixConflict, int64_t tag); 92 int32_t StartSyncMissionsFromRemote(const CallerInfo& callerInfo, std::vector<DstbMissionInfo>& missionInfos); 93 int32_t StopSyncRemoteMissions(const std::string& dstDevId, bool offline, bool exit = false); 94 void StopSyncMissionsFromRemote(const std::string& deviceId); 95 bool needSyncDevice(const std::string& deviceId); 96 97 void NotifySnapshotChanged(const std::string& networkId, int32_t missionId); 98 void OnRemoteDied(const wptr<IRemoteObject>& remote); 99 100 void EnqueueCachedSnapshotInfo(const std::string& deviceId, int32_t missionId, std::unique_ptr<Snapshot> snapshot); 101 std::unique_ptr<Snapshot> DequeueCachedSnapshotInfo(const std::string& deviceId, int32_t missionId); 102 int32_t NotifyMissionsChangedToRemote(const std::vector<DstbMissionInfo>& missionInfos); 103 int32_t NotifyMissionsChangedFromRemote(const CallerInfo& callerInfo, 104 const std::vector<DstbMissionInfo>& missionInfos); 105 int32_t CheckSupportOsd(const std::string& deviceId); 106 int32_t CheckOsdSwitch(const std::string& deviceId); 107 void GetCachedOsdSwitch(std::vector<std::u16string>& deviceIds, std::vector<int32_t>& values); 108 int32_t GetOsdSwitchValueFromRemote(); 109 int32_t UpdateOsdSwitchValueFromRemote(int32_t switchVal, const std::string& deviceId); 110 int32_t UpdateSwitchValueToRemote(); 111 void UpdateConnCapSupportOsd(const std::string& deviceId); 112 void NotifyOsdSwitchChanged(bool needNotifyChanged); 113 void OnRemoteDmsDied(const wptr<IRemoteObject>& remote); 114 void NotifyDmsProxyProcessDied(); 115 void OnDnetDied(); 116 void NotifyLocalMissionsChanged(); 117 void NotifyMissionSnapshotCreated(int32_t missionId); 118 void NotifyMissionSnapshotChanged(int32_t missionId); 119 void NotifyMissionSnapshotDestroyed(int32_t missionId); 120 void NotifyRemoteDied(const wptr<IRemoteObject>& remote); 121 private: 122 std::map<std::string, std::shared_ptr<AppExecFwk::EventHandler>> deviceHandle_; 123 mutable std::mutex remoteMissionInfosLock_; 124 std::map<std::string, std::vector<DstbMissionInfo>> deviceMissionInfos_; 125 sptr<IDistributedSched> GetRemoteDms(const std::string& deviceId); 126 bool IsDeviceIdValidated(const std::string& deviceId); 127 std::shared_ptr<AppExecFwk::EventHandler> FetchDeviceHandler(const std::string& deviceId); 128 bool GenerateCallerInfo(CallerInfo& callerInfo); 129 void NotifyMissionsChangedToRemoteInner(const std::string& remoteUuid, 130 const std::vector<DstbMissionInfo>& missionInfos, const CallerInfo& callerInfo); GenerateKeyInfo(const std::string & devId,int32_t missionId)131 std::string GenerateKeyInfo(const std::string& devId, int32_t missionId) 132 { 133 return devId + "_" + std::to_string(missionId); 134 } 135 bool AllowMissionUid(int32_t uid); 136 int32_t StartSyncRemoteMissions(const std::string& dstDevId, const std::string& localDevId); 137 int32_t StartSyncRemoteMissions(const std::string& dstDevId, const sptr<IDistributedSched>& remoteDms); 138 void CleanMissionResources(const std::string& dstDevId); 139 void RetryStartSyncRemoteMissions(const std::string& dstDeviceId, const std::string& localDevId, 140 int32_t retryTimes); 141 bool HasSyncListener(const std::string& networkId); 142 void DeleteCachedSnapshotInfo(const std::string& networkId); 143 int32_t FetchCachedRemoteMissions(const std::string& srcId, int32_t numMissions, 144 std::vector<DstbMissionInfo>& missionInfos); 145 void RebornMissionCache(const std::string& deviceId, const std::vector<DstbMissionInfo>& missionInfos); 146 void CleanMissionCache(const std::string& deviceId); 147 void UpdateSwitchValueToRemoteInner(std::set<std::string>& remoteSyncDeviceSet, 148 const std::string& localNetworkId); 149 void TryUpdateSwitchValueToRemote(const std::string& localNetworkId, 150 const std::string& destUuid, int32_t retryTime); 151 bool IsValidOsdSwitchValue(int32_t osdSwitchVal); 152 bool IsConnCapSupportOsd(const std::string& deviceId); 153 bool GetConnCapSupportOsd(const std::string& deviceId); 154 bool GetConnCapSupportOsdInnerLocked(const std::string& deviceId); 155 bool PreCheckSupportOsd(const std::string& deviceId); 156 void NotifyOsdSwitchChanged(bool needNotifyChanged, const std::string& deviceId, int32_t switchVal); 157 void OnMissionListenerDied(const sptr<IRemoteObject>& remote); 158 void OnRemoteDmsDied(const sptr<IRemoteObject>& remote); 159 void RetryRegisterMissionChange(int32_t retryTimes); 160 void InitAllSnapshots(const std::vector<DstbMissionInfo>& missionInfos); 161 int32_t MissionSnapshotChanged(int32_t missionId); 162 int32_t MissionSnapshotDestroyed(int32_t missionId); 163 int32_t MissionSnapshotSequence(const Snapshot& snapshot, MessageParcel& data); 164 165 class ListenerDeathRecipient : public IRemoteObject::DeathRecipient { 166 public: 167 void OnRemoteDied(const wptr<IRemoteObject>& remote) override; 168 }; 169 sptr<ListenerDeathRecipient> listenerDeath_; 170 171 std::set<std::string> remoteSyncDeviceSet_; 172 std::mutex remoteSyncDeviceLock_; 173 174 std::map<std::string, std::unique_ptr<Snapshot>> cachedSnapshotInfos_; 175 std::map<std::u16string, ListenerInfo> listenDeviceMap_; 176 std::mutex listenDeviceLock_; 177 std::shared_ptr<DistributedDataStorage> distributedDataStorage_; 178 179 std::set<int32_t> allowMissionUids_; 180 std::mutex allowMissionUidsLock_; 181 std::atomic<bool> isRegMissionChange_ = false; 182 sptr<DistributedMissionChangeListener> missonChangeListener_; 183 std::shared_ptr<AppExecFwk::EventHandler> missionChangeHandler_; 184 185 class RemoteDmsDeathRecipient : public IRemoteObject::DeathRecipient { 186 public: 187 void OnRemoteDied(const wptr<IRemoteObject>& remote) override; 188 }; 189 sptr<RemoteDmsDeathRecipient> remoteDmsRecipient_; 190 std::map<std::string, sptr<IDistributedSched>> remoteDmsMap_; 191 std::mutex remoteDmsLock_; 192 std::shared_ptr<AppExecFwk::EventHandler> missionHandler_; 193 std::shared_ptr<AppExecFwk::EventHandler> updateHandler_; 194 std::mutex osdSwitchLock_; 195 std::map<std::string, int32_t> osdSwitchValueMap_; // key is uuid 196 std::map<std::string, bool> connCapSupportOsdMap_; // key is networkId 197 }; 198 } 199 } 200 #endif // DISTRIBUTEDSCHED_MISSION_MANAGER_H 201