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" |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 23 | |
| 24 | namespace phosphor |
| 25 | { |
| 26 | namespace fan |
| 27 | { |
| 28 | namespace control |
| 29 | { |
| 30 | |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 31 | using namespace std::chrono; |
Matthew Barth | 9014980 | 2017-08-15 10:51:37 -0500 | [diff] [blame^] | 32 | using namespace phosphor::fan; |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 33 | using namespace phosphor::logging; |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 34 | using InternalFailure = sdbusplus::xyz::openbmc_project::Common:: |
| 35 | Error::InternalFailure; |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 36 | |
Matthew Barth | 1418413 | 2017-05-19 14:37:30 -0500 | [diff] [blame] | 37 | Zone::Zone(Mode mode, |
| 38 | sdbusplus::bus::bus& bus, |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 39 | phosphor::fan::event::EventPtr& events, |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 40 | const ZoneDefinition& def) : |
| 41 | _bus(bus), |
| 42 | _fullSpeed(std::get<fullSpeedPos>(def)), |
Matthew Barth | 1de6662 | 2017-06-12 13:13:02 -0500 | [diff] [blame] | 43 | _zoneNum(std::get<zoneNumPos>(def)), |
Matthew Barth | e0ca13e | 2017-06-13 16:29:09 -0500 | [diff] [blame] | 44 | _defFloorSpeed(std::get<floorSpeedPos>(def)), |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 45 | _defCeilingSpeed(std::get<fullSpeedPos>(def)), |
Matthew Barth | a956184 | 2017-06-29 11:43:45 -0500 | [diff] [blame] | 46 | _incDelay(std::get<incDelayPos>(def)), |
| 47 | _decInterval(std::get<decIntervalPos>(def)), |
Matthew Barth | 1ee48f2 | 2017-06-27 15:14:48 -0500 | [diff] [blame] | 48 | _incTimer(events, [this](){ this->incTimerExpired(); }), |
Matthew Barth | 9014980 | 2017-08-15 10:51:37 -0500 | [diff] [blame^] | 49 | _decTimer(events, [this](){ this->decTimerExpired(); }), |
| 50 | _sdEvents(events) |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 51 | { |
| 52 | auto& fanDefs = std::get<fanListPos>(def); |
| 53 | |
| 54 | for (auto& def : fanDefs) |
| 55 | { |
| 56 | _fans.emplace_back(std::make_unique<Fan>(bus, def)); |
| 57 | } |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 58 | |
Matthew Barth | 1418413 | 2017-05-19 14:37:30 -0500 | [diff] [blame] | 59 | // Do not enable set speed events when in init mode |
| 60 | if (mode != Mode::init) |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 61 | { |
Matthew Barth | ccc7770 | 2017-07-28 13:43:04 -0500 | [diff] [blame] | 62 | // Setup signal trigger for set speed events |
| 63 | for (auto& event : std::get<setSpeedEventsPos>(def)) |
| 64 | { |
| 65 | initEvent(event); |
| 66 | } |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 67 | // Start timer for fan speed decreases |
Matthew Barth | a956184 | 2017-06-29 11:43:45 -0500 | [diff] [blame] | 68 | if (!_decTimer.running() && _decInterval != seconds::zero()) |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 69 | { |
Matthew Barth | a956184 | 2017-06-29 11:43:45 -0500 | [diff] [blame] | 70 | _decTimer.start(_decInterval, |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 71 | phosphor::fan::util::Timer::TimerType::repeating); |
| 72 | } |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 73 | } |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | |
| 77 | void Zone::setSpeed(uint64_t speed) |
| 78 | { |
| 79 | for (auto& fan : _fans) |
| 80 | { |
| 81 | fan->setSpeed(speed); |
| 82 | } |
| 83 | } |
| 84 | |
Matthew Barth | 861d77c | 2017-05-22 14:18:25 -0500 | [diff] [blame] | 85 | void Zone::setActiveAllow(const Group* group, bool isActiveAllow) |
| 86 | { |
| 87 | _active[group] = isActiveAllow; |
| 88 | if (!isActiveAllow) |
| 89 | { |
| 90 | _isActive = false; |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | // Check all entries are set to allow control active |
| 95 | auto actPred = [](auto const& entry) {return entry.second;}; |
| 96 | _isActive = std::all_of(_active.begin(), |
| 97 | _active.end(), |
| 98 | actPred); |
| 99 | } |
| 100 | } |
| 101 | |
Matthew Barth | b4a7cb9 | 2017-06-28 15:29:50 -0500 | [diff] [blame] | 102 | void Zone::setFloor(uint64_t speed) |
| 103 | { |
| 104 | _floorSpeed = speed; |
| 105 | // Floor speed above target, update target to floor speed |
| 106 | if (_targetSpeed < _floorSpeed) |
| 107 | { |
| 108 | requestSpeedIncrease(_floorSpeed - _targetSpeed); |
| 109 | } |
| 110 | } |
| 111 | |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame] | 112 | void Zone::requestSpeedIncrease(uint64_t targetDelta) |
| 113 | { |
| 114 | // Only increase speed when delta is higher than |
| 115 | // the current increase delta for the zone and currently under ceiling |
| 116 | if (targetDelta > _incSpeedDelta && |
| 117 | _targetSpeed < _ceilingSpeed) |
| 118 | { |
| 119 | _targetSpeed = (targetDelta - _incSpeedDelta) + _targetSpeed; |
| 120 | _incSpeedDelta = targetDelta; |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame] | 121 | // Target speed can not go above a defined ceiling speed |
| 122 | if (_targetSpeed > _ceilingSpeed) |
| 123 | { |
| 124 | _targetSpeed = _ceilingSpeed; |
| 125 | } |
Matthew Barth | 1ee48f2 | 2017-06-27 15:14:48 -0500 | [diff] [blame] | 126 | // Cancel current timer countdown |
| 127 | if (_incTimer.running()) |
| 128 | { |
| 129 | _incTimer.stop(); |
| 130 | } |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame] | 131 | setSpeed(_targetSpeed); |
Matthew Barth | 1ee48f2 | 2017-06-27 15:14:48 -0500 | [diff] [blame] | 132 | // Start timer countdown for fan speed increase |
Matthew Barth | a956184 | 2017-06-29 11:43:45 -0500 | [diff] [blame] | 133 | _incTimer.start(_incDelay, |
Matthew Barth | 1ee48f2 | 2017-06-27 15:14:48 -0500 | [diff] [blame] | 134 | phosphor::fan::util::Timer::TimerType::oneshot); |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame] | 135 | } |
Matthew Barth | 1ee48f2 | 2017-06-27 15:14:48 -0500 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void Zone::incTimerExpired() |
| 139 | { |
| 140 | // Clear increase delta when timer expires allowing additional speed |
| 141 | // increase requests or speed decreases to occur |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame] | 142 | _incSpeedDelta = 0; |
| 143 | } |
| 144 | |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 145 | void Zone::requestSpeedDecrease(uint64_t targetDelta) |
| 146 | { |
| 147 | // Only decrease the lowest target delta requested |
| 148 | if (_decSpeedDelta == 0 || targetDelta < _decSpeedDelta) |
| 149 | { |
| 150 | _decSpeedDelta = targetDelta; |
| 151 | } |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 152 | } |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 153 | |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 154 | void Zone::decTimerExpired() |
| 155 | { |
Matthew Barth | 1ee48f2 | 2017-06-27 15:14:48 -0500 | [diff] [blame] | 156 | // Only decrease speeds when no requested increases exist and |
| 157 | // the increase timer is not running (i.e. not in the middle of increasing) |
| 158 | if (_incSpeedDelta == 0 && !_incTimer.running()) |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 159 | { |
| 160 | // Target speed can not go below the defined floor speed |
| 161 | if ((_targetSpeed < _decSpeedDelta) || |
| 162 | (_targetSpeed - _decSpeedDelta < _floorSpeed)) |
| 163 | { |
| 164 | _targetSpeed = _floorSpeed; |
| 165 | } |
| 166 | else |
| 167 | { |
| 168 | _targetSpeed = _targetSpeed - _decSpeedDelta; |
| 169 | } |
| 170 | setSpeed(_targetSpeed); |
| 171 | } |
| 172 | // Clear decrease delta when timer expires |
| 173 | _decSpeedDelta = 0; |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 174 | // Decrease timer is restarted since its repeating |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 175 | } |
| 176 | |
Matthew Barth | ccc7770 | 2017-07-28 13:43:04 -0500 | [diff] [blame] | 177 | void Zone::initEvent(const SetSpeedEvent& event) |
Matthew Barth | 1bf0ce4 | 2017-06-23 16:16:30 -0500 | [diff] [blame] | 178 | { |
Matthew Barth | ccc7770 | 2017-07-28 13:43:04 -0500 | [diff] [blame] | 179 | // Get the current value for each property |
Matthew Barth | 604329e | 2017-08-04 11:18:28 -0500 | [diff] [blame] | 180 | for (auto& group : std::get<groupPos>(event)) |
Matthew Barth | 1bf0ce4 | 2017-06-23 16:16:30 -0500 | [diff] [blame] | 181 | { |
Matthew Barth | 604329e | 2017-08-04 11:18:28 -0500 | [diff] [blame] | 182 | try |
| 183 | { |
| 184 | refreshProperty(_bus, |
| 185 | group.first, |
| 186 | std::get<intfPos>(group.second), |
| 187 | std::get<propPos>(group.second)); |
| 188 | } |
| 189 | catch (const InternalFailure& ife) |
| 190 | { |
| 191 | log<level::ERR>( |
| 192 | "Unable to find property: ", |
| 193 | entry("PATH=%s", group.first.c_str()), |
| 194 | entry("INTERFACE=%s", std::get<intfPos>(group.second).c_str()), |
| 195 | entry("PROPERTY=%s", std::get<propPos>(group.second).c_str())); |
| 196 | } |
Matthew Barth | 1bf0ce4 | 2017-06-23 16:16:30 -0500 | [diff] [blame] | 197 | } |
Matthew Barth | ccc7770 | 2017-07-28 13:43:04 -0500 | [diff] [blame] | 198 | // Setup signal matches for property change events |
| 199 | for (auto& prop : std::get<propChangeListPos>(event)) |
| 200 | { |
Matthew Barth | f6b76d8 | 2017-08-04 12:58:02 -0500 | [diff] [blame] | 201 | std::unique_ptr<EventData> eventData = |
| 202 | std::make_unique<EventData>( |
| 203 | EventData |
| 204 | { |
| 205 | std::get<groupPos>(event), |
| 206 | std::get<handlerObjPos>(prop), |
| 207 | std::get<actionPos>(event) |
| 208 | } |
| 209 | ); |
| 210 | std::unique_ptr<sdbusplus::server::match::match> match = |
| 211 | std::make_unique<sdbusplus::server::match::match>( |
Matthew Barth | ccc7770 | 2017-07-28 13:43:04 -0500 | [diff] [blame] | 212 | _bus, |
| 213 | std::get<signaturePos>(prop).c_str(), |
| 214 | std::bind(std::mem_fn(&Zone::handleEvent), |
| 215 | this, |
| 216 | std::placeholders::_1, |
Matthew Barth | f6b76d8 | 2017-08-04 12:58:02 -0500 | [diff] [blame] | 217 | eventData.get()) |
| 218 | ); |
| 219 | _signalEvents.emplace_back(std::move(eventData), std::move(match)); |
Matthew Barth | ccc7770 | 2017-07-28 13:43:04 -0500 | [diff] [blame] | 220 | } |
Matthew Barth | 9014980 | 2017-08-15 10:51:37 -0500 | [diff] [blame^] | 221 | // Attach a timer to run the action of an event |
| 222 | auto eventTimer = std::get<timerPos>(event); |
| 223 | if (std::get<intervalPos>(eventTimer) != seconds(0)) |
| 224 | { |
| 225 | std::unique_ptr<util::Timer> timer = |
| 226 | std::make_unique<util::Timer>( |
| 227 | _sdEvents, |
| 228 | [this, |
| 229 | action = &(std::get<actionPos>(event)), |
| 230 | group = &(std::get<groupPos>(event))]() |
| 231 | { |
| 232 | this->timerExpired(*group, *action); |
| 233 | }); |
| 234 | if (!timer->running()) |
| 235 | { |
| 236 | timer->start(std::get<intervalPos>(eventTimer), |
| 237 | util::Timer::TimerType::repeating); |
| 238 | } |
| 239 | _timerEvents.emplace_back(std::move(timer)); |
| 240 | } |
Matthew Barth | ccc7770 | 2017-07-28 13:43:04 -0500 | [diff] [blame] | 241 | // Run action function for initial event state |
| 242 | std::get<actionPos>(event)(*this, |
| 243 | std::get<groupPos>(event)); |
Matthew Barth | 1bf0ce4 | 2017-06-23 16:16:30 -0500 | [diff] [blame] | 244 | } |
| 245 | |
Matthew Barth | f6b76d8 | 2017-08-04 12:58:02 -0500 | [diff] [blame] | 246 | void Zone::removeEvent(const SetSpeedEvent& event) |
| 247 | { |
| 248 | // Find the signal event to be removed |
| 249 | auto it = std::find_if( |
| 250 | _signalEvents.begin(), |
| 251 | _signalEvents.end(), |
| 252 | [&event](auto const& se) |
| 253 | { |
| 254 | auto seEventData = *std::get<signalEventDataPos>(se); |
| 255 | // TODO Use the action function target for comparison |
| 256 | return |
| 257 | ( |
| 258 | std::get<eventGroupPos>(seEventData) == |
| 259 | std::get<groupPos>(event) && |
| 260 | std::get<eventActionPos>(seEventData).target_type().name() == |
| 261 | std::get<actionPos>(event).target_type().name() |
| 262 | ); |
| 263 | }); |
| 264 | if (it != std::end(_signalEvents)) |
| 265 | { |
| 266 | std::get<signalEventDataPos>(*it).reset(); |
| 267 | std::get<signalMatchPos>(*it).reset(); |
| 268 | _signalEvents.erase(it); |
| 269 | } |
| 270 | } |
| 271 | |
Matthew Barth | 1bf0ce4 | 2017-06-23 16:16:30 -0500 | [diff] [blame] | 272 | void Zone::refreshProperty(sdbusplus::bus::bus& bus, |
| 273 | const std::string& path, |
| 274 | const std::string& iface, |
| 275 | const std::string& prop) |
| 276 | { |
| 277 | PropertyVariantType property; |
| 278 | getProperty(_bus, path, iface, prop, property); |
| 279 | setPropertyValue(path.c_str(), iface.c_str(), prop.c_str(), property); |
| 280 | } |
| 281 | |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 282 | void Zone::getProperty(sdbusplus::bus::bus& bus, |
| 283 | const std::string& path, |
| 284 | const std::string& iface, |
| 285 | const std::string& prop, |
Matthew Barth | 9e741ed | 2017-06-02 16:29:09 -0500 | [diff] [blame] | 286 | PropertyVariantType& value) |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 287 | { |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 288 | auto serv = phosphor::fan::util::getService(path, iface, bus); |
| 289 | auto hostCall = bus.new_method_call(serv.c_str(), |
| 290 | path.c_str(), |
| 291 | "org.freedesktop.DBus.Properties", |
| 292 | "Get"); |
| 293 | hostCall.append(iface); |
| 294 | hostCall.append(prop); |
| 295 | auto hostResponseMsg = bus.call(hostCall); |
| 296 | if (hostResponseMsg.is_method_error()) |
| 297 | { |
Dinesh Chinari | 618027a | 2017-06-26 23:26:50 -0500 | [diff] [blame] | 298 | log<level::ERR>("Error in host call response for retrieving property"); |
| 299 | elog<InternalFailure>(); |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 300 | } |
Matthew Barth | 9e741ed | 2017-06-02 16:29:09 -0500 | [diff] [blame] | 301 | hostResponseMsg.read(value); |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 302 | } |
| 303 | |
Matthew Barth | 9014980 | 2017-08-15 10:51:37 -0500 | [diff] [blame^] | 304 | void Zone::timerExpired(Group eventGroup, Action eventAction) |
| 305 | { |
| 306 | // Perform the action |
| 307 | eventAction(*this, eventGroup); |
| 308 | } |
| 309 | |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 310 | void Zone::handleEvent(sdbusplus::message::message& msg, |
Matthew Barth | 34f1bda | 2017-05-31 13:45:36 -0500 | [diff] [blame] | 311 | const EventData* eventData) |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 312 | { |
| 313 | // Handle the callback |
Matthew Barth | 34f1bda | 2017-05-31 13:45:36 -0500 | [diff] [blame] | 314 | std::get<eventHandlerPos>(*eventData)(_bus, msg, *this); |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 315 | // Perform the action |
Matthew Barth | 34f1bda | 2017-05-31 13:45:36 -0500 | [diff] [blame] | 316 | std::get<eventActionPos>(*eventData)(*this, |
| 317 | std::get<eventGroupPos>(*eventData)); |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 318 | } |
| 319 | |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | } |