xref: /openbmc/phosphor-fan-presence/control/json/manager.cpp (revision d0ba86a3a5deb0157bddee5cbc254ad248615e9c)
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 
31c024d780SMatthew Barth #include <systemd/sd-bus.h>
32c024d780SMatthew Barth 
33acd737cdSMatthew Barth #include <nlohmann/json.hpp>
34a227a16dSMatthew Barth #include <sdbusplus/bus.hpp>
351542fb5aSMatthew Barth #include <sdbusplus/server/manager.hpp>
36acd737cdSMatthew Barth #include <sdeventplus/event.hpp>
37d9cb63b6SMatthew Barth #include <sdeventplus/utility/timer.hpp>
38a227a16dSMatthew Barth 
39de90fb4dSMatthew Barth #include <algorithm>
40d9cb63b6SMatthew Barth #include <chrono>
41a227a16dSMatthew Barth #include <filesystem>
42d9cb63b6SMatthew Barth #include <functional>
43d9cb63b6SMatthew Barth #include <map>
44d9cb63b6SMatthew Barth #include <memory>
45d9cb63b6SMatthew Barth #include <tuple>
46d9cb63b6SMatthew Barth #include <utility>
4706764946SMatthew Barth #include <vector>
48a227a16dSMatthew Barth 
49a227a16dSMatthew Barth namespace phosphor::fan::control::json
50a227a16dSMatthew Barth {
51a227a16dSMatthew Barth 
52acd737cdSMatthew Barth using json = nlohmann::json;
53acd737cdSMatthew Barth 
54acd737cdSMatthew Barth std::vector<std::string> Manager::_activeProfiles;
5512cb125aSMatthew Barth std::map<std::string,
564ca87faeSMatthew Barth          std::map<std::string, std::pair<bool, std::vector<std::string>>>>
5712cb125aSMatthew Barth     Manager::_servTree;
5807fecfc6SMatthew Barth std::map<std::string,
5907fecfc6SMatthew Barth          std::map<std::string, std::map<std::string, PropertyVariantType>>>
6007fecfc6SMatthew Barth     Manager::_objects;
61d76351bdSMatt Spinler std::unordered_map<std::string, PropertyVariantType> Manager::_parameters;
62*d0ba86a3SMatt Spinler std::unordered_map<std::string, TriggerActions> Manager::_parameterTriggers;
63acd737cdSMatthew Barth 
647787def0SMatt Spinler const std::string Manager::dumpFile = "/tmp/fan_control_dump.json";
657787def0SMatt Spinler 
669403a217SMatthew Barth Manager::Manager(const sdeventplus::Event& event) :
6748f44daaSMatthew Barth     _bus(util::SDBusPlus::getBus()), _event(event),
683770a1daSMatthew Barth     _mgr(util::SDBusPlus::getBus(), CONTROL_OBJPATH), _loadAllowed(true),
6948f44daaSMatthew Barth     _powerState(std::make_unique<PGoodState>(
7048f44daaSMatthew Barth         util::SDBusPlus::getBus(),
7148f44daaSMatthew Barth         std::bind(std::mem_fn(&Manager::powerStateChanged), this,
7248f44daaSMatthew Barth                   std::placeholders::_1)))
733770a1daSMatthew Barth {}
74e91ac864SMatthew Barth 
75e91ac864SMatthew Barth void Manager::sighupHandler(sdeventplus::source::Signal&,
76e91ac864SMatthew Barth                             const struct signalfd_siginfo*)
77e91ac864SMatthew Barth {
78e91ac864SMatthew Barth     // Save current set of available and active profiles
79e91ac864SMatthew Barth     std::map<configKey, std::unique_ptr<Profile>> profiles;
80e91ac864SMatthew Barth     profiles.swap(_profiles);
81e91ac864SMatthew Barth     std::vector<std::string> activeProfiles;
82e91ac864SMatthew Barth     activeProfiles.swap(_activeProfiles);
83e91ac864SMatthew Barth 
84e91ac864SMatthew Barth     try
85e91ac864SMatthew Barth     {
863770a1daSMatthew Barth         _loadAllowed = true;
87e91ac864SMatthew Barth         load();
88e91ac864SMatthew Barth     }
89ddb773b2SPatrick Williams     catch (const std::runtime_error& re)
90e91ac864SMatthew Barth     {
91e91ac864SMatthew Barth         // Restore saved available and active profiles
923770a1daSMatthew Barth         _loadAllowed = false;
93e91ac864SMatthew Barth         _profiles.swap(profiles);
94e91ac864SMatthew Barth         _activeProfiles.swap(activeProfiles);
95e91ac864SMatthew Barth         log<level::ERR>("Error reloading configs, no changes made",
96e91ac864SMatthew Barth                         entry("LOAD_ERROR=%s", re.what()));
97e91ac864SMatthew Barth     }
98e91ac864SMatthew Barth }
99e91ac864SMatthew Barth 
1002fc0a35dSMatt Spinler void Manager::sigUsr1Handler(sdeventplus::source::Signal&,
1012fc0a35dSMatt Spinler                              const struct signalfd_siginfo*)
1022fc0a35dSMatt Spinler {
1037787def0SMatt Spinler     debugDumpEventSource = std::make_unique<sdeventplus::source::Defer>(
1047787def0SMatt Spinler         _event, std::bind(std::mem_fn(&Manager::dumpDebugData), this,
1052fc0a35dSMatt Spinler                           std::placeholders::_1));
1062fc0a35dSMatt Spinler }
1072fc0a35dSMatt Spinler 
1087787def0SMatt Spinler void Manager::dumpDebugData(sdeventplus::source::EventBase& /*source*/)
1092fc0a35dSMatt Spinler {
1107787def0SMatt Spinler     json data;
1117787def0SMatt Spinler     FlightRecorder::instance().dump(data);
112b5c21a24SMatt Spinler     dumpCache(data);
1137787def0SMatt Spinler 
1149db6dd1dSMatt Spinler     std::for_each(_zones.begin(), _zones.end(), [&data](const auto& zone) {
1159db6dd1dSMatt Spinler         data["zones"][zone.second->getName()] = zone.second->dump();
1169db6dd1dSMatt Spinler     });
1179db6dd1dSMatt Spinler 
1187787def0SMatt Spinler     std::ofstream file{Manager::dumpFile};
1197787def0SMatt Spinler     if (!file)
1207787def0SMatt Spinler     {
1217787def0SMatt Spinler         log<level::ERR>("Could not open file for fan dump");
1227787def0SMatt Spinler         return;
1237787def0SMatt Spinler     }
1247787def0SMatt Spinler 
1257787def0SMatt Spinler     file << std::setw(4) << data;
1267787def0SMatt Spinler 
1277787def0SMatt Spinler     debugDumpEventSource.reset();
1282fc0a35dSMatt Spinler }
1292fc0a35dSMatt Spinler 
130b5c21a24SMatt Spinler void Manager::dumpCache(json& data)
131b5c21a24SMatt Spinler {
132b5c21a24SMatt Spinler     auto& objects = data["objects"];
133b5c21a24SMatt Spinler     for (const auto& [path, interfaces] : _objects)
134b5c21a24SMatt Spinler     {
135b5c21a24SMatt Spinler         auto& interfaceJSON = objects[path];
136b5c21a24SMatt Spinler 
137b5c21a24SMatt Spinler         for (const auto& [interface, properties] : interfaces)
138b5c21a24SMatt Spinler         {
139b5c21a24SMatt Spinler             auto& propertyJSON = interfaceJSON[interface];
140b5c21a24SMatt Spinler             for (const auto& [propName, propValue] : properties)
141b5c21a24SMatt Spinler             {
142b5c21a24SMatt Spinler                 std::visit(
143b5c21a24SMatt Spinler                     [&obj = propertyJSON[propName]](auto&& val) { obj = val; },
144b5c21a24SMatt Spinler                     propValue);
145b5c21a24SMatt Spinler             }
146b5c21a24SMatt Spinler         }
147b5c21a24SMatt Spinler     }
148b5c21a24SMatt Spinler 
149b5c21a24SMatt Spinler     auto& parameters = data["parameters"];
150b5c21a24SMatt Spinler     for (const auto& [name, value] : _parameters)
151b5c21a24SMatt Spinler     {
15229088e79SMatt Spinler         std::visit([&obj = parameters[name]](auto&& val) { obj = val; }, value);
153b5c21a24SMatt Spinler     }
154b5c21a24SMatt Spinler 
155b5c21a24SMatt Spinler     data["services"] = _servTree;
156b5c21a24SMatt Spinler }
157b5c21a24SMatt Spinler 
158e91ac864SMatthew Barth void Manager::load()
159e91ac864SMatthew Barth {
1603770a1daSMatthew Barth     if (_loadAllowed)
1613770a1daSMatthew Barth     {
162e91ac864SMatthew Barth         // Load the available profiles and which are active
163acd737cdSMatthew Barth         setProfiles();
164acd737cdSMatthew Barth 
165acd737cdSMatthew Barth         // Load the zone configurations
166e91ac864SMatthew Barth         auto zones = getConfig<Zone>(false, _event, this);
167de90fb4dSMatthew Barth         // Load the fan configurations and move each fan into its zone
1689403a217SMatthew Barth         auto fans = getConfig<Fan>(false);
169de90fb4dSMatthew Barth         for (auto& fan : fans)
170de90fb4dSMatthew Barth         {
1710206c728SMatthew Barth             configKey fanProfile =
1720206c728SMatthew Barth                 std::make_pair(fan.second->getZone(), fan.first.second);
1730206c728SMatthew Barth             auto itZone = std::find_if(
174e91ac864SMatthew Barth                 zones.begin(), zones.end(), [&fanProfile](const auto& zone) {
1750206c728SMatthew Barth                     return Manager::inConfig(fanProfile, zone.first);
176de90fb4dSMatthew Barth                 });
177e91ac864SMatthew Barth             if (itZone != zones.end())
178de90fb4dSMatthew Barth             {
1796f787309SMatthew Barth                 if (itZone->second->getTarget() != fan.second->getTarget() &&
1806f787309SMatthew Barth                     fan.second->getTarget() != 0)
1816f787309SMatthew Barth                 {
182e91ac864SMatthew Barth                     // Update zone target to current target of the fan in the
183e91ac864SMatthew Barth                     // zone
1846f787309SMatthew Barth                     itZone->second->setTarget(fan.second->getTarget());
1856f787309SMatthew Barth                 }
186de90fb4dSMatthew Barth                 itZone->second->addFan(std::move(fan.second));
187de90fb4dSMatthew Barth             }
188de90fb4dSMatthew Barth         }
189e91ac864SMatthew Barth 
1903695ac30SMatthew Barth         // Save all currently available groups, if any, then clear for reloading
1913695ac30SMatthew Barth         auto groups = std::move(Event::getAllGroups(false));
1923695ac30SMatthew Barth         Event::clearAllGroups();
1933695ac30SMatthew Barth 
1943695ac30SMatthew Barth         std::map<configKey, std::unique_ptr<Event>> events;
1953695ac30SMatthew Barth         try
1963695ac30SMatthew Barth         {
1973695ac30SMatthew Barth             // Load any events configured, including all the groups
1983695ac30SMatthew Barth             events = getConfig<Event>(true, this, zones);
1993695ac30SMatthew Barth         }
2003695ac30SMatthew Barth         catch (const std::runtime_error& re)
2013695ac30SMatthew Barth         {
2023695ac30SMatthew Barth             // Restore saved set of all available groups for current events
2033695ac30SMatthew Barth             Event::setAllGroups(std::move(groups));
2043695ac30SMatthew Barth             throw re;
2053695ac30SMatthew Barth         }
206e91ac864SMatthew Barth 
20714303a45SMatthew Barth         // Enable zones
208e91ac864SMatthew Barth         _zones = std::move(zones);
20914303a45SMatthew Barth         std::for_each(_zones.begin(), _zones.end(),
21014303a45SMatthew Barth                       [](const auto& entry) { entry.second->enable(); });
211b584d818SMatthew Barth 
212e91ac864SMatthew Barth         // Clear current timers and signal subscriptions before enabling events
2133770a1daSMatthew Barth         // To save reloading services and/or objects into cache, do not clear
2143770a1daSMatthew Barth         // cache
215e91ac864SMatthew Barth         _timers.clear();
216e91ac864SMatthew Barth         _signals.clear();
217e91ac864SMatthew Barth 
218e91ac864SMatthew Barth         // Enable events
219e91ac864SMatthew Barth         _events = std::move(events);
22054b5a24fSMatthew Barth         std::for_each(_events.begin(), _events.end(),
22154b5a24fSMatthew Barth                       [](const auto& entry) { entry.second->enable(); });
2223770a1daSMatthew Barth 
2233770a1daSMatthew Barth         _loadAllowed = false;
2243770a1daSMatthew Barth     }
22506764946SMatthew Barth }
226acd737cdSMatthew Barth 
22748f44daaSMatthew Barth void Manager::powerStateChanged(bool powerStateOn)
22848f44daaSMatthew Barth {
22948f44daaSMatthew Barth     if (powerStateOn)
23048f44daaSMatthew Barth     {
2316a2418a2SMatthew Barth         if (_zones.empty())
2326a2418a2SMatthew Barth         {
2336a2418a2SMatthew Barth             throw std::runtime_error("No configured zones found at poweron");
2346a2418a2SMatthew Barth         }
23548f44daaSMatthew Barth         std::for_each(_zones.begin(), _zones.end(), [](const auto& entry) {
23648f44daaSMatthew Barth             entry.second->setTarget(entry.second->getPoweronTarget());
23748f44daaSMatthew Barth         });
238d1f97f43SMatt Spinler 
239d1f97f43SMatt Spinler         // Tell events to run their power on triggers
240d1f97f43SMatt Spinler         std::for_each(_events.begin(), _events.end(),
241d1f97f43SMatt Spinler                       [](const auto& entry) { entry.second->powerOn(); });
242d1f97f43SMatt Spinler     }
243d1f97f43SMatt Spinler     else
244d1f97f43SMatt Spinler     {
245d1f97f43SMatt Spinler         // Tell events to run their power off triggers
246d1f97f43SMatt Spinler         std::for_each(_events.begin(), _events.end(),
247d1f97f43SMatt Spinler                       [](const auto& entry) { entry.second->powerOff(); });
24848f44daaSMatthew Barth     }
24948f44daaSMatthew Barth }
25048f44daaSMatthew Barth 
251acd737cdSMatthew Barth const std::vector<std::string>& Manager::getActiveProfiles()
252acd737cdSMatthew Barth {
253acd737cdSMatthew Barth     return _activeProfiles;
254a227a16dSMatthew Barth }
255a227a16dSMatthew Barth 
2560206c728SMatthew Barth bool Manager::inConfig(const configKey& input, const configKey& comp)
2570206c728SMatthew Barth {
2580206c728SMatthew Barth     // Config names dont match, do not include in config
2590206c728SMatthew Barth     if (input.first != comp.first)
2600206c728SMatthew Barth     {
2610206c728SMatthew Barth         return false;
2620206c728SMatthew Barth     }
2630206c728SMatthew Barth     // No profiles specified by input config, can be used in any config
2640206c728SMatthew Barth     if (input.second.empty())
2650206c728SMatthew Barth     {
2660206c728SMatthew Barth         return true;
2670206c728SMatthew Barth     }
2680206c728SMatthew Barth     else
2690206c728SMatthew Barth     {
2700206c728SMatthew Barth         // Profiles must have one match in the other's profiles(and they must be
2710206c728SMatthew Barth         // an active profile) to be used in the config
2720206c728SMatthew Barth         return std::any_of(
2730206c728SMatthew Barth             input.second.begin(), input.second.end(),
2740206c728SMatthew Barth             [&comp](const auto& lProfile) {
2750206c728SMatthew Barth                 return std::any_of(
2760206c728SMatthew Barth                     comp.second.begin(), comp.second.end(),
2770206c728SMatthew Barth                     [&lProfile](const auto& rProfile) {
2780206c728SMatthew Barth                         if (lProfile != rProfile)
2790206c728SMatthew Barth                         {
2800206c728SMatthew Barth                             return false;
2810206c728SMatthew Barth                         }
2820206c728SMatthew Barth                         auto activeProfs = getActiveProfiles();
2830206c728SMatthew Barth                         return std::find(activeProfs.begin(), activeProfs.end(),
2840206c728SMatthew Barth                                          lProfile) != activeProfs.end();
2850206c728SMatthew Barth                     });
2860206c728SMatthew Barth             });
2870206c728SMatthew Barth     }
2880206c728SMatthew Barth }
2890206c728SMatthew Barth 
29012cb125aSMatthew Barth bool Manager::hasOwner(const std::string& path, const std::string& intf)
29112cb125aSMatthew Barth {
29212cb125aSMatthew Barth     auto itServ = _servTree.find(path);
29312cb125aSMatthew Barth     if (itServ == _servTree.end())
29412cb125aSMatthew Barth     {
29512cb125aSMatthew Barth         // Path not found in cache, therefore owner missing
29612cb125aSMatthew Barth         return false;
29712cb125aSMatthew Barth     }
2984ca87faeSMatthew Barth     for (const auto& service : itServ->second)
29912cb125aSMatthew Barth     {
30012cb125aSMatthew Barth         auto itIntf = std::find_if(
3014ca87faeSMatthew Barth             service.second.second.begin(), service.second.second.end(),
30212cb125aSMatthew Barth             [&intf](const auto& interface) { return intf == interface; });
3034ca87faeSMatthew Barth         if (itIntf != std::end(service.second.second))
30412cb125aSMatthew Barth         {
30512cb125aSMatthew Barth             // Service found, return owner state
3064ca87faeSMatthew Barth             return service.second.first;
30712cb125aSMatthew Barth         }
30812cb125aSMatthew Barth     }
30912cb125aSMatthew Barth     // Interface not found in cache, therefore owner missing
31012cb125aSMatthew Barth     return false;
31112cb125aSMatthew Barth }
31212cb125aSMatthew Barth 
3134ca87faeSMatthew Barth void Manager::setOwner(const std::string& path, const std::string& serv,
3144ca87faeSMatthew Barth                        const std::string& intf, bool isOwned)
3154ca87faeSMatthew Barth {
3162a9e7b2eSMatthew Barth     // Set owner state for specific object given
3172a9e7b2eSMatthew Barth     auto& ownIntf = _servTree[path][serv];
3182a9e7b2eSMatthew Barth     ownIntf.first = isOwned;
3194ca87faeSMatthew Barth     auto itIntf = std::find_if(
3202a9e7b2eSMatthew Barth         ownIntf.second.begin(), ownIntf.second.end(),
3214ca87faeSMatthew Barth         [&intf](const auto& interface) { return intf == interface; });
3222a9e7b2eSMatthew Barth     if (itIntf == std::end(ownIntf.second))
3234ca87faeSMatthew Barth     {
3242a9e7b2eSMatthew Barth         ownIntf.second.emplace_back(intf);
3252a9e7b2eSMatthew Barth     }
3262a9e7b2eSMatthew Barth 
3272a9e7b2eSMatthew Barth     // Update owner state on all entries of the same `serv` & `intf`
3282a9e7b2eSMatthew Barth     for (auto& itPath : _servTree)
3294ca87faeSMatthew Barth     {
3302a9e7b2eSMatthew Barth         if (itPath.first == path)
3312a9e7b2eSMatthew Barth         {
3322a9e7b2eSMatthew Barth             // Already set/updated owner on this path for `serv` & `intf`
3332a9e7b2eSMatthew Barth             continue;
3342a9e7b2eSMatthew Barth         }
3352a9e7b2eSMatthew Barth         for (auto& itServ : itPath.second)
3362a9e7b2eSMatthew Barth         {
3372a9e7b2eSMatthew Barth             if (itServ.first != serv)
3382a9e7b2eSMatthew Barth             {
3392a9e7b2eSMatthew Barth                 continue;
3402a9e7b2eSMatthew Barth             }
3412a9e7b2eSMatthew Barth             auto itIntf = std::find_if(
3422a9e7b2eSMatthew Barth                 itServ.second.second.begin(), itServ.second.second.end(),
3432a9e7b2eSMatthew Barth                 [&intf](const auto& interface) { return intf == interface; });
3442a9e7b2eSMatthew Barth             if (itIntf != std::end(itServ.second.second))
3452a9e7b2eSMatthew Barth             {
3462a9e7b2eSMatthew Barth                 itServ.second.first = isOwned;
3474ca87faeSMatthew Barth             }
3484ca87faeSMatthew Barth         }
3494ca87faeSMatthew Barth     }
3504ca87faeSMatthew Barth }
3514ca87faeSMatthew Barth 
3524ca87faeSMatthew Barth const std::string& Manager::findService(const std::string& path,
3534ca87faeSMatthew Barth                                         const std::string& intf)
3544ca87faeSMatthew Barth {
3554ca87faeSMatthew Barth     static const std::string empty = "";
3564ca87faeSMatthew Barth 
3574ca87faeSMatthew Barth     auto itServ = _servTree.find(path);
3584ca87faeSMatthew Barth     if (itServ != _servTree.end())
3594ca87faeSMatthew Barth     {
3604ca87faeSMatthew Barth         for (const auto& service : itServ->second)
3614ca87faeSMatthew Barth         {
3624ca87faeSMatthew Barth             auto itIntf = std::find_if(
3634ca87faeSMatthew Barth                 service.second.second.begin(), service.second.second.end(),
3644ca87faeSMatthew Barth                 [&intf](const auto& interface) { return intf == interface; });
3654ca87faeSMatthew Barth             if (itIntf != std::end(service.second.second))
3664ca87faeSMatthew Barth             {
3674ca87faeSMatthew Barth                 // Service found, return service name
3684ca87faeSMatthew Barth                 return service.first;
3694ca87faeSMatthew Barth             }
3704ca87faeSMatthew Barth         }
3714ca87faeSMatthew Barth     }
3724ca87faeSMatthew Barth 
3734ca87faeSMatthew Barth     return empty;
3744ca87faeSMatthew Barth }
3754ca87faeSMatthew Barth 
37698f6fc17SMatthew Barth void Manager::addServices(const std::string& intf, int32_t depth)
3774ca87faeSMatthew Barth {
3784ca87faeSMatthew Barth     // Get all subtree objects for the given interface
37934835150SMatt Spinler     auto objects = util::SDBusPlus::getSubTreeRaw(util::SDBusPlus::getBus(),
38034835150SMatt Spinler                                                   "/", intf, depth);
3814ca87faeSMatthew Barth     // Add what's returned to the cache of path->services
3824ca87faeSMatthew Barth     for (auto& itPath : objects)
3834ca87faeSMatthew Barth     {
3844ca87faeSMatthew Barth         auto pathIter = _servTree.find(itPath.first);
3854ca87faeSMatthew Barth         if (pathIter != _servTree.end())
3864ca87faeSMatthew Barth         {
3874ca87faeSMatthew Barth             // Path found in cache
3884ca87faeSMatthew Barth             for (auto& itServ : itPath.second)
3894ca87faeSMatthew Barth             {
3904ca87faeSMatthew Barth                 auto servIter = pathIter->second.find(itServ.first);
3914ca87faeSMatthew Barth                 if (servIter != pathIter->second.end())
3924ca87faeSMatthew Barth                 {
3934ca87faeSMatthew Barth                     // Service found in cache
3944ca87faeSMatthew Barth                     for (auto& itIntf : itServ.second)
3954ca87faeSMatthew Barth                     {
3964ca87faeSMatthew Barth                         if (std::find(servIter->second.second.begin(),
3974ca87faeSMatthew Barth                                       servIter->second.second.end(),
3984ca87faeSMatthew Barth                                       itIntf) == servIter->second.second.end())
3994ca87faeSMatthew Barth                         {
4004ca87faeSMatthew Barth                             // Add interface to cache
4014ca87faeSMatthew Barth                             servIter->second.second.emplace_back(itIntf);
4024ca87faeSMatthew Barth                         }
4034ca87faeSMatthew Barth                     }
4044ca87faeSMatthew Barth                 }
4054ca87faeSMatthew Barth                 else
4064ca87faeSMatthew Barth                 {
4074ca87faeSMatthew Barth                     // Service not found in cache
4084ca87faeSMatthew Barth                     auto intfs = {intf};
4094ca87faeSMatthew Barth                     pathIter->second[itServ.first] =
4104ca87faeSMatthew Barth                         std::make_pair(true, intfs);
4114ca87faeSMatthew Barth                 }
4124ca87faeSMatthew Barth             }
4134ca87faeSMatthew Barth         }
4144ca87faeSMatthew Barth         else
4154ca87faeSMatthew Barth         {
4164ca87faeSMatthew Barth             // Path not found in cache
4174ca87faeSMatthew Barth             auto intfs = {intf};
418a0b8a68fSMatt Spinler             for (const auto& [servName, servIntfs] : itPath.second)
419a0b8a68fSMatt Spinler             {
420a0b8a68fSMatt Spinler                 _servTree[itPath.first][servName] = std::make_pair(true, intfs);
421a0b8a68fSMatt Spinler             }
4224ca87faeSMatthew Barth         }
4234ca87faeSMatthew Barth     }
4244ca87faeSMatthew Barth }
4254ca87faeSMatthew Barth 
4264ca87faeSMatthew Barth const std::string& Manager::getService(const std::string& path,
4274ca87faeSMatthew Barth                                        const std::string& intf)
4284ca87faeSMatthew Barth {
4294ca87faeSMatthew Barth     // Retrieve service from cache
4304ca87faeSMatthew Barth     const auto& serviceName = findService(path, intf);
4314ca87faeSMatthew Barth     if (serviceName.empty())
4324ca87faeSMatthew Barth     {
43398f6fc17SMatthew Barth         addServices(intf, 0);
4344ca87faeSMatthew Barth         return findService(path, intf);
4354ca87faeSMatthew Barth     }
4364ca87faeSMatthew Barth 
4374ca87faeSMatthew Barth     return serviceName;
4384ca87faeSMatthew Barth }
4394ca87faeSMatthew Barth 
440f41e947bSMatthew Barth std::vector<std::string> Manager::findPaths(const std::string& serv,
441f41e947bSMatthew Barth                                             const std::string& intf)
442f41e947bSMatthew Barth {
443f41e947bSMatthew Barth     std::vector<std::string> paths;
444f41e947bSMatthew Barth 
445f41e947bSMatthew Barth     for (const auto& path : _servTree)
446f41e947bSMatthew Barth     {
447f41e947bSMatthew Barth         auto itServ = path.second.find(serv);
448f41e947bSMatthew Barth         if (itServ != path.second.end())
449f41e947bSMatthew Barth         {
450f41e947bSMatthew Barth             if (std::find(itServ->second.second.begin(),
451f41e947bSMatthew Barth                           itServ->second.second.end(),
452f41e947bSMatthew Barth                           intf) != itServ->second.second.end())
453f41e947bSMatthew Barth             {
454f41e947bSMatthew Barth                 if (std::find(paths.begin(), paths.end(), path.first) ==
455f41e947bSMatthew Barth                     paths.end())
456f41e947bSMatthew Barth                 {
457f41e947bSMatthew Barth                     paths.push_back(path.first);
458f41e947bSMatthew Barth                 }
459f41e947bSMatthew Barth             }
460f41e947bSMatthew Barth         }
461f41e947bSMatthew Barth     }
462f41e947bSMatthew Barth 
463f41e947bSMatthew Barth     return paths;
464f41e947bSMatthew Barth }
465f41e947bSMatthew Barth 
466f41e947bSMatthew Barth std::vector<std::string> Manager::getPaths(const std::string& serv,
467f41e947bSMatthew Barth                                            const std::string& intf)
468f41e947bSMatthew Barth {
469f41e947bSMatthew Barth     auto paths = findPaths(serv, intf);
470f41e947bSMatthew Barth     if (paths.empty())
471f41e947bSMatthew Barth     {
472f41e947bSMatthew Barth         addServices(intf, 0);
473f41e947bSMatthew Barth         return findPaths(serv, intf);
474f41e947bSMatthew Barth     }
475f41e947bSMatthew Barth 
476f41e947bSMatthew Barth     return paths;
477f41e947bSMatthew Barth }
478f41e947bSMatthew Barth 
4791a19eaddSMike Capps void Manager::insertFilteredObjects(ManagedObjects& ref)
4801a19eaddSMike Capps {
4811a19eaddSMike Capps     for (auto& [path, pathMap] : ref)
4821a19eaddSMike Capps     {
4831a19eaddSMike Capps         for (auto& [intf, intfMap] : pathMap)
4841a19eaddSMike Capps         {
4851a19eaddSMike Capps             // for each property on this path+interface
4861a19eaddSMike Capps             for (auto& [prop, value] : intfMap)
4871a19eaddSMike Capps             {
4881a19eaddSMike Capps                 setProperty(path, intf, prop, value);
4891a19eaddSMike Capps             }
4901a19eaddSMike Capps         }
4911a19eaddSMike Capps     }
4921a19eaddSMike Capps }
4931a19eaddSMike Capps 
494f41e947bSMatthew Barth void Manager::addObjects(const std::string& path, const std::string& intf,
495f41e947bSMatthew Barth                          const std::string& prop)
496f41e947bSMatthew Barth {
497f41e947bSMatthew Barth     auto service = getService(path, intf);
498f41e947bSMatthew Barth     if (service.empty())
499f41e947bSMatthew Barth     {
500f41e947bSMatthew Barth         // Log service not found for object
50134835150SMatt Spinler         log<level::DEBUG>(
502f41e947bSMatthew Barth             fmt::format("Unable to get service name for path {}, interface {}",
503f41e947bSMatthew Barth                         path, intf)
504f41e947bSMatthew Barth                 .c_str());
505f41e947bSMatthew Barth         return;
506f41e947bSMatthew Barth     }
507f41e947bSMatthew Barth 
508f41e947bSMatthew Barth     auto objMgrPaths = getPaths(service, "org.freedesktop.DBus.ObjectManager");
509f41e947bSMatthew Barth     if (objMgrPaths.empty())
510f41e947bSMatthew Barth     {
511f41e947bSMatthew Barth         // No object manager interface provided by service?
512f41e947bSMatthew Barth         // Attempt to retrieve property directly
5131a19eaddSMike Capps         auto value = util::SDBusPlus::getPropertyVariant<PropertyVariantType>(
514f41e947bSMatthew Barth             _bus, service, path, intf, prop);
5151a19eaddSMike Capps 
5161a19eaddSMike Capps         setProperty(path, intf, prop, value);
517f41e947bSMatthew Barth         return;
518f41e947bSMatthew Barth     }
519f41e947bSMatthew Barth 
520f41e947bSMatthew Barth     for (const auto& objMgrPath : objMgrPaths)
521f41e947bSMatthew Barth     {
522f41e947bSMatthew Barth         // Get all managed objects of service
5231a19eaddSMike Capps         // want to filter here...
524f41e947bSMatthew Barth         auto objects = util::SDBusPlus::getManagedObjects<PropertyVariantType>(
525f41e947bSMatthew Barth             _bus, service, objMgrPath);
526f41e947bSMatthew Barth 
5271a19eaddSMike Capps         // insert all objects but remove any NaN values
5281a19eaddSMike Capps         insertFilteredObjects(objects);
529f41e947bSMatthew Barth     }
530f41e947bSMatthew Barth }
531f41e947bSMatthew Barth 
532f41e947bSMatthew Barth const std::optional<PropertyVariantType>
533f41e947bSMatthew Barth     Manager::getProperty(const std::string& path, const std::string& intf,
534f41e947bSMatthew Barth                          const std::string& prop)
535f41e947bSMatthew Barth {
536f41e947bSMatthew Barth     // TODO Objects hosted by fan control (i.e. ThermalMode) are required to
537f41e947bSMatthew Barth     // update the cache upon being set/updated
538f41e947bSMatthew Barth     auto itPath = _objects.find(path);
539f41e947bSMatthew Barth     if (itPath != _objects.end())
540f41e947bSMatthew Barth     {
541f41e947bSMatthew Barth         auto itIntf = itPath->second.find(intf);
542f41e947bSMatthew Barth         if (itIntf != itPath->second.end())
543f41e947bSMatthew Barth         {
544f41e947bSMatthew Barth             auto itProp = itIntf->second.find(prop);
545f41e947bSMatthew Barth             if (itProp != itIntf->second.end())
546f41e947bSMatthew Barth             {
547f41e947bSMatthew Barth                 return itProp->second;
548f41e947bSMatthew Barth             }
549f41e947bSMatthew Barth         }
550f41e947bSMatthew Barth     }
551f41e947bSMatthew Barth 
552f41e947bSMatthew Barth     return std::nullopt;
553f41e947bSMatthew Barth }
554f41e947bSMatthew Barth 
5551a19eaddSMike Capps void Manager::setProperty(const std::string& path, const std::string& intf,
5561a19eaddSMike Capps                           const std::string& prop, PropertyVariantType value)
5571a19eaddSMike Capps {
5581a19eaddSMike Capps     // filter NaNs out of the cache
5591a19eaddSMike Capps     if (PropertyContainsNan(value))
5601a19eaddSMike Capps     {
5611a19eaddSMike Capps         // dont use operator [] if paths dont exist
5621a19eaddSMike Capps         if (_objects.find(path) != _objects.end() &&
5631a19eaddSMike Capps             _objects[path].find(intf) != _objects[path].end())
5641a19eaddSMike Capps         {
5651a19eaddSMike Capps             _objects[path][intf].erase(prop);
5661a19eaddSMike Capps         }
5671a19eaddSMike Capps     }
5681a19eaddSMike Capps     else
5691a19eaddSMike Capps     {
5701a19eaddSMike Capps         _objects[path][intf][prop] = std::move(value);
5711a19eaddSMike Capps     }
5721a19eaddSMike Capps }
5731a19eaddSMike Capps 
574d9cb63b6SMatthew Barth void Manager::addTimer(const TimerType type,
575d9cb63b6SMatthew Barth                        const std::chrono::microseconds interval,
576d9cb63b6SMatthew Barth                        std::unique_ptr<TimerPkg> pkg)
577d9cb63b6SMatthew Barth {
578d9cb63b6SMatthew Barth     auto dataPtr =
579d9cb63b6SMatthew Barth         std::make_unique<TimerData>(std::make_pair(type, std::move(*pkg)));
580d9cb63b6SMatthew Barth     Timer timer(_event,
581d9cb63b6SMatthew Barth                 std::bind(&Manager::timerExpired, this, std::ref(*dataPtr)));
582d9cb63b6SMatthew Barth     if (type == TimerType::repeating)
583d9cb63b6SMatthew Barth     {
584d9cb63b6SMatthew Barth         timer.restart(interval);
585d9cb63b6SMatthew Barth     }
586d9cb63b6SMatthew Barth     else if (type == TimerType::oneshot)
587d9cb63b6SMatthew Barth     {
588d9cb63b6SMatthew Barth         timer.restartOnce(interval);
589d9cb63b6SMatthew Barth     }
590d9cb63b6SMatthew Barth     else
591d9cb63b6SMatthew Barth     {
592d9cb63b6SMatthew Barth         throw std::invalid_argument("Invalid Timer Type");
593d9cb63b6SMatthew Barth     }
594d9cb63b6SMatthew Barth     _timers.emplace_back(std::move(dataPtr), std::move(timer));
595d9cb63b6SMatthew Barth }
596d9cb63b6SMatthew Barth 
597ade0c377SMatt Spinler void Manager::addGroup(const Group& group)
598ade0c377SMatt Spinler {
599ade0c377SMatt Spinler     const auto& members = group.getMembers();
600ade0c377SMatt Spinler     for (const auto& member : members)
601ade0c377SMatt Spinler     {
602ade0c377SMatt Spinler         try
603ade0c377SMatt Spinler         {
604ade0c377SMatt Spinler             auto service = getService(member, group.getInterface());
605ade0c377SMatt Spinler 
606ade0c377SMatt Spinler             auto variant =
607ade0c377SMatt Spinler                 util::SDBusPlus::getPropertyVariant<PropertyVariantType>(
608ade0c377SMatt Spinler                     service, member, group.getInterface(), group.getProperty());
609ade0c377SMatt Spinler 
610ade0c377SMatt Spinler             setProperty(member, group.getInterface(), group.getProperty(),
611ade0c377SMatt Spinler                         variant);
612ade0c377SMatt Spinler         }
613ade0c377SMatt Spinler         catch (const std::exception& e)
614ade0c377SMatt Spinler         {
615ade0c377SMatt Spinler             try
616ade0c377SMatt Spinler             {
617ade0c377SMatt Spinler                 _objects.at(member)
618ade0c377SMatt Spinler                     .at(group.getInterface())
619ade0c377SMatt Spinler                     .erase(group.getProperty());
620ade0c377SMatt Spinler             }
621ade0c377SMatt Spinler             catch (const std::out_of_range&)
622ade0c377SMatt Spinler             {}
623ade0c377SMatt Spinler         }
624ade0c377SMatt Spinler     }
625ade0c377SMatt Spinler }
626ade0c377SMatt Spinler 
627d9cb63b6SMatthew Barth void Manager::timerExpired(TimerData& data)
628d9cb63b6SMatthew Barth {
629ade0c377SMatt Spinler     if (std::get<bool>(data.second))
630ade0c377SMatt Spinler     {
631ade0c377SMatt Spinler         const auto& groups = std::get<const std::vector<Group>&>(data.second);
632ade0c377SMatt Spinler         std::for_each(groups.begin(), groups.end(),
633ade0c377SMatt Spinler                       [this](const auto& group) { addGroup(group); });
634ade0c377SMatt Spinler     }
635ade0c377SMatt Spinler 
636d9cb63b6SMatthew Barth     auto& actions =
637d9cb63b6SMatthew Barth         std::get<std::vector<std::unique_ptr<ActionBase>>&>(data.second);
638d9cb63b6SMatthew Barth     // Perform the actions in the timer data
639d9cb63b6SMatthew Barth     std::for_each(actions.begin(), actions.end(),
64000f6aa09SMatthew Barth                   [](auto& action) { action->run(); });
641d9cb63b6SMatthew Barth 
642d9cb63b6SMatthew Barth     // Remove oneshot timers after they expired
643d9cb63b6SMatthew Barth     if (data.first == TimerType::oneshot)
644d9cb63b6SMatthew Barth     {
645d9cb63b6SMatthew Barth         auto itTimer = std::find_if(
646d9cb63b6SMatthew Barth             _timers.begin(), _timers.end(), [&data](const auto& timer) {
647d9cb63b6SMatthew Barth                 return (data.first == timer.first->first &&
648d9cb63b6SMatthew Barth                         (std::get<std::string>(data.second) ==
649d9cb63b6SMatthew Barth                          std::get<std::string>(timer.first->second)));
650d9cb63b6SMatthew Barth             });
651d9cb63b6SMatthew Barth         if (itTimer != std::end(_timers))
652d9cb63b6SMatthew Barth         {
653d9cb63b6SMatthew Barth             _timers.erase(itTimer);
654d9cb63b6SMatthew Barth         }
655d9cb63b6SMatthew Barth     }
656d9cb63b6SMatthew Barth }
657d9cb63b6SMatthew Barth 
658ebabc040SMatthew Barth void Manager::handleSignal(sdbusplus::message::message& msg,
659c024d780SMatthew Barth                            const std::vector<SignalPkg>* pkgs)
660ebabc040SMatthew Barth {
661c024d780SMatthew Barth     for (auto& pkg : *pkgs)
662ebabc040SMatthew Barth     {
663ebabc040SMatthew Barth         // Handle the signal callback and only run the actions if the handler
664ebabc040SMatthew Barth         // updated the cache for the given SignalObject
665ebabc040SMatthew Barth         if (std::get<SignalHandler>(pkg)(msg, std::get<SignalObject>(pkg),
666ebabc040SMatthew Barth                                          *this))
667ebabc040SMatthew Barth         {
668ebabc040SMatthew Barth             // Perform the actions in the handler package
669*d0ba86a3SMatt Spinler             auto& actions = std::get<TriggerActions>(pkg);
670c3a69087SMatthew Barth             std::for_each(actions.begin(), actions.end(), [](auto& action) {
671c3a69087SMatthew Barth                 if (action.get())
672c3a69087SMatthew Barth                 {
673c3a69087SMatthew Barth                     action.get()->run();
674c3a69087SMatthew Barth                 }
675c3a69087SMatthew Barth             });
676ebabc040SMatthew Barth         }
677c024d780SMatthew Barth         // Only rewind message when not last package
678c024d780SMatthew Barth         if (&pkg != &pkgs->back())
679c024d780SMatthew Barth         {
680c024d780SMatthew Barth             sd_bus_message_rewind(msg.get(), true);
681c024d780SMatthew Barth         }
682ebabc040SMatthew Barth     }
683ebabc040SMatthew Barth }
684ebabc040SMatthew Barth 
685acd737cdSMatthew Barth void Manager::setProfiles()
686acd737cdSMatthew Barth {
687acd737cdSMatthew Barth     // Profiles JSON config file is optional
688acd737cdSMatthew Barth     auto confFile = fan::JsonConfig::getConfFile(_bus, confAppName,
689acd737cdSMatthew Barth                                                  Profile::confFileName, true);
690e91ac864SMatthew Barth 
691e91ac864SMatthew Barth     _profiles.clear();
692acd737cdSMatthew Barth     if (!confFile.empty())
693acd737cdSMatthew Barth     {
694acd737cdSMatthew Barth         for (const auto& entry : fan::JsonConfig::load(confFile))
695acd737cdSMatthew Barth         {
696acd737cdSMatthew Barth             auto obj = std::make_unique<Profile>(entry);
697acd737cdSMatthew Barth             _profiles.emplace(
698acd737cdSMatthew Barth                 std::make_pair(obj->getName(), obj->getProfiles()),
699acd737cdSMatthew Barth                 std::move(obj));
700acd737cdSMatthew Barth         }
701acd737cdSMatthew Barth     }
702e91ac864SMatthew Barth 
703acd737cdSMatthew Barth     // Ensure all configurations use the same set of active profiles
704acd737cdSMatthew Barth     // (In case a profile's active state changes during configuration)
705e91ac864SMatthew Barth     _activeProfiles.clear();
706acd737cdSMatthew Barth     for (const auto& profile : _profiles)
707acd737cdSMatthew Barth     {
708acd737cdSMatthew Barth         if (profile.second->isActive())
709acd737cdSMatthew Barth         {
710acd737cdSMatthew Barth             _activeProfiles.emplace_back(profile.first.first);
711acd737cdSMatthew Barth         }
712acd737cdSMatthew Barth     }
713acd737cdSMatthew Barth }
714acd737cdSMatthew Barth 
715*d0ba86a3SMatt Spinler void Manager::addParameterTrigger(
716*d0ba86a3SMatt Spinler     const std::string& name, std::vector<std::unique_ptr<ActionBase>>& actions)
717*d0ba86a3SMatt Spinler {
718*d0ba86a3SMatt Spinler     auto it = _parameterTriggers.find(name);
719*d0ba86a3SMatt Spinler     if (it != _parameterTriggers.end())
720*d0ba86a3SMatt Spinler     {
721*d0ba86a3SMatt Spinler         std::for_each(actions.begin(), actions.end(),
722*d0ba86a3SMatt Spinler                       [&actList = it->second](auto& action) {
723*d0ba86a3SMatt Spinler                           actList.emplace_back(std::ref(action));
724*d0ba86a3SMatt Spinler                       });
725*d0ba86a3SMatt Spinler     }
726*d0ba86a3SMatt Spinler     else
727*d0ba86a3SMatt Spinler     {
728*d0ba86a3SMatt Spinler         TriggerActions triggerActions;
729*d0ba86a3SMatt Spinler         std::for_each(actions.begin(), actions.end(),
730*d0ba86a3SMatt Spinler                       [&triggerActions](auto& action) {
731*d0ba86a3SMatt Spinler                           triggerActions.emplace_back(std::ref(action));
732*d0ba86a3SMatt Spinler                       });
733*d0ba86a3SMatt Spinler         _parameterTriggers[name] = std::move(triggerActions);
734*d0ba86a3SMatt Spinler     }
735*d0ba86a3SMatt Spinler }
736*d0ba86a3SMatt Spinler 
737*d0ba86a3SMatt Spinler void Manager::runParameterActions(const std::string& name)
738*d0ba86a3SMatt Spinler {
739*d0ba86a3SMatt Spinler     auto it = _parameterTriggers.find(name);
740*d0ba86a3SMatt Spinler     if (it != _parameterTriggers.end())
741*d0ba86a3SMatt Spinler     {
742*d0ba86a3SMatt Spinler         std::for_each(it->second.begin(), it->second.end(),
743*d0ba86a3SMatt Spinler                       [](auto& action) { action.get()->run(); });
744*d0ba86a3SMatt Spinler     }
745*d0ba86a3SMatt Spinler }
746*d0ba86a3SMatt Spinler 
747a227a16dSMatthew Barth } // namespace phosphor::fan::control::json
748