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

Side by Side Diff: device/generic_sensor/linux/platform_sensor_utils_linux.cc

Issue 2533793002: [sensors](CrOS/Linux) Implement Sensor device manager for sensors (Closed)
Patch Set: construct manager Created 4 years 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/linux/platform_sensor_utils_linux.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 #include "base/threading/thread_restrictions.h"
12 #include "device/generic_sensor/public/cpp/sensor_reading.h"
13
14 namespace device {
15
16 namespace {
17
18 bool InitSensorPaths(const std::vector<std::string>& input_names,
19 const char* base_path,
20 std::vector<base::FilePath>* sensor_paths) {
21 // Search the iio/devices directory for a subdirectory (eg "device0" or
22 // "iio:device0") that contains the specified input_name file (eg
23 // "in_illuminance_input" or "in_illuminance0_input").
24 base::FileEnumerator dir_enumerator(base::FilePath(base_path), false,
25 base::FileEnumerator::DIRECTORIES);
26 for (base::FilePath check_path = dir_enumerator.Next(); !check_path.empty();
27 check_path = dir_enumerator.Next()) {
28 for (auto const& file_name : input_names) {
29 base::FilePath full_path = check_path.Append(file_name);
30 if (base::PathExists(full_path)) {
31 sensor_paths->push_back(full_path);
32 return true;
33 }
34 }
35 }
36 return false;
37 }
38
39 bool GetSensorFilePaths(const SensorDataLinux& 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 (auto const& file_names : data.sensor_file_names) {
46 // Supply InitSensorPaths() with a set of files.
47 // Only one file from each set should be found.
48 if (!InitSensorPaths(file_names, data.base_path_sensor_linux, sensor_paths))
49 return false;
50 }
51 return true;
52 }
53
54 // Returns -1 if unable to read a scaling value.
55 // Otherwise, returns a scaling value read from a file.
56 double ReadSensorScalingValue(const base::FilePath& scale_file_path) {
57 std::string value;
58 if (!base::ReadFileToString(scale_file_path, &value))
59 return -1;
60
61 double scaling_value;
62 base::TrimWhitespaceASCII(value, base::TRIM_ALL, &value);
63 if (!base::StringToDouble(value, &scaling_value))
64 return -1;
65 return scaling_value;
66 }
67
68 } // namespace
69
70 // static
71 std::unique_ptr<SensorReader> SensorReader::Create(
72 const SensorDataLinux& data) {
73 base::ThreadRestrictions::AssertIOAllowed();
74 std::vector<base::FilePath> sensor_paths;
75 if (!GetSensorFilePaths(data, &sensor_paths))
76 return nullptr;
77
78 DCHECK(!sensor_paths.empty());
79 // Scaling value is 1 if scaling file is not specified is |data| or scaling
80 // file is not found.
81 double scaling_value = 1;
82 if (!data.sensor_scale_name.empty()) {
83 const base::FilePath scale_file_path =
84 sensor_paths.back().DirName().Append(data.sensor_scale_name);
85 if (base::PathExists(scale_file_path))
86 scaling_value = ReadSensorScalingValue(scale_file_path);
87 }
88
89 // A file with a scaling value is found, but couldn't be read.
90 if (scaling_value == -1)
91 return nullptr;
92
93 return base::WrapUnique(new SensorReader(
94 std::move(sensor_paths), scaling_value, data.apply_scaling_func));
95 }
96
97 SensorReader::SensorReader(
98 std::vector<base::FilePath> sensor_paths,
99 double scaling_value,
100 const SensorDataLinux::ReaderFunctor& apply_scaling_func)
101 : sensor_paths_(std::move(sensor_paths)),
102 scaling_value_(scaling_value),
103 apply_scaling_func_(apply_scaling_func) {
104 DCHECK(!sensor_paths_.empty());
105 }
106
107 SensorReader::~SensorReader() = default;
108
109 bool SensorReader::ReadSensorReading(SensorReading* reading) {
110 base::ThreadRestrictions::AssertIOAllowed();
111 SensorReading readings;
112 DCHECK_LE(sensor_paths_.size(), arraysize(readings.values));
113 int i = 0;
114 for (const auto& path : sensor_paths_) {
115 std::string new_read_value;
116 if (!base::ReadFileToString(path, &new_read_value))
117 return false;
118
119 double new_value = 0;
120 base::TrimWhitespaceASCII(new_read_value, base::TRIM_ALL, &new_read_value);
121 if (!base::StringToDouble(new_read_value, &new_value))
122 return false;
123 readings.values[i++] = new_value;
124 }
125 if (!apply_scaling_func_.is_null())
126 apply_scaling_func_.Run(scaling_value_, readings);
127 *reading = readings;
128 return true;
129 }
130
131 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698