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