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); 33 34 msg.read(resp); 35 if (msg.is_method_error()) 36 { 37 log<level::ERR>("Error in mapper call"); 38 elog<InternalFailure>(); 39 } 40 } 41 catch (const sdbusplus::exception::SdBusError& ex) 42 { 43 if (ex.name() == std::string(sdbusplus::xyz::openbmc_project::Common:: 44 Error::ResourceNotFound::errName)) 45 { 46 // The service isn't on D-Bus yet. 47 return std::string{}; 48 } 49 50 throw; 51 } 52 53 if (resp.begin() == resp.end()) 54 { 55 // Shouldn't happen, if the mapper can't find it it is handled above. 56 throw std::runtime_error("Unable to find Object: " + path); 57 } 58 59 return resp.begin()->first; 60 } 61 62 template <typename T> 63 64 T getDbusProperty(sdbusplus::bus::bus& bus, const std::string& service, 65 const std::string& path, const std::string& intf, 66 const std::string& property) 67 { 68 69 Value value; 70 71 auto method = 72 bus.new_method_call(service.c_str(), path.c_str(), propIntf, methodGet); 73 74 method.append(intf, property); 75 76 auto msg = bus.call(method); 77 78 if (msg.is_method_error()) 79 { 80 log<level::ERR>("Failed to get property", 81 entry("PROPERTY=%s", property.c_str()), 82 entry("PATH=%s", path.c_str()), 83 entry("INTERFACE=%s", intf.c_str())); 84 elog<InternalFailure>(); 85 } 86 87 msg.read(value); 88 89 return std::get<T>(value); 90 } 91