2 * Copyright (C) 2014 Intel Corporation.
7 #include <hardware/sensors.h>
8 #include "enumeration.h"
9 #include "description.h"
11 #include "transform.h"
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.
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 )
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)
45 char sysfs_path[PATH_MAX];
49 if (sensor_count == MAX_SENSORS) {
50 ALOGE("Too many sensors!\n");
54 sensor_type = sensor_catalog[catalog_index].type;
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.
64 sensor_info[s].dev_num = dev_num;
65 sensor_info[s].catalog_index = catalog_index;
68 sensor_info[s].num_channels = 0;
70 sensor_info[s].num_channels =
71 sensor_catalog[catalog_index].num_channels;
73 prefix = sensor_catalog[catalog_index].tag;
76 * receiving the illumination sensor calibration inputs from
77 * the Android properties and setting it within sysfs
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);
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);
89 /* See if we have general offsets and scale values for this sensor */
91 sprintf(sysfs_path, SENSOR_OFFSET_PATH, dev_num, prefix);
92 sysfs_read_float(sysfs_path, &sensor_info[s].offset);
94 sprintf(sysfs_path, SENSOR_SCALE_PATH, dev_num, prefix);
95 if (!sysfs_read_float(sysfs_path, &scale))
96 sensor_info[s].scale = scale;
98 sensor_info[s].scale = 1;
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);
110 if (sensor_info[s].internal_name[0] == '\0') {
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.
116 ALOGW("Using null trigger on sensor %d (dev %d)\n", s, dev_num);
117 strcpy(sensor_info[s].internal_name, "(null)");
120 /* Select one of the available sensor sample processing styles */
127 static void discover_poll_sensors (int dev_num, char map[CATALOG_SIZE])
129 char base_dir[PATH_MAX];
131 char sysfs_dir[PATH_MAX];
132 struct sensor *sensor;
137 memset(map, 0, CATALOG_SIZE);
139 snprintf(base_dir, sizeof(base_dir), BASE_PATH, dev_num);
141 dir = opendir(base_dir);
146 /* Enumerate entries in this iio device's base folder */
148 while ((d = readdir(dir))) {
149 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
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) ||
158 sensor_catalog[i].channel[c].input_path)) {
168 static void discover_trig_sensors (int dev_num, char map[CATALOG_SIZE])
170 char scan_elem_dir[PATH_MAX];
172 char sysfs_dir[PATH_MAX];
173 struct sensor *sensor;
177 memset(map, 0, CATALOG_SIZE);
179 /* Enumerate entries in this iio device's scan_elements folder */
181 snprintf(scan_elem_dir, sizeof(scan_elem_dir), CHANNEL_PATH, dev_num);
183 dir = opendir(scan_elem_dir);
188 while ((d = readdir(dir))) {
189 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
192 /* Compare en entry to known ones and create matching sensors */
194 for (i = 0; i<CATALOG_SIZE; i++)
195 if (!strcmp(d->d_name,
196 sensor_catalog[i].channel[0].en_path)) {
206 static void orientation_sensor_check(void)
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.
225 for (i=0; i<sensor_count; i++)
226 switch (sensor_catalog[sensor_info[i].catalog_index].type) {
227 case SENSOR_TYPE_ACCELEROMETER:
230 case SENSOR_TYPE_GYROSCOPE:
233 case SENSOR_TYPE_MAGNETIC_FIELD:
236 case SENSOR_TYPE_ORIENTATION:
239 case SENSOR_TYPE_ROTATION_VECTOR:
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");
254 void enumerate_sensors (void)
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.
263 char poll_sensors[CATALOG_SIZE];
264 char trig_sensors[CATALOG_SIZE];
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);
272 for (i=0; i<CATALOG_SIZE; i++)
274 add_sensor(dev_num, i, 0);
277 add_sensor(dev_num, i, 1);
280 ALOGI("Discovered %d sensors\n", sensor_count);
282 /* Make sure Android fall backs to its own orientation sensor */
283 orientation_sensor_check();
287 void delete_enumeration_data (void)
289 /* Reset sensor count */
294 int get_sensors_list( struct sensors_module_t* module,
295 struct sensor_t const** list)