• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "input_reporter.h"
16 #include <stdio.h>
17 #include <unistd.h>
18 #include <malloc.h>
19 #include <sys/ioctl.h>
20 #include <securec.h>
21 #include "hdf_io_service_if.h"
22 #include "osal_time.h"
23 #include "input_common.h"
24 
25 #define POLL_WAIT_MS 100
26 #define MOUSE_DATA_LEN 4
27 #define REL_X_BYTE 1
28 #define REL_Y_BYTE 2
29 #define MAX_EVENT_PKG_NUM 100
30 
31 InputDevManager *GetDevManager(void);
32 
EventListenerCallback(struct HdfDevEventlistener * listener,struct HdfIoService * service,uint32_t id,struct HdfSBuf * data)33 static int32_t EventListenerCallback(struct HdfDevEventlistener *listener, struct HdfIoService *service,
34     uint32_t id, struct HdfSBuf *data)
35 {
36     (void)listener;
37     (void)id;
38     int32_t count = 0;
39     uint32_t len = 0;
40     InputEventPackage *pkgs[MAX_EVENT_PKG_NUM] = {0};
41     DeviceInfoNode *pos = NULL;
42     DeviceInfoNode *next = NULL;
43     InputDevManager *manager = NULL;
44 
45     if (service == NULL || data == NULL) {
46         HDF_LOGE("%s: invalid param", __func__);
47         return INPUT_INVALID_PARAM;
48     }
49 
50     manager = GetDevManager();
51     if (manager == NULL) {
52         HDF_LOGE("%s: get manager failed", __func__);
53         return INPUT_NULL_PTR;
54     }
55 
56     while (true) {
57         if (count >= MAX_EVENT_PKG_NUM) {
58             break;
59         }
60 
61         if (!HdfSbufReadBuffer(data, (const void **)&pkgs[count], &len)) {
62             HDF_LOGE("%s: sbuf read finished", __func__);
63             break;
64         }
65 
66         if (pkgs[count] == NULL) {
67             break;
68         }
69         count++;
70     }
71 
72     DLIST_FOR_EACH_ENTRY_SAFE(pos, next, &manager->devList, DeviceInfoNode, node) {
73         if (pos->service == service) {
74             if (pos->eventCb == NULL) {
75                 HDF_LOGE("%s: param is null", __func__);
76                 return INPUT_NULL_PTR;
77             }
78             pos->eventCb->EventPkgCallback((const InputEventPackage **)pkgs, count, pos->payload.devIndex);
79         }
80     }
81     return INPUT_SUCCESS;
82 }
83 
EventListenerInstance(void)84 static struct HdfDevEventlistener *EventListenerInstance(void)
85 {
86     struct HdfDevEventlistener *listener = (struct HdfDevEventlistener *)malloc(sizeof(struct HdfDevEventlistener));
87     if (listener == NULL) {
88         HDF_LOGE("%s: instance listener failed", __func__);
89         return NULL;
90     }
91 
92     (void)memset_s(listener, sizeof(struct HdfDevEventlistener), 0, sizeof(struct HdfDevEventlistener));
93     listener->onReceive = EventListenerCallback;
94     return listener;
95 }
96 
RegisterReportCallback(uint32_t devIndex,InputEventCb * callback)97 static int32_t RegisterReportCallback(uint32_t devIndex, InputEventCb *callback)
98 {
99     DeviceInfoNode *pos = NULL;
100     DeviceInfoNode *next = NULL;
101     InputDevManager *manager = NULL;
102 
103     if ((devIndex >= MAX_INPUT_DEV_NUM) || (callback == NULL) || (callback->EventPkgCallback == NULL)) {
104         HDF_LOGE("%s: invalid param", __func__);
105         return INPUT_INVALID_PARAM;
106     }
107     GET_MANAGER_CHECK_RETURN(manager);
108 
109     pthread_mutex_lock(&manager->mutex);
110     DLIST_FOR_EACH_ENTRY_SAFE(pos, next, &manager->devList, DeviceInfoNode, node) {
111         if (pos->payload.devIndex != devIndex) {
112             continue;
113         }
114         struct HdfDevEventlistener *listener = EventListenerInstance();
115         if (listener == NULL) {
116             pthread_mutex_unlock(&manager->mutex);
117             return INPUT_FAILURE;
118         }
119         if (HdfDeviceRegisterEventListener(pos->service, listener) != INPUT_SUCCESS) {
120             free(listener);
121             pthread_mutex_unlock(&manager->mutex);
122             HDF_LOGE("%s: fail to register listener", __func__);
123             return INPUT_FAILURE;
124         }
125         manager->evtCallbackNum++;
126         pos->eventCb = callback;
127         pos->listener = listener;
128         pthread_mutex_unlock(&manager->mutex);
129         HDF_LOGI("%s: device%u register callback succ, evtCallbackNum = %d", __func__,
130             devIndex, manager->evtCallbackNum);
131         return INPUT_SUCCESS;
132     }
133 
134     pthread_mutex_unlock(&manager->mutex);
135     HDF_LOGE("%s: device%u doesn't exist, can't register callback", __func__, devIndex);
136     return INPUT_FAILURE;
137 }
138 
UnregisterReportCallback(uint32_t devIndex)139 static int32_t UnregisterReportCallback(uint32_t devIndex)
140 {
141     DeviceInfoNode *pos = NULL;
142     DeviceInfoNode *next = NULL;
143     InputDevManager *manager = NULL;
144 
145     if (devIndex >= MAX_INPUT_DEV_NUM) {
146         HDF_LOGE("%s: invalid param", __func__);
147         return INPUT_INVALID_PARAM;
148     }
149     GET_MANAGER_CHECK_RETURN(manager);
150 
151     pthread_mutex_lock(&manager->mutex);
152     DLIST_FOR_EACH_ENTRY_SAFE(pos, next, &manager->devList, DeviceInfoNode, node) {
153         if (pos->payload.devIndex != devIndex) {
154             continue;
155         }
156         if (pos->eventCb != NULL) {
157             if (HdfDeviceUnregisterEventListener(pos->service, pos->listener) != INPUT_SUCCESS) {
158                 pthread_mutex_unlock(&manager->mutex);
159                 HDF_LOGE("%s: fail to unregister listener", __func__);
160                 return INPUT_FAILURE;
161             }
162             free(pos->listener);
163             pos->listener = NULL;
164             pos->eventCb = NULL;
165             manager->evtCallbackNum--;
166             pthread_mutex_unlock(&manager->mutex);
167             HDF_LOGI("%s: device%u unregister callback succ", __func__, devIndex);
168             return INPUT_SUCCESS;
169         } else {
170             pthread_mutex_unlock(&manager->mutex);
171             HDF_LOGE("%s: device%u does not register callback", __func__, devIndex);
172             return INPUT_FAILURE;
173         }
174     }
175 
176     pthread_mutex_unlock(&manager->mutex);
177     HDF_LOGE("%s: device%u doesn't exist, can't unregister callback", __func__, devIndex);
178     return INPUT_FAILURE;
179 }
180 
HotPlugEventListenerCallback(struct HdfDevEventlistener * listener,struct HdfIoService * service,uint32_t id,struct HdfSBuf * data)181 static int32_t HotPlugEventListenerCallback(struct HdfDevEventlistener *listener,
182     struct HdfIoService *service, uint32_t id, struct HdfSBuf *data)
183 {
184     (void)listener;
185     (void)id;
186     uint32_t len = 0;
187     InputHotPlugEvent *event = NULL;
188     InputDevManager *manager = NULL;
189     bool ret = false;
190 
191     if (service == NULL || data == NULL) {
192         HDF_LOGE("%s: invalid param", __func__);
193         return INPUT_INVALID_PARAM;
194     }
195 
196     GET_MANAGER_CHECK_RETURN(manager);
197 
198     ret = HdfSbufReadBuffer(data, (const void **)&event, &len);
199     if (!ret) {
200         HDF_LOGE("%s: read sbuf failed", __func__);
201         return INPUT_FAILURE;
202     }
203     if (manager->hostDev.hostCb == NULL) {
204         HDF_LOGE("%s: param is null", __func__);
205         return INPUT_NULL_PTR;
206     }
207     manager->hostDev.hostCb->HotPlugCallback((const InputHotPlugEvent *)event);
208     return INPUT_SUCCESS;
209 }
210 
HotPlugEventListenerInstance(void)211 static struct HdfDevEventlistener *HotPlugEventListenerInstance(void)
212 {
213     struct HdfDevEventlistener *listener = (struct HdfDevEventlistener *)malloc(sizeof(struct HdfDevEventlistener));
214     if (listener == NULL) {
215         HDF_LOGE("%s: instance listener failed", __func__);
216         return NULL;
217     }
218 
219     (void)memset_s(listener, sizeof(struct HdfDevEventlistener), 0, sizeof(struct HdfDevEventlistener));
220     listener->onReceive = HotPlugEventListenerCallback;
221     return listener;
222 }
223 
RegisterHotPlugCallback(InputHostCb * callback)224 static int32_t RegisterHotPlugCallback(InputHostCb *callback)
225 {
226     InputDevManager *manager = NULL;
227 
228     if ((callback == NULL) || (callback->HotPlugCallback == NULL)) {
229         HDF_LOGE("%s: invalid param", __func__);
230         return INPUT_INVALID_PARAM;
231     }
232     GET_MANAGER_CHECK_RETURN(manager);
233 
234     pthread_mutex_lock(&manager->mutex);
235     if (manager->hostDev.service == NULL) {
236         manager->hostDev.service = HdfIoServiceBind(DEV_MANAGER_SERVICE_NAME);
237         if (manager->hostDev.service == NULL) {
238             pthread_mutex_unlock(&manager->mutex);
239             return INPUT_FAILURE;
240         }
241     }
242 
243     struct HdfDevEventlistener *listener = HotPlugEventListenerInstance();
244     if (listener == NULL) {
245         pthread_mutex_unlock(&manager->mutex);
246         HDF_LOGE("%s: fail to instance listener", __func__);
247         return INPUT_FAILURE;
248     }
249     if (HdfDeviceRegisterEventListener(manager->hostDev.service, listener) != INPUT_SUCCESS) {
250         pthread_mutex_unlock(&manager->mutex);
251         HDF_LOGE("%s: fail to register listener", __func__);
252         free(listener);
253         return INPUT_FAILURE;
254     }
255     manager->hostDev.hostCb = callback;
256     manager->hostDev.listener = listener;
257     pthread_mutex_unlock(&manager->mutex);
258     return INPUT_SUCCESS;
259 }
260 
UnregisterHotPlugCallback(void)261 static int32_t UnregisterHotPlugCallback(void)
262 {
263     InputDevManager *manager = NULL;
264     GET_MANAGER_CHECK_RETURN(manager);
265 
266     pthread_mutex_lock(&manager->mutex);
267     if (manager->hostDev.listener != NULL) {
268         free(manager->hostDev.listener);
269         manager->hostDev.listener = NULL;
270     }
271     manager->hostDev.hostCb = NULL;
272     pthread_mutex_unlock(&manager->mutex);
273     return INPUT_SUCCESS;
274 }
275 
InstanceReporterHdi(InputReporter ** reporter)276 int32_t InstanceReporterHdi(InputReporter **reporter)
277 {
278     InputReporter *reporterHdi = (InputReporter *)malloc(sizeof(InputReporter));
279     if (reporterHdi == NULL) {
280         HDF_LOGE("%s: malloc fail", __func__);
281         return INPUT_NOMEM;
282     }
283 
284     (void)memset_s(reporterHdi, sizeof(InputReporter), 0, sizeof(InputReporter));
285 
286     reporterHdi->RegisterReportCallback = RegisterReportCallback;
287     reporterHdi->UnregisterReportCallback = UnregisterReportCallback;
288     reporterHdi->RegisterHotPlugCallback = RegisterHotPlugCallback;
289     reporterHdi->UnregisterHotPlugCallback = UnregisterHotPlugCallback;
290     *reporter = reporterHdi;
291 
292     return INPUT_SUCCESS;
293 }
294