2 * Copyright (C) 2014 Intel Corporation.
7 #include <hardware/sensors.h>
8 #include "enumeration.h"
9 #include "description.h"
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.
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 DECLARE_SENSOR0("timestamp", SENSOR_TYPE_DEVICE_PRIVATE_BASE )
30 #define CATALOG_SIZE ARRAY_SIZE(sensor_catalog)
33 /* We equate sensor handles to indices in these tables */
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 */
40 static void add_sensor (int dev_num, int catalog_index, int use_polling)
44 char sysfs_path[PATH_MAX];
48 if (sensor_count == MAX_SENSORS) {
49 ALOGE("Too many sensors!\n");
53 sensor_type = sensor_catalog[catalog_index].type;
56 * At this point we could check that the expected sysfs attributes are
57 * present ; that would enable having multiple catalog entries with the
58 * same sensor type, accomodating different sets of sysfs attributes.
63 sensor_info[s].dev_num = dev_num;
64 sensor_info[s].catalog_index = catalog_index;
67 sensor_info[s].num_channels = 0;
69 sensor_info[s].num_channels =
70 sensor_catalog[catalog_index].num_channels;
72 prefix = sensor_catalog[catalog_index].tag;
74 /* Read name attribute, if available */
75 sprintf(sysfs_path, NAME_PATH, dev_num);
76 sysfs_read_str(sysfs_path, sensor_info[s].internal_name, MAX_NAME_SIZE);
78 /* See if we have general offsets and scale values for this sensor */
80 sprintf(sysfs_path, COMMON_OFFSET_PATH, dev_num, prefix);
81 sysfs_read_float(sysfs_path, &sensor_info[s].offset);
83 sprintf(sysfs_path, COMMON_SCALE_PATH, dev_num, prefix);
84 if (!sysfs_read_float(sysfs_path, &scale))
85 sensor_info[s].scale = scale;
87 sensor_info[s].scale = 1;
89 /* Initialize Android-visible descriptor */
90 sensor_desc[s].name = sensor_get_name(s);
91 sensor_desc[s].vendor = sensor_get_vendor(s);
92 sensor_desc[s].version = sensor_get_version(s);
93 sensor_desc[s].handle = s;
94 sensor_desc[s].type = sensor_type;
95 sensor_desc[s].maxRange = sensor_get_max_range(s);
96 sensor_desc[s].resolution = sensor_get_resolution(s);
97 sensor_desc[s].power = sensor_get_power(s);
99 if (sensor_info[s].internal_name[0] == '\0') {
101 * In case the kernel-mode driver doesn't expose a name for
102 * the iio device, use (null)-dev%d as the trigger name...
103 * This can be considered a kernel-mode iio driver bug.
105 ALOGW("Using null trigger on sensor %d (dev %d)\n", s, dev_num);
106 strcpy(sensor_info[s].internal_name, "(null)");
113 static void discover_poll_sensors (int dev_num, char map[CATALOG_SIZE])
115 char base_dir[PATH_MAX];
117 char sysfs_dir[PATH_MAX];
118 struct sensor *sensor;
123 memset(map, 0, CATALOG_SIZE);
125 snprintf(base_dir, sizeof(base_dir), BASE_PATH, dev_num);
127 dir = opendir(base_dir);
132 /* Enumerate entries in this iio device's base folder */
134 while ((d = readdir(dir))) {
135 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
138 /* If the name matches a catalog entry, flag it */
139 for (i = 0; i<CATALOG_SIZE; i++)
140 for (c=0; c<sensor_catalog[i].num_channels; c++)
141 if (!strcmp(d->d_name,
142 sensor_catalog[i].channel[c].raw_path) ||
144 sensor_catalog[i].channel[c].input_path)) {
154 static void discover_trig_sensors (int dev_num, char map[CATALOG_SIZE])
156 char scan_elem_dir[PATH_MAX];
158 char sysfs_dir[PATH_MAX];
159 struct sensor *sensor;
163 memset(map, 0, CATALOG_SIZE);
165 /* Enumerate entries in this iio device's scan_elements folder */
167 snprintf(scan_elem_dir, sizeof(scan_elem_dir), CHANNEL_PATH, dev_num);
169 dir = opendir(scan_elem_dir);
174 while ((d = readdir(dir))) {
175 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
178 /* Compare en entry to known ones and create matching sensors */
180 for (i = 0; i<CATALOG_SIZE; i++)
181 if (!strcmp(d->d_name,
182 sensor_catalog[i].channel[0].en_path)) {
192 void enumerate_sensors (void)
195 * Discover supported sensors and allocate control structures for them.
196 * Multiple sensors can potentially rely on a single iio device (each
197 * using their own channels). We can't have multiple sensors of the same
198 * type on the same device. In case of detection as both a poll-mode
199 * and trigger-based sensor, use the trigger usage mode.
201 char poll_sensors[CATALOG_SIZE];
202 char trig_sensors[CATALOG_SIZE];
206 for (dev_num=0; dev_num<MAX_DEVICES; dev_num++) {
207 discover_poll_sensors(dev_num, poll_sensors);
208 discover_trig_sensors(dev_num, trig_sensors);
210 for (i=0; i<CATALOG_SIZE; i++)
212 add_sensor(dev_num, i, 0);
215 add_sensor(dev_num, i, 1);
218 ALOGI("Discovered %d sensors\n", sensor_count);
222 void delete_enumeration_data (void)
224 /* Reset sensor count */
229 int get_sensors_list( struct sensors_module_t* module,
230 struct sensor_t const** list)