Let PowerState class run multiple callbacks
Previously the PowerState class could only handle a single callback
function. This commit changes that to allow other callback functions to
be added with a addCallback() method. The callback functions are then
executed when the power state changes.
This also changes the use of the PowerState object in the fan presence
code to a shared_ptr instead of a unique_ptr, and adds a function to
return the PowerState object to use in the app, creating it if
necessary. This allows multiple pieces of the code to add their own
callbacks to the same object.
Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: I35fc8ab94c4806f0c7fd2f1552c58131b8f30f23
diff --git a/presence/Makefile.am b/presence/Makefile.am
index 2affa91..b0b00df 100644
--- a/presence/Makefile.am
+++ b/presence/Makefile.am
@@ -19,6 +19,7 @@
error_reporter.cpp \
fallback.cpp \
fan.cpp \
+ get_power_state.cpp \
gpio.cpp \
logging.cpp \
psensor.cpp \
diff --git a/presence/error_reporter.cpp b/presence/error_reporter.cpp
index f1dfaa9..72c7dae 100644
--- a/presence/error_reporter.cpp
+++ b/presence/error_reporter.cpp
@@ -15,6 +15,7 @@
*/
#include "error_reporter.hpp"
+#include "get_power_state.hpp"
#include "logging.hpp"
#include "psensor.hpp"
#include "utility.hpp"
@@ -46,14 +47,12 @@
const std::vector<
std::tuple<Fan, std::vector<std::unique_ptr<PresenceSensor>>>>& fans) :
_bus(bus),
- _event(sdeventplus::Event::get_default())
+ _event(sdeventplus::Event::get_default()),
+ _powerState(getPowerStateObject())
{
- // If different methods to check the power state are needed across the
- // various platforms, the method/class to use could be read out of JSON
- // or set with a compilation flag.
- _powerState = std::make_unique<PGoodState>(
- bus, std::bind(std::mem_fn(&ErrorReporter::powerStateChanged), this,
- std::placeholders::_1));
+ _powerState->addCallback("errorReporter",
+ std::bind(&ErrorReporter::powerStateChanged, this,
+ std::placeholders::_1));
for (const auto& fan : fans)
{
diff --git a/presence/error_reporter.hpp b/presence/error_reporter.hpp
index a6a86fe..a8c9d6f 100644
--- a/presence/error_reporter.hpp
+++ b/presence/error_reporter.hpp
@@ -104,7 +104,7 @@
/**
* @brief Base class pointer to the power state implementation.
*/
- std::unique_ptr<PowerState> _powerState;
+ std::shared_ptr<PowerState> _powerState;
/**
* @brief The map of fan paths to their presence states.
diff --git a/presence/get_power_state.cpp b/presence/get_power_state.cpp
new file mode 100644
index 0000000..f1d913f
--- /dev/null
+++ b/presence/get_power_state.cpp
@@ -0,0 +1,17 @@
+#include "power_state.hpp"
+
+namespace phosphor::fan
+{
+
+std::shared_ptr<PowerState> powerState;
+
+std::shared_ptr<PowerState> getPowerStateObject()
+{
+ if (!powerState)
+ {
+ powerState = std::make_shared<PGoodState>();
+ }
+ return powerState;
+}
+
+} // namespace phosphor::fan
diff --git a/presence/get_power_state.hpp b/presence/get_power_state.hpp
new file mode 100644
index 0000000..422852a
--- /dev/null
+++ b/presence/get_power_state.hpp
@@ -0,0 +1,18 @@
+#pragma once
+
+#include "power_state.hpp"
+
+namespace phosphor::fan
+{
+
+/**
+ * @brief Returns the PowerState object as a shared_ptr.
+ *
+ * Callers can use addCallback() on the return object to
+ * have functions run when the power state changes.
+ *
+ * @return shared_ptr<PowerState>
+ */
+std::shared_ptr<PowerState> getPowerStateObject();
+
+} // namespace phosphor::fan