blob: 7ae60f1df2c4b0321be806d77d23c89d07169030 [file] [log] [blame]
Patrick Ventureaadb30d2020-08-10 09:17:11 -07001#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
11namespace pid_control
12{
13
14class 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
22 DbusHelper() = default;
23 ~DbusHelper() = default;
24 DbusHelper(const DbusHelper&) = default;
25 DbusHelper& operator=(const DbusHelper&) = default;
26 DbusHelper(DbusHelper&&) = default;
27 DbusHelper& operator=(DbusHelper&&) = default;
28
29 std::string getService(sdbusplus::bus::bus& bus, const std::string& intf,
30 const std::string& path) override;
31
32 void getProperties(sdbusplus::bus::bus& bus, const std::string& service,
33 const std::string& path,
34 struct SensorProperties* prop) override;
35
36 bool thresholdsAsserted(sdbusplus::bus::bus& bus,
37 const std::string& service,
38 const std::string& path) override;
39
40 template <typename T>
41 void getProperty(sdbusplus::bus::bus& bus, const std::string& service,
42 const std::string& path, const std::string& interface,
43 const std::string& propertyName, T& prop)
44 {
45 namespace log = phosphor::logging;
46
47 auto msg = bus.new_method_call(service.c_str(), path.c_str(),
48 propertiesintf, "Get");
49
50 msg.append(interface, propertyName);
51
52 std::variant<T> result;
53 try
54 {
55 auto valueResponseMsg = bus.call(msg);
56 valueResponseMsg.read(result);
57 }
58 catch (const sdbusplus::exception::SdBusError& ex)
59 {
60 log::log<log::level::ERR>("Get Property Failed",
61 log::entry("WHAT=%s", ex.what()));
62 throw;
63 }
64
65 prop = std::get<T>(result);
66 }
67};
68
69} // namespace pid_control