OSDN Git Service

Min/Max delay for Light and Temperature
[android-x86/hardware-intel-libsensors.git] / description.c
index 8932ea8..bcfba2e 100644 (file)
 #include <utils/Log.h>
 #include <cutils/properties.h>
 #include <hardware/sensors.h>
+#include "common.h"
 #include "enumeration.h"
+#include "description.h"
 
 #define IIO_SENSOR_HAL_VERSION 1
 
+/*
+ * About properties
+ *
+ * We acquire a number of parameters about sensors by reading properties.
+ * The idea here is that someone (either a script, or daemon, sets them
+ * depending on the set of sensors present on the machine.
+ *
+ * There are fallback paths in case the properties are not defined, but it is
+ * highly desirable to at least have the following for each sensor:
+ *
+ * ro.iio.anglvel.name = Gyroscope
+ * ro.iio.anglvel.vendor = Intel
+ * ro.iio.anglvel.max_range = 35
+ * ro.iio.anglvel.resolution = 0.002
+ * ro.iio.anglvel.power = 6.1
+ *
+ * Besides these, we have a couple of knobs initially used to cope with Intel
+ * Sensor Hub oddities, such as HID inspired units or firmware bugs:
+ *
+ * ro.iio.anglvel.transform = ISH
+ * ro.iio.anglvel.quirks = init-rate
+ *
+ * The "terse" quirk indicates that the underlying driver only sends events
+ * when the sensor reports a change. The HAL then periodically generates
+ * duplicate events so the sensor behaves as a continously firing one.
+ *
+ * The "noisy" quirk indicates that the underlying driver has a unusually high
+ * level of noise in its readings, and that the HAL has to accomodate it
+ * somehow, e.g. in the magnetometer calibration code path.
+ *
+ * This one is used specifically to pass a calibration scale to ALS drivers:
+ *
+ * ro.iio.illuminance.name = CPLM3218x Ambient Light Sensor
+ * ro.iio.illuminance.vendor = Capella Microsystems
+ * ro.iio.illuminance.max_range = 167000
+ * ro.iio.illuminance.resolution = 1
+ * ro.iio.illuminance.power = .001
+ * ro.iio.illuminance.illumincalib = 7400
+ *
+ * There's a 'opt_scale' specifier, documented as follows:
+ *
+ *  This adds support for a scaling factor that can be expressed
+ *  using properties, for all sensors, on a channel basis. That
+ *  scaling factor is applied after all other transforms have been
+ *  applied, and is intended as a way to compensate for problems
+ *  such as an incorrect axis polarity for a given sensor.
+ *
+ *  The syntax is <usual property prefix>.<channel>.opt_scale, e.g.
+ *  ro.iio.accel.y.opt_scale = -1 to negate the sign of the y readings
+ *  for the accelerometer.
+ *
+ *  For sensors using a single channel - and only those - the channel
+ *  name is implicitly void and a syntax such as ro.iio.illuminance.
+ *  opt_scale = 3 has to be used.
+ *
+ * 'panel' and 'rotation' specifiers can be used to express ACPI PLD placement
+ * information ; if found they will be used in priority over the actual ACPI
+ * data. That is intended as a way to verify values during development.
+ *
+ * It's possible to use the contents of the iio device name as a way to
+ * discriminate between sensors. Several sensors of the same type can coexist:
+ * e.g. ro.iio.temp.bmg160.name = BMG160 Thermometer will be used in priority
+ * over ro.iio.temp.name = BMC150 Thermometer if the sensor for which we query
+ * properties values happen to have its iio device name set to bmg160.
+ */
 
 static int sensor_get_st_prop (int s, const char* sel, char val[MAX_NAME_SIZE])
 {
        char prop_name[PROP_NAME_MAX];
        char prop_val[PROP_VALUE_MAX];
+       char extended_sel[PROP_VALUE_MAX];
+
        int i                   = sensor_info[s].catalog_index;
        const char *prefix      = sensor_catalog[i].tag;
 
+       /* First try most specialized form, like ro.iio.anglvel.bmg160.name */
+
+       snprintf(extended_sel, PROP_NAME_MAX, "%s.%s",
+                sensor_info[s].internal_name, sel);
+
+       snprintf(prop_name, PROP_NAME_MAX, PROP_BASE, prefix, extended_sel);
+
+       if (property_get(prop_name, prop_val, "")) {
+               strncpy(val, prop_val, MAX_NAME_SIZE-1);
+               val[MAX_NAME_SIZE-1] = '\0';
+               return 0;
+       }
+
+       /* Fall back to simple form, like ro.iio.anglvel.name */
+
        sprintf(prop_name, PROP_BASE, prefix, sel);
 
        if (property_get(prop_name, prop_val, "")) {
@@ -30,7 +114,19 @@ static int sensor_get_st_prop (int s, const char* sel, char val[MAX_NAME_SIZE])
 }
 
 
-static int sensor_get_fl_prop (int s, const char* sel, float* val)
+int sensor_get_prop (int s, const char* sel, int* val)
+{
+       char buf[MAX_NAME_SIZE];
+
+       if (sensor_get_st_prop(s, sel, buf))
+               return -1;
+
+       *val = atoi(buf);
+       return 0;
+}
+
+
+int sensor_get_fl_prop (int s, const char* sel, float* val)
 {
        char buf[MAX_NAME_SIZE];
 
@@ -52,7 +148,6 @@ char* sensor_get_name (int s)
        if (sensor_info[s].internal_name[0]) {
                snprintf(sensor_info[s].friendly_name, MAX_NAME_SIZE, "S%d-%s",
                         s, sensor_info[s].internal_name);
-               sensor_info[s].friendly_name[MAX_NAME_SIZE-1] = '\0';
        } else {
                sprintf(sensor_info[s].friendly_name, "S%d", s);
        }
@@ -153,3 +248,291 @@ float sensor_get_illumincalib (int s)
 
        return 0;
 }
+
+
+uint32_t sensor_get_quirks (int s)
+{
+       char quirks_buf[MAX_NAME_SIZE];
+
+       /* Read and decode quirks property on first reference */
+       if (!(sensor_info[s].quirks & QUIRK_ALREADY_DECODED)) {
+               quirks_buf[0] = '\0';
+               sensor_get_st_prop(s, "quirks", quirks_buf);
+
+               if (strstr(quirks_buf, "init-rate"))
+                       sensor_info[s].quirks |= QUIRK_INITIAL_RATE;
+
+               if (strstr(quirks_buf, "continuous")) {
+                       sensor_info[s].quirks |= QUIRK_CONTINUOUS_DRIVER;
+               }
+
+               if (strstr(quirks_buf, "terse") && !(sensor_info[s].quirks & QUIRK_CONTINUOUS_DRIVER))
+                       sensor_info[s].quirks |= QUIRK_TERSE_DRIVER;
+
+               if (strstr(quirks_buf, "noisy"))
+                       sensor_info[s].quirks |= QUIRK_NOISY;
+
+               sensor_info[s].quirks |= QUIRK_ALREADY_DECODED;
+       }
+
+       return sensor_info[s].quirks;
+}
+
+
+int sensor_get_order (int s, unsigned char map[MAX_CHANNELS])
+{
+       char buf[MAX_NAME_SIZE];
+       int i;
+       int count = sensor_catalog[sensor_info[s].catalog_index].num_channels;
+
+       if  (sensor_get_st_prop(s, "order", buf))
+               return 0; /* No order property */
+
+       /* Assume ASCII characters, in the '0'..'9' range */
+
+       for (i=0; i<count; i++)
+               if (buf[i] - '0' >= count) {
+                       ALOGE("Order index out of range for sensor %d\n", s);
+                       return 0;
+               }
+
+       for (i=0; i<count; i++)
+               map[i] = buf[i] - '0';
+
+       return 1;       /* OK to use modified ordering map */
+}
+
+char* sensor_get_string_type(int s)
+{
+       int catalog_index;
+       int sensor_type;
+
+       catalog_index = sensor_info[s].catalog_index;
+       sensor_type = sensor_catalog[catalog_index].type;
+
+       switch (sensor_type) {
+               case SENSOR_TYPE_ACCELEROMETER:
+                       return SENSOR_STRING_TYPE_ACCELEROMETER;
+
+               case SENSOR_TYPE_MAGNETIC_FIELD:
+                       return SENSOR_STRING_TYPE_MAGNETIC_FIELD;
+
+               case SENSOR_TYPE_ORIENTATION:
+                       return SENSOR_STRING_TYPE_ORIENTATION;
+
+               case SENSOR_TYPE_GYROSCOPE:
+                       return SENSOR_STRING_TYPE_GYROSCOPE;
+
+               case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
+                       return SENSOR_STRING_TYPE_GYROSCOPE_UNCALIBRATED;
+
+               case SENSOR_TYPE_LIGHT:
+                       return SENSOR_STRING_TYPE_LIGHT;
+
+               case SENSOR_TYPE_AMBIENT_TEMPERATURE:
+                       return SENSOR_STRING_TYPE_AMBIENT_TEMPERATURE;
+
+               case SENSOR_TYPE_TEMPERATURE:
+                       return SENSOR_STRING_TYPE_TEMPERATURE;
+
+               case SENSOR_TYPE_PROXIMITY:
+                       return SENSOR_STRING_TYPE_PROXIMITY;
+
+               case SENSOR_TYPE_PRESSURE:
+                       return SENSOR_STRING_TYPE_PRESSURE;
+
+               case SENSOR_TYPE_RELATIVE_HUMIDITY:
+                       return SENSOR_STRING_TYPE_RELATIVE_HUMIDITY;
+
+               default:
+                       return "";
+               }
+}
+
+flag_t sensor_get_flags (int s)
+{
+       int catalog_index;
+       int sensor_type;
+
+       flag_t flags = 0x0;
+       catalog_index = sensor_info[s].catalog_index;
+       sensor_type = sensor_catalog[catalog_index].type;
+
+       switch (sensor_type) {
+               case SENSOR_TYPE_ACCELEROMETER:
+               case SENSOR_TYPE_MAGNETIC_FIELD:
+               case SENSOR_TYPE_ORIENTATION:
+               case SENSOR_TYPE_GYROSCOPE:
+               case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
+               case SENSOR_TYPE_PRESSURE:
+                       flags |= SENSOR_FLAG_CONTINUOUS_MODE;
+                       break;
+
+               case SENSOR_TYPE_LIGHT:
+               case SENSOR_TYPE_AMBIENT_TEMPERATURE:
+               case SENSOR_TYPE_TEMPERATURE:
+               case SENSOR_TYPE_RELATIVE_HUMIDITY:
+                       flags |= SENSOR_FLAG_ON_CHANGE_MODE;
+                       break;
+
+
+               case SENSOR_TYPE_PROXIMITY:
+                       flags |= SENSOR_FLAG_WAKE_UP;
+                       flags |= SENSOR_FLAG_ON_CHANGE_MODE;
+                       break;
+
+               default:
+                       ALOGI("Unknown sensor");
+               }
+       return flags;
+}
+
+int get_cdd_freq(int s, int must)
+{
+       int catalog_index = sensor_info[s].catalog_index;
+       int sensor_type = sensor_catalog[catalog_index].type;
+
+       switch (sensor_type) {
+               case SENSOR_TYPE_ACCELEROMETER:
+                       return (must ? 100 : 200); /* must 100 Hz, should 200 Hz, CDD compliant */
+               case SENSOR_TYPE_GYROSCOPE:
+               case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED:
+                       return (must ? 200 : 200); /* must 200 Hz, should 200 Hz, CDD compliant */
+               case SENSOR_TYPE_MAGNETIC_FIELD:
+                       return (must ? 10 : 50);   /* must 10 Hz, should 50 Hz, CDD compliant */
+               case SENSOR_TYPE_LIGHT:
+               case SENSOR_TYPE_AMBIENT_TEMPERATURE:
+               case SENSOR_TYPE_TEMPERATURE:
+                       return (must ? 1 : 2);     /* must 1 Hz, should 2Hz, not mentioned in CDD */
+               default:
+                       return 0;
+       }
+}
+
+/* This value is defined only for continuous mode and on-change sensors. It is the delay between
+ * two sensor events corresponding to the lowest frequency that this sensor supports. When lower
+ * frequencies are requested through batch()/setDelay() the events will be generated at this
+ * frequency instead. It can be used by the framework or applications to estimate when the batch
+ * FIFO may be full.
+ *
+ * NOTE: 1) period_ns is in nanoseconds where as maxDelay/minDelay are in microseconds.
+ *              continuous, on-change: maximum sampling period allowed in microseconds.
+ *              one-shot, special : 0
+ *   2) maxDelay should always fit within a 32 bit signed integer. It is declared as 64 bit
+ *      on 64 bit architectures only for binary compatibility reasons.
+ * Availability: SENSORS_DEVICE_API_VERSION_1_3
+ */
+max_delay_t sensor_get_max_delay (int s)
+{
+       char avail_sysfs_path[PATH_MAX];
+       int dev_num     = sensor_info[s].dev_num;
+       char freqs_buf[100];
+       char* cursor;
+       float min_supported_rate = 1000;
+       float sr;
+
+       /* continuous, on-change: maximum sampling period allowed in microseconds.
+        * one-shot, special : 0
+        */
+       if (REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_ONE_SHOT_MODE ||
+           REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_SPECIAL_REPORTING_MODE)
+               return 0;
+
+       sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
+
+       if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) < 0) {
+               /* If poll mode sensor */
+               if (!sensor_info[s].num_channels) {
+                       /* The must rate */
+                       min_supported_rate = get_cdd_freq(s, 1);
+               }
+       } else {
+               cursor = freqs_buf;
+               while (*cursor && cursor[0]) {
+
+                       /* Decode a single value */
+                       sr = strtod(cursor, NULL);
+
+                       if (sr < min_supported_rate)
+                               min_supported_rate = sr;
+
+                       /* Skip digits */
+                       while (cursor[0] && !isspace(cursor[0]))
+                               cursor++;
+
+                       /* Skip spaces */
+                       while (cursor[0] && isspace(cursor[0]))
+                               cursor++;
+               }
+       }
+
+       /* return 0 for wrong values */
+       if (min_supported_rate < 0.1)
+               return 0;
+
+       /* Return microseconds */
+       return (max_delay_t)(1000000.0 / min_supported_rate);
+}
+
+/* this value depends on the reporting mode:
+ *
+ *   continuous: minimum sample period allowed in microseconds
+ *   on-change : 0
+ *   one-shot  :-1
+ *   special   : 0, unless otherwise noted
+ */
+int32_t sensor_get_min_delay(int s)
+{
+       char avail_sysfs_path[PATH_MAX];
+       int dev_num     = sensor_info[s].dev_num;
+       char freqs_buf[100];
+       char* cursor;
+       float max_supported_rate = 0;
+       float sr;
+
+       /* continuous: minimum sampling period allowed in microseconds.
+        * on-change, special : 0
+        * one-shot  :-1
+        */
+       if (REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_ON_CHANGE_MODE ||
+           REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_SPECIAL_REPORTING_MODE)
+               return 0;
+
+       if (REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_ONE_SHOT_MODE)
+               return -1;
+
+       sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
+
+       if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) < 0) {
+               /* If poll mode sensor */
+               if (!sensor_info[s].num_channels) {
+                       /* The should rate */
+                       max_supported_rate = get_cdd_freq(s, 0);
+               }
+       } else {
+               cursor = freqs_buf;
+               while (*cursor && cursor[0]) {
+
+                       /* Decode a single value */
+                       sr = strtod(cursor, NULL);
+
+                       if (sr > max_supported_rate && sr <= MAX_EVENTS)
+                               max_supported_rate = sr;
+
+                       /* Skip digits */
+                       while (cursor[0] && !isspace(cursor[0]))
+                               cursor++;
+
+                       /* Skip spaces */
+                       while (cursor[0] && isspace(cursor[0]))
+                               cursor++;
+               }
+       }
+
+       /* return 0 for wrong values */
+       if (max_supported_rate < 0.1)
+               return 0;
+
+       /* Return microseconds */
+       return (int32_t)(1000000.0 / max_supported_rate);
+}