blob: 5bfa07c3645b98849a97eeae3131f32084eb6917 [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 Barthc024d782021-11-09 16:15:49 -060031#include <systemd/sd-bus.h>
32
Matthew Barthacd737c2021-03-04 11:04:01 -060033#include <nlohmann/json.hpp>
Matthew Bartha227a162020-08-05 10:51:45 -050034#include <sdbusplus/bus.hpp>
Matthew Barth1542fb52021-06-10 14:09:09 -050035#include <sdbusplus/server/manager.hpp>
Matthew Barthacd737c2021-03-04 11:04:01 -060036#include <sdeventplus/event.hpp>
Matthew Barthd9cb63b2021-03-24 14:36:49 -050037#include <sdeventplus/utility/timer.hpp>
Matthew Bartha227a162020-08-05 10:51:45 -050038
Matthew Barthde90fb42021-03-04 16:34:28 -060039#include <algorithm>
Matthew Barthd9cb63b2021-03-24 14:36:49 -050040#include <chrono>
Matthew Bartha227a162020-08-05 10:51:45 -050041#include <filesystem>
Matthew Barthd9cb63b2021-03-24 14:36:49 -050042#include <functional>
43#include <map>
44#include <memory>
45#include <tuple>
46#include <utility>
Matthew Barth06764942021-03-04 09:28:40 -060047#include <vector>
Matthew Bartha227a162020-08-05 10:51:45 -050048
49namespace phosphor::fan::control::json
50{
51
Matthew Barthacd737c2021-03-04 11:04:01 -060052using json = nlohmann::json;
53
54std::vector<std::string> Manager::_activeProfiles;
Matthew Barth12cb1252021-03-08 16:47:30 -060055std::map<std::string,
Matthew Barth4ca87fa2021-04-14 11:31:13 -050056 std::map<std::string, std::pair<bool, std::vector<std::string>>>>
Matthew Barth12cb1252021-03-08 16:47:30 -060057 Manager::_servTree;
Matthew Barth07fecfc2021-01-29 09:04:43 -060058std::map<std::string,
59 std::map<std::string, std::map<std::string, PropertyVariantType>>>
60 Manager::_objects;
Matt Spinlerd76351b2021-08-05 16:23:09 -050061std::unordered_map<std::string, PropertyVariantType> Manager::_parameters;
Matthew Barthacd737c2021-03-04 11:04:01 -060062
Matt Spinler7787def2021-10-14 16:33:16 -050063const std::string Manager::dumpFile = "/tmp/fan_control_dump.json";
64
Matthew Barth9403a212021-05-17 09:31:50 -050065Manager::Manager(const sdeventplus::Event& event) :
Matthew Barth48f44da2021-05-27 15:43:34 -050066 _bus(util::SDBusPlus::getBus()), _event(event),
Matthew Barth3770a1d2021-06-10 15:09:37 -050067 _mgr(util::SDBusPlus::getBus(), CONTROL_OBJPATH), _loadAllowed(true),
Matthew Barth48f44da2021-05-27 15:43:34 -050068 _powerState(std::make_unique<PGoodState>(
69 util::SDBusPlus::getBus(),
70 std::bind(std::mem_fn(&Manager::powerStateChanged), this,
71 std::placeholders::_1)))
Matthew Barth3770a1d2021-06-10 15:09:37 -050072{}
Matthew Barthe91ac862021-05-25 16:22:17 -050073
74void Manager::sighupHandler(sdeventplus::source::Signal&,
75 const struct signalfd_siginfo*)
76{
77 // Save current set of available and active profiles
78 std::map<configKey, std::unique_ptr<Profile>> profiles;
79 profiles.swap(_profiles);
80 std::vector<std::string> activeProfiles;
81 activeProfiles.swap(_activeProfiles);
82
83 try
84 {
Matthew Barth3770a1d2021-06-10 15:09:37 -050085 _loadAllowed = true;
Matthew Barthe91ac862021-05-25 16:22:17 -050086 load();
87 }
Patrick Williamsddb773b2021-10-06 11:24:49 -050088 catch (const std::runtime_error& re)
Matthew Barthe91ac862021-05-25 16:22:17 -050089 {
90 // Restore saved available and active profiles
Matthew Barth3770a1d2021-06-10 15:09:37 -050091 _loadAllowed = false;
Matthew Barthe91ac862021-05-25 16:22:17 -050092 _profiles.swap(profiles);
93 _activeProfiles.swap(activeProfiles);
94 log<level::ERR>("Error reloading configs, no changes made",
95 entry("LOAD_ERROR=%s", re.what()));
96 }
97}
98
Matt Spinler2fc0a352021-10-04 15:10:57 -050099void Manager::sigUsr1Handler(sdeventplus::source::Signal&,
100 const struct signalfd_siginfo*)
101{
Matt Spinler7787def2021-10-14 16:33:16 -0500102 debugDumpEventSource = std::make_unique<sdeventplus::source::Defer>(
103 _event, std::bind(std::mem_fn(&Manager::dumpDebugData), this,
Matt Spinler2fc0a352021-10-04 15:10:57 -0500104 std::placeholders::_1));
105}
106
Matt Spinler7787def2021-10-14 16:33:16 -0500107void Manager::dumpDebugData(sdeventplus::source::EventBase& /*source*/)
Matt Spinler2fc0a352021-10-04 15:10:57 -0500108{
Matt Spinler7787def2021-10-14 16:33:16 -0500109 json data;
110 FlightRecorder::instance().dump(data);
Matt Spinlerb5c21a22021-10-14 16:52:12 -0500111 dumpCache(data);
Matt Spinler7787def2021-10-14 16:33:16 -0500112
Matt Spinler9db6dd12021-10-29 16:10:08 -0500113 std::for_each(_zones.begin(), _zones.end(), [&data](const auto& zone) {
114 data["zones"][zone.second->getName()] = zone.second->dump();
115 });
116
Matt Spinler7787def2021-10-14 16:33:16 -0500117 std::ofstream file{Manager::dumpFile};
118 if (!file)
119 {
120 log<level::ERR>("Could not open file for fan dump");
121 return;
122 }
123
124 file << std::setw(4) << data;
125
126 debugDumpEventSource.reset();
Matt Spinler2fc0a352021-10-04 15:10:57 -0500127}
128
Matt Spinlerb5c21a22021-10-14 16:52:12 -0500129void Manager::dumpCache(json& data)
130{
131 auto& objects = data["objects"];
132 for (const auto& [path, interfaces] : _objects)
133 {
134 auto& interfaceJSON = objects[path];
135
136 for (const auto& [interface, properties] : interfaces)
137 {
138 auto& propertyJSON = interfaceJSON[interface];
139 for (const auto& [propName, propValue] : properties)
140 {
141 std::visit(
142 [&obj = propertyJSON[propName]](auto&& val) { obj = val; },
143 propValue);
144 }
145 }
146 }
147
148 auto& parameters = data["parameters"];
149 for (const auto& [name, value] : _parameters)
150 {
Matt Spinler29088e72021-11-08 16:23:27 -0600151 std::visit([&obj = parameters[name]](auto&& val) { obj = val; }, value);
Matt Spinlerb5c21a22021-10-14 16:52:12 -0500152 }
153
154 data["services"] = _servTree;
155}
156
Matthew Barthe91ac862021-05-25 16:22:17 -0500157void Manager::load()
158{
Matthew Barth3770a1d2021-06-10 15:09:37 -0500159 if (_loadAllowed)
Matthew Barthde90fb42021-03-04 16:34:28 -0600160 {
Matthew Barth3770a1d2021-06-10 15:09:37 -0500161 // Load the available profiles and which are active
162 setProfiles();
163
164 // Load the zone configurations
165 auto zones = getConfig<Zone>(false, _event, this);
166 // Load the fan configurations and move each fan into its zone
167 auto fans = getConfig<Fan>(false);
168 for (auto& fan : fans)
Matthew Barthde90fb42021-03-04 16:34:28 -0600169 {
Matthew Barth3770a1d2021-06-10 15:09:37 -0500170 configKey fanProfile =
171 std::make_pair(fan.second->getZone(), fan.first.second);
172 auto itZone = std::find_if(
173 zones.begin(), zones.end(), [&fanProfile](const auto& zone) {
174 return Manager::inConfig(fanProfile, zone.first);
175 });
176 if (itZone != zones.end())
Matthew Barth6f787302021-03-25 15:01:01 -0500177 {
Matthew Barth3770a1d2021-06-10 15:09:37 -0500178 if (itZone->second->getTarget() != fan.second->getTarget() &&
179 fan.second->getTarget() != 0)
180 {
181 // Update zone target to current target of the fan in the
182 // zone
183 itZone->second->setTarget(fan.second->getTarget());
184 }
185 itZone->second->addFan(std::move(fan.second));
Matthew Barth6f787302021-03-25 15:01:01 -0500186 }
Matthew Barthde90fb42021-03-04 16:34:28 -0600187 }
Matthew Barth3770a1d2021-06-10 15:09:37 -0500188
Matthew Barth3695ac32021-10-06 14:55:30 -0500189 // Save all currently available groups, if any, then clear for reloading
190 auto groups = std::move(Event::getAllGroups(false));
191 Event::clearAllGroups();
192
193 std::map<configKey, std::unique_ptr<Event>> events;
194 try
195 {
196 // Load any events configured, including all the groups
197 events = getConfig<Event>(true, this, zones);
198 }
199 catch (const std::runtime_error& re)
200 {
201 // Restore saved set of all available groups for current events
202 Event::setAllGroups(std::move(groups));
203 throw re;
204 }
Matthew Barth3770a1d2021-06-10 15:09:37 -0500205
206 // Enable zones
207 _zones = std::move(zones);
208 std::for_each(_zones.begin(), _zones.end(),
209 [](const auto& entry) { entry.second->enable(); });
210
211 // Clear current timers and signal subscriptions before enabling events
212 // To save reloading services and/or objects into cache, do not clear
213 // cache
214 _timers.clear();
215 _signals.clear();
216
217 // Enable events
218 _events = std::move(events);
219 std::for_each(_events.begin(), _events.end(),
220 [](const auto& entry) { entry.second->enable(); });
221
222 _loadAllowed = false;
Matthew Barthde90fb42021-03-04 16:34:28 -0600223 }
Matthew Barthacd737c2021-03-04 11:04:01 -0600224}
225
Matthew Barth48f44da2021-05-27 15:43:34 -0500226void Manager::powerStateChanged(bool powerStateOn)
227{
228 if (powerStateOn)
229 {
Matthew Barth6a2418a2021-09-01 09:10:09 -0500230 if (_zones.empty())
231 {
232 throw std::runtime_error("No configured zones found at poweron");
233 }
Matthew Barth48f44da2021-05-27 15:43:34 -0500234 std::for_each(_zones.begin(), _zones.end(), [](const auto& entry) {
235 entry.second->setTarget(entry.second->getPoweronTarget());
236 });
Matt Spinlerd1f97f42021-10-29 16:19:24 -0500237
238 // Tell events to run their power on triggers
239 std::for_each(_events.begin(), _events.end(),
240 [](const auto& entry) { entry.second->powerOn(); });
241 }
242 else
243 {
244 // Tell events to run their power off triggers
245 std::for_each(_events.begin(), _events.end(),
246 [](const auto& entry) { entry.second->powerOff(); });
Matthew Barth48f44da2021-05-27 15:43:34 -0500247 }
248}
249
Matthew Barthacd737c2021-03-04 11:04:01 -0600250const std::vector<std::string>& Manager::getActiveProfiles()
251{
252 return _activeProfiles;
Matthew Bartha227a162020-08-05 10:51:45 -0500253}
254
Matthew Barth0206c722021-03-30 15:20:05 -0500255bool Manager::inConfig(const configKey& input, const configKey& comp)
256{
257 // Config names dont match, do not include in config
258 if (input.first != comp.first)
259 {
260 return false;
261 }
262 // No profiles specified by input config, can be used in any config
263 if (input.second.empty())
264 {
265 return true;
266 }
267 else
268 {
269 // Profiles must have one match in the other's profiles(and they must be
270 // an active profile) to be used in the config
271 return std::any_of(
272 input.second.begin(), input.second.end(),
273 [&comp](const auto& lProfile) {
274 return std::any_of(
275 comp.second.begin(), comp.second.end(),
276 [&lProfile](const auto& rProfile) {
277 if (lProfile != rProfile)
278 {
279 return false;
280 }
281 auto activeProfs = getActiveProfiles();
282 return std::find(activeProfs.begin(), activeProfs.end(),
283 lProfile) != activeProfs.end();
284 });
285 });
286 }
287}
288
Matthew Barth12cb1252021-03-08 16:47:30 -0600289bool Manager::hasOwner(const std::string& path, const std::string& intf)
290{
291 auto itServ = _servTree.find(path);
292 if (itServ == _servTree.end())
293 {
294 // Path not found in cache, therefore owner missing
295 return false;
296 }
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500297 for (const auto& service : itServ->second)
Matthew Barth12cb1252021-03-08 16:47:30 -0600298 {
299 auto itIntf = std::find_if(
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500300 service.second.second.begin(), service.second.second.end(),
Matthew Barth12cb1252021-03-08 16:47:30 -0600301 [&intf](const auto& interface) { return intf == interface; });
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500302 if (itIntf != std::end(service.second.second))
Matthew Barth12cb1252021-03-08 16:47:30 -0600303 {
304 // Service found, return owner state
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500305 return service.second.first;
Matthew Barth12cb1252021-03-08 16:47:30 -0600306 }
307 }
308 // Interface not found in cache, therefore owner missing
309 return false;
310}
311
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500312void Manager::setOwner(const std::string& path, const std::string& serv,
313 const std::string& intf, bool isOwned)
314{
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500315 // Set owner state for specific object given
316 auto& ownIntf = _servTree[path][serv];
317 ownIntf.first = isOwned;
318 auto itIntf = std::find_if(
319 ownIntf.second.begin(), ownIntf.second.end(),
320 [&intf](const auto& interface) { return intf == interface; });
321 if (itIntf == std::end(ownIntf.second))
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500322 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500323 ownIntf.second.emplace_back(intf);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500324 }
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500325
326 // Update owner state on all entries of the same `serv` & `intf`
327 for (auto& itPath : _servTree)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500328 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500329 if (itPath.first == path)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500330 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500331 // Already set/updated owner on this path for `serv` & `intf`
332 continue;
333 }
334 for (auto& itServ : itPath.second)
335 {
336 if (itServ.first != serv)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500337 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500338 continue;
339 }
340 auto itIntf = std::find_if(
341 itServ.second.second.begin(), itServ.second.second.end(),
342 [&intf](const auto& interface) { return intf == interface; });
343 if (itIntf != std::end(itServ.second.second))
344 {
345 itServ.second.first = isOwned;
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500346 }
347 }
348 }
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500349}
350
351const std::string& Manager::findService(const std::string& path,
352 const std::string& intf)
353{
354 static const std::string empty = "";
355
356 auto itServ = _servTree.find(path);
357 if (itServ != _servTree.end())
358 {
359 for (const auto& service : itServ->second)
360 {
361 auto itIntf = std::find_if(
362 service.second.second.begin(), service.second.second.end(),
363 [&intf](const auto& interface) { return intf == interface; });
364 if (itIntf != std::end(service.second.second))
365 {
366 // Service found, return service name
367 return service.first;
368 }
369 }
370 }
371
372 return empty;
373}
374
Matthew Barth98f6fc12021-04-16 10:48:37 -0500375void Manager::addServices(const std::string& intf, int32_t depth)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500376{
377 // Get all subtree objects for the given interface
Matt Spinler34835152021-07-01 12:28:58 -0600378 auto objects = util::SDBusPlus::getSubTreeRaw(util::SDBusPlus::getBus(),
379 "/", intf, depth);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500380 // Add what's returned to the cache of path->services
381 for (auto& itPath : objects)
382 {
383 auto pathIter = _servTree.find(itPath.first);
384 if (pathIter != _servTree.end())
385 {
386 // Path found in cache
387 for (auto& itServ : itPath.second)
388 {
389 auto servIter = pathIter->second.find(itServ.first);
390 if (servIter != pathIter->second.end())
391 {
392 // Service found in cache
393 for (auto& itIntf : itServ.second)
394 {
395 if (std::find(servIter->second.second.begin(),
396 servIter->second.second.end(),
397 itIntf) == servIter->second.second.end())
398 {
399 // Add interface to cache
400 servIter->second.second.emplace_back(itIntf);
401 }
402 }
403 }
404 else
405 {
406 // Service not found in cache
407 auto intfs = {intf};
408 pathIter->second[itServ.first] =
409 std::make_pair(true, intfs);
410 }
411 }
412 }
413 else
414 {
415 // Path not found in cache
416 auto intfs = {intf};
Matt Spinlera0b8a682021-10-14 15:38:48 -0500417 for (const auto& [servName, servIntfs] : itPath.second)
418 {
419 _servTree[itPath.first][servName] = std::make_pair(true, intfs);
420 }
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500421 }
422 }
423}
424
425const std::string& Manager::getService(const std::string& path,
426 const std::string& intf)
427{
428 // Retrieve service from cache
429 const auto& serviceName = findService(path, intf);
430 if (serviceName.empty())
431 {
Matthew Barth98f6fc12021-04-16 10:48:37 -0500432 addServices(intf, 0);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500433 return findService(path, intf);
434 }
435
436 return serviceName;
437}
438
Matthew Barthf41e9472021-05-13 16:38:06 -0500439std::vector<std::string> Manager::findPaths(const std::string& serv,
440 const std::string& intf)
441{
442 std::vector<std::string> paths;
443
444 for (const auto& path : _servTree)
445 {
446 auto itServ = path.second.find(serv);
447 if (itServ != path.second.end())
448 {
449 if (std::find(itServ->second.second.begin(),
450 itServ->second.second.end(),
451 intf) != itServ->second.second.end())
452 {
453 if (std::find(paths.begin(), paths.end(), path.first) ==
454 paths.end())
455 {
456 paths.push_back(path.first);
457 }
458 }
459 }
460 }
461
462 return paths;
463}
464
465std::vector<std::string> Manager::getPaths(const std::string& serv,
466 const std::string& intf)
467{
468 auto paths = findPaths(serv, intf);
469 if (paths.empty())
470 {
471 addServices(intf, 0);
472 return findPaths(serv, intf);
473 }
474
475 return paths;
476}
477
Mike Capps1a19ead2021-10-22 09:15:14 -0400478void Manager::insertFilteredObjects(ManagedObjects& ref)
479{
480 for (auto& [path, pathMap] : ref)
481 {
482 for (auto& [intf, intfMap] : pathMap)
483 {
484 // for each property on this path+interface
485 for (auto& [prop, value] : intfMap)
486 {
487 setProperty(path, intf, prop, value);
488 }
489 }
490 }
491}
492
Matthew Barthf41e9472021-05-13 16:38:06 -0500493void Manager::addObjects(const std::string& path, const std::string& intf,
494 const std::string& prop)
495{
496 auto service = getService(path, intf);
497 if (service.empty())
498 {
499 // Log service not found for object
Matt Spinler34835152021-07-01 12:28:58 -0600500 log<level::DEBUG>(
Matthew Barthf41e9472021-05-13 16:38:06 -0500501 fmt::format("Unable to get service name for path {}, interface {}",
502 path, intf)
503 .c_str());
504 return;
505 }
506
507 auto objMgrPaths = getPaths(service, "org.freedesktop.DBus.ObjectManager");
508 if (objMgrPaths.empty())
509 {
510 // No object manager interface provided by service?
511 // Attempt to retrieve property directly
Mike Capps1a19ead2021-10-22 09:15:14 -0400512 auto value = util::SDBusPlus::getPropertyVariant<PropertyVariantType>(
Matthew Barthf41e9472021-05-13 16:38:06 -0500513 _bus, service, path, intf, prop);
Mike Capps1a19ead2021-10-22 09:15:14 -0400514
515 setProperty(path, intf, prop, value);
Matthew Barthf41e9472021-05-13 16:38:06 -0500516 return;
517 }
518
519 for (const auto& objMgrPath : objMgrPaths)
520 {
521 // Get all managed objects of service
Mike Capps1a19ead2021-10-22 09:15:14 -0400522 // want to filter here...
Matthew Barthf41e9472021-05-13 16:38:06 -0500523 auto objects = util::SDBusPlus::getManagedObjects<PropertyVariantType>(
524 _bus, service, objMgrPath);
525
Mike Capps1a19ead2021-10-22 09:15:14 -0400526 // insert all objects but remove any NaN values
527 insertFilteredObjects(objects);
Matthew Barthf41e9472021-05-13 16:38:06 -0500528 }
529}
530
531const std::optional<PropertyVariantType>
532 Manager::getProperty(const std::string& path, const std::string& intf,
533 const std::string& prop)
534{
535 // TODO Objects hosted by fan control (i.e. ThermalMode) are required to
536 // update the cache upon being set/updated
537 auto itPath = _objects.find(path);
538 if (itPath != _objects.end())
539 {
540 auto itIntf = itPath->second.find(intf);
541 if (itIntf != itPath->second.end())
542 {
543 auto itProp = itIntf->second.find(prop);
544 if (itProp != itIntf->second.end())
545 {
546 return itProp->second;
547 }
548 }
549 }
550
551 return std::nullopt;
552}
553
Mike Capps1a19ead2021-10-22 09:15:14 -0400554void Manager::setProperty(const std::string& path, const std::string& intf,
555 const std::string& prop, PropertyVariantType value)
556{
557 // filter NaNs out of the cache
558 if (PropertyContainsNan(value))
559 {
560 // dont use operator [] if paths dont exist
561 if (_objects.find(path) != _objects.end() &&
562 _objects[path].find(intf) != _objects[path].end())
563 {
564 _objects[path][intf].erase(prop);
565 }
566 }
567 else
568 {
569 _objects[path][intf][prop] = std::move(value);
570 }
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 Barthc024d782021-11-09 16:15:49 -0600658 const std::vector<SignalPkg>* pkgs)
Matthew Barthebabc042021-05-13 15:38:58 -0500659{
Matthew Barthc024d782021-11-09 16:15:49 -0600660 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);
Matthew Barthc3a69082021-11-15 14:32:48 -0600669 std::for_each(actions.begin(), actions.end(), [](auto& action) {
670 if (action.get())
671 {
672 action.get()->run();
673 }
674 });
Matthew Barthebabc042021-05-13 15:38:58 -0500675 }
Matthew Barthc024d782021-11-09 16:15:49 -0600676 // Only rewind message when not last package
677 if (&pkg != &pkgs->back())
678 {
679 sd_bus_message_rewind(msg.get(), true);
680 }
Matthew Barthebabc042021-05-13 15:38:58 -0500681 }
682}
683
Matthew Barthacd737c2021-03-04 11:04:01 -0600684void Manager::setProfiles()
685{
686 // Profiles JSON config file is optional
687 auto confFile = fan::JsonConfig::getConfFile(_bus, confAppName,
688 Profile::confFileName, true);
Matthew Barthe91ac862021-05-25 16:22:17 -0500689
690 _profiles.clear();
Matthew Barthacd737c2021-03-04 11:04:01 -0600691 if (!confFile.empty())
692 {
693 for (const auto& entry : fan::JsonConfig::load(confFile))
694 {
695 auto obj = std::make_unique<Profile>(entry);
696 _profiles.emplace(
697 std::make_pair(obj->getName(), obj->getProfiles()),
698 std::move(obj));
699 }
700 }
Matthew Barthe91ac862021-05-25 16:22:17 -0500701
Matthew Barthacd737c2021-03-04 11:04:01 -0600702 // Ensure all configurations use the same set of active profiles
703 // (In case a profile's active state changes during configuration)
Matthew Barthe91ac862021-05-25 16:22:17 -0500704 _activeProfiles.clear();
Matthew Barthacd737c2021-03-04 11:04:01 -0600705 for (const auto& profile : _profiles)
706 {
707 if (profile.second->isActive())
708 {
709 _activeProfiles.emplace_back(profile.first.first);
710 }
711 }
712}
713
Matthew Bartha227a162020-08-05 10:51:45 -0500714} // namespace phosphor::fan::control::json