xref: /openbmc/phosphor-fan-presence/presence/fallback.hpp (revision 5593560b1e1a7785a491d4650c4f3f61ffdaba90)
1 #pragma once
2 
3 #include <functional>
4 #include <vector>
5 #include "fan.hpp"
6 #include "rpolicy.hpp"
7 
8 namespace phosphor
9 {
10 namespace fan
11 {
12 namespace presence
13 {
14 
15 class PresenceSensor;
16 
17 /**
18  * @class Fallback
19  * @brief Fallback redundancy policy.
20  *
21  * The fallback redundancy policy falls back to
22  * subsequent presence sensors when the active
23  * sensor indicates not present and a fallback
24  * sensor indicates the fan is present.
25  */
26 class Fallback : public RedundancyPolicy
27 {
28     public:
29         Fallback() = delete;
30         Fallback(const Fallback&) = default;
31         Fallback& operator=(const Fallback&) = default;
32         Fallback(Fallback&&) = default;
33         Fallback& operator=(Fallback&&) = default;
34         ~Fallback() = default;
35 
36         /**
37          * @brief Construct a fallback policy.
38          *
39          * @param[in] fan - The fan associated with the policy.
40          * @param[in] s - The set of sensors associated with the policy.
41          */
42         Fallback(
43                 const Fan& fan,
44                 const std::vector<std::reference_wrapper<PresenceSensor>>& s) :
45             RedundancyPolicy(fan), sensors(s)
46         {
47             activeSensor = sensors.begin();
48         }
49 
50         /**
51          * @brief stateChanged
52          *
53          * Update the inventory and execute the fallback
54          * policy.
55          *
56          * @param[in] present - The new presence state according
57          *             to the active sensor.
58          */
59         void stateChanged(bool present) override;
60 
61         /**
62          * @brief monitor
63          *
64          * Start monitoring the fan.
65          */
66         void monitor() override;
67 
68     private:
69 
70         /** @brief All presence sensors in the redundancy set. */
71         std::vector<std::reference_wrapper<PresenceSensor>> sensors;
72 
73         /** @brief The active presence sensor. */
74         decltype(sensors)::iterator activeSensor;
75 };
76 
77 } // namespace presence
78 } // namespace fan
79 } // namespace phosphor
80