OSDN Git Service

2fa1e136a9de31005d7e7324d6cb6731b6e6cf78
[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  * 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  * 'panel' and 'rotation' specifiers can be used to express ACPI PLD placement
71  * information ; if found they will be used in priority over the actual ACPI
72  * data. That is intended as a way to verify values during development.
73  *
74  * It's possible to use the contents of the iio device name as a way to
75  * discriminate between sensors. Several sensors of the same type can coexist:
76  * e.g. ro.iio.temp.bmg160.name = BMG160 Thermometer will be used in priority
77  * over ro.iio.temp.name = BMC150 Thermometer if the sensor for which we query
78  * properties values happen to have its iio device name set to bmg160.
79  */
80
81 static int sensor_get_st_prop (int s, const char* sel, char val[MAX_NAME_SIZE])
82 {
83         char prop_name[PROP_NAME_MAX];
84         char prop_val[PROP_VALUE_MAX];
85         char extended_sel[PROP_VALUE_MAX];
86
87         int i                   = sensor_info[s].catalog_index;
88         const char *prefix      = sensor_catalog[i].tag;
89
90         /* First try most specialized form, like ro.iio.anglvel.bmg160.name */
91
92         snprintf(extended_sel, PROP_NAME_MAX, "%s.%s",
93                  sensor_info[s].internal_name, sel);
94
95         snprintf(prop_name, PROP_NAME_MAX, PROP_BASE, prefix, extended_sel);
96
97         if (property_get(prop_name, prop_val, "")) {
98                 strncpy(val, prop_val, MAX_NAME_SIZE-1);
99                 val[MAX_NAME_SIZE-1] = '\0';
100                 return 0;
101         }
102
103         /* Fall back to simple form, like ro.iio.anglvel.name */
104
105         sprintf(prop_name, PROP_BASE, prefix, sel);
106
107         if (property_get(prop_name, prop_val, "")) {
108                 strncpy(val, prop_val, MAX_NAME_SIZE-1);
109                 val[MAX_NAME_SIZE-1] = '\0';
110                 return 0;
111         }
112
113         return -1;
114 }
115
116
117 int sensor_get_prop (int s, const char* sel, int* val)
118 {
119         char buf[MAX_NAME_SIZE];
120
121         if (sensor_get_st_prop(s, sel, buf))
122                 return -1;
123
124         *val = atoi(buf);
125         return 0;
126 }
127
128
129 int sensor_get_fl_prop (int s, const char* sel, float* val)
130 {
131         char buf[MAX_NAME_SIZE];
132
133         if (sensor_get_st_prop(s, sel, buf))
134                 return -1;
135
136         *val = (float) strtod(buf, NULL);
137         return 0;
138 }
139
140
141 char* sensor_get_name (int s)
142 {
143         if (sensor_info[s].friendly_name[0] != '\0' ||
144                 !sensor_get_st_prop(s, "name", sensor_info[s].friendly_name))
145                         return sensor_info[s].friendly_name;
146
147         /* If we got a iio device name from sysfs, use it */
148         if (sensor_info[s].internal_name[0]) {
149                 snprintf(sensor_info[s].friendly_name, MAX_NAME_SIZE, "S%d-%s",
150                          s, sensor_info[s].internal_name);
151         } else {
152                 sprintf(sensor_info[s].friendly_name, "S%d", s);
153         }
154
155         return sensor_info[s].friendly_name;
156 }
157
158
159 char* sensor_get_vendor (int s)
160 {
161         if (sensor_info[s].vendor_name[0] ||
162                 !sensor_get_st_prop(s, "vendor", sensor_info[s].vendor_name))
163                         return sensor_info[s].vendor_name;
164
165         return "";
166 }
167
168
169 int sensor_get_version (int s)
170 {
171         return IIO_SENSOR_HAL_VERSION;
172 }
173
174
175 float sensor_get_max_range (int s)
176 {
177         int catalog_index;
178         int sensor_type;
179
180         if (sensor_info[s].max_range != 0.0 ||
181                 !sensor_get_fl_prop(s, "max_range", &sensor_info[s].max_range))
182                         return sensor_info[s].max_range;
183
184         /* Try returning a sensible value given the sensor type */
185
186         /* We should cap returned samples accordingly... */
187
188         catalog_index = sensor_info[s].catalog_index;
189         sensor_type = sensor_catalog[catalog_index].type;
190
191         switch (sensor_type) {
192                 case SENSOR_TYPE_ACCELEROMETER:         /* m/s^2        */
193                         return 50;
194
195                 case SENSOR_TYPE_MAGNETIC_FIELD:        /* micro-tesla  */
196                         return 500;
197
198                 case SENSOR_TYPE_ORIENTATION:           /* degrees      */
199                         return 360;
200
201                 case SENSOR_TYPE_GYROSCOPE:             /* radians/s    */
202                         return 10;
203
204                 case SENSOR_TYPE_LIGHT:                 /* SI lux units */
205                         return 50000;
206
207                 case SENSOR_TYPE_AMBIENT_TEMPERATURE:   /* Â°C          */
208                 case SENSOR_TYPE_TEMPERATURE:           /* Â°C          */
209                 case SENSOR_TYPE_PROXIMITY:             /* centimeters  */
210                 case SENSOR_TYPE_PRESSURE:              /* hecto-pascal */
211                 case SENSOR_TYPE_RELATIVE_HUMIDITY:     /* percent */
212                         return 100;
213
214                 default:
215                         return 0.0;
216                 }
217 }
218
219
220 float sensor_get_resolution (int s)
221 {
222         if (sensor_info[s].resolution != 0.0 ||
223                 !sensor_get_fl_prop(s, "resolution", &sensor_info[s].resolution))
224                         return sensor_info[s].resolution;
225
226         return 0;
227 }
228
229
230 float sensor_get_power (int s)
231 {
232         /* mA used while sensor is in use ; not sure about volts :) */
233         if (sensor_info[s].power != 0.0 ||
234                 !sensor_get_fl_prop(s, "power", &sensor_info[s].power))
235                         return sensor_info[s].power;
236
237         return 0;
238 }
239
240
241 float sensor_get_illumincalib (int s)
242 {
243         /* calibrating the ALS Sensor*/
244         if (sensor_info[s].illumincalib != 0.0 ||
245                 !sensor_get_fl_prop(s, "illumincalib", &sensor_info[s].illumincalib)) {
246                         return sensor_info[s].illumincalib;
247         }
248
249         return 0;
250 }
251
252
253 uint32_t sensor_get_quirks (int s)
254 {
255         char quirks_buf[MAX_NAME_SIZE];
256
257         /* Read and decode quirks property on first reference */
258         if (!(sensor_info[s].quirks & QUIRK_ALREADY_DECODED)) {
259                 quirks_buf[0] = '\0';
260                 sensor_get_st_prop(s, "quirks", quirks_buf);
261
262                 if (strstr(quirks_buf, "init-rate"))
263                         sensor_info[s].quirks |= QUIRK_INITIAL_RATE;
264
265                 if (strstr(quirks_buf, "continuous")) {
266                         sensor_info[s].quirks |= QUIRK_CONTINUOUS_DRIVER;
267                 }
268
269                 if (strstr(quirks_buf, "terse") && !(sensor_info[s].quirks & QUIRK_CONTINUOUS_DRIVER))
270                         sensor_info[s].quirks |= QUIRK_TERSE_DRIVER;
271
272                 if (strstr(quirks_buf, "noisy"))
273                         sensor_info[s].quirks |= QUIRK_NOISY;
274
275                 sensor_info[s].quirks |= QUIRK_ALREADY_DECODED;
276         }
277
278         return sensor_info[s].quirks;
279 }
280
281
282 int sensor_get_order (int s, unsigned char map[MAX_CHANNELS])
283 {
284         char buf[MAX_NAME_SIZE];
285         int i;
286         int count = sensor_catalog[sensor_info[s].catalog_index].num_channels;
287
288         if  (sensor_get_st_prop(s, "order", buf))
289                 return 0; /* No order property */
290
291         /* Assume ASCII characters, in the '0'..'9' range */
292
293         for (i=0; i<count; i++)
294                 if (buf[i] - '0' >= count) {
295                         ALOGE("Order index out of range for sensor %d\n", s);
296                         return 0;
297                 }
298
299         for (i=0; i<count; i++)
300                 map[i] = buf[i] - '0';
301
302         return 1;       /* OK to use modified ordering map */
303 }
304
305 char* sensor_get_string_type(int s)
306 {
307         int catalog_index;
308         int sensor_type;
309
310         catalog_index = sensor_info[s].catalog_index;
311         sensor_type = sensor_catalog[catalog_index].type;
312
313         switch (sensor_type) {
314                 case SENSOR_TYPE_ACCELEROMETER:
315                         return SENSOR_STRING_TYPE_ACCELEROMETER;
316
317                 case SENSOR_TYPE_MAGNETIC_FIELD:
318                         return SENSOR_STRING_TYPE_MAGNETIC_FIELD;
319
320                 case SENSOR_TYPE_ORIENTATION:
321                         return SENSOR_STRING_TYPE_ORIENTATION;
322
323                 case SENSOR_TYPE_GYROSCOPE:
324                         return SENSOR_STRING_TYPE_GYROSCOPE;
325
326                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
327                         return SENSOR_STRING_TYPE_GYROSCOPE_UNCALIBRATED;
328
329                 case SENSOR_TYPE_LIGHT:
330                         return SENSOR_STRING_TYPE_LIGHT;
331
332                 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
333                         return SENSOR_STRING_TYPE_AMBIENT_TEMPERATURE;
334
335                 case SENSOR_TYPE_TEMPERATURE:
336                         return SENSOR_STRING_TYPE_TEMPERATURE;
337
338                 case SENSOR_TYPE_PROXIMITY:
339                         return SENSOR_STRING_TYPE_PROXIMITY;
340
341                 case SENSOR_TYPE_PRESSURE:
342                         return SENSOR_STRING_TYPE_PRESSURE;
343
344                 case SENSOR_TYPE_RELATIVE_HUMIDITY:
345                         return SENSOR_STRING_TYPE_RELATIVE_HUMIDITY;
346
347                 default:
348                         return "";
349                 }
350 }
351
352 flag_t sensor_get_flags (int s)
353 {
354         int catalog_index;
355         int sensor_type;
356
357         flag_t flags = 0x0;
358         catalog_index = sensor_info[s].catalog_index;
359         sensor_type = sensor_catalog[catalog_index].type;
360
361         switch (sensor_type) {
362                 case SENSOR_TYPE_ACCELEROMETER:
363                 case SENSOR_TYPE_MAGNETIC_FIELD:
364                 case SENSOR_TYPE_ORIENTATION:
365                 case SENSOR_TYPE_GYROSCOPE:
366                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
367                 case SENSOR_TYPE_PRESSURE:
368                         flags |= SENSOR_FLAG_CONTINUOUS_MODE;
369                         break;
370
371                 case SENSOR_TYPE_LIGHT:
372                 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
373                 case SENSOR_TYPE_TEMPERATURE:
374                 case SENSOR_TYPE_RELATIVE_HUMIDITY:
375                         flags |= SENSOR_FLAG_ON_CHANGE_MODE;
376                         break;
377
378
379                 case SENSOR_TYPE_PROXIMITY:
380                         flags |= SENSOR_FLAG_WAKE_UP;
381                         flags |= SENSOR_FLAG_ON_CHANGE_MODE;
382                         break;
383
384                 default:
385                         ALOGI("Unknown sensor");
386                 }
387         return flags;
388 }
389
390 max_delay_t sensor_get_max_delay (int s)
391 {
392         char avail_sysfs_path[PATH_MAX];
393         int dev_num     = sensor_info[s].dev_num;
394         char freqs_buf[100];
395         char* cursor;
396         float min_supported_rate = 1000;
397         float sr;
398
399         /* continuous: maximum sampling period allowed in microseconds.
400          * on-change, one-shot, special : 0
401          */
402
403         if (sensor_desc[s].flags)
404                 return 0;
405
406         sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
407
408         if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) < 0)
409                 return 0;
410
411         cursor = freqs_buf;
412         while (*cursor && cursor[0]) {
413
414                 /* Decode a single value */
415                 sr = strtod(cursor, NULL);
416
417                 if (sr < min_supported_rate)
418                         min_supported_rate = sr;
419
420                 /* Skip digits */
421                 while (cursor[0] && !isspace(cursor[0]))
422                         cursor++;
423
424                 /* Skip spaces */
425                 while (cursor[0] && isspace(cursor[0]))
426                         cursor++;
427         }
428
429         /* return 0 for wrong values */
430         if (min_supported_rate < 0.1)
431                 return 0;
432
433         /* Return microseconds */
434         return (max_delay_t)(1000000.0 / min_supported_rate);
435 }
436
437 /* this value depends on the reporting mode:
438  *
439  *   continuous: minimum sample period allowed in microseconds
440  *   on-change : 0
441  *   one-shot  :-1
442  *   special   : 0, unless otherwise noted
443  */
444 int32_t sensor_get_min_delay(int s)
445 {
446         char avail_sysfs_path[PATH_MAX];
447         int dev_num     = sensor_info[s].dev_num;
448         char freqs_buf[100];
449         char* cursor;
450         float max_supported_rate = 0;
451         float sr;
452         int catalog_index = sensor_info[s].catalog_index;
453         int sensor_type = sensor_catalog[catalog_index].type;
454
455
456         sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
457
458         if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) < 0) {
459                 /* If poll mode sensor */
460                 if (!sensor_info[s].num_channels) {
461                         switch (sensor_type) {
462                                 case SENSOR_TYPE_ACCELEROMETER:
463                                         max_supported_rate = 125; /* 125 Hz */
464                                         break;
465                                 case SENSOR_TYPE_GYROSCOPE:
466                                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
467                                         max_supported_rate = 200; /* 200 Hz */
468                                         break;
469                                 case SENSOR_TYPE_MAGNETIC_FIELD:
470                                         max_supported_rate = 10; /* 10 Hz */
471                                         break;
472                                 default:
473                                         max_supported_rate = 0;
474                         }
475                 }
476         } else {
477                 cursor = freqs_buf;
478                 while (*cursor && cursor[0]) {
479
480                         /* Decode a single value */
481                         sr = strtod(cursor, NULL);
482
483                         if (sr > max_supported_rate && sr <= MAX_EVENTS)
484                                 max_supported_rate = sr;
485
486                         /* Skip digits */
487                         while (cursor[0] && !isspace(cursor[0]))
488                                 cursor++;
489
490                         /* Skip spaces */
491                         while (cursor[0] && isspace(cursor[0]))
492                                 cursor++;
493                 }
494         }
495
496         return (int32_t)(1000000.0 / max_supported_rate);
497 }