blob: a932db2a44afa383197009222fd65496ade4deff [file] [log] [blame]
Matt Spinlerabf8da32017-04-27 14:08:45 -05001#pragma once
2
Matt Spinlerb0412d02020-10-12 16:53:52 -05003#include "config.h"
4
Matt Spinlerabf8da32017-04-27 14:08:45 -05005#include "tach_sensor.hpp"
Matt Spinlerc39e8592017-09-28 13:13:08 -05006#include "trust_manager.hpp"
Matt Spinlerabf8da32017-04-27 14:08:45 -05007#include "types.hpp"
8
Matthew Barth177fe982020-05-26 11:05:19 -05009#include <sdbusplus/bus.hpp>
10#include <sdeventplus/event.hpp>
11
12#include <tuple>
13#include <vector>
14
Matt Spinlerabf8da32017-04-27 14:08:45 -050015namespace phosphor
16{
17namespace fan
18{
19namespace monitor
20{
21
Matt Spinlerb0412d02020-10-12 16:53:52 -050022class System;
23
Brad Bishopedaeb312017-07-30 19:38:20 -040024/**
Matt Spinlerabf8da32017-04-27 14:08:45 -050025 * @class Fan
26 *
27 * Represents a fan, which can contain 1 or more sensors which
28 * loosely correspond to rotors. See below.
29 *
30 * There is a sensor when hwmon exposes one, which means there is a
31 * speed value to be read. Sometimes there is a sensor per rotor,
32 * and other times multiple rotors just use 1 sensor total where
33 * the sensor reports the slowest speed of all of the rotors.
34 *
35 * A rotor's speed is set by writing the Target value of a sensor.
36 * Sometimes each sensor in a fan supports having a Target, and other
37 * times not all of them do. A TachSensor object knows if it supports
38 * the Target property.
39 *
40 * The strategy for monitoring fan speeds is as follows:
41 *
42 * Every time a Target (new speed written) or Input (actual speed read)
43 * sensor changes, check if the input value is within some range of the target
44 * value. If it isn't, start a timer at the end of which the sensor will be
45 * set to not functional. If enough sensors in the fan are now nonfunctional,
46 * set the whole fan to nonfunctional in the inventory.
47 *
48 * When sensor inputs come back within a specified range of the target,
49 * stop its timer if running, make the sensor functional again if it wasn't,
50 * and if enough sensors in the fan are now functional set the whole fan
51 * back to functional in the inventory.
52 */
53class Fan
54{
Matt Spinlerb1e18512017-04-27 14:42:33 -050055 using Property = std::string;
Patrick Williamsc21d0b32020-05-13 17:55:14 -050056 using Value = std::variant<bool>;
Matt Spinlerb1e18512017-04-27 14:42:33 -050057 using PropertyMap = std::map<Property, Value>;
58
59 using Interface = std::string;
60 using InterfaceMap = std::map<Interface, PropertyMap>;
61
62 using Object = sdbusplus::message::object_path;
63 using ObjectMap = std::map<Object, InterfaceMap>;
Matt Spinlerabf8da32017-04-27 14:08:45 -050064
Matthew Barth177fe982020-05-26 11:05:19 -050065 public:
66 Fan() = delete;
67 Fan(const Fan&) = delete;
68 Fan(Fan&&) = default;
69 Fan& operator=(const Fan&) = delete;
70 Fan& operator=(Fan&&) = default;
71 ~Fan() = default;
Matt Spinlerabf8da32017-04-27 14:08:45 -050072
Matthew Barth177fe982020-05-26 11:05:19 -050073 /**
74 * @brief Constructor
75 *
76 * @param mode - mode of fan monitor
77 * @param bus - the dbus object
78 * @param event - event loop reference
79 * @param trust - the tach trust manager
80 * @param def - the fan definition structure
Matt Spinlerb0412d02020-10-12 16:53:52 -050081 * @param system - Reference to the system object
Matthew Barth177fe982020-05-26 11:05:19 -050082 */
83 Fan(Mode mode, sdbusplus::bus::bus& bus, const sdeventplus::Event& event,
Matt Spinlerb0412d02020-10-12 16:53:52 -050084 std::unique_ptr<trust::Manager>& trust, const FanDefinition& def,
85 System& system);
Matt Spinlerabf8da32017-04-27 14:08:45 -050086
Matthew Barth177fe982020-05-26 11:05:19 -050087 /**
88 * @brief Callback function for when an input sensor changes
89 *
90 * Starts a timer, where if it expires then the sensor
91 * was out of range for too long and can be considered not functional.
92 */
93 void tachChanged(TachSensor& sensor);
Matt Spinlerabf8da32017-04-27 14:08:45 -050094
Matthew Barth177fe982020-05-26 11:05:19 -050095 /**
96 * @brief Calls tachChanged(sensor) on each sensor
97 */
98 void tachChanged();
Matt Spinlerebaae612017-04-27 14:21:48 -050099
Matthew Barth177fe982020-05-26 11:05:19 -0500100 /**
Jolie Ku69f2f482020-10-21 09:59:43 +0800101 * @brief The callback function for the method
Matthew Barth177fe982020-05-26 11:05:19 -0500102 *
103 * Sets the sensor to not functional.
104 * If enough sensors are now not functional,
105 * updates the functional status of the whole
106 * fan in the inventory.
107 *
Jolie Ku69f2f482020-10-21 09:59:43 +0800108 * @param[in] sensor - the sensor for state update
Matthew Barth177fe982020-05-26 11:05:19 -0500109 */
Jolie Ku69f2f482020-10-21 09:59:43 +0800110 void updateState(TachSensor& sensor);
Matt Spinlerabf8da32017-04-27 14:08:45 -0500111
Matthew Barth177fe982020-05-26 11:05:19 -0500112 /**
113 * @brief Get the name of the fan
114 *
115 * @return - The fan name
116 */
117 inline const std::string& getName() const
118 {
119 return _name;
120 }
Matt Spinlera9406a72017-04-27 14:29:24 -0500121
Matthew Barth177fe982020-05-26 11:05:19 -0500122 /**
123 * @brief Finds the target speed of this fan
124 *
125 * Finds the target speed from the list of sensors that make up this
126 * fan. At least one sensor should contain a target speed value.
127 *
128 * @return - The target speed found from the list of sensors on the fan
129 */
130 uint64_t findTargetSpeed();
Matthew Barth4d982852017-11-17 09:37:13 -0600131
Matt Spinlerb63aa092020-10-14 09:45:11 -0500132 /**
133 * @brief Returns the contained TachSensor objects
134 *
135 * @return std::vector<std::shared_ptr<TachSensor>> - The sensors
136 */
137 const std::vector<std::shared_ptr<TachSensor>>& sensors() const
138 {
139 return _sensors;
140 }
141
142 /**
143 * @brief Returns the presence status of the fan
144 *
145 * @return bool - If the fan is present or not
146 */
147 bool present() const
148 {
149 return _present;
150 }
151
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500152 /**
153 * @brief Called from TachSensor when its error timer expires
154 * so an event log calling out the fan can be created.
155 *
156 * @param[in] sensor - The nonfunctional sensor
157 */
158 void sensorErrorTimerExpired(const TachSensor& sensor);
159
Matthew Barthfcb0dbc2021-02-10 14:23:39 -0600160 /**
161 * @brief Process the state of the given tach sensor without checking
162 * any trust groups the sensor may be included in
163 *
164 * @param[in] sensor - Tach sensor to process
165 *
166 * This function is intended to check the current state of a tach sensor
167 * regardless of whether or not the tach sensor is configured to be in any
168 * trust groups.
169 */
170 void process(TachSensor& sensor);
171
Matt Spinler7d135642021-02-04 12:44:17 -0600172 /**
173 * @brief The function that runs when the power state changes
174 *
175 * @param[in] powerStateOn - If power is now on or not
176 */
177 void powerStateChanged(bool powerStateOn);
178
Matt Spinler623635c2021-03-29 13:13:59 -0500179 /**
180 * @brief Timer callback function that deals with sensors using
181 * the 'count' method for determining functional status.
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600182 *
183 * @param[in] sensor - TachSensor object
Matt Spinler623635c2021-03-29 13:13:59 -0500184 */
Matt Spinlerfdfcc672021-06-01 14:51:06 -0600185 void countTimerExpired(TachSensor& sensor);
Matt Spinler623635c2021-03-29 13:13:59 -0500186
Matthew Barth177fe982020-05-26 11:05:19 -0500187 private:
188 /**
189 * @brief Returns true if the sensor input is not within
190 * some deviation of the target.
191 *
192 * @param[in] sensor - the sensor to check
193 */
194 bool outOfRange(const TachSensor& sensor);
Matthew Barthf552ea52018-01-15 16:22:04 -0600195
Matthew Barth177fe982020-05-26 11:05:19 -0500196 /**
Matthew Barth7c23a042021-01-26 16:21:45 -0600197 * @brief Returns the number sensors that are nonfunctional
Matthew Barth177fe982020-05-26 11:05:19 -0500198 */
Matthew Barth7c23a042021-01-26 16:21:45 -0600199 size_t countNonFunctionalSensors();
Matt Spinlerabf8da32017-04-27 14:08:45 -0500200
Matthew Barth177fe982020-05-26 11:05:19 -0500201 /**
202 * @brief Updates the Functional property in the inventory
203 * for the fan based on the value passed in.
204 *
205 * @param[in] functional - If the Functional property should
206 * be set to true or false.
207 */
208 void updateInventory(bool functional);
Matt Spinlerabf8da32017-04-27 14:08:45 -0500209
Matthew Barth177fe982020-05-26 11:05:19 -0500210 /**
Matt Spinlerb0412d02020-10-12 16:53:52 -0500211 * @brief Called by _monitorTimer to start fan monitoring some
212 * amount of time after startup.
213 */
214 void startMonitor();
215
216 /**
Matt Spinlerb63aa092020-10-14 09:45:11 -0500217 * @brief Called when the fan presence property changes on D-Bus
218 *
219 * @param[in] msg - The message from the propertiesChanged signal
220 */
221 void presenceChanged(sdbusplus::message::message& msg);
222
223 /**
Matt Spinler7d135642021-02-04 12:44:17 -0600224 * @brief Called when there is an interfacesAdded signal on the
225 * fan D-Bus path so the code can look for the 'Present'
226 * property value.
227 *
228 * @param[in] msg - The message from the interfacesAdded signal
229 */
230 void presenceIfaceAdded(sdbusplus::message::message& msg);
231
232 /**
Matthew Barth177fe982020-05-26 11:05:19 -0500233 * @brief the dbus object
234 */
235 sdbusplus::bus::bus& _bus;
Matt Spinlerabf8da32017-04-27 14:08:45 -0500236
Matthew Barth177fe982020-05-26 11:05:19 -0500237 /**
238 * @brief The inventory name of the fan
239 */
240 const std::string _name;
Matt Spinlerb1e18512017-04-27 14:42:33 -0500241
Matthew Barth177fe982020-05-26 11:05:19 -0500242 /**
243 * @brief The percentage that the input speed must be below
244 * the target speed to be considered an error.
245 * Between 0 and 100.
246 */
247 const size_t _deviation;
Matt Spinlerabf8da32017-04-27 14:08:45 -0500248
Matthew Barth177fe982020-05-26 11:05:19 -0500249 /**
250 * The number of sensors that must be nonfunctional at the
251 * same time in order for the fan to be set to nonfunctional
252 * in the inventory.
253 */
254 const size_t _numSensorFailsForNonFunc;
Matt Spinlerabf8da32017-04-27 14:08:45 -0500255
Matthew Barth177fe982020-05-26 11:05:19 -0500256 /**
Jolie Ku5d564a92020-10-23 09:04:28 +0800257 * The number of failed sensors
258 */
259 size_t _numFailedSensor = 0;
260
261 /**
Matthew Barth177fe982020-05-26 11:05:19 -0500262 * @brief The current functional state of the fan
263 */
264 bool _functional = true;
Matt Spinlerabf8da32017-04-27 14:08:45 -0500265
Matthew Barth177fe982020-05-26 11:05:19 -0500266 /**
267 * The sensor objects for the fan
268 */
269 std::vector<std::shared_ptr<TachSensor>> _sensors;
Matt Spinlerabf8da32017-04-27 14:08:45 -0500270
Matthew Barth177fe982020-05-26 11:05:19 -0500271 /**
272 * The tach trust manager object
273 */
274 std::unique_ptr<trust::Manager>& _trustManager;
Matt Spinlerb0412d02020-10-12 16:53:52 -0500275
276#ifdef MONITOR_USE_JSON
277 /**
278 * @brief The number of seconds to wait after startup until
279 * fan sensors should checked against their targets.
280 */
281 size_t _monitorDelay;
282
283 /**
284 * @brief Expires after _monitorDelay to start fan monitoring.
285 */
286 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> _monitorTimer;
287#endif
288
289 /**
290 * @brief Set to true when monitoring can start.
291 */
292 bool _monitorReady = false;
293
294 /**
295 * @brief Reference to the System object
296 */
297 System& _system;
Matt Spinlerb63aa092020-10-14 09:45:11 -0500298
299 /**
300 * @brief The match object for propertiesChanged signals
301 * for the inventory item interface to track the
302 * Present property.
303 */
304 sdbusplus::bus::match::match _presenceMatch;
305
306 /**
Matt Spinler7d135642021-02-04 12:44:17 -0600307 * @brief The match object for the interfacesAdded signal
308 * for the interface that has the Present property.
309 */
310 sdbusplus::bus::match::match _presenceIfaceAddedMatch;
311
312 /**
Matt Spinlerb63aa092020-10-14 09:45:11 -0500313 * @brief The current presence state
314 */
315 bool _present = false;
Matt Spinler27f6b682020-10-27 08:43:37 -0500316
317 /**
318 * @brief The number of seconds to wait after a fan is removed before
319 * creating an event log for it. If std::nullopt, then no
320 * event log will be created.
321 */
322 const std::optional<size_t> _fanMissingErrorDelay;
323
324 /**
325 * @brief The timer that uses the _fanMissingErrorDelay timeout,
326 * at the end of which an event log will be created.
327 */
328 std::unique_ptr<
329 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>
330 _fanMissingErrorTimer;
Matt Spinler623635c2021-03-29 13:13:59 -0500331
332 /**
Matt Spinlera3584bd2021-03-29 15:48:30 -0500333 * @brief If the fan and sensors should be set to functional when
334 * a fan plug is detected.
335 */
336 bool _setFuncOnPresent;
Matt Spinlerabf8da32017-04-27 14:08:45 -0500337};
338
Matthew Barth177fe982020-05-26 11:05:19 -0500339} // namespace monitor
340} // namespace fan
341} // namespace phosphor