blob: 5086826554a291e285c3298f77d9fc076c34bada [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";
Alex.Song8f73ad72021-10-07 00:18:27 +080021 static constexpr char availabilityIntf[] =
22 "xyz.openbmc_project.State.Decorator.Availability";
Patrick Ventureaadb30d2020-08-10 09:17:11 -070023
Patrick Williams8c051122023-05-10 07:50:59 -050024 explicit DbusHelper(sdbusplus::bus_t bus) : _bus(std::move(bus)) {}
Patrick Ventureaadb30d2020-08-10 09:17:11 -070025 ~DbusHelper() = default;
Patrick Venture8729eb92020-08-10 10:38:44 -070026
27 DbusHelper(const DbusHelper&) = delete;
28 DbusHelper& operator=(const DbusHelper&) = delete;
29
Patrick Ventureaadb30d2020-08-10 09:17:11 -070030 DbusHelper(DbusHelper&&) = default;
31 DbusHelper& operator=(DbusHelper&&) = default;
32
Patrick Venture9b936922020-08-10 11:28:39 -070033 std::string getService(const std::string& intf,
Patrick Ventureaadb30d2020-08-10 09:17:11 -070034 const std::string& path) override;
35
Patrick Venture9b936922020-08-10 11:28:39 -070036 void getProperties(const std::string& service, const std::string& path,
Patrick Venture1df9e872020-10-08 15:35:01 -070037 SensorProperties* prop) override;
Patrick Ventureaadb30d2020-08-10 09:17:11 -070038
Patrick Venture9b936922020-08-10 11:28:39 -070039 bool thresholdsAsserted(const std::string& service,
Patrick Ventureaadb30d2020-08-10 09:17:11 -070040 const std::string& path) override;
41
42 template <typename T>
Patrick Venture9b936922020-08-10 11:28:39 -070043 void getProperty(const std::string& service, const std::string& path,
44 const std::string& interface,
Patrick Ventureaadb30d2020-08-10 09:17:11 -070045 const std::string& propertyName, T& prop)
46 {
47 namespace log = phosphor::logging;
48
Patrick Venture8729eb92020-08-10 10:38:44 -070049 auto msg = _bus.new_method_call(service.c_str(), path.c_str(),
50 propertiesintf, "Get");
Patrick Ventureaadb30d2020-08-10 09:17:11 -070051
52 msg.append(interface, propertyName);
53
54 std::variant<T> result;
55 try
56 {
Patrick Venture8729eb92020-08-10 10:38:44 -070057 auto valueResponseMsg = _bus.call(msg);
Patrick Ventureaadb30d2020-08-10 09:17:11 -070058 valueResponseMsg.read(result);
59 }
Patrick Williamsb228bc32022-07-22 19:26:56 -050060 catch (const sdbusplus::exception_t& ex)
Patrick Ventureaadb30d2020-08-10 09:17:11 -070061 {
62 log::log<log::level::ERR>("Get Property Failed",
63 log::entry("WHAT=%s", ex.what()));
64 throw;
65 }
66
67 prop = std::get<T>(result);
68 }
Patrick Venture8729eb92020-08-10 10:38:44 -070069
70 private:
Patrick Williamsb228bc32022-07-22 19:26:56 -050071 sdbusplus::bus_t _bus;
Patrick Ventureaadb30d2020-08-10 09:17:11 -070072};
73
74} // namespace pid_control