2 * Copyright (C) 2014 Intel Corporation.
7 #include <cutils/properties.h>
8 #include <hardware/sensors.h>
9 #include "enumeration.h"
11 #define IIO_SENSOR_HAL_VERSION 1
16 * We acquire a number of parameters about sensors by reading properties.
17 * The idea here is that someone (either a script, or daemon, sets them
18 * depending on the set of sensors present on the machine.
20 * There are fallback paths in case the properties are not defined, but it is
21 * highly desirable to at least have the following for each sensor:
23 * ro.iio.anglvel.name = Gyroscope
24 * ro.iio.anglvel.vendor = Intel
25 * ro.iio.anglvel.max_range = 35
26 * ro.iio.anglvel.resolution = 0.002
27 * ro.iio.anglvel.power = 6.1
29 * Besides these, we have a couple of knobs initially used to cope with Intel
30 * Sensor Hub oddities, such as HID inspired units or firmware bugs:
32 * ro.iio.anglvel.transform = ISH
33 * ro.iio.anglvel.quirks = init-rate
35 * This one is used specifically to pass a calibration scale to ALS drivers:
37 * ro.iio.illuminance.name = CPLM3218x Ambient Light Sensor
38 * ro.iio.illuminance.vendor = Capella Microsystems
39 * ro.iio.illuminance.max_range = 167000
40 * ro.iio.illuminance.resolution = 1
41 * ro.iio.illuminance.power = .001
42 * ro.iio.illuminance.illumincalib = 7400
44 * Finally there's a 'opt_scale' specifier, documented as follows:
46 * This adds support for a scaling factor that can be expressed
47 * using properties, for all sensors, on a channel basis. That
48 * scaling factor is applied after all other transforms have been
49 * applied, and is intended as a way to compensate for problems
50 * such as an incorrect axis polarity for a given sensor.
52 * The syntax is <usual property prefix>.<channel>.opt_scale, e.g.
53 * ro.iio.accel.y.opt_scale = -1 to negate the sign of the y readings
54 * for the accelerometer.
56 * For sensors using a single channel - and only those - the channel
57 * name is implicitly void and a syntax such as ro.iio.illuminance.
58 * opt_scale = 3 has to be used.
61 static int sensor_get_st_prop (int s, const char* sel, char val[MAX_NAME_SIZE])
63 char prop_name[PROP_NAME_MAX];
64 char prop_val[PROP_VALUE_MAX];
65 int i = sensor_info[s].catalog_index;
66 const char *prefix = sensor_catalog[i].tag;
68 sprintf(prop_name, PROP_BASE, prefix, sel);
70 if (property_get(prop_name, prop_val, "")) {
71 strncpy(val, prop_val, MAX_NAME_SIZE-1);
72 val[MAX_NAME_SIZE-1] = '\0';
80 int sensor_get_fl_prop (int s, const char* sel, float* val)
82 char buf[MAX_NAME_SIZE];
84 if (sensor_get_st_prop(s, sel, buf))
87 *val = (float) strtod(buf, NULL);
92 char* sensor_get_name (int s)
94 if (sensor_info[s].friendly_name[0] != '\0' ||
95 !sensor_get_st_prop(s, "name", sensor_info[s].friendly_name))
96 return sensor_info[s].friendly_name;
98 /* If we got a iio device name from sysfs, use it */
99 if (sensor_info[s].internal_name[0]) {
100 snprintf(sensor_info[s].friendly_name, MAX_NAME_SIZE, "S%d-%s",
101 s, sensor_info[s].internal_name);
103 sprintf(sensor_info[s].friendly_name, "S%d", s);
106 return sensor_info[s].friendly_name;
110 char* sensor_get_vendor (int s)
112 if (sensor_info[s].vendor_name[0] ||
113 !sensor_get_st_prop(s, "vendor", sensor_info[s].vendor_name))
114 return sensor_info[s].vendor_name;
120 int sensor_get_version (int s)
122 return IIO_SENSOR_HAL_VERSION;
126 float sensor_get_max_range (int s)
131 if (sensor_info[s].max_range != 0.0 ||
132 !sensor_get_fl_prop(s, "max_range", &sensor_info[s].max_range))
133 return sensor_info[s].max_range;
135 /* Try returning a sensible value given the sensor type */
137 /* We should cap returned samples accordingly... */
139 catalog_index = sensor_info[s].catalog_index;
140 sensor_type = sensor_catalog[catalog_index].type;
142 switch (sensor_type) {
143 case SENSOR_TYPE_ACCELEROMETER: /* m/s^2 */
146 case SENSOR_TYPE_MAGNETIC_FIELD: /* micro-tesla */
149 case SENSOR_TYPE_ORIENTATION: /* degrees */
152 case SENSOR_TYPE_GYROSCOPE: /* radians/s */
155 case SENSOR_TYPE_LIGHT: /* SI lux units */
158 case SENSOR_TYPE_AMBIENT_TEMPERATURE: /* °C */
159 case SENSOR_TYPE_TEMPERATURE: /* °C */
160 case SENSOR_TYPE_PROXIMITY: /* centimeters */
161 case SENSOR_TYPE_PRESSURE: /* hecto-pascal */
162 case SENSOR_TYPE_RELATIVE_HUMIDITY: /* percent */
171 float sensor_get_resolution (int s)
173 if (sensor_info[s].resolution != 0.0 ||
174 !sensor_get_fl_prop(s, "resolution", &sensor_info[s].resolution))
175 return sensor_info[s].resolution;
181 float sensor_get_power (int s)
183 /* mA used while sensor is in use ; not sure about volts :) */
184 if (sensor_info[s].power != 0.0 ||
185 !sensor_get_fl_prop(s, "power", &sensor_info[s].power))
186 return sensor_info[s].power;
192 float sensor_get_illumincalib (int s)
194 /* calibrating the ALS Sensor*/
195 if (sensor_info[s].illumincalib != 0.0 ||
196 !sensor_get_fl_prop(s, "illumincalib", &sensor_info[s].illumincalib)) {
197 return sensor_info[s].illumincalib;
204 int sensor_get_order (int s, unsigned char map[MAX_CHANNELS])
206 char buf[MAX_NAME_SIZE];
208 int count = sensor_catalog[sensor_info[s].catalog_index].num_channels;
210 memset(map, 0, MAX_CHANNELS);
212 if (sensor_get_st_prop(s, "order", buf))
213 return 0; /* No order property */
215 /* Assume ASCII characters, in the '0'..'9' range */
217 for (i=0; i<count; i++)
218 map[i] = buf[i] - '0';
220 /* Check that our indices are in range */
221 for (i=0; i<count; i++)
222 if (map[i] >= count) {
223 ALOGE("Order index out of range for sensor %d\n", s);
227 return 1; /* OK to use modified ordering map */