blob: 0495ceb18378a882ab77a824dd74bb6e8172b6c3 [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
Patrick Williams8e11ccc2022-07-22 19:26:57 -050016std::string getService(sdbusplus::bus_t& bus, const std::string& path,
Vijay Khemka7452a862020-08-11 16:01:23 -070017 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 Williams8e11ccc2022-07-22 19:26:57 -050033 catch (const sdbusplus::exception_t& 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
Patrick Williams8e11ccc2022-07-22 19:26:57 -050056T getDbusProperty(sdbusplus::bus_t& bus, const std::string& service,
Vijay Khemka7452a862020-08-11 16:01:23 -070057 const std::string& path, const std::string& intf,
58 const std::string& property)
59{
Vijay Khemka7452a862020-08-11 16:01:23 -070060 Value value;
61
62 auto method =
63 bus.new_method_call(service.c_str(), path.c_str(), propIntf, methodGet);
64
65 method.append(intf, property);
66
Harvey Wu187582b2021-03-30 09:42:39 +080067 try
68 {
69 auto msg = bus.call(method);
70 msg.read(value);
71 }
Patrick Williams8e11ccc2022-07-22 19:26:57 -050072 catch (const sdbusplus::exception_t& ex)
Harvey Wu187582b2021-03-30 09:42:39 +080073 {
74 return std::numeric_limits<T>::quiet_NaN();
75 }
Vijay Khemka7452a862020-08-11 16:01:23 -070076
77 return std::get<T>(value);
78}