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

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

Issue 2370343002: [sensors] Ambient light sensor implementation for ChromeOS and Linux. (Closed)
Patch Set: Newest version. and some comments 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/iio/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 bool InitSensorPaths(const char** input_names,
17 const char* base_path,
18 size_t size,
19 std::vector<base::FilePath>* sensor_paths) {
20 // Search the iio/devices directory for a subdirectory (eg "device0" or
21 // "iio:device0") that contains the specified input_name file (eg
22 // "in_illuminance_input" or "in_illuminance0_input").
23 base::FileEnumerator dir_enumerator(base::FilePath(base_path), false,
24 base::FileEnumerator::DIRECTORIES);
25
26 for (base::FilePath check_path = dir_enumerator.Next(); !check_path.empty();
27 check_path = dir_enumerator.Next()) {
28 for (size_t i = 0; i < size; i++) {
29 base::FilePath als_path = check_path.Append(*(input_names + i));
30 if (base::PathExists(als_path)) {
31 sensor_paths->push_back(als_path);
32 return true;
33 }
34 }
35 }
36 return false;
37 }
38
39 bool GetSensorFilePaths(const SensorDataIio& data,
40 std::vector<base::FilePath>* sensor_paths) {
41 DCHECK(sensor_paths->empty());
42 // Depending on a sensor, there can be up to three sets of files that need
43 // to be checked. If one of three files is not found, a sensor is
44 // treated as a non-existing one.
45 for (size_t i = 0; i < data.sensor_file_names_rows; i++) {
46 // Supply InitSensorPaths() with a set of files.
47 // Only one file from each set should be found.
48 if (!InitSensorPaths(data.sensor_file_names[i], data.base_path_sensor_iio,
49 data.sensor_file_names_cols, sensor_paths)) {
50 return false;
51 }
52 }
53 return true;
54 }
55
56 } // namespace
57
58 // static
59 std::unique_ptr<SensorReader> SensorReader::Create(const SensorDataIio& data) {
60 std::vector<base::FilePath> sensor_paths;
61 if (!GetSensorFilePaths(data, &sensor_paths)) {
62 return nullptr;
63 }
64 return base::WrapUnique(new SensorReader(std::move(sensor_paths)));
65 }
66
67 SensorReader::SensorReader(std::vector<base::FilePath> sensor_paths)
68 : sensor_paths_(std::move(sensor_paths)) {
69 DCHECK(!sensor_paths_.empty());
70 }
71
72 SensorReader::~SensorReader() = default;
73
74 bool SensorReader::ReadSensorReading(SensorReading* reading) {
75 int i = 0;
76 for (const auto& path : sensor_paths_) {
77 std::string new_read_value;
78 if (!base::ReadFileToString(path, &new_read_value))
79 return false;
80
81 double new_value = 0;
82 base::TrimWhitespaceASCII(new_read_value, base::TRIM_ALL, &new_read_value);
83 if (!base::StringToDouble(new_read_value, &new_value))
84 return false;
85 reading->values[i++] = new_value;
86 }
87 return true;
88 }
89
90 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698