OSDN Git Service

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