Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 1 | /** |
| 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 Barth | bc89a8a | 2021-05-25 15:42:58 -0500 | [diff] [blame] | 18 | #include "dbus_zone.hpp" |
Matthew Barth | de90fb4 | 2021-03-04 16:34:28 -0600 | [diff] [blame] | 19 | #include "fan.hpp" |
Matthew Barth | 9403a21 | 2021-05-17 09:31:50 -0500 | [diff] [blame] | 20 | #include "sdbusplus.hpp" |
Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 21 | |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 22 | #include <nlohmann/json.hpp> |
| 23 | #include <phosphor-logging/log.hpp> |
Matthew Barth | 603ef16 | 2021-03-24 15:34:53 -0500 | [diff] [blame] | 24 | #include <sdeventplus/event.hpp> |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 25 | |
Matthew Barth | a0dd135 | 2021-03-09 11:10:49 -0600 | [diff] [blame] | 26 | #include <algorithm> |
Matthew Barth | 007de09 | 2021-03-25 13:56:04 -0500 | [diff] [blame] | 27 | #include <chrono> |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 28 | #include <iterator> |
| 29 | #include <map> |
Matthew Barth | bc89a8a | 2021-05-25 15:42:58 -0500 | [diff] [blame] | 30 | #include <memory> |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 31 | #include <numeric> |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 32 | #include <utility> |
Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 33 | #include <vector> |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 34 | |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 35 | namespace phosphor::fan::control::json |
| 36 | { |
| 37 | |
| 38 | using json = nlohmann::json; |
| 39 | using namespace phosphor::logging; |
| 40 | |
Matthew Barth | bc89a8a | 2021-05-25 15:42:58 -0500 | [diff] [blame] | 41 | const 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 Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 49 | |
Matthew Barth | 9403a21 | 2021-05-17 09:31:50 -0500 | [diff] [blame] | 50 | Zone::Zone(const json& jsonObj, const sdeventplus::Event& event, Manager* mgr) : |
Matthew Barth | ab8e4b8 | 2021-05-27 13:25:46 -0500 | [diff] [blame] | 51 | ConfigBase(jsonObj), _dbusZone{}, _manager(mgr), _defaultFloor(0), |
Matthew Barth | 2504c77 | 2021-05-27 14:02:31 -0500 | [diff] [blame] | 52 | _incDelay(0), _decInterval(0), _floor(0), _target(0), _incDelta(0), |
| 53 | _decDelta(0), _requestTargetBase(0), _isActive(true), |
Matthew Barth | ab8e4b8 | 2021-05-27 13:25:46 -0500 | [diff] [blame] | 54 | _incTimer(event, std::bind(&Zone::incTimerExpired, this)), |
Matthew Barth | 007de09 | 2021-03-25 13:56:04 -0500 | [diff] [blame] | 55 | _decTimer(event, std::bind(&Zone::decTimerExpired, this)) |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 56 | { |
Matthew Barth | e47c958 | 2021-03-09 14:24:02 -0600 | [diff] [blame] | 57 | // Increase delay is optional, defaults to 0 |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 58 | if (jsonObj.contains("increase_delay")) |
| 59 | { |
Matthew Barth | 007de09 | 2021-03-25 13:56:04 -0500 | [diff] [blame] | 60 | _incDelay = |
| 61 | std::chrono::seconds(jsonObj["increase_delay"].get<uint64_t>()); |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 62 | } |
Matthew Barth | ab8e4b8 | 2021-05-27 13:25:46 -0500 | [diff] [blame] | 63 | |
| 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 Barth | 2504c77 | 2021-05-27 14:02:31 -0500 | [diff] [blame] | 84 | // 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 Barth | ab8e4b8 | 2021-05-27 13:25:46 -0500 | [diff] [blame] | 91 | |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 92 | // Setting properties on interfaces to be served are optional |
| 93 | if (jsonObj.contains("interfaces")) |
| 94 | { |
| 95 | setInterfaces(jsonObj); |
| 96 | } |
Matthew Barth | 14303a4 | 2021-05-21 13:04:08 -0500 | [diff] [blame] | 97 | } |
Matthew Barth | 007de09 | 2021-03-25 13:56:04 -0500 | [diff] [blame] | 98 | |
Matthew Barth | 14303a4 | 2021-05-21 13:04:08 -0500 | [diff] [blame] | 99 | void Zone::enable() |
| 100 | { |
Matthew Barth | bc89a8a | 2021-05-25 15:42:58 -0500 | [diff] [blame] | 101 | // Create thermal control dbus object |
| 102 | _dbusZone = std::make_unique<DBusZone>(*this); |
Matthew Barth | a448374 | 2021-03-25 14:09:01 -0500 | [diff] [blame] | 103 | |
Matthew Barth | bc89a8a | 2021-05-25 15:42:58 -0500 | [diff] [blame] | 104 | // 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 Barth | a448374 | 2021-03-25 14:09:01 -0500 | [diff] [blame] | 120 | |
Matthew Barth | 2504c77 | 2021-05-27 14:02:31 -0500 | [diff] [blame] | 121 | // 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 Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 127 | } |
| 128 | |
Matthew Barth | de90fb4 | 2021-03-04 16:34:28 -0600 | [diff] [blame] | 129 | void Zone::addFan(std::unique_ptr<Fan> fan) |
| 130 | { |
| 131 | _fans.emplace_back(std::move(fan)); |
| 132 | } |
| 133 | |
Matthew Barth | 8ba715e | 2021-03-05 09:00:05 -0600 | [diff] [blame] | 134 | void Zone::setTarget(uint64_t target) |
| 135 | { |
Matt Spinler | 5a2b501 | 2021-07-22 14:13:19 -0600 | [diff] [blame^] | 136 | if (_isActive) |
Matthew Barth | 8ba715e | 2021-03-05 09:00:05 -0600 | [diff] [blame] | 137 | { |
| 138 | _target = target; |
| 139 | for (auto& fan : _fans) |
| 140 | { |
| 141 | fan->setTarget(_target); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | void 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 Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 161 | void Zone::setFloor(uint64_t target) |
| 162 | { |
| 163 | // Check all entries are set to allow floor to be set |
Matthew Barth | 8ba715e | 2021-03-05 09:00:05 -0600 | [diff] [blame] | 164 | auto pred = [](const auto& entry) { return entry.second; }; |
Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 165 | 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 | |
| 176 | void Zone::requestIncrease(uint64_t targetDelta) |
| 177 | { |
Matthew Barth | 2b3253e | 2021-03-09 14:51:16 -0600 | [diff] [blame] | 178 | // 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 Barth | 8ba715e | 2021-03-05 09:00:05 -0600 | [diff] [blame] | 190 | setTarget(requestTarget); |
Matthew Barth | 007de09 | 2021-03-25 13:56:04 -0500 | [diff] [blame] | 191 | // Restart timer countdown for fan target increase |
| 192 | _incTimer.restartOnce(_incDelay); |
Matthew Barth | 2b3253e | 2021-03-09 14:51:16 -0600 | [diff] [blame] | 193 | } |
Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 194 | } |
| 195 | |
Matthew Barth | 007de09 | 2021-03-25 13:56:04 -0500 | [diff] [blame] | 196 | void 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 Barth | 45c44ea | 2021-03-03 13:16:14 -0600 | [diff] [blame] | 203 | void 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 Barth | 007de09 | 2021-03-25 13:56:04 -0500 | [diff] [blame] | 212 | void 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 Barth | a0dd135 | 2021-03-09 11:10:49 -0600 | [diff] [blame] | 246 | void Zone::setPersisted(const std::string& intf, const std::string& prop) |
| 247 | { |
| 248 | if (std::find_if(_propsPersisted[intf].begin(), _propsPersisted[intf].end(), |
Matthew Barth | bc89a8a | 2021-05-25 15:42:58 -0500 | [diff] [blame] | 249 | [&prop](const auto& p) { return prop == p; }) == |
Matthew Barth | a0dd135 | 2021-03-09 11:10:49 -0600 | [diff] [blame] | 250 | _propsPersisted[intf].end()) |
| 251 | { |
| 252 | _propsPersisted[intf].emplace_back(prop); |
| 253 | } |
| 254 | } |
| 255 | |
Matthew Barth | 279183f | 2021-05-25 10:19:43 -0500 | [diff] [blame] | 256 | bool 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 Barth | ab8e4b8 | 2021-05-27 13:25:46 -0500 | [diff] [blame] | 268 | void Zone::setPowerOnTarget(const json& jsonObj) |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 269 | { |
Matthew Barth | ab8e4b8 | 2021-05-27 13:25:46 -0500 | [diff] [blame] | 270 | // TODO Remove "full_speed" after configs replaced with "poweron_target" |
| 271 | if (!jsonObj.contains("full_speed") && !jsonObj.contains("poweron_target")) |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 272 | { |
Matthew Barth | ab8e4b8 | 2021-05-27 13:25:46 -0500 | [diff] [blame] | 273 | 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 Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 276 | } |
Matthew Barth | 5606ec8 | 2021-05-17 12:08:52 -0500 | [diff] [blame] | 277 | if (jsonObj.contains("full_speed")) |
| 278 | { |
Matthew Barth | ab8e4b8 | 2021-05-27 13:25:46 -0500 | [diff] [blame] | 279 | _poweronTarget = jsonObj["full_speed"].get<uint64_t>(); |
Matthew Barth | 5606ec8 | 2021-05-17 12:08:52 -0500 | [diff] [blame] | 280 | } |
| 281 | else |
| 282 | { |
Matthew Barth | ab8e4b8 | 2021-05-27 13:25:46 -0500 | [diff] [blame] | 283 | _poweronTarget = jsonObj["poweron_target"].get<uint64_t>(); |
Matthew Barth | 5606ec8 | 2021-05-17 12:08:52 -0500 | [diff] [blame] | 284 | } |
Matthew Barth | ab8e4b8 | 2021-05-27 13:25:46 -0500 | [diff] [blame] | 285 | // Start with the current target set as the poweron target |
| 286 | _target = _poweronTarget; |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 287 | } |
| 288 | |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 289 | void 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 Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 300 | 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 Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 316 | 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 Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 334 | auto propFunc = |
| 335 | propFuncs->second.find(property["name"].get<std::string>()); |
| 336 | if (propFunc == propFuncs->second.end()) |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 337 | { |
Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 338 | // Construct list of available configurable properties |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 339 | auto props = std::accumulate( |
Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 340 | std::next(propFuncs->second.begin()), |
| 341 | propFuncs->second.end(), propFuncs->second.begin()->first, |
| 342 | [](auto list, auto prop) { |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 343 | return std::move(list) + ", " + prop.first; |
| 344 | }); |
Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 345 | log<level::ERR>("Configured property not available", |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 346 | 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 Barth | bc89a8a | 2021-05-25 15:42:58 -0500 | [diff] [blame] | 351 | |
| 352 | _propInitFunctions.emplace_back( |
| 353 | propFunc->second(property, persist)); |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 354 | } |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
| 358 | /** |
Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 359 | * Properties of interfaces supported by the zone configuration that return |
Matthew Barth | ab8e4b8 | 2021-05-27 13:25:46 -0500 | [diff] [blame] | 360 | * a handler function that sets the zone's property value(s) and persist |
| 361 | * state. |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 362 | */ |
| 363 | namespace zone::property |
| 364 | { |
Matthew Barth | b584d81 | 2021-03-11 15:55:04 -0600 | [diff] [blame] | 365 | // Get a set property handler function for the configured values of the |
| 366 | // "Supported" property |
Matthew Barth | bc89a8a | 2021-05-25 15:42:58 -0500 | [diff] [blame] | 367 | std::function<void(DBusZone&, Zone&)> supported(const json& jsonObj, |
| 368 | bool persist) |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 369 | { |
| 370 | std::vector<std::string> values; |
Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 371 | if (!jsonObj.contains("values")) |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 372 | { |
Matthew Barth | ab8e4b8 | 2021-05-27 13:25:46 -0500 | [diff] [blame] | 373 | log<level::ERR>("No 'values' found for \"Supported\" property, " |
| 374 | "using an empty list", |
| 375 | entry("JSON=%s", jsonObj.dump().c_str())); |
Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 376 | } |
| 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 Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 392 | } |
| 393 | |
Matthew Barth | b584d81 | 2021-03-11 15:55:04 -0600 | [diff] [blame] | 394 | return Zone::setProperty<std::vector<std::string>>( |
Matthew Barth | bc89a8a | 2021-05-25 15:42:58 -0500 | [diff] [blame] | 395 | DBusZone::thermalModeIntf, DBusZone::supportedProp, |
| 396 | &DBusZone::supported, std::move(values), persist); |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 397 | } |
| 398 | |
Matthew Barth | ab8e4b8 | 2021-05-27 13:25:46 -0500 | [diff] [blame] | 399 | // Get a set property handler function for a configured value of the |
| 400 | // "Current" property |
Matthew Barth | bc89a8a | 2021-05-25 15:42:58 -0500 | [diff] [blame] | 401 | std::function<void(DBusZone&, Zone&)> current(const json& jsonObj, bool persist) |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 402 | { |
Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 403 | // Use default value for "Current" property if no "value" entry given |
| 404 | if (!jsonObj.contains("value")) |
| 405 | { |
Matthew Barth | b584d81 | 2021-03-11 15:55:04 -0600 | [diff] [blame] | 406 | 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 Barth | bc89a8a | 2021-05-25 15:42:58 -0500 | [diff] [blame] | 410 | return Zone::setPropertyPersist(DBusZone::thermalModeIntf, |
| 411 | DBusZone::currentProp, persist); |
Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 412 | } |
| 413 | |
Matthew Barth | b584d81 | 2021-03-11 15:55:04 -0600 | [diff] [blame] | 414 | return Zone::setProperty<std::string>( |
Matthew Barth | bc89a8a | 2021-05-25 15:42:58 -0500 | [diff] [blame] | 415 | DBusZone::thermalModeIntf, DBusZone::currentProp, &DBusZone::current, |
Matthew Barth | b584d81 | 2021-03-11 15:55:04 -0600 | [diff] [blame] | 416 | jsonObj["value"].get<std::string>(), persist); |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 417 | } |
Matthew Barth | b584d81 | 2021-03-11 15:55:04 -0600 | [diff] [blame] | 418 | |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 419 | } // namespace zone::property |
| 420 | |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 421 | } // namespace phosphor::fan::control::json |