17452a867SVijay Khemka #include <phosphor-logging/elog-errors.hpp> 27452a867SVijay Khemka #include <phosphor-logging/log.hpp> 37452a867SVijay Khemka #include <xyz/openbmc_project/Common/error.hpp> 47452a867SVijay Khemka 57452a867SVijay Khemka const char* propIntf = "org.freedesktop.DBus.Properties"; 67452a867SVijay Khemka const char* mapperBusName = "xyz.openbmc_project.ObjectMapper"; 77452a867SVijay Khemka const char* mapperPath = "/xyz/openbmc_project/object_mapper"; 87452a867SVijay Khemka const char* mapperIntf = "xyz.openbmc_project.ObjectMapper"; 97452a867SVijay Khemka 107452a867SVijay Khemka const char* methodGetObject = "GetObject"; 117452a867SVijay Khemka const char* methodGet = "Get"; 127452a867SVijay Khemka 137452a867SVijay Khemka using namespace phosphor::logging; 147452a867SVijay Khemka using namespace sdbusplus::xyz::openbmc_project::Common::Error; 157452a867SVijay Khemka 167452a867SVijay Khemka using Value = std::variant<int64_t, double, std::string, bool>; 177452a867SVijay Khemka 187452a867SVijay Khemka std::string getService(sdbusplus::bus::bus& bus, const std::string& path, 197452a867SVijay Khemka const char* intf) 207452a867SVijay Khemka { 217452a867SVijay Khemka /* Get mapper object for sensor path */ 227452a867SVijay Khemka auto mapper = bus.new_method_call(mapperBusName, mapperPath, mapperIntf, 237452a867SVijay Khemka methodGetObject); 247452a867SVijay Khemka 257452a867SVijay Khemka mapper.append(path.c_str()); 267452a867SVijay Khemka mapper.append(std::vector<std::string>({intf})); 277452a867SVijay Khemka 287452a867SVijay Khemka std::unordered_map<std::string, std::vector<std::string>> resp; 297452a867SVijay Khemka 307452a867SVijay Khemka try 317452a867SVijay Khemka { 327452a867SVijay Khemka auto msg = bus.call(mapper); 337452a867SVijay Khemka msg.read(resp); 347452a867SVijay Khemka } 357452a867SVijay Khemka catch (const sdbusplus::exception::SdBusError& ex) 367452a867SVijay Khemka { 37f8db7225SMatt Spinler if (ex.name() == std::string(sdbusplus::xyz::openbmc_project::Common:: 38f8db7225SMatt Spinler Error::ResourceNotFound::errName)) 39f8db7225SMatt Spinler { 40f8db7225SMatt Spinler // The service isn't on D-Bus yet. 41f8db7225SMatt Spinler return std::string{}; 42f8db7225SMatt Spinler } 43f8db7225SMatt Spinler 447452a867SVijay Khemka throw; 457452a867SVijay Khemka } 467452a867SVijay Khemka 477452a867SVijay Khemka if (resp.begin() == resp.end()) 487452a867SVijay Khemka { 49f8db7225SMatt Spinler // Shouldn't happen, if the mapper can't find it it is handled above. 507452a867SVijay Khemka throw std::runtime_error("Unable to find Object: " + path); 517452a867SVijay Khemka } 527452a867SVijay Khemka 537452a867SVijay Khemka return resp.begin()->first; 547452a867SVijay Khemka } 557452a867SVijay Khemka 567452a867SVijay Khemka template <typename T> 577452a867SVijay Khemka 587452a867SVijay Khemka T getDbusProperty(sdbusplus::bus::bus& bus, const std::string& service, 597452a867SVijay Khemka const std::string& path, const std::string& intf, 607452a867SVijay Khemka const std::string& property) 617452a867SVijay Khemka { 627452a867SVijay Khemka 637452a867SVijay Khemka Value value; 647452a867SVijay Khemka 657452a867SVijay Khemka auto method = 667452a867SVijay Khemka bus.new_method_call(service.c_str(), path.c_str(), propIntf, methodGet); 677452a867SVijay Khemka 687452a867SVijay Khemka method.append(intf, property); 697452a867SVijay Khemka 70*187582bdSHarvey Wu try 71*187582bdSHarvey Wu { 727452a867SVijay Khemka auto msg = bus.call(method); 737452a867SVijay Khemka msg.read(value); 74*187582bdSHarvey Wu } 75*187582bdSHarvey Wu catch (const sdbusplus::exception::SdBusError& ex) 76*187582bdSHarvey Wu { 77*187582bdSHarvey Wu return std::numeric_limits<T>::quiet_NaN(); 78*187582bdSHarvey Wu } 797452a867SVijay Khemka 807452a867SVijay Khemka return std::get<T>(value); 817452a867SVijay Khemka } 82