xref: /openbmc/phosphor-pid-control/sensors/host.hpp (revision f8b6e55147148c3cfb42327ff267197a460b411c)
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);
49 
HostSensor(const std::string & name,int64_t timeout,sdbusplus::bus_t & bus,const char * objPath,bool defer)50     HostSensor(const std::string& name, int64_t timeout, sdbusplus::bus_t& bus,
51                const char* objPath, bool defer) :
52         Sensor(name, timeout),
53         ValueObject(bus, objPath,
54                     defer ? ValueObject::action::defer_emit
55                           : ValueObject::action::emit_object_added)
56     {}
57 
58     ValueType value(ValueType value) override;
59 
60     ReadReturn read(void) override;
61     void write(double value) override;
62     bool getFailed(void) override;
63 
64   private:
65     /*
66      * _lock will be used to make sure _updated & _value are updated
67      * together.
68      */
69     std::mutex _lock;
70     std::chrono::high_resolution_clock::time_point _updated;
71     double _value = 0;
72 };
73 
74 } // namespace pid_control
75