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_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 "device/generic_sensor/linux/sensor_data_linux.h" |
| 15 #include "device/generic_sensor/platform_sensor_linux.h" |
| 16 #include "device/generic_sensor/public/cpp/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(FROM_HERE, base::TimeDelta::FromMicroseconds( |
| 90 base::Time::kMicrosecondsPerSecond / |
| 91 configuration.frequency()), |
| 92 this, &PollingSensorReader::PollForData); |
| 93 is_reading_active_ = true; |
| 94 } |
| 95 |
| 96 void PollingSensorReader::PollForData() { |
| 97 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 98 |
| 99 SensorReading readings; |
| 100 DCHECK_LE(sensor_file_paths_.size(), arraysize(readings.values)); |
| 101 int i = 0; |
| 102 for (const auto& path : sensor_file_paths_) { |
| 103 std::string new_read_value; |
| 104 if (!base::ReadFileToString(path, &new_read_value)) { |
| 105 NotifyReadError(); |
| 106 StopFetchingData(); |
| 107 return; |
| 108 } |
| 109 |
| 110 double new_value = 0; |
| 111 base::TrimWhitespaceASCII(new_read_value, base::TRIM_ALL, &new_read_value); |
| 112 if (!base::StringToDouble(new_read_value, &new_value)) { |
| 113 NotifyReadError(); |
| 114 StopFetchingData(); |
| 115 return; |
| 116 } |
| 117 readings.values[i++] = new_value; |
| 118 } |
| 119 if (!apply_scaling_func_.is_null()) |
| 120 apply_scaling_func_.Run(scaling_value_, offset_value_, readings); |
| 121 |
| 122 if (is_reading_active_) { |
| 123 task_runner_->PostTask( |
| 124 FROM_HERE, base::Bind(&PlatformSensorLinux::UpdatePlatformSensorReading, |
| 125 sensor_, readings)); |
| 126 } |
| 127 } |
| 128 |
| 129 // static |
| 130 std::unique_ptr<SensorReader> SensorReader::Create( |
| 131 const SensorInfoLinux* sensor_device, |
| 132 base::WeakPtr<PlatformSensorLinux> sensor, |
| 133 scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| 134 base::ThreadRestrictions::AssertIOAllowed(); |
| 135 // TODO(maksims): implement triggered reading. At the moment, |
| 136 // only polling read is supported. |
| 137 return base::MakeUnique<PollingSensorReader>(sensor_device, sensor, |
| 138 std::move(task_runner)); |
| 139 } |
| 140 |
| 141 SensorReader::SensorReader( |
| 142 base::WeakPtr<PlatformSensorLinux> sensor, |
| 143 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 144 : sensor_(sensor), |
| 145 task_runner_(std::move(task_runner)), |
| 146 is_reading_active_(false) { |
| 147 DETACH_FROM_THREAD(thread_checker_); |
| 148 } |
| 149 |
| 150 SensorReader::~SensorReader() { |
| 151 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 152 } |
| 153 |
| 154 void SensorReader::NotifyReadError() { |
| 155 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_); |
| 156 if (is_reading_active_) { |
| 157 task_runner_->PostTask( |
| 158 FROM_HERE, |
| 159 base::Bind(&PlatformSensorLinux::NotifyPlatformSensorError, sensor_)); |
| 160 } |
| 161 } |
| 162 |
| 163 } // namespace device |
OLD | NEW |