blob: 72546e109abc1cec65f50861a689affe2ead9f93 [file] [log] [blame]
Vijay Khemka7452a862020-08-11 16:01:23 -07001#include <phosphor-logging/elog-errors.hpp>
Vijay Khemka7452a862020-08-11 16:01:23 -07002#include <xyz/openbmc_project/Common/error.hpp>
3
4const char* propIntf = "org.freedesktop.DBus.Properties";
5const char* mapperBusName = "xyz.openbmc_project.ObjectMapper";
6const char* mapperPath = "/xyz/openbmc_project/object_mapper";
7const char* mapperIntf = "xyz.openbmc_project.ObjectMapper";
8
9const char* methodGetObject = "GetObject";
10const char* methodGet = "Get";
11
Vijay Khemka7452a862020-08-11 16:01:23 -070012using namespace sdbusplus::xyz::openbmc_project::Common::Error;
13
14using Value = std::variant<int64_t, double, std::string, bool>;
15
16std::string getService(sdbusplus::bus::bus& bus, const std::string& path,
17 const char* intf)
18{
19 /* Get mapper object for sensor path */
20 auto mapper = bus.new_method_call(mapperBusName, mapperPath, mapperIntf,
21 methodGetObject);
22
23 mapper.append(path.c_str());
24 mapper.append(std::vector<std::string>({intf}));
25
26 std::unordered_map<std::string, std::vector<std::string>> resp;
27
28 try
29 {
30 auto msg = bus.call(mapper);
Vijay Khemka7452a862020-08-11 16:01:23 -070031 msg.read(resp);
Vijay Khemka7452a862020-08-11 16:01:23 -070032 }
Patrick Williams74c1e7d2021-09-02 09:50:55 -050033 catch (const sdbusplus::exception::exception& ex)
Vijay Khemka7452a862020-08-11 16:01:23 -070034 {
Matt Spinlerf8db7222020-11-10 08:15:03 -060035 if (ex.name() == std::string(sdbusplus::xyz::openbmc_project::Common::
36 Error::ResourceNotFound::errName))
37 {
38 // The service isn't on D-Bus yet.
39 return std::string{};
40 }
41
Vijay Khemka7452a862020-08-11 16:01:23 -070042 throw;
43 }
44
45 if (resp.begin() == resp.end())
46 {
Matt Spinlerf8db7222020-11-10 08:15:03 -060047 // Shouldn't happen, if the mapper can't find it it is handled above.
Vijay Khemka7452a862020-08-11 16:01:23 -070048 throw std::runtime_error("Unable to find Object: " + path);
49 }
50
51 return resp.begin()->first;
52}
53
54template <typename T>
55
56T getDbusProperty(sdbusplus::bus::bus& bus, const std::string& service,
57 const std::string& path, const std::string& intf,
58 const std::string& property)
59{
60
61 Value value;
62
63 auto method =
64 bus.new_method_call(service.c_str(), path.c_str(), propIntf, methodGet);
65
66 method.append(intf, property);
67
Harvey Wu187582b2021-03-30 09:42:39 +080068 try
69 {
70 auto msg = bus.call(method);
71 msg.read(value);
72 }
Patrick Williams74c1e7d2021-09-02 09:50:55 -050073 catch (const sdbusplus::exception::exception& ex)
Harvey Wu187582b2021-03-30 09:42:39 +080074 {
75 return std::numeric_limits<T>::quiet_NaN();
76 }
Vijay Khemka7452a862020-08-11 16:01:23 -070077
78 return std::get<T>(value);
79}