Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2017 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 | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 16 | #include <chrono> |
William A. Kennington III | 22c36ab | 2018-10-30 19:50:57 -0700 | [diff] [blame] | 17 | #include <functional> |
Matthew Barth | cc8912e | 2019-01-21 11:35:27 -0600 | [diff] [blame] | 18 | #include <fstream> |
| 19 | #include <cereal/cereal.hpp> |
| 20 | #include <cereal/archives/json.hpp> |
| 21 | #include <experimental/filesystem> |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 22 | #include <phosphor-logging/log.hpp> |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 23 | #include <phosphor-logging/elog.hpp> |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 24 | #include <phosphor-logging/elog-errors.hpp> |
William A. Kennington III | 8fd879f | 2018-10-30 19:49:29 -0700 | [diff] [blame] | 25 | #include <stdexcept> |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 26 | #include <xyz/openbmc_project/Common/error.hpp> |
Matthew Barth | cc8912e | 2019-01-21 11:35:27 -0600 | [diff] [blame] | 27 | #include "config.h" |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 28 | #include "zone.hpp" |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 29 | #include "utility.hpp" |
Matthew Barth | d953bb2 | 2017-08-22 10:45:28 -0500 | [diff] [blame] | 30 | #include "sdbusplus.hpp" |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 31 | |
| 32 | namespace phosphor |
| 33 | { |
| 34 | namespace fan |
| 35 | { |
| 36 | namespace control |
| 37 | { |
| 38 | |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 39 | using namespace std::chrono; |
Matthew Barth | 9014980 | 2017-08-15 10:51:37 -0500 | [diff] [blame] | 40 | using namespace phosphor::fan; |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 41 | using namespace phosphor::logging; |
Matthew Barth | cc8912e | 2019-01-21 11:35:27 -0600 | [diff] [blame] | 42 | namespace fs = std::experimental::filesystem; |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 43 | using InternalFailure = sdbusplus::xyz::openbmc_project::Common:: |
| 44 | Error::InternalFailure; |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 45 | |
Matthew Barth | 1418413 | 2017-05-19 14:37:30 -0500 | [diff] [blame] | 46 | Zone::Zone(Mode mode, |
| 47 | sdbusplus::bus::bus& bus, |
Matthew Barth | 93af419 | 2019-01-18 09:30:57 -0600 | [diff] [blame] | 48 | const std::string& path, |
William A. Kennington III | 1cfc2f1 | 2018-10-19 17:29:46 -0700 | [diff] [blame] | 49 | const sdeventplus::Event& event, |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 50 | const ZoneDefinition& def) : |
Matthew Barth | 93af419 | 2019-01-18 09:30:57 -0600 | [diff] [blame] | 51 | ThermalObject(bus, path.c_str(), true), |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 52 | _bus(bus), |
Matthew Barth | 93af419 | 2019-01-18 09:30:57 -0600 | [diff] [blame] | 53 | _path(path), |
Matthew Barth | 766f854 | 2019-01-29 12:44:13 -0600 | [diff] [blame] | 54 | _ifaces({"xyz.openbmc_project.Control.ThermalMode"}), |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 55 | _fullSpeed(std::get<fullSpeedPos>(def)), |
Matthew Barth | 1de6662 | 2017-06-12 13:13:02 -0500 | [diff] [blame] | 56 | _zoneNum(std::get<zoneNumPos>(def)), |
Matthew Barth | e0ca13e | 2017-06-13 16:29:09 -0500 | [diff] [blame] | 57 | _defFloorSpeed(std::get<floorSpeedPos>(def)), |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 58 | _defCeilingSpeed(std::get<fullSpeedPos>(def)), |
Matthew Barth | a956184 | 2017-06-29 11:43:45 -0500 | [diff] [blame] | 59 | _incDelay(std::get<incDelayPos>(def)), |
| 60 | _decInterval(std::get<decIntervalPos>(def)), |
William A. Kennington III | 22c36ab | 2018-10-30 19:50:57 -0700 | [diff] [blame] | 61 | _incTimer(event, std::bind(&Zone::incTimerExpired, this)), |
| 62 | _decTimer(event, std::bind(&Zone::decTimerExpired, this)), |
William A. Kennington III | 1cfc2f1 | 2018-10-19 17:29:46 -0700 | [diff] [blame] | 63 | _eventLoop(event) |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 64 | { |
| 65 | auto& fanDefs = std::get<fanListPos>(def); |
| 66 | |
| 67 | for (auto& def : fanDefs) |
| 68 | { |
| 69 | _fans.emplace_back(std::make_unique<Fan>(bus, def)); |
| 70 | } |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 71 | |
Matthew Barth | 1418413 | 2017-05-19 14:37:30 -0500 | [diff] [blame] | 72 | // Do not enable set speed events when in init mode |
Matthew Barth | 93af419 | 2019-01-18 09:30:57 -0600 | [diff] [blame] | 73 | if (mode == Mode::control) |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 74 | { |
Matthew Barth | 1b3e960 | 2019-02-13 11:37:03 -0600 | [diff] [blame] | 75 | // Process any zone handlers defined |
| 76 | for (auto& hand : std::get<handlerPos>(def)) |
| 77 | { |
| 78 | hand(*this); |
| 79 | } |
| 80 | |
Matthew Barth | 9e4db25 | 2019-01-21 13:08:02 -0600 | [diff] [blame] | 81 | // Restore thermal control current mode state |
| 82 | restoreCurrentMode(); |
Matthew Barth | 93af419 | 2019-01-18 09:30:57 -0600 | [diff] [blame] | 83 | |
| 84 | // Emit objects added in control mode only |
| 85 | this->emit_object_added(); |
| 86 | |
Matthew Barth | 2b3db61 | 2017-10-25 10:56:51 -0500 | [diff] [blame] | 87 | // Update target speed to current zone target speed |
| 88 | if (!_fans.empty()) |
| 89 | { |
| 90 | _targetSpeed = _fans.front()->getTargetSpeed(); |
| 91 | } |
Matthew Barth | ccc7770 | 2017-07-28 13:43:04 -0500 | [diff] [blame] | 92 | // Setup signal trigger for set speed events |
| 93 | for (auto& event : std::get<setSpeedEventsPos>(def)) |
| 94 | { |
| 95 | initEvent(event); |
| 96 | } |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 97 | // Start timer for fan speed decreases |
William A. Kennington III | 8fd879f | 2018-10-30 19:49:29 -0700 | [diff] [blame] | 98 | _decTimer.restart(_decInterval); |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 99 | } |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 100 | } |
| 101 | |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 102 | void Zone::setSpeed(uint64_t speed) |
| 103 | { |
Matthew Barth | 60b0076 | 2017-08-15 13:39:06 -0500 | [diff] [blame] | 104 | if (_isActive) |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 105 | { |
Matthew Barth | 60b0076 | 2017-08-15 13:39:06 -0500 | [diff] [blame] | 106 | _targetSpeed = speed; |
| 107 | for (auto& fan : _fans) |
| 108 | { |
| 109 | fan->setSpeed(_targetSpeed); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | void Zone::setFullSpeed() |
| 115 | { |
| 116 | if (_fullSpeed != 0) |
| 117 | { |
| 118 | _targetSpeed = _fullSpeed; |
| 119 | for (auto& fan : _fans) |
| 120 | { |
| 121 | fan->setSpeed(_targetSpeed); |
| 122 | } |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
Matthew Barth | 861d77c | 2017-05-22 14:18:25 -0500 | [diff] [blame] | 126 | void Zone::setActiveAllow(const Group* group, bool isActiveAllow) |
| 127 | { |
Matthew Barth | 60b0076 | 2017-08-15 13:39:06 -0500 | [diff] [blame] | 128 | _active[*(group)] = isActiveAllow; |
Matthew Barth | 861d77c | 2017-05-22 14:18:25 -0500 | [diff] [blame] | 129 | if (!isActiveAllow) |
| 130 | { |
| 131 | _isActive = false; |
| 132 | } |
| 133 | else |
| 134 | { |
| 135 | // Check all entries are set to allow control active |
| 136 | auto actPred = [](auto const& entry) {return entry.second;}; |
| 137 | _isActive = std::all_of(_active.begin(), |
| 138 | _active.end(), |
| 139 | actPred); |
| 140 | } |
| 141 | } |
| 142 | |
Matthew Barth | 55dea64 | 2017-11-06 13:34:32 -0600 | [diff] [blame] | 143 | void Zone::removeService(const Group* group, |
| 144 | const std::string& name) |
| 145 | { |
| 146 | try |
| 147 | { |
| 148 | auto& sNames = _services.at(*group); |
| 149 | auto it = std::find_if( |
| 150 | sNames.begin(), |
| 151 | sNames.end(), |
| 152 | [&name](auto const& entry) |
| 153 | { |
| 154 | return name == std::get<namePos>(entry); |
| 155 | } |
| 156 | ); |
| 157 | if (it != std::end(sNames)) |
| 158 | { |
| 159 | // Remove service name from group |
| 160 | sNames.erase(it); |
| 161 | } |
| 162 | } |
| 163 | catch (const std::out_of_range& oore) |
| 164 | { |
| 165 | // No services for group found |
| 166 | } |
| 167 | } |
| 168 | |
Matthew Barth | e59fdf7 | 2017-09-27 09:33:42 -0500 | [diff] [blame] | 169 | void Zone::setServiceOwner(const Group* group, |
| 170 | const std::string& name, |
| 171 | const bool hasOwner) |
| 172 | { |
| 173 | try |
| 174 | { |
| 175 | auto& sNames = _services.at(*group); |
| 176 | auto it = std::find_if( |
| 177 | sNames.begin(), |
| 178 | sNames.end(), |
| 179 | [&name](auto const& entry) |
| 180 | { |
| 181 | return name == std::get<namePos>(entry); |
| 182 | } |
| 183 | ); |
| 184 | if (it != std::end(sNames)) |
| 185 | { |
| 186 | std::get<hasOwnerPos>(*it) = hasOwner; |
| 187 | } |
| 188 | else |
| 189 | { |
| 190 | _services[*group].emplace_back(name, hasOwner); |
| 191 | } |
| 192 | } |
| 193 | catch (const std::out_of_range& oore) |
| 194 | { |
| 195 | _services[*group].emplace_back(name, hasOwner); |
| 196 | } |
| 197 | } |
| 198 | |
Matthew Barth | 480787c | 2017-11-06 11:00:00 -0600 | [diff] [blame] | 199 | void Zone::setServices(const Group* group) |
| 200 | { |
Matthew Barth | 55dea64 | 2017-11-06 13:34:32 -0600 | [diff] [blame] | 201 | // Remove the empty service name if exists |
| 202 | removeService(group, ""); |
Matthew Barth | 480787c | 2017-11-06 11:00:00 -0600 | [diff] [blame] | 203 | for (auto it = group->begin(); it != group->end(); ++it) |
| 204 | { |
| 205 | std::string name; |
| 206 | bool hasOwner = false; |
| 207 | try |
| 208 | { |
Matthew Barth | 146b739 | 2018-03-08 16:17:58 -0600 | [diff] [blame] | 209 | name = getService(std::get<pathPos>(*it), |
| 210 | std::get<intfPos>(*it)); |
Matthew Barth | 480787c | 2017-11-06 11:00:00 -0600 | [diff] [blame] | 211 | hasOwner = util::SDBusPlus::callMethodAndRead<bool>( |
| 212 | _bus, |
| 213 | "org.freedesktop.DBus", |
| 214 | "/org/freedesktop/DBus", |
| 215 | "org.freedesktop.DBus", |
| 216 | "NameHasOwner", |
| 217 | name); |
| 218 | } |
Matt Spinler | ba7b5fe | 2018-04-25 15:26:10 -0500 | [diff] [blame] | 219 | catch (const util::DBusMethodError& e) |
Matthew Barth | 480787c | 2017-11-06 11:00:00 -0600 | [diff] [blame] | 220 | { |
| 221 | // Failed to get service name owner state |
| 222 | hasOwner = false; |
| 223 | } |
| 224 | setServiceOwner(group, name, hasOwner); |
| 225 | } |
| 226 | } |
| 227 | |
Matthew Barth | b4a7cb9 | 2017-06-28 15:29:50 -0500 | [diff] [blame] | 228 | void Zone::setFloor(uint64_t speed) |
| 229 | { |
Matthew Barth | 98726c4 | 2017-10-17 10:35:20 -0500 | [diff] [blame] | 230 | // Check all entries are set to allow floor to be set |
| 231 | auto pred = [](auto const& entry) {return entry.second;}; |
| 232 | auto setFloor = std::all_of(_floorChange.begin(), |
| 233 | _floorChange.end(), |
| 234 | pred); |
| 235 | if (setFloor) |
Matthew Barth | b4a7cb9 | 2017-06-28 15:29:50 -0500 | [diff] [blame] | 236 | { |
Matthew Barth | 98726c4 | 2017-10-17 10:35:20 -0500 | [diff] [blame] | 237 | _floorSpeed = speed; |
| 238 | // Floor speed above target, update target to floor speed |
| 239 | if (_targetSpeed < _floorSpeed) |
| 240 | { |
| 241 | requestSpeedIncrease(_floorSpeed - _targetSpeed); |
| 242 | } |
Matthew Barth | b4a7cb9 | 2017-06-28 15:29:50 -0500 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame] | 246 | void Zone::requestSpeedIncrease(uint64_t targetDelta) |
| 247 | { |
| 248 | // Only increase speed when delta is higher than |
| 249 | // the current increase delta for the zone and currently under ceiling |
| 250 | if (targetDelta > _incSpeedDelta && |
| 251 | _targetSpeed < _ceilingSpeed) |
| 252 | { |
Matthew Barth | 4e72854 | 2017-09-14 16:47:55 -0500 | [diff] [blame] | 253 | auto requestTarget = getRequestSpeedBase(); |
Matthew Barth | 60b0076 | 2017-08-15 13:39:06 -0500 | [diff] [blame] | 254 | requestTarget = (targetDelta - _incSpeedDelta) + requestTarget; |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame] | 255 | _incSpeedDelta = targetDelta; |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame] | 256 | // Target speed can not go above a defined ceiling speed |
Matthew Barth | 60b0076 | 2017-08-15 13:39:06 -0500 | [diff] [blame] | 257 | if (requestTarget > _ceilingSpeed) |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame] | 258 | { |
Matthew Barth | 60b0076 | 2017-08-15 13:39:06 -0500 | [diff] [blame] | 259 | requestTarget = _ceilingSpeed; |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame] | 260 | } |
Matthew Barth | 60b0076 | 2017-08-15 13:39:06 -0500 | [diff] [blame] | 261 | setSpeed(requestTarget); |
William A. Kennington III | 8fd879f | 2018-10-30 19:49:29 -0700 | [diff] [blame] | 262 | // Retart timer countdown for fan speed increase |
| 263 | _incTimer.restartOnce(_incDelay); |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame] | 264 | } |
Matthew Barth | 1ee48f2 | 2017-06-27 15:14:48 -0500 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | void Zone::incTimerExpired() |
| 268 | { |
| 269 | // Clear increase delta when timer expires allowing additional speed |
| 270 | // increase requests or speed decreases to occur |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame] | 271 | _incSpeedDelta = 0; |
| 272 | } |
| 273 | |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 274 | void Zone::requestSpeedDecrease(uint64_t targetDelta) |
| 275 | { |
| 276 | // Only decrease the lowest target delta requested |
| 277 | if (_decSpeedDelta == 0 || targetDelta < _decSpeedDelta) |
| 278 | { |
| 279 | _decSpeedDelta = targetDelta; |
| 280 | } |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 281 | } |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 282 | |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 283 | void Zone::decTimerExpired() |
| 284 | { |
Matthew Barth | e4338cd | 2017-12-14 11:14:30 -0600 | [diff] [blame] | 285 | // Check all entries are set to allow a decrease |
| 286 | auto pred = [](auto const& entry) {return entry.second;}; |
| 287 | auto decAllowed = std::all_of(_decAllowed.begin(), |
| 288 | _decAllowed.end(), |
| 289 | pred); |
| 290 | |
| 291 | // Only decrease speeds when allowed, |
Matthew Barth | d4c001f | 2019-09-19 11:31:20 -0500 | [diff] [blame^] | 292 | // a requested decrease speed delta exists, |
Matthew Barth | e4338cd | 2017-12-14 11:14:30 -0600 | [diff] [blame] | 293 | // where no requested increases exist and |
| 294 | // the increase timer is not running |
| 295 | // (i.e. not in the middle of increasing) |
Matthew Barth | d4c001f | 2019-09-19 11:31:20 -0500 | [diff] [blame^] | 296 | if (decAllowed && _decSpeedDelta != 0 && |
| 297 | _incSpeedDelta == 0 && !_incTimer.isEnabled()) |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 298 | { |
Matthew Barth | 4e72854 | 2017-09-14 16:47:55 -0500 | [diff] [blame] | 299 | auto requestTarget = getRequestSpeedBase(); |
Matthew Barth | c63973a | 2017-12-08 15:31:35 -0600 | [diff] [blame] | 300 | // Request target speed should not start above ceiling |
| 301 | if (requestTarget > _ceilingSpeed) |
| 302 | { |
| 303 | requestTarget = _ceilingSpeed; |
| 304 | } |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 305 | // Target speed can not go below the defined floor speed |
Matthew Barth | 60b0076 | 2017-08-15 13:39:06 -0500 | [diff] [blame] | 306 | if ((requestTarget < _decSpeedDelta) || |
| 307 | (requestTarget - _decSpeedDelta < _floorSpeed)) |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 308 | { |
Matthew Barth | 60b0076 | 2017-08-15 13:39:06 -0500 | [diff] [blame] | 309 | requestTarget = _floorSpeed; |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 310 | } |
| 311 | else |
| 312 | { |
Matthew Barth | 60b0076 | 2017-08-15 13:39:06 -0500 | [diff] [blame] | 313 | requestTarget = requestTarget - _decSpeedDelta; |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 314 | } |
Matthew Barth | 60b0076 | 2017-08-15 13:39:06 -0500 | [diff] [blame] | 315 | setSpeed(requestTarget); |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 316 | } |
| 317 | // Clear decrease delta when timer expires |
| 318 | _decSpeedDelta = 0; |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 319 | // Decrease timer is restarted since its repeating |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 320 | } |
| 321 | |
Matthew Barth | ccc7770 | 2017-07-28 13:43:04 -0500 | [diff] [blame] | 322 | void Zone::initEvent(const SetSpeedEvent& event) |
Matthew Barth | 1bf0ce4 | 2017-06-23 16:16:30 -0500 | [diff] [blame] | 323 | { |
Matthew Barth | 1b4de26 | 2018-03-06 13:03:16 -0600 | [diff] [blame] | 324 | // Enable event triggers |
| 325 | std::for_each( |
| 326 | std::get<triggerPos>(event).begin(), |
| 327 | std::get<triggerPos>(event).end(), |
| 328 | [this, &event](auto const& trigger) |
| 329 | { |
Matthew Barth | 75d9735 | 2018-11-01 10:34:49 -0500 | [diff] [blame] | 330 | if (!std::get<actionsPos>(event).empty()) |
| 331 | { |
| 332 | std::for_each( |
| 333 | std::get<actionsPos>(event).begin(), |
| 334 | std::get<actionsPos>(event).end(), |
| 335 | [this, &trigger, &event](auto const& action) |
| 336 | { |
| 337 | // Default to use group defined with action if exists |
| 338 | if (!std::get<adGroupPos>(action).empty()) |
| 339 | { |
| 340 | trigger(*this, |
| 341 | std::get<sseNamePos>(event), |
| 342 | std::get<adGroupPos>(action), |
| 343 | std::get<adActionsPos>(action)); |
| 344 | } |
| 345 | else |
| 346 | { |
| 347 | trigger(*this, |
| 348 | std::get<sseNamePos>(event), |
| 349 | std::get<groupPos>(event), |
| 350 | std::get<adActionsPos>(action)); |
| 351 | } |
| 352 | } |
| 353 | ); |
| 354 | } |
| 355 | else |
| 356 | { |
| 357 | trigger(*this, |
| 358 | std::get<sseNamePos>(event), |
| 359 | std::get<groupPos>(event), |
| 360 | {}); |
| 361 | } |
Matthew Barth | 1b4de26 | 2018-03-06 13:03:16 -0600 | [diff] [blame] | 362 | } |
| 363 | ); |
Matthew Barth | 1bf0ce4 | 2017-06-23 16:16:30 -0500 | [diff] [blame] | 364 | } |
| 365 | |
Matthew Barth | f6b76d8 | 2017-08-04 12:58:02 -0500 | [diff] [blame] | 366 | void Zone::removeEvent(const SetSpeedEvent& event) |
| 367 | { |
Matthew Barth | 79cb831 | 2018-11-14 15:20:31 -0600 | [diff] [blame] | 368 | // Remove event signals |
| 369 | auto sigIter = _signalEvents.find(std::get<sseNamePos>(event)); |
| 370 | if (sigIter != _signalEvents.end()) |
Matthew Barth | f6b76d8 | 2017-08-04 12:58:02 -0500 | [diff] [blame] | 371 | { |
Matthew Barth | 79cb831 | 2018-11-14 15:20:31 -0600 | [diff] [blame] | 372 | auto& signals = sigIter->second; |
| 373 | for (auto it = signals.begin(); it != signals.end(); ++it) |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 374 | { |
Matthew Barth | 33bfe76 | 2018-11-05 11:13:25 -0600 | [diff] [blame] | 375 | removeSignal(it); |
Matthew Barth | 336f18a | 2017-09-26 09:15:56 -0500 | [diff] [blame] | 376 | } |
Matthew Barth | 79cb831 | 2018-11-14 15:20:31 -0600 | [diff] [blame] | 377 | _signalEvents.erase(sigIter); |
Matthew Barth | f6b76d8 | 2017-08-04 12:58:02 -0500 | [diff] [blame] | 378 | } |
Matthew Barth | 79cb831 | 2018-11-14 15:20:31 -0600 | [diff] [blame] | 379 | |
Matthew Barth | d7b716a | 2018-11-16 13:37:57 -0600 | [diff] [blame] | 380 | // Remove event timers |
| 381 | auto timIter = _timerEvents.find(std::get<sseNamePos>(event)); |
| 382 | if (timIter != _timerEvents.end()) |
Matthew Barth | 33bfe76 | 2018-11-05 11:13:25 -0600 | [diff] [blame] | 383 | { |
Matthew Barth | d7b716a | 2018-11-16 13:37:57 -0600 | [diff] [blame] | 384 | _timerEvents.erase(timIter); |
Matthew Barth | 33bfe76 | 2018-11-05 11:13:25 -0600 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
Matthew Barth | bfb1a56 | 2017-10-05 17:03:40 -0500 | [diff] [blame] | 388 | std::vector<TimerEvent>::iterator Zone::findTimer( |
| 389 | const Group& eventGroup, |
Matthew Barth | d7b716a | 2018-11-16 13:37:57 -0600 | [diff] [blame] | 390 | const std::vector<Action>& eventActions, |
| 391 | std::vector<TimerEvent>& eventTimers) |
Matthew Barth | bfb1a56 | 2017-10-05 17:03:40 -0500 | [diff] [blame] | 392 | { |
Matthew Barth | d7b716a | 2018-11-16 13:37:57 -0600 | [diff] [blame] | 393 | for (auto it = eventTimers.begin(); it != eventTimers.end(); ++it) |
Matthew Barth | bfb1a56 | 2017-10-05 17:03:40 -0500 | [diff] [blame] | 394 | { |
William A. Kennington III | 0420a93 | 2018-10-30 19:53:16 -0700 | [diff] [blame] | 395 | const auto& teEventData = *std::get<timerEventDataPos>(*it); |
Matthew Barth | d7b716a | 2018-11-16 13:37:57 -0600 | [diff] [blame] | 396 | if (std::get<eventGroupPos>(teEventData) == eventGroup && |
| 397 | std::get<eventActionsPos>(teEventData).size() == |
Matthew Barth | bfb1a56 | 2017-10-05 17:03:40 -0500 | [diff] [blame] | 398 | eventActions.size()) |
| 399 | { |
| 400 | // TODO openbmc/openbmc#2328 - Use the action function target |
| 401 | // for comparison |
| 402 | auto actsEqual = [](auto const& a1, |
| 403 | auto const& a2) |
| 404 | { |
| 405 | return a1.target_type().name() == |
| 406 | a2.target_type().name(); |
| 407 | }; |
Matthew Barth | d7b716a | 2018-11-16 13:37:57 -0600 | [diff] [blame] | 408 | if (std::equal(eventActions.begin(), |
Matthew Barth | bfb1a56 | 2017-10-05 17:03:40 -0500 | [diff] [blame] | 409 | eventActions.end(), |
| 410 | std::get<eventActionsPos>(teEventData).begin(), |
| 411 | actsEqual)) |
| 412 | { |
| 413 | return it; |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | |
Matthew Barth | d7b716a | 2018-11-16 13:37:57 -0600 | [diff] [blame] | 418 | return eventTimers.end(); |
Matthew Barth | bfb1a56 | 2017-10-05 17:03:40 -0500 | [diff] [blame] | 419 | } |
| 420 | |
Matthew Barth | d7b716a | 2018-11-16 13:37:57 -0600 | [diff] [blame] | 421 | void Zone::addTimer(const std::string& name, |
| 422 | const Group& group, |
William A. Kennington III | 94fe1a0 | 2018-10-30 19:00:27 -0700 | [diff] [blame] | 423 | const std::vector<Action>& actions, |
| 424 | const TimerConf& tConf) |
| 425 | { |
William A. Kennington III | 8fd879f | 2018-10-30 19:49:29 -0700 | [diff] [blame] | 426 | auto eventData = std::make_unique<EventData>( |
William A. Kennington III | 94fe1a0 | 2018-10-30 19:00:27 -0700 | [diff] [blame] | 427 | group, |
| 428 | "", |
| 429 | nullptr, |
| 430 | actions |
| 431 | ); |
William A. Kennington III | 8fd879f | 2018-10-30 19:49:29 -0700 | [diff] [blame] | 432 | Timer timer( |
William A. Kennington III | 94fe1a0 | 2018-10-30 19:00:27 -0700 | [diff] [blame] | 433 | _eventLoop, |
William A. Kennington III | c0c5f07 | 2018-10-30 19:11:01 -0700 | [diff] [blame] | 434 | std::bind(&Zone::timerExpired, |
| 435 | this, |
William A. Kennington III | 8fd879f | 2018-10-30 19:49:29 -0700 | [diff] [blame] | 436 | std::cref(std::get<Group>(*eventData)), |
| 437 | std::cref(std::get<std::vector<Action>>(*eventData)))); |
| 438 | if (std::get<TimerType>(tConf) == TimerType::repeating) |
William A. Kennington III | 94fe1a0 | 2018-10-30 19:00:27 -0700 | [diff] [blame] | 439 | { |
William A. Kennington III | 8fd879f | 2018-10-30 19:49:29 -0700 | [diff] [blame] | 440 | timer.restart(std::get<intervalPos>(tConf)); |
William A. Kennington III | 94fe1a0 | 2018-10-30 19:00:27 -0700 | [diff] [blame] | 441 | } |
William A. Kennington III | 8fd879f | 2018-10-30 19:49:29 -0700 | [diff] [blame] | 442 | else if (std::get<TimerType>(tConf) == TimerType::oneshot) |
| 443 | { |
| 444 | timer.restartOnce(std::get<intervalPos>(tConf)); |
| 445 | } |
| 446 | else |
| 447 | { |
| 448 | throw std::invalid_argument("Invalid Timer Type"); |
| 449 | } |
Matthew Barth | d7b716a | 2018-11-16 13:37:57 -0600 | [diff] [blame] | 450 | _timerEvents[name].emplace_back(std::move(eventData), std::move(timer)); |
William A. Kennington III | 94fe1a0 | 2018-10-30 19:00:27 -0700 | [diff] [blame] | 451 | } |
| 452 | |
William A. Kennington III | c0c5f07 | 2018-10-30 19:11:01 -0700 | [diff] [blame] | 453 | void Zone::timerExpired(const Group& eventGroup, |
| 454 | const std::vector<Action>& eventActions) |
Matthew Barth | 9014980 | 2017-08-15 10:51:37 -0500 | [diff] [blame] | 455 | { |
Matthew Barth | f9201ab | 2017-09-11 16:07:58 -0500 | [diff] [blame] | 456 | // Perform the actions |
| 457 | std::for_each(eventActions.begin(), |
| 458 | eventActions.end(), |
| 459 | [this, &eventGroup](auto const& action) |
| 460 | { |
| 461 | action(*this, eventGroup); |
| 462 | }); |
Matthew Barth | 9014980 | 2017-08-15 10:51:37 -0500 | [diff] [blame] | 463 | } |
| 464 | |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 465 | void Zone::handleEvent(sdbusplus::message::message& msg, |
Matthew Barth | 34f1bda | 2017-05-31 13:45:36 -0500 | [diff] [blame] | 466 | const EventData* eventData) |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 467 | { |
| 468 | // Handle the callback |
Matthew Barth | 34f1bda | 2017-05-31 13:45:36 -0500 | [diff] [blame] | 469 | std::get<eventHandlerPos>(*eventData)(_bus, msg, *this); |
Matthew Barth | f9201ab | 2017-09-11 16:07:58 -0500 | [diff] [blame] | 470 | // Perform the actions |
| 471 | std::for_each( |
| 472 | std::get<eventActionsPos>(*eventData).begin(), |
| 473 | std::get<eventActionsPos>(*eventData).end(), |
| 474 | [this, &eventData](auto const& action) |
| 475 | { |
| 476 | action(*this, |
| 477 | std::get<eventGroupPos>(*eventData)); |
| 478 | }); |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 479 | } |
| 480 | |
Matthew Barth | a603ed0 | 2018-01-19 16:56:26 -0600 | [diff] [blame] | 481 | const std::string& Zone::getService(const std::string& path, |
| 482 | const std::string& intf) |
| 483 | { |
| 484 | // Retrieve service from cache |
| 485 | auto srvIter = _servTree.find(path); |
| 486 | if (srvIter != _servTree.end()) |
| 487 | { |
| 488 | for (auto& serv : srvIter->second) |
| 489 | { |
| 490 | auto it = std::find_if( |
| 491 | serv.second.begin(), |
| 492 | serv.second.end(), |
| 493 | [&intf](auto const& interface) |
| 494 | { |
| 495 | return intf == interface; |
| 496 | }); |
| 497 | if (it != std::end(serv.second)) |
| 498 | { |
| 499 | // Service found |
| 500 | return serv.first; |
| 501 | } |
| 502 | } |
| 503 | // Interface not found in cache, add and return |
| 504 | return addServices(path, intf, 0); |
| 505 | } |
| 506 | else |
| 507 | { |
| 508 | // Path not found in cache, add and return |
| 509 | return addServices(path, intf, 0); |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | const std::string& Zone::addServices(const std::string& path, |
| 514 | const std::string& intf, |
| 515 | int32_t depth) |
| 516 | { |
| 517 | static const std::string empty = ""; |
| 518 | auto it = _servTree.end(); |
| 519 | |
| 520 | // Get all subtree objects for the given interface |
| 521 | auto objects = util::SDBusPlus::getSubTree(_bus, "/", intf, depth); |
| 522 | // Add what's returned to the cache of path->services |
| 523 | for (auto& pIter : objects) |
| 524 | { |
| 525 | auto pathIter = _servTree.find(pIter.first); |
| 526 | if (pathIter != _servTree.end()) |
| 527 | { |
| 528 | // Path found in cache |
| 529 | for (auto& sIter : pIter.second) |
| 530 | { |
| 531 | auto servIter = pathIter->second.find(sIter.first); |
| 532 | if (servIter != pathIter->second.end()) |
| 533 | { |
| 534 | // Service found in cache |
| 535 | for (auto& iIter : sIter.second) |
| 536 | { |
Matthew Barth | e8b340b | 2018-11-09 10:05:34 -0600 | [diff] [blame] | 537 | if (std::find(servIter->second.begin(), |
| 538 | servIter->second.end(), |
| 539 | iIter) == servIter->second.end()) |
| 540 | { |
| 541 | // Add interface to cache |
| 542 | servIter->second.emplace_back(iIter); |
| 543 | } |
Matthew Barth | a603ed0 | 2018-01-19 16:56:26 -0600 | [diff] [blame] | 544 | } |
| 545 | } |
| 546 | else |
| 547 | { |
| 548 | // Service not found in cache |
| 549 | pathIter->second.insert(sIter); |
| 550 | } |
| 551 | } |
| 552 | } |
| 553 | else |
| 554 | { |
| 555 | _servTree.insert(pIter); |
| 556 | } |
| 557 | // When the paths match, since a single interface constraint is given, |
| 558 | // that is the service to return |
| 559 | if (path == pIter.first) |
| 560 | { |
| 561 | it = _servTree.find(pIter.first); |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | if (it != _servTree.end()) |
| 566 | { |
| 567 | return it->second.begin()->first; |
| 568 | } |
| 569 | |
| 570 | return empty; |
| 571 | } |
| 572 | |
Matthew Barth | 70b2e7d | 2019-02-18 11:03:07 -0600 | [diff] [blame] | 573 | auto Zone::getPersisted(const std::string& intf, |
| 574 | const std::string& prop) |
| 575 | { |
| 576 | auto persisted = false; |
| 577 | |
| 578 | auto it = _persisted.find(intf); |
| 579 | if (it != _persisted.end()) |
| 580 | { |
| 581 | return std::any_of(it->second.begin(), |
| 582 | it->second.end(), |
| 583 | [&prop](auto& p) |
| 584 | { |
| 585 | return prop == p; |
| 586 | }); |
| 587 | } |
| 588 | |
| 589 | return persisted; |
| 590 | } |
| 591 | |
Matthew Barth | 6faf894 | 2019-01-22 09:26:09 -0600 | [diff] [blame] | 592 | std::string Zone::current(std::string value) |
| 593 | { |
Matthew Barth | b390df1 | 2019-02-14 15:18:27 -0600 | [diff] [blame] | 594 | auto current = ThermalObject::current(); |
| 595 | std::transform(value.begin(), value.end(), value.begin(), toupper); |
| 596 | |
Matthew Barth | 221c90c | 2019-02-15 10:35:50 -0600 | [diff] [blame] | 597 | auto supported = ThermalObject::supported(); |
| 598 | auto isSupported = std::any_of( |
| 599 | supported.begin(), |
| 600 | supported.end(), |
| 601 | [&value](auto& s) |
| 602 | { |
| 603 | std::transform(s.begin(), s.end(), s.begin(), toupper); |
| 604 | return value == s; |
| 605 | }); |
| 606 | |
| 607 | if (value != current && isSupported) |
Matthew Barth | 6faf894 | 2019-01-22 09:26:09 -0600 | [diff] [blame] | 608 | { |
| 609 | current = ThermalObject::current(value); |
Matthew Barth | 70b2e7d | 2019-02-18 11:03:07 -0600 | [diff] [blame] | 610 | if (getPersisted("xyz.openbmc_project.Control.ThermalMode", "Current")) |
| 611 | { |
| 612 | saveCurrentMode(); |
| 613 | } |
Matthew Barth | b390df1 | 2019-02-14 15:18:27 -0600 | [diff] [blame] | 614 | // Trigger event(s) for current mode property change |
Matthew Barth | 0a1f686 | 2019-01-30 16:27:38 -0600 | [diff] [blame] | 615 | auto eData = _objects[_path] |
Matthew Barth | baea6c3 | 2019-01-29 14:20:19 -0600 | [diff] [blame] | 616 | ["xyz.openbmc_project.Control.ThermalMode"] |
| 617 | ["Current"]; |
| 618 | if (eData != nullptr) |
| 619 | { |
| 620 | sdbusplus::message::message nullMsg{nullptr}; |
| 621 | handleEvent(nullMsg, eData); |
| 622 | } |
Matthew Barth | 6faf894 | 2019-01-22 09:26:09 -0600 | [diff] [blame] | 623 | } |
Matthew Barth | b390df1 | 2019-02-14 15:18:27 -0600 | [diff] [blame] | 624 | |
Matthew Barth | 6faf894 | 2019-01-22 09:26:09 -0600 | [diff] [blame] | 625 | return current; |
| 626 | } |
| 627 | |
Matthew Barth | cc8912e | 2019-01-21 11:35:27 -0600 | [diff] [blame] | 628 | void Zone::saveCurrentMode() |
| 629 | { |
| 630 | fs::path path{CONTROL_PERSIST_ROOT_PATH}; |
| 631 | // Append zone and property description |
| 632 | path /= std::to_string(_zoneNum); |
| 633 | path /= "CurrentMode"; |
| 634 | std::ofstream ofs(path.c_str(), std::ios::binary); |
| 635 | cereal::JSONOutputArchive oArch(ofs); |
| 636 | oArch(ThermalObject::current()); |
| 637 | } |
| 638 | |
Matthew Barth | 9e4db25 | 2019-01-21 13:08:02 -0600 | [diff] [blame] | 639 | void Zone::restoreCurrentMode() |
| 640 | { |
Matthew Barth | a2bed6e | 2019-02-14 16:07:00 -0600 | [diff] [blame] | 641 | auto current = ThermalObject::current(); |
Matthew Barth | 9e4db25 | 2019-01-21 13:08:02 -0600 | [diff] [blame] | 642 | fs::path path{CONTROL_PERSIST_ROOT_PATH}; |
| 643 | path /= std::to_string(_zoneNum); |
| 644 | path /= "CurrentMode"; |
| 645 | fs::create_directories(path.parent_path()); |
| 646 | |
| 647 | try |
| 648 | { |
| 649 | if (fs::exists(path)) |
| 650 | { |
| 651 | std::ifstream ifs(path.c_str(), std::ios::in | std::ios::binary); |
| 652 | cereal::JSONInputArchive iArch(ifs); |
| 653 | iArch(current); |
| 654 | } |
| 655 | } |
| 656 | catch (std::exception& e) |
| 657 | { |
| 658 | log<level::ERR>(e.what()); |
| 659 | fs::remove(path); |
Matthew Barth | a2bed6e | 2019-02-14 16:07:00 -0600 | [diff] [blame] | 660 | current = ThermalObject::current(); |
Matthew Barth | 9e4db25 | 2019-01-21 13:08:02 -0600 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | this->current(current); |
| 664 | } |
| 665 | |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 666 | } |
| 667 | } |
| 668 | } |