blob: 432891eb982010841e3f1c6e4c3c80d01ac7f6cc [file] [log] [blame]
Wentao Xudb82bb12013-01-14 18:26:04 -05001/*
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#include <cutils/log.h>
25
26#include "PressureSensor.h"
27#include "sensors.h"
28
29#define FETCH_FULL_EVENT_BEFORE_RETURN 1
30
31#define EVENT_TYPE_PRESSURE ABS_PRESSURE
32
33#define CONVERT_PRESSURE (0.01)
34
35#define IGNORE_EVENT_TIME 0
36
37/*****************************************************************************/
38
39PressureSensor::PressureSensor()
Wentao Xu4cc7dff2013-02-19 16:48:37 -050040 : SensorBase(NULL, "bmp18x"),
Wentao Xudb82bb12013-01-14 18:26:04 -050041 mEnabled(0),
42 mInputReader(4),
43 mHasPendingEvent(false),
44 mEnabledTime(0)
45{
46 mPendingEvent.version = sizeof(sensors_event_t);
47 mPendingEvent.sensor = SENSORS_PRESSURE_HANDLE;
48 mPendingEvent.type = SENSOR_TYPE_PRESSURE;
49 memset(mPendingEvent.data, 0, sizeof(mPendingEvent.data));
50
51 if (data_fd) {
52 strcpy(input_sysfs_path, "/sys/class/input/");
53 strcat(input_sysfs_path, input_name);
Xiaocheng Li09436cd2013-07-01 16:00:32 -070054#ifdef TARGET_8610
55 strcat(input_sysfs_path, "/device/");
56#else
Wentao Xudb82bb12013-01-14 18:26:04 -050057 strcat(input_sysfs_path, "/device/device/");
Xiaocheng Li09436cd2013-07-01 16:00:32 -070058#endif
Wentao Xudb82bb12013-01-14 18:26:04 -050059 input_sysfs_path_len = strlen(input_sysfs_path);
60 enable(0, 1);
61 }
62}
63
Jie Chengfab048f2014-01-06 15:48:40 +080064PressureSensor::PressureSensor(char *name)
65 : SensorBase(NULL, "bmp18x"),
66 mEnabled(0),
67 mInputReader(4),
68 mHasPendingEvent(false),
69 mEnabledTime(0)
70{
71 mPendingEvent.version = sizeof(sensors_event_t);
72 mPendingEvent.sensor = SENSORS_PRESSURE_HANDLE;
73 mPendingEvent.type = SENSOR_TYPE_PRESSURE;
74 memset(mPendingEvent.data, 0, sizeof(mPendingEvent.data));
75
76 if (data_fd) {
77 strlcpy(input_sysfs_path, SYSFS_CLASS, sizeof(input_sysfs_path));
78 strlcat(input_sysfs_path, "/", sizeof(input_sysfs_path));
79 strlcat(input_sysfs_path, name, sizeof(input_sysfs_path));
80 strlcat(input_sysfs_path, "/", sizeof(input_sysfs_path));
81 input_sysfs_path_len = strlen(input_sysfs_path);
82 ALOGI("The pressure sensor path is %s",input_sysfs_path);
83 enable(0, 1);
84 }
85}
86
Wentao Xudb82bb12013-01-14 18:26:04 -050087PressureSensor::~PressureSensor() {
88 if (mEnabled) {
89 enable(0, 0);
90 }
91}
92
93int PressureSensor::setInitialState() {
94 struct input_absinfo absinfo;
95 float value;
96 if (!ioctl(data_fd, EVIOCGABS(EVENT_TYPE_PRESSURE), &absinfo)) {
97 value = absinfo.value;
98 mPendingEvent.pressure = value * CONVERT_PRESSURE;
99 mHasPendingEvent = true;
100 }
101 return 0;
102}
103
104int PressureSensor::enable(int32_t, int en) {
105 int flags = en ? 1 : 0;
106 if (flags != mEnabled) {
107 int fd;
Jie Chengfab048f2014-01-06 15:48:40 +0800108 strlcpy(&input_sysfs_path[input_sysfs_path_len],
109 SYSFS_ENABLE, SYSFS_MAXLEN);
Wentao Xudb82bb12013-01-14 18:26:04 -0500110 fd = open(input_sysfs_path, O_RDWR);
111 if (fd >= 0) {
112 char buf[2];
113 int err;
114 buf[1] = 0;
115 if (flags) {
116 buf[0] = '1';
117 mEnabledTime = getTimestamp() + IGNORE_EVENT_TIME;
118 } else {
119 buf[0] = '0';
120 }
121 err = write(fd, buf, sizeof(buf));
122 close(fd);
123 mEnabled = flags;
124 setInitialState();
125 return 0;
126 }
127 return -1;
128 }
129 return 0;
130}
131
132bool PressureSensor::hasPendingEvents() const {
133 return mHasPendingEvent;
134}
135
136int PressureSensor::setDelay(int32_t handle, int64_t delay_ns)
137{
138 int fd;
Wentao Xu4cc7dff2013-02-19 16:48:37 -0500139 int delay_ms = delay_ns / 1000000;
Jie Chengfab048f2014-01-06 15:48:40 +0800140 strlcpy(&input_sysfs_path[input_sysfs_path_len],
141 SYSFS_POLL_DELAY, SYSFS_MAXLEN);
Wentao Xudb82bb12013-01-14 18:26:04 -0500142 fd = open(input_sysfs_path, O_RDWR);
143 if (fd >= 0) {
144 char buf[80];
Wentao Xu4cc7dff2013-02-19 16:48:37 -0500145 sprintf(buf, "%d", delay_ms);
Wentao Xudb82bb12013-01-14 18:26:04 -0500146 write(fd, buf, strlen(buf)+1);
147 close(fd);
148 return 0;
149 }
150 return -1;
151}
152
153int PressureSensor::readEvents(sensors_event_t* data, int count)
154{
155 if (count < 1)
156 return -EINVAL;
157
158 if (mHasPendingEvent) {
159 mHasPendingEvent = false;
160 mPendingEvent.timestamp = getTimestamp();
161 *data = mPendingEvent;
162 return mEnabled ? 1 : 0;
163 }
164
165 ssize_t n = mInputReader.fill(data_fd);
166 if (n < 0)
167 return n;
168
169 int numEventReceived = 0;
170 input_event const* event;
171
172#if FETCH_FULL_EVENT_BEFORE_RETURN
173again:
174#endif
175 while (count && mInputReader.readEvent(&event)) {
176 int type = event->type;
177 if (type == EV_ABS) {
178 float value = event->value;
179 mPendingEvent.pressure = value * CONVERT_PRESSURE;
180 } else if (type == EV_SYN) {
181 mPendingEvent.timestamp = timevalToNano(event->time);
182 if (mEnabled) {
183 if (mPendingEvent.timestamp >= mEnabledTime) {
184 *data++ = mPendingEvent;
185 numEventReceived++;
186 }
187 count--;
188 }
189 } else {
190 ALOGE("PressureSensor: unknown event (type=%d, code=%d)",
191 type, event->code);
192 }
193 mInputReader.next();
194 }
195
196#if FETCH_FULL_EVENT_BEFORE_RETURN
197 /* if we didn't read a complete event, see if we can fill and
198 try again instead of returning with nothing and redoing poll. */
199 if (numEventReceived == 0 && mEnabled == 1) {
200 n = mInputReader.fill(data_fd);
201 if (n)
202 goto again;
203 }
204#endif
205
206 return numEventReceived;
207}
208