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
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.
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:
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
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:
36 * ro.iio.anglvel.transform = ISH
37 * ro.iio.anglvel.quirks = init-rate
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.
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.
47 * This one is used specifically to pass a calibration scale to ALS drivers:
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
56 * There's a 'opt_scale' specifier, documented as follows:
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.
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.
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.
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.
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.
83 static int sensor_get_st_prop (int s, const char* sel, char val[MAX_NAME_SIZE])
85 char prop_name[PROP_NAME_MAX];
86 char prop_val[PROP_VALUE_MAX];
87 char extended_sel[PROP_VALUE_MAX];
89 int i = sensor[s].catalog_index;
90 const char *prefix = sensor_catalog[i].tag;
92 /* First try most specialized form, like ro.iio.anglvel.bmg160.name */
94 snprintf(extended_sel, PROP_NAME_MAX, "%s.%s",
95 sensor[s].internal_name, sel);
97 snprintf(prop_name, PROP_NAME_MAX, PROP_BASE, prefix, extended_sel);
99 if (property_get(prop_name, prop_val, "")) {
100 strncpy(val, prop_val, MAX_NAME_SIZE-1);
101 val[MAX_NAME_SIZE-1] = '\0';
105 /* Fall back to simple form, like ro.iio.anglvel.name */
107 sprintf(prop_name, PROP_BASE, prefix, sel);
109 if (property_get(prop_name, prop_val, "")) {
110 strncpy(val, prop_val, MAX_NAME_SIZE-1);
111 val[MAX_NAME_SIZE-1] = '\0';
119 int sensor_get_prop (int s, const char* sel, int* val)
121 char buf[MAX_NAME_SIZE];
123 if (sensor_get_st_prop(s, sel, buf))
131 int sensor_get_fl_prop (int s, const char* sel, float* val)
133 char buf[MAX_NAME_SIZE];
135 if (sensor_get_st_prop(s, sel, buf))
138 *val = (float) strtod(buf, NULL);
143 char* sensor_get_name (int s)
145 char buf[MAX_NAME_SIZE];
147 if (sensor[s].is_virtual) {
148 switch (sensor[s].type) {
149 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
150 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
151 strcpy(buf, sensor[sensor[s].base[0]].friendly_name);
152 snprintf(sensor[s].friendly_name,
154 "%s %s", "Uncalibrated", buf);
155 return sensor[s].friendly_name;
162 if (sensor[s].friendly_name[0] != '\0' ||
163 !sensor_get_st_prop(s, "name", sensor[s].friendly_name))
164 return sensor[s].friendly_name;
166 /* If we got a iio device name from sysfs, use it */
167 if (sensor[s].internal_name[0]) {
168 snprintf(sensor[s].friendly_name, MAX_NAME_SIZE, "S%d-%s",
169 s, sensor[s].internal_name);
171 sprintf(sensor[s].friendly_name, "S%d", s);
174 return sensor[s].friendly_name;
178 char* sensor_get_vendor (int s)
180 if (sensor[s].is_virtual) {
181 switch (sensor[s].type) {
182 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
183 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
184 return sensor[sensor[s].base[0]].vendor_name;
193 if (sensor[s].vendor_name[0] ||
194 !sensor_get_st_prop(s, "vendor", sensor[s].vendor_name))
195 return sensor[s].vendor_name;
201 int sensor_get_version (__attribute__((unused)) int s)
203 return IIO_SENSOR_HAL_VERSION;
207 float sensor_get_max_range (int s)
209 if (sensor[s].is_virtual) {
210 switch (sensor[s].type) {
211 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
212 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
213 return sensor[sensor[s].base[0]].max_range;
220 if (sensor[s].max_range != 0.0 ||
221 !sensor_get_fl_prop(s, "max_range", &sensor[s].max_range))
222 return sensor[s].max_range;
224 /* Try returning a sensible value given the sensor type */
226 /* We should cap returned samples accordingly... */
228 switch (sensor[s].type) {
229 case SENSOR_TYPE_ACCELEROMETER: /* m/s^2 */
232 case SENSOR_TYPE_MAGNETIC_FIELD: /* micro-tesla */
235 case SENSOR_TYPE_ORIENTATION: /* degrees */
238 case SENSOR_TYPE_GYROSCOPE: /* radians/s */
241 case SENSOR_TYPE_LIGHT: /* SI lux units */
244 case SENSOR_TYPE_AMBIENT_TEMPERATURE: /* °C */
245 case SENSOR_TYPE_TEMPERATURE: /* °C */
246 case SENSOR_TYPE_PROXIMITY: /* centimeters */
247 case SENSOR_TYPE_PRESSURE: /* hecto-pascal */
248 case SENSOR_TYPE_RELATIVE_HUMIDITY: /* percent */
256 static float sensor_get_min_freq (int s)
259 * Check if a low cap has been specified for this sensor sampling rate.
260 * In some case, even when the driver supports lower rate, we still
261 * wish to receive a certain number of samples per seconds for various
262 * reasons (calibration, filtering, no change in power consumption...).
267 if (!sensor_get_fl_prop(s, "min_freq", &min_freq))
274 static float sensor_get_max_freq (int s)
278 if (!sensor_get_fl_prop(s, "max_freq", &max_freq))
284 int sensor_get_cal_steps (int s)
287 if (!sensor_get_prop(s, "cal_steps", &cal_steps))
293 float sensor_get_resolution (int s)
295 if (sensor[s].is_virtual) {
296 switch (sensor[s].type) {
297 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
298 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
299 return sensor[sensor[s].base[0]].resolution;
306 if (sensor[s].resolution != 0.0 ||
307 !sensor_get_fl_prop(s, "resolution", &sensor[s].resolution))
308 return sensor[s].resolution;
314 float sensor_get_power (int s)
317 if (sensor[s].is_virtual) {
318 switch (sensor[s].type) {
319 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
320 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
321 return sensor[sensor[s].base[0]].power;
328 /* mA used while sensor is in use ; not sure about volts :) */
329 if (sensor[s].power != 0.0 ||
330 !sensor_get_fl_prop(s, "power", &sensor[s].power))
331 return sensor[s].power;
337 float sensor_get_illumincalib (int s)
339 /* calibrating the ALS Sensor*/
340 if (sensor[s].illumincalib != 0.0 ||
341 !sensor_get_fl_prop(s, "illumincalib", &sensor[s].illumincalib)) {
342 return sensor[s].illumincalib;
349 uint32_t sensor_get_quirks (int s)
351 char quirks_buf[MAX_NAME_SIZE];
353 /* Read and decode quirks property on first reference */
354 if (!(sensor[s].quirks & QUIRK_ALREADY_DECODED)) {
355 quirks_buf[0] = '\0';
356 sensor_get_st_prop(s, "quirks", quirks_buf);
358 if (strstr(quirks_buf, "init-rate"))
359 sensor[s].quirks |= QUIRK_INITIAL_RATE;
361 if (strstr(quirks_buf, "continuous"))
362 sensor[s].quirks |= QUIRK_FORCE_CONTINUOUS;
364 if (strstr(quirks_buf, "terse"))
365 sensor[s].quirks |= QUIRK_TERSE_DRIVER;
367 if (strstr(quirks_buf, "noisy"))
368 sensor[s].quirks |= QUIRK_NOISY;
370 sensor[s].quirks |= QUIRK_ALREADY_DECODED;
373 return sensor[s].quirks;
377 int sensor_get_order (int s, unsigned char map[MAX_CHANNELS])
379 char buf[MAX_NAME_SIZE];
381 int count = sensor_catalog[sensor[s].catalog_index].num_channels;
383 if (sensor_get_st_prop(s, "order", buf))
384 return 0; /* No order property */
386 /* Assume ASCII characters, in the '0'..'9' range */
388 for (i=0; i<count; i++)
389 if (buf[i] - '0' >= count) {
390 ALOGE("Order index out of range for sensor %d\n", s);
394 for (i=0; i<count; i++)
395 map[i] = buf[i] - '0';
397 return 1; /* OK to use modified ordering map */
400 char* sensor_get_string_type (int s)
402 switch (sensor[s].type) {
403 case SENSOR_TYPE_ACCELEROMETER:
404 return SENSOR_STRING_TYPE_ACCELEROMETER;
406 case SENSOR_TYPE_MAGNETIC_FIELD:
407 return SENSOR_STRING_TYPE_MAGNETIC_FIELD;
409 case SENSOR_TYPE_ORIENTATION:
410 return SENSOR_STRING_TYPE_ORIENTATION;
412 case SENSOR_TYPE_GYROSCOPE:
413 return SENSOR_STRING_TYPE_GYROSCOPE;
415 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
416 return SENSOR_STRING_TYPE_GYROSCOPE_UNCALIBRATED;
418 case SENSOR_TYPE_LIGHT:
419 return SENSOR_STRING_TYPE_LIGHT;
421 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
422 return SENSOR_STRING_TYPE_AMBIENT_TEMPERATURE;
424 case SENSOR_TYPE_TEMPERATURE:
425 return SENSOR_STRING_TYPE_TEMPERATURE;
427 case SENSOR_TYPE_PROXIMITY:
428 return SENSOR_STRING_TYPE_PROXIMITY;
430 case SENSOR_TYPE_PRESSURE:
431 return SENSOR_STRING_TYPE_PRESSURE;
433 case SENSOR_TYPE_RELATIVE_HUMIDITY:
434 return SENSOR_STRING_TYPE_RELATIVE_HUMIDITY;
441 flag_t sensor_get_flags (int s)
445 switch (sensor[s].type) {
446 case SENSOR_TYPE_ACCELEROMETER:
447 case SENSOR_TYPE_MAGNETIC_FIELD:
448 case SENSOR_TYPE_ORIENTATION:
449 case SENSOR_TYPE_GYROSCOPE:
450 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
451 case SENSOR_TYPE_PRESSURE:
452 flags |= SENSOR_FLAG_CONTINUOUS_MODE;
455 case SENSOR_TYPE_LIGHT:
456 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
457 case SENSOR_TYPE_TEMPERATURE:
458 case SENSOR_TYPE_RELATIVE_HUMIDITY:
459 flags |= SENSOR_FLAG_ON_CHANGE_MODE;
463 case SENSOR_TYPE_PROXIMITY:
464 flags |= SENSOR_FLAG_WAKE_UP;
465 flags |= SENSOR_FLAG_ON_CHANGE_MODE;
469 ALOGI("Unknown sensor");
474 int get_cdd_freq (int s, int must)
476 switch (sensor[s].type) {
477 case SENSOR_TYPE_ACCELEROMETER:
478 return (must ? 100 : 200); /* must 100 Hz, should 200 Hz, CDD compliant */
479 case SENSOR_TYPE_GYROSCOPE:
480 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
481 return (must ? 200 : 200); /* must 200 Hz, should 200 Hz, CDD compliant */
482 case SENSOR_TYPE_MAGNETIC_FIELD:
483 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 */
493 /* This value is defined only for continuous mode and on-change sensors. It is the delay between
494 * two sensor events corresponding to the lowest frequency that this sensor supports. When lower
495 * frequencies are requested through batch()/setDelay() the events will be generated at this
496 * frequency instead. It can be used by the framework or applications to estimate when the batch
499 * NOTE: 1) period_ns is in nanoseconds where as maxDelay/minDelay are in microseconds.
500 * continuous, on-change: maximum sampling period allowed in microseconds.
501 * one-shot, special : 0
502 * 2) maxDelay should always fit within a 32 bit signed integer. It is declared as 64 bit
503 * on 64 bit architectures only for binary compatibility reasons.
504 * Availability: SENSORS_DEVICE_API_VERSION_1_3
506 max_delay_t sensor_get_max_delay (int s)
508 char avail_sysfs_path[PATH_MAX];
509 int dev_num = sensor[s].dev_num;
512 float min_supported_rate = 1000;
516 /* continuous, on-change: maximum sampling period allowed in microseconds.
517 * one-shot, special : 0
519 if (REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_ONE_SHOT_MODE ||
520 REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_SPECIAL_REPORTING_MODE)
523 if (sensor[s].is_virtual) {
524 switch (sensor[s].type) {
525 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
526 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
527 return sensor_desc[sensor[s].base[0]].maxDelay;
532 sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
534 if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) < 0) {
535 /* If poll mode sensor */
536 if (!sensor[s].num_channels) {
538 min_supported_rate = get_cdd_freq(s, 1);
542 while (*cursor && cursor[0]) {
544 /* Decode a single value */
545 sr = strtod(cursor, NULL);
547 if (sr < min_supported_rate)
548 min_supported_rate = sr;
551 while (cursor[0] && !isspace(cursor[0]))
555 while (cursor[0] && isspace(cursor[0]))
560 /* Check if a minimum rate was specified for this sensor */
561 rate_cap = sensor_get_min_freq(s);
563 if (min_supported_rate < rate_cap)
564 min_supported_rate = rate_cap;
566 /* return 0 for wrong values */
567 if (min_supported_rate < 0.1)
570 /* Return microseconds */
571 return (max_delay_t)(1000000.0 / min_supported_rate);
574 /* this value depends on the reporting mode:
576 * continuous: minimum sample period allowed in microseconds
579 * special : 0, unless otherwise noted
581 int32_t sensor_get_min_delay(int s)
583 char avail_sysfs_path[PATH_MAX];
584 int dev_num = sensor[s].dev_num;
587 float max_supported_rate = 0;
590 /* continuous: minimum sampling period allowed in microseconds.
591 * on-change, special : 0
594 if (REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_ON_CHANGE_MODE ||
595 REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_SPECIAL_REPORTING_MODE)
598 if (REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_ONE_SHOT_MODE)
601 if (sensor[s].is_virtual) {
602 switch (sensor[s].type) {
603 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
604 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
605 return sensor_desc[sensor[s].base[0]].minDelay;
611 sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
613 if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) < 0) {
614 /* If poll mode sensor */
615 if (!sensor[s].num_channels) {
616 /* The should rate */
617 max_supported_rate = get_cdd_freq(s, 0);
621 while (*cursor && cursor[0]) {
623 /* Decode a single value */
624 sr = strtod(cursor, NULL);
626 if (sr > max_supported_rate && sr <= sensor_get_max_freq(s))
627 max_supported_rate = sr;
630 while (cursor[0] && !isspace(cursor[0]))
634 while (cursor[0] && isspace(cursor[0]))
639 /* return 0 for wrong values */
640 if (max_supported_rate < 0.1)
643 /* Return microseconds */
644 return (int32_t)(1000000.0 / max_supported_rate);