blob: 83b988920834db9d7c0b85c2585b4d193e275de7 [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>
Patrick Williamsb228bc32022-07-22 19:26:56 -050014using ServerObject = typename sdbusplus::server::object_t<T...>;
Patrick Venture863b9242018-03-08 08:29:23 -080015
16using ValueInterface = sdbusplus::xyz::openbmc_project::Sensor::server::Value;
17using ValueObject = ServerObject<ValueInterface>;
18
Patrick Venturea0764872020-08-08 07:48:43 -070019namespace pid_control
20{
21
James Feist8154e322020-06-22 17:12:18 -070022class ValueHelper : public ValueInterface
23{
James Feist8154e322020-06-22 17:12:18 -070024 public:
25 auto operator()() const
26 {
27 return value();
28 }
29};
30
31constexpr bool usingDouble =
32 std::is_same_v<std::result_of_t<ValueHelper()>, double>;
33using ValueType = std::conditional_t<usingDouble, double, int64_t>;
34
Patrick Venture863b9242018-03-08 08:29:23 -080035/*
36 * HostSensor object is a Sensor derivative that also implements a ValueObject,
37 * which comes from the dbus as an object that implements Sensor.Value.
38 */
39class HostSensor : public Sensor, public ValueObject
40{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070041 public:
Patrick Venture563a3562018-10-30 09:31:26 -070042 static std::unique_ptr<Sensor> createTemp(const std::string& name,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070043 int64_t timeout,
Patrick Williamsb228bc32022-07-22 19:26:56 -050044 sdbusplus::bus_t& bus,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070045 const char* objPath, bool defer);
Patrick Venture863b9242018-03-08 08:29:23 -080046
Patrick Williamsb228bc32022-07-22 19:26:56 -050047 HostSensor(const std::string& name, int64_t timeout, sdbusplus::bus_t& bus,
48 const char* objPath, bool defer) :
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070049 Sensor(name, timeout),
Patrick Williamsd8c5a452022-04-07 16:08:56 -050050 ValueObject(bus, objPath,
51 defer ? ValueObject::action::defer_emit
52 : ValueObject::action::emit_object_added)
Patrick Venturea83a3ec2020-08-04 09:52:05 -070053 {}
Patrick Venture863b9242018-03-08 08:29:23 -080054
James Feist8154e322020-06-22 17:12:18 -070055 ValueType value(ValueType value) override;
Patrick Venture863b9242018-03-08 08:29:23 -080056
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070057 ReadReturn read(void) override;
58 void write(double value) override;
Harvey Wu17971ef2021-02-17 14:36:38 +080059 bool getFailed(void) override;
Patrick Venture863b9242018-03-08 08:29:23 -080060
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070061 private:
62 /*
63 * _lock will be used to make sure _updated & _value are updated
64 * together.
65 */
66 std::mutex _lock;
67 std::chrono::high_resolution_clock::time_point _updated;
68 double _value = 0;
Patrick Venture863b9242018-03-08 08:29:23 -080069};
Patrick Venturea0764872020-08-08 07:48:43 -070070
71} // namespace pid_control