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