blob: 2ed3166c1ce1baa6b2956ed5560b3894d1b875ab [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 Barth4f0d3b72020-08-27 14:32:15 -0500163 private:
164 /* The zone's full speed value for fans */
165 uint64_t _fullSpeed;
166
167 /* The zone's default floor speed value for fans */
168 uint64_t _defaultFloor;
169
170 /* Zone's speed increase delay(in seconds) (OPTIONAL) */
171 uint64_t _incDelay;
172
173 /* Zone's speed decrease interval(in seconds) */
174 uint64_t _decInterval;
175
Matthew Barth216229c2020-09-24 13:47:33 -0500176 /**
177 * Zone interface handler functions for its
178 * configured interfaces (OPTIONAL)
179 */
180 std::vector<ZoneHandler> _zoneHandlers;
Matthew Barth651f03a2020-08-27 16:15:11 -0500181
Matthew Barth216229c2020-09-24 13:47:33 -0500182 /* Interface to property mapping of their associated handler function */
183 static const std::map<std::string, std::map<std::string, propHandler>>
184 _intfPropHandlers;
Matthew Barth651f03a2020-08-27 16:15:11 -0500185
Matthew Barthde90fb42021-03-04 16:34:28 -0600186 /* List of fans included in this zone */
187 std::vector<std::unique_ptr<Fan>> _fans;
188
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500189 /**
190 * @brief Parse and set the zone's full speed value
191 *
192 * @param[in] jsonObj - JSON object for the zone
193 *
194 * Sets the full speed value for the zone from the JSON configuration object
195 */
196 void setFullSpeed(const json& jsonObj);
197
198 /**
199 * @brief Parse and set the zone's default floor speed value
200 *
201 * @param[in] jsonObj - JSON object for the zone
202 *
203 * Sets the default floor speed value for the zone from the JSON
204 * configuration object
205 */
206 void setDefaultFloor(const json& jsonObj);
207
208 /**
209 * @brief Parse and set the zone's decrease interval(in seconds)
210 *
211 * @param[in] jsonObj - JSON object for the zone
212 *
213 * Sets the speed decrease interval(in seconds) for the zone from the JSON
214 * configuration object
215 */
216 void setDecInterval(const json& jsonObj);
Matthew Barth651f03a2020-08-27 16:15:11 -0500217
218 /**
Matthew Barth216229c2020-09-24 13:47:33 -0500219 * @brief Parse and set the interfaces served by the zone(OPTIONAL)
Matthew Barth651f03a2020-08-27 16:15:11 -0500220 *
221 * @param[in] jsonObj - JSON object for the zone
222 *
Matthew Barth216229c2020-09-24 13:47:33 -0500223 * Constructs any zone interface handler functions for interfaces that the
224 * zone serves which contains the interface's property's value and
225 * persistency state (OPTIONAL). A property's "persist" state is defaulted
226 * to not be persisted when not given.
Matthew Barth651f03a2020-08-27 16:15:11 -0500227 */
228 void setInterfaces(const json& jsonObj);
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500229};
230
Matthew Barth651f03a2020-08-27 16:15:11 -0500231/**
232 * Properties of interfaces supported by the zone configuration
233 */
234namespace zone::property
235{
236
237/**
Matthew Barth216229c2020-09-24 13:47:33 -0500238 * @brief "Supported" property on the "xyz.openbmc_project.Control.ThermalMode"
239 * interface
Matthew Barth651f03a2020-08-27 16:15:11 -0500240 *
241 * @param[in] jsonObj - JSON object for the "Supported" property
Matthew Barth216229c2020-09-24 13:47:33 -0500242 * @param[in] persist - Whether to persist the value or not
Matthew Barth651f03a2020-08-27 16:15:11 -0500243 *
Matthew Barth216229c2020-09-24 13:47:33 -0500244 * @return Zone interface handler function for the property
Matthew Barth651f03a2020-08-27 16:15:11 -0500245 */
Matthew Barth216229c2020-09-24 13:47:33 -0500246ZoneHandler supported(const json& jsonObj, bool persist);
Matthew Barth651f03a2020-08-27 16:15:11 -0500247
248/**
Matthew Barth216229c2020-09-24 13:47:33 -0500249 * @brief "Current" property on the "xyz.openbmc_project.Control.ThermalMode"
250 * interface
Matthew Barth651f03a2020-08-27 16:15:11 -0500251 *
252 * @param[in] jsonObj - JSON object for the "Current" property
Matthew Barth216229c2020-09-24 13:47:33 -0500253 * @param[in] persist - Whether to persist the value or not
Matthew Barth651f03a2020-08-27 16:15:11 -0500254 *
Matthew Barth216229c2020-09-24 13:47:33 -0500255 * @return Zone interface handler function for the property
Matthew Barth651f03a2020-08-27 16:15:11 -0500256 */
Matthew Barth216229c2020-09-24 13:47:33 -0500257ZoneHandler current(const json& jsonObj, bool persist);
Matthew Barth651f03a2020-08-27 16:15:11 -0500258
259} // namespace zone::property
260
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500261} // namespace phosphor::fan::control::json