Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hwmon: refactor & support lookup of all indices #226

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 84 additions & 60 deletions src/hwmon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@
namespace thinkfan {



static int filter_hwmon_dirs(const struct dirent *entry)
{
return (entry->d_type == DT_DIR || entry->d_type == DT_LNK)
&& (!strncmp("hwmon", entry->d_name, 5) || !strcmp("device", entry->d_name));
&& (string(entry->d_name) == "hwmon" || string(entry->d_name) == "device");
}


Expand All @@ -48,29 +49,60 @@ static int filter_subdirs(const struct dirent *entry)
}


template<class HwmonT>
vector<string> HwmonInterface<HwmonT>::find_files(const string &path, const vector<unsigned int> &indices)
template<>
int HwmonInterface<SensorDriver>::filter_driver_file(const struct dirent *entry)
{
int idx;
return (entry->d_type == DT_REG || entry->d_type == DT_LNK)
&& ::sscanf(entry->d_name, "temp%d_input", &idx) == 1
;
}

template<>
int HwmonInterface<FanDriver>::filter_driver_file(const struct dirent *entry)
{
int idx;
return (entry->d_type == DT_REG || entry->d_type == DT_LNK)
&& ::sscanf(entry->d_name, "pwm%d", &idx) == 1
;
}


template<int (* filter_fn)(const struct dirent *)>
vector<string> dir_entries(const filesystem::path &dir)
{
struct dirent **entries;
int nentries = ::scandir(dir.c_str(), &entries, filter_fn, nullptr);
if (nentries == -1)
return {};

vector<string> rv;
for (unsigned int idx : indices) {
const string fpath(path + "/" + filename(idx));
std::ifstream f(fpath);
if (f.is_open() && f.good())
rv.push_back(fpath);
else
throw IOerror("Can't find hwmon file: " + fpath, errno);
for (int i = 0; i < nentries; ++i) {
rv.emplace_back(dir / entries[i]->d_name);
::free(entries[i]);
}
::free(entries);
return rv;
}

template<>
string HwmonInterface<SensorDriver>::filename(unsigned int index)
{ return "temp" + std::to_string(index) + "_input"; }

template<>
string HwmonInterface<FanDriver>::filename(unsigned int index)
string HwmonInterface<FanDriver>::filename_by_index(unsigned int index)
{ return "pwm" + std::to_string(index); }

template<>
string HwmonInterface<SensorDriver>::filename_by_index(unsigned int index)
{ return "temp" + std::to_string(index) + "_input"; }


template<class HwmonT>
vector<string> HwmonInterface<HwmonT>::filenames_by_indices(const vector<unsigned int> &indices)
{
vector<string> rv;
for (unsigned int index : indices)
rv.push_back(filename_by_index(index));
return rv;
}


template<class HwmonT>
Expand All @@ -85,16 +117,17 @@ HwmonInterface<HwmonT>::HwmonInterface(const string &base_path, opt<const string
, indices_(indices)
{}


template<class HwmonT>
vector<string> HwmonInterface<HwmonT>::find_hwmons_by_name(
const string &path,
const filesystem::path &path,
const string &name,
unsigned char depth
) {
const unsigned char max_depth = 5;
vector<string> result;

ifstream f(path + "/name");
ifstream f(path / "name");
if (f.is_open() && f.good()) {
string tmp;
if ((f >> tmp) && tmp == name) {
Expand All @@ -106,15 +139,7 @@ vector<string> HwmonInterface<HwmonT>::find_hwmons_by_name(
return result; // don't recurse to subdirs
}

struct dirent **entries;
int nentries = ::scandir(path.c_str(), &entries, filter_subdirs, nullptr);
if (nentries == -1) {
return result;
}
for (int i = 0; i < nentries; i++) {
auto subdir = path + "/" + entries[i]->d_name;
free(entries[i]);

for (const auto &subdir : dir_entries<filter_subdirs>(path)) {
struct stat statbuf;
int err = stat(path.c_str(), &statbuf);
if (err || (statbuf.st_mode & S_IFMT) != S_IFDIR)
Expand All @@ -123,21 +148,21 @@ vector<string> HwmonInterface<HwmonT>::find_hwmons_by_name(
auto found = find_hwmons_by_name(subdir, name, depth + 1);
result.insert(result.end(), found.begin(), found.end());
}
free(entries);

return result;
}


template<class HwmonT>
vector<string> HwmonInterface<HwmonT>::find_hwmons_by_model(
const string &path,
const filesystem::path &path,
const string &model,
unsigned char depth
) {
const unsigned char max_depth = 5;
vector<string> result;

ifstream f(path + "/model");
ifstream f(path / "model");
if (f.is_open() && f.good()) {
string tmp;
if (getline(f, tmp)) {
Expand All @@ -152,15 +177,7 @@ vector<string> HwmonInterface<HwmonT>::find_hwmons_by_model(
return result; // don't recurse to subdirs
}

struct dirent **entries;
int nentries = ::scandir(path.c_str(), &entries, filter_subdirs, nullptr);
if (nentries == -1) {
return result;
}
for (int i = 0; i < nentries; i++) {
auto subdir = path + "/" + entries[i]->d_name;
free(entries[i]);

for (const auto &subdir : dir_entries<filter_subdirs>(path)) {
struct stat statbuf;
int err = stat(path.c_str(), &statbuf);
if (err || (statbuf.st_mode & S_IFMT) != S_IFDIR)
Expand All @@ -169,52 +186,57 @@ vector<string> HwmonInterface<HwmonT>::find_hwmons_by_model(
auto found = find_hwmons_by_model(subdir, model, depth + 1);
result.insert(result.end(), found.begin(), found.end());
}
free(entries);

return result;
}


template<class HwmonT>
vector<string> HwmonInterface<HwmonT>::find_hwmons_by_indices(
const string &path,
const filesystem::path &path,
const vector<unsigned int> &indices,
unsigned char depth
) {
constexpr unsigned char max_depth = 3;

try {
return find_files(path, indices);
vector<string> rv;
vector<string> filenames = filenames_by_indices(indices);
for (const filesystem::path fname : filenames) {
const filesystem::path fpath(path / fname);
std::ifstream f(fpath);
if (f.is_open() && f.good())
rv.push_back(fpath);
else if (rv.size()) // Found one, but another is missing: Error
throw IOerror("Can't find hwmon file: " + string(fpath), errno);
}
catch (IOerror &) {

if (rv.empty()) { // No matching files in this directory
if (depth <= max_depth) {
struct dirent **entries;
int nentries = ::scandir(path.c_str(), &entries, filter_hwmon_dirs, alphasort);
if (nentries < 0)
throw IOerror("Error scanning " + path + ": ", errno);
vector<string> hwmon_dirs = dir_entries<filter_hwmon_dirs>(path);
if (hwmon_dirs.empty())
return {};

vector<string> rv;
for (int i = 0; i < nentries; i++) {
for (const auto &hwmon_dir : hwmon_dirs) {
rv = HwmonInterface<HwmonT>::find_hwmons_by_indices(
path + "/" + entries[i]->d_name,
hwmon_dir,
indices,
depth + 1
);
if (rv.size())
break;
return rv;
}
for (int i = 0; i < nentries; i++)
free(entries[i]);
free(entries);

return rv;
if (depth == 0) // Nothing found here or after recursion
throw DriverInitError(
"Could not find requested files " + vec_to_string(filenames)
+ " in " + string(path) + "."
);
}
else
throw DriverInitError("Could not find an `hwmon*' directory or `temp*_input' file in " + path + ".");
}
return rv;
}



template<class HwmonT>
string HwmonInterface<HwmonT>::lookup()
{
Expand All @@ -229,7 +251,7 @@ string HwmonInterface<HwmonT>::lookup()
if (paths.size() != 1) {
string msg(path + ": ");
if (paths.size() == 0) {
msg += "Could not find a hwmon with this name: " + name_.value();
msg += "Could not find an hwmon with this name: " + name_.value();
vmatare marked this conversation as resolved.
Show resolved Hide resolved
} else {
msg += MSG_MULTIPLE_HWMONS_FOUND;
for (string hwmon_path : paths)
Expand Down Expand Up @@ -259,8 +281,10 @@ string HwmonInterface<HwmonT>::lookup()
if (found_paths_.size() == 0)
throw DriverInitError(path + ": " + "Could not find any hwmons in " + path);
}
else
found_paths_.push_back(path);
else {
vector<string> paths = dir_entries<filter_driver_file>(path);
found_paths_.swap(paths);
}

paths_it_.emplace(found_paths_.begin());
}
Expand Down
15 changes: 10 additions & 5 deletions src/hwmon.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@
#include "thinkfan.h"

#include <dirent.h>
#include <filesystem>

namespace thinkfan {


class HwmonSensorDriver;
class HwmonFanDriver;

namespace filesystem = std::filesystem;


template<class HwmonT>
class HwmonInterface {
Expand All @@ -41,12 +44,14 @@ class HwmonInterface {
string lookup();

private:
static vector<string> find_files(const string &path, const vector<unsigned int> &indices);
static string filename(unsigned int index);
static int filter_driver_file(const struct dirent *entry);
static string filename_by_index(unsigned int index);
static vector<string> filenames_by_indices(const vector<unsigned int> &indices);

static vector<string> find_hwmons_by_model(const filesystem::path &path, const string &model, unsigned char depth);
static vector<string> find_hwmons_by_name(const filesystem::path &path, const string &name, unsigned char depth);
static vector<string> find_hwmons_by_indices(const filesystem::path &path, const vector<unsigned int> &indices, unsigned char depth);

static vector<string> find_hwmons_by_model(const string &path, const string &model, unsigned char depth);
static vector<string> find_hwmons_by_name(const string &path, const string &name, unsigned char depth);
static vector<string> find_hwmons_by_indices(const string &path, const vector<unsigned int> &indices, unsigned char depth);

protected:
opt<const string> base_path_;
Expand Down
26 changes: 26 additions & 0 deletions src/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,32 @@ template<class ErrT, class... ArgTs> void error(const ArgTs &... args) {
}


template<class T>
string to_string(const T &);


template<>
string to_string(const string &s)
{ return s; }

template<class T>
string to_string(const T &o)
{ return std::to_string(o); }


template<class T>
string vec_to_string(const vector<T> &v)
{
string rv = "[";
for (const T& item : v)
rv += to_string(item) + ", ";
if (rv.length() > 2)
rv = rv.substr(0, rv.length() - 2);
rv += "]";
return rv;
}


} // namespace thinkfan


Expand Down
Loading