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