• 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 
16 #ifndef INPUT_DEVICE_MANAGER_H
17 #define INPUT_DEVICE_MANAGER_H
18 
19 #include <functional>
20 #include <linux/input.h>
21 #include <list>
22 #include <map>
23 #include <mutex>
24 #include <pthread.h>
25 #include <sys/epoll.h>
26 #include <thread>
27 #include <unistd.h>
28 #include <vector>
29 #include "hdf_log.h"
30 #include "input_manager.h"
31 
32 namespace OHOS {
33 namespace Input {
34 using namespace std;
35 #define EPOLL_WAIT_TIMEOUT  (-1)
36 #define DEVICE_INFO_SIZE    (64)
37 #define EPOLL_MAX_EVENTS    (32)
38 #define MAX_SUPPORT_DEVS    (32)
39 #define BUFFER_SIZE         (1024)
40 #define ARRAY_LENGTH        (128)
41 #define NAME_LENGTH         (128)
42 #define EVENT_BUFFER_SIZE   (512)
43 #define MS_THOUSAND         (1000)
44 
45 enum InputDeviceStatus {
46     INPUT_DEVICE_STATUS_OPENED = 1,
47     INPUT_DEVICE_STATUS_CLOSED,
48     INPUT_DEVICE_STATUS_UNKNOWN
49 };
50 
51 // manager the device node list
52 typedef struct {
53     uint32_t index;
54     uint32_t status;
55     int32_t fd;
56     char devPathNode[DEV_NAME_LEN];
57     InputDeviceInfo detailInfo;
58 } InputDevListNode;
59 
60 class InputDeviceManager {
61 public:
62     InputDeviceManager() = default;
~InputDeviceManager()63     virtual ~InputDeviceManager()
64     {
65         inputDevList_.clear();
66         reportEventPkgCallback_.clear();
67     };
68     InputDeviceManager(const InputDeviceManager &other) = delete;
69     InputDeviceManager(InputDeviceManager &&other) = delete;
70     InputDeviceManager &operator=(const InputDeviceManager &other) = delete;
71     InputDeviceManager &operator=(InputDeviceManager &&other) = delete;
72     void Init(void);
73     vector<string> GetFiles(string path);
74     void DoRead(int32_t fd, struct input_event *event, size_t size);
75     int32_t OpenInputDevice(string devPath);
76     RetStatus CloseInputDevice(string devPath);
77     int32_t GetInputDeviceInfo(int32_t fd, InputDeviceInfo *detailInfo);
78     int32_t InotifyEventHandler(int32_t epollFd, int32_t notifyFd);
79     void RemoveEpoll(int32_t epollFd, int32_t fileFd);
80     int32_t AddToEpoll(int32_t epollFd, int32_t fileFd);
81     void WorkerThread();
82     int32_t FindIndexFromFd(int32_t &fd, uint32_t *index);
83     int32_t FindIndexFromDevName(string devName, uint32_t *index);
84     void DoWithEventDeviceAdd(int32_t &epollFd, int32_t &fd, string devPath);
85     void SendHotPlugEvent(uint32_t &type, uint32_t &index, uint32_t status);
86     void DoWithEventDeviceDel(int32_t &epollFd, uint32_t &index);
87     void ReportEventPkg(int32_t iFd, InputEventPackage **iEvtPkg, size_t iCount);
88     // InputManager
89     RetStatus ScanDevice(InputDevDesc *staArr, uint32_t arrLen);
90     RetStatus OpenDevice(uint32_t deviceIndex);
91     RetStatus CloseDevice(uint32_t deviceIndex);
92     int32_t GetDevice(int32_t deviceIndex, InputDeviceInfo **devInfo);
93     int32_t GetDeviceList(uint32_t *devNum, InputDeviceInfo **deviceList, uint32_t size);
94     // InputController
95     RetStatus SetPowerStatus(uint32_t devIndex, uint32_t status);
96     RetStatus GetPowerStatus(uint32_t devIndex, uint32_t *status);
97     RetStatus GetDeviceType(uint32_t devIndex, uint32_t *deviceType);
98     RetStatus GetChipInfo(uint32_t devIndex, char *chipInfo, uint32_t length);
99     RetStatus GetVendorName(uint32_t devIndex, char *vendorName, uint32_t length);
100     RetStatus GetChipName(uint32_t devIndex, char *chipName, uint32_t length);
101     RetStatus SetGestureMode(uint32_t devIndex, uint32_t gestureMode);
102     RetStatus RunCapacitanceTest(uint32_t devIndex, uint32_t testType, char *result, uint32_t length);
103     RetStatus RunExtraCommand(uint32_t devIndex, InputExtraCmd *cmd);
104     // InputReporter
105     RetStatus RegisterReportCallback(uint32_t devIndex, InputEventCb *callback);
106     RetStatus UnregisterReportCallback(uint32_t devIndex);
107     RetStatus RegisterHotPlugCallback(InputHostCb *callback);
108     RetStatus UnregisterHotPlugCallback(void);
dumpInfoList(InputDevListNode in)109     void dumpInfoList(InputDevListNode in)
110     {
111         HDF_LOGD("%{public}s index: %{public}u state:%{public}u fd:%{public}d devPathNode:%{public}s", __func__,
112                  in.index, in.status, in.fd, in.devPathNode);
113         HDF_LOGD("devIndex: %{public}u devType:%{public}u chipInfo:%{public}s "
114                  "vendorName: %{public}s chipName: %{public}s attrSet.devName: %s "
115                  "attrSet.id.busType: %{public}u attrSet.id.vendor: %{public}u "
116                  "attrSet.id.product: %{public}u attrSet.id.version: %{public}u ",
117                  in.detailInfo.devIndex, in.detailInfo.devType,
118                  in.detailInfo.chipInfo, in.detailInfo.vendorName,
119                  in.detailInfo.chipName, in.detailInfo.attrSet.devName,
120                  in.detailInfo.attrSet.id.busType, in.detailInfo.attrSet.id.vendor,
121                  in.detailInfo.attrSet.id.product, in.detailInfo.attrSet.id.version);
122         for (int32_t i = 0; i < ABS_CNT; i++) {
123             HDF_LOGD("attrSet.axisInfo.axis: %{public}d attrSet.axisInfo.min: %{public}d "
124                      "attrSet.axisInfo.max: %{public}d attrSet.axisInfo.fuzz: %{public}d "
125                      "attrSet.axisInfo.flat: %{public}d attrSet.axisInfo.range: %{public}d ",
126                      in.detailInfo.attrSet.axisInfo[i].axis, in.detailInfo.attrSet.axisInfo[i].flat,
127                      in.detailInfo.attrSet.axisInfo[i].fuzz, in.detailInfo.attrSet.axisInfo[i].max,
128                      in.detailInfo.attrSet.axisInfo[i].min, in.detailInfo.attrSet.axisInfo[i].range);
129         }
130     }
131 private:
132     int32_t DoInputDeviceAction(void);
133     void DeleteDevListNode(int index);
134     int32_t AddDeviceNodeToList(
135         int32_t &epollFd, int32_t &fd, string devPath, std::shared_ptr<InputDeviceInfo> &detailInfo);
136     int32_t GetCurDevIndex();
137     void LoadInputDevices(std::vector<std::string> &flist);
138     void ReloadInputDevices(std::vector<std::string> flist);
139     int32_t CreateInputDevListNode(InputDevListNode &inputDevNode, std::string &fileName);
140 
141     mutable std::mutex lock_;
142     std::mutex reportEventPkgCallBackLock_;
143     std::map<uint32_t, InputDevListNode> inputDevList_;
144     struct epoll_event epollEventList_[EPOLL_MAX_EVENTS] {};
145     std::string devPath_ {"/dev/input"};
146     uint32_t devIndex_ {1};
147     std::map<int32_t, InputEventCb*> reportEventPkgCallback_;
148     InputHostCb *reportHotPlugEventCallback_ = nullptr;
149     std::thread thread_ {};
150     int32_t mEpollId_ {0};
151     int32_t mInotifyId_ {0};
152 };
153 }
154 }
155 #endif // INPUT_DEVICE_MANAGER_H
156