blob: 4e57ef5d022fd6fc33776faa45db018a319ee762 [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 */
Matthew Barthf552ea52018-01-15 16:22:04 -060066 uint64_t getTarget() const;
Matt Spinlerabf8da32017-04-27 14:08:45 -050067
68 /**
69 * @brief Returns the input speed value
70 */
71 inline int64_t getInput() const
72 {
73 return _tachInput;
74 }
75
76 /**
77 * @brief Returns true if sensor has a target
78 */
79 inline bool hasTarget() const
80 {
81 return _hasTarget;
82 }
83
84 /**
85 * Returns true if the hardware behind this
86 * sensor is considered working OK/functional.
87 */
88 inline bool functional() const
89 {
90 return _functional;
91 }
92
93 /**
Matthew Barthd199dcd2017-11-20 10:36:41 -060094 * Set the functional status and update inventory to match
Matt Spinlerabf8da32017-04-27 14:08:45 -050095 */
Matthew Barthd199dcd2017-11-20 10:36:41 -060096 void setFunctional(bool functional);
Matt Spinlerabf8da32017-04-27 14:08:45 -050097
Matt Spinlera9406a72017-04-27 14:29:24 -050098 /**
Matt Spinler6fa181c2017-09-27 16:24:45 -050099 * @brief Says if the timer is running or not
100 *
101 * @return bool - if timer is currently running
Matt Spinlera9406a72017-04-27 14:29:24 -0500102 */
Matt Spinler6fa181c2017-09-27 16:24:45 -0500103 inline bool timerRunning()
Matt Spinlera9406a72017-04-27 14:29:24 -0500104 {
Matt Spinler6fa181c2017-09-27 16:24:45 -0500105 return _timer.running();
106 }
107
108 /**
109 * @brief Starts the timer for the amount of time
110 * specified in the constructor
111 */
112 inline void startTimer()
113 {
114 _timer.start(
115 getTimeout(),
116 phosphor::fan::util::Timer::TimerType::oneshot);
117 }
118
119 /**
120 * @brief Stops the timer
121 */
122 inline void stopTimer()
123 {
124 _timer.stop();
Matt Spinlera9406a72017-04-27 14:29:24 -0500125 }
126
127 /**
128 * @brief Returns the timeout value to use for the sensor
129 */
130 std::chrono::microseconds getTimeout();
131
Matt Spinlerce75b512017-07-26 15:10:48 -0500132 /**
133 * Returns the sensor name
134 */
135 inline const std::string& name() const
136 {
137 return _name;
138 };
139
Matt Spinlerabf8da32017-04-27 14:08:45 -0500140 private:
141
142 /**
Matt Spinlerebaae612017-04-27 14:21:48 -0500143 * @brief Returns the match string to use for matching
144 * on a properties changed signal.
145 */
146 std::string getMatchString(const std::string& interface);
147
148 /**
Matt Spinlerebaae612017-04-27 14:21:48 -0500149 * @brief Reads the Target property and stores in _tachTarget.
150 * Also calls Fan::tachChanged().
151 *
152 * @param[in] msg - the dbus message
Matt Spinlerebaae612017-04-27 14:21:48 -0500153 */
Brad Bishop771659f2017-07-30 19:52:21 -0400154 void handleTargetChange(sdbusplus::message::message& msg);
Matt Spinlerebaae612017-04-27 14:21:48 -0500155
156 /**
157 * @brief Reads the Value property and stores in _tachInput.
158 * Also calls Fan::tachChanged().
159 *
160 * @param[in] msg - the dbus message
Matt Spinlerebaae612017-04-27 14:21:48 -0500161 */
Brad Bishop771659f2017-07-30 19:52:21 -0400162 void handleTachChange(sdbusplus::message::message& msg);
Matt Spinlerebaae612017-04-27 14:21:48 -0500163
Matthew Barth4d982852017-11-17 09:37:13 -0600164 /**
165 * @brief Updates the Functional property in the inventory
166 * for this tach sensor based on the value passed in.
167 *
168 * @param[in] functional - If the Functional property should
169 * be set to true or false.
170 */
171 void updateInventory(bool functional);
Matt Spinlerebaae612017-04-27 14:21:48 -0500172
173 /**
Matt Spinlerabf8da32017-04-27 14:08:45 -0500174 * @brief the dbus object
175 */
176 sdbusplus::bus::bus& _bus;
177
178 /**
179 * @brief Reference to the parent Fan object
180 */
181 Fan& _fan;
182
183 /**
184 * @brief The name of the sensor, including the full path
185 *
186 * For example /xyz/openbmc_project/sensors/fan_tach/fan0
187 */
188 const std::string _name;
189
190 /**
Matthew Barth4d982852017-11-17 09:37:13 -0600191 * @brief The inventory name of the sensor, including the full path
192 */
193 const std::string _invName;
194
195 /**
Matt Spinlerabf8da32017-04-27 14:08:45 -0500196 * @brief If functional (not too slow). The parent
197 * fan object sets this.
198 */
Matthew Barthd199dcd2017-11-20 10:36:41 -0600199 bool _functional;
Matt Spinlerabf8da32017-04-27 14:08:45 -0500200
201 /**
202 * @brief If the sensor has a Target property (can set speed)
203 */
204 const bool _hasTarget;
205
206 /**
207 * @brief The input speed, from the Value dbus property
208 */
209 int64_t _tachInput = 0;
210
211 /**
212 * @brief The current target speed, from the Target dbus property
213 * (if applicable)
214 */
215 uint64_t _tachTarget = 0;
216
217 /**
218 * @brief The timeout value to use
219 */
220 const size_t _timeout;
Matt Spinlerebaae612017-04-27 14:21:48 -0500221
222 /**
Matt Spinlera9406a72017-04-27 14:29:24 -0500223 * The timer object
224 */
225 phosphor::fan::util::Timer _timer;
226
227 /**
Matt Spinlerebaae612017-04-27 14:21:48 -0500228 * @brief The match object for the Value properties changed signal
229 */
230 std::unique_ptr<sdbusplus::server::match::match> tachSignal;
231
232 /**
233 * @brief The match object for the Target properties changed signal
234 */
235 std::unique_ptr<sdbusplus::server::match::match> targetSignal;
Matt Spinlerabf8da32017-04-27 14:08:45 -0500236};
237
238}
239}
240}