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