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