blob: c7995b6958d7640e1f07de9c0ed3961994831690 [file] [log] [blame]
Matt Spinler7f88fe62017-04-10 14:39:02 -05001#pragma once
Matthew Bartha9561842017-06-29 11:43:45 -05002#include <chrono>
Matt Spinler7f88fe62017-04-10 14:39:02 -05003#include <vector>
Matthew Barthe0ca13e2017-06-13 16:29:09 -05004#include <algorithm>
Matt Spinler7f88fe62017-04-10 14:39:02 -05005#include <sdbusplus/bus.hpp>
6#include "fan.hpp"
7#include "types.hpp"
Matthew Barth8600d9a2017-06-23 14:38:05 -05008#include "timer.hpp"
Matt Spinler7f88fe62017-04-10 14:39:02 -05009
10namespace phosphor
11{
12namespace fan
13{
14namespace control
15{
16
17/**
Matthew Barth14184132017-05-19 14:37:30 -050018 * The mode fan control will run in:
19 * - init - only do the initialization steps
20 * - control - run normal control algorithms
21 */
22enum class Mode
23{
24 init,
25 control
26};
27
28/**
Matt Spinler7f88fe62017-04-10 14:39:02 -050029 * @class Represents a fan control zone, which is a group of fans
30 * that behave the same.
31 */
32class Zone
33{
34 public:
35
36 Zone() = delete;
37 Zone(const Zone&) = delete;
38 Zone(Zone&&) = default;
39 Zone& operator=(const Zone&) = delete;
40 Zone& operator=(Zone&&) = delete;
41 ~Zone() = default;
42
43 /**
44 * Constructor
45 * Creates the appropriate fan objects based on
46 * the zone definition data passed in.
47 *
Matthew Barth14184132017-05-19 14:37:30 -050048 * @param[in] mode - mode of fan control
Matt Spinler7f88fe62017-04-10 14:39:02 -050049 * @param[in] bus - the dbus object
Matthew Barth8600d9a2017-06-23 14:38:05 -050050 * @param[in] events - sd_event pointer
Matt Spinler7f88fe62017-04-10 14:39:02 -050051 * @param[in] def - the fan zone definition data
52 */
Matthew Barth14184132017-05-19 14:37:30 -050053 Zone(Mode mode,
54 sdbusplus::bus::bus& bus,
Matthew Barth8600d9a2017-06-23 14:38:05 -050055 phosphor::fan::event::EventPtr& events,
Matt Spinler7f88fe62017-04-10 14:39:02 -050056 const ZoneDefinition& def);
57
58 /**
59 * Sets all fans in the zone to the speed
Matthew Barth60b00762017-08-15 13:39:06 -050060 * passed in when the zone is active
Matt Spinler7f88fe62017-04-10 14:39:02 -050061 *
62 * @param[in] speed - the fan speed
63 */
64 void setSpeed(uint64_t speed);
65
66 /**
Matthew Barth60b00762017-08-15 13:39:06 -050067 * Sets the zone to full speed regardless of zone's active state
Matt Spinler7f88fe62017-04-10 14:39:02 -050068 */
Matthew Barth60b00762017-08-15 13:39:06 -050069 void setFullSpeed();
Matt Spinler7f88fe62017-04-10 14:39:02 -050070
Matthew Barth38a93a82017-05-11 14:12:27 -050071 /**
Matthew Barth861d77c2017-05-22 14:18:25 -050072 * @brief Sets the automatic fan control allowed active state
73 *
74 * @param[in] group - A group that affects the active state
75 * @param[in] isActiveAllow - Active state according to group
76 */
77 void setActiveAllow(const Group* group, bool isActiveAllow);
78
79 /**
Matthew Barth38a93a82017-05-11 14:12:27 -050080 * @brief Sets a given object's property value
81 *
82 * @param[in] object - Name of the object containing the property
Matthew Barthcec5ab72017-06-02 15:20:56 -050083 * @param[in] interface - Interface name containing the property
Matthew Barth38a93a82017-05-11 14:12:27 -050084 * @param[in] property - Property name
85 * @param[in] value - Property value
86 */
Matthew Barth9e741ed2017-06-02 16:29:09 -050087 template <typename T>
Matthew Barth38a93a82017-05-11 14:12:27 -050088 void setPropertyValue(const char* object,
Matthew Barthcec5ab72017-06-02 15:20:56 -050089 const char* interface,
Matthew Barth38a93a82017-05-11 14:12:27 -050090 const char* property,
Matthew Barth9e741ed2017-06-02 16:29:09 -050091 T value)
Matthew Barth38a93a82017-05-11 14:12:27 -050092 {
Matthew Barthcec5ab72017-06-02 15:20:56 -050093 _properties[object][interface][property] = value;
Matthew Barth38a93a82017-05-11 14:12:27 -050094 };
95
Matthew Barth17d1fe22017-05-11 15:00:36 -050096 /**
97 * @brief Get the value of an object's property
98 *
99 * @param[in] object - Name of the object containing the property
Matthew Barthcec5ab72017-06-02 15:20:56 -0500100 * @param[in] interface - Interface name containing the property
Matthew Barth17d1fe22017-05-11 15:00:36 -0500101 * @param[in] property - Property name
102 *
103 * @return - The property value
104 */
Matthew Barth9e741ed2017-06-02 16:29:09 -0500105 template <typename T>
Matthew Barth17d1fe22017-05-11 15:00:36 -0500106 inline auto getPropertyValue(const std::string& object,
Matthew Barthcec5ab72017-06-02 15:20:56 -0500107 const std::string& interface,
Matthew Barth17d1fe22017-05-11 15:00:36 -0500108 const std::string& property)
109 {
Matthew Barth9e741ed2017-06-02 16:29:09 -0500110 return sdbusplus::message::variant_ns::get<T>(
Matthew Barthbc651602017-08-10 16:59:43 -0500111 _properties.at(object).at(interface).at(property));
Matthew Barth17d1fe22017-05-11 15:00:36 -0500112 };
113
Matthew Barth1de66622017-06-12 13:13:02 -0500114 /**
Matthew Barth604329e2017-08-04 11:18:28 -0500115 * @brief Get the object's property variant
116 *
117 * @param[in] object - Name of the object containing the property
118 * @param[in] interface - Interface name containing the property
119 * @param[in] property - Property name
120 *
121 * @return - The property variant
122 */
123 inline auto getPropValueVariant(const std::string& object,
124 const std::string& interface,
125 const std::string& property)
126 {
127 return _properties.at(object).at(interface).at(property);
128 };
129
130 /**
131 * @brief Initialize a set speed event properties and actions
132 *
133 * @param[in] event - Set speed event
134 */
135 void initEvent(const SetSpeedEvent& event);
136
137 /**
Matthew Barthf6b76d82017-08-04 12:58:02 -0500138 * @brief Removes all the set speed event properties and actions
139 *
140 * @param[in] event - Set speed event
141 */
142 void removeEvent(const SetSpeedEvent& event);
143
144 /**
Matthew Barth1de66622017-06-12 13:13:02 -0500145 * @brief Get the default floor speed
146 *
147 * @return - The defined default floor speed
148 */
149 inline auto getDefFloor()
150 {
151 return _defFloorSpeed;
152 };
153
Matthew Barth4af419c2017-06-12 13:39:31 -0500154 /**
Matthew Barthe0ca13e2017-06-13 16:29:09 -0500155 * @brief Get the ceiling speed
156 *
157 * @return - The current ceiling speed
158 */
159 inline auto& getCeiling() const
160 {
161 return _ceilingSpeed;
162 };
163
164 /**
165 * @brief Set the ceiling speed to the given speed
166 *
167 * @param[in] speed - Speed to set the ceiling to
168 */
169 inline void setCeiling(uint64_t speed)
170 {
171 _ceilingSpeed = speed;
172 };
173
174 /**
175 * @brief Swaps the ceiling key value with what's given and
176 * returns the value that was swapped.
177 *
178 * @param[in] keyValue - New ceiling key value
179 *
180 * @return - Ceiling key value prior to swapping
181 */
182 inline auto swapCeilingKeyValue(int64_t keyValue)
183 {
184 std::swap(_ceilingKeyValue, keyValue);
185 return keyValue;
186 };
187
Matthew Barth24623522017-06-21 14:09:57 -0500188 /**
189 * @brief Get the increase speed delta
190 *
191 * @return - The current increase speed delta
192 */
193 inline auto& getIncSpeedDelta() const
194 {
195 return _incSpeedDelta;
196 };
197
Matthew Barth240397b2017-06-22 11:23:30 -0500198 /**
Matthew Barth0ce99d82017-06-22 15:07:29 -0500199 * @brief Get the decrease speed delta
200 *
201 * @return - The current decrease speed delta
202 */
203 inline auto& getDecSpeedDelta() const
204 {
205 return _decSpeedDelta;
206 };
207
208 /**
Matthew Barthb4a7cb92017-06-28 15:29:50 -0500209 * @brief Set the floor speed to the given speed and increase target
210 * speed to the floor when target is below floor.
211 *
212 * @param[in] speed - Speed to set the floor to
213 */
214 void setFloor(uint64_t speed);
215
216 /**
Matthew Barth1bfdc422017-09-14 16:23:28 -0500217 * @brief Set the requested speed base to be used as the speed to
218 * base a new requested speed target from
219 *
220 * @param[in] speedBase - Base speed value to use
221 */
222 inline void setRequestSpeedBase(uint64_t speedBase)
223 {
224 _requestSpeedBase = speedBase;
225 };
226
227 /**
Matthew Barth240397b2017-06-22 11:23:30 -0500228 * @brief Calculate the requested target speed from the given delta
229 * and increase the fan speeds, not going above the ceiling.
230 *
231 * @param[in] targetDelta - The delta to increase the target speed by
232 */
233 void requestSpeedIncrease(uint64_t targetDelta);
234
Matthew Barth0ce99d82017-06-22 15:07:29 -0500235 /**
236 * @brief Calculate the requested target speed from the given delta
237 * and increase the fan speeds, not going above the ceiling.
238 *
239 * @param[in] targetDelta - The delta to increase the target speed by
240 */
241 void requestSpeedDecrease(uint64_t targetDelta);
242
Matthew Barth8600d9a2017-06-23 14:38:05 -0500243 /**
Matthew Barth1ee48f22017-06-27 15:14:48 -0500244 * @brief Callback function for the increase timer that delays
245 * processing of requested speed increases while fans are increasing
246 */
247 void incTimerExpired();
248
249 /**
Matthew Barth8600d9a2017-06-23 14:38:05 -0500250 * @brief Callback function for the decrease timer that processes any
251 * requested speed decreases if allowed
252 */
253 void decTimerExpired();
254
Matthew Barth90149802017-08-15 10:51:37 -0500255 /**
256 * @brief Callback function for event timers that processes the given
Matthew Barthf9201ab2017-09-11 16:07:58 -0500257 * actions for a group
Matthew Barth90149802017-08-15 10:51:37 -0500258 *
Matthew Barthf9201ab2017-09-11 16:07:58 -0500259 * @param[in] eventGroup - Group to process actions on
260 * @param[in] eventActions - List of event actions to run
Matthew Barth90149802017-08-15 10:51:37 -0500261 */
Matthew Barthf9201ab2017-09-11 16:07:58 -0500262 void timerExpired(Group eventGroup, std::vector<Action> eventActions);
Matthew Barth90149802017-08-15 10:51:37 -0500263
Matt Spinler7f88fe62017-04-10 14:39:02 -0500264 private:
265
266 /**
267 * The dbus object
268 */
269 sdbusplus::bus::bus& _bus;
270
271 /**
272 * Full speed for the zone
273 */
274 const uint64_t _fullSpeed;
275
276 /**
277 * The zone number
278 */
279 const size_t _zoneNum;
280
281 /**
Matthew Barth1de66622017-06-12 13:13:02 -0500282 * The default floor speed for the zone
283 */
284 const uint64_t _defFloorSpeed;
285
286 /**
Matthew Barthe0ca13e2017-06-13 16:29:09 -0500287 * The default ceiling speed for the zone
288 */
289 const uint64_t _defCeilingSpeed;
290
291 /**
Matthew Barth4af419c2017-06-12 13:39:31 -0500292 * The floor speed to not go below
293 */
294 uint64_t _floorSpeed = _defFloorSpeed;
295
296 /**
Matthew Barthe0ca13e2017-06-13 16:29:09 -0500297 * The ceiling speed to not go above
298 */
299 uint64_t _ceilingSpeed = _defCeilingSpeed;
300
301 /**
302 * The previous sensor value for calculating the ceiling
303 */
304 int64_t _ceilingKeyValue = 0;
305
306 /**
Matthew Barth861d77c2017-05-22 14:18:25 -0500307 * Automatic fan control active state
308 */
309 bool _isActive = true;
310
311 /**
Matthew Barth240397b2017-06-22 11:23:30 -0500312 * Target speed for this zone
313 */
314 uint64_t _targetSpeed = _fullSpeed;
315
316 /**
Matthew Barth24623522017-06-21 14:09:57 -0500317 * Speed increase delta
318 */
319 uint64_t _incSpeedDelta = 0;
320
321 /**
Matthew Barth0ce99d82017-06-22 15:07:29 -0500322 * Speed decrease delta
323 */
324 uint64_t _decSpeedDelta = 0;
325
326 /**
Matthew Barth1bfdc422017-09-14 16:23:28 -0500327 * Requested speed base
328 */
329 uint64_t _requestSpeedBase = 0;
330
331 /**
Matthew Bartha9561842017-06-29 11:43:45 -0500332 * Speed increase delay in seconds
333 */
334 std::chrono::seconds _incDelay;
335
336 /**
337 * Speed decrease interval in seconds
338 */
339 std::chrono::seconds _decInterval;
340
341 /**
Matthew Barth1ee48f22017-06-27 15:14:48 -0500342 * The increase timer object
343 */
344 phosphor::fan::util::Timer _incTimer;
345
346 /**
Matthew Barth8600d9a2017-06-23 14:38:05 -0500347 * The decrease timer object
348 */
349 phosphor::fan::util::Timer _decTimer;
350
351 /**
Matthew Barth90149802017-08-15 10:51:37 -0500352 * Dbus event used on set speed event timers
353 */
354 phosphor::fan::event::EventPtr& _sdEvents;
355
356 /**
Matt Spinler7f88fe62017-04-10 14:39:02 -0500357 * The vector of fans in this zone
358 */
359 std::vector<std::unique_ptr<Fan>> _fans;
Matthew Barth38a93a82017-05-11 14:12:27 -0500360
361 /**
362 * @brief Map of object property values
363 */
Matthew Barthcec5ab72017-06-02 15:20:56 -0500364 std::map<std::string,
365 std::map<std::string,
366 std::map<std::string,
Matthew Barth9e741ed2017-06-02 16:29:09 -0500367 PropertyVariantType>>> _properties;
Matthew Barth38a93a82017-05-11 14:12:27 -0500368
369 /**
Matthew Barth861d77c2017-05-22 14:18:25 -0500370 * @brief Map of active fan control allowed by groups
371 */
Matthew Barth60b00762017-08-15 13:39:06 -0500372 std::map<const Group, bool> _active;
Matthew Barth861d77c2017-05-22 14:18:25 -0500373
374 /**
Matthew Barthf6b76d82017-08-04 12:58:02 -0500375 * @brief List of signal event arguments and Dbus matches for callbacks
Matthew Barth38a93a82017-05-11 14:12:27 -0500376 */
Matthew Barthf6b76d82017-08-04 12:58:02 -0500377 std::vector<SignalEvent> _signalEvents;
Matthew Barth38a93a82017-05-11 14:12:27 -0500378
379 /**
Matthew Barth90149802017-08-15 10:51:37 -0500380 * @brief List of timers for events
381 */
382 std::vector<std::unique_ptr<phosphor::fan::util::Timer>> _timerEvents;
383
384 /**
Matthew Barth4e728542017-09-14 16:47:55 -0500385 * @brief Get the request speed base if defined, otherwise the
386 * the current target speed is returned
387 *
388 * @return - The request speed base or current target speed
389 */
390 inline auto getRequestSpeedBase() const
391 {
392 return (_requestSpeedBase != 0) ? _requestSpeedBase : _targetSpeed;
393 };
394
395 /**
Matthew Barth1bf0ce42017-06-23 16:16:30 -0500396 * @brief Refresh the given property's cached value
397 *
398 * @param[in] bus - the bus to use
399 * @param[in] path - the dbus path name
400 * @param[in] iface - the dbus interface name
401 * @param[in] prop - the property name
402 */
403 void refreshProperty(sdbusplus::bus::bus& bus,
404 const std::string& path,
405 const std::string& iface,
406 const std::string& prop);
407
408 /**
Matthew Barthdf3e8d62017-05-31 11:07:24 -0500409 * @brief Get a property value from the path/interface given
410 *
411 * @param[in] bus - the bus to use
412 * @param[in] path - the dbus path name
413 * @param[in] iface - the dbus interface name
414 * @param[in] prop - the property name
415 * @param[out] value - the value of the property
416 */
Matthew Barthdf3e8d62017-05-31 11:07:24 -0500417 static void getProperty(sdbusplus::bus::bus& bus,
418 const std::string& path,
419 const std::string& iface,
420 const std::string& prop,
Matthew Barth9e741ed2017-06-02 16:29:09 -0500421 PropertyVariantType& value);
Matthew Barthdf3e8d62017-05-31 11:07:24 -0500422
423 /**
Matthew Barth34f1bda2017-05-31 13:45:36 -0500424 * @brief Dbus signal change callback handler
Matthew Barth38a93a82017-05-11 14:12:27 -0500425 *
Matthew Barth34f1bda2017-05-31 13:45:36 -0500426 * @param[in] msg - Expanded sdbusplus message data
427 * @param[in] eventData - The single event's data
Matthew Barth38a93a82017-05-11 14:12:27 -0500428 */
Matthew Barth34f1bda2017-05-31 13:45:36 -0500429 void handleEvent(sdbusplus::message::message& msg,
430 const EventData* eventData);
Matt Spinler7f88fe62017-04-10 14:39:02 -0500431};
432
433}
434}
435}