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..d7ed174821595b760330802f6b2ad1c3e7ef5b10 | 
| --- /dev/null | 
| +++ b/device/generic_sensor/platform_sensor_iio.cc | 
| @@ -0,0 +1,116 @@ | 
| +// 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 && | 
| 
 
shalamov
2016/10/19 16:37:11
I think we have restricted frequency to be > 0 and
 
maksims (do not use this acc)
2016/10/20 09:50:36
It will be sensor dependent, when I start to work
 
 | 
| + 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)) { | 
| + if (!HasValuesChanged(reading, old_values_)) { | 
| + return; | 
| + } | 
| + notifyNeeded = true; | 
| + } | 
| + | 
| + old_values_ = reading; | 
| + reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); | 
| + UpdateSensorReading(reading, notifyNeeded); | 
| +} | 
| + | 
| +} // namespace device |