blob: 144f2e5bebfb857b3c10bb76a05b961d5d3859ca [file] [log] [blame]
// Copyright 2019 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/iio_temperature_sensor.h"
#include <string>
#include "base/check.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
using std::stoi;
using std::string;
using std::to_string;
namespace thermald {
std::unique_ptr<IioTemperatureSensor>
IioTemperatureSensor::Get(const string &name, const string &iio_device_name,
const string &iio_sensor_name) {
base::FilePath iio_device_dir("/sys/bus/iio/devices/");
base::FileEnumerator iio_enum(iio_device_dir, false,
base::FileEnumerator::DIRECTORIES,
FILE_PATH_LITERAL("iio:device*"));
base::FilePath iio_device;
for (iio_device = iio_enum.Next(); !iio_device.empty();
iio_device = iio_enum.Next()) {
if (iio_device.BaseName().value() == iio_device_name) {
break;
}
}
if (iio_device.empty()) {
return NULL;
}
return std::make_unique<IioTemperatureSensor>(name, iio_device,
iio_sensor_name);
}
IioTemperatureSensor::IioTemperatureSensor(const string &name,
const base::FilePath &iio_dev_path,
const string &iio_sensor_name)
: TemperatureSensorCommon(name),
sysfs_attr_input_(iio_dev_path.value() + "/in_temp_" + iio_sensor_name) {
}
bool IioTemperatureSensor::ReadTemperature(int *value) {
DCHECK(value);
string contents;
if (!base::ReadFileToString(sysfs_attr_input_, &contents)) {
LOG(ERROR) << "[" << name() << "] Failed to read '"
<< sysfs_attr_input_.value() << "'";
return false;
}
*value = stoi(contents);
return true;
}
} // namespace thermald