PageRenderTime 30ms CodeModel.GetById 9ms app.highlight 16ms RepoModel.GetById 2ms app.codeStats 0ms

/libsensors/SensorBase.cpp

http://github.com/CyanogenMod/android_device_zte_blade
C++ | 122 lines | 92 code | 14 blank | 16 comment | 17 complexity | 84677eb87053299f895707a04705b648 MD5 | raw file
  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    data_fd = openInput(data_name);
 40}
 41
 42SensorBase::~SensorBase() {
 43    if (data_fd >= 0) {
 44        close(data_fd);
 45    }
 46    if (dev_fd >= 0) {
 47        close(dev_fd);
 48    }
 49}
 50
 51int SensorBase::open_device() {
 52    if (dev_fd<0 && dev_name) {
 53        dev_fd = open(dev_name, O_RDONLY);
 54        LOGE_IF(dev_fd<0, "Couldn't open %s (%s)", dev_name, strerror(errno));
 55    }
 56    return 0;
 57}
 58
 59int SensorBase::close_device() {
 60    if (dev_fd >= 0) {
 61        close(dev_fd);
 62        dev_fd = -1;
 63    }
 64    return 0;
 65}
 66
 67int SensorBase::getFd() const {
 68    return data_fd;
 69}
 70
 71int SensorBase::setDelay(int32_t handle, int64_t ns) {
 72    return 0;
 73}
 74
 75bool SensorBase::hasPendingEvents() const {
 76    return false;
 77}
 78
 79int64_t SensorBase::getTimestamp() {
 80    struct timespec t;
 81    t.tv_sec = t.tv_nsec = 0;
 82    clock_gettime(CLOCK_MONOTONIC, &t);
 83    return int64_t(t.tv_sec)*1000000000LL + t.tv_nsec;
 84}
 85
 86int SensorBase::openInput(const char* inputName) {
 87    int fd = -1;
 88    const char *dirname = "/dev/input";
 89    char devname[PATH_MAX];
 90    char *filename;
 91    DIR *dir;
 92    struct dirent *de;
 93    dir = opendir(dirname);
 94    if(dir == NULL)
 95        return -1;
 96    strcpy(devname, dirname);
 97    filename = devname + strlen(devname);
 98    *filename++ = '/';
 99    while((de = readdir(dir))) {
100        if(de->d_name[0] == '.' &&
101                (de->d_name[1] == '\0' ||
102                        (de->d_name[1] == '.' && de->d_name[2] == '\0')))
103            continue;
104        strcpy(filename, de->d_name);
105        fd = open(devname, O_RDONLY);
106        if (fd>=0) {
107            char name[80];
108            if (ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) {
109                name[0] = '\0';
110            }
111            if (!strcmp(name, inputName)) {
112                break;
113            } else {
114                close(fd);
115                fd = -1;
116            }
117        }
118    }
119    closedir(dir);
120    LOGE_IF(fd<0, "couldn't find '%s' input device", inputName);
121    return fd;
122}