1 /*
2 * Copyright (C) 2011 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_NDEBUG 0
18 #define LOG_TAG "Sensors"
19 //#define FUNC_LOG LOGV("%s", __PRETTY_FUNCTION__)
20 #define FUNC_LOG
21
22 #include <hardware/sensors.h>
23 #include <fcntl.h>
24 #include <errno.h>
25 #include <dirent.h>
26 #include <math.h>
27 #include <poll.h>
28 #include <pthread.h>
29 #include <stdlib.h>
30
31 #include <linux/input.h>
32
33 #include <utils/Atomic.h>
34 #include <utils/Log.h>
35
36 #include "sensors.h"
37
38 #include "MPLSensor.h"
39 #include "LightSensor.h"
40 #include "ProximitySensor.h"
41 #include "PressureSensor.h"
42
43
44 /*****************************************************************************/
45
46 #define DELAY_OUT_TIME 0x7FFFFFFF
47
48 #define LIGHT_SENSOR_POLLTIME 2000000000
49
50 #define SENSORS_ROTATION_VECTOR (1<<ID_RV)
51 #define SENSORS_LINEAR_ACCEL (1<<ID_LA)
52 #define SENSORS_GRAVITY (1<<ID_GR)
53 #define SENSORS_GYROSCOPE (1<<ID_GY)
54 #define SENSORS_ACCELERATION (1<<ID_A)
55 #define SENSORS_MAGNETIC_FIELD (1<<ID_M)
56 #define SENSORS_ORIENTATION (1<<ID_O)
57 #define SENSORS_LIGHT (1<<ID_L)
58 #define SENSORS_PROXIMITY (1<<ID_P)
59 #define SENSORS_PRESSURE (1<<ID_PR)
60
61 #define SENSORS_ROTATION_VECTOR_HANDLE (ID_RV)
62 #define SENSORS_LINEAR_ACCEL_HANDLE (ID_LA)
63 #define SENSORS_GRAVITY_HANDLE (ID_GR)
64 #define SENSORS_GYROSCOPE_HANDLE (ID_GY)
65 #define SENSORS_ACCELERATION_HANDLE (ID_A)
66 #define SENSORS_MAGNETIC_FIELD_HANDLE (ID_M)
67 #define SENSORS_ORIENTATION_HANDLE (ID_O)
68 #define SENSORS_LIGHT_HANDLE (ID_L)
69 #define SENSORS_PROXIMITY_HANDLE (ID_P)
70 #define SENSORS_PRESSURE_HANDLE (ID_PR)
71 #define AKM_FTRACE 0
72 #define AKM_DEBUG 0
73 #define AKM_DATA 0
74
75 /*****************************************************************************/
76
77 /* The SENSORS Module */
78 #define LOCAL_SENSORS (3)
79 static struct sensor_t sSensorList[LOCAL_SENSORS + MPLSensor::numSensors] = {
80 { "GP2A Light sensor",
81 "Sharp",
82 1, SENSORS_LIGHT_HANDLE,
83 SENSOR_TYPE_LIGHT, 3000.0f, 1.0f, 0.75f, 0, { } },
84 { "GP2A Proximity sensor",
85 "Sharp",
86 1, SENSORS_PROXIMITY_HANDLE,
87 SENSOR_TYPE_PROXIMITY, 5.0f, 5.0f, 0.75f, 0, { } },
88 { "BMP180 Pressure sensor",
89 "Bosch",
90 1, SENSORS_PRESSURE_HANDLE,
91 SENSOR_TYPE_PRESSURE, 1100.0f, 0.01f, 0.67f, 20000, { } },
92 };
93 static int numSensors = LOCAL_SENSORS;
94
95 static int open_sensors(const struct hw_module_t* module, const char* id,
96 struct hw_device_t** device);
97
98
sensors__get_sensors_list(struct sensors_module_t * module,struct sensor_t const ** list)99 static int sensors__get_sensors_list(struct sensors_module_t* module,
100 struct sensor_t const** list)
101 {
102 *list = sSensorList;
103 return numSensors;
104 }
105
106 static struct hw_module_methods_t sensors_module_methods = {
107 open: open_sensors
108 };
109
110 struct sensors_module_t HAL_MODULE_INFO_SYM = {
111 common: {
112 tag: HARDWARE_MODULE_TAG,
113 version_major: 1,
114 version_minor: 0,
115 id: SENSORS_HARDWARE_MODULE_ID,
116 name: "Samsung Sensor module",
117 author: "Samsung Electronic Company",
118 methods: &sensors_module_methods,
119 dso: 0,
120 reserved: {},
121 },
122 get_sensors_list: sensors__get_sensors_list,
123 };
124
125 struct sensors_poll_context_t {
126 struct sensors_poll_device_t device; // must be first
127
128 sensors_poll_context_t();
129 ~sensors_poll_context_t();
130 int activate(int handle, int enabled);
131 int setDelay(int handle, int64_t ns);
132 int pollEvents(sensors_event_t* data, int count);
133
134 private:
135 enum {
136 mpl = 0, //all mpl entries must be consecutive and in this order
137 mpl_accel,
138 mpl_timer,
139 light,
140 proximity,
141 pressure,
142 numSensorDrivers, // wake pipe goes here
143 mpl_power, //special handle for MPL pm interaction
144 numFds,
145 };
146
147 static const size_t wake = numFds - 2;
148 static const char WAKE_MESSAGE = 'W';
149 struct pollfd mPollFds[numFds];
150 int mWritePipeFd;
151 SensorBase* mSensors[numSensorDrivers];
152
handleToDriversensors_poll_context_t153 int handleToDriver(int handle) const {
154 switch (handle) {
155 case ID_RV:
156 case ID_LA:
157 case ID_GR:
158 case ID_GY:
159 case ID_A:
160 case ID_M:
161 case ID_O:
162 return mpl;
163 case ID_L:
164 return light;
165 case ID_P:
166 return proximity;
167 case ID_PR:
168 return pressure;
169 }
170 return -EINVAL;
171 }
172 };
173
174 /*****************************************************************************/
175
sensors_poll_context_t()176 sensors_poll_context_t::sensors_poll_context_t()
177 {
178 FUNC_LOG;
179 MPLSensor* p_mplsen = new MPLSensor();
180 setCallbackObject(p_mplsen); //setup the callback object for handing mpl callbacks
181 numSensors =
182 LOCAL_SENSORS +
183 p_mplsen->populateSensorList(sSensorList + LOCAL_SENSORS,
184 sizeof(sSensorList[0]) * (ARRAY_SIZE(sSensorList) - LOCAL_SENSORS));
185
186 mSensors[mpl] = p_mplsen;
187 mPollFds[mpl].fd = mSensors[mpl]->getFd();
188 mPollFds[mpl].events = POLLIN;
189 mPollFds[mpl].revents = 0;
190
191 mSensors[mpl_accel] = mSensors[mpl];
192 mPollFds[mpl_accel].fd = ((MPLSensor*)mSensors[mpl])->getAccelFd();
193 mPollFds[mpl_accel].events = POLLIN;
194 mPollFds[mpl_accel].revents = 0;
195
196 mSensors[mpl_timer] = mSensors[mpl];
197 mPollFds[mpl_timer].fd = ((MPLSensor*)mSensors[mpl])->getTimerFd();
198 mPollFds[mpl_timer].events = POLLIN;
199 mPollFds[mpl_timer].revents = 0;
200
201 mSensors[light] = new LightSensor();
202 mPollFds[light].fd = mSensors[light]->getFd();
203 mPollFds[light].events = POLLIN;
204 mPollFds[light].revents = 0;
205
206 mSensors[proximity] = new ProximitySensor();
207 mPollFds[proximity].fd = mSensors[proximity]->getFd();
208 mPollFds[proximity].events = POLLIN;
209 mPollFds[proximity].revents = 0;
210
211 mSensors[pressure] = new PressureSensor();
212 mPollFds[pressure].fd = mSensors[pressure]->getFd();
213 mPollFds[pressure].events = POLLIN;
214 mPollFds[pressure].revents = 0;
215
216 int wakeFds[2];
217 int result = pipe(wakeFds);
218 LOGE_IF(result<0, "error creating wake pipe (%s)", strerror(errno));
219 fcntl(wakeFds[0], F_SETFL, O_NONBLOCK);
220 fcntl(wakeFds[1], F_SETFL, O_NONBLOCK);
221 mWritePipeFd = wakeFds[1];
222
223 mPollFds[wake].fd = wakeFds[0];
224 mPollFds[wake].events = POLLIN;
225 mPollFds[wake].revents = 0;
226
227 //setup MPL pm interaction handle
228 mPollFds[mpl_power].fd = ((MPLSensor*)mSensors[mpl])->getPowerFd();
229 mPollFds[mpl_power].events = POLLIN;
230 mPollFds[mpl_power].revents = 0;
231 }
232
~sensors_poll_context_t()233 sensors_poll_context_t::~sensors_poll_context_t()
234 {
235 FUNC_LOG;
236 for (int i=0 ; i<numSensorDrivers ; i++) {
237 delete mSensors[i];
238 }
239 close(mPollFds[wake].fd);
240 close(mWritePipeFd);
241 }
242
activate(int handle,int enabled)243 int sensors_poll_context_t::activate(int handle, int enabled)
244 {
245 FUNC_LOG;
246 int index = handleToDriver(handle);
247 if (index < 0) return index;
248 int err = mSensors[index]->enable(handle, enabled);
249 if (!err) {
250 const char wakeMessage(WAKE_MESSAGE);
251 int result = write(mWritePipeFd, &wakeMessage, 1);
252 LOGE_IF(result<0, "error sending wake message (%s)", strerror(errno));
253 }
254 return err;
255 }
256
setDelay(int handle,int64_t ns)257 int sensors_poll_context_t::setDelay(int handle, int64_t ns)
258 {
259 FUNC_LOG;
260 int index = handleToDriver(handle);
261 if (index < 0) return index;
262 return mSensors[index]->setDelay(handle, ns);
263 }
264
pollEvents(sensors_event_t * data,int count)265 int sensors_poll_context_t::pollEvents(sensors_event_t* data, int count)
266 {
267 //FUNC_LOG;
268 int nbEvents = 0;
269 int n = 0;
270 int polltime = -1;
271
272 do {
273 // see if we have some leftover from the last poll()
274 for (int i=0 ; count && i<numSensorDrivers ; i++) {
275 SensorBase* const sensor(mSensors[i]);
276 if ((mPollFds[i].revents & POLLIN) || (sensor->hasPendingEvents())) {
277 int nb = sensor->readEvents(data, count);
278 if (nb < count) {
279 // no more data for this sensor
280 mPollFds[i].revents = 0;
281 }
282 count -= nb;
283 nbEvents += nb;
284 data += nb;
285
286 //special handling for the mpl, which has multiple handles
287 if(i==mpl) {
288 i+=2; //skip accel and timer
289 mPollFds[mpl_accel].revents = 0;
290 mPollFds[mpl_timer].revents = 0;
291 }
292 if(i==mpl_accel) {
293 i+=1; //skip timer
294 mPollFds[mpl_timer].revents = 0;
295 }
296 }
297 }
298
299 if (count) {
300 // we still have some room, so try to see if we can get
301 // some events immediately or just wait if we don't have
302 // anything to return
303 int i;
304
305 n = poll(mPollFds, numFds, nbEvents ? 0 : polltime);
306 if (n<0) {
307 LOGE("poll() failed (%s)", strerror(errno));
308 return -errno;
309 }
310 if (mPollFds[wake].revents & POLLIN) {
311 char msg;
312 int result = read(mPollFds[wake].fd, &msg, 1);
313 LOGE_IF(result<0, "error reading from wake pipe (%s)", strerror(errno));
314 LOGE_IF(msg != WAKE_MESSAGE, "unknown message on wake queue (0x%02x)", int(msg));
315 mPollFds[wake].revents = 0;
316 }
317 if(mPollFds[mpl_power].revents & POLLIN) {
318 ((MPLSensor*)mSensors[mpl])->handlePowerEvent();
319 mPollFds[mpl_power].revents = 0;
320 }
321 }
322 // if we have events and space, go read them
323 } while (n && count);
324
325 return nbEvents;
326 }
327
328 /*****************************************************************************/
329
poll__close(struct hw_device_t * dev)330 static int poll__close(struct hw_device_t *dev)
331 {
332 FUNC_LOG;
333 sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
334 if (ctx) {
335 delete ctx;
336 }
337 return 0;
338 }
339
poll__activate(struct sensors_poll_device_t * dev,int handle,int enabled)340 static int poll__activate(struct sensors_poll_device_t *dev,
341 int handle, int enabled)
342 {
343 FUNC_LOG;
344 sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
345 return ctx->activate(handle, enabled);
346 }
347
poll__setDelay(struct sensors_poll_device_t * dev,int handle,int64_t ns)348 static int poll__setDelay(struct sensors_poll_device_t *dev,
349 int handle, int64_t ns)
350 {
351 FUNC_LOG;
352 sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
353 return ctx->setDelay(handle, ns);
354 }
355
poll__poll(struct sensors_poll_device_t * dev,sensors_event_t * data,int count)356 static int poll__poll(struct sensors_poll_device_t *dev,
357 sensors_event_t* data, int count)
358 {
359 FUNC_LOG;
360 sensors_poll_context_t *ctx = (sensors_poll_context_t *)dev;
361 return ctx->pollEvents(data, count);
362 }
363
364 /*****************************************************************************/
365
366 /** Open a new instance of a sensor device using name */
open_sensors(const struct hw_module_t * module,const char * id,struct hw_device_t ** device)367 static int open_sensors(const struct hw_module_t* module, const char* id,
368 struct hw_device_t** device)
369 {
370 FUNC_LOG;
371 int status = -EINVAL;
372 sensors_poll_context_t *dev = new sensors_poll_context_t();
373
374 memset(&dev->device, 0, sizeof(sensors_poll_device_t));
375
376 dev->device.common.tag = HARDWARE_DEVICE_TAG;
377 dev->device.common.version = 0;
378 dev->device.common.module = const_cast<hw_module_t*>(module);
379 dev->device.common.close = poll__close;
380 dev->device.activate = poll__activate;
381 dev->device.setDelay = poll__setDelay;
382 dev->device.poll = poll__poll;
383
384 *device = &dev->device.common;
385 status = 0;
386
387 return status;
388 }
389
390
391