blob: 78d30f3d58737c6b94be5aed391fee0fc47d7aae [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 Barth06764942021-03-04 09:28:40 -060025#include "profile.hpp"
Matthew Barth9403a212021-05-17 09:31:50 -050026#include "sdbusplus.hpp"
Matthew Barthacd737c2021-03-04 11:04:01 -060027#include "zone.hpp"
Matthew Bartha227a162020-08-05 10:51:45 -050028
Matthew Barthacd737c2021-03-04 11:04:01 -060029#include <nlohmann/json.hpp>
Matthew Bartha227a162020-08-05 10:51:45 -050030#include <sdbusplus/bus.hpp>
Matthew Barthacd737c2021-03-04 11:04:01 -060031#include <sdeventplus/event.hpp>
Matthew Barthd9cb63b2021-03-24 14:36:49 -050032#include <sdeventplus/utility/timer.hpp>
Matthew Bartha227a162020-08-05 10:51:45 -050033
Matthew Barthde90fb42021-03-04 16:34:28 -060034#include <algorithm>
Matthew Barthd9cb63b2021-03-24 14:36:49 -050035#include <chrono>
Matthew Bartha227a162020-08-05 10:51:45 -050036#include <filesystem>
Matthew Barthd9cb63b2021-03-24 14:36:49 -050037#include <functional>
38#include <map>
39#include <memory>
40#include <tuple>
41#include <utility>
Matthew Barth06764942021-03-04 09:28:40 -060042#include <vector>
Matthew Bartha227a162020-08-05 10:51:45 -050043
44namespace phosphor::fan::control::json
45{
46
Matthew Barthacd737c2021-03-04 11:04:01 -060047using json = nlohmann::json;
48
49std::vector<std::string> Manager::_activeProfiles;
Matthew Barth12cb1252021-03-08 16:47:30 -060050std::map<std::string,
Matthew Barth4ca87fa2021-04-14 11:31:13 -050051 std::map<std::string, std::pair<bool, std::vector<std::string>>>>
Matthew Barth12cb1252021-03-08 16:47:30 -060052 Manager::_servTree;
Matthew Barth07fecfc2021-01-29 09:04:43 -060053std::map<std::string,
54 std::map<std::string, std::map<std::string, PropertyVariantType>>>
55 Manager::_objects;
Matthew Barthacd737c2021-03-04 11:04:01 -060056
Matthew Barth9403a212021-05-17 09:31:50 -050057Manager::Manager(const sdeventplus::Event& event) :
58 _bus(util::SDBusPlus::getBus()), _event(event)
Matthew Bartha227a162020-08-05 10:51:45 -050059{
Matthew Barthe91ac862021-05-25 16:22:17 -050060 load();
61}
62
63void Manager::sighupHandler(sdeventplus::source::Signal&,
64 const struct signalfd_siginfo*)
65{
66 // Save current set of available and active profiles
67 std::map<configKey, std::unique_ptr<Profile>> profiles;
68 profiles.swap(_profiles);
69 std::vector<std::string> activeProfiles;
70 activeProfiles.swap(_activeProfiles);
71
72 try
73 {
74 load();
75 }
76 catch (std::runtime_error& re)
77 {
78 // Restore saved available and active profiles
79 _profiles.swap(profiles);
80 _activeProfiles.swap(activeProfiles);
81 log<level::ERR>("Error reloading configs, no changes made",
82 entry("LOAD_ERROR=%s", re.what()));
83 }
84}
85
86void Manager::load()
87{
88 // TODO - Remove Manager JSON config support since it's not needed with init
89 // mode removed
Matthew Bartha227a162020-08-05 10:51:45 -050090 auto confFile =
Matthew Barth9403a212021-05-17 09:31:50 -050091 fan::JsonConfig::getConfFile(_bus, confAppName, confFileName, true);
Matthew Bartha227a162020-08-05 10:51:45 -050092 if (!confFile.empty())
93 {
94 _jsonObj = fan::JsonConfig::load(confFile);
95 }
Matthew Barth06764942021-03-04 09:28:40 -060096
Matthew Barthe91ac862021-05-25 16:22:17 -050097 // Load the available profiles and which are active
Matthew Barthacd737c2021-03-04 11:04:01 -060098 setProfiles();
99
100 // Load the zone configurations
Matthew Barthe91ac862021-05-25 16:22:17 -0500101 auto zones = getConfig<Zone>(false, _event, this);
Matthew Barthde90fb42021-03-04 16:34:28 -0600102 // Load the fan configurations and move each fan into its zone
Matthew Barth9403a212021-05-17 09:31:50 -0500103 auto fans = getConfig<Fan>(false);
Matthew Barthde90fb42021-03-04 16:34:28 -0600104 for (auto& fan : fans)
105 {
Matthew Barth0206c722021-03-30 15:20:05 -0500106 configKey fanProfile =
107 std::make_pair(fan.second->getZone(), fan.first.second);
108 auto itZone = std::find_if(
Matthew Barthe91ac862021-05-25 16:22:17 -0500109 zones.begin(), zones.end(), [&fanProfile](const auto& zone) {
Matthew Barth0206c722021-03-30 15:20:05 -0500110 return Manager::inConfig(fanProfile, zone.first);
111 });
Matthew Barthe91ac862021-05-25 16:22:17 -0500112 if (itZone != zones.end())
Matthew Barthde90fb42021-03-04 16:34:28 -0600113 {
Matthew Barth6f787302021-03-25 15:01:01 -0500114 if (itZone->second->getTarget() != fan.second->getTarget() &&
115 fan.second->getTarget() != 0)
116 {
Matthew Barthe91ac862021-05-25 16:22:17 -0500117 // Update zone target to current target of the fan in the
118 // zone
Matthew Barth6f787302021-03-25 15:01:01 -0500119 itZone->second->setTarget(fan.second->getTarget());
120 }
Matthew Barthde90fb42021-03-04 16:34:28 -0600121 itZone->second->addFan(std::move(fan.second));
122 }
123 }
Matthew Barthe91ac862021-05-25 16:22:17 -0500124
125 // Load any events configured
126 auto events = getConfig<Event>(true, this, zones);
127
Matthew Barth14303a42021-05-21 13:04:08 -0500128 // Enable zones
Matthew Barthe91ac862021-05-25 16:22:17 -0500129 _zones = std::move(zones);
Matthew Barth14303a42021-05-21 13:04:08 -0500130 std::for_each(_zones.begin(), _zones.end(),
131 [](const auto& entry) { entry.second->enable(); });
Matthew Barthb584d812021-03-11 15:55:04 -0600132
Matthew Barthe91ac862021-05-25 16:22:17 -0500133 // Clear current timers and signal subscriptions before enabling events
134 // To save reloading services and/or objects into cache, do not clear cache
135 _timers.clear();
136 _signals.clear();
137
138 // Enable events
139 _events = std::move(events);
Matthew Barth54b5a242021-05-21 11:02:52 -0500140 std::for_each(_events.begin(), _events.end(),
141 [](const auto& entry) { entry.second->enable(); });
Matthew Barthacd737c2021-03-04 11:04:01 -0600142}
143
144const std::vector<std::string>& Manager::getActiveProfiles()
145{
146 return _activeProfiles;
Matthew Bartha227a162020-08-05 10:51:45 -0500147}
148
Matthew Barth0206c722021-03-30 15:20:05 -0500149bool Manager::inConfig(const configKey& input, const configKey& comp)
150{
151 // Config names dont match, do not include in config
152 if (input.first != comp.first)
153 {
154 return false;
155 }
156 // No profiles specified by input config, can be used in any config
157 if (input.second.empty())
158 {
159 return true;
160 }
161 else
162 {
163 // Profiles must have one match in the other's profiles(and they must be
164 // an active profile) to be used in the config
165 return std::any_of(
166 input.second.begin(), input.second.end(),
167 [&comp](const auto& lProfile) {
168 return std::any_of(
169 comp.second.begin(), comp.second.end(),
170 [&lProfile](const auto& rProfile) {
171 if (lProfile != rProfile)
172 {
173 return false;
174 }
175 auto activeProfs = getActiveProfiles();
176 return std::find(activeProfs.begin(), activeProfs.end(),
177 lProfile) != activeProfs.end();
178 });
179 });
180 }
181}
182
Matthew Barth12cb1252021-03-08 16:47:30 -0600183bool Manager::hasOwner(const std::string& path, const std::string& intf)
184{
185 auto itServ = _servTree.find(path);
186 if (itServ == _servTree.end())
187 {
188 // Path not found in cache, therefore owner missing
189 return false;
190 }
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500191 for (const auto& service : itServ->second)
Matthew Barth12cb1252021-03-08 16:47:30 -0600192 {
193 auto itIntf = std::find_if(
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500194 service.second.second.begin(), service.second.second.end(),
Matthew Barth12cb1252021-03-08 16:47:30 -0600195 [&intf](const auto& interface) { return intf == interface; });
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500196 if (itIntf != std::end(service.second.second))
Matthew Barth12cb1252021-03-08 16:47:30 -0600197 {
198 // Service found, return owner state
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500199 return service.second.first;
Matthew Barth12cb1252021-03-08 16:47:30 -0600200 }
201 }
202 // Interface not found in cache, therefore owner missing
203 return false;
204}
205
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500206void Manager::setOwner(const std::string& path, const std::string& serv,
207 const std::string& intf, bool isOwned)
208{
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500209 // Set owner state for specific object given
210 auto& ownIntf = _servTree[path][serv];
211 ownIntf.first = isOwned;
212 auto itIntf = std::find_if(
213 ownIntf.second.begin(), ownIntf.second.end(),
214 [&intf](const auto& interface) { return intf == interface; });
215 if (itIntf == std::end(ownIntf.second))
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500216 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500217 ownIntf.second.emplace_back(intf);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500218 }
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500219
220 // Update owner state on all entries of the same `serv` & `intf`
221 for (auto& itPath : _servTree)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500222 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500223 if (itPath.first == path)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500224 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500225 // Already set/updated owner on this path for `serv` & `intf`
226 continue;
227 }
228 for (auto& itServ : itPath.second)
229 {
230 if (itServ.first != serv)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500231 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500232 continue;
233 }
234 auto itIntf = std::find_if(
235 itServ.second.second.begin(), itServ.second.second.end(),
236 [&intf](const auto& interface) { return intf == interface; });
237 if (itIntf != std::end(itServ.second.second))
238 {
239 itServ.second.first = isOwned;
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500240 }
241 }
242 }
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500243}
244
245const std::string& Manager::findService(const std::string& path,
246 const std::string& intf)
247{
248 static const std::string empty = "";
249
250 auto itServ = _servTree.find(path);
251 if (itServ != _servTree.end())
252 {
253 for (const auto& service : itServ->second)
254 {
255 auto itIntf = std::find_if(
256 service.second.second.begin(), service.second.second.end(),
257 [&intf](const auto& interface) { return intf == interface; });
258 if (itIntf != std::end(service.second.second))
259 {
260 // Service found, return service name
261 return service.first;
262 }
263 }
264 }
265
266 return empty;
267}
268
Matthew Barth98f6fc12021-04-16 10:48:37 -0500269void Manager::addServices(const std::string& intf, int32_t depth)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500270{
271 // Get all subtree objects for the given interface
272 auto objects = util::SDBusPlus::getSubTree(util::SDBusPlus::getBus(), "/",
273 intf, depth);
274 // Add what's returned to the cache of path->services
275 for (auto& itPath : objects)
276 {
277 auto pathIter = _servTree.find(itPath.first);
278 if (pathIter != _servTree.end())
279 {
280 // Path found in cache
281 for (auto& itServ : itPath.second)
282 {
283 auto servIter = pathIter->second.find(itServ.first);
284 if (servIter != pathIter->second.end())
285 {
286 // Service found in cache
287 for (auto& itIntf : itServ.second)
288 {
289 if (std::find(servIter->second.second.begin(),
290 servIter->second.second.end(),
291 itIntf) == servIter->second.second.end())
292 {
293 // Add interface to cache
294 servIter->second.second.emplace_back(itIntf);
295 }
296 }
297 }
298 else
299 {
300 // Service not found in cache
301 auto intfs = {intf};
302 pathIter->second[itServ.first] =
303 std::make_pair(true, intfs);
304 }
305 }
306 }
307 else
308 {
309 // Path not found in cache
310 auto intfs = {intf};
311 _servTree[itPath.first] = {
312 {itPath.second.begin()->first, std::make_pair(true, intfs)}};
313 }
314 }
315}
316
317const std::string& Manager::getService(const std::string& path,
318 const std::string& intf)
319{
320 // Retrieve service from cache
321 const auto& serviceName = findService(path, intf);
322 if (serviceName.empty())
323 {
Matthew Barth98f6fc12021-04-16 10:48:37 -0500324 addServices(intf, 0);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500325 return findService(path, intf);
326 }
327
328 return serviceName;
329}
330
Matthew Barthf41e9472021-05-13 16:38:06 -0500331std::vector<std::string> Manager::findPaths(const std::string& serv,
332 const std::string& intf)
333{
334 std::vector<std::string> paths;
335
336 for (const auto& path : _servTree)
337 {
338 auto itServ = path.second.find(serv);
339 if (itServ != path.second.end())
340 {
341 if (std::find(itServ->second.second.begin(),
342 itServ->second.second.end(),
343 intf) != itServ->second.second.end())
344 {
345 if (std::find(paths.begin(), paths.end(), path.first) ==
346 paths.end())
347 {
348 paths.push_back(path.first);
349 }
350 }
351 }
352 }
353
354 return paths;
355}
356
357std::vector<std::string> Manager::getPaths(const std::string& serv,
358 const std::string& intf)
359{
360 auto paths = findPaths(serv, intf);
361 if (paths.empty())
362 {
363 addServices(intf, 0);
364 return findPaths(serv, intf);
365 }
366
367 return paths;
368}
369
370void Manager::addObjects(const std::string& path, const std::string& intf,
371 const std::string& prop)
372{
373 auto service = getService(path, intf);
374 if (service.empty())
375 {
376 // Log service not found for object
377 log<level::ERR>(
378 fmt::format("Unable to get service name for path {}, interface {}",
379 path, intf)
380 .c_str());
381 return;
382 }
383
384 auto objMgrPaths = getPaths(service, "org.freedesktop.DBus.ObjectManager");
385 if (objMgrPaths.empty())
386 {
387 // No object manager interface provided by service?
388 // Attempt to retrieve property directly
389 auto variant = util::SDBusPlus::getPropertyVariant<PropertyVariantType>(
390 _bus, service, path, intf, prop);
391 _objects[path][intf][prop] = variant;
392 return;
393 }
394
395 for (const auto& objMgrPath : objMgrPaths)
396 {
397 // Get all managed objects of service
398 auto objects = util::SDBusPlus::getManagedObjects<PropertyVariantType>(
399 _bus, service, objMgrPath);
400
401 // Add what's returned to the cache of objects
402 for (auto& object : objects)
403 {
404 auto itPath = _objects.find(object.first);
405 if (itPath != _objects.end())
406 {
407 // Path found in cache
408 for (auto& interface : itPath->second)
409 {
410 auto itIntf = itPath->second.find(interface.first);
411 if (itIntf != itPath->second.end())
412 {
413 // Interface found in cache
414 for (auto& property : itIntf->second)
415 {
416 auto itProp = itIntf->second.find(property.first);
417 if (itProp != itIntf->second.end())
418 {
419 // Property found, update value
420 itProp->second = property.second;
421 }
422 else
423 {
424 itIntf->second.insert(property);
425 }
426 }
427 }
428 else
429 {
430 // Interface not found in cache
431 itPath->second.insert(interface);
432 }
433 }
434 }
435 else
436 {
437 // Path not found in cache
438 _objects.insert(object);
439 }
440 }
441 }
442}
443
444const std::optional<PropertyVariantType>
445 Manager::getProperty(const std::string& path, const std::string& intf,
446 const std::string& prop)
447{
448 // TODO Objects hosted by fan control (i.e. ThermalMode) are required to
449 // update the cache upon being set/updated
450 auto itPath = _objects.find(path);
451 if (itPath != _objects.end())
452 {
453 auto itIntf = itPath->second.find(intf);
454 if (itIntf != itPath->second.end())
455 {
456 auto itProp = itIntf->second.find(prop);
457 if (itProp != itIntf->second.end())
458 {
459 return itProp->second;
460 }
461 }
462 }
463
464 return std::nullopt;
465}
466
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500467void Manager::addTimer(const TimerType type,
468 const std::chrono::microseconds interval,
469 std::unique_ptr<TimerPkg> pkg)
470{
471 auto dataPtr =
472 std::make_unique<TimerData>(std::make_pair(type, std::move(*pkg)));
473 Timer timer(_event,
474 std::bind(&Manager::timerExpired, this, std::ref(*dataPtr)));
475 if (type == TimerType::repeating)
476 {
477 timer.restart(interval);
478 }
479 else if (type == TimerType::oneshot)
480 {
481 timer.restartOnce(interval);
482 }
483 else
484 {
485 throw std::invalid_argument("Invalid Timer Type");
486 }
487 _timers.emplace_back(std::move(dataPtr), std::move(timer));
488}
489
490void Manager::timerExpired(TimerData& data)
491{
492 auto& actions =
493 std::get<std::vector<std::unique_ptr<ActionBase>>&>(data.second);
494 // Perform the actions in the timer data
495 std::for_each(actions.begin(), actions.end(),
Matthew Barth00f6aa02021-04-09 10:49:47 -0500496 [](auto& action) { action->run(); });
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500497
498 // Remove oneshot timers after they expired
499 if (data.first == TimerType::oneshot)
500 {
501 auto itTimer = std::find_if(
502 _timers.begin(), _timers.end(), [&data](const auto& timer) {
503 return (data.first == timer.first->first &&
504 (std::get<std::string>(data.second) ==
505 std::get<std::string>(timer.first->second)));
506 });
507 if (itTimer != std::end(_timers))
508 {
509 _timers.erase(itTimer);
510 }
511 }
512}
513
Matthew Barthebabc042021-05-13 15:38:58 -0500514void Manager::handleSignal(sdbusplus::message::message& msg,
515 const std::vector<SignalPkg>* pkgs)
516{
517 for (auto& pkg : *pkgs)
518 {
519 // Handle the signal callback and only run the actions if the handler
520 // updated the cache for the given SignalObject
521 if (std::get<SignalHandler>(pkg)(msg, std::get<SignalObject>(pkg),
522 *this))
523 {
524 // Perform the actions in the handler package
525 auto& actions = std::get<SignalActions>(pkg);
526 std::for_each(actions.begin(), actions.end(),
527 [](auto& action) { action.get()->run(); });
528 }
529 }
530}
531
Matthew Bartha227a162020-08-05 10:51:45 -0500532unsigned int Manager::getPowerOnDelay()
533{
534 auto powerOnDelay = 0;
535
536 // Parse optional "power_on_delay" from JSON object
537 if (!_jsonObj.empty() && _jsonObj.contains("power_on_delay"))
538 {
539 powerOnDelay = _jsonObj["power_on_delay"].get<unsigned int>();
540 }
541
542 return powerOnDelay;
543}
544
Matthew Barthacd737c2021-03-04 11:04:01 -0600545void Manager::setProfiles()
546{
547 // Profiles JSON config file is optional
548 auto confFile = fan::JsonConfig::getConfFile(_bus, confAppName,
549 Profile::confFileName, true);
Matthew Barthe91ac862021-05-25 16:22:17 -0500550
551 _profiles.clear();
Matthew Barthacd737c2021-03-04 11:04:01 -0600552 if (!confFile.empty())
553 {
554 for (const auto& entry : fan::JsonConfig::load(confFile))
555 {
556 auto obj = std::make_unique<Profile>(entry);
557 _profiles.emplace(
558 std::make_pair(obj->getName(), obj->getProfiles()),
559 std::move(obj));
560 }
561 }
Matthew Barthe91ac862021-05-25 16:22:17 -0500562
Matthew Barthacd737c2021-03-04 11:04:01 -0600563 // Ensure all configurations use the same set of active profiles
564 // (In case a profile's active state changes during configuration)
Matthew Barthe91ac862021-05-25 16:22:17 -0500565 _activeProfiles.clear();
Matthew Barthacd737c2021-03-04 11:04:01 -0600566 for (const auto& profile : _profiles)
567 {
568 if (profile.second->isActive())
569 {
570 _activeProfiles.emplace_back(profile.first.first);
571 }
572 }
573}
574
Matthew Bartha227a162020-08-05 10:51:45 -0500575} // namespace phosphor::fan::control::json