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 == nullptr || *hdi == nullptr) {
176 return;
177 }
178 if ((*hdi)->iInputManager != nullptr) {
179 OsalMemFree((*hdi)->iInputManager);
180 (*hdi)->iInputManager = nullptr;
181 }
182 if ((*hdi)->iInputController != nullptr) {
183 OsalMemFree((*hdi)->iInputController);
184 (*hdi)->iInputController = nullptr;
185 }
186 if ((*hdi)->iInputReporter != nullptr) {
187 OsalMemFree((*hdi)->iInputReporter);
188 (*hdi)->iInputReporter = nullptr;
189 }
190 OsalMemFree((*hdi));
191 *hdi = nullptr;
192 }
193
InstanceInputHdi(void)194 static IInputInterface *InstanceInputHdi(void)
195 {
196 int32_t ret;
197 IInputInterface *hdi = (IInputInterface *)OsalMemAlloc(sizeof(IInputInterface));
198 if (hdi == nullptr) {
199 HDF_LOGE("%{public}s: OsalMemAlloc failed", __func__);
200 return nullptr;
201 }
202 (void)memset_s(hdi, sizeof(IInputInterface), 0, sizeof(IInputInterface));
203
204 ret = InstanceManagerHdi(&hdi->iInputManager);
205 if (ret != INPUT_SUCCESS) {
206 FreeInputHdi(&hdi);
207 return nullptr;
208 }
209 ret = InstanceControllerHdi(&hdi->iInputController);
210 if (ret != INPUT_SUCCESS) {
211 FreeInputHdi(&hdi);
212 return nullptr;
213 }
214 ret = InstanceReporterHdi(&hdi->iInputReporter);
215 if (ret != INPUT_SUCCESS) {
216 FreeInputHdi(&hdi);
217 return nullptr;
218 }
219 return hdi;
220 }
221
GetInputInterface(IInputInterface ** inputInterface)222 int32_t GetInputInterface(IInputInterface **inputInterface)
223 {
224 IInputInterface *inputHdi = nullptr;
225 if (inputInterface == nullptr) {
226 HDF_LOGE("%{public}s: parameter is null", __func__);
227 return INPUT_INVALID_PARAM;
228 }
229 inputHdi = InstanceInputHdi();
230 if (inputHdi == nullptr) {
231 HDF_LOGE("%{public}s: failed to instance hdi", __func__);
232 return INPUT_NULL_PTR;
233 }
234 *inputInterface = inputHdi;
235 HDF_LOGI("%{public}s: exit succ", __func__);
236 return INPUT_SUCCESS;
237 }
238
ReleaseInputInterface(IInputInterface ** inputInterface)239 void ReleaseInputInterface(IInputInterface **inputInterface)
240 {
241 if (inputInterface == nullptr || *inputInterface == nullptr) {
242 return;
243 }
244 FreeInputHdi(inputInterface);
245 }
246 #ifdef __cplusplus
247 }
248 #endif
249