1094332d3Sopenharmony_ci/* 2094332d3Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License. 5094332d3Sopenharmony_ci * You may obtain a copy of the License at 6094332d3Sopenharmony_ci * 7094332d3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8094332d3Sopenharmony_ci * 9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and 13094332d3Sopenharmony_ci * limitations under the License. 14094332d3Sopenharmony_ci */ 15094332d3Sopenharmony_ci 16094332d3Sopenharmony_ci#ifndef INPUT_DEVICE_MANAGER_H 17094332d3Sopenharmony_ci#define INPUT_DEVICE_MANAGER_H 18094332d3Sopenharmony_ci 19094332d3Sopenharmony_ci#include <functional> 20094332d3Sopenharmony_ci#include <linux/input.h> 21094332d3Sopenharmony_ci#include <list> 22094332d3Sopenharmony_ci#include <map> 23094332d3Sopenharmony_ci#include <mutex> 24094332d3Sopenharmony_ci#include <pthread.h> 25094332d3Sopenharmony_ci#include <sys/epoll.h> 26094332d3Sopenharmony_ci#include <thread> 27094332d3Sopenharmony_ci#include <unistd.h> 28094332d3Sopenharmony_ci#include <vector> 29094332d3Sopenharmony_ci#include "input_manager.h" 30094332d3Sopenharmony_ci#include "input_uhdf_log.h" 31094332d3Sopenharmony_ci 32094332d3Sopenharmony_cinamespace OHOS { 33094332d3Sopenharmony_cinamespace Input { 34094332d3Sopenharmony_ciusing namespace std; 35094332d3Sopenharmony_ci#define EPOLL_WAIT_TIMEOUT (-1) 36094332d3Sopenharmony_ci#define DEVICE_INFO_SIZE (64) 37094332d3Sopenharmony_ci#define EPOLL_MAX_EVENTS (32) 38094332d3Sopenharmony_ci#define MAX_SUPPORT_DEVS (32) 39094332d3Sopenharmony_ci#define BUFFER_SIZE (1024) 40094332d3Sopenharmony_ci#define ARRAY_LENGTH (128) 41094332d3Sopenharmony_ci#define NAME_LENGTH (128) 42094332d3Sopenharmony_ci#define EVENT_BUFFER_SIZE (512) 43094332d3Sopenharmony_ci#define MS_THOUSAND (1000) 44094332d3Sopenharmony_ci 45094332d3Sopenharmony_cienum InputDeviceStatus { 46094332d3Sopenharmony_ci INPUT_DEVICE_STATUS_OPENED = 1, 47094332d3Sopenharmony_ci INPUT_DEVICE_STATUS_CLOSED, 48094332d3Sopenharmony_ci INPUT_DEVICE_STATUS_UNKNOWN 49094332d3Sopenharmony_ci}; 50094332d3Sopenharmony_ci 51094332d3Sopenharmony_ci// manager the device node list 52094332d3Sopenharmony_citypedef struct { 53094332d3Sopenharmony_ci uint32_t index; 54094332d3Sopenharmony_ci uint32_t status; 55094332d3Sopenharmony_ci int32_t fd; 56094332d3Sopenharmony_ci char devPathNode[DEV_NAME_LEN]; 57094332d3Sopenharmony_ci InputDeviceInfo detailInfo; 58094332d3Sopenharmony_ci} InputDevListNode; 59094332d3Sopenharmony_ci 60094332d3Sopenharmony_ciclass InputDeviceManager { 61094332d3Sopenharmony_cipublic: 62094332d3Sopenharmony_ci InputDeviceManager() = default; 63094332d3Sopenharmony_ci virtual ~InputDeviceManager() 64094332d3Sopenharmony_ci { 65094332d3Sopenharmony_ci inputDevList_.clear(); 66094332d3Sopenharmony_ci reportEventPkgCallback_.clear(); 67094332d3Sopenharmony_ci }; 68094332d3Sopenharmony_ci InputDeviceManager(const InputDeviceManager &other) = delete; 69094332d3Sopenharmony_ci InputDeviceManager(InputDeviceManager &&other) = delete; 70094332d3Sopenharmony_ci InputDeviceManager &operator=(const InputDeviceManager &other) = delete; 71094332d3Sopenharmony_ci InputDeviceManager &operator=(InputDeviceManager &&other) = delete; 72094332d3Sopenharmony_ci void Init(void); 73094332d3Sopenharmony_ci vector<string> GetFiles(string path); 74094332d3Sopenharmony_ci void DoRead(int32_t fd, struct input_event *event, size_t size); 75094332d3Sopenharmony_ci int32_t OpenInputDevice(string devPath); 76094332d3Sopenharmony_ci RetStatus CloseInputDevice(string devPath); 77094332d3Sopenharmony_ci int32_t GetInputDeviceInfo(int32_t fd, InputDeviceInfo *detailInfo); 78094332d3Sopenharmony_ci void GetInputDeviceInfoList(int32_t epollFd = 0); 79094332d3Sopenharmony_ci int32_t DoInputDeviceAction(void); 80094332d3Sopenharmony_ci int32_t InotifyEventHandler(int32_t epollFd, int32_t notifyFd); 81094332d3Sopenharmony_ci void RemoveEpoll(int32_t epollFd, int32_t fileFd); 82094332d3Sopenharmony_ci int32_t AddToEpoll(int32_t epollFd, int32_t fileFd); 83094332d3Sopenharmony_ci void WorkerThread(); 84094332d3Sopenharmony_ci int32_t FindIndexFromFd(int32_t &fd, uint32_t *index); 85094332d3Sopenharmony_ci int32_t FindIndexFromDevName(string devName, uint32_t *index); 86094332d3Sopenharmony_ci void DoWithEventDeviceAdd(int32_t &epollFd, int32_t &fd, string devPath); 87094332d3Sopenharmony_ci void SendHotPlugEvent(uint32_t &type, uint32_t &index, uint32_t status); 88094332d3Sopenharmony_ci void DoWithEventDeviceDel(int32_t &epollFd, uint32_t &index); 89094332d3Sopenharmony_ci void ReportEventPkg(int32_t iFd, InputEventPackage **iEvtPkg, size_t iCount); 90094332d3Sopenharmony_ci // InputManager 91094332d3Sopenharmony_ci RetStatus ScanDevice(InputDevDesc *staArr, uint32_t arrLen); 92094332d3Sopenharmony_ci RetStatus OpenDevice(uint32_t deviceIndex); 93094332d3Sopenharmony_ci RetStatus CloseDevice(uint32_t deviceIndex); 94094332d3Sopenharmony_ci int32_t GetDevice(int32_t deviceIndex, InputDeviceInfo **devInfo); 95094332d3Sopenharmony_ci int32_t GetDeviceList(uint32_t *devNum, InputDeviceInfo **deviceList, uint32_t size); 96094332d3Sopenharmony_ci // InputController 97094332d3Sopenharmony_ci RetStatus SetPowerStatus(uint32_t devIndex, uint32_t status); 98094332d3Sopenharmony_ci RetStatus GetPowerStatus(uint32_t devIndex, uint32_t *status); 99094332d3Sopenharmony_ci RetStatus GetDeviceType(uint32_t devIndex, uint32_t *deviceType); 100094332d3Sopenharmony_ci RetStatus GetChipInfo(uint32_t devIndex, char *chipInfo, uint32_t length); 101094332d3Sopenharmony_ci RetStatus GetVendorName(uint32_t devIndex, char *vendorName, uint32_t length); 102094332d3Sopenharmony_ci RetStatus GetChipName(uint32_t devIndex, char *chipName, uint32_t length); 103094332d3Sopenharmony_ci RetStatus SetGestureMode(uint32_t devIndex, uint32_t gestureMode); 104094332d3Sopenharmony_ci RetStatus RunCapacitanceTest(uint32_t devIndex, uint32_t testType, char *result, uint32_t length); 105094332d3Sopenharmony_ci RetStatus RunExtraCommand(uint32_t devIndex, InputExtraCmd *cmd); 106094332d3Sopenharmony_ci // InputReporter 107094332d3Sopenharmony_ci RetStatus RegisterReportCallback(uint32_t devIndex, InputEventCb *callback); 108094332d3Sopenharmony_ci RetStatus UnregisterReportCallback(uint32_t devIndex); 109094332d3Sopenharmony_ci RetStatus RegisterHotPlugCallback(InputHostCb *callback); 110094332d3Sopenharmony_ci RetStatus UnregisterHotPlugCallback(void); 111094332d3Sopenharmony_ci void dumpInfoList(InputDevListNode in) 112094332d3Sopenharmony_ci { 113094332d3Sopenharmony_ci HDF_LOGD("%{public}s index: %{public}u state:%{public}u fd:%{public}d devPathNode:%{public}s", __func__, 114094332d3Sopenharmony_ci in.index, in.status, in.fd, in.devPathNode); 115094332d3Sopenharmony_ci HDF_LOGD("devIndex: %{public}u devType:%{public}u chipInfo:%{public}s " 116094332d3Sopenharmony_ci "vendorName: %{public}s chipName: %{public}s attrSet.devName: %s " 117094332d3Sopenharmony_ci "attrSet.id.busType: %{public}u attrSet.id.vendor: %{public}u " 118094332d3Sopenharmony_ci "attrSet.id.product: %{public}u attrSet.id.version: %{public}u ", 119094332d3Sopenharmony_ci in.detailInfo.devIndex, in.detailInfo.devType, 120094332d3Sopenharmony_ci in.detailInfo.chipInfo, in.detailInfo.vendorName, 121094332d3Sopenharmony_ci in.detailInfo.chipName, in.detailInfo.attrSet.devName, 122094332d3Sopenharmony_ci in.detailInfo.attrSet.id.busType, in.detailInfo.attrSet.id.vendor, 123094332d3Sopenharmony_ci in.detailInfo.attrSet.id.product, in.detailInfo.attrSet.id.version); 124094332d3Sopenharmony_ci for (int32_t i = 0; i < ABS_CNT; i++) { 125094332d3Sopenharmony_ci HDF_LOGD("attrSet.axisInfo.axis: %{public}d attrSet.axisInfo.min: %{public}d " 126094332d3Sopenharmony_ci "attrSet.axisInfo.max: %{public}d attrSet.axisInfo.fuzz: %{public}d " 127094332d3Sopenharmony_ci "attrSet.axisInfo.flat: %{public}d attrSet.axisInfo.range: %{public}d ", 128094332d3Sopenharmony_ci in.detailInfo.attrSet.axisInfo[i].axis, in.detailInfo.attrSet.axisInfo[i].flat, 129094332d3Sopenharmony_ci in.detailInfo.attrSet.axisInfo[i].fuzz, in.detailInfo.attrSet.axisInfo[i].max, 130094332d3Sopenharmony_ci in.detailInfo.attrSet.axisInfo[i].min, in.detailInfo.attrSet.axisInfo[i].range); 131094332d3Sopenharmony_ci } 132094332d3Sopenharmony_ci } 133094332d3Sopenharmony_ciprivate: 134094332d3Sopenharmony_ci void DeleteDevListNode(int index); 135094332d3Sopenharmony_ci int32_t AddDeviceNodeToList( 136094332d3Sopenharmony_ci int32_t &epollFd, int32_t &fd, string devPath, std::shared_ptr<InputDeviceInfo> &detailInfo); 137094332d3Sopenharmony_ci 138094332d3Sopenharmony_ci mutable std::mutex lock_; 139094332d3Sopenharmony_ci std::mutex reportEventPkgCallBackLock_; 140094332d3Sopenharmony_ci std::map<uint32_t, InputDevListNode> inputDevList_; 141094332d3Sopenharmony_ci struct epoll_event epollEventList_[EPOLL_MAX_EVENTS] {}; 142094332d3Sopenharmony_ci std::string devPath_ {"/dev/input"}; 143094332d3Sopenharmony_ci uint32_t devIndex_ {1}; 144094332d3Sopenharmony_ci std::map<int32_t, InputEventCb*> reportEventPkgCallback_; 145094332d3Sopenharmony_ci InputHostCb *reportHotPlugEventCallback_ = nullptr; 146094332d3Sopenharmony_ci std::thread thread_ {}; 147094332d3Sopenharmony_ci int32_t mEpollId_ {0}; 148094332d3Sopenharmony_ci int32_t mInotifyId_ {0}; 149094332d3Sopenharmony_ci}; 150094332d3Sopenharmony_ci} 151094332d3Sopenharmony_ci} 152094332d3Sopenharmony_ci#endif // INPUT_DEVICE_MANAGER_H 153