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_reader_linux.h" | 5 #include "device/generic_sensor/platform_sensor_reader_linux.h" |
6 | 6 |
7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/threading/thread_restrictions.h" | 10 #include "base/threading/thread_restrictions.h" |
11 #include "base/timer/timer.h" | 11 #include "base/timer/timer.h" |
12 #include "device/generic_sensor/linux/sensor_data_linux.h" | 12 #include "device/generic_sensor/linux/sensor_data_linux.h" |
13 #include "device/generic_sensor/platform_sensor_linux.h" | 13 #include "device/generic_sensor/platform_sensor_linux.h" |
14 #include "device/generic_sensor/public/cpp/sensor_reading.h" | 14 #include "device/generic_sensor/public/cpp/sensor_reading.h" |
15 | 15 |
16 namespace device { | 16 namespace device { |
17 | 17 |
18 class PollingSensorReader : public SensorReader { | 18 class PollingSensorReader : public SensorReader { |
19 public: | 19 public: |
20 PollingSensorReader( | 20 PollingSensorReader(const SensorInfoLinux* sensor_device, |
21 const SensorInfoLinux* sensor_device, | 21 base::WeakPtr<PlatformSensorLinux> sensor, |
22 PlatformSensorLinux* sensor, | 22 scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
23 scoped_refptr<base::SingleThreadTaskRunner> polling_task_runner); | |
24 ~PollingSensorReader() override; | 23 ~PollingSensorReader() override; |
25 | 24 |
26 // SensorReader implements: | 25 // SensorReader implements: |
27 bool StartFetchingData( | 26 void StartFetchingData( |
28 const PlatformSensorConfiguration& configuration) override; | 27 const PlatformSensorConfiguration& configuration) override; |
29 void StopFetchingData() override; | 28 void StopFetchingData() override; |
30 | 29 |
31 private: | 30 private: |
32 // Initializes a read timer. | 31 // Initializes a read timer. |
33 void InitializeTimer(const PlatformSensorConfiguration& configuration); | 32 void InitializeTimer(const PlatformSensorConfiguration& configuration); |
34 | 33 |
35 // Polls data and sends it to a |sensor_|. | 34 // Polls data and sends it to a |sensor_|. |
36 void PollForData(); | 35 void PollForData(); |
37 | 36 |
38 // Paths to sensor read files. | 37 // Paths to sensor read files. |
39 const std::vector<base::FilePath> sensor_file_paths_; | 38 const std::vector<base::FilePath> sensor_file_paths_; |
40 | 39 |
41 // Scaling value that are applied to raw data from sensors. | 40 // Scaling value that are applied to raw data from sensors. |
42 const double scaling_value_; | 41 const double scaling_value_; |
43 | 42 |
44 // Offset value. | 43 // Offset value. |
45 const double offset_value_; | 44 const double offset_value_; |
46 | 45 |
47 // Used to apply scalings and invert signs if needed. | 46 // Used to apply scalings and invert signs if needed. |
48 const SensorPathsLinux::ReaderFunctor apply_scaling_func_; | 47 const SensorPathsLinux::ReaderFunctor apply_scaling_func_; |
49 | 48 |
50 // Owned pointer to a timer. Will be deleted on a polling thread once | 49 // Repeating timer for data polling. |
51 // destructor is called. | 50 base::RepeatingTimer timer_; |
52 base::RepeatingTimer* timer_; | |
53 | |
54 base::WeakPtrFactory<PollingSensorReader> weak_factory_; | |
55 | 51 |
56 DISALLOW_COPY_AND_ASSIGN(PollingSensorReader); | 52 DISALLOW_COPY_AND_ASSIGN(PollingSensorReader); |
57 }; | 53 }; |
58 | 54 |
59 PollingSensorReader::PollingSensorReader( | 55 PollingSensorReader::PollingSensorReader( |
60 const SensorInfoLinux* sensor_device, | 56 const SensorInfoLinux* sensor_device, |
61 PlatformSensorLinux* sensor, | 57 base::WeakPtr<PlatformSensorLinux> sensor, |
62 scoped_refptr<base::SingleThreadTaskRunner> polling_task_runner) | 58 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
63 : SensorReader(sensor, polling_task_runner), | 59 : SensorReader(sensor, std::move(task_runner)), |
64 sensor_file_paths_(sensor_device->device_reading_files), | 60 sensor_file_paths_(sensor_device->device_reading_files), |
65 scaling_value_(sensor_device->device_scaling_value), | 61 scaling_value_(sensor_device->device_scaling_value), |
66 offset_value_(sensor_device->device_offset_value), | 62 offset_value_(sensor_device->device_offset_value), |
67 apply_scaling_func_(sensor_device->apply_scaling_func), | 63 apply_scaling_func_(sensor_device->apply_scaling_func) {} |
68 timer_(new base::RepeatingTimer()), | |
69 weak_factory_(this) {} | |
70 | 64 |
71 PollingSensorReader::~PollingSensorReader() { | 65 PollingSensorReader::~PollingSensorReader() { |
72 polling_task_runner_->DeleteSoon(FROM_HERE, timer_); | 66 DCHECK(thread_checker_.CalledOnValidThread()); |
73 } | 67 } |
74 | 68 |
75 bool PollingSensorReader::StartFetchingData( | 69 void PollingSensorReader::StartFetchingData( |
76 const PlatformSensorConfiguration& configuration) { | 70 const PlatformSensorConfiguration& configuration) { |
| 71 DCHECK(thread_checker_.CalledOnValidThread()); |
77 if (is_reading_active_) | 72 if (is_reading_active_) |
78 StopFetchingData(); | 73 StopFetchingData(); |
79 | 74 InitializeTimer(configuration); |
80 return polling_task_runner_->PostTask( | |
81 FROM_HERE, base::Bind(&PollingSensorReader::InitializeTimer, | |
82 weak_factory_.GetWeakPtr(), configuration)); | |
83 } | 75 } |
84 | 76 |
85 void PollingSensorReader::StopFetchingData() { | 77 void PollingSensorReader::StopFetchingData() { |
| 78 DCHECK(thread_checker_.CalledOnValidThread()); |
86 is_reading_active_ = false; | 79 is_reading_active_ = false; |
87 timer_->Stop(); | 80 timer_.Stop(); |
88 } | 81 } |
89 | 82 |
90 void PollingSensorReader::InitializeTimer( | 83 void PollingSensorReader::InitializeTimer( |
91 const PlatformSensorConfiguration& configuration) { | 84 const PlatformSensorConfiguration& configuration) { |
92 DCHECK(polling_task_runner_->BelongsToCurrentThread()); | 85 DCHECK(thread_checker_.CalledOnValidThread()); |
93 timer_->Start(FROM_HERE, base::TimeDelta::FromMicroseconds( | 86 DCHECK(!is_reading_active_); |
94 base::Time::kMicrosecondsPerSecond / | 87 timer_.Start(FROM_HERE, base::TimeDelta::FromMicroseconds( |
95 configuration.frequency()), | 88 base::Time::kMicrosecondsPerSecond / |
96 this, &PollingSensorReader::PollForData); | 89 configuration.frequency()), |
| 90 this, &PollingSensorReader::PollForData); |
97 is_reading_active_ = true; | 91 is_reading_active_ = true; |
98 } | 92 } |
99 | 93 |
100 void PollingSensorReader::PollForData() { | 94 void PollingSensorReader::PollForData() { |
101 DCHECK(polling_task_runner_->BelongsToCurrentThread()); | 95 DCHECK(thread_checker_.CalledOnValidThread()); |
102 base::ThreadRestrictions::AssertIOAllowed(); | |
103 | 96 |
104 SensorReading readings; | 97 SensorReading readings; |
105 DCHECK_LE(sensor_file_paths_.size(), arraysize(readings.values)); | 98 DCHECK_LE(sensor_file_paths_.size(), arraysize(readings.values)); |
106 int i = 0; | 99 int i = 0; |
107 for (const auto& path : sensor_file_paths_) { | 100 for (const auto& path : sensor_file_paths_) { |
108 std::string new_read_value; | 101 std::string new_read_value; |
109 if (!base::ReadFileToString(path, &new_read_value)) { | 102 if (!base::ReadFileToString(path, &new_read_value)) { |
110 NotifyReadError(); | 103 NotifyReadError(); |
111 StopFetchingData(); | 104 StopFetchingData(); |
112 return; | 105 return; |
113 } | 106 } |
114 | 107 |
115 double new_value = 0; | 108 double new_value = 0; |
116 base::TrimWhitespaceASCII(new_read_value, base::TRIM_ALL, &new_read_value); | 109 base::TrimWhitespaceASCII(new_read_value, base::TRIM_ALL, &new_read_value); |
117 if (!base::StringToDouble(new_read_value, &new_value)) { | 110 if (!base::StringToDouble(new_read_value, &new_value)) { |
118 NotifyReadError(); | 111 NotifyReadError(); |
119 StopFetchingData(); | 112 StopFetchingData(); |
120 return; | 113 return; |
121 } | 114 } |
122 readings.values[i++] = new_value; | 115 readings.values[i++] = new_value; |
123 } | 116 } |
124 if (!apply_scaling_func_.is_null()) | 117 if (!apply_scaling_func_.is_null()) |
125 apply_scaling_func_.Run(scaling_value_, offset_value_, readings); | 118 apply_scaling_func_.Run(scaling_value_, offset_value_, readings); |
126 | 119 |
127 if (is_reading_active_) { | 120 if (is_reading_active_) { |
128 task_runner_->PostTask( | 121 task_runner_->PostTask( |
129 FROM_HERE, base::Bind(&PlatformSensorLinux::UpdatePlatformSensorReading, | 122 FROM_HERE, base::Bind(&PlatformSensorLinux::UpdatePlatformSensorReading, |
130 base::Unretained(sensor_), readings)); | 123 sensor_, readings)); |
131 } | 124 } |
132 } | 125 } |
133 | 126 |
134 // static | 127 // static |
135 std::unique_ptr<SensorReader> SensorReader::Create( | 128 std::unique_ptr<SensorReader> SensorReader::Create( |
136 const SensorInfoLinux* sensor_device, | 129 const SensorInfoLinux* sensor_device, |
137 PlatformSensorLinux* sensor, | 130 base::WeakPtr<PlatformSensorLinux> sensor, |
138 scoped_refptr<base::SingleThreadTaskRunner> polling_thread_task_runner) { | 131 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| 132 base::ThreadRestrictions::AssertIOAllowed(); |
139 // TODO(maksims): implement triggered reading. At the moment, | 133 // TODO(maksims): implement triggered reading. At the moment, |
140 // only polling read is supported. | 134 // only polling read is supported. |
141 return base::MakeUnique<PollingSensorReader>(sensor_device, sensor, | 135 return base::MakeUnique<PollingSensorReader>(sensor_device, sensor, |
142 polling_thread_task_runner); | 136 std::move(task_runner)); |
143 } | 137 } |
144 | 138 |
145 SensorReader::SensorReader( | 139 SensorReader::SensorReader( |
146 PlatformSensorLinux* sensor, | 140 base::WeakPtr<PlatformSensorLinux> sensor, |
147 scoped_refptr<base::SingleThreadTaskRunner> polling_task_runner) | 141 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
148 : sensor_(sensor), | 142 : sensor_(sensor), |
149 polling_task_runner_(polling_task_runner), | 143 task_runner_(std::move(task_runner)), |
150 task_runner_(base::ThreadTaskRunnerHandle::Get()), | 144 is_reading_active_(false) { |
151 is_reading_active_(false) {} | 145 thread_checker_.DetachFromThread(); |
| 146 } |
152 | 147 |
153 SensorReader::~SensorReader() = default; | 148 SensorReader::~SensorReader() { |
| 149 DCHECK(thread_checker_.CalledOnValidThread()); |
| 150 } |
154 | 151 |
155 void SensorReader::NotifyReadError() { | 152 void SensorReader::NotifyReadError() { |
| 153 DCHECK(thread_checker_.CalledOnValidThread()); |
156 if (is_reading_active_) { | 154 if (is_reading_active_) { |
157 task_runner_->PostTask( | 155 task_runner_->PostTask( |
158 FROM_HERE, base::Bind(&PlatformSensorLinux::NotifyPlatformSensorError, | 156 FROM_HERE, |
159 base::Unretained(sensor_))); | 157 base::Bind(&PlatformSensorLinux::NotifyPlatformSensorError, sensor_)); |
160 } | 158 } |
161 } | 159 } |
162 | 160 |
163 } // namespace device | 161 } // namespace device |
OLD | NEW |