Add base class to simplify threshold logic
Add base class for the default case check_thresholds
calls the thresholds.cpp function. This way for normal
things we can just pass sensors around as the base class.
Make it so that theshold alerts are only sent once.
Change-Id: Ic8e3ae1900aeb12b74c099f637f45bc038bd3c9a
Signed-off-by: James Feist <james.feist@linux.intel.com>
diff --git a/sensors/src/CPUSensor.cpp b/sensors/src/CPUSensor.cpp
index 66bca1f..ce9e60b 100644
--- a/sensors/src/CPUSensor.cpp
+++ b/sensors/src/CPUSensor.cpp
@@ -35,27 +35,28 @@
boost::asio::io_service &io, const std::string &sensorName,
std::vector<thresholds::Threshold> &&_thresholds,
const std::string &sensorConfiguration) :
- path(path),
- objectType(objectType), objServer(objectServer),
+ Sensor(),
+ path(path), objectType(objectType), objServer(objectServer),
name(boost::replace_all_copy(sensorName, " ", "_")), dbusConnection(conn),
- thresholds(std::move(_thresholds)), configuration(sensorConfiguration),
- sensor_interface(objectServer.add_interface(
- "/xyz/openbmc_project/sensors/temperature/" + name,
- "xyz.openbmc_project.Sensor.Value")),
- input_dev(io, open(path.c_str(), O_RDONLY)), wait_timer(io),
- value(std::numeric_limits<double>::quiet_NaN()), err_count(0),
+ configuration(sensorConfiguration),
+
+ input_dev(io, open(path.c_str(), O_RDONLY)), wait_timer(io), err_count(0),
// todo, get these from config
max_value(127), min_value(-128)
{
+ thresholds = std::move(_thresholds);
+ sensorInterface = objectServer.add_interface(
+ "/xyz/openbmc_project/sensors/temperature/" + name,
+ "xyz.openbmc_project.Sensor.Value");
if (thresholds::HasWarningInterface(thresholds))
{
- threshold_interface_warning = objectServer.add_interface(
+ thresholdInterfaceWarning = objectServer.add_interface(
"/xyz/openbmc_project/sensors/temperature/" + name,
"xyz.openbmc_project.Sensor.Threshold.Warning");
}
if (thresholds::HasCriticalInterface(thresholds))
{
- threshold_interface_critical = objectServer.add_interface(
+ thresholdInterfaceCritical = objectServer.add_interface(
"/xyz/openbmc_project/sensors/temperature/" + name,
"xyz.openbmc_project.Sensor.Threshold.Critical");
}
@@ -69,9 +70,9 @@
// close the input dev to cancel async operations
input_dev.close();
wait_timer.cancel();
- objServer.remove_interface(threshold_interface_warning);
- objServer.remove_interface(threshold_interface_critical);
- objServer.remove_interface(sensor_interface);
+ objServer.remove_interface(thresholdInterfaceWarning);
+ objServer.remove_interface(thresholdInterfaceCritical);
+ objServer.remove_interface(sensorInterface);
}
void CPUSensor::setup_read(void)
@@ -128,7 +129,7 @@
else
{
err_count = 0; // check power again in 10 cycles
- sensor_interface->set_property(
+ sensorInterface->set_property(
"Value", std::numeric_limits<double>::quiet_NaN());
}
}
@@ -154,92 +155,23 @@
void CPUSensor::check_thresholds(void)
{
- if (thresholds.empty())
- return;
- for (auto threshold : thresholds)
- {
- if (threshold.direction == thresholds::Direction::HIGH)
- {
- if (value > threshold.value)
- {
- assert_thresholds(threshold.level, threshold.direction, true);
- }
- else
- {
- assert_thresholds(threshold.level, threshold.direction, false);
- }
- }
- else
- {
- if (value < threshold.value)
- {
- assert_thresholds(threshold.level, threshold.direction, true);
- }
- else
- {
- assert_thresholds(threshold.level, threshold.direction, false);
- }
- }
- }
+ thresholds::checkThresholds(this);
}
void CPUSensor::update_value(const double &new_value)
{
- sensor_interface->set_property("Value", new_value);
+ sensorInterface->set_property("Value", new_value);
value = new_value;
check_thresholds();
}
-void CPUSensor::assert_thresholds(thresholds::Level level,
- thresholds::Direction direction, bool assert)
-{
- std::string property;
- std::shared_ptr<sdbusplus::asio::dbus_interface> interface;
- if (level == thresholds::Level::WARNING &&
- direction == thresholds::Direction::HIGH)
- {
- property = "WarningAlarmHigh";
- interface = threshold_interface_warning;
- }
- else if (level == thresholds::Level::WARNING &&
- direction == thresholds::Direction::LOW)
- {
- property = "WarningAlarmLow";
- interface = threshold_interface_warning;
- }
- else if (level == thresholds::Level::CRITICAL &&
- direction == thresholds::Direction::HIGH)
- {
- property = "CriticalAlarmHigh";
- interface = threshold_interface_critical;
- }
- else if (level == thresholds::Level::CRITICAL &&
- direction == thresholds::Direction::LOW)
- {
- property = "CriticalAlarmLow";
- interface = threshold_interface_critical;
- }
- else
- {
- std::cerr << "Unknown threshold, level " << level << "direction "
- << direction << "\n";
- return;
- }
- if (!interface)
- {
- std::cout << "trying to set uninitialized interface\n";
- return;
- }
- interface->set_property(property, assert);
-}
-
void CPUSensor::set_initial_properties(
std::shared_ptr<sdbusplus::asio::connection> &conn)
{
// todo, get max and min from configuration
- sensor_interface->register_property("MaxValue", max_value);
- sensor_interface->register_property("MinValue", min_value);
- sensor_interface->register_property("Value", value);
+ sensorInterface->register_property("MaxValue", max_value);
+ sensorInterface->register_property("MinValue", min_value);
+ sensorInterface->register_property("Value", value);
for (auto &threshold : thresholds)
{
@@ -248,7 +180,7 @@
std::string alarm;
if (threshold.level == thresholds::Level::CRITICAL)
{
- iface = threshold_interface_critical;
+ iface = thresholdInterfaceCritical;
if (threshold.direction == thresholds::Direction::HIGH)
{
level = "CriticalHigh";
@@ -262,7 +194,7 @@
}
else if (threshold.level == thresholds::Level::WARNING)
{
- iface = threshold_interface_warning;
+ iface = thresholdInterfaceWarning;
if (threshold.direction == thresholds::Direction::HIGH)
{
level = "WarningHigh";
@@ -302,18 +234,16 @@
}
iface->register_property(alarm, false);
}
- if (!sensor_interface->initialize())
+ if (!sensorInterface->initialize())
{
std::cerr << "error initializing value interface\n";
}
- if (threshold_interface_warning &&
- !threshold_interface_warning->initialize())
+ if (thresholdInterfaceWarning && !thresholdInterfaceWarning->initialize())
{
std::cerr << "error initializing warning threshold interface\n";
}
- if (threshold_interface_critical &&
- !threshold_interface_critical->initialize())
+ if (thresholdInterfaceCritical && !thresholdInterfaceCritical->initialize())
{
std::cerr << "error initializing critical threshold interface\n";
}