OSDN Git Service

Merge branch 'lineage-16.0' of https://github.com/me176c-dev/android_hardware_iio...
[android-x86/hardware-intel-libsensors.git] / description.c
index a4dd2df..60a0b1e 100644 (file)
@@ -1,6 +1,18 @@
 /*
- * Copyright (C) 2014-2015 Intel Corporation.
- */
+// Copyright (c) 2015 Intel Corporation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+*/
 
 #include <stdlib.h>
 #include <ctype.h>
@@ -11,6 +23,7 @@
 #include "enumeration.h"
 #include "description.h"
 #include "utils.h"
+#include "transform.h"
 
 #define IIO_SENSOR_HAL_VERSION 1
 
@@ -218,6 +231,60 @@ int sensor_get_version (__attribute__((unused)) int s)
        return IIO_SENSOR_HAL_VERSION;
 }
 
+void sensor_update_max_range(int s)
+{
+       if (sensor[s].max_range)
+               return;
+
+       if (sensor[s].num_channels && sensor[s].channel[0].type_info.realbits) {
+               switch (sensor[s].type) {
+               case SENSOR_TYPE_MAGNETIC_FIELD:
+                       sensor[s].max_range = (1ULL << sensor[s].channel[0].type_info.realbits) *
+                                       CONVERT_MICROTESLA_TO_GAUSS(sensor[s].resolution) +
+                                       (sensor[s].offset || sensor[s].channel[0].offset);
+                       sensor[s].max_range = CONVERT_GAUSS_TO_MICROTESLA(sensor[s].max_range);
+                       break;
+               case SENSOR_TYPE_PROXIMITY:
+                       break;
+               default:
+                       sensor[s].max_range =  (1ULL << sensor[s].channel[0].type_info.realbits) *
+                               sensor[s].resolution + (sensor[s].offset || sensor[s].channel[0].offset);
+                       break;
+               }
+       }
+
+       if (!sensor[s].max_range) {
+               /* Try returning a sensible value given the sensor type */
+               /* We should cap returned samples accordingly... */
+               switch (sensor[s].type) {
+               case SENSOR_TYPE_ACCELEROMETER:         /* m/s^2        */
+                       sensor[s].max_range = 50;
+                       break;
+               case SENSOR_TYPE_MAGNETIC_FIELD:        /* micro-tesla  */
+                       sensor[s].max_range = 500;
+                       break;
+               case SENSOR_TYPE_ORIENTATION:           /* degrees      */
+                       sensor[s].max_range = 360;
+                       break;
+               case SENSOR_TYPE_GYROSCOPE:             /* radians/s    */
+                       sensor[s].max_range = 10;
+                       break;
+               case SENSOR_TYPE_LIGHT:                 /* SI lux units */
+                       sensor[s].max_range = 50000;
+                       break;
+               case SENSOR_TYPE_AMBIENT_TEMPERATURE:   /* °C          */
+               case SENSOR_TYPE_TEMPERATURE:           /* °C          */
+               case SENSOR_TYPE_PROXIMITY:             /* centimeters  */
+               case SENSOR_TYPE_PRESSURE:              /* hecto-pascal */
+               case SENSOR_TYPE_RELATIVE_HUMIDITY:     /* percent */
+                       sensor[s].max_range = 100;
+                       break;
+               }
+       }
+
+       if (sensor[s].max_range)
+               sensor_desc[s].maxRange = sensor[s].max_range;
+}
 
 float sensor_get_max_range (int s)
 {
@@ -236,39 +303,10 @@ float sensor_get_max_range (int s)
                !sensor_get_fl_prop(s, "max_range", &sensor[s].max_range))
                        return sensor[s].max_range;
 
-       /* Try returning a sensible value given the sensor type */
-
-       /* We should cap returned samples accordingly... */
-
-       switch (sensor_desc[s].type) {
-               case SENSOR_TYPE_ACCELEROMETER:         /* m/s^2        */
-                       return 50;
-
-               case SENSOR_TYPE_MAGNETIC_FIELD:        /* micro-tesla  */
-                       return 500;
-
-               case SENSOR_TYPE_ORIENTATION:           /* degrees      */
-                       return 360;
-
-               case SENSOR_TYPE_GYROSCOPE:             /* radians/s    */
-                       return 10;
-
-               case SENSOR_TYPE_LIGHT:                 /* SI lux units */
-                       return 50000;
-
-               case SENSOR_TYPE_AMBIENT_TEMPERATURE:   /* °C          */
-               case SENSOR_TYPE_TEMPERATURE:           /* °C          */
-               case SENSOR_TYPE_PROXIMITY:             /* centimeters  */
-               case SENSOR_TYPE_PRESSURE:              /* hecto-pascal */
-               case SENSOR_TYPE_RELATIVE_HUMIDITY:     /* percent */
-                       return 100;
-
-               default:
-                       return 0;
-               }
+       return 0;
 }
 
-static float sensor_get_min_freq (int s)
+float sensor_get_min_freq (int s)
 {
        /*
         * Check if a low cap has been specified for this sensor sampling rate.
@@ -286,7 +324,7 @@ static float sensor_get_min_freq (int s)
 }
 
 
-static float sensor_get_max_freq (int s)
+float sensor_get_max_freq (int s)
 {
        float max_freq;
 
@@ -319,10 +357,18 @@ float sensor_get_resolution (int s)
        }
 
        if (sensor[s].resolution != 0.0 ||
-               !sensor_get_fl_prop(s, "resolution", &sensor[s].resolution))
-                       return sensor[s].resolution;
+           !sensor_get_fl_prop(s, "resolution", &sensor[s].resolution)) {
+               return sensor[s].resolution;
+       }
 
-       return 0;
+       sensor[s].resolution = sensor[s].scale;
+       if (!sensor[s].resolution && sensor[s].num_channels)
+               sensor[s].resolution = sensor[s].channel[0].scale;
+
+       if (sensor[s].type == SENSOR_TYPE_MAGNETIC_FIELD)
+               sensor[s].resolution = CONVERT_GAUSS_TO_MICROTESLA(sensor[s].resolution);
+
+       return sensor[s].resolution ? : 1;
 }
 
 
@@ -433,6 +479,40 @@ int sensor_get_order (int s, unsigned char map[MAX_CHANNELS])
        return 1;       /* OK to use modified ordering map */
 }
 
+int sensor_get_available_frequencies (int s)
+{
+       int dev_num = sensor[s].dev_num, err, i;
+       const char *prefix = sensor_catalog[sensor[s].catalog_index].tag;
+       char avail_sysfs_path[PATH_MAX], freqs_buf[100];
+       char *p, *end;
+       float f;
+
+       sensor[s].avail_freqs_count = 0;
+       sensor[s].avail_freqs = 0;
+
+       sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
+
+       err = sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf));
+       if (err < 0) {
+               sprintf(avail_sysfs_path, SENSOR_AVAIL_FREQ_PATH, dev_num, prefix);
+               err = sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf));
+               if (err < 0)
+                       return 0;
+       }
+
+       for (p = freqs_buf, f = strtof(p, &end); p != end; p = end, f = strtof(p, &end))
+               sensor[s].avail_freqs_count++;
+
+       if (sensor[s].avail_freqs_count) {
+               sensor[s].avail_freqs = (float*) calloc(sensor[s].avail_freqs_count, sizeof(float));
+
+               for (p = freqs_buf, f = strtof(p, &end), i = 0; p != end; p = end, f = strtof(p, &end), i++)
+                       sensor[s].avail_freqs[i] = f;
+       }
+
+       return 0;
+}
+
 int sensor_get_mounting_matrix (int s, float mm[9])
 {
        int dev_num = sensor[s].dev_num, err, i;
@@ -577,7 +657,7 @@ static int get_cdd_freq (int s, int must)
        }
 }
 
-/* 
+/*
  * 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. maxDelay should always fit within a 32 bit signed integer. It is declared as
@@ -585,13 +665,9 @@ static int get_cdd_freq (int s, int must)
  */
 max_delay_t sensor_get_max_delay (int s)
 {
-       char avail_sysfs_path[PATH_MAX];
-       int dev_num     = sensor[s].dev_num;
-       char freqs_buf[100];
-       char* cursor;
+       int i;
        float min_supported_rate;
        float rate_cap;
-       float sr;
 
        /*
         * continuous, on-change: maximum sampling period allowed in microseconds.
@@ -622,32 +698,14 @@ max_delay_t sensor_get_max_delay (int s)
        switch (sensor[s].mode) {
                case MODE_TRIGGER:
                        /* For interrupt-based devices, obey the list of supported sampling rates */
-                       sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
-                       if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) > 0) {
-
+                       if (sensor[s].avail_freqs_count) {
                                min_supported_rate = 1000;
-                               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++;
+                               for (i = 0; i < sensor[s].avail_freqs_count; i++) {
+                                       if (sensor[s].avail_freqs[i] < min_supported_rate)
+                                               min_supported_rate = sensor[s].avail_freqs[i];
                                }
-
                                break;
                        }
-
                        /* Fall through ... */
 
                default:
@@ -670,16 +728,24 @@ max_delay_t sensor_get_max_delay (int s)
        return (max_delay_t) (1000000.0 / min_supported_rate);
 }
 
+float sensor_get_max_static_freq(int s)
+{
+       float max_from_prop = sensor_get_max_freq(s);
+
+       /* If we have max specified via a property use it */
+       if (max_from_prop != ANDROID_MAX_FREQ) {
+               return max_from_prop;
+       } else {
+               /* The should rate */
+               return get_cdd_freq(s, 0);
+       }
+}
 
 int32_t sensor_get_min_delay (int s)
 {
-       char avail_sysfs_path[PATH_MAX];
-       int dev_num     = sensor[s].dev_num;
-       char freqs_buf[100];
-       char* cursor;
+       int i;
        float max_supported_rate = 0;
        float max_from_prop = sensor_get_max_freq(s);
-       float sr;
 
        /* continuous, on change: minimum sampling period allowed in microseconds.
         * special : 0, unless otherwise noted
@@ -709,9 +775,7 @@ int32_t sensor_get_min_delay (int s)
                }
        }
 
-       sprintf(avail_sysfs_path, DEVICE_AVAIL_FREQ_PATH, dev_num);
-
-       if (sysfs_read_str(avail_sysfs_path, freqs_buf, sizeof(freqs_buf)) < 0) {
+       if (!sensor[s].avail_freqs_count) {
                if (sensor[s].mode == MODE_POLL) {
                        /* If we have max specified via a property use it */
                        if (max_from_prop != ANDROID_MAX_FREQ)
@@ -721,22 +785,11 @@ int32_t sensor_get_min_delay (int s)
                                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_from_prop)
-                               max_supported_rate = sr;
-
-                       /* Skip digits */
-                       while (cursor[0] && !isspace(cursor[0]))
-                               cursor++;
-
-                       /* Skip spaces */
-                       while (cursor[0] && isspace(cursor[0]))
-                               cursor++;
+               for (i = 0; i < sensor[s].avail_freqs_count; i++) {
+                       if (sensor[s].avail_freqs[i] > max_supported_rate &&
+                               sensor[s].avail_freqs[i] <= max_from_prop) {
+                               max_supported_rate = sensor[s].avail_freqs[i];
+                       }
                }
        }