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 #include "device/generic_sensor/iio/sensor_data_iio.h" | |
13 #include "device/generic_sensor/public/cpp/sensor_reading.h" | |
14 | |
15 namespace device { | |
16 | |
17 namespace { | |
18 | |
19 bool InitSensorPaths(const std::vector<std::string>& input_names, | |
20 const char* base_path, | |
21 std::vector<base::FilePath>* sensor_paths) { | |
22 // Search the iio/devices directory for a subdirectory (eg "device0" or | |
23 // "iio:device0") that contains the specified input_name file (eg | |
24 // "in_illuminance_input" or "in_illuminance0_input"). | |
25 base::FileEnumerator dir_enumerator(base::FilePath(base_path), false, | |
26 base::FileEnumerator::DIRECTORIES); | |
27 for (base::FilePath check_path = dir_enumerator.Next(); !check_path.empty(); | |
28 check_path = dir_enumerator.Next()) { | |
29 for (auto const& file_name : input_names) { | |
30 base::FilePath full_path = check_path.Append(file_name); | |
31 if (base::PathExists(full_path)) { | |
32 sensor_paths->push_back(full_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 (auto const& file_names : data.sensor_file_names) { | |
47 // Supply InitSensorPaths() with a set of files. | |
48 // Only one file from each set should be found. | |
49 if (!InitSensorPaths(file_names, data.base_path_sensor_iio, sensor_paths)) | |
50 return false; | |
51 } | |
52 return true; | |
53 } | |
54 | |
55 } // namespace | |
56 | |
57 // static | |
58 std::unique_ptr<SensorReader> SensorReader::Create(const SensorDataIio& data) { | |
59 base::ThreadRestrictions::AssertIOAllowed(); | |
60 std::vector<base::FilePath> sensor_paths; | |
61 if (!GetSensorFilePaths(data, &sensor_paths)) | |
62 return nullptr; | |
63 return base::WrapUnique(new SensorReader(std::move(sensor_paths))); | |
Reilly Grant (use Gerrit)
2016/10/31 21:18:09
Since this is a static function on SensorReader it
maksims (do not use this acc)
2016/11/01 08:10:31
Nope, it will not event compile! Otherwise I have
Reilly Grant (use Gerrit)
2016/11/01 18:58:16
Right, because the ability to access the construct
| |
64 } | |
65 | |
66 SensorReader::SensorReader(std::vector<base::FilePath> sensor_paths) | |
67 : sensor_paths_(std::move(sensor_paths)) { | |
68 DCHECK(!sensor_paths_.empty()); | |
69 } | |
70 | |
71 SensorReader::~SensorReader() = default; | |
72 | |
73 bool SensorReader::ReadSensorReading(SensorReading* reading) { | |
74 base::ThreadRestrictions::AssertIOAllowed(); | |
75 SensorReading readings; | |
76 DCHECK_LE(sensor_paths_.size(), arraysize(readings.values)); | |
77 int i = 0; | |
78 for (const auto& path : sensor_paths_) { | |
79 std::string new_read_value; | |
80 if (!base::ReadFileToString(path, &new_read_value)) | |
81 return false; | |
82 | |
83 double new_value = 0; | |
84 base::TrimWhitespaceASCII(new_read_value, base::TRIM_ALL, &new_read_value); | |
85 if (!base::StringToDouble(new_read_value, &new_value)) | |
86 return false; | |
87 readings.values[i++] = new_value; | |
88 } | |
89 *reading = readings; | |
90 return true; | |
91 } | |
92 | |
93 } // namespace device | |
OLD | NEW |