blob: 9b1011068e107181ba5dfdd73640b7f9e6ac1dc5 [file] [log] [blame]
Matt Spinlerabf8da32017-04-27 14:08:45 -05001#pragma once
2
3#include <chrono>
4#include <sdbusplus/bus.hpp>
5#include <sdbusplus/server.hpp>
Matt Spinlere824f982017-05-11 10:07:55 -05006#include "event.hpp"
Matt Spinlera9406a72017-04-27 14:29:24 -05007#include "timer.hpp"
Matt Spinlerabf8da32017-04-27 14:08:45 -05008
9namespace phosphor
10{
11namespace fan
12{
13namespace monitor
14{
15
16class Fan;
17
Matt Spinler78689dd2017-09-28 10:12:07 -050018constexpr auto FAN_SENSOR_PATH = "/xyz/openbmc_project/sensors/fan_tach/";
Matt Spinlerabf8da32017-04-27 14:08:45 -050019
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 */
32class TachSensor
33{
34 public:
35
36 TachSensor() = delete;
37 TachSensor(const TachSensor&) = delete;
Brad Bishopfa0766e2017-07-30 15:42:10 -040038 // TachSensor is not moveable since the this pointer is used as systemd
39 // callback context.
40 TachSensor(TachSensor&&) = delete;
Matt Spinlerabf8da32017-04-27 14:08:45 -050041 TachSensor& operator=(const TachSensor&) = delete;
Brad Bishopfa0766e2017-07-30 15:42:10 -040042 TachSensor& operator=(TachSensor&&) = delete;
Matt Spinlerabf8da32017-04-27 14:08:45 -050043 ~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 Spinlera9406a72017-04-27 14:29:24 -050054 * @param[in] events - sd_event pointer
Matt Spinlerabf8da32017-04-27 14:08:45 -050055 */
56 TachSensor(sdbusplus::bus::bus& bus,
57 Fan& fan,
58 const std::string& id,
59 bool hasTarget,
Matt Spinlera9406a72017-04-27 14:29:24 -050060 size_t timeout,
Matt Spinlere824f982017-05-11 10:07:55 -050061 phosphor::fan::event::EventPtr& events);
Matt Spinlerabf8da32017-04-27 14:08:45 -050062
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 /**
97 * Sets functional status
98 */
99 inline void setFunctional(bool functional)
100 {
101 _functional = functional;
102 }
103
Matt Spinlera9406a72017-04-27 14:29:24 -0500104 /**
Matt Spinler6fa181c2017-09-27 16:24:45 -0500105 * @brief Says if the timer is running or not
106 *
107 * @return bool - if timer is currently running
Matt Spinlera9406a72017-04-27 14:29:24 -0500108 */
Matt Spinler6fa181c2017-09-27 16:24:45 -0500109 inline bool timerRunning()
Matt Spinlera9406a72017-04-27 14:29:24 -0500110 {
Matt Spinler6fa181c2017-09-27 16:24:45 -0500111 return _timer.running();
112 }
113
114 /**
115 * @brief Starts the timer for the amount of time
116 * specified in the constructor
117 */
118 inline void startTimer()
119 {
120 _timer.start(
121 getTimeout(),
122 phosphor::fan::util::Timer::TimerType::oneshot);
123 }
124
125 /**
126 * @brief Stops the timer
127 */
128 inline void stopTimer()
129 {
130 _timer.stop();
Matt Spinlera9406a72017-04-27 14:29:24 -0500131 }
132
133 /**
134 * @brief Returns the timeout value to use for the sensor
135 */
136 std::chrono::microseconds getTimeout();
137
Matt Spinlerce75b512017-07-26 15:10:48 -0500138 /**
139 * Returns the sensor name
140 */
141 inline const std::string& name() const
142 {
143 return _name;
144 };
145
Matt Spinlerabf8da32017-04-27 14:08:45 -0500146 private:
147
148 /**
Matt Spinlerebaae612017-04-27 14:21:48 -0500149 * @brief Returns the match string to use for matching
150 * on a properties changed signal.
151 */
152 std::string getMatchString(const std::string& interface);
153
154 /**
Matt Spinlerebaae612017-04-27 14:21:48 -0500155 * @brief Reads the Target property and stores in _tachTarget.
156 * Also calls Fan::tachChanged().
157 *
158 * @param[in] msg - the dbus message
Matt Spinlerebaae612017-04-27 14:21:48 -0500159 */
Brad Bishop771659f2017-07-30 19:52:21 -0400160 void handleTargetChange(sdbusplus::message::message& msg);
Matt Spinlerebaae612017-04-27 14:21:48 -0500161
162 /**
163 * @brief Reads the Value property and stores in _tachInput.
164 * Also calls Fan::tachChanged().
165 *
166 * @param[in] msg - the dbus message
Matt Spinlerebaae612017-04-27 14:21:48 -0500167 */
Brad Bishop771659f2017-07-30 19:52:21 -0400168 void handleTachChange(sdbusplus::message::message& msg);
Matt Spinlerebaae612017-04-27 14:21:48 -0500169
170
171 /**
Matt Spinlerabf8da32017-04-27 14:08:45 -0500172 * @brief the dbus object
173 */
174 sdbusplus::bus::bus& _bus;
175
176 /**
177 * @brief Reference to the parent Fan object
178 */
179 Fan& _fan;
180
181 /**
182 * @brief The name of the sensor, including the full path
183 *
184 * For example /xyz/openbmc_project/sensors/fan_tach/fan0
185 */
186 const std::string _name;
187
188 /**
189 * @brief If functional (not too slow). The parent
190 * fan object sets this.
191 */
192 bool _functional = true;
193
194 /**
195 * @brief If the sensor has a Target property (can set speed)
196 */
197 const bool _hasTarget;
198
199 /**
200 * @brief The input speed, from the Value dbus property
201 */
202 int64_t _tachInput = 0;
203
204 /**
205 * @brief The current target speed, from the Target dbus property
206 * (if applicable)
207 */
208 uint64_t _tachTarget = 0;
209
210 /**
211 * @brief The timeout value to use
212 */
213 const size_t _timeout;
Matt Spinlerebaae612017-04-27 14:21:48 -0500214
215 /**
Matt Spinlera9406a72017-04-27 14:29:24 -0500216 * The timer object
217 */
218 phosphor::fan::util::Timer _timer;
219
220 /**
Matt Spinlerebaae612017-04-27 14:21:48 -0500221 * @brief The match object for the Value properties changed signal
222 */
223 std::unique_ptr<sdbusplus::server::match::match> tachSignal;
224
225 /**
226 * @brief The match object for the Target properties changed signal
227 */
228 std::unique_ptr<sdbusplus::server::match::match> targetSignal;
Matt Spinlerabf8da32017-04-27 14:08:45 -0500229};
230
231}
232}
233}