blob: e190029aa40200ad8f675f307092e6cbe616baeb [file] [log] [blame]
Brad Bishop221b36b2017-06-14 16:52:42 -04001#pragma once
2
Matt Spinlerbc4179e2022-10-04 15:15:06 -05003#include "eeprom_device.hpp"
Brad Bishop221b36b2017-06-14 16:52:42 -04004#include "fan.hpp"
5
6namespace phosphor
7{
8namespace fan
9{
10namespace presence
11{
12
Brad Bishop11083ec2017-07-25 19:08:53 -040013class PresenceSensor;
14
Brad Bishop221b36b2017-06-14 16:52:42 -040015/**
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 */
26class RedundancyPolicy
27{
Matthew Barth2d2caa32020-05-26 11:07:24 -050028 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;
Brad Bishop221b36b2017-06-14 16:52:42 -040034
Matthew Barth2d2caa32020-05-26 11:07:24 -050035 /**
36 * @brief Construct a new Redundancy Policy.
37 *
38 * @param[in] fan - The fan associated with this policy.
Matt Spinlerbc4179e2022-10-04 15:15:06 -050039 * @param[in] eeprom - EEPROM device instance
Matthew Barth2d2caa32020-05-26 11:07:24 -050040 */
Matt Spinlerbc4179e2022-10-04 15:15:06 -050041 explicit RedundancyPolicy(const Fan& f,
42 std::unique_ptr<EEPROMDevice> eeprom) :
43 fan(f),
44 eepromDevice(std::move(eeprom))
Matthew Barth2d2caa32020-05-26 11:07:24 -050045 {}
Brad Bishop221b36b2017-06-14 16:52:42 -040046
Matthew Barth2d2caa32020-05-26 11:07:24 -050047 /**
48 * @brief stateChanged
49 *
50 * Typically invoked by presence sensors to signify
51 * a change of presence. Implementations should update
52 * the inventory and execute their policy logic.
53 *
54 * @param[in] present - The new state of the sensor.
55 * @param[in] sensor - The sensor that changed state.
56 */
57 virtual void stateChanged(bool present, PresenceSensor& sensor) = 0;
Brad Bishop221b36b2017-06-14 16:52:42 -040058
Matthew Barth2d2caa32020-05-26 11:07:24 -050059 /**
60 * @brief monitor
61 *
62 * Implementations should start monitoring the sensors
63 * associated with the fan.
64 */
65 virtual void monitor() = 0;
Brad Bishop221b36b2017-06-14 16:52:42 -040066
Matthew Barth2d2caa32020-05-26 11:07:24 -050067 protected:
68 /** @brief Fan name and inventory path. */
69 const Fan& fan;
Matt Spinlerbc4179e2022-10-04 15:15:06 -050070
71 /**
72 * @brief Handles binding the EEPROM driver on fan plug
73 * if configured to do so.
74 */
75 std::unique_ptr<EEPROMDevice> eepromDevice;
Brad Bishop221b36b2017-06-14 16:52:42 -040076};
77
78/**
79 * @class PolicyAccess
80 * @brief Policy association.
81 *
82 * PolicyAccess can be used to associate a redundancy policy
83 * with something else.
84 *
85 * Wrap the type to be associated with a policy with PolicyAccess.
86 *
87 * @tparam T - The type to associate with a redundancy policy.
88 * @tparam Policy - An array type where the policy is stored.
89 */
90template <typename T, typename Policy>
91class PolicyAccess : public T
92{
Matthew Barth2d2caa32020-05-26 11:07:24 -050093 public:
94 PolicyAccess() = default;
95 PolicyAccess(const PolicyAccess&) = default;
96 PolicyAccess& operator=(const PolicyAccess&) = default;
97 PolicyAccess(PolicyAccess&&) = default;
98 PolicyAccess& operator=(PolicyAccess&&) = default;
99 ~PolicyAccess() = default;
Brad Bishop221b36b2017-06-14 16:52:42 -0400100
Matthew Barth2d2caa32020-05-26 11:07:24 -0500101 /**
102 * @brief Construct a new PolicyAccess wrapped object.
103 *
104 * @param[in] index - The array index in Policy.
105 * @tparam Args - Forwarded to wrapped type constructor.
106 */
107 template <typename... Args>
108 PolicyAccess(size_t index, Args&&... args) :
109 T(std::forward<Args>(args)...), policy(index)
110 {}
Brad Bishop221b36b2017-06-14 16:52:42 -0400111
Matthew Barth2d2caa32020-05-26 11:07:24 -0500112 private:
113 /**
114 * @brief Get the associated policy.
115 */
116 RedundancyPolicy& getPolicy() override
117 {
118 return *Policy::get()[policy];
119 }
Brad Bishop221b36b2017-06-14 16:52:42 -0400120
Matthew Barth2d2caa32020-05-26 11:07:24 -0500121 /** The associated policy index. */
122 size_t policy;
Brad Bishop221b36b2017-06-14 16:52:42 -0400123};
124} // namespace presence
125} // namespace fan
126} // namespace phosphor