blob: 7f6f3f3a933934b14a0be674b36935eafc5d59c1 [file] [log] [blame]
Vijay Khemka7452a862020-08-11 16:01:23 -07001#include <phosphor-logging/elog-errors.hpp>
2#include <phosphor-logging/log.hpp>
3#include <xyz/openbmc_project/Common/error.hpp>
4
5const char* propIntf = "org.freedesktop.DBus.Properties";
6const char* mapperBusName = "xyz.openbmc_project.ObjectMapper";
7const char* mapperPath = "/xyz/openbmc_project/object_mapper";
8const char* mapperIntf = "xyz.openbmc_project.ObjectMapper";
9
10const char* methodGetObject = "GetObject";
11const char* methodGet = "Get";
12
13using namespace phosphor::logging;
14using namespace sdbusplus::xyz::openbmc_project::Common::Error;
15
16using Value = std::variant<int64_t, double, std::string, bool>;
17
18std::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 {
Matt Spinlerf8db7222020-11-10 08:15:03 -060043 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
Vijay Khemka7452a862020-08-11 16:01:23 -070050 throw;
51 }
52
53 if (resp.begin() == resp.end())
54 {
Matt Spinlerf8db7222020-11-10 08:15:03 -060055 // Shouldn't happen, if the mapper can't find it it is handled above.
Vijay Khemka7452a862020-08-11 16:01:23 -070056 throw std::runtime_error("Unable to find Object: " + path);
57 }
58
59 return resp.begin()->first;
60}
61
62template <typename T>
63
64T 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}