1 /*
2  * Copyright (c) 2022-2023 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 "distributed_ability_manager_proxy.h"
17 
18 #include "base/continuationmgr_log.h"
19 #include "base/parcel_helper.h"
20 #include "ipc_types.h"
21 #include "string_ex.h"
22 
23 namespace OHOS {
24 namespace DistributedSchedule {
25 namespace {
26 const std::string TAG = "ContinuationManagerProxy";
27 }
28 
Register(const std::shared_ptr<ContinuationExtraParams> & continuationExtraParams,int32_t & token)29 int32_t DistributedAbilityManagerProxy::Register(
30     const std::shared_ptr<ContinuationExtraParams>& continuationExtraParams, int32_t& token)
31 {
32     HILOGD("called.");
33     sptr<IRemoteObject> remote = Remote();
34     if (remote == nullptr) {
35         HILOGE("Register remote is null");
36         return ERR_NULL_OBJECT;
37     }
38     MessageParcel data;
39     if (!data.WriteInterfaceToken(IDistributedAbilityManager::GetDescriptor())) {
40         return ERR_FLATTEN_OBJECT;
41     }
42     if (continuationExtraParams == nullptr) {
43         PARCEL_WRITE_HELPER(data, Int32, VALUE_NULL);
44     } else {
45         PARCEL_WRITE_HELPER(data, Int32, VALUE_OBJECT);
46         PARCEL_WRITE_HELPER(data, Parcelable, continuationExtraParams.get());
47     }
48     MessageParcel reply;
49     MessageOption option;
50     int32_t error = remote->SendRequest(static_cast<uint32_t>(IDAbilityManagerInterfaceCode::REGISTER),
51         data, reply, option);
52     if (error != ERR_NONE) {
53         HILOGE("SendRequest error = %{public}d", error);
54         return error;
55     }
56     int32_t result = reply.ReadInt32();
57     if (result != ERR_NONE) {
58         HILOGE("result = %{public}d", result);
59         return result;
60     }
61     PARCEL_READ_HELPER(reply, Int32, token);
62     return ERR_NONE;
63 }
64 
Unregister(int32_t token)65 int32_t DistributedAbilityManagerProxy::Unregister(int32_t token)
66 {
67     HILOGD("called.");
68     sptr<IRemoteObject> remote = Remote();
69     if (remote == nullptr) {
70         HILOGE("Unregister remote is null");
71         return ERR_NULL_OBJECT;
72     }
73     MessageParcel data;
74     if (!data.WriteInterfaceToken(IDistributedAbilityManager::GetDescriptor())) {
75         return ERR_FLATTEN_OBJECT;
76     }
77     PARCEL_WRITE_HELPER(data, Int32, token);
78     MessageParcel reply;
79     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDAbilityManagerInterfaceCode::UNREGISTER), data, reply);
80 }
81 
RegisterDeviceSelectionCallback(int32_t token,const std::string & cbType,const sptr<IRemoteObject> & notifier)82 int32_t DistributedAbilityManagerProxy::RegisterDeviceSelectionCallback(
83     int32_t token, const std::string& cbType, const sptr<IRemoteObject>& notifier)
84 {
85     HILOGD("called.");
86     if (cbType.empty()) {
87         HILOGE("RegisterDeviceSelectionCallback cbType is empty");
88         return ERR_NULL_OBJECT;
89     }
90     if (notifier == nullptr) {
91         HILOGE("RegisterDeviceSelectionCallback notifier is nullptr");
92         return ERR_NULL_OBJECT;
93     }
94     sptr<IRemoteObject> remote = Remote();
95     if (remote == nullptr) {
96         HILOGE("RegisterDeviceSelectionCallback remote is null");
97         return ERR_NULL_OBJECT;
98     }
99 
100     MessageParcel data;
101     if (!data.WriteInterfaceToken(IDistributedAbilityManager::GetDescriptor())) {
102         return ERR_FLATTEN_OBJECT;
103     }
104     PARCEL_WRITE_HELPER(data, Int32, token);
105     PARCEL_WRITE_HELPER(data, String, cbType);
106     PARCEL_WRITE_HELPER(data, RemoteObject, notifier);
107     MessageParcel reply;
108     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>
109         (IDAbilityManagerInterfaceCode::REGISTER_DEVICE_SELECTION_CALLBACK), data, reply);
110 }
111 
UnregisterDeviceSelectionCallback(int32_t token,const std::string & cbType)112 int32_t DistributedAbilityManagerProxy::UnregisterDeviceSelectionCallback(int32_t token, const std::string& cbType)
113 {
114     HILOGD("called.");
115     if (cbType.empty()) {
116         HILOGE("UnregisterDeviceSelectionCallback cbType is empty");
117         return ERR_NULL_OBJECT;
118     }
119     sptr<IRemoteObject> remote = Remote();
120     if (remote == nullptr) {
121         HILOGE("UnregisterDeviceSelectionCallback remote is null");
122         return ERR_NULL_OBJECT;
123     }
124     MessageParcel data;
125     if (!data.WriteInterfaceToken(IDistributedAbilityManager::GetDescriptor())) {
126         return ERR_FLATTEN_OBJECT;
127     }
128     PARCEL_WRITE_HELPER(data, Int32, token);
129     PARCEL_WRITE_HELPER(data, String, cbType);
130     MessageParcel reply;
131     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>
132         (IDAbilityManagerInterfaceCode::UNREGISTER_DEVICE_SELECTION_CALLBACK), data, reply);
133 }
134 
UpdateConnectStatus(int32_t token,const std::string & deviceId,const DeviceConnectStatus & deviceConnectStatus)135 int32_t DistributedAbilityManagerProxy::UpdateConnectStatus(int32_t token, const std::string& deviceId,
136     const DeviceConnectStatus& deviceConnectStatus)
137 {
138     HILOGD("called.");
139     sptr<IRemoteObject> remote = Remote();
140     if (remote == nullptr) {
141         HILOGE("UpdateConnectStatus remote is null");
142         return ERR_NULL_OBJECT;
143     }
144     MessageParcel data;
145     if (!data.WriteInterfaceToken(IDistributedAbilityManager::GetDescriptor())) {
146         return ERR_FLATTEN_OBJECT;
147     }
148     PARCEL_WRITE_HELPER(data, Int32, token);
149     PARCEL_WRITE_HELPER(data, String, deviceId);
150     PARCEL_WRITE_HELPER(data, Int32, static_cast<int32_t>(deviceConnectStatus));
151     MessageParcel reply;
152     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDAbilityManagerInterfaceCode::UPDATE_CONNECT_STATUS),
153         data, reply);
154 }
155 
StartDeviceManager(int32_t token,const std::shared_ptr<ContinuationExtraParams> & continuationExtraParams)156 int32_t DistributedAbilityManagerProxy::StartDeviceManager(
157     int32_t token, const std::shared_ptr<ContinuationExtraParams>& continuationExtraParams)
158 {
159     HILOGD("called.");
160     sptr<IRemoteObject> remote = Remote();
161     if (remote == nullptr) {
162         HILOGE("StartDeviceManager remote is null");
163         return ERR_NULL_OBJECT;
164     }
165     MessageParcel data;
166     if (!data.WriteInterfaceToken(IDistributedAbilityManager::GetDescriptor())) {
167         return ERR_FLATTEN_OBJECT;
168     }
169     PARCEL_WRITE_HELPER(data, Int32, token);
170     if (continuationExtraParams == nullptr) {
171         PARCEL_WRITE_HELPER(data, Int32, VALUE_NULL);
172     } else {
173         PARCEL_WRITE_HELPER(data, Int32, VALUE_OBJECT);
174         PARCEL_WRITE_HELPER(data, Parcelable, continuationExtraParams.get());
175     }
176     MessageParcel reply;
177     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDAbilityManagerInterfaceCode::START_DEVICE_MANAGER),
178         data, reply);
179 }
180 } // namespace DistributedSchedule
181 } // namespace OHOS