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(std::move(bus)) {}
27     ~DbusHelper() = default;
28 
29     DbusHelper(const DbusHelper&) = delete;
30     DbusHelper& operator=(const DbusHelper&) = delete;
31 
32     DbusHelper(DbusHelper&&) = default;
33     DbusHelper& operator=(DbusHelper&&) = default;
34 
35     std::string getService(const std::string& intf,
36                            const std::string& path) override;
37 
38     void getProperties(const std::string& service, const std::string& path,
39                        SensorProperties* prop) override;
40 
41     bool thresholdsAsserted(const std::string& service,
42                             const std::string& path) override;
43 
44     template <typename T>
getProperty(const std::string & service,const std::string & path,const std::string & interface,const std::string & propertyName,T & prop)45     void getProperty(const std::string& service, const std::string& path,
46                      const std::string& interface,
47                      const std::string& propertyName, T& prop)
48     {
49         namespace log = phosphor::logging;
50 
51         auto msg = _bus.new_method_call(service.c_str(), path.c_str(),
52                                         propertiesintf, "Get");
53 
54         msg.append(interface, propertyName);
55 
56         std::variant<T> result;
57         try
58         {
59             auto valueResponseMsg = _bus.call(msg);
60             valueResponseMsg.read(result);
61         }
62         catch (const sdbusplus::exception_t& ex)
63         {
64             log::log<log::level::ERR>("Get Property Failed",
65                                       log::entry("WHAT=%s", ex.what()));
66             throw;
67         }
68 
69         prop = std::get<T>(result);
70     }
71 
72   private:
73     sdbusplus::bus_t _bus;
74 };
75 
76 } // namespace pid_control
77