xref: /openbmc/phosphor-fan-presence/control/json/manager.cpp (revision 6a2418a23ae68d4420a9d28e2fd6af6a88ccecd4)
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 
20d9cb63b6SMatthew Barth #include "action.hpp"
2144ab7693SMatthew Barth #include "event.hpp"
22de90fb4dSMatthew Barth #include "fan.hpp"
23d9cb63b6SMatthew Barth #include "group.hpp"
24a227a16dSMatthew Barth #include "json_config.hpp"
2548f44daaSMatthew Barth #include "power_state.hpp"
2606764946SMatthew Barth #include "profile.hpp"
279403a217SMatthew Barth #include "sdbusplus.hpp"
28acd737cdSMatthew Barth #include "zone.hpp"
29a227a16dSMatthew Barth 
30acd737cdSMatthew Barth #include <nlohmann/json.hpp>
31a227a16dSMatthew Barth #include <sdbusplus/bus.hpp>
321542fb5aSMatthew Barth #include <sdbusplus/server/manager.hpp>
33acd737cdSMatthew Barth #include <sdeventplus/event.hpp>
34d9cb63b6SMatthew Barth #include <sdeventplus/utility/timer.hpp>
35a227a16dSMatthew Barth 
36de90fb4dSMatthew Barth #include <algorithm>
37d9cb63b6SMatthew Barth #include <chrono>
38a227a16dSMatthew Barth #include <filesystem>
39d9cb63b6SMatthew Barth #include <functional>
40d9cb63b6SMatthew Barth #include <map>
41d9cb63b6SMatthew Barth #include <memory>
42d9cb63b6SMatthew Barth #include <tuple>
43d9cb63b6SMatthew Barth #include <utility>
4406764946SMatthew Barth #include <vector>
45a227a16dSMatthew Barth 
46a227a16dSMatthew Barth namespace phosphor::fan::control::json
47a227a16dSMatthew Barth {
48a227a16dSMatthew Barth 
49acd737cdSMatthew Barth using json = nlohmann::json;
50acd737cdSMatthew Barth 
51acd737cdSMatthew Barth std::vector<std::string> Manager::_activeProfiles;
5212cb125aSMatthew Barth std::map<std::string,
534ca87faeSMatthew Barth          std::map<std::string, std::pair<bool, std::vector<std::string>>>>
5412cb125aSMatthew Barth     Manager::_servTree;
5507fecfc6SMatthew Barth std::map<std::string,
5607fecfc6SMatthew Barth          std::map<std::string, std::map<std::string, PropertyVariantType>>>
5707fecfc6SMatthew Barth     Manager::_objects;
58d76351bdSMatt Spinler std::unordered_map<std::string, PropertyVariantType> Manager::_parameters;
59acd737cdSMatthew Barth 
609403a217SMatthew Barth Manager::Manager(const sdeventplus::Event& event) :
6148f44daaSMatthew Barth     _bus(util::SDBusPlus::getBus()), _event(event),
623770a1daSMatthew Barth     _mgr(util::SDBusPlus::getBus(), CONTROL_OBJPATH), _loadAllowed(true),
6348f44daaSMatthew Barth     _powerState(std::make_unique<PGoodState>(
6448f44daaSMatthew Barth         util::SDBusPlus::getBus(),
6548f44daaSMatthew Barth         std::bind(std::mem_fn(&Manager::powerStateChanged), this,
6648f44daaSMatthew Barth                   std::placeholders::_1)))
673770a1daSMatthew Barth {}
68e91ac864SMatthew Barth 
69e91ac864SMatthew Barth void Manager::sighupHandler(sdeventplus::source::Signal&,
70e91ac864SMatthew Barth                             const struct signalfd_siginfo*)
71e91ac864SMatthew Barth {
72e91ac864SMatthew Barth     // Save current set of available and active profiles
73e91ac864SMatthew Barth     std::map<configKey, std::unique_ptr<Profile>> profiles;
74e91ac864SMatthew Barth     profiles.swap(_profiles);
75e91ac864SMatthew Barth     std::vector<std::string> activeProfiles;
76e91ac864SMatthew Barth     activeProfiles.swap(_activeProfiles);
77e91ac864SMatthew Barth 
78e91ac864SMatthew Barth     try
79e91ac864SMatthew Barth     {
803770a1daSMatthew Barth         _loadAllowed = true;
81e91ac864SMatthew Barth         load();
82e91ac864SMatthew Barth     }
83e91ac864SMatthew Barth     catch (std::runtime_error& re)
84e91ac864SMatthew Barth     {
85e91ac864SMatthew Barth         // Restore saved available and active profiles
863770a1daSMatthew Barth         _loadAllowed = false;
87e91ac864SMatthew Barth         _profiles.swap(profiles);
88e91ac864SMatthew Barth         _activeProfiles.swap(activeProfiles);
89e91ac864SMatthew Barth         log<level::ERR>("Error reloading configs, no changes made",
90e91ac864SMatthew Barth                         entry("LOAD_ERROR=%s", re.what()));
91e91ac864SMatthew Barth     }
92e91ac864SMatthew Barth }
93e91ac864SMatthew Barth 
94e91ac864SMatthew Barth void Manager::load()
95e91ac864SMatthew Barth {
963770a1daSMatthew Barth     if (_loadAllowed)
973770a1daSMatthew Barth     {
98e91ac864SMatthew Barth         // Load the available profiles and which are active
99acd737cdSMatthew Barth         setProfiles();
100acd737cdSMatthew Barth 
101acd737cdSMatthew Barth         // Load the zone configurations
102e91ac864SMatthew Barth         auto zones = getConfig<Zone>(false, _event, this);
103de90fb4dSMatthew Barth         // Load the fan configurations and move each fan into its zone
1049403a217SMatthew Barth         auto fans = getConfig<Fan>(false);
105de90fb4dSMatthew Barth         for (auto& fan : fans)
106de90fb4dSMatthew Barth         {
1070206c728SMatthew Barth             configKey fanProfile =
1080206c728SMatthew Barth                 std::make_pair(fan.second->getZone(), fan.first.second);
1090206c728SMatthew Barth             auto itZone = std::find_if(
110e91ac864SMatthew Barth                 zones.begin(), zones.end(), [&fanProfile](const auto& zone) {
1110206c728SMatthew Barth                     return Manager::inConfig(fanProfile, zone.first);
112de90fb4dSMatthew Barth                 });
113e91ac864SMatthew Barth             if (itZone != zones.end())
114de90fb4dSMatthew Barth             {
1156f787309SMatthew Barth                 if (itZone->second->getTarget() != fan.second->getTarget() &&
1166f787309SMatthew Barth                     fan.second->getTarget() != 0)
1176f787309SMatthew Barth                 {
118e91ac864SMatthew Barth                     // Update zone target to current target of the fan in the
119e91ac864SMatthew Barth                     // zone
1206f787309SMatthew Barth                     itZone->second->setTarget(fan.second->getTarget());
1216f787309SMatthew Barth                 }
122de90fb4dSMatthew Barth                 itZone->second->addFan(std::move(fan.second));
123de90fb4dSMatthew Barth             }
124de90fb4dSMatthew Barth         }
125e91ac864SMatthew Barth 
126e91ac864SMatthew Barth         // Load any events configured
127e91ac864SMatthew Barth         auto events = getConfig<Event>(true, this, zones);
128e91ac864SMatthew Barth 
12914303a45SMatthew Barth         // Enable zones
130e91ac864SMatthew Barth         _zones = std::move(zones);
13114303a45SMatthew Barth         std::for_each(_zones.begin(), _zones.end(),
13214303a45SMatthew Barth                       [](const auto& entry) { entry.second->enable(); });
133b584d818SMatthew Barth 
134e91ac864SMatthew Barth         // Clear current timers and signal subscriptions before enabling events
1353770a1daSMatthew Barth         // To save reloading services and/or objects into cache, do not clear
1363770a1daSMatthew Barth         // cache
137e91ac864SMatthew Barth         _timers.clear();
138e91ac864SMatthew Barth         _signals.clear();
139e91ac864SMatthew Barth 
140e91ac864SMatthew Barth         // Enable events
141e91ac864SMatthew Barth         _events = std::move(events);
14254b5a24fSMatthew Barth         std::for_each(_events.begin(), _events.end(),
14354b5a24fSMatthew Barth                       [](const auto& entry) { entry.second->enable(); });
1443770a1daSMatthew Barth 
1453770a1daSMatthew Barth         _loadAllowed = false;
1463770a1daSMatthew Barth     }
14706764946SMatthew Barth }
148acd737cdSMatthew Barth 
14948f44daaSMatthew Barth void Manager::powerStateChanged(bool powerStateOn)
15048f44daaSMatthew Barth {
15148f44daaSMatthew Barth     if (powerStateOn)
15248f44daaSMatthew Barth     {
153*6a2418a2SMatthew Barth         if (_zones.empty())
154*6a2418a2SMatthew Barth         {
155*6a2418a2SMatthew Barth             throw std::runtime_error("No configured zones found at poweron");
156*6a2418a2SMatthew Barth         }
15748f44daaSMatthew Barth         std::for_each(_zones.begin(), _zones.end(), [](const auto& entry) {
15848f44daaSMatthew Barth             entry.second->setTarget(entry.second->getPoweronTarget());
15948f44daaSMatthew Barth         });
16048f44daaSMatthew Barth     }
16148f44daaSMatthew Barth }
16248f44daaSMatthew Barth 
163acd737cdSMatthew Barth const std::vector<std::string>& Manager::getActiveProfiles()
164acd737cdSMatthew Barth {
165acd737cdSMatthew Barth     return _activeProfiles;
166a227a16dSMatthew Barth }
167a227a16dSMatthew Barth 
1680206c728SMatthew Barth bool Manager::inConfig(const configKey& input, const configKey& comp)
1690206c728SMatthew Barth {
1700206c728SMatthew Barth     // Config names dont match, do not include in config
1710206c728SMatthew Barth     if (input.first != comp.first)
1720206c728SMatthew Barth     {
1730206c728SMatthew Barth         return false;
1740206c728SMatthew Barth     }
1750206c728SMatthew Barth     // No profiles specified by input config, can be used in any config
1760206c728SMatthew Barth     if (input.second.empty())
1770206c728SMatthew Barth     {
1780206c728SMatthew Barth         return true;
1790206c728SMatthew Barth     }
1800206c728SMatthew Barth     else
1810206c728SMatthew Barth     {
1820206c728SMatthew Barth         // Profiles must have one match in the other's profiles(and they must be
1830206c728SMatthew Barth         // an active profile) to be used in the config
1840206c728SMatthew Barth         return std::any_of(
1850206c728SMatthew Barth             input.second.begin(), input.second.end(),
1860206c728SMatthew Barth             [&comp](const auto& lProfile) {
1870206c728SMatthew Barth                 return std::any_of(
1880206c728SMatthew Barth                     comp.second.begin(), comp.second.end(),
1890206c728SMatthew Barth                     [&lProfile](const auto& rProfile) {
1900206c728SMatthew Barth                         if (lProfile != rProfile)
1910206c728SMatthew Barth                         {
1920206c728SMatthew Barth                             return false;
1930206c728SMatthew Barth                         }
1940206c728SMatthew Barth                         auto activeProfs = getActiveProfiles();
1950206c728SMatthew Barth                         return std::find(activeProfs.begin(), activeProfs.end(),
1960206c728SMatthew Barth                                          lProfile) != activeProfs.end();
1970206c728SMatthew Barth                     });
1980206c728SMatthew Barth             });
1990206c728SMatthew Barth     }
2000206c728SMatthew Barth }
2010206c728SMatthew Barth 
20212cb125aSMatthew Barth bool Manager::hasOwner(const std::string& path, const std::string& intf)
20312cb125aSMatthew Barth {
20412cb125aSMatthew Barth     auto itServ = _servTree.find(path);
20512cb125aSMatthew Barth     if (itServ == _servTree.end())
20612cb125aSMatthew Barth     {
20712cb125aSMatthew Barth         // Path not found in cache, therefore owner missing
20812cb125aSMatthew Barth         return false;
20912cb125aSMatthew Barth     }
2104ca87faeSMatthew Barth     for (const auto& service : itServ->second)
21112cb125aSMatthew Barth     {
21212cb125aSMatthew Barth         auto itIntf = std::find_if(
2134ca87faeSMatthew Barth             service.second.second.begin(), service.second.second.end(),
21412cb125aSMatthew Barth             [&intf](const auto& interface) { return intf == interface; });
2154ca87faeSMatthew Barth         if (itIntf != std::end(service.second.second))
21612cb125aSMatthew Barth         {
21712cb125aSMatthew Barth             // Service found, return owner state
2184ca87faeSMatthew Barth             return service.second.first;
21912cb125aSMatthew Barth         }
22012cb125aSMatthew Barth     }
22112cb125aSMatthew Barth     // Interface not found in cache, therefore owner missing
22212cb125aSMatthew Barth     return false;
22312cb125aSMatthew Barth }
22412cb125aSMatthew Barth 
2254ca87faeSMatthew Barth void Manager::setOwner(const std::string& path, const std::string& serv,
2264ca87faeSMatthew Barth                        const std::string& intf, bool isOwned)
2274ca87faeSMatthew Barth {
2282a9e7b2eSMatthew Barth     // Set owner state for specific object given
2292a9e7b2eSMatthew Barth     auto& ownIntf = _servTree[path][serv];
2302a9e7b2eSMatthew Barth     ownIntf.first = isOwned;
2314ca87faeSMatthew Barth     auto itIntf = std::find_if(
2322a9e7b2eSMatthew Barth         ownIntf.second.begin(), ownIntf.second.end(),
2334ca87faeSMatthew Barth         [&intf](const auto& interface) { return intf == interface; });
2342a9e7b2eSMatthew Barth     if (itIntf == std::end(ownIntf.second))
2354ca87faeSMatthew Barth     {
2362a9e7b2eSMatthew Barth         ownIntf.second.emplace_back(intf);
2372a9e7b2eSMatthew Barth     }
2382a9e7b2eSMatthew Barth 
2392a9e7b2eSMatthew Barth     // Update owner state on all entries of the same `serv` & `intf`
2402a9e7b2eSMatthew Barth     for (auto& itPath : _servTree)
2414ca87faeSMatthew Barth     {
2422a9e7b2eSMatthew Barth         if (itPath.first == path)
2432a9e7b2eSMatthew Barth         {
2442a9e7b2eSMatthew Barth             // Already set/updated owner on this path for `serv` & `intf`
2452a9e7b2eSMatthew Barth             continue;
2462a9e7b2eSMatthew Barth         }
2472a9e7b2eSMatthew Barth         for (auto& itServ : itPath.second)
2482a9e7b2eSMatthew Barth         {
2492a9e7b2eSMatthew Barth             if (itServ.first != serv)
2502a9e7b2eSMatthew Barth             {
2512a9e7b2eSMatthew Barth                 continue;
2522a9e7b2eSMatthew Barth             }
2532a9e7b2eSMatthew Barth             auto itIntf = std::find_if(
2542a9e7b2eSMatthew Barth                 itServ.second.second.begin(), itServ.second.second.end(),
2552a9e7b2eSMatthew Barth                 [&intf](const auto& interface) { return intf == interface; });
2562a9e7b2eSMatthew Barth             if (itIntf != std::end(itServ.second.second))
2572a9e7b2eSMatthew Barth             {
2582a9e7b2eSMatthew Barth                 itServ.second.first = isOwned;
2594ca87faeSMatthew Barth             }
2604ca87faeSMatthew Barth         }
2614ca87faeSMatthew Barth     }
2624ca87faeSMatthew Barth }
2634ca87faeSMatthew Barth 
2644ca87faeSMatthew Barth const std::string& Manager::findService(const std::string& path,
2654ca87faeSMatthew Barth                                         const std::string& intf)
2664ca87faeSMatthew Barth {
2674ca87faeSMatthew Barth     static const std::string empty = "";
2684ca87faeSMatthew Barth 
2694ca87faeSMatthew Barth     auto itServ = _servTree.find(path);
2704ca87faeSMatthew Barth     if (itServ != _servTree.end())
2714ca87faeSMatthew Barth     {
2724ca87faeSMatthew Barth         for (const auto& service : itServ->second)
2734ca87faeSMatthew Barth         {
2744ca87faeSMatthew Barth             auto itIntf = std::find_if(
2754ca87faeSMatthew Barth                 service.second.second.begin(), service.second.second.end(),
2764ca87faeSMatthew Barth                 [&intf](const auto& interface) { return intf == interface; });
2774ca87faeSMatthew Barth             if (itIntf != std::end(service.second.second))
2784ca87faeSMatthew Barth             {
2794ca87faeSMatthew Barth                 // Service found, return service name
2804ca87faeSMatthew Barth                 return service.first;
2814ca87faeSMatthew Barth             }
2824ca87faeSMatthew Barth         }
2834ca87faeSMatthew Barth     }
2844ca87faeSMatthew Barth 
2854ca87faeSMatthew Barth     return empty;
2864ca87faeSMatthew Barth }
2874ca87faeSMatthew Barth 
28898f6fc17SMatthew Barth void Manager::addServices(const std::string& intf, int32_t depth)
2894ca87faeSMatthew Barth {
2904ca87faeSMatthew Barth     // Get all subtree objects for the given interface
29134835150SMatt Spinler     auto objects = util::SDBusPlus::getSubTreeRaw(util::SDBusPlus::getBus(),
29234835150SMatt Spinler                                                   "/", intf, depth);
2934ca87faeSMatthew Barth     // Add what's returned to the cache of path->services
2944ca87faeSMatthew Barth     for (auto& itPath : objects)
2954ca87faeSMatthew Barth     {
2964ca87faeSMatthew Barth         auto pathIter = _servTree.find(itPath.first);
2974ca87faeSMatthew Barth         if (pathIter != _servTree.end())
2984ca87faeSMatthew Barth         {
2994ca87faeSMatthew Barth             // Path found in cache
3004ca87faeSMatthew Barth             for (auto& itServ : itPath.second)
3014ca87faeSMatthew Barth             {
3024ca87faeSMatthew Barth                 auto servIter = pathIter->second.find(itServ.first);
3034ca87faeSMatthew Barth                 if (servIter != pathIter->second.end())
3044ca87faeSMatthew Barth                 {
3054ca87faeSMatthew Barth                     // Service found in cache
3064ca87faeSMatthew Barth                     for (auto& itIntf : itServ.second)
3074ca87faeSMatthew Barth                     {
3084ca87faeSMatthew Barth                         if (std::find(servIter->second.second.begin(),
3094ca87faeSMatthew Barth                                       servIter->second.second.end(),
3104ca87faeSMatthew Barth                                       itIntf) == servIter->second.second.end())
3114ca87faeSMatthew Barth                         {
3124ca87faeSMatthew Barth                             // Add interface to cache
3134ca87faeSMatthew Barth                             servIter->second.second.emplace_back(itIntf);
3144ca87faeSMatthew Barth                         }
3154ca87faeSMatthew Barth                     }
3164ca87faeSMatthew Barth                 }
3174ca87faeSMatthew Barth                 else
3184ca87faeSMatthew Barth                 {
3194ca87faeSMatthew Barth                     // Service not found in cache
3204ca87faeSMatthew Barth                     auto intfs = {intf};
3214ca87faeSMatthew Barth                     pathIter->second[itServ.first] =
3224ca87faeSMatthew Barth                         std::make_pair(true, intfs);
3234ca87faeSMatthew Barth                 }
3244ca87faeSMatthew Barth             }
3254ca87faeSMatthew Barth         }
3264ca87faeSMatthew Barth         else
3274ca87faeSMatthew Barth         {
3284ca87faeSMatthew Barth             // Path not found in cache
3294ca87faeSMatthew Barth             auto intfs = {intf};
3304ca87faeSMatthew Barth             _servTree[itPath.first] = {
3314ca87faeSMatthew Barth                 {itPath.second.begin()->first, std::make_pair(true, intfs)}};
3324ca87faeSMatthew Barth         }
3334ca87faeSMatthew Barth     }
3344ca87faeSMatthew Barth }
3354ca87faeSMatthew Barth 
3364ca87faeSMatthew Barth const std::string& Manager::getService(const std::string& path,
3374ca87faeSMatthew Barth                                        const std::string& intf)
3384ca87faeSMatthew Barth {
3394ca87faeSMatthew Barth     // Retrieve service from cache
3404ca87faeSMatthew Barth     const auto& serviceName = findService(path, intf);
3414ca87faeSMatthew Barth     if (serviceName.empty())
3424ca87faeSMatthew Barth     {
34398f6fc17SMatthew Barth         addServices(intf, 0);
3444ca87faeSMatthew Barth         return findService(path, intf);
3454ca87faeSMatthew Barth     }
3464ca87faeSMatthew Barth 
3474ca87faeSMatthew Barth     return serviceName;
3484ca87faeSMatthew Barth }
3494ca87faeSMatthew Barth 
350f41e947bSMatthew Barth std::vector<std::string> Manager::findPaths(const std::string& serv,
351f41e947bSMatthew Barth                                             const std::string& intf)
352f41e947bSMatthew Barth {
353f41e947bSMatthew Barth     std::vector<std::string> paths;
354f41e947bSMatthew Barth 
355f41e947bSMatthew Barth     for (const auto& path : _servTree)
356f41e947bSMatthew Barth     {
357f41e947bSMatthew Barth         auto itServ = path.second.find(serv);
358f41e947bSMatthew Barth         if (itServ != path.second.end())
359f41e947bSMatthew Barth         {
360f41e947bSMatthew Barth             if (std::find(itServ->second.second.begin(),
361f41e947bSMatthew Barth                           itServ->second.second.end(),
362f41e947bSMatthew Barth                           intf) != itServ->second.second.end())
363f41e947bSMatthew Barth             {
364f41e947bSMatthew Barth                 if (std::find(paths.begin(), paths.end(), path.first) ==
365f41e947bSMatthew Barth                     paths.end())
366f41e947bSMatthew Barth                 {
367f41e947bSMatthew Barth                     paths.push_back(path.first);
368f41e947bSMatthew Barth                 }
369f41e947bSMatthew Barth             }
370f41e947bSMatthew Barth         }
371f41e947bSMatthew Barth     }
372f41e947bSMatthew Barth 
373f41e947bSMatthew Barth     return paths;
374f41e947bSMatthew Barth }
375f41e947bSMatthew Barth 
376f41e947bSMatthew Barth std::vector<std::string> Manager::getPaths(const std::string& serv,
377f41e947bSMatthew Barth                                            const std::string& intf)
378f41e947bSMatthew Barth {
379f41e947bSMatthew Barth     auto paths = findPaths(serv, intf);
380f41e947bSMatthew Barth     if (paths.empty())
381f41e947bSMatthew Barth     {
382f41e947bSMatthew Barth         addServices(intf, 0);
383f41e947bSMatthew Barth         return findPaths(serv, intf);
384f41e947bSMatthew Barth     }
385f41e947bSMatthew Barth 
386f41e947bSMatthew Barth     return paths;
387f41e947bSMatthew Barth }
388f41e947bSMatthew Barth 
389f41e947bSMatthew Barth void Manager::addObjects(const std::string& path, const std::string& intf,
390f41e947bSMatthew Barth                          const std::string& prop)
391f41e947bSMatthew Barth {
392f41e947bSMatthew Barth     auto service = getService(path, intf);
393f41e947bSMatthew Barth     if (service.empty())
394f41e947bSMatthew Barth     {
395f41e947bSMatthew Barth         // Log service not found for object
39634835150SMatt Spinler         log<level::DEBUG>(
397f41e947bSMatthew Barth             fmt::format("Unable to get service name for path {}, interface {}",
398f41e947bSMatthew Barth                         path, intf)
399f41e947bSMatthew Barth                 .c_str());
400f41e947bSMatthew Barth         return;
401f41e947bSMatthew Barth     }
402f41e947bSMatthew Barth 
403f41e947bSMatthew Barth     auto objMgrPaths = getPaths(service, "org.freedesktop.DBus.ObjectManager");
404f41e947bSMatthew Barth     if (objMgrPaths.empty())
405f41e947bSMatthew Barth     {
406f41e947bSMatthew Barth         // No object manager interface provided by service?
407f41e947bSMatthew Barth         // Attempt to retrieve property directly
408f41e947bSMatthew Barth         auto variant = util::SDBusPlus::getPropertyVariant<PropertyVariantType>(
409f41e947bSMatthew Barth             _bus, service, path, intf, prop);
410f41e947bSMatthew Barth         _objects[path][intf][prop] = variant;
411f41e947bSMatthew Barth         return;
412f41e947bSMatthew Barth     }
413f41e947bSMatthew Barth 
414f41e947bSMatthew Barth     for (const auto& objMgrPath : objMgrPaths)
415f41e947bSMatthew Barth     {
416f41e947bSMatthew Barth         // Get all managed objects of service
417f41e947bSMatthew Barth         auto objects = util::SDBusPlus::getManagedObjects<PropertyVariantType>(
418f41e947bSMatthew Barth             _bus, service, objMgrPath);
419f41e947bSMatthew Barth 
420f41e947bSMatthew Barth         // Add what's returned to the cache of objects
421f41e947bSMatthew Barth         for (auto& object : objects)
422f41e947bSMatthew Barth         {
423f41e947bSMatthew Barth             auto itPath = _objects.find(object.first);
424f41e947bSMatthew Barth             if (itPath != _objects.end())
425f41e947bSMatthew Barth             {
426f41e947bSMatthew Barth                 // Path found in cache
427f41e947bSMatthew Barth                 for (auto& interface : itPath->second)
428f41e947bSMatthew Barth                 {
429f41e947bSMatthew Barth                     auto itIntf = itPath->second.find(interface.first);
430f41e947bSMatthew Barth                     if (itIntf != itPath->second.end())
431f41e947bSMatthew Barth                     {
432f41e947bSMatthew Barth                         // Interface found in cache
433f41e947bSMatthew Barth                         for (auto& property : itIntf->second)
434f41e947bSMatthew Barth                         {
435f41e947bSMatthew Barth                             auto itProp = itIntf->second.find(property.first);
436f41e947bSMatthew Barth                             if (itProp != itIntf->second.end())
437f41e947bSMatthew Barth                             {
438f41e947bSMatthew Barth                                 // Property found, update value
439f41e947bSMatthew Barth                                 itProp->second = property.second;
440f41e947bSMatthew Barth                             }
441f41e947bSMatthew Barth                             else
442f41e947bSMatthew Barth                             {
443f41e947bSMatthew Barth                                 itIntf->second.insert(property);
444f41e947bSMatthew Barth                             }
445f41e947bSMatthew Barth                         }
446f41e947bSMatthew Barth                     }
447f41e947bSMatthew Barth                     else
448f41e947bSMatthew Barth                     {
449f41e947bSMatthew Barth                         // Interface not found in cache
450f41e947bSMatthew Barth                         itPath->second.insert(interface);
451f41e947bSMatthew Barth                     }
452f41e947bSMatthew Barth                 }
453f41e947bSMatthew Barth             }
454f41e947bSMatthew Barth             else
455f41e947bSMatthew Barth             {
456f41e947bSMatthew Barth                 // Path not found in cache
457f41e947bSMatthew Barth                 _objects.insert(object);
458f41e947bSMatthew Barth             }
459f41e947bSMatthew Barth         }
460f41e947bSMatthew Barth     }
461f41e947bSMatthew Barth }
462f41e947bSMatthew Barth 
463f41e947bSMatthew Barth const std::optional<PropertyVariantType>
464f41e947bSMatthew Barth     Manager::getProperty(const std::string& path, const std::string& intf,
465f41e947bSMatthew Barth                          const std::string& prop)
466f41e947bSMatthew Barth {
467f41e947bSMatthew Barth     // TODO Objects hosted by fan control (i.e. ThermalMode) are required to
468f41e947bSMatthew Barth     // update the cache upon being set/updated
469f41e947bSMatthew Barth     auto itPath = _objects.find(path);
470f41e947bSMatthew Barth     if (itPath != _objects.end())
471f41e947bSMatthew Barth     {
472f41e947bSMatthew Barth         auto itIntf = itPath->second.find(intf);
473f41e947bSMatthew Barth         if (itIntf != itPath->second.end())
474f41e947bSMatthew Barth         {
475f41e947bSMatthew Barth             auto itProp = itIntf->second.find(prop);
476f41e947bSMatthew Barth             if (itProp != itIntf->second.end())
477f41e947bSMatthew Barth             {
478f41e947bSMatthew Barth                 return itProp->second;
479f41e947bSMatthew Barth             }
480f41e947bSMatthew Barth         }
481f41e947bSMatthew Barth     }
482f41e947bSMatthew Barth 
483f41e947bSMatthew Barth     return std::nullopt;
484f41e947bSMatthew Barth }
485f41e947bSMatthew Barth 
486d9cb63b6SMatthew Barth void Manager::addTimer(const TimerType type,
487d9cb63b6SMatthew Barth                        const std::chrono::microseconds interval,
488d9cb63b6SMatthew Barth                        std::unique_ptr<TimerPkg> pkg)
489d9cb63b6SMatthew Barth {
490d9cb63b6SMatthew Barth     auto dataPtr =
491d9cb63b6SMatthew Barth         std::make_unique<TimerData>(std::make_pair(type, std::move(*pkg)));
492d9cb63b6SMatthew Barth     Timer timer(_event,
493d9cb63b6SMatthew Barth                 std::bind(&Manager::timerExpired, this, std::ref(*dataPtr)));
494d9cb63b6SMatthew Barth     if (type == TimerType::repeating)
495d9cb63b6SMatthew Barth     {
496d9cb63b6SMatthew Barth         timer.restart(interval);
497d9cb63b6SMatthew Barth     }
498d9cb63b6SMatthew Barth     else if (type == TimerType::oneshot)
499d9cb63b6SMatthew Barth     {
500d9cb63b6SMatthew Barth         timer.restartOnce(interval);
501d9cb63b6SMatthew Barth     }
502d9cb63b6SMatthew Barth     else
503d9cb63b6SMatthew Barth     {
504d9cb63b6SMatthew Barth         throw std::invalid_argument("Invalid Timer Type");
505d9cb63b6SMatthew Barth     }
506d9cb63b6SMatthew Barth     _timers.emplace_back(std::move(dataPtr), std::move(timer));
507d9cb63b6SMatthew Barth }
508d9cb63b6SMatthew Barth 
509d9cb63b6SMatthew Barth void Manager::timerExpired(TimerData& data)
510d9cb63b6SMatthew Barth {
511d9cb63b6SMatthew Barth     auto& actions =
512d9cb63b6SMatthew Barth         std::get<std::vector<std::unique_ptr<ActionBase>>&>(data.second);
513d9cb63b6SMatthew Barth     // Perform the actions in the timer data
514d9cb63b6SMatthew Barth     std::for_each(actions.begin(), actions.end(),
51500f6aa09SMatthew Barth                   [](auto& action) { action->run(); });
516d9cb63b6SMatthew Barth 
517d9cb63b6SMatthew Barth     // Remove oneshot timers after they expired
518d9cb63b6SMatthew Barth     if (data.first == TimerType::oneshot)
519d9cb63b6SMatthew Barth     {
520d9cb63b6SMatthew Barth         auto itTimer = std::find_if(
521d9cb63b6SMatthew Barth             _timers.begin(), _timers.end(), [&data](const auto& timer) {
522d9cb63b6SMatthew Barth                 return (data.first == timer.first->first &&
523d9cb63b6SMatthew Barth                         (std::get<std::string>(data.second) ==
524d9cb63b6SMatthew Barth                          std::get<std::string>(timer.first->second)));
525d9cb63b6SMatthew Barth             });
526d9cb63b6SMatthew Barth         if (itTimer != std::end(_timers))
527d9cb63b6SMatthew Barth         {
528d9cb63b6SMatthew Barth             _timers.erase(itTimer);
529d9cb63b6SMatthew Barth         }
530d9cb63b6SMatthew Barth     }
531d9cb63b6SMatthew Barth }
532d9cb63b6SMatthew Barth 
533ebabc040SMatthew Barth void Manager::handleSignal(sdbusplus::message::message& msg,
534fac8a2feSMatthew Barth                            const std::vector<SignalPkg>& pkgs)
535ebabc040SMatthew Barth {
536fac8a2feSMatthew Barth     for (auto& pkg : pkgs)
537ebabc040SMatthew Barth     {
538ebabc040SMatthew Barth         // Handle the signal callback and only run the actions if the handler
539ebabc040SMatthew Barth         // updated the cache for the given SignalObject
540ebabc040SMatthew Barth         if (std::get<SignalHandler>(pkg)(msg, std::get<SignalObject>(pkg),
541ebabc040SMatthew Barth                                          *this))
542ebabc040SMatthew Barth         {
543ebabc040SMatthew Barth             // Perform the actions in the handler package
544ebabc040SMatthew Barth             auto& actions = std::get<SignalActions>(pkg);
545ebabc040SMatthew Barth             std::for_each(actions.begin(), actions.end(),
546ebabc040SMatthew Barth                           [](auto& action) { action.get()->run(); });
547ebabc040SMatthew Barth         }
548ebabc040SMatthew Barth     }
549ebabc040SMatthew Barth }
550ebabc040SMatthew Barth 
551acd737cdSMatthew Barth void Manager::setProfiles()
552acd737cdSMatthew Barth {
553acd737cdSMatthew Barth     // Profiles JSON config file is optional
554acd737cdSMatthew Barth     auto confFile = fan::JsonConfig::getConfFile(_bus, confAppName,
555acd737cdSMatthew Barth                                                  Profile::confFileName, true);
556e91ac864SMatthew Barth 
557e91ac864SMatthew Barth     _profiles.clear();
558acd737cdSMatthew Barth     if (!confFile.empty())
559acd737cdSMatthew Barth     {
560acd737cdSMatthew Barth         for (const auto& entry : fan::JsonConfig::load(confFile))
561acd737cdSMatthew Barth         {
562acd737cdSMatthew Barth             auto obj = std::make_unique<Profile>(entry);
563acd737cdSMatthew Barth             _profiles.emplace(
564acd737cdSMatthew Barth                 std::make_pair(obj->getName(), obj->getProfiles()),
565acd737cdSMatthew Barth                 std::move(obj));
566acd737cdSMatthew Barth         }
567acd737cdSMatthew Barth     }
568e91ac864SMatthew Barth 
569acd737cdSMatthew Barth     // Ensure all configurations use the same set of active profiles
570acd737cdSMatthew Barth     // (In case a profile's active state changes during configuration)
571e91ac864SMatthew Barth     _activeProfiles.clear();
572acd737cdSMatthew Barth     for (const auto& profile : _profiles)
573acd737cdSMatthew Barth     {
574acd737cdSMatthew Barth         if (profile.second->isActive())
575acd737cdSMatthew Barth         {
576acd737cdSMatthew Barth             _activeProfiles.emplace_back(profile.first.first);
577acd737cdSMatthew Barth         }
578acd737cdSMatthew Barth     }
579acd737cdSMatthew Barth }
580acd737cdSMatthew Barth 
581a227a16dSMatthew Barth } // namespace phosphor::fan::control::json
582