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