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 | 48f44da | 2021-05-27 15:43:34 -0500 | [diff] [blame] | 23 | #include "power_state.hpp" |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 24 | #include "profile.hpp" |
Matthew Barth | 9403a21 | 2021-05-17 09:31:50 -0500 | [diff] [blame] | 25 | #include "sdbusplus.hpp" |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 26 | #include "zone.hpp" |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 27 | |
Matthew Barth | 68ac004 | 2021-06-01 15:38:36 -0500 | [diff] [blame] | 28 | #include <fmt/format.h> |
| 29 | |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 30 | #include <nlohmann/json.hpp> |
Matthew Barth | 68ac004 | 2021-06-01 15:38:36 -0500 | [diff] [blame] | 31 | #include <phosphor-logging/log.hpp> |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 32 | #include <sdbusplus/bus.hpp> |
Matthew Barth | 1542fb5 | 2021-06-10 14:09:09 -0500 | [diff] [blame] | 33 | #include <sdbusplus/server/manager.hpp> |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 34 | #include <sdeventplus/event.hpp> |
Matt Spinler | 2fc0a35 | 2021-10-04 15:10:57 -0500 | [diff] [blame] | 35 | #include <sdeventplus/source/event.hpp> |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 36 | #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 Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 45 | |
| 46 | namespace phosphor::fan::control::json |
| 47 | { |
| 48 | |
| 49 | using json = nlohmann::json; |
Matthew Barth | 68ac004 | 2021-06-01 15:38:36 -0500 | [diff] [blame] | 50 | using namespace phosphor::logging; |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 51 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 52 | /* Application name to be appended to the path for loading a JSON config file */ |
| 53 | constexpr auto confAppName = "control"; |
| 54 | |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 55 | /* Type of timers supported */ |
| 56 | enum class TimerType |
| 57 | { |
| 58 | oneshot, |
| 59 | repeating, |
| 60 | }; |
| 61 | /** |
| 62 | * Package of data required when a timer expires |
| 63 | * Tuple constructed of: |
| 64 | * std::string = Timer package unique identifier |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 65 | * std::vector<std::unique_ptr<ActionBase>> = List of pointers to actions |
| 66 | * that run when the timer expires |
| 67 | */ |
Matthew Barth | 00f6aa0 | 2021-04-09 10:49:47 -0500 | [diff] [blame] | 68 | using TimerPkg = |
| 69 | std::tuple<std::string, std::vector<std::unique_ptr<ActionBase>>&>; |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 70 | /** |
| 71 | * Data associated with a running timer that's used when it expires |
| 72 | * Pair constructed of: |
| 73 | * TimerType = Type of timer to manage expired timer instances |
| 74 | * TimerPkg = Package of data required when the timer expires |
| 75 | */ |
| 76 | using TimerData = std::pair<TimerType, TimerPkg>; |
| 77 | /* Dbus event timer */ |
| 78 | using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>; |
| 79 | |
Matthew Barth | ebabc04 | 2021-05-13 15:38:58 -0500 | [diff] [blame] | 80 | /* Dbus signal object */ |
| 81 | constexpr auto Path = 0; |
| 82 | constexpr auto Intf = 1; |
| 83 | constexpr auto Prop = 2; |
| 84 | using SignalObject = std::tuple<std::string, std::string, std::string>; |
| 85 | /* Dbus signal actions */ |
Matthew Barth | 039f91e | 2021-10-04 15:18:07 -0500 | [diff] [blame] | 86 | using SignalActions = std::vector<std::unique_ptr<ActionBase>>&; |
Matthew Barth | ebabc04 | 2021-05-13 15:38:58 -0500 | [diff] [blame] | 87 | /** |
| 88 | * Signal handler function that handles parsing a signal's message for a |
| 89 | * particular signal object and stores the results in the manager |
| 90 | */ |
| 91 | using SignalHandler = std::function<bool(sdbusplus::message::message&, |
| 92 | const SignalObject&, Manager&)>; |
| 93 | /** |
| 94 | * Package of data required when a signal is received |
| 95 | * Tuple constructed of: |
| 96 | * SignalHandler = Signal handler function |
| 97 | * SignalObject = Dbus signal object |
| 98 | * SignalActions = List of actions that are run when the signal is received |
| 99 | */ |
| 100 | using SignalPkg = std::tuple<SignalHandler, SignalObject, SignalActions>; |
| 101 | /** |
| 102 | * Data associated to a subscribed signal |
| 103 | * Tuple constructed of: |
Matthew Barth | fac8a2f | 2021-06-10 15:50:36 -0500 | [diff] [blame] | 104 | * std::vector<SignalPkg> = List of the signal's packages |
Matthew Barth | ebabc04 | 2021-05-13 15:38:58 -0500 | [diff] [blame] | 105 | * std::unique_ptr<sdbusplus::server::match::match> = |
| 106 | * Pointer to match holding the subscription to a signal |
| 107 | */ |
Matthew Barth | fac8a2f | 2021-06-10 15:50:36 -0500 | [diff] [blame] | 108 | using SignalData = std::tuple<std::vector<SignalPkg>, |
Matthew Barth | ebabc04 | 2021-05-13 15:38:58 -0500 | [diff] [blame] | 109 | std::unique_ptr<sdbusplus::server::match::match>>; |
| 110 | |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 111 | /** |
| 112 | * @class Manager - Represents the fan control manager's configuration |
| 113 | * |
| 114 | * A fan control manager configuration is optional, therefore the "manager.json" |
| 115 | * file is also optional. The manager configuration is used to populate |
| 116 | * fan control's manager parameters which are used in how the application |
| 117 | * operates, not in how the fans are controlled. |
| 118 | * |
| 119 | * When no manager configuration exists, the fan control application starts, |
| 120 | * processes any configured events and then begins controlling fans according |
| 121 | * to those events. |
| 122 | */ |
| 123 | class Manager |
| 124 | { |
| 125 | public: |
| 126 | Manager() = delete; |
| 127 | Manager(const Manager&) = delete; |
| 128 | Manager(Manager&&) = delete; |
| 129 | Manager& operator=(const Manager&) = delete; |
| 130 | Manager& operator=(Manager&&) = delete; |
| 131 | ~Manager() = default; |
| 132 | |
| 133 | /** |
| 134 | * Constructor |
| 135 | * Parses and populates the fan control manager attributes from a json file |
| 136 | * |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 137 | * @param[in] event - sdeventplus event loop |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 138 | */ |
Matthew Barth | 9403a21 | 2021-05-17 09:31:50 -0500 | [diff] [blame] | 139 | Manager(const sdeventplus::Event& event); |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 140 | |
| 141 | /** |
Matthew Barth | e91ac86 | 2021-05-25 16:22:17 -0500 | [diff] [blame] | 142 | * @brief Callback function to handle receiving a HUP signal to reload the |
| 143 | * JSON configurations. |
| 144 | */ |
| 145 | void sighupHandler(sdeventplus::source::Signal&, |
| 146 | const struct signalfd_siginfo*); |
| 147 | |
| 148 | /** |
Matt Spinler | 2fc0a35 | 2021-10-04 15:10:57 -0500 | [diff] [blame] | 149 | * @brief Callback function to handle receiving a USR1 signal to dump |
| 150 | * the flight recorder. |
| 151 | */ |
| 152 | void sigUsr1Handler(sdeventplus::source::Signal&, |
| 153 | const struct signalfd_siginfo*); |
| 154 | |
| 155 | /** |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 156 | * @brief Get the active profiles of the system where an empty list |
| 157 | * represents that only configuration entries without a profile defined will |
| 158 | * be loaded. |
| 159 | * |
| 160 | * @return - The list of active profiles |
| 161 | */ |
| 162 | static const std::vector<std::string>& getActiveProfiles(); |
| 163 | |
| 164 | /** |
| 165 | * @brief Load the configuration of a given JSON class object based on the |
| 166 | * active profiles |
| 167 | * |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 168 | * @param[in] isOptional - JSON configuration file is optional or not |
Matthew Barth | 603ef16 | 2021-03-24 15:34:53 -0500 | [diff] [blame] | 169 | * @param[in] args - Arguments to be forwarded to each instance of `T` |
Matthew Barth | e6d1f78 | 2021-05-14 12:52:20 -0500 | [diff] [blame] | 170 | * (*Note that a sdbusplus bus object is required as the first argument) |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 171 | * |
| 172 | * @return Map of configuration entries |
| 173 | * Map of configuration keys to their corresponding configuration object |
| 174 | */ |
Matthew Barth | 603ef16 | 2021-03-24 15:34:53 -0500 | [diff] [blame] | 175 | template <typename T, typename... Args> |
Matthew Barth | e6d1f78 | 2021-05-14 12:52:20 -0500 | [diff] [blame] | 176 | static std::map<configKey, std::unique_ptr<T>> getConfig(bool isOptional, |
| 177 | Args&&... args) |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 178 | { |
| 179 | std::map<configKey, std::unique_ptr<T>> config; |
| 180 | |
Matthew Barth | 9403a21 | 2021-05-17 09:31:50 -0500 | [diff] [blame] | 181 | auto confFile = |
| 182 | fan::JsonConfig::getConfFile(util::SDBusPlus::getBus(), confAppName, |
| 183 | T::confFileName, isOptional); |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 184 | if (!confFile.empty()) |
| 185 | { |
| 186 | for (const auto& entry : fan::JsonConfig::load(confFile)) |
| 187 | { |
| 188 | if (entry.contains("profiles")) |
| 189 | { |
| 190 | std::vector<std::string> profiles; |
| 191 | for (const auto& profile : entry["profiles"]) |
| 192 | { |
| 193 | profiles.emplace_back( |
| 194 | profile.template get<std::string>()); |
| 195 | } |
| 196 | // Do not create the object if its profiles are not in the |
| 197 | // list of active profiles |
Matthew Barth | 4242805 | 2021-03-30 12:50:59 -0500 | [diff] [blame] | 198 | if (!profiles.empty() && |
| 199 | !std::any_of(profiles.begin(), profiles.end(), |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 200 | [](const auto& name) { |
| 201 | return std::find( |
| 202 | getActiveProfiles().begin(), |
| 203 | getActiveProfiles().end(), |
| 204 | name) != |
| 205 | getActiveProfiles().end(); |
| 206 | })) |
| 207 | { |
| 208 | continue; |
| 209 | } |
| 210 | } |
Matthew Barth | 603ef16 | 2021-03-24 15:34:53 -0500 | [diff] [blame] | 211 | auto obj = |
| 212 | std::make_unique<T>(entry, std::forward<Args>(args)...); |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 213 | config.emplace( |
| 214 | std::make_pair(obj->getName(), obj->getProfiles()), |
| 215 | std::move(obj)); |
| 216 | } |
Matthew Barth | 68ac004 | 2021-06-01 15:38:36 -0500 | [diff] [blame] | 217 | log<level::INFO>( |
| 218 | fmt::format("Configuration({}) loaded successfully", |
| 219 | T::confFileName) |
| 220 | .c_str()); |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 221 | } |
| 222 | return config; |
| 223 | } |
| 224 | |
| 225 | /** |
Matthew Barth | 0206c72 | 2021-03-30 15:20:05 -0500 | [diff] [blame] | 226 | * @brief Check if the given input configuration key matches with another |
| 227 | * configuration key that it's to be included in |
| 228 | * |
| 229 | * @param[in] input - Config key to be included in another config object |
| 230 | * @param[in] comp - Config key of the config object to compare with |
| 231 | * |
| 232 | * @return Whether the configuration object should be included |
| 233 | */ |
| 234 | static bool inConfig(const configKey& input, const configKey& comp); |
| 235 | |
| 236 | /** |
Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 237 | * @brief Check if the given path and inteface is owned by a dbus service |
| 238 | * |
| 239 | * @param[in] path - Dbus object path |
| 240 | * @param[in] intf - Dbus object interface |
| 241 | * |
| 242 | * @return - Whether the service has an owner for the given object path and |
| 243 | * interface |
| 244 | */ |
| 245 | static bool hasOwner(const std::string& path, const std::string& intf); |
| 246 | |
| 247 | /** |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 248 | * @brief Sets the dbus service owner state of a given object |
| 249 | * |
| 250 | * @param[in] path - Dbus object path |
| 251 | * @param[in] serv - Dbus service name |
| 252 | * @param[in] intf - Dbus object interface |
| 253 | * @param[in] isOwned - Dbus service owner state |
| 254 | */ |
| 255 | void setOwner(const std::string& path, const std::string& serv, |
| 256 | const std::string& intf, bool isOwned); |
| 257 | |
| 258 | /** |
| 259 | * @brief Add a set of services for a path and interface by retrieving all |
| 260 | * the path subtrees to the given depth from root for the interface |
| 261 | * |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 262 | * @param[in] intf - Interface to add services for |
| 263 | * @param[in] depth - Depth of tree traversal from root path |
| 264 | * |
| 265 | * @throws - DBusMethodError |
| 266 | * Throws a DBusMethodError when the `getSubTree` method call fails |
| 267 | */ |
Matthew Barth | 98f6fc1 | 2021-04-16 10:48:37 -0500 | [diff] [blame] | 268 | static void addServices(const std::string& intf, int32_t depth); |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 269 | |
| 270 | /** |
| 271 | * @brief Get the service for a given path and interface from cached |
| 272 | * dataset and attempt to add all the services for the given path/interface |
| 273 | * when it's not found |
| 274 | * |
| 275 | * @param[in] path - Path to get service for |
| 276 | * @param[in] intf - Interface to get service for |
| 277 | * |
| 278 | * @return - The now cached service name |
| 279 | * |
| 280 | * @throws - DBusMethodError |
| 281 | * Ripples up a DBusMethodError exception from calling addServices |
| 282 | */ |
| 283 | static const std::string& getService(const std::string& path, |
| 284 | const std::string& intf); |
| 285 | |
| 286 | /** |
Matthew Barth | f41e947 | 2021-05-13 16:38:06 -0500 | [diff] [blame] | 287 | * @brief Get all the object paths for a given service and interface from |
| 288 | * the cached dataset and try to add all the services for the given |
| 289 | * interface when no paths are found and then attempt to get all the object |
| 290 | * paths again |
| 291 | * |
| 292 | * @param[in] serv - Service name to get paths for |
| 293 | * @param[in] intf - Interface to get paths for |
| 294 | * |
| 295 | * @return The cached object paths |
| 296 | */ |
| 297 | std::vector<std::string> getPaths(const std::string& serv, |
| 298 | const std::string& intf); |
| 299 | |
| 300 | /** |
| 301 | * @brief Add objects to the cached dataset by first using |
| 302 | * `getManagedObjects` for the same service providing the given path and |
| 303 | * interface or just add the single object of the given path, interface, and |
| 304 | * property if that fails. |
| 305 | * |
| 306 | * @param[in] path - Dbus object's path |
| 307 | * @param[in] intf - Dbus object's interface |
| 308 | * @param[in] prop - Dbus object's property |
| 309 | * |
| 310 | * @throws - DBusMethodError |
| 311 | * Throws a DBusMethodError when the the service is failed to be found or |
| 312 | * when the `getManagedObjects` method call fails |
| 313 | */ |
| 314 | void addObjects(const std::string& path, const std::string& intf, |
| 315 | const std::string& prop); |
| 316 | |
| 317 | /** |
| 318 | * @brief Get an object's property value |
| 319 | * |
| 320 | * @param[in] path - Dbus object's path |
| 321 | * @param[in] intf - Dbus object's interface |
| 322 | * @param[in] prop - Dbus object's property |
| 323 | */ |
| 324 | const std::optional<PropertyVariantType> |
| 325 | getProperty(const std::string& path, const std::string& intf, |
| 326 | const std::string& prop); |
| 327 | |
| 328 | /** |
Matthew Barth | baeeb8f | 2021-05-13 16:03:54 -0500 | [diff] [blame] | 329 | * @brief Set/update an object's property value |
| 330 | * |
| 331 | * @param[in] path - Dbus object's path |
| 332 | * @param[in] intf - Dbus object's interface |
| 333 | * @param[in] prop - Dbus object's property |
| 334 | * @param[in] value - Dbus object's property value |
| 335 | */ |
| 336 | void setProperty(const std::string& path, const std::string& intf, |
| 337 | const std::string& prop, PropertyVariantType value) |
| 338 | { |
| 339 | _objects[path][intf][prop] = std::move(value); |
| 340 | } |
| 341 | |
| 342 | /** |
Matthew Barth | c269140 | 2021-05-13 16:20:32 -0500 | [diff] [blame] | 343 | * @brief Remove an object's interface |
| 344 | * |
| 345 | * @param[in] path - Dbus object's path |
| 346 | * @param[in] intf - Dbus object's interface |
| 347 | */ |
| 348 | inline void removeInterface(const std::string& path, |
| 349 | const std::string& intf) |
| 350 | { |
| 351 | auto itPath = _objects.find(path); |
| 352 | if (itPath != std::end(_objects)) |
| 353 | { |
| 354 | _objects[path].erase(intf); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | /** |
Matthew Barth | 07fecfc | 2021-01-29 09:04:43 -0600 | [diff] [blame] | 359 | * @brief Get the object's property value as a variant |
| 360 | * |
| 361 | * @param[in] path - Path of the object containing the property |
| 362 | * @param[in] intf - Interface name containing the property |
| 363 | * @param[in] prop - Name of property |
| 364 | * |
| 365 | * @return - The object's property value as a variant |
| 366 | */ |
| 367 | static inline auto getObjValueVariant(const std::string& path, |
| 368 | const std::string& intf, |
| 369 | const std::string& prop) |
| 370 | { |
| 371 | return _objects.at(path).at(intf).at(prop); |
| 372 | }; |
| 373 | |
| 374 | /** |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 375 | * @brief Add a dbus timer |
| 376 | * |
| 377 | * @param[in] type - Type of timer |
| 378 | * @param[in] interval - Timer interval in microseconds |
| 379 | * @param[in] pkg - Packaged data for when timer expires |
| 380 | */ |
| 381 | void addTimer(const TimerType type, |
| 382 | const std::chrono::microseconds interval, |
| 383 | std::unique_ptr<TimerPkg> pkg); |
| 384 | |
| 385 | /** |
| 386 | * @brief Callback when a timer expires |
| 387 | * |
| 388 | * @param[in] data - Data to be used when the timer expired |
| 389 | */ |
| 390 | void timerExpired(TimerData& data); |
| 391 | |
| 392 | /** |
Matthew Barth | ebabc04 | 2021-05-13 15:38:58 -0500 | [diff] [blame] | 393 | * @brief Get the signal data for a given match string |
| 394 | * |
| 395 | * @param[in] sigMatch - Signal match string |
| 396 | * |
| 397 | * @return - Reference to the signal data for the given match string |
| 398 | */ |
| 399 | std::vector<SignalData>& getSignal(const std::string& sigMatch) |
| 400 | { |
| 401 | return _signals[sigMatch]; |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * @brief Handle receiving signals |
| 406 | * |
| 407 | * @param[in] msg - Signal message containing the signal's data |
| 408 | * @param[in] pkgs - Signal packages associated to the signal being handled |
| 409 | */ |
| 410 | void handleSignal(sdbusplus::message::message& msg, |
Matthew Barth | fac8a2f | 2021-06-10 15:50:36 -0500 | [diff] [blame] | 411 | const std::vector<SignalPkg>& pkgs); |
Matthew Barth | ebabc04 | 2021-05-13 15:38:58 -0500 | [diff] [blame] | 412 | |
| 413 | /** |
Matthew Barth | eebde06 | 2021-04-14 12:48:52 -0500 | [diff] [blame] | 414 | * @brief Get the sdbusplus bus object |
| 415 | */ |
| 416 | inline auto& getBus() |
| 417 | { |
| 418 | return _bus; |
| 419 | } |
| 420 | |
| 421 | /** |
Matthew Barth | 48f44da | 2021-05-27 15:43:34 -0500 | [diff] [blame] | 422 | * @brief Is the power state on |
| 423 | * |
| 424 | * @return Current power state of the system |
| 425 | */ |
| 426 | inline bool isPowerOn() const |
| 427 | { |
| 428 | return _powerState->isPowerOn(); |
| 429 | } |
| 430 | |
Matthew Barth | 3770a1d | 2021-06-10 15:09:37 -0500 | [diff] [blame] | 431 | /** |
| 432 | * @brief Load all the fan control JSON configuration files |
| 433 | * |
| 434 | * This is where all the fan control JSON configuration files are parsed and |
| 435 | * loaded into their associated objects. Anything that needs to be done when |
| 436 | * the Manager object is constructed or handling a SIGHUP to reload the |
| 437 | * configurations needs to be done here. |
| 438 | */ |
| 439 | void load(); |
| 440 | |
Matt Spinler | d76351b | 2021-08-05 16:23:09 -0500 | [diff] [blame] | 441 | /** |
| 442 | * @brief Sets a value in the parameter map. |
| 443 | * |
| 444 | * @param[in] name - The parameter name |
| 445 | * @param[in] value - The parameter value |
| 446 | */ |
| 447 | static void setParameter(const std::string& name, |
| 448 | const PropertyVariantType& value) |
| 449 | { |
| 450 | _parameters[name] = value; |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * @brief Returns a value from the parameter map |
| 455 | * |
| 456 | * @param[in] name - The parameter name |
| 457 | * |
| 458 | * @return The parameter value, or std::nullopt if not found |
| 459 | */ |
| 460 | static std::optional<PropertyVariantType> |
| 461 | getParameter(const std::string& name) |
| 462 | { |
| 463 | auto it = _parameters.find(name); |
| 464 | if (it != _parameters.end()) |
| 465 | { |
| 466 | return it->second; |
| 467 | } |
| 468 | |
| 469 | return std::nullopt; |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * @brief Deletes a parameter from the parameter map |
| 474 | * |
| 475 | * @param[in] name - The parameter name |
| 476 | */ |
| 477 | static void deleteParameter(const std::string& name) |
| 478 | { |
| 479 | _parameters.erase(name); |
| 480 | } |
| 481 | |
Matt Spinler | 7787def | 2021-10-14 16:33:16 -0500 | [diff] [blame] | 482 | /* The name of the dump file */ |
| 483 | static const std::string dumpFile; |
| 484 | |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 485 | private: |
Matthew Barth | 1542fb5 | 2021-06-10 14:09:09 -0500 | [diff] [blame] | 486 | /* The sdbusplus bus object to use */ |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 487 | sdbusplus::bus::bus& _bus; |
| 488 | |
Matthew Barth | 1542fb5 | 2021-06-10 14:09:09 -0500 | [diff] [blame] | 489 | /* The sdeventplus even loop to use */ |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 490 | sdeventplus::Event _event; |
| 491 | |
Matthew Barth | 1542fb5 | 2021-06-10 14:09:09 -0500 | [diff] [blame] | 492 | /* The sdbusplus manager object to set the ObjectManager interface */ |
| 493 | sdbusplus::server::manager::manager _mgr; |
| 494 | |
Matthew Barth | 3770a1d | 2021-06-10 15:09:37 -0500 | [diff] [blame] | 495 | /* Whether loading the config files is allowed or not */ |
| 496 | bool _loadAllowed; |
| 497 | |
Matthew Barth | 48f44da | 2021-05-27 15:43:34 -0500 | [diff] [blame] | 498 | /* The system's power state determination object */ |
| 499 | std::unique_ptr<PowerState> _powerState; |
| 500 | |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 501 | /* List of profiles configured */ |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 502 | std::map<configKey, std::unique_ptr<Profile>> _profiles; |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 503 | |
| 504 | /* List of active profiles */ |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 505 | static std::vector<std::string> _activeProfiles; |
| 506 | |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 507 | /* Subtree map of paths to services of interfaces(with ownership state) */ |
| 508 | static std::map< |
| 509 | std::string, |
| 510 | std::map<std::string, std::pair<bool, std::vector<std::string>>>> |
Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 511 | _servTree; |
| 512 | |
Matthew Barth | 07fecfc | 2021-01-29 09:04:43 -0600 | [diff] [blame] | 513 | /* Object map of paths to interfaces of properties and their values */ |
| 514 | static std::map< |
| 515 | std::string, |
| 516 | std::map<std::string, std::map<std::string, PropertyVariantType>>> |
| 517 | _objects; |
| 518 | |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 519 | /* List of timers and their data to be processed when expired */ |
| 520 | std::vector<std::pair<std::unique_ptr<TimerData>, Timer>> _timers; |
| 521 | |
Matthew Barth | ebabc04 | 2021-05-13 15:38:58 -0500 | [diff] [blame] | 522 | /* Map of signal match strings to a list of signal handler data */ |
| 523 | std::unordered_map<std::string, std::vector<SignalData>> _signals; |
| 524 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 525 | /* List of zones configured */ |
| 526 | std::map<configKey, std::unique_ptr<Zone>> _zones; |
| 527 | |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 528 | /* List of events configured */ |
| 529 | std::map<configKey, std::unique_ptr<Event>> _events; |
| 530 | |
Matt Spinler | 7787def | 2021-10-14 16:33:16 -0500 | [diff] [blame] | 531 | /* The sdeventplus wrapper around sd_event_add_defer to dump debug |
| 532 | * data from the event loop after the USR1 signal. */ |
| 533 | std::unique_ptr<sdeventplus::source::Defer> debugDumpEventSource; |
Matt Spinler | 2fc0a35 | 2021-10-04 15:10:57 -0500 | [diff] [blame] | 534 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 535 | /** |
Matt Spinler | d76351b | 2021-08-05 16:23:09 -0500 | [diff] [blame] | 536 | * @brief A map of parameter names and values that are something |
| 537 | * other than just D-Bus property values that other actions |
| 538 | * can set and use. |
| 539 | */ |
| 540 | static std::unordered_map<std::string, PropertyVariantType> _parameters; |
| 541 | |
| 542 | /** |
Matthew Barth | b2cd93f | 2021-06-16 16:37:40 -0500 | [diff] [blame] | 543 | * @brief Callback for power state changes |
| 544 | * |
| 545 | * @param[in] powerStateOn - Whether the power state is on or not |
| 546 | * |
| 547 | * Callback function bound to the PowerState object instance to handle each |
| 548 | * time the power state changes. |
| 549 | */ |
| 550 | void powerStateChanged(bool powerStateOn); |
| 551 | |
| 552 | /** |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 553 | * @brief Find the service name for a given path and interface from the |
| 554 | * cached dataset |
| 555 | * |
| 556 | * @param[in] path - Path to get service for |
| 557 | * @param[in] intf - Interface to get service for |
| 558 | * |
| 559 | * @return - The cached service name |
| 560 | */ |
| 561 | static const std::string& findService(const std::string& path, |
| 562 | const std::string& intf); |
| 563 | |
| 564 | /** |
Matthew Barth | f41e947 | 2021-05-13 16:38:06 -0500 | [diff] [blame] | 565 | * @brief Find all the paths for a given service and interface from the |
| 566 | * cached dataset |
| 567 | * |
| 568 | * @param[in] serv - Service name to get paths for |
| 569 | * @param[in] intf - Interface to get paths for |
| 570 | * |
| 571 | * @return - The cached object paths |
| 572 | */ |
| 573 | std::vector<std::string> findPaths(const std::string& serv, |
| 574 | const std::string& intf); |
| 575 | |
| 576 | /** |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 577 | * @brief Parse and set the configured profiles from the profiles JSON file |
| 578 | * |
| 579 | * Retrieves the optional profiles JSON configuration file, parses it, and |
| 580 | * creates a list of configured profiles available to the other |
| 581 | * configuration files. These profiles can be used to remove or include |
| 582 | * entries within the other configuration files. |
| 583 | */ |
| 584 | void setProfiles(); |
Matt Spinler | 2fc0a35 | 2021-10-04 15:10:57 -0500 | [diff] [blame] | 585 | |
| 586 | /** |
Matt Spinler | 7787def | 2021-10-14 16:33:16 -0500 | [diff] [blame] | 587 | * @brief Callback from debugDumpEventSource to dump debug data |
Matt Spinler | 2fc0a35 | 2021-10-04 15:10:57 -0500 | [diff] [blame] | 588 | */ |
Matt Spinler | 7787def | 2021-10-14 16:33:16 -0500 | [diff] [blame] | 589 | void dumpDebugData(sdeventplus::source::EventBase&); |
Matt Spinler | b5c21a2 | 2021-10-14 16:52:12 -0500 | [diff] [blame^] | 590 | |
| 591 | /** |
| 592 | * @brief Dump the _objects, _servTree, and _parameters maps to JSON |
| 593 | * |
| 594 | * @param[out] data - The JSON that will be filled in |
| 595 | */ |
| 596 | void dumpCache(json& data); |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 597 | }; |
| 598 | |
| 599 | } // namespace phosphor::fan::control::json |