Add mapping logic, refactor stuff
This commit is contained in:
@@ -9,11 +9,14 @@ find_package(Boost 1.79.0 COMPONENTS json)
|
|||||||
|
|
||||||
add_executable(app
|
add_executable(app
|
||||||
src/main.cxx
|
src/main.cxx
|
||||||
|
src/sensor/SensorsWrapper.cxx
|
||||||
|
src/sensor/Sensor.cxx
|
||||||
src/sensor/NvidiaSensor.cxx
|
src/sensor/NvidiaSensor.cxx
|
||||||
|
src/sensor/HwmonSensor.cxx
|
||||||
src/fan/Pwm.cxx
|
src/fan/Pwm.cxx
|
||||||
src/fan/PwmControl.cxx
|
src/fan/PwmControl.cxx
|
||||||
src/SensorsWrapper.cxx
|
src/fan/HwmonFan.cxx
|
||||||
src/sensor/HwmonSensor.cxx
|
src/Mapping.cxx
|
||||||
)
|
)
|
||||||
|
|
||||||
set_property(TARGET app PROPERTY CXX_STANDARD 17)
|
set_property(TARGET app PROPERTY CXX_STANDARD 17)
|
||||||
|
|||||||
23
app/include/Mapping.h
Normal file
23
app/include/Mapping.h
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
#ifndef MAPPING_H_
|
||||||
|
#define MAPPING_H_
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <fan/Fan.h>
|
||||||
|
#include <fan/PwmControl.h>
|
||||||
|
#include <sensor/Sensor.h>
|
||||||
|
|
||||||
|
class Mapping {
|
||||||
|
public:
|
||||||
|
std::vector<std::shared_ptr<Fan>>
|
||||||
|
createMapping(std::vector<std::shared_ptr<Sensor>> rpmSensors,
|
||||||
|
std::vector<std::shared_ptr<PwmControl>> pwmControls);
|
||||||
|
|
||||||
|
private:
|
||||||
|
template <class Printable>
|
||||||
|
void print(std::string listLabel,
|
||||||
|
std::vector<std::shared_ptr<Printable>> list);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // MAPPING_H_
|
||||||
11
app/include/Printable.h
Normal file
11
app/include/Printable.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#ifndef PRINTABLE_H_
|
||||||
|
#define PRINTABLE_H_
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class Printable {
|
||||||
|
public:
|
||||||
|
virtual const std::string toString() const = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // PRINTABLE_H_
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
#ifndef SENSORSWRAPPER_H_
|
|
||||||
#define SENSORSWRAPPER_H_
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include <sensors/sensors.h>
|
|
||||||
|
|
||||||
struct Sensor {
|
|
||||||
sensors_chip_name chipName;
|
|
||||||
sensors_feature feature;
|
|
||||||
sensors_subfeature subFeature;
|
|
||||||
};
|
|
||||||
|
|
||||||
class SensorsWrapper {
|
|
||||||
public:
|
|
||||||
SensorsWrapper();
|
|
||||||
~SensorsWrapper();
|
|
||||||
|
|
||||||
std::vector<Sensor> getTemperatureSensors();
|
|
||||||
int getValue(Sensor sensor);
|
|
||||||
std::string getLabel(Sensor sensor);
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::vector<Sensor> mTemperatureSensors;
|
|
||||||
std::vector<Sensor> mFanSensors;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // SENSORSWRAPPER_H_
|
|
||||||
@@ -9,15 +9,15 @@
|
|||||||
|
|
||||||
class HwmonFan : public Fan {
|
class HwmonFan : public Fan {
|
||||||
public:
|
public:
|
||||||
HwmonFan(std::unique_ptr<PwmControl> pwmControl,
|
HwmonFan(std::shared_ptr<PwmControl> pwmControl,
|
||||||
std::unique_ptr<Sensor> rpmSensor);
|
std::shared_ptr<Sensor> rpmSensor);
|
||||||
|
|
||||||
void pwm(int percent) override;
|
void pwm(int percent) override;
|
||||||
int rpm() override;
|
int rpm() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<PwmControl> mPwmControl;
|
std::shared_ptr<PwmControl> mPwmControl;
|
||||||
std::unique_ptr<Sensor> mRpmSensor;
|
std::shared_ptr<Sensor> mRpmSensor;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // HWMONFAN_H_
|
#endif // HWMONFAN_H_
|
||||||
|
|||||||
@@ -3,11 +3,13 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include <Printable.h>
|
||||||
|
|
||||||
enum class PWM_CONTROL_PROPERTY { CONTROL, ENABLE, MODE };
|
enum class PWM_CONTROL_PROPERTY { CONTROL, ENABLE, MODE };
|
||||||
enum class PWM_ENABLE { FULL_SPEED = 0, MANUAL_CONTROL };
|
enum class PWM_ENABLE { FULL_SPEED = 0, MANUAL_CONTROL };
|
||||||
enum class PWM_MODE { DC = 0, PWM };
|
enum class PWM_MODE { DC = 0, PWM };
|
||||||
|
|
||||||
class PwmControl {
|
class PwmControl : public Printable {
|
||||||
public:
|
public:
|
||||||
PwmControl(std::string controlPath);
|
PwmControl(std::string controlPath);
|
||||||
|
|
||||||
@@ -17,6 +19,8 @@ public:
|
|||||||
void enableManualControl();
|
void enableManualControl();
|
||||||
void reset();
|
void reset();
|
||||||
|
|
||||||
|
const std::string toString() const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int readValue(std::string path);
|
int readValue(std::string path);
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ public:
|
|||||||
const sensors_subfeature *subfeature);
|
const sensors_subfeature *subfeature);
|
||||||
|
|
||||||
int value() override;
|
int value() override;
|
||||||
std::string name() override;
|
const std::string toString() const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const sensors_chip_name *mChipName;
|
const sensors_chip_name *mChipName;
|
||||||
|
|||||||
@@ -9,7 +9,8 @@ public:
|
|||||||
~NvidiaSensor();
|
~NvidiaSensor();
|
||||||
|
|
||||||
int value() override;
|
int value() override;
|
||||||
std::string name() override;
|
|
||||||
|
const std::string toString() const override;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // NVIDIASENSOR_H_
|
#endif // NVIDIASENSOR_H_
|
||||||
|
|||||||
@@ -1,15 +1,18 @@
|
|||||||
#ifndef SENSOR_H_
|
#ifndef SENSOR_H_
|
||||||
#define SENSOR_H_
|
#define SENSOR_H_
|
||||||
|
|
||||||
#include <iostream>
|
#include <Printable.h>
|
||||||
|
|
||||||
class Sensor {
|
class Sensor : public Printable {
|
||||||
public:
|
public:
|
||||||
// Read the current value
|
// Read the current value
|
||||||
virtual int value() = 0;
|
virtual int value() = 0;
|
||||||
// Name for displaying. Should be descriptive, e.g. "GPU" or the label from
|
|
||||||
// libsensors.
|
virtual int max() const;
|
||||||
virtual std::string name() = 0;
|
virtual void max(int value);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
int mMax = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SENSOR_H_
|
#endif // SENSOR_H_
|
||||||
|
|||||||
26
app/include/sensor/SensorsWrapper.h
Normal file
26
app/include/sensor/SensorsWrapper.h
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
#ifndef SENSORSWRAPPER_H_
|
||||||
|
#define SENSORSWRAPPER_H_
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <sensors/sensors.h>
|
||||||
|
|
||||||
|
#include <fan/PwmControl.h>
|
||||||
|
#include <sensor/Sensor.h>
|
||||||
|
|
||||||
|
class SensorsWrapper {
|
||||||
|
public:
|
||||||
|
SensorsWrapper();
|
||||||
|
~SensorsWrapper();
|
||||||
|
|
||||||
|
std::vector<std::shared_ptr<Sensor>>
|
||||||
|
Sensors(sensors_subfeature_type sensorType);
|
||||||
|
|
||||||
|
std::vector<std::shared_ptr<PwmControl>> PwmControls();
|
||||||
|
|
||||||
|
private:
|
||||||
|
FILE *mConfigFile;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // SENSORSWRAPPER_H_
|
||||||
76
app/src/Mapping.cxx
Normal file
76
app/src/Mapping.cxx
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
#include <chrono>
|
||||||
|
#include <iostream>
|
||||||
|
#include <map>
|
||||||
|
#include <memory>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
#include <Mapping.h>
|
||||||
|
#include <fan/HwmonFan.h>
|
||||||
|
#include <fan/PwmControl.h>
|
||||||
|
|
||||||
|
#define SETTLE_TIMEOUT 5
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
vector<shared_ptr<Fan>>
|
||||||
|
Mapping::createMapping(vector<shared_ptr<Sensor>> rpmSensors,
|
||||||
|
vector<shared_ptr<PwmControl>> pwmControls) {
|
||||||
|
print("RPM Sensors", rpmSensors);
|
||||||
|
print("PWM controllers", pwmControls);
|
||||||
|
vector<shared_ptr<Fan>> mapping;
|
||||||
|
|
||||||
|
cout << "Settings all fans to maximum speed" << endl;
|
||||||
|
for (auto c : pwmControls) {
|
||||||
|
c->enableManualControl();
|
||||||
|
c->pwm(100);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait for fans to settle
|
||||||
|
cout << "Waiting for fans to settle" << endl;
|
||||||
|
this_thread::sleep_for(chrono::seconds(SETTLE_TIMEOUT));
|
||||||
|
|
||||||
|
// Record values of all RPM sensors
|
||||||
|
for (auto s : rpmSensors) {
|
||||||
|
int value = s->value();
|
||||||
|
s->max(value);
|
||||||
|
|
||||||
|
cout << s->toString() << " max value: " << s->max() << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set each fan to 50% and check if a sensor matches
|
||||||
|
for (auto c : pwmControls) {
|
||||||
|
cout << "Setting " << c->toString()
|
||||||
|
<< " to 50% and wait for it to settle..." << endl;
|
||||||
|
c->pwm(50);
|
||||||
|
|
||||||
|
this_thread::sleep_for(chrono::seconds(SETTLE_TIMEOUT));
|
||||||
|
|
||||||
|
for (auto s : rpmSensors) {
|
||||||
|
if (s->value() < s->max() * 0.7) {
|
||||||
|
cout << "Found matching sensor " << s->toString() << endl;
|
||||||
|
mapping.push_back(make_shared<HwmonFan>(c, s));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << "Setting fan back to 100% and wait for settling" << endl;
|
||||||
|
c->pwm(100);
|
||||||
|
this_thread::sleep_for(chrono::seconds(SETTLE_TIMEOUT));
|
||||||
|
}
|
||||||
|
|
||||||
|
cout << "Resetting all fans" << endl;
|
||||||
|
for (auto c : pwmControls) {
|
||||||
|
c->reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
return mapping;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class Printable>
|
||||||
|
void Mapping::print(string listLabel, vector<shared_ptr<Printable>> list) {
|
||||||
|
cout << listLabel << ": " << endl;
|
||||||
|
|
||||||
|
for (auto i : list) {
|
||||||
|
cout << i->toString() << endl;
|
||||||
|
}
|
||||||
|
cout << "\n";
|
||||||
|
}
|
||||||
@@ -1,58 +0,0 @@
|
|||||||
#include <exception>
|
|
||||||
#include <iostream>
|
|
||||||
#include <sensors/sensors.h>
|
|
||||||
#include <stdexcept>
|
|
||||||
|
|
||||||
#include <SensorsWrapper.h>
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
#define CONFIG_FILE "/etc/conf.d/sensors"
|
|
||||||
|
|
||||||
SensorsWrapper::SensorsWrapper() {
|
|
||||||
auto config = fopen(CONFIG_FILE, "r");
|
|
||||||
if (sensors_init(config) != 0) {
|
|
||||||
throw runtime_error("Config file doesn't exist");
|
|
||||||
}
|
|
||||||
|
|
||||||
int c = 0;
|
|
||||||
for (const sensors_chip_name *chipName;
|
|
||||||
(chipName = sensors_get_detected_chips(0, &c)) != NULL;) {
|
|
||||||
|
|
||||||
int d = 0;
|
|
||||||
for (const sensors_feature *feature;
|
|
||||||
(feature = sensors_get_features(chipName, &d)) != NULL;) {
|
|
||||||
auto tempFeature = sensors_get_subfeature(chipName, feature,
|
|
||||||
SENSORS_SUBFEATURE_TEMP_INPUT);
|
|
||||||
if (tempFeature)
|
|
||||||
mTemperatureSensors.push_back(
|
|
||||||
Sensor{*chipName, *feature, *tempFeature});
|
|
||||||
|
|
||||||
auto fanFeature = sensors_get_subfeature(chipName, feature,
|
|
||||||
SENSORS_SUBFEATURE_FAN_INPUT);
|
|
||||||
if (fanFeature)
|
|
||||||
mFanSensors.push_back(Sensor{*chipName, *feature, *fanFeature});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
SensorsWrapper::~SensorsWrapper() { sensors_cleanup(); }
|
|
||||||
|
|
||||||
std::vector<Sensor> SensorsWrapper::getTemperatureSensors() {
|
|
||||||
return mTemperatureSensors;
|
|
||||||
}
|
|
||||||
|
|
||||||
int SensorsWrapper::getValue(Sensor sensor) {
|
|
||||||
double value;
|
|
||||||
const sensors_chip_name *chipName = &sensor.chipName;
|
|
||||||
|
|
||||||
sensors_get_value(chipName, sensor.subFeature.number, &value);
|
|
||||||
|
|
||||||
return (int)value;
|
|
||||||
}
|
|
||||||
|
|
||||||
string SensorsWrapper::getLabel(Sensor sensor) {
|
|
||||||
const sensors_chip_name *chipName = &sensor.chipName;
|
|
||||||
const sensors_feature *feature = &sensor.feature;
|
|
||||||
return string(sensors_get_label(chipName, feature));
|
|
||||||
}
|
|
||||||
11
app/src/fan/HwmonFan.cxx
Normal file
11
app/src/fan/HwmonFan.cxx
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#include <fan/HwmonFan.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
HwmonFan::HwmonFan(shared_ptr<PwmControl> pwmControl,
|
||||||
|
shared_ptr<Sensor> rpmSensor)
|
||||||
|
: mPwmControl(pwmControl), mRpmSensor(rpmSensor) {}
|
||||||
|
|
||||||
|
void HwmonFan::pwm(int percent) { mPwmControl->pwm(percent); }
|
||||||
|
|
||||||
|
int HwmonFan::rpm() { return mRpmSensor->value(); }
|
||||||
@@ -64,3 +64,7 @@ void PwmControl::reset() {
|
|||||||
|
|
||||||
ostrm.close();
|
ostrm.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const string PwmControl::toString() const {
|
||||||
|
return fs::path(mControlPath).filename();
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,91 +1,20 @@
|
|||||||
#include <exception>
|
|
||||||
#include <filesystem>
|
|
||||||
#include <iostream>
|
|
||||||
#include <memory>
|
|
||||||
#include <regex>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include <sensors/sensors.h>
|
#include <Mapping.h>
|
||||||
|
|
||||||
#include <fan/PwmControl.h>
|
|
||||||
#include <sensor/HwmonSensor.h>
|
|
||||||
#include <sensor/NvidiaSensor.h>
|
#include <sensor/NvidiaSensor.h>
|
||||||
|
#include <sensor/SensorsWrapper.h>
|
||||||
#define CONFIG_FILE "/etc/conf.d/sensors"
|
|
||||||
#define HWMON_BASE_PATH "/sys/class/hwmon"
|
|
||||||
|
|
||||||
namespace fs = std::filesystem;
|
|
||||||
|
|
||||||
template <sensors_subfeature_type T>
|
|
||||||
std::vector<std::shared_ptr<Sensor>> sensors() {
|
|
||||||
std::vector<std::shared_ptr<Sensor>> sensors;
|
|
||||||
|
|
||||||
int c = 0;
|
|
||||||
for (const sensors_chip_name *chipName;
|
|
||||||
(chipName = sensors_get_detected_chips(0, &c)) != NULL;) {
|
|
||||||
|
|
||||||
int d = 0;
|
|
||||||
for (const sensors_feature *feature;
|
|
||||||
(feature = sensors_get_features(chipName, &d)) != NULL;) {
|
|
||||||
auto subFeature = sensors_get_subfeature(chipName, feature, T);
|
|
||||||
if (subFeature) {
|
|
||||||
sensors.push_back(
|
|
||||||
std::make_shared<HwmonSensor>(chipName, feature, subFeature));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return sensors;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<std::shared_ptr<PwmControl>> pwmControls() {
|
|
||||||
std::vector<std::shared_ptr<PwmControl>> controls;
|
|
||||||
|
|
||||||
const std::regex re_ctrl_enable("pwm[0-9]_enable");
|
|
||||||
const std::regex re_ctrl_mode("pwm[0-9]_mode");
|
|
||||||
const std::regex re_ctrl("pwm[0-9]");
|
|
||||||
|
|
||||||
if (!fs::exists(HWMON_BASE_PATH)) {
|
|
||||||
std::cout << HWMON_BASE_PATH << " doesn't exist" << std::endl;
|
|
||||||
} else {
|
|
||||||
for (const fs::directory_entry &hwmon_device :
|
|
||||||
fs::directory_iterator{HWMON_BASE_PATH}) {
|
|
||||||
|
|
||||||
for (const fs::directory_entry &control :
|
|
||||||
fs::directory_iterator{hwmon_device}) {
|
|
||||||
auto filename = control.path().filename().string();
|
|
||||||
|
|
||||||
if (regex_match(filename, re_ctrl)) {
|
|
||||||
auto controlPath = control.path().string();
|
|
||||||
|
|
||||||
controls.push_back(std::make_shared<PwmControl>(controlPath));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return controls;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
auto config = fopen(CONFIG_FILE, "r");
|
SensorsWrapper sensorsWrapper;
|
||||||
if (sensors_init(config) != 0) {
|
|
||||||
throw std::runtime_error("Config file doesn't exist");
|
|
||||||
}
|
|
||||||
|
|
||||||
auto tempSensors = sensors<SENSORS_SUBFEATURE_TEMP_INPUT>();
|
auto tempSensors = sensorsWrapper.Sensors(SENSORS_SUBFEATURE_TEMP_INPUT);
|
||||||
tempSensors.push_back(std::make_shared<NvidiaSensor>());
|
tempSensors.push_back(std::make_shared<NvidiaSensor>());
|
||||||
|
|
||||||
for (auto s : tempSensors) {
|
auto pwmSensors = sensorsWrapper.Sensors(SENSORS_SUBFEATURE_FAN_INPUT);
|
||||||
std::cout << s->name() << ": " << s->value() << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::cout << "pwm" << std::endl;
|
auto controls = sensorsWrapper.PwmControls();
|
||||||
|
|
||||||
auto pwmSensors = sensors<SENSORS_SUBFEATURE_FAN_INPUT>();
|
Mapping m;
|
||||||
for (auto s : pwmSensors) {
|
m.createMapping(pwmSensors, controls);
|
||||||
std::cout << s->name() << ": " << s->value() << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
#include <sensors/sensors.h>
|
|
||||||
|
|
||||||
#include <sensor/HwmonSensor.h>
|
#include <sensor/HwmonSensor.h>
|
||||||
|
#include <sensors/sensors.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@@ -10,10 +9,12 @@ HwmonSensor::HwmonSensor(const sensors_chip_name *chipName,
|
|||||||
: mChipName(chipName), mFeature(feature), mSubFeature(subfeature) {}
|
: mChipName(chipName), mFeature(feature), mSubFeature(subfeature) {}
|
||||||
|
|
||||||
int HwmonSensor::value() {
|
int HwmonSensor::value() {
|
||||||
double *value;
|
double value;
|
||||||
sensors_get_value(mChipName, mSubFeature->number, value);
|
sensors_get_value(mChipName, mSubFeature->number, &value);
|
||||||
|
|
||||||
return static_cast<int>(*value);
|
return static_cast<int>(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
string HwmonSensor::name() { return sensors_get_label(mChipName, mFeature); }
|
const string HwmonSensor::toString() const {
|
||||||
|
return sensors_get_label(mChipName, mFeature);
|
||||||
|
}
|
||||||
|
|||||||
@@ -18,4 +18,4 @@ int NvidiaSensor::value() {
|
|||||||
return static_cast<int>(temp);
|
return static_cast<int>(temp);
|
||||||
}
|
}
|
||||||
|
|
||||||
string NvidiaSensor::name() { return "GPU"; }
|
const std::string NvidiaSensor::toString() const { return "Nvidia GPU"; }
|
||||||
|
|||||||
5
app/src/sensor/Sensor.cxx
Normal file
5
app/src/sensor/Sensor.cxx
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
#include <sensor/Sensor.h>
|
||||||
|
|
||||||
|
int Sensor::max() const { return mMax; }
|
||||||
|
|
||||||
|
void Sensor::max(int value) { mMax = value; }
|
||||||
78
app/src/sensor/SensorsWrapper.cxx
Normal file
78
app/src/sensor/SensorsWrapper.cxx
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
#include <cstdio>
|
||||||
|
#include <filesystem>
|
||||||
|
#include <iostream>
|
||||||
|
#include <regex>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
|
#include <sensors/sensors.h>
|
||||||
|
|
||||||
|
#include <sensor/SensorsWrapper.h>
|
||||||
|
|
||||||
|
#include <sensor/HwmonSensor.h>
|
||||||
|
#include <sensor/NvidiaSensor.h>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
namespace fs = std::filesystem;
|
||||||
|
|
||||||
|
#define CONFIG_FILE "/etc/conf.d/sensors"
|
||||||
|
#define HWMON_BASE_PATH "/sys/class/hwmon"
|
||||||
|
|
||||||
|
SensorsWrapper::SensorsWrapper() : mConfigFile(fopen(CONFIG_FILE, "r")) {
|
||||||
|
if (sensors_init(mConfigFile) != 0) {
|
||||||
|
throw runtime_error("Config file doesn't exist");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SensorsWrapper::~SensorsWrapper() { sensors_cleanup(); }
|
||||||
|
|
||||||
|
std::vector<std::shared_ptr<Sensor>>
|
||||||
|
SensorsWrapper::Sensors(sensors_subfeature_type sensorType) {
|
||||||
|
std::vector<std::shared_ptr<Sensor>> sensors;
|
||||||
|
|
||||||
|
int c = 0;
|
||||||
|
for (const sensors_chip_name *chipName;
|
||||||
|
(chipName = sensors_get_detected_chips(0, &c)) != NULL;) {
|
||||||
|
|
||||||
|
int d = 0;
|
||||||
|
for (const sensors_feature *feature;
|
||||||
|
(feature = sensors_get_features(chipName, &d)) != NULL;) {
|
||||||
|
auto subFeature = sensors_get_subfeature(chipName, feature, sensorType);
|
||||||
|
if (subFeature) {
|
||||||
|
sensors.push_back(
|
||||||
|
std::make_shared<HwmonSensor>(chipName, feature, subFeature));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return sensors;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<std::shared_ptr<PwmControl>> SensorsWrapper::PwmControls() {
|
||||||
|
std::vector<std::shared_ptr<PwmControl>> controls;
|
||||||
|
|
||||||
|
const std::regex re_ctrl_enable("pwm[0-9]_enable");
|
||||||
|
const std::regex re_ctrl_mode("pwm[0-9]_mode");
|
||||||
|
const std::regex re_ctrl("pwm[0-9]");
|
||||||
|
|
||||||
|
if (!fs::exists(HWMON_BASE_PATH)) {
|
||||||
|
std::cout << HWMON_BASE_PATH << " doesn't exist" << std::endl;
|
||||||
|
} else {
|
||||||
|
for (const fs::directory_entry &hwmon_device :
|
||||||
|
fs::directory_iterator{HWMON_BASE_PATH}) {
|
||||||
|
|
||||||
|
for (const fs::directory_entry &control :
|
||||||
|
fs::directory_iterator{hwmon_device}) {
|
||||||
|
auto filename = control.path().filename().string();
|
||||||
|
|
||||||
|
if (regex_match(filename, re_ctrl)) {
|
||||||
|
auto controlPath = control.path().string();
|
||||||
|
|
||||||
|
controls.push_back(std::make_shared<PwmControl>(controlPath));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return controls;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user