• Main Page
  • Namespaces
  • Data Structures
  • Files

sensors.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008 The Android Open Source Project
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef ANDROID_SENSORS_INTERFACE_H
00018 #define ANDROID_SENSORS_INTERFACE_H
00019 
00020 #include <stdint.h>
00021 #include <sys/cdefs.h>
00022 #include <sys/types.h>
00023 
00024 #include <hardware/hardware.h>
00025 #include <cutils/native_handle.h>
00026 
00027 __BEGIN_DECLS
00028 
00029 /**
00030  * The id of this module
00031  */
00032 #define SENSORS_HARDWARE_MODULE_ID "sensors"
00033 
00034 /**
00035  * Name of the sensors device to open
00036  */
00037 #define SENSORS_HARDWARE_CONTROL    "control"
00038 #define SENSORS_HARDWARE_DATA       "data"
00039 
00040 /**
00041  * Handles must be higher than SENSORS_HANDLE_BASE and must be unique.
00042  * A Handle identifies a given sensors. The handle is used to activate
00043  * and/or deactivate sensors.
00044  * In this version of the API there can only be 256 handles.
00045  */
00046 #define SENSORS_HANDLE_BASE             0
00047 #define SENSORS_HANDLE_BITS             8
00048 #define SENSORS_HANDLE_COUNT            (1<<SENSORS_HANDLE_BITS)
00049 
00050 
00051 /**
00052  * Sensor types
00053  */
00054 #define SENSOR_TYPE_ACCELEROMETER       1
00055 #define SENSOR_TYPE_MAGNETIC_FIELD      2
00056 #define SENSOR_TYPE_ORIENTATION         3
00057 #define SENSOR_TYPE_GYROSCOPE           4
00058 #define SENSOR_TYPE_LIGHT               5
00059 #define SENSOR_TYPE_PRESSURE            6
00060 #define SENSOR_TYPE_TEMPERATURE         7
00061 #define SENSOR_TYPE_PROXIMITY           8
00062 
00063 /**
00064  * Values returned by the accelerometer in various locations in the universe.
00065  * all values are in SI units (m/s^2)
00066  */
00067 
00068 #define GRAVITY_SUN             (275.0f)
00069 #define GRAVITY_MERCURY         (3.70f)
00070 #define GRAVITY_VENUS           (8.87f)
00071 #define GRAVITY_EARTH           (9.80665f)
00072 #define GRAVITY_MOON            (1.6f)
00073 #define GRAVITY_MARS            (3.71f)
00074 #define GRAVITY_JUPITER         (23.12f)
00075 #define GRAVITY_SATURN          (8.96f)
00076 #define GRAVITY_URANUS          (8.69f)
00077 #define GRAVITY_NEPTUNE         (11.0f)
00078 #define GRAVITY_PLUTO           (0.6f)
00079 #define GRAVITY_DEATH_STAR_I    (0.000000353036145f)
00080 #define GRAVITY_THE_ISLAND      (4.815162342f)
00081 
00082 /** Maximum magnetic field on Earth's surface */
00083 #define MAGNETIC_FIELD_EARTH_MAX    (60.0f)
00084 
00085 /** Minimum magnetic field on Earth's surface */
00086 #define MAGNETIC_FIELD_EARTH_MIN    (30.0f)
00087 
00088 
00089 /**
00090  * status of each sensor
00091  */
00092 
00093 #define SENSOR_STATUS_UNRELIABLE        0
00094 #define SENSOR_STATUS_ACCURACY_LOW      1
00095 #define SENSOR_STATUS_ACCURACY_MEDIUM   2
00096 #define SENSOR_STATUS_ACCURACY_HIGH     3
00097 
00098 /**
00099  * Definition of the axis
00100  * ----------------------
00101  *
00102  * This API is relative to the screen of the device in its default orientation,
00103  * that is, if the device can be used in portrait or landscape, this API
00104  * is only relative to the NATURAL orientation of the screen. In other words,
00105  * the axis are not swapped when the device's screen orientation changes.
00106  * Higher level services /may/ perform this transformation.
00107  *
00108  *   x<0         x>0
00109  *                ^
00110  *                |
00111  *    +-----------+-->  y>0
00112  *    |           |
00113  *    |           |
00114  *    |           |
00115  *    |           |   / z<0
00116  *    |           |  /
00117  *    |           | /
00118  *    O-----------+/
00119  *    |[]  [ ]  []/
00120  *    +----------/+     y<0
00121  *              /
00122  *             /
00123  *           |/ z>0 (toward the sky)
00124  *
00125  *    O: Origin (x=0,y=0,z=0)
00126  *
00127  *
00128  * Orientation
00129  * ----------- 
00130  * 
00131  * All values are angles in degrees.
00132  * 
00133  * azimuth: angle between the magnetic north direction and the Y axis, around 
00134  *  the Z axis (0<=azimuth<360).
00135  *      0=North, 90=East, 180=South, 270=West
00136  * 
00137  * pitch: Rotation around X axis (-180<=pitch<=180), with positive values when
00138  *  the z-axis moves toward the y-axis.
00139  *
00140  * roll: Rotation around Y axis (-90<=roll<=90), with positive values when
00141  *  the x-axis moves AWAY from the z-axis.
00142  * 
00143  * Note: This definition is different from yaw, pitch and roll used in aviation
00144  *  where the X axis is along the long side of the plane (tail to nose).
00145  *  
00146  *  
00147  * Acceleration
00148  * ------------
00149  *
00150  *  All values are in SI units (m/s^2) and measure the acceleration of the
00151  *  device minus the force of gravity.
00152  *  
00153  *  x: Acceleration minus Gx on the x-axis 
00154  *  y: Acceleration minus Gy on the y-axis 
00155  *  z: Acceleration minus Gz on the z-axis
00156  *  
00157  *  Examples:
00158  *    When the device lies flat on a table and is pushed on its left side
00159  *    toward the right, the x acceleration value is positive.
00160  *    
00161  *    When the device lies flat on a table, the acceleration value is +9.81,
00162  *    which correspond to the acceleration of the device (0 m/s^2) minus the
00163  *    force of gravity (-9.81 m/s^2).
00164  *    
00165  *    When the device lies flat on a table and is pushed toward the sky, the
00166  *    acceleration value is greater than +9.81, which correspond to the
00167  *    acceleration of the device (+A m/s^2) minus the force of 
00168  *    gravity (-9.81 m/s^2).
00169  *    
00170  *    
00171  * Magnetic Field
00172  * --------------
00173  * 
00174  *  All values are in micro-Tesla (uT) and measure the ambient magnetic
00175  *  field in the X, Y and Z axis.
00176  *    
00177  */
00178 typedef struct {
00179     union {
00180         float v[3];
00181         struct {
00182             float x;
00183             float y;
00184             float z;
00185         };
00186         struct {
00187             float azimuth;
00188             float pitch;
00189             float roll;
00190         };
00191     };
00192     int8_t status;
00193     uint8_t reserved[3];
00194 } sensors_vec_t;
00195 
00196 /**
00197  * Union of the various types of sensor data
00198  * that can be returned.
00199  */
00200 typedef struct {
00201     /* sensor identifier */
00202     int             sensor;
00203 
00204     union {
00205         /* x,y,z values of the given sensor */
00206         sensors_vec_t   vector;
00207 
00208         /* orientation values are in degrees */
00209         sensors_vec_t   orientation;
00210 
00211         /* acceleration values are in meter per second per second (m/s^2) */
00212         sensors_vec_t   acceleration;
00213 
00214         /* magnetic vector values are in micro-Tesla (uT) */
00215         sensors_vec_t   magnetic;
00216 
00217         /* temperature is in degrees centigrade (Celsius) */
00218         float           temperature;
00219     };
00220 
00221     /* time is in nanosecond */
00222     int64_t         time;
00223 
00224     uint32_t        reserved;
00225 } sensors_data_t;
00226 
00227 
00228 struct sensor_t;
00229 
00230 /**
00231  * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
00232  * and the fields of this data structure must begin with hw_module_t
00233  * followed by module specific information.
00234  */
00235 struct sensors_module_t {
00236     struct hw_module_t common;
00237 
00238     /**
00239      * Enumerate all available sensors. The list is returned in "list".
00240      * @return number of sensors in the list
00241      */
00242     int (*get_sensors_list)(struct sensors_module_t* module,
00243             struct sensor_t const** list);
00244 };
00245 
00246 struct sensor_t {
00247     /* name of this sensors */
00248     const char*     name;
00249     /* vendor of the hardware part */
00250     const char*     vendor;
00251     /* version of the hardware part + driver. The value of this field is
00252      * left to the implementation and doesn't have to be monotonicaly
00253      * increasing.
00254      */    
00255     int             version;
00256     /* handle that identifies this sensors. This handle is used to activate
00257      * and deactivate this sensor. The value of the handle must be 8 bits
00258      * in this version of the API. 
00259      */
00260     int             handle;
00261     /* this sensor's type. */
00262     int             type;
00263     /* maximaum range of this sensor's value in SI units */
00264     float           maxRange;
00265     /* smallest difference between two values reported by this sensor */
00266     float           resolution;
00267     /* rough estimate of this sensor's power consumption in mA */
00268     float           power;
00269     /* reserved fields, must be zero */
00270     void*           reserved[9];
00271 };
00272 
00273 
00274 /**
00275  * Every device data structure must begin with hw_device_t
00276  * followed by module specific public methods and attributes.
00277  */
00278 struct sensors_control_device_t {
00279     struct hw_device_t common;
00280     
00281     /**
00282      * Returns a native_handle_t, which will be the parameter to
00283      * sensors_data_device_t::open_data(). 
00284      * The caller takes ownership of this handle. This is intended to be
00285      * passed cross processes.
00286      *
00287      * @return a native_handle_t if successful, NULL on error
00288      */
00289     native_handle_t* (*open_data_source)(struct sensors_control_device_t *dev);
00290     
00291     /** Activate/deactivate one sensor.
00292      *
00293      * @param handle is the handle of the sensor to change.
00294      * @param enabled set to 1 to enable, or 0 to disable the sensor.
00295      *
00296      * @return 0 on success, negative errno code otherwise
00297      */
00298     int (*activate)(struct sensors_control_device_t *dev, 
00299             int handle, int enabled);
00300     
00301     /**
00302      * Set the delay between sensor events in ms
00303      *
00304      * @return 0 if successful, < 0 on error
00305      */
00306     int (*set_delay)(struct sensors_control_device_t *dev, int32_t ms);
00307 
00308     /**
00309      * Causes sensors_data_device_t.poll() to return -EWOULDBLOCK immediately.
00310      */
00311     int (*wake)(struct sensors_control_device_t *dev);
00312 };
00313 
00314 struct sensors_data_device_t {
00315     struct hw_device_t common;
00316 
00317     /**
00318      * Prepare to read sensor data.
00319      *
00320      * This routine does NOT take ownership of the handle
00321      * and must not close it. Typically this routine would
00322      * use a duplicate of the nh parameter.
00323      *
00324      * @param nh from sensors_control_open.
00325      *
00326      * @return 0 if successful, < 0 on error
00327      */
00328     int (*data_open)(struct sensors_data_device_t *dev, native_handle_t* nh);
00329     
00330     /**
00331      * Caller has completed using the sensor data.
00332      * The caller will not be blocked in sensors_data_poll
00333      * when this routine is called.
00334      *
00335      * @return 0 if successful, < 0 on error
00336      */
00337     int (*data_close)(struct sensors_data_device_t *dev);
00338     
00339     /**
00340      * Return sensor data for one of the enabled sensors.
00341      *
00342      * @return sensor handle for the returned data, 0x7FFFFFFF when 
00343      * sensors_control_device_t.wake() is called and -errno on error
00344      *  
00345      */
00346     int (*poll)(struct sensors_data_device_t *dev, 
00347             sensors_data_t* data);
00348 };
00349 
00350 
00351 /** convenience API for opening and closing a device */
00352 
00353 static inline int sensors_control_open(const struct hw_module_t* module, 
00354         struct sensors_control_device_t** device) {
00355     return module->methods->open(module, 
00356             SENSORS_HARDWARE_CONTROL, (struct hw_device_t**)device);
00357 }
00358 
00359 static inline int sensors_control_close(struct sensors_control_device_t* device) {
00360     return device->common.close(&device->common);
00361 }
00362 
00363 static inline int sensors_data_open(const struct hw_module_t* module, 
00364         struct sensors_data_device_t** device) {
00365     return module->methods->open(module, 
00366             SENSORS_HARDWARE_DATA, (struct hw_device_t**)device);
00367 }
00368 
00369 static inline int sensors_data_close(struct sensors_data_device_t* device) {
00370     return device->common.close(&device->common);
00371 }
00372 
00373 
00374 __END_DECLS
00375 
00376 #endif  // ANDROID_SENSORS_INTERFACE_H