1 /**
2 * Copyright © 2022 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "config.h"
17
18 #include "manager.hpp"
19
20 #include "action.hpp"
21 #include "dbus_paths.hpp"
22 #include "event.hpp"
23 #include "fan.hpp"
24 #include "group.hpp"
25 #include "json_config.hpp"
26 #include "power_state.hpp"
27 #include "profile.hpp"
28 #include "sdbusplus.hpp"
29 #include "utils/flight_recorder.hpp"
30 #include "zone.hpp"
31
32 #include <systemd/sd-bus.h>
33
34 #include <nlohmann/json.hpp>
35 #include <sdbusplus/bus.hpp>
36 #include <sdbusplus/server/manager.hpp>
37 #include <sdeventplus/event.hpp>
38 #include <sdeventplus/utility/timer.hpp>
39
40 #include <algorithm>
41 #include <chrono>
42 #include <filesystem>
43 #include <functional>
44 #include <map>
45 #include <memory>
46 #include <tuple>
47 #include <utility>
48 #include <vector>
49
50 namespace phosphor::fan::control::json
51 {
52
53 using json = nlohmann::json;
54
55 std::vector<std::string> Manager::_activeProfiles;
56 std::map<std::string,
57 std::map<std::string, std::pair<bool, std::vector<std::string>>>>
58 Manager::_servTree;
59 std::map<std::string,
60 std::map<std::string, std::map<std::string, PropertyVariantType>>>
61 Manager::_objects;
62 std::unordered_map<std::string, PropertyVariantType> Manager::_parameters;
63 std::unordered_map<std::string, TriggerActions> Manager::_parameterTriggers;
64
65 const std::string Manager::dumpFile = "/tmp/fan_control_dump.json";
66
Manager(const sdeventplus::Event & event)67 Manager::Manager(const sdeventplus::Event& event) :
68 _bus(util::SDBusPlus::getBus()), _event(event),
69 _mgr(util::SDBusPlus::getBus(), CONTROL_OBJPATH), _loadAllowed(true),
70 _powerState(std::make_unique<PGoodState>(
71 util::SDBusPlus::getBus(),
72 std::bind(std::mem_fn(&Manager::powerStateChanged), this,
73 std::placeholders::_1)))
74 {}
75
sighupHandler(sdeventplus::source::Signal &,const struct signalfd_siginfo *)76 void Manager::sighupHandler(sdeventplus::source::Signal&,
77 const struct signalfd_siginfo*)
78 {
79 FlightRecorder::instance().log("main", "SIGHUP received");
80 // Save current set of available and active profiles
81 std::map<configKey, std::unique_ptr<Profile>> profiles;
82 profiles.swap(_profiles);
83 std::vector<std::string> activeProfiles;
84 activeProfiles.swap(_activeProfiles);
85
86 try
87 {
88 _loadAllowed = true;
89 load();
90 }
91 catch (const std::runtime_error& re)
92 {
93 // Restore saved available and active profiles
94 _loadAllowed = false;
95 _profiles.swap(profiles);
96 _activeProfiles.swap(activeProfiles);
97 log<level::ERR>("Error reloading configs, no changes made",
98 entry("LOAD_ERROR=%s", re.what()));
99 FlightRecorder::instance().log(
100 "main", std::format("Error reloading configs, no changes made: {}",
101 re.what()));
102 }
103 }
104
dumpDebugData(sdeventplus::source::Signal &,const struct signalfd_siginfo *)105 void Manager::dumpDebugData(sdeventplus::source::Signal&,
106 const struct signalfd_siginfo*)
107 {
108 json data;
109 FlightRecorder::instance().dump(data);
110 dumpCache(data);
111
112 std::for_each(_zones.begin(), _zones.end(), [&data](const auto& zone) {
113 data["zones"][zone.second->getName()] = zone.second->dump();
114 });
115
116 std::ofstream file{Manager::dumpFile};
117 if (!file)
118 {
119 log<level::ERR>("Could not open file for fan dump");
120 return;
121 }
122
123 file << std::setw(4) << data;
124 }
125
dumpCache(json & data)126 void Manager::dumpCache(json& data)
127 {
128 auto& objects = data["objects"];
129 for (const auto& [path, interfaces] : _objects)
130 {
131 auto& interfaceJSON = objects[path];
132
133 for (const auto& [interface, properties] : interfaces)
134 {
135 auto& propertyJSON = interfaceJSON[interface];
136 for (const auto& [propName, propValue] : properties)
137 {
138 std::visit(
139 [&obj = propertyJSON[propName]](auto&& val) { obj = val; },
140 propValue);
141 }
142 }
143 }
144
145 auto& parameters = data["parameters"];
146 for (const auto& [name, value] : _parameters)
147 {
148 std::visit([&obj = parameters[name]](auto&& val) { obj = val; }, value);
149 }
150
151 std::for_each(_events.begin(), _events.end(), [&data](const auto& event) {
152 data["events"][event.second->getName()] = event.second->dump();
153 });
154
155 data["services"] = _servTree;
156 }
157
load()158 void Manager::load()
159 {
160 if (_loadAllowed)
161 {
162 // Load the available profiles and which are active
163 setProfiles();
164
165 // Load the zone configurations
166 auto zones = getConfig<Zone>(false, _event, this);
167 // Load the fan configurations and move each fan into its zone
168 auto fans = getConfig<Fan>(false);
169 for (auto& fan : fans)
170 {
171 configKey fanProfile = std::make_pair(fan.second->getZone(),
172 fan.first.second);
173 auto itZone = std::find_if(zones.begin(), zones.end(),
174 [&fanProfile](const auto& zone) {
175 return Manager::inConfig(fanProfile, zone.first);
176 });
177 if (itZone != zones.end())
178 {
179 if (itZone->second->getTarget() != fan.second->getTarget() &&
180 fan.second->getTarget() != 0)
181 {
182 // Update zone target to current target of the fan in the
183 // zone
184 itZone->second->setTarget(fan.second->getTarget());
185 }
186 itZone->second->addFan(std::move(fan.second));
187 }
188 }
189
190 // Save all currently available groups, if any, then clear for reloading
191 auto groups = std::move(Event::getAllGroups(false));
192 Event::clearAllGroups();
193
194 std::map<configKey, std::unique_ptr<Event>> events;
195 try
196 {
197 // Load any events configured, including all the groups
198 events = getConfig<Event>(true, this, zones);
199 }
200 catch (const std::runtime_error& re)
201 {
202 // Restore saved set of all available groups for current events
203 Event::setAllGroups(std::move(groups));
204 throw re;
205 }
206
207 // Enable zones
208 _zones = std::move(zones);
209 std::for_each(_zones.begin(), _zones.end(),
210 [](const auto& entry) { entry.second->enable(); });
211
212 // Clear current timers and signal subscriptions before enabling events
213 // To save reloading services and/or objects into cache, do not clear
214 // cache
215 _timers.clear();
216 _signals.clear();
217
218 // Enable events
219 _events = std::move(events);
220 FlightRecorder::instance().log("main", "Enabling events");
221 std::for_each(_events.begin(), _events.end(),
222 [](const auto& entry) { entry.second->enable(); });
223 FlightRecorder::instance().log("main", "Done enabling events");
224
225 _loadAllowed = false;
226 }
227 }
228
powerStateChanged(bool powerStateOn)229 void Manager::powerStateChanged(bool powerStateOn)
230 {
231 if (powerStateOn)
232 {
233 FlightRecorder::instance().log("power", "Power on");
234 if (_zones.empty())
235 {
236 throw std::runtime_error("No configured zones found at poweron");
237 }
238 std::for_each(_zones.begin(), _zones.end(), [](const auto& entry) {
239 entry.second->setTarget(entry.second->getPoweronTarget());
240 });
241
242 // Tell events to run their power on triggers
243 std::for_each(_events.begin(), _events.end(),
244 [](const auto& entry) { entry.second->powerOn(); });
245 }
246 else
247 {
248 FlightRecorder::instance().log("power", "Power off");
249 // Tell events to run their power off triggers
250 std::for_each(_events.begin(), _events.end(),
251 [](const auto& entry) { entry.second->powerOff(); });
252 }
253 }
254
getActiveProfiles()255 const std::vector<std::string>& Manager::getActiveProfiles()
256 {
257 return _activeProfiles;
258 }
259
inConfig(const configKey & input,const configKey & comp)260 bool Manager::inConfig(const configKey& input, const configKey& comp)
261 {
262 // Config names dont match, do not include in config
263 if (input.first != comp.first)
264 {
265 return false;
266 }
267 // No profiles specified by input config, can be used in any config
268 if (input.second.empty())
269 {
270 return true;
271 }
272 else
273 {
274 // Profiles must have one match in the other's profiles(and they must be
275 // an active profile) to be used in the config
276 return std::any_of(input.second.begin(), input.second.end(),
277 [&comp](const auto& lProfile) {
278 return std::any_of(comp.second.begin(), comp.second.end(),
279 [&lProfile](const auto& rProfile) {
280 if (lProfile != rProfile)
281 {
282 return false;
283 }
284 auto activeProfs = getActiveProfiles();
285 return std::find(activeProfs.begin(), activeProfs.end(),
286 lProfile) != activeProfs.end();
287 });
288 });
289 }
290 }
291
hasOwner(const std::string & path,const std::string & intf)292 bool Manager::hasOwner(const std::string& path, const std::string& intf)
293 {
294 auto itServ = _servTree.find(path);
295 if (itServ == _servTree.end())
296 {
297 // Path not found in cache, therefore owner missing
298 return false;
299 }
300 for (const auto& service : itServ->second)
301 {
302 auto itIntf = std::find_if(
303 service.second.second.begin(), service.second.second.end(),
304 [&intf](const auto& interface) { return intf == interface; });
305 if (itIntf != std::end(service.second.second))
306 {
307 // Service found, return owner state
308 return service.second.first;
309 }
310 }
311 // Interface not found in cache, therefore owner missing
312 return false;
313 }
314
setOwner(const std::string & serv,bool hasOwner)315 void Manager::setOwner(const std::string& serv, bool hasOwner)
316 {
317 // Update owner state on all entries of `serv`
318 for (auto& itPath : _servTree)
319 {
320 auto itServ = itPath.second.find(serv);
321 if (itServ != itPath.second.end())
322 {
323 itServ->second.first = hasOwner;
324
325 // Remove associated interfaces from object cache when service no
326 // longer has an owner
327 if (!hasOwner && _objects.find(itPath.first) != _objects.end())
328 {
329 for (auto& intf : itServ->second.second)
330 {
331 _objects[itPath.first].erase(intf);
332 }
333 }
334 }
335 }
336 }
337
setOwner(const std::string & path,const std::string & serv,const std::string & intf,bool isOwned)338 void Manager::setOwner(const std::string& path, const std::string& serv,
339 const std::string& intf, bool isOwned)
340 {
341 // Set owner state for specific object given
342 auto& ownIntf = _servTree[path][serv];
343 ownIntf.first = isOwned;
344 auto itIntf = std::find_if(
345 ownIntf.second.begin(), ownIntf.second.end(),
346 [&intf](const auto& interface) { return intf == interface; });
347 if (itIntf == std::end(ownIntf.second))
348 {
349 ownIntf.second.emplace_back(intf);
350 }
351
352 // Update owner state on all entries of the same `serv` & `intf`
353 for (auto& itPath : _servTree)
354 {
355 if (itPath.first == path)
356 {
357 // Already set/updated owner on this path for `serv` & `intf`
358 continue;
359 }
360 for (auto& itServ : itPath.second)
361 {
362 if (itServ.first != serv)
363 {
364 continue;
365 }
366 auto itIntf = std::find_if(
367 itServ.second.second.begin(), itServ.second.second.end(),
368 [&intf](const auto& interface) { return intf == interface; });
369 if (itIntf != std::end(itServ.second.second))
370 {
371 itServ.second.first = isOwned;
372 }
373 }
374 }
375 }
376
findService(const std::string & path,const std::string & intf)377 const std::string& Manager::findService(const std::string& path,
378 const std::string& intf)
379 {
380 static const std::string empty = "";
381
382 auto itServ = _servTree.find(path);
383 if (itServ != _servTree.end())
384 {
385 for (const auto& service : itServ->second)
386 {
387 auto itIntf = std::find_if(
388 service.second.second.begin(), service.second.second.end(),
389 [&intf](const auto& interface) { return intf == interface; });
390 if (itIntf != std::end(service.second.second))
391 {
392 // Service found, return service name
393 return service.first;
394 }
395 }
396 }
397
398 return empty;
399 }
400
addServices(const std::string & intf,int32_t depth)401 void Manager::addServices(const std::string& intf, int32_t depth)
402 {
403 // Get all subtree objects for the given interface
404 auto objects = util::SDBusPlus::getSubTreeRaw(util::SDBusPlus::getBus(),
405 "/", intf, depth);
406 // Add what's returned to the cache of path->services
407 for (auto& itPath : objects)
408 {
409 auto pathIter = _servTree.find(itPath.first);
410 if (pathIter != _servTree.end())
411 {
412 // Path found in cache
413 for (auto& itServ : itPath.second)
414 {
415 auto servIter = pathIter->second.find(itServ.first);
416 if (servIter != pathIter->second.end())
417 {
418 if (std::find(servIter->second.second.begin(),
419 servIter->second.second.end(),
420 intf) == servIter->second.second.end())
421 {
422 // Add interface to cache
423 servIter->second.second.emplace_back(intf);
424 }
425 }
426 else
427 {
428 // Service not found in cache
429 auto intfs = {intf};
430 pathIter->second[itServ.first] = std::make_pair(true,
431 intfs);
432 }
433 }
434 }
435 else
436 {
437 // Path not found in cache
438 auto intfs = {intf};
439 for (const auto& [servName, servIntfs] : itPath.second)
440 {
441 _servTree[itPath.first][servName] = std::make_pair(true, intfs);
442 }
443 }
444 }
445 }
446
getService(const std::string & path,const std::string & intf)447 const std::string& Manager::getService(const std::string& path,
448 const std::string& intf)
449 {
450 // Retrieve service from cache
451 const auto& serviceName = findService(path, intf);
452 if (serviceName.empty())
453 {
454 addServices(intf, 0);
455 return findService(path, intf);
456 }
457
458 return serviceName;
459 }
460
findPaths(const std::string & serv,const std::string & intf)461 std::vector<std::string> Manager::findPaths(const std::string& serv,
462 const std::string& intf)
463 {
464 std::vector<std::string> paths;
465
466 for (const auto& path : _servTree)
467 {
468 auto itServ = path.second.find(serv);
469 if (itServ != path.second.end())
470 {
471 if (std::find(itServ->second.second.begin(),
472 itServ->second.second.end(),
473 intf) != itServ->second.second.end())
474 {
475 if (std::find(paths.begin(), paths.end(), path.first) ==
476 paths.end())
477 {
478 paths.push_back(path.first);
479 }
480 }
481 }
482 }
483
484 return paths;
485 }
486
getPaths(const std::string & serv,const std::string & intf)487 std::vector<std::string> Manager::getPaths(const std::string& serv,
488 const std::string& intf)
489 {
490 auto paths = findPaths(serv, intf);
491 if (paths.empty())
492 {
493 addServices(intf, 0);
494 return findPaths(serv, intf);
495 }
496
497 return paths;
498 }
499
insertFilteredObjects(ManagedObjects & ref)500 void Manager::insertFilteredObjects(ManagedObjects& ref)
501 {
502 // Filter out objects that aren't part of a group
503 const auto& allGroupMembers = Group::getAllMembers();
504 auto it = ref.begin();
505
506 while (it != ref.end())
507 {
508 if (allGroupMembers.find(it->first) == allGroupMembers.end())
509 {
510 it = ref.erase(it);
511 }
512 else
513 {
514 it++;
515 }
516 }
517
518 for (auto& [path, pathMap] : ref)
519 {
520 for (auto& [intf, intfMap] : pathMap)
521 {
522 // for each property on this path+interface
523 for (auto& [prop, value] : intfMap)
524 {
525 setProperty(path, intf, prop, value);
526 }
527 }
528 }
529 }
530
addObjects(const std::string & path,const std::string & intf,const std::string & prop,const std::string & serviceName)531 void Manager::addObjects(const std::string& path, const std::string& intf,
532 const std::string& prop,
533 const std::string& serviceName)
534 {
535 auto service = serviceName;
536 if (service.empty())
537 {
538 service = getService(path, intf);
539 if (service.empty())
540 {
541 // Log service not found for object
542 log<level::DEBUG>(
543 std::format(
544 "Unable to get service name for path {}, interface {}",
545 path, intf)
546 .c_str());
547 return;
548 }
549 }
550 else
551 {
552 // The service is known, so the service cache can be
553 // populated even if the path itself isn't present.
554 const auto& s = findService(path, intf);
555 if (s.empty())
556 {
557 addServices(intf, 0);
558 }
559 }
560
561 auto objMgrPaths = getPaths(service, "org.freedesktop.DBus.ObjectManager");
562
563 bool useManagedObj = false;
564
565 if (!objMgrPaths.empty())
566 {
567 for (const auto& objMgrPath : objMgrPaths)
568 {
569 // Get all managed objects of service
570 auto objects =
571 util::SDBusPlus::getManagedObjects<PropertyVariantType>(
572 _bus, service, objMgrPath);
573 if (objects.size() > 0)
574 {
575 useManagedObj = true;
576 }
577 // insert all objects that are in groups but remove any NaN values
578 insertFilteredObjects(objects);
579 }
580 }
581
582 if (!useManagedObj)
583 {
584 // No object manager interface provided by service?
585 // Or no object is managed?
586 // Attempt to retrieve property directly
587 try
588 {
589 auto value =
590 util::SDBusPlus::getPropertyVariant<PropertyVariantType>(
591 _bus, service, path, intf, prop);
592
593 setProperty(path, intf, prop, value);
594 }
595 catch (const std::exception& e)
596 {}
597 return;
598 }
599 }
600
601 const std::optional<PropertyVariantType>
getProperty(const std::string & path,const std::string & intf,const std::string & prop)602 Manager::getProperty(const std::string& path, const std::string& intf,
603 const std::string& prop)
604 {
605 // TODO Objects hosted by fan control (i.e. ThermalMode) are required to
606 // update the cache upon being set/updated
607 auto itPath = _objects.find(path);
608 if (itPath != _objects.end())
609 {
610 auto itIntf = itPath->second.find(intf);
611 if (itIntf != itPath->second.end())
612 {
613 auto itProp = itIntf->second.find(prop);
614 if (itProp != itIntf->second.end())
615 {
616 return itProp->second;
617 }
618 }
619 }
620
621 return std::nullopt;
622 }
623
setProperty(const std::string & path,const std::string & intf,const std::string & prop,PropertyVariantType value)624 void Manager::setProperty(const std::string& path, const std::string& intf,
625 const std::string& prop, PropertyVariantType value)
626 {
627 // filter NaNs out of the cache
628 if (PropertyContainsNan(value))
629 {
630 // dont use operator [] if paths dont exist
631 if (_objects.find(path) != _objects.end() &&
632 _objects[path].find(intf) != _objects[path].end())
633 {
634 _objects[path][intf].erase(prop);
635 }
636 }
637 else
638 {
639 _objects[path][intf][prop] = std::move(value);
640 }
641 }
642
addTimer(const TimerType type,const std::chrono::microseconds interval,std::unique_ptr<TimerPkg> pkg)643 void Manager::addTimer(const TimerType type,
644 const std::chrono::microseconds interval,
645 std::unique_ptr<TimerPkg> pkg)
646 {
647 auto dataPtr =
648 std::make_unique<TimerData>(std::make_pair(type, std::move(*pkg)));
649 Timer timer(_event,
650 std::bind(&Manager::timerExpired, this, std::ref(*dataPtr)));
651 if (type == TimerType::repeating)
652 {
653 timer.restart(interval);
654 }
655 else if (type == TimerType::oneshot)
656 {
657 timer.restartOnce(interval);
658 }
659 else
660 {
661 throw std::invalid_argument("Invalid Timer Type");
662 }
663 _timers.emplace_back(std::move(dataPtr), std::move(timer));
664 }
665
addGroups(const std::vector<Group> & groups)666 void Manager::addGroups(const std::vector<Group>& groups)
667 {
668 std::string lastServ;
669 std::vector<std::string> objMgrPaths;
670 std::set<std::string> services;
671 for (const auto& group : groups)
672 {
673 for (const auto& member : group.getMembers())
674 {
675 try
676 {
677 auto service = group.getService();
678 if (service.empty())
679 {
680 service = getService(member, group.getInterface());
681 }
682
683 if (!service.empty())
684 {
685 if (lastServ != service)
686 {
687 objMgrPaths = getPaths(
688 service, "org.freedesktop.DBus.ObjectManager");
689 lastServ = service;
690 }
691
692 // Look for the ObjectManager as an ancestor from the
693 // member.
694 auto hasObjMgr = std::any_of(objMgrPaths.begin(),
695 objMgrPaths.end(),
696 [&member](const auto& path) {
697 return member.find(path) != std::string::npos;
698 });
699
700 if (!hasObjMgr)
701 {
702 // No object manager interface provided for group member
703 // Attempt to retrieve group member property directly
704 try
705 {
706 auto value = util::SDBusPlus::getPropertyVariant<
707 PropertyVariantType>(_bus, service, member,
708 group.getInterface(),
709 group.getProperty());
710 setProperty(member, group.getInterface(),
711 group.getProperty(), value);
712 }
713 catch (const std::exception& e)
714 {}
715 continue;
716 }
717
718 if (services.find(service) == services.end())
719 {
720 services.insert(service);
721 for (const auto& objMgrPath : objMgrPaths)
722 {
723 // Get all managed objects from the service
724 auto objects = util::SDBusPlus::getManagedObjects<
725 PropertyVariantType>(_bus, service, objMgrPath);
726
727 // Insert objects into cache
728 insertFilteredObjects(objects);
729 }
730 }
731 }
732 }
733 catch (const util::DBusError&)
734 {
735 // No service or property found for group member with the
736 // group's configured interface
737 continue;
738 }
739 }
740 }
741 }
742
timerExpired(TimerData & data)743 void Manager::timerExpired(TimerData& data)
744 {
745 if (std::get<bool>(data.second))
746 {
747 addGroups(std::get<const std::vector<Group>&>(data.second));
748 }
749
750 auto& actions =
751 std::get<std::vector<std::unique_ptr<ActionBase>>&>(data.second);
752 // Perform the actions in the timer data
753 std::for_each(actions.begin(), actions.end(),
754 [](auto& action) { action->run(); });
755
756 // Remove oneshot timers after they expired
757 if (data.first == TimerType::oneshot)
758 {
759 auto itTimer = std::find_if(_timers.begin(), _timers.end(),
760 [&data](const auto& timer) {
761 return (data.first == timer.first->first &&
762 (std::get<std::string>(data.second) ==
763 std::get<std::string>(timer.first->second)));
764 });
765 if (itTimer != std::end(_timers))
766 {
767 _timers.erase(itTimer);
768 }
769 }
770 }
771
handleSignal(sdbusplus::message_t & msg,const std::vector<SignalPkg> * pkgs)772 void Manager::handleSignal(sdbusplus::message_t& msg,
773 const std::vector<SignalPkg>* pkgs)
774 {
775 for (auto& pkg : *pkgs)
776 {
777 // Handle the signal callback and only run the actions if the handler
778 // updated the cache for the given SignalObject
779 if (std::get<SignalHandler>(pkg)(msg, std::get<SignalObject>(pkg),
780 *this))
781 {
782 // Perform the actions in the handler package
783 auto& actions = std::get<TriggerActions>(pkg);
784 std::for_each(actions.begin(), actions.end(), [](auto& action) {
785 if (action.get())
786 {
787 action.get()->run();
788 }
789 });
790 }
791 // Only rewind message when not last package
792 if (&pkg != &pkgs->back())
793 {
794 sd_bus_message_rewind(msg.get(), true);
795 }
796 }
797 }
798
setProfiles()799 void Manager::setProfiles()
800 {
801 // Profiles JSON config file is optional
802 auto confFile = fan::JsonConfig::getConfFile(confAppName,
803 Profile::confFileName, true);
804
805 _profiles.clear();
806 if (!confFile.empty())
807 {
808 for (const auto& entry : fan::JsonConfig::load(confFile))
809 {
810 auto obj = std::make_unique<Profile>(entry);
811 _profiles.emplace(
812 std::make_pair(obj->getName(), obj->getProfiles()),
813 std::move(obj));
814 }
815 }
816
817 // Ensure all configurations use the same set of active profiles
818 // (In case a profile's active state changes during configuration)
819 _activeProfiles.clear();
820 for (const auto& profile : _profiles)
821 {
822 if (profile.second->isActive())
823 {
824 _activeProfiles.emplace_back(profile.first.first);
825 }
826 }
827 }
828
addParameterTrigger(const std::string & name,std::vector<std::unique_ptr<ActionBase>> & actions)829 void Manager::addParameterTrigger(
830 const std::string& name, std::vector<std::unique_ptr<ActionBase>>& actions)
831 {
832 auto it = _parameterTriggers.find(name);
833 if (it != _parameterTriggers.end())
834 {
835 std::for_each(actions.begin(), actions.end(),
836 [&actList = it->second](auto& action) {
837 actList.emplace_back(std::ref(action));
838 });
839 }
840 else
841 {
842 TriggerActions triggerActions;
843 std::for_each(actions.begin(), actions.end(),
844 [&triggerActions](auto& action) {
845 triggerActions.emplace_back(std::ref(action));
846 });
847 _parameterTriggers[name] = std::move(triggerActions);
848 }
849 }
850
runParameterActions(const std::string & name)851 void Manager::runParameterActions(const std::string& name)
852 {
853 auto it = _parameterTriggers.find(name);
854 if (it != _parameterTriggers.end())
855 {
856 std::for_each(it->second.begin(), it->second.end(),
857 [](auto& action) { action.get()->run(); });
858 }
859 }
860
861 } // namespace phosphor::fan::control::json
862