2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "Sensors"
19 #include <hardware/sensors.h>
29 #include <linux/input.h>
31 #include <utils/Atomic.h>
32 #include <utils/Log.h>
35 #include "SensorBase.h"
37 #include "SensorConfig.h"
38 #include "BoardConfig.h"
40 #include "RotVecSensor.h"
41 #include "SynthCompassSensor.h"
43 /*****************************************************************************/
45 /* The SENSORS Module */
46 static int open_sensors(const struct hw_module_t* module, const char* id,
47 struct hw_device_t** device);
50 static int sensors__get_sensors_list(struct sensors_module_t*,
51 struct sensor_t const** list)
53 *list = BoardConfig::sensorList();
54 return BoardConfig::sensorListSize();
57 static struct hw_module_methods_t sensors_module_methods = {
61 struct sensors_module_t HAL_MODULE_INFO_SYM = {
63 tag: HARDWARE_MODULE_TAG,
66 id: SENSORS_HARDWARE_MODULE_ID,
67 name: "Samsung Sensor module",
68 author: "Samsung Electronic Company",
69 methods: &sensors_module_methods,
73 get_sensors_list: sensors__get_sensors_list,
76 struct sensors_poll_context_t {
77 struct sensors_poll_device_t device; // must be first
79 sensors_poll_context_t();
80 ~sensors_poll_context_t();
81 int activate(int handle, int enabled);
82 int setDelay(int handle, int64_t ns);
83 int pollEvents(sensors_event_t* data, int count);
86 static const size_t wake = numFds - 1;
87 static const char WAKE_MESSAGE = 'W';
88 struct pollfd mPollFds[numFds];
90 SensorBase* mSensors[numSensorDrivers];
93 /*****************************************************************************/
95 sensors_poll_context_t::sensors_poll_context_t()
98 BoardConfig::initSensors(mSensors);
100 for (int i = 0; i < numSensorDrivers; i++) {
103 mPollFds[i].fd = mSensors[i]->fd();
104 mPollFds[i].events = POLLIN;
105 mPollFds[i].revents = 0;
109 // Mild hack: the synthetic compass is implemented as a slave of
110 // the rotation vector, so we have to tell them about each other
111 // explicitly in lieu of an architecture that lets them probe.
112 SensorBase *rv = mSensors[rotvec], *sc = mSensors[syncompass];
114 ((RotVecSensor*)rv)->setSynthCompass((SynthCompassSensor*)sc);
115 ((SynthCompassSensor*)sc)->setRotVecSensor((RotVecSensor*)rv);
119 int result = pipe(wakeFds);
120 ALOGE_IF(result<0, "error creating wake pipe (%s)", strerror(errno));
121 fcntl(wakeFds[0], F_SETFL, O_NONBLOCK);
122 fcntl(wakeFds[1], F_SETFL, O_NONBLOCK);
123 mWritePipeFd = wakeFds[1];
125 mPollFds[wake].fd = wakeFds[0];
126 mPollFds[wake].events = POLLIN;
127 mPollFds[wake].revents = 0;
130 sensors_poll_context_t::~sensors_poll_context_t() {
131 for (int i=0 ; i<numSensorDrivers ; i++) {
134 close(mPollFds[wake].fd);
138 int sensors_poll_context_t::activate(int handle, int enabled) {
139 int index = BoardConfig::handleToDriver(handle);
140 if (index < 0) return index;
142 ALOGD("%s: %s sensor %d", __func__, enabled?"enable":"disable", index);
143 SensorBase* const s(mSensors[index]);
147 if ((err = s->enable(1)) < 0)
154 /* If the file descriptor is closed or opened the stored desciptor
155 should be updated. Even when not opened/closed the fd should
156 return correct value */
157 if (mPollFds[index].fd != s->fd()) {
158 const char wakeMessage(WAKE_MESSAGE);
159 int result = write(mWritePipeFd, &wakeMessage, 1);
160 ALOGE_IF(result<0, "error sending wake message (%s)", strerror(errno));
161 mPollFds[index].fd = s->fd();
166 int sensors_poll_context_t::setDelay(int handle, int64_t ns) {
168 int index = BoardConfig::handleToDriver(handle);
169 if (index < 0) return index;
170 return mSensors[index]->setDelay(ns);
173 int sensors_poll_context_t::pollEvents(sensors_event_t* data, int count)
179 // see if we have some leftover from the last poll()
180 for (int i=0 ; count && i<numSensorDrivers ; i++) {
181 SensorBase* const sensor(mSensors[i]);
182 mPollFds[i].fd = sensor->fd();
183 if (sensor && ((mPollFds[i].revents & POLLIN) || (sensor->hasPendingEvents()))) {
184 int nb = sensor->readEvents(data, count);
186 // no more data for this sensor
187 mPollFds[i].revents = 0;
190 ALOGD("%s: handle:%d error:%d", __func__, i, nb);
200 // Some sensors ("slaves") may have generated events based on
201 // the input to the sensors above. So check again.
202 for (int i=0 ; count && i<numSensorDrivers ; i++) {
203 SensorBase *sensor = mSensors[i];
204 if (sensor->hasPendingEvents()) {
205 int nb = sensor->readEvents(data, count);
215 // we still have some room, so try to see if we can get
216 // some events immediately or just wait if we don't have
217 // anything to return
218 n = poll(mPollFds, numFds, nbEvents ? 0 : POLL_TIMEOUT_MSEC);
220 ALOGE("poll() failed (%s)", strerror(errno));
223 if (mPollFds[wake].revents & POLLIN) {
225 int result = read(mPollFds[wake].fd, &msg, 1);
226 ALOGE_IF(result<0, "error reading from wake pipe (%s)", strerror(errno));
227 ALOGE_IF(msg != WAKE_MESSAGE, "unknown message on wake queue (0x%02x)", int(msg));
228 mPollFds[wake].revents = 0;
231 // if we have events and space, go read them
232 } while (n && count);
237 /*****************************************************************************/
239 static int poll__close(struct hw_device_t *dev)
241 sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
248 static int poll__activate(struct sensors_poll_device_t *dev,
249 int handle, int enabled) {
250 sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
251 return ctx->activate(handle, enabled);
254 static int poll__setDelay(struct sensors_poll_device_t *dev,
255 int handle, int64_t ns) {
256 sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
257 return ctx->setDelay(handle, ns);
260 static int poll__poll(struct sensors_poll_device_t *dev,
261 sensors_event_t* data, int count) {
262 sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
263 return ctx->pollEvents(data, count);
266 /*****************************************************************************/
268 /** Open a new instance of a sensor device using name */
269 static int open_sensors(const struct hw_module_t* module, const char* id,
270 struct hw_device_t** device)
272 int status = -EINVAL;
273 sensors_poll_context_t *dev = new sensors_poll_context_t();
275 memset(&dev->device, 0, sizeof(sensors_poll_device_t));
277 dev->device.common.tag = HARDWARE_DEVICE_TAG;
278 dev->device.common.version = 0;
279 dev->device.common.module = const_cast < hw_module_t * > (module);
280 dev->device.common.close = poll__close;
281 dev->device.activate = poll__activate;
282 dev->device.setDelay = poll__setDelay;
283 dev->device.poll = poll__poll;
285 *device = &dev->device.common;