• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "mission/distributed_sched_mission_manager.h"
17 
18 #include <chrono>
19 #include <sys/time.h>
20 #include <unistd.h>
21 
22 #include "datetime_ex.h"
23 #include "distributed_sched_adapter.h"
24 #include "dtbschedmgr_device_info_storage.h"
25 #include "dtbschedmgr_log.h"
26 #include "ipc_skeleton.h"
27 #include "iservice_registry.h"
28 #include "mission/mission_changed_notify.h"
29 #include "mission/mission_constant.h"
30 #include "mission/mission_info_converter.h"
31 #include "mission/snapshot_converter.h"
32 #include "nlohmann/json.hpp"
33 #include "string_ex.h"
34 #include "system_ability_definition.h"
35 
36 namespace OHOS {
37 namespace DistributedSchedule {
38 namespace {
39 const std::string TAG = "DistributedSchedMissionManager";
40 constexpr size_t MAX_CACHED_ITEM = 10;
41 constexpr int32_t FIRST_APPLICATION_UID = 10000;
42 constexpr int32_t MULTIUSER_HAP_PER_USER_RANGE = 100000;
43 constexpr int32_t MAX_RETRY_TIMES = 15;
44 constexpr int32_t RETRY_DELAYED = 2000;
45 constexpr int32_t GET_FOREGROUND_SNAPSHOT_DELAY_TIME = 800; // ms
46 const std::string DELETE_DATA_STORAGE = "DeleteDataStorage";
47 constexpr int32_t DELETE_DATA_STORAGE_DELAYED = 60000; // ms
48 constexpr int32_t REGISTER_MISSION_LISTENER = 0;
49 constexpr int32_t UNREGISTER_MISSION_LISTENER = 1;
50 constexpr int64_t DELAY_TIME = 300;
51 const std::string INVAILD_LOCAL_DEVICE_ID = "-1";
52 }
53 namespace Mission {
54 constexpr int32_t GET_MAX_MISSIONS = 20;
55 } // Mission
56 using namespace std::chrono;
57 using namespace Constants::Mission;
58 using namespace OHOS::DistributedKv;
59 
60 IMPLEMENT_SINGLE_INSTANCE(DistributedSchedMissionManager);
61 
Init()62 void DistributedSchedMissionManager::Init()
63 {
64     listenerDeath_ = new ListenerDeathRecipient();
65     remoteDmsRecipient_ = new RemoteDmsDeathRecipient();
66     auto runner = AppExecFwk::EventRunner::Create("MissionManagerHandler");
67     missionHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
68     auto updateRunner = AppExecFwk::EventRunner::Create("UpdateHandler");
69     updateHandler_ = std::make_shared<AppExecFwk::EventHandler>(updateRunner);
70     missonChangeListener_ = new DistributedMissionChangeListener();
71     auto missionChangeRunner = AppExecFwk::EventRunner::Create("DistributedMissionChange");
72     missionChangeHandler_ = std::make_shared<AppExecFwk::EventHandler>(missionChangeRunner);
73 }
74 
GetMissionInfos(const std::string & deviceId,int32_t numMissions,std::vector<AAFwk::MissionInfo> & missionInfos)75 int32_t DistributedSchedMissionManager::GetMissionInfos(const std::string& deviceId,
76     int32_t numMissions, std::vector<AAFwk::MissionInfo>& missionInfos)
77 {
78     HILOGI("start!");
79     if (!AllowMissionUid(IPCSkeleton::GetCallingUid())) {
80         HILOGE("permission denied!");
81         return ERR_PERMISSION_DENIED;
82     }
83     if (!IsDeviceIdValidated(deviceId)) {
84         return INVALID_PARAMETERS_ERR;
85     }
86     if (numMissions <= 0) {
87         HILOGE("numMissions is illegal! numMissions:%{public}d", numMissions);
88         return INVALID_PARAMETERS_ERR;
89     }
90     std::vector<DstbMissionInfo> dstbMissionInfos;
91     int32_t ret = FetchCachedRemoteMissions(deviceId, numMissions, dstbMissionInfos);
92     if (ret != ERR_OK) {
93         HILOGE("FetchCachedRemoteMissions failed, ret = %{public}d", ret);
94         return ret;
95     }
96     return MissionInfoConverter::ConvertToMissionInfos(dstbMissionInfos, missionInfos);
97 }
98 
GetRemoteDms(const std::string & deviceId)99 sptr<IDistributedSched> DistributedSchedMissionManager::GetRemoteDms(const std::string& deviceId)
100 {
101     int64_t begin = GetTickCount();
102     if (deviceId.empty()) {
103         HILOGE("GetRemoteDms remoteDeviceId is empty");
104         return nullptr;
105     }
106     {
107         std::lock_guard<std::mutex> autoLock(remoteDmsLock_);
108         auto iter = remoteDmsMap_.find(deviceId);
109         if (iter != remoteDmsMap_.end()) {
110             auto object = iter->second;
111             if (object != nullptr) {
112                 HILOGI("[PerformanceTest] GetRemoteDms from cache spend %{public}" PRId64 " ms",
113                     GetTickCount() - begin);
114                 return object;
115             }
116         }
117     }
118     HILOGD("GetRemoteDms connect deviceid is %s", deviceId.c_str());
119     auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
120     if (samgr == nullptr) {
121         HILOGE("GetRemoteDms failed to connect to systemAbilityMgr!");
122         return nullptr;
123     }
124     auto object = samgr->CheckSystemAbility(DISTRIBUTED_SCHED_SA_ID, deviceId);
125     if (object == nullptr) {
126         HILOGE("GetRemoteDms failed to get dms for remote device:%{public}s!",
127             DnetworkAdapter::AnonymizeDeviceId(deviceId).c_str());
128         return nullptr;
129     }
130     auto ret = object->AddDeathRecipient(remoteDmsRecipient_);
131     HILOGD("GetRemoteDms AddDeathRecipient ret : %{public}d", ret);
132     sptr<IDistributedSched> remoteDmsObj = iface_cast<IDistributedSched>(object);
133     {
134         std::lock_guard<std::mutex> autoLock(remoteDmsLock_);
135         auto iter = remoteDmsMap_.find(deviceId);
136         if (iter != remoteDmsMap_.end()) {
137             iter->second->AsObject()->RemoveDeathRecipient(remoteDmsRecipient_);
138         }
139         remoteDmsMap_[deviceId] = remoteDmsObj;
140     }
141     HILOGI("[PerformanceTest] GetRemoteDms spend %{public}" PRId64 " ms", GetTickCount() - begin);
142     return remoteDmsObj;
143 }
144 
IsDeviceIdValidated(const std::string & deviceId)145 bool DistributedSchedMissionManager::IsDeviceIdValidated(const std::string& deviceId)
146 {
147     if (deviceId.empty()) {
148         HILOGE("IsDeviceIdValidated deviceId is empty!");
149         return false;
150     }
151     if (DtbschedmgrDeviceInfoStorage::GetInstance().GetDeviceInfoById(deviceId) == nullptr) {
152         HILOGW("IsDeviceIdValidated device offline.");
153         return false;
154     }
155 
156     return true;
157 }
158 
NotifyRemoteDied(const wptr<IRemoteObject> & remote)159 void DistributedSchedMissionManager::NotifyRemoteDied(const wptr<IRemoteObject>& remote)
160 {
161     if (distributedDataStorage_ == nullptr) {
162         HILOGE("DistributedDataStorage null!");
163         return;
164     }
165     distributedDataStorage_->NotifyRemoteDied(remote);
166 }
167 
InitDataStorage()168 int32_t DistributedSchedMissionManager::InitDataStorage()
169 {
170     if (distributedDataStorage_ == nullptr) {
171         distributedDataStorage_ = std::make_shared<DistributedDataStorage>();
172     }
173     if (!distributedDataStorage_->Init()) {
174         HILOGE("InitDataStorage DistributedDataStorage init failed!");
175         return ERR_NULL_OBJECT;
176     }
177     return ERR_NONE;
178 }
179 
StopDataStorage()180 int32_t DistributedSchedMissionManager::StopDataStorage()
181 {
182     if (distributedDataStorage_ == nullptr) {
183         HILOGE("StopDataStorage DistributedDataStorage null!");
184         return ERR_NULL_OBJECT;
185     }
186     if (!distributedDataStorage_->Stop()) {
187         HILOGE("StopDataStorage DistributedDataStorage stop failed!");
188         return ERR_NULL_OBJECT;
189     }
190     return ERR_NONE;
191 }
192 
StoreSnapshotInfo(const std::string & deviceId,int32_t missionId,const uint8_t * byteStream,size_t len)193 int32_t DistributedSchedMissionManager::StoreSnapshotInfo(const std::string& deviceId, int32_t missionId,
194     const uint8_t* byteStream, size_t len)
195 {
196     if (distributedDataStorage_ == nullptr) {
197         HILOGE("StoreSnapshotInfo DistributedDataStorage null!");
198         return ERR_NULL_OBJECT;
199     }
200     if (!distributedDataStorage_->Insert(deviceId, missionId, byteStream, len)) {
201         HILOGE("StoreSnapshotInfo DistributedDataStorage insert failed!");
202         return INVALID_PARAMETERS_ERR;
203     }
204     return ERR_NONE;
205 }
206 
RemoveSnapshotInfo(const std::string & deviceId,int32_t missionId)207 int32_t DistributedSchedMissionManager::RemoveSnapshotInfo(const std::string& deviceId, int32_t missionId)
208 {
209     if (distributedDataStorage_ == nullptr) {
210         HILOGE("RemoveSnapshotInfo DistributedDataStorage null!");
211         return ERR_NULL_OBJECT;
212     }
213     if (!distributedDataStorage_->Delete(deviceId, missionId)) {
214         HILOGE("RemoveSnapshotInfo DistributedDataStorage delete failed!");
215         return INVALID_PARAMETERS_ERR;
216     }
217     return ERR_NONE;
218 }
219 
GetRemoteSnapshotInfo(const std::string & deviceId,int32_t missionId)220 std::unique_ptr<Snapshot> DistributedSchedMissionManager::GetRemoteSnapshotInfo(const std::string& deviceId,
221     int32_t missionId)
222 {
223     if (!AllowMissionUid(IPCSkeleton::GetCallingUid())) {
224         HILOGE("GetRemoteSnapshotInfo permission denied!");
225         return nullptr;
226     }
227     std::string uuid = DtbschedmgrDeviceInfoStorage::GetInstance().GetUuidByNetworkId(deviceId);
228     if (uuid.empty()) {
229         HILOGE("GetRemoteSnapshotInfo uuid empty!");
230         return nullptr;
231     }
232     std::unique_ptr<Snapshot> snapshot = DequeueCachedSnapshotInfo(uuid, missionId);
233     if (snapshot != nullptr) {
234         return snapshot;
235     }
236     if (distributedDataStorage_ == nullptr) {
237         HILOGE("GetRemoteSnapshotInfo DistributedDataStorage null!");
238         return nullptr;
239     }
240     DistributedKv::Value value;
241     bool ret = distributedDataStorage_->Query(deviceId, missionId, value);
242     if (!ret) {
243         HILOGE("GetRemoteSnapshotInfo DistributedDataStorage query failed!");
244         return nullptr;
245     }
246     snapshot = Snapshot::Create(value.Data());
247     if (snapshot == nullptr) {
248         HILOGE("GetRemoteSnapshotInfo snapshot create failed!");
249         return nullptr;
250     }
251     return snapshot;
252 }
253 
GetRemoteMissionSnapshotInfo(const std::string & networkId,int32_t missionId,std::unique_ptr<AAFwk::MissionSnapshot> & missionSnapshot)254 int32_t DistributedSchedMissionManager::GetRemoteMissionSnapshotInfo(const std::string& networkId, int32_t missionId,
255     std::unique_ptr<AAFwk::MissionSnapshot>& missionSnapshot)
256 {
257     if (!AllowMissionUid(IPCSkeleton::GetCallingUid())) {
258         HILOGE("permission denied!");
259         return DMS_PERMISSION_DENIED;
260     }
261     std::string uuid = DtbschedmgrDeviceInfoStorage::GetInstance().GetUuidByNetworkId(networkId);
262     if (uuid.empty()) {
263         HILOGE("uuid is empty!");
264         return INVALID_PARAMETERS_ERR;
265     }
266     std::unique_ptr<Snapshot> snapshotPtr = DequeueCachedSnapshotInfo(uuid, missionId);
267     if (snapshotPtr != nullptr) {
268         HILOGI("get uuid = %{public}s + missionId = %{public}d snapshot from cache successful!",
269             DnetworkAdapter::AnonymizeDeviceId(uuid).c_str(), missionId);
270         SnapshotConverter::ConvertToMissionSnapshot(*snapshotPtr, missionSnapshot);
271         return ERR_NONE;
272     }
273     if (distributedDataStorage_ == nullptr) {
274         HILOGE("DistributedDataStorage null!");
275         return ERR_NULL_OBJECT;
276     }
277     DistributedKv::Value value;
278     bool ret = distributedDataStorage_->Query(networkId, missionId, value);
279     if (!ret) {
280         HILOGE("DistributedDataStorage query failed!");
281         return INVALID_PARAMETERS_ERR;
282     }
283     snapshotPtr = Snapshot::Create(value.Data());
284     if (snapshotPtr == nullptr) {
285         HILOGE("snapshot create failed!");
286         return ERR_NULL_OBJECT;
287     }
288     HILOGI("get uuid = %{public}s + missionId = %{public}d snapshot from DistributedDB successful!",
289         DnetworkAdapter::AnonymizeDeviceId(uuid).c_str(), missionId);
290     SnapshotConverter::ConvertToMissionSnapshot(*snapshotPtr, missionSnapshot);
291     return ERR_NONE;
292 }
293 
DeviceOnlineNotify(const std::string & deviceId)294 void DistributedSchedMissionManager::DeviceOnlineNotify(const std::string& deviceId)
295 {
296     if (deviceId.empty()) {
297         HILOGW("DeviceOnlineNotify deviceId empty!");
298         return;
299     }
300 
301     std::string uuid = DtbschedmgrDeviceInfoStorage::GetInstance().GetUuidByNetworkId(deviceId);
302     if (missionHandler_ != nullptr) {
303         HILOGI("DeviceOnlineNotify RemoveTask");
304         missionHandler_->RemoveTask(DELETE_DATA_STORAGE + uuid);
305     }
306 }
307 
DeviceOfflineNotify(const std::string & deviceId)308 void DistributedSchedMissionManager::DeviceOfflineNotify(const std::string& deviceId)
309 {
310     if (deviceId.empty()) {
311         HILOGW("DeviceOfflineNotify deviceId empty!");
312         return;
313     }
314     StopSyncMissionsFromRemote(deviceId);
315     CleanMissionResources(deviceId);
316     {
317         std::lock_guard<std::mutex> autoLock(remoteDmsLock_);
318         auto iter = remoteDmsMap_.find(deviceId);
319         if (iter != remoteDmsMap_.end()) {
320             iter->second->AsObject()->RemoveDeathRecipient(remoteDmsRecipient_);
321             remoteDmsMap_.erase(iter);
322         }
323     }
324     int64_t begin = GetTickCount();
325     {
326         std::lock_guard<std::mutex> autoLock(osdSwitchLock_);
327         connCapSupportOsdMap_.erase(deviceId);
328     }
329     HILOGI("DeviceOfflineNotify erase value for deviceId: %{public}s spend %{public}" PRId64 " ms",
330         DnetworkAdapter::AnonymizeDeviceId(deviceId).c_str(), GetTickCount() - begin);
331 }
332 
UpdateConnCapSupportOsd(const std::string & deviceId)333 void DistributedSchedMissionManager::UpdateConnCapSupportOsd(const std::string& deviceId)
334 {
335     HILOGI("UpdateConnCapSupportOsd begin, deviceId is %{public}s",
336         DnetworkAdapter::AnonymizeDeviceId(deviceId).c_str());
337     bool isSupportOsd = IsConnCapSupportOsd(deviceId);
338     bool needNotifyChanged = false;
339     int32_t switchVal = MISSION_OSD_NOT_SUPPORTED;
340     {
341         std::string uuid = DtbschedmgrDeviceInfoStorage::GetInstance().GetUuidByNetworkId(deviceId);
342         std::lock_guard<std::mutex> autoLock(osdSwitchLock_);
343         auto iter = connCapSupportOsdMap_.find(deviceId);
344         if (iter != connCapSupportOsdMap_.end()) {
345             needNotifyChanged = (iter->second != isSupportOsd);
346         } else {
347             needNotifyChanged = true;
348         }
349         connCapSupportOsdMap_[deviceId] = isSupportOsd;
350         HILOGI("UpdateConnCapSupportOsd end, isSupportOsd is %{public}d", isSupportOsd);
351         auto iterOsdSwitch = osdSwitchValueMap_.find(uuid);
352         if (isSupportOsd && iterOsdSwitch == osdSwitchValueMap_.end()) {
353             HILOGI("UpdateConnCapSupportOsd can not find osd switch value!");
354             return;
355         }
356         switchVal = isSupportOsd ? iterOsdSwitch->second : MISSION_OSD_NOT_SUPPORTED;
357     }
358     NotifyOsdSwitchChanged(needNotifyChanged, deviceId, switchVal);
359 }
360 
IsConnCapSupportOsd(const std::string & deviceId)361 bool DistributedSchedMissionManager::IsConnCapSupportOsd(const std::string& deviceId)
362 {
363     return true;
364 }
365 
DeleteDataStorage(const std::string & deviceId,bool isDelayed)366 void DistributedSchedMissionManager::DeleteDataStorage(const std::string& deviceId, bool isDelayed)
367 {
368     if (distributedDataStorage_ == nullptr) {
369         HILOGE("DeleteDataStorage DistributedDataStorage null!");
370         return;
371     }
372     std::string uuid = DtbschedmgrDeviceInfoStorage::GetInstance().GetUuidByNetworkId(deviceId);
373     auto callback = [this, uuid, deviceId]() {
374         if (!distributedDataStorage_->FuzzyDelete(deviceId)) {
375             HILOGE("DeleteDataStorage storage delete failed!");
376         } else {
377             HILOGI("DeleteDataStorage storage delete successfully!");
378         }
379     };
380     if (isDelayed) {
381         if (missionHandler_ != nullptr) {
382             HILOGI("DeleteDataStorage PostTask");
383             missionHandler_->PostTask(callback, DELETE_DATA_STORAGE + uuid, DELETE_DATA_STORAGE_DELAYED);
384         }
385     } else {
386         if (missionHandler_ != nullptr) {
387             HILOGI("DeleteDataStorage RemoveTask");
388             missionHandler_->RemoveTask(DELETE_DATA_STORAGE + uuid);
389         }
390         callback();
391     }
392 }
393 
RegisterMissionListener(const std::u16string & devId,const sptr<IRemoteObject> & listener)394 int32_t DistributedSchedMissionManager::RegisterMissionListener(const std::u16string& devId,
395     const sptr<IRemoteObject>& listener)
396 {
397     std::string uuid = DtbschedmgrDeviceInfoStorage::GetInstance().GetUuidByNetworkId(Str16ToStr8(devId));
398     if (missionHandler_ != nullptr) {
399         HILOGI("RemoveTask");
400         missionHandler_->RemoveTask(DELETE_DATA_STORAGE + uuid);
401     }
402     if (listener == nullptr) {
403         return INVALID_PARAMETERS_ERR;
404     }
405     if (!AllowMissionUid(IPCSkeleton::GetCallingUid())) {
406         HILOGE("permission denied!");
407         return DMS_PERMISSION_DENIED;
408     }
409     std::string localDeviceId;
410     std::string remoteDeviceId = Str16ToStr8(devId);
411     if (!DtbschedmgrDeviceInfoStorage::GetInstance().GetLocalDeviceId(localDeviceId)
412         || localDeviceId == remoteDeviceId) {
413         HILOGE("check deviceId failed!");
414         return INVALID_PARAMETERS_ERR;
415     }
416     {
417         std::lock_guard<std::mutex> autoLock(listenDeviceLock_);
418         auto& listenerInfo = listenDeviceMap_[devId];
419         if (!listenerInfo.Emplace(listener)) {
420             HILOGW("RegisterSyncListener listener has already inserted!");
421             return ERR_NONE;
422         }
423         bool ret = listener->AddDeathRecipient(listenerDeath_);
424         if (!ret) {
425             HILOGW("RegisterSyncListener AddDeathRecipient failed!");
426         }
427         if (listenerInfo.Size() > 1) {
428             HILOGI("RegisterMissionListener not notify remote DMS!");
429             return ERR_NONE;
430         }
431     }
432 
433     if (CheckOsdSwitch(remoteDeviceId) != MISSION_OSD_ENABLED) {
434         NotifyOsdSwitchChanged(true, remoteDeviceId, MISSION_OSD_NOT_SUPPORTED);
435         return MISSION_OSD_NOT_SUPPORTED;
436     }
437     return ERR_NONE;
438 }
439 
StartSyncRemoteMissions(const std::string & dstDevId,const std::string & localDevId)440 int32_t DistributedSchedMissionManager::StartSyncRemoteMissions(const std::string& dstDevId,
441     const std::string& localDevId)
442 {
443     std::u16string devId = Str8ToStr16(dstDevId);
444     {
445         std::lock_guard<std::mutex> autoLock(listenDeviceLock_);
446         auto iterItem = listenDeviceMap_.find(devId);
447         if (iterItem == listenDeviceMap_.end()) {
448             return ERR_NONE;
449         }
450         bool callFlag = iterItem->second.called;
451         if (callFlag) {
452             HILOGI("StartSyncRemoteMissions already called!");
453             return ERR_NONE;
454         }
455     }
456     sptr<IDistributedSched> remoteDms = GetRemoteDms(dstDevId);
457     if (remoteDms == nullptr) {
458         HILOGE("get remoteDms failed!");
459         RetryStartSyncRemoteMissions(dstDevId, localDevId, 0);
460         return GET_REMOTE_DMS_FAIL;
461     }
462     int32_t ret = StartSyncRemoteMissions(dstDevId, remoteDms);
463     if (ret == ERR_NONE) {
464         std::lock_guard<std::mutex> autoLock(listenDeviceLock_);
465         auto iterItem = listenDeviceMap_.find(devId);
466         if (iterItem != listenDeviceMap_.end()) {
467             iterItem->second.called = true;
468         }
469     }
470     return ret;
471 }
472 
StartSyncRemoteMissions(const std::string & dstDevId,const sptr<IDistributedSched> & remoteDms)473 int32_t DistributedSchedMissionManager::StartSyncRemoteMissions(const std::string& dstDevId,
474     const sptr<IDistributedSched>& remoteDms)
475 {
476     std::vector<DstbMissionInfo> missionInfos;
477     CallerInfo callerInfo;
478     if (!GenerateCallerInfo(callerInfo)) {
479         return GET_LOCAL_DEVICE_ERR;
480     }
481     int64_t begin = GetTickCount();
482     int32_t ret = remoteDms->StartSyncMissionsFromRemote(callerInfo, missionInfos);
483     HILOGI("[PerformanceTest] StartSyncMissionsFromRemote ret:%{public}d, spend %{public}" PRId64 " ms",
484         ret, GetTickCount() - begin);
485     if (ret == ERR_NONE) {
486         RebornMissionCache(dstDevId, missionInfos);
487     }
488     return ret;
489 }
490 
UnRegisterMissionListener(const std::u16string & devId,const sptr<IRemoteObject> & listener)491 int32_t DistributedSchedMissionManager::UnRegisterMissionListener(const std::u16string& devId,
492     const sptr<IRemoteObject>& listener)
493 {
494     if (listener == nullptr) {
495         return INVALID_PARAMETERS_ERR;
496     }
497     if (!AllowMissionUid(IPCSkeleton::GetCallingUid())) {
498         HILOGE("permission denied!");
499         return DMS_PERMISSION_DENIED;
500     }
501     std::string localDeviceId;
502     if (!DtbschedmgrDeviceInfoStorage::GetInstance().GetLocalDeviceId(localDeviceId)
503         || localDeviceId == Str16ToStr8(devId)) {
504         HILOGE("check deviceId fail");
505         return INVALID_PARAMETERS_ERR;
506     }
507     {
508         std::lock_guard<std::mutex> autoLock(listenDeviceLock_);
509         auto iterItem = listenDeviceMap_.find(devId);
510         if (iterItem == listenDeviceMap_.end()) {
511             return ERR_NONE;
512         }
513         auto& listenerInfo = iterItem->second;
514         auto ret = listenerInfo.Find(listener);
515         if (!ret) {
516             HILOGI("listener not registered!");
517             return ERR_NONE;
518         }
519         listener->RemoveDeathRecipient(listenerDeath_);
520         listenerInfo.Erase(listener);
521         if (!listenerInfo.Empty()) {
522             return ERR_NONE;
523         }
524         listenDeviceMap_.erase(iterItem);
525     }
526     return ERR_NONE;
527 }
528 
CleanMissionResources(const std::string & dstDevId)529 void DistributedSchedMissionManager::CleanMissionResources(const std::string& dstDevId)
530 {
531     {
532         std::lock_guard<std::mutex> autoLock(listenDeviceLock_);
533         auto iterDevice = listenDeviceMap_.find(Str8ToStr16(dstDevId));
534         if (iterDevice == listenDeviceMap_.end()) {
535             return;
536         }
537         auto& listenerInfo = iterDevice->second;
538         for (sptr<IRemoteObject> listener : listenerInfo.listenerSet) {
539             if (listener != nullptr) {
540                 listener->RemoveDeathRecipient(listenerDeath_);
541             }
542         }
543         listenDeviceMap_.erase(iterDevice);
544     }
545     StopSyncRemoteMissions(dstDevId, true);
546 }
547 
StopSyncRemoteMissions(const std::string & dstDevId,bool offline,bool exit)548 int32_t DistributedSchedMissionManager::StopSyncRemoteMissions(const std::string& dstDevId,
549     bool offline, bool exit)
550 {
551     CleanMissionCache(dstDevId);
552     DeleteCachedSnapshotInfo(dstDevId);
553     DeleteDataStorage(dstDevId, true);
554 
555     if (offline) {
556         return ERR_NONE;
557     }
558     sptr<IDistributedSched> remoteDms = GetRemoteDms(dstDevId);
559     if (remoteDms == nullptr) {
560         HILOGE("DMS get remoteDms failed");
561         return GET_REMOTE_DMS_FAIL;
562     }
563 
564     CallerInfo callerInfo;
565     if (!GenerateCallerInfo(callerInfo)) {
566         return GET_LOCAL_DEVICE_ERR;
567     }
568     int64_t begin = GetTickCount();
569     int32_t ret = remoteDms->StopSyncMissionsFromRemote(callerInfo);
570     HILOGI("[PerformanceTest] ret:%d, spend %{public}" PRId64 " ms", ret, GetTickCount() - begin);
571     return ret;
572 }
573 
StartSyncRemoteMissions(const std::string & dstDevId,bool fixConflict,int64_t tag)574 int32_t DistributedSchedMissionManager::StartSyncRemoteMissions(const std::string& dstDevId, bool fixConflict,
575     int64_t tag)
576 {
577     std::string localDeviceId;
578     if (!DtbschedmgrDeviceInfoStorage::GetInstance().GetLocalDeviceId(localDeviceId)
579         || (dstDevId == localDeviceId)) {
580         HILOGE("check deviceId fail");
581         return INVALID_PARAMETERS_ERR;
582     }
583     HILOGI("begin, dstDevId is %{public}s, local deviceId is %{public}s",
584         DnetworkAdapter::AnonymizeDeviceId(dstDevId).c_str(),
585         DnetworkAdapter::AnonymizeDeviceId(localDeviceId).c_str());
586     if (dstDevId != INVAILD_LOCAL_DEVICE_ID && dstDevId != localDeviceId &&
587         CheckOsdSwitch(dstDevId) != MISSION_OSD_ENABLED) {
588         NotifyOsdSwitchChanged(true, dstDevId, MISSION_OSD_NOT_SUPPORTED);
589         return MISSION_OSD_NOT_SUPPORTED;
590     }
591     auto ret = StartSyncRemoteMissions(dstDevId, localDeviceId);
592     if (ret != ERR_NONE) {
593         HILOGE("StartSyncRemoteMissions failed, %{public}d", ret);
594         return ret;
595     }
596     return ERR_NONE;
597 }
598 
StartSyncMissionsFromRemote(const CallerInfo & callerInfo,std::vector<DstbMissionInfo> & missionInfos)599 int32_t DistributedSchedMissionManager::StartSyncMissionsFromRemote(const CallerInfo& callerInfo,
600     std::vector<DstbMissionInfo>& missionInfos)
601 {
602     auto deviceId = callerInfo.sourceDeviceId;
603     HILOGD("remote version is %{public}d!", callerInfo.dmsVersion);
604     {
605         std::lock_guard<std::mutex> autoLock(remoteSyncDeviceLock_);
606         remoteSyncDeviceSet_.emplace(deviceId);
607         if (remoteSyncDeviceSet_.size() == 1) {
608             DistributedSchedAdapter::GetInstance().OnOsdEventOccur(REGISTER_MISSION_LISTENER);
609         }
610     }
611     if (GetOsdSwitchValueFromRemote() != MISSION_OSD_ENABLED) {
612         HILOGI("osd function is disable!");
613         return ERR_NONE;
614     }
615     int32_t result = DistributedSchedAdapter::GetInstance().GetLocalMissionInfos(Mission::GET_MAX_MISSIONS,
616         missionInfos);
617     auto func = [this, missionInfos]() {
618         HILOGD("RegisterMissionListener called.");
619         if (!isRegMissionChange_) {
620             int32_t ret = DistributedSchedAdapter::GetInstance().RegisterMissionListener(missonChangeListener_);
621             if (ret == ERR_OK) {
622                 isRegMissionChange_ = true;
623             }
624             InitAllSnapshots(missionInfos);
625         }
626     };
627     if (!missionHandler_->PostTask(func)) {
628         HILOGE("post RegisterMissionListener and InitAllSnapshots Task failed");
629     }
630     return result;
631 }
632 
StopSyncMissionsFromRemote(const std::string & deviceId)633 void DistributedSchedMissionManager::StopSyncMissionsFromRemote(const std::string& deviceId)
634 {
635     HILOGD(" %{private}s!", deviceId.c_str());
636     {
637         std::lock_guard<std::mutex> autoLock(remoteSyncDeviceLock_);
638         remoteSyncDeviceSet_.erase(deviceId);
639         if (remoteSyncDeviceSet_.empty()) {
640             auto func = [this]() {
641                 int32_t ret = DistributedSchedAdapter::GetInstance().UnRegisterMissionListener(missonChangeListener_);
642                 if (ret == ERR_OK) {
643                     DistributedSchedAdapter::GetInstance().OnOsdEventOccur(UNREGISTER_MISSION_LISTENER);
644                     isRegMissionChange_ = false;
645                 }
646             };
647             if (!missionHandler_->PostTask(func)) {
648                 HILOGE("post UnRegisterMissionListener Task failed");
649             }
650         }
651     }
652 }
653 
needSyncDevice(const std::string & deviceId)654 bool DistributedSchedMissionManager::needSyncDevice(const std::string& deviceId)
655 {
656     std::lock_guard<std::mutex> autoLock(remoteSyncDeviceLock_);
657     if (remoteSyncDeviceSet_.count(deviceId) == 0) {
658         return false;
659     }
660     return true;
661 }
662 
HasSyncListener(const std::string & networkId)663 bool DistributedSchedMissionManager::HasSyncListener(const std::string& networkId)
664 {
665     std::lock_guard<std::mutex> autoLock(listenDeviceLock_);
666     auto iter = listenDeviceMap_.find(Str8ToStr16(networkId));
667     if (iter != listenDeviceMap_.end()) {
668         return iter->second.called;
669     }
670     return false;
671 }
672 
NotifySnapshotChanged(const std::string & networkId,int32_t missionId)673 void DistributedSchedMissionManager::NotifySnapshotChanged(const std::string& networkId, int32_t missionId)
674 {
675     std::u16string u16DevId = Str8ToStr16(networkId);
676     std::lock_guard<std::mutex> autoLock(listenDeviceLock_);
677     auto iter = listenDeviceMap_.find(u16DevId);
678     if (iter == listenDeviceMap_.end()) {
679         return;
680     }
681     auto& listenerInfo = iter->second;
682     for (auto& listener : listenerInfo.listenerSet) {
683         MissionChangedNotify::NotifySnapshot(listener, u16DevId, missionId);
684     }
685 }
686 
OnRemoteDied(const wptr<IRemoteObject> & remote)687 void DistributedSchedMissionManager::OnRemoteDied(const wptr<IRemoteObject>& remote)
688 {
689     HILOGD("OnRemoteDied!");
690     sptr<IRemoteObject> listener = remote.promote();
691     if (listener == nullptr) {
692         return;
693     }
694     auto remoteDiedFunc = [this, listener]() {
695         OnMissionListenerDied(listener);
696     };
697     if (missionHandler_ != nullptr) {
698         missionHandler_->PostTask(remoteDiedFunc);
699     }
700 }
701 
OnRemoteDied(const wptr<IRemoteObject> & remote)702 void DistributedSchedMissionManager::ListenerDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
703 {
704     DistributedSchedMissionManager::GetInstance().OnRemoteDied(remote);
705 }
706 
EnqueueCachedSnapshotInfo(const std::string & deviceId,int32_t missionId,std::unique_ptr<Snapshot> snapshot)707 void DistributedSchedMissionManager::EnqueueCachedSnapshotInfo(const std::string& deviceId, int32_t missionId,
708     std::unique_ptr<Snapshot> snapshot)
709 {
710     if (deviceId.empty() || snapshot == nullptr) {
711         HILOGW("EnqueueCachedSnapshotInfo invalid input param!");
712         return;
713     }
714     std::lock_guard<std::mutex> autoLock(listenDeviceLock_);
715     std::string keyInfo = GenerateKeyInfo(deviceId, missionId);
716     auto iter = cachedSnapshotInfos_.find(keyInfo);
717     if (iter != cachedSnapshotInfos_.end()) {
718         if (snapshot->GetCreatedTime() < iter->second->GetCreatedTime()) {
719             return;
720         }
721     }
722 
723     if (cachedSnapshotInfos_.size() == MAX_CACHED_ITEM) {
724         int64_t oldest = -1;
725         auto iterOldest = cachedSnapshotInfos_.end();
726         for (auto iterItem = cachedSnapshotInfos_.begin(); iterItem != cachedSnapshotInfos_.end(); ++iterItem) {
727             if (oldest == -1 || iterItem->second->GetLastAccessTime() < oldest) {
728                 oldest = iterItem->second->GetLastAccessTime();
729                 iterOldest = iterItem;
730             }
731         }
732         if (iterOldest != cachedSnapshotInfos_.end()) {
733             cachedSnapshotInfos_.erase(iterOldest);
734         }
735     }
736     cachedSnapshotInfos_[keyInfo] = std::move(snapshot);
737 }
738 
DequeueCachedSnapshotInfo(const std::string & deviceId,int32_t missionId)739 std::unique_ptr<Snapshot> DistributedSchedMissionManager::DequeueCachedSnapshotInfo(const std::string& deviceId,
740     int32_t missionId)
741 {
742     if (deviceId.empty()) {
743         HILOGW("DequeueCachedSnapshotInfo invalid input param!");
744         return nullptr;
745     }
746     std::lock_guard<std::mutex> autoLock(listenDeviceLock_);
747     auto iter = cachedSnapshotInfos_.find(GenerateKeyInfo(deviceId, missionId));
748     if (iter != cachedSnapshotInfos_.end()) {
749         std::unique_ptr<Snapshot> snapshot = std::move(iter->second);
750         snapshot->UpdateLastAccessTime(GetTickCount());
751         iter->second = nullptr;
752         cachedSnapshotInfos_.erase(iter);
753         return snapshot;
754     }
755     return nullptr;
756 }
757 
DeleteCachedSnapshotInfo(const std::string & networkId)758 void DistributedSchedMissionManager::DeleteCachedSnapshotInfo(const std::string& networkId)
759 {
760     if (networkId.empty()) {
761         return;
762     }
763     std::string uuid = DtbschedmgrDeviceInfoStorage::GetInstance().GetUuidByNetworkId(networkId);
764     if (uuid.empty()) {
765         return;
766     }
767     std::lock_guard<std::mutex> autoLock(listenDeviceLock_);
768     auto iter = cachedSnapshotInfos_.begin();
769     while (iter != cachedSnapshotInfos_.end()) {
770         if (iter->first.find(uuid) != std::string::npos) {
771             iter = cachedSnapshotInfos_.erase(iter);
772         } else {
773             ++iter;
774         }
775     }
776 }
777 
FetchCachedRemoteMissions(const std::string & srcId,int32_t numMissions,std::vector<DstbMissionInfo> & missionInfos)778 int32_t DistributedSchedMissionManager::FetchCachedRemoteMissions(const std::string& srcId, int32_t numMissions,
779     std::vector<DstbMissionInfo>& missionInfos)
780 {
781     std::string uuid = DtbschedmgrDeviceInfoStorage::GetInstance().GetUuidByNetworkId(srcId);
782     if (uuid.empty()) {
783         HILOGE("uuid empty!");
784         return INVALID_PARAMETERS_ERR;
785     }
786     std::lock_guard<std::mutex> autoLock(remoteMissionInfosLock_);
787     auto iter = deviceMissionInfos_.find(uuid);
788     if (iter == deviceMissionInfos_.end()) {
789         HILOGE("can not find uuid, deviceId: %{public}s!",
790             DnetworkAdapter::AnonymizeDeviceId(srcId).c_str());
791         return ERR_NULL_OBJECT;
792     }
793 
794     // get at most numMissions missions
795     int32_t actualNums = static_cast<int32_t>((iter->second).size());
796     if (actualNums < 0) {
797         HILOGE("invalid size!");
798         return ERR_NULL_OBJECT;
799     }
800     missionInfos.assign((iter->second).begin(),
801         (actualNums > numMissions) ? (iter->second).begin() + numMissions : (iter->second).end());
802     return ERR_NONE;
803 }
804 
RebornMissionCache(const std::string & deviceId,const std::vector<DstbMissionInfo> & missionInfos)805 void DistributedSchedMissionManager::RebornMissionCache(const std::string& deviceId,
806     const std::vector<DstbMissionInfo>& missionInfos)
807 {
808     HILOGI("start! deviceId is %{public}s",
809         DnetworkAdapter::AnonymizeDeviceId(deviceId).c_str());
810     std::string uuid = DtbschedmgrDeviceInfoStorage::GetInstance().GetUuidByNetworkId(deviceId);
811     if (uuid.empty()) {
812         HILOGE("uuid empty!");
813         return;
814     }
815     {
816         std::lock_guard<std::mutex> autoLock(remoteMissionInfosLock_);
817         deviceMissionInfos_[uuid] = missionInfos;
818     }
819     HILOGI("RebornMissionCache end!");
820 }
821 
CleanMissionCache(const std::string & deviceId)822 void DistributedSchedMissionManager::CleanMissionCache(const std::string& deviceId)
823 {
824     HILOGI("CleanMissionCache start! deviceId is %{public}s",
825         DnetworkAdapter::AnonymizeDeviceId(deviceId).c_str());
826     std::string uuid = DtbschedmgrDeviceInfoStorage::GetInstance().GetUuidByNetworkId(deviceId);
827     if (uuid.empty()) {
828         HILOGE("CleanMissionCache uuid empty!");
829         return;
830     }
831     {
832         std::lock_guard<std::mutex> autoLock(remoteMissionInfosLock_);
833         deviceMissionInfos_.erase(uuid);
834     }
835     HILOGI("CleanMissionCache end!");
836 }
837 
NotifyMissionsChangedFromRemote(const CallerInfo & callerInfo,const std::vector<DstbMissionInfo> & missionInfos)838 int32_t DistributedSchedMissionManager::NotifyMissionsChangedFromRemote(const CallerInfo& callerInfo,
839     const std::vector<DstbMissionInfo>& missionInfos)
840 {
841     HILOGI("NotifyMissionsChangedFromRemote version is %{public}d!", callerInfo.dmsVersion);
842     std::u16string u16DevId = Str8ToStr16(callerInfo.sourceDeviceId);
843     RebornMissionCache(callerInfo.sourceDeviceId, missionInfos);
844     {
845         HILOGI("NotifyMissionsChangedFromRemote notify mission start!");
846         std::lock_guard<std::mutex> autoLock(listenDeviceLock_);
847         auto iter = listenDeviceMap_.find(u16DevId);
848         if (iter == listenDeviceMap_.end()) {
849             HILOGE("NotifyMissionsChangedFromRemote notify mission no listener!");
850             return INVALID_PARAMETERS_ERR;
851         }
852         auto& listenerSet = iter->second.listenerSet;
853         auto notifyChanged = [listenerSet, u16DevId] () {
854             for (const auto& listener : listenerSet) {
855                 MissionChangedNotify::NotifyMissionsChanged(listener, u16DevId);
856             }
857         };
858         if (missionHandler_ != nullptr) {
859             missionHandler_->PostTask(notifyChanged);
860             HILOGI("NotifyMissionsChangedFromRemote end!");
861             return ERR_NONE;
862         }
863     }
864     return INVALID_PARAMETERS_ERR;
865 }
866 
NotifyLocalMissionsChanged()867 void DistributedSchedMissionManager::NotifyLocalMissionsChanged()
868 {
869     auto func = [this]() {
870         HILOGI("NotifyLocalMissionsChanged");
871         std::vector<DstbMissionInfo> missionInfos;
872         int32_t ret = DistributedSchedAdapter::GetInstance().GetLocalMissionInfos(Mission::GET_MAX_MISSIONS,
873             missionInfos);
874         if (ret == ERR_OK) {
875             int32_t result = NotifyMissionsChangedToRemote(missionInfos);
876             HILOGI("NotifyMissionsChangedToRemote result = %{public}d", result);
877         }
878     };
879     if (!missionChangeHandler_->PostTask(func)) {
880         HILOGE("postTask failed");
881     }
882 }
883 
NotifyMissionSnapshotCreated(int32_t missionId)884 void DistributedSchedMissionManager::NotifyMissionSnapshotCreated(int32_t missionId)
885 {
886     auto func = [this, missionId]() {
887         HILOGD("called.");
888         ErrCode errCode = MissionSnapshotChanged(missionId);
889         if (errCode != ERR_OK) {
890             HILOGE("mission snapshot changed failed, missionId=%{public}d, errCode=%{public}d", missionId, errCode);
891         }
892     };
893     if (!missionChangeHandler_->PostTask(func, GET_FOREGROUND_SNAPSHOT_DELAY_TIME)) {
894         HILOGE("post MissionSnapshotChanged delay Task failed");
895     }
896 }
897 
NotifyMissionSnapshotChanged(int32_t missionId)898 void DistributedSchedMissionManager::NotifyMissionSnapshotChanged(int32_t missionId)
899 {
900     auto func = [this, missionId]() {
901         HILOGD("called.");
902         ErrCode errCode = MissionSnapshotChanged(missionId);
903         if (errCode != ERR_OK) {
904             HILOGE("mission snapshot changed failed, missionId=%{public}d, errCode=%{public}d", missionId, errCode);
905         }
906     };
907     if (!missionChangeHandler_->PostTask(func)) {
908         HILOGE("post MissionSnapshotChanged Task failed");
909     }
910 }
911 
NotifyMissionSnapshotDestroyed(int32_t missionId)912 void DistributedSchedMissionManager::NotifyMissionSnapshotDestroyed(int32_t missionId)
913 {
914     auto func = [this, missionId]() {
915         HILOGD("called.");
916         ErrCode errCode = MissionSnapshotDestroyed(missionId);
917         if (errCode != ERR_OK) {
918             HILOGE("mission snapshot removed failed, missionId=%{public}d, errCode=%{public}d", missionId, errCode);
919         }
920     };
921     if (!missionChangeHandler_->PostTask(func)) {
922         HILOGE("post MissionSnapshotDestroyed Task failed");
923     }
924 }
925 
NotifyMissionsChangedToRemote(const std::vector<DstbMissionInfo> & missionInfos)926 int32_t DistributedSchedMissionManager::NotifyMissionsChangedToRemote(const std::vector<DstbMissionInfo>& missionInfos)
927 {
928     CallerInfo callerInfo;
929     if (!GenerateCallerInfo(callerInfo)) {
930         return GET_LOCAL_DEVICE_ERR;
931     }
932     std::lock_guard<std::mutex> autoLock(remoteSyncDeviceLock_);
933     for (const auto& destDeviceId : remoteSyncDeviceSet_) {
934         auto handler = FetchDeviceHandler(destDeviceId);
935         if (handler == nullptr) {
936             HILOGE("NotifyMissionsChangedToRemote fetch handler failed!");
937             continue;
938         }
939         auto callback = [destDeviceId, missionInfos, callerInfo, this] () {
940             NotifyMissionsChangedToRemoteInner(destDeviceId, missionInfos, callerInfo);
941         };
942         if (!handler->PostTask(callback)) {
943             HILOGE("NotifyMissionsChangedToRemote PostTask failed!");
944             return ERR_NULL_OBJECT;
945         }
946     }
947 
948     return ERR_NONE;
949 }
950 
NotifyMissionsChangedToRemoteInner(const std::string & deviceId,const std::vector<DstbMissionInfo> & missionInfos,const CallerInfo & callerInfo)951 void DistributedSchedMissionManager::NotifyMissionsChangedToRemoteInner(const std::string& deviceId,
952     const std::vector<DstbMissionInfo>& missionInfos, const CallerInfo& callerInfo)
953 {
954     sptr<IDistributedSched> remoteDms = GetRemoteDms(deviceId);
955     if (remoteDms == nullptr) {
956         HILOGE("NotifyMissionsChangedToRemote DMS get remoteDms failed");
957         return;
958     }
959     int64_t begin = GetTickCount();
960     int32_t result = remoteDms->NotifyMissionsChangedFromRemote(missionInfos, callerInfo);
961     HILOGI("[PerformanceTest] NotifyMissionsChangedFromRemote ret:%{public}d, spend %{public}" PRId64 " ms",
962         result, GetTickCount() - begin);
963 }
964 
GenerateCallerInfo(CallerInfo & callerInfo)965 bool DistributedSchedMissionManager::GenerateCallerInfo(CallerInfo& callerInfo)
966 {
967     std::string localUuid;
968     if (!DtbschedmgrDeviceInfoStorage::GetInstance().GetLocalDeviceId(localUuid)) {
969         HILOGE("get local uuid failed!");
970         return false;
971     }
972     callerInfo.uid = IPCSkeleton::GetCallingUid();
973     callerInfo.pid = IPCSkeleton::GetCallingPid();
974     callerInfo.callerType = CALLER_TYPE_HARMONY;
975     callerInfo.sourceDeviceId = localUuid;
976     callerInfo.dmsVersion = VERSION;
977     return true;
978 }
979 
FetchDeviceHandler(const std::string & deviceId)980 std::shared_ptr<AppExecFwk::EventHandler> DistributedSchedMissionManager::FetchDeviceHandler(
981     const std::string& deviceId)
982 {
983     if (!IsDeviceIdValidated(deviceId)) {
984         HILOGW("FetchDeviceHandler device:%{public}s offline.",
985             DnetworkAdapter::AnonymizeDeviceId(deviceId).c_str());
986         return nullptr;
987     }
988 
989     std::string uuid = DtbschedmgrDeviceInfoStorage::GetInstance().GetUuidByNetworkId(deviceId);
990     if (uuid.empty()) {
991         HILOGE("FetchDeviceHandler uuid empty!");
992         return nullptr;
993     }
994 
995     auto iter = deviceHandle_.find(uuid);
996     if (iter != deviceHandle_.end()) {
997         return iter->second;
998     }
999 
1000     auto anonyUuid = DnetworkAdapter::AnonymizeDeviceId(uuid);
1001     auto runner = AppExecFwk::EventRunner::Create(anonyUuid + "_MissionN");
1002     auto handler = std::make_shared<AppExecFwk::EventHandler>(runner);
1003     if (handler != nullptr) {
1004         deviceHandle_.emplace(uuid, handler);
1005     }
1006     return handler;
1007 }
1008 
GetConnCapSupportOsd(const std::string & deviceId)1009 bool DistributedSchedMissionManager::GetConnCapSupportOsd(const std::string& deviceId)
1010 {
1011     HILOGI("GetConnCapSupportOsd begin, deviceId is %{public}s",
1012         DnetworkAdapter::AnonymizeDeviceId(deviceId).c_str());
1013     std::lock_guard<std::mutex> autoLock(osdSwitchLock_);
1014     return GetConnCapSupportOsdInnerLocked(deviceId);
1015 }
1016 
GetConnCapSupportOsdInnerLocked(const std::string & deviceId)1017 bool DistributedSchedMissionManager::GetConnCapSupportOsdInnerLocked(const std::string& deviceId)
1018 {
1019     bool connCapSupportOsd = false;
1020     auto iter = connCapSupportOsdMap_.find(deviceId);
1021     if (iter != connCapSupportOsdMap_.end()) {
1022         connCapSupportOsd = iter->second;
1023         HILOGI("CheckSupportOsd find connCapSupportOsd is %{public}d", connCapSupportOsd);
1024     } else {
1025         HILOGI("CheckSupportOsd can not find connCapSupportOsd, try to get it!");
1026         connCapSupportOsd = IsConnCapSupportOsd(deviceId);
1027         connCapSupportOsdMap_[deviceId] = connCapSupportOsd;
1028     }
1029     return connCapSupportOsd;
1030 }
1031 
PreCheckSupportOsd(const std::string & deviceId)1032 bool DistributedSchedMissionManager::PreCheckSupportOsd(const std::string& deviceId)
1033 {
1034     if (!AllowMissionUid(IPCSkeleton::GetCallingUid())) {
1035         HILOGE("CheckSupportOsd permission denied!");
1036         return false;
1037     }
1038     if (!IsDeviceIdValidated(deviceId)) {
1039         HILOGE("DeviceId is inValidated!");
1040         return false;
1041     }
1042     if (!GetConnCapSupportOsd(deviceId)) {
1043         HILOGI("CheckSupportOsd conn cap not support osd!");
1044         return false;
1045     }
1046     return true;
1047 }
1048 
CheckSupportOsd(const std::string & deviceId)1049 int32_t DistributedSchedMissionManager::CheckSupportOsd(const std::string& deviceId)
1050 {
1051     HILOGI("CheckSupportOsd start, deviceId: %{public}s!",
1052         DnetworkAdapter::AnonymizeDeviceId(deviceId).c_str());
1053     if (!PreCheckSupportOsd(deviceId)) {
1054         return MISSION_OSD_NOT_SUPPORTED;
1055     }
1056     int64_t begin = GetTickCount();
1057     {
1058         std::string uuid = DtbschedmgrDeviceInfoStorage::GetInstance().GetUuidByNetworkId(deviceId);
1059         std::lock_guard<std::mutex> autoLock(osdSwitchLock_);
1060         auto iter = osdSwitchValueMap_.find(uuid);
1061         if (iter != osdSwitchValueMap_.end()) {
1062             HILOGI("CheckSupportOsd find value spend %{public}" PRId64 " ms", GetTickCount() - begin);
1063             return iter->second;
1064         }
1065     }
1066     HILOGI("CheckSupportOsd can not find value spend %{public}" PRId64 " ms", GetTickCount() - begin);
1067 
1068     sptr<IDistributedSched> remoteDms = GetRemoteDms(deviceId);
1069     if (remoteDms == nullptr) {
1070         HILOGE("CheckSupportOsd DMS get remoteDms failed");
1071         return MISSION_OSD_NOT_SUPPORTED;
1072     }
1073     begin = GetTickCount();
1074     int32_t osdSwitchVal = remoteDms->GetOsdSwitchValueFromRemote();
1075     HILOGI("[PerformanceTest] GetOsdSwitchValueFromRemote osdSwitch:%d, spend %{public}" PRId64 " ms",
1076         osdSwitchVal, GetTickCount() - begin);
1077     if (osdSwitchVal == IPC_STUB_UNKNOW_TRANS_ERR) {
1078         HILOGI("GetOsdSwitchValueFromRemote destUuid: %{public}s does not support osd result: %{public}d",
1079             DnetworkAdapter::AnonymizeDeviceId(deviceId).c_str(), osdSwitchVal);
1080         osdSwitchVal = MISSION_OSD_NOT_SUPPORTED;
1081     }
1082     if (!IsValidOsdSwitchValue(osdSwitchVal)) {
1083         HILOGE("CheckSupportOsd osdSwitch %{public}d is invalid!", osdSwitchVal);
1084         return MISSION_OSD_NOT_SUPPORTED;
1085     }
1086     begin = GetTickCount();
1087     std::string uuid = DtbschedmgrDeviceInfoStorage::GetInstance().GetUuidByNetworkId(deviceId);
1088     std::lock_guard<std::mutex> autoLock(osdSwitchLock_);
1089     osdSwitchValueMap_[uuid] = osdSwitchVal;
1090     HILOGI("CheckSupportOsd insert value spend %{public}" PRId64 " ms", GetTickCount() - begin);
1091     return osdSwitchVal;
1092 }
1093 
CheckOsdSwitch(const std::string & deviceId)1094 int32_t DistributedSchedMissionManager::CheckOsdSwitch(const std::string& deviceId)
1095 {
1096     HILOGI("CheckOsdSwitch start, deviceId: %{public}s!",
1097         DnetworkAdapter::AnonymizeDeviceId(deviceId).c_str());
1098     if (!PreCheckSupportOsd(deviceId)) {
1099         return MISSION_OSD_NOT_SUPPORTED;
1100     }
1101     int64_t begin = GetTickCount();
1102     {
1103         std::string uuid = DtbschedmgrDeviceInfoStorage::GetInstance().GetUuidByNetworkId(deviceId);
1104         std::lock_guard<std::mutex> autoLock(osdSwitchLock_);
1105         auto iter = osdSwitchValueMap_.find(uuid);
1106         if (iter != osdSwitchValueMap_.end()) {
1107             HILOGI("CheckOsdSwitch find value spend %{public}" PRId64 " ms", GetTickCount() - begin);
1108             return iter->second;
1109         }
1110     }
1111     HILOGI("CheckOsdSwitch can not find value spend %{public}" PRId64 " ms", GetTickCount() - begin);
1112     return MISSION_OSD_ENABLED;
1113 }
1114 
GetCachedOsdSwitch(std::vector<std::u16string> & deviceIds,std::vector<int32_t> & values)1115 void DistributedSchedMissionManager::GetCachedOsdSwitch(std::vector<std::u16string>& deviceIds,
1116     std::vector<int32_t>& values)
1117 {
1118     HILOGI("GetCachedOsdSwitch start");
1119     if (!AllowMissionUid(IPCSkeleton::GetCallingUid())) {
1120         HILOGE("GetCachedOsdSwitch permission denied!");
1121         return;
1122     }
1123     std::lock_guard<std::mutex> autoLock(osdSwitchLock_);
1124     for (const auto& [uuid, value] : osdSwitchValueMap_) {
1125         std::string deviceId = DtbschedmgrDeviceInfoStorage::GetInstance().GetNetworkIdByUuid(uuid);
1126         deviceIds.emplace_back(Str8ToStr16(deviceId));
1127         bool connCapSupportOsd = GetConnCapSupportOsdInnerLocked(deviceId);
1128         values.emplace_back(connCapSupportOsd ? value : MISSION_OSD_NOT_SUPPORTED);
1129     }
1130 }
1131 
GetOsdSwitchValueFromRemote()1132 int32_t DistributedSchedMissionManager::GetOsdSwitchValueFromRemote()
1133 {
1134     HILOGI("GetOsdSwitchValueFromRemote start!");
1135     return DistributedSchedAdapter::GetInstance().GetOsdSwitch();
1136 }
1137 
AllowMissionUid(int32_t uid)1138 bool DistributedSchedMissionManager::AllowMissionUid(int32_t uid)
1139 {
1140     int32_t appId = uid % MULTIUSER_HAP_PER_USER_RANGE;
1141     if (appId < FIRST_APPLICATION_UID) {
1142         return true;
1143     }
1144     std::lock_guard<std::mutex> autoLock(allowMissionUidsLock_);
1145     if (allowMissionUids_.count(appId) == 0) {
1146         int64_t begin = GetTickCount();
1147         bool ret = DistributedSchedAdapter::GetInstance().AllowMissionUid(uid);
1148         HILOGI("AllowMissionUid ret:%{public}s, spend %{public}" PRId64 " ms!",
1149             (ret ? "succ" : "fail"), GetTickCount() - begin);
1150         if (!ret) {
1151             HILOGW("AllowMissionUid failed!");
1152             return false;
1153         }
1154         allowMissionUids_.insert(appId);
1155     }
1156     return true;
1157 }
1158 
OnRemoteDied(const wptr<IRemoteObject> & remote)1159 void DistributedSchedMissionManager::RemoteDmsDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
1160 {
1161     HILOGI("OnRemoteDied received died notify!");
1162     DistributedSchedMissionManager::GetInstance().OnRemoteDmsDied(remote);
1163 }
1164 
OnRemoteDmsDied(const wptr<IRemoteObject> & remote)1165 void DistributedSchedMissionManager::OnRemoteDmsDied(const wptr<IRemoteObject>& remote)
1166 {
1167     sptr<IRemoteObject> diedRemoted = remote.promote();
1168     if (diedRemoted == nullptr) {
1169         HILOGW("OnRemoteDmsDied promote failed!");
1170         return;
1171     }
1172     HILOGD("delete diedRemoted");
1173     auto remoteDmsDiedFunc = [this, diedRemoted]() {
1174         OnRemoteDmsDied(diedRemoted);
1175     };
1176     if (missionHandler_ != nullptr) {
1177         missionHandler_->PostTask(remoteDmsDiedFunc);
1178     }
1179 }
1180 
RetryStartSyncRemoteMissions(const std::string & dstDeviceId,const std::string & localDevId,int32_t retryTimes)1181 void DistributedSchedMissionManager::RetryStartSyncRemoteMissions(const std::string& dstDeviceId,
1182     const std::string& localDevId, int32_t retryTimes)
1183 {
1184     auto retryFunc = [this, dstDeviceId, localDevId, retryTimes]() {
1185         bool ret = HasSyncListener(dstDeviceId);
1186         if (!ret) {
1187             return;
1188         }
1189         sptr<IDistributedSched> remoteDms = GetRemoteDms(dstDeviceId);
1190         if (remoteDms == nullptr) {
1191             HILOGI("RetryStartSyncRemoteMissions DMS get remoteDms failed");
1192             RetryStartSyncRemoteMissions(dstDeviceId, localDevId, retryTimes + 1);
1193             return;
1194         }
1195         int32_t errNo = StartSyncRemoteMissions(dstDeviceId, remoteDms);
1196         HILOGI("RetryStartSyncRemoteMissions result:%{public}d", errNo);
1197     };
1198     if (missionHandler_ != nullptr && retryTimes < MAX_RETRY_TIMES) {
1199         missionHandler_->PostTask(retryFunc, RETRY_DELAYED);
1200     }
1201 }
1202 
UpdateOsdSwitchValueFromRemote(int32_t switchVal,const std::string & deviceId)1203 int32_t DistributedSchedMissionManager::UpdateOsdSwitchValueFromRemote(int32_t switchVal,
1204     const std::string& deviceId)
1205 {
1206     HILOGI("UpdateOsdSwitchValueFromRemote notify start, switchVal: %{public}d, deviceId: %{public}s!",
1207         switchVal, DnetworkAdapter::AnonymizeDeviceId(deviceId).c_str());
1208     bool needNotifyChanged = false;
1209     bool connCap = false;
1210     int64_t begin = GetTickCount();
1211     {
1212         std::string uuid = DtbschedmgrDeviceInfoStorage::GetInstance().GetUuidByNetworkId(deviceId);
1213         std::lock_guard<std::mutex> autoLock(osdSwitchLock_);
1214         auto iter = osdSwitchValueMap_.find(uuid);
1215         if (iter != osdSwitchValueMap_.end()) {
1216             needNotifyChanged = (iter->second != switchVal);
1217         } else {
1218             needNotifyChanged = true;
1219         }
1220         osdSwitchValueMap_[uuid] = switchVal;
1221         connCap = GetConnCapSupportOsdInnerLocked(deviceId);
1222     }
1223     HILOGI("UpdateOsdSwitchValueFromRemote update value spend %{public}" PRId64 " ms", GetTickCount() - begin);
1224     NotifyOsdSwitchChanged(needNotifyChanged, deviceId, connCap ? switchVal : MISSION_OSD_NOT_SUPPORTED);
1225     HILOGD("UpdateOsdSwitchValueFromRemote end!");
1226     return ERR_NONE;
1227 }
1228 
NotifyOsdSwitchChanged(bool needNotifyChanged,const std::string & deviceId,int32_t switchVal)1229 void DistributedSchedMissionManager::NotifyOsdSwitchChanged(bool needNotifyChanged, const std::string& deviceId,
1230     int32_t switchVal)
1231 {
1232     std::lock_guard<std::mutex> autoLock(listenDeviceLock_);
1233     if (!needNotifyChanged) {
1234         HILOGI("NotifyOsdSwitchChanged no need to notify!");
1235         return;
1236     }
1237     auto iter = listenDeviceMap_.find(Str8ToStr16(deviceId));
1238     if (iter == listenDeviceMap_.end()) {
1239         HILOGI("NotifyOsdSwitchChanged notify no listener!");
1240         return;
1241     }
1242 
1243     auto& listenerSet = iter->second.listenerSet;
1244     auto notifyChanged = [listenerSet, deviceId, switchVal, this] () {
1245         HILOGI("NotifyOsdSwitchChanged %{public}s!",
1246             DnetworkAdapter::AnonymizeDeviceId(deviceId).c_str());
1247         bool isSwitchOn = (switchVal == MISSION_OSD_ENABLED);
1248         for (const auto& listener : listenerSet) {
1249             MissionChangedNotify::NotifyOsdSwitchChanged(listener, Str8ToStr16(deviceId), isSwitchOn);
1250         }
1251     };
1252     if (missionHandler_ != nullptr && !missionHandler_->PostTask(notifyChanged)) {
1253         HILOGE("NotifyOsdSwitchChanged PostTask failed!");
1254     }
1255 }
1256 
UpdateSwitchValueToRemote()1257 int32_t DistributedSchedMissionManager::UpdateSwitchValueToRemote()
1258 {
1259     auto callback = [this] () {
1260         std::string localNetworkId;
1261         if (!DtbschedmgrDeviceInfoStorage::GetInstance().GetLocalDeviceId(localNetworkId)) {
1262             HILOGE("UpdateSwitchValueToRemote get local uuid failed!");
1263             return;
1264         }
1265         std::set<std::string> remoteSyncDeviceSet;
1266         DtbschedmgrDeviceInfoStorage::GetInstance().GetDeviceIdSet(remoteSyncDeviceSet);
1267         UpdateSwitchValueToRemoteInner(remoteSyncDeviceSet, localNetworkId);
1268     };
1269     if (updateHandler_ == nullptr) {
1270         HILOGE("UpdateSwitchValueToRemote updateHandler_ is nullptr");
1271         return ERR_NULL_OBJECT;
1272     }
1273     if (!updateHandler_->PostTask(callback)) {
1274         HILOGE("UpdateSwitchValueToRemote PostTask failed!");
1275         return ERR_NULL_OBJECT;
1276     }
1277     return ERR_NONE;
1278 }
1279 
UpdateSwitchValueToRemoteInner(std::set<std::string> & remoteSyncDeviceSet,const std::string & localNetworkId)1280 void DistributedSchedMissionManager::UpdateSwitchValueToRemoteInner(std::set<std::string>& remoteSyncDeviceSet,
1281     const std::string& localNetworkId)
1282 {
1283     HILOGD("UpdateSwitchValueToRemote Start");
1284     for (auto iter = remoteSyncDeviceSet.begin(); iter != remoteSyncDeviceSet.end(); iter++) {
1285         TryUpdateSwitchValueToRemote(localNetworkId, *iter, 0);
1286     }
1287 }
1288 
TryUpdateSwitchValueToRemote(const std::string & localNetworkId,const std::string & destUuid,int32_t retryTime)1289 void DistributedSchedMissionManager::TryUpdateSwitchValueToRemote(const std::string& localNetworkId,
1290     const std::string& destUuid, int32_t retryTime)
1291 {
1292     std::lock_guard<std::mutex> autoLock(remoteSyncDeviceLock_);
1293     auto handler = FetchDeviceHandler(destUuid);
1294     auto callback = [retryTime, localNetworkId, destUuid, this] () {
1295         sptr<IDistributedSched> remoteDms = GetRemoteDms(destUuid);
1296         int32_t result = ERR_FLATTEN_OBJECT;
1297         if (remoteDms != nullptr) {
1298             int64_t begin = GetTickCount();
1299             int32_t switchVal = DistributedSchedAdapter::GetInstance().GetOsdSwitch();
1300             HILOGI("TryUpdateSwitchValueToRemote switchVal: %{public}d, destUuid: %{public}s retry:%{public}d",
1301                 switchVal, DnetworkAdapter::AnonymizeDeviceId(destUuid).c_str(), retryTime);
1302             if (!IsValidOsdSwitchValue(switchVal)) {
1303                 HILOGI("UpdateSwitchValueToRemote invalid switch value %{public}d!", switchVal);
1304                 return;
1305             }
1306             result = remoteDms->UpdateOsdSwitchValueFromRemote(switchVal, localNetworkId);
1307             HILOGI("[PerformanceTest] TryUpdateSwitchValueToRemote ret:%d, spend %{public}" PRId64 " ms",
1308                 result, GetTickCount() - begin);
1309         } else {
1310             HILOGE("TryUpdateSwitchValueToRemote DMS get remoteDms failed");
1311         }
1312         if (result == IPC_STUB_UNKNOW_TRANS_ERR) {
1313             HILOGI("TryUpdateSwitchValueToRemote destUuid: %{public}s does not support osd result: %{public}d",
1314                 DnetworkAdapter::AnonymizeDeviceId(destUuid).c_str(), result);
1315             return;
1316         }
1317         if (result != ERR_NONE) {
1318             TryUpdateSwitchValueToRemote(localNetworkId, destUuid, retryTime + 1);
1319             HILOGI("DMS TryUpdateSwitchValueToRemote retry");
1320         }
1321         HILOGI("DMS TryUpdateSwitchValueToRemote result:%{public}d", result);
1322     };
1323     if (handler != nullptr && retryTime < MAX_RETRY_TIMES) {
1324         handler->PostTask(callback, DELAY_TIME);
1325     }
1326 }
1327 
IsValidOsdSwitchValue(int32_t osdSwitchVal)1328 bool DistributedSchedMissionManager::IsValidOsdSwitchValue(int32_t osdSwitchVal)
1329 {
1330     return osdSwitchVal == MISSION_OSD_NOT_SUPPORTED || osdSwitchVal == MISSION_OSD_NOT_ENABLED ||
1331         osdSwitchVal == MISSION_OSD_ENABLED || osdSwitchVal == MISSION_OSD_CCM_NOT_SUPPORTED ||
1332         osdSwitchVal == MISSION_OSD_WIFI_OFF || osdSwitchVal == MISSION_OSD_CHILDMODE_ON ||
1333         osdSwitchVal == MISSION_OSD_CLOUD_SWITCH_OFF;
1334 }
1335 
OnMissionListenerDied(const sptr<IRemoteObject> & remote)1336 void DistributedSchedMissionManager::OnMissionListenerDied(const sptr<IRemoteObject>& remote)
1337 {
1338     HILOGI("OnMissionListenerDied");
1339     std::set<std::string> deviceSet;
1340     {
1341         std::lock_guard<std::mutex> autoLock(listenDeviceLock_);
1342         auto iterItem = listenDeviceMap_.begin();
1343         while (iterItem != listenDeviceMap_.end()) {
1344             auto& listenerInfo = iterItem->second;
1345             auto ret = listenerInfo.Find(remote);
1346             if (!ret) {
1347                 ++iterItem;
1348                 continue;
1349             }
1350             remote->RemoveDeathRecipient(listenerDeath_);
1351             listenerInfo.Erase(remote);
1352             if (listenerInfo.Empty()) {
1353                 if (listenerInfo.called) {
1354                     deviceSet.emplace(Str16ToStr8(iterItem->first));
1355                 }
1356                 iterItem = listenDeviceMap_.erase(iterItem);
1357             } else {
1358                 ++iterItem;
1359             }
1360         }
1361     }
1362     if (deviceSet.empty()) {
1363         return;
1364     }
1365     for (auto& devId : deviceSet) {
1366         StopSyncRemoteMissions(devId, false);
1367     }
1368 }
1369 
OnRemoteDmsDied(const sptr<IRemoteObject> & remote)1370 void DistributedSchedMissionManager::OnRemoteDmsDied(const sptr<IRemoteObject>& remote)
1371 {
1372     HILOGI("OnRemoteDmsDied");
1373     std::string devId;
1374     {
1375         std::lock_guard<std::mutex> autoLock(remoteDmsLock_);
1376         for (auto iter = remoteDmsMap_.begin(); iter != remoteDmsMap_.end(); ++iter) {
1377             if (iter->second->AsObject() == remote) {
1378                 iter->second->AsObject()->RemoveDeathRecipient(remoteDmsRecipient_);
1379                 devId = iter->first;
1380                 remoteDmsMap_.erase(iter);
1381                 break;
1382             }
1383         }
1384     }
1385     if (devId.empty()) {
1386         return;
1387     }
1388     bool ret = HasSyncListener(devId);
1389     if (ret) {
1390         std::string localDeviceId;
1391         if (!DtbschedmgrDeviceInfoStorage::GetInstance().GetLocalDeviceId(localDeviceId)) {
1392             return;
1393         }
1394         RetryStartSyncRemoteMissions(devId, localDeviceId, 0);
1395     }
1396 }
1397 
NotifyDmsProxyProcessDied()1398 void DistributedSchedMissionManager::NotifyDmsProxyProcessDied()
1399 {
1400     HILOGI("NotifyDmsProxyProcessDied!");
1401     if (!isRegMissionChange_) {
1402         return;
1403     }
1404     RetryRegisterMissionChange(0);
1405 }
1406 
RetryRegisterMissionChange(int32_t retryTimes)1407 void DistributedSchedMissionManager::RetryRegisterMissionChange(int32_t retryTimes)
1408 {
1409     auto remoteDiedFunc = [this, retryTimes]() {
1410         HILOGI("RetryRegisterMissionChange retryTimes:%{public}d begin", retryTimes);
1411         if (!isRegMissionChange_) {
1412             return;
1413         }
1414         int32_t ret = DistributedSchedAdapter::GetInstance().RegisterMissionListener(missonChangeListener_);
1415         if (ret == ERR_NULL_OBJECT) {
1416             RetryRegisterMissionChange(retryTimes + 1);
1417             HILOGI("RetryRegisterMissionChange dmsproxy null, retry!");
1418             return;
1419         }
1420         HILOGI("RetryRegisterMissionChange result:%{public}d", ret);
1421     };
1422     if (missionHandler_ != nullptr && retryTimes < MAX_RETRY_TIMES) {
1423         missionHandler_->PostTask(remoteDiedFunc, RETRY_DELAYED);
1424     }
1425 }
1426 
InitAllSnapshots(const std::vector<DstbMissionInfo> & missionInfos)1427 void DistributedSchedMissionManager::InitAllSnapshots(const std::vector<DstbMissionInfo>& missionInfos)
1428 {
1429     for (auto iter = missionInfos.begin(); iter != missionInfos.end(); iter++) {
1430         ErrCode errCode = MissionSnapshotChanged(iter->id);
1431         if (errCode != ERR_OK) {
1432             HILOGE("mission snapshot changed failed, missionId=%{public}d, errCode=%{public}d", iter->id, errCode);
1433         }
1434     }
1435 }
1436 
MissionSnapshotChanged(int32_t missionId)1437 int32_t DistributedSchedMissionManager::MissionSnapshotChanged(int32_t missionId)
1438 {
1439     std::string networkId;
1440     if (!DtbschedmgrDeviceInfoStorage::GetInstance().GetLocalDeviceId(networkId)) {
1441         HILOGE("get local networkId failed!");
1442         return INVALID_PARAMETERS_ERR;
1443     }
1444     AAFwk::MissionSnapshot missionSnapshot;
1445     ErrCode errCode = DistributedSchedAdapter::GetInstance()
1446         .GetLocalMissionSnapshotInfo(networkId, missionId, missionSnapshot);
1447     if (errCode != ERR_OK) {
1448         HILOGE("get local mission snapshot failed, missionId=%{public}d, errCode=%{public}d", missionId, errCode);
1449         return errCode;
1450     }
1451     Snapshot snapshot;
1452     SnapshotConverter::ConvertToSnapshot(missionSnapshot, snapshot);
1453     MessageParcel data;
1454     errCode = MissionSnapshotSequence(snapshot, data);
1455     if (errCode != ERR_OK) {
1456         HILOGE("mission snapshot sequence failed, errCode=%{public}d", errCode);
1457         return errCode;
1458     }
1459     size_t len = data.GetReadableBytes();
1460     const uint8_t* byteStream = data.ReadBuffer(len);
1461     errCode = StoreSnapshotInfo(networkId, missionId, byteStream, len);
1462     return errCode;
1463 }
1464 
MissionSnapshotDestroyed(int32_t missionId)1465 int32_t DistributedSchedMissionManager::MissionSnapshotDestroyed(int32_t missionId)
1466 {
1467     std::string networkId;
1468     if (!DtbschedmgrDeviceInfoStorage::GetInstance().GetLocalDeviceId(networkId)) {
1469         HILOGE("get local networkId failed!");
1470         return INVALID_PARAMETERS_ERR;
1471     }
1472     ErrCode errCode = RemoveSnapshotInfo(networkId, missionId);
1473     return errCode;
1474 }
1475 
MissionSnapshotSequence(const Snapshot & snapshot,MessageParcel & data)1476 int32_t DistributedSchedMissionManager::MissionSnapshotSequence(const Snapshot& snapshot, MessageParcel& data)
1477 {
1478     bool ret = snapshot.WriteSnapshotInfo(data);
1479     if (!ret) {
1480         HILOGE("WriteSnapshotInfo failed!");
1481         return ERR_FLATTEN_OBJECT;
1482     }
1483     ret = snapshot.WritePixelMap(data);
1484     if (!ret) {
1485         HILOGE("WritePixelMap failed!");
1486         return ERR_FLATTEN_OBJECT;
1487     }
1488     return ERR_OK;
1489 }
1490 
OnDnetDied()1491 void DistributedSchedMissionManager::OnDnetDied()
1492 {
1493     auto dnetDiedFunc = [this]() {
1494         HILOGI("OnDnetDied");
1495         std::lock_guard<std::mutex> autoLock(remoteSyncDeviceLock_);
1496         if (!isRegMissionChange_) {
1497             return;
1498         }
1499         remoteSyncDeviceSet_.clear();
1500         DistributedSchedAdapter::GetInstance().UnRegisterMissionListener(missonChangeListener_);
1501         DistributedSchedAdapter::GetInstance().OnOsdEventOccur(UNREGISTER_MISSION_LISTENER);
1502         isRegMissionChange_ = false;
1503     };
1504     if (missionHandler_ != nullptr) {
1505         missionHandler_->PostTask(dnetDiedFunc);
1506     }
1507 }
1508 } // namespace DistributedSchedule
1509 } // namespace OHOS
1510