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 class ValueHelper : public ValueInterface 20 { 21 22 public: 23 auto operator()() const 24 { 25 return value(); 26 } 27 }; 28 29 constexpr bool usingDouble = 30 std::is_same_v<std::result_of_t<ValueHelper()>, double>; 31 using ValueType = std::conditional_t<usingDouble, double, int64_t>; 32 33 /* 34 * HostSensor object is a Sensor derivative that also implements a ValueObject, 35 * which comes from the dbus as an object that implements Sensor.Value. 36 */ 37 class HostSensor : public Sensor, public ValueObject 38 { 39 public: 40 static std::unique_ptr<Sensor> createTemp(const std::string& name, 41 int64_t timeout, 42 sdbusplus::bus::bus& bus, 43 const char* objPath, bool defer); 44 45 HostSensor(const std::string& name, int64_t timeout, 46 sdbusplus::bus::bus& bus, const char* objPath, bool defer) : 47 Sensor(name, timeout), 48 ValueObject(bus, objPath, defer) 49 {} 50 51 ValueType value(ValueType value) override; 52 53 ReadReturn read(void) override; 54 void write(double value) override; 55 56 private: 57 /* 58 * _lock will be used to make sure _updated & _value are updated 59 * together. 60 */ 61 std::mutex _lock; 62 std::chrono::high_resolution_clock::time_point _updated; 63 double _value = 0; 64 }; 65