• 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 #include <climits>
16 #include <cstdio>
17 #include <cstdlib>
18 #include <fcntl.h>
19 #include <functional>
20 #include <malloc.h>
21 #include <sys/ioctl.h>
22 #include <securec.h>
23 #include <unistd.h>
24 #include "hdf_io_service_if.h"
25 #include "hdf_log.h"
26 #include "input_device_manager.h"
27 #include "osal_mem.h"
28 
29 #define HDF_LOG_TAG InputHdiManager
30 
31 using namespace OHOS::Input;
32 static std::shared_ptr<InputDeviceManager> gInputDeviceManager_ {nullptr};
ScanInputDevice(InputDevDesc * staArr,uint32_t arrLen)33 static int32_t ScanInputDevice(InputDevDesc *staArr, uint32_t arrLen)
34 {
35     if (!gInputDeviceManager_) {
36         HDF_LOGE("%{public}s: gInputDeviceManager_ is null", __func__);
37         return INPUT_FAILURE;
38     }
39     return gInputDeviceManager_->ScanDevice(staArr, arrLen);
40 }
41 
OpenInputDevice(uint32_t devIndex)42 static int32_t OpenInputDevice(uint32_t devIndex)
43 {
44     if (!gInputDeviceManager_) {
45         HDF_LOGE("%{public}s: gInputDeviceManager_ is null", __func__);
46         return INPUT_FAILURE;
47     }
48     return gInputDeviceManager_->OpenDevice(devIndex);
49 }
50 
CloseInputDevice(uint32_t devIndex)51 static int32_t CloseInputDevice(uint32_t devIndex)
52 {
53     if (!gInputDeviceManager_) {
54         HDF_LOGE("%{public}s: gInputDeviceManager_ is null", __func__);
55         return INPUT_FAILURE;
56     }
57     return gInputDeviceManager_->CloseDevice(devIndex);
58 }
59 
GetInputDevice(uint32_t devIndex,InputDeviceInfo ** devInfo)60 static int32_t GetInputDevice(uint32_t devIndex, InputDeviceInfo **devInfo)
61 {
62     if (!gInputDeviceManager_) {
63         HDF_LOGE("%{public}s: gInputDeviceManager_ is null", __func__);
64         return INPUT_FAILURE;
65     }
66     return gInputDeviceManager_->GetDevice(devIndex, devInfo);
67 }
68 
GetInputDeviceList(uint32_t * devNum,InputDeviceInfo ** devList,uint32_t size)69 static int32_t GetInputDeviceList(uint32_t *devNum, InputDeviceInfo **devList, uint32_t size)
70 {
71     if (!gInputDeviceManager_) {
72         HDF_LOGE("%{public}s: gInputDeviceManager_ is null", __func__);
73         return INPUT_FAILURE;
74     }
75     return gInputDeviceManager_->GetDeviceList(devNum, devList, size);
76 }
77 
RegisterReportCallback(uint32_t devIndex,InputEventCb * callback)78 static int32_t RegisterReportCallback(uint32_t devIndex, InputEventCb *callback)
79 {
80     if (!gInputDeviceManager_) {
81         HDF_LOGE("%{public}s: gInputDeviceManager_ is null", __func__);
82         return INPUT_FAILURE;
83     }
84     return gInputDeviceManager_->RegisterReportCallback(devIndex, callback);
85 }
86 
UnregisterReportCallback(uint32_t devIndex)87 static int32_t UnregisterReportCallback(uint32_t devIndex)
88 {
89     if (!gInputDeviceManager_) {
90         HDF_LOGE("%{public}s: gInputDeviceManager_ is null", __func__);
91         return INPUT_FAILURE;
92     }
93     return gInputDeviceManager_->UnregisterReportCallback(devIndex);
94 }
95 
RegisterHotPlugCallback(InputHostCb * callback)96 static int32_t RegisterHotPlugCallback(InputHostCb *callback)
97 {
98     if (!gInputDeviceManager_) {
99         HDF_LOGE("%{public}s: gInputDeviceManager_ is null", __func__);
100         return INPUT_FAILURE;
101     }
102     return gInputDeviceManager_->RegisterHotPlugCallback(callback);
103 }
104 
UnregisterHotPlugCallback(void)105 static int32_t UnregisterHotPlugCallback(void)
106 {
107     if (!gInputDeviceManager_) {
108         HDF_LOGE("%{public}s: gInputDeviceManager_ is null", __func__);
109         return INPUT_FAILURE;
110     }
111     return gInputDeviceManager_->UnregisterHotPlugCallback();
112 }
113 
114 #ifdef __cplusplus
115 extern "C" {
116 #endif
117 
118 int32_t InstanceReporterHdi(InputReporter **reporter);
119 int32_t InstanceControllerHdi(InputController **controller);
InstanceManagerHdi(InputManager ** manager)120 static int32_t InstanceManagerHdi(InputManager **manager)
121 {
122     InputManager *managerHdi = (InputManager *)OsalMemAlloc(sizeof(InputManager));
123     if (managerHdi == nullptr) {
124         HDF_LOGE("%{public}s: OsalMemAlloc failed", __func__);
125         return INPUT_NOMEM;
126     }
127     (void)memset_s(managerHdi, sizeof(InputManager), 0, sizeof(InputManager));
128     gInputDeviceManager_ = std::make_shared<InputDeviceManager>();
129     if (!gInputDeviceManager_) {
130         HDF_LOGE("%{public}s: gInputDeviceManager_ is null", __func__);
131         OsalMemFree(managerHdi);
132         managerHdi = nullptr;
133         return INPUT_NOMEM;
134     }
135     gInputDeviceManager_->Init();
136     managerHdi->ScanInputDevice = &ScanInputDevice;
137     managerHdi->OpenInputDevice = &OpenInputDevice;
138     managerHdi->CloseInputDevice = &CloseInputDevice;
139     managerHdi->GetInputDevice = &GetInputDevice;
140     managerHdi->GetInputDeviceList = GetInputDeviceList;
141     *manager = managerHdi;
142     return INPUT_SUCCESS;
143 }
144 
InstanceControllerHdi(InputController ** controller)145 int32_t InstanceControllerHdi(InputController **controller)
146 {
147     InputController *controllerHdi = (InputController *)OsalMemAlloc(sizeof(InputController));
148     if (controllerHdi == nullptr) {
149         HDF_LOGE("%{public}s: OsalMemAlloc failed", __func__);
150         return INPUT_NOMEM;
151     }
152     (void)memset_s(controllerHdi, sizeof(InputController), 0, sizeof(InputController));
153     *controller = controllerHdi;
154     return INPUT_SUCCESS;
155 }
156 
InstanceReporterHdi(InputReporter ** reporter)157 int32_t InstanceReporterHdi(InputReporter **reporter)
158 {
159     InputReporter *reporterHdi = (InputReporter *)OsalMemAlloc(sizeof(InputReporter));
160     if (reporterHdi == nullptr) {
161         HDF_LOGE("%{public}s: OsalMemAlloc failed", __func__);
162         return INPUT_NOMEM;
163     }
164     (void)memset_s(reporterHdi, sizeof(InputReporter), 0, sizeof(InputReporter));
165     reporterHdi->RegisterReportCallback = RegisterReportCallback;
166     reporterHdi->UnregisterReportCallback = UnregisterReportCallback;
167     reporterHdi->RegisterHotPlugCallback = RegisterHotPlugCallback;
168     reporterHdi->UnregisterHotPlugCallback = UnregisterHotPlugCallback;
169     *reporter = reporterHdi;
170     return INPUT_SUCCESS;
171 }
172 
FreeInputHdi(IInputInterface * hdi)173 static void FreeInputHdi(IInputInterface *hdi)
174 {
175     if (hdi->iInputManager != nullptr) {
176         OsalMemFree(hdi->iInputManager);
177         hdi->iInputManager = nullptr;
178     }
179     if (hdi->iInputController != nullptr) {
180         OsalMemFree(hdi->iInputController);
181         hdi->iInputController = nullptr;
182     }
183     if (hdi->iInputReporter != nullptr) {
184         OsalMemFree(hdi->iInputReporter);
185         hdi->iInputReporter = nullptr;
186     }
187     OsalMemFree(hdi);
188 }
189 
InstanceInputHdi(void)190 static IInputInterface *InstanceInputHdi(void)
191 {
192     int32_t ret;
193     IInputInterface *hdi = (IInputInterface *)OsalMemAlloc(sizeof(IInputInterface));
194     if (hdi == nullptr) {
195         HDF_LOGE("%{public}s: OsalMemAlloc failed", __func__);
196         return nullptr;
197     }
198     (void)memset_s(hdi, sizeof(IInputInterface), 0, sizeof(IInputInterface));
199 
200     ret = InstanceManagerHdi(&hdi->iInputManager);
201     if (ret != INPUT_SUCCESS) {
202         FreeInputHdi(hdi);
203         return nullptr;
204     }
205     ret = InstanceControllerHdi(&hdi->iInputController);
206     if (ret != INPUT_SUCCESS) {
207         FreeInputHdi(hdi);
208         return nullptr;
209     }
210     ret = InstanceReporterHdi(&hdi->iInputReporter);
211     if (ret != INPUT_SUCCESS) {
212         FreeInputHdi(hdi);
213         return nullptr;
214     }
215     return hdi;
216 }
217 
GetInputInterface(IInputInterface ** inputInterface)218 int32_t GetInputInterface(IInputInterface **inputInterface)
219 {
220     IInputInterface *inputHdi = nullptr;
221     if (inputInterface == nullptr) {
222         HDF_LOGE("%{public}s: parameter is null", __func__);
223         return INPUT_INVALID_PARAM;
224     }
225     inputHdi = InstanceInputHdi();
226     if (inputHdi == nullptr) {
227         HDF_LOGE("%{public}s: failed to instance hdi", __func__);
228         return INPUT_NULL_PTR;
229     }
230     *inputInterface = inputHdi;
231     HDF_LOGI("%{public}s: exit succ", __func__);
232     return INPUT_SUCCESS;
233 }
234 
ReleaseInputInterface(IInputInterface * inputInterface)235 void ReleaseInputInterface(IInputInterface *inputInterface)
236 {
237     if (inputInterface == nullptr) {
238         return;
239     }
240     FreeInputHdi(inputInterface);
241     inputInterface = nullptr;
242 }
243 #ifdef __cplusplus
244 }
245 #endif
246