xref: /openbmc/phosphor-fan-presence/monitor/fan.hpp (revision b0412d0720760de9e8f62b582cb5edbd242f42b4)
1abf8da36SMatt Spinler #pragma once
2abf8da36SMatt Spinler 
3*b0412d07SMatt Spinler #include "config.h"
4*b0412d07SMatt 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 
22*b0412d07SMatt Spinler class System;
23*b0412d07SMatt 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
90*b0412d07SMatt Spinler      * @param system - Reference to the system object
91abf8da36SMatt Spinler      */
92177fe986SMatthew Barth     Fan(Mode mode, sdbusplus::bus::bus& bus, const sdeventplus::Event& event,
93*b0412d07SMatt Spinler         std::unique_ptr<trust::Manager>& trust, const FanDefinition& def,
94*b0412d07SMatt 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     /**
110a9406a77SMatt Spinler      * @brief The callback function for the timer
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      *
117a9406a77SMatt Spinler      * @param[in] sensor - the sensor whose timer expired
118a9406a77SMatt Spinler      */
119a9406a77SMatt Spinler     void timerExpired(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 
141abf8da36SMatt Spinler   private:
142abf8da36SMatt Spinler     /**
143abf8da36SMatt Spinler      * @brief Returns true if the sensor input is not within
144abf8da36SMatt Spinler      * some deviation of the target.
145abf8da36SMatt Spinler      *
146abf8da36SMatt Spinler      * @param[in] sensor - the sensor to check
147abf8da36SMatt Spinler      */
148abf8da36SMatt Spinler     bool outOfRange(const TachSensor& sensor);
149abf8da36SMatt Spinler 
150abf8da36SMatt Spinler     /**
151abf8da36SMatt Spinler      * @brief Returns true if too many sensors are nonfunctional
152abf8da36SMatt Spinler      *        as defined by _numSensorFailsForNonFunc
153abf8da36SMatt Spinler      */
154abf8da36SMatt Spinler     bool tooManySensorsNonfunctional();
155abf8da36SMatt Spinler 
156b1e18514SMatt Spinler     /**
157b1e18514SMatt Spinler      * @brief Updates the Functional property in the inventory
158b1e18514SMatt Spinler      *        for the fan based on the value passed in.
159b1e18514SMatt Spinler      *
160b1e18514SMatt Spinler      * @param[in] functional - If the Functional property should
161b1e18514SMatt Spinler      *                         be set to true or false.
162b1e18514SMatt Spinler      */
163b1e18514SMatt Spinler     void updateInventory(bool functional);
164b1e18514SMatt Spinler 
165b1e18514SMatt Spinler     /**
166*b0412d07SMatt Spinler      * @brief Called by _monitorTimer to start fan monitoring some
167*b0412d07SMatt Spinler      *        amount of time after startup.
168*b0412d07SMatt Spinler      */
169*b0412d07SMatt Spinler     void startMonitor();
170*b0412d07SMatt Spinler 
171*b0412d07SMatt Spinler     /**
172abf8da36SMatt Spinler      * @brief the dbus object
173abf8da36SMatt Spinler      */
174abf8da36SMatt Spinler     sdbusplus::bus::bus& _bus;
175abf8da36SMatt Spinler 
176abf8da36SMatt Spinler     /**
177abf8da36SMatt Spinler      * @brief The inventory name of the fan
178abf8da36SMatt Spinler      */
179abf8da36SMatt Spinler     const std::string _name;
180abf8da36SMatt Spinler 
181abf8da36SMatt Spinler     /**
182abf8da36SMatt Spinler      * @brief The percentage that the input speed must be below
183abf8da36SMatt Spinler      *        the target speed to be considered an error.
184abf8da36SMatt Spinler      *        Between 0 and 100.
185abf8da36SMatt Spinler      */
186abf8da36SMatt Spinler     const size_t _deviation;
187abf8da36SMatt Spinler 
188abf8da36SMatt Spinler     /**
189abf8da36SMatt Spinler      * The number of sensors that must be nonfunctional at the
190abf8da36SMatt Spinler      * same time in order for the fan to be set to nonfunctional
191abf8da36SMatt Spinler      * in the inventory.
192abf8da36SMatt Spinler      */
193abf8da36SMatt Spinler     const size_t _numSensorFailsForNonFunc;
194abf8da36SMatt Spinler 
195abf8da36SMatt Spinler     /**
1965d564a9fSJolie Ku      * The number of failed sensors
1975d564a9fSJolie Ku      */
1985d564a9fSJolie Ku     size_t _numFailedSensor = 0;
1995d564a9fSJolie Ku 
2005d564a9fSJolie Ku     /**
201abf8da36SMatt Spinler      * @brief The current functional state of the fan
202abf8da36SMatt Spinler      */
203abf8da36SMatt Spinler     bool _functional = true;
204abf8da36SMatt Spinler 
205abf8da36SMatt Spinler     /**
206abf8da36SMatt Spinler      * The sensor objects for the fan
207abf8da36SMatt Spinler      */
20832affb98SMatthew Barth     std::vector<std::shared_ptr<TachSensor>> _sensors;
209c39e859bSMatt Spinler 
210c39e859bSMatt Spinler     /**
211c39e859bSMatt Spinler      * The tach trust manager object
212c39e859bSMatt Spinler      */
213c39e859bSMatt Spinler     std::unique_ptr<trust::Manager>& _trustManager;
214*b0412d07SMatt Spinler 
215*b0412d07SMatt Spinler #ifdef MONITOR_USE_JSON
216*b0412d07SMatt Spinler     /**
217*b0412d07SMatt Spinler      * @brief The number of seconds to wait after startup until
218*b0412d07SMatt Spinler      *        fan sensors should checked against their targets.
219*b0412d07SMatt Spinler      */
220*b0412d07SMatt Spinler     size_t _monitorDelay;
221*b0412d07SMatt Spinler 
222*b0412d07SMatt Spinler     /**
223*b0412d07SMatt Spinler      * @brief Expires after _monitorDelay to start fan monitoring.
224*b0412d07SMatt Spinler      */
225*b0412d07SMatt Spinler     sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> _monitorTimer;
226*b0412d07SMatt Spinler #endif
227*b0412d07SMatt Spinler 
228*b0412d07SMatt Spinler     /**
229*b0412d07SMatt Spinler      * @brief Set to true when monitoring can start.
230*b0412d07SMatt Spinler      */
231*b0412d07SMatt Spinler     bool _monitorReady = false;
232*b0412d07SMatt Spinler 
233*b0412d07SMatt Spinler     /**
234*b0412d07SMatt Spinler      * @brief Reference to the System object
235*b0412d07SMatt Spinler      */
236*b0412d07SMatt Spinler     System& _system;
237abf8da36SMatt Spinler };
238abf8da36SMatt Spinler 
239177fe986SMatthew Barth } // namespace monitor
240177fe986SMatthew Barth } // namespace fan
241177fe986SMatthew Barth } // namespace phosphor
242