Chromium Code Reviews| 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..2dc245a55cfd4158767ad3437317f805708fbebc |
| --- /dev/null |
| +++ b/device/generic_sensor/platform_sensor_iio.cc |
| @@ -0,0 +1,124 @@ |
| +// 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" |
| + |
| +#include "base/threading/thread.h" |
| +#include "base/timer/timer.h" |
| +#include "device/generic_sensor/iio/platform_sensor_utils_iio.h" |
| +#include "device/generic_sensor/iio/sensor_data_iio.h" |
| + |
| +namespace device { |
| + |
| +namespace { |
| + |
| +// Checks if at least one value has been changed. |
| +bool HasValuesChanged(const SensorReading& lhs, const SensorReading& rhs) { |
|
Reilly Grant (use Gerrit)
2016/10/24 21:49:27
HaveValuesChanged
maksims (do not use this acc)
2016/10/25 06:23:54
Done.
|
| + 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()); |
| + // TODO(maksims): make this sensor dependent. |
| + // For example, in case of accelerometer, check current polling frequency |
| + // exposed by iio driver. |
| + 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)) { |
| + task_runner_->PostTask( |
| + FROM_HERE, base::Bind(&PlatformSensorIio::NotifySensorError, this)); |
| + StopPoll(); |
| + return; |
| + } |
| + |
| + bool notifyNeeded = false; |
| + if ((GetReportingMode() == mojom::ReportingMode::ON_CHANGE)) { |
|
Reilly Grant (use Gerrit)
2016/10/24 21:49:27
nit: Extra set of parens.
maksims (do not use this acc)
2016/10/25 06:23:54
Done.
|
| + if (!HasValuesChanged(reading, old_values_)) { |
| + return; |
| + } |
|
Reilly Grant (use Gerrit)
2016/10/24 21:49:27
nit: No braces around single-line if.
maksims (do not use this acc)
2016/10/25 06:23:54
Done.
|
| + notifyNeeded = true; |
| + } |
| + |
| + old_values_ = reading; |
| + reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); |
| + UpdateSensorReading(reading, notifyNeeded); |
| +} |
| + |
| +} // namespace device |