2 * Copyright (C) 2014 Intel Corporation.
7 #include <cutils/properties.h>
8 #include <hardware/sensors.h>
9 #include "enumeration.h"
12 * This information should be provided on a sensor basis through a configuration
13 * file, or we should build a catalog of known sensors.
16 #define IIO_SENSOR_HAL_VERSION 1
19 static int sensor_get_st_prop (int s, const char* sel, char val[MAX_NAME_SIZE])
21 char prop_name[PROP_NAME_MAX];
22 char prop_val[PROP_VALUE_MAX];
23 int i = sensor_info[s].catalog_index;
24 const char *prefix = sensor_catalog[i].tag;
26 sprintf(prop_name, PROP_BASE, prefix, sel);
28 if (property_get(prop_name, prop_val, "")) {
29 strncpy(val, prop_val, MAX_NAME_SIZE-1);
30 val[MAX_NAME_SIZE-1] = '\0';
38 static int sensor_get_fl_prop (int s, const char* sel, float* val)
40 char buf[MAX_NAME_SIZE];
42 if (sensor_get_st_prop(s, sel, buf))
45 *val = (float) strtod(buf, NULL);
50 char* sensor_get_name (int s)
52 if (sensor_info[s].friendly_name[0] != '\0' ||
53 !sensor_get_st_prop(s, "name", sensor_info[s].friendly_name))
54 return sensor_info[s].friendly_name;
56 /* If we got a iio device name from sysfs, use it */
57 if (sensor_info[s].internal_name[0]) {
58 snprintf(sensor_info[s].friendly_name, MAX_NAME_SIZE, "S%d-%s",
59 s, sensor_info[s].internal_name);
60 sensor_info[s].friendly_name[MAX_NAME_SIZE-1] = '\0';
62 sprintf(sensor_info[s].friendly_name, "S%d", s);
65 return sensor_info[s].friendly_name;
69 char* sensor_get_vendor (int s)
71 if (sensor_info[s].vendor_name[0] ||
72 !sensor_get_st_prop(s, "vendor", sensor_info[s].vendor_name))
73 return sensor_info[s].vendor_name;
79 int sensor_get_version (int s)
81 return IIO_SENSOR_HAL_VERSION;
85 float sensor_get_max_range (int s)
90 if (sensor_info[s].max_range != 0.0 ||
91 !sensor_get_fl_prop(s, "max_range", &sensor_info[s].max_range))
92 return sensor_info[s].max_range;
94 /* Try returning a sensible value given the sensor type */
96 /* We should cap returned samples accordingly... */
98 catalog_index = sensor_info[s].catalog_index;
99 sensor_type = sensor_catalog[catalog_index].type;
101 switch (sensor_type) {
102 case SENSOR_TYPE_ACCELEROMETER: /* m/s^2 */
105 case SENSOR_TYPE_MAGNETIC_FIELD: /* micro-tesla */
108 case SENSOR_TYPE_ORIENTATION: /* degrees */
111 case SENSOR_TYPE_GYROSCOPE: /* radians/s */
114 case SENSOR_TYPE_LIGHT: /* SI lux units */
117 case SENSOR_TYPE_AMBIENT_TEMPERATURE: /* °C */
118 case SENSOR_TYPE_TEMPERATURE: /* °C */
119 case SENSOR_TYPE_PROXIMITY: /* centimeters */
120 case SENSOR_TYPE_PRESSURE: /* hecto-pascal */
121 case SENSOR_TYPE_RELATIVE_HUMIDITY: /* percent */
130 float sensor_get_resolution (int s)
132 if (sensor_info[s].resolution != 0.0 ||
133 !sensor_get_fl_prop(s, "resolution", &sensor_info[s].resolution))
134 return sensor_info[s].resolution;
140 float sensor_get_power (int s)
142 /* mA used while sensor is in use ; not sure about volts :) */
143 if (sensor_info[s].power != 0.0 ||
144 !sensor_get_fl_prop(s, "power", &sensor_info[s].power))
145 return sensor_info[s].power;