blob: ad9272032622a52ec4fe563d557a7b54194b089b [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 Barth68ac0042021-06-01 15:38:36 -050028#include <fmt/format.h>
29
Matthew Bartha227a162020-08-05 10:51:45 -050030#include <nlohmann/json.hpp>
Matthew Barth68ac0042021-06-01 15:38:36 -050031#include <phosphor-logging/log.hpp>
Matthew Bartha227a162020-08-05 10:51:45 -050032#include <sdbusplus/bus.hpp>
Matthew Barth1542fb52021-06-10 14:09:09 -050033#include <sdbusplus/server/manager.hpp>
Matthew Barth06764942021-03-04 09:28:40 -060034#include <sdeventplus/event.hpp>
Matt Spinler2fc0a352021-10-04 15:10:57 -050035#include <sdeventplus/source/event.hpp>
Matthew Barthd9cb63b2021-03-24 14:36:49 -050036#include <sdeventplus/utility/timer.hpp>
37
38#include <chrono>
39#include <map>
40#include <memory>
41#include <optional>
42#include <tuple>
43#include <utility>
44#include <vector>
Matthew Bartha227a162020-08-05 10:51:45 -050045
46namespace phosphor::fan::control::json
47{
48
49using json = nlohmann::json;
Matthew Barth68ac0042021-06-01 15:38:36 -050050using namespace phosphor::logging;
Matthew Bartha227a162020-08-05 10:51:45 -050051
Matthew Barthacd737c2021-03-04 11:04:01 -060052/* Application name to be appended to the path for loading a JSON config file */
53constexpr auto confAppName = "control";
54
Matthew Barthd9cb63b2021-03-24 14:36:49 -050055/* Type of timers supported */
56enum class TimerType
57{
58 oneshot,
59 repeating,
60};
Mike Cappsb2e9a4f2022-06-13 10:15:42 -040061
Matthew Barthd9cb63b2021-03-24 14:36:49 -050062/**
63 * Package of data required when a timer expires
64 * Tuple constructed of:
65 * std::string = Timer package unique identifier
Matthew Barthd9cb63b2021-03-24 14:36:49 -050066 * std::vector<std::unique_ptr<ActionBase>> = List of pointers to actions
67 * that run when the timer expires
Matt Spinlerade0c372021-10-28 16:09:44 -050068 * const std::vector<Group> = List of groups
69 * bool = If groups should be preloaded before actions are run
Matthew Barthd9cb63b2021-03-24 14:36:49 -050070 */
Matthew Barth00f6aa02021-04-09 10:49:47 -050071using TimerPkg =
Matt Spinlerade0c372021-10-28 16:09:44 -050072 std::tuple<std::string, std::vector<std::unique_ptr<ActionBase>>&,
73 const std::vector<Group>&, bool>;
Matthew Barthd9cb63b2021-03-24 14:36:49 -050074/**
75 * Data associated with a running timer that's used when it expires
76 * Pair constructed of:
77 * TimerType = Type of timer to manage expired timer instances
78 * TimerPkg = Package of data required when the timer expires
79 */
80using TimerData = std::pair<TimerType, TimerPkg>;
81/* Dbus event timer */
82using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>;
83
Matthew Barthebabc042021-05-13 15:38:58 -050084/* Dbus signal object */
85constexpr auto Path = 0;
86constexpr auto Intf = 1;
87constexpr auto Prop = 2;
88using SignalObject = std::tuple<std::string, std::string, std::string>;
89/* Dbus signal actions */
Matt Spinlerd0ba86a2021-11-09 10:09:13 -060090using TriggerActions =
Matthew Barthc3a69082021-11-15 14:32:48 -060091 std::vector<std::reference_wrapper<std::unique_ptr<ActionBase>>>;
Matthew Barthebabc042021-05-13 15:38:58 -050092/**
93 * Signal handler function that handles parsing a signal's message for a
94 * particular signal object and stores the results in the manager
95 */
Patrick Williamscb356d42022-07-22 19:26:53 -050096using SignalHandler =
97 std::function<bool(sdbusplus::message_t&, const SignalObject&, Manager&)>;
Matthew Barthebabc042021-05-13 15:38:58 -050098/**
99 * Package of data required when a signal is received
100 * Tuple constructed of:
101 * SignalHandler = Signal handler function
102 * SignalObject = Dbus signal object
Matt Spinlerd0ba86a2021-11-09 10:09:13 -0600103 * TriggerActions = List of actions that are run when the signal is received
Matthew Barthebabc042021-05-13 15:38:58 -0500104 */
Matt Spinlerd0ba86a2021-11-09 10:09:13 -0600105using SignalPkg = std::tuple<SignalHandler, SignalObject, TriggerActions>;
Matthew Barthebabc042021-05-13 15:38:58 -0500106/**
107 * Data associated to a subscribed signal
108 * Tuple constructed of:
Matthew Barthc024d782021-11-09 16:15:49 -0600109 * std::unique_ptr<std::vector<SignalPkg>> =
110 * Pointer to list of the signal's packages
Patrick Williams3ea9ec22021-11-19 12:21:08 -0600111 * std::unique_ptr<sdbusplus::bus::match_t> =
Matthew Barthebabc042021-05-13 15:38:58 -0500112 * Pointer to match holding the subscription to a signal
113 */
Matthew Barthc024d782021-11-09 16:15:49 -0600114using SignalData = std::tuple<std::unique_ptr<std::vector<SignalPkg>>,
Patrick Williams3ea9ec22021-11-19 12:21:08 -0600115 std::unique_ptr<sdbusplus::bus::match_t>>;
Matthew Barthebabc042021-05-13 15:38:58 -0500116
Matthew Bartha227a162020-08-05 10:51:45 -0500117/**
Mike Capps1a19ead2021-10-22 09:15:14 -0400118 * Package of data from a D-Bus call to get managed objects
119 * Tuple constructed of:
120 * std::map<Path, // D-Bus Path
121 * std::map<Intf, // D-Bus Interface
122 * std::map<Property, // D-Bus Property
123 * std::variant>>> // Variant value of that property
124 */
125using Path_v = sdbusplus::message::object_path;
126using Intf_v = std::string;
127using Prop_v = std::string;
128using ManagedObjects =
129 std::map<Path_v, std::map<Intf_v, std::map<Prop_v, PropertyVariantType>>>;
130
131/**
Matt Spinlerd0ba86a2021-11-09 10:09:13 -0600132 * Actions to run when a parameter trigger runs.
133 */
134using ParamTriggerData = std::vector<
135 std::reference_wrapper<const std::vector<std::unique_ptr<ActionBase>>>>;
136
137/**
Matthew Bartha227a162020-08-05 10:51:45 -0500138 * @class Manager - Represents the fan control manager's configuration
139 *
140 * A fan control manager configuration is optional, therefore the "manager.json"
141 * file is also optional. The manager configuration is used to populate
142 * fan control's manager parameters which are used in how the application
143 * operates, not in how the fans are controlled.
144 *
145 * When no manager configuration exists, the fan control application starts,
146 * processes any configured events and then begins controlling fans according
147 * to those events.
148 */
149class Manager
150{
151 public:
152 Manager() = delete;
153 Manager(const Manager&) = delete;
154 Manager(Manager&&) = delete;
155 Manager& operator=(const Manager&) = delete;
156 Manager& operator=(Manager&&) = delete;
157 ~Manager() = default;
158
159 /**
160 * Constructor
161 * Parses and populates the fan control manager attributes from a json file
162 *
Matthew Barth06764942021-03-04 09:28:40 -0600163 * @param[in] event - sdeventplus event loop
Matthew Bartha227a162020-08-05 10:51:45 -0500164 */
Matt Spinler9b062432023-01-26 14:38:50 -0600165 explicit Manager(const sdeventplus::Event& event);
Matthew Bartha227a162020-08-05 10:51:45 -0500166
167 /**
Matthew Barthe91ac862021-05-25 16:22:17 -0500168 * @brief Callback function to handle receiving a HUP signal to reload the
169 * JSON configurations.
170 */
171 void sighupHandler(sdeventplus::source::Signal&,
172 const struct signalfd_siginfo*);
173
174 /**
Matt Spinler2fc0a352021-10-04 15:10:57 -0500175 * @brief Callback function to handle receiving a USR1 signal to dump
176 * the flight recorder.
177 */
Matt Spinler27f5f4e2022-09-01 14:57:39 -0500178 void dumpDebugData(sdeventplus::source::Signal&,
179 const struct signalfd_siginfo*);
Matt Spinler2fc0a352021-10-04 15:10:57 -0500180
181 /**
Matthew Barthacd737c2021-03-04 11:04:01 -0600182 * @brief Get the active profiles of the system where an empty list
183 * represents that only configuration entries without a profile defined will
184 * be loaded.
185 *
186 * @return - The list of active profiles
187 */
188 static const std::vector<std::string>& getActiveProfiles();
189
190 /**
191 * @brief Load the configuration of a given JSON class object based on the
192 * active profiles
193 *
Matthew Barthacd737c2021-03-04 11:04:01 -0600194 * @param[in] isOptional - JSON configuration file is optional or not
Matthew Barth603ef162021-03-24 15:34:53 -0500195 * @param[in] args - Arguments to be forwarded to each instance of `T`
Matthew Barthe6d1f782021-05-14 12:52:20 -0500196 * (*Note that a sdbusplus bus object is required as the first argument)
Matthew Barthacd737c2021-03-04 11:04:01 -0600197 *
198 * @return Map of configuration entries
199 * Map of configuration keys to their corresponding configuration object
200 */
Matthew Barth603ef162021-03-24 15:34:53 -0500201 template <typename T, typename... Args>
Matthew Barthe6d1f782021-05-14 12:52:20 -0500202 static std::map<configKey, std::unique_ptr<T>> getConfig(bool isOptional,
203 Args&&... args)
Matthew Barthacd737c2021-03-04 11:04:01 -0600204 {
205 std::map<configKey, std::unique_ptr<T>> config;
206
Mike Capps808d7fe2022-06-13 10:12:16 -0400207 auto confFile = fan::JsonConfig::getConfFile(
208 confAppName, T::confFileName, isOptional);
209
Matthew Barthacd737c2021-03-04 11:04:01 -0600210 if (!confFile.empty())
211 {
Matthew Barthbd52ed02022-02-07 15:15:10 -0600212 FlightRecorder::instance().log(
213 "main", fmt::format("Loading configuration from {}",
214 confFile.string()));
Matthew Barthacd737c2021-03-04 11:04:01 -0600215 for (const auto& entry : fan::JsonConfig::load(confFile))
216 {
217 if (entry.contains("profiles"))
218 {
219 std::vector<std::string> profiles;
220 for (const auto& profile : entry["profiles"])
221 {
222 profiles.emplace_back(
223 profile.template get<std::string>());
224 }
225 // Do not create the object if its profiles are not in the
226 // list of active profiles
Matthew Barth42428052021-03-30 12:50:59 -0500227 if (!profiles.empty() &&
228 !std::any_of(profiles.begin(), profiles.end(),
Matthew Barthacd737c2021-03-04 11:04:01 -0600229 [](const auto& name) {
Patrick Williams61b73292023-05-10 07:50:12 -0500230 return std::find(getActiveProfiles().begin(),
231 getActiveProfiles().end(),
232 name) != getActiveProfiles().end();
233 }))
Matthew Barthacd737c2021-03-04 11:04:01 -0600234 {
235 continue;
236 }
237 }
Patrick Williams61b73292023-05-10 07:50:12 -0500238 auto obj = std::make_unique<T>(entry,
239 std::forward<Args>(args)...);
Matthew Barthacd737c2021-03-04 11:04:01 -0600240 config.emplace(
241 std::make_pair(obj->getName(), obj->getProfiles()),
242 std::move(obj));
243 }
Matthew Barth68ac0042021-06-01 15:38:36 -0500244 log<level::INFO>(
245 fmt::format("Configuration({}) loaded successfully",
246 T::confFileName)
247 .c_str());
Matthew Barthbd52ed02022-02-07 15:15:10 -0600248 FlightRecorder::instance().log(
249 "main", fmt::format("Configuration({}) loaded successfully",
250 T::confFileName));
Matthew Barthacd737c2021-03-04 11:04:01 -0600251 }
252 return config;
253 }
254
255 /**
Matthew Barth0206c722021-03-30 15:20:05 -0500256 * @brief Check if the given input configuration key matches with another
257 * configuration key that it's to be included in
258 *
259 * @param[in] input - Config key to be included in another config object
260 * @param[in] comp - Config key of the config object to compare with
261 *
262 * @return Whether the configuration object should be included
263 */
264 static bool inConfig(const configKey& input, const configKey& comp);
265
266 /**
Matthew Barth12cb1252021-03-08 16:47:30 -0600267 * @brief Check if the given path and inteface is owned by a dbus service
268 *
269 * @param[in] path - Dbus object path
270 * @param[in] intf - Dbus object interface
271 *
272 * @return - Whether the service has an owner for the given object path and
273 * interface
274 */
275 static bool hasOwner(const std::string& path, const std::string& intf);
276
277 /**
Matthew Barth6d8e2d32022-02-01 16:47:08 -0600278 * @brief Sets the dbus service owner state for all entries in the _servTree
279 * cache and removes associated objects from the _objects cache
280 *
281 * @param[in] serv - Dbus service name
282 * @param[in] hasOwner - Dbus service owner state
283 */
284 void setOwner(const std::string& serv, bool hasOwner);
285
286 /**
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500287 * @brief Sets the dbus service owner state of a given object
288 *
289 * @param[in] path - Dbus object path
290 * @param[in] serv - Dbus service name
291 * @param[in] intf - Dbus object interface
292 * @param[in] isOwned - Dbus service owner state
293 */
294 void setOwner(const std::string& path, const std::string& serv,
295 const std::string& intf, bool isOwned);
296
297 /**
298 * @brief Add a set of services for a path and interface by retrieving all
299 * the path subtrees to the given depth from root for the interface
300 *
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500301 * @param[in] intf - Interface to add services for
302 * @param[in] depth - Depth of tree traversal from root path
303 *
304 * @throws - DBusMethodError
305 * Throws a DBusMethodError when the `getSubTree` method call fails
306 */
Matthew Barth98f6fc12021-04-16 10:48:37 -0500307 static void addServices(const std::string& intf, int32_t depth);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500308
309 /**
310 * @brief Get the service for a given path and interface from cached
311 * dataset and attempt to add all the services for the given path/interface
312 * when it's not found
313 *
314 * @param[in] path - Path to get service for
315 * @param[in] intf - Interface to get service for
316 *
317 * @return - The now cached service name
318 *
319 * @throws - DBusMethodError
320 * Ripples up a DBusMethodError exception from calling addServices
321 */
322 static const std::string& getService(const std::string& path,
323 const std::string& intf);
324
325 /**
Matthew Barthf41e9472021-05-13 16:38:06 -0500326 * @brief Get all the object paths for a given service and interface from
327 * the cached dataset and try to add all the services for the given
328 * interface when no paths are found and then attempt to get all the object
329 * paths again
330 *
331 * @param[in] serv - Service name to get paths for
332 * @param[in] intf - Interface to get paths for
333 *
334 * @return The cached object paths
335 */
336 std::vector<std::string> getPaths(const std::string& serv,
337 const std::string& intf);
338
339 /**
340 * @brief Add objects to the cached dataset by first using
341 * `getManagedObjects` for the same service providing the given path and
342 * interface or just add the single object of the given path, interface, and
343 * property if that fails.
344 *
345 * @param[in] path - Dbus object's path
346 * @param[in] intf - Dbus object's interface
347 * @param[in] prop - Dbus object's property
348 *
349 * @throws - DBusMethodError
350 * Throws a DBusMethodError when the the service is failed to be found or
351 * when the `getManagedObjects` method call fails
352 */
353 void addObjects(const std::string& path, const std::string& intf,
Matt Spinler9ac325c2022-04-25 14:13:49 -0500354 const std::string& prop)
355 {
356 addObjects(path, intf, prop, std::string{});
357 }
358
359 /**
360 * @copydoc Manager::addObjects()
361 *
362 * If the service is known, then it can be used to add all objects
363 * in that service with the interface passed in to the cache instead of
364 * having to look it up. This is done so objects can still be
365 * added even when the D-Bus path passed in doesn't exist so it
366 * can't be used to get a service name.
367 *
368 * @param[in] path - Dbus object's path
369 * @param[in] intf - Dbus object's interface
370 * @param[in] prop - Dbus object's property
371 * @param[in] serviceName - The service of the path/intf/prop if known
372 */
373 void addObjects(const std::string& path, const std::string& intf,
374 const std::string& prop, const std::string& serviceName);
Matthew Barthf41e9472021-05-13 16:38:06 -0500375
376 /**
377 * @brief Get an object's property value
378 *
379 * @param[in] path - Dbus object's path
380 * @param[in] intf - Dbus object's interface
381 * @param[in] prop - Dbus object's property
382 */
383 const std::optional<PropertyVariantType>
384 getProperty(const std::string& path, const std::string& intf,
385 const std::string& prop);
386
387 /**
Matthew Barthbaeeb8f2021-05-13 16:03:54 -0500388 * @brief Set/update an object's property value
389 *
390 * @param[in] path - Dbus object's path
391 * @param[in] intf - Dbus object's interface
392 * @param[in] prop - Dbus object's property
393 * @param[in] value - Dbus object's property value
394 */
395 void setProperty(const std::string& path, const std::string& intf,
Mike Capps1a19ead2021-10-22 09:15:14 -0400396 const std::string& prop, PropertyVariantType value);
Matthew Barthbaeeb8f2021-05-13 16:03:54 -0500397
398 /**
Matthew Barthc2691402021-05-13 16:20:32 -0500399 * @brief Remove an object's interface
400 *
401 * @param[in] path - Dbus object's path
402 * @param[in] intf - Dbus object's interface
403 */
404 inline void removeInterface(const std::string& path,
405 const std::string& intf)
406 {
407 auto itPath = _objects.find(path);
408 if (itPath != std::end(_objects))
409 {
410 _objects[path].erase(intf);
411 }
412 }
413
414 /**
Matthew Barth07fecfc2021-01-29 09:04:43 -0600415 * @brief Get the object's property value as a variant
416 *
417 * @param[in] path - Path of the object containing the property
418 * @param[in] intf - Interface name containing the property
419 * @param[in] prop - Name of property
420 *
421 * @return - The object's property value as a variant
422 */
423 static inline auto getObjValueVariant(const std::string& path,
424 const std::string& intf,
425 const std::string& prop)
426 {
427 return _objects.at(path).at(intf).at(prop);
428 };
429
430 /**
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500431 * @brief Add a dbus timer
432 *
433 * @param[in] type - Type of timer
434 * @param[in] interval - Timer interval in microseconds
435 * @param[in] pkg - Packaged data for when timer expires
436 */
437 void addTimer(const TimerType type,
438 const std::chrono::microseconds interval,
439 std::unique_ptr<TimerPkg> pkg);
440
441 /**
442 * @brief Callback when a timer expires
443 *
444 * @param[in] data - Data to be used when the timer expired
445 */
446 void timerExpired(TimerData& data);
447
448 /**
Matthew Barthebabc042021-05-13 15:38:58 -0500449 * @brief Get the signal data for a given match string
450 *
451 * @param[in] sigMatch - Signal match string
452 *
453 * @return - Reference to the signal data for the given match string
454 */
455 std::vector<SignalData>& getSignal(const std::string& sigMatch)
456 {
457 return _signals[sigMatch];
458 }
459
460 /**
461 * @brief Handle receiving signals
462 *
463 * @param[in] msg - Signal message containing the signal's data
464 * @param[in] pkgs - Signal packages associated to the signal being handled
465 */
Patrick Williamscb356d42022-07-22 19:26:53 -0500466 void handleSignal(sdbusplus::message_t& msg,
Matthew Barthc024d782021-11-09 16:15:49 -0600467 const std::vector<SignalPkg>* pkgs);
Matthew Barthebabc042021-05-13 15:38:58 -0500468
469 /**
Matthew Bartheebde062021-04-14 12:48:52 -0500470 * @brief Get the sdbusplus bus object
471 */
472 inline auto& getBus()
473 {
474 return _bus;
475 }
476
477 /**
Matthew Barth48f44da2021-05-27 15:43:34 -0500478 * @brief Is the power state on
479 *
480 * @return Current power state of the system
481 */
482 inline bool isPowerOn() const
483 {
484 return _powerState->isPowerOn();
485 }
486
Matthew Barth3770a1d2021-06-10 15:09:37 -0500487 /**
488 * @brief Load all the fan control JSON configuration files
489 *
490 * This is where all the fan control JSON configuration files are parsed and
491 * loaded into their associated objects. Anything that needs to be done when
492 * the Manager object is constructed or handling a SIGHUP to reload the
493 * configurations needs to be done here.
494 */
495 void load();
496
Matt Spinlerd76351b2021-08-05 16:23:09 -0500497 /**
498 * @brief Sets a value in the parameter map.
499 *
Matt Spinler72c4af42021-11-29 14:40:17 -0600500 * If it's a std::nullopt, it will be deleted instead.
501 *
Matt Spinlerd76351b2021-08-05 16:23:09 -0500502 * @param[in] name - The parameter name
503 * @param[in] value - The parameter value
504 */
505 static void setParameter(const std::string& name,
Matt Spinler72c4af42021-11-29 14:40:17 -0600506 const std::optional<PropertyVariantType>& value)
Matt Spinlerd76351b2021-08-05 16:23:09 -0500507 {
Matt Spinler72c4af42021-11-29 14:40:17 -0600508 if (value)
Matt Spinlerd0ba86a2021-11-09 10:09:13 -0600509 {
Matt Spinler72c4af42021-11-29 14:40:17 -0600510 auto it = _parameters.find(name);
511 auto changed = (it == _parameters.end()) ||
512 ((it != _parameters.end()) && it->second != *value);
513 _parameters[name] = *value;
514
515 if (changed)
516 {
517 runParameterActions(name);
518 }
519 }
520 else
521 {
522 size_t deleted = _parameters.erase(name);
523
524 if (deleted)
525 {
526 runParameterActions(name);
527 }
Matt Spinlerd0ba86a2021-11-09 10:09:13 -0600528 }
Matt Spinlerd76351b2021-08-05 16:23:09 -0500529 }
530
531 /**
532 * @brief Returns a value from the parameter map
533 *
534 * @param[in] name - The parameter name
535 *
536 * @return The parameter value, or std::nullopt if not found
537 */
538 static std::optional<PropertyVariantType>
539 getParameter(const std::string& name)
540 {
541 auto it = _parameters.find(name);
542 if (it != _parameters.end())
543 {
544 return it->second;
545 }
546
547 return std::nullopt;
548 }
549
550 /**
Matt Spinlerd0ba86a2021-11-09 10:09:13 -0600551 * @brief Runs the actions registered to a parameter
552 * trigger with this name.
553 *
554 * @param[in] name - The parameter name
555 */
556 static void runParameterActions(const std::string& name);
557
558 /**
559 * @brief Adds a parameter trigger
560 *
561 * @param[in] name - The parameter name
562 * @param[in] actions - The actions to run on the trigger
563 */
564 static void
565 addParameterTrigger(const std::string& name,
566 std::vector<std::unique_ptr<ActionBase>>& actions);
567
Matt Spinler7787def2021-10-14 16:33:16 -0500568 /* The name of the dump file */
569 static const std::string dumpFile;
570
Matthew Bartha227a162020-08-05 10:51:45 -0500571 private:
Mike Capps1a19ead2021-10-22 09:15:14 -0400572 /**
573 * @brief Helper to detect when a property's double contains a NaN
574 * (not-a-number) value.
575 *
576 * @param[in] value - The property to test
577 */
578 static bool PropertyContainsNan(const PropertyVariantType& value)
579 {
580 return (std::holds_alternative<double>(value) &&
581 std::isnan(std::get<double>(value)));
582 }
583
584 /**
585 * @brief Insert managed objects into cache, but filter out properties
Matt Spinlerc2c2db72022-04-07 13:59:37 -0500586 * containing unwanted NaN (not-a-number) values and properties that
587 * are on D-Bus paths that aren't in an existing Group object.
Mike Capps1a19ead2021-10-22 09:15:14 -0400588 *
589 * @param[in] ref - The map of ManagedObjects to insert into cache
590 */
591 void insertFilteredObjects(ManagedObjects& ref);
592
Matthew Barth1542fb52021-06-10 14:09:09 -0500593 /* The sdbusplus bus object to use */
Patrick Williamscb356d42022-07-22 19:26:53 -0500594 sdbusplus::bus_t& _bus;
Matthew Barthacd737c2021-03-04 11:04:01 -0600595
Matthew Barth1542fb52021-06-10 14:09:09 -0500596 /* The sdeventplus even loop to use */
Matthew Barthacd737c2021-03-04 11:04:01 -0600597 sdeventplus::Event _event;
598
Matthew Barth1542fb52021-06-10 14:09:09 -0500599 /* The sdbusplus manager object to set the ObjectManager interface */
Patrick Williamscb356d42022-07-22 19:26:53 -0500600 sdbusplus::server::manager_t _mgr;
Matthew Barth1542fb52021-06-10 14:09:09 -0500601
Matthew Barth3770a1d2021-06-10 15:09:37 -0500602 /* Whether loading the config files is allowed or not */
603 bool _loadAllowed;
604
Matthew Barth48f44da2021-05-27 15:43:34 -0500605 /* The system's power state determination object */
606 std::unique_ptr<PowerState> _powerState;
607
Matthew Barth06764942021-03-04 09:28:40 -0600608 /* List of profiles configured */
Matthew Barthacd737c2021-03-04 11:04:01 -0600609 std::map<configKey, std::unique_ptr<Profile>> _profiles;
Matthew Barth06764942021-03-04 09:28:40 -0600610
611 /* List of active profiles */
Matthew Barthacd737c2021-03-04 11:04:01 -0600612 static std::vector<std::string> _activeProfiles;
613
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500614 /* Subtree map of paths to services of interfaces(with ownership state) */
615 static std::map<
616 std::string,
617 std::map<std::string, std::pair<bool, std::vector<std::string>>>>
Matthew Barth12cb1252021-03-08 16:47:30 -0600618 _servTree;
619
Matthew Barth07fecfc2021-01-29 09:04:43 -0600620 /* Object map of paths to interfaces of properties and their values */
621 static std::map<
622 std::string,
623 std::map<std::string, std::map<std::string, PropertyVariantType>>>
624 _objects;
625
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500626 /* List of timers and their data to be processed when expired */
627 std::vector<std::pair<std::unique_ptr<TimerData>, Timer>> _timers;
628
Matthew Barthebabc042021-05-13 15:38:58 -0500629 /* Map of signal match strings to a list of signal handler data */
630 std::unordered_map<std::string, std::vector<SignalData>> _signals;
631
Matthew Barthacd737c2021-03-04 11:04:01 -0600632 /* List of zones configured */
633 std::map<configKey, std::unique_ptr<Zone>> _zones;
634
Matthew Barth44ab7692021-03-26 11:40:10 -0500635 /* List of events configured */
636 std::map<configKey, std::unique_ptr<Event>> _events;
637
Matthew Barthacd737c2021-03-04 11:04:01 -0600638 /**
Matt Spinlerd76351b2021-08-05 16:23:09 -0500639 * @brief A map of parameter names and values that are something
640 * other than just D-Bus property values that other actions
641 * can set and use.
642 */
643 static std::unordered_map<std::string, PropertyVariantType> _parameters;
644
645 /**
Matt Spinlerd0ba86a2021-11-09 10:09:13 -0600646 * @brief Map of parameter names to the actions to run when their
647 * values change.
648 */
649 static std::unordered_map<std::string, TriggerActions> _parameterTriggers;
650
651 /**
Matthew Barthb2cd93f2021-06-16 16:37:40 -0500652 * @brief Callback for power state changes
653 *
654 * @param[in] powerStateOn - Whether the power state is on or not
655 *
656 * Callback function bound to the PowerState object instance to handle each
657 * time the power state changes.
658 */
659 void powerStateChanged(bool powerStateOn);
660
661 /**
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500662 * @brief Find the service name for a given path and interface from the
663 * cached dataset
664 *
665 * @param[in] path - Path to get service for
666 * @param[in] intf - Interface to get service for
667 *
668 * @return - The cached service name
669 */
670 static const std::string& findService(const std::string& path,
671 const std::string& intf);
672
673 /**
Matthew Barthf41e9472021-05-13 16:38:06 -0500674 * @brief Find all the paths for a given service and interface from the
675 * cached dataset
676 *
677 * @param[in] serv - Service name to get paths for
678 * @param[in] intf - Interface to get paths for
679 *
680 * @return - The cached object paths
681 */
682 std::vector<std::string> findPaths(const std::string& serv,
683 const std::string& intf);
684
685 /**
Matthew Barthacd737c2021-03-04 11:04:01 -0600686 * @brief Parse and set the configured profiles from the profiles JSON file
687 *
688 * Retrieves the optional profiles JSON configuration file, parses it, and
689 * creates a list of configured profiles available to the other
690 * configuration files. These profiles can be used to remove or include
691 * entries within the other configuration files.
692 */
693 void setProfiles();
Matt Spinler2fc0a352021-10-04 15:10:57 -0500694
695 /**
Matt Spinlerb5c21a22021-10-14 16:52:12 -0500696 * @brief Dump the _objects, _servTree, and _parameters maps to JSON
697 *
698 * @param[out] data - The JSON that will be filled in
699 */
700 void dumpCache(json& data);
Matt Spinlerade0c372021-10-28 16:09:44 -0500701
702 /**
Matthew Barth2f359f72022-02-15 10:00:26 -0600703 * @brief Add a list of groups to the cache dataset.
Matt Spinlerade0c372021-10-28 16:09:44 -0500704 *
Matthew Barth2f359f72022-02-15 10:00:26 -0600705 * @param[in] groups - The groups to add
Matt Spinlerade0c372021-10-28 16:09:44 -0500706 */
Matthew Barth2f359f72022-02-15 10:00:26 -0600707 void addGroups(const std::vector<Group>& groups);
Matthew Bartha227a162020-08-05 10:51:45 -0500708};
709
710} // namespace phosphor::fan::control::json