xref: /openbmc/phosphor-fan-presence/monitor/fan.hpp (revision 69f2f48eb6708c7318e47f287fea1b28d9ec9ba7)
1abf8da36SMatt Spinler #pragma once
2abf8da36SMatt Spinler 
3b0412d07SMatt Spinler #include "config.h"
4b0412d07SMatt Spinler 
5abf8da36SMatt Spinler #include "tach_sensor.hpp"
6c39e859bSMatt Spinler #include "trust_manager.hpp"
7abf8da36SMatt Spinler #include "types.hpp"
8abf8da36SMatt Spinler 
9177fe986SMatthew Barth #include <sdbusplus/bus.hpp>
10177fe986SMatthew Barth #include <sdeventplus/event.hpp>
11177fe986SMatthew Barth 
12177fe986SMatthew Barth #include <tuple>
13177fe986SMatthew Barth #include <vector>
14177fe986SMatthew Barth 
15abf8da36SMatt Spinler namespace phosphor
16abf8da36SMatt Spinler {
17abf8da36SMatt Spinler namespace fan
18abf8da36SMatt Spinler {
19abf8da36SMatt Spinler namespace monitor
20abf8da36SMatt Spinler {
21abf8da36SMatt Spinler 
22b0412d07SMatt Spinler class System;
23b0412d07SMatt Spinler 
24edaeb31cSBrad Bishop /**
25edaeb31cSBrad Bishop  * @class InvalidSensorError
26edaeb31cSBrad Bishop  *
27edaeb31cSBrad Bishop  * An exception type for sensors that don't exist or
28edaeb31cSBrad Bishop  * are otherwise inaccessible.
29edaeb31cSBrad Bishop  */
30177fe986SMatthew Barth class InvalidSensorError : public std::exception
31177fe986SMatthew Barth {};
32abf8da36SMatt Spinler 
33abf8da36SMatt Spinler /**
34abf8da36SMatt Spinler  * @class Fan
35abf8da36SMatt Spinler  *
36abf8da36SMatt Spinler  * Represents a fan, which can contain 1 or more sensors which
37abf8da36SMatt Spinler  * loosely correspond to rotors.  See below.
38abf8da36SMatt Spinler  *
39abf8da36SMatt Spinler  * There is a sensor when hwmon exposes one, which means there is a
40abf8da36SMatt Spinler  * speed value to be read.  Sometimes there is a sensor per rotor,
41abf8da36SMatt Spinler  * and other times multiple rotors just use 1 sensor total where
42abf8da36SMatt Spinler  * the sensor reports the slowest speed of all of the rotors.
43abf8da36SMatt Spinler  *
44abf8da36SMatt Spinler  * A rotor's speed is set by writing the Target value of a sensor.
45abf8da36SMatt Spinler  * Sometimes each sensor in a fan supports having a Target, and other
46abf8da36SMatt Spinler  * times not all of them do.  A TachSensor object knows if it supports
47abf8da36SMatt Spinler  * the Target property.
48abf8da36SMatt Spinler  *
49abf8da36SMatt Spinler  * The strategy for monitoring fan speeds is as follows:
50abf8da36SMatt Spinler  *
51abf8da36SMatt Spinler  * Every time a Target (new speed written) or Input (actual speed read)
52abf8da36SMatt Spinler  * sensor changes, check if the input value is within some range of the target
53abf8da36SMatt Spinler  * value.  If it isn't, start a timer at the end of which the sensor will be
54abf8da36SMatt Spinler  * set to not functional.  If enough sensors in the fan are now nonfunctional,
55abf8da36SMatt Spinler  * set the whole fan to nonfunctional in the inventory.
56abf8da36SMatt Spinler  *
57abf8da36SMatt Spinler  * When sensor inputs come back within a specified range of the target,
58abf8da36SMatt Spinler  * stop its timer if running, make the sensor functional again if it wasn't,
59abf8da36SMatt Spinler  * and if enough sensors in the fan are now functional set the whole fan
60abf8da36SMatt Spinler  * back to functional in the inventory.
61abf8da36SMatt Spinler  */
62abf8da36SMatt Spinler class Fan
63abf8da36SMatt Spinler {
64b1e18514SMatt Spinler     using Property = std::string;
65c21d0b36SPatrick Williams     using Value = std::variant<bool>;
66b1e18514SMatt Spinler     using PropertyMap = std::map<Property, Value>;
67b1e18514SMatt Spinler 
68b1e18514SMatt Spinler     using Interface = std::string;
69b1e18514SMatt Spinler     using InterfaceMap = std::map<Interface, PropertyMap>;
70b1e18514SMatt Spinler 
71b1e18514SMatt Spinler     using Object = sdbusplus::message::object_path;
72b1e18514SMatt Spinler     using ObjectMap = std::map<Object, InterfaceMap>;
73abf8da36SMatt Spinler 
74abf8da36SMatt Spinler   public:
75abf8da36SMatt Spinler     Fan() = delete;
76abf8da36SMatt Spinler     Fan(const Fan&) = delete;
77abf8da36SMatt Spinler     Fan(Fan&&) = default;
78abf8da36SMatt Spinler     Fan& operator=(const Fan&) = delete;
79abf8da36SMatt Spinler     Fan& operator=(Fan&&) = default;
80abf8da36SMatt Spinler     ~Fan() = default;
81abf8da36SMatt Spinler 
82abf8da36SMatt Spinler     /**
83abf8da36SMatt Spinler      * @brief Constructor
84abf8da36SMatt Spinler      *
856ad28430SMatthew Barth      * @param mode - mode of fan monitor
86abf8da36SMatt Spinler      * @param bus - the dbus object
871cfc2f11SWilliam A. Kennington III      * @param event - event loop reference
88c39e859bSMatt Spinler      * @param trust - the tach trust manager
89abf8da36SMatt Spinler      * @param def - the fan definition structure
90b0412d07SMatt Spinler      * @param system - Reference to the system object
91abf8da36SMatt Spinler      */
92177fe986SMatthew Barth     Fan(Mode mode, sdbusplus::bus::bus& bus, const sdeventplus::Event& event,
93b0412d07SMatt Spinler         std::unique_ptr<trust::Manager>& trust, const FanDefinition& def,
94b0412d07SMatt Spinler         System& system);
95abf8da36SMatt Spinler 
96ebaae611SMatt Spinler     /**
97ebaae611SMatt Spinler      * @brief Callback function for when an input sensor changes
98ebaae611SMatt Spinler      *
99ebaae611SMatt Spinler      * Starts a timer, where if it expires then the sensor
100a9406a77SMatt Spinler      * was out of range for too long and can be considered not functional.
101ebaae611SMatt Spinler      */
102ebaae611SMatt Spinler     void tachChanged(TachSensor& sensor);
103ebaae611SMatt Spinler 
104ebaae611SMatt Spinler     /**
105ebaae611SMatt Spinler      * @brief Calls tachChanged(sensor) on each sensor
106ebaae611SMatt Spinler      */
107ebaae611SMatt Spinler     void tachChanged();
108abf8da36SMatt Spinler 
109a9406a77SMatt Spinler     /**
110*69f2f48eSJolie Ku      * @brief The callback function for the method
111a9406a77SMatt Spinler      *
112a9406a77SMatt Spinler      * Sets the sensor to not functional.
113a9406a77SMatt Spinler      * If enough sensors are now not functional,
114a9406a77SMatt Spinler      * updates the functional status of the whole
115a9406a77SMatt Spinler      * fan in the inventory.
116a9406a77SMatt Spinler      *
117*69f2f48eSJolie Ku      * @param[in] sensor - the sensor for state update
118a9406a77SMatt Spinler      */
119*69f2f48eSJolie Ku     void updateState(TachSensor& sensor);
120a9406a77SMatt Spinler 
1214d982856SMatthew Barth     /**
1224d982856SMatthew Barth      * @brief Get the name of the fan
1234d982856SMatthew Barth      *
1244d982856SMatthew Barth      * @return - The fan name
1254d982856SMatthew Barth      */
1264d982856SMatthew Barth     inline const std::string& getName() const
1274d982856SMatthew Barth     {
1284d982856SMatthew Barth         return _name;
1294d982856SMatthew Barth     }
1304d982856SMatthew Barth 
131f552ea5cSMatthew Barth     /**
132f552ea5cSMatthew Barth      * @brief Finds the target speed of this fan
133f552ea5cSMatthew Barth      *
134f552ea5cSMatthew Barth      * Finds the target speed from the list of sensors that make up this
135f552ea5cSMatthew Barth      * fan. At least one sensor should contain a target speed value.
136f552ea5cSMatthew Barth      *
137f552ea5cSMatthew Barth      * @return - The target speed found from the list of sensors on the fan
138f552ea5cSMatthew Barth      */
139f552ea5cSMatthew Barth     uint64_t findTargetSpeed();
140f552ea5cSMatthew Barth 
141b63aa09eSMatt Spinler     /**
142b63aa09eSMatt Spinler      * @brief Returns the contained TachSensor objects
143b63aa09eSMatt Spinler      *
144b63aa09eSMatt Spinler      * @return std::vector<std::shared_ptr<TachSensor>> - The sensors
145b63aa09eSMatt Spinler      */
146b63aa09eSMatt Spinler     const std::vector<std::shared_ptr<TachSensor>>& sensors() const
147b63aa09eSMatt Spinler     {
148b63aa09eSMatt Spinler         return _sensors;
149b63aa09eSMatt Spinler     }
150b63aa09eSMatt Spinler 
151b63aa09eSMatt Spinler     /**
152b63aa09eSMatt Spinler      * @brief Returns the presence status of the fan
153b63aa09eSMatt Spinler      *
154b63aa09eSMatt Spinler      * @return bool - If the fan is present or not
155b63aa09eSMatt Spinler      */
156b63aa09eSMatt Spinler     bool present() const
157b63aa09eSMatt Spinler     {
158b63aa09eSMatt Spinler         return _present;
159b63aa09eSMatt Spinler     }
160b63aa09eSMatt Spinler 
161f13b42e2SMatt Spinler     /**
162f13b42e2SMatt Spinler      * @brief Called from TachSensor when its error timer expires
163f13b42e2SMatt Spinler      *        so an event log calling out the fan can be created.
164f13b42e2SMatt Spinler      *
165f13b42e2SMatt Spinler      * @param[in] sensor - The nonfunctional sensor
166f13b42e2SMatt Spinler      */
167f13b42e2SMatt Spinler     void sensorErrorTimerExpired(const TachSensor& sensor);
168f13b42e2SMatt Spinler 
169abf8da36SMatt Spinler   private:
170abf8da36SMatt Spinler     /**
171abf8da36SMatt Spinler      * @brief Returns true if the sensor input is not within
172abf8da36SMatt Spinler      * some deviation of the target.
173abf8da36SMatt Spinler      *
174abf8da36SMatt Spinler      * @param[in] sensor - the sensor to check
175abf8da36SMatt Spinler      */
176abf8da36SMatt Spinler     bool outOfRange(const TachSensor& sensor);
177abf8da36SMatt Spinler 
178abf8da36SMatt Spinler     /**
179abf8da36SMatt Spinler      * @brief Returns true if too many sensors are nonfunctional
180abf8da36SMatt Spinler      *        as defined by _numSensorFailsForNonFunc
181abf8da36SMatt Spinler      */
182abf8da36SMatt Spinler     bool tooManySensorsNonfunctional();
183abf8da36SMatt Spinler 
184b1e18514SMatt Spinler     /**
185b1e18514SMatt Spinler      * @brief Updates the Functional property in the inventory
186b1e18514SMatt Spinler      *        for the fan based on the value passed in.
187b1e18514SMatt Spinler      *
188b1e18514SMatt Spinler      * @param[in] functional - If the Functional property should
189b1e18514SMatt Spinler      *                         be set to true or false.
190b1e18514SMatt Spinler      */
191b1e18514SMatt Spinler     void updateInventory(bool functional);
192b1e18514SMatt Spinler 
193b1e18514SMatt Spinler     /**
194b0412d07SMatt Spinler      * @brief Called by _monitorTimer to start fan monitoring some
195b0412d07SMatt Spinler      *        amount of time after startup.
196b0412d07SMatt Spinler      */
197b0412d07SMatt Spinler     void startMonitor();
198b0412d07SMatt Spinler 
199b0412d07SMatt Spinler     /**
200b63aa09eSMatt Spinler      * @brief Called when the fan presence property changes on D-Bus
201b63aa09eSMatt Spinler      *
202b63aa09eSMatt Spinler      * @param[in] msg - The message from the propertiesChanged signal
203b63aa09eSMatt Spinler      */
204b63aa09eSMatt Spinler     void presenceChanged(sdbusplus::message::message& msg);
205b63aa09eSMatt Spinler 
206b63aa09eSMatt Spinler     /**
207abf8da36SMatt Spinler      * @brief the dbus object
208abf8da36SMatt Spinler      */
209abf8da36SMatt Spinler     sdbusplus::bus::bus& _bus;
210abf8da36SMatt Spinler 
211abf8da36SMatt Spinler     /**
212abf8da36SMatt Spinler      * @brief The inventory name of the fan
213abf8da36SMatt Spinler      */
214abf8da36SMatt Spinler     const std::string _name;
215abf8da36SMatt Spinler 
216abf8da36SMatt Spinler     /**
217abf8da36SMatt Spinler      * @brief The percentage that the input speed must be below
218abf8da36SMatt Spinler      *        the target speed to be considered an error.
219abf8da36SMatt Spinler      *        Between 0 and 100.
220abf8da36SMatt Spinler      */
221abf8da36SMatt Spinler     const size_t _deviation;
222abf8da36SMatt Spinler 
223abf8da36SMatt Spinler     /**
224abf8da36SMatt Spinler      * The number of sensors that must be nonfunctional at the
225abf8da36SMatt Spinler      * same time in order for the fan to be set to nonfunctional
226abf8da36SMatt Spinler      * in the inventory.
227abf8da36SMatt Spinler      */
228abf8da36SMatt Spinler     const size_t _numSensorFailsForNonFunc;
229abf8da36SMatt Spinler 
230abf8da36SMatt Spinler     /**
2315d564a9fSJolie Ku      * The number of failed sensors
2325d564a9fSJolie Ku      */
2335d564a9fSJolie Ku     size_t _numFailedSensor = 0;
2345d564a9fSJolie Ku 
2355d564a9fSJolie Ku     /**
236abf8da36SMatt Spinler      * @brief The current functional state of the fan
237abf8da36SMatt Spinler      */
238abf8da36SMatt Spinler     bool _functional = true;
239abf8da36SMatt Spinler 
240abf8da36SMatt Spinler     /**
241abf8da36SMatt Spinler      * The sensor objects for the fan
242abf8da36SMatt Spinler      */
24332affb98SMatthew Barth     std::vector<std::shared_ptr<TachSensor>> _sensors;
244c39e859bSMatt Spinler 
245c39e859bSMatt Spinler     /**
246c39e859bSMatt Spinler      * The tach trust manager object
247c39e859bSMatt Spinler      */
248c39e859bSMatt Spinler     std::unique_ptr<trust::Manager>& _trustManager;
249b0412d07SMatt Spinler 
250b0412d07SMatt Spinler #ifdef MONITOR_USE_JSON
251b0412d07SMatt Spinler     /**
252b0412d07SMatt Spinler      * @brief The number of seconds to wait after startup until
253b0412d07SMatt Spinler      *        fan sensors should checked against their targets.
254b0412d07SMatt Spinler      */
255b0412d07SMatt Spinler     size_t _monitorDelay;
256b0412d07SMatt Spinler 
257b0412d07SMatt Spinler     /**
258b0412d07SMatt Spinler      * @brief Expires after _monitorDelay to start fan monitoring.
259b0412d07SMatt Spinler      */
260b0412d07SMatt Spinler     sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> _monitorTimer;
261b0412d07SMatt Spinler #endif
262b0412d07SMatt Spinler 
263b0412d07SMatt Spinler     /**
264b0412d07SMatt Spinler      * @brief Set to true when monitoring can start.
265b0412d07SMatt Spinler      */
266b0412d07SMatt Spinler     bool _monitorReady = false;
267b0412d07SMatt Spinler 
268b0412d07SMatt Spinler     /**
269b0412d07SMatt Spinler      * @brief Reference to the System object
270b0412d07SMatt Spinler      */
271b0412d07SMatt Spinler     System& _system;
272b63aa09eSMatt Spinler 
273b63aa09eSMatt Spinler     /**
274b63aa09eSMatt Spinler      * @brief The match object for propertiesChanged signals
275b63aa09eSMatt Spinler      *        for the inventory item interface to track the
276b63aa09eSMatt Spinler      *        Present property.
277b63aa09eSMatt Spinler      */
278b63aa09eSMatt Spinler     sdbusplus::bus::match::match _presenceMatch;
279b63aa09eSMatt Spinler 
280b63aa09eSMatt Spinler     /**
281b63aa09eSMatt Spinler      * @brief The current presence state
282b63aa09eSMatt Spinler      */
283b63aa09eSMatt Spinler     bool _present = false;
28427f6b686SMatt Spinler 
28527f6b686SMatt Spinler     /**
28627f6b686SMatt Spinler      * @brief The number of seconds to wait after a fan is removed before
28727f6b686SMatt Spinler      *        creating an event log for it.  If std::nullopt, then no
28827f6b686SMatt Spinler      *        event log will be created.
28927f6b686SMatt Spinler      */
29027f6b686SMatt Spinler     const std::optional<size_t> _fanMissingErrorDelay;
29127f6b686SMatt Spinler 
29227f6b686SMatt Spinler     /**
29327f6b686SMatt Spinler      * @brief The timer that uses the _fanMissingErrorDelay timeout,
29427f6b686SMatt Spinler      *        at the end of which an event log will be created.
29527f6b686SMatt Spinler      */
29627f6b686SMatt Spinler     std::unique_ptr<
29727f6b686SMatt Spinler         sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>
29827f6b686SMatt Spinler         _fanMissingErrorTimer;
299abf8da36SMatt Spinler };
300abf8da36SMatt Spinler 
301177fe986SMatthew Barth } // namespace monitor
302177fe986SMatthew Barth } // namespace fan
303177fe986SMatthew Barth } // namespace phosphor
304