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