blob: 2f661817f8779fd27501a3e39505c6e54d8d337c [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 */
16#pragma once
17
Matthew Barthd9cb63b2021-03-24 14:36:49 -050018#include "action.hpp"
Matthew Barth44ab7692021-03-26 11:40:10 -050019#include "event.hpp"
Matthew Barthd9cb63b2021-03-24 14:36:49 -050020#include "group.hpp"
Matthew Barthacd737c2021-03-04 11:04:01 -060021#include "json_config.hpp"
Matthew Barth48f44da2021-05-27 15:43:34 -050022#include "power_state.hpp"
Matthew Barth06764942021-03-04 09:28:40 -060023#include "profile.hpp"
Matthew Barth9403a212021-05-17 09:31:50 -050024#include "sdbusplus.hpp"
Matthew Barthbd52ed02022-02-07 15:15:10 -060025#include "utils/flight_recorder.hpp"
Matthew Barthacd737c2021-03-04 11:04:01 -060026#include "zone.hpp"
Matthew Barth06764942021-03-04 09:28:40 -060027
Matthew Bartha227a162020-08-05 10:51:45 -050028#include <nlohmann/json.hpp>
Matthew Barth68ac0042021-06-01 15:38:36 -050029#include <phosphor-logging/log.hpp>
Matthew Bartha227a162020-08-05 10:51:45 -050030#include <sdbusplus/bus.hpp>
Matthew Barth1542fb52021-06-10 14:09:09 -050031#include <sdbusplus/server/manager.hpp>
Matthew Barth06764942021-03-04 09:28:40 -060032#include <sdeventplus/event.hpp>
Matt Spinler2fc0a352021-10-04 15:10:57 -050033#include <sdeventplus/source/event.hpp>
Matthew Barthd9cb63b2021-03-24 14:36:49 -050034#include <sdeventplus/utility/timer.hpp>
35
36#include <chrono>
Patrick Williamsfbf47032023-07-17 12:27:34 -050037#include <format>
Matthew Barthd9cb63b2021-03-24 14:36:49 -050038#include <map>
39#include <memory>
40#include <optional>
41#include <tuple>
42#include <utility>
43#include <vector>
Matthew Bartha227a162020-08-05 10:51:45 -050044
45namespace phosphor::fan::control::json
46{
47
48using json = nlohmann::json;
Matthew Barth68ac0042021-06-01 15:38:36 -050049using namespace phosphor::logging;
Matthew Bartha227a162020-08-05 10:51:45 -050050
Matthew Barthacd737c2021-03-04 11:04:01 -060051/* Application name to be appended to the path for loading a JSON config file */
52constexpr auto confAppName = "control";
53
Matthew Barthd9cb63b2021-03-24 14:36:49 -050054/* Type of timers supported */
55enum class TimerType
56{
57 oneshot,
58 repeating,
59};
Mike Cappsb2e9a4f2022-06-13 10:15:42 -040060
Matthew Barthd9cb63b2021-03-24 14:36:49 -050061/**
62 * Package of data required when a timer expires
63 * Tuple constructed of:
64 * std::string = Timer package unique identifier
Matthew Barthd9cb63b2021-03-24 14:36:49 -050065 * std::vector<std::unique_ptr<ActionBase>> = List of pointers to actions
66 * that run when the timer expires
Matt Spinlerade0c372021-10-28 16:09:44 -050067 * const std::vector<Group> = List of groups
68 * bool = If groups should be preloaded before actions are run
Matthew Barthd9cb63b2021-03-24 14:36:49 -050069 */
Matthew Barth00f6aa02021-04-09 10:49:47 -050070using TimerPkg =
Matt Spinlerade0c372021-10-28 16:09:44 -050071 std::tuple<std::string, std::vector<std::unique_ptr<ActionBase>>&,
72 const std::vector<Group>&, bool>;
Matthew Barthd9cb63b2021-03-24 14:36:49 -050073/**
74 * Data associated with a running timer that's used when it expires
75 * Pair constructed of:
76 * TimerType = Type of timer to manage expired timer instances
77 * TimerPkg = Package of data required when the timer expires
78 */
79using TimerData = std::pair<TimerType, TimerPkg>;
80/* Dbus event timer */
81using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>;
82
Matthew Barthebabc042021-05-13 15:38:58 -050083/* Dbus signal object */
84constexpr auto Path = 0;
85constexpr auto Intf = 1;
86constexpr auto Prop = 2;
87using SignalObject = std::tuple<std::string, std::string, std::string>;
88/* Dbus signal actions */
Matt Spinlerd0ba86a2021-11-09 10:09:13 -060089using TriggerActions =
Matthew Barthc3a69082021-11-15 14:32:48 -060090 std::vector<std::reference_wrapper<std::unique_ptr<ActionBase>>>;
Matthew Barthebabc042021-05-13 15:38:58 -050091/**
92 * Signal handler function that handles parsing a signal's message for a
93 * particular signal object and stores the results in the manager
94 */
Patrick Williamscb356d42022-07-22 19:26:53 -050095using SignalHandler =
96 std::function<bool(sdbusplus::message_t&, const SignalObject&, Manager&)>;
Matthew Barthebabc042021-05-13 15:38:58 -050097/**
98 * Package of data required when a signal is received
99 * Tuple constructed of:
100 * SignalHandler = Signal handler function
101 * SignalObject = Dbus signal object
Matt Spinlerd0ba86a2021-11-09 10:09:13 -0600102 * TriggerActions = List of actions that are run when the signal is received
Matthew Barthebabc042021-05-13 15:38:58 -0500103 */
Matt Spinlerd0ba86a2021-11-09 10:09:13 -0600104using SignalPkg = std::tuple<SignalHandler, SignalObject, TriggerActions>;
Matthew Barthebabc042021-05-13 15:38:58 -0500105/**
106 * Data associated to a subscribed signal
107 * Tuple constructed of:
Matthew Barthc024d782021-11-09 16:15:49 -0600108 * std::unique_ptr<std::vector<SignalPkg>> =
109 * Pointer to list of the signal's packages
Patrick Williams3ea9ec22021-11-19 12:21:08 -0600110 * std::unique_ptr<sdbusplus::bus::match_t> =
Matthew Barthebabc042021-05-13 15:38:58 -0500111 * Pointer to match holding the subscription to a signal
112 */
Matthew Barthc024d782021-11-09 16:15:49 -0600113using SignalData = std::tuple<std::unique_ptr<std::vector<SignalPkg>>,
Patrick Williams3ea9ec22021-11-19 12:21:08 -0600114 std::unique_ptr<sdbusplus::bus::match_t>>;
Matthew Barthebabc042021-05-13 15:38:58 -0500115
Matthew Bartha227a162020-08-05 10:51:45 -0500116/**
Mike Capps1a19ead2021-10-22 09:15:14 -0400117 * Package of data from a D-Bus call to get managed objects
118 * Tuple constructed of:
119 * std::map<Path, // D-Bus Path
120 * std::map<Intf, // D-Bus Interface
121 * std::map<Property, // D-Bus Property
122 * std::variant>>> // Variant value of that property
123 */
124using Path_v = sdbusplus::message::object_path;
125using Intf_v = std::string;
126using Prop_v = std::string;
127using ManagedObjects =
128 std::map<Path_v, std::map<Intf_v, std::map<Prop_v, PropertyVariantType>>>;
129
130/**
Matt Spinlerd0ba86a2021-11-09 10:09:13 -0600131 * Actions to run when a parameter trigger runs.
132 */
133using ParamTriggerData = std::vector<
134 std::reference_wrapper<const std::vector<std::unique_ptr<ActionBase>>>>;
135
136/**
Matthew Bartha227a162020-08-05 10:51:45 -0500137 * @class Manager - Represents the fan control manager's configuration
138 *
139 * A fan control manager configuration is optional, therefore the "manager.json"
140 * file is also optional. The manager configuration is used to populate
141 * fan control's manager parameters which are used in how the application
142 * operates, not in how the fans are controlled.
143 *
144 * When no manager configuration exists, the fan control application starts,
145 * processes any configured events and then begins controlling fans according
146 * to those events.
147 */
148class Manager
149{
150 public:
151 Manager() = delete;
152 Manager(const Manager&) = delete;
153 Manager(Manager&&) = delete;
154 Manager& operator=(const Manager&) = delete;
155 Manager& operator=(Manager&&) = delete;
156 ~Manager() = default;
157
158 /**
159 * Constructor
160 * Parses and populates the fan control manager attributes from a json file
161 *
Matthew Barth06764942021-03-04 09:28:40 -0600162 * @param[in] event - sdeventplus event loop
Matthew Bartha227a162020-08-05 10:51:45 -0500163 */
Matt Spinler9b062432023-01-26 14:38:50 -0600164 explicit Manager(const sdeventplus::Event& event);
Matthew Bartha227a162020-08-05 10:51:45 -0500165
166 /**
Matthew Barthe91ac862021-05-25 16:22:17 -0500167 * @brief Callback function to handle receiving a HUP signal to reload the
168 * JSON configurations.
169 */
170 void sighupHandler(sdeventplus::source::Signal&,
171 const struct signalfd_siginfo*);
172
173 /**
Matt Spinler2fc0a352021-10-04 15:10:57 -0500174 * @brief Callback function to handle receiving a USR1 signal to dump
175 * the flight recorder.
176 */
Matt Spinler27f5f4e2022-09-01 14:57:39 -0500177 void dumpDebugData(sdeventplus::source::Signal&,
178 const struct signalfd_siginfo*);
Matt Spinler2fc0a352021-10-04 15:10:57 -0500179
180 /**
Matthew Barthacd737c2021-03-04 11:04:01 -0600181 * @brief Get the active profiles of the system where an empty list
182 * represents that only configuration entries without a profile defined will
183 * be loaded.
184 *
185 * @return - The list of active profiles
186 */
187 static const std::vector<std::string>& getActiveProfiles();
188
189 /**
190 * @brief Load the configuration of a given JSON class object based on the
191 * active profiles
192 *
Matthew Barthacd737c2021-03-04 11:04:01 -0600193 * @param[in] isOptional - JSON configuration file is optional or not
Matthew Barth603ef162021-03-24 15:34:53 -0500194 * @param[in] args - Arguments to be forwarded to each instance of `T`
Matthew Barthe6d1f782021-05-14 12:52:20 -0500195 * (*Note that a sdbusplus bus object is required as the first argument)
Matthew Barthacd737c2021-03-04 11:04:01 -0600196 *
197 * @return Map of configuration entries
198 * Map of configuration keys to their corresponding configuration object
199 */
Matthew Barth603ef162021-03-24 15:34:53 -0500200 template <typename T, typename... Args>
Patrick Williamsdfddd642024-08-16 15:21:51 -0400201 static std::map<configKey, std::unique_ptr<T>>
202 getConfig(bool isOptional, Args&&... args)
Matthew Barthacd737c2021-03-04 11:04:01 -0600203 {
204 std::map<configKey, std::unique_ptr<T>> config;
205
Mike Capps808d7fe2022-06-13 10:12:16 -0400206 auto confFile = fan::JsonConfig::getConfFile(
207 confAppName, T::confFileName, isOptional);
208
Matthew Barthacd737c2021-03-04 11:04:01 -0600209 if (!confFile.empty())
210 {
Matthew Barthbd52ed02022-02-07 15:15:10 -0600211 FlightRecorder::instance().log(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500212 "main", std::format("Loading configuration from {}",
Matthew Barthbd52ed02022-02-07 15:15:10 -0600213 confFile.string()));
Matthew Barthacd737c2021-03-04 11:04:01 -0600214 for (const auto& entry : fan::JsonConfig::load(confFile))
215 {
216 if (entry.contains("profiles"))
217 {
218 std::vector<std::string> profiles;
219 for (const auto& profile : entry["profiles"])
220 {
221 profiles.emplace_back(
222 profile.template get<std::string>());
223 }
224 // Do not create the object if its profiles are not in the
225 // list of active profiles
Matthew Barth42428052021-03-30 12:50:59 -0500226 if (!profiles.empty() &&
227 !std::any_of(profiles.begin(), profiles.end(),
Matthew Barthacd737c2021-03-04 11:04:01 -0600228 [](const auto& name) {
Patrick Williamsdfddd642024-08-16 15:21:51 -0400229 return std::find(
230 getActiveProfiles().begin(),
231 getActiveProfiles().end(),
232 name) !=
233 getActiveProfiles().end();
234 }))
Matthew Barthacd737c2021-03-04 11:04:01 -0600235 {
236 continue;
237 }
238 }
Patrick Williamsdfddd642024-08-16 15:21:51 -0400239 auto obj =
240 std::make_unique<T>(entry, std::forward<Args>(args)...);
Matthew Barthacd737c2021-03-04 11:04:01 -0600241 config.emplace(
242 std::make_pair(obj->getName(), obj->getProfiles()),
243 std::move(obj));
244 }
Matthew Barth68ac0042021-06-01 15:38:36 -0500245 log<level::INFO>(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500246 std::format("Configuration({}) loaded successfully",
Matthew Barth68ac0042021-06-01 15:38:36 -0500247 T::confFileName)
248 .c_str());
Matthew Barthbd52ed02022-02-07 15:15:10 -0600249 FlightRecorder::instance().log(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500250 "main", std::format("Configuration({}) loaded successfully",
Matthew Barthbd52ed02022-02-07 15:15:10 -0600251 T::confFileName));
Matthew Barthacd737c2021-03-04 11:04:01 -0600252 }
253 return config;
254 }
255
256 /**
Matthew Barth0206c722021-03-30 15:20:05 -0500257 * @brief Check if the given input configuration key matches with another
258 * configuration key that it's to be included in
259 *
260 * @param[in] input - Config key to be included in another config object
261 * @param[in] comp - Config key of the config object to compare with
262 *
263 * @return Whether the configuration object should be included
264 */
265 static bool inConfig(const configKey& input, const configKey& comp);
266
267 /**
Matthew Barth12cb1252021-03-08 16:47:30 -0600268 * @brief Check if the given path and inteface is owned by a dbus service
269 *
270 * @param[in] path - Dbus object path
271 * @param[in] intf - Dbus object interface
272 *
273 * @return - Whether the service has an owner for the given object path and
274 * interface
275 */
276 static bool hasOwner(const std::string& path, const std::string& intf);
277
278 /**
Matthew Barth6d8e2d32022-02-01 16:47:08 -0600279 * @brief Sets the dbus service owner state for all entries in the _servTree
280 * cache and removes associated objects from the _objects cache
281 *
282 * @param[in] serv - Dbus service name
283 * @param[in] hasOwner - Dbus service owner state
284 */
285 void setOwner(const std::string& serv, bool hasOwner);
286
287 /**
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500288 * @brief Sets the dbus service owner state of a given object
289 *
290 * @param[in] path - Dbus object path
291 * @param[in] serv - Dbus service name
292 * @param[in] intf - Dbus object interface
293 * @param[in] isOwned - Dbus service owner state
294 */
295 void setOwner(const std::string& path, const std::string& serv,
296 const std::string& intf, bool isOwned);
297
298 /**
299 * @brief Add a set of services for a path and interface by retrieving all
300 * the path subtrees to the given depth from root for the interface
301 *
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500302 * @param[in] intf - Interface to add services for
303 * @param[in] depth - Depth of tree traversal from root path
304 *
305 * @throws - DBusMethodError
306 * Throws a DBusMethodError when the `getSubTree` method call fails
307 */
Matthew Barth98f6fc12021-04-16 10:48:37 -0500308 static void addServices(const std::string& intf, int32_t depth);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500309
310 /**
311 * @brief Get the service for a given path and interface from cached
312 * dataset and attempt to add all the services for the given path/interface
313 * when it's not found
314 *
315 * @param[in] path - Path to get service for
316 * @param[in] intf - Interface to get service for
317 *
318 * @return - The now cached service name
319 *
320 * @throws - DBusMethodError
321 * Ripples up a DBusMethodError exception from calling addServices
322 */
323 static const std::string& getService(const std::string& path,
324 const std::string& intf);
325
326 /**
Matthew Barthf41e9472021-05-13 16:38:06 -0500327 * @brief Get all the object paths for a given service and interface from
328 * the cached dataset and try to add all the services for the given
329 * interface when no paths are found and then attempt to get all the object
330 * paths again
331 *
332 * @param[in] serv - Service name to get paths for
333 * @param[in] intf - Interface to get paths for
334 *
335 * @return The cached object paths
336 */
337 std::vector<std::string> getPaths(const std::string& serv,
338 const std::string& intf);
339
340 /**
341 * @brief Add objects to the cached dataset by first using
342 * `getManagedObjects` for the same service providing the given path and
343 * interface or just add the single object of the given path, interface, and
344 * property if that fails.
345 *
346 * @param[in] path - Dbus object's path
347 * @param[in] intf - Dbus object's interface
348 * @param[in] prop - Dbus object's property
349 *
350 * @throws - DBusMethodError
351 * Throws a DBusMethodError when the the service is failed to be found or
352 * when the `getManagedObjects` method call fails
353 */
354 void addObjects(const std::string& path, const std::string& intf,
Matt Spinler9ac325c2022-04-25 14:13:49 -0500355 const std::string& prop)
356 {
357 addObjects(path, intf, prop, std::string{});
358 }
359
360 /**
361 * @copydoc Manager::addObjects()
362 *
363 * If the service is known, then it can be used to add all objects
364 * in that service with the interface passed in to the cache instead of
365 * having to look it up. This is done so objects can still be
366 * added even when the D-Bus path passed in doesn't exist so it
367 * can't be used to get a service name.
368 *
369 * @param[in] path - Dbus object's path
370 * @param[in] intf - Dbus object's interface
371 * @param[in] prop - Dbus object's property
372 * @param[in] serviceName - The service of the path/intf/prop if known
373 */
374 void addObjects(const std::string& path, const std::string& intf,
375 const std::string& prop, const std::string& serviceName);
Matthew Barthf41e9472021-05-13 16:38:06 -0500376
377 /**
378 * @brief Get an object's property value
379 *
380 * @param[in] path - Dbus object's path
381 * @param[in] intf - Dbus object's interface
382 * @param[in] prop - Dbus object's property
383 */
384 const std::optional<PropertyVariantType>
385 getProperty(const std::string& path, const std::string& intf,
386 const std::string& prop);
387
388 /**
Matthew Barthbaeeb8f2021-05-13 16:03:54 -0500389 * @brief Set/update an object's property value
390 *
391 * @param[in] path - Dbus object's path
392 * @param[in] intf - Dbus object's interface
393 * @param[in] prop - Dbus object's property
394 * @param[in] value - Dbus object's property value
395 */
396 void setProperty(const std::string& path, const std::string& intf,
Mike Capps1a19ead2021-10-22 09:15:14 -0400397 const std::string& prop, PropertyVariantType value);
Matthew Barthbaeeb8f2021-05-13 16:03:54 -0500398
399 /**
Matthew Barthc2691402021-05-13 16:20:32 -0500400 * @brief Remove an object's interface
401 *
402 * @param[in] path - Dbus object's path
403 * @param[in] intf - Dbus object's interface
404 */
405 inline void removeInterface(const std::string& path,
406 const std::string& intf)
407 {
408 auto itPath = _objects.find(path);
409 if (itPath != std::end(_objects))
410 {
411 _objects[path].erase(intf);
412 }
413 }
414
415 /**
Matthew Barth07fecfc2021-01-29 09:04:43 -0600416 * @brief Get the object's property value as a variant
417 *
418 * @param[in] path - Path of the object containing the property
419 * @param[in] intf - Interface name containing the property
420 * @param[in] prop - Name of property
421 *
422 * @return - The object's property value as a variant
423 */
424 static inline auto getObjValueVariant(const std::string& path,
425 const std::string& intf,
426 const std::string& prop)
427 {
428 return _objects.at(path).at(intf).at(prop);
429 };
430
431 /**
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500432 * @brief Add a dbus timer
433 *
434 * @param[in] type - Type of timer
435 * @param[in] interval - Timer interval in microseconds
436 * @param[in] pkg - Packaged data for when timer expires
437 */
438 void addTimer(const TimerType type,
439 const std::chrono::microseconds interval,
440 std::unique_ptr<TimerPkg> pkg);
441
442 /**
443 * @brief Callback when a timer expires
444 *
445 * @param[in] data - Data to be used when the timer expired
446 */
447 void timerExpired(TimerData& data);
448
449 /**
Matthew Barthebabc042021-05-13 15:38:58 -0500450 * @brief Get the signal data for a given match string
451 *
452 * @param[in] sigMatch - Signal match string
453 *
454 * @return - Reference to the signal data for the given match string
455 */
456 std::vector<SignalData>& getSignal(const std::string& sigMatch)
457 {
458 return _signals[sigMatch];
459 }
460
461 /**
462 * @brief Handle receiving signals
463 *
464 * @param[in] msg - Signal message containing the signal's data
465 * @param[in] pkgs - Signal packages associated to the signal being handled
466 */
Patrick Williamscb356d42022-07-22 19:26:53 -0500467 void handleSignal(sdbusplus::message_t& msg,
Matthew Barthc024d782021-11-09 16:15:49 -0600468 const std::vector<SignalPkg>* pkgs);
Matthew Barthebabc042021-05-13 15:38:58 -0500469
470 /**
Matthew Bartheebde062021-04-14 12:48:52 -0500471 * @brief Get the sdbusplus bus object
472 */
473 inline auto& getBus()
474 {
475 return _bus;
476 }
477
478 /**
Matthew Barth48f44da2021-05-27 15:43:34 -0500479 * @brief Is the power state on
480 *
481 * @return Current power state of the system
482 */
483 inline bool isPowerOn() const
484 {
485 return _powerState->isPowerOn();
486 }
487
Matthew Barth3770a1d2021-06-10 15:09:37 -0500488 /**
489 * @brief Load all the fan control JSON configuration files
490 *
491 * This is where all the fan control JSON configuration files are parsed and
492 * loaded into their associated objects. Anything that needs to be done when
493 * the Manager object is constructed or handling a SIGHUP to reload the
494 * configurations needs to be done here.
495 */
496 void load();
497
Matt Spinlerd76351b2021-08-05 16:23:09 -0500498 /**
499 * @brief Sets a value in the parameter map.
500 *
Matt Spinler72c4af42021-11-29 14:40:17 -0600501 * If it's a std::nullopt, it will be deleted instead.
502 *
Matt Spinlerd76351b2021-08-05 16:23:09 -0500503 * @param[in] name - The parameter name
504 * @param[in] value - The parameter value
505 */
506 static void setParameter(const std::string& name,
Matt Spinler72c4af42021-11-29 14:40:17 -0600507 const std::optional<PropertyVariantType>& value)
Matt Spinlerd76351b2021-08-05 16:23:09 -0500508 {
Matt Spinler72c4af42021-11-29 14:40:17 -0600509 if (value)
Matt Spinlerd0ba86a2021-11-09 10:09:13 -0600510 {
Matt Spinler72c4af42021-11-29 14:40:17 -0600511 auto it = _parameters.find(name);
512 auto changed = (it == _parameters.end()) ||
513 ((it != _parameters.end()) && it->second != *value);
514 _parameters[name] = *value;
515
516 if (changed)
517 {
518 runParameterActions(name);
519 }
520 }
521 else
522 {
523 size_t deleted = _parameters.erase(name);
524
525 if (deleted)
526 {
527 runParameterActions(name);
528 }
Matt Spinlerd0ba86a2021-11-09 10:09:13 -0600529 }
Matt Spinlerd76351b2021-08-05 16:23:09 -0500530 }
531
532 /**
533 * @brief Returns a value from the parameter map
534 *
535 * @param[in] name - The parameter name
536 *
537 * @return The parameter value, or std::nullopt if not found
538 */
539 static std::optional<PropertyVariantType>
540 getParameter(const std::string& name)
541 {
542 auto it = _parameters.find(name);
543 if (it != _parameters.end())
544 {
545 return it->second;
546 }
547
548 return std::nullopt;
549 }
550
551 /**
Matt Spinlerd0ba86a2021-11-09 10:09:13 -0600552 * @brief Runs the actions registered to a parameter
553 * trigger with this name.
554 *
555 * @param[in] name - The parameter name
556 */
557 static void runParameterActions(const std::string& name);
558
559 /**
560 * @brief Adds a parameter trigger
561 *
562 * @param[in] name - The parameter name
563 * @param[in] actions - The actions to run on the trigger
564 */
565 static void
566 addParameterTrigger(const std::string& name,
567 std::vector<std::unique_ptr<ActionBase>>& actions);
568
Matt Spinler7787def2021-10-14 16:33:16 -0500569 /* The name of the dump file */
570 static const std::string dumpFile;
571
Matthew Bartha227a162020-08-05 10:51:45 -0500572 private:
Mike Capps1a19ead2021-10-22 09:15:14 -0400573 /**
574 * @brief Helper to detect when a property's double contains a NaN
575 * (not-a-number) value.
576 *
577 * @param[in] value - The property to test
578 */
579 static bool PropertyContainsNan(const PropertyVariantType& value)
580 {
581 return (std::holds_alternative<double>(value) &&
582 std::isnan(std::get<double>(value)));
583 }
584
585 /**
586 * @brief Insert managed objects into cache, but filter out properties
Matt Spinlerc2c2db72022-04-07 13:59:37 -0500587 * containing unwanted NaN (not-a-number) values and properties that
588 * are on D-Bus paths that aren't in an existing Group object.
Mike Capps1a19ead2021-10-22 09:15:14 -0400589 *
590 * @param[in] ref - The map of ManagedObjects to insert into cache
591 */
592 void insertFilteredObjects(ManagedObjects& ref);
593
Matthew Barth1542fb52021-06-10 14:09:09 -0500594 /* The sdbusplus bus object to use */
Patrick Williamscb356d42022-07-22 19:26:53 -0500595 sdbusplus::bus_t& _bus;
Matthew Barthacd737c2021-03-04 11:04:01 -0600596
Matthew Barth1542fb52021-06-10 14:09:09 -0500597 /* The sdeventplus even loop to use */
Matthew Barthacd737c2021-03-04 11:04:01 -0600598 sdeventplus::Event _event;
599
Matthew Barth1542fb52021-06-10 14:09:09 -0500600 /* The sdbusplus manager object to set the ObjectManager interface */
Patrick Williamscb356d42022-07-22 19:26:53 -0500601 sdbusplus::server::manager_t _mgr;
Matthew Barth1542fb52021-06-10 14:09:09 -0500602
Matthew Barth3770a1d2021-06-10 15:09:37 -0500603 /* Whether loading the config files is allowed or not */
604 bool _loadAllowed;
605
Matthew Barth48f44da2021-05-27 15:43:34 -0500606 /* The system's power state determination object */
607 std::unique_ptr<PowerState> _powerState;
608
Matthew Barth06764942021-03-04 09:28:40 -0600609 /* List of profiles configured */
Matthew Barthacd737c2021-03-04 11:04:01 -0600610 std::map<configKey, std::unique_ptr<Profile>> _profiles;
Matthew Barth06764942021-03-04 09:28:40 -0600611
612 /* List of active profiles */
Matthew Barthacd737c2021-03-04 11:04:01 -0600613 static std::vector<std::string> _activeProfiles;
614
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500615 /* Subtree map of paths to services of interfaces(with ownership state) */
616 static std::map<
617 std::string,
618 std::map<std::string, std::pair<bool, std::vector<std::string>>>>
Matthew Barth12cb1252021-03-08 16:47:30 -0600619 _servTree;
620
Matthew Barth07fecfc2021-01-29 09:04:43 -0600621 /* Object map of paths to interfaces of properties and their values */
622 static std::map<
623 std::string,
624 std::map<std::string, std::map<std::string, PropertyVariantType>>>
625 _objects;
626
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500627 /* List of timers and their data to be processed when expired */
628 std::vector<std::pair<std::unique_ptr<TimerData>, Timer>> _timers;
629
Matthew Barthebabc042021-05-13 15:38:58 -0500630 /* Map of signal match strings to a list of signal handler data */
631 std::unordered_map<std::string, std::vector<SignalData>> _signals;
632
Matthew Barthacd737c2021-03-04 11:04:01 -0600633 /* List of zones configured */
634 std::map<configKey, std::unique_ptr<Zone>> _zones;
635
Matthew Barth44ab7692021-03-26 11:40:10 -0500636 /* List of events configured */
637 std::map<configKey, std::unique_ptr<Event>> _events;
638
Matthew Barthacd737c2021-03-04 11:04:01 -0600639 /**
Matt Spinlerd76351b2021-08-05 16:23:09 -0500640 * @brief A map of parameter names and values that are something
641 * other than just D-Bus property values that other actions
642 * can set and use.
643 */
644 static std::unordered_map<std::string, PropertyVariantType> _parameters;
645
646 /**
Matt Spinlerd0ba86a2021-11-09 10:09:13 -0600647 * @brief Map of parameter names to the actions to run when their
648 * values change.
649 */
650 static std::unordered_map<std::string, TriggerActions> _parameterTriggers;
651
652 /**
Matthew Barthb2cd93f2021-06-16 16:37:40 -0500653 * @brief Callback for power state changes
654 *
655 * @param[in] powerStateOn - Whether the power state is on or not
656 *
657 * Callback function bound to the PowerState object instance to handle each
658 * time the power state changes.
659 */
660 void powerStateChanged(bool powerStateOn);
661
662 /**
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500663 * @brief Find the service name for a given path and interface from the
664 * cached dataset
665 *
666 * @param[in] path - Path to get service for
667 * @param[in] intf - Interface to get service for
668 *
669 * @return - The cached service name
670 */
671 static const std::string& findService(const std::string& path,
672 const std::string& intf);
673
674 /**
Matthew Barthf41e9472021-05-13 16:38:06 -0500675 * @brief Find all the paths for a given service and interface from the
676 * cached dataset
677 *
678 * @param[in] serv - Service name to get paths for
679 * @param[in] intf - Interface to get paths for
680 *
681 * @return - The cached object paths
682 */
683 std::vector<std::string> findPaths(const std::string& serv,
684 const std::string& intf);
685
686 /**
Matthew Barthacd737c2021-03-04 11:04:01 -0600687 * @brief Parse and set the configured profiles from the profiles JSON file
688 *
689 * Retrieves the optional profiles JSON configuration file, parses it, and
690 * creates a list of configured profiles available to the other
691 * configuration files. These profiles can be used to remove or include
692 * entries within the other configuration files.
693 */
694 void setProfiles();
Matt Spinler2fc0a352021-10-04 15:10:57 -0500695
696 /**
Matt Spinlerb5c21a22021-10-14 16:52:12 -0500697 * @brief Dump the _objects, _servTree, and _parameters maps to JSON
698 *
699 * @param[out] data - The JSON that will be filled in
700 */
701 void dumpCache(json& data);
Matt Spinlerade0c372021-10-28 16:09:44 -0500702
703 /**
Matthew Barth2f359f72022-02-15 10:00:26 -0600704 * @brief Add a list of groups to the cache dataset.
Matt Spinlerade0c372021-10-28 16:09:44 -0500705 *
Matthew Barth2f359f72022-02-15 10:00:26 -0600706 * @param[in] groups - The groups to add
Matt Spinlerade0c372021-10-28 16:09:44 -0500707 */
Matthew Barth2f359f72022-02-15 10:00:26 -0600708 void addGroups(const std::vector<Group>& groups);
Matthew Bartha227a162020-08-05 10:51:45 -0500709};
710
711} // namespace phosphor::fan::control::json