Brad Bishop | 221b36b | 2017-06-14 16:52:42 -0400 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Matt Spinler | bc4179e | 2022-10-04 15:15:06 -0500 | [diff] [blame] | 3 | #include "eeprom_device.hpp" |
Brad Bishop | 221b36b | 2017-06-14 16:52:42 -0400 | [diff] [blame] | 4 | #include "fan.hpp" |
| 5 | |
| 6 | namespace phosphor |
| 7 | { |
| 8 | namespace fan |
| 9 | { |
| 10 | namespace presence |
| 11 | { |
| 12 | |
Brad Bishop | 11083ec | 2017-07-25 19:08:53 -0400 | [diff] [blame] | 13 | class PresenceSensor; |
| 14 | |
Brad Bishop | 221b36b | 2017-06-14 16:52:42 -0400 | [diff] [blame] | 15 | /** |
| 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 | */ |
| 26 | class RedundancyPolicy |
| 27 | { |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 28 | 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 Bishop | 221b36b | 2017-06-14 16:52:42 -0400 | [diff] [blame] | 34 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 35 | /** |
| 36 | * @brief Construct a new Redundancy Policy. |
| 37 | * |
| 38 | * @param[in] fan - The fan associated with this policy. |
Matt Spinler | bc4179e | 2022-10-04 15:15:06 -0500 | [diff] [blame] | 39 | * @param[in] eeprom - EEPROM device instance |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 40 | */ |
Matt Spinler | bc4179e | 2022-10-04 15:15:06 -0500 | [diff] [blame] | 41 | explicit RedundancyPolicy(const Fan& f, |
| 42 | std::unique_ptr<EEPROMDevice> eeprom) : |
| 43 | fan(f), |
| 44 | eepromDevice(std::move(eeprom)) |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 45 | {} |
Brad Bishop | 221b36b | 2017-06-14 16:52:42 -0400 | [diff] [blame] | 46 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 47 | /** |
| 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 Bishop | 221b36b | 2017-06-14 16:52:42 -0400 | [diff] [blame] | 58 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 59 | /** |
| 60 | * @brief monitor |
| 61 | * |
| 62 | * Implementations should start monitoring the sensors |
| 63 | * associated with the fan. |
| 64 | */ |
| 65 | virtual void monitor() = 0; |
Brad Bishop | 221b36b | 2017-06-14 16:52:42 -0400 | [diff] [blame] | 66 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 67 | protected: |
| 68 | /** @brief Fan name and inventory path. */ |
| 69 | const Fan& fan; |
Matt Spinler | bc4179e | 2022-10-04 15:15:06 -0500 | [diff] [blame] | 70 | |
| 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 Bishop | 221b36b | 2017-06-14 16:52:42 -0400 | [diff] [blame] | 76 | }; |
| 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 | */ |
| 90 | template <typename T, typename Policy> |
| 91 | class PolicyAccess : public T |
| 92 | { |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 93 | 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 Bishop | 221b36b | 2017-06-14 16:52:42 -0400 | [diff] [blame] | 100 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 101 | /** |
| 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 Bishop | 221b36b | 2017-06-14 16:52:42 -0400 | [diff] [blame] | 111 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 112 | private: |
| 113 | /** |
| 114 | * @brief Get the associated policy. |
| 115 | */ |
| 116 | RedundancyPolicy& getPolicy() override |
| 117 | { |
| 118 | return *Policy::get()[policy]; |
| 119 | } |
Brad Bishop | 221b36b | 2017-06-14 16:52:42 -0400 | [diff] [blame] | 120 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 121 | /** The associated policy index. */ |
| 122 | size_t policy; |
Brad Bishop | 221b36b | 2017-06-14 16:52:42 -0400 | [diff] [blame] | 123 | }; |
| 124 | } // namespace presence |
| 125 | } // namespace fan |
| 126 | } // namespace phosphor |