sensors.h revision b01a043df41e987321f6c01930ac306befb1d3dc
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_CONTROL "control" 38#define SENSORS_HARDWARE_DATA "data" 39 40/** 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. 45 */ 46#define SENSORS_HANDLE_BASE 0 47#define SENSORS_HANDLE_BITS 8 48#define SENSORS_HANDLE_COUNT (1<<SENSORS_HANDLE_BITS) 49 50 51/** 52 * Sensor types 53 */ 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 62#define SENSOR_TYPE_GRAVITY 9 63#define SENSOR_TYPE_LINEAR_ACCELERATION 10 64#define SENSOR_TYPE_ROTATION_VECTOR 11 65 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) 72#define GRAVITY_MERCURY (3.70f) 73#define GRAVITY_VENUS (8.87f) 74#define GRAVITY_EARTH (9.80665f) 75#define GRAVITY_MOON (1.6f) 76#define GRAVITY_MARS (3.71f) 77#define GRAVITY_JUPITER (23.12f) 78#define GRAVITY_SATURN (8.96f) 79#define GRAVITY_URANUS (8.69f) 80#define GRAVITY_NEPTUNE (11.0f) 81#define GRAVITY_PLUTO (0.6f) 82#define GRAVITY_DEATH_STAR_I (0.000000353036145f) 83#define GRAVITY_THE_ISLAND (4.815162342f) 84 85/** Maximum magnetic field on Earth's surface */ 86#define MAGNETIC_FIELD_EARTH_MAX (60.0f) 87 88/** Minimum magnetic field on Earth's surface */ 89#define MAGNETIC_FIELD_EARTH_MIN (30.0f) 90 91 92/** 93 * status of each sensor 94 */ 95 96#define SENSOR_STATUS_UNRELIABLE 0 97#define SENSOR_STATUS_ACCURACY_LOW 1 98#define SENSOR_STATUS_ACCURACY_MEDIUM 2 99#define SENSOR_STATUS_ACCURACY_HIGH 3 100 101/** 102 * Definition of the axis 103 * ---------------------- 104 * 105 * This API is relative to the screen of the device in its default orientation, 106 * that is, if the device can be used in portrait or landscape, this API 107 * is only relative to the NATURAL orientation of the screen. In other words, 108 * the axis are not swapped when the device's screen orientation changes. 109 * Higher level services /may/ perform this transformation. 110 * 111 * x<0 x>0 112 * ^ 113 * | 114 * +-----------+--> y>0 115 * | | 116 * | | 117 * | | 118 * | | / z<0 119 * | | / 120 * | | / 121 * O-----------+/ 122 * |[] [ ] []/ 123 * +----------/+ y<0 124 * / 125 * / 126 * |/ z>0 (toward the sky) 127 * 128 * O: Origin (x=0,y=0,z=0) 129 * 130 * 131 * Orientation 132 * ----------- 133 * 134 * All values are angles in degrees. 135 * 136 * azimuth: angle between the magnetic north direction and the Y axis, around 137 * the Z axis (0<=azimuth<360). 138 * 0=North, 90=East, 180=South, 270=West 139 * 140 * pitch: Rotation around X axis (-180<=pitch<=180), with positive values when 141 * the z-axis moves toward the y-axis. 142 * 143 * roll: Rotation around Y axis (-90<=roll<=90), with positive values when 144 * the x-axis moves towards the z-axis. 145 * 146 * Note: For historical reasons the roll angle is positive in the clockwise 147 * direction (mathematically speaking, it should be positive in the 148 * counter-clockwise direction): 149 * 150 * Z 151 * ^ 152 * (+roll) .--> | 153 * / | 154 * | | roll: rotation around Y axis 155 * X <-------(.) 156 * Y 157 * note that +Y == -roll 158 * 159 * 160 * 161 * Note: This definition is different from yaw, pitch and roll used in aviation 162 * where the X axis is along the long side of the plane (tail to nose). 163 * 164 * 165 * Acceleration 166 * ------------ 167 * 168 * All values are in SI units (m/s^2) and measure the acceleration of the 169 * device minus the force of gravity. 170 * 171 * x: Acceleration minus Gx on the x-axis 172 * y: Acceleration minus Gy on the y-axis 173 * z: Acceleration minus Gz on the z-axis 174 * 175 * Examples: 176 * When the device lies flat on a table and is pushed on its left side 177 * toward the right, the x acceleration value is positive. 178 * 179 * When the device lies flat on a table, the acceleration value is +9.81, 180 * which correspond to the acceleration of the device (0 m/s^2) minus the 181 * force of gravity (-9.81 m/s^2). 182 * 183 * When the device lies flat on a table and is pushed toward the sky, the 184 * acceleration value is greater than +9.81, which correspond to the 185 * acceleration of the device (+A m/s^2) minus the force of 186 * gravity (-9.81 m/s^2). 187 * 188 * 189 * Magnetic Field 190 * -------------- 191 * 192 * All values are in micro-Tesla (uT) and measure the ambient magnetic 193 * field in the X, Y and Z axis. 194 * 195 * Gyroscope 196 * --------- 197 * All values are in radians/second and measure the rate of rotation 198 * around the X, Y and Z axis. The coordinate system is the same as is 199 * used for the acceleration sensor. Rotation is positive in the counter-clockwise 200 * direction. That is, an observer looking from some positive location on the x, y. 201 * or z axis at a device positioned on the origin would report positive rotation 202 * if the device appeared to be rotating counter clockwise. Note that this is the 203 * standard mathematical definition of positive rotation and does not agree with the 204 * definition of roll given earlier. 205 * 206 * 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 * 214 * Light 215 * ----- 216 * 217 * The light sensor value is returned in SI lux units. 218 * 219 * Gravity 220 * ------- 221 * A gravity output indicates the direction of and magnitude of gravity in the devices's 222 * coordinates. On Earth, the magnitude is 9.8. Units are m/s^2. The coordinate system 223 * is the same as is used for the acceleration sensor. 224 * 225 * Linear Acceleration 226 * ------------------- 227 * Indicates the linear acceleration of the device in device coordinates, not including gravity. 228 * This output is essentially Acceleration - Gravity. Units are m/s^2. The coordinate system is 229 * the same as is used for the acceleration sensor. 230 * 231 * Rotation Vector 232 * --------------- 233 * A rotation vector represents the orientation of the device as a combination 234 * of an angle and an axis, in which the device has rotated through an angle 235 * theta around an axis <x, y, z>. The three elements of the rotation vector 236 * are <x*sin(theta/2), y*sin(theta/2), z*sin(theta/2)>, such that the magnitude 237 * of the rotation vector is equal to sin(theta/2), and the direction of the 238 * rotation vector is equal to the direction of the axis of rotation. The three 239 * elements of the rotation vector are equal to the last three components of a 240 * unit quaternion <cos(theta/2), x*sin(theta/2), y*sin(theta/2), z*sin(theta/2)>. 241 * Elements of the rotation vector are unitless. The x, y, and z axis are defined 242 * in the same was as for the acceleration sensor. 243 */ 244 245typedef struct { 246 union { 247 float v[3]; 248 struct { 249 float x; 250 float y; 251 float z; 252 }; 253 struct { 254 float azimuth; 255 float pitch; 256 float roll; 257 }; 258 }; 259 int8_t status; 260 uint8_t reserved[3]; 261} sensors_vec_t; 262 263/** 264 * Union of the various types of sensor data 265 * that can be returned. 266 */ 267typedef struct { 268 /* sensor identifier */ 269 int sensor; 270 271 union { 272 /* x,y,z values of the given sensor */ 273 sensors_vec_t vector; 274 275 /* orientation values are in degrees */ 276 sensors_vec_t orientation; 277 278 /* acceleration values are in meter per second per second (m/s^2) */ 279 sensors_vec_t acceleration; 280 281 /* magnetic vector values are in micro-Tesla (uT) */ 282 sensors_vec_t magnetic; 283 284 /* temperature is in degrees centigrade (Celsius) */ 285 float temperature; 286 287 /* distance in centimeters */ 288 float distance; 289 290 /* light in SI lux units */ 291 float light; 292 }; 293 294 /* time is in nanosecond */ 295 int64_t time; 296 297 uint32_t reserved; 298} sensors_data_t; 299 300 301struct sensor_t; 302 303/** 304 * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM 305 * and the fields of this data structure must begin with hw_module_t 306 * followed by module specific information. 307 */ 308struct sensors_module_t { 309 struct hw_module_t common; 310 311 /** 312 * Enumerate all available sensors. The list is returned in "list". 313 * @return number of sensors in the list 314 */ 315 int (*get_sensors_list)(struct sensors_module_t* module, 316 struct sensor_t const** list); 317}; 318 319struct sensor_t { 320 /* name of this sensors */ 321 const char* name; 322 /* vendor of the hardware part */ 323 const char* vendor; 324 /* version of the hardware part + driver. The value of this field is 325 * left to the implementation and doesn't have to be monotonicaly 326 * increasing. 327 */ 328 int version; 329 /* handle that identifies this sensors. This handle is used to activate 330 * and deactivate this sensor. The value of the handle must be 8 bits 331 * in this version of the API. 332 */ 333 int handle; 334 /* this sensor's type. */ 335 int type; 336 /* maximaum range of this sensor's value in SI units */ 337 float maxRange; 338 /* smallest difference between two values reported by this sensor */ 339 float resolution; 340 /* rough estimate of this sensor's power consumption in mA */ 341 float power; 342 /* reserved fields, must be zero */ 343 void* reserved[9]; 344}; 345 346 347/** 348 * Every device data structure must begin with hw_device_t 349 * followed by module specific public methods and attributes. 350 */ 351struct sensors_control_device_t { 352 struct hw_device_t common; 353 354 /** 355 * Returns a native_handle_t, which will be the parameter to 356 * sensors_data_device_t::open_data(). 357 * The caller takes ownership of this handle. This is intended to be 358 * passed cross processes. 359 * 360 * @return a native_handle_t if successful, NULL on error 361 */ 362 native_handle_t* (*open_data_source)(struct sensors_control_device_t *dev); 363 364 /** 365 * Releases any resources that were created by open_data_source. 366 * This call is optional and can be NULL if not implemented 367 * by the sensor HAL. 368 * 369 * @return 0 if successful, < 0 on error 370 */ 371 int (*close_data_source)(struct sensors_control_device_t *dev); 372 373 /** Activate/deactivate one sensor. 374 * 375 * @param handle is the handle of the sensor to change. 376 * @param enabled set to 1 to enable, or 0 to disable the sensor. 377 * 378 * @return 0 on success, negative errno code otherwise 379 */ 380 int (*activate)(struct sensors_control_device_t *dev, 381 int handle, int enabled); 382 383 /** 384 * Set the delay between sensor events in ms 385 * 386 * @return 0 if successful, < 0 on error 387 */ 388 int (*set_delay)(struct sensors_control_device_t *dev, int32_t ms); 389 390 /** 391 * Causes sensors_data_device_t.poll() to return -EWOULDBLOCK immediately. 392 */ 393 int (*wake)(struct sensors_control_device_t *dev); 394}; 395 396struct sensors_data_device_t { 397 struct hw_device_t common; 398 399 /** 400 * Prepare to read sensor data. 401 * 402 * This routine does NOT take ownership of the handle 403 * and must not close it. Typically this routine would 404 * use a duplicate of the nh parameter. 405 * 406 * @param nh from sensors_control_open. 407 * 408 * @return 0 if successful, < 0 on error 409 */ 410 int (*data_open)(struct sensors_data_device_t *dev, native_handle_t* nh); 411 412 /** 413 * Caller has completed using the sensor data. 414 * The caller will not be blocked in sensors_data_poll 415 * when this routine is called. 416 * 417 * @return 0 if successful, < 0 on error 418 */ 419 int (*data_close)(struct sensors_data_device_t *dev); 420 421 /** 422 * Return sensor data for one of the enabled sensors. 423 * 424 * @return sensor handle for the returned data, 0x7FFFFFFF when 425 * sensors_control_device_t.wake() is called and -errno on error 426 * 427 */ 428 int (*poll)(struct sensors_data_device_t *dev, 429 sensors_data_t* data); 430}; 431 432 433/** convenience API for opening and closing a device */ 434 435static inline int sensors_control_open(const struct hw_module_t* module, 436 struct sensors_control_device_t** device) { 437 return module->methods->open(module, 438 SENSORS_HARDWARE_CONTROL, (struct hw_device_t**)device); 439} 440 441static inline int sensors_control_close(struct sensors_control_device_t* device) { 442 return device->common.close(&device->common); 443} 444 445static inline int sensors_data_open(const struct hw_module_t* module, 446 struct sensors_data_device_t** device) { 447 return module->methods->open(module, 448 SENSORS_HARDWARE_DATA, (struct hw_device_t**)device); 449} 450 451static inline int sensors_data_close(struct sensors_data_device_t* device) { 452 return device->common.close(&device->common); 453} 454 455 456__END_DECLS 457 458#endif // ANDROID_SENSORS_INTERFACE_H 459