blob: b664c48c5ecf78e0bca5ffb5df1982b1f8ca91f9 [file] [log] [blame]
Vijay Khemka7452a862020-08-11 16:01:23 -07001#include <phosphor-logging/elog-errors.hpp>
Tao Lin91799db2022-07-27 21:02:20 +08002#include <phosphor-logging/lg2.hpp>
Vijay Khemka7452a862020-08-11 16:01:23 -07003#include <xyz/openbmc_project/Common/error.hpp>
4
Tao Lin91799db2022-07-27 21:02:20 +08005const constexpr char* entityManagerBusName =
6 "xyz.openbmc_project.EntityManager";
Vijay Khemka7452a862020-08-11 16:01:23 -07007const char* propIntf = "org.freedesktop.DBus.Properties";
8const char* mapperBusName = "xyz.openbmc_project.ObjectMapper";
9const char* mapperPath = "/xyz/openbmc_project/object_mapper";
10const char* mapperIntf = "xyz.openbmc_project.ObjectMapper";
11
12const char* methodGetObject = "GetObject";
13const char* methodGet = "Get";
Tao Lin91799db2022-07-27 21:02:20 +080014const char* methodSet = "Set";
Vijay Khemka7452a862020-08-11 16:01:23 -070015
Vijay Khemka7452a862020-08-11 16:01:23 -070016using namespace sdbusplus::xyz::openbmc_project::Common::Error;
17
18using Value = std::variant<int64_t, double, std::string, bool>;
19
Patrick Williams8e11ccc2022-07-22 19:26:57 -050020std::string getService(sdbusplus::bus_t& bus, const std::string& path,
Vijay Khemka7452a862020-08-11 16:01:23 -070021 const char* intf)
22{
23 /* Get mapper object for sensor path */
24 auto mapper = bus.new_method_call(mapperBusName, mapperPath, mapperIntf,
25 methodGetObject);
26
27 mapper.append(path.c_str());
28 mapper.append(std::vector<std::string>({intf}));
29
30 std::unordered_map<std::string, std::vector<std::string>> resp;
31
32 try
33 {
34 auto msg = bus.call(mapper);
Vijay Khemka7452a862020-08-11 16:01:23 -070035 msg.read(resp);
Vijay Khemka7452a862020-08-11 16:01:23 -070036 }
Patrick Williams8e11ccc2022-07-22 19:26:57 -050037 catch (const sdbusplus::exception_t& ex)
Vijay Khemka7452a862020-08-11 16:01:23 -070038 {
Matt Spinlerf8db7222020-11-10 08:15:03 -060039 if (ex.name() == std::string(sdbusplus::xyz::openbmc_project::Common::
40 Error::ResourceNotFound::errName))
41 {
42 // The service isn't on D-Bus yet.
43 return std::string{};
44 }
45
Vijay Khemka7452a862020-08-11 16:01:23 -070046 throw;
47 }
48
49 if (resp.begin() == resp.end())
50 {
Matt Spinlerf8db7222020-11-10 08:15:03 -060051 // Shouldn't happen, if the mapper can't find it it is handled above.
Vijay Khemka7452a862020-08-11 16:01:23 -070052 throw std::runtime_error("Unable to find Object: " + path);
53 }
54
55 return resp.begin()->first;
56}
57
58template <typename T>
59
Patrick Williams8e11ccc2022-07-22 19:26:57 -050060T getDbusProperty(sdbusplus::bus_t& bus, const std::string& service,
Vijay Khemka7452a862020-08-11 16:01:23 -070061 const std::string& path, const std::string& intf,
62 const std::string& property)
63{
Vijay Khemka7452a862020-08-11 16:01:23 -070064 Value value;
65
66 auto method =
67 bus.new_method_call(service.c_str(), path.c_str(), propIntf, methodGet);
68
69 method.append(intf, property);
70
Harvey Wu187582b2021-03-30 09:42:39 +080071 try
72 {
73 auto msg = bus.call(method);
74 msg.read(value);
75 }
Patrick Williams8e11ccc2022-07-22 19:26:57 -050076 catch (const sdbusplus::exception_t& ex)
Harvey Wu187582b2021-03-30 09:42:39 +080077 {
78 return std::numeric_limits<T>::quiet_NaN();
79 }
Vijay Khemka7452a862020-08-11 16:01:23 -070080
81 return std::get<T>(value);
82}
Tao Lin91799db2022-07-27 21:02:20 +080083
84int setDbusProperty(sdbusplus::bus_t& bus, const std::string& service,
85 const std::string& path, const std::string& intf,
86 const std::string& property, const Value& value)
87{
88 try
89 {
90 auto method = bus.new_method_call(service.c_str(), path.c_str(),
91 propIntf, methodSet);
92 method.append(intf, property, value);
93 auto msg = bus.call(method);
94 }
95 catch (const sdbusplus::exception_t& e)
96 {
97 lg2::error(
98 "Faild to set dbus property. service:{SERVICE} path:{PATH} intf:{INTF} Property:{PROP},{ERRMSG}",
99 "SERVICE", service, "PATH", path, "INTF", intf, "PROP", property,
100 "ERRMSG", e);
101 return -1;
102 }
103
104 return 0;
105}