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