Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -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 | |
| 18 | #include "config_base.hpp" |
| 19 | |
| 20 | #include <nlohmann/json.hpp> |
| 21 | #include <sdbusplus/bus.hpp> |
| 22 | |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame^] | 23 | #include <any> |
| 24 | #include <functional> |
| 25 | #include <map> |
| 26 | #include <tuple> |
| 27 | |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 28 | namespace phosphor::fan::control::json |
| 29 | { |
| 30 | |
| 31 | using json = nlohmann::json; |
| 32 | |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame^] | 33 | /* Interface property handler function */ |
| 34 | using propertyHandler = std::function<std::any(const json&)>; |
| 35 | |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 36 | /** |
| 37 | * @class Zone - Represents a configured fan control zone |
| 38 | * |
| 39 | * A zone object contains the configured attributes for a zone that groups |
| 40 | * a number of fans together to be under the same speed control. These |
| 41 | * configuration attributes include, but are not limited to, the full speed |
| 42 | * of the fans within the zone, a default floor speed, the delay between speed |
| 43 | * increases, a decrease interval, and any profiles(OPTIONAL) the zone should |
| 44 | * be included in. |
| 45 | * |
| 46 | * (When no profile for a zone is given, the zone defaults to always exist) |
| 47 | * |
| 48 | */ |
| 49 | class Zone : public ConfigBase |
| 50 | { |
| 51 | public: |
| 52 | /* JSON file name for zones */ |
| 53 | static constexpr auto confFileName = "zones.json"; |
| 54 | |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame^] | 55 | /* Map of interfaces to properties with their values and persistency */ |
| 56 | static constexpr auto propValue = 0; |
| 57 | static constexpr auto propPersist = 1; |
| 58 | using Interfaces = |
| 59 | std::map<std::string, |
| 60 | std::map<std::string, std::tuple<std::any, bool>>>; |
| 61 | |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 62 | Zone() = delete; |
| 63 | Zone(const Zone&) = delete; |
| 64 | Zone(Zone&&) = delete; |
| 65 | Zone& operator=(const Zone&) = delete; |
| 66 | Zone& operator=(Zone&&) = delete; |
| 67 | ~Zone() = default; |
| 68 | |
| 69 | /** |
| 70 | * Constructor |
| 71 | * Parses and populates a zone from JSON object data |
| 72 | * |
| 73 | * @param[in] bus - sdbusplus bus object |
| 74 | * @param[in] jsonObj - JSON object |
| 75 | */ |
| 76 | Zone(sdbusplus::bus::bus& bus, const json& jsonObj); |
| 77 | |
| 78 | /** |
| 79 | * @brief Get the full speed |
| 80 | * |
| 81 | * Full speed is the speed set to the fans within this zone unless there |
| 82 | * are events configured that alter the fan speeds. |
| 83 | * |
| 84 | * @return Full speed of this zone |
| 85 | */ |
| 86 | inline const auto& getFullSpeed() const |
| 87 | { |
| 88 | return _fullSpeed; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @brief Get the default floor speed |
| 93 | * |
| 94 | * The default floor speed is the lowest speed the fans within this zone |
| 95 | * are allowed to decrease to. The zone's floor speed defaults to this |
| 96 | * unless changed by some configured event. |
| 97 | * |
| 98 | * @return Default floor speed |
| 99 | */ |
| 100 | inline const auto& getDefaultFloor() const |
| 101 | { |
| 102 | return _defaultFloor; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @brief Get the speed increase delay(OPTIONAL) |
| 107 | * |
| 108 | * The speed increase delay is the amount of time(in seconds) increases |
| 109 | * to a target speed are delayed before being made. The default is 0, which |
| 110 | * results in immediate speed increase requests when any events result in |
| 111 | * a change to the target speed. |
| 112 | * |
| 113 | * It is recommend a value other than 0 is configured, but that inherently |
| 114 | * depends on the fan controller and configured speed increases. |
| 115 | * |
| 116 | * @return Speed increase delay(in seconds) |
| 117 | */ |
| 118 | inline const auto& getIncDelay() const |
| 119 | { |
| 120 | return _incDelay; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * @brief Get the speed decrease interval |
| 125 | * |
| 126 | * Speed decreases happen on a set interval when no requests for an increase |
| 127 | * in fan speeds exists. This is the interval(in seconds) at which the fans |
| 128 | * within the zone are decreased if events exist that result in a target |
| 129 | * speed decrease. |
| 130 | * |
| 131 | * @return Speed decrease interval(in seconds) |
| 132 | */ |
| 133 | inline const auto& getDecInterval() const |
| 134 | { |
| 135 | return _decInterval; |
| 136 | } |
| 137 | |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame^] | 138 | /** |
| 139 | * @brief Get the configuration of interfaces |
| 140 | * |
| 141 | * Interfaces hosted by a zone can optionally be configured to set their |
| 142 | * property values and/or persistency. These interfaces must be supported |
| 143 | * by the zone object they are configured for. |
| 144 | * |
| 145 | * @return Map of interfaces to properties with their values and persistency |
| 146 | */ |
| 147 | inline const auto& getInterfaces() const |
| 148 | { |
| 149 | return _interfaces; |
| 150 | } |
| 151 | |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 152 | private: |
| 153 | /* The zone's full speed value for fans */ |
| 154 | uint64_t _fullSpeed; |
| 155 | |
| 156 | /* The zone's default floor speed value for fans */ |
| 157 | uint64_t _defaultFloor; |
| 158 | |
| 159 | /* Zone's speed increase delay(in seconds) (OPTIONAL) */ |
| 160 | uint64_t _incDelay; |
| 161 | |
| 162 | /* Zone's speed decrease interval(in seconds) */ |
| 163 | uint64_t _decInterval; |
| 164 | |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame^] | 165 | /* Map of interfaces to properties with their values and persistency */ |
| 166 | Interfaces _interfaces; |
| 167 | |
| 168 | /* Interface properties mapping to their associated handler function */ |
| 169 | static const std::map<std::string, propertyHandler> _props; |
| 170 | |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 171 | /** |
| 172 | * @brief Parse and set the zone's full speed value |
| 173 | * |
| 174 | * @param[in] jsonObj - JSON object for the zone |
| 175 | * |
| 176 | * Sets the full speed value for the zone from the JSON configuration object |
| 177 | */ |
| 178 | void setFullSpeed(const json& jsonObj); |
| 179 | |
| 180 | /** |
| 181 | * @brief Parse and set the zone's default floor speed value |
| 182 | * |
| 183 | * @param[in] jsonObj - JSON object for the zone |
| 184 | * |
| 185 | * Sets the default floor speed value for the zone from the JSON |
| 186 | * configuration object |
| 187 | */ |
| 188 | void setDefaultFloor(const json& jsonObj); |
| 189 | |
| 190 | /** |
| 191 | * @brief Parse and set the zone's decrease interval(in seconds) |
| 192 | * |
| 193 | * @param[in] jsonObj - JSON object for the zone |
| 194 | * |
| 195 | * Sets the speed decrease interval(in seconds) for the zone from the JSON |
| 196 | * configuration object |
| 197 | */ |
| 198 | void setDecInterval(const json& jsonObj); |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame^] | 199 | |
| 200 | /** |
| 201 | * @brief Parse and set properties on interfaces the zone serves(OPTIONAL) |
| 202 | * |
| 203 | * @param[in] jsonObj - JSON object for the zone |
| 204 | * |
| 205 | * Sets any properties on the interfaces that the zone to serves along |
| 206 | * with the property's persistency state (OPTIONAL). A property's "persist" |
| 207 | * state is defaulted to not be persisted when not given. |
| 208 | */ |
| 209 | void setInterfaces(const json& jsonObj); |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 210 | }; |
| 211 | |
Matthew Barth | 651f03a | 2020-08-27 16:15:11 -0500 | [diff] [blame^] | 212 | /** |
| 213 | * Properties of interfaces supported by the zone configuration |
| 214 | */ |
| 215 | namespace zone::property |
| 216 | { |
| 217 | |
| 218 | /** |
| 219 | * @brief "Supported" property on the "ThermalMode" interface |
| 220 | * |
| 221 | * @param[in] jsonObj - JSON object for the "Supported" property |
| 222 | * |
| 223 | * @return - A std::any object of the "Supported" property's value |
| 224 | */ |
| 225 | std::any supported(const json& jsonObj); |
| 226 | |
| 227 | /** |
| 228 | * @brief "Current property on the "ThermalMode" interface |
| 229 | * |
| 230 | * @param[in] jsonObj - JSON object for the "Current" property |
| 231 | * |
| 232 | * @return - A std::any object of the "Current" property's value |
| 233 | */ |
| 234 | std::any current(const json& jsonObj); |
| 235 | |
| 236 | } // namespace zone::property |
| 237 | |
Matthew Barth | 4f0d3b7 | 2020-08-27 14:32:15 -0500 | [diff] [blame] | 238 | } // namespace phosphor::fan::control::json |