blob: f65784cc8bdfa50fbad1cee30926708fb44922a9 [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 Williamsb228bc32022-07-22 19:26:56 -050024 explicit DbusHelper(sdbusplus::bus_t bus) : _bus(std::move(bus))
Patrick Venture8729eb92020-08-10 10:38:44 -070025 {}
Patrick Ventureaadb30d2020-08-10 09:17:11 -070026 ~DbusHelper() = default;
Patrick Venture8729eb92020-08-10 10:38:44 -070027
28 DbusHelper(const DbusHelper&) = delete;
29 DbusHelper& operator=(const DbusHelper&) = delete;
30
Patrick Ventureaadb30d2020-08-10 09:17:11 -070031 DbusHelper(DbusHelper&&) = default;
32 DbusHelper& operator=(DbusHelper&&) = default;
33
Patrick Venture9b936922020-08-10 11:28:39 -070034 std::string getService(const std::string& intf,
Patrick Ventureaadb30d2020-08-10 09:17:11 -070035 const std::string& path) override;
36
Patrick Venture9b936922020-08-10 11:28:39 -070037 void getProperties(const std::string& service, const std::string& path,
Patrick Venture1df9e872020-10-08 15:35:01 -070038 SensorProperties* prop) override;
Patrick Ventureaadb30d2020-08-10 09:17:11 -070039
Patrick Venture9b936922020-08-10 11:28:39 -070040 bool thresholdsAsserted(const std::string& service,
Patrick Ventureaadb30d2020-08-10 09:17:11 -070041 const std::string& path) override;
42
43 template <typename T>
Patrick Venture9b936922020-08-10 11:28:39 -070044 void getProperty(const std::string& service, const std::string& path,
45 const std::string& interface,
Patrick Ventureaadb30d2020-08-10 09:17:11 -070046 const std::string& propertyName, T& prop)
47 {
48 namespace log = phosphor::logging;
49
Patrick Venture8729eb92020-08-10 10:38:44 -070050 auto msg = _bus.new_method_call(service.c_str(), path.c_str(),
51 propertiesintf, "Get");
Patrick Ventureaadb30d2020-08-10 09:17:11 -070052
53 msg.append(interface, propertyName);
54
55 std::variant<T> result;
56 try
57 {
Patrick Venture8729eb92020-08-10 10:38:44 -070058 auto valueResponseMsg = _bus.call(msg);
Patrick Ventureaadb30d2020-08-10 09:17:11 -070059 valueResponseMsg.read(result);
60 }
Patrick Williamsb228bc32022-07-22 19:26:56 -050061 catch (const sdbusplus::exception_t& ex)
Patrick Ventureaadb30d2020-08-10 09:17:11 -070062 {
63 log::log<log::level::ERR>("Get Property Failed",
64 log::entry("WHAT=%s", ex.what()));
65 throw;
66 }
67
68 prop = std::get<T>(result);
69 }
Patrick Venture8729eb92020-08-10 10:38:44 -070070
71 private:
Patrick Williamsb228bc32022-07-22 19:26:56 -050072 sdbusplus::bus_t _bus;
Patrick Ventureaadb30d2020-08-10 09:17:11 -070073};
74
75} // namespace pid_control