1 /*
2 * Copyright (C) 2012 Invensense, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <fcntl.h>
18 #include <errno.h>
19 #include <math.h>
20 #include <poll.h>
21 #include <dirent.h>
22 #include <sys/select.h>
23 #include <cutils/log.h>
24 #include <linux/input.h>
25
26 #include "SensorBase.h"
27 #include "local_log_def.h"
28
29 /*****************************************************************************/
30
SensorBase(const char * dev_name,const char * data_name)31 SensorBase::SensorBase(const char* dev_name,
32 const char* data_name) : dev_name(dev_name),
33 data_name(data_name),
34 dev_fd(-1),
35 data_fd(-1)
36 {
37 if (data_name) {
38 data_fd = openInput(data_name);
39 }
40 }
41
~SensorBase()42 SensorBase::~SensorBase()
43 {
44 if (data_fd >= 0) {
45 close(data_fd);
46 }
47 if (dev_fd >= 0) {
48 close(dev_fd);
49 }
50 }
51
open_device()52 int SensorBase::open_device()
53 {
54 if (dev_fd<0 && dev_name) {
55 dev_fd = open(dev_name, O_RDONLY);
56 LOGE_IF(dev_fd<0, "Couldn't open %s (%s)", dev_name, strerror(errno));
57 }
58 return 0;
59 }
60
close_device()61 int SensorBase::close_device()
62 {
63 if (dev_fd >= 0) {
64 close(dev_fd);
65 dev_fd = -1;
66 }
67 return 0;
68 }
69
getFd() const70 int SensorBase::getFd() const
71 {
72 if (!data_name) {
73 return dev_fd;
74 }
75 return data_fd;
76 }
77
setDelay(int32_t,int64_t)78 int SensorBase::setDelay(int32_t /*handle*/, int64_t /*ns*/)
79 {
80 return 0;
81 }
82
hasPendingEvents() const83 bool SensorBase::hasPendingEvents() const
84 {
85 return false;
86 }
87
openInput(const char * inputName)88 int SensorBase::openInput(const char *inputName)
89 {
90 int fd = -1;
91 const char *dirname = "/dev/input";
92 char devname[PATH_MAX];
93 char *filename;
94 DIR *dir;
95 struct dirent *de;
96 dir = opendir(dirname);
97 if(dir == NULL)
98 return -1;
99 strcpy(devname, dirname);
100 filename = devname + strlen(devname);
101 *filename++ = '/';
102 while((de = readdir(dir))) {
103 if(de->d_name[0] == '.' &&
104 (de->d_name[1] == '\0' ||
105 (de->d_name[1] == '.' && de->d_name[2] == '\0')))
106 continue;
107 strcpy(filename, de->d_name);
108 fd = open(devname, O_RDONLY);
109 LOGV_IF(EXTRA_VERBOSE, "path open %s", devname);
110 LOGI("path open %s", devname);
111 if (fd >= 0) {
112 char name[80];
113 if (ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {
114 name[0] = '\0';
115 }
116 LOGV_IF(EXTRA_VERBOSE, "name read %s", name);
117 if (!strcmp(name, inputName)) {
118 strcpy(input_name, filename);
119 break;
120 } else {
121 close(fd);
122 fd = -1;
123 }
124 }
125 }
126 closedir(dir);
127 LOGE_IF(fd < 0, "couldn't find '%s' input device", inputName);
128 return fd;
129 }
130
enable(int32_t,int)131 int SensorBase::enable(int32_t /*handle*/, int /*enabled*/)
132 {
133 return 0;
134 }
135