Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2020 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #pragma once |
| 17 | |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 18 | #include "action.hpp" |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 19 | #include "config_base.hpp" |
| 20 | #include "event.hpp" |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 21 | #include "group.hpp" |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 22 | #include "json_config.hpp" |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 23 | #include "profile.hpp" |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 24 | #include "zone.hpp" |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 25 | |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 26 | #include <nlohmann/json.hpp> |
| 27 | #include <sdbusplus/bus.hpp> |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 28 | #include <sdeventplus/event.hpp> |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 29 | #include <sdeventplus/utility/timer.hpp> |
| 30 | |
| 31 | #include <chrono> |
| 32 | #include <map> |
| 33 | #include <memory> |
| 34 | #include <optional> |
| 35 | #include <tuple> |
| 36 | #include <utility> |
| 37 | #include <vector> |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 38 | |
| 39 | namespace phosphor::fan::control::json |
| 40 | { |
| 41 | |
| 42 | using json = nlohmann::json; |
| 43 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 44 | /* Application name to be appended to the path for loading a JSON config file */ |
| 45 | constexpr auto confAppName = "control"; |
| 46 | |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 47 | /* Type of timers supported */ |
| 48 | enum class TimerType |
| 49 | { |
| 50 | oneshot, |
| 51 | repeating, |
| 52 | }; |
| 53 | /** |
| 54 | * Package of data required when a timer expires |
| 55 | * Tuple constructed of: |
| 56 | * std::string = Timer package unique identifier |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 57 | * std::vector<std::unique_ptr<ActionBase>> = List of pointers to actions |
| 58 | * that run when the timer expires |
| 59 | */ |
Matthew Barth | 00f6aa0 | 2021-04-09 10:49:47 -0500 | [diff] [blame] | 60 | using TimerPkg = |
| 61 | std::tuple<std::string, std::vector<std::unique_ptr<ActionBase>>&>; |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 62 | /** |
| 63 | * Data associated with a running timer that's used when it expires |
| 64 | * Pair constructed of: |
| 65 | * TimerType = Type of timer to manage expired timer instances |
| 66 | * TimerPkg = Package of data required when the timer expires |
| 67 | */ |
| 68 | using TimerData = std::pair<TimerType, TimerPkg>; |
| 69 | /* Dbus event timer */ |
| 70 | using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>; |
| 71 | |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 72 | /** |
| 73 | * @class Manager - Represents the fan control manager's configuration |
| 74 | * |
| 75 | * A fan control manager configuration is optional, therefore the "manager.json" |
| 76 | * file is also optional. The manager configuration is used to populate |
| 77 | * fan control's manager parameters which are used in how the application |
| 78 | * operates, not in how the fans are controlled. |
| 79 | * |
| 80 | * When no manager configuration exists, the fan control application starts, |
| 81 | * processes any configured events and then begins controlling fans according |
| 82 | * to those events. |
| 83 | */ |
| 84 | class Manager |
| 85 | { |
| 86 | public: |
| 87 | Manager() = delete; |
| 88 | Manager(const Manager&) = delete; |
| 89 | Manager(Manager&&) = delete; |
| 90 | Manager& operator=(const Manager&) = delete; |
| 91 | Manager& operator=(Manager&&) = delete; |
| 92 | ~Manager() = default; |
| 93 | |
| 94 | /** |
| 95 | * Constructor |
| 96 | * Parses and populates the fan control manager attributes from a json file |
| 97 | * |
| 98 | * @param[in] bus - sdbusplus bus object |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 99 | * @param[in] event - sdeventplus event loop |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 100 | */ |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 101 | Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event); |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 102 | |
| 103 | /** |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 104 | * @brief Get the active profiles of the system where an empty list |
| 105 | * represents that only configuration entries without a profile defined will |
| 106 | * be loaded. |
| 107 | * |
| 108 | * @return - The list of active profiles |
| 109 | */ |
| 110 | static const std::vector<std::string>& getActiveProfiles(); |
| 111 | |
| 112 | /** |
| 113 | * @brief Load the configuration of a given JSON class object based on the |
| 114 | * active profiles |
| 115 | * |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 116 | * @param[in] isOptional - JSON configuration file is optional or not |
Matthew Barth | 603ef16 | 2021-03-24 15:34:53 -0500 | [diff] [blame] | 117 | * @param[in] bus - The sdbusplus bus object |
| 118 | * @param[in] args - Arguments to be forwarded to each instance of `T` |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 119 | * |
| 120 | * @return Map of configuration entries |
| 121 | * Map of configuration keys to their corresponding configuration object |
| 122 | */ |
Matthew Barth | 603ef16 | 2021-03-24 15:34:53 -0500 | [diff] [blame] | 123 | template <typename T, typename... Args> |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 124 | static std::map<configKey, std::unique_ptr<T>> |
Matthew Barth | 603ef16 | 2021-03-24 15:34:53 -0500 | [diff] [blame] | 125 | getConfig(bool isOptional, sdbusplus::bus::bus& bus, Args&&... args) |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 126 | { |
| 127 | std::map<configKey, std::unique_ptr<T>> config; |
| 128 | |
| 129 | auto confFile = fan::JsonConfig::getConfFile( |
| 130 | bus, confAppName, T::confFileName, isOptional); |
| 131 | if (!confFile.empty()) |
| 132 | { |
| 133 | for (const auto& entry : fan::JsonConfig::load(confFile)) |
| 134 | { |
| 135 | if (entry.contains("profiles")) |
| 136 | { |
| 137 | std::vector<std::string> profiles; |
| 138 | for (const auto& profile : entry["profiles"]) |
| 139 | { |
| 140 | profiles.emplace_back( |
| 141 | profile.template get<std::string>()); |
| 142 | } |
| 143 | // Do not create the object if its profiles are not in the |
| 144 | // list of active profiles |
Matthew Barth | 4242805 | 2021-03-30 12:50:59 -0500 | [diff] [blame] | 145 | if (!profiles.empty() && |
| 146 | !std::any_of(profiles.begin(), profiles.end(), |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 147 | [](const auto& name) { |
| 148 | return std::find( |
| 149 | getActiveProfiles().begin(), |
| 150 | getActiveProfiles().end(), |
| 151 | name) != |
| 152 | getActiveProfiles().end(); |
| 153 | })) |
| 154 | { |
| 155 | continue; |
| 156 | } |
| 157 | } |
Matthew Barth | 603ef16 | 2021-03-24 15:34:53 -0500 | [diff] [blame] | 158 | auto obj = |
| 159 | std::make_unique<T>(entry, std::forward<Args>(args)...); |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 160 | config.emplace( |
| 161 | std::make_pair(obj->getName(), obj->getProfiles()), |
| 162 | std::move(obj)); |
| 163 | } |
| 164 | } |
| 165 | return config; |
| 166 | } |
| 167 | |
| 168 | /** |
Matthew Barth | 0206c72 | 2021-03-30 15:20:05 -0500 | [diff] [blame] | 169 | * @brief Check if the given input configuration key matches with another |
| 170 | * configuration key that it's to be included in |
| 171 | * |
| 172 | * @param[in] input - Config key to be included in another config object |
| 173 | * @param[in] comp - Config key of the config object to compare with |
| 174 | * |
| 175 | * @return Whether the configuration object should be included |
| 176 | */ |
| 177 | static bool inConfig(const configKey& input, const configKey& comp); |
| 178 | |
| 179 | /** |
Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 180 | * @brief Check if the given path and inteface is owned by a dbus service |
| 181 | * |
| 182 | * @param[in] path - Dbus object path |
| 183 | * @param[in] intf - Dbus object interface |
| 184 | * |
| 185 | * @return - Whether the service has an owner for the given object path and |
| 186 | * interface |
| 187 | */ |
| 188 | static bool hasOwner(const std::string& path, const std::string& intf); |
| 189 | |
| 190 | /** |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 191 | * @brief Sets the dbus service owner state of a given object |
| 192 | * |
| 193 | * @param[in] path - Dbus object path |
| 194 | * @param[in] serv - Dbus service name |
| 195 | * @param[in] intf - Dbus object interface |
| 196 | * @param[in] isOwned - Dbus service owner state |
| 197 | */ |
| 198 | void setOwner(const std::string& path, const std::string& serv, |
| 199 | const std::string& intf, bool isOwned); |
| 200 | |
| 201 | /** |
| 202 | * @brief Add a set of services for a path and interface by retrieving all |
| 203 | * the path subtrees to the given depth from root for the interface |
| 204 | * |
| 205 | * @param[in] path - Path to add services for |
| 206 | * @param[in] intf - Interface to add services for |
| 207 | * @param[in] depth - Depth of tree traversal from root path |
| 208 | * |
| 209 | * @throws - DBusMethodError |
| 210 | * Throws a DBusMethodError when the `getSubTree` method call fails |
| 211 | */ |
| 212 | static void addServices(const std::string& path, const std::string& intf, |
| 213 | int32_t depth); |
| 214 | |
| 215 | /** |
| 216 | * @brief Get the service for a given path and interface from cached |
| 217 | * dataset and attempt to add all the services for the given path/interface |
| 218 | * when it's not found |
| 219 | * |
| 220 | * @param[in] path - Path to get service for |
| 221 | * @param[in] intf - Interface to get service for |
| 222 | * |
| 223 | * @return - The now cached service name |
| 224 | * |
| 225 | * @throws - DBusMethodError |
| 226 | * Ripples up a DBusMethodError exception from calling addServices |
| 227 | */ |
| 228 | static const std::string& getService(const std::string& path, |
| 229 | const std::string& intf); |
| 230 | |
| 231 | /** |
Matthew Barth | 07fecfc | 2021-01-29 09:04:43 -0600 | [diff] [blame] | 232 | * @brief Get the object's property value as a variant |
| 233 | * |
| 234 | * @param[in] path - Path of the object containing the property |
| 235 | * @param[in] intf - Interface name containing the property |
| 236 | * @param[in] prop - Name of property |
| 237 | * |
| 238 | * @return - The object's property value as a variant |
| 239 | */ |
| 240 | static inline auto getObjValueVariant(const std::string& path, |
| 241 | const std::string& intf, |
| 242 | const std::string& prop) |
| 243 | { |
| 244 | return _objects.at(path).at(intf).at(prop); |
| 245 | }; |
| 246 | |
| 247 | /** |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 248 | * @brief Add a dbus timer |
| 249 | * |
| 250 | * @param[in] type - Type of timer |
| 251 | * @param[in] interval - Timer interval in microseconds |
| 252 | * @param[in] pkg - Packaged data for when timer expires |
| 253 | */ |
| 254 | void addTimer(const TimerType type, |
| 255 | const std::chrono::microseconds interval, |
| 256 | std::unique_ptr<TimerPkg> pkg); |
| 257 | |
| 258 | /** |
| 259 | * @brief Callback when a timer expires |
| 260 | * |
| 261 | * @param[in] data - Data to be used when the timer expired |
| 262 | */ |
| 263 | void timerExpired(TimerData& data); |
| 264 | |
| 265 | /** |
Matthew Barth | eebde06 | 2021-04-14 12:48:52 -0500 | [diff] [blame^] | 266 | * @brief Get the sdbusplus bus object |
| 267 | */ |
| 268 | inline auto& getBus() |
| 269 | { |
| 270 | return _bus; |
| 271 | } |
| 272 | |
| 273 | /** |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 274 | * @brief Get the configured power on delay(OPTIONAL) |
| 275 | * |
| 276 | * @return Power on delay in seconds |
| 277 | * Configured power on delay in seconds, otherwise 0 |
| 278 | */ |
| 279 | unsigned int getPowerOnDelay(); |
| 280 | |
| 281 | private: |
| 282 | /* JSON file name for manager configuration attributes */ |
| 283 | static constexpr auto confFileName = "manager.json"; |
| 284 | |
| 285 | /* The parsed JSON object */ |
| 286 | json _jsonObj; |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 287 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 288 | /** |
| 289 | * The sdbusplus bus object to use |
| 290 | */ |
| 291 | sdbusplus::bus::bus& _bus; |
| 292 | |
| 293 | /** |
| 294 | * The sdeventplus even loop to use |
| 295 | */ |
| 296 | sdeventplus::Event _event; |
| 297 | |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 298 | /* List of profiles configured */ |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 299 | std::map<configKey, std::unique_ptr<Profile>> _profiles; |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 300 | |
| 301 | /* List of active profiles */ |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 302 | static std::vector<std::string> _activeProfiles; |
| 303 | |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 304 | /* Subtree map of paths to services of interfaces(with ownership state) */ |
| 305 | static std::map< |
| 306 | std::string, |
| 307 | std::map<std::string, std::pair<bool, std::vector<std::string>>>> |
Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 308 | _servTree; |
| 309 | |
Matthew Barth | 07fecfc | 2021-01-29 09:04:43 -0600 | [diff] [blame] | 310 | /* Object map of paths to interfaces of properties and their values */ |
| 311 | static std::map< |
| 312 | std::string, |
| 313 | std::map<std::string, std::map<std::string, PropertyVariantType>>> |
| 314 | _objects; |
| 315 | |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 316 | /* List of timers and their data to be processed when expired */ |
| 317 | std::vector<std::pair<std::unique_ptr<TimerData>, Timer>> _timers; |
| 318 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 319 | /* List of zones configured */ |
| 320 | std::map<configKey, std::unique_ptr<Zone>> _zones; |
| 321 | |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 322 | /* List of events configured */ |
| 323 | std::map<configKey, std::unique_ptr<Event>> _events; |
| 324 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 325 | /** |
Matthew Barth | 4ca87fa | 2021-04-14 11:31:13 -0500 | [diff] [blame] | 326 | * @brief Find the service name for a given path and interface from the |
| 327 | * cached dataset |
| 328 | * |
| 329 | * @param[in] path - Path to get service for |
| 330 | * @param[in] intf - Interface to get service for |
| 331 | * |
| 332 | * @return - The cached service name |
| 333 | */ |
| 334 | static const std::string& findService(const std::string& path, |
| 335 | const std::string& intf); |
| 336 | |
| 337 | /** |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 338 | * @brief Parse and set the configured profiles from the profiles JSON file |
| 339 | * |
| 340 | * Retrieves the optional profiles JSON configuration file, parses it, and |
| 341 | * creates a list of configured profiles available to the other |
| 342 | * configuration files. These profiles can be used to remove or include |
| 343 | * entries within the other configuration files. |
| 344 | */ |
| 345 | void setProfiles(); |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 346 | }; |
| 347 | |
| 348 | } // namespace phosphor::fan::control::json |