blob: a505fce9940c4f3787b683a878ba41899b0a72f4 [file] [log] [blame]
Matthew Bartha227a162020-08-05 10:51:45 -05001/**
Mike Cappsb2e9a4f2022-06-13 10:15:42 -04002 * Copyright © 2022 IBM Corporation
Matthew Bartha227a162020-08-05 10:51:45 -05003 *
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"
Mike Cappsbf8e56f2022-06-29 14:23:07 -040021#include "dbus_paths.hpp"
Matthew Barth44ab7692021-03-26 11:40:10 -050022#include "event.hpp"
Matthew Barthde90fb42021-03-04 16:34:28 -060023#include "fan.hpp"
Matthew Barthd9cb63b2021-03-24 14:36:49 -050024#include "group.hpp"
Matthew Bartha227a162020-08-05 10:51:45 -050025#include "json_config.hpp"
Matthew Barth48f44da2021-05-27 15:43:34 -050026#include "power_state.hpp"
Matthew Barth06764942021-03-04 09:28:40 -060027#include "profile.hpp"
Matthew Barth9403a212021-05-17 09:31:50 -050028#include "sdbusplus.hpp"
Matthew Barthbd52ed02022-02-07 15:15:10 -060029#include "utils/flight_recorder.hpp"
Matthew Barthacd737c2021-03-04 11:04:01 -060030#include "zone.hpp"
Matthew Bartha227a162020-08-05 10:51:45 -050031
Matthew Barthc024d782021-11-09 16:15:49 -060032#include <systemd/sd-bus.h>
33
Matthew Barthacd737c2021-03-04 11:04:01 -060034#include <nlohmann/json.hpp>
Matthew Bartha227a162020-08-05 10:51:45 -050035#include <sdbusplus/bus.hpp>
Matthew Barth1542fb52021-06-10 14:09:09 -050036#include <sdbusplus/server/manager.hpp>
Matthew Barthacd737c2021-03-04 11:04:01 -060037#include <sdeventplus/event.hpp>
Matthew Barthd9cb63b2021-03-24 14:36:49 -050038#include <sdeventplus/utility/timer.hpp>
Matthew Bartha227a162020-08-05 10:51:45 -050039
Matthew Barthde90fb42021-03-04 16:34:28 -060040#include <algorithm>
Matthew Barthd9cb63b2021-03-24 14:36:49 -050041#include <chrono>
Matthew Bartha227a162020-08-05 10:51:45 -050042#include <filesystem>
Matthew Barthd9cb63b2021-03-24 14:36:49 -050043#include <functional>
44#include <map>
45#include <memory>
46#include <tuple>
47#include <utility>
Matthew Barth06764942021-03-04 09:28:40 -060048#include <vector>
Matthew Bartha227a162020-08-05 10:51:45 -050049
50namespace phosphor::fan::control::json
51{
52
Matthew Barthacd737c2021-03-04 11:04:01 -060053using json = nlohmann::json;
54
55std::vector<std::string> Manager::_activeProfiles;
Matthew Barth12cb1252021-03-08 16:47:30 -060056std::map<std::string,
Matthew Barth4ca87fa2021-04-14 11:31:13 -050057 std::map<std::string, std::pair<bool, std::vector<std::string>>>>
Matthew Barth12cb1252021-03-08 16:47:30 -060058 Manager::_servTree;
Matthew Barth07fecfc2021-01-29 09:04:43 -060059std::map<std::string,
60 std::map<std::string, std::map<std::string, PropertyVariantType>>>
61 Manager::_objects;
Matt Spinlerd76351b2021-08-05 16:23:09 -050062std::unordered_map<std::string, PropertyVariantType> Manager::_parameters;
Matt Spinlerd0ba86a2021-11-09 10:09:13 -060063std::unordered_map<std::string, TriggerActions> Manager::_parameterTriggers;
Matthew Barthacd737c2021-03-04 11:04:01 -060064
Matt Spinler7787def2021-10-14 16:33:16 -050065const std::string Manager::dumpFile = "/tmp/fan_control_dump.json";
66
Matthew Barth9403a212021-05-17 09:31:50 -050067Manager::Manager(const sdeventplus::Event& event) :
Matthew Barth48f44da2021-05-27 15:43:34 -050068 _bus(util::SDBusPlus::getBus()), _event(event),
Matthew Barth3770a1d2021-06-10 15:09:37 -050069 _mgr(util::SDBusPlus::getBus(), CONTROL_OBJPATH), _loadAllowed(true),
Matthew Barth48f44da2021-05-27 15:43:34 -050070 _powerState(std::make_unique<PGoodState>(
71 util::SDBusPlus::getBus(),
72 std::bind(std::mem_fn(&Manager::powerStateChanged), this,
73 std::placeholders::_1)))
Matthew Barth3770a1d2021-06-10 15:09:37 -050074{}
Matthew Barthe91ac862021-05-25 16:22:17 -050075
76void Manager::sighupHandler(sdeventplus::source::Signal&,
77 const struct signalfd_siginfo*)
78{
Matthew Barthbd52ed02022-02-07 15:15:10 -060079 FlightRecorder::instance().log("main", "SIGHUP received");
Matthew Barthe91ac862021-05-25 16:22:17 -050080 // Save current set of available and active profiles
81 std::map<configKey, std::unique_ptr<Profile>> profiles;
82 profiles.swap(_profiles);
83 std::vector<std::string> activeProfiles;
84 activeProfiles.swap(_activeProfiles);
85
86 try
87 {
Matthew Barth3770a1d2021-06-10 15:09:37 -050088 _loadAllowed = true;
Matthew Barthe91ac862021-05-25 16:22:17 -050089 load();
90 }
Patrick Williamsddb773b2021-10-06 11:24:49 -050091 catch (const std::runtime_error& re)
Matthew Barthe91ac862021-05-25 16:22:17 -050092 {
93 // Restore saved available and active profiles
Matthew Barth3770a1d2021-06-10 15:09:37 -050094 _loadAllowed = false;
Matthew Barthe91ac862021-05-25 16:22:17 -050095 _profiles.swap(profiles);
96 _activeProfiles.swap(activeProfiles);
97 log<level::ERR>("Error reloading configs, no changes made",
98 entry("LOAD_ERROR=%s", re.what()));
Matthew Barthbd52ed02022-02-07 15:15:10 -060099 FlightRecorder::instance().log(
100 "main", fmt::format("Error reloading configs, no changes made: {}",
101 re.what()));
Matthew Barthe91ac862021-05-25 16:22:17 -0500102 }
103}
104
Matt Spinler2fc0a352021-10-04 15:10:57 -0500105void Manager::sigUsr1Handler(sdeventplus::source::Signal&,
106 const struct signalfd_siginfo*)
107{
Matt Spinler7787def2021-10-14 16:33:16 -0500108 debugDumpEventSource = std::make_unique<sdeventplus::source::Defer>(
109 _event, std::bind(std::mem_fn(&Manager::dumpDebugData), this,
Matt Spinler2fc0a352021-10-04 15:10:57 -0500110 std::placeholders::_1));
111}
112
Matt Spinler7787def2021-10-14 16:33:16 -0500113void Manager::dumpDebugData(sdeventplus::source::EventBase& /*source*/)
Matt Spinler2fc0a352021-10-04 15:10:57 -0500114{
Matt Spinler7787def2021-10-14 16:33:16 -0500115 json data;
116 FlightRecorder::instance().dump(data);
Matt Spinlerb5c21a22021-10-14 16:52:12 -0500117 dumpCache(data);
Matt Spinler7787def2021-10-14 16:33:16 -0500118
Matt Spinler9db6dd12021-10-29 16:10:08 -0500119 std::for_each(_zones.begin(), _zones.end(), [&data](const auto& zone) {
120 data["zones"][zone.second->getName()] = zone.second->dump();
121 });
122
Matt Spinler7787def2021-10-14 16:33:16 -0500123 std::ofstream file{Manager::dumpFile};
124 if (!file)
125 {
126 log<level::ERR>("Could not open file for fan dump");
127 return;
128 }
129
130 file << std::setw(4) << data;
131
132 debugDumpEventSource.reset();
Matt Spinler2fc0a352021-10-04 15:10:57 -0500133}
134
Matt Spinlerb5c21a22021-10-14 16:52:12 -0500135void Manager::dumpCache(json& data)
136{
137 auto& objects = data["objects"];
138 for (const auto& [path, interfaces] : _objects)
139 {
140 auto& interfaceJSON = objects[path];
141
142 for (const auto& [interface, properties] : interfaces)
143 {
144 auto& propertyJSON = interfaceJSON[interface];
145 for (const auto& [propName, propValue] : properties)
146 {
147 std::visit(
148 [&obj = propertyJSON[propName]](auto&& val) { obj = val; },
149 propValue);
150 }
151 }
152 }
153
154 auto& parameters = data["parameters"];
155 for (const auto& [name, value] : _parameters)
156 {
Matt Spinler29088e72021-11-08 16:23:27 -0600157 std::visit([&obj = parameters[name]](auto&& val) { obj = val; }, value);
Matt Spinlerb5c21a22021-10-14 16:52:12 -0500158 }
159
Matt Spinlerc3eb7b32022-04-25 15:44:16 -0500160 std::for_each(_events.begin(), _events.end(), [&data](const auto& event) {
161 data["events"][event.second->getName()] = event.second->dump();
162 });
163
Matt Spinlerb5c21a22021-10-14 16:52:12 -0500164 data["services"] = _servTree;
165}
166
Matthew Barthe91ac862021-05-25 16:22:17 -0500167void Manager::load()
168{
Matthew Barth3770a1d2021-06-10 15:09:37 -0500169 if (_loadAllowed)
Matthew Barthde90fb42021-03-04 16:34:28 -0600170 {
Matthew Barth3770a1d2021-06-10 15:09:37 -0500171 // Load the available profiles and which are active
172 setProfiles();
173
174 // Load the zone configurations
175 auto zones = getConfig<Zone>(false, _event, this);
176 // Load the fan configurations and move each fan into its zone
177 auto fans = getConfig<Fan>(false);
178 for (auto& fan : fans)
Matthew Barthde90fb42021-03-04 16:34:28 -0600179 {
Matthew Barth3770a1d2021-06-10 15:09:37 -0500180 configKey fanProfile =
181 std::make_pair(fan.second->getZone(), fan.first.second);
182 auto itZone = std::find_if(
183 zones.begin(), zones.end(), [&fanProfile](const auto& zone) {
184 return Manager::inConfig(fanProfile, zone.first);
185 });
186 if (itZone != zones.end())
Matthew Barth6f787302021-03-25 15:01:01 -0500187 {
Matthew Barth3770a1d2021-06-10 15:09:37 -0500188 if (itZone->second->getTarget() != fan.second->getTarget() &&
189 fan.second->getTarget() != 0)
190 {
191 // Update zone target to current target of the fan in the
192 // zone
193 itZone->second->setTarget(fan.second->getTarget());
194 }
195 itZone->second->addFan(std::move(fan.second));
Matthew Barth6f787302021-03-25 15:01:01 -0500196 }
Matthew Barthde90fb42021-03-04 16:34:28 -0600197 }
Matthew Barth3770a1d2021-06-10 15:09:37 -0500198
Matthew Barth3695ac32021-10-06 14:55:30 -0500199 // Save all currently available groups, if any, then clear for reloading
200 auto groups = std::move(Event::getAllGroups(false));
201 Event::clearAllGroups();
202
203 std::map<configKey, std::unique_ptr<Event>> events;
204 try
205 {
206 // Load any events configured, including all the groups
207 events = getConfig<Event>(true, this, zones);
208 }
209 catch (const std::runtime_error& re)
210 {
211 // Restore saved set of all available groups for current events
212 Event::setAllGroups(std::move(groups));
213 throw re;
214 }
Matthew Barth3770a1d2021-06-10 15:09:37 -0500215
216 // Enable zones
217 _zones = std::move(zones);
218 std::for_each(_zones.begin(), _zones.end(),
219 [](const auto& entry) { entry.second->enable(); });
220
221 // Clear current timers and signal subscriptions before enabling events
222 // To save reloading services and/or objects into cache, do not clear
223 // cache
224 _timers.clear();
225 _signals.clear();
226
227 // Enable events
228 _events = std::move(events);
229 std::for_each(_events.begin(), _events.end(),
230 [](const auto& entry) { entry.second->enable(); });
231
232 _loadAllowed = false;
Matthew Barthde90fb42021-03-04 16:34:28 -0600233 }
Matthew Barthacd737c2021-03-04 11:04:01 -0600234}
235
Matthew Barth48f44da2021-05-27 15:43:34 -0500236void Manager::powerStateChanged(bool powerStateOn)
237{
238 if (powerStateOn)
239 {
Matthew Barth6a2418a2021-09-01 09:10:09 -0500240 if (_zones.empty())
241 {
242 throw std::runtime_error("No configured zones found at poweron");
243 }
Matthew Barth48f44da2021-05-27 15:43:34 -0500244 std::for_each(_zones.begin(), _zones.end(), [](const auto& entry) {
245 entry.second->setTarget(entry.second->getPoweronTarget());
246 });
Matt Spinlerd1f97f42021-10-29 16:19:24 -0500247
248 // Tell events to run their power on triggers
249 std::for_each(_events.begin(), _events.end(),
250 [](const auto& entry) { entry.second->powerOn(); });
251 }
252 else
253 {
254 // Tell events to run their power off triggers
255 std::for_each(_events.begin(), _events.end(),
256 [](const auto& entry) { entry.second->powerOff(); });
Matthew Barth48f44da2021-05-27 15:43:34 -0500257 }
258}
259
Matthew Barthacd737c2021-03-04 11:04:01 -0600260const std::vector<std::string>& Manager::getActiveProfiles()
261{
262 return _activeProfiles;
Matthew Bartha227a162020-08-05 10:51:45 -0500263}
264
Matthew Barth0206c722021-03-30 15:20:05 -0500265bool Manager::inConfig(const configKey& input, const configKey& comp)
266{
267 // Config names dont match, do not include in config
268 if (input.first != comp.first)
269 {
270 return false;
271 }
272 // No profiles specified by input config, can be used in any config
273 if (input.second.empty())
274 {
275 return true;
276 }
277 else
278 {
279 // Profiles must have one match in the other's profiles(and they must be
280 // an active profile) to be used in the config
281 return std::any_of(
282 input.second.begin(), input.second.end(),
283 [&comp](const auto& lProfile) {
284 return std::any_of(
285 comp.second.begin(), comp.second.end(),
286 [&lProfile](const auto& rProfile) {
287 if (lProfile != rProfile)
288 {
289 return false;
290 }
291 auto activeProfs = getActiveProfiles();
292 return std::find(activeProfs.begin(), activeProfs.end(),
293 lProfile) != activeProfs.end();
294 });
295 });
296 }
297}
298
Matthew Barth12cb1252021-03-08 16:47:30 -0600299bool Manager::hasOwner(const std::string& path, const std::string& intf)
300{
301 auto itServ = _servTree.find(path);
302 if (itServ == _servTree.end())
303 {
304 // Path not found in cache, therefore owner missing
305 return false;
306 }
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500307 for (const auto& service : itServ->second)
Matthew Barth12cb1252021-03-08 16:47:30 -0600308 {
309 auto itIntf = std::find_if(
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500310 service.second.second.begin(), service.second.second.end(),
Matthew Barth12cb1252021-03-08 16:47:30 -0600311 [&intf](const auto& interface) { return intf == interface; });
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500312 if (itIntf != std::end(service.second.second))
Matthew Barth12cb1252021-03-08 16:47:30 -0600313 {
314 // Service found, return owner state
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500315 return service.second.first;
Matthew Barth12cb1252021-03-08 16:47:30 -0600316 }
317 }
318 // Interface not found in cache, therefore owner missing
319 return false;
320}
321
Matthew Barth6d8e2d32022-02-01 16:47:08 -0600322void Manager::setOwner(const std::string& serv, bool hasOwner)
323{
324 // Update owner state on all entries of `serv`
325 for (auto& itPath : _servTree)
326 {
327 auto itServ = itPath.second.find(serv);
328 if (itServ != itPath.second.end())
329 {
330 itServ->second.first = hasOwner;
331
332 // Remove associated interfaces from object cache when service no
333 // longer has an owner
334 if (!hasOwner && _objects.find(itPath.first) != _objects.end())
335 {
336 for (auto& intf : itServ->second.second)
337 {
338 _objects[itPath.first].erase(intf);
339 }
340 }
341 }
342 }
343}
344
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500345void Manager::setOwner(const std::string& path, const std::string& serv,
346 const std::string& intf, bool isOwned)
347{
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500348 // Set owner state for specific object given
349 auto& ownIntf = _servTree[path][serv];
350 ownIntf.first = isOwned;
351 auto itIntf = std::find_if(
352 ownIntf.second.begin(), ownIntf.second.end(),
353 [&intf](const auto& interface) { return intf == interface; });
354 if (itIntf == std::end(ownIntf.second))
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500355 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500356 ownIntf.second.emplace_back(intf);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500357 }
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500358
359 // Update owner state on all entries of the same `serv` & `intf`
360 for (auto& itPath : _servTree)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500361 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500362 if (itPath.first == path)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500363 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500364 // Already set/updated owner on this path for `serv` & `intf`
365 continue;
366 }
367 for (auto& itServ : itPath.second)
368 {
369 if (itServ.first != serv)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500370 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500371 continue;
372 }
373 auto itIntf = std::find_if(
374 itServ.second.second.begin(), itServ.second.second.end(),
375 [&intf](const auto& interface) { return intf == interface; });
376 if (itIntf != std::end(itServ.second.second))
377 {
378 itServ.second.first = isOwned;
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500379 }
380 }
381 }
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500382}
383
384const std::string& Manager::findService(const std::string& path,
385 const std::string& intf)
386{
387 static const std::string empty = "";
388
389 auto itServ = _servTree.find(path);
390 if (itServ != _servTree.end())
391 {
392 for (const auto& service : itServ->second)
393 {
394 auto itIntf = std::find_if(
395 service.second.second.begin(), service.second.second.end(),
396 [&intf](const auto& interface) { return intf == interface; });
397 if (itIntf != std::end(service.second.second))
398 {
399 // Service found, return service name
400 return service.first;
401 }
402 }
403 }
404
405 return empty;
406}
407
Matthew Barth98f6fc12021-04-16 10:48:37 -0500408void Manager::addServices(const std::string& intf, int32_t depth)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500409{
410 // Get all subtree objects for the given interface
Matt Spinler34835152021-07-01 12:28:58 -0600411 auto objects = util::SDBusPlus::getSubTreeRaw(util::SDBusPlus::getBus(),
412 "/", intf, depth);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500413 // Add what's returned to the cache of path->services
414 for (auto& itPath : objects)
415 {
416 auto pathIter = _servTree.find(itPath.first);
417 if (pathIter != _servTree.end())
418 {
419 // Path found in cache
420 for (auto& itServ : itPath.second)
421 {
422 auto servIter = pathIter->second.find(itServ.first);
423 if (servIter != pathIter->second.end())
424 {
Matt Spinlerd9f85c92022-04-07 12:55:48 -0500425 if (std::find(servIter->second.second.begin(),
426 servIter->second.second.end(),
427 intf) == servIter->second.second.end())
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500428 {
Matt Spinlerd9f85c92022-04-07 12:55:48 -0500429 // Add interface to cache
430 servIter->second.second.emplace_back(intf);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500431 }
432 }
433 else
434 {
435 // Service not found in cache
436 auto intfs = {intf};
437 pathIter->second[itServ.first] =
438 std::make_pair(true, intfs);
439 }
440 }
441 }
442 else
443 {
444 // Path not found in cache
445 auto intfs = {intf};
Matt Spinlera0b8a682021-10-14 15:38:48 -0500446 for (const auto& [servName, servIntfs] : itPath.second)
447 {
448 _servTree[itPath.first][servName] = std::make_pair(true, intfs);
449 }
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500450 }
451 }
452}
453
454const std::string& Manager::getService(const std::string& path,
455 const std::string& intf)
456{
457 // Retrieve service from cache
458 const auto& serviceName = findService(path, intf);
459 if (serviceName.empty())
460 {
Matthew Barth98f6fc12021-04-16 10:48:37 -0500461 addServices(intf, 0);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500462 return findService(path, intf);
463 }
464
465 return serviceName;
466}
467
Matthew Barthf41e9472021-05-13 16:38:06 -0500468std::vector<std::string> Manager::findPaths(const std::string& serv,
469 const std::string& intf)
470{
471 std::vector<std::string> paths;
472
473 for (const auto& path : _servTree)
474 {
475 auto itServ = path.second.find(serv);
476 if (itServ != path.second.end())
477 {
478 if (std::find(itServ->second.second.begin(),
479 itServ->second.second.end(),
480 intf) != itServ->second.second.end())
481 {
482 if (std::find(paths.begin(), paths.end(), path.first) ==
483 paths.end())
484 {
485 paths.push_back(path.first);
486 }
487 }
488 }
489 }
490
491 return paths;
492}
493
494std::vector<std::string> Manager::getPaths(const std::string& serv,
495 const std::string& intf)
496{
497 auto paths = findPaths(serv, intf);
498 if (paths.empty())
499 {
500 addServices(intf, 0);
501 return findPaths(serv, intf);
502 }
503
504 return paths;
505}
506
Mike Capps1a19ead2021-10-22 09:15:14 -0400507void Manager::insertFilteredObjects(ManagedObjects& ref)
508{
Matt Spinlerc2c2db72022-04-07 13:59:37 -0500509 // Filter out objects that aren't part of a group
510 const auto& allGroupMembers = Group::getAllMembers();
511 auto it = ref.begin();
512
513 while (it != ref.end())
514 {
515 if (allGroupMembers.find(it->first) == allGroupMembers.end())
516 {
517 it = ref.erase(it);
518 }
519 else
520 {
521 it++;
522 }
523 }
524
Mike Capps1a19ead2021-10-22 09:15:14 -0400525 for (auto& [path, pathMap] : ref)
526 {
527 for (auto& [intf, intfMap] : pathMap)
528 {
529 // for each property on this path+interface
530 for (auto& [prop, value] : intfMap)
531 {
532 setProperty(path, intf, prop, value);
533 }
534 }
535 }
536}
537
Matthew Barthf41e9472021-05-13 16:38:06 -0500538void Manager::addObjects(const std::string& path, const std::string& intf,
Matt Spinler9ac325c2022-04-25 14:13:49 -0500539 const std::string& prop,
540 const std::string& serviceName)
Matthew Barthf41e9472021-05-13 16:38:06 -0500541{
Matt Spinler9ac325c2022-04-25 14:13:49 -0500542 auto service = serviceName;
Matthew Barthf41e9472021-05-13 16:38:06 -0500543 if (service.empty())
544 {
Matt Spinler9ac325c2022-04-25 14:13:49 -0500545 service = getService(path, intf);
546 if (service.empty())
547 {
548 // Log service not found for object
549 log<level::DEBUG>(
550 fmt::format(
551 "Unable to get service name for path {}, interface {}",
552 path, intf)
553 .c_str());
554 return;
555 }
556 }
557 else
558 {
559 // The service is known, so the service cache can be
560 // populated even if the path itself isn't present.
561 const auto& s = findService(path, intf);
562 if (s.empty())
563 {
564 addServices(intf, 0);
565 }
Matthew Barthf41e9472021-05-13 16:38:06 -0500566 }
567
568 auto objMgrPaths = getPaths(service, "org.freedesktop.DBus.ObjectManager");
569 if (objMgrPaths.empty())
570 {
571 // No object manager interface provided by service?
572 // Attempt to retrieve property directly
Matt Spinlerf16f0632022-05-09 14:27:46 -0500573 try
574 {
575 auto value =
576 util::SDBusPlus::getPropertyVariant<PropertyVariantType>(
577 _bus, service, path, intf, prop);
Mike Capps1a19ead2021-10-22 09:15:14 -0400578
Matt Spinlerf16f0632022-05-09 14:27:46 -0500579 setProperty(path, intf, prop, value);
580 }
581 catch (const std::exception& e)
582 {}
Matthew Barthf41e9472021-05-13 16:38:06 -0500583 return;
584 }
585
586 for (const auto& objMgrPath : objMgrPaths)
587 {
588 // Get all managed objects of service
589 auto objects = util::SDBusPlus::getManagedObjects<PropertyVariantType>(
590 _bus, service, objMgrPath);
591
Matt Spinlerc2c2db72022-04-07 13:59:37 -0500592 // insert all objects that are in groups but remove any NaN values
Mike Capps1a19ead2021-10-22 09:15:14 -0400593 insertFilteredObjects(objects);
Matthew Barthf41e9472021-05-13 16:38:06 -0500594 }
595}
596
597const std::optional<PropertyVariantType>
598 Manager::getProperty(const std::string& path, const std::string& intf,
599 const std::string& prop)
600{
601 // TODO Objects hosted by fan control (i.e. ThermalMode) are required to
602 // update the cache upon being set/updated
603 auto itPath = _objects.find(path);
604 if (itPath != _objects.end())
605 {
606 auto itIntf = itPath->second.find(intf);
607 if (itIntf != itPath->second.end())
608 {
609 auto itProp = itIntf->second.find(prop);
610 if (itProp != itIntf->second.end())
611 {
612 return itProp->second;
613 }
614 }
615 }
616
617 return std::nullopt;
618}
619
Mike Capps1a19ead2021-10-22 09:15:14 -0400620void Manager::setProperty(const std::string& path, const std::string& intf,
621 const std::string& prop, PropertyVariantType value)
622{
623 // filter NaNs out of the cache
624 if (PropertyContainsNan(value))
625 {
626 // dont use operator [] if paths dont exist
627 if (_objects.find(path) != _objects.end() &&
628 _objects[path].find(intf) != _objects[path].end())
629 {
630 _objects[path][intf].erase(prop);
631 }
632 }
633 else
634 {
635 _objects[path][intf][prop] = std::move(value);
636 }
637}
638
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500639void Manager::addTimer(const TimerType type,
640 const std::chrono::microseconds interval,
641 std::unique_ptr<TimerPkg> pkg)
642{
643 auto dataPtr =
644 std::make_unique<TimerData>(std::make_pair(type, std::move(*pkg)));
645 Timer timer(_event,
646 std::bind(&Manager::timerExpired, this, std::ref(*dataPtr)));
647 if (type == TimerType::repeating)
648 {
649 timer.restart(interval);
650 }
651 else if (type == TimerType::oneshot)
652 {
653 timer.restartOnce(interval);
654 }
655 else
656 {
657 throw std::invalid_argument("Invalid Timer Type");
658 }
659 _timers.emplace_back(std::move(dataPtr), std::move(timer));
660}
661
Matthew Barth2f359f72022-02-15 10:00:26 -0600662void Manager::addGroups(const std::vector<Group>& groups)
Matt Spinlerade0c372021-10-28 16:09:44 -0500663{
Matthew Barth1a5c6232022-02-14 14:28:41 -0600664 std::string lastServ;
665 std::vector<std::string> objMgrPaths;
Matthew Barth2f359f72022-02-15 10:00:26 -0600666 std::set<std::string> services;
667 for (const auto& group : groups)
Matt Spinlerade0c372021-10-28 16:09:44 -0500668 {
Matthew Barth2f359f72022-02-15 10:00:26 -0600669 for (const auto& member : group.getMembers())
Matt Spinlerade0c372021-10-28 16:09:44 -0500670 {
Matthew Barth2f359f72022-02-15 10:00:26 -0600671 try
Matthew Barth95d73492022-02-09 11:30:35 -0600672 {
Matthew Barth2f359f72022-02-15 10:00:26 -0600673 auto service = group.getService();
674 if (service.empty())
Matthew Barth55627ad2021-12-02 22:28:29 -0600675 {
Matthew Barth2f359f72022-02-15 10:00:26 -0600676 service = getService(member, group.getInterface());
Matthew Barth1a5c6232022-02-14 14:28:41 -0600677 }
Matthew Barth55627ad2021-12-02 22:28:29 -0600678
Matthew Barth2f359f72022-02-15 10:00:26 -0600679 if (!service.empty())
Matthew Barth1a5c6232022-02-14 14:28:41 -0600680 {
Matthew Barth2f359f72022-02-15 10:00:26 -0600681 if (lastServ != service)
Matthew Barth1a5c6232022-02-14 14:28:41 -0600682 {
Matthew Barth2f359f72022-02-15 10:00:26 -0600683 objMgrPaths = getPaths(
684 service, "org.freedesktop.DBus.ObjectManager");
685 lastServ = service;
686 }
Matthew Barth1a5c6232022-02-14 14:28:41 -0600687
Matthew Barth2f359f72022-02-15 10:00:26 -0600688 // Look for the ObjectManager as an ancestor from the
689 // member.
690 auto hasObjMgr = std::any_of(
691 objMgrPaths.begin(), objMgrPaths.end(),
692 [&member](const auto& path) {
693 return member.find(path) != std::string::npos;
694 });
695
696 if (!hasObjMgr)
697 {
698 // No object manager interface provided for group member
699 // Attempt to retrieve group member property directly
Matt Spinlerf16f0632022-05-09 14:27:46 -0500700 try
701 {
702 auto value = util::SDBusPlus::getPropertyVariant<
703 PropertyVariantType>(_bus, service, member,
704 group.getInterface(),
705 group.getProperty());
706 setProperty(member, group.getInterface(),
707 group.getProperty(), value);
708 }
709 catch (const std::exception& e)
710 {}
Matthew Barth2f359f72022-02-15 10:00:26 -0600711 continue;
712 }
713
714 if (services.find(service) == services.end())
715 {
716 services.insert(service);
717 for (const auto& objMgrPath : objMgrPaths)
718 {
719 // Get all managed objects from the service
720 auto objects = util::SDBusPlus::getManagedObjects<
721 PropertyVariantType>(_bus, service, objMgrPath);
722
723 // Insert objects into cache
724 insertFilteredObjects(objects);
725 }
Matthew Barth1a5c6232022-02-14 14:28:41 -0600726 }
Matthew Barth55627ad2021-12-02 22:28:29 -0600727 }
728 }
Matthew Barth2f359f72022-02-15 10:00:26 -0600729 catch (const util::DBusError&)
730 {
731 // No service or property found for group member with the
732 // group's configured interface
733 continue;
734 }
Matt Spinlerade0c372021-10-28 16:09:44 -0500735 }
736 }
737}
738
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500739void Manager::timerExpired(TimerData& data)
740{
Matt Spinlerade0c372021-10-28 16:09:44 -0500741 if (std::get<bool>(data.second))
742 {
Matthew Barth2f359f72022-02-15 10:00:26 -0600743 addGroups(std::get<const std::vector<Group>&>(data.second));
Matt Spinlerade0c372021-10-28 16:09:44 -0500744 }
745
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500746 auto& actions =
747 std::get<std::vector<std::unique_ptr<ActionBase>>&>(data.second);
748 // Perform the actions in the timer data
749 std::for_each(actions.begin(), actions.end(),
Matthew Barth00f6aa02021-04-09 10:49:47 -0500750 [](auto& action) { action->run(); });
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500751
752 // Remove oneshot timers after they expired
753 if (data.first == TimerType::oneshot)
754 {
755 auto itTimer = std::find_if(
756 _timers.begin(), _timers.end(), [&data](const auto& timer) {
757 return (data.first == timer.first->first &&
758 (std::get<std::string>(data.second) ==
759 std::get<std::string>(timer.first->second)));
760 });
761 if (itTimer != std::end(_timers))
762 {
763 _timers.erase(itTimer);
764 }
765 }
766}
767
Patrick Williamscb356d42022-07-22 19:26:53 -0500768void Manager::handleSignal(sdbusplus::message_t& msg,
Matthew Barthc024d782021-11-09 16:15:49 -0600769 const std::vector<SignalPkg>* pkgs)
Matthew Barthebabc042021-05-13 15:38:58 -0500770{
Matthew Barthc024d782021-11-09 16:15:49 -0600771 for (auto& pkg : *pkgs)
Matthew Barthebabc042021-05-13 15:38:58 -0500772 {
773 // Handle the signal callback and only run the actions if the handler
774 // updated the cache for the given SignalObject
775 if (std::get<SignalHandler>(pkg)(msg, std::get<SignalObject>(pkg),
776 *this))
777 {
778 // Perform the actions in the handler package
Matt Spinlerd0ba86a2021-11-09 10:09:13 -0600779 auto& actions = std::get<TriggerActions>(pkg);
Matthew Barthc3a69082021-11-15 14:32:48 -0600780 std::for_each(actions.begin(), actions.end(), [](auto& action) {
781 if (action.get())
782 {
783 action.get()->run();
784 }
785 });
Matthew Barthebabc042021-05-13 15:38:58 -0500786 }
Matthew Barthc024d782021-11-09 16:15:49 -0600787 // Only rewind message when not last package
788 if (&pkg != &pkgs->back())
789 {
790 sd_bus_message_rewind(msg.get(), true);
791 }
Matthew Barthebabc042021-05-13 15:38:58 -0500792 }
793}
794
Matthew Barthacd737c2021-03-04 11:04:01 -0600795void Manager::setProfiles()
796{
797 // Profiles JSON config file is optional
Mike Capps808d7fe2022-06-13 10:12:16 -0400798 auto confFile =
799 fan::JsonConfig::getConfFile(confAppName, Profile::confFileName, true);
Matthew Barthe91ac862021-05-25 16:22:17 -0500800
801 _profiles.clear();
Matthew Barthacd737c2021-03-04 11:04:01 -0600802 if (!confFile.empty())
803 {
804 for (const auto& entry : fan::JsonConfig::load(confFile))
805 {
806 auto obj = std::make_unique<Profile>(entry);
807 _profiles.emplace(
808 std::make_pair(obj->getName(), obj->getProfiles()),
809 std::move(obj));
810 }
811 }
Matthew Barthe91ac862021-05-25 16:22:17 -0500812
Matthew Barthacd737c2021-03-04 11:04:01 -0600813 // Ensure all configurations use the same set of active profiles
814 // (In case a profile's active state changes during configuration)
Matthew Barthe91ac862021-05-25 16:22:17 -0500815 _activeProfiles.clear();
Matthew Barthacd737c2021-03-04 11:04:01 -0600816 for (const auto& profile : _profiles)
817 {
818 if (profile.second->isActive())
819 {
820 _activeProfiles.emplace_back(profile.first.first);
821 }
822 }
823}
824
Matt Spinlerd0ba86a2021-11-09 10:09:13 -0600825void Manager::addParameterTrigger(
826 const std::string& name, std::vector<std::unique_ptr<ActionBase>>& actions)
827{
828 auto it = _parameterTriggers.find(name);
829 if (it != _parameterTriggers.end())
830 {
831 std::for_each(actions.begin(), actions.end(),
832 [&actList = it->second](auto& action) {
833 actList.emplace_back(std::ref(action));
834 });
835 }
836 else
837 {
838 TriggerActions triggerActions;
839 std::for_each(actions.begin(), actions.end(),
840 [&triggerActions](auto& action) {
841 triggerActions.emplace_back(std::ref(action));
842 });
843 _parameterTriggers[name] = std::move(triggerActions);
844 }
845}
846
847void Manager::runParameterActions(const std::string& name)
848{
849 auto it = _parameterTriggers.find(name);
850 if (it != _parameterTriggers.end())
851 {
852 std::for_each(it->second.begin(), it->second.end(),
853 [](auto& action) { action.get()->run(); });
854 }
855}
856
Matthew Bartha227a162020-08-05 10:51:45 -0500857} // namespace phosphor::fan::control::json