OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "device/generic_sensor/ambient_light_reader_iio.h" |
| 6 |
| 7 #include "base/files/file_enumerator.h" |
| 8 #include "base/files/file_util.h" |
| 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/string_util.h" |
| 11 |
| 12 namespace device { |
| 13 |
| 14 namespace { |
| 15 |
| 16 // Base path where sensors are located. |
| 17 const base::FilePath::CharType kAmbientLightIioBasePath[] = |
| 18 FILE_PATH_LITERAL("/sys/bus/iio/devices"); |
| 19 |
| 20 } // namespace |
| 21 |
| 22 AmbientLightSensorReaderIio::AmbientLightSensorReaderIio() = default; |
| 23 AmbientLightSensorReaderIio::~AmbientLightSensorReaderIio() = default; |
| 24 |
| 25 bool AmbientLightSensorReaderIio::ReadSensorLuxValue(double* lux) { |
| 26 *lux = std::numeric_limits<double>::infinity(); |
| 27 std::string value; |
| 28 if (!base::ReadFileToString(sensor_path_, &value)) |
| 29 return false; |
| 30 |
| 31 base::TrimWhitespaceASCII(value, base::TRIM_ALL, &value); |
| 32 if (!base::StringToDouble(value, lux)) |
| 33 return false; |
| 34 return true; |
| 35 } |
| 36 |
| 37 bool AmbientLightSensorReaderIio::DetectLightSensor() { |
| 38 // Search the iio/devices directory for a subdirectory (eg "device0" or |
| 39 // "iio:device0") that contains the "[in_]illuminance[0]_{input|raw}" file. |
| 40 base::FileEnumerator dir_enumerator(base::FilePath(kAmbientLightIioBasePath), |
| 41 false, base::FileEnumerator::DIRECTORIES); |
| 42 const char* input_names[] = { |
| 43 "in_illuminance0_input", "in_illuminance_input", "in_illuminance0_raw", |
| 44 "in_illuminance_raw", "illuminance0_input", |
| 45 }; |
| 46 |
| 47 for (base::FilePath check_path = dir_enumerator.Next(); !check_path.empty(); |
| 48 check_path = dir_enumerator.Next()) { |
| 49 for (unsigned int i = 0; i < arraysize(input_names); i++) { |
| 50 base::FilePath als_path = check_path.Append(input_names[i]); |
| 51 if (base::PathExists(als_path)) { |
| 52 sensor_path_ = als_path; |
| 53 return true; |
| 54 } |
| 55 } |
| 56 } |
| 57 return false; |
| 58 } |
| 59 |
| 60 } // namespace content |
OLD | NEW |