blob: 259a31bdcfcc19e938212b8cdaf7c6b910628820 [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) :
Patrick Williamsdfddd642024-08-16 15:21:51 -040043 fan(f), eepromDevice(std::move(eeprom))
Matthew Barth2d2caa32020-05-26 11:07:24 -050044 {}
Brad Bishop221b36b2017-06-14 16:52:42 -040045
Matthew Barth2d2caa32020-05-26 11:07:24 -050046 /**
47 * @brief stateChanged
48 *
49 * Typically invoked by presence sensors to signify
50 * a change of presence. Implementations should update
51 * the inventory and execute their policy logic.
52 *
53 * @param[in] present - The new state of the sensor.
54 * @param[in] sensor - The sensor that changed state.
55 */
56 virtual void stateChanged(bool present, PresenceSensor& sensor) = 0;
Brad Bishop221b36b2017-06-14 16:52:42 -040057
Matthew Barth2d2caa32020-05-26 11:07:24 -050058 /**
59 * @brief monitor
60 *
61 * Implementations should start monitoring the sensors
62 * associated with the fan.
63 */
64 virtual void monitor() = 0;
Brad Bishop221b36b2017-06-14 16:52:42 -040065
Matthew Barth2d2caa32020-05-26 11:07:24 -050066 protected:
67 /** @brief Fan name and inventory path. */
68 const Fan& fan;
Matt Spinlerbc4179e2022-10-04 15:15:06 -050069
70 /**
71 * @brief Handles binding the EEPROM driver on fan plug
72 * if configured to do so.
73 */
74 std::unique_ptr<EEPROMDevice> eepromDevice;
Brad Bishop221b36b2017-06-14 16:52:42 -040075};
76
77/**
78 * @class PolicyAccess
79 * @brief Policy association.
80 *
81 * PolicyAccess can be used to associate a redundancy policy
82 * with something else.
83 *
84 * Wrap the type to be associated with a policy with PolicyAccess.
85 *
86 * @tparam T - The type to associate with a redundancy policy.
87 * @tparam Policy - An array type where the policy is stored.
88 */
89template <typename T, typename Policy>
90class PolicyAccess : public T
91{
Matthew Barth2d2caa32020-05-26 11:07:24 -050092 public:
93 PolicyAccess() = default;
94 PolicyAccess(const PolicyAccess&) = default;
95 PolicyAccess& operator=(const PolicyAccess&) = default;
96 PolicyAccess(PolicyAccess&&) = default;
97 PolicyAccess& operator=(PolicyAccess&&) = default;
98 ~PolicyAccess() = default;
Brad Bishop221b36b2017-06-14 16:52:42 -040099
Matthew Barth2d2caa32020-05-26 11:07:24 -0500100 /**
101 * @brief Construct a new PolicyAccess wrapped object.
102 *
103 * @param[in] index - The array index in Policy.
104 * @tparam Args - Forwarded to wrapped type constructor.
105 */
106 template <typename... Args>
107 PolicyAccess(size_t index, Args&&... args) :
108 T(std::forward<Args>(args)...), policy(index)
109 {}
Brad Bishop221b36b2017-06-14 16:52:42 -0400110
Matthew Barth2d2caa32020-05-26 11:07:24 -0500111 private:
112 /**
113 * @brief Get the associated policy.
114 */
115 RedundancyPolicy& getPolicy() override
116 {
117 return *Policy::get()[policy];
118 }
Brad Bishop221b36b2017-06-14 16:52:42 -0400119
Matthew Barth2d2caa32020-05-26 11:07:24 -0500120 /** The associated policy index. */
121 size_t policy;
Brad Bishop221b36b2017-06-14 16:52:42 -0400122};
123} // namespace presence
124} // namespace fan
125} // namespace phosphor