OSDN Git Service

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