| OLD | NEW |
| (Empty) |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "device/generic_sensor/platform_sensor_linux.h" | |
| 6 | |
| 7 #include "base/single_thread_task_runner.h" | |
| 8 #include "device/generic_sensor/linux/sensor_data_linux.h" | |
| 9 #include "device/generic_sensor/platform_sensor_reader_linux.h" | |
| 10 | |
| 11 namespace device { | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 // Checks if at least one value has been changed. | |
| 16 bool HaveValuesChanged(const SensorReading& lhs, const SensorReading& rhs) { | |
| 17 return lhs.values[0] != rhs.values[0] || lhs.values[1] != rhs.values[1] || | |
| 18 lhs.values[2] != rhs.values[2]; | |
| 19 } | |
| 20 | |
| 21 } // namespace | |
| 22 | |
| 23 PlatformSensorLinux::PlatformSensorLinux( | |
| 24 mojom::SensorType type, | |
| 25 mojo::ScopedSharedBufferMapping mapping, | |
| 26 PlatformSensorProvider* provider, | |
| 27 const SensorInfoLinux* sensor_device, | |
| 28 scoped_refptr<base::SingleThreadTaskRunner> polling_thread_task_runner) | |
| 29 : PlatformSensor(type, std::move(mapping), provider), | |
| 30 default_configuration_( | |
| 31 PlatformSensorConfiguration(sensor_device->device_frequency)), | |
| 32 reporting_mode_(sensor_device->reporting_mode), | |
| 33 polling_thread_task_runner_(std::move(polling_thread_task_runner)), | |
| 34 weak_factory_(this) { | |
| 35 sensor_reader_ = SensorReader::Create( | |
| 36 sensor_device, weak_factory_.GetWeakPtr(), task_runner_); | |
| 37 } | |
| 38 | |
| 39 PlatformSensorLinux::~PlatformSensorLinux() { | |
| 40 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 41 polling_thread_task_runner_->DeleteSoon(FROM_HERE, sensor_reader_.release()); | |
| 42 } | |
| 43 | |
| 44 mojom::ReportingMode PlatformSensorLinux::GetReportingMode() { | |
| 45 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 46 return reporting_mode_; | |
| 47 } | |
| 48 | |
| 49 void PlatformSensorLinux::UpdatePlatformSensorReading(SensorReading reading) { | |
| 50 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 51 bool notifyNeeded = false; | |
| 52 if (GetReportingMode() == mojom::ReportingMode::ON_CHANGE) { | |
| 53 if (!HaveValuesChanged(reading, old_values_)) | |
| 54 return; | |
| 55 notifyNeeded = true; | |
| 56 } | |
| 57 old_values_ = reading; | |
| 58 reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); | |
| 59 UpdateSensorReading(reading, notifyNeeded); | |
| 60 } | |
| 61 | |
| 62 void PlatformSensorLinux::NotifyPlatformSensorError() { | |
| 63 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 64 NotifySensorError(); | |
| 65 } | |
| 66 | |
| 67 bool PlatformSensorLinux::StartSensor( | |
| 68 const PlatformSensorConfiguration& configuration) { | |
| 69 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 70 polling_thread_task_runner_->PostTask( | |
| 71 FROM_HERE, | |
| 72 base::Bind(&SensorReader::StartFetchingData, | |
| 73 base::Unretained(sensor_reader_.get()), configuration)); | |
| 74 return true; | |
| 75 } | |
| 76 | |
| 77 void PlatformSensorLinux::StopSensor() { | |
| 78 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 79 polling_thread_task_runner_->PostTask( | |
| 80 FROM_HERE, base::Bind(&SensorReader::StopFetchingData, | |
| 81 base::Unretained(sensor_reader_.get()))); | |
| 82 } | |
| 83 | |
| 84 bool PlatformSensorLinux::CheckSensorConfiguration( | |
| 85 const PlatformSensorConfiguration& configuration) { | |
| 86 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 87 return configuration.frequency() > 0 && | |
| 88 configuration.frequency() <= default_configuration_.frequency(); | |
| 89 } | |
| 90 | |
| 91 PlatformSensorConfiguration PlatformSensorLinux::GetDefaultConfiguration() { | |
| 92 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 93 return default_configuration_; | |
| 94 } | |
| 95 | |
| 96 } // namespace device | |
| OLD | NEW |