xref: /openbmc/phosphor-pid-control/sensors/sensor.hpp (revision e1fa85942c66533699a3b785990d95e9c89b6050)
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      */
getDefaultTimeout(const std::string & type)26     static int64_t getDefaultTimeout(const std::string& type)
27     {
28         return (type == "fan") ? 0 : 2;
29     }
30 
Sensor(const std::string & name,int64_t timeout,bool ignoreFailIfHostOff=false)31     Sensor(const std::string& name, int64_t timeout,
32            bool ignoreFailIfHostOff = false) :
33         _name(name), _timeout(timeout),
34         _ignoreFailIfHostOff(ignoreFailIfHostOff)
35     {}
36 
37     virtual ~Sensor() = default;
38 
39     virtual ReadReturn read(void) = 0;
40     virtual void write(double value) = 0;
41 
write(double value,bool force,int64_t * written)42     virtual void write(double value, bool force, int64_t* written)
43     {
44         (void)force;
45         (void)written;
46         return write(value);
47     }
48 
getFailed(void)49     virtual bool getFailed(void)
50     {
51         return false;
52     };
53 
getFailReason(void)54     virtual std::string getFailReason(void)
55     {
56         return "Unimplemented";
57     }
58 
getName(void) const59     std::string getName(void) const
60     {
61         return _name;
62     }
63 
64     /* Returns the configurable timeout period
65      * for this sensor in seconds (undecorated).
66      */
getTimeout(void) const67     int64_t getTimeout(void) const
68     {
69         return _timeout;
70     }
71 
getIgnoreFailIfHostOff(void) const72     bool getIgnoreFailIfHostOff(void) const
73     {
74         return _ignoreFailIfHostOff;
75     }
76 
77   private:
78     std::string _name;
79     int64_t _timeout;
80     bool _ignoreFailIfHostOff;
81 };
82 
83 } // namespace pid_control
84