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 | mapperResponseMsg.read(mapperResponse); |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 34 | if (mapperResponse.empty()) |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 35 | { |
| 36 | log<level::ERR>("ERROR reading mapper response", |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 37 | entry("PATH=%s", path.c_str()), |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 38 | entry("INTERFACE=%s", interface.c_str())); |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 39 | |
| 40 | elog<InternalFailure>(); |
| 41 | } |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 42 | |
| 43 | // the value here will be the service name |
| 44 | return mapperResponse.cbegin()->first; |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 45 | } |
| 46 | |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 47 | const PropertyValue getProperty(const std::string& objectPath, |
| 48 | const std::string& interface, |
| 49 | const std::string& propertyName) |
| 50 | { |
| 51 | PropertyValue value{}; |
| 52 | |
| 53 | auto& bus = getBus(); |
| 54 | auto service = getService(objectPath, interface); |
| 55 | if (service.empty()) |
| 56 | { |
| 57 | return value; |
| 58 | } |
| 59 | |
| 60 | auto method = bus.new_method_call(service.c_str(), objectPath.c_str(), |
| 61 | DBUS_PROPERTY_IFACE, "Get"); |
| 62 | method.append(interface, propertyName); |
| 63 | |
| 64 | auto reply = bus.call(method); |
| 65 | reply.read(value); |
| 66 | |
| 67 | return value; |
| 68 | } |
| 69 | |
Matt Spinler | 5901abd | 2021-09-23 13:50:03 -0500 | [diff] [blame] | 70 | std::vector<std::string> |
| 71 | getSubtreePaths(const std::vector<std::string>& interfaces, |
| 72 | const std::string& path) |
| 73 | { |
| 74 | std::vector<std::string> paths; |
| 75 | |
| 76 | auto& bus = getBus(); |
| 77 | auto method = bus.new_method_call(MAPPER_BUSNAME, MAPPER_OBJ_PATH, |
| 78 | MAPPER_IFACE, "GetSubTreePaths"); |
| 79 | method.append(path, 0, interfaces); |
| 80 | |
| 81 | auto reply = bus.call(method); |
| 82 | reply.read(paths); |
| 83 | |
| 84 | return paths; |
| 85 | } |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 86 | } // namespace utils |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 87 | } // namespace occ |
| 88 | } // namespace open_power |