blob: b91af2ff0d0dda869a20f7cc75330322a49a60d1 [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 Barth216229c2020-09-24 13:47:33 -050020#include "types.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 Barth651f03a2020-08-27 16:15:11 -050035/* Interface property handler function */
Matthew Barth216229c2020-09-24 13:47:33 -050036using propHandler = std::function<ZoneHandler(const json&, bool)>;
Matthew Barth651f03a2020-08-27 16:15:11 -050037
Matthew Barth4f0d3b72020-08-27 14:32:15 -050038/**
39 * @class Zone - Represents a configured fan control zone
40 *
41 * A zone object contains the configured attributes for a zone that groups
42 * a number of fans together to be under the same speed control. These
43 * configuration attributes include, but are not limited to, the full speed
44 * of the fans within the zone, a default floor speed, the delay between speed
45 * increases, a decrease interval, and any profiles(OPTIONAL) the zone should
46 * be included in.
47 *
48 * (When no profile for a zone is given, the zone defaults to always exist)
49 *
50 */
51class Zone : public ConfigBase
52{
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 /**
78 * @brief Get the full speed
79 *
80 * Full speed is the speed set to the fans within this zone unless there
81 * are events configured that alter the fan speeds.
82 *
83 * @return Full speed of this zone
84 */
85 inline const auto& getFullSpeed() const
86 {
87 return _fullSpeed;
88 }
89
90 /**
91 * @brief Get the default floor speed
92 *
93 * The default floor speed is the lowest speed the fans within this zone
94 * are allowed to decrease to. The zone's floor speed defaults to this
95 * unless changed by some configured event.
96 *
97 * @return Default floor speed
98 */
99 inline const auto& getDefaultFloor() const
100 {
101 return _defaultFloor;
102 }
103
104 /**
105 * @brief Get the speed increase delay(OPTIONAL)
106 *
107 * The speed increase delay is the amount of time(in seconds) increases
108 * to a target speed are delayed before being made. The default is 0, which
109 * results in immediate speed increase requests when any events result in
110 * a change to the target speed.
111 *
112 * It is recommend a value other than 0 is configured, but that inherently
113 * depends on the fan controller and configured speed increases.
114 *
115 * @return Speed increase delay(in seconds)
116 */
117 inline const auto& getIncDelay() const
118 {
119 return _incDelay;
120 }
121
122 /**
123 * @brief Get the speed decrease interval
124 *
125 * Speed decreases happen on a set interval when no requests for an increase
126 * in fan speeds exists. This is the interval(in seconds) at which the fans
127 * within the zone are decreased if events exist that result in a target
128 * speed decrease.
129 *
130 * @return Speed decrease interval(in seconds)
131 */
132 inline const auto& getDecInterval() const
133 {
134 return _decInterval;
135 }
136
Matthew Barth651f03a2020-08-27 16:15:11 -0500137 /**
Matthew Barth216229c2020-09-24 13:47:33 -0500138 * @brief Get the configuration of zone interface handlers
Matthew Barth651f03a2020-08-27 16:15:11 -0500139 *
140 * Interfaces hosted by a zone can optionally be configured to set their
141 * property values and/or persistency. These interfaces must be supported
142 * by the zone object they are configured for.
143 *
Matthew Barth216229c2020-09-24 13:47:33 -0500144 * @return List of zone interface handler functions that set an interface's
145 * property values and persistency states
Matthew Barth651f03a2020-08-27 16:15:11 -0500146 */
Matthew Barth216229c2020-09-24 13:47:33 -0500147 inline const auto& getZoneHandlers() const
Matthew Barth651f03a2020-08-27 16:15:11 -0500148 {
Matthew Barth216229c2020-09-24 13:47:33 -0500149 return _zoneHandlers;
Matthew Barth651f03a2020-08-27 16:15:11 -0500150 }
151
Matthew Barthde90fb42021-03-04 16:34:28 -0600152 /**
153 * @brief Add a fan object to the zone
154 *
155 * @param[in] fan - Unique pointer to a fan object that will be moved into
156 * the zone
157 *
158 * Adds a fan object to the list of fans that make up the zone by moving the
159 * fan object into the list.
160 */
161 void addFan(std::unique_ptr<Fan> fan);
162
Matthew Barth12cb1252021-03-08 16:47:30 -0600163 /**
164 * @brief Set the floor to the given target and increase target to the floor
165 * when the target is below the floor value when floor changes are allowed.
166 *
167 * @param[in] target - Target to set the floor to
168 */
169 void setFloor(uint64_t target);
170
171 /**
172 * @brief Sets the floor change allowed state
173 *
174 * @param[in] ident - An identifier that affects floor changes
175 * @param[in] isAllow - Allow state according to the identifier
176 */
177 inline void setFloorChangeAllow(const std::string& ident, bool isAllow)
178 {
179 _floorChange[ident] = isAllow;
180 }
181
182 /**
183 * @brief Calculate the requested target from the given delta and increases
184 * the fans, not going above the ceiling.
185 *
186 * @param[in] targetDelta - The delta to increase the target by
187 */
188 void requestIncrease(uint64_t targetDelta);
189
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500190 private:
191 /* The zone's full speed value for fans */
192 uint64_t _fullSpeed;
193
194 /* The zone's default floor speed value for fans */
195 uint64_t _defaultFloor;
196
197 /* Zone's speed increase delay(in seconds) (OPTIONAL) */
198 uint64_t _incDelay;
199
200 /* Zone's speed decrease interval(in seconds) */
201 uint64_t _decInterval;
202
Matthew Barth12cb1252021-03-08 16:47:30 -0600203 /* The floor target to not go below */
204 uint64_t _floor;
205
206 /* Target for this zone */
207 uint64_t _target;
208
209 /* Map of whether floor changes are allowed by a string identifier */
210 std::map<std::string, bool> _floorChange;
211
Matthew Barth216229c2020-09-24 13:47:33 -0500212 /**
213 * Zone interface handler functions for its
214 * configured interfaces (OPTIONAL)
215 */
216 std::vector<ZoneHandler> _zoneHandlers;
Matthew Barth651f03a2020-08-27 16:15:11 -0500217
Matthew Barth216229c2020-09-24 13:47:33 -0500218 /* Interface to property mapping of their associated handler function */
219 static const std::map<std::string, std::map<std::string, propHandler>>
220 _intfPropHandlers;
Matthew Barth651f03a2020-08-27 16:15:11 -0500221
Matthew Barthde90fb42021-03-04 16:34:28 -0600222 /* List of fans included in this zone */
223 std::vector<std::unique_ptr<Fan>> _fans;
224
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500225 /**
226 * @brief Parse and set the zone's full speed value
227 *
228 * @param[in] jsonObj - JSON object for the zone
229 *
230 * Sets the full speed value for the zone from the JSON configuration object
231 */
232 void setFullSpeed(const json& jsonObj);
233
234 /**
235 * @brief Parse and set the zone's default floor speed value
236 *
237 * @param[in] jsonObj - JSON object for the zone
238 *
239 * Sets the default floor speed value for the zone from the JSON
240 * configuration object
241 */
242 void setDefaultFloor(const json& jsonObj);
243
244 /**
245 * @brief Parse and set the zone's decrease interval(in seconds)
246 *
247 * @param[in] jsonObj - JSON object for the zone
248 *
249 * Sets the speed decrease interval(in seconds) for the zone from the JSON
250 * configuration object
251 */
252 void setDecInterval(const json& jsonObj);
Matthew Barth651f03a2020-08-27 16:15:11 -0500253
254 /**
Matthew Barth216229c2020-09-24 13:47:33 -0500255 * @brief Parse and set the interfaces served by the zone(OPTIONAL)
Matthew Barth651f03a2020-08-27 16:15:11 -0500256 *
257 * @param[in] jsonObj - JSON object for the zone
258 *
Matthew Barth216229c2020-09-24 13:47:33 -0500259 * Constructs any zone interface handler functions for interfaces that the
260 * zone serves which contains the interface's property's value and
261 * persistency state (OPTIONAL). A property's "persist" state is defaulted
262 * to not be persisted when not given.
Matthew Barth651f03a2020-08-27 16:15:11 -0500263 */
264 void setInterfaces(const json& jsonObj);
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500265};
266
Matthew Barth651f03a2020-08-27 16:15:11 -0500267/**
268 * Properties of interfaces supported by the zone configuration
269 */
270namespace zone::property
271{
272
273/**
Matthew Barth216229c2020-09-24 13:47:33 -0500274 * @brief "Supported" property on the "xyz.openbmc_project.Control.ThermalMode"
275 * interface
Matthew Barth651f03a2020-08-27 16:15:11 -0500276 *
277 * @param[in] jsonObj - JSON object for the "Supported" property
Matthew Barth216229c2020-09-24 13:47:33 -0500278 * @param[in] persist - Whether to persist the value or not
Matthew Barth651f03a2020-08-27 16:15:11 -0500279 *
Matthew Barth216229c2020-09-24 13:47:33 -0500280 * @return Zone interface handler function for the property
Matthew Barth651f03a2020-08-27 16:15:11 -0500281 */
Matthew Barth216229c2020-09-24 13:47:33 -0500282ZoneHandler supported(const json& jsonObj, bool persist);
Matthew Barth651f03a2020-08-27 16:15:11 -0500283
284/**
Matthew Barth216229c2020-09-24 13:47:33 -0500285 * @brief "Current" property on the "xyz.openbmc_project.Control.ThermalMode"
286 * interface
Matthew Barth651f03a2020-08-27 16:15:11 -0500287 *
288 * @param[in] jsonObj - JSON object for the "Current" property
Matthew Barth216229c2020-09-24 13:47:33 -0500289 * @param[in] persist - Whether to persist the value or not
Matthew Barth651f03a2020-08-27 16:15:11 -0500290 *
Matthew Barth216229c2020-09-24 13:47:33 -0500291 * @return Zone interface handler function for the property
Matthew Barth651f03a2020-08-27 16:15:11 -0500292 */
Matthew Barth216229c2020-09-24 13:47:33 -0500293ZoneHandler current(const json& jsonObj, bool persist);
Matthew Barth651f03a2020-08-27 16:15:11 -0500294
295} // namespace zone::property
296
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500297} // namespace phosphor::fan::control::json