xref: /openbmc/phosphor-fan-presence/monitor/fan.hpp (revision 6ad28430764b7d16ee7abcb9965b76ea5583008f)
1abf8da36SMatt Spinler #pragma once
2abf8da36SMatt Spinler 
3abf8da36SMatt Spinler #include <sdbusplus/bus.hpp>
4abf8da36SMatt Spinler #include <tuple>
5abf8da36SMatt Spinler #include <vector>
6e824f985SMatt Spinler #include "event.hpp"
7abf8da36SMatt Spinler #include "tach_sensor.hpp"
8abf8da36SMatt Spinler #include "types.hpp"
9abf8da36SMatt Spinler 
10abf8da36SMatt Spinler namespace phosphor
11abf8da36SMatt Spinler {
12abf8da36SMatt Spinler namespace fan
13abf8da36SMatt Spinler {
14abf8da36SMatt Spinler namespace monitor
15abf8da36SMatt Spinler {
16abf8da36SMatt Spinler 
17edaeb31cSBrad Bishop /**
18*6ad28430SMatthew Barth  * The mode fan monitor will run in:
19*6ad28430SMatthew Barth  *   - init - only do the initialization steps
20*6ad28430SMatthew Barth  *   - monitor - run normal monitoring algorithm
21*6ad28430SMatthew Barth  */
22*6ad28430SMatthew Barth enum class Mode
23*6ad28430SMatthew Barth {
24*6ad28430SMatthew Barth     init,
25*6ad28430SMatthew Barth     monitor
26*6ad28430SMatthew Barth };
27*6ad28430SMatthew Barth 
28*6ad28430SMatthew Barth /**
29edaeb31cSBrad Bishop  * @class InvalidSensorError
30edaeb31cSBrad Bishop  *
31edaeb31cSBrad Bishop  * An exception type for sensors that don't exist or
32edaeb31cSBrad Bishop  * are otherwise inaccessible.
33edaeb31cSBrad Bishop  */
34edaeb31cSBrad Bishop class InvalidSensorError : public std::exception {};
35abf8da36SMatt Spinler 
36abf8da36SMatt Spinler /**
37abf8da36SMatt Spinler  * @class Fan
38abf8da36SMatt Spinler  *
39abf8da36SMatt Spinler  * Represents a fan, which can contain 1 or more sensors which
40abf8da36SMatt Spinler  * loosely correspond to rotors.  See below.
41abf8da36SMatt Spinler  *
42abf8da36SMatt Spinler  * There is a sensor when hwmon exposes one, which means there is a
43abf8da36SMatt Spinler  * speed value to be read.  Sometimes there is a sensor per rotor,
44abf8da36SMatt Spinler  * and other times multiple rotors just use 1 sensor total where
45abf8da36SMatt Spinler  * the sensor reports the slowest speed of all of the rotors.
46abf8da36SMatt Spinler  *
47abf8da36SMatt Spinler  * A rotor's speed is set by writing the Target value of a sensor.
48abf8da36SMatt Spinler  * Sometimes each sensor in a fan supports having a Target, and other
49abf8da36SMatt Spinler  * times not all of them do.  A TachSensor object knows if it supports
50abf8da36SMatt Spinler  * the Target property.
51abf8da36SMatt Spinler  *
52abf8da36SMatt Spinler  * The strategy for monitoring fan speeds is as follows:
53abf8da36SMatt Spinler  *
54abf8da36SMatt Spinler  * Every time a Target (new speed written) or Input (actual speed read)
55abf8da36SMatt Spinler  * sensor changes, check if the input value is within some range of the target
56abf8da36SMatt Spinler  * value.  If it isn't, start a timer at the end of which the sensor will be
57abf8da36SMatt Spinler  * set to not functional.  If enough sensors in the fan are now nonfunctional,
58abf8da36SMatt Spinler  * set the whole fan to nonfunctional in the inventory.
59abf8da36SMatt Spinler  *
60abf8da36SMatt Spinler  * When sensor inputs come back within a specified range of the target,
61abf8da36SMatt Spinler  * stop its timer if running, make the sensor functional again if it wasn't,
62abf8da36SMatt Spinler  * and if enough sensors in the fan are now functional set the whole fan
63abf8da36SMatt Spinler  * back to functional in the inventory.
64abf8da36SMatt Spinler  */
65abf8da36SMatt Spinler class Fan
66abf8da36SMatt Spinler {
67b1e18514SMatt Spinler     using Property = std::string;
68b1e18514SMatt Spinler     using Value = sdbusplus::message::variant<bool>;
69b1e18514SMatt Spinler     using PropertyMap = std::map<Property, Value>;
70b1e18514SMatt Spinler 
71b1e18514SMatt Spinler     using Interface = std::string;
72b1e18514SMatt Spinler     using InterfaceMap = std::map<Interface, PropertyMap>;
73b1e18514SMatt Spinler 
74b1e18514SMatt Spinler     using Object = sdbusplus::message::object_path;
75b1e18514SMatt Spinler     using ObjectMap = std::map<Object, InterfaceMap>;
76abf8da36SMatt Spinler 
77abf8da36SMatt Spinler     public:
78abf8da36SMatt Spinler 
79abf8da36SMatt Spinler         Fan() = delete;
80abf8da36SMatt Spinler         Fan(const Fan&) = delete;
81abf8da36SMatt Spinler         Fan(Fan&&) = default;
82abf8da36SMatt Spinler         Fan& operator=(const Fan&) = delete;
83abf8da36SMatt Spinler         Fan& operator=(Fan&&) = default;
84abf8da36SMatt Spinler         ~Fan() = default;
85abf8da36SMatt Spinler 
86abf8da36SMatt Spinler         /**
87abf8da36SMatt Spinler          * @brief Constructor
88abf8da36SMatt Spinler          *
89*6ad28430SMatthew Barth          * @param mode - mode of fan monitor
90abf8da36SMatt Spinler          * @param bus - the dbus object
91abf8da36SMatt Spinler          * @param events - pointer to sd_event object
92abf8da36SMatt Spinler          * @param def - the fan definition structure
93abf8da36SMatt Spinler          */
94*6ad28430SMatthew Barth         Fan(Mode mode,
95*6ad28430SMatthew Barth             sdbusplus::bus::bus& bus,
96e824f985SMatt Spinler             phosphor::fan::event::EventPtr& events,
97abf8da36SMatt Spinler             const FanDefinition& def);
98abf8da36SMatt Spinler 
99ebaae611SMatt Spinler         /**
100ebaae611SMatt Spinler          * @brief Callback function for when an input sensor changes
101ebaae611SMatt Spinler          *
102ebaae611SMatt Spinler          * Starts a timer, where if it expires then the sensor
103a9406a77SMatt Spinler          * was out of range for too long and can be considered not functional.
104ebaae611SMatt Spinler          */
105ebaae611SMatt Spinler         void tachChanged(TachSensor& sensor);
106ebaae611SMatt Spinler 
107ebaae611SMatt Spinler         /**
108ebaae611SMatt Spinler          * @brief Calls tachChanged(sensor) on each sensor
109ebaae611SMatt Spinler          */
110ebaae611SMatt Spinler         void tachChanged();
111abf8da36SMatt Spinler 
112a9406a77SMatt Spinler         /**
113a9406a77SMatt Spinler          * @brief The callback function for the timer
114a9406a77SMatt Spinler          *
115a9406a77SMatt Spinler          * Sets the sensor to not functional.
116a9406a77SMatt Spinler          * If enough sensors are now not functional,
117a9406a77SMatt Spinler          * updates the functional status of the whole
118a9406a77SMatt Spinler          * fan in the inventory.
119a9406a77SMatt Spinler          *
120a9406a77SMatt Spinler          * @param[in] sensor - the sensor whose timer expired
121a9406a77SMatt Spinler          */
122a9406a77SMatt Spinler         void timerExpired(TachSensor& sensor);
123a9406a77SMatt Spinler 
124abf8da36SMatt Spinler     private:
125abf8da36SMatt Spinler 
126abf8da36SMatt Spinler         /**
127abf8da36SMatt Spinler          * @brief Returns the target speed of the sensor
128abf8da36SMatt Spinler          *
129abf8da36SMatt Spinler          * If the sensor itself doesn't have a target, it finds
130abf8da36SMatt Spinler          * the target speed from another sensor.
131abf8da36SMatt Spinler          *
132abf8da36SMatt Spinler          * @param[in] sensor - the sensor to get the target speed for
133abf8da36SMatt Spinler          */
134abf8da36SMatt Spinler         uint64_t getTargetSpeed(const TachSensor& sensor);
135abf8da36SMatt Spinler 
136abf8da36SMatt Spinler         /**
137abf8da36SMatt Spinler          * @brief Returns true if the sensor input is not within
138abf8da36SMatt Spinler          * some deviation of the target.
139abf8da36SMatt Spinler          *
140abf8da36SMatt Spinler          * @param[in] sensor - the sensor to check
141abf8da36SMatt Spinler          */
142abf8da36SMatt Spinler         bool outOfRange(const TachSensor& sensor);
143abf8da36SMatt Spinler 
144abf8da36SMatt Spinler         /**
145abf8da36SMatt Spinler          * @brief Returns true if too many sensors are nonfunctional
146abf8da36SMatt Spinler          *        as defined by _numSensorFailsForNonFunc
147abf8da36SMatt Spinler          */
148abf8da36SMatt Spinler         bool tooManySensorsNonfunctional();
149abf8da36SMatt Spinler 
150b1e18514SMatt Spinler         /**
151b1e18514SMatt Spinler          * @brief Updates the Functional property in the inventory
152b1e18514SMatt Spinler          *        for the fan based on the value passed in.
153b1e18514SMatt Spinler          *
154b1e18514SMatt Spinler          * @param[in] functional - If the Functional property should
155b1e18514SMatt Spinler          *                         be set to true or false.
156b1e18514SMatt Spinler          */
157b1e18514SMatt Spinler         void updateInventory(bool functional);
158b1e18514SMatt Spinler 
159b1e18514SMatt Spinler         /**
160b1e18514SMatt Spinler          * @brief Returns the object map to use when updating the inventory
161b1e18514SMatt Spinler          *
162b1e18514SMatt Spinler          * @param[in] functional - If the Functional property should
163b1e18514SMatt Spinler          *                         be set to true or false.
164b1e18514SMatt Spinler          */
165b1e18514SMatt Spinler         ObjectMap getObjectMap(bool functional);
166abf8da36SMatt Spinler 
167abf8da36SMatt Spinler         /**
168abf8da36SMatt Spinler          * @brief the dbus object
169abf8da36SMatt Spinler          */
170abf8da36SMatt Spinler         sdbusplus::bus::bus& _bus;
171abf8da36SMatt Spinler 
172abf8da36SMatt Spinler         /**
173abf8da36SMatt Spinler          * @brief The inventory name of the fan
174abf8da36SMatt Spinler          */
175abf8da36SMatt Spinler         const std::string _name;
176abf8da36SMatt Spinler 
177abf8da36SMatt Spinler         /**
178abf8da36SMatt Spinler          * @brief The percentage that the input speed must be below
179abf8da36SMatt Spinler          *        the target speed to be considered an error.
180abf8da36SMatt Spinler          *        Between 0 and 100.
181abf8da36SMatt Spinler          */
182abf8da36SMatt Spinler         const size_t _deviation;
183abf8da36SMatt Spinler 
184abf8da36SMatt Spinler         /**
185abf8da36SMatt Spinler          * The number of sensors that must be nonfunctional at the
186abf8da36SMatt Spinler          * same time in order for the fan to be set to nonfunctional
187abf8da36SMatt Spinler          * in the inventory.
188abf8da36SMatt Spinler          */
189abf8da36SMatt Spinler         const size_t _numSensorFailsForNonFunc;
190abf8da36SMatt Spinler 
191abf8da36SMatt Spinler         /**
192abf8da36SMatt Spinler          * @brief The current functional state of the fan
193abf8da36SMatt Spinler          */
194abf8da36SMatt Spinler         bool _functional = true;
195abf8da36SMatt Spinler 
196abf8da36SMatt Spinler         /**
197abf8da36SMatt Spinler          * The sensor objects for the fan
198abf8da36SMatt Spinler          */
199abf8da36SMatt Spinler         std::vector<std::unique_ptr<TachSensor>> _sensors;
200abf8da36SMatt Spinler };
201abf8da36SMatt Spinler 
202abf8da36SMatt Spinler }
203abf8da36SMatt Spinler }
204abf8da36SMatt Spinler }
205