blob: 9b731947aeb244df09625d6fed63b59668da6541 [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>
7
8#include <string>
9#include <variant>
10
11namespace pid_control
12{
13
14class DbusHelper : public DbusHelperInterface
15{
16 public:
17 static constexpr char sensorintf[] = "xyz.openbmc_project.Sensor.Value";
18 static constexpr char propertiesintf[] = "org.freedesktop.DBus.Properties";
19 static constexpr char criticalThreshInf[] =
20 "xyz.openbmc_project.Sensor.Threshold.Critical";
Jonico Eustaquioaf97d8e2024-01-02 14:35:07 -060021 static constexpr char warningThreshInf[] =
22 "xyz.openbmc_project.Sensor.Threshold.Warning";
Alex.Song8f73ad72021-10-07 00:18:27 +080023 static constexpr char availabilityIntf[] =
24 "xyz.openbmc_project.State.Decorator.Availability";
Patrick Ventureaadb30d2020-08-10 09:17:11 -070025
Patrick Williamscd1e78a2025-04-07 17:21:05 -040026 explicit DbusHelper(sdbusplus::bus_t& bus) : _bus(bus) {}
27 DbusHelper() = delete;
Patrick Ventureaadb30d2020-08-10 09:17:11 -070028 ~DbusHelper() = default;
Patrick Venture8729eb92020-08-10 10:38:44 -070029
30 DbusHelper(const DbusHelper&) = delete;
31 DbusHelper& operator=(const DbusHelper&) = delete;
32
Patrick Ventureaadb30d2020-08-10 09:17:11 -070033 DbusHelper(DbusHelper&&) = default;
34 DbusHelper& operator=(DbusHelper&&) = default;
35
Patrick Venture9b936922020-08-10 11:28:39 -070036 std::string getService(const std::string& intf,
Patrick Ventureaadb30d2020-08-10 09:17:11 -070037 const std::string& path) override;
38
Patrick Venture9b936922020-08-10 11:28:39 -070039 void getProperties(const std::string& service, const std::string& path,
Patrick Venture1df9e872020-10-08 15:35:01 -070040 SensorProperties* prop) override;
Patrick Ventureaadb30d2020-08-10 09:17:11 -070041
Patrick Venture9b936922020-08-10 11:28:39 -070042 bool thresholdsAsserted(const std::string& service,
Patrick Ventureaadb30d2020-08-10 09:17:11 -070043 const std::string& path) override;
44
45 template <typename T>
Patrick Venture9b936922020-08-10 11:28:39 -070046 void getProperty(const std::string& service, const std::string& path,
47 const std::string& interface,
Patrick Ventureaadb30d2020-08-10 09:17:11 -070048 const std::string& propertyName, T& prop)
49 {
50 namespace log = phosphor::logging;
51
Patrick Venture8729eb92020-08-10 10:38:44 -070052 auto msg = _bus.new_method_call(service.c_str(), path.c_str(),
53 propertiesintf, "Get");
Patrick Ventureaadb30d2020-08-10 09:17:11 -070054
55 msg.append(interface, propertyName);
56
57 std::variant<T> result;
58 try
59 {
Patrick Venture8729eb92020-08-10 10:38:44 -070060 auto valueResponseMsg = _bus.call(msg);
Patrick Ventureaadb30d2020-08-10 09:17:11 -070061 valueResponseMsg.read(result);
62 }
Patrick Williamsb228bc32022-07-22 19:26:56 -050063 catch (const sdbusplus::exception_t& ex)
Patrick Ventureaadb30d2020-08-10 09:17:11 -070064 {
65 log::log<log::level::ERR>("Get Property Failed",
66 log::entry("WHAT=%s", ex.what()));
67 throw;
68 }
69
70 prop = std::get<T>(result);
71 }
Patrick Venture8729eb92020-08-10 10:38:44 -070072
73 private:
Patrick Williamscd1e78a2025-04-07 17:21:05 -040074 sdbusplus::bus_t& _bus;
Patrick Ventureaadb30d2020-08-10 09:17:11 -070075};
76
77} // namespace pid_control