Rearrange thresholds to be pair based
It was pointed out in another review that the way the dbus interfaces
are set up, they are all pair-wise, with a positive and negative end
of the interface. This changes the data structures to match that
reality, inventing a "ThresholdDefinition" object. This reduces the
code, and allows us to promote the level definitions up to a higher
scope in the structure.
This greatly simplifies the thresProp structure to remove a number of
elements that were redundant, and allows adding new thresholds easier
in the future.
Tested: Tested on Vegman system; Thresholds work as designed.
Signed-off-by: Ed Tanous <edtanous@google.com>
Change-Id: Ic9d5ba8282f78fe928689cb7fbd98d575a570200
diff --git a/src/Thresholds.cpp b/src/Thresholds.cpp
index f50905a..89269a2 100644
--- a/src/Thresholds.cpp
+++ b/src/Thresholds.cpp
@@ -19,11 +19,11 @@
static constexpr bool debug = false;
namespace thresholds
{
-Level findThresholdLevel(uint8_t sev, const std::string& direct)
+Level findThresholdLevel(uint8_t sev)
{
- for (Sensor::ThresholdProperty prop : Sensor::thresProp)
+ for (const ThresholdDefinition& prop : thresProp)
{
- if ((prop.sevOrder == sev) && (prop.dirOrder == direct))
+ if (prop.sevOrder == sev)
{
return prop.level;
}
@@ -31,23 +31,24 @@
return Level::ERROR;
}
-Direction findThresholdDirection(uint8_t sev, const std::string& direct)
+Direction findThresholdDirection(const std::string& direct)
{
- for (Sensor::ThresholdProperty prop : Sensor::thresProp)
+ if (direct == "greater than")
{
- if ((prop.sevOrder == sev) && (prop.dirOrder == direct))
- {
- return prop.direction;
- }
+ return Direction::HIGH;
+ }
+ if (direct == "less than")
+ {
+ return Direction::LOW;
}
return Direction::ERROR;
}
-bool findOrder(Level lev, Direction dir)
+bool isValidLevel(Level lev)
{
- for (Sensor::ThresholdProperty prop : Sensor::thresProp)
+ for (const ThresholdDefinition& prop : thresProp)
{
- if ((prop.level == lev) && (prop.direction == dir))
+ if (prop.level == lev)
{
return true;
}
@@ -123,8 +124,8 @@
std::string directions =
std::visit(VariantToStringVisitor(), directionFind->second);
- Level level = findThresholdLevel(severity, directions);
- Direction direction = findThresholdDirection(severity, directions);
+ Level level = findThresholdLevel(severity);
+ Direction direction = findThresholdDirection(directions);
if ((level == Level::ERROR) || (direction == Direction::ERROR))
{
@@ -186,9 +187,8 @@
std::string dir =
std::visit(VariantToStringVisitor(), directionFind->second);
- if (((findThresholdLevel(severity, dir)) != threshold.level) ||
- ((findThresholdDirection(severity, dir)) !=
- threshold.direction))
+ if ((findThresholdLevel(severity) != threshold.level) ||
+ (findThresholdDirection(dir) != threshold.direction))
{
return; // not the droid we're looking for
}
@@ -214,7 +214,7 @@
{
for (const auto& threshold : sensor->thresholds)
{
- if (!findOrder(threshold.level, threshold.direction))
+ if (!isValidLevel(threshold.level))
{
continue;
}
@@ -454,7 +454,7 @@
thresholds::Level level, thresholds::Direction direction,
bool assert)
{
- if (!findOrder(level, direction))
+ if (!isValidLevel(level))
{
return;
}
@@ -548,26 +548,14 @@
std::string getInterface(const Level thresholdLevel)
{
- std::string level;
- switch (thresholdLevel)
+ for (const ThresholdDefinition& thresh : thresProp)
{
- case Level::WARNING:
- level = "Warning";
- break;
- case Level::CRITICAL:
- level = "Critical";
- break;
- case Level::SOFTSHUTDOWN:
- level = "SoftShutdown";
- break;
- case Level::HARDSHUTDOWN:
- level = "HardShutdown";
- break;
- case Level::ERROR:
- level = "Error";
- break;
+ if (thresh.level == thresholdLevel)
+ {
+ return std::string("xyz.openbmc_project.Sensor.Threshold.") +
+ thresh.levelName;
+ }
}
- std::string interface = "xyz.openbmc_project.Sensor.Threshold." + level;
- return interface;
+ return "";
}
} // namespace thresholds