blob: 0831bd422236915670cdab5f1d05fa25fb16ba5f [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";
21
Patrick Venture8729eb92020-08-10 10:38:44 -070022 explicit DbusHelper(sdbusplus::bus::bus bus) : _bus(std::move(bus))
23 {}
Patrick Ventureaadb30d2020-08-10 09:17:11 -070024 ~DbusHelper() = default;
Patrick Venture8729eb92020-08-10 10:38:44 -070025
26 DbusHelper(const DbusHelper&) = delete;
27 DbusHelper& operator=(const DbusHelper&) = delete;
28
Patrick Ventureaadb30d2020-08-10 09:17:11 -070029 DbusHelper(DbusHelper&&) = default;
30 DbusHelper& operator=(DbusHelper&&) = default;
31
32 std::string getService(sdbusplus::bus::bus& bus, const std::string& intf,
33 const std::string& path) override;
34
35 void getProperties(sdbusplus::bus::bus& bus, const std::string& service,
36 const std::string& path,
37 struct SensorProperties* prop) override;
38
39 bool thresholdsAsserted(sdbusplus::bus::bus& bus,
40 const std::string& service,
41 const std::string& path) override;
42
43 template <typename T>
44 void getProperty(sdbusplus::bus::bus& bus, const std::string& service,
45 const std::string& path, const std::string& interface,
46 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 }
61 catch (const sdbusplus::exception::SdBusError& ex)
62 {
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:
72 sdbusplus::bus::bus _bus;
Patrick Ventureaadb30d2020-08-10 09:17:11 -070073};
74
75} // namespace pid_control