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