100b52082SBrad Bishop #pragma once 200b52082SBrad Bishop 300b52082SBrad Bishop #include "fan.hpp" 4c65d91d6SMatt Spinler #include "power_state.hpp" 500b52082SBrad Bishop #include "rpolicy.hpp" 600b52082SBrad Bishop 7c65d91d6SMatt Spinler #include <sdeventplus/utility/timer.hpp> 8c65d91d6SMatt Spinler 92d2caa34SMatthew Barth #include <functional> 102d2caa34SMatthew Barth #include <vector> 112d2caa34SMatthew Barth 1200b52082SBrad Bishop namespace phosphor 1300b52082SBrad Bishop { 1400b52082SBrad Bishop namespace fan 1500b52082SBrad Bishop { 1600b52082SBrad Bishop namespace presence 1700b52082SBrad Bishop { 1800b52082SBrad Bishop 1900b52082SBrad Bishop class PresenceSensor; 2000b52082SBrad Bishop 2100b52082SBrad Bishop /** 2200b52082SBrad Bishop * @class AnyOf 2300b52082SBrad Bishop * @brief AnyOf redundancy policy. 2400b52082SBrad Bishop * 2500b52082SBrad Bishop * The any of redundancy policy monitors all sensor 2600b52082SBrad Bishop * states in the redundancy set and reports true when any 2700b52082SBrad Bishop * sensor in the set reports true. 2800b52082SBrad Bishop */ 2900b52082SBrad Bishop class AnyOf : public RedundancyPolicy 3000b52082SBrad Bishop { 3100b52082SBrad Bishop public: 3200b52082SBrad Bishop AnyOf() = delete; 3300b52082SBrad Bishop AnyOf(const AnyOf&) = default; 3400b52082SBrad Bishop AnyOf& operator=(const AnyOf&) = default; 3500b52082SBrad Bishop AnyOf(AnyOf&&) = default; 3600b52082SBrad Bishop AnyOf& operator=(AnyOf&&) = default; 3700b52082SBrad Bishop ~AnyOf() = default; 3800b52082SBrad Bishop 3900b52082SBrad Bishop /** 4000b52082SBrad Bishop * @brief Construct an any of bitwise policy. 4100b52082SBrad Bishop * 4200b52082SBrad Bishop * @param[in] fan - The fan associated with the policy. 4300b52082SBrad Bishop * @param[in] s - The set of sensors associated with the policy. 44bc4179e9SMatt Spinler * @param[in] e - EEPROM device instance 4500b52082SBrad Bishop */ 462d2caa34SMatthew Barth AnyOf(const Fan& fan, 47bc4179e9SMatt Spinler const std::vector<std::reference_wrapper<PresenceSensor>>& s, 48bc4179e9SMatt Spinler std::unique_ptr<EEPROMDevice> e); 4900b52082SBrad Bishop 5000b52082SBrad Bishop /** 51*08bc72f2SMatt Spinler * @brief Construct an any of bitwise policy. 52*08bc72f2SMatt Spinler * 53*08bc72f2SMatt Spinler * @param[in] fan - The fan associated with the policy. 54*08bc72f2SMatt Spinler * @param[in] s - The set of sensors associated with the policy. 55*08bc72f2SMatt Spinler */ AnyOf(const Fan & fan,const std::vector<std::reference_wrapper<PresenceSensor>> & s)56*08bc72f2SMatt Spinler AnyOf(const Fan& fan, 57*08bc72f2SMatt Spinler const std::vector<std::reference_wrapper<PresenceSensor>>& s) : 58*08bc72f2SMatt Spinler AnyOf(fan, s, nullptr) 59*08bc72f2SMatt Spinler {} 60*08bc72f2SMatt Spinler 61*08bc72f2SMatt Spinler /** 6200b52082SBrad Bishop * @brief stateChanged 6300b52082SBrad Bishop * 6400b52082SBrad Bishop * Update the inventory and execute the fallback 6500b52082SBrad Bishop * policy. 6600b52082SBrad Bishop * 6700b52082SBrad Bishop * @param[in] present - The new presence state according 6800b52082SBrad Bishop * to the specified sensor. 6900b52082SBrad Bishop * @param[in] sensor - The sensor reporting the new state. 7000b52082SBrad Bishop */ 7100b52082SBrad Bishop void stateChanged(bool present, PresenceSensor& sensor) override; 7200b52082SBrad Bishop 7300b52082SBrad Bishop /** 7400b52082SBrad Bishop * @brief monitor 7500b52082SBrad Bishop * 7600b52082SBrad Bishop * Start monitoring the fan. 7700b52082SBrad Bishop */ 7800b52082SBrad Bishop void monitor() override; 7900b52082SBrad Bishop 8000b52082SBrad Bishop private: 81c65d91d6SMatt Spinler /** 82c65d91d6SMatt Spinler * @brief Checks that the sensors contained in this policy all 83c65d91d6SMatt Spinler * agree on the presence value. If they don't, then call 84c65d91d6SMatt Spinler * logConflict() on the sensors that don't think the fan 85c65d91d6SMatt Spinler * is present as they may be broken. 86c65d91d6SMatt Spinler * 87c65d91d6SMatt Spinler * This check will only happen when power is on. 88c65d91d6SMatt Spinler */ 89c65d91d6SMatt Spinler void checkSensorConflicts(); 90c65d91d6SMatt Spinler 91c65d91d6SMatt Spinler /** 92c65d91d6SMatt Spinler * @brief Callback function called after a post-poweron delay. 93c65d91d6SMatt Spinler * 94c65d91d6SMatt Spinler * The _powerOnDelayTimer is started when _powerState says the 95c65d91d6SMatt Spinler * power is on to give fans a bit of time to spin up so tachs 96c65d91d6SMatt Spinler * wouldn't be zero. This is the callback function for that timer. 97c65d91d6SMatt Spinler * 98c65d91d6SMatt Spinler * It will call checkSensorConflicts(). 99c65d91d6SMatt Spinler */ 100c65d91d6SMatt Spinler void delayedAfterPowerOn(); 101c65d91d6SMatt Spinler 102c65d91d6SMatt Spinler /** 103c65d91d6SMatt Spinler * @brief Says if power is on, though not until the post 104c65d91d6SMatt Spinler * power on delay is complete. 105c65d91d6SMatt Spinler * 106c65d91d6SMatt Spinler * @return bool - if power is on. 107c65d91d6SMatt Spinler */ isPowerOn() const108c65d91d6SMatt Spinler inline bool isPowerOn() const 109c65d91d6SMatt Spinler { 110c65d91d6SMatt Spinler return _powerOn; 111c65d91d6SMatt Spinler } 112c65d91d6SMatt Spinler 113c65d91d6SMatt Spinler /** 114c65d91d6SMatt Spinler * @brief Called by the PowerState object when the power 115c65d91d6SMatt Spinler * state changes. 116c65d91d6SMatt Spinler * 117c65d91d6SMatt Spinler * When power changes to on: 118c65d91d6SMatt Spinler * - Clears the memory of previous sensor conflicts. 119c65d91d6SMatt Spinler * - Starts the post power on delay timer. 120c65d91d6SMatt Spinler * 121c65d91d6SMatt Spinler * @param[in] powerOn - If power is now on or off. 122c65d91d6SMatt Spinler */ 123c65d91d6SMatt Spinler void powerStateChanged(bool powerOn); 124c65d91d6SMatt Spinler 125c65d91d6SMatt Spinler static constexpr size_t sensorPos = 0; 126c65d91d6SMatt Spinler static constexpr size_t presentPos = 1; 127c65d91d6SMatt Spinler static constexpr size_t conflictPos = 2; 128c65d91d6SMatt Spinler 129c65d91d6SMatt Spinler /** 130c65d91d6SMatt Spinler * @brief All presence sensors in the redundancy set. 131c65d91d6SMatt Spinler * 132c65d91d6SMatt Spinler * Each entry contains: 133c65d91d6SMatt Spinler * - A reference to a PresenceSensor 134c65d91d6SMatt Spinler * - The current presence state 135c65d91d6SMatt Spinler * - If the sensors have logged conflicts in their answers. 136c65d91d6SMatt Spinler */ 137c65d91d6SMatt Spinler std::vector<std::tuple<std::reference_wrapper<PresenceSensor>, bool, bool>> 138c65d91d6SMatt Spinler state; 139c65d91d6SMatt Spinler 140c65d91d6SMatt Spinler /** 141c65d91d6SMatt Spinler * @brief Pointer to the PowerState object used to track power changes. 142c65d91d6SMatt Spinler */ 143c65d91d6SMatt Spinler std::shared_ptr<PowerState> _powerState; 144c65d91d6SMatt Spinler 145c65d91d6SMatt Spinler /** 146c65d91d6SMatt Spinler * @brief Post power on delay timer, where the conflict checking code 147c65d91d6SMatt Spinler * doesn't consider power on until this timer expires. 148c65d91d6SMatt Spinler * 149c65d91d6SMatt Spinler * This gives fans a chance to start spinning before checking them. 150c65d91d6SMatt Spinler */ 151c65d91d6SMatt Spinler sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> 152c65d91d6SMatt Spinler _powerOnDelayTimer; 153c65d91d6SMatt Spinler 154c65d91d6SMatt Spinler /** 155c65d91d6SMatt Spinler * @brief Current power state. 156c65d91d6SMatt Spinler */ 157c65d91d6SMatt Spinler bool _powerOn; 15800b52082SBrad Bishop }; 15900b52082SBrad Bishop 16000b52082SBrad Bishop } // namespace presence 16100b52082SBrad Bishop } // namespace fan 16200b52082SBrad Bishop } // namespace phosphor 163