OSDN Git Service

iio-sensors: Add suport for STEP_DETECTOR sensor
[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     9       /* Check iio devices 0 to MAX_DEVICES-1 */
9 #define MAX_SENSORS     12      /* We can handle as many sensors */
10 #define MAX_CHANNELS    4       /* We can handle as many channels per sensor */
11 #define MAX_EVENTS      8       /* We can handle as many events per channel */
12 #define MAX_TRIGGERS    8       /* Check for triggers 0 to MAX_TRIGGERS-1 */
13
14 #define DEV_FILE_PATH           "/dev/iio:device%d"
15 #define BASE_PATH               "/sys/bus/iio/devices/iio:device%d/"
16 #define TRIGGER_FILE_PATH       "/sys/bus/iio/devices/trigger%d/name"
17
18 #define CHANNEL_PATH            BASE_PATH "scan_elements/"
19 #define ENABLE_PATH             BASE_PATH "buffer/enable"
20 #define NAME_PATH               BASE_PATH "name"
21 #define TRIGGER_PATH            BASE_PATH "trigger/current_trigger"
22 #define EVENTS_PATH             BASE_PATH "events/"
23 #define SENSOR_ENABLE_PATH      BASE_PATH "in_%s_en"
24 #define SENSOR_OFFSET_PATH      BASE_PATH "in_%s_offset"
25 #define SENSOR_SCALE_PATH       BASE_PATH "in_%s_scale"
26 #define SENSOR_SAMPLING_PATH    BASE_PATH "in_%s_sampling_frequency"
27 #define DEVICE_SAMPLING_PATH    BASE_PATH "sampling_frequency"
28 #define DEVICE_AVAIL_FREQ_PATH  BASE_PATH "sampling_frequency_available"
29 #define ILLUMINATION_CALIBPATH  BASE_PATH "in_illuminance_calibscale"
30 #define SENSOR_CALIB_BIAS_PATH  BASE_PATH "in_%s_calibbias"
31
32 #define PROP_BASE               "ro.iio.%s.%s" /* Note: PROPERTY_KEY_MAX is small */
33
34 #define MAX_TYPE_SPEC_LEN       32      /* Channel type spec len; ex: "le:u10/16>>0" */
35 #define MAX_SENSOR_REPORT_SIZE  32      /* Sensor report buffer size */
36 #define MAX_DEVICE_REPORT_SIZE  32      /* iio device scan buffer size */
37
38 #define MAX_NAME_SIZE           32
39
40 #define MAX_SENSOR_BASES        3       /* Max number of base sensors a sensor can rely on */
41
42 #define ARRAY_SIZE(x) sizeof(x)/sizeof(x[0])
43 #define REPORTING_MODE(x)       ((x) & 0x06)
44
45 #define FILTER_TYPE_NONE                0
46 #define FILTER_TYPE_MOVING_AVERAGE      1
47 #define FILTER_TYPE_MEDIAN              2
48
49 #define MODE_AUTO       0 /* autodetect */
50 #define MODE_POLL       1
51 #define MODE_TRIGGER    2
52 #define MODE_EVENT      3
53
54 typedef struct
55 {
56         const char *type; /* event type; e.g: transition */
57         const char *dir;  /* event direction; e.g: rising */
58
59         /* sysfs entries located in /sys/bus/iio/devices/iio:deviceX/events/ */
60         const char *ev_en_path;
61         const char *ev_value_path;
62 }
63 event_descriptor_t;
64
65
66 typedef struct
67 {
68         const char *name;       /* channel name ; ex: x */
69
70         /* sysfs entries located under scan_elements */
71         const char *en_path;    /* Enabled sysfs file name ; ex: "in_temp_en" */
72         const char *type_path;  /* _type sysfs file name  */
73         const char *index_path; /* _index sysfs file name */
74
75         /* sysfs entries located in /sys/bus/iio/devices/iio:deviceX */
76         const char *raw_path;   /* _raw sysfs file name  */
77         const char *input_path; /* _input sysfs file name */
78         const char *scale_path; /* _scale sysfs file name */
79
80         const int num_events;
81         event_descriptor_t event[MAX_EVENTS];
82 }
83 channel_descriptor_t;
84
85
86 typedef struct
87 {
88         const char *tag;        /* Prefix such as "accel", "gyro", "temp"... */
89         const int type;         /* Sensor type ; ex: SENSOR_TYPE_ACCELEROMETER */
90         const int num_channels; /* Expected iio channels for this sensor */
91         const int is_virtual;   /* Is the sensor virtual or not */
92         channel_descriptor_t channel[MAX_CHANNELS];
93 }
94 sensor_catalog_entry_t;
95
96
97 typedef struct
98 {
99         char sign;
100         char endianness;
101         short realbits;
102         short storagebits;
103         short shift;
104 }
105 datum_info_t;
106
107
108 typedef struct
109 {
110         int offset;     /* Offset in bytes within the iio character device report */
111         int size;       /* Field size in bytes */
112         float scale;    /* Scale for each channel */
113         char type_spec[MAX_TYPE_SPEC_LEN];      /* From driver; ex: le:u10/16>>0 */
114         datum_info_t type_info;                 /* Decoded contents of type spec */
115         float opt_scale; /*
116                           * Optional correction scale read from a property such as iio.accel.x.scale, allowing late compensation of
117                           * problems such as misconfigured axes ; set to 1 by default. Applied at the end of the scaling process.
118                           */
119 }
120 channel_info_t;
121
122
123 typedef struct
124 {
125         /* Conversion function called once per channel */
126         float (*transform) (int s, int c, unsigned char* sample_data);
127
128         /* Function called once per sample */
129         int (*finalize) (int s, sensors_event_t* data);
130 }
131 sample_ops_t;
132
133
134 /*
135  * Whenever we have sensor data recorded for a sensor in the associated
136  * sensor cell, its report_pending field is set to a non-zero value
137  * indicating how we got this data.
138  */
139 #define DATA_TRIGGER    1       /* From /dev/iio:device fd              */
140 #define DATA_SYSFS      2       /* Through polling                      */
141 #define DATA_DUPLICATE  3       /* Duplicate of triggered motion sample */
142
143
144 typedef struct
145 {
146         char friendly_name[MAX_NAME_SIZE];      /* ex: Accelerometer         */
147         char internal_name[MAX_NAME_SIZE];      /* ex: accel_3d              */
148         char vendor_name[MAX_NAME_SIZE];        /* ex: Intel                 */
149         char init_trigger_name[MAX_NAME_SIZE];  /* ex: accel-name-dev1       */
150         char motion_trigger_name[MAX_NAME_SIZE];/* ex: accel-any-motion-dev1 */
151         float max_range;
152         float resolution;
153         float power;
154
155         /*
156          * Currently active trigger - either a pointer to the initial (default) trigger name buffer, or a pointer to the motion trigger name buffer,
157          * 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
158          * the default trigger to the motion trigger for a sensor, or rather for the interrupt-driven sensors associated to a given iio device.
159          */
160         const char* selected_trigger;
161
162         float offset;           /* (cooked = raw + offset) * scale                      */
163         float scale;            /* default:1. when set to 0, use channel specific value */
164         float illumincalib;     /* to set the calibration for the ALS                   */
165
166         float requested_rate;   /* requested events / second                            */
167         float sampling_rate;    /* setup events / second                                */
168
169         float min_supported_rate;
170         float max_supported_rate;
171
172         int dev_num;            /* Associated iio dev num, ex: 3 for /dev/iio:device3   */
173
174         int catalog_index;      /* Associated entry within the sensor_catalog array     */
175         int type;               /* Sensor type, such as SENSOR_TYPE_GYROSCOPE           */
176
177         int num_channels;       /* Actual channel count ; 0 for poll mode sensors       */
178
179         int mode;       /* Usage mode, ex: poll, trigger ... */
180
181         /*
182          * The array below indicates where to gather report data for this sensor inside the reports that we read from the iio character device.
183          * It is updated whenever channels are enabled or disabled on the same device. Channel size indicates the size in bytes of fields, and
184          * should be zero for disabled channels. The type field indicates how a specific channel data item is structured.
185          */
186         channel_info_t channel[MAX_CHANNELS];
187
188         /*
189          * 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
190          * contains that data. Valid values are 0: empty, 1: normal, 2: repeat of last acquired value after timeout.
191          */
192         int report_pending;
193
194         /* This flag is set if we have a meta data event pending */
195         int meta_data_pending;
196
197         /*
198          * Timestamp closely matching the date of sampling, preferably retrieved from a iio channel alongside sample data. Value zero indicates that
199          * we couldn't get such a closely correlated timestamp, and that one has to be generated before the report gets sent up to Android.
200          */
201         int64_t report_ts;
202
203         /* Buffer containing the last generated sensor report for this sensor */
204         unsigned char report_buffer[MAX_SENSOR_REPORT_SIZE];
205
206         /* Whether or not the above buffer contains data from a device report */
207         int report_initialized;
208
209         /* Channel and sample finalization callbacks for this sensor */
210         sample_ops_t ops;
211
212         int cal_level; /* 0 means not calibrated */
213
214         /*
215          * Depending on the sensor, calibration may take too much time at higher levels. Allow optional capping to a certain level.
216          */
217         int max_cal_level;
218
219         void *cal_data; /* Sensor calibration data, e.g. for magnetometer */
220
221         void* filter;   /* Filtering data for noisy sensors */
222         int filter_type;/* FILTER_ specification for this sensor ; default is FILTER_NONE */
223
224         /* Previously reported value, for on-change sensors */
225         union {
226                 float data;
227                 uint64_t data64;
228         } prev_val;
229         /*
230          * Certain sensors expose their readings through sysfs files that have a long response time (100-200 ms for ALS). Rather than block our
231          * global control loop for several hundred ms each second, offload those lengthy blocking reads to dedicated threads, which will then report
232          * their data through a fd that we can add to our poll fd set.
233          */
234         int thread_data_fd[2];
235         pthread_t acquisition_thread;
236
237         int base_count; /* How many base sensors is the sensor depending on */
238         int base[MAX_SENSOR_BASES];
239
240         uint32_t quirks; /* Bit mask expressing the need for special tweaks */
241
242         /* Note: we may have to explicitely serialize access to some fields */
243
244         int is_virtual;                 /* Composite sensor, exposed from data acquired through other sensors */
245
246         uint32_t ref_count;             /* Dependency count - for a real sensor how many active virtual sensors are depending on it */
247
248         uint32_t directly_enabled;      /* Flag showing if a sensor was enabled directly by Android */
249
250         /*
251          * 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
252          * more than one at a later time.
253          */
254         sensors_event_t sample;
255
256         /*
257          * 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
258          * before transmitting them to Android ; they form a mapping between the indices of the input and output arrays: ex: 0123 is identity for
259          * a sample containing 4 fields.
260          */
261         unsigned char order[MAX_CHANNELS];
262
263         /*
264          * 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
265          * events before filtering kicks in. We can also use it for statistics.
266          */
267         uint64_t event_count;
268
269         /* Some polled sensors need to be first enabled so that they start
270          * computing a set of values in hardware (e.g step counter). Enabling
271          * is done through a sysfs attribute in_<tag>_en
272          */
273         int needs_enable;
274
275 }
276 sensor_info_t;
277
278
279 /* Reference a few commonly used variables... */
280 extern int                      sensor_count;
281 extern struct sensor_t          sensor_desc[MAX_SENSORS];
282 extern sensor_info_t            sensor[MAX_SENSORS];
283 extern sensor_catalog_entry_t   sensor_catalog[];
284
285 #endif