blob: 59ccef28c000fb20638bf97093d92736a72f84c7 [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>
Patrick Ventureaadb30d2020-08-10 09:17:11 -07008
9#include <string>
10#include <variant>
11
12namespace pid_control
13{
14
15class DbusHelper : public DbusHelperInterface
16{
17 public:
18 static constexpr char sensorintf[] = "xyz.openbmc_project.Sensor.Value";
19 static constexpr char propertiesintf[] = "org.freedesktop.DBus.Properties";
20 static constexpr char criticalThreshInf[] =
21 "xyz.openbmc_project.Sensor.Threshold.Critical";
Jonico Eustaquioaf97d8e2024-01-02 14:35:07 -060022 static constexpr char warningThreshInf[] =
23 "xyz.openbmc_project.Sensor.Threshold.Warning";
Alex.Song8f73ad72021-10-07 00:18:27 +080024 static constexpr char availabilityIntf[] =
25 "xyz.openbmc_project.State.Decorator.Availability";
Patrick Ventureaadb30d2020-08-10 09:17:11 -070026
Patrick Williamscd1e78a2025-04-07 17:21:05 -040027 explicit DbusHelper(sdbusplus::bus_t& bus) : _bus(bus) {}
28 DbusHelper() = delete;
Ed Tanousd2768c52025-06-26 11:42:57 -070029 ~DbusHelper() override = default;
Patrick Venture8729eb92020-08-10 10:38:44 -070030
31 DbusHelper(const DbusHelper&) = delete;
32 DbusHelper& operator=(const DbusHelper&) = delete;
33
Patrick Ventureaadb30d2020-08-10 09:17:11 -070034 DbusHelper(DbusHelper&&) = default;
Eric Yang841531c2025-05-19 11:30:54 +080035 DbusHelper& operator=(DbusHelper&&) = delete;
Patrick Ventureaadb30d2020-08-10 09:17:11 -070036
Patrick Venture9b936922020-08-10 11:28:39 -070037 std::string getService(const std::string& intf,
Patrick Ventureaadb30d2020-08-10 09:17:11 -070038 const std::string& path) override;
39
Patrick Venture9b936922020-08-10 11:28:39 -070040 void getProperties(const std::string& service, const std::string& path,
Patrick Venture1df9e872020-10-08 15:35:01 -070041 SensorProperties* prop) override;
Patrick Ventureaadb30d2020-08-10 09:17:11 -070042
Patrick Venture9b936922020-08-10 11:28:39 -070043 bool thresholdsAsserted(const std::string& service,
Patrick Ventureaadb30d2020-08-10 09:17:11 -070044 const std::string& path) override;
45
46 template <typename T>
Patrick Venture9b936922020-08-10 11:28:39 -070047 void getProperty(const std::string& service, const std::string& path,
48 const std::string& interface,
Patrick Ventureaadb30d2020-08-10 09:17:11 -070049 const std::string& propertyName, T& prop)
50 {
51 namespace log = phosphor::logging;
52
Patrick Venture8729eb92020-08-10 10:38:44 -070053 auto msg = _bus.new_method_call(service.c_str(), path.c_str(),
54 propertiesintf, "Get");
Patrick Ventureaadb30d2020-08-10 09:17:11 -070055
56 msg.append(interface, propertyName);
57
58 std::variant<T> result;
59 try
60 {
Patrick Venture8729eb92020-08-10 10:38:44 -070061 auto valueResponseMsg = _bus.call(msg);
Patrick Ventureaadb30d2020-08-10 09:17:11 -070062 valueResponseMsg.read(result);
63 }
Patrick Williamsb228bc32022-07-22 19:26:56 -050064 catch (const sdbusplus::exception_t& ex)
Patrick Ventureaadb30d2020-08-10 09:17:11 -070065 {
66 log::log<log::level::ERR>("Get Property Failed",
67 log::entry("WHAT=%s", ex.what()));
68 throw;
69 }
70
71 prop = std::get<T>(result);
72 }
Patrick Venture8729eb92020-08-10 10:38:44 -070073
74 private:
Patrick Williamscd1e78a2025-04-07 17:21:05 -040075 sdbusplus::bus_t& _bus;
Patrick Ventureaadb30d2020-08-10 09:17:11 -070076};
77
78} // namespace pid_control