blob: 3457d3c29e312c5a9a8c9692d8f36fb5194b860a [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 Barthacd737c2021-03-04 11:04:01 -060026#include "zone.hpp"
Matthew Bartha227a162020-08-05 10:51:45 -050027
Matthew Barthacd737c2021-03-04 11:04:01 -060028#include <nlohmann/json.hpp>
Matthew Bartha227a162020-08-05 10:51:45 -050029#include <sdbusplus/bus.hpp>
Matthew Barthacd737c2021-03-04 11:04:01 -060030#include <sdeventplus/event.hpp>
Matthew Barthd9cb63b2021-03-24 14:36:49 -050031#include <sdeventplus/utility/timer.hpp>
Matthew Bartha227a162020-08-05 10:51:45 -050032
Matthew Barthde90fb42021-03-04 16:34:28 -060033#include <algorithm>
Matthew Barthd9cb63b2021-03-24 14:36:49 -050034#include <chrono>
Matthew Bartha227a162020-08-05 10:51:45 -050035#include <filesystem>
Matthew Barthd9cb63b2021-03-24 14:36:49 -050036#include <functional>
37#include <map>
38#include <memory>
39#include <tuple>
40#include <utility>
Matthew Barth06764942021-03-04 09:28:40 -060041#include <vector>
Matthew Bartha227a162020-08-05 10:51:45 -050042
43namespace phosphor::fan::control::json
44{
45
Matthew Barthacd737c2021-03-04 11:04:01 -060046using json = nlohmann::json;
47
48std::vector<std::string> Manager::_activeProfiles;
Matthew Barth12cb1252021-03-08 16:47:30 -060049std::map<std::string,
Matthew Barth4ca87fa2021-04-14 11:31:13 -050050 std::map<std::string, std::pair<bool, std::vector<std::string>>>>
Matthew Barth12cb1252021-03-08 16:47:30 -060051 Manager::_servTree;
Matthew Barth07fecfc2021-01-29 09:04:43 -060052std::map<std::string,
53 std::map<std::string, std::map<std::string, PropertyVariantType>>>
54 Manager::_objects;
Matthew Barthacd737c2021-03-04 11:04:01 -060055
Matthew Barth06764942021-03-04 09:28:40 -060056Manager::Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event) :
Matthew Barthacd737c2021-03-04 11:04:01 -060057 _bus(bus), _event(event)
Matthew Bartha227a162020-08-05 10:51:45 -050058{
59 // Manager JSON config file is optional
60 auto confFile =
61 fan::JsonConfig::getConfFile(bus, confAppName, confFileName, true);
62 if (!confFile.empty())
63 {
64 _jsonObj = fan::JsonConfig::load(confFile);
65 }
Matthew Barth06764942021-03-04 09:28:40 -060066
Matthew Barthacd737c2021-03-04 11:04:01 -060067 // Parse and set the available profiles and which are active
68 setProfiles();
69
70 // Load the zone configurations
Matthew Barth603ef162021-03-24 15:34:53 -050071 _zones = getConfig<Zone>(false, bus, bus, event, this);
Matthew Barthde90fb42021-03-04 16:34:28 -060072
73 // Load the fan configurations and move each fan into its zone
Matthew Barth603ef162021-03-24 15:34:53 -050074 auto fans = getConfig<Fan>(false, bus, bus);
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 Barthb584d812021-03-11 15:55:04 -060094
Matthew Barth44ab7692021-03-26 11:40:10 -050095 // Load the configured groups that are copied into events where they're used
96 auto groups = getConfig<Group>(true, bus);
97
98 // Load any events configured
Matthew Barthd4bd0ae2021-04-14 12:42:03 -050099 _events = getConfig<Event>(true, bus, bus, this, groups, _zones);
Matthew Barth44ab7692021-03-26 11:40:10 -0500100
Matthew Barthb584d812021-03-11 15:55:04 -0600101 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{
169 auto itServ = _servTree.find(path);
170 if (itServ == _servTree.end())
171 {
172 auto intfs = {intf};
173 _servTree[path] = {{serv, std::make_pair(isOwned, intfs)}};
174 return;
175 }
176 for (auto& service : itServ->second)
177 {
178 auto itIntf = std::find_if(
179 service.second.second.begin(), service.second.second.end(),
180 [&intf](const auto& interface) { return intf == interface; });
181 if (itIntf != std::end(service.second.second))
182 {
183 if (service.first == serv)
184 {
185 service.second.first = isOwned;
186 return;
187 }
188 }
189 }
190 auto intfs = {intf};
191 itServ->second[serv] = std::make_pair(isOwned, intfs);
192}
193
194const std::string& Manager::findService(const std::string& path,
195 const std::string& intf)
196{
197 static const std::string empty = "";
198
199 auto itServ = _servTree.find(path);
200 if (itServ != _servTree.end())
201 {
202 for (const auto& service : itServ->second)
203 {
204 auto itIntf = std::find_if(
205 service.second.second.begin(), service.second.second.end(),
206 [&intf](const auto& interface) { return intf == interface; });
207 if (itIntf != std::end(service.second.second))
208 {
209 // Service found, return service name
210 return service.first;
211 }
212 }
213 }
214
215 return empty;
216}
217
Matthew Barth98f6fc12021-04-16 10:48:37 -0500218void Manager::addServices(const std::string& intf, int32_t depth)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500219{
220 // Get all subtree objects for the given interface
221 auto objects = util::SDBusPlus::getSubTree(util::SDBusPlus::getBus(), "/",
222 intf, depth);
223 // Add what's returned to the cache of path->services
224 for (auto& itPath : objects)
225 {
226 auto pathIter = _servTree.find(itPath.first);
227 if (pathIter != _servTree.end())
228 {
229 // Path found in cache
230 for (auto& itServ : itPath.second)
231 {
232 auto servIter = pathIter->second.find(itServ.first);
233 if (servIter != pathIter->second.end())
234 {
235 // Service found in cache
236 for (auto& itIntf : itServ.second)
237 {
238 if (std::find(servIter->second.second.begin(),
239 servIter->second.second.end(),
240 itIntf) == servIter->second.second.end())
241 {
242 // Add interface to cache
243 servIter->second.second.emplace_back(itIntf);
244 }
245 }
246 }
247 else
248 {
249 // Service not found in cache
250 auto intfs = {intf};
251 pathIter->second[itServ.first] =
252 std::make_pair(true, intfs);
253 }
254 }
255 }
256 else
257 {
258 // Path not found in cache
259 auto intfs = {intf};
260 _servTree[itPath.first] = {
261 {itPath.second.begin()->first, std::make_pair(true, intfs)}};
262 }
263 }
264}
265
266const std::string& Manager::getService(const std::string& path,
267 const std::string& intf)
268{
269 // Retrieve service from cache
270 const auto& serviceName = findService(path, intf);
271 if (serviceName.empty())
272 {
Matthew Barth98f6fc12021-04-16 10:48:37 -0500273 addServices(intf, 0);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500274 return findService(path, intf);
275 }
276
277 return serviceName;
278}
279
Matthew Barthf41e9472021-05-13 16:38:06 -0500280std::vector<std::string> Manager::findPaths(const std::string& serv,
281 const std::string& intf)
282{
283 std::vector<std::string> paths;
284
285 for (const auto& path : _servTree)
286 {
287 auto itServ = path.second.find(serv);
288 if (itServ != path.second.end())
289 {
290 if (std::find(itServ->second.second.begin(),
291 itServ->second.second.end(),
292 intf) != itServ->second.second.end())
293 {
294 if (std::find(paths.begin(), paths.end(), path.first) ==
295 paths.end())
296 {
297 paths.push_back(path.first);
298 }
299 }
300 }
301 }
302
303 return paths;
304}
305
306std::vector<std::string> Manager::getPaths(const std::string& serv,
307 const std::string& intf)
308{
309 auto paths = findPaths(serv, intf);
310 if (paths.empty())
311 {
312 addServices(intf, 0);
313 return findPaths(serv, intf);
314 }
315
316 return paths;
317}
318
319void Manager::addObjects(const std::string& path, const std::string& intf,
320 const std::string& prop)
321{
322 auto service = getService(path, intf);
323 if (service.empty())
324 {
325 // Log service not found for object
326 log<level::ERR>(
327 fmt::format("Unable to get service name for path {}, interface {}",
328 path, intf)
329 .c_str());
330 return;
331 }
332
333 auto objMgrPaths = getPaths(service, "org.freedesktop.DBus.ObjectManager");
334 if (objMgrPaths.empty())
335 {
336 // No object manager interface provided by service?
337 // Attempt to retrieve property directly
338 auto variant = util::SDBusPlus::getPropertyVariant<PropertyVariantType>(
339 _bus, service, path, intf, prop);
340 _objects[path][intf][prop] = variant;
341 return;
342 }
343
344 for (const auto& objMgrPath : objMgrPaths)
345 {
346 // Get all managed objects of service
347 auto objects = util::SDBusPlus::getManagedObjects<PropertyVariantType>(
348 _bus, service, objMgrPath);
349
350 // Add what's returned to the cache of objects
351 for (auto& object : objects)
352 {
353 auto itPath = _objects.find(object.first);
354 if (itPath != _objects.end())
355 {
356 // Path found in cache
357 for (auto& interface : itPath->second)
358 {
359 auto itIntf = itPath->second.find(interface.first);
360 if (itIntf != itPath->second.end())
361 {
362 // Interface found in cache
363 for (auto& property : itIntf->second)
364 {
365 auto itProp = itIntf->second.find(property.first);
366 if (itProp != itIntf->second.end())
367 {
368 // Property found, update value
369 itProp->second = property.second;
370 }
371 else
372 {
373 itIntf->second.insert(property);
374 }
375 }
376 }
377 else
378 {
379 // Interface not found in cache
380 itPath->second.insert(interface);
381 }
382 }
383 }
384 else
385 {
386 // Path not found in cache
387 _objects.insert(object);
388 }
389 }
390 }
391}
392
393const std::optional<PropertyVariantType>
394 Manager::getProperty(const std::string& path, const std::string& intf,
395 const std::string& prop)
396{
397 // TODO Objects hosted by fan control (i.e. ThermalMode) are required to
398 // update the cache upon being set/updated
399 auto itPath = _objects.find(path);
400 if (itPath != _objects.end())
401 {
402 auto itIntf = itPath->second.find(intf);
403 if (itIntf != itPath->second.end())
404 {
405 auto itProp = itIntf->second.find(prop);
406 if (itProp != itIntf->second.end())
407 {
408 return itProp->second;
409 }
410 }
411 }
412
413 return std::nullopt;
414}
415
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500416void Manager::addTimer(const TimerType type,
417 const std::chrono::microseconds interval,
418 std::unique_ptr<TimerPkg> pkg)
419{
420 auto dataPtr =
421 std::make_unique<TimerData>(std::make_pair(type, std::move(*pkg)));
422 Timer timer(_event,
423 std::bind(&Manager::timerExpired, this, std::ref(*dataPtr)));
424 if (type == TimerType::repeating)
425 {
426 timer.restart(interval);
427 }
428 else if (type == TimerType::oneshot)
429 {
430 timer.restartOnce(interval);
431 }
432 else
433 {
434 throw std::invalid_argument("Invalid Timer Type");
435 }
436 _timers.emplace_back(std::move(dataPtr), std::move(timer));
437}
438
439void Manager::timerExpired(TimerData& data)
440{
441 auto& actions =
442 std::get<std::vector<std::unique_ptr<ActionBase>>&>(data.second);
443 // Perform the actions in the timer data
444 std::for_each(actions.begin(), actions.end(),
Matthew Barth00f6aa02021-04-09 10:49:47 -0500445 [](auto& action) { action->run(); });
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500446
447 // Remove oneshot timers after they expired
448 if (data.first == TimerType::oneshot)
449 {
450 auto itTimer = std::find_if(
451 _timers.begin(), _timers.end(), [&data](const auto& timer) {
452 return (data.first == timer.first->first &&
453 (std::get<std::string>(data.second) ==
454 std::get<std::string>(timer.first->second)));
455 });
456 if (itTimer != std::end(_timers))
457 {
458 _timers.erase(itTimer);
459 }
460 }
461}
462
Matthew Barthebabc042021-05-13 15:38:58 -0500463void Manager::handleSignal(sdbusplus::message::message& msg,
464 const std::vector<SignalPkg>* pkgs)
465{
466 for (auto& pkg : *pkgs)
467 {
468 // Handle the signal callback and only run the actions if the handler
469 // updated the cache for the given SignalObject
470 if (std::get<SignalHandler>(pkg)(msg, std::get<SignalObject>(pkg),
471 *this))
472 {
473 // Perform the actions in the handler package
474 auto& actions = std::get<SignalActions>(pkg);
475 std::for_each(actions.begin(), actions.end(),
476 [](auto& action) { action.get()->run(); });
477 }
478 }
479}
480
Matthew Bartha227a162020-08-05 10:51:45 -0500481unsigned int Manager::getPowerOnDelay()
482{
483 auto powerOnDelay = 0;
484
485 // Parse optional "power_on_delay" from JSON object
486 if (!_jsonObj.empty() && _jsonObj.contains("power_on_delay"))
487 {
488 powerOnDelay = _jsonObj["power_on_delay"].get<unsigned int>();
489 }
490
491 return powerOnDelay;
492}
493
Matthew Barthacd737c2021-03-04 11:04:01 -0600494void Manager::setProfiles()
495{
496 // Profiles JSON config file is optional
497 auto confFile = fan::JsonConfig::getConfFile(_bus, confAppName,
498 Profile::confFileName, true);
499 if (!confFile.empty())
500 {
501 for (const auto& entry : fan::JsonConfig::load(confFile))
502 {
503 auto obj = std::make_unique<Profile>(entry);
504 _profiles.emplace(
505 std::make_pair(obj->getName(), obj->getProfiles()),
506 std::move(obj));
507 }
508 }
509 // Ensure all configurations use the same set of active profiles
510 // (In case a profile's active state changes during configuration)
511 for (const auto& profile : _profiles)
512 {
513 if (profile.second->isActive())
514 {
515 _activeProfiles.emplace_back(profile.first.first);
516 }
517 }
518}
519
Matthew Bartha227a162020-08-05 10:51:45 -0500520} // namespace phosphor::fan::control::json