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, "terse"))
266 sensor_info[s].quirks |= QUIRK_TERSE_DRIVER;
268 if (strstr(quirks_buf, "noisy"))
269 sensor_info[s].quirks |= QUIRK_NOISY;
271 sensor_info[s].quirks |= QUIRK_ALREADY_DECODED;
274 return sensor_info[s].quirks;
278 int sensor_get_order (int s, unsigned char map[MAX_CHANNELS])
280 char buf[MAX_NAME_SIZE];
282 int count = sensor_catalog[sensor_info[s].catalog_index].num_channels;
284 if (sensor_get_st_prop(s, "order", buf))
285 return 0; /* No order property */
287 /* Assume ASCII characters, in the '0'..'9' range */
289 for (i=0; i<count; i++)
290 if (buf[i] - '0' >= count) {
291 ALOGE("Order index out of range for sensor %d\n", s);
295 for (i=0; i<count; i++)
296 map[i] = buf[i] - '0';
298 return 1; /* OK to use modified ordering map */
301 char* sensor_get_string_type(int s)
306 catalog_index = sensor_info[s].catalog_index;
307 sensor_type = sensor_catalog[catalog_index].type;
309 switch (sensor_type) {
310 case SENSOR_TYPE_ACCELEROMETER:
311 return SENSOR_STRING_TYPE_ACCELEROMETER;
313 case SENSOR_TYPE_MAGNETIC_FIELD:
314 return SENSOR_STRING_TYPE_MAGNETIC_FIELD;
316 case SENSOR_TYPE_ORIENTATION:
317 return SENSOR_STRING_TYPE_ORIENTATION;
319 case SENSOR_TYPE_GYROSCOPE:
320 return SENSOR_STRING_TYPE_GYROSCOPE;
322 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
323 return SENSOR_STRING_TYPE_GYROSCOPE_UNCALIBRATED;
325 case SENSOR_TYPE_LIGHT:
326 return SENSOR_STRING_TYPE_LIGHT;
328 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
329 return SENSOR_STRING_TYPE_AMBIENT_TEMPERATURE;
331 case SENSOR_TYPE_TEMPERATURE:
332 return SENSOR_STRING_TYPE_TEMPERATURE;
334 case SENSOR_TYPE_PROXIMITY:
335 return SENSOR_STRING_TYPE_PROXIMITY;
337 case SENSOR_TYPE_PRESSURE:
338 return SENSOR_STRING_TYPE_PRESSURE;
340 case SENSOR_TYPE_RELATIVE_HUMIDITY:
341 return SENSOR_STRING_TYPE_RELATIVE_HUMIDITY;
348 flag_t sensor_get_flags (int s)
354 catalog_index = sensor_info[s].catalog_index;
355 sensor_type = sensor_catalog[catalog_index].type;
357 switch (sensor_type) {
358 case SENSOR_TYPE_ACCELEROMETER:
359 case SENSOR_TYPE_MAGNETIC_FIELD:
360 case SENSOR_TYPE_ORIENTATION:
361 case SENSOR_TYPE_GYROSCOPE:
362 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
363 case SENSOR_TYPE_PRESSURE:
364 flags |= SENSOR_FLAG_CONTINUOUS_MODE;
367 case SENSOR_TYPE_LIGHT:
368 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
369 case SENSOR_TYPE_TEMPERATURE:
370 case SENSOR_TYPE_RELATIVE_HUMIDITY:
371 flags |= SENSOR_FLAG_ON_CHANGE_MODE;
375 case SENSOR_TYPE_PROXIMITY:
376 flags |= SENSOR_FLAG_WAKE_UP;
377 flags |= SENSOR_FLAG_ON_CHANGE_MODE;
381 ALOGI("Unknown sensor");
386 max_delay_t sensor_get_max_delay (int s)
388 char avail_sysfs_path[PATH_MAX];
389 int dev_num = sensor_info[s].dev_num;
392 float min_supported_rate = 1000;
395 /* continuous: maximum sampling period allowed in microseconds.
396 * on-change, one-shot, special : 0
399 if (sensor_desc[s].flags)
402 sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
404 if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) < 0)
408 while (*cursor && cursor[0]) {
410 /* Decode a single value */
411 sr = strtod(cursor, NULL);
413 if (sr < min_supported_rate)
414 min_supported_rate = sr;
417 while (cursor[0] && !isspace(cursor[0]))
421 while (cursor[0] && isspace(cursor[0]))
425 /* return 0 for wrong values */
426 if (min_supported_rate < 0.1)
429 /* Return microseconds */
430 return (max_delay_t)(1000000.0 / min_supported_rate);
433 /* this value depends on the reporting mode:
435 * continuous: minimum sample period allowed in microseconds
438 * special : 0, unless otherwise noted
440 int32_t sensor_get_min_delay(int s)
442 char avail_sysfs_path[PATH_MAX];
443 int dev_num = sensor_info[s].dev_num;
446 float max_supported_rate = 0;
448 int catalog_index = sensor_info[s].catalog_index;
449 int sensor_type = sensor_catalog[catalog_index].type;
452 sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
454 if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) < 0) {
455 /* If poll mode sensor */
456 if (!sensor_info[s].num_channels) {
457 switch (sensor_type) {
458 case SENSOR_TYPE_ACCELEROMETER:
459 max_supported_rate = 125; /* 125 Hz */
461 case SENSOR_TYPE_GYROSCOPE:
462 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
463 max_supported_rate = 200; /* 200 Hz */
465 case SENSOR_TYPE_MAGNETIC_FIELD:
466 max_supported_rate = 10; /* 10 Hz */
469 max_supported_rate = 0;
474 while (*cursor && cursor[0]) {
476 /* Decode a single value */
477 sr = strtod(cursor, NULL);
479 if (sr > max_supported_rate && sr <= MAX_EVENTS)
480 max_supported_rate = sr;
483 while (cursor[0] && !isspace(cursor[0]))
487 while (cursor[0] && isspace(cursor[0]))
492 return (int32_t)(1000000.0 / max_supported_rate);