OSDN Git Service

Populate L specific sensor_t fields
[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
184         if (sensor_info[s].internal_name[0] == '\0') {
185                 /*
186                  * In case the kernel-mode driver doesn't expose a name for
187                  * the iio device, use (null)-dev%d as the trigger name...
188                  * This can be considered a kernel-mode iio driver bug.
189                  */
190                 ALOGW("Using null trigger on sensor %d (dev %d)\n", s, dev_num);
191                 strcpy(sensor_info[s].internal_name, "(null)");
192         }
193
194         if (sensor_catalog[catalog_index].type == SENSOR_TYPE_GYROSCOPE ||
195                 sensor_catalog[catalog_index].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED) {
196                 struct gyro_cal* calibration_data = calloc(1, sizeof(struct gyro_cal));
197                 sensor_info[s].cal_data = calibration_data;
198         }
199
200         if (sensor_catalog[catalog_index].type == SENSOR_TYPE_MAGNETIC_FIELD) {
201                 struct compass_cal* calibration_data = calloc(1, sizeof(struct compass_cal));
202                 sensor_info[s].cal_data = calibration_data;
203         }
204
205         /* Select one of the available sensor sample processing styles */
206         select_transform(s);
207
208         /* Initialize fields related to sysfs reads offloading */
209         sensor_info[s].thread_data_fd[0]  = -1;
210         sensor_info[s].thread_data_fd[1]  = -1;
211         sensor_info[s].acquisition_thread = -1;
212
213         /* Check if we have a special ordering property on this sensor */
214         if (sensor_get_order(s, sensor_info[s].order))
215                 sensor_info[s].flags |= FLAG_FIELD_ORDERING;
216
217         sensor_count++;
218 }
219
220
221 static void discover_poll_sensors (int dev_num, char map[CATALOG_SIZE])
222 {
223         char base_dir[PATH_MAX];
224         DIR *dir;
225         struct dirent *d;
226         unsigned int i;
227         int c;
228
229         memset(map, 0, CATALOG_SIZE);
230
231         snprintf(base_dir, sizeof(base_dir), BASE_PATH, dev_num);
232
233         dir = opendir(base_dir);
234         if (!dir) {
235                 return;
236         }
237
238         /* Enumerate entries in this iio device's base folder */
239
240         while ((d = readdir(dir))) {
241                 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
242                         continue;
243
244                 /* If the name matches a catalog entry, flag it */
245                 for (i = 0; i<CATALOG_SIZE; i++) {
246                 /* This will be added separately later */
247                 if (sensor_catalog[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED)
248                         continue;
249                 for (c=0; c<sensor_catalog[i].num_channels; c++)
250                         if (!strcmp(d->d_name,sensor_catalog[i].channel[c].raw_path) ||
251                                 !strcmp(d->d_name, sensor_catalog[i].channel[c].input_path)) {
252                                         map[i] = 1;
253                                         break;
254                         }
255                 }
256         }
257
258         closedir(dir);
259 }
260
261
262 static void discover_trig_sensors (int dev_num, char map[CATALOG_SIZE])
263 {
264         char scan_elem_dir[PATH_MAX];
265         DIR *dir;
266         struct dirent *d;
267         unsigned int i;
268
269         memset(map, 0, CATALOG_SIZE);
270
271         /* Enumerate entries in this iio device's scan_elements folder */
272
273         snprintf(scan_elem_dir, sizeof(scan_elem_dir), CHANNEL_PATH, dev_num);
274
275         dir = opendir(scan_elem_dir);
276         if (!dir) {
277                 return;
278         }
279
280         while ((d = readdir(dir))) {
281                 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
282                         continue;
283
284                 /* Compare en entry to known ones and create matching sensors */
285
286                 for (i = 0; i<CATALOG_SIZE; i++) {
287                         if (sensor_catalog[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED)
288                                 continue;
289                         if (!strcmp(d->d_name,
290                                         sensor_catalog[i].channel[0].en_path)) {
291                                         map[i] = 1;
292                                         break;
293                         }
294                 }
295         }
296
297         closedir(dir);
298 }
299
300
301 static void orientation_sensor_check(void)
302 {
303         /*
304          * If we have accel + gyro + magn but no rotation vector sensor,
305          * SensorService replaces the HAL provided orientation sensor by the
306          * AOSP version... provided we report one. So initialize a virtual
307          * orientation sensor with zero values, which will get replaced. See:
308          * frameworks/native/services/sensorservice/SensorService.cpp, looking
309          * for SENSOR_TYPE_ROTATION_VECTOR; that code should presumably fall
310          * back to mUserSensorList.add instead of replaceAt, but accommodate it.
311          */
312
313         int i;
314         int has_acc = 0;
315         int has_gyr = 0;
316         int has_mag = 0;
317         int has_rot = 0;
318         int has_ori = 0;
319         int catalog_size = CATALOG_SIZE;
320
321         for (i=0; i<sensor_count; i++)
322                 switch (sensor_catalog[sensor_info[i].catalog_index].type) {
323                         case SENSOR_TYPE_ACCELEROMETER:
324                                 has_acc = 1;
325                                 break;
326                         case SENSOR_TYPE_GYROSCOPE:
327                                 has_gyr = 1;
328                                 break;
329                         case SENSOR_TYPE_MAGNETIC_FIELD:
330                                 has_mag = 1;
331                                 break;
332                         case SENSOR_TYPE_ORIENTATION:
333                                 has_ori = 1;
334                                 break;
335                         case SENSOR_TYPE_ROTATION_VECTOR:
336                                 has_rot = 1;
337                                 break;
338                 }
339
340         if (has_acc && has_gyr && has_mag && !has_rot && !has_ori)
341                 for (i=0; i<catalog_size; i++)
342                         if (sensor_catalog[i].type == SENSOR_TYPE_ORIENTATION) {
343                                 ALOGI("Adding placeholder orientation sensor");
344                                 add_sensor(0, i, 1);
345                                 break;
346                         }
347 }
348
349 static void uncalibrated_gyro_check (void)
350 {
351         unsigned int has_gyr = 0;
352         unsigned int dev_num;
353         int i, c;
354         unsigned int is_poll_sensor;
355
356         int cal_idx = 0;
357         int uncal_idx = 0;
358
359         /* Checking to see if we have a gyroscope - we can only have uncal if we have the base sensor */
360         for (i=0; i < sensor_count; i++)
361                 if(sensor_catalog[sensor_info[i].catalog_index].type == SENSOR_TYPE_GYROSCOPE)
362                 {
363                         has_gyr=1;
364                         dev_num = sensor_info[i].dev_num;
365                         is_poll_sensor = !sensor_info[i].num_channels;
366                         cal_idx = i;
367                         break;
368                 }
369
370         /*
371          * If we have a gyro we can add the uncalibrated sensor of the same type and
372          * on the same dev_num. We will save indexes for easy finding and also save the
373          * channel specific information.
374          */
375         if (has_gyr)
376                 for (i=0; i<CATALOG_SIZE; i++)
377                         if (sensor_catalog[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED) {
378                                 add_sensor(dev_num, i, is_poll_sensor);
379
380                                 uncal_idx = sensor_count - 1; /* Just added uncalibrated sensor */
381
382                                 /* Similar to build_sensor_report_maps */
383                                 for (c = 0; c < sensor_info[uncal_idx].num_channels; c++)
384                                 {
385                                         memcpy( &(sensor_info[uncal_idx].channel[c].type_spec),
386                                                 &(sensor_info[cal_idx].channel[c].type_spec),
387                                                 sizeof(sensor_info[uncal_idx].channel[c].type_spec));
388                                         sensor_info[uncal_idx].channel[c].type_info = sensor_info[cal_idx].channel[c].type_info;
389                                         sensor_info[uncal_idx].channel[c].offset    = sensor_info[cal_idx].channel[c].offset;
390                                         sensor_info[uncal_idx].channel[c].size      = sensor_info[cal_idx].channel[c].size;
391                                 }
392                                 sensor_info[uncal_idx].pair_idx = cal_idx;
393                                 sensor_info[cal_idx].pair_idx = uncal_idx;
394                                 break;
395                         }
396 }
397
398 static void update_sensor_matching_trigger_name (char name[MAX_NAME_SIZE])
399 {
400         /*
401          * Check if we have a sensor matching the specified trigger name,
402          * which should then begin with the sensor name, and end with a number
403          * equal to the iio device number the sensor is associated to. If so,
404          * update the string we're going to write to trigger/current_trigger
405          * when enabling this sensor.
406          */
407
408         int s;
409         int dev_num;
410         int len;
411         char* cursor;
412
413         /*
414          * First determine the iio device number this trigger refers to. We
415          * expect the last few characters (typically one) of the trigger name
416          * to be this number, so perform a few checks.
417          */
418         len = strnlen(name, MAX_NAME_SIZE);
419
420         if (len < 2)
421                 return;
422
423         cursor = name + len - 1;
424
425         if (!isdigit(*cursor))
426                 return;
427
428         while (len && isdigit(*cursor)) {
429                 len--;
430                 cursor--;
431         }
432
433         dev_num = atoi(cursor+1);
434
435         /* See if that matches a sensor */
436         for (s=0; s<sensor_count; s++)
437                 if (sensor_info[s].dev_num == dev_num &&
438                         !strncmp(name, sensor_info[s].internal_name,
439                                 strlen(sensor_info[s].internal_name))) {
440                                 /* Update sensor structure and return */
441                                 strcpy(sensor_info[s].trigger_name, name);
442                                 return;
443                         }
444 }
445
446
447 static void setup_trigger_names (void)
448 {
449         char filename[PATH_MAX];
450         char buf[MAX_NAME_SIZE];
451         int len;
452         int s;
453         int trigger;
454         int ret;
455
456         /* By default, use the name-dev convention that most drivers use */
457         for (s=0; s<sensor_count; s++)
458                 snprintf(sensor_info[s].trigger_name, MAX_NAME_SIZE, "%s-dev%d",
459                         sensor_info[s].internal_name, sensor_info[s].dev_num);
460
461         /* Now have a look to /sys/bus/iio/devices/triggerX entries */
462
463         for (trigger=0; trigger<MAX_TRIGGERS; trigger++) {
464
465                 snprintf(filename, sizeof(filename), TRIGGER_FILE_PATH,trigger);
466
467                 ret = sysfs_read_str(filename, buf, sizeof(buf));
468
469                 if (ret < 0)
470                         break;
471
472                 update_sensor_matching_trigger_name(buf);
473         }
474
475         for (s=0; s<sensor_count; s++)
476                 if (sensor_info[s].num_channels) {
477                         ALOGI(  "Sensor %d (%s) using iio trigger %s\n", s,
478                                 sensor_info[s].friendly_name,
479                                 sensor_info[s].trigger_name);
480                 }
481 }
482
483
484 void enumerate_sensors (void)
485 {
486         /*
487          * Discover supported sensors and allocate control structures for them.
488          * Multiple sensors can potentially rely on a single iio device (each
489          * using their own channels). We can't have multiple sensors of the same
490          * type on the same device. In case of detection as both a poll-mode
491          * and trigger-based sensor, use the trigger usage mode.
492          */
493         char poll_sensors[CATALOG_SIZE];
494         char trig_sensors[CATALOG_SIZE];
495         int dev_num;
496         unsigned int i;
497         int trig_found;
498
499         for (dev_num=0; dev_num<MAX_DEVICES; dev_num++) {
500                 trig_found = 0;
501
502                 discover_poll_sensors(dev_num, poll_sensors);
503                 discover_trig_sensors(dev_num, trig_sensors);
504
505                 for (i=0; i<CATALOG_SIZE; i++)
506                         if (trig_sensors[i]) {
507                                 add_sensor(dev_num, i, 0);
508                                 trig_found = 1;
509                         }
510                         else
511                                 if (poll_sensors[i])
512                                         add_sensor(dev_num, i, 1);
513
514                 if (trig_found) {
515                         build_sensor_report_maps(dev_num);
516                 }
517         }
518
519         ALOGI("Discovered %d sensors\n", sensor_count);
520
521         /* Set up default - as well as custom - trigger names */
522         setup_trigger_names();
523
524         /* Make sure Android fall backs to its own orientation sensor */
525         orientation_sensor_check();
526
527         /* Create the uncalibrated counterpart to the compensated gyroscope;
528          * This is is a new sensor type in Android 4.4 */
529         uncalibrated_gyro_check();
530 }
531
532
533 void delete_enumeration_data (void)
534 {
535
536         int i;
537         for (i = 0; i < sensor_count; i++)
538         switch (sensor_catalog[sensor_info[i].catalog_index].type) {
539                 case SENSOR_TYPE_MAGNETIC_FIELD:
540                 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
541                 case SENSOR_TYPE_GYROSCOPE:
542                         if (sensor_info[i].cal_data != NULL) {
543                                 free(sensor_info[i].cal_data);
544                                 sensor_info[i].cal_data = NULL;
545                                 sensor_info[i].calibrated = 0;
546                         }
547                         break;
548                 default:
549                         break;
550         }
551         /* Reset sensor count */
552         sensor_count = 0;
553 }
554
555
556 int get_sensors_list(   struct sensors_module_t* module,
557                         struct sensor_t const** list)
558 {
559         *list = sensor_desc;
560         return sensor_count;
561 }
562