Chromium Code Reviews| Index: device/generic_sensor/iio/platform_sensor_utils_iio.cc |
| diff --git a/device/generic_sensor/iio/platform_sensor_utils_iio.cc b/device/generic_sensor/iio/platform_sensor_utils_iio.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a16a86a7eff4ec1a56837af3ac46d33d6deea868 |
| --- /dev/null |
| +++ b/device/generic_sensor/iio/platform_sensor_utils_iio.cc |
| @@ -0,0 +1,96 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "device/generic_sensor/iio/platform_sensor_utils_iio.h" |
| + |
| +#include "base/files/file_enumerator.h" |
| +#include "base/files/file_util.h" |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/strings/string_util.h" |
| +#include "base/threading/thread_restrictions.h" |
| +#include "device/generic_sensor/iio/sensor_data_iio.h" |
| +#include "device/generic_sensor/public/cpp/sensor_reading.h" |
| + |
| +namespace device { |
| + |
| +namespace { |
| + |
| +bool InitSensorPaths(const char** input_names, |
| + const char* base_path, |
| + size_t size, |
| + std::vector<base::FilePath>* sensor_paths) { |
| + // Search the iio/devices directory for a subdirectory (eg "device0" or |
| + // "iio:device0") that contains the specified input_name file (eg |
| + // "in_illuminance_input" or "in_illuminance0_input"). |
| + base::FileEnumerator dir_enumerator(base::FilePath(base_path), false, |
| + base::FileEnumerator::DIRECTORIES); |
| + for (base::FilePath check_path = dir_enumerator.Next(); !check_path.empty(); |
| + check_path = dir_enumerator.Next()) { |
| + for (size_t i = 0; i < size; ++i) { |
| + base::FilePath full_path = check_path.Append(*(input_names + i)); |
| + if (base::PathExists(full_path)) { |
| + sensor_paths->push_back(full_path); |
| + return true; |
| + } |
| + } |
| + } |
| + return false; |
| +} |
| + |
| +bool GetSensorFilePaths(const SensorDataIio& data, |
| + std::vector<base::FilePath>* sensor_paths) { |
| + DCHECK(sensor_paths->empty()); |
| + // Depending on a sensor, there can be up to three sets of files that need |
| + // to be checked. If one of three files is not found, a sensor is |
| + // treated as a non-existing one. |
| + for (size_t i = 0; i < data.sensor_file_names_rows; ++i) { |
| + // Supply InitSensorPaths() with a set of files. |
| + // Only one file from each set should be found. |
| + if (!InitSensorPaths(data.sensor_file_names[i], data.base_path_sensor_iio, |
| + data.sensor_file_names_cols, sensor_paths)) { |
| + return false; |
| + } |
| + } |
| + return true; |
| +} |
| + |
| +} // namespace |
| + |
| +// static |
| +std::unique_ptr<SensorReader> SensorReader::Create(const SensorDataIio& data) { |
| + base::ThreadRestrictions::AssertIOAllowed(); |
| + std::vector<base::FilePath> sensor_paths; |
| + if (!GetSensorFilePaths(data, &sensor_paths)) { |
| + return nullptr; |
| + } |
|
Reilly Grant (use Gerrit)
2016/10/24 21:49:27
nit: No braces around a single-line if.
maksims (do not use this acc)
2016/10/25 06:23:53
Done.
|
| + return base::WrapUnique(new SensorReader(std::move(sensor_paths))); |
|
Reilly Grant (use Gerrit)
2016/10/24 21:49:26
base::MakeUnique<SensorReader>(std::move(sensor_pa
maksims (do not use this acc)
2016/10/25 06:23:54
Done.
maksims (do not use this acc)
2016/10/25 06:50:14
The constructor is private.
|
| +} |
| + |
| +SensorReader::SensorReader(std::vector<base::FilePath> sensor_paths) |
| + : sensor_paths_(std::move(sensor_paths)) { |
| + DCHECK(!sensor_paths_.empty()); |
| +} |
| + |
| +SensorReader::~SensorReader() = default; |
| + |
| +bool SensorReader::ReadSensorReading(SensorReading* reading) { |
| + base::ThreadRestrictions::AssertIOAllowed(); |
| + SensorReading readings; |
| + int i = 0; |
|
Reilly Grant (use Gerrit)
2016/10/24 21:49:26
DCHECK_LE(sensor_paths_.size(), arraysize(readings
maksims (do not use this acc)
2016/10/25 06:23:54
Done.
|
| + for (const auto& path : sensor_paths_) { |
| + std::string new_read_value; |
| + if (!base::ReadFileToString(path, &new_read_value)) |
| + return false; |
| + |
| + double new_value = 0; |
| + base::TrimWhitespaceASCII(new_read_value, base::TRIM_ALL, &new_read_value); |
| + if (!base::StringToDouble(new_read_value, &new_value)) |
| + return false; |
| + readings.values[i++] = new_value; |
| + } |
| + *reading = readings; |
| + return true; |
| +} |
| + |
| +} // namespace device |