2 * Copyright (C) 2014 Intel Corporation.
7 #include <hardware/sensors.h>
8 #include "enumeration.h"
9 #include "description.h"
11 #include "transform.h"
12 #include "description.h"
16 * This table maps syfs entries in scan_elements directories to sensor types,
17 * and will also be used to determine other sysfs names as well as the iio
18 * device number associated to a specific sensor.
21 struct sensor_catalog_entry_t sensor_catalog[] = {
22 DECLARE_SENSOR3("accel", SENSOR_TYPE_ACCELEROMETER, "x", "y", "z")
23 DECLARE_SENSOR3("anglvel", SENSOR_TYPE_GYROSCOPE, "x", "y", "z")
24 DECLARE_SENSOR3("magn", SENSOR_TYPE_MAGNETIC_FIELD, "x", "y", "z")
25 DECLARE_SENSOR1("intensity", SENSOR_TYPE_LIGHT, "both" )
26 DECLARE_SENSOR0("illuminance",SENSOR_TYPE_LIGHT )
27 DECLARE_SENSOR3("incli", SENSOR_TYPE_ORIENTATION, "x", "y", "z")
28 DECLARE_SENSOR4("rot", SENSOR_TYPE_ROTATION_VECTOR,
29 "quat_x", "quat_y", "quat_z", "quat_w")
30 DECLARE_SENSOR0("temp", SENSOR_TYPE_AMBIENT_TEMPERATURE )
33 #define CATALOG_SIZE ARRAY_SIZE(sensor_catalog)
36 /* We equate sensor handles to indices in these tables */
38 struct sensor_t sensor_desc[MAX_SENSORS]; /* Android-level descriptors */
39 struct sensor_info_t sensor_info[MAX_SENSORS]; /* Internal descriptors */
40 int sensor_count; /* Detected sensors */
43 static void add_sensor (int dev_num, int catalog_index, int use_polling)
48 char sysfs_path[PATH_MAX];
55 char suffix[MAX_NAME_SIZE + 8];
57 if (sensor_count == MAX_SENSORS) {
58 ALOGE("Too many sensors!\n");
62 sensor_type = sensor_catalog[catalog_index].type;
65 * At this point we could check that the expected sysfs attributes are
66 * present ; that would enable having multiple catalog entries with the
67 * same sensor type, accomodating different sets of sysfs attributes.
72 sensor_info[s].dev_num = dev_num;
73 sensor_info[s].catalog_index = catalog_index;
76 sensor_info[s].num_channels = 0;
78 sensor_info[s].num_channels =
79 sensor_catalog[catalog_index].num_channels;
81 prefix = sensor_catalog[catalog_index].tag;
84 * receiving the illumination sensor calibration inputs from
85 * the Android properties and setting it within sysfs
87 if (sensor_catalog[catalog_index].type == SENSOR_TYPE_LIGHT) {
88 retval = sensor_get_illumincalib(s);
89 sprintf(sysfs_path, ILLUMINATION_CALIBPATH, dev_num);
90 sysfs_write_int(sysfs_path, retval);
93 /* Read name attribute, if available */
94 sprintf(sysfs_path, NAME_PATH, dev_num);
95 sysfs_read_str(sysfs_path, sensor_info[s].internal_name, MAX_NAME_SIZE);
97 /* See if we have general offsets and scale values for this sensor */
99 sprintf(sysfs_path, SENSOR_OFFSET_PATH, dev_num, prefix);
100 sysfs_read_float(sysfs_path, &sensor_info[s].offset);
102 sprintf(sysfs_path, SENSOR_SCALE_PATH, dev_num, prefix);
103 if (!sysfs_read_float(sysfs_path, &scale)) {
104 sensor_info[s].scale = scale;
105 ALOGI("Scale path:%s scale:%f dev_num:%d\n",
106 sysfs_path, scale, dev_num);
108 sensor_info[s].scale = 1;
110 /* Read channel specific scale if any*/
111 for (c = 0; c < sensor_catalog[catalog_index].num_channels; c++)
113 sprintf(sysfs_path, BASE_PATH "%s", dev_num,
114 sensor_catalog[catalog_index].channel[c].scale_path);
116 if (!sysfs_read_float(sysfs_path, &scale)) {
117 sensor_info[s].channel[c].scale = scale;
118 sensor_info[s].scale = 0;
120 ALOGI( "Scale path:%s "
121 "channel scale:%f dev_num:%d\n",
122 sysfs_path, scale, dev_num);
128 * See if we have optional correction scaling factors for each of the
129 * channels of this sensor. These would be expressed using properties
130 * like iio.accel.y.scale = -1. In case of a single channel we also
131 * support things such as iio.temp.scale = -1. Note that this works
132 * for all types of sensors, and whatever transform is selected, on top
133 * of any previous conversions.
135 num_channels = sensor_catalog[catalog_index].num_channels;
138 for (c = 0; c < num_channels; c++) {
141 ch_name = sensor_catalog[catalog_index].channel[c].name;
142 sprintf(suffix, "%s.scale", ch_name);
143 sensor_get_fl_prop(s, suffix, &opt_scale);
145 sensor_info[s].channel[c].opt_scale = opt_scale;
149 sensor_get_fl_prop(s, "scale", &opt_scale);
150 sensor_info[s].channel[0].opt_scale = opt_scale;
153 /* Initialize Android-visible descriptor */
154 sensor_desc[s].name = sensor_get_name(s);
155 sensor_desc[s].vendor = sensor_get_vendor(s);
156 sensor_desc[s].version = sensor_get_version(s);
157 sensor_desc[s].handle = s;
158 sensor_desc[s].type = sensor_type;
159 sensor_desc[s].maxRange = sensor_get_max_range(s);
160 sensor_desc[s].resolution = sensor_get_resolution(s);
161 sensor_desc[s].power = sensor_get_power(s);
163 if (sensor_info[s].internal_name[0] == '\0') {
165 * In case the kernel-mode driver doesn't expose a name for
166 * the iio device, use (null)-dev%d as the trigger name...
167 * This can be considered a kernel-mode iio driver bug.
169 ALOGW("Using null trigger on sensor %d (dev %d)\n", s, dev_num);
170 strcpy(sensor_info[s].internal_name, "(null)");
173 /* Select one of the available sensor sample processing styles */
180 static void discover_poll_sensors (int dev_num, char map[CATALOG_SIZE])
182 char base_dir[PATH_MAX];
184 char sysfs_dir[PATH_MAX];
185 struct sensor *sensor;
190 memset(map, 0, CATALOG_SIZE);
192 snprintf(base_dir, sizeof(base_dir), BASE_PATH, dev_num);
194 dir = opendir(base_dir);
199 /* Enumerate entries in this iio device's base folder */
201 while ((d = readdir(dir))) {
202 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
205 /* If the name matches a catalog entry, flag it */
206 for (i = 0; i<CATALOG_SIZE; i++)
207 for (c=0; c<sensor_catalog[i].num_channels; c++)
208 if (!strcmp(d->d_name,
209 sensor_catalog[i].channel[c].raw_path) ||
211 sensor_catalog[i].channel[c].input_path)) {
221 static void discover_trig_sensors (int dev_num, char map[CATALOG_SIZE])
223 char scan_elem_dir[PATH_MAX];
225 char sysfs_dir[PATH_MAX];
226 struct sensor *sensor;
230 memset(map, 0, CATALOG_SIZE);
232 /* Enumerate entries in this iio device's scan_elements folder */
234 snprintf(scan_elem_dir, sizeof(scan_elem_dir), CHANNEL_PATH, dev_num);
236 dir = opendir(scan_elem_dir);
241 while ((d = readdir(dir))) {
242 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
245 /* Compare en entry to known ones and create matching sensors */
247 for (i = 0; i<CATALOG_SIZE; i++)
248 if (!strcmp(d->d_name,
249 sensor_catalog[i].channel[0].en_path)) {
259 static void orientation_sensor_check(void)
262 * If we have accel + gyro + magn but no rotation vector sensor,
263 * SensorService replaces the HAL provided orientation sensor by the
264 * AOSP version... provided we report one. So initialize a virtual
265 * orientation sensor with zero values, which will get replaced. See:
266 * frameworks/native/services/sensorservice/SensorService.cpp, looking
267 * for SENSOR_TYPE_ROTATION_VECTOR; that code should presumably fall
268 * back to mUserSensorList.add instead of replaceAt, but accommodate it.
277 int catalog_size = CATALOG_SIZE;
279 for (i=0; i<sensor_count; i++)
280 switch (sensor_catalog[sensor_info[i].catalog_index].type) {
281 case SENSOR_TYPE_ACCELEROMETER:
284 case SENSOR_TYPE_GYROSCOPE:
287 case SENSOR_TYPE_MAGNETIC_FIELD:
290 case SENSOR_TYPE_ORIENTATION:
293 case SENSOR_TYPE_ROTATION_VECTOR:
298 if (has_acc && has_gyr && has_mag && !has_rot && !has_ori)
299 for (i=0; i<catalog_size; i++)
300 if (sensor_catalog[i].type == SENSOR_TYPE_ORIENTATION) {
301 ALOGI("Adding placeholder orientation sensor");
308 void enumerate_sensors (void)
311 * Discover supported sensors and allocate control structures for them.
312 * Multiple sensors can potentially rely on a single iio device (each
313 * using their own channels). We can't have multiple sensors of the same
314 * type on the same device. In case of detection as both a poll-mode
315 * and trigger-based sensor, use the trigger usage mode.
317 char poll_sensors[CATALOG_SIZE];
318 char trig_sensors[CATALOG_SIZE];
323 for (dev_num=0; dev_num<MAX_DEVICES; dev_num++) {
326 discover_poll_sensors(dev_num, poll_sensors);
327 discover_trig_sensors(dev_num, trig_sensors);
329 for (i=0; i<CATALOG_SIZE; i++)
330 if (trig_sensors[i]) {
331 add_sensor(dev_num, i, 0);
336 add_sensor(dev_num, i, 1);
339 build_sensor_report_maps(dev_num);
342 ALOGI("Discovered %d sensors\n", sensor_count);
344 /* Make sure Android fall backs to its own orientation sensor */
345 orientation_sensor_check();
349 void delete_enumeration_data (void)
351 /* Reset sensor count */
356 int get_sensors_list( struct sensors_module_t* module,
357 struct sensor_t const** list)