blob: 9786db4088c4a816a4e0412798c34330bb633e3f [file] [log] [blame]
Matt Spinler7f88fe62017-04-10 14:39:02 -05001#pragma once
2#include <vector>
Matthew Barthe0ca13e2017-06-13 16:29:09 -05003#include <algorithm>
Matt Spinler7f88fe62017-04-10 14:39:02 -05004#include <sdbusplus/bus.hpp>
Matthew Barth38a93a82017-05-11 14:12:27 -05005#include <sdbusplus/server.hpp>
Matt Spinler7f88fe62017-04-10 14:39:02 -05006#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
60 * passed in
61 *
62 * @param[in] speed - the fan speed
63 */
64 void setSpeed(uint64_t speed);
65
66 /**
67 * Sets the zone to full speed
68 */
69 inline void setFullSpeed()
70 {
71 if (_fullSpeed != 0)
72 {
73 setSpeed(_fullSpeed);
74 }
75 }
76
Matthew Barth38a93a82017-05-11 14:12:27 -050077 /**
Matthew Barth861d77c2017-05-22 14:18:25 -050078 * @brief Sets the automatic fan control allowed active state
79 *
80 * @param[in] group - A group that affects the active state
81 * @param[in] isActiveAllow - Active state according to group
82 */
83 void setActiveAllow(const Group* group, bool isActiveAllow);
84
85 /**
Matthew Barth38a93a82017-05-11 14:12:27 -050086 * @brief Sets a given object's property value
87 *
88 * @param[in] object - Name of the object containing the property
Matthew Barthcec5ab72017-06-02 15:20:56 -050089 * @param[in] interface - Interface name containing the property
Matthew Barth38a93a82017-05-11 14:12:27 -050090 * @param[in] property - Property name
91 * @param[in] value - Property value
92 */
Matthew Barth9e741ed2017-06-02 16:29:09 -050093 template <typename T>
Matthew Barth38a93a82017-05-11 14:12:27 -050094 void setPropertyValue(const char* object,
Matthew Barthcec5ab72017-06-02 15:20:56 -050095 const char* interface,
Matthew Barth38a93a82017-05-11 14:12:27 -050096 const char* property,
Matthew Barth9e741ed2017-06-02 16:29:09 -050097 T value)
Matthew Barth38a93a82017-05-11 14:12:27 -050098 {
Matthew Barthcec5ab72017-06-02 15:20:56 -050099 _properties[object][interface][property] = value;
Matthew Barth38a93a82017-05-11 14:12:27 -0500100 };
101
Matthew Barth17d1fe22017-05-11 15:00:36 -0500102 /**
103 * @brief Get the value of an object's property
104 *
105 * @param[in] object - Name of the object containing the property
Matthew Barthcec5ab72017-06-02 15:20:56 -0500106 * @param[in] interface - Interface name containing the property
Matthew Barth17d1fe22017-05-11 15:00:36 -0500107 * @param[in] property - Property name
108 *
109 * @return - The property value
110 */
Matthew Barth9e741ed2017-06-02 16:29:09 -0500111 template <typename T>
Matthew Barth17d1fe22017-05-11 15:00:36 -0500112 inline auto getPropertyValue(const std::string& object,
Matthew Barthcec5ab72017-06-02 15:20:56 -0500113 const std::string& interface,
Matthew Barth17d1fe22017-05-11 15:00:36 -0500114 const std::string& property)
115 {
Matthew Barth9e741ed2017-06-02 16:29:09 -0500116 return sdbusplus::message::variant_ns::get<T>(
117 _properties[object][interface][property]);
Matthew Barth17d1fe22017-05-11 15:00:36 -0500118 };
119
Matthew Barth1de66622017-06-12 13:13:02 -0500120 /**
121 * @brief Get the default floor speed
122 *
123 * @return - The defined default floor speed
124 */
125 inline auto getDefFloor()
126 {
127 return _defFloorSpeed;
128 };
129
Matthew Barth4af419c2017-06-12 13:39:31 -0500130 /**
Matthew Barthe0ca13e2017-06-13 16:29:09 -0500131 * @brief Get the ceiling speed
132 *
133 * @return - The current ceiling speed
134 */
135 inline auto& getCeiling() const
136 {
137 return _ceilingSpeed;
138 };
139
140 /**
141 * @brief Set the ceiling speed to the given speed
142 *
143 * @param[in] speed - Speed to set the ceiling to
144 */
145 inline void setCeiling(uint64_t speed)
146 {
147 _ceilingSpeed = speed;
148 };
149
150 /**
151 * @brief Swaps the ceiling key value with what's given and
152 * returns the value that was swapped.
153 *
154 * @param[in] keyValue - New ceiling key value
155 *
156 * @return - Ceiling key value prior to swapping
157 */
158 inline auto swapCeilingKeyValue(int64_t keyValue)
159 {
160 std::swap(_ceilingKeyValue, keyValue);
161 return keyValue;
162 };
163
Matthew Barth24623522017-06-21 14:09:57 -0500164 /**
165 * @brief Get the increase speed delta
166 *
167 * @return - The current increase speed delta
168 */
169 inline auto& getIncSpeedDelta() const
170 {
171 return _incSpeedDelta;
172 };
173
Matthew Barth240397b2017-06-22 11:23:30 -0500174 /**
Matthew Barth0ce99d82017-06-22 15:07:29 -0500175 * @brief Get the decrease speed delta
176 *
177 * @return - The current decrease speed delta
178 */
179 inline auto& getDecSpeedDelta() const
180 {
181 return _decSpeedDelta;
182 };
183
184 /**
Matthew Barthb4a7cb92017-06-28 15:29:50 -0500185 * @brief Set the floor speed to the given speed and increase target
186 * speed to the floor when target is below floor.
187 *
188 * @param[in] speed - Speed to set the floor to
189 */
190 void setFloor(uint64_t speed);
191
192 /**
Matthew Barth240397b2017-06-22 11:23:30 -0500193 * @brief Calculate the requested target speed from the given delta
194 * and increase the fan speeds, not going above the ceiling.
195 *
196 * @param[in] targetDelta - The delta to increase the target speed by
197 */
198 void requestSpeedIncrease(uint64_t targetDelta);
199
Matthew Barth0ce99d82017-06-22 15:07:29 -0500200 /**
201 * @brief Calculate the requested target speed from the given delta
202 * and increase the fan speeds, not going above the ceiling.
203 *
204 * @param[in] targetDelta - The delta to increase the target speed by
205 */
206 void requestSpeedDecrease(uint64_t targetDelta);
207
Matthew Barth8600d9a2017-06-23 14:38:05 -0500208 /**
Matthew Barth1ee48f22017-06-27 15:14:48 -0500209 * @brief Callback function for the increase timer that delays
210 * processing of requested speed increases while fans are increasing
211 */
212 void incTimerExpired();
213
214 /**
Matthew Barth8600d9a2017-06-23 14:38:05 -0500215 * @brief Callback function for the decrease timer that processes any
216 * requested speed decreases if allowed
217 */
218 void decTimerExpired();
219
Matt Spinler7f88fe62017-04-10 14:39:02 -0500220 private:
221
222 /**
223 * The dbus object
224 */
225 sdbusplus::bus::bus& _bus;
226
227 /**
228 * Full speed for the zone
229 */
230 const uint64_t _fullSpeed;
231
232 /**
233 * The zone number
234 */
235 const size_t _zoneNum;
236
237 /**
Matthew Barth1de66622017-06-12 13:13:02 -0500238 * The default floor speed for the zone
239 */
240 const uint64_t _defFloorSpeed;
241
242 /**
Matthew Barthe0ca13e2017-06-13 16:29:09 -0500243 * The default ceiling speed for the zone
244 */
245 const uint64_t _defCeilingSpeed;
246
247 /**
Matthew Barth4af419c2017-06-12 13:39:31 -0500248 * The floor speed to not go below
249 */
250 uint64_t _floorSpeed = _defFloorSpeed;
251
252 /**
Matthew Barthe0ca13e2017-06-13 16:29:09 -0500253 * The ceiling speed to not go above
254 */
255 uint64_t _ceilingSpeed = _defCeilingSpeed;
256
257 /**
258 * The previous sensor value for calculating the ceiling
259 */
260 int64_t _ceilingKeyValue = 0;
261
262 /**
Matthew Barth861d77c2017-05-22 14:18:25 -0500263 * Automatic fan control active state
264 */
265 bool _isActive = true;
266
267 /**
Matthew Barth240397b2017-06-22 11:23:30 -0500268 * Target speed for this zone
269 */
270 uint64_t _targetSpeed = _fullSpeed;
271
272 /**
Matthew Barth24623522017-06-21 14:09:57 -0500273 * Speed increase delta
274 */
275 uint64_t _incSpeedDelta = 0;
276
277 /**
Matthew Barth0ce99d82017-06-22 15:07:29 -0500278 * Speed decrease delta
279 */
280 uint64_t _decSpeedDelta = 0;
281
282 /**
Matthew Barth1ee48f22017-06-27 15:14:48 -0500283 * The increase timer object
284 */
285 phosphor::fan::util::Timer _incTimer;
286
287 /**
Matthew Barth8600d9a2017-06-23 14:38:05 -0500288 * The decrease timer object
289 */
290 phosphor::fan::util::Timer _decTimer;
291
292 /**
Matt Spinler7f88fe62017-04-10 14:39:02 -0500293 * The vector of fans in this zone
294 */
295 std::vector<std::unique_ptr<Fan>> _fans;
Matthew Barth38a93a82017-05-11 14:12:27 -0500296
297 /**
298 * @brief Map of object property values
299 */
Matthew Barthcec5ab72017-06-02 15:20:56 -0500300 std::map<std::string,
301 std::map<std::string,
302 std::map<std::string,
Matthew Barth9e741ed2017-06-02 16:29:09 -0500303 PropertyVariantType>>> _properties;
Matthew Barth38a93a82017-05-11 14:12:27 -0500304
305 /**
Matthew Barth861d77c2017-05-22 14:18:25 -0500306 * @brief Map of active fan control allowed by groups
307 */
308 std::map<const Group*, bool> _active;
309
310 /**
Matthew Barth38a93a82017-05-11 14:12:27 -0500311 * @brief List of signal event arguments
312 */
Matthew Barth34f1bda2017-05-31 13:45:36 -0500313 std::vector<std::unique_ptr<EventData>> _signalEvents;
Matthew Barth38a93a82017-05-11 14:12:27 -0500314
315 /**
316 * @brief list of Dbus matches for callbacks
317 */
318 std::vector<sdbusplus::server::match::match> _matches;
319
320 /**
Matthew Barth1bf0ce42017-06-23 16:16:30 -0500321 * @brief Initialize all the set speed event properties and actions
322 *
323 * @param[in] def - zone definition containing set speed events
324 */
325 void initEvents(const ZoneDefinition& def);
326
327 /**
328 * @brief Refresh the given property's cached value
329 *
330 * @param[in] bus - the bus to use
331 * @param[in] path - the dbus path name
332 * @param[in] iface - the dbus interface name
333 * @param[in] prop - the property name
334 */
335 void refreshProperty(sdbusplus::bus::bus& bus,
336 const std::string& path,
337 const std::string& iface,
338 const std::string& prop);
339
340 /**
Matthew Barthdf3e8d62017-05-31 11:07:24 -0500341 * @brief Get a property value from the path/interface given
342 *
343 * @param[in] bus - the bus to use
344 * @param[in] path - the dbus path name
345 * @param[in] iface - the dbus interface name
346 * @param[in] prop - the property name
347 * @param[out] value - the value of the property
348 */
Matthew Barthdf3e8d62017-05-31 11:07:24 -0500349 static void getProperty(sdbusplus::bus::bus& bus,
350 const std::string& path,
351 const std::string& iface,
352 const std::string& prop,
Matthew Barth9e741ed2017-06-02 16:29:09 -0500353 PropertyVariantType& value);
Matthew Barthdf3e8d62017-05-31 11:07:24 -0500354
355 /**
Matthew Barth34f1bda2017-05-31 13:45:36 -0500356 * @brief Dbus signal change callback handler
Matthew Barth38a93a82017-05-11 14:12:27 -0500357 *
Matthew Barth34f1bda2017-05-31 13:45:36 -0500358 * @param[in] msg - Expanded sdbusplus message data
359 * @param[in] eventData - The single event's data
Matthew Barth38a93a82017-05-11 14:12:27 -0500360 */
Matthew Barth34f1bda2017-05-31 13:45:36 -0500361 void handleEvent(sdbusplus::message::message& msg,
362 const EventData* eventData);
Matt Spinler7f88fe62017-04-10 14:39:02 -0500363};
364
365}
366}
367}