Refactoring threshold properties code.
Multiple threshold interfaces like Hardshutdown and Softshutdown
needs to be created for sensors. Therefore, array has been created
to store multiple properties of thresholds.
Threshold level, direction and properties can be accessed based on
the array created. Moreover, lines of code has been reduced by
refactoring existing threshold code.
TESTED: Tested on Facebook YosemiteV2 hardware. Verified that the
Warning and Critical interfaces and properties are created and
displaying in dbus objects.
Signed-off-by: Jayashree Dhanapal <jayashree-d@hcl.com>
Change-Id: I98be6dd723a6f66336147ec572f161a6c5df3691
diff --git a/include/Thresholds.hpp b/include/Thresholds.hpp
index 209d68e..528fc95 100644
--- a/include/Thresholds.hpp
+++ b/include/Thresholds.hpp
@@ -13,15 +13,17 @@
struct Sensor;
namespace thresholds
{
-enum Level
+enum class Level
{
WARNING,
- CRITICAL
+ CRITICAL,
+ ERROR
};
-enum Direction
+enum class Direction
{
HIGH,
- LOW
+ LOW,
+ ERROR
};
struct Threshold
{
diff --git a/include/sensor.hpp b/include/sensor.hpp
index d38bcde..ff8bf8e 100644
--- a/include/sensor.hpp
+++ b/include/sensor.hpp
@@ -96,6 +96,26 @@
// construction of your Sensor subclass. See ExternalSensor for example.
std::function<void()> externalSetHook;
+ struct ThresholdProperty
+ {
+ thresholds::Level level;
+ thresholds::Direction direction;
+ uint8_t sevOrder;
+ const char* levelProperty;
+ const char* alarmProperty;
+ const char* dirOrder;
+ };
+
+ constexpr static std::array<ThresholdProperty, 4> thresProp = {
+ {{thresholds::Level::WARNING, thresholds::Direction::HIGH, 0,
+ "WarningHigh", "WarningAlarmHigh", "greater than"},
+ {thresholds::Level::WARNING, thresholds::Direction::LOW, 0,
+ "WarningLow", "WarningAlarmLow", "less than"},
+ {thresholds::Level::CRITICAL, thresholds::Direction::HIGH, 1,
+ "CriticalHigh", "CriticalAlarmHigh", "greater than"},
+ {thresholds::Level::CRITICAL, thresholds::Direction::LOW, 1,
+ "CriticalLow", "CriticalAlarmLow", "less than"}}};
+
void updateInstrumentation(double readValue)
{
// Do nothing if this feature is not enabled
@@ -245,40 +265,18 @@
threshold.hysteresis = hysteresisTrigger;
}
std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
- std::string level;
- std::string alarm;
if (threshold.level == thresholds::Level::CRITICAL)
{
iface = thresholdInterfaceCritical;
- if (threshold.direction == thresholds::Direction::HIGH)
- {
- level = "CriticalHigh";
- alarm = "CriticalAlarmHigh";
- }
- else
- {
- level = "CriticalLow";
- alarm = "CriticalAlarmLow";
- }
}
else if (threshold.level == thresholds::Level::WARNING)
{
iface = thresholdInterfaceWarning;
- if (threshold.direction == thresholds::Direction::HIGH)
- {
- level = "WarningHigh";
- alarm = "WarningAlarmHigh";
- }
- else
- {
- level = "WarningLow";
- alarm = "WarningAlarmLow";
- }
}
else
{
- std::cerr << "Unknown threshold level" << threshold.level
- << "\n";
+ std::cerr << "Unknown threshold level"
+ << static_cast<int>(threshold.level) << "\n";
continue;
}
if (!iface)
@@ -287,6 +285,15 @@
continue;
}
+ std::string level =
+ propertyLevel(threshold.level, threshold.direction);
+ std::string alarm =
+ propertyAlarm(threshold.level, threshold.direction);
+
+ if ((level.empty()) || (alarm.empty()))
+ {
+ continue;
+ }
size_t thresSize =
label.empty() ? thresholds.size() : thresholdSize;
iface->register_property(
@@ -373,6 +380,32 @@
}
}
+ std::string propertyLevel(const thresholds::Level lev,
+ const thresholds::Direction dir)
+ {
+ for (ThresholdProperty prop : thresProp)
+ {
+ if ((prop.level == lev) && (prop.direction == dir))
+ {
+ return prop.levelProperty;
+ }
+ }
+ return "";
+ }
+
+ std::string propertyAlarm(const thresholds::Level lev,
+ const thresholds::Direction dir)
+ {
+ for (ThresholdProperty prop : thresProp)
+ {
+ if ((prop.level == lev) && (prop.direction == dir))
+ {
+ return prop.alarmProperty;
+ }
+ }
+ return "";
+ }
+
bool readingStateGood()
{
if (readState == PowerState::on && !isPowerOn())