xref: /openbmc/phosphor-fan-presence/presence/rpolicy.hpp (revision dfddd648cb81b27492afead4e2346f5fcd1397cb)
1 #pragma once
2 
3 #include "eeprom_device.hpp"
4 #include "fan.hpp"
5 
6 namespace phosphor
7 {
8 namespace fan
9 {
10 namespace presence
11 {
12 
13 class PresenceSensor;
14 
15 /**
16  * @class RedundancyPolicy
17  * @brief Redundancy policy interface.
18  *
19  * Provide concrete implementations of RedundancyPolicy to realize
20  * new redundancy logic.
21  *
22  * A fan can have multiple ways to detect whether or not it is present.
23  * The redundancy policy encapsulates the logic to aggregate those
24  * inputs into a single yes or no the fan is present.
25  */
26 class RedundancyPolicy
27 {
28   public:
29     RedundancyPolicy(const RedundancyPolicy&) = default;
30     RedundancyPolicy& operator=(const RedundancyPolicy&) = default;
31     RedundancyPolicy(RedundancyPolicy&&) = default;
32     RedundancyPolicy& operator=(RedundancyPolicy&&) = default;
33     virtual ~RedundancyPolicy() = default;
34 
35     /**
36      * @brief Construct a new Redundancy Policy.
37      *
38      * @param[in] fan - The fan associated with this policy.
39      * @param[in] eeprom - EEPROM device instance
40      */
RedundancyPolicy(const Fan & f,std::unique_ptr<EEPROMDevice> eeprom)41     explicit RedundancyPolicy(const Fan& f,
42                               std::unique_ptr<EEPROMDevice> eeprom) :
43         fan(f), eepromDevice(std::move(eeprom))
44     {}
45 
46     /**
47      * @brief stateChanged
48      *
49      * Typically invoked by presence sensors to signify
50      * a change of presence.  Implementations should update
51      * the inventory and execute their policy logic.
52      *
53      * @param[in] present - The new state of the sensor.
54      * @param[in] sensor - The sensor that changed state.
55      */
56     virtual void stateChanged(bool present, PresenceSensor& sensor) = 0;
57 
58     /**
59      * @brief monitor
60      *
61      * Implementations should start monitoring the sensors
62      * associated with the fan.
63      */
64     virtual void monitor() = 0;
65 
66   protected:
67     /** @brief Fan name and inventory path. */
68     const Fan& fan;
69 
70     /**
71      * @brief Handles binding the EEPROM driver on fan plug
72      *        if configured to do so.
73      */
74     std::unique_ptr<EEPROMDevice> eepromDevice;
75 };
76 
77 /**
78  * @class PolicyAccess
79  * @brief Policy association.
80  *
81  * PolicyAccess can be used to associate a redundancy policy
82  * with something else.
83  *
84  * Wrap the type to be associated with a policy with PolicyAccess.
85  *
86  * @tparam T - The type to associate with a redundancy policy.
87  * @tparam Policy - An array type where the policy is stored.
88  */
89 template <typename T, typename Policy>
90 class PolicyAccess : public T
91 {
92   public:
93     PolicyAccess() = default;
94     PolicyAccess(const PolicyAccess&) = default;
95     PolicyAccess& operator=(const PolicyAccess&) = default;
96     PolicyAccess(PolicyAccess&&) = default;
97     PolicyAccess& operator=(PolicyAccess&&) = default;
98     ~PolicyAccess() = default;
99 
100     /**
101      * @brief Construct a new PolicyAccess wrapped object.
102      *
103      * @param[in] index - The array index in Policy.
104      * @tparam Args - Forwarded to wrapped type constructor.
105      */
106     template <typename... Args>
PolicyAccess(size_t index,Args &&...args)107     PolicyAccess(size_t index, Args&&... args) :
108         T(std::forward<Args>(args)...), policy(index)
109     {}
110 
111   private:
112     /**
113      * @brief Get the associated policy.
114      */
getPolicy()115     RedundancyPolicy& getPolicy() override
116     {
117         return *Policy::get()[policy];
118     }
119 
120     /** The associated policy index. */
121     size_t policy;
122 };
123 } // namespace presence
124 } // namespace fan
125 } // namespace phosphor
126