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