blob: 0161304743d45da75895d4651c252514ededf579 [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(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500100 "main", std::format("Error reloading configs, no changes made: {}",
Matthew Barthbd52ed02022-02-07 15:15:10 -0600101 re.what()));
Matthew Barthe91ac862021-05-25 16:22:17 -0500102 }
103}
104
Matt Spinler27f5f4e2022-09-01 14:57:39 -0500105void Manager::dumpDebugData(sdeventplus::source::Signal&,
106 const struct signalfd_siginfo*)
Matt Spinler2fc0a352021-10-04 15:10:57 -0500107{
Matt Spinler7787def2021-10-14 16:33:16 -0500108 json data;
109 FlightRecorder::instance().dump(data);
Matt Spinlerb5c21a22021-10-14 16:52:12 -0500110 dumpCache(data);
Matt Spinler7787def2021-10-14 16:33:16 -0500111
Matt Spinler9db6dd12021-10-29 16:10:08 -0500112 std::for_each(_zones.begin(), _zones.end(), [&data](const auto& zone) {
113 data["zones"][zone.second->getName()] = zone.second->dump();
114 });
115
Matt Spinler7787def2021-10-14 16:33:16 -0500116 std::ofstream file{Manager::dumpFile};
117 if (!file)
118 {
119 log<level::ERR>("Could not open file for fan dump");
120 return;
121 }
122
123 file << std::setw(4) << data;
Matt Spinler2fc0a352021-10-04 15:10:57 -0500124}
125
Matt Spinlerb5c21a22021-10-14 16:52:12 -0500126void Manager::dumpCache(json& data)
127{
128 auto& objects = data["objects"];
129 for (const auto& [path, interfaces] : _objects)
130 {
131 auto& interfaceJSON = objects[path];
132
133 for (const auto& [interface, properties] : interfaces)
134 {
135 auto& propertyJSON = interfaceJSON[interface];
136 for (const auto& [propName, propValue] : properties)
137 {
138 std::visit(
139 [&obj = propertyJSON[propName]](auto&& val) { obj = val; },
140 propValue);
141 }
142 }
143 }
144
145 auto& parameters = data["parameters"];
146 for (const auto& [name, value] : _parameters)
147 {
Matt Spinler29088e72021-11-08 16:23:27 -0600148 std::visit([&obj = parameters[name]](auto&& val) { obj = val; }, value);
Matt Spinlerb5c21a22021-10-14 16:52:12 -0500149 }
150
Matt Spinlerc3eb7b32022-04-25 15:44:16 -0500151 std::for_each(_events.begin(), _events.end(), [&data](const auto& event) {
152 data["events"][event.second->getName()] = event.second->dump();
153 });
154
Matt Spinlerb5c21a22021-10-14 16:52:12 -0500155 data["services"] = _servTree;
156}
157
Matthew Barthe91ac862021-05-25 16:22:17 -0500158void Manager::load()
159{
Matthew Barth3770a1d2021-06-10 15:09:37 -0500160 if (_loadAllowed)
Matthew Barthde90fb42021-03-04 16:34:28 -0600161 {
Matthew Barth3770a1d2021-06-10 15:09:37 -0500162 // Load the available profiles and which are active
163 setProfiles();
164
165 // Load the zone configurations
166 auto zones = getConfig<Zone>(false, _event, this);
167 // Load the fan configurations and move each fan into its zone
168 auto fans = getConfig<Fan>(false);
169 for (auto& fan : fans)
Matthew Barthde90fb42021-03-04 16:34:28 -0600170 {
Patrick Williams61b73292023-05-10 07:50:12 -0500171 configKey fanProfile = std::make_pair(fan.second->getZone(),
172 fan.first.second);
173 auto itZone = std::find_if(zones.begin(), zones.end(),
174 [&fanProfile](const auto& zone) {
175 return Manager::inConfig(fanProfile, zone.first);
176 });
Matthew Barth3770a1d2021-06-10 15:09:37 -0500177 if (itZone != zones.end())
Matthew Barth6f787302021-03-25 15:01:01 -0500178 {
Matthew Barth3770a1d2021-06-10 15:09:37 -0500179 if (itZone->second->getTarget() != fan.second->getTarget() &&
180 fan.second->getTarget() != 0)
181 {
182 // Update zone target to current target of the fan in the
183 // zone
184 itZone->second->setTarget(fan.second->getTarget());
185 }
186 itZone->second->addFan(std::move(fan.second));
Matthew Barth6f787302021-03-25 15:01:01 -0500187 }
Matthew Barthde90fb42021-03-04 16:34:28 -0600188 }
Matthew Barth3770a1d2021-06-10 15:09:37 -0500189
Matthew Barth3695ac32021-10-06 14:55:30 -0500190 // Save all currently available groups, if any, then clear for reloading
191 auto groups = std::move(Event::getAllGroups(false));
192 Event::clearAllGroups();
193
194 std::map<configKey, std::unique_ptr<Event>> events;
195 try
196 {
197 // Load any events configured, including all the groups
198 events = getConfig<Event>(true, this, zones);
199 }
200 catch (const std::runtime_error& re)
201 {
202 // Restore saved set of all available groups for current events
203 Event::setAllGroups(std::move(groups));
204 throw re;
205 }
Matthew Barth3770a1d2021-06-10 15:09:37 -0500206
207 // Enable zones
208 _zones = std::move(zones);
209 std::for_each(_zones.begin(), _zones.end(),
210 [](const auto& entry) { entry.second->enable(); });
211
212 // Clear current timers and signal subscriptions before enabling events
213 // To save reloading services and/or objects into cache, do not clear
214 // cache
215 _timers.clear();
216 _signals.clear();
217
218 // Enable events
219 _events = std::move(events);
Matt Spinlere56672d2023-01-10 16:05:11 -0600220 FlightRecorder::instance().log("main", "Enabling events");
Matthew Barth3770a1d2021-06-10 15:09:37 -0500221 std::for_each(_events.begin(), _events.end(),
222 [](const auto& entry) { entry.second->enable(); });
Matt Spinlere56672d2023-01-10 16:05:11 -0600223 FlightRecorder::instance().log("main", "Done enabling events");
Matthew Barth3770a1d2021-06-10 15:09:37 -0500224
225 _loadAllowed = false;
Matthew Barthde90fb42021-03-04 16:34:28 -0600226 }
Matthew Barthacd737c2021-03-04 11:04:01 -0600227}
228
Matthew Barth48f44da2021-05-27 15:43:34 -0500229void Manager::powerStateChanged(bool powerStateOn)
230{
231 if (powerStateOn)
232 {
Matt Spinler8d9c3912023-11-08 12:41:47 -0600233 FlightRecorder::instance().log("power", "Power on");
Matthew Barth6a2418a2021-09-01 09:10:09 -0500234 if (_zones.empty())
235 {
236 throw std::runtime_error("No configured zones found at poweron");
237 }
Matthew Barth48f44da2021-05-27 15:43:34 -0500238 std::for_each(_zones.begin(), _zones.end(), [](const auto& entry) {
239 entry.second->setTarget(entry.second->getPoweronTarget());
240 });
Matt Spinlerd1f97f42021-10-29 16:19:24 -0500241
242 // Tell events to run their power on triggers
243 std::for_each(_events.begin(), _events.end(),
244 [](const auto& entry) { entry.second->powerOn(); });
245 }
246 else
247 {
Matt Spinler8d9c3912023-11-08 12:41:47 -0600248 FlightRecorder::instance().log("power", "Power off");
Matt Spinlerd1f97f42021-10-29 16:19:24 -0500249 // Tell events to run their power off triggers
250 std::for_each(_events.begin(), _events.end(),
251 [](const auto& entry) { entry.second->powerOff(); });
Matthew Barth48f44da2021-05-27 15:43:34 -0500252 }
253}
254
Matthew Barthacd737c2021-03-04 11:04:01 -0600255const std::vector<std::string>& Manager::getActiveProfiles()
256{
257 return _activeProfiles;
Matthew Bartha227a162020-08-05 10:51:45 -0500258}
259
Matthew Barth0206c722021-03-30 15:20:05 -0500260bool Manager::inConfig(const configKey& input, const configKey& comp)
261{
262 // Config names dont match, do not include in config
263 if (input.first != comp.first)
264 {
265 return false;
266 }
267 // No profiles specified by input config, can be used in any config
268 if (input.second.empty())
269 {
270 return true;
271 }
272 else
273 {
274 // Profiles must have one match in the other's profiles(and they must be
275 // an active profile) to be used in the config
Patrick Williams61b73292023-05-10 07:50:12 -0500276 return std::any_of(input.second.begin(), input.second.end(),
277 [&comp](const auto& lProfile) {
278 return std::any_of(comp.second.begin(), comp.second.end(),
279 [&lProfile](const auto& rProfile) {
280 if (lProfile != rProfile)
281 {
282 return false;
283 }
284 auto activeProfs = getActiveProfiles();
285 return std::find(activeProfs.begin(), activeProfs.end(),
286 lProfile) != activeProfs.end();
Matthew Barth0206c722021-03-30 15:20:05 -0500287 });
Patrick Williams61b73292023-05-10 07:50:12 -0500288 });
Matthew Barth0206c722021-03-30 15:20:05 -0500289 }
290}
291
Matthew Barth12cb1252021-03-08 16:47:30 -0600292bool Manager::hasOwner(const std::string& path, const std::string& intf)
293{
294 auto itServ = _servTree.find(path);
295 if (itServ == _servTree.end())
296 {
297 // Path not found in cache, therefore owner missing
298 return false;
299 }
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500300 for (const auto& service : itServ->second)
Matthew Barth12cb1252021-03-08 16:47:30 -0600301 {
302 auto itIntf = std::find_if(
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500303 service.second.second.begin(), service.second.second.end(),
Matthew Barth12cb1252021-03-08 16:47:30 -0600304 [&intf](const auto& interface) { return intf == interface; });
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500305 if (itIntf != std::end(service.second.second))
Matthew Barth12cb1252021-03-08 16:47:30 -0600306 {
307 // Service found, return owner state
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500308 return service.second.first;
Matthew Barth12cb1252021-03-08 16:47:30 -0600309 }
310 }
311 // Interface not found in cache, therefore owner missing
312 return false;
313}
314
Matthew Barth6d8e2d32022-02-01 16:47:08 -0600315void Manager::setOwner(const std::string& serv, bool hasOwner)
316{
317 // Update owner state on all entries of `serv`
318 for (auto& itPath : _servTree)
319 {
320 auto itServ = itPath.second.find(serv);
321 if (itServ != itPath.second.end())
322 {
323 itServ->second.first = hasOwner;
324
325 // Remove associated interfaces from object cache when service no
326 // longer has an owner
327 if (!hasOwner && _objects.find(itPath.first) != _objects.end())
328 {
329 for (auto& intf : itServ->second.second)
330 {
331 _objects[itPath.first].erase(intf);
332 }
333 }
334 }
335 }
336}
337
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500338void Manager::setOwner(const std::string& path, const std::string& serv,
339 const std::string& intf, bool isOwned)
340{
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500341 // Set owner state for specific object given
342 auto& ownIntf = _servTree[path][serv];
343 ownIntf.first = isOwned;
Patrick Williams5e15c3b2023-10-20 11:18:11 -0500344 auto itIntf = std::find_if(
345 ownIntf.second.begin(), ownIntf.second.end(),
346 [&intf](const auto& interface) { return intf == interface; });
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500347 if (itIntf == std::end(ownIntf.second))
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500348 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500349 ownIntf.second.emplace_back(intf);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500350 }
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500351
352 // Update owner state on all entries of the same `serv` & `intf`
353 for (auto& itPath : _servTree)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500354 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500355 if (itPath.first == path)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500356 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500357 // Already set/updated owner on this path for `serv` & `intf`
358 continue;
359 }
360 for (auto& itServ : itPath.second)
361 {
362 if (itServ.first != serv)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500363 {
Matthew Barth2a9e7b22021-05-17 11:54:54 -0500364 continue;
365 }
366 auto itIntf = std::find_if(
367 itServ.second.second.begin(), itServ.second.second.end(),
368 [&intf](const auto& interface) { return intf == interface; });
369 if (itIntf != std::end(itServ.second.second))
370 {
371 itServ.second.first = isOwned;
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500372 }
373 }
374 }
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500375}
376
377const std::string& Manager::findService(const std::string& path,
378 const std::string& intf)
379{
380 static const std::string empty = "";
381
382 auto itServ = _servTree.find(path);
383 if (itServ != _servTree.end())
384 {
385 for (const auto& service : itServ->second)
386 {
387 auto itIntf = std::find_if(
388 service.second.second.begin(), service.second.second.end(),
389 [&intf](const auto& interface) { return intf == interface; });
390 if (itIntf != std::end(service.second.second))
391 {
392 // Service found, return service name
393 return service.first;
394 }
395 }
396 }
397
398 return empty;
399}
400
Matthew Barth98f6fc12021-04-16 10:48:37 -0500401void Manager::addServices(const std::string& intf, int32_t depth)
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500402{
403 // Get all subtree objects for the given interface
Matt Spinler34835152021-07-01 12:28:58 -0600404 auto objects = util::SDBusPlus::getSubTreeRaw(util::SDBusPlus::getBus(),
405 "/", intf, depth);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500406 // Add what's returned to the cache of path->services
407 for (auto& itPath : objects)
408 {
409 auto pathIter = _servTree.find(itPath.first);
410 if (pathIter != _servTree.end())
411 {
412 // Path found in cache
413 for (auto& itServ : itPath.second)
414 {
415 auto servIter = pathIter->second.find(itServ.first);
416 if (servIter != pathIter->second.end())
417 {
Matt Spinlerd9f85c92022-04-07 12:55:48 -0500418 if (std::find(servIter->second.second.begin(),
419 servIter->second.second.end(),
420 intf) == servIter->second.second.end())
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500421 {
Matt Spinlerd9f85c92022-04-07 12:55:48 -0500422 // Add interface to cache
423 servIter->second.second.emplace_back(intf);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500424 }
425 }
426 else
427 {
428 // Service not found in cache
429 auto intfs = {intf};
Patrick Williams61b73292023-05-10 07:50:12 -0500430 pathIter->second[itServ.first] = std::make_pair(true,
431 intfs);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500432 }
433 }
434 }
435 else
436 {
437 // Path not found in cache
438 auto intfs = {intf};
Matt Spinlera0b8a682021-10-14 15:38:48 -0500439 for (const auto& [servName, servIntfs] : itPath.second)
440 {
441 _servTree[itPath.first][servName] = std::make_pair(true, intfs);
442 }
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500443 }
444 }
445}
446
447const std::string& Manager::getService(const std::string& path,
448 const std::string& intf)
449{
450 // Retrieve service from cache
451 const auto& serviceName = findService(path, intf);
452 if (serviceName.empty())
453 {
Matthew Barth98f6fc12021-04-16 10:48:37 -0500454 addServices(intf, 0);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500455 return findService(path, intf);
456 }
457
458 return serviceName;
459}
460
Matthew Barthf41e9472021-05-13 16:38:06 -0500461std::vector<std::string> Manager::findPaths(const std::string& serv,
462 const std::string& intf)
463{
464 std::vector<std::string> paths;
465
466 for (const auto& path : _servTree)
467 {
468 auto itServ = path.second.find(serv);
469 if (itServ != path.second.end())
470 {
471 if (std::find(itServ->second.second.begin(),
472 itServ->second.second.end(),
473 intf) != itServ->second.second.end())
474 {
475 if (std::find(paths.begin(), paths.end(), path.first) ==
476 paths.end())
477 {
478 paths.push_back(path.first);
479 }
480 }
481 }
482 }
483
484 return paths;
485}
486
487std::vector<std::string> Manager::getPaths(const std::string& serv,
488 const std::string& intf)
489{
490 auto paths = findPaths(serv, intf);
491 if (paths.empty())
492 {
493 addServices(intf, 0);
494 return findPaths(serv, intf);
495 }
496
497 return paths;
498}
499
Mike Capps1a19ead2021-10-22 09:15:14 -0400500void Manager::insertFilteredObjects(ManagedObjects& ref)
501{
Matt Spinlerc2c2db72022-04-07 13:59:37 -0500502 // Filter out objects that aren't part of a group
503 const auto& allGroupMembers = Group::getAllMembers();
504 auto it = ref.begin();
505
506 while (it != ref.end())
507 {
508 if (allGroupMembers.find(it->first) == allGroupMembers.end())
509 {
510 it = ref.erase(it);
511 }
512 else
513 {
514 it++;
515 }
516 }
517
Mike Capps1a19ead2021-10-22 09:15:14 -0400518 for (auto& [path, pathMap] : ref)
519 {
520 for (auto& [intf, intfMap] : pathMap)
521 {
522 // for each property on this path+interface
523 for (auto& [prop, value] : intfMap)
524 {
525 setProperty(path, intf, prop, value);
526 }
527 }
528 }
529}
530
Matthew Barthf41e9472021-05-13 16:38:06 -0500531void Manager::addObjects(const std::string& path, const std::string& intf,
Matt Spinler9ac325c2022-04-25 14:13:49 -0500532 const std::string& prop,
533 const std::string& serviceName)
Matthew Barthf41e9472021-05-13 16:38:06 -0500534{
Matt Spinler9ac325c2022-04-25 14:13:49 -0500535 auto service = serviceName;
Matthew Barthf41e9472021-05-13 16:38:06 -0500536 if (service.empty())
537 {
Matt Spinler9ac325c2022-04-25 14:13:49 -0500538 service = getService(path, intf);
539 if (service.empty())
540 {
541 // Log service not found for object
542 log<level::DEBUG>(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500543 std::format(
Matt Spinler9ac325c2022-04-25 14:13:49 -0500544 "Unable to get service name for path {}, interface {}",
545 path, intf)
546 .c_str());
547 return;
548 }
549 }
550 else
551 {
552 // The service is known, so the service cache can be
553 // populated even if the path itself isn't present.
554 const auto& s = findService(path, intf);
555 if (s.empty())
556 {
557 addServices(intf, 0);
558 }
Matthew Barthf41e9472021-05-13 16:38:06 -0500559 }
560
561 auto objMgrPaths = getPaths(service, "org.freedesktop.DBus.ObjectManager");
Chau Ly078c0a82024-06-11 09:45:28 +0000562
563 bool useManagedObj = false;
564
565 if (!objMgrPaths.empty())
566 {
567 for (const auto& objMgrPath : objMgrPaths)
568 {
569 // Get all managed objects of service
570 auto objects =
571 util::SDBusPlus::getManagedObjects<PropertyVariantType>(
572 _bus, service, objMgrPath);
573 if (objects.size() > 0)
574 {
575 useManagedObj = true;
576 }
577 // insert all objects that are in groups but remove any NaN values
578 insertFilteredObjects(objects);
579 }
580 }
581
582 if (!useManagedObj)
Matthew Barthf41e9472021-05-13 16:38:06 -0500583 {
584 // No object manager interface provided by service?
Chau Ly078c0a82024-06-11 09:45:28 +0000585 // Or no object is managed?
Matthew Barthf41e9472021-05-13 16:38:06 -0500586 // Attempt to retrieve property directly
Matt Spinlerf16f0632022-05-09 14:27:46 -0500587 try
588 {
589 auto value =
590 util::SDBusPlus::getPropertyVariant<PropertyVariantType>(
591 _bus, service, path, intf, prop);
Mike Capps1a19ead2021-10-22 09:15:14 -0400592
Matt Spinlerf16f0632022-05-09 14:27:46 -0500593 setProperty(path, intf, prop, value);
594 }
595 catch (const std::exception& e)
596 {}
Matthew Barthf41e9472021-05-13 16:38:06 -0500597 return;
598 }
Matthew Barthf41e9472021-05-13 16:38:06 -0500599}
600
601const std::optional<PropertyVariantType>
602 Manager::getProperty(const std::string& path, const std::string& intf,
603 const std::string& prop)
604{
605 // TODO Objects hosted by fan control (i.e. ThermalMode) are required to
606 // update the cache upon being set/updated
607 auto itPath = _objects.find(path);
608 if (itPath != _objects.end())
609 {
610 auto itIntf = itPath->second.find(intf);
611 if (itIntf != itPath->second.end())
612 {
613 auto itProp = itIntf->second.find(prop);
614 if (itProp != itIntf->second.end())
615 {
616 return itProp->second;
617 }
618 }
619 }
620
621 return std::nullopt;
622}
623
Mike Capps1a19ead2021-10-22 09:15:14 -0400624void Manager::setProperty(const std::string& path, const std::string& intf,
625 const std::string& prop, PropertyVariantType value)
626{
627 // filter NaNs out of the cache
628 if (PropertyContainsNan(value))
629 {
630 // dont use operator [] if paths dont exist
631 if (_objects.find(path) != _objects.end() &&
632 _objects[path].find(intf) != _objects[path].end())
633 {
634 _objects[path][intf].erase(prop);
635 }
636 }
637 else
638 {
639 _objects[path][intf][prop] = std::move(value);
640 }
641}
642
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500643void Manager::addTimer(const TimerType type,
644 const std::chrono::microseconds interval,
645 std::unique_ptr<TimerPkg> pkg)
646{
647 auto dataPtr =
648 std::make_unique<TimerData>(std::make_pair(type, std::move(*pkg)));
649 Timer timer(_event,
650 std::bind(&Manager::timerExpired, this, std::ref(*dataPtr)));
651 if (type == TimerType::repeating)
652 {
653 timer.restart(interval);
654 }
655 else if (type == TimerType::oneshot)
656 {
657 timer.restartOnce(interval);
658 }
659 else
660 {
661 throw std::invalid_argument("Invalid Timer Type");
662 }
663 _timers.emplace_back(std::move(dataPtr), std::move(timer));
664}
665
Matthew Barth2f359f72022-02-15 10:00:26 -0600666void Manager::addGroups(const std::vector<Group>& groups)
Matt Spinlerade0c372021-10-28 16:09:44 -0500667{
Matthew Barth1a5c6232022-02-14 14:28:41 -0600668 std::string lastServ;
669 std::vector<std::string> objMgrPaths;
Matthew Barth2f359f72022-02-15 10:00:26 -0600670 std::set<std::string> services;
671 for (const auto& group : groups)
Matt Spinlerade0c372021-10-28 16:09:44 -0500672 {
Matthew Barth2f359f72022-02-15 10:00:26 -0600673 for (const auto& member : group.getMembers())
Matt Spinlerade0c372021-10-28 16:09:44 -0500674 {
Matthew Barth2f359f72022-02-15 10:00:26 -0600675 try
Matthew Barth95d73492022-02-09 11:30:35 -0600676 {
Matthew Barth2f359f72022-02-15 10:00:26 -0600677 auto service = group.getService();
678 if (service.empty())
Matthew Barth55627ad2021-12-02 22:28:29 -0600679 {
Matthew Barth2f359f72022-02-15 10:00:26 -0600680 service = getService(member, group.getInterface());
Matthew Barth1a5c6232022-02-14 14:28:41 -0600681 }
Matthew Barth55627ad2021-12-02 22:28:29 -0600682
Matthew Barth2f359f72022-02-15 10:00:26 -0600683 if (!service.empty())
Matthew Barth1a5c6232022-02-14 14:28:41 -0600684 {
Matthew Barth2f359f72022-02-15 10:00:26 -0600685 if (lastServ != service)
Matthew Barth1a5c6232022-02-14 14:28:41 -0600686 {
Matthew Barth2f359f72022-02-15 10:00:26 -0600687 objMgrPaths = getPaths(
688 service, "org.freedesktop.DBus.ObjectManager");
689 lastServ = service;
690 }
Matthew Barth1a5c6232022-02-14 14:28:41 -0600691
Matthew Barth2f359f72022-02-15 10:00:26 -0600692 // Look for the ObjectManager as an ancestor from the
693 // member.
Patrick Williams61b73292023-05-10 07:50:12 -0500694 auto hasObjMgr = std::any_of(objMgrPaths.begin(),
695 objMgrPaths.end(),
696 [&member](const auto& path) {
697 return member.find(path) != std::string::npos;
698 });
Matthew Barth2f359f72022-02-15 10:00:26 -0600699
700 if (!hasObjMgr)
701 {
702 // No object manager interface provided for group member
703 // Attempt to retrieve group member property directly
Matt Spinlerf16f0632022-05-09 14:27:46 -0500704 try
705 {
706 auto value = util::SDBusPlus::getPropertyVariant<
707 PropertyVariantType>(_bus, service, member,
708 group.getInterface(),
709 group.getProperty());
710 setProperty(member, group.getInterface(),
711 group.getProperty(), value);
712 }
713 catch (const std::exception& e)
714 {}
Matthew Barth2f359f72022-02-15 10:00:26 -0600715 continue;
716 }
717
718 if (services.find(service) == services.end())
719 {
720 services.insert(service);
721 for (const auto& objMgrPath : objMgrPaths)
722 {
723 // Get all managed objects from the service
724 auto objects = util::SDBusPlus::getManagedObjects<
725 PropertyVariantType>(_bus, service, objMgrPath);
726
727 // Insert objects into cache
728 insertFilteredObjects(objects);
729 }
Matthew Barth1a5c6232022-02-14 14:28:41 -0600730 }
Matthew Barth55627ad2021-12-02 22:28:29 -0600731 }
732 }
Matthew Barth2f359f72022-02-15 10:00:26 -0600733 catch (const util::DBusError&)
734 {
735 // No service or property found for group member with the
736 // group's configured interface
737 continue;
738 }
Matt Spinlerade0c372021-10-28 16:09:44 -0500739 }
740 }
741}
742
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500743void Manager::timerExpired(TimerData& data)
744{
Matt Spinlerade0c372021-10-28 16:09:44 -0500745 if (std::get<bool>(data.second))
746 {
Matthew Barth2f359f72022-02-15 10:00:26 -0600747 addGroups(std::get<const std::vector<Group>&>(data.second));
Matt Spinlerade0c372021-10-28 16:09:44 -0500748 }
749
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500750 auto& actions =
751 std::get<std::vector<std::unique_ptr<ActionBase>>&>(data.second);
752 // Perform the actions in the timer data
753 std::for_each(actions.begin(), actions.end(),
Matthew Barth00f6aa02021-04-09 10:49:47 -0500754 [](auto& action) { action->run(); });
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500755
756 // Remove oneshot timers after they expired
757 if (data.first == TimerType::oneshot)
758 {
Patrick Williams61b73292023-05-10 07:50:12 -0500759 auto itTimer = std::find_if(_timers.begin(), _timers.end(),
760 [&data](const auto& timer) {
761 return (data.first == timer.first->first &&
762 (std::get<std::string>(data.second) ==
763 std::get<std::string>(timer.first->second)));
764 });
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500765 if (itTimer != std::end(_timers))
766 {
767 _timers.erase(itTimer);
768 }
769 }
770}
771
Patrick Williamscb356d42022-07-22 19:26:53 -0500772void Manager::handleSignal(sdbusplus::message_t& msg,
Matthew Barthc024d782021-11-09 16:15:49 -0600773 const std::vector<SignalPkg>* pkgs)
Matthew Barthebabc042021-05-13 15:38:58 -0500774{
Matthew Barthc024d782021-11-09 16:15:49 -0600775 for (auto& pkg : *pkgs)
Matthew Barthebabc042021-05-13 15:38:58 -0500776 {
777 // Handle the signal callback and only run the actions if the handler
778 // updated the cache for the given SignalObject
779 if (std::get<SignalHandler>(pkg)(msg, std::get<SignalObject>(pkg),
780 *this))
781 {
782 // Perform the actions in the handler package
Matt Spinlerd0ba86a2021-11-09 10:09:13 -0600783 auto& actions = std::get<TriggerActions>(pkg);
Matthew Barthc3a69082021-11-15 14:32:48 -0600784 std::for_each(actions.begin(), actions.end(), [](auto& action) {
785 if (action.get())
786 {
787 action.get()->run();
788 }
789 });
Matthew Barthebabc042021-05-13 15:38:58 -0500790 }
Matthew Barthc024d782021-11-09 16:15:49 -0600791 // Only rewind message when not last package
792 if (&pkg != &pkgs->back())
793 {
794 sd_bus_message_rewind(msg.get(), true);
795 }
Matthew Barthebabc042021-05-13 15:38:58 -0500796 }
797}
798
Matthew Barthacd737c2021-03-04 11:04:01 -0600799void Manager::setProfiles()
800{
801 // Profiles JSON config file is optional
Patrick Williams61b73292023-05-10 07:50:12 -0500802 auto confFile = fan::JsonConfig::getConfFile(confAppName,
803 Profile::confFileName, true);
Matthew Barthe91ac862021-05-25 16:22:17 -0500804
805 _profiles.clear();
Matthew Barthacd737c2021-03-04 11:04:01 -0600806 if (!confFile.empty())
807 {
808 for (const auto& entry : fan::JsonConfig::load(confFile))
809 {
810 auto obj = std::make_unique<Profile>(entry);
811 _profiles.emplace(
812 std::make_pair(obj->getName(), obj->getProfiles()),
813 std::move(obj));
814 }
815 }
Matthew Barthe91ac862021-05-25 16:22:17 -0500816
Matthew Barthacd737c2021-03-04 11:04:01 -0600817 // Ensure all configurations use the same set of active profiles
818 // (In case a profile's active state changes during configuration)
Matthew Barthe91ac862021-05-25 16:22:17 -0500819 _activeProfiles.clear();
Matthew Barthacd737c2021-03-04 11:04:01 -0600820 for (const auto& profile : _profiles)
821 {
822 if (profile.second->isActive())
823 {
824 _activeProfiles.emplace_back(profile.first.first);
825 }
826 }
827}
828
Matt Spinlerd0ba86a2021-11-09 10:09:13 -0600829void Manager::addParameterTrigger(
830 const std::string& name, std::vector<std::unique_ptr<ActionBase>>& actions)
831{
832 auto it = _parameterTriggers.find(name);
833 if (it != _parameterTriggers.end())
834 {
835 std::for_each(actions.begin(), actions.end(),
836 [&actList = it->second](auto& action) {
Patrick Williams61b73292023-05-10 07:50:12 -0500837 actList.emplace_back(std::ref(action));
838 });
Matt Spinlerd0ba86a2021-11-09 10:09:13 -0600839 }
840 else
841 {
842 TriggerActions triggerActions;
843 std::for_each(actions.begin(), actions.end(),
844 [&triggerActions](auto& action) {
Patrick Williams61b73292023-05-10 07:50:12 -0500845 triggerActions.emplace_back(std::ref(action));
846 });
Matt Spinlerd0ba86a2021-11-09 10:09:13 -0600847 _parameterTriggers[name] = std::move(triggerActions);
848 }
849}
850
851void Manager::runParameterActions(const std::string& name)
852{
853 auto it = _parameterTriggers.find(name);
854 if (it != _parameterTriggers.end())
855 {
856 std::for_each(it->second.begin(), it->second.end(),
857 [](auto& action) { action.get()->run(); });
858 }
859}
860
Matthew Bartha227a162020-08-05 10:51:45 -0500861} // namespace phosphor::fan::control::json