OSDN Git Service

Merge remote-tracking branch 'origin/abt/topic/gmin/l-dev/sensors/master' into gmin...
[android-x86/hardware-intel-libsensors.git] / description.c
1 /*
2  * Copyright (C) 2014 Intel Corporation.
3  */
4
5 #include <stdlib.h>
6 #include <ctype.h>
7 #include <utils/Log.h>
8 #include <cutils/properties.h>
9 #include <hardware/sensors.h>
10 #include "common.h"
11 #include "enumeration.h"
12 #include "description.h"
13 #include "utils.h"
14
15 #define IIO_SENSOR_HAL_VERSION  1
16
17 /*
18  * About properties
19  *
20  * We acquire a number of parameters about sensors by reading properties.
21  * The idea here is that someone (either a script, or daemon, sets them
22  * depending on the set of sensors present on the machine.
23  *
24  * There are fallback paths in case the properties are not defined, but it is
25  * highly desirable to at least have the following for each sensor:
26  *
27  * ro.iio.anglvel.name = Gyroscope
28  * ro.iio.anglvel.vendor = Intel
29  * ro.iio.anglvel.max_range = 35
30  * ro.iio.anglvel.resolution = 0.002
31  * ro.iio.anglvel.power = 6.1
32  *
33  * Besides these, we have a couple of knobs initially used to cope with Intel
34  * Sensor Hub oddities, such as HID inspired units or firmware bugs:
35  *
36  * ro.iio.anglvel.transform = ISH
37  * ro.iio.anglvel.quirks = init-rate
38  *
39  * The "terse" quirk indicates that the underlying driver only sends events
40  * when the sensor reports a change. The HAL then periodically generates
41  * duplicate events so the sensor behaves as a continously firing one.
42  *
43  * The "noisy" quirk indicates that the underlying driver has a unusually high
44  * level of noise in its readings, and that the HAL has to accomodate it
45  * somehow, e.g. in the magnetometer calibration code path.
46  *
47  * This one is used specifically to pass a calibration scale to ALS drivers:
48  *
49  * ro.iio.illuminance.name = CPLM3218x Ambient Light Sensor
50  * ro.iio.illuminance.vendor = Capella Microsystems
51  * ro.iio.illuminance.max_range = 167000
52  * ro.iio.illuminance.resolution = 1
53  * ro.iio.illuminance.power = .001
54  * ro.iio.illuminance.illumincalib = 7400
55  *
56  * There's a 'opt_scale' specifier, documented as follows:
57  *
58  *  This adds support for a scaling factor that can be expressed
59  *  using properties, for all sensors, on a channel basis. That
60  *  scaling factor is applied after all other transforms have been
61  *  applied, and is intended as a way to compensate for problems
62  *  such as an incorrect axis polarity for a given sensor.
63  *
64  *  The syntax is <usual property prefix>.<channel>.opt_scale, e.g.
65  *  ro.iio.accel.y.opt_scale = -1 to negate the sign of the y readings
66  *  for the accelerometer.
67  *
68  *  For sensors using a single channel - and only those - the channel
69  *  name is implicitly void and a syntax such as ro.iio.illuminance.
70  *  opt_scale = 3 has to be used.
71  *
72  * 'panel' and 'rotation' specifiers can be used to express ACPI PLD placement
73  * information ; if found they will be used in priority over the actual ACPI
74  * data. That is intended as a way to verify values during development.
75  *
76  * It's possible to use the contents of the iio device name as a way to
77  * discriminate between sensors. Several sensors of the same type can coexist:
78  * e.g. ro.iio.temp.bmg160.name = BMG160 Thermometer will be used in priority
79  * over ro.iio.temp.name = BMC150 Thermometer if the sensor for which we query
80  * properties values happen to have its iio device name set to bmg160.
81  */
82
83 static int sensor_get_st_prop (int s, const char* sel, char val[MAX_NAME_SIZE])
84 {
85         char prop_name[PROP_NAME_MAX];
86         char prop_val[PROP_VALUE_MAX];
87         char extended_sel[PROP_VALUE_MAX];
88
89         int i                   = sensor_info[s].catalog_index;
90         const char *prefix      = sensor_catalog[i].tag;
91
92         /* First try most specialized form, like ro.iio.anglvel.bmg160.name */
93
94         snprintf(extended_sel, PROP_NAME_MAX, "%s.%s",
95                  sensor_info[s].internal_name, sel);
96
97         snprintf(prop_name, PROP_NAME_MAX, PROP_BASE, prefix, extended_sel);
98
99         if (property_get(prop_name, prop_val, "")) {
100                 strncpy(val, prop_val, MAX_NAME_SIZE-1);
101                 val[MAX_NAME_SIZE-1] = '\0';
102                 return 0;
103         }
104
105         /* Fall back to simple form, like ro.iio.anglvel.name */
106
107         sprintf(prop_name, PROP_BASE, prefix, sel);
108
109         if (property_get(prop_name, prop_val, "")) {
110                 strncpy(val, prop_val, MAX_NAME_SIZE-1);
111                 val[MAX_NAME_SIZE-1] = '\0';
112                 return 0;
113         }
114
115         return -1;
116 }
117
118
119 int sensor_get_prop (int s, const char* sel, int* val)
120 {
121         char buf[MAX_NAME_SIZE];
122
123         if (sensor_get_st_prop(s, sel, buf))
124                 return -1;
125
126         *val = atoi(buf);
127         return 0;
128 }
129
130
131 int sensor_get_fl_prop (int s, const char* sel, float* val)
132 {
133         char buf[MAX_NAME_SIZE];
134
135         if (sensor_get_st_prop(s, sel, buf))
136                 return -1;
137
138         *val = (float) strtod(buf, NULL);
139         return 0;
140 }
141
142
143 char* sensor_get_name (int s)
144 {
145         if (sensor_info[s].friendly_name[0] != '\0' ||
146                 !sensor_get_st_prop(s, "name", sensor_info[s].friendly_name))
147                         return sensor_info[s].friendly_name;
148
149         /* If we got a iio device name from sysfs, use it */
150         if (sensor_info[s].internal_name[0]) {
151                 snprintf(sensor_info[s].friendly_name, MAX_NAME_SIZE, "S%d-%s",
152                          s, sensor_info[s].internal_name);
153         } else {
154                 sprintf(sensor_info[s].friendly_name, "S%d", s);
155         }
156
157         return sensor_info[s].friendly_name;
158 }
159
160
161 char* sensor_get_vendor (int s)
162 {
163         if (sensor_info[s].vendor_name[0] ||
164                 !sensor_get_st_prop(s, "vendor", sensor_info[s].vendor_name))
165                         return sensor_info[s].vendor_name;
166
167         return "";
168 }
169
170
171 int sensor_get_version (int s)
172 {
173         return IIO_SENSOR_HAL_VERSION;
174 }
175
176
177 float sensor_get_max_range (int s)
178 {
179         if (sensor_info[s].max_range != 0.0 ||
180                 !sensor_get_fl_prop(s, "max_range", &sensor_info[s].max_range))
181                         return sensor_info[s].max_range;
182
183         /* Try returning a sensible value given the sensor type */
184
185         /* We should cap returned samples accordingly... */
186
187         switch (sensor_info[s].type) {
188                 case SENSOR_TYPE_ACCELEROMETER:         /* m/s^2        */
189                         return 50;
190
191                 case SENSOR_TYPE_MAGNETIC_FIELD:        /* micro-tesla  */
192                         return 500;
193
194                 case SENSOR_TYPE_ORIENTATION:           /* degrees      */
195                         return 360;
196
197                 case SENSOR_TYPE_GYROSCOPE:             /* radians/s    */
198                         return 10;
199
200                 case SENSOR_TYPE_LIGHT:                 /* SI lux units */
201                         return 50000;
202
203                 case SENSOR_TYPE_AMBIENT_TEMPERATURE:   /* °C          */
204                 case SENSOR_TYPE_TEMPERATURE:           /* °C          */
205                 case SENSOR_TYPE_PROXIMITY:             /* centimeters  */
206                 case SENSOR_TYPE_PRESSURE:              /* hecto-pascal */
207                 case SENSOR_TYPE_RELATIVE_HUMIDITY:     /* percent */
208                         return 100;
209
210                 default:
211                         return 0.0;
212                 }
213 }
214
215
216 float sensor_get_resolution (int s)
217 {
218         if (sensor_info[s].resolution != 0.0 ||
219                 !sensor_get_fl_prop(s, "resolution", &sensor_info[s].resolution))
220                         return sensor_info[s].resolution;
221
222         return 0;
223 }
224
225
226 float sensor_get_power (int s)
227 {
228         /* mA used while sensor is in use ; not sure about volts :) */
229         if (sensor_info[s].power != 0.0 ||
230                 !sensor_get_fl_prop(s, "power", &sensor_info[s].power))
231                         return sensor_info[s].power;
232
233         return 0;
234 }
235
236
237 float sensor_get_illumincalib (int s)
238 {
239         /* calibrating the ALS Sensor*/
240         if (sensor_info[s].illumincalib != 0.0 ||
241                 !sensor_get_fl_prop(s, "illumincalib", &sensor_info[s].illumincalib)) {
242                         return sensor_info[s].illumincalib;
243         }
244
245         return 0;
246 }
247
248
249 uint32_t sensor_get_quirks (int s)
250 {
251         char quirks_buf[MAX_NAME_SIZE];
252
253         /* Read and decode quirks property on first reference */
254         if (!(sensor_info[s].quirks & QUIRK_ALREADY_DECODED)) {
255                 quirks_buf[0] = '\0';
256                 sensor_get_st_prop(s, "quirks", quirks_buf);
257
258                 if (strstr(quirks_buf, "init-rate"))
259                         sensor_info[s].quirks |= QUIRK_INITIAL_RATE;
260
261                 if (strstr(quirks_buf, "continuous"))
262                         sensor_info[s].quirks |= QUIRK_FORCE_CONTINUOUS;
263
264                 if (strstr(quirks_buf, "terse"))
265                         sensor_info[s].quirks |= QUIRK_TERSE_DRIVER;
266
267                 if (strstr(quirks_buf, "noisy"))
268                         sensor_info[s].quirks |= QUIRK_NOISY;
269
270                 sensor_info[s].quirks |= QUIRK_ALREADY_DECODED;
271         }
272
273         return sensor_info[s].quirks;
274 }
275
276
277 int sensor_get_order (int s, unsigned char map[MAX_CHANNELS])
278 {
279         char buf[MAX_NAME_SIZE];
280         int i;
281         int count = sensor_catalog[sensor_info[s].catalog_index].num_channels;
282
283         if  (sensor_get_st_prop(s, "order", buf))
284                 return 0; /* No order property */
285
286         /* Assume ASCII characters, in the '0'..'9' range */
287
288         for (i=0; i<count; i++)
289                 if (buf[i] - '0' >= count) {
290                         ALOGE("Order index out of range for sensor %d\n", s);
291                         return 0;
292                 }
293
294         for (i=0; i<count; i++)
295                 map[i] = buf[i] - '0';
296
297         return 1;       /* OK to use modified ordering map */
298 }
299
300 char* sensor_get_string_type (int s)
301 {
302         switch (sensor_info[s].type) {
303                 case SENSOR_TYPE_ACCELEROMETER:
304                         return SENSOR_STRING_TYPE_ACCELEROMETER;
305
306                 case SENSOR_TYPE_MAGNETIC_FIELD:
307                         return SENSOR_STRING_TYPE_MAGNETIC_FIELD;
308
309                 case SENSOR_TYPE_ORIENTATION:
310                         return SENSOR_STRING_TYPE_ORIENTATION;
311
312                 case SENSOR_TYPE_GYROSCOPE:
313                         return SENSOR_STRING_TYPE_GYROSCOPE;
314
315                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
316                         return SENSOR_STRING_TYPE_GYROSCOPE_UNCALIBRATED;
317
318                 case SENSOR_TYPE_LIGHT:
319                         return SENSOR_STRING_TYPE_LIGHT;
320
321                 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
322                         return SENSOR_STRING_TYPE_AMBIENT_TEMPERATURE;
323
324                 case SENSOR_TYPE_TEMPERATURE:
325                         return SENSOR_STRING_TYPE_TEMPERATURE;
326
327                 case SENSOR_TYPE_PROXIMITY:
328                         return SENSOR_STRING_TYPE_PROXIMITY;
329
330                 case SENSOR_TYPE_PRESSURE:
331                         return SENSOR_STRING_TYPE_PRESSURE;
332
333                 case SENSOR_TYPE_RELATIVE_HUMIDITY:
334                         return SENSOR_STRING_TYPE_RELATIVE_HUMIDITY;
335
336                 default:
337                         return "";
338                 }
339 }
340
341 flag_t sensor_get_flags (int s)
342 {
343         flag_t flags = 0x0;
344
345         switch (sensor_info[s].type) {
346                 case SENSOR_TYPE_ACCELEROMETER:
347                 case SENSOR_TYPE_MAGNETIC_FIELD:
348                 case SENSOR_TYPE_ORIENTATION:
349                 case SENSOR_TYPE_GYROSCOPE:
350                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
351                 case SENSOR_TYPE_PRESSURE:
352                         flags |= SENSOR_FLAG_CONTINUOUS_MODE;
353                         break;
354
355                 case SENSOR_TYPE_LIGHT:
356                 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
357                 case SENSOR_TYPE_TEMPERATURE:
358                 case SENSOR_TYPE_RELATIVE_HUMIDITY:
359                         flags |= SENSOR_FLAG_ON_CHANGE_MODE;
360                         break;
361
362
363                 case SENSOR_TYPE_PROXIMITY:
364                         flags |= SENSOR_FLAG_WAKE_UP;
365                         flags |= SENSOR_FLAG_ON_CHANGE_MODE;
366                         break;
367
368                 default:
369                         ALOGI("Unknown sensor");
370                 }
371         return flags;
372 }
373
374 int get_cdd_freq (int s, int must)
375 {
376         switch (sensor_info[s].type) {
377                 case SENSOR_TYPE_ACCELEROMETER:
378                         return (must ? 100 : 200); /* must 100 Hz, should 200 Hz, CDD compliant */
379                 case SENSOR_TYPE_GYROSCOPE:
380                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
381                         return (must ? 200 : 200); /* must 200 Hz, should 200 Hz, CDD compliant */
382                 case SENSOR_TYPE_MAGNETIC_FIELD:
383                         return (must ? 10 : 50);   /* must 10 Hz, should 50 Hz, CDD compliant */
384                 case SENSOR_TYPE_LIGHT:
385                 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
386                 case SENSOR_TYPE_TEMPERATURE:
387                         return (must ? 1 : 2);     /* must 1 Hz, should 2Hz, not mentioned in CDD */
388                 default:
389                         return 0;
390         }
391 }
392
393 /* This value is defined only for continuous mode and on-change sensors. It is the delay between
394  * two sensor events corresponding to the lowest frequency that this sensor supports. When lower
395  * frequencies are requested through batch()/setDelay() the events will be generated at this
396  * frequency instead. It can be used by the framework or applications to estimate when the batch
397  * FIFO may be full.
398  *
399  * NOTE: 1) period_ns is in nanoseconds where as maxDelay/minDelay are in microseconds.
400  *              continuous, on-change: maximum sampling period allowed in microseconds.
401  *              one-shot, special : 0
402  *   2) maxDelay should always fit within a 32 bit signed integer. It is declared as 64 bit
403  *      on 64 bit architectures only for binary compatibility reasons.
404  * Availability: SENSORS_DEVICE_API_VERSION_1_3
405  */
406 max_delay_t sensor_get_max_delay (int s)
407 {
408         char avail_sysfs_path[PATH_MAX];
409         int dev_num     = sensor_info[s].dev_num;
410         char freqs_buf[100];
411         char* cursor;
412         float min_supported_rate = 1000;
413         float sr;
414
415         /* continuous, on-change: maximum sampling period allowed in microseconds.
416          * one-shot, special : 0
417          */
418         if (REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_ONE_SHOT_MODE ||
419             REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_SPECIAL_REPORTING_MODE)
420                 return 0;
421
422         sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
423
424         if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) < 0) {
425                 /* If poll mode sensor */
426                 if (!sensor_info[s].num_channels) {
427                         /* The must rate */
428                         min_supported_rate = get_cdd_freq(s, 1);
429                 }
430         } else {
431                 cursor = freqs_buf;
432                 while (*cursor && cursor[0]) {
433
434                         /* Decode a single value */
435                         sr = strtod(cursor, NULL);
436
437                         if (sr < min_supported_rate)
438                                 min_supported_rate = sr;
439
440                         /* Skip digits */
441                         while (cursor[0] && !isspace(cursor[0]))
442                                 cursor++;
443
444                         /* Skip spaces */
445                         while (cursor[0] && isspace(cursor[0]))
446                                 cursor++;
447                 }
448         }
449
450         /* return 0 for wrong values */
451         if (min_supported_rate < 0.1)
452                 return 0;
453
454         /* Return microseconds */
455         return (max_delay_t)(1000000.0 / min_supported_rate);
456 }
457
458 /* this value depends on the reporting mode:
459  *
460  *   continuous: minimum sample period allowed in microseconds
461  *   on-change : 0
462  *   one-shot  :-1
463  *   special   : 0, unless otherwise noted
464  */
465 int32_t sensor_get_min_delay(int s)
466 {
467         char avail_sysfs_path[PATH_MAX];
468         int dev_num     = sensor_info[s].dev_num;
469         char freqs_buf[100];
470         char* cursor;
471         float max_supported_rate = 0;
472         float sr;
473
474         /* continuous: minimum sampling period allowed in microseconds.
475          * on-change, special : 0
476          * one-shot  :-1
477          */
478         if (REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_ON_CHANGE_MODE ||
479             REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_SPECIAL_REPORTING_MODE)
480                 return 0;
481
482         if (REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_ONE_SHOT_MODE)
483                 return -1;
484
485         sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
486
487         if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) < 0) {
488                 /* If poll mode sensor */
489                 if (!sensor_info[s].num_channels) {
490                         /* The should rate */
491                         max_supported_rate = get_cdd_freq(s, 0);
492                 }
493         } else {
494                 cursor = freqs_buf;
495                 while (*cursor && cursor[0]) {
496
497                         /* Decode a single value */
498                         sr = strtod(cursor, NULL);
499
500                         if (sr > max_supported_rate && sr <= MAX_EVENTS)
501                                 max_supported_rate = sr;
502
503                         /* Skip digits */
504                         while (cursor[0] && !isspace(cursor[0]))
505                                 cursor++;
506
507                         /* Skip spaces */
508                         while (cursor[0] && isspace(cursor[0]))
509                                 cursor++;
510                 }
511         }
512
513         /* return 0 for wrong values */
514         if (max_supported_rate < 0.1)
515                 return 0;
516
517         /* Return microseconds */
518         return (int32_t)(1000000.0 / max_supported_rate);
519 }