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* module,
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,
72 get_sensors_list: sensors__get_sensors_list,
75 struct sensors_poll_context_t {
76 struct sensors_poll_device_t device; // must be first
78 sensors_poll_context_t();
79 ~sensors_poll_context_t();
80 int activate(int handle, int enabled);
81 int setDelay(int handle, int64_t ns);
82 int pollEvents(sensors_event_t* data, int count);
85 static const size_t wake = numFds - 1;
86 static const char WAKE_MESSAGE = 'W';
87 struct pollfd mPollFds[numFds];
89 SensorBase* mSensors[numSensorDrivers];
92 /*****************************************************************************/
94 sensors_poll_context_t::sensors_poll_context_t()
97 BoardConfig::initSensors(mSensors);
99 for (int i = 0; i < numSensorDrivers; i++) {
102 mPollFds[i].fd = mSensors[i]->fd();
103 mPollFds[i].events = POLLIN;
104 mPollFds[i].revents = 0;
108 // Mild hack: the synthetic compass is implemented as a slave of
109 // the rotation vector, so we have to tell them about each other
110 // explicitly in lieu of an architecture that lets them probe.
111 SensorBase *rv = mSensors[rotvec], *sc = mSensors[syncompass];
113 ((RotVecSensor*)rv)->setSynthCompass((SynthCompassSensor*)sc);
114 ((SynthCompassSensor*)sc)->setRotVecSensor((RotVecSensor*)rv);
118 int result = pipe(wakeFds);
119 ALOGE_IF(result<0, "error creating wake pipe (%s)", strerror(errno));
120 fcntl(wakeFds[0], F_SETFL, O_NONBLOCK);
121 fcntl(wakeFds[1], F_SETFL, O_NONBLOCK);
122 mWritePipeFd = wakeFds[1];
124 mPollFds[wake].fd = wakeFds[0];
125 mPollFds[wake].events = POLLIN;
126 mPollFds[wake].revents = 0;
129 sensors_poll_context_t::~sensors_poll_context_t() {
130 for (int i=0 ; i<numSensorDrivers ; i++) {
133 close(mPollFds[wake].fd);
137 int sensors_poll_context_t::activate(int handle, int enabled) {
138 int index = BoardConfig::handleToDriver(handle);
139 if (index < 0) return index;
141 ALOGD("%s: %s sensor %d", __func__, enabled?"enable":"disable", index);
142 SensorBase* const s(mSensors[index]);
146 if ((err = s->enable(1)) < 0)
153 /* If the file descriptor is closed or opened the stored desciptor
154 should be updated. Even when not opened/closed the fd should
155 return correct value */
156 if (mPollFds[index].fd != s->fd()) {
157 const char wakeMessage(WAKE_MESSAGE);
158 int result = write(mWritePipeFd, &wakeMessage, 1);
159 ALOGE_IF(result<0, "error sending wake message (%s)", strerror(errno));
160 mPollFds[index].fd = s->fd();
165 int sensors_poll_context_t::setDelay(int handle, int64_t ns) {
167 int index = BoardConfig::handleToDriver(handle);
168 if (index < 0) return index;
169 return mSensors[index]->setDelay(ns);
172 int sensors_poll_context_t::pollEvents(sensors_event_t* data, int count)
178 // see if we have some leftover from the last poll()
179 for (int i=0 ; count && i<numSensorDrivers ; i++) {
180 SensorBase* const sensor(mSensors[i]);
181 mPollFds[i].fd = sensor->fd();
182 if (sensor && ((mPollFds[i].revents & POLLIN) || (sensor->hasPendingEvents()))) {
183 int nb = sensor->readEvents(data, count);
185 // no more data for this sensor
186 mPollFds[i].revents = 0;
189 ALOGD("%s: handle:%d error:%d", __func__, i, nb);
199 // Some sensors ("slaves") may have generated events based on
200 // the input to the sensors above. So check again.
201 for (int i=0 ; count && i<numSensorDrivers ; i++) {
202 SensorBase *sensor = mSensors[i];
203 if (sensor->hasPendingEvents()) {
204 int nb = sensor->readEvents(data, count);
214 // we still have some room, so try to see if we can get
215 // some events immediately or just wait if we don't have
216 // anything to return
217 n = poll(mPollFds, numFds, nbEvents ? 0 : POLL_TIMEOUT_MSEC);
219 ALOGE("poll() failed (%s)", strerror(errno));
222 if (mPollFds[wake].revents & POLLIN) {
224 int result = read(mPollFds[wake].fd, &msg, 1);
225 ALOGE_IF(result<0, "error reading from wake pipe (%s)", strerror(errno));
226 ALOGE_IF(msg != WAKE_MESSAGE, "unknown message on wake queue (0x%02x)", int(msg));
227 mPollFds[wake].revents = 0;
230 // if we have events and space, go read them
231 } while (n && count);
236 /*****************************************************************************/
238 static int poll__close(struct hw_device_t *dev)
240 sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
247 static int poll__activate(struct sensors_poll_device_t *dev,
248 int handle, int enabled) {
249 sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
250 return ctx->activate(handle, enabled);
253 static int poll__setDelay(struct sensors_poll_device_t *dev,
254 int handle, int64_t ns) {
255 sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
256 return ctx->setDelay(handle, ns);
259 static int poll__poll(struct sensors_poll_device_t *dev,
260 sensors_event_t* data, int count) {
261 sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
262 return ctx->pollEvents(data, count);
265 /*****************************************************************************/
267 /** Open a new instance of a sensor device using name */
268 static int open_sensors(const struct hw_module_t* module, const char* id,
269 struct hw_device_t** device)
271 int status = -EINVAL;
272 sensors_poll_context_t *dev = new sensors_poll_context_t();
274 memset(&dev->device, 0, sizeof(sensors_poll_device_t));
276 dev->device.common.tag = HARDWARE_DEVICE_TAG;
277 dev->device.common.version = 0;
278 dev->device.common.module = const_cast < hw_module_t * > (module);
279 dev->device.common.close = poll__close;
280 dev->device.activate = poll__activate;
281 dev->device.setDelay = poll__setDelay;
282 dev->device.poll = poll__poll;
284 *device = &dev->device.common;