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