blob: c5b591dba068ed16b9e7b0cde1514835e8d660b4 [file] [log] [blame]
Matthew Bartha227a162020-08-05 10:51:45 -05001/**
2 * Copyright © 2020 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#pragma once
17
Matthew Barthd9cb63b2021-03-24 14:36:49 -050018#include "action.hpp"
Matthew Barth44ab7692021-03-26 11:40:10 -050019#include "config_base.hpp"
20#include "event.hpp"
Matthew Barthd9cb63b2021-03-24 14:36:49 -050021#include "group.hpp"
Matthew Barthacd737c2021-03-04 11:04:01 -060022#include "json_config.hpp"
Matthew Barth06764942021-03-04 09:28:40 -060023#include "profile.hpp"
Matthew Barth9403a212021-05-17 09:31:50 -050024#include "sdbusplus.hpp"
Matthew Barthacd737c2021-03-04 11:04:01 -060025#include "zone.hpp"
Matthew Barth06764942021-03-04 09:28:40 -060026
Matthew Bartha227a162020-08-05 10:51:45 -050027#include <nlohmann/json.hpp>
28#include <sdbusplus/bus.hpp>
Matthew Barth06764942021-03-04 09:28:40 -060029#include <sdeventplus/event.hpp>
Matthew Barthd9cb63b2021-03-24 14:36:49 -050030#include <sdeventplus/utility/timer.hpp>
31
32#include <chrono>
33#include <map>
34#include <memory>
35#include <optional>
36#include <tuple>
37#include <utility>
38#include <vector>
Matthew Bartha227a162020-08-05 10:51:45 -050039
40namespace phosphor::fan::control::json
41{
42
43using json = nlohmann::json;
44
Matthew Barthacd737c2021-03-04 11:04:01 -060045/* Application name to be appended to the path for loading a JSON config file */
46constexpr auto confAppName = "control";
47
Matthew Barthd9cb63b2021-03-24 14:36:49 -050048/* Type of timers supported */
49enum class TimerType
50{
51 oneshot,
52 repeating,
53};
54/**
55 * Package of data required when a timer expires
56 * Tuple constructed of:
57 * std::string = Timer package unique identifier
Matthew Barthd9cb63b2021-03-24 14:36:49 -050058 * std::vector<std::unique_ptr<ActionBase>> = List of pointers to actions
59 * that run when the timer expires
60 */
Matthew Barth00f6aa02021-04-09 10:49:47 -050061using TimerPkg =
62 std::tuple<std::string, std::vector<std::unique_ptr<ActionBase>>&>;
Matthew Barthd9cb63b2021-03-24 14:36:49 -050063/**
64 * Data associated with a running timer that's used when it expires
65 * Pair constructed of:
66 * TimerType = Type of timer to manage expired timer instances
67 * TimerPkg = Package of data required when the timer expires
68 */
69using TimerData = std::pair<TimerType, TimerPkg>;
70/* Dbus event timer */
71using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>;
72
Matthew Barthebabc042021-05-13 15:38:58 -050073/* Dbus signal object */
74constexpr auto Path = 0;
75constexpr auto Intf = 1;
76constexpr auto Prop = 2;
77using SignalObject = std::tuple<std::string, std::string, std::string>;
78/* Dbus signal actions */
79using SignalActions =
80 std::vector<std::reference_wrapper<std::unique_ptr<ActionBase>>>;
81/**
82 * Signal handler function that handles parsing a signal's message for a
83 * particular signal object and stores the results in the manager
84 */
85using SignalHandler = std::function<bool(sdbusplus::message::message&,
86 const SignalObject&, Manager&)>;
87/**
88 * Package of data required when a signal is received
89 * Tuple constructed of:
90 * SignalHandler = Signal handler function
91 * SignalObject = Dbus signal object
92 * SignalActions = List of actions that are run when the signal is received
93 */
94using SignalPkg = std::tuple<SignalHandler, SignalObject, SignalActions>;
95/**
96 * Data associated to a subscribed signal
97 * Tuple constructed of:
98 * std::unique_ptr<std::vector<SignalPkg>> =
99 * Pointer to the signal's packages
100 * std::unique_ptr<sdbusplus::server::match::match> =
101 * Pointer to match holding the subscription to a signal
102 */
103using SignalData = std::tuple<std::unique_ptr<std::vector<SignalPkg>>,
104 std::unique_ptr<sdbusplus::server::match::match>>;
105
Matthew Bartha227a162020-08-05 10:51:45 -0500106/**
107 * @class Manager - Represents the fan control manager's configuration
108 *
109 * A fan control manager configuration is optional, therefore the "manager.json"
110 * file is also optional. The manager configuration is used to populate
111 * fan control's manager parameters which are used in how the application
112 * operates, not in how the fans are controlled.
113 *
114 * When no manager configuration exists, the fan control application starts,
115 * processes any configured events and then begins controlling fans according
116 * to those events.
117 */
118class Manager
119{
120 public:
121 Manager() = delete;
122 Manager(const Manager&) = delete;
123 Manager(Manager&&) = delete;
124 Manager& operator=(const Manager&) = delete;
125 Manager& operator=(Manager&&) = delete;
126 ~Manager() = default;
127
128 /**
129 * Constructor
130 * Parses and populates the fan control manager attributes from a json file
131 *
Matthew Barth06764942021-03-04 09:28:40 -0600132 * @param[in] event - sdeventplus event loop
Matthew Bartha227a162020-08-05 10:51:45 -0500133 */
Matthew Barth9403a212021-05-17 09:31:50 -0500134 Manager(const sdeventplus::Event& event);
Matthew Bartha227a162020-08-05 10:51:45 -0500135
136 /**
Matthew Barthe91ac862021-05-25 16:22:17 -0500137 * @brief Callback function to handle receiving a HUP signal to reload the
138 * JSON configurations.
139 */
140 void sighupHandler(sdeventplus::source::Signal&,
141 const struct signalfd_siginfo*);
142
143 /**
Matthew Barthacd737c2021-03-04 11:04:01 -0600144 * @brief Get the active profiles of the system where an empty list
145 * represents that only configuration entries without a profile defined will
146 * be loaded.
147 *
148 * @return - The list of active profiles
149 */
150 static const std::vector<std::string>& getActiveProfiles();
151
152 /**
153 * @brief Load the configuration of a given JSON class object based on the
154 * active profiles
155 *
Matthew Barthacd737c2021-03-04 11:04:01 -0600156 * @param[in] isOptional - JSON configuration file is optional or not
Matthew Barth603ef162021-03-24 15:34:53 -0500157 * @param[in] args - Arguments to be forwarded to each instance of `T`
Matthew Barthe6d1f782021-05-14 12:52:20 -0500158 * (*Note that a sdbusplus bus object is required as the first argument)
Matthew Barthacd737c2021-03-04 11:04:01 -0600159 *
160 * @return Map of configuration entries
161 * Map of configuration keys to their corresponding configuration object
162 */
Matthew Barth603ef162021-03-24 15:34:53 -0500163 template <typename T, typename... Args>
Matthew Barthe6d1f782021-05-14 12:52:20 -0500164 static std::map<configKey, std::unique_ptr<T>> getConfig(bool isOptional,
165 Args&&... args)
Matthew Barthacd737c2021-03-04 11:04:01 -0600166 {
167 std::map<configKey, std::unique_ptr<T>> config;
168
Matthew Barth9403a212021-05-17 09:31:50 -0500169 auto confFile =
170 fan::JsonConfig::getConfFile(util::SDBusPlus::getBus(), confAppName,
171 T::confFileName, isOptional);
Matthew Barthacd737c2021-03-04 11:04:01 -0600172 if (!confFile.empty())
173 {
174 for (const auto& entry : fan::JsonConfig::load(confFile))
175 {
176 if (entry.contains("profiles"))
177 {
178 std::vector<std::string> profiles;
179 for (const auto& profile : entry["profiles"])
180 {
181 profiles.emplace_back(
182 profile.template get<std::string>());
183 }
184 // Do not create the object if its profiles are not in the
185 // list of active profiles
Matthew Barth42428052021-03-30 12:50:59 -0500186 if (!profiles.empty() &&
187 !std::any_of(profiles.begin(), profiles.end(),
Matthew Barthacd737c2021-03-04 11:04:01 -0600188 [](const auto& name) {
189 return std::find(
190 getActiveProfiles().begin(),
191 getActiveProfiles().end(),
192 name) !=
193 getActiveProfiles().end();
194 }))
195 {
196 continue;
197 }
198 }
Matthew Barth603ef162021-03-24 15:34:53 -0500199 auto obj =
200 std::make_unique<T>(entry, std::forward<Args>(args)...);
Matthew Barthacd737c2021-03-04 11:04:01 -0600201 config.emplace(
202 std::make_pair(obj->getName(), obj->getProfiles()),
203 std::move(obj));
204 }
205 }
206 return config;
207 }
208
209 /**
Matthew Barth0206c722021-03-30 15:20:05 -0500210 * @brief Check if the given input configuration key matches with another
211 * configuration key that it's to be included in
212 *
213 * @param[in] input - Config key to be included in another config object
214 * @param[in] comp - Config key of the config object to compare with
215 *
216 * @return Whether the configuration object should be included
217 */
218 static bool inConfig(const configKey& input, const configKey& comp);
219
220 /**
Matthew Barth12cb1252021-03-08 16:47:30 -0600221 * @brief Check if the given path and inteface is owned by a dbus service
222 *
223 * @param[in] path - Dbus object path
224 * @param[in] intf - Dbus object interface
225 *
226 * @return - Whether the service has an owner for the given object path and
227 * interface
228 */
229 static bool hasOwner(const std::string& path, const std::string& intf);
230
231 /**
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500232 * @brief Sets the dbus service owner state of a given object
233 *
234 * @param[in] path - Dbus object path
235 * @param[in] serv - Dbus service name
236 * @param[in] intf - Dbus object interface
237 * @param[in] isOwned - Dbus service owner state
238 */
239 void setOwner(const std::string& path, const std::string& serv,
240 const std::string& intf, bool isOwned);
241
242 /**
243 * @brief Add a set of services for a path and interface by retrieving all
244 * the path subtrees to the given depth from root for the interface
245 *
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500246 * @param[in] intf - Interface to add services for
247 * @param[in] depth - Depth of tree traversal from root path
248 *
249 * @throws - DBusMethodError
250 * Throws a DBusMethodError when the `getSubTree` method call fails
251 */
Matthew Barth98f6fc12021-04-16 10:48:37 -0500252 static void addServices(const std::string& intf, int32_t depth);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500253
254 /**
255 * @brief Get the service for a given path and interface from cached
256 * dataset and attempt to add all the services for the given path/interface
257 * when it's not found
258 *
259 * @param[in] path - Path to get service for
260 * @param[in] intf - Interface to get service for
261 *
262 * @return - The now cached service name
263 *
264 * @throws - DBusMethodError
265 * Ripples up a DBusMethodError exception from calling addServices
266 */
267 static const std::string& getService(const std::string& path,
268 const std::string& intf);
269
270 /**
Matthew Barthf41e9472021-05-13 16:38:06 -0500271 * @brief Get all the object paths for a given service and interface from
272 * the cached dataset and try to add all the services for the given
273 * interface when no paths are found and then attempt to get all the object
274 * paths again
275 *
276 * @param[in] serv - Service name to get paths for
277 * @param[in] intf - Interface to get paths for
278 *
279 * @return The cached object paths
280 */
281 std::vector<std::string> getPaths(const std::string& serv,
282 const std::string& intf);
283
284 /**
285 * @brief Add objects to the cached dataset by first using
286 * `getManagedObjects` for the same service providing the given path and
287 * interface or just add the single object of the given path, interface, and
288 * property if that fails.
289 *
290 * @param[in] path - Dbus object's path
291 * @param[in] intf - Dbus object's interface
292 * @param[in] prop - Dbus object's property
293 *
294 * @throws - DBusMethodError
295 * Throws a DBusMethodError when the the service is failed to be found or
296 * when the `getManagedObjects` method call fails
297 */
298 void addObjects(const std::string& path, const std::string& intf,
299 const std::string& prop);
300
301 /**
302 * @brief Get an object's property value
303 *
304 * @param[in] path - Dbus object's path
305 * @param[in] intf - Dbus object's interface
306 * @param[in] prop - Dbus object's property
307 */
308 const std::optional<PropertyVariantType>
309 getProperty(const std::string& path, const std::string& intf,
310 const std::string& prop);
311
312 /**
Matthew Barthbaeeb8f2021-05-13 16:03:54 -0500313 * @brief Set/update an object's property value
314 *
315 * @param[in] path - Dbus object's path
316 * @param[in] intf - Dbus object's interface
317 * @param[in] prop - Dbus object's property
318 * @param[in] value - Dbus object's property value
319 */
320 void setProperty(const std::string& path, const std::string& intf,
321 const std::string& prop, PropertyVariantType value)
322 {
323 _objects[path][intf][prop] = std::move(value);
324 }
325
326 /**
Matthew Barthc2691402021-05-13 16:20:32 -0500327 * @brief Remove an object's interface
328 *
329 * @param[in] path - Dbus object's path
330 * @param[in] intf - Dbus object's interface
331 */
332 inline void removeInterface(const std::string& path,
333 const std::string& intf)
334 {
335 auto itPath = _objects.find(path);
336 if (itPath != std::end(_objects))
337 {
338 _objects[path].erase(intf);
339 }
340 }
341
342 /**
Matthew Barth07fecfc2021-01-29 09:04:43 -0600343 * @brief Get the object's property value as a variant
344 *
345 * @param[in] path - Path of the object containing the property
346 * @param[in] intf - Interface name containing the property
347 * @param[in] prop - Name of property
348 *
349 * @return - The object's property value as a variant
350 */
351 static inline auto getObjValueVariant(const std::string& path,
352 const std::string& intf,
353 const std::string& prop)
354 {
355 return _objects.at(path).at(intf).at(prop);
356 };
357
358 /**
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500359 * @brief Add a dbus timer
360 *
361 * @param[in] type - Type of timer
362 * @param[in] interval - Timer interval in microseconds
363 * @param[in] pkg - Packaged data for when timer expires
364 */
365 void addTimer(const TimerType type,
366 const std::chrono::microseconds interval,
367 std::unique_ptr<TimerPkg> pkg);
368
369 /**
370 * @brief Callback when a timer expires
371 *
372 * @param[in] data - Data to be used when the timer expired
373 */
374 void timerExpired(TimerData& data);
375
376 /**
Matthew Barthebabc042021-05-13 15:38:58 -0500377 * @brief Get the signal data for a given match string
378 *
379 * @param[in] sigMatch - Signal match string
380 *
381 * @return - Reference to the signal data for the given match string
382 */
383 std::vector<SignalData>& getSignal(const std::string& sigMatch)
384 {
385 return _signals[sigMatch];
386 }
387
388 /**
389 * @brief Handle receiving signals
390 *
391 * @param[in] msg - Signal message containing the signal's data
392 * @param[in] pkgs - Signal packages associated to the signal being handled
393 */
394 void handleSignal(sdbusplus::message::message& msg,
395 const std::vector<SignalPkg>* pkgs);
396
397 /**
Matthew Bartheebde062021-04-14 12:48:52 -0500398 * @brief Get the sdbusplus bus object
399 */
400 inline auto& getBus()
401 {
402 return _bus;
403 }
404
405 /**
Matthew Bartha227a162020-08-05 10:51:45 -0500406 * @brief Get the configured power on delay(OPTIONAL)
407 *
408 * @return Power on delay in seconds
409 * Configured power on delay in seconds, otherwise 0
410 */
411 unsigned int getPowerOnDelay();
412
413 private:
414 /* JSON file name for manager configuration attributes */
415 static constexpr auto confFileName = "manager.json";
416
417 /* The parsed JSON object */
418 json _jsonObj;
Matthew Barth06764942021-03-04 09:28:40 -0600419
Matthew Barthacd737c2021-03-04 11:04:01 -0600420 /**
421 * The sdbusplus bus object to use
422 */
423 sdbusplus::bus::bus& _bus;
424
425 /**
426 * The sdeventplus even loop to use
427 */
428 sdeventplus::Event _event;
429
Matthew Barth06764942021-03-04 09:28:40 -0600430 /* List of profiles configured */
Matthew Barthacd737c2021-03-04 11:04:01 -0600431 std::map<configKey, std::unique_ptr<Profile>> _profiles;
Matthew Barth06764942021-03-04 09:28:40 -0600432
433 /* List of active profiles */
Matthew Barthacd737c2021-03-04 11:04:01 -0600434 static std::vector<std::string> _activeProfiles;
435
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500436 /* Subtree map of paths to services of interfaces(with ownership state) */
437 static std::map<
438 std::string,
439 std::map<std::string, std::pair<bool, std::vector<std::string>>>>
Matthew Barth12cb1252021-03-08 16:47:30 -0600440 _servTree;
441
Matthew Barth07fecfc2021-01-29 09:04:43 -0600442 /* Object map of paths to interfaces of properties and their values */
443 static std::map<
444 std::string,
445 std::map<std::string, std::map<std::string, PropertyVariantType>>>
446 _objects;
447
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500448 /* List of timers and their data to be processed when expired */
449 std::vector<std::pair<std::unique_ptr<TimerData>, Timer>> _timers;
450
Matthew Barthebabc042021-05-13 15:38:58 -0500451 /* Map of signal match strings to a list of signal handler data */
452 std::unordered_map<std::string, std::vector<SignalData>> _signals;
453
Matthew Barthacd737c2021-03-04 11:04:01 -0600454 /* List of zones configured */
455 std::map<configKey, std::unique_ptr<Zone>> _zones;
456
Matthew Barth44ab7692021-03-26 11:40:10 -0500457 /* List of events configured */
458 std::map<configKey, std::unique_ptr<Event>> _events;
459
Matthew Barthacd737c2021-03-04 11:04:01 -0600460 /**
Matthew Barthe91ac862021-05-25 16:22:17 -0500461 * @brief Load all the fan control JSON configuration files
462 *
463 * This is where all the fan control JSON configuration files are parsed and
464 * loaded into their associated objects. Anything that needs to be done when
465 * the Manager object is constructed or handling a SIGHUP to reload the
466 * configurations needs to be done here.
467 */
468 void load();
469
470 /**
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500471 * @brief Find the service name for a given path and interface from the
472 * cached dataset
473 *
474 * @param[in] path - Path to get service for
475 * @param[in] intf - Interface to get service for
476 *
477 * @return - The cached service name
478 */
479 static const std::string& findService(const std::string& path,
480 const std::string& intf);
481
482 /**
Matthew Barthf41e9472021-05-13 16:38:06 -0500483 * @brief Find all the paths for a given service and interface from the
484 * cached dataset
485 *
486 * @param[in] serv - Service name to get paths for
487 * @param[in] intf - Interface to get paths for
488 *
489 * @return - The cached object paths
490 */
491 std::vector<std::string> findPaths(const std::string& serv,
492 const std::string& intf);
493
494 /**
Matthew Barthacd737c2021-03-04 11:04:01 -0600495 * @brief Parse and set the configured profiles from the profiles JSON file
496 *
497 * Retrieves the optional profiles JSON configuration file, parses it, and
498 * creates a list of configured profiles available to the other
499 * configuration files. These profiles can be used to remove or include
500 * entries within the other configuration files.
501 */
502 void setProfiles();
Matthew Bartha227a162020-08-05 10:51:45 -0500503};
504
505} // namespace phosphor::fan::control::json