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