2 * Copyright (C) 2014 Intel Corporation.
7 #include <cutils/properties.h>
8 #include <hardware/sensors.h>
10 #include "enumeration.h"
11 #include "description.h"
13 #define IIO_SENSOR_HAL_VERSION 1
18 * We acquire a number of parameters about sensors by reading properties.
19 * The idea here is that someone (either a script, or daemon, sets them
20 * depending on the set of sensors present on the machine.
22 * There are fallback paths in case the properties are not defined, but it is
23 * highly desirable to at least have the following for each sensor:
25 * ro.iio.anglvel.name = Gyroscope
26 * ro.iio.anglvel.vendor = Intel
27 * ro.iio.anglvel.max_range = 35
28 * ro.iio.anglvel.resolution = 0.002
29 * ro.iio.anglvel.power = 6.1
31 * Besides these, we have a couple of knobs initially used to cope with Intel
32 * Sensor Hub oddities, such as HID inspired units or firmware bugs:
34 * ro.iio.anglvel.transform = ISH
35 * ro.iio.anglvel.quirks = init-rate
37 * The "terse" quirk indicates that the underlying driver only sends events
38 * when the sensor reports a change. The HAL then periodically generates
39 * duplicate events so the sensor behaves as a continously firing one.
41 * The "noisy" quirk indicates that the underlying driver has a unusually high
42 * level of noise in its readings, and that the HAL has to accomodate it
43 * somehow, e.g. in the magnetometer calibration code path.
45 * This one is used specifically to pass a calibration scale to ALS drivers:
47 * ro.iio.illuminance.name = CPLM3218x Ambient Light Sensor
48 * ro.iio.illuminance.vendor = Capella Microsystems
49 * ro.iio.illuminance.max_range = 167000
50 * ro.iio.illuminance.resolution = 1
51 * ro.iio.illuminance.power = .001
52 * ro.iio.illuminance.illumincalib = 7400
54 * There's a 'opt_scale' specifier, documented as follows:
56 * This adds support for a scaling factor that can be expressed
57 * using properties, for all sensors, on a channel basis. That
58 * scaling factor is applied after all other transforms have been
59 * applied, and is intended as a way to compensate for problems
60 * such as an incorrect axis polarity for a given sensor.
62 * The syntax is <usual property prefix>.<channel>.opt_scale, e.g.
63 * ro.iio.accel.y.opt_scale = -1 to negate the sign of the y readings
64 * for the accelerometer.
66 * For sensors using a single channel - and only those - the channel
67 * name is implicitly void and a syntax such as ro.iio.illuminance.
68 * opt_scale = 3 has to be used.
70 * 'panel' and 'rotation' specifiers can be used to express ACPI PLD placement
71 * information ; if found they will be used in priority over the actual ACPI
72 * data. That is intended as a way to verify values during development.
74 * It's possible to use the contents of the iio device name as a way to
75 * discriminate between sensors. Several sensors of the same type can coexist:
76 * e.g. ro.iio.temp.bmg160.name = BMG160 Thermometer will be used in priority
77 * over ro.iio.temp.name = BMC150 Thermometer if the sensor for which we query
78 * properties values happen to have its iio device name set to bmg160.
81 static int sensor_get_st_prop (int s, const char* sel, char val[MAX_NAME_SIZE])
83 char prop_name[PROP_NAME_MAX];
84 char prop_val[PROP_VALUE_MAX];
85 char extended_sel[PROP_VALUE_MAX];
87 int i = sensor_info[s].catalog_index;
88 const char *prefix = sensor_catalog[i].tag;
90 /* First try most specialized form, like ro.iio.anglvel.bmg160.name */
92 snprintf(extended_sel, PROP_NAME_MAX, "%s.%s",
93 sensor_info[s].internal_name, sel);
95 snprintf(prop_name, PROP_NAME_MAX, PROP_BASE, prefix, extended_sel);
97 if (property_get(prop_name, prop_val, "")) {
98 strncpy(val, prop_val, MAX_NAME_SIZE-1);
99 val[MAX_NAME_SIZE-1] = '\0';
103 /* Fall back to simple form, like ro.iio.anglvel.name */
105 sprintf(prop_name, PROP_BASE, prefix, sel);
107 if (property_get(prop_name, prop_val, "")) {
108 strncpy(val, prop_val, MAX_NAME_SIZE-1);
109 val[MAX_NAME_SIZE-1] = '\0';
117 int sensor_get_prop (int s, const char* sel, int* val)
119 char buf[MAX_NAME_SIZE];
121 if (sensor_get_st_prop(s, sel, buf))
129 int sensor_get_fl_prop (int s, const char* sel, float* val)
131 char buf[MAX_NAME_SIZE];
133 if (sensor_get_st_prop(s, sel, buf))
136 *val = (float) strtod(buf, NULL);
141 char* sensor_get_name (int s)
143 if (sensor_info[s].friendly_name[0] != '\0' ||
144 !sensor_get_st_prop(s, "name", sensor_info[s].friendly_name))
145 return sensor_info[s].friendly_name;
147 /* If we got a iio device name from sysfs, use it */
148 if (sensor_info[s].internal_name[0]) {
149 snprintf(sensor_info[s].friendly_name, MAX_NAME_SIZE, "S%d-%s",
150 s, sensor_info[s].internal_name);
152 sprintf(sensor_info[s].friendly_name, "S%d", s);
155 return sensor_info[s].friendly_name;
159 char* sensor_get_vendor (int s)
161 if (sensor_info[s].vendor_name[0] ||
162 !sensor_get_st_prop(s, "vendor", sensor_info[s].vendor_name))
163 return sensor_info[s].vendor_name;
169 int sensor_get_version (int s)
171 return IIO_SENSOR_HAL_VERSION;
175 float sensor_get_max_range (int s)
180 if (sensor_info[s].max_range != 0.0 ||
181 !sensor_get_fl_prop(s, "max_range", &sensor_info[s].max_range))
182 return sensor_info[s].max_range;
184 /* Try returning a sensible value given the sensor type */
186 /* We should cap returned samples accordingly... */
188 catalog_index = sensor_info[s].catalog_index;
189 sensor_type = sensor_catalog[catalog_index].type;
191 switch (sensor_type) {
192 case SENSOR_TYPE_ACCELEROMETER: /* m/s^2 */
195 case SENSOR_TYPE_MAGNETIC_FIELD: /* micro-tesla */
198 case SENSOR_TYPE_ORIENTATION: /* degrees */
201 case SENSOR_TYPE_GYROSCOPE: /* radians/s */
204 case SENSOR_TYPE_LIGHT: /* SI lux units */
207 case SENSOR_TYPE_AMBIENT_TEMPERATURE: /* °C */
208 case SENSOR_TYPE_TEMPERATURE: /* °C */
209 case SENSOR_TYPE_PROXIMITY: /* centimeters */
210 case SENSOR_TYPE_PRESSURE: /* hecto-pascal */
211 case SENSOR_TYPE_RELATIVE_HUMIDITY: /* percent */
220 float sensor_get_resolution (int s)
222 if (sensor_info[s].resolution != 0.0 ||
223 !sensor_get_fl_prop(s, "resolution", &sensor_info[s].resolution))
224 return sensor_info[s].resolution;
230 float sensor_get_power (int s)
232 /* mA used while sensor is in use ; not sure about volts :) */
233 if (sensor_info[s].power != 0.0 ||
234 !sensor_get_fl_prop(s, "power", &sensor_info[s].power))
235 return sensor_info[s].power;
241 float sensor_get_illumincalib (int s)
243 /* calibrating the ALS Sensor*/
244 if (sensor_info[s].illumincalib != 0.0 ||
245 !sensor_get_fl_prop(s, "illumincalib", &sensor_info[s].illumincalib)) {
246 return sensor_info[s].illumincalib;
253 uint32_t sensor_get_quirks (int s)
255 char quirks_buf[MAX_NAME_SIZE];
257 /* Read and decode quirks property on first reference */
258 if (!(sensor_info[s].quirks & QUIRK_ALREADY_DECODED)) {
259 quirks_buf[0] = '\0';
260 sensor_get_st_prop(s, "quirks", quirks_buf);
262 if (strstr(quirks_buf, "init-rate"))
263 sensor_info[s].quirks |= QUIRK_INITIAL_RATE;
265 if (strstr(quirks_buf, "continuous")) {
266 sensor_info[s].quirks |= QUIRK_CONTINUOUS_DRIVER;
269 if (strstr(quirks_buf, "terse") && !(sensor_info[s].quirks & QUIRK_CONTINUOUS_DRIVER))
270 sensor_info[s].quirks |= QUIRK_TERSE_DRIVER;
272 if (strstr(quirks_buf, "noisy"))
273 sensor_info[s].quirks |= QUIRK_NOISY;
275 sensor_info[s].quirks |= QUIRK_ALREADY_DECODED;
278 return sensor_info[s].quirks;
282 int sensor_get_order (int s, unsigned char map[MAX_CHANNELS])
284 char buf[MAX_NAME_SIZE];
286 int count = sensor_catalog[sensor_info[s].catalog_index].num_channels;
288 if (sensor_get_st_prop(s, "order", buf))
289 return 0; /* No order property */
291 /* Assume ASCII characters, in the '0'..'9' range */
293 for (i=0; i<count; i++)
294 if (buf[i] - '0' >= count) {
295 ALOGE("Order index out of range for sensor %d\n", s);
299 for (i=0; i<count; i++)
300 map[i] = buf[i] - '0';
302 return 1; /* OK to use modified ordering map */
305 char* sensor_get_string_type(int s)
310 catalog_index = sensor_info[s].catalog_index;
311 sensor_type = sensor_catalog[catalog_index].type;
313 switch (sensor_type) {
314 case SENSOR_TYPE_ACCELEROMETER:
315 return SENSOR_STRING_TYPE_ACCELEROMETER;
317 case SENSOR_TYPE_MAGNETIC_FIELD:
318 return SENSOR_STRING_TYPE_MAGNETIC_FIELD;
320 case SENSOR_TYPE_ORIENTATION:
321 return SENSOR_STRING_TYPE_ORIENTATION;
323 case SENSOR_TYPE_GYROSCOPE:
324 return SENSOR_STRING_TYPE_GYROSCOPE;
326 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
327 return SENSOR_STRING_TYPE_GYROSCOPE_UNCALIBRATED;
329 case SENSOR_TYPE_LIGHT:
330 return SENSOR_STRING_TYPE_LIGHT;
332 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
333 return SENSOR_STRING_TYPE_AMBIENT_TEMPERATURE;
335 case SENSOR_TYPE_TEMPERATURE:
336 return SENSOR_STRING_TYPE_TEMPERATURE;
338 case SENSOR_TYPE_PROXIMITY:
339 return SENSOR_STRING_TYPE_PROXIMITY;
341 case SENSOR_TYPE_PRESSURE:
342 return SENSOR_STRING_TYPE_PRESSURE;
344 case SENSOR_TYPE_RELATIVE_HUMIDITY:
345 return SENSOR_STRING_TYPE_RELATIVE_HUMIDITY;
352 flag_t sensor_get_flags (int s)
358 catalog_index = sensor_info[s].catalog_index;
359 sensor_type = sensor_catalog[catalog_index].type;
361 switch (sensor_type) {
362 case SENSOR_TYPE_ACCELEROMETER:
363 case SENSOR_TYPE_MAGNETIC_FIELD:
364 case SENSOR_TYPE_ORIENTATION:
365 case SENSOR_TYPE_GYROSCOPE:
366 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
367 case SENSOR_TYPE_PRESSURE:
368 flags |= SENSOR_FLAG_CONTINUOUS_MODE;
371 case SENSOR_TYPE_LIGHT:
372 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
373 case SENSOR_TYPE_TEMPERATURE:
374 case SENSOR_TYPE_RELATIVE_HUMIDITY:
375 flags |= SENSOR_FLAG_ON_CHANGE_MODE;
379 case SENSOR_TYPE_PROXIMITY:
380 flags |= SENSOR_FLAG_WAKE_UP;
381 flags |= SENSOR_FLAG_ON_CHANGE_MODE;
385 ALOGI("Unknown sensor");
390 int get_cdd_freq(int s, int must)
392 int catalog_index = sensor_info[s].catalog_index;
393 int sensor_type = sensor_catalog[catalog_index].type;
395 switch (sensor_type) {
396 case SENSOR_TYPE_ACCELEROMETER:
397 return (must ? 100 : 200); /* must 100 Hz, should 200 Hz, CDD compliant */
398 case SENSOR_TYPE_GYROSCOPE:
399 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
400 return (must ? 200 : 200); /* must 200 Hz, should 200 Hz, CDD compliant */
401 case SENSOR_TYPE_MAGNETIC_FIELD:
402 return (must ? 10 : 50); /* must 10 Hz, should 50 Hz, CDD compliant */
403 case SENSOR_TYPE_LIGHT:
404 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
405 case SENSOR_TYPE_TEMPERATURE:
406 return (must ? 1 : 2); /* must 1 Hz, should 2Hz, not mentioned in CDD */
412 /* This value is defined only for continuous mode and on-change sensors. It is the delay between
413 * two sensor events corresponding to the lowest frequency that this sensor supports. When lower
414 * frequencies are requested through batch()/setDelay() the events will be generated at this
415 * frequency instead. It can be used by the framework or applications to estimate when the batch
418 * NOTE: 1) period_ns is in nanoseconds where as maxDelay/minDelay are in microseconds.
419 * continuous, on-change: maximum sampling period allowed in microseconds.
420 * one-shot, special : 0
421 * 2) maxDelay should always fit within a 32 bit signed integer. It is declared as 64 bit
422 * on 64 bit architectures only for binary compatibility reasons.
423 * Availability: SENSORS_DEVICE_API_VERSION_1_3
425 max_delay_t sensor_get_max_delay (int s)
427 char avail_sysfs_path[PATH_MAX];
428 int dev_num = sensor_info[s].dev_num;
431 float min_supported_rate = 1000;
434 /* continuous, on-change: maximum sampling period allowed in microseconds.
435 * one-shot, special : 0
437 if (REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_ONE_SHOT_MODE ||
438 REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_SPECIAL_REPORTING_MODE)
441 sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
443 if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) < 0) {
444 /* If poll mode sensor */
445 if (!sensor_info[s].num_channels) {
447 min_supported_rate = get_cdd_freq(s, 1);
451 while (*cursor && cursor[0]) {
453 /* Decode a single value */
454 sr = strtod(cursor, NULL);
456 if (sr < min_supported_rate)
457 min_supported_rate = sr;
460 while (cursor[0] && !isspace(cursor[0]))
464 while (cursor[0] && isspace(cursor[0]))
469 /* return 0 for wrong values */
470 if (min_supported_rate < 0.1)
473 /* Return microseconds */
474 return (max_delay_t)(1000000.0 / min_supported_rate);
477 /* this value depends on the reporting mode:
479 * continuous: minimum sample period allowed in microseconds
482 * special : 0, unless otherwise noted
484 int32_t sensor_get_min_delay(int s)
486 char avail_sysfs_path[PATH_MAX];
487 int dev_num = sensor_info[s].dev_num;
490 float max_supported_rate = 0;
493 /* continuous: minimum sampling period allowed in microseconds.
494 * on-change, special : 0
497 if (REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_ON_CHANGE_MODE ||
498 REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_SPECIAL_REPORTING_MODE)
501 if (REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_ONE_SHOT_MODE)
504 sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
506 if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) < 0) {
507 /* If poll mode sensor */
508 if (!sensor_info[s].num_channels) {
509 /* The should rate */
510 max_supported_rate = get_cdd_freq(s, 0);
514 while (*cursor && cursor[0]) {
516 /* Decode a single value */
517 sr = strtod(cursor, NULL);
519 if (sr > max_supported_rate && sr <= MAX_EVENTS)
520 max_supported_rate = sr;
523 while (cursor[0] && !isspace(cursor[0]))
527 while (cursor[0] && isspace(cursor[0]))
532 /* return 0 for wrong values */
533 if (max_supported_rate < 0.1)
536 /* Return microseconds */
537 return (int32_t)(1000000.0 / max_supported_rate);