blob: a7db524cda9b8e6267a541215d5e73f842ef6733 [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/**
25 * @class InvalidSensorError
26 *
27 * An exception type for sensors that don't exist or
28 * are otherwise inaccessible.
29 */
Matthew Barth177fe982020-05-26 11:05:19 -050030class InvalidSensorError : public std::exception
31{};
Matt Spinlerabf8da32017-04-27 14:08:45 -050032
33/**
34 * @class Fan
35 *
36 * Represents a fan, which can contain 1 or more sensors which
37 * loosely correspond to rotors. See below.
38 *
39 * There is a sensor when hwmon exposes one, which means there is a
40 * speed value to be read. Sometimes there is a sensor per rotor,
41 * and other times multiple rotors just use 1 sensor total where
42 * the sensor reports the slowest speed of all of the rotors.
43 *
44 * A rotor's speed is set by writing the Target value of a sensor.
45 * Sometimes each sensor in a fan supports having a Target, and other
46 * times not all of them do. A TachSensor object knows if it supports
47 * the Target property.
48 *
49 * The strategy for monitoring fan speeds is as follows:
50 *
51 * Every time a Target (new speed written) or Input (actual speed read)
52 * sensor changes, check if the input value is within some range of the target
53 * value. If it isn't, start a timer at the end of which the sensor will be
54 * set to not functional. If enough sensors in the fan are now nonfunctional,
55 * set the whole fan to nonfunctional in the inventory.
56 *
57 * When sensor inputs come back within a specified range of the target,
58 * stop its timer if running, make the sensor functional again if it wasn't,
59 * and if enough sensors in the fan are now functional set the whole fan
60 * back to functional in the inventory.
61 */
62class Fan
63{
Matt Spinlerb1e18512017-04-27 14:42:33 -050064 using Property = std::string;
Patrick Williamsc21d0b32020-05-13 17:55:14 -050065 using Value = std::variant<bool>;
Matt Spinlerb1e18512017-04-27 14:42:33 -050066 using PropertyMap = std::map<Property, Value>;
67
68 using Interface = std::string;
69 using InterfaceMap = std::map<Interface, PropertyMap>;
70
71 using Object = sdbusplus::message::object_path;
72 using ObjectMap = std::map<Object, InterfaceMap>;
Matt Spinlerabf8da32017-04-27 14:08:45 -050073
Matthew Barth177fe982020-05-26 11:05:19 -050074 public:
75 Fan() = delete;
76 Fan(const Fan&) = delete;
77 Fan(Fan&&) = default;
78 Fan& operator=(const Fan&) = delete;
79 Fan& operator=(Fan&&) = default;
80 ~Fan() = default;
Matt Spinlerabf8da32017-04-27 14:08:45 -050081
Matthew Barth177fe982020-05-26 11:05:19 -050082 /**
83 * @brief Constructor
84 *
85 * @param mode - mode of fan monitor
86 * @param bus - the dbus object
87 * @param event - event loop reference
88 * @param trust - the tach trust manager
89 * @param def - the fan definition structure
Matt Spinlerb0412d02020-10-12 16:53:52 -050090 * @param system - Reference to the system object
Matthew Barth177fe982020-05-26 11:05:19 -050091 */
92 Fan(Mode mode, sdbusplus::bus::bus& bus, const sdeventplus::Event& event,
Matt Spinlerb0412d02020-10-12 16:53:52 -050093 std::unique_ptr<trust::Manager>& trust, const FanDefinition& def,
94 System& system);
Matt Spinlerabf8da32017-04-27 14:08:45 -050095
Matthew Barth177fe982020-05-26 11:05:19 -050096 /**
97 * @brief Callback function for when an input sensor changes
98 *
99 * Starts a timer, where if it expires then the sensor
100 * was out of range for too long and can be considered not functional.
101 */
102 void tachChanged(TachSensor& sensor);
Matt Spinlerabf8da32017-04-27 14:08:45 -0500103
Matthew Barth177fe982020-05-26 11:05:19 -0500104 /**
105 * @brief Calls tachChanged(sensor) on each sensor
106 */
107 void tachChanged();
Matt Spinlerebaae612017-04-27 14:21:48 -0500108
Matthew Barth177fe982020-05-26 11:05:19 -0500109 /**
110 * @brief The callback function for the timer
111 *
112 * Sets the sensor to not functional.
113 * If enough sensors are now not functional,
114 * updates the functional status of the whole
115 * fan in the inventory.
116 *
117 * @param[in] sensor - the sensor whose timer expired
118 */
119 void timerExpired(TachSensor& sensor);
Matt Spinlerabf8da32017-04-27 14:08:45 -0500120
Matthew Barth177fe982020-05-26 11:05:19 -0500121 /**
122 * @brief Get the name of the fan
123 *
124 * @return - The fan name
125 */
126 inline const std::string& getName() const
127 {
128 return _name;
129 }
Matt Spinlera9406a72017-04-27 14:29:24 -0500130
Matthew Barth177fe982020-05-26 11:05:19 -0500131 /**
132 * @brief Finds the target speed of this fan
133 *
134 * Finds the target speed from the list of sensors that make up this
135 * fan. At least one sensor should contain a target speed value.
136 *
137 * @return - The target speed found from the list of sensors on the fan
138 */
139 uint64_t findTargetSpeed();
Matthew Barth4d982852017-11-17 09:37:13 -0600140
Matt Spinlerb63aa092020-10-14 09:45:11 -0500141 /**
142 * @brief Returns the contained TachSensor objects
143 *
144 * @return std::vector<std::shared_ptr<TachSensor>> - The sensors
145 */
146 const std::vector<std::shared_ptr<TachSensor>>& sensors() const
147 {
148 return _sensors;
149 }
150
151 /**
152 * @brief Returns the presence status of the fan
153 *
154 * @return bool - If the fan is present or not
155 */
156 bool present() const
157 {
158 return _present;
159 }
160
Matthew Barth177fe982020-05-26 11:05:19 -0500161 private:
162 /**
163 * @brief Returns true if the sensor input is not within
164 * some deviation of the target.
165 *
166 * @param[in] sensor - the sensor to check
167 */
168 bool outOfRange(const TachSensor& sensor);
Matthew Barthf552ea52018-01-15 16:22:04 -0600169
Matthew Barth177fe982020-05-26 11:05:19 -0500170 /**
171 * @brief Returns true if too many sensors are nonfunctional
172 * as defined by _numSensorFailsForNonFunc
173 */
174 bool tooManySensorsNonfunctional();
Matt Spinlerabf8da32017-04-27 14:08:45 -0500175
Matthew Barth177fe982020-05-26 11:05:19 -0500176 /**
177 * @brief Updates the Functional property in the inventory
178 * for the fan based on the value passed in.
179 *
180 * @param[in] functional - If the Functional property should
181 * be set to true or false.
182 */
183 void updateInventory(bool functional);
Matt Spinlerabf8da32017-04-27 14:08:45 -0500184
Matthew Barth177fe982020-05-26 11:05:19 -0500185 /**
Matt Spinlerb0412d02020-10-12 16:53:52 -0500186 * @brief Called by _monitorTimer to start fan monitoring some
187 * amount of time after startup.
188 */
189 void startMonitor();
190
191 /**
Matt Spinlerb63aa092020-10-14 09:45:11 -0500192 * @brief Called when the fan presence property changes on D-Bus
193 *
194 * @param[in] msg - The message from the propertiesChanged signal
195 */
196 void presenceChanged(sdbusplus::message::message& msg);
197
198 /**
Matthew Barth177fe982020-05-26 11:05:19 -0500199 * @brief the dbus object
200 */
201 sdbusplus::bus::bus& _bus;
Matt Spinlerabf8da32017-04-27 14:08:45 -0500202
Matthew Barth177fe982020-05-26 11:05:19 -0500203 /**
204 * @brief The inventory name of the fan
205 */
206 const std::string _name;
Matt Spinlerb1e18512017-04-27 14:42:33 -0500207
Matthew Barth177fe982020-05-26 11:05:19 -0500208 /**
209 * @brief The percentage that the input speed must be below
210 * the target speed to be considered an error.
211 * Between 0 and 100.
212 */
213 const size_t _deviation;
Matt Spinlerabf8da32017-04-27 14:08:45 -0500214
Matthew Barth177fe982020-05-26 11:05:19 -0500215 /**
216 * The number of sensors that must be nonfunctional at the
217 * same time in order for the fan to be set to nonfunctional
218 * in the inventory.
219 */
220 const size_t _numSensorFailsForNonFunc;
Matt Spinlerabf8da32017-04-27 14:08:45 -0500221
Matthew Barth177fe982020-05-26 11:05:19 -0500222 /**
Jolie Ku5d564a92020-10-23 09:04:28 +0800223 * The number of failed sensors
224 */
225 size_t _numFailedSensor = 0;
226
227 /**
Matthew Barth177fe982020-05-26 11:05:19 -0500228 * @brief The current functional state of the fan
229 */
230 bool _functional = true;
Matt Spinlerabf8da32017-04-27 14:08:45 -0500231
Matthew Barth177fe982020-05-26 11:05:19 -0500232 /**
233 * The sensor objects for the fan
234 */
235 std::vector<std::shared_ptr<TachSensor>> _sensors;
Matt Spinlerabf8da32017-04-27 14:08:45 -0500236
Matthew Barth177fe982020-05-26 11:05:19 -0500237 /**
238 * The tach trust manager object
239 */
240 std::unique_ptr<trust::Manager>& _trustManager;
Matt Spinlerb0412d02020-10-12 16:53:52 -0500241
242#ifdef MONITOR_USE_JSON
243 /**
244 * @brief The number of seconds to wait after startup until
245 * fan sensors should checked against their targets.
246 */
247 size_t _monitorDelay;
248
249 /**
250 * @brief Expires after _monitorDelay to start fan monitoring.
251 */
252 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> _monitorTimer;
253#endif
254
255 /**
256 * @brief Set to true when monitoring can start.
257 */
258 bool _monitorReady = false;
259
260 /**
261 * @brief Reference to the System object
262 */
263 System& _system;
Matt Spinlerb63aa092020-10-14 09:45:11 -0500264
265 /**
266 * @brief The match object for propertiesChanged signals
267 * for the inventory item interface to track the
268 * Present property.
269 */
270 sdbusplus::bus::match::match _presenceMatch;
271
272 /**
273 * @brief The current presence state
274 */
275 bool _present = false;
Matt Spinlerabf8da32017-04-27 14:08:45 -0500276};
277
Matthew Barth177fe982020-05-26 11:05:19 -0500278} // namespace monitor
279} // namespace fan
280} // namespace phosphor