blob: 294ba7d06e1688f7a50e7a9488507f558e5f0fbf [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 Barthbaeeb8f2021-05-13 16:03:54 -0500263 * @brief Set/update an object's property value
264 *
265 * @param[in] path - Dbus object's path
266 * @param[in] intf - Dbus object's interface
267 * @param[in] prop - Dbus object's property
268 * @param[in] value - Dbus object's property value
269 */
270 void setProperty(const std::string& path, const std::string& intf,
271 const std::string& prop, PropertyVariantType value)
272 {
273 _objects[path][intf][prop] = std::move(value);
274 }
275
276 /**
Matthew Barth07fecfc2021-01-29 09:04:43 -0600277 * @brief Get the object's property value as a variant
278 *
279 * @param[in] path - Path of the object containing the property
280 * @param[in] intf - Interface name containing the property
281 * @param[in] prop - Name of property
282 *
283 * @return - The object's property value as a variant
284 */
285 static inline auto getObjValueVariant(const std::string& path,
286 const std::string& intf,
287 const std::string& prop)
288 {
289 return _objects.at(path).at(intf).at(prop);
290 };
291
292 /**
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500293 * @brief Add a dbus timer
294 *
295 * @param[in] type - Type of timer
296 * @param[in] interval - Timer interval in microseconds
297 * @param[in] pkg - Packaged data for when timer expires
298 */
299 void addTimer(const TimerType type,
300 const std::chrono::microseconds interval,
301 std::unique_ptr<TimerPkg> pkg);
302
303 /**
304 * @brief Callback when a timer expires
305 *
306 * @param[in] data - Data to be used when the timer expired
307 */
308 void timerExpired(TimerData& data);
309
310 /**
Matthew Barthebabc042021-05-13 15:38:58 -0500311 * @brief Get the signal data for a given match string
312 *
313 * @param[in] sigMatch - Signal match string
314 *
315 * @return - Reference to the signal data for the given match string
316 */
317 std::vector<SignalData>& getSignal(const std::string& sigMatch)
318 {
319 return _signals[sigMatch];
320 }
321
322 /**
323 * @brief Handle receiving signals
324 *
325 * @param[in] msg - Signal message containing the signal's data
326 * @param[in] pkgs - Signal packages associated to the signal being handled
327 */
328 void handleSignal(sdbusplus::message::message& msg,
329 const std::vector<SignalPkg>* pkgs);
330
331 /**
Matthew Bartheebde062021-04-14 12:48:52 -0500332 * @brief Get the sdbusplus bus object
333 */
334 inline auto& getBus()
335 {
336 return _bus;
337 }
338
339 /**
Matthew Bartha227a162020-08-05 10:51:45 -0500340 * @brief Get the configured power on delay(OPTIONAL)
341 *
342 * @return Power on delay in seconds
343 * Configured power on delay in seconds, otherwise 0
344 */
345 unsigned int getPowerOnDelay();
346
347 private:
348 /* JSON file name for manager configuration attributes */
349 static constexpr auto confFileName = "manager.json";
350
351 /* The parsed JSON object */
352 json _jsonObj;
Matthew Barth06764942021-03-04 09:28:40 -0600353
Matthew Barthacd737c2021-03-04 11:04:01 -0600354 /**
355 * The sdbusplus bus object to use
356 */
357 sdbusplus::bus::bus& _bus;
358
359 /**
360 * The sdeventplus even loop to use
361 */
362 sdeventplus::Event _event;
363
Matthew Barth06764942021-03-04 09:28:40 -0600364 /* List of profiles configured */
Matthew Barthacd737c2021-03-04 11:04:01 -0600365 std::map<configKey, std::unique_ptr<Profile>> _profiles;
Matthew Barth06764942021-03-04 09:28:40 -0600366
367 /* List of active profiles */
Matthew Barthacd737c2021-03-04 11:04:01 -0600368 static std::vector<std::string> _activeProfiles;
369
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500370 /* Subtree map of paths to services of interfaces(with ownership state) */
371 static std::map<
372 std::string,
373 std::map<std::string, std::pair<bool, std::vector<std::string>>>>
Matthew Barth12cb1252021-03-08 16:47:30 -0600374 _servTree;
375
Matthew Barth07fecfc2021-01-29 09:04:43 -0600376 /* Object map of paths to interfaces of properties and their values */
377 static std::map<
378 std::string,
379 std::map<std::string, std::map<std::string, PropertyVariantType>>>
380 _objects;
381
Matthew Barthd9cb63b2021-03-24 14:36:49 -0500382 /* List of timers and their data to be processed when expired */
383 std::vector<std::pair<std::unique_ptr<TimerData>, Timer>> _timers;
384
Matthew Barthebabc042021-05-13 15:38:58 -0500385 /* Map of signal match strings to a list of signal handler data */
386 std::unordered_map<std::string, std::vector<SignalData>> _signals;
387
Matthew Barthacd737c2021-03-04 11:04:01 -0600388 /* List of zones configured */
389 std::map<configKey, std::unique_ptr<Zone>> _zones;
390
Matthew Barth44ab7692021-03-26 11:40:10 -0500391 /* List of events configured */
392 std::map<configKey, std::unique_ptr<Event>> _events;
393
Matthew Barthacd737c2021-03-04 11:04:01 -0600394 /**
Matthew Barth4ca87fa2021-04-14 11:31:13 -0500395 * @brief Find the service name for a given path and interface from the
396 * cached dataset
397 *
398 * @param[in] path - Path to get service for
399 * @param[in] intf - Interface to get service for
400 *
401 * @return - The cached service name
402 */
403 static const std::string& findService(const std::string& path,
404 const std::string& intf);
405
406 /**
Matthew Barthacd737c2021-03-04 11:04:01 -0600407 * @brief Parse and set the configured profiles from the profiles JSON file
408 *
409 * Retrieves the optional profiles JSON configuration file, parses it, and
410 * creates a list of configured profiles available to the other
411 * configuration files. These profiles can be used to remove or include
412 * entries within the other configuration files.
413 */
414 void setProfiles();
Matthew Bartha227a162020-08-05 10:51:45 -0500415};
416
417} // namespace phosphor::fan::control::json