George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 1 | #include "utils.hpp" |
| 2 | |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 3 | #include <fmt/core.h> |
| 4 | |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 5 | #include <phosphor-logging/elog-errors.hpp> |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 6 | #include <sdbusplus/bus.hpp> |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 7 | #include <xyz/openbmc_project/Common/error.hpp> |
George Liu | b5ca101 | 2021-09-10 12:53:11 +0800 | [diff] [blame] | 8 | |
| 9 | #include <string> |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 10 | namespace open_power |
| 11 | { |
| 12 | namespace occ |
| 13 | { |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 14 | namespace utils |
| 15 | { |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 16 | // For throwing exceptions |
| 17 | using namespace phosphor::logging; |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 18 | using InternalFailure = |
| 19 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 20 | |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 21 | const std::string getService(const std::string& path, |
| 22 | const std::string& interface) |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 23 | { |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 24 | |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 25 | using InterfaceList = std::vector<std::string>; |
| 26 | std::map<std::string, std::vector<std::string>> mapperResponse; |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 27 | |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 28 | auto& bus = getBus(); |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 29 | |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 30 | auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH, |
| 31 | MAPPER_IFACE, "GetObject"); |
| 32 | mapper.append(path, InterfaceList({interface})); |
| 33 | |
| 34 | auto mapperResponseMsg = bus.call(mapper); |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 35 | mapperResponseMsg.read(mapperResponse); |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 36 | if (mapperResponse.empty()) |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 37 | { |
| 38 | log<level::ERR>("ERROR reading mapper response", |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 39 | entry("PATH=%s", path.c_str()), |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 40 | entry("INTERFACE=%s", interface.c_str())); |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 41 | |
| 42 | elog<InternalFailure>(); |
| 43 | } |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 44 | |
| 45 | // the value here will be the service name |
| 46 | return mapperResponse.cbegin()->first; |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 47 | } |
| 48 | |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 49 | const PropertyValue getProperty(const std::string& objectPath, |
| 50 | const std::string& interface, |
| 51 | const std::string& propertyName) |
| 52 | { |
| 53 | PropertyValue value{}; |
| 54 | |
| 55 | auto& bus = getBus(); |
| 56 | auto service = getService(objectPath, interface); |
| 57 | if (service.empty()) |
| 58 | { |
| 59 | return value; |
| 60 | } |
| 61 | |
| 62 | auto method = bus.new_method_call(service.c_str(), objectPath.c_str(), |
| 63 | DBUS_PROPERTY_IFACE, "Get"); |
| 64 | method.append(interface, propertyName); |
| 65 | |
| 66 | auto reply = bus.call(method); |
| 67 | reply.read(value); |
| 68 | |
| 69 | return value; |
| 70 | } |
| 71 | |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 72 | /** |
| 73 | * @brief Sets a given object's property value |
| 74 | * |
| 75 | * @param[in] object - Name of the object containing the property |
| 76 | * @param[in] interface - Interface name containing the property |
| 77 | * @param[in] property - Property name |
| 78 | * @param[in] value - Property value |
| 79 | */ |
| 80 | void setProperty(const std::string& objectPath, const std::string& interface, |
| 81 | const std::string& propertyName, std::string&& value) |
| 82 | { |
| 83 | using namespace std::literals::string_literals; |
| 84 | std::variant<std::string> varValue(std::forward<std::string>(value)); |
| 85 | |
| 86 | auto& bus = getBus(); |
| 87 | auto service = getService(objectPath, interface); |
| 88 | if (service.empty()) |
| 89 | { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | auto method = bus.new_method_call(service.c_str(), objectPath.c_str(), |
| 94 | DBUS_PROPERTY_IFACE, "Set"); |
| 95 | method.append(interface, propertyName, varValue); |
| 96 | |
| 97 | auto reply = bus.call(method); |
| 98 | if (reply.is_method_error()) |
| 99 | { |
| 100 | log<level::ERR>( |
| 101 | fmt::format("util::setProperty: Failed to set property {}", |
| 102 | propertyName) |
| 103 | .c_str()); |
| 104 | } |
| 105 | } |
| 106 | |
Matt Spinler | 5901abd | 2021-09-23 13:50:03 -0500 | [diff] [blame] | 107 | std::vector<std::string> |
| 108 | getSubtreePaths(const std::vector<std::string>& interfaces, |
| 109 | const std::string& path) |
| 110 | { |
| 111 | std::vector<std::string> paths; |
| 112 | |
| 113 | auto& bus = getBus(); |
| 114 | auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH, |
| 115 | MAPPER_IFACE, "GetSubTreePaths"); |
| 116 | method.append(path, 0, interfaces); |
| 117 | |
| 118 | auto reply = bus.call(method); |
| 119 | reply.read(paths); |
| 120 | |
| 121 | return paths; |
| 122 | } |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 123 | } // namespace utils |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 124 | } // namespace occ |
| 125 | } // namespace open_power |