 Chromium Code Reviews
 Chromium Code Reviews Issue 2370343002:
  [sensors] Ambient light sensor implementation for ChromeOS and Linux.  (Closed)
    
  
    Issue 2370343002:
  [sensors] Ambient light sensor implementation for ChromeOS and Linux.  (Closed) 
  | 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..0c9274868458168d6d8bc0c65e18feb2daffa929 | 
| --- /dev/null | 
| +++ b/device/generic_sensor/iio/platform_sensor_utils_iio.cc | 
| @@ -0,0 +1,93 @@ | 
| +// 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" | 
| + | 
| +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++) { | 
| 
Mikhail
2016/10/17 10:34:11
nit: ++i
 
maksims (do not use this acc)
2016/10/18 06:50:41
Done.
 | 
| + base::FilePath als_path = check_path.Append(*(input_names + i)); | 
| 
Mikhail
2016/10/17 10:34:11
why als_ ?
 
maksims (do not use this acc)
2016/10/18 06:50:41
left from prev versions.
 | 
| + if (base::PathExists(als_path)) { | 
| + sensor_paths->push_back(als_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; | 
| + } | 
| + return base::WrapUnique(new SensorReader(std::move(sensor_paths))); | 
| +} | 
| + | 
| +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(); | 
| + int i = 0; | 
| + 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; | 
| + reading->values[i++] = new_value; | 
| 
Mikhail
2016/10/17 10:34:11
given parameter should not be modified unless the
 
maksims (do not use this acc)
2016/10/18 06:50:41
Done.
 | 
| + } | 
| + return true; | 
| +} | 
| + | 
| +} // namespace device |