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