Chromium Code Reviews| 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/linux/platform_sensor_utils_linux.h" | 5 #include "device/generic_sensor/linux/platform_sensor_utils_linux.h" |
| 6 | 6 |
| 7 #include "base/files/file_enumerator.h" | 7 #include "base/files/file_enumerator.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 #include "base/threading/thread_restrictions.h" | 11 #include "base/threading/thread_restrictions.h" |
| 12 #include "device/generic_sensor/linux/sensor_data_linux.h" | |
| 13 #include "device/generic_sensor/public/cpp/sensor_reading.h" | 12 #include "device/generic_sensor/public/cpp/sensor_reading.h" |
| 14 | 13 |
| 15 namespace device { | 14 namespace device { |
| 16 | 15 |
| 17 namespace { | 16 namespace { |
| 18 | 17 |
| 19 bool InitSensorPaths(const std::vector<std::string>& input_names, | 18 bool InitSensorPaths(const std::vector<std::string>& input_names, |
| 20 const char* base_path, | 19 const char* base_path, |
| 21 std::vector<base::FilePath>* sensor_paths) { | 20 std::vector<base::FilePath>* sensor_paths) { |
| 22 // Search the iio/devices directory for a subdirectory (eg "device0" or | 21 // Search the iio/devices directory for a subdirectory (eg "device0" or |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 45 // treated as a non-existing one. | 44 // treated as a non-existing one. |
| 46 for (auto const& file_names : data.sensor_file_names) { | 45 for (auto const& file_names : data.sensor_file_names) { |
| 47 // Supply InitSensorPaths() with a set of files. | 46 // Supply InitSensorPaths() with a set of files. |
| 48 // Only one file from each set should be found. | 47 // Only one file from each set should be found. |
| 49 if (!InitSensorPaths(file_names, data.base_path_sensor_linux, sensor_paths)) | 48 if (!InitSensorPaths(file_names, data.base_path_sensor_linux, sensor_paths)) |
| 50 return false; | 49 return false; |
| 51 } | 50 } |
| 52 return true; | 51 return true; |
| 53 } | 52 } |
| 54 | 53 |
| 54 double GetSensorScalingValues(const char* scale_file_name, | |
|
Mikhail
2016/11/10 15:45:02
better to pass std::string
maksims (do not use this acc)
2016/11/11 08:52:02
Done.
| |
| 55 const base::FilePath& sensor_base_path) { | |
| 56 double scaling_value = 1; | |
| 57 | |
| 58 base::FilePath scale_path = sensor_base_path.Append(scale_file_name); | |
| 59 if (base::PathExists(scale_path)) { | |
| 60 std::string scale; | |
| 61 if (!base::ReadFileToString(scale_path, &scale)) | |
| 62 return scaling_value; | |
| 63 base::TrimWhitespaceASCII(scale, base::TRIM_ALL, &scale); | |
| 64 double scale_value = 1; | |
|
Reilly Grant (use Gerrit)
2016/11/10 16:53:17
There's no reason to declare a new variable here.
maksims (do not use this acc)
2016/11/11 08:52:02
Done.
| |
| 65 if (!base::StringToDouble(scale, &scale_value)) | |
| 66 return scaling_value; | |
| 67 scaling_value = scale_value; | |
| 68 } | |
| 69 | |
| 70 return scaling_value; | |
| 71 } | |
| 72 | |
| 55 } // namespace | 73 } // namespace |
| 56 | 74 |
| 57 // static | 75 // static |
| 58 std::unique_ptr<SensorReader> SensorReader::Create( | 76 std::unique_ptr<SensorReader> SensorReader::Create( |
| 59 const SensorDataLinux& data) { | 77 const SensorDataLinux& data) { |
| 60 base::ThreadRestrictions::AssertIOAllowed(); | 78 base::ThreadRestrictions::AssertIOAllowed(); |
| 61 std::vector<base::FilePath> sensor_paths; | 79 std::vector<base::FilePath> sensor_paths; |
| 62 if (!GetSensorFilePaths(data, &sensor_paths)) | 80 if (!GetSensorFilePaths(data, &sensor_paths)) |
| 63 return nullptr; | 81 return nullptr; |
| 64 return base::WrapUnique(new SensorReader(std::move(sensor_paths))); | 82 |
| 83 base::FilePath sensor_base_path = sensor_paths.back().DirName(); | |
|
Mikhail
2016/11/10 15:45:02
check that sensor_paths is not empty?
maksims (do not use this acc)
2016/11/11 08:52:02
Done.
| |
| 84 double scaling_value = | |
| 85 GetSensorScalingValues(data.sensor_scale_name.c_str(), sensor_base_path); | |
| 86 | |
| 87 return base::WrapUnique(new SensorReader( | |
| 88 std::move(sensor_paths), scaling_value, data.apply_scaling_func)); | |
| 65 } | 89 } |
| 66 | 90 |
| 67 SensorReader::SensorReader(std::vector<base::FilePath> sensor_paths) | 91 SensorReader::SensorReader( |
| 68 : sensor_paths_(std::move(sensor_paths)) { | 92 std::vector<base::FilePath> sensor_paths, |
| 93 double scaling_value, | |
| 94 const SensorDataLinux::ReaderFunctor& apply_scaling_func) | |
| 95 : sensor_paths_(std::move(sensor_paths)), | |
| 96 scaling_value_(scaling_value), | |
| 97 apply_scaling_func_(apply_scaling_func) { | |
| 69 DCHECK(!sensor_paths_.empty()); | 98 DCHECK(!sensor_paths_.empty()); |
| 70 } | 99 } |
| 71 | 100 |
| 72 SensorReader::~SensorReader() = default; | 101 SensorReader::~SensorReader() = default; |
| 73 | 102 |
| 74 bool SensorReader::ReadSensorReading(SensorReading* reading) { | 103 bool SensorReader::ReadSensorReading(SensorReading* reading) { |
| 75 base::ThreadRestrictions::AssertIOAllowed(); | 104 base::ThreadRestrictions::AssertIOAllowed(); |
| 76 SensorReading readings; | 105 SensorReading readings; |
| 77 DCHECK_LE(sensor_paths_.size(), arraysize(readings.values)); | 106 DCHECK_LE(sensor_paths_.size(), arraysize(readings.values)); |
| 78 int i = 0; | 107 int i = 0; |
| 79 for (const auto& path : sensor_paths_) { | 108 for (const auto& path : sensor_paths_) { |
| 80 std::string new_read_value; | 109 std::string new_read_value; |
| 81 if (!base::ReadFileToString(path, &new_read_value)) | 110 if (!base::ReadFileToString(path, &new_read_value)) |
| 82 return false; | 111 return false; |
| 83 | 112 |
| 84 double new_value = 0; | 113 double new_value = 0; |
| 85 base::TrimWhitespaceASCII(new_read_value, base::TRIM_ALL, &new_read_value); | 114 base::TrimWhitespaceASCII(new_read_value, base::TRIM_ALL, &new_read_value); |
| 86 if (!base::StringToDouble(new_read_value, &new_value)) | 115 if (!base::StringToDouble(new_read_value, &new_value)) |
| 87 return false; | 116 return false; |
| 88 readings.values[i++] = new_value; | 117 readings.values[i++] = new_value; |
| 89 } | 118 } |
| 119 if (!apply_scaling_func_.is_null()) | |
| 120 apply_scaling_func_.Run(scaling_value_, readings); | |
| 90 *reading = readings; | 121 *reading = readings; |
| 91 return true; | 122 return true; |
| 92 } | 123 } |
| 93 | 124 |
| 94 } // namespace device | 125 } // namespace device |
| OLD | NEW |