blob: 9c2aa2a813ad8e0a136b7438639bc8d52a3988ed [file] [log] [blame]
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -08001/*
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#ifndef ANDROID_SENSORS_INTERFACE_H
18#define ANDROID_SENSORS_INTERFACE_H
19
20#include <stdint.h>
21#include <sys/cdefs.h>
22#include <sys/types.h>
23
24#include <hardware/hardware.h>
Mike Lockwood21b652f2009-05-22 10:05:48 -040025#include <cutils/native_handle.h>
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -080026
27__BEGIN_DECLS
28
29/**
30 * The id of this module
31 */
32#define SENSORS_HARDWARE_MODULE_ID "sensors"
33
34/**
35 * Name of the sensors device to open
36 */
Mathias Agopianb1e212e2010-07-08 16:44:54 -070037#define SENSORS_HARDWARE_POLL "poll"
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -080038
39/**
40 * Handles must be higher than SENSORS_HANDLE_BASE and must be unique.
41 * A Handle identifies a given sensors. The handle is used to activate
42 * and/or deactivate sensors.
43 * In this version of the API there can only be 256 handles.
44 */
45#define SENSORS_HANDLE_BASE 0
46#define SENSORS_HANDLE_BITS 8
47#define SENSORS_HANDLE_COUNT (1<<SENSORS_HANDLE_BITS)
48
49
50/**
51 * Sensor types
52 */
53#define SENSOR_TYPE_ACCELEROMETER 1
54#define SENSOR_TYPE_MAGNETIC_FIELD 2
55#define SENSOR_TYPE_ORIENTATION 3
56#define SENSOR_TYPE_GYROSCOPE 4
57#define SENSOR_TYPE_LIGHT 5
58#define SENSOR_TYPE_PRESSURE 6
Mathias Agopian54f9dd02011-03-22 18:42:03 -070059#define SENSOR_TYPE_TEMPERATURE 7 // deprecated
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -080060#define SENSOR_TYPE_PROXIMITY 8
Kevin Powellb01a0432010-07-19 19:12:15 -070061#define SENSOR_TYPE_GRAVITY 9
62#define SENSOR_TYPE_LINEAR_ACCELERATION 10
63#define SENSOR_TYPE_ROTATION_VECTOR 11
Mathias Agopian54f9dd02011-03-22 18:42:03 -070064#define SENSOR_TYPE_AMBIENT_TEMPERATURE 13
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -080065
66/**
67 * Values returned by the accelerometer in various locations in the universe.
68 * all values are in SI units (m/s^2)
69 */
70
71#define GRAVITY_SUN (275.0f)
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -080072#define GRAVITY_EARTH (9.80665f)
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -080073
74/** Maximum magnetic field on Earth's surface */
75#define MAGNETIC_FIELD_EARTH_MAX (60.0f)
76
77/** Minimum magnetic field on Earth's surface */
78#define MAGNETIC_FIELD_EARTH_MIN (30.0f)
79
80
81/**
82 * status of each sensor
83 */
84
85#define SENSOR_STATUS_UNRELIABLE 0
86#define SENSOR_STATUS_ACCURACY_LOW 1
87#define SENSOR_STATUS_ACCURACY_MEDIUM 2
88#define SENSOR_STATUS_ACCURACY_HIGH 3
89
90/**
91 * Definition of the axis
92 * ----------------------
93 *
94 * This API is relative to the screen of the device in its default orientation,
95 * that is, if the device can be used in portrait or landscape, this API
96 * is only relative to the NATURAL orientation of the screen. In other words,
97 * the axis are not swapped when the device's screen orientation changes.
98 * Higher level services /may/ perform this transformation.
99 *
100 * x<0 x>0
101 * ^
102 * |
103 * +-----------+--> y>0
104 * | |
105 * | |
106 * | |
107 * | | / z<0
108 * | | /
109 * | | /
110 * O-----------+/
111 * |[] [ ] []/
112 * +----------/+ y<0
113 * /
114 * /
115 * |/ z>0 (toward the sky)
116 *
117 * O: Origin (x=0,y=0,z=0)
118 *
119 *
120 * Orientation
121 * -----------
122 *
123 * All values are angles in degrees.
124 *
Mathias Agopian66a40952010-07-22 17:11:50 -0700125 * Orientation sensors return sensor events for all 3 axes at a constant
126 * rate defined by setDelay().
127 *
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800128 * azimuth: angle between the magnetic north direction and the Y axis, around
129 * the Z axis (0<=azimuth<360).
130 * 0=North, 90=East, 180=South, 270=West
131 *
132 * pitch: Rotation around X axis (-180<=pitch<=180), with positive values when
133 * the z-axis moves toward the y-axis.
134 *
135 * roll: Rotation around Y axis (-90<=roll<=90), with positive values when
Mathias Agopian19ea59f2010-02-26 13:15:18 -0800136 * the x-axis moves towards the z-axis.
137 *
138 * Note: For historical reasons the roll angle is positive in the clockwise
139 * direction (mathematically speaking, it should be positive in the
140 * counter-clockwise direction):
141 *
142 * Z
143 * ^
144 * (+roll) .--> |
145 * / |
146 * | | roll: rotation around Y axis
147 * X <-------(.)
148 * Y
149 * note that +Y == -roll
150 *
151 *
152 *
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800153 * Note: This definition is different from yaw, pitch and roll used in aviation
154 * where the X axis is along the long side of the plane (tail to nose).
155 *
156 *
157 * Acceleration
158 * ------------
159 *
160 * All values are in SI units (m/s^2) and measure the acceleration of the
161 * device minus the force of gravity.
162 *
Mathias Agopian66a40952010-07-22 17:11:50 -0700163 * Acceleration sensors return sensor events for all 3 axes at a constant
164 * rate defined by setDelay().
165 *
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800166 * x: Acceleration minus Gx on the x-axis
167 * y: Acceleration minus Gy on the y-axis
168 * z: Acceleration minus Gz on the z-axis
169 *
170 * Examples:
171 * When the device lies flat on a table and is pushed on its left side
172 * toward the right, the x acceleration value is positive.
173 *
174 * When the device lies flat on a table, the acceleration value is +9.81,
175 * which correspond to the acceleration of the device (0 m/s^2) minus the
176 * force of gravity (-9.81 m/s^2).
177 *
178 * When the device lies flat on a table and is pushed toward the sky, the
179 * acceleration value is greater than +9.81, which correspond to the
180 * acceleration of the device (+A m/s^2) minus the force of
181 * gravity (-9.81 m/s^2).
182 *
183 *
184 * Magnetic Field
185 * --------------
186 *
187 * All values are in micro-Tesla (uT) and measure the ambient magnetic
188 * field in the X, Y and Z axis.
Mike Lockwooda2414312009-11-03 10:29:50 -0500189 *
Mathias Agopian66a40952010-07-22 17:11:50 -0700190 * Magnetic Field sensors return sensor events for all 3 axes at a constant
191 * rate defined by setDelay().
192 *
Kevin Powellb01a0432010-07-19 19:12:15 -0700193 * Gyroscope
194 * ---------
195 * All values are in radians/second and measure the rate of rotation
196 * around the X, Y and Z axis. The coordinate system is the same as is
Mathias Agopianc04e5f62010-09-14 10:53:55 -0700197 * used for the acceleration sensor. Rotation is positive in the
198 * counter-clockwise direction (right-hand rule). That is, an observer
199 * looking from some positive location on the x, y or z axis at a device
200 * positioned on the origin would report positive rotation if the device
201 * appeared to be rotating counter clockwise. Note that this is the
202 * standard mathematical definition of positive rotation and does not agree
203 * with the definition of roll given earlier.
204 * The range should at least be 17.45 rad/s (ie: ~1000 deg/s).
Kevin Powellb01a0432010-07-19 19:12:15 -0700205 *
Mike Lockwooda2414312009-11-03 10:29:50 -0500206 * Proximity
207 * ---------
208 *
209 * The distance value is measured in centimeters. Note that some proximity
210 * sensors only support a binary "close" or "far" measurement. In this case,
211 * the sensor should report its maxRange value in the "far" state and a value
212 * less than maxRange in the "near" state.
213 *
Mathias Agopian478994a2010-07-23 17:23:43 -0700214 * Proximity sensors report a value only when it changes and each time the
215 * sensor is enabled. setDelay() is ignored.
Mathias Agopian66a40952010-07-22 17:11:50 -0700216 *
Mike Lockwooda2414312009-11-03 10:29:50 -0500217 * Light
218 * -----
219 *
220 * The light sensor value is returned in SI lux units.
221 *
Mathias Agopian478994a2010-07-23 17:23:43 -0700222 * Light sensors report a value only when it changes and each time the
223 * sensor is enabled. setDelay() is ignored.
Mathias Agopian66a40952010-07-22 17:11:50 -0700224 *
Mathias Agopian1832f552010-07-29 15:22:30 -0700225 * Pressure
226 * --------
227 *
228 * The pressure sensor value is returned in hectopascal (hPa)
229 *
230 * Pressure sensors report events at a constant rate defined by setDelay().
231 *
Makarand Karvekar3120b582010-08-11 15:10:10 -0700232 * Gyro
233 * --------
234 *
235 * The gyroscope sensor values are returned in degrees per second (dps)
236 *
237 * Gyroscope sensor report events at a constant rate defined by setDelay().
238 *
Kevin Powellb01a0432010-07-19 19:12:15 -0700239 * Gravity
240 * -------
241 * A gravity output indicates the direction of and magnitude of gravity in the devices's
242 * coordinates. On Earth, the magnitude is 9.8. Units are m/s^2. The coordinate system
243 * is the same as is used for the acceleration sensor.
Mathias Agopian42b743c2010-11-22 15:55:32 -0800244 * When the device is at rest, the output of the gravity sensor should be identical
245 * to that of the accelerometer.
Kevin Powellb01a0432010-07-19 19:12:15 -0700246 *
247 * Linear Acceleration
248 * -------------------
249 * Indicates the linear acceleration of the device in device coordinates, not including gravity.
250 * This output is essentially Acceleration - Gravity. Units are m/s^2. The coordinate system is
251 * the same as is used for the acceleration sensor.
Mathias Agopian42b743c2010-11-22 15:55:32 -0800252 * The output of the accelerometer, gravity and linear-acceleration sensors must obey the
253 * following relation:
254 *
255 * acceleration = gravity + linear-acceleration
256 *
Kevin Powellb01a0432010-07-19 19:12:15 -0700257 *
258 * Rotation Vector
259 * ---------------
260 * A rotation vector represents the orientation of the device as a combination
261 * of an angle and an axis, in which the device has rotated through an angle
262 * theta around an axis <x, y, z>. The three elements of the rotation vector
263 * are <x*sin(theta/2), y*sin(theta/2), z*sin(theta/2)>, such that the magnitude
264 * of the rotation vector is equal to sin(theta/2), and the direction of the
265 * rotation vector is equal to the direction of the axis of rotation. The three
266 * elements of the rotation vector are equal to the last three components of a
267 * unit quaternion <cos(theta/2), x*sin(theta/2), y*sin(theta/2), z*sin(theta/2)>.
268 * Elements of the rotation vector are unitless. The x, y, and z axis are defined
269 * in the same was as for the acceleration sensor.
Mathias Agopian42b743c2010-11-22 15:55:32 -0800270 *
Mathias Agopiand93ff972011-05-02 19:10:31 -0700271 * The reference coordinate system is defined as a direct orthonormal basis,
272 * where:
273 *
274 * - X is defined as the vector product Y.Z (It is tangential to
275 * the ground at the device's current location and roughly points East).
276 *
277 * - Y is tangential to the ground at the device's current location and
278 * points towards the magnetic North Pole.
279 *
280 * - Z points towards the sky and is perpendicular to the ground.
281 *
282 *
Mathias Agopian42b743c2010-11-22 15:55:32 -0800283 * The rotation-vector is stored as:
284 *
285 * sensors_event_t.data[0] = x*sin(theta/2)
286 * sensors_event_t.data[1] = y*sin(theta/2)
287 * sensors_event_t.data[2] = z*sin(theta/2)
288 * sensors_event_t.data[3] = cos(theta/2)
289 *
Mathias Agopian54f9dd02011-03-22 18:42:03 -0700290 * Ambient Temperature
291 * -------------------
292 *
293 * The ambient (room) temperature in degree Celsius.
294 *
295 * Temperature sensors report a value only when it changes and each time the
296 * sensor is enabled. setDelay() is ignored.
297 *
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800298 */
Kevin Powellb01a0432010-07-19 19:12:15 -0700299
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800300typedef struct {
301 union {
302 float v[3];
303 struct {
304 float x;
305 float y;
306 float z;
307 };
308 struct {
309 float azimuth;
310 float pitch;
311 float roll;
312 };
313 };
314 int8_t status;
315 uint8_t reserved[3];
316} sensors_vec_t;
317
318/**
319 * Union of the various types of sensor data
320 * that can be returned.
321 */
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700322typedef struct sensors_event_t {
323 /* must be sizeof(struct sensors_event_t) */
324 int32_t version;
325
326 /* sensor identifier */
327 int32_t sensor;
328
329 /* sensor type */
330 int32_t type;
331
332 /* reserved */
333 int32_t reserved0;
334
335 /* time is in nanosecond */
336 int64_t timestamp;
337
338 union {
339 float data[16];
340
341 /* acceleration values are in meter per second per second (m/s^2) */
342 sensors_vec_t acceleration;
343
344 /* magnetic vector values are in micro-Tesla (uT) */
345 sensors_vec_t magnetic;
346
347 /* orientation values are in degrees */
348 sensors_vec_t orientation;
349
Mathias Agopianc04e5f62010-09-14 10:53:55 -0700350 /* gyroscope values are in rad/s */
351 sensors_vec_t gyro;
Makarand Karvekar3120b582010-08-11 15:10:10 -0700352
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700353 /* temperature is in degrees centigrade (Celsius) */
354 float temperature;
355
356 /* distance in centimeters */
357 float distance;
358
359 /* light in SI lux units */
360 float light;
Mathias Agopian1832f552010-07-29 15:22:30 -0700361
362 /* pressure in hectopascal (hPa) */
363 float pressure;
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700364 };
365 uint32_t reserved1[4];
366} sensors_event_t;
367
368
369
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800370struct sensor_t;
371
372/**
373 * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
374 * and the fields of this data structure must begin with hw_module_t
375 * followed by module specific information.
376 */
377struct sensors_module_t {
378 struct hw_module_t common;
379
380 /**
381 * Enumerate all available sensors. The list is returned in "list".
382 * @return number of sensors in the list
383 */
384 int (*get_sensors_list)(struct sensors_module_t* module,
385 struct sensor_t const** list);
386};
387
388struct sensor_t {
389 /* name of this sensors */
390 const char* name;
391 /* vendor of the hardware part */
392 const char* vendor;
393 /* version of the hardware part + driver. The value of this field is
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700394 * left to the implementation and doesn't have to be monotonically
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800395 * increasing.
396 */
397 int version;
398 /* handle that identifies this sensors. This handle is used to activate
399 * and deactivate this sensor. The value of the handle must be 8 bits
400 * in this version of the API.
401 */
402 int handle;
403 /* this sensor's type. */
404 int type;
405 /* maximaum range of this sensor's value in SI units */
406 float maxRange;
407 /* smallest difference between two values reported by this sensor */
408 float resolution;
409 /* rough estimate of this sensor's power consumption in mA */
410 float power;
Mathias Agopian1511e202010-07-29 15:33:22 -0700411 /* minimum delay allowed between events in microseconds. A value of zero
412 * means that this sensor doesn't report events at a constant rate, but
413 * rather only when a new data is available */
414 int32_t minDelay;
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800415 /* reserved fields, must be zero */
Mathias Agopian1511e202010-07-29 15:33:22 -0700416 void* reserved[8];
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800417};
418
419
420/**
421 * Every device data structure must begin with hw_device_t
422 * followed by module specific public methods and attributes.
423 */
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700424struct sensors_poll_device_t {
425 struct hw_device_t common;
426
427 /** Activate/deactivate one sensor.
428 *
429 * @param handle is the handle of the sensor to change.
430 * @param enabled set to 1 to enable, or 0 to disable the sensor.
431 *
432 * @return 0 on success, negative errno code otherwise
433 */
434 int (*activate)(struct sensors_poll_device_t *dev,
435 int handle, int enabled);
436
437 /**
Mathias Agopian1511e202010-07-29 15:33:22 -0700438 * Set the delay between sensor events in nanoseconds for a given sensor.
439 * It is an error to set a delay inferior to the value defined by
440 * sensor_t::minDelay. If sensor_t::minDelay is zero, setDelay() is
441 * ignored and returns 0.
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700442 *
443 * @return 0 if successful, < 0 on error
444 */
445 int (*setDelay)(struct sensors_poll_device_t *dev,
446 int handle, int64_t ns);
447
448 /**
449 * Returns an array of sensor data.
Mathias Agopian1511e202010-07-29 15:33:22 -0700450 * This function must block until events are available.
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700451 *
452 * @return the number of events read on success, or -errno in case of an error.
Mathias Agopian1511e202010-07-29 15:33:22 -0700453 * This function should never return 0 (no event).
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700454 *
455 */
456 int (*poll)(struct sensors_poll_device_t *dev,
Mathias Agopiancdefccd2010-07-15 18:29:03 -0700457 sensors_event_t* data, int count);
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700458};
459
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800460/** convenience API for opening and closing a device */
461
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700462static inline int sensors_open(const struct hw_module_t* module,
463 struct sensors_poll_device_t** device) {
464 return module->methods->open(module,
465 SENSORS_HARDWARE_POLL, (struct hw_device_t**)device);
466}
467
468static inline int sensors_close(struct sensors_poll_device_t* device) {
469 return device->common.close(&device->common);
470}
471
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800472__END_DECLS
473
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800474#endif // ANDROID_SENSORS_INTERFACE_H