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