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 { 209146b7390SMatthew Barth name = getService(std::get<pathPos>(*it), 210146b7390SMatthew Barth std::get<intfPos>(*it)); 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, 329*2885ba90SMatthew Barth std::get<sseNamePos>(event), 3301b4de26aSMatthew Barth std::get<groupPos>(event), 3311b4de26aSMatthew Barth std::get<actionsPos>(event)); 3329014980aSMatthew Barth } 3331b4de26aSMatthew Barth ); 3341bf0ce4bSMatthew Barth } 3351bf0ce4bSMatthew Barth 336f6b76d8eSMatthew Barth void Zone::removeEvent(const SetSpeedEvent& event) 337f6b76d8eSMatthew Barth { 338016bd24cSMatthew Barth // Remove triggers of the event 339016bd24cSMatthew Barth for (auto& trig : std::get<triggerPos>(event)) 340f6b76d8eSMatthew Barth { 341016bd24cSMatthew Barth auto it = findSignal(trig, 34233bfe761SMatthew Barth std::get<groupPos>(event), 34333bfe761SMatthew Barth std::get<actionsPos>(event)); 34433bfe761SMatthew Barth if (it != std::end(getSignalEvents())) 345f9201abbSMatthew Barth { 34633bfe761SMatthew Barth removeSignal(it); 347f9201abbSMatthew Barth } 34833bfe761SMatthew Barth } 34933bfe761SMatthew Barth // Remove timers of the event 35033bfe761SMatthew Barth auto it = findTimer(std::get<groupPos>(event), 35133bfe761SMatthew Barth std::get<actionsPos>(event)); 35233bfe761SMatthew Barth if (it != std::end(getTimerEvents())) 35333bfe761SMatthew Barth { 35433bfe761SMatthew Barth removeTimer(it); 35533bfe761SMatthew Barth } 35633bfe761SMatthew Barth } 35733bfe761SMatthew Barth 35833bfe761SMatthew Barth std::vector<SignalEvent>::iterator Zone::findSignal( 359016bd24cSMatthew Barth const Trigger& signal, 36033bfe761SMatthew Barth const Group& eGroup, 36133bfe761SMatthew Barth const std::vector<Action>& eActions) 36233bfe761SMatthew Barth { 36333bfe761SMatthew Barth // Find the signal in the event to be removed 36433bfe761SMatthew Barth for (auto it = _signalEvents.begin(); it != _signalEvents.end(); ++ it) 36533bfe761SMatthew Barth { 36633bfe761SMatthew Barth const auto& seEventData = *std::get<signalEventDataPos>(*it); 36733bfe761SMatthew Barth if (eGroup == std::get<eventGroupPos>(seEventData) && 36833bfe761SMatthew Barth eActions.size() == std::get<eventActionsPos>(seEventData).size()) 36933bfe761SMatthew Barth { 37033bfe761SMatthew Barth // TODO openbmc/openbmc#2328 - Use the function target 371f9201abbSMatthew Barth // for comparison 372f9201abbSMatthew Barth auto actsEqual = [](auto const& a1, 373f9201abbSMatthew Barth auto const& a2) 374f9201abbSMatthew Barth { 375f9201abbSMatthew Barth return a1.target_type().name() == 376f9201abbSMatthew Barth a2.target_type().name(); 377f9201abbSMatthew Barth }; 37833bfe761SMatthew Barth if (std::equal(eActions.begin(), 37933bfe761SMatthew Barth eActions.end(), 380f9201abbSMatthew Barth std::get<eventActionsPos>(seEventData).begin(), 38133bfe761SMatthew Barth actsEqual)) 382f6b76d8eSMatthew Barth { 38333bfe761SMatthew Barth return it; 384336f18a5SMatthew Barth } 385f6b76d8eSMatthew Barth } 386f6b76d8eSMatthew Barth } 387f6b76d8eSMatthew Barth 38833bfe761SMatthew Barth return _signalEvents.end(); 38933bfe761SMatthew Barth } 39033bfe761SMatthew Barth 391bfb1a566SMatthew Barth std::vector<TimerEvent>::iterator Zone::findTimer( 392bfb1a566SMatthew Barth const Group& eventGroup, 393bfb1a566SMatthew Barth const std::vector<Action>& eventActions) 394bfb1a566SMatthew Barth { 395bfb1a566SMatthew Barth for (auto it = _timerEvents.begin(); it != _timerEvents.end(); ++it) 396bfb1a566SMatthew Barth { 3970420a932SWilliam A. Kennington III const auto& teEventData = *std::get<timerEventDataPos>(*it); 398bfb1a566SMatthew Barth if (std::get<eventActionsPos>(teEventData).size() == 399bfb1a566SMatthew Barth eventActions.size()) 400bfb1a566SMatthew Barth { 401bfb1a566SMatthew Barth // TODO openbmc/openbmc#2328 - Use the action function target 402bfb1a566SMatthew Barth // for comparison 403bfb1a566SMatthew Barth auto actsEqual = [](auto const& a1, 404bfb1a566SMatthew Barth auto const& a2) 405bfb1a566SMatthew Barth { 406bfb1a566SMatthew Barth return a1.target_type().name() == 407bfb1a566SMatthew Barth a2.target_type().name(); 408bfb1a566SMatthew Barth }; 409bfb1a566SMatthew Barth if (std::get<eventGroupPos>(teEventData) == eventGroup && 410bfb1a566SMatthew Barth std::equal(eventActions.begin(), 411bfb1a566SMatthew Barth eventActions.end(), 412bfb1a566SMatthew Barth std::get<eventActionsPos>(teEventData).begin(), 413bfb1a566SMatthew Barth actsEqual)) 414bfb1a566SMatthew Barth { 415bfb1a566SMatthew Barth return it; 416bfb1a566SMatthew Barth } 417bfb1a566SMatthew Barth } 418bfb1a566SMatthew Barth } 419bfb1a566SMatthew Barth 420bfb1a566SMatthew Barth return _timerEvents.end(); 421bfb1a566SMatthew Barth } 422bfb1a566SMatthew Barth 42394fe1a0cSWilliam A. Kennington III void Zone::addTimer(const Group& group, 42494fe1a0cSWilliam A. Kennington III const std::vector<Action>& actions, 42594fe1a0cSWilliam A. Kennington III const TimerConf& tConf) 42694fe1a0cSWilliam A. Kennington III { 4278fd879fbSWilliam A. Kennington III auto eventData = std::make_unique<EventData>( 42894fe1a0cSWilliam A. Kennington III group, 42994fe1a0cSWilliam A. Kennington III "", 43094fe1a0cSWilliam A. Kennington III nullptr, 43194fe1a0cSWilliam A. Kennington III actions 43294fe1a0cSWilliam A. Kennington III ); 4338fd879fbSWilliam A. Kennington III Timer timer( 43494fe1a0cSWilliam A. Kennington III _eventLoop, 435c0c5f07fSWilliam A. Kennington III std::bind(&Zone::timerExpired, 436c0c5f07fSWilliam A. Kennington III this, 4378fd879fbSWilliam A. Kennington III std::cref(std::get<Group>(*eventData)), 4388fd879fbSWilliam A. Kennington III std::cref(std::get<std::vector<Action>>(*eventData)))); 4398fd879fbSWilliam A. Kennington III if (std::get<TimerType>(tConf) == TimerType::repeating) 44094fe1a0cSWilliam A. Kennington III { 4418fd879fbSWilliam A. Kennington III timer.restart(std::get<intervalPos>(tConf)); 44294fe1a0cSWilliam A. Kennington III } 4438fd879fbSWilliam A. Kennington III else if (std::get<TimerType>(tConf) == TimerType::oneshot) 4448fd879fbSWilliam A. Kennington III { 4458fd879fbSWilliam A. Kennington III timer.restartOnce(std::get<intervalPos>(tConf)); 4468fd879fbSWilliam A. Kennington III } 4478fd879fbSWilliam A. Kennington III else 4488fd879fbSWilliam A. Kennington III { 4498fd879fbSWilliam A. Kennington III throw std::invalid_argument("Invalid Timer Type"); 4508fd879fbSWilliam A. Kennington III } 4518fd879fbSWilliam A. Kennington III _timerEvents.emplace_back(std::move(eventData), std::move(timer)); 45294fe1a0cSWilliam A. Kennington III } 45394fe1a0cSWilliam A. Kennington III 454c0c5f07fSWilliam A. Kennington III void Zone::timerExpired(const Group& eventGroup, 455c0c5f07fSWilliam A. Kennington III const std::vector<Action>& eventActions) 4569014980aSMatthew Barth { 457f9201abbSMatthew Barth // Perform the actions 458f9201abbSMatthew Barth std::for_each(eventActions.begin(), 459f9201abbSMatthew Barth eventActions.end(), 460f9201abbSMatthew Barth [this, &eventGroup](auto const& action) 461f9201abbSMatthew Barth { 462f9201abbSMatthew Barth action(*this, eventGroup); 463f9201abbSMatthew Barth }); 4649014980aSMatthew Barth } 4659014980aSMatthew Barth 46638a93a8aSMatthew Barth void Zone::handleEvent(sdbusplus::message::message& msg, 46734f1bda2SMatthew Barth const EventData* eventData) 46838a93a8aSMatthew Barth { 46938a93a8aSMatthew Barth // Handle the callback 47034f1bda2SMatthew Barth std::get<eventHandlerPos>(*eventData)(_bus, msg, *this); 471f9201abbSMatthew Barth // Perform the actions 472f9201abbSMatthew Barth std::for_each( 473f9201abbSMatthew Barth std::get<eventActionsPos>(*eventData).begin(), 474f9201abbSMatthew Barth std::get<eventActionsPos>(*eventData).end(), 475f9201abbSMatthew Barth [this, &eventData](auto const& action) 476f9201abbSMatthew Barth { 477f9201abbSMatthew Barth action(*this, 47834f1bda2SMatthew Barth std::get<eventGroupPos>(*eventData)); 479f9201abbSMatthew Barth }); 48038a93a8aSMatthew Barth } 48138a93a8aSMatthew Barth 482a603ed01SMatthew Barth const std::string& Zone::getService(const std::string& path, 483a603ed01SMatthew Barth const std::string& intf) 484a603ed01SMatthew Barth { 485a603ed01SMatthew Barth // Retrieve service from cache 486a603ed01SMatthew Barth auto srvIter = _servTree.find(path); 487a603ed01SMatthew Barth if (srvIter != _servTree.end()) 488a603ed01SMatthew Barth { 489a603ed01SMatthew Barth for (auto& serv : srvIter->second) 490a603ed01SMatthew Barth { 491a603ed01SMatthew Barth auto it = std::find_if( 492a603ed01SMatthew Barth serv.second.begin(), 493a603ed01SMatthew Barth serv.second.end(), 494a603ed01SMatthew Barth [&intf](auto const& interface) 495a603ed01SMatthew Barth { 496a603ed01SMatthew Barth return intf == interface; 497a603ed01SMatthew Barth }); 498a603ed01SMatthew Barth if (it != std::end(serv.second)) 499a603ed01SMatthew Barth { 500a603ed01SMatthew Barth // Service found 501a603ed01SMatthew Barth return serv.first; 502a603ed01SMatthew Barth } 503a603ed01SMatthew Barth } 504a603ed01SMatthew Barth // Interface not found in cache, add and return 505a603ed01SMatthew Barth return addServices(path, intf, 0); 506a603ed01SMatthew Barth } 507a603ed01SMatthew Barth else 508a603ed01SMatthew Barth { 509a603ed01SMatthew Barth // Path not found in cache, add and return 510a603ed01SMatthew Barth return addServices(path, intf, 0); 511a603ed01SMatthew Barth } 512a603ed01SMatthew Barth } 513a603ed01SMatthew Barth 514a603ed01SMatthew Barth const std::string& Zone::addServices(const std::string& path, 515a603ed01SMatthew Barth const std::string& intf, 516a603ed01SMatthew Barth int32_t depth) 517a603ed01SMatthew Barth { 518a603ed01SMatthew Barth static const std::string empty = ""; 519a603ed01SMatthew Barth auto it = _servTree.end(); 520a603ed01SMatthew Barth 521a603ed01SMatthew Barth // Get all subtree objects for the given interface 522a603ed01SMatthew Barth auto objects = util::SDBusPlus::getSubTree(_bus, "/", intf, depth); 523a603ed01SMatthew Barth // Add what's returned to the cache of path->services 524a603ed01SMatthew Barth for (auto& pIter : objects) 525a603ed01SMatthew Barth { 526a603ed01SMatthew Barth auto pathIter = _servTree.find(pIter.first); 527a603ed01SMatthew Barth if (pathIter != _servTree.end()) 528a603ed01SMatthew Barth { 529a603ed01SMatthew Barth // Path found in cache 530a603ed01SMatthew Barth for (auto& sIter : pIter.second) 531a603ed01SMatthew Barth { 532a603ed01SMatthew Barth auto servIter = pathIter->second.find(sIter.first); 533a603ed01SMatthew Barth if (servIter != pathIter->second.end()) 534a603ed01SMatthew Barth { 535a603ed01SMatthew Barth // Service found in cache 536a603ed01SMatthew Barth for (auto& iIter : sIter.second) 537a603ed01SMatthew Barth { 538e8b340bdSMatthew Barth if (std::find(servIter->second.begin(), 539e8b340bdSMatthew Barth servIter->second.end(), 540e8b340bdSMatthew Barth iIter) == servIter->second.end()) 541e8b340bdSMatthew Barth { 542a603ed01SMatthew Barth // Add interface to cache 543a603ed01SMatthew Barth servIter->second.emplace_back(iIter); 544a603ed01SMatthew Barth } 545a603ed01SMatthew Barth } 546e8b340bdSMatthew Barth } 547a603ed01SMatthew Barth else 548a603ed01SMatthew Barth { 549a603ed01SMatthew Barth // Service not found in cache 550a603ed01SMatthew Barth pathIter->second.insert(sIter); 551a603ed01SMatthew Barth } 552a603ed01SMatthew Barth } 553a603ed01SMatthew Barth } 554a603ed01SMatthew Barth else 555a603ed01SMatthew Barth { 556a603ed01SMatthew Barth _servTree.insert(pIter); 557a603ed01SMatthew Barth } 558a603ed01SMatthew Barth // When the paths match, since a single interface constraint is given, 559a603ed01SMatthew Barth // that is the service to return 560a603ed01SMatthew Barth if (path == pIter.first) 561a603ed01SMatthew Barth { 562a603ed01SMatthew Barth it = _servTree.find(pIter.first); 563a603ed01SMatthew Barth } 564a603ed01SMatthew Barth } 565a603ed01SMatthew Barth 566a603ed01SMatthew Barth if (it != _servTree.end()) 567a603ed01SMatthew Barth { 568a603ed01SMatthew Barth return it->second.begin()->first; 569a603ed01SMatthew Barth } 570a603ed01SMatthew Barth 571a603ed01SMatthew Barth return empty; 572a603ed01SMatthew Barth } 573a603ed01SMatthew Barth 57470b2e7daSMatthew Barth auto Zone::getPersisted(const std::string& intf, 57570b2e7daSMatthew Barth const std::string& prop) 57670b2e7daSMatthew Barth { 57770b2e7daSMatthew Barth auto persisted = false; 57870b2e7daSMatthew Barth 57970b2e7daSMatthew Barth auto it = _persisted.find(intf); 58070b2e7daSMatthew Barth if (it != _persisted.end()) 58170b2e7daSMatthew Barth { 58270b2e7daSMatthew Barth return std::any_of(it->second.begin(), 58370b2e7daSMatthew Barth it->second.end(), 58470b2e7daSMatthew Barth [&prop](auto& p) 58570b2e7daSMatthew Barth { 58670b2e7daSMatthew Barth return prop == p; 58770b2e7daSMatthew Barth }); 58870b2e7daSMatthew Barth } 58970b2e7daSMatthew Barth 59070b2e7daSMatthew Barth return persisted; 59170b2e7daSMatthew Barth } 59270b2e7daSMatthew Barth 5936faf8943SMatthew Barth std::string Zone::current(std::string value) 5946faf8943SMatthew Barth { 595b390df1eSMatthew Barth auto current = ThermalObject::current(); 596b390df1eSMatthew Barth std::transform(value.begin(), value.end(), value.begin(), toupper); 597b390df1eSMatthew Barth 598221c90c3SMatthew Barth auto supported = ThermalObject::supported(); 599221c90c3SMatthew Barth auto isSupported = std::any_of( 600221c90c3SMatthew Barth supported.begin(), 601221c90c3SMatthew Barth supported.end(), 602221c90c3SMatthew Barth [&value](auto& s) 603221c90c3SMatthew Barth { 604221c90c3SMatthew Barth std::transform(s.begin(), s.end(), s.begin(), toupper); 605221c90c3SMatthew Barth return value == s; 606221c90c3SMatthew Barth }); 607221c90c3SMatthew Barth 608221c90c3SMatthew Barth if (value != current && isSupported) 6096faf8943SMatthew Barth { 6106faf8943SMatthew Barth current = ThermalObject::current(value); 61170b2e7daSMatthew Barth if (getPersisted("xyz.openbmc_project.Control.ThermalMode", "Current")) 61270b2e7daSMatthew Barth { 6136faf8943SMatthew Barth saveCurrentMode(); 61470b2e7daSMatthew Barth } 615b390df1eSMatthew Barth // Trigger event(s) for current mode property change 6160a1f686cSMatthew Barth auto eData = _objects[_path] 617baea6c3fSMatthew Barth ["xyz.openbmc_project.Control.ThermalMode"] 618baea6c3fSMatthew Barth ["Current"]; 619baea6c3fSMatthew Barth if (eData != nullptr) 620baea6c3fSMatthew Barth { 621baea6c3fSMatthew Barth sdbusplus::message::message nullMsg{nullptr}; 622baea6c3fSMatthew Barth handleEvent(nullMsg, eData); 623baea6c3fSMatthew Barth } 6246faf8943SMatthew Barth } 625b390df1eSMatthew Barth 6266faf8943SMatthew Barth return current; 6276faf8943SMatthew Barth } 6286faf8943SMatthew Barth 629cc8912e9SMatthew Barth void Zone::saveCurrentMode() 630cc8912e9SMatthew Barth { 631cc8912e9SMatthew Barth fs::path path{CONTROL_PERSIST_ROOT_PATH}; 632cc8912e9SMatthew Barth // Append zone and property description 633cc8912e9SMatthew Barth path /= std::to_string(_zoneNum); 634cc8912e9SMatthew Barth path /= "CurrentMode"; 635cc8912e9SMatthew Barth std::ofstream ofs(path.c_str(), std::ios::binary); 636cc8912e9SMatthew Barth cereal::JSONOutputArchive oArch(ofs); 637cc8912e9SMatthew Barth oArch(ThermalObject::current()); 638cc8912e9SMatthew Barth } 639cc8912e9SMatthew Barth 6409e4db25cSMatthew Barth void Zone::restoreCurrentMode() 6419e4db25cSMatthew Barth { 642a2bed6edSMatthew Barth auto current = ThermalObject::current(); 6439e4db25cSMatthew Barth fs::path path{CONTROL_PERSIST_ROOT_PATH}; 6449e4db25cSMatthew Barth path /= std::to_string(_zoneNum); 6459e4db25cSMatthew Barth path /= "CurrentMode"; 6469e4db25cSMatthew Barth fs::create_directories(path.parent_path()); 6479e4db25cSMatthew Barth 6489e4db25cSMatthew Barth try 6499e4db25cSMatthew Barth { 6509e4db25cSMatthew Barth if (fs::exists(path)) 6519e4db25cSMatthew Barth { 6529e4db25cSMatthew Barth std::ifstream ifs(path.c_str(), std::ios::in | std::ios::binary); 6539e4db25cSMatthew Barth cereal::JSONInputArchive iArch(ifs); 6549e4db25cSMatthew Barth iArch(current); 6559e4db25cSMatthew Barth } 6569e4db25cSMatthew Barth } 6579e4db25cSMatthew Barth catch (std::exception& e) 6589e4db25cSMatthew Barth { 6599e4db25cSMatthew Barth log<level::ERR>(e.what()); 6609e4db25cSMatthew Barth fs::remove(path); 661a2bed6edSMatthew Barth current = ThermalObject::current(); 6629e4db25cSMatthew Barth } 6639e4db25cSMatthew Barth 6649e4db25cSMatthew Barth this->current(current); 6659e4db25cSMatthew Barth } 6669e4db25cSMatthew Barth 6677f88fe61SMatt Spinler } 6687f88fe61SMatt Spinler } 6697f88fe61SMatt Spinler } 670