/*
- * 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>
#include "enumeration.h"
#include "description.h"
#include "utils.h"
+#include "transform.h"
#define IIO_SENSOR_HAL_VERSION 1
int i = sensor[s].catalog_index;
const char *prefix = sensor_catalog[i].tag;
+ const char *shorthand = sensor_catalog[i].shorthand;
/* First try most specialized form, like ro.iio.anglvel.bmg160.name */
return 0;
}
+ if (shorthand[0] != '\0') {
+ /* Try with shorthand instead of prefix */
+ snprintf(prop_name, PROP_NAME_MAX, PROP_BASE, shorthand, 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 */
snprintf(prop_name, PROP_NAME_MAX, PROP_BASE, prefix, sel);
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)
{
!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[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.
}
-static float sensor_get_max_freq (int s)
+float sensor_get_max_freq (int s)
{
float max_freq;
}
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;
}
if (strstr(quirks_buf, "no-poll"))
sensor[s].quirks |= QUIRK_NO_POLL_MODE;
+ if (strstr(quirks_buf, "hrtimer"))
+ sensor[s].quirks |= QUIRK_HRTIMER;
+
+ if (strstr(quirks_buf, "secondary"))
+ sensor[s].quirks |= QUIRK_SECONDARY;
+
sensor[s].quirks |= QUIRK_ALREADY_DECODED;
}
return 1; /* OK to use modified ordering map */
}
+int sensor_get_available_frequencies (int s)
+{
+ int dev_num = sensor[s].dev_num, err, i;
+ 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)
+ 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;
+ char mm_path[PATH_MAX], mm_buf[100];
+ char *tmp1 = mm_buf, *tmp2;
+
+ switch (sensor[s].type) {
+ case SENSOR_TYPE_ACCELEROMETER:
+ case SENSOR_TYPE_MAGNETIC_FIELD:
+ case SENSOR_TYPE_GYROSCOPE:
+ case SENSOR_TYPE_PROXIMITY:
+ break;
+ default:
+ return 0;
+ }
+
+ sprintf(mm_path, MOUNTING_MATRIX_PATH, dev_num);
+
+ err = sysfs_read_str(mm_path, mm_buf, sizeof(mm_buf));
+ if (err < 0)
+ return 0;
+
+ for(i = 0; i < 9; i++) {
+ float f;
+
+ f = strtof(tmp1, &tmp2);
+ if (!f && tmp1 == tmp2)
+ return 0;
+ mm[i] = f;
+ tmp1 = tmp2 + 1;
+ }
+
+ /*
+ * For proximity sensors, interpret a negative final z value as a hint that the sensor is back mounted. In that case, mark the sensor as secondary to
+ * ensure that it gets listed after other sensors of same type that would be front-mounted. Most applications will only ask for the default proximity
+ * sensor and it makes more sense to point to, say, the IR based proximity sensor rather than SAR based one if we have both, as on SoFIA LTE MRD boards.
+ */
+ if (sensor[s].type == SENSOR_TYPE_PROXIMITY) {
+ if (mm[8] < 0) {
+ sensor[s].quirks |= QUIRK_SECONDARY;
+ }
+ return 0;
+ }
+
+ ALOGI("%s: %f %f %f %f %f %f %f %f %f\n", __func__, mm[0], mm[1], mm[2], mm[3], mm[4], mm[5], mm[6], mm[7], mm[8]);
+ return 1;
+}
+
char* sensor_get_string_type (int s)
{
- switch (sensor[s].type) {
+ switch (sensor_desc[s].type) {
case SENSOR_TYPE_ACCELEROMETER:
return SENSOR_STRING_TYPE_ACCELEROMETER;
{
flag_t flags = 0;
- switch (sensor[s].type) {
+ switch (sensor_desc[s].type) {
case SENSOR_TYPE_LIGHT:
case SENSOR_TYPE_AMBIENT_TEMPERATURE:
case SENSOR_TYPE_TEMPERATURE:
static int get_cdd_freq (int s, int must)
{
- switch (sensor[s].type) {
+ switch (sensor_desc[s].type) {
case SENSOR_TYPE_ACCELEROMETER:
return (must ? 100 : 200); /* must 100 Hz, should 200 Hz, CDD compliant */
}
}
-/*
+/*
* 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
*/
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;
- float min_supported_rate = 1000;
+ int i;
+ float min_supported_rate;
float rate_cap;
- float sr;
/*
* continuous, on-change: maximum sampling period allowed in microseconds.
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 (sensor[s].mode == MODE_POLL) {
- /* 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;
+ switch (sensor[s].mode) {
+ case MODE_TRIGGER:
+ /* For interrupt-based devices, obey the list of supported sampling rates */
+ if (sensor[s].avail_freqs_count) {
+ min_supported_rate = 1000;
+ 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 ... */
- /* Skip digits */
- while (cursor[0] && !isspace(cursor[0]))
- cursor++;
-
- /* Skip spaces */
- while (cursor[0] && isspace(cursor[0]))
- cursor++;
- }
+ default:
+ /* Report 1 Hz */
+ min_supported_rate = 1;
+ break;
}
/* Check if a minimum rate was specified for this sensor */
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
}
}
- 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)
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];
+ }
}
}