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