Index: chromeos/system/cpu_temp_reader.cc |
diff --git a/chromeos/system/cpu_temp_reader.cc b/chromeos/system/cpu_temp_reader.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..776251190518266fe9af9278e6a9f07f1b7e0e22 |
--- /dev/null |
+++ b/chromeos/system/cpu_temp_reader.cc |
@@ -0,0 +1,82 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chromeos/system/cpu_temp_reader.h" |
+ |
+#include "base/files/file_enumerator.h" |
+#include "base/files/file_path.h" |
+#include "base/files/file_util.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "base/strings/string_util.h" |
+ |
+namespace chromeos { |
+namespace system { |
+ |
+namespace { |
+ |
+// The location we read our CPU temperature and channel label from. |
+const char kHwmonDir[] = "/sys/class/hwmon/"; |
+const char kDeviceDir[] = "device"; |
+const char kHwmonDirectoryPattern[] = "hwmon*"; |
+const char kCPUTempFilePattern[] = "temp*_input"; |
+ |
+} // namespace |
+ |
+std::vector<CPUTemperatureInfo> GetCPUTemperatures() { |
+ std::vector<CPUTemperatureInfo> result; |
+ |
+ // Get directories /sys/class/hwmon/hwmon* |
+ base::FileEnumerator hwmon_enumerator(base::FilePath(kHwmonDir), false, |
+ base::FileEnumerator::DIRECTORIES, |
+ kHwmonDirectoryPattern); |
+ for (base::FilePath hwmon_path = hwmon_enumerator.Next(); !hwmon_path.empty(); |
+ hwmon_path = hwmon_enumerator.Next()) { |
+ // Get temp*_input files in hwmon*/ and hwmon*/device/ |
+ if (base::PathExists(hwmon_path.Append(kDeviceDir))) { |
+ hwmon_path = hwmon_path.Append(kDeviceDir); |
+ } |
+ base::FileEnumerator enumerator( |
+ hwmon_path, false, base::FileEnumerator::FILES, kCPUTempFilePattern); |
+ for (base::FilePath temperature_path = enumerator.Next(); |
+ !temperature_path.empty(); temperature_path = enumerator.Next()) { |
+ // Get appropriate temp*_label file. |
+ std::string label_path = temperature_path.MaybeAsASCII(); |
+ if (label_path.empty()) { |
+ LOG(WARNING) << "Unable to parse a path to temp*_input file as ASCII"; |
+ continue; |
+ } |
stevenjb
2017/04/14 15:44:04
This would be a bit more readable if some or all o
Simon Que
2017/04/17 16:41:07
Done.
|
+ base::ReplaceSubstringsAfterOffset(&label_path, 0, "input", "label"); |
+ base::FilePath name_path = hwmon_path.Append("name"); |
+ |
+ // Get the label describing this temperature. Use temp*_label |
+ // if present, fall back on name file or blank. |
+ std::string label; |
+ if (base::PathExists(base::FilePath(label_path))) { |
+ base::ReadFileToString(base::FilePath(label_path), &label); |
+ } else if (base::PathExists(base::FilePath(name_path))) { |
+ base::ReadFileToString(name_path, &label); |
+ } else { |
+ label = std::string(); |
+ } |
+ |
+ // Read temperature in millidegree Celsius. |
+ std::string temperature_string; |
+ uint32_t temperature = 0; |
+ if (base::ReadFileToString(temperature_path, &temperature_string) && |
+ base::StringToUint(temperature_string, &temperature)) { |
+ CPUTemperatureInfo info; |
+ info.temp_celsius = static_cast<double>(temperature) / 1000; |
+ info.label = label; |
+ result.push_back(info); |
Daniel Erat
2017/04/14 18:53:32
i think, but am not certain, that you might just b
Simon Que
2017/04/17 16:41:07
No longer applicable due to refactoring.
|
+ } else { |
+ LOG(WARNING) << "Unable to read CPU temp from " |
+ << temperature_path.MaybeAsASCII(); |
+ } |
+ } |
+ } |
+ return result; |
+} |
+ |
+} // namespace system |
+} // namespace chromeos |