blob: 20b9ac9f3da5d249dda89d3cf1fcd929c8fd8748 [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"
8
9namespace phosphor
10{
11namespace fan
12{
13namespace control
14{
15
16/**
Matthew Barth14184132017-05-19 14:37:30 -050017 * The mode fan control will run in:
18 * - init - only do the initialization steps
19 * - control - run normal control algorithms
20 */
21enum class Mode
22{
23 init,
24 control
25};
26
27/**
Matt Spinler7f88fe62017-04-10 14:39:02 -050028 * @class Represents a fan control zone, which is a group of fans
29 * that behave the same.
30 */
31class Zone
32{
33 public:
34
35 Zone() = delete;
36 Zone(const Zone&) = delete;
37 Zone(Zone&&) = default;
38 Zone& operator=(const Zone&) = delete;
39 Zone& operator=(Zone&&) = delete;
40 ~Zone() = default;
41
42 /**
43 * Constructor
44 * Creates the appropriate fan objects based on
45 * the zone definition data passed in.
46 *
Matthew Barth14184132017-05-19 14:37:30 -050047 * @param[in] mode - mode of fan control
Matt Spinler7f88fe62017-04-10 14:39:02 -050048 * @param[in] bus - the dbus object
49 * @param[in] def - the fan zone definition data
50 */
Matthew Barth14184132017-05-19 14:37:30 -050051 Zone(Mode mode,
52 sdbusplus::bus::bus& bus,
Matt Spinler7f88fe62017-04-10 14:39:02 -050053 const ZoneDefinition& def);
54
55 /**
56 * Sets all fans in the zone to the speed
57 * passed in
58 *
59 * @param[in] speed - the fan speed
60 */
61 void setSpeed(uint64_t speed);
62
63 /**
64 * Sets the zone to full speed
65 */
66 inline void setFullSpeed()
67 {
68 if (_fullSpeed != 0)
69 {
70 setSpeed(_fullSpeed);
71 }
72 }
73
Matthew Barth38a93a82017-05-11 14:12:27 -050074 /**
Matthew Barth861d77c2017-05-22 14:18:25 -050075 * @brief Sets the automatic fan control allowed active state
76 *
77 * @param[in] group - A group that affects the active state
78 * @param[in] isActiveAllow - Active state according to group
79 */
80 void setActiveAllow(const Group* group, bool isActiveAllow);
81
82 /**
Matthew Barth38a93a82017-05-11 14:12:27 -050083 * @brief Sets a given object's property value
84 *
85 * @param[in] object - Name of the object containing the property
Matthew Barthcec5ab72017-06-02 15:20:56 -050086 * @param[in] interface - Interface name containing the property
Matthew Barth38a93a82017-05-11 14:12:27 -050087 * @param[in] property - Property name
88 * @param[in] value - Property value
89 */
Matthew Barth9e741ed2017-06-02 16:29:09 -050090 template <typename T>
Matthew Barth38a93a82017-05-11 14:12:27 -050091 void setPropertyValue(const char* object,
Matthew Barthcec5ab72017-06-02 15:20:56 -050092 const char* interface,
Matthew Barth38a93a82017-05-11 14:12:27 -050093 const char* property,
Matthew Barth9e741ed2017-06-02 16:29:09 -050094 T value)
Matthew Barth38a93a82017-05-11 14:12:27 -050095 {
Matthew Barthcec5ab72017-06-02 15:20:56 -050096 _properties[object][interface][property] = value;
Matthew Barth38a93a82017-05-11 14:12:27 -050097 };
98
Matthew Barth17d1fe22017-05-11 15:00:36 -050099 /**
100 * @brief Get the value of an object's property
101 *
102 * @param[in] object - Name of the object containing the property
Matthew Barthcec5ab72017-06-02 15:20:56 -0500103 * @param[in] interface - Interface name containing the property
Matthew Barth17d1fe22017-05-11 15:00:36 -0500104 * @param[in] property - Property name
105 *
106 * @return - The property value
107 */
Matthew Barth9e741ed2017-06-02 16:29:09 -0500108 template <typename T>
Matthew Barth17d1fe22017-05-11 15:00:36 -0500109 inline auto getPropertyValue(const std::string& object,
Matthew Barthcec5ab72017-06-02 15:20:56 -0500110 const std::string& interface,
Matthew Barth17d1fe22017-05-11 15:00:36 -0500111 const std::string& property)
112 {
Matthew Barth9e741ed2017-06-02 16:29:09 -0500113 return sdbusplus::message::variant_ns::get<T>(
114 _properties[object][interface][property]);
Matthew Barth17d1fe22017-05-11 15:00:36 -0500115 };
116
Matthew Barth1de66622017-06-12 13:13:02 -0500117 /**
118 * @brief Get the default floor speed
119 *
120 * @return - The defined default floor speed
121 */
122 inline auto getDefFloor()
123 {
124 return _defFloorSpeed;
125 };
126
Matthew Barth4af419c2017-06-12 13:39:31 -0500127 /**
128 * @brief Set the floor speed to the given speed
129 *
130 * @param[in] speed - Speed to set the floor to
131 */
132 inline void setFloor(uint64_t speed)
133 {
134 _floorSpeed = speed;
135 };
136
Matthew Barthe0ca13e2017-06-13 16:29:09 -0500137 /**
138 * @brief Get the ceiling speed
139 *
140 * @return - The current ceiling speed
141 */
142 inline auto& getCeiling() const
143 {
144 return _ceilingSpeed;
145 };
146
147 /**
148 * @brief Set the ceiling speed to the given speed
149 *
150 * @param[in] speed - Speed to set the ceiling to
151 */
152 inline void setCeiling(uint64_t speed)
153 {
154 _ceilingSpeed = speed;
155 };
156
157 /**
158 * @brief Swaps the ceiling key value with what's given and
159 * returns the value that was swapped.
160 *
161 * @param[in] keyValue - New ceiling key value
162 *
163 * @return - Ceiling key value prior to swapping
164 */
165 inline auto swapCeilingKeyValue(int64_t keyValue)
166 {
167 std::swap(_ceilingKeyValue, keyValue);
168 return keyValue;
169 };
170
Matthew Barth24623522017-06-21 14:09:57 -0500171 /**
172 * @brief Get the increase speed delta
173 *
174 * @return - The current increase speed delta
175 */
176 inline auto& getIncSpeedDelta() const
177 {
178 return _incSpeedDelta;
179 };
180
Matt Spinler7f88fe62017-04-10 14:39:02 -0500181 private:
182
183 /**
184 * The dbus object
185 */
186 sdbusplus::bus::bus& _bus;
187
188 /**
189 * Full speed for the zone
190 */
191 const uint64_t _fullSpeed;
192
193 /**
194 * The zone number
195 */
196 const size_t _zoneNum;
197
198 /**
Matthew Barth1de66622017-06-12 13:13:02 -0500199 * The default floor speed for the zone
200 */
201 const uint64_t _defFloorSpeed;
202
203 /**
Matthew Barthe0ca13e2017-06-13 16:29:09 -0500204 * The default ceiling speed for the zone
205 */
206 const uint64_t _defCeilingSpeed;
207
208 /**
Matthew Barth4af419c2017-06-12 13:39:31 -0500209 * The floor speed to not go below
210 */
211 uint64_t _floorSpeed = _defFloorSpeed;
212
213 /**
Matthew Barthe0ca13e2017-06-13 16:29:09 -0500214 * The ceiling speed to not go above
215 */
216 uint64_t _ceilingSpeed = _defCeilingSpeed;
217
218 /**
219 * The previous sensor value for calculating the ceiling
220 */
221 int64_t _ceilingKeyValue = 0;
222
223 /**
Matthew Barth861d77c2017-05-22 14:18:25 -0500224 * Automatic fan control active state
225 */
226 bool _isActive = true;
227
228 /**
Matthew Barth24623522017-06-21 14:09:57 -0500229 * Speed increase delta
230 */
231 uint64_t _incSpeedDelta = 0;
232
233 /**
Matt Spinler7f88fe62017-04-10 14:39:02 -0500234 * The vector of fans in this zone
235 */
236 std::vector<std::unique_ptr<Fan>> _fans;
Matthew Barth38a93a82017-05-11 14:12:27 -0500237
238 /**
239 * @brief Map of object property values
240 */
Matthew Barthcec5ab72017-06-02 15:20:56 -0500241 std::map<std::string,
242 std::map<std::string,
243 std::map<std::string,
Matthew Barth9e741ed2017-06-02 16:29:09 -0500244 PropertyVariantType>>> _properties;
Matthew Barth38a93a82017-05-11 14:12:27 -0500245
246 /**
Matthew Barth861d77c2017-05-22 14:18:25 -0500247 * @brief Map of active fan control allowed by groups
248 */
249 std::map<const Group*, bool> _active;
250
251 /**
Matthew Barth38a93a82017-05-11 14:12:27 -0500252 * @brief List of signal event arguments
253 */
Matthew Barth34f1bda2017-05-31 13:45:36 -0500254 std::vector<std::unique_ptr<EventData>> _signalEvents;
Matthew Barth38a93a82017-05-11 14:12:27 -0500255
256 /**
257 * @brief list of Dbus matches for callbacks
258 */
259 std::vector<sdbusplus::server::match::match> _matches;
260
261 /**
Matthew Barthdf3e8d62017-05-31 11:07:24 -0500262 * @brief Get a property value from the path/interface given
263 *
264 * @param[in] bus - the bus to use
265 * @param[in] path - the dbus path name
266 * @param[in] iface - the dbus interface name
267 * @param[in] prop - the property name
268 * @param[out] value - the value of the property
269 */
Matthew Barthdf3e8d62017-05-31 11:07:24 -0500270 static void getProperty(sdbusplus::bus::bus& bus,
271 const std::string& path,
272 const std::string& iface,
273 const std::string& prop,
Matthew Barth9e741ed2017-06-02 16:29:09 -0500274 PropertyVariantType& value);
Matthew Barthdf3e8d62017-05-31 11:07:24 -0500275
276 /**
Matthew Barth34f1bda2017-05-31 13:45:36 -0500277 * @brief Dbus signal change callback handler
Matthew Barth38a93a82017-05-11 14:12:27 -0500278 *
Matthew Barth34f1bda2017-05-31 13:45:36 -0500279 * @param[in] msg - Expanded sdbusplus message data
280 * @param[in] eventData - The single event's data
Matthew Barth38a93a82017-05-11 14:12:27 -0500281 */
Matthew Barth34f1bda2017-05-31 13:45:36 -0500282 void handleEvent(sdbusplus::message::message& msg,
283 const EventData* eventData);
Matt Spinler7f88fe62017-04-10 14:39:02 -0500284};
285
286}
287}
288}