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