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