blob: a12a21dea9536e5a1b6e14a90131619759011d5b [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 {
43 log<level::ERR>("ObjectMapper call failure",
44 entry("WHAT=%s", ex.what()));
45 throw;
46 }
47
48 if (resp.begin() == resp.end())
49 {
50 throw std::runtime_error("Unable to find Object: " + path);
51 }
52
53 return resp.begin()->first;
54}
55
56template <typename T>
57
58T 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
70 auto msg = bus.call(method);
71
72 if (msg.is_method_error())
73 {
74 log<level::ERR>("Failed to get property",
75 entry("PROPERTY=%s", property.c_str()),
76 entry("PATH=%s", path.c_str()),
77 entry("INTERFACE=%s", intf.c_str()));
78 elog<InternalFailure>();
79 }
80
81 msg.read(value);
82
83 return std::get<T>(value);
84}