blob: 7d2be2d00da55255dc4aa5b0a3eaa8f7706f06b0 [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";
Jonico Eustaquioaf97d8e2024-01-02 14:35:07 -060021 static constexpr char warningThreshInf[] =
22 "xyz.openbmc_project.Sensor.Threshold.Warning";
Alex.Song8f73ad72021-10-07 00:18:27 +080023 static constexpr char availabilityIntf[] =
24 "xyz.openbmc_project.State.Decorator.Availability";
Patrick Ventureaadb30d2020-08-10 09:17:11 -070025
Patrick Williams8c051122023-05-10 07:50:59 -050026 explicit DbusHelper(sdbusplus::bus_t bus) : _bus(std::move(bus)) {}
Patrick Ventureaadb30d2020-08-10 09:17:11 -070027 ~DbusHelper() = default;
Patrick Venture8729eb92020-08-10 10:38:44 -070028
29 DbusHelper(const DbusHelper&) = delete;
30 DbusHelper& operator=(const DbusHelper&) = delete;
31
Patrick Ventureaadb30d2020-08-10 09:17:11 -070032 DbusHelper(DbusHelper&&) = default;
33 DbusHelper& operator=(DbusHelper&&) = default;
34
Patrick Venture9b936922020-08-10 11:28:39 -070035 std::string getService(const std::string& intf,
Patrick Ventureaadb30d2020-08-10 09:17:11 -070036 const std::string& path) override;
37
Patrick Venture9b936922020-08-10 11:28:39 -070038 void getProperties(const std::string& service, const std::string& path,
Patrick Venture1df9e872020-10-08 15:35:01 -070039 SensorProperties* prop) override;
Patrick Ventureaadb30d2020-08-10 09:17:11 -070040
Patrick Venture9b936922020-08-10 11:28:39 -070041 bool thresholdsAsserted(const std::string& service,
Patrick Ventureaadb30d2020-08-10 09:17:11 -070042 const std::string& path) override;
43
44 template <typename T>
Patrick Venture9b936922020-08-10 11:28:39 -070045 void getProperty(const std::string& service, const std::string& path,
46 const std::string& interface,
Patrick Ventureaadb30d2020-08-10 09:17:11 -070047 const std::string& propertyName, T& prop)
48 {
49 namespace log = phosphor::logging;
50
Patrick Venture8729eb92020-08-10 10:38:44 -070051 auto msg = _bus.new_method_call(service.c_str(), path.c_str(),
52 propertiesintf, "Get");
Patrick Ventureaadb30d2020-08-10 09:17:11 -070053
54 msg.append(interface, propertyName);
55
56 std::variant<T> result;
57 try
58 {
Patrick Venture8729eb92020-08-10 10:38:44 -070059 auto valueResponseMsg = _bus.call(msg);
Patrick Ventureaadb30d2020-08-10 09:17:11 -070060 valueResponseMsg.read(result);
61 }
Patrick Williamsb228bc32022-07-22 19:26:56 -050062 catch (const sdbusplus::exception_t& ex)
Patrick Ventureaadb30d2020-08-10 09:17:11 -070063 {
64 log::log<log::level::ERR>("Get Property Failed",
65 log::entry("WHAT=%s", ex.what()));
66 throw;
67 }
68
69 prop = std::get<T>(result);
70 }
Patrick Venture8729eb92020-08-10 10:38:44 -070071
72 private:
Patrick Williamsb228bc32022-07-22 19:26:56 -050073 sdbusplus::bus_t _bus;
Patrick Ventureaadb30d2020-08-10 09:17:11 -070074};
75
76} // namespace pid_control