OSDN Git Service

Winter cleanup: comment harmonization, space shuffling, etc.
[android-x86/hardware-intel-libsensors.git] / common.h
index a868d82..3a2da16 100644 (file)
--- a/common.h
+++ b/common.h
@@ -8,11 +8,11 @@
 #define MAX_DEVICES    8       /* Check iio devices 0 to MAX_DEVICES-1 */
 #define MAX_SENSORS    10      /* We can handle as many sensors */
 #define MAX_CHANNELS   4       /* We can handle as many channels per sensor */
+#define MAX_TRIGGERS   8       /* Check for triggers 0 to MAX_TRIGGERS-1 */
 
-#define DEV_FILE_PATH  "/dev/iio:device%d"
-
-#define BASE_PATH      "/sys/bus/iio/devices/iio:device%d/"
-
+#define DEV_FILE_PATH          "/dev/iio:device%d"
+#define BASE_PATH              "/sys/bus/iio/devices/iio:device%d/"
+#define TRIGGER_FILE_PATH      "/sys/bus/iio/devices/trigger%d/name"
 
 #define CHANNEL_PATH           BASE_PATH "scan_elements/"
 #define ENABLE_PATH            BASE_PATH "buffer/enable"
 
 #define MAX_TYPE_SPEC_LEN 32   /* Channel type spec len; ex: "le:u10/16>>0" */
 #define MAX_SENSOR_REPORT_SIZE 32      /* Sensor report buffer size */
+#define MAX_DEVICE_REPORT_SIZE 32      /* iio device scan buffer size */
 
 #define MAX_NAME_SIZE          32
 
-#define ARRAY_SIZE(x) sizeof(x)/sizeof(x[0])
+#define MAX_REAL_DEP 3 /* Max number of base sensors a sensor can depend on */
 
+#define ARRAY_SIZE(x) sizeof(x)/sizeof(x[0])
+#define REPORTING_MODE(x)      ((x) & 0x06)
+
+#ifdef __LP64__
+       typedef uint64_t flag_t;
+       typedef int64_t max_delay_t;
+#else
+       typedef uint32_t flag_t;
+       typedef int32_t max_delay_t;
+#endif
 
 struct channel_descriptor_t
 {
@@ -55,6 +66,7 @@ struct sensor_catalog_entry_t
        const char *tag; /* Prefix such as "accel", "gyro", "temp"... */
        const int type;  /* Sensor type ; ex: SENSOR_TYPE_ACCELEROMETER */
        const int num_channels; /* Expected iio channels for this sensor */
+       const int is_virtual;  /* Is the sensor virtual or not */
        struct channel_descriptor_t channel[MAX_CHANNELS];
 };
 
@@ -90,26 +102,47 @@ struct sample_ops_t
        int (*finalize)(int s, struct sensors_event_t* data);
 };
 
+/*
+ * Whenever we have sensor data recorded for a sensor in the associated
+ * sensor cell, its report_pending field is set to a non-zero value
+ * indicating how we got this data.
+ */
+#define DATA_TRIGGER   1       /* From /dev/iio:device fd              */
+#define DATA_SYSFS     2       /* Through polling                      */
+#define DATA_DUPLICATE 3       /* Duplicate of triggered motion sample */
+
 struct sensor_info_t
 {
-       char friendly_name[MAX_NAME_SIZE];      /* ex: Accelerometer */
-       char internal_name[MAX_NAME_SIZE];      /* ex: accel_3d */
-       char vendor_name[MAX_NAME_SIZE];        /* ex: Intel */
-
+       char friendly_name[MAX_NAME_SIZE];      /* ex: Accelerometer         */
+       char internal_name[MAX_NAME_SIZE];      /* ex: accel_3d              */
+       char vendor_name[MAX_NAME_SIZE];        /* ex: Intel                 */
+       char init_trigger_name[MAX_NAME_SIZE];  /* ex: accel-name-dev1       */
+       char motion_trigger_name[MAX_NAME_SIZE];/* ex: accel-any-motion-dev1 */
        float max_range;
        float resolution;
        float power;
 
+       /*
+        * Currently active trigger - either a pointer to the initial (default)
+        * trigger name buffer, or a pointer to the motion trigger name buffer,
+        * or something else (typically NULL or a pointer to some static "\n".
+        * This is used to determine if the conditions are met to switch from
+        * the default trigger to the motion trigger for a sensor, or rather for
+        * the interrupt-driven sensors associated to a given iio device.
+        */
+       const char* selected_trigger;
+
        float offset;   /* (cooked = raw + offset) * scale */
-       float scale;    /*default: 1. when set to 0, use channel specific value*/
+       float scale;    /*default:1. when set to 0, use channel specific value*/
        float illumincalib;     /* to set the calibration for the ALS */
 
-       float sampling_rate;    /* requested events / second */
+       float requested_rate;   /* requested events / second */
+       float sampling_rate;    /* setup events / second */
 
        int dev_num;    /* Associated iio dev num, ex: 3 for /dev/iio:device3 */
-       int enable_count;
 
        int catalog_index;/* Associated entry within the sensor_catalog array */
+       int type;         /* Sensor type, such as SENSOR_TYPE_GYROSCOPE       */
 
        int num_channels; /* Actual channel count ; 0 for poll mode sensors */
 
@@ -126,11 +159,17 @@ struct sensor_info_t
        /*
         * This flag is set if we acquired data from the sensor but did not
         * forward it to upper layers (i.e. Android) yet. If so, report_buffer
-        * contains that data.
+        * contains that data. Valid values are 0: empty, 1: normal, 2: repeat
+        * of last acquired value after timeout.
         */
        int report_pending;
 
        /*
+        * This flag is set if we have a meta data event pending
+        */
+       int meta_data_pending;
+
+       /*
         * Timestamp closely matching the date of sampling, preferably retrieved
         * from a iio channel alongside sample data. Value zero indicates that
         * we couldn't get such a closely correlated timestamp, and that one
@@ -138,12 +177,27 @@ struct sensor_info_t
         */
        int64_t report_ts;
 
+       /* Buffer containing the last generated sensor report for this sensor */
        unsigned char report_buffer[MAX_SENSOR_REPORT_SIZE];
 
+       /* Whether or not the above buffer contains data from a device report */
+       int report_initialized;
+
+       /* Channel and sample finalization callbacks for this sensor */
        struct sample_ops_t ops;
 
-       int calibrated;
-       void* cal_data;
+       int cal_level; /* 0 means not calibrated */
+
+       /*
+        * Depending on the sensor, calibration may take too much time at
+        * higher levels. Allow optional capping to a certain level.
+        */
+       int max_cal_level;
+
+       void *cal_data; /* Sensor calibration data, e.g. for magnetometer */
+
+       /* Filtering data for noisy sensors */
+       void* filter;
 
        float prev_val; /* Previously reported value, for on-change sensors */
 
@@ -157,12 +211,61 @@ struct sensor_info_t
        int thread_data_fd[2];
        pthread_t acquisition_thread;
 
+       /* How many base sensors is the sensor depending on */
+       int base_count;
+       int base_idx[MAX_REAL_DEP];
+
+       uint32_t quirks; /* Bit mask expressing the need for special tweaks */
+
        /* Note: we may have to explicitely serialize access to some fields */
+
+       int is_virtual;
+       /*
+       * Dependency count - for a real sensor how many active virtual sensors are
+       * depending on it
+       */
+       uint32_t ref_count;
+
+       /*
+        * Flag showing if a sensor was enabled directly by Android
+        */
+       uint32_t directly_enabled;
+
+       /*
+        * Current sample for a virtual sensor - when a report is ready we'll
+        * keep the data here until it's finally processed. Can be modified for
+        * more than one at a later time.
+        */
+       struct sensors_event_t sample;
+
+       /*
+        * If the QUIRK_FIELD_ORDERING bit is set in quirks, the contents of
+        * this array are used in the finalization stage to swap sample fields
+        * before transmitting them to Android ; they form a mapping between
+        * the indices of the input and output arrays: ex: 0123 is identity for
+        * a sample containing 4 fields.
+        */
+       unsigned char order[MAX_CHANNELS];
+
+       /* A few variables used for data filtering */
+       float *history;         /* Working buffer containing recorded samples */
+       float *history_sum;     /* The current sum of the history elements    */
+       int history_size;       /* Number of recorded samples                 */
+       int history_entries;    /* How many of these are initialized          */
+       int history_index;      /* Index of sample to evict next time         */
+
+       /*
+        * Event counter - will be used to check if we have a significant sample
+        * for noisy sensors. We want to make sure we do not send any wrong
+        * events before filtering kicks in. We can also use it for statistics.
+        */
+       uint64_t event_count;
 };
 
 /* Reference a few commonly used variables... */
 extern int                             sensor_count;
-extern struct sensor_info_t            sensor_info[MAX_SENSORS];
+extern struct sensor_t      sensor_desc[MAX_SENSORS];
+extern struct sensor_info_t            sensor[MAX_SENSORS];
 extern struct sensor_catalog_entry_t   sensor_catalog[];
 
 #endif