Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 1 | #include <phosphor-logging/elog-errors.hpp> |
| 2 | #include <phosphor-logging/log.hpp> |
| 3 | #include <xyz/openbmc_project/Common/error.hpp> |
| 4 | |
| 5 | const char* propIntf = "org.freedesktop.DBus.Properties"; |
| 6 | const char* mapperBusName = "xyz.openbmc_project.ObjectMapper"; |
| 7 | const char* mapperPath = "/xyz/openbmc_project/object_mapper"; |
| 8 | const char* mapperIntf = "xyz.openbmc_project.ObjectMapper"; |
| 9 | |
| 10 | const char* methodGetObject = "GetObject"; |
| 11 | const char* methodGet = "Get"; |
| 12 | |
| 13 | using namespace phosphor::logging; |
| 14 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
| 15 | |
| 16 | using Value = std::variant<int64_t, double, std::string, bool>; |
| 17 | |
| 18 | std::string getService(sdbusplus::bus::bus& bus, const std::string& path, |
| 19 | const char* intf) |
| 20 | { |
| 21 | /* Get mapper object for sensor path */ |
| 22 | auto mapper = bus.new_method_call(mapperBusName, mapperPath, mapperIntf, |
| 23 | methodGetObject); |
| 24 | |
| 25 | mapper.append(path.c_str()); |
| 26 | mapper.append(std::vector<std::string>({intf})); |
| 27 | |
| 28 | std::unordered_map<std::string, std::vector<std::string>> resp; |
| 29 | |
| 30 | try |
| 31 | { |
| 32 | auto msg = bus.call(mapper); |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 33 | msg.read(resp); |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 34 | } |
| 35 | catch (const sdbusplus::exception::SdBusError& ex) |
| 36 | { |
Matt Spinler | f8db722 | 2020-11-10 08:15:03 -0600 | [diff] [blame] | 37 | if (ex.name() == std::string(sdbusplus::xyz::openbmc_project::Common:: |
| 38 | Error::ResourceNotFound::errName)) |
| 39 | { |
| 40 | // The service isn't on D-Bus yet. |
| 41 | return std::string{}; |
| 42 | } |
| 43 | |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 44 | throw; |
| 45 | } |
| 46 | |
| 47 | if (resp.begin() == resp.end()) |
| 48 | { |
Matt Spinler | f8db722 | 2020-11-10 08:15:03 -0600 | [diff] [blame] | 49 | // Shouldn't happen, if the mapper can't find it it is handled above. |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 50 | throw std::runtime_error("Unable to find Object: " + path); |
| 51 | } |
| 52 | |
| 53 | return resp.begin()->first; |
| 54 | } |
| 55 | |
| 56 | template <typename T> |
| 57 | |
| 58 | T getDbusProperty(sdbusplus::bus::bus& bus, const std::string& service, |
| 59 | const std::string& path, const std::string& intf, |
| 60 | const std::string& property) |
| 61 | { |
| 62 | |
| 63 | Value value; |
| 64 | |
| 65 | auto method = |
| 66 | bus.new_method_call(service.c_str(), path.c_str(), propIntf, methodGet); |
| 67 | |
| 68 | method.append(intf, property); |
| 69 | |
Harvey Wu | 187582b | 2021-03-30 09:42:39 +0800 | [diff] [blame] | 70 | try |
| 71 | { |
| 72 | auto msg = bus.call(method); |
| 73 | msg.read(value); |
| 74 | } |
| 75 | catch (const sdbusplus::exception::SdBusError& ex) |
| 76 | { |
| 77 | return std::numeric_limits<T>::quiet_NaN(); |
| 78 | } |
Vijay Khemka | 7452a86 | 2020-08-11 16:01:23 -0700 | [diff] [blame] | 79 | |
| 80 | return std::get<T>(value); |
| 81 | } |