blob: dd2222b79275752f0688ba0b72ba483ea8267140 [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 /**
104 * Returns the timer object for this sensor
105 */
106 inline phosphor::fan::util::Timer& getTimer()
107 {
108 return _timer;
109 }
110
111 /**
112 * @brief Returns the timeout value to use for the sensor
113 */
114 std::chrono::microseconds getTimeout();
115
Matt Spinlerce75b512017-07-26 15:10:48 -0500116 /**
117 * Returns the sensor name
118 */
119 inline const std::string& name() const
120 {
121 return _name;
122 };
123
Matt Spinlerabf8da32017-04-27 14:08:45 -0500124 private:
125
126 /**
Matt Spinlerebaae612017-04-27 14:21:48 -0500127 * @brief Returns the match string to use for matching
128 * on a properties changed signal.
129 */
130 std::string getMatchString(const std::string& interface);
131
132 /**
133 * @brief Callback function for a tach input properties
134 * changed signal
135 *
136 * @param[in] msg - the dbus message
137 * @param[in] data - user data
138 * @param[in] err - dbus error
139 */
140 static int handleTachChangeSignal(sd_bus_message* msg,
141 void* data,
142 sd_bus_error* err);
143
144 /**
145 * @brief Callback function for a Target properties
146 * changed signal
147 *
148 * @param[in] msg - the dbus message
149 * @param[in] data - user data
150 * @param[in] err - dbus error
151 */
152 static int handleTargetChangeSignal(sd_bus_message* msg,
153 void* data,
154 sd_bus_error* err);
155
156 /**
157 * @brief Reads the Target property and stores in _tachTarget.
158 * Also calls Fan::tachChanged().
159 *
160 * @param[in] msg - the dbus message
161 * @param[in] err - dbus error
162 */
163 void handleTargetChange(sdbusplus::message::message& msg,
164 sd_bus_error* err);
165
166 /**
167 * @brief Reads the Value property and stores in _tachInput.
168 * Also calls Fan::tachChanged().
169 *
170 * @param[in] msg - the dbus message
171 * @param[in] err - dbus error
172 */
173 void handleTachChange(sdbusplus::message::message& msg,
174 sd_bus_error* err);
175
176
177 /**
Matt Spinlerabf8da32017-04-27 14:08:45 -0500178 * @brief the dbus object
179 */
180 sdbusplus::bus::bus& _bus;
181
182 /**
183 * @brief Reference to the parent Fan object
184 */
185 Fan& _fan;
186
187 /**
188 * @brief The name of the sensor, including the full path
189 *
190 * For example /xyz/openbmc_project/sensors/fan_tach/fan0
191 */
192 const std::string _name;
193
194 /**
195 * @brief If functional (not too slow). The parent
196 * fan object sets this.
197 */
198 bool _functional = true;
199
200 /**
201 * @brief If the sensor has a Target property (can set speed)
202 */
203 const bool _hasTarget;
204
205 /**
206 * @brief The input speed, from the Value dbus property
207 */
208 int64_t _tachInput = 0;
209
210 /**
211 * @brief The current target speed, from the Target dbus property
212 * (if applicable)
213 */
214 uint64_t _tachTarget = 0;
215
216 /**
217 * @brief The timeout value to use
218 */
219 const size_t _timeout;
Matt Spinlerebaae612017-04-27 14:21:48 -0500220
221 /**
Matt Spinlera9406a72017-04-27 14:29:24 -0500222 * The timer object
223 */
224 phosphor::fan::util::Timer _timer;
225
226 /**
Matt Spinlerebaae612017-04-27 14:21:48 -0500227 * @brief The match object for the Value properties changed signal
228 */
229 std::unique_ptr<sdbusplus::server::match::match> tachSignal;
230
231 /**
232 * @brief The match object for the Target properties changed signal
233 */
234 std::unique_ptr<sdbusplus::server::match::match> targetSignal;
Matt Spinlerabf8da32017-04-27 14:08:45 -0500235};
236
237}
238}
239}