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