Index: device/generic_sensor/platform_sensor_utils_iio.cc |
diff --git a/device/generic_sensor/platform_sensor_utils_iio.cc b/device/generic_sensor/platform_sensor_utils_iio.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d38bad6005ca8f7950a938a09fd51f4976db3b28 |
--- /dev/null |
+++ b/device/generic_sensor/platform_sensor_utils_iio.cc |
@@ -0,0 +1,91 @@ |
+// 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/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" |
+ |
+namespace device { |
+ |
+namespace { |
+ |
+const base::FilePath::CharType kAmbientLightIioBasePath[] = |
+ FILE_PATH_LITERAL("/sys/bus/iio/devices"); |
+ |
+// This will be templated. |
+bool GetSensorFilePaths(std::vector<base::FilePath>* path) { |
+ // Search the iio/devices directory for a subdirectory (eg "device0" or |
+ // "iio:device0") that contains the "[in_]illuminance[0]_{input|raw}" file. |
+ base::FileEnumerator dir_enumerator(base::FilePath(kAmbientLightIioBasePath), |
+ false, base::FileEnumerator::DIRECTORIES); |
+ const char* input_names[] = { |
+ "in_illuminance0_input", "in_illuminance_input", "in_illuminance0_raw", |
+ "in_illuminance_raw", "illuminance0_input", |
+ }; |
+ |
+ for (base::FilePath check_path = dir_enumerator.Next(); !check_path.empty(); |
+ check_path = dir_enumerator.Next()) { |
+ for (unsigned int i = 0; i < arraysize(input_names); i++) { |
+ base::FilePath als_path = check_path.Append(input_names[i]); |
+ if (base::PathExists(als_path)) { |
+ path->push_back(als_path); |
+ // temporary for als sensor. |
+ return true; |
+ } |
+ } |
+ } |
+ |
+ if (path->empty()) |
+ return false; |
+ return true; |
+} |
+ |
+} // namespace |
+ |
+// static |
+std::unique_ptr<SensorReader> SensorReader::Create(mojom::SensorType type) { |
+ std::vector<base::FilePath> sensor_paths; |
+ std::unique_ptr<SensorReader> sensor_reader = nullptr; |
+ |
+ switch (type) { |
+ case mojom::SensorType::AMBIENT_LIGHT: |
+ if (GetSensorFilePaths(&sensor_paths)) |
+ sensor_reader.reset(new SensorReader(type, std::move(sensor_paths))); |
+ break; |
+ default: |
+ NOTIMPLEMENTED(); |
+ break; |
+ } |
+ |
+ return sensor_reader; |
+} |
+ |
+SensorReader::SensorReader(mojom::SensorType type, |
+ std::vector<base::FilePath> sensor_paths) |
+ : type_(type), sensor_paths_(std::move(sensor_paths)) { |
+ DCHECK(!sensor_paths_.empty()); |
+} |
+ |
+bool SensorReader::ReadSensorValue(double* lux) { |
+ *lux = std::numeric_limits<double>::infinity(); |
+ std::string value; |
+ for (const auto& path : sensor_paths_) { |
+ if (!base::ReadFileToString(path, &value)) |
+ return false; |
+ |
+ base::TrimWhitespaceASCII(value, base::TRIM_ALL, &value); |
+ if (!base::StringToDouble(value, lux)) |
+ return false; |
+ } |
+ return true; |
+} |
+ |
+mojom::SensorType SensorReader::type() const { |
+ return type_; |
+} |
+ |
+} // device |