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 | * |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 75 | * @param[in] objectPath - Name of the object containing the property |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 76 | * @param[in] interface - Interface name containing the property |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 77 | * @param[in] propertyName - Property name |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 78 | * @param[in] value - Property value |
| 79 | */ |
| 80 | void setProperty(const std::string& objectPath, const std::string& interface, |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 81 | const std::string& propertyName, PropertyValue&& value) |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 82 | { |
| 83 | using namespace std::literals::string_literals; |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 84 | PropertyValue varValue(std::forward<PropertyValue>(value)); |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 85 | |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 86 | try |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 87 | { |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 88 | auto& bus = getBus(); |
| 89 | auto service = getService(objectPath, interface); |
| 90 | if (service.empty()) |
| 91 | { |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | auto method = bus.new_method_call(service.c_str(), objectPath.c_str(), |
| 96 | DBUS_PROPERTY_IFACE, "Set"); |
| 97 | method.append(interface, propertyName, varValue); |
| 98 | |
| 99 | auto reply = bus.call(method); |
| 100 | if (reply.is_method_error()) |
| 101 | { |
| 102 | log<level::ERR>( |
| 103 | fmt::format("util::setProperty: Failed to set property {}", |
| 104 | propertyName) |
| 105 | .c_str()); |
| 106 | } |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 107 | } |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 108 | catch (const std::exception& e) |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 109 | { |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 110 | auto error = errno; |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 111 | log<level::ERR>( |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 112 | fmt::format("setProperty: failed to Set {}, errno={}, what={}", |
| 113 | propertyName.c_str(), error, e.what()) |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 114 | .c_str()); |
| 115 | } |
| 116 | } |
| 117 | |
Matt Spinler | 5901abd | 2021-09-23 13:50:03 -0500 | [diff] [blame] | 118 | std::vector<std::string> |
| 119 | getSubtreePaths(const std::vector<std::string>& interfaces, |
| 120 | const std::string& path) |
| 121 | { |
| 122 | std::vector<std::string> paths; |
| 123 | |
| 124 | auto& bus = getBus(); |
| 125 | auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH, |
| 126 | MAPPER_IFACE, "GetSubTreePaths"); |
| 127 | method.append(path, 0, interfaces); |
| 128 | |
| 129 | auto reply = bus.call(method); |
| 130 | reply.read(paths); |
| 131 | |
| 132 | return paths; |
| 133 | } |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 134 | |
| 135 | // Get the service and object path for an interface |
| 136 | std::string getServiceUsingSubTree(const std::string& interface, |
| 137 | std::string& path) |
| 138 | { |
| 139 | using Path = std::string; |
| 140 | using Intf = std::string; |
| 141 | using Serv = std::string; |
| 142 | using Intfs = std::vector<Intf>; |
| 143 | using Objects = std::map<Path, std::map<Serv, Intfs>>; |
| 144 | Serv service; |
| 145 | Objects rspObjects; |
| 146 | |
| 147 | auto& bus = getBus(); |
| 148 | auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH, |
| 149 | MAPPER_IFACE, "GetSubTree"); |
| 150 | method.append(path, 0, std::vector{interface}); |
| 151 | |
| 152 | auto mapperResponseMsg = bus.call(method); |
| 153 | mapperResponseMsg.read(rspObjects); |
| 154 | if (rspObjects.empty()) |
| 155 | { |
| 156 | log<level::ERR>( |
| 157 | fmt::format( |
| 158 | "util::getServiceUsingSubTree: Failed getSubTree({},0,{})", |
| 159 | path.c_str(), interface) |
| 160 | .c_str()); |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | path = rspObjects.begin()->first; |
| 165 | if (!rspObjects.begin()->second.empty()) |
| 166 | { |
| 167 | service = rspObjects.begin()->second.begin()->first; |
| 168 | } |
| 169 | else |
| 170 | { |
| 171 | log<level::ERR>( |
| 172 | fmt::format( |
| 173 | "getServiceUsingSubTree: service not found for interface {} (path={})", |
| 174 | interface, path.c_str()) |
| 175 | .c_str()); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | return service; |
| 180 | } |
| 181 | |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 182 | } // namespace utils |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 183 | } // namespace occ |
| 184 | } // namespace open_power |