xref: /openbmc/phosphor-pid-control/sensors/sensor.hpp (revision f8b6e55147148c3cfb42327ff267197a460b411c)
1 #pragma once
2 
3 #include "interfaces.hpp"
4 
5 #include <cstdint>
6 #include <string>
7 
8 namespace pid_control
9 {
10 
11 /**
12  * Abstract base class for all sensors.
13  */
14 class Sensor
15 {
16   public:
17     /**
18      * Given a sensor's type, return the default timeout value.
19      * A timeout of 0 means there isn't a timeout for this sensor.
20      * By default a fan sensor isn't checked for a timeout, whereas
21      * any of sensor is meant to be sampled once per second.  By default.
22      *
23      * @param[in] type - the sensor type (e.g. fan)
24      * @return the default timeout for that type (in seconds).
25      */
26     static int64_t getDefaultTimeout(const std::string& type)
27     {
28         return (type == "fan") ? 0 : 2;
29     }
30 
31     Sensor(const std::string& name, int64_t timeout) :
32         _name(name), _timeout(timeout)
33     {}
34 
35     virtual ~Sensor() = default;
36 
37     virtual ReadReturn read(void) = 0;
38     virtual void write(double value) = 0;
39 
40     virtual void write(double value, bool force, int64_t* written)
41     {
42         (void)force;
43         (void)written;
44         return write(value);
45     }
46 
47     virtual bool getFailed(void)
48     {
49         return false;
50     };
51 
52     virtual std::string getFailReason(void)
53     {
54         return "Unimplemented";
55     }
56 
57     std::string getName(void) const
58     {
59         return _name;
60     }
61 
62     /* Returns the configurable timeout period
63      * for this sensor in seconds (undecorated).
64      */
65     int64_t getTimeout(void) const
66     {
67         return _timeout;
68     }
69 
70   private:
71     std::string _name;
72     int64_t _timeout;
73 };
74 
75 } // namespace pid_control
76