2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef ANDROID_SENSORS_INTERFACE_H
18 #define ANDROID_SENSORS_INTERFACE_H
21 #include <sys/cdefs.h>
22 #include <sys/types.h>
24 #include <hardware/hardware.h>
25 #include <cutils/native_handle.h>
30 * The id of this module
32 #define SENSORS_HARDWARE_MODULE_ID "sensors"
35 * Name of the sensors device to open
37 #define SENSORS_HARDWARE_CONTROL "control"
38 #define SENSORS_HARDWARE_DATA "data"
41 * Handles must be higher than SENSORS_HANDLE_BASE and must be unique.
42 * A Handle identifies a given sensors. The handle is used to activate
43 * and/or deactivate sensors.
44 * In this version of the API there can only be 256 handles.
46 #define SENSORS_HANDLE_BASE 0
47 #define SENSORS_HANDLE_BITS 8
48 #define SENSORS_HANDLE_COUNT (1<<SENSORS_HANDLE_BITS)
54 #define SENSOR_TYPE_ACCELEROMETER 1
55 #define SENSOR_TYPE_MAGNETIC_FIELD 2
56 #define SENSOR_TYPE_ORIENTATION 3
57 #define SENSOR_TYPE_GYROSCOPE 4
58 #define SENSOR_TYPE_LIGHT 5
59 #define SENSOR_TYPE_PRESSURE 6
60 #define SENSOR_TYPE_TEMPERATURE 7
61 #define SENSOR_TYPE_PROXIMITY 8
64 * Values returned by the accelerometer in various locations in the universe.
65 * all values are in SI units (m/s^2)
68 #define GRAVITY_SUN (275.0f)
69 #define GRAVITY_MERCURY (3.70f)
70 #define GRAVITY_VENUS (8.87f)
71 #define GRAVITY_EARTH (9.80665f)
72 #define GRAVITY_MOON (1.6f)
73 #define GRAVITY_MARS (3.71f)
74 #define GRAVITY_JUPITER (23.12f)
75 #define GRAVITY_SATURN (8.96f)
76 #define GRAVITY_URANUS (8.69f)
77 #define GRAVITY_NEPTUNE (11.0f)
78 #define GRAVITY_PLUTO (0.6f)
79 #define GRAVITY_DEATH_STAR_I (0.000000353036145f)
80 #define GRAVITY_THE_ISLAND (4.815162342f)
82 /** Maximum magnetic field on Earth's surface */
83 #define MAGNETIC_FIELD_EARTH_MAX (60.0f)
85 /** Minimum magnetic field on Earth's surface */
86 #define MAGNETIC_FIELD_EARTH_MIN (30.0f)
90 * status of each sensor
93 #define SENSOR_STATUS_UNRELIABLE 0
94 #define SENSOR_STATUS_ACCURACY_LOW 1
95 #define SENSOR_STATUS_ACCURACY_MEDIUM 2
96 #define SENSOR_STATUS_ACCURACY_HIGH 3
99 * Definition of the axis
100 * ----------------------
102 * This API is relative to the screen of the device in its default orientation,
103 * that is, if the device can be used in portrait or landscape, this API
104 * is only relative to the NATURAL orientation of the screen. In other words,
105 * the axis are not swapped when the device's screen orientation changes.
106 * Higher level services /may/ perform this transformation.
111 * +-----------+--> y>0
123 * |/ z>0 (toward the sky)
125 * O: Origin (x=0,y=0,z=0)
131 * All values are angles in degrees.
133 * azimuth: angle between the magnetic north direction and the Y axis, around
134 * the Z axis (0<=azimuth<360).
135 * 0=North, 90=East, 180=South, 270=West
137 * pitch: Rotation around X axis (-180<=pitch<=180), with positive values when
138 * the z-axis moves toward the y-axis.
140 * roll: Rotation around Y axis (-90<=roll<=90), with positive values when
141 * the x-axis moves towards the z-axis.
143 * Note: For historical reasons the roll angle is positive in the clockwise
144 * direction (mathematically speaking, it should be positive in the
145 * counter-clockwise direction):
151 * | | roll: rotation around Y axis
154 * note that +Y == -roll
158 * Note: This definition is different from yaw, pitch and roll used in aviation
159 * where the X axis is along the long side of the plane (tail to nose).
165 * All values are in SI units (m/s^2) and measure the acceleration of the
166 * device minus the force of gravity.
168 * x: Acceleration minus Gx on the x-axis
169 * y: Acceleration minus Gy on the y-axis
170 * z: Acceleration minus Gz on the z-axis
173 * When the device lies flat on a table and is pushed on its left side
174 * toward the right, the x acceleration value is positive.
176 * When the device lies flat on a table, the acceleration value is +9.81,
177 * which correspond to the acceleration of the device (0 m/s^2) minus the
178 * force of gravity (-9.81 m/s^2).
180 * When the device lies flat on a table and is pushed toward the sky, the
181 * acceleration value is greater than +9.81, which correspond to the
182 * acceleration of the device (+A m/s^2) minus the force of
183 * gravity (-9.81 m/s^2).
189 * All values are in micro-Tesla (uT) and measure the ambient magnetic
190 * field in the X, Y and Z axis.
195 * The distance value is measured in centimeters. Note that some proximity
196 * sensors only support a binary "close" or "far" measurement. In this case,
197 * the sensor should report its maxRange value in the "far" state and a value
198 * less than maxRange in the "near" state.
203 * The light sensor value is returned in SI lux units.
225 * Union of the various types of sensor data
226 * that can be returned.
229 /* sensor identifier */
233 /* x,y,z values of the given sensor */
234 sensors_vec_t vector;
236 /* orientation values are in degrees */
237 sensors_vec_t orientation;
239 /* acceleration values are in meter per second per second (m/s^2) */
240 sensors_vec_t acceleration;
242 /* magnetic vector values are in micro-Tesla (uT) */
243 sensors_vec_t magnetic;
245 /* temperature is in degrees centigrade (Celsius) */
248 /* distance in centimeters */
251 /* light in SI lux units */
255 /* time is in nanosecond */
265 * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
266 * and the fields of this data structure must begin with hw_module_t
267 * followed by module specific information.
269 struct sensors_module_t {
270 struct hw_module_t common;
273 * Enumerate all available sensors. The list is returned in "list".
274 * @return number of sensors in the list
276 int (*get_sensors_list)(struct sensors_module_t* module,
277 struct sensor_t const** list);
281 /* name of this sensors */
283 /* vendor of the hardware part */
285 /* version of the hardware part + driver. The value of this field is
286 * left to the implementation and doesn't have to be monotonicaly
290 /* handle that identifies this sensors. This handle is used to activate
291 * and deactivate this sensor. The value of the handle must be 8 bits
292 * in this version of the API.
295 /* this sensor's type. */
297 /* maximaum range of this sensor's value in SI units */
299 /* smallest difference between two values reported by this sensor */
301 /* rough estimate of this sensor's power consumption in mA */
303 /* reserved fields, must be zero */
309 * Every device data structure must begin with hw_device_t
310 * followed by module specific public methods and attributes.
312 struct sensors_control_device_t {
313 struct hw_device_t common;
316 * Returns a native_handle_t, which will be the parameter to
317 * sensors_data_device_t::open_data().
318 * The caller takes ownership of this handle. This is intended to be
319 * passed cross processes.
321 * @return a native_handle_t if successful, NULL on error
323 native_handle_t* (*open_data_source)(struct sensors_control_device_t *dev);
326 * Releases any resources that were created by open_data_source.
327 * This call is optional and can be NULL if not implemented
330 * @return 0 if successful, < 0 on error
332 int (*close_data_source)(struct sensors_control_device_t *dev);
334 /** Activate/deactivate one sensor.
336 * @param handle is the handle of the sensor to change.
337 * @param enabled set to 1 to enable, or 0 to disable the sensor.
339 * @return 0 on success, negative errno code otherwise
341 int (*activate)(struct sensors_control_device_t *dev,
342 int handle, int enabled);
345 * Set the delay between sensor events in ms
347 * @return 0 if successful, < 0 on error
349 int (*set_delay)(struct sensors_control_device_t *dev, int32_t ms);
352 * Causes sensors_data_device_t.poll() to return -EWOULDBLOCK immediately.
354 int (*wake)(struct sensors_control_device_t *dev);
357 struct sensors_data_device_t {
358 struct hw_device_t common;
361 * Prepare to read sensor data.
363 * This routine does NOT take ownership of the handle
364 * and must not close it. Typically this routine would
365 * use a duplicate of the nh parameter.
367 * @param nh from sensors_control_open.
369 * @return 0 if successful, < 0 on error
371 int (*data_open)(struct sensors_data_device_t *dev, native_handle_t* nh);
374 * Caller has completed using the sensor data.
375 * The caller will not be blocked in sensors_data_poll
376 * when this routine is called.
378 * @return 0 if successful, < 0 on error
380 int (*data_close)(struct sensors_data_device_t *dev);
383 * Return sensor data for one of the enabled sensors.
385 * @return sensor handle for the returned data, 0x7FFFFFFF when
386 * sensors_control_device_t.wake() is called and -errno on error
389 int (*poll)(struct sensors_data_device_t *dev,
390 sensors_data_t* data);
394 /** convenience API for opening and closing a device */
396 static inline int sensors_control_open(const struct hw_module_t* module,
397 struct sensors_control_device_t** device) {
398 return module->methods->open(module,
399 SENSORS_HARDWARE_CONTROL, (struct hw_device_t**)device);
402 static inline int sensors_control_close(struct sensors_control_device_t* device) {
403 return device->common.close(&device->common);
406 static inline int sensors_data_open(const struct hw_module_t* module,
407 struct sensors_data_device_t** device) {
408 return module->methods->open(module,
409 SENSORS_HARDWARE_DATA, (struct hw_device_t**)device);
412 static inline int sensors_data_close(struct sensors_data_device_t* device) {
413 return device->common.close(&device->common);
419 #endif // ANDROID_SENSORS_INTERFACE_H