Move threshold interfaces to common function
Threshold Hardshutdown and Softshutdown interfaces needs to be
created for sensors. Therefore, created a common function
called getInterface() to access all the threshold
interfaces from all the sensors source file and removed
hasCriticalInterface(), hasWarningInterface() functions.
Moreover, threshold interfaces from all the sensor constructor has
been refactored to avoid duplicating functions.
TESTED: Tested on Facebook YosemiteV2 hardware. Verified that the
Warning and Critical interfaces are created and displaying in
dbus objects.
Signed-off-by: Jayashree Dhanapal <jayashree-d@hcl.com>
Change-Id: I9c7d049c7d4445d7199bf63d8e729838990880e9
diff --git a/include/Thresholds.hpp b/include/Thresholds.hpp
index 528fc95..5dcc00d 100644
--- a/include/Thresholds.hpp
+++ b/include/Thresholds.hpp
@@ -110,6 +110,8 @@
std::list<TimerPair> timers;
};
+bool findOrder(Level lev, Direction dir);
+
bool parseThresholdsFromConfig(
const SensorData& sensorData,
std::vector<thresholds::Threshold>& thresholdVector,
@@ -119,11 +121,8 @@
const std::string& inputPath,
const double& scaleFactor,
const double& offset = 0);
-bool hasCriticalInterface(
- const std::vector<thresholds::Threshold>& thresholdVector);
-bool hasWarningInterface(
- const std::vector<thresholds::Threshold>& thresholdVector);
+std::string getInterface(const Level level);
void persistThreshold(const std::string& baseInterface, const std::string& path,
const thresholds::Threshold& threshold,
diff --git a/include/sensor.hpp b/include/sensor.hpp
index ff8bf8e..9d01482 100644
--- a/include/sensor.hpp
+++ b/include/sensor.hpp
@@ -73,8 +73,6 @@
double minValue;
std::vector<thresholds::Threshold> thresholds;
std::shared_ptr<sdbusplus::asio::dbus_interface> sensorInterface;
- std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceWarning;
- std::shared_ptr<sdbusplus::asio::dbus_interface> thresholdInterfaceCritical;
std::shared_ptr<sdbusplus::asio::dbus_interface> association;
std::shared_ptr<sdbusplus::asio::dbus_interface> availableInterface;
std::shared_ptr<sdbusplus::asio::dbus_interface> operationalInterface;
@@ -116,6 +114,23 @@
{thresholds::Level::CRITICAL, thresholds::Direction::LOW, 1,
"CriticalLow", "CriticalAlarmLow", "less than"}}};
+ std::array<std::shared_ptr<sdbusplus::asio::dbus_interface>, 2>
+ thresholdInterfaces;
+
+ std::shared_ptr<sdbusplus::asio::dbus_interface>
+ getThresholdInterface(thresholds::Level lev)
+ {
+ size_t index = static_cast<size_t>(lev);
+ if (index >= thresholdInterfaces.size())
+ {
+ std::cout << "Unknown threshold level \n";
+ return nullptr;
+ }
+ std::shared_ptr<sdbusplus::asio::dbus_interface> interface =
+ thresholdInterfaces[index];
+ return interface;
+ }
+
void updateInstrumentation(double readValue)
{
// Do nothing if this feature is not enabled
@@ -264,21 +279,13 @@
{
threshold.hysteresis = hysteresisTrigger;
}
- std::shared_ptr<sdbusplus::asio::dbus_interface> iface;
- if (threshold.level == thresholds::Level::CRITICAL)
+ if (!(thresholds::findOrder(threshold.level, threshold.direction)))
{
- iface = thresholdInterfaceCritical;
- }
- else if (threshold.level == thresholds::Level::WARNING)
- {
- iface = thresholdInterfaceWarning;
- }
- else
- {
- std::cerr << "Unknown threshold level"
- << static_cast<int>(threshold.level) << "\n";
continue;
}
+ std::shared_ptr<sdbusplus::asio::dbus_interface> iface =
+ getThresholdInterface(threshold.level);
+
if (!iface)
{
std::cout << "trying to set uninitialized interface\n";
@@ -321,16 +328,16 @@
{
std::cerr << "error initializing value interface\n";
}
- if (thresholdInterfaceWarning &&
- !thresholdInterfaceWarning->initialize(true))
- {
- std::cerr << "error initializing warning threshold interface\n";
- }
- if (thresholdInterfaceCritical &&
- !thresholdInterfaceCritical->initialize(true))
+ for (auto& thresIface : thresholdInterfaces)
{
- std::cerr << "error initializing critical threshold interface\n";
+ if (thresIface)
+ {
+ if (!thresIface->initialize(true))
+ {
+ std::cerr << "Error initializing threshold interface \n";
+ }
+ }
}
if (isValueMutable)