blob: 4f99872f9f4dc36cded88898b04dbd9fdb718139 [file] [log] [blame]
Tao Linf2e94222023-10-31 17:38:17 +08001#pragma once
2
Vijay Khemka7452a862020-08-11 16:01:23 -07003#include <phosphor-logging/elog-errors.hpp>
Tao Lin91799db2022-07-27 21:02:20 +08004#include <phosphor-logging/lg2.hpp>
Vijay Khemka7452a862020-08-11 16:01:23 -07005#include <xyz/openbmc_project/Common/error.hpp>
6
Vijay Khemka7452a862020-08-11 16:01:23 -07007using namespace sdbusplus::xyz::openbmc_project::Common::Error;
8
9using Value = std::variant<int64_t, double, std::string, bool>;
10
Patrick Williams8e11ccc2022-07-22 19:26:57 -050011std::string getService(sdbusplus::bus_t& bus, const std::string& path,
Tao Linf2e94222023-10-31 17:38:17 +080012 const char* intf);
Vijay Khemka7452a862020-08-11 16:01:23 -070013
14template <typename T>
15
Patrick Williams8e11ccc2022-07-22 19:26:57 -050016T getDbusProperty(sdbusplus::bus_t& bus, const std::string& service,
Vijay Khemka7452a862020-08-11 16:01:23 -070017 const std::string& path, const std::string& intf,
18 const std::string& property)
19{
Vijay Khemka7452a862020-08-11 16:01:23 -070020 Value value;
21
Tao Linf2e94222023-10-31 17:38:17 +080022 auto method = bus.new_method_call(service.c_str(), path.c_str(),
23 "org.freedesktop.DBus.Properties", "Get");
Vijay Khemka7452a862020-08-11 16:01:23 -070024
25 method.append(intf, property);
26
Harvey Wu187582b2021-03-30 09:42:39 +080027 try
28 {
29 auto msg = bus.call(method);
30 msg.read(value);
31 }
Patrick Williams8e11ccc2022-07-22 19:26:57 -050032 catch (const sdbusplus::exception_t& ex)
Harvey Wu187582b2021-03-30 09:42:39 +080033 {
34 return std::numeric_limits<T>::quiet_NaN();
35 }
Vijay Khemka7452a862020-08-11 16:01:23 -070036
37 return std::get<T>(value);
38}
Tao Lin91799db2022-07-27 21:02:20 +080039
40int setDbusProperty(sdbusplus::bus_t& bus, const std::string& service,
41 const std::string& path, const std::string& intf,
Tao Linf2e94222023-10-31 17:38:17 +080042 const std::string& property, const Value& value);