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_stub.h"
17 
18 #include "accesstoken_kit.h"
19 #include "constants_dinput.h"
20 #include "dinput_errcode.h"
21 #include "dinput_ipc_interface_code.h"
22 #include "dinput_log.h"
23 #include "ipc_skeleton.h"
24 
25 namespace OHOS {
26 namespace DistributedHardware {
27 namespace DistributedInput {
DistributedInputSourceStub()28 DistributedInputSourceStub::DistributedInputSourceStub()
29 {
30 }
31 
~DistributedInputSourceStub()32 DistributedInputSourceStub::~DistributedInputSourceStub()
33 {
34 }
35 
HasEnableDHPermission()36 bool DistributedInputSourceStub::HasEnableDHPermission()
37 {
38     Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
39     const std::string permissionName = "ohos.permission.ENABLE_DISTRIBUTED_HARDWARE";
40     int32_t result = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callerToken,
41         permissionName);
42     return (result == Security::AccessToken::PERMISSION_GRANTED);
43 }
44 
HasAccessDHPermission()45 bool DistributedInputSourceStub::HasAccessDHPermission()
46 {
47     Security::AccessToken::AccessTokenID callerToken = IPCSkeleton::GetCallingTokenID();
48     const std::string permissionName = "ohos.permission.ACCESS_DISTRIBUTED_HARDWARE";
49     int32_t result = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callerToken,
50         permissionName);
51     return (result == Security::AccessToken::PERMISSION_GRANTED);
52 }
53 
HandleInitDistributedHardware(MessageParcel & reply)54 int32_t DistributedInputSourceStub::HandleInitDistributedHardware(MessageParcel &reply)
55 {
56     if (!HasEnableDHPermission()) {
57         DHLOGE("The caller has no ENABLE_DISTRIBUTED_HARDWARE permission.");
58         return ERR_DH_INPUT_SRC_ENABLE_PERMISSION_CHECK_FAIL;
59     }
60     std::unique_lock<std::mutex> lock(operatorMutex_);
61     if (sourceManagerInitFlag_.load()) {
62         DHLOGE("DistributedInputSourceStub already init.");
63         return DH_SUCCESS;
64     }
65     int32_t ret = Init();
66     if (!reply.WriteInt32(ret)) {
67         DHLOGE("DistributedInputSourceStub Init write ret failed");
68         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
69     }
70     sourceManagerInitFlag_.store(true);
71     return DH_SUCCESS;
72 }
73 
HandleReleaseDistributedHardware(MessageParcel & reply)74 int32_t DistributedInputSourceStub::HandleReleaseDistributedHardware(MessageParcel &reply)
75 {
76     if (!HasEnableDHPermission()) {
77         DHLOGE("The caller has no ENABLE_DISTRIBUTED_HARDWARE permission.");
78         return ERR_DH_INPUT_SRC_ENABLE_PERMISSION_CHECK_FAIL;
79     }
80     std::unique_lock<std::mutex> lock(operatorMutex_);
81     if (!sourceManagerInitFlag_.load()) {
82         DHLOGE("DistributedInputSourceStub already Release.");
83         return DH_SUCCESS;
84     }
85     int32_t ret = Release();
86     if (!reply.WriteInt32(ret)) {
87         DHLOGE("DistributedInputSourceStub release write ret failed");
88         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
89     }
90     sourceManagerInitFlag_.store(false);
91     return DH_SUCCESS;
92 }
93 
HandleRegisterDistributedHardware(MessageParcel & data,MessageParcel & reply)94 int32_t DistributedInputSourceStub::HandleRegisterDistributedHardware(MessageParcel &data, MessageParcel &reply)
95 {
96     if (!HasEnableDHPermission()) {
97         DHLOGE("The caller has no ENABLE_DISTRIBUTED_HARDWARE permission.");
98         return ERR_DH_INPUT_SRC_ENABLE_PERMISSION_CHECK_FAIL;
99     }
100     std::string devId = data.ReadString();
101     std::string dhId = data.ReadString();
102     std::string params = data.ReadString();
103     sptr<IRegisterDInputCallback> callback = iface_cast<IRegisterDInputCallback>(data.ReadRemoteObject());
104     if (callback == nullptr) {
105         DHLOGE("HandleRegisterDistributedHardware failed, callback is nullptr.");
106         return ERR_DH_INPUT_POINTER_NULL;
107     }
108     int32_t ret = RegisterDistributedHardware(devId, dhId, params, callback);
109     if (!reply.WriteInt32(ret)) {
110         DHLOGE("HandleRegisterDistributedHardware write ret failed");
111         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
112     }
113     return DH_SUCCESS;
114 }
115 
HandleUnregisterDistributedHardware(MessageParcel & data,MessageParcel & reply)116 int32_t DistributedInputSourceStub::HandleUnregisterDistributedHardware(MessageParcel &data, MessageParcel &reply)
117 {
118     if (!HasEnableDHPermission()) {
119         DHLOGE("The caller has no ENABLE_DISTRIBUTED_HARDWARE permission.");
120         return ERR_DH_INPUT_SRC_ENABLE_PERMISSION_CHECK_FAIL;
121     }
122     std::string devId = data.ReadString();
123     std::string dhId = data.ReadString();
124     sptr<IUnregisterDInputCallback> callback = iface_cast<IUnregisterDInputCallback>(data.ReadRemoteObject());
125     if (callback == nullptr) {
126         DHLOGE("HandleUnregisterDistributedHardware failed, callback is nullptr.");
127         return ERR_DH_INPUT_POINTER_NULL;
128     }
129     int32_t ret = UnregisterDistributedHardware(devId, dhId, callback);
130     if (!reply.WriteInt32(ret)) {
131         DHLOGE("HandleUnregisterDistributedHardware write ret failed");
132         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
133     }
134     return DH_SUCCESS;
135 }
136 
HandlePrepareRemoteInput(MessageParcel & data,MessageParcel & reply)137 int32_t DistributedInputSourceStub::HandlePrepareRemoteInput(MessageParcel &data, MessageParcel &reply)
138 {
139     if (!HasAccessDHPermission()) {
140         DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
141         return ERR_DH_INPUT_SRC_ACCESS_PERMISSION_CHECK_FAIL;
142     }
143     std::string deviceId = data.ReadString();
144     sptr<IPrepareDInputCallback> callback = iface_cast<IPrepareDInputCallback>(data.ReadRemoteObject());
145     if (callback == nullptr) {
146         DHLOGE("HandlePrepareRemoteInput failed, callback is nullptr.");
147         return ERR_DH_INPUT_POINTER_NULL;
148     }
149     int32_t ret = PrepareRemoteInput(deviceId, callback);
150     if (!reply.WriteInt32(ret)) {
151         DHLOGE("HandlePrepareRemoteInput write ret failed");
152         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
153     }
154     return DH_SUCCESS;
155 }
156 
HandleUnprepareRemoteInput(MessageParcel & data,MessageParcel & reply)157 int32_t DistributedInputSourceStub::HandleUnprepareRemoteInput(MessageParcel &data, MessageParcel &reply)
158 {
159     if (!HasAccessDHPermission()) {
160         DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
161         return ERR_DH_INPUT_SRC_ACCESS_PERMISSION_CHECK_FAIL;
162     }
163     std::string deviceId = data.ReadString();
164     sptr<IUnprepareDInputCallback> callback = iface_cast<IUnprepareDInputCallback>(data.ReadRemoteObject());
165     if (callback == nullptr) {
166         DHLOGE("HandleUnprepareRemoteInput failed, callback is nullptr.");
167         return ERR_DH_INPUT_POINTER_NULL;
168     }
169     int32_t ret = UnprepareRemoteInput(deviceId, callback);
170     if (!reply.WriteInt32(ret)) {
171         DHLOGE("HandleUnprepareRemoteInput write ret failed");
172         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
173     }
174     return DH_SUCCESS;
175 }
176 
HandleStartRemoteInput(MessageParcel & data,MessageParcel & reply)177 int32_t DistributedInputSourceStub::HandleStartRemoteInput(MessageParcel &data, MessageParcel &reply)
178 {
179     if (!HasAccessDHPermission()) {
180         DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
181         return ERR_DH_INPUT_SRC_ACCESS_PERMISSION_CHECK_FAIL;
182     }
183     std::string deviceId = data.ReadString();
184     uint32_t inputTypes = data.ReadUint32();
185     sptr<IStartDInputCallback> callback = iface_cast<IStartDInputCallback>(data.ReadRemoteObject());
186     if (callback == nullptr) {
187         DHLOGE("HandleStartRemoteInput failed, callback is nullptr.");
188         return ERR_DH_INPUT_POINTER_NULL;
189     }
190     int32_t ret = StartRemoteInput(deviceId, inputTypes, callback);
191     if (!reply.WriteInt32(ret)) {
192         DHLOGE("HandleStartRemoteInput write ret failed");
193         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
194     }
195     return DH_SUCCESS;
196 }
197 
HandleStopRemoteInput(MessageParcel & data,MessageParcel & reply)198 int32_t DistributedInputSourceStub::HandleStopRemoteInput(MessageParcel &data, MessageParcel &reply)
199 {
200     if (!HasAccessDHPermission()) {
201         DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
202         return ERR_DH_INPUT_SRC_ACCESS_PERMISSION_CHECK_FAIL;
203     }
204     std::string deviceId = data.ReadString();
205     uint32_t inputTypes = data.ReadUint32();
206     sptr<IStopDInputCallback> callback = iface_cast<IStopDInputCallback>(data.ReadRemoteObject());
207     if (callback == nullptr) {
208         DHLOGE("HandleStopRemoteInput failed, callback is nullptr.");
209         return ERR_DH_INPUT_POINTER_NULL;
210     }
211     int32_t ret = StopRemoteInput(deviceId, inputTypes, callback);
212     if (!reply.WriteInt32(ret)) {
213         DHLOGE("HandleStopRemoteInput write ret failed");
214         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
215     }
216     return DH_SUCCESS;
217 }
218 
HandleStartRelayTypeRemoteInput(MessageParcel & data,MessageParcel & reply)219 int32_t DistributedInputSourceStub::HandleStartRelayTypeRemoteInput(MessageParcel &data, MessageParcel &reply)
220 {
221     if (!HasAccessDHPermission()) {
222         DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
223         return ERR_DH_INPUT_SRC_ACCESS_PERMISSION_CHECK_FAIL;
224     }
225     std::string srcId = data.ReadString();
226     std::string sinkId = data.ReadString();
227     uint32_t inputTypes = data.ReadUint32();
228     sptr<IStartDInputCallback> callback = iface_cast<IStartDInputCallback>(data.ReadRemoteObject());
229     if (callback == nullptr) {
230         DHLOGE("HandleStartRelayTypeRemoteInput failed, callback is nullptr.");
231         return ERR_DH_INPUT_POINTER_NULL;
232     }
233     int32_t ret = StartRemoteInput(srcId, sinkId, inputTypes, callback);
234     if (!reply.WriteInt32(ret)) {
235         DHLOGE("HandleStartRelayTypeRemoteInput write ret failed");
236         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
237     }
238     return DH_SUCCESS;
239 }
240 
HandleStopRelayTypeRemoteInput(MessageParcel & data,MessageParcel & reply)241 int32_t DistributedInputSourceStub::HandleStopRelayTypeRemoteInput(MessageParcel &data, MessageParcel &reply)
242 {
243     if (!HasAccessDHPermission()) {
244         DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
245         return ERR_DH_INPUT_SRC_ACCESS_PERMISSION_CHECK_FAIL;
246     }
247     std::string srcId = data.ReadString();
248     std::string sinkId = data.ReadString();
249     uint32_t inputTypes = data.ReadUint32();
250     sptr<IStopDInputCallback> callback = iface_cast<IStopDInputCallback>(data.ReadRemoteObject());
251     if (callback == nullptr) {
252         DHLOGE("HandleStopRelayTypeRemoteInput failed, callback is nullptr.");
253         return ERR_DH_INPUT_POINTER_NULL;
254     }
255     int32_t ret = StopRemoteInput(srcId, sinkId, inputTypes, callback);
256     if (!reply.WriteInt32(ret)) {
257         DHLOGE("HandleStopRelayTypeRemoteInput write ret failed");
258         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
259     }
260     return DH_SUCCESS;
261 }
262 
HandlePrepareRelayRemoteInput(MessageParcel & data,MessageParcel & reply)263 int32_t DistributedInputSourceStub::HandlePrepareRelayRemoteInput(MessageParcel &data, MessageParcel &reply)
264 {
265     if (!HasAccessDHPermission()) {
266         DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
267         return ERR_DH_INPUT_SRC_ACCESS_PERMISSION_CHECK_FAIL;
268     }
269     std::string srcId = data.ReadString();
270     std::string sinkId = data.ReadString();
271     sptr<IPrepareDInputCallback> callback = iface_cast<IPrepareDInputCallback>(data.ReadRemoteObject());
272     if (callback == nullptr) {
273         DHLOGE("HandlePrepareRelayRemoteInput failed, callback is nullptr.");
274         return ERR_DH_INPUT_POINTER_NULL;
275     }
276     int32_t ret = PrepareRemoteInput(srcId, sinkId, callback);
277     if (!reply.WriteInt32(ret)) {
278         DHLOGE("HandlePrepareRelayRemoteInput write ret failed");
279         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
280     }
281     return DH_SUCCESS;
282 }
283 
HandleUnprepareRelayRemoteInput(MessageParcel & data,MessageParcel & reply)284 int32_t DistributedInputSourceStub::HandleUnprepareRelayRemoteInput(MessageParcel &data, MessageParcel &reply)
285 {
286     if (!HasAccessDHPermission()) {
287         DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
288         return ERR_DH_INPUT_SRC_ACCESS_PERMISSION_CHECK_FAIL;
289     }
290     std::string srcId = data.ReadString();
291     std::string sinkId = data.ReadString();
292     sptr<IUnprepareDInputCallback> callback = iface_cast<IUnprepareDInputCallback>(data.ReadRemoteObject());
293     if (callback == nullptr) {
294         DHLOGE("HandleUnprepareRelayRemoteInput failed, callback is nullptr.");
295         return ERR_DH_INPUT_POINTER_NULL;
296     }
297     int32_t ret = UnprepareRemoteInput(srcId, sinkId, callback);
298     if (!reply.WriteInt32(ret)) {
299         DHLOGE("HandleUnprepareRelayRemoteInput write ret failed");
300         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
301     }
302     return DH_SUCCESS;
303 }
304 
HandleStartDhidRemoteInput(MessageParcel & data,MessageParcel & reply)305 int32_t DistributedInputSourceStub::HandleStartDhidRemoteInput(MessageParcel &data, MessageParcel &reply)
306 {
307     if (!HasAccessDHPermission()) {
308         DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
309         return ERR_DH_INPUT_SRC_ACCESS_PERMISSION_CHECK_FAIL;
310     }
311     std::string sinkId = data.ReadString();
312 
313     std::vector<std::string> tempVector;
314     uint32_t vecSize = data.ReadUint32();
315     if (vecSize > IPC_VECTOR_MAX_SIZE) {
316         DHLOGE("HandleStartDhidRemoteInput vecSize too large");
317         return ERR_DH_INPUT_IPC_READ_VALID_FAIL;
318     }
319 
320     for (uint32_t i = 0; i < vecSize; i++) {
321         std::string dhid = data.ReadString();
322         if (dhid.empty()) {
323             DHLOGE("HandleStartDhidRemoteInput dhid is empty");
324             continue;
325         }
326         tempVector.push_back(dhid);
327     }
328 
329     sptr<IStartStopDInputsCallback> callback = iface_cast<IStartStopDInputsCallback>(data.ReadRemoteObject());
330     if (callback == nullptr) {
331         DHLOGE("HandleStartDhidRemoteInput failed, callback is nullptr.");
332         return ERR_DH_INPUT_POINTER_NULL;
333     }
334     int32_t ret = StartRemoteInput(sinkId, tempVector, callback);
335     if (!reply.WriteInt32(ret)) {
336         DHLOGE("HandleStartDhidRemoteInput write ret failed");
337         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
338     }
339     return DH_SUCCESS;
340 }
341 
HandleStopDhidRemoteInput(MessageParcel & data,MessageParcel & reply)342 int32_t DistributedInputSourceStub::HandleStopDhidRemoteInput(MessageParcel &data, MessageParcel &reply)
343 {
344     if (!HasAccessDHPermission()) {
345         DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
346         return ERR_DH_INPUT_SRC_ACCESS_PERMISSION_CHECK_FAIL;
347     }
348     std::string sinkId = data.ReadString();
349 
350     std::vector<std::string> tempVector;
351     uint32_t vecSize = data.ReadUint32();
352     if (vecSize > IPC_VECTOR_MAX_SIZE) {
353         DHLOGE("HandleStopDhidRemoteInput vecSize too large");
354         return ERR_DH_INPUT_IPC_READ_VALID_FAIL;
355     }
356 
357     for (uint32_t i = 0; i < vecSize; i++) {
358         std::string dhid = data.ReadString();
359         if (dhid.empty()) {
360             DHLOGE("HandleStopDhidRemoteInput dhid is empty");
361             continue;
362         }
363         tempVector.push_back(dhid);
364     }
365 
366     sptr<IStartStopDInputsCallback> callback = iface_cast<IStartStopDInputsCallback>(data.ReadRemoteObject());
367     if (callback == nullptr) {
368         DHLOGE("HandleStopDhidRemoteInput failed, callback is nullptr.");
369         return ERR_DH_INPUT_POINTER_NULL;
370     }
371     int32_t ret = StopRemoteInput(sinkId, tempVector, callback);
372     if (!reply.WriteInt32(ret)) {
373         DHLOGE("HandleStopDhidRemoteInput write ret failed");
374         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
375     }
376     return DH_SUCCESS;
377 }
378 
HandleStartRelayDhidRemoteInput(MessageParcel & data,MessageParcel & reply)379 int32_t DistributedInputSourceStub::HandleStartRelayDhidRemoteInput(MessageParcel &data, MessageParcel &reply)
380 {
381     if (!HasAccessDHPermission()) {
382         DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
383         return ERR_DH_INPUT_SRC_ACCESS_PERMISSION_CHECK_FAIL;
384     }
385     std::string srcId = data.ReadString();
386     std::string sinkId = data.ReadString();
387 
388     std::vector<std::string> tempVector;
389     uint32_t vecSize = data.ReadUint32();
390     if (vecSize > IPC_VECTOR_MAX_SIZE) {
391         DHLOGE("HandleStartRelayDhidRemoteInput vecSize too large");
392         return ERR_DH_INPUT_IPC_READ_VALID_FAIL;
393     }
394 
395     for (uint32_t i = 0; i < vecSize; i++) {
396         std::string dhid = data.ReadString();
397         if (dhid.empty()) {
398             DHLOGE("HandleStartRelayDhidRemoteInput dhid is empty");
399             continue;
400         }
401         tempVector.push_back(dhid);
402     }
403 
404     sptr<IStartStopDInputsCallback> callback = iface_cast<IStartStopDInputsCallback>(data.ReadRemoteObject());
405     if (callback == nullptr) {
406         DHLOGE("HandleStartRelayDhidRemoteInput failed, callback is nullptr.");
407         return ERR_DH_INPUT_POINTER_NULL;
408     }
409     int32_t ret = StartRemoteInput(srcId, sinkId, tempVector, callback);
410     if (!reply.WriteInt32(ret)) {
411         DHLOGE("HandleStartRelayDhidRemoteInput write ret failed");
412         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
413     }
414     return DH_SUCCESS;
415 }
416 
HandleStopRelayDhidRemoteInput(MessageParcel & data,MessageParcel & reply)417 int32_t DistributedInputSourceStub::HandleStopRelayDhidRemoteInput(MessageParcel &data, MessageParcel &reply)
418 {
419     if (!HasAccessDHPermission()) {
420         DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
421         return ERR_DH_INPUT_SRC_ACCESS_PERMISSION_CHECK_FAIL;
422     }
423     std::string srcId = data.ReadString();
424     std::string sinkId = data.ReadString();
425 
426     std::vector<std::string> tempVector;
427     uint32_t vecSize = data.ReadUint32();
428     if (vecSize > IPC_VECTOR_MAX_SIZE) {
429         DHLOGE("HandleStopRelayDhidRemoteInput vecSize too large");
430         return ERR_DH_INPUT_IPC_READ_VALID_FAIL;
431     }
432 
433     for (uint32_t i = 0; i < vecSize; i++) {
434         std::string dhid = data.ReadString();
435         if (dhid.empty()) {
436             DHLOGE("HandleStopRelayDhidRemoteInput dhid is empty");
437             continue;
438         }
439         tempVector.push_back(dhid);
440     }
441 
442     sptr<IStartStopDInputsCallback> callback = iface_cast<IStartStopDInputsCallback>(data.ReadRemoteObject());
443     if (callback == nullptr) {
444         DHLOGE("HandleStopRelayDhidRemoteInput failed, callback is nullptr.");
445         return ERR_DH_INPUT_POINTER_NULL;
446     }
447     int32_t ret = StopRemoteInput(srcId, sinkId, tempVector, callback);
448     if (!reply.WriteInt32(ret)) {
449         DHLOGE("HandleStopRelayDhidRemoteInput write ret failed");
450         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
451     }
452     return DH_SUCCESS;
453 }
454 
HandleRegisterAddWhiteListCallback(MessageParcel & data,MessageParcel & reply)455 int32_t DistributedInputSourceStub::HandleRegisterAddWhiteListCallback(MessageParcel &data, MessageParcel &reply)
456 {
457     sptr<IAddWhiteListInfosCallback> callback = iface_cast<IAddWhiteListInfosCallback>(data.ReadRemoteObject());
458     if (callback == nullptr) {
459         DHLOGE("HandleRegisterAddWhiteListCallback failed, callback is nullptr.");
460         return ERR_DH_INPUT_POINTER_NULL;
461     }
462     int32_t ret = RegisterAddWhiteListCallback(callback);
463     if (!reply.WriteInt32(ret)) {
464         DHLOGE("HandleRegisterAddWhiteListCallback write ret failed");
465         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
466     }
467     return DH_SUCCESS;
468 }
469 
HandleRegisterDelWhiteListCallback(MessageParcel & data,MessageParcel & reply)470 int32_t DistributedInputSourceStub::HandleRegisterDelWhiteListCallback(MessageParcel &data, MessageParcel &reply)
471 {
472     sptr<IDelWhiteListInfosCallback> callback = iface_cast<IDelWhiteListInfosCallback>(data.ReadRemoteObject());
473     if (callback == nullptr) {
474         DHLOGE("HandleRegisterDelWhiteListCallback failed, callback is nullptr.");
475         return ERR_DH_INPUT_POINTER_NULL;
476     }
477     int32_t ret = RegisterDelWhiteListCallback(callback);
478     if (!reply.WriteInt32(ret)) {
479         DHLOGE("HandleRegisterDelWhiteListCallback write ret failed");
480         return ERR_DH_INPUT_IPC_WRITE_VALID_FAIL;
481     }
482     return DH_SUCCESS;
483 }
484 
HandleRegisterSimulationEventListener(MessageParcel & data,MessageParcel & reply)485 int32_t DistributedInputSourceStub::HandleRegisterSimulationEventListener(MessageParcel &data, MessageParcel &reply)
486 {
487     sptr<ISimulationEventListener> callback = iface_cast<ISimulationEventListener>(data.ReadRemoteObject());
488     if (callback == nullptr) {
489         DHLOGE("HandleRegisterSimulationEventListener failed, callback is nullptr.");
490         return ERR_DH_INPUT_POINTER_NULL;
491     }
492     int32_t ret = RegisterSimulationEventListener(callback);
493     if (!reply.WriteInt32(ret)) {
494         DHLOGE("HandleRegisterSimulationEventListener write ret failed, ret = %{public}d", ret);
495         return ERR_DH_INPUT_SOURCE_STUB_REGISTER_SIMULATION_EVENT_LISTENER_FAIL;
496     }
497 
498     return DH_SUCCESS;
499 }
500 
HandleUnregisterSimulationEventListener(MessageParcel & data,MessageParcel & reply)501 int32_t DistributedInputSourceStub::HandleUnregisterSimulationEventListener(MessageParcel &data, MessageParcel &reply)
502 {
503     sptr<ISimulationEventListener> callback = iface_cast<ISimulationEventListener>(data.ReadRemoteObject());
504     if (callback == nullptr) {
505         DHLOGE("HandleUnregisterSimulationEventListener failed, callback is nullptr.");
506         return ERR_DH_INPUT_POINTER_NULL;
507     }
508     int32_t ret = UnregisterSimulationEventListener(callback);
509     if (!reply.WriteInt32(ret)) {
510         DHLOGE("HandleUnregisterSimulationEventListener write ret failed, ret = %{public}d", ret);
511         return ERR_DH_INPUT_SOURCE_STUB_UNREGISTER_SIMULATION_EVENT_LISTENER_FAIL;
512     }
513 
514     return DH_SUCCESS;
515 }
516 
HandleRegisterSessionStateCb(MessageParcel & data,MessageParcel & reply)517 int32_t DistributedInputSourceStub::HandleRegisterSessionStateCb(MessageParcel &data, MessageParcel &reply)
518 {
519     if (!HasAccessDHPermission()) {
520         DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
521         return ERR_DH_INPUT_SRC_ACCESS_PERMISSION_CHECK_FAIL;
522     }
523     sptr<ISessionStateCallback> callback = iface_cast<ISessionStateCallback>(data.ReadRemoteObject());
524     if (callback == nullptr) {
525         DHLOGE("HandleRegisterSessionStateCb failed, callback is nullptr.");
526         return ERR_DH_INPUT_POINTER_NULL;
527     }
528     int32_t ret = RegisterSessionStateCb(callback);
529     if (!reply.WriteInt32(ret)) {
530         DHLOGE("HandleRegisterSessionStateCb write ret failed, ret = %{public}d", ret);
531         return ERR_DH_INPUT_SRC_STUB_REGISTER_SESSION_STATE_FAIL;
532     }
533 
534     return DH_SUCCESS;
535 }
536 
HandleUnregisterSessionStateCb(MessageParcel & data,MessageParcel & reply)537 int32_t DistributedInputSourceStub::HandleUnregisterSessionStateCb(MessageParcel &data, MessageParcel &reply)
538 {
539     if (!HasAccessDHPermission()) {
540         DHLOGE("The caller has no ACCESS_DISTRIBUTED_HARDWARE permission.");
541         return ERR_DH_INPUT_SRC_ACCESS_PERMISSION_CHECK_FAIL;
542     }
543     int32_t ret = UnregisterSessionStateCb();
544     if (!reply.WriteInt32(ret)) {
545         DHLOGE("HandleUnregisterSessionStateCb write ret failed, ret = %{public}d", ret);
546         return ERR_DH_INPUT_SRC_STUB_UNREGISTER_SESSION_STATE_FAIL;
547     }
548 
549     return DH_SUCCESS;
550 }
551 
HandleRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)552 int32_t DistributedInputSourceStub::HandleRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
553     MessageOption &option)
554 {
555     switch (code) {
556         case static_cast<uint32_t>(IDInputSourceInterfaceCode::START_RELAY_DHID_REMOTE_INPUT):
557             return HandleStartRelayDhidRemoteInput(data, reply);
558         case static_cast<uint32_t>(IDInputSourceInterfaceCode::STOP_RELAY_DHID_REMOTE_INPUT):
559             return HandleStopRelayDhidRemoteInput(data, reply);
560         case static_cast<uint32_t>(IDInputSourceInterfaceCode::REGISTER_ADD_WHITE_LIST_CB_REMOTE_INPUT):
561             return HandleRegisterAddWhiteListCallback(data, reply);
562         case static_cast<uint32_t>(IDInputSourceInterfaceCode::REGISTER_DEL_WHITE_LIST_CB_REMOTE_INPUT):
563             return HandleRegisterDelWhiteListCallback(data, reply);
564         case static_cast<uint32_t>(IDInputSourceInterfaceCode::REGISTER_SIMULATION_EVENT_LISTENER):
565             return HandleRegisterSimulationEventListener(data, reply);
566         case static_cast<uint32_t>(IDInputSourceInterfaceCode::UNREGISTER_SIMULATION_EVENT_LISTENER):
567             return HandleUnregisterSimulationEventListener(data, reply);
568         case static_cast<uint32_t>(IDInputSourceInterfaceCode::REGISTER_SESSION_STATE_CB):
569             return HandleRegisterSessionStateCb(data, reply);
570         case static_cast<uint32_t>(IDInputSourceInterfaceCode::UNREGISTER_SESSION_STATE_CB):
571             return HandleUnregisterSessionStateCb(data, reply);
572         default:
573             DHLOGE("invalid request code is %{public}u.", code);
574             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
575     }
576 }
577 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)578 int32_t DistributedInputSourceStub::OnRemoteRequest(
579     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
580 {
581     DHLOGI("OnRemoteRequest code: %{public}u.", code);
582     if (data.ReadInterfaceToken() != GetDescriptor()) {
583         DHLOGE("DistributedInputSourceStub read token valid failed");
584         return ERR_DH_INPUT_IPC_WRITE_TOKEN_VALID_FAIL;
585     }
586     if (code == static_cast<uint32_t>(IDInputSourceInterfaceCode::REGISTER_REMOTE_INPUT)) {
587         DHLOGI("Receive Register DInput cmd");
588     }
589 
590     if (code == static_cast<uint32_t>(IDInputSourceInterfaceCode::UNREGISTER_REMOTE_INPUT)) {
591         DHLOGI("Receive UnRegister DInput cmd");
592     }
593     switch (code) {
594         case static_cast<uint32_t>(IDInputSourceInterfaceCode::INIT):
595             return HandleInitDistributedHardware(reply);
596         case static_cast<uint32_t>(IDInputSourceInterfaceCode::RELEASE):
597             return HandleReleaseDistributedHardware(reply);
598         case static_cast<uint32_t>(IDInputSourceInterfaceCode::REGISTER_REMOTE_INPUT):
599             return HandleRegisterDistributedHardware(data, reply);
600         case static_cast<uint32_t>(IDInputSourceInterfaceCode::UNREGISTER_REMOTE_INPUT):
601             return HandleUnregisterDistributedHardware(data, reply);
602         case static_cast<uint32_t>(IDInputSourceInterfaceCode::PREPARE_REMOTE_INPUT):
603             return HandlePrepareRemoteInput(data, reply);
604         case static_cast<uint32_t>(IDInputSourceInterfaceCode::UNPREPARE_REMOTE_INPUT):
605             return HandleUnprepareRemoteInput(data, reply);
606         case static_cast<uint32_t>(IDInputSourceInterfaceCode::START_REMOTE_INPUT):
607             return HandleStartRemoteInput(data, reply);
608         case static_cast<uint32_t>(IDInputSourceInterfaceCode::STOP_REMOTE_INPUT):
609             return HandleStopRemoteInput(data, reply);
610         case static_cast<uint32_t>(IDInputSourceInterfaceCode::START_RELAY_TYPE_REMOTE_INPUT):
611             return HandleStartRelayTypeRemoteInput(data, reply);
612         case static_cast<uint32_t>(IDInputSourceInterfaceCode::STOP_RELAY_TYPE_REMOTE_INPUT):
613             return HandleStopRelayTypeRemoteInput(data, reply);
614         case static_cast<uint32_t>(IDInputSourceInterfaceCode::PREPARE_RELAY_REMOTE_INPUT):
615             return HandlePrepareRelayRemoteInput(data, reply);
616         case static_cast<uint32_t>(IDInputSourceInterfaceCode::UNPREPARE_RELAY_REMOTE_INPUT):
617             return HandleUnprepareRelayRemoteInput(data, reply);
618         case static_cast<uint32_t>(IDInputSourceInterfaceCode::START_DHID_REMOTE_INPUT):
619             return HandleStartDhidRemoteInput(data, reply);
620         case static_cast<uint32_t>(IDInputSourceInterfaceCode::STOP_DHID_REMOTE_INPUT):
621             return HandleStopDhidRemoteInput(data, reply);
622         default:
623             return HandleRemoteRequest(code, data, reply, option);
624     }
625 }
626 } // namespace DistributedInput
627 } // namespace DistributedHardware
628 } // namespace OHOS
629