blob: 7983e8ac793080c52331fa2b32c84810fd3d5df6 [file] [log] [blame]
Patrick Venture863b9242018-03-08 08:29:23 -08001#pragma once
2
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07003#include "sensor.hpp"
Patrick Venture863b9242018-03-08 08:29:23 -08004
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07005#include <sdbusplus/bus.hpp>
6#include <sdbusplus/server.hpp>
Patrick Venture1248b152018-10-30 19:09:54 -07007#include <xyz/openbmc_project/Sensor/Value/server.hpp>
Patrick Venture863b9242018-03-08 08:29:23 -08008
Patrick Venturea83a3ec2020-08-04 09:52:05 -07009#include <memory>
10#include <mutex>
11#include <type_traits>
12
Patrick Venture863b9242018-03-08 08:29:23 -080013template <typename... T>
14using ServerObject = typename sdbusplus::server::object::object<T...>;
15
16using ValueInterface = sdbusplus::xyz::openbmc_project::Sensor::server::Value;
17using ValueObject = ServerObject<ValueInterface>;
18
James Feist8154e322020-06-22 17:12:18 -070019class ValueHelper : public ValueInterface
20{
21
22 public:
23 auto operator()() const
24 {
25 return value();
26 }
27};
28
29constexpr bool usingDouble =
30 std::is_same_v<std::result_of_t<ValueHelper()>, double>;
31using ValueType = std::conditional_t<usingDouble, double, int64_t>;
32
Patrick Venture863b9242018-03-08 08:29:23 -080033/*
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 */
37class HostSensor : public Sensor, public ValueObject
38{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070039 public:
Patrick Venture563a3562018-10-30 09:31:26 -070040 static std::unique_ptr<Sensor> createTemp(const std::string& name,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070041 int64_t timeout,
42 sdbusplus::bus::bus& bus,
43 const char* objPath, bool defer);
Patrick Venture863b9242018-03-08 08:29:23 -080044
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070045 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)
Patrick Venturea83a3ec2020-08-04 09:52:05 -070049 {}
Patrick Venture863b9242018-03-08 08:29:23 -080050
James Feist8154e322020-06-22 17:12:18 -070051 ValueType value(ValueType value) override;
Patrick Venture863b9242018-03-08 08:29:23 -080052
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070053 ReadReturn read(void) override;
54 void write(double value) override;
Patrick Venture863b9242018-03-08 08:29:23 -080055
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070056 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;
Patrick Venture863b9242018-03-08 08:29:23 -080064};