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 5 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
88 changes: 55 additions & 33 deletions src/hwmon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@
#include <cstring>
#include <sys/types.h>
#include <sys/stat.h>
#include <filesystem>

namespace thinkfan {

namespace filesystem = std::filesystem;


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,6 +51,43 @@ static int filter_subdirs(const struct dirent *entry)
}


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 (int i = 0; i < nentries; ++i) {
rv.emplace_back(dir / entries[i]->d_name);
::free(entries[i]);
}
::free(entries);
return rv;
}


template<class HwmonT>
vector<string> HwmonInterface<HwmonT>::find_files(const string &path, const vector<unsigned int> &indices)
{
Expand Down Expand Up @@ -85,6 +125,7 @@ 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,
Expand All @@ -106,15 +147,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 filesystem::path 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,11 +156,11 @@ 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,
Expand All @@ -152,15 +185,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 filesystem::path 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,11 +194,11 @@ 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,
Expand All @@ -187,24 +212,20 @@ vector<string> HwmonInterface<HwmonT>::find_hwmons_by_indices(
}
catch (IOerror &) {
if (depth <= max_depth) {
struct dirent **entries;
int nentries = ::scandir(path.c_str(), &entries, filter_hwmon_dirs, alphasort);
if (nentries < 0)
vector<string> hwmon_dirs = dir_entries<filter_hwmon_dirs>(path);
if (hwmon_dirs.empty())
throw IOerror("Error scanning " + path + ": ", errno);

vector<string> rv;
for (int i = 0; i < nentries; i++) {
for (const filesystem::path 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;
}
for (int i = 0; i < nentries; i++)
free(entries[i]);
free(entries);

return rv;
}
Expand All @@ -214,7 +235,6 @@ vector<string> HwmonInterface<HwmonT>::find_hwmons_by_indices(
}



template<class HwmonT>
string HwmonInterface<HwmonT>::lookup()
{
Expand All @@ -229,7 +249,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 +279,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
2 changes: 2 additions & 0 deletions src/hwmon.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,15 @@ class HwmonInterface {
string lookup();

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

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_;
opt<const string> name_;
Expand Down
Loading