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.
75 static int sensor_get_st_prop (int s, const char* sel, char val[MAX_NAME_SIZE])
77 char prop_name[PROP_NAME_MAX];
78 char prop_val[PROP_VALUE_MAX];
79 int i = sensor_info[s].catalog_index;
80 const char *prefix = sensor_catalog[i].tag;
82 sprintf(prop_name, PROP_BASE, prefix, sel);
84 if (property_get(prop_name, prop_val, "")) {
85 strncpy(val, prop_val, MAX_NAME_SIZE-1);
86 val[MAX_NAME_SIZE-1] = '\0';
94 int sensor_get_prop (int s, const char* sel, int* val)
96 char buf[MAX_NAME_SIZE];
98 if (sensor_get_st_prop(s, sel, buf))
106 int sensor_get_fl_prop (int s, const char* sel, float* val)
108 char buf[MAX_NAME_SIZE];
110 if (sensor_get_st_prop(s, sel, buf))
113 *val = (float) strtod(buf, NULL);
118 char* sensor_get_name (int s)
120 if (sensor_info[s].friendly_name[0] != '\0' ||
121 !sensor_get_st_prop(s, "name", sensor_info[s].friendly_name))
122 return sensor_info[s].friendly_name;
124 /* If we got a iio device name from sysfs, use it */
125 if (sensor_info[s].internal_name[0]) {
126 snprintf(sensor_info[s].friendly_name, MAX_NAME_SIZE, "S%d-%s",
127 s, sensor_info[s].internal_name);
129 sprintf(sensor_info[s].friendly_name, "S%d", s);
132 return sensor_info[s].friendly_name;
136 char* sensor_get_vendor (int s)
138 if (sensor_info[s].vendor_name[0] ||
139 !sensor_get_st_prop(s, "vendor", sensor_info[s].vendor_name))
140 return sensor_info[s].vendor_name;
146 int sensor_get_version (int s)
148 return IIO_SENSOR_HAL_VERSION;
152 float sensor_get_max_range (int s)
157 if (sensor_info[s].max_range != 0.0 ||
158 !sensor_get_fl_prop(s, "max_range", &sensor_info[s].max_range))
159 return sensor_info[s].max_range;
161 /* Try returning a sensible value given the sensor type */
163 /* We should cap returned samples accordingly... */
165 catalog_index = sensor_info[s].catalog_index;
166 sensor_type = sensor_catalog[catalog_index].type;
168 switch (sensor_type) {
169 case SENSOR_TYPE_ACCELEROMETER: /* m/s^2 */
172 case SENSOR_TYPE_MAGNETIC_FIELD: /* micro-tesla */
175 case SENSOR_TYPE_ORIENTATION: /* degrees */
178 case SENSOR_TYPE_GYROSCOPE: /* radians/s */
181 case SENSOR_TYPE_LIGHT: /* SI lux units */
184 case SENSOR_TYPE_AMBIENT_TEMPERATURE: /* °C */
185 case SENSOR_TYPE_TEMPERATURE: /* °C */
186 case SENSOR_TYPE_PROXIMITY: /* centimeters */
187 case SENSOR_TYPE_PRESSURE: /* hecto-pascal */
188 case SENSOR_TYPE_RELATIVE_HUMIDITY: /* percent */
197 float sensor_get_resolution (int s)
199 if (sensor_info[s].resolution != 0.0 ||
200 !sensor_get_fl_prop(s, "resolution", &sensor_info[s].resolution))
201 return sensor_info[s].resolution;
207 float sensor_get_power (int s)
209 /* mA used while sensor is in use ; not sure about volts :) */
210 if (sensor_info[s].power != 0.0 ||
211 !sensor_get_fl_prop(s, "power", &sensor_info[s].power))
212 return sensor_info[s].power;
218 float sensor_get_illumincalib (int s)
220 /* calibrating the ALS Sensor*/
221 if (sensor_info[s].illumincalib != 0.0 ||
222 !sensor_get_fl_prop(s, "illumincalib", &sensor_info[s].illumincalib)) {
223 return sensor_info[s].illumincalib;
230 uint32_t sensor_get_quirks (int s)
232 char quirks_buf[MAX_NAME_SIZE];
234 /* Read and decode quirks property on first reference */
235 if (!(sensor_info[s].quirks & QUIRK_ALREADY_DECODED)) {
236 quirks_buf[0] = '\0';
237 sensor_get_st_prop(s, "quirks", quirks_buf);
239 if (strstr(quirks_buf, "init-rate"))
240 sensor_info[s].quirks |= QUIRK_INITIAL_RATE;
242 if (strstr(quirks_buf, "terse"))
243 sensor_info[s].quirks |= QUIRK_TERSE_DRIVER;
245 if (strstr(quirks_buf, "noisy"))
246 sensor_info[s].quirks |= QUIRK_NOISY;
248 sensor_info[s].quirks |= QUIRK_ALREADY_DECODED;
251 return sensor_info[s].quirks;
255 int sensor_get_order (int s, unsigned char map[MAX_CHANNELS])
257 char buf[MAX_NAME_SIZE];
259 int count = sensor_catalog[sensor_info[s].catalog_index].num_channels;
261 if (sensor_get_st_prop(s, "order", buf))
262 return 0; /* No order property */
264 /* Assume ASCII characters, in the '0'..'9' range */
266 for (i=0; i<count; i++)
267 if (buf[i] - '0' >= count) {
268 ALOGE("Order index out of range for sensor %d\n", s);
272 for (i=0; i<count; i++)
273 map[i] = buf[i] - '0';
275 return 1; /* OK to use modified ordering map */
278 char* sensor_get_string_type(int s)
283 catalog_index = sensor_info[s].catalog_index;
284 sensor_type = sensor_catalog[catalog_index].type;
286 switch (sensor_type) {
287 case SENSOR_TYPE_ACCELEROMETER:
288 return SENSOR_STRING_TYPE_ACCELEROMETER;
290 case SENSOR_TYPE_MAGNETIC_FIELD:
291 return SENSOR_STRING_TYPE_MAGNETIC_FIELD;
293 case SENSOR_TYPE_ORIENTATION:
294 return SENSOR_STRING_TYPE_ORIENTATION;
296 case SENSOR_TYPE_GYROSCOPE:
297 return SENSOR_STRING_TYPE_GYROSCOPE;
299 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
300 return SENSOR_STRING_TYPE_GYROSCOPE_UNCALIBRATED;
302 case SENSOR_TYPE_LIGHT:
303 return SENSOR_STRING_TYPE_LIGHT;
305 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
306 return SENSOR_STRING_TYPE_AMBIENT_TEMPERATURE;
308 case SENSOR_TYPE_TEMPERATURE:
309 return SENSOR_STRING_TYPE_TEMPERATURE;
311 case SENSOR_TYPE_PROXIMITY:
312 return SENSOR_STRING_TYPE_PROXIMITY;
314 case SENSOR_TYPE_PRESSURE:
315 return SENSOR_STRING_TYPE_PRESSURE;
317 case SENSOR_TYPE_RELATIVE_HUMIDITY:
318 return SENSOR_STRING_TYPE_RELATIVE_HUMIDITY;
325 flag_t sensor_get_flags (int s)
331 catalog_index = sensor_info[s].catalog_index;
332 sensor_type = sensor_catalog[catalog_index].type;
334 switch (sensor_type) {
335 case SENSOR_TYPE_ACCELEROMETER:
336 case SENSOR_TYPE_MAGNETIC_FIELD:
337 case SENSOR_TYPE_ORIENTATION:
338 case SENSOR_TYPE_GYROSCOPE:
339 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
340 case SENSOR_TYPE_PRESSURE:
341 flags |= SENSOR_FLAG_CONTINUOUS_MODE;
344 case SENSOR_TYPE_LIGHT:
345 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
346 case SENSOR_TYPE_TEMPERATURE:
347 case SENSOR_TYPE_RELATIVE_HUMIDITY:
348 flags |= SENSOR_FLAG_ON_CHANGE_MODE;
352 case SENSOR_TYPE_PROXIMITY:
353 flags |= SENSOR_FLAG_WAKE_UP;
354 flags |= SENSOR_FLAG_ON_CHANGE_MODE;
358 ALOGI("Unknown sensor");
363 max_delay_t sensor_get_max_delay (int s)
365 char avail_sysfs_path[PATH_MAX];
366 int dev_num = sensor_info[s].dev_num;
369 float min_supported_rate = 1000;
372 /* continuous: maximum sampling period allowed in microseconds.
373 * on-change, one-shot, special : 0
376 if (sensor_desc[s].flags)
379 sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
381 if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) < 0)
385 while (*cursor && cursor[0]) {
387 /* Decode a single value */
388 sr = strtod(cursor, NULL);
390 if (sr < min_supported_rate)
391 min_supported_rate = sr;
394 while (cursor[0] && !isspace(cursor[0]))
398 while (cursor[0] && isspace(cursor[0]))
402 /* return 0 for wrong values */
403 if (min_supported_rate < 0.1)
406 /* Return microseconds */
407 return (max_delay_t)(1000000.0 / min_supported_rate);
410 /* this value depends on the reporting mode:
412 * continuous: minimum sample period allowed in microseconds
415 * special : 0, unless otherwise noted
417 int32_t sensor_get_min_delay(int s)
419 char avail_sysfs_path[PATH_MAX];
420 int dev_num = sensor_info[s].dev_num;
423 float max_supported_rate = 0;
425 int catalog_index = sensor_info[s].catalog_index;
426 int sensor_type = sensor_catalog[catalog_index].type;
429 sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
431 if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) < 0) {
432 /* If poll mode sensor */
433 if (!sensor_info[s].num_channels) {
434 switch (sensor_type) {
435 case SENSOR_TYPE_ACCELEROMETER:
436 max_supported_rate = 125; /* 125 Hz */
438 case SENSOR_TYPE_GYROSCOPE:
439 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
440 max_supported_rate = 200; /* 200 Hz */
442 case SENSOR_TYPE_MAGNETIC_FIELD:
443 max_supported_rate = 10; /* 10 Hz */
446 max_supported_rate = 0;
451 while (*cursor && cursor[0]) {
453 /* Decode a single value */
454 sr = strtod(cursor, NULL);
456 if (sr > max_supported_rate && sr <= MAX_EVENTS)
457 max_supported_rate = sr;
460 while (cursor[0] && !isspace(cursor[0]))
464 while (cursor[0] && isspace(cursor[0]))
469 return (int32_t)(1000000.0 / max_supported_rate);