Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 1 | #pragma once |
Matthew Barth | a956184 | 2017-06-29 11:43:45 -0500 | [diff] [blame] | 2 | #include <chrono> |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 3 | #include <vector> |
Matthew Barth | e0ca13e | 2017-06-13 16:29:09 -0500 | [diff] [blame] | 4 | #include <algorithm> |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 5 | #include <sdbusplus/bus.hpp> |
| 6 | #include "fan.hpp" |
| 7 | #include "types.hpp" |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 8 | #include "timer.hpp" |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 9 | |
| 10 | namespace phosphor |
| 11 | { |
| 12 | namespace fan |
| 13 | { |
| 14 | namespace control |
| 15 | { |
| 16 | |
| 17 | /** |
Matthew Barth | 1418413 | 2017-05-19 14:37:30 -0500 | [diff] [blame] | 18 | * The mode fan control will run in: |
| 19 | * - init - only do the initialization steps |
| 20 | * - control - run normal control algorithms |
| 21 | */ |
| 22 | enum class Mode |
| 23 | { |
| 24 | init, |
| 25 | control |
| 26 | }; |
| 27 | |
| 28 | /** |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 29 | * @class Represents a fan control zone, which is a group of fans |
| 30 | * that behave the same. |
| 31 | */ |
| 32 | class Zone |
| 33 | { |
| 34 | public: |
| 35 | |
| 36 | Zone() = delete; |
| 37 | Zone(const Zone&) = delete; |
| 38 | Zone(Zone&&) = default; |
| 39 | Zone& operator=(const Zone&) = delete; |
| 40 | Zone& operator=(Zone&&) = delete; |
| 41 | ~Zone() = default; |
| 42 | |
| 43 | /** |
| 44 | * Constructor |
| 45 | * Creates the appropriate fan objects based on |
| 46 | * the zone definition data passed in. |
| 47 | * |
Matthew Barth | 1418413 | 2017-05-19 14:37:30 -0500 | [diff] [blame] | 48 | * @param[in] mode - mode of fan control |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 49 | * @param[in] bus - the dbus object |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 50 | * @param[in] events - sd_event pointer |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 51 | * @param[in] def - the fan zone definition data |
| 52 | */ |
Matthew Barth | 1418413 | 2017-05-19 14:37:30 -0500 | [diff] [blame] | 53 | Zone(Mode mode, |
| 54 | sdbusplus::bus::bus& bus, |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 55 | phosphor::fan::event::EventPtr& events, |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 56 | const ZoneDefinition& def); |
| 57 | |
| 58 | /** |
| 59 | * Sets all fans in the zone to the speed |
| 60 | * passed in |
| 61 | * |
| 62 | * @param[in] speed - the fan speed |
| 63 | */ |
| 64 | void setSpeed(uint64_t speed); |
| 65 | |
| 66 | /** |
| 67 | * Sets the zone to full speed |
| 68 | */ |
| 69 | inline void setFullSpeed() |
| 70 | { |
| 71 | if (_fullSpeed != 0) |
| 72 | { |
| 73 | setSpeed(_fullSpeed); |
| 74 | } |
| 75 | } |
| 76 | |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 77 | /** |
Matthew Barth | 861d77c | 2017-05-22 14:18:25 -0500 | [diff] [blame] | 78 | * @brief Sets the automatic fan control allowed active state |
| 79 | * |
| 80 | * @param[in] group - A group that affects the active state |
| 81 | * @param[in] isActiveAllow - Active state according to group |
| 82 | */ |
| 83 | void setActiveAllow(const Group* group, bool isActiveAllow); |
| 84 | |
| 85 | /** |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 86 | * @brief Sets a given object's property value |
| 87 | * |
| 88 | * @param[in] object - Name of the object containing the property |
Matthew Barth | cec5ab7 | 2017-06-02 15:20:56 -0500 | [diff] [blame] | 89 | * @param[in] interface - Interface name containing the property |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 90 | * @param[in] property - Property name |
| 91 | * @param[in] value - Property value |
| 92 | */ |
Matthew Barth | 9e741ed | 2017-06-02 16:29:09 -0500 | [diff] [blame] | 93 | template <typename T> |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 94 | void setPropertyValue(const char* object, |
Matthew Barth | cec5ab7 | 2017-06-02 15:20:56 -0500 | [diff] [blame] | 95 | const char* interface, |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 96 | const char* property, |
Matthew Barth | 9e741ed | 2017-06-02 16:29:09 -0500 | [diff] [blame] | 97 | T value) |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 98 | { |
Matthew Barth | cec5ab7 | 2017-06-02 15:20:56 -0500 | [diff] [blame] | 99 | _properties[object][interface][property] = value; |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 100 | }; |
| 101 | |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 102 | /** |
| 103 | * @brief Get the value of an object's property |
| 104 | * |
| 105 | * @param[in] object - Name of the object containing the property |
Matthew Barth | cec5ab7 | 2017-06-02 15:20:56 -0500 | [diff] [blame] | 106 | * @param[in] interface - Interface name containing the property |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 107 | * @param[in] property - Property name |
| 108 | * |
| 109 | * @return - The property value |
| 110 | */ |
Matthew Barth | 9e741ed | 2017-06-02 16:29:09 -0500 | [diff] [blame] | 111 | template <typename T> |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 112 | inline auto getPropertyValue(const std::string& object, |
Matthew Barth | cec5ab7 | 2017-06-02 15:20:56 -0500 | [diff] [blame] | 113 | const std::string& interface, |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 114 | const std::string& property) |
| 115 | { |
Matthew Barth | 9e741ed | 2017-06-02 16:29:09 -0500 | [diff] [blame] | 116 | return sdbusplus::message::variant_ns::get<T>( |
Matthew Barth | bc65160 | 2017-08-10 16:59:43 -0500 | [diff] [blame] | 117 | _properties.at(object).at(interface).at(property)); |
Matthew Barth | 17d1fe2 | 2017-05-11 15:00:36 -0500 | [diff] [blame] | 118 | }; |
| 119 | |
Matthew Barth | 1de6662 | 2017-06-12 13:13:02 -0500 | [diff] [blame] | 120 | /** |
Matthew Barth | 604329e | 2017-08-04 11:18:28 -0500 | [diff] [blame] | 121 | * @brief Get the object's property variant |
| 122 | * |
| 123 | * @param[in] object - Name of the object containing the property |
| 124 | * @param[in] interface - Interface name containing the property |
| 125 | * @param[in] property - Property name |
| 126 | * |
| 127 | * @return - The property variant |
| 128 | */ |
| 129 | inline auto getPropValueVariant(const std::string& object, |
| 130 | const std::string& interface, |
| 131 | const std::string& property) |
| 132 | { |
| 133 | return _properties.at(object).at(interface).at(property); |
| 134 | }; |
| 135 | |
| 136 | /** |
| 137 | * @brief Initialize a set speed event properties and actions |
| 138 | * |
| 139 | * @param[in] event - Set speed event |
| 140 | */ |
| 141 | void initEvent(const SetSpeedEvent& event); |
| 142 | |
| 143 | /** |
Matthew Barth | f6b76d8 | 2017-08-04 12:58:02 -0500 | [diff] [blame] | 144 | * @brief Removes all the set speed event properties and actions |
| 145 | * |
| 146 | * @param[in] event - Set speed event |
| 147 | */ |
| 148 | void removeEvent(const SetSpeedEvent& event); |
| 149 | |
| 150 | /** |
Matthew Barth | 1de6662 | 2017-06-12 13:13:02 -0500 | [diff] [blame] | 151 | * @brief Get the default floor speed |
| 152 | * |
| 153 | * @return - The defined default floor speed |
| 154 | */ |
| 155 | inline auto getDefFloor() |
| 156 | { |
| 157 | return _defFloorSpeed; |
| 158 | }; |
| 159 | |
Matthew Barth | 4af419c | 2017-06-12 13:39:31 -0500 | [diff] [blame] | 160 | /** |
Matthew Barth | e0ca13e | 2017-06-13 16:29:09 -0500 | [diff] [blame] | 161 | * @brief Get the ceiling speed |
| 162 | * |
| 163 | * @return - The current ceiling speed |
| 164 | */ |
| 165 | inline auto& getCeiling() const |
| 166 | { |
| 167 | return _ceilingSpeed; |
| 168 | }; |
| 169 | |
| 170 | /** |
| 171 | * @brief Set the ceiling speed to the given speed |
| 172 | * |
| 173 | * @param[in] speed - Speed to set the ceiling to |
| 174 | */ |
| 175 | inline void setCeiling(uint64_t speed) |
| 176 | { |
| 177 | _ceilingSpeed = speed; |
| 178 | }; |
| 179 | |
| 180 | /** |
| 181 | * @brief Swaps the ceiling key value with what's given and |
| 182 | * returns the value that was swapped. |
| 183 | * |
| 184 | * @param[in] keyValue - New ceiling key value |
| 185 | * |
| 186 | * @return - Ceiling key value prior to swapping |
| 187 | */ |
| 188 | inline auto swapCeilingKeyValue(int64_t keyValue) |
| 189 | { |
| 190 | std::swap(_ceilingKeyValue, keyValue); |
| 191 | return keyValue; |
| 192 | }; |
| 193 | |
Matthew Barth | 2462352 | 2017-06-21 14:09:57 -0500 | [diff] [blame] | 194 | /** |
| 195 | * @brief Get the increase speed delta |
| 196 | * |
| 197 | * @return - The current increase speed delta |
| 198 | */ |
| 199 | inline auto& getIncSpeedDelta() const |
| 200 | { |
| 201 | return _incSpeedDelta; |
| 202 | }; |
| 203 | |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame] | 204 | /** |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 205 | * @brief Get the decrease speed delta |
| 206 | * |
| 207 | * @return - The current decrease speed delta |
| 208 | */ |
| 209 | inline auto& getDecSpeedDelta() const |
| 210 | { |
| 211 | return _decSpeedDelta; |
| 212 | }; |
| 213 | |
| 214 | /** |
Matthew Barth | b4a7cb9 | 2017-06-28 15:29:50 -0500 | [diff] [blame] | 215 | * @brief Set the floor speed to the given speed and increase target |
| 216 | * speed to the floor when target is below floor. |
| 217 | * |
| 218 | * @param[in] speed - Speed to set the floor to |
| 219 | */ |
| 220 | void setFloor(uint64_t speed); |
| 221 | |
| 222 | /** |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame] | 223 | * @brief Calculate the requested target speed from the given delta |
| 224 | * and increase the fan speeds, not going above the ceiling. |
| 225 | * |
| 226 | * @param[in] targetDelta - The delta to increase the target speed by |
| 227 | */ |
| 228 | void requestSpeedIncrease(uint64_t targetDelta); |
| 229 | |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 230 | /** |
| 231 | * @brief Calculate the requested target speed from the given delta |
| 232 | * and increase the fan speeds, not going above the ceiling. |
| 233 | * |
| 234 | * @param[in] targetDelta - The delta to increase the target speed by |
| 235 | */ |
| 236 | void requestSpeedDecrease(uint64_t targetDelta); |
| 237 | |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 238 | /** |
Matthew Barth | 1ee48f2 | 2017-06-27 15:14:48 -0500 | [diff] [blame] | 239 | * @brief Callback function for the increase timer that delays |
| 240 | * processing of requested speed increases while fans are increasing |
| 241 | */ |
| 242 | void incTimerExpired(); |
| 243 | |
| 244 | /** |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 245 | * @brief Callback function for the decrease timer that processes any |
| 246 | * requested speed decreases if allowed |
| 247 | */ |
| 248 | void decTimerExpired(); |
| 249 | |
Matthew Barth | 9014980 | 2017-08-15 10:51:37 -0500 | [diff] [blame^] | 250 | /** |
| 251 | * @brief Callback function for event timers that processes the given |
| 252 | * action for a group |
| 253 | * |
| 254 | * @param[in] eventGroup - Group to process action on |
| 255 | * @param[in] eventAction - Event action to run |
| 256 | */ |
| 257 | void timerExpired(Group eventGroup, Action eventAction); |
| 258 | |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 259 | private: |
| 260 | |
| 261 | /** |
| 262 | * The dbus object |
| 263 | */ |
| 264 | sdbusplus::bus::bus& _bus; |
| 265 | |
| 266 | /** |
| 267 | * Full speed for the zone |
| 268 | */ |
| 269 | const uint64_t _fullSpeed; |
| 270 | |
| 271 | /** |
| 272 | * The zone number |
| 273 | */ |
| 274 | const size_t _zoneNum; |
| 275 | |
| 276 | /** |
Matthew Barth | 1de6662 | 2017-06-12 13:13:02 -0500 | [diff] [blame] | 277 | * The default floor speed for the zone |
| 278 | */ |
| 279 | const uint64_t _defFloorSpeed; |
| 280 | |
| 281 | /** |
Matthew Barth | e0ca13e | 2017-06-13 16:29:09 -0500 | [diff] [blame] | 282 | * The default ceiling speed for the zone |
| 283 | */ |
| 284 | const uint64_t _defCeilingSpeed; |
| 285 | |
| 286 | /** |
Matthew Barth | 4af419c | 2017-06-12 13:39:31 -0500 | [diff] [blame] | 287 | * The floor speed to not go below |
| 288 | */ |
| 289 | uint64_t _floorSpeed = _defFloorSpeed; |
| 290 | |
| 291 | /** |
Matthew Barth | e0ca13e | 2017-06-13 16:29:09 -0500 | [diff] [blame] | 292 | * The ceiling speed to not go above |
| 293 | */ |
| 294 | uint64_t _ceilingSpeed = _defCeilingSpeed; |
| 295 | |
| 296 | /** |
| 297 | * The previous sensor value for calculating the ceiling |
| 298 | */ |
| 299 | int64_t _ceilingKeyValue = 0; |
| 300 | |
| 301 | /** |
Matthew Barth | 861d77c | 2017-05-22 14:18:25 -0500 | [diff] [blame] | 302 | * Automatic fan control active state |
| 303 | */ |
| 304 | bool _isActive = true; |
| 305 | |
| 306 | /** |
Matthew Barth | 240397b | 2017-06-22 11:23:30 -0500 | [diff] [blame] | 307 | * Target speed for this zone |
| 308 | */ |
| 309 | uint64_t _targetSpeed = _fullSpeed; |
| 310 | |
| 311 | /** |
Matthew Barth | 2462352 | 2017-06-21 14:09:57 -0500 | [diff] [blame] | 312 | * Speed increase delta |
| 313 | */ |
| 314 | uint64_t _incSpeedDelta = 0; |
| 315 | |
| 316 | /** |
Matthew Barth | 0ce99d8 | 2017-06-22 15:07:29 -0500 | [diff] [blame] | 317 | * Speed decrease delta |
| 318 | */ |
| 319 | uint64_t _decSpeedDelta = 0; |
| 320 | |
| 321 | /** |
Matthew Barth | a956184 | 2017-06-29 11:43:45 -0500 | [diff] [blame] | 322 | * Speed increase delay in seconds |
| 323 | */ |
| 324 | std::chrono::seconds _incDelay; |
| 325 | |
| 326 | /** |
| 327 | * Speed decrease interval in seconds |
| 328 | */ |
| 329 | std::chrono::seconds _decInterval; |
| 330 | |
| 331 | /** |
Matthew Barth | 1ee48f2 | 2017-06-27 15:14:48 -0500 | [diff] [blame] | 332 | * The increase timer object |
| 333 | */ |
| 334 | phosphor::fan::util::Timer _incTimer; |
| 335 | |
| 336 | /** |
Matthew Barth | 8600d9a | 2017-06-23 14:38:05 -0500 | [diff] [blame] | 337 | * The decrease timer object |
| 338 | */ |
| 339 | phosphor::fan::util::Timer _decTimer; |
| 340 | |
| 341 | /** |
Matthew Barth | 9014980 | 2017-08-15 10:51:37 -0500 | [diff] [blame^] | 342 | * Dbus event used on set speed event timers |
| 343 | */ |
| 344 | phosphor::fan::event::EventPtr& _sdEvents; |
| 345 | |
| 346 | /** |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 347 | * The vector of fans in this zone |
| 348 | */ |
| 349 | std::vector<std::unique_ptr<Fan>> _fans; |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 350 | |
| 351 | /** |
| 352 | * @brief Map of object property values |
| 353 | */ |
Matthew Barth | cec5ab7 | 2017-06-02 15:20:56 -0500 | [diff] [blame] | 354 | std::map<std::string, |
| 355 | std::map<std::string, |
| 356 | std::map<std::string, |
Matthew Barth | 9e741ed | 2017-06-02 16:29:09 -0500 | [diff] [blame] | 357 | PropertyVariantType>>> _properties; |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 358 | |
| 359 | /** |
Matthew Barth | 861d77c | 2017-05-22 14:18:25 -0500 | [diff] [blame] | 360 | * @brief Map of active fan control allowed by groups |
| 361 | */ |
| 362 | std::map<const Group*, bool> _active; |
| 363 | |
| 364 | /** |
Matthew Barth | f6b76d8 | 2017-08-04 12:58:02 -0500 | [diff] [blame] | 365 | * @brief List of signal event arguments and Dbus matches for callbacks |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 366 | */ |
Matthew Barth | f6b76d8 | 2017-08-04 12:58:02 -0500 | [diff] [blame] | 367 | std::vector<SignalEvent> _signalEvents; |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 368 | |
| 369 | /** |
Matthew Barth | 9014980 | 2017-08-15 10:51:37 -0500 | [diff] [blame^] | 370 | * @brief List of timers for events |
| 371 | */ |
| 372 | std::vector<std::unique_ptr<phosphor::fan::util::Timer>> _timerEvents; |
| 373 | |
| 374 | /** |
Matthew Barth | 1bf0ce4 | 2017-06-23 16:16:30 -0500 | [diff] [blame] | 375 | * @brief Refresh the given property's cached value |
| 376 | * |
| 377 | * @param[in] bus - the bus to use |
| 378 | * @param[in] path - the dbus path name |
| 379 | * @param[in] iface - the dbus interface name |
| 380 | * @param[in] prop - the property name |
| 381 | */ |
| 382 | void refreshProperty(sdbusplus::bus::bus& bus, |
| 383 | const std::string& path, |
| 384 | const std::string& iface, |
| 385 | const std::string& prop); |
| 386 | |
| 387 | /** |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 388 | * @brief Get a property value from the path/interface given |
| 389 | * |
| 390 | * @param[in] bus - the bus to use |
| 391 | * @param[in] path - the dbus path name |
| 392 | * @param[in] iface - the dbus interface name |
| 393 | * @param[in] prop - the property name |
| 394 | * @param[out] value - the value of the property |
| 395 | */ |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 396 | static void getProperty(sdbusplus::bus::bus& bus, |
| 397 | const std::string& path, |
| 398 | const std::string& iface, |
| 399 | const std::string& prop, |
Matthew Barth | 9e741ed | 2017-06-02 16:29:09 -0500 | [diff] [blame] | 400 | PropertyVariantType& value); |
Matthew Barth | df3e8d6 | 2017-05-31 11:07:24 -0500 | [diff] [blame] | 401 | |
| 402 | /** |
Matthew Barth | 34f1bda | 2017-05-31 13:45:36 -0500 | [diff] [blame] | 403 | * @brief Dbus signal change callback handler |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 404 | * |
Matthew Barth | 34f1bda | 2017-05-31 13:45:36 -0500 | [diff] [blame] | 405 | * @param[in] msg - Expanded sdbusplus message data |
| 406 | * @param[in] eventData - The single event's data |
Matthew Barth | 38a93a8 | 2017-05-11 14:12:27 -0500 | [diff] [blame] | 407 | */ |
Matthew Barth | 34f1bda | 2017-05-31 13:45:36 -0500 | [diff] [blame] | 408 | void handleEvent(sdbusplus::message::message& msg, |
| 409 | const EventData* eventData); |
Matt Spinler | 7f88fe6 | 2017-04-10 14:39:02 -0500 | [diff] [blame] | 410 | }; |
| 411 | |
| 412 | } |
| 413 | } |
| 414 | } |