Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <chrono> |
| 4 | #include <sdbusplus/bus.hpp> |
| 5 | #include <sdbusplus/server.hpp> |
Matt Spinler | e824f98 | 2017-05-11 10:07:55 -0500 | [diff] [blame] | 6 | #include "event.hpp" |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 7 | #include "timer.hpp" |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 8 | |
| 9 | namespace phosphor |
| 10 | { |
| 11 | namespace fan |
| 12 | { |
| 13 | namespace monitor |
| 14 | { |
| 15 | |
| 16 | class Fan; |
| 17 | |
| 18 | |
| 19 | /** |
| 20 | * @class TachSensor |
| 21 | * |
| 22 | * This class represents the sensor that reads a tach value. |
| 23 | * It may also support a Target, which is the property used to |
| 24 | * set a speed. Since it doesn't necessarily have a Target, it |
| 25 | * won't for sure know if it is running too slow, so it leaves |
| 26 | * that determination to other code. |
| 27 | * |
| 28 | * This class has a parent Fan object that knows about all |
| 29 | * sensors for that fan. |
| 30 | */ |
| 31 | class TachSensor |
| 32 | { |
| 33 | public: |
| 34 | |
| 35 | TachSensor() = delete; |
| 36 | TachSensor(const TachSensor&) = delete; |
Brad Bishop | fa0766e | 2017-07-30 15:42:10 -0400 | [diff] [blame] | 37 | // TachSensor is not moveable since the this pointer is used as systemd |
| 38 | // callback context. |
| 39 | TachSensor(TachSensor&&) = delete; |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 40 | TachSensor& operator=(const TachSensor&) = delete; |
Brad Bishop | fa0766e | 2017-07-30 15:42:10 -0400 | [diff] [blame] | 41 | TachSensor& operator=(TachSensor&&) = delete; |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 42 | ~TachSensor() = default; |
| 43 | |
| 44 | /** |
| 45 | * @brief Constructor |
| 46 | * |
| 47 | * @param[in] bus - the dbus object |
| 48 | * @param[in] fan - the parent fan object |
| 49 | * @param[in] id - the id of the sensor |
| 50 | * @param[in] hasTarget - if the sensor supports |
| 51 | * setting the speed |
| 52 | * @param[in] timeout - Normal timeout value to use |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 53 | * @param[in] events - sd_event pointer |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 54 | */ |
| 55 | TachSensor(sdbusplus::bus::bus& bus, |
| 56 | Fan& fan, |
| 57 | const std::string& id, |
| 58 | bool hasTarget, |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 59 | size_t timeout, |
Matt Spinler | e824f98 | 2017-05-11 10:07:55 -0500 | [diff] [blame] | 60 | phosphor::fan::event::EventPtr& events); |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * @brief Returns the target speed value |
| 64 | */ |
| 65 | inline uint64_t getTarget() const |
| 66 | { |
| 67 | return _tachTarget; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @brief Returns the input speed value |
| 72 | */ |
| 73 | inline int64_t getInput() const |
| 74 | { |
| 75 | return _tachInput; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @brief Returns true if sensor has a target |
| 80 | */ |
| 81 | inline bool hasTarget() const |
| 82 | { |
| 83 | return _hasTarget; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Returns true if the hardware behind this |
| 88 | * sensor is considered working OK/functional. |
| 89 | */ |
| 90 | inline bool functional() const |
| 91 | { |
| 92 | return _functional; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Sets functional status |
| 97 | */ |
| 98 | inline void setFunctional(bool functional) |
| 99 | { |
| 100 | _functional = functional; |
| 101 | } |
| 102 | |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 103 | /** |
Matt Spinler | 6fa181c | 2017-09-27 16:24:45 -0500 | [diff] [blame^] | 104 | * @brief Says if the timer is running or not |
| 105 | * |
| 106 | * @return bool - if timer is currently running |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 107 | */ |
Matt Spinler | 6fa181c | 2017-09-27 16:24:45 -0500 | [diff] [blame^] | 108 | inline bool timerRunning() |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 109 | { |
Matt Spinler | 6fa181c | 2017-09-27 16:24:45 -0500 | [diff] [blame^] | 110 | return _timer.running(); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * @brief Starts the timer for the amount of time |
| 115 | * specified in the constructor |
| 116 | */ |
| 117 | inline void startTimer() |
| 118 | { |
| 119 | _timer.start( |
| 120 | getTimeout(), |
| 121 | phosphor::fan::util::Timer::TimerType::oneshot); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * @brief Stops the timer |
| 126 | */ |
| 127 | inline void stopTimer() |
| 128 | { |
| 129 | _timer.stop(); |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /** |
| 133 | * @brief Returns the timeout value to use for the sensor |
| 134 | */ |
| 135 | std::chrono::microseconds getTimeout(); |
| 136 | |
Matt Spinler | ce75b51 | 2017-07-26 15:10:48 -0500 | [diff] [blame] | 137 | /** |
| 138 | * Returns the sensor name |
| 139 | */ |
| 140 | inline const std::string& name() const |
| 141 | { |
| 142 | return _name; |
| 143 | }; |
| 144 | |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 145 | private: |
| 146 | |
| 147 | /** |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 148 | * @brief Returns the match string to use for matching |
| 149 | * on a properties changed signal. |
| 150 | */ |
| 151 | std::string getMatchString(const std::string& interface); |
| 152 | |
| 153 | /** |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 154 | * @brief Reads the Target property and stores in _tachTarget. |
| 155 | * Also calls Fan::tachChanged(). |
| 156 | * |
| 157 | * @param[in] msg - the dbus message |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 158 | */ |
Brad Bishop | 771659f | 2017-07-30 19:52:21 -0400 | [diff] [blame] | 159 | void handleTargetChange(sdbusplus::message::message& msg); |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 160 | |
| 161 | /** |
| 162 | * @brief Reads the Value property and stores in _tachInput. |
| 163 | * Also calls Fan::tachChanged(). |
| 164 | * |
| 165 | * @param[in] msg - the dbus message |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 166 | */ |
Brad Bishop | 771659f | 2017-07-30 19:52:21 -0400 | [diff] [blame] | 167 | void handleTachChange(sdbusplus::message::message& msg); |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 168 | |
| 169 | |
| 170 | /** |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 171 | * @brief the dbus object |
| 172 | */ |
| 173 | sdbusplus::bus::bus& _bus; |
| 174 | |
| 175 | /** |
| 176 | * @brief Reference to the parent Fan object |
| 177 | */ |
| 178 | Fan& _fan; |
| 179 | |
| 180 | /** |
| 181 | * @brief The name of the sensor, including the full path |
| 182 | * |
| 183 | * For example /xyz/openbmc_project/sensors/fan_tach/fan0 |
| 184 | */ |
| 185 | const std::string _name; |
| 186 | |
| 187 | /** |
| 188 | * @brief If functional (not too slow). The parent |
| 189 | * fan object sets this. |
| 190 | */ |
| 191 | bool _functional = true; |
| 192 | |
| 193 | /** |
| 194 | * @brief If the sensor has a Target property (can set speed) |
| 195 | */ |
| 196 | const bool _hasTarget; |
| 197 | |
| 198 | /** |
| 199 | * @brief The input speed, from the Value dbus property |
| 200 | */ |
| 201 | int64_t _tachInput = 0; |
| 202 | |
| 203 | /** |
| 204 | * @brief The current target speed, from the Target dbus property |
| 205 | * (if applicable) |
| 206 | */ |
| 207 | uint64_t _tachTarget = 0; |
| 208 | |
| 209 | /** |
| 210 | * @brief The timeout value to use |
| 211 | */ |
| 212 | const size_t _timeout; |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 213 | |
| 214 | /** |
Matt Spinler | a9406a7 | 2017-04-27 14:29:24 -0500 | [diff] [blame] | 215 | * The timer object |
| 216 | */ |
| 217 | phosphor::fan::util::Timer _timer; |
| 218 | |
| 219 | /** |
Matt Spinler | ebaae61 | 2017-04-27 14:21:48 -0500 | [diff] [blame] | 220 | * @brief The match object for the Value properties changed signal |
| 221 | */ |
| 222 | std::unique_ptr<sdbusplus::server::match::match> tachSignal; |
| 223 | |
| 224 | /** |
| 225 | * @brief The match object for the Target properties changed signal |
| 226 | */ |
| 227 | std::unique_ptr<sdbusplus::server::match::match> targetSignal; |
Matt Spinler | abf8da3 | 2017-04-27 14:08:45 -0500 | [diff] [blame] | 228 | }; |
| 229 | |
| 230 | } |
| 231 | } |
| 232 | } |