Tao Lin | f2e9422 | 2023-10-31 17:38:17 +0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 3 | #include <phosphor-logging/elog-errors.hpp> |
Tao Lin | 91799db | 2022-07-27 21:02:20 +0800 | [diff] [blame] | 4 | #include <phosphor-logging/lg2.hpp> |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 5 | #include <xyz/openbmc_project/Common/error.hpp> |
| 6 | |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 7 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 8 | |
| 9 | using Value = std::variant<int64_t, double, std::string, bool>; |
| 10 | |
Patrick Williams | 8e11ccc | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 11 | std::string getService(sdbusplus::bus_t& bus, const std::string& path, |
Tao Lin | f2e9422 | 2023-10-31 17:38:17 +0800 | [diff] [blame] | 12 | const char* intf); |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 13 | |
| 14 | template <typename T> |
| 15 | |
Patrick Williams | 8e11ccc | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 16 | T getDbusProperty(sdbusplus::bus_t& bus, const std::string& service, |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 17 | const std::string& path, const std::string& intf, |
| 18 | const std::string& property) |
| 19 | { |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 20 | Value value; |
| 21 | |
Tao Lin | f2e9422 | 2023-10-31 17:38:17 +0800 | [diff] [blame] | 22 | auto method = bus.new_method_call(service.c_str(), path.c_str(), |
| 23 | "org.freedesktop.DBus.Properties", "Get"); |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 24 | |
| 25 | method.append(intf, property); |
| 26 | |
Harvey Wu | 187582b | 2021-03-30 09:42:39 +0800 | [diff] [blame] | 27 | try |
| 28 | { |
| 29 | auto msg = bus.call(method); |
| 30 | msg.read(value); |
| 31 | } |
Patrick Williams | 8e11ccc | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 32 | catch (const sdbusplus::exception_t& ex) |
Harvey Wu | 187582b | 2021-03-30 09:42:39 +0800 | [diff] [blame] | 33 | { |
| 34 | return std::numeric_limits<T>::quiet_NaN(); |
| 35 | } |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 36 | |
| 37 | return std::get<T>(value); |
| 38 | } |
Tao Lin | 91799db | 2022-07-27 21:02:20 +0800 | [diff] [blame] | 39 | |
| 40 | int setDbusProperty(sdbusplus::bus_t& bus, const std::string& service, |
| 41 | const std::string& path, const std::string& intf, |
Tao Lin | f2e9422 | 2023-10-31 17:38:17 +0800 | [diff] [blame] | 42 | const std::string& property, const Value& value); |