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