OSDN Git Service

Add the ordering flag to the quirks array
[android-x86/hardware-intel-libsensors.git] / description.c
1 /*
2  * Copyright (C) 2014 Intel Corporation.
3  */
4
5 #include <stdlib.h>
6 #include <utils/Log.h>
7 #include <cutils/properties.h>
8 #include <hardware/sensors.h>
9 #include "enumeration.h"
10 #include "description.h"
11
12 #define IIO_SENSOR_HAL_VERSION  1
13
14 /*
15  * About properties
16  *
17  * We acquire a number of parameters about sensors by reading properties.
18  * The idea here is that someone (either a script, or daemon, sets them
19  * depending on the set of sensors present on the machine.
20  *
21  * There are fallback paths in case the properties are not defined, but it is
22  * highly desirable to at least have the following for each sensor:
23  *
24  * ro.iio.anglvel.name = Gyroscope
25  * ro.iio.anglvel.vendor = Intel
26  * ro.iio.anglvel.max_range = 35
27  * ro.iio.anglvel.resolution = 0.002
28  * ro.iio.anglvel.power = 6.1
29  *
30  * Besides these, we have a couple of knobs initially used to cope with Intel
31  * Sensor Hub oddities, such as HID inspired units or firmware bugs:
32  *
33  * ro.iio.anglvel.transform = ISH
34  * ro.iio.anglvel.quirks = init-rate
35  *
36  * The "terse" quirk indicates that the underlying driver only sends events
37  * when the sensor reports a change. The HAL then periodically generates
38  * duplicate events so the sensor behaves as a continously firing one.
39  *
40  * The "noisy" quirk indicates that the underlying driver has a unusually high
41  * level of noise in its readings, and that the HAL has to accomodate it
42  * somehow, e.g. in the magnetometer calibration code path.
43  *
44  * This one is used specifically to pass a calibration scale to ALS drivers:
45  *
46  * ro.iio.illuminance.name = CPLM3218x Ambient Light Sensor
47  * ro.iio.illuminance.vendor = Capella Microsystems
48  * ro.iio.illuminance.max_range = 167000
49  * ro.iio.illuminance.resolution = 1
50  * ro.iio.illuminance.power = .001
51  * ro.iio.illuminance.illumincalib = 7400
52  *
53  * Finally there's a 'opt_scale' specifier, documented as follows:
54  *
55  *  This adds support for a scaling factor that can be expressed
56  *  using properties, for all sensors, on a channel basis. That
57  *  scaling factor is applied after all other transforms have been
58  *  applied, and is intended as a way to compensate for problems
59  *  such as an incorrect axis polarity for a given sensor.
60  *
61  *  The syntax is <usual property prefix>.<channel>.opt_scale, e.g.
62  *  ro.iio.accel.y.opt_scale = -1 to negate the sign of the y readings
63  *  for the accelerometer.
64  *
65  *  For sensors using a single channel - and only those - the channel
66  *  name is implicitly void and a syntax such as ro.iio.illuminance.
67  *  opt_scale = 3 has to be used.
68  */
69
70 static int sensor_get_st_prop (int s, const char* sel, char val[MAX_NAME_SIZE])
71 {
72         char prop_name[PROP_NAME_MAX];
73         char prop_val[PROP_VALUE_MAX];
74         int i                   = sensor_info[s].catalog_index;
75         const char *prefix      = sensor_catalog[i].tag;
76
77         sprintf(prop_name, PROP_BASE, prefix, sel);
78
79         if (property_get(prop_name, prop_val, "")) {
80                 strncpy(val, prop_val, MAX_NAME_SIZE-1);
81                 val[MAX_NAME_SIZE-1] = '\0';
82                 return 0;
83         }
84
85         return -1;
86 }
87
88
89 int sensor_get_fl_prop (int s, const char* sel, float* val)
90 {
91         char buf[MAX_NAME_SIZE];
92
93         if (sensor_get_st_prop(s, sel, buf))
94                 return -1;
95
96         *val = (float) strtod(buf, NULL);
97         return 0;
98 }
99
100
101 char* sensor_get_name (int s)
102 {
103         if (sensor_info[s].friendly_name[0] != '\0' ||
104                 !sensor_get_st_prop(s, "name", sensor_info[s].friendly_name))
105                         return sensor_info[s].friendly_name;
106
107         /* If we got a iio device name from sysfs, use it */
108         if (sensor_info[s].internal_name[0]) {
109                 snprintf(sensor_info[s].friendly_name, MAX_NAME_SIZE, "S%d-%s",
110                          s, sensor_info[s].internal_name);
111         } else {
112                 sprintf(sensor_info[s].friendly_name, "S%d", s);
113         }
114
115         return sensor_info[s].friendly_name;
116 }
117
118
119 char* sensor_get_vendor (int s)
120 {
121         if (sensor_info[s].vendor_name[0] ||
122                 !sensor_get_st_prop(s, "vendor", sensor_info[s].vendor_name))
123                         return sensor_info[s].vendor_name;
124
125         return "";
126 }
127
128
129 int sensor_get_version (int s)
130 {
131         return IIO_SENSOR_HAL_VERSION;
132 }
133
134
135 float sensor_get_max_range (int s)
136 {
137         int catalog_index;
138         int sensor_type;
139
140         if (sensor_info[s].max_range != 0.0 ||
141                 !sensor_get_fl_prop(s, "max_range", &sensor_info[s].max_range))
142                         return sensor_info[s].max_range;
143
144         /* Try returning a sensible value given the sensor type */
145
146         /* We should cap returned samples accordingly... */
147
148         catalog_index = sensor_info[s].catalog_index;
149         sensor_type = sensor_catalog[catalog_index].type;
150
151         switch (sensor_type) {
152                 case SENSOR_TYPE_ACCELEROMETER:         /* m/s^2        */
153                         return 50;
154
155                 case SENSOR_TYPE_MAGNETIC_FIELD:        /* micro-tesla  */
156                         return 500;
157
158                 case SENSOR_TYPE_ORIENTATION:           /* degrees      */
159                         return 360;
160
161                 case SENSOR_TYPE_GYROSCOPE:             /* radians/s    */
162                         return 10;
163
164                 case SENSOR_TYPE_LIGHT:                 /* SI lux units */
165                         return 50000;
166
167                 case SENSOR_TYPE_AMBIENT_TEMPERATURE:   /* °C          */
168                 case SENSOR_TYPE_TEMPERATURE:           /* °C          */
169                 case SENSOR_TYPE_PROXIMITY:             /* centimeters  */
170                 case SENSOR_TYPE_PRESSURE:              /* hecto-pascal */
171                 case SENSOR_TYPE_RELATIVE_HUMIDITY:     /* percent */
172                         return 100;
173
174                 default:
175                         return 0.0;
176                 }
177 }
178
179
180 float sensor_get_resolution (int s)
181 {
182         if (sensor_info[s].resolution != 0.0 ||
183                 !sensor_get_fl_prop(s, "resolution", &sensor_info[s].resolution))
184                         return sensor_info[s].resolution;
185
186         return 0;
187 }
188
189
190 float sensor_get_power (int s)
191 {
192         /* mA used while sensor is in use ; not sure about volts :) */
193         if (sensor_info[s].power != 0.0 ||
194                 !sensor_get_fl_prop(s, "power", &sensor_info[s].power))
195                         return sensor_info[s].power;
196
197         return 0;
198 }
199
200
201 float sensor_get_illumincalib (int s)
202 {
203         /* calibrating the ALS Sensor*/
204         if (sensor_info[s].illumincalib != 0.0 ||
205                 !sensor_get_fl_prop(s, "illumincalib", &sensor_info[s].illumincalib)) {
206                         return sensor_info[s].illumincalib;
207         }
208
209         return 0;
210 }
211
212
213 uint32_t sensor_get_quirks (int s)
214 {
215         char quirks_buf[MAX_NAME_SIZE];
216
217         /* Read and decode quirks property on first reference */
218         if (!(sensor_info[s].quirks & QUIRK_ALREADY_DECODED)) {
219                 quirks_buf[0] = '\0';
220                 sensor_get_st_prop(s, "quirks", quirks_buf);
221
222                 if (strstr(quirks_buf, "init-rate"))
223                         sensor_info[s].quirks |= QUIRK_INITIAL_RATE;
224
225                 if (strstr(quirks_buf, "terse"))
226                         sensor_info[s].quirks |= QUIRK_TERSE_DRIVER;
227
228                 if (strstr(quirks_buf, "noisy"))
229                         sensor_info[s].quirks |= QUIRK_NOISY;
230
231                 sensor_info[s].quirks |= QUIRK_ALREADY_DECODED;
232         }
233
234         return sensor_info[s].quirks;
235 }
236
237
238 int sensor_get_order (int s, unsigned char map[MAX_CHANNELS])
239 {
240         char buf[MAX_NAME_SIZE];
241         int i;
242         int count = sensor_catalog[sensor_info[s].catalog_index].num_channels;
243
244         memset(map, 0, MAX_CHANNELS);
245
246         if  (sensor_get_st_prop(s, "order", buf))
247                 return 0; /* No order property */
248
249         /* Assume ASCII characters, in the '0'..'9' range */
250
251         for (i=0; i<count; i++)
252                 map[i] = buf[i] - '0';
253
254         /* Check that our indices are in range */
255         for (i=0; i<count; i++)
256                 if (map[i] >= count) {
257                         ALOGE("Order index out of range for sensor %d\n", s);
258                         return 0;
259                 }
260
261         return 1;       /* OK to use modified ordering map */
262 }
263
264 char* sensor_get_string_type(int s)
265 {
266         int catalog_index;
267         int sensor_type;
268
269         catalog_index = sensor_info[s].catalog_index;
270         sensor_type = sensor_catalog[catalog_index].type;
271
272         switch (sensor_type) {
273                 case SENSOR_TYPE_ACCELEROMETER:
274                         return SENSOR_STRING_TYPE_ACCELEROMETER;
275
276                 case SENSOR_TYPE_MAGNETIC_FIELD:
277                         return SENSOR_STRING_TYPE_MAGNETIC_FIELD;
278
279                 case SENSOR_TYPE_ORIENTATION:
280                         return SENSOR_STRING_TYPE_ORIENTATION;
281
282                 case SENSOR_TYPE_GYROSCOPE:
283                         return SENSOR_STRING_TYPE_GYROSCOPE;
284
285                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
286                         return SENSOR_STRING_TYPE_GYROSCOPE_UNCALIBRATED;
287
288                 case SENSOR_TYPE_LIGHT:
289                         return SENSOR_STRING_TYPE_LIGHT;
290
291                 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
292                         return SENSOR_STRING_TYPE_AMBIENT_TEMPERATURE;
293
294                 case SENSOR_TYPE_TEMPERATURE:
295                         return SENSOR_STRING_TYPE_TEMPERATURE;
296
297                 case SENSOR_TYPE_PROXIMITY:
298                         return SENSOR_STRING_TYPE_PROXIMITY;
299
300                 case SENSOR_TYPE_PRESSURE:
301                         return SENSOR_STRING_TYPE_PRESSURE;
302
303                 case SENSOR_TYPE_RELATIVE_HUMIDITY:
304                         return SENSOR_STRING_TYPE_RELATIVE_HUMIDITY;
305
306                 default:
307                         return "";
308                 }
309 }
310
311 flag_t sensor_get_flags (int s)
312 {
313         int catalog_index;
314         int sensor_type;
315
316         flag_t flags = 0x0;
317         catalog_index = sensor_info[s].catalog_index;
318         sensor_type = sensor_catalog[catalog_index].type;
319
320         switch (sensor_type) {
321                 case SENSOR_TYPE_ACCELEROMETER:
322                 case SENSOR_TYPE_MAGNETIC_FIELD:
323                 case SENSOR_TYPE_ORIENTATION:
324                 case SENSOR_TYPE_GYROSCOPE:
325                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
326                 case SENSOR_TYPE_PRESSURE:
327                         flags |= SENSOR_FLAG_CONTINUOUS_MODE;
328                         break;
329
330                 case SENSOR_TYPE_LIGHT:
331                 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
332                 case SENSOR_TYPE_TEMPERATURE:
333                 case SENSOR_TYPE_RELATIVE_HUMIDITY:
334                         flags |= SENSOR_FLAG_ON_CHANGE_MODE;
335                         break;
336
337
338                 case SENSOR_TYPE_PROXIMITY:
339                         flags |= SENSOR_FLAG_WAKE_UP;
340                         flags |= SENSOR_FLAG_ON_CHANGE_MODE;
341                         break;
342
343                 default:
344                         ALOGI("Unknown sensor");
345                 }
346         return flags;
347 }