1 #pragma once
2 
3 #include <chrono>
4 #include <sdbusplus/bus.hpp>
5 #include <sdbusplus/server.hpp>
6 #include "event.hpp"
7 #include "timer.hpp"
8 
9 namespace phosphor
10 {
11 namespace fan
12 {
13 namespace monitor
14 {
15 
16 class 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  */
31 class TachSensor
32 {
33     public:
34 
35         TachSensor() = delete;
36         TachSensor(const TachSensor&) = delete;
37         TachSensor(TachSensor&&) = default;
38         TachSensor& operator=(const TachSensor&) = delete;
39         TachSensor& operator=(TachSensor&&) = default;
40         ~TachSensor() = default;
41 
42         /**
43          * @brief Constructor
44          *
45          * @param[in] bus - the dbus object
46          * @param[in] fan - the parent fan object
47          * @param[in] id - the id of the sensor
48          * @param[in] hasTarget - if the sensor supports
49          *                        setting the speed
50          * @param[in] timeout - Normal timeout value to use
51          * @param[in] events - sd_event pointer
52          */
53         TachSensor(sdbusplus::bus::bus& bus,
54                    Fan& fan,
55                    const std::string& id,
56                    bool hasTarget,
57                    size_t timeout,
58                    phosphor::fan::event::EventPtr& events);
59 
60         /**
61          * @brief Returns the target speed value
62          */
63         inline uint64_t getTarget() const
64         {
65             return _tachTarget;
66         }
67 
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         /**
94          * Sets functional status
95          */
96         inline void setFunctional(bool functional)
97         {
98             _functional = functional;
99         }
100 
101         /**
102          * Returns the timer object for this sensor
103          */
104         inline phosphor::fan::util::Timer& getTimer()
105         {
106             return _timer;
107         }
108 
109         /**
110          * @brief Returns the timeout value to use for the sensor
111          */
112         std::chrono::microseconds getTimeout();
113 
114     private:
115 
116         /**
117          * @brief Returns the service name for reading the sensor
118          */
119         std::string getService();
120 
121         /**
122          * @brief Returns the match string to use for matching
123          *        on a properties changed signal.
124          */
125         std::string getMatchString(const std::string& interface);
126 
127         /**
128          * @brief Callback function for a tach input properties
129          *        changed signal
130          *
131          * @param[in] msg - the dbus message
132          * @param[in] data - user data
133          * @param[in] err - dbus error
134          */
135         static int handleTachChangeSignal(sd_bus_message* msg,
136                                           void* data,
137                                           sd_bus_error* err);
138 
139         /**
140          * @brief Callback function for a Target properties
141          *        changed signal
142          *
143          * @param[in] msg - the dbus message
144          * @param[in] data - user data
145          * @param[in] err - dbus error
146          */
147         static int handleTargetChangeSignal(sd_bus_message* msg,
148                                             void* data,
149                                             sd_bus_error* err);
150 
151         /**
152          * @brief Reads the Target property and stores in _tachTarget.
153          *        Also calls Fan::tachChanged().
154          *
155          * @param[in] msg - the dbus message
156          * @param[in] err - dbus error
157          */
158         void handleTargetChange(sdbusplus::message::message& msg,
159                                 sd_bus_error* err);
160 
161         /**
162          * @brief Reads the Value property and stores in _tachInput.
163          *        Also calls Fan::tachChanged().
164          *
165          * @param[in] msg - the dbus message
166          * @param[in] err - dbus error
167          */
168         void handleTachChange(sdbusplus::message::message& msg,
169                               sd_bus_error* err);
170 
171 
172         /**
173          * @brief the dbus object
174          */
175         sdbusplus::bus::bus& _bus;
176 
177         /**
178          * @brief Reference to the parent Fan object
179          */
180         Fan& _fan;
181 
182         /**
183          * @brief The name of the sensor, including the full path
184          *
185          * For example /xyz/openbmc_project/sensors/fan_tach/fan0
186          */
187         const std::string _name;
188 
189         /**
190          * @brief If functional (not too slow).  The parent
191          *        fan object sets this.
192          */
193         bool _functional = true;
194 
195         /**
196          * @brief If the sensor has a Target property (can set speed)
197          */
198         const bool _hasTarget;
199 
200         /**
201          * @brief The input speed, from the Value dbus property
202          */
203         int64_t _tachInput = 0;
204 
205         /**
206          * @brief The current target speed, from the Target dbus property
207          *        (if applicable)
208          */
209         uint64_t _tachTarget = 0;
210 
211         /**
212          * @brief The timeout value to use
213          */
214         const size_t _timeout;
215 
216         /**
217          * The timer object
218          */
219         phosphor::fan::util::Timer _timer;
220 
221         /**
222          * @brief The match object for the Value properties changed signal
223          */
224         std::unique_ptr<sdbusplus::server::match::match> tachSignal;
225 
226         /**
227          * @brief The match object for the Target properties changed signal
228          */
229         std::unique_ptr<sdbusplus::server::match::match> targetSignal;
230 };
231 
232 }
233 }
234 }
235