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