blob: 291a5c518d2a96f27195e29df755e706a353319b [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
46 * a number of fans together to be under the same speed control. These
47 * configuration attributes include, but are not limited to, the full speed
48 * of the fans within the zone, a default floor speed, the delay between speed
49 * increases, a decrease interval, and any profiles(OPTIONAL) the zone should
50 * be included in.
51 *
52 * (When no profile for a zone is given, the zone defaults to always exist)
53 *
54 */
Matthew Bartha0dd1352021-03-09 11:10:49 -060055class Zone : public ConfigBase, public ThermalObject
Matthew Barth4f0d3b72020-08-27 14:32:15 -050056{
57 public:
58 /* JSON file name for zones */
59 static constexpr auto confFileName = "zones.json";
Matthew Barth216229c2020-09-24 13:47:33 -050060 static constexpr auto thermModeIntf =
61 "xyz.openbmc_project.Control.ThermalMode";
62 static constexpr auto supportedProp = "Supported";
63 static constexpr auto currentProp = "Current";
Matthew Barth651f03a2020-08-27 16:15:11 -050064
Matthew Barth4f0d3b72020-08-27 14:32:15 -050065 Zone() = delete;
66 Zone(const Zone&) = delete;
67 Zone(Zone&&) = delete;
68 Zone& operator=(const Zone&) = delete;
69 Zone& operator=(Zone&&) = delete;
70 ~Zone() = default;
71
72 /**
73 * Constructor
74 * Parses and populates a zone from JSON object data
75 *
Matthew Barthacd737c2021-03-04 11:04:01 -060076 * @param[in] bus - sdbusplus bus object
Matthew Barth4f0d3b72020-08-27 14:32:15 -050077 * @param[in] jsonObj - JSON object
78 */
Matthew Barthacd737c2021-03-04 11:04:01 -060079 Zone(sdbusplus::bus::bus& bus, const json& jsonObj);
Matthew Barth4f0d3b72020-08-27 14:32:15 -050080
81 /**
82 * @brief Get the full speed
83 *
84 * Full speed is the speed set to the fans within this zone unless there
85 * are events configured that alter the fan speeds.
86 *
87 * @return Full speed of this zone
88 */
89 inline const auto& getFullSpeed() const
90 {
91 return _fullSpeed;
92 }
93
94 /**
95 * @brief Get the default floor speed
96 *
97 * The default floor speed is the lowest speed the fans within this zone
98 * are allowed to decrease to. The zone's floor speed defaults to this
99 * unless changed by some configured event.
100 *
101 * @return Default floor speed
102 */
103 inline const auto& getDefaultFloor() const
104 {
105 return _defaultFloor;
106 }
107
108 /**
109 * @brief Get the speed increase delay(OPTIONAL)
110 *
111 * The speed increase delay is the amount of time(in seconds) increases
112 * to a target speed are delayed before being made. The default is 0, which
113 * results in immediate speed increase requests when any events result in
114 * a change to the target speed.
115 *
116 * It is recommend a value other than 0 is configured, but that inherently
117 * depends on the fan controller and configured speed increases.
118 *
119 * @return Speed increase delay(in seconds)
120 */
121 inline const auto& getIncDelay() const
122 {
123 return _incDelay;
124 }
125
126 /**
127 * @brief Get the speed decrease interval
128 *
129 * Speed decreases happen on a set interval when no requests for an increase
130 * in fan speeds exists. This is the interval(in seconds) at which the fans
131 * within the zone are decreased if events exist that result in a target
132 * speed decrease.
133 *
134 * @return Speed decrease interval(in seconds)
135 */
136 inline const auto& getDecInterval() const
137 {
138 return _decInterval;
139 }
140
Matthew Barth651f03a2020-08-27 16:15:11 -0500141 /**
Matthew Barth216229c2020-09-24 13:47:33 -0500142 * @brief Get the configuration of zone interface handlers
Matthew Barth651f03a2020-08-27 16:15:11 -0500143 *
144 * Interfaces hosted by a zone can optionally be configured to set their
145 * property values and/or persistency. These interfaces must be supported
146 * by the zone object they are configured for.
147 *
Matthew Barth216229c2020-09-24 13:47:33 -0500148 * @return List of zone interface handler functions that set an interface's
149 * property values and persistency states
Matthew Barth651f03a2020-08-27 16:15:11 -0500150 */
Matthew Barth216229c2020-09-24 13:47:33 -0500151 inline const auto& getZoneHandlers() const
Matthew Barth651f03a2020-08-27 16:15:11 -0500152 {
Matthew Barth216229c2020-09-24 13:47:33 -0500153 return _zoneHandlers;
Matthew Barth651f03a2020-08-27 16:15:11 -0500154 }
155
Matthew Barthde90fb42021-03-04 16:34:28 -0600156 /**
157 * @brief Add a fan object to the zone
158 *
159 * @param[in] fan - Unique pointer to a fan object that will be moved into
160 * the zone
161 *
162 * Adds a fan object to the list of fans that make up the zone by moving the
163 * fan object into the list.
164 */
165 void addFan(std::unique_ptr<Fan> fan);
166
Matthew Barth12cb1252021-03-08 16:47:30 -0600167 /**
168 * @brief Set the floor to the given target and increase target to the floor
169 * when the target is below the floor value when floor changes are allowed.
170 *
171 * @param[in] target - Target to set the floor to
172 */
173 void setFloor(uint64_t target);
174
175 /**
176 * @brief Sets the floor change allowed state
177 *
178 * @param[in] ident - An identifier that affects floor changes
179 * @param[in] isAllow - Allow state according to the identifier
180 */
181 inline void setFloorChangeAllow(const std::string& ident, bool isAllow)
182 {
183 _floorChange[ident] = isAllow;
184 }
185
186 /**
187 * @brief Calculate the requested target from the given delta and increases
188 * the fans, not going above the ceiling.
189 *
190 * @param[in] targetDelta - The delta to increase the target by
191 */
192 void requestIncrease(uint64_t targetDelta);
193
Matthew Bartha0dd1352021-03-09 11:10:49 -0600194 /**
195 * @brief Set a property to be persisted
196 *
197 * @param[in] intf - Interface containing property
198 * @param[in] prop - Property to be persisted
199 */
200 void setPersisted(const std::string& intf, const std::string& prop);
201
202 /**
203 * @brief Overridden thermal object's set 'Current' property function
204 *
205 * @param[in] value - Value to set 'Current' to
206 *
207 * @return - The updated value of the 'Current' property
208 */
209 std::string current(std::string value) override;
210
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500211 private:
212 /* The zone's full speed value for fans */
213 uint64_t _fullSpeed;
214
215 /* The zone's default floor speed value for fans */
216 uint64_t _defaultFloor;
217
218 /* Zone's speed increase delay(in seconds) (OPTIONAL) */
219 uint64_t _incDelay;
220
221 /* Zone's speed decrease interval(in seconds) */
222 uint64_t _decInterval;
223
Matthew Barth12cb1252021-03-08 16:47:30 -0600224 /* The floor target to not go below */
225 uint64_t _floor;
226
227 /* Target for this zone */
228 uint64_t _target;
229
230 /* Map of whether floor changes are allowed by a string identifier */
231 std::map<std::string, bool> _floorChange;
232
Matthew Bartha0dd1352021-03-09 11:10:49 -0600233 /* Map of interfaces to persisted properties the zone hosts*/
234 std::map<std::string, std::vector<std::string>> _propsPersisted;
235
Matthew Barth216229c2020-09-24 13:47:33 -0500236 /**
237 * Zone interface handler functions for its
238 * configured interfaces (OPTIONAL)
239 */
240 std::vector<ZoneHandler> _zoneHandlers;
Matthew Barth651f03a2020-08-27 16:15:11 -0500241
Matthew Barth216229c2020-09-24 13:47:33 -0500242 /* Interface to property mapping of their associated handler function */
243 static const std::map<std::string, std::map<std::string, propHandler>>
244 _intfPropHandlers;
Matthew Barth651f03a2020-08-27 16:15:11 -0500245
Matthew Barthde90fb42021-03-04 16:34:28 -0600246 /* List of fans included in this zone */
247 std::vector<std::unique_ptr<Fan>> _fans;
248
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500249 /**
250 * @brief Parse and set the zone's full speed value
251 *
252 * @param[in] jsonObj - JSON object for the zone
253 *
254 * Sets the full speed value for the zone from the JSON configuration object
255 */
256 void setFullSpeed(const json& jsonObj);
257
258 /**
259 * @brief Parse and set the zone's default floor speed value
260 *
261 * @param[in] jsonObj - JSON object for the zone
262 *
263 * Sets the default floor speed value for the zone from the JSON
264 * configuration object
265 */
266 void setDefaultFloor(const json& jsonObj);
267
268 /**
269 * @brief Parse and set the zone's decrease interval(in seconds)
270 *
271 * @param[in] jsonObj - JSON object for the zone
272 *
273 * Sets the speed decrease interval(in seconds) for the zone from the JSON
274 * configuration object
275 */
276 void setDecInterval(const json& jsonObj);
Matthew Barth651f03a2020-08-27 16:15:11 -0500277
278 /**
Matthew Barth216229c2020-09-24 13:47:33 -0500279 * @brief Parse and set the interfaces served by the zone(OPTIONAL)
Matthew Barth651f03a2020-08-27 16:15:11 -0500280 *
281 * @param[in] jsonObj - JSON object for the zone
282 *
Matthew Barth216229c2020-09-24 13:47:33 -0500283 * Constructs any zone interface handler functions for interfaces that the
284 * zone serves which contains the interface's property's value and
285 * persistency state (OPTIONAL). A property's "persist" state is defaulted
286 * to not be persisted when not given.
Matthew Barth651f03a2020-08-27 16:15:11 -0500287 */
288 void setInterfaces(const json& jsonObj);
Matthew Bartha0dd1352021-03-09 11:10:49 -0600289
290 /**
291 * @brief Is the property persisted
292 *
293 * @param[in] intf - Interface containing property
294 * @param[in] prop - Property to check if persisted
295 *
296 * @return - True if property is to be persisted, false otherwise
297 */
298 bool isPersisted(const std::string& intf, const std::string& prop);
299
300 /**
301 * @brief Save the thermal control current mode property to persisted
302 * storage
303 */
304 void saveCurrentMode();
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