2 * Copyright (C) 2014-2015 Intel Corporation.
8 #include <cutils/properties.h>
9 #include <hardware/sensors.h>
11 #include "enumeration.h"
12 #include "description.h"
15 #define IIO_SENSOR_HAL_VERSION 1
17 #define MIN_ON_CHANGE_SAMPLING_PERIOD_US 200000 /* For on change sensors (temperature, proximity, ALS, etc.) report we support 5 Hz max (0.2 s min period) */
18 #define MAX_ON_CHANGE_SAMPLING_PERIOD_US 10000000 /* 0.1 Hz min (10 s max period)*/
19 #define ANDROID_MAX_FREQ 1000 /* 1000 Hz - This is how much Android requests for the fastest frequency */
24 * We acquire a number of parameters about sensors by reading properties.
25 * The idea here is that someone (either a script, or daemon, sets them
26 * depending on the set of sensors present on the machine.
28 * There are fallback paths in case the properties are not defined, but it is
29 * highly desirable to at least have the following for each sensor:
31 * ro.iio.anglvel.name = Gyroscope
32 * ro.iio.anglvel.vendor = Intel
33 * ro.iio.anglvel.max_range = 35
34 * ro.iio.anglvel.resolution = 0.002
35 * ro.iio.anglvel.power = 6.1
37 * Besides these, we have a couple of knobs initially used to cope with Intel
38 * Sensor Hub oddities, such as HID inspired units or firmware bugs:
40 * ro.iio.anglvel.transform = ISH
41 * ro.iio.anglvel.quirks = init-rate
43 * The "terse" quirk indicates that the underlying driver only sends events
44 * when the sensor reports a change. The HAL then periodically generates
45 * duplicate events so the sensor behaves as a continously firing one.
47 * The "noisy" quirk indicates that the underlying driver has a unusually high
48 * level of noise in its readings, and that the HAL has to accomodate it
49 * somehow, e.g. in the magnetometer calibration code path.
51 * This one is used specifically to pass a calibration scale to ALS drivers:
53 * ro.iio.illuminance.name = CPLM3218x Ambient Light Sensor
54 * ro.iio.illuminance.vendor = Capella Microsystems
55 * ro.iio.illuminance.max_range = 167000
56 * ro.iio.illuminance.resolution = 1
57 * ro.iio.illuminance.power = .001
58 * ro.iio.illuminance.illumincalib = 7400
60 * There's a 'opt_scale' specifier, documented as follows:
62 * This adds support for a scaling factor that can be expressed
63 * using properties, for all sensors, on a channel basis. That
64 * scaling factor is applied after all other transforms have been
65 * applied, and is intended as a way to compensate for problems
66 * such as an incorrect axis polarity for a given sensor.
68 * The syntax is <usual property prefix>.<channel>.opt_scale, e.g.
69 * ro.iio.accel.y.opt_scale = -1 to negate the sign of the y readings
70 * for the accelerometer.
72 * For sensors using a single channel - and only those - the channel
73 * name is implicitly void and a syntax such as ro.iio.illuminance.
74 * opt_scale = 3 has to be used.
76 * 'panel' and 'rotation' specifiers can be used to express ACPI PLD placement
77 * information ; if found they will be used in priority over the actual ACPI
78 * data. That is intended as a way to verify values during development.
80 * It's possible to use the contents of the iio device name as a way to
81 * discriminate between sensors. Several sensors of the same type can coexist:
82 * e.g. ro.iio.temp.bmg160.name = BMG160 Thermometer will be used in priority
83 * over ro.iio.temp.name = BMC150 Thermometer if the sensor for which we query
84 * properties values happen to have its iio device name set to bmg160.
87 int sensor_get_st_prop (int s, const char* sel, char val[MAX_NAME_SIZE])
89 char prop_name[PROP_NAME_MAX];
90 char prop_val[PROP_VALUE_MAX];
91 char extended_sel[PROP_VALUE_MAX];
93 int i = sensor[s].catalog_index;
94 const char *prefix = sensor_catalog[i].tag;
96 /* First try most specialized form, like ro.iio.anglvel.bmg160.name */
98 snprintf(extended_sel, PROP_NAME_MAX, "%s.%s",
99 sensor[s].internal_name, sel);
101 snprintf(prop_name, PROP_NAME_MAX, PROP_BASE, prefix, extended_sel);
103 if (property_get(prop_name, prop_val, "")) {
104 strncpy(val, prop_val, MAX_NAME_SIZE-1);
105 val[MAX_NAME_SIZE-1] = '\0';
109 /* Fall back to simple form, like ro.iio.anglvel.name */
111 snprintf(prop_name, PROP_NAME_MAX, PROP_BASE, prefix, sel);
113 if (property_get(prop_name, prop_val, "")) {
114 strncpy(val, prop_val, MAX_NAME_SIZE-1);
115 val[MAX_NAME_SIZE-1] = '\0';
123 int sensor_get_prop (int s, const char* sel, int* val)
125 char buf[MAX_NAME_SIZE];
127 if (sensor_get_st_prop(s, sel, buf))
135 int sensor_get_fl_prop (int s, const char* sel, float* val)
137 char buf[MAX_NAME_SIZE];
139 if (sensor_get_st_prop(s, sel, buf))
142 *val = (float) strtod(buf, NULL);
147 char* sensor_get_name (int s)
149 char buf[MAX_NAME_SIZE];
151 if (sensor[s].is_virtual) {
152 switch (sensor[s].type) {
153 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
154 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
155 strcpy(buf, sensor[sensor[s].base[0]].friendly_name);
156 snprintf(sensor[s].friendly_name,
158 "%s %s", "Uncalibrated", buf);
159 return sensor[s].friendly_name;
166 if (sensor[s].friendly_name[0] != '\0' ||
167 !sensor_get_st_prop(s, "name", sensor[s].friendly_name))
168 return sensor[s].friendly_name;
170 /* If we got a iio device name from sysfs, use it */
171 if (sensor[s].internal_name[0]) {
172 snprintf(sensor[s].friendly_name, MAX_NAME_SIZE, "S%d-%s",
173 s, sensor[s].internal_name);
175 sprintf(sensor[s].friendly_name, "S%d", s);
178 return sensor[s].friendly_name;
182 char* sensor_get_vendor (int s)
184 if (sensor[s].is_virtual) {
185 switch (sensor[s].type) {
186 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
187 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
188 return sensor[sensor[s].base[0]].vendor_name;
197 if (sensor[s].vendor_name[0] ||
198 !sensor_get_st_prop(s, "vendor", sensor[s].vendor_name))
199 return sensor[s].vendor_name;
205 int sensor_get_version (__attribute__((unused)) int s)
207 return IIO_SENSOR_HAL_VERSION;
211 float sensor_get_max_range (int s)
213 if (sensor[s].is_virtual) {
214 switch (sensor[s].type) {
215 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
216 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
217 return sensor[sensor[s].base[0]].max_range;
224 if (sensor[s].max_range != 0.0 ||
225 !sensor_get_fl_prop(s, "max_range", &sensor[s].max_range))
226 return sensor[s].max_range;
228 /* Try returning a sensible value given the sensor type */
230 /* We should cap returned samples accordingly... */
232 switch (sensor_desc[s].type) {
233 case SENSOR_TYPE_ACCELEROMETER: /* m/s^2 */
236 case SENSOR_TYPE_MAGNETIC_FIELD: /* micro-tesla */
239 case SENSOR_TYPE_ORIENTATION: /* degrees */
242 case SENSOR_TYPE_GYROSCOPE: /* radians/s */
245 case SENSOR_TYPE_LIGHT: /* SI lux units */
248 case SENSOR_TYPE_AMBIENT_TEMPERATURE: /* °C */
249 case SENSOR_TYPE_TEMPERATURE: /* °C */
250 case SENSOR_TYPE_PROXIMITY: /* centimeters */
251 case SENSOR_TYPE_PRESSURE: /* hecto-pascal */
252 case SENSOR_TYPE_RELATIVE_HUMIDITY: /* percent */
260 static float sensor_get_min_freq (int s)
263 * Check if a low cap has been specified for this sensor sampling rate.
264 * In some case, even when the driver supports lower rate, we still
265 * wish to receive a certain number of samples per seconds for various
266 * reasons (calibration, filtering, no change in power consumption...).
271 if (!sensor_get_fl_prop(s, "min_freq", &min_freq))
278 static float sensor_get_max_freq (int s)
282 if (!sensor_get_fl_prop(s, "max_freq", &max_freq))
285 return ANDROID_MAX_FREQ;
288 int sensor_get_cal_steps (int s)
291 if (!sensor_get_prop(s, "cal_steps", &cal_steps))
297 float sensor_get_resolution (int s)
299 if (sensor[s].is_virtual) {
300 switch (sensor[s].type) {
301 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
302 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
303 return sensor[sensor[s].base[0]].resolution;
310 if (sensor[s].resolution != 0.0 ||
311 !sensor_get_fl_prop(s, "resolution", &sensor[s].resolution))
312 return sensor[s].resolution;
318 float sensor_get_power (int s)
321 if (sensor[s].is_virtual) {
322 switch (sensor[s].type) {
323 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
324 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
325 return sensor[sensor[s].base[0]].power;
332 /* mA used while sensor is in use ; not sure about volts :) */
333 if (sensor[s].power != 0.0 ||
334 !sensor_get_fl_prop(s, "power", &sensor[s].power))
335 return sensor[s].power;
341 float sensor_get_illumincalib (int s)
343 /* calibrating the ALS Sensor*/
344 if (sensor[s].illumincalib != 0.0 ||
345 !sensor_get_fl_prop(s, "illumincalib", &sensor[s].illumincalib)) {
346 return sensor[s].illumincalib;
353 uint32_t sensor_get_quirks (int s)
355 char quirks_buf[MAX_NAME_SIZE];
357 /* Read and decode quirks property on first reference */
358 if (!(sensor[s].quirks & QUIRK_ALREADY_DECODED)) {
359 quirks_buf[0] = '\0';
360 sensor_get_st_prop(s, "quirks", quirks_buf);
362 if (strstr(quirks_buf, "init-rate"))
363 sensor[s].quirks |= QUIRK_INITIAL_RATE;
365 if (strstr(quirks_buf, "continuous"))
366 sensor[s].quirks |= QUIRK_FORCE_CONTINUOUS;
368 if (strstr(quirks_buf, "terse"))
369 sensor[s].quirks |= QUIRK_TERSE_DRIVER;
371 if (strstr(quirks_buf, "noisy"))
372 sensor[s].quirks |= QUIRK_NOISY;
374 if (strstr(quirks_buf, "biased"))
375 sensor[s].quirks |= QUIRK_BIASED;
377 if (strstr(quirks_buf, "spotty"))
378 sensor[s].quirks |= QUIRK_SPOTTY;
380 if (strstr(quirks_buf, "no-event"))
381 sensor[s].quirks |= QUIRK_NO_EVENT_MODE;
383 if (strstr(quirks_buf, "no-trig"))
384 sensor[s].quirks |= QUIRK_NO_TRIG_MODE;
386 if (strstr(quirks_buf, "no-poll"))
387 sensor[s].quirks |= QUIRK_NO_POLL_MODE;
389 sensor[s].quirks |= QUIRK_ALREADY_DECODED;
392 return sensor[s].quirks;
396 int sensor_get_order (int s, unsigned char map[MAX_CHANNELS])
398 char buf[MAX_NAME_SIZE];
400 int count = sensor_catalog[sensor[s].catalog_index].num_channels;
402 if (sensor_get_st_prop(s, "order", buf))
403 return 0; /* No order property */
405 /* Assume ASCII characters, in the '0'..'9' range */
407 for (i=0; i<count; i++)
408 if (buf[i] - '0' >= count) {
409 ALOGE("Order index out of range for sensor %d\n", s);
413 for (i=0; i<count; i++)
414 map[i] = buf[i] - '0';
416 return 1; /* OK to use modified ordering map */
420 char* sensor_get_string_type (int s)
422 switch (sensor_desc[s].type) {
423 case SENSOR_TYPE_ACCELEROMETER:
424 return SENSOR_STRING_TYPE_ACCELEROMETER;
426 case SENSOR_TYPE_MAGNETIC_FIELD:
427 return SENSOR_STRING_TYPE_MAGNETIC_FIELD;
429 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
430 return SENSOR_STRING_TYPE_MAGNETIC_FIELD_UNCALIBRATED;
432 case SENSOR_TYPE_ORIENTATION:
433 return SENSOR_STRING_TYPE_ORIENTATION;
435 case SENSOR_TYPE_GYROSCOPE:
436 return SENSOR_STRING_TYPE_GYROSCOPE;
438 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
439 return SENSOR_STRING_TYPE_GYROSCOPE_UNCALIBRATED;
441 case SENSOR_TYPE_LIGHT:
442 return SENSOR_STRING_TYPE_LIGHT;
444 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
445 return SENSOR_STRING_TYPE_AMBIENT_TEMPERATURE;
447 case SENSOR_TYPE_TEMPERATURE:
448 return SENSOR_STRING_TYPE_TEMPERATURE;
450 case SENSOR_TYPE_PROXIMITY:
451 return SENSOR_STRING_TYPE_PROXIMITY;
453 case SENSOR_TYPE_PRESSURE:
454 return SENSOR_STRING_TYPE_PRESSURE;
456 case SENSOR_TYPE_RELATIVE_HUMIDITY:
457 return SENSOR_STRING_TYPE_RELATIVE_HUMIDITY;
465 flag_t sensor_get_flags (int s)
469 switch (sensor_desc[s].type) {
470 case SENSOR_TYPE_LIGHT:
471 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
472 case SENSOR_TYPE_TEMPERATURE:
473 case SENSOR_TYPE_RELATIVE_HUMIDITY:
474 case SENSOR_TYPE_STEP_COUNTER:
475 flags |= SENSOR_FLAG_ON_CHANGE_MODE;
479 case SENSOR_TYPE_PROXIMITY:
480 flags |= SENSOR_FLAG_WAKE_UP;
481 flags |= SENSOR_FLAG_ON_CHANGE_MODE;
483 case SENSOR_TYPE_STEP_DETECTOR:
484 flags |= SENSOR_FLAG_SPECIAL_REPORTING_MODE;
493 static int get_cdd_freq (int s, int must)
495 switch (sensor_desc[s].type) {
496 case SENSOR_TYPE_ACCELEROMETER:
497 return (must ? 100 : 200); /* must 100 Hz, should 200 Hz, CDD compliant */
499 case SENSOR_TYPE_GYROSCOPE:
500 return (must ? 200 : 200); /* must 200 Hz, should 200 Hz, CDD compliant */
502 case SENSOR_TYPE_MAGNETIC_FIELD:
503 return (must ? 10 : 50); /* must 10 Hz, should 50 Hz, CDD compliant */
505 case SENSOR_TYPE_LIGHT:
506 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
507 case SENSOR_TYPE_TEMPERATURE:
508 return (must ? 1 : 2); /* must 1 Hz, should 2Hz, not mentioned in CDD */
511 return 1; /* Use 1 Hz by default, e.g. for proximity */
516 * This value is defined only for continuous mode and on-change sensors. It is the delay between two sensor events corresponding to the lowest frequency that
517 * this sensor supports. When lower frequencies are requested through batch()/setDelay() the events will be generated at this frequency instead. It can be used
518 * by the framework or applications to estimate when the batch FIFO may be full. maxDelay should always fit within a 32 bit signed integer. It is declared as
519 * 64 bit on 64 bit architectures only for binary compatibility reasons. Availability: SENSORS_DEVICE_API_VERSION_1_3
521 max_delay_t sensor_get_max_delay (int s)
523 char avail_sysfs_path[PATH_MAX];
524 int dev_num = sensor[s].dev_num;
527 float min_supported_rate = 1000;
532 * continuous, on-change: maximum sampling period allowed in microseconds.
533 * one-shot, special : 0
535 switch (REPORTING_MODE(sensor_desc[s].flags)) {
536 case SENSOR_FLAG_ONE_SHOT_MODE:
537 case SENSOR_FLAG_SPECIAL_REPORTING_MODE:
540 case SENSOR_FLAG_ON_CHANGE_MODE:
541 return MAX_ON_CHANGE_SAMPLING_PERIOD_US;
547 if (sensor[s].is_virtual) {
548 switch (sensor[s].type) {
549 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
550 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
551 return sensor_desc[sensor[s].base[0]].maxDelay;
556 sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
558 if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) < 0) {
559 if (sensor[s].mode == MODE_POLL) {
561 min_supported_rate = get_cdd_freq(s, 1);
565 while (*cursor && cursor[0]) {
567 /* Decode a single value */
568 sr = strtod(cursor, NULL);
570 if (sr < min_supported_rate)
571 min_supported_rate = sr;
574 while (cursor[0] && !isspace(cursor[0]))
578 while (cursor[0] && isspace(cursor[0]))
583 /* Check if a minimum rate was specified for this sensor */
584 rate_cap = sensor_get_min_freq(s);
586 if (min_supported_rate < rate_cap)
587 min_supported_rate = rate_cap;
589 /* return 0 for wrong values */
590 if (min_supported_rate < 0.1)
593 /* Return microseconds */
594 return (max_delay_t) (1000000.0 / min_supported_rate);
598 int32_t sensor_get_min_delay (int s)
600 char avail_sysfs_path[PATH_MAX];
601 int dev_num = sensor[s].dev_num;
604 float max_supported_rate = 0;
605 float max_from_prop = sensor_get_max_freq(s);
608 /* continuous, on change: minimum sampling period allowed in microseconds.
609 * special : 0, unless otherwise noted
612 switch (REPORTING_MODE(sensor_desc[s].flags)) {
613 case SENSOR_FLAG_ON_CHANGE_MODE:
614 return MIN_ON_CHANGE_SAMPLING_PERIOD_US;
616 case SENSOR_FLAG_SPECIAL_REPORTING_MODE:
619 case SENSOR_FLAG_ONE_SHOT_MODE:
626 if (sensor[s].is_virtual) {
627 switch (sensor[s].type) {
628 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
629 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
630 return sensor_desc[sensor[s].base[0]].minDelay;
636 sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
638 if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) < 0) {
639 if (sensor[s].mode == MODE_POLL) {
640 /* If we have max specified via a property use it */
641 if (max_from_prop != ANDROID_MAX_FREQ)
642 max_supported_rate = max_from_prop;
644 /* The should rate */
645 max_supported_rate = get_cdd_freq(s, 0);
649 while (*cursor && cursor[0]) {
651 /* Decode a single value */
652 sr = strtod(cursor, NULL);
654 if (sr > max_supported_rate && sr <= max_from_prop)
655 max_supported_rate = sr;
658 while (cursor[0] && !isspace(cursor[0]))
662 while (cursor[0] && isspace(cursor[0]))
667 /* return 0 for wrong values */
668 if (max_supported_rate < 0.1)
671 /* Return microseconds */
672 return (int32_t) (1000000.0 / max_supported_rate);