blob: dd89bbfbaa4735791470f9ce9d64bf6d7c41bcd0 [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/**
Matthew Barth0a9fe162018-01-26 12:53:15 -060021 * The mode fan monitor will run in:
22 * - init - only do the initialization steps
23 * - monitor - run normal monitoring algorithm
24 */
25enum class Mode
26{
27 init,
28 monitor
29};
30
31/**
Matt Spinlerabf8da32017-04-27 14:08:45 -050032 * @class TachSensor
33 *
34 * This class represents the sensor that reads a tach value.
35 * It may also support a Target, which is the property used to
36 * set a speed. Since it doesn't necessarily have a Target, it
37 * won't for sure know if it is running too slow, so it leaves
38 * that determination to other code.
39 *
40 * This class has a parent Fan object that knows about all
41 * sensors for that fan.
42 */
43class TachSensor
44{
45 public:
46
47 TachSensor() = delete;
48 TachSensor(const TachSensor&) = delete;
Brad Bishopfa0766e2017-07-30 15:42:10 -040049 // TachSensor is not moveable since the this pointer is used as systemd
50 // callback context.
51 TachSensor(TachSensor&&) = delete;
Matt Spinlerabf8da32017-04-27 14:08:45 -050052 TachSensor& operator=(const TachSensor&) = delete;
Brad Bishopfa0766e2017-07-30 15:42:10 -040053 TachSensor& operator=(TachSensor&&) = delete;
Matt Spinlerabf8da32017-04-27 14:08:45 -050054 ~TachSensor() = default;
55
56 /**
57 * @brief Constructor
58 *
Matthew Barth0a9fe162018-01-26 12:53:15 -060059 * @param[in] mode - mode of fan monitor
Matt Spinlerabf8da32017-04-27 14:08:45 -050060 * @param[in] bus - the dbus object
61 * @param[in] fan - the parent fan object
62 * @param[in] id - the id of the sensor
63 * @param[in] hasTarget - if the sensor supports
64 * setting the speed
65 * @param[in] timeout - Normal timeout value to use
Matt Spinlera9406a72017-04-27 14:29:24 -050066 * @param[in] events - sd_event pointer
Matt Spinlerabf8da32017-04-27 14:08:45 -050067 */
Matthew Barth0a9fe162018-01-26 12:53:15 -060068 TachSensor(Mode mode,
69 sdbusplus::bus::bus& bus,
Matt Spinlerabf8da32017-04-27 14:08:45 -050070 Fan& fan,
71 const std::string& id,
72 bool hasTarget,
Matt Spinlera9406a72017-04-27 14:29:24 -050073 size_t timeout,
Matt Spinlere824f982017-05-11 10:07:55 -050074 phosphor::fan::event::EventPtr& events);
Matt Spinlerabf8da32017-04-27 14:08:45 -050075
76 /**
77 * @brief Returns the target speed value
78 */
Matthew Barthf552ea52018-01-15 16:22:04 -060079 uint64_t getTarget() const;
Matt Spinlerabf8da32017-04-27 14:08:45 -050080
81 /**
82 * @brief Returns the input speed value
83 */
84 inline int64_t getInput() const
85 {
86 return _tachInput;
87 }
88
89 /**
90 * @brief Returns true if sensor has a target
91 */
92 inline bool hasTarget() const
93 {
94 return _hasTarget;
95 }
96
97 /**
98 * Returns true if the hardware behind this
99 * sensor is considered working OK/functional.
100 */
101 inline bool functional() const
102 {
103 return _functional;
104 }
105
106 /**
Matthew Barthd199dcd2017-11-20 10:36:41 -0600107 * Set the functional status and update inventory to match
Matt Spinlerabf8da32017-04-27 14:08:45 -0500108 */
Matthew Barthd199dcd2017-11-20 10:36:41 -0600109 void setFunctional(bool functional);
Matt Spinlerabf8da32017-04-27 14:08:45 -0500110
Matt Spinlera9406a72017-04-27 14:29:24 -0500111 /**
Matt Spinler6fa181c2017-09-27 16:24:45 -0500112 * @brief Says if the timer is running or not
113 *
114 * @return bool - if timer is currently running
Matt Spinlera9406a72017-04-27 14:29:24 -0500115 */
Matt Spinler6fa181c2017-09-27 16:24:45 -0500116 inline bool timerRunning()
Matt Spinlera9406a72017-04-27 14:29:24 -0500117 {
Matt Spinler6fa181c2017-09-27 16:24:45 -0500118 return _timer.running();
119 }
120
121 /**
122 * @brief Starts the timer for the amount of time
123 * specified in the constructor
124 */
125 inline void startTimer()
126 {
127 _timer.start(
128 getTimeout(),
129 phosphor::fan::util::Timer::TimerType::oneshot);
130 }
131
132 /**
133 * @brief Stops the timer
134 */
135 inline void stopTimer()
136 {
137 _timer.stop();
Matt Spinlera9406a72017-04-27 14:29:24 -0500138 }
139
140 /**
141 * @brief Returns the timeout value to use for the sensor
142 */
143 std::chrono::microseconds getTimeout();
144
Matt Spinlerce75b512017-07-26 15:10:48 -0500145 /**
146 * Returns the sensor name
147 */
148 inline const std::string& name() const
149 {
150 return _name;
151 };
152
Matt Spinlerabf8da32017-04-27 14:08:45 -0500153 private:
154
155 /**
Matt Spinlerebaae612017-04-27 14:21:48 -0500156 * @brief Returns the match string to use for matching
157 * on a properties changed signal.
158 */
159 std::string getMatchString(const std::string& interface);
160
161 /**
Matt Spinlerebaae612017-04-27 14:21:48 -0500162 * @brief Reads the Target property and stores in _tachTarget.
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 handleTargetChange(sdbusplus::message::message& msg);
Matt Spinlerebaae612017-04-27 14:21:48 -0500168
169 /**
170 * @brief Reads the Value property and stores in _tachInput.
171 * Also calls Fan::tachChanged().
172 *
173 * @param[in] msg - the dbus message
Matt Spinlerebaae612017-04-27 14:21:48 -0500174 */
Brad Bishop771659f2017-07-30 19:52:21 -0400175 void handleTachChange(sdbusplus::message::message& msg);
Matt Spinlerebaae612017-04-27 14:21:48 -0500176
Matthew Barth4d982852017-11-17 09:37:13 -0600177 /**
178 * @brief Updates the Functional property in the inventory
179 * for this tach sensor based on the value passed in.
180 *
181 * @param[in] functional - If the Functional property should
182 * be set to true or false.
183 */
184 void updateInventory(bool functional);
Matt Spinlerebaae612017-04-27 14:21:48 -0500185
186 /**
Matt Spinlerabf8da32017-04-27 14:08:45 -0500187 * @brief the dbus object
188 */
189 sdbusplus::bus::bus& _bus;
190
191 /**
192 * @brief Reference to the parent Fan object
193 */
194 Fan& _fan;
195
196 /**
197 * @brief The name of the sensor, including the full path
198 *
199 * For example /xyz/openbmc_project/sensors/fan_tach/fan0
200 */
201 const std::string _name;
202
203 /**
Matthew Barth4d982852017-11-17 09:37:13 -0600204 * @brief The inventory name of the sensor, including the full path
205 */
206 const std::string _invName;
207
208 /**
Matt Spinlerabf8da32017-04-27 14:08:45 -0500209 * @brief If functional (not too slow). The parent
210 * fan object sets this.
211 */
Matthew Barthd199dcd2017-11-20 10:36:41 -0600212 bool _functional;
Matt Spinlerabf8da32017-04-27 14:08:45 -0500213
214 /**
215 * @brief If the sensor has a Target property (can set speed)
216 */
217 const bool _hasTarget;
218
219 /**
220 * @brief The input speed, from the Value dbus property
221 */
222 int64_t _tachInput = 0;
223
224 /**
225 * @brief The current target speed, from the Target dbus property
226 * (if applicable)
227 */
228 uint64_t _tachTarget = 0;
229
230 /**
231 * @brief The timeout value to use
232 */
233 const size_t _timeout;
Matt Spinlerebaae612017-04-27 14:21:48 -0500234
235 /**
Matt Spinlera9406a72017-04-27 14:29:24 -0500236 * The timer object
237 */
238 phosphor::fan::util::Timer _timer;
239
240 /**
Matt Spinlerebaae612017-04-27 14:21:48 -0500241 * @brief The match object for the Value properties changed signal
242 */
243 std::unique_ptr<sdbusplus::server::match::match> tachSignal;
244
245 /**
246 * @brief The match object for the Target properties changed signal
247 */
248 std::unique_ptr<sdbusplus::server::match::match> targetSignal;
Matt Spinlerabf8da32017-04-27 14:08:45 -0500249};
250
251}
252}
253}