1a227a16dSMatthew Barth /**
2b2e9a4fcSMike Capps * Copyright © 2022 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"
21bf8e56f6SMike Capps #include "dbus_paths.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"
29bd52ed02SMatthew Barth #include "utils/flight_recorder.hpp"
30acd737cdSMatthew Barth #include "zone.hpp"
31a227a16dSMatthew Barth
32c024d780SMatthew Barth #include <systemd/sd-bus.h>
33c024d780SMatthew Barth
34acd737cdSMatthew Barth #include <nlohmann/json.hpp>
35a227a16dSMatthew Barth #include <sdbusplus/bus.hpp>
361542fb5aSMatthew Barth #include <sdbusplus/server/manager.hpp>
37acd737cdSMatthew Barth #include <sdeventplus/event.hpp>
38d9cb63b6SMatthew Barth #include <sdeventplus/utility/timer.hpp>
39a227a16dSMatthew Barth
40de90fb4dSMatthew Barth #include <algorithm>
41d9cb63b6SMatthew Barth #include <chrono>
42a227a16dSMatthew Barth #include <filesystem>
43d9cb63b6SMatthew Barth #include <functional>
44d9cb63b6SMatthew Barth #include <map>
45d9cb63b6SMatthew Barth #include <memory>
46d9cb63b6SMatthew Barth #include <tuple>
47d9cb63b6SMatthew Barth #include <utility>
4806764946SMatthew Barth #include <vector>
49a227a16dSMatthew Barth
50a227a16dSMatthew Barth namespace phosphor::fan::control::json
51a227a16dSMatthew Barth {
52a227a16dSMatthew Barth
53acd737cdSMatthew Barth using json = nlohmann::json;
54acd737cdSMatthew Barth
55acd737cdSMatthew Barth std::vector<std::string> Manager::_activeProfiles;
5612cb125aSMatthew Barth std::map<std::string,
574ca87faeSMatthew Barth std::map<std::string, std::pair<bool, std::vector<std::string>>>>
5812cb125aSMatthew Barth Manager::_servTree;
5907fecfc6SMatthew Barth std::map<std::string,
6007fecfc6SMatthew Barth std::map<std::string, std::map<std::string, PropertyVariantType>>>
6107fecfc6SMatthew Barth Manager::_objects;
62d76351bdSMatt Spinler std::unordered_map<std::string, PropertyVariantType> Manager::_parameters;
63d0ba86a3SMatt Spinler std::unordered_map<std::string, TriggerActions> Manager::_parameterTriggers;
64acd737cdSMatthew Barth
657787def0SMatt Spinler const std::string Manager::dumpFile = "/tmp/fan_control_dump.json";
667787def0SMatt Spinler
Manager(const sdeventplus::Event & event)679403a217SMatthew Barth Manager::Manager(const sdeventplus::Event& event) :
6848f44daaSMatthew Barth _bus(util::SDBusPlus::getBus()), _event(event),
693770a1daSMatthew Barth _mgr(util::SDBusPlus::getBus(), CONTROL_OBJPATH), _loadAllowed(true),
7048f44daaSMatthew Barth _powerState(std::make_unique<PGoodState>(
7148f44daaSMatthew Barth util::SDBusPlus::getBus(),
7248f44daaSMatthew Barth std::bind(std::mem_fn(&Manager::powerStateChanged), this,
7348f44daaSMatthew Barth std::placeholders::_1)))
743770a1daSMatthew Barth {}
75e91ac864SMatthew Barth
sighupHandler(sdeventplus::source::Signal &,const struct signalfd_siginfo *)76e91ac864SMatthew Barth void Manager::sighupHandler(sdeventplus::source::Signal&,
77e91ac864SMatthew Barth const struct signalfd_siginfo*)
78e91ac864SMatthew Barth {
79bd52ed02SMatthew Barth FlightRecorder::instance().log("main", "SIGHUP received");
80e91ac864SMatthew Barth // Save current set of available and active profiles
81e91ac864SMatthew Barth std::map<configKey, std::unique_ptr<Profile>> profiles;
82e91ac864SMatthew Barth profiles.swap(_profiles);
83e91ac864SMatthew Barth std::vector<std::string> activeProfiles;
84e91ac864SMatthew Barth activeProfiles.swap(_activeProfiles);
85e91ac864SMatthew Barth
86e91ac864SMatthew Barth try
87e91ac864SMatthew Barth {
883770a1daSMatthew Barth _loadAllowed = true;
89e91ac864SMatthew Barth load();
90e91ac864SMatthew Barth }
91ddb773b2SPatrick Williams catch (const std::runtime_error& re)
92e91ac864SMatthew Barth {
93e91ac864SMatthew Barth // Restore saved available and active profiles
943770a1daSMatthew Barth _loadAllowed = false;
95e91ac864SMatthew Barth _profiles.swap(profiles);
96e91ac864SMatthew Barth _activeProfiles.swap(activeProfiles);
97e91ac864SMatthew Barth log<level::ERR>("Error reloading configs, no changes made",
98e91ac864SMatthew Barth entry("LOAD_ERROR=%s", re.what()));
99bd52ed02SMatthew Barth FlightRecorder::instance().log(
100fbf4703fSPatrick Williams "main", std::format("Error reloading configs, no changes made: {}",
101bd52ed02SMatthew Barth re.what()));
102e91ac864SMatthew Barth }
103e91ac864SMatthew Barth }
104e91ac864SMatthew Barth
dumpDebugData(sdeventplus::source::Signal &,const struct signalfd_siginfo *)10527f5f4e9SMatt Spinler void Manager::dumpDebugData(sdeventplus::source::Signal&,
1062fc0a35dSMatt Spinler const struct signalfd_siginfo*)
1072fc0a35dSMatt Spinler {
1087787def0SMatt Spinler json data;
1097787def0SMatt Spinler FlightRecorder::instance().dump(data);
110b5c21a24SMatt Spinler dumpCache(data);
1117787def0SMatt Spinler
1129db6dd1dSMatt Spinler std::for_each(_zones.begin(), _zones.end(), [&data](const auto& zone) {
1139db6dd1dSMatt Spinler data["zones"][zone.second->getName()] = zone.second->dump();
1149db6dd1dSMatt Spinler });
1159db6dd1dSMatt Spinler
1167787def0SMatt Spinler std::ofstream file{Manager::dumpFile};
1177787def0SMatt Spinler if (!file)
1187787def0SMatt Spinler {
1197787def0SMatt Spinler log<level::ERR>("Could not open file for fan dump");
1207787def0SMatt Spinler return;
1217787def0SMatt Spinler }
1227787def0SMatt Spinler
1237787def0SMatt Spinler file << std::setw(4) << data;
1242fc0a35dSMatt Spinler }
1252fc0a35dSMatt Spinler
dumpCache(json & data)126b5c21a24SMatt Spinler void Manager::dumpCache(json& data)
127b5c21a24SMatt Spinler {
128b5c21a24SMatt Spinler auto& objects = data["objects"];
129b5c21a24SMatt Spinler for (const auto& [path, interfaces] : _objects)
130b5c21a24SMatt Spinler {
131b5c21a24SMatt Spinler auto& interfaceJSON = objects[path];
132b5c21a24SMatt Spinler
133b5c21a24SMatt Spinler for (const auto& [interface, properties] : interfaces)
134b5c21a24SMatt Spinler {
135b5c21a24SMatt Spinler auto& propertyJSON = interfaceJSON[interface];
136b5c21a24SMatt Spinler for (const auto& [propName, propValue] : properties)
137b5c21a24SMatt Spinler {
138b5c21a24SMatt Spinler std::visit(
139b5c21a24SMatt Spinler [&obj = propertyJSON[propName]](auto&& val) { obj = val; },
140b5c21a24SMatt Spinler propValue);
141b5c21a24SMatt Spinler }
142b5c21a24SMatt Spinler }
143b5c21a24SMatt Spinler }
144b5c21a24SMatt Spinler
145b5c21a24SMatt Spinler auto& parameters = data["parameters"];
146b5c21a24SMatt Spinler for (const auto& [name, value] : _parameters)
147b5c21a24SMatt Spinler {
14829088e79SMatt Spinler std::visit([&obj = parameters[name]](auto&& val) { obj = val; }, value);
149b5c21a24SMatt Spinler }
150b5c21a24SMatt Spinler
151c3eb7b3cSMatt Spinler std::for_each(_events.begin(), _events.end(), [&data](const auto& event) {
152c3eb7b3cSMatt Spinler data["events"][event.second->getName()] = event.second->dump();
153c3eb7b3cSMatt Spinler });
154c3eb7b3cSMatt Spinler
155b5c21a24SMatt Spinler data["services"] = _servTree;
156b5c21a24SMatt Spinler }
157b5c21a24SMatt Spinler
load()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 {
171*dfddd648SPatrick Williams configKey fanProfile =
172*dfddd648SPatrick Williams std::make_pair(fan.second->getZone(), fan.first.second);
173*dfddd648SPatrick Williams auto itZone = std::find_if(
174*dfddd648SPatrick Williams 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);
220e56672d5SMatt Spinler FlightRecorder::instance().log("main", "Enabling events");
22154b5a24fSMatthew Barth std::for_each(_events.begin(), _events.end(),
22254b5a24fSMatthew Barth [](const auto& entry) { entry.second->enable(); });
223e56672d5SMatt Spinler FlightRecorder::instance().log("main", "Done enabling events");
2243770a1daSMatthew Barth
2253770a1daSMatthew Barth _loadAllowed = false;
2263770a1daSMatthew Barth }
22706764946SMatthew Barth }
228acd737cdSMatthew Barth
powerStateChanged(bool powerStateOn)22948f44daaSMatthew Barth void Manager::powerStateChanged(bool powerStateOn)
23048f44daaSMatthew Barth {
23148f44daaSMatthew Barth if (powerStateOn)
23248f44daaSMatthew Barth {
2338d9c391aSMatt Spinler FlightRecorder::instance().log("power", "Power on");
2346a2418a2SMatthew Barth if (_zones.empty())
2356a2418a2SMatthew Barth {
2366a2418a2SMatthew Barth throw std::runtime_error("No configured zones found at poweron");
2376a2418a2SMatthew Barth }
23848f44daaSMatthew Barth std::for_each(_zones.begin(), _zones.end(), [](const auto& entry) {
23948f44daaSMatthew Barth entry.second->setTarget(entry.second->getPoweronTarget());
24048f44daaSMatthew Barth });
241d1f97f43SMatt Spinler
242d1f97f43SMatt Spinler // Tell events to run their power on triggers
243d1f97f43SMatt Spinler std::for_each(_events.begin(), _events.end(),
244d1f97f43SMatt Spinler [](const auto& entry) { entry.second->powerOn(); });
245d1f97f43SMatt Spinler }
246d1f97f43SMatt Spinler else
247d1f97f43SMatt Spinler {
2488d9c391aSMatt Spinler FlightRecorder::instance().log("power", "Power off");
249d1f97f43SMatt Spinler // Tell events to run their power off triggers
250d1f97f43SMatt Spinler std::for_each(_events.begin(), _events.end(),
251d1f97f43SMatt Spinler [](const auto& entry) { entry.second->powerOff(); });
25248f44daaSMatthew Barth }
25348f44daaSMatthew Barth }
25448f44daaSMatthew Barth
getActiveProfiles()255acd737cdSMatthew Barth const std::vector<std::string>& Manager::getActiveProfiles()
256acd737cdSMatthew Barth {
257acd737cdSMatthew Barth return _activeProfiles;
258a227a16dSMatthew Barth }
259a227a16dSMatthew Barth
inConfig(const configKey & input,const configKey & comp)2600206c728SMatthew Barth bool Manager::inConfig(const configKey& input, const configKey& comp)
2610206c728SMatthew Barth {
2620206c728SMatthew Barth // Config names dont match, do not include in config
2630206c728SMatthew Barth if (input.first != comp.first)
2640206c728SMatthew Barth {
2650206c728SMatthew Barth return false;
2660206c728SMatthew Barth }
2670206c728SMatthew Barth // No profiles specified by input config, can be used in any config
2680206c728SMatthew Barth if (input.second.empty())
2690206c728SMatthew Barth {
2700206c728SMatthew Barth return true;
2710206c728SMatthew Barth }
2720206c728SMatthew Barth else
2730206c728SMatthew Barth {
2740206c728SMatthew Barth // Profiles must have one match in the other's profiles(and they must be
2750206c728SMatthew Barth // an active profile) to be used in the config
276*dfddd648SPatrick Williams return std::any_of(
277*dfddd648SPatrick Williams input.second.begin(), input.second.end(),
2780206c728SMatthew Barth [&comp](const auto& lProfile) {
279*dfddd648SPatrick Williams return std::any_of(
280*dfddd648SPatrick Williams comp.second.begin(), comp.second.end(),
2810206c728SMatthew Barth [&lProfile](const auto& rProfile) {
2820206c728SMatthew Barth if (lProfile != rProfile)
2830206c728SMatthew Barth {
2840206c728SMatthew Barth return false;
2850206c728SMatthew Barth }
2860206c728SMatthew Barth auto activeProfs = getActiveProfiles();
2870206c728SMatthew Barth return std::find(activeProfs.begin(), activeProfs.end(),
2880206c728SMatthew Barth lProfile) != activeProfs.end();
2890206c728SMatthew Barth });
2900206c728SMatthew Barth });
2910206c728SMatthew Barth }
2920206c728SMatthew Barth }
2930206c728SMatthew Barth
hasOwner(const std::string & path,const std::string & intf)29412cb125aSMatthew Barth bool Manager::hasOwner(const std::string& path, const std::string& intf)
29512cb125aSMatthew Barth {
29612cb125aSMatthew Barth auto itServ = _servTree.find(path);
29712cb125aSMatthew Barth if (itServ == _servTree.end())
29812cb125aSMatthew Barth {
29912cb125aSMatthew Barth // Path not found in cache, therefore owner missing
30012cb125aSMatthew Barth return false;
30112cb125aSMatthew Barth }
3024ca87faeSMatthew Barth for (const auto& service : itServ->second)
30312cb125aSMatthew Barth {
30412cb125aSMatthew Barth auto itIntf = std::find_if(
3054ca87faeSMatthew Barth service.second.second.begin(), service.second.second.end(),
30612cb125aSMatthew Barth [&intf](const auto& interface) { return intf == interface; });
3074ca87faeSMatthew Barth if (itIntf != std::end(service.second.second))
30812cb125aSMatthew Barth {
30912cb125aSMatthew Barth // Service found, return owner state
3104ca87faeSMatthew Barth return service.second.first;
31112cb125aSMatthew Barth }
31212cb125aSMatthew Barth }
31312cb125aSMatthew Barth // Interface not found in cache, therefore owner missing
31412cb125aSMatthew Barth return false;
31512cb125aSMatthew Barth }
31612cb125aSMatthew Barth
setOwner(const std::string & serv,bool hasOwner)3176d8e2d3eSMatthew Barth void Manager::setOwner(const std::string& serv, bool hasOwner)
3186d8e2d3eSMatthew Barth {
3196d8e2d3eSMatthew Barth // Update owner state on all entries of `serv`
3206d8e2d3eSMatthew Barth for (auto& itPath : _servTree)
3216d8e2d3eSMatthew Barth {
3226d8e2d3eSMatthew Barth auto itServ = itPath.second.find(serv);
3236d8e2d3eSMatthew Barth if (itServ != itPath.second.end())
3246d8e2d3eSMatthew Barth {
3256d8e2d3eSMatthew Barth itServ->second.first = hasOwner;
3266d8e2d3eSMatthew Barth
3276d8e2d3eSMatthew Barth // Remove associated interfaces from object cache when service no
3286d8e2d3eSMatthew Barth // longer has an owner
3296d8e2d3eSMatthew Barth if (!hasOwner && _objects.find(itPath.first) != _objects.end())
3306d8e2d3eSMatthew Barth {
3316d8e2d3eSMatthew Barth for (auto& intf : itServ->second.second)
3326d8e2d3eSMatthew Barth {
3336d8e2d3eSMatthew Barth _objects[itPath.first].erase(intf);
3346d8e2d3eSMatthew Barth }
3356d8e2d3eSMatthew Barth }
3366d8e2d3eSMatthew Barth }
3376d8e2d3eSMatthew Barth }
3386d8e2d3eSMatthew Barth }
3396d8e2d3eSMatthew Barth
setOwner(const std::string & path,const std::string & serv,const std::string & intf,bool isOwned)3404ca87faeSMatthew Barth void Manager::setOwner(const std::string& path, const std::string& serv,
3414ca87faeSMatthew Barth const std::string& intf, bool isOwned)
3424ca87faeSMatthew Barth {
3432a9e7b2eSMatthew Barth // Set owner state for specific object given
3442a9e7b2eSMatthew Barth auto& ownIntf = _servTree[path][serv];
3452a9e7b2eSMatthew Barth ownIntf.first = isOwned;
3465e15c3baSPatrick Williams auto itIntf = std::find_if(
3475e15c3baSPatrick Williams ownIntf.second.begin(), ownIntf.second.end(),
3485e15c3baSPatrick Williams [&intf](const auto& interface) { return intf == interface; });
3492a9e7b2eSMatthew Barth if (itIntf == std::end(ownIntf.second))
3504ca87faeSMatthew Barth {
3512a9e7b2eSMatthew Barth ownIntf.second.emplace_back(intf);
3522a9e7b2eSMatthew Barth }
3532a9e7b2eSMatthew Barth
3542a9e7b2eSMatthew Barth // Update owner state on all entries of the same `serv` & `intf`
3552a9e7b2eSMatthew Barth for (auto& itPath : _servTree)
3564ca87faeSMatthew Barth {
3572a9e7b2eSMatthew Barth if (itPath.first == path)
3582a9e7b2eSMatthew Barth {
3592a9e7b2eSMatthew Barth // Already set/updated owner on this path for `serv` & `intf`
3602a9e7b2eSMatthew Barth continue;
3612a9e7b2eSMatthew Barth }
3622a9e7b2eSMatthew Barth for (auto& itServ : itPath.second)
3632a9e7b2eSMatthew Barth {
3642a9e7b2eSMatthew Barth if (itServ.first != serv)
3652a9e7b2eSMatthew Barth {
3662a9e7b2eSMatthew Barth continue;
3672a9e7b2eSMatthew Barth }
3682a9e7b2eSMatthew Barth auto itIntf = std::find_if(
3692a9e7b2eSMatthew Barth itServ.second.second.begin(), itServ.second.second.end(),
3702a9e7b2eSMatthew Barth [&intf](const auto& interface) { return intf == interface; });
3712a9e7b2eSMatthew Barth if (itIntf != std::end(itServ.second.second))
3722a9e7b2eSMatthew Barth {
3732a9e7b2eSMatthew Barth itServ.second.first = isOwned;
3744ca87faeSMatthew Barth }
3754ca87faeSMatthew Barth }
3764ca87faeSMatthew Barth }
3774ca87faeSMatthew Barth }
3784ca87faeSMatthew Barth
findService(const std::string & path,const std::string & intf)3794ca87faeSMatthew Barth const std::string& Manager::findService(const std::string& path,
3804ca87faeSMatthew Barth const std::string& intf)
3814ca87faeSMatthew Barth {
3824ca87faeSMatthew Barth static const std::string empty = "";
3834ca87faeSMatthew Barth
3844ca87faeSMatthew Barth auto itServ = _servTree.find(path);
3854ca87faeSMatthew Barth if (itServ != _servTree.end())
3864ca87faeSMatthew Barth {
3874ca87faeSMatthew Barth for (const auto& service : itServ->second)
3884ca87faeSMatthew Barth {
3894ca87faeSMatthew Barth auto itIntf = std::find_if(
3904ca87faeSMatthew Barth service.second.second.begin(), service.second.second.end(),
3914ca87faeSMatthew Barth [&intf](const auto& interface) { return intf == interface; });
3924ca87faeSMatthew Barth if (itIntf != std::end(service.second.second))
3934ca87faeSMatthew Barth {
3944ca87faeSMatthew Barth // Service found, return service name
3954ca87faeSMatthew Barth return service.first;
3964ca87faeSMatthew Barth }
3974ca87faeSMatthew Barth }
3984ca87faeSMatthew Barth }
3994ca87faeSMatthew Barth
4004ca87faeSMatthew Barth return empty;
4014ca87faeSMatthew Barth }
4024ca87faeSMatthew Barth
addServices(const std::string & intf,int32_t depth)40398f6fc17SMatthew Barth void Manager::addServices(const std::string& intf, int32_t depth)
4044ca87faeSMatthew Barth {
4054ca87faeSMatthew Barth // Get all subtree objects for the given interface
40634835150SMatt Spinler auto objects = util::SDBusPlus::getSubTreeRaw(util::SDBusPlus::getBus(),
40734835150SMatt Spinler "/", intf, depth);
4084ca87faeSMatthew Barth // Add what's returned to the cache of path->services
4094ca87faeSMatthew Barth for (auto& itPath : objects)
4104ca87faeSMatthew Barth {
4114ca87faeSMatthew Barth auto pathIter = _servTree.find(itPath.first);
4124ca87faeSMatthew Barth if (pathIter != _servTree.end())
4134ca87faeSMatthew Barth {
4144ca87faeSMatthew Barth // Path found in cache
4154ca87faeSMatthew Barth for (auto& itServ : itPath.second)
4164ca87faeSMatthew Barth {
4174ca87faeSMatthew Barth auto servIter = pathIter->second.find(itServ.first);
4184ca87faeSMatthew Barth if (servIter != pathIter->second.end())
4194ca87faeSMatthew Barth {
4204ca87faeSMatthew Barth if (std::find(servIter->second.second.begin(),
421*dfddd648SPatrick Williams servIter->second.second.end(), intf) ==
422*dfddd648SPatrick Williams servIter->second.second.end())
4234ca87faeSMatthew Barth {
4244ca87faeSMatthew Barth // Add interface to cache
425d9f85c9aSMatt Spinler servIter->second.second.emplace_back(intf);
4264ca87faeSMatthew Barth }
4274ca87faeSMatthew Barth }
4284ca87faeSMatthew Barth else
4294ca87faeSMatthew Barth {
4304ca87faeSMatthew Barth // Service not found in cache
4314ca87faeSMatthew Barth auto intfs = {intf};
432*dfddd648SPatrick Williams pathIter->second[itServ.first] =
433*dfddd648SPatrick Williams std::make_pair(true, intfs);
4344ca87faeSMatthew Barth }
4354ca87faeSMatthew Barth }
4364ca87faeSMatthew Barth }
4374ca87faeSMatthew Barth else
4384ca87faeSMatthew Barth {
4394ca87faeSMatthew Barth // Path not found in cache
4404ca87faeSMatthew Barth auto intfs = {intf};
441a0b8a68fSMatt Spinler for (const auto& [servName, servIntfs] : itPath.second)
442a0b8a68fSMatt Spinler {
443a0b8a68fSMatt Spinler _servTree[itPath.first][servName] = std::make_pair(true, intfs);
444a0b8a68fSMatt Spinler }
4454ca87faeSMatthew Barth }
4464ca87faeSMatthew Barth }
4474ca87faeSMatthew Barth }
4484ca87faeSMatthew Barth
getService(const std::string & path,const std::string & intf)4494ca87faeSMatthew Barth const std::string& Manager::getService(const std::string& path,
4504ca87faeSMatthew Barth const std::string& intf)
4514ca87faeSMatthew Barth {
4524ca87faeSMatthew Barth // Retrieve service from cache
4534ca87faeSMatthew Barth const auto& serviceName = findService(path, intf);
4544ca87faeSMatthew Barth if (serviceName.empty())
4554ca87faeSMatthew Barth {
45698f6fc17SMatthew Barth addServices(intf, 0);
4574ca87faeSMatthew Barth return findService(path, intf);
4584ca87faeSMatthew Barth }
4594ca87faeSMatthew Barth
4604ca87faeSMatthew Barth return serviceName;
4614ca87faeSMatthew Barth }
4624ca87faeSMatthew Barth
463*dfddd648SPatrick Williams std::vector<std::string>
findPaths(const std::string & serv,const std::string & intf)464*dfddd648SPatrick Williams Manager::findPaths(const std::string& serv, const std::string& intf)
465f41e947bSMatthew Barth {
466f41e947bSMatthew Barth std::vector<std::string> paths;
467f41e947bSMatthew Barth
468f41e947bSMatthew Barth for (const auto& path : _servTree)
469f41e947bSMatthew Barth {
470f41e947bSMatthew Barth auto itServ = path.second.find(serv);
471f41e947bSMatthew Barth if (itServ != path.second.end())
472f41e947bSMatthew Barth {
473f41e947bSMatthew Barth if (std::find(itServ->second.second.begin(),
474*dfddd648SPatrick Williams itServ->second.second.end(), intf) !=
475*dfddd648SPatrick Williams itServ->second.second.end())
476f41e947bSMatthew Barth {
477f41e947bSMatthew Barth if (std::find(paths.begin(), paths.end(), path.first) ==
478f41e947bSMatthew Barth paths.end())
479f41e947bSMatthew Barth {
480f41e947bSMatthew Barth paths.push_back(path.first);
481f41e947bSMatthew Barth }
482f41e947bSMatthew Barth }
483f41e947bSMatthew Barth }
484f41e947bSMatthew Barth }
485f41e947bSMatthew Barth
486f41e947bSMatthew Barth return paths;
487f41e947bSMatthew Barth }
488f41e947bSMatthew Barth
getPaths(const std::string & serv,const std::string & intf)489f41e947bSMatthew Barth std::vector<std::string> Manager::getPaths(const std::string& serv,
490f41e947bSMatthew Barth const std::string& intf)
491f41e947bSMatthew Barth {
492f41e947bSMatthew Barth auto paths = findPaths(serv, intf);
493f41e947bSMatthew Barth if (paths.empty())
494f41e947bSMatthew Barth {
495f41e947bSMatthew Barth addServices(intf, 0);
496f41e947bSMatthew Barth return findPaths(serv, intf);
497f41e947bSMatthew Barth }
498f41e947bSMatthew Barth
499f41e947bSMatthew Barth return paths;
500f41e947bSMatthew Barth }
501f41e947bSMatthew Barth
insertFilteredObjects(ManagedObjects & ref)5021a19eaddSMike Capps void Manager::insertFilteredObjects(ManagedObjects& ref)
5031a19eaddSMike Capps {
504c2c2db7dSMatt Spinler // Filter out objects that aren't part of a group
505c2c2db7dSMatt Spinler const auto& allGroupMembers = Group::getAllMembers();
506c2c2db7dSMatt Spinler auto it = ref.begin();
507c2c2db7dSMatt Spinler
508c2c2db7dSMatt Spinler while (it != ref.end())
509c2c2db7dSMatt Spinler {
510c2c2db7dSMatt Spinler if (allGroupMembers.find(it->first) == allGroupMembers.end())
511c2c2db7dSMatt Spinler {
512c2c2db7dSMatt Spinler it = ref.erase(it);
513c2c2db7dSMatt Spinler }
514c2c2db7dSMatt Spinler else
515c2c2db7dSMatt Spinler {
516c2c2db7dSMatt Spinler it++;
517c2c2db7dSMatt Spinler }
518c2c2db7dSMatt Spinler }
519c2c2db7dSMatt Spinler
5201a19eaddSMike Capps for (auto& [path, pathMap] : ref)
5211a19eaddSMike Capps {
5221a19eaddSMike Capps for (auto& [intf, intfMap] : pathMap)
5231a19eaddSMike Capps {
5241a19eaddSMike Capps // for each property on this path+interface
5251a19eaddSMike Capps for (auto& [prop, value] : intfMap)
5261a19eaddSMike Capps {
5271a19eaddSMike Capps setProperty(path, intf, prop, value);
5281a19eaddSMike Capps }
5291a19eaddSMike Capps }
5301a19eaddSMike Capps }
5311a19eaddSMike Capps }
5321a19eaddSMike Capps
addObjects(const std::string & path,const std::string & intf,const std::string & prop,const std::string & serviceName)533f41e947bSMatthew Barth void Manager::addObjects(const std::string& path, const std::string& intf,
5349ac325c5SMatt Spinler const std::string& prop,
5359ac325c5SMatt Spinler const std::string& serviceName)
536f41e947bSMatthew Barth {
5379ac325c5SMatt Spinler auto service = serviceName;
5389ac325c5SMatt Spinler if (service.empty())
5399ac325c5SMatt Spinler {
5409ac325c5SMatt Spinler service = getService(path, intf);
541f41e947bSMatthew Barth if (service.empty())
542f41e947bSMatthew Barth {
543f41e947bSMatthew Barth // Log service not found for object
54434835150SMatt Spinler log<level::DEBUG>(
545fbf4703fSPatrick Williams std::format(
5469ac325c5SMatt Spinler "Unable to get service name for path {}, interface {}",
547f41e947bSMatthew Barth path, intf)
548f41e947bSMatthew Barth .c_str());
549f41e947bSMatthew Barth return;
550f41e947bSMatthew Barth }
5519ac325c5SMatt Spinler }
5529ac325c5SMatt Spinler else
5539ac325c5SMatt Spinler {
5549ac325c5SMatt Spinler // The service is known, so the service cache can be
5559ac325c5SMatt Spinler // populated even if the path itself isn't present.
5569ac325c5SMatt Spinler const auto& s = findService(path, intf);
5579ac325c5SMatt Spinler if (s.empty())
5589ac325c5SMatt Spinler {
5599ac325c5SMatt Spinler addServices(intf, 0);
5609ac325c5SMatt Spinler }
5619ac325c5SMatt Spinler }
562f41e947bSMatthew Barth
563f41e947bSMatthew Barth auto objMgrPaths = getPaths(service, "org.freedesktop.DBus.ObjectManager");
564078c0a86SChau Ly
565078c0a86SChau Ly bool useManagedObj = false;
566078c0a86SChau Ly
567078c0a86SChau Ly if (!objMgrPaths.empty())
568078c0a86SChau Ly {
569078c0a86SChau Ly for (const auto& objMgrPath : objMgrPaths)
570078c0a86SChau Ly {
571078c0a86SChau Ly // Get all managed objects of service
572078c0a86SChau Ly auto objects =
573078c0a86SChau Ly util::SDBusPlus::getManagedObjects<PropertyVariantType>(
574078c0a86SChau Ly _bus, service, objMgrPath);
575078c0a86SChau Ly if (objects.size() > 0)
576078c0a86SChau Ly {
577078c0a86SChau Ly useManagedObj = true;
578078c0a86SChau Ly }
579078c0a86SChau Ly // insert all objects that are in groups but remove any NaN values
580078c0a86SChau Ly insertFilteredObjects(objects);
581078c0a86SChau Ly }
582078c0a86SChau Ly }
583078c0a86SChau Ly
584078c0a86SChau Ly if (!useManagedObj)
585f41e947bSMatthew Barth {
586f41e947bSMatthew Barth // No object manager interface provided by service?
587078c0a86SChau Ly // Or no object is managed?
588f41e947bSMatthew Barth // Attempt to retrieve property directly
589f16f063bSMatt Spinler try
590f16f063bSMatt Spinler {
591f16f063bSMatt Spinler auto value =
592f16f063bSMatt Spinler util::SDBusPlus::getPropertyVariant<PropertyVariantType>(
593f41e947bSMatthew Barth _bus, service, path, intf, prop);
5941a19eaddSMike Capps
5951a19eaddSMike Capps setProperty(path, intf, prop, value);
596f16f063bSMatt Spinler }
597f16f063bSMatt Spinler catch (const std::exception& e)
598f16f063bSMatt Spinler {}
599f41e947bSMatthew Barth return;
600f41e947bSMatthew Barth }
601f41e947bSMatthew Barth }
602f41e947bSMatthew Barth
getProperty(const std::string & path,const std::string & intf,const std::string & prop)603*dfddd648SPatrick Williams const std::optional<PropertyVariantType> Manager::getProperty(
604*dfddd648SPatrick Williams const std::string& path, const std::string& intf, const std::string& prop)
605f41e947bSMatthew Barth {
606f41e947bSMatthew Barth // TODO Objects hosted by fan control (i.e. ThermalMode) are required to
607f41e947bSMatthew Barth // update the cache upon being set/updated
608f41e947bSMatthew Barth auto itPath = _objects.find(path);
609f41e947bSMatthew Barth if (itPath != _objects.end())
610f41e947bSMatthew Barth {
611f41e947bSMatthew Barth auto itIntf = itPath->second.find(intf);
612f41e947bSMatthew Barth if (itIntf != itPath->second.end())
613f41e947bSMatthew Barth {
614f41e947bSMatthew Barth auto itProp = itIntf->second.find(prop);
615f41e947bSMatthew Barth if (itProp != itIntf->second.end())
616f41e947bSMatthew Barth {
617f41e947bSMatthew Barth return itProp->second;
618f41e947bSMatthew Barth }
619f41e947bSMatthew Barth }
620f41e947bSMatthew Barth }
621f41e947bSMatthew Barth
622f41e947bSMatthew Barth return std::nullopt;
623f41e947bSMatthew Barth }
624f41e947bSMatthew Barth
setProperty(const std::string & path,const std::string & intf,const std::string & prop,PropertyVariantType value)6251a19eaddSMike Capps void Manager::setProperty(const std::string& path, const std::string& intf,
6261a19eaddSMike Capps const std::string& prop, PropertyVariantType value)
6271a19eaddSMike Capps {
6281a19eaddSMike Capps // filter NaNs out of the cache
6291a19eaddSMike Capps if (PropertyContainsNan(value))
6301a19eaddSMike Capps {
6311a19eaddSMike Capps // dont use operator [] if paths dont exist
6321a19eaddSMike Capps if (_objects.find(path) != _objects.end() &&
6331a19eaddSMike Capps _objects[path].find(intf) != _objects[path].end())
6341a19eaddSMike Capps {
6351a19eaddSMike Capps _objects[path][intf].erase(prop);
6361a19eaddSMike Capps }
6371a19eaddSMike Capps }
6381a19eaddSMike Capps else
6391a19eaddSMike Capps {
6401a19eaddSMike Capps _objects[path][intf][prop] = std::move(value);
6411a19eaddSMike Capps }
6421a19eaddSMike Capps }
6431a19eaddSMike Capps
addTimer(const TimerType type,const std::chrono::microseconds interval,std::unique_ptr<TimerPkg> pkg)644d9cb63b6SMatthew Barth void Manager::addTimer(const TimerType type,
645d9cb63b6SMatthew Barth const std::chrono::microseconds interval,
646d9cb63b6SMatthew Barth std::unique_ptr<TimerPkg> pkg)
647d9cb63b6SMatthew Barth {
648d9cb63b6SMatthew Barth auto dataPtr =
649d9cb63b6SMatthew Barth std::make_unique<TimerData>(std::make_pair(type, std::move(*pkg)));
650d9cb63b6SMatthew Barth Timer timer(_event,
651d9cb63b6SMatthew Barth std::bind(&Manager::timerExpired, this, std::ref(*dataPtr)));
652d9cb63b6SMatthew Barth if (type == TimerType::repeating)
653d9cb63b6SMatthew Barth {
654d9cb63b6SMatthew Barth timer.restart(interval);
655d9cb63b6SMatthew Barth }
656d9cb63b6SMatthew Barth else if (type == TimerType::oneshot)
657d9cb63b6SMatthew Barth {
658d9cb63b6SMatthew Barth timer.restartOnce(interval);
659d9cb63b6SMatthew Barth }
660d9cb63b6SMatthew Barth else
661d9cb63b6SMatthew Barth {
662d9cb63b6SMatthew Barth throw std::invalid_argument("Invalid Timer Type");
663d9cb63b6SMatthew Barth }
664d9cb63b6SMatthew Barth _timers.emplace_back(std::move(dataPtr), std::move(timer));
665d9cb63b6SMatthew Barth }
666d9cb63b6SMatthew Barth
addGroups(const std::vector<Group> & groups)6672f359f72SMatthew Barth void Manager::addGroups(const std::vector<Group>& groups)
668ade0c377SMatt Spinler {
6691a5c6236SMatthew Barth std::string lastServ;
6701a5c6236SMatthew Barth std::vector<std::string> objMgrPaths;
6712f359f72SMatthew Barth std::set<std::string> services;
6722f359f72SMatthew Barth for (const auto& group : groups)
6732f359f72SMatthew Barth {
67455627adfSMatthew Barth for (const auto& member : group.getMembers())
675ade0c377SMatt Spinler {
676ade0c377SMatt Spinler try
677ade0c377SMatt Spinler {
6781a5c6236SMatthew Barth auto service = group.getService();
67995d73490SMatthew Barth if (service.empty())
68095d73490SMatthew Barth {
68195d73490SMatthew Barth service = getService(member, group.getInterface());
68295d73490SMatthew Barth }
683ade0c377SMatt Spinler
6841a5c6236SMatthew Barth if (!service.empty())
6851a5c6236SMatthew Barth {
6861a5c6236SMatthew Barth if (lastServ != service)
6871a5c6236SMatthew Barth {
6882f359f72SMatthew Barth objMgrPaths = getPaths(
6892f359f72SMatthew Barth service, "org.freedesktop.DBus.ObjectManager");
6901a5c6236SMatthew Barth lastServ = service;
6911a5c6236SMatthew Barth }
69255627adfSMatthew Barth
6932f359f72SMatthew Barth // Look for the ObjectManager as an ancestor from the
6942f359f72SMatthew Barth // member.
695*dfddd648SPatrick Williams auto hasObjMgr = std::any_of(
696*dfddd648SPatrick Williams objMgrPaths.begin(), objMgrPaths.end(),
69755627adfSMatthew Barth [&member](const auto& path) {
69855627adfSMatthew Barth return member.find(path) != std::string::npos;
69955627adfSMatthew Barth });
70055627adfSMatthew Barth
70155627adfSMatthew Barth if (!hasObjMgr)
70255627adfSMatthew Barth {
70355627adfSMatthew Barth // No object manager interface provided for group member
70455627adfSMatthew Barth // Attempt to retrieve group member property directly
705f16f063bSMatt Spinler try
706f16f063bSMatt Spinler {
7071a5c6236SMatthew Barth auto value = util::SDBusPlus::getPropertyVariant<
7081a5c6236SMatthew Barth PropertyVariantType>(_bus, service, member,
7091a5c6236SMatthew Barth group.getInterface(),
71055627adfSMatthew Barth group.getProperty());
7111a5c6236SMatthew Barth setProperty(member, group.getInterface(),
7121a5c6236SMatthew Barth group.getProperty(), value);
713f16f063bSMatt Spinler }
714f16f063bSMatt Spinler catch (const std::exception& e)
715f16f063bSMatt Spinler {}
71655627adfSMatthew Barth continue;
717ade0c377SMatt Spinler }
71855627adfSMatthew Barth
7192f359f72SMatthew Barth if (services.find(service) == services.end())
720ade0c377SMatt Spinler {
7212f359f72SMatthew Barth services.insert(service);
72255627adfSMatthew Barth for (const auto& objMgrPath : objMgrPaths)
723ade0c377SMatt Spinler {
72455627adfSMatthew Barth // Get all managed objects from the service
7251a5c6236SMatthew Barth auto objects = util::SDBusPlus::getManagedObjects<
7261a5c6236SMatthew Barth PropertyVariantType>(_bus, service, objMgrPath);
72755627adfSMatthew Barth
72855627adfSMatthew Barth // Insert objects into cache
72955627adfSMatthew Barth insertFilteredObjects(objects);
730ade0c377SMatt Spinler }
73155627adfSMatthew Barth }
73255627adfSMatthew Barth }
7331a5c6236SMatthew Barth }
73495d73490SMatthew Barth catch (const util::DBusError&)
73555627adfSMatthew Barth {
73695d73490SMatthew Barth // No service or property found for group member with the
73795d73490SMatthew Barth // group's configured interface
73855627adfSMatthew Barth continue;
739ade0c377SMatt Spinler }
740ade0c377SMatt Spinler }
741ade0c377SMatt Spinler }
7422f359f72SMatthew Barth }
743ade0c377SMatt Spinler
timerExpired(TimerData & data)744d9cb63b6SMatthew Barth void Manager::timerExpired(TimerData& data)
745d9cb63b6SMatthew Barth {
746ade0c377SMatt Spinler if (std::get<bool>(data.second))
747ade0c377SMatt Spinler {
7482f359f72SMatthew Barth addGroups(std::get<const std::vector<Group>&>(data.second));
749ade0c377SMatt Spinler }
750ade0c377SMatt Spinler
751d9cb63b6SMatthew Barth auto& actions =
752d9cb63b6SMatthew Barth std::get<std::vector<std::unique_ptr<ActionBase>>&>(data.second);
753d9cb63b6SMatthew Barth // Perform the actions in the timer data
754d9cb63b6SMatthew Barth std::for_each(actions.begin(), actions.end(),
75500f6aa09SMatthew Barth [](auto& action) { action->run(); });
756d9cb63b6SMatthew Barth
757d9cb63b6SMatthew Barth // Remove oneshot timers after they expired
758d9cb63b6SMatthew Barth if (data.first == TimerType::oneshot)
759d9cb63b6SMatthew Barth {
760*dfddd648SPatrick Williams auto itTimer = std::find_if(
761*dfddd648SPatrick Williams _timers.begin(), _timers.end(), [&data](const auto& timer) {
762d9cb63b6SMatthew Barth return (data.first == timer.first->first &&
763d9cb63b6SMatthew Barth (std::get<std::string>(data.second) ==
764d9cb63b6SMatthew Barth std::get<std::string>(timer.first->second)));
765d9cb63b6SMatthew Barth });
766d9cb63b6SMatthew Barth if (itTimer != std::end(_timers))
767d9cb63b6SMatthew Barth {
768d9cb63b6SMatthew Barth _timers.erase(itTimer);
769d9cb63b6SMatthew Barth }
770d9cb63b6SMatthew Barth }
771d9cb63b6SMatthew Barth }
772d9cb63b6SMatthew Barth
handleSignal(sdbusplus::message_t & msg,const std::vector<SignalPkg> * pkgs)773cb356d48SPatrick Williams void Manager::handleSignal(sdbusplus::message_t& msg,
774c024d780SMatthew Barth const std::vector<SignalPkg>* pkgs)
775ebabc040SMatthew Barth {
776c024d780SMatthew Barth for (auto& pkg : *pkgs)
777ebabc040SMatthew Barth {
778ebabc040SMatthew Barth // Handle the signal callback and only run the actions if the handler
779ebabc040SMatthew Barth // updated the cache for the given SignalObject
780*dfddd648SPatrick Williams if (std::get<SignalHandler>(
781*dfddd648SPatrick Williams pkg)(msg, std::get<SignalObject>(pkg), *this))
782ebabc040SMatthew Barth {
783ebabc040SMatthew Barth // Perform the actions in the handler package
784d0ba86a3SMatt Spinler auto& actions = std::get<TriggerActions>(pkg);
785c3a69087SMatthew Barth std::for_each(actions.begin(), actions.end(), [](auto& action) {
786c3a69087SMatthew Barth if (action.get())
787c3a69087SMatthew Barth {
788c3a69087SMatthew Barth action.get()->run();
789c3a69087SMatthew Barth }
790c3a69087SMatthew Barth });
791ebabc040SMatthew Barth }
792c024d780SMatthew Barth // Only rewind message when not last package
793c024d780SMatthew Barth if (&pkg != &pkgs->back())
794c024d780SMatthew Barth {
795c024d780SMatthew Barth sd_bus_message_rewind(msg.get(), true);
796c024d780SMatthew Barth }
797ebabc040SMatthew Barth }
798ebabc040SMatthew Barth }
799ebabc040SMatthew Barth
setProfiles()800acd737cdSMatthew Barth void Manager::setProfiles()
801acd737cdSMatthew Barth {
802acd737cdSMatthew Barth // Profiles JSON config file is optional
803*dfddd648SPatrick Williams auto confFile =
804*dfddd648SPatrick Williams fan::JsonConfig::getConfFile(confAppName, Profile::confFileName, true);
805e91ac864SMatthew Barth
806e91ac864SMatthew Barth _profiles.clear();
807acd737cdSMatthew Barth if (!confFile.empty())
808acd737cdSMatthew Barth {
809acd737cdSMatthew Barth for (const auto& entry : fan::JsonConfig::load(confFile))
810acd737cdSMatthew Barth {
811acd737cdSMatthew Barth auto obj = std::make_unique<Profile>(entry);
812acd737cdSMatthew Barth _profiles.emplace(
813acd737cdSMatthew Barth std::make_pair(obj->getName(), obj->getProfiles()),
814acd737cdSMatthew Barth std::move(obj));
815acd737cdSMatthew Barth }
816acd737cdSMatthew Barth }
817e91ac864SMatthew Barth
818acd737cdSMatthew Barth // Ensure all configurations use the same set of active profiles
819acd737cdSMatthew Barth // (In case a profile's active state changes during configuration)
820e91ac864SMatthew Barth _activeProfiles.clear();
821acd737cdSMatthew Barth for (const auto& profile : _profiles)
822acd737cdSMatthew Barth {
823acd737cdSMatthew Barth if (profile.second->isActive())
824acd737cdSMatthew Barth {
825acd737cdSMatthew Barth _activeProfiles.emplace_back(profile.first.first);
826acd737cdSMatthew Barth }
827acd737cdSMatthew Barth }
828acd737cdSMatthew Barth }
829acd737cdSMatthew Barth
addParameterTrigger(const std::string & name,std::vector<std::unique_ptr<ActionBase>> & actions)830d0ba86a3SMatt Spinler void Manager::addParameterTrigger(
831d0ba86a3SMatt Spinler const std::string& name, std::vector<std::unique_ptr<ActionBase>>& actions)
832d0ba86a3SMatt Spinler {
833d0ba86a3SMatt Spinler auto it = _parameterTriggers.find(name);
834d0ba86a3SMatt Spinler if (it != _parameterTriggers.end())
835d0ba86a3SMatt Spinler {
836d0ba86a3SMatt Spinler std::for_each(actions.begin(), actions.end(),
837d0ba86a3SMatt Spinler [&actList = it->second](auto& action) {
838d0ba86a3SMatt Spinler actList.emplace_back(std::ref(action));
839d0ba86a3SMatt Spinler });
840d0ba86a3SMatt Spinler }
841d0ba86a3SMatt Spinler else
842d0ba86a3SMatt Spinler {
843d0ba86a3SMatt Spinler TriggerActions triggerActions;
844d0ba86a3SMatt Spinler std::for_each(actions.begin(), actions.end(),
845d0ba86a3SMatt Spinler [&triggerActions](auto& action) {
846d0ba86a3SMatt Spinler triggerActions.emplace_back(std::ref(action));
847d0ba86a3SMatt Spinler });
848d0ba86a3SMatt Spinler _parameterTriggers[name] = std::move(triggerActions);
849d0ba86a3SMatt Spinler }
850d0ba86a3SMatt Spinler }
851d0ba86a3SMatt Spinler
runParameterActions(const std::string & name)852d0ba86a3SMatt Spinler void Manager::runParameterActions(const std::string& name)
853d0ba86a3SMatt Spinler {
854d0ba86a3SMatt Spinler auto it = _parameterTriggers.find(name);
855d0ba86a3SMatt Spinler if (it != _parameterTriggers.end())
856d0ba86a3SMatt Spinler {
857d0ba86a3SMatt Spinler std::for_each(it->second.begin(), it->second.end(),
858d0ba86a3SMatt Spinler [](auto& action) { action.get()->run(); });
859d0ba86a3SMatt Spinler }
860d0ba86a3SMatt Spinler }
861d0ba86a3SMatt Spinler
862a227a16dSMatthew Barth } // namespace phosphor::fan::control::json
863