blob: 8fac1a2ac9e6ddccaafe257e6f4bd8aace2bc044 [file] [log] [blame]
Patrick Ventureaadb30d2020-08-10 09:17:11 -07001#pragma once
2
3#include "dbushelper_interface.hpp"
4
5#include <phosphor-logging/log.hpp>
6#include <sdbusplus/bus.hpp>
Ed Tanousf8b6e552025-06-27 13:27:50 -07007#include <sdbusplus/exception.hpp>
Alexander Hansend3e71ce2025-11-12 16:20:53 +01008#include <xyz/openbmc_project/Sensor/Threshold/Critical/common.hpp>
9#include <xyz/openbmc_project/Sensor/Threshold/Warning/common.hpp>
Alexander Hansen11a1edc2025-11-12 17:31:23 +010010#include <xyz/openbmc_project/State/Decorator/Availability/common.hpp>
Patrick Ventureaadb30d2020-08-10 09:17:11 -070011
12#include <string>
13#include <variant>
14
Alexander Hansend3e71ce2025-11-12 16:20:53 +010015using SensorThresholdWarning =
16 sdbusplus::common::xyz::openbmc_project::sensor::threshold::Warning;
17using SensorThresholdCritical =
18 sdbusplus::common::xyz::openbmc_project::sensor::threshold::Critical;
Alexander Hansen11a1edc2025-11-12 17:31:23 +010019using StateDecoratorAvailability =
20 sdbusplus::common::xyz::openbmc_project::state::decorator::Availability;
Alexander Hansend3e71ce2025-11-12 16:20:53 +010021
Patrick Ventureaadb30d2020-08-10 09:17:11 -070022namespace pid_control
23{
24
25class DbusHelper : public DbusHelperInterface
26{
27 public:
Patrick Ventureaadb30d2020-08-10 09:17:11 -070028 static constexpr char propertiesintf[] = "org.freedesktop.DBus.Properties";
Patrick Ventureaadb30d2020-08-10 09:17:11 -070029
Patrick Williamscd1e78a2025-04-07 17:21:05 -040030 explicit DbusHelper(sdbusplus::bus_t& bus) : _bus(bus) {}
31 DbusHelper() = delete;
Ed Tanousd2768c52025-06-26 11:42:57 -070032 ~DbusHelper() override = default;
Patrick Venture8729eb92020-08-10 10:38:44 -070033
34 DbusHelper(const DbusHelper&) = delete;
35 DbusHelper& operator=(const DbusHelper&) = delete;
36
Patrick Ventureaadb30d2020-08-10 09:17:11 -070037 DbusHelper(DbusHelper&&) = default;
Eric Yang841531c2025-05-19 11:30:54 +080038 DbusHelper& operator=(DbusHelper&&) = delete;
Patrick Ventureaadb30d2020-08-10 09:17:11 -070039
Patrick Venture9b936922020-08-10 11:28:39 -070040 std::string getService(const std::string& intf,
Patrick Ventureaadb30d2020-08-10 09:17:11 -070041 const std::string& path) override;
42
Patrick Venture9b936922020-08-10 11:28:39 -070043 void getProperties(const std::string& service, const std::string& path,
Patrick Venture1df9e872020-10-08 15:35:01 -070044 SensorProperties* prop) override;
Patrick Ventureaadb30d2020-08-10 09:17:11 -070045
Patrick Venture9b936922020-08-10 11:28:39 -070046 bool thresholdsAsserted(const std::string& service,
Patrick Ventureaadb30d2020-08-10 09:17:11 -070047 const std::string& path) override;
48
49 template <typename T>
Patrick Venture9b936922020-08-10 11:28:39 -070050 void getProperty(const std::string& service, const std::string& path,
51 const std::string& interface,
Patrick Ventureaadb30d2020-08-10 09:17:11 -070052 const std::string& propertyName, T& prop)
53 {
54 namespace log = phosphor::logging;
55
Patrick Venture8729eb92020-08-10 10:38:44 -070056 auto msg = _bus.new_method_call(service.c_str(), path.c_str(),
57 propertiesintf, "Get");
Patrick Ventureaadb30d2020-08-10 09:17:11 -070058
59 msg.append(interface, propertyName);
60
61 std::variant<T> result;
62 try
63 {
Patrick Venture8729eb92020-08-10 10:38:44 -070064 auto valueResponseMsg = _bus.call(msg);
Patrick Ventureaadb30d2020-08-10 09:17:11 -070065 valueResponseMsg.read(result);
66 }
Patrick Williamsb228bc32022-07-22 19:26:56 -050067 catch (const sdbusplus::exception_t& ex)
Patrick Ventureaadb30d2020-08-10 09:17:11 -070068 {
69 log::log<log::level::ERR>("Get Property Failed",
70 log::entry("WHAT=%s", ex.what()));
71 throw;
72 }
73
74 prop = std::get<T>(result);
75 }
Patrick Venture8729eb92020-08-10 10:38:44 -070076
77 private:
Patrick Williamscd1e78a2025-04-07 17:21:05 -040078 sdbusplus::bus_t& _bus;
Patrick Ventureaadb30d2020-08-10 09:17:11 -070079};
80
81} // namespace pid_control