blob: e09ee615fa687b6110ff83294a5761104bd78317 [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#include "zone.hpp"
17
Matthew Barthbc89a8a2021-05-25 15:42:58 -050018#include "dbus_zone.hpp"
Matthew Barthde90fb42021-03-04 16:34:28 -060019#include "fan.hpp"
Matthew Barth9403a212021-05-17 09:31:50 -050020#include "sdbusplus.hpp"
Matthew Barth216229c2020-09-24 13:47:33 -050021
Matthew Barth4f0d3b72020-08-27 14:32:15 -050022#include <nlohmann/json.hpp>
23#include <phosphor-logging/log.hpp>
Matthew Barth603ef162021-03-24 15:34:53 -050024#include <sdeventplus/event.hpp>
Matthew Barth4f0d3b72020-08-27 14:32:15 -050025
Matthew Bartha0dd1352021-03-09 11:10:49 -060026#include <algorithm>
Matthew Barth007de092021-03-25 13:56:04 -050027#include <chrono>
Matthew Barth651f03a2020-08-27 16:15:11 -050028#include <iterator>
29#include <map>
Matthew Barthbc89a8a2021-05-25 15:42:58 -050030#include <memory>
Matthew Barth651f03a2020-08-27 16:15:11 -050031#include <numeric>
Matthew Barth651f03a2020-08-27 16:15:11 -050032#include <utility>
Matthew Barth216229c2020-09-24 13:47:33 -050033#include <vector>
Matthew Barth651f03a2020-08-27 16:15:11 -050034
Matthew Barth4f0d3b72020-08-27 14:32:15 -050035namespace phosphor::fan::control::json
36{
37
38using json = nlohmann::json;
39using namespace phosphor::logging;
40
Matthew Barthbc89a8a2021-05-25 15:42:58 -050041const std::map<
42 std::string,
43 std::map<std::string, std::function<std::function<void(DBusZone&, Zone&)>(
44 const json&, bool)>>>
45 Zone::_intfPropHandlers = {
46 {DBusZone::thermalModeIntf,
47 {{DBusZone::supportedProp, zone::property::supported},
48 {DBusZone::currentProp, zone::property::current}}}};
Matthew Barth651f03a2020-08-27 16:15:11 -050049
Matthew Barth9403a212021-05-17 09:31:50 -050050Zone::Zone(const json& jsonObj, const sdeventplus::Event& event, Manager* mgr) :
Matthew Barthab8e4b82021-05-27 13:25:46 -050051 ConfigBase(jsonObj), _dbusZone{}, _manager(mgr), _defaultFloor(0),
Matthew Barth2504c772021-05-27 14:02:31 -050052 _incDelay(0), _decInterval(0), _floor(0), _target(0), _incDelta(0),
53 _decDelta(0), _requestTargetBase(0), _isActive(true),
Matthew Barthab8e4b82021-05-27 13:25:46 -050054 _incTimer(event, std::bind(&Zone::incTimerExpired, this)),
Matthew Barth007de092021-03-25 13:56:04 -050055 _decTimer(event, std::bind(&Zone::decTimerExpired, this))
Matthew Barth4f0d3b72020-08-27 14:32:15 -050056{
Matthew Barthe47c9582021-03-09 14:24:02 -060057 // Increase delay is optional, defaults to 0
Matthew Barth4f0d3b72020-08-27 14:32:15 -050058 if (jsonObj.contains("increase_delay"))
59 {
Matthew Barth007de092021-03-25 13:56:04 -050060 _incDelay =
61 std::chrono::seconds(jsonObj["increase_delay"].get<uint64_t>());
Matthew Barth4f0d3b72020-08-27 14:32:15 -050062 }
Matthew Barthab8e4b82021-05-27 13:25:46 -050063
64 // Poweron target is required
65 setPowerOnTarget(jsonObj);
66
67 // Default ceiling is optional, defaults to poweron target
68 _defaultCeiling = _poweronTarget;
69 if (jsonObj.contains("default_ceiling"))
70 {
71 _defaultCeiling = jsonObj["default_ceiling"].get<uint64_t>();
72 }
73 // Start with the current ceiling set as the default ceiling
74 _ceiling = _defaultCeiling;
75
76 // Default floor is optional, defaults to 0
77 if (jsonObj.contains("default_floor"))
78 {
79 _defaultFloor = jsonObj["default_floor"].get<uint64_t>();
80 // Start with the current floor set as the default
81 _floor = _defaultFloor;
82 }
83
Matthew Barth2504c772021-05-27 14:02:31 -050084 // Decrease interval is optional, defaults to 0
85 // A decrease interval of 0sec disables the decrease timer
86 if (jsonObj.contains("decrease_interval"))
87 {
88 _decInterval =
89 std::chrono::seconds(jsonObj["decrease_interval"].get<uint64_t>());
90 }
Matthew Barthab8e4b82021-05-27 13:25:46 -050091
Matthew Barth651f03a2020-08-27 16:15:11 -050092 // Setting properties on interfaces to be served are optional
93 if (jsonObj.contains("interfaces"))
94 {
95 setInterfaces(jsonObj);
96 }
Matthew Barth14303a42021-05-21 13:04:08 -050097}
Matthew Barth007de092021-03-25 13:56:04 -050098
Matthew Barth14303a42021-05-21 13:04:08 -050099void Zone::enable()
100{
Matthew Barthbc89a8a2021-05-25 15:42:58 -0500101 // Create thermal control dbus object
102 _dbusZone = std::make_unique<DBusZone>(*this);
Matthew Bartha4483742021-03-25 14:09:01 -0500103
Matthew Barthbc89a8a2021-05-25 15:42:58 -0500104 // Init all configured dbus interfaces' property states
105 for (const auto& func : _propInitFunctions)
106 {
107 // Only call non-null init property functions
108 if (func)
109 {
110 func(*_dbusZone, *this);
111 }
112 }
113
114 // TODO - Restore any persisted properties in init function
115 // Restore thermal control current mode state, if exists
116 _dbusZone->restoreCurrentMode();
117
118 // Emit object added for this zone's associated dbus object
119 _dbusZone->emit_object_added();
Matthew Bartha4483742021-03-25 14:09:01 -0500120
Matthew Barth2504c772021-05-27 14:02:31 -0500121 // A decrease interval of 0sec disables the decrease timer
122 if (_decInterval != std::chrono::seconds::zero())
123 {
124 // Start timer for fan target decreases
125 _decTimer.restart(_decInterval);
126 }
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500127}
128
Matthew Barthde90fb42021-03-04 16:34:28 -0600129void Zone::addFan(std::unique_ptr<Fan> fan)
130{
131 _fans.emplace_back(std::move(fan));
132}
133
Matthew Barth8ba715e2021-03-05 09:00:05 -0600134void Zone::setTarget(uint64_t target)
135{
Matt Spinler5a2b5012021-07-22 14:13:19 -0600136 if (_isActive)
Matthew Barth8ba715e2021-03-05 09:00:05 -0600137 {
138 _target = target;
139 for (auto& fan : _fans)
140 {
141 fan->setTarget(_target);
142 }
143 }
144}
145
146void Zone::setActiveAllow(const std::string& ident, bool isActiveAllow)
147{
148 _active[ident] = isActiveAllow;
149 if (!isActiveAllow)
150 {
151 _isActive = false;
152 }
153 else
154 {
155 // Check all entries are set to allow active fan control
156 auto actPred = [](const auto& entry) { return entry.second; };
157 _isActive = std::all_of(_active.begin(), _active.end(), actPred);
158 }
159}
160
Matthew Barth12cb1252021-03-08 16:47:30 -0600161void Zone::setFloor(uint64_t target)
162{
163 // Check all entries are set to allow floor to be set
Matthew Barth8ba715e2021-03-05 09:00:05 -0600164 auto pred = [](const auto& entry) { return entry.second; };
Matthew Barth12cb1252021-03-08 16:47:30 -0600165 if (std::all_of(_floorChange.begin(), _floorChange.end(), pred))
166 {
167 _floor = target;
168 // Floor above target, update target to floor
169 if (_target < _floor)
170 {
171 requestIncrease(_floor - _target);
172 }
173 }
174}
175
176void Zone::requestIncrease(uint64_t targetDelta)
177{
Matthew Barth2b3253e2021-03-09 14:51:16 -0600178 // Only increase when delta is higher than the current increase delta for
179 // the zone and currently under ceiling
180 if (targetDelta > _incDelta && _target < _ceiling)
181 {
182 auto requestTarget = getRequestTargetBase();
183 requestTarget = (targetDelta - _incDelta) + requestTarget;
184 _incDelta = targetDelta;
185 // Target can not go above a current ceiling
186 if (requestTarget > _ceiling)
187 {
188 requestTarget = _ceiling;
189 }
Matthew Barth8ba715e2021-03-05 09:00:05 -0600190 setTarget(requestTarget);
Matthew Barth007de092021-03-25 13:56:04 -0500191 // Restart timer countdown for fan target increase
192 _incTimer.restartOnce(_incDelay);
Matthew Barth2b3253e2021-03-09 14:51:16 -0600193 }
Matthew Barth12cb1252021-03-08 16:47:30 -0600194}
195
Matthew Barth007de092021-03-25 13:56:04 -0500196void Zone::incTimerExpired()
197{
198 // Clear increase delta when timer expires allowing additional target
199 // increase requests or target decreases to occur
200 _incDelta = 0;
201}
202
Matthew Barth45c44ea2021-03-03 13:16:14 -0600203void Zone::requestDecrease(uint64_t targetDelta)
204{
205 // Only decrease the lowest target delta requested
206 if (_decDelta == 0 || targetDelta < _decDelta)
207 {
208 _decDelta = targetDelta;
209 }
210}
211
Matthew Barth007de092021-03-25 13:56:04 -0500212void Zone::decTimerExpired()
213{
214 // Check all entries are set to allow a decrease
215 auto pred = [](auto const& entry) { return entry.second; };
216 auto decAllowed = std::all_of(_decAllowed.begin(), _decAllowed.end(), pred);
217
218 // Only decrease targets when allowed, a requested decrease target delta
219 // exists, where no requested increases exist and the increase timer is not
220 // running (i.e. not in the middle of increasing)
221 if (decAllowed && _decDelta != 0 && _incDelta == 0 &&
222 !_incTimer.isEnabled())
223 {
224 auto requestTarget = getRequestTargetBase();
225 // Request target should not start above ceiling
226 if (requestTarget > _ceiling)
227 {
228 requestTarget = _ceiling;
229 }
230 // Target can not go below the defined floor
231 if ((requestTarget < _decDelta) || (requestTarget - _decDelta < _floor))
232 {
233 requestTarget = _floor;
234 }
235 else
236 {
237 requestTarget = requestTarget - _decDelta;
238 }
239 setTarget(requestTarget);
240 }
241 // Clear decrease delta when timer expires
242 _decDelta = 0;
243 // Decrease timer is restarted since its repeating
244}
245
Matthew Bartha0dd1352021-03-09 11:10:49 -0600246void Zone::setPersisted(const std::string& intf, const std::string& prop)
247{
248 if (std::find_if(_propsPersisted[intf].begin(), _propsPersisted[intf].end(),
Matthew Barthbc89a8a2021-05-25 15:42:58 -0500249 [&prop](const auto& p) { return prop == p; }) ==
Matthew Bartha0dd1352021-03-09 11:10:49 -0600250 _propsPersisted[intf].end())
251 {
252 _propsPersisted[intf].emplace_back(prop);
253 }
254}
255
Matthew Barth279183f2021-05-25 10:19:43 -0500256bool Zone::isPersisted(const std::string& intf, const std::string& prop) const
257{
258 auto it = _propsPersisted.find(intf);
259 if (it == _propsPersisted.end())
260 {
261 return false;
262 }
263
264 return std::any_of(it->second.begin(), it->second.end(),
265 [&prop](const auto& p) { return prop == p; });
266}
267
Matthew Barthab8e4b82021-05-27 13:25:46 -0500268void Zone::setPowerOnTarget(const json& jsonObj)
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500269{
Matthew Barthab8e4b82021-05-27 13:25:46 -0500270 // TODO Remove "full_speed" after configs replaced with "poweron_target"
271 if (!jsonObj.contains("full_speed") && !jsonObj.contains("poweron_target"))
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500272 {
Matthew Barthab8e4b82021-05-27 13:25:46 -0500273 auto msg = "Missing required zone's poweron target";
274 log<level::ERR>(msg, entry("JSON=%s", jsonObj.dump().c_str()));
275 throw std::runtime_error(msg);
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500276 }
Matthew Barth5606ec82021-05-17 12:08:52 -0500277 if (jsonObj.contains("full_speed"))
278 {
Matthew Barthab8e4b82021-05-27 13:25:46 -0500279 _poweronTarget = jsonObj["full_speed"].get<uint64_t>();
Matthew Barth5606ec82021-05-17 12:08:52 -0500280 }
281 else
282 {
Matthew Barthab8e4b82021-05-27 13:25:46 -0500283 _poweronTarget = jsonObj["poweron_target"].get<uint64_t>();
Matthew Barth5606ec82021-05-17 12:08:52 -0500284 }
Matthew Barthab8e4b82021-05-27 13:25:46 -0500285 // Start with the current target set as the poweron target
286 _target = _poweronTarget;
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500287}
288
Matthew Barth651f03a2020-08-27 16:15:11 -0500289void Zone::setInterfaces(const json& jsonObj)
290{
291 for (const auto& interface : jsonObj["interfaces"])
292 {
293 if (!interface.contains("name") || !interface.contains("properties"))
294 {
295 log<level::ERR>("Missing required zone interface attributes",
296 entry("JSON=%s", interface.dump().c_str()));
297 throw std::runtime_error(
298 "Missing required zone interface attributes");
299 }
Matthew Barth216229c2020-09-24 13:47:33 -0500300 auto propFuncs =
301 _intfPropHandlers.find(interface["name"].get<std::string>());
302 if (propFuncs == _intfPropHandlers.end())
303 {
304 // Construct list of available configurable interfaces
305 auto intfs = std::accumulate(
306 std::next(_intfPropHandlers.begin()), _intfPropHandlers.end(),
307 _intfPropHandlers.begin()->first, [](auto list, auto intf) {
308 return std::move(list) + ", " + intf.first;
309 });
310 log<level::ERR>("Configured interface not available",
311 entry("JSON=%s", interface.dump().c_str()),
312 entry("AVAILABLE_INTFS=%s", intfs.c_str()));
313 throw std::runtime_error("Configured interface not available");
314 }
315
Matthew Barth651f03a2020-08-27 16:15:11 -0500316 for (const auto& property : interface["properties"])
317 {
318 if (!property.contains("name"))
319 {
320 log<level::ERR>(
321 "Missing required interface property attributes",
322 entry("JSON=%s", property.dump().c_str()));
323 throw std::runtime_error(
324 "Missing required interface property attributes");
325 }
326 // Attribute "persist" is optional, defaults to `false`
327 auto persist = false;
328 if (property.contains("persist"))
329 {
330 persist = property["persist"].get<bool>();
331 }
332 // Property name from JSON must exactly match supported
333 // index names to functions in property namespace
Matthew Barth216229c2020-09-24 13:47:33 -0500334 auto propFunc =
335 propFuncs->second.find(property["name"].get<std::string>());
336 if (propFunc == propFuncs->second.end())
Matthew Barth651f03a2020-08-27 16:15:11 -0500337 {
Matthew Barth216229c2020-09-24 13:47:33 -0500338 // Construct list of available configurable properties
Matthew Barth651f03a2020-08-27 16:15:11 -0500339 auto props = std::accumulate(
Matthew Barth216229c2020-09-24 13:47:33 -0500340 std::next(propFuncs->second.begin()),
341 propFuncs->second.end(), propFuncs->second.begin()->first,
342 [](auto list, auto prop) {
Matthew Barth651f03a2020-08-27 16:15:11 -0500343 return std::move(list) + ", " + prop.first;
344 });
Matthew Barth216229c2020-09-24 13:47:33 -0500345 log<level::ERR>("Configured property not available",
Matthew Barth651f03a2020-08-27 16:15:11 -0500346 entry("JSON=%s", property.dump().c_str()),
347 entry("AVAILABLE_PROPS=%s", props.c_str()));
348 throw std::runtime_error(
349 "Configured property function not available");
350 }
Matthew Barthbc89a8a2021-05-25 15:42:58 -0500351
352 _propInitFunctions.emplace_back(
353 propFunc->second(property, persist));
Matthew Barth651f03a2020-08-27 16:15:11 -0500354 }
Matthew Barth651f03a2020-08-27 16:15:11 -0500355 }
356}
357
358/**
Matthew Barth216229c2020-09-24 13:47:33 -0500359 * Properties of interfaces supported by the zone configuration that return
Matthew Barthab8e4b82021-05-27 13:25:46 -0500360 * a handler function that sets the zone's property value(s) and persist
361 * state.
Matthew Barth651f03a2020-08-27 16:15:11 -0500362 */
363namespace zone::property
364{
Matthew Barthb584d812021-03-11 15:55:04 -0600365// Get a set property handler function for the configured values of the
366// "Supported" property
Matthew Barthbc89a8a2021-05-25 15:42:58 -0500367std::function<void(DBusZone&, Zone&)> supported(const json& jsonObj,
368 bool persist)
Matthew Barth651f03a2020-08-27 16:15:11 -0500369{
370 std::vector<std::string> values;
Matthew Barth216229c2020-09-24 13:47:33 -0500371 if (!jsonObj.contains("values"))
Matthew Barth651f03a2020-08-27 16:15:11 -0500372 {
Matthew Barthab8e4b82021-05-27 13:25:46 -0500373 log<level::ERR>("No 'values' found for \"Supported\" property, "
374 "using an empty list",
375 entry("JSON=%s", jsonObj.dump().c_str()));
Matthew Barth216229c2020-09-24 13:47:33 -0500376 }
377 else
378 {
379 for (const auto& value : jsonObj["values"])
380 {
381 if (!value.contains("value"))
382 {
383 log<level::ERR>("No 'value' found for \"Supported\" property "
384 "entry, skipping",
385 entry("JSON=%s", value.dump().c_str()));
386 }
387 else
388 {
389 values.emplace_back(value["value"].get<std::string>());
390 }
391 }
Matthew Barth651f03a2020-08-27 16:15:11 -0500392 }
393
Matthew Barthb584d812021-03-11 15:55:04 -0600394 return Zone::setProperty<std::vector<std::string>>(
Matthew Barthbc89a8a2021-05-25 15:42:58 -0500395 DBusZone::thermalModeIntf, DBusZone::supportedProp,
396 &DBusZone::supported, std::move(values), persist);
Matthew Barth651f03a2020-08-27 16:15:11 -0500397}
398
Matthew Barthab8e4b82021-05-27 13:25:46 -0500399// Get a set property handler function for a configured value of the
400// "Current" property
Matthew Barthbc89a8a2021-05-25 15:42:58 -0500401std::function<void(DBusZone&, Zone&)> current(const json& jsonObj, bool persist)
Matthew Barth651f03a2020-08-27 16:15:11 -0500402{
Matthew Barth216229c2020-09-24 13:47:33 -0500403 // Use default value for "Current" property if no "value" entry given
404 if (!jsonObj.contains("value"))
405 {
Matthew Barthb584d812021-03-11 15:55:04 -0600406 log<level::INFO>("No 'value' found for \"Current\" property, "
407 "using default",
408 entry("JSON=%s", jsonObj.dump().c_str()));
409 // Set persist state of property
Matthew Barthbc89a8a2021-05-25 15:42:58 -0500410 return Zone::setPropertyPersist(DBusZone::thermalModeIntf,
411 DBusZone::currentProp, persist);
Matthew Barth216229c2020-09-24 13:47:33 -0500412 }
413
Matthew Barthb584d812021-03-11 15:55:04 -0600414 return Zone::setProperty<std::string>(
Matthew Barthbc89a8a2021-05-25 15:42:58 -0500415 DBusZone::thermalModeIntf, DBusZone::currentProp, &DBusZone::current,
Matthew Barthb584d812021-03-11 15:55:04 -0600416 jsonObj["value"].get<std::string>(), persist);
Matthew Barth651f03a2020-08-27 16:15:11 -0500417}
Matthew Barthb584d812021-03-11 15:55:04 -0600418
Matthew Barth651f03a2020-08-27 16:15:11 -0500419} // namespace zone::property
420
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500421} // namespace phosphor::fan::control::json