blob: 60a55e300c24125f9af53e4345cdee59794d9780 [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
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 */
31class TachSensor
32{
33 public:
34
35 TachSensor() = delete;
36 TachSensor(const TachSensor&) = delete;
Brad Bishopfa0766e2017-07-30 15:42:10 -040037 // TachSensor is not moveable since the this pointer is used as systemd
38 // callback context.
39 TachSensor(TachSensor&&) = delete;
Matt Spinlerabf8da32017-04-27 14:08:45 -050040 TachSensor& operator=(const TachSensor&) = delete;
Brad Bishopfa0766e2017-07-30 15:42:10 -040041 TachSensor& operator=(TachSensor&&) = delete;
Matt Spinlerabf8da32017-04-27 14:08:45 -050042 ~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 Spinlera9406a72017-04-27 14:29:24 -050053 * @param[in] events - sd_event pointer
Matt Spinlerabf8da32017-04-27 14:08:45 -050054 */
55 TachSensor(sdbusplus::bus::bus& bus,
56 Fan& fan,
57 const std::string& id,
58 bool hasTarget,
Matt Spinlera9406a72017-04-27 14:29:24 -050059 size_t timeout,
Matt Spinlere824f982017-05-11 10:07:55 -050060 phosphor::fan::event::EventPtr& events);
Matt Spinlerabf8da32017-04-27 14:08:45 -050061
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 Spinlera9406a72017-04-27 14:29:24 -0500103 /**
Matt Spinler6fa181c2017-09-27 16:24:45 -0500104 * @brief Says if the timer is running or not
105 *
106 * @return bool - if timer is currently running
Matt Spinlera9406a72017-04-27 14:29:24 -0500107 */
Matt Spinler6fa181c2017-09-27 16:24:45 -0500108 inline bool timerRunning()
Matt Spinlera9406a72017-04-27 14:29:24 -0500109 {
Matt Spinler6fa181c2017-09-27 16:24:45 -0500110 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 Spinlera9406a72017-04-27 14:29:24 -0500130 }
131
132 /**
133 * @brief Returns the timeout value to use for the sensor
134 */
135 std::chrono::microseconds getTimeout();
136
Matt Spinlerce75b512017-07-26 15:10:48 -0500137 /**
138 * Returns the sensor name
139 */
140 inline const std::string& name() const
141 {
142 return _name;
143 };
144
Matt Spinlerabf8da32017-04-27 14:08:45 -0500145 private:
146
147 /**
Matt Spinlerebaae612017-04-27 14:21:48 -0500148 * @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 Spinlerebaae612017-04-27 14:21:48 -0500154 * @brief Reads the Target property and stores in _tachTarget.
155 * Also calls Fan::tachChanged().
156 *
157 * @param[in] msg - the dbus message
Matt Spinlerebaae612017-04-27 14:21:48 -0500158 */
Brad Bishop771659f2017-07-30 19:52:21 -0400159 void handleTargetChange(sdbusplus::message::message& msg);
Matt Spinlerebaae612017-04-27 14:21:48 -0500160
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 Spinlerebaae612017-04-27 14:21:48 -0500166 */
Brad Bishop771659f2017-07-30 19:52:21 -0400167 void handleTachChange(sdbusplus::message::message& msg);
Matt Spinlerebaae612017-04-27 14:21:48 -0500168
169
170 /**
Matt Spinlerabf8da32017-04-27 14:08:45 -0500171 * @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 Spinlerebaae612017-04-27 14:21:48 -0500213
214 /**
Matt Spinlera9406a72017-04-27 14:29:24 -0500215 * The timer object
216 */
217 phosphor::fan::util::Timer _timer;
218
219 /**
Matt Spinlerebaae612017-04-27 14:21:48 -0500220 * @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 Spinlerabf8da32017-04-27 14:08:45 -0500228};
229
230}
231}
232}