blob: c45c8731e15cadb2c2d0974e4b95f1a6b0e4e7b5 [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;
Matt Spinlerd0ba86a2021-11-09 10:09:13 -060062std::unordered_map<std::string, TriggerActions> Manager::_parameterTriggers;
Matthew Barthacd737c2021-03-04 11:04:01 -060063
Matt Spinler7787def2021-10-14 16:33:16 -050064const std::string Manager::dumpFile = "/tmp/fan_control_dump.json";
65
Matthew Barth9403a212021-05-17 09:31:50 -050066Manager::Manager(const sdeventplus::Event& event) :
Matthew Barth48f44da2021-05-27 15:43:34 -050067 _bus(util::SDBusPlus::getBus()), _event(event),
Matthew Barth3770a1d2021-06-10 15:09:37 -050068 _mgr(util::SDBusPlus::getBus(), CONTROL_OBJPATH), _loadAllowed(true),
Matthew Barth48f44da2021-05-27 15:43:34 -050069 _powerState(std::make_unique<PGoodState>(
70 util::SDBusPlus::getBus(),
71 std::bind(std::mem_fn(&Manager::powerStateChanged), this,
72 std::placeholders::_1)))
Matthew Barth3770a1d2021-06-10 15:09:37 -050073{}
Matthew Barthe91ac862021-05-25 16:22:17 -050074
75void Manager::sighupHandler(sdeventplus::source::Signal&,
76 const struct signalfd_siginfo*)
77{
78 // Save current set of available and active profiles
79 std::map<configKey, std::unique_ptr<Profile>> profiles;
80 profiles.swap(_profiles);
81 std::vector<std::string> activeProfiles;
82 activeProfiles.swap(_activeProfiles);
83
84 try
85 {
Matthew Barth3770a1d2021-06-10 15:09:37 -050086 _loadAllowed = true;
Matthew Barthe91ac862021-05-25 16:22:17 -050087 load();
88 }
Patrick Williamsddb773b2021-10-06 11:24:49 -050089 catch (const std::runtime_error& re)
Matthew Barthe91ac862021-05-25 16:22:17 -050090 {
91 // Restore saved available and active profiles
Matthew Barth3770a1d2021-06-10 15:09:37 -050092 _loadAllowed = false;
Matthew Barthe91ac862021-05-25 16:22:17 -050093 _profiles.swap(profiles);
94 _activeProfiles.swap(activeProfiles);
95 log<level::ERR>("Error reloading configs, no changes made",
96 entry("LOAD_ERROR=%s", re.what()));
97 }
98}
99
Matt Spinler2fc0a352021-10-04 15:10:57 -0500100void Manager::sigUsr1Handler(sdeventplus::source::Signal&,
101 const struct signalfd_siginfo*)
102{
Matt Spinler7787def2021-10-14 16:33:16 -0500103 debugDumpEventSource = std::make_unique<sdeventplus::source::Defer>(
104 _event, std::bind(std::mem_fn(&Manager::dumpDebugData), this,
Matt Spinler2fc0a352021-10-04 15:10:57 -0500105 std::placeholders::_1));
106}
107
Matt Spinler7787def2021-10-14 16:33:16 -0500108void Manager::dumpDebugData(sdeventplus::source::EventBase& /*source*/)
Matt Spinler2fc0a352021-10-04 15:10:57 -0500109{
Matt Spinler7787def2021-10-14 16:33:16 -0500110 json data;
111 FlightRecorder::instance().dump(data);
Matt Spinlerb5c21a22021-10-14 16:52:12 -0500112 dumpCache(data);
Matt Spinler7787def2021-10-14 16:33:16 -0500113
Matt Spinler9db6dd12021-10-29 16:10:08 -0500114 std::for_each(_zones.begin(), _zones.end(), [&data](const auto& zone) {
115 data["zones"][zone.second->getName()] = zone.second->dump();
116 });
117
Matt Spinler7787def2021-10-14 16:33:16 -0500118 std::ofstream file{Manager::dumpFile};
119 if (!file)
120 {
121 log<level::ERR>("Could not open file for fan dump");
122 return;
123 }
124
125 file << std::setw(4) << data;
126
127 debugDumpEventSource.reset();
Matt Spinler2fc0a352021-10-04 15:10:57 -0500128}
129
Matt Spinlerb5c21a22021-10-14 16:52:12 -0500130void Manager::dumpCache(json& data)
131{
132 auto& objects = data["objects"];
133 for (const auto& [path, interfaces] : _objects)
134 {
135 auto& interfaceJSON = objects[path];
136
137 for (const auto& [interface, properties] : interfaces)
138 {
139 auto& propertyJSON = interfaceJSON[interface];
140 for (const auto& [propName, propValue] : properties)
141 {
142 std::visit(
143 [&obj = propertyJSON[propName]](auto&& val) { obj = val; },
144 propValue);
145 }
146 }
147 }
148
149 auto& parameters = data["parameters"];
150 for (const auto& [name, value] : _parameters)
151 {
Matt Spinler29088e72021-11-08 16:23:27 -0600152 std::visit([&obj = parameters[name]](auto&& val) { obj = val; }, value);
Matt Spinlerb5c21a22021-10-14 16:52:12 -0500153 }
154
155 data["services"] = _servTree;
156}
157
Matthew Barthe91ac862021-05-25 16:22:17 -0500158void Manager::load()
159{
Matthew Barth3770a1d2021-06-10 15:09:37 -0500160 if (_loadAllowed)
Matthew Barthde90fb42021-03-04 16:34:28 -0600161 {
Matthew Barth3770a1d2021-06-10 15:09:37 -0500162 // 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)
Matthew Barthde90fb42021-03-04 16:34:28 -0600170 {
Matthew Barth3770a1d2021-06-10 15:09:37 -0500171 configKey fanProfile =
172 std::make_pair(fan.second->getZone(), fan.first.second);
173 auto itZone = std::find_if(
174 zones.begin(), zones.end(), [&fanProfile](const auto& zone) {
175 return Manager::inConfig(fanProfile, zone.first);
176 });
177 if (itZone != zones.end())
Matthew Barth6f787302021-03-25 15:01:01 -0500178 {
Matthew Barth3770a1d2021-06-10 15:09:37 -0500179 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));
Matthew Barth6f787302021-03-25 15:01:01 -0500187 }
Matthew Barthde90fb42021-03-04 16:34:28 -0600188 }
Matthew Barth3770a1d2021-06-10 15:09:37 -0500189
Matthew Barth3695ac32021-10-06 14:55:30 -0500190 // 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 }
Matthew Barth3770a1d2021-06-10 15:09:37 -0500206
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 std::for_each(_events.begin(), _events.end(),
221 [](const auto& entry) { entry.second->enable(); });
222
223 _loadAllowed = false;
Matthew Barthde90fb42021-03-04 16:34:28 -0600224 }
Matthew Barthacd737c2021-03-04 11:04:01 -0600225}
226
Matthew Barth48f44da2021-05-27 15:43:34 -0500227void Manager::powerStateChanged(bool powerStateOn)
228{
229 if (powerStateOn)
230 {
Matthew Barth6a2418a2021-09-01 09:10:09 -0500231 if (_zones.empty())
232 {
233 throw std::runtime_error("No configured zones found at poweron");
234 }
Matthew Barth48f44da2021-05-27 15:43:34 -0500235 std::for_each(_zones.begin(), _zones.end(), [](const auto& entry) {
236 entry.second->setTarget(entry.second->getPoweronTarget());
237 });
Matt Spinlerd1f97f42021-10-29 16:19:24 -0500238
239 // Tell events to run their power on triggers
240 std::for_each(_events.begin(), _events.end(),
241 [](const auto& entry) { entry.second->powerOn(); });
242 }
243 else
244 {
245 // Tell events to run their power off triggers
246 std::for_each(_events.begin(), _events.end(),
247 [](const auto& entry) { entry.second->powerOff(); });
Matthew Barth48f44da2021-05-27 15:43:34 -0500248 }
249}
250
Matthew Barthacd737c2021-03-04 11:04:01 -0600251const std::vector<std::string>& Manager::getActiveProfiles()
252{
253 return _activeProfiles;
Matthew Bartha227a162020-08-05 10:51:45 -0500254}
255
Matthew Barth0206c722021-03-30 15:20:05 -0500256bool Manager::inConfig(const configKey& input, const configKey& comp)
257{
258 // Config names dont match, do not include in config
259 if (input.first != comp.first)
260 {
261 return false;
262 }
263 // No profiles specified by input config, can be used in any config
264 if (input.second.empty())
265 {
266 return true;
267 }
268 else
269 {
270 // Profiles must have one match in the other's profiles(and they must be
271 // an active profile) to be used in the config
272 return std::any_of(
273 input.second.begin(), input.second.end(),
274 [&comp](const auto& lProfile) {
275 return std::any_of(
276 comp.second.begin(), comp.second.end(),
277 [&lProfile](const auto& rProfile) {
278 if (lProfile != rProfile)
279 {
280 return false;
281 }
282 auto activeProfs = getActiveProfiles();
283 return std::find(activeProfs.begin(), activeProfs.end(),
284 lProfile) != activeProfs.end();
285 });
286 });
287 }
288}
289
Matthew Barth12cb1252021-03-08 16:47:30 -0600290bool Manager::hasOwner(const std::string& path, const std::string& intf)
291{
292 auto itServ = _servTree.find(path);
293 if (itServ == _servTree.end())
294 {
295 // Path not found in cache, therefore owner missing
296 return false;
297 }
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500298 for (const auto& service : itServ->second)
Matthew Barth12cb1252021-03-08 16:47:30 -0600299 {
300 auto itIntf = std::find_if(
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500301 service.second.second.begin(), service.second.second.end(),
Matthew Barth12cb1252021-03-08 16:47:30 -0600302 [&intf](const auto& interface) { return intf == interface; });
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500303 if (itIntf != std::end(service.second.second))
Matthew Barth12cb1252021-03-08 16:47:30 -0600304 {
305 // Service found, return owner state
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500306 return service.second.first;
Matthew Barth12cb1252021-03-08 16:47:30 -0600307 }
308 }
309 // Interface not found in cache, therefore owner missing
310 return false;
311}
312
Matthew Barth6d8e2d32022-02-01 16:47:08 -0600313void Manager::setOwner(const std::string& serv, bool hasOwner)
314{
315 // Update owner state on all entries of `serv`
316 for (auto& itPath : _servTree)
317 {
318 auto itServ = itPath.second.find(serv);
319 if (itServ != itPath.second.end())
320 {
321 itServ->second.first = hasOwner;
322
323 // Remove associated interfaces from object cache when service no
324 // longer has an owner
325 if (!hasOwner && _objects.find(itPath.first) != _objects.end())
326 {
327 for (auto& intf : itServ->second.second)
328 {
329 _objects[itPath.first].erase(intf);
330 }
331 }
332 }
333 }
334}
335
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500336void Manager::setOwner(const std::string& path, const std::string& serv,
337 const std::string& intf, bool isOwned)
338{
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500339 // Set owner state for specific object given
340 auto& ownIntf = _servTree[path][serv];
341 ownIntf.first = isOwned;
342 auto itIntf = std::find_if(
343 ownIntf.second.begin(), ownIntf.second.end(),
344 [&intf](const auto& interface) { return intf == interface; });
345 if (itIntf == std::end(ownIntf.second))
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500346 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500347 ownIntf.second.emplace_back(intf);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500348 }
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500349
350 // Update owner state on all entries of the same `serv` & `intf`
351 for (auto& itPath : _servTree)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500352 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500353 if (itPath.first == path)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500354 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500355 // Already set/updated owner on this path for `serv` & `intf`
356 continue;
357 }
358 for (auto& itServ : itPath.second)
359 {
360 if (itServ.first != serv)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500361 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500362 continue;
363 }
364 auto itIntf = std::find_if(
365 itServ.second.second.begin(), itServ.second.second.end(),
366 [&intf](const auto& interface) { return intf == interface; });
367 if (itIntf != std::end(itServ.second.second))
368 {
369 itServ.second.first = isOwned;
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500370 }
371 }
372 }
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500373}
374
375const std::string& Manager::findService(const std::string& path,
376 const std::string& intf)
377{
378 static const std::string empty = "";
379
380 auto itServ = _servTree.find(path);
381 if (itServ != _servTree.end())
382 {
383 for (const auto& service : itServ->second)
384 {
385 auto itIntf = std::find_if(
386 service.second.second.begin(), service.second.second.end(),
387 [&intf](const auto& interface) { return intf == interface; });
388 if (itIntf != std::end(service.second.second))
389 {
390 // Service found, return service name
391 return service.first;
392 }
393 }
394 }
395
396 return empty;
397}
398
Matthew Barth98f6fc12021-04-16 10:48:37 -0500399void Manager::addServices(const std::string& intf, int32_t depth)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500400{
401 // Get all subtree objects for the given interface
Matt Spinler34835152021-07-01 12:28:58 -0600402 auto objects = util::SDBusPlus::getSubTreeRaw(util::SDBusPlus::getBus(),
403 "/", intf, depth);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500404 // Add what's returned to the cache of path->services
405 for (auto& itPath : objects)
406 {
407 auto pathIter = _servTree.find(itPath.first);
408 if (pathIter != _servTree.end())
409 {
410 // Path found in cache
411 for (auto& itServ : itPath.second)
412 {
413 auto servIter = pathIter->second.find(itServ.first);
414 if (servIter != pathIter->second.end())
415 {
416 // Service found in cache
417 for (auto& itIntf : itServ.second)
418 {
419 if (std::find(servIter->second.second.begin(),
420 servIter->second.second.end(),
421 itIntf) == servIter->second.second.end())
422 {
423 // Add interface to cache
424 servIter->second.second.emplace_back(itIntf);
425 }
426 }
427 }
428 else
429 {
430 // Service not found in cache
431 auto intfs = {intf};
432 pathIter->second[itServ.first] =
433 std::make_pair(true, intfs);
434 }
435 }
436 }
437 else
438 {
439 // Path not found in cache
440 auto intfs = {intf};
Matt Spinlera0b8a682021-10-14 15:38:48 -0500441 for (const auto& [servName, servIntfs] : itPath.second)
442 {
443 _servTree[itPath.first][servName] = std::make_pair(true, intfs);
444 }
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500445 }
446 }
447}
448
449const std::string& Manager::getService(const std::string& path,
450 const std::string& intf)
451{
452 // Retrieve service from cache
453 const auto& serviceName = findService(path, intf);
454 if (serviceName.empty())
455 {
Matthew Barth98f6fc12021-04-16 10:48:37 -0500456 addServices(intf, 0);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500457 return findService(path, intf);
458 }
459
460 return serviceName;
461}
462
Matthew Barthf41e9472021-05-13 16:38:06 -0500463std::vector<std::string> Manager::findPaths(const std::string& serv,
464 const std::string& intf)
465{
466 std::vector<std::string> paths;
467
468 for (const auto& path : _servTree)
469 {
470 auto itServ = path.second.find(serv);
471 if (itServ != path.second.end())
472 {
473 if (std::find(itServ->second.second.begin(),
474 itServ->second.second.end(),
475 intf) != itServ->second.second.end())
476 {
477 if (std::find(paths.begin(), paths.end(), path.first) ==
478 paths.end())
479 {
480 paths.push_back(path.first);
481 }
482 }
483 }
484 }
485
486 return paths;
487}
488
489std::vector<std::string> Manager::getPaths(const std::string& serv,
490 const std::string& intf)
491{
492 auto paths = findPaths(serv, intf);
493 if (paths.empty())
494 {
495 addServices(intf, 0);
496 return findPaths(serv, intf);
497 }
498
499 return paths;
500}
501
Mike Capps1a19ead2021-10-22 09:15:14 -0400502void Manager::insertFilteredObjects(ManagedObjects& ref)
503{
504 for (auto& [path, pathMap] : ref)
505 {
506 for (auto& [intf, intfMap] : pathMap)
507 {
508 // for each property on this path+interface
509 for (auto& [prop, value] : intfMap)
510 {
511 setProperty(path, intf, prop, value);
512 }
513 }
514 }
515}
516
Matthew Barthf41e9472021-05-13 16:38:06 -0500517void Manager::addObjects(const std::string& path, const std::string& intf,
518 const std::string& prop)
519{
520 auto service = getService(path, intf);
521 if (service.empty())
522 {
523 // Log service not found for object
Matt Spinler34835152021-07-01 12:28:58 -0600524 log<level::DEBUG>(
Matthew Barthf41e9472021-05-13 16:38:06 -0500525 fmt::format("Unable to get service name for path {}, interface {}",
526 path, intf)
527 .c_str());
528 return;
529 }
530
531 auto objMgrPaths = getPaths(service, "org.freedesktop.DBus.ObjectManager");
532 if (objMgrPaths.empty())
533 {
534 // No object manager interface provided by service?
535 // Attempt to retrieve property directly
Mike Capps1a19ead2021-10-22 09:15:14 -0400536 auto value = util::SDBusPlus::getPropertyVariant<PropertyVariantType>(
Matthew Barthf41e9472021-05-13 16:38:06 -0500537 _bus, service, path, intf, prop);
Mike Capps1a19ead2021-10-22 09:15:14 -0400538
539 setProperty(path, intf, prop, value);
Matthew Barthf41e9472021-05-13 16:38:06 -0500540 return;
541 }
542
543 for (const auto& objMgrPath : objMgrPaths)
544 {
545 // Get all managed objects of service
Mike Capps1a19ead2021-10-22 09:15:14 -0400546 // want to filter here...
Matthew Barthf41e9472021-05-13 16:38:06 -0500547 auto objects = util::SDBusPlus::getManagedObjects<PropertyVariantType>(
548 _bus, service, objMgrPath);
549
Mike Capps1a19ead2021-10-22 09:15:14 -0400550 // insert all objects but remove any NaN values
551 insertFilteredObjects(objects);
Matthew Barthf41e9472021-05-13 16:38:06 -0500552 }
553}
554
555const std::optional<PropertyVariantType>
556 Manager::getProperty(const std::string& path, const std::string& intf,
557 const std::string& prop)
558{
559 // TODO Objects hosted by fan control (i.e. ThermalMode) are required to
560 // update the cache upon being set/updated
561 auto itPath = _objects.find(path);
562 if (itPath != _objects.end())
563 {
564 auto itIntf = itPath->second.find(intf);
565 if (itIntf != itPath->second.end())
566 {
567 auto itProp = itIntf->second.find(prop);
568 if (itProp != itIntf->second.end())
569 {
570 return itProp->second;
571 }
572 }
573 }
574
575 return std::nullopt;
576}
577
Mike Capps1a19ead2021-10-22 09:15:14 -0400578void Manager::setProperty(const std::string& path, const std::string& intf,
579 const std::string& prop, PropertyVariantType value)
580{
581 // filter NaNs out of the cache
582 if (PropertyContainsNan(value))
583 {
584 // dont use operator [] if paths dont exist
585 if (_objects.find(path) != _objects.end() &&
586 _objects[path].find(intf) != _objects[path].end())
587 {
588 _objects[path][intf].erase(prop);
589 }
590 }
591 else
592 {
593 _objects[path][intf][prop] = std::move(value);
594 }
595}
596
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500597void Manager::addTimer(const TimerType type,
598 const std::chrono::microseconds interval,
599 std::unique_ptr<TimerPkg> pkg)
600{
601 auto dataPtr =
602 std::make_unique<TimerData>(std::make_pair(type, std::move(*pkg)));
603 Timer timer(_event,
604 std::bind(&Manager::timerExpired, this, std::ref(*dataPtr)));
605 if (type == TimerType::repeating)
606 {
607 timer.restart(interval);
608 }
609 else if (type == TimerType::oneshot)
610 {
611 timer.restartOnce(interval);
612 }
613 else
614 {
615 throw std::invalid_argument("Invalid Timer Type");
616 }
617 _timers.emplace_back(std::move(dataPtr), std::move(timer));
618}
619
Matt Spinlerade0c372021-10-28 16:09:44 -0500620void Manager::addGroup(const Group& group)
621{
Matthew Barth55627ad2021-12-02 22:28:29 -0600622 std::set<std::string> services;
623 for (const auto& member : group.getMembers())
Matt Spinlerade0c372021-10-28 16:09:44 -0500624 {
625 try
626 {
Matthew Barth55627ad2021-12-02 22:28:29 -0600627 const auto& service =
628 util::SDBusPlus::getService(member, group.getInterface());
Matt Spinlerade0c372021-10-28 16:09:44 -0500629
Matthew Barth55627ad2021-12-02 22:28:29 -0600630 // TODO - Optimize to only retrieve the paths on this service
631 auto objMgrPaths =
632 getPaths(service, "org.freedesktop.DBus.ObjectManager");
Matt Spinlerade0c372021-10-28 16:09:44 -0500633
Matthew Barth55627ad2021-12-02 22:28:29 -0600634 // Look for the ObjectManager as an ancestor from the member.
635 auto hasObjMgr =
636 std::any_of(objMgrPaths.begin(), objMgrPaths.end(),
637 [&member](const auto& path) {
638 return member.find(path) != std::string::npos;
639 });
640
641 if (!hasObjMgr)
Matt Spinlerade0c372021-10-28 16:09:44 -0500642 {
Matthew Barth55627ad2021-12-02 22:28:29 -0600643 // No object manager interface provided for group member
644 // Attempt to retrieve group member property directly
645 auto value =
646 util::SDBusPlus::getPropertyVariant<PropertyVariantType>(
647 _bus, service, member, group.getInterface(),
648 group.getProperty());
649
650 setProperty(member, group.getInterface(), group.getProperty(),
651 value);
652 continue;
Matt Spinlerade0c372021-10-28 16:09:44 -0500653 }
Matthew Barth55627ad2021-12-02 22:28:29 -0600654
655 if (services.find(service) == services.end())
656 {
657 services.insert(service);
658 for (const auto& objMgrPath : objMgrPaths)
659 {
660 // Get all managed objects from the service
661 auto objects =
662 util::SDBusPlus::getManagedObjects<PropertyVariantType>(
663 _bus, service, objMgrPath);
664
665 // Insert objects into cache
666 insertFilteredObjects(objects);
667 }
668 }
669 }
670 catch (const util::DBusServiceError&)
671 {
672 // No service found for group member containing the group's
673 // configured interface
674 continue;
Matt Spinlerade0c372021-10-28 16:09:44 -0500675 }
676 }
677}
678
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500679void Manager::timerExpired(TimerData& data)
680{
Matt Spinlerade0c372021-10-28 16:09:44 -0500681 if (std::get<bool>(data.second))
682 {
683 const auto& groups = std::get<const std::vector<Group>&>(data.second);
684 std::for_each(groups.begin(), groups.end(),
685 [this](const auto& group) { addGroup(group); });
686 }
687
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500688 auto& actions =
689 std::get<std::vector<std::unique_ptr<ActionBase>>&>(data.second);
690 // Perform the actions in the timer data
691 std::for_each(actions.begin(), actions.end(),
Matthew Barth00f6aa02021-04-09 10:49:47 -0500692 [](auto& action) { action->run(); });
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500693
694 // Remove oneshot timers after they expired
695 if (data.first == TimerType::oneshot)
696 {
697 auto itTimer = std::find_if(
698 _timers.begin(), _timers.end(), [&data](const auto& timer) {
699 return (data.first == timer.first->first &&
700 (std::get<std::string>(data.second) ==
701 std::get<std::string>(timer.first->second)));
702 });
703 if (itTimer != std::end(_timers))
704 {
705 _timers.erase(itTimer);
706 }
707 }
708}
709
Matthew Barthebabc042021-05-13 15:38:58 -0500710void Manager::handleSignal(sdbusplus::message::message& msg,
Matthew Barthc024d782021-11-09 16:15:49 -0600711 const std::vector<SignalPkg>* pkgs)
Matthew Barthebabc042021-05-13 15:38:58 -0500712{
Matthew Barthc024d782021-11-09 16:15:49 -0600713 for (auto& pkg : *pkgs)
Matthew Barthebabc042021-05-13 15:38:58 -0500714 {
715 // Handle the signal callback and only run the actions if the handler
716 // updated the cache for the given SignalObject
717 if (std::get<SignalHandler>(pkg)(msg, std::get<SignalObject>(pkg),
718 *this))
719 {
720 // Perform the actions in the handler package
Matt Spinlerd0ba86a2021-11-09 10:09:13 -0600721 auto& actions = std::get<TriggerActions>(pkg);
Matthew Barthc3a69082021-11-15 14:32:48 -0600722 std::for_each(actions.begin(), actions.end(), [](auto& action) {
723 if (action.get())
724 {
725 action.get()->run();
726 }
727 });
Matthew Barthebabc042021-05-13 15:38:58 -0500728 }
Matthew Barthc024d782021-11-09 16:15:49 -0600729 // Only rewind message when not last package
730 if (&pkg != &pkgs->back())
731 {
732 sd_bus_message_rewind(msg.get(), true);
733 }
Matthew Barthebabc042021-05-13 15:38:58 -0500734 }
735}
736
Matthew Barthacd737c2021-03-04 11:04:01 -0600737void Manager::setProfiles()
738{
739 // Profiles JSON config file is optional
740 auto confFile = fan::JsonConfig::getConfFile(_bus, confAppName,
741 Profile::confFileName, true);
Matthew Barthe91ac862021-05-25 16:22:17 -0500742
743 _profiles.clear();
Matthew Barthacd737c2021-03-04 11:04:01 -0600744 if (!confFile.empty())
745 {
746 for (const auto& entry : fan::JsonConfig::load(confFile))
747 {
748 auto obj = std::make_unique<Profile>(entry);
749 _profiles.emplace(
750 std::make_pair(obj->getName(), obj->getProfiles()),
751 std::move(obj));
752 }
753 }
Matthew Barthe91ac862021-05-25 16:22:17 -0500754
Matthew Barthacd737c2021-03-04 11:04:01 -0600755 // Ensure all configurations use the same set of active profiles
756 // (In case a profile's active state changes during configuration)
Matthew Barthe91ac862021-05-25 16:22:17 -0500757 _activeProfiles.clear();
Matthew Barthacd737c2021-03-04 11:04:01 -0600758 for (const auto& profile : _profiles)
759 {
760 if (profile.second->isActive())
761 {
762 _activeProfiles.emplace_back(profile.first.first);
763 }
764 }
765}
766
Matt Spinlerd0ba86a2021-11-09 10:09:13 -0600767void Manager::addParameterTrigger(
768 const std::string& name, std::vector<std::unique_ptr<ActionBase>>& actions)
769{
770 auto it = _parameterTriggers.find(name);
771 if (it != _parameterTriggers.end())
772 {
773 std::for_each(actions.begin(), actions.end(),
774 [&actList = it->second](auto& action) {
775 actList.emplace_back(std::ref(action));
776 });
777 }
778 else
779 {
780 TriggerActions triggerActions;
781 std::for_each(actions.begin(), actions.end(),
782 [&triggerActions](auto& action) {
783 triggerActions.emplace_back(std::ref(action));
784 });
785 _parameterTriggers[name] = std::move(triggerActions);
786 }
787}
788
789void Manager::runParameterActions(const std::string& name)
790{
791 auto it = _parameterTriggers.find(name);
792 if (it != _parameterTriggers.end())
793 {
794 std::for_each(it->second.begin(), it->second.end(),
795 [](auto& action) { action.get()->run(); });
796 }
797}
798
Matthew Bartha227a162020-08-05 10:51:45 -0500799} // namespace phosphor::fan::control::json