blob: b0da04a9853d9625d50d451b2530f74521384614 [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 {
Matt Spinler29088e72021-11-08 16:23:27 -0600149 std::visit([&obj = parameters[name]](auto&& val) { obj = val; }, value);
Matt Spinlerb5c21a22021-10-14 16:52:12 -0500150 }
151
152 data["services"] = _servTree;
153}
154
Matthew Barthe91ac862021-05-25 16:22:17 -0500155void Manager::load()
156{
Matthew Barth3770a1d2021-06-10 15:09:37 -0500157 if (_loadAllowed)
Matthew Barthde90fb42021-03-04 16:34:28 -0600158 {
Matthew Barth3770a1d2021-06-10 15:09:37 -0500159 // Load the available profiles and which are active
160 setProfiles();
161
162 // Load the zone configurations
163 auto zones = getConfig<Zone>(false, _event, this);
164 // Load the fan configurations and move each fan into its zone
165 auto fans = getConfig<Fan>(false);
166 for (auto& fan : fans)
Matthew Barthde90fb42021-03-04 16:34:28 -0600167 {
Matthew Barth3770a1d2021-06-10 15:09:37 -0500168 configKey fanProfile =
169 std::make_pair(fan.second->getZone(), fan.first.second);
170 auto itZone = std::find_if(
171 zones.begin(), zones.end(), [&fanProfile](const auto& zone) {
172 return Manager::inConfig(fanProfile, zone.first);
173 });
174 if (itZone != zones.end())
Matthew Barth6f787302021-03-25 15:01:01 -0500175 {
Matthew Barth3770a1d2021-06-10 15:09:37 -0500176 if (itZone->second->getTarget() != fan.second->getTarget() &&
177 fan.second->getTarget() != 0)
178 {
179 // Update zone target to current target of the fan in the
180 // zone
181 itZone->second->setTarget(fan.second->getTarget());
182 }
183 itZone->second->addFan(std::move(fan.second));
Matthew Barth6f787302021-03-25 15:01:01 -0500184 }
Matthew Barthde90fb42021-03-04 16:34:28 -0600185 }
Matthew Barth3770a1d2021-06-10 15:09:37 -0500186
Matthew Barth3695ac32021-10-06 14:55:30 -0500187 // Save all currently available groups, if any, then clear for reloading
188 auto groups = std::move(Event::getAllGroups(false));
189 Event::clearAllGroups();
190
191 std::map<configKey, std::unique_ptr<Event>> events;
192 try
193 {
194 // Load any events configured, including all the groups
195 events = getConfig<Event>(true, this, zones);
196 }
197 catch (const std::runtime_error& re)
198 {
199 // Restore saved set of all available groups for current events
200 Event::setAllGroups(std::move(groups));
201 throw re;
202 }
Matthew Barth3770a1d2021-06-10 15:09:37 -0500203
204 // Enable zones
205 _zones = std::move(zones);
206 std::for_each(_zones.begin(), _zones.end(),
207 [](const auto& entry) { entry.second->enable(); });
208
209 // Clear current timers and signal subscriptions before enabling events
210 // To save reloading services and/or objects into cache, do not clear
211 // cache
212 _timers.clear();
213 _signals.clear();
214
215 // Enable events
216 _events = std::move(events);
217 std::for_each(_events.begin(), _events.end(),
218 [](const auto& entry) { entry.second->enable(); });
219
220 _loadAllowed = false;
Matthew Barthde90fb42021-03-04 16:34:28 -0600221 }
Matthew Barthacd737c2021-03-04 11:04:01 -0600222}
223
Matthew Barth48f44da2021-05-27 15:43:34 -0500224void Manager::powerStateChanged(bool powerStateOn)
225{
226 if (powerStateOn)
227 {
Matthew Barth6a2418a2021-09-01 09:10:09 -0500228 if (_zones.empty())
229 {
230 throw std::runtime_error("No configured zones found at poweron");
231 }
Matthew Barth48f44da2021-05-27 15:43:34 -0500232 std::for_each(_zones.begin(), _zones.end(), [](const auto& entry) {
233 entry.second->setTarget(entry.second->getPoweronTarget());
234 });
Matt Spinlerd1f97f42021-10-29 16:19:24 -0500235
236 // Tell events to run their power on triggers
237 std::for_each(_events.begin(), _events.end(),
238 [](const auto& entry) { entry.second->powerOn(); });
239 }
240 else
241 {
242 // Tell events to run their power off triggers
243 std::for_each(_events.begin(), _events.end(),
244 [](const auto& entry) { entry.second->powerOff(); });
Matthew Barth48f44da2021-05-27 15:43:34 -0500245 }
246}
247
Matthew Barthacd737c2021-03-04 11:04:01 -0600248const std::vector<std::string>& Manager::getActiveProfiles()
249{
250 return _activeProfiles;
Matthew Bartha227a162020-08-05 10:51:45 -0500251}
252
Matthew Barth0206c722021-03-30 15:20:05 -0500253bool Manager::inConfig(const configKey& input, const configKey& comp)
254{
255 // Config names dont match, do not include in config
256 if (input.first != comp.first)
257 {
258 return false;
259 }
260 // No profiles specified by input config, can be used in any config
261 if (input.second.empty())
262 {
263 return true;
264 }
265 else
266 {
267 // Profiles must have one match in the other's profiles(and they must be
268 // an active profile) to be used in the config
269 return std::any_of(
270 input.second.begin(), input.second.end(),
271 [&comp](const auto& lProfile) {
272 return std::any_of(
273 comp.second.begin(), comp.second.end(),
274 [&lProfile](const auto& rProfile) {
275 if (lProfile != rProfile)
276 {
277 return false;
278 }
279 auto activeProfs = getActiveProfiles();
280 return std::find(activeProfs.begin(), activeProfs.end(),
281 lProfile) != activeProfs.end();
282 });
283 });
284 }
285}
286
Matthew Barth12cb1252021-03-08 16:47:30 -0600287bool Manager::hasOwner(const std::string& path, const std::string& intf)
288{
289 auto itServ = _servTree.find(path);
290 if (itServ == _servTree.end())
291 {
292 // Path not found in cache, therefore owner missing
293 return false;
294 }
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500295 for (const auto& service : itServ->second)
Matthew Barth12cb1252021-03-08 16:47:30 -0600296 {
297 auto itIntf = std::find_if(
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500298 service.second.second.begin(), service.second.second.end(),
Matthew Barth12cb1252021-03-08 16:47:30 -0600299 [&intf](const auto& interface) { return intf == interface; });
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500300 if (itIntf != std::end(service.second.second))
Matthew Barth12cb1252021-03-08 16:47:30 -0600301 {
302 // Service found, return owner state
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500303 return service.second.first;
Matthew Barth12cb1252021-03-08 16:47:30 -0600304 }
305 }
306 // Interface not found in cache, therefore owner missing
307 return false;
308}
309
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500310void Manager::setOwner(const std::string& path, const std::string& serv,
311 const std::string& intf, bool isOwned)
312{
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500313 // Set owner state for specific object given
314 auto& ownIntf = _servTree[path][serv];
315 ownIntf.first = isOwned;
316 auto itIntf = std::find_if(
317 ownIntf.second.begin(), ownIntf.second.end(),
318 [&intf](const auto& interface) { return intf == interface; });
319 if (itIntf == std::end(ownIntf.second))
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500320 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500321 ownIntf.second.emplace_back(intf);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500322 }
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500323
324 // Update owner state on all entries of the same `serv` & `intf`
325 for (auto& itPath : _servTree)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500326 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500327 if (itPath.first == path)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500328 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500329 // Already set/updated owner on this path for `serv` & `intf`
330 continue;
331 }
332 for (auto& itServ : itPath.second)
333 {
334 if (itServ.first != serv)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500335 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500336 continue;
337 }
338 auto itIntf = std::find_if(
339 itServ.second.second.begin(), itServ.second.second.end(),
340 [&intf](const auto& interface) { return intf == interface; });
341 if (itIntf != std::end(itServ.second.second))
342 {
343 itServ.second.first = isOwned;
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500344 }
345 }
346 }
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500347}
348
349const std::string& Manager::findService(const std::string& path,
350 const std::string& intf)
351{
352 static const std::string empty = "";
353
354 auto itServ = _servTree.find(path);
355 if (itServ != _servTree.end())
356 {
357 for (const auto& service : itServ->second)
358 {
359 auto itIntf = std::find_if(
360 service.second.second.begin(), service.second.second.end(),
361 [&intf](const auto& interface) { return intf == interface; });
362 if (itIntf != std::end(service.second.second))
363 {
364 // Service found, return service name
365 return service.first;
366 }
367 }
368 }
369
370 return empty;
371}
372
Matthew Barth98f6fc12021-04-16 10:48:37 -0500373void Manager::addServices(const std::string& intf, int32_t depth)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500374{
375 // Get all subtree objects for the given interface
Matt Spinler34835152021-07-01 12:28:58 -0600376 auto objects = util::SDBusPlus::getSubTreeRaw(util::SDBusPlus::getBus(),
377 "/", intf, depth);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500378 // Add what's returned to the cache of path->services
379 for (auto& itPath : objects)
380 {
381 auto pathIter = _servTree.find(itPath.first);
382 if (pathIter != _servTree.end())
383 {
384 // Path found in cache
385 for (auto& itServ : itPath.second)
386 {
387 auto servIter = pathIter->second.find(itServ.first);
388 if (servIter != pathIter->second.end())
389 {
390 // Service found in cache
391 for (auto& itIntf : itServ.second)
392 {
393 if (std::find(servIter->second.second.begin(),
394 servIter->second.second.end(),
395 itIntf) == servIter->second.second.end())
396 {
397 // Add interface to cache
398 servIter->second.second.emplace_back(itIntf);
399 }
400 }
401 }
402 else
403 {
404 // Service not found in cache
405 auto intfs = {intf};
406 pathIter->second[itServ.first] =
407 std::make_pair(true, intfs);
408 }
409 }
410 }
411 else
412 {
413 // Path not found in cache
414 auto intfs = {intf};
Matt Spinlera0b8a682021-10-14 15:38:48 -0500415 for (const auto& [servName, servIntfs] : itPath.second)
416 {
417 _servTree[itPath.first][servName] = std::make_pair(true, intfs);
418 }
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500419 }
420 }
421}
422
423const std::string& Manager::getService(const std::string& path,
424 const std::string& intf)
425{
426 // Retrieve service from cache
427 const auto& serviceName = findService(path, intf);
428 if (serviceName.empty())
429 {
Matthew Barth98f6fc12021-04-16 10:48:37 -0500430 addServices(intf, 0);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500431 return findService(path, intf);
432 }
433
434 return serviceName;
435}
436
Matthew Barthf41e9472021-05-13 16:38:06 -0500437std::vector<std::string> Manager::findPaths(const std::string& serv,
438 const std::string& intf)
439{
440 std::vector<std::string> paths;
441
442 for (const auto& path : _servTree)
443 {
444 auto itServ = path.second.find(serv);
445 if (itServ != path.second.end())
446 {
447 if (std::find(itServ->second.second.begin(),
448 itServ->second.second.end(),
449 intf) != itServ->second.second.end())
450 {
451 if (std::find(paths.begin(), paths.end(), path.first) ==
452 paths.end())
453 {
454 paths.push_back(path.first);
455 }
456 }
457 }
458 }
459
460 return paths;
461}
462
463std::vector<std::string> Manager::getPaths(const std::string& serv,
464 const std::string& intf)
465{
466 auto paths = findPaths(serv, intf);
467 if (paths.empty())
468 {
469 addServices(intf, 0);
470 return findPaths(serv, intf);
471 }
472
473 return paths;
474}
475
476void Manager::addObjects(const std::string& path, const std::string& intf,
477 const std::string& prop)
478{
479 auto service = getService(path, intf);
480 if (service.empty())
481 {
482 // Log service not found for object
Matt Spinler34835152021-07-01 12:28:58 -0600483 log<level::DEBUG>(
Matthew Barthf41e9472021-05-13 16:38:06 -0500484 fmt::format("Unable to get service name for path {}, interface {}",
485 path, intf)
486 .c_str());
487 return;
488 }
489
490 auto objMgrPaths = getPaths(service, "org.freedesktop.DBus.ObjectManager");
491 if (objMgrPaths.empty())
492 {
493 // No object manager interface provided by service?
494 // Attempt to retrieve property directly
495 auto variant = util::SDBusPlus::getPropertyVariant<PropertyVariantType>(
496 _bus, service, path, intf, prop);
497 _objects[path][intf][prop] = variant;
498 return;
499 }
500
501 for (const auto& objMgrPath : objMgrPaths)
502 {
503 // Get all managed objects of service
504 auto objects = util::SDBusPlus::getManagedObjects<PropertyVariantType>(
505 _bus, service, objMgrPath);
506
507 // Add what's returned to the cache of objects
508 for (auto& object : objects)
509 {
510 auto itPath = _objects.find(object.first);
511 if (itPath != _objects.end())
512 {
513 // Path found in cache
514 for (auto& interface : itPath->second)
515 {
516 auto itIntf = itPath->second.find(interface.first);
517 if (itIntf != itPath->second.end())
518 {
519 // Interface found in cache
520 for (auto& property : itIntf->second)
521 {
522 auto itProp = itIntf->second.find(property.first);
523 if (itProp != itIntf->second.end())
524 {
525 // Property found, update value
526 itProp->second = property.second;
527 }
528 else
529 {
530 itIntf->second.insert(property);
531 }
532 }
533 }
534 else
535 {
536 // Interface not found in cache
537 itPath->second.insert(interface);
538 }
539 }
540 }
541 else
542 {
543 // Path not found in cache
544 _objects.insert(object);
545 }
546 }
547 }
548}
549
550const std::optional<PropertyVariantType>
551 Manager::getProperty(const std::string& path, const std::string& intf,
552 const std::string& prop)
553{
554 // TODO Objects hosted by fan control (i.e. ThermalMode) are required to
555 // update the cache upon being set/updated
556 auto itPath = _objects.find(path);
557 if (itPath != _objects.end())
558 {
559 auto itIntf = itPath->second.find(intf);
560 if (itIntf != itPath->second.end())
561 {
562 auto itProp = itIntf->second.find(prop);
563 if (itProp != itIntf->second.end())
564 {
565 return itProp->second;
566 }
567 }
568 }
569
570 return std::nullopt;
571}
572
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500573void Manager::addTimer(const TimerType type,
574 const std::chrono::microseconds interval,
575 std::unique_ptr<TimerPkg> pkg)
576{
577 auto dataPtr =
578 std::make_unique<TimerData>(std::make_pair(type, std::move(*pkg)));
579 Timer timer(_event,
580 std::bind(&Manager::timerExpired, this, std::ref(*dataPtr)));
581 if (type == TimerType::repeating)
582 {
583 timer.restart(interval);
584 }
585 else if (type == TimerType::oneshot)
586 {
587 timer.restartOnce(interval);
588 }
589 else
590 {
591 throw std::invalid_argument("Invalid Timer Type");
592 }
593 _timers.emplace_back(std::move(dataPtr), std::move(timer));
594}
595
Matt Spinlerade0c372021-10-28 16:09:44 -0500596void Manager::addGroup(const Group& group)
597{
598 const auto& members = group.getMembers();
599 for (const auto& member : members)
600 {
601 try
602 {
603 auto service = getService(member, group.getInterface());
604
605 auto variant =
606 util::SDBusPlus::getPropertyVariant<PropertyVariantType>(
607 service, member, group.getInterface(), group.getProperty());
608
609 setProperty(member, group.getInterface(), group.getProperty(),
610 variant);
611 }
612 catch (const std::exception& e)
613 {
614 try
615 {
616 _objects.at(member)
617 .at(group.getInterface())
618 .erase(group.getProperty());
619 }
620 catch (const std::out_of_range&)
621 {}
622 }
623 }
624}
625
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500626void Manager::timerExpired(TimerData& data)
627{
Matt Spinlerade0c372021-10-28 16:09:44 -0500628 if (std::get<bool>(data.second))
629 {
630 const auto& groups = std::get<const std::vector<Group>&>(data.second);
631 std::for_each(groups.begin(), groups.end(),
632 [this](const auto& group) { addGroup(group); });
633 }
634
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500635 auto& actions =
636 std::get<std::vector<std::unique_ptr<ActionBase>>&>(data.second);
637 // Perform the actions in the timer data
638 std::for_each(actions.begin(), actions.end(),
Matthew Barth00f6aa02021-04-09 10:49:47 -0500639 [](auto& action) { action->run(); });
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500640
641 // Remove oneshot timers after they expired
642 if (data.first == TimerType::oneshot)
643 {
644 auto itTimer = std::find_if(
645 _timers.begin(), _timers.end(), [&data](const auto& timer) {
646 return (data.first == timer.first->first &&
647 (std::get<std::string>(data.second) ==
648 std::get<std::string>(timer.first->second)));
649 });
650 if (itTimer != std::end(_timers))
651 {
652 _timers.erase(itTimer);
653 }
654 }
655}
656
Matthew Barthebabc042021-05-13 15:38:58 -0500657void Manager::handleSignal(sdbusplus::message::message& msg,
Matthew Barthfac8a2f2021-06-10 15:50:36 -0500658 const std::vector<SignalPkg>& pkgs)
Matthew Barthebabc042021-05-13 15:38:58 -0500659{
Matthew Barthfac8a2f2021-06-10 15:50:36 -0500660 for (auto& pkg : pkgs)
Matthew Barthebabc042021-05-13 15:38:58 -0500661 {
662 // Handle the signal callback and only run the actions if the handler
663 // updated the cache for the given SignalObject
664 if (std::get<SignalHandler>(pkg)(msg, std::get<SignalObject>(pkg),
665 *this))
666 {
667 // Perform the actions in the handler package
668 auto& actions = std::get<SignalActions>(pkg);
669 std::for_each(actions.begin(), actions.end(),
670 [](auto& action) { action.get()->run(); });
671 }
672 }
673}
674
Matthew Barthacd737c2021-03-04 11:04:01 -0600675void Manager::setProfiles()
676{
677 // Profiles JSON config file is optional
678 auto confFile = fan::JsonConfig::getConfFile(_bus, confAppName,
679 Profile::confFileName, true);
Matthew Barthe91ac862021-05-25 16:22:17 -0500680
681 _profiles.clear();
Matthew Barthacd737c2021-03-04 11:04:01 -0600682 if (!confFile.empty())
683 {
684 for (const auto& entry : fan::JsonConfig::load(confFile))
685 {
686 auto obj = std::make_unique<Profile>(entry);
687 _profiles.emplace(
688 std::make_pair(obj->getName(), obj->getProfiles()),
689 std::move(obj));
690 }
691 }
Matthew Barthe91ac862021-05-25 16:22:17 -0500692
Matthew Barthacd737c2021-03-04 11:04:01 -0600693 // Ensure all configurations use the same set of active profiles
694 // (In case a profile's active state changes during configuration)
Matthew Barthe91ac862021-05-25 16:22:17 -0500695 _activeProfiles.clear();
Matthew Barthacd737c2021-03-04 11:04:01 -0600696 for (const auto& profile : _profiles)
697 {
698 if (profile.second->isActive())
699 {
700 _activeProfiles.emplace_back(profile.first.first);
701 }
702 }
703}
704
Matthew Bartha227a162020-08-05 10:51:45 -0500705} // namespace phosphor::fan::control::json