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