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