blob: d596dfa8c24fcf500360dcf1098b0f84406ed0ca [file] [log] [blame]
Brad Bishop221b36b2017-06-14 16:52:42 -04001#pragma once
2
3#include "fan.hpp"
4
5namespace phosphor
6{
7namespace fan
8{
9namespace presence
10{
11
Brad Bishop11083ec2017-07-25 19:08:53 -040012class PresenceSensor;
13
Brad Bishop221b36b2017-06-14 16:52:42 -040014/**
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 */
25class RedundancyPolicy
26{
Matthew Barth2d2caa32020-05-26 11:07:24 -050027 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;
Brad Bishop221b36b2017-06-14 16:52:42 -040033
Matthew Barth2d2caa32020-05-26 11:07:24 -050034 /**
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 {}
Brad Bishop221b36b2017-06-14 16:52:42 -040041
Matthew Barth2d2caa32020-05-26 11:07:24 -050042 /**
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;
Brad Bishop221b36b2017-06-14 16:52:42 -040053
Matthew Barth2d2caa32020-05-26 11:07:24 -050054 /**
55 * @brief monitor
56 *
57 * Implementations should start monitoring the sensors
58 * associated with the fan.
59 */
60 virtual void monitor() = 0;
Brad Bishop221b36b2017-06-14 16:52:42 -040061
Matthew Barth2d2caa32020-05-26 11:07:24 -050062 protected:
63 /** @brief Fan name and inventory path. */
64 const Fan& fan;
Brad Bishop221b36b2017-06-14 16:52:42 -040065};
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 */
79template <typename T, typename Policy>
80class PolicyAccess : public T
81{
Matthew Barth2d2caa32020-05-26 11:07:24 -050082 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;
Brad Bishop221b36b2017-06-14 16:52:42 -040089
Matthew Barth2d2caa32020-05-26 11:07:24 -050090 /**
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 {}
Brad Bishop221b36b2017-06-14 16:52:42 -0400100
Matthew Barth2d2caa32020-05-26 11:07:24 -0500101 private:
102 /**
103 * @brief Get the associated policy.
104 */
105 RedundancyPolicy& getPolicy() override
106 {
107 return *Policy::get()[policy];
108 }
Brad Bishop221b36b2017-06-14 16:52:42 -0400109
Matthew Barth2d2caa32020-05-26 11:07:24 -0500110 /** The associated policy index. */
111 size_t policy;
Brad Bishop221b36b2017-06-14 16:52:42 -0400112};
113} // namespace presence
114} // namespace fan
115} // namespace phosphor