1aadb30ddSPatrick Venture #pragma once
2aadb30ddSPatrick Venture 
3aadb30ddSPatrick Venture #include "dbushelper_interface.hpp"
4aadb30ddSPatrick Venture 
5aadb30ddSPatrick Venture #include <phosphor-logging/log.hpp>
6aadb30ddSPatrick Venture #include <sdbusplus/bus.hpp>
7aadb30ddSPatrick Venture 
8aadb30ddSPatrick Venture #include <string>
9aadb30ddSPatrick Venture #include <variant>
10aadb30ddSPatrick Venture 
11aadb30ddSPatrick Venture namespace pid_control
12aadb30ddSPatrick Venture {
13aadb30ddSPatrick Venture 
14aadb30ddSPatrick Venture class DbusHelper : public DbusHelperInterface
15aadb30ddSPatrick Venture {
16aadb30ddSPatrick Venture   public:
17aadb30ddSPatrick Venture     static constexpr char sensorintf[] = "xyz.openbmc_project.Sensor.Value";
18aadb30ddSPatrick Venture     static constexpr char propertiesintf[] = "org.freedesktop.DBus.Properties";
19aadb30ddSPatrick Venture     static constexpr char criticalThreshInf[] =
20aadb30ddSPatrick Venture         "xyz.openbmc_project.Sensor.Threshold.Critical";
21aadb30ddSPatrick Venture 
228729eb98SPatrick Venture     explicit DbusHelper(sdbusplus::bus::bus bus) : _bus(std::move(bus))
238729eb98SPatrick Venture     {}
24aadb30ddSPatrick Venture     ~DbusHelper() = default;
258729eb98SPatrick Venture 
268729eb98SPatrick Venture     DbusHelper(const DbusHelper&) = delete;
278729eb98SPatrick Venture     DbusHelper& operator=(const DbusHelper&) = delete;
288729eb98SPatrick Venture 
29aadb30ddSPatrick Venture     DbusHelper(DbusHelper&&) = default;
30aadb30ddSPatrick Venture     DbusHelper& operator=(DbusHelper&&) = default;
31aadb30ddSPatrick Venture 
329b93692dSPatrick Venture     std::string getService(const std::string& intf,
33aadb30ddSPatrick Venture                            const std::string& path) override;
34aadb30ddSPatrick Venture 
359b93692dSPatrick Venture     void getProperties(const std::string& service, const std::string& path,
36*1df9e879SPatrick Venture                        SensorProperties* prop) override;
37aadb30ddSPatrick Venture 
389b93692dSPatrick Venture     bool thresholdsAsserted(const std::string& service,
39aadb30ddSPatrick Venture                             const std::string& path) override;
40aadb30ddSPatrick Venture 
41aadb30ddSPatrick Venture     template <typename T>
429b93692dSPatrick Venture     void getProperty(const std::string& service, const std::string& path,
439b93692dSPatrick Venture                      const std::string& interface,
44aadb30ddSPatrick Venture                      const std::string& propertyName, T& prop)
45aadb30ddSPatrick Venture     {
46aadb30ddSPatrick Venture         namespace log = phosphor::logging;
47aadb30ddSPatrick Venture 
488729eb98SPatrick Venture         auto msg = _bus.new_method_call(service.c_str(), path.c_str(),
49aadb30ddSPatrick Venture                                         propertiesintf, "Get");
50aadb30ddSPatrick Venture 
51aadb30ddSPatrick Venture         msg.append(interface, propertyName);
52aadb30ddSPatrick Venture 
53aadb30ddSPatrick Venture         std::variant<T> result;
54aadb30ddSPatrick Venture         try
55aadb30ddSPatrick Venture         {
568729eb98SPatrick Venture             auto valueResponseMsg = _bus.call(msg);
57aadb30ddSPatrick Venture             valueResponseMsg.read(result);
58aadb30ddSPatrick Venture         }
59aadb30ddSPatrick Venture         catch (const sdbusplus::exception::SdBusError& ex)
60aadb30ddSPatrick Venture         {
61aadb30ddSPatrick Venture             log::log<log::level::ERR>("Get Property Failed",
62aadb30ddSPatrick Venture                                       log::entry("WHAT=%s", ex.what()));
63aadb30ddSPatrick Venture             throw;
64aadb30ddSPatrick Venture         }
65aadb30ddSPatrick Venture 
66aadb30ddSPatrick Venture         prop = std::get<T>(result);
67aadb30ddSPatrick Venture     }
688729eb98SPatrick Venture 
698729eb98SPatrick Venture   private:
708729eb98SPatrick Venture     sdbusplus::bus::bus _bus;
71aadb30ddSPatrick Venture };
72aadb30ddSPatrick Venture 
73aadb30ddSPatrick Venture } // namespace pid_control
74