Brad Bishop | 00b5208 | 2017-07-25 19:52:22 -0400 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Brad Bishop | 00b5208 | 2017-07-25 19:52:22 -0400 | [diff] [blame] | 3 | #include "fan.hpp" |
Matt Spinler | c65d91d | 2021-04-21 13:09:49 -0500 | [diff] [blame] | 4 | #include "power_state.hpp" |
Brad Bishop | 00b5208 | 2017-07-25 19:52:22 -0400 | [diff] [blame] | 5 | #include "rpolicy.hpp" |
| 6 | |
Matt Spinler | c65d91d | 2021-04-21 13:09:49 -0500 | [diff] [blame] | 7 | #include <sdeventplus/utility/timer.hpp> |
| 8 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 9 | #include <functional> |
| 10 | #include <vector> |
| 11 | |
Brad Bishop | 00b5208 | 2017-07-25 19:52:22 -0400 | [diff] [blame] | 12 | namespace phosphor |
| 13 | { |
| 14 | namespace fan |
| 15 | { |
| 16 | namespace presence |
| 17 | { |
| 18 | |
| 19 | class PresenceSensor; |
| 20 | |
| 21 | /** |
| 22 | * @class AnyOf |
| 23 | * @brief AnyOf redundancy policy. |
| 24 | * |
| 25 | * The any of redundancy policy monitors all sensor |
| 26 | * states in the redundancy set and reports true when any |
| 27 | * sensor in the set reports true. |
| 28 | */ |
| 29 | class AnyOf : public RedundancyPolicy |
| 30 | { |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 31 | public: |
| 32 | AnyOf() = delete; |
| 33 | AnyOf(const AnyOf&) = default; |
| 34 | AnyOf& operator=(const AnyOf&) = default; |
| 35 | AnyOf(AnyOf&&) = default; |
| 36 | AnyOf& operator=(AnyOf&&) = default; |
| 37 | ~AnyOf() = default; |
Brad Bishop | 00b5208 | 2017-07-25 19:52:22 -0400 | [diff] [blame] | 38 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 39 | /** |
| 40 | * @brief Construct an any of bitwise policy. |
| 41 | * |
| 42 | * @param[in] fan - The fan associated with the policy. |
| 43 | * @param[in] s - The set of sensors associated with the policy. |
Matt Spinler | bc4179e | 2022-10-04 15:15:06 -0500 | [diff] [blame^] | 44 | * @param[in] e - EEPROM device instance |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 45 | */ |
| 46 | AnyOf(const Fan& fan, |
Matt Spinler | bc4179e | 2022-10-04 15:15:06 -0500 | [diff] [blame^] | 47 | const std::vector<std::reference_wrapper<PresenceSensor>>& s, |
| 48 | std::unique_ptr<EEPROMDevice> e); |
Brad Bishop | 00b5208 | 2017-07-25 19:52:22 -0400 | [diff] [blame] | 49 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 50 | /** |
| 51 | * @brief stateChanged |
| 52 | * |
| 53 | * Update the inventory and execute the fallback |
| 54 | * policy. |
| 55 | * |
| 56 | * @param[in] present - The new presence state according |
| 57 | * to the specified sensor. |
| 58 | * @param[in] sensor - The sensor reporting the new state. |
| 59 | */ |
| 60 | void stateChanged(bool present, PresenceSensor& sensor) override; |
Brad Bishop | 00b5208 | 2017-07-25 19:52:22 -0400 | [diff] [blame] | 61 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 62 | /** |
| 63 | * @brief monitor |
| 64 | * |
| 65 | * Start monitoring the fan. |
| 66 | */ |
| 67 | void monitor() override; |
Brad Bishop | 00b5208 | 2017-07-25 19:52:22 -0400 | [diff] [blame] | 68 | |
Matthew Barth | 2d2caa3 | 2020-05-26 11:07:24 -0500 | [diff] [blame] | 69 | private: |
Matt Spinler | c65d91d | 2021-04-21 13:09:49 -0500 | [diff] [blame] | 70 | /** |
| 71 | * @brief Checks that the sensors contained in this policy all |
| 72 | * agree on the presence value. If they don't, then call |
| 73 | * logConflict() on the sensors that don't think the fan |
| 74 | * is present as they may be broken. |
| 75 | * |
| 76 | * This check will only happen when power is on. |
| 77 | */ |
| 78 | void checkSensorConflicts(); |
| 79 | |
| 80 | /** |
| 81 | * @brief Callback function called after a post-poweron delay. |
| 82 | * |
| 83 | * The _powerOnDelayTimer is started when _powerState says the |
| 84 | * power is on to give fans a bit of time to spin up so tachs |
| 85 | * wouldn't be zero. This is the callback function for that timer. |
| 86 | * |
| 87 | * It will call checkSensorConflicts(). |
| 88 | */ |
| 89 | void delayedAfterPowerOn(); |
| 90 | |
| 91 | /** |
| 92 | * @brief Says if power is on, though not until the post |
| 93 | * power on delay is complete. |
| 94 | * |
| 95 | * @return bool - if power is on. |
| 96 | */ |
| 97 | inline bool isPowerOn() const |
| 98 | { |
| 99 | return _powerOn; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @brief Called by the PowerState object when the power |
| 104 | * state changes. |
| 105 | * |
| 106 | * When power changes to on: |
| 107 | * - Clears the memory of previous sensor conflicts. |
| 108 | * - Starts the post power on delay timer. |
| 109 | * |
| 110 | * @param[in] powerOn - If power is now on or off. |
| 111 | */ |
| 112 | void powerStateChanged(bool powerOn); |
| 113 | |
| 114 | static constexpr size_t sensorPos = 0; |
| 115 | static constexpr size_t presentPos = 1; |
| 116 | static constexpr size_t conflictPos = 2; |
| 117 | |
| 118 | /** |
| 119 | * @brief All presence sensors in the redundancy set. |
| 120 | * |
| 121 | * Each entry contains: |
| 122 | * - A reference to a PresenceSensor |
| 123 | * - The current presence state |
| 124 | * - If the sensors have logged conflicts in their answers. |
| 125 | */ |
| 126 | std::vector<std::tuple<std::reference_wrapper<PresenceSensor>, bool, bool>> |
| 127 | state; |
| 128 | |
| 129 | /** |
| 130 | * @brief Pointer to the PowerState object used to track power changes. |
| 131 | */ |
| 132 | std::shared_ptr<PowerState> _powerState; |
| 133 | |
| 134 | /** |
| 135 | * @brief Post power on delay timer, where the conflict checking code |
| 136 | * doesn't consider power on until this timer expires. |
| 137 | * |
| 138 | * This gives fans a chance to start spinning before checking them. |
| 139 | */ |
| 140 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> |
| 141 | _powerOnDelayTimer; |
| 142 | |
| 143 | /** |
| 144 | * @brief Current power state. |
| 145 | */ |
| 146 | bool _powerOn; |
Brad Bishop | 00b5208 | 2017-07-25 19:52:22 -0400 | [diff] [blame] | 147 | }; |
| 148 | |
| 149 | } // namespace presence |
| 150 | } // namespace fan |
| 151 | } // namespace phosphor |