OSDN Git Service

libsensors: fix building issues on Android 5.0
[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
31 #include <utils/Atomic.h>
32 #include <utils/Log.h>
33
34 #include "common.h"
35 #include "SensorBase.h"
36
37 #include "SensorConfig.h"
38 #include "BoardConfig.h"
39
40 #include "RotVecSensor.h"
41 #include "SynthCompassSensor.h"
42
43 /*****************************************************************************/
44
45 /* The SENSORS Module */
46 static int open_sensors(const struct hw_module_t* module, const char* id,
47                         struct hw_device_t** device);
48
49
50 static int sensors__get_sensors_list(struct sensors_module_t* module,
51                                      struct sensor_t const** list)
52 {
53         *list = BoardConfig::sensorList();
54         return BoardConfig::sensorListSize();
55 }
56
57 static struct hw_module_methods_t sensors_module_methods = {
58         open: open_sensors
59 };
60
61 struct sensors_module_t HAL_MODULE_INFO_SYM = {
62         common: {
63                 tag: HARDWARE_MODULE_TAG,
64                 version_major: 1,
65                 version_minor: 0,
66                 id: SENSORS_HARDWARE_MODULE_ID,
67                 name: "Samsung Sensor module",
68                 author: "Samsung Electronic Company",
69                 methods: &sensors_module_methods,
70                 dso: NULL,
71         },
72         get_sensors_list: sensors__get_sensors_list,
73 };
74
75 struct sensors_poll_context_t {
76     struct sensors_poll_device_t device; // must be first
77
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);
83
84 private:
85     static const size_t wake = numFds - 1;
86     static const char WAKE_MESSAGE = 'W';
87     struct pollfd mPollFds[numFds];
88     int mWritePipeFd;
89     SensorBase* mSensors[numSensorDrivers];
90 };
91
92 /*****************************************************************************/
93
94 sensors_poll_context_t::sensors_poll_context_t()
95                        :mSensors()
96 {
97     BoardConfig::initSensors(mSensors);
98
99     for (int i = 0; i < numSensorDrivers; i++) {
100         assert(mSensors[i]);
101         if (mSensors[i]){
102             mPollFds[i].fd = mSensors[i]->fd();
103             mPollFds[i].events = POLLIN;
104             mPollFds[i].revents = 0;
105         }
106     }
107
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];
112     if (rv && sc) {
113         ((RotVecSensor*)rv)->setSynthCompass((SynthCompassSensor*)sc);
114         ((SynthCompassSensor*)sc)->setRotVecSensor((RotVecSensor*)rv);
115     }
116
117     int wakeFds[2];
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];
123
124     mPollFds[wake].fd = wakeFds[0];
125     mPollFds[wake].events = POLLIN;
126     mPollFds[wake].revents = 0;
127 }
128
129 sensors_poll_context_t::~sensors_poll_context_t() {
130     for (int i=0 ; i<numSensorDrivers ; i++) {
131         delete mSensors[i];
132     }
133     close(mPollFds[wake].fd);
134     close(mWritePipeFd);
135 }
136
137 int sensors_poll_context_t::activate(int handle, int enabled) {
138     int index = BoardConfig::handleToDriver(handle);
139     if (index < 0) return index;
140
141     ALOGD("%s: %s sensor %d", __func__, enabled?"enable":"disable", index);
142     SensorBase* const s(mSensors[index]);
143     int err = 0;
144
145     if (enabled) {
146         if ((err = s->enable(1)) < 0)
147             s->close();
148     } else {
149         s->enable(0);
150         s->close();
151     }
152
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();
161     }
162     return err;
163 }
164
165 int sensors_poll_context_t::setDelay(int handle, int64_t ns) {
166
167     int index = BoardConfig::handleToDriver(handle);
168     if (index < 0) return index;
169     return mSensors[index]->setDelay(ns);
170 }
171
172 int sensors_poll_context_t::pollEvents(sensors_event_t* data, int count)
173 {
174     int nbEvents = 0;
175     int n = 0;
176
177     do {
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);
184                 if (nb < count) {
185                     // no more data for this sensor
186                     mPollFds[i].revents = 0;
187
188                     if (nb < 0) {
189                         ALOGD("%s: handle:%d error:%d", __func__, i, nb);
190                         continue;
191                     }
192                 }
193                 count -= nb;
194                 nbEvents += nb;
195                 data += nb;
196             }
197         }
198
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);
205                 if (nb > 0) {
206                     count -= nb;
207                     nbEvents += nb;
208                     data += nb;
209                 }
210             }
211         }
212
213         if (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);
218             if (n<0) {
219                 ALOGE("poll() failed (%s)", strerror(errno));
220                 return -errno;
221             }
222             if (mPollFds[wake].revents & POLLIN) {
223                 char msg;
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;
228             }
229         }
230         // if we have events and space, go read them
231     } while (n && count);
232
233     return nbEvents;
234 }
235
236 /*****************************************************************************/
237
238 static int poll__close(struct hw_device_t *dev)
239 {
240     sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
241     if (ctx) {
242         delete ctx;
243     }
244     return 0;
245 }
246
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);
251 }
252
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);
257 }
258
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);
263 }
264
265 /*****************************************************************************/
266
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)
270 {
271     int status = -EINVAL;
272     sensors_poll_context_t *dev = new sensors_poll_context_t();
273
274     memset(&dev->device, 0, sizeof(sensors_poll_device_t));
275
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;
283
284     *device = &dev->device.common;
285     status = 0;
286     return status;
287 }