Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 1 | /** |
| 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 Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 18 | #include "action.hpp" |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 19 | #include "config_base.hpp" |
| 20 | #include "event.hpp" |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 21 | #include "group.hpp" |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 22 | #include "json_config.hpp" |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 23 | #include "profile.hpp" |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 24 | #include "zone.hpp" |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 25 | |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 26 | #include <nlohmann/json.hpp> |
| 27 | #include <sdbusplus/bus.hpp> |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 28 | #include <sdeventplus/event.hpp> |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 29 | #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 Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 38 | |
| 39 | namespace phosphor::fan::control::json |
| 40 | { |
| 41 | |
| 42 | using json = nlohmann::json; |
| 43 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 44 | /* Application name to be appended to the path for loading a JSON config file */ |
| 45 | constexpr auto confAppName = "control"; |
| 46 | |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 47 | /* Type of timers supported */ |
| 48 | enum 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 Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 57 | * std::vector<std::unique_ptr<ActionBase>> = List of pointers to actions |
| 58 | * that run when the timer expires |
| 59 | */ |
Matthew Barth | 00f6aa0 | 2021-04-09 10:49:47 -0500 | [diff] [blame] | 60 | using TimerPkg = |
| 61 | std::tuple<std::string, std::vector<std::unique_ptr<ActionBase>>&>; |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 62 | /** |
| 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 | */ |
| 68 | using TimerData = std::pair<TimerType, TimerPkg>; |
| 69 | /* Dbus event timer */ |
| 70 | using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>; |
| 71 | |
Matthew Barth | ebabc04 | 2021-05-13 15:38:58 -0500 | [diff] [blame] | 72 | /* Dbus signal object */ |
| 73 | constexpr auto Path = 0; |
| 74 | constexpr auto Intf = 1; |
| 75 | constexpr auto Prop = 2; |
| 76 | using SignalObject = std::tuple<std::string, std::string, std::string>; |
| 77 | /* Dbus signal actions */ |
| 78 | using 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 | */ |
| 84 | using 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 | */ |
| 93 | using 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 | */ |
| 102 | using SignalData = std::tuple<std::unique_ptr<std::vector<SignalPkg>>, |
| 103 | std::unique_ptr<sdbusplus::server::match::match>>; |
| 104 | |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 105 | /** |
| 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 | */ |
| 117 | class 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 Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 132 | * @param[in] event - sdeventplus event loop |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 133 | */ |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 134 | Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event); |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 135 | |
| 136 | /** |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 137 | * @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 Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 149 | * @param[in] isOptional - JSON configuration file is optional or not |
Matthew Barth | 603ef16 | 2021-03-24 15:34:53 -0500 | [diff] [blame] | 150 | * @param[in] bus - The sdbusplus bus object |
| 151 | * @param[in] args - Arguments to be forwarded to each instance of `T` |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 152 | * |
| 153 | * @return Map of configuration entries |
| 154 | * Map of configuration keys to their corresponding configuration object |
| 155 | */ |
Matthew Barth | 603ef16 | 2021-03-24 15:34:53 -0500 | [diff] [blame] | 156 | template <typename T, typename... Args> |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 157 | static std::map<configKey, std::unique_ptr<T>> |
Matthew Barth | 603ef16 | 2021-03-24 15:34:53 -0500 | [diff] [blame] | 158 | getConfig(bool isOptional, sdbusplus::bus::bus& bus, Args&&... args) |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 159 | { |
| 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 Barth | 4242805 | 2021-03-30 12:50:59 -0500 | [diff] [blame] | 178 | if (!profiles.empty() && |
| 179 | !std::any_of(profiles.begin(), profiles.end(), |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 180 | [](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 Barth | 603ef16 | 2021-03-24 15:34:53 -0500 | [diff] [blame] | 191 | auto obj = |
| 192 | std::make_unique<T>(entry, std::forward<Args>(args)...); |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 193 | config.emplace( |
| 194 | std::make_pair(obj->getName(), obj->getProfiles()), |
| 195 | std::move(obj)); |
| 196 | } |
| 197 | } |
| 198 | return config; |
| 199 | } |
| 200 | |
| 201 | /** |
Matthew Barth | 0206c72 | 2021-03-30 15:20:05 -0500 | [diff] [blame] | 202 | * @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 Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 213 | * @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 Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 224 | * @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 Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 238 | * @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 Barth | 98f6fc1 | 2021-04-16 10:48:37 -0500 | [diff] [blame] | 244 | static void addServices(const std::string& intf, int32_t depth); |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 245 | |
| 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 Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame^] | 263 | * @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 Barth | 07fecfc | 2021-01-29 09:04:43 -0600 | [diff] [blame] | 277 | * @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 Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 293 | * @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 Barth | ebabc04 | 2021-05-13 15:38:58 -0500 | [diff] [blame] | 311 | * @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 Barth | eebde06 | 2021-04-14 12:48:52 -0500 | [diff] [blame] | 332 | * @brief Get the sdbusplus bus object |
| 333 | */ |
| 334 | inline auto& getBus() |
| 335 | { |
| 336 | return _bus; |
| 337 | } |
| 338 | |
| 339 | /** |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 340 | * @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 Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 353 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 354 | /** |
| 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 Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 364 | /* List of profiles configured */ |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 365 | std::map<configKey, std::unique_ptr<Profile>> _profiles; |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 366 | |
| 367 | /* List of active profiles */ |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 368 | static std::vector<std::string> _activeProfiles; |
| 369 | |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 370 | /* 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 Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 374 | _servTree; |
| 375 | |
Matthew Barth | 07fecfc | 2021-01-29 09:04:43 -0600 | [diff] [blame] | 376 | /* 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 Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 382 | /* List of timers and their data to be processed when expired */ |
| 383 | std::vector<std::pair<std::unique_ptr<TimerData>, Timer>> _timers; |
| 384 | |
Matthew Barth | ebabc04 | 2021-05-13 15:38:58 -0500 | [diff] [blame] | 385 | /* Map of signal match strings to a list of signal handler data */ |
| 386 | std::unordered_map<std::string, std::vector<SignalData>> _signals; |
| 387 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 388 | /* List of zones configured */ |
| 389 | std::map<configKey, std::unique_ptr<Zone>> _zones; |
| 390 | |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 391 | /* List of events configured */ |
| 392 | std::map<configKey, std::unique_ptr<Event>> _events; |
| 393 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 394 | /** |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 395 | * @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 Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 407 | * @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 Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 415 | }; |
| 416 | |
| 417 | } // namespace phosphor::fan::control::json |