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 33 SensorBase::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 44 SensorBase::~SensorBase() { 45 if (data_fd >= 0) { 46 close(data_fd); 47 } 48 if (dev_fd >= 0) { 49 close(dev_fd); 50 } 51 } 52 53 int 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 61 int SensorBase::close_device() { 62 if (dev_fd >= 0) { 63 close(dev_fd); 64 dev_fd = -1; 65 } 66 return 0; 67 } 68 69 int SensorBase::write_sys_attribute( 70 const char *path, const char *value, int bytes) 71 { 72 int fd, amt; 73 74 fd = open(path, O_WRONLY); 75 if (fd < 0) { 76 ALOGE("SensorBase: write_attr failed to open %s (%s)", 77 path, strerror(errno)); 78 return -1; 79 } 80 81 amt = write(fd, value, bytes); 82 amt = ((amt == -1) ? -errno : 0); 83 ALOGE_IF(amt < 0, "SensorBase: write_int failed to write %s (%s)", 84 path, strerror(errno)); 85 close(fd); 86 return amt; 87 } 88 89 int SensorBase::getFd() const { 90 if (!data_name) { 91 return dev_fd; 92 } 93 return data_fd; 94 } 95 96 int SensorBase::setDelay(int32_t handle, int64_t ns) { 97 return 0; 98 } 99 100 int64_t SensorBase::getDelay(int32_t handle) { 101 return 0; 102 } 103 104 bool SensorBase::hasPendingEvents() const { 105 return false; 106 } 107 108 int64_t SensorBase::getTimestamp() { 109 struct timespec t; 110 t.tv_sec = t.tv_nsec = 0; 111 clock_gettime(CLOCK_MONOTONIC, &t); 112 return int64_t(t.tv_sec)*1000000000LL + t.tv_nsec; 113 } 114 115 int SensorBase::openInput(const char* inputName) { 116 int fd = -1; 117 const char *dirname = "/dev/input"; 118 char devname[PATH_MAX]; 119 char *filename; 120 DIR *dir; 121 struct dirent *de; 122 dir = opendir(dirname); 123 if(dir == NULL) 124 return -1; 125 strcpy(devname, dirname); 126 filename = devname + strlen(devname); 127 *filename++ = '/'; 128 while((de = readdir(dir))) { 129 if(de->d_name[0] == '.' && 130 (de->d_name[1] == '\0' || 131 (de->d_name[1] == '.' && de->d_name[2] == '\0'))) 132 continue; 133 strcpy(filename, de->d_name); 134 fd = open(devname, O_RDONLY); 135 if (fd>=0) { 136 char name[80]; 137 if (ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) { 138 name[0] = '\0'; 139 } 140 if (!strcmp(name, inputName)) { 141 strcpy(input_name, filename); 142 break; 143 } else { 144 close(fd); 145 fd = -1; 146 } 147 } 148 } 149 closedir(dir); 150 ALOGE_IF(fd<0, "couldn't find '%s' input device", inputName); 151 return fd; 152 } 153