xref: /openbmc/phosphor-pid-control/dbus/dbushelper.hpp (revision 11a1edcd204b87c6258592dc47f3d04e426e66e8)
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 #include <xyz/openbmc_project/Sensor/Threshold/Critical/common.hpp>
9 #include <xyz/openbmc_project/Sensor/Threshold/Warning/common.hpp>
10 #include <xyz/openbmc_project/State/Decorator/Availability/common.hpp>
11 
12 #include <string>
13 #include <variant>
14 
15 using SensorThresholdWarning =
16     sdbusplus::common::xyz::openbmc_project::sensor::threshold::Warning;
17 using SensorThresholdCritical =
18     sdbusplus::common::xyz::openbmc_project::sensor::threshold::Critical;
19 using StateDecoratorAvailability =
20     sdbusplus::common::xyz::openbmc_project::state::decorator::Availability;
21 
22 namespace pid_control
23 {
24 
25 class DbusHelper : public DbusHelperInterface
26 {
27   public:
28     static constexpr char propertiesintf[] = "org.freedesktop.DBus.Properties";
29 
DbusHelper(sdbusplus::bus_t & bus)30     explicit DbusHelper(sdbusplus::bus_t& bus) : _bus(bus) {}
31     DbusHelper() = delete;
32     ~DbusHelper() override = default;
33 
34     DbusHelper(const DbusHelper&) = delete;
35     DbusHelper& operator=(const DbusHelper&) = delete;
36 
37     DbusHelper(DbusHelper&&) = default;
38     DbusHelper& operator=(DbusHelper&&) = delete;
39 
40     std::string getService(const std::string& intf,
41                            const std::string& path) override;
42 
43     void getProperties(const std::string& service, const std::string& path,
44                        SensorProperties* prop) override;
45 
46     bool thresholdsAsserted(const std::string& service,
47                             const std::string& path) override;
48 
49     template <typename T>
getProperty(const std::string & service,const std::string & path,const std::string & interface,const std::string & propertyName,T & prop)50     void getProperty(const std::string& service, const std::string& path,
51                      const std::string& interface,
52                      const std::string& propertyName, T& prop)
53     {
54         namespace log = phosphor::logging;
55 
56         auto msg = _bus.new_method_call(service.c_str(), path.c_str(),
57                                         propertiesintf, "Get");
58 
59         msg.append(interface, propertyName);
60 
61         std::variant<T> result;
62         try
63         {
64             auto valueResponseMsg = _bus.call(msg);
65             valueResponseMsg.read(result);
66         }
67         catch (const sdbusplus::exception_t& ex)
68         {
69             log::log<log::level::ERR>("Get Property Failed",
70                                       log::entry("WHAT=%s", ex.what()));
71             throw;
72         }
73 
74         prop = std::get<T>(result);
75     }
76 
77   private:
78     sdbusplus::bus_t& _bus;
79 };
80 
81 } // namespace pid_control
82