blob: b414c62de078c2b27422d641c3697a19b1651eb3 [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 Barthacd737c2021-03-04 11:04:01 -060024#include "zone.hpp"
Matthew Barth06764942021-03-04 09:28:40 -060025
Matthew Bartha227a162020-08-05 10:51:45 -050026#include <nlohmann/json.hpp>
27#include <sdbusplus/bus.hpp>
Matthew Barth06764942021-03-04 09:28:40 -060028#include <sdeventplus/event.hpp>
Matthew Barthd9cb63b2021-03-24 14:36:49 -050029#include <sdeventplus/utility/timer.hpp>
30
31#include <chrono>
32#include <map>
33#include <memory>
34#include <optional>
35#include <tuple>
36#include <utility>
37#include <vector>
Matthew Bartha227a162020-08-05 10:51:45 -050038
39namespace phosphor::fan::control::json
40{
41
42using json = nlohmann::json;
43
Matthew Barthacd737c2021-03-04 11:04:01 -060044/* Application name to be appended to the path for loading a JSON config file */
45constexpr auto confAppName = "control";
46
Matthew Barthd9cb63b2021-03-24 14:36:49 -050047/* Type of timers supported */
48enum class TimerType
49{
50 oneshot,
51 repeating,
52};
53/**
54 * Package of data required when a timer expires
55 * Tuple constructed of:
56 * std::string = Timer package unique identifier
Matthew Barthd9cb63b2021-03-24 14:36:49 -050057 * std::vector<std::unique_ptr<ActionBase>> = List of pointers to actions
58 * that run when the timer expires
59 */
Matthew Barth00f6aa02021-04-09 10:49:47 -050060using TimerPkg =
61 std::tuple<std::string, std::vector<std::unique_ptr<ActionBase>>&>;
Matthew Barthd9cb63b2021-03-24 14:36:49 -050062/**
63 * Data associated with a running timer that's used when it expires
64 * Pair constructed of:
65 * TimerType = Type of timer to manage expired timer instances
66 * TimerPkg = Package of data required when the timer expires
67 */
68using TimerData = std::pair<TimerType, TimerPkg>;
69/* Dbus event timer */
70using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>;
71
Matthew Barthebabc042021-05-13 15:38:58 -050072/* Dbus signal object */
73constexpr auto Path = 0;
74constexpr auto Intf = 1;
75constexpr auto Prop = 2;
76using SignalObject = std::tuple<std::string, std::string, std::string>;
77/* Dbus signal actions */
78using SignalActions =
79 std::vector<std::reference_wrapper<std::unique_ptr<ActionBase>>>;
80/**
81 * Signal handler function that handles parsing a signal's message for a
82 * particular signal object and stores the results in the manager
83 */
84using SignalHandler = std::function<bool(sdbusplus::message::message&,
85 const SignalObject&, Manager&)>;
86/**
87 * Package of data required when a signal is received
88 * Tuple constructed of:
89 * SignalHandler = Signal handler function
90 * SignalObject = Dbus signal object
91 * SignalActions = List of actions that are run when the signal is received
92 */
93using SignalPkg = std::tuple<SignalHandler, SignalObject, SignalActions>;
94/**
95 * Data associated to a subscribed signal
96 * Tuple constructed of:
97 * std::unique_ptr<std::vector<SignalPkg>> =
98 * Pointer to the signal's packages
99 * std::unique_ptr<sdbusplus::server::match::match> =
100 * Pointer to match holding the subscription to a signal
101 */
102using SignalData = std::tuple<std::unique_ptr<std::vector<SignalPkg>>,
103 std::unique_ptr<sdbusplus::server::match::match>>;
104
Matthew Bartha227a162020-08-05 10:51:45 -0500105/**
106 * @class Manager - Represents the fan control manager's configuration
107 *
108 * A fan control manager configuration is optional, therefore the "manager.json"
109 * file is also optional. The manager configuration is used to populate
110 * fan control's manager parameters which are used in how the application
111 * operates, not in how the fans are controlled.
112 *
113 * When no manager configuration exists, the fan control application starts,
114 * processes any configured events and then begins controlling fans according
115 * to those events.
116 */
117class Manager
118{
119 public:
120 Manager() = delete;
121 Manager(const Manager&) = delete;
122 Manager(Manager&&) = delete;
123 Manager& operator=(const Manager&) = delete;
124 Manager& operator=(Manager&&) = delete;
125 ~Manager() = default;
126
127 /**
128 * Constructor
129 * Parses and populates the fan control manager attributes from a json file
130 *
131 * @param[in] bus - sdbusplus bus object
Matthew Barth06764942021-03-04 09:28:40 -0600132 * @param[in] event - sdeventplus event loop
Matthew Bartha227a162020-08-05 10:51:45 -0500133 */
Matthew Barth06764942021-03-04 09:28:40 -0600134 Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event);
Matthew Bartha227a162020-08-05 10:51:45 -0500135
136 /**
Matthew Barthacd737c2021-03-04 11:04:01 -0600137 * @brief Get the active profiles of the system where an empty list
138 * represents that only configuration entries without a profile defined will
139 * be loaded.
140 *
141 * @return - The list of active profiles
142 */
143 static const std::vector<std::string>& getActiveProfiles();
144
145 /**
146 * @brief Load the configuration of a given JSON class object based on the
147 * active profiles
148 *
Matthew Barthacd737c2021-03-04 11:04:01 -0600149 * @param[in] isOptional - JSON configuration file is optional or not
Matthew Barth603ef162021-03-24 15:34:53 -0500150 * @param[in] bus - The sdbusplus bus object
151 * @param[in] args - Arguments to be forwarded to each instance of `T`
Matthew Barthacd737c2021-03-04 11:04:01 -0600152 *
153 * @return Map of configuration entries
154 * Map of configuration keys to their corresponding configuration object
155 */
Matthew Barth603ef162021-03-24 15:34:53 -0500156 template <typename T, typename... Args>
Matthew Barthacd737c2021-03-04 11:04:01 -0600157 static std::map<configKey, std::unique_ptr<T>>
Matthew Barth603ef162021-03-24 15:34:53 -0500158 getConfig(bool isOptional, sdbusplus::bus::bus& bus, Args&&... args)
Matthew Barthacd737c2021-03-04 11:04:01 -0600159 {
160 std::map<configKey, std::unique_ptr<T>> config;
161
162 auto confFile = fan::JsonConfig::getConfFile(
163 bus, confAppName, T::confFileName, isOptional);
164 if (!confFile.empty())
165 {
166 for (const auto& entry : fan::JsonConfig::load(confFile))
167 {
168 if (entry.contains("profiles"))
169 {
170 std::vector<std::string> profiles;
171 for (const auto& profile : entry["profiles"])
172 {
173 profiles.emplace_back(
174 profile.template get<std::string>());
175 }
176 // Do not create the object if its profiles are not in the
177 // list of active profiles
Matthew Barth42428052021-03-30 12:50:59 -0500178 if (!profiles.empty() &&
179 !std::any_of(profiles.begin(), profiles.end(),
Matthew Barthacd737c2021-03-04 11:04:01 -0600180 [](const auto& name) {
181 return std::find(
182 getActiveProfiles().begin(),
183 getActiveProfiles().end(),
184 name) !=
185 getActiveProfiles().end();
186 }))
187 {
188 continue;
189 }
190 }
Matthew Barth603ef162021-03-24 15:34:53 -0500191 auto obj =
192 std::make_unique<T>(entry, std::forward<Args>(args)...);
Matthew Barthacd737c2021-03-04 11:04:01 -0600193 config.emplace(
194 std::make_pair(obj->getName(), obj->getProfiles()),
195 std::move(obj));
196 }
197 }
198 return config;
199 }
200
201 /**
Matthew Barth0206c722021-03-30 15:20:05 -0500202 * @brief Check if the given input configuration key matches with another
203 * configuration key that it's to be included in
204 *
205 * @param[in] input - Config key to be included in another config object
206 * @param[in] comp - Config key of the config object to compare with
207 *
208 * @return Whether the configuration object should be included
209 */
210 static bool inConfig(const configKey& input, const configKey& comp);
211
212 /**
Matthew Barth12cb1252021-03-08 16:47:30 -0600213 * @brief Check if the given path and inteface is owned by a dbus service
214 *
215 * @param[in] path - Dbus object path
216 * @param[in] intf - Dbus object interface
217 *
218 * @return - Whether the service has an owner for the given object path and
219 * interface
220 */
221 static bool hasOwner(const std::string& path, const std::string& intf);
222
223 /**
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500224 * @brief Sets the dbus service owner state of a given object
225 *
226 * @param[in] path - Dbus object path
227 * @param[in] serv - Dbus service name
228 * @param[in] intf - Dbus object interface
229 * @param[in] isOwned - Dbus service owner state
230 */
231 void setOwner(const std::string& path, const std::string& serv,
232 const std::string& intf, bool isOwned);
233
234 /**
235 * @brief Add a set of services for a path and interface by retrieving all
236 * the path subtrees to the given depth from root for the interface
237 *
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500238 * @param[in] intf - Interface to add services for
239 * @param[in] depth - Depth of tree traversal from root path
240 *
241 * @throws - DBusMethodError
242 * Throws a DBusMethodError when the `getSubTree` method call fails
243 */
Matthew Barth98f6fc12021-04-16 10:48:37 -0500244 static void addServices(const std::string& intf, int32_t depth);
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500245
246 /**
247 * @brief Get the service for a given path and interface from cached
248 * dataset and attempt to add all the services for the given path/interface
249 * when it's not found
250 *
251 * @param[in] path - Path to get service for
252 * @param[in] intf - Interface to get service for
253 *
254 * @return - The now cached service name
255 *
256 * @throws - DBusMethodError
257 * Ripples up a DBusMethodError exception from calling addServices
258 */
259 static const std::string& getService(const std::string& path,
260 const std::string& intf);
261
262 /**
Matthew Barthf41e9472021-05-13 16:38:06 -0500263 * @brief Get all the object paths for a given service and interface from
264 * the cached dataset and try to add all the services for the given
265 * interface when no paths are found and then attempt to get all the object
266 * paths again
267 *
268 * @param[in] serv - Service name to get paths for
269 * @param[in] intf - Interface to get paths for
270 *
271 * @return The cached object paths
272 */
273 std::vector<std::string> getPaths(const std::string& serv,
274 const std::string& intf);
275
276 /**
277 * @brief Add objects to the cached dataset by first using
278 * `getManagedObjects` for the same service providing the given path and
279 * interface or just add the single object of the given path, interface, and
280 * property if that fails.
281 *
282 * @param[in] path - Dbus object's path
283 * @param[in] intf - Dbus object's interface
284 * @param[in] prop - Dbus object's property
285 *
286 * @throws - DBusMethodError
287 * Throws a DBusMethodError when the the service is failed to be found or
288 * when the `getManagedObjects` method call fails
289 */
290 void addObjects(const std::string& path, const std::string& intf,
291 const std::string& prop);
292
293 /**
294 * @brief Get an object's property value
295 *
296 * @param[in] path - Dbus object's path
297 * @param[in] intf - Dbus object's interface
298 * @param[in] prop - Dbus object's property
299 */
300 const std::optional<PropertyVariantType>
301 getProperty(const std::string& path, const std::string& intf,
302 const std::string& prop);
303
304 /**
Matthew Barthbaeeb8f2021-05-13 16:03:54 -0500305 * @brief Set/update an object's property value
306 *
307 * @param[in] path - Dbus object's path
308 * @param[in] intf - Dbus object's interface
309 * @param[in] prop - Dbus object's property
310 * @param[in] value - Dbus object's property value
311 */
312 void setProperty(const std::string& path, const std::string& intf,
313 const std::string& prop, PropertyVariantType value)
314 {
315 _objects[path][intf][prop] = std::move(value);
316 }
317
318 /**
Matthew Barthc2691402021-05-13 16:20:32 -0500319 * @brief Remove an object's interface
320 *
321 * @param[in] path - Dbus object's path
322 * @param[in] intf - Dbus object's interface
323 */
324 inline void removeInterface(const std::string& path,
325 const std::string& intf)
326 {
327 auto itPath = _objects.find(path);
328 if (itPath != std::end(_objects))
329 {
330 _objects[path].erase(intf);
331 }
332 }
333
334 /**
Matthew Barth07fecfc2021-01-29 09:04:43 -0600335 * @brief Get the object's property value as a variant
336 *
337 * @param[in] path - Path of the object containing the property
338 * @param[in] intf - Interface name containing the property
339 * @param[in] prop - Name of property
340 *
341 * @return - The object's property value as a variant
342 */
343 static inline auto getObjValueVariant(const std::string& path,
344 const std::string& intf,
345 const std::string& prop)
346 {
347 return _objects.at(path).at(intf).at(prop);
348 };
349
350 /**
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500351 * @brief Add a dbus timer
352 *
353 * @param[in] type - Type of timer
354 * @param[in] interval - Timer interval in microseconds
355 * @param[in] pkg - Packaged data for when timer expires
356 */
357 void addTimer(const TimerType type,
358 const std::chrono::microseconds interval,
359 std::unique_ptr<TimerPkg> pkg);
360
361 /**
362 * @brief Callback when a timer expires
363 *
364 * @param[in] data - Data to be used when the timer expired
365 */
366 void timerExpired(TimerData& data);
367
368 /**
Matthew Barthebabc042021-05-13 15:38:58 -0500369 * @brief Get the signal data for a given match string
370 *
371 * @param[in] sigMatch - Signal match string
372 *
373 * @return - Reference to the signal data for the given match string
374 */
375 std::vector<SignalData>& getSignal(const std::string& sigMatch)
376 {
377 return _signals[sigMatch];
378 }
379
380 /**
381 * @brief Handle receiving signals
382 *
383 * @param[in] msg - Signal message containing the signal's data
384 * @param[in] pkgs - Signal packages associated to the signal being handled
385 */
386 void handleSignal(sdbusplus::message::message& msg,
387 const std::vector<SignalPkg>* pkgs);
388
389 /**
Matthew Bartheebde062021-04-14 12:48:52 -0500390 * @brief Get the sdbusplus bus object
391 */
392 inline auto& getBus()
393 {
394 return _bus;
395 }
396
397 /**
Matthew Bartha227a162020-08-05 10:51:45 -0500398 * @brief Get the configured power on delay(OPTIONAL)
399 *
400 * @return Power on delay in seconds
401 * Configured power on delay in seconds, otherwise 0
402 */
403 unsigned int getPowerOnDelay();
404
405 private:
406 /* JSON file name for manager configuration attributes */
407 static constexpr auto confFileName = "manager.json";
408
409 /* The parsed JSON object */
410 json _jsonObj;
Matthew Barth06764942021-03-04 09:28:40 -0600411
Matthew Barthacd737c2021-03-04 11:04:01 -0600412 /**
413 * The sdbusplus bus object to use
414 */
415 sdbusplus::bus::bus& _bus;
416
417 /**
418 * The sdeventplus even loop to use
419 */
420 sdeventplus::Event _event;
421
Matthew Barth06764942021-03-04 09:28:40 -0600422 /* List of profiles configured */
Matthew Barthacd737c2021-03-04 11:04:01 -0600423 std::map<configKey, std::unique_ptr<Profile>> _profiles;
Matthew Barth06764942021-03-04 09:28:40 -0600424
425 /* List of active profiles */
Matthew Barthacd737c2021-03-04 11:04:01 -0600426 static std::vector<std::string> _activeProfiles;
427
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500428 /* Subtree map of paths to services of interfaces(with ownership state) */
429 static std::map<
430 std::string,
431 std::map<std::string, std::pair<bool, std::vector<std::string>>>>
Matthew Barth12cb1252021-03-08 16:47:30 -0600432 _servTree;
433
Matthew Barth07fecfc2021-01-29 09:04:43 -0600434 /* Object map of paths to interfaces of properties and their values */
435 static std::map<
436 std::string,
437 std::map<std::string, std::map<std::string, PropertyVariantType>>>
438 _objects;
439
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500440 /* List of timers and their data to be processed when expired */
441 std::vector<std::pair<std::unique_ptr<TimerData>, Timer>> _timers;
442
Matthew Barthebabc042021-05-13 15:38:58 -0500443 /* Map of signal match strings to a list of signal handler data */
444 std::unordered_map<std::string, std::vector<SignalData>> _signals;
445
Matthew Barthacd737c2021-03-04 11:04:01 -0600446 /* List of zones configured */
447 std::map<configKey, std::unique_ptr<Zone>> _zones;
448
Matthew Barth44ab7692021-03-26 11:40:10 -0500449 /* List of events configured */
450 std::map<configKey, std::unique_ptr<Event>> _events;
451
Matthew Barthacd737c2021-03-04 11:04:01 -0600452 /**
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500453 * @brief Find the service name for a given path and interface from the
454 * cached dataset
455 *
456 * @param[in] path - Path to get service for
457 * @param[in] intf - Interface to get service for
458 *
459 * @return - The cached service name
460 */
461 static const std::string& findService(const std::string& path,
462 const std::string& intf);
463
464 /**
Matthew Barthf41e9472021-05-13 16:38:06 -0500465 * @brief Find all the paths for a given service and interface from the
466 * cached dataset
467 *
468 * @param[in] serv - Service name to get paths for
469 * @param[in] intf - Interface to get paths for
470 *
471 * @return - The cached object paths
472 */
473 std::vector<std::string> findPaths(const std::string& serv,
474 const std::string& intf);
475
476 /**
Matthew Barthacd737c2021-03-04 11:04:01 -0600477 * @brief Parse and set the configured profiles from the profiles JSON file
478 *
479 * Retrieves the optional profiles JSON configuration file, parses it, and
480 * creates a list of configured profiles available to the other
481 * configuration files. These profiles can be used to remove or include
482 * entries within the other configuration files.
483 */
484 void setProfiles();
Matthew Bartha227a162020-08-05 10:51:45 -0500485};
486
487} // namespace phosphor::fan::control::json