1 /*
2  * Copyright (c) 2021-2024 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_input_source_proxy.h"
17 
18 #include "dinput_errcode.h"
19 #include "dinput_ipc_interface_code.h"
20 #include "dinput_log.h"
21 
22 namespace OHOS {
23 namespace DistributedHardware {
24 namespace DistributedInput {
DistributedInputSourceProxy(const sptr<IRemoteObject> & object)25 DistributedInputSourceProxy::DistributedInputSourceProxy(const sptr<IRemoteObject> &object)
26     : IRemoteProxy<IDistributedSourceInput>(object)
27 {}
28 
~DistributedInputSourceProxy()29 DistributedInputSourceProxy::~DistributedInputSourceProxy()
30 {}
31 
Init()32 int32_t DistributedInputSourceProxy::Init()
33 {
34     MessageParcel data;
35     MessageParcel reply;
36     if (!data.WriteInterfaceToken(GetDescriptor())) {
37         DHLOGE("DistributedInputSourceProxy write token valid failed");
38         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
39     }
40     int32_t result = ERR_DH_INPUT_SOURCE_PROXY_INIT_FAIL;
41     bool ret = SendRequest(static_cast<uint32_t>(IDInputSourceInterfaceCode::INIT), data, reply);
42     if (ret) {
43         result = reply.ReadInt32();
44     }
45     return result;
46 }
47 
Release()48 int32_t DistributedInputSourceProxy::Release()
49 {
50     MessageParcel data;
51     MessageParcel reply;
52     int32_t result = ERR_DH_INPUT_SOURCE_PROXY_RELEASE_FAIL;
53     if (!data.WriteInterfaceToken(GetDescriptor())) {
54         DHLOGE("DistributedInputSourceProxy write token valid failed");
55         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
56     }
57     bool ret = SendRequest(static_cast<uint32_t>(IDInputSourceInterfaceCode::RELEASE), data, reply);
58     if (ret) {
59         result = reply.ReadInt32();
60     }
61     return result;
62 }
63 
RegisterDistributedHardware(const std::string & devId,const std::string & dhId,const std::string & parameters,sptr<IRegisterDInputCallback> callback)64 int32_t DistributedInputSourceProxy::RegisterDistributedHardware(const std::string &devId, const std::string &dhId,
65     const std::string &parameters, sptr<IRegisterDInputCallback> callback)
66 {
67     MessageParcel data;
68     if (!data.WriteInterfaceToken(GetDescriptor())) {
69         DHLOGE("DistributedInputSourceProxy write token valid failed");
70         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
71     }
72     if (!data.WriteString(devId)) {
73         DHLOGE("DistributedInputSourceProxy write devId failed");
74         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
75     }
76     if (!data.WriteString(dhId)) {
77         DHLOGE("DistributedInputSourceProxy write dhId failed");
78         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
79     }
80     if (!data.WriteString(parameters)) {
81         DHLOGE("DistributedInputSourceProxy write parameters failed");
82         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
83     }
84     if (!data.WriteRemoteObject(callback->AsObject())) {
85         DHLOGE("DistributedInputSourceProxy write callback failed");
86         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
87     }
88     MessageParcel reply;
89     int32_t result = ERR_DH_INPUT_SOURCE_PROXY_REGISTER_FAIL;
90     bool ret = SendRequest(static_cast<uint32_t>(IDInputSourceInterfaceCode::REGISTER_REMOTE_INPUT), data, reply);
91     if (ret) {
92         result = reply.ReadInt32();
93     }
94     return result;
95 }
96 
UnregisterDistributedHardware(const std::string & devId,const std::string & dhId,sptr<IUnregisterDInputCallback> callback)97 int32_t DistributedInputSourceProxy::UnregisterDistributedHardware(const std::string &devId, const std::string &dhId,
98     sptr<IUnregisterDInputCallback> callback)
99 {
100     if (callback == nullptr) {
101         DHLOGE("callback is nullptr");
102         return ERR_DH_INPUT_SRC_PROXY_CALLBACK_IS_NULL;
103     }
104     MessageParcel data;
105     if (!data.WriteInterfaceToken(GetDescriptor())) {
106         DHLOGE("DistributedInputSourceProxy write token valid failed");
107         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
108     }
109     if (!data.WriteString(devId)) {
110         DHLOGE("DistributedInputSourceProxy write devId failed");
111         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
112     }
113     if (!data.WriteString(dhId)) {
114         DHLOGE("DistributedInputSourceProxy write dhId failed");
115         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
116     }
117     if (!data.WriteRemoteObject(callback->AsObject())) {
118         DHLOGE("DistributedInputSourceProxy write callback failed");
119         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
120     }
121     MessageParcel reply;
122     int32_t result = ERR_DH_INPUT_SOURCE_PROXY_UNREGISTER_FAIL;
123     bool ret = SendRequest(static_cast<uint32_t>(IDInputSourceInterfaceCode::UNREGISTER_REMOTE_INPUT), data, reply);
124     if (ret) {
125         result = reply.ReadInt32();
126     }
127     return result;
128 }
129 
PrepareRemoteInput(const std::string & deviceId,sptr<IPrepareDInputCallback> callback)130 int32_t DistributedInputSourceProxy::PrepareRemoteInput(
131     const std::string &deviceId, sptr<IPrepareDInputCallback> callback)
132 {
133     if (callback == nullptr) {
134         DHLOGE("callback is nullptr");
135         return ERR_DH_INPUT_SRC_PROXY_CALLBACK_IS_NULL;
136     }
137     MessageParcel data;
138     if (!data.WriteInterfaceToken(GetDescriptor())) {
139         DHLOGE("DistributedInputSourceProxy write token valid failed");
140         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
141     }
142     if (!data.WriteString(deviceId)) {
143         DHLOGE("DistributedInputSourceProxy write deviceId failed");
144         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
145     }
146     if (!data.WriteRemoteObject(callback->AsObject())) {
147         DHLOGE("DistributedInputSourceProxy write callback failed");
148         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
149     }
150 
151     MessageParcel reply;
152     int32_t result = ERR_DH_INPUT_SOURCE_PROXY_PREPARE_FAIL;
153     bool ret = SendRequest(static_cast<uint32_t>(IDInputSourceInterfaceCode::PREPARE_REMOTE_INPUT), data, reply);
154     if (ret) {
155         result = reply.ReadInt32();
156     }
157     DHLOGI("PrepareRemoteInput end, result:%{public}d", result);
158     return result;
159 }
160 
UnprepareRemoteInput(const std::string & deviceId,sptr<IUnprepareDInputCallback> callback)161 int32_t DistributedInputSourceProxy::UnprepareRemoteInput(const std::string &deviceId,
162     sptr<IUnprepareDInputCallback> callback)
163 {
164     if (callback == nullptr) {
165         DHLOGE("callback is nullptr");
166         return ERR_DH_INPUT_SRC_PROXY_CALLBACK_IS_NULL;
167     }
168     MessageParcel data;
169     if (!data.WriteInterfaceToken(GetDescriptor())) {
170         DHLOGE("DistributedInputSourceProxy write token valid failed");
171         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
172     }
173     if (!data.WriteString(deviceId)) {
174         DHLOGE("DistributedInputSourceProxy write deviceId failed");
175         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
176     }
177     if (!data.WriteRemoteObject(callback->AsObject())) {
178         DHLOGE("DistributedInputSourceProxy write callback failed");
179         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
180     }
181     MessageParcel reply;
182     int32_t result = ERR_DH_INPUT_SOURCE_PROXY_UNPREPARE_FAIL;
183     bool ret = SendRequest(static_cast<uint32_t>(IDInputSourceInterfaceCode::UNPREPARE_REMOTE_INPUT), data, reply);
184     if (ret) {
185         result = reply.ReadInt32();
186     }
187     DHLOGE("UnprepareRemoteInput end, result=%{public}d", result);
188     return result;
189 }
190 
StartRemoteInput(const std::string & deviceId,const uint32_t & inputTypes,sptr<IStartDInputCallback> callback)191 int32_t DistributedInputSourceProxy::StartRemoteInput(
192     const std::string &deviceId, const uint32_t &inputTypes, sptr<IStartDInputCallback> callback)
193 {
194     if (callback == nullptr) {
195         DHLOGE("callback is nullptr");
196         return ERR_DH_INPUT_SRC_PROXY_CALLBACK_IS_NULL;
197     }
198     DHLOGI("source proxy StartRemoteInput start");
199     MessageParcel data;
200     if (!data.WriteInterfaceToken(GetDescriptor())) {
201         DHLOGE("DistributedInputSourceProxy write token valid failed");
202         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
203     }
204     if (!data.WriteString(deviceId)) {
205         DHLOGE("DistributedInputSourceProxy write deviceId failed");
206         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
207     }
208     if (!data.WriteUint32(inputTypes)) {
209         DHLOGE("DistributedInputSourceProxy write inputTypes failed");
210         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
211     }
212     if (!data.WriteRemoteObject(callback->AsObject())) {
213         DHLOGE("DistributedInputSourceProxy write callback failed");
214         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
215     }
216     MessageParcel reply;
217     int32_t result = ERR_DH_INPUT_SOURCE_PROXY_START_FAIL;
218     bool ret = SendRequest(static_cast<uint32_t>(IDInputSourceInterfaceCode::START_REMOTE_INPUT), data, reply);
219     if (ret) {
220         result = reply.ReadInt32();
221     }
222     DHLOGI("source proxy StartRemoteInput end, result:%{public}d.", result);
223     return result;
224 }
225 
StopRemoteInput(const std::string & deviceId,const uint32_t & inputTypes,sptr<IStopDInputCallback> callback)226 int32_t DistributedInputSourceProxy::StopRemoteInput(
227     const std::string &deviceId, const uint32_t &inputTypes, sptr<IStopDInputCallback> callback)
228 {
229     if (callback == nullptr) {
230         DHLOGE("callback is nullptr");
231         return ERR_DH_INPUT_SRC_PROXY_CALLBACK_IS_NULL;
232     }
233     MessageParcel data;
234     if (!data.WriteInterfaceToken(GetDescriptor())) {
235         DHLOGE("DistributedInputSourceProxy write token valid failed");
236         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
237     }
238     if (!data.WriteString(deviceId)) {
239         DHLOGE("DistributedInputSourceProxy write deviceId failed");
240         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
241     }
242     if (!data.WriteUint32(inputTypes)) {
243         DHLOGE("DistributedInputSourceProxy write inputTypes failed");
244         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
245     }
246     if (!data.WriteRemoteObject(callback->AsObject())) {
247         DHLOGE("DistributedInputSourceProxy write callback failed");
248         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
249     }
250     MessageParcel reply;
251     int32_t result = ERR_DH_INPUT_SOURCE_PROXY_STOP_FAIL;
252     bool ret = SendRequest(static_cast<uint32_t>(IDInputSourceInterfaceCode::STOP_REMOTE_INPUT), data, reply);
253     if (ret) {
254         result = reply.ReadInt32();
255     }
256     return result;
257 }
258 
StartRemoteInput(const std::string & srcId,const std::string & sinkId,const uint32_t & inputTypes,sptr<IStartDInputCallback> callback)259 int32_t DistributedInputSourceProxy::StartRemoteInput(const std::string &srcId, const std::string &sinkId,
260     const uint32_t &inputTypes, sptr<IStartDInputCallback> callback)
261 {
262     if (callback == nullptr) {
263         DHLOGE("callback is nullptr");
264         return ERR_DH_INPUT_SRC_PROXY_CALLBACK_IS_NULL;
265     }
266     DHLOGI("source proxy StartRemoteInput start");
267     MessageParcel data;
268     if (!data.WriteInterfaceToken(GetDescriptor())) {
269         DHLOGE("DistributedInputSourceProxy write token valid failed");
270         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
271     }
272     if (!data.WriteString(srcId)) {
273         DHLOGE("DistributedInputSourceProxy write srcId failed");
274         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
275     }
276     if (!data.WriteString(sinkId)) {
277         DHLOGE("DistributedInputSourceProxy write sinkId failed");
278         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
279     }
280     if (!data.WriteUint32(inputTypes)) {
281         DHLOGE("DistributedInputSourceProxy write inputTypes failed");
282         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
283     }
284     if (!data.WriteRemoteObject(callback->AsObject())) {
285         DHLOGE("DistributedInputSourceProxy write callback failed");
286         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
287     }
288     MessageParcel reply;
289     int32_t result = ERR_DH_INPUT_SOURCE_PROXY_START_FAIL;
290     bool ret = SendRequest(static_cast<uint32_t>(IDInputSourceInterfaceCode::START_RELAY_TYPE_REMOTE_INPUT), data,
291         reply);
292     if (ret) {
293         result = reply.ReadInt32();
294     }
295     DHLOGI("source proxy StartRemoteInput end, result:%{public}d.", result);
296     return result;
297 }
298 
StopRemoteInput(const std::string & srcId,const std::string & sinkId,const uint32_t & inputTypes,sptr<IStopDInputCallback> callback)299 int32_t DistributedInputSourceProxy::StopRemoteInput(const std::string &srcId, const std::string &sinkId,
300     const uint32_t &inputTypes, sptr<IStopDInputCallback> callback)
301 {
302     if (callback == nullptr) {
303         DHLOGE("callback is nullptr");
304         return ERR_DH_INPUT_SRC_PROXY_CALLBACK_IS_NULL;
305     }
306     MessageParcel data;
307     if (!data.WriteInterfaceToken(GetDescriptor())) {
308         DHLOGE("DistributedInputSourceProxy write token valid failed");
309         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
310     }
311     if (!data.WriteString(srcId)) {
312         DHLOGE("DistributedInputSourceProxy write srcId failed");
313         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
314     }
315     if (!data.WriteString(sinkId)) {
316         DHLOGE("DistributedInputSourceProxy write sinkId failed");
317         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
318     }
319     if (!data.WriteUint32(inputTypes)) {
320         DHLOGE("DistributedInputSourceProxy write inputTypes failed");
321         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
322     }
323     if (!data.WriteRemoteObject(callback->AsObject())) {
324         DHLOGE("DistributedInputSourceProxy write callback failed");
325         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
326     }
327     MessageParcel reply;
328     int32_t result = ERR_DH_INPUT_SOURCE_PROXY_STOP_FAIL;
329     bool ret = SendRequest(static_cast<uint32_t>(IDInputSourceInterfaceCode::STOP_RELAY_TYPE_REMOTE_INPUT),
330         data, reply);
331     if (ret) {
332         result = reply.ReadInt32();
333     }
334     return result;
335 }
336 
PrepareRemoteInput(const std::string & srcId,const std::string & sinkId,sptr<IPrepareDInputCallback> callback)337 int32_t DistributedInputSourceProxy::PrepareRemoteInput(const std::string &srcId, const std::string &sinkId,
338     sptr<IPrepareDInputCallback> callback)
339 {
340     if (callback == nullptr) {
341         DHLOGE("callback is nullptr");
342         return ERR_DH_INPUT_SRC_PROXY_CALLBACK_IS_NULL;
343     }
344     MessageParcel data;
345     if (!data.WriteInterfaceToken(GetDescriptor())) {
346         DHLOGE("DistributedInputSourceProxy write token valid failed");
347         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
348     }
349     if (!data.WriteString(srcId)) {
350         DHLOGE("Write PrepareRemoteInput relay srcid to parcel failed");
351         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
352     }
353     if (!data.WriteString(sinkId)) {
354         DHLOGE("Write PrepareRemoteInput relay sinkid to parcel failed");
355         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
356     }
357     if (!data.WriteRemoteObject(callback->AsObject())) {
358         DHLOGE("Write PrepareRemoteInput relay callback to parcel failed");
359         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
360     }
361 
362     MessageParcel reply;
363     int32_t result = ERR_DH_INPUT_SOURCE_PROXY_PREPARE_FAIL;
364     bool ret = SendRequest(static_cast<uint32_t>(IDInputSourceInterfaceCode::PREPARE_RELAY_REMOTE_INPUT), data, reply);
365     if (ret) {
366         result = reply.ReadInt32();
367     }
368     DHLOGI("PrepareRemoteInput relay result:%{public}d", result);
369     return result;
370 }
371 
UnprepareRemoteInput(const std::string & srcId,const std::string & sinkId,sptr<IUnprepareDInputCallback> callback)372 int32_t DistributedInputSourceProxy::UnprepareRemoteInput(const std::string &srcId, const std::string &sinkId,
373     sptr<IUnprepareDInputCallback> callback)
374 {
375     if (callback == nullptr) {
376         DHLOGE("callback is nullptr");
377         return ERR_DH_INPUT_SRC_PROXY_CALLBACK_IS_NULL;
378     }
379     MessageParcel data;
380     if (!data.WriteInterfaceToken(GetDescriptor())) {
381         DHLOGE("DistributedInputSourceProxy write token valid failed");
382         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
383     }
384     if (!data.WriteString(srcId)) {
385         DHLOGE("Write UnprepareRemoteInput relay srcid to parcel failed");
386         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
387     }
388     if (!data.WriteString(sinkId)) {
389         DHLOGE("Write UnprepareRemoteInput relay sinkid to parcel failed");
390         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
391     }
392     if (!data.WriteRemoteObject(callback->AsObject())) {
393         DHLOGE("Write UnprepareRemoteInput relay callback to parcel failed");
394         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
395     }
396     MessageParcel reply;
397     int32_t result = ERR_DH_INPUT_SOURCE_PROXY_UNPREPARE_FAIL;
398     bool ret = SendRequest(static_cast<uint32_t>(IDInputSourceInterfaceCode::UNPREPARE_RELAY_REMOTE_INPUT),
399         data, reply);
400     if (ret) {
401         result = reply.ReadInt32();
402     }
403     DHLOGI("UnprepareRemoteInput relay result:%{public}d", result);
404     return result;
405 }
406 
StartRemoteInput(const std::string & sinkId,const std::vector<std::string> & dhIds,sptr<IStartStopDInputsCallback> callback)407 int32_t DistributedInputSourceProxy::StartRemoteInput(const std::string &sinkId, const std::vector<std::string> &dhIds,
408     sptr<IStartStopDInputsCallback> callback)
409 {
410     if (callback == nullptr) {
411         DHLOGE("callback is nullptr");
412         return ERR_DH_INPUT_SRC_PROXY_CALLBACK_IS_NULL;
413     }
414     MessageParcel data;
415     if (!data.WriteInterfaceToken(GetDescriptor())) {
416         DHLOGE("DistributedInputSourceProxy write token valid failed");
417         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
418     }
419     if (!data.WriteString(sinkId)) {
420         DHLOGE("Write StartRemoteInput dhid sinkId to parcel failed");
421         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
422     }
423 
424     if (!data.WriteUint32(dhIds.size())) {
425         DHLOGE("Write StartRemoteInput dhid size to parcel failed");
426         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
427     }
428     for (auto it = dhIds.begin(); it != dhIds.end(); ++it) {
429         if (!data.WriteString(*it)) {
430             DHLOGE("Write StartRemoteInput dhid dhid to parcel failed");
431             return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
432         }
433     }
434 
435     if (!data.WriteRemoteObject(callback->AsObject())) {
436         DHLOGE("Write StartRemoteInput dhid callback to parcel failed");
437         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
438     }
439     MessageParcel reply;
440     int32_t result = ERR_DH_INPUT_SOURCE_PROXY_START_FAIL;
441     bool ret = SendRequest(static_cast<uint32_t>(IDInputSourceInterfaceCode::START_DHID_REMOTE_INPUT), data, reply);
442     if (ret) {
443         result = reply.ReadInt32();
444     }
445     DHLOGI("Source proxy StartRemoteInput dhid end, result:%{public}d.", result);
446     return result;
447 }
448 
StopRemoteInput(const std::string & sinkId,const std::vector<std::string> & dhIds,sptr<IStartStopDInputsCallback> callback)449 int32_t DistributedInputSourceProxy::StopRemoteInput(const std::string &sinkId, const std::vector<std::string> &dhIds,
450     sptr<IStartStopDInputsCallback> callback)
451 {
452     if (callback == nullptr) {
453         DHLOGE("callback is nullptr");
454         return ERR_DH_INPUT_SRC_PROXY_CALLBACK_IS_NULL;
455     }
456     MessageParcel data;
457     if (!data.WriteInterfaceToken(GetDescriptor())) {
458         DHLOGE("DistributedInputSourceProxy write token valid failed");
459         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
460     }
461     if (!data.WriteString(sinkId)) {
462         DHLOGE("Write StopRemoteInput dhid sinkId to parcel failed");
463         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
464     }
465 
466     if (!data.WriteUint32(dhIds.size())) {
467         DHLOGE("Write StopRemoteInput dhid size to parcel failed");
468         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
469     }
470     for (auto it = dhIds.begin(); it != dhIds.end(); ++it) {
471         if (!data.WriteString(*it)) {
472             DHLOGE("Write StopRemoteInput dhid dhid to parcel failed");
473             return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
474         }
475     }
476 
477     if (!data.WriteRemoteObject(callback->AsObject())) {
478         DHLOGE("Write StopRemoteInput dhid callback to parcel failed");
479         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
480     }
481     MessageParcel reply;
482     int32_t result = ERR_DH_INPUT_SOURCE_PROXY_STOP_FAIL;
483     bool ret = SendRequest(static_cast<uint32_t>(IDInputSourceInterfaceCode::STOP_DHID_REMOTE_INPUT), data, reply);
484     if (ret) {
485         result = reply.ReadInt32();
486     }
487     DHLOGI("Source proxy StopRemoteInput dhid end, result:%{public}d.", result);
488     return result;
489 }
490 
StartRemoteInput(const std::string & srcId,const std::string & sinkId,const std::vector<std::string> & dhIds,sptr<IStartStopDInputsCallback> callback)491 int32_t DistributedInputSourceProxy::StartRemoteInput(const std::string &srcId, const std::string &sinkId,
492     const std::vector<std::string> &dhIds, sptr<IStartStopDInputsCallback> callback)
493 {
494     if (callback == nullptr) {
495         DHLOGE("callback is nullptr");
496         return ERR_DH_INPUT_SRC_PROXY_CALLBACK_IS_NULL;
497     }
498     MessageParcel data;
499     if (!data.WriteInterfaceToken(GetDescriptor())) {
500         DHLOGE("DistributedInputSourceProxy relay dhid write token valid failed");
501         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
502     }
503     if (!data.WriteString(srcId)) {
504         DHLOGE("Write StartRemoteInput relay dhid srcId to parcel failed");
505         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
506     }
507     if (!data.WriteString(sinkId)) {
508         DHLOGE("Write StartRemoteInput relay dhid sinkId to parcel failed");
509         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
510     }
511 
512     if (!data.WriteUint32(dhIds.size())) {
513         DHLOGE("Write StartRemoteInput relay dhid size to parcel failed");
514         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
515     }
516     for (auto it = dhIds.begin(); it != dhIds.end(); ++it) {
517         if (!data.WriteString(*it)) {
518             DHLOGE("Write StartRemoteInput relay dhid dhid to parcel failed");
519             return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
520         }
521     }
522 
523     if (!data.WriteRemoteObject(callback->AsObject())) {
524         DHLOGE("Write StartRemoteInput relay dhid callback to parcel failed");
525         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
526     }
527     MessageParcel reply;
528     int32_t result = ERR_DH_INPUT_SOURCE_PROXY_START_FAIL;
529     bool ret = SendRequest(static_cast<uint32_t>(IDInputSourceInterfaceCode::START_RELAY_DHID_REMOTE_INPUT),
530         data, reply);
531     if (ret) {
532         result = reply.ReadInt32();
533     }
534     DHLOGI("Source proxy StartRemoteInput relay dhid end, result:%{public}d.", result);
535     return result;
536 }
537 
StopRemoteInput(const std::string & srcId,const std::string & sinkId,const std::vector<std::string> & dhIds,sptr<IStartStopDInputsCallback> callback)538 int32_t DistributedInputSourceProxy::StopRemoteInput(const std::string &srcId, const std::string &sinkId,
539     const std::vector<std::string> &dhIds, sptr<IStartStopDInputsCallback> callback)
540 {
541     if (callback == nullptr) {
542         DHLOGE("callback is nullptr");
543         return ERR_DH_INPUT_SRC_PROXY_CALLBACK_IS_NULL;
544     }
545     MessageParcel data;
546     if (!data.WriteInterfaceToken(GetDescriptor())) {
547         DHLOGE("DistributedInputSourceProxy write token valid failed");
548         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
549     }
550     if (!data.WriteString(srcId)) {
551         DHLOGE("Write StopRemoteInput relay dhid srcId to parcel failed");
552         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
553     }
554     if (!data.WriteString(sinkId)) {
555         DHLOGE("Write StopRemoteInput relay dhid sinkId to parcel failed");
556         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
557     }
558 
559     if (!data.WriteUint32(dhIds.size())) {
560         DHLOGE("Write StopRemoteInput relay dhid size to parcel failed");
561         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
562     }
563     for (auto it = dhIds.begin(); it != dhIds.end(); ++it) {
564         if (!data.WriteString(*it)) {
565             DHLOGE("Write StopRemoteInput relay dhid dhid to parcel failed");
566             return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
567         }
568     }
569 
570     if (!data.WriteRemoteObject(callback->AsObject())) {
571         DHLOGE("Write StopRemoteInput relay dhid callback to parcel failed");
572         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
573     }
574     MessageParcel reply;
575     int32_t result = ERR_DH_INPUT_SOURCE_PROXY_STOP_FAIL;
576     bool ret = SendRequest(static_cast<uint32_t>(IDInputSourceInterfaceCode::STOP_RELAY_DHID_REMOTE_INPUT),
577         data, reply);
578     if (ret) {
579         result = reply.ReadInt32();
580     }
581     DHLOGI("Source proxy StopRemoteInput relay dhid end, result:%{public}d.", result);
582     return result;
583 }
584 
RegisterAddWhiteListCallback(sptr<IAddWhiteListInfosCallback> addWhiteListCallback)585 int32_t DistributedInputSourceProxy::RegisterAddWhiteListCallback(sptr<IAddWhiteListInfosCallback> addWhiteListCallback)
586     __attribute__((no_sanitize("cfi")))
587 {
588     if (addWhiteListCallback == nullptr) {
589         DHLOGE("DistributedInputSourceProxy addWhiteListCallback is null.");
590         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
591     }
592     MessageParcel data;
593     if (!data.WriteInterfaceToken(GetDescriptor())) {
594         DHLOGE("DistributedInputSourceProxy write token valid failed");
595         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
596     }
597     if (!data.WriteRemoteObject(addWhiteListCallback->AsObject())) {
598         DHLOGE("DistributedInputSourceProxy write callback failed");
599         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
600     }
601     MessageParcel reply;
602     int32_t result = ERR_DH_INPUT_SOURCE_PROXY_REGISTER_WHITELIST_FAIL;
603     bool ret = SendRequest(static_cast<uint32_t>(IDInputSourceInterfaceCode::REGISTER_ADD_WHITE_LIST_CB_REMOTE_INPUT),
604         data, reply);
605     if (ret) {
606         result = reply.ReadInt32();
607     }
608     return result;
609 }
610 
RegisterDelWhiteListCallback(sptr<IDelWhiteListInfosCallback> delWhiteListCallback)611 int32_t DistributedInputSourceProxy::RegisterDelWhiteListCallback(sptr<IDelWhiteListInfosCallback> delWhiteListCallback)
612 {
613     if (delWhiteListCallback == nullptr) {
614         DHLOGE("DistributedInputSourceProxy delWhiteListCallback is null.");
615         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
616     }
617     MessageParcel data;
618     if (!data.WriteInterfaceToken(GetDescriptor())) {
619         DHLOGE("DistributedInputSourceProxy write token valid failed");
620         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
621     }
622     if (!data.WriteRemoteObject(delWhiteListCallback->AsObject())) {
623         DHLOGE("DistributedInputSourceProxy write callback failed");
624         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
625     }
626     MessageParcel reply;
627     int32_t result = ERR_DH_INPUT_SOURCE_PROXY_REGISTER_WHITELIST_FAIL;
628     bool ret = SendRequest(static_cast<uint32_t>(IDInputSourceInterfaceCode::REGISTER_DEL_WHITE_LIST_CB_REMOTE_INPUT),
629         data, reply);
630     if (ret) {
631         result = reply.ReadInt32();
632     }
633     return result;
634 }
635 
RegisterSimulationEventListener(sptr<ISimulationEventListener> listener)636 int32_t DistributedInputSourceProxy::RegisterSimulationEventListener(sptr<ISimulationEventListener> listener)
637 {
638     if (listener == nullptr) {
639         DHLOGE("callback is nullptr");
640         return ERR_DH_INPUT_SRC_PROXY_EVENT_LISTENER_IS_NULL;
641     }
642     MessageParcel data;
643     if (!data.WriteInterfaceToken(GetDescriptor())) {
644         DHLOGE("RegisterSimulationEventListener write token valid failed");
645         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
646     }
647 
648     if (!data.WriteRemoteObject(listener->AsObject())) {
649         DHLOGE("RegisterSimulationEventListener write callback failed");
650         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
651     }
652 
653     MessageParcel reply;
654     int32_t result = ERR_DH_INPUT_SOURCE_PROXY_REGISTER_SIMULATION_LISTENER_FAIL;
655     bool ret = SendRequest(static_cast<uint32_t>(IDInputSourceInterfaceCode::REGISTER_SIMULATION_EVENT_LISTENER),
656         data, reply);
657     if (ret) {
658         result = reply.ReadInt32();
659     }
660     return result;
661 }
662 
UnregisterSimulationEventListener(sptr<ISimulationEventListener> listener)663 int32_t DistributedInputSourceProxy::UnregisterSimulationEventListener(sptr<ISimulationEventListener> listener)
664 {
665     if (listener == nullptr) {
666         DHLOGE("callback is nullptr");
667         return ERR_DH_INPUT_SRC_PROXY_EVENT_LISTENER_IS_NULL;
668     }
669     MessageParcel data;
670     if (!data.WriteInterfaceToken(GetDescriptor())) {
671         DHLOGE("UnregisterSimulationEventListener write token valid failed");
672         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
673     }
674 
675     if (!data.WriteRemoteObject(listener->AsObject())) {
676         DHLOGE("UnregisterSimulationEventListener write callback failed");
677         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
678     }
679 
680     MessageParcel reply;
681     int32_t result = ERR_DH_INPUT_SOURCE_PROXY_UNREGISTER_SIMULATION_LISTENER_FAIL;
682     bool ret = SendRequest(static_cast<uint32_t>(IDInputSourceInterfaceCode::UNREGISTER_SIMULATION_EVENT_LISTENER),
683         data, reply);
684     if (ret) {
685         result = reply.ReadInt32();
686     }
687     return result;
688 }
689 
RegisterSessionStateCb(sptr<ISessionStateCallback> callback)690 int32_t DistributedInputSourceProxy::RegisterSessionStateCb(sptr<ISessionStateCallback> callback)
691 {
692     if (callback == nullptr) {
693         DHLOGE("callback is nullptr");
694         return ERR_DH_INPUT_SRC_PROXY_CALLBACK_IS_NULL;
695     }
696     MessageParcel data;
697     if (!data.WriteInterfaceToken(GetDescriptor())) {
698         DHLOGE("RegisterSessionStateCb write token valid failed");
699         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
700     }
701 
702     if (!data.WriteRemoteObject(callback->AsObject())) {
703         DHLOGE("RegisterSessionStateCb write callback failed");
704         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
705     }
706 
707     MessageParcel reply;
708     int32_t result = ERR_DH_INPUT_SRC_STUB_REGISTER_SESSION_STATE_FAIL;
709     bool ret = SendRequest(static_cast<uint32_t>(IDInputSourceInterfaceCode::REGISTER_SESSION_STATE_CB),
710         data, reply);
711     if (ret) {
712         result = reply.ReadInt32();
713     }
714     return result;
715 }
716 
UnregisterSessionStateCb()717 int32_t DistributedInputSourceProxy::UnregisterSessionStateCb()
718 {
719     MessageParcel data;
720     if (!data.WriteInterfaceToken(GetDescriptor())) {
721         DHLOGE("UnregisterSessionStateCb write token valid failed");
722         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
723     }
724 
725     MessageParcel reply;
726     int32_t result = ERR_DH_INPUT_SRC_STUB_UNREGISTER_SESSION_STATE_FAIL;
727     bool ret = SendRequest(static_cast<uint32_t>(IDInputSourceInterfaceCode::UNREGISTER_SESSION_STATE_CB),
728         data, reply);
729     if (ret) {
730         result = reply.ReadInt32();
731     }
732     return result;
733 }
734 
SendRequest(const uint32_t code,MessageParcel & data,MessageParcel & reply)735 bool DistributedInputSourceProxy::SendRequest(const uint32_t code, MessageParcel &data, MessageParcel &reply)
736 {
737     sptr<IRemoteObject> remote = Remote();
738     if (remote == nullptr) {
739         DHLOGE("DistributedInputSourceProxy SendRequest remote is null.");
740         return false;
741     }
742     MessageOption option(MessageOption::TF_SYNC);
743     int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
744     if (result != DH_SUCCESS) {
745         DHLOGE("DistributedInputSourceProxy SendRequest error, result:%{public}d.", result);
746         return false;
747     }
748     return true;
749 }
750 } // namespace DistributedInput
751 } // namespace DistributedHardware
752 } // namespace OHOS
753