OSDN Git Service

7874c2f5e0dc8975dc77ac537b3c5de5b493cd10
[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 "enumeration.h"
10
11 #define IIO_SENSOR_HAL_VERSION  1
12
13 /*
14  * About properties
15  *
16  * We acquire a number of parameters about sensors by reading properties.
17  * The idea here is that someone (either a script, or daemon, sets them
18  * depending on the set of sensors present on the machine.
19  *
20  * There are fallback paths in case the properties are not defined, but it is
21  * highly desirable to at least have the following for each sensor:
22  *
23  * ro.iio.anglvel.name = Gyroscope
24  * ro.iio.anglvel.vendor = Intel
25  * ro.iio.anglvel.max_range = 35
26  * ro.iio.anglvel.resolution = 0.002
27  * ro.iio.anglvel.power = 6.1
28  *
29  * Besides these, we have a couple of knobs initially used to cope with Intel
30  * Sensor Hub oddities, such as HID inspired units or firmware bugs:
31  *
32  * ro.iio.anglvel.transform = ISH
33  * ro.iio.anglvel.quirks = init-rate
34  *
35  * This one is used specifically to pass a calibration scale to ALS drivers:
36  *
37  * ro.iio.illuminance.name = CPLM3218x Ambient Light Sensor
38  * ro.iio.illuminance.vendor = Capella Microsystems
39  * ro.iio.illuminance.max_range = 167000
40  * ro.iio.illuminance.resolution = 1
41  * ro.iio.illuminance.power = .001
42  * ro.iio.illuminance.illumincalib = 7400
43  *
44  * Finally there's a 'opt_scale' specifier, documented as follows:
45  *
46  *  This adds support for a scaling factor that can be expressed
47  *  using properties, for all sensors, on a channel basis. That
48  *  scaling factor is applied after all other transforms have been
49  *  applied, and is intended as a way to compensate for problems
50  *  such as an incorrect axis polarity for a given sensor.
51  *
52  *  The syntax is <usual property prefix>.<channel>.opt_scale, e.g.
53  *  ro.iio.accel.y.opt_scale = -1 to negate the sign of the y readings
54  *  for the accelerometer.
55  *
56  *  For sensors using a single channel - and only those - the channel
57  *  name is implicitly void and a syntax such as ro.iio.illuminance.
58  *  opt_scale = 3 has to be used.
59  */
60
61 static int sensor_get_st_prop (int s, const char* sel, char val[MAX_NAME_SIZE])
62 {
63         char prop_name[PROP_NAME_MAX];
64         char prop_val[PROP_VALUE_MAX];
65         int i                   = sensor_info[s].catalog_index;
66         const char *prefix      = sensor_catalog[i].tag;
67
68         sprintf(prop_name, PROP_BASE, prefix, sel);
69
70         if (property_get(prop_name, prop_val, "")) {
71                 strncpy(val, prop_val, MAX_NAME_SIZE-1);
72                 val[MAX_NAME_SIZE-1] = '\0';
73                 return 0;
74         }
75
76         return -1;
77 }
78
79
80 int sensor_get_fl_prop (int s, const char* sel, float* val)
81 {
82         char buf[MAX_NAME_SIZE];
83
84         if (sensor_get_st_prop(s, sel, buf))
85                 return -1;
86
87         *val = (float) strtod(buf, NULL);
88         return 0;
89 }
90
91
92 char* sensor_get_name (int s)
93 {
94         if (sensor_info[s].friendly_name[0] != '\0' ||
95                 !sensor_get_st_prop(s, "name", sensor_info[s].friendly_name))
96                         return sensor_info[s].friendly_name;
97
98         /* If we got a iio device name from sysfs, use it */
99         if (sensor_info[s].internal_name[0]) {
100                 snprintf(sensor_info[s].friendly_name, MAX_NAME_SIZE, "S%d-%s",
101                          s, sensor_info[s].internal_name);
102         } else {
103                 sprintf(sensor_info[s].friendly_name, "S%d", s);
104         }
105
106         return sensor_info[s].friendly_name;
107 }
108
109
110 char* sensor_get_vendor (int s)
111 {
112         if (sensor_info[s].vendor_name[0] ||
113                 !sensor_get_st_prop(s, "vendor", sensor_info[s].vendor_name))
114                         return sensor_info[s].vendor_name;
115
116         return "";
117 }
118
119
120 int sensor_get_version (int s)
121 {
122         return IIO_SENSOR_HAL_VERSION;
123 }
124
125
126 float sensor_get_max_range (int s)
127 {
128         int catalog_index;
129         int sensor_type;
130
131         if (sensor_info[s].max_range != 0.0 ||
132                 !sensor_get_fl_prop(s, "max_range", &sensor_info[s].max_range))
133                         return sensor_info[s].max_range;
134
135         /* Try returning a sensible value given the sensor type */
136
137         /* We should cap returned samples accordingly... */
138
139         catalog_index = sensor_info[s].catalog_index;
140         sensor_type = sensor_catalog[catalog_index].type;
141
142         switch (sensor_type) {
143                 case SENSOR_TYPE_ACCELEROMETER:         /* m/s^2        */
144                         return 50;
145
146                 case SENSOR_TYPE_MAGNETIC_FIELD:        /* micro-tesla  */
147                         return 500;
148
149                 case SENSOR_TYPE_ORIENTATION:           /* degrees      */
150                         return 360;
151
152                 case SENSOR_TYPE_GYROSCOPE:             /* radians/s    */
153                         return 10;
154
155                 case SENSOR_TYPE_LIGHT:                 /* SI lux units */
156                         return 50000;
157
158                 case SENSOR_TYPE_AMBIENT_TEMPERATURE:   /* °C          */
159                 case SENSOR_TYPE_TEMPERATURE:           /* °C          */
160                 case SENSOR_TYPE_PROXIMITY:             /* centimeters  */
161                 case SENSOR_TYPE_PRESSURE:              /* hecto-pascal */
162                 case SENSOR_TYPE_RELATIVE_HUMIDITY:     /* percent */
163                         return 100;
164
165                 default:
166                         return 0.0;
167                 }
168 }
169
170
171 float sensor_get_resolution (int s)
172 {
173         if (sensor_info[s].resolution != 0.0 ||
174                 !sensor_get_fl_prop(s, "resolution", &sensor_info[s].resolution))
175                         return sensor_info[s].resolution;
176
177         return 0;
178 }
179
180
181 float sensor_get_power (int s)
182 {
183         /* mA used while sensor is in use ; not sure about volts :) */
184         if (sensor_info[s].power != 0.0 ||
185                 !sensor_get_fl_prop(s, "power", &sensor_info[s].power))
186                         return sensor_info[s].power;
187
188         return 0;
189 }
190
191
192 float sensor_get_illumincalib (int s)
193 {
194         /* calibrating the ALS Sensor*/
195         if (sensor_info[s].illumincalib != 0.0 ||
196                 !sensor_get_fl_prop(s, "illumincalib", &sensor_info[s].illumincalib)) {
197                         return sensor_info[s].illumincalib;
198         }
199
200         return 0;
201 }
202
203
204 int sensor_get_order (int s, unsigned char map[MAX_CHANNELS])
205 {
206         char buf[MAX_NAME_SIZE];
207         int i;
208         int count = sensor_catalog[sensor_info[s].catalog_index].num_channels;
209
210         memset(map, 0, MAX_CHANNELS);
211
212         if  (sensor_get_st_prop(s, "order", buf))
213                 return 0; /* No order property */
214
215         /* Assume ASCII characters, in the '0'..'9' range */
216
217         for (i=0; i<count; i++)
218                 map[i] = buf[i] - '0';
219
220         /* Check that our indices are in range */
221         for (i=0; i<count; i++)
222                 if (map[i] >= count) {
223                         ALOGE("Order index out of range for sensor %d\n", s);
224                         return 0;
225                 }
226
227         return 1;       /* OK to use modified ordering map */
228 }