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
16686862fbSopenharmony_ci#include "distributed_ability_manager_proxy.h"
17686862fbSopenharmony_ci
18686862fbSopenharmony_ci#include "base/continuationmgr_log.h"
19686862fbSopenharmony_ci#include "base/parcel_helper.h"
20686862fbSopenharmony_ci#include "ipc_types.h"
21686862fbSopenharmony_ci#include "string_ex.h"
22686862fbSopenharmony_ci
23686862fbSopenharmony_cinamespace OHOS {
24686862fbSopenharmony_cinamespace DistributedSchedule {
25686862fbSopenharmony_cinamespace {
26686862fbSopenharmony_ciconst std::string TAG = "ContinuationManagerProxy";
27686862fbSopenharmony_ci}
28686862fbSopenharmony_ci
29686862fbSopenharmony_ciint32_t DistributedAbilityManagerProxy::Register(
30686862fbSopenharmony_ci    const std::shared_ptr<ContinuationExtraParams>& continuationExtraParams, int32_t& token)
31686862fbSopenharmony_ci{
32686862fbSopenharmony_ci    HILOGD("called.");
33686862fbSopenharmony_ci    sptr<IRemoteObject> remote = Remote();
34686862fbSopenharmony_ci    if (remote == nullptr) {
35686862fbSopenharmony_ci        HILOGE("Register remote is null");
36686862fbSopenharmony_ci        return ERR_NULL_OBJECT;
37686862fbSopenharmony_ci    }
38686862fbSopenharmony_ci    MessageParcel data;
39686862fbSopenharmony_ci    if (!data.WriteInterfaceToken(IDistributedAbilityManager::GetDescriptor())) {
40686862fbSopenharmony_ci        return ERR_FLATTEN_OBJECT;
41686862fbSopenharmony_ci    }
42686862fbSopenharmony_ci    if (continuationExtraParams == nullptr) {
43686862fbSopenharmony_ci        PARCEL_WRITE_HELPER(data, Int32, VALUE_NULL);
44686862fbSopenharmony_ci    } else {
45686862fbSopenharmony_ci        PARCEL_WRITE_HELPER(data, Int32, VALUE_OBJECT);
46686862fbSopenharmony_ci        PARCEL_WRITE_HELPER(data, Parcelable, continuationExtraParams.get());
47686862fbSopenharmony_ci    }
48686862fbSopenharmony_ci    MessageParcel reply;
49686862fbSopenharmony_ci    MessageOption option;
50686862fbSopenharmony_ci    int32_t error = remote->SendRequest(static_cast<uint32_t>(IDAbilityManagerInterfaceCode::REGISTER),
51686862fbSopenharmony_ci        data, reply, option);
52686862fbSopenharmony_ci    if (error != ERR_NONE) {
53686862fbSopenharmony_ci        HILOGE("SendRequest error = %{public}d", error);
54686862fbSopenharmony_ci        return error;
55686862fbSopenharmony_ci    }
56686862fbSopenharmony_ci    int32_t result = reply.ReadInt32();
57686862fbSopenharmony_ci    if (result != ERR_NONE) {
58686862fbSopenharmony_ci        HILOGE("result = %{public}d", result);
59686862fbSopenharmony_ci        return result;
60686862fbSopenharmony_ci    }
61686862fbSopenharmony_ci    PARCEL_READ_HELPER(reply, Int32, token);
62686862fbSopenharmony_ci    return ERR_NONE;
63686862fbSopenharmony_ci}
64686862fbSopenharmony_ci
65686862fbSopenharmony_ciint32_t DistributedAbilityManagerProxy::Unregister(int32_t token)
66686862fbSopenharmony_ci{
67686862fbSopenharmony_ci    HILOGD("called.");
68686862fbSopenharmony_ci    sptr<IRemoteObject> remote = Remote();
69686862fbSopenharmony_ci    if (remote == nullptr) {
70686862fbSopenharmony_ci        HILOGE("Unregister remote is null");
71686862fbSopenharmony_ci        return ERR_NULL_OBJECT;
72686862fbSopenharmony_ci    }
73686862fbSopenharmony_ci    MessageParcel data;
74686862fbSopenharmony_ci    if (!data.WriteInterfaceToken(IDistributedAbilityManager::GetDescriptor())) {
75686862fbSopenharmony_ci        return ERR_FLATTEN_OBJECT;
76686862fbSopenharmony_ci    }
77686862fbSopenharmony_ci    PARCEL_WRITE_HELPER(data, Int32, token);
78686862fbSopenharmony_ci    MessageParcel reply;
79686862fbSopenharmony_ci    PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDAbilityManagerInterfaceCode::UNREGISTER), data, reply);
80686862fbSopenharmony_ci}
81686862fbSopenharmony_ci
82686862fbSopenharmony_ciint32_t DistributedAbilityManagerProxy::RegisterDeviceSelectionCallback(
83686862fbSopenharmony_ci    int32_t token, const std::string& cbType, const sptr<IRemoteObject>& notifier)
84686862fbSopenharmony_ci{
85686862fbSopenharmony_ci    HILOGD("called.");
86686862fbSopenharmony_ci    if (cbType.empty()) {
87686862fbSopenharmony_ci        HILOGE("RegisterDeviceSelectionCallback cbType is empty");
88686862fbSopenharmony_ci        return ERR_NULL_OBJECT;
89686862fbSopenharmony_ci    }
90686862fbSopenharmony_ci    if (notifier == nullptr) {
91686862fbSopenharmony_ci        HILOGE("RegisterDeviceSelectionCallback notifier is nullptr");
92686862fbSopenharmony_ci        return ERR_NULL_OBJECT;
93686862fbSopenharmony_ci    }
94686862fbSopenharmony_ci    sptr<IRemoteObject> remote = Remote();
95686862fbSopenharmony_ci    if (remote == nullptr) {
96686862fbSopenharmony_ci        HILOGE("RegisterDeviceSelectionCallback remote is null");
97686862fbSopenharmony_ci        return ERR_NULL_OBJECT;
98686862fbSopenharmony_ci    }
99686862fbSopenharmony_ci
100686862fbSopenharmony_ci    MessageParcel data;
101686862fbSopenharmony_ci    if (!data.WriteInterfaceToken(IDistributedAbilityManager::GetDescriptor())) {
102686862fbSopenharmony_ci        return ERR_FLATTEN_OBJECT;
103686862fbSopenharmony_ci    }
104686862fbSopenharmony_ci    PARCEL_WRITE_HELPER(data, Int32, token);
105686862fbSopenharmony_ci    PARCEL_WRITE_HELPER(data, String, cbType);
106686862fbSopenharmony_ci    PARCEL_WRITE_HELPER(data, RemoteObject, notifier);
107686862fbSopenharmony_ci    MessageParcel reply;
108686862fbSopenharmony_ci    PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>
109686862fbSopenharmony_ci        (IDAbilityManagerInterfaceCode::REGISTER_DEVICE_SELECTION_CALLBACK), data, reply);
110686862fbSopenharmony_ci}
111686862fbSopenharmony_ci
112686862fbSopenharmony_ciint32_t DistributedAbilityManagerProxy::UnregisterDeviceSelectionCallback(int32_t token, const std::string& cbType)
113686862fbSopenharmony_ci{
114686862fbSopenharmony_ci    HILOGD("called.");
115686862fbSopenharmony_ci    if (cbType.empty()) {
116686862fbSopenharmony_ci        HILOGE("UnregisterDeviceSelectionCallback cbType is empty");
117686862fbSopenharmony_ci        return ERR_NULL_OBJECT;
118686862fbSopenharmony_ci    }
119686862fbSopenharmony_ci    sptr<IRemoteObject> remote = Remote();
120686862fbSopenharmony_ci    if (remote == nullptr) {
121686862fbSopenharmony_ci        HILOGE("UnregisterDeviceSelectionCallback remote is null");
122686862fbSopenharmony_ci        return ERR_NULL_OBJECT;
123686862fbSopenharmony_ci    }
124686862fbSopenharmony_ci    MessageParcel data;
125686862fbSopenharmony_ci    if (!data.WriteInterfaceToken(IDistributedAbilityManager::GetDescriptor())) {
126686862fbSopenharmony_ci        return ERR_FLATTEN_OBJECT;
127686862fbSopenharmony_ci    }
128686862fbSopenharmony_ci    PARCEL_WRITE_HELPER(data, Int32, token);
129686862fbSopenharmony_ci    PARCEL_WRITE_HELPER(data, String, cbType);
130686862fbSopenharmony_ci    MessageParcel reply;
131686862fbSopenharmony_ci    PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>
132686862fbSopenharmony_ci        (IDAbilityManagerInterfaceCode::UNREGISTER_DEVICE_SELECTION_CALLBACK), data, reply);
133686862fbSopenharmony_ci}
134686862fbSopenharmony_ci
135686862fbSopenharmony_ciint32_t DistributedAbilityManagerProxy::UpdateConnectStatus(int32_t token, const std::string& deviceId,
136686862fbSopenharmony_ci    const DeviceConnectStatus& deviceConnectStatus)
137686862fbSopenharmony_ci{
138686862fbSopenharmony_ci    HILOGD("called.");
139686862fbSopenharmony_ci    sptr<IRemoteObject> remote = Remote();
140686862fbSopenharmony_ci    if (remote == nullptr) {
141686862fbSopenharmony_ci        HILOGE("UpdateConnectStatus remote is null");
142686862fbSopenharmony_ci        return ERR_NULL_OBJECT;
143686862fbSopenharmony_ci    }
144686862fbSopenharmony_ci    MessageParcel data;
145686862fbSopenharmony_ci    if (!data.WriteInterfaceToken(IDistributedAbilityManager::GetDescriptor())) {
146686862fbSopenharmony_ci        return ERR_FLATTEN_OBJECT;
147686862fbSopenharmony_ci    }
148686862fbSopenharmony_ci    PARCEL_WRITE_HELPER(data, Int32, token);
149686862fbSopenharmony_ci    PARCEL_WRITE_HELPER(data, String, deviceId);
150686862fbSopenharmony_ci    PARCEL_WRITE_HELPER(data, Int32, static_cast<int32_t>(deviceConnectStatus));
151686862fbSopenharmony_ci    MessageParcel reply;
152686862fbSopenharmony_ci    PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDAbilityManagerInterfaceCode::UPDATE_CONNECT_STATUS),
153686862fbSopenharmony_ci        data, reply);
154686862fbSopenharmony_ci}
155686862fbSopenharmony_ci
156686862fbSopenharmony_ciint32_t DistributedAbilityManagerProxy::StartDeviceManager(
157686862fbSopenharmony_ci    int32_t token, const std::shared_ptr<ContinuationExtraParams>& continuationExtraParams)
158686862fbSopenharmony_ci{
159686862fbSopenharmony_ci    HILOGD("called.");
160686862fbSopenharmony_ci    sptr<IRemoteObject> remote = Remote();
161686862fbSopenharmony_ci    if (remote == nullptr) {
162686862fbSopenharmony_ci        HILOGE("StartDeviceManager remote is null");
163686862fbSopenharmony_ci        return ERR_NULL_OBJECT;
164686862fbSopenharmony_ci    }
165686862fbSopenharmony_ci    MessageParcel data;
166686862fbSopenharmony_ci    if (!data.WriteInterfaceToken(IDistributedAbilityManager::GetDescriptor())) {
167686862fbSopenharmony_ci        return ERR_FLATTEN_OBJECT;
168686862fbSopenharmony_ci    }
169686862fbSopenharmony_ci    PARCEL_WRITE_HELPER(data, Int32, token);
170686862fbSopenharmony_ci    if (continuationExtraParams == nullptr) {
171686862fbSopenharmony_ci        PARCEL_WRITE_HELPER(data, Int32, VALUE_NULL);
172686862fbSopenharmony_ci    } else {
173686862fbSopenharmony_ci        PARCEL_WRITE_HELPER(data, Int32, VALUE_OBJECT);
174686862fbSopenharmony_ci        PARCEL_WRITE_HELPER(data, Parcelable, continuationExtraParams.get());
175686862fbSopenharmony_ci    }
176686862fbSopenharmony_ci    MessageParcel reply;
177686862fbSopenharmony_ci    PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDAbilityManagerInterfaceCode::START_DEVICE_MANAGER),
178686862fbSopenharmony_ci        data, reply);
179686862fbSopenharmony_ci}
180686862fbSopenharmony_ci} // namespace DistributedSchedule
181686862fbSopenharmony_ci} // namespace OHOS