blob: 56fb855c9c26b6882fee3ec5474dbd12eddaefa4 [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 /**
192 * @brief Set a property to be persisted
193 *
194 * @param[in] intf - Interface containing property
195 * @param[in] prop - Property to be persisted
196 */
197 void setPersisted(const std::string& intf, const std::string& prop);
198
199 /**
200 * @brief Overridden thermal object's set 'Current' property function
201 *
202 * @param[in] value - Value to set 'Current' to
203 *
204 * @return - The updated value of the 'Current' property
205 */
206 std::string current(std::string value) override;
207
Matthew Barthb584d812021-03-11 15:55:04 -0600208 /**
209 * @brief A handler function to set/update a property on a zone
210 * @details Sets or updates a zone's dbus property to the given value using
211 * the provided base dbus object's set property function
212 *
213 * @param[in] intf - Interface on zone object
214 * @param[in] prop - Property on interface
215 * @param[in] func - Zone object's set property function pointer
216 * @param[in] value - Value to set property to
217 * @param[in] persist - Persist property value or not
218 *
219 * @return Lambda function
220 * A lambda function to set/update the zone's dbus property
221 */
222 template <typename T>
223 static auto setProperty(const char* intf, const char* prop,
224 T (Zone::*func)(T), T&& value, bool persist)
225 {
226 return [=, value = std::forward<T>(value)](Zone* zone) {
227 (zone->*func)(value);
228 if (persist)
229 {
230 zone->setPersisted(intf, prop);
231 }
232 };
233 }
234
235 /**
236 * @brief A handler function to set/update a zone's dbus property's persist
237 * state
238 * @details Sets or updates a zone's dbus property's persist state where the
239 * value of the property is to be left unchanged
240 *
241 * @param[in] intf - Interface on zone object
242 * @param[in] prop - Property on interface
243 * @param[in] persist - Persist property value or not
244 *
245 * @return Lambda function
246 * A lambda function to set/update the zone's dbus property's persist
247 * state
248 */
249 static auto setPropertyPersist(const char* intf, const char* prop,
250 bool persist)
251 {
252 return [=](Zone* zone) {
253 if (persist)
254 {
255 zone->setPersisted(intf, prop);
256 }
257 };
258 }
259
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500260 private:
Matthew Barthe47c9582021-03-09 14:24:02 -0600261 /* The zone's default ceiling value for fans */
262 uint64_t _defaultCeiling;
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500263
Matthew Barthe47c9582021-03-09 14:24:02 -0600264 /* The zone's default floor value for fans */
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500265 uint64_t _defaultFloor;
266
Matthew Barthe47c9582021-03-09 14:24:02 -0600267 /* Zone's increase delay(in seconds) (OPTIONAL) */
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500268 uint64_t _incDelay;
269
Matthew Barthe47c9582021-03-09 14:24:02 -0600270 /* Zone's decrease interval(in seconds) */
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500271 uint64_t _decInterval;
272
Matthew Barth12cb1252021-03-08 16:47:30 -0600273 /* The floor target to not go below */
274 uint64_t _floor;
275
276 /* Target for this zone */
277 uint64_t _target;
278
Matthew Barth2b3253e2021-03-09 14:51:16 -0600279 /* Zone increase delta */
280 uint64_t _incDelta;
281
282 /* The ceiling target to not go above */
283 uint64_t _ceiling;
284
285 /* Requested target base */
286 uint64_t _requestTargetBase;
287
Matthew Barth12cb1252021-03-08 16:47:30 -0600288 /* Map of whether floor changes are allowed by a string identifier */
289 std::map<std::string, bool> _floorChange;
290
Matthew Bartha0dd1352021-03-09 11:10:49 -0600291 /* Map of interfaces to persisted properties the zone hosts*/
292 std::map<std::string, std::vector<std::string>> _propsPersisted;
293
Matthew Barth8ba715e2021-03-05 09:00:05 -0600294 /* Automatic fan control active state */
295 bool _isActive;
296
297 /* Map of active fan control allowed by a string identifier */
298 std::map<std::string, bool> _active;
299
Matthew Barthb584d812021-03-11 15:55:04 -0600300 /* Interface to property mapping of their associated set property handler
301 * function */
302 static const std::map<
303 std::string,
304 std::map<std::string,
305 std::function<std::function<void(Zone*)>(const json&, bool)>>>
Matthew Barth216229c2020-09-24 13:47:33 -0500306 _intfPropHandlers;
Matthew Barth651f03a2020-08-27 16:15:11 -0500307
Matthew Barthde90fb42021-03-04 16:34:28 -0600308 /* List of fans included in this zone */
309 std::vector<std::unique_ptr<Fan>> _fans;
310
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500311 /**
Matthew Barthe47c9582021-03-09 14:24:02 -0600312 * @brief Parse and set the zone's default ceiling value
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500313 *
314 * @param[in] jsonObj - JSON object for the zone
315 *
Matthew Barthe47c9582021-03-09 14:24:02 -0600316 * Sets the default ceiling value for the zone from the JSON configuration
317 * object
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500318 */
Matthew Barthe47c9582021-03-09 14:24:02 -0600319 void setDefaultCeiling(const json& jsonObj);
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500320
321 /**
Matthew Barthe47c9582021-03-09 14:24:02 -0600322 * @brief Parse and set the zone's default floor value
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500323 *
324 * @param[in] jsonObj - JSON object for the zone
325 *
Matthew Barthe47c9582021-03-09 14:24:02 -0600326 * Sets the default floor value for the zone from the JSON configuration
327 * object
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500328 */
329 void setDefaultFloor(const json& jsonObj);
330
331 /**
332 * @brief Parse and set the zone's decrease interval(in seconds)
333 *
334 * @param[in] jsonObj - JSON object for the zone
335 *
Matthew Barthe47c9582021-03-09 14:24:02 -0600336 * Sets the decrease interval(in seconds) for the zone from the JSON
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500337 * configuration object
338 */
339 void setDecInterval(const json& jsonObj);
Matthew Barth651f03a2020-08-27 16:15:11 -0500340
341 /**
Matthew Barth216229c2020-09-24 13:47:33 -0500342 * @brief Parse and set the interfaces served by the zone(OPTIONAL)
Matthew Barth651f03a2020-08-27 16:15:11 -0500343 *
344 * @param[in] jsonObj - JSON object for the zone
345 *
Matthew Barth216229c2020-09-24 13:47:33 -0500346 * Constructs any zone interface handler functions for interfaces that the
347 * zone serves which contains the interface's property's value and
348 * persistency state (OPTIONAL). A property's "persist" state is defaulted
349 * to not be persisted when not given.
Matthew Barth651f03a2020-08-27 16:15:11 -0500350 */
351 void setInterfaces(const json& jsonObj);
Matthew Bartha0dd1352021-03-09 11:10:49 -0600352
353 /**
354 * @brief Is the property persisted
355 *
356 * @param[in] intf - Interface containing property
357 * @param[in] prop - Property to check if persisted
358 *
359 * @return - True if property is to be persisted, false otherwise
360 */
361 bool isPersisted(const std::string& intf, const std::string& prop);
362
363 /**
364 * @brief Save the thermal control current mode property to persisted
365 * storage
366 */
367 void saveCurrentMode();
Matthew Barth2b3253e2021-03-09 14:51:16 -0600368
369 /**
370 * @brief Get the request target base if defined, otherwise the the current
371 * target is returned
372 *
373 * @return - The request target base or current target
374 */
375 inline auto getRequestTargetBase() const
376 {
377 return (_requestTargetBase != 0) ? _requestTargetBase : _target;
378 };
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500379};
380
Matthew Barth651f03a2020-08-27 16:15:11 -0500381/**
382 * Properties of interfaces supported by the zone configuration
383 */
384namespace zone::property
385{
386
387/**
Matthew Barth216229c2020-09-24 13:47:33 -0500388 * @brief "Supported" property on the "xyz.openbmc_project.Control.ThermalMode"
Matthew Barthb584d812021-03-11 15:55:04 -0600389 * interface parser. Also creates the handler function for the Zone object to
390 * initialize the property according to what's parsed from the configuration.
Matthew Barth651f03a2020-08-27 16:15:11 -0500391 *
392 * @param[in] jsonObj - JSON object for the "Supported" property
Matthew Barth216229c2020-09-24 13:47:33 -0500393 * @param[in] persist - Whether to persist the value or not
Matthew Barth651f03a2020-08-27 16:15:11 -0500394 *
Matthew Barthb584d812021-03-11 15:55:04 -0600395 * @return Zone interface set property handler function for the "Supported"
396 * property
Matthew Barth651f03a2020-08-27 16:15:11 -0500397 */
Matthew Barthb584d812021-03-11 15:55:04 -0600398std::function<void(Zone*)> supported(const json& jsonObj, bool persist);
Matthew Barth651f03a2020-08-27 16:15:11 -0500399
400/**
Matthew Barth216229c2020-09-24 13:47:33 -0500401 * @brief "Current" property on the "xyz.openbmc_project.Control.ThermalMode"
Matthew Barthb584d812021-03-11 15:55:04 -0600402 * interface parser. Also creates the handler function for the Zone object to
403 * initialize the property according to what's parsed from the configuration.
Matthew Barth651f03a2020-08-27 16:15:11 -0500404 *
405 * @param[in] jsonObj - JSON object for the "Current" property
Matthew Barth216229c2020-09-24 13:47:33 -0500406 * @param[in] persist - Whether to persist the value or not
Matthew Barth651f03a2020-08-27 16:15:11 -0500407 *
Matthew Barthb584d812021-03-11 15:55:04 -0600408 * @return Zone interface set property handler function for the "Current"
409 * property
Matthew Barth651f03a2020-08-27 16:15:11 -0500410 */
Matthew Barthb584d812021-03-11 15:55:04 -0600411std::function<void(Zone*)> current(const json& jsonObj, bool persist);
Matthew Barth651f03a2020-08-27 16:15:11 -0500412
413} // namespace zone::property
414
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500415} // namespace phosphor::fan::control::json