17f88fe61SMatt Spinler /** 27f88fe61SMatt Spinler * Copyright © 2017 IBM Corporation 37f88fe61SMatt Spinler * 47f88fe61SMatt Spinler * Licensed under the Apache License, Version 2.0 (the "License"); 57f88fe61SMatt Spinler * you may not use this file except in compliance with the License. 67f88fe61SMatt Spinler * You may obtain a copy of the License at 77f88fe61SMatt Spinler * 87f88fe61SMatt Spinler * http://www.apache.org/licenses/LICENSE-2.0 97f88fe61SMatt Spinler * 107f88fe61SMatt Spinler * Unless required by applicable law or agreed to in writing, software 117f88fe61SMatt Spinler * distributed under the License is distributed on an "AS IS" BASIS, 127f88fe61SMatt Spinler * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 137f88fe61SMatt Spinler * See the License for the specific language governing permissions and 147f88fe61SMatt Spinler * limitations under the License. 157f88fe61SMatt Spinler */ 168600d9a0SMatthew Barth #include <chrono> 1722c36ab6SWilliam A. Kennington III #include <functional> 18cc8912e9SMatthew Barth #include <fstream> 19cc8912e9SMatthew Barth #include <cereal/cereal.hpp> 20cc8912e9SMatthew Barth #include <cereal/archives/json.hpp> 21cc8912e9SMatthew Barth #include <experimental/filesystem> 22618027abSDinesh Chinari #include <phosphor-logging/log.hpp> 23df3e8d67SMatthew Barth #include <phosphor-logging/elog.hpp> 24618027abSDinesh Chinari #include <phosphor-logging/elog-errors.hpp> 258fd879fbSWilliam A. Kennington III #include <stdexcept> 26618027abSDinesh Chinari #include <xyz/openbmc_project/Common/error.hpp> 27cc8912e9SMatthew Barth #include "config.h" 287f88fe61SMatt Spinler #include "zone.hpp" 29df3e8d67SMatthew Barth #include "utility.hpp" 30d953bb25SMatthew Barth #include "sdbusplus.hpp" 317f88fe61SMatt Spinler 327f88fe61SMatt Spinler namespace phosphor 337f88fe61SMatt Spinler { 347f88fe61SMatt Spinler namespace fan 357f88fe61SMatt Spinler { 367f88fe61SMatt Spinler namespace control 377f88fe61SMatt Spinler { 387f88fe61SMatt Spinler 398600d9a0SMatthew Barth using namespace std::chrono; 409014980aSMatthew Barth using namespace phosphor::fan; 41df3e8d67SMatthew Barth using namespace phosphor::logging; 42cc8912e9SMatthew Barth namespace fs = std::experimental::filesystem; 43618027abSDinesh Chinari using InternalFailure = sdbusplus::xyz::openbmc_project::Common:: 44618027abSDinesh Chinari Error::InternalFailure; 457f88fe61SMatt Spinler 4614184131SMatthew Barth Zone::Zone(Mode mode, 4714184131SMatthew Barth sdbusplus::bus::bus& bus, 4893af4194SMatthew Barth const std::string& path, 491cfc2f11SWilliam A. Kennington III const sdeventplus::Event& event, 507f88fe61SMatt Spinler const ZoneDefinition& def) : 5193af4194SMatthew Barth ThermalObject(bus, path.c_str(), true), 527f88fe61SMatt Spinler _bus(bus), 5393af4194SMatthew Barth _path(path), 54766f8545SMatthew Barth _ifaces({"xyz.openbmc_project.Control.ThermalMode"}), 557f88fe61SMatt Spinler _fullSpeed(std::get<fullSpeedPos>(def)), 561de66629SMatthew Barth _zoneNum(std::get<zoneNumPos>(def)), 57e0ca13ebSMatthew Barth _defFloorSpeed(std::get<floorSpeedPos>(def)), 588600d9a0SMatthew Barth _defCeilingSpeed(std::get<fullSpeedPos>(def)), 59a956184bSMatthew Barth _incDelay(std::get<incDelayPos>(def)), 60a956184bSMatthew Barth _decInterval(std::get<decIntervalPos>(def)), 6122c36ab6SWilliam A. Kennington III _incTimer(event, std::bind(&Zone::incTimerExpired, this)), 6222c36ab6SWilliam A. Kennington III _decTimer(event, std::bind(&Zone::decTimerExpired, this)), 631cfc2f11SWilliam A. Kennington III _eventLoop(event) 647f88fe61SMatt Spinler { 657f88fe61SMatt Spinler auto& fanDefs = std::get<fanListPos>(def); 667f88fe61SMatt Spinler 677f88fe61SMatt Spinler for (auto& def : fanDefs) 687f88fe61SMatt Spinler { 697f88fe61SMatt Spinler _fans.emplace_back(std::make_unique<Fan>(bus, def)); 707f88fe61SMatt Spinler } 7138a93a8aSMatthew Barth 7214184131SMatthew Barth // Do not enable set speed events when in init mode 7393af4194SMatthew Barth if (mode == Mode::control) 7414184131SMatthew Barth { 751b3e9602SMatthew Barth // Process any zone handlers defined 761b3e9602SMatthew Barth for (auto& hand : std::get<handlerPos>(def)) 771b3e9602SMatthew Barth { 781b3e9602SMatthew Barth hand(*this); 791b3e9602SMatthew Barth } 801b3e9602SMatthew Barth 819e4db25cSMatthew Barth // Restore thermal control current mode state 829e4db25cSMatthew Barth restoreCurrentMode(); 8393af4194SMatthew Barth 8493af4194SMatthew Barth // Emit objects added in control mode only 8593af4194SMatthew Barth this->emit_object_added(); 8693af4194SMatthew Barth 872b3db618SMatthew Barth // Update target speed to current zone target speed 882b3db618SMatthew Barth if (!_fans.empty()) 892b3db618SMatthew Barth { 902b3db618SMatthew Barth _targetSpeed = _fans.front()->getTargetSpeed(); 912b3db618SMatthew Barth } 92ccc7770eSMatthew Barth // Setup signal trigger for set speed events 93ccc7770eSMatthew Barth for (auto& event : std::get<setSpeedEventsPos>(def)) 94ccc7770eSMatthew Barth { 95ccc7770eSMatthew Barth initEvent(event); 96ccc7770eSMatthew Barth } 978600d9a0SMatthew Barth // Start timer for fan speed decreases 988fd879fbSWilliam A. Kennington III _decTimer.restart(_decInterval); 997f88fe61SMatt Spinler } 10014184131SMatthew Barth } 1017f88fe61SMatt Spinler 1027f88fe61SMatt Spinler void Zone::setSpeed(uint64_t speed) 1037f88fe61SMatt Spinler { 10460b00766SMatthew Barth if (_isActive) 10560b00766SMatthew Barth { 10660b00766SMatthew Barth _targetSpeed = speed; 1077f88fe61SMatt Spinler for (auto& fan : _fans) 1087f88fe61SMatt Spinler { 10960b00766SMatthew Barth fan->setSpeed(_targetSpeed); 11060b00766SMatthew Barth } 11160b00766SMatthew Barth } 11260b00766SMatthew Barth } 11360b00766SMatthew Barth 11460b00766SMatthew Barth void Zone::setFullSpeed() 11560b00766SMatthew Barth { 11660b00766SMatthew Barth if (_fullSpeed != 0) 11760b00766SMatthew Barth { 11860b00766SMatthew Barth _targetSpeed = _fullSpeed; 11960b00766SMatthew Barth for (auto& fan : _fans) 12060b00766SMatthew Barth { 12160b00766SMatthew Barth fan->setSpeed(_targetSpeed); 12260b00766SMatthew Barth } 1237f88fe61SMatt Spinler } 1247f88fe61SMatt Spinler } 1257f88fe61SMatt Spinler 126861d77c3SMatthew Barth void Zone::setActiveAllow(const Group* group, bool isActiveAllow) 127861d77c3SMatthew Barth { 12860b00766SMatthew Barth _active[*(group)] = isActiveAllow; 129861d77c3SMatthew Barth if (!isActiveAllow) 130861d77c3SMatthew Barth { 131861d77c3SMatthew Barth _isActive = false; 132861d77c3SMatthew Barth } 133861d77c3SMatthew Barth else 134861d77c3SMatthew Barth { 135861d77c3SMatthew Barth // Check all entries are set to allow control active 136861d77c3SMatthew Barth auto actPred = [](auto const& entry) {return entry.second;}; 137861d77c3SMatthew Barth _isActive = std::all_of(_active.begin(), 138861d77c3SMatthew Barth _active.end(), 139861d77c3SMatthew Barth actPred); 140861d77c3SMatthew Barth } 141861d77c3SMatthew Barth } 142861d77c3SMatthew Barth 14355dea643SMatthew Barth void Zone::removeService(const Group* group, 14455dea643SMatthew Barth const std::string& name) 14555dea643SMatthew Barth { 14655dea643SMatthew Barth try 14755dea643SMatthew Barth { 14855dea643SMatthew Barth auto& sNames = _services.at(*group); 14955dea643SMatthew Barth auto it = std::find_if( 15055dea643SMatthew Barth sNames.begin(), 15155dea643SMatthew Barth sNames.end(), 15255dea643SMatthew Barth [&name](auto const& entry) 15355dea643SMatthew Barth { 15455dea643SMatthew Barth return name == std::get<namePos>(entry); 15555dea643SMatthew Barth } 15655dea643SMatthew Barth ); 15755dea643SMatthew Barth if (it != std::end(sNames)) 15855dea643SMatthew Barth { 15955dea643SMatthew Barth // Remove service name from group 16055dea643SMatthew Barth sNames.erase(it); 16155dea643SMatthew Barth } 16255dea643SMatthew Barth } 16355dea643SMatthew Barth catch (const std::out_of_range& oore) 16455dea643SMatthew Barth { 16555dea643SMatthew Barth // No services for group found 16655dea643SMatthew Barth } 16755dea643SMatthew Barth } 16855dea643SMatthew Barth 169e59fdf70SMatthew Barth void Zone::setServiceOwner(const Group* group, 170e59fdf70SMatthew Barth const std::string& name, 171e59fdf70SMatthew Barth const bool hasOwner) 172e59fdf70SMatthew Barth { 173e59fdf70SMatthew Barth try 174e59fdf70SMatthew Barth { 175e59fdf70SMatthew Barth auto& sNames = _services.at(*group); 176e59fdf70SMatthew Barth auto it = std::find_if( 177e59fdf70SMatthew Barth sNames.begin(), 178e59fdf70SMatthew Barth sNames.end(), 179e59fdf70SMatthew Barth [&name](auto const& entry) 180e59fdf70SMatthew Barth { 181e59fdf70SMatthew Barth return name == std::get<namePos>(entry); 182e59fdf70SMatthew Barth } 183e59fdf70SMatthew Barth ); 184e59fdf70SMatthew Barth if (it != std::end(sNames)) 185e59fdf70SMatthew Barth { 186e59fdf70SMatthew Barth std::get<hasOwnerPos>(*it) = hasOwner; 187e59fdf70SMatthew Barth } 188e59fdf70SMatthew Barth else 189e59fdf70SMatthew Barth { 190e59fdf70SMatthew Barth _services[*group].emplace_back(name, hasOwner); 191e59fdf70SMatthew Barth } 192e59fdf70SMatthew Barth } 193e59fdf70SMatthew Barth catch (const std::out_of_range& oore) 194e59fdf70SMatthew Barth { 195e59fdf70SMatthew Barth _services[*group].emplace_back(name, hasOwner); 196e59fdf70SMatthew Barth } 197e59fdf70SMatthew Barth } 198e59fdf70SMatthew Barth 199480787c1SMatthew Barth void Zone::setServices(const Group* group) 200480787c1SMatthew Barth { 20155dea643SMatthew Barth // Remove the empty service name if exists 20255dea643SMatthew Barth removeService(group, ""); 203480787c1SMatthew Barth for (auto it = group->begin(); it != group->end(); ++it) 204480787c1SMatthew Barth { 205480787c1SMatthew Barth std::string name; 206480787c1SMatthew Barth bool hasOwner = false; 207480787c1SMatthew Barth try 208480787c1SMatthew Barth { 209c72b8911SMatthew Barth name = getService(it->first, 210480787c1SMatthew Barth std::get<intfPos>(it->second)); 211480787c1SMatthew Barth hasOwner = util::SDBusPlus::callMethodAndRead<bool>( 212480787c1SMatthew Barth _bus, 213480787c1SMatthew Barth "org.freedesktop.DBus", 214480787c1SMatthew Barth "/org/freedesktop/DBus", 215480787c1SMatthew Barth "org.freedesktop.DBus", 216480787c1SMatthew Barth "NameHasOwner", 217480787c1SMatthew Barth name); 218480787c1SMatthew Barth } 219ba7b5feaSMatt Spinler catch (const util::DBusMethodError& e) 220480787c1SMatthew Barth { 221480787c1SMatthew Barth // Failed to get service name owner state 222480787c1SMatthew Barth hasOwner = false; 223480787c1SMatthew Barth } 224480787c1SMatthew Barth setServiceOwner(group, name, hasOwner); 225480787c1SMatthew Barth } 226480787c1SMatthew Barth } 227480787c1SMatthew Barth 228b4a7cb99SMatthew Barth void Zone::setFloor(uint64_t speed) 229b4a7cb99SMatthew Barth { 23098726c45SMatthew Barth // Check all entries are set to allow floor to be set 23198726c45SMatthew Barth auto pred = [](auto const& entry) {return entry.second;}; 23298726c45SMatthew Barth auto setFloor = std::all_of(_floorChange.begin(), 23398726c45SMatthew Barth _floorChange.end(), 23498726c45SMatthew Barth pred); 23598726c45SMatthew Barth if (setFloor) 23698726c45SMatthew Barth { 237b4a7cb99SMatthew Barth _floorSpeed = speed; 238b4a7cb99SMatthew Barth // Floor speed above target, update target to floor speed 239b4a7cb99SMatthew Barth if (_targetSpeed < _floorSpeed) 240b4a7cb99SMatthew Barth { 241b4a7cb99SMatthew Barth requestSpeedIncrease(_floorSpeed - _targetSpeed); 242b4a7cb99SMatthew Barth } 243b4a7cb99SMatthew Barth } 24498726c45SMatthew Barth } 245b4a7cb99SMatthew Barth 246240397b9SMatthew Barth void Zone::requestSpeedIncrease(uint64_t targetDelta) 247240397b9SMatthew Barth { 248240397b9SMatthew Barth // Only increase speed when delta is higher than 249240397b9SMatthew Barth // the current increase delta for the zone and currently under ceiling 250240397b9SMatthew Barth if (targetDelta > _incSpeedDelta && 251240397b9SMatthew Barth _targetSpeed < _ceilingSpeed) 252240397b9SMatthew Barth { 2534e728542SMatthew Barth auto requestTarget = getRequestSpeedBase(); 25460b00766SMatthew Barth requestTarget = (targetDelta - _incSpeedDelta) + requestTarget; 255240397b9SMatthew Barth _incSpeedDelta = targetDelta; 256240397b9SMatthew Barth // Target speed can not go above a defined ceiling speed 25760b00766SMatthew Barth if (requestTarget > _ceilingSpeed) 258240397b9SMatthew Barth { 25960b00766SMatthew Barth requestTarget = _ceilingSpeed; 260240397b9SMatthew Barth } 26160b00766SMatthew Barth setSpeed(requestTarget); 2628fd879fbSWilliam A. Kennington III // Retart timer countdown for fan speed increase 2638fd879fbSWilliam A. Kennington III _incTimer.restartOnce(_incDelay); 2641ee48f2bSMatthew Barth } 2651ee48f2bSMatthew Barth } 2661ee48f2bSMatthew Barth 2671ee48f2bSMatthew Barth void Zone::incTimerExpired() 2681ee48f2bSMatthew Barth { 2691ee48f2bSMatthew Barth // Clear increase delta when timer expires allowing additional speed 2701ee48f2bSMatthew Barth // increase requests or speed decreases to occur 271240397b9SMatthew Barth _incSpeedDelta = 0; 272240397b9SMatthew Barth } 273240397b9SMatthew Barth 2740ce99d8bSMatthew Barth void Zone::requestSpeedDecrease(uint64_t targetDelta) 2750ce99d8bSMatthew Barth { 2760ce99d8bSMatthew Barth // Only decrease the lowest target delta requested 2770ce99d8bSMatthew Barth if (_decSpeedDelta == 0 || targetDelta < _decSpeedDelta) 2780ce99d8bSMatthew Barth { 2790ce99d8bSMatthew Barth _decSpeedDelta = targetDelta; 2800ce99d8bSMatthew Barth } 2818600d9a0SMatthew Barth } 2820ce99d8bSMatthew Barth 2838600d9a0SMatthew Barth void Zone::decTimerExpired() 2848600d9a0SMatthew Barth { 285e4338cdbSMatthew Barth // Check all entries are set to allow a decrease 286e4338cdbSMatthew Barth auto pred = [](auto const& entry) {return entry.second;}; 287e4338cdbSMatthew Barth auto decAllowed = std::all_of(_decAllowed.begin(), 288e4338cdbSMatthew Barth _decAllowed.end(), 289e4338cdbSMatthew Barth pred); 290e4338cdbSMatthew Barth 291e4338cdbSMatthew Barth // Only decrease speeds when allowed, 292e4338cdbSMatthew Barth // where no requested increases exist and 293e4338cdbSMatthew Barth // the increase timer is not running 294e4338cdbSMatthew Barth // (i.e. not in the middle of increasing) 2958fd879fbSWilliam A. Kennington III if (decAllowed && _incSpeedDelta == 0 && !_incTimer.isEnabled()) 2960ce99d8bSMatthew Barth { 2974e728542SMatthew Barth auto requestTarget = getRequestSpeedBase(); 298c63973a1SMatthew Barth // Request target speed should not start above ceiling 299c63973a1SMatthew Barth if (requestTarget > _ceilingSpeed) 300c63973a1SMatthew Barth { 301c63973a1SMatthew Barth requestTarget = _ceilingSpeed; 302c63973a1SMatthew Barth } 3030ce99d8bSMatthew Barth // Target speed can not go below the defined floor speed 30460b00766SMatthew Barth if ((requestTarget < _decSpeedDelta) || 30560b00766SMatthew Barth (requestTarget - _decSpeedDelta < _floorSpeed)) 3060ce99d8bSMatthew Barth { 30760b00766SMatthew Barth requestTarget = _floorSpeed; 3080ce99d8bSMatthew Barth } 3090ce99d8bSMatthew Barth else 3100ce99d8bSMatthew Barth { 31160b00766SMatthew Barth requestTarget = requestTarget - _decSpeedDelta; 3120ce99d8bSMatthew Barth } 31360b00766SMatthew Barth setSpeed(requestTarget); 3140ce99d8bSMatthew Barth } 3150ce99d8bSMatthew Barth // Clear decrease delta when timer expires 3160ce99d8bSMatthew Barth _decSpeedDelta = 0; 3178600d9a0SMatthew Barth // Decrease timer is restarted since its repeating 3180ce99d8bSMatthew Barth } 3190ce99d8bSMatthew Barth 320ccc7770eSMatthew Barth void Zone::initEvent(const SetSpeedEvent& event) 3211bf0ce4bSMatthew Barth { 3221b4de26aSMatthew Barth // Enable event triggers 3231b4de26aSMatthew Barth std::for_each( 3241b4de26aSMatthew Barth std::get<triggerPos>(event).begin(), 3251b4de26aSMatthew Barth std::get<triggerPos>(event).end(), 3261b4de26aSMatthew Barth [this, &event](auto const& trigger) 3279014980aSMatthew Barth { 3281b4de26aSMatthew Barth trigger(*this, 3291b4de26aSMatthew Barth std::get<groupPos>(event), 3301b4de26aSMatthew Barth std::get<actionsPos>(event)); 3319014980aSMatthew Barth } 3321b4de26aSMatthew Barth ); 3331b4de26aSMatthew Barth 334f9201abbSMatthew Barth // Run action functions for initial event state 335f9201abbSMatthew Barth std::for_each( 336f9201abbSMatthew Barth std::get<actionsPos>(event).begin(), 337f9201abbSMatthew Barth std::get<actionsPos>(event).end(), 338f9201abbSMatthew Barth [this, &event](auto const& action) 339f9201abbSMatthew Barth { 340f9201abbSMatthew Barth action(*this, 3411bf0ce4bSMatthew Barth std::get<groupPos>(event)); 342f9201abbSMatthew Barth }); 3431bf0ce4bSMatthew Barth } 3441bf0ce4bSMatthew Barth 345f6b76d8eSMatthew Barth void Zone::removeEvent(const SetSpeedEvent& event) 346f6b76d8eSMatthew Barth { 347*016bd24cSMatthew Barth // Remove triggers of the event 348*016bd24cSMatthew Barth for (auto& trig : std::get<triggerPos>(event)) 349f6b76d8eSMatthew Barth { 350*016bd24cSMatthew Barth auto it = findSignal(trig, 35133bfe761SMatthew Barth std::get<groupPos>(event), 35233bfe761SMatthew Barth std::get<actionsPos>(event)); 35333bfe761SMatthew Barth if (it != std::end(getSignalEvents())) 354f9201abbSMatthew Barth { 35533bfe761SMatthew Barth removeSignal(it); 356f9201abbSMatthew Barth } 35733bfe761SMatthew Barth } 35833bfe761SMatthew Barth // Remove timers of the event 35933bfe761SMatthew Barth auto it = findTimer(std::get<groupPos>(event), 36033bfe761SMatthew Barth std::get<actionsPos>(event)); 36133bfe761SMatthew Barth if (it != std::end(getTimerEvents())) 36233bfe761SMatthew Barth { 36333bfe761SMatthew Barth removeTimer(it); 36433bfe761SMatthew Barth } 36533bfe761SMatthew Barth } 36633bfe761SMatthew Barth 36733bfe761SMatthew Barth std::vector<SignalEvent>::iterator Zone::findSignal( 368*016bd24cSMatthew Barth const Trigger& signal, 36933bfe761SMatthew Barth const Group& eGroup, 37033bfe761SMatthew Barth const std::vector<Action>& eActions) 37133bfe761SMatthew Barth { 37233bfe761SMatthew Barth // Find the signal in the event to be removed 37333bfe761SMatthew Barth for (auto it = _signalEvents.begin(); it != _signalEvents.end(); ++ it) 37433bfe761SMatthew Barth { 37533bfe761SMatthew Barth const auto& seEventData = *std::get<signalEventDataPos>(*it); 37633bfe761SMatthew Barth if (eGroup == std::get<eventGroupPos>(seEventData) && 37733bfe761SMatthew Barth eActions.size() == std::get<eventActionsPos>(seEventData).size()) 37833bfe761SMatthew Barth { 37933bfe761SMatthew Barth // TODO openbmc/openbmc#2328 - Use the function target 380f9201abbSMatthew Barth // for comparison 381f9201abbSMatthew Barth auto actsEqual = [](auto const& a1, 382f9201abbSMatthew Barth auto const& a2) 383f9201abbSMatthew Barth { 384f9201abbSMatthew Barth return a1.target_type().name() == 385f9201abbSMatthew Barth a2.target_type().name(); 386f9201abbSMatthew Barth }; 38733bfe761SMatthew Barth if (std::equal(eActions.begin(), 38833bfe761SMatthew Barth eActions.end(), 389f9201abbSMatthew Barth std::get<eventActionsPos>(seEventData).begin(), 39033bfe761SMatthew Barth actsEqual)) 391f6b76d8eSMatthew Barth { 39233bfe761SMatthew Barth return it; 393336f18a5SMatthew Barth } 394f6b76d8eSMatthew Barth } 395f6b76d8eSMatthew Barth } 396f6b76d8eSMatthew Barth 39733bfe761SMatthew Barth return _signalEvents.end(); 39833bfe761SMatthew Barth } 39933bfe761SMatthew Barth 400bfb1a566SMatthew Barth std::vector<TimerEvent>::iterator Zone::findTimer( 401bfb1a566SMatthew Barth const Group& eventGroup, 402bfb1a566SMatthew Barth const std::vector<Action>& eventActions) 403bfb1a566SMatthew Barth { 404bfb1a566SMatthew Barth for (auto it = _timerEvents.begin(); it != _timerEvents.end(); ++it) 405bfb1a566SMatthew Barth { 4060420a932SWilliam A. Kennington III const auto& teEventData = *std::get<timerEventDataPos>(*it); 407bfb1a566SMatthew Barth if (std::get<eventActionsPos>(teEventData).size() == 408bfb1a566SMatthew Barth eventActions.size()) 409bfb1a566SMatthew Barth { 410bfb1a566SMatthew Barth // TODO openbmc/openbmc#2328 - Use the action function target 411bfb1a566SMatthew Barth // for comparison 412bfb1a566SMatthew Barth auto actsEqual = [](auto const& a1, 413bfb1a566SMatthew Barth auto const& a2) 414bfb1a566SMatthew Barth { 415bfb1a566SMatthew Barth return a1.target_type().name() == 416bfb1a566SMatthew Barth a2.target_type().name(); 417bfb1a566SMatthew Barth }; 418bfb1a566SMatthew Barth if (std::get<eventGroupPos>(teEventData) == eventGroup && 419bfb1a566SMatthew Barth std::equal(eventActions.begin(), 420bfb1a566SMatthew Barth eventActions.end(), 421bfb1a566SMatthew Barth std::get<eventActionsPos>(teEventData).begin(), 422bfb1a566SMatthew Barth actsEqual)) 423bfb1a566SMatthew Barth { 424bfb1a566SMatthew Barth return it; 425bfb1a566SMatthew Barth } 426bfb1a566SMatthew Barth } 427bfb1a566SMatthew Barth } 428bfb1a566SMatthew Barth 429bfb1a566SMatthew Barth return _timerEvents.end(); 430bfb1a566SMatthew Barth } 431bfb1a566SMatthew Barth 43294fe1a0cSWilliam A. Kennington III void Zone::addTimer(const Group& group, 43394fe1a0cSWilliam A. Kennington III const std::vector<Action>& actions, 43494fe1a0cSWilliam A. Kennington III const TimerConf& tConf) 43594fe1a0cSWilliam A. Kennington III { 4368fd879fbSWilliam A. Kennington III auto eventData = std::make_unique<EventData>( 43794fe1a0cSWilliam A. Kennington III group, 43894fe1a0cSWilliam A. Kennington III "", 43994fe1a0cSWilliam A. Kennington III nullptr, 44094fe1a0cSWilliam A. Kennington III actions 44194fe1a0cSWilliam A. Kennington III ); 4428fd879fbSWilliam A. Kennington III Timer timer( 44394fe1a0cSWilliam A. Kennington III _eventLoop, 444c0c5f07fSWilliam A. Kennington III std::bind(&Zone::timerExpired, 445c0c5f07fSWilliam A. Kennington III this, 4468fd879fbSWilliam A. Kennington III std::cref(std::get<Group>(*eventData)), 4478fd879fbSWilliam A. Kennington III std::cref(std::get<std::vector<Action>>(*eventData)))); 4488fd879fbSWilliam A. Kennington III if (std::get<TimerType>(tConf) == TimerType::repeating) 44994fe1a0cSWilliam A. Kennington III { 4508fd879fbSWilliam A. Kennington III timer.restart(std::get<intervalPos>(tConf)); 45194fe1a0cSWilliam A. Kennington III } 4528fd879fbSWilliam A. Kennington III else if (std::get<TimerType>(tConf) == TimerType::oneshot) 4538fd879fbSWilliam A. Kennington III { 4548fd879fbSWilliam A. Kennington III timer.restartOnce(std::get<intervalPos>(tConf)); 4558fd879fbSWilliam A. Kennington III } 4568fd879fbSWilliam A. Kennington III else 4578fd879fbSWilliam A. Kennington III { 4588fd879fbSWilliam A. Kennington III throw std::invalid_argument("Invalid Timer Type"); 4598fd879fbSWilliam A. Kennington III } 4608fd879fbSWilliam A. Kennington III _timerEvents.emplace_back(std::move(eventData), std::move(timer)); 46194fe1a0cSWilliam A. Kennington III } 46294fe1a0cSWilliam A. Kennington III 463c0c5f07fSWilliam A. Kennington III void Zone::timerExpired(const Group& eventGroup, 464c0c5f07fSWilliam A. Kennington III const std::vector<Action>& eventActions) 4659014980aSMatthew Barth { 466f9201abbSMatthew Barth // Perform the actions 467f9201abbSMatthew Barth std::for_each(eventActions.begin(), 468f9201abbSMatthew Barth eventActions.end(), 469f9201abbSMatthew Barth [this, &eventGroup](auto const& action) 470f9201abbSMatthew Barth { 471f9201abbSMatthew Barth action(*this, eventGroup); 472f9201abbSMatthew Barth }); 4739014980aSMatthew Barth } 4749014980aSMatthew Barth 47538a93a8aSMatthew Barth void Zone::handleEvent(sdbusplus::message::message& msg, 47634f1bda2SMatthew Barth const EventData* eventData) 47738a93a8aSMatthew Barth { 47838a93a8aSMatthew Barth // Handle the callback 47934f1bda2SMatthew Barth std::get<eventHandlerPos>(*eventData)(_bus, msg, *this); 480f9201abbSMatthew Barth // Perform the actions 481f9201abbSMatthew Barth std::for_each( 482f9201abbSMatthew Barth std::get<eventActionsPos>(*eventData).begin(), 483f9201abbSMatthew Barth std::get<eventActionsPos>(*eventData).end(), 484f9201abbSMatthew Barth [this, &eventData](auto const& action) 485f9201abbSMatthew Barth { 486f9201abbSMatthew Barth action(*this, 48734f1bda2SMatthew Barth std::get<eventGroupPos>(*eventData)); 488f9201abbSMatthew Barth }); 48938a93a8aSMatthew Barth } 49038a93a8aSMatthew Barth 491a603ed01SMatthew Barth const std::string& Zone::getService(const std::string& path, 492a603ed01SMatthew Barth const std::string& intf) 493a603ed01SMatthew Barth { 494a603ed01SMatthew Barth // Retrieve service from cache 495a603ed01SMatthew Barth auto srvIter = _servTree.find(path); 496a603ed01SMatthew Barth if (srvIter != _servTree.end()) 497a603ed01SMatthew Barth { 498a603ed01SMatthew Barth for (auto& serv : srvIter->second) 499a603ed01SMatthew Barth { 500a603ed01SMatthew Barth auto it = std::find_if( 501a603ed01SMatthew Barth serv.second.begin(), 502a603ed01SMatthew Barth serv.second.end(), 503a603ed01SMatthew Barth [&intf](auto const& interface) 504a603ed01SMatthew Barth { 505a603ed01SMatthew Barth return intf == interface; 506a603ed01SMatthew Barth }); 507a603ed01SMatthew Barth if (it != std::end(serv.second)) 508a603ed01SMatthew Barth { 509a603ed01SMatthew Barth // Service found 510a603ed01SMatthew Barth return serv.first; 511a603ed01SMatthew Barth } 512a603ed01SMatthew Barth } 513a603ed01SMatthew Barth // Interface not found in cache, add and return 514a603ed01SMatthew Barth return addServices(path, intf, 0); 515a603ed01SMatthew Barth } 516a603ed01SMatthew Barth else 517a603ed01SMatthew Barth { 518a603ed01SMatthew Barth // Path not found in cache, add and return 519a603ed01SMatthew Barth return addServices(path, intf, 0); 520a603ed01SMatthew Barth } 521a603ed01SMatthew Barth } 522a603ed01SMatthew Barth 523a603ed01SMatthew Barth const std::string& Zone::addServices(const std::string& path, 524a603ed01SMatthew Barth const std::string& intf, 525a603ed01SMatthew Barth int32_t depth) 526a603ed01SMatthew Barth { 527a603ed01SMatthew Barth static const std::string empty = ""; 528a603ed01SMatthew Barth auto it = _servTree.end(); 529a603ed01SMatthew Barth 530a603ed01SMatthew Barth // Get all subtree objects for the given interface 531a603ed01SMatthew Barth auto objects = util::SDBusPlus::getSubTree(_bus, "/", intf, depth); 532a603ed01SMatthew Barth // Add what's returned to the cache of path->services 533a603ed01SMatthew Barth for (auto& pIter : objects) 534a603ed01SMatthew Barth { 535a603ed01SMatthew Barth auto pathIter = _servTree.find(pIter.first); 536a603ed01SMatthew Barth if (pathIter != _servTree.end()) 537a603ed01SMatthew Barth { 538a603ed01SMatthew Barth // Path found in cache 539a603ed01SMatthew Barth for (auto& sIter : pIter.second) 540a603ed01SMatthew Barth { 541a603ed01SMatthew Barth auto servIter = pathIter->second.find(sIter.first); 542a603ed01SMatthew Barth if (servIter != pathIter->second.end()) 543a603ed01SMatthew Barth { 544a603ed01SMatthew Barth // Service found in cache 545a603ed01SMatthew Barth for (auto& iIter : sIter.second) 546a603ed01SMatthew Barth { 547e8b340bdSMatthew Barth if (std::find(servIter->second.begin(), 548e8b340bdSMatthew Barth servIter->second.end(), 549e8b340bdSMatthew Barth iIter) == servIter->second.end()) 550e8b340bdSMatthew Barth { 551a603ed01SMatthew Barth // Add interface to cache 552a603ed01SMatthew Barth servIter->second.emplace_back(iIter); 553a603ed01SMatthew Barth } 554a603ed01SMatthew Barth } 555e8b340bdSMatthew Barth } 556a603ed01SMatthew Barth else 557a603ed01SMatthew Barth { 558a603ed01SMatthew Barth // Service not found in cache 559a603ed01SMatthew Barth pathIter->second.insert(sIter); 560a603ed01SMatthew Barth } 561a603ed01SMatthew Barth } 562a603ed01SMatthew Barth } 563a603ed01SMatthew Barth else 564a603ed01SMatthew Barth { 565a603ed01SMatthew Barth _servTree.insert(pIter); 566a603ed01SMatthew Barth } 567a603ed01SMatthew Barth // When the paths match, since a single interface constraint is given, 568a603ed01SMatthew Barth // that is the service to return 569a603ed01SMatthew Barth if (path == pIter.first) 570a603ed01SMatthew Barth { 571a603ed01SMatthew Barth it = _servTree.find(pIter.first); 572a603ed01SMatthew Barth } 573a603ed01SMatthew Barth } 574a603ed01SMatthew Barth 575a603ed01SMatthew Barth if (it != _servTree.end()) 576a603ed01SMatthew Barth { 577a603ed01SMatthew Barth return it->second.begin()->first; 578a603ed01SMatthew Barth } 579a603ed01SMatthew Barth 580a603ed01SMatthew Barth return empty; 581a603ed01SMatthew Barth } 582a603ed01SMatthew Barth 58370b2e7daSMatthew Barth auto Zone::getPersisted(const std::string& intf, 58470b2e7daSMatthew Barth const std::string& prop) 58570b2e7daSMatthew Barth { 58670b2e7daSMatthew Barth auto persisted = false; 58770b2e7daSMatthew Barth 58870b2e7daSMatthew Barth auto it = _persisted.find(intf); 58970b2e7daSMatthew Barth if (it != _persisted.end()) 59070b2e7daSMatthew Barth { 59170b2e7daSMatthew Barth return std::any_of(it->second.begin(), 59270b2e7daSMatthew Barth it->second.end(), 59370b2e7daSMatthew Barth [&prop](auto& p) 59470b2e7daSMatthew Barth { 59570b2e7daSMatthew Barth return prop == p; 59670b2e7daSMatthew Barth }); 59770b2e7daSMatthew Barth } 59870b2e7daSMatthew Barth 59970b2e7daSMatthew Barth return persisted; 60070b2e7daSMatthew Barth } 60170b2e7daSMatthew Barth 6026faf8943SMatthew Barth std::string Zone::current(std::string value) 6036faf8943SMatthew Barth { 604b390df1eSMatthew Barth auto current = ThermalObject::current(); 605b390df1eSMatthew Barth std::transform(value.begin(), value.end(), value.begin(), toupper); 606b390df1eSMatthew Barth 607221c90c3SMatthew Barth auto supported = ThermalObject::supported(); 608221c90c3SMatthew Barth auto isSupported = std::any_of( 609221c90c3SMatthew Barth supported.begin(), 610221c90c3SMatthew Barth supported.end(), 611221c90c3SMatthew Barth [&value](auto& s) 612221c90c3SMatthew Barth { 613221c90c3SMatthew Barth std::transform(s.begin(), s.end(), s.begin(), toupper); 614221c90c3SMatthew Barth return value == s; 615221c90c3SMatthew Barth }); 616221c90c3SMatthew Barth 617221c90c3SMatthew Barth if (value != current && isSupported) 6186faf8943SMatthew Barth { 6196faf8943SMatthew Barth current = ThermalObject::current(value); 62070b2e7daSMatthew Barth if (getPersisted("xyz.openbmc_project.Control.ThermalMode", "Current")) 62170b2e7daSMatthew Barth { 6226faf8943SMatthew Barth saveCurrentMode(); 62370b2e7daSMatthew Barth } 624b390df1eSMatthew Barth // Trigger event(s) for current mode property change 6250a1f686cSMatthew Barth auto eData = _objects[_path] 626baea6c3fSMatthew Barth ["xyz.openbmc_project.Control.ThermalMode"] 627baea6c3fSMatthew Barth ["Current"]; 628baea6c3fSMatthew Barth if (eData != nullptr) 629baea6c3fSMatthew Barth { 630baea6c3fSMatthew Barth sdbusplus::message::message nullMsg{nullptr}; 631baea6c3fSMatthew Barth handleEvent(nullMsg, eData); 632baea6c3fSMatthew Barth } 6336faf8943SMatthew Barth } 634b390df1eSMatthew Barth 6356faf8943SMatthew Barth return current; 6366faf8943SMatthew Barth } 6376faf8943SMatthew Barth 638cc8912e9SMatthew Barth void Zone::saveCurrentMode() 639cc8912e9SMatthew Barth { 640cc8912e9SMatthew Barth fs::path path{CONTROL_PERSIST_ROOT_PATH}; 641cc8912e9SMatthew Barth // Append zone and property description 642cc8912e9SMatthew Barth path /= std::to_string(_zoneNum); 643cc8912e9SMatthew Barth path /= "CurrentMode"; 644cc8912e9SMatthew Barth std::ofstream ofs(path.c_str(), std::ios::binary); 645cc8912e9SMatthew Barth cereal::JSONOutputArchive oArch(ofs); 646cc8912e9SMatthew Barth oArch(ThermalObject::current()); 647cc8912e9SMatthew Barth } 648cc8912e9SMatthew Barth 6499e4db25cSMatthew Barth void Zone::restoreCurrentMode() 6509e4db25cSMatthew Barth { 651a2bed6edSMatthew Barth auto current = ThermalObject::current(); 6529e4db25cSMatthew Barth fs::path path{CONTROL_PERSIST_ROOT_PATH}; 6539e4db25cSMatthew Barth path /= std::to_string(_zoneNum); 6549e4db25cSMatthew Barth path /= "CurrentMode"; 6559e4db25cSMatthew Barth fs::create_directories(path.parent_path()); 6569e4db25cSMatthew Barth 6579e4db25cSMatthew Barth try 6589e4db25cSMatthew Barth { 6599e4db25cSMatthew Barth if (fs::exists(path)) 6609e4db25cSMatthew Barth { 6619e4db25cSMatthew Barth std::ifstream ifs(path.c_str(), std::ios::in | std::ios::binary); 6629e4db25cSMatthew Barth cereal::JSONInputArchive iArch(ifs); 6639e4db25cSMatthew Barth iArch(current); 6649e4db25cSMatthew Barth } 6659e4db25cSMatthew Barth } 6669e4db25cSMatthew Barth catch (std::exception& e) 6679e4db25cSMatthew Barth { 6689e4db25cSMatthew Barth log<level::ERR>(e.what()); 6699e4db25cSMatthew Barth fs::remove(path); 670a2bed6edSMatthew Barth current = ThermalObject::current(); 6719e4db25cSMatthew Barth } 6729e4db25cSMatthew Barth 6739e4db25cSMatthew Barth this->current(current); 6749e4db25cSMatthew Barth } 6759e4db25cSMatthew Barth 6767f88fe61SMatt Spinler } 6777f88fe61SMatt Spinler } 6787f88fe61SMatt Spinler } 679