blob: b29c5e95468931a48ec85f86a3c4a99d65be308e [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 Barthacd737c2021-03-04 11:04:01 -060032#include <sdeventplus/event.hpp>
Matthew Barthd9cb63b2021-03-24 14:36:49 -050033#include <sdeventplus/utility/timer.hpp>
Matthew Bartha227a162020-08-05 10:51:45 -050034
Matthew Barthde90fb42021-03-04 16:34:28 -060035#include <algorithm>
Matthew Barthd9cb63b2021-03-24 14:36:49 -050036#include <chrono>
Matthew Bartha227a162020-08-05 10:51:45 -050037#include <filesystem>
Matthew Barthd9cb63b2021-03-24 14:36:49 -050038#include <functional>
39#include <map>
40#include <memory>
41#include <tuple>
42#include <utility>
Matthew Barth06764942021-03-04 09:28:40 -060043#include <vector>
Matthew Bartha227a162020-08-05 10:51:45 -050044
45namespace phosphor::fan::control::json
46{
47
Matthew Barthacd737c2021-03-04 11:04:01 -060048using json = nlohmann::json;
49
50std::vector<std::string> Manager::_activeProfiles;
Matthew Barth12cb1252021-03-08 16:47:30 -060051std::map<std::string,
Matthew Barth4ca87fa2021-04-14 11:31:13 -050052 std::map<std::string, std::pair<bool, std::vector<std::string>>>>
Matthew Barth12cb1252021-03-08 16:47:30 -060053 Manager::_servTree;
Matthew Barth07fecfc2021-01-29 09:04:43 -060054std::map<std::string,
55 std::map<std::string, std::map<std::string, PropertyVariantType>>>
56 Manager::_objects;
Matthew Barthacd737c2021-03-04 11:04:01 -060057
Matthew Barth9403a212021-05-17 09:31:50 -050058Manager::Manager(const sdeventplus::Event& event) :
Matthew Barth48f44da2021-05-27 15:43:34 -050059 _bus(util::SDBusPlus::getBus()), _event(event),
60 _powerState(std::make_unique<PGoodState>(
61 util::SDBusPlus::getBus(),
62 std::bind(std::mem_fn(&Manager::powerStateChanged), this,
63 std::placeholders::_1)))
Matthew Bartha227a162020-08-05 10:51:45 -050064{
Matthew Barthe91ac862021-05-25 16:22:17 -050065 load();
66}
67
68void Manager::sighupHandler(sdeventplus::source::Signal&,
69 const struct signalfd_siginfo*)
70{
71 // Save current set of available and active profiles
72 std::map<configKey, std::unique_ptr<Profile>> profiles;
73 profiles.swap(_profiles);
74 std::vector<std::string> activeProfiles;
75 activeProfiles.swap(_activeProfiles);
76
77 try
78 {
79 load();
80 }
81 catch (std::runtime_error& re)
82 {
83 // Restore saved available and active profiles
84 _profiles.swap(profiles);
85 _activeProfiles.swap(activeProfiles);
86 log<level::ERR>("Error reloading configs, no changes made",
87 entry("LOAD_ERROR=%s", re.what()));
88 }
89}
90
91void Manager::load()
92{
Matthew Barthe91ac862021-05-25 16:22:17 -050093 // Load the available profiles and which are active
Matthew Barthacd737c2021-03-04 11:04:01 -060094 setProfiles();
95
96 // Load the zone configurations
Matthew Barthe91ac862021-05-25 16:22:17 -050097 auto zones = getConfig<Zone>(false, _event, this);
Matthew Barthde90fb42021-03-04 16:34:28 -060098 // Load the fan configurations and move each fan into its zone
Matthew Barth9403a212021-05-17 09:31:50 -050099 auto fans = getConfig<Fan>(false);
Matthew Barthde90fb42021-03-04 16:34:28 -0600100 for (auto& fan : fans)
101 {
Matthew Barth0206c722021-03-30 15:20:05 -0500102 configKey fanProfile =
103 std::make_pair(fan.second->getZone(), fan.first.second);
104 auto itZone = std::find_if(
Matthew Barthe91ac862021-05-25 16:22:17 -0500105 zones.begin(), zones.end(), [&fanProfile](const auto& zone) {
Matthew Barth0206c722021-03-30 15:20:05 -0500106 return Manager::inConfig(fanProfile, zone.first);
107 });
Matthew Barthe91ac862021-05-25 16:22:17 -0500108 if (itZone != zones.end())
Matthew Barthde90fb42021-03-04 16:34:28 -0600109 {
Matthew Barth6f787302021-03-25 15:01:01 -0500110 if (itZone->second->getTarget() != fan.second->getTarget() &&
111 fan.second->getTarget() != 0)
112 {
Matthew Barthe91ac862021-05-25 16:22:17 -0500113 // Update zone target to current target of the fan in the
114 // zone
Matthew Barth6f787302021-03-25 15:01:01 -0500115 itZone->second->setTarget(fan.second->getTarget());
116 }
Matthew Barthde90fb42021-03-04 16:34:28 -0600117 itZone->second->addFan(std::move(fan.second));
118 }
119 }
Matthew Barthe91ac862021-05-25 16:22:17 -0500120
121 // Load any events configured
122 auto events = getConfig<Event>(true, this, zones);
123
Matthew Barth14303a42021-05-21 13:04:08 -0500124 // Enable zones
Matthew Barthe91ac862021-05-25 16:22:17 -0500125 _zones = std::move(zones);
Matthew Barth14303a42021-05-21 13:04:08 -0500126 std::for_each(_zones.begin(), _zones.end(),
127 [](const auto& entry) { entry.second->enable(); });
Matthew Barthb584d812021-03-11 15:55:04 -0600128
Matthew Barthe91ac862021-05-25 16:22:17 -0500129 // Clear current timers and signal subscriptions before enabling events
130 // To save reloading services and/or objects into cache, do not clear cache
131 _timers.clear();
132 _signals.clear();
133
134 // Enable events
135 _events = std::move(events);
Matthew Barth54b5a242021-05-21 11:02:52 -0500136 std::for_each(_events.begin(), _events.end(),
137 [](const auto& entry) { entry.second->enable(); });
Matthew Barthacd737c2021-03-04 11:04:01 -0600138}
139
Matthew Barth48f44da2021-05-27 15:43:34 -0500140void Manager::powerStateChanged(bool powerStateOn)
141{
142 if (powerStateOn)
143 {
144 std::for_each(_zones.begin(), _zones.end(), [](const auto& entry) {
145 entry.second->setTarget(entry.second->getPoweronTarget());
146 });
147 }
148}
149
Matthew Barthacd737c2021-03-04 11:04:01 -0600150const std::vector<std::string>& Manager::getActiveProfiles()
151{
152 return _activeProfiles;
Matthew Bartha227a162020-08-05 10:51:45 -0500153}
154
Matthew Barth0206c722021-03-30 15:20:05 -0500155bool Manager::inConfig(const configKey& input, const configKey& comp)
156{
157 // Config names dont match, do not include in config
158 if (input.first != comp.first)
159 {
160 return false;
161 }
162 // No profiles specified by input config, can be used in any config
163 if (input.second.empty())
164 {
165 return true;
166 }
167 else
168 {
169 // Profiles must have one match in the other's profiles(and they must be
170 // an active profile) to be used in the config
171 return std::any_of(
172 input.second.begin(), input.second.end(),
173 [&comp](const auto& lProfile) {
174 return std::any_of(
175 comp.second.begin(), comp.second.end(),
176 [&lProfile](const auto& rProfile) {
177 if (lProfile != rProfile)
178 {
179 return false;
180 }
181 auto activeProfs = getActiveProfiles();
182 return std::find(activeProfs.begin(), activeProfs.end(),
183 lProfile) != activeProfs.end();
184 });
185 });
186 }
187}
188
Matthew Barth12cb1252021-03-08 16:47:30 -0600189bool Manager::hasOwner(const std::string& path, const std::string& intf)
190{
191 auto itServ = _servTree.find(path);
192 if (itServ == _servTree.end())
193 {
194 // Path not found in cache, therefore owner missing
195 return false;
196 }
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500197 for (const auto& service : itServ->second)
Matthew Barth12cb1252021-03-08 16:47:30 -0600198 {
199 auto itIntf = std::find_if(
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500200 service.second.second.begin(), service.second.second.end(),
Matthew Barth12cb1252021-03-08 16:47:30 -0600201 [&intf](const auto& interface) { return intf == interface; });
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500202 if (itIntf != std::end(service.second.second))
Matthew Barth12cb1252021-03-08 16:47:30 -0600203 {
204 // Service found, return owner state
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500205 return service.second.first;
Matthew Barth12cb1252021-03-08 16:47:30 -0600206 }
207 }
208 // Interface not found in cache, therefore owner missing
209 return false;
210}
211
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500212void Manager::setOwner(const std::string& path, const std::string& serv,
213 const std::string& intf, bool isOwned)
214{
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500215 // Set owner state for specific object given
216 auto& ownIntf = _servTree[path][serv];
217 ownIntf.first = isOwned;
218 auto itIntf = std::find_if(
219 ownIntf.second.begin(), ownIntf.second.end(),
220 [&intf](const auto& interface) { return intf == interface; });
221 if (itIntf == std::end(ownIntf.second))
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500222 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500223 ownIntf.second.emplace_back(intf);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500224 }
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500225
226 // Update owner state on all entries of the same `serv` & `intf`
227 for (auto& itPath : _servTree)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500228 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500229 if (itPath.first == path)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500230 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500231 // Already set/updated owner on this path for `serv` & `intf`
232 continue;
233 }
234 for (auto& itServ : itPath.second)
235 {
236 if (itServ.first != serv)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500237 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500238 continue;
239 }
240 auto itIntf = std::find_if(
241 itServ.second.second.begin(), itServ.second.second.end(),
242 [&intf](const auto& interface) { return intf == interface; });
243 if (itIntf != std::end(itServ.second.second))
244 {
245 itServ.second.first = isOwned;
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500246 }
247 }
248 }
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500249}
250
251const std::string& Manager::findService(const std::string& path,
252 const std::string& intf)
253{
254 static const std::string empty = "";
255
256 auto itServ = _servTree.find(path);
257 if (itServ != _servTree.end())
258 {
259 for (const auto& service : itServ->second)
260 {
261 auto itIntf = std::find_if(
262 service.second.second.begin(), service.second.second.end(),
263 [&intf](const auto& interface) { return intf == interface; });
264 if (itIntf != std::end(service.second.second))
265 {
266 // Service found, return service name
267 return service.first;
268 }
269 }
270 }
271
272 return empty;
273}
274
Matthew Barth98f6fc12021-04-16 10:48:37 -0500275void Manager::addServices(const std::string& intf, int32_t depth)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500276{
277 // Get all subtree objects for the given interface
278 auto objects = util::SDBusPlus::getSubTree(util::SDBusPlus::getBus(), "/",
279 intf, depth);
280 // Add what's returned to the cache of path->services
281 for (auto& itPath : objects)
282 {
283 auto pathIter = _servTree.find(itPath.first);
284 if (pathIter != _servTree.end())
285 {
286 // Path found in cache
287 for (auto& itServ : itPath.second)
288 {
289 auto servIter = pathIter->second.find(itServ.first);
290 if (servIter != pathIter->second.end())
291 {
292 // Service found in cache
293 for (auto& itIntf : itServ.second)
294 {
295 if (std::find(servIter->second.second.begin(),
296 servIter->second.second.end(),
297 itIntf) == servIter->second.second.end())
298 {
299 // Add interface to cache
300 servIter->second.second.emplace_back(itIntf);
301 }
302 }
303 }
304 else
305 {
306 // Service not found in cache
307 auto intfs = {intf};
308 pathIter->second[itServ.first] =
309 std::make_pair(true, intfs);
310 }
311 }
312 }
313 else
314 {
315 // Path not found in cache
316 auto intfs = {intf};
317 _servTree[itPath.first] = {
318 {itPath.second.begin()->first, std::make_pair(true, intfs)}};
319 }
320 }
321}
322
323const std::string& Manager::getService(const std::string& path,
324 const std::string& intf)
325{
326 // Retrieve service from cache
327 const auto& serviceName = findService(path, intf);
328 if (serviceName.empty())
329 {
Matthew Barth98f6fc12021-04-16 10:48:37 -0500330 addServices(intf, 0);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500331 return findService(path, intf);
332 }
333
334 return serviceName;
335}
336
Matthew Barthf41e9472021-05-13 16:38:06 -0500337std::vector<std::string> Manager::findPaths(const std::string& serv,
338 const std::string& intf)
339{
340 std::vector<std::string> paths;
341
342 for (const auto& path : _servTree)
343 {
344 auto itServ = path.second.find(serv);
345 if (itServ != path.second.end())
346 {
347 if (std::find(itServ->second.second.begin(),
348 itServ->second.second.end(),
349 intf) != itServ->second.second.end())
350 {
351 if (std::find(paths.begin(), paths.end(), path.first) ==
352 paths.end())
353 {
354 paths.push_back(path.first);
355 }
356 }
357 }
358 }
359
360 return paths;
361}
362
363std::vector<std::string> Manager::getPaths(const std::string& serv,
364 const std::string& intf)
365{
366 auto paths = findPaths(serv, intf);
367 if (paths.empty())
368 {
369 addServices(intf, 0);
370 return findPaths(serv, intf);
371 }
372
373 return paths;
374}
375
376void Manager::addObjects(const std::string& path, const std::string& intf,
377 const std::string& prop)
378{
379 auto service = getService(path, intf);
380 if (service.empty())
381 {
382 // Log service not found for object
383 log<level::ERR>(
384 fmt::format("Unable to get service name for path {}, interface {}",
385 path, intf)
386 .c_str());
387 return;
388 }
389
390 auto objMgrPaths = getPaths(service, "org.freedesktop.DBus.ObjectManager");
391 if (objMgrPaths.empty())
392 {
393 // No object manager interface provided by service?
394 // Attempt to retrieve property directly
395 auto variant = util::SDBusPlus::getPropertyVariant<PropertyVariantType>(
396 _bus, service, path, intf, prop);
397 _objects[path][intf][prop] = variant;
398 return;
399 }
400
401 for (const auto& objMgrPath : objMgrPaths)
402 {
403 // Get all managed objects of service
404 auto objects = util::SDBusPlus::getManagedObjects<PropertyVariantType>(
405 _bus, service, objMgrPath);
406
407 // Add what's returned to the cache of objects
408 for (auto& object : objects)
409 {
410 auto itPath = _objects.find(object.first);
411 if (itPath != _objects.end())
412 {
413 // Path found in cache
414 for (auto& interface : itPath->second)
415 {
416 auto itIntf = itPath->second.find(interface.first);
417 if (itIntf != itPath->second.end())
418 {
419 // Interface found in cache
420 for (auto& property : itIntf->second)
421 {
422 auto itProp = itIntf->second.find(property.first);
423 if (itProp != itIntf->second.end())
424 {
425 // Property found, update value
426 itProp->second = property.second;
427 }
428 else
429 {
430 itIntf->second.insert(property);
431 }
432 }
433 }
434 else
435 {
436 // Interface not found in cache
437 itPath->second.insert(interface);
438 }
439 }
440 }
441 else
442 {
443 // Path not found in cache
444 _objects.insert(object);
445 }
446 }
447 }
448}
449
450const std::optional<PropertyVariantType>
451 Manager::getProperty(const std::string& path, const std::string& intf,
452 const std::string& prop)
453{
454 // TODO Objects hosted by fan control (i.e. ThermalMode) are required to
455 // update the cache upon being set/updated
456 auto itPath = _objects.find(path);
457 if (itPath != _objects.end())
458 {
459 auto itIntf = itPath->second.find(intf);
460 if (itIntf != itPath->second.end())
461 {
462 auto itProp = itIntf->second.find(prop);
463 if (itProp != itIntf->second.end())
464 {
465 return itProp->second;
466 }
467 }
468 }
469
470 return std::nullopt;
471}
472
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500473void Manager::addTimer(const TimerType type,
474 const std::chrono::microseconds interval,
475 std::unique_ptr<TimerPkg> pkg)
476{
477 auto dataPtr =
478 std::make_unique<TimerData>(std::make_pair(type, std::move(*pkg)));
479 Timer timer(_event,
480 std::bind(&Manager::timerExpired, this, std::ref(*dataPtr)));
481 if (type == TimerType::repeating)
482 {
483 timer.restart(interval);
484 }
485 else if (type == TimerType::oneshot)
486 {
487 timer.restartOnce(interval);
488 }
489 else
490 {
491 throw std::invalid_argument("Invalid Timer Type");
492 }
493 _timers.emplace_back(std::move(dataPtr), std::move(timer));
494}
495
496void Manager::timerExpired(TimerData& data)
497{
498 auto& actions =
499 std::get<std::vector<std::unique_ptr<ActionBase>>&>(data.second);
500 // Perform the actions in the timer data
501 std::for_each(actions.begin(), actions.end(),
Matthew Barth00f6aa02021-04-09 10:49:47 -0500502 [](auto& action) { action->run(); });
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500503
504 // Remove oneshot timers after they expired
505 if (data.first == TimerType::oneshot)
506 {
507 auto itTimer = std::find_if(
508 _timers.begin(), _timers.end(), [&data](const auto& timer) {
509 return (data.first == timer.first->first &&
510 (std::get<std::string>(data.second) ==
511 std::get<std::string>(timer.first->second)));
512 });
513 if (itTimer != std::end(_timers))
514 {
515 _timers.erase(itTimer);
516 }
517 }
518}
519
Matthew Barthebabc042021-05-13 15:38:58 -0500520void Manager::handleSignal(sdbusplus::message::message& msg,
521 const std::vector<SignalPkg>* pkgs)
522{
523 for (auto& pkg : *pkgs)
524 {
525 // Handle the signal callback and only run the actions if the handler
526 // updated the cache for the given SignalObject
527 if (std::get<SignalHandler>(pkg)(msg, std::get<SignalObject>(pkg),
528 *this))
529 {
530 // Perform the actions in the handler package
531 auto& actions = std::get<SignalActions>(pkg);
532 std::for_each(actions.begin(), actions.end(),
533 [](auto& action) { action.get()->run(); });
534 }
535 }
536}
537
Matthew Barthacd737c2021-03-04 11:04:01 -0600538void Manager::setProfiles()
539{
540 // Profiles JSON config file is optional
541 auto confFile = fan::JsonConfig::getConfFile(_bus, confAppName,
542 Profile::confFileName, true);
Matthew Barthe91ac862021-05-25 16:22:17 -0500543
544 _profiles.clear();
Matthew Barthacd737c2021-03-04 11:04:01 -0600545 if (!confFile.empty())
546 {
547 for (const auto& entry : fan::JsonConfig::load(confFile))
548 {
549 auto obj = std::make_unique<Profile>(entry);
550 _profiles.emplace(
551 std::make_pair(obj->getName(), obj->getProfiles()),
552 std::move(obj));
553 }
554 }
Matthew Barthe91ac862021-05-25 16:22:17 -0500555
Matthew Barthacd737c2021-03-04 11:04:01 -0600556 // Ensure all configurations use the same set of active profiles
557 // (In case a profile's active state changes during configuration)
Matthew Barthe91ac862021-05-25 16:22:17 -0500558 _activeProfiles.clear();
Matthew Barthacd737c2021-03-04 11:04:01 -0600559 for (const auto& profile : _profiles)
560 {
561 if (profile.second->isActive())
562 {
563 _activeProfiles.emplace_back(profile.first.first);
564 }
565 }
566}
567
Matthew Bartha227a162020-08-05 10:51:45 -0500568} // namespace phosphor::fan::control::json