1686862fbSopenharmony_ci/*
2686862fbSopenharmony_ci * Copyright (c) 2022-2023 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#include "distributed_ability_manager_client.h"
16686862fbSopenharmony_ci
17686862fbSopenharmony_ci#include "base/continuationmgr_log.h"
18686862fbSopenharmony_ci#include "distributed_ability_manager_proxy.h"
19686862fbSopenharmony_ci#include "if_system_ability_manager.h"
20686862fbSopenharmony_ci#include "ipc_skeleton.h"
21686862fbSopenharmony_ci#include "iservice_registry.h"
22686862fbSopenharmony_ci#include "system_ability_definition.h"
23686862fbSopenharmony_ci
24686862fbSopenharmony_cinamespace OHOS {
25686862fbSopenharmony_cinamespace DistributedSchedule {
26686862fbSopenharmony_cinamespace {
27686862fbSopenharmony_ciconst std::string TAG = "ContinuationManagerClient";
28686862fbSopenharmony_ci}
29686862fbSopenharmony_ci
30686862fbSopenharmony_ciIMPLEMENT_SINGLE_INSTANCE(DistributedAbilityManagerClient);
31686862fbSopenharmony_ci
32686862fbSopenharmony_cisptr<IDistributedAbilityManager> DistributedAbilityManagerClient::GetContinuationMgrService()
33686862fbSopenharmony_ci{
34686862fbSopenharmony_ci    auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
35686862fbSopenharmony_ci    if (samgrProxy == nullptr) {
36686862fbSopenharmony_ci        HILOGE("get samgr failed.");
37686862fbSopenharmony_ci        return nullptr;
38686862fbSopenharmony_ci    }
39686862fbSopenharmony_ci    sptr<IRemoteObject> remoteObj = samgrProxy->GetSystemAbility(CONTINUATION_MANAGER_SA_ID);
40686862fbSopenharmony_ci    if (remoteObj == nullptr) {
41686862fbSopenharmony_ci        HILOGE("get continuationMgrService SA failed.");
42686862fbSopenharmony_ci        return nullptr;
43686862fbSopenharmony_ci    }
44686862fbSopenharmony_ci    return iface_cast<IDistributedAbilityManager>(remoteObj);
45686862fbSopenharmony_ci}
46686862fbSopenharmony_ci
47686862fbSopenharmony_ciint32_t DistributedAbilityManagerClient::Register(
48686862fbSopenharmony_ci    const std::shared_ptr<ContinuationExtraParams>& continuationExtraParams, int32_t& token)
49686862fbSopenharmony_ci{
50686862fbSopenharmony_ci    HILOGD("called.");
51686862fbSopenharmony_ci    sptr<IDistributedAbilityManager> continuationMgrProxy = GetContinuationMgrService();
52686862fbSopenharmony_ci    if (continuationMgrProxy == nullptr) {
53686862fbSopenharmony_ci        HILOGE("continuationMgrProxy is nullptr");
54686862fbSopenharmony_ci        return ERR_NULL_OBJECT;
55686862fbSopenharmony_ci    }
56686862fbSopenharmony_ci    return continuationMgrProxy->Register(continuationExtraParams, token);
57686862fbSopenharmony_ci}
58686862fbSopenharmony_ci
59686862fbSopenharmony_ciint32_t DistributedAbilityManagerClient::Unregister(int32_t token)
60686862fbSopenharmony_ci{
61686862fbSopenharmony_ci    HILOGD("called.");
62686862fbSopenharmony_ci    sptr<IDistributedAbilityManager> continuationMgrProxy = GetContinuationMgrService();
63686862fbSopenharmony_ci    if (continuationMgrProxy == nullptr) {
64686862fbSopenharmony_ci        HILOGE("continuationMgrProxy is nullptr");
65686862fbSopenharmony_ci        return ERR_NULL_OBJECT;
66686862fbSopenharmony_ci    }
67686862fbSopenharmony_ci    return continuationMgrProxy->Unregister(token);
68686862fbSopenharmony_ci}
69686862fbSopenharmony_ci
70686862fbSopenharmony_ciint32_t DistributedAbilityManagerClient::RegisterDeviceSelectionCallback(int32_t token, const std::string& cbType,
71686862fbSopenharmony_ci    const sptr<DeviceSelectionNotifierStub>& notifier)
72686862fbSopenharmony_ci{
73686862fbSopenharmony_ci    HILOGD("called.");
74686862fbSopenharmony_ci    sptr<IDistributedAbilityManager> continuationMgrProxy = GetContinuationMgrService();
75686862fbSopenharmony_ci    if (continuationMgrProxy == nullptr) {
76686862fbSopenharmony_ci        HILOGE("continuationMgrProxy is nullptr");
77686862fbSopenharmony_ci        return ERR_NULL_OBJECT;
78686862fbSopenharmony_ci    }
79686862fbSopenharmony_ci    return continuationMgrProxy->RegisterDeviceSelectionCallback(token, cbType, notifier);
80686862fbSopenharmony_ci}
81686862fbSopenharmony_ci
82686862fbSopenharmony_ciint32_t DistributedAbilityManagerClient::UnregisterDeviceSelectionCallback(int32_t token, const std::string& cbType)
83686862fbSopenharmony_ci{
84686862fbSopenharmony_ci    HILOGD("called.");
85686862fbSopenharmony_ci    sptr<IDistributedAbilityManager> continuationMgrProxy = GetContinuationMgrService();
86686862fbSopenharmony_ci    if (continuationMgrProxy == nullptr) {
87686862fbSopenharmony_ci        HILOGE("continuationMgrProxy is nullptr");
88686862fbSopenharmony_ci        return ERR_NULL_OBJECT;
89686862fbSopenharmony_ci    }
90686862fbSopenharmony_ci    return continuationMgrProxy->UnregisterDeviceSelectionCallback(token, cbType);
91686862fbSopenharmony_ci}
92686862fbSopenharmony_ci
93686862fbSopenharmony_ciint32_t DistributedAbilityManagerClient::UpdateConnectStatus(int32_t token, const std::string& deviceId,
94686862fbSopenharmony_ci    const DeviceConnectStatus& deviceConnectStatus)
95686862fbSopenharmony_ci{
96686862fbSopenharmony_ci    HILOGD("called.");
97686862fbSopenharmony_ci    sptr<IDistributedAbilityManager> continuationMgrProxy = GetContinuationMgrService();
98686862fbSopenharmony_ci    if (continuationMgrProxy == nullptr) {
99686862fbSopenharmony_ci        HILOGE("continuationMgrProxy is nullptr");
100686862fbSopenharmony_ci        return ERR_NULL_OBJECT;
101686862fbSopenharmony_ci    }
102686862fbSopenharmony_ci    return continuationMgrProxy->UpdateConnectStatus(token, deviceId, deviceConnectStatus);
103686862fbSopenharmony_ci}
104686862fbSopenharmony_ci
105686862fbSopenharmony_ciint32_t DistributedAbilityManagerClient::StartDeviceManager(
106686862fbSopenharmony_ci    int32_t token, const std::shared_ptr<ContinuationExtraParams>& continuationExtraParams)
107686862fbSopenharmony_ci{
108686862fbSopenharmony_ci    HILOGD("called.");
109686862fbSopenharmony_ci    sptr<IDistributedAbilityManager> continuationMgrProxy = GetContinuationMgrService();
110686862fbSopenharmony_ci    if (continuationMgrProxy == nullptr) {
111686862fbSopenharmony_ci        HILOGE("continuationMgrProxy is nullptr");
112686862fbSopenharmony_ci        return ERR_NULL_OBJECT;
113686862fbSopenharmony_ci    }
114686862fbSopenharmony_ci    return continuationMgrProxy->StartDeviceManager(token, continuationExtraParams);
115686862fbSopenharmony_ci}
116686862fbSopenharmony_ci}  // namespace DistributedSchedule
117686862fbSopenharmony_ci}  // namespace OHOS