George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 1 | #include "utils.hpp" |
| 2 | |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 3 | #include <phosphor-logging/elog-errors.hpp> |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 4 | #include <sdbusplus/bus.hpp> |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 5 | #include <xyz/openbmc_project/Common/error.hpp> |
George Liu | b5ca101 | 2021-09-10 12:53:11 +0800 | [diff] [blame] | 6 | |
| 7 | #include <string> |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 8 | namespace open_power |
| 9 | { |
| 10 | namespace occ |
| 11 | { |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 12 | namespace utils |
| 13 | { |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 14 | // For throwing exceptions |
| 15 | using namespace phosphor::logging; |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 16 | using InternalFailure = |
| 17 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 18 | |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 19 | const std::string getService(const std::string& path, |
| 20 | const std::string& interface) |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 21 | { |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 22 | |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 23 | using InterfaceList = std::vector<std::string>; |
| 24 | std::map<std::string, std::vector<std::string>> mapperResponse; |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 25 | |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 26 | auto& bus = getBus(); |
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 mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH, |
| 29 | MAPPER_IFACE, "GetObject"); |
| 30 | mapper.append(path, InterfaceList({interface})); |
| 31 | |
| 32 | auto mapperResponseMsg = bus.call(mapper); |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 33 | if (mapperResponseMsg.is_method_error()) |
| 34 | { |
| 35 | log<level::ERR>("ERROR in getting service", |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 36 | entry("PATH=%s", path.c_str()), |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 37 | entry("INTERFACE=%s", interface.c_str())); |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 38 | |
| 39 | elog<InternalFailure>(); |
| 40 | } |
| 41 | |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 42 | mapperResponseMsg.read(mapperResponse); |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 43 | if (mapperResponse.empty()) |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 44 | { |
| 45 | log<level::ERR>("ERROR reading mapper response", |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 46 | entry("PATH=%s", path.c_str()), |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 47 | entry("INTERFACE=%s", interface.c_str())); |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 48 | |
| 49 | elog<InternalFailure>(); |
| 50 | } |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 51 | |
| 52 | // the value here will be the service name |
| 53 | return mapperResponse.cbegin()->first; |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 54 | } |
| 55 | |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 56 | const PropertyValue getProperty(const std::string& objectPath, |
| 57 | const std::string& interface, |
| 58 | const std::string& propertyName) |
| 59 | { |
| 60 | PropertyValue value{}; |
| 61 | |
| 62 | auto& bus = getBus(); |
| 63 | auto service = getService(objectPath, interface); |
| 64 | if (service.empty()) |
| 65 | { |
| 66 | return value; |
| 67 | } |
| 68 | |
| 69 | auto method = bus.new_method_call(service.c_str(), objectPath.c_str(), |
| 70 | DBUS_PROPERTY_IFACE, "Get"); |
| 71 | method.append(interface, propertyName); |
| 72 | |
| 73 | auto reply = bus.call(method); |
| 74 | reply.read(value); |
| 75 | |
| 76 | return value; |
| 77 | } |
| 78 | |
Matt Spinler | 5901abd | 2021-09-23 13:50:03 -0500 | [diff] [blame] | 79 | std::vector<std::string> |
| 80 | getSubtreePaths(const std::vector<std::string>& interfaces, |
| 81 | const std::string& path) |
| 82 | { |
| 83 | std::vector<std::string> paths; |
| 84 | |
| 85 | auto& bus = getBus(); |
| 86 | auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH, |
| 87 | MAPPER_IFACE, "GetSubTreePaths"); |
| 88 | method.append(path, 0, interfaces); |
| 89 | |
| 90 | auto reply = bus.call(method); |
| 91 | reply.read(paths); |
| 92 | |
| 93 | return paths; |
| 94 | } |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 95 | } // namespace utils |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 96 | } // namespace occ |
| 97 | } // namespace open_power |