blob: 956593334ba572aa0b3d6a9d7118c705dbe830c3 [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 Barthbfb1a562017-10-05 17:03:40 -05004#include <cassert>
Matthew Barthe0ca13e2017-06-13 16:29:09 -05005#include <algorithm>
Matt Spinler7f88fe62017-04-10 14:39:02 -05006#include <sdbusplus/bus.hpp>
7#include "fan.hpp"
8#include "types.hpp"
Matthew Barth8600d9a2017-06-23 14:38:05 -05009#include "timer.hpp"
Matt Spinler7f88fe62017-04-10 14:39:02 -050010
11namespace phosphor
12{
13namespace fan
14{
15namespace control
16{
17
18/**
Matthew Barth14184132017-05-19 14:37:30 -050019 * The mode fan control will run in:
20 * - init - only do the initialization steps
21 * - control - run normal control algorithms
22 */
23enum class Mode
24{
25 init,
26 control
27};
28
29/**
Matt Spinler7f88fe62017-04-10 14:39:02 -050030 * @class Represents a fan control zone, which is a group of fans
31 * that behave the same.
32 */
33class Zone
34{
35 public:
36
37 Zone() = delete;
38 Zone(const Zone&) = delete;
39 Zone(Zone&&) = default;
40 Zone& operator=(const Zone&) = delete;
41 Zone& operator=(Zone&&) = delete;
42 ~Zone() = default;
43
44 /**
45 * Constructor
46 * Creates the appropriate fan objects based on
47 * the zone definition data passed in.
48 *
Matthew Barth14184132017-05-19 14:37:30 -050049 * @param[in] mode - mode of fan control
Matt Spinler7f88fe62017-04-10 14:39:02 -050050 * @param[in] bus - the dbus object
Matthew Barth8600d9a2017-06-23 14:38:05 -050051 * @param[in] events - sd_event pointer
Matt Spinler7f88fe62017-04-10 14:39:02 -050052 * @param[in] def - the fan zone definition data
53 */
Matthew Barth14184132017-05-19 14:37:30 -050054 Zone(Mode mode,
55 sdbusplus::bus::bus& bus,
Matthew Barth8600d9a2017-06-23 14:38:05 -050056 phosphor::fan::event::EventPtr& events,
Matt Spinler7f88fe62017-04-10 14:39:02 -050057 const ZoneDefinition& def);
58
59 /**
60 * Sets all fans in the zone to the speed
Matthew Barth60b00762017-08-15 13:39:06 -050061 * passed in when the zone is active
Matt Spinler7f88fe62017-04-10 14:39:02 -050062 *
63 * @param[in] speed - the fan speed
64 */
65 void setSpeed(uint64_t speed);
66
67 /**
Matthew Barth60b00762017-08-15 13:39:06 -050068 * Sets the zone to full speed regardless of zone's active state
Matt Spinler7f88fe62017-04-10 14:39:02 -050069 */
Matthew Barth60b00762017-08-15 13:39:06 -050070 void setFullSpeed();
Matt Spinler7f88fe62017-04-10 14:39:02 -050071
Matthew Barth38a93a82017-05-11 14:12:27 -050072 /**
Matthew Barth861d77c2017-05-22 14:18:25 -050073 * @brief Sets the automatic fan control allowed active state
74 *
75 * @param[in] group - A group that affects the active state
76 * @param[in] isActiveAllow - Active state according to group
77 */
78 void setActiveAllow(const Group* group, bool isActiveAllow);
79
80 /**
Matthew Barth98726c42017-10-17 10:35:20 -050081 * @brief Sets the floor change allowed state
82 *
83 * @param[in] group - A group that affects floor changes
84 * @param[in] isAllow - Allow state according to group
85 */
86 inline void setFloorChangeAllow(const Group* group, bool isAllow)
87 {
88 _floorChange[*(group)] = isAllow;
89 }
90
91 /**
Matthew Barth38a93a82017-05-11 14:12:27 -050092 * @brief Sets a given object's property value
93 *
94 * @param[in] object - Name of the object containing the property
Matthew Barthcec5ab72017-06-02 15:20:56 -050095 * @param[in] interface - Interface name containing the property
Matthew Barth38a93a82017-05-11 14:12:27 -050096 * @param[in] property - Property name
97 * @param[in] value - Property value
98 */
Matthew Barth9e741ed2017-06-02 16:29:09 -050099 template <typename T>
Matthew Barth38a93a82017-05-11 14:12:27 -0500100 void setPropertyValue(const char* object,
Matthew Barthcec5ab72017-06-02 15:20:56 -0500101 const char* interface,
Matthew Barth38a93a82017-05-11 14:12:27 -0500102 const char* property,
Matthew Barth9e741ed2017-06-02 16:29:09 -0500103 T value)
Matthew Barth38a93a82017-05-11 14:12:27 -0500104 {
Matthew Barthcec5ab72017-06-02 15:20:56 -0500105 _properties[object][interface][property] = value;
Matthew Barth38a93a82017-05-11 14:12:27 -0500106 };
107
Matthew Barth17d1fe22017-05-11 15:00:36 -0500108 /**
109 * @brief Get the value of an object's property
110 *
111 * @param[in] object - Name of the object containing the property
Matthew Barthcec5ab72017-06-02 15:20:56 -0500112 * @param[in] interface - Interface name containing the property
Matthew Barth17d1fe22017-05-11 15:00:36 -0500113 * @param[in] property - Property name
114 *
115 * @return - The property value
116 */
Matthew Barth9e741ed2017-06-02 16:29:09 -0500117 template <typename T>
Matthew Barth17d1fe22017-05-11 15:00:36 -0500118 inline auto getPropertyValue(const std::string& object,
Matthew Barthcec5ab72017-06-02 15:20:56 -0500119 const std::string& interface,
Matthew Barth17d1fe22017-05-11 15:00:36 -0500120 const std::string& property)
121 {
Matthew Barth9e741ed2017-06-02 16:29:09 -0500122 return sdbusplus::message::variant_ns::get<T>(
Matthew Barthbc651602017-08-10 16:59:43 -0500123 _properties.at(object).at(interface).at(property));
Matthew Barth17d1fe22017-05-11 15:00:36 -0500124 };
125
Matthew Barth1de66622017-06-12 13:13:02 -0500126 /**
Matthew Barth604329e2017-08-04 11:18:28 -0500127 * @brief Get the object's property variant
128 *
129 * @param[in] object - Name of the object containing the property
130 * @param[in] interface - Interface name containing the property
131 * @param[in] property - Property name
132 *
133 * @return - The property variant
134 */
135 inline auto getPropValueVariant(const std::string& object,
136 const std::string& interface,
137 const std::string& property)
138 {
139 return _properties.at(object).at(interface).at(property);
140 };
141
142 /**
Matthew Barthe59fdf72017-09-27 09:33:42 -0500143 * @brief Set or update a service name owner in use
144 *
145 * @param[in] group - Group associated with service
146 * @param[in] name - Service name
147 * @param[in] hasOwner - Whether the service is owned or not
148 */
149 void setServiceOwner(const Group* group,
150 const std::string& name,
151 const bool hasOwner);
152
153 /**
Matthew Barth480787c2017-11-06 11:00:00 -0600154 * @brief Set or update all services for a group
155 *
156 * @param[in] group - Group to get service names for
157 */
158 void setServices(const Group* group);
159
160 /**
Matthew Barthbfb1a562017-10-05 17:03:40 -0500161 * @brief Get the group's list of service names
162 *
163 * @param[in] group - Group to get service names for
164 *
165 * @return - The list of service names
166 */
167 inline auto getGroupServices(const Group* group)
168 {
169 return _services.at(*group);
170 }
171
172 /**
Matthew Barth604329e2017-08-04 11:18:28 -0500173 * @brief Initialize a set speed event properties and actions
174 *
175 * @param[in] event - Set speed event
176 */
177 void initEvent(const SetSpeedEvent& event);
178
179 /**
Matthew Barthf6b76d82017-08-04 12:58:02 -0500180 * @brief Removes all the set speed event properties and actions
181 *
182 * @param[in] event - Set speed event
183 */
184 void removeEvent(const SetSpeedEvent& event);
185
186 /**
Matthew Barth1de66622017-06-12 13:13:02 -0500187 * @brief Get the default floor speed
188 *
189 * @return - The defined default floor speed
190 */
191 inline auto getDefFloor()
192 {
193 return _defFloorSpeed;
194 };
195
Matthew Barth4af419c2017-06-12 13:39:31 -0500196 /**
Matthew Barthe0ca13e2017-06-13 16:29:09 -0500197 * @brief Get the ceiling speed
198 *
199 * @return - The current ceiling speed
200 */
201 inline auto& getCeiling() const
202 {
203 return _ceilingSpeed;
204 };
205
206 /**
207 * @brief Set the ceiling speed to the given speed
208 *
209 * @param[in] speed - Speed to set the ceiling to
210 */
211 inline void setCeiling(uint64_t speed)
212 {
213 _ceilingSpeed = speed;
214 };
215
216 /**
217 * @brief Swaps the ceiling key value with what's given and
218 * returns the value that was swapped.
219 *
220 * @param[in] keyValue - New ceiling key value
221 *
222 * @return - Ceiling key value prior to swapping
223 */
224 inline auto swapCeilingKeyValue(int64_t keyValue)
225 {
226 std::swap(_ceilingKeyValue, keyValue);
227 return keyValue;
228 };
229
Matthew Barth24623522017-06-21 14:09:57 -0500230 /**
231 * @brief Get the increase speed delta
232 *
233 * @return - The current increase speed delta
234 */
235 inline auto& getIncSpeedDelta() const
236 {
237 return _incSpeedDelta;
238 };
239
Matthew Barth240397b2017-06-22 11:23:30 -0500240 /**
Matthew Barth0ce99d82017-06-22 15:07:29 -0500241 * @brief Get the decrease speed delta
242 *
243 * @return - The current decrease speed delta
244 */
245 inline auto& getDecSpeedDelta() const
246 {
247 return _decSpeedDelta;
248 };
249
250 /**
Matthew Barthb4a7cb92017-06-28 15:29:50 -0500251 * @brief Set the floor speed to the given speed and increase target
Matthew Barth98726c42017-10-17 10:35:20 -0500252 * speed to the floor when target is below floor where floor changes
253 * are allowed.
Matthew Barthb4a7cb92017-06-28 15:29:50 -0500254 *
255 * @param[in] speed - Speed to set the floor to
256 */
257 void setFloor(uint64_t speed);
258
259 /**
Matthew Barth1bfdc422017-09-14 16:23:28 -0500260 * @brief Set the requested speed base to be used as the speed to
261 * base a new requested speed target from
262 *
263 * @param[in] speedBase - Base speed value to use
264 */
265 inline void setRequestSpeedBase(uint64_t speedBase)
266 {
267 _requestSpeedBase = speedBase;
268 };
269
270 /**
Matthew Barth240397b2017-06-22 11:23:30 -0500271 * @brief Calculate the requested target speed from the given delta
272 * and increase the fan speeds, not going above the ceiling.
273 *
274 * @param[in] targetDelta - The delta to increase the target speed by
275 */
276 void requestSpeedIncrease(uint64_t targetDelta);
277
Matthew Barth0ce99d82017-06-22 15:07:29 -0500278 /**
279 * @brief Calculate the requested target speed from the given delta
280 * and increase the fan speeds, not going above the ceiling.
281 *
282 * @param[in] targetDelta - The delta to increase the target speed by
283 */
284 void requestSpeedDecrease(uint64_t targetDelta);
285
Matthew Barth8600d9a2017-06-23 14:38:05 -0500286 /**
Matthew Barth1ee48f22017-06-27 15:14:48 -0500287 * @brief Callback function for the increase timer that delays
288 * processing of requested speed increases while fans are increasing
289 */
290 void incTimerExpired();
291
292 /**
Matthew Barth8600d9a2017-06-23 14:38:05 -0500293 * @brief Callback function for the decrease timer that processes any
294 * requested speed decreases if allowed
295 */
296 void decTimerExpired();
297
Matthew Barth90149802017-08-15 10:51:37 -0500298 /**
Matthew Barthbfb1a562017-10-05 17:03:40 -0500299 * @brief Get the event pointer used with this zone's timers
300 *
301 * @return - The Dbus event pointer for timers
302 */
303 inline auto& getEventPtr()
304 {
305 return _sdEvents;
306 }
307
308 /**
309 * @brief Get the list of timer events
310 *
311 * @return - List of timer events
312 */
313 inline auto& getTimerEvents()
314 {
315 return _timerEvents;
316 }
317
318 /**
319 * @brief Find the first instance of a timer event
320 *
321 * @param[in] eventGroup - Group associated with a timer
322 * @param[in] eventActions - List of actions associated with a timer
323 *
324 * @return - Iterator to the timer event
325 */
326 std::vector<TimerEvent>::iterator findTimer(
327 const Group& eventGroup,
328 const std::vector<Action>& eventActions);
329
330 /**
331 * @brief Add a timer to the list of timer based events
332 *
333 * @param[in] data - Event data for timer
334 * @param[in] timer - Timer to be added
335 */
336 inline void addTimer(
337 std::unique_ptr<EventData>&& data,
338 std::unique_ptr<phosphor::fan::util::Timer>&& timer)
339 {
340 _timerEvents.emplace_back(std::move(data), std::move(timer));
341 };
342
343 /**
344 * @brief Remove the given timer event
345 *
346 * @param[in] teIter - Iterator pointing to the timer event to remove
347 */
348 inline void removeTimer(std::vector<TimerEvent>::iterator& teIter)
349 {
350 assert(teIter != std::end(_timerEvents));
351 std::get<timerEventDataPos>(*teIter).reset();
352 std::get<timerTimerPos>(*teIter).reset();
353 _timerEvents.erase(teIter);
354 }
355
356 /**
Matthew Barth90149802017-08-15 10:51:37 -0500357 * @brief Callback function for event timers that processes the given
Matthew Barthf9201ab2017-09-11 16:07:58 -0500358 * actions for a group
Matthew Barth90149802017-08-15 10:51:37 -0500359 *
Matthew Barthf9201ab2017-09-11 16:07:58 -0500360 * @param[in] eventGroup - Group to process actions on
361 * @param[in] eventActions - List of event actions to run
Matthew Barth90149802017-08-15 10:51:37 -0500362 */
Matthew Barthf9201ab2017-09-11 16:07:58 -0500363 void timerExpired(Group eventGroup, std::vector<Action> eventActions);
Matthew Barth90149802017-08-15 10:51:37 -0500364
Matt Spinler7f88fe62017-04-10 14:39:02 -0500365 private:
366
367 /**
368 * The dbus object
369 */
370 sdbusplus::bus::bus& _bus;
371
372 /**
373 * Full speed for the zone
374 */
375 const uint64_t _fullSpeed;
376
377 /**
378 * The zone number
379 */
380 const size_t _zoneNum;
381
382 /**
Matthew Barth1de66622017-06-12 13:13:02 -0500383 * The default floor speed for the zone
384 */
385 const uint64_t _defFloorSpeed;
386
387 /**
Matthew Barthe0ca13e2017-06-13 16:29:09 -0500388 * The default ceiling speed for the zone
389 */
390 const uint64_t _defCeilingSpeed;
391
392 /**
Matthew Barth4af419c2017-06-12 13:39:31 -0500393 * The floor speed to not go below
394 */
395 uint64_t _floorSpeed = _defFloorSpeed;
396
397 /**
Matthew Barthe0ca13e2017-06-13 16:29:09 -0500398 * The ceiling speed to not go above
399 */
400 uint64_t _ceilingSpeed = _defCeilingSpeed;
401
402 /**
403 * The previous sensor value for calculating the ceiling
404 */
405 int64_t _ceilingKeyValue = 0;
406
407 /**
Matthew Barth861d77c2017-05-22 14:18:25 -0500408 * Automatic fan control active state
409 */
410 bool _isActive = true;
411
412 /**
Matthew Barth240397b2017-06-22 11:23:30 -0500413 * Target speed for this zone
414 */
415 uint64_t _targetSpeed = _fullSpeed;
416
417 /**
Matthew Barth24623522017-06-21 14:09:57 -0500418 * Speed increase delta
419 */
420 uint64_t _incSpeedDelta = 0;
421
422 /**
Matthew Barth0ce99d82017-06-22 15:07:29 -0500423 * Speed decrease delta
424 */
425 uint64_t _decSpeedDelta = 0;
426
427 /**
Matthew Barth1bfdc422017-09-14 16:23:28 -0500428 * Requested speed base
429 */
430 uint64_t _requestSpeedBase = 0;
431
432 /**
Matthew Bartha9561842017-06-29 11:43:45 -0500433 * Speed increase delay in seconds
434 */
435 std::chrono::seconds _incDelay;
436
437 /**
438 * Speed decrease interval in seconds
439 */
440 std::chrono::seconds _decInterval;
441
442 /**
Matthew Barth1ee48f22017-06-27 15:14:48 -0500443 * The increase timer object
444 */
445 phosphor::fan::util::Timer _incTimer;
446
447 /**
Matthew Barth8600d9a2017-06-23 14:38:05 -0500448 * The decrease timer object
449 */
450 phosphor::fan::util::Timer _decTimer;
451
452 /**
Matthew Barth90149802017-08-15 10:51:37 -0500453 * Dbus event used on set speed event timers
454 */
455 phosphor::fan::event::EventPtr& _sdEvents;
456
457 /**
Matt Spinler7f88fe62017-04-10 14:39:02 -0500458 * The vector of fans in this zone
459 */
460 std::vector<std::unique_ptr<Fan>> _fans;
Matthew Barth38a93a82017-05-11 14:12:27 -0500461
462 /**
463 * @brief Map of object property values
464 */
Matthew Barthcec5ab72017-06-02 15:20:56 -0500465 std::map<std::string,
466 std::map<std::string,
467 std::map<std::string,
Matthew Barth9e741ed2017-06-02 16:29:09 -0500468 PropertyVariantType>>> _properties;
Matthew Barth38a93a82017-05-11 14:12:27 -0500469
470 /**
Matthew Barth861d77c2017-05-22 14:18:25 -0500471 * @brief Map of active fan control allowed by groups
472 */
Matthew Barth60b00762017-08-15 13:39:06 -0500473 std::map<const Group, bool> _active;
Matthew Barth861d77c2017-05-22 14:18:25 -0500474
475 /**
Matthew Barth98726c42017-10-17 10:35:20 -0500476 * @brief Map of floor change allowed by groups
477 */
478 std::map<const Group, bool> _floorChange;
479
480 /**
Matthew Barthe59fdf72017-09-27 09:33:42 -0500481 * @brief Map of group service names
482 */
483 std::map<const Group, std::vector<Service>> _services;
484
485 /**
Matthew Barthf6b76d82017-08-04 12:58:02 -0500486 * @brief List of signal event arguments and Dbus matches for callbacks
Matthew Barth38a93a82017-05-11 14:12:27 -0500487 */
Matthew Barthf6b76d82017-08-04 12:58:02 -0500488 std::vector<SignalEvent> _signalEvents;
Matthew Barth38a93a82017-05-11 14:12:27 -0500489
490 /**
Matthew Barth90149802017-08-15 10:51:37 -0500491 * @brief List of timers for events
492 */
Matthew Barthbfb1a562017-10-05 17:03:40 -0500493 std::vector<TimerEvent> _timerEvents;
Matthew Barth90149802017-08-15 10:51:37 -0500494
495 /**
Matthew Barth4e728542017-09-14 16:47:55 -0500496 * @brief Get the request speed base if defined, otherwise the
497 * the current target speed is returned
498 *
499 * @return - The request speed base or current target speed
500 */
501 inline auto getRequestSpeedBase() const
502 {
503 return (_requestSpeedBase != 0) ? _requestSpeedBase : _targetSpeed;
504 };
505
506 /**
Matthew Barth34f1bda2017-05-31 13:45:36 -0500507 * @brief Dbus signal change callback handler
Matthew Barth38a93a82017-05-11 14:12:27 -0500508 *
Matthew Barth34f1bda2017-05-31 13:45:36 -0500509 * @param[in] msg - Expanded sdbusplus message data
510 * @param[in] eventData - The single event's data
Matthew Barth38a93a82017-05-11 14:12:27 -0500511 */
Matthew Barth34f1bda2017-05-31 13:45:36 -0500512 void handleEvent(sdbusplus::message::message& msg,
513 const EventData* eventData);
Matt Spinler7f88fe62017-04-10 14:39:02 -0500514};
515
516}
517}
518}