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