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 "base/threading/thread.h" | 7 #include "device/generic_sensor/linux/platform_sensor_reader_linux.h" |
8 #include "base/timer/timer.h" | 8 #include "device/generic_sensor/platform_sensor_manager_linux.h" |
9 #include "device/generic_sensor/linux/platform_sensor_utils_linux.h" | |
10 #include "device/generic_sensor/linux/sensor_data_linux.h" | |
11 | 9 |
12 namespace device { | 10 namespace device { |
13 | 11 |
14 namespace { | 12 namespace { |
15 | 13 |
16 // Checks if at least one value has been changed. | 14 // Checks if at least one value has been changed. |
17 bool HaveValuesChanged(const SensorReading& lhs, const SensorReading& rhs) { | 15 bool HaveValuesChanged(const SensorReading& lhs, const SensorReading& rhs) { |
18 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] || |
19 lhs.values[2] != rhs.values[2]; | 17 lhs.values[2] != rhs.values[2]; |
20 } | 18 } |
21 | 19 |
22 } // namespace | 20 } // namespace |
23 | 21 |
24 PlatformSensorLinux::PlatformSensorLinux( | 22 PlatformSensorLinux::PlatformSensorLinux( |
25 mojom::SensorType type, | 23 mojom::SensorType type, |
26 mojo::ScopedSharedBufferMapping mapping, | 24 mojo::ScopedSharedBufferMapping mapping, |
27 PlatformSensorProvider* provider, | 25 PlatformSensorProvider* provider, |
28 const SensorDataLinux& data, | 26 SensorDeviceLinux* sensor_device, |
29 std::unique_ptr<SensorReader> sensor_reader, | 27 scoped_refptr<base::SingleThreadTaskRunner> polling_thread_task_runner) |
30 scoped_refptr<base::SingleThreadTaskRunner> polling_thread_task_runner_) | |
31 : PlatformSensor(type, std::move(mapping), provider), | 28 : PlatformSensor(type, std::move(mapping), provider), |
32 timer_(new base::RepeatingTimer()), | 29 default_configuration_( |
33 default_configuration_(data.default_configuration), | 30 PlatformSensorConfiguration(sensor_device->device_frequency)), |
34 reporting_mode_(data.reporting_mode), | 31 reporting_mode_(sensor_device->reporting_mode), |
35 sensor_reader_(std::move(sensor_reader)), | 32 weak_factory_(this) { |
36 polling_thread_task_runner_(polling_thread_task_runner_), | 33 sensor_reader_ = |
37 weak_factory_(this) {} | 34 SensorReader::Create(sensor_device, polling_thread_task_runner); |
38 | |
39 PlatformSensorLinux::~PlatformSensorLinux() { | |
40 polling_thread_task_runner_->DeleteSoon(FROM_HERE, timer_); | |
41 } | 35 } |
42 | 36 |
| 37 PlatformSensorLinux::~PlatformSensorLinux() = default; |
| 38 |
43 mojom::ReportingMode PlatformSensorLinux::GetReportingMode() { | 39 mojom::ReportingMode PlatformSensorLinux::GetReportingMode() { |
| 40 DCHECK(task_runner_->BelongsToCurrentThread()); |
44 return reporting_mode_; | 41 return reporting_mode_; |
45 } | 42 } |
46 | 43 |
| 44 void PlatformSensorLinux::UpdatePlatformSensorReading(SensorReading reading) { |
| 45 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 46 bool notifyNeeded = false; |
| 47 if (GetReportingMode() == mojom::ReportingMode::ON_CHANGE) { |
| 48 if (!HaveValuesChanged(reading, old_values_)) |
| 49 return; |
| 50 notifyNeeded = true; |
| 51 } |
| 52 old_values_ = reading; |
| 53 reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); |
| 54 UpdateSensorReading(reading, notifyNeeded); |
| 55 } |
| 56 |
| 57 void PlatformSensorLinux::NotifyPlatformSensorError() { |
| 58 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 59 NotifySensorError(); |
| 60 } |
| 61 |
47 bool PlatformSensorLinux::StartSensor( | 62 bool PlatformSensorLinux::StartSensor( |
48 const PlatformSensorConfiguration& configuration) { | 63 const PlatformSensorConfiguration& configuration) { |
49 DCHECK(task_runner_->BelongsToCurrentThread()); | 64 DCHECK(task_runner_->BelongsToCurrentThread()); |
50 return polling_thread_task_runner_->PostTask( | 65 if (!sensor_reader_) |
51 FROM_HERE, base::Bind(&PlatformSensorLinux::BeginPoll, | 66 return false; |
52 weak_factory_.GetWeakPtr(), configuration)); | 67 return sensor_reader_->StartFetchingData(configuration, this); |
53 } | 68 } |
54 | 69 |
55 void PlatformSensorLinux::StopSensor() { | 70 void PlatformSensorLinux::StopSensor() { |
56 DCHECK(task_runner_->BelongsToCurrentThread()); | 71 DCHECK(task_runner_->BelongsToCurrentThread()); |
57 polling_thread_task_runner_->PostTask( | 72 DCHECK(sensor_reader_); |
58 FROM_HERE, base::Bind(&PlatformSensorLinux::StopPoll, this)); | 73 sensor_reader_->StopFetchingData(); |
59 } | 74 } |
60 | 75 |
61 bool PlatformSensorLinux::CheckSensorConfiguration( | 76 bool PlatformSensorLinux::CheckSensorConfiguration( |
62 const PlatformSensorConfiguration& configuration) { | 77 const PlatformSensorConfiguration& configuration) { |
63 DCHECK(task_runner_->BelongsToCurrentThread()); | 78 DCHECK(task_runner_->BelongsToCurrentThread()); |
64 // TODO(maksims): make this sensor dependent. | |
65 // For example, in case of accelerometer, check current polling frequency | |
66 // exposed by iio driver. | |
67 return configuration.frequency() > 0 && | 79 return configuration.frequency() > 0 && |
68 configuration.frequency() <= | 80 configuration.frequency() <= default_configuration_.frequency(); |
69 mojom::SensorConfiguration::kMaxAllowedFrequency; | |
70 } | 81 } |
71 | 82 |
72 PlatformSensorConfiguration PlatformSensorLinux::GetDefaultConfiguration() { | 83 PlatformSensorConfiguration PlatformSensorLinux::GetDefaultConfiguration() { |
73 DCHECK(task_runner_->BelongsToCurrentThread()); | 84 DCHECK(task_runner_->BelongsToCurrentThread()); |
74 return default_configuration_; | 85 return default_configuration_; |
75 } | 86 } |
76 | 87 |
77 void PlatformSensorLinux::BeginPoll( | |
78 const PlatformSensorConfiguration& configuration) { | |
79 DCHECK(polling_thread_task_runner_->BelongsToCurrentThread()); | |
80 timer_->Start(FROM_HERE, base::TimeDelta::FromMicroseconds( | |
81 base::Time::kMicrosecondsPerSecond / | |
82 configuration.frequency()), | |
83 this, &PlatformSensorLinux::PollForReadingData); | |
84 } | |
85 | |
86 void PlatformSensorLinux::StopPoll() { | |
87 DCHECK(polling_thread_task_runner_->BelongsToCurrentThread()); | |
88 timer_->Stop(); | |
89 } | |
90 | |
91 void PlatformSensorLinux::PollForReadingData() { | |
92 DCHECK(polling_thread_task_runner_->BelongsToCurrentThread()); | |
93 | |
94 SensorReading reading; | |
95 if (!sensor_reader_->ReadSensorReading(&reading)) { | |
96 task_runner_->PostTask( | |
97 FROM_HERE, base::Bind(&PlatformSensorLinux::NotifySensorError, this)); | |
98 StopPoll(); | |
99 return; | |
100 } | |
101 | |
102 bool notifyNeeded = false; | |
103 if (GetReportingMode() == mojom::ReportingMode::ON_CHANGE) { | |
104 if (!HaveValuesChanged(reading, old_values_)) | |
105 return; | |
106 notifyNeeded = true; | |
107 } | |
108 | |
109 old_values_ = reading; | |
110 reading.timestamp = (base::TimeTicks::Now() - base::TimeTicks()).InSecondsF(); | |
111 UpdateSensorReading(reading, notifyNeeded); | |
112 } | |
113 | |
114 } // namespace device | 88 } // namespace device |
OLD | NEW |