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 { 322336f18a5SMatthew Barth sdbusplus::message::message nullMsg{nullptr}; 323336f18a5SMatthew Barth 32467967f9aSMatthew Barth for (auto& sig : std::get<signalsPos>(event)) 3251bf0ce4bSMatthew Barth { 326336f18a5SMatthew Barth // Setup signal matches of the property for event 327f6b76d8eSMatthew Barth std::unique_ptr<EventData> eventData = 3281bf0ce4bSMatthew Barth std::make_unique<EventData>( 3291bf0ce4bSMatthew Barth std::get<groupPos>(event), 330336f18a5SMatthew Barth std::get<sigMatchPos>(sig), 331336f18a5SMatthew Barth std::get<sigHandlerPos>(sig), 332f9201abbSMatthew Barth std::get<actionsPos>(event) 333f6b76d8eSMatthew Barth ); 334766f8545SMatthew Barth 335766f8545SMatthew Barth // When match is empty, handle if zone object member 336766f8545SMatthew Barth if (std::get<sigMatchPos>(sig).empty()) 337766f8545SMatthew Barth { 338766f8545SMatthew Barth // Set event data for each host group member 339766f8545SMatthew Barth for (auto it = std::get<groupPos>(event).begin(); 340766f8545SMatthew Barth it != std::get<groupPos>(event).end(); ++it) 341766f8545SMatthew Barth { 3420a1f686cSMatthew Barth if (it->first == _path) 343766f8545SMatthew Barth { 344766f8545SMatthew Barth // Group member interface in list owned by zone 345766f8545SMatthew Barth if (std::find(_ifaces.begin(), _ifaces.end(), 346766f8545SMatthew Barth std::get<intfPos>(it->second)) != _ifaces.end()) 347766f8545SMatthew Barth { 348766f8545SMatthew Barth // Store path,interface,property as a managed object 349766f8545SMatthew Barth _objects[it->first] 350766f8545SMatthew Barth [std::get<intfPos>(it->second)] 351766f8545SMatthew Barth [std::get<propPos>(it->second)] = 352766f8545SMatthew Barth eventData.get(); 353766f8545SMatthew Barth } 354766f8545SMatthew Barth } 355766f8545SMatthew Barth } 356766f8545SMatthew Barth } 357766f8545SMatthew Barth 358766f8545SMatthew Barth // Initialize the event signal using handler 359766f8545SMatthew Barth std::get<sigHandlerPos>(sig)(_bus, nullMsg, *this); 360766f8545SMatthew Barth 361766f8545SMatthew Barth // Subscribe to signal match 362336f18a5SMatthew Barth std::unique_ptr<sdbusplus::server::match::match> match = nullptr; 363336f18a5SMatthew Barth if (!std::get<sigMatchPos>(sig).empty()) 364336f18a5SMatthew Barth { 365336f18a5SMatthew Barth match = std::make_unique<sdbusplus::server::match::match>( 3661bf0ce4bSMatthew Barth _bus, 367336f18a5SMatthew Barth std::get<sigMatchPos>(sig).c_str(), 3681bf0ce4bSMatthew Barth std::bind(std::mem_fn(&Zone::handleEvent), 3691bf0ce4bSMatthew Barth this, 3701bf0ce4bSMatthew Barth std::placeholders::_1, 371f6b76d8eSMatthew Barth eventData.get()) 372f6b76d8eSMatthew Barth ); 373336f18a5SMatthew Barth } 374766f8545SMatthew Barth 375f6b76d8eSMatthew Barth _signalEvents.emplace_back(std::move(eventData), std::move(match)); 3761bf0ce4bSMatthew Barth } 377*1b4de26aSMatthew Barth // Enable event triggers 378*1b4de26aSMatthew Barth std::for_each( 379*1b4de26aSMatthew Barth std::get<triggerPos>(event).begin(), 380*1b4de26aSMatthew Barth std::get<triggerPos>(event).end(), 381*1b4de26aSMatthew Barth [this, &event](auto const& trigger) 3829014980aSMatthew Barth { 383*1b4de26aSMatthew Barth trigger(*this, 384*1b4de26aSMatthew Barth std::get<groupPos>(event), 385*1b4de26aSMatthew Barth std::get<actionsPos>(event)); 3869014980aSMatthew Barth } 387*1b4de26aSMatthew Barth ); 388*1b4de26aSMatthew Barth 389f9201abbSMatthew Barth // Run action functions for initial event state 390f9201abbSMatthew Barth std::for_each( 391f9201abbSMatthew Barth std::get<actionsPos>(event).begin(), 392f9201abbSMatthew Barth std::get<actionsPos>(event).end(), 393f9201abbSMatthew Barth [this, &event](auto const& action) 394f9201abbSMatthew Barth { 395f9201abbSMatthew Barth action(*this, 3961bf0ce4bSMatthew Barth std::get<groupPos>(event)); 397f9201abbSMatthew Barth }); 3981bf0ce4bSMatthew Barth } 3991bf0ce4bSMatthew Barth 400f6b76d8eSMatthew Barth void Zone::removeEvent(const SetSpeedEvent& event) 401f6b76d8eSMatthew Barth { 40233bfe761SMatthew Barth // Remove signals of the event 40333bfe761SMatthew Barth for (auto& sig : std::get<signalsPos>(event)) 404f6b76d8eSMatthew Barth { 40533bfe761SMatthew Barth auto it = findSignal(sig, 40633bfe761SMatthew Barth std::get<groupPos>(event), 40733bfe761SMatthew Barth std::get<actionsPos>(event)); 40833bfe761SMatthew Barth if (it != std::end(getSignalEvents())) 409f9201abbSMatthew Barth { 41033bfe761SMatthew Barth removeSignal(it); 411f9201abbSMatthew Barth } 41233bfe761SMatthew Barth } 41333bfe761SMatthew Barth // Remove timers of the event 41433bfe761SMatthew Barth auto it = findTimer(std::get<groupPos>(event), 41533bfe761SMatthew Barth std::get<actionsPos>(event)); 41633bfe761SMatthew Barth if (it != std::end(getTimerEvents())) 41733bfe761SMatthew Barth { 41833bfe761SMatthew Barth removeTimer(it); 41933bfe761SMatthew Barth } 42033bfe761SMatthew Barth } 42133bfe761SMatthew Barth 42233bfe761SMatthew Barth std::vector<SignalEvent>::iterator Zone::findSignal( 42333bfe761SMatthew Barth const Signal& signal, 42433bfe761SMatthew Barth const Group& eGroup, 42533bfe761SMatthew Barth const std::vector<Action>& eActions) 42633bfe761SMatthew Barth { 42733bfe761SMatthew Barth // Find the signal in the event to be removed 42833bfe761SMatthew Barth for (auto it = _signalEvents.begin(); it != _signalEvents.end(); ++ it) 42933bfe761SMatthew Barth { 43033bfe761SMatthew Barth const auto& seEventData = *std::get<signalEventDataPos>(*it); 43133bfe761SMatthew Barth if (eGroup == std::get<eventGroupPos>(seEventData) && 43233bfe761SMatthew Barth std::get<sigMatchPos>(signal) == 43333bfe761SMatthew Barth std::get<eventMatchPos>(seEventData) && 43433bfe761SMatthew Barth std::get<sigHandlerPos>(signal).target_type().name() == 43533bfe761SMatthew Barth std::get<eventHandlerPos>(seEventData).target_type().name() && 43633bfe761SMatthew Barth eActions.size() == std::get<eventActionsPos>(seEventData).size()) 43733bfe761SMatthew Barth { 43833bfe761SMatthew Barth // TODO openbmc/openbmc#2328 - Use the function target 439f9201abbSMatthew Barth // for comparison 440f9201abbSMatthew Barth auto actsEqual = [](auto const& a1, 441f9201abbSMatthew Barth auto const& a2) 442f9201abbSMatthew Barth { 443f9201abbSMatthew Barth return a1.target_type().name() == 444f9201abbSMatthew Barth a2.target_type().name(); 445f9201abbSMatthew Barth }; 44633bfe761SMatthew Barth if (std::equal(eActions.begin(), 44733bfe761SMatthew Barth eActions.end(), 448f9201abbSMatthew Barth std::get<eventActionsPos>(seEventData).begin(), 44933bfe761SMatthew Barth actsEqual)) 450f6b76d8eSMatthew Barth { 45133bfe761SMatthew Barth return it; 452336f18a5SMatthew Barth } 453f6b76d8eSMatthew Barth } 454f6b76d8eSMatthew Barth } 455f6b76d8eSMatthew Barth 45633bfe761SMatthew Barth return _signalEvents.end(); 45733bfe761SMatthew Barth } 45833bfe761SMatthew Barth 459bfb1a566SMatthew Barth std::vector<TimerEvent>::iterator Zone::findTimer( 460bfb1a566SMatthew Barth const Group& eventGroup, 461bfb1a566SMatthew Barth const std::vector<Action>& eventActions) 462bfb1a566SMatthew Barth { 463bfb1a566SMatthew Barth for (auto it = _timerEvents.begin(); it != _timerEvents.end(); ++it) 464bfb1a566SMatthew Barth { 4650420a932SWilliam A. Kennington III const auto& teEventData = *std::get<timerEventDataPos>(*it); 466bfb1a566SMatthew Barth if (std::get<eventActionsPos>(teEventData).size() == 467bfb1a566SMatthew Barth eventActions.size()) 468bfb1a566SMatthew Barth { 469bfb1a566SMatthew Barth // TODO openbmc/openbmc#2328 - Use the action function target 470bfb1a566SMatthew Barth // for comparison 471bfb1a566SMatthew Barth auto actsEqual = [](auto const& a1, 472bfb1a566SMatthew Barth auto const& a2) 473bfb1a566SMatthew Barth { 474bfb1a566SMatthew Barth return a1.target_type().name() == 475bfb1a566SMatthew Barth a2.target_type().name(); 476bfb1a566SMatthew Barth }; 477bfb1a566SMatthew Barth if (std::get<eventGroupPos>(teEventData) == eventGroup && 478bfb1a566SMatthew Barth std::equal(eventActions.begin(), 479bfb1a566SMatthew Barth eventActions.end(), 480bfb1a566SMatthew Barth std::get<eventActionsPos>(teEventData).begin(), 481bfb1a566SMatthew Barth actsEqual)) 482bfb1a566SMatthew Barth { 483bfb1a566SMatthew Barth return it; 484bfb1a566SMatthew Barth } 485bfb1a566SMatthew Barth } 486bfb1a566SMatthew Barth } 487bfb1a566SMatthew Barth 488bfb1a566SMatthew Barth return _timerEvents.end(); 489bfb1a566SMatthew Barth } 490bfb1a566SMatthew Barth 49194fe1a0cSWilliam A. Kennington III void Zone::addTimer(const Group& group, 49294fe1a0cSWilliam A. Kennington III const std::vector<Action>& actions, 49394fe1a0cSWilliam A. Kennington III const TimerConf& tConf) 49494fe1a0cSWilliam A. Kennington III { 4958fd879fbSWilliam A. Kennington III auto eventData = std::make_unique<EventData>( 49694fe1a0cSWilliam A. Kennington III group, 49794fe1a0cSWilliam A. Kennington III "", 49894fe1a0cSWilliam A. Kennington III nullptr, 49994fe1a0cSWilliam A. Kennington III actions 50094fe1a0cSWilliam A. Kennington III ); 5018fd879fbSWilliam A. Kennington III Timer timer( 50294fe1a0cSWilliam A. Kennington III _eventLoop, 503c0c5f07fSWilliam A. Kennington III std::bind(&Zone::timerExpired, 504c0c5f07fSWilliam A. Kennington III this, 5058fd879fbSWilliam A. Kennington III std::cref(std::get<Group>(*eventData)), 5068fd879fbSWilliam A. Kennington III std::cref(std::get<std::vector<Action>>(*eventData)))); 5078fd879fbSWilliam A. Kennington III if (std::get<TimerType>(tConf) == TimerType::repeating) 50894fe1a0cSWilliam A. Kennington III { 5098fd879fbSWilliam A. Kennington III timer.restart(std::get<intervalPos>(tConf)); 51094fe1a0cSWilliam A. Kennington III } 5118fd879fbSWilliam A. Kennington III else if (std::get<TimerType>(tConf) == TimerType::oneshot) 5128fd879fbSWilliam A. Kennington III { 5138fd879fbSWilliam A. Kennington III timer.restartOnce(std::get<intervalPos>(tConf)); 5148fd879fbSWilliam A. Kennington III } 5158fd879fbSWilliam A. Kennington III else 5168fd879fbSWilliam A. Kennington III { 5178fd879fbSWilliam A. Kennington III throw std::invalid_argument("Invalid Timer Type"); 5188fd879fbSWilliam A. Kennington III } 5198fd879fbSWilliam A. Kennington III _timerEvents.emplace_back(std::move(eventData), std::move(timer)); 52094fe1a0cSWilliam A. Kennington III } 52194fe1a0cSWilliam A. Kennington III 522c0c5f07fSWilliam A. Kennington III void Zone::timerExpired(const Group& eventGroup, 523c0c5f07fSWilliam A. Kennington III const std::vector<Action>& eventActions) 5249014980aSMatthew Barth { 525f9201abbSMatthew Barth // Perform the actions 526f9201abbSMatthew Barth std::for_each(eventActions.begin(), 527f9201abbSMatthew Barth eventActions.end(), 528f9201abbSMatthew Barth [this, &eventGroup](auto const& action) 529f9201abbSMatthew Barth { 530f9201abbSMatthew Barth action(*this, eventGroup); 531f9201abbSMatthew Barth }); 5329014980aSMatthew Barth } 5339014980aSMatthew Barth 53438a93a8aSMatthew Barth void Zone::handleEvent(sdbusplus::message::message& msg, 53534f1bda2SMatthew Barth const EventData* eventData) 53638a93a8aSMatthew Barth { 53738a93a8aSMatthew Barth // Handle the callback 53834f1bda2SMatthew Barth std::get<eventHandlerPos>(*eventData)(_bus, msg, *this); 539f9201abbSMatthew Barth // Perform the actions 540f9201abbSMatthew Barth std::for_each( 541f9201abbSMatthew Barth std::get<eventActionsPos>(*eventData).begin(), 542f9201abbSMatthew Barth std::get<eventActionsPos>(*eventData).end(), 543f9201abbSMatthew Barth [this, &eventData](auto const& action) 544f9201abbSMatthew Barth { 545f9201abbSMatthew Barth action(*this, 54634f1bda2SMatthew Barth std::get<eventGroupPos>(*eventData)); 547f9201abbSMatthew Barth }); 54838a93a8aSMatthew Barth } 54938a93a8aSMatthew Barth 550a603ed01SMatthew Barth const std::string& Zone::getService(const std::string& path, 551a603ed01SMatthew Barth const std::string& intf) 552a603ed01SMatthew Barth { 553a603ed01SMatthew Barth // Retrieve service from cache 554a603ed01SMatthew Barth auto srvIter = _servTree.find(path); 555a603ed01SMatthew Barth if (srvIter != _servTree.end()) 556a603ed01SMatthew Barth { 557a603ed01SMatthew Barth for (auto& serv : srvIter->second) 558a603ed01SMatthew Barth { 559a603ed01SMatthew Barth auto it = std::find_if( 560a603ed01SMatthew Barth serv.second.begin(), 561a603ed01SMatthew Barth serv.second.end(), 562a603ed01SMatthew Barth [&intf](auto const& interface) 563a603ed01SMatthew Barth { 564a603ed01SMatthew Barth return intf == interface; 565a603ed01SMatthew Barth }); 566a603ed01SMatthew Barth if (it != std::end(serv.second)) 567a603ed01SMatthew Barth { 568a603ed01SMatthew Barth // Service found 569a603ed01SMatthew Barth return serv.first; 570a603ed01SMatthew Barth } 571a603ed01SMatthew Barth } 572a603ed01SMatthew Barth // Interface not found in cache, add and return 573a603ed01SMatthew Barth return addServices(path, intf, 0); 574a603ed01SMatthew Barth } 575a603ed01SMatthew Barth else 576a603ed01SMatthew Barth { 577a603ed01SMatthew Barth // Path not found in cache, add and return 578a603ed01SMatthew Barth return addServices(path, intf, 0); 579a603ed01SMatthew Barth } 580a603ed01SMatthew Barth } 581a603ed01SMatthew Barth 582a603ed01SMatthew Barth const std::string& Zone::addServices(const std::string& path, 583a603ed01SMatthew Barth const std::string& intf, 584a603ed01SMatthew Barth int32_t depth) 585a603ed01SMatthew Barth { 586a603ed01SMatthew Barth static const std::string empty = ""; 587a603ed01SMatthew Barth auto it = _servTree.end(); 588a603ed01SMatthew Barth 589a603ed01SMatthew Barth // Get all subtree objects for the given interface 590a603ed01SMatthew Barth auto objects = util::SDBusPlus::getSubTree(_bus, "/", intf, depth); 591a603ed01SMatthew Barth // Add what's returned to the cache of path->services 592a603ed01SMatthew Barth for (auto& pIter : objects) 593a603ed01SMatthew Barth { 594a603ed01SMatthew Barth auto pathIter = _servTree.find(pIter.first); 595a603ed01SMatthew Barth if (pathIter != _servTree.end()) 596a603ed01SMatthew Barth { 597a603ed01SMatthew Barth // Path found in cache 598a603ed01SMatthew Barth for (auto& sIter : pIter.second) 599a603ed01SMatthew Barth { 600a603ed01SMatthew Barth auto servIter = pathIter->second.find(sIter.first); 601a603ed01SMatthew Barth if (servIter != pathIter->second.end()) 602a603ed01SMatthew Barth { 603a603ed01SMatthew Barth // Service found in cache 604a603ed01SMatthew Barth for (auto& iIter : sIter.second) 605a603ed01SMatthew Barth { 606e8b340bdSMatthew Barth if (std::find(servIter->second.begin(), 607e8b340bdSMatthew Barth servIter->second.end(), 608e8b340bdSMatthew Barth iIter) == servIter->second.end()) 609e8b340bdSMatthew Barth { 610a603ed01SMatthew Barth // Add interface to cache 611a603ed01SMatthew Barth servIter->second.emplace_back(iIter); 612a603ed01SMatthew Barth } 613a603ed01SMatthew Barth } 614e8b340bdSMatthew Barth } 615a603ed01SMatthew Barth else 616a603ed01SMatthew Barth { 617a603ed01SMatthew Barth // Service not found in cache 618a603ed01SMatthew Barth pathIter->second.insert(sIter); 619a603ed01SMatthew Barth } 620a603ed01SMatthew Barth } 621a603ed01SMatthew Barth } 622a603ed01SMatthew Barth else 623a603ed01SMatthew Barth { 624a603ed01SMatthew Barth _servTree.insert(pIter); 625a603ed01SMatthew Barth } 626a603ed01SMatthew Barth // When the paths match, since a single interface constraint is given, 627a603ed01SMatthew Barth // that is the service to return 628a603ed01SMatthew Barth if (path == pIter.first) 629a603ed01SMatthew Barth { 630a603ed01SMatthew Barth it = _servTree.find(pIter.first); 631a603ed01SMatthew Barth } 632a603ed01SMatthew Barth } 633a603ed01SMatthew Barth 634a603ed01SMatthew Barth if (it != _servTree.end()) 635a603ed01SMatthew Barth { 636a603ed01SMatthew Barth return it->second.begin()->first; 637a603ed01SMatthew Barth } 638a603ed01SMatthew Barth 639a603ed01SMatthew Barth return empty; 640a603ed01SMatthew Barth } 641a603ed01SMatthew Barth 64270b2e7daSMatthew Barth auto Zone::getPersisted(const std::string& intf, 64370b2e7daSMatthew Barth const std::string& prop) 64470b2e7daSMatthew Barth { 64570b2e7daSMatthew Barth auto persisted = false; 64670b2e7daSMatthew Barth 64770b2e7daSMatthew Barth auto it = _persisted.find(intf); 64870b2e7daSMatthew Barth if (it != _persisted.end()) 64970b2e7daSMatthew Barth { 65070b2e7daSMatthew Barth return std::any_of(it->second.begin(), 65170b2e7daSMatthew Barth it->second.end(), 65270b2e7daSMatthew Barth [&prop](auto& p) 65370b2e7daSMatthew Barth { 65470b2e7daSMatthew Barth return prop == p; 65570b2e7daSMatthew Barth }); 65670b2e7daSMatthew Barth } 65770b2e7daSMatthew Barth 65870b2e7daSMatthew Barth return persisted; 65970b2e7daSMatthew Barth } 66070b2e7daSMatthew Barth 6616faf8943SMatthew Barth std::string Zone::current(std::string value) 6626faf8943SMatthew Barth { 663b390df1eSMatthew Barth auto current = ThermalObject::current(); 664b390df1eSMatthew Barth std::transform(value.begin(), value.end(), value.begin(), toupper); 665b390df1eSMatthew Barth 666221c90c3SMatthew Barth auto supported = ThermalObject::supported(); 667221c90c3SMatthew Barth auto isSupported = std::any_of( 668221c90c3SMatthew Barth supported.begin(), 669221c90c3SMatthew Barth supported.end(), 670221c90c3SMatthew Barth [&value](auto& s) 671221c90c3SMatthew Barth { 672221c90c3SMatthew Barth std::transform(s.begin(), s.end(), s.begin(), toupper); 673221c90c3SMatthew Barth return value == s; 674221c90c3SMatthew Barth }); 675221c90c3SMatthew Barth 676221c90c3SMatthew Barth if (value != current && isSupported) 6776faf8943SMatthew Barth { 6786faf8943SMatthew Barth current = ThermalObject::current(value); 67970b2e7daSMatthew Barth if (getPersisted("xyz.openbmc_project.Control.ThermalMode", "Current")) 68070b2e7daSMatthew Barth { 6816faf8943SMatthew Barth saveCurrentMode(); 68270b2e7daSMatthew Barth } 683b390df1eSMatthew Barth // Trigger event(s) for current mode property change 6840a1f686cSMatthew Barth auto eData = _objects[_path] 685baea6c3fSMatthew Barth ["xyz.openbmc_project.Control.ThermalMode"] 686baea6c3fSMatthew Barth ["Current"]; 687baea6c3fSMatthew Barth if (eData != nullptr) 688baea6c3fSMatthew Barth { 689baea6c3fSMatthew Barth sdbusplus::message::message nullMsg{nullptr}; 690baea6c3fSMatthew Barth handleEvent(nullMsg, eData); 691baea6c3fSMatthew Barth } 6926faf8943SMatthew Barth } 693b390df1eSMatthew Barth 6946faf8943SMatthew Barth return current; 6956faf8943SMatthew Barth } 6966faf8943SMatthew Barth 697cc8912e9SMatthew Barth void Zone::saveCurrentMode() 698cc8912e9SMatthew Barth { 699cc8912e9SMatthew Barth fs::path path{CONTROL_PERSIST_ROOT_PATH}; 700cc8912e9SMatthew Barth // Append zone and property description 701cc8912e9SMatthew Barth path /= std::to_string(_zoneNum); 702cc8912e9SMatthew Barth path /= "CurrentMode"; 703cc8912e9SMatthew Barth std::ofstream ofs(path.c_str(), std::ios::binary); 704cc8912e9SMatthew Barth cereal::JSONOutputArchive oArch(ofs); 705cc8912e9SMatthew Barth oArch(ThermalObject::current()); 706cc8912e9SMatthew Barth } 707cc8912e9SMatthew Barth 7089e4db25cSMatthew Barth void Zone::restoreCurrentMode() 7099e4db25cSMatthew Barth { 710a2bed6edSMatthew Barth auto current = ThermalObject::current(); 7119e4db25cSMatthew Barth fs::path path{CONTROL_PERSIST_ROOT_PATH}; 7129e4db25cSMatthew Barth path /= std::to_string(_zoneNum); 7139e4db25cSMatthew Barth path /= "CurrentMode"; 7149e4db25cSMatthew Barth fs::create_directories(path.parent_path()); 7159e4db25cSMatthew Barth 7169e4db25cSMatthew Barth try 7179e4db25cSMatthew Barth { 7189e4db25cSMatthew Barth if (fs::exists(path)) 7199e4db25cSMatthew Barth { 7209e4db25cSMatthew Barth std::ifstream ifs(path.c_str(), std::ios::in | std::ios::binary); 7219e4db25cSMatthew Barth cereal::JSONInputArchive iArch(ifs); 7229e4db25cSMatthew Barth iArch(current); 7239e4db25cSMatthew Barth } 7249e4db25cSMatthew Barth } 7259e4db25cSMatthew Barth catch (std::exception& e) 7269e4db25cSMatthew Barth { 7279e4db25cSMatthew Barth log<level::ERR>(e.what()); 7289e4db25cSMatthew Barth fs::remove(path); 729a2bed6edSMatthew Barth current = ThermalObject::current(); 7309e4db25cSMatthew Barth } 7319e4db25cSMatthew Barth 7329e4db25cSMatthew Barth this->current(current); 7339e4db25cSMatthew Barth } 7349e4db25cSMatthew Barth 7357f88fe61SMatt Spinler } 7367f88fe61SMatt Spinler } 7377f88fe61SMatt Spinler } 738