Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(268)

Side by Side Diff: device/generic_sensor/platform_sensor_utils_iio.cc

Issue 2370343002: [sensors] Ambient light sensor implementation for ChromeOS and Linux. (Closed)
Patch Set: changed implementation Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "device/generic_sensor/platform_sensor_utils_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 const base::FilePath::CharType kAmbientLightIioBasePath[] =
17 FILE_PATH_LITERAL("/sys/bus/iio/devices");
18
19 // This will be templated.
20 bool GetSensorFilePaths(std::vector<base::FilePath>* path) {
21 // Search the iio/devices directory for a subdirectory (eg "device0" or
22 // "iio:device0") that contains the "[in_]illuminance[0]_{input|raw}" file.
23 base::FileEnumerator dir_enumerator(base::FilePath(kAmbientLightIioBasePath),
24 false, base::FileEnumerator::DIRECTORIES);
25 const char* input_names[] = {
26 "in_illuminance0_input", "in_illuminance_input", "in_illuminance0_raw",
27 "in_illuminance_raw", "illuminance0_input",
28 };
29
30 for (base::FilePath check_path = dir_enumerator.Next(); !check_path.empty();
31 check_path = dir_enumerator.Next()) {
32 for (unsigned int i = 0; i < arraysize(input_names); i++) {
33 base::FilePath als_path = check_path.Append(input_names[i]);
34 if (base::PathExists(als_path)) {
35 path->push_back(als_path);
36 // temporary for als sensor.
37 return true;
38 }
39 }
40 }
41
42 if (path->empty())
43 return false;
44 return true;
45 }
46
47 } // namespace
48
49 // static
50 std::unique_ptr<SensorReader> SensorReader::Create(mojom::SensorType type) {
51 std::vector<base::FilePath> sensor_paths;
52 std::unique_ptr<SensorReader> sensor_reader = nullptr;
53
54 switch (type) {
55 case mojom::SensorType::AMBIENT_LIGHT:
56 if (GetSensorFilePaths(&sensor_paths))
57 sensor_reader.reset(new SensorReader(type, std::move(sensor_paths)));
58 break;
59 default:
60 NOTIMPLEMENTED();
61 break;
62 }
63
64 return sensor_reader;
65 }
66
67 SensorReader::SensorReader(mojom::SensorType type,
68 std::vector<base::FilePath> sensor_paths)
69 : type_(type), sensor_paths_(std::move(sensor_paths)) {
70 DCHECK(!sensor_paths_.empty());
71 }
72
73 bool SensorReader::ReadSensorValue(double* lux) {
74 *lux = std::numeric_limits<double>::infinity();
75 std::string value;
76 for (const auto& path : sensor_paths_) {
77 if (!base::ReadFileToString(path, &value))
78 return false;
79
80 base::TrimWhitespaceASCII(value, base::TRIM_ALL, &value);
81 if (!base::StringToDouble(value, lux))
82 return false;
83 }
84 return true;
85 }
86
87 mojom::SensorType SensorReader::type() const {
88 return type_;
89 }
90
91 } // device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698