1abf8da36SMatt Spinler #pragma once
2abf8da36SMatt Spinler 
30d0e3554SMatthew Barth #include <fmt/format.h>
40d0e3554SMatthew Barth 
50d0e3554SMatthew Barth #include <phosphor-logging/log.hpp>
6abf8da36SMatt Spinler #include <sdbusplus/bus.hpp>
73ea9ec2bSPatrick Williams #include <sdbusplus/bus/match.hpp>
88fd879fbSWilliam A. Kennington III #include <sdeventplus/clock.hpp>
91cfc2f11SWilliam A. Kennington III #include <sdeventplus/event.hpp>
108fd879fbSWilliam A. Kennington III #include <sdeventplus/utility/timer.hpp>
11abf8da36SMatt Spinler 
12177fe986SMatthew Barth #include <chrono>
137b34ee0fSMike Capps #include <deque>
148a8aa442SMatthew Barth #include <optional>
157c23a049SMatthew Barth #include <utility>
16177fe986SMatthew Barth 
17abf8da36SMatt Spinler namespace phosphor
18abf8da36SMatt Spinler {
19abf8da36SMatt Spinler namespace fan
20abf8da36SMatt Spinler {
21abf8da36SMatt Spinler namespace monitor
22abf8da36SMatt Spinler {
23abf8da36SMatt Spinler 
24abf8da36SMatt Spinler class Fan;
25abf8da36SMatt Spinler 
2678689dd7SMatt Spinler constexpr auto FAN_SENSOR_PATH = "/xyz/openbmc_project/sensors/fan_tach/";
27abf8da36SMatt Spinler 
28abf8da36SMatt Spinler /**
290a9fe160SMatthew Barth  * The mode fan monitor will run in:
300a9fe160SMatthew Barth  *   - init - only do the initialization steps
310a9fe160SMatthew Barth  *   - monitor - run normal monitoring algorithm
320a9fe160SMatthew Barth  */
330a9fe160SMatthew Barth enum class Mode
340a9fe160SMatthew Barth {
350a9fe160SMatthew Barth     init,
360a9fe160SMatthew Barth     monitor
370a9fe160SMatthew Barth };
380a9fe160SMatthew Barth 
390a9fe160SMatthew Barth /**
403800ae71SMatthew Barth  * The mode that the timer is running in:
413800ae71SMatthew Barth  *   - func - Transition to functional state timer
423800ae71SMatthew Barth  *   - nonfunc - Transition to nonfunctional state timer
433800ae71SMatthew Barth  */
443800ae71SMatthew Barth enum class TimerMode
453800ae71SMatthew Barth {
463800ae71SMatthew Barth     func,
473800ae71SMatthew Barth     nonfunc
483800ae71SMatthew Barth };
493800ae71SMatthew Barth 
503800ae71SMatthew Barth /**
5169f2f48eSJolie Ku  * The mode that the method is running in:
5269f2f48eSJolie Ku  *   - time - Use a percentage based deviation
5369f2f48eSJolie Ku  *   - count - Run up/down count fault detection
5469f2f48eSJolie Ku  */
5569f2f48eSJolie Ku enum MethodMode
5669f2f48eSJolie Ku {
5769f2f48eSJolie Ku     timebased = 0,
5869f2f48eSJolie Ku     count
5969f2f48eSJolie Ku };
6069f2f48eSJolie Ku 
6169f2f48eSJolie Ku /**
62abf8da36SMatt Spinler  * @class TachSensor
63abf8da36SMatt Spinler  *
64abf8da36SMatt Spinler  * This class represents the sensor that reads a tach value.
65abf8da36SMatt Spinler  * It may also support a Target, which is the property used to
66abf8da36SMatt Spinler  * set a speed.  Since it doesn't necessarily have a Target, it
67abf8da36SMatt Spinler  * won't for sure know if it is running too slow, so it leaves
68abf8da36SMatt Spinler  * that determination to other code.
69abf8da36SMatt Spinler  *
70abf8da36SMatt Spinler  * This class has a parent Fan object that knows about all
71abf8da36SMatt Spinler  * sensors for that fan.
72abf8da36SMatt Spinler  */
73abf8da36SMatt Spinler class TachSensor
74abf8da36SMatt Spinler {
75abf8da36SMatt Spinler   public:
76abf8da36SMatt Spinler     TachSensor() = delete;
77abf8da36SMatt Spinler     TachSensor(const TachSensor&) = delete;
78fa0766e3SBrad Bishop     // TachSensor is not moveable since the this pointer is used as systemd
79fa0766e3SBrad Bishop     // callback context.
80fa0766e3SBrad Bishop     TachSensor(TachSensor&&) = delete;
81abf8da36SMatt Spinler     TachSensor& operator=(const TachSensor&) = delete;
82fa0766e3SBrad Bishop     TachSensor& operator=(TachSensor&&) = delete;
83abf8da36SMatt Spinler     ~TachSensor() = default;
84abf8da36SMatt Spinler 
85abf8da36SMatt Spinler     /**
86abf8da36SMatt Spinler      * @brief Constructor
87abf8da36SMatt Spinler      *
880a9fe160SMatthew Barth      * @param[in] mode - mode of fan monitor
89abf8da36SMatt Spinler      * @param[in] bus - the dbus object
90abf8da36SMatt Spinler      * @param[in] fan - the parent fan object
91abf8da36SMatt Spinler      * @param[in] id - the id of the sensor
92abf8da36SMatt Spinler      * @param[in] hasTarget - if the sensor supports
93abf8da36SMatt Spinler      *                        setting the speed
949396bcc3SMatthew Barth      * @param[in] funcDelay - Delay to mark functional
9580f271b2SLei YU      * @param[in] interface - the interface of the target
9627cc39f1SChau Ly      * @param[in] path - the object path of the sensor target
978e5d197bSLei YU      * @param[in] factor - the factor of the sensor target
988e5d197bSLei YU      * @param[in] offset - the offset of the sensor target
9969f2f48eSJolie Ku      * @param[in] method - the method of out of range
10069f2f48eSJolie Ku      * @param[in] threshold - the threshold of counter method
1018a8aa442SMatthew Barth      * @param[in] ignoreAboveMax - whether to ignore being above max or not
102abf8da36SMatt Spinler      * @param[in] timeout - Normal timeout value to use
103f13b42e2SMatt Spinler      * @param[in] errorDelay - Delay in seconds before creating an error
104f13b42e2SMatt Spinler      *                         or std::nullopt if no errors.
105fdfcc679SMatt Spinler      * @param[in] countInterval - In count mode interval
106f13b42e2SMatt Spinler      *
1071cfc2f11SWilliam A. Kennington III      * @param[in] event - Event loop reference
108abf8da36SMatt Spinler      */
109cb356d48SPatrick Williams     TachSensor(Mode mode, sdbusplus::bus_t& bus, Fan& fan,
110177fe986SMatthew Barth                const std::string& id, bool hasTarget, size_t funcDelay,
11127cc39f1SChau Ly                const std::string& interface, const std::string& path,
11227cc39f1SChau Ly                double factor, int64_t offset, size_t method, size_t threshold,
11327cc39f1SChau Ly                bool ignoreAboveMax, size_t timeout,
11427cc39f1SChau Ly                const std::optional<size_t>& errorDelay, size_t countInterval,
11527cc39f1SChau Ly                const sdeventplus::Event& event);
116abf8da36SMatt Spinler 
117abf8da36SMatt Spinler     /**
118c8c8ccf3SMatthew Barth      * @brief Reads a property from the input message and stores it in value.
119c8c8ccf3SMatthew Barth      *        T is the value type.
120c8c8ccf3SMatthew Barth      *
121c8c8ccf3SMatthew Barth      *        Note: This can only be called once per message.
122c8c8ccf3SMatthew Barth      *
123c8c8ccf3SMatthew Barth      * @param[in] msg - the dbus message that contains the data
124c8c8ccf3SMatthew Barth      * @param[in] interface - the interface the property is on
125c8c8ccf3SMatthew Barth      * @param[in] propertName - the name of the property
126c8c8ccf3SMatthew Barth      * @param[out] value - the value to store the property value in
127c8c8ccf3SMatthew Barth      */
128c8c8ccf3SMatthew Barth     template <typename T>
129cb356d48SPatrick Williams     static void readPropertyFromMessage(sdbusplus::message_t& msg,
130c8c8ccf3SMatthew Barth                                         const std::string& interface,
131c8c8ccf3SMatthew Barth                                         const std::string& propertyName,
132c8c8ccf3SMatthew Barth                                         T& value)
133c8c8ccf3SMatthew Barth     {
134c8c8ccf3SMatthew Barth         std::string sensor;
135c8c8ccf3SMatthew Barth         std::map<std::string, std::variant<T>> data;
136c8c8ccf3SMatthew Barth         msg.read(sensor, data);
137c8c8ccf3SMatthew Barth 
138c8c8ccf3SMatthew Barth         if (sensor.compare(interface) == 0)
139c8c8ccf3SMatthew Barth         {
140c8c8ccf3SMatthew Barth             auto propertyMap = data.find(propertyName);
141c8c8ccf3SMatthew Barth             if (propertyMap != data.end())
142c8c8ccf3SMatthew Barth             {
143c8c8ccf3SMatthew Barth                 value = std::get<T>(propertyMap->second);
144c8c8ccf3SMatthew Barth             }
145c8c8ccf3SMatthew Barth         }
146c8c8ccf3SMatthew Barth     }
147c8c8ccf3SMatthew Barth 
148c8c8ccf3SMatthew Barth     /**
149abf8da36SMatt Spinler      * @brief Returns the target speed value
150abf8da36SMatt Spinler      */
151f552ea5cSMatthew Barth     uint64_t getTarget() const;
152abf8da36SMatt Spinler 
153abf8da36SMatt Spinler     /**
154abf8da36SMatt Spinler      * @brief Returns the input speed value
155abf8da36SMatt Spinler      */
1560891e3b3SMatthew Barth     inline double getInput() const
157abf8da36SMatt Spinler     {
158abf8da36SMatt Spinler         return _tachInput;
159abf8da36SMatt Spinler     }
160abf8da36SMatt Spinler 
161abf8da36SMatt Spinler     /**
162abf8da36SMatt Spinler      * @brief Returns true if sensor has a target
163abf8da36SMatt Spinler      */
164abf8da36SMatt Spinler     inline bool hasTarget() const
165abf8da36SMatt Spinler     {
166abf8da36SMatt Spinler         return _hasTarget;
167abf8da36SMatt Spinler     }
168abf8da36SMatt Spinler 
169abf8da36SMatt Spinler     /**
17080f271b2SLei YU      * @brief Returns the interface of the sensor target
17180f271b2SLei YU      */
17280f271b2SLei YU     inline std::string getInterface() const
17380f271b2SLei YU     {
17480f271b2SLei YU         return _interface;
17580f271b2SLei YU     }
17680f271b2SLei YU 
17780f271b2SLei YU     /**
178fdcd5db3SMike Capps      * @brief Returns true if sensor has a D-Bus owner
179fdcd5db3SMike Capps      */
180fdcd5db3SMike Capps     inline bool hasOwner() const
181fdcd5db3SMike Capps     {
182fdcd5db3SMike Capps         return _hasOwner;
183fdcd5db3SMike Capps     }
184fdcd5db3SMike Capps 
185fdcd5db3SMike Capps     /**
186fdcd5db3SMike Capps      * @brief sets D-Bus owner status
187fdcd5db3SMike Capps      *
188fdcd5db3SMike Capps      * @param[in] val - new owner status
189fdcd5db3SMike Capps      */
190fdcd5db3SMike Capps     inline void setOwner(bool val)
191fdcd5db3SMike Capps     {
192fdcd5db3SMike Capps         _hasOwner = val;
193fdcd5db3SMike Capps     }
194fdcd5db3SMike Capps 
195fdcd5db3SMike Capps     /**
1968e5d197bSLei YU      * @brief Returns the factor of the sensor target
1978e5d197bSLei YU      */
1985e7298c5SMatthew Barth     inline double getFactor() const
1998e5d197bSLei YU     {
2008e5d197bSLei YU         return _factor;
2018e5d197bSLei YU     }
2028e5d197bSLei YU 
2038e5d197bSLei YU     /**
204fdcd5db3SMike Capps      * @brief Returns a reference to the sensor's Fan object
205fdcd5db3SMike Capps      */
206fdcd5db3SMike Capps     inline Fan& getFan() const
207fdcd5db3SMike Capps     {
208fdcd5db3SMike Capps         return _fan;
209fdcd5db3SMike Capps     }
210fdcd5db3SMike Capps 
211fdcd5db3SMike Capps     /**
2128e5d197bSLei YU      * @brief Returns the offset of the sensor target
2138e5d197bSLei YU      */
2144eac3cf9SLei YU     inline int64_t getOffset() const
2158e5d197bSLei YU     {
2168e5d197bSLei YU         return _offset;
2178e5d197bSLei YU     }
2188e5d197bSLei YU 
2198e5d197bSLei YU     /**
22069f2f48eSJolie Ku      * @brief Returns the method of out of range
22169f2f48eSJolie Ku      */
22269f2f48eSJolie Ku     inline size_t getMethod() const
22369f2f48eSJolie Ku     {
22469f2f48eSJolie Ku         return _method;
22569f2f48eSJolie Ku     }
22669f2f48eSJolie Ku 
22769f2f48eSJolie Ku     /**
22869f2f48eSJolie Ku      * @brief Returns the threshold of count method
22969f2f48eSJolie Ku      */
23069f2f48eSJolie Ku     inline size_t getThreshold() const
23169f2f48eSJolie Ku     {
23269f2f48eSJolie Ku         return _threshold;
23369f2f48eSJolie Ku     }
23469f2f48eSJolie Ku 
23569f2f48eSJolie Ku     /**
23669f2f48eSJolie Ku      * Set the sensor faulted counter
23769f2f48eSJolie Ku      */
23869f2f48eSJolie Ku     void setCounter(bool count);
23969f2f48eSJolie Ku 
24069f2f48eSJolie Ku     /**
24169f2f48eSJolie Ku      * @brief Returns the sensor faulted count
24269f2f48eSJolie Ku      */
24369f2f48eSJolie Ku     inline size_t getCounter() const
24469f2f48eSJolie Ku     {
24569f2f48eSJolie Ku         return _counter;
24669f2f48eSJolie Ku     }
24769f2f48eSJolie Ku 
24869f2f48eSJolie Ku     /**
249abf8da36SMatt Spinler      * Returns true if the hardware behind this
250abf8da36SMatt Spinler      * sensor is considered working OK/functional.
251abf8da36SMatt Spinler      */
252abf8da36SMatt Spinler     inline bool functional() const
253abf8da36SMatt Spinler     {
254abf8da36SMatt Spinler         return _functional;
255abf8da36SMatt Spinler     }
256abf8da36SMatt Spinler 
257abf8da36SMatt Spinler     /**
258d199dcdfSMatthew Barth      * Set the functional status and update inventory to match
259ae01b5fcSMatt Spinler      *
260ae01b5fcSMatt Spinler      * @param[in] functional - The new state
261ae01b5fcSMatt Spinler      * @param[in] skipErrorTimer - If setting the sensor to
262ae01b5fcSMatt Spinler      *            nonfunctional, don't start the error timer.
263abf8da36SMatt Spinler      */
264ae01b5fcSMatt Spinler     void setFunctional(bool functional, bool skipErrorTimer = false);
265abf8da36SMatt Spinler 
266a9406a77SMatt Spinler     /**
2676fa181c7SMatt Spinler      * @brief Says if the timer is running or not
2686fa181c7SMatt Spinler      *
2696fa181c7SMatt Spinler      * @return bool - if timer is currently running
270a9406a77SMatt Spinler      */
2716fa181c7SMatt Spinler     inline bool timerRunning()
272a9406a77SMatt Spinler     {
2738fd879fbSWilliam A. Kennington III         return _timer.isEnabled();
2746fa181c7SMatt Spinler     }
2756fa181c7SMatt Spinler 
2766fa181c7SMatt Spinler     /**
2773800ae71SMatthew Barth      * @brief Stops the timer when the given mode differs and starts
2783800ae71SMatthew Barth      * the associated timer for the mode given if not already running
2793800ae71SMatthew Barth      *
2803800ae71SMatthew Barth      * @param[in] mode - mode of timer to start
2816fa181c7SMatt Spinler      */
2823800ae71SMatthew Barth     void startTimer(TimerMode mode);
2836fa181c7SMatt Spinler 
2846fa181c7SMatt Spinler     /**
2856fa181c7SMatt Spinler      * @brief Stops the timer
2866fa181c7SMatt Spinler      */
2876fa181c7SMatt Spinler     inline void stopTimer()
2886fa181c7SMatt Spinler     {
2891d7379eaSMatt Spinler         phosphor::logging::log<phosphor::logging::level::DEBUG>(
2900d0e3554SMatthew Barth             fmt::format("Stop running timer on tach sensor {}.", _name)
2910d0e3554SMatthew Barth                 .c_str());
2928fd879fbSWilliam A. Kennington III         _timer.setEnabled(false);
293a9406a77SMatt Spinler     }
294a9406a77SMatt Spinler 
295a9406a77SMatt Spinler     /**
296fdfcc679SMatt Spinler      * @brief Says if the count timer is running
297fdfcc679SMatt Spinler      *
298fdfcc679SMatt Spinler      * @return bool - If count timer running
299fdfcc679SMatt Spinler      */
300fdfcc679SMatt Spinler     inline bool countTimerRunning() const
301fdfcc679SMatt Spinler     {
302fdfcc679SMatt Spinler         return _countTimer && _countTimer->isEnabled();
303fdfcc679SMatt Spinler     }
304fdfcc679SMatt Spinler 
305fdfcc679SMatt Spinler     /**
306fdfcc679SMatt Spinler      * @brief Stops the count timer
307fdfcc679SMatt Spinler      */
308fdfcc679SMatt Spinler     void stopCountTimer();
309fdfcc679SMatt Spinler 
310fdfcc679SMatt Spinler     /**
311fdfcc679SMatt Spinler      * @brief Starts the count timer
312fdfcc679SMatt Spinler      */
313fdfcc679SMatt Spinler     void startCountTimer();
314fdfcc679SMatt Spinler 
315fdfcc679SMatt Spinler     /**
3163800ae71SMatthew Barth      * @brief Return the given timer mode's delay time
3173800ae71SMatthew Barth      *
3183800ae71SMatthew Barth      * @param[in] mode - mode of timer to get delay time for
319a9406a77SMatt Spinler      */
3203800ae71SMatthew Barth     std::chrono::microseconds getDelay(TimerMode mode);
321a9406a77SMatt Spinler 
322ce75b511SMatt Spinler     /**
323ce75b511SMatt Spinler      * Returns the sensor name
324ce75b511SMatt Spinler      */
325ce75b511SMatt Spinler     inline const std::string& name() const
326ce75b511SMatt Spinler     {
327ce75b511SMatt Spinler         return _name;
328ce75b511SMatt Spinler     };
329ce75b511SMatt Spinler 
330f13b42e2SMatt Spinler     /**
331f13b42e2SMatt Spinler      * @brief Says if the error timer is running
332f13b42e2SMatt Spinler      *
333f13b42e2SMatt Spinler      * @return bool - If the timer is running
334f13b42e2SMatt Spinler      */
335f13b42e2SMatt Spinler     bool errorTimerRunning() const
336f13b42e2SMatt Spinler     {
337f13b42e2SMatt Spinler         if (_errorTimer && _errorTimer->isEnabled())
338f13b42e2SMatt Spinler         {
339f13b42e2SMatt Spinler             return true;
340f13b42e2SMatt Spinler         }
341f13b42e2SMatt Spinler         return false;
342f13b42e2SMatt Spinler     }
343f13b42e2SMatt Spinler 
3447c23a049SMatthew Barth     /**
3457c23a049SMatthew Barth      * @brief Get the current allowed range of speeds
3467c23a049SMatthew Barth      *
347*f724c16bSMatt Spinler      * @param[in] lowerDeviation - The configured lower deviation(in percent)
348*f724c16bSMatt Spinler      *                             allowed
349*f724c16bSMatt Spinler      * @param[in] upperDeviation - The configured upper deviation(in percent)
350*f724c16bSMatt Spinler      *                             allowed
3517c23a049SMatthew Barth      *
3528a8aa442SMatthew Barth      * @return pair - Min/Max(optional) range of speeds allowed
3537c23a049SMatthew Barth      */
3548a8aa442SMatthew Barth     std::pair<uint64_t, std::optional<uint64_t>>
355*f724c16bSMatt Spinler         getRange(const size_t lowerDeviation,
356*f724c16bSMatt Spinler                  const size_t upperDeviation) const;
3577c23a049SMatthew Barth 
358fcb0dbcbSMatthew Barth     /**
359fcb0dbcbSMatthew Barth      * @brief Processes the current state of the sensor
360fcb0dbcbSMatthew Barth      */
361fcb0dbcbSMatthew Barth     void processState();
362fcb0dbcbSMatthew Barth 
363fcb0dbcbSMatthew Barth     /**
364fcb0dbcbSMatthew Barth      * @brief Resets the monitoring method of the sensor
365fcb0dbcbSMatthew Barth      */
366fcb0dbcbSMatthew Barth     void resetMethod();
367fcb0dbcbSMatthew Barth 
3684283c5d5SMatt Spinler     /**
3694283c5d5SMatt Spinler      * @brief Refreshes the tach input and target values by
3704283c5d5SMatt Spinler      *        reading them from D-Bus.
3714283c5d5SMatt Spinler      */
3724283c5d5SMatt Spinler     void updateTachAndTarget();
3734283c5d5SMatt Spinler 
3747b34ee0fSMike Capps     /**
3757b34ee0fSMike Capps      * @brief return the previous tach values
3767b34ee0fSMike Capps      */
3777b34ee0fSMike Capps     const std::deque<uint64_t>& getPrevTach() const
3787b34ee0fSMike Capps     {
3797b34ee0fSMike Capps         return _prevTachs;
3807b34ee0fSMike Capps     }
3817b34ee0fSMike Capps 
3827b34ee0fSMike Capps     /**
3837b34ee0fSMike Capps      * @brief return the previous target values
3847b34ee0fSMike Capps      */
3857b34ee0fSMike Capps     const std::deque<uint64_t>& getPrevTarget() const
3867b34ee0fSMike Capps     {
3877b34ee0fSMike Capps         return _prevTargets;
3887b34ee0fSMike Capps     }
3897b34ee0fSMike Capps 
390abf8da36SMatt Spinler   private:
391abf8da36SMatt Spinler     /**
392ebaae611SMatt Spinler      * @brief Returns the match string to use for matching
393ebaae611SMatt Spinler      *        on a properties changed signal.
394ebaae611SMatt Spinler      */
39527cc39f1SChau Ly     std::string getMatchString(const std::optional<std::string> path,
39627cc39f1SChau Ly                                const std::string& interface);
397ebaae611SMatt Spinler 
398ebaae611SMatt Spinler     /**
399ebaae611SMatt Spinler      * @brief Reads the Target property and stores in _tachTarget.
400ebaae611SMatt Spinler      *        Also calls Fan::tachChanged().
401ebaae611SMatt Spinler      *
402ebaae611SMatt Spinler      * @param[in] msg - the dbus message
403ebaae611SMatt Spinler      */
404cb356d48SPatrick Williams     void handleTargetChange(sdbusplus::message_t& msg);
405ebaae611SMatt Spinler 
406ebaae611SMatt Spinler     /**
407ebaae611SMatt Spinler      * @brief Reads the Value property and stores in _tachInput.
408ebaae611SMatt Spinler      *        Also calls Fan::tachChanged().
409ebaae611SMatt Spinler      *
410ebaae611SMatt Spinler      * @param[in] msg - the dbus message
411ebaae611SMatt Spinler      */
412cb356d48SPatrick Williams     void handleTachChange(sdbusplus::message_t& msg);
413ebaae611SMatt Spinler 
4144d982856SMatthew Barth     /**
4154d982856SMatthew Barth      * @brief Updates the Functional property in the inventory
4164d982856SMatthew Barth      *        for this tach sensor based on the value passed in.
4174d982856SMatthew Barth      *
4184d982856SMatthew Barth      * @param[in] functional - If the Functional property should
4194d982856SMatthew Barth      *                         be set to true or false.
4204d982856SMatthew Barth      */
4214d982856SMatthew Barth     void updateInventory(bool functional);
422ebaae611SMatt Spinler 
423ebaae611SMatt Spinler     /**
424abf8da36SMatt Spinler      * @brief the dbus object
425abf8da36SMatt Spinler      */
426cb356d48SPatrick Williams     sdbusplus::bus_t& _bus;
427abf8da36SMatt Spinler 
428abf8da36SMatt Spinler     /**
429abf8da36SMatt Spinler      * @brief Reference to the parent Fan object
430abf8da36SMatt Spinler      */
431abf8da36SMatt Spinler     Fan& _fan;
432abf8da36SMatt Spinler 
433abf8da36SMatt Spinler     /**
434abf8da36SMatt Spinler      * @brief The name of the sensor, including the full path
435abf8da36SMatt Spinler      *
436abf8da36SMatt Spinler      * For example /xyz/openbmc_project/sensors/fan_tach/fan0
437abf8da36SMatt Spinler      */
438abf8da36SMatt Spinler     const std::string _name;
439abf8da36SMatt Spinler 
440abf8da36SMatt Spinler     /**
4414d982856SMatthew Barth      * @brief The inventory name of the sensor, including the full path
4424d982856SMatthew Barth      */
4434d982856SMatthew Barth     const std::string _invName;
4444d982856SMatthew Barth 
4454d982856SMatthew Barth     /**
446abf8da36SMatt Spinler      * @brief If functional (not too slow).  The parent
447abf8da36SMatt Spinler      *        fan object sets this.
448abf8da36SMatt Spinler      */
449ae01b5fcSMatt Spinler     bool _functional = true;
450abf8da36SMatt Spinler 
451abf8da36SMatt Spinler     /**
452abf8da36SMatt Spinler      * @brief If the sensor has a Target property (can set speed)
453abf8da36SMatt Spinler      */
454abf8da36SMatt Spinler     const bool _hasTarget;
455abf8da36SMatt Spinler 
456abf8da36SMatt Spinler     /**
457fdcd5db3SMike Capps      * @brief If the sensor has a D-Bus owner
458fdcd5db3SMike Capps      */
459fdcd5db3SMike Capps     bool _hasOwner = true;
460fdcd5db3SMike Capps 
461fdcd5db3SMike Capps     /**
4629396bcc3SMatthew Barth      * @brief Amount of time to delay updating to functional
4639396bcc3SMatthew Barth      */
4649396bcc3SMatthew Barth     const size_t _funcDelay;
4659396bcc3SMatthew Barth 
4669396bcc3SMatthew Barth     /**
46780f271b2SLei YU      * @brief The interface that the target implements
46880f271b2SLei YU      */
46980f271b2SLei YU     const std::string _interface;
47080f271b2SLei YU 
47180f271b2SLei YU     /**
47227cc39f1SChau Ly      * @brief The object path to set sensor's target
47327cc39f1SChau Ly      */
47427cc39f1SChau Ly     const std::string _path;
47527cc39f1SChau Ly 
47627cc39f1SChau Ly     /**
4778e5d197bSLei YU      * @brief The factor of target to get fan rpm
4788e5d197bSLei YU      */
4795e7298c5SMatthew Barth     const double _factor;
4808e5d197bSLei YU 
4818e5d197bSLei YU     /**
4828e5d197bSLei YU      * @brief The offset of target to get fan rpm
4838e5d197bSLei YU      */
4844eac3cf9SLei YU     const int64_t _offset;
4858e5d197bSLei YU 
4868e5d197bSLei YU     /**
48769f2f48eSJolie Ku      * @brief The method of out of range
48869f2f48eSJolie Ku      */
48969f2f48eSJolie Ku     const size_t _method;
49069f2f48eSJolie Ku 
49169f2f48eSJolie Ku     /**
49269f2f48eSJolie Ku      * @brief The threshold for count method
49369f2f48eSJolie Ku      */
49469f2f48eSJolie Ku     const size_t _threshold;
49569f2f48eSJolie Ku 
49669f2f48eSJolie Ku     /**
4978a8aa442SMatthew Barth      * @brief Whether to ignore being above the max or not
4988a8aa442SMatthew Barth      */
4998a8aa442SMatthew Barth     const bool _ignoreAboveMax;
5008a8aa442SMatthew Barth 
5018a8aa442SMatthew Barth     /**
50269f2f48eSJolie Ku      * @brief The counter for count method
50369f2f48eSJolie Ku      */
50469f2f48eSJolie Ku     size_t _counter = 0;
50569f2f48eSJolie Ku 
50669f2f48eSJolie Ku     /**
507abf8da36SMatt Spinler      * @brief The input speed, from the Value dbus property
508abf8da36SMatt Spinler      */
5090891e3b3SMatthew Barth     double _tachInput = 0;
510abf8da36SMatt Spinler 
511abf8da36SMatt Spinler     /**
512abf8da36SMatt Spinler      * @brief The current target speed, from the Target dbus property
513abf8da36SMatt Spinler      *        (if applicable)
514abf8da36SMatt Spinler      */
515abf8da36SMatt Spinler     uint64_t _tachTarget = 0;
516abf8da36SMatt Spinler 
517abf8da36SMatt Spinler     /**
518abf8da36SMatt Spinler      * @brief The timeout value to use
519abf8da36SMatt Spinler      */
520abf8da36SMatt Spinler     const size_t _timeout;
521ebaae611SMatt Spinler 
522ebaae611SMatt Spinler     /**
5233800ae71SMatthew Barth      * @brief Mode that current timer is in
5243800ae71SMatthew Barth      */
5253800ae71SMatthew Barth     TimerMode _timerMode;
5263800ae71SMatthew Barth 
5273800ae71SMatthew Barth     /**
528a9406a77SMatt Spinler      * The timer object
529a9406a77SMatt Spinler      */
5308fd879fbSWilliam A. Kennington III     sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> _timer;
531a9406a77SMatt Spinler 
532a9406a77SMatt Spinler     /**
533ebaae611SMatt Spinler      * @brief The match object for the Value properties changed signal
534ebaae611SMatt Spinler      */
5353ea9ec2bSPatrick Williams     std::unique_ptr<sdbusplus::bus::match_t> tachSignal;
536ebaae611SMatt Spinler 
537ebaae611SMatt Spinler     /**
538ebaae611SMatt Spinler      * @brief The match object for the Target properties changed signal
539ebaae611SMatt Spinler      */
5403ea9ec2bSPatrick Williams     std::unique_ptr<sdbusplus::bus::match_t> targetSignal;
541f13b42e2SMatt Spinler 
542f13b42e2SMatt Spinler     /**
543f13b42e2SMatt Spinler      * @brief The number of seconds to wait between a sensor being set
544f13b42e2SMatt Spinler      *        to nonfunctional and creating an error for it.
545f13b42e2SMatt Spinler      *
546f13b42e2SMatt Spinler      * If std::nullopt, no errors will be created.
547f13b42e2SMatt Spinler      */
548f13b42e2SMatt Spinler     const std::optional<size_t> _errorDelay;
549f13b42e2SMatt Spinler 
550f13b42e2SMatt Spinler     /**
551f13b42e2SMatt Spinler      * @brief The timer that uses _errorDelay.  When it expires an error
552f13b42e2SMatt Spinler      *        will be created for a faulted fan sensor (rotor).
553f13b42e2SMatt Spinler      *
554f13b42e2SMatt Spinler      * If _errorDelay is std::nullopt, then this won't be created.
555f13b42e2SMatt Spinler      */
556f13b42e2SMatt Spinler     std::unique_ptr<
557f13b42e2SMatt Spinler         sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>
558f13b42e2SMatt Spinler         _errorTimer;
559fdfcc679SMatt Spinler 
560fdfcc679SMatt Spinler     /**
561fdfcc679SMatt Spinler      * @brief The interval, in seconds, to use for the timer that runs
562fdfcc679SMatt Spinler      *        the checks when using the 'count' method.
563fdfcc679SMatt Spinler      */
564fdfcc679SMatt Spinler     size_t _countInterval;
565fdfcc679SMatt Spinler 
566fdfcc679SMatt Spinler     /**
567fdfcc679SMatt Spinler      * @brief The timer used by the 'count' method for determining
568fdfcc679SMatt Spinler      *        functional status.
569fdfcc679SMatt Spinler      */
570fdfcc679SMatt Spinler     std::unique_ptr<
571fdfcc679SMatt Spinler         sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic>>
572fdfcc679SMatt Spinler         _countTimer;
5737b34ee0fSMike Capps 
5747b34ee0fSMike Capps     /**
5757b34ee0fSMike Capps      * @brief record of previous targets
5767b34ee0fSMike Capps      */
5777b34ee0fSMike Capps     std::deque<uint64_t> _prevTargets;
5787b34ee0fSMike Capps 
5797b34ee0fSMike Capps     /**
5807b34ee0fSMike Capps      * @brief record of previous tach readings
5817b34ee0fSMike Capps      */
5827b34ee0fSMike Capps     std::deque<uint64_t> _prevTachs;
583abf8da36SMatt Spinler };
584abf8da36SMatt Spinler 
585177fe986SMatthew Barth } // namespace monitor
586177fe986SMatthew Barth } // namespace fan
587177fe986SMatthew Barth } // namespace phosphor
588