OSDN Git Service

libsensors: make the implementation be compatible with new IIO ABI
[android-x86/hardware-intel-libsensors.git] / sensors.cpp
1 /*
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 #define LOG_TAG "Sensors"
18
19 #include <hardware/sensors.h>
20 #include <fcntl.h>
21 #include <errno.h>
22 #include <dirent.h>
23 #include <math.h>
24 #include <poll.h>
25 #include <pthread.h>
26 #include <stdlib.h>
27 #include <assert.h>
28
29 #include <linux/input.h>
30 #include <linux/akm8973.h>
31
32 #include <utils/Atomic.h>
33 #include <utils/Log.h>
34
35 #include "common.h"
36 #include "SensorBase.h"
37
38 #include "SensorConfig.h"
39 #include "BoardConfig.h"
40
41 #include "RotVecSensor.h"
42 #include "SynthCompassSensor.h"
43
44 /*****************************************************************************/
45
46 /* The SENSORS Module */
47 static int open_sensors(const struct hw_module_t* module, const char* id,
48                         struct hw_device_t** device);
49
50
51 static int sensors__get_sensors_list(struct sensors_module_t*,
52                                      struct sensor_t const** list)
53 {
54         *list = BoardConfig::sensorList();
55         return BoardConfig::sensorListSize();
56 }
57
58 static struct hw_module_methods_t sensors_module_methods = {
59         open: open_sensors
60 };
61
62 struct sensors_module_t HAL_MODULE_INFO_SYM = {
63         common: {
64                 tag: HARDWARE_MODULE_TAG,
65                 version_major: 1,
66                 version_minor: 0,
67                 id: SENSORS_HARDWARE_MODULE_ID,
68                 name: "Samsung Sensor module",
69                 author: "Samsung Electronic Company",
70                 methods: &sensors_module_methods,
71                 dso: NULL,
72                 reserved: {}
73         },
74         get_sensors_list: sensors__get_sensors_list,
75 };
76
77 struct sensors_poll_context_t {
78     struct sensors_poll_device_t device; // must be first
79
80         sensors_poll_context_t();
81         ~sensors_poll_context_t();
82     int activate(int handle, int enabled);
83     int setDelay(int handle, int64_t ns);
84     int pollEvents(sensors_event_t* data, int count);
85
86 private:
87     static const size_t wake = numFds - 1;
88     static const char WAKE_MESSAGE = 'W';
89     struct pollfd mPollFds[numFds];
90     int mWritePipeFd;
91     SensorBase* mSensors[numSensorDrivers];
92 };
93
94 /*****************************************************************************/
95
96 sensors_poll_context_t::sensors_poll_context_t()
97                        :mSensors()
98 {
99     BoardConfig::initSensors(mSensors);
100
101     for (int i = 0; i < numSensorDrivers; i++) {
102         assert(mSensors[i]);
103         if (mSensors[i]){
104             mPollFds[i].fd = mSensors[i]->fd();
105             mPollFds[i].events = POLLIN;
106             mPollFds[i].revents = 0;
107         }
108     }
109
110     // Mild hack: the synthetic compass is implemented as a slave of
111     // the rotation vector, so we have to tell them about each other
112     // explicitly in lieu of an architecture that lets them probe.
113     SensorBase *rv = mSensors[rotvec], *sc = mSensors[syncompass];
114     if (rv && sc) {
115         ((RotVecSensor*)rv)->setSynthCompass((SynthCompassSensor*)sc);
116         ((SynthCompassSensor*)sc)->setRotVecSensor((RotVecSensor*)rv);
117     }
118
119     int wakeFds[2];
120     int result = pipe(wakeFds);
121     ALOGE_IF(result<0, "error creating wake pipe (%s)", strerror(errno));
122     fcntl(wakeFds[0], F_SETFL, O_NONBLOCK);
123     fcntl(wakeFds[1], F_SETFL, O_NONBLOCK);
124     mWritePipeFd = wakeFds[1];
125
126     mPollFds[wake].fd = wakeFds[0];
127     mPollFds[wake].events = POLLIN;
128     mPollFds[wake].revents = 0;
129 }
130
131 sensors_poll_context_t::~sensors_poll_context_t() {
132     for (int i=0 ; i<numSensorDrivers ; i++) {
133         delete mSensors[i];
134     }
135     close(mPollFds[wake].fd);
136     close(mWritePipeFd);
137 }
138
139 int sensors_poll_context_t::activate(int handle, int enabled) {
140     int index = BoardConfig::handleToDriver(handle);
141     if (index < 0) return index;
142
143     ALOGD("%s: %s sensor %d", __func__, enabled?"enable":"disable", index);
144     SensorBase* const s(mSensors[index]);
145     int err = 0;
146
147     if (enabled) {
148         if ((err = s->enable(1)) < 0)
149             s->close();
150     } else {
151         s->enable(0);
152         s->close();
153     }
154
155     /* If the file descriptor is closed or opened the stored desciptor
156        should be updated. Even when not opened/closed the fd should
157        return correct value */
158     if (mPollFds[index].fd != s->fd()) {
159         const char wakeMessage(WAKE_MESSAGE);
160         int result = write(mWritePipeFd, &wakeMessage, 1);
161         ALOGE_IF(result<0, "error sending wake message (%s)", strerror(errno));
162         mPollFds[index].fd = s->fd();
163     }
164     return err;
165 }
166
167 int sensors_poll_context_t::setDelay(int handle, int64_t ns) {
168
169     int index = BoardConfig::handleToDriver(handle);
170     if (index < 0) return index;
171     return mSensors[index]->setDelay(ns);
172 }
173
174 int sensors_poll_context_t::pollEvents(sensors_event_t* data, int count)
175 {
176     int nbEvents = 0;
177     int n = 0;
178
179     do {
180         // see if we have some leftover from the last poll()
181         for (int i=0 ; count && i<numSensorDrivers ; i++) {
182             SensorBase* const sensor(mSensors[i]);
183             mPollFds[i].fd = sensor->fd();
184             if (sensor && ((mPollFds[i].revents & POLLIN) || (sensor->hasPendingEvents()))) {
185                 int nb = sensor->readEvents(data, count);
186                 if (nb < count) {
187                     // no more data for this sensor
188                     mPollFds[i].revents = 0;
189
190                     if (nb < 0) {
191                         ALOGD("%s: handle:%d error:%d", __func__, i, nb);
192                         continue;
193                     }
194                 }
195                 count -= nb;
196                 nbEvents += nb;
197                 data += nb;
198             }
199         }
200
201         // Some sensors ("slaves") may have generated events based on
202         // the input to the sensors above.  So check again.
203         for (int i=0 ; count && i<numSensorDrivers ; i++) {
204             SensorBase *sensor = mSensors[i];
205             if (sensor->hasPendingEvents()) {
206                 int nb = sensor->readEvents(data, count);
207                 if (nb > 0) {
208                     count -= nb;
209                     nbEvents += nb;
210                     data += nb;
211                 }
212             }
213         }
214
215         if (count) {
216             // we still have some room, so try to see if we can get
217             // some events immediately or just wait if we don't have
218             // anything to return
219             n = poll(mPollFds, numFds, nbEvents ? 0 : POLL_TIMEOUT_MSEC);
220             if (n<0) {
221                 ALOGE("poll() failed (%s)", strerror(errno));
222                 return -errno;
223             }
224             if (mPollFds[wake].revents & POLLIN) {
225                 char msg;
226                 int result = read(mPollFds[wake].fd, &msg, 1);
227                 ALOGE_IF(result<0, "error reading from wake pipe (%s)", strerror(errno));
228                 ALOGE_IF(msg != WAKE_MESSAGE, "unknown message on wake queue (0x%02x)", int(msg));
229                 mPollFds[wake].revents = 0;
230             }
231         }
232         // if we have events and space, go read them
233     } while (n && count);
234
235     return nbEvents;
236 }
237
238 /*****************************************************************************/
239
240 static int poll__close(struct hw_device_t *dev)
241 {
242     sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
243     if (ctx) {
244         delete ctx;
245     }
246     return 0;
247 }
248
249 static int poll__activate(struct sensors_poll_device_t *dev,
250         int handle, int enabled) {
251     sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
252     return ctx->activate(handle, enabled);
253 }
254
255 static int poll__setDelay(struct sensors_poll_device_t *dev,
256         int handle, int64_t ns) {
257     sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
258     return ctx->setDelay(handle, ns);
259 }
260
261 static int poll__poll(struct sensors_poll_device_t *dev,
262         sensors_event_t* data, int count) {
263     sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
264     return ctx->pollEvents(data, count);
265 }
266
267 /*****************************************************************************/
268
269 /** Open a new instance of a sensor device using name */
270 static int open_sensors(const struct hw_module_t* module, const char* id,
271                         struct hw_device_t** device)
272 {
273     int status = -EINVAL;
274     sensors_poll_context_t *dev = new sensors_poll_context_t();
275
276     memset(&dev->device, 0, sizeof(sensors_poll_device_t));
277
278     dev->device.common.tag = HARDWARE_DEVICE_TAG;
279     dev->device.common.version = 0;
280     dev->device.common.module = const_cast < hw_module_t * > (module);
281     dev->device.common.close = poll__close;
282     dev->device.activate = poll__activate;
283     dev->device.setDelay = poll__setDelay;
284     dev->device.poll = poll__poll;
285
286     *device = &dev->device.common;
287     status = 0;
288     return status;
289 }