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 |
| 57 | * Zone = Zone object to run the actions against |
| 58 | * Group = Group of dbus objects for the actions |
| 59 | * std::vector<std::unique_ptr<ActionBase>> = List of pointers to actions |
| 60 | * that run when the timer expires |
| 61 | */ |
| 62 | using TimerPkg = std::tuple<std::string, Zone&, const Group&, |
| 63 | std::vector<std::unique_ptr<ActionBase>>&>; |
| 64 | /** |
| 65 | * Data associated with a running timer that's used when it expires |
| 66 | * Pair constructed of: |
| 67 | * TimerType = Type of timer to manage expired timer instances |
| 68 | * TimerPkg = Package of data required when the timer expires |
| 69 | */ |
| 70 | using TimerData = std::pair<TimerType, TimerPkg>; |
| 71 | /* Dbus event timer */ |
| 72 | using Timer = sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>; |
| 73 | |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 74 | /** |
| 75 | * @class Manager - Represents the fan control manager's configuration |
| 76 | * |
| 77 | * A fan control manager configuration is optional, therefore the "manager.json" |
| 78 | * file is also optional. The manager configuration is used to populate |
| 79 | * fan control's manager parameters which are used in how the application |
| 80 | * operates, not in how the fans are controlled. |
| 81 | * |
| 82 | * When no manager configuration exists, the fan control application starts, |
| 83 | * processes any configured events and then begins controlling fans according |
| 84 | * to those events. |
| 85 | */ |
| 86 | class Manager |
| 87 | { |
| 88 | public: |
| 89 | Manager() = delete; |
| 90 | Manager(const Manager&) = delete; |
| 91 | Manager(Manager&&) = delete; |
| 92 | Manager& operator=(const Manager&) = delete; |
| 93 | Manager& operator=(Manager&&) = delete; |
| 94 | ~Manager() = default; |
| 95 | |
| 96 | /** |
| 97 | * Constructor |
| 98 | * Parses and populates the fan control manager attributes from a json file |
| 99 | * |
| 100 | * @param[in] bus - sdbusplus bus object |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 101 | * @param[in] event - sdeventplus event loop |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 102 | */ |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 103 | Manager(sdbusplus::bus::bus& bus, const sdeventplus::Event& event); |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 104 | |
| 105 | /** |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 106 | * @brief Get the active profiles of the system where an empty list |
| 107 | * represents that only configuration entries without a profile defined will |
| 108 | * be loaded. |
| 109 | * |
| 110 | * @return - The list of active profiles |
| 111 | */ |
| 112 | static const std::vector<std::string>& getActiveProfiles(); |
| 113 | |
| 114 | /** |
| 115 | * @brief Load the configuration of a given JSON class object based on the |
| 116 | * active profiles |
| 117 | * |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 118 | * @param[in] isOptional - JSON configuration file is optional or not |
Matthew Barth | 603ef16 | 2021-03-24 15:34:53 -0500 | [diff] [blame] | 119 | * @param[in] bus - The sdbusplus bus object |
| 120 | * @param[in] args - Arguments to be forwarded to each instance of `T` |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 121 | * |
| 122 | * @return Map of configuration entries |
| 123 | * Map of configuration keys to their corresponding configuration object |
| 124 | */ |
Matthew Barth | 603ef16 | 2021-03-24 15:34:53 -0500 | [diff] [blame] | 125 | template <typename T, typename... Args> |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 126 | static std::map<configKey, std::unique_ptr<T>> |
Matthew Barth | 603ef16 | 2021-03-24 15:34:53 -0500 | [diff] [blame] | 127 | getConfig(bool isOptional, sdbusplus::bus::bus& bus, Args&&... args) |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 128 | { |
| 129 | std::map<configKey, std::unique_ptr<T>> config; |
| 130 | |
| 131 | auto confFile = fan::JsonConfig::getConfFile( |
| 132 | bus, confAppName, T::confFileName, isOptional); |
| 133 | if (!confFile.empty()) |
| 134 | { |
| 135 | for (const auto& entry : fan::JsonConfig::load(confFile)) |
| 136 | { |
| 137 | if (entry.contains("profiles")) |
| 138 | { |
| 139 | std::vector<std::string> profiles; |
| 140 | for (const auto& profile : entry["profiles"]) |
| 141 | { |
| 142 | profiles.emplace_back( |
| 143 | profile.template get<std::string>()); |
| 144 | } |
| 145 | // Do not create the object if its profiles are not in the |
| 146 | // list of active profiles |
Matthew Barth | 4242805 | 2021-03-30 12:50:59 -0500 | [diff] [blame^] | 147 | if (!profiles.empty() && |
| 148 | !std::any_of(profiles.begin(), profiles.end(), |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 149 | [](const auto& name) { |
| 150 | return std::find( |
| 151 | getActiveProfiles().begin(), |
| 152 | getActiveProfiles().end(), |
| 153 | name) != |
| 154 | getActiveProfiles().end(); |
| 155 | })) |
| 156 | { |
| 157 | continue; |
| 158 | } |
| 159 | } |
Matthew Barth | 603ef16 | 2021-03-24 15:34:53 -0500 | [diff] [blame] | 160 | auto obj = |
| 161 | std::make_unique<T>(entry, std::forward<Args>(args)...); |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 162 | config.emplace( |
| 163 | std::make_pair(obj->getName(), obj->getProfiles()), |
| 164 | std::move(obj)); |
| 165 | } |
| 166 | } |
| 167 | return config; |
| 168 | } |
| 169 | |
| 170 | /** |
Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 171 | * @brief Check if the given path and inteface is owned by a dbus service |
| 172 | * |
| 173 | * @param[in] path - Dbus object path |
| 174 | * @param[in] intf - Dbus object interface |
| 175 | * |
| 176 | * @return - Whether the service has an owner for the given object path and |
| 177 | * interface |
| 178 | */ |
| 179 | static bool hasOwner(const std::string& path, const std::string& intf); |
| 180 | |
| 181 | /** |
Matthew Barth | 07fecfc | 2021-01-29 09:04:43 -0600 | [diff] [blame] | 182 | * @brief Get the object's property value as a variant |
| 183 | * |
| 184 | * @param[in] path - Path of the object containing the property |
| 185 | * @param[in] intf - Interface name containing the property |
| 186 | * @param[in] prop - Name of property |
| 187 | * |
| 188 | * @return - The object's property value as a variant |
| 189 | */ |
| 190 | static inline auto getObjValueVariant(const std::string& path, |
| 191 | const std::string& intf, |
| 192 | const std::string& prop) |
| 193 | { |
| 194 | return _objects.at(path).at(intf).at(prop); |
| 195 | }; |
| 196 | |
| 197 | /** |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 198 | * @brief Add a dbus timer |
| 199 | * |
| 200 | * @param[in] type - Type of timer |
| 201 | * @param[in] interval - Timer interval in microseconds |
| 202 | * @param[in] pkg - Packaged data for when timer expires |
| 203 | */ |
| 204 | void addTimer(const TimerType type, |
| 205 | const std::chrono::microseconds interval, |
| 206 | std::unique_ptr<TimerPkg> pkg); |
| 207 | |
| 208 | /** |
| 209 | * @brief Callback when a timer expires |
| 210 | * |
| 211 | * @param[in] data - Data to be used when the timer expired |
| 212 | */ |
| 213 | void timerExpired(TimerData& data); |
| 214 | |
| 215 | /** |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 216 | * @brief Get the configured power on delay(OPTIONAL) |
| 217 | * |
| 218 | * @return Power on delay in seconds |
| 219 | * Configured power on delay in seconds, otherwise 0 |
| 220 | */ |
| 221 | unsigned int getPowerOnDelay(); |
| 222 | |
| 223 | private: |
| 224 | /* JSON file name for manager configuration attributes */ |
| 225 | static constexpr auto confFileName = "manager.json"; |
| 226 | |
| 227 | /* The parsed JSON object */ |
| 228 | json _jsonObj; |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 229 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 230 | /** |
| 231 | * The sdbusplus bus object to use |
| 232 | */ |
| 233 | sdbusplus::bus::bus& _bus; |
| 234 | |
| 235 | /** |
| 236 | * The sdeventplus even loop to use |
| 237 | */ |
| 238 | sdeventplus::Event _event; |
| 239 | |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 240 | /* List of profiles configured */ |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 241 | std::map<configKey, std::unique_ptr<Profile>> _profiles; |
Matthew Barth | 0676494 | 2021-03-04 09:28:40 -0600 | [diff] [blame] | 242 | |
| 243 | /* List of active profiles */ |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 244 | static std::vector<std::string> _activeProfiles; |
| 245 | |
Matthew Barth | 12cb125 | 2021-03-08 16:47:30 -0600 | [diff] [blame] | 246 | /* Subtree map of paths to services (with ownership state) of interfaces */ |
| 247 | static std::map<std::string, std::map<std::pair<std::string, bool>, |
| 248 | std::vector<std::string>>> |
| 249 | _servTree; |
| 250 | |
Matthew Barth | 07fecfc | 2021-01-29 09:04:43 -0600 | [diff] [blame] | 251 | /* Object map of paths to interfaces of properties and their values */ |
| 252 | static std::map< |
| 253 | std::string, |
| 254 | std::map<std::string, std::map<std::string, PropertyVariantType>>> |
| 255 | _objects; |
| 256 | |
Matthew Barth | d9cb63b | 2021-03-24 14:36:49 -0500 | [diff] [blame] | 257 | /* List of timers and their data to be processed when expired */ |
| 258 | std::vector<std::pair<std::unique_ptr<TimerData>, Timer>> _timers; |
| 259 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 260 | /* List of zones configured */ |
| 261 | std::map<configKey, std::unique_ptr<Zone>> _zones; |
| 262 | |
Matthew Barth | 44ab769 | 2021-03-26 11:40:10 -0500 | [diff] [blame] | 263 | /* List of events configured */ |
| 264 | std::map<configKey, std::unique_ptr<Event>> _events; |
| 265 | |
Matthew Barth | acd737c | 2021-03-04 11:04:01 -0600 | [diff] [blame] | 266 | /** |
| 267 | * @brief Parse and set the configured profiles from the profiles JSON file |
| 268 | * |
| 269 | * Retrieves the optional profiles JSON configuration file, parses it, and |
| 270 | * creates a list of configured profiles available to the other |
| 271 | * configuration files. These profiles can be used to remove or include |
| 272 | * entries within the other configuration files. |
| 273 | */ |
| 274 | void setProfiles(); |
Matthew Barth | a227a16 | 2020-08-05 10:51:45 -0500 | [diff] [blame] | 275 | }; |
| 276 | |
| 277 | } // namespace phosphor::fan::control::json |