• 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 #include "input_device_manager.h"
17 #include <algorithm>
18 #include <cstdio>
19 #include <cstdlib>
20 #include <cstring>
21 #include <dirent.h>
22 #include <fcntl.h>
23 #include <fstream>
24 #include <functional>
25 #include <future>
26 #include <iostream>
27 #include <memory>
28 #include <sstream>
29 #include <sys/epoll.h>
30 #include <sys/inotify.h>
31 #include <sys/ioctl.h>
32 #include <unistd.h>
33 #include <vector>
34 #include "hdf_log.h"
35 #include "osal_mem.h"
36 #include "securec.h"
37 
38 #define HDF_LOG_TAG InputDeviceHdiManager
39 
40 namespace OHOS {
41 namespace Input {
42 using namespace std;
Init()43 void InputDeviceManager::Init()
44 {
45     inputDevList_.clear();
46     reportEventPkgCallback_.clear();
47     GetInputDeviceInfoList();
48     std::thread t1(std::bind(&InputDeviceManager::WorkerThread, this));
49     std::string wholeName1 = std::to_string(getpid()) + "_" + std::to_string(gettid());
50     thread_ = std::move(t1);
51     thread_.detach();
52     for (auto &inputDev : inputDevList_) {
53         dumpInfoList(inputDev.second);
54     }
55 }
56 
FreeEventPkgs(InputEventPackage ** eventPkgs,size_t count)57 static void FreeEventPkgs(InputEventPackage **eventPkgs, size_t count)
58 {
59     for (size_t i = 0; i < count; i++) {
60         if (eventPkgs[i] != NULL) {
61             free(eventPkgs[i]);
62             eventPkgs[i] = nullptr;
63         }
64     }
65     return;
66 }
67 
68 // get the nodefile list
GetFiles(string path)69 vector<string> InputDeviceManager::GetFiles(string path)
70 {
71     vector<string> fileList;
72     struct dirent *dEnt = nullptr;
73 
74     DIR *dir = opendir(path.c_str());
75     if (dir == nullptr) {
76         HDF_LOGE("%{public}s: no files", __func__);
77         return fileList;
78     }
79     string sDot = ".";
80     string sDotDot = "..";
81     while ((dEnt = readdir(dir)) != nullptr) {
82         if ((string(dEnt->d_name) != sDot) &&
83             (string(dEnt->d_name) != sDotDot)) {
84             if (dEnt->d_type != DT_DIR) {
85                 string d_name(dEnt->d_name);
86                 fileList.push_back(string(dEnt->d_name));
87             }
88         }
89     }
90     // sort the returned files
91     sort(fileList.begin(), fileList.end());
92     closedir(dir);
93     return fileList;
94 }
95 
96 // read action
DoRead(int32_t fd,struct input_event * event,size_t size)97 void InputDeviceManager::DoRead(int32_t fd, struct input_event *event, size_t size)
98 {
99     int32_t readLen = read(fd, event, sizeof(struct input_event) * size);
100 
101     if (readLen == 0 || (readLen < 0 && errno == ENODEV)) {
102         return;
103     } else if (readLen < 0) {
104         if (errno != EAGAIN && errno != EINTR) {
105             HDF_LOGE("%{public}s: could not get event (errno = %{public}d)", __func__, errno);
106         }
107     } else if ((readLen % sizeof(struct input_event)) != 0) {
108         HDF_LOGE("%{public}s: could not get one event size %{public}lu  readLen size: %{public}d", __func__,
109             sizeof(struct input_event), readLen);
110     } else {
111         size_t count = size_t(readLen) / sizeof(struct input_event);
112         InputEventPackage **evtPkg = (InputEventPackage **)OsalMemAlloc(sizeof(InputEventPackage *) * count);
113         if (evtPkg == nullptr) {
114             HDF_LOGE("%{public}s: OsalMemAlloc failed, line: %{public}d", __func__, __LINE__);
115             return;
116         }
117         for (size_t i = 0; i < count; i++) {
118             struct input_event &iEvent = event[i];
119             // device action events happened
120             *(evtPkg + i) = (InputEventPackage *)OsalMemAlloc(sizeof(InputEventPackage));
121             if (evtPkg[i] == nullptr) {
122                 HDF_LOGE("%{public}s: OsalMemAlloc failed, line: %{public}d", __func__, __LINE__);
123                 FreeEventPkgs(evtPkg, i);
124                 free(evtPkg);
125                 evtPkg = nullptr;
126                 return;
127             }
128             evtPkg[i]->type = iEvent.type;
129             evtPkg[i]->code = iEvent.code;
130             evtPkg[i]->value = iEvent.value;
131             evtPkg[i]->timestamp = iEvent.time.tv_sec * MS_THOUSAND * MS_THOUSAND + iEvent.time.tv_usec;
132         }
133         for (auto &callbackFunc : reportEventPkgCallback_) {
134             uint32_t index {0};
135             auto ret = FindIndexFromFd(fd, &index);
136             if (callbackFunc.second != nullptr && ret != INPUT_FAILURE) {
137                 callbackFunc.second->EventPkgCallback(const_cast<const InputEventPackage **>(evtPkg), count, index);
138             }
139         }
140         for (size_t i = 0; i < count; i++) {
141             OsalMemFree(evtPkg[i]);
142             evtPkg[i] = nullptr;
143         }
144         OsalMemFree(evtPkg);
145         evtPkg = nullptr;
146     }
147 }
148 
149 // open input device node
OpenInputDevice(string devPath)150 int32_t InputDeviceManager::OpenInputDevice(string devPath)
151 {
152     char devRealPath[PATH_MAX + 1] = { '\0' };
153     if (realpath(devPath.c_str(), devRealPath) == nullptr) {
154         HDF_LOGE("%{public}s: The absolute path does not exist.", __func__);
155         return INPUT_FAILURE;
156     }
157 
158     int32_t nodeFd = open(devRealPath, O_RDWR | O_CLOEXEC | O_NONBLOCK);
159     if (nodeFd < 0) {
160         HDF_LOGE("%{public}s: could not open %{public}s, %{public}d %{public}s", __func__,
161             devRealPath, errno, strerror(errno));
162         return INPUT_FAILURE;
163     }
164     return nodeFd;
165 }
166 
167 // close input device node
CloseInputDevice(string devPath)168 RetStatus InputDeviceManager::CloseInputDevice(string devPath)
169 {
170     for (auto &inputDev : inputDevList_) {
171         if (string(inputDev.second.devPathNode) == devPath) {
172             int32_t fd = inputDev.second.fd;
173             if (fd > 0) {
174                 RemoveEpoll(mEpollId_, fd);
175                 close(fd);
176                 fd = -1;
177                 inputDev.second.status = INPUT_DEVICE_STATUS_CLOSED;
178                 return INPUT_SUCCESS;
179             }
180         }
181     }
182     // device list remove this node
183     return INPUT_FAILURE;
184 }
185 
GetInputDeviceInfo(int32_t fd,InputDeviceInfo * detailInfo)186 int32_t InputDeviceManager::GetInputDeviceInfo(int32_t fd, InputDeviceInfo *detailInfo)
187 {
188     char buffer[DEVICE_INFO_SIZE] {};
189     struct input_id inputId {};
190     // get the abilitys.
191     (void)ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(detailInfo->abilitySet.keyCode)), &detailInfo->abilitySet.keyCode);
192     (void)ioctl(fd, EVIOCGBIT(EV_REL, sizeof(detailInfo->abilitySet.relCode)), &detailInfo->abilitySet.relCode);
193     (void)ioctl(fd, EVIOCGBIT(EV_ABS, sizeof(detailInfo->abilitySet.absCode)), &detailInfo->abilitySet.absCode);
194     (void)ioctl(fd, EVIOCGBIT(EV_MSC, sizeof(detailInfo->abilitySet.miscCode)), &detailInfo->abilitySet.miscCode);
195     (void)ioctl(fd, EVIOCGBIT(EV_SW, sizeof(detailInfo->abilitySet.switchCode)), &detailInfo->abilitySet.switchCode);
196     (void)ioctl(fd, EVIOCGBIT(EV_LED, sizeof(detailInfo->abilitySet.ledType)), &detailInfo->abilitySet.ledType);
197     (void)ioctl(fd, EVIOCGBIT(EV_SND, sizeof(detailInfo->abilitySet.soundCode)), &detailInfo->abilitySet.soundCode);
198     (void)ioctl(fd, EVIOCGBIT(EV_FF, sizeof(detailInfo->abilitySet.forceCode)), &detailInfo->abilitySet.forceCode);
199     // device name.
200     if (ioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &buffer) < 1) {
201         HDF_LOGE("%{public}s: get device name failed errormsg %{public}s", __func__, strerror(errno));
202     } else {
203         buffer[sizeof(buffer) - 1] = '\0';
204         int32_t ret = strcpy_s(detailInfo->attrSet.devName, DEVICE_INFO_SIZE, buffer);
205         if (ret) {
206             HDF_LOGE("%{public}s: strcpy_s failed, ret %{public}d", __func__, ret);
207         }
208     }
209     // device detailInfo.
210     if (ioctl(fd, EVIOCGID, &inputId)) {
211         HDF_LOGE("%{public}s: get device input id errormsg %{public}s", __func__, strerror(errno));
212     }
213     detailInfo->attrSet.id.busType = inputId.bustype;
214     detailInfo->attrSet.id.product = inputId.product;
215     detailInfo->attrSet.id.vendor = inputId.vendor;
216     detailInfo->attrSet.id.version = inputId.version;
217     // ABS Info
218     for (uint32_t i = 0; i < ABS_CNT; i++) {
219         if (detailInfo->abilitySet.absCode[i] > 0) {
220             if (ioctl(fd, EVIOCGABS(i), &detailInfo->attrSet.axisInfo[i])) {
221                 HDF_LOGE("%{public}s: get axis info failed fd = %{public}d name = %{public}s errormsg = %{public}s",
222                          __func__, fd, detailInfo->attrSet.devName, strerror(errno));
223                 continue;
224             }
225         }
226     }
227     return INPUT_SUCCESS;
228 }
229 
GetInputDeviceInfoList(int32_t epollFd)230 void InputDeviceManager::GetInputDeviceInfoList(int32_t epollFd)
231 {
232     inputDevList_.clear();
233     std::vector<std::string> flist = GetFiles(devPath_);
234     std::shared_ptr<InputDeviceInfo> detailInfo;
235     InputDevListNode inputDevList {};
236     uint32_t type {INDEV_TYPE_UNKNOWN};
237 
238     for (unsigned i = 0; i < flist.size(); i++) {
239         string devPathNode = devPath_ + "/" + flist[i];
240         std::string::size_type n = devPathNode.find("event");
241         if (n != std::string::npos) {
242             auto fd = OpenInputDevice(devPathNode);
243             if (fd < 0) {
244                 HDF_LOGE("%{public}s: open node failed", __func__);
245                 continue;
246             }
247             detailInfo = std::make_shared<InputDeviceInfo>();
248             (void)memset_s(detailInfo.get(), sizeof(InputDeviceInfo), 0, sizeof(InputDeviceInfo));
249             (void)GetInputDeviceInfo(fd, detailInfo.get());
250             auto sDevName = string(detailInfo->attrSet.devName);
251             if (sDevName.find("input_mt_wrapper") != std::string::npos) {
252                 type = INDEV_TYPE_TOUCH;
253             } else if ((sDevName.find("Keyboard") != std::string::npos) &&
254                        (sDevName.find("Headset") == std::string::npos)) {
255                 type = INDEV_TYPE_KEYBOARD;
256             } else if (sDevName.find("Mouse") != std::string::npos) {
257                 type = INDEV_TYPE_MOUSE;
258             } else if ((sDevName.find("_gpio_key") != std::string::npos) ||
259                 (sDevName.find("ponkey_on") != std::string::npos)) {
260                 type = INDEV_TYPE_KEY;
261             } else {
262                 continue;
263             }
264             if (type != INDEV_TYPE_UNKNOWN) {
265                 inputDevList.index = devIndex_;
266                 inputDevList.status = INPUT_DEVICE_STATUS_OPENED;
267                 inputDevList.fd = fd;
268                 detailInfo->devIndex = devIndex_;
269                 detailInfo->devType = type;
270                 (void)memcpy_s(&inputDevList.devPathNode, devPathNode.length(),
271                     devPathNode.c_str(), devPathNode.length());
272                 (void)memcpy_s(&inputDevList.detailInfo, sizeof(InputDeviceInfo), detailInfo.get(),
273                     sizeof(InputDeviceInfo));
274                 inputDevList_.insert_or_assign(devIndex_, inputDevList);
275                 devIndex_ += 1;
276             }
277         }
278     }
279 }
280 
DoInputDeviceAction(void)281 int32_t InputDeviceManager::DoInputDeviceAction(void)
282 {
283     struct input_event evtBuffer[EVENT_BUFFER_SIZE] {};
284     int32_t result = 0;
285 
286     mEpollId_ = epoll_create1(EPOLL_CLOEXEC);
287     if (mEpollId_ == INPUT_FAILURE) {
288         HDF_LOGE("%{public}s: epoll create failed", __func__);
289         return mEpollId_;
290     }
291     mInotifyId_ = inotify_init();
292     result = inotify_add_watch(mInotifyId_, devPath_.c_str(), IN_DELETE | IN_CREATE);
293     if (result == INPUT_FAILURE) {
294         HDF_LOGE("%{public}s: add file watch failed", __func__);
295         return result;
296     }
297     AddToEpoll(mEpollId_, mInotifyId_);
298     while (true) {
299         result = epoll_wait(mEpollId_, epollEventList_, EPOLL_MAX_EVENTS, EPOLL_WAIT_TIMEOUT);
300         if (result <= 0) {
301             continue;
302         }
303         for (int32_t i = 0; i < result; i++) {
304             if (epollEventList_[i].data.fd != mInotifyId_) {
305                 DoRead(epollEventList_[i].data.fd, evtBuffer, EVENT_BUFFER_SIZE);
306                 continue;
307             }
308             if (INPUT_FAILURE == InotifyEventHandler(mEpollId_, mInotifyId_)) {
309                 HDF_LOGE("%{public}s: inotify handler failed", __func__);
310                 return INPUT_FAILURE;
311             }
312         }
313     }
314     return INPUT_SUCCESS;
315 }
316 
DoWithEventDeviceAdd(int32_t & epollFd,int32_t & fd,string devPath)317 void InputDeviceManager::DoWithEventDeviceAdd(int32_t &epollFd, int32_t &fd, string devPath)
318 {
319     bool findDeviceFlag = false;
320     uint32_t type {};
321     uint32_t index {};
322     uint32_t status {};
323 
324     std::shared_ptr<InputDeviceInfo> detailInfo = std::make_shared<InputDeviceInfo>();
325     (void)memset_s(detailInfo.get(), sizeof(InputDeviceInfo), 0, sizeof(InputDeviceInfo));
326     (void)GetInputDeviceInfo(fd, detailInfo.get());
327     auto sDevName = string(detailInfo->attrSet.devName);
328     for (auto it = inputDevList_.begin(); it != inputDevList_.end();) {
329         if (string(it->second.detailInfo.attrSet.devName) == sDevName) {
330             it->second.fd = fd;
331             it->second.status = INPUT_DEVICE_STATUS_OPENED;
332             findDeviceFlag = true;
333             index = it->first;
334             break;
335         } else {
336             ++it;
337         }
338     }
339     if (sDevName.find("Keyboard") != std::string::npos) {
340         detailInfo->devType = INDEV_TYPE_KEYBOARD;
341     }
342     if (sDevName.find("Mouse") != std::string::npos) {
343         detailInfo->devType = INDEV_TYPE_MOUSE;
344     }
345     type = detailInfo->devType;
346     if (!findDeviceFlag) {
347         InputDevListNode inputDevList {};
348         index = devIndex_;
349         inputDevList.index = devIndex_;
350         inputDevList.status = INPUT_DEVICE_STATUS_OPENED;
351         inputDevList.fd = fd;
352         detailInfo->devIndex = devIndex_;
353         (void)memcpy_s(inputDevList.devPathNode, devPath.length(), devPath.c_str(), devPath.length());
354         (void)memcpy_s(&inputDevList.detailInfo, sizeof(InputDeviceInfo), detailInfo.get(), sizeof(InputDeviceInfo));
355         inputDevList_.insert_or_assign(devIndex_, inputDevList);
356     }
357     status = INPUT_DEVICE_STATUS_OPENED;
358     SendHotPlugEvent(type, index, status);
359     if (!findDeviceFlag) {
360         devIndex_ += 1;
361     }
362 }
363 
SendHotPlugEvent(uint32_t & type,uint32_t & index,uint32_t status)364 void InputDeviceManager::SendHotPlugEvent(uint32_t &type, uint32_t &index, uint32_t status)
365 {
366     // hot plug evnets happened
367     InputHotPlugEvent *evtPlusPkg = (InputHotPlugEvent *)OsalMemAlloc(sizeof(InputHotPlugEvent));
368     if (evtPlusPkg == nullptr) {
369         HDF_LOGE("%{public}s: OsalMemAlloc failed", __func__);
370         return;
371     }
372 
373     evtPlusPkg->devType = type;
374     evtPlusPkg->devIndex = index;
375     evtPlusPkg->status = status;
376 
377     if (reportHotPlugEventCallback_ != nullptr) {
378         HDF_LOGD("devType: %{public}u devIndex: %{public}u status: %{public}u", type, index, status);
379         reportHotPlugEventCallback_->HotPlugCallback(evtPlusPkg);
380     }
381 
382     OsalMemFree(evtPlusPkg);
383     evtPlusPkg = nullptr;
384 }
385 
DoWithEventDeviceDel(int32_t & epollFd,uint32_t & index)386 void InputDeviceManager::DoWithEventDeviceDel(int32_t &epollFd, uint32_t &index)
387 {
388     uint32_t type {};
389     uint32_t devIndex {};
390     uint32_t status {};
391 
392     CloseInputDevice(inputDevList_[index].devPathNode);
393     RemoveEpoll(epollFd, inputDevList_[index].fd);
394     HDF_LOGD("%{public}s: index: %{public}d fd: %{public}d devName: %{public}s", __func__,
395              devIndex_, inputDevList_[index].fd, inputDevList_[index].detailInfo.attrSet.devName);
396 
397     // hot plug evnets happened
398     auto sDevName = string(inputDevList_[index].detailInfo.attrSet.devName);
399     if (sDevName.find("Keyboard") != std::string::npos) {
400         type = INDEV_TYPE_KEYBOARD;
401     }
402     if (sDevName.find("Mouse") != std::string::npos) {
403         type = INDEV_TYPE_MOUSE;
404     }
405     auto ret = FindIndexFromDevName(sDevName, &devIndex);
406     if (ret != INPUT_SUCCESS) {
407         HDF_LOGE("%{public}s: no found device maybe it has been removed", __func__);
408         SendHotPlugEvent(type, devIndex_, status);
409         return;
410     }
411     status = INPUT_DEVICE_STATUS_CLOSED;
412     SendHotPlugEvent(type, devIndex, status);
413     for (auto it = inputDevList_.begin(); it != inputDevList_.end();) {
414         if (it->first == devIndex_) {
415             it->second.fd = 0;
416             it->second.status = INPUT_DEVICE_STATUS_CLOSED;
417         } else {
418             ++it;
419         }
420     }
421 }
422 
InotifyEventHandler(int32_t epollFd,int32_t notifyFd)423 int32_t InputDeviceManager::InotifyEventHandler(int32_t epollFd, int32_t notifyFd)
424 {
425     char InfoBuf[BUFFER_SIZE];
426     struct inotify_event *event {};
427     char nodeRealPath[PATH_MAX + 1] = { '\0' };
428     char *p {};
429     int32_t tmpFd {};
430 
431     (void)memset_s(InfoBuf, BUFFER_SIZE, 0, BUFFER_SIZE);
432     int32_t result = read(notifyFd, InfoBuf, BUFFER_SIZE);
433     for (p = InfoBuf; p < InfoBuf + result;) {
434         event = (struct inotify_event *)(p);
435         auto nodePath = devPath_ + "/" + string(event->name);
436         if (realpath(nodePath.c_str(), nodeRealPath) == nullptr) {
437             HDF_LOGE("%{public}s: The absolute path does not exist.", __func__);
438             return INPUT_FAILURE;
439         }
440         if (event->mask & IN_CREATE) {
441             tmpFd = open(nodeRealPath, O_RDWR);
442             if (tmpFd == INPUT_FAILURE) {
443                 HDF_LOGE("%{public}s: open file failure: %{public}s", __func__, nodeRealPath);
444                 return INPUT_FAILURE;
445             }
446             if (nodePath.find("event") == std::string::npos) {
447                 break;
448             }
449             DoWithEventDeviceAdd(epollFd, tmpFd, nodePath);
450         } else if (event->mask & IN_DELETE) {
451             for (auto &inputDev : inputDevList_) {
452                 if (!strcmp(inputDev.second.devPathNode, nodePath.c_str())) {
453                     DoWithEventDeviceDel(epollFd, inputDev.second.index);
454                     break;
455                 }
456             }
457         } else {
458             // do nothing
459             HDF_LOGI("%{public}s: others actions has done", __func__);
460         }
461         p += sizeof(struct inotify_event) + event->len;
462     }
463     return 0;
464 }
465 
AddToEpoll(int32_t epollFd,int32_t fileFd)466 int32_t InputDeviceManager::AddToEpoll(int32_t epollFd, int32_t fileFd)
467 {
468     int32_t result {0};
469     struct epoll_event eventItem {};
470 
471     (void)memset_s(&eventItem, sizeof(eventItem), 0, sizeof(eventItem));
472     eventItem.events = EPOLLIN;
473     eventItem.data.fd = fileFd;
474     result = epoll_ctl(epollFd, EPOLL_CTL_ADD, fileFd, &eventItem);
475     return result;
476 }
RemoveEpoll(int32_t epollFd,int32_t fileFd)477 void InputDeviceManager::RemoveEpoll(int32_t epollFd, int32_t fileFd)
478 {
479     epoll_ctl(epollFd, EPOLL_CTL_DEL, fileFd, nullptr);
480 }
481 
FindIndexFromFd(int32_t & fd,uint32_t * index)482 int32_t InputDeviceManager::FindIndexFromFd(int32_t &fd, uint32_t *index)
483 {
484     std::lock_guard<std::mutex> guard(lock_);
485     for (auto &inputDev : inputDevList_) {
486         if (fd == inputDev.second.fd) {
487             *index = inputDev.first;
488             return INPUT_SUCCESS;
489         }
490     }
491     return INPUT_FAILURE;
492 }
493 
FindIndexFromDevName(string devName,uint32_t * index)494 int32_t InputDeviceManager::FindIndexFromDevName(string devName, uint32_t *index)
495 {
496     std::lock_guard<std::mutex> guard(lock_);
497     for (auto &inputDev : inputDevList_) {
498         if (!strcmp(devName.c_str(), inputDev.second.detailInfo.attrSet.devName)) {
499             *index =  inputDev.first;
500             return INPUT_SUCCESS;
501         }
502     }
503     return INPUT_FAILURE;
504 }
505 
506 // InputManager
ScanDevice(InputDevDesc * staArr,uint32_t arrLen)507 RetStatus InputDeviceManager::ScanDevice(InputDevDesc *staArr, uint32_t arrLen)
508 {
509     if (staArr == nullptr) {
510         HDF_LOGE("%{public}s: param is null", __func__);
511         return INPUT_NULL_PTR;
512     }
513 
514     auto scanCount = (arrLen >= inputDevList_.size() ? inputDevList_.size() : arrLen);
515     if (inputDevList_.size() == 0) {
516         HDF_LOGE("%{public}s: inputDevList_.size is 0", __func__);
517         return INPUT_FAILURE;
518     }
519 
520     for (size_t i = 0; i <= scanCount; i++) {
521         (staArr + i)->devIndex = inputDevList_[i].index;
522         (staArr + i)->devType = inputDevList_[i].detailInfo.devType;
523     }
524 
525     return INPUT_SUCCESS;
526 }
527 
OpenDevice(uint32_t deviceIndex)528 RetStatus InputDeviceManager::OpenDevice(uint32_t deviceIndex)
529 {
530     std::lock_guard<std::mutex> guard(lock_);
531     auto ret = INPUT_FAILURE;
532 
533     if (deviceIndex > MAX_SUPPORT_DEVS) {
534         HDF_LOGE("%{public}s: param is wrong", __func__);
535         return ret;
536     }
537     auto searchIndex = inputDevList_.find(deviceIndex);
538     if (searchIndex != inputDevList_.end()) {
539         if (searchIndex->second.status != INPUT_DEVICE_STATUS_OPENED) {
540             auto openRet = OpenInputDevice(searchIndex->second.devPathNode);
541             if (openRet > 0) {
542                 AddToEpoll(mEpollId_, openRet);
543                 ret = INPUT_SUCCESS;
544             } else {
545                 HDF_LOGE("%{public}s: open error: %{public}d errormsg: %{public}s",
546                          __func__, openRet, strerror(errno));
547                 return ret;
548             }
549             searchIndex->second.fd = openRet;
550         } else {
551             HDF_LOGD("%{public}s: open devPathNoth: %{public}s fd: %{public}d",
552                      __func__, searchIndex->second.devPathNode, searchIndex->second.fd);
553             HDF_LOGD("%{public}s: open devPathNoth: %{public}s status: %{public}d index: %{public}d",
554                      __func__, searchIndex->second.devPathNode, searchIndex->second.status, searchIndex->first);
555             AddToEpoll(mEpollId_, searchIndex->second.fd);
556             ret = INPUT_SUCCESS;
557         }
558     }
559     for (auto &e : inputDevList_) {
560         dumpInfoList(e.second);
561     }
562     return ret;
563 }
564 
CloseDevice(uint32_t deviceIndex)565 RetStatus InputDeviceManager::CloseDevice(uint32_t deviceIndex)
566 {
567     std::lock_guard<std::mutex> guard(lock_);
568     auto ret = INPUT_FAILURE;
569 
570     if (deviceIndex > MAX_SUPPORT_DEVS) {
571         HDF_LOGE("%{public}s: param is wrong", __func__);
572         return ret;
573     }
574     auto searchIndex = inputDevList_.find(deviceIndex);
575     if (searchIndex != inputDevList_.end()) {
576         ret = CloseInputDevice(searchIndex->second.devPathNode);
577     }
578     HDF_LOGD("%{public}s: close devIndex: %{public}u ret: %{public}d inputDevList_ size:%{public}lu ",
579              __func__, deviceIndex, ret, inputDevList_.size());
580     return ret;
581 }
582 
GetDevice(int32_t deviceIndex,InputDeviceInfo ** devInfo)583 int32_t InputDeviceManager::GetDevice(int32_t deviceIndex, InputDeviceInfo **devInfo)
584 {
585     std::lock_guard<std::mutex> guard(lock_);
586     auto ret = INPUT_FAILURE;
587     std::shared_ptr<InputDeviceInfo> detailInfo;
588 
589     if (devInfo == nullptr || deviceIndex > MAX_SUPPORT_DEVS) {
590         HDF_LOGE("%{public}s: param is wrong", __func__);
591         return INPUT_FAILURE;
592     }
593     for (size_t i = 0; i <= inputDevList_.size(); i++) {
594         auto it = inputDevList_.find(deviceIndex);
595         if (it != inputDevList_.end()) {
596             detailInfo = std::make_shared<InputDeviceInfo>();
597             (void)memset_s(detailInfo.get(), sizeof(InputDeviceInfo), 0, sizeof(InputDeviceInfo));
598             (void)memcpy_s(detailInfo.get(), sizeof(InputDeviceInfo), &it->second.detailInfo, sizeof(InputDeviceInfo));
599             *devInfo = detailInfo.get();
600             ret = INPUT_SUCCESS;
601         } else {
602             continue;
603         }
604     }
605     HDF_LOGD("%{public}s: devIndex: %{public}d ret: %{public}d", __func__, deviceIndex, ret);
606     return ret;
607 }
608 
GetDeviceList(uint32_t * devNum,InputDeviceInfo ** deviceList,uint32_t size)609 int32_t InputDeviceManager::GetDeviceList(uint32_t *devNum, InputDeviceInfo **deviceList, uint32_t size)
610 {
611     std::lock_guard<std::mutex> guard(lock_);
612 
613     auto scanCount = (size >= inputDevList_.size() ? inputDevList_.size() : size);
614     if ((devNum == nullptr) || (deviceList == nullptr) || (*deviceList == nullptr)) {
615         HDF_LOGE("%{public}s: null pointer", __func__);
616         return INPUT_FAILURE;
617     }
618     if (inputDevList_.size() == 0) {
619         HDF_LOGE("%{public}s: inputDevList_ size is 0", __func__);
620         return INPUT_FAILURE;
621     }
622     for (size_t i = 0; i < scanCount; i++) {
623         (void)memcpy_s((*deviceList) + i, sizeof(InputDeviceInfo), &inputDevList_[i].detailInfo,
624             sizeof(InputDeviceInfo));
625     }
626     *devNum = inputDevList_.size();
627     HDF_LOGD("%{public}s: devNum: %{public}u devIndex_: %{public}d", __func__, *devNum, devIndex_);
628 
629     return INPUT_SUCCESS;
630 }
631 
632 // InputController
SetPowerStatus(uint32_t devIndex,uint32_t status)633 RetStatus InputDeviceManager::SetPowerStatus(uint32_t devIndex, uint32_t status)
634 {
635     RetStatus rc = INPUT_SUCCESS;
636     return rc;
637 }
638 
GetPowerStatus(uint32_t devIndex,uint32_t * status)639 RetStatus InputDeviceManager::GetPowerStatus(uint32_t devIndex, uint32_t *status)
640 {
641     RetStatus rc = INPUT_SUCCESS;
642     return rc;
643 }
644 
GetDeviceType(uint32_t devIndex,uint32_t * deviceType)645 RetStatus InputDeviceManager::GetDeviceType(uint32_t devIndex, uint32_t *deviceType)
646 {
647     RetStatus rc = INPUT_SUCCESS;
648 
649     *deviceType = inputDevList_[devIndex].detailInfo.devType;
650     HDF_LOGI("%{public}s: devType: %{public}u", __func__, *deviceType);
651     return rc;
652 }
653 
GetChipInfo(uint32_t devIndex,char * chipInfo,uint32_t length)654 RetStatus InputDeviceManager::GetChipInfo(uint32_t devIndex, char *chipInfo, uint32_t length)
655 {
656     RetStatus rc = INPUT_SUCCESS;
657 
658     (void)memcpy_s(chipInfo, length, inputDevList_[devIndex].detailInfo.chipInfo, length);
659     HDF_LOGI("%{public}s: chipInfo: %{public}s", __func__, chipInfo);
660     return rc;
661 }
662 
GetVendorName(uint32_t devIndex,char * vendorName,uint32_t length)663 RetStatus InputDeviceManager::GetVendorName(uint32_t devIndex, char *vendorName, uint32_t length)
664 {
665     RetStatus rc = INPUT_SUCCESS;
666 
667     (void)memcpy_s(vendorName, length, inputDevList_[devIndex].detailInfo.vendorName, length);
668     HDF_LOGI("%{public}s: vendorName: %{public}s", __func__, vendorName);
669     return rc;
670 }
671 
GetChipName(uint32_t devIndex,char * chipName,uint32_t length)672 RetStatus InputDeviceManager::GetChipName(uint32_t devIndex, char *chipName, uint32_t length)
673 {
674     RetStatus rc = INPUT_SUCCESS;
675 
676     (void)memcpy_s(chipName, length, inputDevList_[devIndex].detailInfo.chipName, length);
677     HDF_LOGI("%{public}s: chipName: %{public}s", __func__, chipName);
678     return rc;
679 }
680 
SetGestureMode(uint32_t devIndex,uint32_t gestureMode)681 RetStatus InputDeviceManager::SetGestureMode(uint32_t devIndex, uint32_t gestureMode)
682 {
683     RetStatus rc = INPUT_SUCCESS;
684     return rc;
685 }
686 
RunCapacitanceTest(uint32_t devIndex,uint32_t testType,char * result,uint32_t length)687 RetStatus InputDeviceManager::RunCapacitanceTest(uint32_t devIndex, uint32_t testType, char *result, uint32_t length)
688 {
689     RetStatus rc = INPUT_SUCCESS;
690     return rc;
691 }
692 
RunExtraCommand(uint32_t devIndex,InputExtraCmd * cmd)693 RetStatus InputDeviceManager::RunExtraCommand(uint32_t devIndex, InputExtraCmd *cmd)
694 {
695     RetStatus rc = INPUT_SUCCESS;
696     return rc;
697 }
698 
699 // InputReporter
RegisterReportCallback(uint32_t devIndex,InputEventCb * callback)700 RetStatus InputDeviceManager::RegisterReportCallback(uint32_t devIndex, InputEventCb *callback)
701 {
702     RetStatus rc = INPUT_SUCCESS;
703     if (devIndex > MAX_SUPPORT_DEVS) {
704         HDF_LOGE("%{public}s: param is wrong", __func__);
705         return INPUT_FAILURE;
706     }
707     reportEventPkgCallback_[devIndex] = callback;
708     return rc;
709 }
710 
UnregisterReportCallback(uint32_t devIndex)711 RetStatus InputDeviceManager::UnregisterReportCallback(uint32_t devIndex)
712 {
713     HDF_LOGI("%{public}s: %{public}d dev is unregister", __func__, devIndex);
714     RetStatus rc = INPUT_SUCCESS;
715     if (devIndex > MAX_SUPPORT_DEVS) {
716         HDF_LOGE("%{public}s: param is wrong", __func__);
717         return INPUT_FAILURE;
718     }
719     reportEventPkgCallback_[devIndex] = nullptr;
720     return rc;
721 }
722 
RegisterHotPlugCallback(InputHostCb * callback)723 RetStatus InputDeviceManager::RegisterHotPlugCallback(InputHostCb *callback)
724 {
725     RetStatus rc = INPUT_SUCCESS;
726     reportHotPlugEventCallback_ = callback;
727     HDF_LOGI("%{public}s: called line %{public}d ret %{public}d", __func__, __LINE__, rc);
728     return rc;
729 }
730 
UnregisterHotPlugCallback(void)731 RetStatus InputDeviceManager::UnregisterHotPlugCallback(void)
732 {
733     RetStatus rc = INPUT_SUCCESS;
734     reportHotPlugEventCallback_ = nullptr;
735     HDF_LOGI("%{public}s: called line %{public}d ret:%{public}d", __func__, __LINE__, rc);
736     return rc;
737 }
738 
WorkerThread()739 void InputDeviceManager::WorkerThread()
740 {
741     HDF_LOGI("%{public}s: called line %{public}d ", __func__, __LINE__);
742     std::future<void> fuResult = std::async(std::launch::async, [this]() {
743         DoInputDeviceAction();
744         return;
745     });
746     fuResult.get();
747 }
748 }
749 }
750