OSDN Git Service

1d2927e63f07944b4d72e087ba117716ca0644a7
[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, "terse"))
266                         sensor_info[s].quirks |= QUIRK_TERSE_DRIVER;
267
268                 if (strstr(quirks_buf, "noisy"))
269                         sensor_info[s].quirks |= QUIRK_NOISY;
270
271                 sensor_info[s].quirks |= QUIRK_ALREADY_DECODED;
272         }
273
274         return sensor_info[s].quirks;
275 }
276
277
278 int sensor_get_order (int s, unsigned char map[MAX_CHANNELS])
279 {
280         char buf[MAX_NAME_SIZE];
281         int i;
282         int count = sensor_catalog[sensor_info[s].catalog_index].num_channels;
283
284         if  (sensor_get_st_prop(s, "order", buf))
285                 return 0; /* No order property */
286
287         /* Assume ASCII characters, in the '0'..'9' range */
288
289         for (i=0; i<count; i++)
290                 if (buf[i] - '0' >= count) {
291                         ALOGE("Order index out of range for sensor %d\n", s);
292                         return 0;
293                 }
294
295         for (i=0; i<count; i++)
296                 map[i] = buf[i] - '0';
297
298         return 1;       /* OK to use modified ordering map */
299 }
300
301 char* sensor_get_string_type(int s)
302 {
303         int catalog_index;
304         int sensor_type;
305
306         catalog_index = sensor_info[s].catalog_index;
307         sensor_type = sensor_catalog[catalog_index].type;
308
309         switch (sensor_type) {
310                 case SENSOR_TYPE_ACCELEROMETER:
311                         return SENSOR_STRING_TYPE_ACCELEROMETER;
312
313                 case SENSOR_TYPE_MAGNETIC_FIELD:
314                         return SENSOR_STRING_TYPE_MAGNETIC_FIELD;
315
316                 case SENSOR_TYPE_ORIENTATION:
317                         return SENSOR_STRING_TYPE_ORIENTATION;
318
319                 case SENSOR_TYPE_GYROSCOPE:
320                         return SENSOR_STRING_TYPE_GYROSCOPE;
321
322                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
323                         return SENSOR_STRING_TYPE_GYROSCOPE_UNCALIBRATED;
324
325                 case SENSOR_TYPE_LIGHT:
326                         return SENSOR_STRING_TYPE_LIGHT;
327
328                 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
329                         return SENSOR_STRING_TYPE_AMBIENT_TEMPERATURE;
330
331                 case SENSOR_TYPE_TEMPERATURE:
332                         return SENSOR_STRING_TYPE_TEMPERATURE;
333
334                 case SENSOR_TYPE_PROXIMITY:
335                         return SENSOR_STRING_TYPE_PROXIMITY;
336
337                 case SENSOR_TYPE_PRESSURE:
338                         return SENSOR_STRING_TYPE_PRESSURE;
339
340                 case SENSOR_TYPE_RELATIVE_HUMIDITY:
341                         return SENSOR_STRING_TYPE_RELATIVE_HUMIDITY;
342
343                 default:
344                         return "";
345                 }
346 }
347
348 flag_t sensor_get_flags (int s)
349 {
350         int catalog_index;
351         int sensor_type;
352
353         flag_t flags = 0x0;
354         catalog_index = sensor_info[s].catalog_index;
355         sensor_type = sensor_catalog[catalog_index].type;
356
357         switch (sensor_type) {
358                 case SENSOR_TYPE_ACCELEROMETER:
359                 case SENSOR_TYPE_MAGNETIC_FIELD:
360                 case SENSOR_TYPE_ORIENTATION:
361                 case SENSOR_TYPE_GYROSCOPE:
362                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
363                 case SENSOR_TYPE_PRESSURE:
364                         flags |= SENSOR_FLAG_CONTINUOUS_MODE;
365                         break;
366
367                 case SENSOR_TYPE_LIGHT:
368                 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
369                 case SENSOR_TYPE_TEMPERATURE:
370                 case SENSOR_TYPE_RELATIVE_HUMIDITY:
371                         flags |= SENSOR_FLAG_ON_CHANGE_MODE;
372                         break;
373
374
375                 case SENSOR_TYPE_PROXIMITY:
376                         flags |= SENSOR_FLAG_WAKE_UP;
377                         flags |= SENSOR_FLAG_ON_CHANGE_MODE;
378                         break;
379
380                 default:
381                         ALOGI("Unknown sensor");
382                 }
383         return flags;
384 }
385
386 max_delay_t sensor_get_max_delay (int s)
387 {
388         char avail_sysfs_path[PATH_MAX];
389         int dev_num     = sensor_info[s].dev_num;
390         char freqs_buf[100];
391         char* cursor;
392         float min_supported_rate = 1000;
393         float sr;
394
395         /* continuous: maximum sampling period allowed in microseconds.
396          * on-change, one-shot, special : 0
397          */
398
399         if (sensor_desc[s].flags)
400                 return 0;
401
402         sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
403
404         if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) < 0)
405                 return 0;
406
407         cursor = freqs_buf;
408         while (*cursor && cursor[0]) {
409
410                 /* Decode a single value */
411                 sr = strtod(cursor, NULL);
412
413                 if (sr < min_supported_rate)
414                         min_supported_rate = sr;
415
416                 /* Skip digits */
417                 while (cursor[0] && !isspace(cursor[0]))
418                         cursor++;
419
420                 /* Skip spaces */
421                 while (cursor[0] && isspace(cursor[0]))
422                         cursor++;
423         }
424
425         /* return 0 for wrong values */
426         if (min_supported_rate < 0.1)
427                 return 0;
428
429         /* Return microseconds */
430         return (max_delay_t)(1000000.0 / min_supported_rate);
431 }
432
433 /* this value depends on the reporting mode:
434  *
435  *   continuous: minimum sample period allowed in microseconds
436  *   on-change : 0
437  *   one-shot  :-1
438  *   special   : 0, unless otherwise noted
439  */
440 int32_t sensor_get_min_delay(int s)
441 {
442         char avail_sysfs_path[PATH_MAX];
443         int dev_num     = sensor_info[s].dev_num;
444         char freqs_buf[100];
445         char* cursor;
446         float max_supported_rate = 0;
447         float sr;
448         int catalog_index = sensor_info[s].catalog_index;
449         int sensor_type = sensor_catalog[catalog_index].type;
450
451
452         sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
453
454         if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) < 0) {
455                 /* If poll mode sensor */
456                 if (!sensor_info[s].num_channels) {
457                         switch (sensor_type) {
458                                 case SENSOR_TYPE_ACCELEROMETER:
459                                         max_supported_rate = 125; /* 125 Hz */
460                                         break;
461                                 case SENSOR_TYPE_GYROSCOPE:
462                                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
463                                         max_supported_rate = 200; /* 200 Hz */
464                                         break;
465                                 case SENSOR_TYPE_MAGNETIC_FIELD:
466                                         max_supported_rate = 10; /* 10 Hz */
467                                         break;
468                                 default:
469                                         max_supported_rate = 0;
470                         }
471                 }
472         } else {
473                 cursor = freqs_buf;
474                 while (*cursor && cursor[0]) {
475
476                         /* Decode a single value */
477                         sr = strtod(cursor, NULL);
478
479                         if (sr > max_supported_rate && sr <= MAX_EVENTS)
480                                 max_supported_rate = sr;
481
482                         /* Skip digits */
483                         while (cursor[0] && !isspace(cursor[0]))
484                                 cursor++;
485
486                         /* Skip spaces */
487                         while (cursor[0] && isspace(cursor[0]))
488                                 cursor++;
489                 }
490         }
491
492         return (int32_t)(1000000.0 / max_supported_rate);
493 }