Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 3 | #include <sdbusplus/bus.hpp> |
| 4 | #include <sdbusplus/server.hpp> |
William A. Kennington III | 8fd879f | 2018-10-30 19:49:29 -0700 | [diff] [blame] | 5 | #include <sdeventplus/clock.hpp> |
William A. Kennington III | 1cfc2f1 | 2018-10-19 17:29:46 -0700 | [diff] [blame] | 6 | #include <sdeventplus/event.hpp> |
William A. Kennington III | 8fd879f | 2018-10-30 19:49:29 -0700 | [diff] [blame] | 7 | #include <sdeventplus/utility/timer.hpp> |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 8 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 9 | #include <chrono> |
| 10 | |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 11 | namespace phosphor |
| 12 | { |
| 13 | namespace fan |
| 14 | { |
| 15 | namespace monitor |
| 16 | { |
| 17 | |
| 18 | class Fan; |
| 19 | |
Matt Spinler | 78689dd | 2017-09-28 10:12:07 -0500 | [diff] [blame] | 20 | constexpr auto FAN_SENSOR_PATH = "/xyz/openbmc_project/sensors/fan_tach/"; |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 21 | |
| 22 | /** |
Matthew Barth | 0a9fe16 | 2018-01-26 12:53:15 -0600 | [diff] [blame] | 23 | * The mode fan monitor will run in: |
| 24 | * - init - only do the initialization steps |
| 25 | * - monitor - run normal monitoring algorithm |
| 26 | */ |
| 27 | enum class Mode |
| 28 | { |
| 29 | init, |
| 30 | monitor |
| 31 | }; |
| 32 | |
| 33 | /** |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 34 | * The mode that the timer is running in: |
| 35 | * - func - Transition to functional state timer |
| 36 | * - nonfunc - Transition to nonfunctional state timer |
| 37 | */ |
| 38 | enum class TimerMode |
| 39 | { |
| 40 | func, |
| 41 | nonfunc |
| 42 | }; |
| 43 | |
| 44 | /** |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 45 | * The mode that the method is running in: |
| 46 | * - time - Use a percentage based deviation |
| 47 | * - count - Run up/down count fault detection |
| 48 | */ |
| 49 | enum MethodMode |
| 50 | { |
| 51 | timebased = 0, |
| 52 | count |
| 53 | }; |
| 54 | |
| 55 | /** |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 56 | * @class TachSensor |
| 57 | * |
| 58 | * This class represents the sensor that reads a tach value. |
| 59 | * It may also support a Target, which is the property used to |
| 60 | * set a speed. Since it doesn't necessarily have a Target, it |
| 61 | * won't for sure know if it is running too slow, so it leaves |
| 62 | * that determination to other code. |
| 63 | * |
| 64 | * This class has a parent Fan object that knows about all |
| 65 | * sensors for that fan. |
| 66 | */ |
| 67 | class TachSensor |
| 68 | { |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 69 | public: |
| 70 | TachSensor() = delete; |
| 71 | TachSensor(const TachSensor&) = delete; |
| 72 | // TachSensor is not moveable since the this pointer is used as systemd |
| 73 | // callback context. |
| 74 | TachSensor(TachSensor&&) = delete; |
| 75 | TachSensor& operator=(const TachSensor&) = delete; |
| 76 | TachSensor& operator=(TachSensor&&) = delete; |
| 77 | ~TachSensor() = default; |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 78 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 79 | /** |
| 80 | * @brief Constructor |
| 81 | * |
| 82 | * @param[in] mode - mode of fan monitor |
| 83 | * @param[in] bus - the dbus object |
| 84 | * @param[in] fan - the parent fan object |
| 85 | * @param[in] id - the id of the sensor |
| 86 | * @param[in] hasTarget - if the sensor supports |
| 87 | * setting the speed |
| 88 | * @param[in] funcDelay - Delay to mark functional |
| 89 | * @param[in] interface - the interface of the target |
| 90 | * @param[in] factor - the factor of the sensor target |
| 91 | * @param[in] offset - the offset of the sensor target |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 92 | * @param[in] method - the method of out of range |
| 93 | * @param[in] threshold - the threshold of counter method |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 94 | * @param[in] timeout - Normal timeout value to use |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 95 | * @param[in] errorDelay - Delay in seconds before creating an error |
| 96 | * or std::nullopt if no errors. |
| 97 | * |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 98 | * @param[in] event - Event loop reference |
| 99 | */ |
| 100 | TachSensor(Mode mode, sdbusplus::bus::bus& bus, Fan& fan, |
| 101 | const std::string& id, bool hasTarget, size_t funcDelay, |
| 102 | const std::string& interface, double factor, int64_t offset, |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 103 | size_t method, size_t threshold, size_t timeout, |
| 104 | const std::optional<size_t>& errorDelay, |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 105 | const sdeventplus::Event& event); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 106 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 107 | /** |
| 108 | * @brief Returns the target speed value |
| 109 | */ |
| 110 | uint64_t getTarget() const; |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 111 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 112 | /** |
| 113 | * @brief Returns the input speed value |
| 114 | */ |
Matthew Barth | 0891e3b | 2020-08-13 12:00:23 -0500 | [diff] [blame] | 115 | inline double getInput() const |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 116 | { |
| 117 | return _tachInput; |
| 118 | } |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 119 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 120 | /** |
| 121 | * @brief Returns true if sensor has a target |
| 122 | */ |
| 123 | inline bool hasTarget() const |
| 124 | { |
| 125 | return _hasTarget; |
| 126 | } |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 127 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 128 | /** |
| 129 | * @brief Returns the interface of the sensor target |
| 130 | */ |
| 131 | inline std::string getInterface() const |
| 132 | { |
| 133 | return _interface; |
| 134 | } |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 135 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 136 | /** |
| 137 | * @brief Returns the factor of the sensor target |
| 138 | */ |
| 139 | inline double getFactor() const |
| 140 | { |
| 141 | return _factor; |
| 142 | } |
Lei YU | 80f271b | 2018-01-31 15:24:46 +0800 | [diff] [blame] | 143 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 144 | /** |
| 145 | * @brief Returns the offset of the sensor target |
| 146 | */ |
| 147 | inline int64_t getOffset() const |
| 148 | { |
| 149 | return _offset; |
| 150 | } |
Lei YU | 8e5d197 | 2018-01-26 17:14:00 +0800 | [diff] [blame] | 151 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 152 | /** |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 153 | * @brief Returns the method of out of range |
| 154 | */ |
| 155 | inline size_t getMethod() const |
| 156 | { |
| 157 | return _method; |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * @brief Returns the threshold of count method |
| 162 | */ |
| 163 | inline size_t getThreshold() const |
| 164 | { |
| 165 | return _threshold; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Set the sensor faulted counter |
| 170 | */ |
| 171 | void setCounter(bool count); |
| 172 | |
| 173 | /** |
| 174 | * @brief Returns the sensor faulted count |
| 175 | */ |
| 176 | inline size_t getCounter() const |
| 177 | { |
| 178 | return _counter; |
| 179 | } |
| 180 | |
| 181 | /** |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 182 | * Returns true if the hardware behind this |
| 183 | * sensor is considered working OK/functional. |
| 184 | */ |
| 185 | inline bool functional() const |
| 186 | { |
| 187 | return _functional; |
| 188 | } |
Lei YU | 8e5d197 | 2018-01-26 17:14:00 +0800 | [diff] [blame] | 189 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 190 | /** |
| 191 | * Set the functional status and update inventory to match |
| 192 | */ |
| 193 | void setFunctional(bool functional); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 194 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 195 | /** |
| 196 | * @brief Says if the timer is running or not |
| 197 | * |
| 198 | * @return bool - if timer is currently running |
| 199 | */ |
| 200 | inline bool timerRunning() |
| 201 | { |
| 202 | return _timer.isEnabled(); |
| 203 | } |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 204 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 205 | /** |
| 206 | * @brief Stops the timer when the given mode differs and starts |
| 207 | * the associated timer for the mode given if not already running |
| 208 | * |
| 209 | * @param[in] mode - mode of timer to start |
| 210 | */ |
| 211 | void startTimer(TimerMode mode); |
Matt Spinler | 6fa181c | 2017-09-27 16:24:45 -0500 | [diff] [blame] | 212 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 213 | /** |
| 214 | * @brief Stops the timer |
| 215 | */ |
| 216 | inline void stopTimer() |
| 217 | { |
| 218 | _timer.setEnabled(false); |
| 219 | } |
Matt Spinler | 6fa181c | 2017-09-27 16:24:45 -0500 | [diff] [blame] | 220 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 221 | /** |
| 222 | * @brief Return the given timer mode's delay time |
| 223 | * |
| 224 | * @param[in] mode - mode of timer to get delay time for |
| 225 | */ |
| 226 | std::chrono::microseconds getDelay(TimerMode mode); |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 227 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 228 | /** |
| 229 | * Returns the sensor name |
| 230 | */ |
| 231 | inline const std::string& name() const |
| 232 | { |
| 233 | return _name; |
| 234 | }; |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 235 | |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 236 | /** |
| 237 | * @brief Says if the error timer is running |
| 238 | * |
| 239 | * @return bool - If the timer is running |
| 240 | */ |
| 241 | bool errorTimerRunning() const |
| 242 | { |
| 243 | if (_errorTimer && _errorTimer->isEnabled()) |
| 244 | { |
| 245 | return true; |
| 246 | } |
| 247 | return false; |
| 248 | } |
| 249 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 250 | private: |
| 251 | /** |
| 252 | * @brief Returns the match string to use for matching |
| 253 | * on a properties changed signal. |
| 254 | */ |
| 255 | std::string getMatchString(const std::string& interface); |
Matt Spinler | ce75b51 | 2017-07-26 15:10:48 -0500 | [diff] [blame] | 256 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 257 | /** |
| 258 | * @brief Reads the Target property and stores in _tachTarget. |
| 259 | * Also calls Fan::tachChanged(). |
| 260 | * |
| 261 | * @param[in] msg - the dbus message |
| 262 | */ |
| 263 | void handleTargetChange(sdbusplus::message::message& msg); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 264 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 265 | /** |
| 266 | * @brief Reads the Value property and stores in _tachInput. |
| 267 | * Also calls Fan::tachChanged(). |
| 268 | * |
| 269 | * @param[in] msg - the dbus message |
| 270 | */ |
| 271 | void handleTachChange(sdbusplus::message::message& msg); |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 272 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 273 | /** |
| 274 | * @brief Updates the Functional property in the inventory |
| 275 | * for this tach sensor based on the value passed in. |
| 276 | * |
| 277 | * @param[in] functional - If the Functional property should |
| 278 | * be set to true or false. |
| 279 | */ |
| 280 | void updateInventory(bool functional); |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 281 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 282 | /** |
| 283 | * @brief the dbus object |
| 284 | */ |
| 285 | sdbusplus::bus::bus& _bus; |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 286 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 287 | /** |
| 288 | * @brief Reference to the parent Fan object |
| 289 | */ |
| 290 | Fan& _fan; |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 291 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 292 | /** |
| 293 | * @brief The name of the sensor, including the full path |
| 294 | * |
| 295 | * For example /xyz/openbmc_project/sensors/fan_tach/fan0 |
| 296 | */ |
| 297 | const std::string _name; |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 298 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 299 | /** |
| 300 | * @brief The inventory name of the sensor, including the full path |
| 301 | */ |
| 302 | const std::string _invName; |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 303 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 304 | /** |
| 305 | * @brief If functional (not too slow). The parent |
| 306 | * fan object sets this. |
| 307 | */ |
| 308 | bool _functional; |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 309 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 310 | /** |
| 311 | * @brief If the sensor has a Target property (can set speed) |
| 312 | */ |
| 313 | const bool _hasTarget; |
Matthew Barth | 4d98285 | 2017-11-17 09:37:13 -0600 | [diff] [blame] | 314 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 315 | /** |
| 316 | * @brief Amount of time to delay updating to functional |
| 317 | */ |
| 318 | const size_t _funcDelay; |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 319 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 320 | /** |
| 321 | * @brief The interface that the target implements |
| 322 | */ |
| 323 | const std::string _interface; |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 324 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 325 | /** |
| 326 | * @brief The factor of target to get fan rpm |
| 327 | */ |
| 328 | const double _factor; |
Matthew Barth | 9396bcc | 2018-02-19 14:13:20 -0600 | [diff] [blame] | 329 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 330 | /** |
| 331 | * @brief The offset of target to get fan rpm |
| 332 | */ |
| 333 | const int64_t _offset; |
Lei YU | 80f271b | 2018-01-31 15:24:46 +0800 | [diff] [blame] | 334 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 335 | /** |
Jolie Ku | 69f2f48 | 2020-10-21 09:59:43 +0800 | [diff] [blame] | 336 | * @brief The method of out of range |
| 337 | */ |
| 338 | const size_t _method; |
| 339 | |
| 340 | /** |
| 341 | * @brief The threshold for count method |
| 342 | */ |
| 343 | const size_t _threshold; |
| 344 | |
| 345 | /** |
| 346 | * @brief The counter for count method |
| 347 | */ |
| 348 | size_t _counter = 0; |
| 349 | |
| 350 | /** |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 351 | * @brief The input speed, from the Value dbus property |
| 352 | */ |
Matthew Barth | 0891e3b | 2020-08-13 12:00:23 -0500 | [diff] [blame] | 353 | double _tachInput = 0; |
Lei YU | 8e5d197 | 2018-01-26 17:14:00 +0800 | [diff] [blame] | 354 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 355 | /** |
| 356 | * @brief The current target speed, from the Target dbus property |
| 357 | * (if applicable) |
| 358 | */ |
| 359 | uint64_t _tachTarget = 0; |
Lei YU | 8e5d197 | 2018-01-26 17:14:00 +0800 | [diff] [blame] | 360 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 361 | /** |
| 362 | * @brief The timeout value to use |
| 363 | */ |
| 364 | const size_t _timeout; |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 365 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 366 | /** |
| 367 | * @brief Mode that current timer is in |
| 368 | */ |
| 369 | TimerMode _timerMode; |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 370 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 371 | /** |
| 372 | * The timer object |
| 373 | */ |
| 374 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> _timer; |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 375 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 376 | /** |
| 377 | * @brief The match object for the Value properties changed signal |
| 378 | */ |
| 379 | std::unique_ptr<sdbusplus::server::match::match> tachSignal; |
Matthew Barth | 3800ae7 | 2018-02-19 16:08:04 -0600 | [diff] [blame] | 380 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 381 | /** |
| 382 | * @brief The match object for the Target properties changed signal |
| 383 | */ |
| 384 | std::unique_ptr<sdbusplus::server::match::match> targetSignal; |
Matt Spinler | f13b42e | 2020-10-26 15:29:49 -0500 | [diff] [blame] | 385 | |
| 386 | /** |
| 387 | * @brief The number of seconds to wait between a sensor being set |
| 388 | * to nonfunctional and creating an error for it. |
| 389 | * |
| 390 | * If std::nullopt, no errors will be created. |
| 391 | */ |
| 392 | const std::optional<size_t> _errorDelay; |
| 393 | |
| 394 | /** |
| 395 | * @brief The timer that uses _errorDelay. When it expires an error |
| 396 | * will be created for a faulted fan sensor (rotor). |
| 397 | * |
| 398 | * If _errorDelay is std::nullopt, then this won't be created. |
| 399 | */ |
| 400 | std::unique_ptr< |
| 401 | sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>> |
| 402 | _errorTimer; |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 403 | }; |
| 404 | |
Matthew Barth | 177fe98 | 2020-05-26 11:05:19 -0500 | [diff] [blame] | 405 | } // namespace monitor |
| 406 | } // namespace fan |
| 407 | } // namespace phosphor |