Index: device/generic_sensor/platform_sensor_linux.cc |
diff --git a/device/generic_sensor/platform_sensor_linux.cc b/device/generic_sensor/platform_sensor_linux.cc |
index 3495659729c1035f8a50687ce6fbebb4fc961615..55aa1398d1499c7bd61bfc4ec10b1199b5087c85 100644 |
--- a/device/generic_sensor/platform_sensor_linux.cc |
+++ b/device/generic_sensor/platform_sensor_linux.cc |
@@ -4,10 +4,8 @@ |
#include "device/generic_sensor/platform_sensor_linux.h" |
-#include "base/threading/thread.h" |
-#include "base/timer/timer.h" |
-#include "device/generic_sensor/linux/platform_sensor_utils_linux.h" |
#include "device/generic_sensor/linux/sensor_data_linux.h" |
+#include "device/generic_sensor/platform_sensor_reader_linux.h" |
namespace device { |
@@ -25,48 +23,61 @@ PlatformSensorLinux::PlatformSensorLinux( |
mojom::SensorType type, |
mojo::ScopedSharedBufferMapping mapping, |
PlatformSensorProvider* provider, |
- const SensorDataLinux& data, |
- std::unique_ptr<SensorReader> sensor_reader, |
- scoped_refptr<base::SingleThreadTaskRunner> polling_thread_task_runner_) |
+ const SensorInfoLinux* sensor_device, |
+ scoped_refptr<base::SingleThreadTaskRunner> polling_thread_task_runner) |
: 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_task_runner_(polling_thread_task_runner_), |
- weak_factory_(this) {} |
- |
-PlatformSensorLinux::~PlatformSensorLinux() { |
- polling_thread_task_runner_->DeleteSoon(FROM_HERE, timer_); |
+ default_configuration_( |
+ PlatformSensorConfiguration(sensor_device->device_frequency)), |
+ reporting_mode_(sensor_device->reporting_mode), |
+ weak_factory_(this) { |
+ sensor_reader_ = |
+ SensorReader::Create(sensor_device, this, polling_thread_task_runner); |
} |
+PlatformSensorLinux::~PlatformSensorLinux() = default; |
+ |
mojom::ReportingMode PlatformSensorLinux::GetReportingMode() { |
+ DCHECK(task_runner_->BelongsToCurrentThread()); |
return reporting_mode_; |
} |
+void PlatformSensorLinux::UpdatePlatformSensorReading(SensorReading reading) { |
+ DCHECK(task_runner_->BelongsToCurrentThread()); |
+ bool notifyNeeded = false; |
+ if (GetReportingMode() == mojom::ReportingMode::ON_CHANGE) { |
+ if (!HaveValuesChanged(reading, old_values_)) |
+ return; |
+ notifyNeeded = true; |
+ } |
+ old_values_ = reading; |
+ reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); |
+ UpdateSensorReading(reading, notifyNeeded); |
+} |
+ |
+void PlatformSensorLinux::NotifyPlatformSensorError() { |
+ DCHECK(task_runner_->BelongsToCurrentThread()); |
+ NotifySensorError(); |
+} |
+ |
bool PlatformSensorLinux::StartSensor( |
const PlatformSensorConfiguration& configuration) { |
DCHECK(task_runner_->BelongsToCurrentThread()); |
- return polling_thread_task_runner_->PostTask( |
- FROM_HERE, base::Bind(&PlatformSensorLinux::BeginPoll, |
- weak_factory_.GetWeakPtr(), configuration)); |
+ if (!sensor_reader_) |
+ return false; |
+ return sensor_reader_->StartFetchingData(configuration); |
} |
void PlatformSensorLinux::StopSensor() { |
DCHECK(task_runner_->BelongsToCurrentThread()); |
- polling_thread_task_runner_->PostTask( |
- FROM_HERE, base::Bind(&PlatformSensorLinux::StopPoll, this)); |
+ DCHECK(sensor_reader_); |
+ sensor_reader_->StopFetchingData(); |
} |
bool PlatformSensorLinux::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; |
+ configuration.frequency() <= default_configuration_.frequency(); |
} |
PlatformSensorConfiguration PlatformSensorLinux::GetDefaultConfiguration() { |
@@ -74,41 +85,4 @@ PlatformSensorConfiguration PlatformSensorLinux::GetDefaultConfiguration() { |
return default_configuration_; |
} |
-void PlatformSensorLinux::BeginPoll( |
- const PlatformSensorConfiguration& configuration) { |
- DCHECK(polling_thread_task_runner_->BelongsToCurrentThread()); |
- timer_->Start(FROM_HERE, base::TimeDelta::FromMicroseconds( |
- base::Time::kMicrosecondsPerSecond / |
- configuration.frequency()), |
- this, &PlatformSensorLinux::PollForReadingData); |
-} |
- |
-void PlatformSensorLinux::StopPoll() { |
- DCHECK(polling_thread_task_runner_->BelongsToCurrentThread()); |
- timer_->Stop(); |
-} |
- |
-void PlatformSensorLinux::PollForReadingData() { |
- DCHECK(polling_thread_task_runner_->BelongsToCurrentThread()); |
- |
- SensorReading reading; |
- if (!sensor_reader_->ReadSensorReading(&reading)) { |
- task_runner_->PostTask( |
- FROM_HERE, base::Bind(&PlatformSensorLinux::NotifySensorError, this)); |
- StopPoll(); |
- return; |
- } |
- |
- bool notifyNeeded = false; |
- if (GetReportingMode() == mojom::ReportingMode::ON_CHANGE) { |
- if (!HaveValuesChanged(reading, old_values_)) |
- return; |
- notifyNeeded = true; |
- } |
- |
- old_values_ = reading; |
- reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); |
- UpdateSensorReading(reading, notifyNeeded); |
-} |
- |
} // namespace device |