blob: d3f97ed5ad0ff4373218f94f1d779db003b917a2 [file] [log] [blame]
Patrick Venture863b9242018-03-08 08:29:23 -08001#pragma once
2
Ed Tanousf8b6e552025-06-27 13:27:50 -07003#include "interfaces.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07004#include "sensor.hpp"
Patrick Venture863b9242018-03-08 08:29:23 -08005
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07006#include <sdbusplus/bus.hpp>
Ed Tanousf8b6e552025-06-27 13:27:50 -07007#include <sdbusplus/server/object.hpp>
Patrick Venture1248b152018-10-30 19:09:54 -07008#include <xyz/openbmc_project/Sensor/Value/server.hpp>
Patrick Venture863b9242018-03-08 08:29:23 -08009
Ed Tanousf8b6e552025-06-27 13:27:50 -070010#include <chrono>
11#include <cstdint>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070012#include <memory>
13#include <mutex>
Ed Tanousf8b6e552025-06-27 13:27:50 -070014#include <string>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070015#include <type_traits>
16
Patrick Venture863b9242018-03-08 08:29:23 -080017template <typename... T>
Patrick Williamsb228bc32022-07-22 19:26:56 -050018using ServerObject = typename sdbusplus::server::object_t<T...>;
Patrick Venture863b9242018-03-08 08:29:23 -080019
20using ValueInterface = sdbusplus::xyz::openbmc_project::Sensor::server::Value;
21using ValueObject = ServerObject<ValueInterface>;
22
Patrick Venturea0764872020-08-08 07:48:43 -070023namespace pid_control
24{
25
James Feist8154e322020-06-22 17:12:18 -070026class ValueHelper : public ValueInterface
27{
James Feist8154e322020-06-22 17:12:18 -070028 public:
29 auto operator()() const
30 {
31 return value();
32 }
33};
34
35constexpr bool usingDouble =
36 std::is_same_v<std::result_of_t<ValueHelper()>, double>;
37using ValueType = std::conditional_t<usingDouble, double, int64_t>;
38
Patrick Venture863b9242018-03-08 08:29:23 -080039/*
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 */
43class HostSensor : public Sensor, public ValueObject
44{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070045 public:
Patrick Williams19300272025-02-01 08:22:48 -050046 static std::unique_ptr<Sensor> createTemp(
47 const std::string& name, int64_t timeout, sdbusplus::bus_t& bus,
48 const char* objPath, bool defer);
Patrick Venture863b9242018-03-08 08:29:23 -080049
Patrick Williamsb228bc32022-07-22 19:26:56 -050050 HostSensor(const std::string& name, int64_t timeout, sdbusplus::bus_t& bus,
51 const char* objPath, bool defer) :
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070052 Sensor(name, timeout),
Patrick Williamsd8c5a452022-04-07 16:08:56 -050053 ValueObject(bus, objPath,
54 defer ? ValueObject::action::defer_emit
55 : ValueObject::action::emit_object_added)
Patrick Venturea83a3ec2020-08-04 09:52:05 -070056 {}
Patrick Venture863b9242018-03-08 08:29:23 -080057
James Feist8154e322020-06-22 17:12:18 -070058 ValueType value(ValueType value) override;
Patrick Venture863b9242018-03-08 08:29:23 -080059
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070060 ReadReturn read(void) override;
61 void write(double value) override;
Harvey Wu17971ef2021-02-17 14:36:38 +080062 bool getFailed(void) override;
Patrick Venture863b9242018-03-08 08:29:23 -080063
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070064 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;
Patrick Venture863b9242018-03-08 08:29:23 -080072};
Patrick Venturea0764872020-08-08 07:48:43 -070073
74} // namespace pid_control