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     /**
43      * @brief stateChanged
44      *
45      * Typically invoked by presence sensors to signify
46      * a change of presence.  Implementations should update
47      * the inventory and execute their policy logic.
48      *
49      * @param[in] present - The new state of the sensor.
50      * @param[in] sensor - The sensor that changed state.
51      */
52     virtual void stateChanged(bool present, PresenceSensor& sensor) = 0;
53 
54     /**
55      * @brief monitor
56      *
57      * Implementations should start monitoring the sensors
58      * associated with the fan.
59      */
60     virtual void monitor() = 0;
61 
62   protected:
63     /** @brief Fan name and inventory path. */
64     const Fan& fan;
65 };
66 
67 /**
68  * @class PolicyAccess
69  * @brief Policy association.
70  *
71  * PolicyAccess can be used to associate a redundancy policy
72  * with something else.
73  *
74  * Wrap the type to be associated with a policy with PolicyAccess.
75  *
76  * @tparam T - The type to associate with a redundancy policy.
77  * @tparam Policy - An array type where the policy is stored.
78  */
79 template <typename T, typename Policy>
80 class PolicyAccess : public T
81 {
82   public:
83     PolicyAccess() = default;
84     PolicyAccess(const PolicyAccess&) = default;
85     PolicyAccess& operator=(const PolicyAccess&) = default;
86     PolicyAccess(PolicyAccess&&) = default;
87     PolicyAccess& operator=(PolicyAccess&&) = default;
88     ~PolicyAccess() = default;
89 
90     /**
91      * @brief Construct a new PolicyAccess wrapped object.
92      *
93      * @param[in] index - The array index in Policy.
94      * @tparam Args - Forwarded to wrapped type constructor.
95      */
96     template <typename... Args>
97     PolicyAccess(size_t index, Args&&... args) :
98         T(std::forward<Args>(args)...), policy(index)
99     {}
100 
101   private:
102     /**
103      * @brief Get the associated policy.
104      */
105     RedundancyPolicy& getPolicy() override
106     {
107         return *Policy::get()[policy];
108     }
109 
110     /** The associated policy index. */
111     size_t policy;
112 };
113 } // namespace presence
114 } // namespace fan
115 } // namespace phosphor
116