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