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