xref: /openbmc/phosphor-pid-control/sensors/host.hpp (revision e1fa85942c66533699a3b785990d95e9c89b6050)
1 #pragma once
2 
3 #include "interfaces.hpp"
4 #include "sensor.hpp"
5 
6 #include <sdbusplus/bus.hpp>
7 #include <sdbusplus/server/object.hpp>
8 #include <xyz/openbmc_project/Sensor/Value/server.hpp>
9 
10 #include <chrono>
11 #include <cstdint>
12 #include <memory>
13 #include <mutex>
14 #include <string>
15 #include <type_traits>
16 
17 template <typename... T>
18 using ServerObject = typename sdbusplus::server::object_t<T...>;
19 
20 using ValueInterface = sdbusplus::xyz::openbmc_project::Sensor::server::Value;
21 using ValueObject = ServerObject<ValueInterface>;
22 
23 namespace pid_control
24 {
25 
26 class ValueHelper : public ValueInterface
27 {
28   public:
operator ()() const29     auto operator()() const
30     {
31         return value();
32     }
33 };
34 
35 constexpr bool usingDouble =
36     std::is_same_v<std::result_of_t<ValueHelper()>, double>;
37 using ValueType = std::conditional_t<usingDouble, double, int64_t>;
38 
39 /*
40  * HostSensor object is a Sensor derivative that also implements a ValueObject,
41  * which comes from the dbus as an object that implements Sensor.Value.
42  */
43 class HostSensor : public Sensor, public ValueObject
44 {
45   public:
46     static std::unique_ptr<Sensor> createTemp(
47         const std::string& name, int64_t timeout, sdbusplus::bus_t& bus,
48         const char* objPath, bool defer, bool ignoreFailIfHostOff = false);
49 
HostSensor(const std::string & name,int64_t timeout,sdbusplus::bus_t & bus,const char * objPath,bool defer,bool ignoreFailIfHostOff=false)50     HostSensor(const std::string& name, int64_t timeout, sdbusplus::bus_t& bus,
51                const char* objPath, bool defer,
52                bool ignoreFailIfHostOff = false) :
53         Sensor(name, timeout, ignoreFailIfHostOff),
54         ValueObject(bus, objPath,
55                     defer ? ValueObject::action::defer_emit
56                           : ValueObject::action::emit_object_added)
57     {}
58 
59     ValueType value(ValueType value) override;
60 
61     ReadReturn read(void) override;
62     void write(double value) override;
63     bool getFailed(void) override;
64 
65   private:
66     /*
67      * _lock will be used to make sure _updated & _value are updated
68      * together.
69      */
70     std::mutex _lock;
71     std::chrono::high_resolution_clock::time_point _updated;
72     double _value = 0;
73 };
74 
75 } // namespace pid_control
76