| 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..e788d7de093209646572076128522b5ecc09b4fb
|
| --- /dev/null
|
| +++ b/chromeos/system/cpu_temp_reader.cc
|
| @@ -0,0 +1,92 @@
|
| +// 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 kDefaultHwmonDir[] = "/sys/class/hwmon/";
|
| +const char kDeviceDir[] = "device";
|
| +const char kHwmonDirectoryPattern[] = "hwmon*";
|
| +const char kCPUTempFilePattern[] = "temp*_input";
|
| +
|
| +bool ReadTemperatureFromPath(const base::FilePath& path, double* temp_celsius) {
|
| + std::string temperature_string;
|
| + uint32_t temperature = 0;
|
| + if (base::ReadFileToString(path, &temperature_string) &&
|
| + base::StringToUint(temperature_string, &temperature)) {
|
| + *temp_celsius = static_cast<double>(temperature) / 1000;
|
| + return true;
|
| + }
|
| + return false;
|
| +}
|
| +
|
| +std::string GetLabelFromPath(const base::FilePath& label_path) {
|
| + // 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);
|
| + return label;
|
| + }
|
| +
|
| + base::FilePath name_path = label_path.DirName().Append("name");
|
| + if (base::PathExists(base::FilePath(name_path))) {
|
| + base::ReadFileToString(name_path, &label);
|
| + }
|
| + return label;
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +CPUTemperatureReader::CPUTemperatureReader() : hwmon_dir_(kDefaultHwmonDir) {}
|
| +
|
| +std::vector<CPUTemperatureInfo> CPUTemperatureReader::GetCPUTemperatures() {
|
| + std::vector<CPUTemperatureInfo> result;
|
| +
|
| + // Get directories /sys/class/hwmon/hwmon*
|
| + base::FileEnumerator hwmon_enumerator(base::FilePath(hwmon_dir_), 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;
|
| + }
|
| + CPUTemperatureInfo info;
|
| + if (!ReadTemperatureFromPath(temperature_path, &info.temp_celsius)) {
|
| + LOG(WARNING) << "Unable to read CPU temperature from " << label_path;
|
| + continue;
|
| + }
|
| + base::ReplaceSubstringsAfterOffset(&label_path, 0, "input", "label");
|
| + info.label = GetLabelFromPath(base::FilePath(label_path));
|
| + result.push_back(info);
|
| + }
|
| + }
|
| + return result;
|
| +}
|
| +
|
| +} // namespace system
|
| +} // namespace chromeos
|
|
|