OSDN Git Service

libsensors framework
[android-x86/hardware-intel-libsensors.git] / common / libsensors / 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
28 #include <linux/input.h>
29 #include <linux/akm8973.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 /*****************************************************************************/
41
42 /* The SENSORS Module */
43 static int open_sensors(const struct hw_module_t* module, const char* id,
44                         struct hw_device_t** device);
45
46
47 static int sensors__get_sensors_list(struct sensors_module_t* module,
48                                      struct sensor_t const** list)
49 {
50         *list = BoardConfig::sensorList();
51         return BoardConfig::sensorListSize();
52 }
53
54 static struct hw_module_methods_t sensors_module_methods = {
55         open: open_sensors
56 };
57
58 struct sensors_module_t HAL_MODULE_INFO_SYM = {
59         common: {
60                 tag: HARDWARE_MODULE_TAG,
61                 version_major: 1,
62                 version_minor: 0,
63                 id: SENSORS_HARDWARE_MODULE_ID,
64                 name: "Samsung Sensor module",
65                 author: "Samsung Electronic Company",
66                 methods: &sensors_module_methods,
67         },
68         get_sensors_list: sensors__get_sensors_list,
69 };
70
71 struct sensors_poll_context_t {
72     struct sensors_poll_device_t device; // must be first
73
74         sensors_poll_context_t();
75         ~sensors_poll_context_t();
76     int activate(int handle, int enabled);
77     int setDelay(int handle, int64_t ns);
78     int pollEvents(sensors_event_t* data, int count);
79
80 private:
81     static const size_t wake = numFds - 1;
82     static const char WAKE_MESSAGE = 'W';
83     struct pollfd mPollFds[numFds];
84     int mWritePipeFd;
85     SensorBase* mSensors[numSensorDrivers];
86 };
87
88 /*****************************************************************************/
89
90 sensors_poll_context_t::sensors_poll_context_t()
91 {
92     BoardConfig::initSensors(mSensors);
93
94     for (int i=0; i < numSensorDrivers; i++) {
95         mPollFds[i].fd = mSensors[i]->fd();
96         mPollFds[i].events = POLLIN;
97         mPollFds[i].revents = 0;
98     }
99
100     int wakeFds[2];
101     int result = pipe(wakeFds);
102     LOGE_IF(result<0, "error creating wake pipe (%s)", strerror(errno));
103     fcntl(wakeFds[0], F_SETFL, O_NONBLOCK);
104     fcntl(wakeFds[1], F_SETFL, O_NONBLOCK);
105     mWritePipeFd = wakeFds[1];
106
107     mPollFds[wake].fd = wakeFds[0];
108     mPollFds[wake].events = POLLIN;
109     mPollFds[wake].revents = 0;
110 }
111
112 sensors_poll_context_t::~sensors_poll_context_t() {
113     for (int i=0 ; i<numSensorDrivers ; i++) {
114         delete mSensors[i];
115     }
116     close(mPollFds[wake].fd);
117     close(mWritePipeFd);
118 }
119
120 int sensors_poll_context_t::activate(int handle, int enabled) {
121     int index = BoardConfig::handleToDriver(handle);
122     if (index < 0) return index;
123
124     LOGD("%s: %s sensor %d", __func__, enabled?"enable":"disable", index);
125     SensorBase* const s(mSensors[index]);
126     int err = 0;
127     if (enabled) {
128         if (((err = s->open()) > 0) && (err = s->enable(1)) == 0) {
129             const char wakeMessage(WAKE_MESSAGE);
130             int result = write(mWritePipeFd, &wakeMessage, 1);
131             LOGE_IF(result<0, "error sending wake message (%s)", strerror(errno));
132         } else
133             s->close();
134     } else {
135         s->enable(0);
136         s->close();
137     }
138
139     /* If the file descriptor is closed or opened the stored desciptor
140        should be updated. Even when not opened/closed the fd should
141        return correct value */
142     mPollFds[index].fd = s->fd();
143     return err;
144 }
145
146 int sensors_poll_context_t::setDelay(int handle, int64_t ns) {
147
148     int index = BoardConfig::handleToDriver(handle);
149     if (index < 0) return index;
150     return mSensors[index]->setDelay(ns);
151 }
152
153 int sensors_poll_context_t::pollEvents(sensors_event_t* data, int count)
154 {
155     int nbEvents = 0;
156     int n = 0;
157
158     do {
159         // see if we have some leftover from the last poll()
160         for (int i=0 ; count && i<numSensorDrivers ; i++) {
161             SensorBase* const sensor(mSensors[i]);
162             if ((mPollFds[i].revents & POLLIN) || (sensor->hasPendingEvents())) {
163                 int nb = sensor->readEvents(data, count);
164                 if (nb < count) {
165                     // no more data for this sensor
166                     mPollFds[i].revents = 0;
167
168                     if (nb < 0) {
169                         LOGD("%s: handle:%d error:%d", __func__, i, nb);
170                         continue;
171                     }
172                 }
173                 count -= nb;
174                 nbEvents += nb;
175                 data += nb;
176             }
177         }
178
179         if (count) {
180             // we still have some room, so try to see if we can get
181             // some events immediately or just wait if we don't have
182             // anything to return
183             n = poll(mPollFds, numFds, nbEvents ? 0 : POLL_TIMEOUT_MSEC);
184             if (n<0) {
185                 LOGE("poll() failed (%s)", strerror(errno));
186                 return -errno;
187             }
188             if (mPollFds[wake].revents & POLLIN) {
189                 char msg;
190                 int result = read(mPollFds[wake].fd, &msg, 1);
191                 LOGE_IF(result<0, "error reading from wake pipe (%s)", strerror(errno));
192                 LOGE_IF(msg != WAKE_MESSAGE, "unknown message on wake queue (0x%02x)", int(msg));
193                 mPollFds[wake].revents = 0;
194             }
195         }
196         // if we have events and space, go read them
197     } while (n && count);
198
199     return nbEvents;
200 }
201
202 /*****************************************************************************/
203
204 static int poll__close(struct hw_device_t *dev)
205 {
206     sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
207     if (ctx) {
208         delete ctx;
209     }
210     return 0;
211 }
212
213 static int poll__activate(struct sensors_poll_device_t *dev,
214         int handle, int enabled) {
215     sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
216     return ctx->activate(handle, enabled);
217 }
218
219 static int poll__setDelay(struct sensors_poll_device_t *dev,
220         int handle, int64_t ns) {
221     sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
222     return ctx->setDelay(handle, ns);
223 }
224
225 static int poll__poll(struct sensors_poll_device_t *dev,
226         sensors_event_t* data, int count) {
227     sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
228     return ctx->pollEvents(data, count);
229 }
230
231 /*****************************************************************************/
232
233 /** Open a new instance of a sensor device using name */
234 static int open_sensors(const struct hw_module_t* module, const char* id,
235                         struct hw_device_t** device)
236 {
237         int status = -EINVAL;
238         sensors_poll_context_t *dev = new sensors_poll_context_t();
239
240         memset(&dev->device, 0, sizeof(sensors_poll_device_t));
241
242         dev->device.common.tag = HARDWARE_DEVICE_TAG;
243         dev->device.common.version  = 0;
244         dev->device.common.module   = const_cast<hw_module_t*>(module);
245         dev->device.common.close    = poll__close;
246         dev->device.activate        = poll__activate;
247         dev->device.setDelay        = poll__setDelay;
248         dev->device.poll            = poll__poll;
249
250         *device = &dev->device.common;
251         status = 0;
252
253         return status;
254 }