OSDN Git Service

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