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