OSDN Git Service

7334d5fcd8ea8fd09e90813b4163048c3355fba0
[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 "common.h"
10 #include "enumeration.h"
11 #include "description.h"
12
13 #define IIO_SENSOR_HAL_VERSION  1
14
15 /*
16  * About properties
17  *
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.
21  *
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:
24  *
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
30  *
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:
33  *
34  * ro.iio.anglvel.transform = ISH
35  * ro.iio.anglvel.quirks = init-rate
36  *
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.
40  *
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.
44  *
45  * This one is used specifically to pass a calibration scale to ALS drivers:
46  *
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
53  *
54  * Finally there's a 'opt_scale' specifier, documented as follows:
55  *
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.
61  *
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.
65  *
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.
69  */
70
71 static int sensor_get_st_prop (int s, const char* sel, char val[MAX_NAME_SIZE])
72 {
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;
77
78         sprintf(prop_name, PROP_BASE, prefix, sel);
79
80         if (property_get(prop_name, prop_val, "")) {
81                 strncpy(val, prop_val, MAX_NAME_SIZE-1);
82                 val[MAX_NAME_SIZE-1] = '\0';
83                 return 0;
84         }
85
86         return -1;
87 }
88
89
90 int sensor_get_fl_prop (int s, const char* sel, float* val)
91 {
92         char buf[MAX_NAME_SIZE];
93
94         if (sensor_get_st_prop(s, sel, buf))
95                 return -1;
96
97         *val = (float) strtod(buf, NULL);
98         return 0;
99 }
100
101
102 char* sensor_get_name (int s)
103 {
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;
107
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);
112         } else {
113                 sprintf(sensor_info[s].friendly_name, "S%d", s);
114         }
115
116         return sensor_info[s].friendly_name;
117 }
118
119
120 char* sensor_get_vendor (int s)
121 {
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;
125
126         return "";
127 }
128
129
130 int sensor_get_version (int s)
131 {
132         return IIO_SENSOR_HAL_VERSION;
133 }
134
135
136 float sensor_get_max_range (int s)
137 {
138         int catalog_index;
139         int sensor_type;
140
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;
144
145         /* Try returning a sensible value given the sensor type */
146
147         /* We should cap returned samples accordingly... */
148
149         catalog_index = sensor_info[s].catalog_index;
150         sensor_type = sensor_catalog[catalog_index].type;
151
152         switch (sensor_type) {
153                 case SENSOR_TYPE_ACCELEROMETER:         /* m/s^2        */
154                         return 50;
155
156                 case SENSOR_TYPE_MAGNETIC_FIELD:        /* micro-tesla  */
157                         return 500;
158
159                 case SENSOR_TYPE_ORIENTATION:           /* degrees      */
160                         return 360;
161
162                 case SENSOR_TYPE_GYROSCOPE:             /* radians/s    */
163                         return 10;
164
165                 case SENSOR_TYPE_LIGHT:                 /* SI lux units */
166                         return 50000;
167
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 */
173                         return 100;
174
175                 default:
176                         return 0.0;
177                 }
178 }
179
180
181 float sensor_get_resolution (int s)
182 {
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;
186
187         return 0;
188 }
189
190
191 float sensor_get_power (int s)
192 {
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;
197
198         return 0;
199 }
200
201
202 float sensor_get_illumincalib (int s)
203 {
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;
208         }
209
210         return 0;
211 }
212
213
214 uint32_t sensor_get_quirks (int s)
215 {
216         char quirks_buf[MAX_NAME_SIZE];
217
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);
222
223                 if (strstr(quirks_buf, "init-rate"))
224                         sensor_info[s].quirks |= QUIRK_INITIAL_RATE;
225
226                 if (strstr(quirks_buf, "terse"))
227                         sensor_info[s].quirks |= QUIRK_TERSE_DRIVER;
228
229                 if (strstr(quirks_buf, "noisy"))
230                         sensor_info[s].quirks |= QUIRK_NOISY;
231
232                 sensor_info[s].quirks |= QUIRK_ALREADY_DECODED;
233         }
234
235         return sensor_info[s].quirks;
236 }
237
238
239 int sensor_get_order (int s, unsigned char map[MAX_CHANNELS])
240 {
241         char buf[MAX_NAME_SIZE];
242         int i;
243         int count = sensor_catalog[sensor_info[s].catalog_index].num_channels;
244
245         if  (sensor_get_st_prop(s, "order", buf))
246                 return 0; /* No order property */
247
248         /* Assume ASCII characters, in the '0'..'9' range */
249
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);
253                         return 0;
254                 }
255
256         for (i=0; i<count; i++)
257                 map[i] = buf[i] - '0';
258
259         return 1;       /* OK to use modified ordering map */
260 }
261
262 char* sensor_get_string_type(int s)
263 {
264         int catalog_index;
265         int sensor_type;
266
267         catalog_index = sensor_info[s].catalog_index;
268         sensor_type = sensor_catalog[catalog_index].type;
269
270         switch (sensor_type) {
271                 case SENSOR_TYPE_ACCELEROMETER:
272                         return SENSOR_STRING_TYPE_ACCELEROMETER;
273
274                 case SENSOR_TYPE_MAGNETIC_FIELD:
275                         return SENSOR_STRING_TYPE_MAGNETIC_FIELD;
276
277                 case SENSOR_TYPE_ORIENTATION:
278                         return SENSOR_STRING_TYPE_ORIENTATION;
279
280                 case SENSOR_TYPE_GYROSCOPE:
281                         return SENSOR_STRING_TYPE_GYROSCOPE;
282
283                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
284                         return SENSOR_STRING_TYPE_GYROSCOPE_UNCALIBRATED;
285
286                 case SENSOR_TYPE_LIGHT:
287                         return SENSOR_STRING_TYPE_LIGHT;
288
289                 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
290                         return SENSOR_STRING_TYPE_AMBIENT_TEMPERATURE;
291
292                 case SENSOR_TYPE_TEMPERATURE:
293                         return SENSOR_STRING_TYPE_TEMPERATURE;
294
295                 case SENSOR_TYPE_PROXIMITY:
296                         return SENSOR_STRING_TYPE_PROXIMITY;
297
298                 case SENSOR_TYPE_PRESSURE:
299                         return SENSOR_STRING_TYPE_PRESSURE;
300
301                 case SENSOR_TYPE_RELATIVE_HUMIDITY:
302                         return SENSOR_STRING_TYPE_RELATIVE_HUMIDITY;
303
304                 default:
305                         return "";
306                 }
307 }
308
309 flag_t sensor_get_flags (int s)
310 {
311         int catalog_index;
312         int sensor_type;
313
314         flag_t flags = 0x0;
315         catalog_index = sensor_info[s].catalog_index;
316         sensor_type = sensor_catalog[catalog_index].type;
317
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;
326                         break;
327
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;
333                         break;
334
335
336                 case SENSOR_TYPE_PROXIMITY:
337                         flags |= SENSOR_FLAG_WAKE_UP;
338                         flags |= SENSOR_FLAG_ON_CHANGE_MODE;
339                         break;
340
341                 default:
342                         ALOGI("Unknown sensor");
343                 }
344         return flags;
345 }
346
347 max_delay_t sensor_get_max_delay (int s)
348 {
349         char avail_sysfs_path[PATH_MAX];
350         int dev_num     = sensor_info[s].dev_num;
351         char freqs_buf[100];
352         char* cursor;
353         float min_supported_rate = 1000;
354         float sr;
355
356         /* continuous: maximum sampling period allowed in microseconds.
357          * on-change, one-shot, special : 0
358          */
359
360         if (sensor_desc[s].flags)
361                 return 0;
362
363         sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
364
365         if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) < 0)
366                 return 0;
367
368         cursor = freqs_buf;
369         while (*cursor && cursor[0]) {
370
371                 /* Decode a single value */
372                 sr = strtod(cursor, NULL);
373
374                 if (sr < min_supported_rate)
375                         min_supported_rate = sr;
376
377                 /* Skip digits */
378                 while (cursor[0] && !isspace(cursor[0]))
379                         cursor++;
380
381                 /* Skip spaces */
382                 while (cursor[0] && isspace(cursor[0]))
383                         cursor++;
384         }
385
386         /* return 0 for wrong values */
387         if (min_supported_rate < 0.1)
388                 return 0;
389
390         /* Return microseconds */
391         return (max_delay_t)(1000000.0 / min_supported_rate);
392 }