1a227a16dSMatthew Barth /** 2a227a16dSMatthew Barth * Copyright © 2020 IBM Corporation 3a227a16dSMatthew Barth * 4a227a16dSMatthew Barth * Licensed under the Apache License, Version 2.0 (the "License"); 5a227a16dSMatthew Barth * you may not use this file except in compliance with the License. 6a227a16dSMatthew Barth * You may obtain a copy of the License at 7a227a16dSMatthew Barth * 8a227a16dSMatthew Barth * http://www.apache.org/licenses/LICENSE-2.0 9a227a16dSMatthew Barth * 10a227a16dSMatthew Barth * Unless required by applicable law or agreed to in writing, software 11a227a16dSMatthew Barth * distributed under the License is distributed on an "AS IS" BASIS, 12a227a16dSMatthew Barth * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13a227a16dSMatthew Barth * See the License for the specific language governing permissions and 14a227a16dSMatthew Barth * limitations under the License. 15a227a16dSMatthew Barth */ 16b584d818SMatthew Barth #include "config.h" 17b584d818SMatthew Barth 18a227a16dSMatthew Barth #include "manager.hpp" 19a227a16dSMatthew Barth 202fc0a35dSMatt Spinler #include "../utils/flight_recorder.hpp" 21d9cb63b6SMatthew Barth #include "action.hpp" 2244ab7693SMatthew Barth #include "event.hpp" 23de90fb4dSMatthew Barth #include "fan.hpp" 24d9cb63b6SMatthew Barth #include "group.hpp" 25a227a16dSMatthew Barth #include "json_config.hpp" 2648f44daaSMatthew Barth #include "power_state.hpp" 2706764946SMatthew Barth #include "profile.hpp" 289403a217SMatthew Barth #include "sdbusplus.hpp" 29acd737cdSMatthew Barth #include "zone.hpp" 30a227a16dSMatthew Barth 31acd737cdSMatthew Barth #include <nlohmann/json.hpp> 32a227a16dSMatthew Barth #include <sdbusplus/bus.hpp> 331542fb5aSMatthew Barth #include <sdbusplus/server/manager.hpp> 34acd737cdSMatthew Barth #include <sdeventplus/event.hpp> 35d9cb63b6SMatthew Barth #include <sdeventplus/utility/timer.hpp> 36a227a16dSMatthew Barth 37de90fb4dSMatthew Barth #include <algorithm> 38d9cb63b6SMatthew Barth #include <chrono> 39a227a16dSMatthew Barth #include <filesystem> 40d9cb63b6SMatthew Barth #include <functional> 41d9cb63b6SMatthew Barth #include <map> 42d9cb63b6SMatthew Barth #include <memory> 43d9cb63b6SMatthew Barth #include <tuple> 44d9cb63b6SMatthew Barth #include <utility> 4506764946SMatthew Barth #include <vector> 46a227a16dSMatthew Barth 47a227a16dSMatthew Barth namespace phosphor::fan::control::json 48a227a16dSMatthew Barth { 49a227a16dSMatthew Barth 50acd737cdSMatthew Barth using json = nlohmann::json; 51acd737cdSMatthew Barth 52acd737cdSMatthew Barth std::vector<std::string> Manager::_activeProfiles; 5312cb125aSMatthew Barth std::map<std::string, 544ca87faeSMatthew Barth std::map<std::string, std::pair<bool, std::vector<std::string>>>> 5512cb125aSMatthew Barth Manager::_servTree; 5607fecfc6SMatthew Barth std::map<std::string, 5707fecfc6SMatthew Barth std::map<std::string, std::map<std::string, PropertyVariantType>>> 5807fecfc6SMatthew Barth Manager::_objects; 59d76351bdSMatt Spinler std::unordered_map<std::string, PropertyVariantType> Manager::_parameters; 60acd737cdSMatthew Barth 61*7787def0SMatt Spinler const std::string Manager::dumpFile = "/tmp/fan_control_dump.json"; 62*7787def0SMatt Spinler 639403a217SMatthew Barth Manager::Manager(const sdeventplus::Event& event) : 6448f44daaSMatthew Barth _bus(util::SDBusPlus::getBus()), _event(event), 653770a1daSMatthew Barth _mgr(util::SDBusPlus::getBus(), CONTROL_OBJPATH), _loadAllowed(true), 6648f44daaSMatthew Barth _powerState(std::make_unique<PGoodState>( 6748f44daaSMatthew Barth util::SDBusPlus::getBus(), 6848f44daaSMatthew Barth std::bind(std::mem_fn(&Manager::powerStateChanged), this, 6948f44daaSMatthew Barth std::placeholders::_1))) 703770a1daSMatthew Barth {} 71e91ac864SMatthew Barth 72e91ac864SMatthew Barth void Manager::sighupHandler(sdeventplus::source::Signal&, 73e91ac864SMatthew Barth const struct signalfd_siginfo*) 74e91ac864SMatthew Barth { 75e91ac864SMatthew Barth // Save current set of available and active profiles 76e91ac864SMatthew Barth std::map<configKey, std::unique_ptr<Profile>> profiles; 77e91ac864SMatthew Barth profiles.swap(_profiles); 78e91ac864SMatthew Barth std::vector<std::string> activeProfiles; 79e91ac864SMatthew Barth activeProfiles.swap(_activeProfiles); 80e91ac864SMatthew Barth 81e91ac864SMatthew Barth try 82e91ac864SMatthew Barth { 833770a1daSMatthew Barth _loadAllowed = true; 84e91ac864SMatthew Barth load(); 85e91ac864SMatthew Barth } 86ddb773b2SPatrick Williams catch (const std::runtime_error& re) 87e91ac864SMatthew Barth { 88e91ac864SMatthew Barth // Restore saved available and active profiles 893770a1daSMatthew Barth _loadAllowed = false; 90e91ac864SMatthew Barth _profiles.swap(profiles); 91e91ac864SMatthew Barth _activeProfiles.swap(activeProfiles); 92e91ac864SMatthew Barth log<level::ERR>("Error reloading configs, no changes made", 93e91ac864SMatthew Barth entry("LOAD_ERROR=%s", re.what())); 94e91ac864SMatthew Barth } 95e91ac864SMatthew Barth } 96e91ac864SMatthew Barth 972fc0a35dSMatt Spinler void Manager::sigUsr1Handler(sdeventplus::source::Signal&, 982fc0a35dSMatt Spinler const struct signalfd_siginfo*) 992fc0a35dSMatt Spinler { 100*7787def0SMatt Spinler debugDumpEventSource = std::make_unique<sdeventplus::source::Defer>( 101*7787def0SMatt Spinler _event, std::bind(std::mem_fn(&Manager::dumpDebugData), this, 1022fc0a35dSMatt Spinler std::placeholders::_1)); 1032fc0a35dSMatt Spinler } 1042fc0a35dSMatt Spinler 105*7787def0SMatt Spinler void Manager::dumpDebugData(sdeventplus::source::EventBase& /*source*/) 1062fc0a35dSMatt Spinler { 107*7787def0SMatt Spinler json data; 108*7787def0SMatt Spinler FlightRecorder::instance().dump(data); 109*7787def0SMatt Spinler 110*7787def0SMatt Spinler std::ofstream file{Manager::dumpFile}; 111*7787def0SMatt Spinler if (!file) 112*7787def0SMatt Spinler { 113*7787def0SMatt Spinler log<level::ERR>("Could not open file for fan dump"); 114*7787def0SMatt Spinler return; 115*7787def0SMatt Spinler } 116*7787def0SMatt Spinler 117*7787def0SMatt Spinler file << std::setw(4) << data; 118*7787def0SMatt Spinler 119*7787def0SMatt Spinler debugDumpEventSource.reset(); 1202fc0a35dSMatt Spinler } 1212fc0a35dSMatt Spinler 122e91ac864SMatthew Barth void Manager::load() 123e91ac864SMatthew Barth { 1243770a1daSMatthew Barth if (_loadAllowed) 1253770a1daSMatthew Barth { 126e91ac864SMatthew Barth // Load the available profiles and which are active 127acd737cdSMatthew Barth setProfiles(); 128acd737cdSMatthew Barth 129acd737cdSMatthew Barth // Load the zone configurations 130e91ac864SMatthew Barth auto zones = getConfig<Zone>(false, _event, this); 131de90fb4dSMatthew Barth // Load the fan configurations and move each fan into its zone 1329403a217SMatthew Barth auto fans = getConfig<Fan>(false); 133de90fb4dSMatthew Barth for (auto& fan : fans) 134de90fb4dSMatthew Barth { 1350206c728SMatthew Barth configKey fanProfile = 1360206c728SMatthew Barth std::make_pair(fan.second->getZone(), fan.first.second); 1370206c728SMatthew Barth auto itZone = std::find_if( 138e91ac864SMatthew Barth zones.begin(), zones.end(), [&fanProfile](const auto& zone) { 1390206c728SMatthew Barth return Manager::inConfig(fanProfile, zone.first); 140de90fb4dSMatthew Barth }); 141e91ac864SMatthew Barth if (itZone != zones.end()) 142de90fb4dSMatthew Barth { 1436f787309SMatthew Barth if (itZone->second->getTarget() != fan.second->getTarget() && 1446f787309SMatthew Barth fan.second->getTarget() != 0) 1456f787309SMatthew Barth { 146e91ac864SMatthew Barth // Update zone target to current target of the fan in the 147e91ac864SMatthew Barth // zone 1486f787309SMatthew Barth itZone->second->setTarget(fan.second->getTarget()); 1496f787309SMatthew Barth } 150de90fb4dSMatthew Barth itZone->second->addFan(std::move(fan.second)); 151de90fb4dSMatthew Barth } 152de90fb4dSMatthew Barth } 153e91ac864SMatthew Barth 1543695ac30SMatthew Barth // Save all currently available groups, if any, then clear for reloading 1553695ac30SMatthew Barth auto groups = std::move(Event::getAllGroups(false)); 1563695ac30SMatthew Barth Event::clearAllGroups(); 1573695ac30SMatthew Barth 1583695ac30SMatthew Barth std::map<configKey, std::unique_ptr<Event>> events; 1593695ac30SMatthew Barth try 1603695ac30SMatthew Barth { 1613695ac30SMatthew Barth // Load any events configured, including all the groups 1623695ac30SMatthew Barth events = getConfig<Event>(true, this, zones); 1633695ac30SMatthew Barth } 1643695ac30SMatthew Barth catch (const std::runtime_error& re) 1653695ac30SMatthew Barth { 1663695ac30SMatthew Barth // Restore saved set of all available groups for current events 1673695ac30SMatthew Barth Event::setAllGroups(std::move(groups)); 1683695ac30SMatthew Barth throw re; 1693695ac30SMatthew Barth } 170e91ac864SMatthew Barth 17114303a45SMatthew Barth // Enable zones 172e91ac864SMatthew Barth _zones = std::move(zones); 17314303a45SMatthew Barth std::for_each(_zones.begin(), _zones.end(), 17414303a45SMatthew Barth [](const auto& entry) { entry.second->enable(); }); 175b584d818SMatthew Barth 176e91ac864SMatthew Barth // Clear current timers and signal subscriptions before enabling events 1773770a1daSMatthew Barth // To save reloading services and/or objects into cache, do not clear 1783770a1daSMatthew Barth // cache 179e91ac864SMatthew Barth _timers.clear(); 180e91ac864SMatthew Barth _signals.clear(); 181e91ac864SMatthew Barth 182e91ac864SMatthew Barth // Enable events 183e91ac864SMatthew Barth _events = std::move(events); 18454b5a24fSMatthew Barth std::for_each(_events.begin(), _events.end(), 18554b5a24fSMatthew Barth [](const auto& entry) { entry.second->enable(); }); 1863770a1daSMatthew Barth 1873770a1daSMatthew Barth _loadAllowed = false; 1883770a1daSMatthew Barth } 18906764946SMatthew Barth } 190acd737cdSMatthew Barth 19148f44daaSMatthew Barth void Manager::powerStateChanged(bool powerStateOn) 19248f44daaSMatthew Barth { 19348f44daaSMatthew Barth if (powerStateOn) 19448f44daaSMatthew Barth { 1956a2418a2SMatthew Barth if (_zones.empty()) 1966a2418a2SMatthew Barth { 1976a2418a2SMatthew Barth throw std::runtime_error("No configured zones found at poweron"); 1986a2418a2SMatthew Barth } 19948f44daaSMatthew Barth std::for_each(_zones.begin(), _zones.end(), [](const auto& entry) { 20048f44daaSMatthew Barth entry.second->setTarget(entry.second->getPoweronTarget()); 20148f44daaSMatthew Barth }); 20248f44daaSMatthew Barth } 20348f44daaSMatthew Barth } 20448f44daaSMatthew Barth 205acd737cdSMatthew Barth const std::vector<std::string>& Manager::getActiveProfiles() 206acd737cdSMatthew Barth { 207acd737cdSMatthew Barth return _activeProfiles; 208a227a16dSMatthew Barth } 209a227a16dSMatthew Barth 2100206c728SMatthew Barth bool Manager::inConfig(const configKey& input, const configKey& comp) 2110206c728SMatthew Barth { 2120206c728SMatthew Barth // Config names dont match, do not include in config 2130206c728SMatthew Barth if (input.first != comp.first) 2140206c728SMatthew Barth { 2150206c728SMatthew Barth return false; 2160206c728SMatthew Barth } 2170206c728SMatthew Barth // No profiles specified by input config, can be used in any config 2180206c728SMatthew Barth if (input.second.empty()) 2190206c728SMatthew Barth { 2200206c728SMatthew Barth return true; 2210206c728SMatthew Barth } 2220206c728SMatthew Barth else 2230206c728SMatthew Barth { 2240206c728SMatthew Barth // Profiles must have one match in the other's profiles(and they must be 2250206c728SMatthew Barth // an active profile) to be used in the config 2260206c728SMatthew Barth return std::any_of( 2270206c728SMatthew Barth input.second.begin(), input.second.end(), 2280206c728SMatthew Barth [&comp](const auto& lProfile) { 2290206c728SMatthew Barth return std::any_of( 2300206c728SMatthew Barth comp.second.begin(), comp.second.end(), 2310206c728SMatthew Barth [&lProfile](const auto& rProfile) { 2320206c728SMatthew Barth if (lProfile != rProfile) 2330206c728SMatthew Barth { 2340206c728SMatthew Barth return false; 2350206c728SMatthew Barth } 2360206c728SMatthew Barth auto activeProfs = getActiveProfiles(); 2370206c728SMatthew Barth return std::find(activeProfs.begin(), activeProfs.end(), 2380206c728SMatthew Barth lProfile) != activeProfs.end(); 2390206c728SMatthew Barth }); 2400206c728SMatthew Barth }); 2410206c728SMatthew Barth } 2420206c728SMatthew Barth } 2430206c728SMatthew Barth 24412cb125aSMatthew Barth bool Manager::hasOwner(const std::string& path, const std::string& intf) 24512cb125aSMatthew Barth { 24612cb125aSMatthew Barth auto itServ = _servTree.find(path); 24712cb125aSMatthew Barth if (itServ == _servTree.end()) 24812cb125aSMatthew Barth { 24912cb125aSMatthew Barth // Path not found in cache, therefore owner missing 25012cb125aSMatthew Barth return false; 25112cb125aSMatthew Barth } 2524ca87faeSMatthew Barth for (const auto& service : itServ->second) 25312cb125aSMatthew Barth { 25412cb125aSMatthew Barth auto itIntf = std::find_if( 2554ca87faeSMatthew Barth service.second.second.begin(), service.second.second.end(), 25612cb125aSMatthew Barth [&intf](const auto& interface) { return intf == interface; }); 2574ca87faeSMatthew Barth if (itIntf != std::end(service.second.second)) 25812cb125aSMatthew Barth { 25912cb125aSMatthew Barth // Service found, return owner state 2604ca87faeSMatthew Barth return service.second.first; 26112cb125aSMatthew Barth } 26212cb125aSMatthew Barth } 26312cb125aSMatthew Barth // Interface not found in cache, therefore owner missing 26412cb125aSMatthew Barth return false; 26512cb125aSMatthew Barth } 26612cb125aSMatthew Barth 2674ca87faeSMatthew Barth void Manager::setOwner(const std::string& path, const std::string& serv, 2684ca87faeSMatthew Barth const std::string& intf, bool isOwned) 2694ca87faeSMatthew Barth { 2702a9e7b2eSMatthew Barth // Set owner state for specific object given 2712a9e7b2eSMatthew Barth auto& ownIntf = _servTree[path][serv]; 2722a9e7b2eSMatthew Barth ownIntf.first = isOwned; 2734ca87faeSMatthew Barth auto itIntf = std::find_if( 2742a9e7b2eSMatthew Barth ownIntf.second.begin(), ownIntf.second.end(), 2754ca87faeSMatthew Barth [&intf](const auto& interface) { return intf == interface; }); 2762a9e7b2eSMatthew Barth if (itIntf == std::end(ownIntf.second)) 2774ca87faeSMatthew Barth { 2782a9e7b2eSMatthew Barth ownIntf.second.emplace_back(intf); 2792a9e7b2eSMatthew Barth } 2802a9e7b2eSMatthew Barth 2812a9e7b2eSMatthew Barth // Update owner state on all entries of the same `serv` & `intf` 2822a9e7b2eSMatthew Barth for (auto& itPath : _servTree) 2834ca87faeSMatthew Barth { 2842a9e7b2eSMatthew Barth if (itPath.first == path) 2852a9e7b2eSMatthew Barth { 2862a9e7b2eSMatthew Barth // Already set/updated owner on this path for `serv` & `intf` 2872a9e7b2eSMatthew Barth continue; 2882a9e7b2eSMatthew Barth } 2892a9e7b2eSMatthew Barth for (auto& itServ : itPath.second) 2902a9e7b2eSMatthew Barth { 2912a9e7b2eSMatthew Barth if (itServ.first != serv) 2922a9e7b2eSMatthew Barth { 2932a9e7b2eSMatthew Barth continue; 2942a9e7b2eSMatthew Barth } 2952a9e7b2eSMatthew Barth auto itIntf = std::find_if( 2962a9e7b2eSMatthew Barth itServ.second.second.begin(), itServ.second.second.end(), 2972a9e7b2eSMatthew Barth [&intf](const auto& interface) { return intf == interface; }); 2982a9e7b2eSMatthew Barth if (itIntf != std::end(itServ.second.second)) 2992a9e7b2eSMatthew Barth { 3002a9e7b2eSMatthew Barth itServ.second.first = isOwned; 3014ca87faeSMatthew Barth } 3024ca87faeSMatthew Barth } 3034ca87faeSMatthew Barth } 3044ca87faeSMatthew Barth } 3054ca87faeSMatthew Barth 3064ca87faeSMatthew Barth const std::string& Manager::findService(const std::string& path, 3074ca87faeSMatthew Barth const std::string& intf) 3084ca87faeSMatthew Barth { 3094ca87faeSMatthew Barth static const std::string empty = ""; 3104ca87faeSMatthew Barth 3114ca87faeSMatthew Barth auto itServ = _servTree.find(path); 3124ca87faeSMatthew Barth if (itServ != _servTree.end()) 3134ca87faeSMatthew Barth { 3144ca87faeSMatthew Barth for (const auto& service : itServ->second) 3154ca87faeSMatthew Barth { 3164ca87faeSMatthew Barth auto itIntf = std::find_if( 3174ca87faeSMatthew Barth service.second.second.begin(), service.second.second.end(), 3184ca87faeSMatthew Barth [&intf](const auto& interface) { return intf == interface; }); 3194ca87faeSMatthew Barth if (itIntf != std::end(service.second.second)) 3204ca87faeSMatthew Barth { 3214ca87faeSMatthew Barth // Service found, return service name 3224ca87faeSMatthew Barth return service.first; 3234ca87faeSMatthew Barth } 3244ca87faeSMatthew Barth } 3254ca87faeSMatthew Barth } 3264ca87faeSMatthew Barth 3274ca87faeSMatthew Barth return empty; 3284ca87faeSMatthew Barth } 3294ca87faeSMatthew Barth 33098f6fc17SMatthew Barth void Manager::addServices(const std::string& intf, int32_t depth) 3314ca87faeSMatthew Barth { 3324ca87faeSMatthew Barth // Get all subtree objects for the given interface 33334835150SMatt Spinler auto objects = util::SDBusPlus::getSubTreeRaw(util::SDBusPlus::getBus(), 33434835150SMatt Spinler "/", intf, depth); 3354ca87faeSMatthew Barth // Add what's returned to the cache of path->services 3364ca87faeSMatthew Barth for (auto& itPath : objects) 3374ca87faeSMatthew Barth { 3384ca87faeSMatthew Barth auto pathIter = _servTree.find(itPath.first); 3394ca87faeSMatthew Barth if (pathIter != _servTree.end()) 3404ca87faeSMatthew Barth { 3414ca87faeSMatthew Barth // Path found in cache 3424ca87faeSMatthew Barth for (auto& itServ : itPath.second) 3434ca87faeSMatthew Barth { 3444ca87faeSMatthew Barth auto servIter = pathIter->second.find(itServ.first); 3454ca87faeSMatthew Barth if (servIter != pathIter->second.end()) 3464ca87faeSMatthew Barth { 3474ca87faeSMatthew Barth // Service found in cache 3484ca87faeSMatthew Barth for (auto& itIntf : itServ.second) 3494ca87faeSMatthew Barth { 3504ca87faeSMatthew Barth if (std::find(servIter->second.second.begin(), 3514ca87faeSMatthew Barth servIter->second.second.end(), 3524ca87faeSMatthew Barth itIntf) == servIter->second.second.end()) 3534ca87faeSMatthew Barth { 3544ca87faeSMatthew Barth // Add interface to cache 3554ca87faeSMatthew Barth servIter->second.second.emplace_back(itIntf); 3564ca87faeSMatthew Barth } 3574ca87faeSMatthew Barth } 3584ca87faeSMatthew Barth } 3594ca87faeSMatthew Barth else 3604ca87faeSMatthew Barth { 3614ca87faeSMatthew Barth // Service not found in cache 3624ca87faeSMatthew Barth auto intfs = {intf}; 3634ca87faeSMatthew Barth pathIter->second[itServ.first] = 3644ca87faeSMatthew Barth std::make_pair(true, intfs); 3654ca87faeSMatthew Barth } 3664ca87faeSMatthew Barth } 3674ca87faeSMatthew Barth } 3684ca87faeSMatthew Barth else 3694ca87faeSMatthew Barth { 3704ca87faeSMatthew Barth // Path not found in cache 3714ca87faeSMatthew Barth auto intfs = {intf}; 3724ca87faeSMatthew Barth _servTree[itPath.first] = { 3734ca87faeSMatthew Barth {itPath.second.begin()->first, std::make_pair(true, intfs)}}; 3744ca87faeSMatthew Barth } 3754ca87faeSMatthew Barth } 3764ca87faeSMatthew Barth } 3774ca87faeSMatthew Barth 3784ca87faeSMatthew Barth const std::string& Manager::getService(const std::string& path, 3794ca87faeSMatthew Barth const std::string& intf) 3804ca87faeSMatthew Barth { 3814ca87faeSMatthew Barth // Retrieve service from cache 3824ca87faeSMatthew Barth const auto& serviceName = findService(path, intf); 3834ca87faeSMatthew Barth if (serviceName.empty()) 3844ca87faeSMatthew Barth { 38598f6fc17SMatthew Barth addServices(intf, 0); 3864ca87faeSMatthew Barth return findService(path, intf); 3874ca87faeSMatthew Barth } 3884ca87faeSMatthew Barth 3894ca87faeSMatthew Barth return serviceName; 3904ca87faeSMatthew Barth } 3914ca87faeSMatthew Barth 392f41e947bSMatthew Barth std::vector<std::string> Manager::findPaths(const std::string& serv, 393f41e947bSMatthew Barth const std::string& intf) 394f41e947bSMatthew Barth { 395f41e947bSMatthew Barth std::vector<std::string> paths; 396f41e947bSMatthew Barth 397f41e947bSMatthew Barth for (const auto& path : _servTree) 398f41e947bSMatthew Barth { 399f41e947bSMatthew Barth auto itServ = path.second.find(serv); 400f41e947bSMatthew Barth if (itServ != path.second.end()) 401f41e947bSMatthew Barth { 402f41e947bSMatthew Barth if (std::find(itServ->second.second.begin(), 403f41e947bSMatthew Barth itServ->second.second.end(), 404f41e947bSMatthew Barth intf) != itServ->second.second.end()) 405f41e947bSMatthew Barth { 406f41e947bSMatthew Barth if (std::find(paths.begin(), paths.end(), path.first) == 407f41e947bSMatthew Barth paths.end()) 408f41e947bSMatthew Barth { 409f41e947bSMatthew Barth paths.push_back(path.first); 410f41e947bSMatthew Barth } 411f41e947bSMatthew Barth } 412f41e947bSMatthew Barth } 413f41e947bSMatthew Barth } 414f41e947bSMatthew Barth 415f41e947bSMatthew Barth return paths; 416f41e947bSMatthew Barth } 417f41e947bSMatthew Barth 418f41e947bSMatthew Barth std::vector<std::string> Manager::getPaths(const std::string& serv, 419f41e947bSMatthew Barth const std::string& intf) 420f41e947bSMatthew Barth { 421f41e947bSMatthew Barth auto paths = findPaths(serv, intf); 422f41e947bSMatthew Barth if (paths.empty()) 423f41e947bSMatthew Barth { 424f41e947bSMatthew Barth addServices(intf, 0); 425f41e947bSMatthew Barth return findPaths(serv, intf); 426f41e947bSMatthew Barth } 427f41e947bSMatthew Barth 428f41e947bSMatthew Barth return paths; 429f41e947bSMatthew Barth } 430f41e947bSMatthew Barth 431f41e947bSMatthew Barth void Manager::addObjects(const std::string& path, const std::string& intf, 432f41e947bSMatthew Barth const std::string& prop) 433f41e947bSMatthew Barth { 434f41e947bSMatthew Barth auto service = getService(path, intf); 435f41e947bSMatthew Barth if (service.empty()) 436f41e947bSMatthew Barth { 437f41e947bSMatthew Barth // Log service not found for object 43834835150SMatt Spinler log<level::DEBUG>( 439f41e947bSMatthew Barth fmt::format("Unable to get service name for path {}, interface {}", 440f41e947bSMatthew Barth path, intf) 441f41e947bSMatthew Barth .c_str()); 442f41e947bSMatthew Barth return; 443f41e947bSMatthew Barth } 444f41e947bSMatthew Barth 445f41e947bSMatthew Barth auto objMgrPaths = getPaths(service, "org.freedesktop.DBus.ObjectManager"); 446f41e947bSMatthew Barth if (objMgrPaths.empty()) 447f41e947bSMatthew Barth { 448f41e947bSMatthew Barth // No object manager interface provided by service? 449f41e947bSMatthew Barth // Attempt to retrieve property directly 450f41e947bSMatthew Barth auto variant = util::SDBusPlus::getPropertyVariant<PropertyVariantType>( 451f41e947bSMatthew Barth _bus, service, path, intf, prop); 452f41e947bSMatthew Barth _objects[path][intf][prop] = variant; 453f41e947bSMatthew Barth return; 454f41e947bSMatthew Barth } 455f41e947bSMatthew Barth 456f41e947bSMatthew Barth for (const auto& objMgrPath : objMgrPaths) 457f41e947bSMatthew Barth { 458f41e947bSMatthew Barth // Get all managed objects of service 459f41e947bSMatthew Barth auto objects = util::SDBusPlus::getManagedObjects<PropertyVariantType>( 460f41e947bSMatthew Barth _bus, service, objMgrPath); 461f41e947bSMatthew Barth 462f41e947bSMatthew Barth // Add what's returned to the cache of objects 463f41e947bSMatthew Barth for (auto& object : objects) 464f41e947bSMatthew Barth { 465f41e947bSMatthew Barth auto itPath = _objects.find(object.first); 466f41e947bSMatthew Barth if (itPath != _objects.end()) 467f41e947bSMatthew Barth { 468f41e947bSMatthew Barth // Path found in cache 469f41e947bSMatthew Barth for (auto& interface : itPath->second) 470f41e947bSMatthew Barth { 471f41e947bSMatthew Barth auto itIntf = itPath->second.find(interface.first); 472f41e947bSMatthew Barth if (itIntf != itPath->second.end()) 473f41e947bSMatthew Barth { 474f41e947bSMatthew Barth // Interface found in cache 475f41e947bSMatthew Barth for (auto& property : itIntf->second) 476f41e947bSMatthew Barth { 477f41e947bSMatthew Barth auto itProp = itIntf->second.find(property.first); 478f41e947bSMatthew Barth if (itProp != itIntf->second.end()) 479f41e947bSMatthew Barth { 480f41e947bSMatthew Barth // Property found, update value 481f41e947bSMatthew Barth itProp->second = property.second; 482f41e947bSMatthew Barth } 483f41e947bSMatthew Barth else 484f41e947bSMatthew Barth { 485f41e947bSMatthew Barth itIntf->second.insert(property); 486f41e947bSMatthew Barth } 487f41e947bSMatthew Barth } 488f41e947bSMatthew Barth } 489f41e947bSMatthew Barth else 490f41e947bSMatthew Barth { 491f41e947bSMatthew Barth // Interface not found in cache 492f41e947bSMatthew Barth itPath->second.insert(interface); 493f41e947bSMatthew Barth } 494f41e947bSMatthew Barth } 495f41e947bSMatthew Barth } 496f41e947bSMatthew Barth else 497f41e947bSMatthew Barth { 498f41e947bSMatthew Barth // Path not found in cache 499f41e947bSMatthew Barth _objects.insert(object); 500f41e947bSMatthew Barth } 501f41e947bSMatthew Barth } 502f41e947bSMatthew Barth } 503f41e947bSMatthew Barth } 504f41e947bSMatthew Barth 505f41e947bSMatthew Barth const std::optional<PropertyVariantType> 506f41e947bSMatthew Barth Manager::getProperty(const std::string& path, const std::string& intf, 507f41e947bSMatthew Barth const std::string& prop) 508f41e947bSMatthew Barth { 509f41e947bSMatthew Barth // TODO Objects hosted by fan control (i.e. ThermalMode) are required to 510f41e947bSMatthew Barth // update the cache upon being set/updated 511f41e947bSMatthew Barth auto itPath = _objects.find(path); 512f41e947bSMatthew Barth if (itPath != _objects.end()) 513f41e947bSMatthew Barth { 514f41e947bSMatthew Barth auto itIntf = itPath->second.find(intf); 515f41e947bSMatthew Barth if (itIntf != itPath->second.end()) 516f41e947bSMatthew Barth { 517f41e947bSMatthew Barth auto itProp = itIntf->second.find(prop); 518f41e947bSMatthew Barth if (itProp != itIntf->second.end()) 519f41e947bSMatthew Barth { 520f41e947bSMatthew Barth return itProp->second; 521f41e947bSMatthew Barth } 522f41e947bSMatthew Barth } 523f41e947bSMatthew Barth } 524f41e947bSMatthew Barth 525f41e947bSMatthew Barth return std::nullopt; 526f41e947bSMatthew Barth } 527f41e947bSMatthew Barth 528d9cb63b6SMatthew Barth void Manager::addTimer(const TimerType type, 529d9cb63b6SMatthew Barth const std::chrono::microseconds interval, 530d9cb63b6SMatthew Barth std::unique_ptr<TimerPkg> pkg) 531d9cb63b6SMatthew Barth { 532d9cb63b6SMatthew Barth auto dataPtr = 533d9cb63b6SMatthew Barth std::make_unique<TimerData>(std::make_pair(type, std::move(*pkg))); 534d9cb63b6SMatthew Barth Timer timer(_event, 535d9cb63b6SMatthew Barth std::bind(&Manager::timerExpired, this, std::ref(*dataPtr))); 536d9cb63b6SMatthew Barth if (type == TimerType::repeating) 537d9cb63b6SMatthew Barth { 538d9cb63b6SMatthew Barth timer.restart(interval); 539d9cb63b6SMatthew Barth } 540d9cb63b6SMatthew Barth else if (type == TimerType::oneshot) 541d9cb63b6SMatthew Barth { 542d9cb63b6SMatthew Barth timer.restartOnce(interval); 543d9cb63b6SMatthew Barth } 544d9cb63b6SMatthew Barth else 545d9cb63b6SMatthew Barth { 546d9cb63b6SMatthew Barth throw std::invalid_argument("Invalid Timer Type"); 547d9cb63b6SMatthew Barth } 548d9cb63b6SMatthew Barth _timers.emplace_back(std::move(dataPtr), std::move(timer)); 549d9cb63b6SMatthew Barth } 550d9cb63b6SMatthew Barth 551d9cb63b6SMatthew Barth void Manager::timerExpired(TimerData& data) 552d9cb63b6SMatthew Barth { 553d9cb63b6SMatthew Barth auto& actions = 554d9cb63b6SMatthew Barth std::get<std::vector<std::unique_ptr<ActionBase>>&>(data.second); 555d9cb63b6SMatthew Barth // Perform the actions in the timer data 556d9cb63b6SMatthew Barth std::for_each(actions.begin(), actions.end(), 55700f6aa09SMatthew Barth [](auto& action) { action->run(); }); 558d9cb63b6SMatthew Barth 559d9cb63b6SMatthew Barth // Remove oneshot timers after they expired 560d9cb63b6SMatthew Barth if (data.first == TimerType::oneshot) 561d9cb63b6SMatthew Barth { 562d9cb63b6SMatthew Barth auto itTimer = std::find_if( 563d9cb63b6SMatthew Barth _timers.begin(), _timers.end(), [&data](const auto& timer) { 564d9cb63b6SMatthew Barth return (data.first == timer.first->first && 565d9cb63b6SMatthew Barth (std::get<std::string>(data.second) == 566d9cb63b6SMatthew Barth std::get<std::string>(timer.first->second))); 567d9cb63b6SMatthew Barth }); 568d9cb63b6SMatthew Barth if (itTimer != std::end(_timers)) 569d9cb63b6SMatthew Barth { 570d9cb63b6SMatthew Barth _timers.erase(itTimer); 571d9cb63b6SMatthew Barth } 572d9cb63b6SMatthew Barth } 573d9cb63b6SMatthew Barth } 574d9cb63b6SMatthew Barth 575ebabc040SMatthew Barth void Manager::handleSignal(sdbusplus::message::message& msg, 576fac8a2feSMatthew Barth const std::vector<SignalPkg>& pkgs) 577ebabc040SMatthew Barth { 578fac8a2feSMatthew Barth for (auto& pkg : pkgs) 579ebabc040SMatthew Barth { 580ebabc040SMatthew Barth // Handle the signal callback and only run the actions if the handler 581ebabc040SMatthew Barth // updated the cache for the given SignalObject 582ebabc040SMatthew Barth if (std::get<SignalHandler>(pkg)(msg, std::get<SignalObject>(pkg), 583ebabc040SMatthew Barth *this)) 584ebabc040SMatthew Barth { 585ebabc040SMatthew Barth // Perform the actions in the handler package 586ebabc040SMatthew Barth auto& actions = std::get<SignalActions>(pkg); 587ebabc040SMatthew Barth std::for_each(actions.begin(), actions.end(), 588ebabc040SMatthew Barth [](auto& action) { action.get()->run(); }); 589ebabc040SMatthew Barth } 590ebabc040SMatthew Barth } 591ebabc040SMatthew Barth } 592ebabc040SMatthew Barth 593acd737cdSMatthew Barth void Manager::setProfiles() 594acd737cdSMatthew Barth { 595acd737cdSMatthew Barth // Profiles JSON config file is optional 596acd737cdSMatthew Barth auto confFile = fan::JsonConfig::getConfFile(_bus, confAppName, 597acd737cdSMatthew Barth Profile::confFileName, true); 598e91ac864SMatthew Barth 599e91ac864SMatthew Barth _profiles.clear(); 600acd737cdSMatthew Barth if (!confFile.empty()) 601acd737cdSMatthew Barth { 602acd737cdSMatthew Barth for (const auto& entry : fan::JsonConfig::load(confFile)) 603acd737cdSMatthew Barth { 604acd737cdSMatthew Barth auto obj = std::make_unique<Profile>(entry); 605acd737cdSMatthew Barth _profiles.emplace( 606acd737cdSMatthew Barth std::make_pair(obj->getName(), obj->getProfiles()), 607acd737cdSMatthew Barth std::move(obj)); 608acd737cdSMatthew Barth } 609acd737cdSMatthew Barth } 610e91ac864SMatthew Barth 611acd737cdSMatthew Barth // Ensure all configurations use the same set of active profiles 612acd737cdSMatthew Barth // (In case a profile's active state changes during configuration) 613e91ac864SMatthew Barth _activeProfiles.clear(); 614acd737cdSMatthew Barth for (const auto& profile : _profiles) 615acd737cdSMatthew Barth { 616acd737cdSMatthew Barth if (profile.second->isActive()) 617acd737cdSMatthew Barth { 618acd737cdSMatthew Barth _activeProfiles.emplace_back(profile.first.first); 619acd737cdSMatthew Barth } 620acd737cdSMatthew Barth } 621acd737cdSMatthew Barth } 622acd737cdSMatthew Barth 623a227a16dSMatthew Barth } // namespace phosphor::fan::control::json 624