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, "opt_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
182         if (sensor_catalog[catalog_index].type == SENSOR_TYPE_MAGNETIC_FIELD) {
183                 struct compass_cal* calibration_data = calloc(1, sizeof(struct compass_cal));
184                 sensor_info[s].cal_data = calibration_data;
185         }
186         /* Select one of the available sensor sample processing styles */
187         select_transform(s);
188
189         sensor_count++;
190 }
191
192
193 static void discover_poll_sensors (int dev_num, char map[CATALOG_SIZE])
194 {
195         char base_dir[PATH_MAX];
196         DIR *dir;
197         char sysfs_dir[PATH_MAX];
198         struct sensor *sensor;
199         struct dirent *d;
200         unsigned int i;
201         int c;
202
203         memset(map, 0, CATALOG_SIZE);
204
205         snprintf(base_dir, sizeof(base_dir), BASE_PATH, dev_num);
206
207         dir = opendir(base_dir);
208         if (!dir) {
209                return;
210         }
211
212         /* Enumerate entries in this iio device's base folder */
213
214         while ((d = readdir(dir))) {
215                 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
216                         continue;
217
218                 /* If the name matches a catalog entry, flag it */
219                 for (i = 0; i<CATALOG_SIZE; i++)
220                         for (c=0; c<sensor_catalog[i].num_channels; c++)
221                                 if (!strcmp(d->d_name,
222                                     sensor_catalog[i].channel[c].raw_path) ||
223                                     !strcmp(d->d_name,
224                                     sensor_catalog[i].channel[c].input_path)) {
225                                 map[i] = 1;
226                                 break;
227                         }
228         }
229
230         closedir(dir);
231 }
232
233
234 static void discover_trig_sensors (int dev_num, char map[CATALOG_SIZE])
235 {
236         char scan_elem_dir[PATH_MAX];
237         DIR *dir;
238         char sysfs_dir[PATH_MAX];
239         struct sensor *sensor;
240         struct dirent *d;
241         unsigned int i;
242
243         memset(map, 0, CATALOG_SIZE);
244
245         /* Enumerate entries in this iio device's scan_elements folder */
246
247         snprintf(scan_elem_dir, sizeof(scan_elem_dir), CHANNEL_PATH, dev_num);
248
249         dir = opendir(scan_elem_dir);
250         if (!dir) {
251                return;
252         }
253
254         while ((d = readdir(dir))) {
255                 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
256                         continue;
257
258                 /* Compare en entry to known ones and create matching sensors */
259
260                 for (i = 0; i<CATALOG_SIZE; i++)
261                         if (!strcmp(d->d_name,
262                                     sensor_catalog[i].channel[0].en_path)) {
263                                 map[i] = 1;
264                                 break;
265                         }
266         }
267
268         closedir(dir);
269 }
270
271
272 static void orientation_sensor_check(void)
273 {
274         /*
275          * If we have accel + gyro + magn but no rotation vector sensor,
276          * SensorService replaces the HAL provided orientation sensor by the
277          * AOSP version... provided we report one. So initialize a virtual
278          * orientation sensor with zero values, which will get replaced. See:
279          * frameworks/native/services/sensorservice/SensorService.cpp, looking
280          * for SENSOR_TYPE_ROTATION_VECTOR; that code should presumably fall
281          * back to mUserSensorList.add instead of replaceAt, but accommodate it.
282          */
283
284         int i;
285         int has_acc = 0;
286         int has_gyr = 0;
287         int has_mag = 0;
288         int has_rot = 0;
289         int has_ori = 0;
290         int catalog_size = CATALOG_SIZE;
291
292         for (i=0; i<sensor_count; i++)
293                 switch (sensor_catalog[sensor_info[i].catalog_index].type) {
294                         case SENSOR_TYPE_ACCELEROMETER:
295                                 has_acc = 1;
296                                 break;
297                         case SENSOR_TYPE_GYROSCOPE:
298                                 has_gyr = 1;
299                                 break;
300                         case SENSOR_TYPE_MAGNETIC_FIELD:
301                                 has_mag = 1;
302                                 break;
303                         case SENSOR_TYPE_ORIENTATION:
304                                 has_ori = 1;
305                                 break;
306                         case SENSOR_TYPE_ROTATION_VECTOR:
307                                 has_rot = 1;
308                                 break;
309                 }
310
311         if (has_acc && has_gyr && has_mag && !has_rot && !has_ori)
312                 for (i=0; i<catalog_size; i++)
313                         if (sensor_catalog[i].type == SENSOR_TYPE_ORIENTATION) {
314                                 ALOGI("Adding placeholder orientation sensor");
315                                 add_sensor(0, i, 1);
316                                 break;
317                         }
318 }
319
320
321 void enumerate_sensors (void)
322 {
323         /*
324          * Discover supported sensors and allocate control structures for them.
325          * Multiple sensors can potentially rely on a single iio device (each
326          * using their own channels). We can't have multiple sensors of the same
327          * type on the same device. In case of detection as both a poll-mode
328          * and trigger-based sensor, use the trigger usage mode.
329          */
330         char poll_sensors[CATALOG_SIZE];
331         char trig_sensors[CATALOG_SIZE];
332         int dev_num;
333         unsigned int i;
334         int trig_found;
335
336         for (dev_num=0; dev_num<MAX_DEVICES; dev_num++) {
337                 trig_found = 0;
338
339                 discover_poll_sensors(dev_num, poll_sensors);
340                 discover_trig_sensors(dev_num, trig_sensors);
341
342                 for (i=0; i<CATALOG_SIZE; i++)
343                         if (trig_sensors[i]) {
344                                 add_sensor(dev_num, i, 0);
345                                 trig_found = 1;
346                         }
347                         else
348                                 if (poll_sensors[i])
349                                         add_sensor(dev_num, i, 1);
350
351                 if (trig_found)
352                         build_sensor_report_maps(dev_num);
353         }
354
355         ALOGI("Discovered %d sensors\n", sensor_count);
356
357         /* Make sure Android fall backs to its own orientation sensor */
358         orientation_sensor_check();
359 }
360
361
362 void delete_enumeration_data (void)
363 {
364
365         int i;
366         for (i = 0; i < sensor_count; i++)
367         switch (sensor_catalog[sensor_info[i].catalog_index].type) {
368                 case SENSOR_TYPE_MAGNETIC_FIELD:
369                 case SENSOR_TYPE_GYROSCOPE:
370                         if (sensor_info[i].cal_data != NULL) {
371                                 free(sensor_info[i].cal_data);
372                                 sensor_info[i].cal_data = NULL;
373                                 sensor_info[i].calibrated = 0;
374                         }
375                         break;
376                 default:
377                         break;
378         }
379         /* Reset sensor count */
380         sensor_count = 0;
381 }
382
383
384 int get_sensors_list(   struct sensors_module_t* module,
385                         struct sensor_t const** list)
386 {
387         *list = sensor_desc;
388         return sensor_count;
389 }
390