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 char** input_names, | |
20 const char* base_path, | |
21 size_t size, | |
22 std::vector<base::FilePath>* sensor_paths) { | |
23 // Search the iio/devices directory for a subdirectory (eg "device0" or | |
24 // "iio:device0") that contains the specified input_name file (eg | |
25 // "in_illuminance_input" or "in_illuminance0_input"). | |
26 base::FileEnumerator dir_enumerator(base::FilePath(base_path), false, | |
27 base::FileEnumerator::DIRECTORIES); | |
28 for (base::FilePath check_path = dir_enumerator.Next(); !check_path.empty(); | |
29 check_path = dir_enumerator.Next()) { | |
30 for (size_t i = 0; i < size; ++i) { | |
31 base::FilePath full_path = check_path.Append(*(input_names + i)); | |
32 if (base::PathExists(full_path)) { | |
33 sensor_paths->push_back(full_path); | |
34 return true; | |
35 } | |
36 } | |
37 } | |
38 return false; | |
39 } | |
40 | |
41 bool GetSensorFilePaths(const SensorDataIio& data, | |
42 std::vector<base::FilePath>* sensor_paths) { | |
43 DCHECK(sensor_paths->empty()); | |
44 // Depending on a sensor, there can be up to three sets of files that need | |
45 // to be checked. If one of three files is not found, a sensor is | |
46 // treated as a non-existing one. | |
47 for (size_t i = 0; i < data.sensor_file_names_rows; ++i) { | |
48 // Supply InitSensorPaths() with a set of files. | |
49 // Only one file from each set should be found. | |
50 if (!InitSensorPaths(data.sensor_file_names[i], data.base_path_sensor_iio, | |
51 data.sensor_file_names_cols, sensor_paths)) { | |
52 return false; | |
53 } | |
54 } | |
55 return true; | |
56 } | |
57 | |
58 } // namespace | |
59 | |
60 // static | |
61 std::unique_ptr<SensorReader> SensorReader::Create(const SensorDataIio& data) { | |
62 base::ThreadRestrictions::AssertIOAllowed(); | |
63 std::vector<base::FilePath> sensor_paths; | |
64 if (!GetSensorFilePaths(data, &sensor_paths)) { | |
65 return nullptr; | |
66 } | |
Reilly Grant (use Gerrit)
2016/10/24 21:49:27
nit: No braces around a single-line if.
maksims (do not use this acc)
2016/10/25 06:23:53
Done.
| |
67 return base::WrapUnique(new SensorReader(std::move(sensor_paths))); | |
Reilly Grant (use Gerrit)
2016/10/24 21:49:26
base::MakeUnique<SensorReader>(std::move(sensor_pa
maksims (do not use this acc)
2016/10/25 06:23:54
Done.
maksims (do not use this acc)
2016/10/25 06:50:14
The constructor is private.
| |
68 } | |
69 | |
70 SensorReader::SensorReader(std::vector<base::FilePath> sensor_paths) | |
71 : sensor_paths_(std::move(sensor_paths)) { | |
72 DCHECK(!sensor_paths_.empty()); | |
73 } | |
74 | |
75 SensorReader::~SensorReader() = default; | |
76 | |
77 bool SensorReader::ReadSensorReading(SensorReading* reading) { | |
78 base::ThreadRestrictions::AssertIOAllowed(); | |
79 SensorReading readings; | |
80 int i = 0; | |
Reilly Grant (use Gerrit)
2016/10/24 21:49:26
DCHECK_LE(sensor_paths_.size(), arraysize(readings
maksims (do not use this acc)
2016/10/25 06:23:54
Done.
| |
81 for (const auto& path : sensor_paths_) { | |
82 std::string new_read_value; | |
83 if (!base::ReadFileToString(path, &new_read_value)) | |
84 return false; | |
85 | |
86 double new_value = 0; | |
87 base::TrimWhitespaceASCII(new_read_value, base::TRIM_ALL, &new_read_value); | |
88 if (!base::StringToDouble(new_read_value, &new_value)) | |
89 return false; | |
90 readings.values[i++] = new_value; | |
91 } | |
92 *reading = readings; | |
93 return true; | |
94 } | |
95 | |
96 } // namespace device | |
OLD | NEW |