blob: b837dad0ba4aaeca8dfeff469411e0365567ed94 [file] [log] [blame]
Matthew Bartha227a162020-08-05 10:51:45 -05001/**
2 * Copyright © 2020 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 */
Matthew Barthb584d812021-03-11 15:55:04 -060016#include "config.h"
17
Matthew Bartha227a162020-08-05 10:51:45 -050018#include "manager.hpp"
19
Matt Spinler2fc0a352021-10-04 15:10:57 -050020#include "../utils/flight_recorder.hpp"
Matthew Barthd9cb63b2021-03-24 14:36:49 -050021#include "action.hpp"
Matthew Barth44ab7692021-03-26 11:40:10 -050022#include "event.hpp"
Matthew Barthde90fb42021-03-04 16:34:28 -060023#include "fan.hpp"
Matthew Barthd9cb63b2021-03-24 14:36:49 -050024#include "group.hpp"
Matthew Bartha227a162020-08-05 10:51:45 -050025#include "json_config.hpp"
Matthew Barth48f44da2021-05-27 15:43:34 -050026#include "power_state.hpp"
Matthew Barth06764942021-03-04 09:28:40 -060027#include "profile.hpp"
Matthew Barth9403a212021-05-17 09:31:50 -050028#include "sdbusplus.hpp"
Matthew Barthacd737c2021-03-04 11:04:01 -060029#include "zone.hpp"
Matthew Bartha227a162020-08-05 10:51:45 -050030
Matthew Barthacd737c2021-03-04 11:04:01 -060031#include <nlohmann/json.hpp>
Matthew Bartha227a162020-08-05 10:51:45 -050032#include <sdbusplus/bus.hpp>
Matthew Barth1542fb52021-06-10 14:09:09 -050033#include <sdbusplus/server/manager.hpp>
Matthew Barthacd737c2021-03-04 11:04:01 -060034#include <sdeventplus/event.hpp>
Matthew Barthd9cb63b2021-03-24 14:36:49 -050035#include <sdeventplus/utility/timer.hpp>
Matthew Bartha227a162020-08-05 10:51:45 -050036
Matthew Barthde90fb42021-03-04 16:34:28 -060037#include <algorithm>
Matthew Barthd9cb63b2021-03-24 14:36:49 -050038#include <chrono>
Matthew Bartha227a162020-08-05 10:51:45 -050039#include <filesystem>
Matthew Barthd9cb63b2021-03-24 14:36:49 -050040#include <functional>
41#include <map>
42#include <memory>
43#include <tuple>
44#include <utility>
Matthew Barth06764942021-03-04 09:28:40 -060045#include <vector>
Matthew Bartha227a162020-08-05 10:51:45 -050046
47namespace phosphor::fan::control::json
48{
49
Matthew Barthacd737c2021-03-04 11:04:01 -060050using json = nlohmann::json;
51
52std::vector<std::string> Manager::_activeProfiles;
Matthew Barth12cb1252021-03-08 16:47:30 -060053std::map<std::string,
Matthew Barth4ca87fa2021-04-14 11:31:13 -050054 std::map<std::string, std::pair<bool, std::vector<std::string>>>>
Matthew Barth12cb1252021-03-08 16:47:30 -060055 Manager::_servTree;
Matthew Barth07fecfc2021-01-29 09:04:43 -060056std::map<std::string,
57 std::map<std::string, std::map<std::string, PropertyVariantType>>>
58 Manager::_objects;
Matt Spinlerd76351b2021-08-05 16:23:09 -050059std::unordered_map<std::string, PropertyVariantType> Manager::_parameters;
Matthew Barthacd737c2021-03-04 11:04:01 -060060
Matt Spinler7787def2021-10-14 16:33:16 -050061const std::string Manager::dumpFile = "/tmp/fan_control_dump.json";
62
Matthew Barth9403a212021-05-17 09:31:50 -050063Manager::Manager(const sdeventplus::Event& event) :
Matthew Barth48f44da2021-05-27 15:43:34 -050064 _bus(util::SDBusPlus::getBus()), _event(event),
Matthew Barth3770a1d2021-06-10 15:09:37 -050065 _mgr(util::SDBusPlus::getBus(), CONTROL_OBJPATH), _loadAllowed(true),
Matthew Barth48f44da2021-05-27 15:43:34 -050066 _powerState(std::make_unique<PGoodState>(
67 util::SDBusPlus::getBus(),
68 std::bind(std::mem_fn(&Manager::powerStateChanged), this,
69 std::placeholders::_1)))
Matthew Barth3770a1d2021-06-10 15:09:37 -050070{}
Matthew Barthe91ac862021-05-25 16:22:17 -050071
72void Manager::sighupHandler(sdeventplus::source::Signal&,
73 const struct signalfd_siginfo*)
74{
75 // Save current set of available and active profiles
76 std::map<configKey, std::unique_ptr<Profile>> profiles;
77 profiles.swap(_profiles);
78 std::vector<std::string> activeProfiles;
79 activeProfiles.swap(_activeProfiles);
80
81 try
82 {
Matthew Barth3770a1d2021-06-10 15:09:37 -050083 _loadAllowed = true;
Matthew Barthe91ac862021-05-25 16:22:17 -050084 load();
85 }
Patrick Williamsddb773b2021-10-06 11:24:49 -050086 catch (const std::runtime_error& re)
Matthew Barthe91ac862021-05-25 16:22:17 -050087 {
88 // Restore saved available and active profiles
Matthew Barth3770a1d2021-06-10 15:09:37 -050089 _loadAllowed = false;
Matthew Barthe91ac862021-05-25 16:22:17 -050090 _profiles.swap(profiles);
91 _activeProfiles.swap(activeProfiles);
92 log<level::ERR>("Error reloading configs, no changes made",
93 entry("LOAD_ERROR=%s", re.what()));
94 }
95}
96
Matt Spinler2fc0a352021-10-04 15:10:57 -050097void Manager::sigUsr1Handler(sdeventplus::source::Signal&,
98 const struct signalfd_siginfo*)
99{
Matt Spinler7787def2021-10-14 16:33:16 -0500100 debugDumpEventSource = std::make_unique<sdeventplus::source::Defer>(
101 _event, std::bind(std::mem_fn(&Manager::dumpDebugData), this,
Matt Spinler2fc0a352021-10-04 15:10:57 -0500102 std::placeholders::_1));
103}
104
Matt Spinler7787def2021-10-14 16:33:16 -0500105void Manager::dumpDebugData(sdeventplus::source::EventBase& /*source*/)
Matt Spinler2fc0a352021-10-04 15:10:57 -0500106{
Matt Spinler7787def2021-10-14 16:33:16 -0500107 json data;
108 FlightRecorder::instance().dump(data);
Matt Spinlerb5c21a22021-10-14 16:52:12 -0500109 dumpCache(data);
Matt Spinler7787def2021-10-14 16:33:16 -0500110
Matt Spinler9db6dd12021-10-29 16:10:08 -0500111 std::for_each(_zones.begin(), _zones.end(), [&data](const auto& zone) {
112 data["zones"][zone.second->getName()] = zone.second->dump();
113 });
114
Matt Spinler7787def2021-10-14 16:33:16 -0500115 std::ofstream file{Manager::dumpFile};
116 if (!file)
117 {
118 log<level::ERR>("Could not open file for fan dump");
119 return;
120 }
121
122 file << std::setw(4) << data;
123
124 debugDumpEventSource.reset();
Matt Spinler2fc0a352021-10-04 15:10:57 -0500125}
126
Matt Spinlerb5c21a22021-10-14 16:52:12 -0500127void Manager::dumpCache(json& data)
128{
129 auto& objects = data["objects"];
130 for (const auto& [path, interfaces] : _objects)
131 {
132 auto& interfaceJSON = objects[path];
133
134 for (const auto& [interface, properties] : interfaces)
135 {
136 auto& propertyJSON = interfaceJSON[interface];
137 for (const auto& [propName, propValue] : properties)
138 {
139 std::visit(
140 [&obj = propertyJSON[propName]](auto&& val) { obj = val; },
141 propValue);
142 }
143 }
144 }
145
146 auto& parameters = data["parameters"];
147 for (const auto& [name, value] : _parameters)
148 {
149 std::visit([&obj = parameters["name"]](auto&& val) { obj = val; },
150 value);
151 }
152
153 data["services"] = _servTree;
154}
155
Matthew Barthe91ac862021-05-25 16:22:17 -0500156void Manager::load()
157{
Matthew Barth3770a1d2021-06-10 15:09:37 -0500158 if (_loadAllowed)
Matthew Barthde90fb42021-03-04 16:34:28 -0600159 {
Matthew Barth3770a1d2021-06-10 15:09:37 -0500160 // Load the available profiles and which are active
161 setProfiles();
162
163 // Load the zone configurations
164 auto zones = getConfig<Zone>(false, _event, this);
165 // Load the fan configurations and move each fan into its zone
166 auto fans = getConfig<Fan>(false);
167 for (auto& fan : fans)
Matthew Barthde90fb42021-03-04 16:34:28 -0600168 {
Matthew Barth3770a1d2021-06-10 15:09:37 -0500169 configKey fanProfile =
170 std::make_pair(fan.second->getZone(), fan.first.second);
171 auto itZone = std::find_if(
172 zones.begin(), zones.end(), [&fanProfile](const auto& zone) {
173 return Manager::inConfig(fanProfile, zone.first);
174 });
175 if (itZone != zones.end())
Matthew Barth6f787302021-03-25 15:01:01 -0500176 {
Matthew Barth3770a1d2021-06-10 15:09:37 -0500177 if (itZone->second->getTarget() != fan.second->getTarget() &&
178 fan.second->getTarget() != 0)
179 {
180 // Update zone target to current target of the fan in the
181 // zone
182 itZone->second->setTarget(fan.second->getTarget());
183 }
184 itZone->second->addFan(std::move(fan.second));
Matthew Barth6f787302021-03-25 15:01:01 -0500185 }
Matthew Barthde90fb42021-03-04 16:34:28 -0600186 }
Matthew Barth3770a1d2021-06-10 15:09:37 -0500187
Matthew Barth3695ac32021-10-06 14:55:30 -0500188 // Save all currently available groups, if any, then clear for reloading
189 auto groups = std::move(Event::getAllGroups(false));
190 Event::clearAllGroups();
191
192 std::map<configKey, std::unique_ptr<Event>> events;
193 try
194 {
195 // Load any events configured, including all the groups
196 events = getConfig<Event>(true, this, zones);
197 }
198 catch (const std::runtime_error& re)
199 {
200 // Restore saved set of all available groups for current events
201 Event::setAllGroups(std::move(groups));
202 throw re;
203 }
Matthew Barth3770a1d2021-06-10 15:09:37 -0500204
205 // Enable zones
206 _zones = std::move(zones);
207 std::for_each(_zones.begin(), _zones.end(),
208 [](const auto& entry) { entry.second->enable(); });
209
210 // Clear current timers and signal subscriptions before enabling events
211 // To save reloading services and/or objects into cache, do not clear
212 // cache
213 _timers.clear();
214 _signals.clear();
215
216 // Enable events
217 _events = std::move(events);
218 std::for_each(_events.begin(), _events.end(),
219 [](const auto& entry) { entry.second->enable(); });
220
221 _loadAllowed = false;
Matthew Barthde90fb42021-03-04 16:34:28 -0600222 }
Matthew Barthacd737c2021-03-04 11:04:01 -0600223}
224
Matthew Barth48f44da2021-05-27 15:43:34 -0500225void Manager::powerStateChanged(bool powerStateOn)
226{
227 if (powerStateOn)
228 {
Matthew Barth6a2418a2021-09-01 09:10:09 -0500229 if (_zones.empty())
230 {
231 throw std::runtime_error("No configured zones found at poweron");
232 }
Matthew Barth48f44da2021-05-27 15:43:34 -0500233 std::for_each(_zones.begin(), _zones.end(), [](const auto& entry) {
234 entry.second->setTarget(entry.second->getPoweronTarget());
235 });
Matt Spinlerd1f97f42021-10-29 16:19:24 -0500236
237 // Tell events to run their power on triggers
238 std::for_each(_events.begin(), _events.end(),
239 [](const auto& entry) { entry.second->powerOn(); });
240 }
241 else
242 {
243 // Tell events to run their power off triggers
244 std::for_each(_events.begin(), _events.end(),
245 [](const auto& entry) { entry.second->powerOff(); });
Matthew Barth48f44da2021-05-27 15:43:34 -0500246 }
247}
248
Matthew Barthacd737c2021-03-04 11:04:01 -0600249const std::vector<std::string>& Manager::getActiveProfiles()
250{
251 return _activeProfiles;
Matthew Bartha227a162020-08-05 10:51:45 -0500252}
253
Matthew Barth0206c722021-03-30 15:20:05 -0500254bool Manager::inConfig(const configKey& input, const configKey& comp)
255{
256 // Config names dont match, do not include in config
257 if (input.first != comp.first)
258 {
259 return false;
260 }
261 // No profiles specified by input config, can be used in any config
262 if (input.second.empty())
263 {
264 return true;
265 }
266 else
267 {
268 // Profiles must have one match in the other's profiles(and they must be
269 // an active profile) to be used in the config
270 return std::any_of(
271 input.second.begin(), input.second.end(),
272 [&comp](const auto& lProfile) {
273 return std::any_of(
274 comp.second.begin(), comp.second.end(),
275 [&lProfile](const auto& rProfile) {
276 if (lProfile != rProfile)
277 {
278 return false;
279 }
280 auto activeProfs = getActiveProfiles();
281 return std::find(activeProfs.begin(), activeProfs.end(),
282 lProfile) != activeProfs.end();
283 });
284 });
285 }
286}
287
Matthew Barth12cb1252021-03-08 16:47:30 -0600288bool Manager::hasOwner(const std::string& path, const std::string& intf)
289{
290 auto itServ = _servTree.find(path);
291 if (itServ == _servTree.end())
292 {
293 // Path not found in cache, therefore owner missing
294 return false;
295 }
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500296 for (const auto& service : itServ->second)
Matthew Barth12cb1252021-03-08 16:47:30 -0600297 {
298 auto itIntf = std::find_if(
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500299 service.second.second.begin(), service.second.second.end(),
Matthew Barth12cb1252021-03-08 16:47:30 -0600300 [&intf](const auto& interface) { return intf == interface; });
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500301 if (itIntf != std::end(service.second.second))
Matthew Barth12cb1252021-03-08 16:47:30 -0600302 {
303 // Service found, return owner state
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500304 return service.second.first;
Matthew Barth12cb1252021-03-08 16:47:30 -0600305 }
306 }
307 // Interface not found in cache, therefore owner missing
308 return false;
309}
310
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500311void Manager::setOwner(const std::string& path, const std::string& serv,
312 const std::string& intf, bool isOwned)
313{
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500314 // Set owner state for specific object given
315 auto& ownIntf = _servTree[path][serv];
316 ownIntf.first = isOwned;
317 auto itIntf = std::find_if(
318 ownIntf.second.begin(), ownIntf.second.end(),
319 [&intf](const auto& interface) { return intf == interface; });
320 if (itIntf == std::end(ownIntf.second))
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500321 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500322 ownIntf.second.emplace_back(intf);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500323 }
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500324
325 // Update owner state on all entries of the same `serv` & `intf`
326 for (auto& itPath : _servTree)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500327 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500328 if (itPath.first == path)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500329 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500330 // Already set/updated owner on this path for `serv` & `intf`
331 continue;
332 }
333 for (auto& itServ : itPath.second)
334 {
335 if (itServ.first != serv)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500336 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500337 continue;
338 }
339 auto itIntf = std::find_if(
340 itServ.second.second.begin(), itServ.second.second.end(),
341 [&intf](const auto& interface) { return intf == interface; });
342 if (itIntf != std::end(itServ.second.second))
343 {
344 itServ.second.first = isOwned;
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500345 }
346 }
347 }
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500348}
349
350const std::string& Manager::findService(const std::string& path,
351 const std::string& intf)
352{
353 static const std::string empty = "";
354
355 auto itServ = _servTree.find(path);
356 if (itServ != _servTree.end())
357 {
358 for (const auto& service : itServ->second)
359 {
360 auto itIntf = std::find_if(
361 service.second.second.begin(), service.second.second.end(),
362 [&intf](const auto& interface) { return intf == interface; });
363 if (itIntf != std::end(service.second.second))
364 {
365 // Service found, return service name
366 return service.first;
367 }
368 }
369 }
370
371 return empty;
372}
373
Matthew Barth98f6fc12021-04-16 10:48:37 -0500374void Manager::addServices(const std::string& intf, int32_t depth)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500375{
376 // Get all subtree objects for the given interface
Matt Spinler34835152021-07-01 12:28:58 -0600377 auto objects = util::SDBusPlus::getSubTreeRaw(util::SDBusPlus::getBus(),
378 "/", intf, depth);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500379 // Add what's returned to the cache of path->services
380 for (auto& itPath : objects)
381 {
382 auto pathIter = _servTree.find(itPath.first);
383 if (pathIter != _servTree.end())
384 {
385 // Path found in cache
386 for (auto& itServ : itPath.second)
387 {
388 auto servIter = pathIter->second.find(itServ.first);
389 if (servIter != pathIter->second.end())
390 {
391 // Service found in cache
392 for (auto& itIntf : itServ.second)
393 {
394 if (std::find(servIter->second.second.begin(),
395 servIter->second.second.end(),
396 itIntf) == servIter->second.second.end())
397 {
398 // Add interface to cache
399 servIter->second.second.emplace_back(itIntf);
400 }
401 }
402 }
403 else
404 {
405 // Service not found in cache
406 auto intfs = {intf};
407 pathIter->second[itServ.first] =
408 std::make_pair(true, intfs);
409 }
410 }
411 }
412 else
413 {
414 // Path not found in cache
415 auto intfs = {intf};
Matt Spinlera0b8a682021-10-14 15:38:48 -0500416 for (const auto& [servName, servIntfs] : itPath.second)
417 {
418 _servTree[itPath.first][servName] = std::make_pair(true, intfs);
419 }
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500420 }
421 }
422}
423
424const std::string& Manager::getService(const std::string& path,
425 const std::string& intf)
426{
427 // Retrieve service from cache
428 const auto& serviceName = findService(path, intf);
429 if (serviceName.empty())
430 {
Matthew Barth98f6fc12021-04-16 10:48:37 -0500431 addServices(intf, 0);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500432 return findService(path, intf);
433 }
434
435 return serviceName;
436}
437
Matthew Barthf41e9472021-05-13 16:38:06 -0500438std::vector<std::string> Manager::findPaths(const std::string& serv,
439 const std::string& intf)
440{
441 std::vector<std::string> paths;
442
443 for (const auto& path : _servTree)
444 {
445 auto itServ = path.second.find(serv);
446 if (itServ != path.second.end())
447 {
448 if (std::find(itServ->second.second.begin(),
449 itServ->second.second.end(),
450 intf) != itServ->second.second.end())
451 {
452 if (std::find(paths.begin(), paths.end(), path.first) ==
453 paths.end())
454 {
455 paths.push_back(path.first);
456 }
457 }
458 }
459 }
460
461 return paths;
462}
463
464std::vector<std::string> Manager::getPaths(const std::string& serv,
465 const std::string& intf)
466{
467 auto paths = findPaths(serv, intf);
468 if (paths.empty())
469 {
470 addServices(intf, 0);
471 return findPaths(serv, intf);
472 }
473
474 return paths;
475}
476
477void Manager::addObjects(const std::string& path, const std::string& intf,
478 const std::string& prop)
479{
480 auto service = getService(path, intf);
481 if (service.empty())
482 {
483 // Log service not found for object
Matt Spinler34835152021-07-01 12:28:58 -0600484 log<level::DEBUG>(
Matthew Barthf41e9472021-05-13 16:38:06 -0500485 fmt::format("Unable to get service name for path {}, interface {}",
486 path, intf)
487 .c_str());
488 return;
489 }
490
491 auto objMgrPaths = getPaths(service, "org.freedesktop.DBus.ObjectManager");
492 if (objMgrPaths.empty())
493 {
494 // No object manager interface provided by service?
495 // Attempt to retrieve property directly
496 auto variant = util::SDBusPlus::getPropertyVariant<PropertyVariantType>(
497 _bus, service, path, intf, prop);
498 _objects[path][intf][prop] = variant;
499 return;
500 }
501
502 for (const auto& objMgrPath : objMgrPaths)
503 {
504 // Get all managed objects of service
505 auto objects = util::SDBusPlus::getManagedObjects<PropertyVariantType>(
506 _bus, service, objMgrPath);
507
508 // Add what's returned to the cache of objects
509 for (auto& object : objects)
510 {
511 auto itPath = _objects.find(object.first);
512 if (itPath != _objects.end())
513 {
514 // Path found in cache
515 for (auto& interface : itPath->second)
516 {
517 auto itIntf = itPath->second.find(interface.first);
518 if (itIntf != itPath->second.end())
519 {
520 // Interface found in cache
521 for (auto& property : itIntf->second)
522 {
523 auto itProp = itIntf->second.find(property.first);
524 if (itProp != itIntf->second.end())
525 {
526 // Property found, update value
527 itProp->second = property.second;
528 }
529 else
530 {
531 itIntf->second.insert(property);
532 }
533 }
534 }
535 else
536 {
537 // Interface not found in cache
538 itPath->second.insert(interface);
539 }
540 }
541 }
542 else
543 {
544 // Path not found in cache
545 _objects.insert(object);
546 }
547 }
548 }
549}
550
551const std::optional<PropertyVariantType>
552 Manager::getProperty(const std::string& path, const std::string& intf,
553 const std::string& prop)
554{
555 // TODO Objects hosted by fan control (i.e. ThermalMode) are required to
556 // update the cache upon being set/updated
557 auto itPath = _objects.find(path);
558 if (itPath != _objects.end())
559 {
560 auto itIntf = itPath->second.find(intf);
561 if (itIntf != itPath->second.end())
562 {
563 auto itProp = itIntf->second.find(prop);
564 if (itProp != itIntf->second.end())
565 {
566 return itProp->second;
567 }
568 }
569 }
570
571 return std::nullopt;
572}
573
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500574void Manager::addTimer(const TimerType type,
575 const std::chrono::microseconds interval,
576 std::unique_ptr<TimerPkg> pkg)
577{
578 auto dataPtr =
579 std::make_unique<TimerData>(std::make_pair(type, std::move(*pkg)));
580 Timer timer(_event,
581 std::bind(&Manager::timerExpired, this, std::ref(*dataPtr)));
582 if (type == TimerType::repeating)
583 {
584 timer.restart(interval);
585 }
586 else if (type == TimerType::oneshot)
587 {
588 timer.restartOnce(interval);
589 }
590 else
591 {
592 throw std::invalid_argument("Invalid Timer Type");
593 }
594 _timers.emplace_back(std::move(dataPtr), std::move(timer));
595}
596
Matt Spinlerade0c372021-10-28 16:09:44 -0500597void Manager::addGroup(const Group& group)
598{
599 const auto& members = group.getMembers();
600 for (const auto& member : members)
601 {
602 try
603 {
604 auto service = getService(member, group.getInterface());
605
606 auto variant =
607 util::SDBusPlus::getPropertyVariant<PropertyVariantType>(
608 service, member, group.getInterface(), group.getProperty());
609
610 setProperty(member, group.getInterface(), group.getProperty(),
611 variant);
612 }
613 catch (const std::exception& e)
614 {
615 try
616 {
617 _objects.at(member)
618 .at(group.getInterface())
619 .erase(group.getProperty());
620 }
621 catch (const std::out_of_range&)
622 {}
623 }
624 }
625}
626
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500627void Manager::timerExpired(TimerData& data)
628{
Matt Spinlerade0c372021-10-28 16:09:44 -0500629 if (std::get<bool>(data.second))
630 {
631 const auto& groups = std::get<const std::vector<Group>&>(data.second);
632 std::for_each(groups.begin(), groups.end(),
633 [this](const auto& group) { addGroup(group); });
634 }
635
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500636 auto& actions =
637 std::get<std::vector<std::unique_ptr<ActionBase>>&>(data.second);
638 // Perform the actions in the timer data
639 std::for_each(actions.begin(), actions.end(),
Matthew Barth00f6aa02021-04-09 10:49:47 -0500640 [](auto& action) { action->run(); });
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500641
642 // Remove oneshot timers after they expired
643 if (data.first == TimerType::oneshot)
644 {
645 auto itTimer = std::find_if(
646 _timers.begin(), _timers.end(), [&data](const auto& timer) {
647 return (data.first == timer.first->first &&
648 (std::get<std::string>(data.second) ==
649 std::get<std::string>(timer.first->second)));
650 });
651 if (itTimer != std::end(_timers))
652 {
653 _timers.erase(itTimer);
654 }
655 }
656}
657
Matthew Barthebabc042021-05-13 15:38:58 -0500658void Manager::handleSignal(sdbusplus::message::message& msg,
Matthew Barthfac8a2f2021-06-10 15:50:36 -0500659 const std::vector<SignalPkg>& pkgs)
Matthew Barthebabc042021-05-13 15:38:58 -0500660{
Matthew Barthfac8a2f2021-06-10 15:50:36 -0500661 for (auto& pkg : pkgs)
Matthew Barthebabc042021-05-13 15:38:58 -0500662 {
663 // Handle the signal callback and only run the actions if the handler
664 // updated the cache for the given SignalObject
665 if (std::get<SignalHandler>(pkg)(msg, std::get<SignalObject>(pkg),
666 *this))
667 {
668 // Perform the actions in the handler package
669 auto& actions = std::get<SignalActions>(pkg);
670 std::for_each(actions.begin(), actions.end(),
671 [](auto& action) { action.get()->run(); });
672 }
673 }
674}
675
Matthew Barthacd737c2021-03-04 11:04:01 -0600676void Manager::setProfiles()
677{
678 // Profiles JSON config file is optional
679 auto confFile = fan::JsonConfig::getConfFile(_bus, confAppName,
680 Profile::confFileName, true);
Matthew Barthe91ac862021-05-25 16:22:17 -0500681
682 _profiles.clear();
Matthew Barthacd737c2021-03-04 11:04:01 -0600683 if (!confFile.empty())
684 {
685 for (const auto& entry : fan::JsonConfig::load(confFile))
686 {
687 auto obj = std::make_unique<Profile>(entry);
688 _profiles.emplace(
689 std::make_pair(obj->getName(), obj->getProfiles()),
690 std::move(obj));
691 }
692 }
Matthew Barthe91ac862021-05-25 16:22:17 -0500693
Matthew Barthacd737c2021-03-04 11:04:01 -0600694 // Ensure all configurations use the same set of active profiles
695 // (In case a profile's active state changes during configuration)
Matthew Barthe91ac862021-05-25 16:22:17 -0500696 _activeProfiles.clear();
Matthew Barthacd737c2021-03-04 11:04:01 -0600697 for (const auto& profile : _profiles)
698 {
699 if (profile.second->isActive())
700 {
701 _activeProfiles.emplace_back(profile.first.first);
702 }
703 }
704}
705
Matthew Bartha227a162020-08-05 10:51:45 -0500706} // namespace phosphor::fan::control::json