From cc449e6326bca72150aa0b94a8dd18d185f3aeea Mon Sep 17 00:00:00 2001 From: Patrick Porlan Date: Thu, 18 Dec 2014 11:40:04 +0100 Subject: [PATCH] Winter cleanup: switch to 160 characters lines This allows for nicer traces, comments, and test conditions. Change-Id: Ie477ab33d090bce0ffecf252e13885f384909771 Signed-off-by: Patrick Porlan --- common.h | 119 +++++++------------ control.c | 403 ++++++++++++++++++++------------------------------------------ 2 files changed, 175 insertions(+), 347 deletions(-) diff --git a/common.h b/common.h index 1d40842..f83cd79 100644 --- a/common.h +++ b/common.h @@ -25,15 +25,15 @@ #define DEVICE_AVAIL_FREQ_PATH BASE_PATH "sampling_frequency_available" #define ILLUMINATION_CALIBPATH BASE_PATH "in_illuminance_calibscale" -#define PROP_BASE "ro.iio.%s.%s" /* Note: PROPERTY_KEY_MAX is small */ +#define PROP_BASE "ro.iio.%s.%s" /* Note: PROPERTY_KEY_MAX is small */ -#define MAX_TYPE_SPEC_LEN 32 /* Channel type spec len; ex: "le:u10/16>>0" */ +#define MAX_TYPE_SPEC_LEN 32 /* Channel type spec len; ex: "le:u10/16>>0" */ #define MAX_SENSOR_REPORT_SIZE 32 /* Sensor report buffer size */ #define MAX_DEVICE_REPORT_SIZE 32 /* iio device scan buffer size */ #define MAX_NAME_SIZE 32 -#define MAX_SENSOR_BASES 3 /* Max number of base sensors a sensor can rely on */ +#define MAX_SENSOR_BASES 3 /* Max number of base sensors a sensor can rely on */ #define ARRAY_SIZE(x) sizeof(x)/sizeof(x[0]) #define REPORTING_MODE(x) ((x) & 0x06) @@ -66,10 +66,10 @@ channel_descriptor_t; typedef struct { - const char *tag; /* Prefix such as "accel", "gyro", "temp"... */ - const int type; /* Sensor type ; ex: SENSOR_TYPE_ACCELEROMETER */ + const char *tag; /* Prefix such as "accel", "gyro", "temp"... */ + const int type; /* Sensor type ; ex: SENSOR_TYPE_ACCELEROMETER */ const int num_channels; /* Expected iio channels for this sensor */ - const int is_virtual; /* Is the sensor virtual or not */ + const int is_virtual; /* Is the sensor virtual or not */ channel_descriptor_t channel[MAX_CHANNELS]; } sensor_catalog_entry_t; @@ -88,15 +88,14 @@ datum_info_t; typedef struct { - int offset; /* Offset in bytes within the iio character device report */ - int size; /* Field size in bytes */ - float scale; /* scale for each channel */ - char type_spec[MAX_TYPE_SPEC_LEN]; /* From driver; ex: le:u10/16>>0 */ - datum_info_t type_info; /* Decoded contents of type spec */ - float opt_scale; /* Optional correction scale read from a property such - * as iio.accel.x.scale, allowing late compensation of - * problems such as misconfigured axes ; set to 1 by - * default. Applied at the end of the scaling process. + int offset; /* Offset in bytes within the iio character device report */ + int size; /* Field size in bytes */ + float scale; /* scale for each channel */ + char type_spec[MAX_TYPE_SPEC_LEN]; /* From driver; ex: le:u10/16>>0 */ + datum_info_t type_info; /* Decoded contents of type spec */ + float opt_scale; /* + * Optional correction scale read from a property such as iio.accel.x.scale, allowing late compensation of + * problems such as misconfigured axes ; set to 1 by default. Applied at the end of the scaling process. */ } channel_info_t; @@ -135,57 +134,45 @@ typedef struct float power; /* - * Currently active trigger - either a pointer to the initial (default) - * trigger name buffer, or a pointer to the motion trigger name buffer, - * or something else (typically NULL or a pointer to some static "\n". - * This is used to determine if the conditions are met to switch from - * the default trigger to the motion trigger for a sensor, or rather for - * the interrupt-driven sensors associated to a given iio device. + * Currently active trigger - either a pointer to the initial (default) trigger name buffer, or a pointer to the motion trigger name buffer, + * or something else (typically NULL or a pointer to some static "\n". This is used to determine if the conditions are met to switch from + * the default trigger to the motion trigger for a sensor, or rather for the interrupt-driven sensors associated to a given iio device. */ const char* selected_trigger; - float offset; /* (cooked = raw + offset) * scale */ - float scale; /*default:1. when set to 0, use channel specific value*/ - float illumincalib; /* to set the calibration for the ALS */ + float offset; /* (cooked = raw + offset) * scale */ + float scale; /* default:1. when set to 0, use channel specific value */ + float illumincalib; /* to set the calibration for the ALS */ - float requested_rate; /* requested events / second */ - float sampling_rate; /* setup events / second */ + float requested_rate; /* requested events / second */ + float sampling_rate; /* setup events / second */ - int dev_num; /* Associated iio dev num, ex: 3 for /dev/iio:device3 */ + int dev_num; /* Associated iio dev num, ex: 3 for /dev/iio:device3 */ - int catalog_index;/* Associated entry within the sensor_catalog array */ - int type; /* Sensor type, such as SENSOR_TYPE_GYROSCOPE */ + int catalog_index; /* Associated entry within the sensor_catalog array */ + int type; /* Sensor type, such as SENSOR_TYPE_GYROSCOPE */ - int num_channels; /* Actual channel count ; 0 for poll mode sensors */ + int num_channels; /* Actual channel count ; 0 for poll mode sensors */ /* - * The array below indicates where to gather report data for this - * sensor inside the reports that we read from the iio character device. - * It is updated whenever channels are enabled or disabled on the same - * device. Channel size indicates the size in bytes of fields, and - * should be zero for disabled channels. The type field indicates how a - * specific channel data item is structured. + * The array below indicates where to gather report data for this sensor inside the reports that we read from the iio character device. + * It is updated whenever channels are enabled or disabled on the same device. Channel size indicates the size in bytes of fields, and + * should be zero for disabled channels. The type field indicates how a specific channel data item is structured. */ channel_info_t channel[MAX_CHANNELS]; /* - * This flag is set if we acquired data from the sensor but did not - * forward it to upper layers (i.e. Android) yet. If so, report_buffer - * contains that data. Valid values are 0: empty, 1: normal, 2: repeat - * of last acquired value after timeout. + * This flag is set if we acquired data from the sensor but did not forward it to upper layers (i.e. Android) yet. If so, report_buffer + * contains that data. Valid values are 0: empty, 1: normal, 2: repeat of last acquired value after timeout. */ int report_pending; - /* - * This flag is set if we have a meta data event pending - */ + /* This flag is set if we have a meta data event pending */ int meta_data_pending; /* - * Timestamp closely matching the date of sampling, preferably retrieved - * from a iio channel alongside sample data. Value zero indicates that - * we couldn't get such a closely correlated timestamp, and that one - * has to be generated before the report gets sent up to Android. + * Timestamp closely matching the date of sampling, preferably retrieved from a iio channel alongside sample data. Value zero indicates that + * we couldn't get such a closely correlated timestamp, and that one has to be generated before the report gets sent up to Android. */ int64_t report_ts; @@ -201,8 +188,7 @@ typedef struct int cal_level; /* 0 means not calibrated */ /* - * Depending on the sensor, calibration may take too much time at - * higher levels. Allow optional capping to a certain level. + * Depending on the sensor, calibration may take too much time at higher levels. Allow optional capping to a certain level. */ int max_cal_level; @@ -214,47 +200,35 @@ typedef struct float prev_val; /* Previously reported value, for on-change sensors */ /* - * Certain sensors expose their readings through sysfs files that have - * a long response time (100-200 ms for ALS). Rather than block our - * global control loop for several hundred ms each second, offload those - * lengthy blocking reads to dedicated threads, which will then report + * Certain sensors expose their readings through sysfs files that have a long response time (100-200 ms for ALS). Rather than block our + * global control loop for several hundred ms each second, offload those lengthy blocking reads to dedicated threads, which will then report * their data through a fd that we can add to our poll fd set. */ int thread_data_fd[2]; pthread_t acquisition_thread; - /* How many base sensors is the sensor depending on */ - int base_count; + int base_count; /* How many base sensors is the sensor depending on */ int base[MAX_SENSOR_BASES]; uint32_t quirks; /* Bit mask expressing the need for special tweaks */ /* Note: we may have to explicitely serialize access to some fields */ - int is_virtual; - /* - * Dependency count - for a real sensor how many active virtual sensors are - * depending on it - */ - uint32_t ref_count; + int is_virtual; /* Composite sensor, exposed from data acquired through other sensors */ - /* - * Flag showing if a sensor was enabled directly by Android - */ - uint32_t directly_enabled; + uint32_t ref_count; /* Dependency count - for a real sensor how many active virtual sensors are depending on it */ + + uint32_t directly_enabled; /* Flag showing if a sensor was enabled directly by Android */ /* - * Current sample for a virtual sensor - when a report is ready we'll - * keep the data here until it's finally processed. Can be modified for + * Current sample for a virtual sensor - when a report is ready we'll keep the data here until it's finally processed. Can be modified for * more than one at a later time. */ sensors_event_t sample; /* - * If the QUIRK_FIELD_ORDERING bit is set in quirks, the contents of - * this array are used in the finalization stage to swap sample fields - * before transmitting them to Android ; they form a mapping between - * the indices of the input and output arrays: ex: 0123 is identity for + * If the QUIRK_FIELD_ORDERING bit is set in quirks, the contents of this array are used in the finalization stage to swap sample fields + * before transmitting them to Android ; they form a mapping between the indices of the input and output arrays: ex: 0123 is identity for * a sample containing 4 fields. */ unsigned char order[MAX_CHANNELS]; @@ -267,8 +241,7 @@ typedef struct int history_index; /* Index of sample to evict next time */ /* - * Event counter - will be used to check if we have a significant sample - * for noisy sensors. We want to make sure we do not send any wrong + * Event counter - will be used to check if we have a significant sample for noisy sensors. We want to make sure we do not send any wrong * events before filtering kicks in. We can also use it for statistics. */ uint64_t event_count; diff --git a/control.c b/control.c index 769ba0d..2362428 100644 --- a/control.c +++ b/control.c @@ -21,13 +21,13 @@ #include "filtering.h" /* Currently active sensors count, per device */ -static int poll_sensors_per_dev[MAX_DEVICES]; /* poll-mode sensors */ -static int trig_sensors_per_dev[MAX_DEVICES]; /* trigger, event based */ +static int poll_sensors_per_dev[MAX_DEVICES]; /* poll-mode sensors */ +static int trig_sensors_per_dev[MAX_DEVICES]; /* trigger, event based */ -static int device_fd[MAX_DEVICES]; /* fd on the /dev/iio:deviceX file */ -static int has_iio_ts[MAX_DEVICES]; /* ts channel available on this iio dev */ -static int expected_dev_report_size[MAX_DEVICES]; /* expected iio scan len */ -static int poll_fd; /* epoll instance covering all enabled sensors */ +static int device_fd[MAX_DEVICES]; /* fd on the /dev/iio:deviceX file */ +static int has_iio_ts[MAX_DEVICES]; /* ts channel available on this iio dev */ +static int expected_dev_report_size[MAX_DEVICES]; /* expected iio scan len */ +static int poll_fd; /* epoll instance covering all enabled sensors */ static int active_poll_sensors; /* Number of enabled poll-mode sensors */ @@ -40,8 +40,7 @@ static pthread_mutex_t thread_release_mutex [MAX_SENSORS]; * We associate tags to each of our poll set entries. These tags have the * following values: * - a iio device number if the fd is a iio character device fd - * - THREAD_REPORT_TAG_BASE + sensor handle if the fd is the receiving end of a - * pipe used by a sysfs data acquisition thread + * - THREAD_REPORT_TAG_BASE + sensor handle if the fd is the receiving end of a pipe used by a sysfs data acquisition thread */ #define THREAD_REPORT_TAG_BASE 0x00010000 @@ -59,38 +58,29 @@ static int check_state_change (int s, int enabled, int from_virtual) { if (enabled) { if (sensor[s].directly_enabled) - /* - * We're being enabled but already were - * directly activated: no change. - */ - return 0; + return 0; /* We're being enabled but already were directly activated: no change. */ if (!from_virtual) - /* We're being directly enabled */ - sensor[s].directly_enabled = 1; + sensor[s].directly_enabled = 1; /* We're being directly enabled */ if (sensor[s].ref_count) - /* We were already indirectly enabled */ - return 0; + return 0; /* We were already indirectly enabled */ - return 1; /* Do continue enabling this sensor */ + return 1; /* Do continue enabling this sensor */ } if (!is_enabled(s)) - /* We are being disabled but already were: no change */ - return 0; + return 0; /* We are being disabled but already were: no change */ if (from_virtual && sensor[s].directly_enabled) - /* We're indirectly disabled but the base is still active */ - return 0; + return 0; /* We're indirectly disabled but the base is still active */ - /* We're now directly disabled */ - sensor[s].directly_enabled = 0; + sensor[s].directly_enabled = 0; /* We're now directly disabled */ if (!from_virtual && sensor[s].ref_count) - return 0; /* We still have ref counts */ + return 0; /* We still have ref counts */ - return 1; /* Do continue disabling this sensor */ + return 1; /* Do continue disabling this sensor */ } @@ -134,8 +124,7 @@ static int setup_trigger (int s, const char* trigger_val) sprintf(sysfs_path, TRIGGER_PATH, sensor[s].dev_num); if (trigger_val[0] != '\n') - ALOGI("Setting S%d (%s) trigger to %s\n", s, - sensor[s].friendly_name, trigger_val); + ALOGI("Setting S%d (%s) trigger to %s\n", s, sensor[s].friendly_name, trigger_val); while (ret == -1 && attempts) { ret = sysfs_write_str(sysfs_path, trigger_val); @@ -145,8 +134,7 @@ static int setup_trigger (int s, const char* trigger_val) if (ret != -1) sensor[s].selected_trigger = trigger_val; else - ALOGE("Setting S%d (%s) trigger to %s FAILED.\n", s, - sensor[s].friendly_name, trigger_val); + ALOGE("Setting S%d (%s) trigger to %s FAILED.\n", s, sensor[s].friendly_name, trigger_val); return ret; } @@ -206,17 +194,13 @@ static int decode_type_spec (const char type_buf[MAX_TYPE_SPEC_LEN], /* Valid specs: "le:u10/16>>0", "le:s16/32>>0" or "le:s32/32>>0" */ - tokens = sscanf(type_buf, "%ce:%c%u/%u>>%u", - &endianness, &sign, &realbits, &storagebits, &shift); + tokens = sscanf(type_buf, "%ce:%c%u/%u>>%u", &endianness, &sign, &realbits, &storagebits, &shift); - if (tokens != 5 || - (endianness != 'b' && endianness != 'l') || - (sign != 'u' && sign != 's') || - realbits > storagebits || - (storagebits != 16 && storagebits != 32 && storagebits != 64)) { + if (tokens != 5 || (endianness != 'b' && endianness != 'l') || (sign != 'u' && sign != 's') || + realbits > storagebits || (storagebits != 16 && storagebits != 32 && storagebits != 64)) { ALOGE("Invalid iio channel type spec: %s\n", type_buf); return -1; - } + } type_info->endianness = endianness; type_info->sign = sign; @@ -231,16 +215,11 @@ static int decode_type_spec (const char type_buf[MAX_TYPE_SPEC_LEN], void build_sensor_report_maps (int dev_num) { /* - * Read sysfs files from a iio device's scan_element directory, and - * build a couple of tables from that data. These tables will tell, for - * each sensor, where to gather relevant data in a device report, i.e. - * the structure that we read from the /dev/iio:deviceX file in order to - * sensor report, itself being the data that we return to Android when a - * sensor poll completes. The mapping should be straightforward in the - * case where we have a single sensor active per iio device but, this is - * not the general case. In general several sensors can be handled - * through a single iio device, and the _en, _index and _type syfs - * entries all concur to paint a picture of what the structure of the + * Read sysfs files from a iio device's scan_element directory, and build a couple of tables from that data. These tables will tell, for + * each sensor, where to gather relevant data in a device report, i.e. the structure that we read from the /dev/iio:deviceX file in order to + * sensor report, itself being the data that we return to Android when a sensor poll completes. The mapping should be straightforward in the + * case where we have a single sensor active per iio device but, this is not the general case. In general several sensors can be handled + * through a single iio device, and the _en, _index and _type syfs entries all concur to paint a picture of what the structure of the * device report is. */ @@ -273,18 +252,14 @@ void build_sensor_report_maps (int dev_num) for (c=0; c= MAX_SENSORS) { ALOGE("Index out of bounds!: %s\n", sysfs_path); @@ -327,9 +299,7 @@ void build_sensor_report_maps (int dev_num) /* Turn on channels we're aware of */ for (c=0;c MAX_DEVICE_REPORT_SIZE) { - ALOGE("Unexpectedly large scan buffer on iio dev%d: %d bytes\n", - dev_num, expected_dev_report_size[dev_num]); + ALOGE("Unexpectedly large scan buffer on iio dev%d: %d bytes\n", dev_num, expected_dev_report_size[dev_num]); expected_dev_report_size[dev_num] = MAX_DEVICE_REPORT_SIZE; } @@ -394,12 +358,10 @@ int adjust_counters (int s, int enabled, int from_virtual) int dev_num = sensor[s].dev_num; if (!check_state_change(s, enabled, from_virtual)) - /* The state of the sensor remains the same: we're done */ - return 0; + return 0; /* The state of the sensor remains the same: we're done */ if (enabled) { - ALOGI("Enabling sensor %d (iio device %d: %s)\n", - s, dev_num, sensor[s].friendly_name); + ALOGI("Enabling sensor %d (iio device %d: %s)\n", s, dev_num, sensor[s].friendly_name); switch (sensor[s].type) { case SENSOR_TYPE_MAGNETIC_FIELD: @@ -411,8 +373,7 @@ int adjust_counters (int s, int enabled, int from_virtual) break; } } else { - ALOGI("Disabling sensor %d (iio device %d: %s)\n", s, dev_num, - sensor[s].friendly_name); + ALOGI("Disabling sensor %d (iio device %d: %s)\n", s, dev_num, sensor[s].friendly_name); /* Sensor disabled, lower report available flag */ sensor[s].report_pending = 0; @@ -479,12 +440,9 @@ static int get_field_count (int s) static void* acquisition_routine (void* param) { /* - * Data acquisition routine run in a dedicated thread, covering a single - * sensor. This loop will periodically retrieve sampling data through - * sysfs, then package it as a sample and transfer it to our master poll - * loop through a report fd. Checks for a cancellation signal quite - * frequently, as the thread may be disposed of at any time. Note that - * Bionic does not provide pthread_cancel / pthread_testcancel... + * Data acquisition routine run in a dedicated thread, covering a single sensor. This loop will periodically retrieve sampling data through + * sysfs, then package it as a sample and transfer it to our master poll loop through a report fd. Checks for a cancellation signal quite + * frequently, as the thread may be disposed of at any time. Note that Bionic does not provide pthread_cancel / pthread_testcancel... */ int s = (int) (size_t) param; @@ -500,12 +458,10 @@ static void* acquisition_routine (void* param) return NULL; } - ALOGI("Entering data acquisition thread S%d (%s), rate:%g\n", - s, sensor[s].friendly_name, sensor[s].sampling_rate); + ALOGI("Entering data acquisition thread S%d (%s), rate:%g\n", s, sensor[s].friendly_name, sensor[s].sampling_rate); if (sensor[s].sampling_rate <= 0) { - ALOGE("Invalid rate in acquisition routine for sensor %d: %g\n", - s, sensor[s].sampling_rate); + ALOGE("Invalid rate in acquisition routine for sensor %d: %g\n", s, sensor[s].sampling_rate); return NULL; } @@ -513,10 +469,8 @@ static void* acquisition_routine (void* param) sample_size = sizeof(int64_t) + num_fields * sizeof(float); /* - * Each condition variable is associated to a mutex that has to be - * locked by the thread that's waiting on it. We use these condition - * variables to get the acquisition threads out of sleep quickly after - * the sampling rate is adjusted, or the sensor is disabled. + * Each condition variable is associated to a mutex that has to be locked by the thread that's waiting on it. We use these condition + * variables to get the acquisition threads out of sleep quickly after the sampling rate is adjusted, or the sensor is disabled. */ pthread_mutex_lock(&thread_release_mutex[s]); @@ -540,23 +494,19 @@ static void* acquisition_routine (void* param) if (sensor[s].ops.finalize(s, &data)) { /* Pipe it for transmission to poll loop */ - ret = write( sensor[s].thread_data_fd[1], - &data.timestamp, sample_size); + ret = write(sensor[s].thread_data_fd[1], &data.timestamp, sample_size); if (ret != sample_size) - ALOGE("S%d write failure: wrote %d, got %d\n", - s, sample_size, ret); + ALOGE("S%d write failure: wrote %d, got %d\n", s, sample_size, ret); } /* Check and honor termination requests */ if (sensor[s].thread_data_fd[1] == -1) goto exit; - /* Recalculate period asumming sensor[s].sampling_rate - * can be changed dynamically during the thread run */ + /* Recalculate period assuming sensor[s].sampling_rate can be changed dynamically during the thread run */ if (sensor[s].sampling_rate <= 0) { - ALOGE("Unexpected sampling rate for sensor %d: %g\n", - s, sensor[s].sampling_rate); + ALOGE("Unexpected sampling rate for sensor %d: %g\n", s, sensor[s].sampling_rate); goto exit; } @@ -564,13 +514,9 @@ static void* acquisition_routine (void* param) timestamp += period; set_timestamp(&target_time, timestamp); - /* - * Wait until the sampling time elapses, or a rate change is - * signaled, or a thread exit is requested. + /* Wait until the sampling time elapses, or a rate change is signaled, or a thread exit is requested. */ - ret = pthread_cond_timedwait( &thread_release_cond[s], - &thread_release_mutex[s], - &target_time); + ret = pthread_cond_timedwait(&thread_release_cond[s], &thread_release_mutex[s], &target_time); } exit: @@ -608,10 +554,7 @@ static void start_acquisition_thread (int s) ret = epoll_ctl(poll_fd, EPOLL_CTL_ADD, incoming_data_fd , &ev); /* Create and start worker thread */ - ret = pthread_create( &sensor[s].acquisition_thread, - NULL, - acquisition_routine, - (void*) (size_t) s); + ret = pthread_create( &sensor[s].acquisition_thread, NULL, acquisition_routine, (void*) (size_t) s); } @@ -668,18 +611,13 @@ static int is_fast_accelerometer (int s) static void tentative_switch_trigger (int s) { /* - * Under certain situations it may be beneficial to use an alternate - * trigger: + * Under certain situations it may be beneficial to use an alternate trigger: * - * - for applications using the accelerometer with high sampling rates, - * prefer the continuous trigger over the any-motion one, to avoid + * - for applications using the accelerometer with high sampling rates, prefer the continuous trigger over the any-motion one, to avoid * jumps related to motion thresholds */ - if (is_fast_accelerometer(s) && - !(sensor[s].quirks & QUIRK_TERSE_DRIVER) && - sensor[s].selected_trigger == - sensor[s].motion_trigger_name) + if (is_fast_accelerometer(s) && !(sensor[s].quirks & QUIRK_TERSE_DRIVER) && sensor[s].selected_trigger == sensor[s].motion_trigger_name) setup_trigger(s, sensor[s].init_trigger_name); } @@ -695,21 +633,13 @@ static float get_group_max_sampling_rate (int s) if (is_enabled(s)) arbitrated_rate = sensor[s].requested_rate; - /* - * If any of the currently active sensors built on top of this one need - * a higher sampling rate, switch to this rate. - */ + /* If any of the currently active sensors built on top of this one need a higher sampling rate, switch to this rate */ for (i = 0; i < sensor_count; i++) for (vi = 0; vi < sensor[i].base_count; vi++) - /* If sensor i depends on sensor s */ - if (sensor[i].base[vi] == s && is_enabled(i) && - sensor[i].requested_rate > arbitrated_rate) + if (sensor[i].base[vi] == s && is_enabled(i) && sensor[i].requested_rate > arbitrated_rate) /* If sensor i depends on sensor s */ arbitrated_rate = sensor[i].requested_rate; - /* - * If any of the currently active sensors we rely on is using a higher - * sampling rate, switch to this rate. - */ + /* If any of the currently active sensors we rely on is using a higher sampling rate, switch to this rate */ for (vi = 0; vi < sensor[s].base_count; vi++) { i = sensor[s].base[vi]; if (is_enabled(i) && sensor[i].requested_rate > arbitrated_rate) @@ -722,8 +652,7 @@ static float get_group_max_sampling_rate (int s) static int sensor_set_rate (int s, float requested_rate) { - /* Set the rate at which a specific sensor should report events */ - /* See Android sensors.h for indication on sensor trigger modes */ + /* Set the rate at which a specific sensor should report events. See Android sensors.h for indication on sensor trigger modes */ char sysfs_path[PATH_MAX]; char avail_sysfs_path[PATH_MAX]; @@ -735,8 +664,7 @@ static int sensor_set_rate (int s, float requested_rate) int32_t min_delay_us = sensor_desc[s].minDelay; max_delay_t max_delay_us = sensor_desc[s].maxDelay; float min_supported_rate = max_delay_us ? 1000000.0/max_delay_us : 1; - float max_supported_rate = - min_delay_us && min_delay_us != -1 ? 1000000.0/min_delay_us : 0; + float max_supported_rate = min_delay_us && min_delay_us != -1 ? 1000000.0/min_delay_us : 0; char freqs_buf[100]; char* cursor; int n; @@ -745,18 +673,14 @@ static int sensor_set_rate (int s, float requested_rate) float cur_sampling_rate; /* Currently used sampling rate */ float arb_sampling_rate; /* Granted sampling rate after arbitration */ - ALOGV("Sampling rate %g requested on sensor %d (%s)\n", requested_rate, - s, sensor[s].friendly_name); + ALOGV("Sampling rate %g requested on sensor %d (%s)\n", requested_rate, s, sensor[s].friendly_name); sensor[s].requested_rate = requested_rate; arb_sampling_rate = requested_rate; if (arb_sampling_rate < min_supported_rate) { - ALOGV("Sampling rate %g too low for %s, using %g instead\n", - arb_sampling_rate, sensor[s].friendly_name, - min_supported_rate); - + ALOGV("Sampling rate %g too low for %s, using %g instead\n", arb_sampling_rate, sensor[s].friendly_name, min_supported_rate); arb_sampling_rate = min_supported_rate; } @@ -764,14 +688,12 @@ static int sensor_set_rate (int s, float requested_rate) group_max_sampling_rate = get_group_max_sampling_rate(s); if (arb_sampling_rate < group_max_sampling_rate) { - ALOGV("Using %s sampling rate to %g too due to dependency\n", - sensor[s].friendly_name, arb_sampling_rate); + ALOGV("Using %s sampling rate to %g too due to dependency\n", sensor[s].friendly_name, arb_sampling_rate); arb_sampling_rate = group_max_sampling_rate; } if (max_supported_rate && arb_sampling_rate > max_supported_rate) { - ALOGV("Sampling rate %g too high for %s, using %g instead\n", - arb_sampling_rate, sensor[s].friendly_name, max_supported_rate); + ALOGV("Sampling rate %g too high for %s, using %g instead\n", arb_sampling_rate, sensor[s].friendly_name, max_supported_rate); arb_sampling_rate = max_supported_rate; } @@ -784,8 +706,7 @@ static int sensor_set_rate (int s, float requested_rate) /* If we're dealing with a poll-mode sensor */ if (!sensor[s].num_channels) { if (is_enabled(s)) - /* Wake up thread so the new sampling rate gets used */ - pthread_cond_signal(&thread_release_cond[s]); + pthread_cond_signal(&thread_release_cond[s]); /* Wake up thread so the new sampling rate gets used */ return 0; } @@ -825,21 +746,15 @@ static int sensor_set_rate (int s, float requested_rate) /* Decode a single value */ sr = strtod(cursor, NULL); - /* - * If this matches the selected rate, we're happy. - * Have some tolerance to counter rounding errors and - * avoid needless jumps to higher rates. - */ + /* If this matches the selected rate, we're happy. Have some tolerance for rounding errors and avoid needless jumps to higher rates */ if (fabs(arb_sampling_rate - sr) <= 0.001) { arb_sampling_rate = sr; break; } /* - * If we reached a higher value than the desired rate, - * adjust selected rate so it matches the first higher - * available one and stop parsing - this makes the - * assumption that rates are sorted by increasing value + * If we reached a higher value than the desired rate, adjust selected rate so it matches the first higher + * available one and stop parsing - this makes the assumption that rates are sorted by increasing value * in the allowed frequencies string. */ if (sr > arb_sampling_rate) { @@ -866,25 +781,18 @@ static int sensor_set_rate (int s, float requested_rate) /* Coordinate with others active sensors on the same device, if any */ if (per_device_sampling_rate) for (n=0; n arb_sampling_rate) { - ALOGV("Sampling rate shared between %s and %s, using %g instead of %g\n" - , sensor[s].friendly_name, sensor[n].friendly_name, - sensor[n].sampling_rate, arb_sampling_rate); + if (n != s && sensor[n].dev_num == dev_num && sensor[n].num_channels && is_enabled(n) && sensor[n].sampling_rate > arb_sampling_rate) { + ALOGV("Sampling rate shared between %s and %s, using %g instead of %g\n", sensor[s].friendly_name, sensor[n].friendly_name, + sensor[n].sampling_rate, arb_sampling_rate); arb_sampling_rate = sensor[n].sampling_rate; } sensor[s].sampling_rate = arb_sampling_rate; - /* - * Update actual sampling rate field for this sensor and others which - * may be sharing the same sampling rate. - */ + /* Update actual sampling rate field for this sensor and others which may be sharing the same sampling rate */ if (per_device_sampling_rate) for (n=0; n