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 "input_uhdf_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
SetPowerStatus(uint32_t devIndex,uint32_t status)78 static int32_t SetPowerStatus(uint32_t devIndex, uint32_t status)
79 {
80 if (!gInputDeviceManager_) {
81 HDF_LOGE("%{public}s: gInputDeviceManager_ is null", __func__);
82 return INPUT_FAILURE;
83 }
84 return gInputDeviceManager_->SetPowerStatus(devIndex, status);
85 }
86
GetPowerStatus(uint32_t devIndex,uint32_t * status)87 static int32_t GetPowerStatus(uint32_t devIndex, uint32_t *status)
88 {
89 if (!gInputDeviceManager_) {
90 HDF_LOGE("%{public}s: gInputDeviceManager_ is null", __func__);
91 return INPUT_FAILURE;
92 }
93 return gInputDeviceManager_->GetPowerStatus(devIndex, status);
94 }
95
GetChipInfo(uint32_t devIndex,char * chipInfo,uint32_t length)96 static int32_t GetChipInfo(uint32_t devIndex, char *chipInfo, uint32_t length)
97 {
98 if (!gInputDeviceManager_) {
99 HDF_LOGE("%{public}s: gInputDeviceManager_ is null", __func__);
100 return INPUT_FAILURE;
101 }
102 return gInputDeviceManager_->GetChipInfo(devIndex, chipInfo, length);
103 }
104
GetVendorName(uint32_t devIndex,char * vendorName,uint32_t length)105 static int32_t GetVendorName(uint32_t devIndex, char *vendorName, uint32_t length)
106 {
107 if (!gInputDeviceManager_) {
108 HDF_LOGE("%{public}s: gInputDeviceManager_ is null", __func__);
109 return INPUT_FAILURE;
110 }
111 return gInputDeviceManager_->GetVendorName(devIndex, vendorName, length);
112 }
113
GetChipName(uint32_t devIndex,char * chipName,uint32_t length)114 static int32_t GetChipName(uint32_t devIndex, char *chipName, uint32_t length)
115 {
116 if (!gInputDeviceManager_) {
117 HDF_LOGE("%{public}s: gInputDeviceManager_ is null", __func__);
118 return INPUT_FAILURE;
119 }
120 return gInputDeviceManager_->GetChipName(devIndex, chipName, length);
121 }
122
GetDeviceType(uint32_t devIndex,uint32_t * deviceType)123 static int32_t GetDeviceType(uint32_t devIndex, uint32_t *deviceType)
124 {
125 if (!gInputDeviceManager_) {
126 HDF_LOGE("%{public}s: gInputDeviceManager_ is null", __func__);
127 return INPUT_FAILURE;
128 }
129 return gInputDeviceManager_->GetDeviceType(devIndex, deviceType);
130 }
131
SetGestureMode(uint32_t devIndex,uint32_t gestureMode)132 static int32_t SetGestureMode(uint32_t devIndex, uint32_t gestureMode)
133 {
134 if (!gInputDeviceManager_) {
135 HDF_LOGE("%{public}s: gInputDeviceManager_ is null", __func__);
136 return INPUT_FAILURE;
137 }
138 return gInputDeviceManager_->SetGestureMode(devIndex, gestureMode);
139 }
140
RunCapacitanceTest(uint32_t devIndex,uint32_t testType,char * result,uint32_t length)141 static int32_t RunCapacitanceTest(uint32_t devIndex, uint32_t testType, char *result, uint32_t length)
142 {
143 if (!gInputDeviceManager_) {
144 HDF_LOGE("%{public}s: gInputDeviceManager_ is null", __func__);
145 return INPUT_FAILURE;
146 }
147 return gInputDeviceManager_->RunCapacitanceTest(devIndex, testType, result, length);
148 }
149
RunExtraCommand(uint32_t devIndex,InputExtraCmd * cmd)150 static int32_t RunExtraCommand(uint32_t devIndex, InputExtraCmd *cmd)
151 {
152 if (!gInputDeviceManager_) {
153 HDF_LOGE("%{public}s: gInputDeviceManager_ is null", __func__);
154 return INPUT_FAILURE;
155 }
156 return gInputDeviceManager_->RunExtraCommand(devIndex, cmd);
157 }
158
RegisterReportCallback(uint32_t devIndex,InputEventCb * callback)159 static int32_t RegisterReportCallback(uint32_t devIndex, InputEventCb *callback)
160 {
161 if (!gInputDeviceManager_) {
162 HDF_LOGE("%{public}s: gInputDeviceManager_ is null", __func__);
163 return INPUT_FAILURE;
164 }
165 return gInputDeviceManager_->RegisterReportCallback(devIndex, callback);
166 }
167
UnregisterReportCallback(uint32_t devIndex)168 static int32_t UnregisterReportCallback(uint32_t devIndex)
169 {
170 if (!gInputDeviceManager_) {
171 HDF_LOGE("%{public}s: gInputDeviceManager_ is null", __func__);
172 return INPUT_FAILURE;
173 }
174 return gInputDeviceManager_->UnregisterReportCallback(devIndex);
175 }
176
RegisterHotPlugCallback(InputHostCb * callback)177 static int32_t RegisterHotPlugCallback(InputHostCb *callback)
178 {
179 if (!gInputDeviceManager_) {
180 HDF_LOGE("%{public}s: gInputDeviceManager_ is null", __func__);
181 return INPUT_FAILURE;
182 }
183 return gInputDeviceManager_->RegisterHotPlugCallback(callback);
184 }
185
UnregisterHotPlugCallback(void)186 static int32_t UnregisterHotPlugCallback(void)
187 {
188 if (!gInputDeviceManager_) {
189 HDF_LOGE("%{public}s: gInputDeviceManager_ is null", __func__);
190 return INPUT_FAILURE;
191 }
192 return gInputDeviceManager_->UnregisterHotPlugCallback();
193 }
194
195 #ifdef __cplusplus
196 extern "C" {
197 #endif
198
InstanceManagerHdi(InputManager ** manager)199 static int32_t InstanceManagerHdi(InputManager **manager)
200 {
201 InputManager *managerHdi = (InputManager *)OsalMemAlloc(sizeof(InputManager));
202 if (managerHdi == nullptr) {
203 HDF_LOGE("%{public}s: OsalMemAlloc failed", __func__);
204 return INPUT_NOMEM;
205 }
206 (void)memset_s(managerHdi, sizeof(InputManager), 0, sizeof(InputManager));
207 gInputDeviceManager_ = std::make_shared<InputDeviceManager>();
208 if (!gInputDeviceManager_) {
209 HDF_LOGE("%{public}s: gInputDeviceManager_ is null", __func__);
210 OsalMemFree(managerHdi);
211 managerHdi = nullptr;
212 return INPUT_NOMEM;
213 }
214 gInputDeviceManager_->Init();
215 managerHdi->ScanInputDevice = ScanInputDevice;
216 managerHdi->OpenInputDevice = OpenInputDevice;
217 managerHdi->CloseInputDevice = CloseInputDevice;
218 managerHdi->GetInputDevice = GetInputDevice;
219 managerHdi->GetInputDeviceList = GetInputDeviceList;
220 *manager = managerHdi;
221 return INPUT_SUCCESS;
222 }
223
InstanceControllerHdi(InputController ** controller)224 static int32_t InstanceControllerHdi(InputController **controller)
225 {
226 InputController *controllerHdi = (InputController *)OsalMemAlloc(sizeof(InputController));
227 if (controllerHdi == nullptr) {
228 HDF_LOGE("%{public}s: OsalMemAlloc failed", __func__);
229 return INPUT_NOMEM;
230 }
231 (void)memset_s(controllerHdi, sizeof(InputController), 0, sizeof(InputController));
232 controllerHdi->SetPowerStatus = SetPowerStatus;
233 controllerHdi->GetPowerStatus = GetPowerStatus;
234 controllerHdi->GetDeviceType = GetDeviceType;
235 controllerHdi->GetChipInfo = GetChipInfo;
236 controllerHdi->GetVendorName = GetVendorName;
237 controllerHdi->GetChipName = GetChipName;
238 controllerHdi->SetGestureMode = SetGestureMode;
239 controllerHdi->RunCapacitanceTest = RunCapacitanceTest;
240 controllerHdi->RunExtraCommand = RunExtraCommand;
241 *controller = controllerHdi;
242 return INPUT_SUCCESS;
243 }
244
InstanceReporterHdi(InputReporter ** reporter)245 static int32_t InstanceReporterHdi(InputReporter **reporter)
246 {
247 InputReporter *reporterHdi = (InputReporter *)OsalMemAlloc(sizeof(InputReporter));
248 if (reporterHdi == nullptr) {
249 HDF_LOGE("%{public}s: OsalMemAlloc failed", __func__);
250 return INPUT_NOMEM;
251 }
252 (void)memset_s(reporterHdi, sizeof(InputReporter), 0, sizeof(InputReporter));
253 reporterHdi->RegisterReportCallback = RegisterReportCallback;
254 reporterHdi->UnregisterReportCallback = UnregisterReportCallback;
255 reporterHdi->RegisterHotPlugCallback = RegisterHotPlugCallback;
256 reporterHdi->UnregisterHotPlugCallback = UnregisterHotPlugCallback;
257 *reporter = reporterHdi;
258 return INPUT_SUCCESS;
259 }
260
FreeInputHdi(IInputInterface ** hdi)261 static void FreeInputHdi(IInputInterface **hdi)
262 {
263 if (hdi == nullptr || *hdi == nullptr) {
264 return;
265 }
266 if ((*hdi)->iInputManager != nullptr) {
267 OsalMemFree((*hdi)->iInputManager);
268 (*hdi)->iInputManager = nullptr;
269 }
270 if ((*hdi)->iInputController != nullptr) {
271 OsalMemFree((*hdi)->iInputController);
272 (*hdi)->iInputController = nullptr;
273 }
274 if ((*hdi)->iInputReporter != nullptr) {
275 OsalMemFree((*hdi)->iInputReporter);
276 (*hdi)->iInputReporter = nullptr;
277 }
278 OsalMemFree((*hdi));
279 *hdi = nullptr;
280 }
281
InstanceInputHdi(void)282 static IInputInterface *InstanceInputHdi(void)
283 {
284 int32_t ret;
285 IInputInterface *hdi = (IInputInterface *)OsalMemAlloc(sizeof(IInputInterface));
286 if (hdi == nullptr) {
287 HDF_LOGE("%{public}s: OsalMemAlloc failed", __func__);
288 return nullptr;
289 }
290 (void)memset_s(hdi, sizeof(IInputInterface), 0, sizeof(IInputInterface));
291
292 ret = InstanceManagerHdi(&hdi->iInputManager);
293 if (ret != INPUT_SUCCESS) {
294 FreeInputHdi(&hdi);
295 return nullptr;
296 }
297 ret = InstanceControllerHdi(&hdi->iInputController);
298 if (ret != INPUT_SUCCESS) {
299 FreeInputHdi(&hdi);
300 return nullptr;
301 }
302 ret = InstanceReporterHdi(&hdi->iInputReporter);
303 if (ret != INPUT_SUCCESS) {
304 FreeInputHdi(&hdi);
305 return nullptr;
306 }
307 return hdi;
308 }
309
GetInputInterface(IInputInterface ** inputInterface)310 int32_t GetInputInterface(IInputInterface **inputInterface)
311 {
312 IInputInterface *inputHdi = nullptr;
313 if (inputInterface == nullptr) {
314 HDF_LOGE("%{public}s: parameter is null", __func__);
315 return INPUT_INVALID_PARAM;
316 }
317 inputHdi = InstanceInputHdi();
318 if (inputHdi == nullptr) {
319 HDF_LOGE("%{public}s: failed to instance hdi", __func__);
320 return INPUT_NULL_PTR;
321 }
322 *inputInterface = inputHdi;
323 HDF_LOGI("%{public}s: exit succ", __func__);
324 return INPUT_SUCCESS;
325 }
326
ReleaseInputInterface(IInputInterface ** inputInterface)327 void ReleaseInputInterface(IInputInterface **inputInterface)
328 {
329 if (inputInterface == nullptr || *inputInterface == nullptr) {
330 return;
331 }
332 FreeInputHdi(inputInterface);
333 }
334 #ifdef __cplusplus
335 }
336 #endif
337