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