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 * Finally 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.
71 static int sensor_get_st_prop (int s, const char* sel, char val[MAX_NAME_SIZE])
73 char prop_name[PROP_NAME_MAX];
74 char prop_val[PROP_VALUE_MAX];
75 int i = sensor_info[s].catalog_index;
76 const char *prefix = sensor_catalog[i].tag;
78 sprintf(prop_name, PROP_BASE, prefix, sel);
80 if (property_get(prop_name, prop_val, "")) {
81 strncpy(val, prop_val, MAX_NAME_SIZE-1);
82 val[MAX_NAME_SIZE-1] = '\0';
90 int sensor_get_fl_prop (int s, const char* sel, float* val)
92 char buf[MAX_NAME_SIZE];
94 if (sensor_get_st_prop(s, sel, buf))
97 *val = (float) strtod(buf, NULL);
102 char* sensor_get_name (int s)
104 if (sensor_info[s].friendly_name[0] != '\0' ||
105 !sensor_get_st_prop(s, "name", sensor_info[s].friendly_name))
106 return sensor_info[s].friendly_name;
108 /* If we got a iio device name from sysfs, use it */
109 if (sensor_info[s].internal_name[0]) {
110 snprintf(sensor_info[s].friendly_name, MAX_NAME_SIZE, "S%d-%s",
111 s, sensor_info[s].internal_name);
113 sprintf(sensor_info[s].friendly_name, "S%d", s);
116 return sensor_info[s].friendly_name;
120 char* sensor_get_vendor (int s)
122 if (sensor_info[s].vendor_name[0] ||
123 !sensor_get_st_prop(s, "vendor", sensor_info[s].vendor_name))
124 return sensor_info[s].vendor_name;
130 int sensor_get_version (int s)
132 return IIO_SENSOR_HAL_VERSION;
136 float sensor_get_max_range (int s)
141 if (sensor_info[s].max_range != 0.0 ||
142 !sensor_get_fl_prop(s, "max_range", &sensor_info[s].max_range))
143 return sensor_info[s].max_range;
145 /* Try returning a sensible value given the sensor type */
147 /* We should cap returned samples accordingly... */
149 catalog_index = sensor_info[s].catalog_index;
150 sensor_type = sensor_catalog[catalog_index].type;
152 switch (sensor_type) {
153 case SENSOR_TYPE_ACCELEROMETER: /* m/s^2 */
156 case SENSOR_TYPE_MAGNETIC_FIELD: /* micro-tesla */
159 case SENSOR_TYPE_ORIENTATION: /* degrees */
162 case SENSOR_TYPE_GYROSCOPE: /* radians/s */
165 case SENSOR_TYPE_LIGHT: /* SI lux units */
168 case SENSOR_TYPE_AMBIENT_TEMPERATURE: /* °C */
169 case SENSOR_TYPE_TEMPERATURE: /* °C */
170 case SENSOR_TYPE_PROXIMITY: /* centimeters */
171 case SENSOR_TYPE_PRESSURE: /* hecto-pascal */
172 case SENSOR_TYPE_RELATIVE_HUMIDITY: /* percent */
181 float sensor_get_resolution (int s)
183 if (sensor_info[s].resolution != 0.0 ||
184 !sensor_get_fl_prop(s, "resolution", &sensor_info[s].resolution))
185 return sensor_info[s].resolution;
191 float sensor_get_power (int s)
193 /* mA used while sensor is in use ; not sure about volts :) */
194 if (sensor_info[s].power != 0.0 ||
195 !sensor_get_fl_prop(s, "power", &sensor_info[s].power))
196 return sensor_info[s].power;
202 float sensor_get_illumincalib (int s)
204 /* calibrating the ALS Sensor*/
205 if (sensor_info[s].illumincalib != 0.0 ||
206 !sensor_get_fl_prop(s, "illumincalib", &sensor_info[s].illumincalib)) {
207 return sensor_info[s].illumincalib;
214 uint32_t sensor_get_quirks (int s)
216 char quirks_buf[MAX_NAME_SIZE];
218 /* Read and decode quirks property on first reference */
219 if (!(sensor_info[s].quirks & QUIRK_ALREADY_DECODED)) {
220 quirks_buf[0] = '\0';
221 sensor_get_st_prop(s, "quirks", quirks_buf);
223 if (strstr(quirks_buf, "init-rate"))
224 sensor_info[s].quirks |= QUIRK_INITIAL_RATE;
226 if (strstr(quirks_buf, "terse"))
227 sensor_info[s].quirks |= QUIRK_TERSE_DRIVER;
229 if (strstr(quirks_buf, "noisy"))
230 sensor_info[s].quirks |= QUIRK_NOISY;
232 sensor_info[s].quirks |= QUIRK_ALREADY_DECODED;
235 return sensor_info[s].quirks;
239 int sensor_get_order (int s, unsigned char map[MAX_CHANNELS])
241 char buf[MAX_NAME_SIZE];
243 int count = sensor_catalog[sensor_info[s].catalog_index].num_channels;
245 if (sensor_get_st_prop(s, "order", buf))
246 return 0; /* No order property */
248 /* Assume ASCII characters, in the '0'..'9' range */
250 for (i=0; i<count; i++)
251 if (buf[i] - '0' >= count) {
252 ALOGE("Order index out of range for sensor %d\n", s);
256 for (i=0; i<count; i++)
257 map[i] = buf[i] - '0';
259 return 1; /* OK to use modified ordering map */
262 char* sensor_get_string_type(int s)
267 catalog_index = sensor_info[s].catalog_index;
268 sensor_type = sensor_catalog[catalog_index].type;
270 switch (sensor_type) {
271 case SENSOR_TYPE_ACCELEROMETER:
272 return SENSOR_STRING_TYPE_ACCELEROMETER;
274 case SENSOR_TYPE_MAGNETIC_FIELD:
275 return SENSOR_STRING_TYPE_MAGNETIC_FIELD;
277 case SENSOR_TYPE_ORIENTATION:
278 return SENSOR_STRING_TYPE_ORIENTATION;
280 case SENSOR_TYPE_GYROSCOPE:
281 return SENSOR_STRING_TYPE_GYROSCOPE;
283 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
284 return SENSOR_STRING_TYPE_GYROSCOPE_UNCALIBRATED;
286 case SENSOR_TYPE_LIGHT:
287 return SENSOR_STRING_TYPE_LIGHT;
289 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
290 return SENSOR_STRING_TYPE_AMBIENT_TEMPERATURE;
292 case SENSOR_TYPE_TEMPERATURE:
293 return SENSOR_STRING_TYPE_TEMPERATURE;
295 case SENSOR_TYPE_PROXIMITY:
296 return SENSOR_STRING_TYPE_PROXIMITY;
298 case SENSOR_TYPE_PRESSURE:
299 return SENSOR_STRING_TYPE_PRESSURE;
301 case SENSOR_TYPE_RELATIVE_HUMIDITY:
302 return SENSOR_STRING_TYPE_RELATIVE_HUMIDITY;
309 flag_t sensor_get_flags (int s)
315 catalog_index = sensor_info[s].catalog_index;
316 sensor_type = sensor_catalog[catalog_index].type;
318 switch (sensor_type) {
319 case SENSOR_TYPE_ACCELEROMETER:
320 case SENSOR_TYPE_MAGNETIC_FIELD:
321 case SENSOR_TYPE_ORIENTATION:
322 case SENSOR_TYPE_GYROSCOPE:
323 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
324 case SENSOR_TYPE_PRESSURE:
325 flags |= SENSOR_FLAG_CONTINUOUS_MODE;
328 case SENSOR_TYPE_LIGHT:
329 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
330 case SENSOR_TYPE_TEMPERATURE:
331 case SENSOR_TYPE_RELATIVE_HUMIDITY:
332 flags |= SENSOR_FLAG_ON_CHANGE_MODE;
336 case SENSOR_TYPE_PROXIMITY:
337 flags |= SENSOR_FLAG_WAKE_UP;
338 flags |= SENSOR_FLAG_ON_CHANGE_MODE;
342 ALOGI("Unknown sensor");
347 max_delay_t sensor_get_max_delay (int s)
349 char avail_sysfs_path[PATH_MAX];
350 int dev_num = sensor_info[s].dev_num;
353 float min_supported_rate = 1000;
356 /* continuous: maximum sampling period allowed in microseconds.
357 * on-change, one-shot, special : 0
360 if (sensor_desc[s].flags)
363 sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
365 if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) < 0)
369 while (*cursor && cursor[0]) {
371 /* Decode a single value */
372 sr = strtod(cursor, NULL);
374 if (sr < min_supported_rate)
375 min_supported_rate = sr;
378 while (cursor[0] && !isspace(cursor[0]))
382 while (cursor[0] && isspace(cursor[0]))
386 /* return 0 for wrong values */
387 if (min_supported_rate < 0.1)
390 /* Return microseconds */
391 return (max_delay_t)(1000000.0 / min_supported_rate);