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