OSDN Git Service

Merge remote-tracking branch 'origin/abt/topic/gmin/l-dev/sensors/master' into gmin...
[android-x86/hardware-intel-libsensors.git] / enumeration.c
1 /*
2  * Copyright (C) 2014 Intel Corporation.
3  */
4
5 #include <ctype.h>
6 #include <dirent.h>
7 #include <stdlib.h>
8 #include <utils/Log.h>
9 #include <hardware/sensors.h>
10 #include "enumeration.h"
11 #include "description.h"
12 #include "utils.h"
13 #include "transform.h"
14 #include "description.h"
15 #include "control.h"
16 #include "calibration.h"
17
18 /*
19  * This table maps syfs entries in scan_elements directories to sensor types,
20  * and will also be used to determine other sysfs names as well as the iio
21  * device number associated to a specific sensor.
22  */
23
24  /*
25   * We duplicate entries for the uncalibrated types after their respective base
26   * sensor. This is because all sensor entries must have an associated catalog entry
27   * and also because when only the uncal sensor is active it needs to take it's data
28   * from the same iio device as the base one.
29   */
30
31 struct sensor_catalog_entry_t sensor_catalog[] = {
32         DECLARE_SENSOR3("accel",      SENSOR_TYPE_ACCELEROMETER,  "x", "y", "z")
33         DECLARE_SENSOR3("anglvel",    SENSOR_TYPE_GYROSCOPE,      "x", "y", "z")
34         DECLARE_SENSOR3("magn",       SENSOR_TYPE_MAGNETIC_FIELD, "x", "y", "z")
35         DECLARE_SENSOR1("intensity",  SENSOR_TYPE_LIGHT,          "both"       )
36         DECLARE_SENSOR0("illuminance",SENSOR_TYPE_LIGHT                        )
37         DECLARE_SENSOR3("incli",      SENSOR_TYPE_ORIENTATION,    "x", "y", "z")
38         DECLARE_SENSOR4("rot",        SENSOR_TYPE_ROTATION_VECTOR,
39                                          "quat_x", "quat_y", "quat_z", "quat_w")
40         DECLARE_SENSOR0("temp",       SENSOR_TYPE_AMBIENT_TEMPERATURE          )
41         DECLARE_SENSOR0("proximity",  SENSOR_TYPE_PROXIMITY                    )
42         DECLARE_SENSOR3("anglvel",      SENSOR_TYPE_GYROSCOPE_UNCALIBRATED, "x", "y", "z")
43 };
44
45 #define CATALOG_SIZE    ARRAY_SIZE(sensor_catalog)
46
47
48 /* We equate sensor handles to indices in these tables */
49
50 struct sensor_t      sensor_desc[MAX_SENSORS];  /* Android-level descriptors */
51 struct sensor_info_t sensor_info[MAX_SENSORS];  /* Internal descriptors      */
52 int sensor_count;                               /* Detected sensors          */
53
54
55 static void add_sensor (int dev_num, int catalog_index, int use_polling)
56 {
57         int s;
58         int sensor_type;
59         int retval;
60         char sysfs_path[PATH_MAX];
61         const char* prefix;
62         float scale;
63         int c;
64         float opt_scale;
65         const char* ch_name;
66         int num_channels;
67         char suffix[MAX_NAME_SIZE + 8];
68
69         if (sensor_count == MAX_SENSORS) {
70                 ALOGE("Too many sensors!\n");
71                 return;
72         }
73
74         sensor_type = sensor_catalog[catalog_index].type;
75
76         /*
77          * At this point we could check that the expected sysfs attributes are
78          * present ; that would enable having multiple catalog entries with the
79          * same sensor type, accomodating different sets of sysfs attributes.
80          */
81
82         s = sensor_count;
83
84         sensor_info[s].dev_num          = dev_num;
85         sensor_info[s].catalog_index    = catalog_index;
86
87         if (use_polling)
88                 sensor_info[s].num_channels = 0;
89         else
90                 sensor_info[s].num_channels =
91                                 sensor_catalog[catalog_index].num_channels;
92
93         prefix = sensor_catalog[catalog_index].tag;
94
95         /*
96          * receiving the illumination sensor calibration inputs from
97          * the Android properties and setting it within sysfs
98          */
99         if (sensor_catalog[catalog_index].type == SENSOR_TYPE_LIGHT) {
100                 retval = sensor_get_illumincalib(s);
101                 if (retval > 0) {
102                         sprintf(sysfs_path, ILLUMINATION_CALIBPATH, dev_num);
103                         sysfs_write_int(sysfs_path, retval);
104                 }
105         }
106
107         /* Read name attribute, if available */
108         sprintf(sysfs_path, NAME_PATH, dev_num);
109         sysfs_read_str(sysfs_path, sensor_info[s].internal_name, MAX_NAME_SIZE);
110
111         /* See if we have general offsets and scale values for this sensor */
112
113         sprintf(sysfs_path, SENSOR_OFFSET_PATH, dev_num, prefix);
114         sysfs_read_float(sysfs_path, &sensor_info[s].offset);
115
116         sprintf(sysfs_path, SENSOR_SCALE_PATH, dev_num, prefix);
117         if (!sysfs_read_float(sysfs_path, &scale)) {
118                 sensor_info[s].scale = scale;
119                 ALOGI("Scale path:%s scale:%f dev_num:%d\n",
120                                         sysfs_path, scale, dev_num);
121         } else {
122                 sensor_info[s].scale = 1;
123
124                 /* Read channel specific scale if any*/
125                 for (c = 0; c < sensor_catalog[catalog_index].num_channels; c++)
126                 {
127                         sprintf(sysfs_path, BASE_PATH "%s", dev_num,
128                            sensor_catalog[catalog_index].channel[c].scale_path);
129
130                         if (!sysfs_read_float(sysfs_path, &scale)) {
131                                 sensor_info[s].channel[c].scale = scale;
132                                 sensor_info[s].scale = 0;
133
134                                 ALOGI(  "Scale path:%s "
135                                         "channel scale:%f dev_num:%d\n",
136                                         sysfs_path, scale, dev_num);
137                         }
138                 }
139         }
140
141         /*
142          * See if we have optional correction scaling factors for each of the
143          * channels of this sensor. These would be expressed using properties
144          * like iio.accel.y.opt_scale = -1. In case of a single channel we also
145          * support things such as iio.temp.opt_scale = -1. Note that this works
146          * for all types of sensors, and whatever transform is selected, on top
147          * of any previous conversions.
148          */
149         num_channels = sensor_catalog[catalog_index].num_channels;
150
151         if (num_channels) {
152                 for (c = 0; c < num_channels; c++) {
153                         opt_scale = 1;
154
155                         ch_name = sensor_catalog[catalog_index].channel[c].name;
156                         sprintf(suffix, "%s.opt_scale", ch_name);
157                         sensor_get_fl_prop(s, suffix, &opt_scale);
158
159                         sensor_info[s].channel[c].opt_scale = opt_scale;
160                 }
161         } else {
162                 opt_scale = 1;
163                 sensor_get_fl_prop(s, "opt_scale", &opt_scale);
164                 sensor_info[s].channel[0].opt_scale = opt_scale;
165         }
166
167         /* Initialize Android-visible descriptor */
168         sensor_desc[s].name             = sensor_get_name(s);
169         sensor_desc[s].vendor           = sensor_get_vendor(s);
170         sensor_desc[s].version          = sensor_get_version(s);
171         sensor_desc[s].handle           = s;
172         sensor_desc[s].type             = sensor_type;
173         sensor_desc[s].maxRange         = sensor_get_max_range(s);
174         sensor_desc[s].resolution       = sensor_get_resolution(s);
175         sensor_desc[s].power            = sensor_get_power(s);
176         sensor_desc[s].stringType = sensor_get_string_type(s);
177
178         /* None of our supported sensors requires a special permission.
179         *  If this will be the case we should implement a sensor_get_perm
180         */
181         sensor_desc[s].requiredPermission = "";
182         sensor_desc[s].flags = sensor_get_flags(s);
183         sensor_desc[s].maxDelay = sensor_get_max_delay(s);
184
185         if (sensor_info[s].internal_name[0] == '\0') {
186                 /*
187                  * In case the kernel-mode driver doesn't expose a name for
188                  * the iio device, use (null)-dev%d as the trigger name...
189                  * This can be considered a kernel-mode iio driver bug.
190                  */
191                 ALOGW("Using null trigger on sensor %d (dev %d)\n", s, dev_num);
192                 strcpy(sensor_info[s].internal_name, "(null)");
193         }
194
195         if (sensor_catalog[catalog_index].type == SENSOR_TYPE_GYROSCOPE ||
196                 sensor_catalog[catalog_index].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED) {
197                 struct gyro_cal* calibration_data = calloc(1, sizeof(struct gyro_cal));
198                 sensor_info[s].cal_data = calibration_data;
199         }
200
201         if (sensor_catalog[catalog_index].type == SENSOR_TYPE_MAGNETIC_FIELD) {
202                 struct compass_cal* calibration_data = calloc(1, sizeof(struct compass_cal));
203                 sensor_info[s].cal_data = calibration_data;
204         }
205
206         /* Select one of the available sensor sample processing styles */
207         select_transform(s);
208
209         /* Initialize fields related to sysfs reads offloading */
210         sensor_info[s].thread_data_fd[0]  = -1;
211         sensor_info[s].thread_data_fd[1]  = -1;
212         sensor_info[s].acquisition_thread = -1;
213
214         /* Check if we have a special ordering property on this sensor */
215         if (sensor_get_order(s, sensor_info[s].order))
216                 sensor_info[s].quirks |= QUIRK_FIELD_ORDERING;
217
218         sensor_count++;
219 }
220
221
222 static void discover_poll_sensors (int dev_num, char map[CATALOG_SIZE])
223 {
224         char base_dir[PATH_MAX];
225         DIR *dir;
226         struct dirent *d;
227         unsigned int i;
228         int c;
229
230         memset(map, 0, CATALOG_SIZE);
231
232         snprintf(base_dir, sizeof(base_dir), BASE_PATH, dev_num);
233
234         dir = opendir(base_dir);
235         if (!dir) {
236                 return;
237         }
238
239         /* Enumerate entries in this iio device's base folder */
240
241         while ((d = readdir(dir))) {
242                 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
243                         continue;
244
245                 /* If the name matches a catalog entry, flag it */
246                 for (i = 0; i<CATALOG_SIZE; i++) {
247                 /* This will be added separately later */
248                 if (sensor_catalog[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED)
249                         continue;
250                 for (c=0; c<sensor_catalog[i].num_channels; c++)
251                         if (!strcmp(d->d_name,sensor_catalog[i].channel[c].raw_path) ||
252                                 !strcmp(d->d_name, sensor_catalog[i].channel[c].input_path)) {
253                                         map[i] = 1;
254                                         break;
255                         }
256                 }
257         }
258
259         closedir(dir);
260 }
261
262
263 static void discover_trig_sensors (int dev_num, char map[CATALOG_SIZE])
264 {
265         char scan_elem_dir[PATH_MAX];
266         DIR *dir;
267         struct dirent *d;
268         unsigned int i;
269
270         memset(map, 0, CATALOG_SIZE);
271
272         /* Enumerate entries in this iio device's scan_elements folder */
273
274         snprintf(scan_elem_dir, sizeof(scan_elem_dir), CHANNEL_PATH, dev_num);
275
276         dir = opendir(scan_elem_dir);
277         if (!dir) {
278                 return;
279         }
280
281         while ((d = readdir(dir))) {
282                 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
283                         continue;
284
285                 /* Compare en entry to known ones and create matching sensors */
286
287                 for (i = 0; i<CATALOG_SIZE; i++) {
288                         if (sensor_catalog[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED)
289                                 continue;
290                         if (!strcmp(d->d_name,
291                                         sensor_catalog[i].channel[0].en_path)) {
292                                         map[i] = 1;
293                                         break;
294                         }
295                 }
296         }
297
298         closedir(dir);
299 }
300
301
302 static void orientation_sensor_check(void)
303 {
304         /*
305          * If we have accel + gyro + magn but no rotation vector sensor,
306          * SensorService replaces the HAL provided orientation sensor by the
307          * AOSP version... provided we report one. So initialize a virtual
308          * orientation sensor with zero values, which will get replaced. See:
309          * frameworks/native/services/sensorservice/SensorService.cpp, looking
310          * for SENSOR_TYPE_ROTATION_VECTOR; that code should presumably fall
311          * back to mUserSensorList.add instead of replaceAt, but accommodate it.
312          */
313
314         int i;
315         int has_acc = 0;
316         int has_gyr = 0;
317         int has_mag = 0;
318         int has_rot = 0;
319         int has_ori = 0;
320         int catalog_size = CATALOG_SIZE;
321
322         for (i=0; i<sensor_count; i++)
323                 switch (sensor_catalog[sensor_info[i].catalog_index].type) {
324                         case SENSOR_TYPE_ACCELEROMETER:
325                                 has_acc = 1;
326                                 break;
327                         case SENSOR_TYPE_GYROSCOPE:
328                                 has_gyr = 1;
329                                 break;
330                         case SENSOR_TYPE_MAGNETIC_FIELD:
331                                 has_mag = 1;
332                                 break;
333                         case SENSOR_TYPE_ORIENTATION:
334                                 has_ori = 1;
335                                 break;
336                         case SENSOR_TYPE_ROTATION_VECTOR:
337                                 has_rot = 1;
338                                 break;
339                 }
340
341         if (has_acc && has_gyr && has_mag && !has_rot && !has_ori)
342                 for (i=0; i<catalog_size; i++)
343                         if (sensor_catalog[i].type == SENSOR_TYPE_ORIENTATION) {
344                                 ALOGI("Adding placeholder orientation sensor");
345                                 add_sensor(0, i, 1);
346                                 break;
347                         }
348 }
349
350 static void uncalibrated_gyro_check (void)
351 {
352         unsigned int has_gyr = 0;
353         unsigned int dev_num;
354         int i, c;
355         unsigned int is_poll_sensor;
356
357         int cal_idx = 0;
358         int uncal_idx = 0;
359
360         /* Checking to see if we have a gyroscope - we can only have uncal if we have the base sensor */
361         for (i=0; i < sensor_count; i++)
362                 if(sensor_catalog[sensor_info[i].catalog_index].type == SENSOR_TYPE_GYROSCOPE)
363                 {
364                         has_gyr=1;
365                         dev_num = sensor_info[i].dev_num;
366                         is_poll_sensor = !sensor_info[i].num_channels;
367                         cal_idx = i;
368                         break;
369                 }
370
371         /*
372          * If we have a gyro we can add the uncalibrated sensor of the same type and
373          * on the same dev_num. We will save indexes for easy finding and also save the
374          * channel specific information.
375          */
376         if (has_gyr)
377                 for (i=0; i<CATALOG_SIZE; i++)
378                         if (sensor_catalog[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED) {
379                                 add_sensor(dev_num, i, is_poll_sensor);
380
381                                 uncal_idx = sensor_count - 1; /* Just added uncalibrated sensor */
382
383                                 /* Similar to build_sensor_report_maps */
384                                 for (c = 0; c < sensor_info[uncal_idx].num_channels; c++)
385                                 {
386                                         memcpy( &(sensor_info[uncal_idx].channel[c].type_spec),
387                                                 &(sensor_info[cal_idx].channel[c].type_spec),
388                                                 sizeof(sensor_info[uncal_idx].channel[c].type_spec));
389                                         sensor_info[uncal_idx].channel[c].type_info = sensor_info[cal_idx].channel[c].type_info;
390                                         sensor_info[uncal_idx].channel[c].offset    = sensor_info[cal_idx].channel[c].offset;
391                                         sensor_info[uncal_idx].channel[c].size      = sensor_info[cal_idx].channel[c].size;
392                                 }
393                                 strncpy(sensor_info[uncal_idx].trigger_name,
394                                         sensor_info[cal_idx].trigger_name,
395                                         MAX_NAME_SIZE);
396                                 sensor_info[uncal_idx].pair_idx = cal_idx;
397                                 sensor_info[cal_idx].pair_idx = uncal_idx;
398                                 break;
399                         }
400 }
401
402 static int is_continuous (int s)
403 {
404         /* Is sensor s of the continous trigger type kind? */
405
406         int catalog_index = sensor_info[s].catalog_index;
407         int sensor_type = sensor_catalog[catalog_index].type;
408
409         switch (sensor_type) {
410                 case SENSOR_TYPE_ACCELEROMETER:
411                 case SENSOR_TYPE_MAGNETIC_FIELD:
412                 case SENSOR_TYPE_ORIENTATION:
413                 case SENSOR_TYPE_GYROSCOPE:
414                 case SENSOR_TYPE_PRESSURE:
415                 case SENSOR_TYPE_GRAVITY:
416                 case SENSOR_TYPE_LINEAR_ACCELERATION:
417                 case SENSOR_TYPE_ROTATION_VECTOR:
418                 case SENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED:
419                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
420                 case SENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR:
421                         return 1;
422
423                 default:
424                         return 0;
425         }
426 }
427
428
429 static void propose_new_trigger (int s, char trigger_name[MAX_NAME_SIZE],
430                                  int sensor_name_len)
431 {
432         /*
433          * A new trigger has been enumerated for this sensor. Check if it makes
434          * sense to use it over the currently selected one, and select it if it
435          * is so. The format is something like sensor_name-dev0.
436          */
437
438         const char *suffix = trigger_name + sensor_name_len + 1;
439
440         /* dev is the default, and lowest priority; no need to update */
441         if (!memcmp(suffix, "dev", 3))
442                 return;
443
444         /*
445          * Anything else is higher priority. However if we already found an
446          * any-motion trigger, don't select anything else.
447          */
448
449         if (!memcmp(sensor_info[s].trigger_name + sensor_name_len + 1,
450                     "any-motion-", 11))
451                 return;
452
453         /*
454          * If we're switching to an any-motion trigger, force the sensor to
455          * automatic intermediate event generation mode, at least if it is of a
456          * continuously firing sensor type.
457          */
458
459         if (!memcmp(suffix, "any-motion-", 11) && is_continuous(s))
460                 sensor_info[s].quirks |= QUIRK_TERSE_DRIVER;
461
462         /* Update the trigger name to use for this sensor */
463         strcpy(sensor_info[s].trigger_name, trigger_name);
464 }
465
466
467 static void update_sensor_matching_trigger_name (char name[MAX_NAME_SIZE])
468 {
469         /*
470          * Check if we have a sensor matching the specified trigger name,
471          * which should then begin with the sensor name, and end with a number
472          * equal to the iio device number the sensor is associated to. If so,
473          * update the string we're going to write to trigger/current_trigger
474          * when enabling this sensor.
475          */
476
477         int s;
478         int dev_num;
479         int len;
480         char* cursor;
481         int sensor_name_len;
482
483         /*
484          * First determine the iio device number this trigger refers to. We
485          * expect the last few characters (typically one) of the trigger name
486          * to be this number, so perform a few checks.
487          */
488         len = strnlen(name, MAX_NAME_SIZE);
489
490         if (len < 2)
491                 return;
492
493         cursor = name + len - 1;
494
495         if (!isdigit(*cursor))
496                 return;
497
498         while (len && isdigit(*cursor)) {
499                 len--;
500                 cursor--;
501         }
502
503         dev_num = atoi(cursor+1);
504
505         /* See if that matches a sensor */
506         for (s=0; s<sensor_count; s++)
507                 if (sensor_info[s].dev_num == dev_num) {
508
509                         sensor_name_len = strlen(sensor_info[s].internal_name);
510
511                         if (!strncmp(name,
512                                      sensor_info[s].internal_name,
513                                      sensor_name_len))
514                                 /* Switch to new trigger if appropriate */
515                                 propose_new_trigger(s, name, sensor_name_len);
516                 }
517 }
518
519
520 static void setup_trigger_names (void)
521 {
522         char filename[PATH_MAX];
523         char buf[MAX_NAME_SIZE];
524         int len;
525         int s;
526         int trigger;
527         int ret;
528
529         /* By default, use the name-dev convention that most drivers use */
530         for (s=0; s<sensor_count; s++)
531                 snprintf(sensor_info[s].trigger_name, MAX_NAME_SIZE, "%s-dev%d",
532                         sensor_info[s].internal_name, sensor_info[s].dev_num);
533
534         /* Now have a look to /sys/bus/iio/devices/triggerX entries */
535
536         for (trigger=0; trigger<MAX_TRIGGERS; trigger++) {
537
538                 snprintf(filename, sizeof(filename), TRIGGER_FILE_PATH,trigger);
539
540                 ret = sysfs_read_str(filename, buf, sizeof(buf));
541
542                 if (ret < 0)
543                         break;
544
545                 update_sensor_matching_trigger_name(buf);
546         }
547
548         for (s=0; s<sensor_count; s++)
549                 if (sensor_info[s].num_channels) {
550                         ALOGI(  "Sensor %d (%s) using iio trigger %s\n", s,
551                                 sensor_info[s].friendly_name,
552                                 sensor_info[s].trigger_name);
553                 }
554 }
555
556
557 void enumerate_sensors (void)
558 {
559         /*
560          * Discover supported sensors and allocate control structures for them.
561          * Multiple sensors can potentially rely on a single iio device (each
562          * using their own channels). We can't have multiple sensors of the same
563          * type on the same device. In case of detection as both a poll-mode
564          * and trigger-based sensor, use the trigger usage mode.
565          */
566         char poll_sensors[CATALOG_SIZE];
567         char trig_sensors[CATALOG_SIZE];
568         int dev_num;
569         unsigned int i;
570         int trig_found;
571
572         for (dev_num=0; dev_num<MAX_DEVICES; dev_num++) {
573                 trig_found = 0;
574
575                 discover_poll_sensors(dev_num, poll_sensors);
576                 discover_trig_sensors(dev_num, trig_sensors);
577
578                 for (i=0; i<CATALOG_SIZE; i++)
579                         if (trig_sensors[i]) {
580                                 add_sensor(dev_num, i, 0);
581                                 trig_found = 1;
582                         }
583                         else
584                                 if (poll_sensors[i])
585                                         add_sensor(dev_num, i, 1);
586
587                 if (trig_found) {
588                         build_sensor_report_maps(dev_num);
589                 }
590         }
591
592         ALOGI("Discovered %d sensors\n", sensor_count);
593
594         /* Set up default - as well as custom - trigger names */
595         setup_trigger_names();
596
597         /* Make sure Android fall backs to its own orientation sensor */
598         orientation_sensor_check();
599
600         /* Create the uncalibrated counterpart to the compensated gyroscope;
601          * This is is a new sensor type in Android 4.4 */
602         uncalibrated_gyro_check();
603 }
604
605
606 void delete_enumeration_data (void)
607 {
608
609         int i;
610         for (i = 0; i < sensor_count; i++)
611         switch (sensor_catalog[sensor_info[i].catalog_index].type) {
612                 case SENSOR_TYPE_MAGNETIC_FIELD:
613                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
614                 case SENSOR_TYPE_GYROSCOPE:
615                         if (sensor_info[i].cal_data != NULL) {
616                                 free(sensor_info[i].cal_data);
617                                 sensor_info[i].cal_data = NULL;
618                                 sensor_info[i].cal_level = 0;
619                         }
620                         break;
621                 default:
622                         break;
623         }
624         /* Reset sensor count */
625         sensor_count = 0;
626 }
627
628
629 int get_sensors_list(   struct sensors_module_t* module,
630                         struct sensor_t const** list)
631 {
632         *list = sensor_desc;
633         return sensor_count;
634 }
635