1686862fbSopenharmony_ci/*
2686862fbSopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3686862fbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4686862fbSopenharmony_ci * you may not use this file except in compliance with the License.
5686862fbSopenharmony_ci * You may obtain a copy of the License at
6686862fbSopenharmony_ci *
7686862fbSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8686862fbSopenharmony_ci *
9686862fbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10686862fbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11686862fbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12686862fbSopenharmony_ci * See the License for the specific language governing permissions and
13686862fbSopenharmony_ci * limitations under the License.
14686862fbSopenharmony_ci */
15686862fbSopenharmony_ci
16686862fbSopenharmony_ci#include "distributed_sched_adapter.h"
17686862fbSopenharmony_ci
18686862fbSopenharmony_ci#include "datetime_ex.h"
19686862fbSopenharmony_ci#include "dfx/dms_hisysevent_report.h"
20686862fbSopenharmony_ci#include "distributed_sched_service.h"
21686862fbSopenharmony_ci#include "distributed_sched_utils.h"
22686862fbSopenharmony_ci#include "dtbschedmgr_device_info_storage.h"
23686862fbSopenharmony_ci#include "dtbschedmgr_log.h"
24686862fbSopenharmony_ci#include "ipc_skeleton.h"
25686862fbSopenharmony_ci#include "ipc_types.h"
26686862fbSopenharmony_ci#ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER
27686862fbSopenharmony_ci#include "mission/distributed_sched_mission_manager.h"
28686862fbSopenharmony_ci#include "mission/mission_info_converter.h"
29686862fbSopenharmony_ci#endif
30686862fbSopenharmony_ci#include "os_account_manager.h"
31686862fbSopenharmony_ci#include "parcel_helper.h"
32686862fbSopenharmony_ci#include "string_ex.h"
33686862fbSopenharmony_ci
34686862fbSopenharmony_cinamespace OHOS {
35686862fbSopenharmony_cinamespace DistributedSchedule {
36686862fbSopenharmony_ciusing namespace std;
37686862fbSopenharmony_ciusing namespace AAFwk;
38686862fbSopenharmony_ciusing namespace AccountSA;
39686862fbSopenharmony_ciusing namespace AppExecFwk;
40686862fbSopenharmony_ciusing DstbMissionChangeListener = DistributedMissionChangeListener;
41686862fbSopenharmony_cinamespace {
42686862fbSopenharmony_ci// set a non-zero value on need later
43686862fbSopenharmony_ciconstexpr int64_t DEVICE_OFFLINE_DELAY_TIME = 0;
44686862fbSopenharmony_ciconst std::string TAG = "DistributedSchedAdapter";
45686862fbSopenharmony_ci}
46686862fbSopenharmony_ci
47686862fbSopenharmony_ciIMPLEMENT_SINGLE_INSTANCE(DistributedSchedAdapter);
48686862fbSopenharmony_ci
49686862fbSopenharmony_civoid DistributedSchedAdapter::Init()
50686862fbSopenharmony_ci{
51686862fbSopenharmony_ci    if (dmsAdapterHandler_ == nullptr) {
52686862fbSopenharmony_ci        shared_ptr<EventRunner> runner = EventRunner::Create("dmsAdapter");
53686862fbSopenharmony_ci        if (runner == nullptr) {
54686862fbSopenharmony_ci            HILOGE("create runner failed");
55686862fbSopenharmony_ci            return;
56686862fbSopenharmony_ci        }
57686862fbSopenharmony_ci        dmsAdapterHandler_ = make_shared<EventHandler>(runner);
58686862fbSopenharmony_ci    }
59686862fbSopenharmony_ci}
60686862fbSopenharmony_ci
61686862fbSopenharmony_civoid DistributedSchedAdapter::UnInit()
62686862fbSopenharmony_ci{
63686862fbSopenharmony_ci    dmsAdapterHandler_ = nullptr;
64686862fbSopenharmony_ci}
65686862fbSopenharmony_ci
66686862fbSopenharmony_ciint32_t DistributedSchedAdapter::ConnectAbility(const OHOS::AAFwk::Want& want,
67686862fbSopenharmony_ci    const sptr<IRemoteObject>& connect, const sptr<IRemoteObject>& callerToken)
68686862fbSopenharmony_ci{
69686862fbSopenharmony_ci    HILOGD("ConnectAbility");
70686862fbSopenharmony_ci    ErrCode errCode = AAFwk::AbilityManagerClient::GetInstance()->Connect();
71686862fbSopenharmony_ci    if (errCode != ERR_OK) {
72686862fbSopenharmony_ci        HILOGE("connect ability server failed, errCode=%{public}d", errCode);
73686862fbSopenharmony_ci        DmsHiSysEventReport::ReportFaultEvent(FaultEvent::CONNECT_REMOTE_ABILITY,
74686862fbSopenharmony_ci            EventErrorType::GET_ABILITY_MGR_FAILED);
75686862fbSopenharmony_ci        return errCode;
76686862fbSopenharmony_ci    }
77686862fbSopenharmony_ci    int32_t activeAccountId = -1;
78686862fbSopenharmony_ci#ifdef OS_ACCOUNT_PART
79686862fbSopenharmony_ci    std::vector<int> ids;
80686862fbSopenharmony_ci    errCode = AccountSA::OsAccountManager::QueryActiveOsAccountIds(ids);
81686862fbSopenharmony_ci    if (errCode != ERR_OK || ids.empty()) {
82686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
83686862fbSopenharmony_ci    }
84686862fbSopenharmony_ci    activeAccountId = ids[0];
85686862fbSopenharmony_ci#endif
86686862fbSopenharmony_ci    errCode = AAFwk::AbilityManagerClient::GetInstance()->ConnectAbility(want,
87686862fbSopenharmony_ci        iface_cast<AAFwk::IAbilityConnection>(connect), callerToken, activeAccountId);
88686862fbSopenharmony_ci    return errCode;
89686862fbSopenharmony_ci}
90686862fbSopenharmony_ci
91686862fbSopenharmony_ciint32_t DistributedSchedAdapter::DisconnectAbility(const sptr<IRemoteObject>& connect)
92686862fbSopenharmony_ci{
93686862fbSopenharmony_ci    HILOGD("DisconnectAbility");
94686862fbSopenharmony_ci    ErrCode errCode = AAFwk::AbilityManagerClient::GetInstance()->Connect();
95686862fbSopenharmony_ci    if (errCode != ERR_OK) {
96686862fbSopenharmony_ci        HILOGE("connect ability server failed, errCode=%{public}d", errCode);
97686862fbSopenharmony_ci        DmsHiSysEventReport::ReportFaultEvent(FaultEvent::DISCONNECT_REMOTE_ABILITY,
98686862fbSopenharmony_ci            EventErrorType::GET_ABILITY_MGR_FAILED);
99686862fbSopenharmony_ci        return errCode;
100686862fbSopenharmony_ci    }
101686862fbSopenharmony_ci    ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->DisconnectAbility(
102686862fbSopenharmony_ci        iface_cast<AAFwk::IAbilityConnection>(connect));
103686862fbSopenharmony_ci    return ret;
104686862fbSopenharmony_ci}
105686862fbSopenharmony_ci
106686862fbSopenharmony_civoid DistributedSchedAdapter::DeviceOnline(const std::string& networkId)
107686862fbSopenharmony_ci{
108686862fbSopenharmony_ci    if (dmsAdapterHandler_ == nullptr) {
109686862fbSopenharmony_ci        HILOGE("DeviceOnline dmsAdapterHandler is null");
110686862fbSopenharmony_ci        return;
111686862fbSopenharmony_ci    }
112686862fbSopenharmony_ci
113686862fbSopenharmony_ci    if (networkId.empty()) {
114686862fbSopenharmony_ci        HILOGW("DeviceOnline networkId is empty");
115686862fbSopenharmony_ci        return;
116686862fbSopenharmony_ci    }
117686862fbSopenharmony_ci
118686862fbSopenharmony_ci    HILOGD("process DeviceOnline networkId is %{public}s", GetAnonymStr(networkId).c_str());
119686862fbSopenharmony_ci    dmsAdapterHandler_->RemoveTask(networkId);
120686862fbSopenharmony_ci}
121686862fbSopenharmony_ci
122686862fbSopenharmony_civoid DistributedSchedAdapter::DeviceOffline(const std::string& networkId)
123686862fbSopenharmony_ci{
124686862fbSopenharmony_ci    if (dmsAdapterHandler_ == nullptr) {
125686862fbSopenharmony_ci        HILOGE("DeviceOffline dmsAdapterHandler is null");
126686862fbSopenharmony_ci        return;
127686862fbSopenharmony_ci    }
128686862fbSopenharmony_ci
129686862fbSopenharmony_ci    if (networkId.empty()) {
130686862fbSopenharmony_ci        HILOGW("DeviceOffline networkId is empty");
131686862fbSopenharmony_ci        return;
132686862fbSopenharmony_ci    }
133686862fbSopenharmony_ci    HILOGD("process DeviceOffline networkId is %{public}s", GetAnonymStr(networkId).c_str());
134686862fbSopenharmony_ci    auto callback = [networkId, this] () {
135686862fbSopenharmony_ci        ProcessDeviceOffline(networkId);
136686862fbSopenharmony_ci    };
137686862fbSopenharmony_ci    if (!dmsAdapterHandler_->PostTask(callback, networkId, DEVICE_OFFLINE_DELAY_TIME)) {
138686862fbSopenharmony_ci        HILOGW("DeviceOffline PostTask failed");
139686862fbSopenharmony_ci    }
140686862fbSopenharmony_ci}
141686862fbSopenharmony_ci
142686862fbSopenharmony_civoid DistributedSchedAdapter::ProcessDeviceOffline(const std::string& deviceId)
143686862fbSopenharmony_ci{
144686862fbSopenharmony_ci    HILOGD("ProcessDeviceOffline");
145686862fbSopenharmony_ci    DistributedSchedService::GetInstance().ProcessDeviceOffline(deviceId);
146686862fbSopenharmony_ci}
147686862fbSopenharmony_ci
148686862fbSopenharmony_civoid DistributedSchedAdapter::ProcessConnectDied(const sptr<IRemoteObject>& connect)
149686862fbSopenharmony_ci{
150686862fbSopenharmony_ci    if (dmsAdapterHandler_ == nullptr) {
151686862fbSopenharmony_ci        HILOGE("ProcessConnectDied dmsAdapterHandler is null");
152686862fbSopenharmony_ci        return;
153686862fbSopenharmony_ci    }
154686862fbSopenharmony_ci
155686862fbSopenharmony_ci    if (connect == nullptr) {
156686862fbSopenharmony_ci        HILOGE("ProcessConnectDied connect is null");
157686862fbSopenharmony_ci        return;
158686862fbSopenharmony_ci    }
159686862fbSopenharmony_ci    HILOGD("process connect died");
160686862fbSopenharmony_ci    auto callback = [connect] () {
161686862fbSopenharmony_ci        DistributedSchedService::GetInstance().ProcessConnectDied(connect);
162686862fbSopenharmony_ci    };
163686862fbSopenharmony_ci    if (!dmsAdapterHandler_->PostTask(callback)) {
164686862fbSopenharmony_ci        HILOGW("ProcessConnectDied PostTask failed");
165686862fbSopenharmony_ci    }
166686862fbSopenharmony_ci}
167686862fbSopenharmony_ci
168686862fbSopenharmony_civoid DistributedSchedAdapter::ProcessCalleeDied(const sptr<IRemoteObject>& connect)
169686862fbSopenharmony_ci{
170686862fbSopenharmony_ci    if (dmsAdapterHandler_ == nullptr) {
171686862fbSopenharmony_ci        HILOGE("ProcessCalleeDied dmsAdapterHandler is null");
172686862fbSopenharmony_ci        return;
173686862fbSopenharmony_ci    }
174686862fbSopenharmony_ci    if (connect == nullptr) {
175686862fbSopenharmony_ci        HILOGE("ProcessCalleeDied connect is null");
176686862fbSopenharmony_ci        return;
177686862fbSopenharmony_ci    }
178686862fbSopenharmony_ci    HILOGD("process callee died");
179686862fbSopenharmony_ci    auto callback = [connect] () {
180686862fbSopenharmony_ci        DistributedSchedService::GetInstance().ProcessCalleeDied(connect);
181686862fbSopenharmony_ci    };
182686862fbSopenharmony_ci    if (!dmsAdapterHandler_->PostTask(callback)) {
183686862fbSopenharmony_ci        HILOGE("ProcessCalleeDied PostTask failed");
184686862fbSopenharmony_ci    }
185686862fbSopenharmony_ci}
186686862fbSopenharmony_ci
187686862fbSopenharmony_civoid DistributedSchedAdapter::ProcessCallResult(const sptr<IRemoteObject>& calleeConnect,
188686862fbSopenharmony_ci    const sptr<IRemoteObject>& callerConnect)
189686862fbSopenharmony_ci{
190686862fbSopenharmony_ci    if (dmsAdapterHandler_ == nullptr) {
191686862fbSopenharmony_ci        HILOGE("ProcessCallResult dmsAdapterHandler is null");
192686862fbSopenharmony_ci        return;
193686862fbSopenharmony_ci    }
194686862fbSopenharmony_ci    if (calleeConnect == nullptr || callerConnect == nullptr) {
195686862fbSopenharmony_ci        HILOGE("ProcessCallResult connect is null");
196686862fbSopenharmony_ci        return;
197686862fbSopenharmony_ci    }
198686862fbSopenharmony_ci    HILOGD("process call result start");
199686862fbSopenharmony_ci    auto callback = [calleeConnect, callerConnect] () {
200686862fbSopenharmony_ci        DistributedSchedService::GetInstance().ProcessCallResult(calleeConnect, callerConnect);
201686862fbSopenharmony_ci    };
202686862fbSopenharmony_ci    if (!dmsAdapterHandler_->PostTask(callback)) {
203686862fbSopenharmony_ci        HILOGE("ProcessCalleeDied PostTask failed");
204686862fbSopenharmony_ci    }
205686862fbSopenharmony_ci}
206686862fbSopenharmony_ci
207686862fbSopenharmony_civoid DistributedSchedAdapter::ProcessCallerDied(const sptr<IRemoteObject>& connect, int32_t deviceType)
208686862fbSopenharmony_ci{
209686862fbSopenharmony_ci    if (dmsAdapterHandler_ == nullptr) {
210686862fbSopenharmony_ci        HILOGE("ProcessCallerDied dmsAdapterHandler is null");
211686862fbSopenharmony_ci        return;
212686862fbSopenharmony_ci    }
213686862fbSopenharmony_ci    if (connect == nullptr) {
214686862fbSopenharmony_ci        HILOGE("ProcessCallerDied connect is null");
215686862fbSopenharmony_ci        return;
216686862fbSopenharmony_ci    }
217686862fbSopenharmony_ci    HILOGD("process caller died");
218686862fbSopenharmony_ci    auto callback = [connect, deviceType] () {
219686862fbSopenharmony_ci        DistributedSchedService::GetInstance().ProcessCallerDied(connect, deviceType);
220686862fbSopenharmony_ci    };
221686862fbSopenharmony_ci    if (!dmsAdapterHandler_->PostTask(callback)) {
222686862fbSopenharmony_ci        HILOGE("ProcessCallerDied PostTask failed");
223686862fbSopenharmony_ci    }
224686862fbSopenharmony_ci}
225686862fbSopenharmony_ci
226686862fbSopenharmony_ciint32_t DistributedSchedAdapter::ReleaseAbility(const sptr<IRemoteObject>& connect,
227686862fbSopenharmony_ci    const AppExecFwk::ElementName &element)
228686862fbSopenharmony_ci{
229686862fbSopenharmony_ci    HILOGD("ReleaseAbility called");
230686862fbSopenharmony_ci    ErrCode errCode = AAFwk::AbilityManagerClient::GetInstance()->Connect();
231686862fbSopenharmony_ci    if (errCode != ERR_OK) {
232686862fbSopenharmony_ci        HILOGE("ReleaseAbility:connect ability server failed, errCode=%{public}d", errCode);
233686862fbSopenharmony_ci        DmsHiSysEventReport::ReportFaultEvent(FaultEvent::RELEASE_REMOTE_ABILITY,
234686862fbSopenharmony_ci            EventErrorType::GET_ABILITY_MGR_FAILED);
235686862fbSopenharmony_ci        return errCode;
236686862fbSopenharmony_ci    }
237686862fbSopenharmony_ci    AppExecFwk::ElementName elementWithoutDeviceId("", element.GetBundleName(), element.GetAbilityName());
238686862fbSopenharmony_ci    ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->ReleaseCall(
239686862fbSopenharmony_ci        iface_cast<AAFwk::IAbilityConnection>(connect), elementWithoutDeviceId);
240686862fbSopenharmony_ci    return ret;
241686862fbSopenharmony_ci}
242686862fbSopenharmony_ci
243686862fbSopenharmony_ciint32_t DistributedSchedAdapter::StartAbilityByCall(const OHOS::AAFwk::Want& want,
244686862fbSopenharmony_ci    const sptr<IRemoteObject>& connect, const sptr<IRemoteObject>& callerToken)
245686862fbSopenharmony_ci{
246686862fbSopenharmony_ci    HILOGD("ResolveAbility called");
247686862fbSopenharmony_ci    ErrCode errCode = AAFwk::AbilityManagerClient::GetInstance()->Connect();
248686862fbSopenharmony_ci    if (errCode != ERR_OK) {
249686862fbSopenharmony_ci        HILOGE("ResolveAbility:connect ability server failed, errCode=%{public}d", errCode);
250686862fbSopenharmony_ci        DmsHiSysEventReport::ReportFaultEvent(FaultEvent::START_REMOTE_ABILITY_BYCALL,
251686862fbSopenharmony_ci            EventErrorType::GET_ABILITY_MGR_FAILED);
252686862fbSopenharmony_ci        return errCode;
253686862fbSopenharmony_ci    }
254686862fbSopenharmony_ci    ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->StartAbilityByCall(want,
255686862fbSopenharmony_ci        iface_cast<AAFwk::IAbilityConnection>(connect), callerToken);
256686862fbSopenharmony_ci    return ret;
257686862fbSopenharmony_ci}
258686862fbSopenharmony_ci
259686862fbSopenharmony_cibool DistributedSchedAdapter::InitHichainService()
260686862fbSopenharmony_ci{
261686862fbSopenharmony_ci    if (hichainGmInstance_ != nullptr) {
262686862fbSopenharmony_ci        HILOGD("hichain GmInstance is already exist");
263686862fbSopenharmony_ci        return true;
264686862fbSopenharmony_ci    }
265686862fbSopenharmony_ci    if (InitDeviceAuthService() != ERR_OK) {
266686862fbSopenharmony_ci        HILOGE("hichain init DeviceAuthService failed");
267686862fbSopenharmony_ci        return false;
268686862fbSopenharmony_ci    }
269686862fbSopenharmony_ci    hichainGmInstance_ = GetGmInstance();
270686862fbSopenharmony_ci    if (hichainGmInstance_ == nullptr) {
271686862fbSopenharmony_ci        HILOGE("hichain get GmInstance failed");
272686862fbSopenharmony_ci        return false;
273686862fbSopenharmony_ci    }
274686862fbSopenharmony_ci    return true;
275686862fbSopenharmony_ci}
276686862fbSopenharmony_ci
277686862fbSopenharmony_cibool DistributedSchedAdapter::CheckAccessToGroup(const std::string& groupId, const std::string& targetBundleName)
278686862fbSopenharmony_ci{
279686862fbSopenharmony_ci    std::lock_guard<std::mutex> autoLock(hichainLock_);
280686862fbSopenharmony_ci    int64_t begin = GetTickCount();
281686862fbSopenharmony_ci    if (!InitHichainService()) {
282686862fbSopenharmony_ci        return false;
283686862fbSopenharmony_ci    }
284686862fbSopenharmony_ci    int32_t ret = hichainGmInstance_->checkAccessToGroup(ANY_OS_ACCOUNT, targetBundleName.c_str(),
285686862fbSopenharmony_ci        groupId.c_str());
286686862fbSopenharmony_ci    HILOGI("[PerformanceTest] checkAccessToGroup spend %{public}" PRId64 " ms", GetTickCount() - begin);
287686862fbSopenharmony_ci    if (ret != ERR_OK) {
288686862fbSopenharmony_ci        HILOGE("hichain checkAccessToGroup fail, ret %{public}d.", ret);
289686862fbSopenharmony_ci        return false;
290686862fbSopenharmony_ci    }
291686862fbSopenharmony_ci    HILOGD("hichain checkAccessToGroup success");
292686862fbSopenharmony_ci    return true;
293686862fbSopenharmony_ci}
294686862fbSopenharmony_ci
295686862fbSopenharmony_cibool DistributedSchedAdapter::GetRelatedGroups(const std::string& udid, const std::string& bundleName,
296686862fbSopenharmony_ci    std::string& returnGroups)
297686862fbSopenharmony_ci{
298686862fbSopenharmony_ci    std::lock_guard<std::mutex> autoLock(hichainLock_);
299686862fbSopenharmony_ci    int64_t begin = GetTickCount();
300686862fbSopenharmony_ci    if (!InitHichainService()) {
301686862fbSopenharmony_ci        return false;
302686862fbSopenharmony_ci    }
303686862fbSopenharmony_ci    uint32_t groupNum = 0;
304686862fbSopenharmony_ci    char* groupsJsonStr = nullptr;
305686862fbSopenharmony_ci    int32_t ret = hichainGmInstance_->getRelatedGroups(ANY_OS_ACCOUNT, bundleName.c_str(), udid.c_str(),
306686862fbSopenharmony_ci        &groupsJsonStr, &groupNum);
307686862fbSopenharmony_ci    HILOGI("[PerformanceTest] getRelatedGroups spend %{public}" PRId64 " ms", GetTickCount() - begin);
308686862fbSopenharmony_ci    if (ret != ERR_OK) {
309686862fbSopenharmony_ci        HILOGE("hichain getRelatedGroups failed, ret:%{public}d", ret);
310686862fbSopenharmony_ci        return false;
311686862fbSopenharmony_ci    }
312686862fbSopenharmony_ci    if (groupsJsonStr == nullptr || groupNum == 0) {
313686862fbSopenharmony_ci        HILOGE("groupsJsonStr is nullptr");
314686862fbSopenharmony_ci        return false;
315686862fbSopenharmony_ci    }
316686862fbSopenharmony_ci    returnGroups = groupsJsonStr;
317686862fbSopenharmony_ci    return true;
318686862fbSopenharmony_ci}
319686862fbSopenharmony_ci
320686862fbSopenharmony_ci#ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER
321686862fbSopenharmony_ciint32_t DistributedSchedAdapter::GetLocalMissionInfos(int32_t numMissions,
322686862fbSopenharmony_ci    std::vector<DstbMissionInfo>& missionInfos)
323686862fbSopenharmony_ci
324686862fbSopenharmony_ci{
325686862fbSopenharmony_ci    ErrCode errCode = AAFwk::AbilityManagerClient::GetInstance()->Connect();
326686862fbSopenharmony_ci    if (errCode != ERR_OK) {
327686862fbSopenharmony_ci        HILOGE("get ability server failed, errCode = %{public}d", errCode);
328686862fbSopenharmony_ci        return errCode;
329686862fbSopenharmony_ci    }
330686862fbSopenharmony_ci    std::vector<MissionInfo> amsMissions;
331686862fbSopenharmony_ci    ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->GetMissionInfos("", numMissions, amsMissions);
332686862fbSopenharmony_ci    if (ret != ERR_OK) {
333686862fbSopenharmony_ci        HILOGE("GetMissionInfos failed, ret = %{public}d", ret);
334686862fbSopenharmony_ci        return ret;
335686862fbSopenharmony_ci    }
336686862fbSopenharmony_ci    if (amsMissions.empty()) {
337686862fbSopenharmony_ci        HILOGI("empty missions");
338686862fbSopenharmony_ci        return ERR_OK;
339686862fbSopenharmony_ci    }
340686862fbSopenharmony_ci    HILOGI("GetMissionInfos size:%{public}zu", amsMissions.size());
341686862fbSopenharmony_ci    return MissionInfoConverter::ConvertToDstbMissionInfos(amsMissions, missionInfos);
342686862fbSopenharmony_ci}
343686862fbSopenharmony_ci
344686862fbSopenharmony_ciint32_t DistributedSchedAdapter::RegisterMissionListener(const sptr<IMissionListener>& listener)
345686862fbSopenharmony_ci{
346686862fbSopenharmony_ci    HILOGD("called.");
347686862fbSopenharmony_ci    if (listener == nullptr) {
348686862fbSopenharmony_ci        HILOGE("listener is null");
349686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
350686862fbSopenharmony_ci    }
351686862fbSopenharmony_ci    ErrCode errCode = AAFwk::AbilityManagerClient::GetInstance()->Connect();
352686862fbSopenharmony_ci    if (errCode != ERR_OK) {
353686862fbSopenharmony_ci        HILOGE("get ability server failed, errCode=%{public}d", errCode);
354686862fbSopenharmony_ci        return errCode;
355686862fbSopenharmony_ci    }
356686862fbSopenharmony_ci    ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->RegisterMissionListener(listener);
357686862fbSopenharmony_ci    DmsRadar::GetInstance().RegisterFocusedRes("RegisterMissionListener", ret);
358686862fbSopenharmony_ci    if (ret != ERR_OK) {
359686862fbSopenharmony_ci        HILOGE("RegisterMissionListener failed, ret=%{public}d", ret);
360686862fbSopenharmony_ci        return ret;
361686862fbSopenharmony_ci    }
362686862fbSopenharmony_ci    return ERR_OK;
363686862fbSopenharmony_ci}
364686862fbSopenharmony_ci
365686862fbSopenharmony_ciint32_t DistributedSchedAdapter::UnRegisterMissionListener(const sptr<IMissionListener>& listener)
366686862fbSopenharmony_ci{
367686862fbSopenharmony_ci    if (listener == nullptr) {
368686862fbSopenharmony_ci        HILOGE("listener is null");
369686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
370686862fbSopenharmony_ci    }
371686862fbSopenharmony_ci    ErrCode errCode = AAFwk::AbilityManagerClient::GetInstance()->Connect();
372686862fbSopenharmony_ci    if (errCode != ERR_OK) {
373686862fbSopenharmony_ci        HILOGE("get ability server failed, errCode=%{public}d", errCode);
374686862fbSopenharmony_ci        return errCode;
375686862fbSopenharmony_ci    }
376686862fbSopenharmony_ci
377686862fbSopenharmony_ci    ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->UnRegisterMissionListener(listener);
378686862fbSopenharmony_ci    if (ret != ERR_OK) {
379686862fbSopenharmony_ci        HILOGE("UnRegisterMissionListener failed, ret=%{public}d", ret);
380686862fbSopenharmony_ci        return ret;
381686862fbSopenharmony_ci    }
382686862fbSopenharmony_ci    return ERR_OK;
383686862fbSopenharmony_ci}
384686862fbSopenharmony_ci
385686862fbSopenharmony_ciint32_t DistributedSchedAdapter::GetLocalMissionSnapshotInfo(const std::string& networkId, int32_t missionId,
386686862fbSopenharmony_ci    MissionSnapshot& missionSnapshot)
387686862fbSopenharmony_ci{
388686862fbSopenharmony_ci    int64_t begin = GetTickCount();
389686862fbSopenharmony_ci    ErrCode errCode = AAFwk::AbilityManagerClient::GetInstance()->Connect();
390686862fbSopenharmony_ci    if (errCode != ERR_OK) {
391686862fbSopenharmony_ci        HILOGE("get ability server failed, errCode=%{public}d", errCode);
392686862fbSopenharmony_ci        return errCode;
393686862fbSopenharmony_ci    }
394686862fbSopenharmony_ci    errCode = AAFwk::AbilityManagerClient::GetInstance()->GetMissionSnapshot(networkId,
395686862fbSopenharmony_ci        missionId, missionSnapshot);
396686862fbSopenharmony_ci    HILOGI("[PerformanceTest] GetMissionSnapshot spend %{public}" PRId64 " ms", GetTickCount() - begin);
397686862fbSopenharmony_ci    if (errCode != ERR_OK) {
398686862fbSopenharmony_ci        HILOGE("get mission snapshot failed, missionId=%{public}d, errCode=%{public}d", missionId, errCode);
399686862fbSopenharmony_ci        return errCode;
400686862fbSopenharmony_ci    }
401686862fbSopenharmony_ci    if (missionSnapshot.snapshot == nullptr) {
402686862fbSopenharmony_ci        HILOGE("pixel map is nullptr!");
403686862fbSopenharmony_ci        return ERR_NULL_OBJECT;
404686862fbSopenharmony_ci    }
405686862fbSopenharmony_ci    HILOGD("pixelMap size:%{public}d", missionSnapshot.snapshot->GetCapacity());
406686862fbSopenharmony_ci    return ERR_OK;
407686862fbSopenharmony_ci}
408686862fbSopenharmony_ci#endif
409686862fbSopenharmony_ci} // namespace DistributedSchedule
410686862fbSopenharmony_ci} // namespace OHOS
411