OSDN Git Service

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