blob: 8bf132c04b15f4268cb2b4c722688bf27a48f090 [file] [log] [blame]
Matthew Barth4f0d3b72020-08-27 14:32:15 -05001/**
2 * Copyright © 2020 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#pragma once
17
18#include "config_base.hpp"
Matthew Barthde90fb42021-03-04 16:34:28 -060019#include "fan.hpp"
Matthew Bartha0dd1352021-03-09 11:10:49 -060020#include "xyz/openbmc_project/Control/ThermalMode/server.hpp"
Matthew Barth4f0d3b72020-08-27 14:32:15 -050021
22#include <nlohmann/json.hpp>
Matthew Barthacd737c2021-03-04 11:04:01 -060023#include <sdbusplus/bus.hpp>
Matthew Barth4f0d3b72020-08-27 14:32:15 -050024
Matthew Barth651f03a2020-08-27 16:15:11 -050025#include <any>
26#include <functional>
27#include <map>
28#include <tuple>
29
Matthew Barth4f0d3b72020-08-27 14:32:15 -050030namespace phosphor::fan::control::json
31{
32
33using json = nlohmann::json;
34
Matthew Bartha0dd1352021-03-09 11:10:49 -060035/* Extend the Control::ThermalMode interface */
36using ThermalObject = sdbusplus::server::object::object<
37 sdbusplus::xyz::openbmc_project::Control::server::ThermalMode>;
38
Matthew Barth4f0d3b72020-08-27 14:32:15 -050039/**
40 * @class Zone - Represents a configured fan control zone
41 *
42 * A zone object contains the configured attributes for a zone that groups
Matthew Barthe47c9582021-03-09 14:24:02 -060043 * a number of fans together to be under the same target control. These
44 * configuration attributes include, but are not limited to, the default ceiling
45 * of the fans within the zone, a default floor, the delay between increases, a
46 * decrease interval, and any profiles(OPTIONAL) the zone should be included in.
Matthew Barth4f0d3b72020-08-27 14:32:15 -050047 *
48 * (When no profile for a zone is given, the zone defaults to always exist)
49 *
50 */
Matthew Bartha0dd1352021-03-09 11:10:49 -060051class Zone : public ConfigBase, public ThermalObject
Matthew Barth4f0d3b72020-08-27 14:32:15 -050052{
53 public:
54 /* JSON file name for zones */
55 static constexpr auto confFileName = "zones.json";
Matthew Barth216229c2020-09-24 13:47:33 -050056 static constexpr auto thermModeIntf =
57 "xyz.openbmc_project.Control.ThermalMode";
58 static constexpr auto supportedProp = "Supported";
59 static constexpr auto currentProp = "Current";
Matthew Barth651f03a2020-08-27 16:15:11 -050060
Matthew Barth4f0d3b72020-08-27 14:32:15 -050061 Zone() = delete;
62 Zone(const Zone&) = delete;
63 Zone(Zone&&) = delete;
64 Zone& operator=(const Zone&) = delete;
65 Zone& operator=(Zone&&) = delete;
66 ~Zone() = default;
67
68 /**
69 * Constructor
70 * Parses and populates a zone from JSON object data
71 *
Matthew Barthacd737c2021-03-04 11:04:01 -060072 * @param[in] bus - sdbusplus bus object
Matthew Barth4f0d3b72020-08-27 14:32:15 -050073 * @param[in] jsonObj - JSON object
74 */
Matthew Barthacd737c2021-03-04 11:04:01 -060075 Zone(sdbusplus::bus::bus& bus, const json& jsonObj);
Matthew Barth4f0d3b72020-08-27 14:32:15 -050076
77 /**
Matthew Barthe47c9582021-03-09 14:24:02 -060078 * @brief Get the default ceiling
Matthew Barth4f0d3b72020-08-27 14:32:15 -050079 *
Matthew Barthe47c9582021-03-09 14:24:02 -060080 * Default ceiling is the highest target the fans within this zone is
81 * allowed to increase to. The zone's ceiling defaults to this unless
82 * changed by some configured event.
Matthew Barth4f0d3b72020-08-27 14:32:15 -050083 *
Matthew Barthe47c9582021-03-09 14:24:02 -060084 * @return Default ceiling of this zone
Matthew Barth4f0d3b72020-08-27 14:32:15 -050085 */
Matthew Barthe47c9582021-03-09 14:24:02 -060086 inline const auto& getDefaultCeiling() const
Matthew Barth4f0d3b72020-08-27 14:32:15 -050087 {
Matthew Barthe47c9582021-03-09 14:24:02 -060088 return _defaultCeiling;
Matthew Barth4f0d3b72020-08-27 14:32:15 -050089 }
90
91 /**
Matthew Barthe47c9582021-03-09 14:24:02 -060092 * @brief Get the default floor
Matthew Barth4f0d3b72020-08-27 14:32:15 -050093 *
Matthew Barthe47c9582021-03-09 14:24:02 -060094 * The default floor is the lowest target the fans within this zone
95 * are allowed to decrease to. The zone's floor defaults to this
Matthew Barth4f0d3b72020-08-27 14:32:15 -050096 * unless changed by some configured event.
97 *
Matthew Barthe47c9582021-03-09 14:24:02 -060098 * @return Default floor
Matthew Barth4f0d3b72020-08-27 14:32:15 -050099 */
100 inline const auto& getDefaultFloor() const
101 {
102 return _defaultFloor;
103 }
104
105 /**
Matthew Barthe47c9582021-03-09 14:24:02 -0600106 * @brief Get the increase delay(OPTIONAL)
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500107 *
Matthew Barthe47c9582021-03-09 14:24:02 -0600108 * The increase delay is the amount of time(in seconds) increases
109 * to a target are delayed before being made. The default is 0, which
110 * results in immediate increase requests when any events result in
111 * a change to the target.
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500112 *
113 * It is recommend a value other than 0 is configured, but that inherently
Matthew Barthe47c9582021-03-09 14:24:02 -0600114 * depends on the fan controller and configured increases.
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500115 *
Matthew Barthe47c9582021-03-09 14:24:02 -0600116 * @return Increase delay(in seconds)
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500117 */
118 inline const auto& getIncDelay() const
119 {
120 return _incDelay;
121 }
122
123 /**
Matthew Barthe47c9582021-03-09 14:24:02 -0600124 * @brief Get the decrease interval
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500125 *
Matthew Barthe47c9582021-03-09 14:24:02 -0600126 * Decreases happen on a set interval when no requests for an increase
127 * in fan targets exists. This is the interval(in seconds) at which the fans
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500128 * within the zone are decreased if events exist that result in a target
Matthew Barthe47c9582021-03-09 14:24:02 -0600129 * decrease.
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500130 *
Matthew Barthe47c9582021-03-09 14:24:02 -0600131 * @return Decrease interval(in seconds)
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500132 */
133 inline const auto& getDecInterval() const
134 {
135 return _decInterval;
136 }
137
Matthew Barth651f03a2020-08-27 16:15:11 -0500138 /**
Matthew Barthde90fb42021-03-04 16:34:28 -0600139 * @brief Add a fan object to the zone
140 *
141 * @param[in] fan - Unique pointer to a fan object that will be moved into
142 * the zone
143 *
144 * Adds a fan object to the list of fans that make up the zone by moving the
145 * fan object into the list.
146 */
147 void addFan(std::unique_ptr<Fan> fan);
148
Matthew Barth12cb1252021-03-08 16:47:30 -0600149 /**
Matthew Barth8ba715e2021-03-05 09:00:05 -0600150 * Sets all fans in the zone to the target given when the zone is active
151 *
152 * @param[in] target - Target for fans
153 */
154 void setTarget(uint64_t target);
155
156 /**
157 * @brief Sets the automatic fan control allowed active state
158 *
159 * @param[in] ident - An identifier that affects the active state
160 * @param[in] isActiveAllow - Active state according to group
161 */
162 void setActiveAllow(const std::string& ident, bool isActiveAllow);
163
164 /**
Matthew Barth12cb1252021-03-08 16:47:30 -0600165 * @brief Set the floor to the given target and increase target to the floor
166 * when the target is below the floor value when floor changes are allowed.
167 *
168 * @param[in] target - Target to set the floor to
169 */
170 void setFloor(uint64_t target);
171
172 /**
173 * @brief Sets the floor change allowed state
174 *
175 * @param[in] ident - An identifier that affects floor changes
176 * @param[in] isAllow - Allow state according to the identifier
177 */
178 inline void setFloorChangeAllow(const std::string& ident, bool isAllow)
179 {
180 _floorChange[ident] = isAllow;
181 }
182
183 /**
184 * @brief Calculate the requested target from the given delta and increases
185 * the fans, not going above the ceiling.
186 *
187 * @param[in] targetDelta - The delta to increase the target by
188 */
189 void requestIncrease(uint64_t targetDelta);
190
Matthew Bartha0dd1352021-03-09 11:10:49 -0600191 /**
Matthew Barth07fecfc2021-01-29 09:04:43 -0600192 * @brief Set the requested target base to be used as the target to base a
193 * new requested target from
194 *
195 * @param[in] targetBase - Base target value to use
196 */
197 inline void setRequestTargetBase(uint64_t targetBase)
198 {
199 _requestTargetBase = targetBase;
200 };
201
202 /**
Matthew Bartha0dd1352021-03-09 11:10:49 -0600203 * @brief Set a property to be persisted
204 *
205 * @param[in] intf - Interface containing property
206 * @param[in] prop - Property to be persisted
207 */
208 void setPersisted(const std::string& intf, const std::string& prop);
209
210 /**
211 * @brief Overridden thermal object's set 'Current' property function
212 *
213 * @param[in] value - Value to set 'Current' to
214 *
215 * @return - The updated value of the 'Current' property
216 */
217 std::string current(std::string value) override;
218
Matthew Barthb584d812021-03-11 15:55:04 -0600219 /**
220 * @brief A handler function to set/update a property on a zone
221 * @details Sets or updates a zone's dbus property to the given value using
222 * the provided base dbus object's set property function
223 *
224 * @param[in] intf - Interface on zone object
225 * @param[in] prop - Property on interface
226 * @param[in] func - Zone object's set property function pointer
227 * @param[in] value - Value to set property to
228 * @param[in] persist - Persist property value or not
229 *
230 * @return Lambda function
231 * A lambda function to set/update the zone's dbus property
232 */
233 template <typename T>
234 static auto setProperty(const char* intf, const char* prop,
235 T (Zone::*func)(T), T&& value, bool persist)
236 {
237 return [=, value = std::forward<T>(value)](Zone* zone) {
238 (zone->*func)(value);
239 if (persist)
240 {
241 zone->setPersisted(intf, prop);
242 }
243 };
244 }
245
246 /**
247 * @brief A handler function to set/update a zone's dbus property's persist
248 * state
249 * @details Sets or updates a zone's dbus property's persist state where the
250 * value of the property is to be left unchanged
251 *
252 * @param[in] intf - Interface on zone object
253 * @param[in] prop - Property on interface
254 * @param[in] persist - Persist property value or not
255 *
256 * @return Lambda function
257 * A lambda function to set/update the zone's dbus property's persist
258 * state
259 */
260 static auto setPropertyPersist(const char* intf, const char* prop,
261 bool persist)
262 {
263 return [=](Zone* zone) {
264 if (persist)
265 {
266 zone->setPersisted(intf, prop);
267 }
268 };
269 }
270
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500271 private:
Matthew Barthe47c9582021-03-09 14:24:02 -0600272 /* The zone's default ceiling value for fans */
273 uint64_t _defaultCeiling;
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500274
Matthew Barthe47c9582021-03-09 14:24:02 -0600275 /* The zone's default floor value for fans */
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500276 uint64_t _defaultFloor;
277
Matthew Barthe47c9582021-03-09 14:24:02 -0600278 /* Zone's increase delay(in seconds) (OPTIONAL) */
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500279 uint64_t _incDelay;
280
Matthew Barthe47c9582021-03-09 14:24:02 -0600281 /* Zone's decrease interval(in seconds) */
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500282 uint64_t _decInterval;
283
Matthew Barth12cb1252021-03-08 16:47:30 -0600284 /* The floor target to not go below */
285 uint64_t _floor;
286
287 /* Target for this zone */
288 uint64_t _target;
289
Matthew Barth2b3253e2021-03-09 14:51:16 -0600290 /* Zone increase delta */
291 uint64_t _incDelta;
292
293 /* The ceiling target to not go above */
294 uint64_t _ceiling;
295
296 /* Requested target base */
297 uint64_t _requestTargetBase;
298
Matthew Barth12cb1252021-03-08 16:47:30 -0600299 /* Map of whether floor changes are allowed by a string identifier */
300 std::map<std::string, bool> _floorChange;
301
Matthew Bartha0dd1352021-03-09 11:10:49 -0600302 /* Map of interfaces to persisted properties the zone hosts*/
303 std::map<std::string, std::vector<std::string>> _propsPersisted;
304
Matthew Barth8ba715e2021-03-05 09:00:05 -0600305 /* Automatic fan control active state */
306 bool _isActive;
307
308 /* Map of active fan control allowed by a string identifier */
309 std::map<std::string, bool> _active;
310
Matthew Barthb584d812021-03-11 15:55:04 -0600311 /* Interface to property mapping of their associated set property handler
312 * function */
313 static const std::map<
314 std::string,
315 std::map<std::string,
316 std::function<std::function<void(Zone*)>(const json&, bool)>>>
Matthew Barth216229c2020-09-24 13:47:33 -0500317 _intfPropHandlers;
Matthew Barth651f03a2020-08-27 16:15:11 -0500318
Matthew Barthde90fb42021-03-04 16:34:28 -0600319 /* List of fans included in this zone */
320 std::vector<std::unique_ptr<Fan>> _fans;
321
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500322 /**
Matthew Barthe47c9582021-03-09 14:24:02 -0600323 * @brief Parse and set the zone's default ceiling value
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500324 *
325 * @param[in] jsonObj - JSON object for the zone
326 *
Matthew Barthe47c9582021-03-09 14:24:02 -0600327 * Sets the default ceiling value for the zone from the JSON configuration
328 * object
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500329 */
Matthew Barthe47c9582021-03-09 14:24:02 -0600330 void setDefaultCeiling(const json& jsonObj);
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500331
332 /**
Matthew Barthe47c9582021-03-09 14:24:02 -0600333 * @brief Parse and set the zone's default floor value
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500334 *
335 * @param[in] jsonObj - JSON object for the zone
336 *
Matthew Barthe47c9582021-03-09 14:24:02 -0600337 * Sets the default floor value for the zone from the JSON configuration
338 * object
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500339 */
340 void setDefaultFloor(const json& jsonObj);
341
342 /**
343 * @brief Parse and set the zone's decrease interval(in seconds)
344 *
345 * @param[in] jsonObj - JSON object for the zone
346 *
Matthew Barthe47c9582021-03-09 14:24:02 -0600347 * Sets the decrease interval(in seconds) for the zone from the JSON
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500348 * configuration object
349 */
350 void setDecInterval(const json& jsonObj);
Matthew Barth651f03a2020-08-27 16:15:11 -0500351
352 /**
Matthew Barth216229c2020-09-24 13:47:33 -0500353 * @brief Parse and set the interfaces served by the zone(OPTIONAL)
Matthew Barth651f03a2020-08-27 16:15:11 -0500354 *
355 * @param[in] jsonObj - JSON object for the zone
356 *
Matthew Barth216229c2020-09-24 13:47:33 -0500357 * Constructs any zone interface handler functions for interfaces that the
358 * zone serves which contains the interface's property's value and
359 * persistency state (OPTIONAL). A property's "persist" state is defaulted
360 * to not be persisted when not given.
Matthew Barth651f03a2020-08-27 16:15:11 -0500361 */
362 void setInterfaces(const json& jsonObj);
Matthew Bartha0dd1352021-03-09 11:10:49 -0600363
364 /**
365 * @brief Is the property persisted
366 *
367 * @param[in] intf - Interface containing property
368 * @param[in] prop - Property to check if persisted
369 *
370 * @return - True if property is to be persisted, false otherwise
371 */
372 bool isPersisted(const std::string& intf, const std::string& prop);
373
374 /**
375 * @brief Save the thermal control current mode property to persisted
376 * storage
377 */
378 void saveCurrentMode();
Matthew Barth2b3253e2021-03-09 14:51:16 -0600379
380 /**
381 * @brief Get the request target base if defined, otherwise the the current
382 * target is returned
383 *
384 * @return - The request target base or current target
385 */
386 inline auto getRequestTargetBase() const
387 {
388 return (_requestTargetBase != 0) ? _requestTargetBase : _target;
389 };
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500390};
391
Matthew Barth651f03a2020-08-27 16:15:11 -0500392/**
393 * Properties of interfaces supported by the zone configuration
394 */
395namespace zone::property
396{
397
398/**
Matthew Barth216229c2020-09-24 13:47:33 -0500399 * @brief "Supported" property on the "xyz.openbmc_project.Control.ThermalMode"
Matthew Barthb584d812021-03-11 15:55:04 -0600400 * interface parser. Also creates the handler function for the Zone object to
401 * initialize the property according to what's parsed from the configuration.
Matthew Barth651f03a2020-08-27 16:15:11 -0500402 *
403 * @param[in] jsonObj - JSON object for the "Supported" property
Matthew Barth216229c2020-09-24 13:47:33 -0500404 * @param[in] persist - Whether to persist the value or not
Matthew Barth651f03a2020-08-27 16:15:11 -0500405 *
Matthew Barthb584d812021-03-11 15:55:04 -0600406 * @return Zone interface set property handler function for the "Supported"
407 * property
Matthew Barth651f03a2020-08-27 16:15:11 -0500408 */
Matthew Barthb584d812021-03-11 15:55:04 -0600409std::function<void(Zone*)> supported(const json& jsonObj, bool persist);
Matthew Barth651f03a2020-08-27 16:15:11 -0500410
411/**
Matthew Barth216229c2020-09-24 13:47:33 -0500412 * @brief "Current" property on the "xyz.openbmc_project.Control.ThermalMode"
Matthew Barthb584d812021-03-11 15:55:04 -0600413 * interface parser. Also creates the handler function for the Zone object to
414 * initialize the property according to what's parsed from the configuration.
Matthew Barth651f03a2020-08-27 16:15:11 -0500415 *
416 * @param[in] jsonObj - JSON object for the "Current" property
Matthew Barth216229c2020-09-24 13:47:33 -0500417 * @param[in] persist - Whether to persist the value or not
Matthew Barth651f03a2020-08-27 16:15:11 -0500418 *
Matthew Barthb584d812021-03-11 15:55:04 -0600419 * @return Zone interface set property handler function for the "Current"
420 * property
Matthew Barth651f03a2020-08-27 16:15:11 -0500421 */
Matthew Barthb584d812021-03-11 15:55:04 -0600422std::function<void(Zone*)> current(const json& jsonObj, bool persist);
Matthew Barth651f03a2020-08-27 16:15:11 -0500423
424} // namespace zone::property
425
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500426} // namespace phosphor::fan::control::json