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 | */ |
Matthew Barth | a0dd135 | 2021-03-09 11:10:49 -0600 | [diff] [blame] | 16 | #include "config.h" |
| 17 | |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 18 | #include "zone.hpp" |
| 19 | |
Matthew Barth | de90fb4 | 2021-03-04 16:34:28 -0600 | [diff] [blame] | 20 | #include "fan.hpp" |
Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 21 | |
Matthew Barth | a0dd135 | 2021-03-09 11:10:49 -0600 | [diff] [blame] | 22 | #include <cereal/archives/json.hpp> |
| 23 | #include <cereal/cereal.hpp> |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 24 | #include <nlohmann/json.hpp> |
| 25 | #include <phosphor-logging/log.hpp> |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 26 | #include <sdbusplus/bus.hpp> |
Matthew Barth | 603ef16 | 2021-03-24 15:34:53 -0500 | [diff] [blame] | 27 | #include <sdeventplus/event.hpp> |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 28 | |
Matthew Barth | a0dd135 | 2021-03-09 11:10:49 -0600 | [diff] [blame] | 29 | #include <algorithm> |
Matthew Barth | 007de09 | 2021-03-25 13:56:04 -0500 | [diff] [blame^] | 30 | #include <chrono> |
Matthew Barth | a0dd135 | 2021-03-09 11:10:49 -0600 | [diff] [blame] | 31 | #include <filesystem> |
| 32 | #include <fstream> |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 33 | #include <iterator> |
| 34 | #include <map> |
| 35 | #include <numeric> |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 36 | #include <utility> |
Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 37 | #include <vector> |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 38 | |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 39 | namespace phosphor::fan::control::json |
| 40 | { |
| 41 | |
| 42 | using json = nlohmann::json; |
| 43 | using namespace phosphor::logging; |
Matthew Barth | a0dd135 | 2021-03-09 11:10:49 -0600 | [diff] [blame] | 44 | namespace fs = std::filesystem; |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 45 | |
Matthew Barth | b584d81 | 2021-03-11 15:55:04 -0600 | [diff] [blame] | 46 | const std::map<std::string, |
| 47 | std::map<std::string, std::function<std::function<void(Zone*)>( |
| 48 | const json&, bool)>>> |
Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 49 | Zone::_intfPropHandlers = {{thermModeIntf, |
| 50 | {{supportedProp, zone::property::supported}, |
| 51 | {currentProp, zone::property::current}}}}; |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 52 | |
Matthew Barth | 603ef16 | 2021-03-24 15:34:53 -0500 | [diff] [blame] | 53 | Zone::Zone(const json& jsonObj, sdbusplus::bus::bus& bus, |
| 54 | const sdeventplus::Event& event, Manager* mgr) : |
Matthew Barth | a0dd135 | 2021-03-09 11:10:49 -0600 | [diff] [blame] | 55 | ConfigBase(jsonObj), |
| 56 | ThermalObject(bus, (fs::path{CONTROL_OBJPATH} /= getName()).c_str(), true), |
Matthew Barth | 603ef16 | 2021-03-24 15:34:53 -0500 | [diff] [blame] | 57 | _manager(mgr), _incDelay(0), _floor(0), _target(0), _incDelta(0), |
Matthew Barth | 007de09 | 2021-03-25 13:56:04 -0500 | [diff] [blame^] | 58 | _decDelta(0), _requestTargetBase(0), _isActive(true), |
| 59 | _incTimer(event, std::bind(&Zone::incTimerExpired, this)), |
| 60 | _decTimer(event, std::bind(&Zone::decTimerExpired, this)) |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 61 | { |
Matthew Barth | e47c958 | 2021-03-09 14:24:02 -0600 | [diff] [blame] | 62 | // Increase delay is optional, defaults to 0 |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 63 | if (jsonObj.contains("increase_delay")) |
| 64 | { |
Matthew Barth | 007de09 | 2021-03-25 13:56:04 -0500 | [diff] [blame^] | 65 | _incDelay = |
| 66 | std::chrono::seconds(jsonObj["increase_delay"].get<uint64_t>()); |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 67 | } |
Matthew Barth | e47c958 | 2021-03-09 14:24:02 -0600 | [diff] [blame] | 68 | setDefaultCeiling(jsonObj); |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 69 | setDefaultFloor(jsonObj); |
| 70 | setDecInterval(jsonObj); |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 71 | // Setting properties on interfaces to be served are optional |
| 72 | if (jsonObj.contains("interfaces")) |
| 73 | { |
| 74 | setInterfaces(jsonObj); |
| 75 | } |
Matthew Barth | 007de09 | 2021-03-25 13:56:04 -0500 | [diff] [blame^] | 76 | |
| 77 | // Start timer for fan target decreases |
| 78 | _decTimer.restart(_decInterval); |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 79 | } |
| 80 | |
Matthew Barth | de90fb4 | 2021-03-04 16:34:28 -0600 | [diff] [blame] | 81 | void Zone::addFan(std::unique_ptr<Fan> fan) |
| 82 | { |
| 83 | _fans.emplace_back(std::move(fan)); |
| 84 | } |
| 85 | |
Matthew Barth | 8ba715e | 2021-03-05 09:00:05 -0600 | [diff] [blame] | 86 | void Zone::setTarget(uint64_t target) |
| 87 | { |
| 88 | if (_isActive) |
| 89 | { |
| 90 | _target = target; |
| 91 | for (auto& fan : _fans) |
| 92 | { |
| 93 | fan->setTarget(_target); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void Zone::setActiveAllow(const std::string& ident, bool isActiveAllow) |
| 99 | { |
| 100 | _active[ident] = isActiveAllow; |
| 101 | if (!isActiveAllow) |
| 102 | { |
| 103 | _isActive = false; |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | // Check all entries are set to allow active fan control |
| 108 | auto actPred = [](const auto& entry) { return entry.second; }; |
| 109 | _isActive = std::all_of(_active.begin(), _active.end(), actPred); |
| 110 | } |
| 111 | } |
| 112 | |
Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 113 | void Zone::setFloor(uint64_t target) |
| 114 | { |
| 115 | // Check all entries are set to allow floor to be set |
Matthew Barth | 8ba715e | 2021-03-05 09:00:05 -0600 | [diff] [blame] | 116 | auto pred = [](const auto& entry) { return entry.second; }; |
Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 117 | if (std::all_of(_floorChange.begin(), _floorChange.end(), pred)) |
| 118 | { |
| 119 | _floor = target; |
| 120 | // Floor above target, update target to floor |
| 121 | if (_target < _floor) |
| 122 | { |
| 123 | requestIncrease(_floor - _target); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | void Zone::requestIncrease(uint64_t targetDelta) |
| 129 | { |
Matthew Barth | 2b3253e | 2021-03-09 14:51:16 -0600 | [diff] [blame] | 130 | // Only increase when delta is higher than the current increase delta for |
| 131 | // the zone and currently under ceiling |
| 132 | if (targetDelta > _incDelta && _target < _ceiling) |
| 133 | { |
| 134 | auto requestTarget = getRequestTargetBase(); |
| 135 | requestTarget = (targetDelta - _incDelta) + requestTarget; |
| 136 | _incDelta = targetDelta; |
| 137 | // Target can not go above a current ceiling |
| 138 | if (requestTarget > _ceiling) |
| 139 | { |
| 140 | requestTarget = _ceiling; |
| 141 | } |
Matthew Barth | 8ba715e | 2021-03-05 09:00:05 -0600 | [diff] [blame] | 142 | setTarget(requestTarget); |
Matthew Barth | 007de09 | 2021-03-25 13:56:04 -0500 | [diff] [blame^] | 143 | // Restart timer countdown for fan target increase |
| 144 | _incTimer.restartOnce(_incDelay); |
Matthew Barth | 2b3253e | 2021-03-09 14:51:16 -0600 | [diff] [blame] | 145 | } |
Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 146 | } |
| 147 | |
Matthew Barth | 007de09 | 2021-03-25 13:56:04 -0500 | [diff] [blame^] | 148 | void Zone::incTimerExpired() |
| 149 | { |
| 150 | // Clear increase delta when timer expires allowing additional target |
| 151 | // increase requests or target decreases to occur |
| 152 | _incDelta = 0; |
| 153 | } |
| 154 | |
Matthew Barth | 45c44ea | 2021-03-03 13:16:14 -0600 | [diff] [blame] | 155 | void Zone::requestDecrease(uint64_t targetDelta) |
| 156 | { |
| 157 | // Only decrease the lowest target delta requested |
| 158 | if (_decDelta == 0 || targetDelta < _decDelta) |
| 159 | { |
| 160 | _decDelta = targetDelta; |
| 161 | } |
| 162 | } |
| 163 | |
Matthew Barth | 007de09 | 2021-03-25 13:56:04 -0500 | [diff] [blame^] | 164 | void Zone::decTimerExpired() |
| 165 | { |
| 166 | // Check all entries are set to allow a decrease |
| 167 | auto pred = [](auto const& entry) { return entry.second; }; |
| 168 | auto decAllowed = std::all_of(_decAllowed.begin(), _decAllowed.end(), pred); |
| 169 | |
| 170 | // Only decrease targets when allowed, a requested decrease target delta |
| 171 | // exists, where no requested increases exist and the increase timer is not |
| 172 | // running (i.e. not in the middle of increasing) |
| 173 | if (decAllowed && _decDelta != 0 && _incDelta == 0 && |
| 174 | !_incTimer.isEnabled()) |
| 175 | { |
| 176 | auto requestTarget = getRequestTargetBase(); |
| 177 | // Request target should not start above ceiling |
| 178 | if (requestTarget > _ceiling) |
| 179 | { |
| 180 | requestTarget = _ceiling; |
| 181 | } |
| 182 | // Target can not go below the defined floor |
| 183 | if ((requestTarget < _decDelta) || (requestTarget - _decDelta < _floor)) |
| 184 | { |
| 185 | requestTarget = _floor; |
| 186 | } |
| 187 | else |
| 188 | { |
| 189 | requestTarget = requestTarget - _decDelta; |
| 190 | } |
| 191 | setTarget(requestTarget); |
| 192 | } |
| 193 | // Clear decrease delta when timer expires |
| 194 | _decDelta = 0; |
| 195 | // Decrease timer is restarted since its repeating |
| 196 | } |
| 197 | |
Matthew Barth | a0dd135 | 2021-03-09 11:10:49 -0600 | [diff] [blame] | 198 | void Zone::setPersisted(const std::string& intf, const std::string& prop) |
| 199 | { |
| 200 | if (std::find_if(_propsPersisted[intf].begin(), _propsPersisted[intf].end(), |
| 201 | [&prop](const auto& p) { return prop == p; }) != |
| 202 | _propsPersisted[intf].end()) |
| 203 | { |
| 204 | _propsPersisted[intf].emplace_back(prop); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | std::string Zone::current(std::string value) |
| 209 | { |
| 210 | auto current = ThermalObject::current(); |
| 211 | std::transform(value.begin(), value.end(), value.begin(), toupper); |
| 212 | |
| 213 | auto supported = ThermalObject::supported(); |
| 214 | auto isSupported = |
| 215 | std::any_of(supported.begin(), supported.end(), [&value](auto& s) { |
| 216 | std::transform(s.begin(), s.end(), s.begin(), toupper); |
| 217 | return value == s; |
| 218 | }); |
| 219 | |
| 220 | if (value != current && isSupported) |
| 221 | { |
| 222 | current = ThermalObject::current(value); |
| 223 | if (isPersisted("xyz.openbmc_project.Control.ThermalMode", "Current")) |
| 224 | { |
| 225 | saveCurrentMode(); |
| 226 | } |
| 227 | // TODO Trigger event(s) for current mode property change |
| 228 | // auto eData = |
| 229 | // _objects[_path]["xyz.openbmc_project.Control.ThermalMode"] |
| 230 | // ["Current"]; |
| 231 | // if (eData != nullptr) |
| 232 | // { |
| 233 | // sdbusplus::message::message nullMsg{nullptr}; |
| 234 | // handleEvent(nullMsg, eData); |
| 235 | // } |
| 236 | } |
| 237 | |
| 238 | return current; |
| 239 | } |
| 240 | |
Matthew Barth | e47c958 | 2021-03-09 14:24:02 -0600 | [diff] [blame] | 241 | void Zone::setDefaultCeiling(const json& jsonObj) |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 242 | { |
| 243 | if (!jsonObj.contains("full_speed")) |
| 244 | { |
| 245 | log<level::ERR>("Missing required zone's full speed", |
| 246 | entry("JSON=%s", jsonObj.dump().c_str())); |
| 247 | throw std::runtime_error("Missing required zone's full speed"); |
| 248 | } |
Matthew Barth | e47c958 | 2021-03-09 14:24:02 -0600 | [diff] [blame] | 249 | _defaultCeiling = jsonObj["full_speed"].get<uint64_t>(); |
Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 250 | // Start with the current target set as the default |
Matthew Barth | e47c958 | 2021-03-09 14:24:02 -0600 | [diff] [blame] | 251 | _target = _defaultCeiling; |
Matthew Barth | 2b3253e | 2021-03-09 14:51:16 -0600 | [diff] [blame] | 252 | // Start with the current ceiling set as the default |
| 253 | _ceiling = _defaultCeiling; |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | void Zone::setDefaultFloor(const json& jsonObj) |
| 257 | { |
| 258 | if (!jsonObj.contains("default_floor")) |
| 259 | { |
Matthew Barth | e47c958 | 2021-03-09 14:24:02 -0600 | [diff] [blame] | 260 | log<level::ERR>("Missing required zone's default floor", |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 261 | entry("JSON=%s", jsonObj.dump().c_str())); |
Matthew Barth | e47c958 | 2021-03-09 14:24:02 -0600 | [diff] [blame] | 262 | throw std::runtime_error("Missing required zone's default floor"); |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 263 | } |
| 264 | _defaultFloor = jsonObj["default_floor"].get<uint64_t>(); |
Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 265 | // Start with the current floor set as the default |
| 266 | _floor = _defaultFloor; |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | void Zone::setDecInterval(const json& jsonObj) |
| 270 | { |
| 271 | if (!jsonObj.contains("decrease_interval")) |
| 272 | { |
| 273 | log<level::ERR>("Missing required zone's decrease interval", |
| 274 | entry("JSON=%s", jsonObj.dump().c_str())); |
| 275 | throw std::runtime_error("Missing required zone's decrease interval"); |
| 276 | } |
Matthew Barth | 007de09 | 2021-03-25 13:56:04 -0500 | [diff] [blame^] | 277 | _decInterval = |
| 278 | std::chrono::seconds(jsonObj["decrease_interval"].get<uint64_t>()); |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 279 | } |
| 280 | |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 281 | void Zone::setInterfaces(const json& jsonObj) |
| 282 | { |
| 283 | for (const auto& interface : jsonObj["interfaces"]) |
| 284 | { |
| 285 | if (!interface.contains("name") || !interface.contains("properties")) |
| 286 | { |
| 287 | log<level::ERR>("Missing required zone interface attributes", |
| 288 | entry("JSON=%s", interface.dump().c_str())); |
| 289 | throw std::runtime_error( |
| 290 | "Missing required zone interface attributes"); |
| 291 | } |
Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 292 | auto propFuncs = |
| 293 | _intfPropHandlers.find(interface["name"].get<std::string>()); |
| 294 | if (propFuncs == _intfPropHandlers.end()) |
| 295 | { |
| 296 | // Construct list of available configurable interfaces |
| 297 | auto intfs = std::accumulate( |
| 298 | std::next(_intfPropHandlers.begin()), _intfPropHandlers.end(), |
| 299 | _intfPropHandlers.begin()->first, [](auto list, auto intf) { |
| 300 | return std::move(list) + ", " + intf.first; |
| 301 | }); |
| 302 | log<level::ERR>("Configured interface not available", |
| 303 | entry("JSON=%s", interface.dump().c_str()), |
| 304 | entry("AVAILABLE_INTFS=%s", intfs.c_str())); |
| 305 | throw std::runtime_error("Configured interface not available"); |
| 306 | } |
| 307 | |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 308 | for (const auto& property : interface["properties"]) |
| 309 | { |
| 310 | if (!property.contains("name")) |
| 311 | { |
| 312 | log<level::ERR>( |
| 313 | "Missing required interface property attributes", |
| 314 | entry("JSON=%s", property.dump().c_str())); |
| 315 | throw std::runtime_error( |
| 316 | "Missing required interface property attributes"); |
| 317 | } |
| 318 | // Attribute "persist" is optional, defaults to `false` |
| 319 | auto persist = false; |
| 320 | if (property.contains("persist")) |
| 321 | { |
| 322 | persist = property["persist"].get<bool>(); |
| 323 | } |
| 324 | // Property name from JSON must exactly match supported |
| 325 | // index names to functions in property namespace |
Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 326 | auto propFunc = |
| 327 | propFuncs->second.find(property["name"].get<std::string>()); |
| 328 | if (propFunc == propFuncs->second.end()) |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 329 | { |
Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 330 | // Construct list of available configurable properties |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 331 | auto props = std::accumulate( |
Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 332 | std::next(propFuncs->second.begin()), |
| 333 | propFuncs->second.end(), propFuncs->second.begin()->first, |
| 334 | [](auto list, auto prop) { |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 335 | return std::move(list) + ", " + prop.first; |
| 336 | }); |
Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 337 | log<level::ERR>("Configured property not available", |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 338 | entry("JSON=%s", property.dump().c_str()), |
| 339 | entry("AVAILABLE_PROPS=%s", props.c_str())); |
| 340 | throw std::runtime_error( |
| 341 | "Configured property function not available"); |
| 342 | } |
Matthew Barth | b584d81 | 2021-03-11 15:55:04 -0600 | [diff] [blame] | 343 | auto propHandler = propFunc->second(property, persist); |
| 344 | // Only call non-null set property handler functions |
| 345 | if (propHandler) |
| 346 | { |
| 347 | propHandler(this); |
| 348 | } |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 349 | } |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
Matthew Barth | a0dd135 | 2021-03-09 11:10:49 -0600 | [diff] [blame] | 353 | bool Zone::isPersisted(const std::string& intf, const std::string& prop) |
| 354 | { |
| 355 | auto it = _propsPersisted.find(intf); |
| 356 | if (it == _propsPersisted.end()) |
| 357 | { |
| 358 | return false; |
| 359 | } |
| 360 | |
| 361 | return std::any_of(it->second.begin(), it->second.end(), |
| 362 | [&prop](const auto& p) { return prop == p; }); |
| 363 | } |
| 364 | |
| 365 | void Zone::saveCurrentMode() |
| 366 | { |
| 367 | fs::path path{CONTROL_PERSIST_ROOT_PATH}; |
| 368 | // Append zone name and property description |
| 369 | path /= getName(); |
| 370 | path /= "CurrentMode"; |
| 371 | std::ofstream ofs(path.c_str(), std::ios::binary); |
| 372 | cereal::JSONOutputArchive oArch(ofs); |
| 373 | oArch(ThermalObject::current()); |
| 374 | } |
| 375 | |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 376 | /** |
Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 377 | * Properties of interfaces supported by the zone configuration that return |
Matthew Barth | b584d81 | 2021-03-11 15:55:04 -0600 | [diff] [blame] | 378 | * a handler function that sets the zone's property value(s) and persist state. |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 379 | */ |
| 380 | namespace zone::property |
| 381 | { |
Matthew Barth | b584d81 | 2021-03-11 15:55:04 -0600 | [diff] [blame] | 382 | // Get a set property handler function for the configured values of the |
| 383 | // "Supported" property |
| 384 | std::function<void(Zone*)> supported(const json& jsonObj, bool persist) |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 385 | { |
| 386 | std::vector<std::string> values; |
Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 387 | if (!jsonObj.contains("values")) |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 388 | { |
Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 389 | log<level::ERR>( |
| 390 | "No 'values' found for \"Supported\" property, using an empty list", |
| 391 | entry("JSON=%s", jsonObj.dump().c_str())); |
| 392 | } |
| 393 | else |
| 394 | { |
| 395 | for (const auto& value : jsonObj["values"]) |
| 396 | { |
| 397 | if (!value.contains("value")) |
| 398 | { |
| 399 | log<level::ERR>("No 'value' found for \"Supported\" property " |
| 400 | "entry, skipping", |
| 401 | entry("JSON=%s", value.dump().c_str())); |
| 402 | } |
| 403 | else |
| 404 | { |
| 405 | values.emplace_back(value["value"].get<std::string>()); |
| 406 | } |
| 407 | } |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 408 | } |
| 409 | |
Matthew Barth | b584d81 | 2021-03-11 15:55:04 -0600 | [diff] [blame] | 410 | return Zone::setProperty<std::vector<std::string>>( |
| 411 | Zone::thermModeIntf, Zone::supportedProp, &Zone::supported, |
| 412 | std::move(values), persist); |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 413 | } |
| 414 | |
Matthew Barth | b584d81 | 2021-03-11 15:55:04 -0600 | [diff] [blame] | 415 | // Get a set property handler function for a configured value of the "Current" |
Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 416 | // property |
Matthew Barth | b584d81 | 2021-03-11 15:55:04 -0600 | [diff] [blame] | 417 | std::function<void(Zone*)> current(const json& jsonObj, bool persist) |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 418 | { |
Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 419 | // Use default value for "Current" property if no "value" entry given |
| 420 | if (!jsonObj.contains("value")) |
| 421 | { |
Matthew Barth | b584d81 | 2021-03-11 15:55:04 -0600 | [diff] [blame] | 422 | log<level::INFO>("No 'value' found for \"Current\" property, " |
| 423 | "using default", |
| 424 | entry("JSON=%s", jsonObj.dump().c_str())); |
| 425 | // Set persist state of property |
| 426 | return Zone::setPropertyPersist(Zone::thermModeIntf, Zone::currentProp, |
| 427 | persist); |
Matthew Barth | 216229c | 2020-09-24 13:47:33 -0500 | [diff] [blame] | 428 | } |
| 429 | |
Matthew Barth | b584d81 | 2021-03-11 15:55:04 -0600 | [diff] [blame] | 430 | return Zone::setProperty<std::string>( |
| 431 | Zone::thermModeIntf, Zone::currentProp, &Zone::current, |
| 432 | jsonObj["value"].get<std::string>(), persist); |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 433 | } |
Matthew Barth | b584d81 | 2021-03-11 15:55:04 -0600 | [diff] [blame] | 434 | |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame] | 435 | } // namespace zone::property |
| 436 | |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 437 | } // namespace phosphor::fan::control::json |