OSDN Git Service

libsensors: Added support for Proximity sensor
[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* module,
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         },
73         get_sensors_list: sensors__get_sensors_list,
74 };
75
76 struct sensors_poll_context_t {
77     struct sensors_poll_device_t device; // must be first
78
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);
84
85 private:
86     static const size_t wake = numFds - 1;
87     static const char WAKE_MESSAGE = 'W';
88     struct pollfd mPollFds[numFds];
89     int mWritePipeFd;
90     SensorBase* mSensors[numSensorDrivers];
91 };
92
93 /*****************************************************************************/
94
95 sensors_poll_context_t::sensors_poll_context_t()
96                        :mSensors()
97 {
98     BoardConfig::initSensors(mSensors);
99
100     for (int i = 0; i < numSensorDrivers; i++) {
101         assert(mSensors[i]);
102         if (mSensors[i]){
103             mPollFds[i].fd = mSensors[i]->fd();
104             mPollFds[i].events = POLLIN;
105             mPollFds[i].revents = 0;
106         }
107     }
108
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];
113     if (rv && sc) {
114         ((RotVecSensor*)rv)->setSynthCompass((SynthCompassSensor*)sc);
115         ((SynthCompassSensor*)sc)->setRotVecSensor((RotVecSensor*)rv);
116     }
117
118     int wakeFds[2];
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];
124
125     mPollFds[wake].fd = wakeFds[0];
126     mPollFds[wake].events = POLLIN;
127     mPollFds[wake].revents = 0;
128 }
129
130 sensors_poll_context_t::~sensors_poll_context_t() {
131     for (int i=0 ; i<numSensorDrivers ; i++) {
132         delete mSensors[i];
133     }
134     close(mPollFds[wake].fd);
135     close(mWritePipeFd);
136 }
137
138 int sensors_poll_context_t::activate(int handle, int enabled) {
139     int index = BoardConfig::handleToDriver(handle);
140     if (index < 0) return index;
141
142     ALOGD("%s: %s sensor %d", __func__, enabled?"enable":"disable", index);
143     SensorBase* const s(mSensors[index]);
144     int err = 0;
145
146     if (enabled) {
147         if ((err = s->enable(1)) < 0)
148             s->close();
149     } else {
150         s->enable(0);
151         s->close();
152     }
153
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();
162     }
163     return err;
164 }
165
166 int sensors_poll_context_t::setDelay(int handle, int64_t ns) {
167
168     int index = BoardConfig::handleToDriver(handle);
169     if (index < 0) return index;
170     return mSensors[index]->setDelay(ns);
171 }
172
173 int sensors_poll_context_t::pollEvents(sensors_event_t* data, int count)
174 {
175     int nbEvents = 0;
176     int n = 0;
177
178     do {
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);
185                 if (nb < count) {
186                     // no more data for this sensor
187                     mPollFds[i].revents = 0;
188
189                     if (nb < 0) {
190                         ALOGD("%s: handle:%d error:%d", __func__, i, nb);
191                         continue;
192                     }
193                 }
194                 count -= nb;
195                 nbEvents += nb;
196                 data += nb;
197             }
198         }
199
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);
206                 if (nb > 0) {
207                     count -= nb;
208                     nbEvents += nb;
209                     data += nb;
210                 }
211             }
212         }
213
214         if (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);
219             if (n<0) {
220                 ALOGE("poll() failed (%s)", strerror(errno));
221                 return -errno;
222             }
223             if (mPollFds[wake].revents & POLLIN) {
224                 char msg;
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;
229             }
230         }
231         // if we have events and space, go read them
232     } while (n && count);
233
234     return nbEvents;
235 }
236
237 /*****************************************************************************/
238
239 static int poll__close(struct hw_device_t *dev)
240 {
241     sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
242     if (ctx) {
243         delete ctx;
244     }
245     return 0;
246 }
247
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);
252 }
253
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);
258 }
259
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);
264 }
265
266 /*****************************************************************************/
267
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)
271 {
272     int status = -EINVAL;
273     sensors_poll_context_t *dev = new sensors_poll_context_t();
274
275     memset(&dev->device, 0, sizeof(sensors_poll_device_t));
276
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;
284
285     *device = &dev->device.common;
286     status = 0;
287     return status;
288 }