OSDN Git Service

b876bcd442208fe699480b9deef247365f7e7f62
[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         memset(map, 0, MAX_CHANNELS);
246
247         if  (sensor_get_st_prop(s, "order", buf))
248                 return 0; /* No order property */
249
250         /* Assume ASCII characters, in the '0'..'9' range */
251
252         for (i=0; i<count; i++)
253                 map[i] = buf[i] - '0';
254
255         /* Check that our indices are in range */
256         for (i=0; i<count; i++)
257                 if (map[i] >= count) {
258                         ALOGE("Order index out of range for sensor %d\n", s);
259                         return 0;
260                 }
261
262         return 1;       /* OK to use modified ordering map */
263 }
264
265 char* sensor_get_string_type(int s)
266 {
267         int catalog_index;
268         int sensor_type;
269
270         catalog_index = sensor_info[s].catalog_index;
271         sensor_type = sensor_catalog[catalog_index].type;
272
273         switch (sensor_type) {
274                 case SENSOR_TYPE_ACCELEROMETER:
275                         return SENSOR_STRING_TYPE_ACCELEROMETER;
276
277                 case SENSOR_TYPE_MAGNETIC_FIELD:
278                         return SENSOR_STRING_TYPE_MAGNETIC_FIELD;
279
280                 case SENSOR_TYPE_ORIENTATION:
281                         return SENSOR_STRING_TYPE_ORIENTATION;
282
283                 case SENSOR_TYPE_GYROSCOPE:
284                         return SENSOR_STRING_TYPE_GYROSCOPE;
285
286                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
287                         return SENSOR_STRING_TYPE_GYROSCOPE_UNCALIBRATED;
288
289                 case SENSOR_TYPE_LIGHT:
290                         return SENSOR_STRING_TYPE_LIGHT;
291
292                 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
293                         return SENSOR_STRING_TYPE_AMBIENT_TEMPERATURE;
294
295                 case SENSOR_TYPE_TEMPERATURE:
296                         return SENSOR_STRING_TYPE_TEMPERATURE;
297
298                 case SENSOR_TYPE_PROXIMITY:
299                         return SENSOR_STRING_TYPE_PROXIMITY;
300
301                 case SENSOR_TYPE_PRESSURE:
302                         return SENSOR_STRING_TYPE_PRESSURE;
303
304                 case SENSOR_TYPE_RELATIVE_HUMIDITY:
305                         return SENSOR_STRING_TYPE_RELATIVE_HUMIDITY;
306
307                 default:
308                         return "";
309                 }
310 }
311
312 flag_t sensor_get_flags (int s)
313 {
314         int catalog_index;
315         int sensor_type;
316
317         flag_t flags = 0x0;
318         catalog_index = sensor_info[s].catalog_index;
319         sensor_type = sensor_catalog[catalog_index].type;
320
321         switch (sensor_type) {
322                 case SENSOR_TYPE_ACCELEROMETER:
323                 case SENSOR_TYPE_MAGNETIC_FIELD:
324                 case SENSOR_TYPE_ORIENTATION:
325                 case SENSOR_TYPE_GYROSCOPE:
326                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
327                 case SENSOR_TYPE_PRESSURE:
328                         flags |= SENSOR_FLAG_CONTINUOUS_MODE;
329                         break;
330
331                 case SENSOR_TYPE_LIGHT:
332                 case SENSOR_TYPE_AMBIENT_TEMPERATURE:
333                 case SENSOR_TYPE_TEMPERATURE:
334                 case SENSOR_TYPE_RELATIVE_HUMIDITY:
335                         flags |= SENSOR_FLAG_ON_CHANGE_MODE;
336                         break;
337
338
339                 case SENSOR_TYPE_PROXIMITY:
340                         flags |= SENSOR_FLAG_WAKE_UP;
341                         flags |= SENSOR_FLAG_ON_CHANGE_MODE;
342                         break;
343
344                 default:
345                         ALOGI("Unknown sensor");
346                 }
347         return flags;
348 }
349
350 max_delay_t sensor_get_max_delay (int s)
351 {
352         char avail_sysfs_path[PATH_MAX];
353         int dev_num     = sensor_info[s].dev_num;
354         char freqs_buf[100];
355         char* cursor;
356         float min_supported_rate = 1000;
357         float sr;
358
359         /* continuous: maximum sampling period allowed in microseconds.
360          * on-change, one-shot, special : 0
361          */
362
363         if (sensor_desc[s].flags)
364                 return 0;
365
366         sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
367
368         if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) < 0)
369                 return 0;
370
371         cursor = freqs_buf;
372         while (*cursor && cursor[0]) {
373
374                 /* Decode a single value */
375                 sr = strtod(cursor, NULL);
376
377                 if (sr < min_supported_rate)
378                         min_supported_rate = sr;
379
380                 /* Skip digits */
381                 while (cursor[0] && !isspace(cursor[0]))
382                         cursor++;
383
384                 /* Skip spaces */
385                 while (cursor[0] && isspace(cursor[0]))
386                         cursor++;
387         }
388
389         /* return 0 for wrong values */
390         if (min_supported_rate < 0.1)
391                 return 0;
392
393         /* Return microseconds */
394         return (max_delay_t)(1000000.0 / min_supported_rate);
395 }