OSDN Git Service

Better support for virtual sensors
[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         char buf[MAX_NAME_SIZE];
146
147         if (sensor_info[s].is_virtual) {
148                 switch (sensor_info[s].type) {
149                         case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
150                         case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
151                                 strcpy(buf, sensor_info[sensor_info[s].base_idx[0]].friendly_name);
152                                 snprintf(sensor_info[s].friendly_name,
153                                          MAX_NAME_SIZE,
154                                          "%s %s", "Uncalibrated", buf);
155                                 return sensor_info[s].friendly_name;
156
157                         default:
158                                 return "";
159                 }
160         }
161
162         if (sensor_info[s].friendly_name[0] != '\0' ||
163                 !sensor_get_st_prop(s, "name", sensor_info[s].friendly_name))
164                         return sensor_info[s].friendly_name;
165
166         /* If we got a iio device name from sysfs, use it */
167         if (sensor_info[s].internal_name[0]) {
168                 snprintf(sensor_info[s].friendly_name, MAX_NAME_SIZE, "S%d-%s",
169                          s, sensor_info[s].internal_name);
170         } else {
171                 sprintf(sensor_info[s].friendly_name, "S%d", s);
172         }
173
174         return sensor_info[s].friendly_name;
175 }
176
177
178 char* sensor_get_vendor (int s)
179 {
180         if (sensor_info[s].is_virtual) {
181                 switch (sensor_info[s].type) {
182                         case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
183                         case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
184                                 return sensor_info[sensor_info[s].base_idx[0]].vendor_name;
185                         break;
186
187                         default:
188                                 return "";
189
190                 }
191         }
192
193         if (sensor_info[s].vendor_name[0] ||
194                 !sensor_get_st_prop(s, "vendor", sensor_info[s].vendor_name))
195                         return sensor_info[s].vendor_name;
196
197         return "";
198 }
199
200
201 int sensor_get_version (__attribute__((unused)) int s)
202 {
203         return IIO_SENSOR_HAL_VERSION;
204 }
205
206
207 float sensor_get_max_range (int s)
208 {
209
210         if (sensor_info[s].is_virtual)  {
211                 switch (sensor_info[s].type) {
212                         case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
213                         case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
214                                 return sensor_info[sensor_info[s].base_idx[0]].max_range;
215
216                         default:
217                                 return 0.0;
218                 }
219         }
220
221         if (sensor_info[s].max_range != 0.0 ||
222                 !sensor_get_fl_prop(s, "max_range", &sensor_info[s].max_range))
223                         return sensor_info[s].max_range;
224
225         /* Try returning a sensible value given the sensor type */
226
227         /* We should cap returned samples accordingly... */
228
229         switch (sensor_info[s].type) {
230                 case SENSOR_TYPE_ACCELEROMETER:         /* m/s^2        */
231                         return 50;
232
233                 case SENSOR_TYPE_MAGNETIC_FIELD:        /* micro-tesla  */
234                         return 500;
235
236                 case SENSOR_TYPE_ORIENTATION:           /* degrees      */
237                         return 360;
238
239                 case SENSOR_TYPE_GYROSCOPE:             /* radians/s    */
240                         return 10;
241
242                 case SENSOR_TYPE_LIGHT:                 /* SI lux units */
243                         return 50000;
244
245                 case SENSOR_TYPE_AMBIENT_TEMPERATURE:   /* Â°C          */
246                 case SENSOR_TYPE_TEMPERATURE:           /* Â°C          */
247                 case SENSOR_TYPE_PROXIMITY:             /* centimeters  */
248                 case SENSOR_TYPE_PRESSURE:              /* hecto-pascal */
249                 case SENSOR_TYPE_RELATIVE_HUMIDITY:     /* percent */
250                         return 100;
251
252                 default:
253                         return 0.0;
254                 }
255 }
256
257 static float sensor_get_min_freq (int s)
258 {
259         /*
260          * Check if a low cap has been specified for this sensor sampling rate.
261          * In some case, even when the driver supports lower rate, we still
262          * wish to receive a certain number of samples per seconds for various
263          * reasons (calibration, filtering, no change in power consumption...).
264          */
265
266         float min_freq;
267
268         if (!sensor_get_fl_prop(s, "min_freq", &min_freq))
269                 return min_freq;
270
271         return 0;
272 }
273
274
275 static float sensor_get_max_freq (int s)
276 {
277         float max_freq;
278
279         if (!sensor_get_fl_prop(s, "max_freq", &max_freq))
280                 return max_freq;
281
282         return 1000;
283 }
284
285 int sensor_get_cal_steps (int s)
286 {
287         int cal_steps;
288         if (!sensor_get_prop(s, "cal_steps", &cal_steps))
289                 return cal_steps;
290
291         return 0;
292 }
293
294 float sensor_get_resolution (int s)
295 {
296         if (sensor_info[s].is_virtual) {
297                 switch (sensor_info[s].type) {
298                         case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
299                         case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
300                                 return sensor_info[sensor_info[s].base_idx[0]].resolution;
301
302                         default:
303                                 return 0;
304                 }
305         }
306
307         if (sensor_info[s].resolution != 0.0 ||
308                 !sensor_get_fl_prop(s, "resolution", &sensor_info[s].resolution))
309                         return sensor_info[s].resolution;
310
311         return 0;
312 }
313
314
315 float sensor_get_power (int s)
316 {
317
318         if (sensor_info[s].is_virtual) {
319                 switch (sensor_info[s].type) {
320                         case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
321                         case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
322                                 return sensor_info[sensor_info[s].base_idx[0]].power;
323
324                         default:
325                                 return 0;
326                 }
327         }
328
329         /* mA used while sensor is in use ; not sure about volts :) */
330         if (sensor_info[s].power != 0.0 ||
331                 !sensor_get_fl_prop(s, "power", &sensor_info[s].power))
332                         return sensor_info[s].power;
333
334         return 0;
335 }
336
337
338 float sensor_get_illumincalib (int s)
339 {
340         /* calibrating the ALS Sensor*/
341         if (sensor_info[s].illumincalib != 0.0 ||
342                 !sensor_get_fl_prop(s, "illumincalib", &sensor_info[s].illumincalib)) {
343                         return sensor_info[s].illumincalib;
344         }
345
346         return 0;
347 }
348
349
350 uint32_t sensor_get_quirks (int s)
351 {
352         char quirks_buf[MAX_NAME_SIZE];
353
354         /* Read and decode quirks property on first reference */
355         if (!(sensor_info[s].quirks & QUIRK_ALREADY_DECODED)) {
356                 quirks_buf[0] = '\0';
357                 sensor_get_st_prop(s, "quirks", quirks_buf);
358
359                 if (strstr(quirks_buf, "init-rate"))
360                         sensor_info[s].quirks |= QUIRK_INITIAL_RATE;
361
362                 if (strstr(quirks_buf, "continuous"))
363                         sensor_info[s].quirks |= QUIRK_FORCE_CONTINUOUS;
364
365                 if (strstr(quirks_buf, "terse"))
366                         sensor_info[s].quirks |= QUIRK_TERSE_DRIVER;
367
368                 if (strstr(quirks_buf, "noisy"))
369                         sensor_info[s].quirks |= QUIRK_NOISY;
370
371                 sensor_info[s].quirks |= QUIRK_ALREADY_DECODED;
372         }
373
374         return sensor_info[s].quirks;
375 }
376
377
378 int sensor_get_order (int s, unsigned char map[MAX_CHANNELS])
379 {
380         char buf[MAX_NAME_SIZE];
381         int i;
382         int count = sensor_catalog[sensor_info[s].catalog_index].num_channels;
383
384         if  (sensor_get_st_prop(s, "order", buf))
385                 return 0; /* No order property */
386
387         /* Assume ASCII characters, in the '0'..'9' range */
388
389         for (i=0; i<count; i++)
390                 if (buf[i] - '0' >= count) {
391                         ALOGE("Order index out of range for sensor %d\n", s);
392                         return 0;
393                 }
394
395         for (i=0; i<count; i++)
396                 map[i] = buf[i] - '0';
397
398         return 1;       /* OK to use modified ordering map */
399 }
400
401 char* sensor_get_string_type (int s)
402 {
403         switch (sensor_info[s].type) {
404                 case SENSOR_TYPE_ACCELEROMETER:
405                         return SENSOR_STRING_TYPE_ACCELEROMETER;
406
407                 case SENSOR_TYPE_MAGNETIC_FIELD:
408                         return SENSOR_STRING_TYPE_MAGNETIC_FIELD;
409
410                 case SENSOR_TYPE_ORIENTATION:
411                         return SENSOR_STRING_TYPE_ORIENTATION;
412
413                 case SENSOR_TYPE_GYROSCOPE:
414                         return SENSOR_STRING_TYPE_GYROSCOPE;
415
416                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
417                         return SENSOR_STRING_TYPE_GYROSCOPE_UNCALIBRATED;
418
419                 case SENSOR_TYPE_LIGHT:
420                         return SENSOR_STRING_TYPE_LIGHT;
421
422                 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
423                         return SENSOR_STRING_TYPE_AMBIENT_TEMPERATURE;
424
425                 case SENSOR_TYPE_TEMPERATURE:
426                         return SENSOR_STRING_TYPE_TEMPERATURE;
427
428                 case SENSOR_TYPE_PROXIMITY:
429                         return SENSOR_STRING_TYPE_PROXIMITY;
430
431                 case SENSOR_TYPE_PRESSURE:
432                         return SENSOR_STRING_TYPE_PRESSURE;
433
434                 case SENSOR_TYPE_RELATIVE_HUMIDITY:
435                         return SENSOR_STRING_TYPE_RELATIVE_HUMIDITY;
436
437                 default:
438                         return "";
439                 }
440 }
441
442 flag_t sensor_get_flags (int s)
443 {
444         flag_t flags = 0x0;
445
446         switch (sensor_info[s].type) {
447                 case SENSOR_TYPE_ACCELEROMETER:
448                 case SENSOR_TYPE_MAGNETIC_FIELD:
449                 case SENSOR_TYPE_ORIENTATION:
450                 case SENSOR_TYPE_GYROSCOPE:
451                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
452                 case SENSOR_TYPE_PRESSURE:
453                         flags |= SENSOR_FLAG_CONTINUOUS_MODE;
454                         break;
455
456                 case SENSOR_TYPE_LIGHT:
457                 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
458                 case SENSOR_TYPE_TEMPERATURE:
459                 case SENSOR_TYPE_RELATIVE_HUMIDITY:
460                         flags |= SENSOR_FLAG_ON_CHANGE_MODE;
461                         break;
462
463
464                 case SENSOR_TYPE_PROXIMITY:
465                         flags |= SENSOR_FLAG_WAKE_UP;
466                         flags |= SENSOR_FLAG_ON_CHANGE_MODE;
467                         break;
468
469                 default:
470                         ALOGI("Unknown sensor");
471                 }
472         return flags;
473 }
474
475 int get_cdd_freq (int s, int must)
476 {
477         switch (sensor_info[s].type) {
478                 case SENSOR_TYPE_ACCELEROMETER:
479                         return (must ? 100 : 200); /* must 100 Hz, should 200 Hz, CDD compliant */
480                 case SENSOR_TYPE_GYROSCOPE:
481                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
482                         return (must ? 200 : 200); /* must 200 Hz, should 200 Hz, CDD compliant */
483                 case SENSOR_TYPE_MAGNETIC_FIELD:
484                         return (must ? 10 : 50);   /* must 10 Hz, should 50 Hz, CDD compliant */
485                 case SENSOR_TYPE_LIGHT:
486                 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
487                 case SENSOR_TYPE_TEMPERATURE:
488                         return (must ? 1 : 2);     /* must 1 Hz, should 2Hz, not mentioned in CDD */
489                 default:
490                         return 0;
491         }
492 }
493
494 /* This value is defined only for continuous mode and on-change sensors. It is the delay between
495  * two sensor events corresponding to the lowest frequency that this sensor supports. When lower
496  * frequencies are requested through batch()/setDelay() the events will be generated at this
497  * frequency instead. It can be used by the framework or applications to estimate when the batch
498  * FIFO may be full.
499  *
500  * NOTE: 1) period_ns is in nanoseconds where as maxDelay/minDelay are in microseconds.
501  *              continuous, on-change: maximum sampling period allowed in microseconds.
502  *              one-shot, special : 0
503  *   2) maxDelay should always fit within a 32 bit signed integer. It is declared as 64 bit
504  *      on 64 bit architectures only for binary compatibility reasons.
505  * Availability: SENSORS_DEVICE_API_VERSION_1_3
506  */
507 max_delay_t sensor_get_max_delay (int s)
508 {
509         char avail_sysfs_path[PATH_MAX];
510         int dev_num     = sensor_info[s].dev_num;
511         char freqs_buf[100];
512         char* cursor;
513         float min_supported_rate = 1000;
514         float rate_cap;
515         float sr;
516
517         /* continuous, on-change: maximum sampling period allowed in microseconds.
518          * one-shot, special : 0
519          */
520         if (REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_ONE_SHOT_MODE ||
521             REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_SPECIAL_REPORTING_MODE)
522                 return 0;
523
524         if (sensor_info[s].is_virtual) {
525                 switch (sensor_info[s].type) {
526                         case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
527                         case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
528                                 return sensor_desc[sensor_info[s].base_idx[0]].maxDelay;
529                         default:
530                                 return 0;
531                 }
532         }
533         sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
534
535         if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) < 0) {
536                 /* If poll mode sensor */
537                 if (!sensor_info[s].num_channels) {
538                         /* The must rate */
539                         min_supported_rate = get_cdd_freq(s, 1);
540                 }
541         } else {
542                 cursor = freqs_buf;
543                 while (*cursor && cursor[0]) {
544
545                         /* Decode a single value */
546                         sr = strtod(cursor, NULL);
547
548                         if (sr < min_supported_rate)
549                                 min_supported_rate = sr;
550
551                         /* Skip digits */
552                         while (cursor[0] && !isspace(cursor[0]))
553                                 cursor++;
554
555                         /* Skip spaces */
556                         while (cursor[0] && isspace(cursor[0]))
557                                 cursor++;
558                 }
559         }
560
561         /* Check if a minimum rate was specified for this sensor */
562         rate_cap = sensor_get_min_freq(s);
563
564         if (min_supported_rate < rate_cap)
565                 min_supported_rate = rate_cap;
566
567         /* return 0 for wrong values */
568         if (min_supported_rate < 0.1)
569                 return 0;
570
571         /* Return microseconds */
572         return (max_delay_t)(1000000.0 / min_supported_rate);
573 }
574
575 /* this value depends on the reporting mode:
576  *
577  *   continuous: minimum sample period allowed in microseconds
578  *   on-change : 0
579  *   one-shot  :-1
580  *   special   : 0, unless otherwise noted
581  */
582 int32_t sensor_get_min_delay(int s)
583 {
584         char avail_sysfs_path[PATH_MAX];
585         int dev_num     = sensor_info[s].dev_num;
586         char freqs_buf[100];
587         char* cursor;
588         float max_supported_rate = 0;
589         float sr;
590
591         /* continuous: minimum sampling period allowed in microseconds.
592          * on-change, special : 0
593          * one-shot  :-1
594          */
595         if (REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_ON_CHANGE_MODE ||
596             REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_SPECIAL_REPORTING_MODE)
597                 return 0;
598
599         if (REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_ONE_SHOT_MODE)
600                 return -1;
601
602         if (sensor_info[s].is_virtual) {
603                 switch (sensor_info[s].type) {
604                         case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
605                         case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
606                                 return sensor_desc[sensor_info[s].base_idx[0]].minDelay;
607                         default:
608                                 return 0;
609                 }
610         }
611
612         sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
613
614         if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) < 0) {
615                 /* If poll mode sensor */
616                 if (!sensor_info[s].num_channels) {
617                         /* The should rate */
618                         max_supported_rate = get_cdd_freq(s, 0);
619                 }
620         } else {
621                 cursor = freqs_buf;
622                 while (*cursor && cursor[0]) {
623
624                         /* Decode a single value */
625                         sr = strtod(cursor, NULL);
626
627                         if (sr > max_supported_rate && sr <= sensor_get_max_freq(s))
628                                 max_supported_rate = sr;
629
630                         /* Skip digits */
631                         while (cursor[0] && !isspace(cursor[0]))
632                                 cursor++;
633
634                         /* Skip spaces */
635                         while (cursor[0] && isspace(cursor[0]))
636                                 cursor++;
637                 }
638         }
639
640         /* return 0 for wrong values */
641         if (max_supported_rate < 0.1)
642                 return 0;
643
644         /* Return microseconds */
645         return (int32_t)(1000000.0 / max_supported_rate);
646 }