1*aadb30ddSPatrick Venture #pragma once
2*aadb30ddSPatrick Venture 
3*aadb30ddSPatrick Venture #include "dbushelper_interface.hpp"
4*aadb30ddSPatrick Venture 
5*aadb30ddSPatrick Venture #include <phosphor-logging/log.hpp>
6*aadb30ddSPatrick Venture #include <sdbusplus/bus.hpp>
7*aadb30ddSPatrick Venture 
8*aadb30ddSPatrick Venture #include <string>
9*aadb30ddSPatrick Venture #include <variant>
10*aadb30ddSPatrick Venture 
11*aadb30ddSPatrick Venture namespace pid_control
12*aadb30ddSPatrick Venture {
13*aadb30ddSPatrick Venture 
14*aadb30ddSPatrick Venture class DbusHelper : public DbusHelperInterface
15*aadb30ddSPatrick Venture {
16*aadb30ddSPatrick Venture   public:
17*aadb30ddSPatrick Venture     static constexpr char sensorintf[] = "xyz.openbmc_project.Sensor.Value";
18*aadb30ddSPatrick Venture     static constexpr char propertiesintf[] = "org.freedesktop.DBus.Properties";
19*aadb30ddSPatrick Venture     static constexpr char criticalThreshInf[] =
20*aadb30ddSPatrick Venture         "xyz.openbmc_project.Sensor.Threshold.Critical";
21*aadb30ddSPatrick Venture 
22*aadb30ddSPatrick Venture     DbusHelper() = default;
23*aadb30ddSPatrick Venture     ~DbusHelper() = default;
24*aadb30ddSPatrick Venture     DbusHelper(const DbusHelper&) = default;
25*aadb30ddSPatrick Venture     DbusHelper& operator=(const DbusHelper&) = default;
26*aadb30ddSPatrick Venture     DbusHelper(DbusHelper&&) = default;
27*aadb30ddSPatrick Venture     DbusHelper& operator=(DbusHelper&&) = default;
28*aadb30ddSPatrick Venture 
29*aadb30ddSPatrick Venture     std::string getService(sdbusplus::bus::bus& bus, const std::string& intf,
30*aadb30ddSPatrick Venture                            const std::string& path) override;
31*aadb30ddSPatrick Venture 
32*aadb30ddSPatrick Venture     void getProperties(sdbusplus::bus::bus& bus, const std::string& service,
33*aadb30ddSPatrick Venture                        const std::string& path,
34*aadb30ddSPatrick Venture                        struct SensorProperties* prop) override;
35*aadb30ddSPatrick Venture 
36*aadb30ddSPatrick Venture     bool thresholdsAsserted(sdbusplus::bus::bus& bus,
37*aadb30ddSPatrick Venture                             const std::string& service,
38*aadb30ddSPatrick Venture                             const std::string& path) override;
39*aadb30ddSPatrick Venture 
40*aadb30ddSPatrick Venture     template <typename T>
41*aadb30ddSPatrick Venture     void getProperty(sdbusplus::bus::bus& bus, const std::string& service,
42*aadb30ddSPatrick Venture                      const std::string& path, const std::string& interface,
43*aadb30ddSPatrick Venture                      const std::string& propertyName, T& prop)
44*aadb30ddSPatrick Venture     {
45*aadb30ddSPatrick Venture         namespace log = phosphor::logging;
46*aadb30ddSPatrick Venture 
47*aadb30ddSPatrick Venture         auto msg = bus.new_method_call(service.c_str(), path.c_str(),
48*aadb30ddSPatrick Venture                                        propertiesintf, "Get");
49*aadb30ddSPatrick Venture 
50*aadb30ddSPatrick Venture         msg.append(interface, propertyName);
51*aadb30ddSPatrick Venture 
52*aadb30ddSPatrick Venture         std::variant<T> result;
53*aadb30ddSPatrick Venture         try
54*aadb30ddSPatrick Venture         {
55*aadb30ddSPatrick Venture             auto valueResponseMsg = bus.call(msg);
56*aadb30ddSPatrick Venture             valueResponseMsg.read(result);
57*aadb30ddSPatrick Venture         }
58*aadb30ddSPatrick Venture         catch (const sdbusplus::exception::SdBusError& ex)
59*aadb30ddSPatrick Venture         {
60*aadb30ddSPatrick Venture             log::log<log::level::ERR>("Get Property Failed",
61*aadb30ddSPatrick Venture                                       log::entry("WHAT=%s", ex.what()));
62*aadb30ddSPatrick Venture             throw;
63*aadb30ddSPatrick Venture         }
64*aadb30ddSPatrick Venture 
65*aadb30ddSPatrick Venture         prop = std::get<T>(result);
66*aadb30ddSPatrick Venture     }
67*aadb30ddSPatrick Venture };
68*aadb30ddSPatrick Venture 
69*aadb30ddSPatrick Venture } // namespace pid_control
70