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_info[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_info[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 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;
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);
154 sprintf(sensor_info[s].friendly_name, "S%d", s);
157 return sensor_info[s].friendly_name;
161 char* sensor_get_vendor (int s)
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;
171 int sensor_get_version (int s)
173 return IIO_SENSOR_HAL_VERSION;
177 float sensor_get_max_range (int s)
182 if (sensor_info[s].max_range != 0.0 ||
183 !sensor_get_fl_prop(s, "max_range", &sensor_info[s].max_range))
184 return sensor_info[s].max_range;
186 /* Try returning a sensible value given the sensor type */
188 /* We should cap returned samples accordingly... */
190 catalog_index = sensor_info[s].catalog_index;
191 sensor_type = sensor_catalog[catalog_index].type;
193 switch (sensor_type) {
194 case SENSOR_TYPE_ACCELEROMETER: /* m/s^2 */
197 case SENSOR_TYPE_MAGNETIC_FIELD: /* micro-tesla */
200 case SENSOR_TYPE_ORIENTATION: /* degrees */
203 case SENSOR_TYPE_GYROSCOPE: /* radians/s */
206 case SENSOR_TYPE_LIGHT: /* SI lux units */
209 case SENSOR_TYPE_AMBIENT_TEMPERATURE: /* °C */
210 case SENSOR_TYPE_TEMPERATURE: /* °C */
211 case SENSOR_TYPE_PROXIMITY: /* centimeters */
212 case SENSOR_TYPE_PRESSURE: /* hecto-pascal */
213 case SENSOR_TYPE_RELATIVE_HUMIDITY: /* percent */
222 float sensor_get_resolution (int s)
224 if (sensor_info[s].resolution != 0.0 ||
225 !sensor_get_fl_prop(s, "resolution", &sensor_info[s].resolution))
226 return sensor_info[s].resolution;
232 float sensor_get_power (int s)
234 /* mA used while sensor is in use ; not sure about volts :) */
235 if (sensor_info[s].power != 0.0 ||
236 !sensor_get_fl_prop(s, "power", &sensor_info[s].power))
237 return sensor_info[s].power;
243 float sensor_get_illumincalib (int s)
245 /* calibrating the ALS Sensor*/
246 if (sensor_info[s].illumincalib != 0.0 ||
247 !sensor_get_fl_prop(s, "illumincalib", &sensor_info[s].illumincalib)) {
248 return sensor_info[s].illumincalib;
255 uint32_t sensor_get_quirks (int s)
257 char quirks_buf[MAX_NAME_SIZE];
259 /* Read and decode quirks property on first reference */
260 if (!(sensor_info[s].quirks & QUIRK_ALREADY_DECODED)) {
261 quirks_buf[0] = '\0';
262 sensor_get_st_prop(s, "quirks", quirks_buf);
264 if (strstr(quirks_buf, "init-rate"))
265 sensor_info[s].quirks |= QUIRK_INITIAL_RATE;
267 if (strstr(quirks_buf, "continuous")) {
268 sensor_info[s].quirks |= QUIRK_CONTINUOUS_DRIVER;
271 if (strstr(quirks_buf, "terse") && !(sensor_info[s].quirks & QUIRK_CONTINUOUS_DRIVER))
272 sensor_info[s].quirks |= QUIRK_TERSE_DRIVER;
274 if (strstr(quirks_buf, "noisy"))
275 sensor_info[s].quirks |= QUIRK_NOISY;
277 sensor_info[s].quirks |= QUIRK_ALREADY_DECODED;
280 return sensor_info[s].quirks;
284 int sensor_get_order (int s, unsigned char map[MAX_CHANNELS])
286 char buf[MAX_NAME_SIZE];
288 int count = sensor_catalog[sensor_info[s].catalog_index].num_channels;
290 if (sensor_get_st_prop(s, "order", buf))
291 return 0; /* No order property */
293 /* Assume ASCII characters, in the '0'..'9' range */
295 for (i=0; i<count; i++)
296 if (buf[i] - '0' >= count) {
297 ALOGE("Order index out of range for sensor %d\n", s);
301 for (i=0; i<count; i++)
302 map[i] = buf[i] - '0';
304 return 1; /* OK to use modified ordering map */
307 char* sensor_get_string_type(int s)
312 catalog_index = sensor_info[s].catalog_index;
313 sensor_type = sensor_catalog[catalog_index].type;
315 switch (sensor_type) {
316 case SENSOR_TYPE_ACCELEROMETER:
317 return SENSOR_STRING_TYPE_ACCELEROMETER;
319 case SENSOR_TYPE_MAGNETIC_FIELD:
320 return SENSOR_STRING_TYPE_MAGNETIC_FIELD;
322 case SENSOR_TYPE_ORIENTATION:
323 return SENSOR_STRING_TYPE_ORIENTATION;
325 case SENSOR_TYPE_GYROSCOPE:
326 return SENSOR_STRING_TYPE_GYROSCOPE;
328 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
329 return SENSOR_STRING_TYPE_GYROSCOPE_UNCALIBRATED;
331 case SENSOR_TYPE_LIGHT:
332 return SENSOR_STRING_TYPE_LIGHT;
334 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
335 return SENSOR_STRING_TYPE_AMBIENT_TEMPERATURE;
337 case SENSOR_TYPE_TEMPERATURE:
338 return SENSOR_STRING_TYPE_TEMPERATURE;
340 case SENSOR_TYPE_PROXIMITY:
341 return SENSOR_STRING_TYPE_PROXIMITY;
343 case SENSOR_TYPE_PRESSURE:
344 return SENSOR_STRING_TYPE_PRESSURE;
346 case SENSOR_TYPE_RELATIVE_HUMIDITY:
347 return SENSOR_STRING_TYPE_RELATIVE_HUMIDITY;
354 flag_t sensor_get_flags (int s)
360 catalog_index = sensor_info[s].catalog_index;
361 sensor_type = sensor_catalog[catalog_index].type;
363 switch (sensor_type) {
364 case SENSOR_TYPE_ACCELEROMETER:
365 case SENSOR_TYPE_MAGNETIC_FIELD:
366 case SENSOR_TYPE_ORIENTATION:
367 case SENSOR_TYPE_GYROSCOPE:
368 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
369 case SENSOR_TYPE_PRESSURE:
370 flags |= SENSOR_FLAG_CONTINUOUS_MODE;
373 case SENSOR_TYPE_LIGHT:
374 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
375 case SENSOR_TYPE_TEMPERATURE:
376 case SENSOR_TYPE_RELATIVE_HUMIDITY:
377 flags |= SENSOR_FLAG_ON_CHANGE_MODE;
381 case SENSOR_TYPE_PROXIMITY:
382 flags |= SENSOR_FLAG_WAKE_UP;
383 flags |= SENSOR_FLAG_ON_CHANGE_MODE;
387 ALOGI("Unknown sensor");
392 int get_cdd_freq(int s, int must)
394 int catalog_index = sensor_info[s].catalog_index;
395 int sensor_type = sensor_catalog[catalog_index].type;
397 switch (sensor_type) {
398 case SENSOR_TYPE_ACCELEROMETER:
399 return (must ? 100 : 200); /* must 100 Hz, should 200 Hz, CDD compliant */
400 case SENSOR_TYPE_GYROSCOPE:
401 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
402 return (must ? 200 : 200); /* must 200 Hz, should 200 Hz, CDD compliant */
403 case SENSOR_TYPE_MAGNETIC_FIELD:
404 return (must ? 10 : 50); /* must 10 Hz, should 50 Hz, CDD compliant */
405 case SENSOR_TYPE_LIGHT:
406 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
407 case SENSOR_TYPE_TEMPERATURE:
408 return (must ? 1 : 2); /* must 1 Hz, should 2Hz, not mentioned in CDD */
414 /* This value is defined only for continuous mode and on-change sensors. It is the delay between
415 * two sensor events corresponding to the lowest frequency that this sensor supports. When lower
416 * frequencies are requested through batch()/setDelay() the events will be generated at this
417 * frequency instead. It can be used by the framework or applications to estimate when the batch
420 * NOTE: 1) period_ns is in nanoseconds where as maxDelay/minDelay are in microseconds.
421 * continuous, on-change: maximum sampling period allowed in microseconds.
422 * one-shot, special : 0
423 * 2) maxDelay should always fit within a 32 bit signed integer. It is declared as 64 bit
424 * on 64 bit architectures only for binary compatibility reasons.
425 * Availability: SENSORS_DEVICE_API_VERSION_1_3
427 max_delay_t sensor_get_max_delay (int s)
429 char avail_sysfs_path[PATH_MAX];
430 int dev_num = sensor_info[s].dev_num;
433 float min_supported_rate = 1000;
436 /* continuous, on-change: maximum sampling period allowed in microseconds.
437 * one-shot, special : 0
439 if (REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_ONE_SHOT_MODE ||
440 REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_SPECIAL_REPORTING_MODE)
443 sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
445 if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) < 0) {
446 /* If poll mode sensor */
447 if (!sensor_info[s].num_channels) {
449 min_supported_rate = get_cdd_freq(s, 1);
453 while (*cursor && cursor[0]) {
455 /* Decode a single value */
456 sr = strtod(cursor, NULL);
458 if (sr < min_supported_rate)
459 min_supported_rate = sr;
462 while (cursor[0] && !isspace(cursor[0]))
466 while (cursor[0] && isspace(cursor[0]))
471 /* return 0 for wrong values */
472 if (min_supported_rate < 0.1)
475 /* Return microseconds */
476 return (max_delay_t)(1000000.0 / min_supported_rate);
479 /* this value depends on the reporting mode:
481 * continuous: minimum sample period allowed in microseconds
484 * special : 0, unless otherwise noted
486 int32_t sensor_get_min_delay(int s)
488 char avail_sysfs_path[PATH_MAX];
489 int dev_num = sensor_info[s].dev_num;
492 float max_supported_rate = 0;
495 /* continuous: minimum sampling period allowed in microseconds.
496 * on-change, special : 0
499 if (REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_ON_CHANGE_MODE ||
500 REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_SPECIAL_REPORTING_MODE)
503 if (REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_ONE_SHOT_MODE)
506 sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
508 if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) < 0) {
509 /* If poll mode sensor */
510 if (!sensor_info[s].num_channels) {
511 /* The should rate */
512 max_supported_rate = get_cdd_freq(s, 0);
516 while (*cursor && cursor[0]) {
518 /* Decode a single value */
519 sr = strtod(cursor, NULL);
521 if (sr > max_supported_rate && sr <= MAX_EVENTS)
522 max_supported_rate = sr;
525 while (cursor[0] && !isspace(cursor[0]))
529 while (cursor[0] && isspace(cursor[0]))
534 /* return 0 for wrong values */
535 if (max_supported_rate < 0.1)
538 /* Return microseconds */
539 return (int32_t)(1000000.0 / max_supported_rate);