blob: 94cf6649603ea2f3dcb33bf88634c9dbc1a3b765 [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>
Patrick Ventureaadb30d2020-08-10 09:17:11 -070010
11#include <string>
12#include <variant>
13
Alexander Hansend3e71ce2025-11-12 16:20:53 +010014using SensorThresholdWarning =
15 sdbusplus::common::xyz::openbmc_project::sensor::threshold::Warning;
16using SensorThresholdCritical =
17 sdbusplus::common::xyz::openbmc_project::sensor::threshold::Critical;
18
Patrick Ventureaadb30d2020-08-10 09:17:11 -070019namespace pid_control
20{
21
22class DbusHelper : public DbusHelperInterface
23{
24 public:
Patrick Ventureaadb30d2020-08-10 09:17:11 -070025 static constexpr char propertiesintf[] = "org.freedesktop.DBus.Properties";
Alex.Song8f73ad72021-10-07 00:18:27 +080026 static constexpr char availabilityIntf[] =
27 "xyz.openbmc_project.State.Decorator.Availability";
Patrick Ventureaadb30d2020-08-10 09:17:11 -070028
Patrick Williamscd1e78a2025-04-07 17:21:05 -040029 explicit DbusHelper(sdbusplus::bus_t& bus) : _bus(bus) {}
30 DbusHelper() = delete;
Ed Tanousd2768c52025-06-26 11:42:57 -070031 ~DbusHelper() override = default;
Patrick Venture8729eb92020-08-10 10:38:44 -070032
33 DbusHelper(const DbusHelper&) = delete;
34 DbusHelper& operator=(const DbusHelper&) = delete;
35
Patrick Ventureaadb30d2020-08-10 09:17:11 -070036 DbusHelper(DbusHelper&&) = default;
Eric Yang841531c2025-05-19 11:30:54 +080037 DbusHelper& operator=(DbusHelper&&) = delete;
Patrick Ventureaadb30d2020-08-10 09:17:11 -070038
Patrick Venture9b936922020-08-10 11:28:39 -070039 std::string getService(const std::string& intf,
Patrick Ventureaadb30d2020-08-10 09:17:11 -070040 const std::string& path) override;
41
Patrick Venture9b936922020-08-10 11:28:39 -070042 void getProperties(const std::string& service, const std::string& path,
Patrick Venture1df9e872020-10-08 15:35:01 -070043 SensorProperties* prop) override;
Patrick Ventureaadb30d2020-08-10 09:17:11 -070044
Patrick Venture9b936922020-08-10 11:28:39 -070045 bool thresholdsAsserted(const std::string& service,
Patrick Ventureaadb30d2020-08-10 09:17:11 -070046 const std::string& path) override;
47
48 template <typename T>
Patrick Venture9b936922020-08-10 11:28:39 -070049 void getProperty(const std::string& service, const std::string& path,
50 const std::string& interface,
Patrick Ventureaadb30d2020-08-10 09:17:11 -070051 const std::string& propertyName, T& prop)
52 {
53 namespace log = phosphor::logging;
54
Patrick Venture8729eb92020-08-10 10:38:44 -070055 auto msg = _bus.new_method_call(service.c_str(), path.c_str(),
56 propertiesintf, "Get");
Patrick Ventureaadb30d2020-08-10 09:17:11 -070057
58 msg.append(interface, propertyName);
59
60 std::variant<T> result;
61 try
62 {
Patrick Venture8729eb92020-08-10 10:38:44 -070063 auto valueResponseMsg = _bus.call(msg);
Patrick Ventureaadb30d2020-08-10 09:17:11 -070064 valueResponseMsg.read(result);
65 }
Patrick Williamsb228bc32022-07-22 19:26:56 -050066 catch (const sdbusplus::exception_t& ex)
Patrick Ventureaadb30d2020-08-10 09:17:11 -070067 {
68 log::log<log::level::ERR>("Get Property Failed",
69 log::entry("WHAT=%s", ex.what()));
70 throw;
71 }
72
73 prop = std::get<T>(result);
74 }
Patrick Venture8729eb92020-08-10 10:38:44 -070075
76 private:
Patrick Williamscd1e78a2025-04-07 17:21:05 -040077 sdbusplus::bus_t& _bus;
Patrick Ventureaadb30d2020-08-10 09:17:11 -070078};
79
80} // namespace pid_control