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 #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>
25 #include <cutils/native_handle.h>
26
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 */
37 #define SENSORS_HARDWARE_POLL "poll"
38
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
59 #define SENSOR_TYPE_TEMPERATURE 7 // deprecated
60 #define SENSOR_TYPE_PROXIMITY 8
61 #define SENSOR_TYPE_GRAVITY 9
62 #define SENSOR_TYPE_LINEAR_ACCELERATION 10
63 #define SENSOR_TYPE_ROTATION_VECTOR 11
64 #define SENSOR_TYPE_RELATIVE_HUMIDITY 12
65 #define SENSOR_TYPE_AMBIENT_TEMPERATURE 13
66
67 /**
68 * Values returned by the accelerometer in various locations in the universe.
69 * all values are in SI units (m/s^2)
70 */
71
72 #define GRAVITY_SUN (275.0f)
73 #define GRAVITY_EARTH (9.80665f)
74
75 /** Maximum magnetic field on Earth's surface */
76 #define MAGNETIC_FIELD_EARTH_MAX (60.0f)
77
78 /** Minimum magnetic field on Earth's surface */
79 #define MAGNETIC_FIELD_EARTH_MIN (30.0f)
80
81
82 /**
83 * status of each sensor
84 */
85
86 #define SENSOR_STATUS_UNRELIABLE 0
87 #define SENSOR_STATUS_ACCURACY_LOW 1
88 #define SENSOR_STATUS_ACCURACY_MEDIUM 2
89 #define SENSOR_STATUS_ACCURACY_HIGH 3
90
91 /**
92 * Definition of the axis
93 * ----------------------
94 *
95 * This API is relative to the screen of the device in its default orientation,
96 * that is, if the device can be used in portrait or landscape, this API
97 * is only relative to the NATURAL orientation of the screen. In other words,
98 * the axis are not swapped when the device's screen orientation changes.
99 * Higher level services /may/ perform this transformation.
100 *
101 * x<0 x>0
102 * ^
103 * |
104 * +-----------+--> y>0
105 * | |
106 * | |
107 * | |
108 * | | / z<0
109 * | | /
110 * | | /
111 * O-----------+/
112 * |[] [ ] []/
113 * +----------/+ y<0
114 * /
115 * /
116 * |/ z>0 (toward the sky)
117 *
118 * O: Origin (x=0,y=0,z=0)
119 *
120 *
121 * Orientation
122 * -----------
123 *
124 * All values are angles in degrees.
125 *
126 * Orientation sensors return sensor events for all 3 axes at a constant
127 * rate defined by setDelay().
128 *
129 * azimuth: angle between the magnetic north direction and the Y axis, around
130 * the Z axis (0<=azimuth<360).
131 * 0=North, 90=East, 180=South, 270=West
132 *
133 * pitch: Rotation around X axis (-180<=pitch<=180), with positive values when
134 * the z-axis moves toward the y-axis.
135 *
136 * roll: Rotation around Y axis (-90<=roll<=90), with positive values when
137 * the x-axis moves towards the z-axis.
138 *
139 * Note: For historical reasons the roll angle is positive in the clockwise
140 * direction (mathematically speaking, it should be positive in the
141 * counter-clockwise direction):
142 *
143 * Z
144 * ^
145 * (+roll) .--> |
146 * / |
147 * | | roll: rotation around Y axis
148 * X <-------(.)
149 * Y
150 * note that +Y == -roll
151 *
152 *
153 *
154 * Note: This definition is different from yaw, pitch and roll used in aviation
155 * where the X axis is along the long side of the plane (tail to nose).
156 *
157 *
158 * Acceleration
159 * ------------
160 *
161 * All values are in SI units (m/s^2) and measure the acceleration of the
162 * device minus the force of gravity.
163 *
164 * Acceleration sensors return sensor events for all 3 axes at a constant
165 * rate defined by setDelay().
166 *
167 * x: Acceleration minus Gx on the x-axis
168 * y: Acceleration minus Gy on the y-axis
169 * z: Acceleration minus Gz on the z-axis
170 *
171 * Examples:
172 * When the device lies flat on a table and is pushed on its left side
173 * toward the right, the x acceleration value is positive.
174 *
175 * When the device lies flat on a table, the acceleration value is +9.81,
176 * which correspond to the acceleration of the device (0 m/s^2) minus the
177 * force of gravity (-9.81 m/s^2).
178 *
179 * When the device lies flat on a table and is pushed toward the sky, the
180 * acceleration value is greater than +9.81, which correspond to the
181 * acceleration of the device (+A m/s^2) minus the force of
182 * gravity (-9.81 m/s^2).
183 *
184 *
185 * Magnetic Field
186 * --------------
187 *
188 * All values are in micro-Tesla (uT) and measure the ambient magnetic
189 * field in the X, Y and Z axis.
190 *
191 * Magnetic Field sensors return sensor events for all 3 axes at a constant
192 * rate defined by setDelay().
193 *
194 * Gyroscope
195 * ---------
196 * All values are in radians/second and measure the rate of rotation
197 * around the X, Y and Z axis. The coordinate system is the same as is
198 * used for the acceleration sensor. Rotation is positive in the
199 * counter-clockwise direction (right-hand rule). That is, an observer
200 * looking from some positive location on the x, y or z axis at a device
201 * positioned on the origin would report positive rotation if the device
202 * appeared to be rotating counter clockwise. Note that this is the
203 * standard mathematical definition of positive rotation and does not agree
204 * with the definition of roll given earlier.
205 * The range should at least be 17.45 rad/s (ie: ~1000 deg/s).
206 *
207 * Proximity
208 * ---------
209 *
210 * The distance value is measured in centimeters. Note that some proximity
211 * sensors only support a binary "close" or "far" measurement. In this case,
212 * the sensor should report its maxRange value in the "far" state and a value
213 * less than maxRange in the "near" state.
214 *
215 * Proximity sensors report a value only when it changes and each time the
216 * sensor is enabled. setDelay() is ignored.
217 *
218 * Light
219 * -----
220 *
221 * The light sensor value is returned in SI lux units.
222 *
223 * Light sensors report a value only when it changes and each time the
224 * sensor is enabled. setDelay() is ignored.
225 *
226 * Pressure
227 * --------
228 *
229 * The pressure sensor value is returned in hectopascal (hPa)
230 *
231 * Pressure sensors report events at a constant rate defined by setDelay().
232 *
233 * Gyro
234 * --------
235 *
236 * The gyroscope sensor values are returned in degrees per second (dps)
237 *
238 * Gyroscope sensor report events at a constant rate defined by setDelay().
239 *
240 * Gravity
241 * -------
242 * A gravity output indicates the direction of and magnitude of gravity in the devices's
243 * coordinates. On Earth, the magnitude is 9.8. Units are m/s^2. The coordinate system
244 * is the same as is used for the acceleration sensor.
245 * When the device is at rest, the output of the gravity sensor should be identical
246 * to that of the accelerometer.
247 *
248 * Linear Acceleration
249 * -------------------
250 * Indicates the linear acceleration of the device in device coordinates, not including gravity.
251 * This output is essentially Acceleration - Gravity. Units are m/s^2. The coordinate system is
252 * the same as is used for the acceleration sensor.
253 * The output of the accelerometer, gravity and linear-acceleration sensors must obey the
254 * following relation:
255 *
256 * acceleration = gravity + linear-acceleration
257 *
258 *
259 * Rotation Vector
260 * ---------------
261 * A rotation vector represents the orientation of the device as a combination
262 * of an angle and an axis, in which the device has rotated through an angle
263 * theta around an axis <x, y, z>. The three elements of the rotation vector
264 * are <x*sin(theta/2), y*sin(theta/2), z*sin(theta/2)>, such that the magnitude
265 * of the rotation vector is equal to sin(theta/2), and the direction of the
266 * rotation vector is equal to the direction of the axis of rotation. The three
267 * elements of the rotation vector are equal to the last three components of a
268 * unit quaternion <cos(theta/2), x*sin(theta/2), y*sin(theta/2), z*sin(theta/2)>.
269 * Elements of the rotation vector are unitless. The x, y, and z axis are defined
270 * in the same was as for the acceleration sensor.
271 *
272 * The reference coordinate system is defined as a direct orthonormal basis,
273 * where:
274 *
275 * - X is defined as the vector product Y.Z (It is tangential to
276 * the ground at the device's current location and roughly points East).
277 *
278 * - Y is tangential to the ground at the device's current location and
279 * points towards the magnetic North Pole.
280 *
281 * - Z points towards the sky and is perpendicular to the ground.
282 *
283 *
284 * The rotation-vector is stored as:
285 *
286 * sensors_event_t.data[0] = x*sin(theta/2)
287 * sensors_event_t.data[1] = y*sin(theta/2)
288 * sensors_event_t.data[2] = z*sin(theta/2)
289 * sensors_event_t.data[3] = cos(theta/2)
290 *
291 *
292 * Relative Humidity
293 * -----------------
294 *
295 * A relative humidity sensor measures relative ambient air humidity and
296 * returns a value in percent.
297 *
298 * Relative humidity sensors report a value only when it changes and each
299 * time the sensor is enabled. setDelay() is ignored.
300 *
301 *
302 * Ambient Temperature
303 * -------------------
304 *
305 * The ambient (room) temperature in degree Celsius.
306 *
307 * Temperature sensors report a value only when it changes and each time the
308 * sensor is enabled. setDelay() is ignored.
309 *
310 */
311
312 typedef struct {
313 union {
314 float v[3];
315 struct {
316 float x;
317 float y;
318 float z;
319 };
320 struct {
321 float azimuth;
322 float pitch;
323 float roll;
324 };
325 };
326 int8_t status;
327 uint8_t reserved[3];
328 } sensors_vec_t;
329
330 /**
331 * Union of the various types of sensor data
332 * that can be returned.
333 */
334 typedef struct sensors_event_t {
335 /* must be sizeof(struct sensors_event_t) */
336 int32_t version;
337
338 /* sensor identifier */
339 int32_t sensor;
340
341 /* sensor type */
342 int32_t type;
343
344 /* reserved */
345 int32_t reserved0;
346
347 /* time is in nanosecond */
348 int64_t timestamp;
349
350 union {
351 float data[16];
352
353 /* acceleration values are in meter per second per second (m/s^2) */
354 sensors_vec_t acceleration;
355
356 /* magnetic vector values are in micro-Tesla (uT) */
357 sensors_vec_t magnetic;
358
359 /* orientation values are in degrees */
360 sensors_vec_t orientation;
361
362 /* gyroscope values are in rad/s */
363 sensors_vec_t gyro;
364
365 /* temperature is in degrees centigrade (Celsius) */
366 float temperature;
367
368 /* distance in centimeters */
369 float distance;
370
371 /* light in SI lux units */
372 float light;
373
374 /* pressure in hectopascal (hPa) */
375 float pressure;
376
377 /* relative humidity in percent */
378 float relative_humidity;
379 };
380 uint32_t reserved1[4];
381 } sensors_event_t;
382
383
384
385 struct sensor_t;
386
387 /**
388 * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
389 * and the fields of this data structure must begin with hw_module_t
390 * followed by module specific information.
391 */
392 struct sensors_module_t {
393 struct hw_module_t common;
394
395 /**
396 * Enumerate all available sensors. The list is returned in "list".
397 * @return number of sensors in the list
398 */
399 int (*get_sensors_list)(struct sensors_module_t* module,
400 struct sensor_t const** list);
401 };
402
403 struct sensor_t {
404 /* name of this sensors */
405 const char* name;
406 /* vendor of the hardware part */
407 const char* vendor;
408 /* version of the hardware part + driver. The value of this field is
409 * left to the implementation and doesn't have to be monotonically
410 * increasing.
411 */
412 int version;
413 /* handle that identifies this sensors. This handle is used to activate
414 * and deactivate this sensor. The value of the handle must be 8 bits
415 * in this version of the API.
416 */
417 int handle;
418 /* this sensor's type. */
419 int type;
420 /* maximaum range of this sensor's value in SI units */
421 float maxRange;
422 /* smallest difference between two values reported by this sensor */
423 float resolution;
424 /* rough estimate of this sensor's power consumption in mA */
425 float power;
426 /* minimum delay allowed between events in microseconds. A value of zero
427 * means that this sensor doesn't report events at a constant rate, but
428 * rather only when a new data is available */
429 int32_t minDelay;
430 /* reserved fields, must be zero */
431 void* reserved[8];
432 };
433
434
435 /**
436 * Every device data structure must begin with hw_device_t
437 * followed by module specific public methods and attributes.
438 */
439 struct sensors_poll_device_t {
440 struct hw_device_t common;
441
442 /** Activate/deactivate one sensor.
443 *
444 * @param handle is the handle of the sensor to change.
445 * @param enabled set to 1 to enable, or 0 to disable the sensor.
446 *
447 * @return 0 on success, negative errno code otherwise
448 */
449 int (*activate)(struct sensors_poll_device_t *dev,
450 int handle, int enabled);
451
452 /**
453 * Set the delay between sensor events in nanoseconds for a given sensor.
454 * It is an error to set a delay inferior to the value defined by
455 * sensor_t::minDelay. If sensor_t::minDelay is zero, setDelay() is
456 * ignored and returns 0.
457 *
458 * @return 0 if successful, < 0 on error
459 */
460 int (*setDelay)(struct sensors_poll_device_t *dev,
461 int handle, int64_t ns);
462
463 /**
464 * Returns an array of sensor data.
465 * This function must block until events are available.
466 *
467 * @return the number of events read on success, or -errno in case of an error.
468 * This function should never return 0 (no event).
469 *
470 */
471 int (*poll)(struct sensors_poll_device_t *dev,
472 sensors_event_t* data, int count);
473 };
474
475 /** convenience API for opening and closing a device */
476
sensors_open(const struct hw_module_t * module,struct sensors_poll_device_t ** device)477 static inline int sensors_open(const struct hw_module_t* module,
478 struct sensors_poll_device_t** device) {
479 return module->methods->open(module,
480 SENSORS_HARDWARE_POLL, (struct hw_device_t**)device);
481 }
482
sensors_close(struct sensors_poll_device_t * device)483 static inline int sensors_close(struct sensors_poll_device_t* device) {
484 return device->common.close(&device->common);
485 }
486
487 __END_DECLS
488
489 #endif // ANDROID_SENSORS_INTERFACE_H
490