OSDN Git Service

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