OSDN Git Service

ccc387073fecfd57437205cf4dc91a14ef7b61b1
[android-x86/hardware-intel-libsensors.git] / description.c
1 /*
2  * Copyright (C) 2014 Intel Corporation.
3  */
4
5 #include <stdlib.h>
6 #include <utils/Log.h>
7 #include <cutils/properties.h>
8 #include <hardware/sensors.h>
9 #include "enumeration.h"
10 #include "description.h"
11
12 #define IIO_SENSOR_HAL_VERSION  1
13
14 /*
15  * About properties
16  *
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.
20  *
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:
23  *
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
29  *
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:
32  *
33  * ro.iio.anglvel.transform = ISH
34  * ro.iio.anglvel.quirks = init-rate
35  *
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.
39  *
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.
43  *
44  * This one is used specifically to pass a calibration scale to ALS drivers:
45  *
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
52  *
53  * Finally there's a 'opt_scale' specifier, documented as follows:
54  *
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.
60  *
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.
64  *
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.
68  */
69
70 static int sensor_get_st_prop (int s, const char* sel, char val[MAX_NAME_SIZE])
71 {
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;
76
77         sprintf(prop_name, PROP_BASE, prefix, sel);
78
79         if (property_get(prop_name, prop_val, "")) {
80                 strncpy(val, prop_val, MAX_NAME_SIZE-1);
81                 val[MAX_NAME_SIZE-1] = '\0';
82                 return 0;
83         }
84
85         return -1;
86 }
87
88
89 int sensor_get_fl_prop (int s, const char* sel, float* val)
90 {
91         char buf[MAX_NAME_SIZE];
92
93         if (sensor_get_st_prop(s, sel, buf))
94                 return -1;
95
96         *val = (float) strtod(buf, NULL);
97         return 0;
98 }
99
100
101 char* sensor_get_name (int s)
102 {
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;
106
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);
111         } else {
112                 sprintf(sensor_info[s].friendly_name, "S%d", s);
113         }
114
115         return sensor_info[s].friendly_name;
116 }
117
118
119 char* sensor_get_vendor (int s)
120 {
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;
124
125         return "";
126 }
127
128
129 int sensor_get_version (int s)
130 {
131         return IIO_SENSOR_HAL_VERSION;
132 }
133
134
135 float sensor_get_max_range (int s)
136 {
137         int catalog_index;
138         int sensor_type;
139
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;
143
144         /* Try returning a sensible value given the sensor type */
145
146         /* We should cap returned samples accordingly... */
147
148         catalog_index = sensor_info[s].catalog_index;
149         sensor_type = sensor_catalog[catalog_index].type;
150
151         switch (sensor_type) {
152                 case SENSOR_TYPE_ACCELEROMETER:         /* m/s^2        */
153                         return 50;
154
155                 case SENSOR_TYPE_MAGNETIC_FIELD:        /* micro-tesla  */
156                         return 500;
157
158                 case SENSOR_TYPE_ORIENTATION:           /* degrees      */
159                         return 360;
160
161                 case SENSOR_TYPE_GYROSCOPE:             /* radians/s    */
162                         return 10;
163
164                 case SENSOR_TYPE_LIGHT:                 /* SI lux units */
165                         return 50000;
166
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 */
172                         return 100;
173
174                 default:
175                         return 0.0;
176                 }
177 }
178
179
180 float sensor_get_resolution (int s)
181 {
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;
185
186         return 0;
187 }
188
189
190 float sensor_get_power (int s)
191 {
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;
196
197         return 0;
198 }
199
200
201 float sensor_get_illumincalib (int s)
202 {
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;
207         }
208
209         return 0;
210 }
211
212
213 uint32_t sensor_get_quirks (int s)
214 {
215         char quirks_buf[MAX_NAME_SIZE];
216
217         /* Read and decode quirks property on first reference */
218         if (!(sensor_info[s].quirks & QUIRKS_ALREADY_DECODED)) {
219                 quirks_buf[0] = '\0';
220                 sensor_get_st_prop(s, "quirks", quirks_buf);
221
222                 if (strstr(quirks_buf, "init-rate"))
223                         sensor_info[s].quirks |= QUIRKS_INITIAL_RATE;
224
225                 if (strstr(quirks_buf, "terse"))
226                         sensor_info[s].quirks |= QUIRK_TERSE_DRIVER;
227
228                 if (strstr(quirks_buf, "noisy"))
229                         sensor_info[s].quirks |= QUIRK_NOISY;
230
231                 sensor_info[s].quirks |= QUIRKS_ALREADY_DECODED;
232         }
233
234         return sensor_info[s].quirks;
235 }
236
237
238 int sensor_get_order (int s, unsigned char map[MAX_CHANNELS])
239 {
240         char buf[MAX_NAME_SIZE];
241         int i;
242         int count = sensor_catalog[sensor_info[s].catalog_index].num_channels;
243
244         memset(map, 0, MAX_CHANNELS);
245
246         if  (sensor_get_st_prop(s, "order", buf))
247                 return 0; /* No order property */
248
249         /* Assume ASCII characters, in the '0'..'9' range */
250
251         for (i=0; i<count; i++)
252                 map[i] = buf[i] - '0';
253
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);
258                         return 0;
259                 }
260
261         return 1;       /* OK to use modified ordering map */
262 }