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