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