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