OSDN Git Service

STPK-1429 Remove timestamp sensor definition
[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
12 /*
13  * This table maps syfs entries in scan_elements directories to sensor types,
14  * and will also be used to determine other sysfs names as well as the iio
15  * device number associated to a specific sensor.
16  */
17
18 struct sensor_catalog_entry_t sensor_catalog[] = {
19         DECLARE_SENSOR3("accel",      SENSOR_TYPE_ACCELEROMETER,  "x", "y", "z")
20         DECLARE_SENSOR3("anglvel",    SENSOR_TYPE_GYROSCOPE,      "x", "y", "z")
21         DECLARE_SENSOR3("magn",       SENSOR_TYPE_MAGNETIC_FIELD, "x", "y", "z")
22         DECLARE_SENSOR0("illuminance",SENSOR_TYPE_LIGHT                        )
23         DECLARE_SENSOR3("incli",      SENSOR_TYPE_ORIENTATION,    "x", "y", "z")
24         DECLARE_SENSOR4("rot",        SENSOR_TYPE_ROTATION_VECTOR,
25                                          "quat_x", "quat_y", "quat_z", "quat_w")
26         DECLARE_SENSOR0("temp",       SENSOR_TYPE_TEMPERATURE                  )
27 };
28
29 #define CATALOG_SIZE    ARRAY_SIZE(sensor_catalog)
30
31
32 /* We equate sensor handles to indices in these tables */
33
34 struct sensor_t      sensor_desc[MAX_SENSORS];  /* Android-level descriptors */
35 struct sensor_info_t sensor_info[MAX_SENSORS];  /* Internal descriptors      */
36 int sensor_count;                               /* Detected sensors          */
37
38
39 static void add_sensor (int dev_num, int catalog_index, int use_polling)
40 {
41         int s;
42         int sensor_type;
43         char sysfs_path[PATH_MAX];
44         const char* prefix;
45         float scale;
46
47         if (sensor_count == MAX_SENSORS) {
48                 ALOGE("Too many sensors!\n");
49                 return;
50         }
51
52         sensor_type = sensor_catalog[catalog_index].type;
53
54         /*
55          * At this point we could check that the expected sysfs attributes are
56          * present ; that would enable having multiple catalog entries with the
57          * same sensor type, accomodating different sets of sysfs attributes.
58          */
59
60         s = sensor_count;
61
62         sensor_info[s].dev_num          = dev_num;
63         sensor_info[s].catalog_index    = catalog_index;
64
65         if (use_polling)
66                 sensor_info[s].num_channels = 0;
67         else
68                 sensor_info[s].num_channels =
69                                 sensor_catalog[catalog_index].num_channels;
70
71         prefix = sensor_catalog[catalog_index].tag;
72
73         /* Read name attribute, if available */
74         sprintf(sysfs_path, NAME_PATH, dev_num);
75         sysfs_read_str(sysfs_path, sensor_info[s].internal_name, MAX_NAME_SIZE);
76
77         /* See if we have general offsets and scale values for this sensor */
78
79         sprintf(sysfs_path, COMMON_OFFSET_PATH, dev_num, prefix);
80         sysfs_read_float(sysfs_path, &sensor_info[s].offset);
81
82         sprintf(sysfs_path, COMMON_SCALE_PATH, dev_num, prefix);
83         if (!sysfs_read_float(sysfs_path, &scale))
84                 sensor_info[s].scale = scale;
85         else
86                 sensor_info[s].scale = 1;
87
88         /* Initialize Android-visible descriptor */
89         sensor_desc[s].name             = sensor_get_name(s);
90         sensor_desc[s].vendor           = sensor_get_vendor(s);
91         sensor_desc[s].version          = sensor_get_version(s);
92         sensor_desc[s].handle           = s;
93         sensor_desc[s].type             = sensor_type;
94         sensor_desc[s].maxRange         = sensor_get_max_range(s);
95         sensor_desc[s].resolution       = sensor_get_resolution(s);
96         sensor_desc[s].power            = sensor_get_power(s);
97
98         if (sensor_info[s].internal_name[0] == '\0') {
99                 /*
100                  * In case the kernel-mode driver doesn't expose a name for
101                  * the iio device, use (null)-dev%d as the trigger name...
102                  * This can be considered a kernel-mode iio driver bug.
103                  */
104                 ALOGW("Using null trigger on sensor %d (dev %d)\n", s, dev_num);
105                 strcpy(sensor_info[s].internal_name, "(null)");
106         }
107
108         sensor_count++;
109 }
110
111
112 static void discover_poll_sensors (int dev_num, char map[CATALOG_SIZE])
113 {
114         char base_dir[PATH_MAX];
115         DIR *dir;
116         char sysfs_dir[PATH_MAX];
117         struct sensor *sensor;
118         struct dirent *d;
119         unsigned int i;
120         int c;
121
122         memset(map, 0, CATALOG_SIZE);
123
124         snprintf(base_dir, sizeof(base_dir), BASE_PATH, dev_num);
125
126         dir = opendir(base_dir);
127         if (!dir) {
128                return;
129         }
130
131         /* Enumerate entries in this iio device's base folder */
132
133         while ((d = readdir(dir))) {
134                 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
135                         continue;
136
137                 /* If the name matches a catalog entry, flag it */
138                 for (i = 0; i<CATALOG_SIZE; i++)
139                         for (c=0; c<sensor_catalog[i].num_channels; c++)
140                                 if (!strcmp(d->d_name,
141                                     sensor_catalog[i].channel[c].raw_path) ||
142                                     !strcmp(d->d_name,
143                                     sensor_catalog[i].channel[c].input_path)) {
144                                 map[i] = 1;
145                                 break;
146                         }
147         }
148
149         closedir(dir);
150 }
151
152
153 static void discover_trig_sensors (int dev_num, char map[CATALOG_SIZE])
154 {
155         char scan_elem_dir[PATH_MAX];
156         DIR *dir;
157         char sysfs_dir[PATH_MAX];
158         struct sensor *sensor;
159         struct dirent *d;
160         unsigned int i;
161
162         memset(map, 0, CATALOG_SIZE);
163
164         /* Enumerate entries in this iio device's scan_elements folder */
165
166         snprintf(scan_elem_dir, sizeof(scan_elem_dir), CHANNEL_PATH, dev_num);
167
168         dir = opendir(scan_elem_dir);
169         if (!dir) {
170                return;
171         }
172
173         while ((d = readdir(dir))) {
174                 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
175                         continue;
176
177                 /* Compare en entry to known ones and create matching sensors */
178
179                 for (i = 0; i<CATALOG_SIZE; i++)
180                         if (!strcmp(d->d_name,
181                                     sensor_catalog[i].channel[0].en_path)) {
182                                 map[i] = 1;
183                                 break;
184                         }
185         }
186
187         closedir(dir);
188 }
189
190
191 void enumerate_sensors (void)
192 {
193         /*
194          * Discover supported sensors and allocate control structures for them.
195          * Multiple sensors can potentially rely on a single iio device (each
196          * using their own channels). We can't have multiple sensors of the same
197          * type on the same device. In case of detection as both a poll-mode
198          * and trigger-based sensor, use the trigger usage mode.
199          */
200         char poll_sensors[CATALOG_SIZE];
201         char trig_sensors[CATALOG_SIZE];
202         int dev_num;
203         unsigned int i;
204
205         for (dev_num=0; dev_num<MAX_DEVICES; dev_num++) {
206                 discover_poll_sensors(dev_num, poll_sensors);
207                 discover_trig_sensors(dev_num, trig_sensors);
208
209                 for (i=0; i<CATALOG_SIZE; i++)
210                         if (trig_sensors[i])
211                                 add_sensor(dev_num, i, 0);
212                         else
213                                 if (poll_sensors[i])
214                                         add_sensor(dev_num, i, 1);
215         }
216
217         ALOGI("Discovered %d sensors\n", sensor_count);
218 }
219
220
221 void delete_enumeration_data (void)
222 {
223         /* Reset sensor count */
224         sensor_count = 0;
225 }
226
227
228 int get_sensors_list(   struct sensors_module_t* module,
229                         struct sensor_t const** list)
230 {
231         *list = sensor_desc;
232         return sensor_count;
233 }
234