blob: 4ba61e7d5c0e710e7b0085b6de3f55d475999187 [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;
Matthew Barthacd737c2021-03-04 11:04:01 -060058
Matthew Barth9403a212021-05-17 09:31:50 -050059Manager::Manager(const sdeventplus::Event& event) :
Matthew Barth48f44da2021-05-27 15:43:34 -050060 _bus(util::SDBusPlus::getBus()), _event(event),
Matthew Barth1542fb52021-06-10 14:09:09 -050061 _mgr(util::SDBusPlus::getBus(), CONTROL_OBJPATH),
Matthew Barth48f44da2021-05-27 15:43:34 -050062 _powerState(std::make_unique<PGoodState>(
63 util::SDBusPlus::getBus(),
64 std::bind(std::mem_fn(&Manager::powerStateChanged), this,
65 std::placeholders::_1)))
Matthew Bartha227a162020-08-05 10:51:45 -050066{
Matthew Barthe91ac862021-05-25 16:22:17 -050067 load();
68}
69
70void Manager::sighupHandler(sdeventplus::source::Signal&,
71 const struct signalfd_siginfo*)
72{
73 // Save current set of available and active profiles
74 std::map<configKey, std::unique_ptr<Profile>> profiles;
75 profiles.swap(_profiles);
76 std::vector<std::string> activeProfiles;
77 activeProfiles.swap(_activeProfiles);
78
79 try
80 {
81 load();
82 }
83 catch (std::runtime_error& re)
84 {
85 // Restore saved available and active profiles
86 _profiles.swap(profiles);
87 _activeProfiles.swap(activeProfiles);
88 log<level::ERR>("Error reloading configs, no changes made",
89 entry("LOAD_ERROR=%s", re.what()));
90 }
91}
92
93void Manager::load()
94{
Matthew Barthe91ac862021-05-25 16:22:17 -050095 // Load the available profiles and which are active
Matthew Barthacd737c2021-03-04 11:04:01 -060096 setProfiles();
97
98 // Load the zone configurations
Matthew Barthe91ac862021-05-25 16:22:17 -050099 auto zones = getConfig<Zone>(false, _event, this);
Matthew Barthde90fb42021-03-04 16:34:28 -0600100 // Load the fan configurations and move each fan into its zone
Matthew Barth9403a212021-05-17 09:31:50 -0500101 auto fans = getConfig<Fan>(false);
Matthew Barthde90fb42021-03-04 16:34:28 -0600102 for (auto& fan : fans)
103 {
Matthew Barth0206c722021-03-30 15:20:05 -0500104 configKey fanProfile =
105 std::make_pair(fan.second->getZone(), fan.first.second);
106 auto itZone = std::find_if(
Matthew Barthe91ac862021-05-25 16:22:17 -0500107 zones.begin(), zones.end(), [&fanProfile](const auto& zone) {
Matthew Barth0206c722021-03-30 15:20:05 -0500108 return Manager::inConfig(fanProfile, zone.first);
109 });
Matthew Barthe91ac862021-05-25 16:22:17 -0500110 if (itZone != zones.end())
Matthew Barthde90fb42021-03-04 16:34:28 -0600111 {
Matthew Barth6f787302021-03-25 15:01:01 -0500112 if (itZone->second->getTarget() != fan.second->getTarget() &&
113 fan.second->getTarget() != 0)
114 {
Matthew Barthe91ac862021-05-25 16:22:17 -0500115 // Update zone target to current target of the fan in the
116 // zone
Matthew Barth6f787302021-03-25 15:01:01 -0500117 itZone->second->setTarget(fan.second->getTarget());
118 }
Matthew Barthde90fb42021-03-04 16:34:28 -0600119 itZone->second->addFan(std::move(fan.second));
120 }
121 }
Matthew Barthe91ac862021-05-25 16:22:17 -0500122
123 // Load any events configured
124 auto events = getConfig<Event>(true, this, zones);
125
Matthew Barth14303a42021-05-21 13:04:08 -0500126 // Enable zones
Matthew Barthe91ac862021-05-25 16:22:17 -0500127 _zones = std::move(zones);
Matthew Barth14303a42021-05-21 13:04:08 -0500128 std::for_each(_zones.begin(), _zones.end(),
129 [](const auto& entry) { entry.second->enable(); });
Matthew Barthb584d812021-03-11 15:55:04 -0600130
Matthew Barthe91ac862021-05-25 16:22:17 -0500131 // Clear current timers and signal subscriptions before enabling events
132 // To save reloading services and/or objects into cache, do not clear cache
133 _timers.clear();
134 _signals.clear();
135
136 // Enable events
137 _events = std::move(events);
Matthew Barth54b5a242021-05-21 11:02:52 -0500138 std::for_each(_events.begin(), _events.end(),
139 [](const auto& entry) { entry.second->enable(); });
Matthew Barthacd737c2021-03-04 11:04:01 -0600140}
141
Matthew Barth48f44da2021-05-27 15:43:34 -0500142void Manager::powerStateChanged(bool powerStateOn)
143{
144 if (powerStateOn)
145 {
146 std::for_each(_zones.begin(), _zones.end(), [](const auto& entry) {
147 entry.second->setTarget(entry.second->getPoweronTarget());
148 });
149 }
150}
151
Matthew Barthacd737c2021-03-04 11:04:01 -0600152const std::vector<std::string>& Manager::getActiveProfiles()
153{
154 return _activeProfiles;
Matthew Bartha227a162020-08-05 10:51:45 -0500155}
156
Matthew Barth0206c722021-03-30 15:20:05 -0500157bool Manager::inConfig(const configKey& input, const configKey& comp)
158{
159 // Config names dont match, do not include in config
160 if (input.first != comp.first)
161 {
162 return false;
163 }
164 // No profiles specified by input config, can be used in any config
165 if (input.second.empty())
166 {
167 return true;
168 }
169 else
170 {
171 // Profiles must have one match in the other's profiles(and they must be
172 // an active profile) to be used in the config
173 return std::any_of(
174 input.second.begin(), input.second.end(),
175 [&comp](const auto& lProfile) {
176 return std::any_of(
177 comp.second.begin(), comp.second.end(),
178 [&lProfile](const auto& rProfile) {
179 if (lProfile != rProfile)
180 {
181 return false;
182 }
183 auto activeProfs = getActiveProfiles();
184 return std::find(activeProfs.begin(), activeProfs.end(),
185 lProfile) != activeProfs.end();
186 });
187 });
188 }
189}
190
Matthew Barth12cb1252021-03-08 16:47:30 -0600191bool Manager::hasOwner(const std::string& path, const std::string& intf)
192{
193 auto itServ = _servTree.find(path);
194 if (itServ == _servTree.end())
195 {
196 // Path not found in cache, therefore owner missing
197 return false;
198 }
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500199 for (const auto& service : itServ->second)
Matthew Barth12cb1252021-03-08 16:47:30 -0600200 {
201 auto itIntf = std::find_if(
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500202 service.second.second.begin(), service.second.second.end(),
Matthew Barth12cb1252021-03-08 16:47:30 -0600203 [&intf](const auto& interface) { return intf == interface; });
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500204 if (itIntf != std::end(service.second.second))
Matthew Barth12cb1252021-03-08 16:47:30 -0600205 {
206 // Service found, return owner state
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500207 return service.second.first;
Matthew Barth12cb1252021-03-08 16:47:30 -0600208 }
209 }
210 // Interface not found in cache, therefore owner missing
211 return false;
212}
213
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500214void Manager::setOwner(const std::string& path, const std::string& serv,
215 const std::string& intf, bool isOwned)
216{
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500217 // Set owner state for specific object given
218 auto& ownIntf = _servTree[path][serv];
219 ownIntf.first = isOwned;
220 auto itIntf = std::find_if(
221 ownIntf.second.begin(), ownIntf.second.end(),
222 [&intf](const auto& interface) { return intf == interface; });
223 if (itIntf == std::end(ownIntf.second))
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500224 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500225 ownIntf.second.emplace_back(intf);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500226 }
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500227
228 // Update owner state on all entries of the same `serv` & `intf`
229 for (auto& itPath : _servTree)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500230 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500231 if (itPath.first == path)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500232 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500233 // Already set/updated owner on this path for `serv` & `intf`
234 continue;
235 }
236 for (auto& itServ : itPath.second)
237 {
238 if (itServ.first != serv)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500239 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500240 continue;
241 }
242 auto itIntf = std::find_if(
243 itServ.second.second.begin(), itServ.second.second.end(),
244 [&intf](const auto& interface) { return intf == interface; });
245 if (itIntf != std::end(itServ.second.second))
246 {
247 itServ.second.first = isOwned;
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500248 }
249 }
250 }
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500251}
252
253const std::string& Manager::findService(const std::string& path,
254 const std::string& intf)
255{
256 static const std::string empty = "";
257
258 auto itServ = _servTree.find(path);
259 if (itServ != _servTree.end())
260 {
261 for (const auto& service : itServ->second)
262 {
263 auto itIntf = std::find_if(
264 service.second.second.begin(), service.second.second.end(),
265 [&intf](const auto& interface) { return intf == interface; });
266 if (itIntf != std::end(service.second.second))
267 {
268 // Service found, return service name
269 return service.first;
270 }
271 }
272 }
273
274 return empty;
275}
276
Matthew Barth98f6fc12021-04-16 10:48:37 -0500277void Manager::addServices(const std::string& intf, int32_t depth)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500278{
279 // Get all subtree objects for the given interface
280 auto objects = util::SDBusPlus::getSubTree(util::SDBusPlus::getBus(), "/",
281 intf, depth);
282 // Add what's returned to the cache of path->services
283 for (auto& itPath : objects)
284 {
285 auto pathIter = _servTree.find(itPath.first);
286 if (pathIter != _servTree.end())
287 {
288 // Path found in cache
289 for (auto& itServ : itPath.second)
290 {
291 auto servIter = pathIter->second.find(itServ.first);
292 if (servIter != pathIter->second.end())
293 {
294 // Service found in cache
295 for (auto& itIntf : itServ.second)
296 {
297 if (std::find(servIter->second.second.begin(),
298 servIter->second.second.end(),
299 itIntf) == servIter->second.second.end())
300 {
301 // Add interface to cache
302 servIter->second.second.emplace_back(itIntf);
303 }
304 }
305 }
306 else
307 {
308 // Service not found in cache
309 auto intfs = {intf};
310 pathIter->second[itServ.first] =
311 std::make_pair(true, intfs);
312 }
313 }
314 }
315 else
316 {
317 // Path not found in cache
318 auto intfs = {intf};
319 _servTree[itPath.first] = {
320 {itPath.second.begin()->first, std::make_pair(true, intfs)}};
321 }
322 }
323}
324
325const std::string& Manager::getService(const std::string& path,
326 const std::string& intf)
327{
328 // Retrieve service from cache
329 const auto& serviceName = findService(path, intf);
330 if (serviceName.empty())
331 {
Matthew Barth98f6fc12021-04-16 10:48:37 -0500332 addServices(intf, 0);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500333 return findService(path, intf);
334 }
335
336 return serviceName;
337}
338
Matthew Barthf41e9472021-05-13 16:38:06 -0500339std::vector<std::string> Manager::findPaths(const std::string& serv,
340 const std::string& intf)
341{
342 std::vector<std::string> paths;
343
344 for (const auto& path : _servTree)
345 {
346 auto itServ = path.second.find(serv);
347 if (itServ != path.second.end())
348 {
349 if (std::find(itServ->second.second.begin(),
350 itServ->second.second.end(),
351 intf) != itServ->second.second.end())
352 {
353 if (std::find(paths.begin(), paths.end(), path.first) ==
354 paths.end())
355 {
356 paths.push_back(path.first);
357 }
358 }
359 }
360 }
361
362 return paths;
363}
364
365std::vector<std::string> Manager::getPaths(const std::string& serv,
366 const std::string& intf)
367{
368 auto paths = findPaths(serv, intf);
369 if (paths.empty())
370 {
371 addServices(intf, 0);
372 return findPaths(serv, intf);
373 }
374
375 return paths;
376}
377
378void Manager::addObjects(const std::string& path, const std::string& intf,
379 const std::string& prop)
380{
381 auto service = getService(path, intf);
382 if (service.empty())
383 {
384 // Log service not found for object
385 log<level::ERR>(
386 fmt::format("Unable to get service name for path {}, interface {}",
387 path, intf)
388 .c_str());
389 return;
390 }
391
392 auto objMgrPaths = getPaths(service, "org.freedesktop.DBus.ObjectManager");
393 if (objMgrPaths.empty())
394 {
395 // No object manager interface provided by service?
396 // Attempt to retrieve property directly
397 auto variant = util::SDBusPlus::getPropertyVariant<PropertyVariantType>(
398 _bus, service, path, intf, prop);
399 _objects[path][intf][prop] = variant;
400 return;
401 }
402
403 for (const auto& objMgrPath : objMgrPaths)
404 {
405 // Get all managed objects of service
406 auto objects = util::SDBusPlus::getManagedObjects<PropertyVariantType>(
407 _bus, service, objMgrPath);
408
409 // Add what's returned to the cache of objects
410 for (auto& object : objects)
411 {
412 auto itPath = _objects.find(object.first);
413 if (itPath != _objects.end())
414 {
415 // Path found in cache
416 for (auto& interface : itPath->second)
417 {
418 auto itIntf = itPath->second.find(interface.first);
419 if (itIntf != itPath->second.end())
420 {
421 // Interface found in cache
422 for (auto& property : itIntf->second)
423 {
424 auto itProp = itIntf->second.find(property.first);
425 if (itProp != itIntf->second.end())
426 {
427 // Property found, update value
428 itProp->second = property.second;
429 }
430 else
431 {
432 itIntf->second.insert(property);
433 }
434 }
435 }
436 else
437 {
438 // Interface not found in cache
439 itPath->second.insert(interface);
440 }
441 }
442 }
443 else
444 {
445 // Path not found in cache
446 _objects.insert(object);
447 }
448 }
449 }
450}
451
452const std::optional<PropertyVariantType>
453 Manager::getProperty(const std::string& path, const std::string& intf,
454 const std::string& prop)
455{
456 // TODO Objects hosted by fan control (i.e. ThermalMode) are required to
457 // update the cache upon being set/updated
458 auto itPath = _objects.find(path);
459 if (itPath != _objects.end())
460 {
461 auto itIntf = itPath->second.find(intf);
462 if (itIntf != itPath->second.end())
463 {
464 auto itProp = itIntf->second.find(prop);
465 if (itProp != itIntf->second.end())
466 {
467 return itProp->second;
468 }
469 }
470 }
471
472 return std::nullopt;
473}
474
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500475void Manager::addTimer(const TimerType type,
476 const std::chrono::microseconds interval,
477 std::unique_ptr<TimerPkg> pkg)
478{
479 auto dataPtr =
480 std::make_unique<TimerData>(std::make_pair(type, std::move(*pkg)));
481 Timer timer(_event,
482 std::bind(&Manager::timerExpired, this, std::ref(*dataPtr)));
483 if (type == TimerType::repeating)
484 {
485 timer.restart(interval);
486 }
487 else if (type == TimerType::oneshot)
488 {
489 timer.restartOnce(interval);
490 }
491 else
492 {
493 throw std::invalid_argument("Invalid Timer Type");
494 }
495 _timers.emplace_back(std::move(dataPtr), std::move(timer));
496}
497
498void Manager::timerExpired(TimerData& data)
499{
500 auto& actions =
501 std::get<std::vector<std::unique_ptr<ActionBase>>&>(data.second);
502 // Perform the actions in the timer data
503 std::for_each(actions.begin(), actions.end(),
Matthew Barth00f6aa02021-04-09 10:49:47 -0500504 [](auto& action) { action->run(); });
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500505
506 // Remove oneshot timers after they expired
507 if (data.first == TimerType::oneshot)
508 {
509 auto itTimer = std::find_if(
510 _timers.begin(), _timers.end(), [&data](const auto& timer) {
511 return (data.first == timer.first->first &&
512 (std::get<std::string>(data.second) ==
513 std::get<std::string>(timer.first->second)));
514 });
515 if (itTimer != std::end(_timers))
516 {
517 _timers.erase(itTimer);
518 }
519 }
520}
521
Matthew Barthebabc042021-05-13 15:38:58 -0500522void Manager::handleSignal(sdbusplus::message::message& msg,
523 const std::vector<SignalPkg>* pkgs)
524{
525 for (auto& pkg : *pkgs)
526 {
527 // Handle the signal callback and only run the actions if the handler
528 // updated the cache for the given SignalObject
529 if (std::get<SignalHandler>(pkg)(msg, std::get<SignalObject>(pkg),
530 *this))
531 {
532 // Perform the actions in the handler package
533 auto& actions = std::get<SignalActions>(pkg);
534 std::for_each(actions.begin(), actions.end(),
535 [](auto& action) { action.get()->run(); });
536 }
537 }
538}
539
Matthew Barthacd737c2021-03-04 11:04:01 -0600540void Manager::setProfiles()
541{
542 // Profiles JSON config file is optional
543 auto confFile = fan::JsonConfig::getConfFile(_bus, confAppName,
544 Profile::confFileName, true);
Matthew Barthe91ac862021-05-25 16:22:17 -0500545
546 _profiles.clear();
Matthew Barthacd737c2021-03-04 11:04:01 -0600547 if (!confFile.empty())
548 {
549 for (const auto& entry : fan::JsonConfig::load(confFile))
550 {
551 auto obj = std::make_unique<Profile>(entry);
552 _profiles.emplace(
553 std::make_pair(obj->getName(), obj->getProfiles()),
554 std::move(obj));
555 }
556 }
Matthew Barthe91ac862021-05-25 16:22:17 -0500557
Matthew Barthacd737c2021-03-04 11:04:01 -0600558 // Ensure all configurations use the same set of active profiles
559 // (In case a profile's active state changes during configuration)
Matthew Barthe91ac862021-05-25 16:22:17 -0500560 _activeProfiles.clear();
Matthew Barthacd737c2021-03-04 11:04:01 -0600561 for (const auto& profile : _profiles)
562 {
563 if (profile.second->isActive())
564 {
565 _activeProfiles.emplace_back(profile.first.first);
566 }
567 }
568}
569
Matthew Bartha227a162020-08-05 10:51:45 -0500570} // namespace phosphor::fan::control::json