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