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 <unistd.h>
22 #include <dirent.h>
23 #include <sys/select.h>
24 #include <cutils/log.h>
25 #include <linux/input.h>
26
27 #include "SensorBase.h"
28 #include "local_log_def.h"
29
30 /*****************************************************************************/
31
SensorBase(const char * dev_name,const char * data_name)32 SensorBase::SensorBase(const char* dev_name,
33 const char* data_name) : dev_name(dev_name),
34 data_name(data_name),
35 dev_fd(-1),
36 data_fd(-1)
37 {
38 if (data_name) {
39 data_fd = openInput(data_name);
40 }
41 }
42
~SensorBase()43 SensorBase::~SensorBase()
44 {
45 if (data_fd >= 0) {
46 close(data_fd);
47 }
48 if (dev_fd >= 0) {
49 close(dev_fd);
50 }
51 }
52
open_device()53 int SensorBase::open_device()
54 {
55 if (dev_fd<0 && dev_name) {
56 dev_fd = open(dev_name, O_RDONLY);
57 LOGE_IF(dev_fd<0, "Couldn't open %s (%s)", dev_name, strerror(errno));
58 }
59 return 0;
60 }
61
close_device()62 int SensorBase::close_device()
63 {
64 if (dev_fd >= 0) {
65 close(dev_fd);
66 dev_fd = -1;
67 }
68 return 0;
69 }
70
getFd() const71 int SensorBase::getFd() const
72 {
73 if (!data_name) {
74 return dev_fd;
75 }
76 return data_fd;
77 }
78
setDelay(int32_t handle,int64_t ns)79 int SensorBase::setDelay(int32_t handle, int64_t ns)
80 {
81 return 0;
82 }
83
hasPendingEvents() const84 bool SensorBase::hasPendingEvents() const
85 {
86 return false;
87 }
88
getTimestamp()89 int64_t SensorBase::getTimestamp()
90 {
91 struct timespec t;
92 t.tv_sec = t.tv_nsec = 0;
93 clock_gettime(CLOCK_MONOTONIC, &t);
94 return int64_t(t.tv_sec) * 1000000000LL + t.tv_nsec;
95 }
96
openInput(const char * inputName)97 int SensorBase::openInput(const char *inputName)
98 {
99 int fd = -1;
100 const char *dirname = "/dev/input";
101 char devname[PATH_MAX];
102 char *filename;
103 DIR *dir;
104 struct dirent *de;
105 dir = opendir(dirname);
106 if(dir == NULL)
107 return -1;
108 strcpy(devname, dirname);
109 filename = devname + strlen(devname);
110 *filename++ = '/';
111 while((de = readdir(dir))) {
112 if(de->d_name[0] == '.' &&
113 (de->d_name[1] == '\0' ||
114 (de->d_name[1] == '.' && de->d_name[2] == '\0')))
115 continue;
116 strcpy(filename, de->d_name);
117 fd = open(devname, O_RDONLY);
118 LOGV_IF(EXTRA_VERBOSE, "path open %s", devname);
119 LOGI("path open %s", devname);
120 if (fd >= 0) {
121 char name[80];
122 if (ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {
123 name[0] = '\0';
124 }
125 LOGV_IF(EXTRA_VERBOSE, "name read %s", name);
126 if (!strcmp(name, inputName)) {
127 strcpy(input_name, filename);
128 break;
129 } else {
130 close(fd);
131 fd = -1;
132 }
133 }
134 }
135 closedir(dir);
136 LOGE_IF(fd < 0, "couldn't find '%s' input device", inputName);
137 return fd;
138 }
139
enable(int32_t handle,int enabled)140 int SensorBase::enable(int32_t handle, int enabled)
141 {
142 return 0;
143 }
144