blob: 87e9b39eebbdfbbd834366e38d48cf563e810255 [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 */
37#define SENSORS_HARDWARE_CONTROL "control"
38#define SENSORS_HARDWARE_DATA "data"
Mathias Agopianb1e212e2010-07-08 16:44:54 -070039#define SENSORS_HARDWARE_POLL "poll"
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -080040
41/**
42 * Handles must be higher than SENSORS_HANDLE_BASE and must be unique.
43 * A Handle identifies a given sensors. The handle is used to activate
44 * and/or deactivate sensors.
45 * In this version of the API there can only be 256 handles.
46 */
47#define SENSORS_HANDLE_BASE 0
48#define SENSORS_HANDLE_BITS 8
49#define SENSORS_HANDLE_COUNT (1<<SENSORS_HANDLE_BITS)
50
51
52/**
53 * Sensor types
54 */
55#define SENSOR_TYPE_ACCELEROMETER 1
56#define SENSOR_TYPE_MAGNETIC_FIELD 2
57#define SENSOR_TYPE_ORIENTATION 3
58#define SENSOR_TYPE_GYROSCOPE 4
59#define SENSOR_TYPE_LIGHT 5
60#define SENSOR_TYPE_PRESSURE 6
61#define SENSOR_TYPE_TEMPERATURE 7
62#define SENSOR_TYPE_PROXIMITY 8
63
64/**
65 * Values returned by the accelerometer in various locations in the universe.
66 * all values are in SI units (m/s^2)
67 */
68
69#define GRAVITY_SUN (275.0f)
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -080070#define GRAVITY_EARTH (9.80665f)
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -080071
72/** Maximum magnetic field on Earth's surface */
73#define MAGNETIC_FIELD_EARTH_MAX (60.0f)
74
75/** Minimum magnetic field on Earth's surface */
76#define MAGNETIC_FIELD_EARTH_MIN (30.0f)
77
78
79/**
80 * status of each sensor
81 */
82
83#define SENSOR_STATUS_UNRELIABLE 0
84#define SENSOR_STATUS_ACCURACY_LOW 1
85#define SENSOR_STATUS_ACCURACY_MEDIUM 2
86#define SENSOR_STATUS_ACCURACY_HIGH 3
87
88/**
89 * Definition of the axis
90 * ----------------------
91 *
92 * This API is relative to the screen of the device in its default orientation,
93 * that is, if the device can be used in portrait or landscape, this API
94 * is only relative to the NATURAL orientation of the screen. In other words,
95 * the axis are not swapped when the device's screen orientation changes.
96 * Higher level services /may/ perform this transformation.
97 *
98 * x<0 x>0
99 * ^
100 * |
101 * +-----------+--> y>0
102 * | |
103 * | |
104 * | |
105 * | | / z<0
106 * | | /
107 * | | /
108 * O-----------+/
109 * |[] [ ] []/
110 * +----------/+ y<0
111 * /
112 * /
113 * |/ z>0 (toward the sky)
114 *
115 * O: Origin (x=0,y=0,z=0)
116 *
117 *
118 * Orientation
119 * -----------
120 *
121 * All values are angles in degrees.
122 *
123 * azimuth: angle between the magnetic north direction and the Y axis, around
124 * the Z axis (0<=azimuth<360).
125 * 0=North, 90=East, 180=South, 270=West
126 *
127 * pitch: Rotation around X axis (-180<=pitch<=180), with positive values when
128 * the z-axis moves toward the y-axis.
129 *
130 * roll: Rotation around Y axis (-90<=roll<=90), with positive values when
Mathias Agopian19ea59f2010-02-26 13:15:18 -0800131 * the x-axis moves towards the z-axis.
132 *
133 * Note: For historical reasons the roll angle is positive in the clockwise
134 * direction (mathematically speaking, it should be positive in the
135 * counter-clockwise direction):
136 *
137 * Z
138 * ^
139 * (+roll) .--> |
140 * / |
141 * | | roll: rotation around Y axis
142 * X <-------(.)
143 * Y
144 * note that +Y == -roll
145 *
146 *
147 *
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800148 * Note: This definition is different from yaw, pitch and roll used in aviation
149 * where the X axis is along the long side of the plane (tail to nose).
150 *
151 *
152 * Acceleration
153 * ------------
154 *
155 * All values are in SI units (m/s^2) and measure the acceleration of the
156 * device minus the force of gravity.
157 *
158 * x: Acceleration minus Gx on the x-axis
159 * y: Acceleration minus Gy on the y-axis
160 * z: Acceleration minus Gz on the z-axis
161 *
162 * Examples:
163 * When the device lies flat on a table and is pushed on its left side
164 * toward the right, the x acceleration value is positive.
165 *
166 * When the device lies flat on a table, the acceleration value is +9.81,
167 * which correspond to the acceleration of the device (0 m/s^2) minus the
168 * force of gravity (-9.81 m/s^2).
169 *
170 * When the device lies flat on a table and is pushed toward the sky, the
171 * acceleration value is greater than +9.81, which correspond to the
172 * acceleration of the device (+A m/s^2) minus the force of
173 * gravity (-9.81 m/s^2).
174 *
175 *
176 * Magnetic Field
177 * --------------
178 *
179 * All values are in micro-Tesla (uT) and measure the ambient magnetic
180 * field in the X, Y and Z axis.
Mike Lockwooda2414312009-11-03 10:29:50 -0500181 *
182 * Proximity
183 * ---------
184 *
185 * The distance value is measured in centimeters. Note that some proximity
186 * sensors only support a binary "close" or "far" measurement. In this case,
187 * the sensor should report its maxRange value in the "far" state and a value
188 * less than maxRange in the "near" state.
189 *
190 * Light
191 * -----
192 *
193 * The light sensor value is returned in SI lux units.
194 *
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800195 */
196typedef struct {
197 union {
198 float v[3];
199 struct {
200 float x;
201 float y;
202 float z;
203 };
204 struct {
205 float azimuth;
206 float pitch;
207 float roll;
208 };
209 };
210 int8_t status;
211 uint8_t reserved[3];
212} sensors_vec_t;
213
214/**
215 * Union of the various types of sensor data
216 * that can be returned.
217 */
218typedef struct {
219 /* sensor identifier */
220 int sensor;
221
222 union {
223 /* x,y,z values of the given sensor */
224 sensors_vec_t vector;
225
226 /* orientation values are in degrees */
227 sensors_vec_t orientation;
228
229 /* acceleration values are in meter per second per second (m/s^2) */
230 sensors_vec_t acceleration;
231
232 /* magnetic vector values are in micro-Tesla (uT) */
233 sensors_vec_t magnetic;
234
235 /* temperature is in degrees centigrade (Celsius) */
236 float temperature;
Iliyan Malchevf5a22bc2009-07-08 13:54:17 -0700237
238 /* distance in centimeters */
239 float distance;
Iliyan Malchev61fee892009-08-20 16:47:17 -0700240
Mike Lockwooda2414312009-11-03 10:29:50 -0500241 /* light in SI lux units */
Mike Lockwood2bedac62009-09-04 12:09:05 -0400242 float light;
Dan Murphy2b9fcc32010-06-10 08:47:48 -0500243
244 /* pressure in hecto pascals */
245 float pressure;
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800246 };
247
248 /* time is in nanosecond */
249 int64_t time;
250
251 uint32_t reserved;
252} sensors_data_t;
253
254
255struct sensor_t;
256
257/**
258 * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
259 * and the fields of this data structure must begin with hw_module_t
260 * followed by module specific information.
261 */
262struct sensors_module_t {
263 struct hw_module_t common;
264
265 /**
266 * Enumerate all available sensors. The list is returned in "list".
267 * @return number of sensors in the list
268 */
269 int (*get_sensors_list)(struct sensors_module_t* module,
270 struct sensor_t const** list);
271};
272
273struct sensor_t {
274 /* name of this sensors */
275 const char* name;
276 /* vendor of the hardware part */
277 const char* vendor;
278 /* version of the hardware part + driver. The value of this field is
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700279 * left to the implementation and doesn't have to be monotonically
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800280 * increasing.
281 */
282 int version;
283 /* handle that identifies this sensors. This handle is used to activate
284 * and deactivate this sensor. The value of the handle must be 8 bits
285 * in this version of the API.
286 */
287 int handle;
288 /* this sensor's type. */
289 int type;
290 /* maximaum range of this sensor's value in SI units */
291 float maxRange;
292 /* smallest difference between two values reported by this sensor */
293 float resolution;
294 /* rough estimate of this sensor's power consumption in mA */
295 float power;
296 /* reserved fields, must be zero */
297 void* reserved[9];
298};
299
300
301/**
302 * Every device data structure must begin with hw_device_t
303 * followed by module specific public methods and attributes.
304 */
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700305struct sensors_poll_device_t {
306 struct hw_device_t common;
307
308 /** Activate/deactivate one sensor.
309 *
310 * @param handle is the handle of the sensor to change.
311 * @param enabled set to 1 to enable, or 0 to disable the sensor.
312 *
313 * @return 0 on success, negative errno code otherwise
314 */
315 int (*activate)(struct sensors_poll_device_t *dev,
316 int handle, int enabled);
317
318 /**
319 * Set the delay between sensor events in nanoseconds for a given sensor
320 *
321 * @return 0 if successful, < 0 on error
322 */
323 int (*setDelay)(struct sensors_poll_device_t *dev,
324 int handle, int64_t ns);
325
326 /**
327 * Returns an array of sensor data.
328 *
329 * @return the number of events read on success, or -errno in case of an error.
330 *
331 */
332 int (*poll)(struct sensors_poll_device_t *dev,
333 sensors_data_t* data, int count);
334};
335
336
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800337struct sensors_control_device_t {
338 struct hw_device_t common;
339
340 /**
Mike Lockwood21b652f2009-05-22 10:05:48 -0400341 * Returns a native_handle_t, which will be the parameter to
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800342 * sensors_data_device_t::open_data().
Mike Lockwood21b652f2009-05-22 10:05:48 -0400343 * The caller takes ownership of this handle. This is intended to be
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800344 * passed cross processes.
345 *
Bill Napier8d567482009-06-02 17:45:08 -0700346 * @return a native_handle_t if successful, NULL on error
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800347 */
Mike Lockwood21b652f2009-05-22 10:05:48 -0400348 native_handle_t* (*open_data_source)(struct sensors_control_device_t *dev);
Mike Lockwoodfbbb3472009-08-29 11:55:27 -0400349
350 /**
351 * Releases any resources that were created by open_data_source.
352 * This call is optional and can be NULL if not implemented
353 * by the sensor HAL.
354 *
355 * @return 0 if successful, < 0 on error
356 */
357 int (*close_data_source)(struct sensors_control_device_t *dev);
358
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800359 /** Activate/deactivate one sensor.
360 *
361 * @param handle is the handle of the sensor to change.
362 * @param enabled set to 1 to enable, or 0 to disable the sensor.
363 *
364 * @return 0 on success, negative errno code otherwise
365 */
366 int (*activate)(struct sensors_control_device_t *dev,
367 int handle, int enabled);
368
369 /**
370 * Set the delay between sensor events in ms
371 *
372 * @return 0 if successful, < 0 on error
373 */
374 int (*set_delay)(struct sensors_control_device_t *dev, int32_t ms);
375
376 /**
377 * Causes sensors_data_device_t.poll() to return -EWOULDBLOCK immediately.
378 */
379 int (*wake)(struct sensors_control_device_t *dev);
380};
381
382struct sensors_data_device_t {
383 struct hw_device_t common;
384
385 /**
386 * Prepare to read sensor data.
387 *
Mike Lockwood21b652f2009-05-22 10:05:48 -0400388 * This routine does NOT take ownership of the handle
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800389 * and must not close it. Typically this routine would
Mike Lockwood21b652f2009-05-22 10:05:48 -0400390 * use a duplicate of the nh parameter.
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800391 *
Mike Lockwood21b652f2009-05-22 10:05:48 -0400392 * @param nh from sensors_control_open.
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800393 *
394 * @return 0 if successful, < 0 on error
395 */
Mike Lockwood21b652f2009-05-22 10:05:48 -0400396 int (*data_open)(struct sensors_data_device_t *dev, native_handle_t* nh);
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800397
398 /**
399 * Caller has completed using the sensor data.
400 * The caller will not be blocked in sensors_data_poll
401 * when this routine is called.
402 *
403 * @return 0 if successful, < 0 on error
404 */
405 int (*data_close)(struct sensors_data_device_t *dev);
406
407 /**
408 * Return sensor data for one of the enabled sensors.
409 *
410 * @return sensor handle for the returned data, 0x7FFFFFFF when
411 * sensors_control_device_t.wake() is called and -errno on error
412 *
413 */
414 int (*poll)(struct sensors_data_device_t *dev,
415 sensors_data_t* data);
416};
417
418
419/** convenience API for opening and closing a device */
420
Mathias Agopianb1e212e2010-07-08 16:44:54 -0700421static inline int sensors_open(const struct hw_module_t* module,
422 struct sensors_poll_device_t** device) {
423 return module->methods->open(module,
424 SENSORS_HARDWARE_POLL, (struct hw_device_t**)device);
425}
426
427static inline int sensors_close(struct sensors_poll_device_t* device) {
428 return device->common.close(&device->common);
429}
430
The Android Open Source Projectf53ebec2009-03-03 19:32:14 -0800431static inline int sensors_control_open(const struct hw_module_t* module,
432 struct sensors_control_device_t** device) {
433 return module->methods->open(module,
434 SENSORS_HARDWARE_CONTROL, (struct hw_device_t**)device);
435}
436
437static inline int sensors_control_close(struct sensors_control_device_t* device) {
438 return device->common.close(&device->common);
439}
440
441static inline int sensors_data_open(const struct hw_module_t* module,
442 struct sensors_data_device_t** device) {
443 return module->methods->open(module,
444 SENSORS_HARDWARE_DATA, (struct hw_device_t**)device);
445}
446
447static inline int sensors_data_close(struct sensors_data_device_t* device) {
448 return device->common.close(&device->common);
449}
450
451
452__END_DECLS
453
454#endif // ANDROID_SENSORS_INTERFACE_H