xref: /openbmc/phosphor-pid-control/dbus/dbushelper.hpp (revision f8b6e55147148c3cfb42327ff267197a460b411c)
1 #pragma once
2 
3 #include "dbushelper_interface.hpp"
4 
5 #include <phosphor-logging/log.hpp>
6 #include <sdbusplus/bus.hpp>
7 #include <sdbusplus/exception.hpp>
8 
9 #include <string>
10 #include <variant>
11 
12 namespace pid_control
13 {
14 
15 class 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";
22     static constexpr char warningThreshInf[] =
23         "xyz.openbmc_project.Sensor.Threshold.Warning";
24     static constexpr char availabilityIntf[] =
25         "xyz.openbmc_project.State.Decorator.Availability";
26 
DbusHelper(sdbusplus::bus_t & bus)27     explicit DbusHelper(sdbusplus::bus_t& bus) : _bus(bus) {}
28     DbusHelper() = delete;
29     ~DbusHelper() override = default;
30 
31     DbusHelper(const DbusHelper&) = delete;
32     DbusHelper& operator=(const DbusHelper&) = delete;
33 
34     DbusHelper(DbusHelper&&) = default;
35     DbusHelper& operator=(DbusHelper&&) = delete;
36 
37     std::string getService(const std::string& intf,
38                            const std::string& path) override;
39 
40     void getProperties(const std::string& service, const std::string& path,
41                        SensorProperties* prop) override;
42 
43     bool thresholdsAsserted(const std::string& service,
44                             const std::string& path) override;
45 
46     template <typename T>
getProperty(const std::string & service,const std::string & path,const std::string & interface,const std::string & propertyName,T & prop)47     void getProperty(const std::string& service, const std::string& path,
48                      const std::string& interface,
49                      const std::string& propertyName, T& prop)
50     {
51         namespace log = phosphor::logging;
52 
53         auto msg = _bus.new_method_call(service.c_str(), path.c_str(),
54                                         propertiesintf, "Get");
55 
56         msg.append(interface, propertyName);
57 
58         std::variant<T> result;
59         try
60         {
61             auto valueResponseMsg = _bus.call(msg);
62             valueResponseMsg.read(result);
63         }
64         catch (const sdbusplus::exception_t& ex)
65         {
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     }
73 
74   private:
75     sdbusplus::bus_t& _bus;
76 };
77 
78 } // namespace pid_control
79