blob: a519836cf93ab85b80c1f9f19b64554c4ad5b398 [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{
60 // Manager JSON config file is optional
61 auto confFile =
Matthew Barth9403a212021-05-17 09:31:50 -050062 fan::JsonConfig::getConfFile(_bus, confAppName, confFileName, true);
Matthew Bartha227a162020-08-05 10:51:45 -050063 if (!confFile.empty())
64 {
65 _jsonObj = fan::JsonConfig::load(confFile);
66 }
Matthew Barth06764942021-03-04 09:28:40 -060067
Matthew Barthacd737c2021-03-04 11:04:01 -060068 // Parse and set the available profiles and which are active
69 setProfiles();
70
71 // Load the zone configurations
Matthew Barth9403a212021-05-17 09:31:50 -050072 _zones = getConfig<Zone>(false, event, this);
Matthew Barthde90fb42021-03-04 16:34:28 -060073 // Load the fan configurations and move each fan into its zone
Matthew Barth9403a212021-05-17 09:31:50 -050074 auto fans = getConfig<Fan>(false);
Matthew Barthde90fb42021-03-04 16:34:28 -060075 for (auto& fan : fans)
76 {
Matthew Barth0206c722021-03-30 15:20:05 -050077 configKey fanProfile =
78 std::make_pair(fan.second->getZone(), fan.first.second);
79 auto itZone = std::find_if(
80 _zones.begin(), _zones.end(), [&fanProfile](const auto& zone) {
81 return Manager::inConfig(fanProfile, zone.first);
82 });
Matthew Barthde90fb42021-03-04 16:34:28 -060083 if (itZone != _zones.end())
84 {
Matthew Barth6f787302021-03-25 15:01:01 -050085 if (itZone->second->getTarget() != fan.second->getTarget() &&
86 fan.second->getTarget() != 0)
87 {
88 // Update zone target to current target of the fan in the zone
89 itZone->second->setTarget(fan.second->getTarget());
90 }
Matthew Barthde90fb42021-03-04 16:34:28 -060091 itZone->second->addFan(std::move(fan.second));
92 }
93 }
Matthew Barth14303a42021-05-21 13:04:08 -050094 // Enable zones
95 std::for_each(_zones.begin(), _zones.end(),
96 [](const auto& entry) { entry.second->enable(); });
Matthew Barthb584d812021-03-11 15:55:04 -060097
Matthew Barth54b5a242021-05-21 11:02:52 -050098 // Load any events configured and enable
Matthew Barthc8bde4a2021-05-19 15:34:49 -050099 _events = getConfig<Event>(true, this, _zones);
Matthew Barth54b5a242021-05-21 11:02:52 -0500100 std::for_each(_events.begin(), _events.end(),
101 [](const auto& entry) { entry.second->enable(); });
Matthew Barth44ab7692021-03-26 11:40:10 -0500102
Matthew Barth9403a212021-05-17 09:31:50 -0500103 _bus.request_name(CONTROL_BUSNAME);
Matthew Barthacd737c2021-03-04 11:04:01 -0600104}
105
106const std::vector<std::string>& Manager::getActiveProfiles()
107{
108 return _activeProfiles;
Matthew Bartha227a162020-08-05 10:51:45 -0500109}
110
Matthew Barth0206c722021-03-30 15:20:05 -0500111bool Manager::inConfig(const configKey& input, const configKey& comp)
112{
113 // Config names dont match, do not include in config
114 if (input.first != comp.first)
115 {
116 return false;
117 }
118 // No profiles specified by input config, can be used in any config
119 if (input.second.empty())
120 {
121 return true;
122 }
123 else
124 {
125 // Profiles must have one match in the other's profiles(and they must be
126 // an active profile) to be used in the config
127 return std::any_of(
128 input.second.begin(), input.second.end(),
129 [&comp](const auto& lProfile) {
130 return std::any_of(
131 comp.second.begin(), comp.second.end(),
132 [&lProfile](const auto& rProfile) {
133 if (lProfile != rProfile)
134 {
135 return false;
136 }
137 auto activeProfs = getActiveProfiles();
138 return std::find(activeProfs.begin(), activeProfs.end(),
139 lProfile) != activeProfs.end();
140 });
141 });
142 }
143}
144
Matthew Barth12cb1252021-03-08 16:47:30 -0600145bool Manager::hasOwner(const std::string& path, const std::string& intf)
146{
147 auto itServ = _servTree.find(path);
148 if (itServ == _servTree.end())
149 {
150 // Path not found in cache, therefore owner missing
151 return false;
152 }
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500153 for (const auto& service : itServ->second)
Matthew Barth12cb1252021-03-08 16:47:30 -0600154 {
155 auto itIntf = std::find_if(
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500156 service.second.second.begin(), service.second.second.end(),
Matthew Barth12cb1252021-03-08 16:47:30 -0600157 [&intf](const auto& interface) { return intf == interface; });
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500158 if (itIntf != std::end(service.second.second))
Matthew Barth12cb1252021-03-08 16:47:30 -0600159 {
160 // Service found, return owner state
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500161 return service.second.first;
Matthew Barth12cb1252021-03-08 16:47:30 -0600162 }
163 }
164 // Interface not found in cache, therefore owner missing
165 return false;
166}
167
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500168void Manager::setOwner(const std::string& path, const std::string& serv,
169 const std::string& intf, bool isOwned)
170{
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500171 // Set owner state for specific object given
172 auto& ownIntf = _servTree[path][serv];
173 ownIntf.first = isOwned;
174 auto itIntf = std::find_if(
175 ownIntf.second.begin(), ownIntf.second.end(),
176 [&intf](const auto& interface) { return intf == interface; });
177 if (itIntf == std::end(ownIntf.second))
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500178 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500179 ownIntf.second.emplace_back(intf);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500180 }
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500181
182 // Update owner state on all entries of the same `serv` & `intf`
183 for (auto& itPath : _servTree)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500184 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500185 if (itPath.first == path)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500186 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500187 // Already set/updated owner on this path for `serv` & `intf`
188 continue;
189 }
190 for (auto& itServ : itPath.second)
191 {
192 if (itServ.first != serv)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500193 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500194 continue;
195 }
196 auto itIntf = std::find_if(
197 itServ.second.second.begin(), itServ.second.second.end(),
198 [&intf](const auto& interface) { return intf == interface; });
199 if (itIntf != std::end(itServ.second.second))
200 {
201 itServ.second.first = isOwned;
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500202 }
203 }
204 }
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500205}
206
207const std::string& Manager::findService(const std::string& path,
208 const std::string& intf)
209{
210 static const std::string empty = "";
211
212 auto itServ = _servTree.find(path);
213 if (itServ != _servTree.end())
214 {
215 for (const auto& service : itServ->second)
216 {
217 auto itIntf = std::find_if(
218 service.second.second.begin(), service.second.second.end(),
219 [&intf](const auto& interface) { return intf == interface; });
220 if (itIntf != std::end(service.second.second))
221 {
222 // Service found, return service name
223 return service.first;
224 }
225 }
226 }
227
228 return empty;
229}
230
Matthew Barth98f6fc12021-04-16 10:48:37 -0500231void Manager::addServices(const std::string& intf, int32_t depth)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500232{
233 // Get all subtree objects for the given interface
234 auto objects = util::SDBusPlus::getSubTree(util::SDBusPlus::getBus(), "/",
235 intf, depth);
236 // Add what's returned to the cache of path->services
237 for (auto& itPath : objects)
238 {
239 auto pathIter = _servTree.find(itPath.first);
240 if (pathIter != _servTree.end())
241 {
242 // Path found in cache
243 for (auto& itServ : itPath.second)
244 {
245 auto servIter = pathIter->second.find(itServ.first);
246 if (servIter != pathIter->second.end())
247 {
248 // Service found in cache
249 for (auto& itIntf : itServ.second)
250 {
251 if (std::find(servIter->second.second.begin(),
252 servIter->second.second.end(),
253 itIntf) == servIter->second.second.end())
254 {
255 // Add interface to cache
256 servIter->second.second.emplace_back(itIntf);
257 }
258 }
259 }
260 else
261 {
262 // Service not found in cache
263 auto intfs = {intf};
264 pathIter->second[itServ.first] =
265 std::make_pair(true, intfs);
266 }
267 }
268 }
269 else
270 {
271 // Path not found in cache
272 auto intfs = {intf};
273 _servTree[itPath.first] = {
274 {itPath.second.begin()->first, std::make_pair(true, intfs)}};
275 }
276 }
277}
278
279const std::string& Manager::getService(const std::string& path,
280 const std::string& intf)
281{
282 // Retrieve service from cache
283 const auto& serviceName = findService(path, intf);
284 if (serviceName.empty())
285 {
Matthew Barth98f6fc12021-04-16 10:48:37 -0500286 addServices(intf, 0);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500287 return findService(path, intf);
288 }
289
290 return serviceName;
291}
292
Matthew Barthf41e9472021-05-13 16:38:06 -0500293std::vector<std::string> Manager::findPaths(const std::string& serv,
294 const std::string& intf)
295{
296 std::vector<std::string> paths;
297
298 for (const auto& path : _servTree)
299 {
300 auto itServ = path.second.find(serv);
301 if (itServ != path.second.end())
302 {
303 if (std::find(itServ->second.second.begin(),
304 itServ->second.second.end(),
305 intf) != itServ->second.second.end())
306 {
307 if (std::find(paths.begin(), paths.end(), path.first) ==
308 paths.end())
309 {
310 paths.push_back(path.first);
311 }
312 }
313 }
314 }
315
316 return paths;
317}
318
319std::vector<std::string> Manager::getPaths(const std::string& serv,
320 const std::string& intf)
321{
322 auto paths = findPaths(serv, intf);
323 if (paths.empty())
324 {
325 addServices(intf, 0);
326 return findPaths(serv, intf);
327 }
328
329 return paths;
330}
331
332void Manager::addObjects(const std::string& path, const std::string& intf,
333 const std::string& prop)
334{
335 auto service = getService(path, intf);
336 if (service.empty())
337 {
338 // Log service not found for object
339 log<level::ERR>(
340 fmt::format("Unable to get service name for path {}, interface {}",
341 path, intf)
342 .c_str());
343 return;
344 }
345
346 auto objMgrPaths = getPaths(service, "org.freedesktop.DBus.ObjectManager");
347 if (objMgrPaths.empty())
348 {
349 // No object manager interface provided by service?
350 // Attempt to retrieve property directly
351 auto variant = util::SDBusPlus::getPropertyVariant<PropertyVariantType>(
352 _bus, service, path, intf, prop);
353 _objects[path][intf][prop] = variant;
354 return;
355 }
356
357 for (const auto& objMgrPath : objMgrPaths)
358 {
359 // Get all managed objects of service
360 auto objects = util::SDBusPlus::getManagedObjects<PropertyVariantType>(
361 _bus, service, objMgrPath);
362
363 // Add what's returned to the cache of objects
364 for (auto& object : objects)
365 {
366 auto itPath = _objects.find(object.first);
367 if (itPath != _objects.end())
368 {
369 // Path found in cache
370 for (auto& interface : itPath->second)
371 {
372 auto itIntf = itPath->second.find(interface.first);
373 if (itIntf != itPath->second.end())
374 {
375 // Interface found in cache
376 for (auto& property : itIntf->second)
377 {
378 auto itProp = itIntf->second.find(property.first);
379 if (itProp != itIntf->second.end())
380 {
381 // Property found, update value
382 itProp->second = property.second;
383 }
384 else
385 {
386 itIntf->second.insert(property);
387 }
388 }
389 }
390 else
391 {
392 // Interface not found in cache
393 itPath->second.insert(interface);
394 }
395 }
396 }
397 else
398 {
399 // Path not found in cache
400 _objects.insert(object);
401 }
402 }
403 }
404}
405
406const std::optional<PropertyVariantType>
407 Manager::getProperty(const std::string& path, const std::string& intf,
408 const std::string& prop)
409{
410 // TODO Objects hosted by fan control (i.e. ThermalMode) are required to
411 // update the cache upon being set/updated
412 auto itPath = _objects.find(path);
413 if (itPath != _objects.end())
414 {
415 auto itIntf = itPath->second.find(intf);
416 if (itIntf != itPath->second.end())
417 {
418 auto itProp = itIntf->second.find(prop);
419 if (itProp != itIntf->second.end())
420 {
421 return itProp->second;
422 }
423 }
424 }
425
426 return std::nullopt;
427}
428
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500429void Manager::addTimer(const TimerType type,
430 const std::chrono::microseconds interval,
431 std::unique_ptr<TimerPkg> pkg)
432{
433 auto dataPtr =
434 std::make_unique<TimerData>(std::make_pair(type, std::move(*pkg)));
435 Timer timer(_event,
436 std::bind(&Manager::timerExpired, this, std::ref(*dataPtr)));
437 if (type == TimerType::repeating)
438 {
439 timer.restart(interval);
440 }
441 else if (type == TimerType::oneshot)
442 {
443 timer.restartOnce(interval);
444 }
445 else
446 {
447 throw std::invalid_argument("Invalid Timer Type");
448 }
449 _timers.emplace_back(std::move(dataPtr), std::move(timer));
450}
451
452void Manager::timerExpired(TimerData& data)
453{
454 auto& actions =
455 std::get<std::vector<std::unique_ptr<ActionBase>>&>(data.second);
456 // Perform the actions in the timer data
457 std::for_each(actions.begin(), actions.end(),
Matthew Barth00f6aa02021-04-09 10:49:47 -0500458 [](auto& action) { action->run(); });
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500459
460 // Remove oneshot timers after they expired
461 if (data.first == TimerType::oneshot)
462 {
463 auto itTimer = std::find_if(
464 _timers.begin(), _timers.end(), [&data](const auto& timer) {
465 return (data.first == timer.first->first &&
466 (std::get<std::string>(data.second) ==
467 std::get<std::string>(timer.first->second)));
468 });
469 if (itTimer != std::end(_timers))
470 {
471 _timers.erase(itTimer);
472 }
473 }
474}
475
Matthew Barthebabc042021-05-13 15:38:58 -0500476void Manager::handleSignal(sdbusplus::message::message& msg,
477 const std::vector<SignalPkg>* pkgs)
478{
479 for (auto& pkg : *pkgs)
480 {
481 // Handle the signal callback and only run the actions if the handler
482 // updated the cache for the given SignalObject
483 if (std::get<SignalHandler>(pkg)(msg, std::get<SignalObject>(pkg),
484 *this))
485 {
486 // Perform the actions in the handler package
487 auto& actions = std::get<SignalActions>(pkg);
488 std::for_each(actions.begin(), actions.end(),
489 [](auto& action) { action.get()->run(); });
490 }
491 }
492}
493
Matthew Bartha227a162020-08-05 10:51:45 -0500494unsigned int Manager::getPowerOnDelay()
495{
496 auto powerOnDelay = 0;
497
498 // Parse optional "power_on_delay" from JSON object
499 if (!_jsonObj.empty() && _jsonObj.contains("power_on_delay"))
500 {
501 powerOnDelay = _jsonObj["power_on_delay"].get<unsigned int>();
502 }
503
504 return powerOnDelay;
505}
506
Matthew Barthacd737c2021-03-04 11:04:01 -0600507void Manager::setProfiles()
508{
509 // Profiles JSON config file is optional
510 auto confFile = fan::JsonConfig::getConfFile(_bus, confAppName,
511 Profile::confFileName, true);
512 if (!confFile.empty())
513 {
514 for (const auto& entry : fan::JsonConfig::load(confFile))
515 {
516 auto obj = std::make_unique<Profile>(entry);
517 _profiles.emplace(
518 std::make_pair(obj->getName(), obj->getProfiles()),
519 std::move(obj));
520 }
521 }
522 // Ensure all configurations use the same set of active profiles
523 // (In case a profile's active state changes during configuration)
524 for (const auto& profile : _profiles)
525 {
526 if (profile.second->isActive())
527 {
528 _activeProfiles.emplace_back(profile.first.first);
529 }
530 }
531}
532
Matthew Bartha227a162020-08-05 10:51:45 -0500533} // namespace phosphor::fan::control::json