X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;ds=sidebyside;f=control.c;h=14f5570fbf219e80873d5a12be93a11f80906127;hb=cce276f;hp=a1c06b1543dfb9d0fc096917049fd75d6b052ec2;hpb=87627dc1e9d58b7d3904fa13de13c4bfaf8ce039;p=android-x86%2Fhardware-intel-libsensors.git diff --git a/control.c b/control.c index a1c06b1..14f5570 100644 --- a/control.c +++ b/control.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -17,13 +18,15 @@ #include "transform.h" #include "calibration.h" #include "description.h" +#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 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 */ @@ -39,12 +42,61 @@ static pthread_mutex_t thread_release_mutex [MAX_SENSORS]; * - 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 - * */ + */ #define THREAD_REPORT_TAG_BASE 0x00010000 #define ENABLE_BUFFER_RETRIES 10 #define ENABLE_BUFFER_RETRY_DELAY_MS 10 + +inline int is_enabled (int s) +{ + return sensor[s].directly_enabled || sensor[s].ref_count; +} + + +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; + + if (!from_virtual) + /* We're being directly enabled */ + sensor[s].directly_enabled = 1; + + if (sensor[s].ref_count) + /* We were already indirectly enabled */ + return 0; + + return 1; /* Do continue enabling this sensor */ + } + + if (!is_enabled(s)) + /* We are being disabled but already were: no change */ + return 0; + + if (from_virtual && sensor[s].directly_enabled) + /* We're indirectly disabled but the base is still active */ + return 0; + + /* If it's disable, and it's from Android, and we still have ref counts */ + if (!from_virtual && sensor[s].ref_count) { + sensor[s].directly_enabled = 0; + return 0; + } + + /*If perhaps we are from virtual but we're disabling it*/ + sensor[s].directly_enabled = 0; + + return 1; /* Do continue disabling this sensor */ +} + + static int enable_buffer(int dev_num, int enabled) { char sysfs_path[PATH_MAX]; @@ -82,11 +134,11 @@ static int setup_trigger (int s, const char* trigger_val) char sysfs_path[PATH_MAX]; int ret = -1, attempts = 5; - sprintf(sysfs_path, TRIGGER_PATH, sensor_info[s].dev_num); + 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_info[s].friendly_name, trigger_val); + sensor[s].friendly_name, trigger_val); while (ret == -1 && attempts) { ret = sysfs_write_str(sysfs_path, trigger_val); @@ -94,15 +146,92 @@ static int setup_trigger (int s, const char* trigger_val) } if (ret != -1) - sensor_info[s].selected_trigger = trigger_val; + sensor[s].selected_trigger = trigger_val; else ALOGE("Setting S%d (%s) trigger to %s FAILED.\n", s, - sensor_info[s].friendly_name, trigger_val); + sensor[s].friendly_name, trigger_val); return ret; } -void build_sensor_report_maps(int dev_num) +static void enable_iio_timestamp (int dev_num, int known_channels) +{ + /* Check if we have a dedicated iio timestamp channel */ + + char spec_buf[MAX_TYPE_SPEC_LEN]; + char sysfs_path[PATH_MAX]; + int n; + + sprintf(sysfs_path, CHANNEL_PATH "%s", dev_num, "in_timestamp_type"); + + n = sysfs_read_str(sysfs_path, spec_buf, sizeof(spec_buf)); + + if (n <= 0) + return; + + if (strcmp(spec_buf, "le:s64/64>>0")) + return; + + /* OK, type is int64_t as expected, in little endian representation */ + + sprintf(sysfs_path, CHANNEL_PATH"%s", dev_num, "in_timestamp_index"); + + if (sysfs_read_int(sysfs_path, &n)) + return; + + /* Check that the timestamp comes after the other fields we read */ + if (n != known_channels) + return; + + /* Try enabling that channel */ + sprintf(sysfs_path, CHANNEL_PATH "%s", dev_num, "in_timestamp_en"); + + sysfs_write_int(sysfs_path, 1); + + if (sysfs_read_int(sysfs_path, &n)) + return; + + if (n) { + ALOGI("Detected timestamp channel on iio device %d\n", dev_num); + has_iio_ts[dev_num] = 1; + } +} + + +static int decode_type_spec (const char type_buf[MAX_TYPE_SPEC_LEN], + struct datum_info_t *type_info) +{ + /* Return size in bytes for this type specification, or -1 in error */ + char sign; + char endianness; + unsigned int realbits, storagebits, shift; + int tokens; + + /* 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); + + 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; + type_info->realbits = (short) realbits; + type_info->storagebits = (short) storagebits; + type_info->shift = (short) shift; + + return storagebits / 8; +} + + +void build_sensor_report_maps (int dev_num) { /* * Read sysfs files from a iio device's scan_element directory, and @@ -138,17 +267,17 @@ void build_sensor_report_maps(int dev_num) /* For each sensor that is linked to this device */ for (s=0; s MAX_DEVICE_REPORT_SIZE) { + 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; + } } -int adjust_counters (int s, int enabled) +int adjust_counters (int s, int enabled, int from_virtual) { /* * Adjust counters based on sensor enable action. Return values are: - * -1 if there's an inconsistency: abort action in this case * 0 if the operation was completed and we're all set * 1 if we toggled the state of the sensor and there's work left */ - int dev_num = sensor_info[s].dev_num; - int catalog_index = sensor_info[s].catalog_index; - int sensor_type = sensor_catalog[catalog_index].type; + 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; - /* Refcount per sensor, in terms of enable count */ if (enabled) { ALOGI("Enabling sensor %d (iio device %d: %s)\n", - s, dev_num, sensor_info[s].friendly_name); - - sensor_info[s].enable_count++; + s, dev_num, sensor[s].friendly_name); - if (sensor_info[s].enable_count > 1) - return 0; /* The sensor was, and remains, in use */ - - switch (sensor_type) { + switch (sensor[s].type) { case SENSOR_TYPE_MAGNETIC_FIELD: - compass_read_data(&sensor_info[s]); + compass_read_data(&sensor[s]); break; case SENSOR_TYPE_GYROSCOPE: - case SENSOR_TYPE_GYROSCOPE_UNCALIBRATED: - gyro_cal_init(&sensor_info[s]); + gyro_cal_init(&sensor[s]); break; } } else { - if (sensor_info[s].enable_count == 0) - return -1; /* Spurious disable call */ - ALOGI("Disabling sensor %d (iio device %d: %s)\n", s, dev_num, - sensor_info[s].friendly_name); - - sensor_info[s].enable_count--; - - if (sensor_info[s].enable_count > 0) - return 0; /* The sensor was, and remains, in use */ + sensor[s].friendly_name); /* Sensor disabled, lower report available flag */ - sensor_info[s].report_pending = 0; - - if (sensor_type == SENSOR_TYPE_MAGNETIC_FIELD) - compass_store_data(&sensor_info[s]); - } + sensor[s].report_pending = 0; + if (sensor[s].type == SENSOR_TYPE_MAGNETIC_FIELD) + compass_store_data(&sensor[s]); - /* If uncalibrated type and pair is already active don't adjust counters */ - if (sensor_type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED && - sensor_info[sensor_info[s].pair_idx].enable_count != 0) - return 0; + if (sensor[s].type == SENSOR_TYPE_GYROSCOPE) + gyro_store_data(&sensor[s]); + } - /* We changed the state of a sensor - adjust per iio device counters */ + /* We changed the state of a sensor: adjust device ref counts */ - /* If this is a regular event-driven sensor */ - if (sensor_info[s].num_channels) { + if (sensor[s].num_channels) { if (enabled) trig_sensors_per_dev[dev_num]++; @@ -309,7 +437,7 @@ int adjust_counters (int s, int enabled) trig_sensors_per_dev[dev_num]--; return 1; - } + } if (enabled) { active_poll_sensors++; @@ -325,10 +453,7 @@ int adjust_counters (int s, int enabled) static int get_field_count (int s) { - int catalog_index = sensor_info[s].catalog_index; - int sensor_type = sensor_catalog[catalog_index].type; - - switch (sensor_type) { + switch (sensor[s].type) { case SENSOR_TYPE_ACCELEROMETER: /* m/s^2 */ case SENSOR_TYPE_MAGNETIC_FIELD: /* micro-tesla */ case SENSOR_TYPE_ORIENTATION: /* degrees */ @@ -345,7 +470,7 @@ static int get_field_count (int s) return 1; case SENSOR_TYPE_ROTATION_VECTOR: - return 4; + return 4; default: ALOGE("Unknown sensor type!\n"); @@ -354,7 +479,6 @@ static int get_field_count (int s) } - static void* acquisition_routine (void* param) { /* @@ -372,24 +496,24 @@ static void* acquisition_routine (void* param) int c; int ret; struct timespec target_time; - int64_t timestamp, period; - - ALOGI("Entering data acquisition thread S%d (%s): rate(%f), ts(%lld)\n", s, - sensor_info[s].friendly_name, sensor_info[s].sampling_rate, sensor_info[s].report_ts); + int64_t timestamp, period, start, stop; if (s < 0 || s >= sensor_count) { ALOGE("Invalid sensor handle!\n"); return NULL; } - if (sensor_info[s].sampling_rate <= 0) { - ALOGE("Non-positive rate in acquisition routine for sensor %d: %f\n", - s, sensor_info[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); return NULL; } num_fields = get_field_count(s); - sample_size = num_fields * sizeof(float); + sample_size = sizeof(int64_t) + num_fields * sizeof(float); /* * Each condition variable is associated to a mutex that has to be @@ -400,43 +524,46 @@ static void* acquisition_routine (void* param) pthread_mutex_lock(&thread_release_mutex[s]); /* Pinpoint the moment we start sampling */ - timestamp = get_timestamp(); + timestamp = get_timestamp_monotonic(); /* Check and honor termination requests */ - while (sensor_info[s].thread_data_fd[1] != -1) { - + while (sensor[s].thread_data_fd[1] != -1) { + start = get_timestamp_boot(); /* Read values through sysfs */ for (c=0; c 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; + } + + sensor[s].sampling_rate = arb_sampling_rate; + + /* If we're dealing with a poll-mode sensor */ + if (!sensor[s].num_channels) { + /* Wake up thread so the new sampling rate gets used */ + pthread_cond_signal(&thread_release_cond[s]); + return 0; + } + + sprintf(sysfs_path, SENSOR_SAMPLING_PATH, dev_num, prefix); + + if (sysfs_read_float(sysfs_path, &cur_sampling_rate) != -1) { + per_sensor_sampling_rate = 1; + per_device_sampling_rate = 0; + } else { + per_sensor_sampling_rate = 0; + + sprintf(sysfs_path, DEVICE_SAMPLING_PATH, dev_num); + + if (sysfs_read_float(sysfs_path, &cur_sampling_rate) != -1) + per_device_sampling_rate = 1; + else + per_device_sampling_rate = 0; + } + + if (!per_sensor_sampling_rate && !per_device_sampling_rate) { + ALOGE("No way to adjust sampling rate on sensor %d\n", s); + return -ENOSYS; + } + + /* Coordinate with others active sensors on the same device, if any */ + if (per_device_sampling_rate) + for (n=0; n arb_sampling_rate) + arb_sampling_rate = sensor[n].sampling_rate; + + /* Check if we have contraints on allowed 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){ + cursor = freqs_buf; + + /* Decode allowed sampling rates string, ex: "10 20 50 100" */ + + /* While we're not at the end of the string */ + while (*cursor && cursor[0]) { + + /* 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 (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 + * in the allowed frequencies string. + */ + if (sr > arb_sampling_rate) { + arb_sampling_rate = sr; + break; + } + + /* Skip digits */ + while (cursor[0] && !isspace(cursor[0])) + cursor++; + + /* Skip spaces */ + while (cursor[0] && isspace(cursor[0])) + cursor++; + } + } + + if (max_supported_rate && + arb_sampling_rate > max_supported_rate) { + arb_sampling_rate = max_supported_rate; + } + + /* If the desired rate is already active we're all set */ + if (arb_sampling_rate == cur_sampling_rate) + return 0; + + ALOGI("Sensor %d (%s) sampling rate set to %g\n", + s, sensor[s].friendly_name, arb_sampling_rate); + + if (trig_sensors_per_dev[dev_num]) + enable_buffer(dev_num, 0); + + sysfs_write_float(sysfs_path, arb_sampling_rate); + + /* Check if it makes sense to use an alternate trigger */ + tentative_switch_trigger(s); + + if (trig_sensors_per_dev[dev_num]) + enable_buffer(dev_num, 1); + + return 0; +} + + +/* + * We go through all the virtual sensors of the base - and the base itself + * in order to recompute the maximum requested delay of the group and setup the base + * at that specific delay. + */ +static int arbitrate_bases (int s) +{ + int i, vidx; + + float arbitrated_rate = 0; + + if (sensor[s].directly_enabled) + arbitrated_rate = sensor[s].requested_rate; + + for (i = 0; i < sensor_count; i++) { + for (vidx = 0; vidx < sensor[i].base_count; vidx++) + /* If we have a virtual sensor depending on this one - handle it */ + if (sensor[i].base[vidx] == s && + sensor[i].directly_enabled && + sensor[i].requested_rate > arbitrated_rate) + arbitrated_rate = sensor[i].requested_rate; + } + + return setup_delay_sysfs(s, arbitrated_rate); +} + + +/* + * Re-assesment for delays. We need to re-asses delays for all related groups + * of sensors everytime a sensor enables / disables / changes frequency. + */ +int arbitrate_delays (int s) +{ + int i; - /* If we want to activate gyro calibrated and gyro uncalibrated is activated - * Deactivate gyro uncalibrated - Uncalibrated releases handler - * Activate gyro calibrated - Calibrated has handler - * Reactivate gyro uncalibrated - Uncalibrated gets data from calibrated */ + if (!sensor[s].is_virtual) { + return arbitrate_bases(s); + } + /* Is virtual sensor - go through bases */ + for (i = 0; i < sensor[s].base_count; i++) + arbitrate_bases(sensor[s].base[i]); + + return 0; +} - /* If we want to deactivate gyro calibrated and gyro uncalibrated is active - * Deactivate gyro uncalibrated - Uncalibrated no longer gets data from handler - * Deactivate gyro calibrated - Calibrated releases handler - * Reactivate gyro uncalibrated - Uncalibrated has handler */ - if (sensor_catalog[sensor_info[s].catalog_index].type == SENSOR_TYPE_GYROSCOPE && - sensor_info[s].pair_idx && sensor_info[sensor_info[s].pair_idx].enable_count != 0) { +int sensor_activate (int s, int enabled, int from_virtual) +{ + char device_name[PATH_MAX]; + struct epoll_event ev = {0}; + int dev_fd; + int ret; + int dev_num = sensor[s].dev_num; + int is_poll_sensor = !sensor[s].num_channels; - sensor_activate(sensor_info[s].pair_idx, 0); - ret = sensor_activate(s, enabled); - sensor_activate(sensor_info[s].pair_idx, 1); - return ret; + if (sensor[s].is_virtual) { + sensor_activate_virtual(s, enabled, from_virtual); + arbitrate_delays(s); + return 0; } - ret = adjust_counters(s, enabled); + /* Prepare the report timestamp field for the first event, see set_report_ts method */ + sensor[s].report_ts = 0; + + ret = adjust_counters(s, enabled, from_virtual); /* If the operation was neutral in terms of state, we're done */ if (ret <= 0) return ret; + arbitrate_delays(s); + + sensor[s].event_count = 0; + sensor[s].meta_data_pending = 0; + + if (enabled && (sensor[s].quirks & QUIRK_NOISY)) + /* Initialize filtering data if required */ + setup_noise_filtering(s); if (!is_poll_sensor) { @@ -570,7 +965,7 @@ int sensor_activate(int s, int enabled) if (trig_sensors_per_dev[dev_num]) { /* Start sampling */ - setup_trigger(s, sensor_info[s].init_trigger_name); + setup_trigger(s, sensor[s].init_trigger_name); enable_buffer(dev_num, 1); } } @@ -599,16 +994,8 @@ int sensor_activate(int s, int enabled) device_fd[dev_num] = -1; } - /* If we recorded a trail of samples for filtering, delete it */ - if (sensor_info[s].history) { - free(sensor_info[s].history); - sensor_info[s].history = NULL; - sensor_info[s].history_size = 0; - if (sensor_info[s].history_sum) { - free(sensor_info[s].history_sum); - sensor_info[s].history_sum = NULL; - } - } + /* Release any filtering data we may have accumulated */ + release_noise_filtering_data(s); return 0; } @@ -623,7 +1010,7 @@ int sensor_activate(int s, int enabled) if (dev_fd == -1) { ALOGE("Could not open fd on %s (%s)\n", device_name, strerror(errno)); - adjust_counters(s, 0); + adjust_counters(s, 0, from_virtual); return -1; } @@ -648,7 +1035,7 @@ int sensor_activate(int s, int enabled) } /* Ensure that on-change sensors send at least one event after enable */ - sensor_info[s].prev_val = -1; + sensor[s].prev_val = -1; if (is_poll_sensor) start_acquisition_thread(s); @@ -657,26 +1044,6 @@ int sensor_activate(int s, int enabled) } -static int is_fast_accelerometer (int s) -{ - /* - * Some games don't react well to accelerometers using any-motion - * triggers. Even very low thresholds seem to trip them, and they tend - * to request fairly high event rates. Favor continuous triggers if the - * sensor is an accelerometer and uses a sampling rate of at least 25. - */ - int catalog_index = sensor_info[s].catalog_index; - - if (sensor_catalog[catalog_index].type != SENSOR_TYPE_ACCELEROMETER) - return 0; - - if (sensor_info[s].sampling_rate < 25) - return 0; - - return 1; -} - - static void enable_motion_trigger (int dev_num) { /* @@ -707,24 +1074,24 @@ static void enable_motion_trigger (int dev_num) /* Check that all active sensors are ready to switch */ for (s=0; s (th <= 1.1111) + */ +#define THRESHOLD 1.10 +#define MAX_DELAY 500000000 /* 500 ms */ + void set_report_ts(int s, int64_t ts) { int64_t maxTs, period; @@ -753,27 +1129,44 @@ void set_report_ts(int s, int64_t ts) * this may not be the case. Perhaps we'll get rid of this when * we'll be reading the timestamp from the iio channel for all sensors */ - if (sensor_info[s].report_ts && sensor_info[s].sampling_rate && + if (sensor[s].report_ts && sensor[s].sampling_rate && REPORTING_MODE(sensor_desc[s].flags) == SENSOR_FLAG_CONTINUOUS_MODE) { - period = (int64_t) (1000000000LL / sensor_info[s].sampling_rate); - maxTs = sensor_info[s].report_ts + period; - sensor_info[s].report_ts = (ts < maxTs ? ts : maxTs); + period = (int64_t) (1000000000LL / sensor[s].sampling_rate); + maxTs = sensor[s].report_ts + THRESHOLD * period; + /* If we're too far behind get back on track */ + if (ts - maxTs >= MAX_DELAY) + maxTs = ts; + sensor[s].report_ts = (ts < maxTs ? ts : maxTs); } else { - sensor_info[s].report_ts = ts; + sensor[s].report_ts = ts; } } -static int integrate_device_report(int dev_num) + +static void stamp_reports (int dev_num, int64_t ts) +{ + int s; + + for (s=0; ssensor = s; + data->type = sensor[s].type; + return 1; +} + + +static int propagate_sensor_report (int s, struct sensors_event_t *data) { /* There's a sensor report pending for this sensor ; transmit it */ - int catalog_index = sensor_info[s].catalog_index; - int sensor_type = sensor_catalog[catalog_index].type; int num_fields = get_field_count(s); int c; unsigned char* current_sample; @@ -851,25 +1288,19 @@ static int propagate_sensor_report(int s, struct sensors_event_t *data) if (!num_fields) return 0; - - /* Only return uncalibrated event if also gyro active */ - if (sensor_type == SENSOR_TYPE_GYROSCOPE_UNCALIBRATED && - sensor_info[sensor_info[s].pair_idx].enable_count != 0) - return 0; - memset(data, 0, sizeof(sensors_event_t)); data->version = sizeof(sensors_event_t); data->sensor = s; - data->type = sensor_type; - data->timestamp = sensor_info[s].report_ts; + data->type = sensor[s].type; + data->timestamp = sensor[s].report_ts; - ALOGV("Sample on sensor %d (type %d):\n", s, sensor_type); + ALOGV("Sample on sensor %d (type %d):\n", s, sensor[s].type); - current_sample = sensor_info[s].report_buffer; + current_sample = sensor[s].report_buffer; /* If this is a poll sensor */ - if (!sensor_info[s].num_channels) { + if (!sensor[s].num_channels) { /* Use the data provided by the acquisition thread */ ALOGV("Reporting data from worker thread for S%d\n", s); memcpy(data->data, current_sample, num_fields * sizeof(float)); @@ -879,18 +1310,18 @@ static int propagate_sensor_report(int s, struct sensors_event_t *data) /* Convert the data into the expected Android-level format */ for (c=0; cdata[c] = sensor_info[s].ops.transform + data->data[c] = sensor[s].ops.transform (s, c, current_sample); ALOGV("\tfield %d: %f\n", c, data->data[c]); - current_sample += sensor_info[s].channel[c].size; + current_sample += sensor[s].channel[c].size; } /* * The finalize routine, in addition to its late sample processing duty, * has the final say on whether or not the sample gets sent to Android. */ - return sensor_info[s].ops.finalize(s, data); + return sensor[s].ops.finalize(s, data); } @@ -912,35 +1343,35 @@ static void synthetize_duplicate_samples (void) for (s=0; sbias_x; - data[uncal_start + i].data[1] = data[returned_events + i].data[1] + gyro_data->bias_y; - data[uncal_start + i].data[2] = data[returned_events + i].data[2] + gyro_data->bias_z; - - data[uncal_start + i].uncalibrated_gyro.bias[0] = gyro_data->bias_x; - data[uncal_start + i].uncalibrated_gyro.bias[1] = gyro_data->bias_y; - data[uncal_start + i].uncalibrated_gyro.bias[2] = gyro_data->bias_z; - } - event_count <<= 1; + if (sensor[s].is_virtual) + event_count = propagate_vsensor_report(s, &data[returned_events]); + else { + /* Report this event if it looks OK */ + event_count = propagate_sensor_report(s, &data[returned_events]); } - sensor_info[sensor_info[s].pair_idx].report_pending = 0; + + /* Lower flag */ + sensor[s].report_pending = 0; returned_events += event_count; /* * If the sample was deemed invalid or unreportable, @@ -1070,7 +1485,7 @@ return_available_sensor_reports: * value for a 'on change' sensor, silently drop it. */ } - while (sensor_info[s].meta_data_pending) { + while (sensor[s].meta_data_pending) { /* See sensors.h on these */ data[returned_events].version = META_DATA_VERSION; data[returned_events].sensor = 0; @@ -1080,7 +1495,7 @@ return_available_sensor_reports: data[returned_events].meta_data.sensor = s; data[returned_events].meta_data.what = META_DATA_FLUSH_COMPLETE; returned_events++; - sensor_info[s].meta_data_pending--; + sensor[s].meta_data_pending--; } } if (returned_events) @@ -1123,174 +1538,38 @@ await_event: } -int sensor_set_delay(int s, int64_t ns) +int sensor_set_delay (int s, int64_t ns) { - /* 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]; - int dev_num = sensor_info[s].dev_num; - int i = sensor_info[s].catalog_index; - const char *prefix = sensor_catalog[i].tag; - float new_sampling_rate; /* Granted sampling rate after arbitration */ - float cur_sampling_rate; /* Currently used sampling rate */ - int per_sensor_sampling_rate; - int per_device_sampling_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.0f / max_delay_us) : 1; - float max_supported_rate = - (min_delay_us && min_delay_us != -1) ? (1000000.0f / min_delay_us) : 0; - char freqs_buf[100]; - char* cursor; - int n; - float sr; + float requested_sampling_rate; if (ns <= 0) { - ALOGE("Rejecting non-positive delay request on sensor %d, required delay: %lld\n", s, ns); + ALOGE("Invalid delay requested on sensor %d: %lld\n", s, ns); return -EINVAL; } - new_sampling_rate = 1000000000LL/ns; - - ALOGV("Entering set delay S%d (%s): old rate(%f), new rate(%f)\n", - s, sensor_info[s].friendly_name, sensor_info[s].sampling_rate, - new_sampling_rate); - - /* - * Artificially limit ourselves to 1 Hz or higher. This is mostly to - * avoid setting up the stage for divisions by zero. - */ - if (new_sampling_rate < min_supported_rate) - new_sampling_rate = min_supported_rate; - - if (max_supported_rate && - new_sampling_rate > max_supported_rate) { - new_sampling_rate = max_supported_rate; - } - - sensor_info[s].sampling_rate = new_sampling_rate; - - /* If we're dealing with a poll-mode sensor */ - if (!sensor_info[s].num_channels) { - /* Interrupt current sleep so the new sampling gets used */ - pthread_cond_signal(&thread_release_cond[s]); - return 0; - } - - sprintf(sysfs_path, SENSOR_SAMPLING_PATH, dev_num, prefix); - - if (sysfs_read_float(sysfs_path, &cur_sampling_rate) != -1) { - per_sensor_sampling_rate = 1; - per_device_sampling_rate = 0; - } else { - per_sensor_sampling_rate = 0; - - sprintf(sysfs_path, DEVICE_SAMPLING_PATH, dev_num); - - if (sysfs_read_float(sysfs_path, &cur_sampling_rate) != -1) - per_device_sampling_rate = 1; - else - per_device_sampling_rate = 0; - } - - if (!per_sensor_sampling_rate && !per_device_sampling_rate) { - ALOGE("No way to adjust sampling rate on sensor %d\n", s); - return -ENOSYS; - } - - /* Coordinate with others active sensors on the same device, if any */ - if (per_device_sampling_rate) - for (n=0; n new_sampling_rate) - new_sampling_rate= sensor_info[n].sampling_rate; - - /* Check if we have contraints on allowed 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){ - cursor = freqs_buf; - - /* Decode allowed sampling rates string, ex: "10 20 50 100" */ - - /* While we're not at the end of the string */ - while (*cursor && cursor[0]) { - - /* Decode a single value */ - sr = strtod(cursor, NULL); - - /* If this matches the selected rate, we're happy */ - if (new_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 - * in the allowed frequencies string. - */ - if (sr > new_sampling_rate) { - new_sampling_rate = sr; - break; - } - - /* Skip digits */ - while (cursor[0] && !isspace(cursor[0])) - cursor++; - - /* Skip spaces */ - while (cursor[0] && isspace(cursor[0])) - cursor++; - } - } - - - if (max_supported_rate && - new_sampling_rate > max_supported_rate) { - new_sampling_rate = max_supported_rate; - } - - - /* If the desired rate is already active we're all set */ - if (new_sampling_rate == cur_sampling_rate) - return 0; - - ALOGI("Sensor %d sampling rate set to %g\n", s, new_sampling_rate); - - if (trig_sensors_per_dev[dev_num]) - enable_buffer(dev_num, 0); - - sysfs_write_float(sysfs_path, new_sampling_rate); + requested_sampling_rate = 1000000000.0/ns; - /* Switch back to continuous sampling for accelerometer based games */ - if (is_fast_accelerometer(s) && sensor_info[s].selected_trigger != - sensor_info[s].init_trigger_name) - setup_trigger(s, sensor_info[s].init_trigger_name); + ALOGV("Entering set delay S%d (%s): current rate: %f, requested: %f\n", + s, sensor[s].friendly_name, sensor[s].sampling_rate, + requested_sampling_rate); - if (trig_sensors_per_dev[dev_num]) - enable_buffer(dev_num, 1); + sensor[s].requested_rate = requested_sampling_rate; - return 0; + return arbitrate_delays(s); } + int sensor_flush (int s) { /* If one shot or not enabled return -EINVAL */ - if (sensor_desc[s].flags & SENSOR_FLAG_ONE_SHOT_MODE || - sensor_info[s].enable_count == 0) + if (sensor_desc[s].flags & SENSOR_FLAG_ONE_SHOT_MODE || !is_enabled(s)) return -EINVAL; - sensor_info[s].meta_data_pending++; + sensor[s].meta_data_pending++; return 0; } + int allocate_control_data (void) { int i;