| Index: device/generic_sensor/platform_sensor_iio.cc
|
| diff --git a/device/generic_sensor/platform_sensor_iio.cc b/device/generic_sensor/platform_sensor_iio.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..5b408d86158a90232584d258f457ef75258a4c3e
|
| --- /dev/null
|
| +++ b/device/generic_sensor/platform_sensor_iio.cc
|
| @@ -0,0 +1,114 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "device/generic_sensor/platform_sensor_iio.h"
|
| +
|
| +namespace device {
|
| +
|
| +namespace {
|
| +
|
| +// Checks if at least one value has been changed.
|
| +bool HasValuesChanged(const SensorReading& lhs, const SensorReading& rhs) {
|
| + return lhs.values[0] != rhs.values[0] || lhs.values[1] != rhs.values[1] ||
|
| + lhs.values[2] != rhs.values[2];
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +PlatformSensorIio::PlatformSensorIio(
|
| + mojom::SensorType type,
|
| + mojo::ScopedSharedBufferMapping mapping,
|
| + PlatformSensorProvider* provider,
|
| + const SensorDataIio& data,
|
| + std::unique_ptr<base::Thread> polling_thread,
|
| + std::unique_ptr<SensorReader> sensor_reader)
|
| + : PlatformSensor(type, std::move(mapping), provider),
|
| + timer_(new base::RepeatingTimer()),
|
| + default_configuration_(data.default_configuration),
|
| + reporting_mode_(data.reporting_mode),
|
| + sensor_reader_(std::move(sensor_reader)),
|
| + polling_thread_(std::move(polling_thread)),
|
| + polling_thread_task_runner_(polling_thread_->task_runner()),
|
| + weak_factory_(this) {}
|
| +
|
| +PlatformSensorIio::~PlatformSensorIio() {
|
| + polling_thread_task_runner_->DeleteSoon(FROM_HERE, timer_);
|
| +}
|
| +
|
| +mojom::ReportingMode PlatformSensorIio::GetReportingMode() {
|
| + return reporting_mode_;
|
| +}
|
| +
|
| +bool PlatformSensorIio::StartSensor(
|
| + const PlatformSensorConfiguration& configuration) {
|
| + DCHECK(task_runner_->BelongsToCurrentThread());
|
| + if (!polling_thread_->IsRunning() &&
|
| + !polling_thread_->StartWithOptions(
|
| + base::Thread::Options(base::MessageLoop::TYPE_IO, 0))) {
|
| + return false;
|
| + }
|
| +
|
| + return polling_thread_task_runner_->PostTask(
|
| + FROM_HERE, base::Bind(&PlatformSensorIio::BeginPoll,
|
| + weak_factory_.GetWeakPtr(), configuration));
|
| +}
|
| +
|
| +void PlatformSensorIio::StopSensor() {
|
| + DCHECK(task_runner_->BelongsToCurrentThread());
|
| + if (polling_thread_->IsRunning()) {
|
| + polling_thread_task_runner_->PostTask(
|
| + FROM_HERE, base::Bind(&PlatformSensorIio::StopPoll, this));
|
| + }
|
| +}
|
| +
|
| +bool PlatformSensorIio::CheckSensorConfiguration(
|
| + const PlatformSensorConfiguration& configuration) {
|
| + DCHECK(task_runner_->BelongsToCurrentThread());
|
| + return configuration.frequency() > 0 &&
|
| + configuration.frequency() <=
|
| + mojom::SensorConfiguration::kMaxAllowedFrequency;
|
| +}
|
| +
|
| +PlatformSensorConfiguration PlatformSensorIio::GetDefaultConfiguration() {
|
| + DCHECK(task_runner_->BelongsToCurrentThread());
|
| + return default_configuration_;
|
| +}
|
| +
|
| +void PlatformSensorIio::BeginPoll(
|
| + const PlatformSensorConfiguration& configuration) {
|
| + DCHECK(polling_thread_task_runner_->BelongsToCurrentThread());
|
| + timer_->Start(FROM_HERE, base::TimeDelta::FromMicroseconds(
|
| + base::Time::kMicrosecondsPerSecond /
|
| + configuration.frequency()),
|
| + this, &PlatformSensorIio::PollForReadingData);
|
| +}
|
| +
|
| +void PlatformSensorIio::StopPoll() {
|
| + DCHECK(polling_thread_task_runner_->BelongsToCurrentThread());
|
| + timer_->Stop();
|
| +}
|
| +
|
| +void PlatformSensorIio::PollForReadingData() {
|
| + DCHECK(polling_thread_task_runner_->BelongsToCurrentThread());
|
| +
|
| + SensorReading reading;
|
| + if (!sensor_reader_->ReadSensorReading(&reading)) {
|
| + StopPoll();
|
| + return;
|
| + }
|
| +
|
| + bool notifyNeeded = false;
|
| + if ((GetReportingMode() == mojom::ReportingMode::ON_CHANGE)) {
|
| + if (!HasValuesChanged(reading, old_values_)) {
|
| + return;
|
| + }
|
| + notifyNeeded = true;
|
| + }
|
| +
|
| + old_values_ = reading;
|
| + reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF();
|
| + UpdateSensorReading(reading, notifyNeeded);
|
| +}
|
| +
|
| +} // namespace device
|
|
|