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