OSDN Git Service

iio_sensors: Add Light sensor sysfs interface and transformation
[android-x86/hardware-intel-libsensors.git] / enumeration.c
1 /*
2  * Copyright (C) 2014 Intel Corporation.
3  */
4
5 #include <dirent.h>
6 #include <utils/Log.h>
7 #include <hardware/sensors.h>
8 #include "enumeration.h"
9 #include "description.h"
10 #include "utils.h"
11 #include "transform.h"
12 #include "description.h"
13 #include "control.h"
14
15 /*
16  * This table maps syfs entries in scan_elements directories to sensor types,
17  * and will also be used to determine other sysfs names as well as the iio
18  * device number associated to a specific sensor.
19  */
20
21 struct sensor_catalog_entry_t sensor_catalog[] = {
22         DECLARE_SENSOR3("accel",      SENSOR_TYPE_ACCELEROMETER,  "x", "y", "z")
23         DECLARE_SENSOR3("anglvel",    SENSOR_TYPE_GYROSCOPE,      "x", "y", "z")
24         DECLARE_SENSOR3("magn",       SENSOR_TYPE_MAGNETIC_FIELD, "x", "y", "z")
25         DECLARE_SENSOR1("intensity",  SENSOR_TYPE_LIGHT,          "both"       )
26         DECLARE_SENSOR0("illuminance",SENSOR_TYPE_LIGHT                        )
27         DECLARE_SENSOR3("incli",      SENSOR_TYPE_ORIENTATION,    "x", "y", "z")
28         DECLARE_SENSOR4("rot",        SENSOR_TYPE_ROTATION_VECTOR,
29                                          "quat_x", "quat_y", "quat_z", "quat_w")
30         DECLARE_SENSOR0("temp",       SENSOR_TYPE_AMBIENT_TEMPERATURE          )
31 };
32
33 #define CATALOG_SIZE    ARRAY_SIZE(sensor_catalog)
34
35
36 /* We equate sensor handles to indices in these tables */
37
38 struct sensor_t      sensor_desc[MAX_SENSORS];  /* Android-level descriptors */
39 struct sensor_info_t sensor_info[MAX_SENSORS];  /* Internal descriptors      */
40 int sensor_count;                               /* Detected sensors          */
41
42
43 static void add_sensor (int dev_num, int catalog_index, int use_polling)
44 {
45         int s;
46         int sensor_type;
47         int retval;
48         char sysfs_path[PATH_MAX];
49         const char* prefix;
50         float scale;
51         int c;
52         float opt_scale;
53         const char* ch_name;
54         int num_channels;
55         char suffix[MAX_NAME_SIZE + 8];
56
57         if (sensor_count == MAX_SENSORS) {
58                 ALOGE("Too many sensors!\n");
59                 return;
60         }
61
62         sensor_type = sensor_catalog[catalog_index].type;
63
64         /*
65          * At this point we could check that the expected sysfs attributes are
66          * present ; that would enable having multiple catalog entries with the
67          * same sensor type, accomodating different sets of sysfs attributes.
68          */
69
70         s = sensor_count;
71
72         sensor_info[s].dev_num          = dev_num;
73         sensor_info[s].catalog_index    = catalog_index;
74
75         if (use_polling)
76                 sensor_info[s].num_channels = 0;
77         else
78                 sensor_info[s].num_channels =
79                                 sensor_catalog[catalog_index].num_channels;
80
81         prefix = sensor_catalog[catalog_index].tag;
82
83         /*
84          * receiving the illumination sensor calibration inputs from
85          * the Android properties and setting it within sysfs
86          */
87         if (sensor_catalog[catalog_index].type == SENSOR_TYPE_LIGHT) {
88                 retval = sensor_get_illumincalib(s);
89                 sprintf(sysfs_path, ILLUMINATION_CALIBPATH, dev_num);
90                 sysfs_write_int(sysfs_path, retval);
91         }
92
93         /* Read name attribute, if available */
94         sprintf(sysfs_path, NAME_PATH, dev_num);
95         sysfs_read_str(sysfs_path, sensor_info[s].internal_name, MAX_NAME_SIZE);
96
97         /* See if we have general offsets and scale values for this sensor */
98
99         sprintf(sysfs_path, SENSOR_OFFSET_PATH, dev_num, prefix);
100         sysfs_read_float(sysfs_path, &sensor_info[s].offset);
101
102         sprintf(sysfs_path, SENSOR_SCALE_PATH, dev_num, prefix);
103         if (!sysfs_read_float(sysfs_path, &scale)) {
104                 sensor_info[s].scale = scale;
105                 ALOGI("Scale path:%s scale:%f dev_num:%d\n",
106                                         sysfs_path, scale, dev_num);
107         } else {
108                 sensor_info[s].scale = 1;
109
110                 /* Read channel specific scale if any*/
111                 for (c = 0; c < sensor_catalog[catalog_index].num_channels; c++)
112                 {
113                         sprintf(sysfs_path, BASE_PATH "%s", dev_num,
114                            sensor_catalog[catalog_index].channel[c].scale_path);
115
116                         if (!sysfs_read_float(sysfs_path, &scale)) {
117                                 sensor_info[s].channel[c].scale = scale;
118                                 sensor_info[s].scale = 0;
119
120                                 ALOGI(  "Scale path:%s "
121                                         "channel scale:%f dev_num:%d\n",
122                                         sysfs_path, scale, dev_num);
123                         }
124                 }
125         }
126
127         /*
128          * See if we have optional correction scaling factors for each of the
129          * channels of this sensor. These would be expressed using properties
130          * like iio.accel.y.scale = -1. In case of a single channel we also
131          * support things such as iio.temp.scale = -1. Note that this works
132          * for all types of sensors, and whatever transform is selected, on top
133          * of any previous conversions.
134          */
135         num_channels = sensor_catalog[catalog_index].num_channels;
136
137         if (num_channels) {
138                 for (c = 0; c < num_channels; c++) {
139                         opt_scale = 1;
140
141                         ch_name = sensor_catalog[catalog_index].channel[c].name;
142                         sprintf(suffix, "%s.scale", ch_name);
143                         sensor_get_fl_prop(s, suffix, &opt_scale);
144
145                         sensor_info[s].channel[c].opt_scale = opt_scale;
146                 }
147         } else {
148                 opt_scale = 1;
149                 sensor_get_fl_prop(s, "scale", &opt_scale);
150                 sensor_info[s].channel[0].opt_scale = opt_scale;
151         }
152
153         /* Initialize Android-visible descriptor */
154         sensor_desc[s].name             = sensor_get_name(s);
155         sensor_desc[s].vendor           = sensor_get_vendor(s);
156         sensor_desc[s].version          = sensor_get_version(s);
157         sensor_desc[s].handle           = s;
158         sensor_desc[s].type             = sensor_type;
159         sensor_desc[s].maxRange         = sensor_get_max_range(s);
160         sensor_desc[s].resolution       = sensor_get_resolution(s);
161         sensor_desc[s].power            = sensor_get_power(s);
162
163         if (sensor_info[s].internal_name[0] == '\0') {
164                 /*
165                  * In case the kernel-mode driver doesn't expose a name for
166                  * the iio device, use (null)-dev%d as the trigger name...
167                  * This can be considered a kernel-mode iio driver bug.
168                  */
169                 ALOGW("Using null trigger on sensor %d (dev %d)\n", s, dev_num);
170                 strcpy(sensor_info[s].internal_name, "(null)");
171         }
172
173         /* Select one of the available sensor sample processing styles */
174         select_transform(s);
175
176         sensor_count++;
177 }
178
179
180 static void discover_poll_sensors (int dev_num, char map[CATALOG_SIZE])
181 {
182         char base_dir[PATH_MAX];
183         DIR *dir;
184         char sysfs_dir[PATH_MAX];
185         struct sensor *sensor;
186         struct dirent *d;
187         unsigned int i;
188         int c;
189
190         memset(map, 0, CATALOG_SIZE);
191
192         snprintf(base_dir, sizeof(base_dir), BASE_PATH, dev_num);
193
194         dir = opendir(base_dir);
195         if (!dir) {
196                return;
197         }
198
199         /* Enumerate entries in this iio device's base folder */
200
201         while ((d = readdir(dir))) {
202                 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
203                         continue;
204
205                 /* If the name matches a catalog entry, flag it */
206                 for (i = 0; i<CATALOG_SIZE; i++)
207                         for (c=0; c<sensor_catalog[i].num_channels; c++)
208                                 if (!strcmp(d->d_name,
209                                     sensor_catalog[i].channel[c].raw_path) ||
210                                     !strcmp(d->d_name,
211                                     sensor_catalog[i].channel[c].input_path)) {
212                                 map[i] = 1;
213                                 break;
214                         }
215         }
216
217         closedir(dir);
218 }
219
220
221 static void discover_trig_sensors (int dev_num, char map[CATALOG_SIZE])
222 {
223         char scan_elem_dir[PATH_MAX];
224         DIR *dir;
225         char sysfs_dir[PATH_MAX];
226         struct sensor *sensor;
227         struct dirent *d;
228         unsigned int i;
229
230         memset(map, 0, CATALOG_SIZE);
231
232         /* Enumerate entries in this iio device's scan_elements folder */
233
234         snprintf(scan_elem_dir, sizeof(scan_elem_dir), CHANNEL_PATH, dev_num);
235
236         dir = opendir(scan_elem_dir);
237         if (!dir) {
238                return;
239         }
240
241         while ((d = readdir(dir))) {
242                 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
243                         continue;
244
245                 /* Compare en entry to known ones and create matching sensors */
246
247                 for (i = 0; i<CATALOG_SIZE; i++)
248                         if (!strcmp(d->d_name,
249                                     sensor_catalog[i].channel[0].en_path)) {
250                                 map[i] = 1;
251                                 break;
252                         }
253         }
254
255         closedir(dir);
256 }
257
258
259 static void orientation_sensor_check(void)
260 {
261         /*
262          * If we have accel + gyro + magn but no rotation vector sensor,
263          * SensorService replaces the HAL provided orientation sensor by the
264          * AOSP version... provided we report one. So initialize a virtual
265          * orientation sensor with zero values, which will get replaced. See:
266          * frameworks/native/services/sensorservice/SensorService.cpp, looking
267          * for SENSOR_TYPE_ROTATION_VECTOR; that code should presumably fall
268          * back to mUserSensorList.add instead of replaceAt, but accommodate it.
269          */
270
271         int i;
272         int has_acc = 0;
273         int has_gyr = 0;
274         int has_mag = 0;
275         int has_rot = 0;
276         int has_ori = 0;
277         int catalog_size = CATALOG_SIZE;
278
279         for (i=0; i<sensor_count; i++)
280                 switch (sensor_catalog[sensor_info[i].catalog_index].type) {
281                         case SENSOR_TYPE_ACCELEROMETER:
282                                 has_acc = 1;
283                                 break;
284                         case SENSOR_TYPE_GYROSCOPE:
285                                 has_gyr = 1;
286                                 break;
287                         case SENSOR_TYPE_MAGNETIC_FIELD:
288                                 has_mag = 1;
289                                 break;
290                         case SENSOR_TYPE_ORIENTATION:
291                                 has_ori = 1;
292                                 break;
293                         case SENSOR_TYPE_ROTATION_VECTOR:
294                                 has_rot = 1;
295                                 break;
296                 }
297
298         if (has_acc && has_gyr && has_mag && !has_rot && !has_ori)
299                 for (i=0; i<catalog_size; i++)
300                         if (sensor_catalog[i].type == SENSOR_TYPE_ORIENTATION) {
301                                 ALOGI("Adding placeholder orientation sensor");
302                                 add_sensor(0, i, 1);
303                                 break;
304                         }
305 }
306
307
308 void enumerate_sensors (void)
309 {
310         /*
311          * Discover supported sensors and allocate control structures for them.
312          * Multiple sensors can potentially rely on a single iio device (each
313          * using their own channels). We can't have multiple sensors of the same
314          * type on the same device. In case of detection as both a poll-mode
315          * and trigger-based sensor, use the trigger usage mode.
316          */
317         char poll_sensors[CATALOG_SIZE];
318         char trig_sensors[CATALOG_SIZE];
319         int dev_num;
320         unsigned int i;
321         int trig_found;
322
323         for (dev_num=0; dev_num<MAX_DEVICES; dev_num++) {
324                 trig_found = 0;
325
326                 discover_poll_sensors(dev_num, poll_sensors);
327                 discover_trig_sensors(dev_num, trig_sensors);
328
329                 for (i=0; i<CATALOG_SIZE; i++)
330                         if (trig_sensors[i]) {
331                                 add_sensor(dev_num, i, 0);
332                                 trig_found = 1;
333                         }
334                         else
335                                 if (poll_sensors[i])
336                                         add_sensor(dev_num, i, 1);
337
338                 if (trig_found)
339                         build_sensor_report_maps(dev_num);
340         }
341
342         ALOGI("Discovered %d sensors\n", sensor_count);
343
344         /* Make sure Android fall backs to its own orientation sensor */
345         orientation_sensor_check();
346 }
347
348
349 void delete_enumeration_data (void)
350 {
351         /* Reset sensor count */
352         sensor_count = 0;
353 }
354
355
356 int get_sensors_list(   struct sensors_module_t* module,
357                         struct sensor_t const** list)
358 {
359         *list = sensor_desc;
360         return sensor_count;
361 }
362