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