1 /* 2 * Copyright (C) 2008-2014 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 <dirent.h> 18 #include <errno.h> 19 #include <fcntl.h> 20 #include <linux/input.h> 21 #include <math.h> 22 #include <poll.h> 23 #include <sys/select.h> 24 #include <unistd.h> 25 26 #include <cutils/log.h> 27 28 #include "SensorBase.h" 29 30 /*****************************************************************************/ 31 32 #undef LOG_TAG 33 #define LOG_TAG "CwMcuSensor" 34 35 SensorBase::SensorBase( 36 const char* dev_name, 37 const char* data_name) 38 : dev_name(dev_name) 39 , data_name(data_name) 40 , dev_fd(-1) 41 , data_fd(-1) 42 { 43 } 44 45 SensorBase::~SensorBase() { 46 if (data_fd >= 0) { 47 close(data_fd); 48 } 49 if (dev_fd >= 0) { 50 close(dev_fd); 51 } 52 } 53 54 int SensorBase::open_device() { 55 if (dev_fd<0 && dev_name) { 56 dev_fd = open(dev_name, O_RDONLY); 57 ALOGE_IF(dev_fd<0, "Couldn't open %s (%s)", dev_name, strerror(errno)); 58 } 59 return 0; 60 } 61 62 int SensorBase::close_device() { 63 if (dev_fd >= 0) { 64 close(dev_fd); 65 dev_fd = -1; 66 } 67 return 0; 68 } 69 70 int SensorBase::write_sys_attribute( 71 const char *path, const char *value, int bytes) 72 { 73 int fd, amt; 74 75 fd = open(path, O_WRONLY); 76 if (fd < 0) { 77 ALOGE("SensorBase: write_attr failed to open %s (%s)", 78 path, strerror(errno)); 79 return -1; 80 } 81 82 amt = write(fd, value, bytes); 83 amt = ((amt == -1) ? -errno : 0); 84 ALOGE_IF(amt < 0, "SensorBase: write_int failed to write %s (%s)", 85 path, strerror(errno)); 86 close(fd); 87 return amt; 88 } 89 90 int SensorBase::getFd() const { 91 if (!data_name) { 92 return dev_fd; 93 } 94 return data_fd; 95 } 96 97 int SensorBase::setDelay(int32_t, int64_t) { 98 return 0; 99 } 100 101 int64_t SensorBase::getDelay(int32_t) { 102 return 0; 103 } 104 105 bool SensorBase::hasPendingEvents() const { 106 return false; 107 } 108 109 int64_t SensorBase::getTimestamp() { 110 struct timespec t; 111 t.tv_sec = t.tv_nsec = 0; 112 clock_gettime(CLOCK_BOOTTIME, &t); 113 return int64_t(t.tv_sec)*NS_PER_SEC + t.tv_nsec; 114 } 115 116