blob: f718e6d303795a9df1b06cadfca14735c01fc5f3 [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);
Vijay Khemka7452a862020-08-11 16:01:23 -070033 msg.read(resp);
Vijay Khemka7452a862020-08-11 16:01:23 -070034 }
35 catch (const sdbusplus::exception::SdBusError& ex)
36 {
Matt Spinlerf8db7222020-11-10 08:15:03 -060037 if (ex.name() == std::string(sdbusplus::xyz::openbmc_project::Common::
38 Error::ResourceNotFound::errName))
39 {
40 // The service isn't on D-Bus yet.
41 return std::string{};
42 }
43
Vijay Khemka7452a862020-08-11 16:01:23 -070044 throw;
45 }
46
47 if (resp.begin() == resp.end())
48 {
Matt Spinlerf8db7222020-11-10 08:15:03 -060049 // Shouldn't happen, if the mapper can't find it it is handled above.
Vijay Khemka7452a862020-08-11 16:01:23 -070050 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
Harvey Wu187582b2021-03-30 09:42:39 +080070 try
71 {
72 auto msg = bus.call(method);
73 msg.read(value);
74 }
75 catch (const sdbusplus::exception::SdBusError& ex)
76 {
77 return std::numeric_limits<T>::quiet_NaN();
78 }
Vijay Khemka7452a862020-08-11 16:01:23 -070079
80 return std::get<T>(value);
81}