Skip to content

Commit

Permalink
Add config settings for optional sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
Victor Mataré authored and vmatare committed Apr 8, 2020
1 parent 9a0abd6 commit 8a7d248
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 5 deletions.
12 changes: 11 additions & 1 deletion src/drivers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,8 @@ void HwmonFanDriver::set_speed(const Level *level)
SensorDriver::SensorDriver(std::string path, std::vector<int> correction)
: path_(path),
correction_(correction),
num_temps_(0)
num_temps_(0),
optional_(false)
{
std::ifstream f(path_);
if (!(f.is_open() && f.good()))
Expand Down Expand Up @@ -307,6 +308,15 @@ void SensorDriver::check_correction_length()
}


void SensorDriver::set_optional(bool o)
{ optional_ = o; }

bool SensorDriver::optional() const
{ return optional_; }

const string &SensorDriver::path() const
{ return path_; }

/*----------------------------------------------------------------------------
| HwmonSensorDriver: A driver for sensors provided by other kernel drivers, |
| typically somewhere in sysfs. |
Expand Down
4 changes: 4 additions & 0 deletions src/drivers.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,15 @@ class SensorDriver {
void set_correction(const std::vector<int> &correction);
void set_num_temps(unsigned int n);
bool operator == (const SensorDriver &other) const;
void set_optional(bool);
bool optional() const;
const string &path() const;
protected:
string path_;
std::vector<int> correction_;
private:
unsigned int num_temps_;
bool optional_;
void check_correction_length();
};

Expand Down
11 changes: 10 additions & 1 deletion src/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "message.h"
#include "thinkfan.h"
#include "drivers.h"

#ifdef USE_YAML
#include <yaml-cpp/exceptions.h>
Expand Down Expand Up @@ -73,11 +74,19 @@ class IOerror : public ExpectedError {
const int code_;
public:
IOerror(const string &message, const int error_code);

int code();
};


class SensorLost : public ExpectedError {
public:
template<class CauseT>
SensorLost(const CauseT &cause) {
msg_ = string("Lost sensor ") + cause.what();
}
};


class SyntaxError : public ExpectedError {
public:
SyntaxError(const string filename, const std::ptrdiff_t offset, const string &input);
Expand Down
23 changes: 21 additions & 2 deletions src/thinkfan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "thinkfan.h"
#include "config.h"
#include "message.h"
#include "error.h"


namespace thinkfan {
Expand All @@ -52,6 +53,7 @@ seconds tmp_sleeptime = sleeptime;
float bias_level(1.5);
float depulse = 0;
TemperatureState temp_state(0);
bool can_lose_sensor = false;

#ifdef USE_YAML
std::vector<string> config_files { DEFAULT_YAML_CONFIG, DEFAULT_CONFIG };
Expand Down Expand Up @@ -92,6 +94,15 @@ void sig_handler(int signum) {



static void sensor_lost(const SensorDriver *s, const ExpectedError &e) {
if (!s->optional())
error<SensorLost>(e);
else
log(TF_INF) << SensorLost(e).what();
temp_state.add_temp(0);
}


void run(const Config &config)
{
tmp_sleeptime = sleeptime;
Expand All @@ -115,8 +126,16 @@ void run(const Config &config)

temp_state.restart();

for (const SensorDriver *sensor : config.sensors())
sensor->read_temps();
for (const SensorDriver *sensor : config.sensors()) {
try {
sensor->read_temps();
} catch (SystemError &e) {
sensor_lost(sensor, e);
} catch (IOerror &e) {
sensor_lost(sensor, e);
}
}

if (unlikely(!temp_state.complete()))
throw SystemError(MSG_SENSOR_LOST);

Expand Down
1 change: 1 addition & 0 deletions src/thinkfan.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ extern volatile int interrupted;
extern TemperatureState temp_state;
extern std::vector<string> config_files;
extern float depulse;
extern bool can_lose_sensor;


}
Expand Down
15 changes: 14 additions & 1 deletion src/yamlconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ static const string kw_self(".");
static const string kw_parent("..");
static const string kw_indices("indices");
static const string kw_correction("correction");
static const string kw_optional("optional");

#ifdef HAVE_OLD_YAMLCPP
static inline Mark get_mark_compat(const Node &)
Expand Down Expand Up @@ -265,8 +266,11 @@ struct convert<vector<wtf_ptr<HwmonSensorDriver>>> {
sensor->set_correction(vector<int>(1, *it++));
});
}
for (const wtf_ptr<HwmonSensorDriver> &h : hwmons)
for (wtf_ptr<HwmonSensorDriver> &h : hwmons) {
if (node[kw_optional])
h->set_optional(node[kw_optional].as<bool>());
sensors.push_back(std::move(h));
}
}
else {
wtf_ptr<HwmonSensorDriver> h = make_wtf<HwmonSensorDriver>(path, correction);
Expand Down Expand Up @@ -298,6 +302,9 @@ struct convert<wtf_ptr<TpSensorDriver>> {
else
sensor = make_wtf<TpSensorDriver>(node[kw_tpacpi].as<string>(), correction);

if (node[kw_optional])
sensor->set_optional(node[kw_optional].as<bool>());

return true;
}
};
Expand All @@ -316,6 +323,9 @@ struct convert<wtf_ptr<NvmlSensorDriver>> {
correction = node[kw_correction].as<vector<int>>();

sensor = make_wtf<NvmlSensorDriver>(node[kw_nvidia].as<string>(), correction);
if (node[kw_optional])
sensor->set_optional(node[kw_optional].as<bool>());

return true;
}
};
Expand All @@ -335,6 +345,9 @@ struct convert<wtf_ptr<AtasmartSensorDriver>> {
correction = node[kw_correction].as<vector<int>>();

sensor = make_wtf<AtasmartSensorDriver>(node["atasmart"].as<string>(), correction);
if (node[kw_optional])
sensor->set_optional(node[kw_optional].as<bool>());

return true;
}
};
Expand Down

0 comments on commit 8a7d248

Please sign in to comment.