blob: 4b346bdfbe5a918c2f2d0c5accb672ff3d5036af [file] [log] [blame]
// Copyright 2015 The Chromium OS 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 "thermald/cooling_device.h"
#include <string>
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/strings/stringprintf.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
using std::string;
namespace thermald {
CoolingDevice::CoolingDevice(int id)
: id_(id),
cur_state_attr_(
base::StringPrintf("/sys/class/thermal/cooling_device%d/cur_state",
id)) {
}
bool CoolingDevice::SetThrottleState(int state) {
string str_state(base::NumberToString(state));
if (WriteFile(cur_state_attr_, str_state.c_str(), str_state.length()) !=
static_cast<int>(str_state.length())) {
LOG(ERROR) << "[" << id_ << "] Failed to write '"
<< cur_state_attr_.value() << "'";
return false;
}
return true;
}
bool CoolingDevice::GetThrottleState(int *state) const {
string contents;
if (!base::ReadFileToString(cur_state_attr_, &contents)) {
LOG(ERROR) << "[" << id_ << "] Failed to read '"
<< cur_state_attr_.value() << "'";
return false;
}
base::TrimWhitespaceASCII(contents, base::TRIM_TRAILING, &contents);
return base::StringToInt(contents, state);
}
} // namespace thermald