• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "distributed_ability_manager_proxy.h"
17 
18 #include "dtbschedmgr_log.h"
19 #include "ipc_types.h"
20 #include "parcel_helper.h"
21 #include "string_ex.h"
22 
23 namespace OHOS {
24 namespace DistributedSchedule {
25 namespace {
26 const std::string TAG = "DistributedAbilityManagerProxy";
27 const std::u16string DMS_PROXY_INTERFACE_TOKEN = u"ohos.distributedschedule.accessToken";
28 }
29 
Register(const std::shared_ptr<ContinuationExtraParams> & continuationExtraParams,int32_t & token)30 int32_t DistributedAbilityManagerProxy::Register(
31     const std::shared_ptr<ContinuationExtraParams>& continuationExtraParams, int32_t& token)
32 {
33     HILOGD("called.");
34     sptr<IRemoteObject> remote = Remote();
35     if (remote == nullptr) {
36         HILOGE("Register remote is null");
37         return ERR_NULL_OBJECT;
38     }
39     MessageParcel data;
40     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
41         return ERR_FLATTEN_OBJECT;
42     }
43     if (continuationExtraParams == nullptr) {
44         PARCEL_WRITE_HELPER(data, Int32, VALUE_NULL);
45     } else {
46         PARCEL_WRITE_HELPER(data, Int32, VALUE_OBJECT);
47         PARCEL_WRITE_HELPER(data, Parcelable, continuationExtraParams.get());
48     }
49     MessageParcel reply;
50     MessageOption option;
51     int32_t error = remote->SendRequest(REGISTER, 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(DMS_PROXY_INTERFACE_TOKEN)) {
75         return ERR_FLATTEN_OBJECT;
76     }
77     PARCEL_WRITE_HELPER(data, Int32, token);
78     MessageParcel reply;
79     PARCEL_TRANSACT_SYNC_RET_INT(remote, 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(DMS_PROXY_INTERFACE_TOKEN)) {
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, REGISTER_DEVICE_SELECTION_CALLBACK, data, reply);
109 }
110 
UnregisterDeviceSelectionCallback(int32_t token,const std::string & cbType)111 int32_t DistributedAbilityManagerProxy::UnregisterDeviceSelectionCallback(int32_t token, const std::string& cbType)
112 {
113     HILOGD("called.");
114     if (cbType.empty()) {
115         HILOGE("UnregisterDeviceSelectionCallback cbType is empty");
116         return ERR_NULL_OBJECT;
117     }
118     sptr<IRemoteObject> remote = Remote();
119     if (remote == nullptr) {
120         HILOGE("UnregisterDeviceSelectionCallback remote is null");
121         return ERR_NULL_OBJECT;
122     }
123     MessageParcel data;
124     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
125         return ERR_FLATTEN_OBJECT;
126     }
127     PARCEL_WRITE_HELPER(data, Int32, token);
128     PARCEL_WRITE_HELPER(data, String, cbType);
129     MessageParcel reply;
130     PARCEL_TRANSACT_SYNC_RET_INT(remote, UNREGISTER_DEVICE_SELECTION_CALLBACK, data, reply);
131 }
132 
UpdateConnectStatus(int32_t token,const std::string & deviceId,const DeviceConnectStatus & deviceConnectStatus)133 int32_t DistributedAbilityManagerProxy::UpdateConnectStatus(int32_t token, const std::string& deviceId,
134     const DeviceConnectStatus& deviceConnectStatus)
135 {
136     HILOGD("called.");
137     sptr<IRemoteObject> remote = Remote();
138     if (remote == nullptr) {
139         HILOGE("UpdateConnectStatus remote is null");
140         return ERR_NULL_OBJECT;
141     }
142     MessageParcel data;
143     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
144         return ERR_FLATTEN_OBJECT;
145     }
146     PARCEL_WRITE_HELPER(data, Int32, token);
147     PARCEL_WRITE_HELPER(data, String, deviceId);
148     PARCEL_WRITE_HELPER(data, Int32, static_cast<int32_t>(deviceConnectStatus));
149     MessageParcel reply;
150     PARCEL_TRANSACT_SYNC_RET_INT(remote, UPDATE_CONNECT_STATUS, data, reply);
151 }
152 
StartDeviceManager(int32_t token,const std::shared_ptr<ContinuationExtraParams> & continuationExtraParams)153 int32_t DistributedAbilityManagerProxy::StartDeviceManager(
154     int32_t token, const std::shared_ptr<ContinuationExtraParams>& continuationExtraParams)
155 {
156     HILOGD("called.");
157     sptr<IRemoteObject> remote = Remote();
158     if (remote == nullptr) {
159         HILOGE("StartDeviceManager remote is null");
160         return ERR_NULL_OBJECT;
161     }
162     MessageParcel data;
163     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
164         return ERR_FLATTEN_OBJECT;
165     }
166     PARCEL_WRITE_HELPER(data, Int32, token);
167     if (continuationExtraParams == nullptr) {
168         PARCEL_WRITE_HELPER(data, Int32, VALUE_NULL);
169     } else {
170         PARCEL_WRITE_HELPER(data, Int32, VALUE_OBJECT);
171         PARCEL_WRITE_HELPER(data, Parcelable, continuationExtraParams.get());
172     }
173     MessageParcel reply;
174     PARCEL_TRANSACT_SYNC_RET_INT(remote, START_DEVICE_MANAGER, data, reply);
175 }
176 } // namespace DistributedSchedule
177 } // namespace OHOS