blob: 12f8c0239ea6bad2f060f3fa07d1363e6388df67 [file] [log] [blame]
Matthew Barth4f0d3b72020-08-27 14:32:15 -05001/**
Mike Capps762e8582021-10-07 15:33:23 -04002 * Copyright © 2022 IBM Corporation
Matthew Barth4f0d3b72020-08-27 14:32:15 -05003 *
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
Matt Spinler40554d82021-10-26 15:23:59 -050018#include "../utils/flight_recorder.hpp"
Matthew Barthbc89a8a2021-05-25 15:42:58 -050019#include "dbus_zone.hpp"
Matthew Barthde90fb42021-03-04 16:34:28 -060020#include "fan.hpp"
Matthew Barth9403a212021-05-17 09:31:50 -050021#include "sdbusplus.hpp"
Matthew Barth216229c2020-09-24 13:47:33 -050022
Matthew Barth4f0d3b72020-08-27 14:32:15 -050023#include <nlohmann/json.hpp>
24#include <phosphor-logging/log.hpp>
Matthew Barth603ef162021-03-24 15:34:53 -050025#include <sdeventplus/event.hpp>
Matthew Barth4f0d3b72020-08-27 14:32:15 -050026
Matthew Bartha0dd1352021-03-09 11:10:49 -060027#include <algorithm>
Matthew Barth007de092021-03-25 13:56:04 -050028#include <chrono>
Matthew Barth651f03a2020-08-27 16:15:11 -050029#include <iterator>
30#include <map>
Matthew Barthbc89a8a2021-05-25 15:42:58 -050031#include <memory>
Matthew Barth651f03a2020-08-27 16:15:11 -050032#include <numeric>
Matthew Barth651f03a2020-08-27 16:15:11 -050033#include <utility>
Matthew Barth216229c2020-09-24 13:47:33 -050034#include <vector>
Matthew Barth651f03a2020-08-27 16:15:11 -050035
Matthew Barth4f0d3b72020-08-27 14:32:15 -050036namespace phosphor::fan::control::json
37{
38
39using json = nlohmann::json;
40using namespace phosphor::logging;
41
Matthew Barthbc89a8a2021-05-25 15:42:58 -050042const std::map<
43 std::string,
44 std::map<std::string, std::function<std::function<void(DBusZone&, Zone&)>(
45 const json&, bool)>>>
46 Zone::_intfPropHandlers = {
47 {DBusZone::thermalModeIntf,
48 {{DBusZone::supportedProp, zone::property::supported},
49 {DBusZone::currentProp, zone::property::current}}}};
Matthew Barth651f03a2020-08-27 16:15:11 -050050
Matthew Barth9403a212021-05-17 09:31:50 -050051Zone::Zone(const json& jsonObj, const sdeventplus::Event& event, Manager* mgr) :
Matthew Barthab8e4b82021-05-27 13:25:46 -050052 ConfigBase(jsonObj), _dbusZone{}, _manager(mgr), _defaultFloor(0),
Matthew Barth2504c772021-05-27 14:02:31 -050053 _incDelay(0), _decInterval(0), _floor(0), _target(0), _incDelta(0),
54 _decDelta(0), _requestTargetBase(0), _isActive(true),
Matthew Barthab8e4b82021-05-27 13:25:46 -050055 _incTimer(event, std::bind(&Zone::incTimerExpired, this)),
Matthew Barth007de092021-03-25 13:56:04 -050056 _decTimer(event, std::bind(&Zone::decTimerExpired, this))
Matthew Barth4f0d3b72020-08-27 14:32:15 -050057{
Matthew Barthe47c9582021-03-09 14:24:02 -060058 // Increase delay is optional, defaults to 0
Matthew Barth4f0d3b72020-08-27 14:32:15 -050059 if (jsonObj.contains("increase_delay"))
60 {
Matthew Barth007de092021-03-25 13:56:04 -050061 _incDelay =
62 std::chrono::seconds(jsonObj["increase_delay"].get<uint64_t>());
Matthew Barth4f0d3b72020-08-27 14:32:15 -050063 }
Matthew Barthab8e4b82021-05-27 13:25:46 -050064
65 // Poweron target is required
66 setPowerOnTarget(jsonObj);
67
68 // Default ceiling is optional, defaults to poweron target
69 _defaultCeiling = _poweronTarget;
70 if (jsonObj.contains("default_ceiling"))
71 {
72 _defaultCeiling = jsonObj["default_ceiling"].get<uint64_t>();
73 }
74 // Start with the current ceiling set as the default ceiling
75 _ceiling = _defaultCeiling;
76
77 // Default floor is optional, defaults to 0
78 if (jsonObj.contains("default_floor"))
79 {
80 _defaultFloor = jsonObj["default_floor"].get<uint64_t>();
Matthew Barthdc3152d2022-03-16 14:43:13 -050081 if (_defaultFloor > _ceiling)
82 {
83 log<level::ERR>(
Patrick Williamsfbf47032023-07-17 12:27:34 -050084 std::format("Configured default_floor({}) above ceiling({}), "
Matthew Barthdc3152d2022-03-16 14:43:13 -050085 "setting default floor to ceiling",
86 _defaultFloor, _ceiling)
87 .c_str());
88 _defaultFloor = _ceiling;
89 }
Matthew Barthab8e4b82021-05-27 13:25:46 -050090 // Start with the current floor set as the default
91 _floor = _defaultFloor;
92 }
93
Matthew Barth2504c772021-05-27 14:02:31 -050094 // Decrease interval is optional, defaults to 0
95 // A decrease interval of 0sec disables the decrease timer
96 if (jsonObj.contains("decrease_interval"))
97 {
98 _decInterval =
99 std::chrono::seconds(jsonObj["decrease_interval"].get<uint64_t>());
100 }
Matthew Barthab8e4b82021-05-27 13:25:46 -0500101
Matthew Barth651f03a2020-08-27 16:15:11 -0500102 // Setting properties on interfaces to be served are optional
103 if (jsonObj.contains("interfaces"))
104 {
105 setInterfaces(jsonObj);
106 }
Matthew Barth14303a42021-05-21 13:04:08 -0500107}
Matthew Barth007de092021-03-25 13:56:04 -0500108
Matthew Barth14303a42021-05-21 13:04:08 -0500109void Zone::enable()
110{
Matthew Barthbc89a8a2021-05-25 15:42:58 -0500111 // Create thermal control dbus object
112 _dbusZone = std::make_unique<DBusZone>(*this);
Matthew Bartha4483742021-03-25 14:09:01 -0500113
Matthew Barthbc89a8a2021-05-25 15:42:58 -0500114 // Init all configured dbus interfaces' property states
115 for (const auto& func : _propInitFunctions)
116 {
117 // Only call non-null init property functions
118 if (func)
119 {
120 func(*_dbusZone, *this);
121 }
122 }
123
124 // TODO - Restore any persisted properties in init function
125 // Restore thermal control current mode state, if exists
126 _dbusZone->restoreCurrentMode();
127
128 // Emit object added for this zone's associated dbus object
129 _dbusZone->emit_object_added();
Matthew Bartha4483742021-03-25 14:09:01 -0500130
Matthew Barth2504c772021-05-27 14:02:31 -0500131 // A decrease interval of 0sec disables the decrease timer
132 if (_decInterval != std::chrono::seconds::zero())
133 {
134 // Start timer for fan target decreases
135 _decTimer.restart(_decInterval);
136 }
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500137}
138
Matthew Barthde90fb42021-03-04 16:34:28 -0600139void Zone::addFan(std::unique_ptr<Fan> fan)
140{
141 _fans.emplace_back(std::move(fan));
142}
143
Matthew Barth8ba715e2021-03-05 09:00:05 -0600144void Zone::setTarget(uint64_t target)
145{
Matt Spinler5a2b5012021-07-22 14:13:19 -0600146 if (_isActive)
Matthew Barth8ba715e2021-03-05 09:00:05 -0600147 {
Matt Spinlera917e692022-04-13 14:50:08 -0500148 if (_target != target)
149 {
150 FlightRecorder::instance().log(
151 "zone-set-target" + getName(),
Patrick Williamsfbf47032023-07-17 12:27:34 -0500152 std::format("Set target {} (from {})", target, _target));
Matt Spinlera917e692022-04-13 14:50:08 -0500153 }
Matthew Barth8ba715e2021-03-05 09:00:05 -0600154 _target = target;
155 for (auto& fan : _fans)
156 {
157 fan->setTarget(_target);
158 }
159 }
160}
161
Mike Capps762e8582021-10-07 15:33:23 -0400162void Zone::lockFanTarget(const std::string& fname, uint64_t target)
163{
Patrick Williams5e15c3b2023-10-20 11:18:11 -0500164 auto fanItr = std::find_if(
165 _fans.begin(), _fans.end(),
166 [&fname](const auto& fan) { return fan->getName() == fname; });
Mike Capps762e8582021-10-07 15:33:23 -0400167
168 if (_fans.end() != fanItr)
169 {
170 (*fanItr)->lockTarget(target);
171 }
172 else
173 {
174 log<level::DEBUG>(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500175 std::format("Configured fan {} not found in zone {} to lock target",
Mike Capps762e8582021-10-07 15:33:23 -0400176 fname, getName())
177 .c_str());
178 }
179}
180
181void Zone::unlockFanTarget(const std::string& fname, uint64_t target)
182{
Patrick Williams5e15c3b2023-10-20 11:18:11 -0500183 auto fanItr = std::find_if(
184 _fans.begin(), _fans.end(),
185 [&fname](const auto& fan) { return fan->getName() == fname; });
Mike Capps762e8582021-10-07 15:33:23 -0400186
187 if (_fans.end() != fanItr)
188 {
189 (*fanItr)->unlockTarget(target);
190
191 // attempt to resume Zone target on fan
192 (*fanItr)->setTarget(getTarget());
193 }
194 else
195 {
196 log<level::DEBUG>(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500197 std::format(
Mike Capps762e8582021-10-07 15:33:23 -0400198 "Configured fan {} not found in zone {} to unlock target",
199 fname, getName())
200 .c_str());
201 }
202}
203
Matthew Barth53680112021-09-23 11:20:41 -0500204void Zone::setTargetHold(const std::string& ident, uint64_t target, bool hold)
205{
Matt Spinler98671612021-12-09 14:27:15 -0600206 using namespace std::string_literals;
207
Matthew Barth53680112021-09-23 11:20:41 -0500208 if (!hold)
209 {
Matt Spinler98671612021-12-09 14:27:15 -0600210 size_t removed = _targetHolds.erase(ident);
211 if (removed)
212 {
213 FlightRecorder::instance().log(
214 "zone-target"s + getName(),
Patrick Williamsfbf47032023-07-17 12:27:34 -0500215 std::format("{} is removing target hold", ident));
Matt Spinler98671612021-12-09 14:27:15 -0600216 }
Matthew Barth53680112021-09-23 11:20:41 -0500217 }
218 else
219 {
Matt Spinler98671612021-12-09 14:27:15 -0600220 if (!((_targetHolds.find(ident) != _targetHolds.end()) &&
221 (_targetHolds[ident] == target)))
222 {
223 FlightRecorder::instance().log(
224 "zone-target"s + getName(),
Patrick Williamsfbf47032023-07-17 12:27:34 -0500225 std::format("{} is setting target hold to {}", ident, target));
Matt Spinler98671612021-12-09 14:27:15 -0600226 }
Matt Spinler40554d82021-10-26 15:23:59 -0500227 _targetHolds[ident] = target;
Matthew Barth53680112021-09-23 11:20:41 -0500228 _isActive = false;
229 }
230
Matt Spinler40554d82021-10-26 15:23:59 -0500231 auto itHoldMax = std::max_element(_targetHolds.begin(), _targetHolds.end(),
Matthew Barth53680112021-09-23 11:20:41 -0500232 [](const auto& aHold, const auto& bHold) {
Patrick Williams61b73292023-05-10 07:50:12 -0500233 return aHold.second < bHold.second;
234 });
Matt Spinler40554d82021-10-26 15:23:59 -0500235 if (itHoldMax == _targetHolds.end())
Matthew Barth53680112021-09-23 11:20:41 -0500236 {
237 _isActive = true;
238 }
239 else
240 {
Matt Spinler98671612021-12-09 14:27:15 -0600241 if (_target != itHoldMax->second)
242 {
243 FlightRecorder::instance().log(
244 "zone-target"s + getName(),
Patrick Williamsfbf47032023-07-17 12:27:34 -0500245 std::format("Settings fans to target hold of {}",
Matt Spinler98671612021-12-09 14:27:15 -0600246 itHoldMax->second));
247 }
248
Matthew Barth53680112021-09-23 11:20:41 -0500249 _target = itHoldMax->second;
250 for (auto& fan : _fans)
251 {
252 fan->setTarget(_target);
253 }
254 }
255}
256
Matt Spinler40554d82021-10-26 15:23:59 -0500257void Zone::setFloorHold(const std::string& ident, uint64_t target, bool hold)
258{
259 using namespace std::string_literals;
260
Matthew Barthdc3152d2022-03-16 14:43:13 -0500261 if (target > _ceiling)
262 {
263 target = _ceiling;
264 }
265
Matt Spinler40554d82021-10-26 15:23:59 -0500266 if (!hold)
267 {
268 size_t removed = _floorHolds.erase(ident);
269 if (removed)
270 {
271 FlightRecorder::instance().log(
272 "zone-floor"s + getName(),
Patrick Williamsfbf47032023-07-17 12:27:34 -0500273 std::format("{} is removing floor hold", ident));
Matt Spinler40554d82021-10-26 15:23:59 -0500274 }
275 }
276 else
277 {
278 if (!((_floorHolds.find(ident) != _floorHolds.end()) &&
279 (_floorHolds[ident] == target)))
280 {
281 FlightRecorder::instance().log(
282 "zone-floor"s + getName(),
Patrick Williamsfbf47032023-07-17 12:27:34 -0500283 std::format("{} is setting floor hold to {}", ident, target));
Matt Spinler40554d82021-10-26 15:23:59 -0500284 }
285 _floorHolds[ident] = target;
286 }
287
288 if (!std::all_of(_floorChange.begin(), _floorChange.end(),
289 [](const auto& entry) { return entry.second; }))
290 {
291 return;
292 }
293
294 auto itHoldMax = std::max_element(_floorHolds.begin(), _floorHolds.end(),
295 [](const auto& aHold, const auto& bHold) {
Patrick Williams61b73292023-05-10 07:50:12 -0500296 return aHold.second < bHold.second;
297 });
Matt Spinler40554d82021-10-26 15:23:59 -0500298 if (itHoldMax == _floorHolds.end())
299 {
300 if (_floor != _defaultFloor)
301 {
302 FlightRecorder::instance().log(
303 "zone-floor"s + getName(),
Patrick Williamsfbf47032023-07-17 12:27:34 -0500304 std::format("No set floor exists, using default floor",
Matt Spinler40554d82021-10-26 15:23:59 -0500305 _defaultFloor));
306 }
307 _floor = _defaultFloor;
308 }
309 else
310 {
311 if (_floor != itHoldMax->second)
312 {
313 FlightRecorder::instance().log(
314 "zone-floor"s + getName(),
Patrick Williamsfbf47032023-07-17 12:27:34 -0500315 std::format("Setting new floor to {}", itHoldMax->second));
Matt Spinler40554d82021-10-26 15:23:59 -0500316 }
317 _floor = itHoldMax->second;
318 }
319
320 // Floor above target, update target to floor
321 if (_target < _floor)
322 {
323 requestIncrease(_floor - _target);
324 }
325}
326
Matthew Barth12cb1252021-03-08 16:47:30 -0600327void Zone::setFloor(uint64_t target)
328{
329 // Check all entries are set to allow floor to be set
Matthew Barth8ba715e2021-03-05 09:00:05 -0600330 auto pred = [](const auto& entry) { return entry.second; };
Matthew Barth12cb1252021-03-08 16:47:30 -0600331 if (std::all_of(_floorChange.begin(), _floorChange.end(), pred))
332 {
Matthew Barthdc3152d2022-03-16 14:43:13 -0500333 _floor = (target > _ceiling) ? _ceiling : target;
Matthew Barth12cb1252021-03-08 16:47:30 -0600334 // Floor above target, update target to floor
335 if (_target < _floor)
336 {
337 requestIncrease(_floor - _target);
338 }
339 }
340}
341
342void Zone::requestIncrease(uint64_t targetDelta)
343{
Matthew Barth2b3253e2021-03-09 14:51:16 -0600344 // Only increase when delta is higher than the current increase delta for
345 // the zone and currently under ceiling
346 if (targetDelta > _incDelta && _target < _ceiling)
347 {
348 auto requestTarget = getRequestTargetBase();
349 requestTarget = (targetDelta - _incDelta) + requestTarget;
350 _incDelta = targetDelta;
351 // Target can not go above a current ceiling
352 if (requestTarget > _ceiling)
353 {
354 requestTarget = _ceiling;
355 }
Matthew Barth8ba715e2021-03-05 09:00:05 -0600356 setTarget(requestTarget);
Matthew Barth007de092021-03-25 13:56:04 -0500357 // Restart timer countdown for fan target increase
358 _incTimer.restartOnce(_incDelay);
Matthew Barth2b3253e2021-03-09 14:51:16 -0600359 }
Matthew Barth12cb1252021-03-08 16:47:30 -0600360}
361
Matthew Barth007de092021-03-25 13:56:04 -0500362void Zone::incTimerExpired()
363{
364 // Clear increase delta when timer expires allowing additional target
365 // increase requests or target decreases to occur
366 _incDelta = 0;
367}
368
Matthew Barth45c44ea2021-03-03 13:16:14 -0600369void Zone::requestDecrease(uint64_t targetDelta)
370{
371 // Only decrease the lowest target delta requested
372 if (_decDelta == 0 || targetDelta < _decDelta)
373 {
374 _decDelta = targetDelta;
375 }
376}
377
Matthew Barth007de092021-03-25 13:56:04 -0500378void Zone::decTimerExpired()
379{
380 // Check all entries are set to allow a decrease
Patrick Williams61b73292023-05-10 07:50:12 -0500381 auto pred = [](const auto& entry) { return entry.second; };
Matthew Barth007de092021-03-25 13:56:04 -0500382 auto decAllowed = std::all_of(_decAllowed.begin(), _decAllowed.end(), pred);
383
384 // Only decrease targets when allowed, a requested decrease target delta
385 // exists, where no requested increases exist and the increase timer is not
386 // running (i.e. not in the middle of increasing)
387 if (decAllowed && _decDelta != 0 && _incDelta == 0 &&
388 !_incTimer.isEnabled())
389 {
390 auto requestTarget = getRequestTargetBase();
391 // Request target should not start above ceiling
392 if (requestTarget > _ceiling)
393 {
394 requestTarget = _ceiling;
395 }
396 // Target can not go below the defined floor
397 if ((requestTarget < _decDelta) || (requestTarget - _decDelta < _floor))
398 {
399 requestTarget = _floor;
400 }
401 else
402 {
403 requestTarget = requestTarget - _decDelta;
404 }
405 setTarget(requestTarget);
406 }
407 // Clear decrease delta when timer expires
408 _decDelta = 0;
409 // Decrease timer is restarted since its repeating
410}
411
Matthew Bartha0dd1352021-03-09 11:10:49 -0600412void Zone::setPersisted(const std::string& intf, const std::string& prop)
413{
414 if (std::find_if(_propsPersisted[intf].begin(), _propsPersisted[intf].end(),
Matthew Barthbc89a8a2021-05-25 15:42:58 -0500415 [&prop](const auto& p) { return prop == p; }) ==
Matthew Bartha0dd1352021-03-09 11:10:49 -0600416 _propsPersisted[intf].end())
417 {
418 _propsPersisted[intf].emplace_back(prop);
419 }
420}
421
Matthew Barth279183f2021-05-25 10:19:43 -0500422bool Zone::isPersisted(const std::string& intf, const std::string& prop) const
423{
424 auto it = _propsPersisted.find(intf);
425 if (it == _propsPersisted.end())
426 {
427 return false;
428 }
429
430 return std::any_of(it->second.begin(), it->second.end(),
431 [&prop](const auto& p) { return prop == p; });
432}
433
Matthew Barthab8e4b82021-05-27 13:25:46 -0500434void Zone::setPowerOnTarget(const json& jsonObj)
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500435{
Matthew Barth12e888f2021-08-20 11:41:32 -0500436 if (!jsonObj.contains("poweron_target"))
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500437 {
Matthew Barthab8e4b82021-05-27 13:25:46 -0500438 auto msg = "Missing required zone's poweron target";
439 log<level::ERR>(msg, entry("JSON=%s", jsonObj.dump().c_str()));
440 throw std::runtime_error(msg);
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500441 }
Matthew Barth12e888f2021-08-20 11:41:32 -0500442 _poweronTarget = jsonObj["poweron_target"].get<uint64_t>();
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500443}
444
Matthew Barth651f03a2020-08-27 16:15:11 -0500445void Zone::setInterfaces(const json& jsonObj)
446{
447 for (const auto& interface : jsonObj["interfaces"])
448 {
449 if (!interface.contains("name") || !interface.contains("properties"))
450 {
451 log<level::ERR>("Missing required zone interface attributes",
452 entry("JSON=%s", interface.dump().c_str()));
453 throw std::runtime_error(
454 "Missing required zone interface attributes");
455 }
Matthew Barth216229c2020-09-24 13:47:33 -0500456 auto propFuncs =
457 _intfPropHandlers.find(interface["name"].get<std::string>());
458 if (propFuncs == _intfPropHandlers.end())
459 {
460 // Construct list of available configurable interfaces
Patrick Williams5e15c3b2023-10-20 11:18:11 -0500461 auto intfs = std::accumulate(
462 std::next(_intfPropHandlers.begin()), _intfPropHandlers.end(),
463 _intfPropHandlers.begin()->first, [](auto list, auto intf) {
Patrick Williams61b73292023-05-10 07:50:12 -0500464 return std::move(list) + ", " + intf.first;
465 });
Matthew Barth216229c2020-09-24 13:47:33 -0500466 log<level::ERR>("Configured interface not available",
467 entry("JSON=%s", interface.dump().c_str()),
468 entry("AVAILABLE_INTFS=%s", intfs.c_str()));
469 throw std::runtime_error("Configured interface not available");
470 }
471
Matthew Barth651f03a2020-08-27 16:15:11 -0500472 for (const auto& property : interface["properties"])
473 {
474 if (!property.contains("name"))
475 {
476 log<level::ERR>(
477 "Missing required interface property attributes",
478 entry("JSON=%s", property.dump().c_str()));
479 throw std::runtime_error(
480 "Missing required interface property attributes");
481 }
482 // Attribute "persist" is optional, defaults to `false`
483 auto persist = false;
484 if (property.contains("persist"))
485 {
486 persist = property["persist"].get<bool>();
487 }
488 // Property name from JSON must exactly match supported
489 // index names to functions in property namespace
Matthew Barth216229c2020-09-24 13:47:33 -0500490 auto propFunc =
491 propFuncs->second.find(property["name"].get<std::string>());
492 if (propFunc == propFuncs->second.end())
Matthew Barth651f03a2020-08-27 16:15:11 -0500493 {
Matthew Barth216229c2020-09-24 13:47:33 -0500494 // Construct list of available configurable properties
Matthew Barth651f03a2020-08-27 16:15:11 -0500495 auto props = std::accumulate(
Matthew Barth216229c2020-09-24 13:47:33 -0500496 std::next(propFuncs->second.begin()),
497 propFuncs->second.end(), propFuncs->second.begin()->first,
498 [](auto list, auto prop) {
Patrick Williams61b73292023-05-10 07:50:12 -0500499 return std::move(list) + ", " + prop.first;
Patrick Williams5e15c3b2023-10-20 11:18:11 -0500500 });
Matthew Barth216229c2020-09-24 13:47:33 -0500501 log<level::ERR>("Configured property not available",
Matthew Barth651f03a2020-08-27 16:15:11 -0500502 entry("JSON=%s", property.dump().c_str()),
503 entry("AVAILABLE_PROPS=%s", props.c_str()));
504 throw std::runtime_error(
505 "Configured property function not available");
506 }
Matthew Barthbc89a8a2021-05-25 15:42:58 -0500507
508 _propInitFunctions.emplace_back(
509 propFunc->second(property, persist));
Matthew Barth651f03a2020-08-27 16:15:11 -0500510 }
Matthew Barth651f03a2020-08-27 16:15:11 -0500511 }
512}
513
Matt Spinler9db6dd12021-10-29 16:10:08 -0500514json Zone::dump() const
515{
516 json output;
517
518 output["active"] = _isActive;
519 output["floor"] = _floor;
Matthew Barth077448a2021-12-02 20:38:15 -0600520 output["ceiling"] = _ceiling;
Matt Spinler9db6dd12021-10-29 16:10:08 -0500521 output["target"] = _target;
522 output["increase_delta"] = _incDelta;
523 output["decrease_delta"] = _decDelta;
524 output["power_on_target"] = _poweronTarget;
525 output["default_ceiling"] = _defaultCeiling;
526 output["default_floor"] = _defaultFloor;
527 output["increase_delay"] = _incDelay.count();
528 output["decrease_interval"] = _decInterval.count();
529 output["requested_target_base"] = _requestTargetBase;
530 output["floor_change"] = _floorChange;
531 output["decrease_allowed"] = _decAllowed;
532 output["persisted_props"] = _propsPersisted;
Matt Spinler40554d82021-10-26 15:23:59 -0500533 output["target_holds"] = _targetHolds;
534 output["floor_holds"] = _floorHolds;
Matt Spinler9db6dd12021-10-29 16:10:08 -0500535
Matt Spinler2541f992022-08-16 16:00:04 -0500536 std::map<std::string, std::vector<uint64_t>> lockedTargets;
537 for (const auto& fan : _fans)
538 {
539 const auto& locks = fan->getLockedTargets();
540 if (!locks.empty())
541 {
542 lockedTargets[fan->getName()] = locks;
543 }
544 }
545 output["target_locks"] = lockedTargets;
546
Matt Spinler9db6dd12021-10-29 16:10:08 -0500547 return output;
548}
549
Matthew Barth651f03a2020-08-27 16:15:11 -0500550/**
Matthew Barth216229c2020-09-24 13:47:33 -0500551 * Properties of interfaces supported by the zone configuration that return
Matthew Barthab8e4b82021-05-27 13:25:46 -0500552 * a handler function that sets the zone's property value(s) and persist
553 * state.
Matthew Barth651f03a2020-08-27 16:15:11 -0500554 */
555namespace zone::property
556{
Matthew Barthb584d812021-03-11 15:55:04 -0600557// Get a set property handler function for the configured values of the
558// "Supported" property
Matthew Barthbc89a8a2021-05-25 15:42:58 -0500559std::function<void(DBusZone&, Zone&)> supported(const json& jsonObj,
560 bool persist)
Matthew Barth651f03a2020-08-27 16:15:11 -0500561{
562 std::vector<std::string> values;
Matthew Barth216229c2020-09-24 13:47:33 -0500563 if (!jsonObj.contains("values"))
Matthew Barth651f03a2020-08-27 16:15:11 -0500564 {
Matthew Barthab8e4b82021-05-27 13:25:46 -0500565 log<level::ERR>("No 'values' found for \"Supported\" property, "
566 "using an empty list",
567 entry("JSON=%s", jsonObj.dump().c_str()));
Matthew Barth216229c2020-09-24 13:47:33 -0500568 }
569 else
570 {
571 for (const auto& value : jsonObj["values"])
572 {
573 if (!value.contains("value"))
574 {
575 log<level::ERR>("No 'value' found for \"Supported\" property "
576 "entry, skipping",
577 entry("JSON=%s", value.dump().c_str()));
578 }
579 else
580 {
581 values.emplace_back(value["value"].get<std::string>());
582 }
583 }
Matthew Barth651f03a2020-08-27 16:15:11 -0500584 }
585
Matthew Barthb584d812021-03-11 15:55:04 -0600586 return Zone::setProperty<std::vector<std::string>>(
Matthew Barthbc89a8a2021-05-25 15:42:58 -0500587 DBusZone::thermalModeIntf, DBusZone::supportedProp,
588 &DBusZone::supported, std::move(values), persist);
Matthew Barth651f03a2020-08-27 16:15:11 -0500589}
590
Matthew Barthab8e4b82021-05-27 13:25:46 -0500591// Get a set property handler function for a configured value of the
592// "Current" property
Matthew Barthbc89a8a2021-05-25 15:42:58 -0500593std::function<void(DBusZone&, Zone&)> current(const json& jsonObj, bool persist)
Matthew Barth651f03a2020-08-27 16:15:11 -0500594{
Matthew Barth216229c2020-09-24 13:47:33 -0500595 // Use default value for "Current" property if no "value" entry given
596 if (!jsonObj.contains("value"))
597 {
Matthew Barthb584d812021-03-11 15:55:04 -0600598 log<level::INFO>("No 'value' found for \"Current\" property, "
599 "using default",
600 entry("JSON=%s", jsonObj.dump().c_str()));
601 // Set persist state of property
Matthew Barthbc89a8a2021-05-25 15:42:58 -0500602 return Zone::setPropertyPersist(DBusZone::thermalModeIntf,
603 DBusZone::currentProp, persist);
Matthew Barth216229c2020-09-24 13:47:33 -0500604 }
605
Matthew Barthb584d812021-03-11 15:55:04 -0600606 return Zone::setProperty<std::string>(
Matthew Barthbc89a8a2021-05-25 15:42:58 -0500607 DBusZone::thermalModeIntf, DBusZone::currentProp, &DBusZone::current,
Matthew Barthb584d812021-03-11 15:55:04 -0600608 jsonObj["value"].get<std::string>(), persist);
Matthew Barth651f03a2020-08-27 16:15:11 -0500609}
Matthew Barthb584d812021-03-11 15:55:04 -0600610
Matthew Barth651f03a2020-08-27 16:15:11 -0500611} // namespace zone::property
612
Matthew Barth4f0d3b72020-08-27 14:32:15 -0500613} // namespace phosphor::fan::control::json