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