blob: d7e3b06102b9d62d5430f95041a6b3c18cef4466 [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
Matthew Barthd9cb63b2021-03-24 14:36:49 -050020#include "action.hpp"
Matthew Barth44ab7692021-03-26 11:40:10 -050021#include "event.hpp"
Matthew Barthde90fb42021-03-04 16:34:28 -060022#include "fan.hpp"
Matthew Barthd9cb63b2021-03-24 14:36:49 -050023#include "group.hpp"
Matthew Bartha227a162020-08-05 10:51:45 -050024#include "json_config.hpp"
Matthew Barth48f44da2021-05-27 15:43:34 -050025#include "power_state.hpp"
Matthew Barth06764942021-03-04 09:28:40 -060026#include "profile.hpp"
Matthew Barth9403a212021-05-17 09:31:50 -050027#include "sdbusplus.hpp"
Matthew Barthacd737c2021-03-04 11:04:01 -060028#include "zone.hpp"
Matthew Bartha227a162020-08-05 10:51:45 -050029
Matthew Barthacd737c2021-03-04 11:04:01 -060030#include <nlohmann/json.hpp>
Matthew Bartha227a162020-08-05 10:51:45 -050031#include <sdbusplus/bus.hpp>
Matthew Barth1542fb52021-06-10 14:09:09 -050032#include <sdbusplus/server/manager.hpp>
Matthew Barthacd737c2021-03-04 11:04:01 -060033#include <sdeventplus/event.hpp>
Matthew Barthd9cb63b2021-03-24 14:36:49 -050034#include <sdeventplus/utility/timer.hpp>
Matthew Bartha227a162020-08-05 10:51:45 -050035
Matthew Barthde90fb42021-03-04 16:34:28 -060036#include <algorithm>
Matthew Barthd9cb63b2021-03-24 14:36:49 -050037#include <chrono>
Matthew Bartha227a162020-08-05 10:51:45 -050038#include <filesystem>
Matthew Barthd9cb63b2021-03-24 14:36:49 -050039#include <functional>
40#include <map>
41#include <memory>
42#include <tuple>
43#include <utility>
Matthew Barth06764942021-03-04 09:28:40 -060044#include <vector>
Matthew Bartha227a162020-08-05 10:51:45 -050045
46namespace phosphor::fan::control::json
47{
48
Matthew Barthacd737c2021-03-04 11:04:01 -060049using json = nlohmann::json;
50
51std::vector<std::string> Manager::_activeProfiles;
Matthew Barth12cb1252021-03-08 16:47:30 -060052std::map<std::string,
Matthew Barth4ca87fa2021-04-14 11:31:13 -050053 std::map<std::string, std::pair<bool, std::vector<std::string>>>>
Matthew Barth12cb1252021-03-08 16:47:30 -060054 Manager::_servTree;
Matthew Barth07fecfc2021-01-29 09:04:43 -060055std::map<std::string,
56 std::map<std::string, std::map<std::string, PropertyVariantType>>>
57 Manager::_objects;
Matt Spinlerd76351b2021-08-05 16:23:09 -050058std::unordered_map<std::string, PropertyVariantType> Manager::_parameters;
Matthew Barthacd737c2021-03-04 11:04:01 -060059
Matthew Barth9403a212021-05-17 09:31:50 -050060Manager::Manager(const sdeventplus::Event& event) :
Matthew Barth48f44da2021-05-27 15:43:34 -050061 _bus(util::SDBusPlus::getBus()), _event(event),
Matthew Barth3770a1d2021-06-10 15:09:37 -050062 _mgr(util::SDBusPlus::getBus(), CONTROL_OBJPATH), _loadAllowed(true),
Matthew Barth48f44da2021-05-27 15:43:34 -050063 _powerState(std::make_unique<PGoodState>(
64 util::SDBusPlus::getBus(),
65 std::bind(std::mem_fn(&Manager::powerStateChanged), this,
66 std::placeholders::_1)))
Matthew Barth3770a1d2021-06-10 15:09:37 -050067{}
Matthew Barthe91ac862021-05-25 16:22:17 -050068
69void Manager::sighupHandler(sdeventplus::source::Signal&,
70 const struct signalfd_siginfo*)
71{
72 // Save current set of available and active profiles
73 std::map<configKey, std::unique_ptr<Profile>> profiles;
74 profiles.swap(_profiles);
75 std::vector<std::string> activeProfiles;
76 activeProfiles.swap(_activeProfiles);
77
78 try
79 {
Matthew Barth3770a1d2021-06-10 15:09:37 -050080 _loadAllowed = true;
Matthew Barthe91ac862021-05-25 16:22:17 -050081 load();
82 }
83 catch (std::runtime_error& re)
84 {
85 // Restore saved available and active profiles
Matthew Barth3770a1d2021-06-10 15:09:37 -050086 _loadAllowed = false;
Matthew Barthe91ac862021-05-25 16:22:17 -050087 _profiles.swap(profiles);
88 _activeProfiles.swap(activeProfiles);
89 log<level::ERR>("Error reloading configs, no changes made",
90 entry("LOAD_ERROR=%s", re.what()));
91 }
92}
93
94void Manager::load()
95{
Matthew Barth3770a1d2021-06-10 15:09:37 -050096 if (_loadAllowed)
Matthew Barthde90fb42021-03-04 16:34:28 -060097 {
Matthew Barth3770a1d2021-06-10 15:09:37 -050098 // Load the available profiles and which are active
99 setProfiles();
100
101 // Load the zone configurations
102 auto zones = getConfig<Zone>(false, _event, this);
103 // Load the fan configurations and move each fan into its zone
104 auto fans = getConfig<Fan>(false);
105 for (auto& fan : fans)
Matthew Barthde90fb42021-03-04 16:34:28 -0600106 {
Matthew Barth3770a1d2021-06-10 15:09:37 -0500107 configKey fanProfile =
108 std::make_pair(fan.second->getZone(), fan.first.second);
109 auto itZone = std::find_if(
110 zones.begin(), zones.end(), [&fanProfile](const auto& zone) {
111 return Manager::inConfig(fanProfile, zone.first);
112 });
113 if (itZone != zones.end())
Matthew Barth6f787302021-03-25 15:01:01 -0500114 {
Matthew Barth3770a1d2021-06-10 15:09:37 -0500115 if (itZone->second->getTarget() != fan.second->getTarget() &&
116 fan.second->getTarget() != 0)
117 {
118 // Update zone target to current target of the fan in the
119 // zone
120 itZone->second->setTarget(fan.second->getTarget());
121 }
122 itZone->second->addFan(std::move(fan.second));
Matthew Barth6f787302021-03-25 15:01:01 -0500123 }
Matthew Barthde90fb42021-03-04 16:34:28 -0600124 }
Matthew Barth3770a1d2021-06-10 15:09:37 -0500125
126 // Load any events configured
127 auto events = getConfig<Event>(true, this, zones);
128
129 // Enable zones
130 _zones = std::move(zones);
131 std::for_each(_zones.begin(), _zones.end(),
132 [](const auto& entry) { entry.second->enable(); });
133
134 // Clear current timers and signal subscriptions before enabling events
135 // To save reloading services and/or objects into cache, do not clear
136 // cache
137 _timers.clear();
138 _signals.clear();
139
140 // Enable events
141 _events = std::move(events);
142 std::for_each(_events.begin(), _events.end(),
143 [](const auto& entry) { entry.second->enable(); });
144
145 _loadAllowed = false;
Matthew Barthde90fb42021-03-04 16:34:28 -0600146 }
Matthew Barthacd737c2021-03-04 11:04:01 -0600147}
148
Matthew Barth48f44da2021-05-27 15:43:34 -0500149void Manager::powerStateChanged(bool powerStateOn)
150{
151 if (powerStateOn)
152 {
153 std::for_each(_zones.begin(), _zones.end(), [](const auto& entry) {
154 entry.second->setTarget(entry.second->getPoweronTarget());
155 });
156 }
157}
158
Matthew Barthacd737c2021-03-04 11:04:01 -0600159const std::vector<std::string>& Manager::getActiveProfiles()
160{
161 return _activeProfiles;
Matthew Bartha227a162020-08-05 10:51:45 -0500162}
163
Matthew Barth0206c722021-03-30 15:20:05 -0500164bool Manager::inConfig(const configKey& input, const configKey& comp)
165{
166 // Config names dont match, do not include in config
167 if (input.first != comp.first)
168 {
169 return false;
170 }
171 // No profiles specified by input config, can be used in any config
172 if (input.second.empty())
173 {
174 return true;
175 }
176 else
177 {
178 // Profiles must have one match in the other's profiles(and they must be
179 // an active profile) to be used in the config
180 return std::any_of(
181 input.second.begin(), input.second.end(),
182 [&comp](const auto& lProfile) {
183 return std::any_of(
184 comp.second.begin(), comp.second.end(),
185 [&lProfile](const auto& rProfile) {
186 if (lProfile != rProfile)
187 {
188 return false;
189 }
190 auto activeProfs = getActiveProfiles();
191 return std::find(activeProfs.begin(), activeProfs.end(),
192 lProfile) != activeProfs.end();
193 });
194 });
195 }
196}
197
Matthew Barth12cb1252021-03-08 16:47:30 -0600198bool Manager::hasOwner(const std::string& path, const std::string& intf)
199{
200 auto itServ = _servTree.find(path);
201 if (itServ == _servTree.end())
202 {
203 // Path not found in cache, therefore owner missing
204 return false;
205 }
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500206 for (const auto& service : itServ->second)
Matthew Barth12cb1252021-03-08 16:47:30 -0600207 {
208 auto itIntf = std::find_if(
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500209 service.second.second.begin(), service.second.second.end(),
Matthew Barth12cb1252021-03-08 16:47:30 -0600210 [&intf](const auto& interface) { return intf == interface; });
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500211 if (itIntf != std::end(service.second.second))
Matthew Barth12cb1252021-03-08 16:47:30 -0600212 {
213 // Service found, return owner state
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500214 return service.second.first;
Matthew Barth12cb1252021-03-08 16:47:30 -0600215 }
216 }
217 // Interface not found in cache, therefore owner missing
218 return false;
219}
220
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500221void Manager::setOwner(const std::string& path, const std::string& serv,
222 const std::string& intf, bool isOwned)
223{
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500224 // Set owner state for specific object given
225 auto& ownIntf = _servTree[path][serv];
226 ownIntf.first = isOwned;
227 auto itIntf = std::find_if(
228 ownIntf.second.begin(), ownIntf.second.end(),
229 [&intf](const auto& interface) { return intf == interface; });
230 if (itIntf == std::end(ownIntf.second))
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500231 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500232 ownIntf.second.emplace_back(intf);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500233 }
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500234
235 // Update owner state on all entries of the same `serv` & `intf`
236 for (auto& itPath : _servTree)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500237 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500238 if (itPath.first == path)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500239 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500240 // Already set/updated owner on this path for `serv` & `intf`
241 continue;
242 }
243 for (auto& itServ : itPath.second)
244 {
245 if (itServ.first != serv)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500246 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500247 continue;
248 }
249 auto itIntf = std::find_if(
250 itServ.second.second.begin(), itServ.second.second.end(),
251 [&intf](const auto& interface) { return intf == interface; });
252 if (itIntf != std::end(itServ.second.second))
253 {
254 itServ.second.first = isOwned;
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500255 }
256 }
257 }
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500258}
259
260const std::string& Manager::findService(const std::string& path,
261 const std::string& intf)
262{
263 static const std::string empty = "";
264
265 auto itServ = _servTree.find(path);
266 if (itServ != _servTree.end())
267 {
268 for (const auto& service : itServ->second)
269 {
270 auto itIntf = std::find_if(
271 service.second.second.begin(), service.second.second.end(),
272 [&intf](const auto& interface) { return intf == interface; });
273 if (itIntf != std::end(service.second.second))
274 {
275 // Service found, return service name
276 return service.first;
277 }
278 }
279 }
280
281 return empty;
282}
283
Matthew Barth98f6fc12021-04-16 10:48:37 -0500284void Manager::addServices(const std::string& intf, int32_t depth)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500285{
286 // Get all subtree objects for the given interface
Matt Spinler34835152021-07-01 12:28:58 -0600287 auto objects = util::SDBusPlus::getSubTreeRaw(util::SDBusPlus::getBus(),
288 "/", intf, depth);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500289 // Add what's returned to the cache of path->services
290 for (auto& itPath : objects)
291 {
292 auto pathIter = _servTree.find(itPath.first);
293 if (pathIter != _servTree.end())
294 {
295 // Path found in cache
296 for (auto& itServ : itPath.second)
297 {
298 auto servIter = pathIter->second.find(itServ.first);
299 if (servIter != pathIter->second.end())
300 {
301 // Service found in cache
302 for (auto& itIntf : itServ.second)
303 {
304 if (std::find(servIter->second.second.begin(),
305 servIter->second.second.end(),
306 itIntf) == servIter->second.second.end())
307 {
308 // Add interface to cache
309 servIter->second.second.emplace_back(itIntf);
310 }
311 }
312 }
313 else
314 {
315 // Service not found in cache
316 auto intfs = {intf};
317 pathIter->second[itServ.first] =
318 std::make_pair(true, intfs);
319 }
320 }
321 }
322 else
323 {
324 // Path not found in cache
325 auto intfs = {intf};
326 _servTree[itPath.first] = {
327 {itPath.second.begin()->first, std::make_pair(true, intfs)}};
328 }
329 }
330}
331
332const std::string& Manager::getService(const std::string& path,
333 const std::string& intf)
334{
335 // Retrieve service from cache
336 const auto& serviceName = findService(path, intf);
337 if (serviceName.empty())
338 {
Matthew Barth98f6fc12021-04-16 10:48:37 -0500339 addServices(intf, 0);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500340 return findService(path, intf);
341 }
342
343 return serviceName;
344}
345
Matthew Barthf41e9472021-05-13 16:38:06 -0500346std::vector<std::string> Manager::findPaths(const std::string& serv,
347 const std::string& intf)
348{
349 std::vector<std::string> paths;
350
351 for (const auto& path : _servTree)
352 {
353 auto itServ = path.second.find(serv);
354 if (itServ != path.second.end())
355 {
356 if (std::find(itServ->second.second.begin(),
357 itServ->second.second.end(),
358 intf) != itServ->second.second.end())
359 {
360 if (std::find(paths.begin(), paths.end(), path.first) ==
361 paths.end())
362 {
363 paths.push_back(path.first);
364 }
365 }
366 }
367 }
368
369 return paths;
370}
371
372std::vector<std::string> Manager::getPaths(const std::string& serv,
373 const std::string& intf)
374{
375 auto paths = findPaths(serv, intf);
376 if (paths.empty())
377 {
378 addServices(intf, 0);
379 return findPaths(serv, intf);
380 }
381
382 return paths;
383}
384
385void Manager::addObjects(const std::string& path, const std::string& intf,
386 const std::string& prop)
387{
388 auto service = getService(path, intf);
389 if (service.empty())
390 {
391 // Log service not found for object
Matt Spinler34835152021-07-01 12:28:58 -0600392 log<level::DEBUG>(
Matthew Barthf41e9472021-05-13 16:38:06 -0500393 fmt::format("Unable to get service name for path {}, interface {}",
394 path, intf)
395 .c_str());
396 return;
397 }
398
399 auto objMgrPaths = getPaths(service, "org.freedesktop.DBus.ObjectManager");
400 if (objMgrPaths.empty())
401 {
402 // No object manager interface provided by service?
403 // Attempt to retrieve property directly
404 auto variant = util::SDBusPlus::getPropertyVariant<PropertyVariantType>(
405 _bus, service, path, intf, prop);
406 _objects[path][intf][prop] = variant;
407 return;
408 }
409
410 for (const auto& objMgrPath : objMgrPaths)
411 {
412 // Get all managed objects of service
413 auto objects = util::SDBusPlus::getManagedObjects<PropertyVariantType>(
414 _bus, service, objMgrPath);
415
416 // Add what's returned to the cache of objects
417 for (auto& object : objects)
418 {
419 auto itPath = _objects.find(object.first);
420 if (itPath != _objects.end())
421 {
422 // Path found in cache
423 for (auto& interface : itPath->second)
424 {
425 auto itIntf = itPath->second.find(interface.first);
426 if (itIntf != itPath->second.end())
427 {
428 // Interface found in cache
429 for (auto& property : itIntf->second)
430 {
431 auto itProp = itIntf->second.find(property.first);
432 if (itProp != itIntf->second.end())
433 {
434 // Property found, update value
435 itProp->second = property.second;
436 }
437 else
438 {
439 itIntf->second.insert(property);
440 }
441 }
442 }
443 else
444 {
445 // Interface not found in cache
446 itPath->second.insert(interface);
447 }
448 }
449 }
450 else
451 {
452 // Path not found in cache
453 _objects.insert(object);
454 }
455 }
456 }
457}
458
459const std::optional<PropertyVariantType>
460 Manager::getProperty(const std::string& path, const std::string& intf,
461 const std::string& prop)
462{
463 // TODO Objects hosted by fan control (i.e. ThermalMode) are required to
464 // update the cache upon being set/updated
465 auto itPath = _objects.find(path);
466 if (itPath != _objects.end())
467 {
468 auto itIntf = itPath->second.find(intf);
469 if (itIntf != itPath->second.end())
470 {
471 auto itProp = itIntf->second.find(prop);
472 if (itProp != itIntf->second.end())
473 {
474 return itProp->second;
475 }
476 }
477 }
478
479 return std::nullopt;
480}
481
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500482void Manager::addTimer(const TimerType type,
483 const std::chrono::microseconds interval,
484 std::unique_ptr<TimerPkg> pkg)
485{
486 auto dataPtr =
487 std::make_unique<TimerData>(std::make_pair(type, std::move(*pkg)));
488 Timer timer(_event,
489 std::bind(&Manager::timerExpired, this, std::ref(*dataPtr)));
490 if (type == TimerType::repeating)
491 {
492 timer.restart(interval);
493 }
494 else if (type == TimerType::oneshot)
495 {
496 timer.restartOnce(interval);
497 }
498 else
499 {
500 throw std::invalid_argument("Invalid Timer Type");
501 }
502 _timers.emplace_back(std::move(dataPtr), std::move(timer));
503}
504
505void Manager::timerExpired(TimerData& data)
506{
507 auto& actions =
508 std::get<std::vector<std::unique_ptr<ActionBase>>&>(data.second);
509 // Perform the actions in the timer data
510 std::for_each(actions.begin(), actions.end(),
Matthew Barth00f6aa02021-04-09 10:49:47 -0500511 [](auto& action) { action->run(); });
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500512
513 // Remove oneshot timers after they expired
514 if (data.first == TimerType::oneshot)
515 {
516 auto itTimer = std::find_if(
517 _timers.begin(), _timers.end(), [&data](const auto& timer) {
518 return (data.first == timer.first->first &&
519 (std::get<std::string>(data.second) ==
520 std::get<std::string>(timer.first->second)));
521 });
522 if (itTimer != std::end(_timers))
523 {
524 _timers.erase(itTimer);
525 }
526 }
527}
528
Matthew Barthebabc042021-05-13 15:38:58 -0500529void Manager::handleSignal(sdbusplus::message::message& msg,
Matthew Barthfac8a2f2021-06-10 15:50:36 -0500530 const std::vector<SignalPkg>& pkgs)
Matthew Barthebabc042021-05-13 15:38:58 -0500531{
Matthew Barthfac8a2f2021-06-10 15:50:36 -0500532 for (auto& pkg : pkgs)
Matthew Barthebabc042021-05-13 15:38:58 -0500533 {
534 // Handle the signal callback and only run the actions if the handler
535 // updated the cache for the given SignalObject
536 if (std::get<SignalHandler>(pkg)(msg, std::get<SignalObject>(pkg),
537 *this))
538 {
539 // Perform the actions in the handler package
540 auto& actions = std::get<SignalActions>(pkg);
541 std::for_each(actions.begin(), actions.end(),
542 [](auto& action) { action.get()->run(); });
543 }
544 }
545}
546
Matthew Barthacd737c2021-03-04 11:04:01 -0600547void Manager::setProfiles()
548{
549 // Profiles JSON config file is optional
550 auto confFile = fan::JsonConfig::getConfFile(_bus, confAppName,
551 Profile::confFileName, true);
Matthew Barthe91ac862021-05-25 16:22:17 -0500552
553 _profiles.clear();
Matthew Barthacd737c2021-03-04 11:04:01 -0600554 if (!confFile.empty())
555 {
556 for (const auto& entry : fan::JsonConfig::load(confFile))
557 {
558 auto obj = std::make_unique<Profile>(entry);
559 _profiles.emplace(
560 std::make_pair(obj->getName(), obj->getProfiles()),
561 std::move(obj));
562 }
563 }
Matthew Barthe91ac862021-05-25 16:22:17 -0500564
Matthew Barthacd737c2021-03-04 11:04:01 -0600565 // Ensure all configurations use the same set of active profiles
566 // (In case a profile's active state changes during configuration)
Matthew Barthe91ac862021-05-25 16:22:17 -0500567 _activeProfiles.clear();
Matthew Barthacd737c2021-03-04 11:04:01 -0600568 for (const auto& profile : _profiles)
569 {
570 if (profile.second->isActive())
571 {
572 _activeProfiles.emplace_back(profile.first.first);
573 }
574 }
575}
576
Matthew Bartha227a162020-08-05 10:51:45 -0500577} // namespace phosphor::fan::control::json