Patrick Williams | 029c23e | 2025-03-03 14:12:40 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 3 | #include <phosphor-logging/elog-errors.hpp> |
| 4 | #include <phosphor-logging/elog.hpp> |
| 5 | #include <phosphor-logging/log.hpp> |
| 6 | #include <sdbusplus/bus.hpp> |
| 7 | #include <sdbusplus/bus/match.hpp> |
| 8 | #include <sdbusplus/message.hpp> |
| 9 | #include <xyz/openbmc_project/Common/error.hpp> |
| 10 | |
Patrick Williams | 7da9858 | 2023-05-10 07:50:46 -0500 | [diff] [blame] | 11 | #include <iostream> |
| 12 | |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 13 | namespace phosphor |
| 14 | { |
| 15 | namespace nvme |
| 16 | { |
| 17 | namespace util |
| 18 | { |
| 19 | |
| 20 | using namespace phosphor::logging; |
| 21 | |
| 22 | class SDBusPlus |
| 23 | { |
| 24 | public: |
| 25 | template <typename T> |
Patrick Williams | 7ce68bc | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 26 | static auto setProperty(sdbusplus::bus_t& bus, const std::string& busName, |
| 27 | const std::string& objPath, |
| 28 | const std::string& interface, |
| 29 | const std::string& property, const T& value) |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 30 | { |
Patrick Williams | 05eedaa | 2020-05-13 17:58:42 -0500 | [diff] [blame] | 31 | std::variant<T> data = value; |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 32 | |
| 33 | try |
| 34 | { |
| 35 | auto methodCall = bus.new_method_call( |
| 36 | busName.c_str(), objPath.c_str(), DBUS_PROPERTY_IFACE, "Set"); |
| 37 | |
| 38 | methodCall.append(interface.c_str()); |
| 39 | methodCall.append(property); |
| 40 | methodCall.append(data); |
| 41 | |
| 42 | auto reply = bus.call(methodCall); |
| 43 | } |
| 44 | catch (const std::exception& e) |
| 45 | { |
Brandon Kim | 9b771e2 | 2020-10-05 12:02:01 -0700 | [diff] [blame] | 46 | log<level::ERR>("Set properties fail.", entry("ERROR=%s", e.what()), |
| 47 | entry("OBJ_PATH=%s", objPath.c_str())); |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 48 | return; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | template <typename Property> |
Patrick Williams | 7ce68bc | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 53 | static auto getProperty(sdbusplus::bus_t& bus, const std::string& busName, |
| 54 | const std::string& objPath, |
| 55 | const std::string& interface, |
| 56 | const std::string& property) |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 57 | { |
| 58 | auto methodCall = bus.new_method_call(busName.c_str(), objPath.c_str(), |
| 59 | DBUS_PROPERTY_IFACE, "Get"); |
| 60 | |
| 61 | methodCall.append(interface.c_str()); |
| 62 | methodCall.append(property); |
| 63 | |
Patrick Williams | 05eedaa | 2020-05-13 17:58:42 -0500 | [diff] [blame] | 64 | std::variant<Property> value; |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 65 | |
| 66 | try |
| 67 | { |
| 68 | auto reply = bus.call(methodCall); |
| 69 | reply.read(value); |
| 70 | } |
| 71 | catch (const std::exception& e) |
| 72 | { |
Brandon Kim | 9b771e2 | 2020-10-05 12:02:01 -0700 | [diff] [blame] | 73 | log<level::ERR>("Get properties fail.", entry("ERROR=%s", e.what()), |
| 74 | entry("OBJ_PATH=%s", objPath.c_str())); |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 75 | return false; |
| 76 | } |
| 77 | |
Patrick Williams | c80e6e2 | 2020-05-13 11:50:34 -0500 | [diff] [blame] | 78 | return std::get<Property>(value); |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | template <typename... Args> |
Patrick Williams | 7ce68bc | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 82 | static auto CallMethod(sdbusplus::bus_t& bus, const std::string& busName, |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 83 | const std::string& objPath, |
| 84 | const std::string& interface, |
| 85 | const std::string& method, Args&&... args) |
| 86 | { |
| 87 | auto reqMsg = bus.new_method_call(busName.c_str(), objPath.c_str(), |
| 88 | interface.c_str(), method.c_str()); |
| 89 | reqMsg.append(std::forward<Args>(args)...); |
| 90 | try |
| 91 | { |
| 92 | auto respMsg = bus.call(reqMsg); |
| 93 | } |
| 94 | catch (const std::exception& e) |
| 95 | { |
Brandon Kim | 9b771e2 | 2020-10-05 12:02:01 -0700 | [diff] [blame] | 96 | log<level::ERR>("Call method fail.", entry("ERROR=%s", e.what()), |
| 97 | entry("OBJ_PATH=%s", objPath.c_str())); |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 98 | return; |
| 99 | } |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | } // namespace util |
| 104 | } // namespace nvme |
| 105 | } // namespace phosphor |