OSDN Git Service

GMINL-2659: Keep recent events history for fusion-like processing
[android-x86/hardware-intel-libsensors.git] / common.h
1 /*
2  * Copyright (C) 2014 Intel Corporation.
3  */
4
5 #ifndef __COMMON_H__
6 #define __COMMON_H__
7
8 #define MAX_DEVICES     8       /* Check iio devices 0 to MAX_DEVICES-1 */
9 #define MAX_SENSORS     10      /* We can handle as many sensors */
10 #define MAX_CHANNELS    4       /* We can handle as many channels per sensor */
11 #define MAX_TRIGGERS    8       /* Check for triggers 0 to MAX_TRIGGERS-1 */
12
13 #define DEV_FILE_PATH           "/dev/iio:device%d"
14 #define BASE_PATH               "/sys/bus/iio/devices/iio:device%d/"
15 #define TRIGGER_FILE_PATH       "/sys/bus/iio/devices/trigger%d/name"
16
17 #define CHANNEL_PATH            BASE_PATH "scan_elements/"
18 #define ENABLE_PATH             BASE_PATH "buffer/enable"
19 #define NAME_PATH               BASE_PATH "name"
20 #define TRIGGER_PATH            BASE_PATH "trigger/current_trigger"
21 #define SENSOR_OFFSET_PATH      BASE_PATH "in_%s_offset"
22 #define SENSOR_SCALE_PATH       BASE_PATH "in_%s_scale"
23 #define SENSOR_SAMPLING_PATH    BASE_PATH "in_%s_sampling_frequency"
24 #define DEVICE_SAMPLING_PATH    BASE_PATH "sampling_frequency"
25 #define DEVICE_AVAIL_FREQ_PATH  BASE_PATH "sampling_frequency_available"
26 #define ILLUMINATION_CALIBPATH  BASE_PATH "in_illuminance_calibscale"
27
28 #define PROP_BASE       "ro.iio.%s.%s" /* Note: PROPERTY_KEY_MAX is small */
29
30 #define MAX_EVENTS 400          /* 400 Hz */
31 #define MAX_TYPE_SPEC_LEN 32    /* Channel type spec len; ex: "le:u10/16>>0" */
32 #define MAX_SENSOR_REPORT_SIZE  32      /* Sensor report buffer size */
33
34 #define MAX_NAME_SIZE           32
35
36 #define MIN_SAMPLES 5
37
38 #define ARRAY_SIZE(x) sizeof(x)/sizeof(x[0])
39 #define REPORTING_MODE(x)       ((x) & 0x06)
40
41 #ifdef __LP64__
42         typedef uint64_t flag_t;
43         typedef int64_t max_delay_t;
44 #else
45         typedef uint32_t flag_t;
46         typedef int32_t max_delay_t;
47 #endif
48
49 struct channel_descriptor_t
50 {
51         const char *name;       /* channel name ; ex: x */
52
53         /* sysfs entries located under scan_elements */
54         const char *en_path;    /* Enabled sysfs file name ; ex: "in_temp_en" */
55         const char *type_path;  /* _type sysfs file name  */
56         const char *index_path; /* _index sysfs file name */
57
58         /* sysfs entries located in /sys/bus/iio/devices/iio:deviceX */
59         const char *raw_path;   /* _raw sysfs file name  */
60         const char *input_path; /* _input sysfs file name */
61         const char *scale_path; /* _scale sysfs file name */
62 };
63
64 struct sensor_catalog_entry_t
65 {
66         const char *tag; /* Prefix such as "accel", "gyro", "temp"... */
67         const int type;  /* Sensor type ; ex: SENSOR_TYPE_ACCELEROMETER */
68         const int num_channels; /* Expected iio channels for this sensor */
69         struct channel_descriptor_t channel[MAX_CHANNELS];
70 };
71
72 struct datum_info_t
73 {
74         char sign;
75         char endianness;
76         short realbits;
77         short storagebits;
78         short shift;
79 };
80
81 struct channel_info_t
82 {
83         int offset; /* Offset in bytes within the iio character device report */
84         int size;                       /* Field size in bytes */
85         float scale;                    /* scale for each channel */
86         char type_spec[MAX_TYPE_SPEC_LEN]; /* From driver; ex: le:u10/16>>0 */
87         struct datum_info_t type_info;     /* Decoded contents of type spec */
88         float opt_scale; /* Optional correction scale read from a property such
89                           * as iio.accel.x.scale, allowing late compensation of
90                           * problems such as misconfigured axes ; set to 1 by
91                           * default. Applied at the end of the scaling process.
92                           */
93 };
94
95 struct sample_ops_t
96 {
97         /* Conversion function called once per channel */
98         float (*transform)(int s, int c, unsigned char* sample_data);
99
100         /* Function called once per sample */
101         int (*finalize)(int s, struct sensors_event_t* data);
102 };
103
104 /*
105  * Whenever we have sensor data recorded for a sensor in the associated
106  * sensor_info cell, its report_pending field is set to a non-zero value
107  * indicating how we got this data.
108  */
109 #define DATA_TRIGGER    1       /* From /dev/iio:device fd              */
110 #define DATA_SYSFS      2       /* Through polling                      */
111 #define DATA_DUPLICATE  3       /* Duplicate of triggered motion sample */
112
113 struct sensor_info_t
114 {
115         char friendly_name[MAX_NAME_SIZE];      /* ex: Accelerometer         */
116         char internal_name[MAX_NAME_SIZE];      /* ex: accel_3d              */
117         char vendor_name[MAX_NAME_SIZE];        /* ex: Intel                 */
118         char init_trigger_name[MAX_NAME_SIZE];  /* ex: accel-name-dev1       */
119         char motion_trigger_name[MAX_NAME_SIZE];/* ex: accel-any-motion-dev1 */
120         float max_range;
121         float resolution;
122         float power;
123
124         /*
125          * Currently active trigger - either a pointer to the initial (default)
126          * trigger name buffer, or a pointer to the motion trigger name buffer,
127          * or something else (typically NULL or a pointer to some static "\n".
128          * This is used to determine if the conditions are met to switch from
129          * the default trigger to the motion trigger for a sensor, or rather for
130          * the interrupt-driven sensors associated to a given iio device.
131          */
132         const char* selected_trigger;
133
134         float offset;   /* (cooked = raw + offset) * scale */
135         float scale;    /*default: 1. when set to 0, use channel specific value*/
136         float illumincalib;     /* to set the calibration for the ALS */
137
138         float sampling_rate;    /* requested events / second */
139
140         int dev_num;    /* Associated iio dev num, ex: 3 for /dev/iio:device3 */
141         int enable_count;
142
143         int catalog_index;/* Associated entry within the sensor_catalog array */
144         int type;         /* Sensor type, such as SENSOR_TYPE_GYROSCOPE       */
145
146         int num_channels; /* Actual channel count ; 0 for poll mode sensors */
147
148         /*
149          * The array below indicates where to gather report data for this
150          * sensor inside the reports that we read from the iio character device.
151          * It is updated whenever channels are enabled or disabled on the same
152          * device. Channel size indicates the size in bytes of fields, and
153          * should be zero for disabled channels. The type field indicates how a
154          * specific channel data item is structured.
155          */
156         struct channel_info_t channel[MAX_CHANNELS];
157
158         /*
159          * This flag is set if we acquired data from the sensor but did not
160          * forward it to upper layers (i.e. Android) yet. If so, report_buffer
161          * contains that data. Valid values are 0: empty, 1: normal, 2: repeat
162          * of last acquired value after timeout.
163          */
164         int report_pending;
165
166         /*
167          * This flag is set if we have a meta data event pending
168          */
169         volatile int meta_data_pending;
170
171         /*
172          * Timestamp closely matching the date of sampling, preferably retrieved
173          * from a iio channel alongside sample data. Value zero indicates that
174          * we couldn't get such a closely correlated timestamp, and that one
175          * has to be generated before the report gets sent up to Android.
176          */
177         int64_t report_ts;
178
179         /* Buffer containing the last generated sensor report for this sensor */
180         unsigned char report_buffer[MAX_SENSOR_REPORT_SIZE];
181
182         /* Whether or not the above buffer contains data from a device report */
183         int report_initialized;
184
185         struct sample_ops_t ops;
186
187         int cal_level; /* 0 means not calibrated */
188         void* cal_data;
189
190         /* Filtering data for noisy sensors */
191         void* filter;
192
193         float prev_val; /* Previously reported value, for on-change sensors */
194
195         /*
196          * Certain sensors expose their readings through sysfs files that have
197          * a long response time (100-200 ms for ALS). Rather than block our
198          * global control loop for several hundred ms each second, offload those
199          * lengthy blocking reads to dedicated threads, which will then report
200          * their data through a fd that we can add to our poll fd set.
201          */
202         int thread_data_fd[2];
203         pthread_t acquisition_thread;
204
205         /* For cal-uncal sensor pairs - index to the pair sensor in sensor_info */
206         int pair_idx;
207
208         uint32_t quirks; /* Bit mask expressing the need for special tweaks */
209
210         /* Note: we may have to explicitely serialize access to some fields */
211
212
213         /*
214          * If the QUIRK_FIELD_ORDERING bit is set in quirks, the contents of
215          * this array are used in the finalization stage to swap sample fields
216          * before transmitting them to Android ; they form a mapping between
217          * the indices of the input and output arrays: ex: 0123 is identity for
218          * a sample containing 4 fields.
219          */
220         unsigned char order[MAX_CHANNELS];
221
222         /* A few variables used for data filtering */
223         float *history;         /* Working buffer containing recorded samples */
224         float *history_sum;     /* The current sum of the history elements    */
225         int history_size;       /* Number of recorded samples                 */
226         int history_entries;    /* How many of these are initialized          */
227         int history_index;      /* Index of sample to evict next time         */
228
229         /*
230          * Event counter - will be used to check if we have a significant sample
231          * for noisy sensors. We want to make sure we do not send any wrong
232          * events before filtering kicks in. We can also use it for statistics.
233          */
234         uint64_t event_count;
235 };
236
237 /* Reference a few commonly used variables... */
238 extern int                              sensor_count;
239 extern struct sensor_t      sensor_desc[MAX_SENSORS];
240 extern struct sensor_info_t             sensor_info[MAX_SENSORS];
241 extern struct sensor_catalog_entry_t    sensor_catalog[];
242
243 /* We are required to be in sync with SystemClock.getNanos */
244 extern int64_t ts_delta;
245
246 #endif