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