1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "input_interfaces_impl.h"
17 #include <hdf_base.h>
18 #include <iproxy_broker.h>
19 #include <mutex>
20 #include <securec.h>
21 #include "input_uhdf_log.h"
22 #include "input_manager.h"
23 #include "input_type.h"
24 #include "osal_mem.h"
25
26 #define HDF_LOG_TAG InputInterfacesImpl
27
28 namespace OHOS {
29 namespace HDI {
30 namespace Input {
31 namespace V1_0 {
32 namespace {
33 constexpr uint32_t MAX_DEVICES = 32;
34 InputEventCb eventCb;
35 InputHostCb hostCb;
36 sptr<IInputCallback> g_inputEventCallback = nullptr;
37 sptr<IInputCallback> g_hotplugEventCallback = nullptr;
38 std::mutex g_mutex;
39 }
40
InputInterfacesImplGetInstance(void)41 extern "C" IInputInterfaces *InputInterfacesImplGetInstance(void)
42 {
43 using OHOS::HDI::Input::V1_0::InputInterfacesImpl;
44 InputInterfacesImpl *service = new (std::nothrow) InputInterfacesImpl();
45 if (service == nullptr) {
46 return nullptr;
47 }
48
49 service->Init();
50 return service;
51 }
52
TransferDevInfo(InputDeviceInfo & halDevInfo)53 static struct DeviceInfo TransferDevInfo(InputDeviceInfo &halDevInfo)
54 {
55 DeviceInfo hdiDevInfo;
56 hdiDevInfo.devIndex = halDevInfo.devIndex;
57 hdiDevInfo.devType = halDevInfo.devType;
58 hdiDevInfo.chipInfo = halDevInfo.chipInfo;
59 hdiDevInfo.vendorName = halDevInfo.vendorName;
60 hdiDevInfo.chipName = halDevInfo.chipName;
61 hdiDevInfo.attrSet.devName = halDevInfo.attrSet.devName;
62 hdiDevInfo.attrSet.id.busType = halDevInfo.attrSet.id.busType;
63 hdiDevInfo.attrSet.id.vendor = halDevInfo.attrSet.id.vendor;
64 hdiDevInfo.attrSet.id.product = halDevInfo.attrSet.id.product;
65 hdiDevInfo.attrSet.id.version = halDevInfo.attrSet.id.version;
66 hdiDevInfo.attrSet.axisInfo.resize(ABS_CNT);
67 for (int32_t i = 0; i < ABS_CNT; i++) {
68 hdiDevInfo.attrSet.axisInfo[i].axis = halDevInfo.attrSet.axisInfo[i].axis;
69 hdiDevInfo.attrSet.axisInfo[i].min = halDevInfo.attrSet.axisInfo[i].min;
70 hdiDevInfo.attrSet.axisInfo[i].max = halDevInfo.attrSet.axisInfo[i].max;
71 hdiDevInfo.attrSet.axisInfo[i].fuzz = halDevInfo.attrSet.axisInfo[i].fuzz;
72 hdiDevInfo.attrSet.axisInfo[i].flat = halDevInfo.attrSet.axisInfo[i].flat;
73 hdiDevInfo.attrSet.axisInfo[i].range = halDevInfo.attrSet.axisInfo[i].range;
74 }
75 hdiDevInfo.abilitySet.devProp.assign(halDevInfo.abilitySet.devProp,
76 halDevInfo.abilitySet.devProp + BITS_TO_UINT64(INPUT_PROP_CNT));
77 hdiDevInfo.abilitySet.eventType.assign(halDevInfo.abilitySet.eventType,
78 halDevInfo.abilitySet.eventType + BITS_TO_UINT64(EV_CNT));
79 hdiDevInfo.abilitySet.absCode.assign(halDevInfo.abilitySet.absCode,
80 halDevInfo.abilitySet.absCode + BITS_TO_UINT64(ABS_CNT));
81 hdiDevInfo.abilitySet.relCode.assign(halDevInfo.abilitySet.relCode,
82 halDevInfo.abilitySet.relCode + BITS_TO_UINT64(REL_CNT));
83 hdiDevInfo.abilitySet.keyCode.assign(halDevInfo.abilitySet.keyCode,
84 halDevInfo.abilitySet.keyCode + BITS_TO_UINT64(KEY_CNT));
85 hdiDevInfo.abilitySet.ledCode.assign(halDevInfo.abilitySet.ledCode,
86 halDevInfo.abilitySet.ledCode + BITS_TO_UINT64(LED_CNT));
87 hdiDevInfo.abilitySet.miscCode.assign(halDevInfo.abilitySet.miscCode,
88 halDevInfo.abilitySet.miscCode + BITS_TO_UINT64(MSC_CNT));
89 hdiDevInfo.abilitySet.soundCode.assign(halDevInfo.abilitySet.soundCode,
90 halDevInfo.abilitySet.soundCode + BITS_TO_UINT64(SND_CNT));
91 hdiDevInfo.abilitySet.forceCode.assign(halDevInfo.abilitySet.forceCode,
92 halDevInfo.abilitySet.forceCode + BITS_TO_UINT64(HDF_FF_CNT));
93 hdiDevInfo.abilitySet.switchCode.assign(halDevInfo.abilitySet.switchCode,
94 halDevInfo.abilitySet.switchCode + BITS_TO_UINT64(SW_CNT));
95 hdiDevInfo.abilitySet.keyType.assign(halDevInfo.abilitySet.keyType,
96 halDevInfo.abilitySet.keyType + BITS_TO_UINT64(KEY_CNT));
97 hdiDevInfo.abilitySet.ledType.assign(halDevInfo.abilitySet.ledType,
98 halDevInfo.abilitySet.ledType + BITS_TO_UINT64(LED_CNT));
99 hdiDevInfo.abilitySet.soundType.assign(halDevInfo.abilitySet.soundType,
100 halDevInfo.abilitySet.soundType + BITS_TO_UINT64(SND_CNT));
101 hdiDevInfo.abilitySet.switchType.assign(halDevInfo.abilitySet.switchType,
102 halDevInfo.abilitySet.switchType + BITS_TO_UINT64(SW_CNT));
103 return hdiDevInfo;
104 }
105
InputEventDataCallback(const InputEventPackage ** pkgs,uint32_t count,uint32_t devIndex)106 void InputEventDataCallback(const InputEventPackage **pkgs, uint32_t count, uint32_t devIndex)
107 {
108 if (pkgs == nullptr || *pkgs == nullptr) {
109 HDF_LOGE("%{public}s failed, pkgs is nullptr", __func__);
110 return;
111 }
112
113 std::vector<EventPackage> tmp;
114 for (uint32_t i = 0; i < count; i++) {
115 if (pkgs[i] == nullptr) {
116 HDF_LOGE("%{public}s failed, pkgs[%{public}u] is nullptr", __func__, i);
117 return;
118 }
119 EventPackage InputEvents;
120 InputEvents.type = pkgs[i]->type;
121 InputEvents.code = pkgs[i]->code;
122 InputEvents.value = pkgs[i]->value;
123 InputEvents.timestamp = pkgs[i]->timestamp;
124 tmp.push_back(InputEvents);
125 }
126
127 if (g_inputEventCallback == nullptr) {
128 HDF_LOGE("%{public}s: g_inputEventCallback is nullptr", __func__);
129 return;
130 }
131 g_inputEventCallback->EventPkgCallback(tmp, devIndex);
132 }
133
HotplugEventDataCallback(const InputHotPlugEvent * event)134 void HotplugEventDataCallback(const InputHotPlugEvent *event)
135 {
136 if (event == nullptr) {
137 HDF_LOGE("%{public}s failed, pkgs is nullptr", __func__);
138 return;
139 }
140
141 HotPlugEvent HotPlugEvent;
142 HotPlugEvent.devIndex = event->devIndex;
143 HotPlugEvent.devType = event->devType;
144 HotPlugEvent.status = event->status;
145
146 if (g_hotplugEventCallback == nullptr) {
147 HDF_LOGE("%{public}s: g_hotplugEventCallback is nullptr", __func__);
148 return;
149 }
150 g_hotplugEventCallback->HotPlugCallback(HotPlugEvent);
151 }
152
Init()153 void InputInterfacesImpl::Init()
154 {
155 int32_t ret = GetInputInterface(&inputInterface_);
156 if (inputInterface_ == nullptr || ret != HDF_SUCCESS) {
157 HDF_LOGE("%{public}s: get input Module instance failed", __func__);
158 }
159
160 eventCb.EventPkgCallback = InputEventDataCallback;
161 hostCb.HotPlugCallback = HotplugEventDataCallback;
162 }
163
ScanInputDevice(std::vector<DevDesc> & staArr)164 int32_t InputInterfacesImpl::ScanInputDevice(std::vector<DevDesc> &staArr)
165 {
166 if (inputInterface_ == nullptr || inputInterface_->iInputManager == nullptr ||
167 inputInterface_->iInputManager->ScanInputDevice == nullptr) {
168 HDF_LOGE("%{public}s: get input device Module instance failed", __func__);
169 return HDF_FAILURE;
170 }
171
172 InputDevDesc staArrHdf[MAX_DEVICES];
173 int32_t ret = memset_s(staArrHdf, MAX_DEVICES * sizeof(InputDevDesc), 0, MAX_DEVICES * sizeof(InputDevDesc));
174 if (ret != HDF_SUCCESS) {
175 HDF_LOGE("%{public}s: memset failed", __func__);
176 return HDF_FAILURE;
177 }
178
179 ret = inputInterface_->iInputManager->ScanInputDevice(staArrHdf, MAX_DEVICES);
180 if (ret != INPUT_SUCCESS) {
181 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
182 return HDF_FAILURE;
183 }
184
185 for (uint32_t i = 0; i < MAX_DEVICES; i++) {
186 DevDesc StaArr;
187 StaArr.devIndex = staArrHdf[i].devIndex;
188 StaArr.devType = staArrHdf[i].devType;
189 staArr.push_back(StaArr);
190 }
191
192 return ret;
193 }
194
OpenInputDevice(uint32_t devIndex)195 int32_t InputInterfacesImpl::OpenInputDevice(uint32_t devIndex)
196 {
197 if (inputInterface_ == nullptr || inputInterface_->iInputManager == nullptr ||
198 inputInterface_->iInputManager->OpenInputDevice == nullptr) {
199 HDF_LOGE("%{public}s: get input device Module instance failed", __func__);
200 return HDF_FAILURE;
201 }
202
203 int32_t ret = inputInterface_->iInputManager->OpenInputDevice(devIndex);
204 if (ret != INPUT_SUCCESS) {
205 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
206 }
207
208 return ret;
209 }
210
CloseInputDevice(uint32_t devIndex)211 int32_t InputInterfacesImpl::CloseInputDevice(uint32_t devIndex)
212 {
213 if (inputInterface_ == nullptr || inputInterface_->iInputManager == nullptr ||
214 inputInterface_->iInputManager->CloseInputDevice == nullptr) {
215 HDF_LOGE("%{public}s: get input device Module instance failed", __func__);
216 return HDF_FAILURE;
217 }
218
219 int32_t ret = inputInterface_->iInputManager->CloseInputDevice(devIndex);
220 if (ret != INPUT_SUCCESS) {
221 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
222 }
223
224 return ret;
225 }
226
GetInputDevice(uint32_t devIndex,DeviceInfo & devInfo)227 int32_t InputInterfacesImpl::GetInputDevice(uint32_t devIndex, DeviceInfo &devInfo)
228 {
229 if (inputInterface_ == nullptr || inputInterface_->iInputManager == nullptr ||
230 inputInterface_->iInputManager->GetInputDevice == nullptr) {
231 HDF_LOGE("%{public}s: get input device Module instance failed", __func__);
232 return HDF_FAILURE;
233 }
234
235 InputDeviceInfo *deviceInfo = nullptr;
236
237 int32_t ret = inputInterface_->iInputManager->GetInputDevice(devIndex, &deviceInfo);
238 if (ret != INPUT_SUCCESS) {
239 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
240 return ret;
241 }
242
243 if (deviceInfo == nullptr) {
244 HDF_LOGE("%{public}s get deviceInfo failed, info is null", __func__);
245 return HDF_FAILURE;
246 }
247 devInfo = TransferDevInfo(*deviceInfo);
248 #ifdef DRIVERS_PERIPHERAL_INPUT_FEATURE_UDRIVER
249 OsalMemFree(deviceInfo);
250 #endif /* DRIVERS_PERIPHERAL_INPUT_FEATURE_UDRIVER */
251 return ret;
252 }
253
GetInputDeviceList(uint32_t & devNum,std::vector<DeviceInfo> & devList,uint32_t size)254 int32_t InputInterfacesImpl::GetInputDeviceList(uint32_t &devNum, std::vector<DeviceInfo> &devList, uint32_t size)
255 {
256 if (inputInterface_ == nullptr || inputInterface_->iInputManager == nullptr ||
257 inputInterface_->iInputManager->GetInputDeviceList == nullptr) {
258 HDF_LOGE("%{public}s: get input device Module instance failed", __func__);
259 return HDF_FAILURE;
260 }
261
262 InputDeviceInfo *deviceList = nullptr;
263 InputDeviceInfo *tmp = nullptr;
264 DeviceInfo hdfDevInfo;
265
266 int32_t ret = inputInterface_->iInputManager->GetInputDeviceList(&devNum, &deviceList, size);
267 if (ret != INPUT_SUCCESS) {
268 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
269 return HDF_FAILURE;
270 }
271
272 tmp = deviceList;
273 if (tmp == nullptr) {
274 HDF_LOGE("%{public}s deviceList is null", __func__);
275 return HDF_FAILURE;
276 }
277
278 devList.reserve(devNum);
279 for (uint32_t i = 0; i < devNum; i++) {
280 hdfDevInfo = TransferDevInfo(*tmp);
281 devList.push_back(hdfDevInfo);
282 tmp++;
283 }
284 #ifdef DRIVERS_PERIPHERAL_INPUT_FEATURE_UDRIVER
285 OsalMemFree(deviceList);
286 #endif /* DRIVERS_PERIPHERAL_INPUT_FEATURE_UDRIVER */
287 return ret;
288 }
289
SetPowerStatus(uint32_t devIndex,uint32_t status)290 int32_t InputInterfacesImpl::SetPowerStatus(uint32_t devIndex, uint32_t status)
291 {
292 if (inputInterface_ == nullptr || inputInterface_->iInputController == nullptr ||
293 inputInterface_->iInputController->SetPowerStatus == nullptr) {
294 HDF_LOGE("%{public}s: get input device Module instance failed", __func__);
295 return HDF_FAILURE;
296 }
297
298 int32_t ret = inputInterface_->iInputController->SetPowerStatus(devIndex, status);
299 if (ret != INPUT_SUCCESS) {
300 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
301 }
302
303 return ret;
304 }
305
GetPowerStatus(uint32_t devIndex,uint32_t & status)306 int32_t InputInterfacesImpl::GetPowerStatus(uint32_t devIndex, uint32_t &status)
307 {
308 if (inputInterface_ == nullptr || inputInterface_->iInputController == nullptr ||
309 inputInterface_->iInputController->GetPowerStatus == nullptr) {
310 HDF_LOGE("%{public}s: get input device Module instance failed", __func__);
311 return HDF_FAILURE;
312 }
313
314 int32_t ret = inputInterface_->iInputController->GetPowerStatus(devIndex, &status);
315 if (ret != INPUT_SUCCESS) {
316 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
317 }
318
319 return ret;
320 }
321
GetDeviceType(uint32_t devIndex,uint32_t & deviceType)322 int32_t InputInterfacesImpl::GetDeviceType(uint32_t devIndex, uint32_t &deviceType)
323 {
324 if (inputInterface_ == nullptr || inputInterface_->iInputController == nullptr ||
325 inputInterface_->iInputController->GetDeviceType == nullptr) {
326 HDF_LOGE("%{public}s: get input device Module instance failed", __func__);
327 return HDF_FAILURE;
328 }
329
330 int32_t ret = inputInterface_->iInputController->GetDeviceType(devIndex, &deviceType);
331 if (ret != INPUT_SUCCESS) {
332 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
333 }
334
335 return ret;
336 }
337
GetChipInfo(uint32_t devIndex,std::string & chipInfo)338 int32_t InputInterfacesImpl::GetChipInfo(uint32_t devIndex, std::string &chipInfo)
339 {
340 if (inputInterface_ == nullptr || inputInterface_->iInputController == nullptr ||
341 inputInterface_->iInputController->GetChipInfo == nullptr) {
342 HDF_LOGE("%{public}s: get input device Module instance failed", __func__);
343 return HDF_FAILURE;
344 }
345
346 char infoStr[CHIP_INFO_LEN] = {0};
347 int32_t ret = inputInterface_->iInputController->GetChipInfo(devIndex, infoStr, CHIP_INFO_LEN);
348 if (ret != INPUT_SUCCESS) {
349 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
350 return HDF_FAILURE;
351 }
352
353 chipInfo.assign(infoStr);
354 return ret;
355 }
356
GetVendorName(uint32_t devIndex,std::string & vendorName)357 int32_t InputInterfacesImpl::GetVendorName(uint32_t devIndex, std::string &vendorName)
358 {
359 if (inputInterface_ == nullptr || inputInterface_->iInputController == nullptr ||
360 inputInterface_->iInputController->GetVendorName == nullptr) {
361 HDF_LOGE("%{public}s: get input device Module instance failed", __func__);
362 return HDF_FAILURE;
363 }
364
365 char infoStr[VENDOR_NAME_LEN] = {0};
366 int32_t ret = inputInterface_->iInputController->GetVendorName(devIndex, infoStr, VENDOR_NAME_LEN);
367 if (ret != INPUT_SUCCESS) {
368 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
369 return HDF_FAILURE;
370 }
371
372 vendorName.assign(infoStr);
373 return ret;
374 }
375
GetChipName(uint32_t devIndex,std::string & chipName)376 int32_t InputInterfacesImpl::GetChipName(uint32_t devIndex, std::string &chipName)
377 {
378 if (inputInterface_ == nullptr || inputInterface_->iInputController == nullptr ||
379 inputInterface_->iInputController->GetChipName == nullptr) {
380 HDF_LOGE("%{public}s: get input device Module instance failed", __func__);
381 return HDF_FAILURE;
382 }
383
384 char infoStr[CHIP_NAME_LEN] = {0};
385 int32_t ret = inputInterface_->iInputController->GetChipName(devIndex, infoStr, CHIP_NAME_LEN);
386 if (ret != INPUT_SUCCESS) {
387 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
388 return HDF_FAILURE;
389 }
390
391 chipName.assign(infoStr);
392 return ret;
393 }
394
SetGestureMode(uint32_t devIndex,uint32_t gestureMode)395 int32_t InputInterfacesImpl::SetGestureMode(uint32_t devIndex, uint32_t gestureMode)
396 {
397 if (inputInterface_ == nullptr || inputInterface_->iInputController == nullptr ||
398 inputInterface_->iInputController->SetGestureMode == nullptr) {
399 HDF_LOGE("%{public}s: get input device Module instance failed", __func__);
400 return HDF_FAILURE;
401 }
402
403 int32_t ret = inputInterface_->iInputController->SetGestureMode(devIndex, gestureMode);
404 if (ret != INPUT_SUCCESS) {
405 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
406 }
407
408 return ret;
409 }
410
RunCapacitanceTest(uint32_t devIndex,uint32_t testType,std::string & result,uint32_t length)411 int32_t InputInterfacesImpl::RunCapacitanceTest(uint32_t devIndex, uint32_t testType, std::string &result,
412 uint32_t length)
413 {
414 if (inputInterface_ == nullptr || inputInterface_->iInputController == nullptr ||
415 inputInterface_->iInputController->RunCapacitanceTest == nullptr) {
416 HDF_LOGE("%{public}s: get input device Module instance failed", __func__);
417 return HDF_FAILURE;
418 }
419
420 int32_t ret = inputInterface_->iInputController->RunCapacitanceTest(devIndex, testType, result.data(), length);
421 if (ret != INPUT_SUCCESS) {
422 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
423 }
424
425 return ret;
426 }
427
RunExtraCommand(uint32_t devIndex,const ExtraCmd & cmd)428 int32_t InputInterfacesImpl::RunExtraCommand(uint32_t devIndex, const ExtraCmd &cmd)
429 {
430 InputExtraCmd cmdInfo;
431 cmdInfo.cmdCode = cmd.cmdCode.c_str();
432 cmdInfo.cmdValue = cmd.cmdValue.c_str();
433
434 if (inputInterface_ == nullptr || inputInterface_->iInputController == nullptr ||
435 inputInterface_->iInputController->RunExtraCommand == nullptr) {
436 HDF_LOGE("%{public}s: get input device Module instance failed", __func__);
437 return HDF_FAILURE;
438 }
439
440 int32_t ret = inputInterface_->iInputController->RunExtraCommand(devIndex, &cmdInfo);
441 if (ret != INPUT_SUCCESS) {
442 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
443 }
444
445 return ret;
446 }
447
RegisterReportCallback(uint32_t devIndex,const sptr<IInputCallback> & eventPkgCallback)448 int32_t InputInterfacesImpl::RegisterReportCallback(uint32_t devIndex, const sptr<IInputCallback> &eventPkgCallback)
449 {
450 if (inputInterface_ == nullptr || inputInterface_->iInputReporter == nullptr ||
451 inputInterface_->iInputReporter->RegisterReportCallback == nullptr) {
452 HDF_LOGE("%{public}s: get input device Module instance failed", __func__);
453 return HDF_FAILURE;
454 }
455
456 if (devIndex >= MAX_INPUT_DEV_NUM) {
457 HDF_LOGE("%{public}s: devIndex [%{public}d] out of range", __func__, devIndex);
458 return INPUT_INVALID_PARAM;
459 }
460
461 if (eventPkgCallback == nullptr) {
462 HDF_LOGE("%{public}s: eventPkgCallback is null", __func__);
463 return HDF_FAILURE;
464 }
465
466 int32_t ret = HDF_FAILURE;
467 ret = inputInterface_->iInputReporter->RegisterReportCallback(devIndex, &eventCb);
468 if (ret != INPUT_SUCCESS) {
469 HDF_LOGE("%{public}s failed, ret[%{public}d]", __func__, ret);
470 return ret;
471 }
472
473 std::lock_guard<std::mutex> lock(g_mutex);
474 g_inputEventCallback = eventPkgCallback;
475
476 return ret;
477 }
478
UnregisterReportCallback(uint32_t devIndex)479 int32_t InputInterfacesImpl::UnregisterReportCallback(uint32_t devIndex)
480 {
481 if (inputInterface_ == nullptr || inputInterface_->iInputReporter == nullptr ||
482 inputInterface_->iInputReporter->UnregisterReportCallback == nullptr) {
483 HDF_LOGE("%{public}s: get input device Module instance failed", __func__);
484 return HDF_FAILURE;
485 }
486
487 int32_t ret = inputInterface_->iInputReporter->UnregisterReportCallback(devIndex);
488 if (ret != INPUT_SUCCESS) {
489 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
490 return ret;
491 }
492 return ret;
493 }
494
RegisterHotPlugCallback(const sptr<IInputCallback> & hotPlugCallback)495 int32_t InputInterfacesImpl::RegisterHotPlugCallback(const sptr<IInputCallback> &hotPlugCallback)
496 {
497 if (inputInterface_ == nullptr || inputInterface_->iInputReporter == nullptr ||
498 inputInterface_->iInputReporter->UnregisterReportCallback == nullptr) {
499 HDF_LOGE("%{public}s: get input device Module instance failed", __func__);
500 return HDF_FAILURE;
501 }
502
503 if (hotPlugCallback == nullptr) {
504 HDF_LOGE("%{public}s: hotPlugCallback is null", __func__);
505 return HDF_FAILURE;
506 }
507
508 if (g_hotplugEventCallback != nullptr) {
509 return g_hotplugEventCallback == hotPlugCallback;
510 }
511
512 int32_t ret = HDF_FAILURE;
513 ret = inputInterface_->iInputReporter->RegisterHotPlugCallback(&hostCb);
514 if (ret != INPUT_SUCCESS) {
515 HDF_LOGE("%{public}s failed, ret[%{public}d]", __func__, ret);
516 return ret;
517 }
518
519 std::lock_guard<std::mutex> lock(g_mutex);
520 g_hotplugEventCallback = hotPlugCallback;
521
522 return ret;
523 }
524
UnregisterHotPlugCallback()525 int32_t InputInterfacesImpl::UnregisterHotPlugCallback()
526 {
527 if (inputInterface_ == nullptr || inputInterface_->iInputReporter == nullptr ||
528 inputInterface_->iInputReporter->UnregisterHotPlugCallback == nullptr) {
529 HDF_LOGE("%{public}s: get input device Module instance failed", __func__);
530 return HDF_FAILURE;
531 }
532
533 int32_t ret = inputInterface_->iInputReporter->UnregisterHotPlugCallback();
534 if (ret != INPUT_SUCCESS) {
535 HDF_LOGE("%{public}s failed, error code is %{public}d", __func__, ret);
536 return ret;
537 }
538
539 std::lock_guard<std::mutex> lock(g_mutex);
540 g_hotplugEventCallback = nullptr;
541
542 return ret;
543 }
544 } // V1_0
545 } // Input
546 } // HDI
547 } // OHOS
548