| Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 1 | /** | 
| Mike Capps | 762e858 | 2021-10-07 15:33:23 -0400 | [diff] [blame] | 2 | * Copyright © 2022 IBM Corporation | 
| Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 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 |  | 
| Matt Spinler | 40554d8 | 2021-10-26 15:23:59 -0500 | [diff] [blame] | 18 | #include "../utils/flight_recorder.hpp" | 
| Matthew Barth | bc89a8a | 2021-05-25 15:42:58 -0500 | [diff] [blame] | 19 | #include "dbus_zone.hpp" | 
| Matthew Barth | de90fb4 | 2021-03-04 16:34:28 -0600 | [diff] [blame] | 20 | #include "fan.hpp" | 
| Matthew Barth | 9403a21 | 2021-05-17 09:31:50 -0500 | [diff] [blame] | 21 | #include "sdbusplus.hpp" | 
| Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 22 |  | 
| Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 23 | #include <nlohmann/json.hpp> | 
|  | 24 | #include <phosphor-logging/log.hpp> | 
| Matthew Barth | 603ef16 | 2021-03-24 15:34:53 -0500 | [diff] [blame] | 25 | #include <sdeventplus/event.hpp> | 
| Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 26 |  | 
| Matthew Barth | a0dd135 | 2021-03-09 11:10:49 -0600 | [diff] [blame] | 27 | #include <algorithm> | 
| Matthew Barth | 007de09 | 2021-03-25 13:56:04 -0500 | [diff] [blame] | 28 | #include <chrono> | 
| Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 29 | #include <iterator> | 
|  | 30 | #include <map> | 
| Matthew Barth | bc89a8a | 2021-05-25 15:42:58 -0500 | [diff] [blame] | 31 | #include <memory> | 
| Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 32 | #include <numeric> | 
| Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 33 | #include <utility> | 
| Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 34 | #include <vector> | 
| Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 35 |  | 
| Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 36 | namespace phosphor::fan::control::json | 
|  | 37 | { | 
|  | 38 |  | 
|  | 39 | using json = nlohmann::json; | 
|  | 40 | using namespace phosphor::logging; | 
|  | 41 |  | 
| Matthew Barth | bc89a8a | 2021-05-25 15:42:58 -0500 | [diff] [blame] | 42 | const 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 Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 50 |  | 
| Matthew Barth | 9403a21 | 2021-05-17 09:31:50 -0500 | [diff] [blame] | 51 | Zone::Zone(const json& jsonObj, const sdeventplus::Event& event, Manager* mgr) : | 
| Matthew Barth | ab8e4b8 | 2021-05-27 13:25:46 -0500 | [diff] [blame] | 52 | ConfigBase(jsonObj), _dbusZone{}, _manager(mgr), _defaultFloor(0), | 
| Matthew Barth | 2504c77 | 2021-05-27 14:02:31 -0500 | [diff] [blame] | 53 | _incDelay(0), _decInterval(0), _floor(0), _target(0), _incDelta(0), | 
|  | 54 | _decDelta(0), _requestTargetBase(0), _isActive(true), | 
| Matthew Barth | ab8e4b8 | 2021-05-27 13:25:46 -0500 | [diff] [blame] | 55 | _incTimer(event, std::bind(&Zone::incTimerExpired, this)), | 
| Matthew Barth | 007de09 | 2021-03-25 13:56:04 -0500 | [diff] [blame] | 56 | _decTimer(event, std::bind(&Zone::decTimerExpired, this)) | 
| Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 57 | { | 
| Matthew Barth | e47c958 | 2021-03-09 14:24:02 -0600 | [diff] [blame] | 58 | // Increase delay is optional, defaults to 0 | 
| Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 59 | if (jsonObj.contains("increase_delay")) | 
|  | 60 | { | 
| Matthew Barth | 007de09 | 2021-03-25 13:56:04 -0500 | [diff] [blame] | 61 | _incDelay = | 
|  | 62 | std::chrono::seconds(jsonObj["increase_delay"].get<uint64_t>()); | 
| Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 63 | } | 
| Matthew Barth | ab8e4b8 | 2021-05-27 13:25:46 -0500 | [diff] [blame] | 64 |  | 
|  | 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 Barth | dc3152d | 2022-03-16 14:43:13 -0500 | [diff] [blame] | 81 | if (_defaultFloor > _ceiling) | 
|  | 82 | { | 
|  | 83 | log<level::ERR>( | 
|  | 84 | fmt::format("Configured default_floor({}) above ceiling({}), " | 
|  | 85 | "setting default floor to ceiling", | 
|  | 86 | _defaultFloor, _ceiling) | 
|  | 87 | .c_str()); | 
|  | 88 | _defaultFloor = _ceiling; | 
|  | 89 | } | 
| Matthew Barth | ab8e4b8 | 2021-05-27 13:25:46 -0500 | [diff] [blame] | 90 | // Start with the current floor set as the default | 
|  | 91 | _floor = _defaultFloor; | 
|  | 92 | } | 
|  | 93 |  | 
| Matthew Barth | 2504c77 | 2021-05-27 14:02:31 -0500 | [diff] [blame] | 94 | // 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 Barth | ab8e4b8 | 2021-05-27 13:25:46 -0500 | [diff] [blame] | 101 |  | 
| Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 102 | // Setting properties on interfaces to be served are optional | 
|  | 103 | if (jsonObj.contains("interfaces")) | 
|  | 104 | { | 
|  | 105 | setInterfaces(jsonObj); | 
|  | 106 | } | 
| Matthew Barth | 14303a4 | 2021-05-21 13:04:08 -0500 | [diff] [blame] | 107 | } | 
| Matthew Barth | 007de09 | 2021-03-25 13:56:04 -0500 | [diff] [blame] | 108 |  | 
| Matthew Barth | 14303a4 | 2021-05-21 13:04:08 -0500 | [diff] [blame] | 109 | void Zone::enable() | 
|  | 110 | { | 
| Matthew Barth | bc89a8a | 2021-05-25 15:42:58 -0500 | [diff] [blame] | 111 | // Create thermal control dbus object | 
|  | 112 | _dbusZone = std::make_unique<DBusZone>(*this); | 
| Matthew Barth | a448374 | 2021-03-25 14:09:01 -0500 | [diff] [blame] | 113 |  | 
| Matthew Barth | bc89a8a | 2021-05-25 15:42:58 -0500 | [diff] [blame] | 114 | // 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 Barth | a448374 | 2021-03-25 14:09:01 -0500 | [diff] [blame] | 130 |  | 
| Matthew Barth | 2504c77 | 2021-05-27 14:02:31 -0500 | [diff] [blame] | 131 | // 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 Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 137 | } | 
|  | 138 |  | 
| Matthew Barth | de90fb4 | 2021-03-04 16:34:28 -0600 | [diff] [blame] | 139 | void Zone::addFan(std::unique_ptr<Fan> fan) | 
|  | 140 | { | 
|  | 141 | _fans.emplace_back(std::move(fan)); | 
|  | 142 | } | 
|  | 143 |  | 
| Matthew Barth | 8ba715e | 2021-03-05 09:00:05 -0600 | [diff] [blame] | 144 | void Zone::setTarget(uint64_t target) | 
|  | 145 | { | 
| Matt Spinler | 5a2b501 | 2021-07-22 14:13:19 -0600 | [diff] [blame] | 146 | if (_isActive) | 
| Matthew Barth | 8ba715e | 2021-03-05 09:00:05 -0600 | [diff] [blame] | 147 | { | 
|  | 148 | _target = target; | 
|  | 149 | for (auto& fan : _fans) | 
|  | 150 | { | 
|  | 151 | fan->setTarget(_target); | 
|  | 152 | } | 
|  | 153 | } | 
|  | 154 | } | 
|  | 155 |  | 
| Mike Capps | 762e858 | 2021-10-07 15:33:23 -0400 | [diff] [blame] | 156 | void Zone::lockFanTarget(const std::string& fname, uint64_t target) | 
|  | 157 | { | 
|  | 158 | auto fanItr = | 
|  | 159 | std::find_if(_fans.begin(), _fans.end(), [&fname](const auto& fan) { | 
|  | 160 | return fan->getName() == fname; | 
|  | 161 | }); | 
|  | 162 |  | 
|  | 163 | if (_fans.end() != fanItr) | 
|  | 164 | { | 
|  | 165 | (*fanItr)->lockTarget(target); | 
|  | 166 | } | 
|  | 167 | else | 
|  | 168 | { | 
|  | 169 | log<level::DEBUG>( | 
|  | 170 | fmt::format("Configured fan {} not found in zone {} to lock target", | 
|  | 171 | fname, getName()) | 
|  | 172 | .c_str()); | 
|  | 173 | } | 
|  | 174 | } | 
|  | 175 |  | 
|  | 176 | void Zone::unlockFanTarget(const std::string& fname, uint64_t target) | 
|  | 177 | { | 
|  | 178 | auto fanItr = | 
|  | 179 | std::find_if(_fans.begin(), _fans.end(), [&fname](const auto& fan) { | 
|  | 180 | return fan->getName() == fname; | 
|  | 181 | }); | 
|  | 182 |  | 
|  | 183 | if (_fans.end() != fanItr) | 
|  | 184 | { | 
|  | 185 | (*fanItr)->unlockTarget(target); | 
|  | 186 |  | 
|  | 187 | // attempt to resume Zone target on fan | 
|  | 188 | (*fanItr)->setTarget(getTarget()); | 
|  | 189 | } | 
|  | 190 | else | 
|  | 191 | { | 
|  | 192 | log<level::DEBUG>( | 
|  | 193 | fmt::format( | 
|  | 194 | "Configured fan {} not found in zone {} to unlock target", | 
|  | 195 | fname, getName()) | 
|  | 196 | .c_str()); | 
|  | 197 | } | 
|  | 198 | } | 
|  | 199 |  | 
| Matthew Barth | 5368011 | 2021-09-23 11:20:41 -0500 | [diff] [blame] | 200 | void Zone::setTargetHold(const std::string& ident, uint64_t target, bool hold) | 
|  | 201 | { | 
| Matt Spinler | 9867161 | 2021-12-09 14:27:15 -0600 | [diff] [blame] | 202 | using namespace std::string_literals; | 
|  | 203 |  | 
| Matthew Barth | 5368011 | 2021-09-23 11:20:41 -0500 | [diff] [blame] | 204 | if (!hold) | 
|  | 205 | { | 
| Matt Spinler | 9867161 | 2021-12-09 14:27:15 -0600 | [diff] [blame] | 206 | size_t removed = _targetHolds.erase(ident); | 
|  | 207 | if (removed) | 
|  | 208 | { | 
|  | 209 | FlightRecorder::instance().log( | 
|  | 210 | "zone-target"s + getName(), | 
|  | 211 | fmt::format("{} is removing target hold", ident)); | 
|  | 212 | } | 
| Matthew Barth | 5368011 | 2021-09-23 11:20:41 -0500 | [diff] [blame] | 213 | } | 
|  | 214 | else | 
|  | 215 | { | 
| Matt Spinler | 9867161 | 2021-12-09 14:27:15 -0600 | [diff] [blame] | 216 | if (!((_targetHolds.find(ident) != _targetHolds.end()) && | 
|  | 217 | (_targetHolds[ident] == target))) | 
|  | 218 | { | 
|  | 219 | FlightRecorder::instance().log( | 
|  | 220 | "zone-target"s + getName(), | 
|  | 221 | fmt::format("{} is setting target hold to {}", ident, target)); | 
|  | 222 | } | 
| Matt Spinler | 40554d8 | 2021-10-26 15:23:59 -0500 | [diff] [blame] | 223 | _targetHolds[ident] = target; | 
| Matthew Barth | 5368011 | 2021-09-23 11:20:41 -0500 | [diff] [blame] | 224 | _isActive = false; | 
|  | 225 | } | 
|  | 226 |  | 
| Matt Spinler | 40554d8 | 2021-10-26 15:23:59 -0500 | [diff] [blame] | 227 | auto itHoldMax = std::max_element(_targetHolds.begin(), _targetHolds.end(), | 
| Matthew Barth | 5368011 | 2021-09-23 11:20:41 -0500 | [diff] [blame] | 228 | [](const auto& aHold, const auto& bHold) { | 
|  | 229 | return aHold.second < bHold.second; | 
|  | 230 | }); | 
| Matt Spinler | 40554d8 | 2021-10-26 15:23:59 -0500 | [diff] [blame] | 231 | if (itHoldMax == _targetHolds.end()) | 
| Matthew Barth | 5368011 | 2021-09-23 11:20:41 -0500 | [diff] [blame] | 232 | { | 
|  | 233 | _isActive = true; | 
|  | 234 | } | 
|  | 235 | else | 
|  | 236 | { | 
| Matt Spinler | 9867161 | 2021-12-09 14:27:15 -0600 | [diff] [blame] | 237 | if (_target != itHoldMax->second) | 
|  | 238 | { | 
|  | 239 | FlightRecorder::instance().log( | 
|  | 240 | "zone-target"s + getName(), | 
|  | 241 | fmt::format("Settings fans to target hold of {}", | 
|  | 242 | itHoldMax->second)); | 
|  | 243 | } | 
|  | 244 |  | 
| Matthew Barth | 5368011 | 2021-09-23 11:20:41 -0500 | [diff] [blame] | 245 | _target = itHoldMax->second; | 
|  | 246 | for (auto& fan : _fans) | 
|  | 247 | { | 
|  | 248 | fan->setTarget(_target); | 
|  | 249 | } | 
|  | 250 | } | 
|  | 251 | } | 
|  | 252 |  | 
| Matt Spinler | 40554d8 | 2021-10-26 15:23:59 -0500 | [diff] [blame] | 253 | void Zone::setFloorHold(const std::string& ident, uint64_t target, bool hold) | 
|  | 254 | { | 
|  | 255 | using namespace std::string_literals; | 
|  | 256 |  | 
| Matthew Barth | dc3152d | 2022-03-16 14:43:13 -0500 | [diff] [blame] | 257 | if (target > _ceiling) | 
|  | 258 | { | 
|  | 259 | target = _ceiling; | 
|  | 260 | } | 
|  | 261 |  | 
| Matt Spinler | 40554d8 | 2021-10-26 15:23:59 -0500 | [diff] [blame] | 262 | if (!hold) | 
|  | 263 | { | 
|  | 264 | size_t removed = _floorHolds.erase(ident); | 
|  | 265 | if (removed) | 
|  | 266 | { | 
|  | 267 | FlightRecorder::instance().log( | 
|  | 268 | "zone-floor"s + getName(), | 
|  | 269 | fmt::format("{} is removing floor hold", ident)); | 
|  | 270 | } | 
|  | 271 | } | 
|  | 272 | else | 
|  | 273 | { | 
|  | 274 | if (!((_floorHolds.find(ident) != _floorHolds.end()) && | 
|  | 275 | (_floorHolds[ident] == target))) | 
|  | 276 | { | 
|  | 277 | FlightRecorder::instance().log( | 
|  | 278 | "zone-floor"s + getName(), | 
|  | 279 | fmt::format("{} is setting floor hold to {}", ident, target)); | 
|  | 280 | } | 
|  | 281 | _floorHolds[ident] = target; | 
|  | 282 | } | 
|  | 283 |  | 
|  | 284 | if (!std::all_of(_floorChange.begin(), _floorChange.end(), | 
|  | 285 | [](const auto& entry) { return entry.second; })) | 
|  | 286 | { | 
|  | 287 | return; | 
|  | 288 | } | 
|  | 289 |  | 
|  | 290 | auto itHoldMax = std::max_element(_floorHolds.begin(), _floorHolds.end(), | 
|  | 291 | [](const auto& aHold, const auto& bHold) { | 
|  | 292 | return aHold.second < bHold.second; | 
|  | 293 | }); | 
|  | 294 | if (itHoldMax == _floorHolds.end()) | 
|  | 295 | { | 
|  | 296 | if (_floor != _defaultFloor) | 
|  | 297 | { | 
|  | 298 | FlightRecorder::instance().log( | 
|  | 299 | "zone-floor"s + getName(), | 
|  | 300 | fmt::format("No set floor exists, using default floor", | 
|  | 301 | _defaultFloor)); | 
|  | 302 | } | 
|  | 303 | _floor = _defaultFloor; | 
|  | 304 | } | 
|  | 305 | else | 
|  | 306 | { | 
|  | 307 | if (_floor != itHoldMax->second) | 
|  | 308 | { | 
|  | 309 | FlightRecorder::instance().log( | 
|  | 310 | "zone-floor"s + getName(), | 
|  | 311 | fmt::format("Setting new floor to {}", itHoldMax->second)); | 
|  | 312 | } | 
|  | 313 | _floor = itHoldMax->second; | 
|  | 314 | } | 
|  | 315 |  | 
|  | 316 | // Floor above target, update target to floor | 
|  | 317 | if (_target < _floor) | 
|  | 318 | { | 
|  | 319 | requestIncrease(_floor - _target); | 
|  | 320 | } | 
|  | 321 | } | 
|  | 322 |  | 
| Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 323 | void Zone::setFloor(uint64_t target) | 
|  | 324 | { | 
|  | 325 | // Check all entries are set to allow floor to be set | 
| Matthew Barth | 8ba715e | 2021-03-05 09:00:05 -0600 | [diff] [blame] | 326 | auto pred = [](const auto& entry) { return entry.second; }; | 
| Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 327 | if (std::all_of(_floorChange.begin(), _floorChange.end(), pred)) | 
|  | 328 | { | 
| Matthew Barth | dc3152d | 2022-03-16 14:43:13 -0500 | [diff] [blame] | 329 | _floor = (target > _ceiling) ? _ceiling : target; | 
| Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 330 | // Floor above target, update target to floor | 
|  | 331 | if (_target < _floor) | 
|  | 332 | { | 
|  | 333 | requestIncrease(_floor - _target); | 
|  | 334 | } | 
|  | 335 | } | 
|  | 336 | } | 
|  | 337 |  | 
|  | 338 | void Zone::requestIncrease(uint64_t targetDelta) | 
|  | 339 | { | 
| Matthew Barth | 2b3253e | 2021-03-09 14:51:16 -0600 | [diff] [blame] | 340 | // Only increase when delta is higher than the current increase delta for | 
|  | 341 | // the zone and currently under ceiling | 
|  | 342 | if (targetDelta > _incDelta && _target < _ceiling) | 
|  | 343 | { | 
|  | 344 | auto requestTarget = getRequestTargetBase(); | 
|  | 345 | requestTarget = (targetDelta - _incDelta) + requestTarget; | 
|  | 346 | _incDelta = targetDelta; | 
|  | 347 | // Target can not go above a current ceiling | 
|  | 348 | if (requestTarget > _ceiling) | 
|  | 349 | { | 
|  | 350 | requestTarget = _ceiling; | 
|  | 351 | } | 
| Matthew Barth | 8ba715e | 2021-03-05 09:00:05 -0600 | [diff] [blame] | 352 | setTarget(requestTarget); | 
| Matthew Barth | 007de09 | 2021-03-25 13:56:04 -0500 | [diff] [blame] | 353 | // Restart timer countdown for fan target increase | 
|  | 354 | _incTimer.restartOnce(_incDelay); | 
| Matthew Barth | 2b3253e | 2021-03-09 14:51:16 -0600 | [diff] [blame] | 355 | } | 
| Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 356 | } | 
|  | 357 |  | 
| Matthew Barth | 007de09 | 2021-03-25 13:56:04 -0500 | [diff] [blame] | 358 | void Zone::incTimerExpired() | 
|  | 359 | { | 
|  | 360 | // Clear increase delta when timer expires allowing additional target | 
|  | 361 | // increase requests or target decreases to occur | 
|  | 362 | _incDelta = 0; | 
|  | 363 | } | 
|  | 364 |  | 
| Matthew Barth | 45c44ea | 2021-03-03 13:16:14 -0600 | [diff] [blame] | 365 | void Zone::requestDecrease(uint64_t targetDelta) | 
|  | 366 | { | 
|  | 367 | // Only decrease the lowest target delta requested | 
|  | 368 | if (_decDelta == 0 || targetDelta < _decDelta) | 
|  | 369 | { | 
|  | 370 | _decDelta = targetDelta; | 
|  | 371 | } | 
|  | 372 | } | 
|  | 373 |  | 
| Matthew Barth | 007de09 | 2021-03-25 13:56:04 -0500 | [diff] [blame] | 374 | void Zone::decTimerExpired() | 
|  | 375 | { | 
|  | 376 | // Check all entries are set to allow a decrease | 
|  | 377 | auto pred = [](auto const& entry) { return entry.second; }; | 
|  | 378 | auto decAllowed = std::all_of(_decAllowed.begin(), _decAllowed.end(), pred); | 
|  | 379 |  | 
|  | 380 | // Only decrease targets when allowed, a requested decrease target delta | 
|  | 381 | // exists, where no requested increases exist and the increase timer is not | 
|  | 382 | // running (i.e. not in the middle of increasing) | 
|  | 383 | if (decAllowed && _decDelta != 0 && _incDelta == 0 && | 
|  | 384 | !_incTimer.isEnabled()) | 
|  | 385 | { | 
|  | 386 | auto requestTarget = getRequestTargetBase(); | 
|  | 387 | // Request target should not start above ceiling | 
|  | 388 | if (requestTarget > _ceiling) | 
|  | 389 | { | 
|  | 390 | requestTarget = _ceiling; | 
|  | 391 | } | 
|  | 392 | // Target can not go below the defined floor | 
|  | 393 | if ((requestTarget < _decDelta) || (requestTarget - _decDelta < _floor)) | 
|  | 394 | { | 
|  | 395 | requestTarget = _floor; | 
|  | 396 | } | 
|  | 397 | else | 
|  | 398 | { | 
|  | 399 | requestTarget = requestTarget - _decDelta; | 
|  | 400 | } | 
|  | 401 | setTarget(requestTarget); | 
|  | 402 | } | 
|  | 403 | // Clear decrease delta when timer expires | 
|  | 404 | _decDelta = 0; | 
|  | 405 | // Decrease timer is restarted since its repeating | 
|  | 406 | } | 
|  | 407 |  | 
| Matthew Barth | a0dd135 | 2021-03-09 11:10:49 -0600 | [diff] [blame] | 408 | void Zone::setPersisted(const std::string& intf, const std::string& prop) | 
|  | 409 | { | 
|  | 410 | if (std::find_if(_propsPersisted[intf].begin(), _propsPersisted[intf].end(), | 
| Matthew Barth | bc89a8a | 2021-05-25 15:42:58 -0500 | [diff] [blame] | 411 | [&prop](const auto& p) { return prop == p; }) == | 
| Matthew Barth | a0dd135 | 2021-03-09 11:10:49 -0600 | [diff] [blame] | 412 | _propsPersisted[intf].end()) | 
|  | 413 | { | 
|  | 414 | _propsPersisted[intf].emplace_back(prop); | 
|  | 415 | } | 
|  | 416 | } | 
|  | 417 |  | 
| Matthew Barth | 279183f | 2021-05-25 10:19:43 -0500 | [diff] [blame] | 418 | bool Zone::isPersisted(const std::string& intf, const std::string& prop) const | 
|  | 419 | { | 
|  | 420 | auto it = _propsPersisted.find(intf); | 
|  | 421 | if (it == _propsPersisted.end()) | 
|  | 422 | { | 
|  | 423 | return false; | 
|  | 424 | } | 
|  | 425 |  | 
|  | 426 | return std::any_of(it->second.begin(), it->second.end(), | 
|  | 427 | [&prop](const auto& p) { return prop == p; }); | 
|  | 428 | } | 
|  | 429 |  | 
| Matthew Barth | ab8e4b8 | 2021-05-27 13:25:46 -0500 | [diff] [blame] | 430 | void Zone::setPowerOnTarget(const json& jsonObj) | 
| Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 431 | { | 
| Matthew Barth | 12e888f | 2021-08-20 11:41:32 -0500 | [diff] [blame] | 432 | if (!jsonObj.contains("poweron_target")) | 
| Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 433 | { | 
| Matthew Barth | ab8e4b8 | 2021-05-27 13:25:46 -0500 | [diff] [blame] | 434 | auto msg = "Missing required zone's poweron target"; | 
|  | 435 | log<level::ERR>(msg, entry("JSON=%s", jsonObj.dump().c_str())); | 
|  | 436 | throw std::runtime_error(msg); | 
| Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 437 | } | 
| Matthew Barth | 12e888f | 2021-08-20 11:41:32 -0500 | [diff] [blame] | 438 | _poweronTarget = jsonObj["poweron_target"].get<uint64_t>(); | 
| Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 439 | } | 
|  | 440 |  | 
| Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 441 | void Zone::setInterfaces(const json& jsonObj) | 
|  | 442 | { | 
|  | 443 | for (const auto& interface : jsonObj["interfaces"]) | 
|  | 444 | { | 
|  | 445 | if (!interface.contains("name") || !interface.contains("properties")) | 
|  | 446 | { | 
|  | 447 | log<level::ERR>("Missing required zone interface attributes", | 
|  | 448 | entry("JSON=%s", interface.dump().c_str())); | 
|  | 449 | throw std::runtime_error( | 
|  | 450 | "Missing required zone interface attributes"); | 
|  | 451 | } | 
| Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 452 | auto propFuncs = | 
|  | 453 | _intfPropHandlers.find(interface["name"].get<std::string>()); | 
|  | 454 | if (propFuncs == _intfPropHandlers.end()) | 
|  | 455 | { | 
|  | 456 | // Construct list of available configurable interfaces | 
|  | 457 | auto intfs = std::accumulate( | 
|  | 458 | std::next(_intfPropHandlers.begin()), _intfPropHandlers.end(), | 
|  | 459 | _intfPropHandlers.begin()->first, [](auto list, auto intf) { | 
|  | 460 | return std::move(list) + ", " + intf.first; | 
|  | 461 | }); | 
|  | 462 | log<level::ERR>("Configured interface not available", | 
|  | 463 | entry("JSON=%s", interface.dump().c_str()), | 
|  | 464 | entry("AVAILABLE_INTFS=%s", intfs.c_str())); | 
|  | 465 | throw std::runtime_error("Configured interface not available"); | 
|  | 466 | } | 
|  | 467 |  | 
| Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 468 | for (const auto& property : interface["properties"]) | 
|  | 469 | { | 
|  | 470 | if (!property.contains("name")) | 
|  | 471 | { | 
|  | 472 | log<level::ERR>( | 
|  | 473 | "Missing required interface property attributes", | 
|  | 474 | entry("JSON=%s", property.dump().c_str())); | 
|  | 475 | throw std::runtime_error( | 
|  | 476 | "Missing required interface property attributes"); | 
|  | 477 | } | 
|  | 478 | // Attribute "persist" is optional, defaults to `false` | 
|  | 479 | auto persist = false; | 
|  | 480 | if (property.contains("persist")) | 
|  | 481 | { | 
|  | 482 | persist = property["persist"].get<bool>(); | 
|  | 483 | } | 
|  | 484 | // Property name from JSON must exactly match supported | 
|  | 485 | // index names to functions in property namespace | 
| Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 486 | auto propFunc = | 
|  | 487 | propFuncs->second.find(property["name"].get<std::string>()); | 
|  | 488 | if (propFunc == propFuncs->second.end()) | 
| Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 489 | { | 
| Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 490 | // Construct list of available configurable properties | 
| Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 491 | auto props = std::accumulate( | 
| Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 492 | std::next(propFuncs->second.begin()), | 
|  | 493 | propFuncs->second.end(), propFuncs->second.begin()->first, | 
|  | 494 | [](auto list, auto prop) { | 
| Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 495 | return std::move(list) + ", " + prop.first; | 
|  | 496 | }); | 
| Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 497 | log<level::ERR>("Configured property not available", | 
| Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 498 | entry("JSON=%s", property.dump().c_str()), | 
|  | 499 | entry("AVAILABLE_PROPS=%s", props.c_str())); | 
|  | 500 | throw std::runtime_error( | 
|  | 501 | "Configured property function not available"); | 
|  | 502 | } | 
| Matthew Barth | bc89a8a | 2021-05-25 15:42:58 -0500 | [diff] [blame] | 503 |  | 
|  | 504 | _propInitFunctions.emplace_back( | 
|  | 505 | propFunc->second(property, persist)); | 
| Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 506 | } | 
| Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 507 | } | 
|  | 508 | } | 
|  | 509 |  | 
| Matt Spinler | 9db6dd1 | 2021-10-29 16:10:08 -0500 | [diff] [blame] | 510 | json Zone::dump() const | 
|  | 511 | { | 
|  | 512 | json output; | 
|  | 513 |  | 
|  | 514 | output["active"] = _isActive; | 
|  | 515 | output["floor"] = _floor; | 
| Matthew Barth | 077448a | 2021-12-02 20:38:15 -0600 | [diff] [blame] | 516 | output["ceiling"] = _ceiling; | 
| Matt Spinler | 9db6dd1 | 2021-10-29 16:10:08 -0500 | [diff] [blame] | 517 | output["target"] = _target; | 
|  | 518 | output["increase_delta"] = _incDelta; | 
|  | 519 | output["decrease_delta"] = _decDelta; | 
|  | 520 | output["power_on_target"] = _poweronTarget; | 
|  | 521 | output["default_ceiling"] = _defaultCeiling; | 
|  | 522 | output["default_floor"] = _defaultFloor; | 
|  | 523 | output["increase_delay"] = _incDelay.count(); | 
|  | 524 | output["decrease_interval"] = _decInterval.count(); | 
|  | 525 | output["requested_target_base"] = _requestTargetBase; | 
|  | 526 | output["floor_change"] = _floorChange; | 
|  | 527 | output["decrease_allowed"] = _decAllowed; | 
|  | 528 | output["persisted_props"] = _propsPersisted; | 
| Matt Spinler | 40554d8 | 2021-10-26 15:23:59 -0500 | [diff] [blame] | 529 | output["target_holds"] = _targetHolds; | 
|  | 530 | output["floor_holds"] = _floorHolds; | 
| Matt Spinler | 9db6dd1 | 2021-10-29 16:10:08 -0500 | [diff] [blame] | 531 |  | 
|  | 532 | return output; | 
|  | 533 | } | 
|  | 534 |  | 
| Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 535 | /** | 
| Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 536 | * Properties of interfaces supported by the zone configuration that return | 
| Matthew Barth | ab8e4b8 | 2021-05-27 13:25:46 -0500 | [diff] [blame] | 537 | * a handler function that sets the zone's property value(s) and persist | 
|  | 538 | * state. | 
| Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 539 | */ | 
|  | 540 | namespace zone::property | 
|  | 541 | { | 
| Matthew Barth | b584d81 | 2021-03-11 15:55:04 -0600 | [diff] [blame] | 542 | // Get a set property handler function for the configured values of the | 
|  | 543 | // "Supported" property | 
| Matthew Barth | bc89a8a | 2021-05-25 15:42:58 -0500 | [diff] [blame] | 544 | std::function<void(DBusZone&, Zone&)> supported(const json& jsonObj, | 
|  | 545 | bool persist) | 
| Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 546 | { | 
|  | 547 | std::vector<std::string> values; | 
| Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 548 | if (!jsonObj.contains("values")) | 
| Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 549 | { | 
| Matthew Barth | ab8e4b8 | 2021-05-27 13:25:46 -0500 | [diff] [blame] | 550 | log<level::ERR>("No 'values' found for \"Supported\" property, " | 
|  | 551 | "using an empty list", | 
|  | 552 | entry("JSON=%s", jsonObj.dump().c_str())); | 
| Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 553 | } | 
|  | 554 | else | 
|  | 555 | { | 
|  | 556 | for (const auto& value : jsonObj["values"]) | 
|  | 557 | { | 
|  | 558 | if (!value.contains("value")) | 
|  | 559 | { | 
|  | 560 | log<level::ERR>("No 'value' found for \"Supported\" property " | 
|  | 561 | "entry, skipping", | 
|  | 562 | entry("JSON=%s", value.dump().c_str())); | 
|  | 563 | } | 
|  | 564 | else | 
|  | 565 | { | 
|  | 566 | values.emplace_back(value["value"].get<std::string>()); | 
|  | 567 | } | 
|  | 568 | } | 
| Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 569 | } | 
|  | 570 |  | 
| Matthew Barth | b584d81 | 2021-03-11 15:55:04 -0600 | [diff] [blame] | 571 | return Zone::setProperty<std::vector<std::string>>( | 
| Matthew Barth | bc89a8a | 2021-05-25 15:42:58 -0500 | [diff] [blame] | 572 | DBusZone::thermalModeIntf, DBusZone::supportedProp, | 
|  | 573 | &DBusZone::supported, std::move(values), persist); | 
| Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 574 | } | 
|  | 575 |  | 
| Matthew Barth | ab8e4b8 | 2021-05-27 13:25:46 -0500 | [diff] [blame] | 576 | // Get a set property handler function for a configured value of the | 
|  | 577 | // "Current" property | 
| Matthew Barth | bc89a8a | 2021-05-25 15:42:58 -0500 | [diff] [blame] | 578 | std::function<void(DBusZone&, Zone&)> current(const json& jsonObj, bool persist) | 
| Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 579 | { | 
| Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 580 | // Use default value for "Current" property if no "value" entry given | 
|  | 581 | if (!jsonObj.contains("value")) | 
|  | 582 | { | 
| Matthew Barth | b584d81 | 2021-03-11 15:55:04 -0600 | [diff] [blame] | 583 | log<level::INFO>("No 'value' found for \"Current\" property, " | 
|  | 584 | "using default", | 
|  | 585 | entry("JSON=%s", jsonObj.dump().c_str())); | 
|  | 586 | // Set persist state of property | 
| Matthew Barth | bc89a8a | 2021-05-25 15:42:58 -0500 | [diff] [blame] | 587 | return Zone::setPropertyPersist(DBusZone::thermalModeIntf, | 
|  | 588 | DBusZone::currentProp, persist); | 
| Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 589 | } | 
|  | 590 |  | 
| Matthew Barth | b584d81 | 2021-03-11 15:55:04 -0600 | [diff] [blame] | 591 | return Zone::setProperty<std::string>( | 
| Matthew Barth | bc89a8a | 2021-05-25 15:42:58 -0500 | [diff] [blame] | 592 | DBusZone::thermalModeIntf, DBusZone::currentProp, &DBusZone::current, | 
| Matthew Barth | b584d81 | 2021-03-11 15:55:04 -0600 | [diff] [blame] | 593 | jsonObj["value"].get<std::string>(), persist); | 
| Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 594 | } | 
| Matthew Barth | b584d81 | 2021-03-11 15:55:04 -0600 | [diff] [blame] | 595 |  | 
| Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 596 | } // namespace zone::property | 
|  | 597 |  | 
| Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 598 | } // namespace phosphor::fan::control::json |