presence: Add fallback redundancy policy
Add a fallback redundancy implementation of the RedundancyPolicy
interface.
The fallback policy associates multiple PresenceSensor instances
to a single fan, and "falls-back" on secondary sensor implementations
when the primary sensor cannot see the fan.
Change-Id: I6468d77d97b8916b3ff33bcd0cd28a102d1aaba1
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
diff --git a/presence/fallback.hpp b/presence/fallback.hpp
new file mode 100644
index 0000000..a1a5b29
--- /dev/null
+++ b/presence/fallback.hpp
@@ -0,0 +1,79 @@
+#pragma once
+
+#include <functional>
+#include <vector>
+#include "fan.hpp"
+#include "rpolicy.hpp"
+
+namespace phosphor
+{
+namespace fan
+{
+namespace presence
+{
+
+class PresenceSensor;
+
+/**
+ * @class Fallback
+ * @brief Fallback redundancy policy.
+ *
+ * The fallback redundancy policy falls back to
+ * subsequent presence sensors when the active
+ * sensor indicates not present and a fallback
+ * sensor indicates the fan is present.
+ */
+class Fallback : public RedundancyPolicy
+{
+ public:
+ Fallback() = delete;
+ Fallback(const Fallback&) = default;
+ Fallback& operator=(const Fallback&) = default;
+ Fallback(Fallback&&) = default;
+ Fallback& operator=(Fallback&&) = default;
+ ~Fallback() = default;
+
+ /**
+ * @brief Construct a fallback policy.
+ *
+ * @param[in] fan - The fan associated with the policy.
+ * @param[in] s - The set of sensors associated with the policy.
+ */
+ Fallback(
+ const Fan& fan,
+ const std::vector<std::reference_wrapper<PresenceSensor>>& s) :
+ RedundancyPolicy(fan), sensors(s)
+ {
+ activeSensor = sensors.begin();
+ }
+
+ /**
+ * @brief stateChanged
+ *
+ * Update the inventory and execute the fallback
+ * policy.
+ *
+ * @param[in] present - The new presence state according
+ * to the active sensor.
+ */
+ void stateChanged(bool present) override;
+
+ /**
+ * @brief monitor
+ *
+ * Start monitoring the fan.
+ */
+ void monitor() override;
+
+ private:
+
+ /** @brief All presence sensors in the redundancy set. */
+ std::vector<std::reference_wrapper<PresenceSensor>> sensors;
+
+ /** @brief The active presence sensor. */
+ decltype(sensors)::iterator activeSensor;
+};
+
+} // namespace presence
+} // namespace fan
+} // namespace phosphor