OSDN Git Service

Min/Max delay for Light and Temperature
[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 int get_cdd_freq(int s, int must)
391 {
392         int catalog_index = sensor_info[s].catalog_index;
393         int sensor_type = sensor_catalog[catalog_index].type;
394
395         switch (sensor_type) {
396                 case SENSOR_TYPE_ACCELEROMETER:
397                         return (must ? 100 : 200); /* must 100 Hz, should 200 Hz, CDD compliant */
398                 case SENSOR_TYPE_GYROSCOPE:
399                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
400                         return (must ? 200 : 200); /* must 200 Hz, should 200 Hz, CDD compliant */
401                 case SENSOR_TYPE_MAGNETIC_FIELD:
402                         return (must ? 10 : 50);   /* must 10 Hz, should 50 Hz, CDD compliant */
403                 case SENSOR_TYPE_LIGHT:
404                 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
405                 case SENSOR_TYPE_TEMPERATURE:
406                         return (must ? 1 : 2);     /* must 1 Hz, should 2Hz, not mentioned in CDD */
407                 default:
408                         return 0;
409         }
410 }
411
412 /* This value is defined only for continuous mode and on-change sensors. It is the delay between
413  * two sensor events corresponding to the lowest frequency that this sensor supports. When lower
414  * frequencies are requested through batch()/setDelay() the events will be generated at this
415  * frequency instead. It can be used by the framework or applications to estimate when the batch
416  * FIFO may be full.
417  *
418  * NOTE: 1) period_ns is in nanoseconds where as maxDelay/minDelay are in microseconds.
419  *              continuous, on-change: maximum sampling period allowed in microseconds.
420  *              one-shot, special : 0
421  *   2) maxDelay should always fit within a 32 bit signed integer. It is declared as 64 bit
422  *      on 64 bit architectures only for binary compatibility reasons.
423  * Availability: SENSORS_DEVICE_API_VERSION_1_3
424  */
425 max_delay_t sensor_get_max_delay (int s)
426 {
427         char avail_sysfs_path[PATH_MAX];
428         int dev_num     = sensor_info[s].dev_num;
429         char freqs_buf[100];
430         char* cursor;
431         float min_supported_rate = 1000;
432         float sr;
433
434         /* continuous, on-change: maximum sampling period allowed in microseconds.
435          * one-shot, special : 0
436          */
437         if (REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_ONE_SHOT_MODE ||
438             REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_SPECIAL_REPORTING_MODE)
439                 return 0;
440
441         sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
442
443         if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) < 0) {
444                 /* If poll mode sensor */
445                 if (!sensor_info[s].num_channels) {
446                         /* The must rate */
447                         min_supported_rate = get_cdd_freq(s, 1);
448                 }
449         } else {
450                 cursor = freqs_buf;
451                 while (*cursor && cursor[0]) {
452
453                         /* Decode a single value */
454                         sr = strtod(cursor, NULL);
455
456                         if (sr < min_supported_rate)
457                                 min_supported_rate = sr;
458
459                         /* Skip digits */
460                         while (cursor[0] && !isspace(cursor[0]))
461                                 cursor++;
462
463                         /* Skip spaces */
464                         while (cursor[0] && isspace(cursor[0]))
465                                 cursor++;
466                 }
467         }
468
469         /* return 0 for wrong values */
470         if (min_supported_rate < 0.1)
471                 return 0;
472
473         /* Return microseconds */
474         return (max_delay_t)(1000000.0 / min_supported_rate);
475 }
476
477 /* this value depends on the reporting mode:
478  *
479  *   continuous: minimum sample period allowed in microseconds
480  *   on-change : 0
481  *   one-shot  :-1
482  *   special   : 0, unless otherwise noted
483  */
484 int32_t sensor_get_min_delay(int s)
485 {
486         char avail_sysfs_path[PATH_MAX];
487         int dev_num     = sensor_info[s].dev_num;
488         char freqs_buf[100];
489         char* cursor;
490         float max_supported_rate = 0;
491         float sr;
492
493         /* continuous: minimum sampling period allowed in microseconds.
494          * on-change, special : 0
495          * one-shot  :-1
496          */
497         if (REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_ON_CHANGE_MODE ||
498             REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_SPECIAL_REPORTING_MODE)
499                 return 0;
500
501         if (REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_ONE_SHOT_MODE)
502                 return -1;
503
504         sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
505
506         if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) < 0) {
507                 /* If poll mode sensor */
508                 if (!sensor_info[s].num_channels) {
509                         /* The should rate */
510                         max_supported_rate = get_cdd_freq(s, 0);
511                 }
512         } else {
513                 cursor = freqs_buf;
514                 while (*cursor && cursor[0]) {
515
516                         /* Decode a single value */
517                         sr = strtod(cursor, NULL);
518
519                         if (sr > max_supported_rate && sr <= MAX_EVENTS)
520                                 max_supported_rate = sr;
521
522                         /* Skip digits */
523                         while (cursor[0] && !isspace(cursor[0]))
524                                 cursor++;
525
526                         /* Skip spaces */
527                         while (cursor[0] && isspace(cursor[0]))
528                                 cursor++;
529                 }
530         }
531
532         /* return 0 for wrong values */
533         if (max_supported_rate < 0.1)
534                 return 0;
535
536         /* Return microseconds */
537         return (int32_t)(1000000.0 / max_supported_rate);
538 }