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 29 /*****************************************************************************/ 30 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 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 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 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 70 int SensorBase::getFd() const 71 { 72 if (!data_name) { 73 return dev_fd; 74 } 75 return data_fd; 76 } 77 78 int SensorBase::setDelay(int32_t handle, int64_t ns) 79 { 80 return 0; 81 } 82 83 bool SensorBase::hasPendingEvents() const 84 { 85 return false; 86 } 87 88 int64_t SensorBase::getTimestamp() 89 { 90 struct timespec t; 91 t.tv_sec = t.tv_nsec = 0; 92 clock_gettime(CLOCK_MONOTONIC, &t); 93 return int64_t(t.tv_sec) * 1000000000LL + t.tv_nsec; 94 } 95 96 int SensorBase::openInput(const char *inputName) 97 { 98 int fd = -1; 99 const char *dirname = "/dev/input"; 100 char devname[PATH_MAX]; 101 char *filename; 102 DIR *dir; 103 struct dirent *de; 104 dir = opendir(dirname); 105 if(dir == NULL) 106 return -1; 107 strcpy(devname, dirname); 108 filename = devname + strlen(devname); 109 *filename++ = '/'; 110 while((de = readdir(dir))) { 111 if(de->d_name[0] == '.' && 112 (de->d_name[1] == '\0' || 113 (de->d_name[1] == '.' && de->d_name[2] == '\0'))) 114 continue; 115 strcpy(filename, de->d_name); 116 fd = open(devname, O_RDONLY); 117 LOGV_IF(EXTRA_VERBOSE, "path open %s", devname); 118 LOGI("path open %s", devname); 119 if (fd >= 0) { 120 char name[80]; 121 if (ioctl(fd, EVIOCGNAME(sizeof(name) - 1), &name) < 1) { 122 name[0] = '\0'; 123 } 124 LOGV_IF(EXTRA_VERBOSE, "name read %s", name); 125 if (!strcmp(name, inputName)) { 126 strcpy(input_name, filename); 127 break; 128 } else { 129 close(fd); 130 fd = -1; 131 } 132 } 133 } 134 closedir(dir); 135 LOGE_IF(fd < 0, "couldn't find '%s' input device", inputName); 136 return fd; 137 } 138 139 int SensorBase::enable(int32_t handle, int enabled) 140 { 141 return 0; 142 } 143 144 int SensorBase::query(int what, int* value) 145 { 146 return 0; 147 } 148 149 int SensorBase::batch(int handle, int flags, int64_t period_ns, int64_t timeout) 150 { 151 return 0; 152 } 153