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/threading/thread.h" | |
8 #include "base/timer/timer.h" | |
9 #include "device/generic_sensor/linux/platform_sensor_utils_linux.h" | |
10 #include "device/generic_sensor/linux/sensor_data_linux.h" | |
11 | |
12 namespace device { | |
13 | |
14 namespace { | |
15 | |
16 // Checks if at least one value has been changed. | |
17 bool HaveValuesChanged(const SensorReading& lhs, const SensorReading& rhs) { | |
18 return lhs.values[0] != rhs.values[0] || lhs.values[1] != rhs.values[1] || | |
19 lhs.values[2] != rhs.values[2]; | |
20 } | |
21 | |
22 } // namespace | |
23 | |
24 PlatformSensorLinux::PlatformSensorLinux( | |
25 mojom::SensorType type, | |
26 mojo::ScopedSharedBufferMapping mapping, | |
27 PlatformSensorProvider* provider, | |
28 const SensorDataLinux& data, | |
29 std::unique_ptr<SensorReader> sensor_reader, | |
30 scoped_refptr<base::SingleThreadTaskRunner> polling_thread_task_runner_) | |
31 : PlatformSensor(type, std::move(mapping), provider), | |
32 timer_(new base::RepeatingTimer()), | |
33 default_configuration_(data.default_configuration), | |
34 reporting_mode_(data.reporting_mode), | |
35 sensor_reader_(std::move(sensor_reader)), | |
36 polling_thread_task_runner_(polling_thread_task_runner_), | |
37 weak_factory_(this) {} | |
38 | |
39 PlatformSensorLinux::~PlatformSensorLinux() { | |
40 polling_thread_task_runner_->DeleteSoon(FROM_HERE, timer_); | |
41 } | |
42 | |
43 mojom::ReportingMode PlatformSensorLinux::GetReportingMode() { | |
44 return reporting_mode_; | |
45 } | |
46 | |
47 bool PlatformSensorLinux::StartSensor( | |
48 const PlatformSensorConfiguration& configuration) { | |
49 DCHECK(task_runner_->BelongsToCurrentThread()); | |
50 return polling_thread_task_runner_->PostTask( | |
51 FROM_HERE, base::Bind(&PlatformSensorLinux::BeginPoll, | |
52 weak_factory_.GetWeakPtr(), configuration)); | |
53 } | |
54 | |
55 void PlatformSensorLinux::StopSensor() { | |
56 DCHECK(task_runner_->BelongsToCurrentThread()); | |
57 polling_thread_task_runner_->PostTask( | |
58 FROM_HERE, base::Bind(&PlatformSensorLinux::StopPoll, this)); | |
59 } | |
60 | |
61 bool PlatformSensorLinux::CheckSensorConfiguration( | |
62 const PlatformSensorConfiguration& configuration) { | |
63 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 && | |
68 configuration.frequency() <= | |
69 mojom::SensorConfiguration::kMaxAllowedFrequency; | |
70 } | |
71 | |
72 PlatformSensorConfiguration PlatformSensorLinux::GetDefaultConfiguration() { | |
73 DCHECK(task_runner_->BelongsToCurrentThread()); | |
74 return default_configuration_; | |
75 } | |
76 | |
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 | |
OLD | NEW |