OSDN Git Service

Min/Max delay for Light and Temperature
[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 800          /* 800 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 ARRAY_SIZE(x) sizeof(x)/sizeof(x[0])
37 #define REPORTING_MODE(x)       ((x) & 0x06)
38
39 #ifdef __LP64__
40         typedef uint64_t flag_t;
41         typedef int64_t max_delay_t;
42 #else
43         typedef uint32_t flag_t;
44         typedef int32_t max_delay_t;
45 #endif
46
47 struct channel_descriptor_t
48 {
49         const char *name;       /* channel name ; ex: x */
50
51         /* sysfs entries located under scan_elements */
52         const char *en_path;    /* Enabled sysfs file name ; ex: "in_temp_en" */
53         const char *type_path;  /* _type sysfs file name  */
54         const char *index_path; /* _index sysfs file name */
55
56         /* sysfs entries located in /sys/bus/iio/devices/iio:deviceX */
57         const char *raw_path;   /* _raw sysfs file name  */
58         const char *input_path; /* _input sysfs file name */
59         const char *scale_path; /* _scale sysfs file name */
60 };
61
62 struct sensor_catalog_entry_t
63 {
64         const char *tag; /* Prefix such as "accel", "gyro", "temp"... */
65         const int type;  /* Sensor type ; ex: SENSOR_TYPE_ACCELEROMETER */
66         const int num_channels; /* Expected iio channels for this sensor */
67         struct channel_descriptor_t channel[MAX_CHANNELS];
68 };
69
70 struct datum_info_t
71 {
72         char sign;
73         char endianness;
74         short realbits;
75         short storagebits;
76         short shift;
77 };
78
79 struct channel_info_t
80 {
81         int offset; /* Offset in bytes within the iio character device report */
82         int size;                       /* Field size in bytes */
83         float scale;                    /* scale for each channel */
84         char type_spec[MAX_TYPE_SPEC_LEN]; /* From driver; ex: le:u10/16>>0 */
85         struct datum_info_t type_info;     /* Decoded contents of type spec */
86         float opt_scale; /* Optional correction scale read from a property such
87                           * as iio.accel.x.scale, allowing late compensation of
88                           * problems such as misconfigured axes ; set to 1 by
89                           * default. Applied at the end of the scaling process.
90                           */
91 };
92
93 struct sample_ops_t
94 {
95         /* Conversion function called once per channel */
96         float (*transform)(int s, int c, unsigned char* sample_data);
97
98         /* Function called once per sample */
99         int (*finalize)(int s, struct sensors_event_t* data);
100 };
101
102 struct sensor_info_t
103 {
104         char friendly_name[MAX_NAME_SIZE];      /* ex: Accelerometer         */
105         char internal_name[MAX_NAME_SIZE];      /* ex: accel_3d              */
106         char vendor_name[MAX_NAME_SIZE];        /* ex: Intel                 */
107         char init_trigger_name[MAX_NAME_SIZE];  /* ex: accel-name-dev1       */
108         char motion_trigger_name[MAX_NAME_SIZE];/* ex: accel-any-motion-dev1 */
109         float max_range;
110         float resolution;
111         float power;
112
113         /*
114          * Currently active trigger - either a pointer to the initial (default)
115          * trigger name buffer, or a pointer to the motion trigger name buffer,
116          * or something else (typically NULL or a pointer to some static "\n".
117          * This is used to determine if the conditions are met to switch from
118          * the default trigger to the motion trigger for a sensor, or rather for
119          * the interrupt-driven sensors associated to a given iio device.
120          */
121         const char* selected_trigger;
122
123         float offset;   /* (cooked = raw + offset) * scale */
124         float scale;    /*default: 1. when set to 0, use channel specific value*/
125         float illumincalib;     /* to set the calibration for the ALS */
126
127         float sampling_rate;    /* requested events / second */
128
129         int dev_num;    /* Associated iio dev num, ex: 3 for /dev/iio:device3 */
130         int enable_count;
131
132         int catalog_index;/* Associated entry within the sensor_catalog array */
133
134         int num_channels; /* Actual channel count ; 0 for poll mode sensors */
135
136         /*
137          * The array below indicates where to gather report data for this
138          * sensor inside the reports that we read from the iio character device.
139          * It is updated whenever channels are enabled or disabled on the same
140          * device. Channel size indicates the size in bytes of fields, and
141          * should be zero for disabled channels. The type field indicates how a
142          * specific channel data item is structured.
143          */
144         struct channel_info_t channel[MAX_CHANNELS];
145
146         /*
147          * This flag is set if we acquired data from the sensor but did not
148          * forward it to upper layers (i.e. Android) yet. If so, report_buffer
149          * contains that data.
150          */
151         int report_pending;
152
153         /*
154          * Timestamp closely matching the date of sampling, preferably retrieved
155          * from a iio channel alongside sample data. Value zero indicates that
156          * we couldn't get such a closely correlated timestamp, and that one
157          * has to be generated before the report gets sent up to Android.
158          */
159         int64_t report_ts;
160
161         /* Buffer containing the last generated sensor report for this sensor */
162         unsigned char report_buffer[MAX_SENSOR_REPORT_SIZE];
163
164         /* Whether or not the above buffer contains data from a device report */
165         int report_initialized;
166
167         struct sample_ops_t ops;
168
169         int cal_level; /* 0 means not calibrated */
170         void* cal_data;
171
172         /* Filtering data for noisy sensors */
173         void* filter;
174
175         float prev_val; /* Previously reported value, for on-change sensors */
176
177         /*
178          * Certain sensors expose their readings through sysfs files that have
179          * a long response time (100-200 ms for ALS). Rather than block our
180          * global control loop for several hundred ms each second, offload those
181          * lengthy blocking reads to dedicated threads, which will then report
182          * their data through a fd that we can add to our poll fd set.
183          */
184         int thread_data_fd[2];
185         pthread_t acquisition_thread;
186
187         /* For cal-uncal sensor pairs - index to the pair sensor in sensor_info */
188         int pair_idx;
189
190         uint32_t quirks; /* Bit mask expressing the need for special tweaks */
191
192         /* Note: we may have to explicitely serialize access to some fields */
193
194
195         /*
196          * If the QUIRK_FIELD_ORDERING bit is set in quirks, the contents of
197          * this array are used in the finalization stage to swap sample fields
198          * before transmitting them to Android ; they form a mapping between
199          * the indices of the input and output arrays: ex: 0123 is identity for
200          * a sample containing 4 fields.
201          */
202         unsigned char order[MAX_CHANNELS];
203
204         /* A few variables used for data filtering */
205         float *history;         /* Working buffer containing recorded samples */
206         float *history_sum;     /* The current sum of the history elements    */
207         int history_size;       /* Number of recorded samples                 */
208         int history_entries;    /* How many of these are initialized          */
209         int history_index;      /* Index of sample to evict next time         */
210 };
211
212 /* Reference a few commonly used variables... */
213 extern int                              sensor_count;
214 extern struct sensor_t      sensor_desc[MAX_SENSORS];
215 extern struct sensor_info_t             sensor_info[MAX_SENSORS];
216 extern struct sensor_catalog_entry_t    sensor_catalog[];
217
218 #endif