blob: 9a0afb2018ef0687dc19e7f746b09031809e43d4 [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 Barth651f03a2020-08-27 16:15:11 -050039/* Interface property handler function */
Matthew Barth216229c2020-09-24 13:47:33 -050040using propHandler = std::function<ZoneHandler(const json&, bool)>;
Matthew Barth651f03a2020-08-27 16:15:11 -050041
Matthew Barth4f0d3b72020-08-27 14:32:15 -050042/**
43 * @class Zone - Represents a configured fan control zone
44 *
45 * A zone object contains the configured attributes for a zone that groups
Matthew Barthe47c9582021-03-09 14:24:02 -060046 * a number of fans together to be under the same target control. These
47 * configuration attributes include, but are not limited to, the default ceiling
48 * of the fans within the zone, a default floor, the delay between increases, a
49 * decrease interval, and any profiles(OPTIONAL) the zone should be included in.
Matthew Barth4f0d3b72020-08-27 14:32:15 -050050 *
51 * (When no profile for a zone is given, the zone defaults to always exist)
52 *
53 */
Matthew Bartha0dd1352021-03-09 11:10:49 -060054class Zone : public ConfigBase, public ThermalObject
Matthew Barth4f0d3b72020-08-27 14:32:15 -050055{
56 public:
57 /* JSON file name for zones */
58 static constexpr auto confFileName = "zones.json";
Matthew Barth216229c2020-09-24 13:47:33 -050059 static constexpr auto thermModeIntf =
60 "xyz.openbmc_project.Control.ThermalMode";
61 static constexpr auto supportedProp = "Supported";
62 static constexpr auto currentProp = "Current";
Matthew Barth651f03a2020-08-27 16:15:11 -050063
Matthew Barth4f0d3b72020-08-27 14:32:15 -050064 Zone() = delete;
65 Zone(const Zone&) = delete;
66 Zone(Zone&&) = delete;
67 Zone& operator=(const Zone&) = delete;
68 Zone& operator=(Zone&&) = delete;
69 ~Zone() = default;
70
71 /**
72 * Constructor
73 * Parses and populates a zone from JSON object data
74 *
Matthew Barthacd737c2021-03-04 11:04:01 -060075 * @param[in] bus - sdbusplus bus object
Matthew Barth4f0d3b72020-08-27 14:32:15 -050076 * @param[in] jsonObj - JSON object
77 */
Matthew Barthacd737c2021-03-04 11:04:01 -060078 Zone(sdbusplus::bus::bus& bus, const json& jsonObj);
Matthew Barth4f0d3b72020-08-27 14:32:15 -050079
80 /**
Matthew Barthe47c9582021-03-09 14:24:02 -060081 * @brief Get the default ceiling
Matthew Barth4f0d3b72020-08-27 14:32:15 -050082 *
Matthew Barthe47c9582021-03-09 14:24:02 -060083 * Default ceiling is the highest target the fans within this zone is
84 * allowed to increase to. The zone's ceiling defaults to this unless
85 * changed by some configured event.
Matthew Barth4f0d3b72020-08-27 14:32:15 -050086 *
Matthew Barthe47c9582021-03-09 14:24:02 -060087 * @return Default ceiling of this zone
Matthew Barth4f0d3b72020-08-27 14:32:15 -050088 */
Matthew Barthe47c9582021-03-09 14:24:02 -060089 inline const auto& getDefaultCeiling() const
Matthew Barth4f0d3b72020-08-27 14:32:15 -050090 {
Matthew Barthe47c9582021-03-09 14:24:02 -060091 return _defaultCeiling;
Matthew Barth4f0d3b72020-08-27 14:32:15 -050092 }
93
94 /**
Matthew Barthe47c9582021-03-09 14:24:02 -060095 * @brief Get the default floor
Matthew Barth4f0d3b72020-08-27 14:32:15 -050096 *
Matthew Barthe47c9582021-03-09 14:24:02 -060097 * The default floor is the lowest target the fans within this zone
98 * are allowed to decrease to. The zone's floor defaults to this
Matthew Barth4f0d3b72020-08-27 14:32:15 -050099 * unless changed by some configured event.
100 *
Matthew Barthe47c9582021-03-09 14:24:02 -0600101 * @return Default floor
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500102 */
103 inline const auto& getDefaultFloor() const
104 {
105 return _defaultFloor;
106 }
107
108 /**
Matthew Barthe47c9582021-03-09 14:24:02 -0600109 * @brief Get the increase delay(OPTIONAL)
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500110 *
Matthew Barthe47c9582021-03-09 14:24:02 -0600111 * The increase delay is the amount of time(in seconds) increases
112 * to a target are delayed before being made. The default is 0, which
113 * results in immediate increase requests when any events result in
114 * a change to the target.
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500115 *
116 * It is recommend a value other than 0 is configured, but that inherently
Matthew Barthe47c9582021-03-09 14:24:02 -0600117 * depends on the fan controller and configured increases.
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500118 *
Matthew Barthe47c9582021-03-09 14:24:02 -0600119 * @return Increase delay(in seconds)
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500120 */
121 inline const auto& getIncDelay() const
122 {
123 return _incDelay;
124 }
125
126 /**
Matthew Barthe47c9582021-03-09 14:24:02 -0600127 * @brief Get the decrease interval
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500128 *
Matthew Barthe47c9582021-03-09 14:24:02 -0600129 * Decreases happen on a set interval when no requests for an increase
130 * in fan targets exists. This is the interval(in seconds) at which the fans
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500131 * within the zone are decreased if events exist that result in a target
Matthew Barthe47c9582021-03-09 14:24:02 -0600132 * decrease.
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500133 *
Matthew Barthe47c9582021-03-09 14:24:02 -0600134 * @return Decrease interval(in seconds)
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500135 */
136 inline const auto& getDecInterval() const
137 {
138 return _decInterval;
139 }
140
Matthew Barth651f03a2020-08-27 16:15:11 -0500141 /**
Matthew Barthde90fb42021-03-04 16:34:28 -0600142 * @brief Add a fan object to the zone
143 *
144 * @param[in] fan - Unique pointer to a fan object that will be moved into
145 * the zone
146 *
147 * Adds a fan object to the list of fans that make up the zone by moving the
148 * fan object into the list.
149 */
150 void addFan(std::unique_ptr<Fan> fan);
151
Matthew Barth12cb1252021-03-08 16:47:30 -0600152 /**
153 * @brief Set the floor to the given target and increase target to the floor
154 * when the target is below the floor value when floor changes are allowed.
155 *
156 * @param[in] target - Target to set the floor to
157 */
158 void setFloor(uint64_t target);
159
160 /**
161 * @brief Sets the floor change allowed state
162 *
163 * @param[in] ident - An identifier that affects floor changes
164 * @param[in] isAllow - Allow state according to the identifier
165 */
166 inline void setFloorChangeAllow(const std::string& ident, bool isAllow)
167 {
168 _floorChange[ident] = isAllow;
169 }
170
171 /**
172 * @brief Calculate the requested target from the given delta and increases
173 * the fans, not going above the ceiling.
174 *
175 * @param[in] targetDelta - The delta to increase the target by
176 */
177 void requestIncrease(uint64_t targetDelta);
178
Matthew Bartha0dd1352021-03-09 11:10:49 -0600179 /**
180 * @brief Set a property to be persisted
181 *
182 * @param[in] intf - Interface containing property
183 * @param[in] prop - Property to be persisted
184 */
185 void setPersisted(const std::string& intf, const std::string& prop);
186
187 /**
188 * @brief Overridden thermal object's set 'Current' property function
189 *
190 * @param[in] value - Value to set 'Current' to
191 *
192 * @return - The updated value of the 'Current' property
193 */
194 std::string current(std::string value) override;
195
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500196 private:
Matthew Barthe47c9582021-03-09 14:24:02 -0600197 /* The zone's default ceiling value for fans */
198 uint64_t _defaultCeiling;
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500199
Matthew Barthe47c9582021-03-09 14:24:02 -0600200 /* The zone's default floor value for fans */
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500201 uint64_t _defaultFloor;
202
Matthew Barthe47c9582021-03-09 14:24:02 -0600203 /* Zone's increase delay(in seconds) (OPTIONAL) */
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500204 uint64_t _incDelay;
205
Matthew Barthe47c9582021-03-09 14:24:02 -0600206 /* Zone's decrease interval(in seconds) */
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500207 uint64_t _decInterval;
208
Matthew Barth12cb1252021-03-08 16:47:30 -0600209 /* The floor target to not go below */
210 uint64_t _floor;
211
212 /* Target for this zone */
213 uint64_t _target;
214
Matthew Barth2b3253e2021-03-09 14:51:16 -0600215 /* Zone increase delta */
216 uint64_t _incDelta;
217
218 /* The ceiling target to not go above */
219 uint64_t _ceiling;
220
221 /* Requested target base */
222 uint64_t _requestTargetBase;
223
Matthew Barth12cb1252021-03-08 16:47:30 -0600224 /* Map of whether floor changes are allowed by a string identifier */
225 std::map<std::string, bool> _floorChange;
226
Matthew Bartha0dd1352021-03-09 11:10:49 -0600227 /* Map of interfaces to persisted properties the zone hosts*/
228 std::map<std::string, std::vector<std::string>> _propsPersisted;
229
Matthew Barth216229c2020-09-24 13:47:33 -0500230 /* Interface to property mapping of their associated handler function */
231 static const std::map<std::string, std::map<std::string, propHandler>>
232 _intfPropHandlers;
Matthew Barth651f03a2020-08-27 16:15:11 -0500233
Matthew Barthde90fb42021-03-04 16:34:28 -0600234 /* List of fans included in this zone */
235 std::vector<std::unique_ptr<Fan>> _fans;
236
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500237 /**
Matthew Barthe47c9582021-03-09 14:24:02 -0600238 * @brief Parse and set the zone's default ceiling value
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500239 *
240 * @param[in] jsonObj - JSON object for the zone
241 *
Matthew Barthe47c9582021-03-09 14:24:02 -0600242 * Sets the default ceiling value for the zone from the JSON configuration
243 * object
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500244 */
Matthew Barthe47c9582021-03-09 14:24:02 -0600245 void setDefaultCeiling(const json& jsonObj);
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500246
247 /**
Matthew Barthe47c9582021-03-09 14:24:02 -0600248 * @brief Parse and set the zone's default floor value
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500249 *
250 * @param[in] jsonObj - JSON object for the zone
251 *
Matthew Barthe47c9582021-03-09 14:24:02 -0600252 * Sets the default floor value for the zone from the JSON configuration
253 * object
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500254 */
255 void setDefaultFloor(const json& jsonObj);
256
257 /**
258 * @brief Parse and set the zone's decrease interval(in seconds)
259 *
260 * @param[in] jsonObj - JSON object for the zone
261 *
Matthew Barthe47c9582021-03-09 14:24:02 -0600262 * Sets the decrease interval(in seconds) for the zone from the JSON
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500263 * configuration object
264 */
265 void setDecInterval(const json& jsonObj);
Matthew Barth651f03a2020-08-27 16:15:11 -0500266
267 /**
Matthew Barth216229c2020-09-24 13:47:33 -0500268 * @brief Parse and set the interfaces served by the zone(OPTIONAL)
Matthew Barth651f03a2020-08-27 16:15:11 -0500269 *
270 * @param[in] jsonObj - JSON object for the zone
271 *
Matthew Barth216229c2020-09-24 13:47:33 -0500272 * Constructs any zone interface handler functions for interfaces that the
273 * zone serves which contains the interface's property's value and
274 * persistency state (OPTIONAL). A property's "persist" state is defaulted
275 * to not be persisted when not given.
Matthew Barth651f03a2020-08-27 16:15:11 -0500276 */
277 void setInterfaces(const json& jsonObj);
Matthew Bartha0dd1352021-03-09 11:10:49 -0600278
279 /**
280 * @brief Is the property persisted
281 *
282 * @param[in] intf - Interface containing property
283 * @param[in] prop - Property to check if persisted
284 *
285 * @return - True if property is to be persisted, false otherwise
286 */
287 bool isPersisted(const std::string& intf, const std::string& prop);
288
289 /**
290 * @brief Save the thermal control current mode property to persisted
291 * storage
292 */
293 void saveCurrentMode();
Matthew Barth2b3253e2021-03-09 14:51:16 -0600294
295 /**
296 * @brief Get the request target base if defined, otherwise the the current
297 * target is returned
298 *
299 * @return - The request target base or current target
300 */
301 inline auto getRequestTargetBase() const
302 {
303 return (_requestTargetBase != 0) ? _requestTargetBase : _target;
304 };
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500305};
306
Matthew Barth651f03a2020-08-27 16:15:11 -0500307/**
308 * Properties of interfaces supported by the zone configuration
309 */
310namespace zone::property
311{
312
313/**
Matthew Barth216229c2020-09-24 13:47:33 -0500314 * @brief "Supported" property on the "xyz.openbmc_project.Control.ThermalMode"
315 * interface
Matthew Barth651f03a2020-08-27 16:15:11 -0500316 *
317 * @param[in] jsonObj - JSON object for the "Supported" property
Matthew Barth216229c2020-09-24 13:47:33 -0500318 * @param[in] persist - Whether to persist the value or not
Matthew Barth651f03a2020-08-27 16:15:11 -0500319 *
Matthew Barth216229c2020-09-24 13:47:33 -0500320 * @return Zone interface handler function for the property
Matthew Barth651f03a2020-08-27 16:15:11 -0500321 */
Matthew Barth216229c2020-09-24 13:47:33 -0500322ZoneHandler supported(const json& jsonObj, bool persist);
Matthew Barth651f03a2020-08-27 16:15:11 -0500323
324/**
Matthew Barth216229c2020-09-24 13:47:33 -0500325 * @brief "Current" property on the "xyz.openbmc_project.Control.ThermalMode"
326 * interface
Matthew Barth651f03a2020-08-27 16:15:11 -0500327 *
328 * @param[in] jsonObj - JSON object for the "Current" property
Matthew Barth216229c2020-09-24 13:47:33 -0500329 * @param[in] persist - Whether to persist the value or not
Matthew Barth651f03a2020-08-27 16:15:11 -0500330 *
Matthew Barth216229c2020-09-24 13:47:33 -0500331 * @return Zone interface handler function for the property
Matthew Barth651f03a2020-08-27 16:15:11 -0500332 */
Matthew Barth216229c2020-09-24 13:47:33 -0500333ZoneHandler current(const json& jsonObj, bool persist);
Matthew Barth651f03a2020-08-27 16:15:11 -0500334
335} // namespace zone::property
336
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500337} // namespace phosphor::fan::control::json