OLD | NEW |
---|---|
(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 #include "base/threading/thread_restrictions.h" | |
12 | |
13 namespace device { | |
14 | |
15 namespace { | |
16 | |
17 bool InitSensorPaths(const char** input_names, | |
18 const char* base_path, | |
19 size_t size, | |
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 | |
27 for (base::FilePath check_path = dir_enumerator.Next(); !check_path.empty(); | |
28 check_path = dir_enumerator.Next()) { | |
29 for (size_t i = 0; i < size; i++) { | |
Mikhail
2016/10/17 10:34:11
nit: ++i
maksims (do not use this acc)
2016/10/18 06:50:41
Done.
| |
30 base::FilePath als_path = check_path.Append(*(input_names + i)); | |
Mikhail
2016/10/17 10:34:11
why als_ ?
maksims (do not use this acc)
2016/10/18 06:50:41
left from prev versions.
| |
31 if (base::PathExists(als_path)) { | |
32 sensor_paths->push_back(als_path); | |
33 return true; | |
34 } | |
35 } | |
36 } | |
37 return false; | |
38 } | |
39 | |
40 bool GetSensorFilePaths(const SensorDataIio& data, | |
41 std::vector<base::FilePath>* sensor_paths) { | |
42 DCHECK(sensor_paths->empty()); | |
43 // Depending on a sensor, there can be up to three sets of files that need | |
44 // to be checked. If one of three files is not found, a sensor is | |
45 // treated as a non-existing one. | |
46 for (size_t i = 0; i < data.sensor_file_names_rows; i++) { | |
47 // Supply InitSensorPaths() with a set of files. | |
48 // Only one file from each set should be found. | |
49 if (!InitSensorPaths(data.sensor_file_names[i], data.base_path_sensor_iio, | |
50 data.sensor_file_names_cols, sensor_paths)) { | |
51 return false; | |
52 } | |
53 } | |
54 return true; | |
55 } | |
56 | |
57 } // namespace | |
58 | |
59 // static | |
60 std::unique_ptr<SensorReader> SensorReader::Create(const SensorDataIio& data) { | |
61 base::ThreadRestrictions::AssertIOAllowed(); | |
62 std::vector<base::FilePath> sensor_paths; | |
63 if (!GetSensorFilePaths(data, &sensor_paths)) { | |
64 return nullptr; | |
65 } | |
66 return base::WrapUnique(new SensorReader(std::move(sensor_paths))); | |
67 } | |
68 | |
69 SensorReader::SensorReader(std::vector<base::FilePath> sensor_paths) | |
70 : sensor_paths_(std::move(sensor_paths)) { | |
71 DCHECK(!sensor_paths_.empty()); | |
72 } | |
73 | |
74 SensorReader::~SensorReader() = default; | |
75 | |
76 bool SensorReader::ReadSensorReading(SensorReading* reading) { | |
77 base::ThreadRestrictions::AssertIOAllowed(); | |
78 int i = 0; | |
79 for (const auto& path : sensor_paths_) { | |
80 std::string new_read_value; | |
81 if (!base::ReadFileToString(path, &new_read_value)) | |
82 return false; | |
83 | |
84 double new_value = 0; | |
85 base::TrimWhitespaceASCII(new_read_value, base::TRIM_ALL, &new_read_value); | |
86 if (!base::StringToDouble(new_read_value, &new_value)) | |
87 return false; | |
88 reading->values[i++] = new_value; | |
Mikhail
2016/10/17 10:34:11
given parameter should not be modified unless the
maksims (do not use this acc)
2016/10/18 06:50:41
Done.
| |
89 } | |
90 return true; | |
91 } | |
92 | |
93 } // namespace device | |
OLD | NEW |