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

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

Issue 2506973003: [sensors](CrOS/Linux) Fix: create sensors when no scaling file is specified (Closed)
Patch Set: use the first version Created 4 years, 1 month 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "device/generic_sensor/linux/platform_sensor_utils_linux.h" 5 #include "device/generic_sensor/linux/platform_sensor_utils_linux.h"
6 6
7 #include "base/files/file_enumerator.h" 7 #include "base/files/file_enumerator.h"
8 #include "base/files/file_util.h" 8 #include "base/files/file_util.h"
9 #include "base/strings/string_number_conversions.h" 9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_util.h" 10 #include "base/strings/string_util.h"
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 // treated as a non-existing one. 44 // treated as a non-existing one.
45 for (auto const& file_names : data.sensor_file_names) { 45 for (auto const& file_names : data.sensor_file_names) {
46 // Supply InitSensorPaths() with a set of files. 46 // Supply InitSensorPaths() with a set of files.
47 // Only one file from each set should be found. 47 // Only one file from each set should be found.
48 if (!InitSensorPaths(file_names, data.base_path_sensor_linux, sensor_paths)) 48 if (!InitSensorPaths(file_names, data.base_path_sensor_linux, sensor_paths))
49 return false; 49 return false;
50 } 50 }
51 return true; 51 return true;
52 } 52 }
53 53
54 // Returns -1 if unable to read a scaling value, 1 if scaling value is not 54 // Returns -1 if unable to read a scaling value.
55 // found. Otherwise, returns a scaling value read from a file. 55 // Otherwise, returns a scaling value read from a file.
56 double GetSensorScalingValue(const base::FilePath& scale_file_path) { 56 double ReadSensorScalingValue(const base::FilePath& scale_file_path) {
57 if (!base::PathExists(scale_file_path))
58 return 1;
59
60 std::string value; 57 std::string value;
61 if (!base::ReadFileToString(scale_file_path, &value)) 58 if (!base::ReadFileToString(scale_file_path, &value))
62 return -1; 59 return -1;
63 60
64 double scaling_value; 61 double scaling_value;
65 base::TrimWhitespaceASCII(value, base::TRIM_ALL, &value); 62 base::TrimWhitespaceASCII(value, base::TRIM_ALL, &value);
66 if (!base::StringToDouble(value, &scaling_value)) 63 if (!base::StringToDouble(value, &scaling_value))
67 return -1; 64 return -1;
68 return scaling_value; 65 return scaling_value;
69 } 66 }
70 67
71 } // namespace 68 } // namespace
72 69
73 // static 70 // static
74 std::unique_ptr<SensorReader> SensorReader::Create( 71 std::unique_ptr<SensorReader> SensorReader::Create(
75 const SensorDataLinux& data) { 72 const SensorDataLinux& data) {
76 base::ThreadRestrictions::AssertIOAllowed(); 73 base::ThreadRestrictions::AssertIOAllowed();
77 std::vector<base::FilePath> sensor_paths; 74 std::vector<base::FilePath> sensor_paths;
78 if (!GetSensorFilePaths(data, &sensor_paths)) 75 if (!GetSensorFilePaths(data, &sensor_paths))
79 return nullptr; 76 return nullptr;
80 77
81 DCHECK(!sensor_paths.empty()); 78 DCHECK(!sensor_paths.empty());
82 const base::FilePath scale_file_path = 79 // Scaling value is 1 if scaling file is not specified is |data| or scaling
83 sensor_paths.back().DirName().Append(data.sensor_scale_name); 80 // file is not found.
84 double scaling_value = GetSensorScalingValue(scale_file_path); 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 }
85 88
86 // A file with a scaling value is found, but couldn't be read. 89 // A file with a scaling value is found, but couldn't be read.
87 if (scaling_value == -1) 90 if (scaling_value == -1)
88 return nullptr; 91 return nullptr;
89 92
90 return base::WrapUnique(new SensorReader( 93 return base::WrapUnique(new SensorReader(
91 std::move(sensor_paths), scaling_value, data.apply_scaling_func)); 94 std::move(sensor_paths), scaling_value, data.apply_scaling_func));
92 } 95 }
93 96
94 SensorReader::SensorReader( 97 SensorReader::SensorReader(
(...skipping 24 matching lines...) Expand all
119 return false; 122 return false;
120 readings.values[i++] = new_value; 123 readings.values[i++] = new_value;
121 } 124 }
122 if (!apply_scaling_func_.is_null()) 125 if (!apply_scaling_func_.is_null())
123 apply_scaling_func_.Run(scaling_value_, readings); 126 apply_scaling_func_.Run(scaling_value_, readings);
124 *reading = readings; 127 *reading = readings;
125 return true; 128 return true;
126 } 129 }
127 130
128 } // namespace device 131 } // namespace device
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698