2 * Copyright (C) 2014 Intel Corporation.
7 #include <cutils/properties.h>
8 #include <hardware/sensors.h>
9 #include "enumeration.h"
10 #include "description.h"
12 #define IIO_SENSOR_HAL_VERSION 1
17 * We acquire a number of parameters about sensors by reading properties.
18 * The idea here is that someone (either a script, or daemon, sets them
19 * depending on the set of sensors present on the machine.
21 * There are fallback paths in case the properties are not defined, but it is
22 * highly desirable to at least have the following for each sensor:
24 * ro.iio.anglvel.name = Gyroscope
25 * ro.iio.anglvel.vendor = Intel
26 * ro.iio.anglvel.max_range = 35
27 * ro.iio.anglvel.resolution = 0.002
28 * ro.iio.anglvel.power = 6.1
30 * Besides these, we have a couple of knobs initially used to cope with Intel
31 * Sensor Hub oddities, such as HID inspired units or firmware bugs:
33 * ro.iio.anglvel.transform = ISH
34 * ro.iio.anglvel.quirks = init-rate
36 * The "terse" quirk indicates that the underlying driver only sends events
37 * when the sensor reports a change. The HAL then periodically generates
38 * duplicate events so the sensor behaves as a continously firing one.
40 * The "noisy" quirk indicates that the underlying driver has a unusually high
41 * level of noise in its readings, and that the HAL has to accomodate it
42 * somehow, e.g. in the magnetometer calibration code path.
44 * This one is used specifically to pass a calibration scale to ALS drivers:
46 * ro.iio.illuminance.name = CPLM3218x Ambient Light Sensor
47 * ro.iio.illuminance.vendor = Capella Microsystems
48 * ro.iio.illuminance.max_range = 167000
49 * ro.iio.illuminance.resolution = 1
50 * ro.iio.illuminance.power = .001
51 * ro.iio.illuminance.illumincalib = 7400
53 * Finally there's a 'opt_scale' specifier, documented as follows:
55 * This adds support for a scaling factor that can be expressed
56 * using properties, for all sensors, on a channel basis. That
57 * scaling factor is applied after all other transforms have been
58 * applied, and is intended as a way to compensate for problems
59 * such as an incorrect axis polarity for a given sensor.
61 * The syntax is <usual property prefix>.<channel>.opt_scale, e.g.
62 * ro.iio.accel.y.opt_scale = -1 to negate the sign of the y readings
63 * for the accelerometer.
65 * For sensors using a single channel - and only those - the channel
66 * name is implicitly void and a syntax such as ro.iio.illuminance.
67 * opt_scale = 3 has to be used.
70 static int sensor_get_st_prop (int s, const char* sel, char val[MAX_NAME_SIZE])
72 char prop_name[PROP_NAME_MAX];
73 char prop_val[PROP_VALUE_MAX];
74 int i = sensor_info[s].catalog_index;
75 const char *prefix = sensor_catalog[i].tag;
77 sprintf(prop_name, PROP_BASE, prefix, sel);
79 if (property_get(prop_name, prop_val, "")) {
80 strncpy(val, prop_val, MAX_NAME_SIZE-1);
81 val[MAX_NAME_SIZE-1] = '\0';
89 int sensor_get_fl_prop (int s, const char* sel, float* val)
91 char buf[MAX_NAME_SIZE];
93 if (sensor_get_st_prop(s, sel, buf))
96 *val = (float) strtod(buf, NULL);
101 char* sensor_get_name (int s)
103 if (sensor_info[s].friendly_name[0] != '\0' ||
104 !sensor_get_st_prop(s, "name", sensor_info[s].friendly_name))
105 return sensor_info[s].friendly_name;
107 /* If we got a iio device name from sysfs, use it */
108 if (sensor_info[s].internal_name[0]) {
109 snprintf(sensor_info[s].friendly_name, MAX_NAME_SIZE, "S%d-%s",
110 s, sensor_info[s].internal_name);
112 sprintf(sensor_info[s].friendly_name, "S%d", s);
115 return sensor_info[s].friendly_name;
119 char* sensor_get_vendor (int s)
121 if (sensor_info[s].vendor_name[0] ||
122 !sensor_get_st_prop(s, "vendor", sensor_info[s].vendor_name))
123 return sensor_info[s].vendor_name;
129 int sensor_get_version (int s)
131 return IIO_SENSOR_HAL_VERSION;
135 float sensor_get_max_range (int s)
140 if (sensor_info[s].max_range != 0.0 ||
141 !sensor_get_fl_prop(s, "max_range", &sensor_info[s].max_range))
142 return sensor_info[s].max_range;
144 /* Try returning a sensible value given the sensor type */
146 /* We should cap returned samples accordingly... */
148 catalog_index = sensor_info[s].catalog_index;
149 sensor_type = sensor_catalog[catalog_index].type;
151 switch (sensor_type) {
152 case SENSOR_TYPE_ACCELEROMETER: /* m/s^2 */
155 case SENSOR_TYPE_MAGNETIC_FIELD: /* micro-tesla */
158 case SENSOR_TYPE_ORIENTATION: /* degrees */
161 case SENSOR_TYPE_GYROSCOPE: /* radians/s */
164 case SENSOR_TYPE_LIGHT: /* SI lux units */
167 case SENSOR_TYPE_AMBIENT_TEMPERATURE: /* °C */
168 case SENSOR_TYPE_TEMPERATURE: /* °C */
169 case SENSOR_TYPE_PROXIMITY: /* centimeters */
170 case SENSOR_TYPE_PRESSURE: /* hecto-pascal */
171 case SENSOR_TYPE_RELATIVE_HUMIDITY: /* percent */
180 float sensor_get_resolution (int s)
182 if (sensor_info[s].resolution != 0.0 ||
183 !sensor_get_fl_prop(s, "resolution", &sensor_info[s].resolution))
184 return sensor_info[s].resolution;
190 float sensor_get_power (int s)
192 /* mA used while sensor is in use ; not sure about volts :) */
193 if (sensor_info[s].power != 0.0 ||
194 !sensor_get_fl_prop(s, "power", &sensor_info[s].power))
195 return sensor_info[s].power;
201 float sensor_get_illumincalib (int s)
203 /* calibrating the ALS Sensor*/
204 if (sensor_info[s].illumincalib != 0.0 ||
205 !sensor_get_fl_prop(s, "illumincalib", &sensor_info[s].illumincalib)) {
206 return sensor_info[s].illumincalib;
213 uint32_t sensor_get_quirks (int s)
215 char quirks_buf[MAX_NAME_SIZE];
217 /* Read and decode quirks property on first reference */
218 if (!(sensor_info[s].quirks & QUIRK_ALREADY_DECODED)) {
219 quirks_buf[0] = '\0';
220 sensor_get_st_prop(s, "quirks", quirks_buf);
222 if (strstr(quirks_buf, "init-rate"))
223 sensor_info[s].quirks |= QUIRK_INITIAL_RATE;
225 if (strstr(quirks_buf, "terse"))
226 sensor_info[s].quirks |= QUIRK_TERSE_DRIVER;
228 if (strstr(quirks_buf, "noisy"))
229 sensor_info[s].quirks |= QUIRK_NOISY;
231 sensor_info[s].quirks |= QUIRK_ALREADY_DECODED;
234 return sensor_info[s].quirks;
238 int sensor_get_order (int s, unsigned char map[MAX_CHANNELS])
240 char buf[MAX_NAME_SIZE];
242 int count = sensor_catalog[sensor_info[s].catalog_index].num_channels;
244 memset(map, 0, MAX_CHANNELS);
246 if (sensor_get_st_prop(s, "order", buf))
247 return 0; /* No order property */
249 /* Assume ASCII characters, in the '0'..'9' range */
251 for (i=0; i<count; i++)
252 map[i] = buf[i] - '0';
254 /* Check that our indices are in range */
255 for (i=0; i<count; i++)
256 if (map[i] >= count) {
257 ALOGE("Order index out of range for sensor %d\n", s);
261 return 1; /* OK to use modified ordering map */
264 char* sensor_get_string_type(int s)
269 catalog_index = sensor_info[s].catalog_index;
270 sensor_type = sensor_catalog[catalog_index].type;
272 switch (sensor_type) {
273 case SENSOR_TYPE_ACCELEROMETER:
274 return SENSOR_STRING_TYPE_ACCELEROMETER;
276 case SENSOR_TYPE_MAGNETIC_FIELD:
277 return SENSOR_STRING_TYPE_MAGNETIC_FIELD;
279 case SENSOR_TYPE_ORIENTATION:
280 return SENSOR_STRING_TYPE_ORIENTATION;
282 case SENSOR_TYPE_GYROSCOPE:
283 return SENSOR_STRING_TYPE_GYROSCOPE;
285 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
286 return SENSOR_STRING_TYPE_GYROSCOPE_UNCALIBRATED;
288 case SENSOR_TYPE_LIGHT:
289 return SENSOR_STRING_TYPE_LIGHT;
291 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
292 return SENSOR_STRING_TYPE_AMBIENT_TEMPERATURE;
294 case SENSOR_TYPE_TEMPERATURE:
295 return SENSOR_STRING_TYPE_TEMPERATURE;
297 case SENSOR_TYPE_PROXIMITY:
298 return SENSOR_STRING_TYPE_PROXIMITY;
300 case SENSOR_TYPE_PRESSURE:
301 return SENSOR_STRING_TYPE_PRESSURE;
303 case SENSOR_TYPE_RELATIVE_HUMIDITY:
304 return SENSOR_STRING_TYPE_RELATIVE_HUMIDITY;
311 flag_t sensor_get_flags (int s)
317 catalog_index = sensor_info[s].catalog_index;
318 sensor_type = sensor_catalog[catalog_index].type;
320 switch (sensor_type) {
321 case SENSOR_TYPE_ACCELEROMETER:
322 case SENSOR_TYPE_MAGNETIC_FIELD:
323 case SENSOR_TYPE_ORIENTATION:
324 case SENSOR_TYPE_GYROSCOPE:
325 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
326 case SENSOR_TYPE_PRESSURE:
327 flags |= SENSOR_FLAG_CONTINUOUS_MODE;
330 case SENSOR_TYPE_LIGHT:
331 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
332 case SENSOR_TYPE_TEMPERATURE:
333 case SENSOR_TYPE_RELATIVE_HUMIDITY:
334 flags |= SENSOR_FLAG_ON_CHANGE_MODE;
338 case SENSOR_TYPE_PROXIMITY:
339 flags |= SENSOR_FLAG_WAKE_UP;
340 flags |= SENSOR_FLAG_ON_CHANGE_MODE;
344 ALOGI("Unknown sensor");