2 * Copyright (C) 2014 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)*/
23 * We acquire a number of parameters about sensors by reading properties.
24 * The idea here is that someone (either a script, or daemon, sets them
25 * depending on the set of sensors present on the machine.
27 * There are fallback paths in case the properties are not defined, but it is
28 * highly desirable to at least have the following for each sensor:
30 * ro.iio.anglvel.name = Gyroscope
31 * ro.iio.anglvel.vendor = Intel
32 * ro.iio.anglvel.max_range = 35
33 * ro.iio.anglvel.resolution = 0.002
34 * ro.iio.anglvel.power = 6.1
36 * Besides these, we have a couple of knobs initially used to cope with Intel
37 * Sensor Hub oddities, such as HID inspired units or firmware bugs:
39 * ro.iio.anglvel.transform = ISH
40 * ro.iio.anglvel.quirks = init-rate
42 * The "terse" quirk indicates that the underlying driver only sends events
43 * when the sensor reports a change. The HAL then periodically generates
44 * duplicate events so the sensor behaves as a continously firing one.
46 * The "noisy" quirk indicates that the underlying driver has a unusually high
47 * level of noise in its readings, and that the HAL has to accomodate it
48 * somehow, e.g. in the magnetometer calibration code path.
50 * This one is used specifically to pass a calibration scale to ALS drivers:
52 * ro.iio.illuminance.name = CPLM3218x Ambient Light Sensor
53 * ro.iio.illuminance.vendor = Capella Microsystems
54 * ro.iio.illuminance.max_range = 167000
55 * ro.iio.illuminance.resolution = 1
56 * ro.iio.illuminance.power = .001
57 * ro.iio.illuminance.illumincalib = 7400
59 * There's a 'opt_scale' specifier, documented as follows:
61 * This adds support for a scaling factor that can be expressed
62 * using properties, for all sensors, on a channel basis. That
63 * scaling factor is applied after all other transforms have been
64 * applied, and is intended as a way to compensate for problems
65 * such as an incorrect axis polarity for a given sensor.
67 * The syntax is <usual property prefix>.<channel>.opt_scale, e.g.
68 * ro.iio.accel.y.opt_scale = -1 to negate the sign of the y readings
69 * for the accelerometer.
71 * For sensors using a single channel - and only those - the channel
72 * name is implicitly void and a syntax such as ro.iio.illuminance.
73 * opt_scale = 3 has to be used.
75 * 'panel' and 'rotation' specifiers can be used to express ACPI PLD placement
76 * information ; if found they will be used in priority over the actual ACPI
77 * data. That is intended as a way to verify values during development.
79 * It's possible to use the contents of the iio device name as a way to
80 * discriminate between sensors. Several sensors of the same type can coexist:
81 * e.g. ro.iio.temp.bmg160.name = BMG160 Thermometer will be used in priority
82 * over ro.iio.temp.name = BMC150 Thermometer if the sensor for which we query
83 * properties values happen to have its iio device name set to bmg160.
86 static int sensor_get_st_prop (int s, const char* sel, char val[MAX_NAME_SIZE])
88 char prop_name[PROP_NAME_MAX];
89 char prop_val[PROP_VALUE_MAX];
90 char extended_sel[PROP_VALUE_MAX];
92 int i = sensor[s].catalog_index;
93 const char *prefix = sensor_catalog[i].tag;
95 /* First try most specialized form, like ro.iio.anglvel.bmg160.name */
97 snprintf(extended_sel, PROP_NAME_MAX, "%s.%s",
98 sensor[s].internal_name, sel);
100 snprintf(prop_name, PROP_NAME_MAX, PROP_BASE, prefix, extended_sel);
102 if (property_get(prop_name, prop_val, "")) {
103 strncpy(val, prop_val, MAX_NAME_SIZE-1);
104 val[MAX_NAME_SIZE-1] = '\0';
108 /* Fall back to simple form, like ro.iio.anglvel.name */
110 sprintf(prop_name, PROP_BASE, prefix, sel);
112 if (property_get(prop_name, prop_val, "")) {
113 strncpy(val, prop_val, MAX_NAME_SIZE-1);
114 val[MAX_NAME_SIZE-1] = '\0';
122 int sensor_get_prop (int s, const char* sel, int* val)
124 char buf[MAX_NAME_SIZE];
126 if (sensor_get_st_prop(s, sel, buf))
134 int sensor_get_fl_prop (int s, const char* sel, float* val)
136 char buf[MAX_NAME_SIZE];
138 if (sensor_get_st_prop(s, sel, buf))
141 *val = (float) strtod(buf, NULL);
146 char* sensor_get_name (int s)
148 char buf[MAX_NAME_SIZE];
150 if (sensor[s].is_virtual) {
151 switch (sensor[s].type) {
152 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
153 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
154 strcpy(buf, sensor[sensor[s].base[0]].friendly_name);
155 snprintf(sensor[s].friendly_name,
157 "%s %s", "Uncalibrated", buf);
158 return sensor[s].friendly_name;
165 if (sensor[s].friendly_name[0] != '\0' ||
166 !sensor_get_st_prop(s, "name", sensor[s].friendly_name))
167 return sensor[s].friendly_name;
169 /* If we got a iio device name from sysfs, use it */
170 if (sensor[s].internal_name[0]) {
171 snprintf(sensor[s].friendly_name, MAX_NAME_SIZE, "S%d-%s",
172 s, sensor[s].internal_name);
174 sprintf(sensor[s].friendly_name, "S%d", s);
177 return sensor[s].friendly_name;
181 char* sensor_get_vendor (int s)
183 if (sensor[s].is_virtual) {
184 switch (sensor[s].type) {
185 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
186 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
187 return sensor[sensor[s].base[0]].vendor_name;
196 if (sensor[s].vendor_name[0] ||
197 !sensor_get_st_prop(s, "vendor", sensor[s].vendor_name))
198 return sensor[s].vendor_name;
204 int sensor_get_version (__attribute__((unused)) int s)
206 return IIO_SENSOR_HAL_VERSION;
210 float sensor_get_max_range (int s)
212 if (sensor[s].is_virtual) {
213 switch (sensor[s].type) {
214 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
215 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
216 return sensor[sensor[s].base[0]].max_range;
223 if (sensor[s].max_range != 0.0 ||
224 !sensor_get_fl_prop(s, "max_range", &sensor[s].max_range))
225 return sensor[s].max_range;
227 /* Try returning a sensible value given the sensor type */
229 /* We should cap returned samples accordingly... */
231 switch (sensor[s].type) {
232 case SENSOR_TYPE_ACCELEROMETER: /* m/s^2 */
235 case SENSOR_TYPE_MAGNETIC_FIELD: /* micro-tesla */
238 case SENSOR_TYPE_ORIENTATION: /* degrees */
241 case SENSOR_TYPE_GYROSCOPE: /* radians/s */
244 case SENSOR_TYPE_LIGHT: /* SI lux units */
247 case SENSOR_TYPE_AMBIENT_TEMPERATURE: /* °C */
248 case SENSOR_TYPE_TEMPERATURE: /* °C */
249 case SENSOR_TYPE_PROXIMITY: /* centimeters */
250 case SENSOR_TYPE_PRESSURE: /* hecto-pascal */
251 case SENSOR_TYPE_RELATIVE_HUMIDITY: /* percent */
259 static float sensor_get_min_freq (int s)
262 * Check if a low cap has been specified for this sensor sampling rate.
263 * In some case, even when the driver supports lower rate, we still
264 * wish to receive a certain number of samples per seconds for various
265 * reasons (calibration, filtering, no change in power consumption...).
270 if (!sensor_get_fl_prop(s, "min_freq", &min_freq))
277 static float sensor_get_max_freq (int s)
281 if (!sensor_get_fl_prop(s, "max_freq", &max_freq))
287 int sensor_get_cal_steps (int s)
290 if (!sensor_get_prop(s, "cal_steps", &cal_steps))
296 float sensor_get_resolution (int s)
298 if (sensor[s].is_virtual) {
299 switch (sensor[s].type) {
300 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
301 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
302 return sensor[sensor[s].base[0]].resolution;
309 if (sensor[s].resolution != 0.0 ||
310 !sensor_get_fl_prop(s, "resolution", &sensor[s].resolution))
311 return sensor[s].resolution;
317 float sensor_get_power (int s)
320 if (sensor[s].is_virtual) {
321 switch (sensor[s].type) {
322 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
323 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
324 return sensor[sensor[s].base[0]].power;
331 /* mA used while sensor is in use ; not sure about volts :) */
332 if (sensor[s].power != 0.0 ||
333 !sensor_get_fl_prop(s, "power", &sensor[s].power))
334 return sensor[s].power;
340 float sensor_get_illumincalib (int s)
342 /* calibrating the ALS Sensor*/
343 if (sensor[s].illumincalib != 0.0 ||
344 !sensor_get_fl_prop(s, "illumincalib", &sensor[s].illumincalib)) {
345 return sensor[s].illumincalib;
352 uint32_t sensor_get_quirks (int s)
354 char quirks_buf[MAX_NAME_SIZE];
356 /* Read and decode quirks property on first reference */
357 if (!(sensor[s].quirks & QUIRK_ALREADY_DECODED)) {
358 quirks_buf[0] = '\0';
359 sensor_get_st_prop(s, "quirks", quirks_buf);
361 if (strstr(quirks_buf, "init-rate"))
362 sensor[s].quirks |= QUIRK_INITIAL_RATE;
364 if (strstr(quirks_buf, "continuous"))
365 sensor[s].quirks |= QUIRK_FORCE_CONTINUOUS;
367 if (strstr(quirks_buf, "terse"))
368 sensor[s].quirks |= QUIRK_TERSE_DRIVER;
370 if (strstr(quirks_buf, "noisy"))
371 sensor[s].quirks |= QUIRK_NOISY;
373 sensor[s].quirks |= QUIRK_ALREADY_DECODED;
376 return sensor[s].quirks;
380 int sensor_get_order (int s, unsigned char map[MAX_CHANNELS])
382 char buf[MAX_NAME_SIZE];
384 int count = sensor_catalog[sensor[s].catalog_index].num_channels;
386 if (sensor_get_st_prop(s, "order", buf))
387 return 0; /* No order property */
389 /* Assume ASCII characters, in the '0'..'9' range */
391 for (i=0; i<count; i++)
392 if (buf[i] - '0' >= count) {
393 ALOGE("Order index out of range for sensor %d\n", s);
397 for (i=0; i<count; i++)
398 map[i] = buf[i] - '0';
400 return 1; /* OK to use modified ordering map */
404 char* sensor_get_string_type (int s)
406 switch (sensor[s].type) {
407 case SENSOR_TYPE_ACCELEROMETER:
408 return SENSOR_STRING_TYPE_ACCELEROMETER;
410 case SENSOR_TYPE_MAGNETIC_FIELD:
411 return SENSOR_STRING_TYPE_MAGNETIC_FIELD;
413 case SENSOR_TYPE_ORIENTATION:
414 return SENSOR_STRING_TYPE_ORIENTATION;
416 case SENSOR_TYPE_GYROSCOPE:
417 return SENSOR_STRING_TYPE_GYROSCOPE;
419 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
420 return SENSOR_STRING_TYPE_GYROSCOPE_UNCALIBRATED;
422 case SENSOR_TYPE_LIGHT:
423 return SENSOR_STRING_TYPE_LIGHT;
425 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
426 return SENSOR_STRING_TYPE_AMBIENT_TEMPERATURE;
428 case SENSOR_TYPE_TEMPERATURE:
429 return SENSOR_STRING_TYPE_TEMPERATURE;
431 case SENSOR_TYPE_PROXIMITY:
432 return SENSOR_STRING_TYPE_PROXIMITY;
434 case SENSOR_TYPE_PRESSURE:
435 return SENSOR_STRING_TYPE_PRESSURE;
437 case SENSOR_TYPE_RELATIVE_HUMIDITY:
438 return SENSOR_STRING_TYPE_RELATIVE_HUMIDITY;
446 flag_t sensor_get_flags (int s)
450 switch (sensor[s].type) {
451 case SENSOR_TYPE_LIGHT:
452 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
453 case SENSOR_TYPE_TEMPERATURE:
454 case SENSOR_TYPE_RELATIVE_HUMIDITY:
455 flags |= SENSOR_FLAG_ON_CHANGE_MODE;
459 case SENSOR_TYPE_PROXIMITY:
460 flags |= SENSOR_FLAG_WAKE_UP;
461 flags |= SENSOR_FLAG_ON_CHANGE_MODE;
465 ALOGI("Unknown sensor");
471 static int get_cdd_freq (int s, int must)
473 switch (sensor[s].type) {
474 case SENSOR_TYPE_ACCELEROMETER:
475 return (must ? 100 : 200); /* must 100 Hz, should 200 Hz, CDD compliant */
477 case SENSOR_TYPE_GYROSCOPE:
478 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
479 return (must ? 200 : 200); /* must 200 Hz, should 200 Hz, CDD compliant */
481 case SENSOR_TYPE_MAGNETIC_FIELD:
482 return (must ? 10 : 50); /* must 10 Hz, should 50 Hz, CDD compliant */
484 case SENSOR_TYPE_LIGHT:
485 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
486 case SENSOR_TYPE_TEMPERATURE:
487 return (must ? 1 : 2); /* must 1 Hz, should 2Hz, not mentioned in CDD */
490 return 1; /* Use 1 Hz by default, e.g. for proximity */
495 * 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
496 * this sensor supports. When lower frequencies are requested through batch()/setDelay() the events will be generated at this frequency instead. It can be used
497 * 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
498 * 64 bit on 64 bit architectures only for binary compatibility reasons. Availability: SENSORS_DEVICE_API_VERSION_1_3
500 max_delay_t sensor_get_max_delay (int s)
502 char avail_sysfs_path[PATH_MAX];
503 int dev_num = sensor[s].dev_num;
506 float min_supported_rate = 1000;
511 * continuous, on-change: maximum sampling period allowed in microseconds.
512 * one-shot, special : 0
514 switch (REPORTING_MODE(sensor_desc[s].flags)) {
515 case SENSOR_FLAG_ONE_SHOT_MODE:
516 case SENSOR_FLAG_SPECIAL_REPORTING_MODE:
519 case SENSOR_FLAG_ON_CHANGE_MODE:
520 return MAX_ON_CHANGE_SAMPLING_PERIOD_US;
526 if (sensor[s].is_virtual) {
527 switch (sensor[s].type) {
528 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
529 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
530 return sensor_desc[sensor[s].base[0]].maxDelay;
535 sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
537 if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) < 0) {
538 if (sensor[s].is_polling) {
540 min_supported_rate = get_cdd_freq(s, 1);
544 while (*cursor && cursor[0]) {
546 /* Decode a single value */
547 sr = strtod(cursor, NULL);
549 if (sr < min_supported_rate)
550 min_supported_rate = sr;
553 while (cursor[0] && !isspace(cursor[0]))
557 while (cursor[0] && isspace(cursor[0]))
562 /* Check if a minimum rate was specified for this sensor */
563 rate_cap = sensor_get_min_freq(s);
565 if (min_supported_rate < rate_cap)
566 min_supported_rate = rate_cap;
568 /* return 0 for wrong values */
569 if (min_supported_rate < 0.1)
572 /* Return microseconds */
573 return (max_delay_t) (1000000.0 / min_supported_rate);
577 int32_t sensor_get_min_delay (int s)
579 char avail_sysfs_path[PATH_MAX];
580 int dev_num = sensor[s].dev_num;
583 float max_supported_rate = 0;
586 /* continuous, on change: minimum sampling period allowed in microseconds.
587 * special : 0, unless otherwise noted
590 switch (REPORTING_MODE(sensor_desc[s].flags)) {
591 case SENSOR_FLAG_ON_CHANGE_MODE:
592 return MIN_ON_CHANGE_SAMPLING_PERIOD_US;
594 case SENSOR_FLAG_SPECIAL_REPORTING_MODE:
597 case SENSOR_FLAG_ONE_SHOT_MODE:
604 if (sensor[s].is_virtual) {
605 switch (sensor[s].type) {
606 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
607 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
608 return sensor_desc[sensor[s].base[0]].minDelay;
614 sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
616 if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) < 0) {
617 if (sensor[s].is_polling) {
618 /* The should rate */
619 max_supported_rate = get_cdd_freq(s, 0);
623 while (*cursor && cursor[0]) {
625 /* Decode a single value */
626 sr = strtod(cursor, NULL);
628 if (sr > max_supported_rate && sr <= sensor_get_max_freq(s))
629 max_supported_rate = sr;
632 while (cursor[0] && !isspace(cursor[0]))
636 while (cursor[0] && isspace(cursor[0]))
641 /* return 0 for wrong values */
642 if (max_supported_rate < 0.1)
645 /* Return microseconds */
646 return (int32_t) (1000000.0 / max_supported_rate);