sensors.h revision 51704bed795b5b0e5e3c7b792dcdc2bf2d96a9e9
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
26__BEGIN_DECLS
27
28/**
29 * The id of this module
30 */
31#define SENSORS_HARDWARE_MODULE_ID "sensors"
32
33/**
34 * Name of the sensors device to open
35 */
36#define SENSORS_HARDWARE_CONTROL    "control"
37#define SENSORS_HARDWARE_DATA       "data"
38
39/**
40 * Handles must be higher than SENSORS_HANDLE_BASE
41 */
42#define SENSORS_HANDLE_BASE             0
43#define SENSORS_HANDLE_BITS             8
44#define SENSORS_HANDLE_COUNT            (1<<SENSORS_HANDLE_BITS)
45
46
47/**
48 * Sensor types
49 */
50#define SENSOR_TYPE_ACCELEROMETER       1
51#define SENSOR_TYPE_MAGNETIC_FIELD      2
52#define SENSOR_TYPE_ORIENTATION         3
53#define SENSOR_TYPE_GYROSCOPE           4
54#define SENSOR_TYPE_LIGHT               5
55#define SENSOR_TYPE_PRESSURE            6
56#define SENSOR_TYPE_TEMPERATURE         7
57#define SENSOR_TYPE_PROXIMITY           8
58
59/**
60 * Values returned by the accelerometer in various locations in the universe.
61 * all values are in SI units (m/s^2)
62 */
63
64#define GRAVITY_SUN             (275.0f)
65#define GRAVITY_MERCURY         (3.70f)
66#define GRAVITY_VENUS           (8.87f)
67#define GRAVITY_EARTH           (9.80665f)
68#define GRAVITY_MOON            (1.6f)
69#define GRAVITY_MARS            (3.71f)
70#define GRAVITY_JUPITER         (23.12f)
71#define GRAVITY_SATURN          (8.96f)
72#define GRAVITY_URANUS          (8.69f)
73#define GRAVITY_NEPTUNE         (11.0f)
74#define GRAVITY_PLUTO           (0.6f)
75#define GRAVITY_DEATH_STAR_I    (0.000000353036145f)
76#define GRAVITY_THE_ISLAND      (4.815162342f)
77
78/** Maximum magnetic field on Earth's surface */
79#define MAGNETIC_FIELD_EARTH_MAX    (60.0f)
80
81/** Minimum magnetic field on Earth's surface */
82#define MAGNETIC_FIELD_EARTH_MIN    (30.0f)
83
84
85/**
86 * status of each sensor
87 */
88
89#define SENSOR_STATUS_UNRELIABLE        0
90#define SENSOR_STATUS_ACCURACY_LOW      1
91#define SENSOR_STATUS_ACCURACY_MEDIUM   2
92#define SENSOR_STATUS_ACCURACY_HIGH     3
93
94/**
95 * Definition of the axis
96 * ----------------------
97 *
98 * This API is relative to the screen of the device in its default orientation,
99 * that is, if the device can be used in portrait or landscape, this API
100 * is only relative to the NATURAL orientation of the screen. In other words,
101 * the axis are not swapped when the device's screen orientation changes.
102 * Higher level services /may/ perform this transformation.
103 *
104 *   x<0         x>0
105 *                ^
106 *                |
107 *    +-----------+-->  y>0
108 *    |           |
109 *    |           |
110 *    |           |
111 *    |           |   / z<0
112 *    |           |  /
113 *    |           | /
114 *    O-----------+/
115 *    |[]  [ ]  []/
116 *    +----------/+     y<0
117 *              /
118 *             /
119 *           |/ z>0 (toward the sky)
120 *
121 *    O: Origin (x=0,y=0,z=0)
122 *
123 *
124 * Orientation
125 * -----------
126 *
127 * All values are angles in degrees.
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 z-axis moves AWAY from the x-axis.
138 *
139 * Note: This definition is different from yaw, pitch and roll used in aviation
140 *  where the X axis is along the long side of the plane (tail to nose).
141 *
142 *
143 * Acceleration
144 * ------------
145 *
146 *  All values are in SI units (m/s^2) and measure the acceleration of the
147 *  device minus the force of gravity.
148 *
149 *  x: Acceleration minus Gx on the x-axis
150 *  y: Acceleration minus Gy on the y-axis
151 *  z: Acceleration minus Gz on the z-axis
152 *
153 *  Examples:
154 *    When the device lies flat on a table and is pushed on its left side
155 *    toward the right, the x acceleration value is positive.
156 *
157 *    When the device lies flat on a table, the acceleration value is +9.81,
158 *    which correspond to the acceleration of the device (0 m/s^2) minus the
159 *    force of gravity (-9.81 m/s^2).
160 *
161 *    When the device lies flat on a table and is pushed toward the sky, the
162 *    acceleration value is greater than +9.81, which correspond to the
163 *    acceleration of the device (+A m/s^2) minus the force of
164 *    gravity (-9.81 m/s^2).
165 *
166 *
167 * Magnetic Field
168 * --------------
169 *
170 *  All values are in micro-Tesla (uT) and measure the ambient magnetic
171 *  field in the X, Y and Z axis.
172 *
173 */
174typedef struct {
175    union {
176        float v[3];
177        struct {
178            float x;
179            float y;
180            float z;
181        };
182        struct {
183            float azimuth;
184            float pitch;
185            float roll;
186        };
187    };
188    int8_t status;
189    uint8_t reserved[3];
190} sensors_vec_t;
191
192/**
193 * Union of the various types of sensor data
194 * that can be returned.
195 */
196typedef struct {
197    /* sensor identifier */
198    int             sensor;
199
200    union {
201        /* x,y,z values of the given sensor */
202        sensors_vec_t   vector;
203
204        /* orientation values are in degrees */
205        sensors_vec_t   orientation;
206
207        /* acceleration values are in meter per second per second (m/s^2) */
208        sensors_vec_t   acceleration;
209
210        /* magnetic vector values are in micro-Tesla (uT) */
211        sensors_vec_t   magnetic;
212
213        /* temperature is in degrees centigrade (Celsius) */
214        float           temperature;
215    };
216
217    /* time is in nanosecond */
218    int64_t         time;
219
220    uint32_t        reserved;
221} sensors_data_t;
222
223
224struct sensor_t;
225
226/**
227 * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
228 * and the fields of this data structure must begin with hw_module_t
229 * followed by module specific information.
230 */
231struct sensors_module_t {
232    struct hw_module_t common;
233
234    /**
235     * @return bit map of available sensors defined by
236     *         the constants SENSORS_XXXX.
237     */
238    int (*get_sensors_list)(struct sensors_module_t* module,
239            struct sensor_t const**);
240};
241
242struct sensor_t {
243    const char*     name;
244    const char*     vendor;
245    int             version;
246    int             handle;
247    int             type;
248    float           maxRange;
249    float           resolution;
250    float           power;
251    void*           reserved[9];
252};
253
254
255/**
256 * Every device data structure must begin with hw_device_t
257 * followed by module specific public methods and attributes.
258 */
259struct sensors_control_device_t {
260    struct hw_device_t common;
261
262    /**
263     * Returns the fd which will be the parameter to
264     * sensors_data_device_t::open_data().
265     * The caller takes ownership of this fd. This is intended to be
266     * passed cross processes.
267     *
268     * @return a fd if successful, < 0 on error
269     */
270    int (*open_data_source)(struct sensors_control_device_t *dev);
271
272    /** Activate/deactivate one or more of the sensors.
273     *
274     * @param sensors is the handle of the sensors to change.
275     * @param enabled set to 1 to enable, or 0 to disable the sensor.
276     *
277     * @return 0 on success, negative errno code otherwise
278     */
279    int (*activate)(struct sensors_control_device_t *dev,
280            int handle, int enabled);
281
282    /**
283     * Set the delay between sensor events in ms
284     *
285     * @return 0 if successful, < 0 on error
286     */
287    int (*set_delay)(struct sensors_control_device_t *dev, int32_t ms);
288
289    /**
290     * Causes sensors_data_device_t.poll() to return -EWOULDBLOCK immediately.
291     */
292    int (*wake)(struct sensors_control_device_t *dev);
293};
294
295struct sensors_data_device_t {
296    struct hw_device_t common;
297
298    /**
299     * Prepare to read sensor data.
300     *
301     * This routine does NOT take ownership of the fd
302     * and must not close it. Typically this routine would
303     * use a duplicate of the fd parameter.
304     *
305     * @param fd from sensors_control_open.
306     *
307     * @return 0 if successful, < 0 on error
308     */
309    int (*data_open)(struct sensors_data_device_t *dev, int fd);
310
311    /**
312     * Caller has completed using the sensor data.
313     * The caller will not be blocked in sensors_data_poll
314     * when this routine is called.
315     *
316     * @return 0 if successful, < 0 on error
317     */
318    int (*data_close)(struct sensors_data_device_t *dev);
319
320    /**
321     * Return sensor data for one of the enabled sensors.
322     *
323     * @return sensor handle for the returned data, 0x7FFFFFFF when
324     * sensors_control_device_t.wake() is called and -errno on error
325     *
326     */
327    int (*poll)(struct sensors_data_device_t *dev,
328            sensors_data_t* data);
329};
330
331
332/** convenience API for opening and closing a device */
333
334static inline int sensors_control_open(const struct hw_module_t* module,
335        struct sensors_control_device_t** device) {
336    return module->methods->open(module,
337            SENSORS_HARDWARE_CONTROL, (struct hw_device_t**)device);
338}
339
340static inline int sensors_control_close(struct sensors_control_device_t* device) {
341    return device->common.close(&device->common);
342}
343
344static inline int sensors_data_open(const struct hw_module_t* module,
345        struct sensors_data_device_t** device) {
346    return module->methods->open(module,
347            SENSORS_HARDWARE_DATA, (struct hw_device_t**)device);
348}
349
350static inline int sensors_data_close(struct sensors_data_device_t* device) {
351    return device->common.close(&device->common);
352}
353
354
355__END_DECLS
356
357#endif  // ANDROID_SENSORS_INTERFACE_H
358