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