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