blob: 03fd1a4b1e0c89c852817d7632d7b827fedc9c06 [file] [log] [blame]
Brad Bishop00b52082017-07-25 19:52:22 -04001#pragma once
2
Brad Bishop00b52082017-07-25 19:52:22 -04003#include "fan.hpp"
Matt Spinlerc65d91d2021-04-21 13:09:49 -05004#include "power_state.hpp"
Brad Bishop00b52082017-07-25 19:52:22 -04005#include "rpolicy.hpp"
6
Matt Spinlerc65d91d2021-04-21 13:09:49 -05007#include <sdeventplus/utility/timer.hpp>
8
Matthew Barth2d2caa32020-05-26 11:07:24 -05009#include <functional>
10#include <vector>
11
Brad Bishop00b52082017-07-25 19:52:22 -040012namespace phosphor
13{
14namespace fan
15{
16namespace presence
17{
18
19class 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 */
29class AnyOf : public RedundancyPolicy
30{
Matthew Barth2d2caa32020-05-26 11:07:24 -050031 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 Bishop00b52082017-07-25 19:52:22 -040038
Matthew Barth2d2caa32020-05-26 11:07:24 -050039 /**
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 Spinlerbc4179e2022-10-04 15:15:06 -050044 * @param[in] e - EEPROM device instance
Matthew Barth2d2caa32020-05-26 11:07:24 -050045 */
46 AnyOf(const Fan& fan,
Matt Spinlerbc4179e2022-10-04 15:15:06 -050047 const std::vector<std::reference_wrapper<PresenceSensor>>& s,
48 std::unique_ptr<EEPROMDevice> e);
Brad Bishop00b52082017-07-25 19:52:22 -040049
Matthew Barth2d2caa32020-05-26 11:07:24 -050050 /**
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 Bishop00b52082017-07-25 19:52:22 -040061
Matthew Barth2d2caa32020-05-26 11:07:24 -050062 /**
63 * @brief monitor
64 *
65 * Start monitoring the fan.
66 */
67 void monitor() override;
Brad Bishop00b52082017-07-25 19:52:22 -040068
Matthew Barth2d2caa32020-05-26 11:07:24 -050069 private:
Matt Spinlerc65d91d2021-04-21 13:09:49 -050070 /**
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 Bishop00b52082017-07-25 19:52:22 -0400147};
148
149} // namespace presence
150} // namespace fan
151} // namespace phosphor