xref: /openbmc/phosphor-pid-control/interfaces.hpp (revision f8b6e55147148c3cfb42327ff267197a460b411c)
1 #pragma once
2 
3 #include <chrono>
4 #include <cstdint>
5 #include <limits>
6 #include <string>
7 
8 namespace pid_control
9 {
10 
11 struct ReadReturn
12 {
13     double value = std::numeric_limits<double>::quiet_NaN();
14     std::chrono::high_resolution_clock::time_point updated;
15     double unscaled = value;
16 
operator ==pid_control::ReadReturn17     bool operator==(const ReadReturn& rhs) const
18     {
19         return ((this->value == rhs.value) && (this->updated == rhs.updated) &&
20                 (this->unscaled == rhs.unscaled));
21     }
22 };
23 
24 struct ValueCacheEntry
25 {
26     // This is normalized to (0.0, 1.0) range, using configured min and max
27     double scaled;
28 
29     // This is the raw value, as received from the input/output sensors
30     double unscaled;
31 };
32 
33 /*
34  * A ReadInterface is a plug-in for the PluggableSensor and anyone implementing
35  * this basically is providing a way to read a sensor.
36  */
37 class ReadInterface
38 {
39   public:
40     ReadInterface() = default;
41 
42     virtual ~ReadInterface() = default;
43 
44     virtual ReadReturn read(void) = 0;
45 
getFailed(void) const46     virtual bool getFailed(void) const
47     {
48         return false;
49     }
50 
getFailReason(void) const51     virtual std::string getFailReason(void) const
52     {
53         return "Unimplemented";
54     }
55 };
56 
57 /*
58  * A WriteInterface is a plug-in for the PluggableSensor and anyone implementing
59  * this basically is providing a way to write a sensor.
60  */
61 class WriteInterface
62 {
63   public:
WriteInterface(int64_t min,int64_t max)64     WriteInterface(int64_t min, int64_t max) : _min(min), _max(max) {}
65 
66     virtual ~WriteInterface() = default;
67 
68     virtual void write(double value) = 0;
69 
70     /*
71      * A wrapper around write(), with additional parameters.
72      * force = true to perform redundant write, even if raw value unchanged.
73      * written = non-null to be filled in with the actual raw value written.
74      */
write(double value,bool force,int64_t * written)75     virtual void write(double value, bool force, int64_t* written)
76     {
77         (void)force;
78         (void)written;
79         return write(value);
80     }
81 
82     /*
83      * All WriteInterfaces have min/max available in case they want to error
84      * check.
85      */
getMin(void)86     int64_t getMin(void)
87     {
88         return _min;
89     }
getMax(void)90     int64_t getMax(void)
91     {
92         return _max;
93     }
94 
95   private:
96     int64_t _min;
97     int64_t _max;
98 };
99 
100 } // namespace pid_control
101