2 * Copyright (C) 2014 Intel Corporation.
9 #include <hardware/sensors.h>
10 #include "enumeration.h"
11 #include "description.h"
13 #include "transform.h"
14 #include "description.h"
16 #include "calibration.h"
19 * This table maps syfs entries in scan_elements directories to sensor types,
20 * and will also be used to determine other sysfs names as well as the iio
21 * device number associated to a specific sensor.
25 * We duplicate entries for the uncalibrated types after their respective base
26 * sensor. This is because all sensor entries must have an associated catalog entry
27 * and also because when only the uncal sensor is active it needs to take it's data
28 * from the same iio device as the base one.
31 struct sensor_catalog_entry_t sensor_catalog[] = {
32 DECLARE_SENSOR3("accel", SENSOR_TYPE_ACCELEROMETER, "x", "y", "z")
33 DECLARE_SENSOR3("anglvel", SENSOR_TYPE_GYROSCOPE, "x", "y", "z")
34 DECLARE_SENSOR3("magn", SENSOR_TYPE_MAGNETIC_FIELD, "x", "y", "z")
35 DECLARE_SENSOR1("intensity", SENSOR_TYPE_LIGHT, "both" )
36 DECLARE_SENSOR0("illuminance",SENSOR_TYPE_LIGHT )
37 DECLARE_SENSOR3("incli", SENSOR_TYPE_ORIENTATION, "x", "y", "z")
38 DECLARE_SENSOR4("rot", SENSOR_TYPE_ROTATION_VECTOR,
39 "quat_x", "quat_y", "quat_z", "quat_w")
40 DECLARE_SENSOR0("temp", SENSOR_TYPE_AMBIENT_TEMPERATURE )
41 DECLARE_SENSOR0("proximity", SENSOR_TYPE_PROXIMITY )
42 DECLARE_SENSOR3("anglvel", SENSOR_TYPE_GYROSCOPE_UNCALIBRATED, "x", "y", "z")
45 #define CATALOG_SIZE ARRAY_SIZE(sensor_catalog)
48 /* We equate sensor handles to indices in these tables */
50 struct sensor_t sensor_desc[MAX_SENSORS]; /* Android-level descriptors */
51 struct sensor_info_t sensor_info[MAX_SENSORS]; /* Internal descriptors */
52 int sensor_count; /* Detected sensors */
55 static void add_sensor (int dev_num, int catalog_index, int use_polling)
60 char sysfs_path[PATH_MAX];
67 char suffix[MAX_NAME_SIZE + 8];
69 if (sensor_count == MAX_SENSORS) {
70 ALOGE("Too many sensors!\n");
74 sensor_type = sensor_catalog[catalog_index].type;
77 * At this point we could check that the expected sysfs attributes are
78 * present ; that would enable having multiple catalog entries with the
79 * same sensor type, accomodating different sets of sysfs attributes.
84 sensor_info[s].dev_num = dev_num;
85 sensor_info[s].catalog_index = catalog_index;
88 sensor_info[s].num_channels = 0;
90 sensor_info[s].num_channels =
91 sensor_catalog[catalog_index].num_channels;
93 prefix = sensor_catalog[catalog_index].tag;
96 * receiving the illumination sensor calibration inputs from
97 * the Android properties and setting it within sysfs
99 if (sensor_catalog[catalog_index].type == SENSOR_TYPE_LIGHT) {
100 retval = sensor_get_illumincalib(s);
102 sprintf(sysfs_path, ILLUMINATION_CALIBPATH, dev_num);
103 sysfs_write_int(sysfs_path, retval);
107 /* Read name attribute, if available */
108 sprintf(sysfs_path, NAME_PATH, dev_num);
109 sysfs_read_str(sysfs_path, sensor_info[s].internal_name, MAX_NAME_SIZE);
111 /* See if we have general offsets and scale values for this sensor */
113 sprintf(sysfs_path, SENSOR_OFFSET_PATH, dev_num, prefix);
114 sysfs_read_float(sysfs_path, &sensor_info[s].offset);
116 sprintf(sysfs_path, SENSOR_SCALE_PATH, dev_num, prefix);
117 if (!sysfs_read_float(sysfs_path, &scale)) {
118 sensor_info[s].scale = scale;
119 ALOGI("Scale path:%s scale:%f dev_num:%d\n",
120 sysfs_path, scale, dev_num);
122 sensor_info[s].scale = 1;
124 /* Read channel specific scale if any*/
125 for (c = 0; c < sensor_catalog[catalog_index].num_channels; c++)
127 sprintf(sysfs_path, BASE_PATH "%s", dev_num,
128 sensor_catalog[catalog_index].channel[c].scale_path);
130 if (!sysfs_read_float(sysfs_path, &scale)) {
131 sensor_info[s].channel[c].scale = scale;
132 sensor_info[s].scale = 0;
134 ALOGI( "Scale path:%s "
135 "channel scale:%f dev_num:%d\n",
136 sysfs_path, scale, dev_num);
142 * See if we have optional correction scaling factors for each of the
143 * channels of this sensor. These would be expressed using properties
144 * like iio.accel.y.opt_scale = -1. In case of a single channel we also
145 * support things such as iio.temp.opt_scale = -1. Note that this works
146 * for all types of sensors, and whatever transform is selected, on top
147 * of any previous conversions.
149 num_channels = sensor_catalog[catalog_index].num_channels;
152 for (c = 0; c < num_channels; c++) {
155 ch_name = sensor_catalog[catalog_index].channel[c].name;
156 sprintf(suffix, "%s.opt_scale", ch_name);
157 sensor_get_fl_prop(s, suffix, &opt_scale);
159 sensor_info[s].channel[c].opt_scale = opt_scale;
163 sensor_get_fl_prop(s, "opt_scale", &opt_scale);
164 sensor_info[s].channel[0].opt_scale = opt_scale;
167 /* Initialize Android-visible descriptor */
168 sensor_desc[s].name = sensor_get_name(s);
169 sensor_desc[s].vendor = sensor_get_vendor(s);
170 sensor_desc[s].version = sensor_get_version(s);
171 sensor_desc[s].handle = s;
172 sensor_desc[s].type = sensor_type;
173 sensor_desc[s].maxRange = sensor_get_max_range(s);
174 sensor_desc[s].resolution = sensor_get_resolution(s);
175 sensor_desc[s].power = sensor_get_power(s);
176 sensor_desc[s].stringType = sensor_get_string_type(s);
178 /* None of our supported sensors requires a special permission.
179 * If this will be the case we should implement a sensor_get_perm
181 sensor_desc[s].requiredPermission = "";
182 sensor_desc[s].flags = sensor_get_flags(s);
184 if (sensor_info[s].internal_name[0] == '\0') {
186 * In case the kernel-mode driver doesn't expose a name for
187 * the iio device, use (null)-dev%d as the trigger name...
188 * This can be considered a kernel-mode iio driver bug.
190 ALOGW("Using null trigger on sensor %d (dev %d)\n", s, dev_num);
191 strcpy(sensor_info[s].internal_name, "(null)");
194 if (sensor_catalog[catalog_index].type == SENSOR_TYPE_GYROSCOPE ||
195 sensor_catalog[catalog_index].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED) {
196 struct gyro_cal* calibration_data = calloc(1, sizeof(struct gyro_cal));
197 sensor_info[s].cal_data = calibration_data;
200 if (sensor_catalog[catalog_index].type == SENSOR_TYPE_MAGNETIC_FIELD) {
201 struct compass_cal* calibration_data = calloc(1, sizeof(struct compass_cal));
202 sensor_info[s].cal_data = calibration_data;
205 /* Select one of the available sensor sample processing styles */
208 /* Initialize fields related to sysfs reads offloading */
209 sensor_info[s].thread_data_fd[0] = -1;
210 sensor_info[s].thread_data_fd[1] = -1;
211 sensor_info[s].acquisition_thread = -1;
213 /* Check if we have a special ordering property on this sensor */
214 if (sensor_get_order(s, sensor_info[s].order))
215 sensor_info[s].flags |= FLAG_FIELD_ORDERING;
221 static void discover_poll_sensors (int dev_num, char map[CATALOG_SIZE])
223 char base_dir[PATH_MAX];
229 memset(map, 0, CATALOG_SIZE);
231 snprintf(base_dir, sizeof(base_dir), BASE_PATH, dev_num);
233 dir = opendir(base_dir);
238 /* Enumerate entries in this iio device's base folder */
240 while ((d = readdir(dir))) {
241 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
244 /* If the name matches a catalog entry, flag it */
245 for (i = 0; i<CATALOG_SIZE; i++) {
246 /* This will be added separately later */
247 if (sensor_catalog[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED)
249 for (c=0; c<sensor_catalog[i].num_channels; c++)
250 if (!strcmp(d->d_name,sensor_catalog[i].channel[c].raw_path) ||
251 !strcmp(d->d_name, sensor_catalog[i].channel[c].input_path)) {
262 static void discover_trig_sensors (int dev_num, char map[CATALOG_SIZE])
264 char scan_elem_dir[PATH_MAX];
269 memset(map, 0, CATALOG_SIZE);
271 /* Enumerate entries in this iio device's scan_elements folder */
273 snprintf(scan_elem_dir, sizeof(scan_elem_dir), CHANNEL_PATH, dev_num);
275 dir = opendir(scan_elem_dir);
280 while ((d = readdir(dir))) {
281 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
284 /* Compare en entry to known ones and create matching sensors */
286 for (i = 0; i<CATALOG_SIZE; i++) {
287 if (sensor_catalog[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED)
289 if (!strcmp(d->d_name,
290 sensor_catalog[i].channel[0].en_path)) {
301 static void orientation_sensor_check(void)
304 * If we have accel + gyro + magn but no rotation vector sensor,
305 * SensorService replaces the HAL provided orientation sensor by the
306 * AOSP version... provided we report one. So initialize a virtual
307 * orientation sensor with zero values, which will get replaced. See:
308 * frameworks/native/services/sensorservice/SensorService.cpp, looking
309 * for SENSOR_TYPE_ROTATION_VECTOR; that code should presumably fall
310 * back to mUserSensorList.add instead of replaceAt, but accommodate it.
319 int catalog_size = CATALOG_SIZE;
321 for (i=0; i<sensor_count; i++)
322 switch (sensor_catalog[sensor_info[i].catalog_index].type) {
323 case SENSOR_TYPE_ACCELEROMETER:
326 case SENSOR_TYPE_GYROSCOPE:
329 case SENSOR_TYPE_MAGNETIC_FIELD:
332 case SENSOR_TYPE_ORIENTATION:
335 case SENSOR_TYPE_ROTATION_VECTOR:
340 if (has_acc && has_gyr && has_mag && !has_rot && !has_ori)
341 for (i=0; i<catalog_size; i++)
342 if (sensor_catalog[i].type == SENSOR_TYPE_ORIENTATION) {
343 ALOGI("Adding placeholder orientation sensor");
349 static void uncalibrated_gyro_check (void)
351 unsigned int has_gyr = 0;
352 unsigned int dev_num;
354 unsigned int is_poll_sensor;
359 /* Checking to see if we have a gyroscope - we can only have uncal if we have the base sensor */
360 for (i=0; i < sensor_count; i++)
361 if(sensor_catalog[sensor_info[i].catalog_index].type == SENSOR_TYPE_GYROSCOPE)
364 dev_num = sensor_info[i].dev_num;
365 is_poll_sensor = !sensor_info[i].num_channels;
371 * If we have a gyro we can add the uncalibrated sensor of the same type and
372 * on the same dev_num. We will save indexes for easy finding and also save the
373 * channel specific information.
376 for (i=0; i<CATALOG_SIZE; i++)
377 if (sensor_catalog[i].type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED) {
378 add_sensor(dev_num, i, is_poll_sensor);
380 uncal_idx = sensor_count - 1; /* Just added uncalibrated sensor */
382 /* Similar to build_sensor_report_maps */
383 for (c = 0; c < sensor_info[uncal_idx].num_channels; c++)
385 memcpy( &(sensor_info[uncal_idx].channel[c].type_spec),
386 &(sensor_info[cal_idx].channel[c].type_spec),
387 sizeof(sensor_info[uncal_idx].channel[c].type_spec));
388 sensor_info[uncal_idx].channel[c].type_info = sensor_info[cal_idx].channel[c].type_info;
389 sensor_info[uncal_idx].channel[c].offset = sensor_info[cal_idx].channel[c].offset;
390 sensor_info[uncal_idx].channel[c].size = sensor_info[cal_idx].channel[c].size;
392 sensor_info[uncal_idx].pair_idx = cal_idx;
393 sensor_info[cal_idx].pair_idx = uncal_idx;
398 static void update_sensor_matching_trigger_name (char name[MAX_NAME_SIZE])
401 * Check if we have a sensor matching the specified trigger name,
402 * which should then begin with the sensor name, and end with a number
403 * equal to the iio device number the sensor is associated to. If so,
404 * update the string we're going to write to trigger/current_trigger
405 * when enabling this sensor.
414 * First determine the iio device number this trigger refers to. We
415 * expect the last few characters (typically one) of the trigger name
416 * to be this number, so perform a few checks.
418 len = strnlen(name, MAX_NAME_SIZE);
423 cursor = name + len - 1;
425 if (!isdigit(*cursor))
428 while (len && isdigit(*cursor)) {
433 dev_num = atoi(cursor+1);
435 /* See if that matches a sensor */
436 for (s=0; s<sensor_count; s++)
437 if (sensor_info[s].dev_num == dev_num &&
438 !strncmp(name, sensor_info[s].internal_name,
439 strlen(sensor_info[s].internal_name))) {
440 /* Update sensor structure and return */
441 strcpy(sensor_info[s].trigger_name, name);
447 static void setup_trigger_names (void)
449 char filename[PATH_MAX];
450 char buf[MAX_NAME_SIZE];
456 /* By default, use the name-dev convention that most drivers use */
457 for (s=0; s<sensor_count; s++)
458 snprintf(sensor_info[s].trigger_name, MAX_NAME_SIZE, "%s-dev%d",
459 sensor_info[s].internal_name, sensor_info[s].dev_num);
461 /* Now have a look to /sys/bus/iio/devices/triggerX entries */
463 for (trigger=0; trigger<MAX_TRIGGERS; trigger++) {
465 snprintf(filename, sizeof(filename), TRIGGER_FILE_PATH,trigger);
467 ret = sysfs_read_str(filename, buf, sizeof(buf));
472 update_sensor_matching_trigger_name(buf);
475 for (s=0; s<sensor_count; s++)
476 if (sensor_info[s].num_channels) {
477 ALOGI( "Sensor %d (%s) using iio trigger %s\n", s,
478 sensor_info[s].friendly_name,
479 sensor_info[s].trigger_name);
484 void enumerate_sensors (void)
487 * Discover supported sensors and allocate control structures for them.
488 * Multiple sensors can potentially rely on a single iio device (each
489 * using their own channels). We can't have multiple sensors of the same
490 * type on the same device. In case of detection as both a poll-mode
491 * and trigger-based sensor, use the trigger usage mode.
493 char poll_sensors[CATALOG_SIZE];
494 char trig_sensors[CATALOG_SIZE];
499 for (dev_num=0; dev_num<MAX_DEVICES; dev_num++) {
502 discover_poll_sensors(dev_num, poll_sensors);
503 discover_trig_sensors(dev_num, trig_sensors);
505 for (i=0; i<CATALOG_SIZE; i++)
506 if (trig_sensors[i]) {
507 add_sensor(dev_num, i, 0);
512 add_sensor(dev_num, i, 1);
515 build_sensor_report_maps(dev_num);
519 ALOGI("Discovered %d sensors\n", sensor_count);
521 /* Set up default - as well as custom - trigger names */
522 setup_trigger_names();
524 /* Make sure Android fall backs to its own orientation sensor */
525 orientation_sensor_check();
527 /* Create the uncalibrated counterpart to the compensated gyroscope;
528 * This is is a new sensor type in Android 4.4 */
529 uncalibrated_gyro_check();
533 void delete_enumeration_data (void)
537 for (i = 0; i < sensor_count; i++)
538 switch (sensor_catalog[sensor_info[i].catalog_index].type) {
539 case SENSOR_TYPE_MAGNETIC_FIELD:
540 case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
541 case SENSOR_TYPE_GYROSCOPE:
542 if (sensor_info[i].cal_data != NULL) {
543 free(sensor_info[i].cal_data);
544 sensor_info[i].cal_data = NULL;
545 sensor_info[i].calibrated = 0;
551 /* Reset sensor count */
556 int get_sensors_list( struct sensors_module_t* module,
557 struct sensor_t const** list)