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 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 full_path = check_path.Append(*(input_names + i)); | |
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 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 base::ThreadRestrictions::AssertIOAllowed(); | |
61 std::vector<base::FilePath> sensor_paths; | |
62 if (!GetSensorFilePaths(data, &sensor_paths)) { | |
63 return nullptr; | |
64 } | |
65 return base::WrapUnique(new SensorReader(std::move(sensor_paths))); | |
66 } | |
67 | |
68 SensorReader::SensorReader(std::vector<base::FilePath> sensor_paths) | |
69 : sensor_paths_(std::move(sensor_paths)) { | |
70 DCHECK(!sensor_paths_.empty()); | |
71 } | |
72 | |
73 SensorReader::~SensorReader() = default; | |
74 | |
75 bool SensorReader::ReadSensorReading(SensorReading* reading) { | |
shalamov
2016/10/19 16:37:11
why not to pass SensorReading& reading and then po
Mikhail
2016/10/19 17:25:40
so that the given argument is not modified on func
| |
76 base::ThreadRestrictions::AssertIOAllowed(); | |
77 SensorReading readings; | |
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 readings.values[i] = new_value; | |
Mikhail
2016/10/19 13:56:13
nit: readings.values[i++] = new_value;
maksims (do not use this acc)
2016/10/20 09:50:36
Done.
| |
89 ++i; | |
90 } | |
91 *reading = readings; | |
92 return true; | |
93 } | |
94 | |
95 } // namespace device | |
OLD | NEW |