1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include "osal_time.h"
10 #include "hdf_log.h"
11 #include "event_hub.h"
12 
13 #define SEC_TO_USEC    1000000
14 
15 #ifdef __LITEOS_M__
SendFramePackages(InputDevice * inputDev)16 static void SendFramePackages(InputDevice *inputDev)
17 {
18     struct HdfDeviceObject *hdfDev = inputDev->hdfDevObj;
19     if (hdfDev == NULL || inputDev->pkgBuf == NULL) {
20         HDF_LOGE("%s: hdf dev is null", __func__);
21         return;
22     }
23     HdfDeviceSendEvent(hdfDev, 0, inputDev->pkgBuf);
24 }
25 #else
EventQueueWorkEntry(void * arg)26 void EventQueueWorkEntry(void *arg)
27 {
28     InputDevice *inputDev = (InputDevice *)arg;
29     if (inputDev == NULL) {
30         HDF_LOGE("%s: inputDev is NULL", __func__);
31         return;
32     }
33 
34     HdfDeviceSendEvent(inputDev->hdfDevObj, 0, inputDev->pkgBuf);
35     HdfSbufFlush(inputDev->pkgBuf);
36 }
37 #endif // __LITEOS_M__
38 
PushOnePackage(InputDevice * inputDev,uint32_t type,uint32_t code,int32_t value)39 void PushOnePackage(InputDevice *inputDev, uint32_t type, uint32_t code, int32_t value)
40 {
41     OsalTimespec time;
42     EventPackage package = {0};
43     uint32_t flag;
44     InputManager *inputManager = GetInputManager();
45 
46     if (inputDev == NULL) {
47         HDF_LOGE("%s: parm is null", __func__);
48         return;
49     }
50     OsalSpinLockIrqSave(&inputManager->lock, &flag);
51     package.type = type;
52     package.code = code;
53     package.value = value;
54     OsalGetTime(&time);
55     package.time = time.sec * SEC_TO_USEC + time.usec;
56 
57     if (!HdfSbufWriteBuffer(inputDev->pkgBuf, &package, sizeof(EventPackage))) {
58         HDF_LOGE("%s: sbuf write pkg failed, clear sbuf", __func__);
59         HdfSbufFlush(inputDev->pkgBuf);
60         inputDev->errFrameFlag = true;
61     }
62     inputDev->pkgCount++;
63 
64     if (inputDev->pkgCount >= inputDev->pkgNum) {
65         HDF_LOGE("%s: current pkgs num beyond the sbuf limit", __func__);
66         inputDev->errFrameFlag = true;
67     }
68 
69     if (type == EV_SYN && code == SYN_REPORT) {
70         if (!HdfSbufWriteBuffer(inputDev->pkgBuf, NULL, 0)) {
71             HDF_LOGE("%s: sbuf write null pkg failed, clear sbuf", __func__);
72             HdfSbufFlush(inputDev->pkgBuf);
73             inputDev->errFrameFlag = true;
74         }
75 
76         if (!inputDev->errFrameFlag) {
77 #ifdef __LITEOS_M__
78             SendFramePackages(inputDev);
79 #else
80             (void)HdfAddWork(&inputDev->eventWorkQueue, &inputDev->eventWork);
81 #endif // __LITEOS_M__
82         }
83 
84         inputDev->pkgCount = 0;
85         inputDev->errFrameFlag = false;
86 #ifdef __LITEOS_M__
87         HdfSbufFlush(inputDev->pkgBuf);
88 #endif // __LITEOS_M__
89     }
90     OsalSpinUnlockIrqRestore(&inputManager->lock, &flag);
91 }